@@ -6,13 +6,15 @@ local utils = require('orgmode.utils')
66
77--- @class OrgAttachCore
88--- @field files OrgFiles
9+ --- @field links OrgLinks
910local AttachCore = {}
1011AttachCore .__index = AttachCore
1112
12- --- @param opts { files : OrgFiles }
13+ --- @param opts { files : OrgFiles , links : OrgLinks }
1314function AttachCore .new (opts )
1415 local data = {
1516 files = opts and opts .files ,
17+ links = opts and opts .links ,
1618 }
1719 return setmetatable (data , AttachCore )
1820end
@@ -427,6 +429,8 @@ function AttachCore:attach(node, file, opts)
427429 return nil
428430 end
429431 node :add_auto_tag ()
432+ local link = self .links :store_link_to_attachment ({ attach_dir = attach_dir , original = file })
433+ vim .fn .setreg (vim .v .register , link )
430434 return basename
431435 end )
432436 end )
@@ -453,6 +457,14 @@ function AttachCore:attach_buffer(node, bufnr, opts)
453457 local data = table.concat (vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ), ' \n ' )
454458 return utils .writefile (attach_file , data , { excl = true }):next (function ()
455459 node :add_auto_tag ()
460+ -- Ignore all errors here, this is just to determine whether we can store
461+ -- a link to `bufname`.
462+ local bufname_exists = vim .uv .fs_stat (bufname )
463+ local link = self .links :store_link_to_attachment ({
464+ attach_dir = attach_dir ,
465+ original = bufname_exists and bufname or attach_file ,
466+ })
467+ vim .fn .setreg (vim .v .register , link )
456468 return basename
457469 end )
458470 end )
@@ -480,7 +492,10 @@ function AttachCore:attach_many(node, files, opts)
480492 .mapSeries (function (to_be_attached )
481493 local basename = basename_safe (to_be_attached )
482494 local attach_file = vim .fs .joinpath (attach_dir , basename )
483- return attach (to_be_attached , attach_file )
495+ return attach (to_be_attached , attach_file ):next (function (success )
496+ self .links :store_link_to_attachment ({ attach_dir = attach_dir , original = to_be_attached })
497+ return success
498+ end )
484499 end , files )
485500 --- @param successes boolean[]
486501 :next (function (successes )
0 commit comments