From: Michael D. Lowis Date: Fri, 18 Nov 2016 00:58:42 +0000 (-0500) Subject: Added cut, copy, and paste shortcuts X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=2bec053d9b086627030018bfb4e49221fb7ac2c9;p=projs%2Ftide.git Added cut, copy, and paste shortcuts --- diff --git a/inc/edit.h b/inc/edit.h index ce4d7ba..55f0b3f 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -159,12 +159,8 @@ void view_bol(View* view); void view_eol(View* view); void view_undo(View* view); void view_redo(View* view); - -//size_t view_getoff(View* view, size_t pos, size_t row, size_t col); -//void view_getsize(View* view, size_t* nrows, size_t* ncols); -//void view_clearrow(View* view, size_t row); -//size_t view_setcell(View* view, size_t row, size_t col, uint32_t attr, Rune r); -//UGlyph* view_getglyph(View* view, size_t row, size_t col, size_t* scrwidth); +void view_putstr(View* view, char* str); +char* view_getstr(View* view, Sel* sel); /* Command Executions *****************************************************************************/ @@ -264,4 +260,4 @@ enum { 0xff2aa198, \ 0xff859900 \ } -#define DEFAULT_TAGS "Quit Save Cut Copy Paste | Find \n" +#define DEFAULT_TAGS "Quit Save Cut Copy Paste | Find " diff --git a/libedit/buf.c b/libedit/buf.c index 7fd22fd..96d024b 100644 --- a/libedit/buf.c +++ b/libedit/buf.c @@ -334,39 +334,39 @@ unsigned buf_setcol(Buf* buf, unsigned pos, unsigned col) { return curr; } -char* buf_getstr(Buf* buf, unsigned beg, unsigned end) { - char utf[UTF_MAX] = {0}; - size_t len = 0; - char* str = NULL; - for (; beg <= end; beg++) { - Rune rune = buf_get(buf, beg); - if (rune == RUNE_CRLF) { - str = realloc(str, len + 2); - str[len + 1] = '\r'; - str[len + 2] = '\n'; - len += 2; - } else { - size_t n = utf8encode(utf, rune); - str = realloc(str, len + n); - memcpy(str+len, utf, n); - len += n; - } - } - str = realloc(str, len+1); - if (str) str[len] = '\0'; - return str; -} - -unsigned buf_putstr(Buf* buf, unsigned beg, unsigned end, char* str) { - /* delete the selected text first */ - for (unsigned i = beg; ((end-beg) > 1) && (i <= end); i++) - buf_del(buf, beg); - /* insert the text */ - while (*str) { - Rune rune = 0; - size_t length = 0; - while (!utf8decode(&rune, &length, *str++)); - buf_ins(buf, beg++, rune); - } - return beg; -} +//char* buf_getstr(Buf* buf, unsigned beg, unsigned end) { +// char utf[UTF_MAX] = {0}; +// size_t len = 0; +// char* str = NULL; +// for (; beg <= end; beg++) { +// Rune rune = buf_get(buf, beg); +// if (rune == RUNE_CRLF) { +// str = realloc(str, len + 2); +// str[len + 1] = '\r'; +// str[len + 2] = '\n'; +// len += 2; +// } else { +// size_t n = utf8encode(utf, rune); +// str = realloc(str, len + n); +// memcpy(str+len, utf, n); +// len += n; +// } +// } +// str = realloc(str, len+1); +// if (str) str[len] = '\0'; +// return str; +//} +// +//unsigned buf_putstr(Buf* buf, unsigned beg, unsigned end, char* str) { +// /* delete the selected text first */ +// for (unsigned i = beg; ((end-beg) > 1) && (i <= end); i++) +// buf_del(buf, beg); +// /* insert the text */ +// while (*str) { +// Rune rune = 0; +// size_t length = 0; +// while (!utf8decode(&rune, &length, *str++)); +// buf_ins(buf, beg++, rune); +// } +// return beg; +//} diff --git a/libedit/view.c b/libedit/view.c index bd026f3..d2cf1a8 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -149,7 +149,7 @@ static size_t getoffset(View* view, size_t row, size_t col) { pos++; } if (pos >= buf_end(&(view->buffer))) - return buf_end(&(view->buffer))-1; + return buf_end(&(view->buffer)); return pos; } @@ -226,9 +226,11 @@ void view_byline(View* view, int move) { void view_setcursor(View* view, size_t row, size_t col) { size_t off = getoffset(view, row, col); - view->selection.beg = view->selection.end = off; - view->selection.col = buf_getcol(&(view->buffer), view->selection.end); - sync_view(view, view->selection.end); + if (off != SIZE_MAX) { + view->selection.beg = view->selection.end = off; + view->selection.col = buf_getcol(&(view->buffer), view->selection.end); + view->sync_needed = true; + } } void view_selext(View* view, size_t row, size_t col) { @@ -295,3 +297,40 @@ void view_redo(View* view) { view->selection.col = buf_getcol(&(view->buffer), view->selection.end); view->sync_needed = true; } + +void view_putstr(View* view, char* str) { + while (*str) { + Rune rune = 0; + size_t length = 0; + while (!utf8decode(&rune, &length, *str++)); + view_insert(view, rune); + } +} + +char* view_getstr(View* view, Sel* range) { + Buf* buf = &(view->buffer); + Sel sel = (range ? *range : view->selection); + selswap(&sel); + char utf[UTF_MAX] = {0}; + size_t len = 0; + char* str = NULL; + + for (; sel.beg <= sel.end; sel.beg++) { + Rune rune = buf_get(buf, sel.beg); + if (rune == RUNE_CRLF) { + str = realloc(str, len + 2); + str[len + 1] = '\r'; + str[len + 2] = '\n'; + len += 2; + } else { + size_t n = utf8encode(utf, rune); + str = realloc(str, len + n); + memcpy(str+len, utf, n); + len += n; + } + } + + str = realloc(str, len+1); + if (str) str[len] = '\0'; + return str; +} diff --git a/xedit.c b/xedit.c index 4b42a31..de36454 100644 --- a/xedit.c +++ b/xedit.c @@ -27,6 +27,16 @@ static XConfig Config = { .palette = COLOR_PALETTE }; +/* External Commands + *****************************************************************************/ +#ifdef __MACH__ +static char* CopyCmd[] = { "pbcopy", NULL }; +static char* PasteCmd[] = { "pbpaste", NULL }; +#else +static char* CopyCmd[] = { "xsel", "-bi", NULL }; +static char* PasteCmd[] = { "xsel", "-bo", NULL }; +#endif + /* Region Utils *****************************************************************************/ static View* getview(enum RegionId id) { @@ -120,24 +130,24 @@ static void redo(void) { view_redo(currview()); } -//static void cut(void) { -// char* str = buf_getstr(&Buffer, SelBeg, SelEnd); -// cmdwrite(CopyCmd, str); -// free(str); -// delete(); -//} -// -//static void copy(void) { -// char* str = buf_getstr(&Buffer, SelBeg, SelEnd); -// cmdwrite(CopyCmd, str); -// free(str); -//} -// -//static void paste(void) { -// char* str = cmdread(PasteCmd); -// buf_putstr(&Buffer, SelBeg, SelEnd, str); -// free(str); -//} +static void cut(void) { + char* str = view_getstr(currview(), NULL); + cmdwrite(CopyCmd, str); + free(str); + delete(); +} + +static void copy(void) { + char* str = view_getstr(currview(), NULL); + cmdwrite(CopyCmd, str); + free(str); +} + +static void paste(void) { + char* str = cmdread(PasteCmd); + view_putstr(currview(), str); + free(str); +} /* Mouse Handling *****************************************************************************/ @@ -225,22 +235,22 @@ static void mouse_handler(MouseAct act, MouseBtn btn, int x, int y) { /* Keyboard Bindings *****************************************************************************/ static KeyBinding Insert[] = { - { KEY_DELETE, delete }, - { KEY_UP, cursor_up }, - { KEY_DOWN, cursor_dn }, - { KEY_LEFT, cursor_left }, - { KEY_RIGHT, cursor_right }, - { KEY_HOME, cursor_bol }, - { KEY_END, cursor_eol }, - { KEY_CTRL_T, change_focus }, - { KEY_CTRL_Q, quit }, - { KEY_CTRL_S, save }, - { KEY_CTRL_Z, undo }, - { KEY_CTRL_Y, redo }, - //{ KEY_CTRL_X, cut }, - //{ KEY_CTRL_C, copy }, - //{ KEY_CTRL_V, paste }, - { 0, NULL } + { KEY_DELETE, delete }, + { KEY_UP, cursor_up }, + { KEY_DOWN, cursor_dn }, + { KEY_LEFT, cursor_left }, + { KEY_RIGHT, cursor_right }, + { KEY_HOME, cursor_bol }, + { KEY_END, cursor_eol }, + { KEY_CTRL_T, change_focus }, + { KEY_CTRL_Q, quit }, + { KEY_CTRL_S, save }, + { KEY_CTRL_Z, undo }, + { KEY_CTRL_Y, redo }, + { KEY_CTRL_X, cut }, + { KEY_CTRL_C, copy }, + { KEY_CTRL_V, paste }, + { 0, NULL } }; static void process_table(KeyBinding* bindings, Rune key) { @@ -384,8 +394,8 @@ static void redraw(int width, int height) { int main(int argc, char** argv) { /* load the buffer views */ view_init(getview(TAGS), NULL); + view_putstr(getview(TAGS), DEFAULT_TAGS); view_init(getview(EDIT), (argc > 1 ? argv[1] : NULL)); - buf_putstr(getbuf(TAGS), 0, 0, DEFAULT_TAGS); /* initialize the display engine */ x11_init(&Config); x11_window("edit", Width, Height); @@ -396,28 +406,9 @@ int main(int argc, char** argv) { } #if 0 -/* External Commands - *****************************************************************************/ -#ifdef __MACH__ -static char* CopyCmd[] = { "pbcopy", NULL }; -static char* PasteCmd[] = { "pbpaste", NULL }; -#else -static char* CopyCmd[] = { "xsel", "-bi", NULL }; -static char* PasteCmd[] = { "xsel", "-bo", NULL }; -#endif - -/* Keyboard Actions - *****************************************************************************/ - -static void tagwin(void) { - TagWinExpanded = !TagWinExpanded; -} /* Mouse Actions *****************************************************************************/ -void unused(int x, int y) { -} - void move_cursor(int x, int y) { if (y == 0) return; //SelBeg = SelEnd = screen_getoff(&Buffer, SelEnd, y-1, x);