[Top][All Lists]

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

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

From: Florian
Subject: paren-close-dwim: elisp function of a newbie; feedback welcome
Date: Wed, 25 Sep 2013 13:46:44 +0200
User-agent: Mutt/1.5.21 (2010-09-15)


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

(defun paren-close-dwim ()
  "Insert the next missing closing paranthesis based on the syntax table.
   Otherwise insert a normal closing ?\)"
    (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
            (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,

reply via email to

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