unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* paren-close-dwim: elisp function of a newbie; feedback welcome
@ 2013-09-25 11:46 Florian
  2013-09-25 12:18 ` Andreas Röhler
  2013-09-25 17:32 ` Davis Herring
  0 siblings, 2 replies; 11+ messages in thread
From: Florian @ 2013-09-25 11:46 UTC (permalink / raw)
  To: emacs-devel

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



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-09-29 10:11 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-25 11:46 paren-close-dwim: elisp function of a newbie; feedback welcome Florian
2013-09-25 12:18 ` 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

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).