From 658d0c5ab577bc6bc617cccab91d835fa36dcda9 Mon Sep 17 00:00:00 2001 From: liuzhishan Date: Thu, 20 Jun 2024 20:52:52 +0800 Subject: [PATCH 1/5] add support for relative path for gf --- evil-commands.el | 23 +++++++++++++++++++++++ evil-maps.el | 6 +++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/evil-commands.el b/evil-commands.el index 4ad249fc..6a46357e 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -3727,6 +3727,29 @@ Supports positions in the following formats: \"path:line path(line)\", (when column-number (move-to-column (1- column-number))))))) +(evil-define-command evil-open-file-under-cursor () + "Open the file under cursor and go to position if present. +Supports both relative path and absolute path. If path is relative, then find common +ancestor of path and current buffer file. If path is absolute path, go to the file +directly. Otherwise, fallback to evil-find-file-at-point-with-line." + (interactive) + (let* ((file-path (thing-at-point 'filename t))) + (if (file-name-absolute-p file-path) + (find-file file-path) + (let* ((parts (split-string (buffer-file-name) "/")) + (target-filename "") + (target-exists nil)) + + (dolist (n (number-sequence 1 (length parts)) target-filename) + (let* ((new-filename (string-join (append (-take n parts) (list file-path)) "/"))) + (if (file-exists-p new-filename) + (setq target-filename new-filename + target-exists t)))) + + (if target-exists + (find-file target-filename) + (evil-find-file-at-point-with-line)))))) + (evil-define-command evil-find-file-at-point-visual () "Find the filename selected by the visual region. Signal an error if the file does not exist." diff --git a/evil-maps.el b/evil-maps.el index 0dfa85dd..257eeea7 100644 --- a/evil-maps.el +++ b/evil-maps.el @@ -75,9 +75,9 @@ (define-key evil-normal-state-map "gw" 'evil-fill) (define-key evil-normal-state-map "gu" 'evil-downcase) (define-key evil-normal-state-map "gU" 'evil-upcase) -(define-key evil-normal-state-map "gf" 'find-file-at-point) -(define-key evil-normal-state-map "]f" 'find-file-at-point) -(define-key evil-normal-state-map "[f" 'find-file-at-point) +(define-key evil-normal-state-map "gf" 'evil-open-file-under-cursor) +(define-key evil-normal-state-map "]f" 'evil-open-file-under-cursor) +(define-key evil-normal-state-map "[f" 'evil-open-file-under-cursor) (define-key evil-normal-state-map "gF" 'evil-find-file-at-point-with-line) (define-key evil-normal-state-map "]F" 'evil-find-file-at-point-with-line) (define-key evil-normal-state-map "[F" 'evil-find-file-at-point-with-line) From a837719da7549ada2ed1a19520f7eb1e28f3ca96 Mon Sep 17 00:00:00 2001 From: liuzhishan01 Date: Sat, 22 Jun 2024 16:32:02 +0800 Subject: [PATCH 2/5] fix some logic in evil-open-file-under-cursor --- evil-commands.el | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/evil-commands.el b/evil-commands.el index 6a46357e..bb78e686 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -3727,28 +3727,25 @@ Supports positions in the following formats: \"path:line path(line)\", (when column-number (move-to-column (1- column-number))))))) -(evil-define-command evil-open-file-under-cursor () +(evil-define-command evil-open-file-under-cursor (file-path) "Open the file under cursor and go to position if present. Supports both relative path and absolute path. If path is relative, then find common ancestor of path and current buffer file. If path is absolute path, go to the file -directly. Otherwise, fallback to evil-find-file-at-point-with-line." - (interactive) - (let* ((file-path (thing-at-point 'filename t))) - (if (file-name-absolute-p file-path) - (find-file file-path) - (let* ((parts (split-string (buffer-file-name) "/")) - (target-filename "") - (target-exists nil)) - - (dolist (n (number-sequence 1 (length parts)) target-filename) - (let* ((new-filename (string-join (append (-take n parts) (list file-path)) "/"))) - (if (file-exists-p new-filename) - (setq target-filename new-filename - target-exists t)))) - - (if target-exists - (find-file target-filename) - (evil-find-file-at-point-with-line)))))) +directly. Otherwise, fallback to `evil-find-file-at-point-with-line`." + (interactive + (let ((file-path (thing-at-point 'filename t))) + (list file-path))) + + (if (file-name-absolute-p file-path) + (find-file file-path) + + (if (and (stringp file-path) + (string-match-p "/" file-path)) + (let ((dirname (locate-dominating-file default-directory file-path))) + (if (file-in-directory-p dirname (doom-project-root)) + (find-file (doom-path dirname file-path)) + (evil-find-file-at-point-with-line))) + (evil-find-file-at-point-with-line)))) (evil-define-command evil-find-file-at-point-visual () "Find the filename selected by the visual region. From 6e5e3d7f346d9114e7615c4f7c3699bb1134f187 Mon Sep 17 00:00:00 2001 From: liuzhishan01 Date: Sat, 29 Jun 2024 11:50:20 +0800 Subject: [PATCH 3/5] feat: fix some logic for gf under cursor --- evil-commands.el | 31 ++++++++++++++++++++----------- evil-maps.el | 6 +++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/evil-commands.el b/evil-commands.el index bb78e686..7dbfc07d 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -3727,25 +3727,34 @@ Supports positions in the following formats: \"path:line path(line)\", (when column-number (move-to-column (1- column-number))))))) -(evil-define-command evil-open-file-under-cursor (file-path) +(evil-define-command evil-goto-file (file-path) "Open the file under cursor and go to position if present. Supports both relative path and absolute path. If path is relative, then find common ancestor of path and current buffer file. If path is absolute path, go to the file -directly. Otherwise, fallback to `evil-find-file-at-point-with-line`." +directly. Otherwise, fallback to `find-file-at-point`." (interactive - (let ((file-path (thing-at-point 'filename t))) - (list file-path))) + (list (thing-at-point 'filename t))) (if (file-name-absolute-p file-path) (find-file file-path) - (if (and (stringp file-path) - (string-match-p "/" file-path)) - (let ((dirname (locate-dominating-file default-directory file-path))) - (if (file-in-directory-p dirname (doom-project-root)) - (find-file (doom-path dirname file-path)) - (evil-find-file-at-point-with-line))) - (evil-find-file-at-point-with-line)))) + (let* ((delimiter (if (eq system-type 'windows-nt) + "\\" + "/")) + (parts (split-string (buffer-file-name) delimiter)) + (target-filename "") + (target-exists)) + + ;; check every combination of sub directory and file-path + (dolist (n (number-sequence 1 (length parts)) target-filename) + (let* ((new-filename (string-join (append (take n parts) (list file-path)) delimiter))) + (if (file-exists-p new-filename) + (setq target-filename new-filename + target-exists t)))) + + (if target-exists + (find-file target-filename) + (find-file-at-point file-path))))) (evil-define-command evil-find-file-at-point-visual () "Find the filename selected by the visual region. diff --git a/evil-maps.el b/evil-maps.el index 257eeea7..4180ed05 100644 --- a/evil-maps.el +++ b/evil-maps.el @@ -75,9 +75,9 @@ (define-key evil-normal-state-map "gw" 'evil-fill) (define-key evil-normal-state-map "gu" 'evil-downcase) (define-key evil-normal-state-map "gU" 'evil-upcase) -(define-key evil-normal-state-map "gf" 'evil-open-file-under-cursor) -(define-key evil-normal-state-map "]f" 'evil-open-file-under-cursor) -(define-key evil-normal-state-map "[f" 'evil-open-file-under-cursor) +(define-key evil-normal-state-map "gf" 'evil-goto-file) +(define-key evil-normal-state-map "]f" 'evil-goto-file) +(define-key evil-normal-state-map "[f" 'evil-goto-file) (define-key evil-normal-state-map "gF" 'evil-find-file-at-point-with-line) (define-key evil-normal-state-map "]F" 'evil-find-file-at-point-with-line) (define-key evil-normal-state-map "[F" 'evil-find-file-at-point-with-line) From f0fe562f4e8b0e9ddf9a09c0402aac77b1d593c3 Mon Sep 17 00:00:00 2001 From: Tom Dalziel Date: Wed, 19 Jun 2024 15:59:39 +0200 Subject: [PATCH 4/5] Add :badd --- evil-commands.el | 8 ++++++++ evil-maps.el | 1 + 2 files changed, 9 insertions(+) diff --git a/evil-commands.el b/evil-commands.el index 7dbfc07d..b3224ede 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -3349,6 +3349,14 @@ If no FILE is specified, reload the current buffer from disk as read-only." (revert-buffer bang (or bang (not (buffer-modified-p))) t) (read-only-mode +1))) +(evil-define-command evil-buffer-add (file) + "Add FILE to the buffer list, but don't visit it. " + :repeat nil + (interactive "") + (if (or (not file) (string= "" file)) + (user-error "No file specified") + (find-file-noselect file))) + (evil-define-command evil-read (count file) "Insert the contents of FILE below the current line or line COUNT." :repeat nil diff --git a/evil-maps.el b/evil-maps.el index 4180ed05..7a74fa68 100644 --- a/evil-maps.el +++ b/evil-maps.el @@ -515,6 +515,7 @@ included in `evil-insert-state-bindings' by default." (evil-ex-define-cmd "sav[eas]" 'evil-save) (evil-ex-define-cmd "r[ead]" 'evil-read) (evil-ex-define-cmd "b[uffer]" 'evil-buffer) +(evil-ex-define-cmd "bad[d]" 'evil-buffer-add) (evil-ex-define-cmd "bn[ext]" 'evil-next-buffer) (evil-ex-define-cmd "bp[revious]" 'evil-prev-buffer) (evil-ex-define-cmd "bN[ext]" "bprevious") From 97a2b05fb1bc26bb3ff402c5d532b93daf1bba62 Mon Sep 17 00:00:00 2001 From: liuzhishan01 Date: Sat, 6 Jul 2024 08:33:28 +0800 Subject: [PATCH 5/5] feat: add comments of limitation of evil-goto-file and change search order --- evil-commands.el | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/evil-commands.el b/evil-commands.el index b3224ede..9532a381 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -3739,7 +3739,15 @@ Supports positions in the following formats: \"path:line path(line)\", "Open the file under cursor and go to position if present. Supports both relative path and absolute path. If path is relative, then find common ancestor of path and current buffer file. If path is absolute path, go to the file -directly. Otherwise, fallback to `find-file-at-point`." +directly. Otherwise, fallback to `find-file-at-point`. + +Please notice that for relative path under cursor, only the most possible searching +approach is implemented in this version. There are many possible relative filepath +searching approaches, for example, same directory of current buffer, project root, +paths in system path environment. The most possible search approach would be the +same ancestor directory of current buffer and the target path, from parent directory +to root directory. +" (interactive (list (thing-at-point 'filename t))) @@ -3754,7 +3762,7 @@ directly. Otherwise, fallback to `find-file-at-point`." (target-exists)) ;; check every combination of sub directory and file-path - (dolist (n (number-sequence 1 (length parts)) target-filename) + (dolist (n (number-sequence (length parts) 1 -1) target-filename) (let* ((new-filename (string-join (append (take n parts) (list file-path)) delimiter))) (if (file-exists-p new-filename) (setq target-filename new-filename