]> git.neil.brown.name Git - edlib.git/commitdiff
Add lib-messageline.
authorNeilBrown <neil@brown.name>
Wed, 2 Dec 2015 03:19:07 +0000 (14:19 +1100)
committerNeilBrown <neil@brown.name>
Wed, 2 Dec 2015 03:19:07 +0000 (14:19 +1100)
This places a line on the bottom on the display and displayed messages there.

Signed-off-by: NeilBrown <neil@brown.name>
Makefile
core-keymap.c
doc-text.c
edlib.c
lib-messageline.c [new file with mode: 0644]

index 73fb8e0e5659377683b37eb168bc0427ffe2f8ff..75afe8b5383ef5f76a7a1921b17b6da9aefb00dd 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@ SHOBJ = O/doc-text.o O/doc-dir.o \
        O/render-hex.o O/render-lines.o \
        O/render-format.o O/render-complete.o \
        O/lib-view.o O/lib-tile.o O/lib-popup.o O/lib-line-count.o O/lib-keymap.o \
-       O/lib-search.o \
+       O/lib-search.o O/lib-messageline.o \
        O/lang-python.o \
        O/mode-emacs.o \
        O/display-ncurses.o
index 060325efb18d4737e47fd3cdf448741f79a1ad56..64f77a4218991160a7a3e819cb6e23397bbed10d 100644 (file)
@@ -343,9 +343,13 @@ int key_handle_focus(struct cmd_info *ci)
 {
        return __key_handle_focus(ci, 0);
 }
+
 int key_handle_focus_point(struct cmd_info *ci)
 {
-       return __key_handle_focus(ci, 1);
+       int ret =  __key_handle_focus(ci, 1);
+       if (ret < 0)
+               call5("Message", ci->focus, 0, NULL, "** Command Failed **", 1);
+       return ret;
 }
 
 static int __key_handle_xy(struct cmd_info *ci, int savepoint)
index 930b22d4eef4ce1b271f336efdc2a97f25c460a0..2bd45e4b88c4d27a348e5862ec6f5e720b4e01cd 100644 (file)
@@ -52,6 +52,7 @@
  */
 
 
+#define _GNU_SOURCE /* for asprintf */
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
@@ -298,10 +299,24 @@ DEF_CMD(text_save_file)
 {
        struct doc_data *dd = ci->home->data;
        struct text *t = container_of(dd->doc, struct text, doc);
+       int ret;
+       char *msg;
 
-       if (!t->fname)
-               return -1;
-       return do_text_write_file(t, t->fname);
+       if (!t->fname) {
+               asprintf(&msg, "** No file name known for %s ***", dd->doc->name);
+               ret = -1;
+       } else {
+               ret = do_text_write_file(t, t->fname);
+               if (ret == 0)
+                       asprintf(&msg, "Successfully wrote %s", t->fname);
+               else
+                       asprintf(&msg, "*** Faild to write %s ***", t->fname);
+       }
+       call5("Message", ci->home, 0, NULL, msg, 0);
+       free(msg);
+       if (ret == 0)
+               return 1;
+       return -1;
 }
 
 DEF_CMD(text_same_file)
diff --git a/edlib.c b/edlib.c
index 6a872d037a20c1affe0fdefe78da31349586c853..e94621f3e15beb4e9bbe01b1868cecaefb090599 100644 (file)
--- a/edlib.c
+++ b/edlib.c
@@ -67,7 +67,8 @@ int main(int argc, char *argv[])
        if (!key_lookup(ed->commands, &ci))
                exit(1);
        root = ci.focus;
-       global = pane_attach(root, "global-keymap", NULL, NULL);
+       global = pane_attach(root, "messageline", NULL, NULL);
+       global = pane_attach(global, "global-keymap", NULL, NULL);
 
        editor_load_module(ed, "mode-emacs");
        call5("global-set-keymap", global, 0, NULL, "mode-emacs", 0);
diff --git a/lib-messageline.c b/lib-messageline.c
new file mode 100644 (file)
index 0000000..4adf46c
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright Neil Brown <neil@brown.name> 2015
+ * May be distributed under terms of GPLv2 - see file:COPYING
+ *
+ * trim a line off the bottom of a pane and capture messages
+ * to go there.  They disappear on the next keystroke.
+ *
+ * Later it might be good to allow boarderless popups to appear here.
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "core.h"
+
+struct mlinfo {
+       char *message;
+       struct pane *line;
+};
+
+static void pane_str(struct pane *p, char *s, char *attr, int x, int y)
+{
+       mbstate_t ps = {0};
+       int err;
+       wchar_t ch;
+
+       while ((err = mbrtowc(&ch, s, 5, &ps)) > 0) {
+               pane_text(p, ch, attr, x, y);
+               s += err;
+               x += 1;
+       }
+}
+
+DEF_CMD(messageline_handle)
+{
+       struct mlinfo *mli = ci->home->data;
+
+       if (strcmp(ci->key, "Message") == 0) {
+               if (ci->extra == 0 || mli->message == NULL) {
+                       free(mli->message);
+                       mli->message = strdup(ci->str);
+                       pane_damaged(mli->line, DAMAGED_CONTENT);
+               }
+               return 0; /* allow other handlers */
+       }
+       if (strcmp(ci->key, "Abort") == 0) {
+               free(mli->message);
+               mli->message = strdup("ABORTED");
+               pane_damaged(mli->line, DAMAGED_CONTENT);
+               return 0;
+       }
+       if (strcmp(ci->key, "Refresh") == 0) {
+               if (ci->home == mli->line) {
+                       pane_resize(ci->home, 0, ci->home->parent->h - 1,
+                                   ci->home->parent->w, 1);
+                       pane_clear(mli->line, "");
+                       if (mli->message)
+                               pane_str(mli->line, mli->message, "bold", 0, 0);
+               } else {
+                       pane_resize(ci->home, 0, 0, ci->home->parent->w,
+                                   ci->home->parent->h - 1);
+               }
+               return 1;
+       }
+       /* Anything else clears the message */
+       if (strncmp(ci->key, "pane", 4) != 0 && mli->message) {
+               free(mli->message);
+               mli->message = NULL;
+               pane_clear(mli->line, "");
+       }
+       return 0;
+}
+
+DEF_CMD(messageline_attach)
+{
+       struct mlinfo *mli = malloc(sizeof(*mli));
+       struct pane *p = ci->focus;
+
+       mli->message = NULL;
+       ci->focus = pane_register(p, 0, &messageline_handle, mli, NULL);
+       mli->line = pane_register(p, 1, &messageline_handle, mli, NULL);
+       pane_focus(ci->focus);
+       return 1;
+}
+
+
+void edlib_init(struct editor *ed)
+{
+       key_add(ed->commands, "attach-messageline", &messageline_attach);
+}