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
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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
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
Try the built-in package ffap (find file at point):
I don't use the minor mode, instead binding a key to
ffap that I hit when on a filename.
Great solution. But I agree in using
ffap, which is part of GNU Emacs.
solves many subtle problems, it expands environment variables and also catches
ffap cannot be used easily from own Lisp. My reimplementation of
buttonize-buffer is based on
ffap-guesser. The hard part was to work arround the bug mentioned below, and to get the start point of the file or URL, which
ffap-guesser does not provide.
The parser scans through the current buffer and prints details into the message buffer; there you can see which strings are guessed to be files, which match, and which are buttonized.
(defun buttonize-buffer () "Turn all file paths and URLs into buttons." (interactive) (require 'ffap) (deactivate-mark) (let (token guess beg end reached bound len) (save-excursion (setq reached (point-min)) (goto-char (point-min)) (while (re-search-forward ffap-next-regexp nil t) ;; There seems to be a bug in ffap, Emacs 23.3.1: `ffap-file-at-point' ;; enters endless loop when the string at point is "//". (setq token (ffap-string-at-point)) (unless (string= "//" (substring token 0 2)) ;; Note that `ffap-next-regexp' only finds some "indicator string" for a ;; file or URL. `ffap-string-at-point' blows this up into a token. (save-excursion (beginning-of-line) (when (search-forward token (point-at-eol) t) (setq beg (match-beginning 0) end (match-end 0) reached end)) ) (message "Found token %s at (%d-%d)" token beg (- end 1)) ;; Now let `ffap-guesser' identify the actual file path or URL at ;; point. (when (setq guess (ffap-guesser)) (message " Guessing %s" guess) (save-excursion (beginning-of-line) (when (search-forward guess (point-at-eol) t) (setq len (length guess) end (point) beg (- end len)) ;; Finally we found something worth buttonizing. It shall have ;; at least 2 chars, however. (message " Matched at (%d-%d]" beg (- end 1)) (unless (or (< (length guess) 2)) (message " Buttonize!") (make-button beg end :type 'find-file-button)) ) ) ) ;; Continue one character after the guess, or the original token. (goto-char (max reached end)) (message "Continuing at %d" (point)) ) ) ) ) )
To permanently install the function:
(add-hook 'find-file-hook 'buttonize-buffer)
A nicer solution is to "lazily buttonize" buffers:
(defun buttonize-current-buffer-on-idle (&optional secs) "Idle-timer (see \\[run-with-idle-timer]) that buttonizes filenames and URLs. SECS defaults to 60 seconds idle time." (interactive) (run-with-idle-timer (or secs 60) t 'buttonize-buffer)) (add-hook 'after-init-hook 'buttonize-current-buffer-on-idle)