ob-tangle.el 22 KB

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