all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* auto indenting code blocks
@ 2015-06-13 10:40 Sam Halliday
  2015-06-13 11:25 ` Yuri Khan
  0 siblings, 1 reply; 8+ messages in thread
From: Sam Halliday @ 2015-06-13 10:40 UTC (permalink / raw
  To: help-gnu-emacs

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


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

end of thread, other threads:[~2015-06-14 19:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-13 10:40 auto indenting code blocks Sam Halliday
2015-06-13 11:25 ` 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

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.