add copy operations for relative path and directory path

remotes/origin/HEAD
Ofir Gal 2023-09-05 11:03:29 +03:00
parent 59945bc1c9
commit 45da137f2f
3 changed files with 19 additions and 6 deletions

View File

@ -66,6 +66,9 @@ The following applies to all commands above:
- `.trashFile{trashLocation = "/your/path/"}` or `:Trash`: Move the current file to the trash location. [Defaults to the operating-system-specific trash directory.](https://github.com/chrisgrieser/nvim-genghis/blob/main/lua/genghis.lua#L164) ⚠️ Any existing file in the trash location with the same name is overwritten, making that file irretrievable. If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available, `require'bufdelete.nvim'.bufwipeout` would be used to keep window layout intact instead of `vim.cmd.bwipeout`. - `.trashFile{trashLocation = "/your/path/"}` or `:Trash`: Move the current file to the trash location. [Defaults to the operating-system-specific trash directory.](https://github.com/chrisgrieser/nvim-genghis/blob/main/lua/genghis.lua#L164) ⚠️ Any existing file in the trash location with the same name is overwritten, making that file irretrievable. If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available, `require'bufdelete.nvim'.bufwipeout` would be used to keep window layout intact instead of `vim.cmd.bwipeout`.
- `.copyFilename` or `:CopyFilename`: Copy the file name. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`. - `.copyFilename` or `:CopyFilename`: Copy the file name. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`.
- `.copyFilepath` or `:CopyFilepath`: Copy the absolute file path. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`. - `.copyFilepath` or `:CopyFilepath`: Copy the absolute file path. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`.
- `.copyRelativePath` or `:CopyRelativePath`: Copy the relative file path. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`.
- `.copyDirectoryPath` or `:CopyDirectoryPath`: Copy the absolute directory path. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`.
- `.copyRelativeDirectoryPath` or `:CopyRelativeDirectoryPath`: Copy the relative directory path. When `clipboard="unnamed[plus]"` has been set, copies to the `+` register, otherwise to `"`.
- `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod +x`. - `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod +x`.
### Disable Ex-Commands ### Disable Ex-Commands

View File

@ -152,24 +152,31 @@ function M.moveSelectionToNewFile() fileOp("newFromSel") end
---copying file information ---copying file information
---@param operation string filename|filepath ---@param operation string filename|filepath
local function copyOp(operation) local function copyOp(expandOperation)
local reg = '"' local reg = '"'
local clipboardOpt = vim.opt.clipboard:get() local clipboardOpt = vim.opt.clipboard:get()
local useSystemClipb = #clipboardOpt > 0 and clipboardOpt[1]:find("unnamed") local useSystemClipb = #clipboardOpt > 0 and clipboardOpt[1]:find("unnamed")
if useSystemClipb then reg = "+" end if useSystemClipb then reg = "+" end
local toCopy = expand("%:p") local toCopy = expand(expandOperation)
if operation == "filename" then toCopy = expand("%:t") end
fn.setreg(reg, toCopy) fn.setreg(reg, toCopy)
vim.notify(toCopy, vim.log.levels.INFO, { title = "Copied" }) vim.notify(toCopy, vim.log.levels.INFO, { title = "Copied" })
end end
---Copy absolute path of current file ---Copy absolute path of current file
function M.copyFilepath() copyOp("filepath") end function M.copyFilepath() copyOp("%:p") end
---Copy name of current file ---Copy name of current file
function M.copyFilename() copyOp("filename") end function M.copyFilename() copyOp("%:t") end
---Copy relative path of current file
function M.copyRelativePath() copyOp("%:~:.") end
---Copy absolute directory path of current file
function M.copyDirectoryPath() copyOp("%:p:h") end
---Copy relative directory path of current file
function M.copyRelativeDirectoryPath() copyOp("%:~:.:h") end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -12,6 +12,9 @@ if not vim.g.genghis_disable_commands then
command("Move", function() genghis.moveAndRenameFile() end, {}) command("Move", function() genghis.moveAndRenameFile() end, {})
command("CopyFilename", function() genghis.copyFilename() end, {}) command("CopyFilename", function() genghis.copyFilename() end, {})
command("CopyFilepath", function() genghis.copyFilepath() end, {}) command("CopyFilepath", function() genghis.copyFilepath() end, {})
command("CopyRelativePath", function() genghis.copyRelativePath() end, {})
command("CopyDirectoryPath", function() genghis.copyDirectoryPath() end, {})
command("CopyRelativeDirectoryPath", function() genghis.copyRelativeDirectoryPath() end, {})
command("Chmodx", function() genghis.chmodx() end, {}) command("Chmodx", function() genghis.chmodx() end, {})
command("New", function() genghis.createNewFile() end, {}) command("New", function() genghis.createNewFile() end, {})
end end