Hi,

Writing Clojure code tends to require a larger mix of "()",
"[]" and "{}" characters than other LISPs I use. This
sometimes makes it a bit tiring to write those balanced
expressions.

Writing balanced expressions has been addressed in editors
mainly by providing the automated insertion of matching
characters when you type an opening character. This kind of
support usually also comes with a fancy overloading of the
default insertion behaviour of those characters to
automatically skip extraneous ones, locking you into keeping
everything balanced all the time; I find this extremely
distracting and annoying to use, because it changes the
behaviour I expect from my editor (it doesn't *always*
insert, it is deeply troubling to me). I've tried it, and I
could not get used to it.

I came up with what I see as a better solution, and it feels
right to me: a simple command to automatically insert the
"correct" closing character at the current cursor location.
For example, invoking the same command 4 times when the cursor
is at the '|' location in the following expression will do
the right thing:

  (comment
    (use '[merced.testinput :only (protocol|

results in:

  (comment
    (use '[merced.testinput :only (protocol)]))

One advantage of this approach is the absence of "modality,"
i.e., the behaviour is the same in all contexts, e.g. when I
type to insert, it always inserts. The new command means
"insert to close the sequence here, whatever the sequence
character is."

If you want to try it, here is the corresponding Emacs code:


  (defvar close-matching-chars
    '( (?( . ?))
       (?[ . ?])
       (?{ . ?})
       (?< . >})
       ))

  (defun close-matching ()
    "Close with the most appropriate matching balanced character."
    (interactive)
    ;; Scan backwards until it stops.
    (let ((c (save-excursion
               (while (ignore-errors (forward-sexp -1) (not (<=
(point) 1))))
               (backward-char 1)
               (string-to-char (thing-at-point 'char))
               )))
      (insert-char (cdr (assoc c close-matching-chars)) 1)
      ))


Bind it to your favourite key (I use 'Ctrl-)' ):

  (global-set-key [(control \))] 'close-matching)


Have fun,

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to