aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-10-26 21:50:31 +0100
committerMarc André Tanner <mat@brain-dump.org>2015-10-27 11:13:07 +0100
commit6a7e5a3cfc86db049f595bd20d20041d7346e4ef (patch)
treef1b2b108738935bac9048e78ba27d55cd91a9e47
parentbc9909f4cd9355c197944ee320eed3971431f699 (diff)
downloadvis-6a7e5a3cfc86db049f595bd20d20041d7346e4ef.tar.gz
vis-6a7e5a3cfc86db049f595bd20d20041d7346e4ef.tar.xz
vis: introduce vis_prompt_enter API
This is a really bad API/abtraction but at least it allows us to hide some implementation details.
-rw-r--r--main.c21
-rw-r--r--vis.c23
-rw-r--r--vis.h8
3 files changed, 24 insertions, 28 deletions
diff --git a/main.c b/main.c
index 89f649a..6e86494 100644
--- a/main.c
+++ b/main.c
@@ -87,8 +87,6 @@ static const char *insert_register(Vis*, const char *keys, const Arg *arg);
/* show a user prompt to get input with title arg->s */
static const char *prompt_search(Vis*, const char *keys, const Arg *arg);
static const char *prompt_cmd(Vis*, const char *keys, const Arg *arg);
-/* evaluate user input at prompt, perform search or execute a command */
-static const char *prompt_enter(Vis*, const char *keys, const Arg *arg);
/* exit command mode if the last char is deleted */
static const char *prompt_backspace(Vis*, const char *keys, const Arg *arg);
/* blocks to read 3 consecutive digits and inserts the corresponding byte value */
@@ -764,7 +762,7 @@ static KeyAction vis_action[] = {
[VIS_ACTION_PROMPT_ENTER] = {
"prompt-enter",
"Execute current prompt content",
- prompt_enter
+ call, { .f = vis_prompt_enter }
},
[VIS_ACTION_PROMPT_SHOW_VISUAL] = {
"prompt-show-visual",
@@ -1369,25 +1367,10 @@ static const char *prompt_cmd(Vis *vis, const char *keys, const Arg *arg) {
return keys;
}
-static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) {
- char *s = vis_prompt_get(vis);
- /* it is important to switch back to the previous mode, which hides
- * the prompt and more importantly resets vis->win to the currently
- * focused editor window *before* anything is executed which depends
- * on vis->win.
- */
- vis_mode_set(vis, vis->mode_before_prompt);
- if (s && *s && vis_prompt_cmd(vis, vis->prompt_type, s) && vis->running)
- vis_mode_switch(vis, VIS_MODE_NORMAL);
- free(s);
- vis_draw(vis);
- return keys;
-}
-
static const char *prompt_backspace(Vis *vis, const char *keys, const Arg *arg) {
char *cmd = vis_prompt_get(vis);
if (!cmd || !*cmd)
- prompt_enter(vis, keys, NULL);
+ vis_mode_switch(vis, VIS_MODE_NORMAL);
else
delete(vis, keys, &(const Arg){ .i = MOVE_CHAR_PREV });
free(cmd);
diff --git a/vis.c b/vis.c
index f690ec1..f8853cd 100644
--- a/vis.c
+++ b/vis.c
@@ -126,6 +126,7 @@ typedef struct { /* command definitions for the ':'-prompt */
enum CmdOpt opt; /* command option flags */
} Command;
+static void mode_set(Vis *vis, Mode *new_mode);
/** window / file handling */
static void file_free(Vis *vis, File *file) {
@@ -766,7 +767,7 @@ static void vis_mode_operator_leave(Vis *vis, Mode *new) {
static void vis_mode_operator_input(Vis *vis, const char *str, size_t len) {
/* invalid operator */
action_reset(vis, &vis->action);
- vis_mode_set(vis, vis->mode_prev);
+ mode_set(vis, vis->mode_prev);
}
static void vis_mode_visual_enter(Vis *vis, Mode *old) {
@@ -1513,7 +1514,7 @@ static void action_do(Vis *vis, Action *a) {
if (a->op == &ops[OP_CHANGE])
vis_mode_switch(vis, VIS_MODE_INSERT);
else if (vis->mode == &vis_modes[VIS_MODE_OPERATOR])
- vis_mode_set(vis, vis->mode_prev);
+ mode_set(vis, vis->mode_prev);
else if (vis->mode->visual)
vis_mode_switch(vis, VIS_MODE_NORMAL);
text_snapshot(txt);
@@ -1536,7 +1537,7 @@ static void action_reset(Vis *vis, Action *a) {
a->reg = NULL;
}
-void vis_mode_set(Vis *vis, Mode *new_mode) {
+static void mode_set(Vis *vis, Mode *new_mode) {
if (vis->mode == new_mode)
return;
if (vis->mode->leave)
@@ -2851,7 +2852,7 @@ bool vis_operator(Vis *vis, enum VisOperator id) {
}
void vis_mode_switch(Vis *vis, enum VisMode mode) {
- vis_mode_set(vis, &vis_modes[mode]);
+ mode_set(vis, &vis_modes[mode]);
}
bool vis_motion(Vis *vis, enum VisMotion motion, ...) {
@@ -3075,3 +3076,17 @@ void vis_insert_nl(Vis *vis) {
if (vis->autoindent)
copy_indent_from_previous_line(vis->win);
}
+
+void vis_prompt_enter(Vis *vis) {
+ char *s = vis_prompt_get(vis);
+ /* it is important to switch back to the previous mode, which hides
+ * the prompt and more importantly resets vis->win to the currently
+ * focused editor window *before* anything is executed which depends
+ * on vis->win.
+ */
+ mode_set(vis, vis->mode_before_prompt);
+ if (s && *s && vis_prompt_cmd(vis, vis->prompt_type, s) && vis->running)
+ vis_mode_switch(vis, VIS_MODE_NORMAL);
+ free(s);
+ vis_draw(vis);
+}
diff --git a/vis.h b/vis.h
index c427d06..80e781c 100644
--- a/vis.h
+++ b/vis.h
@@ -44,8 +44,6 @@ typedef struct {
const char *alias;
} KeyBinding;
-
-
Vis *vis_new(Ui*);
void vis_free(Vis*);
void vis_resize(Vis*);
@@ -77,6 +75,8 @@ void vis_window_next(Vis*);
void vis_window_prev(Vis*);
/* display a user prompt with a certain title and default text */
void vis_prompt_show(Vis*, const char *title, const char *text);
+/* TODO: bad abstraction */
+void vis_prompt_enter(Vis*);
/* hide the user prompt if it is currently shown */
void vis_prompt_hide(Vis*);
/* return the content of the command prompt in a malloc(3)-ed string
@@ -124,9 +124,6 @@ bool vis_mode_map(Vis*, enum VisMode, const char *name, KeyBinding*);
bool vis_mode_unmap(Vis*, enum VisMode, const char *name);
bool vis_mode_bindings(Vis*, enum VisMode, KeyBinding **bindings);
const char *vis_mode_status(Vis*);
-/* TODO: temporary */
-typedef struct Mode Mode;
-void vis_mode_set(Vis*, Mode*);
bool vis_action_register(Vis*, KeyAction*);
@@ -335,6 +332,7 @@ bool vis_signal_handler(Vis*, int signum, const siginfo_t *siginfo,
const void *context);
/* TODO: temporary */
+typedef struct Mode Mode;
typedef struct Operator Operator;
typedef struct Movement Movement;
typedef struct TextObject TextObject;