New function: Open nntp: links with Wanderlust
[org-mode/org-kjn.git] / lisp / org-src.el
blob233058a54f2ea39660c0cf6cc4ded77caa504758
1 ;;; org-src.el --- Source code examples in Org
2 ;;
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Bastien Guerry <bzg AT altern DOT org>
8 ;; Dan Davison <davison at stats dot ox dot ac dot uk>
9 ;; Keywords: outlines, hypermedia, calendar, wp
10 ;; Homepage: http://orgmode.org
11 ;; Version: 7.01trans
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;; Commentary:
31 ;; This file contains the code dealing with source code examples in Org-mode.
33 ;;; Code:
35 (require 'org-macs)
36 (require 'org-compat)
37 (require 'ob-keys)
38 (require 'ob-comint)
39 (eval-when-compile
40 (require 'cl))
42 (declare-function org-do-remove-indentation "org" (&optional n))
43 (declare-function org-at-table.el-p "org" ())
44 (declare-function org-get-indentation "org" (&optional line))
45 (declare-function org-switch-to-buffer-other-window "org" (&rest args))
47 (defcustom org-edit-src-region-extra nil
48 "Additional regexps to identify regions for editing with `org-edit-src-code'.
49 For examples see the function `org-edit-src-find-region-and-lang'.
50 The regular expression identifying the begin marker should end with a newline,
51 and the regexp marking the end line should start with a newline, to make sure
52 there are kept outside the narrowed region."
53 :group 'org-edit-structure
54 :type '(repeat
55 (list
56 (regexp :tag "begin regexp")
57 (regexp :tag "end regexp")
58 (choice :tag "language"
59 (string :tag "specify")
60 (integer :tag "from match group")
61 (const :tag "from `lang' element")
62 (const :tag "from `style' element")))))
64 (defcustom org-coderef-label-format "(ref:%s)"
65 "The default coderef format.
66 This format string will be used to search for coderef labels in literal
67 examples (EXAMPLE and SRC blocks). The format can be overwritten in
68 an individual literal example with the -l option, like
70 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
71 ...
72 #+END_SRC
74 If you want to use this for HTML export, make sure that the format does
75 not introduce special font-locking, and avoid the HTML special
76 characters `<', `>', and `&'. The reason for this restriction is that
77 the labels are searched for only after htmlize has done its job."
78 :group 'org-edit-structure ; FIXME this is not in the right group
79 :type 'string)
81 (defcustom org-edit-fixed-width-region-mode 'artist-mode
82 "The mode that should be used to edit fixed-width regions.
83 These are the regions where each line starts with a colon."
84 :group 'org-edit-structure
85 :type '(choice
86 (const artist-mode)
87 (const picture-mode)
88 (const fundamental-mode)
89 (function :tag "Other (specify)")))
91 (defcustom org-src-preserve-indentation nil
92 "If non-nil preserve leading whitespace characters on export.
93 If non-nil leading whitespace characters in source code blocks
94 are preserved on export, and when switching between the org
95 buffer and the language mode edit buffer. If this variable is nil
96 then, after editing with \\[org-edit-src-code], the
97 minimum (across-lines) number of leading whitespace characters
98 are removed from all lines, and the code block is uniformly
99 indented according to the value of `org-edit-src-content-indentation'."
100 :group 'org-edit-structure
101 :type 'boolean)
103 (defcustom org-edit-src-content-indentation 2
104 "Indentation for the content of a source code block.
105 This should be the number of spaces added to the indentation of the #+begin
106 line in order to compute the indentation of the block content after
107 editing it with \\[org-edit-src-code]. Has no effect if
108 `org-src-preserve-indentation' is non-nil."
109 :group 'org-edit-structure
110 :type 'integer)
112 (defcustom org-edit-src-persistent-message t
113 "Non-nil means show persistent exit help message while editing src examples.
114 The message is shown in the header-line, which will be created in the
115 first line of the window showing the editing buffer.
116 When nil, the message will only be shown intermittently in the echo area."
117 :group 'org-edit-structure
118 :type 'boolean)
120 (defcustom org-src-window-setup 'reorganize-frame
121 "How the source code edit buffer should be displayed.
122 Possible values for this option are:
124 current-window Show edit buffer in the current window, keeping all other
125 windows.
126 other-window Use `switch-to-buffer-other-window' to display edit buffer.
127 reorganize-frame Show only two windows on the current frame, the current
128 window and the edit buffer. When exiting the edit buffer,
129 return to one window.
130 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
131 Also, when exiting the edit buffer, kill that frame."
132 :group 'org-edit-structure
133 :type '(choice
134 (const current-window)
135 (const other-frame)
136 (const other-window)
137 (const reorganize-frame)))
139 (defvar org-src-mode-hook nil
140 "Hook run after Org switched a source code snippet to its Emacs mode.
141 This hook will run
143 - when editing a source code snippet with \"C-c '\".
144 - When formatting a source code snippet for export with htmlize.
146 You may want to use this hook for example to turn off `outline-minor-mode'
147 or similar things which you want to have when editing a source code file,
148 but which mess up the display of a snippet in Org exported files.")
150 (defcustom org-src-lang-modes
151 '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
152 ("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql))
153 "Alist mapping languages to their major mode.
154 The key is the language name, the value is the string that should
155 be inserted as the name of the major mode. For many languages this is
156 simple, but for language where this is not the case, this variable
157 provides a way to simplify things on the user side.
158 For example, there is no ocaml-mode in Emacs, but the mode to use is
159 `tuareg-mode'."
160 :group 'org-edit-structure
161 :type '(repeat
162 (cons
163 (string "Language name")
164 (symbol "Major mode"))))
166 ;;; Editing source examples
168 (defvar org-src-mode-map (make-sparse-keymap))
169 (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
171 (defvar org-edit-src-force-single-line nil)
172 (defvar org-edit-src-from-org-mode nil)
173 (defvar org-edit-src-allow-write-back-p t)
174 (defvar org-edit-src-picture nil)
175 (defvar org-edit-src-beg-marker nil)
176 (defvar org-edit-src-end-marker nil)
177 (defvar org-edit-src-overlay nil)
178 (defvar org-edit-src-block-indentation nil)
179 (defvar org-edit-src-saved-temp-window-config nil)
181 (defvar org-src-ask-before-returning-to-edit-buffer t
182 "If nil, when org-edit-src code is used on a block that already
183 has an active edit buffer, it will switch to that edit buffer
184 immediately; otherwise it will ask whether you want to return
185 to the existing edit buffer.")
187 (defvar org-src-babel-info nil)
189 (define-minor-mode org-src-mode
190 "Minor mode for language major mode buffers generated by org.
191 This minor mode is turned on in two situations:
192 - when editing a source code snippet with \"C-c '\".
193 - When formatting a source code snippet for export with htmlize.
194 There is a mode hook, and keybindings for `org-edit-src-exit' and
195 `org-edit-src-save'")
197 (defun org-edit-src-code (&optional context code edit-buffer-name quietp)
198 "Edit the source code example at point.
199 The example is copied to a separate buffer, and that buffer is
200 switched to the correct language mode. When done, exit with
201 \\[org-edit-src-exit]. This will remove the original code in the
202 Org buffer, and replace it with the edited version. Optional
203 argument CONTEXT is used by \\[org-edit-src-save] when calling
204 this function. See \\[org-src-window-setup] to configure the
205 display of windows containing the Org buffer and the code
206 buffer."
207 (interactive)
208 (unless (eq context 'save)
209 (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
210 (let ((line (org-current-line))
211 (col (current-column))
212 (case-fold-search t)
213 (info (org-edit-src-find-region-and-lang))
214 (babel-info (org-babel-get-src-block-info))
215 (org-mode-p (eq major-mode 'org-mode))
216 (beg (make-marker))
217 (end (make-marker))
218 (preserve-indentation org-src-preserve-indentation)
219 (allow-write-back-p (null code))
220 block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg)
221 (if (not info)
223 (setq beg (move-marker beg (nth 0 info))
224 end (move-marker end (nth 1 info))
225 msg (if allow-write-back-p
226 (substitute-command-keys
227 "Edit, then exit with C-c ' (C-c and single quote)")
228 "Exit with C-c ' (C-c and single quote)")
229 code (or code (buffer-substring-no-properties beg end))
230 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
231 (nth 2 info))
232 lang (if (symbolp lang) (symbol-name lang) lang)
233 single (nth 3 info)
234 lfmt (nth 4 info)
235 block-nindent (nth 5 info)
236 lang-f (intern (concat lang "-mode"))
237 begline (save-excursion (goto-char beg) (org-current-line)))
238 (if (equal lang-f 'table.el-mode)
239 (setq lang-f (lambda ()
240 (text-mode)
241 (if (org-bound-and-true-p flyspell-mode)
242 (flyspell-mode -1))
243 (table-recognize)
244 (org-set-local 'org-edit-src-content-indentation 0))))
245 (unless (functionp lang-f)
246 (error "No such language mode: %s" lang-f))
247 (org-goto-line line)
248 (if (and (setq buffer (org-edit-src-find-buffer beg end))
249 (if org-src-ask-before-returning-to-edit-buffer
250 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
251 (org-src-switch-to-buffer buffer 'return)
252 (when buffer
253 (with-current-buffer buffer
254 (if (boundp 'org-edit-src-overlay)
255 (delete-overlay org-edit-src-overlay)))
256 (kill-buffer buffer))
257 (setq buffer (generate-new-buffer
258 (or edit-buffer-name
259 (org-src-construct-edit-buffer-name (buffer-name) lang))))
260 (setq ovl (make-overlay beg end))
261 (overlay-put ovl 'edit-buffer buffer)
262 (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
263 (overlay-put ovl 'face 'secondary-selection)
264 (overlay-put ovl
265 'keymap
266 (let ((map (make-sparse-keymap)))
267 (define-key map [mouse-1] 'org-edit-src-continue)
268 map))
269 (overlay-put ovl :read-only "Leave me alone")
270 (org-src-switch-to-buffer buffer 'edit)
271 (if (eq single 'macro-definition)
272 (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
273 (insert code)
274 (remove-text-properties (point-min) (point-max)
275 '(display nil invisible nil intangible nil))
276 (unless preserve-indentation
277 (setq total-nindent (or (org-do-remove-indentation) 0)))
278 (let ((org-inhibit-startup t))
279 (condition-case e
280 (funcall lang-f)
281 (error
282 (error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
283 (set (make-local-variable 'org-edit-src-force-single-line) single)
284 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
285 (set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p)
286 (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
287 (when babel-info
288 (set (make-local-variable 'org-src-babel-info) babel-info))
289 (when lfmt
290 (set (make-local-variable 'org-coderef-label-format) lfmt))
291 (when org-mode-p
292 (goto-char (point-min))
293 (while (re-search-forward "^," nil t)
294 (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
295 (replace-match "")))
296 (org-goto-line (1+ (- line begline)))
297 (org-move-to-column
298 (if preserve-indentation col (max 0 (- col total-nindent))))
299 (org-set-local 'org-edit-src-beg-marker beg)
300 (org-set-local 'org-edit-src-end-marker end)
301 (org-set-local 'org-edit-src-overlay ovl)
302 (org-set-local 'org-edit-src-block-indentation block-nindent)
303 (org-src-mode)
304 (set-buffer-modified-p nil)
305 (and org-edit-src-persistent-message
306 (org-set-local 'header-line-format msg)))
307 (unless quietp (message "%s" msg))
308 t)))
310 (defun org-edit-src-continue (e)
311 (interactive "e")
312 (mouse-set-point e)
313 (let ((buf (get-char-property (point) 'edit-buffer)))
314 (if buf (org-src-switch-to-buffer buf 'continue)
315 (error "Something is wrong here"))))
317 (defun org-src-switch-to-buffer (buffer context)
318 (case org-src-window-setup
319 ('current-window
320 (switch-to-buffer buffer))
321 ('other-window
322 (switch-to-buffer-other-window buffer))
323 ('other-frame
324 (case context
325 ('exit
326 (let ((frame (selected-frame)))
327 (switch-to-buffer-other-frame buffer)
328 (delete-frame frame)))
329 ('save
330 (kill-buffer (current-buffer))
331 (switch-to-buffer buffer))
333 (switch-to-buffer-other-frame buffer))))
334 ('reorganize-frame
335 (if (eq context 'edit) (delete-other-windows))
336 (org-switch-to-buffer-other-window buffer)
337 (if (eq context 'exit) (delete-other-windows)))
338 ('switch-invisibly
339 (set-buffer buffer))
341 (message "Invalid value %s for org-src-window-setup"
342 (symbol-name org-src-window-setup))
343 (switch-to-buffer buffer))))
345 (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
346 "Construct the buffer name for a source editing buffer."
347 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
349 (defun org-edit-src-find-buffer (beg end)
350 "Find a source editing buffer that is already editing the region BEG to END."
351 (catch 'exit
352 (mapc
353 (lambda (b)
354 (with-current-buffer b
355 (if (and (string-match "\\`*Org Src " (buffer-name))
356 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
357 (local-variable-p 'org-edit-src-end-marker (current-buffer))
358 (equal beg org-edit-src-beg-marker)
359 (equal end org-edit-src-end-marker))
360 (throw 'exit (current-buffer)))))
361 (buffer-list))
362 nil))
364 (defun org-edit-fixed-width-region ()
365 "Edit the fixed-width ascii drawing at point.
366 This must be a region where each line starts with a colon followed by
367 a space character.
368 An new buffer is created and the fixed-width region is copied into it,
369 and the buffer is switched into `artist-mode' for editing. When done,
370 exit with \\[org-edit-src-exit]. The edited text will then replace
371 the fragment in the Org-mode buffer."
372 (interactive)
373 (let ((line (org-current-line))
374 (col (current-column))
375 (case-fold-search t)
376 (msg (substitute-command-keys
377 "Edit, then exit with C-c ' (C-c and single quote)"))
378 (org-mode-p (eq major-mode 'org-mode))
379 (beg (make-marker))
380 (end (make-marker))
381 (preserve-indentation org-src-preserve-indentation)
382 block-nindent ovl beg1 end1 code begline buffer)
383 (beginning-of-line 1)
384 (if (looking-at "[ \t]*[^:\n \t]")
386 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
387 (setq beg1 (point) end1 beg1)
388 (save-excursion
389 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
390 (setq beg1 (point-at-bol 2))
391 (setq beg1 (point))))
392 (save-excursion
393 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
394 (setq end1 (1- (match-beginning 0)))
395 (setq end1 (point))))
396 (org-goto-line line))
397 (setq beg (move-marker beg beg1)
398 end (move-marker end end1)
399 code (buffer-substring-no-properties beg end)
400 begline (save-excursion (goto-char beg) (org-current-line)))
401 (if (and (setq buffer (org-edit-src-find-buffer beg end))
402 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
403 (switch-to-buffer buffer)
404 (when buffer
405 (with-current-buffer buffer
406 (if (boundp 'org-edit-src-overlay)
407 (delete-overlay org-edit-src-overlay)))
408 (kill-buffer buffer))
409 (setq buffer (generate-new-buffer
410 (org-src-construct-edit-buffer-name
411 (buffer-name) "Fixed Width")))
412 (setq ovl (make-overlay beg end))
413 (overlay-put ovl 'face 'secondary-selection)
414 (overlay-put ovl 'edit-buffer buffer)
415 (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
416 (overlay-put ovl 'face 'secondary-selection)
417 (overlay-put ovl
418 'keymap
419 (let ((map (make-sparse-keymap)))
420 (define-key map [mouse-1] 'org-edit-src-continue)
421 map))
422 (overlay-put ovl :read-only "Leave me alone")
423 (switch-to-buffer buffer)
424 (insert code)
425 (remove-text-properties (point-min) (point-max)
426 '(display nil invisible nil intangible nil))
427 (setq block-nindent (or (org-do-remove-indentation) 0))
428 (cond
429 ((eq org-edit-fixed-width-region-mode 'artist-mode)
430 (fundamental-mode)
431 (artist-mode 1))
432 (t (funcall org-edit-fixed-width-region-mode)))
433 (set (make-local-variable 'org-edit-src-force-single-line) nil)
434 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
435 (set (make-local-variable 'org-edit-src-picture) t)
436 (goto-char (point-min))
437 (while (re-search-forward "^[ \t]*: ?" nil t)
438 (replace-match ""))
439 (org-goto-line (1+ (- line begline)))
440 (org-move-to-column (max 0 (- col block-nindent 2)))
441 (org-set-local 'org-edit-src-beg-marker beg)
442 (org-set-local 'org-edit-src-end-marker end)
443 (org-set-local 'org-edit-src-overlay ovl)
444 (org-set-local 'org-edit-src-block-indentation block-nindent)
445 (org-set-local 'org-edit-src-content-indentation 0)
446 (org-set-local 'org-src-preserve-indentation nil)
447 (org-src-mode)
448 (set-buffer-modified-p nil)
449 (and org-edit-src-persistent-message
450 (org-set-local 'header-line-format msg)))
451 (message "%s" msg)
452 t)))
454 (defun org-edit-src-find-region-and-lang ()
455 "Find the region and language for a local edit.
456 Return a list with beginning and end of the region, a string representing
457 the language, a switch telling if the content should be in a single line."
458 (let ((re-list
459 (append
460 org-edit-src-region-extra
462 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
463 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
464 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
465 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
466 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
467 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
468 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
469 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
470 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
471 ("^[ \t]*#\\+html:" "\n" "html" single-line)
472 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
473 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
474 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
475 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
476 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
477 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
478 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
479 "\n" "fundamental" macro-definition)
480 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
482 (pos (point))
483 re1 re2 single beg end lang lfmt match-re1 ind entry)
484 (catch 'exit
485 (while (setq entry (pop re-list))
486 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
487 single (nth 3 entry))
488 (save-excursion
489 (if (or (looking-at re1)
490 (re-search-backward re1 nil t))
491 (progn
492 (setq match-re1 (match-string 0))
493 (setq beg (match-end 0)
494 lang (org-edit-src-get-lang lang)
495 lfmt (org-edit-src-get-label-format match-re1)
496 ind (org-edit-src-get-indentation (match-beginning 0)))
497 (if (and (re-search-forward re2 nil t)
498 (>= (match-end 0) pos))
499 (throw 'exit (list beg (match-beginning 0)
500 lang single lfmt ind))))
501 (if (or (looking-at re2)
502 (re-search-forward re2 nil t))
503 (progn
504 (setq end (match-beginning 0))
505 (if (and (re-search-backward re1 nil t)
506 (<= (match-beginning 0) pos))
507 (progn
508 (setq lfmt (org-edit-src-get-label-format
509 (match-string 0))
510 ind (org-edit-src-get-indentation
511 (match-beginning 0)))
512 (throw 'exit
513 (list (match-end 0) end
514 (org-edit-src-get-lang lang)
515 single lfmt ind)))))))))
516 (when (org-at-table.el-p)
517 (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
518 (setq beg (1+ (point-at-eol)))
519 (goto-char beg)
520 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
521 (progn (goto-char (point-max)) (newline)))
522 (setq end (point-at-bol))
523 (setq ind (org-edit-src-get-indentation beg))
524 (throw 'exit (list beg end 'table.el nil nil ind))))))
526 (defun org-edit-src-get-lang (lang)
527 "Extract the src language."
528 (let ((m (match-string 0)))
529 (cond
530 ((stringp lang) lang)
531 ((integerp lang) (match-string lang))
532 ((and (eq lang 'lang)
533 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
534 (match-string 1 m))
535 ((and (eq lang 'style)
536 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
537 (match-string 1 m))
538 (t "fundamental"))))
540 (defun org-edit-src-get-label-format (s)
541 "Extract the label format."
542 (save-match-data
543 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
544 (match-string 1 s))))
546 (defun org-edit-src-get-indentation (pos)
547 "Count leading whitespace characters on line."
548 (save-match-data
549 (goto-char pos)
550 (org-get-indentation)))
552 (defun org-edit-src-exit (&optional context)
553 "Exit special edit and protect problematic lines."
554 (interactive)
555 (unless (org-bound-and-true-p org-edit-src-from-org-mode)
556 (error "This is not a sub-editing buffer, something is wrong"))
557 (widen)
558 (let* ((beg org-edit-src-beg-marker)
559 (end org-edit-src-end-marker)
560 (ovl org-edit-src-overlay)
561 (buffer (current-buffer))
562 (single (org-bound-and-true-p org-edit-src-force-single-line))
563 (macro (eq single 'macro-definition))
564 (total-nindent (+ (or org-edit-src-block-indentation 0)
565 org-edit-src-content-indentation))
566 (preserve-indentation org-src-preserve-indentation)
567 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p))
568 (delta 0) code line col indent)
569 (when allow-write-back-p
570 (unless preserve-indentation (untabify (point-min) (point-max)))
571 (save-excursion
572 (goto-char (point-min))
573 (if (looking-at "[ \t\n]*\n") (replace-match ""))
574 (unless macro
575 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))))
576 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
578 (org-current-line))
579 col (current-column))
580 (when allow-write-back-p
581 (when single
582 (goto-char (point-min))
583 (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
584 (goto-char (point-min))
585 (let ((cnt 0))
586 (while (re-search-forward "\n" nil t)
587 (setq cnt (1+ cnt))
588 (replace-match (if macro "\\n" " ") t t))
589 (when (and macro (> cnt 0))
590 (goto-char (point-max)) (insert "\\n")))
591 (goto-char (point-min))
592 (if (looking-at "\\s-*") (replace-match " ")))
593 (when (org-bound-and-true-p org-edit-src-from-org-mode)
594 (goto-char (point-min))
595 (while (re-search-forward
596 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
597 (if (eq (org-current-line) line) (setq delta (1+ delta)))
598 (replace-match ",\\1")))
599 (when (org-bound-and-true-p org-edit-src-picture)
600 (setq preserve-indentation nil)
601 (untabify (point-min) (point-max))
602 (goto-char (point-min))
603 (while (re-search-forward "^" nil t)
604 (replace-match ": ")))
605 (unless (or single preserve-indentation (= total-nindent 0))
606 (setq indent (make-string total-nindent ?\ ))
607 (goto-char (point-min))
608 (while (re-search-forward "^" nil t)
609 (replace-match indent)))
610 (if (org-bound-and-true-p org-edit-src-picture)
611 (setq total-nindent (+ total-nindent 2)))
612 (setq code (buffer-string))
613 (set-buffer-modified-p nil))
614 (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
615 (kill-buffer buffer)
616 (goto-char beg)
617 (when allow-write-back-p
618 (delete-region beg end)
619 (insert code)
620 (goto-char beg)
621 (if single (just-one-space)))
622 (if (memq t (mapcar (lambda (overlay)
623 (eq (overlay-get overlay 'invisible)
624 'org-hide-block))
625 (overlays-at (point))))
626 ;; Block is hidden; put point at start of block
627 (beginning-of-line 0)
628 ;; Block is visible, put point where it was in the code buffer
629 (org-goto-line (1- (+ (org-current-line) line)))
630 (org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
631 (move-marker beg nil)
632 (move-marker end nil))
633 (unless (eq context 'save)
634 (when org-edit-src-saved-temp-window-config
635 (set-window-configuration org-edit-src-saved-temp-window-config)
636 (setq org-edit-src-saved-temp-window-config nil))))
638 (defun org-edit-src-save ()
639 "Save parent buffer with current state source-code buffer."
640 (interactive)
641 (let ((p (point)) (m (mark)) msg)
642 (save-window-excursion
643 (org-edit-src-exit 'save)
644 (save-buffer)
645 (setq msg (current-message))
646 (if (eq org-src-window-setup 'other-frame)
647 (let ((org-src-window-setup 'current-window))
648 (org-edit-src-code 'save))
649 (org-edit-src-code 'save)))
650 (push-mark m 'nomessage)
651 (goto-char (min p (point-max)))
652 (message (or msg ""))))
654 (defun org-src-mode-configure-edit-buffer ()
655 (when (org-bound-and-true-p org-edit-src-from-org-mode)
656 (org-add-hook 'kill-buffer-hook
657 '(lambda () (delete-overlay org-edit-src-overlay)) nil 'local)
658 (if (org-bound-and-true-p org-edit-src-allow-write-back-p)
659 (progn
660 (setq buffer-offer-save t)
661 (setq buffer-file-name
662 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
663 "[" (buffer-name) "]"))
664 (if (featurep 'xemacs)
665 (progn
666 (make-variable-buffer-local 'write-contents-hooks) ; needed only for 21.4
667 (setq write-contents-hooks '(org-edit-src-save)))
668 (setq write-contents-functions '(org-edit-src-save))))
669 (setq buffer-read-only t))))
671 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
674 (defun org-src-associate-babel-session (info)
675 "Associate edit buffer with comint session."
676 (interactive)
677 (let ((session (cdr (assoc :session (nth 2 info)))))
678 (and session (not (string= session "none"))
679 (org-babel-comint-buffer-livep session)
680 ((lambda (f) (and (fboundp f) (funcall f session)))
681 (intern (format "org-babel-%s-associate-session" (nth 0 info)))))))
683 (defun org-src-babel-configure-edit-buffer ()
684 (when org-src-babel-info
685 (org-src-associate-babel-session org-src-babel-info)))
687 (org-add-hook 'org-src-mode-hook 'org-src-babel-configure-edit-buffer)
688 (defmacro org-src-do-at-code-block (&rest body)
689 "Execute a command from an edit buffer in the Org-mode buffer."
690 `(let ((beg-marker org-edit-src-beg-marker))
691 (if beg-marker
692 (with-current-buffer (marker-buffer beg-marker)
693 (goto-char (marker-position beg-marker))
694 ,@body))))
696 (defun org-src-do-key-sequence-at-code-block (&optional key)
697 "Execute key sequence at code block in the source Org buffer.
698 The command bound to KEY in the Org-babel key map is executed
699 remotely with point temporarily at the start of the code block in
700 the Org buffer.
702 This command is not bound to a key by default, to avoid conflicts
703 with language major mode bindings. To bind it to C-c @ in all
704 language major modes, you could use
706 (add-hook 'org-src-mode-hook
707 (lambda () (define-key org-src-mode-map \"\\C-c@\"
708 'org-src-do-key-sequence-at-code-block)))
710 In that case, for example, C-c @ t issued in code edit buffers
711 would tangle the current Org code block, C-c @ e would execute
712 the block and C-c @ h would display the other available
713 Org-babel commands."
714 (interactive "kOrg-babel key: ")
715 (if (equal key (kbd "C-g")) (keyboard-quit)
716 (org-edit-src-save)
717 (org-src-do-at-code-block
718 (call-interactively
719 (lookup-key org-babel-map key)))))
721 (defvar org-src-tab-acts-natively nil
722 "If non-nil, the effect of TAB in a code block is as if it were
723 issued in the language major mode buffer.")
725 (defun org-src-native-tab-command-maybe ()
726 "Perform language-specific TAB action.
727 Alter code block according to effect of TAB in the language major
728 mode."
729 (and org-src-tab-acts-natively
730 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
732 (add-hook 'org-tab-first-hook 'org-src-native-tab-command-maybe)
734 (defun org-src-font-lock-fontify-block (lang start end)
735 "Fontify code block.
736 This function is called by emacs automatic fontification, as long
737 as `org-src-fontify-natively' is non-nil. For manual
738 fontification of code blocks see `org-src-fontify-block' and
739 `org-src-fontify-buffer'"
740 (let* ((lang-mode (org-src-get-lang-mode lang))
741 (string (buffer-substring-no-properties start end))
742 (modified (buffer-modified-p))
743 (org-buffer (current-buffer)) pos next)
744 (remove-text-properties start end '(face nil))
745 (with-current-buffer
746 (get-buffer-create
747 (concat " org-src-fontification:" (symbol-name lang-mode)))
748 (delete-region (point-min) (point-max))
749 (insert string)
750 (unless (eq major-mode lang-mode) (funcall lang-mode))
751 (font-lock-fontify-buffer)
752 (setq pos (point-min))
753 (while (setq next (next-single-property-change pos 'face))
754 (put-text-property
755 (+ start (1- pos)) (+ start next) 'face
756 (get-text-property pos 'face) org-buffer)
757 (setq pos next)))
758 (add-text-properties
759 start end
760 '(font-lock-fontified t fontified t font-lock-multiline t))
761 (set-buffer-modified-p modified))
762 t) ;; Tell `org-fontify-meta-lines-and-blocks' that we fontified
764 (defun org-src-fontify-block ()
765 "Fontify code block at point."
766 (interactive)
767 (save-excursion
768 (let ((org-src-fontify-natively t)
769 (info (org-edit-src-find-region-and-lang)))
770 (font-lock-fontify-region (nth 0 info) (nth 1 info)))))
772 (defun org-src-fontify-buffer ()
773 "Fontify all code blocks in the current buffer"
774 (interactive)
775 (org-babel-map-src-blocks nil
776 (org-src-fontify-block)))
778 (defun org-src-get-lang-mode (lang)
779 "Return major mode that should be used for LANG.
780 LANG is a string, and the returned major mode is a symbol."
781 (intern
782 (concat
783 ((lambda (l) (if (symbolp l) (symbol-name l) l))
784 (or (cdr (assoc lang org-src-lang-modes)) lang)) "-mode")))
786 (provide 'org-src)
788 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
789 ;;; org-src.el ends here