Skip to content

Commit

Permalink
vis-lua: make selection first class primitives in Lua API
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Jul 14, 2017
1 parent 546917d commit 386eac2
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 115 deletions.
12 changes: 6 additions & 6 deletions lua/plugins/complete-filename.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- complete file path at primary cursor location using vis-complete(1)
-- complete file path at primary selection location using vis-complete(1)

vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
local win = vis.win
local file = win.file
local pos = win.cursor.pos
local pos = win.selection.pos
if not pos then return end
-- TODO do something clever here
local range = file:text_object_word(pos > 0 and pos-1 or pos);
Expand All @@ -19,15 +19,15 @@ vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
return
end
file:insert(pos, out)
win.cursor.pos = pos + #out
win.selection.pos = pos + #out
end, "Complete file path")

-- complete file path at primary cursor location using vis-open(1)
-- complete file path at primary selection location using vis-open(1)

vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
local win = vis.win
local file = win.file
local pos = win.cursor.pos
local pos = win.selection.pos
if not pos then return end
local range = file:text_object_word(pos > 0 and pos-1 or pos);
if not range then return end
Expand All @@ -48,5 +48,5 @@ vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
out = out:gsub("\n$", "")
file:delete(range)
file:insert(range.start, out)
win.cursor.pos = range.start + #out
win.selection.pos = range.start + #out
end, "Complete file name")
6 changes: 3 additions & 3 deletions lua/plugins/complete-word.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- complete word at primary cursor location using vis-complete(1)
-- complete word at primary selection location using vis-complete(1)

vis:map(vis.modes.INSERT, "<C-n>", function()
local win = vis.win
local file = win.file
local pos = win.cursor.pos
local pos = win.selection.pos
if not pos then return end
local range = file:text_object_word(pos > 0 and pos-1 or pos);
if not range then return end
Expand All @@ -18,5 +18,5 @@ vis:map(vis.modes.INSERT, "<C-n>", function()
return
end
file:insert(pos, out)
win.cursor.pos = pos + #out
win.selection.pos = pos + #out
end, "Complete word in file")
6 changes: 3 additions & 3 deletions lua/plugins/number-inc-dec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ local change = function(delta)
if not count then count = 1 end
vis.count = nil -- reset count, otherwise it affects next motion

for cursor in win:cursors_iterator() do
local pos = cursor.pos
for selection in win:selections_iterator() do
local pos = selection.pos
if not pos then goto continue end
local word = file:text_object_word(pos);
if not word then goto continue end
Expand Down Expand Up @@ -50,7 +50,7 @@ local change = function(delta)
end
file:delete(s, e - s)
file:insert(s, number)
cursor.pos = s
selection.pos = s
::continue::
end
end
Expand Down
12 changes: 6 additions & 6 deletions lua/vis-std.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ vis.events.subscribe(vis.events.WIN_STATUS, function(win)
local left_parts = {}
local right_parts = {}
local file = win.file
local cursor = win.cursor
local selection = win.selection

local mode = modes[vis.mode]
if mode ~= '' and vis.win == win then
Expand All @@ -91,18 +91,18 @@ vis.events.subscribe(vis.events.WIN_STATUS, function(win)
table.insert(left_parts, (file.name or '[No Name]') ..
(file.modified and ' [+]' or '') .. (vis.recording and ' @' or ''))

if #win.cursors > 1 then
table.insert(right_parts, cursor.number..'/'..#win.cursors)
if #win.selections > 1 then
table.insert(right_parts, selection.number..'/'..#win.selections)
end

local size = file.size
local pos = cursor.pos
local pos = selection.pos
if not pos then pos = 0 end
table.insert(right_parts, (size == 0 and "0" or math.ceil(pos/size*100)).."%")

if not win.large then
local col = cursor.col
table.insert(right_parts, cursor.line..', '..col)
local col = selection.col
table.insert(right_parts, selection.line..', '..col)
if size > 33554432 or col > 65536 then
win.large = true
end
Expand Down
7 changes: 3 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,7 @@ static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
}
if (sel_new) {
view_selections_primary_set(sel_new);
if (anchored)
view_selections_anchor(sel_new);
view_selections_anchor(sel_new, anchored);
}
}
vis_count_set(vis, VIS_COUNT_UNKNOWN);
Expand Down Expand Up @@ -1452,7 +1451,7 @@ static const char *selections_match_next(Vis *vis, const char *keys, const Arg *
size_t pos = text_char_prev(txt, word.end);
if ((s = view_selections_new(view, pos))) {
view_selections_set(s, &word);
view_selections_anchor(s);
view_selections_anchor(s, true);
view_selections_primary_set(s);
goto out;
}
Expand All @@ -1465,7 +1464,7 @@ static const char *selections_match_next(Vis *vis, const char *keys, const Arg *
size_t pos = text_char_prev(txt, word.end);
if ((s = view_selections_new(view, pos))) {
view_selections_set(s, &word);
view_selections_anchor(s);
view_selections_anchor(s, true);
view_selections_primary_set(s);
}

Expand Down
6 changes: 3 additions & 3 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) {
if (c->cursor) {
if (visual) {
view_selections_set(c->cursor, &sel);
view_selections_anchor(c->cursor);
view_selections_anchor(c->cursor, true);
} else {
if (memchr(c->data, '\n', c->len))
view_cursors_to(c->cursor, sel.start);
Expand All @@ -1235,7 +1235,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) {
Selection *cursor = view_selections_new(c->win->view, sel.start);
if (cursor) {
view_selections_set(cursor, &sel);
view_selections_anchor(cursor);
view_selections_anchor(cursor, true);
}
}
}
Expand Down Expand Up @@ -1512,7 +1512,7 @@ static bool cmd_print(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele
return false;
if (range->start != range->end) {
view_selections_set(sel, range);
view_selections_anchor(sel);
view_selections_anchor(sel, true);
} else {
view_cursors_to(sel, range->start);
view_selection_clear(sel);
Expand Down
2 changes: 1 addition & 1 deletion test
Submodule test updated 2 files
+50 −50 lua/cursor.lua
+1 −1 lua/map-basic.lua
4 changes: 2 additions & 2 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,8 @@ void view_cursors_place(Selection *s, size_t line, size_t col) {
view_cursors_to(s, pos);
}

void view_selections_anchor(Selection *s) {
s->anchored = true;
void view_selections_anchor(Selection *s, bool anchored) {
s->anchored = anchored;
}

void view_selection_clear(Selection *s) {
Expand Down
2 changes: 1 addition & 1 deletion view.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void view_selections_flip(Selection*);
* Anchor selection.
* Further updates will only update the cursor, the anchor will remain fixed.
*/
void view_selections_anchor(Selection*);
void view_selections_anchor(Selection*, bool anchored);
/** Check whether selection is anchored. */
bool view_selections_anchored(Selection*);
/** Get position of selection cursor. */
Expand Down
Loading

0 comments on commit 386eac2

Please sign in to comment.