1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This file contains the code dealing with source code examples in Org-mode.
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
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))\"
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
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
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
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
100 (defvar org-src-mode-hook nil
101 "Hook run after Org switched a source code snippet to its Emacs mode.
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-exit-edit-mode-map
(make-sparse-keymap))
120 (define-key org-exit-edit-mode-map
"\C-c'" 'org-edit-src-exit
)
121 (define-key org-exit-edit-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-exit-edit-mode
131 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
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
140 (let ((line (org-current-line))
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
))
148 nindent ovl lang lang-f single lfmt code begline buffer
)
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
)
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
))
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
)
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
)
179 (let ((map (make-sparse-keymap)))
180 (define-key map
[mouse-1
] 'org-edit-src-continue
)
182 (org-overlay-put ovl
:read-only
"Leave me alone")
183 (switch-to-buffer buffer
)
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
))
190 (run-hooks 'org-src-mode-hook
))
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
)
194 (set (make-local-variable 'org-coderef-label-format
) lfmt
))
196 (goto-char (point-min))
197 (while (re-search-forward "^," nil t
)
199 (goto-line (1+ (- line begline
)))
201 (org-set-local 'org-edit-src-beg-marker beg
)
202 (org-set-local 'org-edit-src-end-marker end
)
203 (org-set-local 'org-edit-src-overlay ovl
)
204 (org-set-local 'org-edit-src-nindent nindent
)
205 (and org-edit-src-persistent-message
206 (org-set-local 'header-line-format msg
)))
210 (defun org-edit-src-continue (e)
213 (let ((buf (get-char-property (point) 'edit-buffer
)))
214 (if buf
(switch-to-buffer buf
)
215 (error "Something is wrong here"))))
217 (defun org-edit-src-find-buffer (beg end
)
218 "Find a source editing buffer that is already editing the region BEG to END."
222 (with-current-buffer b
223 (if (and (string-match "\\`*Org Edit " (buffer-name))
224 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
225 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
226 (equal beg org-edit-src-beg-marker
)
227 (equal end org-edit-src-end-marker
))
228 (throw 'exit
(current-buffer)))))
232 (defun org-edit-fixed-width-region ()
233 "Edit the fixed-width ascii drawing at point.
234 This must be a region where each line starts with a colon followed by
236 An new buffer is created and the fixed-width region is copied into it,
237 and the buffer is switched into `artist-mode' for editing. When done,
238 exit with \\[org-edit-src-exit]. The edited text will then replace
239 the fragment in the Org-mode buffer."
241 (let ((line (org-current-line))
243 (msg (substitute-command-keys
244 "Edit, then exit with C-c ' (C-c and single quote)"))
245 (org-mode-p (eq major-mode
'org-mode
))
248 nindent ovl beg1 end1 code begline buffer
)
249 (beginning-of-line 1)
250 (if (looking-at "[ \t]*[^:\n \t]")
252 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
253 (setq beg1
(point) end1 beg1
)
255 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
256 (setq beg1
(point-at-bol 2))
257 (setq beg1
(point))))
259 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
260 (setq end1
(1- (match-beginning 0)))
261 (setq end1
(point))))
263 (setq beg
(move-marker beg beg1
)
264 end
(move-marker end end1
)
265 code
(buffer-substring-no-properties beg end
)
266 begline
(save-excursion (goto-char beg
) (org-current-line)))
267 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
268 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
269 (switch-to-buffer buffer
)
271 (with-current-buffer buffer
272 (if (boundp 'org-edit-src-overlay
)
273 (org-delete-overlay org-edit-src-overlay
)))
274 (kill-buffer buffer
))
275 (setq buffer
(generate-new-buffer "*Org Edit Src Example*"))
276 (setq ovl
(org-make-overlay beg end
))
277 (org-overlay-put ovl
'face
'secondary-selection
)
278 (org-overlay-put ovl
'edit-buffer buffer
)
279 (org-overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
280 (org-overlay-put ovl
'face
'secondary-selection
)
283 (let ((map (make-sparse-keymap)))
284 (define-key map
[mouse-1
] 'org-edit-src-continue
)
286 (org-overlay-put ovl
:read-only
"Leave me alone")
287 (switch-to-buffer buffer
)
289 (remove-text-properties (point-min) (point-max)
290 '(display nil invisible nil intangible nil
))
291 (setq nindent
(org-do-remove-indentation))
293 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
296 (t (funcall org-edit-fixed-width-region-mode
)))
297 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
298 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
299 (set (make-local-variable 'org-edit-src-picture
) t
)
300 (goto-char (point-min))
301 (while (re-search-forward "^[ \t]*: ?" nil t
)
303 (goto-line (1+ (- line begline
)))
305 (org-set-local 'org-edit-src-beg-marker beg
)
306 (org-set-local 'org-edit-src-end-marker end
)
307 (org-set-local 'org-edit-src-overlay ovl
)
308 (org-set-local 'org-edit-src-nindent nindent
)
309 (and org-edit-src-persistent-message
310 (org-set-local 'header-line-format msg
)))
314 (defun org-edit-src-find-region-and-lang ()
315 "Find the region and language for a local edit.
316 Return a list with beginning and end of the region, a string representing
317 the language, a switch telling of the content should be in a single line."
320 org-edit-src-region-extra
322 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
323 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
324 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
325 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
326 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
327 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
328 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
329 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
330 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
331 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
332 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
333 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
334 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
335 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
336 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
337 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
338 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
341 re1 re2 single beg end lang lfmt match-re1 ind entry
)
343 (while (setq entry
(pop re-list
))
344 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
345 single
(nth 3 entry
))
347 (if (or (looking-at re1
)
348 (re-search-backward re1 nil t
))
350 (setq match-re1
(match-string 0))
351 (setq beg
(match-end 0)
352 lang
(org-edit-src-get-lang lang
)
353 lfmt
(org-edit-src-get-label-format match-re1
)
354 ind
(org-edit-src-get-indentation (match-beginning 0)))
355 (if (and (re-search-forward re2 nil t
)
356 (>= (match-end 0) pos
))
357 (throw 'exit
(list beg
(match-beginning 0)
358 lang single lfmt ind
))))
359 (if (or (looking-at re2
)
360 (re-search-forward re2 nil t
))
362 (setq end
(match-beginning 0))
363 (if (and (re-search-backward re1 nil t
)
364 (<= (match-beginning 0) pos
))
366 (setq lfmt
(org-edit-src-get-label-format
368 ind
(org-edit-src-get-indentation
369 (match-beginning 0)))
371 (list (match-end 0) end
372 (org-edit-src-get-lang lang
)
373 single lfmt ind
))))))))))))
375 (defun org-edit-src-get-lang (lang)
376 "Extract the src language."
377 (let ((m (match-string 0)))
379 ((stringp lang
) lang
)
380 ((integerp lang
) (match-string lang
))
381 ((and (eq lang
'lang
)
382 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
384 ((and (eq lang
'style
)
385 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
389 (defun org-edit-src-get-label-format (s)
390 "Extract the label format."
392 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
393 (match-string 1 s
))))
395 (defun org-edit-src-get-indentation (pos)
396 "Extract the label format."
399 (org-get-indentation)))
401 (defun org-edit-src-exit ()
402 "Exit special edit and protect problematic lines."
404 (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
405 (error "This is not an sub-editing buffer, something is wrong..."))
406 (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line
)
409 (beg org-edit-src-beg-marker
)
410 (end org-edit-src-end-marker
)
411 (ovl org-edit-src-overlay
)
412 (buffer (current-buffer))
413 (nindent org-edit-src-nindent
)
415 (goto-char (point-min))
416 (if (looking-at "[ \t\n]*\n") (replace-match ""))
417 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))
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
)
422 (goto-char (point-min))
423 (if (looking-at "\\s-*") (replace-match " "))
424 (if (re-search-forward "\\s-+\\'" nil t
)
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 ": ")))
437 (setq nindent
(make-string (+ org-edit-src-content-indentation nindent
)
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
))
446 (org-delete-overlay ovl
)
447 (delete-region beg end
)
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."
457 (let ((p (point)) (m (mark)) msg
)
460 (setq msg
(current-message))
462 (push-mark m
'nomessage
)
463 (goto-char (min p
(point-max)))
464 (message (or msg
""))))
468 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
470 ;;; org-src.el ends here