org-protocol default template should be nil
[org-mode.git] / lisp / org-src.el
blob12d874e5ada358aabdae6a67da83d2d599181e3f
1 ;;; org-src.el --- Source code examples in Org
2 ;;
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5 ;;
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
11 ;; Version: 7.01trans
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;; Commentary:
31 ;; This file contains the code dealing with source code examples in Org-mode.
33 ;;; Code:
35 (require 'org-macs)
36 (require 'org-compat)
37 (require 'ob-keys)
38 (require 'ob-comint)
39 (eval-when-compile
40 (require 'cl))
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
54 :type '(repeat
55 (list
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))\"
71 ...
72 #+END_SRC
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
79 :type 'string)
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
85 :type '(choice
86 (const artist-mode)
87 (const picture-mode)
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
101 :type 'boolean)
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
110 :type 'integer)
112 (defcustom org-edit-src-persistent-message t
113 "Non-nil means show persistent exit help message while editing src examples.
114 The message is shown in the header-line, which will be created in the
115 first line of the window showing the editing buffer.
116 When nil, the message will only be shown intermittently in the echo area."
117 :group 'org-edit-structure
118 :type 'boolean)
120 (defcustom org-src-window-setup 'reorganize-frame
121 "How the source code edit buffer should be displayed.
122 Possible values for this option are:
124 current-window Show edit buffer in the current window, keeping all other
125 windows.
126 other-window Use `switch-to-buffer-other-window' to display edit buffer.
127 reorganize-frame Show only two windows on the current frame, the current
128 window and the edit buffer. When exiting the edit buffer,
129 return to one window.
130 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
131 Also, when exiting the edit buffer, kill that frame."
132 :group 'org-edit-structure
133 :type '(choice
134 (const current-window)
135 (const other-frame)
136 (const other-window)
137 (const reorganize-frame)))
139 (defvar org-src-mode-hook nil
140 "Hook run after Org switched a source code snippet to its Emacs mode.
141 This hook will run
143 - when editing a source code snippet with \"C-c '\".
144 - When formatting a source code snippet for export with htmlize.
146 You may want to use this hook for example to turn off `outline-minor-mode'
147 or similar things which you want to have when editing a source code file,
148 but which mess up the display of a snippet in Org exported files.")
150 (defcustom org-src-lang-modes
151 '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
152 ("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql))
153 "Alist mapping languages to their major mode.
154 The key is the language name, the value is the string that should
155 be inserted as the name of the major mode. For many languages this is
156 simple, but for language where this is not the case, this variable
157 provides a way to simplify things on the user side.
158 For example, there is no ocaml-mode in Emacs, but the mode to use is
159 `tuareg-mode'."
160 :group 'org-edit-structure
161 :type '(repeat
162 (cons
163 (string "Language name")
164 (symbol "Major mode"))))
166 ;;; Editing source examples
168 (defvar org-src-mode-map (make-sparse-keymap))
169 (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
171 (defvar org-edit-src-force-single-line nil)
172 (defvar org-edit-src-from-org-mode nil)
173 (defvar org-edit-src-allow-write-back-p t)
174 (defvar org-edit-src-picture nil)
175 (defvar org-edit-src-beg-marker nil)
176 (defvar org-edit-src-end-marker nil)
177 (defvar org-edit-src-overlay nil)
178 (defvar org-edit-src-block-indentation nil)
179 (defvar org-edit-src-saved-temp-window-config nil)
181 (defvar org-src-ask-before-returning-to-edit-buffer t
182 "If nil, when org-edit-src code is used on a block that already
183 has an active edit buffer, it will switch to that edit buffer
184 immediately; otherwise it will ask whether you want to return
185 to the existing edit buffer.")
187 (defvar org-src-babel-info nil)
189 (define-minor-mode org-src-mode
190 "Minor mode for language major mode buffers generated by org.
191 This minor mode is turned on in two situations:
192 - when editing a source code snippet with \"C-c '\".
193 - When formatting a source code snippet for export with htmlize.
194 There is a mode hook, and keybindings for `org-edit-src-exit' and
195 `org-edit-src-save'")
197 (defun org-edit-src-code (&optional context code edit-buffer-name quietp)
198 "Edit the source code example at point.
199 The example is copied to a separate buffer, and that buffer is
200 switched to the correct language mode. When done, exit with
201 \\[org-edit-src-exit]. This will remove the original code in the
202 Org buffer, and replace it with the edited version. Optional
203 argument CONTEXT is used by \\[org-edit-src-save] when calling
204 this function. See \\[org-src-window-setup] to configure the
205 display of windows containing the Org buffer and the code
206 buffer."
207 (interactive)
208 (unless (eq context 'save)
209 (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
210 (let ((line (org-current-line))
211 (col (current-column))
212 (case-fold-search t)
213 (info (org-edit-src-find-region-and-lang))
214 (babel-info (org-babel-get-src-block-info))
215 (org-mode-p (eq major-mode 'org-mode))
216 (beg (make-marker))
217 (end (make-marker))
218 (preserve-indentation org-src-preserve-indentation)
219 (allow-write-back-p (null code))
220 block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg)
221 (if (not info)
223 (setq beg (move-marker beg (nth 0 info))
224 end (move-marker end (nth 1 info))
225 msg (if allow-write-back-p
226 (substitute-command-keys
227 "Edit, then exit with C-c ' (C-c and single quote)")
228 "Exit with C-c ' (C-c and single quote)")
229 code (or code (buffer-substring-no-properties beg end))
230 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
231 (nth 2 info))
232 lang (if (symbolp lang) (symbol-name lang) lang)
233 single (nth 3 info)
234 lfmt (nth 4 info)
235 block-nindent (nth 5 info)
236 lang-f (intern (concat lang "-mode"))
237 begline (save-excursion (goto-char beg) (org-current-line)))
238 (if (equal lang-f 'table.el-mode)
239 (setq lang-f (lambda ()
240 (text-mode)
241 (if (org-bound-and-true-p flyspell-mode)
242 (flyspell-mode -1))
243 (table-recognize)
244 (org-set-local 'org-edit-src-content-indentation 0))))
245 (unless (functionp lang-f)
246 (error "No such language mode: %s" lang-f))
247 (org-goto-line line)
248 (if (and (setq buffer (org-edit-src-find-buffer beg end))
249 (if org-src-ask-before-returning-to-edit-buffer
250 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
251 (org-src-switch-to-buffer buffer 'return)
252 (when buffer
253 (with-current-buffer buffer
254 (if (boundp 'org-edit-src-overlay)
255 (delete-overlay org-edit-src-overlay)))
256 (kill-buffer buffer))
257 (setq buffer (generate-new-buffer
258 (or edit-buffer-name
259 (org-src-construct-edit-buffer-name (buffer-name) lang))))
260 (setq ovl (make-overlay beg end))
261 (overlay-put ovl 'edit-buffer buffer)
262 (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
263 (overlay-put ovl 'face 'secondary-selection)
264 (overlay-put ovl
265 'keymap
266 (let ((map (make-sparse-keymap)))
267 (define-key map [mouse-1] 'org-edit-src-continue)
268 map))
269 (overlay-put ovl :read-only "Leave me alone")
270 (org-src-switch-to-buffer buffer 'edit)
271 (if (eq single 'macro-definition)
272 (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
273 (insert code)
274 (remove-text-properties (point-min) (point-max)
275 '(display nil invisible nil intangible nil))
276 (unless preserve-indentation
277 (setq total-nindent (or (org-do-remove-indentation) 0)))
278 (let ((org-inhibit-startup t))
279 (funcall lang-f))
280 (set (make-local-variable 'org-edit-src-force-single-line) single)
281 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
282 (set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p)
283 (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
284 (when babel-info
285 (set (make-local-variable 'org-src-babel-info) babel-info))
286 (when lfmt
287 (set (make-local-variable 'org-coderef-label-format) lfmt))
288 (when org-mode-p
289 (goto-char (point-min))
290 (while (re-search-forward "^," nil t)
291 (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
292 (replace-match "")))
293 (org-goto-line (1+ (- line begline)))
294 (org-move-to-column
295 (if preserve-indentation col (max 0 (- col total-nindent))))
296 (org-set-local 'org-edit-src-beg-marker beg)
297 (org-set-local 'org-edit-src-end-marker end)
298 (org-set-local 'org-edit-src-overlay ovl)
299 (org-set-local 'org-edit-src-block-indentation block-nindent)
300 (org-src-mode)
301 (set-buffer-modified-p nil)
302 (and org-edit-src-persistent-message
303 (org-set-local 'header-line-format msg)))
304 (unless quietp (message "%s" msg))
305 t)))
307 (defun org-edit-src-continue (e)
308 (interactive "e")
309 (mouse-set-point e)
310 (let ((buf (get-char-property (point) 'edit-buffer)))
311 (if buf (org-src-switch-to-buffer buf 'continue)
312 (error "Something is wrong here"))))
314 (defun org-src-switch-to-buffer (buffer context)
315 (case org-src-window-setup
316 ('current-window
317 (switch-to-buffer buffer))
318 ('other-window
319 (switch-to-buffer-other-window buffer))
320 ('other-frame
321 (case context
322 ('exit
323 (let ((frame (selected-frame)))
324 (switch-to-buffer-other-frame buffer)
325 (delete-frame frame)))
326 ('save
327 (kill-buffer (current-buffer))
328 (switch-to-buffer buffer))
330 (switch-to-buffer-other-frame buffer))))
331 ('reorganize-frame
332 (if (eq context 'edit) (delete-other-windows))
333 (org-switch-to-buffer-other-window buffer)
334 (if (eq context 'exit) (delete-other-windows)))
335 ('switch-invisibly
336 (set-buffer buffer))
338 (message "Invalid value %s for org-src-window-setup"
339 (symbol-name org-src-window-setup))
340 (switch-to-buffer buffer))))
342 (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
343 "Construct the buffer name for a source editing buffer."
344 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
346 (defun org-edit-src-find-buffer (beg end)
347 "Find a source editing buffer that is already editing the region BEG to END."
348 (catch 'exit
349 (mapc
350 (lambda (b)
351 (with-current-buffer b
352 (if (and (string-match "\\`*Org Src " (buffer-name))
353 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
354 (local-variable-p 'org-edit-src-end-marker (current-buffer))
355 (equal beg org-edit-src-beg-marker)
356 (equal end org-edit-src-end-marker))
357 (throw 'exit (current-buffer)))))
358 (buffer-list))
359 nil))
361 (defun org-edit-fixed-width-region ()
362 "Edit the fixed-width ascii drawing at point.
363 This must be a region where each line starts with a colon followed by
364 a space character.
365 An new buffer is created and the fixed-width region is copied into it,
366 and the buffer is switched into `artist-mode' for editing. When done,
367 exit with \\[org-edit-src-exit]. The edited text will then replace
368 the fragment in the Org-mode buffer."
369 (interactive)
370 (let ((line (org-current-line))
371 (col (current-column))
372 (case-fold-search t)
373 (msg (substitute-command-keys
374 "Edit, then exit with C-c ' (C-c and single quote)"))
375 (org-mode-p (eq major-mode 'org-mode))
376 (beg (make-marker))
377 (end (make-marker))
378 (preserve-indentation org-src-preserve-indentation)
379 block-nindent ovl beg1 end1 code begline buffer)
380 (beginning-of-line 1)
381 (if (looking-at "[ \t]*[^:\n \t]")
383 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
384 (setq beg1 (point) end1 beg1)
385 (save-excursion
386 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
387 (setq beg1 (point-at-bol 2))
388 (setq beg1 (point))))
389 (save-excursion
390 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
391 (setq end1 (1- (match-beginning 0)))
392 (setq end1 (point))))
393 (org-goto-line line))
394 (setq beg (move-marker beg beg1)
395 end (move-marker end end1)
396 code (buffer-substring-no-properties beg end)
397 begline (save-excursion (goto-char beg) (org-current-line)))
398 (if (and (setq buffer (org-edit-src-find-buffer beg end))
399 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
400 (switch-to-buffer buffer)
401 (when buffer
402 (with-current-buffer buffer
403 (if (boundp 'org-edit-src-overlay)
404 (delete-overlay org-edit-src-overlay)))
405 (kill-buffer buffer))
406 (setq buffer (generate-new-buffer
407 (org-src-construct-edit-buffer-name
408 (buffer-name) "Fixed Width")))
409 (setq ovl (make-overlay beg end))
410 (overlay-put ovl 'face 'secondary-selection)
411 (overlay-put ovl 'edit-buffer buffer)
412 (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
413 (overlay-put ovl 'face 'secondary-selection)
414 (overlay-put ovl
415 'keymap
416 (let ((map (make-sparse-keymap)))
417 (define-key map [mouse-1] 'org-edit-src-continue)
418 map))
419 (overlay-put ovl :read-only "Leave me alone")
420 (switch-to-buffer buffer)
421 (insert code)
422 (remove-text-properties (point-min) (point-max)
423 '(display nil invisible nil intangible nil))
424 (setq block-nindent (or (org-do-remove-indentation) 0))
425 (cond
426 ((eq org-edit-fixed-width-region-mode 'artist-mode)
427 (fundamental-mode)
428 (artist-mode 1))
429 (t (funcall org-edit-fixed-width-region-mode)))
430 (set (make-local-variable 'org-edit-src-force-single-line) nil)
431 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
432 (set (make-local-variable 'org-edit-src-picture) t)
433 (goto-char (point-min))
434 (while (re-search-forward "^[ \t]*: ?" nil t)
435 (replace-match ""))
436 (org-goto-line (1+ (- line begline)))
437 (org-move-to-column (max 0 (- col block-nindent 2)))
438 (org-set-local 'org-edit-src-beg-marker beg)
439 (org-set-local 'org-edit-src-end-marker end)
440 (org-set-local 'org-edit-src-overlay ovl)
441 (org-set-local 'org-edit-src-block-indentation block-nindent)
442 (org-set-local 'org-edit-src-content-indentation 0)
443 (org-set-local 'org-src-preserve-indentation nil)
444 (org-src-mode)
445 (set-buffer-modified-p nil)
446 (and org-edit-src-persistent-message
447 (org-set-local 'header-line-format msg)))
448 (message "%s" msg)
449 t)))
451 (defun org-edit-src-find-region-and-lang ()
452 "Find the region and language for a local edit.
453 Return a list with beginning and end of the region, a string representing
454 the language, a switch telling if the content should be in a single line."
455 (let ((re-list
456 (append
457 org-edit-src-region-extra
459 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
460 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
461 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
462 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
463 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
464 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
465 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
466 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
467 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
468 ("^[ \t]*#\\+html:" "\n" "html" single-line)
469 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
470 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
471 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
472 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
473 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
474 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
475 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
476 "\n" "fundamental" macro-definition)
477 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
479 (pos (point))
480 re1 re2 single beg end lang lfmt match-re1 ind entry)
481 (catch 'exit
482 (while (setq entry (pop re-list))
483 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
484 single (nth 3 entry))
485 (save-excursion
486 (if (or (looking-at re1)
487 (re-search-backward re1 nil t))
488 (progn
489 (setq match-re1 (match-string 0))
490 (setq beg (match-end 0)
491 lang (org-edit-src-get-lang lang)
492 lfmt (org-edit-src-get-label-format match-re1)
493 ind (org-edit-src-get-indentation (match-beginning 0)))
494 (if (and (re-search-forward re2 nil t)
495 (>= (match-end 0) pos))
496 (throw 'exit (list beg (match-beginning 0)
497 lang single lfmt ind))))
498 (if (or (looking-at re2)
499 (re-search-forward re2 nil t))
500 (progn
501 (setq end (match-beginning 0))
502 (if (and (re-search-backward re1 nil t)
503 (<= (match-beginning 0) pos))
504 (progn
505 (setq lfmt (org-edit-src-get-label-format
506 (match-string 0))
507 ind (org-edit-src-get-indentation
508 (match-beginning 0)))
509 (throw 'exit
510 (list (match-end 0) end
511 (org-edit-src-get-lang lang)
512 single lfmt ind)))))))))
513 (when (org-at-table.el-p)
514 (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
515 (setq beg (1+ (point-at-eol)))
516 (goto-char beg)
517 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
518 (progn (goto-char (point-max)) (newline)))
519 (setq end (point-at-bol))
520 (setq ind (org-edit-src-get-indentation beg))
521 (throw 'exit (list beg end 'table.el nil nil ind))))))
523 (defun org-edit-src-get-lang (lang)
524 "Extract the src language."
525 (let ((m (match-string 0)))
526 (cond
527 ((stringp lang) lang)
528 ((integerp lang) (match-string lang))
529 ((and (eq lang 'lang)
530 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
531 (match-string 1 m))
532 ((and (eq lang 'style)
533 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
534 (match-string 1 m))
535 (t "fundamental"))))
537 (defun org-edit-src-get-label-format (s)
538 "Extract the label format."
539 (save-match-data
540 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
541 (match-string 1 s))))
543 (defun org-edit-src-get-indentation (pos)
544 "Count leading whitespace characters on line."
545 (save-match-data
546 (goto-char pos)
547 (org-get-indentation)))
549 (defun org-edit-src-exit (&optional context)
550 "Exit special edit and protect problematic lines."
551 (interactive)
552 (unless (org-bound-and-true-p org-edit-src-from-org-mode)
553 (error "This is not a sub-editing buffer, something is wrong"))
554 (widen)
555 (let* ((beg org-edit-src-beg-marker)
556 (end org-edit-src-end-marker)
557 (ovl org-edit-src-overlay)
558 (buffer (current-buffer))
559 (single (org-bound-and-true-p org-edit-src-force-single-line))
560 (macro (eq single 'macro-definition))
561 (total-nindent (+ (or org-edit-src-block-indentation 0)
562 org-edit-src-content-indentation))
563 (preserve-indentation org-src-preserve-indentation)
564 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p))
565 (delta 0) code line col indent)
566 (when allow-write-back-p
567 (unless preserve-indentation (untabify (point-min) (point-max)))
568 (save-excursion
569 (goto-char (point-min))
570 (if (looking-at "[ \t\n]*\n") (replace-match ""))
571 (unless macro
572 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))))
573 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
575 (org-current-line))
576 col (current-column))
577 (when allow-write-back-p
578 (when single
579 (goto-char (point-min))
580 (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
581 (goto-char (point-min))
582 (let ((cnt 0))
583 (while (re-search-forward "\n" nil t)
584 (setq cnt (1+ cnt))
585 (replace-match (if macro "\\n" " ") t t))
586 (when (and macro (> cnt 0))
587 (goto-char (point-max)) (insert "\\n")))
588 (goto-char (point-min))
589 (if (looking-at "\\s-*") (replace-match " ")))
590 (when (org-bound-and-true-p org-edit-src-from-org-mode)
591 (goto-char (point-min))
592 (while (re-search-forward
593 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
594 (if (eq (org-current-line) line) (setq delta (1+ delta)))
595 (replace-match ",\\1")))
596 (when (org-bound-and-true-p org-edit-src-picture)
597 (setq preserve-indentation nil)
598 (untabify (point-min) (point-max))
599 (goto-char (point-min))
600 (while (re-search-forward "^" nil t)
601 (replace-match ": ")))
602 (unless (or single preserve-indentation (= total-nindent 0))
603 (setq indent (make-string total-nindent ?\ ))
604 (goto-char (point-min))
605 (while (re-search-forward "^" nil t)
606 (replace-match indent)))
607 (if (org-bound-and-true-p org-edit-src-picture)
608 (setq total-nindent (+ total-nindent 2)))
609 (setq code (buffer-string))
610 (set-buffer-modified-p nil))
611 (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
612 (kill-buffer buffer)
613 (goto-char beg)
614 (when allow-write-back-p
615 (delete-region beg end)
616 (insert code)
617 (goto-char beg)
618 (if single (just-one-space)))
619 (if (memq t (mapcar (lambda (overlay)
620 (eq (overlay-get overlay 'invisible)
621 'org-hide-block))
622 (overlays-at (point))))
623 ;; Block is hidden; put point at start of block
624 (beginning-of-line 0)
625 ;; Block is visible, put point where it was in the code buffer
626 (org-goto-line (1- (+ (org-current-line) line)))
627 (org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
628 (move-marker beg nil)
629 (move-marker end nil))
630 (unless (eq context 'save)
631 (when org-edit-src-saved-temp-window-config
632 (set-window-configuration org-edit-src-saved-temp-window-config)
633 (setq org-edit-src-saved-temp-window-config nil))))
635 (defun org-edit-src-save ()
636 "Save parent buffer with current state source-code buffer."
637 (interactive)
638 (let ((p (point)) (m (mark)) msg)
639 (save-window-excursion
640 (org-edit-src-exit 'save)
641 (save-buffer)
642 (setq msg (current-message))
643 (if (eq org-src-window-setup 'other-frame)
644 (let ((org-src-window-setup 'current-window))
645 (org-edit-src-code 'save))
646 (org-edit-src-code 'save)))
647 (push-mark m 'nomessage)
648 (goto-char (min p (point-max)))
649 (message (or msg ""))))
651 (defun org-src-mode-configure-edit-buffer ()
652 (when (org-bound-and-true-p org-edit-src-from-org-mode)
653 (org-add-hook 'kill-buffer-hook
654 '(lambda () (delete-overlay org-edit-src-overlay)) nil 'local)
655 (if (org-bound-and-true-p org-edit-src-allow-write-back-p)
656 (progn
657 (setq buffer-offer-save t)
658 (setq buffer-file-name
659 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
660 "[" (buffer-name) "]"))
661 (if (featurep 'xemacs)
662 (progn
663 (make-variable-buffer-local 'write-contents-hooks) ; needed only for 21.4
664 (setq write-contents-hooks '(org-edit-src-save)))
665 (setq write-contents-functions '(org-edit-src-save))))
666 (setq buffer-read-only t))))
668 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
671 (defun org-src-associate-babel-session (info)
672 "Associate edit buffer with comint session."
673 (interactive)
674 (let ((session (cdr (assoc :session (nth 2 info)))))
675 (and session (not (string= session "none"))
676 (org-babel-comint-buffer-livep session)
677 ((lambda (f) (and (fboundp f) (funcall f session)))
678 (intern (format "org-babel-%s-associate-session" (nth 0 info)))))))
680 (defun org-src-babel-configure-edit-buffer ()
681 (when org-src-babel-info
682 (org-src-associate-babel-session org-src-babel-info)))
684 (org-add-hook 'org-src-mode-hook 'org-src-babel-configure-edit-buffer)
685 (defmacro org-src-do-at-code-block (&rest body)
686 "Execute a command from an edit buffer in the Org-mode buffer."
687 `(let ((beg-marker org-edit-src-beg-marker))
688 (if beg-marker
689 (with-current-buffer (marker-buffer beg-marker)
690 (goto-char (marker-position beg-marker))
691 ,@body))))
693 (defun org-src-do-key-sequence-at-code-block (&optional key)
694 "Execute key sequence at code block in the source Org buffer.
695 The command bound to KEY in the Org-babel key map is executed
696 remotely with point temporarily at the start of the code block in
697 the Org buffer.
699 This command is not bound to a key by default, to avoid conflicts
700 with language major mode bindings. To bind it to C-c @ in all
701 language major modes, you could use
703 (add-hook 'org-src-mode-hook
704 (lambda () (define-key org-src-mode-map \"\\C-c@\"
705 'org-src-do-key-sequence-at-code-block)))
707 In that case, for example, C-c @ t issued in code edit buffers
708 would tangle the current Org code block, C-c @ e would execute
709 the block and C-c @ h would display the other available
710 Org-babel commands."
711 (interactive "kOrg-babel key: ")
712 (if (equal key (kbd "C-g")) (keyboard-quit)
713 (org-edit-src-save)
714 (org-src-do-at-code-block
715 (call-interactively
716 (lookup-key org-babel-map key)))))
719 (provide 'org-src)
721 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
722 ;;; org-src.el ends here