1

I'm using a Python code-block (the middle part of the code below) to convert the table (top part) into an Emacs Lisp code-block (the bottom part).

#+NAME: tmp_table
| key | trans    | description           |
|-----+----------+-----------------------|
| a.  | \alpha   | Greek lowercase alpha |
| b.  | \beta    | Greek lowercase beta  |
| e.  | \epsilon | Greek epsilon         |
| p.  | \pi      | Greek lowercase pi    |
| d.  | \delta   | Greek lowercase delta |

#+BEGIN_SRC python :var table=tmp_table :hlines no :results output code :results_switches ":tangle /tmp/tmp.el :noweb yes"
def format_table_to_elisp(table):
    for line in table:
        key, trans, desc = line
        key   = repr(key).replace("\'", "\"")
        trans = repr(trans).replace("\'", "\"")
        print(f"({key}  [{trans:<11}] ) ;; {desc}")

format_table_to_elisp(table)
#+END_SRC

#+RESULTS:
#+begin_src python :tangle /tmp/tmp.el :noweb yes
("a."  ["\\alpha"  ] ) ;; Greek lowercase alpha
("b."  ["\\beta"   ] ) ;; Greek lowercase beta
("e."  ["\\epsilon"] ) ;; Greek epsilon
("p."  ["\\pi"     ] ) ;; Greek lowercase pi
("d."  ["\\delta"  ] ) ;; Greek lowercase delta
#+end_src

So far the Python code above is converting the table into an Emacs Lisp good enough.

However, the outputted result code block (the bottom part) has #+RESULTS: #+begin_src python (Python) and has Python syntax highlighting.


How can I make a Python code block output an Emacs Lisp code-block? I.e., #+RESULTS: #+begin_src elisp

2 Answers 2

3

You can use the :wrap header to do that:

#+BEGIN_SRC python :var table=tmp_table :hlines no :results output :wrap SRC elisp :results_switches ":tangle /tmp/tmp.el :noweb yes" 
...
1
  • Thanks, it works !!
    – Garid
    Commented Apr 9, 2023 at 17:51
1

Although this is not an answer to the exact question, why not use elisp directly?

#+NAME: tmp_table
| key | trans    | description           |
|-----+----------+-----------------------|
| a.  | \alpha   | Greek lowercase alpha |
| b.  | \beta    | Greek lowercase beta  |
| e.  | \epsilon | Greek epsilon         |
| p.  | \pi      | Greek lowercase pi    |
| d.  | \delta   | Greek lowercase delta |
  
#+BEGIN_SRC emacs-lisp :var table=tmp_table :results code
  table
#+END_SRC

#+RESULTS:
#+begin_src emacs-lisp
(("a." "\\alpha" "Greek lowercase alpha")
 ("b." "\\beta" "Greek lowercase beta")
 ("e." "\\epsilon" "Greek epsilon")
 ("p." "\\pi" "Greek lowercase pi")
 ("d." "\\delta" "Greek lowercase delta"))
#+end_src

Of course, instead of printing the table directly, you could add some elisp to manipulate it if you want. For example to obtain the result as of your answer (just formatted differently), you could replace the code block with

#+BEGIN_SRC emacs-lisp :var table=tmp_table :results code
  (mapcar (lambda (line)
            (list (car line) (vector (cadr line))))
          table)
#+END_SRC
4
  • In general case, I can't write elisp, (until I become good enough) I need write it in something I'm good-enough with.
    – Garid
    Commented Apr 9, 2023 at 15:14
  • For just in this case, how to make ("a." ["\\alpha"]) not full ("a." "\\alpha" "Greek lowercase alpha") with elisp?
    – Garid
    Commented Apr 9, 2023 at 15:15
  • 1
    That's fine, of course, although learning some lisp will give you quite some Emacs 'powers'. Also, I am curious why you would like to have the result in elisp. Anyway, I'll update the answer... Commented Apr 9, 2023 at 18:10
  • Yeah, still learning elisp. I'm trying to write Quail input-method for LaTeX equation entering here (my github repo)[github.com/garid3000/Emacs-TeQ/blob/master/Emacs-TeQ.el]. I wanted to maintain this from org file (especially org table)
    – Garid
    Commented Apr 9, 2023 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.