From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Andreas_R=F6hler?= Newsgroups: gmane.emacs.devel Subject: Re: paren-close-dwim: elisp function of a newbie; feedback welcome Date: Wed, 25 Sep 2013 14:18:04 +0200 Message-ID: <5242D47C.4010306@online.de> 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; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1380111374 13996 80.91.229.3 (25 Sep 2013 12:16:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 25 Sep 2013 12:16:14 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 25 14:16:18 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 1VOo0v-0004j2-1L for ged-emacs-devel@m.gmane.org; Wed, 25 Sep 2013 14:16:17 +0200 Original-Received: from localhost ([::1]:52135 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOo0u-0000ah-6C for ged-emacs-devel@m.gmane.org; Wed, 25 Sep 2013 08:16:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOo0i-0000Uy-9h for emacs-devel@gnu.org; Wed, 25 Sep 2013 08:16:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOo0a-0003M5-Vi for emacs-devel@gnu.org; Wed, 25 Sep 2013 08:16:04 -0400 Original-Received: from moutng.kundenserver.de ([212.227.17.10]:57912) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOo0a-0003Lp-LZ for emacs-devel@gnu.org; Wed, 25 Sep 2013 08:15:56 -0400 Original-Received: from purzel.sitgens (brln-4dba0425.pool.mediaWays.net [77.186.4.37]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0M1ygd-1Vi7yT2hg3-00tOrS; Wed, 25 Sep 2013 14:15:55 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: <20130925114643.GA7187@csr-pc9.zib.de> X-Provags-ID: V02:K0:KK134IDX5bPzZPFycDXs1WTIcb0QzpRZYZBBlmCsnQG RFJmpdLDE4+Ry1QMW0O1z1P4qGwdaINBLwyxgL0JIlBTCVX+9f BW3TkEfvOJ2MMwNQCvgz2FyhW/JD3/GGTZoTjucVBQPzVVnTEf Cd3hZxjnic1t5aF90H+fzm0hHZfQ6UA7t89RikjF56NhQlZHSp gI+Ov0oTn5yamSC8E41yHvpw21KnWu7vMaLsb4y5Nz9dXhMldW LQPkdU+ucfbBzc1sYtyJbUWM9xEIWT1gzzvThQlZTx/ihgadFb r0YzgYYp9kfvb0c+2N03t+4waf2LxM9EWRk/HuDyd8fjdq6mdJ BRAPOHS24rsUqK9ltc5s= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 212.227.17.10 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:163631 Archived-At: Am 25.09.2013 13:46, schrieb Florian: > 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 > > Hi Florian, IMO that's quite interesting, thanks. In detail some design suggestions: - use let-bound variables instead of setq closing-paren - don't rely on syntax-table (xclusively), rather make it an universal command, relying on chars that would permit also to close "}" for example, being usable in text-modes. skip-chars-backward MY-DELIMITER_CHARS seems an option. Andreas