From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Benjamin Rutt Newsgroups: gmane.emacs.help Subject: Re: align a column Date: Sat, 11 Jan 2003 14:41:40 -0500 Organization: The Ohio State University Dept. of Computer and Info. Science Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1042314484 1655 80.91.224.249 (11 Jan 2003 19:48:04 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sat, 11 Jan 2003 19:48:04 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18XRbp-0000Pn-00 for ; Sat, 11 Jan 2003 20:47:57 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18XRa4-0007T9-00 for gnu-help-gnu-emacs@m.gmane.org; Sat, 11 Jan 2003 14:46:08 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.cis.ohio-state.edu!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 89 Original-NNTP-Posting-Host: gamma.cis.ohio-state.edu Mail-Copies-To: nobody User-Agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.3.50 (sparc-sun-solaris2.8) Cancel-Lock: sha1:OkUzKYfV/hJdhSJJV63Ys+EBDdE= Original-Xref: shelby.stanford.edu gnu.emacs.help:108852 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:5381 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:5381 Zimmen Gnauh 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