Skip to content

Commit 4a0575f

Browse files
author
jghauser
committed
feat: use option from Papis to open external files
1 parent c4c59ec commit 4a0575f

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ end,
282282
-- Filetypes that start papis.nvim.
283283
init_filetypes = { "markdown", "norg", "yaml" },
284284

285+
-- Papis options to import into papis.nvim.
286+
papis_conf_keys = { "info-name", "notes-name", "dir", "opentool" },
287+
285288
-- Configuration of the search module.
286289
["search"] = {
287290

lua/papis/config.lua

+10-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ local default_config = {
6666
)
6767
end,
6868
init_filetypes = { "markdown", "norg", "yaml" },
69+
papis_conf_keys = { "info-name", "notes-name", "dir", "opentool" },
6970
["formatter"] = {
7071
format_notes_fn = function(entry)
7172
local title_format = {
@@ -145,13 +146,13 @@ local default_config = {
145146
}
146147

147148
---Queries Papis to get various options.
148-
---@param testing_session boolean #If true, will use testing papis conf
149+
---@param papis_conf_keys table #A table with keys to query from Papis
150+
---@param is_testing_session boolean #If true, will use testing papis conf
149151
---@return table #A table { info_name = val, dir = val }
150-
local function get_papis_py_conf(testing_session)
151-
local papis_conf_keys = { "info-name", "notes-name", "dir" }
152+
local function get_papis_py_conf(papis_conf_keys, is_testing_session)
152153
local papis_py_conf_new = {}
153154
local testing_conf_path = ""
154-
if testing_session then
155+
if is_testing_session then
155156
testing_conf_path = "-c ./tests/papis_config "
156157
end
157158
for _, key in ipairs(papis_conf_keys) do
@@ -182,7 +183,8 @@ function M:update_papis_py_conf()
182183
end
183184

184185
local is_testing_session = self["enable_modules"]["testing"]
185-
local papis_py_conf_new = get_papis_py_conf(is_testing_session)
186+
local papis_conf_keys = self["papis_conf_keys"]
187+
local papis_py_conf_new = get_papis_py_conf(papis_conf_keys, is_testing_session)
186188
local papis_py_conf_old = db.config:get()[1]
187189
papis_py_conf_old["id"] = nil
188190

@@ -207,8 +209,9 @@ function M:setup_papis_py_conf()
207209
-- get config from Papis if not already in db
208210
if not db.config:is_setup() then
209211
log.info("Papis.nvim configuration not setup, importing values from Papis now")
210-
local testing_session = self["enable_modules"]["testing"]
211-
local papis_py_conf_new = get_papis_py_conf(testing_session)
212+
local is_testing_session = self["enable_modules"]["testing"]
213+
local papis_conf_keys = self["papis_conf_keys"]
214+
local papis_py_conf_new = get_papis_py_conf(papis_conf_keys, is_testing_session)
212215
db.config:drop()
213216
db.config:update({ id = 1 }, papis_py_conf_new)
214217
end

lua/papis/sqlite-wrapper.lua

+13-6
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,19 @@ M.state = M:tbl("state", {
9797
tag_format = { "text", default = nil },
9898
})
9999

100-
M.config = M:tbl("config", {
101-
id = true,
102-
info_name = { "text", default = nil },
103-
notes_name = { "text", default = nil },
104-
dir = { "text", default = nil },
105-
})
100+
101+
---Creates the schema of the config table
102+
---@return table #The config table schema
103+
local function get_config_tbl_schema()
104+
local tbl_schema = { id = true, }
105+
for _, key in ipairs(config["papis_conf_keys"]) do
106+
local sanitized_key = string.gsub(key, "-", "_")
107+
tbl_schema[sanitized_key] = { "text", default = nil }
108+
end
109+
return tbl_schema
110+
end
111+
112+
M.config = M:tbl("config", get_config_tbl_schema())
106113

107114
---Adds common methods to tbls
108115
---@param tbls table #Set of tables that should have methods added

lua/papis/utils.lua

+11-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747

4848
---Splits string by `inputstr` and trims whitespace
4949
---@param inputstr string #String to be split
50-
---@param sep? string #String giving each character by witch to split
50+
---@param sep? string #String giving each character by which to split
5151
---@return table #List of split elements
5252
function M.do_split_str(inputstr, sep)
5353
if sep == nil then
@@ -63,20 +63,15 @@ end
6363

6464
-- Open file outside neovim
6565
---@param path string #Path to the file
66-
function M.do_open_file_external(path)
67-
local command
68-
local args
69-
if is_windows then
70-
command = "rundll32.exe"
71-
args = { "url.dll,FileProtocolHandler", path }
72-
else
73-
if is_linux then
74-
command = "xdg-open"
75-
elseif is_macos then
76-
command = "open"
77-
end
78-
args = { path }
66+
function M:do_open_file_external(path)
67+
local opentool = require("papis.sqlite-wrapper").config:get_value({ id = 1 }, "opentool")
68+
local opentool_table = self.do_split_str(opentool, " ")
69+
local command = opentool_table[1]
70+
local args = {}
71+
for i = 2, #opentool_table do
72+
table.insert(args, opentool_table[i])
7973
end
74+
table.insert(args, path)
8075

8176
local handle
8277
handle = vim.loop.spawn(command, {
@@ -119,15 +114,15 @@ function M:do_open_attached_files(papis_id)
119114
elseif #filenames == 1 then
120115
log.info("Opening file '" .. filenames[1] .. "' ")
121116
local path = lookup_tbl[filenames[1]]
122-
self.do_open_file_external(path)
117+
self:do_open_file_external(path)
123118
else
124119
vim.ui.select(filenames, {
125120
prompt = "Select attachment to open:",
126121
}, function(choice)
127122
if choice then
128123
log.info("Opening file '" .. choice .. "' ")
129124
local path = lookup_tbl[choice]
130-
self.do_open_file_external(path)
125+
self:do_open_file_external(path)
131126
end
132127
end)
133128
end

0 commit comments

Comments
 (0)