Merge branch 'maint'
[org-mode.git] / lisp / org-src.el
blobb0f952fae19fbdc8b769519a5df73ca6b2b63494
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)
244 (defun org-src--construct-edit-buffer-name (org-buffer-name lang)
245 "Construct the buffer name for a source editing buffer."
246 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
248 (defun org-src--edit-buffer (beg end)
249 "Return buffer editing area between BEG and END.
250 Return nil if there is no such buffer."
251 (catch 'exit
252 (dolist (b (buffer-list))
253 (with-current-buffer b
254 (and (org-src-edit-buffer-p)
255 (= beg org-src--beg-marker)
256 (eq (marker-buffer beg) (marker-buffer org-src--beg-marker))
257 (= end org-src--end-marker)
258 (eq (marker-buffer end) (marker-buffer org-src--end-marker))
259 (throw 'exit b))))))
261 (defun org-src--source-buffer ()
262 "Return source buffer edited by current buffer."
263 (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
264 (or (marker-buffer org-src--beg-marker)
265 (error "No source buffer available for current editing session")))
267 (defun org-src--get-lang-mode (lang)
268 "Return major mode that should be used for LANG.
269 LANG is a string, and the returned major mode is a symbol."
270 (intern
271 (concat
272 (let ((l (or (cdr (assoc lang org-src-lang-modes)) lang)))
273 (if (symbolp l) (symbol-name l) l))
274 "-mode")))
276 (defun org-src--coordinates (pos beg end)
277 "Return coordinates of POS relatively to BEG and END.
278 POS, BEG and END are buffer positions. Return value is either
279 a cons cell (LINE . COLUMN) or symbol `end'. See also
280 `org-src--goto-coordinates'."
281 (if (>= pos end) 'end
282 (org-with-wide-buffer
283 (goto-char (max beg pos))
284 (cons (count-lines beg (line-beginning-position))
285 ;; Column is relative to the end of line to avoid problems of
286 ;; comma escaping or colons appended in front of the line.
287 (- (current-column)
288 (progn (end-of-line) (current-column)))))))
290 (defun org-src--goto-coordinates (coord beg end)
291 "Move to coordinates COORD relatively to BEG and END.
292 COORD are coordinates, as returned by `org-src--coordinates',
293 which see. BEG and END are buffer positions."
294 (goto-char
295 (if (eq coord 'end) (max (1- end) beg)
296 ;; If BEG happens to be located outside of the narrowed part of
297 ;; the buffer, widen it first.
298 (org-with-wide-buffer
299 (goto-char beg)
300 (forward-line (car coord))
301 (end-of-line)
302 (org-move-to-column (max (+ (current-column) (cdr coord)) 0))
303 (point)))))
305 (defun org-src--contents-area (datum)
306 "Return contents boundaries of DATUM.
307 DATUM is an element or object. Return a list (BEG END CONTENTS)
308 where BEG and END are buffer positions and CONTENTS is a string."
309 (let ((type (org-element-type datum)))
310 (org-with-wide-buffer
311 (cond
312 ((eq type 'footnote-definition)
313 (let* ((beg (progn
314 (goto-char (org-element-property :post-affiliated datum))
315 (search-forward "]")))
316 (end (or (org-element-property :contents-end datum) beg)))
317 (list beg end (buffer-substring-no-properties beg end))))
318 ((eq type 'inline-src-block)
319 (let ((beg (progn (goto-char (org-element-property :begin datum))
320 (search-forward "{" (line-end-position) t)))
321 (end (progn (goto-char (org-element-property :end datum))
322 (search-backward "}" (line-beginning-position) t))))
323 (list beg end (buffer-substring-no-properties beg end))))
324 ((org-element-property :contents-begin datum)
325 (let ((beg (org-element-property :contents-begin datum))
326 (end (org-element-property :contents-end datum)))
327 (list beg end (buffer-substring-no-properties beg end))))
328 ((memq type '(example-block export-block src-block))
329 (list (progn (goto-char (org-element-property :post-affiliated datum))
330 (line-beginning-position 2))
331 (progn (goto-char (org-element-property :end datum))
332 (skip-chars-backward " \r\t\n")
333 (line-beginning-position 1))
334 (org-element-property :value datum)))
335 ((memq type '(fixed-width latex-environment table))
336 (let ((beg (org-element-property :post-affiliated datum))
337 (end (progn (goto-char (org-element-property :end datum))
338 (skip-chars-backward " \r\t\n")
339 (line-beginning-position 2))))
340 (list beg
342 (if (eq type 'fixed-width) (org-element-property :value datum)
343 (buffer-substring-no-properties beg end)))))
344 (t (error "Unsupported element or object: %s" type))))))
346 (defun org-src--make-source-overlay (beg end edit-buffer)
347 "Create overlay between BEG and END positions and return it.
348 EDIT-BUFFER is the buffer currently editing area between BEG and
349 END."
350 (let ((overlay (make-overlay beg end)))
351 (overlay-put overlay 'face 'secondary-selection)
352 (overlay-put overlay 'edit-buffer edit-buffer)
353 (overlay-put overlay 'help-echo
354 "Click with mouse-1 to switch to buffer editing this segment")
355 (overlay-put overlay 'face 'secondary-selection)
356 (overlay-put overlay 'keymap
357 (let ((map (make-sparse-keymap)))
358 (define-key map [mouse-1] 'org-edit-src-continue)
359 map))
360 (let ((read-only
361 (list
362 (lambda (&rest _)
363 (user-error
364 "Cannot modify an area being edited in a dedicated buffer")))))
365 (overlay-put overlay 'modification-hooks read-only)
366 (overlay-put overlay 'insert-in-front-hooks read-only)
367 (overlay-put overlay 'insert-behind-hooks read-only))
368 overlay))
370 (defun org-src--remove-overlay ()
371 "Remove overlay from current source buffer."
372 (when (overlayp org-src--overlay) (delete-overlay org-src--overlay)))
374 (defun org-src--on-datum-p (datum)
375 "Non-nil when point is on DATUM.
376 DATUM is an element or an object. Consider blank lines or white
377 spaces after it as being outside."
378 (and (>= (point) (org-element-property :begin datum))
379 (<= (point)
380 (org-with-wide-buffer
381 (goto-char (org-element-property :end datum))
382 (skip-chars-backward " \r\t\n")
383 (if (eq (org-element-class datum) 'element)
384 (line-end-position)
385 (point))))))
387 (defun org-src--contents-for-write-back ()
388 "Return buffer contents in a format appropriate for write back.
389 Assume point is in the corresponding edit buffer."
390 (let ((indentation (or org-src--block-indentation 0))
391 (preserve-indentation org-src--preserve-indentation)
392 (contents (org-with-wide-buffer (buffer-string)))
393 (write-back org-src--allow-write-back))
394 (with-temp-buffer
395 (insert (org-no-properties contents))
396 (goto-char (point-min))
397 (when (functionp write-back) (funcall write-back))
398 (unless (or preserve-indentation (= indentation 0))
399 (let ((ind (make-string indentation ?\s)))
400 (goto-char (point-min))
401 (while (not (eobp))
402 (when (looking-at-p "[ \t]*\\S-") (insert ind))
403 (forward-line))))
404 (buffer-string))))
406 (defun org-src--edit-element
407 (datum name &optional major write-back contents remote)
408 "Edit DATUM contents in a dedicated buffer NAME.
410 MAJOR is the major mode used in the edit buffer. A nil value is
411 equivalent to `fundamental-mode'.
413 When WRITE-BACK is non-nil, assume contents will replace original
414 region. Moreover, if it is a function, apply it in the edit
415 buffer, from point min, before returning the contents.
417 When CONTENTS is non-nil, display them in the edit buffer.
418 Otherwise, show DATUM contents as specified by
419 `org-src--contents-area'.
421 When REMOTE is non-nil, do not try to preserve point or mark when
422 moving from the edit area to the source.
424 Leave point in edit buffer."
425 (setq org-src--saved-temp-window-config (current-window-configuration))
426 (let* ((area (org-src--contents-area datum))
427 (beg (copy-marker (nth 0 area)))
428 (end (copy-marker (nth 1 area) t))
429 (old-edit-buffer (org-src--edit-buffer beg end))
430 (contents (or contents (nth 2 area))))
431 (if (and old-edit-buffer
432 (or (not org-src-ask-before-returning-to-edit-buffer)
433 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
434 ;; Move to existing buffer.
435 (org-src-switch-to-buffer old-edit-buffer 'return)
436 ;; Discard old edit buffer.
437 (when old-edit-buffer
438 (with-current-buffer old-edit-buffer (org-src--remove-overlay))
439 (kill-buffer old-edit-buffer))
440 (let* ((org-mode-p (derived-mode-p 'org-mode))
441 (type (org-element-type datum))
442 (ind (org-with-wide-buffer
443 (goto-char (org-element-property :begin datum))
444 (org-get-indentation)))
445 (preserve-ind
446 (and (memq type '(example-block src-block))
447 (or (org-element-property :preserve-indent datum)
448 org-src-preserve-indentation)))
449 ;; Store relative positions of mark (if any) and point
450 ;; within the edited area.
451 (point-coordinates (and (not remote)
452 (org-src--coordinates (point) beg end)))
453 (mark-coordinates (and (not remote)
454 (org-region-active-p)
455 (let ((m (mark)))
456 (and (>= m beg) (>= end m)
457 (org-src--coordinates m beg end)))))
458 ;; Generate a new edit buffer.
459 (buffer (generate-new-buffer name))
460 ;; Add an overlay on top of source.
461 (overlay (org-src--make-source-overlay beg end buffer)))
462 ;; Switch to edit buffer.
463 (org-src-switch-to-buffer buffer 'edit)
464 ;; Insert contents.
465 (insert contents)
466 (remove-text-properties (point-min) (point-max)
467 '(display nil invisible nil intangible nil))
468 (unless preserve-ind (org-do-remove-indentation))
469 (set-buffer-modified-p nil)
470 (setq buffer-file-name nil)
471 ;; Start major mode.
472 (if (not major) (fundamental-mode)
473 (let ((org-inhibit-startup t))
474 (condition-case e (funcall major)
475 (error (message "Language mode `%s' fails with: %S"
476 major (nth 1 e))))))
477 ;; Transmit buffer-local variables for exit function. It must
478 ;; be done after initializing major mode, as this operation
479 ;; may reset them otherwise.
480 (setq-local org-src--from-org-mode org-mode-p)
481 (setq-local org-src--beg-marker beg)
482 (setq-local org-src--end-marker end)
483 (setq-local org-src--remote remote)
484 (setq-local org-src--block-indentation ind)
485 (setq-local org-src--preserve-indentation preserve-ind)
486 (setq-local org-src--overlay overlay)
487 (setq-local org-src--allow-write-back write-back)
488 ;; Start minor mode.
489 (org-src-mode)
490 ;; Move mark and point in edit buffer to the corresponding
491 ;; location.
492 (if remote
493 (progn
494 ;; Put point at first non read-only character after
495 ;; leading blank.
496 (goto-char
497 (or (text-property-any (point-min) (point-max) 'read-only nil)
498 (point-max)))
499 (skip-chars-forward " \r\t\n"))
500 ;; Set mark and point.
501 (when mark-coordinates
502 (org-src--goto-coordinates mark-coordinates (point-min) (point-max))
503 (push-mark (point) 'no-message t)
504 (setq deactivate-mark nil))
505 (org-src--goto-coordinates
506 point-coordinates (point-min) (point-max)))))))
510 ;;; Fontification of source blocks
512 (defun org-src-font-lock-fontify-block (lang start end)
513 "Fontify code block.
514 This function is called by emacs automatic fontification, as long
515 as `org-src-fontify-natively' is non-nil."
516 (let ((lang-mode (org-src--get-lang-mode lang)))
517 (when (fboundp lang-mode)
518 (let ((string (buffer-substring-no-properties start end))
519 (modified (buffer-modified-p))
520 (org-buffer (current-buffer)))
521 (remove-text-properties start end '(face nil))
522 (with-current-buffer
523 (get-buffer-create
524 (format " *org-src-fontification:%s*" lang-mode))
525 (let ((inhibit-modification-hooks nil))
526 (erase-buffer)
527 ;; Add string and a final space to ensure property change.
528 (insert string " "))
529 (unless (eq major-mode lang-mode) (funcall lang-mode))
530 (org-font-lock-ensure)
531 (let ((pos (point-min)) next)
532 (while (setq next (next-property-change pos))
533 ;; Handle additional properties from font-lock, so as to
534 ;; preserve, e.g., composition.
535 (dolist (prop (cons 'face font-lock-extra-managed-props))
536 (let ((new-prop (get-text-property pos prop)))
537 (put-text-property
538 (+ start (1- pos)) (1- (+ start next)) prop new-prop
539 org-buffer)))
540 (setq pos next))))
541 ;; Add Org faces.
542 (let ((src-face (nth 1 (assoc-string lang org-src-block-faces t))))
543 (when (or (facep src-face) (listp src-face))
544 (font-lock-append-text-property start end 'face src-face))
545 (font-lock-append-text-property start end 'face 'org-block))
546 (add-text-properties
547 start end
548 '(font-lock-fontified t fontified t font-lock-multiline t))
549 (set-buffer-modified-p modified)))))
552 ;;; Escape contents
554 (defun org-escape-code-in-region (beg end)
555 "Escape lines between BEG and END.
556 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
557 \",#+\" by appending a comma to it."
558 (interactive "r")
559 (save-excursion
560 (goto-char end)
561 (while (re-search-backward "^[ \t]*,?\\(\\*\\|#\\+\\)" beg t)
562 (save-excursion (replace-match ",\\1" nil nil nil 1)))))
564 (defun org-escape-code-in-string (s)
565 "Escape lines in string S.
566 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
567 \",#+\" by appending a comma to it."
568 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil 1))
570 (defun org-unescape-code-in-region (beg end)
571 "Un-escape lines between BEG and END.
572 Un-escaping happens by removing the first comma on lines starting
573 with \",*\", \",#+\", \",,*\" and \",,#+\"."
574 (interactive "r")
575 (save-excursion
576 (goto-char end)
577 (while (re-search-backward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" beg t)
578 (save-excursion (replace-match "" nil nil nil 1)))))
580 (defun org-unescape-code-in-string (s)
581 "Un-escape lines in string S.
582 Un-escaping happens by removing the first comma on lines starting
583 with \",*\", \",#+\", \",,*\" and \",,#+\"."
584 (replace-regexp-in-string
585 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1))
589 ;;; Org src minor mode
591 (defvar org-src-mode-map
592 (let ((map (make-sparse-keymap)))
593 (define-key map "\C-c'" 'org-edit-src-exit)
594 (define-key map "\C-c\C-k" 'org-edit-src-abort)
595 (define-key map "\C-x\C-s" 'org-edit-src-save)
596 map))
598 (define-minor-mode org-src-mode
599 "Minor mode for language major mode buffers generated by Org.
600 \\<org-mode-map>
601 This minor mode is turned on in two situations:
602 - when editing a source code snippet with `\\[org-edit-special]'
603 - when formatting a source code snippet for export with htmlize.
605 \\{org-src-mode-map}
607 See also `org-src-mode-hook'."
608 nil " OrgSrc" nil
609 (when org-edit-src-persistent-message
610 (setq-local
611 header-line-format
612 (substitute-command-keys
613 (if org-src--allow-write-back
614 "Edit, then exit with `\\[org-edit-src-exit]' or abort with \
615 `\\[org-edit-src-abort]'"
616 "Exit with `\\[org-edit-src-exit]' or abort with \
617 `\\[org-edit-src-abort]'"))))
618 ;; Possibly activate various auto-save features (for the edit buffer
619 ;; or the source buffer).
620 (when org-edit-src-turn-on-auto-save
621 (setq buffer-auto-save-file-name
622 (concat (make-temp-name "org-src-")
623 (format-time-string "-%Y-%d-%m")
624 ".txt")))
625 (unless (or org-src--auto-save-timer (zerop org-edit-src-auto-save-idle-delay))
626 (setq org-src--auto-save-timer
627 (run-with-idle-timer
628 org-edit-src-auto-save-idle-delay t
629 (lambda ()
630 (save-excursion
631 (let (edit-flag)
632 (dolist (b (buffer-list))
633 (with-current-buffer b
634 (when (org-src-edit-buffer-p)
635 (unless edit-flag (setq edit-flag t))
636 (when (buffer-modified-p) (org-edit-src-save)))))
637 (unless edit-flag
638 (cancel-timer org-src--auto-save-timer)
639 (setq org-src--auto-save-timer nil)))))))))
641 (defun org-src-mode-configure-edit-buffer ()
642 (when (bound-and-true-p org-src--from-org-mode)
643 (add-hook 'kill-buffer-hook #'org-src--remove-overlay nil 'local)
644 (if (bound-and-true-p org-src--allow-write-back)
645 (progn
646 (setq buffer-offer-save t)
647 (setq buffer-file-name
648 (concat (buffer-file-name (marker-buffer org-src--beg-marker))
649 "[" (buffer-name) "]"))
650 (setq-local write-contents-functions '(org-edit-src-save)))
651 (setq buffer-read-only t))))
653 (add-hook 'org-src-mode-hook #'org-src-mode-configure-edit-buffer)
657 ;;; Babel related functions
659 (defun org-src-associate-babel-session (info)
660 "Associate edit buffer with comint session."
661 (interactive)
662 (let ((session (cdr (assq :session (nth 2 info)))))
663 (and session (not (string= session "none"))
664 (org-babel-comint-buffer-livep session)
665 (let ((f (intern (format "org-babel-%s-associate-session"
666 (nth 0 info)))))
667 (and (fboundp f) (funcall f session))))))
669 (defun org-src-babel-configure-edit-buffer ()
670 (when org-src--babel-info
671 (org-src-associate-babel-session org-src--babel-info)))
673 (add-hook 'org-src-mode-hook #'org-src-babel-configure-edit-buffer)
676 ;;; Public API
678 (defmacro org-src-do-at-code-block (&rest body)
679 "Execute BODY from an edit buffer in the Org mode buffer."
680 (declare (debug (body)))
681 `(let ((beg-marker org-src--beg-marker))
682 (when beg-marker
683 (with-current-buffer (marker-buffer beg-marker)
684 (goto-char beg-marker)
685 ,@body))))
687 (defun org-src-do-key-sequence-at-code-block (&optional key)
688 "Execute key sequence at code block in the source Org buffer.
689 The command bound to KEY in the Org-babel key map is executed
690 remotely with point temporarily at the start of the code block in
691 the Org buffer.
693 This command is not bound to a key by default, to avoid conflicts
694 with language major mode bindings. To bind it to C-c @ in all
695 language major modes, you could use
697 (add-hook \\='org-src-mode-hook
698 (lambda () (define-key org-src-mode-map \"\\C-c@\"
699 \\='org-src-do-key-sequence-at-code-block)))
701 In that case, for example, C-c @ t issued in code edit buffers
702 would tangle the current Org code block, C-c @ e would execute
703 the block and C-c @ h would display the other available
704 Org-babel commands."
705 (interactive "kOrg-babel key: ")
706 (if (equal key (kbd "C-g")) (keyboard-quit)
707 (org-edit-src-save)
708 (org-src-do-at-code-block
709 (call-interactively (lookup-key org-babel-map key)))))
711 (defun org-src-edit-buffer-p (&optional buffer)
712 "Non-nil when current buffer is a source editing buffer.
713 If BUFFER is non-nil, test it instead."
714 (let ((buffer (org-base-buffer (or buffer (current-buffer)))))
715 (and (buffer-live-p buffer)
716 (local-variable-p 'org-src--beg-marker buffer)
717 (local-variable-p 'org-src--end-marker buffer))))
719 (defun org-src-switch-to-buffer (buffer context)
720 (pcase org-src-window-setup
721 (`current-window (pop-to-buffer-same-window buffer))
722 (`other-window
723 (switch-to-buffer-other-window buffer))
724 (`other-frame
725 (pcase context
726 (`exit
727 (let ((frame (selected-frame)))
728 (switch-to-buffer-other-frame buffer)
729 (delete-frame frame)))
730 (`save
731 (kill-buffer (current-buffer))
732 (pop-to-buffer-same-window buffer))
733 (_ (switch-to-buffer-other-frame buffer))))
734 (`reorganize-frame
735 (when (eq context 'edit) (delete-other-windows))
736 (org-switch-to-buffer-other-window buffer)
737 (when (eq context 'exit) (delete-other-windows)))
738 (`switch-invisibly (set-buffer buffer))
740 (message "Invalid value %s for `org-src-window-setup'"
741 org-src-window-setup)
742 (pop-to-buffer-same-window buffer))))
744 (defun org-src-coderef-format (&optional element)
745 "Return format string for block at point.
747 When optional argument ELEMENT is provided, use that block.
748 Otherwise, assume point is either at a source block, at an
749 example block.
751 If point is in an edit buffer, retrieve format string associated
752 to the remote source block."
753 (cond
754 ((and element (org-element-property :label-fmt element)))
755 ((org-src-edit-buffer-p) (org-src-do-at-code-block (org-src-coderef-format)))
756 ((org-element-property :label-fmt (org-element-at-point)))
757 (t org-coderef-label-format)))
759 (defun org-src-coderef-regexp (fmt &optional label)
760 "Return regexp matching a coderef format string FMT.
762 When optional argument LABEL is non-nil, match coderef for that
763 label only.
765 Match group 1 contains the full coderef string with surrounding
766 white spaces. Match group 2 contains the same string without any
767 surrounding space. Match group 3 contains the label.
769 A coderef format regexp can only match at the end of a line."
770 (format "\\([ \t]*\\(%s\\)[ \t]*\\)$"
771 (replace-regexp-in-string
772 "%s"
773 (if label (regexp-quote label) "\\([-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\)")
774 (regexp-quote fmt)
775 nil t)))
777 (defun org-edit-footnote-reference ()
778 "Edit definition of footnote reference at point."
779 (interactive)
780 (let* ((context (org-element-context))
781 (label (org-element-property :label context)))
782 (unless (and (eq (org-element-type context) 'footnote-reference)
783 (org-src--on-datum-p context))
784 (user-error "Not on a footnote reference"))
785 (unless label (user-error "Cannot edit remotely anonymous footnotes"))
786 (let* ((definition (org-with-wide-buffer
787 (org-footnote-goto-definition label)
788 (backward-char)
789 (org-element-context)))
790 (inline (eq (org-element-type definition) 'footnote-reference))
791 (contents
792 (let ((c (org-with-wide-buffer
793 (org-trim (buffer-substring-no-properties
794 (org-element-property :begin definition)
795 (org-element-property :end definition))))))
796 (add-text-properties
798 (progn (string-match (if inline "\\`\\[fn:.*?:" "\\`.*?\\]") c)
799 (match-end 0))
800 '(read-only "Cannot edit footnote label" front-sticky t
801 rear-nonsticky t)
803 (when inline
804 (let ((l (length c)))
805 (add-text-properties
806 (1- l) l
807 '(read-only "Cannot edit past footnote reference"
808 front-sticky nil rear-nonsticky nil)
809 c)))
810 c)))
811 (org-src--edit-element
812 definition
813 (format "*Edit footnote [%s]*" label)
814 #'org-mode
815 `(lambda ()
816 (if ,(not inline) (delete-region (point) (search-forward "]"))
817 (delete-region (point) (search-forward ":" nil t 2))
818 (delete-region (1- (point-max)) (point-max))
819 (when (re-search-forward "\n[ \t]*\n" nil t)
820 (user-error "Inline definitions cannot contain blank lines"))
821 ;; If footnote reference belongs to a table, make sure to
822 ;; remove any newline characters in order to preserve
823 ;; table's structure.
824 (when ,(org-element-lineage definition '(table-cell))
825 (while (search-forward "\n" nil t) (delete-char -1)))))
826 (concat contents
827 (and (not (org-element-property :contents-begin definition))
828 " "))
829 'remote))
830 ;; Report success.
833 (defun org-edit-table.el ()
834 "Edit \"table.el\" table at point.
835 \\<org-src-mode-map>
836 A new buffer is created and the table is copied into it. Then
837 the table is recognized with `table-recognize'. When done
838 editing, exit with `\\[org-edit-src-exit]'. The edited text will \
839 then replace
840 the area in the Org mode buffer.
842 Throw an error when not at such a table."
843 (interactive)
844 (let ((element (org-element-at-point)))
845 (unless (and (eq (org-element-type element) 'table)
846 (eq (org-element-property :type element) 'table.el)
847 (org-src--on-datum-p element))
848 (user-error "Not in a table.el table"))
849 (org-src--edit-element
850 element
851 (org-src--construct-edit-buffer-name (buffer-name) "Table")
852 #'text-mode t)
853 (when (bound-and-true-p flyspell-mode) (flyspell-mode -1))
854 (table-recognize)
857 (defun org-edit-latex-environment ()
858 "Edit LaTeX environment at point.
859 \\<org-src-mode-map>
860 The LaTeX environment is copied into a new buffer. Major mode is
861 set to the one associated to \"latex\" in `org-src-lang-modes',
862 or to `latex-mode' if there is none.
864 When done, exit with `\\[org-edit-src-exit]'. The edited text \
865 will then replace
866 the LaTeX environment in the Org mode buffer."
867 (interactive)
868 (let ((element (org-element-at-point)))
869 (unless (and (eq (org-element-type element) 'latex-environment)
870 (org-src--on-datum-p element))
871 (user-error "Not in a LaTeX environment"))
872 (org-src--edit-element
873 element
874 (org-src--construct-edit-buffer-name (buffer-name) "LaTeX environment")
875 (org-src--get-lang-mode "latex")
879 (defun org-edit-export-block ()
880 "Edit export block at point.
881 \\<org-src-mode-map>
882 A new buffer is created and the block is copied into it, and the
883 buffer is switched into an appropriate major mode. See also
884 `org-src-lang-modes'.
886 When done, exit with `\\[org-edit-src-exit]'. The edited text \
887 will then replace
888 the area in the Org mode buffer.
890 Throw an error when not at an export block."
891 (interactive)
892 (let ((element (org-element-at-point)))
893 (unless (and (eq (org-element-type element) 'export-block)
894 (org-src--on-datum-p element))
895 (user-error "Not in an export block"))
896 (let* ((type (downcase (org-element-property :type element)))
897 (mode (org-src--get-lang-mode type)))
898 (unless (functionp mode) (error "No such language mode: %s" mode))
899 (org-src--edit-element
900 element
901 (org-src--construct-edit-buffer-name (buffer-name) type)
902 mode
903 (lambda () (org-escape-code-in-region (point-min) (point-max)))))
906 (defun org-edit-src-code (&optional code edit-buffer-name)
907 "Edit the source or example block at point.
908 \\<org-src-mode-map>
909 The code is copied to a separate buffer and the appropriate mode
910 is turned on. When done, exit with `\\[org-edit-src-exit]'. This \
911 will remove the
912 original code in the Org buffer, and replace it with the edited
913 version. See `org-src-window-setup' to configure the display of
914 windows containing the Org buffer and the code buffer.
916 When optional argument CODE is a string, edit it in a dedicated
917 buffer instead.
919 When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
920 name of the sub-editing buffer."
921 (interactive)
922 (let* ((element (org-element-at-point))
923 (type (org-element-type element)))
924 (unless (and (memq type '(example-block src-block))
925 (org-src--on-datum-p element))
926 (user-error "Not in a source or example block"))
927 (let* ((lang
928 (if (eq type 'src-block) (org-element-property :language element)
929 "example"))
930 (lang-f (and (eq type 'src-block) (org-src--get-lang-mode lang)))
931 (babel-info (and (eq type 'src-block)
932 (org-babel-get-src-block-info 'light)))
933 deactivate-mark)
934 (when (and (eq type 'src-block) (not (functionp lang-f)))
935 (error "No such language mode: %s" lang-f))
936 (org-src--edit-element
937 element
938 (or edit-buffer-name
939 (org-src--construct-edit-buffer-name (buffer-name) lang))
940 lang-f
941 (and (null code)
942 `(lambda ()
943 (unless ,(or org-src-preserve-indentation
944 (org-element-property :preserve-indent element))
945 (when (> org-edit-src-content-indentation 0)
946 (while (not (eobp))
947 (unless (looking-at "[ \t]*$")
948 (indent-line-to (+ (org-get-indentation)
949 org-edit-src-content-indentation)))
950 (forward-line))))
951 (org-escape-code-in-region (point-min) (point-max))))
952 (and code (org-unescape-code-in-string code)))
953 ;; Finalize buffer.
954 (setq-local org-coderef-label-format
955 (or (org-element-property :label-fmt element)
956 org-coderef-label-format))
957 (when (eq type 'src-block)
958 (setq-local org-src--babel-info babel-info)
959 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
960 (when (fboundp edit-prep-func)
961 (funcall edit-prep-func babel-info))))
962 t)))
964 (defun org-edit-inline-src-code ()
965 "Edit inline source code at point."
966 (interactive)
967 (let ((context (org-element-context)))
968 (unless (and (eq (org-element-type context) 'inline-src-block)
969 (org-src--on-datum-p context))
970 (user-error "Not on inline source code"))
971 (let* ((lang (org-element-property :language context))
972 (lang-f (org-src--get-lang-mode lang))
973 (babel-info (org-babel-get-src-block-info 'light))
974 deactivate-mark)
975 (unless (functionp lang-f) (error "No such language mode: %s" lang-f))
976 (org-src--edit-element
977 context
978 (org-src--construct-edit-buffer-name (buffer-name) lang)
979 lang-f
980 (lambda ()
981 ;; Inline src blocks are limited to one line.
982 (while (re-search-forward "\n[ \t]*" nil t) (replace-match " "))
983 ;; Trim contents.
984 (goto-char (point-min))
985 (skip-chars-forward " \t")
986 (delete-region (point-min) (point))
987 (goto-char (point-max))
988 (skip-chars-backward " \t")
989 (delete-region (point) (point-max))))
990 ;; Finalize buffer.
991 (setq-local org-src--babel-info babel-info)
992 (setq-local org-src--preserve-indentation t)
993 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
994 (when (fboundp edit-prep-func) (funcall edit-prep-func babel-info)))
995 ;; Return success.
996 t)))
998 (defun org-edit-fixed-width-region ()
999 "Edit the fixed-width ASCII drawing at point.
1000 \\<org-src-mode-map>
1001 This must be a region where each line starts with a colon
1002 followed by a space or a newline character.
1004 A new buffer is created and the fixed-width region is copied into
1005 it, and the buffer is switched into the major mode defined in
1006 `org-edit-fixed-width-region-mode', which see.
1008 When done, exit with `\\[org-edit-src-exit]'. The edited text \
1009 will then replace
1010 the area in the Org mode buffer."
1011 (interactive)
1012 (let ((element (org-element-at-point)))
1013 (unless (and (eq (org-element-type element) 'fixed-width)
1014 (org-src--on-datum-p element))
1015 (user-error "Not in a fixed-width area"))
1016 (org-src--edit-element
1017 element
1018 (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
1019 org-edit-fixed-width-region-mode
1020 (lambda () (while (not (eobp)) (insert ": ") (forward-line))))
1021 ;; Return success.
1024 (defun org-edit-src-abort ()
1025 "Abort editing of the src code and return to the Org buffer."
1026 (interactive)
1027 (let (org-src--allow-write-back) (org-edit-src-exit)))
1029 (defun org-edit-src-continue (e)
1030 "Unconditionally return to buffer editing area under point.
1031 Throw an error if there is no such buffer."
1032 (interactive "e")
1033 (mouse-set-point e)
1034 (let ((buf (get-char-property (point) 'edit-buffer)))
1035 (if buf (org-src-switch-to-buffer buf 'continue)
1036 (user-error "No sub-editing buffer for area at point"))))
1038 (defun org-edit-src-save ()
1039 "Save parent buffer with current state source-code buffer."
1040 (interactive)
1041 (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
1042 (set-buffer-modified-p nil)
1043 (let ((edited-code (org-src--contents-for-write-back))
1044 (beg org-src--beg-marker)
1045 (end org-src--end-marker)
1046 (overlay org-src--overlay))
1047 (with-current-buffer (org-src--source-buffer)
1048 (undo-boundary)
1049 (goto-char beg)
1050 ;; Temporarily disable read-only features of OVERLAY in order to
1051 ;; insert new contents.
1052 (delete-overlay overlay)
1053 (delete-region beg end)
1054 (let ((expecting-bol (bolp)))
1055 (insert edited-code)
1056 (when (and expecting-bol (not (bolp))) (insert "\n")))
1057 (save-buffer)
1058 (move-overlay overlay beg (point))))
1059 ;; `write-contents-functions' requires the function to return
1060 ;; a non-nil value so that other functions are not called.
1063 (defun org-edit-src-exit ()
1064 "Kill current sub-editing buffer and return to source buffer."
1065 (interactive)
1066 (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
1067 (let* ((beg org-src--beg-marker)
1068 (end org-src--end-marker)
1069 (write-back org-src--allow-write-back)
1070 (remote org-src--remote)
1071 (coordinates (and (not remote)
1072 (org-src--coordinates (point) 1 (point-max))))
1073 (code (and write-back (org-src--contents-for-write-back))))
1074 (set-buffer-modified-p nil)
1075 ;; Switch to source buffer. Kill sub-editing buffer.
1076 (let ((edit-buffer (current-buffer)))
1077 (org-src-switch-to-buffer (marker-buffer beg) 'exit)
1078 (kill-buffer edit-buffer))
1079 ;; Insert modified code. Ensure it ends with a newline character.
1080 (org-with-wide-buffer
1081 (when (and write-back (not (equal (buffer-substring beg end) code)))
1082 (undo-boundary)
1083 (goto-char beg)
1084 (delete-region beg end)
1085 (let ((expecting-bol (bolp)))
1086 (insert code)
1087 (when (and expecting-bol (not (bolp))) (insert "\n")))))
1088 ;; If we are to return to source buffer, put point at an
1089 ;; appropriate location. In particular, if block is hidden, move
1090 ;; to the beginning of the block opening line.
1091 (unless remote
1092 (goto-char beg)
1093 (cond
1094 ;; Block is hidden; move at start of block.
1095 ((cl-some (lambda (o) (eq (overlay-get o 'invisible) 'org-hide-block))
1096 (overlays-at (point)))
1097 (beginning-of-line 0))
1098 (write-back (org-src--goto-coordinates coordinates beg end))))
1099 ;; Clean up left-over markers and restore window configuration.
1100 (set-marker beg nil)
1101 (set-marker end nil)
1102 (when org-src--saved-temp-window-config
1103 (set-window-configuration org-src--saved-temp-window-config)
1104 (setq org-src--saved-temp-window-config nil))))
1107 (provide 'org-src)
1109 ;;; org-src.el ends here