emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: paren-close-dwim: elisp function of a newbie; feedback welcome


From: Andreas Röhler
Subject: Re: paren-close-dwim: elisp function of a newbie; feedback welcome
Date: Wed, 25 Sep 2013 14:18:04 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130801 Thunderbird/17.0.8

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






reply via email to

[Prev in Thread] Current Thread [Next in Thread]