2016-06-18 22:05:25 +00:00
|
|
|
;;; ob-tangle.el --- Extract Source Code From Org Files -*- lexical-binding: t; -*-
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2009-10-29 23:03:43 +00:00
|
|
|
;; Author: Eric Schulte
|
2009-06-15 01:30:29 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 07:44:29 +00:00
|
|
|
;; URL: https://orgmode.org
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2010-06-25 16:20:39 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2010-06-25 16:20:39 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-06-15 01:30:29 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:20:39 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-06-15 01:30:29 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 16:20:39 +00:00
|
|
|
|
2009-06-15 01:30:29 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-06-15 01:30:29 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Extract the code from source blocks out into raw source-code files.
|
|
|
|
|
|
|
|
;;; Code:
|
2016-07-25 14:34:48 +00:00
|
|
|
|
2022-08-04 13:53:05 +00:00
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2016-07-25 14:34:48 +00:00
|
|
|
(require 'cl-lib)
|
2010-06-23 18:24:33 +00:00
|
|
|
(require 'org-src)
|
2016-12-10 08:54:55 +00:00
|
|
|
(require 'org-macs)
|
2019-03-09 09:58:41 +00:00
|
|
|
(require 'ol)
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2015-06-15 22:09:20 +00:00
|
|
|
(declare-function make-directory "files" (dir &optional parents))
|
2016-01-31 10:37:05 +00:00
|
|
|
(declare-function org-at-heading-p "org" (&optional ignored))
|
2016-05-15 02:56:53 +00:00
|
|
|
(declare-function org-babel-update-block-body "ob-core" (new-body))
|
|
|
|
(declare-function org-back-to-heading "org" (&optional invisible-ok))
|
2015-06-15 22:09:20 +00:00
|
|
|
(declare-function org-before-first-heading-p "org" ())
|
2023-05-16 10:41:53 +00:00
|
|
|
(declare-function org-element-lineage "org-element-ast" (datum &optional types with-self))
|
|
|
|
(declare-function org-element-property "org-element-ast" (property node))
|
2023-05-03 14:26:41 +00:00
|
|
|
(declare-function org-element-begin "org-element" (node))
|
2022-01-06 17:39:05 +00:00
|
|
|
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
|
2023-05-16 10:41:53 +00:00
|
|
|
(declare-function org-element-type-p "org-element-ast" (node types))
|
2015-06-15 22:09:20 +00:00
|
|
|
(declare-function org-heading-components "org" ())
|
|
|
|
(declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
|
2020-09-07 04:49:12 +00:00
|
|
|
(declare-function org-in-archived-heading-p "org" (&optional no-inheritance))
|
2015-06-15 22:09:20 +00:00
|
|
|
(declare-function outline-previous-heading "outline" ())
|
2022-01-30 08:42:58 +00:00
|
|
|
(defvar org-id-link-to-org-use-id) ; Dynamically scoped
|
2010-06-23 18:24:33 +00:00
|
|
|
|
2022-11-15 22:49:56 +00:00
|
|
|
(defgroup org-babel-tangle nil
|
|
|
|
"Options for extracting source code from code blocks."
|
|
|
|
:tag "Org Babel Tangle"
|
|
|
|
:group 'org-babel)
|
|
|
|
|
2010-06-17 16:01:24 +00:00
|
|
|
(defcustom org-babel-tangle-lang-exts
|
2014-03-05 15:49:30 +00:00
|
|
|
'(("emacs-lisp" . "el")
|
|
|
|
("elisp" . "el"))
|
2010-06-17 16:01:24 +00:00
|
|
|
"Alist mapping languages to their file extensions.
|
|
|
|
The key is the language name, the value is the string that should
|
|
|
|
be inserted as the extension commonly used to identify files
|
|
|
|
written in this language. If no entry is found in this list,
|
|
|
|
then the name of the language is used."
|
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2010-06-17 16:01:24 +00:00
|
|
|
:type '(repeat
|
|
|
|
(cons
|
|
|
|
(string "Language name")
|
|
|
|
(string "File Extension"))))
|
2010-05-27 22:32:10 +00:00
|
|
|
|
2013-10-09 16:00:28 +00:00
|
|
|
(defcustom org-babel-tangle-use-relative-file-links t
|
2016-08-23 20:13:56 +00:00
|
|
|
"Use relative path names in links from tangled source back the Org file."
|
2013-10-09 16:00:28 +00:00
|
|
|
:group 'org-babel-tangle
|
|
|
|
:type 'boolean)
|
|
|
|
|
2010-07-08 18:53:51 +00:00
|
|
|
(defcustom org-babel-post-tangle-hook nil
|
|
|
|
"Hook run in code files tangled by `org-babel-tangle'."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2010-07-08 18:53:51 +00:00
|
|
|
:type 'hook)
|
|
|
|
|
2010-09-03 13:51:13 +00:00
|
|
|
(defcustom org-babel-pre-tangle-hook '(save-buffer)
|
2022-08-27 03:15:23 +00:00
|
|
|
"Hook run at the beginning of `org-babel-tangle' in the original buffer."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2010-09-03 13:51:13 +00:00
|
|
|
:type 'hook)
|
|
|
|
|
2011-03-14 15:31:33 +00:00
|
|
|
(defcustom org-babel-tangle-body-hook nil
|
|
|
|
"Hook run over the contents of each code block body."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2011-03-14 15:31:33 +00:00
|
|
|
:type 'hook)
|
|
|
|
|
2022-08-27 03:15:23 +00:00
|
|
|
(defcustom org-babel-tangle-finished-hook nil
|
|
|
|
"Hook run at the very end of `org-babel-tangle' in the original buffer.
|
|
|
|
In this way, it is the counterpart to `org-babel-pre-tangle-hook'."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2022-08-27 03:15:23 +00:00
|
|
|
:package-version '(Org . "9.6")
|
|
|
|
:type 'hook)
|
|
|
|
|
2010-10-29 07:37:28 +00:00
|
|
|
(defcustom org-babel-tangle-comment-format-beg "[[%link][%source-name]]"
|
2010-09-05 21:12:43 +00:00
|
|
|
"Format of inserted comments in tangled code files.
|
|
|
|
The following format strings can be used to insert special
|
|
|
|
information into the output using `org-fill-template'.
|
|
|
|
%start-line --- the line number at the start of the code block
|
|
|
|
%file --------- the file from which the code block was tangled
|
2016-08-23 20:13:56 +00:00
|
|
|
%link --------- Org style link to the code block
|
2010-10-17 17:18:20 +00:00
|
|
|
%source-name -- name of the code block
|
|
|
|
|
2014-06-07 15:29:03 +00:00
|
|
|
Upon insertion the formatted comment will be commented out, and
|
|
|
|
followed by a newline. To inhibit this post-insertion processing
|
|
|
|
set the `org-babel-tangle-uncomment-comments' variable to a
|
|
|
|
non-nil value.
|
|
|
|
|
2010-10-17 17:18:20 +00:00
|
|
|
Whether or not comments are inserted during tangling is
|
|
|
|
controlled by the :comments header argument."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2010-09-05 21:12:43 +00:00
|
|
|
:type 'string)
|
|
|
|
|
2010-10-29 07:37:28 +00:00
|
|
|
(defcustom org-babel-tangle-comment-format-end "%source-name ends here"
|
2010-09-05 21:12:43 +00:00
|
|
|
"Format of inserted comments in tangled code files.
|
|
|
|
The following format strings can be used to insert special
|
|
|
|
information into the output using `org-fill-template'.
|
|
|
|
%start-line --- the line number at the start of the code block
|
|
|
|
%file --------- the file from which the code block was tangled
|
2016-08-23 20:13:56 +00:00
|
|
|
%link --------- Org style link to the code block
|
2010-10-17 17:18:20 +00:00
|
|
|
%source-name -- name of the code block
|
|
|
|
|
2014-06-07 15:29:03 +00:00
|
|
|
Upon insertion the formatted comment will be commented out, and
|
|
|
|
followed by a newline. To inhibit this post-insertion processing
|
|
|
|
set the `org-babel-tangle-uncomment-comments' variable to a
|
|
|
|
non-nil value.
|
|
|
|
|
2010-10-17 17:18:20 +00:00
|
|
|
Whether or not comments are inserted during tangling is
|
|
|
|
controlled by the :comments header argument."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2010-09-05 21:12:43 +00:00
|
|
|
:type 'string)
|
|
|
|
|
2014-06-07 15:29:03 +00:00
|
|
|
(defcustom org-babel-tangle-uncomment-comments nil
|
|
|
|
"Inhibits automatic commenting and addition of trailing newline
|
|
|
|
of tangle comments. Use `org-babel-tangle-comment-format-beg'
|
|
|
|
and `org-babel-tangle-comment-format-end' to customize the format
|
|
|
|
of tangled comments."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2014-06-07 15:29:03 +00:00
|
|
|
:type 'boolean)
|
|
|
|
|
2016-06-18 22:05:25 +00:00
|
|
|
(defcustom org-babel-process-comment-text 'org-remove-indentation
|
2016-08-23 20:13:56 +00:00
|
|
|
"Function called to process raw Org text collected to be
|
2011-09-15 22:00:10 +00:00
|
|
|
inserted as comments in tangled source-code files. The function
|
|
|
|
should take a single string argument and return a string
|
2013-11-23 22:57:29 +00:00
|
|
|
result. The default value is `org-remove-indentation'."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2011-09-15 22:00:10 +00:00
|
|
|
:type 'function)
|
|
|
|
|
2023-04-13 05:39:50 +00:00
|
|
|
(defcustom org-babel-tangle-default-file-mode #o644
|
2021-09-30 18:02:22 +00:00
|
|
|
"The default mode used for tangled files, as an integer.
|
2023-04-13 05:39:50 +00:00
|
|
|
The default value 420 correspands to the octal #o644, which is
|
2021-09-30 18:02:22 +00:00
|
|
|
read-write permissions for the user, read-only for everyone else."
|
2022-11-15 22:49:56 +00:00
|
|
|
:group 'org-babel-tangle
|
2021-11-20 19:36:21 +00:00
|
|
|
:package-version '(Org . "9.6")
|
2021-09-30 18:02:22 +00:00
|
|
|
:type 'integer)
|
|
|
|
|
2024-01-23 20:02:20 +00:00
|
|
|
(defcustom org-babel-tangle-remove-file-before-write 'auto
|
|
|
|
"How to overwrite the existing tangle target.
|
|
|
|
When set to nil, `org-babel-tangle' will replace contents of an existing
|
|
|
|
tangle target (and fail when tangle target is read-only).
|
|
|
|
When set to t, the tangle target (including read-only) will be deleted
|
|
|
|
first and a new file, possibly with different ownership and
|
|
|
|
permissions, will be created.
|
|
|
|
When set to symbol `auto', overwrite read-only tangle targets and
|
|
|
|
replace contents otherwise."
|
|
|
|
:group 'org-babel-tangle
|
|
|
|
:package-version '(Org . "9.7")
|
|
|
|
:type '(choice
|
|
|
|
(const :tag "Replace contents, but keep the same file" nil)
|
|
|
|
(const :tag "Re-create file" t)
|
|
|
|
(const :tag "Re-create when read-only" auto))
|
|
|
|
:safe t)
|
|
|
|
|
2010-07-28 13:34:08 +00:00
|
|
|
(defun org-babel-find-file-noselect-refresh (file)
|
|
|
|
"Find file ensuring that the latest changes on disk are
|
|
|
|
represented in the file."
|
2013-06-07 10:43:55 +00:00
|
|
|
(find-file-noselect file 'nowarn)
|
2010-07-28 13:34:08 +00:00
|
|
|
(with-current-buffer (get-file-buffer file)
|
|
|
|
(revert-buffer t t t)))
|
|
|
|
|
2010-07-16 22:55:40 +00:00
|
|
|
(defmacro org-babel-with-temp-filebuffer (file &rest body)
|
|
|
|
"Open FILE into a temporary buffer execute BODY there like
|
|
|
|
`progn', then kill the FILE buffer returning the result of
|
|
|
|
evaluating BODY."
|
2021-02-12 21:08:01 +00:00
|
|
|
(declare (indent 1) (debug t))
|
2012-08-03 19:33:31 +00:00
|
|
|
(let ((temp-path (make-symbol "temp-path"))
|
|
|
|
(temp-result (make-symbol "temp-result"))
|
2010-07-28 13:34:08 +00:00
|
|
|
(temp-file (make-symbol "temp-file"))
|
2010-07-29 00:45:34 +00:00
|
|
|
(visited-p (make-symbol "visited-p")))
|
2012-08-03 19:33:31 +00:00
|
|
|
`(let* ((,temp-path ,file)
|
|
|
|
(,visited-p (get-file-buffer ,temp-path))
|
|
|
|
,temp-result ,temp-file)
|
|
|
|
(org-babel-find-file-noselect-refresh ,temp-path)
|
|
|
|
(setf ,temp-file (get-file-buffer ,temp-path))
|
2010-07-28 13:34:08 +00:00
|
|
|
(with-current-buffer ,temp-file
|
|
|
|
(setf ,temp-result (progn ,@body)))
|
|
|
|
(unless ,visited-p (kill-buffer ,temp-file))
|
2010-07-16 22:55:40 +00:00
|
|
|
,temp-result)))
|
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2020-02-12 21:40:45 +00:00
|
|
|
(defun org-babel-tangle-file (file &optional target-file lang-re)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Extract the bodies of source code blocks in FILE.
|
|
|
|
Source code blocks are extracted with `org-babel-tangle'.
|
2021-09-16 10:32:43 +00:00
|
|
|
|
2010-07-13 23:20:08 +00:00
|
|
|
Optional argument TARGET-FILE can be used to specify a default
|
2021-09-16 10:32:43 +00:00
|
|
|
export file for all source blocks.
|
|
|
|
|
|
|
|
Optional argument LANG-RE can be used to limit the exported
|
|
|
|
source code blocks by languages matching a regular expression.
|
|
|
|
|
2022-11-10 05:36:01 +00:00
|
|
|
Return list of the tangled file names."
|
2009-10-20 14:17:42 +00:00
|
|
|
(interactive "fFile to tangle: \nP")
|
2024-03-16 08:21:30 +00:00
|
|
|
(org-with-file-buffer file
|
|
|
|
(org-with-wide-buffer
|
|
|
|
(mapcar #'expand-file-name
|
|
|
|
(org-babel-tangle nil target-file lang-re)))))
|
2009-07-15 01:26:15 +00:00
|
|
|
|
2010-04-12 05:54:31 +00:00
|
|
|
(defun org-babel-tangle-publish (_ filename pub-dir)
|
|
|
|
"Tangle FILENAME and place the results in PUB-DIR."
|
2015-10-03 22:08:52 +00:00
|
|
|
(unless (file-exists-p pub-dir)
|
|
|
|
(make-directory pub-dir t))
|
2017-09-11 05:22:55 +00:00
|
|
|
(setq pub-dir (file-name-as-directory pub-dir))
|
2023-11-17 16:06:53 +00:00
|
|
|
;; Rename files to avoid copying to same file when publishing to ./
|
|
|
|
;; `copy-file' would throw an error when copying file to self.
|
|
|
|
(mapc (lambda (el) (rename-file el pub-dir t))
|
|
|
|
(org-babel-tangle-file filename)))
|
2010-04-12 05:54:31 +00:00
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2020-02-12 21:40:45 +00:00
|
|
|
(defun org-babel-tangle (&optional arg target-file lang-re)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Write code blocks to source-specific files.
|
|
|
|
Extract the bodies of all source code blocks from the current
|
2022-11-10 05:36:01 +00:00
|
|
|
file into their own source-specific files. Return the list of files.
|
2013-03-02 14:27:25 +00:00
|
|
|
With one universal prefix argument, only tangle the block at point.
|
|
|
|
When two universal prefix arguments, only tangle blocks for the
|
|
|
|
tangle file of the block at point.
|
|
|
|
Optional argument TARGET-FILE can be used to specify a default
|
2020-02-12 21:40:45 +00:00
|
|
|
export file for all source blocks. Optional argument LANG-RE can
|
|
|
|
be used to limit the exported source code blocks by languages
|
|
|
|
matching a regular expression."
|
2011-06-17 23:41:50 +00:00
|
|
|
(interactive "P")
|
2010-09-03 13:51:13 +00:00
|
|
|
(run-hooks 'org-babel-pre-tangle-hook)
|
2013-03-02 14:27:25 +00:00
|
|
|
;; Possibly Restrict the buffer to the current code block
|
2011-06-17 23:41:50 +00:00
|
|
|
(save-restriction
|
2013-10-31 05:47:44 +00:00
|
|
|
(save-excursion
|
|
|
|
(when (equal arg '(4))
|
|
|
|
(let ((head (org-babel-where-is-src-block-head)))
|
2013-03-02 16:15:08 +00:00
|
|
|
(if head
|
|
|
|
(goto-char head)
|
|
|
|
(user-error "Point is not in a source code block"))))
|
2012-08-10 13:40:00 +00:00
|
|
|
(let ((block-counter 0)
|
|
|
|
(org-babel-default-header-args
|
|
|
|
(if target-file
|
|
|
|
(org-babel-merge-params org-babel-default-header-args
|
|
|
|
(list (cons :tangle target-file)))
|
|
|
|
org-babel-default-header-args))
|
2013-03-02 14:27:25 +00:00
|
|
|
(tangle-file
|
|
|
|
(when (equal arg '(16))
|
2022-10-18 15:34:18 +00:00
|
|
|
(or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'no-eval))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(user-error "Point is not in a source code block"))))
|
2024-01-08 15:58:24 +00:00
|
|
|
path-collector
|
|
|
|
(source-file buffer-file-name))
|
2021-05-01 19:18:44 +00:00
|
|
|
(mapc ;; map over file-names
|
|
|
|
(lambda (by-fn)
|
|
|
|
(let ((file-name (car by-fn)))
|
|
|
|
(when file-name
|
|
|
|
(let ((lspecs (cdr by-fn))
|
|
|
|
(fnd (file-name-directory file-name))
|
|
|
|
modes make-dir she-banged lang)
|
|
|
|
;; drop source-blocks to file
|
|
|
|
;; We avoid append-to-file as it does not work with tramp.
|
|
|
|
(with-temp-buffer
|
|
|
|
(mapc
|
|
|
|
(lambda (lspec)
|
|
|
|
(let* ((block-lang (car lspec))
|
|
|
|
(spec (cdr lspec))
|
|
|
|
(get-spec (lambda (name) (cdr (assq name (nth 4 spec)))))
|
|
|
|
(she-bang (let ((sheb (funcall get-spec :shebang)))
|
|
|
|
(when (> (length sheb) 0) sheb)))
|
|
|
|
(tangle-mode (funcall get-spec :tangle-mode)))
|
|
|
|
(unless (string-equal block-lang lang)
|
|
|
|
(setq lang block-lang)
|
|
|
|
(let ((lang-f (org-src-get-lang-mode lang)))
|
|
|
|
(when (fboundp lang-f) (ignore-errors (funcall lang-f)))))
|
|
|
|
;; if file contains she-bangs, then make it executable
|
|
|
|
(when she-bang
|
|
|
|
(unless tangle-mode (setq tangle-mode #o755)))
|
|
|
|
(when tangle-mode
|
2021-09-30 18:02:22 +00:00
|
|
|
(add-to-list 'modes (org-babel-interpret-file-mode tangle-mode)))
|
2021-05-01 19:18:44 +00:00
|
|
|
;; Possibly create the parent directories for file.
|
|
|
|
(let ((m (funcall get-spec :mkdirp)))
|
|
|
|
(and m fnd (not (string= m "no"))
|
|
|
|
(setq make-dir t)))
|
|
|
|
;; Handle :padlines unless first line in file
|
|
|
|
(unless (or (string= "no" (funcall get-spec :padline))
|
|
|
|
(= (point) (point-min)))
|
|
|
|
(insert "\n"))
|
|
|
|
(when (and she-bang (not she-banged))
|
2016-12-09 23:19:45 +00:00
|
|
|
(insert (concat she-bang "\n"))
|
2021-05-01 19:18:44 +00:00
|
|
|
(setq she-banged t))
|
|
|
|
(org-babel-spec-to-string spec)
|
|
|
|
(setq block-counter (+ 1 block-counter))))
|
|
|
|
lspecs)
|
|
|
|
(when make-dir
|
|
|
|
(make-directory fnd 'parents))
|
2022-05-08 04:32:40 +00:00
|
|
|
(unless
|
|
|
|
(and (file-exists-p file-name)
|
|
|
|
(let ((tangle-buf (current-buffer)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents file-name)
|
|
|
|
(and
|
|
|
|
(equal (buffer-size)
|
|
|
|
(buffer-size tangle-buf))
|
|
|
|
(= 0
|
|
|
|
(let (case-fold-search)
|
|
|
|
(compare-buffer-substrings
|
|
|
|
nil nil nil
|
|
|
|
tangle-buf nil nil)))))))
|
2024-01-08 15:58:24 +00:00
|
|
|
(when (equal (if (file-name-absolute-p file-name)
|
|
|
|
file-name
|
|
|
|
(expand-file-name file-name))
|
|
|
|
(if (file-name-absolute-p source-file)
|
|
|
|
source-file
|
|
|
|
(expand-file-name source-file)))
|
|
|
|
(error "Not allowed to tangle into the same file as self"))
|
2023-12-04 12:55:05 +00:00
|
|
|
;; We do not erase, but overwrite previous file
|
|
|
|
;; to preserve any existing symlinks.
|
2024-01-23 20:02:20 +00:00
|
|
|
;; This behavior is modified using
|
|
|
|
;; `org-babel-tangle-remove-file-before-write' to
|
|
|
|
;; tangle to read-only files.
|
|
|
|
(when (and
|
|
|
|
(file-exists-p file-name)
|
|
|
|
(pcase org-babel-tangle-remove-file-before-write
|
|
|
|
(`auto (not (file-writable-p file-name)))
|
|
|
|
(`t t)
|
|
|
|
(`nil nil)
|
|
|
|
(_ (error "Invalid value of `org-babel-tangle-remove-file-before-write': %S"
|
|
|
|
org-babel-tangle-remove-file-before-write))))
|
|
|
|
(delete-file file-name))
|
|
|
|
(write-region nil nil file-name)
|
2022-05-08 04:32:40 +00:00
|
|
|
(mapc (lambda (mode) (set-file-modes file-name mode)) modes))
|
2021-05-01 19:18:44 +00:00
|
|
|
(push file-name path-collector))))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(if (equal arg '(4))
|
|
|
|
(org-babel-tangle-single-block 1 t)
|
2020-02-12 21:40:45 +00:00
|
|
|
(org-babel-tangle-collect-blocks lang-re tangle-file)))
|
2012-08-13 03:59:44 +00:00
|
|
|
(message "Tangled %d code block%s from %s" block-counter
|
2012-08-10 13:40:00 +00:00
|
|
|
(if (= block-counter 1) "" "s")
|
|
|
|
(file-name-nondirectory
|
2013-06-10 15:55:28 +00:00
|
|
|
(buffer-file-name
|
2021-09-19 12:50:37 +00:00
|
|
|
(or (buffer-base-buffer)
|
|
|
|
(current-buffer)
|
|
|
|
(and (org-src-edit-buffer-p)
|
|
|
|
(org-src-source-buffer))))))
|
2012-08-10 13:40:00 +00:00
|
|
|
;; run `org-babel-post-tangle-hook' in all tangled files
|
|
|
|
(when org-babel-post-tangle-hook
|
|
|
|
(mapc
|
|
|
|
(lambda (file)
|
|
|
|
(org-babel-with-temp-filebuffer file
|
|
|
|
(run-hooks 'org-babel-post-tangle-hook)))
|
2021-05-01 19:18:44 +00:00
|
|
|
path-collector))
|
2022-08-27 03:15:23 +00:00
|
|
|
(run-hooks 'org-babel-tangle-finished-hook)
|
2021-05-01 19:18:44 +00:00
|
|
|
path-collector))))
|
2009-06-26 18:43:50 +00:00
|
|
|
|
2021-09-30 18:02:22 +00:00
|
|
|
(defun org-babel-interpret-file-mode (mode)
|
|
|
|
"Determine the integer representation of a file MODE specification.
|
2022-11-30 15:59:41 +00:00
|
|
|
The following forms are currently recognized:
|
2021-09-30 18:02:22 +00:00
|
|
|
- an integer (returned without modification)
|
2021-11-20 14:40:26 +00:00
|
|
|
- \"o755\" (chmod style octal)
|
2021-09-30 18:02:22 +00:00
|
|
|
- \"rwxrw-r--\" (ls style specification)
|
|
|
|
- \"a=rw,u+x\" (chmod style) *
|
|
|
|
|
|
|
|
* The interpretation of these forms relies on `file-modes-symbolic-to-number',
|
2021-11-20 17:39:41 +00:00
|
|
|
and uses `org-babel-tangle-default-file-mode' as the base mode."
|
2021-09-30 18:02:22 +00:00
|
|
|
(cond
|
2021-11-20 14:43:44 +00:00
|
|
|
((integerp mode)
|
|
|
|
(if (string-match-p "^[0-7][0-7][0-7]$" (format "%o" mode))
|
|
|
|
mode
|
|
|
|
(user-error "%1$o is not a valid file mode octal. \
|
|
|
|
Did you give the decimal value %1$d by mistake?" mode)))
|
2021-09-30 18:02:22 +00:00
|
|
|
((not (stringp mode))
|
2022-11-30 15:59:41 +00:00
|
|
|
(error "File mode %S not recognized as a valid format." mode))
|
2021-11-20 14:40:26 +00:00
|
|
|
((string-match-p "^o0?[0-7][0-7][0-7]$" mode)
|
|
|
|
(string-to-number (replace-regexp-in-string "^o" "" mode) 8))
|
2021-09-30 18:02:22 +00:00
|
|
|
((string-match-p "^[ugoa]*\\(?:[+-=][rwxXstugo]*\\)+\\(,[ugoa]*\\(?:[+-=][rwxXstugo]*\\)+\\)*$" mode)
|
2021-11-20 14:47:44 +00:00
|
|
|
;; Match regexp taken from `file-modes-symbolic-to-number'.
|
2021-11-20 17:39:41 +00:00
|
|
|
(file-modes-symbolic-to-number mode org-babel-tangle-default-file-mode))
|
2021-11-29 18:45:32 +00:00
|
|
|
((string-match-p "^[r-][w-][xs-][r-][w-][xs-][r-][w-][x-]$" mode)
|
2023-04-13 05:34:58 +00:00
|
|
|
(file-modes-symbolic-to-number (concat "u=" (delete ?- (substring mode 0 3))
|
|
|
|
",g=" (delete ?- (substring mode 3 6))
|
|
|
|
",o=" (delete ?- (substring mode 6 9)))
|
2021-09-30 18:02:22 +00:00
|
|
|
0))
|
2022-11-30 15:59:41 +00:00
|
|
|
(t (error "File mode %S not recognized as a valid format. See `org-babel-interpret-file-mode'." mode))))
|
2021-09-30 18:02:22 +00:00
|
|
|
|
2009-08-20 22:32:57 +00:00
|
|
|
(defun org-babel-tangle-clean ()
|
2010-07-13 23:20:08 +00:00
|
|
|
"Remove comments inserted by `org-babel-tangle'.
|
|
|
|
Call this function inside of a source-code file generated by
|
2009-08-20 22:32:57 +00:00
|
|
|
`org-babel-tangle' to remove all comments inserted automatically
|
|
|
|
by `org-babel-tangle'. Warning, this comment removes any lines
|
2016-08-23 20:13:56 +00:00
|
|
|
containing constructs which resemble Org file links or noweb
|
2009-08-20 22:32:57 +00:00
|
|
|
references."
|
|
|
|
(interactive)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
|
2012-03-19 20:38:12 +00:00
|
|
|
(re-search-forward (org-babel-noweb-wrap) nil t))
|
2023-05-10 13:27:13 +00:00
|
|
|
(delete-region (save-excursion (forward-line) (point))
|
Xemacs incompatibilities
Carsten Dominik <carsten.dominik@gmail.com> writes:
> On May 17, 2010, at 4:39 PM, Michael Sperber wrote:
>
>> In particular, fixing the require won't be enough: org-babel-python.el
>> uses `run-python' and interacts with the inferior Python, whereas
>> python-mode.el defines `py-shell'.
>>
>> Should I try to abstract over the differences?
>
> Yes, this would be much appreciated. (I think, Eric or Dan?)
OK, I've attached a patch that makes `org-babel-python' work on XEmacs.
Most of the issues are pure XEmacs issues. Notes:
- XEmacs doesn't have [[:digit:]] - I hope to rectify this in the
future, but it seems there's no downside in this particular case to
replacing by [0-9].
- XEmacs doesn't have `move-end-of-line', but does have `end-of-line'.
I don't understand the intent of having both of these, but the code
seems fine with `end-of-line'.
- It seems there are way too few `require's throughout org-babel. I
don't know if it's OK to add the ones I needed.
- `org-babel-python-evaluate' looked broken as-is: It doesn't use the
`body' argument properly, the result is (I think) processed in the
wrong order and not properly split into lines. I've fixed all these,
but a review is probably in order.
2010-05-24 19:22:50 +00:00
|
|
|
(save-excursion (end-of-line 1) (forward-char 1) (point)))))
|
2009-08-20 22:32:57 +00:00
|
|
|
|
2012-08-10 13:40:00 +00:00
|
|
|
(defun org-babel-spec-to-string (spec)
|
|
|
|
"Insert SPEC into the current file.
|
|
|
|
|
2013-02-27 07:58:32 +00:00
|
|
|
Insert the source-code specified by SPEC into the current source
|
|
|
|
code file. This function uses `comment-region' which assumes
|
|
|
|
that the appropriate major-mode is set. SPEC has the form:
|
|
|
|
|
2015-09-17 23:08:20 +00:00
|
|
|
(start-line file link source-name params body comment)"
|
2016-12-09 23:20:11 +00:00
|
|
|
(pcase-let*
|
|
|
|
((`(,start ,file ,link ,source ,info ,body ,comment) spec)
|
|
|
|
(comments (cdr (assq :comments info)))
|
|
|
|
(link? (or (string= comments "both") (string= comments "link")
|
|
|
|
(string= comments "yes") (string= comments "noweb")))
|
|
|
|
(link-data `(("start-line" . ,(number-to-string start))
|
|
|
|
("file" . ,file)
|
|
|
|
("link" . ,link)
|
|
|
|
("source-name" . ,source)))
|
|
|
|
(insert-comment (lambda (text)
|
|
|
|
(when (and comments
|
|
|
|
(not (string= comments "no"))
|
|
|
|
(org-string-nw-p text))
|
|
|
|
(if org-babel-tangle-uncomment-comments
|
|
|
|
;; Plain comments: no processing.
|
|
|
|
(insert text)
|
|
|
|
;; Ensure comments are made to be
|
|
|
|
;; comments, and add a trailing newline.
|
|
|
|
;; Also ignore invisible characters when
|
|
|
|
;; commenting.
|
|
|
|
(comment-region
|
|
|
|
(point)
|
|
|
|
(progn (insert (org-no-properties text))
|
|
|
|
(point)))
|
|
|
|
(end-of-line)
|
|
|
|
(insert "\n"))))))
|
2012-08-10 13:40:00 +00:00
|
|
|
(when comment (funcall insert-comment comment))
|
2016-12-09 23:20:11 +00:00
|
|
|
(when link?
|
|
|
|
(funcall insert-comment
|
|
|
|
(org-fill-template
|
|
|
|
org-babel-tangle-comment-format-beg link-data)))
|
|
|
|
(insert body "\n")
|
|
|
|
(when link?
|
|
|
|
(funcall insert-comment
|
|
|
|
(org-fill-template
|
|
|
|
org-babel-tangle-comment-format-end link-data)))))
|
2012-08-10 13:40:00 +00:00
|
|
|
|
2021-05-05 15:45:09 +00:00
|
|
|
(defun org-babel-effective-tangled-filename (buffer-fn src-lang src-tfile)
|
2023-07-12 18:24:48 +00:00
|
|
|
"Return effective tangled absolute filename of a source-code block.
|
|
|
|
BUFFER-FN is the absolute file name of the buffer, SRC-LANG the
|
|
|
|
language of the block and SRC-TFILE is the value of the :tangle
|
|
|
|
header argument, as computed by `org-babel-tangle-single-block'."
|
|
|
|
(let* ((fnd (file-name-directory buffer-fn))
|
|
|
|
(base-name (cond
|
|
|
|
((string= "yes" src-tfile)
|
|
|
|
;; Use the buffer name
|
|
|
|
(file-name-sans-extension buffer-fn))
|
|
|
|
((string= "no" src-tfile) nil)
|
|
|
|
((> (length src-tfile) 0)
|
|
|
|
(expand-file-name src-tfile fnd))))
|
|
|
|
(ext (or (cdr (assoc src-lang org-babel-tangle-lang-exts)) src-lang)))
|
2021-05-05 15:45:09 +00:00
|
|
|
(when base-name
|
|
|
|
;; decide if we want to add ext to base-name
|
|
|
|
(if (and ext (string= "yes" src-tfile))
|
|
|
|
(concat base-name "." ext) base-name))))
|
|
|
|
|
2020-02-12 21:40:45 +00:00
|
|
|
(defun org-babel-tangle-collect-blocks (&optional lang-re tangle-file)
|
2015-06-15 19:38:14 +00:00
|
|
|
"Collect source blocks in the current Org file.
|
2021-05-01 19:18:44 +00:00
|
|
|
Return an association list of language and source-code block
|
|
|
|
specifications of the form used by `org-babel-spec-to-string'
|
2021-09-16 10:32:43 +00:00
|
|
|
grouped by tangled file name.
|
|
|
|
|
|
|
|
Optional argument LANG-RE can be used to limit the collected
|
|
|
|
source code blocks by languages matching a regular expression.
|
|
|
|
|
|
|
|
Optional argument TANGLE-FILE can be used to limit the collected
|
|
|
|
code blocks by target file."
|
2023-07-12 18:24:48 +00:00
|
|
|
(let ((counter 0)
|
|
|
|
(buffer-fn (buffer-file-name (buffer-base-buffer)))
|
|
|
|
last-heading-pos blocks)
|
2010-07-12 04:35:58 +00:00
|
|
|
(org-babel-map-src-blocks (buffer-file-name)
|
2015-06-15 19:38:14 +00:00
|
|
|
(let ((current-heading-pos
|
2023-05-07 20:47:19 +00:00
|
|
|
(or (org-element-begin
|
|
|
|
(org-element-lineage
|
|
|
|
(org-element-at-point)
|
|
|
|
'headline t))
|
|
|
|
1)))
|
2016-07-25 14:34:48 +00:00
|
|
|
(if (eq last-heading-pos current-heading-pos) (cl-incf counter)
|
2016-01-02 16:05:00 +00:00
|
|
|
(setq counter 1)
|
|
|
|
(setq last-heading-pos current-heading-pos)))
|
2020-09-07 04:49:12 +00:00
|
|
|
(unless (or (org-in-commented-heading-p)
|
|
|
|
(org-in-archived-heading-p))
|
2022-10-18 15:34:18 +00:00
|
|
|
(let* ((info (org-babel-get-src-block-info 'no-eval))
|
2015-06-15 19:38:14 +00:00
|
|
|
(src-lang (nth 0 info))
|
|
|
|
(src-tfile (cdr (assq :tangle (nth 2 info)))))
|
2016-01-02 16:05:00 +00:00
|
|
|
(unless (or (string= src-tfile "no")
|
2023-10-12 14:34:54 +00:00
|
|
|
(not src-lang) ;; src block without lang
|
2015-06-15 19:38:14 +00:00
|
|
|
(and tangle-file (not (equal tangle-file src-tfile)))
|
2020-02-12 21:40:45 +00:00
|
|
|
(and lang-re (not (string-match-p lang-re src-lang))))
|
2021-05-01 19:18:44 +00:00
|
|
|
;; Add the spec for this block to blocks under its tangled
|
|
|
|
;; file name.
|
|
|
|
(let* ((block (org-babel-tangle-single-block counter))
|
2021-05-05 06:31:43 +00:00
|
|
|
(src-tfile (cdr (assq :tangle (nth 4 block))))
|
2021-05-05 15:45:09 +00:00
|
|
|
(file-name (org-babel-effective-tangled-filename
|
2023-07-12 18:24:48 +00:00
|
|
|
buffer-fn src-lang src-tfile))
|
2021-05-01 19:18:44 +00:00
|
|
|
(by-fn (assoc file-name blocks)))
|
|
|
|
(if by-fn (setcdr by-fn (cons (cons src-lang block) (cdr by-fn)))
|
|
|
|
(push (cons file-name (list (cons src-lang block))) blocks)))))))
|
2015-06-15 19:38:14 +00:00
|
|
|
;; Ensure blocks are in the correct order.
|
2018-10-10 22:01:50 +00:00
|
|
|
(mapcar (lambda (b) (cons (car b) (nreverse (cdr b))))
|
|
|
|
(nreverse blocks))))
|
2009-06-28 00:25:01 +00:00
|
|
|
|
2022-08-08 20:58:05 +00:00
|
|
|
(defun org-babel-tangle--unbracketed-link (params)
|
|
|
|
"Get a raw link to the src block at point, without brackets.
|
|
|
|
|
|
|
|
The PARAMS are the 3rd element of the info for the same src block."
|
|
|
|
(unless (string= "no" (cdr (assq :comments params)))
|
|
|
|
(save-match-data
|
2023-08-08 12:54:56 +00:00
|
|
|
(let* ((l (org-no-properties
|
2022-08-08 20:58:05 +00:00
|
|
|
(cl-letf (((symbol-function 'org-store-link-functions)
|
|
|
|
(lambda () nil)))
|
|
|
|
(org-store-link nil))))
|
2022-12-20 02:25:02 +00:00
|
|
|
(bare (and l
|
|
|
|
(string-match org-link-bracket-re l)
|
2022-08-08 20:58:05 +00:00
|
|
|
(match-string 1 l))))
|
|
|
|
(when bare
|
|
|
|
(if (and org-babel-tangle-use-relative-file-links
|
|
|
|
(string-match org-link-types-re bare)
|
|
|
|
(string= (match-string 1 bare) "file"))
|
|
|
|
(concat "file:"
|
|
|
|
(file-relative-name (substring bare (match-end 0))
|
|
|
|
(file-name-directory
|
|
|
|
(cdr (assq :tangle params)))))
|
|
|
|
bare))))))
|
|
|
|
|
2023-06-24 12:34:22 +00:00
|
|
|
(defvar org-outline-regexp) ; defined in lisp/org.el
|
2016-12-09 23:20:11 +00:00
|
|
|
(defun org-babel-tangle-single-block (block-counter &optional only-this-block)
|
2013-03-02 16:15:08 +00:00
|
|
|
"Collect the tangled source for current block.
|
|
|
|
Return the list of block attributes needed by
|
2016-12-09 23:20:11 +00:00
|
|
|
`org-babel-tangle-collect-blocks'. When ONLY-THIS-BLOCK is
|
|
|
|
non-nil, return the full association list to be used by
|
|
|
|
`org-babel-tangle' directly."
|
2013-03-02 16:15:08 +00:00
|
|
|
(let* ((info (org-babel-get-src-block-info))
|
|
|
|
(start-line
|
|
|
|
(save-restriction (widen)
|
|
|
|
(+ 1 (line-number-at-pos (point)))))
|
2015-09-09 03:48:44 +00:00
|
|
|
(file (buffer-file-name (buffer-base-buffer)))
|
2013-03-02 16:15:08 +00:00
|
|
|
(src-lang (nth 0 info))
|
|
|
|
(params (nth 2 info))
|
|
|
|
(extra (nth 3 info))
|
2021-04-07 19:29:01 +00:00
|
|
|
(coderef (nth 6 info))
|
|
|
|
(cref-regexp (org-src-coderef-regexp coderef))
|
2022-08-08 20:58:05 +00:00
|
|
|
(link (org-babel-tangle--unbracketed-link params))
|
2013-03-02 16:15:08 +00:00
|
|
|
(source-name
|
2016-06-18 22:05:25 +00:00
|
|
|
(or (nth 4 info)
|
|
|
|
(format "%s:%d"
|
|
|
|
(or (ignore-errors (nth 4 (org-heading-components)))
|
|
|
|
"No heading")
|
|
|
|
block-counter)))
|
2016-12-09 23:20:11 +00:00
|
|
|
(expand-cmd (intern (concat "org-babel-expand-body:" src-lang)))
|
2013-03-02 16:15:08 +00:00
|
|
|
(assignments-cmd
|
|
|
|
(intern (concat "org-babel-variable-assignments:" src-lang)))
|
|
|
|
(body
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
;; Run the tangle-body-hook.
|
2016-12-09 23:59:08 +00:00
|
|
|
(let ((body (if (org-babel-noweb-p params :tangle)
|
2022-11-06 20:08:06 +00:00
|
|
|
(if (string= "strip-tangle" (cdr (assq :noweb (nth 2 info))))
|
|
|
|
(replace-regexp-in-string (org-babel-noweb-wrap) "" (nth 1 info))
|
|
|
|
(org-babel-expand-noweb-references info))
|
2016-12-09 23:59:08 +00:00
|
|
|
(nth 1 info))))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert
|
|
|
|
;; Expand body in language specific manner.
|
2016-12-09 23:20:11 +00:00
|
|
|
(cond ((assq :no-expand params) body)
|
|
|
|
((fboundp expand-cmd) (funcall expand-cmd body params))
|
|
|
|
(t
|
|
|
|
(org-babel-expand-body:generic
|
|
|
|
body params (and (fboundp assignments-cmd)
|
2016-12-09 23:59:08 +00:00
|
|
|
(funcall assignments-cmd params))))))
|
|
|
|
(when (string-match "-r" extra)
|
|
|
|
(goto-char (point-min))
|
2021-04-07 19:29:01 +00:00
|
|
|
(while (re-search-forward cref-regexp nil t)
|
2016-12-09 23:59:08 +00:00
|
|
|
(replace-match "")))
|
|
|
|
(run-hooks 'org-babel-tangle-body-hook)
|
|
|
|
(buffer-string))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(comment
|
2016-09-22 17:45:15 +00:00
|
|
|
(when (or (string= "both" (cdr (assq :comments params)))
|
|
|
|
(string= "org" (cdr (assq :comments params))))
|
2013-03-02 16:15:08 +00:00
|
|
|
;; From the previous heading or code-block end
|
|
|
|
(funcall
|
|
|
|
org-babel-process-comment-text
|
|
|
|
(buffer-substring
|
|
|
|
(max (condition-case nil
|
|
|
|
(save-excursion
|
2023-06-24 12:32:40 +00:00
|
|
|
(org-back-to-heading t)
|
2023-06-22 09:54:23 +00:00
|
|
|
(re-search-forward org-outline-regexp))
|
2013-03-02 16:15:08 +00:00
|
|
|
(error (point-min)))
|
|
|
|
(save-excursion
|
|
|
|
(if (re-search-backward
|
|
|
|
org-babel-src-block-regexp nil t)
|
|
|
|
(match-end 0)
|
|
|
|
(point-min))))
|
|
|
|
(point)))))
|
2021-06-28 19:42:29 +00:00
|
|
|
(src-tfile (cdr (assq :tangle params)))
|
2013-03-02 16:15:08 +00:00
|
|
|
(result
|
2016-12-09 23:20:11 +00:00
|
|
|
(list start-line
|
|
|
|
(if org-babel-tangle-use-relative-file-links
|
|
|
|
(file-relative-name file)
|
|
|
|
file)
|
2022-08-08 20:58:05 +00:00
|
|
|
link
|
2016-12-09 23:20:11 +00:00
|
|
|
source-name
|
|
|
|
params
|
2023-07-13 08:51:15 +00:00
|
|
|
(if (org-src-preserve-indentation-p) (org-trim body t)
|
2017-10-21 03:20:35 +00:00
|
|
|
(org-trim (org-remove-indentation body)))
|
2016-12-09 23:20:11 +00:00
|
|
|
comment)))
|
2013-03-02 16:15:08 +00:00
|
|
|
(if only-this-block
|
2021-06-28 19:42:29 +00:00
|
|
|
(let* ((file-name (org-babel-effective-tangled-filename
|
2023-07-12 18:24:48 +00:00
|
|
|
file src-lang src-tfile)))
|
2021-05-05 15:45:09 +00:00
|
|
|
(list (cons file-name (list (cons src-lang result)))))
|
2013-03-02 16:15:08 +00:00
|
|
|
result)))
|
|
|
|
|
2016-06-18 22:05:25 +00:00
|
|
|
(defun org-babel-tangle-comment-links (&optional info)
|
2019-08-24 20:44:18 +00:00
|
|
|
"Return a list of begin and end link comments for the code block at point.
|
|
|
|
INFO, when non nil, is the source block information, as returned
|
|
|
|
by `org-babel-get-src-block-info'."
|
2022-10-18 15:34:18 +00:00
|
|
|
(let ((link-data (pcase (or info (org-babel-get-src-block-info 'no-eval))
|
2022-08-08 20:58:05 +00:00
|
|
|
(`(,_ ,_ ,params ,_ ,name ,start ,_)
|
2019-08-24 20:44:18 +00:00
|
|
|
`(("start-line" . ,(org-with-point-at start
|
2019-08-24 20:58:29 +00:00
|
|
|
(number-to-string
|
|
|
|
(line-number-at-pos))))
|
2019-08-24 20:44:18 +00:00
|
|
|
("file" . ,(buffer-file-name))
|
2022-08-08 20:58:05 +00:00
|
|
|
("link" . ,(org-babel-tangle--unbracketed-link params))
|
2019-08-24 20:44:18 +00:00
|
|
|
("source-name" . ,name))))))
|
2011-01-16 15:21:16 +00:00
|
|
|
(list (org-fill-template org-babel-tangle-comment-format-beg link-data)
|
|
|
|
(org-fill-template org-babel-tangle-comment-format-end link-data))))
|
|
|
|
|
|
|
|
;; de-tangling functions
|
2010-10-29 07:49:47 +00:00
|
|
|
(defun org-babel-detangle (&optional source-code-file)
|
2024-01-20 12:17:43 +00:00
|
|
|
"Propagate changes from current source buffer back to the original Org file.
|
2010-10-29 07:49:47 +00:00
|
|
|
This requires that code blocks were tangled with link comments
|
2024-01-20 12:17:43 +00:00
|
|
|
which enable the original code blocks to be found.
|
|
|
|
|
|
|
|
Optional argument SOURCE-CODE-FILE is the file path to be used instead
|
|
|
|
of the current buffer."
|
2010-10-29 07:49:47 +00:00
|
|
|
(interactive)
|
|
|
|
(save-excursion
|
|
|
|
(when source-code-file (find-file source-code-file))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(let ((counter 0) new-body end)
|
2019-03-09 00:26:26 +00:00
|
|
|
(while (re-search-forward org-link-bracket-re nil t)
|
2020-01-28 22:51:29 +00:00
|
|
|
(if (and (match-string 2)
|
|
|
|
(re-search-forward
|
|
|
|
(concat " " (regexp-quote (match-string 2)) " ends here") nil t))
|
|
|
|
(progn (setq end (match-end 0))
|
|
|
|
(forward-line -1)
|
|
|
|
(save-excursion
|
|
|
|
(when (setq new-body (org-babel-tangle-jump-to-org))
|
|
|
|
(org-babel-update-block-body new-body)))
|
|
|
|
(setq counter (+ 1 counter)))
|
|
|
|
(setq end (point)))
|
2010-10-29 07:49:47 +00:00
|
|
|
(goto-char end))
|
2012-08-13 03:59:44 +00:00
|
|
|
(prog1 counter (message "Detangled %d code blocks" counter)))))
|
2010-10-29 07:49:47 +00:00
|
|
|
|
|
|
|
(defun org-babel-tangle-jump-to-org ()
|
2016-01-31 10:37:05 +00:00
|
|
|
"Jump from a tangled code file to the related Org mode file."
|
2010-10-29 07:49:47 +00:00
|
|
|
(interactive)
|
|
|
|
(let ((mid (point))
|
2019-03-09 00:26:26 +00:00
|
|
|
start body-start end target-buffer target-char link block-name body)
|
2010-10-29 07:49:47 +00:00
|
|
|
(save-window-excursion
|
|
|
|
(save-excursion
|
2019-03-09 00:26:26 +00:00
|
|
|
(while (and (re-search-backward org-link-bracket-re nil t)
|
2011-01-13 21:04:28 +00:00
|
|
|
(not ; ever wider searches until matching block comments
|
2016-01-31 10:37:05 +00:00
|
|
|
(and (setq start (line-beginning-position))
|
|
|
|
(setq body-start (line-beginning-position 2))
|
2011-01-13 21:04:28 +00:00
|
|
|
(setq link (match-string 0))
|
2019-03-09 09:58:41 +00:00
|
|
|
(setq block-name (match-string 2))
|
2011-01-13 21:04:28 +00:00
|
|
|
(save-excursion
|
|
|
|
(save-match-data
|
|
|
|
(re-search-forward
|
|
|
|
(concat " " (regexp-quote block-name)
|
2020-02-18 21:57:37 +00:00
|
|
|
" ends here")
|
|
|
|
nil t)
|
2016-01-31 10:37:05 +00:00
|
|
|
(setq end (line-beginning-position))))))))
|
2011-01-13 21:04:28 +00:00
|
|
|
(unless (and start (< start mid) (< mid end))
|
2012-08-13 03:59:44 +00:00
|
|
|
(error "Not in tangled code"))
|
2016-01-31 10:37:05 +00:00
|
|
|
(setq body (buffer-substring body-start end)))
|
|
|
|
;; Go to the beginning of the relative block in Org file.
|
2022-07-23 02:10:24 +00:00
|
|
|
;; Explicitly allow fuzzy search even if user customized
|
|
|
|
;; otherwise.
|
|
|
|
(let (org-link-search-must-match-exact-headline)
|
|
|
|
(org-link-open-from-string link))
|
2019-03-09 00:26:26 +00:00
|
|
|
(setq target-buffer (current-buffer))
|
2010-10-29 07:49:47 +00:00
|
|
|
(if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
|
2016-01-31 10:37:05 +00:00
|
|
|
(let ((n (string-to-number (match-string 1 block-name))))
|
|
|
|
(if (org-before-first-heading-p) (goto-char (point-min))
|
|
|
|
(org-back-to-heading t))
|
|
|
|
;; Do not skip the first block if it begins at point min.
|
|
|
|
(cond ((or (org-at-heading-p)
|
2023-05-16 10:41:53 +00:00
|
|
|
(not (org-element-type-p (org-element-at-point) 'src-block)))
|
2016-01-31 10:37:05 +00:00
|
|
|
(org-babel-next-src-block n))
|
|
|
|
((= n 1))
|
|
|
|
(t (org-babel-next-src-block (1- n)))))
|
2010-10-29 07:49:47 +00:00
|
|
|
(org-babel-goto-named-src-block block-name))
|
2013-05-30 22:03:21 +00:00
|
|
|
(goto-char (org-babel-where-is-src-block-head))
|
|
|
|
(forward-line 1)
|
2018-06-05 09:55:55 +00:00
|
|
|
;; Try to preserve location of point within the source code in
|
|
|
|
;; tangled code file.
|
|
|
|
(let ((offset (- mid body-start)))
|
|
|
|
(when (> end (+ offset (point)))
|
|
|
|
(forward-char offset)))
|
2010-10-29 07:49:47 +00:00
|
|
|
(setq target-char (point)))
|
2013-06-06 15:11:54 +00:00
|
|
|
(org-src-switch-to-buffer target-buffer t)
|
2016-06-18 22:05:25 +00:00
|
|
|
(goto-char target-char)
|
|
|
|
body))
|
2010-10-29 07:49:47 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-tangle)
|
2010-06-25 16:20:39 +00:00
|
|
|
|
2012-10-02 06:50:46 +00:00
|
|
|
;; Local variables:
|
|
|
|
;; generated-autoload-file: "org-loaddefs.el"
|
|
|
|
;; End:
|
2010-06-25 16:20:39 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-tangle.el ends here
|