]> git.neil.brown.name Git - edlib.git/commitdiff
move simple word/char/eol/file movement from 'view' to 'doc'.
authorNeilBrown <neil@brown.name>
Thu, 26 Nov 2015 03:34:01 +0000 (14:34 +1100)
committerNeilBrown <neil@brown.name>
Thu, 26 Nov 2015 03:34:01 +0000 (14:34 +1100)
I think it makes more sense here.

Signed-off-by: NeilBrown <neil@brown.name>
core-doc.c
lib-view.c

index dab9653be0a6d453d27aae12dee18bbdbc1346f6..0715fd8920cc951480fb754fc9f66ba36f765454 100644 (file)
@@ -20,6 +20,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <wchar.h>
+#include <wctype.h>
 #include <stdio.h>
 
 #define PRIVATE_DOC_REF
@@ -137,9 +139,158 @@ void doc_init(struct doc *d)
        d->home = NULL;
 }
 
+
+DEF_CMD(doc_char)
+{
+       struct point *pt = *ci->pointp;
+       int rpt = RPT_NUM(ci);
+
+       while (rpt > 0) {
+               if (mark_next(pt->doc, ci->mark) == WEOF)
+                       break;
+               rpt -= 1;
+       }
+       while (rpt < 0) {
+               if (mark_prev(pt->doc, ci->mark) == WEOF)
+                       break;
+               rpt += 1;
+       }
+
+       return 1;
+}
+
+DEF_CMD(doc_word)
+{
+       struct point *pt = *ci->pointp;
+       int rpt = RPT_NUM(ci);
+
+       /* We skip spaces, then either alphanum or non-space/alphanum */
+       while (rpt > 0) {
+               while (iswspace(doc_following(pt->doc, ci->mark)))
+                       mark_next(pt->doc, ci->mark);
+               if (iswalnum(doc_following(pt->doc, ci->mark))) {
+                       while (iswalnum(doc_following(pt->doc, ci->mark)))
+                               mark_next(pt->doc, ci->mark);
+               } else {
+                       wint_t wi;
+                       while ((wi=doc_following(pt->doc, ci->mark)) != WEOF &&
+                              !iswspace(wi) && !iswalnum(wi))
+                               mark_next(pt->doc, ci->mark);
+               }
+               rpt -= 1;
+       }
+       while (rpt < 0) {
+               while (iswspace(doc_prior(pt->doc, ci->mark)))
+                       mark_prev(pt->doc, ci->mark);
+               if (iswalnum(doc_prior(pt->doc, ci->mark))) {
+                       while (iswalnum(doc_prior(pt->doc, ci->mark)))
+                               mark_prev(pt->doc, ci->mark);
+               } else {
+                       wint_t wi;
+                       while ((wi=doc_prior(pt->doc, ci->mark)) != WEOF &&
+                              !iswspace(wi) && !iswalnum(wi))
+                               mark_prev(pt->doc, ci->mark);
+               }
+               rpt += 1;
+       }
+
+       return 1;
+}
+
+DEF_CMD(doc_word)
+{
+       struct point *pt = *ci->pointp;
+       int rpt = RPT_NUM(ci);
+
+       /* We skip spaces, then non-spaces */
+       while (rpt > 0) {
+               wint_t wi;
+               while (iswspace(doc_following(pt->doc, ci->mark)))
+                       mark_next(pt->doc, ci->mark);
+
+               while ((wi=doc_following(pt->doc, ci->mark)) != WEOF &&
+                      !iswspace(wi))
+                       mark_next(pt->doc, ci->mark);
+               rpt -= 1;
+       }
+       while (rpt < 0) {
+               wint_t wi;
+               while (iswspace(doc_prior(pt->doc, ci->mark)))
+                       mark_prev(pt->doc, ci->mark);
+               while ((wi=doc_prior(pt->doc, ci->mark)) != WEOF &&
+                      !iswspace(wi))
+                       mark_prev(pt->doc, ci->mark);
+               rpt += 1;
+       }
+
+       return 1;
+}
+
+DEF_CMD(doc_eol)
+{
+       struct doc *d = (*ci->pointp)->doc;
+       wint_t ch = 1;
+       int rpt = RPT_NUM(ci);
+
+       while (rpt > 0 && ch != WEOF) {
+               while ((ch = mark_next(d, ci->mark)) != WEOF &&
+                      ch != '\n')
+                       ;
+               rpt -= 1;
+       }
+       while (rpt < 0 && ch != WEOF) {
+               while ((ch = mark_prev(d, ci->mark)) != WEOF &&
+                      ch != '\n')
+                       ;
+               rpt += 1;
+       }
+       if (ch == '\n') {
+               if (RPT_NUM(ci) > 0)
+                       mark_prev(d, ci->mark);
+               else if (RPT_NUM(ci) < 0)
+                       mark_next(d, ci->mark);
+       }
+       return 1;
+}
+
+DEF_CMD(doc_file)
+{
+       struct point *pt = *ci->pointp;
+       wint_t ch = 1;
+       int rpt = RPT_NUM(ci);
+
+       if (ci->mark == NULL)
+               ci->mark = &pt->m;
+       while (rpt > 0 && ch != WEOF) {
+               while ((ch = mark_next(pt->doc, ci->mark)) != WEOF)
+                       ;
+               rpt = 0;
+       }
+       while (rpt < 0 && ch != WEOF) {
+               while ((ch = mark_prev(pt->doc, ci->mark)) != WEOF)
+                       ;
+               rpt = 0;
+       }
+       return 1;
+}
+
+static struct map *doc_default_cmd;
+
+static void init_doc_defaults(void)
+{
+       doc_default_cmd = key_alloc();
+
+       key_add(doc_default_cmd, "Move-Char", &doc_char);
+       key_add(doc_default_cmd, "Move-Word", &doc_word);
+       key_add(doc_default_cmd, "Move-WORD", &doc_word);
+       key_add(doc_default_cmd, "Move-EOL", &doc_eol);
+       key_add(doc_default_cmd, "Move-File", &doc_file);
+}
+
 DEF_CMD(doc_handle)
 {
        struct doc *d = ci->home->data;
+       int ret;
 
        /* This is a hack - I should use a watcher, but I don't have
         * anywhere to store it.
@@ -222,7 +373,9 @@ DEF_CMD(doc_handle)
                return 1;
        }
 
-       return key_lookup(d->map, ci);
+       ret = key_lookup(d->map, ci);
+       ret = ret ?: key_lookup(doc_default_cmd, ci);
+       return ret;
 }
 
 struct pane *doc_attach(struct pane *parent, struct doc *d)
@@ -242,6 +395,9 @@ struct pane *doc_new(struct editor *ed, char *type)
        char buf[100];
        struct cmd_info ci = {0};
 
+       if (!doc_default_cmd)
+               init_doc_defaults();
+
        sprintf(buf, "doc-%s", type);
        ci.key = buf;
        ci.focus = ci.home = &ed->root;
index 039e678c6efe4232db076021e6c2b7c5e8df787e..a5c5418a98ba836d67c69b9113dc5eab3ce07a9e 100644 (file)
@@ -246,119 +246,6 @@ DEF_CMD(view_attach)
        return ci->focus != NULL;
 }
 
-DEF_CMD(view_char)
-{
-       struct point *pt = *ci->pointp;
-       int rpt = RPT_NUM(ci);
-
-       while (rpt > 0) {
-               if (mark_next(pt->doc, ci->mark) == WEOF)
-                       break;
-               rpt -= 1;
-       }
-       while (rpt < 0) {
-               if (mark_prev(pt->doc, ci->mark) == WEOF)
-                       break;
-               rpt += 1;
-       }
-
-       return 1;
-}
-
-DEF_CMD(view_word)
-{
-       struct point *pt = *ci->pointp;
-       int rpt = RPT_NUM(ci);
-
-       /* We skip spaces, then either alphanum or non-space/alphanum */
-       while (rpt > 0) {
-               while (iswspace(doc_following(pt->doc, ci->mark)))
-                       mark_next(pt->doc, ci->mark);
-               if (iswalnum(doc_following(pt->doc, ci->mark))) {
-                       while (iswalnum(doc_following(pt->doc, ci->mark)))
-                               mark_next(pt->doc, ci->mark);
-               } else {
-                       wint_t wi;
-                       while ((wi=doc_following(pt->doc, ci->mark)) != WEOF &&
-                              !iswspace(wi) && !iswalnum(wi))
-                               mark_next(pt->doc, ci->mark);
-               }
-               rpt -= 1;
-       }
-       while (rpt < 0) {
-               while (iswspace(doc_prior(pt->doc, ci->mark)))
-                       mark_prev(pt->doc, ci->mark);
-               if (iswalnum(doc_prior(pt->doc, ci->mark))) {
-                       while (iswalnum(doc_prior(pt->doc, ci->mark)))
-                               mark_prev(pt->doc, ci->mark);
-               } else {
-                       wint_t wi;
-                       while ((wi=doc_prior(pt->doc, ci->mark)) != WEOF &&
-                              !iswspace(wi) && !iswalnum(wi))
-                               mark_prev(pt->doc, ci->mark);
-               }
-               rpt += 1;
-       }
-
-       return 1;
-}
-
-DEF_CMD(view_WORD)
-{
-       struct point *pt = *ci->pointp;
-       int rpt = RPT_NUM(ci);
-
-       /* We skip spaces, then non-spaces */
-       while (rpt > 0) {
-               wint_t wi;
-               while (iswspace(doc_following(pt->doc, ci->mark)))
-                       mark_next(pt->doc, ci->mark);
-
-               while ((wi=doc_following(pt->doc, ci->mark)) != WEOF &&
-                      !iswspace(wi))
-                       mark_next(pt->doc, ci->mark);
-               rpt -= 1;
-       }
-       while (rpt < 0) {
-               wint_t wi;
-               while (iswspace(doc_prior(pt->doc, ci->mark)))
-                       mark_prev(pt->doc, ci->mark);
-               while ((wi=doc_prior(pt->doc, ci->mark)) != WEOF &&
-                      !iswspace(wi))
-                       mark_prev(pt->doc, ci->mark);
-               rpt += 1;
-       }
-
-       return 1;
-}
-
-DEF_CMD(view_eol)
-{
-       struct doc *d = (*ci->pointp)->doc;
-       wint_t ch = 1;
-       int rpt = RPT_NUM(ci);
-
-       while (rpt > 0 && ch != WEOF) {
-               while ((ch = mark_next(d, ci->mark)) != WEOF &&
-                      ch != '\n')
-                       ;
-               rpt -= 1;
-       }
-       while (rpt < 0 && ch != WEOF) {
-               while ((ch = mark_prev(d, ci->mark)) != WEOF &&
-                      ch != '\n')
-                       ;
-               rpt += 1;
-       }
-       if (ch == '\n') {
-               if (RPT_NUM(ci) > 0)
-                       mark_prev(d, ci->mark);
-               else if (RPT_NUM(ci) < 0)
-                       mark_next(d, ci->mark);
-       }
-       return 1;
-}
-
 DEF_CMD(view_line)
 {
        struct point *pt = *ci->pointp;
@@ -380,27 +267,6 @@ DEF_CMD(view_line)
        return 1;
 }
 
-DEF_CMD(view_file)
-{
-       struct point *pt = *ci->pointp;
-       wint_t ch = 1;
-       int rpt = RPT_NUM(ci);
-
-       if (ci->mark == NULL)
-               ci->mark = &pt->m;
-       while (rpt > 0 && ch != WEOF) {
-               while ((ch = mark_next(pt->doc, ci->mark)) != WEOF)
-                       ;
-               rpt = 0;
-       }
-       while (rpt < 0 && ch != WEOF) {
-               while ((ch = mark_prev(pt->doc, ci->mark)) != WEOF)
-                       ;
-               rpt = 0;
-       }
-       return 1;
-}
-
 DEF_CMD(view_page)
 {
        struct point *pt = *ci->pointp;
@@ -470,12 +336,7 @@ void edlib_init(struct editor *ed)
 {
        view_map = key_alloc();
 
-       key_add(view_map, "Move-Char", &view_char);
-       key_add(view_map, "Move-Word", &view_word);
-       key_add(view_map, "Move-WORD", &view_WORD);
-       key_add(view_map, "Move-EOL", &view_eol);
        key_add(view_map, "Move-Line", &view_line);
-       key_add(view_map, "Move-File", &view_file);
        key_add(view_map, "Move-View-Large", &view_page);
 
        key_add(view_map, "Replace", &view_replace);