AnswerBun.com

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))
  (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!

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 AnswerBun.com. All rights reserved.