There’s lots of opinion about coding styles and which is best. I prefer one that helps you read the code. And, to that end, I prefer to have spaces inside parenthesis, like this:
dump( data[ a ] );
}
Call me a heathen if you like, but it’s how I roll!
And it’s how I set out to configure emails to help me roll…
Unfortunately, emacs’s electric-pair-mode
doesn’t do this sort of thing and can’t be configured to, either. There are two problems. The first is that electric-pair-mode
doesn’t allow for conditions as to when it will insert a pair. So, I could add a pair of spaces, (?\s . ?\s)
, to electric-pair-pairs
so that it would automatically insert a second space, but it would do this for every space I typed. The second problem is that electric-pair-mode
does some white-space skipping internally, which I think would be problematic. :(
So I did it the old-fashioned way and added my own functions:
(interactive "*P")
(let ((prev (char-before))
(next (char-after)))
(self-insert-command (prefix-numeric-value arg))
(if (and prev next
(string-match-p "[[({]" (string prev))
(string-match-p "[])}]" (string next)))
(save-excursion (self-insert-command 1)))))
(defun my/c-mode-delete-space (arg &optional killp)
(interactive "*p\nP")
(let ((prev (char-before))
(next (char-after))
(pprev (char-before (- (point) 1))))
(if (and prev next pprev
(char-equal prev ?\s) (char-equal next ?\s)
(string-match "[[({]" (string pprev)))
(delete-char 1))
(backward-delete-char-untabify arg killp)))
They can be bound in c-mode
and c-mode
derivatives like this:
(lambda ()
(local-set-key " " 'my/c-mode-insert-space)
(local-set-key "\177" 'my/c-mode-delete-space)))
dev.ed.am down :( can’t see contents of your posts
Should work again!