Skip to content

Instantly share code, notes, and snippets.

@cmarqu
Last active December 22, 2015 15:52
Show Gist options
  • Save cmarqu/8a915d6913d732e9544d to your computer and use it in GitHub Desktop.
Save cmarqu/8a915d6913d732e9544d to your computer and use it in GitHub Desktop.
;;; http://stackoverflow.com/questions/10879133/highlight-which-of-the-file-names-in-emacs-buffer-are-missing
(defvar filehi-path-re "\\([[:alnum:]/$_.-]+/[[:alnum:]$_.\\*/-]+\\)"
"Regexp used for path matching.")
(defface filehi-file-existing
'((t (:background "lawn green")))
"Face for existing files.")
(defface filehi-file-missing
'((t (:background "tomato")))
"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 highlight 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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment