diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-02-17 20:59:26 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-02-18 16:50:40 +0100 |
| commit | 44ff1f95ecc14521fb2a1cec67a628140fc46598 (patch) | |
| tree | 61e5439273640ebd33747637d6e8359a1dee056b /vis-motions.c | |
| parent | 58df45177a7db63223c8459be7aef727a0cc22f7 (diff) | |
| download | vis-44ff1f95ecc14521fb2a1cec67a628140fc46598.tar.gz vis-44ff1f95ecc14521fb2a1cec67a628140fc46598.tar.xz | |
Add infrastructure to add custom motion functions
A motion function can be registered with vis_motion_register(...)
the returned id (if non negative) can then be used as an argument
to vis_motion(...)
Diffstat (limited to 'vis-motions.c')
| -rw-r--r-- | vis-motions.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/vis-motions.c b/vis-motions.c index e0db048..6111d3e 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -5,6 +5,7 @@ #include "text-motions.h" #include "text-objects.h" #include "text-util.h" +#include "util.h" /** utility functions */ @@ -210,6 +211,23 @@ void vis_motion_type(Vis *vis, enum VisMotionType type) { vis->action.type = type; } +int vis_motion_register(Vis *vis, enum VisMotionType type, void *data, + size_t (*motion)(Vis*, Win*, void*, size_t pos)) { + + Movement *move = calloc(1, sizeof *move); + if (!move) + return -1; + + move->user = motion; + move->type = type; + move->data = data; + + if (array_add(&vis->motions, move)) + return VIS_MOVE_LAST + array_length(&vis->motions) - 1; + free(move); + return -1; +} + bool vis_motion(Vis *vis, enum VisMotion motion, ...) { va_list ap; va_start(ap, motion); @@ -287,7 +305,14 @@ bool vis_motion(Vis *vis, enum VisMotion motion, ...) { break; } - vis->action.movement = &vis_motions[motion]; + if (motion < LENGTH(vis_motions)) + vis->action.movement = &vis_motions[motion]; + else + vis->action.movement = array_get(&vis->motions, motion - VIS_MOVE_LAST); + + if (!vis->action.movement) + goto err; + va_end(ap); action_do(vis, &vis->action); return true; |
