Skip to content

Commit 73accac

Browse files
author
jghauser
committed
fix(at-cursor): get latex cite formats to work (v2)
1 parent f8c0ec7 commit 73accac

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ enable_modules = {
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:
238238
-- - `start_str`: Precedes the citation.
239-
-- - `start_str_alt`: Alternatives of start string. Used only for parsing.
239+
-- - `start_pattern`: Alternative lua pattern for more complex parsing.
240240
-- - `end_str`: Appended after the citation.
241241
-- - `ref_prefix`: Precedes each `ref` in a citation.
242242
-- - `separator_str`: Gets added between `ref`s if there are multiple in a citation.
@@ -245,7 +245,7 @@ enable_modules = {
245245
cite_formats = {
246246
tex = {
247247
start_str = [[\cite{]],
248-
start_str_alt = { [[\citep{]], [[\citet{]] },
248+
start_pattern = [[\cite[pt]?%[?[^%{]*]],
249249
end_str = "}",
250250
separator_str = ", ",
251251
},

lua/papis/at-cursor/init.lua

+11-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
local function get_ref_under_cursor()
2626
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 {}
28+
local start_pattern = cite_format.start_pattern
2929
local ref_prefix = cite_format.ref_prefix
3030

3131
-- get current line and cursor position
@@ -41,17 +41,18 @@ local function get_ref_under_cursor()
4141
-- Extract the word
4242
local ref = current_line:sub(word_start_col, word_end_col)
4343

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)
44+
-- First check if a start pattern is defined and try to use it
45+
if start_pattern then
46+
local _, pattern_end = ref:find(start_pattern)
47+
if pattern_end then
48+
ref = ref:sub(pattern_end + 1)
49+
end
50+
elseif start_str then
51+
-- If no pattern, use start_str
52+
local escaped_start_str = start_str:gsub("%W", "%%%0")
53+
local _, ref_start = string.find(ref, escaped_start_str)
5254
if ref_start then
5355
ref = string.sub(ref, ref_start + 1)
54-
break -- Exit the loop once we've found a match
5556
end
5657
end
5758

lua/papis/config.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local default_config = {
2020
cite_formats = {
2121
tex = {
2222
start_str = [[\cite{]],
23-
start_str_alt = { [[\citep{]], [[\citet{]] },
23+
start_pattern = [[\cite[pt]?%[?[^%{]*]],
2424
end_str = "}",
2525
separator_str = ", ",
2626
},

0 commit comments

Comments
 (0)