all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Florian <floriansbriefe@gmail.com>
To: emacs-devel@gnu.org
Subject: paren-close-dwim: elisp function of a newbie; feedback welcome
Date: Wed, 25 Sep 2013 13:46:44 +0200	[thread overview]
Message-ID: <20130925114643.GA7187@csr-pc9.zib.de> (raw)

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



             reply	other threads:[~2013-09-25 11:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-25 11:46 Florian [this message]
2013-09-25 12:18 ` paren-close-dwim: elisp function of a newbie; feedback welcome Andreas Röhler
2013-09-25 17:32 ` Davis Herring
2013-09-26 19:54   ` Harry Putnam
2013-09-26 20:38     ` Davis Herring
2013-09-26 20:44       ` Florian
2013-09-27  5:51         ` Andreas Röhler
2013-09-29  9:36           ` Ted Zlatanov
2013-09-27 21:33         ` Harry Putnam
2013-09-29 10:11         ` Óscar Fuentes
2013-09-27 21:30       ` Harry Putnam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130925114643.GA7187@csr-pc9.zib.de \
    --to=floriansbriefe@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.