changeset 93198:eb3d659f5085

(reb-mode-common): Remove reference to bogus variable `reb-kill-buffer'; don't make hooks buffer-local, use the LOCAL arg of `add-hook'. (reb-blink-delay, reb-mode-hook, reb-re-syntax, reb-auto-match-limit): Remove spurious * from defcustom docstrings. (reb-next-match, reb-prev-match, reb-enter-subexp-mode): Fix typos in messages. (reb-mode-buffer-p): New function. (re-builder, reb-kill-buffer): Use `reb-mode-buffer-p'. Use `when'. (top, reb-show-subexp, reb-auto-update, reb-auto-update, reb-delete-overlays, reb-cook-regexp, reb-update-regexp, reb-update-overlays): Use `unless', `when'. (re-builder-unload-function): New function.
author Juanma Barranquero <lekktu@gmail.com>
date Tue, 25 Mar 2008 16:48:19 +0000
parents 212fa666680e
children 8a866fcb62fb
files lisp/emacs-lisp/re-builder.el
diffstat 1 files changed, 81 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/emacs-lisp/re-builder.el	Tue Mar 25 16:02:32 2008 +0000
+++ b/lisp/emacs-lisp/re-builder.el	Tue Mar 25 16:48:19 2008 +0000
@@ -109,8 +109,8 @@
 ;;; Code:
 
 ;; On XEmacs, load the overlay compatibility library
-(if (not (fboundp 'make-overlay))
-    (require 'overlay))
+(unless (fboundp 'make-overlay)
+  (require 'overlay))
 
 ;; User customizable variables
 (defgroup re-builder nil
@@ -119,17 +119,17 @@
   :prefix "reb-")
 
 (defcustom reb-blink-delay 0.5
-  "*Seconds to blink cursor for next/previous match in RE Builder."
+  "Seconds to blink cursor for next/previous match in RE Builder."
   :group 're-builder
   :type 'number)
 
 (defcustom reb-mode-hook nil
-  "*Hooks to run on entering RE Builder mode."
+  "Hooks to run on entering RE Builder mode."
   :group 're-builder
   :type 'hook)
 
 (defcustom reb-re-syntax 'read
-  "*Syntax for the REs in the RE Builder.
+  "Syntax for the REs in the RE Builder.
 Can either be `read', `string', `sregex', `lisp-re', `rx'."
   :group 're-builder
   :type '(choice (const :tag "Read syntax" read)
@@ -139,7 +139,7 @@
 		 (const :tag "`rx' syntax" rx)))
 
 (defcustom reb-auto-match-limit 200
-  "*Positive integer limiting the matches for RE Builder auto updates.
+  "Positive integer limiting the matches for RE Builder auto updates.
 Set it to nil if you don't want limits here."
   :group 're-builder
   :type '(restricted-sexp :match-alternatives
@@ -292,12 +292,9 @@
 	mode-line-buffer-identification
 	                 '(25 . ("%b" reb-mode-string reb-valid-string)))
   (reb-update-modestring)
-  (make-local-variable 'after-change-functions)
-  (add-hook 'after-change-functions
-	    'reb-auto-update)
+  (add-hook 'after-change-functions 'reb-auto-update nil t)
   ;; At least make the overlays go away if the buffer is killed
-  (make-local-variable 'reb-kill-buffer)
-  (add-hook 'kill-buffer-hook 'reb-kill-buffer)
+  (add-hook 'kill-buffer-hook 'reb-kill-buffer nil t)
   (reb-auto-update nil nil nil))
 
 (defun reb-color-display-p ()
@@ -326,6 +323,10 @@
          (reb-lisp-mode))
         (t (reb-mode))))
 
+(defun reb-mode-buffer-p ()
+  "Return non-nil if the current buffer is a RE Builder buffer."
+  (memq major-mode '(reb-mode reb-lisp-mode)))
+
 ;;; This is to help people find this in Apropos.
 ;;;###autoload
 (defalias 'regexp-builder 're-builder)
@@ -336,10 +337,10 @@
   (interactive)
 
   (if (and (string= (buffer-name) reb-buffer)
-           (memq major-mode '(reb-mode reb-lisp-mode)))
+	   (reb-mode-buffer-p))
       (message "Already in the RE Builder")
-    (if reb-target-buffer
-        (reb-delete-overlays))
+    (when reb-target-buffer
+      (reb-delete-overlays))
     (setq reb-target-buffer (current-buffer)
           reb-target-window (selected-window)
           reb-window-config (current-window-configuration))
@@ -385,7 +386,7 @@
   (reb-assert-buffer-in-window)
   (with-selected-window reb-target-window
     (if (not (re-search-forward reb-regexp (point-max) t))
-	(message "No more matches.")
+	(message "No more matches")
       (reb-show-subexp
        (or (and reb-subexp-mode reb-subexp-displayed) 0)
        t))))
@@ -403,7 +404,7 @@
            (or (and reb-subexp-mode reb-subexp-displayed) 0)
            t)
         (goto-char p)
-        (message "No more matches.")))))
+        (message "No more matches")))))
 
 (defun reb-toggle-case ()
   "Toggle case sensitivity of searches for RE Builder target buffer."
@@ -432,7 +433,7 @@
   (setq reb-subexp-mode t)
   (reb-update-modestring)
   (use-local-map reb-subexp-mode-map)
-  (message "`0'-`9' to display subexpressions  `q' to quit subexp mode."))
+  (message "`0'-`9' to display subexpressions  `q' to quit subexp mode"))
 
 (defun reb-show-subexp (subexp &optional pause)
   "Visually show limit of subexpression SUBEXP of recent search.
@@ -441,12 +442,12 @@
 On other displays jump to the beginning and the end of it.
 If the optional PAUSE is non-nil then pause at the end in any case."
   (with-selected-window reb-target-window
-    (if (not (reb-color-display-p))
-	(progn (goto-char (match-beginning subexp))
-	       (sit-for reb-blink-delay)))
+    (unless (reb-color-display-p)
+      (goto-char (match-beginning subexp))
+      (sit-for reb-blink-delay))
     (goto-char (match-end subexp))
-    (if (or (not (reb-color-display-p)) pause)
-	(sit-for reb-blink-delay))))
+    (when (or (not (reb-color-display-p)) pause)
+      (sit-for reb-blink-delay))))
 
 (defun reb-quit-subexp-mode ()
   "Quit the subexpression mode in the RE Builder."
@@ -494,10 +495,9 @@
 	(new-valid
 	 (condition-case nil
 	     (progn
-	       (if (or (reb-update-regexp) force)
-		   (progn
-		     (reb-assert-buffer-in-window)
-		     (reb-do-update)))
+	       (when (or (reb-update-regexp) force)
+		 (reb-assert-buffer-in-window)
+		 (reb-do-update))
 	       "")
 	   (error " *invalid*"))))
     (setq reb-valid-string new-valid)
@@ -506,14 +506,14 @@
     ;; Through the caching of the re a change invalidating the syntax
     ;; for symbolic expressions will not delete the overlays so we
     ;; catch it here
-    (if (and (reb-lisp-syntax-p)
-	     (not (string= prev-valid new-valid))
-	     (string= prev-valid ""))
-	(reb-delete-overlays))))
+    (when (and (reb-lisp-syntax-p)
+	       (not (string= prev-valid new-valid))
+	       (string= prev-valid ""))
+      (reb-delete-overlays))))
 
 (defun reb-delete-overlays ()
   "Delete all RE Builder overlays in the `reb-target-buffer' buffer."
-  (if (buffer-live-p reb-target-buffer)
+  (when (buffer-live-p reb-target-buffer)
     (with-current-buffer reb-target-buffer
       (mapc 'delete-overlay reb-overlays)
       (setq reb-overlays nil))))
@@ -548,8 +548,8 @@
 (defun reb-kill-buffer ()
   "When the RE Builder buffer is killed make sure no overlays stay around."
 
-  (if (member major-mode '(reb-mode reb-lisp-mode))
-      (reb-delete-overlays)))
+  (when (reb-mode-buffer-p)
+    (reb-delete-overlays)))
 
 
 ;; The next functions are the interface between the regexp and
@@ -594,8 +594,8 @@
 (defun reb-cook-regexp (re)
   "Return RE after processing it according to `reb-re-syntax'."
   (cond ((eq reb-re-syntax 'lisp-re)
-	 (if (fboundp 'lre-compile-string)
-	     (lre-compile-string (eval (car (read-from-string 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)
@@ -613,8 +613,8 @@
 	    (not (string= oldre re))
 	  (setq reb-regexp re)
 	  ;; Only update the source re for the lisp formats
-	  (if (reb-lisp-syntax-p)
-	      (setq reb-regexp-src re-src)))))))
+	  (when (reb-lisp-syntax-p)
+	    (setq reb-regexp-src re-src)))))))
 
 
 ;; And now the real core of the whole thing
@@ -643,38 +643,38 @@
 		  (re-search-forward re (point-max) t)
 		  (or (not reb-auto-match-limit)
 		      (< matches reb-auto-match-limit)))
-	(if (= 0 (length (match-string 0)))
-	    (unless (eobp)
-	      (forward-char 1)))
+	(when (and (= 0 (length (match-string 0)))
+		   (not (eobp)))
+	  (forward-char 1))
 	(let ((i 0)
 	      suffix max-suffix)
 	  (setq matches (1+ matches))
 	  (while (<= i subexps)
-	    (if (and (or (not subexp) (= subexp i))
-		     (match-beginning i))
-		(let ((overlay (make-overlay (match-beginning i)
-					     (match-end i)))
-		      ;; When we have exceeded the number of provided faces,
-		      ;; cycle thru them where `max-suffix' denotes the maximum
-		      ;; suffix for `reb-match-*' that has been defined and
-		      ;; `suffix' the suffix calculated for the current match.
-		      (face
-		       (cond
-			(max-suffix
-			 (if (= suffix max-suffix)
-			     (setq suffix 1)
-			   (setq suffix (1+ suffix)))
-			 (intern-soft (format "reb-match-%d" suffix)))
-			((intern-soft (format "reb-match-%d" i)))
-			((setq max-suffix (1- i))
-			 (setq suffix 1)
-			 ;; `reb-match-1' must exist.
-			 'reb-match-1))))
-		  (unless firstmatch (setq firstmatch (match-data)))
-		  (setq reb-overlays (cons overlay reb-overlays)
-			submatches (1+ submatches))
-		  (overlay-put overlay 'face face)
-		  (overlay-put overlay 'priority i)))
+	    (when (and (or (not subexp) (= subexp i))
+		       (match-beginning i))
+	      (let ((overlay (make-overlay (match-beginning i)
+					   (match-end i)))
+		    ;; When we have exceeded the number of provided faces,
+		    ;; cycle thru them where `max-suffix' denotes the maximum
+		    ;; suffix for `reb-match-*' that has been defined and
+		    ;; `suffix' the suffix calculated for the current match.
+		    (face
+		     (cond
+		       (max-suffix
+			(if (= suffix max-suffix)
+			    (setq suffix 1)
+			  (setq suffix (1+ suffix)))
+			(intern-soft (format "reb-match-%d" suffix)))
+		       ((intern-soft (format "reb-match-%d" i)))
+		       ((setq max-suffix (1- i))
+			(setq suffix 1)
+			;; `reb-match-1' must exist.
+			'reb-match-1))))
+		(unless firstmatch (setq firstmatch (match-data)))
+		(setq reb-overlays (cons overlay reb-overlays)
+		      submatches (1+ submatches))
+		(overlay-put overlay 'face face)
+		(overlay-put overlay 'priority i)))
 	    (setq i (1+ i))))))
     (let ((count (if subexp submatches matches)))
       (message "%s %smatch%s%s"
@@ -684,9 +684,22 @@
 	       (if (and reb-auto-match-limit
 			(= reb-auto-match-limit count))
 		   " (limit reached)" "")))
-    (if firstmatch
-	(progn (store-match-data firstmatch)
-	       (reb-show-subexp (or subexp 0))))))
+    (when firstmatch
+      (store-match-data firstmatch)
+      (reb-show-subexp (or subexp 0)))))
+
+;; The End
+(defun re-builder-unload-function ()
+  "Unload the RE Builder library."
+  (when (buffer-live-p (get-buffer reb-buffer))
+    (with-current-buffer reb-buffer
+      (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))))
+  ;; continue standard unloading
+  nil)
 
 (provide 're-builder)