Skip to content

Commit 26f6bf7

Browse files
authored
Merge pull request #85 from jghauser/only-start-once
Only start once
2 parents 2012f1d + a94e18d commit 26f6bf7

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

doc/papis.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*papis.txt* For NVIM v0.8.0 Last change: 2024 July 02
1+
*papis.txt* For NVIM v0.8.0 Last change: 2024 July 03
22

33
==============================================================================
44
Table of Contents *papis-table-of-contents*

lua/papis/config.lua

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ function M:update(opts)
165165
end
166166
end
167167

168+
-- add checkhealth to filetypes (so papis gets loaded there)
169+
table.insert(newconf.init_filetypes, "checkhealth")
170+
168171
-- if debug mode is on, log level should be at least debug
169172
if newconf.enable_modules["debug"] == true then
170173
newconf.log = {

lua/papis/init.lua

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
local config = require("papis.config")
99
local api = vim.api
10+
local has_run = false
1011

1112
---Creates the `autocmd` that starts papis.nvim when configured conditions are fulfilled
1213
local function make_start_autocmd()
1314
local load_papis = api.nvim_create_augroup("loadPapis", { clear = true })
1415
api.nvim_create_autocmd("FileType", {
1516
once = true,
1617
pattern = config.init_filetypes,
17-
callback = require("papis").start,
18+
callback = function()
19+
if not has_run then
20+
require("papis").start()
21+
end
22+
end,
1823
group = load_papis,
1924
desc = "Load papis.nvim for defined filetypes",
2025
})
@@ -52,6 +57,8 @@ function M.start()
5257
log.new(config["log"] or log.get_default_config(), true)
5358
log.debug("_________________________STARTING PAPIS.NVIM_________________________")
5459

60+
has_run = true
61+
5562
-- set up db
5663
local db = require("papis.sqlite-wrapper")
5764
if not db then
@@ -72,11 +79,6 @@ function M.start()
7279
return nil
7380
end
7481

75-
-- setup commands
76-
require("papis.commands").setup()
77-
-- setup keymaps
78-
require("papis.keymaps"):setup()
79-
8082
-- setup enabled modules
8183
for module_name, _ in pairs(config.enable_modules) do
8284
log.trace(module_name .. " is enabled")
@@ -88,6 +90,12 @@ function M.start()
8890
end
8991
end
9092

93+
-- setup commands
94+
require("papis.commands").setup()
95+
-- setup keymaps
96+
require("papis.keymaps"):setup()
97+
98+
9199
-- check if other neovim instances has file watchers
92100
local does_pid_exist = require("papis.utils").does_pid_exist
93101
if not does_pid_exist(db.state:get_fw_running()) then

lua/papis/keymaps.lua

+29-8
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,50 @@
55
-- Sets up default keymaps.
66
--
77

8+
local api = vim.api
89
local config = require("papis.config")
910

1011
local M = {}
1112

1213
---@class PapisKeymaps
1314
---@type table<string, table>
14-
local keymaps = {}
15+
local keymaps_tbl = {}
16+
17+
local function create_keymaps()
18+
for _, module_keymaps in pairs(keymaps_tbl) do
19+
for _, keymap in pairs(module_keymaps) do
20+
local opts = vim.deepcopy(keymap.opts)
21+
opts.silent = true
22+
opts.buffer = true
23+
vim.keymap.set(keymap.mode, keymap.lhs, keymap.rhs, opts)
24+
end
25+
end
26+
end
27+
28+
---Creates the `autocmd` that starts papis.nvim when configured conditions are fulfilled
29+
local function make_keymap_autocmd()
30+
local create_papis_keymap = api.nvim_create_augroup("createPapisKeymap", { clear = true })
31+
api.nvim_create_autocmd("FileType", {
32+
pattern = config.init_filetypes,
33+
callback = create_keymaps,
34+
group = create_papis_keymap,
35+
desc = "Set Papis keymap",
36+
})
37+
end
1538

1639
---Sets up the keymaps for all enabled modules
1740
function M:setup()
18-
self:add_keymaps(keymaps)
41+
-- create keymaps for the buffer when papis is first started
42+
create_keymaps()
43+
-- creates keymaps for all subsequent buffers
44+
make_keymap_autocmd()
1945
end
2046

2147
--- Recursively merges the provided table with the keymaps table.
2248
---@param module_keymaps table #A table with a module's keymaps
2349
function M:add_keymaps(module_keymaps)
2450
if config.enable_keymaps then
25-
for _, keymap in pairs(module_keymaps) do
26-
local opts = vim.deepcopy(keymap.opts)
27-
opts.silent = true
28-
opts.buffer = true
29-
vim.keymap.set(keymap.mode, keymap.lhs, keymap.rhs, opts)
30-
end
51+
table.insert(keymaps_tbl, module_keymaps)
3152
end
3253
end
3354

0 commit comments

Comments
 (0)