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 When nil, the message will only be shown intermittently in the echo area."
121 :group
'org-edit-structure
124 (defcustom org-src-window-setup
'reorganize-frame
125 "How the source code edit buffer should be displayed.
126 Possible values for this option are:
128 current-window Show edit buffer in the current window, keeping all other
130 other-window Use `switch-to-buffer-other-window' to display edit buffer.
131 reorganize-frame Show only two windows on the current frame, the current
132 window and the edit buffer. When exiting the edit buffer,
133 return to one window.
134 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
135 Also, when exiting the edit buffer, kill that frame."
136 :group
'org-edit-structure
138 (const current-window
)
141 (const reorganize-frame
)))
143 (defvar org-src-mode-hook nil
144 "Hook run after Org switched a source code snippet to its Emacs mode.
147 - when editing a source code snippet with \"C-c '\".
148 - When formatting a source code snippet for export with htmlize.
150 You may want to use this hook for example to turn off `outline-minor-mode'
151 or similar things which you want to have when editing a source code file,
152 but which mess up the display of a snippet in Org exported files.")
154 (defcustom org-src-lang-modes
155 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
156 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
157 ("calc" . fundamental
))
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 quietp
)
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 (babel-info (org-babel-get-src-block-info 'light
))
219 (org-mode-p (eq major-mode
'org-mode
))
222 (preserve-indentation org-src-preserve-indentation
)
223 (allow-write-back-p (null code
))
224 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
225 begline markline markcol line col
)
228 (setq beg
(move-marker beg
(nth 0 info
))
229 end
(move-marker end
(nth 1 info
))
230 msg
(if allow-write-back-p
231 (substitute-command-keys
232 "Edit, then exit with C-c ' (C-c and single quote)")
233 "Exit with C-c ' (C-c and single quote)")
234 code
(or code
(buffer-substring-no-properties beg end
))
235 lang
(or (cdr (assoc (nth 2 info
) org-src-lang-modes
))
237 lang
(if (symbolp lang
) (symbol-name lang
) lang
)
240 block-nindent
(nth 5 info
)
241 lang-f
(intern (concat lang
"-mode"))
242 begline
(save-excursion (goto-char beg
) (org-current-line)))
243 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
244 (save-excursion (goto-char (min mark end
))
245 (setq markline
(org-current-line)
246 markcol
(current-column))))
247 (if (equal lang-f
'table.el-mode
)
248 (setq lang-f
(lambda ()
250 (if (org-bound-and-true-p flyspell-mode
)
253 (org-set-local 'org-edit-src-content-indentation
0))))
254 (unless (functionp lang-f
)
255 (error "No such language mode: %s" lang-f
))
257 (if (> (point) end
) (goto-char end
))
258 (setq line
(org-current-line)
259 col
(current-column)))
260 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
261 (if org-src-ask-before-returning-to-edit-buffer
262 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t
))
263 (org-src-switch-to-buffer buffer
'return
)
265 (with-current-buffer buffer
266 (if (boundp 'org-edit-src-overlay
)
267 (delete-overlay org-edit-src-overlay
)))
268 (kill-buffer buffer
))
269 (setq buffer
(generate-new-buffer
271 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
272 (setq ovl
(make-overlay beg end
))
273 (overlay-put ovl
'edit-buffer buffer
)
274 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
275 (overlay-put ovl
'face
'secondary-selection
)
278 (let ((map (make-sparse-keymap)))
279 (define-key map
[mouse-1
] 'org-edit-src-continue
)
281 (overlay-put ovl
:read-only
"Leave me alone")
282 (org-src-switch-to-buffer buffer
'edit
)
283 (if (eq single
'macro-definition
)
284 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
286 (remove-text-properties (point-min) (point-max)
287 '(display nil invisible nil intangible nil
))
288 (unless preserve-indentation
289 (setq total-nindent
(or (org-do-remove-indentation) 0)))
290 (let ((org-inhibit-startup t
))
294 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
295 (set (make-local-variable 'org-edit-src-force-single-line
) single
)
296 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
297 (set (make-local-variable 'org-edit-src-allow-write-back-p
) allow-write-back-p
)
298 (set (make-local-variable 'org-src-preserve-indentation
) preserve-indentation
)
300 (set (make-local-variable 'org-src-babel-info
) babel-info
))
302 (set (make-local-variable 'org-coderef-label-format
) lfmt
))
304 (goto-char (point-min))
305 (while (re-search-forward "^," nil t
)
306 (if (eq (org-current-line) line
) (setq total-nindent
(1+ total-nindent
)))
309 (org-goto-line (1+ (- markline begline
)))
311 (if preserve-indentation markcol
(max 0 (- markcol total-nindent
))))
312 (push-mark (point) 'no-message t
)
313 (setq deactivate-mark nil
))
314 (org-goto-line (1+ (- line begline
)))
316 (if preserve-indentation col
(max 0 (- col total-nindent
))))
317 (org-set-local 'org-edit-src-beg-marker beg
)
318 (org-set-local 'org-edit-src-end-marker end
)
319 (org-set-local 'org-edit-src-overlay ovl
)
320 (org-set-local 'org-edit-src-block-indentation block-nindent
)
322 (set-buffer-modified-p nil
)
323 (and org-edit-src-persistent-message
324 (org-set-local 'header-line-format msg
)))
325 (unless quietp
(message "%s" msg
))
328 (defun org-edit-src-continue (e)
331 (let ((buf (get-char-property (point) 'edit-buffer
)))
332 (if buf
(org-src-switch-to-buffer buf
'continue
)
333 (error "Something is wrong here"))))
335 (defun org-src-switch-to-buffer (buffer context
)
336 (case org-src-window-setup
338 (switch-to-buffer buffer
))
340 (switch-to-buffer-other-window buffer
))
344 (let ((frame (selected-frame)))
345 (switch-to-buffer-other-frame buffer
)
346 (delete-frame frame
)))
348 (kill-buffer (current-buffer))
349 (switch-to-buffer buffer
))
351 (switch-to-buffer-other-frame buffer
))))
353 (if (eq context
'edit
) (delete-other-windows))
354 (org-switch-to-buffer-other-window buffer
)
355 (if (eq context
'exit
) (delete-other-windows)))
359 (message "Invalid value %s for org-src-window-setup"
360 (symbol-name org-src-window-setup
))
361 (switch-to-buffer buffer
))))
363 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
364 "Construct the buffer name for a source editing buffer."
365 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
367 (defun org-edit-src-find-buffer (beg end
)
368 "Find a source editing buffer that is already editing the region BEG to END."
372 (with-current-buffer b
373 (if (and (string-match "\\`*Org Src " (buffer-name))
374 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
375 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
376 (equal beg org-edit-src-beg-marker
)
377 (equal end org-edit-src-end-marker
))
378 (throw 'exit
(current-buffer)))))
382 (defun org-edit-fixed-width-region ()
383 "Edit the fixed-width ascii drawing at point.
384 This must be a region where each line starts with a colon followed by
386 An new buffer is created and the fixed-width region is copied into it,
387 and the buffer is switched into `artist-mode' for editing. When done,
388 exit with \\[org-edit-src-exit]. The edited text will then replace
389 the fragment in the Org-mode buffer."
391 (let ((line (org-current-line))
392 (col (current-column))
394 (msg (substitute-command-keys
395 "Edit, then exit with C-c ' (C-c and single quote)"))
396 (org-mode-p (eq major-mode
'org-mode
))
399 (preserve-indentation org-src-preserve-indentation
)
400 block-nindent ovl beg1 end1 code begline buffer
)
401 (beginning-of-line 1)
402 (if (looking-at "[ \t]*[^:\n \t]")
404 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
405 (setq beg1
(point) end1 beg1
)
407 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
408 (setq beg1
(point-at-bol 2))
409 (setq beg1
(point))))
411 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
412 (setq end1
(1- (match-beginning 0)))
413 (setq end1
(point))))
414 (org-goto-line line
))
415 (setq beg
(move-marker beg beg1
)
416 end
(move-marker end end1
)
417 code
(buffer-substring-no-properties beg end
)
418 begline
(save-excursion (goto-char beg
) (org-current-line)))
419 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
420 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
421 (switch-to-buffer buffer
)
423 (with-current-buffer buffer
424 (if (boundp 'org-edit-src-overlay
)
425 (delete-overlay org-edit-src-overlay
)))
426 (kill-buffer buffer
))
427 (setq buffer
(generate-new-buffer
428 (org-src-construct-edit-buffer-name
429 (buffer-name) "Fixed Width")))
430 (setq ovl
(make-overlay beg end
))
431 (overlay-put ovl
'face
'secondary-selection
)
432 (overlay-put ovl
'edit-buffer buffer
)
433 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
434 (overlay-put ovl
'face
'secondary-selection
)
437 (let ((map (make-sparse-keymap)))
438 (define-key map
[mouse-1
] 'org-edit-src-continue
)
440 (overlay-put ovl
:read-only
"Leave me alone")
441 (switch-to-buffer buffer
)
443 (remove-text-properties (point-min) (point-max)
444 '(display nil invisible nil intangible nil
))
445 (setq block-nindent
(or (org-do-remove-indentation) 0))
447 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
450 (t (funcall org-edit-fixed-width-region-mode
)))
451 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
452 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
453 (set (make-local-variable 'org-edit-src-picture
) t
)
454 (goto-char (point-min))
455 (while (re-search-forward "^[ \t]*: ?" nil t
)
457 (org-goto-line (1+ (- line begline
)))
458 (org-move-to-column (max 0 (- col block-nindent
2)))
459 (org-set-local 'org-edit-src-beg-marker beg
)
460 (org-set-local 'org-edit-src-end-marker end
)
461 (org-set-local 'org-edit-src-overlay ovl
)
462 (org-set-local 'org-edit-src-block-indentation block-nindent
)
463 (org-set-local 'org-edit-src-content-indentation
0)
464 (org-set-local 'org-src-preserve-indentation nil
)
466 (set-buffer-modified-p nil
)
467 (and org-edit-src-persistent-message
468 (org-set-local 'header-line-format msg
)))
472 (defun org-edit-src-find-region-and-lang ()
473 "Find the region and language for a local edit.
474 Return a list with beginning and end of the region, a string representing
475 the language, a switch telling if the content should be in a single line."
478 org-edit-src-region-extra
480 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
481 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
482 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
483 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
484 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
485 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
486 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
487 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
488 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
489 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
490 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
491 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
492 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
493 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
494 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
495 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
496 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
497 "\n" "fundamental" macro-definition
)
498 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
501 re1 re2 single beg end lang lfmt match-re1 ind entry
)
503 (while (setq entry
(pop re-list
))
504 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
505 single
(nth 3 entry
))
507 (if (or (looking-at re1
)
508 (re-search-backward re1 nil t
))
510 (setq match-re1
(match-string 0))
511 (setq beg
(match-end 0)
512 lang
(org-edit-src-get-lang lang
)
513 lfmt
(org-edit-src-get-label-format match-re1
)
514 ind
(org-edit-src-get-indentation (match-beginning 0)))
515 (if (and (re-search-forward re2 nil t
)
516 (>= (match-end 0) pos
))
517 (throw 'exit
(list beg
(match-beginning 0)
518 lang single lfmt ind
))))
519 (if (or (looking-at re2
)
520 (re-search-forward re2 nil t
))
522 (setq end
(match-beginning 0))
523 (if (and (re-search-backward re1 nil t
)
524 (<= (match-beginning 0) pos
))
526 (setq lfmt
(org-edit-src-get-label-format
528 ind
(org-edit-src-get-indentation
529 (match-beginning 0)))
531 (list (match-end 0) end
532 (org-edit-src-get-lang lang
)
533 single lfmt ind
)))))))))
534 (when (org-at-table.el-p
)
535 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
536 (setq beg
(1+ (point-at-eol)))
538 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
539 (progn (goto-char (point-max)) (newline)))
540 (setq end
(point-at-bol))
541 (setq ind
(org-edit-src-get-indentation beg
))
542 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
544 (defun org-edit-src-get-lang (lang)
545 "Extract the src language."
546 (let ((m (match-string 0)))
548 ((stringp lang
) lang
)
549 ((integerp lang
) (match-string lang
))
550 ((and (eq lang
'lang
)
551 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
553 ((and (eq lang
'style
)
554 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
558 (defun org-edit-src-get-label-format (s)
559 "Extract the label format."
561 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
562 (match-string 1 s
))))
564 (defun org-edit-src-get-indentation (pos)
565 "Count leading whitespace characters on line."
568 (org-get-indentation)))
570 (defun org-edit-src-exit (&optional context
)
571 "Exit special edit and protect problematic lines."
573 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
574 (error "This is not a sub-editing buffer, something is wrong"))
576 (let* ((beg org-edit-src-beg-marker
)
577 (end org-edit-src-end-marker
)
578 (ovl org-edit-src-overlay
)
579 (buffer (current-buffer))
580 (single (org-bound-and-true-p org-edit-src-force-single-line
))
581 (macro (eq single
'macro-definition
))
582 (total-nindent (+ (or org-edit-src-block-indentation
0)
583 org-edit-src-content-indentation
))
584 (preserve-indentation org-src-preserve-indentation
)
585 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
586 (delta 0) code line col indent
)
587 (when allow-write-back-p
588 (unless preserve-indentation
(untabify (point-min) (point-max)))
589 (if org-src-strip-leading-and-trailing-blank-lines
591 (goto-char (point-min))
592 (if (looking-at "[ \t\n]*\n") (replace-match ""))
594 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
595 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
598 col
(current-column))
599 (when allow-write-back-p
601 (goto-char (point-min))
602 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
603 (goto-char (point-min))
605 (while (re-search-forward "\n" nil t
)
607 (replace-match (if macro
"\\n" " ") t t
))
608 (when (and macro
(> cnt
0))
609 (goto-char (point-max)) (insert "\\n")))
610 (goto-char (point-min))
611 (if (looking-at "\\s-*") (replace-match " ")))
612 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
613 (goto-char (point-min))
614 (while (re-search-forward
615 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
616 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
617 (replace-match ",\\1")))
618 (when (org-bound-and-true-p org-edit-src-picture
)
619 (setq preserve-indentation nil
)
620 (untabify (point-min) (point-max))
621 (goto-char (point-min))
622 (while (re-search-forward "^" nil t
)
623 (replace-match ": ")))
624 (unless (or single preserve-indentation
(= total-nindent
0))
625 (setq indent
(make-string total-nindent ?\
))
626 (goto-char (point-min))
627 (while (re-search-forward "^" nil t
)
628 (replace-match indent
)))
629 (if (org-bound-and-true-p org-edit-src-picture
)
630 (setq total-nindent
(+ total-nindent
2)))
631 (setq code
(buffer-string))
632 (set-buffer-modified-p nil
))
633 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
636 (when allow-write-back-p
637 (delete-region beg end
)
640 (if single
(just-one-space)))
641 (if (memq t
(mapcar (lambda (overlay)
642 (eq (overlay-get overlay
'invisible
)
644 (overlays-at (point))))
645 ;; Block is hidden; put point at start of block
646 (beginning-of-line 0)
647 ;; Block is visible, put point where it was in the code buffer
648 (org-goto-line (1- (+ (org-current-line) line
)))
649 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
650 (move-marker beg nil
)
651 (move-marker end nil
))
652 (unless (eq context
'save
)
653 (when org-edit-src-saved-temp-window-config
654 (set-window-configuration org-edit-src-saved-temp-window-config
)
655 (setq org-edit-src-saved-temp-window-config nil
))))
657 (defun org-edit-src-save ()
658 "Save parent buffer with current state source-code buffer."
660 (let ((p (point)) (m (mark)) msg
)
661 (save-window-excursion
662 (org-edit-src-exit 'save
)
664 (setq msg
(current-message))
665 (if (eq org-src-window-setup
'other-frame
)
666 (let ((org-src-window-setup 'current-window
))
667 (org-edit-src-code 'save
))
668 (org-edit-src-code 'save
)))
669 (push-mark m
'nomessage
)
670 (goto-char (min p
(point-max)))
671 (message (or msg
""))))
673 (defun org-src-mode-configure-edit-buffer ()
674 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
675 (org-add-hook 'kill-buffer-hook
676 '(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
677 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
679 (setq buffer-offer-save t
)
680 (setq buffer-file-name
681 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
682 "[" (buffer-name) "]"))
683 (if (featurep 'xemacs
)
685 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
686 (setq write-contents-hooks
'(org-edit-src-save)))
687 (setq write-contents-functions
'(org-edit-src-save))))
688 (setq buffer-read-only t
))))
690 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
693 (defun org-src-associate-babel-session (info)
694 "Associate edit buffer with comint session."
696 (let ((session (cdr (assoc :session
(nth 2 info
)))))
697 (and session
(not (string= session
"none"))
698 (org-babel-comint-buffer-livep session
)
699 ((lambda (f) (and (fboundp f
) (funcall f session
)))
700 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
702 (defun org-src-babel-configure-edit-buffer ()
703 (when org-src-babel-info
704 (org-src-associate-babel-session org-src-babel-info
)))
706 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
707 (defmacro org-src-do-at-code-block
(&rest body
)
708 "Execute a command from an edit buffer in the Org-mode buffer."
709 `(let ((beg-marker org-edit-src-beg-marker
))
711 (with-current-buffer (marker-buffer beg-marker
)
712 (goto-char (marker-position beg-marker
))
715 (defun org-src-do-key-sequence-at-code-block (&optional key
)
716 "Execute key sequence at code block in the source Org buffer.
717 The command bound to KEY in the Org-babel key map is executed
718 remotely with point temporarily at the start of the code block in
721 This command is not bound to a key by default, to avoid conflicts
722 with language major mode bindings. To bind it to C-c @ in all
723 language major modes, you could use
725 (add-hook 'org-src-mode-hook
726 (lambda () (define-key org-src-mode-map \"\\C-c@\"
727 'org-src-do-key-sequence-at-code-block)))
729 In that case, for example, C-c @ t issued in code edit buffers
730 would tangle the current Org code block, C-c @ e would execute
731 the block and C-c @ h would display the other available
733 (interactive "kOrg-babel key: ")
734 (if (equal key
(kbd "C-g")) (keyboard-quit)
736 (org-src-do-at-code-block
738 (lookup-key org-babel-map key
)))))
740 (defcustom org-src-tab-acts-natively nil
741 "If non-nil, the effect of TAB in a code block is as if it were
742 issued in the language major mode buffer."
746 (defun org-src-native-tab-command-maybe ()
747 "Perform language-specific TAB action.
748 Alter code block according to effect of TAB in the language major
750 (and org-src-tab-acts-natively
751 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
752 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
754 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
756 (defun org-src-font-lock-fontify-block (lang start end
)
758 This function is called by emacs automatic fontification, as long
759 as `org-src-fontify-natively' is non-nil. For manual
760 fontification of code blocks see `org-src-fontify-block' and
761 `org-src-fontify-buffer'"
762 (let* ((lang-mode (org-src-get-lang-mode lang
))
763 (string (buffer-substring-no-properties start end
))
764 (modified (buffer-modified-p))
765 (org-buffer (current-buffer)) pos next
)
766 (remove-text-properties start end
'(face nil
))
769 (concat " org-src-fontification:" (symbol-name lang-mode
)))
770 (delete-region (point-min) (point-max))
772 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
773 (font-lock-fontify-buffer)
774 (setq pos
(point-min))
775 (while (setq next
(next-single-property-change pos
'face
))
777 (+ start
(1- pos
)) (+ start next
) 'face
778 (get-text-property pos
'face
) org-buffer
)
782 '(font-lock-fontified t fontified t font-lock-multiline t
))
783 (set-buffer-modified-p modified
))
784 t
) ;; Tell `org-fontify-meta-lines-and-blocks' that we fontified
786 (defun org-src-fontify-block ()
787 "Fontify code block at point."
790 (let ((org-src-fontify-natively t
)
791 (info (org-edit-src-find-region-and-lang)))
792 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
794 (defun org-src-fontify-buffer ()
795 "Fontify all code blocks in the current buffer"
797 (org-babel-map-src-blocks nil
798 (org-src-fontify-block)))
800 (defun org-src-get-lang-mode (lang)
801 "Return major mode that should be used for LANG.
802 LANG is a string, and the returned major mode is a symbol."
805 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
806 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
810 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
811 ;;; org-src.el ends here