]> git.neil.brown.name Git - edlib.git/blob - lib-keymap.c
core-log: reduce number of statics
[edlib.git] / lib-keymap.c
1 /*
2  * Copyright Neil Brown ©2015-2023 <neil@brown.name>
3  * May be distributed under terms of GPLv2 - see file:COPYING
4  *
5  * Keymap management panes for edlib.
6  *
7  * A keymap pane makes it easy to attach keymaps into a pane tree.
8  *   global-set-keymap
9  * is given a command which is used to as all incoming requests.
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #define PANE_DATA_TYPE struct key_data
15 #include "core.h"
16
17 struct key_data {
18         struct command  *globalcmd;
19 };
20 #include "core-pane.h"
21
22 static struct pane *safe do_keymap_attach(struct pane *p safe);
23
24 DEF_CMD(keymap_handle)
25 {
26         struct key_data *kd = ci->home->data;
27
28         if (strcmp(ci->key, "Close") == 0) {
29                 command_put(kd->globalcmd);
30                 return 1;
31         }
32         if (strcmp(ci->key, "Clone") == 0) {
33                 struct pane *p = do_keymap_attach(ci->focus);
34                 struct key_data *kd_old = ci->home->data;
35                 struct key_data *kd_new;
36                 if (!p)
37                         return Efail;
38                 kd_new = p->data;
39                 if (kd_old->globalcmd)
40                         kd_new->globalcmd = command_get(kd_old->globalcmd);
41
42                 pane_clone_children(ci->home, p);
43                 return 1;
44         }
45
46         if (kd->globalcmd) {
47                 int ret;
48                 ((struct cmd_info*)ci)->comm = kd->globalcmd;
49                 ret = kd->globalcmd->func(ci);
50                 if (ret)
51                         return ret;
52         }
53         if (strcmp(ci->key, "global-set-keymap") == 0) {
54                 struct command *cm = ci->comm2;
55                 if (!cm)
56                         return Enoarg;
57                 command_put(kd->globalcmd);
58                 kd->globalcmd = command_get(cm);
59                 return 1;
60         }
61
62         return Efallthrough;
63 }
64
65 static struct pane *safe do_keymap_attach(struct pane *p safe)
66 {
67         return pane_register(p, 0, &keymap_handle);
68 }
69
70 DEF_CMD(keymap_attach)
71 {
72         struct pane *p = do_keymap_attach(ci->focus);
73         if (p)
74                 return comm_call(ci->comm2, "callback:attach", p);
75         return Efail;
76 }
77
78 void edlib_init(struct pane *ed safe)
79 {
80         call_comm("global-set-command", ed, &keymap_attach, 0, NULL,
81                   "attach-global-keymap");
82 }