Tables: Allow forced alignment to left or right.

Michael Brand writes:

>  2) One could like to have configurable left/right alignment, even
>     combinable with column width, e. g.
>
>  | <l10>      | <r>       |
>  | 3.14       |      0x10 |
>  | 3.141592=> | 0x32 0x10 |

Good idea, I would say.
This commit is contained in:
Carsten Dominik 2009-06-21 22:40:45 +02:00
parent 94c4f801a2
commit c1c3c3f7cb
4 changed files with 43 additions and 19 deletions

View File

@ -1880,11 +1880,15 @@ it off with
@noindent Then the only table command that still works is
@kbd{C-c C-c} to do a manual re-align.
@node Narrow columns, Column groups, Built-in table editor, Tables
@section Narrow columns
@node Column width and aligment, Column groups, Built-in table editor, Tables
@section Column width and aligment
@cindex narrow columns in tables
@cindex alignment in tables
The width of columns is automatically determined by the table editor. And
also the alignment of a column is determined automatically from the fraction
of number-like versus non-number fields in the column.
The width of columns is automatically determined by the table editor.
Sometimes a single field or a few fields need to carry more text,
leading to inconveniently wide columns. To limit@footnote{This feature
does not work on XEmacs.} the width of a column, one field anywhere in
@ -1927,6 +1931,11 @@ on a per-file basis with:
#+STARTUP: noalign
@end example
If you would like to overrule the automatic aligment of number-rich columns
to the right and of string-rich column to the left, you and use @samp{<r>} or
@samp{<l>} in a similar fashion. You may also combine alignment and field
width like this: @samp{<l10>}.
@node Column groups, Orgtbl mode, Narrow columns, Tables
@section Column groups
@cindex grouping columns in tables

View File

@ -1,5 +1,15 @@
2009-06-21 Carsten Dominik <carsten.dominik@gmail.com>
* org.el (org-set-font-lock-defaults): Adapt formatting to capture
new alignment strings.
* org-table.el (orgtbl-self-insert-command): Add yas/expand to
command list.
(org-table-align): Check for forced align type.
* org.el (org-self-insert-command): Add yas/expand to command
list.
* org-clock.el (org-clock-in-hook): New hook.
(org-clock-in): Run `org-clock-in-hook.
(org-clock-out-hook): New hook.

View File

@ -573,7 +573,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
(make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
(hfmt1 (concat
(make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
emptystrings links dates emph narrow fmax f1 len c e)
emptystrings links dates emph narrow falign falign1 fmax f1 len c e)
(untabify beg end)
(remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
;; Check if we have links or dates
@ -594,7 +594,9 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
;; Check if we are narrowing any columns
(goto-char beg)
(setq narrow (and org-format-transports-properties-p
(re-search-forward "<[0-9]+>" end t)))
(re-search-forward "<[rl]?[0-9]+>" end t)))
(goto-char beg)
(setq falign (re-search-forward "<[rl][0-9]*>" end t))
;; Get the rows
(setq lines (org-split-string
(buffer-substring beg end) "\n"))
@ -629,12 +631,14 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
(while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
(setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
;; Check if there is an explicit width specified
(when narrow
(setq c column fmax nil)
(when (or narrow falign)
(setq c column fmax nil falign1 nil)
(while c
(setq e (pop c))
(if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
(setq fmax (string-to-number (match-string 1 e)) c nil)))
(when (and (stringp e) (string-match "^<\\([rl]\\)?\\([0-9]+\\)?>$" e))
(if (match-end 1) (setq falign1 (match-string 1 e)))
(if (match-end 2)
(setq fmax (string-to-number (match-string 2 e)) c nil))))
;; Find fields that are wider than fmax, and shorten them
(when fmax
(loop for xx in column do
@ -654,14 +658,16 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
;; Get the maximum width for each column
(push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
;; Get the fraction of numbers, to decide about alignment of the column
(setq cnt 0 frac 0.0)
(loop for x in column do
(if (equal x "")
nil
(setq frac ( / (+ (* frac cnt)
(if (string-match org-table-number-regexp x) 1 0))
(setq cnt (1+ cnt))))))
(push (>= frac org-table-number-fraction) typenums))
(if falign1
(push (equal (downcase falign1) "r") typenums)
(setq cnt 0 frac 0.0)
(loop for x in column do
(if (equal x "")
nil
(setq frac ( / (+ (* frac cnt)
(if (string-match org-table-number-regexp x) 1 0))
(setq cnt (1+ cnt))))))
(push (>= frac org-table-number-fraction) typenums)))
(setq lengths (nreverse lengths) typenums (nreverse typenums))
;; Store the alignment of this table, for later editing of single fields

View File

@ -4522,6 +4522,7 @@ between words."
'("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
'("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
'("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
'("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
;; Drawers
(list org-drawer-regexp '(0 'org-special-keyword t))
(list "^[ \t]*:END:" '(0 'org-special-keyword t))
@ -4529,8 +4530,6 @@ between words."
(list org-property-re
'(1 'org-special-keyword t)
'(3 'org-property-value t))
(if org-format-transports-properties-p
'("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
;; Links
(if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
(if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))