Skip to content

Commit

Permalink
[Interface] Add the possibility to copy solely the filename at point.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiaanspeck authored and Alexander-Miller committed Dec 22, 2024
1 parent 4364a66 commit b26c155
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ Treemacs' keybindings are distributed to several keymaps, based on common keybin
| ya | treemacs-copy-absolute-path-at-point | Copy the absolute path of the node at point. |
| yr | treemacs-copy-relative-path-at-point | Copy the path of the node at point relative to the project root. |
| yp | treemacs-copy-project-path-at-point | Copy the absolute path of the project root for the node at point. |
| yn | treemacs-copy-filename-at-point | Copy the filename for the node at point. |
| yf | treemacs-copy-file | Copy the file at point. |

*** General Keybinds
Expand Down
13 changes: 13 additions & 0 deletions src/elisp/treemacs-interface.el
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,19 @@ With a prefix ARG substract the increment value multiple times."
(kill-new copied)
(treemacs-pulse-on-success "Copied project path: %s" (propertize copied 'face 'font-lock-string-face))))))

(defun treemacs-copy-filename-at-point ()
"Copy the filename of the node at point."
(interactive)
(treemacs-block
(-let [path (treemacs--prop-at-point :path)]
(treemacs-error-return-if (null path)
"There is nothing to copy here")
(treemacs-error-return-if (not (stringp path))
"Path at point is not a file.")
(let ((filename (file-name-nondirectory path)))
(kill-new filename)
(treemacs-pulse-on-success "Copied filename: %s" (propertize filename 'face 'font-lock-string-face))))))

(defun treemacs-paste-dir-at-point-to-minibuffer ()
"Paste the directory at point into the minibuffer.
This is used by the \"Paste here\" mouse menu button, which assumes that we are
Expand Down
1 change: 1 addition & 0 deletions src/elisp/treemacs-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Will be set by `treemacs--post-command'.")
(define-key map (kbd "a") 'treemacs-copy-absolute-path-at-point)
(define-key map (kbd "r") 'treemacs-copy-relative-path-at-point)
(define-key map (kbd "p") 'treemacs-copy-project-path-at-point)
(define-key map (kbd "n") 'treemacs-copy-filename-at-point)
(define-key map (kbd "f") 'treemacs-copy-file)
(define-key map (kbd "v") 'treemacs-paste-dir-at-point-to-minibuffer)
map)
Expand Down
3 changes: 2 additions & 1 deletion src/elisp/treemacs-mouse-interface.el
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ and ignore any prefix argument."
["Copy File" treemacs-copy-file :visible ,(check node)]
["Copy Absolute Path" treemacs-copy-absolute-path-at-point :visible ,(check node)]
["Copy Relative Path" treemacs-copy-relative-path-at-point :visible ,(check node)]
["Copy Project Path" treemacs-copy-project-path-at-point :visible ,(check node)])
["Copy Project Path" treemacs-copy-project-path-at-point :visible ,(check node)]
["Copy Filename" treemacs-copy-filename-at-point :visible ,(check node)])

["--" #'ignore t]
("Projects"
Expand Down
1 change: 1 addition & 0 deletions src/extra/treemacs-evil.el
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Then call ORIG-FUN with its ARGS and reopen treemacs if it was open before."
(evil-define-key 'treemacs treemacs-mode-map (kbd "yp") #'treemacs-copy-project-path-at-point)
(evil-define-key 'treemacs treemacs-mode-map (kbd "ya") #'treemacs-copy-absolute-path-at-point)
(evil-define-key 'treemacs treemacs-mode-map (kbd "yr") #'treemacs-copy-relative-path-at-point)
(evil-define-key 'treemacs treemacs-mode-map (kbd "yn") #'treemacs-copy-filename-at-point)
(evil-define-key 'treemacs treemacs-mode-map (kbd "yf") #'treemacs-copy-file)
(evil-define-key 'treemacs treemacs-mode-map (kbd "yv") #'treemacs-paste-dir-at-point-to-minibuffer)
(evil-define-key 'treemacs treemacs-mode-map (kbd "gr") #'treemacs-refresh)
Expand Down

0 comments on commit b26c155

Please sign in to comment.