all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sam Halliday <sam.halliday@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: auto indenting code blocks
Date: Sat, 13 Jun 2015 03:40:16 -0700 (PDT)	[thread overview]
Message-ID: <43ca5eee-94a4-47f3-8b69-3af8a38227cd@googlegroups.com> (raw)

Hi all,

I find it extremely convenient when writing Scala code (and this may be relevant in all c-mode derivations) to be able to have my code blocks automatically expanded.

I have smart-parens installed, so when I type `{' the closing brace is placed after point.

But often I intend to write a multi-line block of code (this may well be the norm in C and C++) and when I type newline I expect the second brace to be moved down a line, indented, and the point placed, indented, in the middle.

For example, let's say we start with this (point denoted by pipe)

  blah |

Type `{' and smart-parens will insert the closing brace

  blah {|}

I'd actually prefer it to be, so if anybody knows how to do that I'd be greatly appreciative of their advice.

  blah { | }

Then I hit newline and this happens

  blah {
  |}

But I *actually* want

  blah {
    |
  }


I have cooked up a little post-self-insert-hook (see bottom of mail) but it feels like I'm inventing some form of wheel.

  1. is there an existing preferred way to do what my scala-block-indent is trying to do?
  2. is there an easier way (perhaps via smart-parens) to achieve what my scala-block-pad is doing, but at the point when the closing brace is inserted?



(defun scala-block-indent ()
  "It is convenient for newlines that follow a curly bracket to be indented."
  (when (and (eq major-mode 'scala-mode)
             ;; ordered for performance
             (looking-at "}") (looking-back "{[[:space:]]*\n"))
    (indent-according-to-mode)
    (forward-line -1)
    (move-end-of-line nil)
    (newline-and-indent)))
(add-hook 'post-self-insert-hook 'scala-block-indent)

(defun scala-block-pad ()
  "It is convenient for spaces following a brace to be space padded."
  (when (and (eq major-mode 'scala-mode)
             ;; ordered for performance
             (looking-at "}") (looking-back "{[[:space:]]"))
    (insert-char ?\s)
    (backward-char)))
(add-hook 'post-self-insert-hook 'scala-block-pad)

Best regards,
Sam


             reply	other threads:[~2015-06-13 10:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-13 10:40 Sam Halliday [this message]
2015-06-13 11:25 ` auto indenting code blocks Yuri Khan
2015-06-13 11:55   ` Michael Heerdegen
2015-06-13 13:14     ` Yuri Khan
2015-06-13 23:30       ` Michael Heerdegen
2015-06-14  5:24         ` Yuri Khan
     [not found]         ` <mailman.4973.1434259496.904.help-gnu-emacs@gnu.org>
2015-06-14 19:54           ` Sam Halliday
2015-06-14 19:59           ` Stefan Monnier

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=43ca5eee-94a4-47f3-8b69-3af8a38227cd@googlegroups.com \
    --to=sam.halliday@gmail.com \
    --cc=help-gnu-emacs@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.