Release 6.36c
[org-mode.git] / lisp / org-src.el
blob592364d49f850acfc433704fdb18c503e10ba0cb
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.36c
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, leading whitespace characters in source code
91 blocks are preserved on export, and when switching between the
92 org buffer and the language mode edit buffer. If this variable
93 is nil then, after editing with \\[org-edit-src-code], the
94 minimum (across-lines) number of leading whitespace characters
95 are removed from all lines, and the code block is uniformly
96 indented according to the value of `org-edit-src-content-indentation'."
97 :group 'org-edit-structure
98 :type 'boolean)
100 (defcustom org-edit-src-content-indentation 2
101 "Indentation for the content of a source code block.
102 This should be the number of spaces added to the indentation of the #+begin
103 line in order to compute the indentation of the block content after
104 editing it with \\[org-edit-src-code]. Has no effect if
105 `org-src-preserve-indentation' is non-nil."
106 :group 'org-edit-structure
107 :type 'integer)
109 (defcustom org-edit-src-persistent-message t
110 "Non-nil means show persistent exit help message while editing src examples.
111 The message is shown in the header-line, which will be created in the
112 first line of the window showing the editing buffer.
113 When nil, the message will only be shown intermittently in the echo area."
114 :group 'org-edit-structure
115 :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-allow-write-back-p nil)
170 (defvar org-edit-src-picture nil)
171 (defvar org-edit-src-beg-marker nil)
172 (defvar org-edit-src-end-marker nil)
173 (defvar org-edit-src-overlay nil)
174 (defvar org-edit-src-block-indentation nil)
175 (defvar org-edit-src-saved-temp-window-config nil)
177 (defvar org-src-ask-before-returning-to-edit-buffer t
178 "If nil, when org-edit-src code is used on a block that already
179 has an active edit buffer, it will switch to that edit buffer
180 immediately; otherwise it will ask whether you want to return
181 to the existing edit buffer.")
183 (define-minor-mode org-src-mode
184 "Minor mode for language major mode buffers generated by org.
185 This minor mode is turned on in two situations:
186 - when editing a source code snippet with \"C-c '\".
187 - When formatting a source code snippet for export with htmlize.
188 There is a mode hook, and keybindings for `org-edit-src-exit' and
189 `org-edit-src-save'")
191 (defun org-edit-src-code (&optional context code edit-buffer-name)
192 "Edit the source code example at point.
193 The example is copied to a separate buffer, and that buffer is switched
194 to the correct language mode. When done, exit with \\[org-edit-src-exit].
195 This will remove the original code in the Org buffer, and replace it with
196 the edited version. Optional argument CONTEXT is used by
197 \\[org-edit-src-save] when calling this function."
198 (interactive)
199 (unless (eq context 'save)
200 (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
201 (let ((line (org-current-line))
202 (col (current-column))
203 (case-fold-search t)
204 (info (org-edit-src-find-region-and-lang))
205 (org-mode-p (eq major-mode 'org-mode))
206 (beg (make-marker))
207 (end (make-marker))
208 (preserve-indentation org-src-preserve-indentation)
209 (allow-write-back-p (null code))
210 block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg)
211 (if (not info)
213 (setq beg (move-marker beg (nth 0 info))
214 end (move-marker end (nth 1 info))
215 msg (if allow-write-back-p
216 (substitute-command-keys
217 "Edit, then exit with C-c ' (C-c and single quote)")
218 "Exit with C-c ' (C-c and single quote)")
219 code (or code (buffer-substring-no-properties beg end))
220 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
221 (nth 2 info))
222 lang (if (symbolp lang) (symbol-name lang) lang)
223 single (nth 3 info)
224 lfmt (nth 4 info)
225 block-nindent (nth 5 info)
226 lang-f (intern (concat lang "-mode"))
227 begline (save-excursion (goto-char beg) (org-current-line)))
228 (if (equal lang-f 'table.el-mode)
229 (setq lang-f (lambda ()
230 (text-mode)
231 (if (org-bound-and-true-p flyspell-mode)
232 (flyspell-mode -1))
233 (table-recognize)
234 (org-set-local 'org-edit-src-content-indentation 0))))
235 (unless (functionp lang-f)
236 (error "No such language mode: %s" lang-f))
237 (org-goto-line line)
238 (if (and (setq buffer (org-edit-src-find-buffer beg end))
239 (if org-src-ask-before-returning-to-edit-buffer
240 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
241 (org-src-switch-to-buffer buffer 'return)
242 (when buffer
243 (with-current-buffer buffer
244 (if (boundp 'org-edit-src-overlay)
245 (delete-overlay org-edit-src-overlay)))
246 (kill-buffer buffer))
247 (setq buffer (generate-new-buffer
248 (or edit-buffer-name
249 (org-src-construct-edit-buffer-name (buffer-name) lang))))
250 (setq ovl (make-overlay beg end))
251 (overlay-put ovl 'edit-buffer buffer)
252 (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
253 (overlay-put ovl 'face 'secondary-selection)
254 (overlay-put ovl
255 'keymap
256 (let ((map (make-sparse-keymap)))
257 (define-key map [mouse-1] 'org-edit-src-continue)
258 map))
259 (overlay-put ovl :read-only "Leave me alone")
260 (org-src-switch-to-buffer buffer 'edit)
261 (if (eq single 'macro-definition)
262 (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
263 (insert code)
264 (remove-text-properties (point-min) (point-max)
265 '(display nil invisible nil intangible nil))
266 (unless preserve-indentation
267 (setq total-nindent (or (org-do-remove-indentation) 0)))
268 (let ((org-inhibit-startup t))
269 (funcall lang-f))
270 (set (make-local-variable 'org-edit-src-force-single-line) single)
271 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
272 (set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p)
273 (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
274 (when lfmt
275 (set (make-local-variable 'org-coderef-label-format) lfmt))
276 (when org-mode-p
277 (goto-char (point-min))
278 (while (re-search-forward "^," nil t)
279 (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
280 (replace-match "")))
281 (org-goto-line (1+ (- line begline)))
282 (org-move-to-column
283 (if preserve-indentation col (max 0 (- col total-nindent))))
284 (org-set-local 'org-edit-src-beg-marker beg)
285 (org-set-local 'org-edit-src-end-marker end)
286 (org-set-local 'org-edit-src-overlay ovl)
287 (org-set-local 'org-edit-src-block-indentation block-nindent)
288 (org-src-mode)
289 (set-buffer-modified-p nil)
290 (and org-edit-src-persistent-message
291 (org-set-local 'header-line-format msg)))
292 (message "%s" msg)
293 t)))
295 (defun org-edit-src-continue (e)
296 (interactive "e")
297 (mouse-set-point e)
298 (let ((buf (get-char-property (point) 'edit-buffer)))
299 (if buf (org-src-switch-to-buffer buf 'continue)
300 (error "Something is wrong here"))))
302 (defun org-src-switch-to-buffer (buffer context)
303 (case org-src-window-setup
304 ('current-window
305 (switch-to-buffer buffer))
306 ('other-window
307 (switch-to-buffer-other-window buffer))
308 ('other-frame
309 (case context
310 ('exit
311 (let ((frame (selected-frame)))
312 (switch-to-buffer-other-frame buffer)
313 (delete-frame frame)))
314 ('save
315 (kill-buffer (current-buffer))
316 (switch-to-buffer buffer))
318 (switch-to-buffer-other-frame buffer))))
319 ('reorganize-frame
320 (if (eq context 'edit) (delete-other-windows))
321 (org-switch-to-buffer-other-window buffer)
322 (if (eq context 'exit) (delete-other-windows)))
324 (message "Invalid value %s for org-src-window-setup"
325 (symbol-name org-src-window-setup))
326 (switch-to-buffer buffer))))
328 (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
329 "Construct the buffer name for a source editing buffer"
330 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
332 (defun org-edit-src-find-buffer (beg end)
333 "Find a source editing buffer that is already editing the region BEG to END."
334 (catch 'exit
335 (mapc
336 (lambda (b)
337 (with-current-buffer b
338 (if (and (string-match "\\`*Org Src " (buffer-name))
339 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
340 (local-variable-p 'org-edit-src-end-marker (current-buffer))
341 (equal beg org-edit-src-beg-marker)
342 (equal end org-edit-src-end-marker))
343 (throw 'exit (current-buffer)))))
344 (buffer-list))
345 nil))
347 (defun org-edit-fixed-width-region ()
348 "Edit the fixed-width ascii drawing at point.
349 This must be a region where each line starts with a colon followed by
350 a space character.
351 An new buffer is created and the fixed-width region is copied into it,
352 and the buffer is switched into `artist-mode' for editing. When done,
353 exit with \\[org-edit-src-exit]. The edited text will then replace
354 the fragment in the Org-mode buffer."
355 (interactive)
356 (let ((line (org-current-line))
357 (col (current-column))
358 (case-fold-search t)
359 (msg (substitute-command-keys
360 "Edit, then exit with C-c ' (C-c and single quote)"))
361 (org-mode-p (eq major-mode 'org-mode))
362 (beg (make-marker))
363 (end (make-marker))
364 (preserve-indentation org-src-preserve-indentation)
365 block-nindent ovl beg1 end1 code begline buffer)
366 (beginning-of-line 1)
367 (if (looking-at "[ \t]*[^:\n \t]")
369 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
370 (setq beg1 (point) end1 beg1)
371 (save-excursion
372 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
373 (setq beg1 (point-at-bol 2))
374 (setq beg1 (point))))
375 (save-excursion
376 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
377 (setq end1 (1- (match-beginning 0)))
378 (setq end1 (point))))
379 (org-goto-line line))
380 (setq beg (move-marker beg beg1)
381 end (move-marker end end1)
382 code (buffer-substring-no-properties beg end)
383 begline (save-excursion (goto-char beg) (org-current-line)))
384 (if (and (setq buffer (org-edit-src-find-buffer beg end))
385 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
386 (switch-to-buffer buffer)
387 (when buffer
388 (with-current-buffer buffer
389 (if (boundp 'org-edit-src-overlay)
390 (delete-overlay org-edit-src-overlay)))
391 (kill-buffer buffer))
392 (setq buffer (generate-new-buffer
393 (org-src-construct-edit-buffer-name
394 (buffer-name) "Fixed Width")))
395 (setq ovl (make-overlay beg end))
396 (overlay-put ovl 'face 'secondary-selection)
397 (overlay-put ovl 'edit-buffer buffer)
398 (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
399 (overlay-put ovl 'face 'secondary-selection)
400 (overlay-put ovl
401 'keymap
402 (let ((map (make-sparse-keymap)))
403 (define-key map [mouse-1] 'org-edit-src-continue)
404 map))
405 (overlay-put ovl :read-only "Leave me alone")
406 (switch-to-buffer buffer)
407 (insert code)
408 (remove-text-properties (point-min) (point-max)
409 '(display nil invisible nil intangible nil))
410 (setq block-nindent (or (org-do-remove-indentation) 0))
411 (cond
412 ((eq org-edit-fixed-width-region-mode 'artist-mode)
413 (fundamental-mode)
414 (artist-mode 1))
415 (t (funcall org-edit-fixed-width-region-mode)))
416 (set (make-local-variable 'org-edit-src-force-single-line) nil)
417 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
418 (set (make-local-variable 'org-edit-src-picture) t)
419 (goto-char (point-min))
420 (while (re-search-forward "^[ \t]*: ?" nil t)
421 (replace-match ""))
422 (org-goto-line (1+ (- line begline)))
423 (org-move-to-column (max 0 (- col block-nindent 2)))
424 (org-set-local 'org-edit-src-beg-marker beg)
425 (org-set-local 'org-edit-src-end-marker end)
426 (org-set-local 'org-edit-src-overlay ovl)
427 (org-set-local 'org-edit-src-block-indentation block-nindent)
428 (org-set-local 'org-edit-src-content-indentation 0)
429 (org-set-local 'org-src-preserve-indentation nil)
430 (org-src-mode)
431 (set-buffer-modified-p nil)
432 (and org-edit-src-persistent-message
433 (org-set-local 'header-line-format msg)))
434 (message "%s" msg)
435 t)))
437 (defun org-edit-src-find-region-and-lang ()
438 "Find the region and language for a local edit.
439 Return a list with beginning and end of the region, a string representing
440 the language, a switch telling if the content should be in a single line."
441 (let ((re-list
442 (append
443 org-edit-src-region-extra
445 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
446 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
447 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
448 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
449 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
450 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
451 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
452 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
453 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
454 ("^[ \t]*#\\+html:" "\n" "html" single-line)
455 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
456 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
457 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
458 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
459 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
460 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
461 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
462 "\n" "fundamental" macro-definition)
463 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
465 (pos (point))
466 re1 re2 single beg end lang lfmt match-re1 ind entry)
467 (catch 'exit
468 (while (setq entry (pop re-list))
469 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
470 single (nth 3 entry))
471 (save-excursion
472 (if (or (looking-at re1)
473 (re-search-backward re1 nil t))
474 (progn
475 (setq match-re1 (match-string 0))
476 (setq beg (match-end 0)
477 lang (org-edit-src-get-lang lang)
478 lfmt (org-edit-src-get-label-format match-re1)
479 ind (org-edit-src-get-indentation (match-beginning 0)))
480 (if (and (re-search-forward re2 nil t)
481 (>= (match-end 0) pos))
482 (throw 'exit (list beg (match-beginning 0)
483 lang single lfmt ind))))
484 (if (or (looking-at re2)
485 (re-search-forward re2 nil t))
486 (progn
487 (setq end (match-beginning 0))
488 (if (and (re-search-backward re1 nil t)
489 (<= (match-beginning 0) pos))
490 (progn
491 (setq lfmt (org-edit-src-get-label-format
492 (match-string 0))
493 ind (org-edit-src-get-indentation
494 (match-beginning 0)))
495 (throw 'exit
496 (list (match-end 0) end
497 (org-edit-src-get-lang lang)
498 single lfmt ind)))))))))
499 (when (org-at-table.el-p)
500 (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
501 (setq beg (1+ (point-at-eol)))
502 (goto-char beg)
503 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
504 (progn (goto-char (point-max)) (newline)))
505 (setq end (point-at-bol))
506 (setq ind (org-edit-src-get-indentation beg))
507 (throw 'exit (list beg end 'table.el nil nil ind))))))
509 (defun org-edit-src-get-lang (lang)
510 "Extract the src language."
511 (let ((m (match-string 0)))
512 (cond
513 ((stringp lang) lang)
514 ((integerp lang) (match-string lang))
515 ((and (eq lang 'lang)
516 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
517 (match-string 1 m))
518 ((and (eq lang 'style)
519 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
520 (match-string 1 m))
521 (t "fundamental"))))
523 (defun org-edit-src-get-label-format (s)
524 "Extract the label format."
525 (save-match-data
526 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
527 (match-string 1 s))))
529 (defun org-edit-src-get-indentation (pos)
530 "Count leading whitespace characters on line"
531 (save-match-data
532 (goto-char pos)
533 (org-get-indentation)))
535 (defun org-edit-src-exit (&optional context)
536 "Exit special edit and protect problematic lines."
537 (interactive)
538 (unless (org-bound-and-true-p org-edit-src-from-org-mode)
539 (error "This is not a sub-editing buffer, something is wrong..."))
540 (widen)
541 (let* ((beg org-edit-src-beg-marker)
542 (end org-edit-src-end-marker)
543 (ovl org-edit-src-overlay)
544 (buffer (current-buffer))
545 (single (org-bound-and-true-p org-edit-src-force-single-line))
546 (macro (eq single 'macro-definition))
547 (total-nindent (+ (or org-edit-src-block-indentation 0)
548 org-edit-src-content-indentation))
549 (preserve-indentation org-src-preserve-indentation)
550 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p))
551 (delta 0) code line col indent)
552 (when allow-write-back-p
553 (unless preserve-indentation (untabify (point-min) (point-max)))
554 (save-excursion
555 (goto-char (point-min))
556 (if (looking-at "[ \t\n]*\n") (replace-match ""))
557 (unless macro
558 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))))
559 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
561 (org-current-line))
562 col (current-column))
563 (when allow-write-back-p
564 (when single
565 (goto-char (point-min))
566 (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
567 (goto-char (point-min))
568 (let ((cnt 0))
569 (while (re-search-forward "\n" nil t)
570 (setq cnt (1+ cnt))
571 (replace-match (if macro "\\n" " ") t t))
572 (when (and macro (> cnt 0))
573 (goto-char (point-max)) (insert "\\n")))
574 (goto-char (point-min))
575 (if (looking-at "\\s-*") (replace-match " ")))
576 (when (org-bound-and-true-p org-edit-src-from-org-mode)
577 (goto-char (point-min))
578 (while (re-search-forward
579 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
580 (if (eq (org-current-line) line) (setq delta (1+ delta)))
581 (replace-match ",\\1")))
582 (when (org-bound-and-true-p org-edit-src-picture)
583 (setq preserve-indentation nil)
584 (untabify (point-min) (point-max))
585 (goto-char (point-min))
586 (while (re-search-forward "^" nil t)
587 (replace-match ": ")))
588 (unless (or single preserve-indentation (= total-nindent 0))
589 (setq indent (make-string total-nindent ?\ ))
590 (goto-char (point-min))
591 (while (re-search-forward "^" nil t)
592 (replace-match indent)))
593 (if (org-bound-and-true-p org-edit-src-picture)
594 (setq total-nindent (+ total-nindent 2)))
595 (setq code (buffer-string))
596 (set-buffer-modified-p nil))
597 (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
598 (kill-buffer buffer)
599 (goto-char beg)
600 (when allow-write-back-p
601 (delete-region beg end)
602 (insert code)
603 (goto-char beg)
604 (if single (just-one-space)))
605 (if (memq t (mapcar (lambda (overlay)
606 (eq (overlay-get overlay 'invisible)
607 'org-hide-block))
608 (overlays-at (point))))
609 ;; Block is hidden; put point at start of block
610 (beginning-of-line 0)
611 ;; Block is visible, put point where it was in the code buffer
612 (org-goto-line (1- (+ (org-current-line) line)))
613 (org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
614 (move-marker beg nil)
615 (move-marker end nil))
616 (unless (eq context 'save)
617 (when org-edit-src-saved-temp-window-config
618 (set-window-configuration org-edit-src-saved-temp-window-config)
619 (setq org-edit-src-saved-temp-window-config nil))))
621 (defun org-edit-src-save ()
622 "Save parent buffer with current state source-code buffer."
623 (interactive)
624 (let ((p (point)) (m (mark)) msg)
625 (save-window-excursion
626 (org-edit-src-exit 'save)
627 (save-buffer)
628 (setq msg (current-message))
629 (if (eq org-src-window-setup 'other-frame)
630 (let ((org-src-window-setup 'current-window))
631 (org-edit-src-code 'save))
632 (org-edit-src-code 'save)))
633 (push-mark m 'nomessage)
634 (goto-char (min p (point-max)))
635 (message (or msg ""))))
637 (defun org-src-mode-configure-edit-buffer ()
638 (when (org-bound-and-true-p org-edit-src-from-org-mode)
639 (org-add-hook 'kill-buffer-hook
640 '(lambda () (delete-overlay org-edit-src-overlay)) nil 'local)
641 (if (org-bound-and-true-p org-edit-src-allow-write-back-p)
642 (progn
643 (setq buffer-offer-save t)
644 (setq buffer-file-name
645 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
646 "[" (buffer-name) "]"))
647 (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
648 '(org-edit-src-save)))
649 (setq buffer-read-only t))))
651 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
653 (provide 'org-src)
655 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
656 ;;; org-src.el ends here