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
))
46 (declare-function org-pop-to-buffer-same-window
47 "org-compat" (&optional buffer-or-name norecord label
))
49 (defcustom org-edit-src-region-extra nil
50 "Additional regexps to identify regions for editing with `org-edit-src-code'.
51 For examples see the function `org-edit-src-find-region-and-lang'.
52 The regular expression identifying the begin marker should end with a newline,
53 and the regexp marking the end line should start with a newline, to make sure
54 there are kept outside the narrowed region."
55 :group
'org-edit-structure
58 (regexp :tag
"begin regexp")
59 (regexp :tag
"end regexp")
60 (choice :tag
"language"
61 (string :tag
"specify")
62 (integer :tag
"from match group")
63 (const :tag
"from `lang' element")
64 (const :tag
"from `style' element")))))
66 (defcustom org-coderef-label-format
"(ref:%s)"
67 "The default coderef format.
68 This format string will be used to search for coderef labels in literal
69 examples (EXAMPLE and SRC blocks). The format can be overwritten in
70 an individual literal example with the -l option, like
72 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
76 If you want to use this for HTML export, make sure that the format does
77 not introduce special font-locking, and avoid the HTML special
78 characters `<', `>', and `&'. The reason for this restriction is that
79 the labels are searched for only after htmlize has done its job."
80 :group
'org-edit-structure
; FIXME this is not in the right group
83 (defcustom org-edit-fixed-width-region-mode
'artist-mode
84 "The mode that should be used to edit fixed-width regions.
85 These are the regions where each line starts with a colon."
86 :group
'org-edit-structure
90 (const fundamental-mode
)
91 (function :tag
"Other (specify)")))
93 (defcustom org-src-preserve-indentation nil
94 "If non-nil preserve leading whitespace characters on export.
95 If non-nil leading whitespace characters in source code blocks
96 are preserved on export, and when switching between the org
97 buffer and the language mode edit buffer. If this variable is nil
98 then, after editing with \\[org-edit-src-code], the
99 minimum (across-lines) number of leading whitespace characters
100 are removed from all lines, and the code block is uniformly
101 indented according to the value of `org-edit-src-content-indentation'."
102 :group
'org-edit-structure
105 (defcustom org-edit-src-content-indentation
2
106 "Indentation for the content of a source code block.
107 This should be the number of spaces added to the indentation of the #+begin
108 line in order to compute the indentation of the block content after
109 editing it with \\[org-edit-src-code]. Has no effect if
110 `org-src-preserve-indentation' is non-nil."
111 :group
'org-edit-structure
114 (defvar org-src-strip-leading-and-trailing-blank-lines nil
115 "If non-nil, blank lines are removed when exiting the code edit
118 (defcustom org-edit-src-persistent-message t
119 "Non-nil means show persistent exit help message while editing src examples.
120 The message is shown in the header-line, which will be created in the
121 first line of the window showing the editing buffer."
122 :group
'org-edit-structure
125 (defcustom org-src-window-setup
'reorganize-frame
126 "How the source code edit buffer should be displayed.
127 Possible values for this option are:
129 current-window Show edit buffer in the current window, keeping all other
131 other-window Use `switch-to-buffer-other-window' to display edit buffer.
132 reorganize-frame Show only two windows on the current frame, the current
133 window and the edit buffer. When exiting the edit buffer,
134 return to one window.
135 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
136 Also, when exiting the edit buffer, kill that frame."
137 :group
'org-edit-structure
139 (const current-window
)
142 (const reorganize-frame
)))
144 (defvar org-src-mode-hook nil
145 "Hook run after Org switched a source code snippet to its Emacs mode.
148 - when editing a source code snippet with \"C-c '\".
149 - When formatting a source code snippet for export with htmlize.
151 You may want to use this hook for example to turn off `outline-minor-mode'
152 or similar things which you want to have when editing a source code file,
153 but which mess up the display of a snippet in Org exported files.")
155 (defcustom org-src-lang-modes
156 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
157 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
158 ("calc" . fundamental
) ("C" . c
))
159 "Alist mapping languages to their major mode.
160 The key is the language name, the value is the string that should
161 be inserted as the name of the major mode. For many languages this is
162 simple, but for language where this is not the case, this variable
163 provides a way to simplify things on the user side.
164 For example, there is no ocaml-mode in Emacs, but the mode to use is
166 :group
'org-edit-structure
169 (string "Language name")
170 (symbol "Major mode"))))
172 ;;; Editing source examples
174 (defvar org-src-mode-map
(make-sparse-keymap))
175 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
177 (defvar org-edit-src-force-single-line nil
)
178 (defvar org-edit-src-from-org-mode nil
)
179 (defvar org-edit-src-allow-write-back-p t
)
180 (defvar org-edit-src-picture nil
)
181 (defvar org-edit-src-beg-marker nil
)
182 (defvar org-edit-src-end-marker nil
)
183 (defvar org-edit-src-overlay nil
)
184 (defvar org-edit-src-block-indentation nil
)
185 (defvar org-edit-src-saved-temp-window-config nil
)
187 (defvar org-src-ask-before-returning-to-edit-buffer t
188 "If nil, when org-edit-src code is used on a block that already
189 has an active edit buffer, it will switch to that edit buffer
190 immediately; otherwise it will ask whether you want to return
191 to the existing edit buffer.")
193 (defvar org-src-babel-info nil
)
195 (define-minor-mode org-src-mode
196 "Minor mode for language major mode buffers generated by org.
197 This minor mode is turned on in two situations:
198 - when editing a source code snippet with \"C-c '\".
199 - When formatting a source code snippet for export with htmlize.
200 There is a mode hook, and keybindings for `org-edit-src-exit' and
201 `org-edit-src-save'")
203 (defun org-edit-src-code (&optional context code edit-buffer-name
)
204 "Edit the source code example at point.
205 The example is copied to a separate buffer, and that buffer is
206 switched to the correct language mode. When done, exit with
207 \\[org-edit-src-exit]. This will remove the original code in the
208 Org buffer, and replace it with the edited version. Optional
209 argument CONTEXT is used by \\[org-edit-src-save] when calling
210 this function. See \\[org-src-window-setup] to configure the
211 display of windows containing the Org buffer and the code
214 (unless (eq context
'save
)
215 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
216 (let ((mark (and (org-region-active-p) (mark)))
218 (info (org-edit-src-find-region-and-lang))
219 (full-info (org-babel-get-src-block-info))
220 (org-mode-p (or (org-mode-p) (derived-mode-p 'org-mode
)))
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 transmitted-variables
)
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
)
239 block-nindent
(nth 5 info
)
240 lang-f
(intern (concat lang
"-mode"))
241 begline
(save-excursion (goto-char beg
) (org-current-line))
242 transmitted-variables
243 `((org-edit-src-content-indentation
244 ,org-edit-src-content-indentation
)
245 (org-edit-src-force-single-line ,single
)
246 (org-edit-src-from-org-mode ,org-mode-p
)
247 (org-edit-src-allow-write-back-p ,allow-write-back-p
)
248 (org-src-preserve-indentation ,org-src-preserve-indentation
)
249 (org-src-babel-info ,(org-babel-get-src-block-info 'light
))
250 (org-coderef-label-format
251 ,(or (nth 4 info
) org-coderef-label-format
))
252 (org-edit-src-beg-marker ,beg
)
253 (org-edit-src-end-marker ,end
)
254 (org-edit-src-block-indentation ,block-nindent
)))
255 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
256 (save-excursion (goto-char (min mark end
))
257 (setq markline
(org-current-line)
258 markcol
(current-column))))
259 (if (equal lang-f
'table.el-mode
)
260 (setq lang-f
(lambda ()
262 (if (org-bound-and-true-p flyspell-mode
)
265 (org-set-local 'org-edit-src-content-indentation
0))))
266 (unless (functionp lang-f
)
267 (error "No such language mode: %s" lang-f
))
269 (if (> (point) end
) (goto-char end
))
270 (setq line
(org-current-line)
271 col
(current-column)))
272 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
273 (if org-src-ask-before-returning-to-edit-buffer
274 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t
))
275 (org-src-switch-to-buffer buffer
'return
)
277 (with-current-buffer buffer
278 (if (boundp 'org-edit-src-overlay
)
279 (delete-overlay org-edit-src-overlay
)))
280 (kill-buffer buffer
))
281 (setq buffer
(generate-new-buffer
283 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
284 (setq ovl
(make-overlay beg end
))
285 (overlay-put ovl
'edit-buffer buffer
)
286 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
287 (overlay-put ovl
'face
'secondary-selection
)
290 (let ((map (make-sparse-keymap)))
291 (define-key map
[mouse-1
] 'org-edit-src-continue
)
293 (overlay-put ovl
:read-only
"Leave me alone")
294 (setq transmitted-variables
295 (append transmitted-variables
`((org-edit-src-overlay ,ovl
))))
296 (org-src-switch-to-buffer buffer
'edit
)
297 (if (eq single
'macro-definition
)
298 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
300 (remove-text-properties (point-min) (point-max)
301 '(display nil invisible nil intangible nil
))
302 (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables
))
303 (setq total-nindent
(or (org-do-remove-indentation) 0)))
304 (let ((org-inhibit-startup t
))
308 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
309 (dolist (pair transmitted-variables
)
310 (org-set-local (car pair
) (cadr pair
)))
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
)))
317 (org-goto-line (1+ (- markline begline
)))
319 (if org-src-preserve-indentation markcol
320 (max 0 (- markcol total-nindent
))))
321 (push-mark (point) 'no-message t
)
322 (setq deactivate-mark nil
))
323 (org-goto-line (1+ (- line begline
)))
325 (if org-src-preserve-indentation col
(max 0 (- col total-nindent
))))
327 (set-buffer-modified-p nil
)
328 (and org-edit-src-persistent-message
329 (org-set-local 'header-line-format msg
))
330 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
331 (when (fboundp edit-prep-func
)
332 (funcall edit-prep-func full-info
))))
335 (defun org-edit-src-continue (e)
338 (let ((buf (get-char-property (point) 'edit-buffer
)))
339 (if buf
(org-src-switch-to-buffer buf
'continue
)
340 (error "Something is wrong here"))))
342 (defun org-src-switch-to-buffer (buffer context
)
343 (case org-src-window-setup
345 (org-pop-to-buffer-same-window buffer
))
347 (switch-to-buffer-other-window buffer
))
351 (let ((frame (selected-frame)))
352 (switch-to-buffer-other-frame buffer
)
353 (delete-frame frame
)))
355 (kill-buffer (current-buffer))
356 (org-pop-to-buffer-same-window buffer
))
358 (switch-to-buffer-other-frame buffer
))))
360 (if (eq context
'edit
) (delete-other-windows))
361 (org-switch-to-buffer-other-window buffer
)
362 (if (eq context
'exit
) (delete-other-windows)))
366 (message "Invalid value %s for org-src-window-setup"
367 (symbol-name org-src-window-setup
))
368 (org-pop-to-buffer-same-window buffer
))))
370 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
371 "Construct the buffer name for a source editing buffer."
372 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
374 (defun org-edit-src-find-buffer (beg end
)
375 "Find a source editing buffer that is already editing the region BEG to END."
379 (with-current-buffer b
380 (if (and (string-match "\\`*Org Src " (buffer-name))
381 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
382 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
383 (equal beg org-edit-src-beg-marker
)
384 (equal end org-edit-src-end-marker
))
385 (throw 'exit
(current-buffer)))))
389 (defun org-edit-fixed-width-region ()
390 "Edit the fixed-width ascii drawing at point.
391 This must be a region where each line starts with a colon followed by
393 An new buffer is created and the fixed-width region is copied into it,
394 and the buffer is switched into `artist-mode' for editing. When done,
395 exit with \\[org-edit-src-exit]. The edited text will then replace
396 the fragment in the Org-mode buffer."
398 (let ((line (org-current-line))
399 (col (current-column))
401 (msg (substitute-command-keys
402 "Edit, then exit with C-c ' (C-c and single quote)"))
403 (org-mode-p (org-mode-p))
406 (preserve-indentation org-src-preserve-indentation
)
407 block-nindent ovl beg1 end1 code begline buffer
)
408 (beginning-of-line 1)
409 (if (looking-at "[ \t]*[^:\n \t]")
411 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
412 (setq beg1
(point) end1 beg1
)
414 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
415 (setq beg1
(point-at-bol 2))
416 (setq beg1
(point))))
418 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
419 (setq end1
(1- (match-beginning 0)))
420 (setq end1
(point))))
421 (org-goto-line line
))
422 (setq beg
(move-marker beg beg1
)
423 end
(move-marker end end1
)
424 code
(buffer-substring-no-properties beg end
)
425 begline
(save-excursion (goto-char beg
) (org-current-line)))
426 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
427 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
428 (org-pop-to-buffer-same-window buffer
)
430 (with-current-buffer buffer
431 (if (boundp 'org-edit-src-overlay
)
432 (delete-overlay org-edit-src-overlay
)))
433 (kill-buffer buffer
))
434 (setq buffer
(generate-new-buffer
435 (org-src-construct-edit-buffer-name
436 (buffer-name) "Fixed Width")))
437 (setq ovl
(make-overlay beg end
))
438 (overlay-put ovl
'face
'secondary-selection
)
439 (overlay-put ovl
'edit-buffer buffer
)
440 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
441 (overlay-put ovl
'face
'secondary-selection
)
444 (let ((map (make-sparse-keymap)))
445 (define-key map
[mouse-1
] 'org-edit-src-continue
)
447 (overlay-put ovl
:read-only
"Leave me alone")
448 (org-pop-to-buffer-same-window buffer
)
450 (remove-text-properties (point-min) (point-max)
451 '(display nil invisible nil intangible nil
))
452 (setq block-nindent
(or (org-do-remove-indentation) 0))
454 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
457 (t (funcall org-edit-fixed-width-region-mode
)))
458 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
459 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
460 (set (make-local-variable 'org-edit-src-picture
) t
)
461 (goto-char (point-min))
462 (while (re-search-forward "^[ \t]*: ?" nil t
)
464 (org-goto-line (1+ (- line begline
)))
465 (org-move-to-column (max 0 (- col block-nindent
2)))
466 (org-set-local 'org-edit-src-beg-marker beg
)
467 (org-set-local 'org-edit-src-end-marker end
)
468 (org-set-local 'org-edit-src-overlay ovl
)
469 (org-set-local 'org-edit-src-block-indentation block-nindent
)
470 (org-set-local 'org-edit-src-content-indentation
0)
471 (org-set-local 'org-src-preserve-indentation nil
)
473 (set-buffer-modified-p nil
)
474 (and org-edit-src-persistent-message
475 (org-set-local 'header-line-format msg
)))
479 (defun org-edit-src-find-region-and-lang ()
480 "Find the region and language for a local edit.
481 Return a list with beginning and end of the region, a string representing
482 the language, a switch telling if the content should be in a single line."
485 org-edit-src-region-extra
487 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
488 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
489 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
490 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
491 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
492 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
493 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
494 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
495 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
496 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
497 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
498 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
499 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
500 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
501 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
502 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
503 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
504 "\n" "fundamental" macro-definition
)
505 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
508 re1 re2 single beg end lang lfmt match-re1 ind entry
)
510 (while (setq entry
(pop re-list
))
511 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
512 single
(nth 3 entry
))
514 (if (or (looking-at re1
)
515 (re-search-backward re1 nil t
))
517 (setq match-re1
(match-string 0))
518 (setq beg
(match-end 0)
519 lang
(org-edit-src-get-lang lang
)
520 lfmt
(org-edit-src-get-label-format match-re1
)
521 ind
(org-edit-src-get-indentation (match-beginning 0)))
522 (if (and (re-search-forward re2 nil t
)
523 (>= (match-end 0) pos
))
524 (throw 'exit
(list beg
(match-beginning 0)
525 lang single lfmt ind
))))
526 (if (or (looking-at re2
)
527 (re-search-forward re2 nil t
))
529 (setq end
(match-beginning 0))
530 (if (and (re-search-backward re1 nil t
)
531 (<= (match-beginning 0) pos
))
533 (setq lfmt
(org-edit-src-get-label-format
535 ind
(org-edit-src-get-indentation
536 (match-beginning 0)))
538 (list (match-end 0) end
539 (org-edit-src-get-lang lang
)
540 single lfmt ind
)))))))))
541 (when (org-at-table.el-p
)
542 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
543 (setq beg
(1+ (point-at-eol)))
545 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
546 (progn (goto-char (point-max)) (newline)))
547 (setq end
(point-at-bol))
548 (setq ind
(org-edit-src-get-indentation beg
))
549 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
551 (defun org-edit-src-get-lang (lang)
552 "Extract the src language."
553 (let ((m (match-string 0)))
555 ((stringp lang
) lang
)
556 ((integerp lang
) (match-string lang
))
557 ((and (eq lang
'lang
)
558 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
560 ((and (eq lang
'style
)
561 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
565 (defun org-edit-src-get-label-format (s)
566 "Extract the label format."
568 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
569 (match-string 1 s
))))
571 (defun org-edit-src-get-indentation (pos)
572 "Count leading whitespace characters on line."
575 (org-get-indentation)))
577 (defun org-edit-src-exit (&optional context
)
578 "Exit special edit and protect problematic lines."
580 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
581 (error "This is not a sub-editing buffer, something is wrong"))
583 (let* ((beg org-edit-src-beg-marker
)
584 (end org-edit-src-end-marker
)
585 (ovl org-edit-src-overlay
)
586 (buffer (current-buffer))
587 (single (org-bound-and-true-p org-edit-src-force-single-line
))
588 (macro (eq single
'macro-definition
))
589 (total-nindent (+ (or org-edit-src-block-indentation
0)
590 org-edit-src-content-indentation
))
591 (preserve-indentation org-src-preserve-indentation
)
592 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
593 (delta 0) code line col indent
)
594 (when allow-write-back-p
595 (unless preserve-indentation
(untabify (point-min) (point-max)))
596 (if org-src-strip-leading-and-trailing-blank-lines
598 (goto-char (point-min))
599 (if (looking-at "[ \t\n]*\n") (replace-match ""))
601 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
602 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
605 col
(current-column))
606 (when allow-write-back-p
608 (goto-char (point-min))
609 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
610 (goto-char (point-min))
612 (while (re-search-forward "\n" nil t
)
614 (replace-match (if macro
"\\n" " ") t t
))
615 (when (and macro
(> cnt
0))
616 (goto-char (point-max)) (insert "\\n")))
617 (goto-char (point-min))
618 (if (looking-at "\\s-*") (replace-match " ")))
619 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
620 (goto-char (point-min))
621 (while (re-search-forward
622 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
623 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
624 (replace-match ",\\1")))
625 (when (org-bound-and-true-p org-edit-src-picture
)
626 (setq preserve-indentation nil
)
627 (untabify (point-min) (point-max))
628 (goto-char (point-min))
629 (while (re-search-forward "^" nil t
)
630 (replace-match ": ")))
631 (unless (or single preserve-indentation
(= total-nindent
0))
632 (setq indent
(make-string total-nindent ?\
))
633 (goto-char (point-min))
634 (while (re-search-forward "^" nil t
)
635 (replace-match indent
)))
636 (if (org-bound-and-true-p org-edit-src-picture
)
637 (setq total-nindent
(+ total-nindent
2)))
638 (setq code
(buffer-string))
639 (set-buffer-modified-p nil
))
640 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
643 (when allow-write-back-p
644 (delete-region beg end
)
647 (if single
(just-one-space)))
648 (if (memq t
(mapcar (lambda (overlay)
649 (eq (overlay-get overlay
'invisible
)
651 (overlays-at (point))))
652 ;; Block is hidden; put point at start of block
653 (beginning-of-line 0)
654 ;; Block is visible, put point where it was in the code buffer
655 (org-goto-line (1- (+ (org-current-line) line
)))
656 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
657 (move-marker beg nil
)
658 (move-marker end nil
))
659 (unless (eq context
'save
)
660 (when org-edit-src-saved-temp-window-config
661 (set-window-configuration org-edit-src-saved-temp-window-config
)
662 (setq org-edit-src-saved-temp-window-config nil
))))
664 (defun org-edit-src-save ()
665 "Save parent buffer with current state source-code buffer."
667 (let ((p (point)) (m (mark)) msg
)
668 (save-window-excursion
669 (org-edit-src-exit 'save
)
671 (setq msg
(current-message))
672 (if (eq org-src-window-setup
'other-frame
)
673 (let ((org-src-window-setup 'current-window
))
674 (org-edit-src-code 'save
))
675 (org-edit-src-code 'save
)))
676 (push-mark m
'nomessage
)
677 (goto-char (min p
(point-max)))
678 (message (or msg
""))))
680 (defun org-src-mode-configure-edit-buffer ()
681 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
682 (org-add-hook 'kill-buffer-hook
683 #'(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
684 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
686 (setq buffer-offer-save t
)
687 (setq buffer-file-name
688 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
689 "[" (buffer-name) "]"))
690 (if (featurep 'xemacs
)
692 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
693 (setq write-contents-hooks
'(org-edit-src-save)))
694 (setq write-contents-functions
'(org-edit-src-save))))
695 (setq buffer-read-only t
))))
697 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
700 (defun org-src-associate-babel-session (info)
701 "Associate edit buffer with comint session."
703 (let ((session (cdr (assoc :session
(nth 2 info
)))))
704 (and session
(not (string= session
"none"))
705 (org-babel-comint-buffer-livep session
)
706 ((lambda (f) (and (fboundp f
) (funcall f session
)))
707 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
709 (defun org-src-babel-configure-edit-buffer ()
710 (when org-src-babel-info
711 (org-src-associate-babel-session org-src-babel-info
)))
713 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
714 (defmacro org-src-do-at-code-block
(&rest body
)
715 "Execute a command from an edit buffer in the Org-mode buffer."
716 `(let ((beg-marker org-edit-src-beg-marker
))
718 (with-current-buffer (marker-buffer beg-marker
)
719 (goto-char (marker-position beg-marker
))
722 (defun org-src-do-key-sequence-at-code-block (&optional key
)
723 "Execute key sequence at code block in the source Org buffer.
724 The command bound to KEY in the Org-babel key map is executed
725 remotely with point temporarily at the start of the code block in
728 This command is not bound to a key by default, to avoid conflicts
729 with language major mode bindings. To bind it to C-c @ in all
730 language major modes, you could use
732 (add-hook 'org-src-mode-hook
733 (lambda () (define-key org-src-mode-map \"\\C-c@\"
734 'org-src-do-key-sequence-at-code-block)))
736 In that case, for example, C-c @ t issued in code edit buffers
737 would tangle the current Org code block, C-c @ e would execute
738 the block and C-c @ h would display the other available
740 (interactive "kOrg-babel key: ")
741 (if (equal key
(kbd "C-g")) (keyboard-quit)
743 (org-src-do-at-code-block
745 (lookup-key org-babel-map key
)))))
747 (defcustom org-src-tab-acts-natively nil
748 "If non-nil, the effect of TAB in a code block is as if it were
749 issued in the language major mode buffer."
753 (defun org-src-native-tab-command-maybe ()
754 "Perform language-specific TAB action.
755 Alter code block according to effect of TAB in the language major
757 (and org-src-tab-acts-natively
758 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
759 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
761 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
763 (defun org-src-font-lock-fontify-block (lang start end
)
765 This function is called by emacs automatic fontification, as long
766 as `org-src-fontify-natively' is non-nil. For manual
767 fontification of code blocks see `org-src-fontify-block' and
768 `org-src-fontify-buffer'"
769 (let ((lang-mode (org-src-get-lang-mode lang
)))
770 (if (fboundp lang-mode
)
771 (let ((string (buffer-substring-no-properties start end
))
772 (modified (buffer-modified-p))
773 (org-buffer (current-buffer)) pos next
)
774 (remove-text-properties start end
'(face nil
))
777 (concat " org-src-fontification:" (symbol-name lang-mode
)))
778 (delete-region (point-min) (point-max))
779 (insert (concat string
" ")) ;; so there's a final property change
780 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
781 (font-lock-fontify-buffer)
782 (setq pos
(point-min))
783 (while (setq next
(next-single-property-change pos
'face
))
785 (+ start
(1- pos
)) (+ start next
) 'face
786 (get-text-property pos
'face
) org-buffer
)
790 '(font-lock-fontified t fontified t font-lock-multiline t
))
791 (set-buffer-modified-p modified
)))))
793 (defun org-src-fontify-block ()
794 "Fontify code block at point."
797 (let ((org-src-fontify-natively t
)
798 (info (org-edit-src-find-region-and-lang)))
799 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
801 (defun org-src-fontify-buffer ()
802 "Fontify all code blocks in the current buffer"
804 (org-babel-map-src-blocks nil
805 (org-src-fontify-block)))
807 (defun org-src-get-lang-mode (lang)
808 "Return major mode that should be used for LANG.
809 LANG is a string, and the returned major mode is a symbol."
812 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
813 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
817 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
818 ;;; org-src.el ends here