3

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
Daniil Iaitskov
  • 205
  • 1
  • 8
  • 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](https://emacs.stackexchange.com/questions/2644/understanding-of-emacs-align-regexp)? – Melioratus Jan 21 '21 at 16:22
  • Can you explain how you want this to work, e.g. should it do this automatically when you edit the file? Is it a separate command that you start to align the lines in the block? – Lindydancer Jan 16 '22 at 18:17

1 Answers1

1

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.

rpluim
  • 4,605
  • 8
  • 22