3

I'd like to write a mode, or at least some Lisp functions, to view a tab-separated file like a table. What I mean is lines like this:

foo\t123\tbar
yabba dabba\t12345\treally long field here

Should display something like:

| foo        | 123   | bar            |
| yabba dabba| 12345 | really long... |

Fields would line up, and long fields could be truncated. I might hide certain columns. I want to use it for viewing log files.

I'm searching the Emacs Lisp manual for ways to implement this. So far I see

  • text property '(invisible t), which could be used to truncate some fields
  • overlays and the overlay properties before-string and after-string, which I could use to add padding to fields, or draw separators

Is this how you would do it? Are there some other features and functions that would be good to use?

NickD
  • 27,023
  • 3
  • 23
  • 42
Rob N
  • 547
  • 2
  • 12

2 Answers2

5

You’re on pretty much the right track, though I am not very accomplished at writing modes myself.

However, I feel compelled to point out that csv-mode can do this for you if you want. Open a buffer and put it in csv-mode (assuming Emacs didn’t choose that mode automatically), then run csv-align-fields (bound to C-c C-a by default) and it will line up your fields into nice columns.

There are other modes for dealing with tables of various types, such as orgtbl-mode and ftable. See the valign package for pixel–accurate column alignment.

You can also use any of these packages as inspiration for your own implementation, of course.

NickD
  • 27,023
  • 3
  • 23
  • 42
db48x
  • 15,741
  • 1
  • 19
  • 23
5

One way to do (much of) what you want is as follows:

You can force org-mode on the buffer visiting the file with M-x org-mode and use C-u C-u M-x org-table-convert-region to convert each line consisting of TAB-separated fields into a table:

| foo         |   123 | bar                    |
| yabba dabba | 12345 | really long field here |

You can actually do the above without changing the mode to org-mode but there are some additional things that you can do in Org mode, e.g. you can add a line like this:

|||<15>|   
| foo         |   123 | bar                    |
| yabba dabba | 12345 | really long field here |

then press TAB to align everything:

|             |       | <15>                   |
| foo         |   123 | bar                    |
| yabba dabba | 12345 | really long field here |

and then press C-c <TAB> on the third column to toggle the width of the column:

|             |       | <15>           ...|
| foo         |   123 | bar            ...|
| yabba dabba | 12345 | really long fie...|

This is an approximation of what is shown: the actual effect is done with an overlay so it is purely for display purposes, but it does serve to fit wide tables.

There is no facility to make columns invisible, but you can delete column(s) with M-S-<left> and use normal undo facilities to bring it (them) back afterwards.

NickD
  • 27,023
  • 3
  • 23
  • 42