在GNU Emacs中,我可以使用类似假设的“flyexist.el” - 我有一个带有绝对(Unix)文件名的缓冲区(加上一些附加文本)。大多数这些文件存在,但有些文件丢失了。我想运行一个功能,突出显示我丢失的文件(可能有红色叠加)。这个函数需要弄清楚缓冲区中的哪个文本看起来像文件名(一些误报是可以的),然后使用file-exists-p进行处理。
例如,假设我的缓冲区包含
Some random text mentioning /file/that/does/exist.txt,
some more random text, and a /file/that/does/not-exist.txt
我希望突出显示第二个文件。
这样的事情是否存在?
答案 0 :(得分:6)
我是emacs hacking的新手......这是我的“次要模式”版本。
(defvar filehi-path-re "\\([/$][[:alnum:]$-_.]+\\)+"
"Regexp used for path matching.")
(defface filehi-file-existing
'((t (:foreground "White" :background "Green")))
"Face for existing files.")
(defface filehi-file-missing
'((t (:foreground "Yellow" :background "Red")))
"Face for missing files.")
(defun filehi-check-and-highlight (start end)
"Check if substring is existing file path and highlight it."
(remove-overlays start end 'name 'filehi-highlight)
(let ((overlay (make-overlay start end)))
(overlay-put overlay 'name 'filehi-highlight)
(overlay-put overlay 'face (if (file-exists-p (substitute-in-file-name
(buffer-substring start end)))
'filehi-file-existing
'filehi-file-missing))))
(defun filehi-highlight-file-paths (&optional start end _ignore)
"Run through the buffer and highliht file paths."
(save-excursion
(save-match-data ; fixes problem with dabbrev (and may be more...)
(remove-overlays (point-min) end 'name 'filehi-highlight)
(let ((prev-end (point-min)))
(goto-char (point-min)) ; FIXME use something like greedy
; search-backward
(while (and (<= (point) end)
(re-search-forward filehi-path-re nil t))
(filehi-check-and-highlight (match-beginning 0) (match-end 0)))))))
(define-minor-mode filehi-mode
"Minor mode for highlighting existing file paths.
May conflict with other modes..."
nil " Filehi" nil
(if filehi-mode
(progn ; enable mode
(make-local-hook 'after-change-functions)
(filehi-highlight-file-paths (point-min) (point-max))
(add-hook 'after-change-functions 'filehi-highlight-file-paths nil t))
; disable mode
(remove-hook 'after-change-functions 'filehi-highlight-file-paths t)
(remove-overlays (point-min) (point-max) 'name 'filehi-highlight)))
答案 1 :(得分:2)
尝试使用此功能(您必须手动触发它,或将其合并到其他某些定期例程中):
(defun x-mark-missing-files ()
(interactive)
(save-excursion
(while (search-forward-regexp "~?/[A-Za-z./-]+")
(when (not (file-exists-p (match-string 0)))
(overlay-put
(make-overlay (match-beginning 0) (match-end 0))
'face '(:background "red"))))))
使用文件名regexp播放一下,以正确的方式使用它。