@@ -6,94 +6,95 @@ local actions = require("telescope.actions")
6
6
local action_state = require (" telescope.actions.state" )
7
7
local state = require (" telescope.state" )
8
8
9
- local utils = require (' telescope-orgmode.utils' )
9
+ local utils = require (" telescope-orgmode.utils" )
10
10
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 orgmode = require (" orgmode" )
12
+ local Files = require (" orgmode.parser.files" )
13
+ local Capture = require (" orgmode.capture" )
14
+ local Range = require (" orgmode.parser.range" )
15
15
16
16
return function (opts )
17
- opts = opts or {}
17
+ opts = opts or {}
18
18
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' )
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
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 )
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 )
26
26
27
- local function refile (prompt_bufnr )
28
- local entry = action_state .get_selected_entry ()
29
- actions .close (prompt_bufnr )
27
+ local function refile (prompt_bufnr )
28
+ local entry = action_state .get_selected_entry ()
29
+ actions .close (prompt_bufnr )
30
30
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
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
51
51
52
- if is_capture then
53
- orgmode .action (' capture.kill' )
54
- end
55
- end
52
+ if is_capture then
53
+ orgmode .action (" capture.kill" )
54
+ end
55
+ end
56
56
57
- local function gen_depth_toggle (opts , prompt_bufnr )
57
+ local function gen_depth_toggle (opts , prompt_bufnr )
58
+ local status = state .get_status (prompt_bufnr )
59
+ status ._ot_current_depth = opts .max_depth
60
+ status ._ot_next_depth = nil
61
+ if status ._ot_current_depth ~= 0 then
62
+ status ._ot_next_depth = 0
63
+ end
58
64
59
- local status = state .get_status (prompt_bufnr )
60
- status ._ot_current_depth = opts .max_depth
61
- status ._ot_next_depth = nil
62
- if status ._ot_current_depth ~= 0 then
63
- status ._ot_next_depth = 0
64
- end
65
+ return function ()
66
+ local current_picker = action_state .get_current_picker (prompt_bufnr )
65
67
66
- return function ()
67
- local current_picker = action_state .get_current_picker (prompt_bufnr )
68
+ local aux = status ._ot_current_depth
69
+ status ._ot_current_depth = status ._ot_next_depth
70
+ status ._ot_next_depth = aux
68
71
69
- local aux = status ._ot_current_depth
70
- status ._ot_current_depth = status ._ot_next_depth
71
- status ._ot_next_depth = aux
72
+ opts .max_depth = status ._ot_current_depth
73
+ local new_finder = finders .new_table ({
74
+ results = utils .get_entries (opts ),
75
+ entry_maker = opts .entry_maker or utils .make_entry (opts ),
76
+ })
72
77
73
- opts .max_depth = status ._ot_current_depth
74
- local new_finder = finders .new_table {
75
- results = utils .get_entries (opts ),
76
- entry_maker = opts .entry_maker or utils .make_entry (opts ),
77
- }
78
+ current_picker :refresh (new_finder , opts )
79
+ end
80
+ end
78
81
79
- current_picker :refresh (new_finder , opts )
80
- end
81
- end
82
-
83
- pickers .new (opts , {
84
- -- TODO: alter prompt title when depth is 0: Refile under file, Refile
85
- -- under Headline
86
- prompt_title = " Refile Destination" ,
87
- finder = finders .new_table {
88
- results = utils .get_entries (opts ),
89
- entry_maker = opts .entry_maker or utils .make_entry (opts ),
90
- },
91
- sorter = conf .generic_sorter (opts ),
92
- previewer = conf .grep_previewer (opts ),
93
- attach_mappings = function (prompt_bufnr , map )
94
- action_set .select :replace (refile )
95
- map (" i" , " <c-space>" , gen_depth_toggle (opts , prompt_bufnr ))
96
- return true
97
- end ,
98
- }):find ()
82
+ pickers
83
+ .new (opts , {
84
+ -- TODO: alter prompt title when depth is 0: Refile under file, Refile
85
+ -- under Headline
86
+ prompt_title = " Refile Destination" ,
87
+ finder = finders .new_table ({
88
+ results = utils .get_entries (opts ),
89
+ entry_maker = opts .entry_maker or utils .make_entry (opts ),
90
+ }),
91
+ sorter = conf .generic_sorter (opts ),
92
+ previewer = conf .grep_previewer (opts ),
93
+ attach_mappings = function (prompt_bufnr , map )
94
+ action_set .select :replace (refile )
95
+ map (" i" , " <c-space>" , gen_depth_toggle (opts , prompt_bufnr ))
96
+ return true
97
+ end ,
98
+ })
99
+ :find ()
99
100
end
0 commit comments