1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Bastien Guerry <bzg AT altern DOT org>
8 ;; Dan Davison <davison at stats dot ox dot ac dot uk>
9 ;; Keywords: outlines, hypermedia, calendar, wp
10 ;; Homepage: http://orgmode.org
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; This file contains the code dealing with source code examples in Org-mode.
42 (declare-function org-do-remove-indentation
"org" (&optional n
))
43 (declare-function org-at-table.el-p
"org" ())
44 (declare-function org-get-indentation
"org" (&optional line
))
45 (declare-function org-switch-to-buffer-other-window
"org" (&rest args
))
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
))
157 "Alist mapping languages to their major mode.
158 The key is the language name, the value is the string that should
159 be inserted as the name of the major mode. For many languages this is
160 simple, but for language where this is not the case, this variable
161 provides a way to simplify things on the user side.
162 For example, there is no ocaml-mode in Emacs, but the mode to use is
164 :group
'org-edit-structure
167 (string "Language name")
168 (symbol "Major mode"))))
170 ;;; Editing source examples
172 (defvar org-src-mode-map
(make-sparse-keymap))
173 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
175 (defvar org-edit-src-force-single-line nil
)
176 (defvar org-edit-src-from-org-mode nil
)
177 (defvar org-edit-src-allow-write-back-p t
)
178 (defvar org-edit-src-picture nil
)
179 (defvar org-edit-src-beg-marker nil
)
180 (defvar org-edit-src-end-marker nil
)
181 (defvar org-edit-src-overlay nil
)
182 (defvar org-edit-src-block-indentation nil
)
183 (defvar org-edit-src-saved-temp-window-config nil
)
185 (defvar org-src-ask-before-returning-to-edit-buffer t
186 "If nil, when org-edit-src code is used on a block that already
187 has an active edit buffer, it will switch to that edit buffer
188 immediately; otherwise it will ask whether you want to return
189 to the existing edit buffer.")
191 (defvar org-src-babel-info nil
)
193 (define-minor-mode org-src-mode
194 "Minor mode for language major mode buffers generated by org.
195 This minor mode is turned on in two situations:
196 - when editing a source code snippet with \"C-c '\".
197 - When formatting a source code snippet for export with htmlize.
198 There is a mode hook, and keybindings for `org-edit-src-exit' and
199 `org-edit-src-save'")
201 (defun org-edit-src-code (&optional context code edit-buffer-name
)
202 "Edit the source code example at point.
203 The example is copied to a separate buffer, and that buffer is
204 switched to the correct language mode. When done, exit with
205 \\[org-edit-src-exit]. This will remove the original code in the
206 Org buffer, and replace it with the edited version. Optional
207 argument CONTEXT is used by \\[org-edit-src-save] when calling
208 this function. See \\[org-src-window-setup] to configure the
209 display of windows containing the Org buffer and the code
212 (unless (eq context
'save
)
213 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
214 (let ((mark (and (org-region-active-p) (mark)))
216 (info (org-edit-src-find-region-and-lang))
217 (babel-info (org-babel-get-src-block-info 'light
))
218 (org-mode-p (eq major-mode
'org-mode
))
221 (preserve-indentation org-src-preserve-indentation
)
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
)
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
)
239 block-nindent
(nth 5 info
)
240 lang-f
(intern (concat lang
"-mode"))
241 begline
(save-excursion (goto-char beg
) (org-current-line)))
242 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
243 (save-excursion (goto-char (min mark end
))
244 (setq markline
(org-current-line)
245 markcol
(current-column))))
246 (if (equal lang-f
'table.el-mode
)
247 (setq lang-f
(lambda ()
249 (if (org-bound-and-true-p flyspell-mode
)
252 (org-set-local 'org-edit-src-content-indentation
0))))
253 (unless (functionp lang-f
)
254 (error "No such language mode: %s" lang-f
))
256 (if (> (point) end
) (goto-char end
))
257 (setq line
(org-current-line)
258 col
(current-column)))
259 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
260 (if org-src-ask-before-returning-to-edit-buffer
261 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t
))
262 (org-src-switch-to-buffer buffer
'return
)
264 (with-current-buffer buffer
265 (if (boundp 'org-edit-src-overlay
)
266 (delete-overlay org-edit-src-overlay
)))
267 (kill-buffer buffer
))
268 (setq buffer
(generate-new-buffer
270 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
271 (setq ovl
(make-overlay beg end
))
272 (overlay-put ovl
'edit-buffer buffer
)
273 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
274 (overlay-put ovl
'face
'secondary-selection
)
277 (let ((map (make-sparse-keymap)))
278 (define-key map
[mouse-1
] 'org-edit-src-continue
)
280 (overlay-put ovl
:read-only
"Leave me alone")
281 (org-src-switch-to-buffer buffer
'edit
)
282 (if (eq single
'macro-definition
)
283 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
285 (remove-text-properties (point-min) (point-max)
286 '(display nil invisible nil intangible nil
))
287 (unless preserve-indentation
288 (setq total-nindent
(or (org-do-remove-indentation) 0)))
289 (let ((org-inhibit-startup t
))
293 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
294 (set (make-local-variable 'org-edit-src-force-single-line
) single
)
295 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
296 (set (make-local-variable 'org-edit-src-allow-write-back-p
) allow-write-back-p
)
297 (set (make-local-variable 'org-src-preserve-indentation
) preserve-indentation
)
299 (set (make-local-variable 'org-src-babel-info
) babel-info
))
301 (set (make-local-variable 'org-coderef-label-format
) lfmt
))
303 (goto-char (point-min))
304 (while (re-search-forward "^," nil t
)
305 (if (eq (org-current-line) line
) (setq total-nindent
(1+ total-nindent
)))
308 (org-goto-line (1+ (- markline begline
)))
310 (if preserve-indentation markcol
(max 0 (- markcol total-nindent
))))
311 (push-mark (point) 'no-message t
)
312 (setq deactivate-mark nil
))
313 (org-goto-line (1+ (- line begline
)))
315 (if preserve-indentation col
(max 0 (- col total-nindent
))))
316 (org-set-local 'org-edit-src-beg-marker beg
)
317 (org-set-local 'org-edit-src-end-marker end
)
318 (org-set-local 'org-edit-src-overlay ovl
)
319 (org-set-local 'org-edit-src-block-indentation block-nindent
)
321 (set-buffer-modified-p nil
)
322 (and org-edit-src-persistent-message
323 (org-set-local 'header-line-format msg
)))
326 (defun org-edit-src-continue (e)
329 (let ((buf (get-char-property (point) 'edit-buffer
)))
330 (if buf
(org-src-switch-to-buffer buf
'continue
)
331 (error "Something is wrong here"))))
333 (defun org-src-switch-to-buffer (buffer context
)
334 (case org-src-window-setup
336 (switch-to-buffer buffer
))
338 (switch-to-buffer-other-window buffer
))
342 (let ((frame (selected-frame)))
343 (switch-to-buffer-other-frame buffer
)
344 (delete-frame frame
)))
346 (kill-buffer (current-buffer))
347 (switch-to-buffer buffer
))
349 (switch-to-buffer-other-frame buffer
))))
351 (if (eq context
'edit
) (delete-other-windows))
352 (org-switch-to-buffer-other-window buffer
)
353 (if (eq context
'exit
) (delete-other-windows)))
357 (message "Invalid value %s for org-src-window-setup"
358 (symbol-name org-src-window-setup
))
359 (switch-to-buffer buffer
))))
361 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
362 "Construct the buffer name for a source editing buffer."
363 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
365 (defun org-edit-src-find-buffer (beg end
)
366 "Find a source editing buffer that is already editing the region BEG to END."
370 (with-current-buffer b
371 (if (and (string-match "\\`*Org Src " (buffer-name))
372 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
373 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
374 (equal beg org-edit-src-beg-marker
)
375 (equal end org-edit-src-end-marker
))
376 (throw 'exit
(current-buffer)))))
380 (defun org-edit-fixed-width-region ()
381 "Edit the fixed-width ascii drawing at point.
382 This must be a region where each line starts with a colon followed by
384 An new buffer is created and the fixed-width region is copied into it,
385 and the buffer is switched into `artist-mode' for editing. When done,
386 exit with \\[org-edit-src-exit]. The edited text will then replace
387 the fragment in the Org-mode buffer."
389 (let ((line (org-current-line))
390 (col (current-column))
392 (msg (substitute-command-keys
393 "Edit, then exit with C-c ' (C-c and single quote)"))
394 (org-mode-p (eq major-mode
'org-mode
))
397 (preserve-indentation org-src-preserve-indentation
)
398 block-nindent ovl beg1 end1 code begline buffer
)
399 (beginning-of-line 1)
400 (if (looking-at "[ \t]*[^:\n \t]")
402 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
403 (setq beg1
(point) end1 beg1
)
405 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
406 (setq beg1
(point-at-bol 2))
407 (setq beg1
(point))))
409 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
410 (setq end1
(1- (match-beginning 0)))
411 (setq end1
(point))))
412 (org-goto-line line
))
413 (setq beg
(move-marker beg beg1
)
414 end
(move-marker end end1
)
415 code
(buffer-substring-no-properties beg end
)
416 begline
(save-excursion (goto-char beg
) (org-current-line)))
417 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
418 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
419 (switch-to-buffer buffer
)
421 (with-current-buffer buffer
422 (if (boundp 'org-edit-src-overlay
)
423 (delete-overlay org-edit-src-overlay
)))
424 (kill-buffer buffer
))
425 (setq buffer
(generate-new-buffer
426 (org-src-construct-edit-buffer-name
427 (buffer-name) "Fixed Width")))
428 (setq ovl
(make-overlay beg end
))
429 (overlay-put ovl
'face
'secondary-selection
)
430 (overlay-put ovl
'edit-buffer buffer
)
431 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
432 (overlay-put ovl
'face
'secondary-selection
)
435 (let ((map (make-sparse-keymap)))
436 (define-key map
[mouse-1
] 'org-edit-src-continue
)
438 (overlay-put ovl
:read-only
"Leave me alone")
439 (switch-to-buffer buffer
)
441 (remove-text-properties (point-min) (point-max)
442 '(display nil invisible nil intangible nil
))
443 (setq block-nindent
(or (org-do-remove-indentation) 0))
445 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
448 (t (funcall org-edit-fixed-width-region-mode
)))
449 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
450 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
451 (set (make-local-variable 'org-edit-src-picture
) t
)
452 (goto-char (point-min))
453 (while (re-search-forward "^[ \t]*: ?" nil t
)
455 (org-goto-line (1+ (- line begline
)))
456 (org-move-to-column (max 0 (- col block-nindent
2)))
457 (org-set-local 'org-edit-src-beg-marker beg
)
458 (org-set-local 'org-edit-src-end-marker end
)
459 (org-set-local 'org-edit-src-overlay ovl
)
460 (org-set-local 'org-edit-src-block-indentation block-nindent
)
461 (org-set-local 'org-edit-src-content-indentation
0)
462 (org-set-local 'org-src-preserve-indentation nil
)
464 (set-buffer-modified-p nil
)
465 (and org-edit-src-persistent-message
466 (org-set-local 'header-line-format msg
)))
470 (defun org-edit-src-find-region-and-lang ()
471 "Find the region and language for a local edit.
472 Return a list with beginning and end of the region, a string representing
473 the language, a switch telling if the content should be in a single line."
476 org-edit-src-region-extra
478 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
479 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
480 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
481 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
482 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
483 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
484 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
485 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
486 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
487 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
488 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
489 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
490 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
491 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
492 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
493 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
494 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
495 "\n" "fundamental" macro-definition
)
496 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
499 re1 re2 single beg end lang lfmt match-re1 ind entry
)
501 (while (setq entry
(pop re-list
))
502 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
503 single
(nth 3 entry
))
505 (if (or (looking-at re1
)
506 (re-search-backward re1 nil t
))
508 (setq match-re1
(match-string 0))
509 (setq beg
(match-end 0)
510 lang
(org-edit-src-get-lang lang
)
511 lfmt
(org-edit-src-get-label-format match-re1
)
512 ind
(org-edit-src-get-indentation (match-beginning 0)))
513 (if (and (re-search-forward re2 nil t
)
514 (>= (match-end 0) pos
))
515 (throw 'exit
(list beg
(match-beginning 0)
516 lang single lfmt ind
))))
517 (if (or (looking-at re2
)
518 (re-search-forward re2 nil t
))
520 (setq end
(match-beginning 0))
521 (if (and (re-search-backward re1 nil t
)
522 (<= (match-beginning 0) pos
))
524 (setq lfmt
(org-edit-src-get-label-format
526 ind
(org-edit-src-get-indentation
527 (match-beginning 0)))
529 (list (match-end 0) end
530 (org-edit-src-get-lang lang
)
531 single lfmt ind
)))))))))
532 (when (org-at-table.el-p
)
533 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
534 (setq beg
(1+ (point-at-eol)))
536 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
537 (progn (goto-char (point-max)) (newline)))
538 (setq end
(point-at-bol))
539 (setq ind
(org-edit-src-get-indentation beg
))
540 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
542 (defun org-edit-src-get-lang (lang)
543 "Extract the src language."
544 (let ((m (match-string 0)))
546 ((stringp lang
) lang
)
547 ((integerp lang
) (match-string lang
))
548 ((and (eq lang
'lang
)
549 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
551 ((and (eq lang
'style
)
552 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
556 (defun org-edit-src-get-label-format (s)
557 "Extract the label format."
559 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
560 (match-string 1 s
))))
562 (defun org-edit-src-get-indentation (pos)
563 "Count leading whitespace characters on line."
566 (org-get-indentation)))
568 (defun org-edit-src-exit (&optional context
)
569 "Exit special edit and protect problematic lines."
571 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
572 (error "This is not a sub-editing buffer, something is wrong"))
574 (let* ((beg org-edit-src-beg-marker
)
575 (end org-edit-src-end-marker
)
576 (ovl org-edit-src-overlay
)
577 (buffer (current-buffer))
578 (single (org-bound-and-true-p org-edit-src-force-single-line
))
579 (macro (eq single
'macro-definition
))
580 (total-nindent (+ (or org-edit-src-block-indentation
0)
581 org-edit-src-content-indentation
))
582 (preserve-indentation org-src-preserve-indentation
)
583 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
584 (delta 0) code line col indent
)
585 (when allow-write-back-p
586 (unless preserve-indentation
(untabify (point-min) (point-max)))
587 (if org-src-strip-leading-and-trailing-blank-lines
589 (goto-char (point-min))
590 (if (looking-at "[ \t\n]*\n") (replace-match ""))
592 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
593 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
596 col
(current-column))
597 (when allow-write-back-p
599 (goto-char (point-min))
600 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
601 (goto-char (point-min))
603 (while (re-search-forward "\n" nil t
)
605 (replace-match (if macro
"\\n" " ") t t
))
606 (when (and macro
(> cnt
0))
607 (goto-char (point-max)) (insert "\\n")))
608 (goto-char (point-min))
609 (if (looking-at "\\s-*") (replace-match " ")))
610 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
611 (goto-char (point-min))
612 (while (re-search-forward
613 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
614 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
615 (replace-match ",\\1")))
616 (when (org-bound-and-true-p org-edit-src-picture
)
617 (setq preserve-indentation nil
)
618 (untabify (point-min) (point-max))
619 (goto-char (point-min))
620 (while (re-search-forward "^" nil t
)
621 (replace-match ": ")))
622 (unless (or single preserve-indentation
(= total-nindent
0))
623 (setq indent
(make-string total-nindent ?\
))
624 (goto-char (point-min))
625 (while (re-search-forward "^" nil t
)
626 (replace-match indent
)))
627 (if (org-bound-and-true-p org-edit-src-picture
)
628 (setq total-nindent
(+ total-nindent
2)))
629 (setq code
(buffer-string))
630 (set-buffer-modified-p nil
))
631 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
634 (when allow-write-back-p
635 (delete-region beg end
)
638 (if single
(just-one-space)))
639 (if (memq t
(mapcar (lambda (overlay)
640 (eq (overlay-get overlay
'invisible
)
642 (overlays-at (point))))
643 ;; Block is hidden; put point at start of block
644 (beginning-of-line 0)
645 ;; Block is visible, put point where it was in the code buffer
646 (org-goto-line (1- (+ (org-current-line) line
)))
647 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
648 (move-marker beg nil
)
649 (move-marker end nil
))
650 (unless (eq context
'save
)
651 (when org-edit-src-saved-temp-window-config
652 (set-window-configuration org-edit-src-saved-temp-window-config
)
653 (setq org-edit-src-saved-temp-window-config nil
))))
655 (defun org-edit-src-save ()
656 "Save parent buffer with current state source-code buffer."
658 (let ((p (point)) (m (mark)) msg
)
659 (save-window-excursion
660 (org-edit-src-exit 'save
)
662 (setq msg
(current-message))
663 (if (eq org-src-window-setup
'other-frame
)
664 (let ((org-src-window-setup 'current-window
))
665 (org-edit-src-code 'save
))
666 (org-edit-src-code 'save
)))
667 (push-mark m
'nomessage
)
668 (goto-char (min p
(point-max)))
669 (message (or msg
""))))
671 (defun org-src-mode-configure-edit-buffer ()
672 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
673 (org-add-hook 'kill-buffer-hook
674 '(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
675 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
677 (setq buffer-offer-save t
)
678 (setq buffer-file-name
679 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
680 "[" (buffer-name) "]"))
681 (if (featurep 'xemacs
)
683 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
684 (setq write-contents-hooks
'(org-edit-src-save)))
685 (setq write-contents-functions
'(org-edit-src-save))))
686 (setq buffer-read-only t
))))
688 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
691 (defun org-src-associate-babel-session (info)
692 "Associate edit buffer with comint session."
694 (let ((session (cdr (assoc :session
(nth 2 info
)))))
695 (and session
(not (string= session
"none"))
696 (org-babel-comint-buffer-livep session
)
697 ((lambda (f) (and (fboundp f
) (funcall f session
)))
698 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
700 (defun org-src-babel-configure-edit-buffer ()
701 (when org-src-babel-info
702 (org-src-associate-babel-session org-src-babel-info
)))
704 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
705 (defmacro org-src-do-at-code-block
(&rest body
)
706 "Execute a command from an edit buffer in the Org-mode buffer."
707 `(let ((beg-marker org-edit-src-beg-marker
))
709 (with-current-buffer (marker-buffer beg-marker
)
710 (goto-char (marker-position beg-marker
))
713 (defun org-src-do-key-sequence-at-code-block (&optional key
)
714 "Execute key sequence at code block in the source Org buffer.
715 The command bound to KEY in the Org-babel key map is executed
716 remotely with point temporarily at the start of the code block in
719 This command is not bound to a key by default, to avoid conflicts
720 with language major mode bindings. To bind it to C-c @ in all
721 language major modes, you could use
723 (add-hook 'org-src-mode-hook
724 (lambda () (define-key org-src-mode-map \"\\C-c@\"
725 'org-src-do-key-sequence-at-code-block)))
727 In that case, for example, C-c @ t issued in code edit buffers
728 would tangle the current Org code block, C-c @ e would execute
729 the block and C-c @ h would display the other available
731 (interactive "kOrg-babel key: ")
732 (if (equal key
(kbd "C-g")) (keyboard-quit)
734 (org-src-do-at-code-block
736 (lookup-key org-babel-map key
)))))
738 (defcustom org-src-tab-acts-natively nil
739 "If non-nil, the effect of TAB in a code block is as if it were
740 issued in the language major mode buffer."
744 (defun org-src-native-tab-command-maybe ()
745 "Perform language-specific TAB action.
746 Alter code block according to effect of TAB in the language major
748 (and org-src-tab-acts-natively
749 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
750 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
752 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
754 (defun org-src-font-lock-fontify-block (lang start end
)
756 This function is called by emacs automatic fontification, as long
757 as `org-src-fontify-natively' is non-nil. For manual
758 fontification of code blocks see `org-src-fontify-block' and
759 `org-src-fontify-buffer'"
760 (let ((lang-mode (org-src-get-lang-mode lang
)))
761 (if (fboundp lang-mode
)
762 (let ((string (buffer-substring-no-properties start end
))
763 (modified (buffer-modified-p))
764 (org-buffer (current-buffer)) pos next
)
765 (remove-text-properties start end
'(face nil
))
768 (concat " org-src-fontification:" (symbol-name lang-mode
)))
769 (delete-region (point-min) (point-max))
771 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
772 (font-lock-fontify-buffer)
773 (setq pos
(point-min))
774 (while (setq next
(next-single-property-change pos
'face
))
776 (+ start
(1- pos
)) (+ start next
) 'face
777 (get-text-property pos
'face
) org-buffer
)
781 '(font-lock-fontified t fontified t font-lock-multiline t
))
782 (set-buffer-modified-p modified
)))))
784 (defun org-src-fontify-block ()
785 "Fontify code block at point."
788 (let ((org-src-fontify-natively t
)
789 (info (org-edit-src-find-region-and-lang)))
790 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
792 (defun org-src-fontify-buffer ()
793 "Fontify all code blocks in the current buffer"
795 (org-babel-map-src-blocks nil
796 (org-src-fontify-block)))
798 (defun org-src-get-lang-mode (lang)
799 "Return major mode that should be used for LANG.
800 LANG is a string, and the returned major mode is a symbol."
803 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
804 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
808 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
809 ;;; org-src.el ends here