all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: Sam Halliday <sam.halliday@gmail.com>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: auto indenting code blocks
Date: Sat, 13 Jun 2015 17:25:22 +0600	[thread overview]
Message-ID: <CAP_d_8VL7yHEPgaeUYVbj=oeE5QadfrWUOR3dvmgoV9CsB_5Rw@mail.gmail.com> (raw)
In-Reply-To: <43ca5eee-94a4-47f3-8b69-3af8a38227cd@googlegroups.com>

On Sat, Jun 13, 2015 at 4:40 PM, Sam Halliday <sam.halliday@gmail.com> wrote:

> 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.

I am solving this for C++ with the following ugly hack (BTW thanks for
pointing out “looking-back”):

===
(defun yk-electric-ret ()
  (interactive)
  (if (and (looking-at "\}") (looking-back "\{")))
      (progn
        (newline-and-indent)
        (save-excursion
          (newline-and-indent))
        (indent-according-to-mode))
    (newline-and-indent)))

(defun yk-braces-keys ()
  (local-set-key (kbd "RET") 'yk-electric-ret))
(add-hook 'c-mode-common-hook 'yk-braces-keys)
===

It is essentially equivalent to yours except that I prefer rebinding a
single key to watching for all buffer changes.

> 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?

I am also interested in answers to the above.

Additionally, it would be nice to be able to select a multi-line
region (def: a region that contains at least one line break), press
“{” and have it automatically enclose the region in a pair of braces,
add a line break immediately after the opening brace and another one
before the closing brace, and re-indent all affected lines according
to mode and style. (I could probably compose a function to that effect
and bind it to “{” instead of c-electric-brace or self-insert-command,
but that looks boring :])



  reply	other threads:[~2015-06-13 11:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-13 10:40 auto indenting code blocks Sam Halliday
2015-06-13 11:25 ` Yuri Khan [this message]
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='CAP_d_8VL7yHEPgaeUYVbj=oeE5QadfrWUOR3dvmgoV9CsB_5Rw@mail.gmail.com' \
    --to=yuri.v.khan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=sam.halliday@gmail.com \
    /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.