Skip to content

Commit 3c5bb86

Browse files
author
jghauser
committed
refactor!: don't use autocmd to format new notes
- standardise and rename format_notes and format_references options - keep track of popups to prep for handling multi select and open notes in search
1 parent 16a0e56 commit 3c5bb86

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ enable_icons = true,
392392
-- This function runs when first opening a new note. The `entry` arg is a table
393393
-- containing all the information about the entry (see above `data_tbl_schema`).
394394
-- This example is meant to be used with the `markdown` filetype.
395-
format_notes_fn = function(entry)
395+
format_notes = function(entry)
396396
-- Some string formatting templates (see above `results_format` option for
397397
-- more details)
398398
local title_format = {
@@ -413,14 +413,11 @@ enable_icons = true,
413413
"---",
414414
"",
415415
}
416-
-- Insert the lines
417-
vim.api.nvim_buf_set_lines(0, 0, #lines, false, lines)
418-
-- Move cursor to the bottom
419-
vim.cmd("normal G")
416+
return lines
420417
end,
421418
-- This function runs when inserting a formatted reference (currently by `f/c-f` in
422-
-- Telescope). It works similarly to the `format_notes_fn` above.
423-
format_references_fn = function(entry)
419+
-- Telescope). It works similarly to the `format_notes` above.
420+
format_references = function(entry)
424421
local reference_format = {
425422
{ "author", "%s ", "" },
426423
{ "year", "(%s). ", "" },
@@ -433,7 +430,8 @@ enable_icons = true,
433430
for k, v in ipairs(reference_data) do
434431
reference_data[k] = v[1]
435432
end
436-
return table.concat(reference_data)
433+
local lines = { table.concat(reference_data) }
434+
return lines
437435
end,
438436
},
439437

lua/papis/config.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ local default_config = {
8585
papis_conf_keys = { "info-name", "notes-name", "dir", "opentool" },
8686
enable_icons = true,
8787
["formatter"] = {
88-
format_notes_fn = function(entry)
88+
format_notes = function(entry)
8989
local title_format = {
9090
{ "author", "%s ", "" },
9191
{ "year", "(%s) ", "" },
@@ -101,10 +101,9 @@ local default_config = {
101101
"---",
102102
"",
103103
}
104-
vim.api.nvim_buf_set_lines(0, 0, #lines, false, lines)
105-
vim.cmd("normal G")
104+
return lines
106105
end,
107-
format_references_fn = function(entry)
106+
format_references = function(entry)
108107
local reference_format = {
109108
{ "author", "%s ", "" },
110109
{ "year", "(%s). ", "" },
@@ -117,7 +116,8 @@ local default_config = {
117116
for k, v in ipairs(reference_data) do
118117
reference_data[k] = v[1]
119118
end
120-
return table.concat(reference_data)
119+
local lines = { table.concat(reference_data) }
120+
return lines
121121
end,
122122
},
123123
["at-cursor"] = {

lua/papis/formatter/init.lua

+13-18
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,23 @@
66
--
77

88
local log = require("papis.log")
9+
local config = require("papis.config")
910

10-
local create_autocmd = vim.api.nvim_create_autocmd
11-
local create_augroup = vim.api.nvim_create_augroup
12-
13-
local augroup = create_augroup("papisFormatter", { clear = true })
14-
local autocmd = {
15-
pattern = nil,
16-
callback = nil,
17-
group = augroup,
18-
once = true,
19-
desc = "Papis: format a newly created note",
20-
}
11+
local api = vim.api
2112

2213
local M = {}
2314

24-
function M.create_autocmd(pattern, callback, entry)
25-
autocmd.pattern = pattern
26-
autocmd.callback = function()
27-
log.debug("Running formatter callback...")
28-
callback(entry)
29-
end
30-
create_autocmd("BufEnter", autocmd)
15+
function M.format_entire_file(entry)
16+
log.debug("Formatting new notes file")
17+
local lines = config["formatter"].format_notes(entry)
18+
local notes_path = entry.notes[1]
19+
local buf = api.nvim_create_buf(false, false)
20+
api.nvim_buf_set_name(buf, notes_path)
21+
api.nvim_buf_set_lines(buf, 0, #lines, false, lines)
22+
api.nvim_buf_call(buf, function()
23+
vim.cmd('write')
24+
end)
25+
api.nvim_buf_delete(buf, {})
3126
end
3227

3328
return M

lua/papis/utils.lua

+19-18
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function M:do_open_attached_files(papis_id)
110110
end
111111
end
112112

113+
local popups = {}
113114
---Opens a text file with neovim, asking to select one if there are multiple buf_options
114115
---@param papis_id string #The `papis_id` of the entry
115116
---@param type string #Either "note" or "info", specifying the type of file
@@ -137,7 +138,8 @@ function M:do_open_text_file(papis_id, type)
137138
for _, line in pairs(lines_text) do
138139
width = math.max(width, #line[1])
139140
end
140-
local popup = NuiPopup({
141+
local current_popup = #popups + 1
142+
popups[current_popup] = NuiPopup({
141143
enter = true,
142144
position = "50%",
143145
size = {
@@ -154,28 +156,25 @@ function M:do_open_text_file(papis_id, type)
154156
},
155157
})
156158

157-
popup:map("n", { "Y", "y", "<cr>" }, function(_)
158-
popup:unmount()
159+
popups[current_popup]:map("n", { "Y", "y", "<cr>" }, function(_)
160+
popups[current_popup]:unmount()
161+
popups[current_popup] = nil
159162
local config = require("papis.config")
160163
local create_new_note_fn = config.create_new_note_fn
161164
local notes_name = db.config:get_conf_value("notes_name")
162-
local enable_modules = config.enable_modules
163165
create_new_note_fn(papis_id, notes_name)
164-
if enable_modules["formatter"] then
165-
entry = db.data:get({ papis_id = papis_id })[1]
166-
local pattern = [[*]] .. notes_name:match("^.+(%..+)$")
167-
log.debug("Formatter autocmd pattern: " .. vim.inspect(pattern))
168-
local callback = config["formatter"].format_notes_fn
169-
require("papis.formatter").create_autocmd(pattern, callback, entry)
170-
end
171166
local entry_has_note = new_timer()
172167
local file_opened = false
173168
entry_has_note:start(
174169
0,
175170
5,
176171
vim.schedule_wrap(function()
177-
entry = db.data:get({ papis_id = papis_id }, { "notes" })[1]
172+
entry = db.data:get({ papis_id = papis_id })[1]
178173
if entry.notes and not file_opened then
174+
local enable_modules = config.enable_modules
175+
if enable_modules["formatter"] then
176+
require("papis.formatter").format_entire_file(entry)
177+
end
179178
log.debug("Opening newly created notes file")
180179
self:do_open_text_file(papis_id, type)
181180
file_opened = true
@@ -185,22 +184,24 @@ function M:do_open_text_file(papis_id, type)
185184
end)
186185
)
187186
end, { noremap = true, nowait = true })
188-
popup:map("n", { "N", "n", "<esc>", "q" }, function(_)
189-
popup:unmount()
187+
popups[current_popup]:map("n", { "N", "n", "<esc>", "q" }, function(_)
188+
popups[current_popup]:unmount()
189+
popups[current_popup] = nil
190190
end, { noremap = true, nowait = true })
191191

192-
popup:on({ nuiEvent.BufLeave }, function()
193-
popup:unmount()
192+
popups[current_popup]:on({ nuiEvent.BufLeave }, function()
193+
popups[current_popup]:unmount()
194+
popups[current_popup] = nil
194195
end, { once = true })
195196

196197
for k, line in pairs(lines_text) do
197198
local nuiline = NuiLine()
198199
nuiline:append(unpack(line))
199-
nuiline:render(popup.bufnr, -1, k)
200+
nuiline:render(popups[current_popup].bufnr, -1, k)
200201
end
201202

202203
vim.schedule(function()
203-
popup:mount()
204+
popups[current_popup]:mount()
204205
end)
205206
end
206207
elseif type == "info" then

lua/telescope/_extensions/papis/actions.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ M.ref_insert_formatted = function(prompt_bufnr)
5555
if vim.tbl_isempty(multi) then
5656
local papis_id = action_state.get_selected_entry().id.papis_id
5757
local full_entry = db.data:get({ papis_id = papis_id })[1]
58-
local full_reference = config["formatter"].format_references_fn(full_entry)
59-
string_to_insert = full_reference
58+
local full_reference = config["formatter"].format_references(full_entry)
59+
string_to_insert = full_reference[1]
6060
else
6161
for _, entry in pairs(multi) do
6262
local papis_id = entry.id.papis_id
6363
local full_entry = db.data:get({ papis_id = papis_id })[1]
64-
local full_reference = config["formatter"].format_references_fn(full_entry)
65-
string_to_insert = string_to_insert .. full_reference .. " "
64+
local full_reference = config["formatter"].format_references(full_entry)
65+
-- TODO: this should be able to use arbitary length `full_reference`s
66+
string_to_insert = string_to_insert .. full_reference[1] .. " "
6667
end
6768
end
6869

0 commit comments

Comments
 (0)