1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Bastien Guerry <bzg AT gnu DOT 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-at-table.el-p
"org" ())
42 (declare-function org-in-src-block-p
"org" (&optional inside
))
43 (declare-function org-in-block-p
"org" (names))
44 (declare-function org-get-indentation
"org" (&optional line
))
45 (declare-function org-switch-to-buffer-other-window
"org" (&rest args
))
46 (declare-function org-pop-to-buffer-same-window
47 "org-compat" (&optional buffer-or-name norecord label
))
48 (declare-function org-base-buffer
"org" (buffer))
50 (defcustom org-edit-src-region-extra nil
51 "Additional regexps to identify regions for editing with `org-edit-src-code'.
52 For examples see the function `org-edit-src-find-region-and-lang'.
53 The regular expression identifying the begin marker should end with a newline,
54 and the regexp marking the end line should start with a newline, to make sure
55 there are kept outside the narrowed region."
56 :group
'org-edit-structure
59 (regexp :tag
"begin regexp")
60 (regexp :tag
"end regexp")
61 (choice :tag
"language"
62 (string :tag
"specify")
63 (integer :tag
"from match group")
64 (const :tag
"from `lang' element")
65 (const :tag
"from `style' element")))))
67 (defcustom org-edit-src-auto-save-idle-delay
5
68 "Delay of idle time before auto-saving src code buffers.
69 When a positive integer N, save after N seconds of idle time.
70 When 0 (the default), don't auto-save."
71 :group
'org-edit-structure
75 (defcustom org-coderef-label-format
"(ref:%s)"
76 "The default coderef format.
77 This format string will be used to search for coderef labels in literal
78 examples (EXAMPLE and SRC blocks). The format can be overwritten in
79 an individual literal example with the -l option, like
81 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
85 If you want to use this for HTML export, make sure that the format does
86 not introduce special font-locking, and avoid the HTML special
87 characters `<', `>', and `&'. The reason for this restriction is that
88 the labels are searched for only after htmlize has done its job."
89 :group
'org-edit-structure
; FIXME this is not in the right group
92 (defcustom org-edit-fixed-width-region-mode
'artist-mode
93 "The mode that should be used to edit fixed-width regions.
94 These are the regions where each line starts with a colon."
95 :group
'org-edit-structure
99 (const fundamental-mode
)
100 (function :tag
"Other (specify)")))
102 (defcustom org-src-preserve-indentation nil
103 "If non-nil preserve leading whitespace characters on export.
104 If non-nil leading whitespace characters in source code blocks
105 are preserved on export, and when switching between the org
106 buffer and the language mode edit buffer. If this variable is nil
107 then, after editing with \\[org-edit-src-code], the
108 minimum (across-lines) number of leading whitespace characters
109 are removed from all lines, and the code block is uniformly
110 indented according to the value of `org-edit-src-content-indentation'."
111 :group
'org-edit-structure
114 (defcustom org-edit-src-content-indentation
2
115 "Indentation for the content of a source code block.
116 This should be the number of spaces added to the indentation of the #+begin
117 line in order to compute the indentation of the block content after
118 editing it with \\[org-edit-src-code]. Has no effect if
119 `org-src-preserve-indentation' is non-nil."
120 :group
'org-edit-structure
123 (defvar org-src-strip-leading-and-trailing-blank-lines nil
124 "If non-nil, blank lines are removed when exiting the code edit buffer.")
126 (defcustom org-edit-src-persistent-message t
127 "Non-nil means show persistent exit help message while editing src examples.
128 The message is shown in the header-line, which will be created in the
129 first line of the window showing the editing buffer."
130 :group
'org-edit-structure
133 (defcustom org-src-window-setup
'reorganize-frame
134 "How the source code edit buffer should be displayed.
135 Possible values for this option are:
137 current-window Show edit buffer in the current window, keeping all other
139 other-window Use `switch-to-buffer-other-window' to display edit buffer.
140 reorganize-frame Show only two windows on the current frame, the current
141 window and the edit buffer. When exiting the edit buffer,
142 return to one window.
143 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
144 Also, when exiting the edit buffer, kill that frame."
145 :group
'org-edit-structure
147 (const current-window
)
150 (const reorganize-frame
)))
152 (defvar org-src-mode-hook nil
153 "Hook run after Org switched a source code snippet to its Emacs mode.
156 - when editing a source code snippet with \"C-c '\".
157 - When formatting a source code snippet for export with htmlize.
159 You may want to use this hook for example to turn off `outline-minor-mode'
160 or similar things which you want to have when editing a source code file,
161 but which mess up the display of a snippet in Org exported files.")
163 (defcustom org-src-lang-modes
164 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
165 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
166 ("calc" . fundamental
) ("C" . c
) ("cpp" . c
++)
167 ("screen" . shell-script
))
168 "Alist mapping languages to their major mode.
169 The key is the language name, the value is the string that should
170 be inserted as the name of the major mode. For many languages this is
171 simple, but for language where this is not the case, this variable
172 provides a way to simplify things on the user side.
173 For example, there is no ocaml-mode in Emacs, but the mode to use is
175 :group
'org-edit-structure
178 (string "Language name")
179 (symbol "Major mode"))))
181 ;;; Editing source examples
183 (defvar org-src-mode-map
(make-sparse-keymap))
184 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
185 (define-key org-src-mode-map
"\C-c\C-k" 'org-edit-src-abort
)
186 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
188 (defvar org-edit-src-force-single-line nil
)
189 (defvar org-edit-src-from-org-mode nil
)
190 (defvar org-edit-src-allow-write-back-p t
)
191 (defvar org-edit-src-picture nil
)
192 (defvar org-edit-src-beg-marker nil
)
193 (defvar org-edit-src-end-marker nil
)
194 (defvar org-edit-src-overlay nil
)
195 (defvar org-edit-src-block-indentation nil
)
196 (defvar org-edit-src-saved-temp-window-config nil
)
198 (defcustom org-src-ask-before-returning-to-edit-buffer t
199 "If nil, when org-edit-src code is used on a block that already
200 has an active edit buffer, it will switch to that edit buffer
201 immediately; otherwise it will ask whether you want to return to
202 the existing edit buffer."
203 :group
'org-edit-structure
207 (defvar org-src-babel-info nil
)
209 (define-minor-mode org-src-mode
210 "Minor mode for language major mode buffers generated by org.
211 This minor mode is turned on in two situations:
212 - when editing a source code snippet with \"C-c '\".
213 - When formatting a source code snippet for export with htmlize.
214 There is a mode hook, and keybindings for `org-edit-src-exit' and
215 `org-edit-src-save'")
217 (defvar org-edit-src-code-timer nil
)
218 (defun org-edit-src-code (&optional context code edit-buffer-name
)
219 "Edit the source CODE block at point.
220 The code is copied to a separate buffer and the appropriate mode
221 is turned on. When done, exit with \\[org-edit-src-exit]. This will
222 remove the original code in the Org buffer, and replace it with the
223 edited version. An optional argument CONTEXT is used by \\[org-edit-src-save]
224 when calling this function. See `org-src-window-setup' to configure
225 the display of windows containing the Org buffer and the code buffer."
227 (if (not (or (org-in-block-p '("src" "example" "latex" "html"))
228 (org-at-table.el-p
)))
229 (user-error "Not in a source code or example block")
230 (unless (eq context
'save
)
231 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
232 (let* ((mark (and (org-region-active-p) (mark)))
235 ;; If the src region consists in no lines, we insert a blank
237 (let* ((temp (org-edit-src-find-region-and-lang))
240 (if (>= end beg
) temp
243 (org-edit-src-find-region-and-lang))))
244 (full-info (org-babel-get-src-block-info 'light
))
245 (org-mode-p (derived-mode-p 'org-mode
)) ;; derived-mode-p is reflexive
247 ;; Move marker with inserted text for case when src block is
248 ;; just one empty line, i.e. beg == end.
249 (end (copy-marker (make-marker) t
))
250 (allow-write-back-p (null code
))
251 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
252 begline markline markcol line col transmitted-variables
)
253 (setq beg
(move-marker beg
(nth 0 info
))
254 end
(move-marker end
(nth 1 info
))
255 msg
(if allow-write-back-p
256 (substitute-command-keys
257 "Edit, then exit with C-c ' (C-c and single quote) -- C-c C-k to abort")
258 "Exit with C-c ' (C-c and single quote) -- C-c C-k to abort")
259 code
(or code
(buffer-substring-no-properties beg end
))
260 lang
(or (cdr (assoc (nth 2 info
) org-src-lang-modes
))
262 lang
(if (symbolp lang
) (symbol-name lang
) lang
)
264 block-nindent
(nth 5 info
)
265 lang-f
(intern (concat lang
"-mode"))
266 begline
(save-excursion (goto-char beg
) (org-current-line))
267 transmitted-variables
268 `((org-edit-src-content-indentation
269 ,org-edit-src-content-indentation
)
270 (org-edit-src-force-single-line ,single
)
271 (org-edit-src-from-org-mode ,org-mode-p
)
272 (org-edit-src-allow-write-back-p ,allow-write-back-p
)
273 (org-src-preserve-indentation ,org-src-preserve-indentation
)
274 (org-src-babel-info ,(org-babel-get-src-block-info 'light
))
275 (org-coderef-label-format
276 ,(or (nth 4 info
) org-coderef-label-format
))
277 (org-edit-src-beg-marker ,beg
)
278 (org-edit-src-end-marker ,end
)
279 (org-edit-src-block-indentation ,block-nindent
)))
280 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
281 (save-excursion (goto-char (min mark end
))
282 (setq markline
(org-current-line)
283 markcol
(current-column))))
284 (if (equal lang-f
'table.el-mode
)
285 (setq lang-f
(lambda ()
287 (if (org-bound-and-true-p flyspell-mode
)
290 (org-set-local 'org-edit-src-content-indentation
0))))
291 (unless (functionp lang-f
)
292 (error "No such language mode: %s" lang-f
))
294 (if (> (point) end
) (goto-char end
))
295 (setq line
(org-current-line)
296 col
(current-column)))
297 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
298 (or (eq context
'save
)
299 (if org-src-ask-before-returning-to-edit-buffer
300 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ") t
)))
301 (org-src-switch-to-buffer buffer
'return
)
303 (with-current-buffer buffer
304 (if (boundp 'org-edit-src-overlay
)
305 (delete-overlay org-edit-src-overlay
)))
306 (kill-buffer buffer
))
307 (setq buffer
(generate-new-buffer
309 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
310 (setq ovl
(make-overlay beg end
))
311 (overlay-put ovl
'edit-buffer buffer
)
312 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
313 (overlay-put ovl
'face
'secondary-selection
)
316 (let ((map (make-sparse-keymap)))
317 (define-key map
[mouse-1
] 'org-edit-src-continue
)
319 (overlay-put ovl
:read-only
"Leave me alone")
320 (setq transmitted-variables
321 (append transmitted-variables
`((org-edit-src-overlay ,ovl
))))
322 (org-src-switch-to-buffer buffer
'edit
)
323 (if (eq single
'macro-definition
)
324 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
326 (remove-text-properties (point-min) (point-max)
327 '(display nil invisible nil intangible nil
))
328 (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables
))
329 (setq total-nindent
(or (org-do-remove-indentation) 0)))
330 (let ((org-inhibit-startup t
))
334 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
335 (dolist (pair transmitted-variables
)
336 (org-set-local (car pair
) (cadr pair
)))
337 ;; Remove protecting commas from visible part of buffer.
338 (org-unescape-code-in-region (point-min) (point-max))
340 (org-goto-line (1+ (- markline begline
)))
342 (if org-src-preserve-indentation markcol
343 (max 0 (- markcol total-nindent
))))
344 (push-mark (point) 'no-message t
)
345 (setq deactivate-mark nil
))
346 (org-goto-line (1+ (- line begline
)))
348 (if org-src-preserve-indentation col
(max 0 (- col total-nindent
))))
350 (set-buffer-modified-p nil
)
351 (setq buffer-file-name nil
)
352 (and org-edit-src-persistent-message
353 (org-set-local 'header-line-format msg
))
354 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
355 (when (fboundp edit-prep-func
)
356 (funcall edit-prep-func full-info
)))
357 (or org-edit-src-code-timer
358 (setq org-edit-src-code-timer
359 (unless (zerop org-edit-src-auto-save-idle-delay
)
361 org-edit-src-auto-save-idle-delay t
364 ((and (string-match "\*Org Src" (buffer-name))
370 (string-match "\*Org Src" (buffer-name b
)))
372 (cancel-timer org-edit-src-code-timer
)
373 (setq org-edit-src-code-timer
)))))))))
376 (defun org-edit-src-continue (e)
377 "Continue editing source blocks." ;; Fixme: be more accurate
380 (let ((buf (get-char-property (point) 'edit-buffer
)))
381 (if buf
(org-src-switch-to-buffer buf
'continue
)
382 (error "Something is wrong here"))))
384 (defun org-src-switch-to-buffer (buffer context
)
385 (case org-src-window-setup
387 (org-pop-to-buffer-same-window buffer
))
389 (switch-to-buffer-other-window buffer
))
393 (let ((frame (selected-frame)))
394 (switch-to-buffer-other-frame buffer
)
395 (delete-frame frame
)))
397 (kill-buffer (current-buffer))
398 (org-pop-to-buffer-same-window buffer
))
400 (switch-to-buffer-other-frame buffer
))))
402 (if (eq context
'edit
) (delete-other-windows))
403 (org-switch-to-buffer-other-window buffer
)
404 (if (eq context
'exit
) (delete-other-windows)))
408 (message "Invalid value %s for org-src-window-setup"
409 (symbol-name org-src-window-setup
))
410 (org-pop-to-buffer-same-window buffer
))))
412 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
413 "Construct the buffer name for a source editing buffer."
414 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
416 (defun org-src-edit-buffer-p (&optional buffer
)
417 "Test whether BUFFER (or the current buffer if BUFFER is nil)
418 is a source block editing buffer."
419 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
420 (and (buffer-name buffer
)
421 (string-match "\\`*Org Src " (buffer-name buffer
))
422 (local-variable-p 'org-edit-src-beg-marker buffer
)
423 (local-variable-p 'org-edit-src-end-marker buffer
))))
425 (defun org-edit-src-find-buffer (beg end
)
426 "Find a source editing buffer that is already editing the region BEG to END."
430 (with-current-buffer b
431 (if (and (string-match "\\`*Org Src " (buffer-name))
432 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
433 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
434 (equal beg org-edit-src-beg-marker
)
435 (equal end org-edit-src-end-marker
))
436 (throw 'exit
(current-buffer)))))
440 (defun org-edit-fixed-width-region ()
441 "Edit the fixed-width ascii drawing at point.
442 This must be a region where each line starts with a colon followed by
444 An new buffer is created and the fixed-width region is copied into it,
445 and the buffer is switched into `artist-mode' for editing. When done,
446 exit with \\[org-edit-src-exit]. The edited text will then replace
447 the fragment in the Org-mode buffer."
449 (let ((line (org-current-line))
450 (col (current-column))
452 (msg (substitute-command-keys
453 "Edit, then exit with C-c ' (C-c and single quote) -- C-c C-k to abort"))
454 (org-mode-p (derived-mode-p 'org-mode
))
457 (preserve-indentation org-src-preserve-indentation
)
458 block-nindent ovl beg1 end1 code begline buffer
)
459 (beginning-of-line 1)
460 (if (looking-at "[ \t]*[^:\n \t]")
462 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
463 (setq beg1
(point) end1 beg1
)
465 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
466 (setq beg1
(point-at-bol 2))
467 (setq beg1
(point))))
469 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
470 (setq end1
(1- (match-beginning 0)))
471 (setq end1
(point))))
472 (org-goto-line line
))
473 (setq beg
(move-marker beg beg1
)
474 end
(move-marker end end1
)
475 code
(buffer-substring-no-properties beg end
)
476 begline
(save-excursion (goto-char beg
) (org-current-line)))
477 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
478 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? "))
479 (org-pop-to-buffer-same-window buffer
)
481 (with-current-buffer buffer
482 (if (boundp 'org-edit-src-overlay
)
483 (delete-overlay org-edit-src-overlay
)))
484 (kill-buffer buffer
))
485 (setq buffer
(generate-new-buffer
486 (org-src-construct-edit-buffer-name
487 (buffer-name) "Fixed Width")))
488 (setq ovl
(make-overlay beg end
))
489 (overlay-put ovl
'face
'secondary-selection
)
490 (overlay-put ovl
'edit-buffer buffer
)
491 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
492 (overlay-put ovl
'face
'secondary-selection
)
495 (let ((map (make-sparse-keymap)))
496 (define-key map
[mouse-1
] 'org-edit-src-continue
)
498 (overlay-put ovl
:read-only
"Leave me alone")
499 (org-pop-to-buffer-same-window buffer
)
501 (remove-text-properties (point-min) (point-max)
502 '(display nil invisible nil intangible nil
))
503 (setq block-nindent
(or (org-do-remove-indentation) 0))
505 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
508 (t (funcall org-edit-fixed-width-region-mode
)))
509 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
510 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
511 (set (make-local-variable 'org-edit-src-picture
) t
)
512 (goto-char (point-min))
513 (while (re-search-forward "^[ \t]*: ?" nil t
)
515 (org-goto-line (1+ (- line begline
)))
516 (org-move-to-column (max 0 (- col block-nindent
2)))
517 (org-set-local 'org-edit-src-beg-marker beg
)
518 (org-set-local 'org-edit-src-end-marker end
)
519 (org-set-local 'org-edit-src-overlay ovl
)
520 (org-set-local 'org-edit-src-block-indentation block-nindent
)
521 (org-set-local 'org-edit-src-content-indentation
0)
522 (org-set-local 'org-src-preserve-indentation nil
)
524 (set-buffer-modified-p nil
)
525 (and org-edit-src-persistent-message
526 (org-set-local 'header-line-format msg
)))
530 (defun org-edit-src-find-region-and-lang ()
531 "Find the region and language for a local edit.
532 Return a list with beginning and end of the region, a string representing
533 the language, a switch telling if the content should be in a single line."
536 org-edit-src-region-extra
538 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
539 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
540 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
541 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
542 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
543 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
544 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
545 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
546 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
547 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
548 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
549 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
550 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
551 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
552 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
553 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
554 "\n" "fundamental" macro-definition
)
557 re1 re2 single beg end lang lfmt match-re1 ind entry
)
559 (when (org-at-table.el-p
)
560 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
561 (setq beg
(1+ (point-at-eol)))
563 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
564 (progn (goto-char (point-max)) (newline)))
565 (setq end
(1- (point-at-bol)))
566 (throw 'exit
(list beg end
'table.el nil nil
0)))
567 (while (setq entry
(pop re-list
))
568 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
569 single
(nth 3 entry
))
571 (if (or (looking-at re1
)
572 (re-search-backward re1 nil t
))
574 (setq match-re1
(match-string 0))
575 (setq beg
(match-end 0)
576 lang
(org-edit-src-get-lang lang
)
577 lfmt
(org-edit-src-get-label-format match-re1
)
578 ind
(org-edit-src-get-indentation (match-beginning 0)))
579 (if (and (re-search-forward re2 nil t
)
580 (>= (match-end 0) pos
))
581 (throw 'exit
(list beg
(match-beginning 0)
582 lang single lfmt ind
))))
583 (if (or (looking-at re2
)
584 (re-search-forward re2 nil t
))
586 (setq end
(match-beginning 0))
587 (if (and (re-search-backward re1 nil t
)
588 (<= (match-beginning 0) pos
))
590 (setq lfmt
(org-edit-src-get-label-format
592 ind
(org-edit-src-get-indentation
593 (match-beginning 0)))
595 (list (match-end 0) end
596 (org-edit-src-get-lang lang
)
597 single lfmt ind
))))))))))))
599 (defun org-edit-src-get-lang (lang)
600 "Extract the src language."
601 (let ((m (match-string 0)))
603 ((stringp lang
) lang
)
604 ((integerp lang
) (match-string lang
))
605 ((and (eq lang
'lang
)
606 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
608 ((and (eq lang
'style
)
609 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
613 (defun org-edit-src-get-label-format (s)
614 "Extract the label format."
616 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
617 (match-string 1 s
))))
619 (defun org-edit-src-get-indentation (pos)
620 "Count leading whitespace characters on line."
623 (org-get-indentation)))
625 (defun org-escape-code-in-region (beg end
)
626 "Escape lines between BEG and END.
627 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
628 \",#+\" by appending a comma to it."
632 (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t
)
633 (replace-match ",\\1" nil nil nil
1))))
635 (defun org-escape-code-in-string (s)
636 "Escape lines in string S.
637 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
638 \",#+\" by appending a comma to it."
639 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil
1))
641 (defun org-unescape-code-in-region (beg end
)
642 "Un-escape lines between BEG and END.
643 Un-escaping happens by removing the first comma on lines starting
644 with \",*\", \",#+\", \",,*\" and \",,#+\"."
648 (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t
)
649 (replace-match "" nil nil nil
1))))
651 (defun org-unescape-code-in-string (s)
652 "Un-escape lines in string S.
653 Un-escaping happens by removing the first comma on lines starting
654 with \",*\", \",#+\", \",,*\" and \",,#+\"."
655 (replace-regexp-in-string
656 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil
1))
658 (defun org-edit-src-exit (&optional context
)
659 "Exit special edit and protect problematic lines."
661 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
662 (error "This is not a sub-editing buffer, something is wrong"))
664 (let* ((fixed-width-p (string-match "Fixed Width" (buffer-name)))
665 (beg org-edit-src-beg-marker
)
666 (end org-edit-src-end-marker
)
667 (ovl org-edit-src-overlay
)
668 (bufstr (buffer-string))
669 (buffer (current-buffer))
670 (single (org-bound-and-true-p org-edit-src-force-single-line
))
671 (macro (eq single
'macro-definition
))
672 (total-nindent (+ (or org-edit-src-block-indentation
0)
673 org-edit-src-content-indentation
))
674 (preserve-indentation org-src-preserve-indentation
)
675 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
676 (delta 0) code line col indent
)
677 (when allow-write-back-p
678 (unless preserve-indentation
(untabify (point-min) (point-max)))
679 (if org-src-strip-leading-and-trailing-blank-lines
681 (goto-char (point-min))
682 (if (looking-at "[ \t\n]*\n") (replace-match ""))
684 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
685 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
688 col
(current-column))
689 (when allow-write-back-p
691 (goto-char (point-min))
692 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
693 (goto-char (point-min))
695 (while (re-search-forward "\n" nil t
)
697 (replace-match (if macro
"\\n" " ") t t
))
698 (when (and macro
(> cnt
0))
699 (goto-char (point-max)) (insert "\\n")))
700 (goto-char (point-min))
701 (if (looking-at "\\s-*") (replace-match " ")))
702 (when (and (org-bound-and-true-p org-edit-src-from-org-mode
)
704 (org-escape-code-in-region (point-min) (point-max))
708 (if (looking-at "[ \t]*\\(,,\\)?\\(\\*\\|#+\\)") 1
710 (when (org-bound-and-true-p org-edit-src-picture
)
711 (setq preserve-indentation nil
)
712 (untabify (point-min) (point-max))
713 (goto-char (point-min))
714 (while (re-search-forward "^" nil t
)
715 (replace-match ": ")))
716 (unless (or single preserve-indentation
(= total-nindent
0))
717 (setq indent
(make-string total-nindent ?\
))
718 (goto-char (point-min))
719 (while (re-search-forward "^" nil t
)
720 (replace-match indent
)))
721 (if (org-bound-and-true-p org-edit-src-picture
)
722 (setq total-nindent
(+ total-nindent
2)))
723 (setq code
(buffer-string))
724 (when (eq context
'save
)
727 (set-buffer-modified-p nil
))
728 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
729 (if (eq context
'save
) (save-buffer)
730 (with-current-buffer buffer
731 (set-buffer-modified-p nil
))
732 (kill-buffer buffer
))
734 (when allow-write-back-p
735 (delete-region beg
(max beg end
))
736 (unless (string-match "\\`[ \t]*\\'" code
)
739 (if single
(just-one-space)))
740 (if (memq t
(mapcar (lambda (overlay)
741 (eq (overlay-get overlay
'invisible
)
743 (overlays-at (point))))
744 ;; Block is hidden; put point at start of block
745 (beginning-of-line 0)
746 ;; Block is visible, put point where it was in the code buffer
747 (when allow-write-back-p
748 (org-goto-line (1- (+ (org-current-line) line
)))
749 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
)))))
750 (unless (eq context
'save
)
751 (move-marker beg nil
)
752 (move-marker end nil
)))
753 (when org-edit-src-code-timer
754 (cancel-timer org-edit-src-code-timer
)
755 (setq org-edit-src-code-timer nil
))
756 (unless (eq context
'save
)
757 (when org-edit-src-saved-temp-window-config
758 (set-window-configuration org-edit-src-saved-temp-window-config
)
759 (setq org-edit-src-saved-temp-window-config nil
))))
761 (defun org-edit-src-abort ()
762 "Abort editing of the src code and return to the Org buffer."
764 (let (org-edit-src-allow-write-back-p)
765 (org-edit-src-exit 'exit
)))
767 (defmacro org-src-in-org-buffer
(&rest body
)
768 `(let ((p (point)) (m (mark)) (ul buffer-undo-list
) msg
)
769 (save-window-excursion
770 (org-edit-src-exit 'save
)
772 (setq msg
(current-message))
773 (if (eq org-src-window-setup
'other-frame
)
774 (let ((org-src-window-setup 'current-window
))
775 (org-edit-src-code 'save
))
776 (org-edit-src-code 'save
)))
777 (setq buffer-undo-list ul
)
778 (push-mark m
'nomessage
)
779 (goto-char (min p
(point-max)))
780 (message (or msg
""))))
781 (def-edebug-spec org-src-in-org-buffer
(body))
783 (defun org-edit-src-save ()
784 "Save parent buffer with current state source-code buffer."
786 (if (string-match "Fixed Width" (buffer-name))
787 (user-error "Use C-c ' to save and exit, C-c C-k to abort editing")
788 (org-src-in-org-buffer (save-buffer))))
790 (declare-function org-babel-tangle
"ob-tangle" (&optional only-this-block target-file lang
))
792 (defun org-src-tangle (arg)
793 "Tangle the parent buffer."
795 (org-src-in-org-buffer (org-babel-tangle arg
)))
797 (defun org-src-mode-configure-edit-buffer ()
798 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
799 (org-add-hook 'kill-buffer-hook
800 #'(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
801 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
803 (setq buffer-offer-save t
)
804 (setq buffer-file-name
805 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
806 "[" (buffer-name) "]"))
807 (if (featurep 'xemacs
)
809 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
810 (setq write-contents-hooks
'(org-edit-src-save)))
811 (setq write-contents-functions
'(org-edit-src-save))))
812 (setq buffer-read-only t
))))
814 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
817 (defun org-src-associate-babel-session (info)
818 "Associate edit buffer with comint session."
820 (let ((session (cdr (assoc :session
(nth 2 info
)))))
821 (and session
(not (string= session
"none"))
822 (org-babel-comint-buffer-livep session
)
823 ((lambda (f) (and (fboundp f
) (funcall f session
)))
824 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
826 (defun org-src-babel-configure-edit-buffer ()
827 (when org-src-babel-info
828 (org-src-associate-babel-session org-src-babel-info
)))
830 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
831 (defmacro org-src-do-at-code-block
(&rest body
)
832 "Execute a command from an edit buffer in the Org-mode buffer."
833 `(let ((beg-marker org-edit-src-beg-marker
))
835 (with-current-buffer (marker-buffer beg-marker
)
836 (goto-char (marker-position beg-marker
))
838 (def-edebug-spec org-src-do-at-code-block
(body))
840 (defun org-src-do-key-sequence-at-code-block (&optional key
)
841 "Execute key sequence at code block in the source Org buffer.
842 The command bound to KEY in the Org-babel key map is executed
843 remotely with point temporarily at the start of the code block in
846 This command is not bound to a key by default, to avoid conflicts
847 with language major mode bindings. To bind it to C-c @ in all
848 language major modes, you could use
850 (add-hook 'org-src-mode-hook
851 (lambda () (define-key org-src-mode-map \"\\C-c@\"
852 'org-src-do-key-sequence-at-code-block)))
854 In that case, for example, C-c @ t issued in code edit buffers
855 would tangle the current Org code block, C-c @ e would execute
856 the block and C-c @ h would display the other available
858 (interactive "kOrg-babel key: ")
859 (if (equal key
(kbd "C-g")) (keyboard-quit)
861 (org-src-do-at-code-block
863 (lookup-key org-babel-map key
)))))
865 (defcustom org-src-tab-acts-natively nil
866 "If non-nil, the effect of TAB in a code block is as if it were
867 issued in the language major mode buffer."
872 (defun org-src-native-tab-command-maybe ()
873 "Perform language-specific TAB action.
874 Alter code block according to effect of TAB in the language major
876 (and org-src-tab-acts-natively
877 (not (equal this-command
'org-shifttab
))
878 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
879 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
881 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
883 (defun org-src-font-lock-fontify-block (lang start end
)
885 This function is called by emacs automatic fontification, as long
886 as `org-src-fontify-natively' is non-nil. For manual
887 fontification of code blocks see `org-src-fontify-block' and
888 `org-src-fontify-buffer'"
889 (let ((lang-mode (org-src-get-lang-mode lang
)))
890 (if (fboundp lang-mode
)
891 (let ((string (buffer-substring-no-properties start end
))
892 (modified (buffer-modified-p))
893 (org-buffer (current-buffer)) pos next
)
894 (remove-text-properties start end
'(face nil
))
897 (concat " org-src-fontification:" (symbol-name lang-mode
)))
898 (delete-region (point-min) (point-max))
899 (insert string
" ") ;; so there's a final property change
900 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
901 (font-lock-fontify-buffer)
902 (setq pos
(point-min))
903 (while (setq next
(next-single-property-change pos
'face
))
905 (+ start
(1- pos
)) (1- (+ start next
)) 'face
906 (get-text-property pos
'face
) org-buffer
)
910 '(font-lock-fontified t fontified t font-lock-multiline t
))
911 (set-buffer-modified-p modified
)))))
913 (defun org-src-fontify-block ()
914 "Fontify code block at point."
917 (let ((org-src-fontify-natively t
)
918 (info (org-edit-src-find-region-and-lang)))
919 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
921 (defun org-src-fontify-buffer ()
922 "Fontify all code blocks in the current buffer."
924 (org-babel-map-src-blocks nil
925 (org-src-fontify-block)))
927 (defun org-src-get-lang-mode (lang)
928 "Return major mode that should be used for LANG.
929 LANG is a string, and the returned major mode is a symbol."
932 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
933 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
937 ;;; org-src.el ends here