Skip to content

Commit f8c0ec7

Browse files
authored
Merge pull request #112 from jghauser/fix-tex-at-cursor-citep
fix(at-cursor): correctly handle citet/citep in tex
2 parents b3bafe4 + c606475 commit f8c0ec7

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,17 @@ enable_modules = {
235235

236236
-- Defines citation formats for various filetypes. They define how citation strings
237237
-- are parsed and formatted when inserted. For each filetype, we may define:
238-
-- - `start_str`: precedes the citation
239-
-- - `end_str`: appended after the citation
240-
-- - `ref_prefix`: precedes each `ref` in a citation
241-
-- - `separator_str`: gets added between `ref`s if there are multiple in a citation
238+
-- - `start_str`: Precedes the citation.
239+
-- - `start_str_alt`: Alternatives of start string. Used only for parsing.
240+
-- - `end_str`: Appended after the citation.
241+
-- - `ref_prefix`: Precedes each `ref` in a citation.
242+
-- - `separator_str`: Gets added between `ref`s if there are multiple in a citation.
242243
-- For example, for the `org` filetype if we insert a citation with `Ref1` and `Ref2`,
243244
-- we end up with `[cite:@Ref1;@Ref2]`.
244245
cite_formats = {
245246
tex = {
246247
start_str = [[\cite{]],
248+
start_str_alt = { [[\citep{]], [[\citet{]] },
247249
end_str = "}",
248250
separator_str = ", ",
249251
},

doc/papis.txt

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*papis.txt* For NVIM v0.8.0 Last change: 2025 February 27
1+
*papis.txt* For NVIM v0.8.0 Last change: 2025 March 03
22

33
==============================================================================
44
Table of Contents *papis-table-of-contents*
@@ -274,15 +274,17 @@ All configuration options (with defaults) ~
274274

275275
-- Defines citation formats for various filetypes. They define how citation strings
276276
-- are parsed and formatted when inserted. For each filetype, we may define:
277-
-- - `start_str`: precedes the citation
278-
-- - `end_str`: appended after the citation
279-
-- - `ref_prefix`: precedes each `ref` in a citation
280-
-- - `separator_str`: gets added between `ref`s if there are multiple in a citation
277+
-- - `start_str`: Precedes the citation.
278+
-- - `start_str_alt`: Alternatives of start string. Used only for parsing.
279+
-- - `end_str`: Appended after the citation.
280+
-- - `ref_prefix`: Precedes each `ref` in a citation.
281+
-- - `separator_str`: Gets added between `ref`s if there are multiple in a citation.
281282
-- For example, for the `org` filetype if we insert a citation with `Ref1` and `Ref2`,
282283
-- we end up with `[cite:@Ref1;@Ref2]`.
283284
cite_formats = {
284285
tex = {
285286
start_str = [[\cite{]],
287+
start_str_alt = { [[\citep{]], [[\citet{]] },
286288
end_str = "}",
287289
separator_str = ", ",
288290
},

lua/papis/at-cursor/init.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ local nuiEvent = require("nui.utils.autocmd").event
1212
local log = require("papis.log")
1313
local config = require("papis.config")
1414
local popup_format = config["at-cursor"].popup_format
15-
local cite_format = config:get_cite_format()
1615
local utils = require("papis.utils")
1716
local commands = require("papis.commands")
1817
local keymaps = require("papis.keymaps")
@@ -24,7 +23,9 @@ end
2423
---Tries to identify the ref under cursor
2524
---@return string|nil #Nil if nothing is found, otherwise is the identified ref
2625
local function get_ref_under_cursor()
26+
local cite_format = config:get_cite_format()
2727
local start_str = cite_format.start_str
28+
local start_str_alt = cite_format.start_str_alt or {}
2829
local ref_prefix = cite_format.ref_prefix
2930

3031
-- get current line and cursor position
@@ -40,14 +41,20 @@ local function get_ref_under_cursor()
4041
-- Extract the word
4142
local ref = current_line:sub(word_start_col, word_end_col)
4243

43-
-- if we found the cite_format prefix in the string, we need to strip it
44-
if start_str then
45-
local escaped_start_str = start_str:gsub("%W", "%%%0")
46-
local _, ref_start = string.find(ref, escaped_start_str)
44+
-- Create a combined list of start strings to check, with start_str first
45+
local all_start_strings = { start_str }
46+
vim.list_extend(all_start_strings, start_str_alt)
47+
48+
-- Check all start strings and strip if found
49+
for _, str in ipairs(all_start_strings) do
50+
local escaped_str = str:gsub("%W", "%%%0")
51+
local _, ref_start = string.find(ref, escaped_str)
4752
if ref_start then
4853
ref = string.sub(ref, ref_start + 1)
54+
break -- Exit the loop once we've found a match
4955
end
5056
end
57+
5158
-- if we found the ref_prefix in the string, we need to strip it
5259
if ref_prefix then
5360
local escaped_ref_prefix = ref_prefix:gsub("%W", "%%%0")

lua/papis/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local default_config = {
2020
cite_formats = {
2121
tex = {
2222
start_str = [[\cite{]],
23+
start_str_alt = { [[\citep{]], [[\citet{]] },
2324
end_str = "}",
2425
separator_str = ", ",
2526
},

0 commit comments

Comments
 (0)