Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ require'marks'.setup {

See `:help marks-setup` for all of the keys that can be passed to the setup function.

## Telescope

There is a [telescope](https://github.com/nvim-telescope/telescope.nvim) extension allowing to list marks through telescope.

To activate it you need to load the extension:
```lua
local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
return
end

telescope.load_extension("marks_nvim")
```

You can then use the extension methods to list marks instead of using the native loclist system.
You simply need to call these methods in your mappings.

```lua
require('telescope').extensions.marks_nvim.marks_list_buf(opts) --[[ List buffer marks ]]
require('telescope').extensions.marks_nvim.marks_list_all(opts) --[[ List all marks ]]
require('telescope').extensions.marks_nvim.bookmarks_list_group(1, opts) --[[ List a bookmark group marks (takes the group number as argument) ]]
require('telescope').extensions.marks_nvim.bookmarks_list_all(opts) --[[ List all bookmarks marks ]]
```

These methods will use your `path_display` telescope configuraiton to display paths.
You can also pass a specific property for one method in the `opts` table. Eg.
```lua
require('telescope').extensions.marks_nvim.marks_list_all({ path_display = 'shorten' })
```

## Mappings

The following default mappings are included:
Expand Down
105 changes: 82 additions & 23 deletions lua/marks/bookmark.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require'marks.utils'
local utils = require("marks.utils")
local a = vim.api

local Bookmarks = {}
Expand Down Expand Up @@ -65,7 +65,7 @@ function Bookmarks:place_mark(group_nr, bufnr)
return
end

local data = { buf = bufnr, line = pos[1], col = pos[2], sign_id = -1}
local data = { buf = bufnr, line = pos[1], col = pos[2], sign_id = -1 }

local display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs)
if display_signs and group.sign then
Expand All @@ -76,11 +76,11 @@ function Bookmarks:place_mark(group_nr, bufnr)

local opts = {}
if group.virt_text then
opts.virt_text = {{ group.virt_text, "MarkVirtTextHL" }}
opts.virt_text = { { group.virt_text, "MarkVirtTextHL" } }
opts.virt_text_pos = "eol"
end

local extmark_id = a.nvim_buf_set_extmark(bufnr, group.ns, pos[1]-1, pos[2], opts)
local extmark_id = a.nvim_buf_set_extmark(bufnr, group.ns, pos[1] - 1, pos[2], opts)

data.extmark_id = extmark_id

Expand Down Expand Up @@ -192,8 +192,13 @@ function Bookmarks:next(group_nr)
return false
end

local next = utils.search(marks, {buf = bufnr, line=pos[1]},
{buf=math.huge, line=math.huge}, comparator, false)
local next = utils.search(
marks,
{ buf = bufnr, line = pos[1] },
{ buf = math.huge, line = math.huge },
comparator,
false
)

if not next then
next = marks[1]
Expand Down Expand Up @@ -232,8 +237,7 @@ function Bookmarks:prev(group_nr)
return false
end

local prev = utils.search(marks, {buf = bufnr, line=pos[1]},
{buf=-1, line=-1}, comparator, false)
local prev = utils.search(marks, { buf = bufnr, line = pos[1] }, { buf = -1, line = -1 }, comparator, false)

if not prev then
prev = marks[#marks]
Expand Down Expand Up @@ -268,20 +272,20 @@ function Bookmarks:annotate(group_nr)
local text = vim.fn.input("annotation: ")

if text ~= "" then
a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line-1, bookmark.col, {
id = bookmark.extmark_id, virt_lines = {{{text, "MarkVirtTextHL"}}},
virt_lines_above=true,
a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, {
id = bookmark.extmark_id,
virt_lines = { { { text, "MarkVirtTextHL" } } },
virt_lines_above = true,
})
else
a.nvim_buf_del_extmark(bufnr, self.groups[group_nr].ns, bookmark.extmark_id)

local opts = {}
if self.groups[group_nr].virt_text then
opts.virt_text = {{ self.groups[group_nr].virt_text, "MarkVirtTextHL" }}
opts.virt_text = { { self.groups[group_nr].virt_text, "MarkVirtTextHL" } }
opts.virt_text_pos = "eol"
end
bookmark.extmark_id = a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line-1,
bookmark.col, opts)
bookmark.extmark_id = a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, opts)
end
end

Expand All @@ -299,8 +303,7 @@ function Bookmarks:refresh()
buf_marks = group.marks[bufnr]
if buf_marks then
for _, mark in pairs(vim.tbl_values(buf_marks)) do
local line = a.nvim_buf_get_extmark_by_id(bufnr, group.ns,
mark.extmark_id, {})[1]
local line = a.nvim_buf_get_extmark_by_id(bufnr, group.ns, mark.extmark_id, {})[1]

if line + 1 ~= mark.line then
buf_marks[line + 1] = mark
Expand All @@ -316,6 +319,53 @@ function Bookmarks:refresh()
end
end

function Bookmarks:get_group_list(group_nr)
local items = {}
if not group_nr or not self.groups[group_nr] then
return items
end

for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do
for line, mark in pairs(buffer_marks) do
local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1]
local filename = vim.api.nvim_buf_get_name(bufnr)
table.insert(items, {
bufnr = bufnr,
lnum = line,
col = mark.col + 1,
group = group_nr,
line = vim.trim(text),
path = filename,
})
end
end

return items
end

function Bookmarks:get_all_list()
local items = {}
for group_nr, group in pairs(self.groups) do
for bufnr, buffer_marks in pairs(group.marks) do
for line, mark in pairs(buffer_marks) do
local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1]
local filename = vim.api.nvim_buf_get_name(bufnr)

table.insert(items, {
bufnr = bufnr,
lnum = line,
col = mark.col + 1,
group = group_nr,
line = vim.trim(text),
path = filename,
})
end
end
end

return items
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real changes are only in this hunk, rest is linting.

function Bookmarks:to_list(list_type, group_nr)
if not group_nr or not self.groups[group_nr] then
return
Expand All @@ -327,8 +377,8 @@ function Bookmarks:to_list(list_type, group_nr)
local items = {}
for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do
for line, mark in pairs(buffer_marks) do
local text = a.nvim_buf_get_lines(bufnr, line-1, line, true)[1]
table.insert(items, { bufnr=bufnr, lnum=line, col=mark.col + 1, text=text })
local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1]
table.insert(items, { bufnr = bufnr, lnum = line, col = mark.col + 1, text = text })
end
end

Expand All @@ -343,9 +393,13 @@ function Bookmarks:all_to_list(list_type)
for group_nr, group in pairs(self.groups) do
for bufnr, buffer_marks in pairs(group.marks) do
for line, mark in pairs(buffer_marks) do
local text = a.nvim_buf_get_lines(bufnr, line-1, line, true)[1]
table.insert(items, { bufnr=bufnr, lnum=line, col=mark.col + 1,
text="bookmark group "..group_nr..": "..text })
local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1]
table.insert(items, {
bufnr = bufnr,
lnum = line,
col = mark.col + 1,
text = "bookmark group " .. group_nr .. ": " .. text,
})
end
end
end
Expand All @@ -358,8 +412,13 @@ function Bookmarks:add_sign(bufnr, text, line, id)
end

function Bookmarks.new()
return setmetatable({signs = {"!", "@", "#", "$", "%", "^", "&", "*", "(", [0]=")"},
virt_text = {}, groups = {}, prompt_annotate = {}, opt = {}}, {__index = Bookmarks})
return setmetatable({
signs = { "!", "@", "#", "$", "%", "^", "&", "*", "(", [0] = ")" },
virt_text = {},
groups = {},
prompt_annotate = {},
opt = {},
}, { __index = Bookmarks })
end

return Bookmarks
41 changes: 41 additions & 0 deletions lua/marks/mark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,47 @@ function Mark.preview_mark()
vim.cmd("normal! zz")
end

function Mark:get_buf_list(bufnr)
bufnr = bufnr or a.nvim_get_current_buf()
if not self.buffers[bufnr] then
return
end

local items = {}
for mark, data in pairs(self.buffers[bufnr].placed_marks) do
local text = a.nvim_buf_get_lines(bufnr, data.line-1, data.line, true)[1]
local path = vim.api.nvim_buf_get_name(bufnr)
table.insert(items, {
bufnr = bufnr,
lnum = data.line,
col = data.col + 1,
mark = mark,
line = vim.trim(text),
path = path
})
end
return items
end

function Mark:get_all_list()
local items = {}
for bufnr, buffer_state in pairs(self.buffers) do
for mark, data in pairs(buffer_state.placed_marks) do
local text = a.nvim_buf_get_lines(bufnr, data.line-1, data.line, true)[1]
local path = vim.api.nvim_buf_get_name(bufnr)
table.insert(items, {
bufnr = bufnr,
lnum = data.line,
col = data.col + 1,
mark = mark,
line = vim.trim(text),
path = path
})
end
end
return items
end

function Mark:buffer_to_list(list_type, bufnr)
list_type = list_type or "loclist"

Expand Down
Loading