1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004-2012 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-get-indentation
"org" (&optional line
))
43 (declare-function org-switch-to-buffer-other-window
"org" (&rest args
))
44 (declare-function org-pop-to-buffer-same-window
45 "org-compat" (&optional buffer-or-name norecord label
))
47 (defcustom org-edit-src-region-extra nil
48 "Additional regexps to identify regions for editing with `org-edit-src-code'.
49 For examples see the function `org-edit-src-find-region-and-lang'.
50 The regular expression identifying the begin marker should end with a newline,
51 and the regexp marking the end line should start with a newline, to make sure
52 there are kept outside the narrowed region."
53 :group
'org-edit-structure
56 (regexp :tag
"begin regexp")
57 (regexp :tag
"end regexp")
58 (choice :tag
"language"
59 (string :tag
"specify")
60 (integer :tag
"from match group")
61 (const :tag
"from `lang' element")
62 (const :tag
"from `style' element")))))
64 (defcustom org-coderef-label-format
"(ref:%s)"
65 "The default coderef format.
66 This format string will be used to search for coderef labels in literal
67 examples (EXAMPLE and SRC blocks). The format can be overwritten in
68 an individual literal example with the -l option, like
70 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
74 If you want to use this for HTML export, make sure that the format does
75 not introduce special font-locking, and avoid the HTML special
76 characters `<', `>', and `&'. The reason for this restriction is that
77 the labels are searched for only after htmlize has done its job."
78 :group
'org-edit-structure
; FIXME this is not in the right group
81 (defcustom org-edit-fixed-width-region-mode
'artist-mode
82 "The mode that should be used to edit fixed-width regions.
83 These are the regions where each line starts with a colon."
84 :group
'org-edit-structure
88 (const fundamental-mode
)
89 (function :tag
"Other (specify)")))
91 (defcustom org-src-preserve-indentation nil
92 "If non-nil preserve leading whitespace characters on export.
93 If non-nil leading whitespace characters in source code blocks
94 are preserved on export, and when switching between the org
95 buffer and the language mode edit buffer. If this variable is nil
96 then, after editing with \\[org-edit-src-code], the
97 minimum (across-lines) number of leading whitespace characters
98 are removed from all lines, and the code block is uniformly
99 indented according to the value of `org-edit-src-content-indentation'."
100 :group
'org-edit-structure
103 (defcustom org-edit-src-content-indentation
2
104 "Indentation for the content of a source code block.
105 This should be the number of spaces added to the indentation of the #+begin
106 line in order to compute the indentation of the block content after
107 editing it with \\[org-edit-src-code]. Has no effect if
108 `org-src-preserve-indentation' is non-nil."
109 :group
'org-edit-structure
112 (defvar org-src-strip-leading-and-trailing-blank-lines nil
113 "If non-nil, blank lines are removed when exiting the code edit
116 (defcustom org-edit-src-persistent-message t
117 "Non-nil means show persistent exit help message while editing src examples.
118 The message is shown in the header-line, which will be created in the
119 first line of the window showing the editing buffer."
120 :group
'org-edit-structure
123 (defcustom org-src-window-setup
'reorganize-frame
124 "How the source code edit buffer should be displayed.
125 Possible values for this option are:
127 current-window Show edit buffer in the current window, keeping all other
129 other-window Use `switch-to-buffer-other-window' to display edit buffer.
130 reorganize-frame Show only two windows on the current frame, the current
131 window and the edit buffer. When exiting the edit buffer,
132 return to one window.
133 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
134 Also, when exiting the edit buffer, kill that frame."
135 :group
'org-edit-structure
137 (const current-window
)
140 (const reorganize-frame
)))
142 (defvar org-src-mode-hook nil
143 "Hook run after Org switched a source code snippet to its Emacs mode.
146 - when editing a source code snippet with \"C-c '\".
147 - When formatting a source code snippet for export with htmlize.
149 You may want to use this hook for example to turn off `outline-minor-mode'
150 or similar things which you want to have when editing a source code file,
151 but which mess up the display of a snippet in Org exported files.")
153 (defcustom org-src-lang-modes
154 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
155 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
156 ("calc" . fundamental
) ("C" . c
) ("cpp" . c
++)
157 ("screen" . shell-script
))
158 "Alist mapping languages to their major mode.
159 The key is the language name, the value is the string that should
160 be inserted as the name of the major mode. For many languages this is
161 simple, but for language where this is not the case, this variable
162 provides a way to simplify things on the user side.
163 For example, there is no ocaml-mode in Emacs, but the mode to use is
165 :group
'org-edit-structure
168 (string "Language name")
169 (symbol "Major mode"))))
171 ;;; Editing source examples
173 (defvar org-src-mode-map
(make-sparse-keymap))
174 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
176 (defvar org-edit-src-force-single-line nil
)
177 (defvar org-edit-src-from-org-mode nil
)
178 (defvar org-edit-src-allow-write-back-p t
)
179 (defvar org-edit-src-picture nil
)
180 (defvar org-edit-src-beg-marker nil
)
181 (defvar org-edit-src-end-marker nil
)
182 (defvar org-edit-src-overlay nil
)
183 (defvar org-edit-src-block-indentation nil
)
184 (defvar org-edit-src-saved-temp-window-config nil
)
186 (defvar org-src-ask-before-returning-to-edit-buffer t
187 "If nil, when org-edit-src code is used on a block that already
188 has an active edit buffer, it will switch to that edit buffer
189 immediately; otherwise it will ask whether you want to return
190 to the existing edit buffer.")
192 (defvar org-src-babel-info nil
)
194 (define-minor-mode org-src-mode
195 "Minor mode for language major mode buffers generated by org.
196 This minor mode is turned on in two situations:
197 - when editing a source code snippet with \"C-c '\".
198 - When formatting a source code snippet for export with htmlize.
199 There is a mode hook, and keybindings for `org-edit-src-exit' and
200 `org-edit-src-save'")
202 (defun org-edit-src-code (&optional context code edit-buffer-name
)
203 "Edit the source code example at point.
204 The example is copied to a separate buffer, and that buffer is
205 switched to the correct language mode. When done, exit with
206 \\[org-edit-src-exit]. This will remove the original code in the
207 Org buffer, and replace it with the edited version. Optional
208 argument CONTEXT is used by \\[org-edit-src-save] when calling
209 this function. See \\[org-src-window-setup] to configure the
210 display of windows containing the Org buffer and the code
213 (unless (eq context
'save
)
214 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
215 (let* ((mark (and (org-region-active-p) (mark)))
217 (info (org-edit-src-find-region-and-lang))
218 (full-info (org-babel-get-src-block-info 'light
))
219 (org-mode-p (derived-mode-p 'org-mode
)) ;; derived-mode-p is reflexive
222 (allow-write-back-p (null code
))
223 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
224 begline markline markcol line col transmitted-variables
)
227 (setq beg
(move-marker beg
(nth 0 info
))
228 end
(move-marker end
(nth 1 info
))
229 msg
(if allow-write-back-p
230 (substitute-command-keys
231 "Edit, then exit with C-c ' (C-c and single quote)")
232 "Exit with C-c ' (C-c and single quote)")
233 code
(or code
(buffer-substring-no-properties beg end
))
234 lang
(or (cdr (assoc (nth 2 info
) org-src-lang-modes
))
236 lang
(if (symbolp lang
) (symbol-name lang
) lang
)
238 block-nindent
(nth 5 info
)
239 lang-f
(intern (concat lang
"-mode"))
240 begline
(save-excursion (goto-char beg
) (org-current-line))
241 transmitted-variables
242 `((org-edit-src-content-indentation
243 ,org-edit-src-content-indentation
)
244 (org-edit-src-force-single-line ,single
)
245 (org-edit-src-from-org-mode ,org-mode-p
)
246 (org-edit-src-allow-write-back-p ,allow-write-back-p
)
247 (org-src-preserve-indentation ,org-src-preserve-indentation
)
248 (org-src-babel-info ,(org-babel-get-src-block-info 'light
))
249 (org-coderef-label-format
250 ,(or (nth 4 info
) org-coderef-label-format
))
251 (org-edit-src-beg-marker ,beg
)
252 (org-edit-src-end-marker ,end
)
253 (org-edit-src-block-indentation ,block-nindent
)))
254 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
255 (save-excursion (goto-char (min mark end
))
256 (setq markline
(org-current-line)
257 markcol
(current-column))))
258 (if (equal lang-f
'table.el-mode
)
259 (setq lang-f
(lambda ()
261 (if (org-bound-and-true-p flyspell-mode
)
264 (org-set-local 'org-edit-src-content-indentation
0))))
265 (unless (functionp lang-f
)
266 (error "No such language mode: %s" lang-f
))
268 (if (> (point) end
) (goto-char end
))
269 (setq line
(org-current-line)
270 col
(current-column)))
271 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
272 (if org-src-ask-before-returning-to-edit-buffer
273 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t
))
274 (org-src-switch-to-buffer buffer
'return
)
276 (with-current-buffer buffer
277 (if (boundp 'org-edit-src-overlay
)
278 (delete-overlay org-edit-src-overlay
)))
279 (kill-buffer buffer
))
280 (setq buffer
(generate-new-buffer
282 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
283 (setq ovl
(make-overlay beg end
))
284 (overlay-put ovl
'edit-buffer buffer
)
285 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
286 (overlay-put ovl
'face
'secondary-selection
)
289 (let ((map (make-sparse-keymap)))
290 (define-key map
[mouse-1
] 'org-edit-src-continue
)
292 (overlay-put ovl
:read-only
"Leave me alone")
293 (setq transmitted-variables
294 (append transmitted-variables
`((org-edit-src-overlay ,ovl
))))
295 (org-src-switch-to-buffer buffer
'edit
)
296 (if (eq single
'macro-definition
)
297 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
299 (remove-text-properties (point-min) (point-max)
300 '(display nil invisible nil intangible nil
))
301 (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables
))
302 (setq total-nindent
(or (org-do-remove-indentation) 0)))
303 (let ((org-inhibit-startup t
))
307 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
308 (dolist (pair transmitted-variables
)
309 (org-set-local (car pair
) (cadr pair
)))
310 (if (eq major-mode
'org-mode
)
312 (goto-char (point-min))
313 (while (re-search-forward "^," nil t
)
314 (if (eq (org-current-line) line
) (setq total-nindent
(1+ total-nindent
)))
316 (org-strip-protective-commas (point-min) (point-max)))
318 (org-goto-line (1+ (- markline begline
)))
320 (if org-src-preserve-indentation markcol
321 (max 0 (- markcol total-nindent
))))
322 (push-mark (point) 'no-message t
)
323 (setq deactivate-mark nil
))
324 (org-goto-line (1+ (- line begline
)))
326 (if org-src-preserve-indentation col
(max 0 (- col total-nindent
))))
328 (set-buffer-modified-p nil
)
329 (and org-edit-src-persistent-message
330 (org-set-local 'header-line-format msg
))
331 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
332 (when (fboundp edit-prep-func
)
333 (funcall edit-prep-func full-info
))))
336 (defun org-edit-src-continue (e)
339 (let ((buf (get-char-property (point) 'edit-buffer
)))
340 (if buf
(org-src-switch-to-buffer buf
'continue
)
341 (error "Something is wrong here"))))
343 (defun org-src-switch-to-buffer (buffer context
)
344 (case org-src-window-setup
346 (org-pop-to-buffer-same-window buffer
))
348 (switch-to-buffer-other-window buffer
))
352 (let ((frame (selected-frame)))
353 (switch-to-buffer-other-frame buffer
)
354 (delete-frame frame
)))
356 (kill-buffer (current-buffer))
357 (org-pop-to-buffer-same-window buffer
))
359 (switch-to-buffer-other-frame buffer
))))
361 (if (eq context
'edit
) (delete-other-windows))
362 (org-switch-to-buffer-other-window buffer
)
363 (if (eq context
'exit
) (delete-other-windows)))
367 (message "Invalid value %s for org-src-window-setup"
368 (symbol-name org-src-window-setup
))
369 (org-pop-to-buffer-same-window buffer
))))
371 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
372 "Construct the buffer name for a source editing buffer."
373 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
375 (defun org-src-edit-buffer-p (&optional buffer
)
376 "Test whether BUFFER (or the current buffer if BUFFER is nil)
377 is a source block editing buffer."
378 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
379 (and (buffer-name buffer
)
380 (string-match "\\`*Org Src " (buffer-name buffer
))
381 (local-variable-p 'org-edit-src-beg-marker buffer
)
382 (local-variable-p 'org-edit-src-end-marker buffer
))))
384 (defun org-edit-src-find-buffer (beg end
)
385 "Find a source editing buffer that is already editing the region BEG to END."
389 (with-current-buffer b
390 (if (and (string-match "\\`*Org Src " (buffer-name))
391 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
392 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
393 (equal beg org-edit-src-beg-marker
)
394 (equal end org-edit-src-end-marker
))
395 (throw 'exit
(current-buffer)))))
399 (defun org-edit-fixed-width-region ()
400 "Edit the fixed-width ascii drawing at point.
401 This must be a region where each line starts with a colon followed by
403 An new buffer is created and the fixed-width region is copied into it,
404 and the buffer is switched into `artist-mode' for editing. When done,
405 exit with \\[org-edit-src-exit]. The edited text will then replace
406 the fragment in the Org-mode buffer."
408 (let ((line (org-current-line))
409 (col (current-column))
411 (msg (substitute-command-keys
412 "Edit, then exit with C-c ' (C-c and single quote)"))
413 (org-mode-p (eq major-mode
'org-mode
))
416 (preserve-indentation org-src-preserve-indentation
)
417 block-nindent ovl beg1 end1 code begline buffer
)
418 (beginning-of-line 1)
419 (if (looking-at "[ \t]*[^:\n \t]")
421 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
422 (setq beg1
(point) end1 beg1
)
424 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
425 (setq beg1
(point-at-bol 2))
426 (setq beg1
(point))))
428 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
429 (setq end1
(1- (match-beginning 0)))
430 (setq end1
(point))))
431 (org-goto-line line
))
432 (setq beg
(move-marker beg beg1
)
433 end
(move-marker end end1
)
434 code
(buffer-substring-no-properties beg end
)
435 begline
(save-excursion (goto-char beg
) (org-current-line)))
436 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
437 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
438 (org-pop-to-buffer-same-window buffer
)
440 (with-current-buffer buffer
441 (if (boundp 'org-edit-src-overlay
)
442 (delete-overlay org-edit-src-overlay
)))
443 (kill-buffer buffer
))
444 (setq buffer
(generate-new-buffer
445 (org-src-construct-edit-buffer-name
446 (buffer-name) "Fixed Width")))
447 (setq ovl
(make-overlay beg end
))
448 (overlay-put ovl
'face
'secondary-selection
)
449 (overlay-put ovl
'edit-buffer buffer
)
450 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
451 (overlay-put ovl
'face
'secondary-selection
)
454 (let ((map (make-sparse-keymap)))
455 (define-key map
[mouse-1
] 'org-edit-src-continue
)
457 (overlay-put ovl
:read-only
"Leave me alone")
458 (org-pop-to-buffer-same-window buffer
)
460 (remove-text-properties (point-min) (point-max)
461 '(display nil invisible nil intangible nil
))
462 (setq block-nindent
(or (org-do-remove-indentation) 0))
464 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
467 (t (funcall org-edit-fixed-width-region-mode
)))
468 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
469 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
470 (set (make-local-variable 'org-edit-src-picture
) t
)
471 (goto-char (point-min))
472 (while (re-search-forward "^[ \t]*: ?" nil t
)
474 (org-goto-line (1+ (- line begline
)))
475 (org-move-to-column (max 0 (- col block-nindent
2)))
476 (org-set-local 'org-edit-src-beg-marker beg
)
477 (org-set-local 'org-edit-src-end-marker end
)
478 (org-set-local 'org-edit-src-overlay ovl
)
479 (org-set-local 'org-edit-src-block-indentation block-nindent
)
480 (org-set-local 'org-edit-src-content-indentation
0)
481 (org-set-local 'org-src-preserve-indentation nil
)
483 (set-buffer-modified-p nil
)
484 (and org-edit-src-persistent-message
485 (org-set-local 'header-line-format msg
)))
489 (defun org-edit-src-find-region-and-lang ()
490 "Find the region and language for a local edit.
491 Return a list with beginning and end of the region, a string representing
492 the language, a switch telling if the content should be in a single line."
495 org-edit-src-region-extra
497 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
498 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
499 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
500 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
501 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
502 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
503 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
504 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
505 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
506 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
507 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
508 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
509 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
510 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
511 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
512 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
513 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
514 "\n" "fundamental" macro-definition
)
515 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
518 re1 re2 single beg end lang lfmt match-re1 ind entry
)
520 (while (setq entry
(pop re-list
))
521 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
522 single
(nth 3 entry
))
524 (if (or (looking-at re1
)
525 (re-search-backward re1 nil t
))
527 (setq match-re1
(match-string 0))
528 (setq beg
(match-end 0)
529 lang
(org-edit-src-get-lang lang
)
530 lfmt
(org-edit-src-get-label-format match-re1
)
531 ind
(org-edit-src-get-indentation (match-beginning 0)))
532 (if (and (re-search-forward re2 nil t
)
533 (>= (match-end 0) pos
))
534 (throw 'exit
(list beg
(match-beginning 0)
535 lang single lfmt ind
))))
536 (if (or (looking-at re2
)
537 (re-search-forward re2 nil t
))
539 (setq end
(match-beginning 0))
540 (if (and (re-search-backward re1 nil t
)
541 (<= (match-beginning 0) pos
))
543 (setq lfmt
(org-edit-src-get-label-format
545 ind
(org-edit-src-get-indentation
546 (match-beginning 0)))
548 (list (match-end 0) end
549 (org-edit-src-get-lang lang
)
550 single lfmt ind
)))))))))
551 (when (org-at-table.el-p
)
552 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
553 (setq beg
(1+ (point-at-eol)))
555 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
556 (progn (goto-char (point-max)) (newline)))
557 (setq end
(point-at-bol))
558 (setq ind
(org-edit-src-get-indentation beg
))
559 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
561 (defun org-edit-src-get-lang (lang)
562 "Extract the src language."
563 (let ((m (match-string 0)))
565 ((stringp lang
) lang
)
566 ((integerp lang
) (match-string lang
))
567 ((and (eq lang
'lang
)
568 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
570 ((and (eq lang
'style
)
571 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
575 (defun org-edit-src-get-label-format (s)
576 "Extract the label format."
578 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
579 (match-string 1 s
))))
581 (defun org-edit-src-get-indentation (pos)
582 "Count leading whitespace characters on line."
585 (org-get-indentation)))
587 (defun org-edit-src-exit (&optional context
)
588 "Exit special edit and protect problematic lines."
590 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
591 (error "This is not a sub-editing buffer, something is wrong"))
593 (let* ((beg org-edit-src-beg-marker
)
594 (end org-edit-src-end-marker
)
595 (ovl org-edit-src-overlay
)
596 (buffer (current-buffer))
597 (single (org-bound-and-true-p org-edit-src-force-single-line
))
598 (macro (eq single
'macro-definition
))
599 (total-nindent (+ (or org-edit-src-block-indentation
0)
600 org-edit-src-content-indentation
))
601 (preserve-indentation org-src-preserve-indentation
)
602 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
603 (delta 0) code line col indent
)
604 (when allow-write-back-p
605 (unless preserve-indentation
(untabify (point-min) (point-max)))
606 (if org-src-strip-leading-and-trailing-blank-lines
608 (goto-char (point-min))
609 (if (looking-at "[ \t\n]*\n") (replace-match ""))
611 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
612 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
615 col
(current-column))
616 (when allow-write-back-p
618 (goto-char (point-min))
619 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
620 (goto-char (point-min))
622 (while (re-search-forward "\n" nil t
)
624 (replace-match (if macro
"\\n" " ") t t
))
625 (when (and macro
(> cnt
0))
626 (goto-char (point-max)) (insert "\\n")))
627 (goto-char (point-min))
628 (if (looking-at "\\s-*") (replace-match " ")))
629 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
630 (goto-char (point-min))
631 (while (re-search-forward
632 (if (eq major-mode
'org-mode
) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
633 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
634 (replace-match ",\\1")))
635 (when (org-bound-and-true-p org-edit-src-picture
)
636 (setq preserve-indentation nil
)
637 (untabify (point-min) (point-max))
638 (goto-char (point-min))
639 (while (re-search-forward "^" nil t
)
640 (replace-match ": ")))
641 (unless (or single preserve-indentation
(= total-nindent
0))
642 (setq indent
(make-string total-nindent ?\
))
643 (goto-char (point-min))
644 (while (re-search-forward "^" nil t
)
645 (replace-match indent
)))
646 (if (org-bound-and-true-p org-edit-src-picture
)
647 (setq total-nindent
(+ total-nindent
2)))
648 (setq code
(buffer-string))
649 (set-buffer-modified-p nil
))
650 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
653 (when allow-write-back-p
654 (delete-region beg end
)
657 (if single
(just-one-space)))
658 (if (memq t
(mapcar (lambda (overlay)
659 (eq (overlay-get overlay
'invisible
)
661 (overlays-at (point))))
662 ;; Block is hidden; put point at start of block
663 (beginning-of-line 0)
664 ;; Block is visible, put point where it was in the code buffer
665 (org-goto-line (1- (+ (org-current-line) line
)))
666 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
667 (move-marker beg nil
)
668 (move-marker end nil
))
669 (unless (eq context
'save
)
670 (when org-edit-src-saved-temp-window-config
671 (set-window-configuration org-edit-src-saved-temp-window-config
)
672 (setq org-edit-src-saved-temp-window-config nil
))))
674 (defun org-edit-src-save ()
675 "Save parent buffer with current state source-code buffer."
677 (let ((p (point)) (m (mark)) msg
)
678 (save-window-excursion
679 (org-edit-src-exit 'save
)
681 (setq msg
(current-message))
682 (if (eq org-src-window-setup
'other-frame
)
683 (let ((org-src-window-setup 'current-window
))
684 (org-edit-src-code 'save
))
685 (org-edit-src-code 'save
)))
686 (push-mark m
'nomessage
)
687 (goto-char (min p
(point-max)))
688 (message (or msg
""))))
690 (defun org-src-mode-configure-edit-buffer ()
691 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
692 (org-add-hook 'kill-buffer-hook
693 #'(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
694 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
696 (setq buffer-offer-save t
)
697 (setq buffer-file-name
698 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
699 "[" (buffer-name) "]"))
700 (if (featurep 'xemacs
)
702 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
703 (setq write-contents-hooks
'(org-edit-src-save)))
704 (setq write-contents-functions
'(org-edit-src-save))))
705 (setq buffer-read-only t
))))
707 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
710 (defun org-src-associate-babel-session (info)
711 "Associate edit buffer with comint session."
713 (let ((session (cdr (assoc :session
(nth 2 info
)))))
714 (and session
(not (string= session
"none"))
715 (org-babel-comint-buffer-livep session
)
716 ((lambda (f) (and (fboundp f
) (funcall f session
)))
717 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
719 (defun org-src-babel-configure-edit-buffer ()
720 (when org-src-babel-info
721 (org-src-associate-babel-session org-src-babel-info
)))
723 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
724 (defmacro org-src-do-at-code-block
(&rest body
)
725 "Execute a command from an edit buffer in the Org-mode buffer."
726 `(let ((beg-marker org-edit-src-beg-marker
))
728 (with-current-buffer (marker-buffer beg-marker
)
729 (goto-char (marker-position beg-marker
))
731 (def-edebug-spec org-src-do-at-code-block
(body))
733 (defun org-src-do-key-sequence-at-code-block (&optional key
)
734 "Execute key sequence at code block in the source Org buffer.
735 The command bound to KEY in the Org-babel key map is executed
736 remotely with point temporarily at the start of the code block in
739 This command is not bound to a key by default, to avoid conflicts
740 with language major mode bindings. To bind it to C-c @ in all
741 language major modes, you could use
743 (add-hook 'org-src-mode-hook
744 (lambda () (define-key org-src-mode-map \"\\C-c@\"
745 'org-src-do-key-sequence-at-code-block)))
747 In that case, for example, C-c @ t issued in code edit buffers
748 would tangle the current Org code block, C-c @ e would execute
749 the block and C-c @ h would display the other available
751 (interactive "kOrg-babel key: ")
752 (if (equal key
(kbd "C-g")) (keyboard-quit)
754 (org-src-do-at-code-block
756 (lookup-key org-babel-map key
)))))
758 (defcustom org-src-tab-acts-natively nil
759 "If non-nil, the effect of TAB in a code block is as if it were
760 issued in the language major mode buffer."
764 (defun org-src-native-tab-command-maybe ()
765 "Perform language-specific TAB action.
766 Alter code block according to effect of TAB in the language major
768 (and org-src-tab-acts-natively
769 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
770 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
772 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
774 (defun org-src-font-lock-fontify-block (lang start end
)
776 This function is called by emacs automatic fontification, as long
777 as `org-src-fontify-natively' is non-nil. For manual
778 fontification of code blocks see `org-src-fontify-block' and
779 `org-src-fontify-buffer'"
780 (let ((lang-mode (org-src-get-lang-mode lang
)))
781 (if (fboundp lang-mode
)
782 (let ((string (buffer-substring-no-properties start end
))
783 (modified (buffer-modified-p))
784 (org-buffer (current-buffer)) pos next
)
785 (remove-text-properties start end
'(face nil
))
788 (concat " org-src-fontification:" (symbol-name lang-mode
)))
789 (delete-region (point-min) (point-max))
790 (insert (concat string
" ")) ;; so there's a final property change
791 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
792 (font-lock-fontify-buffer)
793 (setq pos
(point-min))
794 (while (setq next
(next-single-property-change pos
'face
))
796 (+ start
(1- pos
)) (+ start next
) 'face
797 (get-text-property pos
'face
) org-buffer
)
801 '(font-lock-fontified t fontified t font-lock-multiline t
))
802 (set-buffer-modified-p modified
)))))
804 (defun org-src-fontify-block ()
805 "Fontify code block at point."
808 (let ((org-src-fontify-natively t
)
809 (info (org-edit-src-find-region-and-lang)))
810 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
812 (defun org-src-fontify-buffer ()
813 "Fontify all code blocks in the current buffer"
815 (org-babel-map-src-blocks nil
816 (org-src-fontify-block)))
818 (defun org-src-get-lang-mode (lang)
819 "Return major mode that should be used for LANG.
820 LANG is a string, and the returned major mode is a symbol."
823 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
824 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
828 ;;; org-src.el ends here