ob-tangle.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. ;;; ob-tangle.el --- extract source code from org-mode files
  2. ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Extract the code from source blocks out into raw source-code files.
  19. ;;; Code:
  20. (require 'org-src)
  21. (declare-function org-edit-special "org" (&optional arg))
  22. (declare-function org-link-escape "org" (text &optional table))
  23. (declare-function org-store-link "org" (arg))
  24. (declare-function org-open-link-from-string "org" (s &optional arg reference-buffer))
  25. (declare-function org-heading-components "org" ())
  26. (declare-function org-back-to-heading "org" (invisible-ok))
  27. (declare-function org-fill-template "org" (template alist))
  28. (declare-function org-babel-update-block-body "org" (new-body))
  29. (declare-function org-up-heading-safe "org" ())
  30. (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
  31. (declare-function make-directory "files" (dir &optional parents))
  32. (declare-function org-before-first-heading-p "org" ())
  33. (defcustom org-babel-tangle-lang-exts
  34. '(("emacs-lisp" . "el")
  35. ("elisp" . "el"))
  36. "Alist mapping languages to their file extensions.
  37. The key is the language name, the value is the string that should
  38. be inserted as the extension commonly used to identify files
  39. written in this language. If no entry is found in this list,
  40. then the name of the language is used."
  41. :group 'org-babel-tangle
  42. :version "24.1"
  43. :type '(repeat
  44. (cons
  45. (string "Language name")
  46. (string "File Extension"))))
  47. (defcustom org-babel-tangle-use-relative-file-links t
  48. "Use relative path names in links from tangled source back the Org-mode file."
  49. :group 'org-babel-tangle
  50. :type 'boolean)
  51. (defcustom org-babel-post-tangle-hook nil
  52. "Hook run in code files tangled by `org-babel-tangle'."
  53. :group 'org-babel
  54. :version "24.1"
  55. :type 'hook)
  56. (defcustom org-babel-pre-tangle-hook '(save-buffer)
  57. "Hook run at the beginning of `org-babel-tangle'."
  58. :group 'org-babel
  59. :version "24.1"
  60. :type 'hook)
  61. (defcustom org-babel-tangle-body-hook nil
  62. "Hook run over the contents of each code block body."
  63. :group 'org-babel
  64. :version "24.1"
  65. :type 'hook)
  66. (defcustom org-babel-tangle-comment-format-beg "[[%link][%source-name]]"
  67. "Format of inserted comments in tangled code files.
  68. The following format strings can be used to insert special
  69. information into the output using `org-fill-template'.
  70. %start-line --- the line number at the start of the code block
  71. %file --------- the file from which the code block was tangled
  72. %link --------- Org-mode style link to the code block
  73. %source-name -- name of the code block
  74. Upon insertion the formatted comment will be commented out, and
  75. followed by a newline. To inhibit this post-insertion processing
  76. set the `org-babel-tangle-uncomment-comments' variable to a
  77. non-nil value.
  78. Whether or not comments are inserted during tangling is
  79. controlled by the :comments header argument."
  80. :group 'org-babel
  81. :version "24.1"
  82. :type 'string)
  83. (defcustom org-babel-tangle-comment-format-end "%source-name ends here"
  84. "Format of inserted comments in tangled code files.
  85. The following format strings can be used to insert special
  86. information into the output using `org-fill-template'.
  87. %start-line --- the line number at the start of the code block
  88. %file --------- the file from which the code block was tangled
  89. %link --------- Org-mode style link to the code block
  90. %source-name -- name of the code block
  91. Upon insertion the formatted comment will be commented out, and
  92. followed by a newline. To inhibit this post-insertion processing
  93. set the `org-babel-tangle-uncomment-comments' variable to a
  94. non-nil value.
  95. Whether or not comments are inserted during tangling is
  96. controlled by the :comments header argument."
  97. :group 'org-babel
  98. :version "24.1"
  99. :type 'string)
  100. (defcustom org-babel-tangle-uncomment-comments nil
  101. "Inhibits automatic commenting and addition of trailing newline
  102. of tangle comments. Use `org-babel-tangle-comment-format-beg'
  103. and `org-babel-tangle-comment-format-end' to customize the format
  104. of tangled comments."
  105. :group 'org-babel
  106. :type 'boolean)
  107. (defcustom org-babel-process-comment-text #'org-remove-indentation
  108. "Function called to process raw Org-mode text collected to be
  109. inserted as comments in tangled source-code files. The function
  110. should take a single string argument and return a string
  111. result. The default value is `org-remove-indentation'."
  112. :group 'org-babel
  113. :version "24.1"
  114. :type 'function)
  115. (defun org-babel-find-file-noselect-refresh (file)
  116. "Find file ensuring that the latest changes on disk are
  117. represented in the file."
  118. (find-file-noselect file 'nowarn)
  119. (with-current-buffer (get-file-buffer file)
  120. (revert-buffer t t t)))
  121. (defmacro org-babel-with-temp-filebuffer (file &rest body)
  122. "Open FILE into a temporary buffer execute BODY there like
  123. `progn', then kill the FILE buffer returning the result of
  124. evaluating BODY."
  125. (declare (indent 1))
  126. (let ((temp-path (make-symbol "temp-path"))
  127. (temp-result (make-symbol "temp-result"))
  128. (temp-file (make-symbol "temp-file"))
  129. (visited-p (make-symbol "visited-p")))
  130. `(let* ((,temp-path ,file)
  131. (,visited-p (get-file-buffer ,temp-path))
  132. ,temp-result ,temp-file)
  133. (org-babel-find-file-noselect-refresh ,temp-path)
  134. (setf ,temp-file (get-file-buffer ,temp-path))
  135. (with-current-buffer ,temp-file
  136. (setf ,temp-result (progn ,@body)))
  137. (unless ,visited-p (kill-buffer ,temp-file))
  138. ,temp-result)))
  139. (def-edebug-spec org-babel-with-temp-filebuffer (form body))
  140. ;;;###autoload
  141. (defun org-babel-tangle-file (file &optional target-file lang)
  142. "Extract the bodies of source code blocks in FILE.
  143. Source code blocks are extracted with `org-babel-tangle'.
  144. Optional argument TARGET-FILE can be used to specify a default
  145. export file for all source blocks. Optional argument LANG can be
  146. used to limit the exported source code blocks by language.
  147. Return a list whose CAR is the tangled file name."
  148. (interactive "fFile to tangle: \nP")
  149. (let ((visited-p (get-file-buffer (expand-file-name file)))
  150. to-be-removed)
  151. (prog1
  152. (save-window-excursion
  153. (find-file file)
  154. (setq to-be-removed (current-buffer))
  155. (org-babel-tangle nil target-file lang))
  156. (unless visited-p
  157. (kill-buffer to-be-removed)))))
  158. (defun org-babel-tangle-publish (_ filename pub-dir)
  159. "Tangle FILENAME and place the results in PUB-DIR."
  160. (mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
  161. ;;;###autoload
  162. (defun org-babel-tangle (&optional arg target-file lang)
  163. "Write code blocks to source-specific files.
  164. Extract the bodies of all source code blocks from the current
  165. file into their own source-specific files.
  166. With one universal prefix argument, only tangle the block at point.
  167. When two universal prefix arguments, only tangle blocks for the
  168. tangle file of the block at point.
  169. Optional argument TARGET-FILE can be used to specify a default
  170. export file for all source blocks. Optional argument LANG can be
  171. used to limit the exported source code blocks by language."
  172. (interactive "P")
  173. (run-hooks 'org-babel-pre-tangle-hook)
  174. ;; Possibly Restrict the buffer to the current code block
  175. (save-restriction
  176. (save-excursion
  177. (when (equal arg '(4))
  178. (let ((head (org-babel-where-is-src-block-head)))
  179. (if head
  180. (goto-char head)
  181. (user-error "Point is not in a source code block"))))
  182. (let ((block-counter 0)
  183. (org-babel-default-header-args
  184. (if target-file
  185. (org-babel-merge-params org-babel-default-header-args
  186. (list (cons :tangle target-file)))
  187. org-babel-default-header-args))
  188. (tangle-file
  189. (when (equal arg '(16))
  190. (or (cdr (assoc :tangle (nth 2 (org-babel-get-src-block-info 'light))))
  191. (user-error "Point is not in a source code block"))))
  192. path-collector)
  193. (mapc ;; map over all languages
  194. (lambda (by-lang)
  195. (let* ((lang (car by-lang))
  196. (specs (cdr by-lang))
  197. (ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))
  198. (lang-f (intern
  199. (concat
  200. (or (and (cdr (assoc lang org-src-lang-modes))
  201. (symbol-name
  202. (cdr (assoc lang org-src-lang-modes))))
  203. lang)
  204. "-mode")))
  205. she-banged)
  206. (mapc
  207. (lambda (spec)
  208. (let ((get-spec (lambda (name) (cdr (assoc name (nth 4 spec))))))
  209. (let* ((tangle (funcall get-spec :tangle))
  210. (she-bang (let ((sheb (funcall get-spec :shebang)))
  211. (when (> (length sheb) 0) sheb)))
  212. (tangle-mode (funcall get-spec :tangle-mode))
  213. (base-name (cond
  214. ((string= "yes" tangle)
  215. (file-name-sans-extension
  216. (buffer-file-name)))
  217. ((string= "no" tangle) nil)
  218. ((> (length tangle) 0) tangle)))
  219. (file-name (when base-name
  220. ;; decide if we want to add ext to base-name
  221. (if (and ext (string= "yes" tangle))
  222. (concat base-name "." ext) base-name))))
  223. (when file-name
  224. ;; Possibly create the parent directories for file.
  225. (let ((m (funcall get-spec :mkdirp))
  226. (fnd (file-name-directory file-name)))
  227. (and m fnd (not (string= m "no"))
  228. (make-directory fnd 'parents)))
  229. ;; delete any old versions of file
  230. (and (file-exists-p file-name)
  231. (not (member file-name (mapcar #'car path-collector)))
  232. (delete-file file-name))
  233. ;; drop source-block to file
  234. (with-temp-buffer
  235. (when (fboundp lang-f) (ignore-errors (funcall lang-f)))
  236. (when (and she-bang (not (member file-name she-banged)))
  237. (insert (concat she-bang "\n"))
  238. (setq she-banged (cons file-name she-banged)))
  239. (org-babel-spec-to-string spec)
  240. ;; We avoid append-to-file as it does not work with tramp.
  241. (let ((content (buffer-string)))
  242. (with-temp-buffer
  243. (if (file-exists-p file-name)
  244. (insert-file-contents file-name))
  245. (goto-char (point-max))
  246. ;; Handle :padlines unless first line in file
  247. (unless (or (string= "no" (cdr (assoc :padline (nth 4 spec))))
  248. (= (point) (point-min)))
  249. (insert "\n"))
  250. (insert content)
  251. (write-region nil nil file-name))))
  252. ;; if files contain she-bangs, then make the executable
  253. (when she-bang
  254. (unless tangle-mode (setq tangle-mode #o755)))
  255. ;; update counter
  256. (setq block-counter (+ 1 block-counter))
  257. (add-to-list 'path-collector
  258. (cons file-name tangle-mode)
  259. nil
  260. (lambda (a b) (equal (car a) (car b))))))))
  261. specs)))
  262. (if (equal arg '(4))
  263. (org-babel-tangle-single-block 1 t)
  264. (org-babel-tangle-collect-blocks lang tangle-file)))
  265. (message "Tangled %d code block%s from %s" block-counter
  266. (if (= block-counter 1) "" "s")
  267. (file-name-nondirectory
  268. (buffer-file-name
  269. (or (buffer-base-buffer) (current-buffer)))))
  270. ;; run `org-babel-post-tangle-hook' in all tangled files
  271. (when org-babel-post-tangle-hook
  272. (mapc
  273. (lambda (file)
  274. (org-babel-with-temp-filebuffer file
  275. (run-hooks 'org-babel-post-tangle-hook)))
  276. (mapcar #'car path-collector)))
  277. ;; set permissions on tangled files
  278. (mapc (lambda (pair)
  279. (when (cdr pair) (set-file-modes (car pair) (cdr pair))))
  280. path-collector)
  281. (mapcar #'car path-collector)))))
  282. (defun org-babel-tangle-clean ()
  283. "Remove comments inserted by `org-babel-tangle'.
  284. Call this function inside of a source-code file generated by
  285. `org-babel-tangle' to remove all comments inserted automatically
  286. by `org-babel-tangle'. Warning, this comment removes any lines
  287. containing constructs which resemble org-mode file links or noweb
  288. references."
  289. (interactive)
  290. (goto-char (point-min))
  291. (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
  292. (re-search-forward (org-babel-noweb-wrap) nil t))
  293. (delete-region (save-excursion (beginning-of-line 1) (point))
  294. (save-excursion (end-of-line 1) (forward-char 1) (point)))))
  295. (defvar org-stored-links)
  296. (defvar org-bracket-link-regexp)
  297. (defun org-babel-spec-to-string (spec)
  298. "Insert SPEC into the current file.
  299. Insert the source-code specified by SPEC into the current source
  300. code file. This function uses `comment-region' which assumes
  301. that the appropriate major-mode is set. SPEC has the form:
  302. \(start-line file link source-name params body comment)"
  303. (let* ((start-line (nth 0 spec))
  304. (file (if org-babel-tangle-use-relative-file-links
  305. (file-relative-name (nth 1 spec))
  306. (nth 1 spec)))
  307. (link (let ((link (nth 2 spec)))
  308. (if org-babel-tangle-use-relative-file-links
  309. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  310. (let* ((type (match-string 1 link))
  311. (path (match-string 2 link))
  312. (origpath path)
  313. (case-fold-search nil))
  314. (setq path (file-relative-name path))
  315. (concat type path)))
  316. link)))
  317. (source-name (nth 3 spec))
  318. (body (nth 5 spec))
  319. (comment (nth 6 spec))
  320. (comments (cdr (assoc :comments (nth 4 spec))))
  321. (link-p (or (string= comments "both") (string= comments "link")
  322. (string= comments "yes") (string= comments "noweb")))
  323. (link-data (mapcar (lambda (el)
  324. (cons (symbol-name el)
  325. (let ((le (eval el)))
  326. (if (stringp le) le (format "%S" le)))))
  327. '(start-line file link source-name)))
  328. (insert-comment (lambda (text)
  329. (when (and comments (not (string= comments "no"))
  330. (> (length text) 0))
  331. (if org-babel-tangle-uncomment-comments
  332. ;; just plain comments with no processing
  333. (insert text)
  334. ;; ensure comments are made to be
  335. ;; comments, and add a trailing newline
  336. (comment-region
  337. (point) (progn (insert text) (point)))
  338. (end-of-line nil)
  339. (insert "\n"))))))
  340. (when comment (funcall insert-comment comment))
  341. (when link-p
  342. (funcall
  343. insert-comment
  344. (org-fill-template org-babel-tangle-comment-format-beg link-data)))
  345. (insert
  346. (format
  347. "%s\n"
  348. (org-unescape-code-in-string
  349. (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")))))
  350. (when link-p
  351. (funcall
  352. insert-comment
  353. (org-fill-template org-babel-tangle-comment-format-end link-data)))))
  354. (defun org-babel-tangle-collect-blocks (&optional language tangle-file)
  355. "Collect source blocks in the current Org file.
  356. Return an association list of source-code block specifications of
  357. the form used by `org-babel-spec-to-string' grouped by language.
  358. Optional argument LANGUAGE can be used to limit the collected
  359. source code blocks by language. Optional argument TANGLE-FILE
  360. can be used to limit the collected code blocks by target file."
  361. (let ((counter 0) last-heading-pos blocks)
  362. (org-babel-map-src-blocks (buffer-file-name)
  363. (let ((current-heading-pos
  364. (org-with-wide-buffer
  365. (org-with-limited-levels (outline-previous-heading)))))
  366. (cond ((eq last-heading-pos current-heading-pos) (incf counter))
  367. ((= counter 1))
  368. (t (setq counter 1))))
  369. (unless (org-in-commented-heading-p)
  370. (let* ((info (org-babel-get-src-block-info 'light))
  371. (src-lang (nth 0 info))
  372. (src-tfile (cdr (assq :tangle (nth 2 info)))))
  373. (unless (or (string= (cdr (assq :tangle (nth 2 info))) "no")
  374. (and tangle-file (not (equal tangle-file src-tfile)))
  375. (and language (not (string= language src-lang))))
  376. ;; Add the spec for this block to blocks under its
  377. ;; language.
  378. (let ((by-lang (assoc src-lang blocks))
  379. (block (org-babel-tangle-single-block counter)))
  380. (if by-lang (setcdr by-lang (cons block (cdr by-lang)))
  381. (push (cons src-lang (list block)) blocks)))))))
  382. ;; Ensure blocks are in the correct order.
  383. (mapcar (lambda (b) (cons (car b) (nreverse (cdr b)))) blocks)))
  384. (defun org-babel-tangle-single-block
  385. (block-counter &optional only-this-block)
  386. "Collect the tangled source for current block.
  387. Return the list of block attributes needed by
  388. `org-babel-tangle-collect-blocks'.
  389. When ONLY-THIS-BLOCK is non-nil, return the full association
  390. list to be used by `org-babel-tangle' directly."
  391. (let* ((info (org-babel-get-src-block-info))
  392. (start-line
  393. (save-restriction (widen)
  394. (+ 1 (line-number-at-pos (point)))))
  395. (file (buffer-file-name))
  396. (src-lang (nth 0 info))
  397. (params (nth 2 info))
  398. (extra (nth 3 info))
  399. (cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra)
  400. (match-string 1 extra))
  401. org-coderef-label-format))
  402. (link (let ((link (org-no-properties
  403. (org-store-link nil))))
  404. (and (string-match org-bracket-link-regexp link)
  405. (match-string 1 link))))
  406. (source-name
  407. (intern (or (nth 4 info)
  408. (format "%s:%d"
  409. (or (ignore-errors (nth 4 (org-heading-components)))
  410. "No heading")
  411. block-counter))))
  412. (expand-cmd
  413. (intern (concat "org-babel-expand-body:" src-lang)))
  414. (assignments-cmd
  415. (intern (concat "org-babel-variable-assignments:" src-lang)))
  416. (body
  417. ;; Run the tangle-body-hook.
  418. (let* ((body ;; Expand the body in language specific manner.
  419. (if (org-babel-noweb-p params :tangle)
  420. (org-babel-expand-noweb-references info)
  421. (nth 1 info)))
  422. (body
  423. (if (assoc :no-expand params)
  424. body
  425. (if (fboundp expand-cmd)
  426. (funcall expand-cmd body params)
  427. (org-babel-expand-body:generic
  428. body params
  429. (and (fboundp assignments-cmd)
  430. (funcall assignments-cmd params)))))))
  431. (with-temp-buffer
  432. (insert body)
  433. (when (string-match "-r" extra)
  434. (goto-char (point-min))
  435. (while (re-search-forward
  436. (replace-regexp-in-string "%s" ".+" cref-fmt) nil t)
  437. (replace-match "")))
  438. (run-hooks 'org-babel-tangle-body-hook)
  439. (buffer-string))))
  440. (comment
  441. (when (or (string= "both" (cdr (assoc :comments params)))
  442. (string= "org" (cdr (assoc :comments params))))
  443. ;; From the previous heading or code-block end
  444. (funcall
  445. org-babel-process-comment-text
  446. (buffer-substring
  447. (max (condition-case nil
  448. (save-excursion
  449. (org-back-to-heading t) ; Sets match data
  450. (match-end 0))
  451. (error (point-min)))
  452. (save-excursion
  453. (if (re-search-backward
  454. org-babel-src-block-regexp nil t)
  455. (match-end 0)
  456. (point-min))))
  457. (point)))))
  458. (result
  459. (list start-line file link source-name params body comment)))
  460. (if only-this-block
  461. (list (cons src-lang (list result)))
  462. result)))
  463. (defun org-babel-tangle-comment-links ( &optional info)
  464. "Return a list of begin and end link comments for the code block at point."
  465. (let* ((start-line (org-babel-where-is-src-block-head))
  466. (file (buffer-file-name))
  467. (link (org-link-escape (progn (call-interactively 'org-store-link)
  468. (org-no-properties
  469. (car (pop org-stored-links))))))
  470. (source-name (nth 4 (or info (org-babel-get-src-block-info 'light))))
  471. (link-data (mapcar (lambda (el)
  472. (cons (symbol-name el)
  473. (let ((le (eval el)))
  474. (if (stringp le) le (format "%S" le)))))
  475. '(start-line file link source-name))))
  476. (list (org-fill-template org-babel-tangle-comment-format-beg link-data)
  477. (org-fill-template org-babel-tangle-comment-format-end link-data))))
  478. ;; de-tangling functions
  479. (defvar org-bracket-link-analytic-regexp)
  480. (defun org-babel-detangle (&optional source-code-file)
  481. "Propagate changes in source file back original to Org-mode file.
  482. This requires that code blocks were tangled with link comments
  483. which enable the original code blocks to be found."
  484. (interactive)
  485. (save-excursion
  486. (when source-code-file (find-file source-code-file))
  487. (goto-char (point-min))
  488. (let ((counter 0) new-body end)
  489. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  490. (when (re-search-forward
  491. (concat " " (regexp-quote (match-string 5)) " ends here"))
  492. (setq end (match-end 0))
  493. (forward-line -1)
  494. (save-excursion
  495. (when (setq new-body (org-babel-tangle-jump-to-org))
  496. (org-babel-update-block-body new-body)))
  497. (setq counter (+ 1 counter)))
  498. (goto-char end))
  499. (prog1 counter (message "Detangled %d code blocks" counter)))))
  500. (defun org-babel-tangle-jump-to-org ()
  501. "Jump from a tangled code file to the related Org-mode file."
  502. (interactive)
  503. (let ((mid (point))
  504. start body-start end done
  505. target-buffer target-char link path block-name body)
  506. (save-window-excursion
  507. (save-excursion
  508. (while (and (re-search-backward org-bracket-link-analytic-regexp nil t)
  509. (not ; ever wider searches until matching block comments
  510. (and (setq start (point-at-eol))
  511. (setq body-start (save-excursion
  512. (forward-line 2) (point-at-bol)))
  513. (setq link (match-string 0))
  514. (setq path (match-string 3))
  515. (setq block-name (match-string 5))
  516. (save-excursion
  517. (save-match-data
  518. (re-search-forward
  519. (concat " " (regexp-quote block-name)
  520. " ends here") nil t)
  521. (setq end (point-at-bol))))))))
  522. (unless (and start (< start mid) (< mid end))
  523. (error "Not in tangled code"))
  524. (setq body (org-babel-trim (buffer-substring start end))))
  525. (when (string-match "::" path)
  526. (setq path (substring path 0 (match-beginning 0))))
  527. (find-file path) (setq target-buffer (current-buffer))
  528. (goto-char start) (org-open-link-from-string link)
  529. (if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
  530. (org-babel-next-src-block
  531. (string-to-number (match-string 1 block-name)))
  532. (org-babel-goto-named-src-block block-name))
  533. ;; position at the beginning of the code block body
  534. (goto-char (org-babel-where-is-src-block-head))
  535. (forward-line 1)
  536. ;; Use org-edit-special to isolate the code.
  537. (org-edit-special)
  538. ;; Then move forward the correct number of characters in the
  539. ;; code buffer.
  540. (forward-char (- mid body-start))
  541. ;; And return to the Org-mode buffer with the point in the right
  542. ;; place.
  543. (org-edit-src-exit)
  544. (setq target-char (point)))
  545. (org-src-switch-to-buffer target-buffer t)
  546. (prog1 body (goto-char target-char))))
  547. (provide 'ob-tangle)
  548. ;; Local variables:
  549. ;; generated-autoload-file: "org-loaddefs.el"
  550. ;; End:
  551. ;;; ob-tangle.el ends here