File tree Expand file tree Collapse file tree 3 files changed +43
-11
lines changed Expand file tree Collapse file tree 3 files changed +43
-11
lines changed Original file line number Diff line number Diff line change @@ -65,22 +65,14 @@ function M.refile(closest_headline)
65
65
end
66
66
end
67
67
68
- function M .insert (_ )
68
+ function M .insert (opts )
69
69
return function (prompt_bufnr )
70
70
actions .close (prompt_bufnr )
71
71
72
72
--- @type MatchEntry
73
73
local entry = action_state .get_selected_entry ()
74
74
75
- local api_object = entry .value .headline
76
- and org .get_api_headline (entry .filename , entry .value .headline .line_number )
77
- or org .get_api_file (entry .filename )
78
-
79
- if not api_object then
80
- error (' Could not find ' .. (entry .value .headline and ' headline' or ' file' ) .. ' for link' )
81
- end
82
-
83
- local destination = api_object :get_link ()
75
+ local destination = org .get_link_destination (entry , opts )
84
76
85
77
org .insert_link (destination )
86
78
return true
Original file line number Diff line number Diff line change @@ -118,4 +118,44 @@ function M.get_api_file(filename)
118
118
return OrgApi .load (filename )
119
119
end
120
120
121
+ --- Get intra-file link for headline (simple *Headline format)
122
+ --- @param entry table
123
+ --- @return string
124
+ function M .get_intra_file_link (entry )
125
+ return ' *' .. entry .value .headline .title
126
+ end
127
+
128
+ --- Get inter-file link using full API format
129
+ --- @param entry table
130
+ --- @return string
131
+ function M .get_inter_file_link (entry )
132
+ if entry .value .headline then
133
+ local api_headline = M .get_api_headline (entry .filename , entry .value .headline .line_number )
134
+ if api_headline then
135
+ return api_headline :get_link ()
136
+ end
137
+ error (' Could not find headline for link' )
138
+ else
139
+ local api_file = M .get_api_file (entry .filename )
140
+ if api_file then
141
+ return api_file :get_link ()
142
+ end
143
+ error (' Could not find file for link' )
144
+ end
145
+ end
146
+
147
+ --- Get link destination (chooses intra-file vs inter-file format)
148
+ --- @param entry table
149
+ --- @param opts table
150
+ --- @return string
151
+ function M .get_link_destination (entry , opts )
152
+ -- Use intra-file format for headlines in the same file
153
+ if entry .value .headline and opts .original_file and entry .filename == opts .original_file then
154
+ return M .get_intra_file_link (entry )
155
+ end
156
+
157
+ -- Use inter-file format for everything else
158
+ return M .get_inter_file_link (entry )
159
+ end
160
+
121
161
return M
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ return function(opts)
20
20
sorter = conf .generic_sorter (opts ),
21
21
previewer = conf .grep_previewer (opts ),
22
22
attach_mappings = function (_ , map )
23
- action_set .select :replace (actions .insert ())
23
+ action_set .select :replace (actions .insert (opts ))
24
24
mappings .attach_mappings (map , opts )
25
25
return true
26
26
end ,
You can’t perform that action at this time.
0 commit comments