From 5d93f2d3aeb1ccbd6bedce5a617c30218a5c0faf Mon Sep 17 00:00:00 2001 From: Chen Bin Date: Thu, 26 Sep 2024 15:49:00 +1000 Subject: [PATCH] can copy project relative path of current buffer --- lisp/init-clipboard.el | 23 ++++++++++++++++++++++- lisp/init-evil.el | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lisp/init-clipboard.el b/lisp/init-clipboard.el index 2e7d9ba564..0e53890e26 100644 --- a/lisp/init-clipboard.el +++ b/lisp/init-clipboard.el @@ -16,12 +16,33 @@ If N is not nil, copy file name and line number." (message "%s => clipboard&kill-ring" s)))) (defun cp-fullpath-of-current-buffer () - "Copy full path into the yank ring and OS clipboard" + "Copy full path into the yank ring and OS clipboard." (interactive) (when buffer-file-name (copy-yank-str (file-truename buffer-file-name)) (message "file full path => clipboard & yank ring"))) +(defun cp-root-relative-path-of-current-buffer () + "Copy path relative to the project root into the yank ring and OS clipboard" + (interactive) + (when buffer-file-name + ;; require ffip to get project root + (my-ensure 'find-file-in-project) + (let* ((root (ffip-project-root)) + (file-path (file-truename buffer-file-name)) + relative-path) + (cond + ((not root) + (message "Can't find project root.")) + + ((not file-path) + (message "File path of current buffer is empty.")) + + (t + (setq relative-path (file-relative-name file-path root)) + (copy-yank-str relative-path) + (message "%s => clipboard & yank ring" relative-path)))))) + (defun clipboard-to-kill-ring () "Copy from clipboard to `kill-ring'." (interactive) diff --git a/lisp/init-evil.el b/lisp/init-evil.el index c935e87abc..83ed0a7246 100644 --- a/lisp/init-evil.el +++ b/lisp/init-evil.el @@ -614,6 +614,7 @@ If N > 0 and in js, only occurrences in current N lines are renamed." "kc" 'kill-ring-to-clipboard "fn" 'cp-filename-of-current-buffer "fp" 'cp-fullpath-of-current-buffer + "rp" 'cp-root-relative-path-of-current-buffer "dj" 'dired-jump ;; open the dired from current file "xo" 'ace-window "ff" 'my-toggle-full-window ;; I use WIN+F in i3