Let blocking be turned-off on a per-file basis using local variables
[rgr-org-mode.git] / lisp / org-src.el
blob1d155cc6fa3f1662c3e3b28d794381a55962c05e
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.34trans
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 (&optional context)
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. Optional argument CONTEXT is used by
196 \\[org-edit-src-save] when calling this function."
197 (interactive)
198 (unless (eq context 'save)
199 (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
200 (let ((line (org-current-line))
201 (col (current-column))
202 (case-fold-search t)
203 (msg (substitute-command-keys
204 "Edit, then exit with C-c ' (C-c and single quote)"))
205 (info (org-edit-src-find-region-and-lang))
206 (org-mode-p (eq major-mode 'org-mode))
207 (beg (make-marker))
208 (end (make-marker))
209 (preserve-indentation org-src-preserve-indentation)
210 block-nindent total-nindent ovl lang lang-f single lfmt code begline buffer)
211 (if (not info)
213 (setq beg (move-marker beg (nth 0 info))
214 end (move-marker end (nth 1 info))
215 code (buffer-substring-no-properties beg end)
216 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
217 (nth 2 info))
218 lang (if (symbolp lang) (symbol-name lang) lang)
219 single (nth 3 info)
220 lfmt (nth 4 info)
221 block-nindent (nth 5 info)
222 lang-f (intern (concat lang "-mode"))
223 begline (save-excursion (goto-char beg) (org-current-line)))
224 (unless (functionp lang-f)
225 (error "No such language mode: %s" lang-f))
226 (org-goto-line line)
227 (if (and (setq buffer (org-edit-src-find-buffer beg end))
228 (if org-src-ask-before-returning-to-edit-buffer
229 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
230 (org-src-switch-to-buffer buffer 'return)
231 (when buffer
232 (with-current-buffer buffer
233 (if (boundp 'org-edit-src-overlay)
234 (org-delete-overlay org-edit-src-overlay)))
235 (kill-buffer buffer))
236 (setq buffer (generate-new-buffer
237 (org-src-construct-edit-buffer-name (buffer-name) lang)))
238 (setq ovl (org-make-overlay beg end))
239 (org-overlay-put ovl 'edit-buffer buffer)
240 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
241 (org-overlay-put ovl 'face 'secondary-selection)
242 (org-overlay-put ovl
243 'keymap
244 (let ((map (make-sparse-keymap)))
245 (define-key map [mouse-1] 'org-edit-src-continue)
246 map))
247 (org-overlay-put ovl :read-only "Leave me alone")
248 (org-src-switch-to-buffer buffer 'edit)
249 (if (eq single 'macro-definition)
250 (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
251 (insert code)
252 (remove-text-properties (point-min) (point-max)
253 '(display nil invisible nil intangible nil))
254 (unless preserve-indentation
255 (setq total-nindent (or (org-do-remove-indentation) 0)))
256 (let ((org-inhibit-startup t))
257 (funcall lang-f))
258 (set (make-local-variable 'org-edit-src-force-single-line) single)
259 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
260 (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
261 (when lfmt
262 (set (make-local-variable 'org-coderef-label-format) lfmt))
263 (when org-mode-p
264 (goto-char (point-min))
265 (while (re-search-forward "^," nil t)
266 (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
267 (replace-match "")))
268 (org-goto-line (1+ (- line begline)))
269 (org-move-to-column
270 (if preserve-indentation col (max 0 (- col total-nindent))))
271 (org-set-local 'org-edit-src-beg-marker beg)
272 (org-set-local 'org-edit-src-end-marker end)
273 (org-set-local 'org-edit-src-overlay ovl)
274 (org-set-local 'org-edit-src-block-indentation block-nindent)
275 (org-src-mode)
276 (set-buffer-modified-p nil)
277 (and org-edit-src-persistent-message
278 (org-set-local 'header-line-format msg)))
279 (message "%s" msg)
280 t)))
282 (defun org-edit-src-continue (e)
283 (interactive "e")
284 (mouse-set-point e)
285 (let ((buf (get-char-property (point) 'edit-buffer)))
286 (if buf (org-src-switch-to-buffer buf 'continue)
287 (error "Something is wrong here"))))
289 (defun org-src-switch-to-buffer (buffer context)
290 (case org-src-window-setup
291 ('current-window
292 (switch-to-buffer buffer))
293 ('other-window
294 (switch-to-buffer-other-window buffer))
295 ('other-frame
296 (case context
297 ('exit
298 (let ((frame (selected-frame)))
299 (switch-to-buffer-other-frame buffer)
300 (delete-frame frame)))
301 ('save
302 (kill-buffer (current-buffer))
303 (switch-to-buffer buffer))
305 (switch-to-buffer-other-frame buffer))))
306 ('reorganize-frame
307 (if (eq context 'edit) (delete-other-windows))
308 (org-switch-to-buffer-other-window buffer)
309 (if (eq context 'exit) (delete-other-windows)))
311 (message "Invalid value %s for org-src-window-setup"
312 (symbol-name org-src-window-setup))
313 (switch-to-buffer buffer))))
315 (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
316 "Construct the buffer name for a source editing buffer"
317 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
319 (defun org-edit-src-find-buffer (beg end)
320 "Find a source editing buffer that is already editing the region BEG to END."
321 (catch 'exit
322 (mapc
323 (lambda (b)
324 (with-current-buffer b
325 (if (and (string-match "\\`*Org Src " (buffer-name))
326 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
327 (local-variable-p 'org-edit-src-end-marker (current-buffer))
328 (equal beg org-edit-src-beg-marker)
329 (equal end org-edit-src-end-marker))
330 (throw 'exit (current-buffer)))))
331 (buffer-list))
332 nil))
334 (defun org-edit-fixed-width-region ()
335 "Edit the fixed-width ascii drawing at point.
336 This must be a region where each line starts with a colon followed by
337 a space character.
338 An new buffer is created and the fixed-width region is copied into it,
339 and the buffer is switched into `artist-mode' for editing. When done,
340 exit with \\[org-edit-src-exit]. The edited text will then replace
341 the fragment in the Org-mode buffer."
342 (interactive)
343 (let ((line (org-current-line))
344 (col (current-column))
345 (case-fold-search t)
346 (msg (substitute-command-keys
347 "Edit, then exit with C-c ' (C-c and single quote)"))
348 (org-mode-p (eq major-mode 'org-mode))
349 (beg (make-marker))
350 (end (make-marker))
351 (preserve-indentation org-src-preserve-indentation)
352 block-nindent ovl beg1 end1 code begline buffer)
353 (beginning-of-line 1)
354 (if (looking-at "[ \t]*[^:\n \t]")
356 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
357 (setq beg1 (point) end1 beg1)
358 (save-excursion
359 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
360 (setq beg1 (point-at-bol 2))
361 (setq beg1 (point))))
362 (save-excursion
363 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
364 (setq end1 (1- (match-beginning 0)))
365 (setq end1 (point))))
366 (org-goto-line line))
367 (setq beg (move-marker beg beg1)
368 end (move-marker end end1)
369 code (buffer-substring-no-properties beg end)
370 begline (save-excursion (goto-char beg) (org-current-line)))
371 (if (and (setq buffer (org-edit-src-find-buffer beg end))
372 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
373 (switch-to-buffer buffer)
374 (when buffer
375 (with-current-buffer buffer
376 (if (boundp 'org-edit-src-overlay)
377 (org-delete-overlay org-edit-src-overlay)))
378 (kill-buffer buffer))
379 (setq buffer (generate-new-buffer
380 (org-src-construct-edit-buffer-name
381 (buffer-name) "Fixed Width")))
382 (setq ovl (org-make-overlay beg end))
383 (org-overlay-put ovl 'face 'secondary-selection)
384 (org-overlay-put ovl 'edit-buffer buffer)
385 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
386 (org-overlay-put ovl 'face 'secondary-selection)
387 (org-overlay-put ovl
388 'keymap
389 (let ((map (make-sparse-keymap)))
390 (define-key map [mouse-1] 'org-edit-src-continue)
391 map))
392 (org-overlay-put ovl :read-only "Leave me alone")
393 (switch-to-buffer buffer)
394 (insert code)
395 (remove-text-properties (point-min) (point-max)
396 '(display nil invisible nil intangible nil))
397 (setq block-nindent (or (org-do-remove-indentation) 0))
398 (cond
399 ((eq org-edit-fixed-width-region-mode 'artist-mode)
400 (fundamental-mode)
401 (artist-mode 1))
402 (t (funcall org-edit-fixed-width-region-mode)))
403 (set (make-local-variable 'org-edit-src-force-single-line) nil)
404 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
405 (set (make-local-variable 'org-edit-src-picture) t)
406 (goto-char (point-min))
407 (while (re-search-forward "^[ \t]*: ?" nil t)
408 (replace-match ""))
409 (org-goto-line (1+ (- line begline)))
410 (org-move-to-column (max 0 (- col block-nindent 2)))
411 (org-set-local 'org-edit-src-beg-marker beg)
412 (org-set-local 'org-edit-src-end-marker end)
413 (org-set-local 'org-edit-src-overlay ovl)
414 (org-set-local 'org-edit-src-block-indentation block-nindent)
415 (org-set-local 'org-edit-src-content-indentation 0)
416 (org-set-local 'org-src-preserve-indentation nil)
417 (org-src-mode)
418 (set-buffer-modified-p nil)
419 (and org-edit-src-persistent-message
420 (org-set-local 'header-line-format msg)))
421 (message "%s" msg)
422 t)))
424 (defun org-edit-src-find-region-and-lang ()
425 "Find the region and language for a local edit.
426 Return a list with beginning and end of the region, a string representing
427 the language, a switch telling if the content should be in a single line."
428 (let ((re-list
429 (append
430 org-edit-src-region-extra
432 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
433 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
434 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
435 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
436 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
437 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
438 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
439 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
440 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
441 ("^[ \t]*#\\+html:" "\n" "html" single-line)
442 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
443 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
444 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
445 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
446 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
447 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
448 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
449 "\n" "fundamental" macro-definition)
450 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
452 (pos (point))
453 re1 re2 single beg end lang lfmt match-re1 ind entry)
454 (catch 'exit
455 (while (setq entry (pop re-list))
456 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
457 single (nth 3 entry))
458 (save-excursion
459 (if (or (looking-at re1)
460 (re-search-backward re1 nil t))
461 (progn
462 (setq match-re1 (match-string 0))
463 (setq beg (match-end 0)
464 lang (org-edit-src-get-lang lang)
465 lfmt (org-edit-src-get-label-format match-re1)
466 ind (org-edit-src-get-indentation (match-beginning 0)))
467 (if (and (re-search-forward re2 nil t)
468 (>= (match-end 0) pos))
469 (throw 'exit (list beg (match-beginning 0)
470 lang single lfmt ind))))
471 (if (or (looking-at re2)
472 (re-search-forward re2 nil t))
473 (progn
474 (setq end (match-beginning 0))
475 (if (and (re-search-backward re1 nil t)
476 (<= (match-beginning 0) pos))
477 (progn
478 (setq lfmt (org-edit-src-get-label-format
479 (match-string 0))
480 ind (org-edit-src-get-indentation
481 (match-beginning 0)))
482 (throw 'exit
483 (list (match-end 0) end
484 (org-edit-src-get-lang lang)
485 single lfmt ind))))))))))))
487 (defun org-edit-src-get-lang (lang)
488 "Extract the src language."
489 (let ((m (match-string 0)))
490 (cond
491 ((stringp lang) lang)
492 ((integerp lang) (match-string lang))
493 ((and (eq lang 'lang)
494 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
495 (match-string 1 m))
496 ((and (eq lang 'style)
497 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
498 (match-string 1 m))
499 (t "fundamental"))))
501 (defun org-edit-src-get-label-format (s)
502 "Extract the label format."
503 (save-match-data
504 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
505 (match-string 1 s))))
507 (defun org-edit-src-get-indentation (pos)
508 "Count leading whitespace characters on line"
509 (save-match-data
510 (goto-char pos)
511 (org-get-indentation)))
513 (defun org-edit-src-exit (&optional context)
514 "Exit special edit and protect problematic lines."
515 (interactive)
516 (unless org-edit-src-from-org-mode
517 (error "This is not a sub-editing buffer, something is wrong..."))
518 (let* ((beg org-edit-src-beg-marker)
519 (end org-edit-src-end-marker)
520 (ovl org-edit-src-overlay)
521 (buffer (current-buffer))
522 (single (org-bound-and-true-p org-edit-src-force-single-line))
523 (macro (eq single 'macro-definition))
524 (total-nindent (+ (or org-edit-src-block-indentation 0)
525 org-edit-src-content-indentation))
526 (preserve-indentation org-src-preserve-indentation)
527 (delta 0) code line col indent)
528 (unless preserve-indentation (untabify (point-min) (point-max)))
529 (save-excursion
530 (goto-char (point-min))
531 (if (looking-at "[ \t\n]*\n") (replace-match ""))
532 (unless macro
533 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))))
534 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
536 (org-current-line))
537 col (current-column))
538 (when single
539 (goto-char (point-min))
540 (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
541 (goto-char (point-min))
542 (let ((cnt 0))
543 (while (re-search-forward "\n" nil t)
544 (setq cnt (1+ cnt))
545 (replace-match (if macro "\\n" " ") t t))
546 (when (and macro (> cnt 0))
547 (goto-char (point-max)) (insert "\\n")))
548 (goto-char (point-min))
549 (if (looking-at "\\s-*") (replace-match " ")))
550 (when (org-bound-and-true-p org-edit-src-from-org-mode)
551 (goto-char (point-min))
552 (while (re-search-forward
553 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
554 (if (eq (org-current-line) line) (setq delta (1+ delta)))
555 (replace-match ",\\1")))
556 (when (org-bound-and-true-p org-edit-src-picture)
557 (setq preserve-indentation nil)
558 (untabify (point-min) (point-max))
559 (goto-char (point-min))
560 (while (re-search-forward "^" nil t)
561 (replace-match ": ")))
562 (unless (or single preserve-indentation (= total-nindent 0))
563 (setq indent (make-string total-nindent ?\ ))
564 (goto-char (point-min))
565 (while (re-search-forward "^" nil t)
566 (replace-match indent)))
567 (if (org-bound-and-true-p org-edit-src-picture)
568 (setq total-nindent (+ total-nindent 2)))
569 (setq code (buffer-string))
570 (set-buffer-modified-p nil)
571 (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
572 (kill-buffer buffer)
573 (goto-char beg)
574 (delete-region beg end)
575 (insert code)
576 (goto-char beg)
577 (if single (just-one-space))
578 (org-goto-line (1- (+ (org-current-line) line)))
579 (org-move-to-column (if preserve-indentation col (+ col total-nindent delta)))
580 (move-marker beg nil)
581 (move-marker end nil))
582 (unless (eq context 'save)
583 (when org-edit-src-saved-temp-window-config
584 (set-window-configuration org-edit-src-saved-temp-window-config)
585 (setq org-edit-src-saved-temp-window-config nil))))
587 (defun org-edit-src-save ()
588 "Save parent buffer with current state source-code buffer."
589 (interactive)
590 (let ((p (point)) (m (mark)) msg)
591 (save-window-excursion
592 (org-edit-src-exit 'save)
593 (save-buffer)
594 (setq msg (current-message))
595 (if (eq org-src-window-setup 'other-frame)
596 (let ((org-src-window-setup 'current-window))
597 (org-edit-src-code 'save))
598 (org-edit-src-code 'save)))
599 (push-mark m 'nomessage)
600 (goto-char (min p (point-max)))
601 (message (or msg ""))))
603 (defun org-src-mode-configure-edit-buffer ()
604 (when org-edit-src-from-org-mode
605 (setq buffer-offer-save t)
606 (setq buffer-file-name
607 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
608 "[" (buffer-name) "]"))
609 (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
610 '(org-edit-src-save))
611 (org-add-hook 'kill-buffer-hook
612 '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
614 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
616 (provide 'org-src)
618 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
619 ;;; org-src.el ends here