]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/re-builder.el
(rx-syntax): Move sregex style syntax to code.
[gnu-emacs] / lisp / emacs-lisp / re-builder.el
index ace889c962020d70f4b748696cdccdc07d61e66d..9c904e6c0bc83992a9bef8007c037bf2b761a448 100644 (file)
@@ -1,6 +1,6 @@
-;;; re-builder.el --- Building Regexps with visual feedback
+;;; re-builder.el --- building Regexps with visual feedback
 
-;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
+;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
 
 ;; Author: Detlev Zundel <dzu@gnu.org>
 ;; Keywords: matching, lisp, tools
 ;; you want to know the reason why RE Builder considers it as invalid
 ;; call `reb-force-update' ("\C-c\C-u") which should reveal the error.
 
+;; The target buffer can be changed with `reb-change-target-buffer'
+;; ("\C-c\C-b"). Changing the target buffer automatically removes
+;; the overlays from the old buffer and displays the new one in the
+;; target window.
+
 ;; The `re-builder' keeps the focus while updating the matches in the
 ;; target buffer so corrections are easy to incorporate.  If you are
 ;; satisfied with the result you can paste the RE to the kill-ring
@@ -141,30 +146,42 @@ Set it to nil if you don't want limits here."
 
 
 (defface reb-match-0
-  '((((class color))
-     (:background "lightblue"))
-    (t (:inverse-video t)))
+  '((((class color) (background light))
+     :background "lightblue")
+    (((class color) (background dark))
+     :background "steelblue4")
+    (t
+     :inverse-video t))
   "Used for displaying the whole match."
   :group 're-builder)
 
 (defface reb-match-1
-  '((((class color))
-     (:background "aquamarine"))
-    (t (:inverse-video t)))
+  '((((class color) (background light))
+     :background "aquamarine")
+    (((class color) (background dark))
+     :background "blue3")
+    (t
+     :inverse-video t))
   "Used for displaying the first matching subexpression."
   :group 're-builder)
 
 (defface reb-match-2
-  '((((class color))
-     (:background "springgreen"))
-    (t (:inverse-video t)))
+  '((((class color) (background light))
+     :background "springgreen")
+    (((class color) (background dark))
+     :background "chartreuse4")
+    (t
+     :inverse-video t))
   "Used for displaying the second matching subexpression."
   :group 're-builder)
 
 (defface reb-match-3
-  '((((class color))
-     (:background "yellow"))
-    (t (:inverse-video t)))
+  '((((class color) (background light))
+     :background "yellow")
+    (((class color) (background dark))
+     :background "sienna4")
+    (t
+     :inverse-video t))
   "Used for displaying the third matching subexpression."
   :group 're-builder)
 
@@ -224,23 +241,23 @@ Except for Lisp syntax this is the same as `reb-regexp'.")
       (define-key reb-mode-map "\C-c\C-r" 'reb-prev-match)
       (define-key reb-mode-map "\C-c\C-i" 'reb-change-syntax)
       (define-key reb-mode-map "\C-c\C-e" 'reb-enter-subexp-mode)
+      (define-key reb-mode-map "\C-c\C-b" 'reb-change-target-buffer)
       (define-key reb-mode-map "\C-c\C-u" 'reb-force-update)))
 
 (defun reb-mode ()
   "Major mode for interactively building Regular Expressions.
 \\{reb-mode-map}"
   (interactive)
-
-  (setq major-mode       'reb-mode
-        mode-name        "RE Builder")
+  (kill-all-local-variables)
+  (setq major-mode 'reb-mode
+        mode-name "RE Builder")
   (use-local-map reb-mode-map)
   (reb-mode-common)
-  (run-hooks reb-mode-hook))
+  (run-hooks 'reb-mode-hook))
 
 (define-derived-mode reb-lisp-mode
   emacs-lisp-mode "RE Builder Lisp"
-  "Major mode for interactively building symbolic Regular Expressions.
-\\{reb-lisp-mode-map}"
+  "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
@@ -252,29 +269,17 @@ Except for Lisp syntax this is the same as `reb-regexp'.")
 (define-key reb-lisp-mode-map "\C-c"
   (lookup-key reb-mode-map "\C-c"))
 
-(if (boundp 'font-lock-defaults-alist)
-    (setq font-lock-defaults-alist
-         (cons (cons 'reb-lisp-mode
-                     (cdr (assoc 'emacs-lisp-mode
-                                 font-lock-defaults-alist)))
-               font-lock-defaults-alist)))
-
-(defvar reb-subexp-mode-map nil
+(defvar reb-subexp-mode-map
+  (let ((m (make-keymap)))
+    (suppress-keymap m)
+    ;; Again share the "\C-c" keymap for the commands
+    (define-key m "\C-c" (lookup-key reb-mode-map "\C-c"))
+    (define-key m "q" 'reb-quit-subexp-mode)
+    (dotimes (digit 10)
+      (define-key m (int-to-string digit) 'reb-display-subexp))
+    m)
   "Keymap used by the RE Builder for the subexpression mode.")
 
-(if (not reb-subexp-mode-map)
-    (progn
-      (setq reb-subexp-mode-map (make-sparse-keymap))
-      (suppress-keymap reb-subexp-mode-map)
-      ;; Again share the "\C-c" keymap for the commands
-      (define-key reb-subexp-mode-map "\C-c"
-       (lookup-key reb-mode-map "\C-c"))
-      (define-key reb-subexp-mode-map "q" 'reb-quit-subexp-mode)
-      (mapcar (lambda (digit)
-               (define-key reb-subexp-mode-map (int-to-string digit)
-                 'reb-display-subexp))
-             '(0 1 2 3 4 5 6 7 8 9))))
-
 (defun reb-mode-common ()
   "Setup functions common to functions `reb-mode' and `reb-mode-lisp'."
 
@@ -327,21 +332,36 @@ Except for Lisp syntax this is the same as `reb-regexp'.")
   "Call up the RE Builder for the current window."
   (interactive)
 
-  (if 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)))
-  (switch-to-buffer (get-buffer-create reb-buffer))
-  (erase-buffer)
-  (reb-insert-regexp)
-  (goto-char (+ 2 (point-min)))
-  (cond
-   ((reb-lisp-syntax-p)
-    (reb-lisp-mode))
-   (t (reb-mode))))
-
+  (if (and (string= (buffer-name) reb-buffer)
+           (memq major-mode '(reb-mode reb-lisp-mode)))
+      (message "Already in the RE Builder")
+    (if 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)))
+    (switch-to-buffer (get-buffer-create reb-buffer))
+    (erase-buffer)
+    (reb-insert-regexp)
+    (goto-char (+ 2 (point-min)))
+    (cond
+     ((reb-lisp-syntax-p)
+      (reb-lisp-mode))
+     (t (reb-mode)))))
+
+(defun reb-change-target-buffer (buf)
+  "Change the target buffer and display it in the target window."
+  (interactive "bSet target buffer to: ")
+
+  (let ((buffer (get-buffer buf)))
+    (if (not buffer)
+        (error "No such buffer")
+      (reb-delete-overlays)
+      (setq reb-target-buffer buffer)
+      (reb-do-update
+       (if reb-subexp-mode reb-subexp-displayed nil))
+      (reb-update-modestring))))
 
 (defun reb-force-update ()
   "Forces an update in the RE Builder target window without a match limit."
@@ -411,7 +431,6 @@ Except for Lisp syntax this is the same as `reb-regexp'.")
 (defun reb-enter-subexp-mode ()
   "Enter the subexpression mode in the RE Builder."
   (interactive)
-
   (setq reb-subexp-mode t)
   (reb-update-modestring)
   (use-local-map reb-subexp-mode-map)
@@ -434,7 +453,6 @@ If the optional PAUSE is non-nil then pause at the end in any case."
 (defun reb-quit-subexp-mode ()
   "Quit the subexpression mode in the RE Builder."
   (interactive)
-
   (setq reb-subexp-mode nil
        reb-subexp-displayed nil)
   (reb-update-modestring)
@@ -518,7 +536,7 @@ optional fourth argument FORCE is non-nil."
   (setq reb-mode-string
        (concat
         (if reb-subexp-mode
-            (concat " (subexp " (or reb-subexp-displayed "-") ")")
+             (format " (subexp %s)" (or reb-subexp-displayed "-"))
           "")
         (if (not (reb-target-binding case-fold-search))
             " Case"
@@ -662,4 +680,7 @@ If SUBEXP is non-nil mark only the corresponding sub-expressions."
        (progn (store-match-data firstmatch)
               (reb-show-subexp (or subexp 0))))))
 
+(provide 're-builder)
+
+;;; arch-tag: 5c5515ac-4085-4524-a421-033f44f032e7
 ;;; re-builder.el ends here