1 ;;; org-src.el --- Source code examples in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the code dealing with source code examples in
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
66 :package-version
'(Org .
"8.0")
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
78 :package-version
'(Org .
"8.0")
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))\"
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
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
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.
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
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
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
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
148 :package-version
'(Org .
"8.0")
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
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
165 (const current-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.
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
193 :group
'org-edit-structure
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
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")
217 (sexp :tag
"Anonymous face"))))
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."
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
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."
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
))
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."
278 (let ((l (or (cdr (assoc lang org-src-lang-modes
)) lang
)))
279 (if (symbolp l
) (symbol-name l
) l
))
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.
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."
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
306 (forward-line (car coord
))
308 (org-move-to-column (max (+ (current-column) (cdr coord
)) 0))
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
318 ((eq type
'footnote-definition
)
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 latex-environment 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))))
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
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
)
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
))
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
))
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
)
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
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
))
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)
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
))))
426 (defun org-src--edit-element
427 (datum name
&optional initialize write-back contents remote
)
428 "Edit DATUM contents in a dedicated buffer NAME.
430 INITIALIZE is a function to call upon creating the buffer.
432 When WRITE-BACK is non-nil, assume contents will replace original
433 region. Moreover, if it is a function, apply it in the edit
434 buffer, from point min, before returning the contents.
436 When CONTENTS is non-nil, display them in the edit buffer.
437 Otherwise, show DATUM contents as specified by
438 `org-src--contents-area'.
440 When REMOTE is non-nil, do not try to preserve point or mark when
441 moving from the edit area to the source.
443 Leave point in edit buffer."
444 (setq org-src--saved-temp-window-config
(current-window-configuration))
445 (let* ((area (org-src--contents-area datum
))
446 (beg (copy-marker (nth 0 area
)))
447 (end (copy-marker (nth 1 area
) t
))
448 (old-edit-buffer (org-src--edit-buffer beg end
))
449 (contents (or contents
(nth 2 area
))))
450 (if (and old-edit-buffer
451 (or (not org-src-ask-before-returning-to-edit-buffer
)
452 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
453 ;; Move to existing buffer.
454 (org-src-switch-to-buffer old-edit-buffer
'return
)
455 ;; Discard old edit buffer.
456 (when old-edit-buffer
457 (with-current-buffer old-edit-buffer
(org-src--remove-overlay))
458 (kill-buffer old-edit-buffer
))
459 (let* ((org-mode-p (derived-mode-p 'org-mode
))
460 (source-tab-width (if indent-tabs-mode tab-width
0))
461 (type (org-element-type datum
))
462 (ind (org-with-wide-buffer
463 (goto-char (org-element-property :begin datum
))
464 (org-get-indentation)))
466 (and (memq type
'(example-block src-block
))
467 (or (org-element-property :preserve-indent datum
)
468 org-src-preserve-indentation
)))
469 ;; Store relative positions of mark (if any) and point
470 ;; within the edited area.
471 (point-coordinates (and (not remote
)
472 (org-src--coordinates (point) beg end
)))
473 (mark-coordinates (and (not remote
)
474 (org-region-active-p)
476 (and (>= m beg
) (>= end m
)
477 (org-src--coordinates m beg end
)))))
478 ;; Generate a new edit buffer.
479 (buffer (generate-new-buffer name
))
480 ;; Add an overlay on top of source.
481 (overlay (org-src--make-source-overlay beg end buffer
)))
482 ;; Switch to edit buffer.
483 (org-src-switch-to-buffer buffer
'edit
)
486 (remove-text-properties (point-min) (point-max)
487 '(display nil invisible nil intangible nil
))
488 (unless preserve-ind
(org-do-remove-indentation))
489 (set-buffer-modified-p nil
)
490 (setq buffer-file-name nil
)
491 ;; Initialize buffer.
492 (when (functionp initialize
)
493 (let ((org-inhibit-startup t
))
496 (error (message "Initialization fails with: %S"
497 (error-message-string 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
)
513 ;; Move mark and point in edit buffer to the corresponding
517 ;; Put point at first non read-only character after
520 (or (text-property-any (point-min) (point-max) 'read-only nil
)
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
)
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
))
547 (format " *org-src-fontification:%s*" lang-mode
))
548 (let ((inhibit-modification-hooks nil
))
550 ;; Add string and a final space to ensure property change.
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
)))
561 (+ start
(1- pos
)) (1- (+ start next
)) prop new-prop
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
))
571 '(font-lock-fontified t fontified t font-lock-multiline t
))
572 (set-buffer-modified-p modified
)))))
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."
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 \",,#+\"."
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
)
621 (define-minor-mode org-src-mode
622 "Minor mode for language major mode buffers generated by Org.
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.
630 See also `org-src-mode-hook'."
632 (when org-edit-src-persistent-message
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")
648 (unless (or org-src--auto-save-timer
(zerop org-edit-src-auto-save-idle-delay
))
649 (setq org-src--auto-save-timer
651 org-edit-src-auto-save-idle-delay t
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)))))
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
)
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."
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"
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
)
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
))
706 (with-current-buffer (marker-buffer beg-marker
)
707 (goto-char beg-marker
)
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
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
728 (interactive "kOrg-babel key: ")
729 (if (equal key
(kbd "C-g")) (keyboard-quit)
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
))
746 (switch-to-buffer-other-window buffer
))
750 (let ((frame (selected-frame)))
751 (switch-to-buffer-other-frame buffer
)
752 (delete-frame frame
)))
754 (kill-buffer (current-buffer))
755 (pop-to-buffer-same-window buffer
))
756 (_ (switch-to-buffer-other-frame buffer
))))
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
774 If point is in an edit buffer, retrieve format string associated
775 to the remote source block."
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
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
796 (if label
(regexp-quote label
) "\\([-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\)")
800 (defun org-edit-footnote-reference ()
801 "Edit definition of footnote reference at point."
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
)
812 (org-element-context)))
813 (inline?
(eq 'footnote-reference
(org-element-type definition
)))
815 (org-with-wide-buffer
816 (buffer-substring-no-properties
817 (or (org-element-property :post-affiliated definition
)
818 (org-element-property :begin definition
))
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)))))))
826 (progn (string-match (if inline?
"\\`\\[fn:.*?:" "\\`.*?\\]") contents
)
828 '(read-only "Cannot edit footnote label" front-sticky t rear-nonsticky t
)
831 (let ((l (length contents
)))
834 '(read-only "Cannot edit past footnote reference"
835 front-sticky nil rear-nonsticky nil
)
837 (org-src--edit-element
839 (format "*Edit footnote [%s]*" label
)
840 (let ((source (current-buffer)))
843 (org-clone-local-variables source
)))
845 (if (not inline?
) (delete-region (point) (search-forward "]"))
846 (delete-region (point) (search-forward ":" nil t
2))
847 (delete-region (1- (point-max)) (point-max))
848 (when (re-search-forward "\n[ \t]*\n" nil t
)
849 (user-error "Inline definitions cannot contain blank lines"))
850 ;; If footnote reference belongs to a table, make sure to
851 ;; remove any newline characters in order to preserve
852 ;; table's structure.
853 (when (org-element-lineage definition
'(table-cell))
854 (while (search-forward "\n" nil t
) (replace-match "")))))
860 (defun org-edit-table.el
()
861 "Edit \"table.el\" table at point.
863 A new buffer is created and the table is copied into it. Then
864 the table is recognized with `table-recognize'. When done
865 editing, exit with `\\[org-edit-src-exit]'. The edited text will \
867 the area in the Org mode buffer.
869 Throw an error when not at such a table."
871 (let ((element (org-element-at-point)))
872 (unless (and (eq (org-element-type element
) 'table
)
873 (eq (org-element-property :type element
) 'table.el
)
874 (org-src--on-datum-p element
))
875 (user-error "Not in a table.el table"))
876 (org-src--edit-element
878 (org-src--construct-edit-buffer-name (buffer-name) "Table")
880 (when (bound-and-true-p flyspell-mode
) (flyspell-mode -
1))
884 (defun org-edit-latex-environment ()
885 "Edit LaTeX environment at point.
887 The LaTeX environment is copied into a new buffer. Major mode is
888 set to the one associated to \"latex\" in `org-src-lang-modes',
889 or to `latex-mode' if there is none.
891 When done, exit with `\\[org-edit-src-exit]'. The edited text \
893 the LaTeX environment in the Org mode buffer."
895 (let ((element (org-element-at-point)))
896 (unless (and (eq (org-element-type element
) 'latex-environment
)
897 (org-src--on-datum-p element
))
898 (user-error "Not in a LaTeX environment"))
899 (org-src--edit-element
901 (org-src--construct-edit-buffer-name (buffer-name) "LaTeX environment")
902 (org-src--get-lang-mode "latex")
906 (defun org-edit-export-block ()
907 "Edit export block at point.
909 A new buffer is created and the block is copied into it, and the
910 buffer is switched into an appropriate major mode. See also
911 `org-src-lang-modes'.
913 When done, exit with `\\[org-edit-src-exit]'. The edited text \
915 the area in the Org mode buffer.
917 Throw an error when not at an export block."
919 (let ((element (org-element-at-point)))
920 (unless (and (eq (org-element-type element
) 'export-block
)
921 (org-src--on-datum-p element
))
922 (user-error "Not in an export block"))
923 (let* ((type (downcase (org-element-property :type element
)))
924 (mode (org-src--get-lang-mode type
)))
925 (unless (functionp mode
) (error "No such language mode: %s" mode
))
926 (org-src--edit-element
928 (org-src--construct-edit-buffer-name (buffer-name) type
)
930 (lambda () (org-escape-code-in-region (point-min) (point-max)))))
933 (defun org-edit-src-code (&optional code edit-buffer-name
)
934 "Edit the source or example block at point.
936 The code is copied to a separate buffer and the appropriate mode
937 is turned on. When done, exit with `\\[org-edit-src-exit]'. This \
939 original code in the Org buffer, and replace it with the edited
940 version. See `org-src-window-setup' to configure the display of
941 windows containing the Org buffer and the code buffer.
943 When optional argument CODE is a string, edit it in a dedicated
946 When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
947 name of the sub-editing buffer."
949 (let* ((element (org-element-at-point))
950 (type (org-element-type element
)))
951 (unless (and (memq type
'(example-block src-block
))
952 (org-src--on-datum-p element
))
953 (user-error "Not in a source or example block"))
955 (if (eq type
'src-block
) (org-element-property :language element
)
957 (lang-f (and (eq type
'src-block
) (org-src--get-lang-mode lang
)))
958 (babel-info (and (eq type
'src-block
)
959 (org-babel-get-src-block-info 'light
)))
961 (when (and (eq type
'src-block
) (not (functionp lang-f
)))
962 (error "No such language mode: %s" lang-f
))
963 (org-src--edit-element
966 (org-src--construct-edit-buffer-name (buffer-name) lang
))
969 (lambda () (org-escape-code-in-region (point-min) (point-max))))
970 (and code
(org-unescape-code-in-string code
)))
972 (setq-local org-coderef-label-format
973 (or (org-element-property :label-fmt element
)
974 org-coderef-label-format
))
975 (when (eq type
'src-block
)
976 (setq-local org-src--babel-info babel-info
)
977 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
978 (when (fboundp edit-prep-func
)
979 (funcall edit-prep-func babel-info
))))
982 (defun org-edit-inline-src-code ()
983 "Edit inline source code at point."
985 (let ((context (org-element-context)))
986 (unless (and (eq (org-element-type context
) 'inline-src-block
)
987 (org-src--on-datum-p context
))
988 (user-error "Not on inline source code"))
989 (let* ((lang (org-element-property :language context
))
990 (lang-f (org-src--get-lang-mode lang
))
991 (babel-info (org-babel-get-src-block-info 'light
))
993 (unless (functionp lang-f
) (error "No such language mode: %s" lang-f
))
994 (org-src--edit-element
996 (org-src--construct-edit-buffer-name (buffer-name) lang
)
999 ;; Inline src blocks are limited to one line.
1000 (while (re-search-forward "\n[ \t]*" nil t
) (replace-match " "))
1002 (goto-char (point-min))
1003 (skip-chars-forward " \t")
1004 (delete-region (point-min) (point))
1005 (goto-char (point-max))
1006 (skip-chars-backward " \t")
1007 (delete-region (point) (point-max))))
1009 (setq-local org-src--babel-info babel-info
)
1010 (setq-local org-src--preserve-indentation t
)
1011 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
1012 (when (fboundp edit-prep-func
) (funcall edit-prep-func babel-info
)))
1016 (defun org-edit-fixed-width-region ()
1017 "Edit the fixed-width ASCII drawing at point.
1018 \\<org-src-mode-map>
1019 This must be a region where each line starts with a colon
1020 followed by a space or a newline character.
1022 A new buffer is created and the fixed-width region is copied into
1023 it, and the buffer is switched into the major mode defined in
1024 `org-edit-fixed-width-region-mode', which see.
1026 When done, exit with `\\[org-edit-src-exit]'. The edited text \
1028 the area in the Org mode buffer."
1030 (let ((element (org-element-at-point)))
1031 (unless (and (eq (org-element-type element
) 'fixed-width
)
1032 (org-src--on-datum-p element
))
1033 (user-error "Not in a fixed-width area"))
1034 (org-src--edit-element
1036 (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
1037 org-edit-fixed-width-region-mode
1038 (lambda () (while (not (eobp)) (insert ": ") (forward-line))))
1042 (defun org-edit-src-abort ()
1043 "Abort editing of the src code and return to the Org buffer."
1045 (let (org-src--allow-write-back) (org-edit-src-exit)))
1047 (defun org-edit-src-continue (e)
1048 "Unconditionally return to buffer editing area under point.
1049 Throw an error if there is no such buffer."
1052 (let ((buf (get-char-property (point) 'edit-buffer
)))
1053 (if buf
(org-src-switch-to-buffer buf
'continue
)
1054 (user-error "No sub-editing buffer for area at point"))))
1056 (defun org-edit-src-save ()
1057 "Save parent buffer with current state source-code buffer."
1059 (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
1060 (set-buffer-modified-p nil
)
1061 (let ((edited-code (org-src--contents-for-write-back))
1062 (beg org-src--beg-marker
)
1063 (end org-src--end-marker
)
1064 (overlay org-src--overlay
))
1065 (with-current-buffer (org-src--source-buffer)
1068 ;; Temporarily disable read-only features of OVERLAY in order to
1069 ;; insert new contents.
1070 (delete-overlay overlay
)
1071 (delete-region beg end
)
1072 (let ((expecting-bol (bolp)))
1073 (insert edited-code
)
1074 (when (and expecting-bol
(not (bolp))) (insert "\n")))
1076 (move-overlay overlay beg
(point))))
1077 ;; `write-contents-functions' requires the function to return
1078 ;; a non-nil value so that other functions are not called.
1081 (defun org-edit-src-exit ()
1082 "Kill current sub-editing buffer and return to source buffer."
1084 (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
1085 (let* ((beg org-src--beg-marker
)
1086 (end org-src--end-marker
)
1087 (write-back org-src--allow-write-back
)
1088 (remote org-src--remote
)
1089 (coordinates (and (not remote
)
1090 (org-src--coordinates (point) 1 (point-max))))
1091 (code (and write-back
(org-src--contents-for-write-back))))
1092 (set-buffer-modified-p nil
)
1093 ;; Switch to source buffer. Kill sub-editing buffer.
1094 (let ((edit-buffer (current-buffer))
1095 (source-buffer (marker-buffer beg
)))
1096 (unless source-buffer
(error "Source buffer disappeared. Aborting"))
1097 (org-src-switch-to-buffer source-buffer
'exit
)
1098 (kill-buffer edit-buffer
))
1099 ;; Insert modified code. Ensure it ends with a newline character.
1100 (org-with-wide-buffer
1101 (when (and write-back
(not (equal (buffer-substring beg end
) code
)))
1104 (delete-region beg end
)
1105 (let ((expecting-bol (bolp)))
1107 (when (and expecting-bol
(not (bolp))) (insert "\n")))))
1108 ;; If we are to return to source buffer, put point at an
1109 ;; appropriate location. In particular, if block is hidden, move
1110 ;; to the beginning of the block opening line.
1114 ;; Block is hidden; move at start of block.
1115 ((cl-some (lambda (o) (eq (overlay-get o
'invisible
) 'org-hide-block
))
1116 (overlays-at (point)))
1117 (beginning-of-line 0))
1118 (write-back (org-src--goto-coordinates coordinates beg end
))))
1119 ;; Clean up left-over markers and restore window configuration.
1120 (set-marker beg nil
)
1121 (set-marker end nil
)
1122 (when org-src--saved-temp-window-config
1123 (set-window-configuration org-src--saved-temp-window-config
)
1124 (setq org-src--saved-temp-window-config nil
))))
1129 ;;; org-src.el ends here