include libgen.h for basename
[elinks.git] / src / dialogs / exmode.c
blob3f08fb13e8191873d6859f4625f85fcc9e5a9124
1 /* Ex-mode-like commandline support */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <ctype.h>
8 #include <string.h>
10 #include "elinks.h"
12 #include "bfu/dialog.h"
13 #include "config/conf.h"
14 #include "config/kbdbind.h"
15 #include "config/options.h"
16 #include "dialogs/exmode.h"
17 #include "intl/gettext/libintl.h"
18 #include "main/module.h"
19 #include "session/session.h"
20 #include "session/task.h"
21 #include "terminal/terminal.h"
22 #include "util/error.h"
23 #include "util/memory.h"
24 #include "util/string.h"
25 #include "viewer/action.h"
28 /* The Ex-mode commandline is that blue-yellow thing which appears at the
29 * bottom of the screen when you press ':' and lets you enter various commands
30 * (just like in vi), especially actions, events (where they make sense) and
31 * config-file commands. */
34 #define EXMODE_HISTORY_FILENAME "exmodehist"
36 static INIT_INPUT_HISTORY(exmode_history);
38 typedef int (*exmode_handler_T)(struct session *, unsigned char *, unsigned char *);
40 static int
41 exmode_action_handler(struct session *ses, unsigned char *command,
42 unsigned char *args)
44 enum main_action action_id = get_action_from_string(KEYMAP_MAIN, command);
46 if (action_id == ACT_MAIN_NONE) return 0;
47 if (action_id == ACT_MAIN_QUIT) action_id = ACT_MAIN_REALLY_QUIT;
49 if (!*args)
50 return do_action(ses, action_id, 0) != FRAME_EVENT_IGNORED;
52 switch (action_id) {
53 case ACT_MAIN_GOTO_URL:
54 goto_url_with_hook(ses, args);
55 return 1;
56 default:
57 break;
59 return 0;
62 static int
63 exmode_confcmd_handler(struct session *ses, unsigned char *command,
64 unsigned char *args)
66 enum parse_error err;
68 assert(ses && command && args);
70 if (get_cmd_opt_bool("anonymous"))
71 return 0;
73 /* Undo the arguments separation. */
74 if (*args) *(--args) = ' ';
76 err = parse_config_exmode_command(command);
77 return err;
80 static const exmode_handler_T exmode_handlers[] = {
81 exmode_action_handler,
82 exmode_confcmd_handler,
83 NULL,
86 static void
87 exmode_exec(struct session *ses, unsigned char buffer[INPUT_LINE_BUFFER_SIZE])
89 /* First look it up as action, then try it as an event (but the event
90 * part should be thought out somehow yet, I s'pose... let's leave it
91 * off for now). Then try to evaluate it as configfile command. Then at
92 * least pop up an error. */
93 unsigned char *command = buffer;
94 unsigned char *args = command;
95 int i;
97 while (*command == ':') command++;
99 if (!*command) return;
101 add_to_input_history(&exmode_history, command, 1);
103 skip_nonspace(args);
104 if (*args) *args++ = 0;
106 for (i = 0; exmode_handlers[i]; i++) {
107 if (exmode_handlers[i](ses, command, args))
108 break;
113 static enum input_line_code
114 exmode_input_handler(struct input_line *input_line, int action_id)
116 switch (action_id) {
117 case ACT_EDIT_ENTER:
118 exmode_exec(input_line->ses, input_line->buffer);
119 return INPUT_LINE_CANCEL;
121 default:
122 return INPUT_LINE_PROCEED;
126 void
127 exmode_start(struct session *ses)
129 input_field_line(ses, ":", NULL, &exmode_history, exmode_input_handler);
133 static void
134 init_exmode(struct module *module)
136 load_input_history(&exmode_history, EXMODE_HISTORY_FILENAME);
139 static void
140 done_exmode(struct module *module)
142 save_input_history(&exmode_history, EXMODE_HISTORY_FILENAME);
143 free_list(exmode_history.entries);
146 struct module exmode_module = struct_module(
147 /* name: */ N_("Exmode"),
148 /* options: */ NULL,
149 /* hooks: */ NULL,
150 /* submodules: */ NULL,
151 /* data: */ NULL,
152 /* init: */ init_exmode,
153 /* done: */ done_exmode