When opening a file with find-file, the default entry in the minibuffer is the current directory. If I press Down, I see the name of the current file (if the current buffer is visiting a file).

Sometimes, if I press Down, I see what seems to be a guess of a file name in the current buffer, and I need to press Down twice. How can I customize this “guess of a file name”?

Specifically, I often edit files that contain /** and this is offered at the find-file prompt even though it isn't a file name. How can I avoid getting offered to open /**?

I'm using Emacs 24.5 but I want to keep compatibility with other Emacs versions.


This comes from ffap (find file at point), as the name might suggest. Specifically, find-file uses read-file-name to read its input, which in turn calls read-file-name-default unless overridden through the variable read-file-name-function. read-file-name-default calls the auxiliary function read-file-name--defaults to build the initial inputs: current directory, file name at point (if non-nil), and current file name (if non-nil). The file name at point is the first function from file-name-at-point-functions that returns non-nil, and this list of functions contains ffap-guess-file-name-at-point by default. For our purposes, this function is a wrapper around ffap-file-at-point.

To completely turn off the file-name-at-point feature of find-file and other functions that prompt for a file name, you can advise read-file-name--defaults or read-file-name-default or read-file-name. Untested:

(defadvice read-file-name
  (around read-file-name-no-ffap activate)
  (let ((file-name-at-point-functions nil))

To skip not-really-a-file-name strings such as /**, from all uses of ffap-file-at-point, you can advise ffap-file-at-point.

(defadvice ffap-file-at-point
    (after ffap-file-at-point-false-positives activate compile)
  "Omit certain false positives from the guesses."
  (if (save-match-data
        (and ad-return-value
             ;; If it doesn't have at least one letter, it's probably
             ;; not a file name.
             (not (string-match "[A-Za-z]" ad-return-value)))
        (setq ad-return-value nil))))

(This answer uses the “classic” advice interface that's compatible with older Emacs version. If you don't care about old versions, you may want to port them to the modern advice interface.)

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.