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 t
)
198 (defvar org-src--remote nil
)
199 (defvar org-src--beg-marker nil
)
200 (defvar org-src--block-indentation nil
)
201 (defvar org-src--code-timer nil
)
202 (defvar org-src--end-marker nil
)
203 (defvar org-src--from-org-mode nil
)
204 (defvar org-src--overlay nil
)
205 (defvar org-src--saved-temp-window-config nil
)
206 (defvar org-src--type nil
)
207 (defvar org-src--babel-info nil
)
209 (defun org-src--construct-edit-buffer-name (org-buffer-name lang
)
210 "Construct the buffer name for a source editing buffer."
211 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
213 (defun org-src--edit-buffer (beg end
)
214 "Return buffer editing area between BEG and END.
215 Return nil if there is no such buffer."
217 (dolist (b (buffer-list))
218 (with-current-buffer b
219 (and (org-src-edit-buffer-p)
220 (= beg org-src--beg-marker
)
221 (eq (marker-buffer beg
) (marker-buffer org-src--beg-marker
))
222 (= end org-src--end-marker
)
223 (eq (marker-buffer end
) (marker-buffer org-src--end-marker
))
226 (defun org-src--source-buffer ()
227 "Return source buffer edited by current buffer."
228 (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
229 (or (marker-buffer org-src--beg-marker
)
230 (error "No source buffer available for current editing session")))
232 (defun org-src--get-lang-mode (lang)
233 "Return major mode that should be used for LANG.
234 LANG is a string, and the returned major mode is a symbol."
237 (let ((l (or (cdr (assoc lang org-src-lang-modes
)) lang
)))
238 (if (symbolp l
) (symbol-name l
) l
))
241 (defun org-src--coordinates (pos beg end
)
242 "Return coordinates of POS relatively to BEG and END.
243 POS, BEG and END are buffer positions. Return value is either
244 a cons cell (LINE . COLUMN) or symbol `end'. See also
245 `org-src--goto-coordinates'."
246 (if (>= pos end
) 'end
247 (org-with-wide-buffer
248 (goto-char (max beg pos
))
249 (cons (count-lines beg
(line-beginning-position))
250 ;; Column is relative to the end of line to avoid problems of
251 ;; comma escaping or colons appended in front of the line.
253 (progn (end-of-line) (current-column)))))))
255 (defun org-src--goto-coordinates (coord beg end
)
256 "Move to coordinates COORD relatively to BEG and END.
257 COORD are coordinates, as returned by `org-src--coordinates',
258 which see. BEG and END are buffer positions."
260 (if (eq coord
'end
) (max (1- end
) beg
)
261 ;; If BEG happens to be located outside of the narrowed part of
262 ;; the buffer, widen it first.
263 (org-with-wide-buffer
265 (forward-line (car coord
))
267 (org-move-to-column (max (+ (current-column) (cdr coord
)) 0))
270 (defun org-src--element-contents-area (element)
271 "Return contents boundaries of ELEMENT.
272 Return value is a pair (BEG . END) where BEG and END are buffer
274 (let ((type (org-element-type element
)))
276 ((eq type
'footnote-definition
)
277 (cons (org-with-wide-buffer
278 (goto-char (org-element-property :post-affiliated element
))
279 (search-forward "]"))
280 (org-element-property :contents-end element
)))
281 ((org-element-property :contents-begin element
)
282 (cons (org-element-property :contents-begin element
)
283 (org-element-property :contents-end element
)))
284 ((memq type
'(example-block export-block src-block
))
285 (cons (org-with-wide-buffer
286 (goto-char (org-element-property :post-affiliated element
))
287 (line-beginning-position 2))
288 (org-with-wide-buffer
289 (goto-char (org-element-property :end element
))
290 (skip-chars-backward " \r\t\n")
291 (line-beginning-position 1))))
293 (cons (org-element-property :post-affiliated element
)
294 (org-with-wide-buffer
295 (goto-char (org-element-property :end element
))
296 (skip-chars-backward " \r\t\n")
297 (line-beginning-position 2)))))))
299 (defun org-src--make-source-overlay (beg end edit-buffer
)
300 "Create overlay between BEG and END positions and return it.
301 EDIT-BUFFER is the buffer currently editing area between BEG and
303 (let ((overlay (make-overlay beg end
)))
304 (overlay-put overlay
'face
'secondary-selection
)
305 (overlay-put overlay
'edit-buffer edit-buffer
)
306 (overlay-put overlay
'help-echo
307 "Click with mouse-1 to switch to buffer editing this segment")
308 (overlay-put overlay
'face
'secondary-selection
)
309 (overlay-put overlay
'keymap
310 (let ((map (make-sparse-keymap)))
311 (define-key map
[mouse-1
] 'org-edit-src-continue
)
313 ;; TODO: The below line doesn't work for two reasons:
314 ;; - It should be 'read-only
315 ;; - 'read-only apparently doesn't work on overlays (also empirically tested):
316 ;; <https://lists.gnu.org/archive/html/emacs-devel/2008-01/msg01598.html>
317 ;; If this feature is to be kept, it should be implemented via text
318 ;; properties, which will require fiddling around in more places
319 ;; (such as when the contents are copied back into the buffer after
320 ;; editing is complete.)
321 (overlay-put overlay
:read-only
"Leave me alone")
324 (defun org-src--remove-overlay ()
325 "Remove overlay from current source buffer."
326 (when (overlayp org-src--overlay
) (delete-overlay org-src--overlay
)))
328 (defun org-src--on-element-p (element)
329 "Non-nil when point is on ELEMENT."
330 (and (>= (point) (org-element-property :begin element
))
332 (org-with-wide-buffer
333 (goto-char (org-element-property :end element
))
334 (skip-chars-backward " \r\t\n")
335 (line-end-position)))))
337 (defun org-src--contents-for-write-back ()
338 "Return buffer contents in a format appropriate for write back.
339 Assume point is in the corresponding edit buffer."
340 (let ((indentation (+ (or org-src--block-indentation
0)
341 (if (memq org-src--type
'(example-block src-block
))
342 org-edit-src-content-indentation
344 (preserve-indentation org-src-preserve-indentation
)
345 (contents (org-with-wide-buffer (buffer-string)))
346 (fixed-width-p (eq org-src--type
'fixed-width
))
347 (write-back org-src--allow-write-back
))
349 (insert (org-no-properties contents
))
350 (goto-char (point-min))
351 (when (functionp write-back
) (funcall write-back
))
352 (unless (or preserve-indentation
(= indentation
0))
353 (let ((ind (make-string indentation ?\s
)))
354 (goto-char (point-min))
356 (when (org-looking-at-p "[ \t]*\\S-") (insert ind
))
360 (defun org-src--edit-element
361 (element name
&optional major write-back contents remote
)
362 "Edit ELEMENT contents in a dedicated buffer NAME.
364 MAJOR is the major mode used in the edit buffer. A nil value is
365 equivalent to `fundamental-mode'.
367 When WRITE-BACK is non-nil, assume contents will replace original
368 region. If it is a function, applied in the edit buffer, from
369 point min, before returning the contents.
371 When CONTENTS is non-nil, display them in the edit buffer.
372 Otherwise, assume they are located in property `:value'.
374 When REMOTE is non-nil, do not try to preserve point or mark when
375 moving from the edit area to the source.
377 Leave point in edit buffer."
378 (setq org-src--saved-temp-window-config
(current-window-configuration))
379 (let* ((area (org-src--element-contents-area element
))
380 (beg (copy-marker (car area
)))
381 (end (copy-marker (cdr area
) t
))
382 (old-edit-buffer (org-src--edit-buffer beg end
)))
383 (if (and old-edit-buffer
384 (or (not org-src-ask-before-returning-to-edit-buffer
)
385 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
386 ;; Move to existing buffer.
387 (org-src-switch-to-buffer old-edit-buffer
'return
)
388 ;; Discard old edit buffer.
389 (when old-edit-buffer
390 (with-current-buffer old-edit-buffer
(org-src--remove-overlay))
391 (kill-buffer old-edit-buffer
))
392 (let* ((org-mode-p (derived-mode-p 'org-mode
))
393 (type (org-element-type element
))
394 (ind (org-with-wide-buffer
395 (goto-char (org-element-property :begin element
))
396 (org-get-indentation)))
398 (and (memq type
'(example-block src-block
))
399 (or (org-element-property :preserve-indent element
)
400 org-src-preserve-indentation
)))
401 ;; Store relative positions of mark (if any) and point
402 ;; within the edited area.
403 (point-coordinates (and (not remote
)
404 (org-src--coordinates (point) beg end
)))
405 (mark-coordinates (and (not remote
)
406 (org-region-active-p)
408 (and (>= m beg
) (>= end m
)
409 (org-src--coordinates m beg end
)))))
410 ;; Generate a new edit buffer.
411 (buffer (generate-new-buffer name
))
412 ;; Add an overlay on top of source.
413 (overlay (org-src--make-source-overlay beg end buffer
)))
414 ;; Switch to edit buffer.
415 (org-src-switch-to-buffer buffer
'edit
)
417 (insert (or contents
(org-element-property :value element
)))
418 (remove-text-properties (point-min) (point-max)
419 '(display nil invisible nil intangible nil
))
420 (unless preserve-ind
(org-do-remove-indentation))
421 (set-buffer-modified-p nil
)
422 (setq buffer-file-name nil
)
424 (if (not major
) (fundamental-mode)
425 (let ((org-inhibit-startup t
))
426 (condition-case e
(funcall major
)
427 (error (message "Language mode `%s' fails with: %S"
429 ;; Transmit buffer-local variables for exit function. It must
430 ;; be done after initializing major mode, as this operation
431 ;; may reset them otherwise.
432 (org-set-local 'org-src--from-org-mode org-mode-p
)
433 (org-set-local 'org-src--beg-marker beg
)
434 (org-set-local 'org-src--end-marker end
)
435 (org-set-local 'org-src--type type
)
436 (org-set-local 'org-src--remote remote
)
437 (org-set-local 'org-src--block-indentation ind
)
438 (org-set-local 'org-src-preserve-indentation preserve-ind
)
439 (org-set-local 'org-src--overlay overlay
)
440 (org-set-local 'org-src--allow-write-back write-back
)
443 (when org-edit-src-persistent-message
446 (substitute-command-keys
448 "Edit, then exit with \\[org-edit-src-exit] or abort with \
449 \\[org-edit-src-abort]"
450 "Exit with \\[org-edit-src-exit] or abort with \
451 \\[org-edit-src-abort]"))))
452 ;; Possibly start `auto-save-mode'.
453 (when org-edit-src-turn-on-auto-save
454 (setq buffer-auto-save-file-name
455 (concat (make-temp-name "org-src-")
456 (format-time-string "-%Y-%d-%m")
458 ;; Move mark and point in edit buffer to the corresponding
460 (when mark-coordinates
461 (org-src--goto-coordinates mark-coordinates
(point-min) (point-max))
462 (push-mark (point) 'no-message t
)
463 (setq deactivate-mark nil
))
465 (org-src--goto-coordinates
466 point-coordinates
(point-min) (point-max))
467 (goto-char (or (text-property-any
468 (point-min) (point-max) 'read-only nil
)
470 (skip-chars-forward " \r\t\n")))
471 ;; Install idle auto save feature, if necessary.
472 (or org-src--code-timer
473 (zerop org-edit-src-auto-save-idle-delay
)
474 (setq org-src--code-timer
476 org-edit-src-auto-save-idle-delay t
479 ((org-src-edit-buffer-p)
480 (when (buffer-modified-p) (org-edit-src-save)))
481 ((not (org-some (lambda (b) (org-src-edit-buffer-p b
))
483 (cancel-timer org-src--code-timer
)
484 (setq org-src--code-timer nil
))))))))))
488 ;;; Fontification of source blocks
490 (defun org-src-font-lock-fontify-block (lang start end
)
492 This function is called by emacs automatic fontification, as long
493 as `org-src-fontify-natively' is non-nil."
494 (let ((lang-mode (org-src--get-lang-mode lang
)))
495 (when (fboundp lang-mode
)
496 (let ((string (buffer-substring-no-properties start end
))
497 (modified (buffer-modified-p))
498 (org-buffer (current-buffer)) pos next
)
499 (remove-text-properties start end
'(face nil
))
502 (concat " org-src-fontification:" (symbol-name lang-mode
)))
503 (delete-region (point-min) (point-max))
504 (insert string
" ") ;; so there's a final property change
505 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
506 (font-lock-fontify-buffer)
507 (setq pos
(point-min))
508 (while (setq next
(next-single-property-change pos
'face
))
510 (+ start
(1- pos
)) (1- (+ start next
)) 'face
511 (get-text-property pos
'face
) org-buffer
)
515 '(font-lock-fontified t fontified t font-lock-multiline t
))
516 (set-buffer-modified-p modified
)))))
522 (defun org-escape-code-in-region (beg end
)
523 "Escape lines between BEG and END.
524 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
525 \",#+\" by appending a comma to it."
529 (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t
)
530 (replace-match ",\\1" nil nil nil
1))))
532 (defun org-escape-code-in-string (s)
533 "Escape lines in string S.
534 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
535 \",#+\" by appending a comma to it."
536 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil
1))
538 (defun org-unescape-code-in-region (beg end
)
539 "Un-escape lines between BEG and END.
540 Un-escaping happens by removing the first comma on lines starting
541 with \",*\", \",#+\", \",,*\" and \",,#+\"."
545 (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t
)
546 (replace-match "" nil nil nil
1))))
548 (defun org-unescape-code-in-string (s)
549 "Un-escape lines in string S.
550 Un-escaping happens by removing the first comma on lines starting
551 with \",*\", \",#+\", \",,*\" and \",,#+\"."
552 (replace-regexp-in-string
553 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil
1))
557 ;;; Org src minor mode
559 (defvar org-src-mode-map
(make-sparse-keymap))
560 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
561 (define-key org-src-mode-map
"\C-c\C-k" 'org-edit-src-abort
)
562 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
564 (define-minor-mode org-src-mode
565 "Minor mode for language major mode buffers generated by org.
566 This minor mode is turned on in two situations:
567 - when editing a source code snippet with \"C-c '\".
568 - When formatting a source code snippet for export with htmlize.
569 There is a mode hook, and keybindings for `org-edit-src-exit' and
570 `org-edit-src-save'")
572 (defun org-src-mode-configure-edit-buffer ()
573 (when (org-bound-and-true-p org-src--from-org-mode
)
574 (org-add-hook 'kill-buffer-hook
#'org-src--remove-overlay nil
'local
)
575 (if (org-bound-and-true-p org-src--allow-write-back
)
577 (setq buffer-offer-save t
)
578 (setq buffer-file-name
579 (concat (buffer-file-name (marker-buffer org-src--beg-marker
))
580 "[" (buffer-name) "]"))
581 (if (featurep 'xemacs
)
583 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
584 (setq write-contents-hooks
'(org-edit-src-save)))
585 (setq write-contents-functions
'(org-edit-src-save))))
586 (setq buffer-read-only t
))))
588 (org-add-hook 'org-src-mode-hook
#'org-src-mode-configure-edit-buffer
)
592 ;;; Babel related functions
594 (defun org-src-associate-babel-session (info)
595 "Associate edit buffer with comint session."
597 (let ((session (cdr (assoc :session
(nth 2 info
)))))
598 (and session
(not (string= session
"none"))
599 (org-babel-comint-buffer-livep session
)
600 (let ((f (intern (format "org-babel-%s-associate-session"
602 (and (fboundp f
) (funcall f session
))))))
604 (defun org-src-babel-configure-edit-buffer ()
605 (when org-src--babel-info
606 (org-src-associate-babel-session org-src--babel-info
)))
608 (org-add-hook 'org-src-mode-hook
#'org-src-babel-configure-edit-buffer
)
610 (defmacro org-src-do-at-code-block
(&rest body
)
611 "Execute a command from an edit buffer in the Org mode buffer."
612 `(let ((beg-marker org-src--beg-marker
))
614 (with-current-buffer (marker-buffer beg-marker
)
615 (goto-char beg-marker
)
617 (def-edebug-spec org-src-do-at-code-block
(body))
619 (defun org-src-do-key-sequence-at-code-block (&optional key
)
620 "Execute key sequence at code block in the source Org buffer.
621 The command bound to KEY in the Org-babel key map is executed
622 remotely with point temporarily at the start of the code block in
625 This command is not bound to a key by default, to avoid conflicts
626 with language major mode bindings. To bind it to C-c @ in all
627 language major modes, you could use
629 (add-hook 'org-src-mode-hook
630 (lambda () (define-key org-src-mode-map \"\\C-c@\"
631 'org-src-do-key-sequence-at-code-block)))
633 In that case, for example, C-c @ t issued in code edit buffers
634 would tangle the current Org code block, C-c @ e would execute
635 the block and C-c @ h would display the other available
637 (interactive "kOrg-babel key: ")
638 (if (equal key
(kbd "C-g")) (keyboard-quit)
640 (org-src-do-at-code-block
641 (call-interactively (lookup-key org-babel-map key
)))))
647 (defun org-src-edit-buffer-p (&optional buffer
)
648 "Non-nil when current buffer is a source editing buffer.
649 If BUFFER is non-nil, test it instead."
650 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
651 (and (buffer-live-p buffer
)
652 (local-variable-p 'org-src--beg-marker buffer
)
653 (local-variable-p 'org-src--end-marker buffer
))))
655 (defun org-src-switch-to-buffer (buffer context
)
656 (case org-src-window-setup
657 (current-window (org-pop-to-buffer-same-window buffer
))
659 (switch-to-buffer-other-window buffer
))
663 (let ((frame (selected-frame)))
664 (switch-to-buffer-other-frame buffer
)
665 (delete-frame frame
)))
667 (kill-buffer (current-buffer))
668 (org-pop-to-buffer-same-window buffer
))
669 (t (switch-to-buffer-other-frame buffer
))))
671 (when (eq context
'edit
) (delete-other-windows))
672 (org-switch-to-buffer-other-window buffer
)
673 (when (eq context
'exit
) (delete-other-windows)))
674 (switch-invisibly (set-buffer buffer
))
676 (message "Invalid value %s for `org-src-window-setup'"
677 org-src-window-setup
)
678 (org-pop-to-buffer-same-window buffer
))))
680 (defun org-edit-footnote-reference ()
681 "Edit definition of footnote reference at point."
683 (let ((context (org-element-context)))
684 (unless (and (eq (org-element-type context
) 'footnote-reference
)
686 (org-with-wide-buffer
687 (goto-char (org-element-property :end context
))
688 (skip-chars-backward " \t")
690 (user-error "Not on a footnote reference"))
691 (let* ((label (org-element-property :label context
))
693 (org-with-wide-buffer
694 (org-footnote-goto-definition label
)
696 (org-element-at-point))))
697 (unless (eq (org-element-type definition
) 'footnote-definition
)
698 (user-error "Cannot edit remotely inline footnotes"))
699 (org-src--edit-element
700 definition
(format "*Edit footnote [%s]*" label
)
702 (lambda () (delete-region (point) (search-forward "]")))
704 (org-propertize (format "[%s]" label
)
705 'read-only
"Cannot edit footnote label"
708 (org-with-wide-buffer
709 (buffer-substring-no-properties
711 (goto-char (org-element-property :contents-begin definition
))
712 (skip-chars-backward " \r\t\n")
714 (org-element-property :contents-end definition
))))
719 (defun org-edit-table.el
()
720 "Edit \"table.el\" table at point.
722 A new buffer is created and the table is copied into it. Then
723 the table is recognized with `table-recognize'. When done
724 editing, exit with \\[org-edit-src-exit]. The edited text will
725 then replace the area in the Org mode buffer.
727 Throw an error when not at such a table."
729 (let ((element (org-element-at-point)))
730 (unless (and (eq (org-element-type element
) 'table
)
731 (eq (org-element-property :type element
) 'table.el
)
732 (org-src--on-element-p element
))
733 (user-error "Not in a table.el table"))
734 (org-src--edit-element
736 (org-src--construct-edit-buffer-name (buffer-name) "Table")
738 (when (org-bound-and-true-p flyspell-mode
) (flyspell-mode -
1))
742 (defun org-edit-export-block ()
743 "Edit export block at point.
745 A new buffer is created and the block is copied into it, and the
746 buffer is switched into an appropriate major mode. See also
747 `org-src-lang-modes'. When done, exit with
748 \\[org-edit-src-exit]. The edited text will then replace the
749 area in the Org mode buffer.
751 Throw an error when not at an export block."
753 (let ((element (org-element-at-point)))
754 (unless (and (eq (org-element-type element
) 'export-block
)
755 (org-src--on-element-p element
))
756 (user-error "Not in an export block"))
757 (let* ((type (downcase (org-element-property :type element
)))
758 (mode (org-src--get-lang-mode type
)))
759 (unless (functionp mode
) (error "No such language mode: %s" mode
))
760 (org-src--edit-element
762 (org-src--construct-edit-buffer-name (buffer-name) type
)
764 (lambda () (org-escape-code-in-region (point-min) (point-max)))))
767 (defun org-edit-src-code (&optional code edit-buffer-name
)
768 "Edit the source or example block at point.
770 The code is copied to a separate buffer and the appropriate mode
771 is turned on. When done, exit with \\[org-edit-src-exit]. This
772 will remove the original code in the Org buffer, and replace it
773 with the edited version. See `org-src-window-setup' to configure
774 the display of windows containing the Org buffer and the code
777 When optional argument CODE is a string, edit it in a dedicated
780 When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
781 name of the sub-editing buffer."
783 (let* ((element (org-element-at-point))
784 (type (org-element-type element
)))
785 (unless (and (memq type
'(example-block src-block
))
786 (org-src--on-element-p element
))
787 (user-error "Not in a source or example block"))
789 (if (eq type
'src-block
) (org-element-property :language element
)
791 (lang-f (and (eq type
'src-block
) (org-src--get-lang-mode lang
)))
792 (babel-info (and (eq type
'src-block
)
793 (org-babel-get-src-block-info 'light
)))
795 (when (and (eq type
'src-block
) (not (functionp lang-f
)))
796 (error "No such language mode: %s" lang-f
))
797 (org-src--edit-element
800 (org-src--construct-edit-buffer-name (buffer-name) lang
))
804 (unless org-src-preserve-indentation
805 (untabify (point-min) (point-max)))
806 (org-escape-code-in-region (point-min) (point-max))))
807 (and code
(org-unescape-code-in-string code
)))
809 (org-set-local 'org-coderef-label-format
810 (or (org-element-property :label-fmt element
)
811 org-coderef-label-format
))
812 (when (eq type
'src-block
)
813 (org-set-local 'org-src--babel-info babel-info
)
814 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
815 (when (fboundp edit-prep-func
)
816 (funcall edit-prep-func babel-info
))))
819 (defun org-edit-fixed-width-region ()
820 "Edit the fixed-width ASCII drawing at point.
822 This must be a region where each line starts with a colon
823 followed by a space or a newline character.
825 A new buffer is created and the fixed-width region is copied into
826 it, and the buffer is switched into the major mode defined in
827 `org-edit-fixed-width-region-mode', which see. When done, exit
828 with \\[org-edit-src-exit]. The edited text will then replace
829 the area in the Org mode buffer."
831 (let ((element (org-element-at-point)))
832 (unless (and (eq (org-element-type element
) 'fixed-width
)
833 (org-src--on-element-p element
))
834 (user-error "Not in a fixed-width area"))
835 (org-src--edit-element
837 (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
838 org-edit-fixed-width-region-mode
839 (lambda () (while (not (eobp)) (insert ": ") (forward-line))))
843 (defun org-edit-src-abort ()
844 "Abort editing of the src code and return to the Org buffer."
846 (let (org-src--allow-write-back) (org-edit-src-exit)))
848 (defun org-edit-src-continue (e)
849 "Unconditionally return to buffer editing area under point.
850 Throw an error if there is no such buffer."
853 (let ((buf (get-char-property (point) 'edit-buffer
)))
854 (if buf
(org-src-switch-to-buffer buf
'continue
)
855 (user-error "No sub-editing buffer for area at point"))))
857 (defun org-edit-src-save ()
858 "Save parent buffer with current state source-code buffer."
860 (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
861 (set-buffer-modified-p nil
)
862 (let ((edited-code (org-src--contents-for-write-back))
863 (beg org-src--beg-marker
)
864 (end org-src--end-marker
)
865 (overlay org-src--overlay
))
866 (with-current-buffer (org-src--source-buffer)
869 (delete-region beg end
)
870 (when (org-string-nw-p edited-code
) (insert edited-code
))
871 (unless (bolp) (insert "\n"))
872 (move-overlay overlay beg
(point))
875 (defun org-edit-src-exit ()
876 "Kill current sub-editing buffer and return to source buffer."
878 (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
879 (let* ((beg org-src--beg-marker
)
880 (end org-src--end-marker
)
881 (write-back org-src--allow-write-back
)
882 (remote org-src--remote
)
883 (coordinates (and (not remote
)
884 (org-src--coordinates (point) 1 (point-max))))
885 (code (and write-back
(org-src--contents-for-write-back))))
886 (set-buffer-modified-p nil
)
887 ;; Switch to source buffer. Kill sub-editing buffer.
888 (let ((edit-buffer (current-buffer)))
889 (org-src-switch-to-buffer (marker-buffer beg
) 'exit
)
890 (kill-buffer edit-buffer
))
891 ;; Insert modified code. Ensure it ends with a newline character.
892 (org-with-wide-buffer
893 (when (and write-back
(not (equal (buffer-substring beg end
) code
)))
896 (delete-region beg end
)
897 (when (org-string-nw-p code
)
899 (unless (bolp) (insert "\n")))))
900 ;; If we are to return to source buffer, put point at an
901 ;; appropriate location. In particular, if block is hidden, move
902 ;; to the beginning of the block opening line.
906 ;; Block is hidden; move at start of block.
907 ((org-some (lambda (o) (eq (overlay-get o
'invisible
) 'org-hide-block
))
908 (overlays-at (point)))
909 (beginning-of-line 0))
910 (write-back (org-src--goto-coordinates coordinates beg end
))))
911 ;; Clean up left-over markers and restore window configuration.
914 (when org-src--saved-temp-window-config
915 (set-window-configuration org-src--saved-temp-window-config
)
916 (setq org-src--saved-temp-window-config nil
))))
921 ;;; org-src.el ends here