changeset 7410:4eca60cb3c4d

(tempo-insert-region, tempo-show-completion-buffer, tempo-leave-completion-buffer): New variables. (tempo-complete-tag): Added a completion buffer mechanism. (tempo-display-completions): New function. (tempo-insert-template): An extension to the (p ...) tag enables named insertion for later insertion using a (s ...) tag.
author Richard M. Stallman <rms@gnu.org>
date Sun, 08 May 1994 21:11:37 +0000
parents fe56a02838ba
children 266b18250120
files lisp/tempo.el
diffstat 1 files changed, 109 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/tempo.el	Sun May 08 21:06:13 1994 +0000
+++ b/lisp/tempo.el	Sun May 08 21:11:37 1994 +0000
@@ -3,7 +3,7 @@
 
 ;; Author: David K}gedal <davidk@lysator.liu.se >
 ;; Created: 16 Feb 1994
-;; Version: 1.0
+;; Version: 1.1.1
 ;; Keywords: extensions, languages, tools
 
 ;; This file is part of GNU Emacs.
@@ -89,6 +89,20 @@
 If this variable is non-nil, `tempo-insert' prompts the
 user for text to insert in the templates")
 
+(defvar tempo-insert-region nil
+  "*Automatically insert current region when there is a `r' in the template
+If this variable is NIL, `r' elements will be treated just like `p'
+elements, unless the template function is given a prefix (or a non-nil
+argument). If this variable is non-NIL, the behaviour is reversed.")
+
+(defvar tempo-show-completion-buffer t
+  "*If non-NIL, show a buffer with possible completions, when only
+a partial completion can be found")
+
+(defvar tempo-leave-completion-buffer nil
+  "*If NIL, a completion buffer generated by \\[tempo-complete-tag]
+disappears at the next keypress; otherwise, it remains forever.")
+
 (defvar tempo-insert-string-functions nil
   "List of functions to run when inserting a string.
 Each function is called with a single arg, STRING."  )
@@ -112,6 +126,9 @@
 (defvar tempo-default-match-finder "\\b\\([^\\b]*\\)\\="
   "The default regexp used to find the string to match against the tags.")
 
+(defvar tempo-named-insertions nil
+  "Temporary storage for named insertions")
+
 ;; Make some variables local to every buffer
 
 (make-variable-buffer-local 'tempo-marks)
@@ -142,12 +159,17 @@
  - The symbol 'p. This position is saved in `tempo-marks'.
  - The symbol 'r. If `tempo-insert' is called with ON-REGION non-nil
    the current region is placed here. Otherwise it works like 'p.
- - (p . PROMPT) If `tempo-interactive' is non-nil, the user is
+ - (p PROMPT <NAME>) If `tempo-interactive' is non-nil, the user is
    prompted in the minbuffer with PROMPT for a string to be inserted.
+   If the optional parameter NAME is non-nil, the text is saved for
+   later insertion with the `s' tag.
    If `tempo-interactive is nil, it works like 'p.
- - (r . PROMPT) like the previou, but if `tempo-interactive' is nil
+ - (r PROMPT) like the previous, but if `tempo-interactive' is nil
    and `tempo-insert' is called with ON-REGION non-nil, the current
    region is placed here.
+ - (s NAME) Inserts text previously read with the (p ..) construct.
+   Finds the insertion saved under NAME and inserts it. Acts like 'p
+   if tempo-interactive is nil.
  - '& If there is only whitespace between the line start and point,
    nothing happens. Otherwise a newline is inserted.
  - '% If there is only whitespace between point and end-of-line
@@ -169,8 +191,9 @@
 				 (concat "Insert a " name "."))
 			     (list 'interactive "*P")
 			     (list 'tempo-insert-template (list 'quote
-						       template-name)
-				   'arg)))
+								template-name)
+				   (list 'if 'tempo-insert-region
+					 (list 'not 'arg) 'arg))))
     (and tag
 	 (tempo-add-tag tag template-name taglist))
     command-name))
@@ -190,7 +213,8 @@
     (mapcar 'tempo-insert 
 	    (symbol-value template))
     (tempo-insert-mark (point-marker)))
-  (tempo-forward-mark))
+  (tempo-forward-mark)
+  (tempo-forget-insertions))
 
 ;;;
 ;;; tempo-insert
@@ -206,6 +230,10 @@
 	 (if on-region
 	     (exchange-point-and-mark)
 	   (tempo-insert-prompt (cdr element))))
+	((and (consp element) (eq (car element) 's))
+	 (if tempo-interactive
+	     (tempo-insert-named (cdr element))
+	   (tempo-insert-mark (point-marker))))
 	((eq element 'p) (tempo-insert-mark (point-marker)))
 	((eq element 'r) (if on-region
 			     (exchange-point-and-mark)
@@ -234,12 +262,50 @@
 If the variable `tempo-interactive' is non-nil the user is prompted
 for a string in the minibuffer, which is then inserted in the current
 buffer. If `tempo-interactive' is nil, the current point is placed on
-`tempo-forward-mark-list'.
+`tempo-mark'.
+
+PROMPT is the prompt string or a list containing the prompt string and
+a name to save the inserted text under."
+  (if tempo-interactive
+      (let ((prompt-string (if (listp prompt)
+			       (car prompt)
+			     prompt))
+	    (save-name (and (listp prompt) (nth 1 prompt)))
+	    inserted-text)
+
+	(progn
+	  (setq inserted-text (read-string prompt-string))
+	  (insert inserted-text)
+	  (if save-name
+	      (tempo-remember-insertion save-name inserted-text))))
+    (tempo-insert-mark (point-marker))))
+
+;;;
+;;; tempo-remember-insertion
 
-PROMPT is the prompt string."
-  (if tempo-interactive
-      (insert (read-string prompt))
-    (tempo-insert-mark (point-marker))))
+(defun tempo-remember-insertion (save-name string)
+  "Save the text in STRING under the name SAVE-NAME for later retrieval."
+  (setq tempo-named-insertions (cons (cons save-name string)
+				     tempo-named-insertions)))
+
+;;;
+;;; tempo-forget-insertions
+
+(defun tempo-forget-insertions ()
+  "Forget all the saved named insertions."
+  (setq tempo-named-insertions nil))
+
+;;;
+;;; tempo-insert-named
+
+(defun tempo-insert-named (elt)
+  "Insert the previous insertion saved under a named specified in ELT.
+The name is in the car of ELT."
+  (let* ((name (car elt))
+	 (insertion (cdr (assq name tempo-named-insertions))))
+    (if insertion
+	(insert insertion)
+      (error "Named insertion not found"))))
 
 ;;;
 ;;; tempo-process-and-insert-string
@@ -248,7 +314,6 @@
   "Insert a string from a template.
 Run a string through the preprocessors in `tempo-insert-string-functions'
 and insert the results."
-
   (cond ((null tempo-insert-string-functions)
 	 nil)
 	((symbolp tempo-insert-string-functions)
@@ -381,7 +446,7 @@
 ;;; tempo-complete-tag
 
 (defun tempo-complete-tag (&optional silent)
-  "Look for a tag and expand it..
+  "Look for a tag and expand it.
 
 It goes through the tag lists in `tempo-local-tags' (this includes
 `tempo-tags') and for each list it uses the corresponding match-finder
@@ -397,8 +462,12 @@
 in `tempo-local-tags', the result may not be desirable.
 
 If no match is found or a partial match is found, and SILENT is
-non-nil, the function will give a signal."
+non-nil, the function will give a signal.
 
+If tempo-show-completion-buffer is non-NIL, a buffer containing
+possible completions is displayed when a partial completion is found."
+
+  ;; This function is really messy. Some cleaning up is necessary.
   (interactive)
   (if (catch 'completed
 	(mapcar
@@ -411,30 +480,33 @@
 		   (match-string (car match-info))
 		   (match-start (cdr match-info))
 		   (compl (or (cdr (assoc match-string tag-list))
-			      (try-completion (car match-info)
+			      (try-completion match-string
 					      tag-list))))
 	
 	      (if compl			;any match
 		  (delete-region match-start (point)))
 
 	      (cond
-	       ((null compl)
+	       ((null compl)		; No match
 		nil)
-	       ((symbolp compl)
+	       ((symbolp compl)		; ??
 		(tempo-insert-template compl nil)
 		(throw 'completed t))
-	       ((eq compl t)
+	       ((eq compl t)		; Exact, sole match
 		(tempo-insert-template (cdr (assoc match-string tag-list))
 				       nil)
 		(throw 'completed t))
-	       ((stringp compl)
+	       ((stringp compl)		; (partial) completion found
 		(let ((compl2 (assoc compl tag-list)))
 		  (if compl2
 		      (tempo-insert-template (cdr compl2) nil)
 		    (insert compl)
-		    (if (string= match-string compl)
-			(if (not silent)
-			    (ding)))))
+		    (if t ;(string= match-string compl)
+			(if tempo-show-completion-buffer
+			    (tempo-display-completions match-string
+						       tag-list)
+			  (if (not silent)
+			      (ding))))))
 		(throw 'completed t))))))
 	 tempo-local-tags)
 	;; No completion found. Return nil
@@ -446,4 +518,19 @@
 	(ding))
     nil))
 
+;;;
+;;; tempo-display-completions
+
+(defun tempo-display-completions (string tag-list)
+  "Show a buffer containing possible completions for STRING."
+  (if tempo-leave-completion-buffer
+      (with-output-to-temp-buffer "*Completions*"
+	(display-completion-list
+	 (all-completions string tag-list)))
+    (save-window-excursion
+      (with-output-to-temp-buffer "*Completions*"
+	(display-completion-list
+	 (all-completions string tag-list)))
+      (sit-for 32767))))
+
 ;;; tempo.el ends here