@@ -12,10 +12,14 @@ local DirectoryNode = require("nvim-tree.node.directory")
1212--- @alias ClipboardAction " copy" | " cut"
1313--- @alias ClipboardData table<ClipboardAction , Node[]>
1414
15+ --- @alias ClipboardActionFn fun ( source : string , dest : string ): boolean , string ?
16+
1517--- @class Clipboard to handle all actions.fs clipboard API
1618--- @field config table hydrated user opts.filters
1719--- @field private explorer Explorer
1820--- @field private data ClipboardData
21+ --- @field private clipboard_name string
22+ --- @field private reg string
1923local Clipboard = {}
2024
2125--- @param opts table user options
@@ -29,9 +33,10 @@ function Clipboard:new(opts, explorer)
2933 copy = {},
3034 cut = {},
3135 },
36+ clipboard_name = opts .actions .use_system_clipboard and " system" or " neovim" ,
37+ reg = opts .actions .use_system_clipboard and " +" or " 1" ,
3238 config = {
3339 filesystem_watchers = opts .filesystem_watchers ,
34- actions = opts .actions ,
3540 },
3641 }
3742
4550--- @return boolean
4651--- @return string | nil
4752local function do_copy (source , destination )
48- local source_stats , errmsg = vim .loop .fs_stat (source )
53+ local source_stats , err = vim .loop .fs_stat (source )
4954
5055 if not source_stats then
51- log .line (" copy_paste" , " do_copy fs_stat '%s' failed '%s'" , source , errmsg )
52- return false , errmsg
56+ log .line (" copy_paste" , " do_copy fs_stat '%s' failed '%s'" , source , err )
57+ return false , err
5358 end
5459
5560 log .line (" copy_paste" , " do_copy %s '%s' -> '%s'" , source_stats .type , source , destination )
@@ -61,27 +66,27 @@ local function do_copy(source, destination)
6166
6267 if source_stats .type == " file" then
6368 local success
64- success , errmsg = vim .loop .fs_copyfile (source , destination )
69+ success , err = vim .loop .fs_copyfile (source , destination )
6570 if not success then
66- log .line (" copy_paste" , " do_copy fs_copyfile failed '%s'" , errmsg )
67- return false , errmsg
71+ log .line (" copy_paste" , " do_copy fs_copyfile failed '%s'" , err )
72+ return false , err
6873 end
6974 return true
7075 elseif source_stats .type == " directory" then
7176 local handle
72- handle , errmsg = vim .loop .fs_scandir (source )
77+ handle , err = vim .loop .fs_scandir (source )
7378 if type (handle ) == " string" then
7479 return false , handle
7580 elseif not handle then
76- log .line (" copy_paste" , " do_copy fs_scandir '%s' failed '%s'" , source , errmsg )
77- return false , errmsg
81+ log .line (" copy_paste" , " do_copy fs_scandir '%s' failed '%s'" , source , err )
82+ return false , err
7883 end
7984
8085 local success
81- success , errmsg = vim .loop .fs_mkdir (destination , source_stats .mode )
86+ success , err = vim .loop .fs_mkdir (destination , source_stats .mode )
8287 if not success then
83- log .line (" copy_paste" , " do_copy fs_mkdir '%s' failed '%s'" , destination , errmsg )
84- return false , errmsg
88+ log .line (" copy_paste" , " do_copy fs_mkdir '%s' failed '%s'" , destination , err )
89+ return false , err
8590 end
8691
8792 while true do
@@ -92,15 +97,15 @@ local function do_copy(source, destination)
9297
9398 local new_name = utils .path_join ({ source , name })
9499 local new_destination = utils .path_join ({ destination , name })
95- success , errmsg = do_copy (new_name , new_destination )
100+ success , err = do_copy (new_name , new_destination )
96101 if not success then
97- return false , errmsg
102+ return false , err
98103 end
99104 end
100105 else
101- errmsg = string.format (" '%s' illegal file type '%s'" , source , source_stats .type )
102- log .line (" copy_paste" , " do_copy %s" , errmsg )
103- return false , errmsg
106+ err = string.format (" '%s' illegal file type '%s'" , source , source_stats .type )
107+ log .line (" copy_paste" , " do_copy %s" , err )
108+ return false , err
104109 end
105110
106111 return true
@@ -109,27 +114,25 @@ end
109114--- @param source string
110115--- @param dest string
111116--- @param action ClipboardAction
112- --- @param action_fn fun ( source : string , dest : string )
117+ --- @param action_fn ClipboardActionFn
113118--- @return boolean | nil -- success
114119--- @return string | nil -- error message
115120local function do_single_paste (source , dest , action , action_fn )
116- local dest_stats
117- local success , errmsg , errcode
118121 local notify_source = notify .render_path (source )
119122
120123 log .line (" copy_paste" , " do_single_paste '%s' -> '%s'" , source , dest )
121124
122- dest_stats , errmsg , errcode = vim .loop .fs_stat (dest )
123- if not dest_stats and errcode ~= " ENOENT" then
124- notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
125- return false , errmsg
125+ local dest_stats , err , err_name = vim .loop .fs_stat (dest )
126+ if not dest_stats and err_name ~= " ENOENT" then
127+ notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (err or " ???" ))
128+ return false , err
126129 end
127130
128131 local function on_process ()
129- success , errmsg = action_fn (source , dest )
132+ local success , error = action_fn (source , dest )
130133 if not success then
131- notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or " ???" ))
132- return false , errmsg
134+ notify .error (" Could not " .. action .. " " .. notify_source .. " - " .. (error or " ???" ))
135+ return false , error
133136 end
134137
135138 find_file (utils .path_remove_trailing (dest ))
216219--- @private
217220--- @param node Node
218221--- @param action ClipboardAction
219- --- @param action_fn fun ( source : string , dest : string )
222+ --- @param action_fn ClipboardActionFn
220223function Clipboard :do_paste (node , action , action_fn )
221224 if node .name == " .." then
222225 node = self .explorer
@@ -232,10 +235,10 @@ function Clipboard:do_paste(node, action, action_fn)
232235 end
233236
234237 local destination = node .absolute_path
235- local stats , errmsg , errcode = vim .loop .fs_stat (destination )
236- if not stats and errcode ~= " ENOENT" then
237- log .line (" copy_paste" , " do_paste fs_stat '%s' failed '%s'" , destination , errmsg )
238- notify .error (" Could not " .. action .. " " .. notify .render_path (destination ) .. " - " .. (errmsg or " ???" ))
238+ local stats , err , err_name = vim .loop .fs_stat (destination )
239+ if not stats and err_name ~= " ENOENT" then
240+ log .line (" copy_paste" , " do_paste fs_stat '%s' failed '%s'" , destination , err )
241+ notify .error (" Could not " .. action .. " " .. notify .render_path (destination ) .. " - " .. (err or " ???" ))
239242 return
240243 end
241244 local is_dir = stats and stats .type == " directory"
@@ -307,65 +310,45 @@ end
307310
308311--- @param content string
309312function Clipboard :copy_to_reg (content )
310- local clipboard_name
311- local reg
312- if self .config .actions .use_system_clipboard == true then
313- clipboard_name = " system"
314- reg = " +"
315- else
316- clipboard_name = " neovim"
317- reg = " 1"
318- end
319-
320313 -- manually firing TextYankPost does not set vim.v.event
321314 -- workaround: create a scratch buffer with the clipboard contents and send a yank command
322315 local temp_buf = vim .api .nvim_create_buf (false , true )
323316 vim .api .nvim_buf_set_text (temp_buf , 0 , 0 , 0 , 0 , { content })
324317 vim .api .nvim_buf_call (temp_buf , function ()
325- vim .cmd (string.format (' normal! "%sy$' , reg ))
318+ vim .cmd (string.format (' normal! "%sy$' , self . reg ))
326319 end )
327320 vim .api .nvim_buf_delete (temp_buf , {})
328321
329- notify .info (string.format (" Copied %s to %s clipboard!" , content , clipboard_name ))
322+ notify .info (string.format (" Copied %s to %s clipboard!" , content , self . clipboard_name ))
330323end
331324
332325--- @param node Node
333326function Clipboard :copy_filename (node )
334- local content
335-
336327 if node .name == " .." then
337328 -- root
338- content = vim .fn .fnamemodify (self .explorer .absolute_path , " :t" )
329+ self : copy_to_reg ( vim .fn .fnamemodify (self .explorer .absolute_path , " :t" ) )
339330 else
340331 -- node
341- content = node .name
332+ self : copy_to_reg ( node .name )
342333 end
343-
344- self :copy_to_reg (content )
345334end
346335
347336--- @param node Node
348337function Clipboard :copy_basename (node )
349- local content
350-
351338 if node .name == " .." then
352339 -- root
353- content = vim .fn .fnamemodify (self .explorer .absolute_path , " :t:r" )
340+ self : copy_to_reg ( vim .fn .fnamemodify (self .explorer .absolute_path , " :t:r" ) )
354341 else
355342 -- node
356- content = vim .fn .fnamemodify (node .name , " :r" )
343+ self : copy_to_reg ( vim .fn .fnamemodify (node .name , " :r" ) )
357344 end
358-
359- self :copy_to_reg (content )
360345end
361346
362347--- @param node Node
363348function Clipboard :copy_path (node )
364- local content
365-
366349 if node .name == " .." then
367350 -- root
368- content = utils .path_add_trailing (" " )
351+ self : copy_to_reg ( utils .path_add_trailing (" " ) )
369352 else
370353 -- node
371354 local absolute_path = node .absolute_path
@@ -375,10 +358,12 @@ function Clipboard:copy_path(node)
375358 end
376359
377360 local relative_path = utils .path_relative (absolute_path , cwd )
378- content = node :is (DirectoryNode ) and utils .path_add_trailing (relative_path ) or relative_path
361+ if node :is (DirectoryNode ) then
362+ self :copy_to_reg (utils .path_add_trailing (relative_path ))
363+ else
364+ self :copy_to_reg (relative_path )
365+ end
379366 end
380-
381- self :copy_to_reg (content )
382367end
383368
384369--- @param node Node
0 commit comments