如何让 emacs 突出显示和链接文件路径

Gre*_*ory 5 unix emacs elisp

是否有一些 emacs lisp 代码会自动在缓冲区中查找 /nfs 文件路径并突出显示/链接到它们?所以点击它们会打开那个文件?

示例路径:/nfs/foo/bar/file.txt

Tre*_*son 7

可能已经有一个包可以做到这一点,但我不知道。

这段代码将按钮添加到它认为看起来像文件路径的文本中。您可以添加功能'buttonize-buffer,以find-file-hooks或手动(或有条件)运行它。

(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
Run Code Online (Sandbox Code Playgroud)

  • 太棒了。我做了一些更多的字符来避免......重新搜索转发 "/nfs[^ \t\n,\'\"]*" ......我可能会发现更多,但添加起来很容易。 (2认同)

小智 5

尝试内置包 ffap(在点查找文件):

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

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

我不使用次要模式,而是将一个键绑定到ffap我在文件名上击中的键。

  • 这也可能工作,但有一个额外的步骤将指针移动到路径的位置并点击一个键。Trey 的答案是鼠标驱动并且非常高效。 (2认同)