2 * Copyright Neil Brown ©2016 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
7 * A history pane supports selection of lines from a separate
8 * document. The underlying document is assumed to be one line
9 * and this line can be replaced by various lines from the history document.
10 * When a line is replaced, if it had been modified, it is saved first.
11 * M-p - replace current line with previous line from history, if there is one
12 * M-n - replace current line with next line from history. If none, restore
14 * M-r - incremental search - later
15 * When a selection is committed, it is added to end of history.
34 DEF_CMD(history_handle)
36 struct pane *p = ci->home;
37 struct history_info *hi = p->data;
40 if (strcmp(ci->key, "Close") == 0) {
49 /* history document was destroyed */
52 if (strcmp(ci->key, "Notify:Close") == 0 && ci->focus == hi->history) {
53 /* The history document is going away!!! */
58 if (hi->donekey && strcmp(ci->key, hi->donekey) == 0 && ci->str) {
59 call3("Move-File", hi->history, 1, hi->m);
60 call7("doc:replace", hi->history, 1, NULL, ci->str, 1, NULL, hi->m);
61 call7("doc:replace", hi->history, 1, NULL, "\n", 1, NULL, hi->m);
65 if (strcmp(ci->key, "Notify:Replace") == 0) {
70 if (strcmp(ci->key, "M-Chr-p") == 0 || strcmp(ci->key, "M-Chr-n") == 0) {
72 if (ci->key[6] == 'p') {
73 m = mark_dup(hi->m, 1);
74 call3("Move-EOL", hi->history, -2, hi->m);
76 call3("Move-EOL", hi->history, 1, hi->m);
77 call3("Move-Char", hi->history, 1, hi->m);
78 m = mark_dup(hi->m, 1);
79 call3("Move-EOL", hi->history, 1, m);
80 call3("Move-Char", hi->history, 1, m);
82 if (mark_same_pane(hi->history, m, hi->m, NULL)) {
84 if (ci->key[6] == 'p') {
90 l = doc_getstr(hi->history, m, hi->m);
96 call3("Move-EOL", ci->focus, -1, ci->mark);
97 m = mark_dup(ci->mark, 1);
98 call3("Move-EOL", ci->focus, 1, m);
102 hi->saved = doc_getstr(ci->focus, ci->mark, m);
104 call5("Replace", ci->focus, 1, m, l, 1);
113 if (strcmp(ci->key, "M-Chr-r") == 0) {
118 DEF_CMD(history_attach)
121 struct history_info *hi;
127 hi = calloc(1, sizeof(*hi));
128 hi->donekey = ci->str2;
129 hi->history = call_pane7("docs:byname", ci->focus, 0, NULL, 0,
132 hi->history = doc_from_text(ci->focus, ci->str, "");
133 hi->m = vmark_new(hi->history, MARK_UNGROUPED);
134 call3("Move-File", hi->history, 1, hi->m);
135 buf_init(&hi->search);
136 p = pane_register(ci->focus, 0, &history_handle, hi, NULL);
137 pane_add_notify(p, hi->history, "Notify:Close");
138 call3("Request:Notify:Replace", p, 0, NULL);
139 return comm_call(ci->comm2, "callback:attach", p, 0, NULL, NULL, 0);
142 void edlib_init(struct pane *ed)
144 call_comm("global-set-command", ed, 0, NULL, "attach-history",