Release 6.30c
[org-mode/org-tableheadings.git] / lisp / org-src.el
blobb7536b211528ef0b2dd5eb18b9de33b64acefd0b
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.30c
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 "Alist mapping languages to their major mode.
115 The key is the language name, the value is the string that should
116 be inserted as the name of the major mode. For many languages this is
117 simple, but for language where this is not the case, this variable
118 provides a way to simplify things on the user side.
119 For example, there is no ocaml-mode in Emacs, but the mode to use is
120 `tuareg-mode'."
121 :group 'org-edit-structure
122 :type '(repeat
123 (cons
124 (string "Language name")
125 (symbol "Major mode"))))
127 ;;; Editing source examples
129 (defvar org-src-mode-map (make-sparse-keymap))
130 (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
131 (defvar org-edit-src-force-single-line nil)
132 (defvar org-edit-src-from-org-mode nil)
133 (defvar org-edit-src-picture nil)
134 (defvar org-edit-src-beg-marker nil)
135 (defvar org-edit-src-end-marker nil)
136 (defvar org-edit-src-overlay nil)
137 (defvar org-edit-src-nindent nil)
139 (define-minor-mode org-src-mode
140 "Minor mode for language major mode buffers generated by org.
141 This minor mode is turned on in two situations:
142 - when editing a source code snippet with \"C-c '\".
143 - When formatting a source code snippet for export with htmlize.
144 There is a mode hook, and keybindings for `org-edit-src-exit' and
145 `org-edit-src-save'")
147 (defun org-edit-src-code ()
148 "Edit the source code example at point.
149 The example is copied to a separate buffer, and that buffer is switched
150 to the correct language mode. When done, exit with \\[org-edit-src-exit].
151 This will remove the original code in the Org buffer, and replace it with
152 the edited version."
153 (interactive)
154 (let ((line (org-current-line))
155 (case-fold-search t)
156 (msg (substitute-command-keys
157 "Edit, then exit with C-c ' (C-c and single quote)"))
158 (info (org-edit-src-find-region-and-lang))
159 (org-mode-p (eq major-mode 'org-mode))
160 (beg (make-marker))
161 (end (make-marker))
162 nindent ovl lang lang-f single lfmt code begline buffer)
163 (if (not info)
165 (setq beg (move-marker beg (nth 0 info))
166 end (move-marker end (nth 1 info))
167 code (buffer-substring-no-properties beg end)
168 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
169 (nth 2 info))
170 lang (if (symbolp lang) (symbol-name lang) lang)
171 single (nth 3 info)
172 lfmt (nth 4 info)
173 nindent (nth 5 info)
174 lang-f (intern (concat lang "-mode"))
175 begline (save-excursion (goto-char beg) (org-current-line)))
176 (unless (functionp lang-f)
177 (error "No such language mode: %s" lang-f))
178 (org-goto-line line)
179 (if (and (setq buffer (org-edit-src-find-buffer beg end))
180 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
181 (switch-to-buffer buffer)
182 (when buffer
183 (with-current-buffer buffer
184 (if (boundp 'org-edit-src-overlay)
185 (org-delete-overlay org-edit-src-overlay)))
186 (kill-buffer buffer))
187 (setq buffer (generate-new-buffer
188 (concat "*Org Src " (file-name-nondirectory buffer-file-name) "[" lang "]*")))
189 (setq ovl (org-make-overlay beg end))
190 (org-overlay-put ovl 'face 'secondary-selection)
191 (org-overlay-put ovl 'edit-buffer buffer)
192 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
193 (org-overlay-put ovl 'face 'secondary-selection)
194 (org-overlay-put ovl
195 'keymap
196 (let ((map (make-sparse-keymap)))
197 (define-key map [mouse-1] 'org-edit-src-continue)
198 map))
199 (org-overlay-put ovl :read-only "Leave me alone")
200 (switch-to-buffer buffer)
201 (insert code)
202 (remove-text-properties (point-min) (point-max)
203 '(display nil invisible nil intangible nil))
204 (org-do-remove-indentation)
205 (let ((org-inhibit-startup t))
206 (funcall lang-f))
207 (set (make-local-variable 'org-edit-src-force-single-line) single)
208 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
209 (when lfmt
210 (set (make-local-variable 'org-coderef-label-format) lfmt))
211 (when org-mode-p
212 (goto-char (point-min))
213 (while (re-search-forward "^," nil t)
214 (replace-match "")))
215 (org-goto-line (1+ (- line begline)))
216 (org-set-local 'org-edit-src-beg-marker beg)
217 (org-set-local 'org-edit-src-end-marker end)
218 (org-set-local 'org-edit-src-overlay ovl)
219 (org-set-local 'org-edit-src-nindent nindent)
220 (org-src-mode)
221 (set-buffer-modified-p nil)
222 (and org-edit-src-persistent-message
223 (org-set-local 'header-line-format msg)))
224 (message "%s" msg)
225 t)))
227 (defun org-edit-src-continue (e)
228 (interactive "e")
229 (mouse-set-point e)
230 (let ((buf (get-char-property (point) 'edit-buffer)))
231 (if buf (switch-to-buffer buf)
232 (error "Something is wrong here"))))
234 (defun org-edit-src-find-buffer (beg end)
235 "Find a source editing buffer that is already editing the region BEG to END."
236 (catch 'exit
237 (mapc
238 (lambda (b)
239 (with-current-buffer b
240 (if (and (string-match "\\`*Org Edit " (buffer-name))
241 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
242 (local-variable-p 'org-edit-src-end-marker (current-buffer))
243 (equal beg org-edit-src-beg-marker)
244 (equal end org-edit-src-end-marker))
245 (throw 'exit (current-buffer)))))
246 (buffer-list))
247 nil))
249 (defun org-edit-fixed-width-region ()
250 "Edit the fixed-width ascii drawing at point.
251 This must be a region where each line starts with a colon followed by
252 a space character.
253 An new buffer is created and the fixed-width region is copied into it,
254 and the buffer is switched into `artist-mode' for editing. When done,
255 exit with \\[org-edit-src-exit]. The edited text will then replace
256 the fragment in the Org-mode buffer."
257 (interactive)
258 (let ((line (org-current-line))
259 (case-fold-search t)
260 (msg (substitute-command-keys
261 "Edit, then exit with C-c ' (C-c and single quote)"))
262 (org-mode-p (eq major-mode 'org-mode))
263 (beg (make-marker))
264 (end (make-marker))
265 nindent ovl beg1 end1 code begline buffer)
266 (beginning-of-line 1)
267 (if (looking-at "[ \t]*[^:\n \t]")
269 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
270 (setq beg1 (point) end1 beg1)
271 (save-excursion
272 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
273 (setq beg1 (point-at-bol 2))
274 (setq beg1 (point))))
275 (save-excursion
276 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
277 (setq end1 (1- (match-beginning 0)))
278 (setq end1 (point))))
279 (org-goto-line line))
280 (setq beg (move-marker beg beg1)
281 end (move-marker end end1)
282 code (buffer-substring-no-properties beg end)
283 begline (save-excursion (goto-char beg) (org-current-line)))
284 (if (and (setq buffer (org-edit-src-find-buffer beg end))
285 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
286 (switch-to-buffer buffer)
287 (when buffer
288 (with-current-buffer buffer
289 (if (boundp 'org-edit-src-overlay)
290 (org-delete-overlay org-edit-src-overlay)))
291 (kill-buffer buffer))
292 (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
293 (setq ovl (org-make-overlay beg end))
294 (org-overlay-put ovl 'face 'secondary-selection)
295 (org-overlay-put ovl 'edit-buffer buffer)
296 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
297 (org-overlay-put ovl 'face 'secondary-selection)
298 (org-overlay-put ovl
299 'keymap
300 (let ((map (make-sparse-keymap)))
301 (define-key map [mouse-1] 'org-edit-src-continue)
302 map))
303 (org-overlay-put ovl :read-only "Leave me alone")
304 (switch-to-buffer buffer)
305 (insert code)
306 (remove-text-properties (point-min) (point-max)
307 '(display nil invisible nil intangible nil))
308 (setq nindent (org-do-remove-indentation))
309 (cond
310 ((eq org-edit-fixed-width-region-mode 'artist-mode)
311 (fundamental-mode)
312 (artist-mode 1))
313 (t (funcall org-edit-fixed-width-region-mode)))
314 (set (make-local-variable 'org-edit-src-force-single-line) nil)
315 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
316 (set (make-local-variable 'org-edit-src-picture) t)
317 (goto-char (point-min))
318 (while (re-search-forward "^[ \t]*: ?" nil t)
319 (replace-match ""))
320 (org-goto-line (1+ (- line begline)))
321 (org-set-local 'org-edit-src-beg-marker beg)
322 (org-set-local 'org-edit-src-end-marker end)
323 (org-set-local 'org-edit-src-overlay ovl)
324 (org-set-local 'org-edit-src-nindent nindent)
325 (org-src-mode)
326 (set-buffer-modified-p nil)
327 (and org-edit-src-persistent-message
328 (org-set-local 'header-line-format msg)))
329 (message "%s" msg)
330 t)))
332 (defun org-edit-src-find-region-and-lang ()
333 "Find the region and language for a local edit.
334 Return a list with beginning and end of the region, a string representing
335 the language, a switch telling of the content should be in a single line."
336 (let ((re-list
337 (append
338 org-edit-src-region-extra
340 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
341 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
342 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
343 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
344 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
345 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
346 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
347 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
348 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
349 ("^[ \t]*#\\+html:" "\n" "html" single-line)
350 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
351 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
352 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
353 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
354 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
355 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
356 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
358 (pos (point))
359 re1 re2 single beg end lang lfmt match-re1 ind entry)
360 (catch 'exit
361 (while (setq entry (pop re-list))
362 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
363 single (nth 3 entry))
364 (save-excursion
365 (if (or (looking-at re1)
366 (re-search-backward re1 nil t))
367 (progn
368 (setq match-re1 (match-string 0))
369 (setq beg (match-end 0)
370 lang (org-edit-src-get-lang lang)
371 lfmt (org-edit-src-get-label-format match-re1)
372 ind (org-edit-src-get-indentation (match-beginning 0)))
373 (if (and (re-search-forward re2 nil t)
374 (>= (match-end 0) pos))
375 (throw 'exit (list beg (match-beginning 0)
376 lang single lfmt ind))))
377 (if (or (looking-at re2)
378 (re-search-forward re2 nil t))
379 (progn
380 (setq end (match-beginning 0))
381 (if (and (re-search-backward re1 nil t)
382 (<= (match-beginning 0) pos))
383 (progn
384 (setq lfmt (org-edit-src-get-label-format
385 (match-string 0))
386 ind (org-edit-src-get-indentation
387 (match-beginning 0)))
388 (throw 'exit
389 (list (match-end 0) end
390 (org-edit-src-get-lang lang)
391 single lfmt ind))))))))))))
393 (defun org-edit-src-get-lang (lang)
394 "Extract the src language."
395 (let ((m (match-string 0)))
396 (cond
397 ((stringp lang) lang)
398 ((integerp lang) (match-string lang))
399 ((and (eq lang 'lang)
400 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
401 (match-string 1 m))
402 ((and (eq lang 'style)
403 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
404 (match-string 1 m))
405 (t "fundamental"))))
407 (defun org-edit-src-get-label-format (s)
408 "Extract the label format."
409 (save-match-data
410 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
411 (match-string 1 s))))
413 (defun org-edit-src-get-indentation (pos)
414 "Extract the label format."
415 (save-match-data
416 (goto-char pos)
417 (org-get-indentation)))
419 (defun org-edit-src-exit ()
420 "Exit special edit and protect problematic lines."
421 (interactive)
422 (unless org-edit-src-from-org-mode
423 (error "This is not a sub-editing buffer, something is wrong..."))
424 (let ((beg org-edit-src-beg-marker)
425 (end org-edit-src-end-marker)
426 (ovl org-edit-src-overlay)
427 (buffer (current-buffer))
428 (nindent org-edit-src-nindent)
429 code line)
430 (untabify (point-min) (point-max))
431 (save-excursion
432 (goto-char (point-min))
433 (if (looking-at "[ \t\n]*\n") (replace-match ""))
434 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))
435 (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
437 (org-current-line)))
438 (when (org-bound-and-true-p org-edit-src-force-single-line)
439 (goto-char (point-min))
440 (while (re-search-forward "\n" nil t)
441 (replace-match " "))
442 (goto-char (point-min))
443 (if (looking-at "\\s-*") (replace-match " "))
444 (if (re-search-forward "\\s-+\\'" nil t)
445 (replace-match "")))
446 (when (org-bound-and-true-p org-edit-src-from-org-mode)
447 (goto-char (point-min))
448 (while (re-search-forward
449 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
450 (replace-match ",\\1")))
451 (when (org-bound-and-true-p org-edit-src-picture)
452 (untabify (point-min) (point-max))
453 (goto-char (point-min))
454 (while (re-search-forward "^" nil t)
455 (replace-match ": ")))
456 (when nindent
457 (setq nindent (make-string (+ org-edit-src-content-indentation nindent)
458 ?\ ))
459 (goto-char (point-min))
460 (while (re-search-forward "^" nil t)
461 (replace-match nindent)))
462 (setq code (buffer-string))
463 (set-buffer-modified-p nil)
464 (switch-to-buffer (marker-buffer beg))
465 (kill-buffer buffer)
466 (goto-char beg)
467 (delete-region beg end)
468 (insert code)
469 (goto-char beg)
470 (org-goto-line (1- (+ (org-current-line) line)))
471 (move-marker beg nil)
472 (move-marker end nil)))
474 (defun org-edit-src-save ()
475 "Save parent buffer with current state source-code buffer."
476 (interactive)
477 (let ((p (point)) (m (mark)) msg)
478 (org-edit-src-exit)
479 (save-buffer)
480 (setq msg (current-message))
481 (org-edit-src-code)
482 (push-mark m 'nomessage)
483 (goto-char (min p (point-max)))
484 (message (or msg ""))))
486 (defun org-src-mode-configure-edit-buffer ()
487 (when org-edit-src-from-org-mode
488 (setq buffer-offer-save t)
489 (setq buffer-file-name
490 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
491 "[" (buffer-name) "]"))
492 (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
493 '(org-edit-src-save))
494 (org-add-hook 'kill-buffer-hook
495 '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
497 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
499 (provide 'org-src)
501 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
502 ;;; org-src.el ends here