From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Davis Herring Newsgroups: gmane.emacs.devel Subject: Re: paren-close-dwim: elisp function of a newbie; feedback welcome Date: Wed, 25 Sep 2013 11:32:21 -0600 Organization: XCP-1 Message-ID: <52431E25.3030603@lanl.gov> References: <20130925114643.GA7187@csr-pc9.zib.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1380130367 4502 80.91.229.3 (25 Sep 2013 17:32:47 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 25 Sep 2013 17:32:47 +0000 (UTC) Cc: emacs-devel@gnu.org To: Florian Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 25 19:32:49 2013 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VOsxD-0004d1-Vn for ged-emacs-devel@m.gmane.org; Wed, 25 Sep 2013 19:32:48 +0200 Original-Received: from localhost ([::1]:54112 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOsxD-0004KE-Ik for ged-emacs-devel@m.gmane.org; Wed, 25 Sep 2013 13:32:47 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOsx3-0004Jy-Om for emacs-devel@gnu.org; Wed, 25 Sep 2013 13:32:46 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOswt-0007oQ-A9 for emacs-devel@gnu.org; Wed, 25 Sep 2013 13:32:37 -0400 Original-Received: from proofpoint5.lanl.gov ([204.121.3.53]:44244) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOswt-0007nI-22 for emacs-devel@gnu.org; Wed, 25 Sep 2013 13:32:27 -0400 Original-Received: from mailrelay1.lanl.gov (mailrelay1.lanl.gov [128.165.4.101]) by mailgate5.lanl.gov (8.14.5/8.14.5) with ESMTP id r8PHWL0v013687; Wed, 25 Sep 2013 11:32:21 -0600 Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by mailrelay1.lanl.gov (Postfix) with ESMTP id 886892739FD; Wed, 25 Sep 2013 11:32:21 -0600 (MDT) X-NIE-2-Virus-Scanner: amavisd-new at mailrelay1.lanl.gov Original-Received: from [128.165.123.183] (xray-r06.lanl.gov [128.165.123.183]) by mailrelay1.lanl.gov (Postfix) with ESMTP id 76FAF2739F9; Wed, 25 Sep 2013 11:32:21 -0600 (MDT) User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110717 Lanikai/3.1.11 In-Reply-To: <20130925114643.GA7187@csr-pc9.zib.de> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8794, 1.0.431, 0.0.0000 definitions=2013-09-25_08:2013-09-25, 2013-09-25, 1970-01-01 signatures=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 204.121.3.53 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:163641 Archived-At: > (defun paren-close-dwim () > "Insert the next missing closing paranthesis based on the syntax table. Typo: "parenthesis". > Otherwise insert a normal closing ?\)" Don't indent docstring lines. > (interactive) Use "*" to get an immediate error if read-only. You might also consider allowing a prefix count like `self-insert-command'. > (save-excursion > (setq fallback-char ?\)) Use `let' to introduce local variables. However, here you'll be able to do without any... > ;; go backward up a level in the parenthesis hierarchy (which > ;; jumps to the next not yet closed (seen from point) open > ;; parenthesis). Catch unbalanced paranthesis error etc. > (setq closing-paren > (condition-case nil > (progn (backward-up-list) > ;; get character at point > (setq open-paren (point)) > ;; get corresponding closing character from the > ;; syntax table. (syntax-after open-paren) > ;; delivers a cons cell with (OPEN . CLOSE), so > ;; we need the cdr to match open-paren. > (setq syntax-cons (syntax-after open-paren)) Since you only assign and use `open-paren' once, just insert the expression for it in place of the variable. > (if (cdr syntax-cons) > (cdr syntax-cons) > ;; if cdr is nil use the fallback-char > fallback-char)) (or (cdr syntax-cons) fallback-char) This then allows you to drop the `syntax-cons' variable. > (error fallback-char)))) Use `ignore-errors' and promote the `or`: (or (ignore-errors (backward-up-list) (cdr (syntax-after (point)))) fallback-char) Then you can get rid of `fallback-char' too. > ;; insert dwim parenthesis > (insert closing-paren)) Similarly, you can do without the `closing-paren' variable. Indentation error: the `insert' is not inside the `save-excursion'. The final version I get (narrowly wrapped for email): (defun paren-close-dwim () "Insert closing parenthesis from syntax table. Use a normal parenthesis if not inside any." (interactive "*") (insert (or (ignore-errors (save-excursion (backward-up-list) (cdr (syntax-after (point))))) ?\)))) Davis -- This product is sold by volume, not by mass. If it appears too dense or too sparse, it is because mass-energy conversion has occurred during shipping.