Emacs: stay at the correct position when indenting

When indenting a non-blank line, stay at the same cursor position
relative to the content after indenting.
This commit is contained in:
Micah Chalmer 2014-02-22 03:36:11 -05:00
parent 7fbcda1c65
commit 45008f9b3e
2 changed files with 74 additions and 9 deletions

View file

@ -488,6 +488,26 @@ struct Foo {
}
"
rust-test-region-string rust-test-motion-string
rust-test-indent-motion-string
"
fn blank_line(arg:int) -> bool {
}
fn indenting_closing_brace() {
if(true) {
}
}
fn indenting_middle_of_line() {
if(true) {
push_me_out();
} else {
pull_me_back_in();
}
}
"
;; Symbol -> (line column)
rust-test-positions-alist '((start-of-fn1 (2 0))
(start-of-fn1-middle-of-line (2 15))
@ -502,7 +522,17 @@ struct Foo {
(middle-of-fn3 (16 4))
(middle-of-struct (21 10))
(before-start-of-struct (19 0))
(after-end-of-struct (23 0))))
(after-end-of-struct (23 0))
(blank-line-indent-start (3 0))
(blank-line-indent-target (3 4))
(closing-brace-indent-start (8 1))
(closing-brace-indent-target (8 5))
(middle-push-indent-start (13 2))
(middle-push-indent-target (13 9))
(after-whitespace-indent-start (13 1))
(after-whitespace-indent-target (13 8))
(middle-pull-indent-start (15 19))
(middle-pull-indent-target (15 12))))
(defun rust-get-buffer-pos (pos-symbol)
"Get buffer position from POS-SYMBOL.
@ -664,3 +694,38 @@ All positions are position symbols found in `rust-test-positions-alist'."
'middle-of-struct
'before-start-of-struct 'after-end-of-struct
#'mark-defun))
(ert-deftest indent-line-blank-line-motion ()
(rust-test-motion
rust-test-indent-motion-string
'blank-line-indent-start
'blank-line-indent-target
#'indent-for-tab-command))
(ert-deftest indent-line-closing-brace-motion ()
(rust-test-motion
rust-test-indent-motion-string
'closing-brace-indent-start
'closing-brace-indent-target
#'indent-for-tab-command))
(ert-deftest indent-line-middle-push-motion ()
(rust-test-motion
rust-test-indent-motion-string
'middle-push-indent-start
'middle-push-indent-target
#'indent-for-tab-command))
(ert-deftest indent-line-after-whitespace-motion ()
(rust-test-motion
rust-test-indent-motion-string
'after-whitespace-indent-start
'after-whitespace-indent-target
#'indent-for-tab-command))
(ert-deftest indent-line-middle-pull-motion ()
(rust-test-motion
rust-test-indent-motion-string
'middle-pull-indent-start
'middle-pull-indent-target
#'indent-for-tab-command))

View file

@ -114,14 +114,14 @@
;; Otherwise we're in a column-zero definition
(t 0))))))
(cond
;; If we're to the left of the indentation, reindent and jump to it.
((<= (current-column) indent)
(indent-line-to indent))
;; We're to the right; if it needs indent, do so but save excursion.
((not (eq (current-indentation) indent))
(save-excursion (indent-line-to indent))))))
(when (not (eq (current-indentation) indent))
;; If we're at the beginning of the line (before or at the current
;; indentation), jump with the indentation change. Otherwise, save the
;; excursion so that adding the indentations will leave us at the
;; equivalent position within the line to where we were before.
(if (<= (current-column) (current-indentation))
(indent-line-to indent)
(save-excursion (indent-line-to indent))))))
;; Font-locking definitions and helpers