Set the editing mode per view

This commit is contained in:
Francesco Abbate 2021-03-24 11:17:42 +01:00
parent 50bd5e8b2b
commit b0438b60dc
5 changed files with 43 additions and 19 deletions

View File

@ -172,15 +172,16 @@ command.add(nil, {
end)
end,
["core:toggle-vim-mode"] = function()
local mode = core.get_editing_mode(core.active_view)
if mode == 'standard' then
core.set_editing_mode(core.active_view, 'command')
else
core.set_editing_mode(core.active_view, 'standard')
end
end,
["core:set-insert-mode"] = function()
core.mode = 'insert'
end,
["core:set-command-mode"] = function()
core.mode = 'command'
end,
["core:set-standard-mode"] = function()
core.mode = 'standard'
core.set_editing_mode(core.active_view, 'insert')
end,
})

View File

@ -56,6 +56,7 @@ function DocView:new(doc)
self.font = "code_font"
self.last_x_offset = {}
self.blink_timer = 0
self.editing_mode = 'standard'
end
@ -168,6 +169,16 @@ function DocView:get_x_offset_col(line, x)
end
function DocView:get_editing_mode()
return self.editing_mode
end
function DocView:set_editing_mode(mode)
self.editing_mode = mode
end
function DocView:resolve_screen_position(x, y)
local ox, oy = self:get_line_screen_position(1)
local line = math.floor((y - oy) / self:get_line_height()) + 1

View File

@ -394,7 +394,6 @@ function core.init()
core.clip_rect_stack = {{ 0,0,0,0 }}
core.log_items = {}
core.docs = {}
core.mode = 'standard'
core.threads = setmetatable({}, { __mode = "k" })
local project_dir_abs = system.absolute_path(project_dir)
@ -717,12 +716,12 @@ end
function core.on_event(type, ...)
local did_keymap = false
if type == "textinput" and (core.mode == 'insert' or core.mode == 'standard') then
if type == "textinput" then
core.root_view:on_text_input(...)
elseif type == "keypressed" then
did_keymap = keymap.on_key_pressed(core.mode, ...)
did_keymap = keymap.on_key_pressed(...)
elseif type == "keyreleased" then
keymap.on_key_released(core.mode, ...)
keymap.on_key_released(...)
elseif type == "mousemoved" then
core.root_view:on_mouse_moved(...)
elseif type == "mousepressed" then
@ -908,5 +907,16 @@ core.add_save_hook(function(filename)
end)
function core.get_editing_mode(view)
return view.get_editing_mode and view:get_editing_mode() or 'standard'
end
function core.set_editing_mode(view, mode)
if view.set_editing_mode then
view:set_editing_mode(mode)
end
end
return core

View File

@ -108,7 +108,7 @@ function keymap.get_binding(cmd)
end
function keymap.on_key_pressed(editor_mode, k)
function keymap.on_key_pressed(k)
local mk = modkey_map[k]
if mk then
keymap.modkeys[mk] = true
@ -118,7 +118,8 @@ function keymap.on_key_pressed(editor_mode, k)
end
else
local stroke = key_to_stroke(k)
if editor_mode == 'command' then
local mode = core.get_editing_mode(core.active_view)
if mode == 'command' then
if keymap.command_verb == '.' and table_find(keymap.vim_verbs_imm, stroke) then
keymap.vim_execute(stroke, keymap.command_mult)
keymap.reset_vim_command()
@ -134,9 +135,9 @@ function keymap.on_key_pressed(editor_mode, k)
keymap.reset_vim_command()
return true
end
elseif editor_mode == 'insert' then
elseif mode == 'insert' then
if stroke == 'escape' then
core.mode = 'command'
core.set_editing_mode(core.active_view, 'command')
return true
end
return false
@ -154,7 +155,7 @@ function keymap.on_key_pressed(editor_mode, k)
end
function keymap.on_key_released(editor_mode, k)
function keymap.on_key_released(k)
local mk = modkey_map[k]
if mk then
keymap.modkeys[mk] = false

View File

@ -110,6 +110,7 @@ function StatusView:get_items()
local indent = dv.doc.indent_info
local indent_label = (indent and indent.type == "hard") and "tabs: " or "spaces: "
local indent_size = indent and tostring(indent.size) .. (indent.confirmed and "" or "*") or "unknown"
local editing_mode = core.get_editing_mode(dv)
return {
dirty and style.accent or style.text, style.icon_font, "f",
@ -124,7 +125,7 @@ function StatusView:get_items()
self.separator,
string.format("%d%%", line / #dv.doc.lines * 100),
}, {
style.caret, core.mode ~= 'standard' and string.upper(core.mode) or '', style.text, self.separator2,
style.caret, editing_mode ~= 'standard' and string.upper(editing_mode) or '', style.text, self.separator2,
indent_label, indent_size,
style.dim, self.separator2, style.text,
style.icon_font, "g",