Highlight which file names are missing in the emacs buffer

In GNU Emacs, I could use something like the hypothetical "flyexist.el" - I have a buffer with absolute (Unix) file names in it (plus some additional text around them). Most of these files exist, but some are missing. I would like to run a function that gives me the missing files (possibly with a red overlay). This function would have to figure out which of the text in the buffer looks like a file name (some false positives are in order), and then work with it using the exist-p file.

For example, suppose my buffer contains

Some random text mentioning /file/that/does/exist.txt, some more random text, and a /file/that/does/not-exist.txt 

I want to highlight the second file.

Already have something like that?

+4
source share
2 answers

I'm new to emacs hacking ... Here is my version with a small mode.

 (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))) 
+6
source

Try this (you have to start it manually, or include it in some other periodic routine):

 (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")))))) 

Play a little with the regular expression of the file name to understand how you want.

+2
source

Source: https://habr.com/ru/post/1415923/


All Articles