@@ -9,11 +9,10 @@ local NuiPopup = require("nui.popup")
9
9
local nuiAutocmd = require (" nui.utils.autocmd" )
10
10
local nuiEvent = require (" nui.utils.autocmd" ).event
11
11
12
- local fn = vim .fn
13
-
14
12
local log = require (" papis.log" )
15
13
local config = require (" papis.config" )
16
14
local popup_format = config [" at-cursor" ].popup_format
15
+ local cite_format = config :get_cite_format ()
17
16
local utils = require (" papis.utils" )
18
17
local commands = require (" papis.commands" )
19
18
local keymaps = require (" papis.keymaps" )
25
24
--- Tries to identify the ref under cursor
26
25
--- @return string | nil #Nil if nothing is found, otherwise is the identified ref
27
26
local function get_ref_under_cursor ()
28
- -- get the word under the cursor
29
- local ref = fn .expand (" <cWORD>" )
30
- local filetype = vim .bo .filetype
31
- log .debug (" The filetype is: " .. filetype )
32
- local cite_format = utils .get_cite_format (filetype )
33
- if type (cite_format ) == " table" then
34
- cite_format = cite_format [2 ]
35
- end
36
- log .debug (" The cite_format is: " .. cite_format )
37
- local _ , prefix_end = string.find (cite_format , " %%s" )
38
- prefix_end = prefix_end - 2
39
- local cite_format_prefix = string.sub (cite_format , 1 , prefix_end )
40
- local _ , ref_start = string.find (ref , cite_format_prefix )
27
+ local start_str = cite_format .start_str
28
+ local ref_prefix = cite_format .ref_prefix
29
+
30
+ -- get current line and cursor position
31
+ local current_line = vim .api .nvim_get_current_line ()
32
+ local _ , cursor_col = unpack (vim .api .nvim_win_get_cursor (0 ))
33
+
34
+ -- Find the start and end of the word under the cursor
35
+ local line_until_cursor = current_line :sub (1 , cursor_col )
36
+ local word_start_col = line_until_cursor :find (" [^%s,;]*$" ) or 1
37
+ local line_after_cursor = current_line :sub (cursor_col )
38
+ local word_end_col = cursor_col + (line_after_cursor :find (" [%s,;]" ) or # line_after_cursor ) - 1
39
+
40
+ -- Extract the word
41
+ local ref = current_line :sub (word_start_col , word_end_col )
42
+
41
43
-- if we found the cite_format prefix in the string, we need to strip it
42
- if ref_start then
43
- ref_start = ref_start + 1
44
- ref = string.sub (ref , ref_start )
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 )
47
+ if ref_start then
48
+ ref = string.sub (ref , ref_start + 1 )
49
+ end
45
50
end
51
+ -- if we found the ref_prefix in the string, we need to strip it
52
+ if ref_prefix then
53
+ local escaped_ref_prefix = ref_prefix :gsub (" %W" , " %%%0" )
54
+ local _ , ref_start = string.find (ref , escaped_ref_prefix )
55
+ if ref_start then
56
+ ref_start = ref_start + 1
57
+ ref = string.sub (ref , ref_start )
58
+ end
59
+ end
60
+
46
61
-- remove all punctuation characters at the beginning and end of string
47
62
ref = ref :gsub (" ^[%p]*(.-)[%p]*$" , " %1" )
48
63
0 commit comments