From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.devel Subject: Insert character pairs Date: Thu, 29 Apr 2004 11:48:48 +0300 Organization: JURTA Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <87brlb1840.fsf@mail.jurta.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1083229045 11926 80.91.224.253 (29 Apr 2004 08:57:25 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 29 Apr 2004 08:57:25 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Thu Apr 29 10:57:10 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BJ7Ly-0003Qr-00 for ; Thu, 29 Apr 2004 10:57:10 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BJ7Ly-0000Dh-00 for ; Thu, 29 Apr 2004 10:57:10 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BJ7Ir-0005Yu-Ug for emacs-devel@quimby.gnus.org; Thu, 29 Apr 2004 04:53:57 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BJ7Hm-0005EC-AB for emacs-devel@gnu.org; Thu, 29 Apr 2004 04:52:50 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BJ7Gm-0004Gn-Uo for emacs-devel@gnu.org; Thu, 29 Apr 2004 04:52:21 -0400 Original-Received: from [66.33.219.19] (helo=spoon.dreamhost.com) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BJ7Gm-0004Ft-CA for emacs-devel@gnu.org; Thu, 29 Apr 2004 04:51:48 -0400 Original-Received: from mail.jurta.org (80-235-36-125-dsl.mus.estpak.ee [80.235.36.125]) by spoon.dreamhost.com (Postfix) with ESMTP id F3BEA13D884 for ; Thu, 29 Apr 2004 01:51:51 -0700 (PDT) Original-To: emacs-devel@gnu.org User-Agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:22342 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:22342 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/