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