Adding header to a org-mode tangled file

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.

Emacs Asked by mmmmmm on November 30, 2021

2 Answers

2 Answers

You could use something this:

(defun add-tangle-headers ()
  (message "running in %s" (buffer-file-name))
   ((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"))

(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 -*-


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


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
(message "ok")

#+BEGIN_SRC python :tangle :noweb yes

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

or for lexical binding in an elisp file

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

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!

Related Questions

Force Emacs to start in text mode

1  Asked on November 15, 2021 by ddc


Move point inside of image overlay

2  Asked on November 10, 2021 by cardano


Emacs 26.3: Weird frame title issue with KDE

1  Asked on November 10, 2021 by ed-sabol


How to call `org-sparse-tree` from Lisp?

1  Asked on September 2, 2021 by user2567544


Flycheck – Eslint: ‘config file: missing or incorrect’

1  Asked on September 2, 2021 by ben-krck


Emacs fails to open gnuplot files with extension .gp

1  Asked on September 2, 2021 by paul-c


Automatically activating python virtual environments with pyvenv

1  Asked on September 2, 2021 by jacob-pavlock


Ask a Question

Get help from others!

© 2022 All rights reserved.