In C mode and Python mode, hitting <RET> on an indented but otherwise
empty line results in an unindented new line at point, rather than an
indented line.
To reproduce in emacs -Q:
M-x python-mode<RET>
if a == 3:<RET>
<RET>
M-: (current-column)<RET>
Expected result: 4 / indented line
Actual result: 0 / line not indented
Emacs 24.3.1 produces the expected result.
Lossage:
M-x p y t h o n - m o d e <return> i f SPC a SPC =
= SPC 3 : <return> <return> M-: ( c u r r e n t - c
o l u m n ) <return>
The same bug can be seen in C mode:
M-x c-mode<RET>
int main(void) {<RET>
<RET>
M-: (current-column)<RET>
(However, since there are more electric characters in c-mode, the
problem goes away as you continue typing; that isn't true of the
python-mode bug).
Investigating this, it seems the problem is in
electric-indent-post-self-insert-function: its code reads
(when (<= pos (line-beginning-position))
...
(delete-horizontal-space t)))))
(unless (and electric-indent-inhibit
(> pos (line-beginning-position)))
(indent-according-to-mode)))))
where pos is an integer, not a marker. Assuming
electric-indent-inhibit, when delete-horizontal-space does anything
(as is the case for newline on an indented empty line),
(line-beginning-position) changes, but pos doesn't change along with
it, so the (indent-according-to-mode) is skipped even though it should
be run on new empty lines.
The easiest fix would be to change electric--after-char-pos to return
a marker, not an integer:
diff --git a/lisp/electric.el b/lisp/electric.el
index e8ceaa6..874ec09 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -176,7 +176,7 @@
(defun electric--after-char-pos ()
"Return the position after the char we just inserted.
Returns nil when we can't find this char."
- (let ((pos (point)))
+ (let ((pos (point-marker)))
(when (or (eq (char-before) last-command-event) ;; Sanity check.
(save-excursion
(or (progn (skip-chars-backward " \t")
A slightly different fix would avoid using a temporary marker for
every self-insert:
diff --git a/lisp/electric.el b/lisp/electric.el
index e8ceaa6..34567d3 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -259,7 +259,8 @@ or comment."
(unless (eq act 'do-indent) (nth 8 (syntax-ppss))))))))
;; For newline, we want to reindent both lines and basically behave like
;; reindent-then-newline-and-indent (whose code we hence copied).
- (when (<= pos (line-beginning-position))
+ (let ((at-newline (<= pos (line-beginning-position))))
+ (when at-newline
(let ((before (copy-marker (1- pos) t)))
(save-excursion
(unless (or (memq indent-line-function
@@ -280,8 +281,8 @@ or comment."
;; indentation may (re)introduce the whitespace.
(delete-horizontal-space t)))))
(unless (and electric-indent-inhibit
- (> pos (line-beginning-position)))
- (indent-according-to-mode)))))
+ (not at-newline))
+ (indent-according-to-mode))))))
(put 'electric-indent-post-self-insert-function 'priority 60)
Both fixes appear to fix the problem in c-mode and python-mode; I've
not tested rst-mode, which appears to be the only other standard mode
that uses electric-indent-inhibit.
In GNU Emacs 24.4.50.14 (x86_64-unknown-linux-gnu, GTK+ Version 3.10.7)
of 2014-05-08 on amerigo
Windowing system distributor `The X.Org Foundation', version 11.0.11500000
System Description: Debian GNU/Linux unstable (sid)
Configured using:
`configure --without-makeinfo --without-compress-install'
Configured features:
XPM JPEG TIFF GIF PNG RSVG SOUND DBUS GSETTINGS NOTIFY LIBSELINUX GNUTLS
LIBXML2 FREETYPE XFT ZLIB
Important settings:
value of $LANG: en_US.UTF-8
locale-coding-system: utf-8-unix
Major mode: Help
Minor modes in effect:
tooltip-mode: t
electric-indent-mode: t
mouse-wheel-mode: t
tool-bar-mode: t
menu-bar-mode: t
file-name-shadow-mode: t
global-font-lock-mode: t
font-lock-mode: t
blink-cursor-mode: t
auto-composition-mode: t
auto-encryption-mode: t
auto-compression-mode: t
buffer-read-only: t
line-number-mode: t
transient-mark-mode: t
Recent input:
M-x p y t h o n - m o d e <return> i f SPC a SPC =
= SPC 3 : <return> <return> M-: ( c u r r e n t - c
o l u m n ) <return> M-x v i e w - l <tab> <return>
<help-echo> <down-mouse-1> <mouse-1> <up> <up> <up>
<up> <up> <up> <up> <up> C-a C-SPC <C-down> <down>
<up> <right> <C-up> <down> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <right> <right> <down> <right> <left> C-w M-x
r e p <tab> o r <tab> <return>
Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Can't guess python-indent-offset, using defaults: 4
0 (#o0, #x0, ?\C-@)
Type C-x 1 to delete the help window.
Beginning of buffer [4 times]
Mark set
End of buffer
Buffer is read-only: #<buffer *Help*>
Making completion list...
Load-path shadows:
None found.
Features:
(shadow sort gnus-util mail-extr emacsbug message dired format-spec
rfc822 mml mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util help-fns mail-prsvr mail-utils help-mode mule-util python
easymenu comint ring ansi-color time-date tooltip electric uniquify
ediff-hook vc-hooks lisp-float-type mwheel x-win x-dnd tool-bar dnd
fontset image regexp-opt fringe tabulated-list newcomment lisp-mode
prog-mode register page menu-bar rfn-eshadow timer select scroll-bar
mouse jit-lock font-lock syntax facemenu font-core frame cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese case-table epa-hook jka-cmpr-hook help simple abbrev
minibuffer nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote make-network-process
dbusbind gfilenotify dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty emacs)
Memory information:
((conses 16 82158 4302)
(symbols 48 18600 0)
(miscs 40 51 174)
(strings 32 13034 3892)
(string-bytes 1 365906)
(vectors 16 10278)
(vector-slots 8 388612 3417)
(floats 8 68 243)
(intervals 56 260 1)
(buffers 960 13)
(heap 1024 29346 991))