unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Insert character pairs
@ 2004-04-29  8:48 Juri Linkov
  2004-04-29 10:12 ` Andreas Schwab
                   ` (3 more replies)
  0 siblings, 4 replies; 29+ messages in thread
From: Juri Linkov @ 2004-04-29  8:48 UTC (permalink / raw)


It is useful for efficient editing to be able to insert a pair of
symmetrical characters.  Emacs already provides the command M-( to
insert parentheses, but it would be good to extend this command for
other paired characters like quotes and brackets.

The following patch generalizes the function `insert-parentheses'
by changing its name, adding new arguments, and creating the
new function `insert-parentheses' with the old name that calls it.
Also it adds the functions for some most frequent character pairs
to be able to bind them to the keys like M-", M-', M-`, M-[.

And taking into account active regions is useful too.

Index: lisp/emacs-lisp/lisp.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/emacs-lisp/lisp.el,v
retrieving revision 1.52
diff -u -w -b -r1.52 lisp.el
--- lisp/emacs-lisp/lisp.el	14 Apr 2004 18:20:23 -0000	1.52
+++ lisp/emacs-lisp/lisp.el	29 Apr 2004 06:20:33 -0000
@@ -302,7 +306,7 @@
       (end-of-defun)
       (narrow-to-region beg (point)))))
 
-(defun insert-parentheses (arg)
+(defun insert-pair (arg &optional open close)
   "Enclose following ARG sexps in parentheses.  Leave point after open-paren.
 A negative ARG encloses the preceding ARG sexps instead.
 No argument is equivalent to zero: just insert `()' and leave point between.
@@ -311,20 +315,66 @@
   (interactive "P")
   (if arg (setq arg (prefix-numeric-value arg))
     (setq arg 0))
+  (or open  (setq open  ?\())
+  (or close (setq close ?\)))
+  (if (and transient-mark-mode mark-active)
+      (progn
+        (save-excursion (goto-char (region-beginning)) (insert open))
+        (save-excursion (goto-char (region-end))       (insert close)))
   (cond ((> arg 0) (skip-chars-forward " \t"))
 	((< arg 0) (forward-sexp arg) (setq arg (- arg))))
   (and parens-require-spaces
        (not (bobp))
-       (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
+       (memq (char-syntax (preceding-char)) (list ?w ?_ close))
        (insert " "))
-  (insert ?\()
+  (insert open)
   (save-excursion
     (or (eq arg 0) (forward-sexp arg))
-    (insert ?\))
+    (insert close)
     (and parens-require-spaces
 	 (not (eobp))
-	 (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
-	 (insert " "))))
+        (memq (char-syntax (following-char)) (list ?w ?_ open))
+        (insert " ")))))
+
+(defun insert-parentheses (arg)
+  "Insert a pair of parentheses."
+  (interactive "P")
+  (insert-pair arg ?\( ?\)))
+
+(defun insert-double-quotes (arg)
+  "Insert a pair of double quotes."
+  (interactive "P")
+  (insert-pair arg ?\" ?\"))
+
+(defun insert-backquotes (arg)
+  "Insert a pair of backquotes."
+  (interactive "P")
+  (insert-pair arg ?\` ?\`))
+
+(defun insert-backquote-and-quote (arg)
+  "Insert backquote and quote."
+  (interactive "P")
+  (insert-pair arg ?\` ?\'))
+
+(defun insert-apostrophes (arg)
+  "Insert a pair of apostrophes."
+  (interactive "P")
+  (insert-pair arg ?\' ?\'))
+
+(defun insert-square-brackets (arg)
+  "Insert a pair of square brackets."
+  (interactive "P")
+  (insert-pair arg ?\[ ?\]))
+
+(defun insert-curly-brackets (arg)
+  "Insert a pair of curly brackets."
+  (interactive "P")
+  (insert-pair arg ?\{ ?\}))
+
+(defun insert-angle-brackets (arg)
+  "Insert a pair of angle brackets."
+  (interactive "P")
+  (insert-pair arg ?\< ?\>))

-- 
Juri Linkov
http://www.jurta.org/emacs/

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2004-05-08 21:43 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-29  8:48 Insert character pairs Juri Linkov
2004-04-29 10:12 ` Andreas Schwab
2004-04-29 11:00   ` Juri Linkov
2004-04-29 11:21     ` Andreas Schwab
2004-04-29 11:59       ` Juri Linkov
2004-04-29 14:24 ` Stefan Monnier
2004-04-29 15:02   ` Juri Linkov
2004-04-29 16:57     ` Stefan Monnier
2004-04-30  0:48       ` Juri Linkov
2004-04-30 13:43         ` Stefan Monnier
2004-04-30 23:22           ` Juri Linkov
2004-05-01  8:19 ` Juri Linkov
2004-05-02 16:19   ` Juri Linkov
2004-05-03 14:03     ` Richard Stallman
2004-05-04 15:57     ` Johan Bockgård
2004-05-04 16:48       ` Juri Linkov
2004-05-05 20:20       ` Richard Stallman
2004-05-06 13:14         ` Juri Linkov
2004-05-06 17:19           ` Stefan Monnier
2004-05-08 21:43             ` Juri Linkov
2004-05-03 19:48   ` Kevin Rodgers
2004-05-04  5:51     ` Juri Linkov
2004-05-04 18:30       ` Kevin Rodgers
2004-05-04 19:32         ` Juri Linkov
2004-05-05 20:20           ` Richard Stallman
2004-05-06 12:02             ` Juri Linkov
2004-05-01 20:26 ` Stefan Monnier
2004-05-02 16:03   ` Juri Linkov
2004-05-02 16:41     ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).