2

I am looking for a function helping indenting imports. I noticed following pattern.

Origin file:

import       A.B.C          (x, y, z)

Result file:

import qualified A.B.C          (x, y, z)
import           Z.X.Y          (a, b, c)

After typing import I need to insert arbitrary number of spaces to align Z.X.Y with A.B.C and parenthesis expressions with line above. Doing this manually is tedious.

I could spend a day to write such function, but I bet it must be already implemented by somebody.

2
  • That is approximately what indent-for-tab-command does, the normal function run by TAB. You are not saying what mode you are using, but that is Haskell code, right? so maybe some Haskell mode which probably has its own function for that key. – pst Aug 24 '20 at 10:50
  • Are you looking for something like align-regexp? – Melioratus Jan 21 at 16:22
0

If you know where Z.X.Y and the parenthesis are going to appear, you can do e.g.

(setq tab-stop-list '(20 40))

and then use M-i to insert spaces until you've reached a position in that list.

If you want to make this context dependent, then something like the following would work:

(save-excursion
  (goto-char (point-min))
  (let (tsl)
    (while (re-search-forward " [^ ]" (point-at-eol) t)
      (push (- (point) 2) tsl))
    (when tsl
      (setq tab-stop-list (reverse tsl)))))

Adjust the goto-char bit as necessary to ensure you're looking at the right line.

1
  • thanks for effort, but this solution is too rigid. The function should be adaptive to the context. – Daneel Yaitskov Aug 18 '20 at 19:02

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.