1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004-2014 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 (eq beg org-src--beg-marker
)
220 (eq end org-src--end-marker
)
223 (defun org-src--source-buffer ()
224 "Return source buffer edited by current buffer."
225 (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
226 (or (marker-buffer org-src--beg-marker
)
227 (error "No source buffer available for current editing session")))
229 (defun org-src--get-lang-mode (lang)
230 "Return major mode that should be used for LANG.
231 LANG is a string, and the returned major mode is a symbol."
234 (let ((l (or (cdr (assoc lang org-src-lang-modes
)) lang
)))
235 (if (symbolp l
) (symbol-name l
) l
))
238 (defun org-src--coordinates (pos beg end
)
239 "Return coordinates of POS relatively to BEG and END.
240 POS, BEG and END are buffer positions. Return value is either
241 a cons cell (LINE . COLUMN) or symbol `end'. See also
242 `org-src--goto-coordinates'."
243 (if (>= pos end
) 'end
244 (org-with-wide-buffer
245 (goto-char (max beg pos
))
246 (cons (count-lines beg
(line-beginning-position))
247 ;; Column is relative to the end of line to avoid problems of
248 ;; comma escaping or colons appended in front of the line.
250 (progn (end-of-line) (current-column)))))))
252 (defun org-src--goto-coordinates (coord beg end
)
253 "Move to coordinates COORD relatively to BEG and END.
254 COORD are coordinates, as returned by `org-src--coordinates',
255 which see. BEG and END are buffer positions."
257 (if (eq coord
'end
) (max (1- end
) beg
)
258 ;; If BEG happens to be located outside of the narrowed part of
259 ;; the buffer, widen it first.
260 (org-with-wide-buffer
262 (forward-line (car coord
))
264 (org-move-to-column (max (+ (current-column) (cdr coord
)) 0))
267 (defun org-src--element-contents-area (element)
268 "Return contents boundaries of ELEMENT.
269 Return value is a pair (BEG . END) where BEG and END are buffer
271 (let ((blockp (memq (org-element-type element
)
272 '(example-block export-block src-block
))))
273 (cons (org-with-wide-buffer
274 (goto-char (org-element-property :post-affiliated element
))
275 (if blockp
(line-beginning-position 2) (point)))
276 (org-with-wide-buffer
277 (goto-char (org-element-property :end element
))
278 (skip-chars-backward " \r\t\n")
279 (line-beginning-position (if blockp
1 2))))))
281 (defun org-src--make-source-overlay (beg end edit-buffer
)
282 "Create overlay between BEG and END positions and return it.
283 EDIT-BUFFER is the buffer currently editing area between BEG and
285 (let ((overlay (make-overlay beg end
)))
286 (overlay-put overlay
'face
'secondary-selection
)
287 (overlay-put overlay
'edit-buffer edit-buffer
)
288 (overlay-put overlay
'help-echo
289 "Click with mouse-1 to switch to buffer editing this segment")
290 (overlay-put overlay
'face
'secondary-selection
)
291 (overlay-put overlay
'keymap
292 (let ((map (make-sparse-keymap)))
293 (define-key map
[mouse-1
] 'org-edit-src-continue
)
295 (overlay-put overlay
:read-only
"Leave me alone")
298 (defun org-src--on-element-p (element)
299 "Non-nil when point is on ELEMENT."
300 (and (>= (point) (org-element-property :begin element
))
302 (org-with-wide-buffer
303 (goto-char (org-element-property :end element
))
304 (skip-chars-backward " \r\t\n")
305 (line-end-position)))))
307 (defun org-src--contents-for-write-back ()
308 "Return buffer contents in a format appropriate for write back.
309 Assume point is in the corresponding edit buffer."
310 (let ((indentation (+ (or org-src--block-indentation
0)
311 (if (memq org-src--type
'(example-block src-block
))
312 org-edit-src-content-indentation
314 (preserve-indentation org-src-preserve-indentation
)
315 (contents (org-with-wide-buffer (buffer-string)))
316 (fixed-width-p (eq org-src--type
'fixed-width
)))
319 (goto-char (point-min))
322 (untabify (point-min) (point-max))
323 (let ((prefix (concat (and (not preserve-indentation
)
324 (make-string indentation ?\s
))
326 (while (not (eobp)) (insert prefix
) (forward-line))))
327 (unless preserve-indentation
(untabify (point-min) (point-max)))
328 (org-escape-code-in-region (point-min) (point-max))
329 (unless (or preserve-indentation
(zerop indentation
))
330 (let ((ind (make-string indentation ?\s
)))
332 (when (org-looking-at-p "[ \t]*\\S-") (insert ind
))
336 (defun org-src--edit-element (element name
&optional major write-back contents
)
337 "Edit ELEMENT contents in a dedicated buffer.
339 MAJOR is the major mode used in the edit buffer. A nil value is
340 equivalent to `fundamental-mode'. When WRITE-BACK is non-nil,
341 assume contents will replace original region. When CONTENTS is
342 non-nil, display them in the edit buffer. Otherwise, assume they
343 are located in property `:value'.
345 Leave point in edit buffer."
346 (setq org-src--saved-temp-window-config
(current-window-configuration))
347 (let* ((area (org-src--element-contents-area element
))
348 (beg (copy-marker (car area
)))
349 (end (copy-marker (cdr area
) t
))
350 (old-edit-buffer (org-src--edit-buffer beg end
)))
351 (if (and old-edit-buffer
352 (or (not org-src-ask-before-returning-to-edit-buffer
)
353 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ")))
354 ;; Move to existing buffer.
355 (org-src-switch-to-buffer old-edit-buffer
'return
)
356 ;; Discard old edit buffer.
357 (when old-edit-buffer
358 (with-current-buffer old-edit-buffer
359 (when (boundp 'org-src--overlay
)
360 (delete-overlay org-src--overlay
)))
361 (kill-buffer old-edit-buffer
))
362 (let* ((org-mode-p (derived-mode-p 'org-mode
))
363 (type (org-element-type element
))
364 (ind (org-with-wide-buffer
365 (goto-char (org-element-property :begin element
))
366 (org-get-indentation)))
368 (and (memq type
'(example-block src-block
))
369 (or (org-element-property :preserve-indent element
)
370 org-src-preserve-indentation
)))
371 ;; Store relative positions of mark (if any) and point
372 ;; within the edited area.
373 (point-coordinates (org-src--coordinates (point) beg end
))
374 (mark-coordinates (and (org-region-active-p)
376 (and (>= m beg
) (>= end m
)
377 (org-src--coordinates m beg end
)))))
378 ;; Generate a new edit buffer.
379 (buffer (generate-new-buffer name
))
380 ;; Add an overlay on top of source.
381 (overlay (org-src--make-source-overlay beg end buffer
)))
382 ;; Switch to edit buffer.
383 (org-src-switch-to-buffer buffer
'edit
)
385 (insert (or contents
(org-element-property :value element
)))
386 (remove-text-properties (point-min) (point-max)
387 '(display nil invisible nil intangible nil
))
388 (unless preserve-ind
(org-do-remove-indentation))
389 (set-buffer-modified-p nil
)
390 (setq buffer-file-name nil
)
392 (if (not major
) (fundamental-mode)
393 (let ((org-inhibit-startup t
))
394 (condition-case e
(funcall major
)
395 (error (message "Language mode `%s' fails with: %S"
397 ;; Transmit buffer-local variables for exit function. It must
398 ;; be done after initializing major mode, as this operation
399 ;; may reset them otherwise.
400 (org-set-local 'org-src--from-org-mode org-mode-p
)
401 (org-set-local 'org-src--beg-marker beg
)
402 (org-set-local 'org-src--end-marker end
)
403 (org-set-local 'org-src--type type
)
404 (org-set-local 'org-src--block-indentation ind
)
405 (org-set-local 'org-src-preserve-indentation preserve-ind
)
406 (org-set-local 'org-src--overlay overlay
)
407 (org-set-local 'org-src--allow-write-back-p write-back
)
410 (when org-edit-src-persistent-message
413 (substitute-command-keys
415 "Edit, then exit with \\[org-edit-src-exit] or abort with \
416 \\[org-edit-src-abort]"
417 "Exit with \\[org-edit-src-exit] or abort with \
418 \\[org-edit-src-abort]"))))
419 ;; Possibly start `auto-save-mode'.
420 (when org-edit-src-turn-on-auto-save
421 (setq buffer-auto-save-file-name
422 (concat (make-temp-name "org-src-")
423 (format-time-string "-%Y-%d-%m")
425 ;; Move mark and point in edit buffer to the corresponding
427 (when mark-coordinates
428 (org-src--goto-coordinates mark-coordinates
(point-min) (point-max))
429 (push-mark (point) 'no-message t
)
430 (setq deactivate-mark nil
))
431 (org-src--goto-coordinates point-coordinates
(point-min) (point-max)))
432 ;; Install idle auto save feature, if necessary.
433 (or org-src--code-timer
434 (zerop org-edit-src-auto-save-idle-delay
)
435 (setq org-src--code-timer
437 org-edit-src-auto-save-idle-delay t
440 ((org-src-edit-buffer-p)
441 (when (buffer-modified-p) (org-edit-src-save)))
442 ((not (org-some (lambda (b) (org-src-edit-buffer-p b
))
444 (cancel-timer org-src--code-timer
)
445 (setq org-src--code-timer nil
))))))))))
449 ;;; Fontification of source blocks
451 (defun org-src-font-lock-fontify-block (lang start end
)
453 This function is called by emacs automatic fontification, as long
454 as `org-src-fontify-natively' is non-nil."
455 (let ((lang-mode (org-src--get-lang-mode lang
)))
456 (when (fboundp lang-mode
)
457 (let ((string (buffer-substring-no-properties start end
))
458 (modified (buffer-modified-p))
459 (org-buffer (current-buffer)) pos next
)
460 (remove-text-properties start end
'(face nil
))
463 (concat " org-src-fontification:" (symbol-name lang-mode
)))
464 (delete-region (point-min) (point-max))
465 (insert string
" ") ;; so there's a final property change
466 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
467 (font-lock-fontify-buffer)
468 (setq pos
(point-min))
469 (while (setq next
(next-single-property-change pos
'face
))
471 (+ start
(1- pos
)) (1- (+ start next
)) 'face
472 (get-text-property pos
'face
) org-buffer
)
476 '(font-lock-fontified t fontified t font-lock-multiline t
))
477 (set-buffer-modified-p modified
)))))
483 (defun org-escape-code-in-region (beg end
)
484 "Escape lines between BEG and END.
485 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
486 \",#+\" by appending a comma to it."
490 (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t
)
491 (replace-match ",\\1" nil nil nil
1))))
493 (defun org-escape-code-in-string (s)
494 "Escape lines in string S.
495 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
496 \",#+\" by appending a comma to it."
497 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil
1))
499 (defun org-unescape-code-in-region (beg end
)
500 "Un-escape lines between BEG and END.
501 Un-escaping happens by removing the first comma on lines starting
502 with \",*\", \",#+\", \",,*\" and \",,#+\"."
506 (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t
)
507 (replace-match "" nil nil nil
1))))
509 (defun org-unescape-code-in-string (s)
510 "Un-escape lines in string S.
511 Un-escaping happens by removing the first comma on lines starting
512 with \",*\", \",#+\", \",,*\" and \",,#+\"."
513 (replace-regexp-in-string
514 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil
1))
518 ;;; Org src minor mode
520 (defvar org-src-mode-map
(make-sparse-keymap))
521 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
522 (define-key org-src-mode-map
"\C-c\C-k" 'org-edit-src-abort
)
523 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
525 (define-minor-mode org-src-mode
526 "Minor mode for language major mode buffers generated by org.
527 This minor mode is turned on in two situations:
528 - when editing a source code snippet with \"C-c '\".
529 - When formatting a source code snippet for export with htmlize.
530 There is a mode hook, and keybindings for `org-edit-src-exit' and
531 `org-edit-src-save'")
533 (defun org-src-mode-configure-edit-buffer ()
534 (when (org-bound-and-true-p org-src--from-org-mode
)
535 (org-add-hook 'kill-buffer-hook
536 (lambda () (delete-overlay org-src--overlay
)) nil
'local
)
537 (if (org-bound-and-true-p org-src--allow-write-back-p
)
539 (setq buffer-offer-save t
)
540 (setq buffer-file-name
541 (concat (buffer-file-name (marker-buffer org-src--beg-marker
))
542 "[" (buffer-name) "]"))
543 (if (featurep 'xemacs
)
545 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
546 (setq write-contents-hooks
'(org-edit-src-save)))
547 (setq write-contents-functions
'(org-edit-src-save))))
548 (setq buffer-read-only t
))))
550 (org-add-hook 'org-src-mode-hook
#'org-src-mode-configure-edit-buffer
)
554 ;;; Babel related functions
556 (defun org-src-associate-babel-session (info)
557 "Associate edit buffer with comint session."
559 (let ((session (cdr (assoc :session
(nth 2 info
)))))
560 (and session
(not (string= session
"none"))
561 (org-babel-comint-buffer-livep session
)
562 (let ((f (intern (format "org-babel-%s-associate-session"
564 (and (fboundp f
) (funcall f session
))))))
566 (defun org-src-babel-configure-edit-buffer ()
567 (when org-src--babel-info
568 (org-src-associate-babel-session org-src--babel-info
)))
570 (org-add-hook 'org-src-mode-hook
#'org-src-babel-configure-edit-buffer
)
572 (defmacro org-src-do-at-code-block
(&rest body
)
573 "Execute a command from an edit buffer in the Org mode buffer."
574 `(let ((beg-marker org-src--beg-marker
))
576 (with-current-buffer (marker-buffer beg-marker
)
577 (goto-char beg-marker
)
579 (def-edebug-spec org-src-do-at-code-block
(body))
581 (defun org-src-do-key-sequence-at-code-block (&optional key
)
582 "Execute key sequence at code block in the source Org buffer.
583 The command bound to KEY in the Org-babel key map is executed
584 remotely with point temporarily at the start of the code block in
587 This command is not bound to a key by default, to avoid conflicts
588 with language major mode bindings. To bind it to C-c @ in all
589 language major modes, you could use
591 (add-hook 'org-src-mode-hook
592 (lambda () (define-key org-src-mode-map \"\\C-c@\"
593 'org-src-do-key-sequence-at-code-block)))
595 In that case, for example, C-c @ t issued in code edit buffers
596 would tangle the current Org code block, C-c @ e would execute
597 the block and C-c @ h would display the other available
599 (interactive "kOrg-babel key: ")
600 (if (equal key
(kbd "C-g")) (keyboard-quit)
602 (org-src-do-at-code-block
603 (call-interactively (lookup-key org-babel-map key
)))))
609 (defun org-src-edit-buffer-p (&optional buffer
)
610 "Non-nil when current buffer is a source editing buffer.
611 If BUFFER is non-nil, test it instead."
612 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
613 (and (buffer-live-p buffer
)
614 (local-variable-p 'org-src--beg-marker buffer
)
615 (local-variable-p 'org-src--end-marker buffer
))))
617 (defun org-src-switch-to-buffer (buffer context
)
618 (case org-src-window-setup
619 (current-window (org-pop-to-buffer-same-window buffer
))
621 (switch-to-buffer-other-window buffer
))
625 (let ((frame (selected-frame)))
626 (switch-to-buffer-other-frame buffer
)
627 (delete-frame frame
)))
629 (kill-buffer (current-buffer))
630 (org-pop-to-buffer-same-window buffer
))
631 (t (switch-to-buffer-other-frame buffer
))))
633 (when (eq context
'edit
) (delete-other-windows))
634 (org-switch-to-buffer-other-window buffer
)
635 (when (eq context
'exit
) (delete-other-windows)))
636 (switch-invisibly (set-buffer buffer
))
638 (message "Invalid value %s for `org-src-window-setup'"
639 org-src-window-setup
)
640 (org-pop-to-buffer-same-window buffer
))))
642 (defun org-edit-table.el
()
643 "Edit \"table.el\" table at point.
645 A new buffer is created and the table is copied into it. Then
646 the table is recognized with `table-recognize'. When done
647 editing, exit with \\[org-edit-src-exit]. The edited text will
648 then replace the area in the Org mode buffer.
650 Throw an error when not at such a table."
652 (let ((element (org-element-at-point)))
653 (unless (and (eq (org-element-type element
) 'table
)
654 (eq (org-element-property :type element
) 'table.el
)
655 (org-src--on-element-p element
))
656 (user-error "Not in a table.el table"))
657 (org-src--edit-element
659 (org-src--construct-edit-buffer-name (buffer-name) "Table")
661 (when (org-bound-and-true-p flyspell-mode
) (flyspell-mode -
1))
665 (defun org-edit-export-block ()
666 "Edit export block at point.
668 A new buffer is created and the block is copied into it, and the
669 buffer is switched into an appropriate major mode. See also
670 `org-src-lang-modes'. When done, exit with
671 \\[org-edit-src-exit]. The edited text will then replace the
672 area in the Org mode buffer.
674 Throw an error when not at an export block."
676 (let ((element (org-element-at-point)))
677 (unless (and (eq (org-element-type element
) 'export-block
)
678 (org-src--on-element-p element
))
679 (user-error "Not in an export block"))
680 (let* ((type (downcase (org-element-property :type element
)))
681 (mode (org-src--get-lang-mode type
)))
682 (unless (functionp mode
) (error "No such language mode: %s" mode
))
683 (org-src--edit-element
684 element
(org-src--construct-edit-buffer-name (buffer-name) type
) mode t
))
687 (defun org-edit-src-code (&optional code edit-buffer-name
)
688 "Edit the source or example block at point.
690 The code is copied to a separate buffer and the appropriate mode
691 is turned on. When done, exit with \\[org-edit-src-exit]. This
692 will remove the original code in the Org buffer, and replace it
693 with the edited version. See `org-src-window-setup' to configure
694 the display of windows containing the Org buffer and the code
697 When optional argument CODE is a string, edit it in a dedicated
700 When optional argument EDIT-BUFFER-NAME is non-nil, use it as the
701 name of the sub-editing buffer."
703 (let* ((element (org-element-at-point))
704 (type (org-element-type element
)))
705 (unless (and (memq type
'(example-block src-block
))
706 (org-src--on-element-p element
))
707 (user-error "Not in a source or example block"))
709 (if (eq type
'src-block
) (org-element-property :language element
)
711 (lang-f (and (eq type
'src-block
) (org-src--get-lang-mode lang
)))
712 (babel-info (and (eq type
'src-block
)
713 (org-babel-get-src-block-info 'light
))))
714 (when (and (eq type
'src-block
) (not (functionp lang-f
)))
715 (error "No such language mode: %s" lang-f
))
716 (org-src--edit-element
719 (org-src--construct-edit-buffer-name (buffer-name) lang
))
720 lang-f
(null code
) (and code
(org-unescape-code-in-string code
)))
722 (org-set-local 'org-coderef-label-format
723 (or (org-element-property :label-fmt element
)
724 org-coderef-label-format
))
725 (when (eq type
'src-block
)
726 (org-set-local 'org-src--babel-info babel-info
)
727 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
728 (when (fboundp edit-prep-func
)
729 (funcall edit-prep-func babel-info
))))
732 (defun org-edit-fixed-width-region ()
733 "Edit the fixed-width ASCII drawing at point.
735 This must be a region where each line starts with a colon
736 followed by a space or a newline character.
738 A new buffer is created and the fixed-width region is copied into
739 it, and the buffer is switched into the major mode defined in
740 `org-edit-fixed-width-region-mode', which see. When done, exit
741 with \\[org-edit-src-exit]. The edited text will then replace
742 the area in the Org mode buffer."
744 (let ((element (org-element-at-point)))
745 (unless (and (eq (org-element-type element
) 'fixed-width
)
746 (org-src--on-element-p element
))
747 (user-error "Not in a fixed-width area"))
748 (org-src--edit-element
750 (org-src--construct-edit-buffer-name (buffer-name) "Fixed Width")
751 org-edit-fixed-width-region-mode
756 (defun org-edit-src-abort ()
757 "Abort editing of the src code and return to the Org buffer."
759 (let (org-src--allow-write-back-p) (org-edit-src-exit)))
761 (defun org-edit-src-continue (e)
762 "Unconditionally return to buffer editing area under point.
763 Throw an error if there is no such buffer."
766 (let ((buf (get-char-property (point) 'edit-buffer
)))
767 (if buf
(org-src-switch-to-buffer buf
'continue
)
768 (user-error "No sub-editing buffer for area at point"))))
770 (defun org-edit-src-save ()
771 "Save parent buffer with current state source-code buffer."
773 (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
774 (set-buffer-modified-p nil
)
775 (let ((edited-code (org-src--contents-for-write-back))
776 (beg org-src--beg-marker
)
777 (end org-src--end-marker
)
778 (overlay org-src--overlay
))
779 (with-current-buffer (org-src--source-buffer)
781 (delete-region beg end
)
782 (when (org-string-nw-p edited-code
) (insert edited-code
))
783 (unless (bolp) (insert "\n"))
784 (move-overlay overlay beg
(point))
787 (defun org-edit-src-exit ()
788 "Kill current sub-editing buffer and return to source buffer."
790 (unless (org-src-edit-buffer-p) (error "Not in a sub-editing buffer"))
791 (let* ((allow-write-back-p org-src--allow-write-back-p
)
792 (beg org-src--beg-marker
)
793 (end org-src--end-marker
)
794 (coordinates (org-src--coordinates (point) 1 (point-max)))
795 (code (and allow-write-back-p
(org-src--contents-for-write-back))))
796 (set-buffer-modified-p nil
)
797 ;; Switch to source buffer. Kill sub-editing buffer.
798 (let ((edit-buffer (current-buffer)))
799 (org-src-switch-to-buffer (marker-buffer beg
) 'exit
)
800 (kill-buffer edit-buffer
))
801 ;; Insert modified code. Ensure it ends with a newline character.
802 (when (and allow-write-back-p
803 (not (equal (buffer-substring-no-properties beg end
) code
)))
806 (delete-region beg end
)
807 (when (org-string-nw-p code
)
809 (unless (bolp) (insert "\n"))))
810 ;; If we are to return to source buffer, put point at an
811 ;; appropriate location. In particular, if block is hidden, move
812 ;; to the beginning of the block opening line.
815 ;; Block is hidden; move at start of block.
816 ((org-some (lambda (o) (eq (overlay-get o
'invisible
) 'org-hide-block
))
817 (overlays-at (point)))
818 (beginning-of-line 0))
819 (allow-write-back-p (org-src--goto-coordinates coordinates beg end
)))
820 ;; Clean up left-over markers and restore window configuration.
823 (when org-src--saved-temp-window-config
824 (set-window-configuration org-src--saved-temp-window-config
)
825 (setq org-src--saved-temp-window-config nil
))))
830 ;;; org-src.el ends here