From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Florian Newsgroups: gmane.emacs.devel Subject: paren-close-dwim: elisp function of a newbie; feedback welcome Date: Wed, 25 Sep 2013 13:46:44 +0200 Message-ID: <20130925114643.GA7187@csr-pc9.zib.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1380109626 24175 80.91.229.3 (25 Sep 2013 11:47:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 25 Sep 2013 11:47:06 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 25 13:47:10 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 1VOnYh-0007C5-Jd for ged-emacs-devel@m.gmane.org; Wed, 25 Sep 2013 13:47:07 +0200 Original-Received: from localhost ([::1]:51668 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOnYh-00070L-6U for ged-emacs-devel@m.gmane.org; Wed, 25 Sep 2013 07:47:07 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58691) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOnYV-0006zJ-N1 for emacs-devel@gnu.org; Wed, 25 Sep 2013 07:47:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOnYM-0001uX-Tu for emacs-devel@gnu.org; Wed, 25 Sep 2013 07:46:55 -0400 Original-Received: from mail-bk0-x244.google.com ([2a00:1450:4008:c01::244]:52259) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOnYM-0001rj-MK for emacs-devel@gnu.org; Wed, 25 Sep 2013 07:46:46 -0400 Original-Received: by mail-bk0-f68.google.com with SMTP id mx10so724190bkb.3 for ; Wed, 25 Sep 2013 04:46:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:mime-version:content-type :content-disposition:user-agent; bh=ovlWrdNgkDlp/1zXesdhJiC/J7vRci98LBAMWD72gM0=; b=IcGaxPrK+ipZ5rFjrfmJ7WMF5btgGEWmXdpKWWbgdbgehwoe6pXDUJmY+dGjoMayzu TX5Isjx3l7F+UXPbOI40x7ZgMR+lAMccspo4eGNSq+7kzCHZrvOcPNO1HF/cpkKjbUXN krQ/KoUC9AgCNAfmGf90H17/DvtyOz8/9m4UiFExm9zxiN9vBQbrymZUgCE5fUCsHTUf 9fPR48LgoZ2tpjNlrfFsuWqg7oKaYzeI+naqkNh5JyqIWIZ+R9uU294vujyyc/mTVj2t i3KvoMd9aUx/OaSRvgHOHKyYsqHu4+Bm/D47WVZHTgXP26o7+SyZlLrM0C+G20g0uSfR Fmmg== X-Received: by 10.204.69.12 with SMTP id x12mr26815293bki.12.1380109604978; Wed, 25 Sep 2013 04:46:44 -0700 (PDT) Original-Received: from localhost (csr-pc9.zib.de. [130.73.78.129]) by mx.google.com with ESMTPSA id zl3sm14023793bkb.4.1969.12.31.16.00.00 (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 25 Sep 2013 04:46:44 -0700 (PDT) Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4008:c01::244 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:163630 Archived-At: Hi, here is my first elisp function 'paren-close-dwim' and I would be glad to get some feedback on it (whether its done to complicated, in an unusual way, or whether this functionality is already available in some emacs extension which I have not found). The function allows to close braces/brackets/paranthesis without the user to care which kind of brace actually has to be closed next. For me this is much handier than using paren-mode or auto-pair etc, especially when modifying existing code. Automatic paren modes came in the way and did the wrong thing from time to time for me and I had to delete automatically inserted closing paranthesis which was hindering my workflow. So, to be honest, I were not able to master them appropriately enough with my muscle memory. I work with a German keyboard layout where braces are not that easily reachable as on the Englisch layout, but I want to stick with this and just want to avoid some uncomfortable key-strokes for closing braces. So, here is my function paren-close-dwim, which you can freely pick and use for your own configuration. Maybe it is a bit 'over-commented', which only represents my still lacking elisp fluency. (defun paren-close-dwim () "Insert the next missing closing paranthesis based on the syntax table. Otherwise insert a normal closing ?\)" (interactive) (save-excursion (setq fallback-char ?\)) ;; 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)) (if (cdr syntax-cons) (cdr syntax-cons) ;; if cdr is nil use the fallback-char fallback-char)) (error fallback-char)))) ;; insert dwim parenthesis (insert closing-paren)) ;; I bind this to the normal closing paranthesis key and am quite happy ;; with its behaviour in several different modes since a few weeks now. (global-set-key (kbd ")") 'paren-close-dwim) Thanks for your feedback, Florian