Skip to content

Commit

Permalink
Merge pull request #59 from jghauser/opentool
Browse files Browse the repository at this point in the history
use Papis opentool
  • Loading branch information
jghauser committed Jun 15, 2024
2 parents 31304b0 + cdcad1d commit fcead3d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ Minimal setup:

```lua
require("papis").setup({
-- Enable the default keymaps
-- Enable the default keymaps (defaults to `false`)
enable_keymaps = true,
-- You might want to change the filetypes activating papis.nvim
-- init_filetypes = { "markdown", "norg", "yaml" },
})
```

Expand Down Expand Up @@ -261,7 +263,7 @@ db_path = vim.fn.stdpath("data") .. "/papis_db/papis-nvim.sqlite3",
yq_bin = "yq",

-- Function to execute when adding a new note. `ref` is the citation key of the
-- relevant entry and `notes_name` is defined in `papis_python` above.
-- relevant entry and `notes_name` is the name of the notes file.
create_new_note_fn = function(papis_id, notes_name)
local testing_session = require("papis.config")["enable_modules"]["testing"]
local testing_conf_path = ""
Expand All @@ -280,6 +282,9 @@ end,
-- Filetypes that start papis.nvim.
init_filetypes = { "markdown", "norg", "yaml" },

-- Papis options to import into papis.nvim.
papis_conf_keys = { "info-name", "notes-name", "dir", "opentool" },

-- Configuration of the search module.
["search"] = {

Expand Down
11 changes: 8 additions & 3 deletions doc/papis.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*papis.txt* For NVIM v0.8.0 Last change: 2024 June 14
*papis.txt* For NVIM v0.8.0 Last change: 2024 June 15

==============================================================================
Table of Contents *papis-table-of-contents*
Expand Down Expand Up @@ -238,8 +238,10 @@ Minimal setup:

>lua
require("papis").setup({
-- Enable the default keymaps
-- Enable the default keymaps (defaults to `false`)
enable_keymaps = true,
-- You might want to change the filetypes activating papis.nvim
-- init_filetypes = { "markdown", "norg", "yaml" },
})
<

Expand Down Expand Up @@ -318,7 +320,7 @@ All configuration options (with defaults) ~
yq_bin = "yq",

-- Function to execute when adding a new note. `ref` is the citation key of the
-- relevant entry and `notes_name` is defined in `papis_python` above.
-- relevant entry and `notes_name` is the name of the notes file.
create_new_note_fn = function(papis_id, notes_name)
local testing_session = require("papis.config")["enable_modules"]["testing"]
local testing_conf_path = ""
Expand All @@ -337,6 +339,9 @@ All configuration options (with defaults) ~
-- Filetypes that start papis.nvim.
init_filetypes = { "markdown", "norg", "yaml" },

-- Papis options to import into papis.nvim.
papis_conf_keys = { "info-name", "notes-name", "dir", "opentool" },

-- Configuration of the search module.
["search"] = {

Expand Down
17 changes: 10 additions & 7 deletions lua/papis/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ local default_config = {
)
end,
init_filetypes = { "markdown", "norg", "yaml" },
papis_conf_keys = { "info-name", "notes-name", "dir", "opentool" },
["formatter"] = {
format_notes_fn = function(entry)
local title_format = {
Expand Down Expand Up @@ -145,13 +146,13 @@ local default_config = {
}

---Queries Papis to get various options.
---@param testing_session boolean #If true, will use testing papis conf
---@param papis_conf_keys table #A table with keys to query from Papis
---@param is_testing_session boolean #If true, will use testing papis conf
---@return table #A table { info_name = val, dir = val }
local function get_papis_py_conf(testing_session)
local papis_conf_keys = { "info-name", "notes-name", "dir" }
local function get_papis_py_conf(papis_conf_keys, is_testing_session)
local papis_py_conf_new = {}
local testing_conf_path = ""
if testing_session then
if is_testing_session then
testing_conf_path = "-c ./tests/papis_config "
end
for _, key in ipairs(papis_conf_keys) do
Expand Down Expand Up @@ -182,7 +183,8 @@ function M:update_papis_py_conf()
end

local is_testing_session = self["enable_modules"]["testing"]
local papis_py_conf_new = get_papis_py_conf(is_testing_session)
local papis_conf_keys = self["papis_conf_keys"]
local papis_py_conf_new = get_papis_py_conf(papis_conf_keys, is_testing_session)
local papis_py_conf_old = db.config:get()[1]
papis_py_conf_old["id"] = nil

Expand All @@ -207,8 +209,9 @@ function M:setup_papis_py_conf()
-- get config from Papis if not already in db
if not db.config:is_setup() then
log.info("Papis.nvim configuration not setup, importing values from Papis now")
local testing_session = self["enable_modules"]["testing"]
local papis_py_conf_new = get_papis_py_conf(testing_session)
local is_testing_session = self["enable_modules"]["testing"]
local papis_conf_keys = self["papis_conf_keys"]
local papis_py_conf_new = get_papis_py_conf(papis_conf_keys, is_testing_session)
db.config:drop()
db.config:update({ id = 1 }, papis_py_conf_new)
end
Expand Down
19 changes: 13 additions & 6 deletions lua/papis/sqlite-wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ M.state = M:tbl("state", {
tag_format = { "text", default = nil },
})

M.config = M:tbl("config", {
id = true,
info_name = { "text", default = nil },
notes_name = { "text", default = nil },
dir = { "text", default = nil },
})

---Creates the schema of the config table
---@return table #The config table schema
local function get_config_tbl_schema()
local tbl_schema = { id = true, }
for _, key in ipairs(config["papis_conf_keys"]) do
local sanitized_key = string.gsub(key, "-", "_")
tbl_schema[sanitized_key] = { "text", default = nil }
end
return tbl_schema
end

M.config = M:tbl("config", get_config_tbl_schema())

---Adds common methods to tbls
---@param tbls table #Set of tables that should have methods added
Expand Down
27 changes: 11 additions & 16 deletions lua/papis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end

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

-- Open file outside neovim
---@param path string #Path to the file
function M.do_open_file_external(path)
local command
local args
if is_windows then
command = "rundll32.exe"
args = { "url.dll,FileProtocolHandler", path }
else
if is_linux then
command = "xdg-open"
elseif is_macos then
command = "open"
end
args = { path }
function M:do_open_file_external(path)
local opentool = require("papis.sqlite-wrapper").config:get_value({ id = 1 }, "opentool")
local opentool_table = self.do_split_str(opentool, " ")
local command = opentool_table[1]
local args = {}
for i = 2, #opentool_table do
table.insert(args, opentool_table[i])
end
table.insert(args, path)

local handle
handle = vim.loop.spawn(command, {
Expand Down Expand Up @@ -119,15 +114,15 @@ function M:do_open_attached_files(papis_id)
elseif #filenames == 1 then
log.info("Opening file '" .. filenames[1] .. "' ")
local path = lookup_tbl[filenames[1]]
self.do_open_file_external(path)
self:do_open_file_external(path)
else
vim.ui.select(filenames, {
prompt = "Select attachment to open:",
}, function(choice)
if choice then
log.info("Opening file '" .. choice .. "' ")
local path = lookup_tbl[choice]
self.do_open_file_external(path)
self:do_open_file_external(path)
end
end)
end
Expand Down

0 comments on commit fcead3d

Please sign in to comment.