reddit / emacs

Tab separated values

gemilg · 2022-11-18 08:45
Hey guys, I am currently using emacs to analyze files with tab separated values.I already wrote some basic highlighting (different colors for different columns). Is there any way to specify value of tab-width for specific columns?For example, I would like to have tab for first column be 4, for second it would be 10, for third it can be 2 and so on. I checked the csv-mode, but the files that I am working on are not pure .tsv. First part of the file is actually free text, then there are tab separated values. So aligning the columns with csv-mode creates veeery long columns(which is totally unreadable), due to long lines of text in the beginning.
llcc_reddit · 2022-11-18 08:55
It seems like that you are looking for csv-mode. https://elpa.gnu.org/packages/csv-mode.html
gemilg · 2022-11-18 09:03
I checked the csv-mode, it sort of does what I want to, but there is a problem. Files that I am working on are not purely tsv. First part of the file is actually free text, then there are tab separated values. So aligning the columns with csv-mode creates veeery long columns(which is totally unreadable), due to long lines of text in the beginning. That is why I asked for the tab-width. Ofc I can increase or decrease the value so the columns are alligned, but then I have these long empty spaces between columns which is suboptimal. The thing that would work for me would be to specify tab-width per column.
sceadu · 2022-11-18 09:43
You could put it into a table in org mode and specify column widths and collapse them down?
gemilg · 2022-11-18 10:17
The amount of data is pretty big, like 100k lines usually, so I don't think that org-mode will cut it. Also I do not want to add any intermediate processing(putting the table in org mode, specifying the width and collapsing) of the file, I just want to open it in some custom made mode and see the values nicely aligned :)
jplindstrom · 2022-11-18 10:57
If you want to work on only a part of the buffer, look at https://www.emacswiki.org/emacs/Narrowing
MechoLupan · 2022-11-18 11:37
Search for `emacs "display tab stops"`. There's a solution in the Emacs Stack Exchange.
rguy84 · 2022-11-18 12:27
Op could just use whitespace-mode in that case
MechoLupan · 2022-11-18 12:46
You parsed it wrongly. It's "(display tab) stops". "display (tab stops)" doesn't have anything to do with what OP wants.
arthurno1 · 2022-11-18 15:13
> I checked the csv-mode, but the files that I am working on are not pure .tsv. > First part of the file is actually free text, then there are tab separated values. So aligning the columns with csv-mode creates veeery long columns(which is totally unreadable), due to long lines of text in the beginning. [You can hide free text](https://www.gnu.org/software/emacs/manual/html_node/elisp/Invisible-Text.html); That is what is used when you hide/display headings in org-mode for example. You can also programmatically transform your file into another file without free text, so you only have columns and work with that file in Emacs instead. > I already wrote some basic highlighting (different colors for different columns). If you already have some basic font-lock (highlitning), then I guess you have a small minor mode to toggle that syntax off/on. Can't you also make free text invisible when you toggle your mode on, too? > I would like to have tab for first column be 4, for second it would be 10, for third it can be 2 and so on. Same here, you could as well have a function to reparse your data too? Instead of complicating things, you can hard-code a function to just replace each tab with the number of spaces you would like to have, first tap with 4, second with 10, for third 2 and so on. When/if you save your data, just replace all spaces with a single tab again: (defun tsv-untabify (&optional ignore-errors) (save-excursion (goto-char 1) (while (not (eobp)) (if (search-forward "\t" (line-end-position) ignore-errors) (replace-match " ")) (if (search-forward "\t" (line-end-position) ignore-errors) (replace-match " " )) (if (search-forward "\t" (line-end-position) ignore-errors) (replace-match " ")) (forward-line 1)))) (defun tsv-tabify (&optional ignore-errors) (save-excursion (goto-char 1) (while (not (eobp)) (if (search-forward " " (line-end-position) ignore-errors) (replace-match "\t")) (if (search-forward " " (line-end-position) ignore-errors) (replace-match "\t")) (if (search-forward " " (line-end-position) ignore-errors) (replace-match "\t")) (forward-line 1)))) Adapt to your real case and call tsv-untabify when you turn your mode on, and tabify when you turn it off. Otherwise, you could use a "spacer", i.e. put a display property on each tab and make it a certain width which would be a number of spaces you prefer. See the docs for [display properties](https://www.gnu.org/software/emacs/manual/html_node/elisp/Pixel-Specification.html).
owmagow · 2022-11-18 15:24
I wrote a small function that allows you to create tabular columns by providing it with your target column number and the last word printed. It is in CL but could be easily translated to elisp. PM me if you think it would help, and i can send it to you.
11fdriver · 2022-11-18 16:27
Out of interest, why not just *M-x* `edit-tab-stops`?
arthurno1 · 2022-11-18 16:35
No idea, never used it 😀
gemilg · 2022-11-18 16:59
I never knew about this command (and probably about 99% commands in emacs). I think this is not the thing that I am looking for. I am not sure if this can work as a "general" identation in minor/major mode. From the [https://www.gnu.org/software/emacs/manual/html\_node/emacs/Tab-Stops.html](https://www.gnu.org/software/emacs/manual/html_node/emacs/Tab-Stops.html) I see that: >Emacs defines certain column numbers to be tab stops. These are used as stopping points by TAB when inserting whitespace in Text mode and related modes (see Indentation), and by commands like M-i (see Indentation Commands). The variable tab-stop-list controls these positions It looks like this can be used with manual indentation using TAB. Or am I missing something?
gemilg · 2022-11-18 17:02
This worked perfectly, because I can easily add this to my custom mode, for curious: https://emacs.stackexchange.com/questions/12872/is-there-a-way-to-set-a-list-of-display-tab-stops
11fdriver · 2022-11-18 18:57
Oh, I misunderstood your intent. My bad! If you're planning on creating a minor mode for TSV, then perhaps you could try deriving from tabulated list mode. I understand that's a little involved, perhaps. But personally, if this is just a one-off kinda thing, then I would check whether `table-capture` is fast enough for the size of your data. You can then convert back out with `table-release`. You can set column widths when capturing the table, and do various formatting, and it acts upon a region, so you can leave out your freeform text.