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