TransWikia.com

Adding header to a org-mode tangled file

Emacs Asked by mmmmmm on November 30, 2021

I am generating source code files from an org-mode file using org-babel-tangle.

How do I add headers to the files e.g.
for python

# This is a generated file do not edit

for emacs lisp similar plus a header for lexical binding.

These would be different headers to a file I created directly in emacs so I think autoinsert.el won’t have the flexibility.

2 Answers

You could use something this:

(defun add-tangle-headers ()
  (message "running in %s" (buffer-file-name))
  (cond
   ((f-ext? (buffer-file-name) "py")
    (goto-char (point-min))
    (insert "# This is a generated file do not editn"))
   ((f-ext? (buffer-file-name) "el")
    (goto-char (point-min))
    (insert ";;; -*- lexical-binding: t -*-n"))
   (t
    nil))
  (save-buffer))

(add-hook 'org-babel-post-tangle-hook 'add-tangle-headers)

The idea is to insert the desired line depending on the kind of src file you are tangling.

I would have thought you could use a :prologue header arg, but this seems to be only for execution.

Alternatively, you might try noweb:

First, name some source blocks like this with the header you want in each one.

#+name: elisp-header
#+BEGIN_SRC emacs-lisp
;;; -*- lexical-binding: t -*-

#+END_SRC

#+name: python-header
#+BEGIN_SRC python
# This is a generated file do not edit

#+END_SRC

Then, add a noweb yes header arg, and the target in the blocks where you want them.

#+BEGIN_SRC emacs-lisp :tangle test.el :noweb yes
<<elisp-header>>
(message "ok")
#+END_SRC

#+BEGIN_SRC python :tangle test.py :noweb yes
<<python-header>>
print('ok')
#+END_SRC

These will tangle as you want.

Answered by John Kitchin on November 30, 2021

#+BEGIN_SRC python
# This is a generated file do not edit
#+END_SRC

or for lexical binding in an elisp file

#+BEGIN_SRC emacs-lisp
;;; -*- lexical-binding: t -*-
#+END_SRC

You will of course want to make sure that's the very first BEGIN_SRC block in the file.

Answered by xeijin on November 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP