X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/3f715d17fef56ee78a3df7ebb00d4e8b1aec37de..b1f367f797aa19047904e73a67e52f391c720e0d:/lisp/emacs-lisp/re-builder.el diff --git a/lisp/emacs-lisp/re-builder.el b/lisp/emacs-lisp/re-builder.el index a0b2037541..ebbd6ff1fd 100644 --- a/lisp/emacs-lisp/re-builder.el +++ b/lisp/emacs-lisp/re-builder.el @@ -1,7 +1,6 @@ -;;; re-builder.el --- building Regexps with visual feedback +;;; re-builder.el --- building Regexps with visual feedback -*- lexical-binding: t -*- -;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, -;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. +;; Copyright (C) 1999-2011 Free Software Foundation, Inc. ;; Author: Detlev Zundel ;; Keywords: matching, lisp, tools @@ -60,15 +59,13 @@ ;; even the auto updates go all the way. Forcing an update overrides ;; this limit allowing an easy way to see all matches. -;; Currently `re-builder' understands five different forms of input, -;; namely `read', `string', `rx', `sregex' and `lisp-re' syntax. Read +;; Currently `re-builder' understands three different forms of input, +;; namely `read', `string', and `rx' syntax. Read ;; syntax and string syntax are both delimited by `"'s and behave ;; according to their name. With the `string' syntax there's no need ;; to escape the backslashes and double quotes simplifying the editing ;; somewhat. The other three allow editing of symbolic regular -;; expressions supported by the packages of the same name. (`lisp-re' -;; is a package by me and its support may go away as it is nearly the -;; same as the `sregex' package in Emacs) +;; expressions supported by the packages of the same name. ;; Editing symbolic expressions is done through a major mode derived ;; from `emacs-lisp-mode' so you'll get all the good stuff like @@ -77,7 +74,7 @@ ;; When editing a symbolic regular expression, only the first ;; expression in the RE Builder buffer is considered, which helps ;; limiting the extent of the expression like the `"'s do for the text -;; modes. For the `sregex' syntax the function `sregex' is applied to +;; modes. For the `rx' syntax the function `rx-to-string' is applied to ;; the evaluated expression read. So you can use quoted arguments ;; with something like '("findme") or you can construct arguments to ;; your hearts delight with a valid ELisp expression. (The compiled @@ -128,12 +125,10 @@ (defcustom reb-re-syntax 'read "Syntax for the REs in the RE Builder. -Can either be `read', `string', `sregex', `lisp-re', `rx'." +Can either be `read', `string', or `rx'." :group 're-builder :type '(choice (const :tag "Read syntax" read) (const :tag "String syntax" string) - (const :tag "`sregex' syntax" sregex) - (const :tag "`lisp-re' syntax" lisp-re) (const :tag "`rx' syntax" rx))) (defcustom reb-auto-match-limit 200 @@ -247,8 +242,10 @@ Except for Lisp syntax this is the same as `reb-regexp'.") :help "Quit the RE Builder mode")) (define-key menu-map [rt] '(menu-item "Case sensitive" reb-toggle-case - :button (:toggle . case-fold-search) - :help "Toggle case sensitivity of searches for RE Builder target buffer.")) + :button (:toggle . (with-current-buffer + reb-target-buffer + (null case-fold-search))) + :help "Toggle case sensitivity of searches for RE Builder target buffer")) (define-key menu-map [rb] '(menu-item "Change target buffer..." reb-change-target-buffer :help "Change the target buffer and display it in the target window")) @@ -273,34 +270,26 @@ Except for Lisp syntax this is the same as `reb-regexp'.") map) "Keymap used by the RE Builder.") -(defun reb-mode () - "Major mode for interactively building Regular Expressions. -\\{reb-mode-map}" - (interactive) - (kill-all-local-variables) - (setq major-mode 'reb-mode - mode-name "RE Builder") +(define-derived-mode reb-mode nil "RE Builder" + "Major mode for interactively building Regular Expressions." (set (make-local-variable 'blink-matching-paren) nil) - (use-local-map reb-mode-map) - (reb-mode-common) - (run-mode-hooks 'reb-mode-hook)) + (reb-mode-common)) + +(defvar reb-lisp-mode-map + (let ((map (make-sparse-keymap))) + ;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from + ;; `emacs-lisp-mode' + (define-key map "\C-c" (lookup-key reb-mode-map "\C-c")) + map)) (define-derived-mode reb-lisp-mode emacs-lisp-mode "RE Builder Lisp" "Major mode for interactively building symbolic Regular Expressions." - (cond ((eq reb-re-syntax 'lisp-re) ; Pull in packages - (require 'lisp-re)) ; as needed - ((eq reb-re-syntax 'sregex) ; sregex is not autoloaded - (require 'sregex)) ; right now.. - ((eq reb-re-syntax 'rx) ; rx-to-string is autoloaded - (require 'rx))) ; require rx anyway + ;; Pull in packages as needed + (cond ((memq reb-re-syntax '(sregex rx)) ; rx-to-string is autoloaded + (require 'rx))) ; require rx anyway (reb-mode-common)) -;; Use the same "\C-c" keymap as `reb-mode' and use font-locking from -;; `emacs-lisp-mode' -(define-key reb-lisp-mode-map "\C-c" - (lookup-key reb-mode-map "\C-c")) - (defvar reb-subexp-mode-map (let ((m (make-keymap))) (suppress-keymap m) @@ -336,7 +325,7 @@ Except for Lisp syntax this is the same as `reb-regexp'.") (defsubst reb-lisp-syntax-p () "Return non-nil if RE Builder uses a Lisp syntax." - (memq reb-re-syntax '(lisp-re sregex rx))) + (memq reb-re-syntax '(sregex rx))) (defmacro reb-target-binding (symbol) "Return binding for SYMBOL in the RE Builder target buffer." @@ -349,7 +338,8 @@ Except for Lisp syntax this is the same as `reb-regexp'.") (goto-char (+ 2 (point-min))) (cond ((reb-lisp-syntax-p) (reb-lisp-mode)) - (t (reb-mode)))) + (t (reb-mode))) + (reb-do-update)) (defun reb-mode-buffer-p () "Return non-nil if the current buffer is a RE Builder buffer." @@ -361,18 +351,25 @@ Except for Lisp syntax this is the same as `reb-regexp'.") ;;;###autoload (defun re-builder () - "Construct a regexp interactively." - (interactive) + "Construct a regexp interactively. +This command makes the current buffer the \"target\" buffer of +the regexp builder. It displays a buffer named \"*RE-Builder*\" +in another window, initially containing an empty regexp. +As you edit the regexp in the \"*RE-Builder*\" buffer, the +matching parts of the target buffer will be highlighted." + (interactive) (if (and (string= (buffer-name) reb-buffer) (reb-mode-buffer-p)) (message "Already in the RE Builder") (when reb-target-buffer (reb-delete-overlays)) (setq reb-target-buffer (current-buffer) - reb-target-window (selected-window) - reb-window-config (current-window-configuration)) - (select-window (split-window (selected-window) (- (window-height) 4))) + reb-target-window (selected-window)) + (select-window (or (get-buffer-window reb-buffer) + (progn + (setq reb-window-config (current-window-configuration)) + (split-window (selected-window) (- (window-height) 4))))) (switch-to-buffer (get-buffer-create reb-buffer)) (reb-initialize-buffer))) @@ -493,10 +490,10 @@ Optional argument SYNTAX must be specified if called non-interactively." (list (intern (completing-read "Select syntax: " (mapcar (lambda (el) (cons (symbol-name el) 1)) - '(read string lisp-re sregex rx)) + '(read string sregex rx)) nil t (symbol-name reb-re-syntax))))) - (if (memq syntax '(read string lisp-re sregex rx)) + (if (memq syntax '(read string sregex rx)) (let ((buffer (get-buffer reb-buffer))) (setq reb-re-syntax syntax) (when buffer @@ -514,7 +511,7 @@ If SUBEXP is non-nil mark only the corresponding sub-expressions." (reb-update-regexp) (reb-update-overlays subexp)) -(defun reb-auto-update (beg end lenold &optional force) +(defun reb-auto-update (_beg _end _lenold &optional force) "Called from `after-update-functions' to update the display. BEG, END and LENOLD are passed in from the hook. An actual update is only done if the regexp has changed or if the @@ -524,7 +521,6 @@ optional fourth argument FORCE is non-nil." (condition-case nil (progn (when (or (reb-update-regexp) force) - (reb-assert-buffer-in-window) (reb-do-update)) "") (error " *invalid*")))) @@ -569,7 +565,7 @@ optional fourth argument FORCE is non-nil." (interactive) (setq reb-subexp-displayed - (or subexp (string-to-number (format "%c" last-command-char)))) + (or subexp (string-to-number (format "%c" last-command-event)))) (reb-update-modestring) (reb-do-update reb-subexp-displayed)) @@ -621,12 +617,7 @@ optional fourth argument FORCE is non-nil." (defun reb-cook-regexp (re) "Return RE after processing it according to `reb-re-syntax'." - (cond ((eq reb-re-syntax 'lisp-re) - (when (fboundp 'lre-compile-string) - (lre-compile-string (eval (car (read-from-string re)))))) - ((eq reb-re-syntax 'sregex) - (apply 'sregex (eval (car (read-from-string re))))) - ((eq reb-re-syntax 'rx) + (cond ((memq reb-re-syntax '(sregex rx)) (rx-to-string (eval (car (read-from-string re))))) (t re))) @@ -663,8 +654,7 @@ If SUBEXP is non-nil mark only the corresponding sub-expressions." (matches 0) (submatches 0) firstmatch) - (save-excursion - (set-buffer reb-target-buffer) + (with-current-buffer reb-target-buffer (reb-delete-overlays) (goto-char (point-min)) (while (and (not (eobp)) @@ -724,12 +714,10 @@ If SUBEXP is non-nil mark only the corresponding sub-expressions." (remove-hook 'after-change-functions 'reb-auto-update t) (remove-hook 'kill-buffer-hook 'reb-kill-buffer t) (when (reb-mode-buffer-p) - (reb-delete-overlays) - (funcall default-major-mode)))) + (reb-delete-overlays)))) ;; continue standard unloading nil) (provide 're-builder) -;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7 ;;; re-builder.el ends here