Spaces within parenthesis in emacs

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:

for( int a = 0; a < 20; a++ ) {
    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:

(defun my/c-mode-insert-space (arg)
  (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 &amp;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:

(add-hook 'c-mode-common-hook
          (lambda ()
            (local-set-key " " 'my/c-mode-insert-space)
            (local-set-key "\177" 'my/c-mode-delete-space)))
Email this to someoneShare on RedditShare on FacebookTweet about this on TwitterShare on Google+

25th November, 2016 — computing,emacs — 2 comments

comments feed trackback url

Comments

  • AngC
    7th February, 2020 at 11:04 am
    reply
    permalink

    dev.ed.am down :( can’t see contents of your posts

    • edam
      19th March, 2020 at 9:05 pm
      reply
      permalink

      Should work again!

Leave a Reply to AngC




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*/?>

Cancel Reply

    • Home
    • About Me
    • Photography
    • Software
      • Android
        • Export Contacts
        • Import Contacts
        • How to install an APK
      • GTK-based
        • Manage Raws
        • Prep. Images
      • Makefiles
        • Arduino
        • General-purpose
      • sqlite3cc
  • Page Content

  • Categories
  • Archives
  • Meta
    • log in
    • site feed
  • Thanks!
    • Send me bitcoins
    • Flattr this

powered by WordPress and Gallery2   |   hosted at waxworlds.org   |   site design by edam