10

I have a column in an org-mode table that contains long file names. The interesting part of the filename is the last few characters rather than the first. For example, given the files below:

/data/capture/abcd_spectrum_01292000.dat
/data/capture/abcd_spectrum_02251435.dat
/data/capture/abcd_spectrum_02251847.dat
/data/capture/abcd_spectrum_02251848.dat
/data/capture/abcd_spectrum_02251848.dat
/data/capture/abcd_spectrum_02251849.dat
/data/capture/abcd_spectrum_02251851.dat
/data/capture/abcd_spectrum_02251852.dat
/data/capture/abcd_spectrum_02251852.dat
/data/capture/abcd_spectrum_02270910.dat

I want my org table to look like this:

|            File | TD | FD | MF | Notes:               |
|-----------------+----+----+----+----------------------|
|           <r15> |    |    |    | <l20>                |
| <=_01292000.dat |    |    |    |                      |
| <=_02251435.dat |    |    |    |                      |
| <=_02251847.dat |    |    |    |                      |
| <=_02251848.dat |    |    |    |                      |
| <=_02251848.dat |    |    |    |                      |
| <=_02251849.dat |    |    |    |                      |
| <=_02251851.dat |    |    |    |                      |
| <=_02251852.dat |    |    |    |                      |
| <=_02251852.dat |    |    |    |                      |
| <=_02270910.dat |    |    |    |                      |

But instead it looks like this:

|            File | TD | FD | MF | Notes:               |
|-----------------+----+----+----+----------------------|
|           <r15> |    |    |    | <l20>                |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |
| /data/capture=> |    |    |    |                      |

Is it possible to make right-aligned columns truncate to the left, so that I can see the right-most part of the entries?

nispio
  • 8,175
  • 2
  • 35
  • 73

2 Answers2

8

As far as I know, there is no built-in support for customizing truncation of table columns. However, you can modify the command org-table-align to achieve what you want:

  1. Find the file org-table.el. It is located in the directory of your org-mode installation. The fastest way to open it is via M-x find-library RET org-table RET.

  2. Copy the definition of org-table-align to your .emacs file.

  3. Replace

    (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
    (add-text-properties (- f1 2) f1
                   (list 'display org-narrow-column-arrow)
                   xx)))))
    

    with

    (let (s1 e1 s2 e2 arrow-string)
      (if (and falign1 (equal (downcase falign1) "r"))
          (setq s1 0
                e1 (- (length xx) f1)
                s2 (- (length xx) f1)
                e2 (- (length xx) (- f1 2))
                arrow-string "<=")
        (setq s1 f1
              e1 (length xx)
              s2 (- f1 2)
              e2 f1
              arrow-string org-narrow-column-arrow))
      (add-text-properties s1 e1 (list 'org-cwidth t) xx)
      (add-text-properties s2 e2 (list 'display arrow-string) xx))))))
    

Tested using version 8.2.8 of org-mode.

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
  • 3
    Can this be done as advice? In general I try to avoid directly modifying existing libraries if possible. Especially ones that get frequent updates from up-stream sources. EDIT: I see now that you are recommending creating a copy of the function in my init file. I guess that is better than directly modifying it. – nispio Oct 06 '14 at 22:35
  • @nispio "In general I try to avoid directly modifying existing libraries if possible." That is exactly why I suggested creating a copy of the function :) Right now I don't see how you would do this using advice, since the arguments that are relevant for determining which portion of a cell to hide are calculated on the fly when calling `add-text-properties` in the original command. – itsjeyd Oct 06 '14 at 22:50
  • It would be nice if a future version of `org-mode` would break this part out into a separate defun so that it could more easily be advised or replaced. Are the `org-mode` developers pretty open to pull requests for things like this? – nispio Oct 06 '14 at 23:07
  • @nispio Not sure about pull requests, but you could try submitting a suggestion along these lines to the [mailing list](http://orgmode.org/community.html). **EDIT**: It says [here](http://orgmode.org/worg/org-contribute.html) that you can also submit patches to the mailing list, and [this page](http://orgmode.org/worg/org-contribute.html#sec-4) describes the "preferred way of submitting patches" to `org-mode`. – itsjeyd Oct 06 '14 at 23:12
  • **Update 2022**: It looks like `org-align-table` has become significantly more complex since this was written (looking at Org Mode version 9.5), and this modification no longer applies. To achieve this result in the latest org mode would require modifying `org-table--shrink-field`. I will post an update if I ever get around to making it work again. – nispio Apr 05 '22 at 03:59
0

At some point since the question was posted, the code for org-align-table received a major overhaul. The following is an advice that I wrote to wrap around the new function org-table--shrink-field to give the desired right-align behavior.

;; Advice to wrap around org-table--shrink-field so that fields that are
;; right-aligned truncate the beginning of the string instead of always
;; truncating the end of strings
;; Source: https://emacs.stackexchange.com/q/726/93
(defun my/around--shrink-field (orig-fun width align start end contents)
  (let ((args (list width align start end contents)))
    (if (or
         (= start end)                  ;no field to narrow
         (org-table--shrunk-field)      ;already shrunk
         (= 0 width)                    ;shrink to one character
         (eq contents 'hline)
         (equal contents ""))           ;no contents to hide
        (apply orig-fun args)
      (let* ((lead (org-with-point-at start (skip-chars-forward " ")))
             (trail (org-with-point-at end (abs (skip-chars-backward " "))))
             (contents-width (org-string-width
                              (buffer-substring (+ start lead) (- end trail)))))
        (if (<= width contents-width)
            (pcase align
              ("l" (apply orig-fun args))
              ("c" (apply orig-fun args))
              ("r"
               (let ((pre
                      (org-table--make-shrinking-overlay
                       start
                       (let* ((begin (+ start lead))
                              (lower begin)
                              (final (- end trail))
                              (upper (1- final))
                              (width (if (= trail 0) (1+ width) width)))
                         (catch :exit
                           (while (> (- upper lower) 1)
                             (let ((mean (+ (ash lower -1)
                                            (ash upper -1)
                                            (logand lower upper 1))))
                               (pcase (org-string-width (buffer-substring mean final))
                                 ((pred (= width)) (throw :exit mean))
                                 ((pred (< width)) (setq lower mean))
                                 (_ (setq upper mean)))))
                           lower))
                       "" contents))
                     (post
                      (and (> trail 0)
                           (org-table--make-shrinking-overlay
                            (- end trail) end "" contents))))
                 (if pre (list pre post) (list post)))))
          (apply orig-fun args))))))

(eval-after-load "org-table"
  (progn
    (advice-add 'org-table--shrink-field :around #'my/around--shrink-field)))
nispio
  • 8,175
  • 2
  • 35
  • 73