re-builder.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. ;;; re-builder.el --- building Regexps with visual feedback -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Detlev Zundel <dzu@gnu.org>
  4. ;; Keywords: matching, lisp, tools
  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. ;; When I have to come up with regular expressions that are more
  18. ;; complex than simple string matchers, especially if they contain sub
  19. ;; expressions, I find myself spending quite some time in the
  20. ;; `development cycle'. `re-builder' aims to shorten this time span
  21. ;; so I can get on with the more interesting bits.
  22. ;; With it you can have immediate visual feedback about how well the
  23. ;; regexp behaves to your expectations on the intended data.
  24. ;; When called up `re-builder' attaches itself to the current buffer
  25. ;; which becomes its target buffer, where all the matching is done.
  26. ;; The active window is split so you have a view on the data while
  27. ;; authoring the RE. If the edited expression is valid the matches in
  28. ;; the target buffer are marked automatically with colored overlays
  29. ;; (for non-color displays see below) giving you feedback over the
  30. ;; extents of the matched (sub) expressions. The (non-)validity is
  31. ;; shown only in the modeline without throwing the errors at you. If
  32. ;; you want to know the reason why RE Builder considers it as invalid
  33. ;; call `reb-force-update' ("\C-c\C-u") which should reveal the error.
  34. ;; The target buffer can be changed with `reb-change-target-buffer'
  35. ;; ("\C-c\C-b"). Changing the target buffer automatically removes
  36. ;; the overlays from the old buffer and displays the new one in the
  37. ;; target window.
  38. ;; The `re-builder' keeps the focus while updating the matches in the
  39. ;; target buffer so corrections are easy to incorporate. If you are
  40. ;; satisfied with the result you can paste the RE to the kill-ring
  41. ;; with `reb-copy' ("\C-c\C-w"), quit the `re-builder' ("\C-c\C-q")
  42. ;; and use it wherever you need it.
  43. ;; As the automatic updates can take some time on large buffers, they
  44. ;; can be limited by `reb-auto-match-limit' so that they should not
  45. ;; have a negative impact on the editing. Setting it to nil makes
  46. ;; even the auto updates go all the way. Forcing an update overrides
  47. ;; this limit allowing an easy way to see all matches.
  48. ;; Currently `re-builder' understands three different forms of input,
  49. ;; namely `read', `string', and `rx' syntax. Read
  50. ;; syntax and string syntax are both delimited by `"'s and behave
  51. ;; according to their name. With the `string' syntax there's no need
  52. ;; to escape the backslashes and double quotes simplifying the editing
  53. ;; somewhat. The other three allow editing of symbolic regular
  54. ;; expressions supported by the packages of the same name.
  55. ;; Editing symbolic expressions is done through a major mode derived
  56. ;; from `emacs-lisp-mode' so you'll get all the good stuff like
  57. ;; automatic indentation and font-locking etc.
  58. ;; When editing a symbolic regular expression, only the first
  59. ;; expression in the RE Builder buffer is considered, which helps
  60. ;; limiting the extent of the expression like the `"'s do for the text
  61. ;; modes. For the `rx' syntax the function `rx-to-string' is applied to
  62. ;; the evaluated expression read. So you can use quoted arguments
  63. ;; with something like '("findme") or you can construct arguments to
  64. ;; your hearts delight with a valid ELisp expression. (The compiled
  65. ;; string form will be copied by `reb-copy') If you want to take
  66. ;; a glance at the corresponding string you can temporarily change the
  67. ;; input syntax.
  68. ;; Changing the input syntax is transparent (for the obvious exception
  69. ;; non-symbolic -> symbolic) so you can change your mind as often as
  70. ;; you like.
  71. ;; There is also a shortcut function for toggling the
  72. ;; `case-fold-search' variable in the target buffer with an immediate
  73. ;; update.
  74. ;; Q: But what if my display cannot show colored overlays?
  75. ;; A: Then the cursor will flash around the matched text making it stand
  76. ;; out.
  77. ;; Q: But how can I then make out the sub-expressions?
  78. ;; A: Thats where the `sub-expression mode' comes in. In it only the
  79. ;; digit keys are assigned to perform an update that will flash the
  80. ;; corresponding subexp only.
  81. ;;; Code:
  82. ;; On XEmacs, load the overlay compatibility library
  83. (unless (fboundp 'make-overlay)
  84. (require 'overlay))
  85. ;; User customizable variables
  86. (defgroup re-builder nil
  87. "Options for the RE Builder."
  88. :group 'lisp
  89. :prefix "reb-")
  90. (defcustom reb-blink-delay 0.5
  91. "Seconds to blink cursor for next/previous match in RE Builder."
  92. :group 're-builder
  93. :type 'number)
  94. (defcustom reb-mode-hook nil
  95. "Hooks to run on entering RE Builder mode."
  96. :group 're-builder
  97. :type 'hook)
  98. (defcustom reb-re-syntax 'read
  99. "Syntax for the REs in the RE Builder.
  100. Can either be `read', `string', or `rx'."
  101. :group 're-builder
  102. :type '(choice (const :tag "Read syntax" read)
  103. (const :tag "String syntax" string)
  104. (const :tag "`rx' syntax" rx)))
  105. (defcustom reb-auto-match-limit 200
  106. "Positive integer limiting the matches for RE Builder auto updates.
  107. Set it to nil if you don't want limits here."
  108. :group 're-builder
  109. :type '(restricted-sexp :match-alternatives
  110. (integerp 'nil)))
  111. (defface reb-match-0
  112. '((((class color) (background light))
  113. :background "lightblue")
  114. (((class color) (background dark))
  115. :background "steelblue4")
  116. (t
  117. :inverse-video t))
  118. "Used for displaying the whole match."
  119. :group 're-builder)
  120. (defface reb-match-1
  121. '((((class color) (background light))
  122. :background "aquamarine")
  123. (((class color) (background dark))
  124. :background "blue3")
  125. (t
  126. :inverse-video t))
  127. "Used for displaying the first matching subexpression."
  128. :group 're-builder)
  129. (defface reb-match-2
  130. '((((class color) (background light))
  131. :background "springgreen")
  132. (((class color) (background dark))
  133. :background "chartreuse4")
  134. (t
  135. :inverse-video t))
  136. "Used for displaying the second matching subexpression."
  137. :group 're-builder)
  138. (defface reb-match-3
  139. '((((min-colors 88) (class color) (background light))
  140. :background "yellow1")
  141. (((class color) (background light))
  142. :background "yellow")
  143. (((class color) (background dark))
  144. :background "sienna4")
  145. (t
  146. :inverse-video t))
  147. "Used for displaying the third matching subexpression."
  148. :group 're-builder)
  149. ;; Internal variables below
  150. (defvar reb-mode nil
  151. "Enables the RE Builder minor mode.")
  152. (defvar reb-target-buffer nil
  153. "Buffer to which the RE is applied to.")
  154. (defvar reb-target-window nil
  155. "Window to which the RE is applied to.")
  156. (defvar reb-regexp nil
  157. "Last regexp used by RE Builder.")
  158. (defvar reb-regexp-src nil
  159. "Last regexp used by RE Builder before processing it.
  160. Except for Lisp syntax this is the same as `reb-regexp'.")
  161. (defvar reb-overlays nil
  162. "List of overlays of the RE Builder.")
  163. (defvar reb-window-config nil
  164. "Old window configuration.")
  165. (defvar reb-subexp-mode nil
  166. "Indicates whether sub-exp mode is active.")
  167. (defvar reb-subexp-displayed nil
  168. "Indicates which sub-exp is active.")
  169. (defvar reb-mode-string ""
  170. "String in mode line for additional info.")
  171. (defvar reb-valid-string ""
  172. "String in mode line showing validity of RE.")
  173. (make-variable-buffer-local 'reb-overlays)
  174. (make-variable-buffer-local 'reb-regexp)
  175. (make-variable-buffer-local 'reb-regexp-src)
  176. (defconst reb-buffer "*RE-Builder*"
  177. "Buffer to use for the RE Builder.")
  178. ;; Define the local "\C-c" keymap
  179. (defvar reb-mode-map
  180. (let ((map (make-sparse-keymap))
  181. (menu-map (make-sparse-keymap)))
  182. (define-key map "\C-c\C-c" 'reb-toggle-case)
  183. (define-key map "\C-c\C-q" 'reb-quit)
  184. (define-key map "\C-c\C-w" 'reb-copy)
  185. (define-key map "\C-c\C-s" 'reb-next-match)
  186. (define-key map "\C-c\C-r" 'reb-prev-match)
  187. (define-key map "\C-c\C-i" 'reb-change-syntax)
  188. (define-key map "\C-c\C-e" 'reb-enter-subexp-mode)
  189. (define-key map "\C-c\C-b" 'reb-change-target-buffer)
  190. (define-key map "\C-c\C-u" 'reb-force-update)
  191. (define-key map [menu-bar reb-mode] (cons "Re-Builder" menu-map))
  192. (define-key menu-map [rq]
  193. '(menu-item "Quit" reb-quit
  194. :help "Quit the RE Builder mode"))
  195. (define-key menu-map [rt]
  196. '(menu-item "Case sensitive" reb-toggle-case
  197. :button (:toggle . (with-current-buffer
  198. reb-target-buffer
  199. (null case-fold-search)))
  200. :help "Toggle case sensitivity of searches for RE Builder target buffer"))
  201. (define-key menu-map [rb]
  202. '(menu-item "Change target buffer..." reb-change-target-buffer
  203. :help "Change the target buffer and display it in the target window"))
  204. (define-key menu-map [rs]
  205. '(menu-item "Change syntax..." reb-change-syntax
  206. :help "Change the syntax used by the RE Builder"))
  207. (define-key menu-map [re]
  208. '(menu-item "Enter subexpression mode" reb-enter-subexp-mode
  209. :help "Enter the subexpression mode in the RE Builder"))
  210. (define-key menu-map [ru]
  211. '(menu-item "Force update" reb-force-update
  212. :help "Force an update in the RE Builder target window without a match limit"))
  213. (define-key menu-map [rn]
  214. '(menu-item "Go to next match" reb-next-match
  215. :help "Go to next match in the RE Builder target window"))
  216. (define-key menu-map [rp]
  217. '(menu-item "Go to previous match" reb-prev-match
  218. :help "Go to previous match in the RE Builder target window"))
  219. (define-key menu-map [rc]
  220. '(menu-item "Copy current RE" reb-copy
  221. :help "Copy current RE into the kill ring for later insertion"))
  222. map)
  223. "Keymap used by the RE Builder.")
  224. (define-derived-mode reb-mode nil "RE Builder"
  225. "Major mode for interactively building Regular Expressions."
  226. (set (make-local-variable 'blink-matching-paren) nil)
  227. (reb-mode-common))
  228. (defvar reb-lisp-mode-map
  229. (let ((map (make-sparse-keymap)))
  230. ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from
  231. ;; `emacs-lisp-mode'
  232. (define-key map "\C-c" (lookup-key reb-mode-map "\C-c"))
  233. map))
  234. (define-derived-mode reb-lisp-mode
  235. emacs-lisp-mode "RE Builder Lisp"
  236. "Major mode for interactively building symbolic Regular Expressions."
  237. ;; Pull in packages as needed
  238. (cond ((memq reb-re-syntax '(sregex rx)) ; rx-to-string is autoloaded
  239. (require 'rx))) ; require rx anyway
  240. (reb-mode-common))
  241. (defvar reb-subexp-mode-map
  242. (let ((m (make-keymap)))
  243. (suppress-keymap m)
  244. ;; Again share the "\C-c" keymap for the commands
  245. (define-key m "\C-c" (lookup-key reb-mode-map "\C-c"))
  246. (define-key m "q" 'reb-quit-subexp-mode)
  247. (dotimes (digit 10)
  248. (define-key m (int-to-string digit) 'reb-display-subexp))
  249. m)
  250. "Keymap used by the RE Builder for the subexpression mode.")
  251. (defun reb-mode-common ()
  252. "Setup functions common to functions `reb-mode' and `reb-mode-lisp'."
  253. (setq reb-mode-string ""
  254. reb-valid-string ""
  255. mode-line-buffer-identification
  256. '(25 . ("%b" reb-mode-string reb-valid-string)))
  257. (reb-update-modestring)
  258. (add-hook 'after-change-functions 'reb-auto-update nil t)
  259. ;; At least make the overlays go away if the buffer is killed
  260. (add-hook 'kill-buffer-hook 'reb-kill-buffer nil t)
  261. (reb-auto-update nil nil nil))
  262. (defun reb-color-display-p ()
  263. "Return t if display is capable of displaying colors."
  264. (eq 'color
  265. ;; emacs/xemacs compatibility
  266. (if (fboundp 'frame-parameter)
  267. (frame-parameter (selected-frame) 'display-type)
  268. (if (fboundp 'frame-property)
  269. (frame-property (selected-frame) 'display-type)))))
  270. (defsubst reb-lisp-syntax-p ()
  271. "Return non-nil if RE Builder uses a Lisp syntax."
  272. (memq reb-re-syntax '(sregex rx)))
  273. (defmacro reb-target-binding (symbol)
  274. "Return binding for SYMBOL in the RE Builder target buffer."
  275. `(with-current-buffer reb-target-buffer ,symbol))
  276. (defun reb-initialize-buffer ()
  277. "Initialize the current buffer as a RE Builder buffer."
  278. (erase-buffer)
  279. (reb-insert-regexp)
  280. (goto-char (+ 2 (point-min)))
  281. (cond ((reb-lisp-syntax-p)
  282. (reb-lisp-mode))
  283. (t (reb-mode)))
  284. (reb-do-update))
  285. (defun reb-mode-buffer-p ()
  286. "Return non-nil if the current buffer is a RE Builder buffer."
  287. (memq major-mode '(reb-mode reb-lisp-mode)))
  288. ;;; This is to help people find this in Apropos.
  289. ;;;###autoload
  290. (defalias 'regexp-builder 're-builder)
  291. ;;;###autoload
  292. (defun re-builder ()
  293. "Construct a regexp interactively.
  294. This command makes the current buffer the \"target\" buffer of
  295. the regexp builder. It displays a buffer named \"*RE-Builder*\"
  296. in another window, initially containing an empty regexp.
  297. As you edit the regexp in the \"*RE-Builder*\" buffer, the
  298. matching parts of the target buffer will be highlighted."
  299. (interactive)
  300. (if (and (string= (buffer-name) reb-buffer)
  301. (reb-mode-buffer-p))
  302. (message "Already in the RE Builder")
  303. (when reb-target-buffer
  304. (reb-delete-overlays))
  305. (setq reb-target-buffer (current-buffer)
  306. reb-target-window (selected-window))
  307. (select-window (or (get-buffer-window reb-buffer)
  308. (progn
  309. (setq reb-window-config (current-window-configuration))
  310. (split-window (selected-window) (- (window-height) 4)))))
  311. (switch-to-buffer (get-buffer-create reb-buffer))
  312. (reb-initialize-buffer)))
  313. (defun reb-change-target-buffer (buf)
  314. "Change the target buffer and display it in the target window."
  315. (interactive "bSet target buffer to: ")
  316. (let ((buffer (get-buffer buf)))
  317. (if (not buffer)
  318. (error "No such buffer")
  319. (reb-delete-overlays)
  320. (setq reb-target-buffer buffer)
  321. (reb-do-update
  322. (if reb-subexp-mode reb-subexp-displayed nil))
  323. (reb-update-modestring))))
  324. (defun reb-force-update ()
  325. "Force an update in the RE Builder target window without a match limit."
  326. (interactive)
  327. (let ((reb-auto-match-limit nil))
  328. (reb-update-overlays
  329. (if reb-subexp-mode reb-subexp-displayed nil))))
  330. (defun reb-quit ()
  331. "Quit the RE Builder mode."
  332. (interactive)
  333. (setq reb-subexp-mode nil
  334. reb-subexp-displayed nil)
  335. (reb-delete-overlays)
  336. (bury-buffer)
  337. (set-window-configuration reb-window-config))
  338. (defun reb-next-match ()
  339. "Go to next match in the RE Builder target window."
  340. (interactive)
  341. (reb-assert-buffer-in-window)
  342. (with-selected-window reb-target-window
  343. (if (not (re-search-forward reb-regexp (point-max) t))
  344. (message "No more matches")
  345. (reb-show-subexp
  346. (or (and reb-subexp-mode reb-subexp-displayed) 0)
  347. t))))
  348. (defun reb-prev-match ()
  349. "Go to previous match in the RE Builder target window."
  350. (interactive)
  351. (reb-assert-buffer-in-window)
  352. (with-selected-window reb-target-window
  353. (let ((p (point)))
  354. (goto-char (1- p))
  355. (if (re-search-backward reb-regexp (point-min) t)
  356. (reb-show-subexp
  357. (or (and reb-subexp-mode reb-subexp-displayed) 0)
  358. t)
  359. (goto-char p)
  360. (message "No more matches")))))
  361. (defun reb-toggle-case ()
  362. "Toggle case sensitivity of searches for RE Builder target buffer."
  363. (interactive)
  364. (with-current-buffer reb-target-buffer
  365. (setq case-fold-search (not case-fold-search)))
  366. (reb-update-modestring)
  367. (reb-auto-update nil nil nil t))
  368. (defun reb-copy ()
  369. "Copy current RE into the kill ring for later insertion."
  370. (interactive)
  371. (reb-update-regexp)
  372. (let ((re (with-output-to-string
  373. (print (reb-target-binding reb-regexp)))))
  374. (kill-new (substring re 1 (1- (length re))))
  375. (message "Regexp copied to kill-ring")))
  376. ;; The subexpression mode is not electric because the number of
  377. ;; matches should be seen rather than a prompt.
  378. (defun reb-enter-subexp-mode ()
  379. "Enter the subexpression mode in the RE Builder."
  380. (interactive)
  381. (setq reb-subexp-mode t)
  382. (reb-update-modestring)
  383. (use-local-map reb-subexp-mode-map)
  384. (message "`0'-`9' to display subexpressions `q' to quit subexp mode"))
  385. (defun reb-show-subexp (subexp &optional pause)
  386. "Visually show limit of subexpression SUBEXP of recent search.
  387. On color displays this just puts point to the end of the expression as
  388. the match should already be marked by an overlay.
  389. On other displays jump to the beginning and the end of it.
  390. If the optional PAUSE is non-nil then pause at the end in any case."
  391. (with-selected-window reb-target-window
  392. (unless (reb-color-display-p)
  393. (goto-char (match-beginning subexp))
  394. (sit-for reb-blink-delay))
  395. (goto-char (match-end subexp))
  396. (when (or (not (reb-color-display-p)) pause)
  397. (sit-for reb-blink-delay))))
  398. (defun reb-quit-subexp-mode ()
  399. "Quit the subexpression mode in the RE Builder."
  400. (interactive)
  401. (setq reb-subexp-mode nil
  402. reb-subexp-displayed nil)
  403. (reb-update-modestring)
  404. (use-local-map reb-mode-map)
  405. (reb-do-update))
  406. (defun reb-change-syntax (&optional syntax)
  407. "Change the syntax used by the RE Builder.
  408. Optional argument SYNTAX must be specified if called non-interactively."
  409. (interactive
  410. (list (intern
  411. (completing-read "Select syntax: "
  412. (mapcar (lambda (el) (cons (symbol-name el) 1))
  413. '(read string sregex rx))
  414. nil t (symbol-name reb-re-syntax)))))
  415. (if (memq syntax '(read string sregex rx))
  416. (let ((buffer (get-buffer reb-buffer)))
  417. (setq reb-re-syntax syntax)
  418. (when buffer
  419. (with-current-buffer buffer
  420. (reb-initialize-buffer))))
  421. (error "Invalid syntax: %s" syntax)))
  422. ;; Non-interactive functions below
  423. (defun reb-do-update (&optional subexp)
  424. "Update matches in the RE Builder target window.
  425. If SUBEXP is non-nil mark only the corresponding sub-expressions."
  426. (reb-assert-buffer-in-window)
  427. (reb-update-regexp)
  428. (reb-update-overlays subexp))
  429. (defun reb-auto-update (_beg _end _lenold &optional force)
  430. "Called from `after-update-functions' to update the display.
  431. BEG, END and LENOLD are passed in from the hook.
  432. An actual update is only done if the regexp has changed or if the
  433. optional fourth argument FORCE is non-nil."
  434. (let ((prev-valid reb-valid-string)
  435. (new-valid
  436. (condition-case nil
  437. (progn
  438. (when (or (reb-update-regexp) force)
  439. (reb-do-update))
  440. "")
  441. (error " *invalid*"))))
  442. (setq reb-valid-string new-valid)
  443. (force-mode-line-update)
  444. ;; Through the caching of the re a change invalidating the syntax
  445. ;; for symbolic expressions will not delete the overlays so we
  446. ;; catch it here
  447. (when (and (reb-lisp-syntax-p)
  448. (not (string= prev-valid new-valid))
  449. (string= prev-valid ""))
  450. (reb-delete-overlays))))
  451. (defun reb-delete-overlays ()
  452. "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
  453. (when (buffer-live-p reb-target-buffer)
  454. (with-current-buffer reb-target-buffer
  455. (mapc 'delete-overlay reb-overlays)
  456. (setq reb-overlays nil))))
  457. (defun reb-assert-buffer-in-window ()
  458. "Assert that `reb-target-buffer' is displayed in `reb-target-window'."
  459. (if (not (eq reb-target-buffer (window-buffer reb-target-window)))
  460. (set-window-buffer reb-target-window reb-target-buffer)))
  461. (defun reb-update-modestring ()
  462. "Update the variable `reb-mode-string' displayed in the mode line."
  463. (setq reb-mode-string
  464. (concat
  465. (if reb-subexp-mode
  466. (format " (subexp %s)" (or reb-subexp-displayed "-"))
  467. "")
  468. (if (not (reb-target-binding case-fold-search))
  469. " Case"
  470. "")))
  471. (force-mode-line-update))
  472. (defun reb-display-subexp (&optional subexp)
  473. "Highlight only subexpression SUBEXP in the RE Builder."
  474. (interactive)
  475. (setq reb-subexp-displayed
  476. (or subexp (string-to-number (format "%c" last-command-event))))
  477. (reb-update-modestring)
  478. (reb-do-update reb-subexp-displayed))
  479. (defun reb-kill-buffer ()
  480. "When the RE Builder buffer is killed make sure no overlays stay around."
  481. (when (reb-mode-buffer-p)
  482. (reb-delete-overlays)))
  483. ;; The next functions are the interface between the regexp and
  484. ;; its textual representation in the RE Builder buffer.
  485. ;; They are the only functions concerned with the actual syntax
  486. ;; being used.
  487. (defun reb-read-regexp ()
  488. "Read current RE."
  489. (save-excursion
  490. (cond ((eq reb-re-syntax 'read)
  491. (goto-char (point-min))
  492. (read (current-buffer)))
  493. ((eq reb-re-syntax 'string)
  494. (goto-char (point-min))
  495. (re-search-forward "\"")
  496. (let ((beg (point)))
  497. (goto-char (point-max))
  498. (re-search-backward "\"")
  499. (buffer-substring-no-properties beg (point))))
  500. ((reb-lisp-syntax-p)
  501. (buffer-string)))))
  502. (defun reb-empty-regexp ()
  503. "Return empty RE for current syntax."
  504. (cond ((reb-lisp-syntax-p) "'()")
  505. (t "")))
  506. (defun reb-insert-regexp ()
  507. "Insert current RE."
  508. (let ((re (or (reb-target-binding reb-regexp)
  509. (reb-empty-regexp))))
  510. (cond ((eq reb-re-syntax 'read)
  511. (print re (current-buffer)))
  512. ((eq reb-re-syntax 'string)
  513. (insert "\n\"" re "\""))
  514. ;; For the Lisp syntax we need the "source" of the regexp
  515. ((reb-lisp-syntax-p)
  516. (insert (or (reb-target-binding reb-regexp-src)
  517. (reb-empty-regexp)))))))
  518. (defun reb-cook-regexp (re)
  519. "Return RE after processing it according to `reb-re-syntax'."
  520. (cond ((memq reb-re-syntax '(sregex rx))
  521. (rx-to-string (eval (car (read-from-string re)))))
  522. (t re)))
  523. (defun reb-update-regexp ()
  524. "Update the regexp for the target buffer.
  525. Return t if the (cooked) expression changed."
  526. (let* ((re-src (reb-read-regexp))
  527. (re (reb-cook-regexp re-src)))
  528. (with-current-buffer reb-target-buffer
  529. (let ((oldre reb-regexp))
  530. (prog1
  531. (not (string= oldre re))
  532. (setq reb-regexp re)
  533. ;; Only update the source re for the lisp formats
  534. (when (reb-lisp-syntax-p)
  535. (setq reb-regexp-src re-src)))))))
  536. ;; And now the real core of the whole thing
  537. (defun reb-count-subexps (re)
  538. "Return number of sub-expressions in the regexp RE."
  539. (let ((i 0) (beg 0))
  540. (while (string-match "\\\\(" re beg)
  541. (setq i (1+ i)
  542. beg (match-end 0)))
  543. i))
  544. (defun reb-update-overlays (&optional subexp)
  545. "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
  546. If SUBEXP is non-nil mark only the corresponding sub-expressions."
  547. (let* ((re (reb-target-binding reb-regexp))
  548. (subexps (reb-count-subexps re))
  549. (matches 0)
  550. (submatches 0)
  551. firstmatch)
  552. (with-current-buffer reb-target-buffer
  553. (reb-delete-overlays)
  554. (goto-char (point-min))
  555. (while (and (not (eobp))
  556. (re-search-forward re (point-max) t)
  557. (or (not reb-auto-match-limit)
  558. (< matches reb-auto-match-limit)))
  559. (when (and (= 0 (length (match-string 0)))
  560. (not (eobp)))
  561. (forward-char 1))
  562. (let ((i 0)
  563. suffix max-suffix)
  564. (setq matches (1+ matches))
  565. (while (<= i subexps)
  566. (when (and (or (not subexp) (= subexp i))
  567. (match-beginning i))
  568. (let ((overlay (make-overlay (match-beginning i)
  569. (match-end i)))
  570. ;; When we have exceeded the number of provided faces,
  571. ;; cycle thru them where `max-suffix' denotes the maximum
  572. ;; suffix for `reb-match-*' that has been defined and
  573. ;; `suffix' the suffix calculated for the current match.
  574. (face
  575. (cond
  576. (max-suffix
  577. (if (= suffix max-suffix)
  578. (setq suffix 1)
  579. (setq suffix (1+ suffix)))
  580. (intern-soft (format "reb-match-%d" suffix)))
  581. ((intern-soft (format "reb-match-%d" i)))
  582. ((setq max-suffix (1- i))
  583. (setq suffix 1)
  584. ;; `reb-match-1' must exist.
  585. 'reb-match-1))))
  586. (unless firstmatch (setq firstmatch (match-data)))
  587. (setq reb-overlays (cons overlay reb-overlays)
  588. submatches (1+ submatches))
  589. (overlay-put overlay 'face face)
  590. (overlay-put overlay 'priority i)))
  591. (setq i (1+ i))))))
  592. (let ((count (if subexp submatches matches)))
  593. (message "%s %smatch%s%s"
  594. (if (= 0 count) "No" (int-to-string count))
  595. (if subexp "subexpression " "")
  596. (if (= 1 count) "" "es")
  597. (if (and reb-auto-match-limit
  598. (= reb-auto-match-limit count))
  599. " (limit reached)" "")))
  600. (when firstmatch
  601. (store-match-data firstmatch)
  602. (reb-show-subexp (or subexp 0)))))
  603. ;; The End
  604. (defun re-builder-unload-function ()
  605. "Unload the RE Builder library."
  606. (when (buffer-live-p (get-buffer reb-buffer))
  607. (with-current-buffer reb-buffer
  608. (remove-hook 'after-change-functions 'reb-auto-update t)
  609. (remove-hook 'kill-buffer-hook 'reb-kill-buffer t)
  610. (when (reb-mode-buffer-p)
  611. (reb-delete-overlays))))
  612. ;; continue standard unloading
  613. nil)
  614. (provide 're-builder)
  615. ;;; re-builder.el ends here