org-edit-src: now saves window layout when editing source code
[rgr-org-mode.git] / lisp / org-src.el
blobed6607d068f65450c0cf6cbce189bea132523241
1 ;;; org-src.el --- Source code examples in Org
2 ;;
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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: 6.32trans
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 (eval-when-compile
38 (require 'cl))
40 (declare-function org-do-remove-indentation "org" (&optional n))
41 (declare-function org-get-indentation "org" (&optional line))
42 (declare-function org-switch-to-buffer-other-window "org" (&rest args))
44 (defcustom org-edit-src-region-extra nil
45 "Additional regexps to identify regions for editing with `org-edit-src-code'.
46 For examples see the function `org-edit-src-find-region-and-lang'.
47 The regular expression identifying the begin marker should end with a newline,
48 and the regexp marking the end line should start with a newline, to make sure
49 there are kept outside the narrowed region."
50 :group 'org-edit-structure
51 :type '(repeat
52 (list
53 (regexp :tag "begin regexp")
54 (regexp :tag "end regexp")
55 (choice :tag "language"
56 (string :tag "specify")
57 (integer :tag "from match group")
58 (const :tag "from `lang' element")
59 (const :tag "from `style' element")))))
61 (defcustom org-coderef-label-format "(ref:%s)"
62 "The default coderef format.
63 This format string will be used to search for coderef labels in literal
64 examples (EXAMPLE and SRC blocks). The format can be overwritten in
65 an individual literal example with the -f option, like
67 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
68 ...
69 #+END_SRC
71 If you want to use this for HTML export, make sure that the format does
72 not introduce special font-locking, and avoid the HTML special
73 characters `<', `>', and `&'. The reason for this restriction is that
74 the labels are searched for only after htmlize has done its job."
75 :group 'org-edit-structure ; FIXME this is not in the right group
76 :type 'string)
78 (defcustom org-edit-fixed-width-region-mode 'artist-mode
79 "The mode that should be used to edit fixed-width regions.
80 These are the regions where each line starts with a colon."
81 :group 'org-edit-structure
82 :type '(choice
83 (const artist-mode)
84 (const picture-mode)
85 (const fundamental-mode)
86 (function :tag "Other (specify)")))
88 (defcustom org-src-preserve-indentation nil
89 "If non-nil, leading whitespace characters in source code
90 blocks are preserved on export, and when switching between the
91 org buffer and the language mode edit buffer. If this variable
92 is nil then, after editing with \\[org-edit-src-code], the
93 minimum (across-lines) number of leading whitespace characters
94 are removed from all lines, and the code block is uniformly
95 indented according to the value of `org-edit-src-content-indentation'."
96 :group 'org-edit-structure
97 :type 'boolean)
99 (defcustom org-edit-src-content-indentation 2
100 "Indentation for the content of a source code block.
101 This should be the number of spaces added to the indentation of the #+begin
102 line in order to compute the indentation of the block content after
103 editing it with \\[org-edit-src-code]. Has no effect if
104 `org-src-preserve-indentation' is non-nil."
105 :group 'org-edit-structure
106 :type 'integer)
108 (defcustom org-edit-src-persistent-message t
109 "Non-nil means show persistent exit help message while editing src examples.
110 The message is shown in the header-line, which will be created in the
111 first line of the window showing the editing buffer.
112 When nil, the message will only be shown intermittently in the echo area."
113 :group 'org-edit-structure
114 :type 'boolean)
117 (defcustom org-src-window-setup 'reorganize-frame
118 "How the source code edit buffer should be displayed.
119 Possible values for this option are:
121 current-window Show edit buffer in the current window, keeping all other
122 windows.
123 other-window Use `switch-to-buffer-other-window' to display edit buffer.
124 reorganize-frame Show only two windows on the current frame, the current
125 window and the edit buffer. When exiting the edit buffer,
126 return to one window.
127 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
128 Also, when exiting the edit buffer, kill that frame."
129 :group 'org-edit-structure
130 :type '(choice
131 (const current-window)
132 (const other-frame)
133 (const other-window)
134 (const reorganize-frame)))
136 (defvar org-src-mode-hook nil
137 "Hook run after Org switched a source code snippet to its Emacs mode.
138 This hook will run
140 - when editing a source code snippet with \"C-c '\".
141 - When formatting a source code snippet for export with htmlize.
143 You may want to use this hook for example to turn off `outline-minor-mode'
144 or similar things which you want to have when editing a source code file,
145 but which mess up the display of a snippet in Org exported files.")
147 (defcustom org-src-lang-modes
148 '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
149 ("asymptote" . asy) ("dot" . fundamental))
150 "Alist mapping languages to their major mode.
151 The key is the language name, the value is the string that should
152 be inserted as the name of the major mode. For many languages this is
153 simple, but for language where this is not the case, this variable
154 provides a way to simplify things on the user side.
155 For example, there is no ocaml-mode in Emacs, but the mode to use is
156 `tuareg-mode'."
157 :group 'org-edit-structure
158 :type '(repeat
159 (cons
160 (string "Language name")
161 (symbol "Major mode"))))
163 ;;; Editing source examples
165 (defvar org-src-mode-map (make-sparse-keymap))
166 (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
167 (defvar org-edit-src-force-single-line nil)
168 (defvar org-edit-src-from-org-mode nil)
169 (defvar org-edit-src-picture nil)
170 (defvar org-edit-src-beg-marker nil)
171 (defvar org-edit-src-end-marker nil)
172 (defvar org-edit-src-overlay nil)
173 (defvar org-edit-src-block-indentation nil)
174 (defvar org-edit-src-saved-temp-window-config nil)
176 (defvar org-src-ask-before-returning-to-edit-buffer t
177 "If nil, when org-edit-src code is used on a block that already
178 has an active edit buffer, it will switch to that edit buffer
179 immediately; otherwise it will ask whether you want to return
180 to the existing edit buffer.")
182 (define-minor-mode org-src-mode
183 "Minor mode for language major mode buffers generated by org.
184 This minor mode is turned on in two situations:
185 - when editing a source code snippet with \"C-c '\".
186 - When formatting a source code snippet for export with htmlize.
187 There is a mode hook, and keybindings for `org-edit-src-exit' and
188 `org-edit-src-save'")
190 (defun org-edit-src-code ()
191 "Edit the source code example at point.
192 The example is copied to a separate buffer, and that buffer is switched
193 to the correct language mode. When done, exit with \\[org-edit-src-exit].
194 This will remove the original code in the Org buffer, and replace it with
195 the edited version."
196 (interactive)
197 (setq org-edit-src-saved-temp-window-config (current-window-configuration))
198 (let ((line (org-current-line))
199 (col (current-column))
200 (case-fold-search t)
201 (msg (substitute-command-keys
202 "Edit, then exit with C-c ' (C-c and single quote)"))
203 (info (org-edit-src-find-region-and-lang))
204 (org-mode-p (eq major-mode 'org-mode))
205 (beg (make-marker))
206 (end (make-marker))
207 (preserve-indentation org-src-preserve-indentation)
208 block-nindent total-nindent ovl lang lang-f single lfmt code begline buffer)
209 (if (not info)
211 (setq beg (move-marker beg (nth 0 info))
212 end (move-marker end (nth 1 info))
213 code (buffer-substring-no-properties beg end)
214 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
215 (nth 2 info))
216 lang (if (symbolp lang) (symbol-name lang) lang)
217 single (nth 3 info)
218 lfmt (nth 4 info)
219 block-nindent (nth 5 info)
220 lang-f (intern (concat lang "-mode"))
221 begline (save-excursion (goto-char beg) (org-current-line)))
222 (unless (functionp lang-f)
223 (error "No such language mode: %s" lang-f))
224 (org-goto-line line)
225 (if (and (setq buffer (org-edit-src-find-buffer beg end))
226 (if org-src-ask-before-returning-to-edit-buffer
227 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
228 (org-src-switch-to-buffer buffer 'return)
229 (when buffer
230 (with-current-buffer buffer
231 (if (boundp 'org-edit-src-overlay)
232 (org-delete-overlay org-edit-src-overlay)))
233 (kill-buffer buffer))
234 (setq buffer (generate-new-buffer
235 (org-src-construct-edit-buffer-name (buffer-name) lang)))
236 (setq ovl (org-make-overlay beg end))
237 (org-overlay-put ovl 'edit-buffer buffer)
238 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
239 (org-overlay-put ovl 'face 'secondary-selection)
240 (org-overlay-put ovl
241 'keymap
242 (let ((map (make-sparse-keymap)))
243 (define-key map [mouse-1] 'org-edit-src-continue)
244 map))
245 (org-overlay-put ovl :read-only "Leave me alone")
246 (org-src-switch-to-buffer buffer 'edit)
247 (if (eq single 'macro-definition)
248 (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
249 (insert code)
250 (remove-text-properties (point-min) (point-max)
251 '(display nil invisible nil intangible nil))
252 (unless preserve-indentation
253 (setq total-nindent (or (org-do-remove-indentation) 0)))
254 (let ((org-inhibit-startup t))
255 (funcall lang-f))
256 (set (make-local-variable 'org-edit-src-force-single-line) single)
257 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
258 (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
259 (when lfmt
260 (set (make-local-variable 'org-coderef-label-format) lfmt))
261 (when org-mode-p
262 (goto-char (point-min))
263 (while (re-search-forward "^," nil t)
264 (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
265 (replace-match "")))
266 (org-goto-line (1+ (- line begline)))
267 (org-move-to-column
268 (if preserve-indentation col (max 0 (- col total-nindent))))
269 (org-set-local 'org-edit-src-beg-marker beg)
270 (org-set-local 'org-edit-src-end-marker end)
271 (org-set-local 'org-edit-src-overlay ovl)
272 (org-set-local 'org-edit-src-block-indentation block-nindent)
273 (org-src-mode)
274 (set-buffer-modified-p nil)
275 (and org-edit-src-persistent-message
276 (org-set-local 'header-line-format msg)))
277 (message "%s" msg)
278 t)))
280 (defun org-edit-src-continue (e)
281 (interactive "e")
282 (mouse-set-point e)
283 (let ((buf (get-char-property (point) 'edit-buffer)))
284 (if buf (org-src-switch-to-buffer buf 'continue)
285 (error "Something is wrong here"))))
287 (defun org-src-switch-to-buffer (buffer context)
288 (case org-src-window-setup
289 ('current-window
290 (switch-to-buffer buffer))
291 ('other-window
292 (switch-to-buffer-other-window buffer))
293 ('other-frame
294 (case context
295 ('exit
296 (let ((frame (selected-frame)))
297 (switch-to-buffer-other-frame buffer)
298 (delete-frame frame)))
299 ('save
300 (kill-buffer (current-buffer))
301 (switch-to-buffer buffer))
303 (switch-to-buffer-other-frame buffer))))
304 ('reorganize-frame
305 (if (eq context 'edit) (delete-other-windows))
306 (org-switch-to-buffer-other-window buffer)
307 (if (eq context 'exit) (delete-other-windows)))
309 (message "Invalid value %s for org-src-window-setup"
310 (symbol-name org-src-window-setup))
311 (switch-to-buffer buffer))))
313 (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
314 "Construct the buffer name for a source editing buffer"
315 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
317 (defun org-edit-src-find-buffer (beg end)
318 "Find a source editing buffer that is already editing the region BEG to END."
319 (catch 'exit
320 (mapc
321 (lambda (b)
322 (with-current-buffer b
323 (if (and (string-match "\\`*Org Src " (buffer-name))
324 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
325 (local-variable-p 'org-edit-src-end-marker (current-buffer))
326 (equal beg org-edit-src-beg-marker)
327 (equal end org-edit-src-end-marker))
328 (throw 'exit (current-buffer)))))
329 (buffer-list))
330 nil))
332 (defun org-edit-fixed-width-region ()
333 "Edit the fixed-width ascii drawing at point.
334 This must be a region where each line starts with a colon followed by
335 a space character.
336 An new buffer is created and the fixed-width region is copied into it,
337 and the buffer is switched into `artist-mode' for editing. When done,
338 exit with \\[org-edit-src-exit]. The edited text will then replace
339 the fragment in the Org-mode buffer."
340 (interactive)
341 (let ((line (org-current-line))
342 (col (current-column))
343 (case-fold-search t)
344 (msg (substitute-command-keys
345 "Edit, then exit with C-c ' (C-c and single quote)"))
346 (org-mode-p (eq major-mode 'org-mode))
347 (beg (make-marker))
348 (end (make-marker))
349 (preserve-indentation org-src-preserve-indentation)
350 block-nindent ovl beg1 end1 code begline buffer)
351 (beginning-of-line 1)
352 (if (looking-at "[ \t]*[^:\n \t]")
354 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
355 (setq beg1 (point) end1 beg1)
356 (save-excursion
357 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
358 (setq beg1 (point-at-bol 2))
359 (setq beg1 (point))))
360 (save-excursion
361 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
362 (setq end1 (1- (match-beginning 0)))
363 (setq end1 (point))))
364 (org-goto-line line))
365 (setq beg (move-marker beg beg1)
366 end (move-marker end end1)
367 code (buffer-substring-no-properties beg end)
368 begline (save-excursion (goto-char beg) (org-current-line)))
369 (if (and (setq buffer (org-edit-src-find-buffer beg end))
370 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
371 (switch-to-buffer buffer)
372 (when buffer
373 (with-current-buffer buffer
374 (if (boundp 'org-edit-src-overlay)
375 (org-delete-overlay org-edit-src-overlay)))
376 (kill-buffer buffer))
377 (setq buffer (generate-new-buffer
378 (org-src-construct-edit-buffer-name
379 (buffer-name) "Fixed Width")))
380 (setq ovl (org-make-overlay beg end))
381 (org-overlay-put ovl 'face 'secondary-selection)
382 (org-overlay-put ovl 'edit-buffer buffer)
383 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
384 (org-overlay-put ovl 'face 'secondary-selection)
385 (org-overlay-put ovl
386 'keymap
387 (let ((map (make-sparse-keymap)))
388 (define-key map [mouse-1] 'org-edit-src-continue)
389 map))
390 (org-overlay-put ovl :read-only "Leave me alone")
391 (switch-to-buffer buffer)
392 (insert code)
393 (remove-text-properties (point-min) (point-max)
394 '(display nil invisible nil intangible nil))
395 (setq block-nindent (or (org-do-remove-indentation) 0))
396 (cond
397 ((eq org-edit-fixed-width-region-mode 'artist-mode)
398 (fundamental-mode)
399 (artist-mode 1))
400 (t (funcall org-edit-fixed-width-region-mode)))
401 (set (make-local-variable 'org-edit-src-force-single-line) nil)
402 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
403 (set (make-local-variable 'org-edit-src-picture) t)
404 (goto-char (point-min))
405 (while (re-search-forward "^[ \t]*: ?" nil t)
406 (replace-match ""))
407 (org-goto-line (1+ (- line begline)))
408 (org-move-to-column (max 0 (- col block-nindent 2)))
409 (org-set-local 'org-edit-src-beg-marker beg)
410 (org-set-local 'org-edit-src-end-marker end)
411 (org-set-local 'org-edit-src-overlay ovl)
412 (org-set-local 'org-edit-src-block-indentation block-nindent)
413 (org-set-local 'org-edit-src-content-indentation 0)
414 (org-set-local 'org-src-preserve-indentation nil)
415 (org-src-mode)
416 (set-buffer-modified-p nil)
417 (and org-edit-src-persistent-message
418 (org-set-local 'header-line-format msg)))
419 (message "%s" msg)
420 t)))
422 (defun org-edit-src-find-region-and-lang ()
423 "Find the region and language for a local edit.
424 Return a list with beginning and end of the region, a string representing
425 the language, a switch telling if the content should be in a single line."
426 (let ((re-list
427 (append
428 org-edit-src-region-extra
430 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
431 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
432 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
433 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
434 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
435 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
436 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
437 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
438 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
439 ("^[ \t]*#\\+html:" "\n" "html" single-line)
440 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
441 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
442 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
443 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
444 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
445 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
446 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
447 "\n" "fundamental" macro-definition)
448 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
450 (pos (point))
451 re1 re2 single beg end lang lfmt match-re1 ind entry)
452 (catch 'exit
453 (while (setq entry (pop re-list))
454 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
455 single (nth 3 entry))
456 (save-excursion
457 (if (or (looking-at re1)
458 (re-search-backward re1 nil t))
459 (progn
460 (setq match-re1 (match-string 0))
461 (setq beg (match-end 0)
462 lang (org-edit-src-get-lang lang)
463 lfmt (org-edit-src-get-label-format match-re1)
464 ind (org-edit-src-get-indentation (match-beginning 0)))
465 (if (and (re-search-forward re2 nil t)
466 (>= (match-end 0) pos))
467 (throw 'exit (list beg (match-beginning 0)
468 lang single lfmt ind))))
469 (if (or (looking-at re2)
470 (re-search-forward re2 nil t))
471 (progn
472 (setq end (match-beginning 0))
473 (if (and (re-search-backward re1 nil t)
474 (<= (match-beginning 0) pos))
475 (progn
476 (setq lfmt (org-edit-src-get-label-format
477 (match-string 0))
478 ind (org-edit-src-get-indentation
479 (match-beginning 0)))
480 (throw 'exit
481 (list (match-end 0) end
482 (org-edit-src-get-lang lang)
483 single lfmt ind))))))))))))
485 (defun org-edit-src-get-lang (lang)
486 "Extract the src language."
487 (let ((m (match-string 0)))
488 (cond
489 ((stringp lang) lang)
490 ((integerp lang) (match-string lang))
491 ((and (eq lang 'lang)
492 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
493 (match-string 1 m))
494 ((and (eq lang 'style)
495 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
496 (match-string 1 m))
497 (t "fundamental"))))
499 (defun org-edit-src-get-label-format (s)
500 "Extract the label format."
501 (save-match-data
502 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
503 (match-string 1 s))))
505 (defun org-edit-src-get-indentation (pos)
506 "Count leading whitespace characters on line"
507 (save-match-data
508 (goto-char pos)
509 (org-get-indentation)))
511 (defun org-edit-src-exit (&optional context)
512 "Exit special edit and protect problematic lines."
513 (interactive)
514 (unless org-edit-src-from-org-mode
515 (error "This is not a sub-editing buffer, something is wrong..."))
516 (let* ((beg org-edit-src-beg-marker)
517 (end org-edit-src-end-marker)
518 (ovl org-edit-src-overlay)
519 (buffer (current-buffer))
520 (single (org-bound-and-true-p org-edit-src-force-single-line))
521 (macro (eq single 'macro-definition))
522 (total-nindent (+ (or org-edit-src-block-indentation 0)
523 org-edit-src-content-indentation))
524 (preserve-indentation org-src-preserve-indentation)
525 (delta 0) code line col indent)
526 (untabify (point-min) (point-max))
527 (save-excursion
528 (goto-char (point-min))
529 (if (looking-at "[ \t\n]*\n") (replace-match ""))
530 (unless macro
531 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))))
532 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
534 (org-current-line))
535 col (current-column))
536 (when single
537 (goto-char (point-min))
538 (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
539 (goto-char (point-min))
540 (let ((cnt 0))
541 (while (re-search-forward "\n" nil t)
542 (setq cnt (1+ cnt))
543 (replace-match (if macro "\\n" " ") t t))
544 (when (and macro (> cnt 0))
545 (goto-char (point-max)) (insert "\\n")))
546 (goto-char (point-min))
547 (if (looking-at "\\s-*") (replace-match " ")))
548 (when (org-bound-and-true-p org-edit-src-from-org-mode)
549 (goto-char (point-min))
550 (while (re-search-forward
551 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
552 (if (eq (org-current-line) line) (setq delta (1+ delta)))
553 (replace-match ",\\1")))
554 (when (org-bound-and-true-p org-edit-src-picture)
555 (setq preserve-indentation nil)
556 (untabify (point-min) (point-max))
557 (goto-char (point-min))
558 (while (re-search-forward "^" nil t)
559 (replace-match ": ")))
560 (unless (or single preserve-indentation (= total-nindent 0))
561 (setq indent (make-string total-nindent ?\ ))
562 (goto-char (point-min))
563 (while (re-search-forward "^" nil t)
564 (replace-match indent)))
565 (if (org-bound-and-true-p org-edit-src-picture)
566 (setq total-nindent (+ total-nindent 2)))
567 (setq code (buffer-string))
568 (set-buffer-modified-p nil)
569 (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
570 (kill-buffer buffer)
571 (goto-char beg)
572 (delete-region beg end)
573 (insert code)
574 (goto-char beg)
575 (if single (just-one-space))
576 (org-goto-line (1- (+ (org-current-line) line)))
577 (org-move-to-column (if preserve-indentation col (+ col total-nindent delta)))
578 (move-marker beg nil)
579 (move-marker end nil))
580 (when org-edit-src-saved-temp-window-config
581 (set-window-configuration org-edit-src-saved-temp-window-config)
582 (setq org-edit-src-saved-temp-window-config nil)))
584 (defun org-edit-src-save ()
585 "Save parent buffer with current state source-code buffer."
586 (interactive)
587 (let ((p (point)) (m (mark)) msg)
588 (save-window-excursion
589 (org-edit-src-exit 'save)
590 (save-buffer)
591 (setq msg (current-message))
592 (if (eq org-src-window-setup 'other-frame)
593 (let ((org-src-window-setup 'current-window))
594 (org-edit-src-code))
595 (org-edit-src-code)))
596 (push-mark m 'nomessage)
597 (goto-char (min p (point-max)))
598 (message (or msg ""))))
600 (defun org-src-mode-configure-edit-buffer ()
601 (when org-edit-src-from-org-mode
602 (setq buffer-offer-save t)
603 (setq buffer-file-name
604 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
605 "[" (buffer-name) "]"))
606 (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
607 '(org-edit-src-save))
608 (org-add-hook 'kill-buffer-hook
609 '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
611 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
613 (provide 'org-src)
615 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
616 ;;; org-src.el ends here