From 8d67c5faf0627e79017718897d191c9cb9489d9c Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 10 Nov 2016 18:12:05 -0500 Subject: [PATCH] General cleanup and removal of buffer locking. last remnant of modal editing --- inc/edit.h | 3 --- inc/utf.h | 12 ++++++------ libedit/buf.c | 24 +++--------------------- libedit/mouse.c | 8 ++++---- tests/buf.c | 37 ------------------------------------- xedit.c | 1 - xpick.c | 1 - 7 files changed, 13 insertions(+), 73 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 06e5a38..4a0d16a 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -36,7 +36,6 @@ typedef struct buf { char* path; /* the path to the open file */ int charset; /* the character set of the buffer */ int crlf; /* tracks whether the file uses dos style line endings */ - bool locked; /* tracks current mode */ bool modified; /* tracks whether the buffer has been modified */ size_t bufsize; /* size of the buffer in runes */ Rune* bufstart; /* start of the data buffer */ @@ -61,8 +60,6 @@ void buf_del(Buf* buf, unsigned pos); unsigned buf_undo(Buf* buf, unsigned pos); unsigned buf_redo(Buf* buf, unsigned pos); Rune buf_get(Buf* buf, unsigned pos); -void buf_setlocked(Buf* buf, bool locked); -bool buf_locked(Buf* buf); bool buf_iseol(Buf* buf, unsigned pos); unsigned buf_bol(Buf* buf, unsigned pos); unsigned buf_eol(Buf* buf, unsigned pos); diff --git a/inc/utf.h b/inc/utf.h index 82ad5a8..3bacfe9 100644 --- a/inc/utf.h +++ b/inc/utf.h @@ -1,10 +1,10 @@ enum { - UTF_MAX = 6u, /* maximum number of bytes that make up a rune */ - RUNE_SELF = 0x80, /* byte values larger than this are *not* ascii */ - RUNE_ERR = 0xFFFD, /* rune value representing an error */ - RUNE_MAX = 0x10FFFF, /* Maximum decodable rune value */ - RUNE_EOF = UINT32_MAX, /* rune value representing end of file */ - RUNE_CRLF = UINT32_MAX-1, /* rune value representing a \r\n sequence */ + UTF_MAX = 6u, /* maximum number of bytes that make up a rune */ + RUNE_SELF = 0x80, /* byte values larger than this are *not* ascii */ + RUNE_ERR = 0xFFFD, /* rune value representing an error */ + RUNE_MAX = 0x10FFFF, /* Maximum decodable rune value */ + RUNE_EOF = -1, /* rune value representing end of file */ + RUNE_CRLF = -2, /* rune value representing a \r\n sequence */ }; /* Represents a unicode code point */ diff --git a/libedit/buf.c b/libedit/buf.c index deed804..7fd22fd 100644 --- a/libedit/buf.c +++ b/libedit/buf.c @@ -3,7 +3,6 @@ #include void buf_load(Buf* buf, char* path) { - buf_setlocked(buf, false); if (!strcmp(path,"-")) { buf->charset = UTF_8; Rune r; @@ -23,8 +22,9 @@ void buf_load(Buf* buf, char* path) { } funmap(file); } - buf_setlocked(buf, true); buf->modified = false; + free(buf->undo); + buf->undo = NULL; } void buf_save(Buf* buf) { @@ -74,7 +74,6 @@ static void syncgap(Buf* buf, unsigned off) { } void buf_init(Buf* buf) { - buf->locked = true; buf->modified = false; buf->charset = DEFAULT_CHARSET; buf->crlf = DEFAULT_CRLF; @@ -89,9 +88,7 @@ void buf_init(Buf* buf) { static void log_insert(Log** list, unsigned beg, unsigned end) { Log* log = *list; - if (!log || log->locked || !log->insert || - (log->data.ins.beg && beg < log->data.ins.beg-1) || - (end > log->data.ins.end+1)) { + if (!log || log->locked || !log->insert || (end != log->data.ins.end+1)) { Log* newlog = (Log*)calloc(sizeof(Log), 1); newlog->insert = true; newlog->data.ins.beg = beg; @@ -141,7 +138,6 @@ static void insert(Buf* buf, unsigned off, Rune rune) { } void buf_ins(Buf* buf, unsigned off, Rune rune) { - if (buf->locked) { return; } buf->modified = true; log_insert(&(buf->undo), off, off+1); insert(buf, off, rune); @@ -153,7 +149,6 @@ static void delete(Buf* buf, unsigned off) { } void buf_del(Buf* buf, unsigned off) { - if (buf->locked) { return; } buf->modified = true; Rune r = buf_get(buf, off); log_delete(&(buf->undo), off, &r, 1); @@ -209,16 +204,6 @@ Rune buf_get(Buf* buf, unsigned off) { return *(buf->gapend + (off - bsz)); } -void buf_setlocked(Buf* buf, bool locked) { - if (locked && buf->undo) - buf->undo->locked = true; - buf->locked = locked; -} - -bool buf_locked(Buf* buf) { - return buf->locked; -} - bool buf_iseol(Buf* buf, unsigned off) { Rune r = buf_get(buf, off); return (r == '\n' || r == RUNE_CRLF); @@ -373,8 +358,6 @@ char* buf_getstr(Buf* buf, unsigned beg, unsigned end) { } unsigned buf_putstr(Buf* buf, unsigned beg, unsigned end, char* str) { - bool locked = buf_locked(buf); - buf_setlocked(buf, false); /* delete the selected text first */ for (unsigned i = beg; ((end-beg) > 1) && (i <= end); i++) buf_del(buf, beg); @@ -385,6 +368,5 @@ unsigned buf_putstr(Buf* buf, unsigned beg, unsigned end, char* str) { while (!utf8decode(&rune, &length, *str++)); buf_ins(buf, beg++, rune); } - buf_setlocked(buf, locked); return beg; } diff --git a/libedit/mouse.c b/libedit/mouse.c index 81a5086..b910f15 100644 --- a/libedit/mouse.c +++ b/libedit/mouse.c @@ -30,19 +30,19 @@ void selection(MouseEvent* mevnt) { } else if (risword(r)) { SelBeg = buf_bow(&Buffer, SelEnd); SelEnd = buf_eow(&Buffer, SelEnd); - if (!buf_locked(&Buffer)) SelEnd++; + SelEnd++; } else if (r == '(' || r == ')') { SelBeg = buf_lscan(&Buffer, SelEnd, '('); SelEnd = buf_rscan(&Buffer, SelEnd, ')'); - if (!buf_locked(&Buffer)) SelEnd++; + SelEnd++; } else if (r == '[' || r == ']') { SelBeg = buf_lscan(&Buffer, SelEnd, '['); SelEnd = buf_rscan(&Buffer, SelEnd, ']'); - if (!buf_locked(&Buffer)) SelEnd++; + SelEnd++; } else if (r == '{' || r == '}') { SelBeg = buf_lscan(&Buffer, SelEnd, '{'); SelEnd = buf_rscan(&Buffer, SelEnd, '}'); - if (!buf_locked(&Buffer)) SelEnd++; + SelEnd++; } else { bigword(mevnt); } diff --git a/tests/buf.c b/tests/buf.c index 8c8acee..cb4d877 100644 --- a/tests/buf.c +++ b/tests/buf.c @@ -23,7 +23,6 @@ static void set_buffer_text(char* str) { TestBuf.crlf = 1; for (Rune* curr = TestBuf.bufstart; curr < TestBuf.bufend; curr++) *curr = '-'; - TestBuf.locked = false; while (*str) buf_ins(&TestBuf, i++, (Rune)*str++); } @@ -92,42 +91,6 @@ TEST_SUITE(BufferTests) { *************************************************************************/ /* Undo/Redo *************************************************************************/ - /* Locking - *************************************************************************/ - TEST(buf_setlocked should lock the buffer to prevent changes) { - TestBuf.locked = false; - if (TestBuf.undo) { free(TestBuf.undo); TestBuf.undo = NULL; } - buf_setlocked(&TestBuf, true); - CHECK(TestBuf.locked); - } - - TEST(buf_setlocked should lock the buffer to prevent changes and lock the last undo op) { - Log log; - TestBuf.locked = false; - TestBuf.undo = &log; - buf_setlocked(&TestBuf, true); - CHECK(TestBuf.locked); - CHECK(TestBuf.undo->locked); - } - - TEST(buf_setlocked should unlock the buffer) { - Log log; - TestBuf.locked = true; - TestBuf.undo = &log; - buf_setlocked(&TestBuf, false); - CHECK(!TestBuf.locked); - } - - TEST(buf_islocked should return true if locked) { - TestBuf.locked = true; - CHECK(buf_locked(&TestBuf)); - } - - TEST(buf_islocked should return false if locked) { - TestBuf.locked = false; - CHECK(!buf_locked(&TestBuf)); - } - /* Accessors *************************************************************************/ // buf_get diff --git a/xedit.c b/xedit.c index 6a1ffe0..6535ce7 100644 --- a/xedit.c +++ b/xedit.c @@ -266,7 +266,6 @@ int main(int argc, char** argv) { buf_init(&Buffer); if (argc > 1) buf_load(&Buffer, argv[1]); - buf_setlocked(&Buffer, false); /* initialize the display engine */ x11_init(&Config); x11_window("edit", Width, Height); diff --git a/xpick.c b/xpick.c index 610c123..ac36baf 100644 --- a/xpick.c +++ b/xpick.c @@ -215,7 +215,6 @@ int main(int argc, char** argv) { load_choices(); /* initialize the filter edit buffer */ buf_init(&Query); - buf_setlocked(&Query, false); /* initialize the display engine */ x11_init(&Config); x11_dialog("pick", Width, Height); -- 2.52.0