2

I want to do literate programming. Entering language after every code block is prone to errors and distracting. To make matters worse, I do not know how to verify if all my blocks were tangled. I can end up in a situation where my code does not work because I forgot language after #+begin_src

Is it possible to declare at the top of the file a default language for all code blocks? Is there an example available? I have spent a few hours in vain trying to digest contradicting advice possibly created for different versions of org-mode. I use org-mode 9.5.3

5
  • I am not sure what kind of contradicting advice you have 'digested', but I would advise you to either use org-babel-demarcate-block (using some handy shortcut), or create useful snippets with yasnippet (using either a shortcut, or maybe even handier keys + expansion). You might use snippets you find on the web, but it is easy enough to create custom snippets yourself. Commented May 10, 2022 at 11:35
  • Your advice does not answer my question. If I understand you correctly you suggest something like typing scb and using emacs to expand it into a source code block with declared language. I can press a shortcut C-c , s to get a generated source code block. I want it to leave it as it is without adding any headers to the block. there seems to be a possibility to do what I need in the properties at the top of the org file. Doing it that way would be more flexible if I wanted different literate files for different langiages. Commented May 10, 2022 at 11:46
  • emacsdocs.org/docs/org/Structure-of-Code-Blocks#language says the language is mandatory. So there must have been a change in the org mode. Please correct me if I am wrong. Commented May 10, 2022 at 12:16
  • 1
    Indeed, therefore I did not post it as an answer. Can you tell me what C-c , s does? With yasnippet keys you can e.g. insert 'p' followed by your shortcut for yas-maybe-expand to insert a python code block, and 'r' + the shortcut, to enter a ruby code block. I am not sure why you think your approach is more flexible, but you should use the solution that you prefer of course. I just wasn't sure about the advice you had already digested. At least, now it is clear that this advice does not include a satisfactory solution. Commented May 10, 2022 at 12:18
  • I have seen some vague document written years ago suggesting I could have the language argument defined once at the top of the file. Commented May 10, 2022 at 12:23

2 Answers 2

4

In order not to type the language each time you create a code block, I propose you use some keybindings. Here is an example based on my config:

#+begin_src emacs-lisp (add-to-list 'org-structure-template-alist '("b" . "src sh \n")) (add-to-list 'org-structure-template-alist '("d" . "src d2 :file ./Exports/Pics/filename.png :flags -t 4 -l elk \n")) (add-to-list 'org-structure-template-alist '("i" . "src emacs-lisp \n")) (add-to-list 'org-structure-template-alist '("j" . "src jupyter-python :session py \n")) (add-to-list 'org-structure-template-alist '("p" . "src python \n")) (add-to-list 'org-structure-template-alist '("P" . "src jupyter-python :session py :display plain \n")) (add-to-list 'org-structure-template-alist '("t" . "src latex \n")) #+end_src 

With something like this in your init, when you type C-c C-, P the following block will appear:

#+begin_src jupyter-python :session py :display plain #+end_src 
2

It is not possible now. The documentation says the language argument is mandatory.

https://emacsdocs.org/docs/org/Structure-of-Code-Blocks#language

At the top of the file, I can have the property

#+PROPERTY: header-args :tangle "./some-file.el" 

Then I can have some content and the block:

#+begin_src emacs-lisp (provide 'some-file) ;;; some-file.el ends here #+end_src 

So I do not have to repeat that tangle in every code block, but the language, in this case, emacs-lisp is mandatory. The suggested way around the problem was using yasnippet. https://github.com/joaotavora/yasnippet

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.