1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004-2015 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 Org-mode.
40 (declare-function org-do-remove-indentation
"org" (&optional n
))
41 (declare-function org-get-indentation
"org" (&optional line
))
42 (declare-function org-switch-to-buffer-other-window
"org" (&rest args
))
43 (declare-function org-pop-to-buffer-same-window
44 "org-compat" (&optional buffer-or-name norecord label
))
45 (declare-function org-base-buffer
"org" (buffer))
47 (defcustom org-edit-src-turn-on-auto-save nil
48 "Non-nil means turn `auto-save-mode' on when editing a source block.
49 This will save the content of the source code editing buffer into
50 a newly created file, not the base buffer for this source block.
52 If you want to regularly save the base buffer instead of the source
53 code editing buffer, see `org-edit-src-auto-save-idle-delay' instead."
54 :group
'org-edit-structure
56 :package-version
'(Org .
"8.0")
59 (defcustom org-edit-src-auto-save-idle-delay
0
60 "Delay before saving a source code buffer back into its base buffer.
61 When a positive integer N, save after N seconds of idle time.
62 When 0 (the default), don't auto-save.
64 If you want to save the source code buffer itself, don't use this.
65 Check `org-edit-src-turn-on-auto-save' instead."
66 :group
'org-edit-structure
68 :package-version
'(Org .
"8.0")
71 (defcustom org-coderef-label-format
"(ref:%s)"
72 "The default coderef format.
73 This format string will be used to search for coderef labels in literal
74 examples (EXAMPLE and SRC blocks). The format can be overwritten in
75 an individual literal example with the -l option, like
77 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
81 If you want to use this for HTML export, make sure that the format does
82 not introduce special font-locking, and avoid the HTML special
83 characters `<', `>', and `&'. The reason for this restriction is that
84 the labels are searched for only after htmlize has done its job."
85 :group
'org-edit-structure
; FIXME this is not in the right group
88 (defcustom org-edit-fixed-width-region-mode
'artist-mode
89 "The mode that should be used to edit fixed-width regions.
90 These are the regions where each line starts with a colon."
91 :group
'org-edit-structure
95 (const fundamental-mode
)
96 (function :tag
"Other (specify)")))
98 (defcustom org-src-preserve-indentation nil
99 "If non-nil preserve leading whitespace characters on export.
100 If non-nil leading whitespace characters in source code blocks
101 are preserved on export, and when switching between the org
102 buffer and the language mode edit buffer.
104 When this variable is nil, after editing with \\[org-edit-src-code],
105 the minimum (across-lines) number of leading whitespace characters
106 are removed from all lines, and the code block is uniformly indented
107 according to the value of `org-edit-src-content-indentation'."
108 :group
'org-edit-structure
111 (defcustom org-edit-src-content-indentation
2
112 "Indentation for the content of a source code block.
113 This should be the number of spaces added to the indentation of the #+begin
114 line in order to compute the indentation of the block content after
115 editing it with \\[org-edit-src-code]. Has no effect if
116 `org-src-preserve-indentation' is non-nil."
117 :group
'org-edit-structure
120 (defcustom org-edit-src-persistent-message t
121 "Non-nil means show persistent exit help message while editing src examples.
122 The message is shown in the header-line, which will be created in the
123 first line of the window showing the editing buffer."
124 :group
'org-edit-structure
127 (defcustom org-src-ask-before-returning-to-edit-buffer t
128 "Non-nil means ask before switching to an existing edit buffer.
129 If nil, when `org-edit-src-code' is used on a block that already
130 has an active edit buffer, it will switch to that edit buffer
131 immediately; otherwise it will ask whether you want to return to
132 the existing edit buffer."
133 :group
'org-edit-structure
135 :package-version
'(Org .
"8.0")
138 (defcustom org-src-window-setup
'reorganize-frame
139 "How the source code edit buffer should be displayed.
140 Possible values for this option are:
142 current-window Show edit buffer in the current window, keeping all other
144 other-window Use `switch-to-buffer-other-window' to display edit buffer.
145 reorganize-frame Show only two windows on the current frame, the current
146 window and the edit buffer. When exiting the edit buffer,
147 return to one window.
148 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
149 Also, when exiting the edit buffer, kill that frame."
150 :group
'org-edit-structure
152 (const current-window
)
155 (const reorganize-frame
)))
157 (defvar org-src-mode-hook nil
158 "Hook run after Org switched a source code snippet to its Emacs mode.
161 - when editing a source code snippet with \"C-c '\".
162 - When formatting a source code snippet for export with htmlize.
164 You may want to use this hook for example to turn off `outline-minor-mode'
165 or similar things which you want to have when editing a source code file,
166 but which mess up the display of a snippet in Org exported files.")
168 (defcustom org-src-lang-modes
169 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
170 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
171 ("calc" . fundamental
) ("C" . c
) ("cpp" . c
++) ("C++" . c
++)
172 ("screen" . shell-script
) ("shell" . sh
) ("bash" . sh
))
173 "Alist mapping languages to their major mode.
174 The key is the language name, the value is the string that should
175 be inserted as the name of the major mode. For many languages this is
176 simple, but for language where this is not the case, this variable
177 provides a way to simplify things on the user side.
178 For example, there is no ocaml-mode in Emacs, but the mode to use is
180 :group
'org-edit-structure
183 (string "Language name")
184 (symbol "Major mode"))))
186 (defcustom org-src-tab-acts-natively nil
187 "If non-nil, the effect of TAB in a code block is as if it were
188 issued in the language major mode buffer."
195 ;;; Internal functions and variables
197 (defvar org-src--allow-write-back-p t
)
198 (defvar org-src--beg-marker nil
)
199 (defvar org-src--block-indentation nil
)
200 (defvar org-src--code-timer nil
)
201 (defvar org-src--end-marker nil
)
202 (defvar org-src--from-org-mode nil
)
203 (defvar org-src--overlay nil
)
204 (defvar org-src--saved-temp-window-config nil
)
205 (defvar org-src--type nil
)
206 (defvar org-src--babel-info nil
)
208 (defun org-src--construct-edit-buffer-name (org-buffer-name lang
)
209 "Construct the buffer name for a source editing buffer."
210 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
212 (defun org-src--edit-buffer (beg end
)
213 "Return buffer editing area between BEG and END.
214 Return nil if there is no such buffer."
216 (dolist (b (buffer-list))
217 (with-current-buffer b
218 (and (org-src-edit-buffer-p)
219 (= beg org-src--beg-marker
)
220 (eq (marker-buffer beg
) (marker-buffer org-src--beg-marker
))
221 (= end org-src--end-marker
)
222 (eq (marker-buffer end
) (marker-buffer org-src--end-marker
))
225 (defun org-src--source-buffer ()
226 "Return source buffer edited by current buffer."
227 (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
228 (or (marker-buffer org-src--beg-marker
)
229 (error "No source buffer available for current editing session")))
231 (defun org-src--get-lang-mode (lang)
232 "Return major mode that should be used for LANG.
233 LANG is a string, and the returned major mode is a symbol."
236 (let ((l (or (cdr (assoc lang org-src-lang-modes
)) lang
)))
237 (if (symbolp l
) (symbol-name l
) l
))
240 (defun org-src--coordinates (pos beg end
)
241 "Return coordinates of POS relatively to BEG and END.
242 POS, BEG and END are buffer positions. Return value is either
243 a cons cell (LINE . COLUMN) or symbol `end'. See also
244 `org-src--goto-coordinates'."
245 (if (>= pos end
) 'end
246 (org-with-wide-buffer
247 (goto-char (max beg pos
))
248 (cons (count-lines beg
(line-beginning-position))
249 ;; Column is relative to the end of line to avoid problems of
250 ;; comma escaping or colons appended in front of the line.
252 (progn (end-of-line) (current-column)))))))
254 (defun org-src--goto-coordinates (coord beg end
)
255 "Move to coordinates COORD relatively to BEG and END.
256 COORD are coordinates, as returned by `org-src--coordinates',
257 which see. BEG and END are buffer positions."
259 (if (eq coord
'end
) (max (1- end
) beg
)
260 ;; If BEG happens to be located outside of the narrowed part of
261 ;; the buffer, widen it first.
262 (org-with-wide-buffer
264 (forward-line (car coord
))
266 (org-move-to-column (max (+ (current-column) (cdr coord
)) 0))
269 (defun org-src--element-contents-area (element)
270 "Return contents boundaries of ELEMENT.
271 Return value is a pair (BEG . END) where BEG and END are buffer
273 (let ((blockp (memq (org-element-type element
)
274 '(example-block export-block src-block
))))
275 (cons (org-with-wide-buffer
276 (goto-char (org-element-property :post-affiliated element
))
277 (if blockp
(line-beginning-position 2) (point)))
278 (org-with-wide-buffer
279 (goto-char (org-element-property :end element
))
280 (skip-chars-backward " \r\t\n")
281 (line-beginning-position (if blockp
1 2))))))
283 (defun org-src--make-source-overlay (beg end edit-buffer
)
284 "Create overlay between BEG and END positions and return it.
285 EDIT-BUFFER is the buffer currently editing area between BEG and
287 (let ((overlay (make-overlay beg end
)))
288 (overlay-put overlay
'face
'secondary-selection
)
289 (overlay-put overlay
'edit-buffer edit-buffer
)
290 (overlay-put overlay
'help-echo
291 "Click with mouse-1 to switch to buffer editing this segment")
292 (overlay-put overlay
'face
'secondary-selection
)
293 (overlay-put overlay
'keymap
294 (let ((map (make-sparse-keymap)))
295 (define-key map
[mouse-1
] 'org-edit-src-continue
)
297 ;; TODO: The below line doesn't work for two reasons:
298 ;; - It should be 'read-only
299 ;; - 'read-only apparently doesn't work on overlays (also empirically tested):
300 ;; <https://lists.gnu.org/archive/html/emacs-devel/2008-01/msg01598.html>
301 ;; If this feature is to be kept, it should be implemented via text
302 ;; properties, which will require fiddling around in more places
303 ;; (such as when the contents are copied back into the buffer after
304 ;; editing is complete.)
305 (overlay-put overlay
:read-only
"Leave me alone")
308 (defun org-src--remove-overlay ()
309 "Remove overlay from current source buffer."
310 (when (overlayp org-src--overlay
) (delete-overlay org-src--overlay
)))
312 (defun org-src--on-element-p (element)
313 "Non-nil when point is on ELEMENT."
314 (and (>= (point) (org-element-property :begin element
))
316 (org-with-wide-buffer
317 (goto-char (org-element-property :end element
))
318 (skip-chars-backward " \r\t\n")
319 (line-end-position)))))
321 (defun org-src--contents-for-write-back ()
322 "Return buffer contents in a format appropriate for write back.
323 Assume point is in the corresponding edit buffer."
324 (let ((indentation (+ (or org-src--block-indentation
0)
325 (if (memq org-src--type
'(example-block src-block
))
326 org-edit-src-content-indentation
328 (preserve-indentation org-src-preserve-indentation
)
329 (contents (org-with-wide-buffer (buffer-string)))
330 (fixed-width-p (eq org-src--type
'fixed-width
)))
333 (goto-char (point-min))
336 (untabify (point-min) (point-max))
337 (let ((prefix (concat (and (not preserve-indentation
)
338 (make-string indentation ?\s
))
340 (while (not (eobp)) (insert prefix
) (forward-line))))
341 (unless preserve-indentation
(untabify (point-min) (point-max)))
342 (org-escape-code-in-region (point-min) (point-max))
343 (unless (or preserve-indentation
(zerop indentation
))
344 (let ((ind (make-string indentation ?\s
)))
346 (when (org-looking-at-p "[ \t]*\\S-") (insert ind
))
350 (defun org-src--edit-element (element name
&optional major write-back contents
)
351 "Edit ELEMENT contents in a dedicated buffer.
353 MAJOR is the major mode used in the edit buffer. A nil value is
354 equivalent to `fundamental-mode'. When WRITE-BACK is non-nil,
355 assume contents will replace original region. When CONTENTS is
356 non-nil, display them in the edit buffer. Otherwise, assume they
357 are located in property `:value'.
359 Leave point in edit buffer."
360 (setq org-src--saved-temp-window-config
(current-window-configuration))
361 (let* ((area (org-src--element-contents-area element
))
362 (beg (copy-marker (car area
)))
363 (end (copy-marker (cdr area
) t
))
364 (old-edit-buffer (org-src--edit-buffer beg end
)))
365 (if (and old-edit-buffer
366 (or (not org-src-ask-before-returning-to-edit-buffer
)
367 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
368 ;; Move to existing buffer.
369 (org-src-switch-to-buffer old-edit-buffer
'return
)
370 ;; Discard old edit buffer.
371 (when old-edit-buffer
372 (with-current-buffer old-edit-buffer
(org-src--remove-overlay))
373 (kill-buffer old-edit-buffer
))
374 (let* ((org-mode-p (derived-mode-p 'org-mode
))
375 (type (org-element-type element
))
376 (ind (org-with-wide-buffer
377 (goto-char (org-element-property :begin element
))
378 (org-get-indentation)))
380 (and (memq type
'(example-block src-block
))
381 (or (org-element-property :preserve-indent element
)
382 org-src-preserve-indentation
)))
383 ;; Store relative positions of mark (if any) and point
384 ;; within the edited area.
385 (point-coordinates (org-src--coordinates (point) beg end
))
386 (mark-coordinates (and (org-region-active-p)
388 (and (>= m beg
) (>= end m
)
389 (org-src--coordinates m beg end
)))))
390 ;; Generate a new edit buffer.
391 (buffer (generate-new-buffer name
))
392 ;; Add an overlay on top of source.
393 (overlay (org-src--make-source-overlay beg end buffer
)))
394 ;; Switch to edit buffer.
395 (org-src-switch-to-buffer buffer
'edit
)
397 (insert (or contents
(org-element-property :value element
)))
398 (remove-text-properties (point-min) (point-max)
399 '(display nil invisible nil intangible nil
))
400 (unless preserve-ind
(org-do-remove-indentation))
401 (set-buffer-modified-p nil
)
402 (setq buffer-file-name nil
)
404 (if (not major
) (fundamental-mode)
405 (let ((org-inhibit-startup t
))
406 (condition-case e
(funcall major
)
407 (error (message "Language mode `%s' fails with: %S"
409 ;; Transmit buffer-local variables for exit function. It must
410 ;; be done after initializing major mode, as this operation
411 ;; may reset them otherwise.
412 (org-set-local 'org-src--from-org-mode org-mode-p
)
413 (org-set-local 'org-src--beg-marker beg
)
414 (org-set-local 'org-src--end-marker end
)
415 (org-set-local 'org-src--type type
)
416 (org-set-local 'org-src--block-indentation ind
)
417 (org-set-local 'org-src-preserve-indentation preserve-ind
)
418 (org-set-local 'org-src--overlay overlay
)
419 (org-set-local 'org-src--allow-write-back-p write-back
)
422 (when org-edit-src-persistent-message
425 (substitute-command-keys
427 "Edit, then exit with \\[org-edit-src-exit] or abort with \
428 \\[org-edit-src-abort]"
429 "Exit with \\[org-edit-src-exit] or abort with \
430 \\[org-edit-src-abort]"))))
431 ;; Possibly start `auto-save-mode'.
432 (when org-edit-src-turn-on-auto-save
433 (setq buffer-auto-save-file-name
434 (concat (make-temp-name "org-src-")
435 (format-time-string "-%Y-%d-%m")
437 ;; Move mark and point in edit buffer to the corresponding
439 (when mark-coordinates
440 (org-src--goto-coordinates mark-coordinates
(point-min) (point-max))
441 (push-mark (point) 'no-message t
)
442 (setq deactivate-mark nil
))
443 (org-src--goto-coordinates point-coordinates
(point-min) (point-max)))
444 ;; Install idle auto save feature, if necessary.
445 (or org-src--code-timer
446 (zerop org-edit-src-auto-save-idle-delay
)
447 (setq org-src--code-timer
449 org-edit-src-auto-save-idle-delay t
452 ((org-src-edit-buffer-p)
453 (when (buffer-modified-p) (org-edit-src-save)))
454 ((not (org-some (lambda (b) (org-src-edit-buffer-p b
))
456 (cancel-timer org-src--code-timer
)
457 (setq org-src--code-timer nil
))))))))))
461 ;;; Fontification of source blocks
463 (defun org-src-font-lock-fontify-block (lang start end
)
465 This function is called by emacs automatic fontification, as long
466 as `org-src-fontify-natively' is non-nil."
467 (let ((lang-mode (org-src--get-lang-mode lang
)))
468 (when (fboundp lang-mode
)
469 (let ((string (buffer-substring-no-properties start end
))
470 (modified (buffer-modified-p))
471 (org-buffer (current-buffer)) pos next
)
472 (remove-text-properties start end
'(face nil
))
475 (concat " org-src-fontification:" (symbol-name lang-mode
)))
476 (delete-region (point-min) (point-max))
477 (insert string
" ") ;; so there's a final property change
478 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
479 (font-lock-fontify-buffer)
480 (setq pos
(point-min))
481 (while (setq next
(next-single-property-change pos
'face
))
483 (+ start
(1- pos
)) (1- (+ start next
)) 'face
484 (get-text-property pos
'face
) org-buffer
)
488 '(font-lock-fontified t fontified t font-lock-multiline t
))
489 (set-buffer-modified-p modified
)))))
495 (defun org-escape-code-in-region (beg end
)
496 "Escape lines between BEG and END.
497 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
498 \",#+\" by appending a comma to it."
502 (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t
)
503 (replace-match ",\\1" nil nil nil
1))))
505 (defun org-escape-code-in-string (s)
506 "Escape lines in string S.
507 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
508 \",#+\" by appending a comma to it."
509 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil
1))
511 (defun org-unescape-code-in-region (beg end
)
512 "Un-escape lines between BEG and END.
513 Un-escaping happens by removing the first comma on lines starting
514 with \",*\", \",#+\", \",,*\" and \",,#+\"."
518 (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t
)
519 (replace-match "" nil nil nil
1))))
521 (defun org-unescape-code-in-string (s)
522 "Un-escape lines in string S.
523 Un-escaping happens by removing the first comma on lines starting
524 with \",*\", \",#+\", \",,*\" and \",,#+\"."
525 (replace-regexp-in-string
526 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil
1))
530 ;;; Org src minor mode
532 (defvar org-src-mode-map
(make-sparse-keymap))
533 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
534 (define-key org-src-mode-map
"\C-c\C-k" 'org-edit-src-abort
)
535 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
537 (define-minor-mode org-src-mode
538 "Minor mode for language major mode buffers generated by org.
539 This minor mode is turned on in two situations:
540 - when editing a source code snippet with \"C-c '\".
541 - When formatting a source code snippet for export with htmlize.
542 There is a mode hook, and keybindings for `org-edit-src-exit' and
543 `org-edit-src-save'")
545 (defun org-src-mode-configure-edit-buffer ()
546 (when (org-bound-and-true-p org-src--from-org-mode
)
547 (org-add-hook 'kill-buffer-hook
#'org-src--remove-overlay nil
'local
)
548 (if (org-bound-and-true-p org-src--allow-write-back-p
)
550 (setq buffer-offer-save t
)
551 (setq buffer-file-name
552 (concat (buffer-file-name (marker-buffer org-src--beg-marker
))
553 "[" (buffer-name) "]"))
554 (if (featurep 'xemacs
)
556 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
557 (setq write-contents-hooks
'(org-edit-src-save)))
558 (setq write-contents-functions
'(org-edit-src-save))))
559 (setq buffer-read-only t
))))
561 (org-add-hook 'org-src-mode-hook
#'org-src-mode-configure-edit-buffer
)
565 ;;; Babel related functions
567 (defun org-src-associate-babel-session (info)
568 "Associate edit buffer with comint session."
570 (let ((session (cdr (assoc :session
(nth 2 info
)))))
571 (and session
(not (string= session
"none"))
572 (org-babel-comint-buffer-livep session
)
573 (let ((f (intern (format "org-babel-%s-associate-session"
575 (and (fboundp f
) (funcall f session
))))))
577 (defun org-src-babel-configure-edit-buffer ()
578 (when org-src--babel-info
579 (org-src-associate-babel-session org-src--babel-info
)))
581 (org-add-hook 'org-src-mode-hook
#'org-src-babel-configure-edit-buffer
)
583 (defmacro org-src-do-at-code-block
(&rest body
)
584 "Execute a command from an edit buffer in the Org mode buffer."
585 `(let ((beg-marker org-src--beg-marker
))
587 (with-current-buffer (marker-buffer beg-marker
)
588 (goto-char beg-marker
)
590 (def-edebug-spec org-src-do-at-code-block
(body))
592 (defun org-src-do-key-sequence-at-code-block (&optional key
)
593 "Execute key sequence at code block in the source Org buffer.
594 The command bound to KEY in the Org-babel key map is executed
595 remotely with point temporarily at the start of the code block in
598 This command is not bound to a key by default, to avoid conflicts
599 with language major mode bindings. To bind it to C-c @ in all
600 language major modes, you could use
602 (add-hook 'org-src-mode-hook
603 (lambda () (define-key org-src-mode-map \"\\C-c@\"
604 'org-src-do-key-sequence-at-code-block)))
606 In that case, for example, C-c @ t issued in code edit buffers
607 would tangle the current Org code block, C-c @ e would execute
608 the block and C-c @ h would display the other available
610 (interactive "kOrg-babel key: ")
611 (if (equal key
(kbd "C-g")) (keyboard-quit)
613 (org-src-do-at-code-block
614 (call-interactively (lookup-key org-babel-map key
)))))
620 (defun org-src-edit-buffer-p (&optional buffer
)
621 "Non-nil when current buffer is a source editing buffer.
622 If BUFFER is non-nil, test it instead."
623 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
624 (and (buffer-live-p buffer
)
625 (local-variable-p 'org-src--beg-marker buffer
)
626 (local-variable-p 'org-src--end-marker buffer
))))
628 (defun org-src-switch-to-buffer (buffer context
)
629 (case org-src-window-setup
630 (current-window (org-pop-to-buffer-same-window buffer
))
632 (switch-to-buffer-other-window buffer
))
636 (let ((frame (selected-frame)))
637 (switch-to-buffer-other-frame buffer
)
638 (delete-frame frame
)))
640 (kill-buffer (current-buffer))
641 (org-pop-to-buffer-same-window buffer
))
642 (t (switch-to-buffer-other-frame buffer
))))
644 (when (eq context
'edit
) (delete-other-windows))
645 (org-switch-to-buffer-other-window buffer
)
646 (when (eq context
'exit
) (delete-other-windows)))
647 (switch-invisibly (set-buffer buffer
))
649 (message "Invalid value %s for `org-src-window-setup'"
650 org-src-window-setup
)
651 (org-pop-to-buffer-same-window buffer
))))
653 (defun org-edit-table.el
()
654 "Edit \"table.el\" table at point.
656 A new buffer is created and the table is copied into it. Then
657 the table is recognized with `table-recognize'. When done
658 editing, exit with \\[org-edit-src-exit]. The edited text will
659 then replace the area in the Org mode buffer.
661 Throw an error when not at such a table."
663 (let ((element (org-element-at-point)))
664 (unless (and (eq (org-element-type element
) 'table
)
665 (eq (org-element-property :type element
) 'table.el
)
666 (org-src--on-element-p element
))
667 (user-error "Not in a table.el table"))
668 (org-src--edit-element
670 (org-src--construct-edit-buffer-name (buffer-name) "Table")
672 (when (org-bound-and-true-p flyspell-mode
) (flyspell-mode -
1))
676 (defun org-edit-export-block ()
677 "Edit export block at point.
679 A new buffer is created and the block is copied into it, and the
680 buffer is switched into an appropriate major mode. See also
681 `org-src-lang-modes'. When done, exit with
682 \\[org-edit-src-exit]. The edited text will then replace the
683 area in the Org mode buffer.
685 Throw an error when not at an export block."
687 (let ((element (org-element-at-point)))
688 (unless (and (eq (org-element-type element
) 'export-block
)
689 (org-src--on-element-p element
))
690 (user-error "Not in an export block"))
691 (let* ((type (downcase (org-element-property :type element
)))
692 (mode (org-src--get-lang-mode type
)))
693 (unless (functionp mode
) (error "No such language mode: %s" mode
))
694 (org-src--edit-element
695 element
(org-src--construct-edit-buffer-name (buffer-name) type
) mode t
))
698 (defun org-edit-src-code (&optional code edit-buffer-name
)
699 "Edit the source or example block at point.
701 The code is copied to a separate buffer and the appropriate mode
702 is turned on. When done, exit with \\[org-edit-src-exit]. This
703 will remove the original code in the Org buffer, and replace it
704 with the edited version. See `org-src-window-setup' to configure
705 the display of windows containing the Org buffer and the code
708 When optional argument CODE is a string, edit it in a dedicated
711 When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
712 name of the sub-editing buffer."
714 (let* ((element (org-element-at-point))
715 (type (org-element-type element
)))
716 (unless (and (memq type
'(example-block src-block
))
717 (org-src--on-element-p element
))
718 (user-error "Not in a source or example block"))
720 (if (eq type
'src-block
) (org-element-property :language element
)
722 (lang-f (and (eq type
'src-block
) (org-src--get-lang-mode lang
)))
723 (babel-info (and (eq type
'src-block
)
724 (org-babel-get-src-block-info 'light
)))
726 (when (and (eq type
'src-block
) (not (functionp lang-f
)))
727 (error "No such language mode: %s" lang-f
))
728 (org-src--edit-element
731 (org-src--construct-edit-buffer-name (buffer-name) lang
))
732 lang-f
(null code
) (and code
(org-unescape-code-in-string code
)))
734 (org-set-local 'org-coderef-label-format
735 (or (org-element-property :label-fmt element
)
736 org-coderef-label-format
))
737 (when (eq type
'src-block
)
738 (org-set-local 'org-src--babel-info babel-info
)
739 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
740 (when (fboundp edit-prep-func
)
741 (funcall edit-prep-func babel-info
))))
744 (defun org-edit-fixed-width-region ()
745 "Edit the fixed-width ASCII drawing at point.
747 This must be a region where each line starts with a colon
748 followed by a space or a newline character.
750 A new buffer is created and the fixed-width region is copied into
751 it, and the buffer is switched into the major mode defined in
752 `org-edit-fixed-width-region-mode', which see. When done, exit
753 with \\[org-edit-src-exit]. The edited text will then replace
754 the area in the Org mode buffer."
756 (let ((element (org-element-at-point)))
757 (unless (and (eq (org-element-type element
) 'fixed-width
)
758 (org-src--on-element-p element
))
759 (user-error "Not in a fixed-width area"))
760 (org-src--edit-element
762 (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
763 org-edit-fixed-width-region-mode
768 (defun org-edit-src-abort ()
769 "Abort editing of the src code and return to the Org buffer."
771 (let (org-src--allow-write-back-p) (org-edit-src-exit)))
773 (defun org-edit-src-continue (e)
774 "Unconditionally return to buffer editing area under point.
775 Throw an error if there is no such buffer."
778 (let ((buf (get-char-property (point) 'edit-buffer
)))
779 (if buf
(org-src-switch-to-buffer buf
'continue
)
780 (user-error "No sub-editing buffer for area at point"))))
782 (defun org-edit-src-save ()
783 "Save parent buffer with current state source-code buffer."
785 (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
786 (set-buffer-modified-p nil
)
787 (let ((edited-code (org-src--contents-for-write-back))
788 (beg org-src--beg-marker
)
789 (end org-src--end-marker
)
790 (overlay org-src--overlay
))
791 (with-current-buffer (org-src--source-buffer)
794 (delete-region beg end
)
795 (when (org-string-nw-p edited-code
) (insert edited-code
))
796 (unless (bolp) (insert "\n"))
797 (move-overlay overlay beg
(point))
800 (defun org-edit-src-exit ()
801 "Kill current sub-editing buffer and return to source buffer."
803 (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
804 (let* ((allow-write-back-p org-src--allow-write-back-p
)
805 (beg org-src--beg-marker
)
806 (end org-src--end-marker
)
807 (coordinates (org-src--coordinates (point) 1 (point-max)))
808 (code (and allow-write-back-p
(org-src--contents-for-write-back))))
809 (set-buffer-modified-p nil
)
810 ;; Switch to source buffer. Kill sub-editing buffer.
811 (let ((edit-buffer (current-buffer)))
812 (org-src-switch-to-buffer (marker-buffer beg
) 'exit
)
813 (kill-buffer edit-buffer
))
814 ;; Insert modified code. Ensure it ends with a newline character.
815 (when (and allow-write-back-p
816 (not (equal (buffer-substring-no-properties beg end
) code
)))
819 (delete-region beg end
)
820 (when (org-string-nw-p code
)
822 (unless (bolp) (insert "\n"))))
823 ;; If we are to return to source buffer, put point at an
824 ;; appropriate location. In particular, if block is hidden, move
825 ;; to the beginning of the block opening line.
828 ;; Block is hidden; move at start of block.
829 ((org-some (lambda (o) (eq (overlay-get o
'invisible
) 'org-hide-block
))
830 (overlays-at (point)))
831 (beginning-of-line 0))
832 (allow-write-back-p (org-src--goto-coordinates coordinates beg end
)))
833 ;; Clean up left-over markers and restore window configuration.
836 (when org-src--saved-temp-window-config
837 (set-window-configuration org-src--saved-temp-window-config
)
838 (setq org-src--saved-temp-window-config nil
))))
843 ;;; org-src.el ends here