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)
6565 end
6666end
6767
68- function M .insert (_ )
68+ function M .insert (opts )
6969 return function (prompt_bufnr )
7070 actions .close (prompt_bufnr )
7171
7272 --- @type MatchEntry
7373 local entry = action_state .get_selected_entry ()
7474
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 )
8476
8577 org .insert_link (destination )
8678 return true
Original file line number Diff line number Diff line change @@ -118,4 +118,44 @@ function M.get_api_file(filename)
118118 return OrgApi .load (filename )
119119end
120120
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+
121161return M
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ return function(opts)
2020 sorter = conf .generic_sorter (opts ),
2121 previewer = conf .grep_previewer (opts ),
2222 attach_mappings = function (_ , map )
23- action_set .select :replace (actions .insert ())
23+ action_set .select :replace (actions .insert (opts ))
2424 mappings .attach_mappings (map , opts )
2525 return true
2626 end ,
You can’t perform that action at this time.
0 commit comments