2010-06-25 16:20:39 +00:00
|
|
|
;;; ob-tangle.el --- extract source code from org-mode files
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2014-01-07 13:18:17 +00:00
|
|
|
;; Copyright (C) 2009-2014 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
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
|
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
|
2010-06-25 16:20:39 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://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:
|
2010-06-23 18:24:33 +00:00
|
|
|
(require 'org-src)
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2013-06-01 06:23:58 +00:00
|
|
|
(declare-function org-edit-special "org" (&optional arg))
|
2010-06-23 18:24:33 +00:00
|
|
|
(declare-function org-link-escape "org" (text &optional table))
|
2013-06-01 09:21:48 +00:00
|
|
|
(declare-function org-store-link "org" (arg))
|
|
|
|
(declare-function org-open-link-from-string "org" (s &optional arg reference-buffer))
|
2010-07-12 23:48:05 +00:00
|
|
|
(declare-function org-heading-components "org" ())
|
2010-09-04 14:39:19 +00:00
|
|
|
(declare-function org-back-to-heading "org" (invisible-ok))
|
2010-09-05 21:12:43 +00:00
|
|
|
(declare-function org-fill-template "org" (template alist))
|
2010-10-29 07:49:47 +00:00
|
|
|
(declare-function org-babel-update-block-body "org" (new-body))
|
2013-10-27 08:54:27 +00:00
|
|
|
(declare-function org-up-heading-safe "org" ())
|
2011-01-20 03:29:09 +00:00
|
|
|
(declare-function make-directory "files" (dir &optional parents))
|
2014-01-31 08:43:46 +00:00
|
|
|
(declare-function org-before-first-heading-p "org" ())
|
2010-06-23 18:24:33 +00:00
|
|
|
|
2010-06-17 16:01:24 +00:00
|
|
|
(defcustom org-babel-tangle-lang-exts
|
|
|
|
'(("emacs-lisp" . "el"))
|
|
|
|
"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
|
|
|
|
"Use relative path names in links from tangled source back the Org-mode file."
|
|
|
|
: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'."
|
|
|
|
:group 'org-babel
|
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)
|
|
|
|
"Hook run at the beginning of `org-babel-tangle'."
|
|
|
|
:group 'org-babel
|
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."
|
|
|
|
:group 'org-babel
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2011-03-14 15:31:33 +00:00
|
|
|
: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
|
|
|
|
%link --------- Org-mode style link to the code block
|
2010-10-17 17:18:20 +00:00
|
|
|
%source-name -- name of the code block
|
|
|
|
|
|
|
|
Whether or not comments are inserted during tangling is
|
|
|
|
controlled by the :comments header argument."
|
2010-09-05 21:12:43 +00:00
|
|
|
:group 'org-babel
|
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
|
|
|
|
%link --------- Org-mode style link to the code block
|
2010-10-17 17:18:20 +00:00
|
|
|
%source-name -- name of the code block
|
|
|
|
|
|
|
|
Whether or not comments are inserted during tangling is
|
|
|
|
controlled by the :comments header argument."
|
2010-09-05 21:12:43 +00:00
|
|
|
:group 'org-babel
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2010-09-05 21:12:43 +00:00
|
|
|
:type 'string)
|
|
|
|
|
2013-11-23 22:57:29 +00:00
|
|
|
(defcustom org-babel-process-comment-text #'org-remove-indentation
|
2011-09-15 22:00:10 +00:00
|
|
|
"Function called to process raw Org-mode text collected to be
|
|
|
|
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'."
|
2011-09-15 22:00:10 +00:00
|
|
|
:group 'org-babel
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2011-09-15 22:00:10 +00:00
|
|
|
:type 'function)
|
|
|
|
|
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."
|
|
|
|
(declare (indent 1))
|
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)))
|
Provide edebug specifications for macros
* org-macs.el (org-with-gensyms, org-called-interactively-p)
(with-silent-modifications, org-bound-and-true-p)
(org-unmodified, org-re, org-preserve-lc)
(org-without-partial-completion, org-with-point-at)
(org-no-warnings, org-if-unprotected, org-if-unprotected-1)
(org-if-unprotected-at, org-with-remote-undo)
(org-no-read-only, org-save-outline-visibility)
(org-with-wide-buffer, org-with-limited-levels)
(org-eval-in-environment): Provide edebug specifications.
* org-src.el (org-src-do-at-code-block): Dto.
* org-publish.el (org-publish-with-aux-preprocess-maybe): Dto.
* org-compat.el (org-xemacs-without-invisibility): Dto.
* org-clock.el (org-with-clock-position, org-with-clock): Dto.
* org-agenda.el (org-agenda-with-point-at-orig-entry)
(org-batch-agenda, org-batch-agenda-csv)
(org-batch-store-agenda-views): Dto.
* ob.el (org-babel-do-in-edit-buffer)
(org-babel-map-src-blocks, org-babel-map-inline-src-blocks): Dto.
* ob-tangle.el (org-babel-with-temp-filebuffer): Dto.
* ob-table.el (sbe): Dto.
* ob-exp.el (org-babel-exp-in-export-file): Dto.
* ob-comint.el (org-babel-comint-in-buffer)
(org-babel-comint-with-output): Dto.
2011-08-11 06:57:25 +00:00
|
|
|
(def-edebug-spec org-babel-with-temp-filebuffer (form body))
|
2010-07-16 22:55:40 +00:00
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2009-07-30 14:57:26 +00:00
|
|
|
(defun org-babel-tangle-file (file &optional target-file lang)
|
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'.
|
|
|
|
Optional argument TARGET-FILE can be used to specify a default
|
|
|
|
export file for all source blocks. Optional argument LANG can be
|
2013-11-04 11:19:45 +00:00
|
|
|
used to limit the exported source code blocks by language.
|
|
|
|
Return a list whose CAR is the tangled file name."
|
2009-10-20 14:17:42 +00:00
|
|
|
(interactive "fFile to tangle: \nP")
|
2010-06-02 13:18:36 +00:00
|
|
|
(let ((visited-p (get-file-buffer (expand-file-name file)))
|
|
|
|
to-be-removed)
|
2013-11-04 11:19:45 +00:00
|
|
|
(prog1
|
|
|
|
(save-window-excursion
|
|
|
|
(find-file file)
|
|
|
|
(setq to-be-removed (current-buffer))
|
|
|
|
(org-babel-tangle nil target-file lang))
|
|
|
|
(unless visited-p
|
|
|
|
(kill-buffer to-be-removed)))))
|
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."
|
|
|
|
(mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
|
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2013-03-02 17:10:39 +00:00
|
|
|
(defun org-babel-tangle (&optional arg target-file lang)
|
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
|
2013-03-02 14:27:25 +00:00
|
|
|
file into their own source-specific files.
|
|
|
|
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
|
|
|
|
export file for all source blocks. Optional argument LANG can be
|
|
|
|
used to limit the exported source code blocks by language."
|
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))
|
2013-03-31 22:17:16 +00:00
|
|
|
(or (cdr (assoc :tangle (nth 2 (org-babel-get-src-block-info 'light))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(user-error "Point is not in a source code block"))))
|
2012-08-10 13:40:00 +00:00
|
|
|
path-collector)
|
|
|
|
(mapc ;; map over all languages
|
|
|
|
(lambda (by-lang)
|
|
|
|
(let* ((lang (car by-lang))
|
|
|
|
(specs (cdr by-lang))
|
|
|
|
(ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))
|
|
|
|
(lang-f (intern
|
|
|
|
(concat
|
|
|
|
(or (and (cdr (assoc lang org-src-lang-modes))
|
|
|
|
(symbol-name
|
|
|
|
(cdr (assoc lang org-src-lang-modes))))
|
|
|
|
lang)
|
|
|
|
"-mode")))
|
|
|
|
she-banged)
|
|
|
|
(mapc
|
|
|
|
(lambda (spec)
|
|
|
|
(let ((get-spec (lambda (name) (cdr (assoc name (nth 4 spec))))))
|
|
|
|
(let* ((tangle (funcall get-spec :tangle))
|
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
|
|
|
(she-bang (let ((sheb (funcall get-spec :shebang)))
|
|
|
|
(when (> (length sheb) 0) sheb)))
|
2013-06-08 18:29:02 +00:00
|
|
|
(tangle-mode (funcall get-spec :tangle-mode))
|
2012-08-10 13:40:00 +00:00
|
|
|
(base-name (cond
|
|
|
|
((string= "yes" tangle)
|
|
|
|
(file-name-sans-extension
|
|
|
|
(buffer-file-name)))
|
|
|
|
((string= "no" tangle) nil)
|
|
|
|
((> (length tangle) 0) tangle)))
|
|
|
|
(file-name (when base-name
|
|
|
|
;; decide if we want to add ext to base-name
|
|
|
|
(if (and ext (string= "yes" tangle))
|
|
|
|
(concat base-name "." ext) base-name))))
|
|
|
|
(when file-name
|
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
|
|
|
;; Possibly create the parent directories for file.
|
|
|
|
(when (let ((m (funcall get-spec :mkdirp)))
|
|
|
|
(and m (not (string= m "no"))))
|
2012-08-10 13:40:00 +00:00
|
|
|
(make-directory (file-name-directory file-name) 'parents))
|
|
|
|
;; delete any old versions of file
|
|
|
|
(when (and (file-exists-p file-name)
|
2013-06-11 13:09:23 +00:00
|
|
|
(not (member file-name (mapcar #'car path-collector))))
|
2012-08-10 13:40:00 +00:00
|
|
|
(delete-file file-name))
|
|
|
|
;; drop source-block to file
|
|
|
|
(with-temp-buffer
|
|
|
|
(when (fboundp lang-f) (ignore-errors (funcall lang-f)))
|
|
|
|
(when (and she-bang (not (member file-name she-banged)))
|
|
|
|
(insert (concat she-bang "\n"))
|
|
|
|
(setq she-banged (cons file-name she-banged)))
|
|
|
|
(org-babel-spec-to-string spec)
|
|
|
|
;; We avoid append-to-file as it does not work with tramp.
|
|
|
|
(let ((content (buffer-string)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(if (file-exists-p file-name)
|
|
|
|
(insert-file-contents file-name))
|
|
|
|
(goto-char (point-max))
|
2013-09-28 12:15:42 +00:00
|
|
|
;; Handle :padlines unless first line in file
|
|
|
|
(unless (or (string= "no" (cdr (assoc :padline (nth 4 spec))))
|
|
|
|
(= (point) (point-min)))
|
|
|
|
(insert "\n"))
|
2012-08-10 13:40:00 +00:00
|
|
|
(insert content)
|
|
|
|
(write-region nil nil file-name))))
|
2013-06-10 15:55:28 +00:00
|
|
|
;; if files contain she-bangs, then make the executable
|
|
|
|
(when she-bang
|
|
|
|
(unless tangle-mode (setq tangle-mode #o755)))
|
2012-08-10 13:40:00 +00:00
|
|
|
;; update counter
|
|
|
|
(setq block-counter (+ 1 block-counter))
|
2013-06-10 15:55:28 +00:00
|
|
|
(add-to-list 'path-collector
|
|
|
|
(cons file-name tangle-mode)
|
|
|
|
nil
|
|
|
|
(lambda (a b) (equal (car a) (car b))))))))
|
2012-08-10 13:40:00 +00:00
|
|
|
specs)))
|
2013-03-02 16:15:08 +00:00
|
|
|
(if (equal arg '(4))
|
|
|
|
(org-babel-tangle-single-block 1 t)
|
|
|
|
(org-babel-tangle-collect-blocks lang 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
|
|
|
|
(or (buffer-base-buffer) (current-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)))
|
2013-06-10 15:55:28 +00:00
|
|
|
(mapcar #'car path-collector)))
|
|
|
|
;; set permissions on tangled files
|
|
|
|
(mapc (lambda (pair)
|
|
|
|
(when (cdr pair) (set-file-modes (car pair) (cdr pair))))
|
|
|
|
path-collector)
|
|
|
|
(mapcar #'car path-collector)))))
|
2009-06-26 18:43:50 +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
|
|
|
|
containing constructs which resemble org-mode file links or noweb
|
|
|
|
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))
|
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
|
|
|
(delete-region (save-excursion (beginning-of-line 1) (point))
|
|
|
|
(save-excursion (end-of-line 1) (forward-char 1) (point)))))
|
2009-08-20 22:32:57 +00:00
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
(defvar org-stored-links)
|
2011-06-14 18:23:56 +00:00
|
|
|
(defvar org-bracket-link-regexp)
|
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:
|
|
|
|
|
|
|
|
\(start-line file link source-name params body comment)"
|
2012-08-10 13:40:00 +00:00
|
|
|
(let* ((start-line (nth 0 spec))
|
2013-10-09 16:00:28 +00:00
|
|
|
(file (if org-babel-tangle-use-relative-file-links
|
|
|
|
(file-relative-name (nth 1 spec))
|
|
|
|
(nth 1 spec)))
|
|
|
|
(link (let ((link (nth 2 spec)))
|
|
|
|
(if org-babel-tangle-use-relative-file-links
|
|
|
|
(when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
|
|
|
|
(let* ((type (match-string 1 link))
|
|
|
|
(path (match-string 2 link))
|
|
|
|
(origpath path)
|
|
|
|
(case-fold-search nil))
|
|
|
|
(setq path (file-relative-name path))
|
|
|
|
(concat type path)))
|
|
|
|
link)))
|
2012-08-10 13:40:00 +00:00
|
|
|
(source-name (nth 3 spec))
|
|
|
|
(body (nth 5 spec))
|
|
|
|
(comment (nth 6 spec))
|
|
|
|
(comments (cdr (assoc :comments (nth 4 spec))))
|
|
|
|
(link-p (or (string= comments "both") (string= comments "link")
|
|
|
|
(string= comments "yes") (string= comments "noweb")))
|
|
|
|
(link-data (mapcar (lambda (el)
|
|
|
|
(cons (symbol-name el)
|
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
|
|
|
(let ((le (eval el)))
|
|
|
|
(if (stringp le) le (format "%S" le)))))
|
2012-08-10 13:40:00 +00:00
|
|
|
'(start-line file link source-name)))
|
|
|
|
(insert-comment (lambda (text)
|
|
|
|
(when (and comments (not (string= comments "no"))
|
|
|
|
(> (length text) 0))
|
|
|
|
(comment-region (point) (progn (insert text) (point)))
|
|
|
|
(end-of-line nil) (insert "\n")))))
|
|
|
|
(when comment (funcall insert-comment comment))
|
|
|
|
(when link-p
|
|
|
|
(funcall
|
|
|
|
insert-comment
|
|
|
|
(org-fill-template org-babel-tangle-comment-format-beg link-data)))
|
|
|
|
(insert
|
|
|
|
(format
|
|
|
|
"%s\n"
|
2013-02-27 07:58:32 +00:00
|
|
|
(org-unescape-code-in-string
|
2012-08-10 13:40:00 +00:00
|
|
|
(org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")))))
|
|
|
|
(when link-p
|
|
|
|
(funcall
|
|
|
|
insert-comment
|
|
|
|
(org-fill-template org-babel-tangle-comment-format-end link-data)))))
|
|
|
|
|
2013-10-27 08:54:27 +00:00
|
|
|
(defvar org-comment-string) ;; Defined in org.el
|
2013-10-19 21:52:31 +00:00
|
|
|
(defun org-babel-under-commented-heading-p ()
|
|
|
|
"Return t if currently under a commented heading."
|
2014-01-29 23:57:24 +00:00
|
|
|
(unless (org-before-first-heading-p)
|
|
|
|
(if (let ((hd (nth 4 (org-heading-components))))
|
|
|
|
(and hd (string-match (concat "^" org-comment-string) hd)))
|
|
|
|
t
|
|
|
|
(save-excursion
|
|
|
|
(and (org-up-heading-safe)
|
|
|
|
(org-babel-under-commented-heading-p))))))
|
2013-10-19 21:52:31 +00:00
|
|
|
|
2013-03-04 12:52:04 +00:00
|
|
|
(defun org-babel-tangle-collect-blocks (&optional language tangle-file)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Collect source blocks in the current Org-mode file.
|
2009-07-30 14:57:26 +00:00
|
|
|
Return an association list of source-code block specifications of
|
|
|
|
the form used by `org-babel-spec-to-string' grouped by language.
|
2013-03-04 13:44:31 +00:00
|
|
|
Optional argument LANGUAGE can be used to limit the collected
|
|
|
|
source code blocks by language. Optional argument TANGLE-FILE
|
|
|
|
can be used to limit the collected code blocks by target file."
|
2013-03-02 16:15:08 +00:00
|
|
|
(let ((block-counter 1) (current-heading "") blocks by-lang)
|
2010-07-12 04:35:58 +00:00
|
|
|
(org-babel-map-src-blocks (buffer-file-name)
|
2013-10-09 16:28:33 +00:00
|
|
|
((lambda (new-heading)
|
|
|
|
(if (not (string= new-heading current-heading))
|
|
|
|
(progn
|
|
|
|
(setq block-counter 1)
|
|
|
|
(setq current-heading new-heading))
|
|
|
|
(setq block-counter (+ 1 block-counter))))
|
|
|
|
(replace-regexp-in-string "[ \t]" "-"
|
|
|
|
(condition-case nil
|
|
|
|
(or (nth 4 (org-heading-components))
|
|
|
|
"(dummy for heading without text)")
|
|
|
|
(error (buffer-file-name)))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(let* ((info (org-babel-get-src-block-info 'light))
|
2013-03-02 14:27:25 +00:00
|
|
|
(src-lang (nth 0 info))
|
|
|
|
(src-tfile (cdr (assoc :tangle (nth 2 info)))))
|
2013-10-19 21:52:31 +00:00
|
|
|
(unless (or (org-babel-under-commented-heading-p)
|
2013-03-02 14:27:25 +00:00
|
|
|
(string= (cdr (assoc :tangle (nth 2 info))) "no")
|
|
|
|
(and tangle-file (not (equal tangle-file src-tfile))))
|
2013-03-04 12:52:04 +00:00
|
|
|
(unless (and language (not (string= language src-lang)))
|
2013-03-02 16:15:08 +00:00
|
|
|
;; Add the spec for this block to blocks under it's language
|
|
|
|
(setq by-lang (cdr (assoc src-lang blocks)))
|
|
|
|
(setq blocks (delq (assoc src-lang blocks) blocks))
|
|
|
|
(setq blocks (cons
|
|
|
|
(cons src-lang
|
|
|
|
(cons
|
|
|
|
(org-babel-tangle-single-block
|
|
|
|
block-counter)
|
|
|
|
by-lang)) blocks))))))
|
2013-03-02 14:27:25 +00:00
|
|
|
;; Ensure blocks are in the correct order
|
2009-08-03 17:00:42 +00:00
|
|
|
(setq blocks
|
2010-06-02 12:52:30 +00:00
|
|
|
(mapcar
|
|
|
|
(lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang))))
|
|
|
|
blocks))
|
2009-06-28 00:25:01 +00:00
|
|
|
blocks))
|
|
|
|
|
2013-03-02 16:15:08 +00:00
|
|
|
(defun org-babel-tangle-single-block
|
|
|
|
(block-counter &optional only-this-block)
|
|
|
|
"Collect the tangled source for current block.
|
|
|
|
Return the list of block attributes needed by
|
|
|
|
`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."
|
|
|
|
(let* ((info (org-babel-get-src-block-info))
|
|
|
|
(start-line
|
|
|
|
(save-restriction (widen)
|
|
|
|
(+ 1 (line-number-at-pos (point)))))
|
|
|
|
(file (buffer-file-name))
|
|
|
|
(src-lang (nth 0 info))
|
|
|
|
(params (nth 2 info))
|
|
|
|
(extra (nth 3 info))
|
|
|
|
(cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra)
|
|
|
|
(match-string 1 extra))
|
|
|
|
org-coderef-label-format))
|
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
|
|
|
(link (let ((link (org-no-properties
|
|
|
|
(org-store-link nil))))
|
|
|
|
(and (string-match org-bracket-link-regexp link)
|
|
|
|
(match-string 1 link))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(source-name
|
|
|
|
(intern (or (nth 4 info)
|
|
|
|
(format "%s:%d"
|
|
|
|
(or (ignore-errors (nth 4 (org-heading-components)))
|
|
|
|
"No heading")
|
|
|
|
block-counter))))
|
|
|
|
(expand-cmd
|
|
|
|
(intern (concat "org-babel-expand-body:" src-lang)))
|
|
|
|
(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.
|
|
|
|
(let* ((body ;; Expand the body in language specific manner.
|
|
|
|
(if (org-babel-noweb-p params :tangle)
|
|
|
|
(org-babel-expand-noweb-references info)
|
|
|
|
(nth 1 info)))
|
|
|
|
(body
|
|
|
|
(if (assoc :no-expand params)
|
|
|
|
body
|
|
|
|
(if (fboundp expand-cmd)
|
|
|
|
(funcall expand-cmd body params)
|
|
|
|
(org-babel-expand-body:generic
|
|
|
|
body params
|
|
|
|
(and (fboundp assignments-cmd)
|
|
|
|
(funcall assignments-cmd params)))))))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert body)
|
|
|
|
(when (string-match "-r" extra)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward
|
|
|
|
(replace-regexp-in-string "%s" ".+" cref-fmt) nil t)
|
|
|
|
(replace-match "")))
|
|
|
|
(run-hooks 'org-babel-tangle-body-hook)
|
|
|
|
(buffer-string))))
|
2013-03-02 16:15:08 +00:00
|
|
|
(comment
|
|
|
|
(when (or (string= "both" (cdr (assoc :comments params)))
|
|
|
|
(string= "org" (cdr (assoc :comments params))))
|
|
|
|
;; From the previous heading or code-block end
|
|
|
|
(funcall
|
|
|
|
org-babel-process-comment-text
|
|
|
|
(buffer-substring
|
|
|
|
(max (condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(org-back-to-heading t) ; Sets match data
|
|
|
|
(match-end 0))
|
|
|
|
(error (point-min)))
|
|
|
|
(save-excursion
|
|
|
|
(if (re-search-backward
|
|
|
|
org-babel-src-block-regexp nil t)
|
|
|
|
(match-end 0)
|
|
|
|
(point-min))))
|
|
|
|
(point)))))
|
|
|
|
(result
|
|
|
|
(list start-line file link source-name params body comment)))
|
|
|
|
(if only-this-block
|
|
|
|
(list (cons src-lang (list result)))
|
|
|
|
result)))
|
|
|
|
|
2011-01-16 15:21:16 +00:00
|
|
|
(defun org-babel-tangle-comment-links ( &optional info)
|
|
|
|
"Return a list of begin and end link comments for the code block at point."
|
|
|
|
(let* ((start-line (org-babel-where-is-src-block-head))
|
|
|
|
(file (buffer-file-name))
|
|
|
|
(link (org-link-escape (progn (call-interactively 'org-store-link)
|
2012-08-15 07:39:24 +00:00
|
|
|
(org-no-properties
|
2011-01-16 15:21:16 +00:00
|
|
|
(car (pop org-stored-links))))))
|
|
|
|
(source-name (nth 4 (or info (org-babel-get-src-block-info 'light))))
|
|
|
|
(link-data (mapcar (lambda (el)
|
|
|
|
(cons (symbol-name el)
|
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
|
|
|
(let ((le (eval el)))
|
|
|
|
(if (stringp le) le (format "%S" le)))))
|
2011-01-16 15:21:16 +00:00
|
|
|
'(start-line file link source-name))))
|
|
|
|
(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
|
|
|
(defvar org-bracket-link-analytic-regexp)
|
|
|
|
(defun org-babel-detangle (&optional source-code-file)
|
|
|
|
"Propagate changes in source file back original to Org-mode file.
|
|
|
|
This requires that code blocks were tangled with link comments
|
|
|
|
which enable the original code blocks to be found."
|
|
|
|
(interactive)
|
|
|
|
(save-excursion
|
|
|
|
(when source-code-file (find-file source-code-file))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(let ((counter 0) new-body end)
|
|
|
|
(while (re-search-forward org-bracket-link-analytic-regexp nil t)
|
|
|
|
(when (re-search-forward
|
|
|
|
(concat " " (regexp-quote (match-string 5)) " ends here"))
|
|
|
|
(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)))
|
|
|
|
(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 ()
|
|
|
|
"Jump from a tangled code file to the related Org-mode file."
|
|
|
|
(interactive)
|
|
|
|
(let ((mid (point))
|
2013-05-30 22:03:21 +00:00
|
|
|
start body-start end done
|
2011-01-13 21:04:28 +00:00
|
|
|
target-buffer target-char link path block-name body)
|
2010-10-29 07:49:47 +00:00
|
|
|
(save-window-excursion
|
|
|
|
(save-excursion
|
2011-01-13 21:04:28 +00:00
|
|
|
(while (and (re-search-backward org-bracket-link-analytic-regexp nil t)
|
|
|
|
(not ; ever wider searches until matching block comments
|
|
|
|
(and (setq start (point-at-eol))
|
2013-05-30 22:03:21 +00:00
|
|
|
(setq body-start (save-excursion
|
|
|
|
(forward-line 2) (point-at-bol)))
|
2011-01-13 21:04:28 +00:00
|
|
|
(setq link (match-string 0))
|
|
|
|
(setq path (match-string 3))
|
|
|
|
(setq block-name (match-string 5))
|
|
|
|
(save-excursion
|
|
|
|
(save-match-data
|
|
|
|
(re-search-forward
|
|
|
|
(concat " " (regexp-quote block-name)
|
|
|
|
" ends here") nil t)
|
|
|
|
(setq end (point-at-bol))))))))
|
|
|
|
(unless (and start (< start mid) (< mid end))
|
2012-08-13 03:59:44 +00:00
|
|
|
(error "Not in tangled code"))
|
2010-10-29 07:49:47 +00:00
|
|
|
(setq body (org-babel-trim (buffer-substring start end))))
|
|
|
|
(when (string-match "::" path)
|
|
|
|
(setq path (substring path 0 (match-beginning 0))))
|
|
|
|
(find-file path) (setq target-buffer (current-buffer))
|
|
|
|
(goto-char start) (org-open-link-from-string link)
|
|
|
|
(if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
|
|
|
|
(org-babel-next-src-block
|
|
|
|
(string-to-number (match-string 1 block-name)))
|
|
|
|
(org-babel-goto-named-src-block block-name))
|
2013-05-30 22:03:21 +00:00
|
|
|
;; position at the beginning of the code block body
|
|
|
|
(goto-char (org-babel-where-is-src-block-head))
|
|
|
|
(forward-line 1)
|
|
|
|
;; Use org-edit-special to isolate the code.
|
|
|
|
(org-edit-special)
|
|
|
|
;; Then move forward the correct number of characters in the
|
|
|
|
;; code buffer.
|
|
|
|
(forward-char (- mid body-start))
|
|
|
|
;; And return to the Org-mode buffer with the point in the right
|
|
|
|
;; place.
|
|
|
|
(org-edit-src-exit)
|
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)
|
2010-10-29 07:49:47 +00:00
|
|
|
(prog1 body (goto-char target-char))))
|
|
|
|
|
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
|