* lisp/tempo.el (tempo-insert): Use pcase and dolist

This commit is contained in:
Stefan Monnier 2023-09-08 19:41:28 -04:00
parent 740a1e8f33
commit 285210bee5

View file

@ -321,72 +321,57 @@ elements are replaced with the current region.
See documentation for `tempo-define-template' for the kind of elements
possible."
(cond ((stringp element) (tempo-process-and-insert-string element))
((and (consp element)
(eq (car element) 'p)) (tempo-insert-prompt-compat
(cdr element)))
((and (consp element)
(eq (car element) 'P)) (let ((tempo-interactive t))
(tempo-insert-prompt-compat
(cdr element))))
;;; ((and (consp element)
;;; (eq (car element) 'v)) (tempo-save-named
;;; (nth 1 element)
;;; nil
;;; (nth 2 element)))
((and (consp element)
(eq (car element) 'r)) (if on-region
(goto-char tempo-region-stop)
(tempo-insert-prompt-compat
(cdr element))))
((and (consp element)
(eq (car element) 'r>)) (if on-region
(progn
(goto-char tempo-region-stop)
(indent-region (mark) (point) nil))
(tempo-insert-prompt-compat
(cdr element))))
((and (consp element)
(eq (car element) 's)) (tempo-insert-named (car (cdr element))))
((and (consp element)
(eq (car element) 'l)) (mapcar (lambda (elt)
(tempo-insert elt on-region))
(cdr element)))
((eq element 'p) (tempo-insert-mark (point-marker)))
((eq element 'r) (if on-region
(goto-char tempo-region-stop)
(tempo-insert-mark (point-marker))))
((eq element 'r>) (if on-region
(progn
(goto-char tempo-region-stop)
(indent-region (mark) (point) nil))
(tempo-insert-mark (point-marker))))
((eq element '>) (indent-according-to-mode))
((eq element '&) (if (not (or (= (current-column) 0)
(save-excursion
(re-search-backward
"^\\s-*\\=" nil t))))
(insert "\n")))
((eq element '%) (if (not (or (eolp)
(save-excursion
(re-search-forward
"\\=\\s-*$" nil t))))
(insert "\n")))
((eq element 'n) (insert "\n"))
((eq element 'n>) (insert "\n") (indent-according-to-mode))
;; Bug: If the 'o is the first element in a template, strange
;; things can happen when the template is inserted at the
;; beginning of a line.
((eq element 'o) (if (not (or on-region
(eolp)
(save-excursion
(re-search-forward
"\\=\\s-*$" nil t))))
(open-line 1)))
((null element))
(t (tempo-insert (or (tempo-is-user-element element)
(eval element t))
on-region))))
(pcase element
((pred stringp) (tempo-process-and-insert-string element))
(`(p . ,rest) (tempo-insert-prompt-compat rest))
(`(P . ,rest) (let ((tempo-interactive t))
(tempo-insert-prompt-compat rest)))
;; (`(v ,name ,data) (tempo-save-named name nil data))
(`(r . ,rest) (if on-region
(goto-char tempo-region-stop)
(tempo-insert-prompt-compat rest)))
(`(r> . ,rest) (if on-region
(progn
(goto-char tempo-region-stop)
(indent-region (mark) (point) nil))
(tempo-insert-prompt-compat rest)))
(`(s ,name) (tempo-insert-named name))
(`(l . ,rest) (dolist (elt rest) (tempo-insert elt on-region)))
('p (tempo-insert-mark (point-marker)))
('r (if on-region
(goto-char tempo-region-stop)
(tempo-insert-mark (point-marker))))
('r> (if on-region
(progn
(goto-char tempo-region-stop)
(indent-region (mark) (point) nil))
(tempo-insert-mark (point-marker))))
('> (indent-according-to-mode))
('& (if (not (or (= (current-column) 0)
(save-excursion
(re-search-backward
"^\\s-*\\=" nil t))))
(insert "\n")))
('% (if (not (or (eolp)
(save-excursion
(re-search-forward
"\\=\\s-*$" nil t))))
(insert "\n")))
('n (insert "\n"))
('n> (insert "\n") (indent-according-to-mode))
;; Bug: If the 'o is the first element in a template, strange
;; things can happen when the template is inserted at the
;; beginning of a line.
('o (if (not (or on-region
(eolp)
(save-excursion
(re-search-forward
"\\=\\s-*$" nil t))))
(open-line 1)))
('nil nil)
(_ (tempo-insert (or (tempo-is-user-element element)
(eval element t))
on-region))))
;;;
;;; tempo-insert-prompt