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