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