[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Operate on each line in region
From: |
Jacob Gerlach |
Subject: |
Re: Operate on each line in region |
Date: |
Fri, 25 Apr 2014 20:02:04 -0700 (PDT) |
User-agent: |
G2/1.0 |
Took some more tweaking, but I finally got this working. Comments or
recommendations welcome. I was never able to figure out if align-regexp could
do what I needed.
Figured I'd post the final function:
(defun my-justify-equals (start end)
"Indents current region to justify equals signs.
Indents lines with no equals sign to default tab
width"
(interactive "r")
(save-excursion
(goto-char end)
(goto-char (line-beginning-position))
(let ((left-length 0))
(save-excursion
(while (>= (point) start)
(if (re-search-forward "[
\t]*\\(.*\\)=" (line-end-position) t)
(when (> (length
(match-string-no-properties 1)) left-length)
(setq
left-length (length (match-string-no-properties 1)))))
(forward-line -1)))
(goto-char end)
(goto-char (line-beginning-position))
(while (>= (point) start)
(if (re-search-forward "^[ \t]*\\(.*\\)="
(line-end-position) t)
(indent-line-to (+ tab-width (-
left-length (length (match-string-no-properties 1)))))
(indent-line-to tab-width))
(forward-line -1)))))