org-src.el 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. ;;; org-src.el --- Source code examples in Org -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Bastien Guerry <bzg@gnu.org>
  7. ;; Dan Davison <davison at stats dot ox dot ac dot uk>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;; This file contains the code dealing with source code examples in
  27. ;; Org mode.
  28. ;;; Code:
  29. (require 'cl-lib)
  30. (require 'org-macs)
  31. (require 'org-compat)
  32. (require 'ob-keys)
  33. (require 'ob-comint)
  34. (declare-function org-base-buffer "org" (buffer))
  35. (declare-function org-do-remove-indentation "org" (&optional n))
  36. (declare-function org-element-at-point "org-element" ())
  37. (declare-function org-element-class "org-element" (datum &optional parent))
  38. (declare-function org-element-context "org-element" (&optional element))
  39. (declare-function org-element-lineage "org-element"
  40. (blob &optional types with-self))
  41. (declare-function org-element-property "org-element" (property element))
  42. (declare-function org-element-type "org-element" (element))
  43. (declare-function org-footnote-goto-definition "org-footnote"
  44. (label &optional location))
  45. (declare-function org-get-indentation "org" (&optional line))
  46. (declare-function org-switch-to-buffer-other-window "org" (&rest args))
  47. (declare-function org-trim "org" (s &optional keep-lead))
  48. (defvar org-inhibit-startup)
  49. (defcustom org-edit-src-turn-on-auto-save nil
  50. "Non-nil means turn `auto-save-mode' on when editing a source block.
  51. This will save the content of the source code editing buffer into
  52. a newly created file, not the base buffer for this source block.
  53. If you want to regularly save the base buffer instead of the source
  54. code editing buffer, see `org-edit-src-auto-save-idle-delay' instead."
  55. :group 'org-edit-structure
  56. :version "24.4"
  57. :package-version '(Org . "8.0")
  58. :type 'boolean)
  59. (defcustom org-edit-src-auto-save-idle-delay 0
  60. "Delay before saving a source code buffer back into its base buffer.
  61. When a positive integer N, save after N seconds of idle time.
  62. When 0 (the default), don't auto-save.
  63. If you want to save the source code buffer itself, don't use this.
  64. Check `org-edit-src-turn-on-auto-save' instead."
  65. :group 'org-edit-structure
  66. :version "24.4"
  67. :package-version '(Org . "8.0")
  68. :type 'integer)
  69. (defcustom org-coderef-label-format "(ref:%s)"
  70. "The default coderef format.
  71. This format string will be used to search for coderef labels in literal
  72. examples (EXAMPLE and SRC blocks). The format can be overwritten in
  73. an individual literal example with the -l option, like
  74. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  75. ...
  76. #+END_SRC
  77. If you want to use this for HTML export, make sure that the format does
  78. not introduce special font-locking, and avoid the HTML special
  79. characters `<', `>', and `&'. The reason for this restriction is that
  80. the labels are searched for only after htmlize has done its job."
  81. :group 'org-edit-structure ; FIXME this is not in the right group
  82. :type 'string)
  83. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  84. "The mode that should be used to edit fixed-width regions.
  85. These are the regions where each line starts with a colon."
  86. :group 'org-edit-structure
  87. :type '(choice
  88. (const artist-mode)
  89. (const picture-mode)
  90. (const fundamental-mode)
  91. (function :tag "Other (specify)")))
  92. (defcustom org-src-preserve-indentation nil
  93. "If non-nil preserve leading whitespace characters on export.
  94. \\<org-mode-map>
  95. If non-nil leading whitespace characters in source code blocks
  96. are preserved on export, and when switching between the org
  97. buffer and the language mode edit buffer.
  98. When this variable is nil, after editing with `\\[org-edit-src-code]',
  99. the minimum (across-lines) number of leading whitespace characters
  100. are removed from all lines, and the code block is uniformly indented
  101. according to the value of `org-edit-src-content-indentation'."
  102. :group 'org-edit-structure
  103. :type 'boolean)
  104. (defcustom org-edit-src-content-indentation 2
  105. "Indentation for the content of a source code block.
  106. This should be the number of spaces added to the indentation of the #+begin
  107. line in order to compute the indentation of the block content after
  108. editing it with `\\[org-edit-src-code]'.
  109. It has no effect if `org-src-preserve-indentation' is non-nil."
  110. :group 'org-edit-structure
  111. :type 'integer)
  112. (defcustom org-edit-src-persistent-message t
  113. "Non-nil means show persistent exit help message while editing src examples.
  114. The message is shown in the header-line, which will be created in the
  115. first line of the window showing the editing buffer."
  116. :group 'org-edit-structure
  117. :type 'boolean)
  118. (defcustom org-src-ask-before-returning-to-edit-buffer t
  119. "Non-nil means ask before switching to an existing edit buffer.
  120. If nil, when `org-edit-src-code' is used on a block that already
  121. has an active edit buffer, it will switch to that edit buffer
  122. immediately; otherwise it will ask whether you want to return to
  123. the existing edit buffer."
  124. :group 'org-edit-structure
  125. :version "24.4"
  126. :package-version '(Org . "8.0")
  127. :type 'boolean)
  128. (defcustom org-src-window-setup 'reorganize-frame
  129. "How the source code edit buffer should be displayed.
  130. Possible values for this option are:
  131. current-window Show edit buffer in the current window, keeping all other
  132. windows.
  133. other-window Use `switch-to-buffer-other-window' to display edit buffer.
  134. reorganize-frame Show only two windows on the current frame, the current
  135. window and the edit buffer. When exiting the edit buffer,
  136. return to one window.
  137. other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
  138. Also, when exiting the edit buffer, kill that frame."
  139. :group 'org-edit-structure
  140. :type '(choice
  141. (const current-window)
  142. (const other-frame)
  143. (const other-window)
  144. (const reorganize-frame)))
  145. (defvar org-src-mode-hook nil
  146. "Hook run after Org switched a source code snippet to its Emacs mode.
  147. \\<org-mode-map>
  148. This hook will run:
  149. - when editing a source code snippet with `\\[org-edit-special]'
  150. - when formatting a source code snippet for export with htmlize.
  151. You may want to use this hook for example to turn off `outline-minor-mode'
  152. or similar things which you want to have when editing a source code file,
  153. but which mess up the display of a snippet in Org exported files.")
  154. (defcustom org-src-lang-modes
  155. '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
  156. ("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql)
  157. ("calc" . fundamental) ("C" . c) ("cpp" . c++) ("C++" . c++)
  158. ("screen" . shell-script) ("shell" . sh) ("bash" . sh))
  159. "Alist mapping languages to their major mode.
  160. The key is the language name, the value is the string that should
  161. be inserted as the name of the major mode. For many languages this is
  162. simple, but for language where this is not the case, this variable
  163. provides a way to simplify things on the user side.
  164. For example, there is no ocaml-mode in Emacs, but the mode to use is
  165. `tuareg-mode'."
  166. :group 'org-edit-structure
  167. :type '(repeat
  168. (cons
  169. (string "Language name")
  170. (symbol "Major mode"))))
  171. (defcustom org-src-block-faces nil
  172. "Alist of faces to be used for source-block.
  173. Each element is a cell of the format
  174. (\"language\" FACE)
  175. Where FACE is either a defined face or an anonymous face.
  176. For instance, the following value would color the background of
  177. emacs-lisp source blocks and python source blocks in purple and
  178. green, respectability.
  179. \\='((\"emacs-lisp\" (:background \"#EEE2FF\"))
  180. (\"python\" (:background \"#e5ffb8\")))"
  181. :group 'org-edit-structure
  182. :type '(repeat (list (string :tag "language")
  183. (choice
  184. (face :tag "Face")
  185. (sexp :tag "Anonymous face"))))
  186. :version "26.1"
  187. :package-version '(Org . "9.0"))
  188. (defcustom org-src-tab-acts-natively nil
  189. "If non-nil, the effect of TAB in a code block is as if it were
  190. issued in the language major mode buffer."
  191. :type 'boolean
  192. :version "24.1"
  193. :group 'org-babel)
  194. ;;; Internal functions and variables
  195. (defvar org-src--allow-write-back t)
  196. (defvar org-src--auto-save-timer nil)
  197. (defvar org-src--babel-info nil)
  198. (defvar org-src--beg-marker nil)
  199. (defvar org-src--block-indentation nil)
  200. (defvar org-src--end-marker nil)
  201. (defvar org-src--from-org-mode nil)
  202. (defvar org-src--overlay nil)
  203. (defvar org-src--preserve-indentation nil)
  204. (defvar org-src--remote nil)
  205. (defvar org-src--saved-temp-window-config nil)
  206. (defvar org-src--source-type nil
  207. "Type of element being edited, as a symbol.")
  208. (defvar org-src--tab-width nil
  209. "Contains `tab-width' value from Org source buffer.
  210. However, if `indent-tabs-mode' is nil in that buffer, its value
  211. is 0.")
  212. (defun org-src--construct-edit-buffer-name (org-buffer-name lang)
  213. "Construct the buffer name for a source editing buffer."
  214. (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
  215. (defun org-src--edit-buffer (beg end)
  216. "Return buffer editing area between BEG and END.
  217. Return nil if there is no such buffer."
  218. (catch 'exit
  219. (dolist (b (buffer-list))
  220. (with-current-buffer b
  221. (and (org-src-edit-buffer-p)
  222. (= beg org-src--beg-marker)
  223. (eq (marker-buffer beg) (marker-buffer org-src--beg-marker))
  224. (= end org-src--end-marker)
  225. (eq (marker-buffer end) (marker-buffer org-src--end-marker))
  226. (throw 'exit b))))))
  227. (defun org-src--source-buffer ()
  228. "Return source buffer edited by current buffer."
  229. (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
  230. (or (marker-buffer org-src--beg-marker)
  231. (error "No source buffer available for current editing session")))
  232. (defun org-src--get-lang-mode (lang)
  233. "Return major mode that should be used for LANG.
  234. LANG is a string, and the returned major mode is a symbol."
  235. (intern
  236. (concat
  237. (let ((l (or (cdr (assoc lang org-src-lang-modes)) lang)))
  238. (if (symbolp l) (symbol-name l) l))
  239. "-mode")))
  240. (defun org-src--coordinates (pos beg end)
  241. "Return coordinates of POS relatively to BEG and END.
  242. POS, BEG and END are buffer positions. Return value is either
  243. a cons cell (LINE . COLUMN) or symbol `end'. See also
  244. `org-src--goto-coordinates'."
  245. (if (>= pos end) 'end
  246. (org-with-wide-buffer
  247. (goto-char (max beg pos))
  248. (cons (count-lines beg (line-beginning-position))
  249. ;; Column is relative to the end of line to avoid problems of
  250. ;; comma escaping or colons appended in front of the line.
  251. (- (current-column)
  252. (progn (end-of-line) (current-column)))))))
  253. (defun org-src--goto-coordinates (coord beg end)
  254. "Move to coordinates COORD relatively to BEG and END.
  255. COORD are coordinates, as returned by `org-src--coordinates',
  256. which see. BEG and END are buffer positions."
  257. (goto-char
  258. (if (eq coord 'end) (max (1- end) beg)
  259. ;; If BEG happens to be located outside of the narrowed part of
  260. ;; the buffer, widen it first.
  261. (org-with-wide-buffer
  262. (goto-char beg)
  263. (forward-line (car coord))
  264. (end-of-line)
  265. (org-move-to-column (max (+ (current-column) (cdr coord)) 0))
  266. (point)))))
  267. (defun org-src--contents-area (datum)
  268. "Return contents boundaries of DATUM.
  269. DATUM is an element or object. Return a list (BEG END CONTENTS)
  270. where BEG and END are buffer positions and CONTENTS is a string."
  271. (let ((type (org-element-type datum)))
  272. (org-with-wide-buffer
  273. (cond
  274. ((eq type 'footnote-definition)
  275. (let* ((beg (progn
  276. (goto-char (org-element-property :post-affiliated datum))
  277. (search-forward "]")))
  278. (end (or (org-element-property :contents-end datum) beg)))
  279. (list beg end (buffer-substring-no-properties beg end))))
  280. ((eq type 'inline-src-block)
  281. (let ((beg (progn (goto-char (org-element-property :begin datum))
  282. (search-forward "{" (line-end-position) t)))
  283. (end (progn (goto-char (org-element-property :end datum))
  284. (search-backward "}" (line-beginning-position) t))))
  285. (list beg end (buffer-substring-no-properties beg end))))
  286. ((org-element-property :contents-begin datum)
  287. (let ((beg (org-element-property :contents-begin datum))
  288. (end (org-element-property :contents-end datum)))
  289. (list beg end (buffer-substring-no-properties beg end))))
  290. ((memq type '(example-block export-block src-block))
  291. (list (progn (goto-char (org-element-property :post-affiliated datum))
  292. (line-beginning-position 2))
  293. (progn (goto-char (org-element-property :end datum))
  294. (skip-chars-backward " \r\t\n")
  295. (line-beginning-position 1))
  296. (org-element-property :value datum)))
  297. ((memq type '(fixed-width table))
  298. (let ((beg (org-element-property :post-affiliated datum))
  299. (end (progn (goto-char (org-element-property :end datum))
  300. (skip-chars-backward " \r\t\n")
  301. (line-beginning-position 2))))
  302. (list beg
  303. end
  304. (if (eq type 'fixed-width) (org-element-property :value datum)
  305. (buffer-substring-no-properties beg end)))))
  306. (t (error "Unsupported element or object: %s" type))))))
  307. (defun org-src--make-source-overlay (beg end edit-buffer)
  308. "Create overlay between BEG and END positions and return it.
  309. EDIT-BUFFER is the buffer currently editing area between BEG and
  310. END."
  311. (let ((overlay (make-overlay beg end)))
  312. (overlay-put overlay 'face 'secondary-selection)
  313. (overlay-put overlay 'edit-buffer edit-buffer)
  314. (overlay-put overlay 'help-echo
  315. "Click with mouse-1 to switch to buffer editing this segment")
  316. (overlay-put overlay 'face 'secondary-selection)
  317. (overlay-put overlay 'keymap
  318. (let ((map (make-sparse-keymap)))
  319. (define-key map [mouse-1] 'org-edit-src-continue)
  320. map))
  321. (let ((read-only
  322. (list
  323. (lambda (&rest _)
  324. (user-error
  325. "Cannot modify an area being edited in a dedicated buffer")))))
  326. (overlay-put overlay 'modification-hooks read-only)
  327. (overlay-put overlay 'insert-in-front-hooks read-only)
  328. (overlay-put overlay 'insert-behind-hooks read-only))
  329. overlay))
  330. (defun org-src--remove-overlay ()
  331. "Remove overlay from current source buffer."
  332. (when (overlayp org-src--overlay) (delete-overlay org-src--overlay)))
  333. (defun org-src--on-datum-p (datum)
  334. "Non-nil when point is on DATUM.
  335. DATUM is an element or an object. Consider blank lines or white
  336. spaces after it as being outside."
  337. (and (>= (point) (org-element-property :begin datum))
  338. (<= (point)
  339. (org-with-wide-buffer
  340. (goto-char (org-element-property :end datum))
  341. (skip-chars-backward " \r\t\n")
  342. (if (eq (org-element-class datum) 'element)
  343. (line-end-position)
  344. (point))))))
  345. (defun org-src--contents-for-write-back ()
  346. "Return buffer contents in a format appropriate for write back.
  347. Assume point is in the corresponding edit buffer."
  348. (let ((indentation-offset
  349. (if org-src--preserve-indentation 0
  350. (+ (or org-src--block-indentation 0)
  351. (if (memq org-src--source-type '(example-block src-block))
  352. org-edit-src-content-indentation
  353. 0))))
  354. (use-tabs? (and (> org-src--tab-width 0) t))
  355. (source-tab-width org-src--tab-width)
  356. (contents (org-with-wide-buffer (buffer-string)))
  357. (write-back org-src--allow-write-back))
  358. (with-temp-buffer
  359. ;; Reproduce indentation parameters from source buffer.
  360. (setq-local indent-tabs-mode use-tabs?)
  361. (when (> source-tab-width 0) (setq-local tab-width source-tab-width))
  362. ;; Apply WRITE-BACK function on edit buffer contents.
  363. (insert (org-no-properties contents))
  364. (goto-char (point-min))
  365. (when (functionp write-back) (save-excursion (funcall write-back)))
  366. ;; Add INDENTATION-OFFSET to every non-empty line in buffer,
  367. ;; unless indentation is meant to be preserved.
  368. (when (> indentation-offset 0)
  369. (while (not (eobp))
  370. (skip-chars-forward " \t")
  371. (unless (eolp) ;ignore blank lines
  372. (let ((i (current-column)))
  373. (delete-region (line-beginning-position) (point))
  374. (indent-to (+ i indentation-offset))))
  375. (forward-line)))
  376. (buffer-string))))
  377. (defun org-src--edit-element
  378. (datum name &optional major write-back contents remote)
  379. "Edit DATUM contents in a dedicated buffer NAME.
  380. MAJOR is the major mode used in the edit buffer. A nil value is
  381. equivalent to `fundamental-mode'.
  382. When WRITE-BACK is non-nil, assume contents will replace original
  383. region. Moreover, if it is a function, apply it in the edit
  384. buffer, from point min, before returning the contents.
  385. When CONTENTS is non-nil, display them in the edit buffer.
  386. Otherwise, show DATUM contents as specified by
  387. `org-src--contents-area'.
  388. When REMOTE is non-nil, do not try to preserve point or mark when
  389. moving from the edit area to the source.
  390. Leave point in edit buffer."
  391. (setq org-src--saved-temp-window-config (current-window-configuration))
  392. (let* ((area (org-src--contents-area datum))
  393. (beg (copy-marker (nth 0 area)))
  394. (end (copy-marker (nth 1 area) t))
  395. (old-edit-buffer (org-src--edit-buffer beg end))
  396. (contents (or contents (nth 2 area))))
  397. (if (and old-edit-buffer
  398. (or (not org-src-ask-before-returning-to-edit-buffer)
  399. (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
  400. ;; Move to existing buffer.
  401. (org-src-switch-to-buffer old-edit-buffer 'return)
  402. ;; Discard old edit buffer.
  403. (when old-edit-buffer
  404. (with-current-buffer old-edit-buffer (org-src--remove-overlay))
  405. (kill-buffer old-edit-buffer))
  406. (let* ((org-mode-p (derived-mode-p 'org-mode))
  407. (source-tab-width (if indent-tabs-mode tab-width 0))
  408. (type (org-element-type datum))
  409. (ind (org-with-wide-buffer
  410. (goto-char (org-element-property :begin datum))
  411. (org-get-indentation)))
  412. (preserve-ind
  413. (and (memq type '(example-block src-block))
  414. (or (org-element-property :preserve-indent datum)
  415. org-src-preserve-indentation)))
  416. ;; Store relative positions of mark (if any) and point
  417. ;; within the edited area.
  418. (point-coordinates (and (not remote)
  419. (org-src--coordinates (point) beg end)))
  420. (mark-coordinates (and (not remote)
  421. (org-region-active-p)
  422. (let ((m (mark)))
  423. (and (>= m beg) (>= end m)
  424. (org-src--coordinates m beg end)))))
  425. ;; Generate a new edit buffer.
  426. (buffer (generate-new-buffer name))
  427. ;; Add an overlay on top of source.
  428. (overlay (org-src--make-source-overlay beg end buffer)))
  429. ;; Switch to edit buffer.
  430. (org-src-switch-to-buffer buffer 'edit)
  431. ;; Insert contents.
  432. (insert contents)
  433. (remove-text-properties (point-min) (point-max)
  434. '(display nil invisible nil intangible nil))
  435. (unless preserve-ind (org-do-remove-indentation))
  436. (set-buffer-modified-p nil)
  437. (setq buffer-file-name nil)
  438. ;; Start major mode.
  439. (if (not major) (fundamental-mode)
  440. (let ((org-inhibit-startup t))
  441. (condition-case e (funcall major)
  442. (error (message "Language mode `%s' fails with: %S"
  443. major (nth 1 e))))))
  444. ;; Transmit buffer-local variables for exit function. It must
  445. ;; be done after initializing major mode, as this operation
  446. ;; may reset them otherwise.
  447. (setq-local org-src--tab-width source-tab-width)
  448. (setq-local org-src--from-org-mode org-mode-p)
  449. (setq-local org-src--beg-marker beg)
  450. (setq-local org-src--end-marker end)
  451. (setq-local org-src--remote remote)
  452. (setq-local org-src--source-type type)
  453. (setq-local org-src--block-indentation ind)
  454. (setq-local org-src--preserve-indentation preserve-ind)
  455. (setq-local org-src--overlay overlay)
  456. (setq-local org-src--allow-write-back write-back)
  457. ;; Start minor mode.
  458. (org-src-mode)
  459. ;; Move mark and point in edit buffer to the corresponding
  460. ;; location.
  461. (if remote
  462. (progn
  463. ;; Put point at first non read-only character after
  464. ;; leading blank.
  465. (goto-char
  466. (or (text-property-any (point-min) (point-max) 'read-only nil)
  467. (point-max)))
  468. (skip-chars-forward " \r\t\n"))
  469. ;; Set mark and point.
  470. (when mark-coordinates
  471. (org-src--goto-coordinates mark-coordinates (point-min) (point-max))
  472. (push-mark (point) 'no-message t)
  473. (setq deactivate-mark nil))
  474. (org-src--goto-coordinates
  475. point-coordinates (point-min) (point-max)))))))
  476. ;;; Fontification of source blocks
  477. (defun org-src-font-lock-fontify-block (lang start end)
  478. "Fontify code block.
  479. This function is called by emacs automatic fontification, as long
  480. as `org-src-fontify-natively' is non-nil."
  481. (let ((lang-mode (org-src--get-lang-mode lang)))
  482. (when (fboundp lang-mode)
  483. (let ((string (buffer-substring-no-properties start end))
  484. (modified (buffer-modified-p))
  485. (org-buffer (current-buffer)))
  486. (remove-text-properties start end '(face nil))
  487. (with-current-buffer
  488. (get-buffer-create
  489. (format " *org-src-fontification:%s*" lang-mode))
  490. (let ((inhibit-modification-hooks nil))
  491. (erase-buffer)
  492. ;; Add string and a final space to ensure property change.
  493. (insert string " "))
  494. (unless (eq major-mode lang-mode) (funcall lang-mode))
  495. (org-font-lock-ensure)
  496. (let ((pos (point-min)) next)
  497. (while (setq next (next-property-change pos))
  498. ;; Handle additional properties from font-lock, so as to
  499. ;; preserve, e.g., composition.
  500. (dolist (prop (cons 'face font-lock-extra-managed-props))
  501. (let ((new-prop (get-text-property pos prop)))
  502. (put-text-property
  503. (+ start (1- pos)) (1- (+ start next)) prop new-prop
  504. org-buffer)))
  505. (setq pos next))))
  506. ;; Add Org faces.
  507. (let ((src-face (nth 1 (assoc-string lang org-src-block-faces t))))
  508. (when (or (facep src-face) (listp src-face))
  509. (font-lock-append-text-property start end 'face src-face))
  510. (font-lock-append-text-property start end 'face 'org-block))
  511. (add-text-properties
  512. start end
  513. '(font-lock-fontified t fontified t font-lock-multiline t))
  514. (set-buffer-modified-p modified)))))
  515. ;;; Escape contents
  516. (defun org-escape-code-in-region (beg end)
  517. "Escape lines between BEG and END.
  518. Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
  519. \",#+\" by appending a comma to it."
  520. (interactive "r")
  521. (save-excursion
  522. (goto-char end)
  523. (while (re-search-backward "^[ \t]*,?\\(\\*\\|#\\+\\)" beg t)
  524. (save-excursion (replace-match ",\\1" nil nil nil 1)))))
  525. (defun org-escape-code-in-string (s)
  526. "Escape lines in string S.
  527. Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
  528. \",#+\" by appending a comma to it."
  529. (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil 1))
  530. (defun org-unescape-code-in-region (beg end)
  531. "Un-escape lines between BEG and END.
  532. Un-escaping happens by removing the first comma on lines starting
  533. with \",*\", \",#+\", \",,*\" and \",,#+\"."
  534. (interactive "r")
  535. (save-excursion
  536. (goto-char end)
  537. (while (re-search-backward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" beg t)
  538. (save-excursion (replace-match "" nil nil nil 1)))))
  539. (defun org-unescape-code-in-string (s)
  540. "Un-escape lines in string S.
  541. Un-escaping happens by removing the first comma on lines starting
  542. with \",*\", \",#+\", \",,*\" and \",,#+\"."
  543. (replace-regexp-in-string
  544. "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1))
  545. ;;; Org src minor mode
  546. (defvar org-src-mode-map
  547. (let ((map (make-sparse-keymap)))
  548. (define-key map "\C-c'" 'org-edit-src-exit)
  549. (define-key map "\C-c\C-k" 'org-edit-src-abort)
  550. (define-key map "\C-x\C-s" 'org-edit-src-save)
  551. map))
  552. (define-minor-mode org-src-mode
  553. "Minor mode for language major mode buffers generated by Org.
  554. \\<org-mode-map>
  555. This minor mode is turned on in two situations:
  556. - when editing a source code snippet with `\\[org-edit-special]'
  557. - when formatting a source code snippet for export with htmlize.
  558. \\{org-src-mode-map}
  559. See also `org-src-mode-hook'."
  560. nil " OrgSrc" nil
  561. (when org-edit-src-persistent-message
  562. (setq-local
  563. header-line-format
  564. (substitute-command-keys
  565. (if org-src--allow-write-back
  566. "Edit, then exit with `\\[org-edit-src-exit]' or abort with \
  567. `\\[org-edit-src-abort]'"
  568. "Exit with `\\[org-edit-src-exit]' or abort with \
  569. `\\[org-edit-src-abort]'"))))
  570. ;; Possibly activate various auto-save features (for the edit buffer
  571. ;; or the source buffer).
  572. (when org-edit-src-turn-on-auto-save
  573. (setq buffer-auto-save-file-name
  574. (concat (make-temp-name "org-src-")
  575. (format-time-string "-%Y-%d-%m")
  576. ".txt")))
  577. (unless (or org-src--auto-save-timer (zerop org-edit-src-auto-save-idle-delay))
  578. (setq org-src--auto-save-timer
  579. (run-with-idle-timer
  580. org-edit-src-auto-save-idle-delay t
  581. (lambda ()
  582. (save-excursion
  583. (let (edit-flag)
  584. (dolist (b (buffer-list))
  585. (with-current-buffer b
  586. (when (org-src-edit-buffer-p)
  587. (unless edit-flag (setq edit-flag t))
  588. (when (buffer-modified-p) (org-edit-src-save)))))
  589. (unless edit-flag
  590. (cancel-timer org-src--auto-save-timer)
  591. (setq org-src--auto-save-timer nil)))))))))
  592. (defun org-src-mode-configure-edit-buffer ()
  593. (when (bound-and-true-p org-src--from-org-mode)
  594. (add-hook 'kill-buffer-hook #'org-src--remove-overlay nil 'local)
  595. (if (bound-and-true-p org-src--allow-write-back)
  596. (progn
  597. (setq buffer-offer-save t)
  598. (setq buffer-file-name
  599. (concat (buffer-file-name (marker-buffer org-src--beg-marker))
  600. "[" (buffer-name) "]"))
  601. (setq-local write-contents-functions '(org-edit-src-save)))
  602. (setq buffer-read-only t))))
  603. (add-hook 'org-src-mode-hook #'org-src-mode-configure-edit-buffer)
  604. ;;; Babel related functions
  605. (defun org-src-associate-babel-session (info)
  606. "Associate edit buffer with comint session."
  607. (interactive)
  608. (let ((session (cdr (assq :session (nth 2 info)))))
  609. (and session (not (string= session "none"))
  610. (org-babel-comint-buffer-livep session)
  611. (let ((f (intern (format "org-babel-%s-associate-session"
  612. (nth 0 info)))))
  613. (and (fboundp f) (funcall f session))))))
  614. (defun org-src-babel-configure-edit-buffer ()
  615. (when org-src--babel-info
  616. (org-src-associate-babel-session org-src--babel-info)))
  617. (add-hook 'org-src-mode-hook #'org-src-babel-configure-edit-buffer)
  618. ;;; Public API
  619. (defmacro org-src-do-at-code-block (&rest body)
  620. "Execute BODY from an edit buffer in the Org mode buffer."
  621. (declare (debug (body)))
  622. `(let ((beg-marker org-src--beg-marker))
  623. (when beg-marker
  624. (with-current-buffer (marker-buffer beg-marker)
  625. (goto-char beg-marker)
  626. ,@body))))
  627. (defun org-src-do-key-sequence-at-code-block (&optional key)
  628. "Execute key sequence at code block in the source Org buffer.
  629. The command bound to KEY in the Org-babel key map is executed
  630. remotely with point temporarily at the start of the code block in
  631. the Org buffer.
  632. This command is not bound to a key by default, to avoid conflicts
  633. with language major mode bindings. To bind it to C-c @ in all
  634. language major modes, you could use
  635. (add-hook \\='org-src-mode-hook
  636. (lambda () (define-key org-src-mode-map \"\\C-c@\"
  637. \\='org-src-do-key-sequence-at-code-block)))
  638. In that case, for example, C-c @ t issued in code edit buffers
  639. would tangle the current Org code block, C-c @ e would execute
  640. the block and C-c @ h would display the other available
  641. Org-babel commands."
  642. (interactive "kOrg-babel key: ")
  643. (if (equal key (kbd "C-g")) (keyboard-quit)
  644. (org-edit-src-save)
  645. (org-src-do-at-code-block
  646. (call-interactively (lookup-key org-babel-map key)))))
  647. (defun org-src-edit-buffer-p (&optional buffer)
  648. "Non-nil when current buffer is a source editing buffer.
  649. If BUFFER is non-nil, test it instead."
  650. (let ((buffer (org-base-buffer (or buffer (current-buffer)))))
  651. (and (buffer-live-p buffer)
  652. (local-variable-p 'org-src--beg-marker buffer)
  653. (local-variable-p 'org-src--end-marker buffer))))
  654. (defun org-src-switch-to-buffer (buffer context)
  655. (pcase org-src-window-setup
  656. (`current-window (pop-to-buffer-same-window buffer))
  657. (`other-window
  658. (switch-to-buffer-other-window buffer))
  659. (`other-frame
  660. (pcase context
  661. (`exit
  662. (let ((frame (selected-frame)))
  663. (switch-to-buffer-other-frame buffer)
  664. (delete-frame frame)))
  665. (`save
  666. (kill-buffer (current-buffer))
  667. (pop-to-buffer-same-window buffer))
  668. (_ (switch-to-buffer-other-frame buffer))))
  669. (`reorganize-frame
  670. (when (eq context 'edit) (delete-other-windows))
  671. (org-switch-to-buffer-other-window buffer)
  672. (when (eq context 'exit) (delete-other-windows)))
  673. (`switch-invisibly (set-buffer buffer))
  674. (_
  675. (message "Invalid value %s for `org-src-window-setup'"
  676. org-src-window-setup)
  677. (pop-to-buffer-same-window buffer))))
  678. (defun org-src-coderef-format (&optional element)
  679. "Return format string for block at point.
  680. When optional argument ELEMENT is provided, use that block.
  681. Otherwise, assume point is either at a source block, at an
  682. example block.
  683. If point is in an edit buffer, retrieve format string associated
  684. to the remote source block."
  685. (cond
  686. ((and element (org-element-property :label-fmt element)))
  687. ((org-src-edit-buffer-p) (org-src-do-at-code-block (org-src-coderef-format)))
  688. ((org-element-property :label-fmt (org-element-at-point)))
  689. (t org-coderef-label-format)))
  690. (defun org-src-coderef-regexp (fmt &optional label)
  691. "Return regexp matching a coderef format string FMT.
  692. When optional argument LABEL is non-nil, match coderef for that
  693. label only.
  694. Match group 1 contains the full coderef string with surrounding
  695. white spaces. Match group 2 contains the same string without any
  696. surrounding space. Match group 3 contains the label.
  697. A coderef format regexp can only match at the end of a line."
  698. (format "\\([ \t]*\\(%s\\)[ \t]*\\)$"
  699. (replace-regexp-in-string
  700. "%s"
  701. (if label (regexp-quote label) "\\([-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\)")
  702. (regexp-quote fmt)
  703. nil t)))
  704. (defun org-edit-footnote-reference ()
  705. "Edit definition of footnote reference at point."
  706. (interactive)
  707. (let* ((context (org-element-context))
  708. (label (org-element-property :label context)))
  709. (unless (and (eq (org-element-type context) 'footnote-reference)
  710. (org-src--on-datum-p context))
  711. (user-error "Not on a footnote reference"))
  712. (unless label (user-error "Cannot edit remotely anonymous footnotes"))
  713. (let* ((definition (org-with-wide-buffer
  714. (org-footnote-goto-definition label)
  715. (backward-char)
  716. (org-element-context)))
  717. (inline (eq (org-element-type definition) 'footnote-reference))
  718. (contents
  719. (let ((c (org-with-wide-buffer
  720. (org-trim (buffer-substring-no-properties
  721. (org-element-property :begin definition)
  722. (org-element-property :end definition))))))
  723. (add-text-properties
  724. 0
  725. (progn (string-match (if inline "\\`\\[fn:.*?:" "\\`.*?\\]") c)
  726. (match-end 0))
  727. '(read-only "Cannot edit footnote label" front-sticky t
  728. rear-nonsticky t)
  729. c)
  730. (when inline
  731. (let ((l (length c)))
  732. (add-text-properties
  733. (1- l) l
  734. '(read-only "Cannot edit past footnote reference"
  735. front-sticky nil rear-nonsticky nil)
  736. c)))
  737. c)))
  738. (org-src--edit-element
  739. definition
  740. (format "*Edit footnote [%s]*" label)
  741. #'org-mode
  742. `(lambda ()
  743. (if ,(not inline) (delete-region (point) (search-forward "]"))
  744. (delete-region (point) (search-forward ":" nil t 2))
  745. (delete-region (1- (point-max)) (point-max))
  746. (when (re-search-forward "\n[ \t]*\n" nil t)
  747. (user-error "Inline definitions cannot contain blank lines"))
  748. ;; If footnote reference belongs to a table, make sure to
  749. ;; remove any newline characters in order to preserve
  750. ;; table's structure.
  751. (when ,(org-element-lineage definition '(table-cell))
  752. (while (search-forward "\n" nil t) (delete-char -1)))))
  753. (concat contents
  754. (and (not (org-element-property :contents-begin definition))
  755. " "))
  756. 'remote))
  757. ;; Report success.
  758. t))
  759. (defun org-edit-table.el ()
  760. "Edit \"table.el\" table at point.
  761. \\<org-src-mode-map>
  762. A new buffer is created and the table is copied into it. Then
  763. the table is recognized with `table-recognize'. When done
  764. editing, exit with `\\[org-edit-src-exit]'. The edited text will \
  765. then replace
  766. the area in the Org mode buffer.
  767. Throw an error when not at such a table."
  768. (interactive)
  769. (let ((element (org-element-at-point)))
  770. (unless (and (eq (org-element-type element) 'table)
  771. (eq (org-element-property :type element) 'table.el)
  772. (org-src--on-datum-p element))
  773. (user-error "Not in a table.el table"))
  774. (org-src--edit-element
  775. element
  776. (org-src--construct-edit-buffer-name (buffer-name) "Table")
  777. #'text-mode t)
  778. (when (bound-and-true-p flyspell-mode) (flyspell-mode -1))
  779. (table-recognize)
  780. t))
  781. (defun org-edit-export-block ()
  782. "Edit export block at point.
  783. \\<org-src-mode-map>
  784. A new buffer is created and the block is copied into it, and the
  785. buffer is switched into an appropriate major mode. See also
  786. `org-src-lang-modes'.
  787. When done, exit with `\\[org-edit-src-exit]'. The edited text \
  788. will then replace
  789. the area in the Org mode buffer.
  790. Throw an error when not at an export block."
  791. (interactive)
  792. (let ((element (org-element-at-point)))
  793. (unless (and (eq (org-element-type element) 'export-block)
  794. (org-src--on-datum-p element))
  795. (user-error "Not in an export block"))
  796. (let* ((type (downcase (org-element-property :type element)))
  797. (mode (org-src--get-lang-mode type)))
  798. (unless (functionp mode) (error "No such language mode: %s" mode))
  799. (org-src--edit-element
  800. element
  801. (org-src--construct-edit-buffer-name (buffer-name) type)
  802. mode
  803. (lambda () (org-escape-code-in-region (point-min) (point-max)))))
  804. t))
  805. (defun org-edit-src-code (&optional code edit-buffer-name)
  806. "Edit the source or example block at point.
  807. \\<org-src-mode-map>
  808. The code is copied to a separate buffer and the appropriate mode
  809. is turned on. When done, exit with `\\[org-edit-src-exit]'. This \
  810. will remove the
  811. original code in the Org buffer, and replace it with the edited
  812. version. See `org-src-window-setup' to configure the display of
  813. windows containing the Org buffer and the code buffer.
  814. When optional argument CODE is a string, edit it in a dedicated
  815. buffer instead.
  816. When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
  817. name of the sub-editing buffer."
  818. (interactive)
  819. (let* ((element (org-element-at-point))
  820. (type (org-element-type element)))
  821. (unless (and (memq type '(example-block src-block))
  822. (org-src--on-datum-p element))
  823. (user-error "Not in a source or example block"))
  824. (let* ((lang
  825. (if (eq type 'src-block) (org-element-property :language element)
  826. "example"))
  827. (lang-f (and (eq type 'src-block) (org-src--get-lang-mode lang)))
  828. (babel-info (and (eq type 'src-block)
  829. (org-babel-get-src-block-info 'light)))
  830. deactivate-mark)
  831. (when (and (eq type 'src-block) (not (functionp lang-f)))
  832. (error "No such language mode: %s" lang-f))
  833. (org-src--edit-element
  834. element
  835. (or edit-buffer-name
  836. (org-src--construct-edit-buffer-name (buffer-name) lang))
  837. lang-f
  838. (and (null code)
  839. (lambda () (org-escape-code-in-region (point-min) (point-max))))
  840. (and code (org-unescape-code-in-string code)))
  841. ;; Finalize buffer.
  842. (setq-local org-coderef-label-format
  843. (or (org-element-property :label-fmt element)
  844. org-coderef-label-format))
  845. (when (eq type 'src-block)
  846. (setq-local org-src--babel-info babel-info)
  847. (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
  848. (when (fboundp edit-prep-func)
  849. (funcall edit-prep-func babel-info))))
  850. t)))
  851. (defun org-edit-inline-src-code ()
  852. "Edit inline source code at point."
  853. (interactive)
  854. (let ((context (org-element-context)))
  855. (unless (and (eq (org-element-type context) 'inline-src-block)
  856. (org-src--on-datum-p context))
  857. (user-error "Not on inline source code"))
  858. (let* ((lang (org-element-property :language context))
  859. (lang-f (org-src--get-lang-mode lang))
  860. (babel-info (org-babel-get-src-block-info 'light))
  861. deactivate-mark)
  862. (unless (functionp lang-f) (error "No such language mode: %s" lang-f))
  863. (org-src--edit-element
  864. context
  865. (org-src--construct-edit-buffer-name (buffer-name) lang)
  866. lang-f
  867. (lambda ()
  868. ;; Inline src blocks are limited to one line.
  869. (while (re-search-forward "\n[ \t]*" nil t) (replace-match " "))
  870. ;; Trim contents.
  871. (goto-char (point-min))
  872. (skip-chars-forward " \t")
  873. (delete-region (point-min) (point))
  874. (goto-char (point-max))
  875. (skip-chars-backward " \t")
  876. (delete-region (point) (point-max))))
  877. ;; Finalize buffer.
  878. (setq-local org-src--babel-info babel-info)
  879. (setq-local org-src--preserve-indentation t)
  880. (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
  881. (when (fboundp edit-prep-func) (funcall edit-prep-func babel-info)))
  882. ;; Return success.
  883. t)))
  884. (defun org-edit-fixed-width-region ()
  885. "Edit the fixed-width ASCII drawing at point.
  886. \\<org-src-mode-map>
  887. This must be a region where each line starts with a colon
  888. followed by a space or a newline character.
  889. A new buffer is created and the fixed-width region is copied into
  890. it, and the buffer is switched into the major mode defined in
  891. `org-edit-fixed-width-region-mode', which see.
  892. When done, exit with `\\[org-edit-src-exit]'. The edited text \
  893. will then replace
  894. the area in the Org mode buffer."
  895. (interactive)
  896. (let ((element (org-element-at-point)))
  897. (unless (and (eq (org-element-type element) 'fixed-width)
  898. (org-src--on-datum-p element))
  899. (user-error "Not in a fixed-width area"))
  900. (org-src--edit-element
  901. element
  902. (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
  903. org-edit-fixed-width-region-mode
  904. (lambda () (while (not (eobp)) (insert ": ") (forward-line))))
  905. ;; Return success.
  906. t))
  907. (defun org-edit-src-abort ()
  908. "Abort editing of the src code and return to the Org buffer."
  909. (interactive)
  910. (let (org-src--allow-write-back) (org-edit-src-exit)))
  911. (defun org-edit-src-continue (e)
  912. "Unconditionally return to buffer editing area under point.
  913. Throw an error if there is no such buffer."
  914. (interactive "e")
  915. (mouse-set-point e)
  916. (let ((buf (get-char-property (point) 'edit-buffer)))
  917. (if buf (org-src-switch-to-buffer buf 'continue)
  918. (user-error "No sub-editing buffer for area at point"))))
  919. (defun org-edit-src-save ()
  920. "Save parent buffer with current state source-code buffer."
  921. (interactive)
  922. (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
  923. (set-buffer-modified-p nil)
  924. (let ((edited-code (org-src--contents-for-write-back))
  925. (beg org-src--beg-marker)
  926. (end org-src--end-marker)
  927. (overlay org-src--overlay))
  928. (with-current-buffer (org-src--source-buffer)
  929. (undo-boundary)
  930. (goto-char beg)
  931. ;; Temporarily disable read-only features of OVERLAY in order to
  932. ;; insert new contents.
  933. (delete-overlay overlay)
  934. (delete-region beg end)
  935. (let ((expecting-bol (bolp)))
  936. (insert edited-code)
  937. (when (and expecting-bol (not (bolp))) (insert "\n")))
  938. (save-buffer)
  939. (move-overlay overlay beg (point))))
  940. ;; `write-contents-functions' requires the function to return
  941. ;; a non-nil value so that other functions are not called.
  942. t)
  943. (defun org-edit-src-exit ()
  944. "Kill current sub-editing buffer and return to source buffer."
  945. (interactive)
  946. (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
  947. (let* ((beg org-src--beg-marker)
  948. (end org-src--end-marker)
  949. (write-back org-src--allow-write-back)
  950. (remote org-src--remote)
  951. (coordinates (and (not remote)
  952. (org-src--coordinates (point) 1 (point-max))))
  953. (code (and write-back (org-src--contents-for-write-back))))
  954. (set-buffer-modified-p nil)
  955. ;; Switch to source buffer. Kill sub-editing buffer.
  956. (let ((edit-buffer (current-buffer))
  957. (source-buffer (marker-buffer beg)))
  958. (unless source-buffer (error "Source buffer disappeared. Aborting"))
  959. (org-src-switch-to-buffer source-buffer 'exit)
  960. (kill-buffer edit-buffer))
  961. ;; Insert modified code. Ensure it ends with a newline character.
  962. (org-with-wide-buffer
  963. (when (and write-back (not (equal (buffer-substring beg end) code)))
  964. (undo-boundary)
  965. (goto-char beg)
  966. (delete-region beg end)
  967. (let ((expecting-bol (bolp)))
  968. (insert code)
  969. (when (and expecting-bol (not (bolp))) (insert "\n")))))
  970. ;; If we are to return to source buffer, put point at an
  971. ;; appropriate location. In particular, if block is hidden, move
  972. ;; to the beginning of the block opening line.
  973. (unless remote
  974. (goto-char beg)
  975. (cond
  976. ;; Block is hidden; move at start of block.
  977. ((cl-some (lambda (o) (eq (overlay-get o 'invisible) 'org-hide-block))
  978. (overlays-at (point)))
  979. (beginning-of-line 0))
  980. (write-back (org-src--goto-coordinates coordinates beg end))))
  981. ;; Clean up left-over markers and restore window configuration.
  982. (set-marker beg nil)
  983. (set-marker end nil)
  984. (when org-src--saved-temp-window-config
  985. (set-window-configuration org-src--saved-temp-window-config)
  986. (setq org-src--saved-temp-window-config nil))))
  987. (provide 'org-src)
  988. ;;; org-src.el ends here