突出显示emacs缓冲区中缺少的文件名

突出显示emacs缓冲区中缺少的文件名,emacs,overlay,elisp,highlighting,Emacs,Overlay,Elisp,Highlighting,在GNU Emacs中,我可以使用类似于假设的“flyexist.el”的东西—我有一个带有绝对(Unix)文件名的缓冲区(加上它们周围的一些附加文本)。这些文件大部分都存在,但有些丢失了。我想运行一个函数,突出显示丢失的文件(可能带有红色覆盖)。这个函数需要找出缓冲区中哪个文本看起来像文件名(一些误报是可以的),然后使用file-exists-p处理它 例如,假设我的缓冲区包含 Some random text mentioning /file/that/does/exist.txt, so

在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
我想突出显示第二个文件

像这样的东西已经存在了吗?

试试这个(你必须手动触发它,或者将它合并到其他定期程序中):


对文件名regexp进行一点修改,使其符合您的要求。

我是emacs黑客新手。。。这是我的“次要模式”版本

(defvar filehi path re“\\([/$][[:alnum::$-\.]+\\)+”
“用于路径匹配的Regexp。”)
(defface filehi文件已存在)
((t(:前景“白色”:背景“绿色”))
“现有文件的面。”)
(defface filehi文件丢失
((t(:前景“黄色”:背景“红色”))
“缺少文件的面。”)
(取消文件HI检查并高亮显示(开始-结束)
“检查子字符串是否为现有文件路径并高亮显示。”
(删除重叠开始结束“名称”文件HI高亮显示)
(let((覆盖(使覆盖开始结束)))
(覆盖放置覆盖“名称”文件HI高亮显示)
(覆盖层放置覆盖层的面(如果(文件-exists-p)(替换为文件名
(缓冲区子字符串开始-结束)))
'filehi文件已存在
'filehi文件丢失])
(defun filehi高亮显示文件路径(&可选开始-结束\u忽略)
“运行缓冲区和highliht文件路径。”
(省去远足
(保存匹配数据;修复dabbrev的问题(可能更多…)
(删除重叠(最小点)结束“名称”文件HI高亮显示)
(让((上一个端点(最小点)))
(goto char(point min));使用贪婪
;向后搜索

(虽然(和)(并且您需要代码来确定
/file/that/does/exist.txt
存在,即使
/file/that/does/exist.txt,
(假定)不存在?如果存在,也不突出显示
/file/that/does/not exist.tx
?如果存在文件,则不应突出显示该文件(或者以其他颜色,例如绿色),但我想快速查看不存在的文件。@cmarqu:+1…我也会发现这非常方便。解决/path/to/file和~/path/to/file并以红色突出显示缺失的文件将非常有用:)谢谢,这太棒了。因为您的解决方案非常有用,而且是最完整的,所以我“接受”将其作为答案。您是否考虑在github中托管该代码,并使用适当的许可证等?扩展环境变量很容易-只需添加一个函数调用(请参见diff)并更改regexp。Elisp引用是您的朋友!至于托管和许可,您可以随意使用该代码。我不想维护它:)此后,我对regexp进行了一些修改,使其也支持本地文件名—它希望找到至少一个斜杠(/)。
(defvar filehi path re“\\”([[:alnum:$\.-]+/[:alnum:::$\.\*/-]+\\)“用于路径匹配的regexp。”)
谢谢,我在regexp中添加了0-9和“\\”,效果也很好。
(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"))))))
(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)))