changeset 51325:6ef863c4a5d8

(forms-mode-hook): Rename from forms-mode-hooks. (forms-mode): Use add-hook rather than make-local-variable+setq. Use with-current-buffer. Run the new and the old hooks. (forms--update): Use with-current-buffer and line-end-position. (forms--goto-record): New fun. (forms-jump-record, forms-insert-record, forms-delete-record): Use it. (forms--process-format-list): Remove unused var `this-item'. (forms--intuit-from-file): Remove unused var `the-result'. (forms--trans): Remove unused var `x'. (forms--exit, forms-exit, forms-exit-no-save): Remove unused arg.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Thu, 29 May 2003 23:53:21 +0000
parents dfd8b52708ae
children 92b845a3d3a6
files lisp/forms.el
diffstat 1 files changed, 49 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/forms.el	Thu May 29 23:29:29 2003 +0000
+++ b/lisp/forms.el	Thu May 29 23:53:21 2003 +0000
@@ -301,12 +301,12 @@
 (provide 'forms)			;;; official
 (provide 'forms-mode)			;;; for compatibility
 
-(defconst forms-version (substring "$Revision: 2.46 $" 11 -2)
+(defconst forms-version (substring "$Revision: 2.47 $" 11 -2)
   "The version number of forms-mode (as string).  The complete RCS id is:
 
-  $Id: forms.el,v 2.46 2003/05/23 12:48:06 rms Exp $")
+  $Id: forms.el,v 2.47 2003/05/28 11:19:48 rms Exp $")
 
-(defcustom forms-mode-hooks nil
+(defcustom forms-mode-hook nil
   "Hook run upon entering Forms mode."
   :group 'forms
   :type 'hook)
@@ -648,30 +648,24 @@
   (let ((read-file-filter forms-read-file-filter)
 	(write-file-filter forms-write-file-filter))
     (if read-file-filter
-	(save-excursion
-	  (set-buffer forms--file-buffer)
+	(with-current-buffer forms--file-buffer
 	  (let ((inhibit-read-only t)
 		(file-modified (buffer-modified-p)))
 	    (run-hooks 'read-file-filter)
 	    (if (not file-modified) (set-buffer-modified-p nil)))
 	  (if write-file-filter
-	      (progn
-		(make-local-variable 'write-file-functions)
-		(setq write-file-functions (list write-file-filter)))))
+	      (add-hook 'write-file-functions write-file-filter nil t)))
       (if write-file-filter
-	  (save-excursion
-	    (set-buffer forms--file-buffer)
-	    (make-local-variable 'write-file-functions)
-	    (setq write-file-functions (list write-file-filter))))))
+	  (with-current-buffer forms--file-buffer
+	    (add-hook 'write-file-functions write-file-filter nil t)))))
 
   ;; count the number of records, and set see if it may be modified
   (let (ro)
     (setq forms--total-records
-	  (save-excursion
+	  (with-current-buffer forms--file-buffer
 	    (prog1
 		(progn
 		  ;;(message "forms: counting records...")
-		  (set-buffer forms--file-buffer)
 		  (bury-buffer (current-buffer))
 		  (setq ro buffer-read-only)
 		  (count-lines (point-min) (point-max)))
@@ -724,7 +718,7 @@
 
   ;; user customising
   ;;(message "forms: proceeding setup (user hooks)...")
-  (run-hooks 'forms-mode-hooks)
+  (run-hooks 'forms-mode-hook 'forms-mode-hooks)
   ;;(message "forms: setting up... done.")
 
   ;; be helpful
@@ -757,7 +751,6 @@
   (setq forms--elements (make-vector forms-number-of-fields nil))
 
   (let ((the-list forms-format-list)	; the list of format elements
-	(this-item 0)			; element in list
 	(prev-item nil)
 	(field-num 0))			; highest field number
 
@@ -1226,8 +1219,7 @@
 	(let ((read-file-filter forms-read-file-filter)
 	      (the-record))
 	  (setq the-record
-		(save-excursion
-		  (set-buffer forms--file-buffer)
+		(with-current-buffer forms--file-buffer
 		  (let ((inhibit-read-only t))
 		    (run-hooks 'read-file-filter))
 		  (goto-char (point-min))
@@ -1238,8 +1230,7 @@
 	  (kill-buffer forms--file-buffer)
 
 	  ;; Count the number of fields in `the-record'.
-	  (let (the-result
-		(start-pos 0)
+	  (let ((start-pos 0)
 		found-pos
 		(field-sep-length (length forms-field-sep)))
 	    (setq forms-number-of-fields 1)
@@ -1453,14 +1444,13 @@
   "Translate in SUBJ all chars ARG into char REP.  ARG and REP should
  be single-char strings."
   (let ((i 0)
-	(x (length subj))
 	(re (regexp-quote arg))
 	(k (string-to-char rep)))
     (while (setq i (string-match re subj i))
       (aset subj i k)
       (setq i (1+ i)))))
 
-(defun forms--exit (query &optional save)
+(defun forms--exit (&optional save)
   "Internal exit from forms mode function."
 
   (let ((buf (buffer-name forms--file-buffer)))
@@ -1468,8 +1458,7 @@
     (if (and save
 	     (buffer-modified-p forms--file-buffer))
 	(forms-save-buffer))
-    (save-excursion
-      (set-buffer forms--file-buffer)
+    (with-current-buffer forms--file-buffer
       (delete-auto-save-file-if-necessary)
       (kill-buffer (current-buffer)))
     (if (get-buffer buf)	; not killed???
@@ -1596,12 +1585,10 @@
     (if (string-match "\n" the-record)
 	(error "Multi-line fields in this record - update refused"))
 
-    (save-excursion
-      (set-buffer forms--file-buffer)
+    (with-current-buffer forms--file-buffer
       ;; Use delete-region instead of kill-region, to avoid
       ;; adding junk to the kill-ring.
-      (delete-region (save-excursion (beginning-of-line) (point))
-		     (save-excursion (end-of-line) (point)))
+      (delete-region (line-beginning-position) (line-end-position))
       (insert the-record)
       (beginning-of-line))))
 
@@ -1633,15 +1620,15 @@
     (find-file-other-window fn)
     (or forms--mode-setup (forms-mode t))))
 
-(defun forms-exit (query)
+(defun forms-exit ()
   "Normal exit from Forms mode.  Modified buffers are saved."
-  (interactive "P")
-  (forms--exit query t))
+  (interactive)
+  (forms--exit t))
 
-(defun forms-exit-no-save (query)
+(defun forms-exit-no-save ()
   "Exit from Forms mode without saving buffers."
-  (interactive "P")
-  (forms--exit query nil))
+  (interactive)
+  (forms--exit nil))
 
 ;;; Navigating commands
 
@@ -1655,6 +1642,16 @@
   (interactive "P")
   (forms-jump-record (- forms--current-record (prefix-numeric-value arg)) t))
 
+(defun forms--goto-record (rn &optional current)
+  "Goto record number RN.
+If CURRENT is provided, it specifies the current record and can be used
+to speed up access to RN.  Returns the number of records missing, if any."
+  (if current
+      (forward-line (- rn current))
+    ;; goto-line does not do what we want when the buffer is narrowed.
+    (goto-char (point-min))
+    (forward-line (1- rn))))
+
 (defun forms-jump-record (arg &optional relative)
   "Jump to a random record."
   (interactive "NRecord number: ")
@@ -1673,25 +1670,18 @@
   (forms--checkmod)
 
   ;; Calculate displacement.
-  (let ((disp (- arg forms--current-record))
-	(cur forms--current-record))
+  (let ((cur forms--current-record))
 
     ;; `forms--show-record' needs it now.
     (setq forms--current-record arg)
 
     ;; Get the record and show it.
     (forms--show-record
-     (save-excursion
-       (set-buffer forms--file-buffer)
+     (with-current-buffer forms--file-buffer
        (beginning-of-line)
 
        ;; Move, and adjust the amount if needed (shouldn't happen).
-       (if relative
-	   (if (zerop disp)
-	       nil
-	     (setq cur (+ cur disp (- (forward-line disp)))))
-	 (goto-char (point-min))
-	 (setq cur (+ cur disp (- (forward-line (1- arg))))))
+       (setq cur (- arg (forms--goto-record arg (if relative cur))))
 
        (forms--get-record)))
 
@@ -1712,8 +1702,7 @@
   (interactive)
   (let
       ((numrec
-	(save-excursion
-	  (set-buffer forms--file-buffer)
+	(with-current-buffer forms--file-buffer
 	  (count-lines (point-min) (point-max)))))
     (if (= numrec forms--total-records)
 	nil
@@ -1738,8 +1727,7 @@
 
       ;; Enable edit mode, if possible.
       (let ((ro forms-read-only))
-	(if (save-excursion
-	      (set-buffer forms--file-buffer)
+	(if (with-current-buffer forms--file-buffer
 	      buffer-read-only)
 	    (progn
 	      (setq forms-read-only t)
@@ -1799,10 +1787,8 @@
 	  the-list
 	  forms-field-sep))
 
-    (save-excursion
-      (set-buffer forms--file-buffer)
-      (goto-char (point-min))
-      (forward-line (1- ln))
+    (with-current-buffer forms--file-buffer
+      (forms--goto-record ln)
       (open-line 1)
       (insert the-record)
       (beginning-of-line))
@@ -1823,10 +1809,8 @@
   (if (or arg
 	  (y-or-n-p "Really delete this record? "))
       (let ((ln forms--current-record))
-	(save-excursion
-	  (set-buffer forms--file-buffer)
-	  (goto-char (point-min))
-	  (forward-line (1- ln))
+	(with-current-buffer forms--file-buffer
+	  (forms--goto-record ln)
 	  ;; Use delete-region instead of kill-region, to avoid
 	  ;; adding junk to the kill-ring.
 	  (delete-region (progn (beginning-of-line) (point))
@@ -1850,10 +1834,8 @@
       (setq regexp forms--search-regexp))
   (forms--checkmod)
 
-  (let (the-line the-record here
-		 (fld-sep forms-field-sep))
-    (save-excursion
-      (set-buffer forms--file-buffer)
+  (let (the-line the-record here)
+    (with-current-buffer forms--file-buffer
       (end-of-line)
       (setq here (point))
       (if (or (re-search-forward regexp nil t)
@@ -1886,10 +1868,8 @@
       (setq regexp forms--search-regexp))
   (forms--checkmod)
 
-  (let (the-line the-record here
-		 (fld-sep forms-field-sep))
-    (save-excursion
-      (set-buffer forms--file-buffer)
+  (let (the-line the-record here)
+    (with-current-buffer forms--file-buffer
       (beginning-of-line)
       (setq here (point))
       (if (or (re-search-backward regexp nil t)
@@ -1919,10 +1899,9 @@
   (let ((write-file-filter forms-write-file-filter)
 	(read-file-filter forms-read-file-filter)
 	(cur forms--current-record))
-    (save-excursion
-      (set-buffer forms--file-buffer)
+    (with-current-buffer forms--file-buffer
       (let ((inhibit-read-only t))
-	;; Write file hooks are run via local-write-file-hooks.
+	;; Write file hooks are run via write-file-functions.
 	;; (if write-file-filter
 	;;  (save-excursion
 	;;   (run-hooks 'write-file-filter)))
@@ -2019,16 +1998,14 @@
     (while (<= nb-record forms--total-records)
       (forms-jump-record nb-record)
       (setq record (buffer-string))
-      (save-excursion
-	(set-buffer (get-buffer-create "*forms-print*"))
+      (with-current-buffer (get-buffer-create "*forms-print*")
 	(goto-char (buffer-end 1))
 	(insert record)
 	(setq buffer-read-only nil)
 	(if (< nb-record total-nb-records)
 	    (insert "\n\n")))
       (setq nb-record (1+ nb-record)))
-    (save-excursion
-      (set-buffer "*forms-print*")
+    (with-current-buffer "*forms-print*"
       (print-buffer)
       (set-buffer-modified-p nil)
       (kill-buffer (current-buffer)))
@@ -2076,8 +2053,7 @@
 	      (if (fboundp el)
 		  (setq ret (concat ret (prin1-to-string (symbol-function el))
 				    "\n"))))))
-	(save-excursion
-	  (set-buffer (get-buffer-create "*forms-mode debug*"))
+	(with-current-buffer (get-buffer-create "*forms-mode debug*")
 	  (if (zerop (buffer-size))
 	      (emacs-lisp-mode))
 	  (goto-char (point-max))