Minor fixes
[org-mode.git] / lisp / org-src.el
blob65b72c1a0108e4748605f67b49bec461aa248730
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.27trans
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))
39 (defcustom org-edit-src-region-extra nil
40 "Additional regexps to identify regions for editing with `org-edit-src-code'.
41 For examples see the function `org-edit-src-find-region-and-lang'.
42 The regular expression identifying the begin marker should end with a newline,
43 and the regexp marking the end line should start with a newline, to make sure
44 there are kept outside the narrowed region."
45 :group 'org-edit-structure
46 :type '(repeat
47 (list
48 (regexp :tag "begin regexp")
49 (regexp :tag "end regexp")
50 (choice :tag "language"
51 (string :tag "specify")
52 (integer :tag "from match group")
53 (const :tag "from `lang' element")
54 (const :tag "from `style' element")))))
56 (defcustom org-coderef-label-format "(ref:%s)"
57 "The default coderef format.
58 This format string will be used to search for coderef labels in literal
59 examples (EXAMPLE and SRC blocks). The format can be overwritten
60 an individual literal example with the -f option, like
62 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
63 ...
64 #+END_SRC
66 If you want to use this for HTML export, make sure that the format does
67 not introduce special font-locking, and avoid the HTML special
68 characters `<', `>', and `&'. The reason for this restriction is that
69 the labels are searched for only after htmlize has done its job."
70 :group 'org-edit-structure ; FIXME this is not in the right group
71 :type 'string)
73 (defcustom org-edit-fixed-width-region-mode 'artist-mode
74 "The mode that should be used to edit fixed-width regions.
75 These are the regions where each line starts with a colon."
76 :group 'org-edit-structure
77 :type '(choice
78 (const artist-mode)
79 (const picture-mode)
80 (const fundamental-mode)
81 (function :tag "Other (specify)")))
83 (defcustom org-edit-src-content-indentation 2
84 "Indentation for the content is a source code block.
85 This should be the number of spaces added to the indentation of the #+begin
86 line in order to compute the indentation of the block content after
87 editing it with \\[org-edit-src-code]."
88 :group 'org-edit-structure
89 :type 'integer)
91 (defcustom org-edit-src-persistent-message t
92 "Non-nil means show persistent exit help message while editing src examples.
93 The message is shown in the header-line, which will be created in the
94 first line of the window showing the editing buffer.
95 When nil, the message will only be shown intermittently in the echo area."
96 :group 'org-edit-structure
97 :type 'boolean)
100 (defvar org-src-mode-hook nil
101 "Hook run after Org switched a source code snippet to its Emacs mode.
102 This hook will run
104 - when editing a source code snippet with \"C-c '\".
105 - When formatting a source code snippet for export with htmlize.
107 You may want to use this hook for example to turn off `outline-minor-mode'
108 or similar things which you want to have when editing a source code file,
109 but which mess up the display of a snippet in Org exported files.")
111 (defvar org-protecting-blocks
112 '("src" "example" "latex" "ascii" "html" "docbook")
113 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
114 This is needed for font-lock setup.")
117 ;;; Editing source examples
119 (defvar org-src-mode-map (make-sparse-keymap))
120 (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
121 (define-key org-src-mode-map "\C-x\C-s" 'org-edit-src-save)
122 (defvar org-edit-src-force-single-line nil)
123 (defvar org-edit-src-from-org-mode nil)
124 (defvar org-edit-src-picture nil)
125 (defvar org-edit-src-beg-marker nil)
126 (defvar org-edit-src-end-marker nil)
127 (defvar org-edit-src-overlay nil)
128 (defvar org-edit-src-nindent nil)
130 (define-minor-mode org-src-mode
131 "Minor mode for language major mode buffers generated by org.
132 This minor mode is turned on in two situations:
133 - when editing a source code snippet with \"C-c '\".
134 - When formatting a source code snippet for export with htmlize.
135 There is a mode hook, and keybindings for `org-edit-src-exit' and
136 `org-edit-src-save'")
138 (defun org-edit-src-code ()
139 "Edit the source code example at point.
140 The example is copied to a separate buffer, and that buffer is switched
141 to the correct language mode. When done, exit with \\[org-edit-src-exit].
142 This will remove the original code in the Org buffer, and replace it with
143 the edited version."
144 (interactive)
145 (let ((line (org-current-line))
146 (case-fold-search t)
147 (msg (substitute-command-keys
148 "Edit, then exit with C-c ' (C-c and single quote)"))
149 (info (org-edit-src-find-region-and-lang))
150 (org-mode-p (eq major-mode 'org-mode))
151 (beg (make-marker))
152 (end (make-marker))
153 nindent ovl lang lang-f single lfmt code begline buffer)
154 (if (not info)
156 (setq beg (move-marker beg (nth 0 info))
157 end (move-marker end (nth 1 info))
158 code (buffer-substring-no-properties beg end)
159 lang (nth 2 info)
160 single (nth 3 info)
161 lfmt (nth 4 info)
162 nindent (nth 5 info)
163 lang-f (intern (concat lang "-mode"))
164 begline (save-excursion (goto-char beg) (org-current-line)))
165 (unless (functionp lang-f)
166 (error "No such language mode: %s" lang-f))
167 (goto-line line)
168 (if (and (setq buffer (org-edit-src-find-buffer beg end))
169 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
170 (switch-to-buffer buffer)
171 (when buffer
172 (with-current-buffer buffer
173 (if (boundp 'org-edit-src-overlay)
174 (org-delete-overlay org-edit-src-overlay)))
175 (kill-buffer buffer))
176 (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
177 (setq ovl (org-make-overlay beg end))
178 (org-overlay-put ovl 'face 'secondary-selection)
179 (org-overlay-put ovl 'edit-buffer buffer)
180 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
181 (org-overlay-put ovl 'face 'secondary-selection)
182 (org-overlay-put ovl
183 'keymap
184 (let ((map (make-sparse-keymap)))
185 (define-key map [mouse-1] 'org-edit-src-continue)
186 map))
187 (org-overlay-put ovl :read-only "Leave me alone")
188 (switch-to-buffer buffer)
189 (insert code)
190 (remove-text-properties (point-min) (point-max)
191 '(display nil invisible nil intangible nil))
192 (org-do-remove-indentation)
193 (let ((org-inhibit-startup t))
194 (funcall lang-f)
195 (org-src-mode))
196 (set (make-local-variable 'org-edit-src-force-single-line) single)
197 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
198 (when lfmt
199 (set (make-local-variable 'org-coderef-label-format) lfmt))
200 (when org-mode-p
201 (goto-char (point-min))
202 (while (re-search-forward "^," nil t)
203 (replace-match "")))
204 (goto-line (1+ (- line begline)))
205 (org-set-local 'org-edit-src-beg-marker beg)
206 (org-set-local 'org-edit-src-end-marker end)
207 (org-set-local 'org-edit-src-overlay ovl)
208 (org-set-local 'org-edit-src-nindent nindent)
209 (and org-edit-src-persistent-message
210 (org-set-local 'header-line-format msg)))
211 (message "%s" msg)
212 t)))
214 (defun org-edit-src-continue (e)
215 (interactive "e")
216 (mouse-set-point e)
217 (let ((buf (get-char-property (point) 'edit-buffer)))
218 (if buf (switch-to-buffer buf)
219 (error "Something is wrong here"))))
221 (defun org-edit-src-find-buffer (beg end)
222 "Find a source editing buffer that is already editing the region BEG to END."
223 (catch 'exit
224 (mapc
225 (lambda (b)
226 (with-current-buffer b
227 (if (and (string-match "\\`*Org Edit " (buffer-name))
228 (local-variable-p 'org-edit-src-beg-marker (current-buffer))
229 (local-variable-p 'org-edit-src-end-marker (current-buffer))
230 (equal beg org-edit-src-beg-marker)
231 (equal end org-edit-src-end-marker))
232 (throw 'exit (current-buffer)))))
233 (buffer-list))
234 nil))
236 (defun org-edit-fixed-width-region ()
237 "Edit the fixed-width ascii drawing at point.
238 This must be a region where each line starts with a colon followed by
239 a space character.
240 An new buffer is created and the fixed-width region is copied into it,
241 and the buffer is switched into `artist-mode' for editing. When done,
242 exit with \\[org-edit-src-exit]. The edited text will then replace
243 the fragment in the Org-mode buffer."
244 (interactive)
245 (let ((line (org-current-line))
246 (case-fold-search t)
247 (msg (substitute-command-keys
248 "Edit, then exit with C-c ' (C-c and single quote)"))
249 (org-mode-p (eq major-mode 'org-mode))
250 (beg (make-marker))
251 (end (make-marker))
252 nindent ovl beg1 end1 code begline buffer)
253 (beginning-of-line 1)
254 (if (looking-at "[ \t]*[^:\n \t]")
256 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
257 (setq beg1 (point) end1 beg1)
258 (save-excursion
259 (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
260 (setq beg1 (point-at-bol 2))
261 (setq beg1 (point))))
262 (save-excursion
263 (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
264 (setq end1 (1- (match-beginning 0)))
265 (setq end1 (point))))
266 (goto-line line))
267 (setq beg (move-marker beg beg1)
268 end (move-marker end end1)
269 code (buffer-substring-no-properties beg end)
270 begline (save-excursion (goto-char beg) (org-current-line)))
271 (if (and (setq buffer (org-edit-src-find-buffer beg end))
272 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
273 (switch-to-buffer buffer)
274 (when buffer
275 (with-current-buffer buffer
276 (if (boundp 'org-edit-src-overlay)
277 (org-delete-overlay org-edit-src-overlay)))
278 (kill-buffer buffer))
279 (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
280 (setq ovl (org-make-overlay beg end))
281 (org-overlay-put ovl 'face 'secondary-selection)
282 (org-overlay-put ovl 'edit-buffer buffer)
283 (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
284 (org-overlay-put ovl 'face 'secondary-selection)
285 (org-overlay-put ovl
286 'keymap
287 (let ((map (make-sparse-keymap)))
288 (define-key map [mouse-1] 'org-edit-src-continue)
289 map))
290 (org-overlay-put ovl :read-only "Leave me alone")
291 (switch-to-buffer buffer)
292 (insert code)
293 (remove-text-properties (point-min) (point-max)
294 '(display nil invisible nil intangible nil))
295 (setq nindent (org-do-remove-indentation))
296 (cond
297 ((eq org-edit-fixed-width-region-mode 'artist-mode)
298 (fundamental-mode)
299 (artist-mode 1))
300 (t (funcall org-edit-fixed-width-region-mode)))
301 (set (make-local-variable 'org-edit-src-force-single-line) nil)
302 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
303 (set (make-local-variable 'org-edit-src-picture) t)
304 (goto-char (point-min))
305 (while (re-search-forward "^[ \t]*: ?" nil t)
306 (replace-match ""))
307 (goto-line (1+ (- line begline)))
308 (org-src-mode)
309 (org-set-local 'org-edit-src-beg-marker beg)
310 (org-set-local 'org-edit-src-end-marker end)
311 (org-set-local 'org-edit-src-overlay ovl)
312 (org-set-local 'org-edit-src-nindent nindent)
313 (and org-edit-src-persistent-message
314 (org-set-local 'header-line-format msg)))
315 (message "%s" msg)
316 t)))
318 (defun org-edit-src-find-region-and-lang ()
319 "Find the region and language for a local edit.
320 Return a list with beginning and end of the region, a string representing
321 the language, a switch telling of the content should be in a single line."
322 (let ((re-list
323 (append
324 org-edit-src-region-extra
326 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
327 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
328 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
329 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
330 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
331 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
332 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
333 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
334 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
335 ("^[ \t]*#\\+html:" "\n" "html" single-line)
336 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
337 ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
338 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
339 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
340 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
341 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
342 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
344 (pos (point))
345 re1 re2 single beg end lang lfmt match-re1 ind entry)
346 (catch 'exit
347 (while (setq entry (pop re-list))
348 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
349 single (nth 3 entry))
350 (save-excursion
351 (if (or (looking-at re1)
352 (re-search-backward re1 nil t))
353 (progn
354 (setq match-re1 (match-string 0))
355 (setq beg (match-end 0)
356 lang (org-edit-src-get-lang lang)
357 lfmt (org-edit-src-get-label-format match-re1)
358 ind (org-edit-src-get-indentation (match-beginning 0)))
359 (if (and (re-search-forward re2 nil t)
360 (>= (match-end 0) pos))
361 (throw 'exit (list beg (match-beginning 0)
362 lang single lfmt ind))))
363 (if (or (looking-at re2)
364 (re-search-forward re2 nil t))
365 (progn
366 (setq end (match-beginning 0))
367 (if (and (re-search-backward re1 nil t)
368 (<= (match-beginning 0) pos))
369 (progn
370 (setq lfmt (org-edit-src-get-label-format
371 (match-string 0))
372 ind (org-edit-src-get-indentation
373 (match-beginning 0)))
374 (throw 'exit
375 (list (match-end 0) end
376 (org-edit-src-get-lang lang)
377 single lfmt ind))))))))))))
379 (defun org-edit-src-get-lang (lang)
380 "Extract the src language."
381 (let ((m (match-string 0)))
382 (cond
383 ((stringp lang) lang)
384 ((integerp lang) (match-string lang))
385 ((and (eq lang 'lang)
386 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
387 (match-string 1 m))
388 ((and (eq lang 'style)
389 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
390 (match-string 1 m))
391 (t "fundamental"))))
393 (defun org-edit-src-get-label-format (s)
394 "Extract the label format."
395 (save-match-data
396 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
397 (match-string 1 s))))
399 (defun org-edit-src-get-indentation (pos)
400 "Extract the label format."
401 (save-match-data
402 (goto-char pos)
403 (org-get-indentation)))
405 (defun org-edit-src-exit ()
406 "Exit special edit and protect problematic lines."
407 (interactive)
408 (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
409 (error "This is not an sub-editing buffer, something is wrong..."))
410 (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
412 (org-current-line)))
413 (beg org-edit-src-beg-marker)
414 (end org-edit-src-end-marker)
415 (ovl org-edit-src-overlay)
416 (buffer (current-buffer))
417 (nindent org-edit-src-nindent)
418 code)
419 (goto-char (point-min))
420 (if (looking-at "[ \t\n]*\n") (replace-match ""))
421 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
422 (when (org-bound-and-true-p org-edit-src-force-single-line)
423 (goto-char (point-min))
424 (while (re-search-forward "\n" nil t)
425 (replace-match " "))
426 (goto-char (point-min))
427 (if (looking-at "\\s-*") (replace-match " "))
428 (if (re-search-forward "\\s-+\\'" nil t)
429 (replace-match "")))
430 (when (org-bound-and-true-p org-edit-src-from-org-mode)
431 (goto-char (point-min))
432 (while (re-search-forward
433 (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
434 (replace-match ",\\1")))
435 (when (org-bound-and-true-p org-edit-src-picture)
436 (untabify (point-min) (point-max))
437 (goto-char (point-min))
438 (while (re-search-forward "^" nil t)
439 (replace-match ": ")))
440 (when nindent
441 (setq nindent (make-string (+ org-edit-src-content-indentation nindent)
442 ?\ ))
443 (goto-char (point-min))
444 (while (re-search-forward "^" nil t)
445 (replace-match nindent)))
446 (setq code (buffer-string))
447 (switch-to-buffer (marker-buffer beg))
448 (kill-buffer buffer)
449 (goto-char beg)
450 (org-delete-overlay ovl)
451 (delete-region beg end)
452 (insert code)
453 (goto-char beg)
454 (goto-line (1- (+ (org-current-line) line)))
455 (move-marker beg nil)
456 (move-marker end nil)))
458 (defun org-edit-src-save ()
459 "Save parent buffer with current state source-code buffer."
460 (interactive)
461 (let ((p (point)) (m (mark)) msg)
462 (org-edit-src-exit)
463 (save-buffer)
464 (setq msg (current-message))
465 (org-edit-src-code)
466 (push-mark m 'nomessage)
467 (goto-char (min p (point-max)))
468 (message (or msg ""))))
470 (provide 'org-src)
472 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
474 ;;; org-src.el ends here