aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/ChangeLog4
-rw-r--r--lisp/electric.el24
-rw-r--r--lisp/progmodes/python.el6
3 files changed, 25 insertions, 9 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 64fedd32191..520e41afc68 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,9 @@
12013-10-07 Stefan Monnier <monnier@iro.umontreal.ca> 12013-10-07 Stefan Monnier <monnier@iro.umontreal.ca>
2 2
3 * electric.el (electric-indent-inhibit): New var.
4 (electric-indent-post-self-insert-function): Use it.
5 * progmodes/python.el (python-mode): Set it.
6
3 * progmodes/ruby-mode.el (ruby-smie-rules): Tweak handling of 7 * progmodes/ruby-mode.el (ruby-smie-rules): Tweak handling of
4 open braces. 8 open braces.
5 9
diff --git a/lisp/electric.el b/lisp/electric.el
index 36ec4a00b88..b62de874b93 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -187,7 +187,7 @@ Returns nil when we can't find this char."
187 (eq (char-before) last-command-event))))) 187 (eq (char-before) last-command-event)))))
188 pos))) 188 pos)))
189 189
190;; Electric indentation. 190;;; Electric indentation.
191 191
192;; Autoloading variables is generally undesirable, but major modes 192;; Autoloading variables is generally undesirable, but major modes
193;; should usually set this variable by adding elements to the default 193;; should usually set this variable by adding elements to the default
@@ -202,6 +202,11 @@ Each function is called with one argument (the inserted char), with
202point right after that char, and it should return t to cause indentation, 202point right after that char, and it should return t to cause indentation,
203`no-indent' to prevent indentation or nil to let other functions decide.") 203`no-indent' to prevent indentation or nil to let other functions decide.")
204 204
205(defvar-local electric-indent-inhibit nil
206 "If non-nil, reindentation is not appropriate for this buffer.
207This should be set by major modes such as `python-mode' since
208Python does not lend itself to fully automatic indentation.")
209
205(defun electric-indent-post-self-insert-function () 210(defun electric-indent-post-self-insert-function ()
206 ;; FIXME: This reindents the current line, but what we really want instead is 211 ;; FIXME: This reindents the current line, but what we really want instead is
207 ;; to reindent the whole affected text. That's the current line for simple 212 ;; to reindent the whole affected text. That's the current line for simple
@@ -229,12 +234,13 @@ point right after that char, and it should return t to cause indentation,
229 (unless (eq act 'do-indent) (nth 8 (syntax-ppss)))))))) 234 (unless (eq act 'do-indent) (nth 8 (syntax-ppss))))))))
230 ;; For newline, we want to reindent both lines and basically behave like 235 ;; For newline, we want to reindent both lines and basically behave like
231 ;; reindent-then-newline-and-indent (whose code we hence copied). 236 ;; reindent-then-newline-and-indent (whose code we hence copied).
232 (when (< (1- pos) (line-beginning-position)) 237 (when (<= pos (line-beginning-position))
233 (let ((before (copy-marker (1- pos) t))) 238 (let ((before (copy-marker (1- pos) t)))
234 (save-excursion 239 (save-excursion
235 (unless (memq indent-line-function 240 (unless (or (memq indent-line-function
236 '(indent-relative indent-to-left-margin 241 '(indent-relative indent-to-left-margin
237 indent-relative-maybe)) 242 indent-relative-maybe))
243 electric-indent-inhibit)
238 ;; Don't reindent the previous line if the indentation function 244 ;; Don't reindent the previous line if the indentation function
239 ;; is not a real one. 245 ;; is not a real one.
240 (goto-char before) 246 (goto-char before)
@@ -248,7 +254,9 @@ point right after that char, and it should return t to cause indentation,
248 ;; Remove the trailing whitespace after indentation because 254 ;; Remove the trailing whitespace after indentation because
249 ;; indentation may (re)introduce the whitespace. 255 ;; indentation may (re)introduce the whitespace.
250 (delete-horizontal-space t)))) 256 (delete-horizontal-space t))))
251 (unless (memq indent-line-function '(indent-to-left-margin)) 257 (unless (or (memq indent-line-function '(indent-to-left-margin))
258 (and electric-indent-inhibit
259 (> pos (line-beginning-position))))
252 (indent-according-to-mode))))) 260 (indent-according-to-mode)))))
253 261
254;;;###autoload 262;;;###autoload
@@ -281,7 +289,7 @@ insert a character from `electric-indent-chars'."
281 (delq #'electric-indent-post-self-insert-function 289 (delq #'electric-indent-post-self-insert-function
282 (cdr bp)))))))) 290 (cdr bp))))))))
283 291
284;; Electric pairing. 292;;; Electric pairing.
285 293
286(defcustom electric-pair-pairs 294(defcustom electric-pair-pairs
287 '((?\" . ?\")) 295 '((?\" . ?\"))
@@ -414,7 +422,7 @@ See options `electric-pair-pairs' and `electric-pair-skip-self'."
414 (remove-hook 'self-insert-uses-region-functions 422 (remove-hook 'self-insert-uses-region-functions
415 #'electric-pair-will-use-region))) 423 #'electric-pair-will-use-region)))
416 424
417;; Automatically add newlines after/before/around some chars. 425;;; Electric newlines after/before/around some chars.
418 426
419(defvar electric-layout-rules '() 427(defvar electric-layout-rules '()
420 "List of rules saying where to automatically insert newlines. 428 "List of rules saying where to automatically insert newlines.
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 5f919bf495f..ce727391ce8 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3544,6 +3544,8 @@ list is returned as is."
3544 (reverse acc)))) 3544 (reverse acc))))
3545 3545
3546 3546
3547(defvar electric-indent-inhibit)
3548
3547;;;###autoload 3549;;;###autoload
3548(define-derived-mode python-mode prog-mode "Python" 3550(define-derived-mode python-mode prog-mode "Python"
3549 "Major mode for editing Python files. 3551 "Major mode for editing Python files.
@@ -3572,7 +3574,9 @@ if that value is non-nil."
3572 (set (make-local-variable 'indent-line-function) 3574 (set (make-local-variable 'indent-line-function)
3573 #'python-indent-line-function) 3575 #'python-indent-line-function)
3574 (set (make-local-variable 'indent-region-function) #'python-indent-region) 3576 (set (make-local-variable 'indent-region-function) #'python-indent-region)
3575 3577 ;; Because indentation is not redundant, we cannot safely reindent code.
3578 (setq-local electric-indent-inhibit t)
3579
3576 (set (make-local-variable 'paragraph-start) "\\s-*$") 3580 (set (make-local-variable 'paragraph-start) "\\s-*$")
3577 (set (make-local-variable 'fill-paragraph-function) 3581 (set (make-local-variable 'fill-paragraph-function)
3578 'python-fill-paragraph) 3582 'python-fill-paragraph)