ob-tangle.el 22 KB

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