1

From indent.el:

(defun indent-relative (&optional unindented-ok)
  "Space out to under next indent point in previous nonblank line.
An indent point is a non-whitespace character following whitespace.
The following line shows the indentation points in this line.
    ^         ^    ^     ^   ^           ^      ^  ^    ^
If the previous nonblank line has no indent points beyond the
column point starts at, `tab-to-tab-stop' is done instead, unless
this command is invoked with a numeric argument, in which case it
does nothing.

See also `indent-relative-maybe'."
  (interactive "P")
  (if (and abbrev-mode
       (eq (char-syntax (preceding-char)) ?w))
      (expand-abbrev))
  (let ((start-column (current-column))
    indent)
    (save-excursion
      (beginning-of-line)
      (if (re-search-backward "^[^\n]" nil t)
      (let ((end (save-excursion (forward-line 1) (point))))
        (move-to-column start-column)
        ;; Is start-column inside a tab on this line?
        (if (> (current-column) start-column)
        (backward-char 1))
        (or (looking-at "[ \t]")
        unindented-ok
        (skip-chars-forward "^ \t" end))
        (skip-chars-forward " \t" end)
        (or (= (point) end) (setq indent (current-column))))))
    (if indent
    (let ((opoint (point-marker)))
      (delete-region (point) (progn (skip-chars-backward " \t") (point)))
      (indent-to indent 0)
      (if (> opoint (point))
          (goto-char opoint))
      (move-marker opoint nil))
      (tab-to-tab-stop))))

How would I modify this function so that it aligns the current line with the indentation point in the next, as opposed to the previous, line?

0

One way is to swap the current line with the next one, use indent-relative and then swap back:

(defun indent-relative-below ()
  (interactive)
  (save-excursion
    (next-line)
    (transpose-lines 1)
    (previous-line)
    (indent-relative)
    (transpose-lines 1)
    )
  (end-of-line)
  )
  • This doesn't seem to work. Though that may have something to do with my use of the "evil" plugin. – LMZ Mar 28 '15 at 22:55
  • You could start Emacs with emacs -Q to see if it's something in your config. – erikstokes Mar 29 '15 at 0:46
0

Based on erikstokes suggestion, I've come up with a working solution.

edit: I also used code from

http://emacsredux.com/blog/2013/04/02/move-current-line-up-or-down/

(defun move-line-down ()
  (interactive)
  (let ((col (current-column)))
    (save-excursion
      (forward-line)
      (transpose-lines 1))
    (forward-line)
    (move-to-column col)))

(defun move-line-up ()
  (interactive)
  (let ((col (current-column)))
    (save-excursion
      (forward-line)
      (transpose-lines -1))
    (move-to-column col)))

;; Indent relative, but works for the line below rather than above 
(defun indent-relative-below ()
  (interactive)
  (move-line-down)
  (indent-relative)
  (move-line-up))
(global-set-key  (kbd "C-;") 'indent-relative-below)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.