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