ob-tangle.el 22 KB

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