org-babel: asymptote maps to asy-mode in `org-src-lang-modes' and added requirements...
[org-mode.git] / lisp / org-src.el
blob5cd2fc670b360252ccfb0e7736e1cf3d195a9732
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 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
10 ;; Version: 6.30trans
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file contains the code dealing with source code examples in Org-mode.
32 ;;; Code:
34 (require 'org-macs)
35 (require 'org-compat)
37 (declare-function org-do-remove-indentation "org" (&optional n))
38 (declare-function org-get-indentation "org" (&optional line))
40 (defcustom org-edit-src-region-extra nil
41 "Additional regexps to identify regions for editing with `org-edit-src-code'.
42 For examples see the function `org-edit-src-find-region-and-lang'.
43 The regular expression identifying the begin marker should end with a newline,
44 and the regexp marking the end line should start with a newline, to make sure
45 there are kept outside the narrowed region."
46 :group 'org-edit-structure
47 :type '(repeat
48 (list
49 (regexp :tag "begin regexp")
50 (regexp :tag "end regexp")
51 (choice :tag "language"
52 (string :tag "specify")
53 (integer :tag "from match group")
54 (const :tag "from `lang' element")
55 (const :tag "from `style' element")))))
57 (defcustom org-coderef-label-format "(ref:%s)"
58 "The default coderef format.
59 This format string will be used to search for coderef labels in literal
60 examples (EXAMPLE and SRC blocks). The format can be overwritten in
61 an individual literal example with the -f option, like
63 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
64 ...
65 #+END_SRC
67 If you want to use this for HTML export, make sure that the format does
68 not introduce special font-locking, and avoid the HTML special
69 characters `<', `>', and `&'. The reason for this restriction is that
70 the labels are searched for only after htmlize has done its job."
71 :group 'org-edit-structure ; FIXME this is not in the right group
72 :type 'string)
74 (defcustom org-edit-fixed-width-region-mode 'artist-mode
75 "The mode that should be used to edit fixed-width regions.
76 These are the regions where each line starts with a colon."
77 :group 'org-edit-structure
78 :type '(choice
79 (const artist-mode)
80 (const picture-mode)
81 (const fundamental-mode)
82 (function :tag "Other (specify)")))
84 (defcustom org-edit-src-content-indentation 2
85 "Indentation for the content is a source code block.
86 This should be the number of spaces added to the indentation of the #+begin
87 line in order to compute the indentation of the block content after
88 editing it with \\[org-edit-src-code]."
89 :group 'org-edit-structure
90 :type 'integer)
92 (defcustom org-edit-src-persistent-message t
93 "Non-nil means show persistent exit help message while editing src examples.
94 The message is shown in the header-line, which will be created in the
95 first line of the window showing the editing buffer.
96 When nil, the message will only be shown intermittently in the echo area."
97 :group 'org-edit-structure
98 :type 'boolean)
101 (defvar org-src-mode-hook nil
102 "Hook run after Org switched a source code snippet to its Emacs mode.
103 This hook will run
105 - when editing a source code snippet with \"C-c '\".
106 - When formatting a source code snippet for export with htmlize.
108 You may want to use this hook for example to turn off `outline-minor-mode'
109 or similar things which you want to have when editing a source code file,
110 but which mess up the display of a snippet in Org exported files.")
112 (defcustom org-src-lang-modes
113 '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
114 ("asymptote" . asy))
115 "Alist mapping languages to their major mode.
116 The key is the language name, the value is the string that should
117 be inserted as the name of the major mode. For many languages this is
118 simple, but for language where this is not the case, this variable
119 provides a way to simplify things on the user side.
120 For example, there is no ocaml-mode in Emacs, but the mode to use is
121 `tuareg-mode'."
122 :group 'org-edit-structure
123 :type '(repeat
124 (cons
125 (string "Language name")
126 (symbol "Major mode"))))
128 ;;; Editing source examples
130 (defvar org-src-mode-map (make-sparse-keymap))
131 (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
132 (defvar org-edit-src-force-single-line nil)
133 (defvar org-edit-src-from-org-mode nil)
134 (defvar org-edit-src-picture nil)
135 (defvar org-edit-src-beg-marker nil)
136 (defvar org-edit-src-end-marker nil)
137 (defvar org-edit-src-overlay nil)
138 (defvar org-edit-src-nindent nil)
140 (define-minor-mode org-src-mode
141 "Minor mode for language major mode buffers generated by org.
142 This minor mode is turned on in two situations:
143 - when editing a source code snippet with \"C-c '\".
144 - When formatting a source code snippet for export with htmlize.
145 There is a mode hook, and keybindings for `org-edit-src-exit' and
146 `org-edit-src-save'")
148 (defun org-edit-src-code ()
149 "Edit the source code example at point.
150 The example is copied to a separate buffer, and that buffer is switched
151 to the correct language mode. When done, exit with \\[org-edit-src-exit].
152 This will remove the original code in the Org buffer, and replace it with
153 the edited version."
154 (interactive)
155 (let ((line (org-current-line))
156 (case-fold-search t)
157 (msg (substitute-command-keys
158 "Edit, then exit with C-c ' (C-c and single quote)"))
159 (info (org-edit-src-find-region-and-lang))
160 (org-mode-p (eq major-mode 'org-mode))
161 (beg (make-marker))
162 (end (make-marker))
163 nindent ovl lang lang-f single lfmt code begline buffer)
164 (if (not info)
166 (setq beg (move-marker beg (nth 0 info))
167 end (move-marker end (nth 1 info))
168 code (buffer-substring-no-properties beg end)
169 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
170 (nth 2 info))
171 lang (if (symbolp lang) (symbol-name lang) lang)
172 single (nth 3 info)
173 lfmt (nth 4 info)
174 nindent (nth 5 info)
175 lang-f (intern (concat lang "-mode"))
176 begline (save-excursion (goto-char beg) (org-current-line)))
177 (unless (functionp lang-f)
178 (error "No such language mode: %s" lang-f))
179 (org-goto-line line)
180 (if (and (setq buffer (org-edit-src-find-buffer beg end))
181 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
182 (switch-to-buffer buffer)
183 (when buffer
184 (with-current-buffer buffer
185 (if (boundp 'org-edit-src-overlay)
186 (org-delete-overlay org-edit-src-overlay)))
187 (kill-buffer buffer))
188 (setq buffer (generate-new-buffer
189 (org-src-construct-edit-buffer-name (buffer-name) lang)))
190 (setq ovl (org-make-overlay beg end))
191 (org-overlay-put ovl 'face 'secondary-selection)
192 (org-overlay-put ovl 'edit-buffer buffer)
193 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
194 (org-overlay-put ovl 'face 'secondary-selection)
195 (org-overlay-put ovl
196 'keymap
197 (let ((map (make-sparse-keymap)))
198 (define-key map [mouse-1] 'org-edit-src-continue)
199 map))
200 (org-overlay-put ovl :read-only "Leave me alone")
201 (switch-to-buffer buffer)
202 (insert code)
203 (remove-text-properties (point-min) (point-max)
204 '(display nil invisible nil intangible nil))
205 (org-do-remove-indentation)
206 (let ((org-inhibit-startup t))
207 (funcall lang-f))
208 (set (make-local-variable 'org-edit-src-force-single-line) single)
209 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
210 (when lfmt
211 (set (make-local-variable 'org-coderef-label-format) lfmt))
212 (when org-mode-p
213 (goto-char (point-min))
214 (while (re-search-forward "^," nil t)
215 (replace-match "")))
216 (org-goto-line (1+ (- line begline)))
217 (org-set-local 'org-edit-src-beg-marker beg)
218 (org-set-local 'org-edit-src-end-marker end)
219 (org-set-local 'org-edit-src-overlay ovl)
220 (org-set-local 'org-edit-src-nindent nindent)
221 (org-src-mode)
222 (set-buffer-modified-p nil)
223 (and org-edit-src-persistent-message
224 (org-set-local 'header-line-format msg)))
225 (message "%s" msg)
226 t)))
228 (defun org-edit-src-continue (e)
229 (interactive "e")
230 (mouse-set-point e)
231 (let ((buf (get-char-property (point) 'edit-buffer)))
232 (if buf (switch-to-buffer buf)
233 (error "Something is wrong here"))))
235 (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
236 "Construct the buffer name for a source editing buffer"
237 (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
239 (defun org-edit-src-find-buffer (beg end)
240 "Find a source editing buffer that is already editing the region BEG to END."
241 (catch 'exit
242 (mapc
243 (lambda (b)
244 (with-current-buffer b
245 (if (and (string-match "\\`*Org Src " (buffer-name))
246 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
247 (local-variable-p 'org-edit-src-end-marker (current-buffer))
248 (equal beg org-edit-src-beg-marker)
249 (equal end org-edit-src-end-marker))
250 (throw 'exit (current-buffer)))))
251 (buffer-list))
252 nil))
254 (defun org-edit-fixed-width-region ()
255 "Edit the fixed-width ascii drawing at point.
256 This must be a region where each line starts with a colon followed by
257 a space character.
258 An new buffer is created and the fixed-width region is copied into it,
259 and the buffer is switched into `artist-mode' for editing. When done,
260 exit with \\[org-edit-src-exit]. The edited text will then replace
261 the fragment in the Org-mode buffer."
262 (interactive)
263 (let ((line (org-current-line))
264 (case-fold-search t)
265 (msg (substitute-command-keys
266 "Edit, then exit with C-c ' (C-c and single quote)"))
267 (org-mode-p (eq major-mode 'org-mode))
268 (beg (make-marker))
269 (end (make-marker))
270 nindent ovl beg1 end1 code begline buffer)
271 (beginning-of-line 1)
272 (if (looking-at "[ \t]*[^:\n \t]")
274 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
275 (setq beg1 (point) end1 beg1)
276 (save-excursion
277 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
278 (setq beg1 (point-at-bol 2))
279 (setq beg1 (point))))
280 (save-excursion
281 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
282 (setq end1 (1- (match-beginning 0)))
283 (setq end1 (point))))
284 (org-goto-line line))
285 (setq beg (move-marker beg beg1)
286 end (move-marker end end1)
287 code (buffer-substring-no-properties beg end)
288 begline (save-excursion (goto-char beg) (org-current-line)))
289 (if (and (setq buffer (org-edit-src-find-buffer beg end))
290 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
291 (switch-to-buffer buffer)
292 (when buffer
293 (with-current-buffer buffer
294 (if (boundp 'org-edit-src-overlay)
295 (org-delete-overlay org-edit-src-overlay)))
296 (kill-buffer buffer))
297 (setq buffer (generate-new-buffer
298 (org-src-construct-edit-buffer-name
299 (buffer-name) "Fixed Width")))
300 (setq ovl (org-make-overlay beg end))
301 (org-overlay-put ovl 'face 'secondary-selection)
302 (org-overlay-put ovl 'edit-buffer buffer)
303 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
304 (org-overlay-put ovl 'face 'secondary-selection)
305 (org-overlay-put ovl
306 'keymap
307 (let ((map (make-sparse-keymap)))
308 (define-key map [mouse-1] 'org-edit-src-continue)
309 map))
310 (org-overlay-put ovl :read-only "Leave me alone")
311 (switch-to-buffer buffer)
312 (insert code)
313 (remove-text-properties (point-min) (point-max)
314 '(display nil invisible nil intangible nil))
315 (setq nindent (org-do-remove-indentation))
316 (cond
317 ((eq org-edit-fixed-width-region-mode 'artist-mode)
318 (fundamental-mode)
319 (artist-mode 1))
320 (t (funcall org-edit-fixed-width-region-mode)))
321 (set (make-local-variable 'org-edit-src-force-single-line) nil)
322 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
323 (set (make-local-variable 'org-edit-src-picture) t)
324 (goto-char (point-min))
325 (while (re-search-forward "^[ \t]*: ?" nil t)
326 (replace-match ""))
327 (org-goto-line (1+ (- line begline)))
328 (org-set-local 'org-edit-src-beg-marker beg)
329 (org-set-local 'org-edit-src-end-marker end)
330 (org-set-local 'org-edit-src-overlay ovl)
331 (org-set-local 'org-edit-src-nindent nindent)
332 (org-src-mode)
333 (set-buffer-modified-p nil)
334 (and org-edit-src-persistent-message
335 (org-set-local 'header-line-format msg)))
336 (message "%s" msg)
337 t)))
339 (defun org-edit-src-find-region-and-lang ()
340 "Find the region and language for a local edit.
341 Return a list with beginning and end of the region, a string representing
342 the language, a switch telling of the content should be in a single line."
343 (let ((re-list
344 (append
345 org-edit-src-region-extra
347 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
348 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
349 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
350 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
351 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
352 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
353 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
354 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
355 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
356 ("^[ \t]*#\\+html:" "\n" "html" single-line)
357 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
358 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
359 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
360 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
361 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
362 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
363 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
365 (pos (point))
366 re1 re2 single beg end lang lfmt match-re1 ind entry)
367 (catch 'exit
368 (while (setq entry (pop re-list))
369 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
370 single (nth 3 entry))
371 (save-excursion
372 (if (or (looking-at re1)
373 (re-search-backward re1 nil t))
374 (progn
375 (setq match-re1 (match-string 0))
376 (setq beg (match-end 0)
377 lang (org-edit-src-get-lang lang)
378 lfmt (org-edit-src-get-label-format match-re1)
379 ind (org-edit-src-get-indentation (match-beginning 0)))
380 (if (and (re-search-forward re2 nil t)
381 (>= (match-end 0) pos))
382 (throw 'exit (list beg (match-beginning 0)
383 lang single lfmt ind))))
384 (if (or (looking-at re2)
385 (re-search-forward re2 nil t))
386 (progn
387 (setq end (match-beginning 0))
388 (if (and (re-search-backward re1 nil t)
389 (<= (match-beginning 0) pos))
390 (progn
391 (setq lfmt (org-edit-src-get-label-format
392 (match-string 0))
393 ind (org-edit-src-get-indentation
394 (match-beginning 0)))
395 (throw 'exit
396 (list (match-end 0) end
397 (org-edit-src-get-lang lang)
398 single lfmt ind))))))))))))
400 (defun org-edit-src-get-lang (lang)
401 "Extract the src language."
402 (let ((m (match-string 0)))
403 (cond
404 ((stringp lang) lang)
405 ((integerp lang) (match-string lang))
406 ((and (eq lang 'lang)
407 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
408 (match-string 1 m))
409 ((and (eq lang 'style)
410 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
411 (match-string 1 m))
412 (t "fundamental"))))
414 (defun org-edit-src-get-label-format (s)
415 "Extract the label format."
416 (save-match-data
417 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
418 (match-string 1 s))))
420 (defun org-edit-src-get-indentation (pos)
421 "Extract the label format."
422 (save-match-data
423 (goto-char pos)
424 (org-get-indentation)))
426 (defun org-edit-src-exit ()
427 "Exit special edit and protect problematic lines."
428 (interactive)
429 (unless org-edit-src-from-org-mode
430 (error "This is not a sub-editing buffer, something is wrong..."))
431 (let ((beg org-edit-src-beg-marker)
432 (end org-edit-src-end-marker)
433 (ovl org-edit-src-overlay)
434 (buffer (current-buffer))
435 (nindent org-edit-src-nindent)
436 code line)
437 (untabify (point-min) (point-max))
438 (save-excursion
439 (goto-char (point-min))
440 (if (looking-at "[ \t\n]*\n") (replace-match ""))
441 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))
442 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
444 (org-current-line)))
445 (when (org-bound-and-true-p org-edit-src-force-single-line)
446 (goto-char (point-min))
447 (while (re-search-forward "\n" nil t)
448 (replace-match " "))
449 (goto-char (point-min))
450 (if (looking-at "\\s-*") (replace-match " "))
451 (if (re-search-forward "\\s-+\\'" nil t)
452 (replace-match "")))
453 (when (org-bound-and-true-p org-edit-src-from-org-mode)
454 (goto-char (point-min))
455 (while (re-search-forward
456 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
457 (replace-match ",\\1")))
458 (when (org-bound-and-true-p org-edit-src-picture)
459 (untabify (point-min) (point-max))
460 (goto-char (point-min))
461 (while (re-search-forward "^" nil t)
462 (replace-match ": ")))
463 (when nindent
464 (setq nindent (make-string (+ org-edit-src-content-indentation nindent)
465 ?\ ))
466 (goto-char (point-min))
467 (while (re-search-forward "^" nil t)
468 (replace-match nindent)))
469 (setq code (buffer-string))
470 (set-buffer-modified-p nil)
471 (switch-to-buffer (marker-buffer beg))
472 (kill-buffer buffer)
473 (goto-char beg)
474 (delete-region beg end)
475 (insert code)
476 (goto-char beg)
477 (org-goto-line (1- (+ (org-current-line) line)))
478 (move-marker beg nil)
479 (move-marker end nil)))
481 (defun org-edit-src-save ()
482 "Save parent buffer with current state source-code buffer."
483 (interactive)
484 (save-window-excursion
485 (let ((p (point)) (m (mark)) msg)
486 (org-edit-src-exit)
487 (save-buffer)
488 (setq msg (current-message))
489 (org-edit-src-code)
490 (push-mark m 'nomessage)
491 (goto-char (min p (point-max)))
492 (message (or msg "")))))
494 (defun org-src-mode-configure-edit-buffer ()
495 (when org-edit-src-from-org-mode
496 (setq buffer-offer-save t)
497 (setq buffer-file-name
498 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
499 "[" (buffer-name) "]"))
500 (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
501 '(org-edit-src-save))
502 (org-add-hook 'kill-buffer-hook
503 '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
505 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
507 (provide 'org-src)
509 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
510 ;;; org-src.el ends here