FindFileAtPoint

Find File at Point (ffap.el) is a builtin Emacs library, created by MichaelangeloGrigni, for opening a filename or URL at point. The basic features are described in the Emacs manual (ffap).

There’s various mode-specific features hiding in ffap,

Here are some other libraries and modes extending ffap,

RFC host (Emacs 21)

In Emacs 21 the default host to download RFC documents is ds.internic.net, but it’s gone away. Emacs 22 has changed to use rfc-editor.org, you can do the same with

    (setq ffap-rfc-path "/anonymous@ftp.rfc-editor.org:/in-notes/rfc%s.txt")

TeX Path Search Enhancement

The default TeX path search method cannot search all the TeX package. Here is some enhancement for the path search scheme:

(defvar my-texlive-db-path
  '("D:\\texlive\\2009\\texmf" "D:\\texlive\\2009\\texmf-dist"))

(defun ffap-locate-tex-path (pakname)
  (save-excursion
    (save-match-data
      (when (re-search-forward (concat "^" pakname "$") nil t)
        ;; if we found
        (re-search-backward "\\(\\..*\\):$" nil t)
        (concat
         (file-name-as-directory
          (concat (file-name-as-directory dbpath) (match-string 1)))
         pakname)))))

(defun ffap-locate-tex-file (pakname suffix paths)
  (catch 'break
    (let (r)
      (dolist (dbpath paths)
        (with-temp-buffer
          (condition-case nil
              (progn
                (insert-file-contents
                 (concat (file-name-as-directory dbpath) "ls-R"))
                (dolist (suf suffix)
                  (setq r (ffap-locate-tex-path (concat pakname suf)))
                  (if r (throw 'break r))))
            (error nil)))))))

(eval-after-load "ffap"
  '(progn
     (defun ffap-tex-mode (name)
       (ffap-tex-init)
       (ffap-locate-tex-file name '(".tex" "") my-texlive-db-path))

     (defun ffap-latex-mode (name)
       (ffap-tex-init)
       ;; only rare need for ""
       (ffap-locate-tex-file name '(".cls" ".sty" ".tex" "") my-texlive-db-path))
     ))

Ignore '/'

On unix based systems, / is the root path. So all string starts with / is recognized as a path.

This is annoying especially when editing HTML or XML. The following advice ignore / as a wrong result.

(defadvice ffap-file-at-point (after ffap-file-at-point-after-advice ())
  (if (string= ad-return-value "/")
      (setq ad-return-value nil)))
(ad-activate 'ffap-file-at-point)
;; (ad-deactivate 'ffap-file-at-point)

– tomykaira, feel free to use this snippet in any way.

make ffap open remote absolute path correctly with tramp mode.

At this time when you open an absolute path in a remote tramp buffer, it will open this file locally. I wrote a new function to fix this and make the line number recognition enhanced.

(defun my-find-file-at-point-with-line ()
  "Opens the file at point and goes to line-number."
  (interactive)
  (let ((fname (ffap-file-at-point)))
    (if fname
      (let ((line
             (save-excursion
               (goto-char (cadr ffap-string-at-point-region))
               (and (re-search-backward ":\\([0-9]+\\)"
                                        (line-beginning-position) t)
                    (string-to-int (match-string 1))))))
        ;; (message "file:%s,line:%s" fname line)
        (when (and (tramp-tramp-file-p default-directory)
                   (= ?/ (aref fname 0)))
          ;; if fname is an absolute path in remote machine, it will not return a tramp path,fix it here.
          (let ((pos (position ?: default-directory)))
            (if (not pos) (error "failed find first tramp indentifier ':'"))
            (setf pos (position ?: default-directory :start (1+ pos)))
            (if (not pos) (error "failed find second tramp indentifier ':'"))
            (setf fname (concat (substring default-directory 0 (1+ pos)) fname))))
        (message "fname:%s" fname)
        (find-file-existing fname)
        (when line (goto-line line)))
      (error "File does not exist."))))

JingtaoXu.

Maybe I’m missing something, but it sounds like you are reporting an Emacs (Tramp) bug here, together with a workaround/fix. If so, why don’t you report the bug to the Tramp maintainer? ‘M-x report-emacs-bug’DrewAdams

Make ffap open a Java source file from import statement

You can open a Java source file or directory from the import statement in Java. The location of the source must be known to work via ffap-java/source-directories. The source should be either a directory name that contains the sources, or a jar archive name that contains the sources. Download the source here. – cinsk

Check ffap string for line-number and goto it (using advice)

Useful e.g. for output from grep -nH. Expects file-name:line-number.

(defvar ffap-file-at-point-line-number nil
  "Variable to hold line number from the last `ffap-file-at-point' call.")

(defadvice ffap-file-at-point (after ffap-store-line-number activate)
  "Search `ffap-string-at-point' for a line number pattern and
save it in `ffap-file-at-point-line-number' variable."
  (let* ((string (ffap-string-at-point)) ;; string/name definition copied from `ffap-string-at-point'
         (name
          (or (condition-case nil
                  (and (not (string-match "//" string)) ; foo.com://bar
                       (substitute-in-file-name string))
                (error nil))
              string))
         (line-number-string 
          (and (string-match ":[0-9]+" name)
               (substring name (1+ (match-beginning 0)) (match-end 0))))
         (line-number
          (and line-number-string
               (string-to-number line-number-string))))
    (if (and line-number (> line-number 0)) 
        (setq ffap-file-at-point-line-number line-number)
      (setq ffap-file-at-point-line-number nil))))

(defadvice find-file-at-point (after ffap-goto-line-number activate)
  "If `ffap-file-at-point-line-number' is non-nil goto this line."
  (when ffap-file-at-point-line-number
    (goto-line ffap-file-at-point-line-number)
    (setq ffap-file-at-point-line-number nil)))

– DanielPoersch?


CategoryFiles OpenFilesInDifferentWays