emacs gift and question

Luke Gorrie <>
Wed Nov 10 19:45:05 CET 1999


G'day all,

I wrote a little bit of elisp for turning:

foo(1) -> 1;
foo(12) -> 12;
foo(123) -> 123.

into

foo(1)   -> 1;
foo(12)  -> 12;
foo(123) -> 123.

to cut down on hand alignment. I've attached the code incase it's
useful to anyone, and I would be grateful if someone could tell me how
to make it atomic with respect to undo -- currently, undoing an
alignment moves the point. Or if there's already a better
implementation, perhaps someone could tell me where to find it.

Cheers,
Luke

----------------------------------------

(setq arrow-regexp "\\(^.*\\)\\(\\)->")	; FIXME: don't match quoted arrows
(setq match-upto-arrow 1)
(setq match-at-arrow 2)

(defun erlang-arrow-align-region ()
  "Align all arrows beginning functions in region."
  (interactive)
  (save-excursion
    (if (> (point) (mark))
	(exchange-point-and-mark))
    (erlang-align-arrows)
    (erlang-indent-region (point) (mark))))

(defun erlang-align-arrows ()
  "Align all arrows (->) in region"
  (save-excursion
    (indent-arrows (save-excursion (arrow-furthest-indent (mark) 0))
		   (mark))))

(defun arrow-furthest-indent (bound best)
  "Return the furthest-indented arrow between point and bound"
  (let ((idx (arrow-indent-idx bound)))
    (if idx
	(max idx (arrow-furthest-indent bound best))
      best)))

(defun arrow-indent-idx (bound)
  "Find how far the first arrow after point is indented"
  (if (search-forward-regexp arrow-regexp bound t)
      (- (match-end match-upto-arrow) (match-beginning match-upto-arrow))
    nil))

(defun indent-arrows (align-idx bound)
  "Indent all arrows between point and bount to align-idx"
  (let ((arrow-idx (arrow-indent-idx bound)))
    (if (null arrow-idx)
	t
      (let ((pad (- align-idx arrow-idx)))
	(unless (= pad 0)
	  (replace-match (make-string pad ? ) nil nil nil match-at-arrow))
	(indent-arrows align-idx bound)))))





More information about the erlang-questions mailing list