views:

62

answers:

2

Is there some emacs lisp code that would automatically find /nfs file paths in the buffer and highlight/link to them? So clicking on them would open that file?

Example path: /nfs/foo/bar/file.txt

+3  A: 

There's probably a package that does this already, but I don't know of it.

This bit of code adds buttons to text it thinks looks like a path to a file. You can add the function 'buttonize-buffer to find-file-hooks or run it manually (or conditionally).

(define-button-type 'find-file-button
  'follow-link t
  'action #'find-file-button)

(defun find-file-button (button)
  (find-file (buffer-substring (button-start button) (button-end button))))

(defun buttonize-buffer ()
  "turn all file paths into buttons"
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "/[^ \t]*" nil t)
      (make-button (match-beginning 0) (match-end 0) :type 'find-file-button))))

; (add-hook 'find-file-hook 'buttonize-buffer)   ; uncomment to add to find file
Trey Jackson
This is perfect. I did some more characters to avoid... re-search-forward "/nfs[^ \t\n,\'\"]*" .. there's probably more that I will find, but it's easy enough to add.
Gregory
+2  A: 

Try the built-in package ffap (find file at point):

http://www.emacswiki.org/emacs/FindFileAtPoint

http://www.gnu.org/software/emacs/manual/html_node/emacs/FFAP.html

I don't use the minor mode, instead binding a key to ffap that I hit when on a filename.

scottfrazer
this might work too, but has the extra step of moving the pointer to the place of the path and hitting a key. Trey's answer is mouse driven and very efficient.
Gregory