all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Aligning colums
@ 2005-01-20 15:46 Roy Smith
  2005-01-20 20:16 ` Stefan Reichör
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Roy Smith @ 2005-01-20 15:46 UTC (permalink / raw)


I've got a bunch of text that looks like this:

	 foo ("arg1", "xxxxxxxxxxxxxx", 1, "xxxx", 2)
	 foo ("arg1", "xxxxxxxxx", 1, "xxx", 2)
	 foo ("arg1", "xxxxxxxxxxx", 1, "xxxxxxxx", 2)
	 foo ("arg1", "xxxxxxxx", 1, "xxxxxx", 2)
	 foo ("arg1", "xxxxxxxxxx", 1, "xxxxxxx", 2)

I want to get the arguments lined up so it looks like:

	 foo ("arg1", "xxxxxxxxxxxxxx",  1, "xxxx",       2)
	 foo ("arg1", "xxxxxxxxx",       1, "xxx",        2)
	 foo ("arg1", "xxxxxxxxxxx",     1, "xxxxxxxx",   2)
	 foo ("arg1", "xxxxxxxx",        1, "xxxxxx",     2)
	 foo ("arg1", "xxxxxxxxxx",      1, "xxxxxxx",    2)

What's the best way to do that?  I suspect M-X align is what I want, I
can't make heads or tails out of the on-line help.

Note, the above text looks like C code, but I seem to run into things
like this is various languages, not just C, so I'm looking for a
general solution.

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

* Re: Aligning colums
  2005-01-20 15:46 Aligning colums Roy Smith
@ 2005-01-20 20:16 ` Stefan Reichör
  2005-01-23  4:05 ` Alan Wehmann
  2005-01-24 23:57 ` Peter Heslin
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Reichör @ 2005-01-20 20:16 UTC (permalink / raw)


roy@panix.com (Roy Smith) writes:

> I've got a bunch of text that looks like this:
> 
> 	 foo ("arg1", "xxxxxxxxxxxxxx", 1, "xxxx", 2)
> 	 foo ("arg1", "xxxxxxxxx", 1, "xxx", 2)
> 	 foo ("arg1", "xxxxxxxxxxx", 1, "xxxxxxxx", 2)
> 	 foo ("arg1", "xxxxxxxx", 1, "xxxxxx", 2)
> 	 foo ("arg1", "xxxxxxxxxx", 1, "xxxxxxx", 2)
> 
> I want to get the arguments lined up so it looks like:
> 
> 	 foo ("arg1", "xxxxxxxxxxxxxx",  1, "xxxx",       2)
> 	 foo ("arg1", "xxxxxxxxx",       1, "xxx",        2)
> 	 foo ("arg1", "xxxxxxxxxxx",     1, "xxxxxxxx",   2)
> 	 foo ("arg1", "xxxxxxxx",        1, "xxxxxx",     2)
> 	 foo ("arg1", "xxxxxxxxxx",      1, "xxxxxxx",    2)
> 
> What's the best way to do that?  I suspect M-X align is what I want, I
> can't make heads or tails out of the on-line help.
> 
> Note, the above text looks like C code, but I seem to run into things
> like this is various languages, not just C, so I'm looking for a
> general solution.

You could use my progr-align.el package:
www.xsteve.at/prg/emacs/progr-align.el

You specify a header line that matches the columns and the needed widths:

// #palign "\w" 4, "(\"\w+\"" 9, "\"" 19, "[0-9]+" 17, "[0-9]+"

Then you can put the cursor on a line or mark a region and call
M-x progr-align. That aligns the above code the way you wanted it.

Let me explain the #palign line:
"\w" matches foo, the column should be 4 characters wide
"(\"\w+\"" matches "arg1", columnwidth: 9 characters
"\"" matches "xxx", columnwidth: 19
"[0-9]+" matches 1, columnwidth: 17
"[0-9]+" matches 2, last column, no width needed


Stefan.

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

* Re: Aligning colums
  2005-01-20 15:46 Aligning colums Roy Smith
  2005-01-20 20:16 ` Stefan Reichör
@ 2005-01-23  4:05 ` Alan Wehmann
  2005-01-24 23:57 ` Peter Heslin
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Wehmann @ 2005-01-23  4:05 UTC (permalink / raw)


Look for pretty-column.el.  It will do what you want and is easy to
use.  I think I got it from gnu.emacs.sources, so chances are you can
find it in the Google group collection of postings.

roy@panix.com (Roy Smith) writes:

> I've got a bunch of text that looks like this:
>
> 	 foo ("arg1", "xxxxxxxxxxxxxx", 1, "xxxx", 2)
> 	 foo ("arg1", "xxxxxxxxx", 1, "xxx", 2)
> 	 foo ("arg1", "xxxxxxxxxxx", 1, "xxxxxxxx", 2)
> 	 foo ("arg1", "xxxxxxxx", 1, "xxxxxx", 2)
> 	 foo ("arg1", "xxxxxxxxxx", 1, "xxxxxxx", 2)
>
> I want to get the arguments lined up so it looks like:
>
> 	 foo ("arg1", "xxxxxxxxxxxxxx",  1, "xxxx",       2)
> 	 foo ("arg1", "xxxxxxxxx",       1, "xxx",        2)
> 	 foo ("arg1", "xxxxxxxxxxx",     1, "xxxxxxxx",   2)
> 	 foo ("arg1", "xxxxxxxx",        1, "xxxxxx",     2)
> 	 foo ("arg1", "xxxxxxxxxx",      1, "xxxxxxx",    2)
>
> What's the best way to do that?  I suspect M-X align is what I want, I
> can't make heads or tails out of the on-line help.
>
> Note, the above text looks like C code, but I seem to run into things
> like this is various languages, not just C, so I'm looking for a
> general solution.
>
>
>

-- 
Alan Wehmann
wehmann_nospam@fnal.gov

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

* Re: Aligning colums
  2005-01-20 15:46 Aligning colums Roy Smith
  2005-01-20 20:16 ` Stefan Reichör
  2005-01-23  4:05 ` Alan Wehmann
@ 2005-01-24 23:57 ` Peter Heslin
  2 siblings, 0 replies; 4+ messages in thread
From: Peter Heslin @ 2005-01-24 23:57 UTC (permalink / raw)


On 2005-01-20, Roy Smith <roy@panix.com> wrote:
>  I want to get the arguments lined up so it looks like:
> 
>  	 foo ("arg1", "xxxxxxxxxxxxxx",  1, "xxxx",       2)
>  	 foo ("arg1", "xxxxxxxxx",       1, "xxx",        2)
>  	 foo ("arg1", "xxxxxxxxxxx",     1, "xxxxxxxx",   2)
>  	 foo ("arg1", "xxxxxxxx",        1, "xxxxxx",     2)
>  	 foo ("arg1", "xxxxxxxxxx",      1, "xxxxxxx",    2)
> 
>  What's the best way to do that?  I suspect M-X align is what I want, I
>  can't make heads or tails out of the on-line help.

I think that this package will work for you out of the box.  I can't
remember where I found it.

;;; align-cols.el --- aling text seperated by whitespaces in columns

;; Copyright (C) 1999 by Matthias Helmling

;; Author: Matthias Helmling <matt@acid.rhein-neckar.de>
;; Keywords: extensions
;; Version: 0.2
;; Time-stamp: "99/01/30 15:46:16 matt"

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; tested with XEmacs 20.4 and GNU Emacs 20.2.1

;;; Code:
(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))))
      )))

(provide 'align-cols)

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

end of thread, other threads:[~2005-01-24 23:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-20 15:46 Aligning colums Roy Smith
2005-01-20 20:16 ` Stefan Reichör
2005-01-23  4:05 ` Alan Wehmann
2005-01-24 23:57 ` Peter Heslin

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.