mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
Import a simple text editor I started working on.
This commit is contained in:
parent
405383fd2f
commit
ca6847b5bb
Notes:
sideshowbarker
2024-07-19 16:08:48 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/ca6847b5bb6
59 changed files with 895 additions and 39 deletions
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
#include "Types.h"
|
||||
#include "kmalloc.h"
|
||||
#include "Assertions.h"
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "Assertions.h"
|
||||
#include "Retainable.h"
|
||||
#include "RetainPtr.h"
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
#include "kmalloc.h"
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "Buffer.h"
|
||||
#include "Types.h"
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "AKString.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
#include "Assertions.h"
|
||||
#include "OwnPtr.h"
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "HashTable.h"
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
#include "kstdio.h"
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "Assertions.h"
|
||||
#include "DoublyLinkedList.h"
|
||||
#include "Traits.h"
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
#include "kstdio.h"
|
||||
|
||||
//#define HASHTABLE_DEBUG
|
||||
|
|
|
@ -5,7 +5,7 @@ CXXFLAGS = -std=c++17 -O0 -W -Wall -ggdb3
|
|||
|
||||
all: $(PROGRAM)
|
||||
|
||||
test.o: Vector.h String.h StringImpl.h MappedFile.h HashTable.h SinglyLinkedList.h Traits.h HashMap.h TemporaryFile.h Buffer.h FileSystemPath.h StringBuilder.h
|
||||
test.o: Vector.h AKString.h StringImpl.h MappedFile.h HashTable.h SinglyLinkedList.h Traits.h HashMap.h TemporaryFile.h Buffer.h FileSystemPath.h StringBuilder.h
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "AKString.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
|
6
AK/Noncopyable.h
Normal file
6
AK/Noncopyable.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#define AK_MAKE_NONCOPYABLE(c) \
|
||||
private: \
|
||||
c(const c&) = delete; \
|
||||
c& operator=(const c&) = delete;
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
#include "Types.h"
|
||||
#include "Traits.h"
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "String.h"
|
||||
#include "StdLib.h"
|
||||
#include "AKString.h"
|
||||
#include "StdLibExtras.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "AKString.h"
|
||||
#include "Vector.h"
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "StringImpl.h"
|
||||
#include "StdLib.h"
|
||||
#include "StdLibExtras.h"
|
||||
#include "kmalloc.h"
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "AKString.h"
|
||||
#include <stdio.h>
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "String.h"
|
||||
#include "AKString.h"
|
||||
//#include "StringBuilder.h"
|
||||
#include "Vector.h"
|
||||
#include <stdio.h>
|
||||
|
|
46
Editor/Document.cpp
Normal file
46
Editor/Document.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include "Document.h"
|
||||
#include "FileReader.h"
|
||||
|
||||
OwnPtr<Document> Document::create_from_file(const std::string& path)
|
||||
{
|
||||
auto document = make<Document>();
|
||||
|
||||
FileReader reader(path);
|
||||
while (reader.can_read()) {
|
||||
auto line = reader.read_line();
|
||||
document->m_lines.push_back(Line(line));
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
void Document::dump()
|
||||
{
|
||||
fprintf(stderr, "Document{%p}\n", this);
|
||||
for (size_t i = 0; i < m_lines.size(); ++i) {
|
||||
fprintf(stderr, "[%02zu] %s\n", i, m_lines[i].data().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool Document::backspace_at(Position position)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Document::insert_at(Position position, const std::string& text)
|
||||
{
|
||||
static FILE* f = fopen("log", "a");
|
||||
fprintf(f, "@%zu,%zu: +%s\n", position.line(), position.column(), text.c_str());
|
||||
fflush(f);
|
||||
ASSERT(position.is_valid());
|
||||
if (!position.is_valid())
|
||||
return false;
|
||||
ASSERT(position.line() < line_count());
|
||||
if (position.line() >= line_count())
|
||||
return false;
|
||||
Line& line = m_lines[position.line()];
|
||||
if (position.column() > line.length())
|
||||
return false;
|
||||
line.insert(position.column(), text);
|
||||
return true;
|
||||
}
|
27
Editor/Document.h
Normal file
27
Editor/Document.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "cuki.h"
|
||||
#include "Line.h"
|
||||
#include "Position.h"
|
||||
#include "OwnPtr.h"
|
||||
#include <string>
|
||||
|
||||
class Document {
|
||||
public:
|
||||
Document() { }
|
||||
~Document() { }
|
||||
|
||||
const std::deque<Line>& lines() const { return m_lines; }
|
||||
std::deque<Line>& lines() { return m_lines; }
|
||||
size_t line_count() const { return m_lines.size(); }
|
||||
|
||||
static OwnPtr<Document> create_from_file(const std::string& path);
|
||||
|
||||
bool insert_at(Position, const std::string&);
|
||||
bool backspace_at(Position);
|
||||
|
||||
void dump();
|
||||
|
||||
private:
|
||||
std::deque<Line> m_lines;
|
||||
};
|
336
Editor/Editor.cpp
Normal file
336
Editor/Editor.cpp
Normal file
|
@ -0,0 +1,336 @@
|
|||
#include "Editor.h"
|
||||
#include "Document.h"
|
||||
#include "InsertOperation.h"
|
||||
|
||||
#define _XOPEN_SOURCE_EXTENDED
|
||||
#include <locale.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
static int statusbar_attributes;
|
||||
static int ruler_attributes;
|
||||
|
||||
Editor::Editor()
|
||||
{
|
||||
setlocale(LC_ALL, "");
|
||||
initscr();
|
||||
start_color();
|
||||
use_default_colors();
|
||||
|
||||
init_pair(1, COLOR_WHITE, COLOR_BLUE);
|
||||
init_pair(2, COLOR_BLUE, -1);
|
||||
|
||||
statusbar_attributes = COLOR_PAIR(1);
|
||||
ruler_attributes = COLOR_PAIR(2);
|
||||
|
||||
raw();
|
||||
keypad(stdscr, true);
|
||||
timeout(10);
|
||||
noecho();
|
||||
refresh();
|
||||
}
|
||||
|
||||
Editor::~Editor()
|
||||
{
|
||||
//move(2, 2);
|
||||
//printw("*** Press any key to exit! ***");
|
||||
//getch();
|
||||
endwin();
|
||||
}
|
||||
|
||||
void Editor::set_document(OwnPtr<Document>&& document)
|
||||
{
|
||||
m_document = std::move(document);
|
||||
m_cursor.move_to(0, 0);
|
||||
m_scroll_position.move_to(0, 0);
|
||||
}
|
||||
|
||||
void Editor::redraw()
|
||||
{
|
||||
clear();
|
||||
if (!m_document)
|
||||
return;
|
||||
|
||||
size_t window_height = getmaxy(stdscr);
|
||||
size_t window_width = getmaxx(stdscr);
|
||||
|
||||
for (size_t row = 0; row < window_height - 1; ++row) {
|
||||
size_t current_document_line = m_scroll_position.line() + row;
|
||||
size_t current_document_column = m_scroll_position.column();
|
||||
|
||||
move(row, 0);
|
||||
|
||||
if (current_document_line >= m_document->line_count()) {
|
||||
printw("~");
|
||||
} else {
|
||||
attron(ruler_attributes);
|
||||
printw("%3d ", current_document_line);
|
||||
attroff(ruler_attributes);
|
||||
m_ruler_width = 4;
|
||||
size_t line_length = m_document->lines()[current_document_line].data().size();
|
||||
const char* line_data = m_document->lines()[current_document_line].data().c_str();
|
||||
if (m_scroll_position.column() < line_length)
|
||||
addnstr(line_data + m_scroll_position.column(), window_width - m_ruler_width);
|
||||
}
|
||||
}
|
||||
|
||||
draw_status_bar();
|
||||
|
||||
draw_cursor();
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Editor::draw_cursor()
|
||||
{
|
||||
ssize_t cursor_row_on_screen = m_cursor.line() - m_scroll_position.line();
|
||||
ssize_t cursor_column_on_screen = m_cursor.column() - m_scroll_position.column();
|
||||
|
||||
move(cursor_row_on_screen, cursor_column_on_screen + m_ruler_width);
|
||||
|
||||
}
|
||||
|
||||
void Editor::draw_status_bar()
|
||||
{
|
||||
int old_background = getbkgd(stdscr);
|
||||
bkgdset(' ' | statusbar_attributes);
|
||||
|
||||
size_t window_height = getmaxy(stdscr);
|
||||
size_t window_width = getmaxx(stdscr);
|
||||
|
||||
move(window_height - 1, 0);
|
||||
clrtoeol();
|
||||
|
||||
if (is_editing_document()) {
|
||||
attron(A_STANDOUT);
|
||||
printw("* Editing *");
|
||||
attroff(A_STANDOUT);
|
||||
} else if (is_editing_command()) {
|
||||
printw("\\%s", m_command.c_str());
|
||||
} else {
|
||||
attron(A_BOLD);
|
||||
addstr("~(^_^)~ ");
|
||||
if (m_status_text.size() > 0) {
|
||||
addstr(m_status_text.c_str());
|
||||
}
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
|
||||
move(window_height - 1, window_width - 20);
|
||||
printw("%zu, %zu", m_scroll_position.line(), m_scroll_position.column());
|
||||
|
||||
move(window_height - 1, window_width - 8);
|
||||
printw("%zu, %zu", m_cursor.line(), m_cursor.column());
|
||||
attroff(statusbar_attributes);
|
||||
|
||||
bkgdset(old_background);
|
||||
}
|
||||
|
||||
int Editor::exec()
|
||||
{
|
||||
while (!m_should_quit) {
|
||||
redraw();
|
||||
int ch = getch();
|
||||
if (ch == ERR) {
|
||||
continue;
|
||||
fprintf(stderr, "getch() returned ERR\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_editing_document() || is_editing_command()) {
|
||||
if (ch == 27)
|
||||
set_mode(Idle);
|
||||
else {
|
||||
if (is_editing_document())
|
||||
handle_document_key_press(ch);
|
||||
else
|
||||
handle_command_key_press(ch);
|
||||
}
|
||||
} else {
|
||||
switch (ch) {
|
||||
case 'h': move_left(); break;
|
||||
case 'j': move_down(); break;
|
||||
case 'k': move_up(); break;
|
||||
case 'l': move_right(); break;
|
||||
case 'i': set_mode(EditingDocument); break;
|
||||
case 'q': m_should_quit = true; break;
|
||||
case '\\': set_mode(EditingCommand); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Editor::move_left()
|
||||
{
|
||||
if (m_cursor.column() == 0)
|
||||
return;
|
||||
m_cursor.move_by(0, -1);
|
||||
update_scroll_position_if_needed();
|
||||
}
|
||||
|
||||
void Editor::move_down()
|
||||
{
|
||||
if (m_cursor.line() >= max_line())
|
||||
return;
|
||||
m_cursor.move_by(1, 0);
|
||||
if (m_cursor.column() > max_column())
|
||||
m_cursor.set_column(max_column());
|
||||
|
||||
update_scroll_position_if_needed();
|
||||
}
|
||||
|
||||
void Editor::move_up()
|
||||
{
|
||||
if (m_cursor.line() == 0)
|
||||
return;
|
||||
m_cursor.move_by(-1, 0);
|
||||
if (m_cursor.column() > max_column())
|
||||
m_cursor.set_column(max_column());
|
||||
|
||||
update_scroll_position_if_needed();
|
||||
}
|
||||
|
||||
void Editor::move_right()
|
||||
{
|
||||
if (m_cursor.column() >= max_column())
|
||||
return;
|
||||
m_cursor.move_by(0, 1);
|
||||
update_scroll_position_if_needed();
|
||||
}
|
||||
|
||||
size_t Editor::max_line() const
|
||||
{
|
||||
return m_document->line_count() - 1;
|
||||
}
|
||||
|
||||
size_t Editor::max_column() const
|
||||
{
|
||||
return m_document->lines()[m_cursor.line()].data().size();
|
||||
}
|
||||
|
||||
void Editor::update_scroll_position_if_needed()
|
||||
{
|
||||
ssize_t max_row = getmaxy(stdscr) - 2;
|
||||
ssize_t max_column = getmaxx(stdscr) - 1 - m_ruler_width;
|
||||
|
||||
ssize_t cursor_row_on_screen = m_cursor.line() - m_scroll_position.line();
|
||||
ssize_t cursor_column_on_screen = m_cursor.column() - m_scroll_position.column();
|
||||
|
||||
// FIXME: Need to move by more than just 1 step sometimes!
|
||||
|
||||
if (cursor_row_on_screen < 0) {
|
||||
m_scroll_position.move_by(-1, 0);
|
||||
}
|
||||
|
||||
if (cursor_row_on_screen > max_row) {
|
||||
m_scroll_position.move_by(1, 0);
|
||||
}
|
||||
|
||||
if (cursor_column_on_screen < 0) {
|
||||
m_scroll_position.move_by(0, -1);
|
||||
}
|
||||
|
||||
if (cursor_column_on_screen > max_column) {
|
||||
m_scroll_position.move_by(0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::set_mode(Mode m)
|
||||
{
|
||||
if (m_mode == m)
|
||||
return;
|
||||
m_mode = m;
|
||||
m_command = "";
|
||||
}
|
||||
|
||||
static bool is_backspace(int ch)
|
||||
{
|
||||
return ch == 8 || ch == 127;
|
||||
}
|
||||
|
||||
static bool is_newline(int ch)
|
||||
{
|
||||
return ch == 10 || ch == 13;
|
||||
}
|
||||
|
||||
void Editor::handle_command_key_press(int ch)
|
||||
{
|
||||
if (is_backspace(ch)) {
|
||||
if (m_command.size() > 0)
|
||||
m_command.pop_back();
|
||||
else
|
||||
set_mode(Idle);
|
||||
return;
|
||||
}
|
||||
if (is_newline(ch)) {
|
||||
if (m_command.size() > 0)
|
||||
exec_command();
|
||||
set_mode(Idle);
|
||||
return;
|
||||
}
|
||||
m_command.push_back(ch);
|
||||
}
|
||||
|
||||
void Editor::handle_document_key_press(int ch)
|
||||
{
|
||||
if (is_backspace(ch)) {
|
||||
//auto op = make<EraseOperation>(1);
|
||||
m_document->backspace_at(m_cursor);
|
||||
} else {
|
||||
auto op = make<InsertOperation>(ch);
|
||||
run(std::move(op));
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::run(OwnPtr<Operation>&& op)
|
||||
{
|
||||
ASSERT(op);
|
||||
op->apply(*this);
|
||||
m_undo_stack.push(std::move(op));
|
||||
}
|
||||
|
||||
void Editor::insert_at_cursor(int ch)
|
||||
{
|
||||
std::string s;
|
||||
s += ch;
|
||||
m_document->insert_at(m_cursor, s);
|
||||
m_cursor.move_by(0, 1);
|
||||
}
|
||||
|
||||
bool Editor::insert_text_at_cursor(const std::string& text)
|
||||
{
|
||||
ASSERT(text.size() == 1);
|
||||
m_document->insert_at(m_cursor, text);
|
||||
m_cursor.move_by(0, text.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Editor::remove_text_at_cursor(const std::string& text)
|
||||
{
|
||||
// FIXME: Implement
|
||||
ASSERT(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
void Editor::set_status_text(const std::string& text)
|
||||
{
|
||||
m_status_text = text;
|
||||
}
|
||||
|
||||
void Editor::exec_command()
|
||||
{
|
||||
if (m_command == "q") {
|
||||
m_should_quit = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_command == "about") {
|
||||
set_status_text("cuki editor!");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string s;
|
||||
s = "Invalid command: '";
|
||||
s += m_command;
|
||||
s += "'";
|
||||
set_status_text(s);
|
||||
}
|
74
Editor/Editor.h
Normal file
74
Editor/Editor.h
Normal file
|
@ -0,0 +1,74 @@
|
|||
#pragma once
|
||||
|
||||
#include "OwnPtr.h"
|
||||
#include "Position.h"
|
||||
#include "UndoStack.h"
|
||||
#include <string>
|
||||
|
||||
class Document;
|
||||
|
||||
class Editor {
|
||||
public:
|
||||
Editor();
|
||||
~Editor();
|
||||
|
||||
void set_document(OwnPtr<Document>&&);
|
||||
void redraw();
|
||||
|
||||
int exec();
|
||||
|
||||
enum Mode {
|
||||
Idle,
|
||||
EditingCommand,
|
||||
EditingDocument,
|
||||
};
|
||||
|
||||
void set_mode(Mode);
|
||||
Mode mode() const { return m_mode; }
|
||||
bool is_editing_document() const { return m_mode == EditingDocument; }
|
||||
bool is_editing_command() const { return m_mode == EditingCommand; }
|
||||
bool is_idle() const { return m_mode == Idle; }
|
||||
|
||||
void set_status_text(const std::string&);
|
||||
|
||||
bool insert_text_at_cursor(const std::string&);
|
||||
bool remove_text_at_cursor(const std::string&);
|
||||
|
||||
void run(OwnPtr<Operation>&&);
|
||||
|
||||
private:
|
||||
void move_left();
|
||||
void move_down();
|
||||
void move_up();
|
||||
void move_right();
|
||||
|
||||
size_t max_line() const;
|
||||
size_t max_column() const;
|
||||
|
||||
void update_scroll_position_if_needed();
|
||||
|
||||
void draw_status_bar();
|
||||
void draw_cursor();
|
||||
|
||||
void handle_document_key_press(int ch);
|
||||
void handle_command_key_press(int ch);
|
||||
|
||||
void insert_at_cursor(int ch);
|
||||
|
||||
void exec_command();
|
||||
|
||||
OwnPtr<Document> m_document;
|
||||
|
||||
UndoStack m_undo_stack;
|
||||
|
||||
// Document relative
|
||||
Position m_scroll_position;
|
||||
Position m_cursor;
|
||||
|
||||
std::string m_command;
|
||||
std::string m_status_text;
|
||||
|
||||
bool m_should_quit { false };
|
||||
size_t m_ruler_width { 0 };
|
||||
Mode m_mode { Idle };
|
||||
};
|
40
Editor/FileReader.cpp
Normal file
40
Editor/FileReader.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include "FileReader.h"
|
||||
|
||||
FileReader::FileReader(const std::string& path)
|
||||
: m_path(path)
|
||||
{
|
||||
m_file = fopen(path.c_str(), "r");
|
||||
}
|
||||
|
||||
FileReader::~FileReader()
|
||||
{
|
||||
if (m_file)
|
||||
fclose(m_file);
|
||||
m_file = nullptr;
|
||||
}
|
||||
|
||||
bool FileReader::can_read() const
|
||||
{
|
||||
return m_file && !feof(m_file);
|
||||
}
|
||||
|
||||
std::string FileReader::read_line()
|
||||
{
|
||||
if (!m_file) {
|
||||
fprintf(stderr, "Error: FileReader::readLine() called on invalid FileReader for '%s'\n", m_path.c_str());
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string line;
|
||||
|
||||
while (can_read()) {
|
||||
int ch = fgetc(m_file);
|
||||
if (ch == EOF)
|
||||
break;
|
||||
if (ch == '\n')
|
||||
break;
|
||||
line += ch;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
17
Editor/FileReader.h
Normal file
17
Editor/FileReader.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
class FileReader {
|
||||
public:
|
||||
explicit FileReader(const std::string& path);
|
||||
~FileReader();
|
||||
|
||||
bool can_read() const;
|
||||
std::string read_line();
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
FILE* m_file { nullptr };
|
||||
};
|
26
Editor/InsertOperation.cpp
Normal file
26
Editor/InsertOperation.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "InsertOperation.h"
|
||||
#include "Editor.h"
|
||||
|
||||
InsertOperation::InsertOperation(const std::string& text)
|
||||
: m_text(text)
|
||||
{
|
||||
}
|
||||
|
||||
InsertOperation::InsertOperation(char ch)
|
||||
: m_text(&ch, 1)
|
||||
{
|
||||
}
|
||||
|
||||
InsertOperation::~InsertOperation()
|
||||
{
|
||||
}
|
||||
|
||||
bool InsertOperation::apply(Editor& editor)
|
||||
{
|
||||
return editor.insert_text_at_cursor(m_text);
|
||||
}
|
||||
|
||||
bool InsertOperation::unapply(Editor& editor)
|
||||
{
|
||||
return editor.remove_text_at_cursor(m_text);
|
||||
}
|
17
Editor/InsertOperation.h
Normal file
17
Editor/InsertOperation.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include "Operation.h"
|
||||
#include <string>
|
||||
|
||||
class InsertOperation final : public Operation {
|
||||
public:
|
||||
explicit InsertOperation(const std::string&);
|
||||
explicit InsertOperation(char);
|
||||
~InsertOperation();
|
||||
|
||||
virtual bool apply(Editor&) override;
|
||||
virtual bool unapply(Editor&) override;
|
||||
|
||||
private:
|
||||
std::string m_text;
|
||||
};
|
95
Editor/Line.cpp
Normal file
95
Editor/Line.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include "Line.h"
|
||||
|
||||
Chunk::Chunk(const std::string& str)
|
||||
: m_data(str)
|
||||
{
|
||||
}
|
||||
|
||||
Chunk::~Chunk()
|
||||
{
|
||||
}
|
||||
|
||||
Line::Line(const std::string& str)
|
||||
{
|
||||
m_chunks.push_back(Chunk(str));
|
||||
}
|
||||
|
||||
Line::Line(Line&& other)
|
||||
: m_chunks(std::move(other.m_chunks))
|
||||
{
|
||||
}
|
||||
|
||||
Line::~Line()
|
||||
{
|
||||
}
|
||||
|
||||
std::string Line::data() const
|
||||
{
|
||||
std::string str;
|
||||
for (auto& chunk : m_chunks)
|
||||
str += chunk.data();
|
||||
return str;
|
||||
}
|
||||
|
||||
void Line::append(const std::string& text)
|
||||
{
|
||||
m_chunks.push_back(Chunk(text));
|
||||
}
|
||||
|
||||
void Line::prepend(const std::string& text)
|
||||
{
|
||||
m_chunks.push_front(Chunk(text));
|
||||
}
|
||||
|
||||
void Line::insert(size_t index, const std::string& text)
|
||||
{
|
||||
if (index == 0) {
|
||||
prepend(text);
|
||||
return;
|
||||
}
|
||||
|
||||
if (index == length()) {
|
||||
append(text);
|
||||
return;
|
||||
}
|
||||
|
||||
auto chunkAddress = chunkIndexForPosition(index);
|
||||
auto chunkIndex = std::get<0>(chunkAddress);
|
||||
auto& chunk = m_chunks[chunkIndex];
|
||||
auto indexInChunk = std::get<1>(chunkAddress);
|
||||
|
||||
static FILE* f = fopen("log", "a");
|
||||
fprintf(f, "#Column:%zu, Chunk:%zu, Index:%zu\n", index, chunkIndex, indexInChunk);
|
||||
|
||||
auto leftString = chunk.data().substr(0, indexInChunk);
|
||||
auto rightString = chunk.data().substr(indexInChunk, chunk.length() - indexInChunk);
|
||||
|
||||
fprintf(f, "#{\"%s\", \"%s\", \"%s\"}\n", leftString.c_str(), text.c_str(), rightString.c_str());
|
||||
fflush(f);
|
||||
|
||||
Chunk leftChunk { leftString };
|
||||
Chunk midChunk { text };
|
||||
Chunk rightChunk { rightString };
|
||||
|
||||
auto iterator = m_chunks.begin() + chunkIndex;
|
||||
m_chunks.erase(iterator);
|
||||
iterator = m_chunks.begin() + chunkIndex;
|
||||
|
||||
// Note reverse insertion order!
|
||||
m_chunks.insert(iterator, rightChunk);
|
||||
m_chunks.insert(iterator, midChunk);
|
||||
m_chunks.insert(iterator, leftChunk);
|
||||
}
|
||||
|
||||
std::tuple<size_t, size_t> Line::chunkIndexForPosition(size_t position)
|
||||
{
|
||||
ASSERT(position < length());
|
||||
size_t seen { 0 };
|
||||
for (size_t i = 0; i < m_chunks.size(); ++i) {
|
||||
if (position < seen + m_chunks[i].length())
|
||||
return std::make_tuple(i, position - seen);
|
||||
seen += m_chunks[i].length();
|
||||
}
|
||||
ASSERT(false);
|
||||
return std::make_tuple(0, 0);
|
||||
}
|
40
Editor/Line.h
Normal file
40
Editor/Line.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include "cuki.h"
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
class Chunk {
|
||||
public:
|
||||
explicit Chunk(const std::string&);
|
||||
~Chunk();
|
||||
|
||||
const std::string& data() const { return m_data; }
|
||||
size_t length() const { return m_data.size(); }
|
||||
|
||||
private:
|
||||
std::string m_data;
|
||||
};
|
||||
|
||||
class Line {
|
||||
AK_MAKE_NONCOPYABLE(Line);
|
||||
public:
|
||||
Line() { }
|
||||
Line(const std::string&);
|
||||
Line(Line&&);
|
||||
~Line();
|
||||
|
||||
std::string data() const;
|
||||
size_t length() const { return data().size(); }
|
||||
|
||||
void insert(size_t index, const std::string&);
|
||||
|
||||
private:
|
||||
void append(const std::string&);
|
||||
void prepend(const std::string&);
|
||||
|
||||
std::tuple<size_t, size_t> chunkIndexForPosition(size_t);
|
||||
|
||||
std::deque<Chunk> m_chunks;
|
||||
};
|
25
Editor/Makefile
Normal file
25
Editor/Makefile
Normal file
|
@ -0,0 +1,25 @@
|
|||
BINARY = cuki
|
||||
|
||||
OBJS = \
|
||||
Document.o \
|
||||
Line.o \
|
||||
FileReader.o \
|
||||
Editor.o \
|
||||
main.o \
|
||||
InsertOperation.o \
|
||||
Operation.o \
|
||||
UndoStack.o
|
||||
|
||||
CXXFLAGS = -Os -std=c++1z -W -Wall -g -I../AK
|
||||
LDFLAGS = -lncurses
|
||||
|
||||
$(BINARY): $(OBJS)
|
||||
$(CXX) $(LDFLAGS) -o $@ $(OBJS)
|
||||
|
||||
all: $(BINARY)
|
||||
|
||||
clean:
|
||||
rm -f $(BINARY) $(OBJS)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
9
Editor/Operation.cpp
Normal file
9
Editor/Operation.cpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include "Operation.h"
|
||||
|
||||
Operation::Operation()
|
||||
{
|
||||
}
|
||||
|
||||
Operation::~Operation()
|
||||
{
|
||||
}
|
14
Editor/Operation.h
Normal file
14
Editor/Operation.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
class Editor;
|
||||
|
||||
class Operation {
|
||||
public:
|
||||
virtual ~Operation();
|
||||
|
||||
virtual bool apply(Editor&) = 0;
|
||||
virtual bool unapply(Editor&) = 0;
|
||||
|
||||
protected:
|
||||
Operation();
|
||||
};
|
27
Editor/Position.h
Normal file
27
Editor/Position.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
class Position {
|
||||
public:
|
||||
Position() { }
|
||||
Position(size_t line, size_t column) : m_line(line), m_column(column) { }
|
||||
|
||||
size_t line() const { return m_line; }
|
||||
size_t column() const { return m_column; }
|
||||
|
||||
void set_line(size_t l) { m_line = l; }
|
||||
void set_column(size_t c) { m_column = c; }
|
||||
|
||||
void move_to(size_t l, size_t c) { m_line = l; m_column = c; }
|
||||
|
||||
void move_by(ssize_t l, ssize_t c) { m_line += l; m_column += c; }
|
||||
|
||||
bool is_valid() const { return m_line != InvalidValue && m_column != InvalidValue; }
|
||||
|
||||
private:
|
||||
static const size_t InvalidValue = 0xFFFFFFFF;
|
||||
|
||||
size_t m_line { InvalidValue };
|
||||
size_t m_column { InvalidValue };
|
||||
};
|
14
Editor/UndoStack.cpp
Normal file
14
Editor/UndoStack.cpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "cuki.h"
|
||||
#include "UndoStack.h"
|
||||
|
||||
void UndoStack::push(OwnPtr<Operation>&& op)
|
||||
{
|
||||
m_stack.push(std::move(op));
|
||||
}
|
||||
|
||||
OwnPtr<Operation> UndoStack::pop()
|
||||
{
|
||||
OwnPtr<Operation> op = std::move(m_stack.top());
|
||||
m_stack.pop();
|
||||
return op;
|
||||
}
|
18
Editor/UndoStack.h
Normal file
18
Editor/UndoStack.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
#include <stack>
|
||||
#include "Operation.h"
|
||||
#include "OwnPtr.h"
|
||||
|
||||
class UndoStack {
|
||||
public:
|
||||
UndoStack() { }
|
||||
|
||||
void push(OwnPtr<Operation>&&);
|
||||
OwnPtr<Operation> pop();
|
||||
|
||||
bool is_empty() const { return m_stack.empty(); }
|
||||
|
||||
private:
|
||||
std::stack<OwnPtr<Operation>> m_stack;
|
||||
};
|
6
Editor/cuki.h
Normal file
6
Editor/cuki.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
// cuki text editor
|
||||
|
||||
#include "Assertions.h"
|
||||
#include "Noncopyable.h"
|
||||
|
||||
// eof
|
19
Editor/main.cpp
Normal file
19
Editor/main.cpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include "Document.h"
|
||||
#include "Editor.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int c, char** v)
|
||||
{
|
||||
std::string file_to_open = "cuki.h";
|
||||
if (c > 1) {
|
||||
file_to_open = v[1];
|
||||
}
|
||||
auto document = Document::create_from_file(file_to_open);
|
||||
if (!document) {
|
||||
fprintf(stderr, "Failed to open file.\n");
|
||||
return 1;
|
||||
}
|
||||
Editor editor;
|
||||
editor.set_document(std::move(document));
|
||||
return editor.exec();
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include "elf.h"
|
||||
#include "types.h"
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "FIFO.h"
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
//#define FIFO_DEBUG
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <AK/RetainPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <VirtualFileSystem/VirtualFileSystem.h>
|
||||
|
||||
class Process;
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include "i8253.h"
|
||||
#include "RTC.h"
|
||||
#include "ProcFileSystem.h"
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <LibC/signal_numbers.h>
|
||||
#include "Syscall.h"
|
||||
#include "Scheduler.h"
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <VirtualFileSystem/VirtualFileSystem.h>
|
||||
#include <VirtualFileSystem/UnixTypes.h>
|
||||
#include <AK/InlineLinkedList.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
class FileDescriptor;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "IO.h"
|
||||
#include "StdLib.h"
|
||||
#include "Keyboard.h"
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
static byte* s_vga_buffer;
|
||||
static VirtualConsole* s_consoles[6];
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "types.h"
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
struct KSym {
|
||||
dword address;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
//#define TERMCAP_DEBUG
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
static unsigned parseUInt(const String& str, bool& ok)
|
||||
{
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
static int do_dir(const char* path);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
static unsigned parseUInt(const String& str, bool& ok)
|
||||
{
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include "ext2_fs.h"
|
||||
#include "UnixTypes.h"
|
||||
#include <AK/Bitmap.h>
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/kmalloc.h>
|
||||
#include <AK/ktime.h>
|
||||
#include <AK/kstdio.h>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "DiskDevice.h"
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Retainable.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/kstdio.h>
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "FullDevice.h"
|
||||
#include "Limits.h"
|
||||
#include <LibC/errno_numbers.h>
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/kstdio.h>
|
||||
|
||||
FullDevice::FullDevice()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "NullDevice.h"
|
||||
#include "Limits.h"
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/kstdio.h>
|
||||
|
||||
NullDevice::NullDevice()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "RandomDevice.h"
|
||||
#include "Limits.h"
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
RandomDevice::RandomDevice()
|
||||
: CharacterDevice(1, 8)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "SyntheticFileSystem.h"
|
||||
#include "FileDescriptor.h"
|
||||
#include <LibC/errno_numbers.h>
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
#ifndef SERENITY
|
||||
typedef int InterruptDisabler;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <AK/HashMap.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/Function.h>
|
||||
#include "InodeIdentifier.h"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "ZeroDevice.h"
|
||||
#include "Limits.h"
|
||||
#include <AK/StdLib.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/kstdio.h>
|
||||
|
||||
ZeroDevice::ZeroDevice()
|
||||
|
|
Loading…
Reference in a new issue