1
1
local utils = require (' orgmode.utils' )
2
+ local fs = require (' orgmode.utils.fs' )
2
3
local config = require (' orgmode.config' )
3
4
local Templates = require (' orgmode.capture.templates' )
4
5
local Template = require (' orgmode.capture.template' )
@@ -363,12 +364,13 @@ function Capture:get_destination()
363
364
local valid_destinations = self :_get_autocompletion_files ()
364
365
365
366
local destination = vim .fn .OrgmodeInput (' Enter destination: ' , ' ' , function (arg_lead )
366
- return self :autocomplete_refile (arg_lead )
367
+ return self :autocomplete_refile (arg_lead , valid_destinations )
367
368
end )
368
369
369
- destination = vim .split (destination , ' /' , { plain = true })
370
+ local path = destination :match (' ^.*%.org/' )
371
+ local headline_title = path and destination :sub (# path + 1 ) or ' '
370
372
371
- if not valid_destinations [destination [ 1 ] ] then
373
+ if not valid_destinations [path ] then
372
374
utils .echo_error (
373
375
(' "%s" is not a is not a file specified in the "org_agenda_files" setting. Refiling cancelled.' ):format (
374
376
destination [1 ]
@@ -377,11 +379,10 @@ function Capture:get_destination()
377
379
return {}
378
380
end
379
381
380
- local destination_file = valid_destinations [destination [ 1 ] ]
382
+ local destination_file = valid_destinations [path ]
381
383
local result = {
382
384
file = destination_file ,
383
385
}
384
- local headline_title = table.concat ({ unpack (destination , 2 ) }, ' /' )
385
386
386
387
if not headline_title or vim .trim (headline_title ) == ' ' then
387
388
return result
@@ -406,24 +407,24 @@ function Capture:get_destination()
406
407
end
407
408
408
409
--- @param arg_lead string
410
+ --- @param files table<string , OrgFile>
409
411
--- @return string[]
410
- function Capture :autocomplete_refile (arg_lead )
411
- local valid_files = self :_get_autocompletion_files (true )
412
-
412
+ function Capture :autocomplete_refile (arg_lead , files )
413
413
if not arg_lead or # arg_lead == 0 then
414
- return vim .tbl_keys (valid_files )
414
+ return vim .tbl_keys (files )
415
415
end
416
416
417
- local filename = vim .split (arg_lead , ' /' , { plain = true })[1 ]
418
- local selected_file = valid_files [filename .. ' /' ]
417
+ local filename = arg_lead :match (' ^.*%.org/' )
418
+
419
+ local selected_file = filename and files [filename ]
419
420
420
421
if not selected_file then
421
- return vim .fn .matchfuzzy (vim .tbl_keys (valid_files ), filename )
422
+ return vim .fn .matchfuzzy (vim .tbl_keys (files ), filename or arg_lead )
422
423
end
423
424
424
425
local headlines = selected_file :get_opened_unfinished_headlines ()
425
426
local result = vim .tbl_map (function (headline )
426
- return string.format (' %s/ %s' , vim . fn . fnamemodify ( headline . file . filename , ' :t ' ) , headline :get_title ())
427
+ return string.format (' %s%s' , filename , headline :get_title ())
427
428
end , headlines )
428
429
429
430
return vim .tbl_filter (function (item )
@@ -597,16 +598,25 @@ function Capture:_create_menu_items(templates)
597
598
end
598
599
599
600
--- @private
600
- --- @param add_slash_suffix ? boolean
601
601
--- @return table<string , OrgFile>
602
- function Capture :_get_autocompletion_files (add_slash_suffix )
602
+ function Capture :_get_autocompletion_files ()
603
603
local valid_destinations = {}
604
+ local filenames = {}
604
605
for _ , file in ipairs (self .files :all ()) do
605
606
if not file :is_archive_file () then
606
- valid_destinations [vim .fn .fnamemodify (file .filename , ' :t' ) .. (add_slash_suffix and ' /' or ' ' )] = file
607
+ table.insert (valid_destinations , file )
608
+ table.insert (filenames , file .filename )
607
609
end
608
610
end
609
- return valid_destinations
611
+
612
+ filenames = fs .trim_common_root (filenames )
613
+ local result = {}
614
+
615
+ for i , filename in ipairs (filenames ) do
616
+ result [filename .. ' /' ] = valid_destinations [i ]
617
+ end
618
+
619
+ return result
610
620
end
611
621
612
622
--- @private
0 commit comments