From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sam Halliday Newsgroups: gmane.emacs.help Subject: auto indenting code blocks Date: Sat, 13 Jun 2015 03:40:16 -0700 (PDT) Message-ID: <43ca5eee-94a4-47f3-8b69-3af8a38227cd@googlegroups.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1434192321 2671 80.91.229.3 (13 Jun 2015 10:45:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 13 Jun 2015 10:45:21 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 13 12:45:20 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z3iwA-0002JF-Jg for geh-help-gnu-emacs@m.gmane.org; Sat, 13 Jun 2015 12:45:18 +0200 Original-Received: from localhost ([::1]:55427 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z3iw9-0005wY-Ak for geh-help-gnu-emacs@m.gmane.org; Sat, 13 Jun 2015 06:45:17 -0400 X-Received: by 10.66.100.163 with SMTP id ez3mr24260937pab.38.1434192017301; Sat, 13 Jun 2015 03:40:17 -0700 (PDT) X-Received: by 10.140.21.111 with SMTP id 102mr276216qgk.4.1434192017018; Sat, 13 Jun 2015 03:40:17 -0700 (PDT) Original-Path: usenet.stanford.edu!h15no2369109igd.0!news-out.google.com!k20ni1525qgd.0!nntp.google.com!q107no970175qgd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=82.13.129.22; posting-account=kRukCAoAAAANs-vsVh9dFwo5kp5pwnPz Original-NNTP-Posting-Host: 82.13.129.22 User-Agent: G2/1.0 Injection-Date: Sat, 13 Jun 2015 10:40:17 +0000 Original-Xref: usenet.stanford.edu gnu.emacs.help:212626 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:104910 Archived-At: 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