0

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.

1

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 edit\n"))
   ((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.

| improve this answer | |
0
#+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.

| improve this answer | |
  • I am looking for way of saying that anew file does this rather than hard coding it – mmmmmm Feb 23 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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