[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: align-regexp not working as intended
From: |
jpkotta |
Subject: |
Re: align-regexp not working as intended |
Date: |
Mon, 18 Mar 2013 11:39:48 -0700 (PDT) |
User-agent: |
G2/1.0 |
On Monday, March 18, 2013 5:15:25 AM UTC-5, Florian Lindner wrote:
> Hello,
>
>
>
> on my Emacs 24.2.1 7 / Arch align-regexp is not working like I expect it to:
>
>
>
> I have a latex table:
>
>
>
> & $C_D$ & $C_L$ & $C_M$ & $C_D$ & $C_L$ & $C_M$ & $C_D$ & $C_L$ &
>
> $C_M$ \\ \hline
>
> 5-Spoke & $0.640$ & $0.396$ & $\approx 0$ & $0.599$ & $0.394$ &
>
> $0.017$ & $0.599$ & $0.356$ & $0.019$ \\
>
> 7-Spoke & $0.646$ & $0.407$ & $0.0017$ & $0.592$ & $0.377$ &
>
> $0.0167$ & $0.606$ & $0.349$ & $0.0280$ \\
>
> DA Wheel & $0.649$ & $0.421$ & $-0.0004$ & $0.607$ & $0.392$ &
>
> $0.0145$ & $0.614$ & $0.384$ & $0.024$ \\
>
> GenWheel Open & $0.640$ & $0.330$ & $0.0012$ & $0.613$ & $0.253$ &
>
> $0.018$ & $0.660$ & $0.201$ & $0.031$ \\
>
> GenWheel Closed & $0.617$ & $0.302$ & $0.0015$ & $0.579$ & $0.280$
>
> & $0.0206$ & $0.580$ & $0.281$ & $0.0206$ \\
>
>
>
> This should be aligned at the &: M-x align-regexp & with the part
>
> above as active region gives me:
>
>
>
> & $C_D$ & $C_L$ & $C_M$ & $C_D$ & $C_L$ & $C_M$ &
>
> $C_D$ & $C_L$ & $C_M$ \\ \hline
>
> 5-Spoke & $0.640$ & $0.396$ & $\approx 0$ & $0.599$ &
>
> $0.394$ & $0.017$ & $0.599$ & $0.356$ & $0.019$ \\
>
> 7-Spoke & $0.646$ & $0.407$ & $0.0017$ & $0.592$ & $0.377$
>
> & $0.0167$ & $0.606$ & $0.349$ & $0.0280$ \\
>
> DA Wheel & $0.649$ & $0.421$ & $-0.0004$ & $0.607$ &
>
> $0.392$ & $0.0145$ & $0.614$ & $0.384$ & $0.024$ \\
>
> GenWheel Open & $0.640$ & $0.330$ & $0.0012$ & $0.613$ & $0.253$
>
> & $0.018$ & $0.660$ & $0.201$ & $0.031$ \\
>
> GenWheel Closed & $0.617$ & $0.302$ & $0.0015$ & $0.579$ & $0.280$
>
> & $0.0206$ & $0.580$ & $0.281$ & $0.0206$ \\
>
>
>
> (you should swith your font to fixed-width now)
>
>
>
> The align is fine at the first columns but somehow got off later...
>
>
>
> What is the problem here?
>
>
>
> If there is some problem with mail reader introduced line breaks or
>
> font width, the entire posting is available here:
>
> http://pastebin.com/raw.php?i=3JZiKiaS
>
>
>
> Thanks!
>
>
>
> Florian
align-regexp has an optional repeat argument which must be set to t to do what
you want. Here is a command that I use for these sorts of tasks:
(defun align-regexp-repeated (start stop regexp)
"Like align-regexp, but repeated for multiple columns. See
http://www.emacswiki.org/emacs/AlignCommands"
(interactive "r\nsAlign regexp: ")
(let ((spacing 1)
(old-buffer-size (buffer-size)))
;; If our align regexp is just spaces, then we don't need any
;; extra spacing.
(when (string-match regexp " ")
(setq spacing 0))
(align-regexp start stop
;; add space at beginning of regexp
(concat "\\([[:space:]]*\\)" regexp)
1 spacing t)
;; modify stop because align-regexp will add/remove characters
(align-regexp start (+ stop (- (buffer-size) old-buffer-size))
;; add space at end of regexp
(concat regexp "\\([[:space:]]*\\)")
1 spacing t)))