diff options
-rw-r--r-- | runtime/autoload/usermark.vim | 7 | ||||
-rw-r--r-- | runtime/lua/vim/usermark.lua | 68 | ||||
-rw-r--r-- | runtime/plugin/usermark.vim | 1 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 1 |
4 files changed, 77 insertions, 0 deletions
diff --git a/runtime/autoload/usermark.vim b/runtime/autoload/usermark.vim new file mode 100644 index 0000000000..b1b4113d1a --- /dev/null +++ b/runtime/autoload/usermark.vim @@ -0,0 +1,7 @@ +" This is used for the default userreg function. + +lua vim.usermark = require('vim.usermark') + +function! usermark#func(action, mark) abort + return v:lua.vim.usermark.fn(a:action, a:mark) +endfunction diff --git a/runtime/lua/vim/usermark.lua b/runtime/lua/vim/usermark.lua new file mode 100644 index 0000000000..0d1ec0ae0f --- /dev/null +++ b/runtime/lua/vim/usermark.lua @@ -0,0 +1,68 @@ +-- Defualt implementation of the usermarkfunc. This default implementation is +-- extensible and allows other plugins to register handlers for different +-- registers. +-- +-- The default handler behaves just as a normal register would. + +local vim = assert(vim) +local usermark = {} + +-- Returns a "default handler" which behaves like normal global marks. When a +-- call to set() is made, it stores the current line and col of the cursor and +-- the filename of the current file. +function usermark._default_handler() + local d = {} + + -- Called when a mark is recalled using the "'" command. Just returns what was + -- stored before or nothing if it was never set before. + function d.get(self, mark) + return self.content or {} + end + + -- Called when a mark is set using the "m" command. Stores the current cursor + -- position to be recalled at a later time. + function d.set(self, mark) + local r,c = unpack(vim.api.nvim_win_get_cursor(0)) + local file = vim.fn.expand("%:p") + + self.content = { + line = r; + col = c; + } + + if file ~= '' then + self.content.file = file + end + end + + return d +end + +-- The store for register default handler +usermark._marktable = {} + +-- Function for the 'usermarkfunc'. Will defer to the handler associated with +-- the provided mark. +-- +-- If not handler is registered to a given mark, the default handler is used, +-- which is a re-implementation of standard mark behavior. +function usermark.fn(action, mark) + if not usermark._marktable[mark] then + usermark._marktable[mark] = usermark._default_handler() + end + + if action == "get" then + return usermark._marktable[mark]:get(mark) + else + usermark._marktable[mark]:set(mark) + return nil + end +end + +-- Registers a handler with a mark. Gets and sets will then defer to this +-- handler when determining the mark's behavior. +function usermark.register_handler(mark, handler) + usermark._marktable[mark] = handler +end + +return usermark diff --git a/runtime/plugin/usermark.vim b/runtime/plugin/usermark.vim new file mode 100644 index 0000000000..917e7510f1 --- /dev/null +++ b/runtime/plugin/usermark.vim @@ -0,0 +1 @@ +set usermarkfunc=usermark#func diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 0a02ba5a03..4ad3f5860b 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -3139,6 +3139,7 @@ static void f_has(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) "title", "user-commands", // was accidentally included in 5.4 "user_commands", + "usermarks", "vartabs", "vertsplit", "vimscript-1", |