From: NeilBrown Date: Thu, 26 Nov 2015 03:34:01 +0000 (+1100) Subject: move simple word/char/eol/file movement from 'view' to 'doc'. X-Git-Tag: lca2016~204 X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=1e86c6e25aab67b94b7cffd33bea54e04493e4af;p=edlib.git move simple word/char/eol/file movement from 'view' to 'doc'. I think it makes more sense here. Signed-off-by: NeilBrown --- diff --git a/core-doc.c b/core-doc.c index dab9653b..0715fd89 100644 --- a/core-doc.c +++ b/core-doc.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #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; diff --git a/lib-view.c b/lib-view.c index 039e678c..a5c5418a 100644 --- a/lib-view.c +++ b/lib-view.c @@ -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);