amiralul · 2022-04-29 08:50
It takes a while to get used to Lisp technology when you're not a programmer, and you've only been playing with things like Python for small applications needed for personal use.
I have a [pet project in org-mode](https://github.com/ctanas/parsec.ro) that reads some data from a text file and arrange them nicely, but I'm currently using a way too complex process to do this, involving PostgreSQL that makes my org-mode export kinda slow. I think it would be faster to replace all the SQL quarries with Elisp and that would also reduce my dependence on other software and keep everything within Emacs.
Basically, I would need three things and if anyone is willing to help me with hints, idea or code snippets, that would be great:
1. Read a text file (over 6000 lines long) in, I suppose, a list; or a list of lists?; the text file is fixed-formatted, ideally the reading process would acknowledge that;
2. Count some list elements (atoms, I guess?) and have the results available as variables across the org-mode file;
3. Create org-mode tables with list elements of my choosing (n-th elements from the m-th list, p-th elements from q-th list and so on).
Is this doable in Elisp?
h-m-b-tu · 2022-04-29 09:16
If you are into formal languages you could create a parser by one of the available frameworks. For example [Parsing Expression Grammars](http://elpa.gnu.org/packages/peg.html).
TeMPOraL_PL · 2022-04-29 11:27
I haven't done anything like it in Elisp so far, but I have a possible hint for 1:
> Read a text file (over 6000 lines long) in, I suppose, a list; or a list of lists?
Elisp already has a data structure dedicated for storing and processing text: *a buffer*. So I'd start by just reading the file into a buffer, and use functions operating on buffers to navigate lines and identify tokens.
amiralul · 2022-04-29 11:45
Now that you mention it, it could work. This way, I don’t need variables to store things I’m looking for, I could directly pick them from the buffer when I’d need them. This could work…
yantar92 · 2022-04-29 11:49
> Create org-mode tables with list elements of my choosing (n-th elements from the m-th list, p-th elements from q-th list and so on).
You can programmatically generate Org files using org-element API. Using org-element-interpret-data. You can examine structure of org tables by creating an example table in a new Org buffer and then running org-element-parse-buffer
arthurno1 · 2022-04-30 05:41
> Now that you mention it, it could work.
It is not *it could*, it is how it works :-). Emacs is lisp with text processing extensions. Buffer is the basic block.
Typically
1. Read a text file
(with-temp-buffer
(insert-file-contents-literally "your-text-file-name")
2. Count some list elements
(while (re-search-forward "your-element" nil t)
(put your stuff directly in org-table ...
no need for intermediary lists
but if you prefer to keep your stuff in separate data structurs
you can push stuff in various list and pick it later into tables
as a separate step ))
> Is this doable in Elisp?
Yes, relatively easy.
You should take a look at elisp manual and org manual that comes with your Emacs. C-h i will get you there.