Add test for table alignment
[org-mode.git] / lisp / org-src.el
blobf59d7eca51feca8eb4d0c3dcc042d14c9f5d4ccc
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
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-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-switch-to-buffer-other-window "org" (&rest args))
52 (declare-function org-trim "org" (s &optional keep-lead))
54 (defvar org-element-all-elements)
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 "25.2"
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 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 (memq (org-element-type datum) org-element-all-elements)
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 (erase-buffer)
526 ;; Add string and a final space to ensure property change.
527 (insert string " ")
528 (unless (eq major-mode lang-mode) (funcall lang-mode))
529 (org-font-lock-ensure)
530 (let ((pos (point-min)) next)
531 (while (setq next (next-property-change pos))
532 ;; Handle additional properties from font-lock, so as to
533 ;; preserve, e.g., composition.
534 (dolist (prop (cons 'face font-lock-extra-managed-props))
535 (let ((new-prop (get-text-property pos prop)))
536 (put-text-property
537 (+ start (1- pos)) (1- (+ start next)) prop new-prop
538 org-buffer)))
539 (setq pos next))))
540 ;; Add Org faces.
541 (let ((src-face (nth 1 (assoc-string lang org-src-block-faces t))))
542 (when (or (facep src-face) (listp src-face))
543 (font-lock-append-text-property start end 'face src-face))
544 (font-lock-append-text-property start end 'face 'org-block))
545 (add-text-properties
546 start end
547 '(font-lock-fontified t fontified t font-lock-multiline t))
548 (set-buffer-modified-p modified)))))
551 ;;; Escape contents
553 (defun org-escape-code-in-region (beg end)
554 "Escape lines between BEG and END.
555 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
556 \",#+\" by appending a comma to it."
557 (interactive "r")
558 (save-excursion
559 (goto-char end)
560 (while (re-search-backward "^[ \t]*,?\\(\\*\\|#\\+\\)" beg t)
561 (save-excursion (replace-match ",\\1" nil nil nil 1)))))
563 (defun org-escape-code-in-string (s)
564 "Escape lines in string S.
565 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
566 \",#+\" by appending a comma to it."
567 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil 1))
569 (defun org-unescape-code-in-region (beg end)
570 "Un-escape lines between BEG and END.
571 Un-escaping happens by removing the first comma on lines starting
572 with \",*\", \",#+\", \",,*\" and \",,#+\"."
573 (interactive "r")
574 (save-excursion
575 (goto-char end)
576 (while (re-search-backward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" beg t)
577 (save-excursion (replace-match "" nil nil nil 1)))))
579 (defun org-unescape-code-in-string (s)
580 "Un-escape lines in string S.
581 Un-escaping happens by removing the first comma on lines starting
582 with \",*\", \",#+\", \",,*\" and \",,#+\"."
583 (replace-regexp-in-string
584 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1))
588 ;;; Org src minor mode
590 (defvar org-src-mode-map
591 (let ((map (make-sparse-keymap)))
592 (define-key map "\C-c'" 'org-edit-src-exit)
593 (define-key map "\C-c\C-k" 'org-edit-src-abort)
594 (define-key map "\C-x\C-s" 'org-edit-src-save)
595 map))
597 (define-minor-mode org-src-mode
598 "Minor mode for language major mode buffers generated by Org.
599 \\<org-mode-map>
600 This minor mode is turned on in two situations:
601 - when editing a source code snippet with `\\[org-edit-special]'
602 - when formatting a source code snippet for export with htmlize.
604 \\{org-src-mode-map}
606 See also `org-src-mode-hook'."
607 nil " OrgSrc" nil
608 (when org-edit-src-persistent-message
609 (setq-local
610 header-line-format
611 (substitute-command-keys
612 (if org-src--allow-write-back
613 "Edit, then exit with `\\[org-edit-src-exit]' or abort with \
614 `\\[org-edit-src-abort]'"
615 "Exit with `\\[org-edit-src-exit]' or abort with \
616 `\\[org-edit-src-abort]'"))))
617 ;; Possibly activate various auto-save features (for the edit buffer
618 ;; or the source buffer).
619 (when org-edit-src-turn-on-auto-save
620 (setq buffer-auto-save-file-name
621 (concat (make-temp-name "org-src-")
622 (format-time-string "-%Y-%d-%m")
623 ".txt")))
624 (unless (or org-src--auto-save-timer (zerop org-edit-src-auto-save-idle-delay))
625 (setq org-src--auto-save-timer
626 (run-with-idle-timer
627 org-edit-src-auto-save-idle-delay t
628 (lambda ()
629 (save-excursion
630 (let (edit-flag)
631 (dolist (b (buffer-list))
632 (with-current-buffer b
633 (when (org-src-edit-buffer-p)
634 (unless edit-flag (setq edit-flag t))
635 (when (buffer-modified-p) (org-edit-src-save)))))
636 (unless edit-flag
637 (cancel-timer org-src--auto-save-timer)
638 (setq org-src--auto-save-timer nil)))))))))
640 (defun org-src-mode-configure-edit-buffer ()
641 (when (bound-and-true-p org-src--from-org-mode)
642 (add-hook 'kill-buffer-hook #'org-src--remove-overlay nil 'local)
643 (if (bound-and-true-p org-src--allow-write-back)
644 (progn
645 (setq buffer-offer-save t)
646 (setq buffer-file-name
647 (concat (buffer-file-name (marker-buffer org-src--beg-marker))
648 "[" (buffer-name) "]")))
649 (setq buffer-read-only t))))
651 (add-hook 'org-src-mode-hook #'org-src-mode-configure-edit-buffer)
655 ;;; Babel related functions
657 (defun org-src-associate-babel-session (info)
658 "Associate edit buffer with comint session."
659 (interactive)
660 (let ((session (cdr (assq :session (nth 2 info)))))
661 (and session (not (string= session "none"))
662 (org-babel-comint-buffer-livep session)
663 (let ((f (intern (format "org-babel-%s-associate-session"
664 (nth 0 info)))))
665 (and (fboundp f) (funcall f session))))))
667 (defun org-src-babel-configure-edit-buffer ()
668 (when org-src--babel-info
669 (org-src-associate-babel-session org-src--babel-info)))
671 (add-hook 'org-src-mode-hook #'org-src-babel-configure-edit-buffer)
674 ;;; Public API
676 (defmacro org-src-do-at-code-block (&rest body)
677 "Execute BODY from an edit buffer in the Org mode buffer."
678 (declare (debug (body)))
679 `(let ((beg-marker org-src--beg-marker))
680 (when beg-marker
681 (with-current-buffer (marker-buffer beg-marker)
682 (goto-char beg-marker)
683 ,@body))))
685 (defun org-src-do-key-sequence-at-code-block (&optional key)
686 "Execute key sequence at code block in the source Org buffer.
687 The command bound to KEY in the Org-babel key map is executed
688 remotely with point temporarily at the start of the code block in
689 the Org buffer.
691 This command is not bound to a key by default, to avoid conflicts
692 with language major mode bindings. To bind it to C-c @ in all
693 language major modes, you could use
695 (add-hook \\='org-src-mode-hook
696 (lambda () (define-key org-src-mode-map \"\\C-c@\"
697 \\='org-src-do-key-sequence-at-code-block)))
699 In that case, for example, C-c @ t issued in code edit buffers
700 would tangle the current Org code block, C-c @ e would execute
701 the block and C-c @ h would display the other available
702 Org-babel commands."
703 (interactive "kOrg-babel key: ")
704 (if (equal key (kbd "C-g")) (keyboard-quit)
705 (org-edit-src-save)
706 (org-src-do-at-code-block
707 (call-interactively (lookup-key org-babel-map key)))))
709 (defun org-src-edit-buffer-p (&optional buffer)
710 "Non-nil when current buffer is a source editing buffer.
711 If BUFFER is non-nil, test it instead."
712 (let ((buffer (org-base-buffer (or buffer (current-buffer)))))
713 (and (buffer-live-p buffer)
714 (local-variable-p 'org-src--beg-marker buffer)
715 (local-variable-p 'org-src--end-marker buffer))))
717 (defun org-src-switch-to-buffer (buffer context)
718 (pcase org-src-window-setup
719 (`current-window (pop-to-buffer-same-window buffer))
720 (`other-window
721 (switch-to-buffer-other-window buffer))
722 (`other-frame
723 (pcase context
724 (`exit
725 (let ((frame (selected-frame)))
726 (switch-to-buffer-other-frame buffer)
727 (delete-frame frame)))
728 (`save
729 (kill-buffer (current-buffer))
730 (pop-to-buffer-same-window buffer))
731 (_ (switch-to-buffer-other-frame buffer))))
732 (`reorganize-frame
733 (when (eq context 'edit) (delete-other-windows))
734 (org-switch-to-buffer-other-window buffer)
735 (when (eq context 'exit) (delete-other-windows)))
736 (`switch-invisibly (set-buffer buffer))
738 (message "Invalid value %s for `org-src-window-setup'"
739 org-src-window-setup)
740 (pop-to-buffer-same-window buffer))))
742 (defun org-src-coderef-format (&optional element)
743 "Return format string for block at point.
745 When optional argument ELEMENT is provided, use that block.
746 Otherwise, assume point is either at a source block, at an
747 example block.
749 If point is in an edit buffer, retrieve format string associated
750 to the remote source block."
751 (cond
752 ((and element (org-element-property :label-fmt element)))
753 ((org-src-edit-buffer-p) (org-src-do-at-code-block (org-src-coderef-format)))
754 ((org-element-property :label-fmt (org-element-at-point)))
755 (t org-coderef-label-format)))
757 (defun org-src-coderef-regexp (fmt &optional label)
758 "Return regexp matching a coderef format string FMT.
760 When optional argument LABEL is non-nil, match coderef for that
761 label only.
763 Match group 1 contains the full coderef string with surrounding
764 white spaces. Match group 2 contains the same string without any
765 surrounding space. Match group 3 contains the label.
767 A coderef format regexp can only match at the end of a line."
768 (format "\\S-\\([ \t]*\\(%s\\)[ \t]*\\)$"
769 (replace-regexp-in-string
770 "%s"
771 (if label (regexp-quote label) "\\([-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\)")
772 (regexp-quote fmt)
773 nil t)))
775 (defun org-edit-footnote-reference ()
776 "Edit definition of footnote reference at point."
777 (interactive)
778 (let* ((context (org-element-context))
779 (label (org-element-property :label context)))
780 (unless (and (eq (org-element-type context) 'footnote-reference)
781 (org-src--on-datum-p context))
782 (user-error "Not on a footnote reference"))
783 (unless label (user-error "Cannot edit remotely anonymous footnotes"))
784 (let* ((definition (org-with-wide-buffer
785 (org-footnote-goto-definition label)
786 (backward-char)
787 (org-element-context)))
788 (inline (eq (org-element-type definition) 'footnote-reference))
789 (contents
790 (let ((c (org-with-wide-buffer
791 (org-trim (buffer-substring-no-properties
792 (org-element-property :begin definition)
793 (org-element-property :end definition))))))
794 (add-text-properties
796 (progn (string-match (if inline "\\`\\[fn:.*?:" "\\`.*?\\]") c)
797 (match-end 0))
798 '(read-only "Cannot edit footnote label" front-sticky t
799 rear-nonsticky t)
801 (when inline
802 (let ((l (length c)))
803 (add-text-properties
804 (1- l) l
805 '(read-only "Cannot edit past footnote reference"
806 front-sticky nil rear-nonsticky nil)
807 c)))
808 c)))
809 (org-src--edit-element
810 definition
811 (format "*Edit footnote [%s]*" label)
812 #'org-mode
813 `(lambda ()
814 (if ,(not inline) (delete-region (point) (search-forward "]"))
815 (delete-region (point) (search-forward ":" nil t 2))
816 (delete-region (1- (point-max)) (point-max))
817 (when (re-search-forward "\n[ \t]*\n" nil t)
818 (user-error "Inline definitions cannot contain blank lines"))
819 ;; If footnote reference belongs to a table, make sure to
820 ;; remove any newline characters in order to preserve
821 ;; table's structure.
822 (when ,(org-element-lineage definition '(table-cell))
823 (while (search-forward "\n" nil t) (delete-char -1)))))
824 (concat contents
825 (and (not (org-element-property :contents-begin definition))
826 " "))
827 'remote))
828 ;; Report success.
831 (defun org-edit-table.el ()
832 "Edit \"table.el\" table at point.
833 \\<org-src-mode-map>
834 A new buffer is created and the table is copied into it. Then
835 the table is recognized with `table-recognize'. When done
836 editing, exit with `\\[org-edit-src-exit]'. The edited text will \
837 then replace
838 the area in the Org mode buffer.
840 Throw an error when not at such a table."
841 (interactive)
842 (let ((element (org-element-at-point)))
843 (unless (and (eq (org-element-type element) 'table)
844 (eq (org-element-property :type element) 'table.el)
845 (org-src--on-datum-p element))
846 (user-error "Not in a table.el table"))
847 (org-src--edit-element
848 element
849 (org-src--construct-edit-buffer-name (buffer-name) "Table")
850 #'text-mode t)
851 (when (bound-and-true-p flyspell-mode) (flyspell-mode -1))
852 (table-recognize)
855 (defun org-edit-export-block ()
856 "Edit export block at point.
857 \\<org-src-mode-map>
858 A new buffer is created and the block is copied into it, and the
859 buffer is switched into an appropriate major mode. See also
860 `org-src-lang-modes'.
862 When done, exit with `\\[org-edit-src-exit]'. The edited text \
863 will then replace
864 the area in the Org mode buffer.
866 Throw an error when not at an export block."
867 (interactive)
868 (let ((element (org-element-at-point)))
869 (unless (and (eq (org-element-type element) 'export-block)
870 (org-src--on-datum-p element))
871 (user-error "Not in an export block"))
872 (let* ((type (downcase (org-element-property :type element)))
873 (mode (org-src--get-lang-mode type)))
874 (unless (functionp mode) (error "No such language mode: %s" mode))
875 (org-src--edit-element
876 element
877 (org-src--construct-edit-buffer-name (buffer-name) type)
878 mode
879 (lambda () (org-escape-code-in-region (point-min) (point-max)))))
882 (defun org-edit-src-code (&optional code edit-buffer-name)
883 "Edit the source or example block at point.
884 \\<org-src-mode-map>
885 The code is copied to a separate buffer and the appropriate mode
886 is turned on. When done, exit with `\\[org-edit-src-exit]'. This \
887 will remove the
888 original code in the Org buffer, and replace it with the edited
889 version. See `org-src-window-setup' to configure the display of
890 windows containing the Org buffer and the code buffer.
892 When optional argument CODE is a string, edit it in a dedicated
893 buffer instead.
895 When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
896 name of the sub-editing buffer."
897 (interactive)
898 (let* ((element (org-element-at-point))
899 (type (org-element-type element)))
900 (unless (and (memq type '(example-block src-block))
901 (org-src--on-datum-p element))
902 (user-error "Not in a source or example block"))
903 (let* ((lang
904 (if (eq type 'src-block) (org-element-property :language element)
905 "example"))
906 (lang-f (and (eq type 'src-block) (org-src--get-lang-mode lang)))
907 (babel-info (and (eq type 'src-block)
908 (org-babel-get-src-block-info 'light)))
909 deactivate-mark)
910 (when (and (eq type 'src-block) (not (functionp lang-f)))
911 (error "No such language mode: %s" lang-f))
912 (org-src--edit-element
913 element
914 (or edit-buffer-name
915 (org-src--construct-edit-buffer-name (buffer-name) lang))
916 lang-f
917 (and (null code)
918 `(lambda ()
919 (unless ,(or org-src-preserve-indentation
920 (org-element-property :preserve-indent element))
921 (when (> org-edit-src-content-indentation 0)
922 (while (not (eobp))
923 (unless (looking-at "[ \t]*$")
924 (indent-line-to (+ (org-get-indentation)
925 org-edit-src-content-indentation)))
926 (forward-line))))
927 (org-escape-code-in-region (point-min) (point-max))))
928 (and code (org-unescape-code-in-string code)))
929 ;; Finalize buffer.
930 (setq-local org-coderef-label-format
931 (or (org-element-property :label-fmt element)
932 org-coderef-label-format))
933 (when (eq type 'src-block)
934 (setq-local org-src--babel-info babel-info)
935 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
936 (when (fboundp edit-prep-func)
937 (funcall edit-prep-func babel-info))))
938 t)))
940 (defun org-edit-inline-src-code ()
941 "Edit inline source code at point."
942 (interactive)
943 (let ((context (org-element-context)))
944 (unless (and (eq (org-element-type context) 'inline-src-block)
945 (org-src--on-datum-p context))
946 (user-error "Not on inline source code"))
947 (let* ((lang (org-element-property :language context))
948 (lang-f (org-src--get-lang-mode lang))
949 (babel-info (org-babel-get-src-block-info 'light))
950 deactivate-mark)
951 (unless (functionp lang-f) (error "No such language mode: %s" lang-f))
952 (org-src--edit-element
953 context
954 (org-src--construct-edit-buffer-name (buffer-name) lang)
955 lang-f
956 (lambda ()
957 ;; Inline src blocks are limited to one line.
958 (while (re-search-forward "\n[ \t]*" nil t) (replace-match " "))
959 ;; Trim contents.
960 (goto-char (point-min))
961 (skip-chars-forward " \t")
962 (delete-region (point-min) (point))
963 (goto-char (point-max))
964 (skip-chars-backward " \t")
965 (delete-region (point) (point-max))))
966 ;; Finalize buffer.
967 (setq-local org-src--babel-info babel-info)
968 (setq-local org-src--preserve-indentation t)
969 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
970 (when (fboundp edit-prep-func) (funcall edit-prep-func babel-info)))
971 ;; Return success.
972 t)))
974 (defun org-edit-fixed-width-region ()
975 "Edit the fixed-width ASCII drawing at point.
976 \\<org-src-mode-map>
977 This must be a region where each line starts with a colon
978 followed by a space or a newline character.
980 A new buffer is created and the fixed-width region is copied into
981 it, and the buffer is switched into the major mode defined in
982 `org-edit-fixed-width-region-mode', which see.
984 When done, exit with `\\[org-edit-src-exit]'. The edited text \
985 will then replace
986 the area in the Org mode buffer."
987 (interactive)
988 (let ((element (org-element-at-point)))
989 (unless (and (eq (org-element-type element) 'fixed-width)
990 (org-src--on-datum-p element))
991 (user-error "Not in a fixed-width area"))
992 (org-src--edit-element
993 element
994 (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
995 org-edit-fixed-width-region-mode
996 (lambda () (while (not (eobp)) (insert ": ") (forward-line))))
997 ;; Return success.
1000 (defun org-edit-src-abort ()
1001 "Abort editing of the src code and return to the Org buffer."
1002 (interactive)
1003 (let (org-src--allow-write-back) (org-edit-src-exit)))
1005 (defun org-edit-src-continue (e)
1006 "Unconditionally return to buffer editing area under point.
1007 Throw an error if there is no such buffer."
1008 (interactive "e")
1009 (mouse-set-point e)
1010 (let ((buf (get-char-property (point) 'edit-buffer)))
1011 (if buf (org-src-switch-to-buffer buf 'continue)
1012 (user-error "No sub-editing buffer for area at point"))))
1014 (defun org-edit-src-save ()
1015 "Save parent buffer with current state source-code buffer."
1016 (interactive)
1017 (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
1018 (set-buffer-modified-p nil)
1019 (let ((edited-code (org-src--contents-for-write-back))
1020 (beg org-src--beg-marker)
1021 (end org-src--end-marker)
1022 (overlay org-src--overlay))
1023 (with-current-buffer (org-src--source-buffer)
1024 (undo-boundary)
1025 (goto-char beg)
1026 ;; Temporarily disable read-only features of OVERLAY in order to
1027 ;; insert new contents.
1028 (delete-overlay overlay)
1029 (delete-region beg end)
1030 (let ((expecting-bol (bolp)))
1031 (insert edited-code)
1032 (when (and expecting-bol (not (bolp))) (insert "\n")))
1033 (save-buffer)
1034 (move-overlay overlay beg (point)))))
1036 (defun org-edit-src-exit ()
1037 "Kill current sub-editing buffer and return to source buffer."
1038 (interactive)
1039 (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
1040 (let* ((beg org-src--beg-marker)
1041 (end org-src--end-marker)
1042 (write-back org-src--allow-write-back)
1043 (remote org-src--remote)
1044 (coordinates (and (not remote)
1045 (org-src--coordinates (point) 1 (point-max))))
1046 (code (and write-back (org-src--contents-for-write-back))))
1047 (set-buffer-modified-p nil)
1048 ;; Switch to source buffer. Kill sub-editing buffer.
1049 (let ((edit-buffer (current-buffer)))
1050 (org-src-switch-to-buffer (marker-buffer beg) 'exit)
1051 (kill-buffer edit-buffer))
1052 ;; Insert modified code. Ensure it ends with a newline character.
1053 (org-with-wide-buffer
1054 (when (and write-back (not (equal (buffer-substring beg end) code)))
1055 (undo-boundary)
1056 (goto-char beg)
1057 (delete-region beg end)
1058 (let ((expecting-bol (bolp)))
1059 (insert code)
1060 (when (and expecting-bol (not (bolp))) (insert "\n")))))
1061 ;; If we are to return to source buffer, put point at an
1062 ;; appropriate location. In particular, if block is hidden, move
1063 ;; to the beginning of the block opening line.
1064 (unless remote
1065 (goto-char beg)
1066 (cond
1067 ;; Block is hidden; move at start of block.
1068 ((cl-some (lambda (o) (eq (overlay-get o 'invisible) 'org-hide-block))
1069 (overlays-at (point)))
1070 (beginning-of-line 0))
1071 (write-back (org-src--goto-coordinates coordinates beg end))))
1072 ;; Clean up left-over markers and restore window configuration.
1073 (set-marker beg nil)
1074 (set-marker end nil)
1075 (when org-src--saved-temp-window-config
1076 (set-window-configuration org-src--saved-temp-window-config)
1077 (setq org-src--saved-temp-window-config nil))))
1080 (provide 'org-src)
1082 ;;; org-src.el ends here