From 1fb1a7f363e14b38fc651fcdf295a82183df0e9f Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 4 Mar 2017 19:45:45 -0500 Subject: [PATCH] added shortcut to complete tag using fuzzy picker --- TODO.md | 5 ++--- lib/view.c | 3 ++- xedit.c | 17 +++++++++++++++-- xpick.c | 9 +++++++-- xtagpick | 56 +++++++++++++++++++++++++++++++++++++----------------- 5 files changed, 65 insertions(+), 25 deletions(-) diff --git a/TODO.md b/TODO.md index add30c8..d8e80fe 100644 --- a/TODO.md +++ b/TODO.md @@ -2,19 +2,18 @@ Up Next: -* Make Fn keys execute nth command in the tags buffer * check for file changes on save * check for file changes when window regains focus * Right click in tags region should search edit region -* Add keyboard shortcut to highlight the thing under the cursor * 100% coverage with unit and unit-integration tests Tomorrow-ish: +* Make Fn keys execute nth command in the tags buffer * selecting text should set PRIMARY x11 selection * Add a SaveAs tag that takes an argument for the filename to save as * Add a GoTo tag for ctags lookup and line number jump (or right click magic?) -* Add a ctrl+space shortcut to autocomplete ctag +* Add keyboard shortcut to highlight the thing under the cursor * off by one error on scrolling up with wrapped lines * tab inserts dont coalesce like one would expect diff --git a/lib/view.c b/lib/view.c index 76bee77..3c62e97 100644 --- a/lib/view.c +++ b/lib/view.c @@ -316,7 +316,8 @@ static void selcontext(View* view, Sel* sel) { void view_selword(View* view, size_t row, size_t col) { buf_loglock(&(view->buffer)); - view_setcursor(view, row, col); + if (row != SIZE_MAX && col != SIZE_MAX) + view_setcursor(view, row, col); Sel sel = view->selection; buf_getword(&(view->buffer), risbigword, &(sel)); sel.end++; diff --git a/xedit.c b/xedit.c index ea3899d..c0cb86d 100644 --- a/xedit.c +++ b/xedit.c @@ -17,7 +17,7 @@ typedef struct { static char* ShellCmd[] = { NULL, "-c", NULL, NULL }; static char* SedCmd[] = { "sed", "-e", NULL, NULL }; static char* PickFileCmd[] = { "xfilepick", ".", NULL }; -static char* PickTagCmd[] = { "xtagpick", "tags", NULL, NULL }; +static char* PickTagCmd[] = { "xtagpick", NULL, "tags", NULL, NULL }; static char* OpenCmd[] = { "xedit", NULL, NULL }; static Tag Builtins[]; static int SearchDir = DOWN; @@ -333,7 +333,8 @@ static void open_file(void) { } static void pick_symbol(char* symbol) { - PickTagCmd[2] = symbol; + PickTagCmd[1] = "fetch"; + PickTagCmd[3] = symbol; char* pick = cmdread(PickTagCmd, NULL); if (pick) { Buf* buf = win_buf(EDIT); @@ -355,6 +356,17 @@ static void pick_ctag(void) { pick_symbol(NULL); } +static void complete(void) { + View* view = win_view(FOCUSED); + view_selword(view, SIZE_MAX, SIZE_MAX); + PickTagCmd[1] = "print"; + PickTagCmd[3] = view_getstr(view, NULL); + char* pick = cmdread(PickTagCmd, NULL); + if (pick) + view_putstr(view, chomp(pick)); + free(PickTagCmd[3]); +} + static void goto_ctag(void) { char* str = view_getctx(win_view(FOCUSED)); if (str) { @@ -482,6 +494,7 @@ static KeyBinding Bindings[] = { { ModCtrl, 'n', new_win }, { ModCtrl, '\n', newline }, { ModCtrl|ModShift, '\n', newline }, + { ModCtrl, ' ', complete }, { 0, 0, 0 } }; diff --git a/xpick.c b/xpick.c index 181130d..8141d5b 100644 --- a/xpick.c +++ b/xpick.c @@ -197,9 +197,14 @@ static void keyboard_input(int mods, uint32_t key) { #ifndef TEST int main(int argc, char** argv) { load_choices(); + buf_init(&Query); + if (argc >= 2) { + char* str = argv[1]; + while (*str) + buf_insert(&Query, false, Pos++, *(str++)); + score(); + } if (vec_size(&Choices) > 1) { - /* initialize the filter edit buffer */ - buf_init(&Query); /* initialize the display engine */ x11_init(&Config); x11_dialog("pick", Width, Height); diff --git a/xtagpick b/xtagpick index dbe515f..eb59ed1 100755 --- a/xtagpick +++ b/xtagpick @@ -1,28 +1,50 @@ #!/bin/sh -TAGFILE="$1" -TAG="$2" +ACTION="$1" +TAGFILE="$2" +TAG="$3" usage(){ - echo "Usage: $0 TAGFILE [TAG]" + echo "Usage: $0 ACTION TAGFILE [TAG]" + echo "" + echo "Actions:" + echo " fetch - Print the filename and line number of the selcted tag" + echo " print - Print the selected tag" exit 1 } -if [ "" == "$TAGFILE" ]; then +if [ "" == "$TAGFILE" ] || [ "" == "$ACTION" ]; then usage fi -if [ "" == "$TAG" ]; then - TAG=$(cat tags | grep -v '^!' | cut -f1 | uniq | xpick) - [ "" == "$TAG" ] && exit -fi +printtags(){ + cat "$TAGFILE" | grep -v '^!' | cut -f1 | uniq +} + +print(){ + printtags | xpick "$TAG" +} -awk -v TAG="$TAG" ' -BEGIN { FS = "[\t]+" } -($1 == TAG) { - matchstr = $3 - sub(/^\//, "\"", matchstr) - sub(/\$?\/;"$/, "\"", matchstr) - gsub(/\*/, "\\*", matchstr) - print "grep -Hn", matchstr, $2, "| cut -d: -f1,2" +fetch(){ + if [ "" == "$TAG" ]; then + TAG=$(printtags | xpick) + [ "" == "$TAG" ] && exit + fi + awk -v TAG="$TAG" ' + BEGIN { FS = "[\t]+" } + ($1 == TAG) { + matchstr = $3 + sub(/^\//, "\"", matchstr) + sub(/\$?\/;"$/, "\"", matchstr) + gsub(/\*/, "\\*", matchstr) + print "grep -Hn", matchstr, $2, "| cut -d: -f1,2" + } + ' "$TAGFILE" | /bin/sh | xpick } -' "$TAGFILE" | /bin/sh | xpick + +case "$ACTION" in + "print") print ;; + "fetch") fetch ;; + *) usage ;; +esac + + -- 2.51.0