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 "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 quietp
)
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 (use-region-p) (mark)))
216 (info (org-edit-src-find-region-and-lang))
217 (babel-info (org-babel-get-src-block-info))
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 end
))
243 (save-excursion (goto-char mark
)
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
)))
324 (unless quietp
(message "%s" msg
))
327 (defun org-edit-src-continue (e)
330 (let ((buf (get-char-property (point) 'edit-buffer
)))
331 (if buf
(org-src-switch-to-buffer buf
'continue
)
332 (error "Something is wrong here"))))
334 (defun org-src-switch-to-buffer (buffer context
)
335 (case org-src-window-setup
337 (switch-to-buffer buffer
))
339 (switch-to-buffer-other-window buffer
))
343 (let ((frame (selected-frame)))
344 (switch-to-buffer-other-frame buffer
)
345 (delete-frame frame
)))
347 (kill-buffer (current-buffer))
348 (switch-to-buffer buffer
))
350 (switch-to-buffer-other-frame buffer
))))
352 (if (eq context
'edit
) (delete-other-windows))
353 (org-switch-to-buffer-other-window buffer
)
354 (if (eq context
'exit
) (delete-other-windows)))
358 (message "Invalid value %s for org-src-window-setup"
359 (symbol-name org-src-window-setup
))
360 (switch-to-buffer buffer
))))
362 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
363 "Construct the buffer name for a source editing buffer."
364 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
366 (defun org-edit-src-find-buffer (beg end
)
367 "Find a source editing buffer that is already editing the region BEG to END."
371 (with-current-buffer b
372 (if (and (string-match "\\`*Org Src " (buffer-name))
373 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
374 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
375 (equal beg org-edit-src-beg-marker
)
376 (equal end org-edit-src-end-marker
))
377 (throw 'exit
(current-buffer)))))
381 (defun org-edit-fixed-width-region ()
382 "Edit the fixed-width ascii drawing at point.
383 This must be a region where each line starts with a colon followed by
385 An new buffer is created and the fixed-width region is copied into it,
386 and the buffer is switched into `artist-mode' for editing. When done,
387 exit with \\[org-edit-src-exit]. The edited text will then replace
388 the fragment in the Org-mode buffer."
390 (let ((line (org-current-line))
391 (col (current-column))
393 (msg (substitute-command-keys
394 "Edit, then exit with C-c ' (C-c and single quote)"))
395 (org-mode-p (eq major-mode
'org-mode
))
398 (preserve-indentation org-src-preserve-indentation
)
399 block-nindent ovl beg1 end1 code begline buffer
)
400 (beginning-of-line 1)
401 (if (looking-at "[ \t]*[^:\n \t]")
403 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
404 (setq beg1
(point) end1 beg1
)
406 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
407 (setq beg1
(point-at-bol 2))
408 (setq beg1
(point))))
410 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
411 (setq end1
(1- (match-beginning 0)))
412 (setq end1
(point))))
413 (org-goto-line line
))
414 (setq beg
(move-marker beg beg1
)
415 end
(move-marker end end1
)
416 code
(buffer-substring-no-properties beg end
)
417 begline
(save-excursion (goto-char beg
) (org-current-line)))
418 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
419 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
420 (switch-to-buffer buffer
)
422 (with-current-buffer buffer
423 (if (boundp 'org-edit-src-overlay
)
424 (delete-overlay org-edit-src-overlay
)))
425 (kill-buffer buffer
))
426 (setq buffer
(generate-new-buffer
427 (org-src-construct-edit-buffer-name
428 (buffer-name) "Fixed Width")))
429 (setq ovl
(make-overlay beg end
))
430 (overlay-put ovl
'face
'secondary-selection
)
431 (overlay-put ovl
'edit-buffer buffer
)
432 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
433 (overlay-put ovl
'face
'secondary-selection
)
436 (let ((map (make-sparse-keymap)))
437 (define-key map
[mouse-1
] 'org-edit-src-continue
)
439 (overlay-put ovl
:read-only
"Leave me alone")
440 (switch-to-buffer buffer
)
442 (remove-text-properties (point-min) (point-max)
443 '(display nil invisible nil intangible nil
))
444 (setq block-nindent
(or (org-do-remove-indentation) 0))
446 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
449 (t (funcall org-edit-fixed-width-region-mode
)))
450 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
451 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
452 (set (make-local-variable 'org-edit-src-picture
) t
)
453 (goto-char (point-min))
454 (while (re-search-forward "^[ \t]*: ?" nil t
)
456 (org-goto-line (1+ (- line begline
)))
457 (org-move-to-column (max 0 (- col block-nindent
2)))
458 (org-set-local 'org-edit-src-beg-marker beg
)
459 (org-set-local 'org-edit-src-end-marker end
)
460 (org-set-local 'org-edit-src-overlay ovl
)
461 (org-set-local 'org-edit-src-block-indentation block-nindent
)
462 (org-set-local 'org-edit-src-content-indentation
0)
463 (org-set-local 'org-src-preserve-indentation nil
)
465 (set-buffer-modified-p nil
)
466 (and org-edit-src-persistent-message
467 (org-set-local 'header-line-format msg
)))
471 (defun org-edit-src-find-region-and-lang ()
472 "Find the region and language for a local edit.
473 Return a list with beginning and end of the region, a string representing
474 the language, a switch telling if the content should be in a single line."
477 org-edit-src-region-extra
479 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
480 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
481 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
482 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
483 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
484 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
485 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
486 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
487 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
488 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
489 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
490 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
491 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
492 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
493 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
494 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
495 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
496 "\n" "fundamental" macro-definition
)
497 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
500 re1 re2 single beg end lang lfmt match-re1 ind entry
)
502 (while (setq entry
(pop re-list
))
503 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
504 single
(nth 3 entry
))
506 (if (or (looking-at re1
)
507 (re-search-backward re1 nil t
))
509 (setq match-re1
(match-string 0))
510 (setq beg
(match-end 0)
511 lang
(org-edit-src-get-lang lang
)
512 lfmt
(org-edit-src-get-label-format match-re1
)
513 ind
(org-edit-src-get-indentation (match-beginning 0)))
514 (if (and (re-search-forward re2 nil t
)
515 (>= (match-end 0) pos
))
516 (throw 'exit
(list beg
(match-beginning 0)
517 lang single lfmt ind
))))
518 (if (or (looking-at re2
)
519 (re-search-forward re2 nil t
))
521 (setq end
(match-beginning 0))
522 (if (and (re-search-backward re1 nil t
)
523 (<= (match-beginning 0) pos
))
525 (setq lfmt
(org-edit-src-get-label-format
527 ind
(org-edit-src-get-indentation
528 (match-beginning 0)))
530 (list (match-end 0) end
531 (org-edit-src-get-lang lang
)
532 single lfmt ind
)))))))))
533 (when (org-at-table.el-p
)
534 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
535 (setq beg
(1+ (point-at-eol)))
537 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
538 (progn (goto-char (point-max)) (newline)))
539 (setq end
(point-at-bol))
540 (setq ind
(org-edit-src-get-indentation beg
))
541 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
543 (defun org-edit-src-get-lang (lang)
544 "Extract the src language."
545 (let ((m (match-string 0)))
547 ((stringp lang
) lang
)
548 ((integerp lang
) (match-string lang
))
549 ((and (eq lang
'lang
)
550 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
552 ((and (eq lang
'style
)
553 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
557 (defun org-edit-src-get-label-format (s)
558 "Extract the label format."
560 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
561 (match-string 1 s
))))
563 (defun org-edit-src-get-indentation (pos)
564 "Count leading whitespace characters on line."
567 (org-get-indentation)))
569 (defun org-edit-src-exit (&optional context
)
570 "Exit special edit and protect problematic lines."
572 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
573 (error "This is not a sub-editing buffer, something is wrong"))
575 (let* ((beg org-edit-src-beg-marker
)
576 (end org-edit-src-end-marker
)
577 (ovl org-edit-src-overlay
)
578 (buffer (current-buffer))
579 (single (org-bound-and-true-p org-edit-src-force-single-line
))
580 (macro (eq single
'macro-definition
))
581 (total-nindent (+ (or org-edit-src-block-indentation
0)
582 org-edit-src-content-indentation
))
583 (preserve-indentation org-src-preserve-indentation
)
584 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
585 (delta 0) code line col indent
)
586 (when allow-write-back-p
587 (unless preserve-indentation
(untabify (point-min) (point-max)))
588 (if org-src-strip-leading-and-trailing-blank-lines
590 (goto-char (point-min))
591 (if (looking-at "[ \t\n]*\n") (replace-match ""))
593 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
594 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
597 col
(current-column))
598 (when allow-write-back-p
600 (goto-char (point-min))
601 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
602 (goto-char (point-min))
604 (while (re-search-forward "\n" nil t
)
606 (replace-match (if macro
"\\n" " ") t t
))
607 (when (and macro
(> cnt
0))
608 (goto-char (point-max)) (insert "\\n")))
609 (goto-char (point-min))
610 (if (looking-at "\\s-*") (replace-match " ")))
611 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
612 (goto-char (point-min))
613 (while (re-search-forward
614 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
615 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
616 (replace-match ",\\1")))
617 (when (org-bound-and-true-p org-edit-src-picture
)
618 (setq preserve-indentation nil
)
619 (untabify (point-min) (point-max))
620 (goto-char (point-min))
621 (while (re-search-forward "^" nil t
)
622 (replace-match ": ")))
623 (unless (or single preserve-indentation
(= total-nindent
0))
624 (setq indent
(make-string total-nindent ?\
))
625 (goto-char (point-min))
626 (while (re-search-forward "^" nil t
)
627 (replace-match indent
)))
628 (if (org-bound-and-true-p org-edit-src-picture
)
629 (setq total-nindent
(+ total-nindent
2)))
630 (setq code
(buffer-string))
631 (set-buffer-modified-p nil
))
632 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
635 (when allow-write-back-p
636 (delete-region beg end
)
639 (if single
(just-one-space)))
640 (if (memq t
(mapcar (lambda (overlay)
641 (eq (overlay-get overlay
'invisible
)
643 (overlays-at (point))))
644 ;; Block is hidden; put point at start of block
645 (beginning-of-line 0)
646 ;; Block is visible, put point where it was in the code buffer
647 (org-goto-line (1- (+ (org-current-line) line
)))
648 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
649 (move-marker beg nil
)
650 (move-marker end nil
))
651 (unless (eq context
'save
)
652 (when org-edit-src-saved-temp-window-config
653 (set-window-configuration org-edit-src-saved-temp-window-config
)
654 (setq org-edit-src-saved-temp-window-config nil
))))
656 (defun org-edit-src-save ()
657 "Save parent buffer with current state source-code buffer."
659 (let ((p (point)) (m (mark)) msg
)
660 (save-window-excursion
661 (org-edit-src-exit 'save
)
663 (setq msg
(current-message))
664 (if (eq org-src-window-setup
'other-frame
)
665 (let ((org-src-window-setup 'current-window
))
666 (org-edit-src-code 'save
))
667 (org-edit-src-code 'save
)))
668 (push-mark m
'nomessage
)
669 (goto-char (min p
(point-max)))
670 (message (or msg
""))))
672 (defun org-src-mode-configure-edit-buffer ()
673 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
674 (org-add-hook 'kill-buffer-hook
675 '(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
676 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
678 (setq buffer-offer-save t
)
679 (setq buffer-file-name
680 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
681 "[" (buffer-name) "]"))
682 (if (featurep 'xemacs
)
684 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
685 (setq write-contents-hooks
'(org-edit-src-save)))
686 (setq write-contents-functions
'(org-edit-src-save))))
687 (setq buffer-read-only t
))))
689 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
692 (defun org-src-associate-babel-session (info)
693 "Associate edit buffer with comint session."
695 (let ((session (cdr (assoc :session
(nth 2 info
)))))
696 (and session
(not (string= session
"none"))
697 (org-babel-comint-buffer-livep session
)
698 ((lambda (f) (and (fboundp f
) (funcall f session
)))
699 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
701 (defun org-src-babel-configure-edit-buffer ()
702 (when org-src-babel-info
703 (org-src-associate-babel-session org-src-babel-info
)))
705 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
706 (defmacro org-src-do-at-code-block
(&rest body
)
707 "Execute a command from an edit buffer in the Org-mode buffer."
708 `(let ((beg-marker org-edit-src-beg-marker
))
710 (with-current-buffer (marker-buffer beg-marker
)
711 (goto-char (marker-position beg-marker
))
714 (defun org-src-do-key-sequence-at-code-block (&optional key
)
715 "Execute key sequence at code block in the source Org buffer.
716 The command bound to KEY in the Org-babel key map is executed
717 remotely with point temporarily at the start of the code block in
720 This command is not bound to a key by default, to avoid conflicts
721 with language major mode bindings. To bind it to C-c @ in all
722 language major modes, you could use
724 (add-hook 'org-src-mode-hook
725 (lambda () (define-key org-src-mode-map \"\\C-c@\"
726 'org-src-do-key-sequence-at-code-block)))
728 In that case, for example, C-c @ t issued in code edit buffers
729 would tangle the current Org code block, C-c @ e would execute
730 the block and C-c @ h would display the other available
732 (interactive "kOrg-babel key: ")
733 (if (equal key
(kbd "C-g")) (keyboard-quit)
735 (org-src-do-at-code-block
737 (lookup-key org-babel-map key
)))))
739 (defcustom org-src-tab-acts-natively nil
740 "If non-nil, the effect of TAB in a code block is as if it were
741 issued in the language major mode buffer."
745 (defun org-src-native-tab-command-maybe ()
746 "Perform language-specific TAB action.
747 Alter code block according to effect of TAB in the language major
749 (and org-src-tab-acts-natively
750 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
751 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
753 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
755 (defun org-src-font-lock-fontify-block (lang start end
)
757 This function is called by emacs automatic fontification, as long
758 as `org-src-fontify-natively' is non-nil. For manual
759 fontification of code blocks see `org-src-fontify-block' and
760 `org-src-fontify-buffer'"
761 (let* ((lang-mode (org-src-get-lang-mode lang
))
762 (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
))
783 t
) ;; Tell `org-fontify-meta-lines-and-blocks' that we fontified
785 (defun org-src-fontify-block ()
786 "Fontify code block at point."
789 (let ((org-src-fontify-natively t
)
790 (info (org-edit-src-find-region-and-lang)))
791 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
793 (defun org-src-fontify-buffer ()
794 "Fontify all code blocks in the current buffer"
796 (org-babel-map-src-blocks nil
797 (org-src-fontify-block)))
799 (defun org-src-get-lang-mode (lang)
800 "Return major mode that should be used for LANG.
801 LANG is a string, and the returned major mode is a symbol."
804 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
805 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
809 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
810 ;;; org-src.el ends here