@@ -8,50 +8,29 @@ local state = require("telescope.state")
88
99local utils = require (" telescope-orgmode.utils" )
1010
11- local orgmode = require (" orgmode" )
12- local Files = require (" orgmode.parser.files" )
13- local Capture = require (" orgmode.capture" )
14- local Range = require (" orgmode.parser.range" )
11+ local api = require (" orgmode.api" )
1512
1613return function (opts )
1714 opts = opts or {}
1815
19- -- TODO: this should be included in return from Files.get_current_file
20- local is_capture = vim .F .npcall (vim .api .nvim_buf_get_var , 0 , " org_capture" )
21-
22- local src_file = Files .get_current_file ()
23- -- In capture, refile top level heading even if cursor closer to a subheading
24- local src_item = is_capture and src_file :get_headlines ()[1 ] or src_file :get_closest_headline ()
25- local src_lines = src_file :get_headline_lines (src_item )
16+ local closest_headline = api .current ():get_closest_headline ()
2617
2718 local function refile (prompt_bufnr )
2819 local entry = action_state .get_selected_entry ()
2920 actions .close (prompt_bufnr )
3021
31- local dst_file = entry .value .file
32- local dst_headline = entry .value .headline
33- if dst_headline then
34- -- NOTE: adapted from Capture:refile_to_headline
35- local is_same_file = dst_file .filename == src_item .root .filename
36- src_lines = Capture :_adapt_headline_level (src_item , dst_headline .level , is_same_file )
37- local refile_opts = {
38- file = dst_file .filename ,
39- lines = src_lines ,
40- item = src_item ,
41- range = Range .from_line (dst_headline .position .end_line ),
42- headline = dst_headline .title ,
43- }
44- local refiled = Capture :_refile_to (refile_opts )
45- if not refiled then
46- return false
47- end
48- else
49- Capture :_refile_to_end (dst_file .filename , src_lines , src_item )
50- end
22+ -- Refile to the file by default
23+ local destination = entry .value .file
5124
52- if is_capture then
53- orgmode .action (" capture.kill" )
25+ -- Refile to a specific heading if is set
26+ if entry .value .headline then
27+ destination = entry .value .headline
5428 end
29+
30+ return api .refile ({
31+ source = closest_headline ,
32+ destination = destination ,
33+ })
5534 end
5635
5736 local function gen_depth_toggle (opts , prompt_bufnr )
0 commit comments