Add 'delete' and 'deleteall' commands

This commit is contained in:
Diogo Duarte
2024-02-09 19:17:17 +00:00
parent 5424f5431c
commit be45ad7367
2 changed files with 35 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
# Bookmarks.yazi # Bookmarks.yazi
Simple implementation of [vi-like marks](https://neovim.io/doc/user/motion.html#mark-motions) for [yazi](https://github.com/sxyazi/yazi). A [Yazi](https://github.com/sxyazi/yazi) plugin that adds the basic functionality of [vi-like marks](https://neovim.io/doc/user/motion.html#mark-motions).
```toml ```toml
[[manager.prepend_keymap]] [[manager.prepend_keymap]]
@@ -15,3 +15,17 @@ on = [ "'" ]
exec = "plugin bookmarks --sync --args='jump'" exec = "plugin bookmarks --sync --args='jump'"
desc = "Jump to a bookmark" desc = "Jump to a bookmark"
``` ```
```toml
[[manager.prepend_keymap]]
on = [ "b", "d" ]
exec = "plugin bookmarks --sync --args='delete'"
desc = "Jump to a bookmark"
```
```toml
[[manager.prepend_keymap]]
on = [ "b", "D" ]
exec = "plugin bookmarks --sync --args='deleteall'"
desc = "Jump to a bookmark"
```

View File

@@ -37,6 +37,15 @@ local function jump_to_bookmark(bookmark)
ya.manager_emit("arrow", { selected_bookmark.cursor }) ya.manager_emit("arrow", { selected_bookmark.cursor })
end end
local function delete_bookmark(bookmark)
state.bookmarks = state.bookmarks or {}
state.bookmarks[bookmark] = nil
end
local function delete_all_bookmarks()
state.bookmarks = nil
end
local function next(sync, args) local function next(sync, args)
ya.manager_emit("plugin", { "bookmarks", sync = sync, args = table.concat(args, " ") }) ya.manager_emit("plugin", { "bookmarks", sync = sync, args = table.concat(args, " ") })
end end
@@ -67,12 +76,12 @@ return {
end end
next(true, { "set", key }) next(true, { "set", key })
elseif action == "jump" then elseif action == "jump" or action == "delete" then
local bookmark = args[2] local bookmark = args[2]
if not bookmark then if not bookmark then
-- tried to use ya.sync but was unsuccessful, doing this way for the moment -- tried to use ya.sync but was unsuccessful, doing this way for the moment
if state.bookmarks then if state.bookmarks then
local arguments = { "_jump" } local arguments = { "_" .. action }
for k, _ in pairs(state.bookmarks) do for k, _ in pairs(state.bookmarks) do
table.insert(arguments, k) table.insert(arguments, k)
table.insert(arguments, state.bookmarks[k].path) table.insert(arguments, state.bookmarks[k].path)
@@ -80,9 +89,13 @@ return {
next(false, arguments) next(false, arguments)
end end
else else
jump_to_bookmark(bookmark) if action == "jump" then
jump_to_bookmark(bookmark)
elseif action == "delete" then
delete_bookmark(bookmark)
end
end end
elseif action == "_jump" then elseif action == "_jump" or action == "_delete" then
if #args == 1 then if #args == 1 then
-- Should never enter here, but just to be safe -- Should never enter here, but just to be safe
return return
@@ -102,7 +115,9 @@ return {
return return
end end
next(true, { "jump", marked_keys[selected_bookmark].on }) next(true, { string.sub(action, 2), marked_keys[selected_bookmark].on })
elseif action == "deleteall" then
delete_all_bookmarks()
end end
end, end,
} }