]> git.neil.brown.name Git - edlib.git/commitdiff
Allow command name to be used to choose some data files.
authorNeilBrown <neil@brown.name>
Fri, 25 Aug 2023 01:10:37 +0000 (11:10 +1000)
committerNeilBrown <neil@brown.name>
Fri, 25 Aug 2023 05:35:37 +0000 (15:35 +1000)
The icon file, Welcome text, and initial config file are now chosen
based on the base name of the command being run.
So if "edlib" is copied to "elma", then "elma.ini" is used for config,
and "elma-icon.png" for an icon.
If the expected file isn't found, a file named for "edlib" is tried
instead.

This should make it easy to have have various commands providing
different interfaces.
 elma - EMACS mode
 elvi - VI mode
 elnm - notmuch email reader
 elpnt - presentation mode
 eled - editor with A binding.
maybe.

Signed-off-by: NeilBrown <neil@brown.name>
DOC/TODO.md
Makefile
core-editor.c
core.h
display-x11-xcb.c
edlib.c

index a7f360ecff446ad49fd9ad2181fc55d0f3e606b6..77edd7185e092eafc14915a3061515d3b70d96af 100644 (file)
@@ -98,7 +98,7 @@ Requirements for a v1.0 release
 - [ ] vi mode
 - [ ] CUA mode
 - [ ] nano mode(?)
-- [ ] multiple front ends: elvi, elma, elnm, eled?
+- [ ] multiple front ends: elvi, elma, elnm, eled, elpnt?
 - [ ] introspection
 - [ ] markdown editor (with PDF output)
 - [ ] non-line-based render, such as a tabular render for spreadsheet,
index 0f1205b229dae9c2e6611226ac20e8a51039f77b..537b013b3899719b69ca24b4eadf9d5e79731370 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -94,9 +94,10 @@ SHOBJ = O/doc-text.o O/doc-dir.o O/doc-docs.o \
 XOBJ = O/rexel.o
 WOBJ = O/libwiggle.a
 
-BIN = edlib elc el-askpass
+BIN = edlib elma elc el-askpass
 
 bin/edlib : edlib
+bin/elma : edlib
 bin/elc : python/lib-server.py
 bin/el-askpass : python/lib-server.py
 
index da680677a6bfa0e2d049891f80dbd521c4aaaf75..988208cedd48a27b2e8419642c6f8157c8c2a05e 100644 (file)
@@ -630,7 +630,9 @@ static char *set_bin_path(struct pane *p safe)
 DEF_CMD(global_find_file)
 {
        /*
-        * ->str is a file basename.
+        * ->str is a file basename.  If it contains {COMM}, that will
+        * be replaced with the "command-name" attr from root, or
+        * "edlib" if nothing can be found.
         * ->str2 is one of "data", "config", "bin"
         * We find a file with basename in a known location following
         * the XDG Base Directory Specificaton.
@@ -649,8 +651,11 @@ DEF_CMD(global_find_file)
         * For bin we look in $HERE/../bin and $PATH
         */
        char *path = NULL;
+       const char *base[2] = {ci->str, NULL};
+       int i;
+       char *cn;
 
-       if (ci->str == NULL || ci->str2 == NULL)
+       if (base[0] == NULL || ci->str2 == NULL)
                return -Enoarg;
        if (strcmp(ci->str2, "data") == 0)
                path = set_data_path(ci->home);
@@ -661,25 +666,38 @@ DEF_CMD(global_find_file)
 
        if (!path)
                return Einval;
-       for (; path && *path; path += strlen(path)+1) {
-               char *p = strconcat(NULL, path, ci->str);
-               int fd;
-               if (!p)
-                       continue;
-               fd = open(p, O_RDONLY);
-               if (fd < 0) {
+       cn = strstr(base[0], "{COMM}");
+       if (cn) {
+               char *p = strndup(base[0], cn - base[0]);
+               char *comm = attr_find(ci->home->attrs, "command-name");
+               if (!comm)
+                       comm = "edlib";
+               base[0] = strconcat(ci->home, p, comm, cn+6);
+               if (strcmp(comm, "edlib") != 0)
+                       base[1] = strconcat(ci->home, p, "edlib", cn+6);
+       }
+       for (i = 0; i < 2 && base[i] ; i++) {
+               char *pth;
+               for (pth = path; pth && *pth; pth += strlen(pth)+1) {
+                       char *p = strconcat(NULL, pth, base[i]);
+                       int fd;
+                       if (!p)
+                               continue;
+                       fd = open(p, O_RDONLY);
+                       if (fd < 0) {
+                               free(p);
+                               continue;
+                       }
+                       close(fd);
+                       comm_call(ci->comm2, "cb", ci->focus, 0, NULL, p);
                        free(p);
-                       continue;
+                       return 1;
                }
-               close(fd);
-               comm_call(ci->comm2, "cb", ci->focus, 0, NULL, p);
-               free(p);
-               return 1;
        }
        return Efalse;
 }
 
-struct pane *editor_new(void)
+struct pane *editor_new(const char *comm_name)
 {
        struct pane *ed;
        struct ed_info *ei;
@@ -708,6 +726,7 @@ struct pane *editor_new(void)
                return NULL;
        ei = &ed->data;
        ei->magic = ED_MAGIC;
+       attr_set_str(&ed->attrs, "command-name", comm_name ?: "edlib");
        ei->testing = (getenv("EDLIB_TESTING") != NULL);
        ei->map = key_alloc();
        key_add_chain(ei->map, ed_map);
diff --git a/core.h b/core.h
index b6cae64b52704ef143bf278c6a172b7da1b56d0d..68bc788890c141c2bb1d7dfad59db9d0cca16372 100644 (file)
--- a/core.h
+++ b/core.h
@@ -111,7 +111,7 @@ int do_pane_notify(struct pane *home, const char *notification safe,
                   struct command *comm2);
 void pane_drop_notifiers(struct pane *p safe, char *notification);
 
-struct pane *editor_new(void);
+struct pane *editor_new(const char *comm_name);
 void * safe memsave(struct pane *p safe, const char *buf, int len);
 char *strsave(struct pane *p safe, const char *buf);
 char *strnsave(struct pane *p safe, const char *buf, int len);
index f27c87007309666e8fc811847dda2913f3b85174..b5bd7aa8991b923e6ac7dbb4c67779293d575b14 100644 (file)
@@ -1953,7 +1953,7 @@ static struct pane *xcb_display_init(const char *d safe,
                        XCB_MOD_MASK_LOCK |
                        XCB_MOD_MASK_CONTROL);
 
-       xcb_load_icon(focus, xd, "edlib-icon.png");
+       xcb_load_icon(focus, xd, "{COMM}-icon.png");
        xcb_map_window(conn, xd->win);
        xcb_flush(conn);
        pane_resize(p, 0, 0, xd->charwidth*80, xd->lineheight*26);
diff --git a/edlib.c b/edlib.c
index 9955a8e8c83e8ef8d4d11263bd2afa039c3fba94..c4e8d47c20270d46f2b0ebaf54438d3247395981 100644 (file)
--- a/edlib.c
+++ b/edlib.c
@@ -21,11 +21,21 @@ static const char shortopt[] = "gtx";
 
 int main(int argc, char *argv[])
 {
-       struct pane *ed = editor_new();
+       struct pane *ed;
        struct pane *first_window = NULL;
        struct pane *p, *doc = NULL;
        bool gtk = False, term = False, x11 = False;
        int opt;
+       char *base = NULL;
+
+       if (argv[0]) {
+               base = strrchr(argv[0], '/');
+               if (base)
+                       base += 1;
+               else
+                       base = argv[0];
+       }
+       ed = editor_new(base);
 
        if (!ed)
                exit(1);
@@ -50,7 +60,7 @@ int main(int argc, char *argv[])
        setlocale(LC_CTYPE, "enUS.UTF-8");
 
        call("global-load-module", ed, 0, NULL, "lib-config");
-       call("config-load", ed, 0, NULL, "edlib.ini");
+       call("config-load", ed, 0, NULL, "{COMM}.ini");
 
        call("attach-doc-docs", ed);
 
@@ -68,7 +78,7 @@ int main(int argc, char *argv[])
 
        if (!doc) {
                char *welcome_file = call_ret(str, "xdg-find-edlib-file", ed,
-                                             0, NULL, "Welcome-edlib.txt",
+                                             0, NULL, "Welcome-{COMM}.txt",
                                              0, NULL, "data");
                char *WelcomeText = NULL;
                if (welcome_file) {