all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* align a column
@ 2003-01-11 17:14 Zimmen Gnauh
  2003-01-11 19:41 ` Benjamin Rutt
  0 siblings, 1 reply; 2+ messages in thread
From: Zimmen Gnauh @ 2003-01-11 17:14 UTC (permalink / raw)



Suppose I have the following text

first     test
second      line
third       line
fourth       line
fifth         line
...

How can I quickly align the second column to the first word (test)?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: align a column
  2003-01-11 17:14 align a column Zimmen Gnauh
@ 2003-01-11 19:41 ` Benjamin Rutt
  0 siblings, 0 replies; 2+ messages in thread
From: Benjamin Rutt @ 2003-01-11 19:41 UTC (permalink / raw)


Zimmen Gnauh <yah00204052@yahoo.com> writes:

> Suppose I have the following text
>
> first     test
> second      line
> third       line
> fourth       line
> fifth         line
> ...
>
> How can I quickly align the second column to the first word (test)?

I found the following function 'align-cols' in a file called align.el
(not the one by John Wiegley included with emacs).  It should do most
of what you want.

(defun align-cols (start end max-cols)
"Align text between point and mark as columns.
Columns are separated by whitespace characters.
Prefix arg means align that many columns. (default is all)"
  (interactive "r\nP")
  (save-excursion
    (let ((p start)
	  pos
	  end-of-line
	  word
	  count
	  (max-cols (if (numberp max-cols) (max 0 (1- max-cols)) nil))
	  (pos-list nil)
	  (ref-list nil))
      ;; find the positions
      (goto-char start)
      (while (< p end)
	(beginning-of-line)
	(setq count 0)
	(setq end-of-line (save-excursion (end-of-line) (point)))
	(re-search-forward "^\\s-*" end-of-line t)
	(setq pos (current-column))	;start of first word
	(if (null (car ref-list))
	    (setq pos-list (list pos))
	  (setq pos-list (list (max pos (car ref-list))))
	  (setq ref-list (cdr ref-list)))
	(while (and (if max-cols (< count max-cols) t)
		    (re-search-forward "\\s-+" end-of-line t))
	  (setq count (1+ count))
	  (setq word (- (current-column) pos))
	  ;; length of next word including following whitespaces
	  (setq pos (current-column))
	  (if (null (car ref-list))
	      (setq pos-list (cons word pos-list))
	    (setq pos-list (cons (max word (car ref-list)) pos-list))
	    (setq ref-list (cdr ref-list))))
	(while ref-list
	  (setq pos-list (cons (car ref-list) pos-list))
	  (setq ref-list (cdr ref-list)))
	(setq ref-list (nreverse pos-list))
	(forward-line)
	(setq p (point)))
      ;; aling the cols starting with last row
      (setq pos-list (copy-sequence ref-list))
      (setq start 
	    (save-excursion (goto-char start) (beginning-of-line) (point)))
      (goto-char end)
      (beginning-of-line)
      (while (>= p start)
	(beginning-of-line)
	(setq count 0)
	(setq end-of-line (save-excursion (end-of-line) (point)))
	(re-search-forward "^\\s-*" end-of-line t)
	(goto-char (match-end 0))
	(setq pos (nth count pos-list))
	(while (< (current-column) pos)
	  (insert-char ?\040 1))
	(setq end-of-line (save-excursion (end-of-line) (point)))
	(while (and (if max-cols (< count max-cols) t)
		    (re-search-forward "\\s-+" end-of-line t))
	  (setq count (1+ count))
	  (setq pos   (+  pos (nth count pos-list)))
	  (goto-char (match-end 0))
	  (while (< (current-column) pos)
	    (insert-char ?\040 1))
	  (setq end-of-line (save-excursion (end-of-line) (point))))
	(forward-line -1)
	(if (= p (point-min)) (setq p (1- p))
	  (setq p (point)))))))

-- 
Benjamin

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-01-11 19:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-11 17:14 align a column Zimmen Gnauh
2003-01-11 19:41 ` Benjamin Rutt

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.