@@ -16,6 +16,8 @@ function M.get_selected_files_from_tree()
1616 return M ._get_neotree_selection ()
1717 elseif current_ft == " oil" then
1818 return M ._get_oil_selection ()
19+ elseif current_ft == " netrw" then
20+ return M ._get_netrw_selection ()
1921 else
2022 return nil , " Not in a supported tree buffer (current filetype: " .. current_ft .. " )"
2123 end
@@ -261,4 +263,54 @@ function M._get_oil_selection()
261263 return {}, " No file found under cursor"
262264end
263265
266+ --- Get selected files from netrw
267+ --- Supports both marked files and single file under cursor
268+ --- Reference: :help netrw-mf, :help markfilelist
269+ --- @return table files List of file paths
270+ --- @return string | nil error Error message if operation failed
271+ function M ._get_netrw_selection ()
272+ -- 1. Check for marked files
273+ local mf_ok , mf_result = pcall (function ()
274+ if vim .fn .exists (" *netrw#Expose" ) == 1 then
275+ return vim .fn .call (" netrw#Expose" , { " netrwmarkfilelist" })
276+ end
277+ return nil
278+ end )
279+
280+ local marked_files = {}
281+
282+ if mf_ok and mf_result and type (mf_result ) == " table" and # mf_result > 0 then
283+ for _ , file_path in ipairs (mf_result ) do
284+ if vim .fn .filereadable (file_path ) == 1 or vim .fn .isdirectory (file_path ) == 1 then
285+ table.insert (marked_files , file_path )
286+ end
287+ end
288+ end
289+
290+ if # marked_files > 0 then
291+ return marked_files , nil
292+ end
293+
294+ -- 2. No marked files. Check for a file or dir under cursor
295+ local path_ok , path_result = pcall (function ()
296+ if vim .fn .exists (" *netrw#Call" ) == 1 then
297+ local word = vim .fn .call (" netrw#Call" , { " NetrwGetWord" })
298+ if word ~= " " then
299+ return vim .fn .call (" netrw#Call" , { " NetrwFile" , word })
300+ end
301+ end
302+ return nil
303+ end )
304+
305+ if not path_ok or not path_result or path_result == " " then
306+ return {}, " Failed to get path from netrw"
307+ end
308+
309+ if vim .fn .filereadable (path_result ) == 1 or vim .fn .isdirectory (path_result ) == 1 then
310+ return { path_result }, nil
311+ end
312+
313+ return {}, " Invalid file or directory path: " .. path_result
314+ end
315+
264316return M
0 commit comments