From 259cf83ef1e73e6940f8e91d0f856974f0dbacbb Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 29 Apr 2021 14:30:17 -0400 Subject: [PATCH] Enhance FFAP lookup backend gf (+lookup/file) has been changed to: + Use the active selection instead of the filename-at-point when searching for a file (as requested in #4942). + Pre-fill the FFAP prompt with the thing at point if no obvious filepath can be guessed from it (via ffap-alist). + Offer to search the current project for the thing-at-point (if counsel-file-jump is available). + Fall back to a standard ffap prompt if all else fails. I've also reversed te order of file lookup backends so that our FFAP backend is treated as a never-failing last resort (also because the bug-reference backend is known to fail gracefully, so we can trust it to run first). Closes #4942 --- modules/tools/lookup/autoload/lookup.el | 33 +++++++++++++++---------- modules/tools/lookup/config.el | 4 +-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/modules/tools/lookup/autoload/lookup.el b/modules/tools/lookup/autoload/lookup.el index 98c520f65..26eecb8c0 100644 --- a/modules/tools/lookup/autoload/lookup.el +++ b/modules/tools/lookup/autoload/lookup.el @@ -258,11 +258,25 @@ current buffer." (not (and (>= pt beg) (< pt end)))))))) -(defun +lookup-ffap-backend-fn (_identifier) - "Uses `find-file-at-point' to read file at point." - (require 'ffap) - (when (ffap-guesser) - (find-file-at-point) +(defun +lookup-ffap-backend-fn (identifier) + "Tries to locate the file at point (or in active selection). +Uses find-in-project functionality (provided by ivy, helm, or project), +otherwise falling back to ffap.el (find-file-at-point)." + (let ((guess + (cond (identifier) + ((doom-region-active-p) + (buffer-substring-no-properties + (doom-region-beginning) + (doom-region-end))) + ((if (require 'ffap) (ffap-guesser))) + ((thing-at-point 'filename t))))) + (cond ((and (stringp guess) + (file-exists-p guess)) + (find-file-at-point guess)) + ((and (featurep! :completion ivy) + (doom-project-p)) + (counsel-file-jump guess (doom-project-root))) + ((find-file-at-point (ffap-prompter guess)))) t)) (defun +lookup-bug-reference-backend-fn (_identifier) @@ -351,7 +365,6 @@ for the current mode/buffer (if any), then falls back to the backends in (cond ((+lookup--jump-to :documentation identifier #'pop-to-buffer arg)) ((user-error "Couldn't find documentation for %S" identifier)))) -(defvar ffap-file-finder) ;;;###autoload (defun +lookup/file (&optional path) "Figure out PATH from whatever is at point and open it. @@ -368,13 +381,7 @@ Otherwise, falls back on `find-file-at-point'." ((+lookup--jump-to :file path)) - ((stringp path) (find-file-at-point path)) - - ((featurep! :completion ivy) - (counsel-file-jump (thing-at-point 'filename t) - (doom-project-root))) - - ((ffap-prompter (thing-at-point 'filename t))))) + ((user-error "Couldn't find any files here")))) ;; diff --git a/modules/tools/lookup/config.el b/modules/tools/lookup/config.el index 9064fadf7..792dbefb6 100644 --- a/modules/tools/lookup/config.el +++ b/modules/tools/lookup/config.el @@ -99,8 +99,8 @@ argument: the identifier at point. See `set-lookup-handlers!' about adding to this list.") (defvar +lookup-file-functions - '(+lookup-ffap-backend-fn - +lookup-bug-reference-backend-fn) + '(+lookup-bug-reference-backend-fn + +lookup-ffap-backend-fn) "Function for `+lookup/file' to try, before restoring to `find-file-at-point'. Stops at the first function to return non-nil or change the current window/point.