1

I'm very new to customizing Emacs, and haven't written Elisp commands before. I'm struggling to find a command and key binding that combines find-file-at-point and find-file-in-project (or similar), perhaps a:

M-x find-file-in-project-at-point

such that given a file/folder structure:

├── main.txt
├── file1.txt
└── somepath
    └── file2.txt

where I'm editing main.txt

% cat main.txt
line 1 file1.txt
line 2 file2.txt

and a shortcut will find file2.txt when my cursor is somewhere on the text of file2.txt - despite it being located in ./somepath/file2.txt. It would be safe to assume to start the search relative to main.txt. Ideally, open the file if only 1 match is found, otherwise list possible choices in minibuffer.

I have had success opening file1.txt from main.txt OK, even w/o confirmation with this pretty neat command.

I'm typically not dealing with a "project" (.git) folder structures, & folder/file structure isn't that large where an overly complex package is needed (where it might to run/index in background).

My apologies if I missed an obvious solution.

Other commands & packages functions that appear useful, but I don't have the expertise to combine them in the way I want:

Any advice?

0

Interesting idea, I never thought I need find file at point. I usually just use expand-region.el to select file name or partial of file path. Then M-x find-file-in-project-by-selected.

Any way, here is my implementation of find file at point:

(defun find-file-in-project-at-point (&optional open-another-window)
  "Find file whose name is guessed around point.
If OPEN-ANOTHER-WINDOW is not nil, the file will be opened in new window."
  (interactive "P")
  (let* ((filename (or (ffap-file-at-point)
                       (thing-at-point 'filename)
                       (thing-at-point 'symbol)
                       (read-string "No file name at point. Please provide file name:")))
         ;; filename could be a path
         (ffip-match-path-instead-of-filename t))
    (cond
     (filename
      ;; strip prefix "../../" or "././" from file name
      (setq filename (replace-regexp-in-string "^\\(\\.\\.*/\\)*" "" filename))
      (ffip-find-files filename open-another-window))
     (t
      (message "No file name is provided.")))))

Either copy this function into your .emacs or just upgrade find-file-in-project to v5.5.0

  • Perfect! This is an excellent feature. Thank you! – bscipio Feb 19 '18 at 22:53
  • So this is perfect for the example in original post. However, when the text (main.txt) contains a full path: line 3 /fullpath/file3.txt and I select file3.txt it appears to search the /fullpath/ or somewhere for awhile and then the function still didn't find the file. This seems very close to being super @chenbin – bscipio Feb 19 '18 at 23:26
  • Just double check. So you expect only the "file3.txt" is used as search keyword if "file.txt" is selected text, right? If you got any other requirements, raise issue at github.com/technomancy/find-file-in-project – chen bin Feb 19 '18 at 23:43
  • Hopefully this helps: If the cursor was on a file.txt - but where it is a full-path (ex. /fullpath/file3.txt), then I would desire the function to use that full-path first to open it. Otherwise (if not found, or in general not a full-path), search for file.txt based on location of main.txt path. I can also add a github issue later with more detailed example. Thanks so much for responding! – bscipio Feb 20 '18 at 2:24
  • Yes, please raise an issue on github, thanks. – chen bin Feb 20 '18 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.