hi-lock.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. ;;; hi-lock.el --- minor mode for interactive automatic highlighting
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: David M. Koppelman <koppel@ece.lsu.edu>
  4. ;; Keywords: faces, minor-mode, matching, display
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; With the hi-lock commands text matching interactively entered
  19. ;; regexp's can be highlighted. For example, `M-x highlight-regexp
  20. ;; RET clearly RET RET' will highlight all occurrences of `clearly'
  21. ;; using a yellow background face. New occurrences of `clearly' will
  22. ;; be highlighted as they are typed. `M-x unhighlight-regexp RET'
  23. ;; will remove the highlighting. Any existing face can be used for
  24. ;; highlighting and a set of appropriate faces is provided. The
  25. ;; regexps can be written into the current buffer in a form that will
  26. ;; be recognized the next time the corresponding file is read (when
  27. ;; file patterns is turned on).
  28. ;;
  29. ;; Applications:
  30. ;;
  31. ;; In program source code highlight a variable to quickly see all
  32. ;; places it is modified or referenced:
  33. ;; M-x highlight-regexp ground_contact_switches_closed RET RET
  34. ;;
  35. ;; In a shell or other buffer that is showing lots of program
  36. ;; output, highlight the parts of the output you're interested in:
  37. ;; M-x highlight-regexp Total execution time [0-9]+ RET hi-blue-b RET
  38. ;;
  39. ;; In buffers displaying tables, highlight the lines you're interested in:
  40. ;; M-x highlight-lines-matching-regexp January 2000 RET hi-black-b RET
  41. ;;
  42. ;; When writing text, highlight personal cliches. This can be
  43. ;; amusing.
  44. ;; M-x highlight-phrase as can be seen RET RET
  45. ;;
  46. ;; Setup:
  47. ;;
  48. ;; Put the following code in your .emacs file. This turns on
  49. ;; hi-lock mode and adds a "Regexp Highlighting" entry
  50. ;; to the edit menu.
  51. ;;
  52. ;; (global-hi-lock-mode 1)
  53. ;;
  54. ;; To enable the use of patterns found in files (presumably placed
  55. ;; there by hi-lock) include the following in your .emacs file:
  56. ;;
  57. ;; (setq hi-lock-file-patterns-policy 'ask)
  58. ;;
  59. ;; If you get tired of being asked each time a file is loaded replace
  60. ;; `ask' with a function that returns t if patterns should be read.
  61. ;;
  62. ;; You might also want to bind the hi-lock commands to more
  63. ;; finger-friendly sequences:
  64. ;; (define-key hi-lock-map "\C-z\C-h" 'highlight-lines-matching-regexp)
  65. ;; (define-key hi-lock-map "\C-zi" 'hi-lock-find-patterns)
  66. ;; (define-key hi-lock-map "\C-zh" 'highlight-regexp)
  67. ;; (define-key hi-lock-map "\C-zp" 'highlight-phrase)
  68. ;; (define-key hi-lock-map "\C-zr" 'unhighlight-regexp)
  69. ;; (define-key hi-lock-map "\C-zb" 'hi-lock-write-interactive-patterns))
  70. ;; See the documentation for hi-lock-mode `C-h f hi-lock-mode' for
  71. ;; additional instructions.
  72. ;; Sample file patterns:
  73. ; Hi-lock: (("^;;; .*" (0 (quote hi-black-hb) t)))
  74. ; Hi-lock: ( ("make-variable-buffer-\\(local\\)" (0 font-lock-keyword-face)(1 'italic append)))))
  75. ; Hi-lock: end
  76. ;;; Code:
  77. (require 'font-lock)
  78. (defgroup hi-lock nil
  79. "Interactively add and remove font-lock patterns for highlighting text."
  80. :link '(custom-manual "(emacs)Highlight Interactively")
  81. :group 'font-lock)
  82. (defcustom hi-lock-file-patterns-range 10000
  83. "Limit of search in a buffer for hi-lock patterns.
  84. When a file is visited and hi-lock mode is on, patterns starting
  85. up to this limit are added to font-lock's patterns. See documentation
  86. of functions `hi-lock-mode' and `hi-lock-find-patterns'."
  87. :type 'integer
  88. :group 'hi-lock)
  89. (defcustom hi-lock-highlight-range 200000
  90. "Size of area highlighted by hi-lock when font-lock not active.
  91. Font-lock is not active in buffers that do their own highlighting,
  92. such as the buffer created by `list-colors-display'. In those buffers
  93. hi-lock patterns will only be applied over a range of
  94. `hi-lock-highlight-range' characters. If font-lock is active then
  95. highlighting will be applied throughout the buffer."
  96. :type 'integer
  97. :group 'hi-lock)
  98. (defcustom hi-lock-exclude-modes
  99. '(rmail-mode mime/viewer-mode gnus-article-mode)
  100. "List of major modes in which hi-lock will not run.
  101. For security reasons since font lock patterns can specify function
  102. calls."
  103. :type '(repeat symbol)
  104. :group 'hi-lock)
  105. (defcustom hi-lock-file-patterns-policy 'ask
  106. "Specify when hi-lock should use patterns found in file.
  107. If `ask', prompt when patterns found in buffer; if bound to a function,
  108. use patterns when function returns t (function is called with patterns
  109. as first argument); if nil or `never' or anything else, don't use file
  110. patterns."
  111. :type '(choice (const :tag "Do not use file patterns" never)
  112. (const :tag "Ask about file patterns" ask)
  113. (function :tag "Function to check file patterns"))
  114. :group 'hi-lock
  115. :version "22.1")
  116. ;; It can have a function value.
  117. (put 'hi-lock-file-patterns-policy 'risky-local-variable t)
  118. (defgroup hi-lock-faces nil
  119. "Faces for hi-lock."
  120. :group 'hi-lock
  121. :group 'faces)
  122. (defface hi-yellow
  123. '((((min-colors 88) (background dark))
  124. (:background "yellow1" :foreground "black"))
  125. (((background dark)) (:background "yellow" :foreground "black"))
  126. (((min-colors 88)) (:background "yellow1"))
  127. (t (:background "yellow")))
  128. "Default face for hi-lock mode."
  129. :group 'hi-lock-faces)
  130. (defface hi-pink
  131. '((((background dark)) (:background "pink" :foreground "black"))
  132. (t (:background "pink")))
  133. "Face for hi-lock mode."
  134. :group 'hi-lock-faces)
  135. (defface hi-green
  136. '((((min-colors 88) (background dark))
  137. (:background "green1" :foreground "black"))
  138. (((background dark)) (:background "green" :foreground "black"))
  139. (((min-colors 88)) (:background "green1"))
  140. (t (:background "green")))
  141. "Face for hi-lock mode."
  142. :group 'hi-lock-faces)
  143. (defface hi-blue
  144. '((((background dark)) (:background "light blue" :foreground "black"))
  145. (t (:background "light blue")))
  146. "Face for hi-lock mode."
  147. :group 'hi-lock-faces)
  148. (defface hi-black-b
  149. '((t (:weight bold)))
  150. "Face for hi-lock mode."
  151. :group 'hi-lock-faces)
  152. (defface hi-blue-b
  153. '((((min-colors 88)) (:weight bold :foreground "blue1"))
  154. (t (:weight bold :foreground "blue")))
  155. "Face for hi-lock mode."
  156. :group 'hi-lock-faces)
  157. (defface hi-green-b
  158. '((((min-colors 88)) (:weight bold :foreground "green1"))
  159. (t (:weight bold :foreground "green")))
  160. "Face for hi-lock mode."
  161. :group 'hi-lock-faces)
  162. (defface hi-red-b
  163. '((((min-colors 88)) (:weight bold :foreground "red1"))
  164. (t (:weight bold :foreground "red")))
  165. "Face for hi-lock mode."
  166. :group 'hi-lock-faces)
  167. (defface hi-black-hb
  168. '((t (:weight bold :height 1.67 :inherit variable-pitch)))
  169. "Face for hi-lock mode."
  170. :group 'hi-lock-faces)
  171. (defvar hi-lock-file-patterns nil
  172. "Patterns found in file for hi-lock. Should not be changed.")
  173. (defvar hi-lock-interactive-patterns nil
  174. "Patterns provided to hi-lock by user. Should not be changed.")
  175. (defvar hi-lock-face-defaults
  176. '("hi-yellow" "hi-pink" "hi-green" "hi-blue" "hi-black-b"
  177. "hi-blue-b" "hi-red-b" "hi-green-b" "hi-black-hb")
  178. "Default faces for hi-lock interactive functions.")
  179. ;(dolist (f hi-lock-face-defaults) (unless (facep f) (error "%s not a face" f)))
  180. (define-obsolete-variable-alias 'hi-lock-face-history
  181. 'hi-lock-face-defaults
  182. "23.1")
  183. (define-obsolete-variable-alias 'hi-lock-regexp-history
  184. 'regexp-history
  185. "23.1")
  186. (defvar hi-lock-file-patterns-prefix "Hi-lock"
  187. "Search target for finding hi-lock patterns at top of file.")
  188. (defvar hi-lock-archaic-interface-message-used nil
  189. "True if user alerted that `global-hi-lock-mode' is now the global switch.
  190. Earlier versions of hi-lock used `hi-lock-mode' as the global switch;
  191. the message is issued if it appears that `hi-lock-mode' is used assuming
  192. that older functionality. This variable avoids multiple reminders.")
  193. (defvar hi-lock-archaic-interface-deduce nil
  194. "If non-nil, sometimes assume that `hi-lock-mode' means `global-hi-lock-mode'.
  195. Assumption is made if `hi-lock-mode' used in the *scratch* buffer while
  196. a library is being loaded.")
  197. (make-variable-buffer-local 'hi-lock-interactive-patterns)
  198. (put 'hi-lock-interactive-patterns 'permanent-local t)
  199. (make-variable-buffer-local 'hi-lock-file-patterns)
  200. (put 'hi-lock-file-patterns 'permanent-local t)
  201. (defvar hi-lock-menu
  202. (let ((map (make-sparse-keymap "Hi Lock")))
  203. (define-key-after map [highlight-regexp]
  204. '(menu-item "Highlight Regexp..." highlight-regexp
  205. :help "Highlight text matching PATTERN (a regexp)."))
  206. (define-key-after map [highlight-phrase]
  207. '(menu-item "Highlight Phrase..." highlight-phrase
  208. :help "Highlight text matching PATTERN (a regexp processed to match phrases)."))
  209. (define-key-after map [highlight-lines-matching-regexp]
  210. '(menu-item "Highlight Lines..." highlight-lines-matching-regexp
  211. :help "Highlight lines containing match of PATTERN (a regexp)."))
  212. (define-key-after map [unhighlight-regexp]
  213. '(menu-item "Remove Highlighting..." unhighlight-regexp
  214. :help "Remove previously entered highlighting pattern."
  215. :enable hi-lock-interactive-patterns))
  216. (define-key-after map [hi-lock-write-interactive-patterns]
  217. '(menu-item "Patterns to Buffer" hi-lock-write-interactive-patterns
  218. :help "Insert interactively added REGEXPs into buffer at point."
  219. :enable hi-lock-interactive-patterns))
  220. (define-key-after map [hi-lock-find-patterns]
  221. '(menu-item "Patterns from Buffer" hi-lock-find-patterns
  222. :help "Use patterns (if any) near top of buffer."))
  223. map)
  224. "Menu for hi-lock mode.")
  225. (defvar hi-lock-map
  226. (let ((map (make-sparse-keymap "Hi Lock")))
  227. (define-key map "\C-xwi" 'hi-lock-find-patterns)
  228. (define-key map "\C-xwl" 'highlight-lines-matching-regexp)
  229. (define-key map "\C-xwp" 'highlight-phrase)
  230. (define-key map "\C-xwh" 'highlight-regexp)
  231. (define-key map "\C-xwr" 'unhighlight-regexp)
  232. (define-key map "\C-xwb" 'hi-lock-write-interactive-patterns)
  233. map)
  234. "Key map for hi-lock.")
  235. ;; Visible Functions
  236. ;;;###autoload
  237. (define-minor-mode hi-lock-mode
  238. "Toggle selective highlighting of patterns (Hi Lock mode).
  239. With a prefix argument ARG, enable Hi Lock mode if ARG is
  240. positive, and disable it otherwise. If called from Lisp, enable
  241. the mode if ARG is omitted or nil.
  242. Issuing one the highlighting commands listed below will
  243. automatically enable Hi Lock mode. To enable Hi Lock mode in all
  244. buffers, use `global-hi-lock-mode' or add (global-hi-lock-mode 1)
  245. to your init file. When Hi Lock mode is enabled, a \"Regexp
  246. Highlighting\" submenu is added to the \"Edit\" menu. The
  247. commands in the submenu, which can be called interactively, are:
  248. \\[highlight-regexp] REGEXP FACE
  249. Highlight matches of pattern REGEXP in current buffer with FACE.
  250. \\[highlight-phrase] PHRASE FACE
  251. Highlight matches of phrase PHRASE in current buffer with FACE.
  252. (PHRASE can be any REGEXP, but spaces will be replaced by matches
  253. to whitespace and initial lower-case letters will become case insensitive.)
  254. \\[highlight-lines-matching-regexp] REGEXP FACE
  255. Highlight lines containing matches of REGEXP in current buffer with FACE.
  256. \\[unhighlight-regexp] REGEXP
  257. Remove highlighting on matches of REGEXP in current buffer.
  258. \\[hi-lock-write-interactive-patterns]
  259. Write active REGEXPs into buffer as comments (if possible). They may
  260. be read the next time file is loaded or when the \\[hi-lock-find-patterns] command
  261. is issued. The inserted regexps are in the form of font lock keywords.
  262. (See `font-lock-keywords'.) They may be edited and re-loaded with \\[hi-lock-find-patterns],
  263. any valid `font-lock-keywords' form is acceptable. When a file is
  264. loaded the patterns are read if `hi-lock-file-patterns-policy' is
  265. 'ask and the user responds y to the prompt, or if
  266. `hi-lock-file-patterns-policy' is bound to a function and that
  267. function returns t.
  268. \\[hi-lock-find-patterns]
  269. Re-read patterns stored in buffer (in the format produced by \\[hi-lock-write-interactive-patterns]).
  270. When hi-lock is started and if the mode is not excluded or patterns
  271. rejected, the beginning of the buffer is searched for lines of the
  272. form:
  273. Hi-lock: FOO
  274. where FOO is a list of patterns. These are added to the font lock
  275. keywords already present. The patterns must start before position
  276. \(number of characters into buffer) `hi-lock-file-patterns-range'.
  277. Patterns will be read until
  278. Hi-lock: end
  279. is found. A mode is excluded if it's in the list `hi-lock-exclude-modes'."
  280. :group 'hi-lock
  281. :lighter (:eval (if (or hi-lock-interactive-patterns
  282. hi-lock-file-patterns)
  283. " Hi" ""))
  284. :global nil
  285. :keymap hi-lock-map
  286. (when (and (equal (buffer-name) "*scratch*")
  287. load-in-progress
  288. (not (called-interactively-p 'interactive))
  289. (not hi-lock-archaic-interface-message-used))
  290. (setq hi-lock-archaic-interface-message-used t)
  291. (if hi-lock-archaic-interface-deduce
  292. (global-hi-lock-mode hi-lock-mode)
  293. (warn
  294. "Possible archaic use of (hi-lock-mode).
  295. Use (global-hi-lock-mode 1) in .emacs to enable hi-lock for all buffers,
  296. use (hi-lock-mode 1) for individual buffers. For compatibility with Emacs
  297. versions before 22 use the following in your .emacs file:
  298. (if (functionp 'global-hi-lock-mode)
  299. (global-hi-lock-mode 1)
  300. (hi-lock-mode 1))
  301. ")))
  302. (if hi-lock-mode
  303. ;; Turned on.
  304. (progn
  305. (unless font-lock-mode (font-lock-mode 1))
  306. (define-key-after menu-bar-edit-menu [hi-lock]
  307. (cons "Regexp Highlighting" hi-lock-menu))
  308. (hi-lock-find-patterns)
  309. (add-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook nil t))
  310. ;; Turned off.
  311. (when (or hi-lock-interactive-patterns
  312. hi-lock-file-patterns)
  313. (when hi-lock-interactive-patterns
  314. (font-lock-remove-keywords nil hi-lock-interactive-patterns)
  315. (setq hi-lock-interactive-patterns nil))
  316. (when hi-lock-file-patterns
  317. (font-lock-remove-keywords nil hi-lock-file-patterns)
  318. (setq hi-lock-file-patterns nil))
  319. (remove-overlays nil nil 'hi-lock-overlay t)
  320. (when font-lock-fontified (font-lock-fontify-buffer)))
  321. (define-key-after menu-bar-edit-menu [hi-lock] nil)
  322. (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook t)))
  323. ;;;###autoload
  324. (define-globalized-minor-mode global-hi-lock-mode
  325. hi-lock-mode turn-on-hi-lock-if-enabled
  326. :group 'hi-lock)
  327. (defun turn-on-hi-lock-if-enabled ()
  328. (setq hi-lock-archaic-interface-message-used t)
  329. (unless (memq major-mode hi-lock-exclude-modes)
  330. (hi-lock-mode 1)))
  331. ;;;###autoload
  332. (defalias 'highlight-lines-matching-regexp 'hi-lock-line-face-buffer)
  333. ;;;###autoload
  334. (defun hi-lock-line-face-buffer (regexp &optional face)
  335. "Set face of all lines containing a match of REGEXP to FACE.
  336. Interactively, prompt for REGEXP then FACE. Buffer-local history
  337. list maintained for regexps, global history maintained for faces.
  338. \\<minibuffer-local-map>Use \\[previous-history-element] to retrieve previous history items,
  339. and \\[next-history-element] to retrieve default values.
  340. \(See info node `Minibuffer History'.)"
  341. (interactive
  342. (list
  343. (hi-lock-regexp-okay
  344. (read-regexp "Regexp to highlight line" (car regexp-history)))
  345. (hi-lock-read-face-name)))
  346. (or (facep face) (setq face 'hi-yellow))
  347. (unless hi-lock-mode (hi-lock-mode 1))
  348. (hi-lock-set-pattern
  349. ;; The \\(?:...\\) grouping construct ensures that a leading ^, +, * or ?
  350. ;; or a trailing $ in REGEXP will be interpreted correctly.
  351. (concat "^.*\\(?:" regexp "\\).*$") face))
  352. ;;;###autoload
  353. (defalias 'highlight-regexp 'hi-lock-face-buffer)
  354. ;;;###autoload
  355. (defun hi-lock-face-buffer (regexp &optional face)
  356. "Set face of each match of REGEXP to FACE.
  357. Interactively, prompt for REGEXP then FACE. Buffer-local history
  358. list maintained for regexps, global history maintained for faces.
  359. \\<minibuffer-local-map>Use \\[previous-history-element] to retrieve previous history items,
  360. and \\[next-history-element] to retrieve default values.
  361. \(See info node `Minibuffer History'.)"
  362. (interactive
  363. (list
  364. (hi-lock-regexp-okay
  365. (read-regexp "Regexp to highlight" (car regexp-history)))
  366. (hi-lock-read-face-name)))
  367. (or (facep face) (setq face 'hi-yellow))
  368. (unless hi-lock-mode (hi-lock-mode 1))
  369. (hi-lock-set-pattern regexp face))
  370. ;;;###autoload
  371. (defalias 'highlight-phrase 'hi-lock-face-phrase-buffer)
  372. ;;;###autoload
  373. (defun hi-lock-face-phrase-buffer (regexp &optional face)
  374. "Set face of each match of phrase REGEXP to FACE.
  375. Whitespace in REGEXP converted to arbitrary whitespace and initial
  376. lower-case letters made case insensitive."
  377. (interactive
  378. (list
  379. (hi-lock-regexp-okay
  380. (hi-lock-process-phrase
  381. (read-regexp "Phrase to highlight" (car regexp-history))))
  382. (hi-lock-read-face-name)))
  383. (or (facep face) (setq face 'hi-yellow))
  384. (unless hi-lock-mode (hi-lock-mode 1))
  385. (hi-lock-set-pattern regexp face))
  386. (declare-function x-popup-menu "menu.c" (position menu))
  387. ;;;###autoload
  388. (defalias 'unhighlight-regexp 'hi-lock-unface-buffer)
  389. ;;;###autoload
  390. (defun hi-lock-unface-buffer (regexp)
  391. "Remove highlighting of each match to REGEXP set by hi-lock.
  392. Interactively, prompt for REGEXP. Buffer-local history of inserted
  393. regexp's maintained. Will accept only regexps inserted by hi-lock
  394. interactive functions. \(See `hi-lock-interactive-patterns'.\)
  395. \\<minibuffer-local-must-match-map>Use \\[minibuffer-complete] to complete a partially typed regexp.
  396. \(See info node `Minibuffer History'.\)"
  397. (interactive
  398. (if (and (display-popup-menus-p)
  399. (listp last-nonmenu-event)
  400. use-dialog-box)
  401. (catch 'snafu
  402. (or
  403. (x-popup-menu
  404. t
  405. (cons
  406. `keymap
  407. (cons "Select Pattern to Unhighlight"
  408. (mapcar (lambda (pattern)
  409. (list (car pattern)
  410. (format
  411. "%s (%s)" (car pattern)
  412. (symbol-name
  413. (car
  414. (cdr (car (cdr (car (cdr pattern))))))))
  415. (cons nil nil)
  416. (car pattern)))
  417. hi-lock-interactive-patterns))))
  418. ;; If the user clicks outside the menu, meaning that they
  419. ;; change their mind, x-popup-menu returns nil, and
  420. ;; interactive signals a wrong number of arguments error.
  421. ;; To prevent that, we return an empty string, which will
  422. ;; effectively disable the rest of the function.
  423. (throw 'snafu '(""))))
  424. (let ((history-list (mapcar (lambda (p) (car p))
  425. hi-lock-interactive-patterns)))
  426. (unless hi-lock-interactive-patterns
  427. (error "No highlighting to remove"))
  428. (list
  429. (completing-read "Regexp to unhighlight: "
  430. hi-lock-interactive-patterns nil t
  431. (car (car hi-lock-interactive-patterns))
  432. (cons 'history-list 1))))))
  433. (let ((keyword (assoc regexp hi-lock-interactive-patterns)))
  434. (when keyword
  435. (font-lock-remove-keywords nil (list keyword))
  436. (setq hi-lock-interactive-patterns
  437. (delq keyword hi-lock-interactive-patterns))
  438. (remove-overlays
  439. nil nil 'hi-lock-overlay-regexp (hi-lock-string-serialize regexp))
  440. (when font-lock-fontified (font-lock-fontify-buffer)))))
  441. ;;;###autoload
  442. (defun hi-lock-write-interactive-patterns ()
  443. "Write interactively added patterns, if any, into buffer at point.
  444. Interactively added patterns are those normally specified using
  445. `highlight-regexp' and `highlight-lines-matching-regexp'; they can
  446. be found in variable `hi-lock-interactive-patterns'."
  447. (interactive)
  448. (if (null hi-lock-interactive-patterns)
  449. (error "There are no interactive patterns"))
  450. (let ((beg (point)))
  451. (mapc
  452. (lambda (pattern)
  453. (insert (format "%s: (%s)\n"
  454. hi-lock-file-patterns-prefix
  455. (prin1-to-string pattern))))
  456. hi-lock-interactive-patterns)
  457. (comment-region beg (point)))
  458. (when (> (point) hi-lock-file-patterns-range)
  459. (warn "Inserted keywords not close enough to top of file")))
  460. ;; Implementation Functions
  461. (defun hi-lock-process-phrase (phrase)
  462. "Convert regexp PHRASE to a regexp that matches phrases.
  463. Blanks in PHRASE replaced by regexp that matches arbitrary whitespace
  464. and initial lower-case letters made case insensitive."
  465. (let ((mod-phrase nil))
  466. (setq mod-phrase
  467. (replace-regexp-in-string
  468. "\\<[a-z]" (lambda (m) (format "[%s%s]" (upcase m) m)) phrase))
  469. (setq mod-phrase
  470. (replace-regexp-in-string
  471. "\\s-+" "[ \t\n]+" mod-phrase nil t))))
  472. (defun hi-lock-regexp-okay (regexp)
  473. "Return REGEXP if it appears suitable for a font-lock pattern.
  474. Otherwise signal an error. A pattern that matches the null string is
  475. not suitable."
  476. (if (string-match regexp "")
  477. (error "Regexp cannot match an empty string")
  478. regexp))
  479. (defun hi-lock-read-face-name ()
  480. "Read face name from minibuffer with completion and history."
  481. (intern (completing-read
  482. "Highlight using face: "
  483. obarray 'facep t
  484. (cons (car hi-lock-face-defaults)
  485. (let ((prefix
  486. (try-completion
  487. (substring (car hi-lock-face-defaults) 0 1)
  488. hi-lock-face-defaults)))
  489. (if (and (stringp prefix)
  490. (not (equal prefix (car hi-lock-face-defaults))))
  491. (length prefix) 0)))
  492. 'face-name-history
  493. (cdr hi-lock-face-defaults))))
  494. (defun hi-lock-set-pattern (regexp face)
  495. "Highlight REGEXP with face FACE."
  496. (let ((pattern (list regexp (list 0 (list 'quote face) t))))
  497. (unless (member pattern hi-lock-interactive-patterns)
  498. (push pattern hi-lock-interactive-patterns)
  499. (if font-lock-fontified
  500. (progn
  501. (font-lock-add-keywords nil (list pattern) t)
  502. (font-lock-fontify-buffer))
  503. (let* ((serial (hi-lock-string-serialize regexp))
  504. (range-min (- (point) (/ hi-lock-highlight-range 2)))
  505. (range-max (+ (point) (/ hi-lock-highlight-range 2)))
  506. (search-start
  507. (max (point-min)
  508. (- range-min (max 0 (- range-max (point-max))))))
  509. (search-end
  510. (min (point-max)
  511. (+ range-max (max 0 (- (point-min) range-min))))))
  512. (save-excursion
  513. (goto-char search-start)
  514. (while (re-search-forward regexp search-end t)
  515. (let ((overlay (make-overlay (match-beginning 0) (match-end 0))))
  516. (overlay-put overlay 'hi-lock-overlay t)
  517. (overlay-put overlay 'hi-lock-overlay-regexp serial)
  518. (overlay-put overlay 'face face))
  519. (goto-char (match-end 0)))))))))
  520. (defun hi-lock-set-file-patterns (patterns)
  521. "Replace file patterns list with PATTERNS and refontify."
  522. (when (or hi-lock-file-patterns patterns)
  523. (font-lock-remove-keywords nil hi-lock-file-patterns)
  524. (setq hi-lock-file-patterns patterns)
  525. (font-lock-add-keywords nil hi-lock-file-patterns t)
  526. (font-lock-fontify-buffer)))
  527. (defun hi-lock-find-patterns ()
  528. "Find patterns in current buffer for hi-lock."
  529. (interactive)
  530. (unless (memq major-mode hi-lock-exclude-modes)
  531. (let ((all-patterns nil)
  532. (target-regexp (concat "\\<" hi-lock-file-patterns-prefix ":")))
  533. (save-excursion
  534. (save-restriction
  535. (widen)
  536. (goto-char (point-min))
  537. (re-search-forward target-regexp
  538. (+ (point) hi-lock-file-patterns-range) t)
  539. (beginning-of-line)
  540. (while (and (re-search-forward target-regexp (+ (point) 100) t)
  541. (not (looking-at "\\s-*end")))
  542. (condition-case nil
  543. (setq all-patterns (append (read (current-buffer)) all-patterns))
  544. (error (message "Invalid pattern list expression at %d"
  545. (line-number-at-pos)))))))
  546. (when (and all-patterns
  547. hi-lock-mode
  548. (cond
  549. ((eq this-command 'hi-lock-find-patterns) t)
  550. ((functionp hi-lock-file-patterns-policy)
  551. (funcall hi-lock-file-patterns-policy all-patterns))
  552. ((eq hi-lock-file-patterns-policy 'ask)
  553. (y-or-n-p "Add patterns from this buffer to hi-lock? "))
  554. (t nil)))
  555. (hi-lock-set-file-patterns all-patterns)
  556. (if (called-interactively-p 'interactive)
  557. (message "Hi-lock added %d patterns." (length all-patterns)))))))
  558. (defun hi-lock-font-lock-hook ()
  559. "Add hi-lock patterns to font-lock's."
  560. (when font-lock-fontified
  561. (font-lock-add-keywords nil hi-lock-file-patterns t)
  562. (font-lock-add-keywords nil hi-lock-interactive-patterns t)))
  563. (defvar hi-lock-string-serialize-hash
  564. (make-hash-table :test 'equal)
  565. "Hash table used to assign unique numbers to strings.")
  566. (defvar hi-lock-string-serialize-serial 1
  567. "Number assigned to last new string in call to `hi-lock-string-serialize'.
  568. A string is considered new if it had not previously been used in a call to
  569. `hi-lock-string-serialize'.")
  570. (defun hi-lock-string-serialize (string)
  571. "Return unique serial number for STRING."
  572. (interactive)
  573. (let ((val (gethash string hi-lock-string-serialize-hash)))
  574. (if val val
  575. (puthash string
  576. (setq hi-lock-string-serialize-serial
  577. (1+ hi-lock-string-serialize-serial))
  578. hi-lock-string-serialize-hash)
  579. hi-lock-string-serialize-serial)))
  580. (defun hi-lock-unload-function ()
  581. "Unload the Hi-Lock library."
  582. (global-hi-lock-mode -1)
  583. ;; continue standard unloading
  584. nil)
  585. (provide 'hi-lock)
  586. ;;; hi-lock.el ends here