1 ;;; org-src.el --- Source code examples in Org
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Bastien Guerry <bzg AT gnu DOT org>
7 ;; Dan Davison <davison at stats dot ox dot ac dot uk>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the code dealing with source code examples in Org-mode.
40 (declare-function org-do-remove-indentation
"org" (&optional n
))
41 (declare-function org-at-table.el-p
"org" ())
42 (declare-function org-in-src-block-p
"org" (&optional inside
))
43 (declare-function org-get-indentation
"org" (&optional line
))
44 (declare-function org-switch-to-buffer-other-window
"org" (&rest args
))
45 (declare-function org-pop-to-buffer-same-window
46 "org-compat" (&optional buffer-or-name norecord label
))
47 (declare-function org-base-buffer
"org" (buffer))
49 (defcustom org-edit-src-region-extra nil
50 "Additional regexps to identify regions for editing with `org-edit-src-code'.
51 For examples see the function `org-edit-src-find-region-and-lang'.
52 The regular expression identifying the begin marker should end with a newline,
53 and the regexp marking the end line should start with a newline, to make sure
54 there are kept outside the narrowed region."
55 :group
'org-edit-structure
58 (regexp :tag
"begin regexp")
59 (regexp :tag
"end regexp")
60 (choice :tag
"language"
61 (string :tag
"specify")
62 (integer :tag
"from match group")
63 (const :tag
"from `lang' element")
64 (const :tag
"from `style' element")))))
66 (defcustom org-coderef-label-format
"(ref:%s)"
67 "The default coderef format.
68 This format string will be used to search for coderef labels in literal
69 examples (EXAMPLE and SRC blocks). The format can be overwritten in
70 an individual literal example with the -l option, like
72 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
76 If you want to use this for HTML export, make sure that the format does
77 not introduce special font-locking, and avoid the HTML special
78 characters `<', `>', and `&'. The reason for this restriction is that
79 the labels are searched for only after htmlize has done its job."
80 :group
'org-edit-structure
; FIXME this is not in the right group
83 (defcustom org-edit-fixed-width-region-mode
'artist-mode
84 "The mode that should be used to edit fixed-width regions.
85 These are the regions where each line starts with a colon."
86 :group
'org-edit-structure
90 (const fundamental-mode
)
91 (function :tag
"Other (specify)")))
93 (defcustom org-src-preserve-indentation nil
94 "If non-nil preserve leading whitespace characters on export.
95 If non-nil leading whitespace characters in source code blocks
96 are preserved on export, and when switching between the org
97 buffer and the language mode edit buffer. If this variable is nil
98 then, after editing with \\[org-edit-src-code], the
99 minimum (across-lines) number of leading whitespace characters
100 are removed from all lines, and the code block is uniformly
101 indented according to the value of `org-edit-src-content-indentation'."
102 :group
'org-edit-structure
105 (defcustom org-edit-src-content-indentation
2
106 "Indentation for the content of a source code block.
107 This should be the number of spaces added to the indentation of the #+begin
108 line in order to compute the indentation of the block content after
109 editing it with \\[org-edit-src-code]. Has no effect if
110 `org-src-preserve-indentation' is non-nil."
111 :group
'org-edit-structure
114 (defvar org-src-strip-leading-and-trailing-blank-lines nil
115 "If non-nil, blank lines are removed when exiting the code edit buffer.")
117 (defcustom org-edit-src-persistent-message t
118 "Non-nil means show persistent exit help message while editing src examples.
119 The message is shown in the header-line, which will be created in the
120 first line of the window showing the editing buffer."
121 :group
'org-edit-structure
124 (defcustom org-src-window-setup
'reorganize-frame
125 "How the source code edit buffer should be displayed.
126 Possible values for this option are:
128 current-window Show edit buffer in the current window, keeping all other
130 other-window Use `switch-to-buffer-other-window' to display edit buffer.
131 reorganize-frame Show only two windows on the current frame, the current
132 window and the edit buffer. When exiting the edit buffer,
133 return to one window.
134 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
135 Also, when exiting the edit buffer, kill that frame."
136 :group
'org-edit-structure
138 (const current-window
)
141 (const reorganize-frame
)))
143 (defvar org-src-mode-hook nil
144 "Hook run after Org switched a source code snippet to its Emacs mode.
147 - when editing a source code snippet with \"C-c '\".
148 - When formatting a source code snippet for export with htmlize.
150 You may want to use this hook for example to turn off `outline-minor-mode'
151 or similar things which you want to have when editing a source code file,
152 but which mess up the display of a snippet in Org exported files.")
154 (defcustom org-src-lang-modes
155 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
156 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
157 ("calc" . fundamental
) ("C" . c
) ("cpp" . c
++)
158 ("screen" . shell-script
))
159 "Alist mapping languages to their major mode.
160 The key is the language name, the value is the string that should
161 be inserted as the name of the major mode. For many languages this is
162 simple, but for language where this is not the case, this variable
163 provides a way to simplify things on the user side.
164 For example, there is no ocaml-mode in Emacs, but the mode to use is
166 :group
'org-edit-structure
169 (string "Language name")
170 (symbol "Major mode"))))
172 ;;; Editing source examples
174 (defvar org-src-mode-map
(make-sparse-keymap))
175 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
176 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
178 (defvar org-edit-src-force-single-line nil
)
179 (defvar org-edit-src-from-org-mode nil
)
180 (defvar org-edit-src-allow-write-back-p t
)
181 (defvar org-edit-src-picture nil
)
182 (defvar org-edit-src-beg-marker nil
)
183 (defvar org-edit-src-end-marker nil
)
184 (defvar org-edit-src-overlay nil
)
185 (defvar org-edit-src-block-indentation nil
)
186 (defvar org-edit-src-saved-temp-window-config nil
)
188 (defvar org-src-ask-before-returning-to-edit-buffer t
189 "If nil, when org-edit-src code is used on a block that already
190 has an active edit buffer, it will switch to that edit buffer
191 immediately; otherwise it will ask whether you want to return to
192 the existing edit buffer.")
194 (defvar org-src-babel-info nil
)
196 (define-minor-mode org-src-mode
197 "Minor mode for language major mode buffers generated by org.
198 This minor mode is turned on in two situations:
199 - when editing a source code snippet with \"C-c '\".
200 - When formatting a source code snippet for export with htmlize.
201 There is a mode hook, and keybindings for `org-edit-src-exit' and
202 `org-edit-src-save'")
204 (defun org-edit-src-code (&optional context code edit-buffer-name
)
205 "Edit the source CODE block at point.
206 The code is copied to a separate buffer and the appropriate mode
207 is turned on. When done, exit with \\[org-edit-src-exit]. This will
208 remove the original code in the Org buffer, and replace it with the
209 edited version. An optional argument CONTEXT is used by \\[org-edit-src-save]
210 when calling this function. See `org-src-window-setup' to configure
211 the display of windows containing the Org buffer and the code buffer."
213 (if (not (org-in-src-block-p))
214 (user-error "Not in a source code block")
215 (unless (eq context
'save
)
216 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
217 (let* ((mark (and (org-region-active-p) (mark)))
220 ;; If the src region consists in no lines, we insert a blank
222 (let* ((temp (org-edit-src-find-region-and-lang))
225 (if (>= end beg
) temp
228 (org-edit-src-find-region-and-lang))))
229 (full-info (org-babel-get-src-block-info 'light
))
230 (org-mode-p (derived-mode-p 'org-mode
)) ;; derived-mode-p is reflexive
232 ;; Move marker with inserted text for case when src block is
233 ;; just one empty line, i.e. beg == end.
234 (end (copy-marker (make-marker) t
))
235 (allow-write-back-p (null code
))
236 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
237 begline markline markcol line col transmitted-variables
)
238 (setq beg
(move-marker beg
(nth 0 info
))
239 end
(move-marker end
(nth 1 info
))
240 msg
(if allow-write-back-p
241 (substitute-command-keys
242 "Edit, then exit with C-c ' (C-c and single quote)")
243 "Exit with C-c ' (C-c and single quote)")
244 code
(or code
(buffer-substring-no-properties beg end
))
245 lang
(or (cdr (assoc (nth 2 info
) org-src-lang-modes
))
247 lang
(if (symbolp lang
) (symbol-name lang
) lang
)
249 block-nindent
(nth 5 info
)
250 lang-f
(intern (concat lang
"-mode"))
251 begline
(save-excursion (goto-char beg
) (org-current-line))
252 transmitted-variables
253 `((org-edit-src-content-indentation
254 ,org-edit-src-content-indentation
)
255 (org-edit-src-force-single-line ,single
)
256 (org-edit-src-from-org-mode ,org-mode-p
)
257 (org-edit-src-allow-write-back-p ,allow-write-back-p
)
258 (org-src-preserve-indentation ,org-src-preserve-indentation
)
259 (org-src-babel-info ,(org-babel-get-src-block-info 'light
))
260 (org-coderef-label-format
261 ,(or (nth 4 info
) org-coderef-label-format
))
262 (org-edit-src-beg-marker ,beg
)
263 (org-edit-src-end-marker ,end
)
264 (org-edit-src-block-indentation ,block-nindent
)))
265 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
266 (save-excursion (goto-char (min mark end
))
267 (setq markline
(org-current-line)
268 markcol
(current-column))))
269 (if (equal lang-f
'table.el-mode
)
270 (setq lang-f
(lambda ()
272 (if (org-bound-and-true-p flyspell-mode
)
275 (org-set-local 'org-edit-src-content-indentation
0))))
276 (unless (functionp lang-f
)
277 (error "No such language mode: %s" lang-f
))
279 (if (> (point) end
) (goto-char end
))
280 (setq line
(org-current-line)
281 col
(current-column)))
282 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
283 (or (eq context
'save
)
284 (if org-src-ask-before-returning-to-edit-buffer
285 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? ") t
)))
286 (org-src-switch-to-buffer buffer
'return
)
288 (with-current-buffer buffer
289 (if (boundp 'org-edit-src-overlay
)
290 (delete-overlay org-edit-src-overlay
)))
291 (kill-buffer buffer
))
292 (setq buffer
(generate-new-buffer
294 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
295 (setq ovl
(make-overlay beg end
))
296 (overlay-put ovl
'edit-buffer buffer
)
297 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
298 (overlay-put ovl
'face
'secondary-selection
)
301 (let ((map (make-sparse-keymap)))
302 (define-key map
[mouse-1
] 'org-edit-src-continue
)
304 (overlay-put ovl
:read-only
"Leave me alone")
305 (setq transmitted-variables
306 (append transmitted-variables
`((org-edit-src-overlay ,ovl
))))
307 (org-src-switch-to-buffer buffer
'edit
)
308 (if (eq single
'macro-definition
)
309 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
311 (remove-text-properties (point-min) (point-max)
312 '(display nil invisible nil intangible nil
))
313 (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables
))
314 (setq total-nindent
(or (org-do-remove-indentation) 0)))
315 (let ((org-inhibit-startup t
))
319 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
320 (dolist (pair transmitted-variables
)
321 (org-set-local (car pair
) (cadr pair
)))
322 ;; Remove protecting commas from visible part of buffer.
323 (org-unescape-code-in-region (point-min) (point-max))
325 (org-goto-line (1+ (- markline begline
)))
327 (if org-src-preserve-indentation markcol
328 (max 0 (- markcol total-nindent
))))
329 (push-mark (point) 'no-message t
)
330 (setq deactivate-mark nil
))
331 (org-goto-line (1+ (- line begline
)))
333 (if org-src-preserve-indentation col
(max 0 (- col total-nindent
))))
335 (set-buffer-modified-p nil
)
336 (setq buffer-file-name nil
)
337 (and org-edit-src-persistent-message
338 (org-set-local 'header-line-format msg
))
339 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
340 (when (fboundp edit-prep-func
)
341 (funcall edit-prep-func full-info
))))
344 (defun org-edit-src-continue (e)
345 "Continue editing source blocks." ;; Fixme: be more accurate
348 (let ((buf (get-char-property (point) 'edit-buffer
)))
349 (if buf
(org-src-switch-to-buffer buf
'continue
)
350 (error "Something is wrong here"))))
352 (defun org-src-switch-to-buffer (buffer context
)
353 (case org-src-window-setup
355 (org-pop-to-buffer-same-window buffer
))
357 (switch-to-buffer-other-window buffer
))
361 (let ((frame (selected-frame)))
362 (switch-to-buffer-other-frame buffer
)
363 (delete-frame frame
)))
365 (kill-buffer (current-buffer))
366 (org-pop-to-buffer-same-window buffer
))
368 (switch-to-buffer-other-frame buffer
))))
370 (if (eq context
'edit
) (delete-other-windows))
371 (org-switch-to-buffer-other-window buffer
)
372 (if (eq context
'exit
) (delete-other-windows)))
376 (message "Invalid value %s for org-src-window-setup"
377 (symbol-name org-src-window-setup
))
378 (org-pop-to-buffer-same-window buffer
))))
380 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
381 "Construct the buffer name for a source editing buffer."
382 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
384 (defun org-src-edit-buffer-p (&optional buffer
)
385 "Test whether BUFFER (or the current buffer if BUFFER is nil)
386 is a source block editing buffer."
387 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
388 (and (buffer-name buffer
)
389 (string-match "\\`*Org Src " (buffer-name buffer
))
390 (local-variable-p 'org-edit-src-beg-marker buffer
)
391 (local-variable-p 'org-edit-src-end-marker buffer
))))
393 (defun org-edit-src-find-buffer (beg end
)
394 "Find a source editing buffer that is already editing the region BEG to END."
398 (with-current-buffer b
399 (if (and (string-match "\\`*Org Src " (buffer-name))
400 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
401 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
402 (equal beg org-edit-src-beg-marker
)
403 (equal end org-edit-src-end-marker
))
404 (throw 'exit
(current-buffer)))))
408 (defun org-edit-fixed-width-region ()
409 "Edit the fixed-width ascii drawing at point.
410 This must be a region where each line starts with a colon followed by
412 An new buffer is created and the fixed-width region is copied into it,
413 and the buffer is switched into `artist-mode' for editing. When done,
414 exit with \\[org-edit-src-exit]. The edited text will then replace
415 the fragment in the Org-mode buffer."
417 (let ((line (org-current-line))
418 (col (current-column))
420 (msg (substitute-command-keys
421 "Edit, then exit with C-c ' (C-c and single quote)"))
422 (org-mode-p (derived-mode-p 'org-mode
))
425 (preserve-indentation org-src-preserve-indentation
)
426 block-nindent ovl beg1 end1 code begline buffer
)
427 (beginning-of-line 1)
428 (if (looking-at "[ \t]*[^:\n \t]")
430 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
431 (setq beg1
(point) end1 beg1
)
433 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
434 (setq beg1
(point-at-bol 2))
435 (setq beg1
(point))))
437 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
438 (setq end1
(1- (match-beginning 0)))
439 (setq end1
(point))))
440 (org-goto-line line
))
441 (setq beg
(move-marker beg beg1
)
442 end
(move-marker end end1
)
443 code
(buffer-substring-no-properties beg end
)
444 begline
(save-excursion (goto-char beg
) (org-current-line)))
445 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
446 (y-or-n-p "Return to existing edit buffer ([n] will revert changes)? "))
447 (org-pop-to-buffer-same-window buffer
)
449 (with-current-buffer buffer
450 (if (boundp 'org-edit-src-overlay
)
451 (delete-overlay org-edit-src-overlay
)))
452 (kill-buffer buffer
))
453 (setq buffer
(generate-new-buffer
454 (org-src-construct-edit-buffer-name
455 (buffer-name) "Fixed Width")))
456 (setq ovl
(make-overlay beg end
))
457 (overlay-put ovl
'face
'secondary-selection
)
458 (overlay-put ovl
'edit-buffer buffer
)
459 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
460 (overlay-put ovl
'face
'secondary-selection
)
463 (let ((map (make-sparse-keymap)))
464 (define-key map
[mouse-1
] 'org-edit-src-continue
)
466 (overlay-put ovl
:read-only
"Leave me alone")
467 (org-pop-to-buffer-same-window buffer
)
469 (remove-text-properties (point-min) (point-max)
470 '(display nil invisible nil intangible nil
))
471 (setq block-nindent
(or (org-do-remove-indentation) 0))
473 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
476 (t (funcall org-edit-fixed-width-region-mode
)))
477 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
478 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
479 (set (make-local-variable 'org-edit-src-picture
) t
)
480 (goto-char (point-min))
481 (while (re-search-forward "^[ \t]*: ?" nil t
)
483 (org-goto-line (1+ (- line begline
)))
484 (org-move-to-column (max 0 (- col block-nindent
2)))
485 (org-set-local 'org-edit-src-beg-marker beg
)
486 (org-set-local 'org-edit-src-end-marker end
)
487 (org-set-local 'org-edit-src-overlay ovl
)
488 (org-set-local 'org-edit-src-block-indentation block-nindent
)
489 (org-set-local 'org-edit-src-content-indentation
0)
490 (org-set-local 'org-src-preserve-indentation nil
)
492 (set-buffer-modified-p nil
)
493 (and org-edit-src-persistent-message
494 (org-set-local 'header-line-format msg
)))
498 (defun org-edit-src-find-region-and-lang ()
499 "Find the region and language for a local edit.
500 Return a list with beginning and end of the region, a string representing
501 the language, a switch telling if the content should be in a single line."
504 org-edit-src-region-extra
506 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
507 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
508 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
509 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
510 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
511 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
512 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
513 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
514 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
515 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
516 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
517 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
518 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
519 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
520 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
521 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
522 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
523 "\n" "fundamental" macro-definition
)
524 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
527 re1 re2 single beg end lang lfmt match-re1 ind entry
)
529 (while (setq entry
(pop re-list
))
530 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
531 single
(nth 3 entry
))
533 (if (or (looking-at re1
)
534 (re-search-backward re1 nil t
))
536 (setq match-re1
(match-string 0))
537 (setq beg
(match-end 0)
538 lang
(org-edit-src-get-lang lang
)
539 lfmt
(org-edit-src-get-label-format match-re1
)
540 ind
(org-edit-src-get-indentation (match-beginning 0)))
541 (if (and (re-search-forward re2 nil t
)
542 (>= (match-end 0) pos
))
543 (throw 'exit
(list beg
(match-beginning 0)
544 lang single lfmt ind
))))
545 (if (or (looking-at re2
)
546 (re-search-forward re2 nil t
))
548 (setq end
(match-beginning 0))
549 (if (and (re-search-backward re1 nil t
)
550 (<= (match-beginning 0) pos
))
552 (setq lfmt
(org-edit-src-get-label-format
554 ind
(org-edit-src-get-indentation
555 (match-beginning 0)))
557 (list (match-end 0) end
558 (org-edit-src-get-lang lang
)
559 single lfmt ind
)))))))))
560 (when (org-at-table.el-p
)
561 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
562 (setq beg
(1+ (point-at-eol)))
564 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
565 (progn (goto-char (point-max)) (newline)))
566 (setq end
(point-at-bol))
567 (setq ind
(org-edit-src-get-indentation beg
))
568 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
570 (defun org-edit-src-get-lang (lang)
571 "Extract the src language."
572 (let ((m (match-string 0)))
574 ((stringp lang
) lang
)
575 ((integerp lang
) (match-string lang
))
576 ((and (eq lang
'lang
)
577 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
579 ((and (eq lang
'style
)
580 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
584 (defun org-edit-src-get-label-format (s)
585 "Extract the label format."
587 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
588 (match-string 1 s
))))
590 (defun org-edit-src-get-indentation (pos)
591 "Count leading whitespace characters on line."
594 (org-get-indentation)))
596 (defun org-escape-code-in-region (beg end
)
597 "Escape lines between BEG and END.
598 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
599 \",#+\" by appending a comma to it."
603 (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t
)
604 (replace-match ",\\1" nil nil nil
1))))
606 (defun org-escape-code-in-string (s)
607 "Escape lines in string S.
608 Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
609 \",#+\" by appending a comma to it."
610 (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil
1))
612 (defun org-unescape-code-in-region (beg end
)
613 "Un-escape lines between BEG and END.
614 Un-escaping happens by removing the first comma on lines starting
615 with \",*\", \",#+\", \",,*\" and \",,#+\"."
619 (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t
)
620 (replace-match "" nil nil nil
1))))
622 (defun org-unescape-code-in-string (s)
623 "Un-escape lines in string S.
624 Un-escaping happens by removing the first comma on lines starting
625 with \",*\", \",#+\", \",,*\" and \",,#+\"."
626 (replace-regexp-in-string
627 "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil
1))
629 (defun org-edit-src-exit (&optional context
)
630 "Exit special edit and protect problematic lines."
632 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
633 (error "This is not a sub-editing buffer, something is wrong"))
635 (let* ((beg org-edit-src-beg-marker
)
636 (end org-edit-src-end-marker
)
637 (ovl org-edit-src-overlay
)
638 (bufstr (buffer-string))
639 (buffer (current-buffer))
640 (single (org-bound-and-true-p org-edit-src-force-single-line
))
641 (macro (eq single
'macro-definition
))
642 (total-nindent (+ (or org-edit-src-block-indentation
0)
643 org-edit-src-content-indentation
))
644 (preserve-indentation org-src-preserve-indentation
)
645 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
646 (delta 0) code line col indent
)
647 (when allow-write-back-p
648 (unless preserve-indentation
(untabify (point-min) (point-max)))
649 (if org-src-strip-leading-and-trailing-blank-lines
651 (goto-char (point-min))
652 (if (looking-at "[ \t\n]*\n") (replace-match ""))
654 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
655 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
658 col
(current-column))
659 (when allow-write-back-p
661 (goto-char (point-min))
662 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
663 (goto-char (point-min))
665 (while (re-search-forward "\n" nil t
)
667 (replace-match (if macro
"\\n" " ") t t
))
668 (when (and macro
(> cnt
0))
669 (goto-char (point-max)) (insert "\\n")))
670 (goto-char (point-min))
671 (if (looking-at "\\s-*") (replace-match " ")))
672 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
673 (org-escape-code-in-region (point-min) (point-max))
677 (if (looking-at "[ \t]*\\(,,\\)?\\(\\*\\|#+\\)") 1
679 (when (org-bound-and-true-p org-edit-src-picture
)
680 (setq preserve-indentation nil
)
681 (untabify (point-min) (point-max))
682 (goto-char (point-min))
683 (while (re-search-forward "^" nil t
)
684 (replace-match ": ")))
685 (unless (or single preserve-indentation
(= total-nindent
0))
686 (setq indent
(make-string total-nindent ?\
))
687 (goto-char (point-min))
688 (while (re-search-forward "^" nil t
)
689 (replace-match indent
)))
690 (if (org-bound-and-true-p org-edit-src-picture
)
691 (setq total-nindent
(+ total-nindent
2)))
692 (setq code
(buffer-string))
693 (when (eq context
'save
)
696 (set-buffer-modified-p nil
))
697 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
698 (if (eq context
'save
) (save-buffer)
699 (kill-buffer buffer
))
701 (when allow-write-back-p
702 (delete-region beg
(max beg end
))
703 (unless (string-match "\\`[ \t]*\\'" code
)
706 (if single
(just-one-space)))
707 (if (memq t
(mapcar (lambda (overlay)
708 (eq (overlay-get overlay
'invisible
)
710 (overlays-at (point))))
711 ;; Block is hidden; put point at start of block
712 (beginning-of-line 0)
713 ;; Block is visible, put point where it was in the code buffer
714 (org-goto-line (1- (+ (org-current-line) line
)))
715 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
716 (unless (eq context
'save
)
717 (move-marker beg nil
)
718 (move-marker end nil
)))
719 (unless (eq context
'save
)
720 (when org-edit-src-saved-temp-window-config
721 (set-window-configuration org-edit-src-saved-temp-window-config
)
722 (setq org-edit-src-saved-temp-window-config nil
))))
724 (defmacro org-src-in-org-buffer
(&rest body
)
725 `(let ((p (point)) (m (mark)) (ul buffer-undo-list
) msg
)
726 (save-window-excursion
727 (org-edit-src-exit 'save
)
729 (setq msg
(current-message))
730 (if (eq org-src-window-setup
'other-frame
)
731 (let ((org-src-window-setup 'current-window
))
732 (org-edit-src-code 'save
))
733 (org-edit-src-code 'save
)))
734 (setq buffer-undo-list ul
)
735 (push-mark m
'nomessage
)
736 (goto-char (min p
(point-max)))
737 (message (or msg
""))))
738 (def-edebug-spec org-src-in-org-buffer
(body))
740 (defun org-edit-src-save ()
741 "Save parent buffer with current state source-code buffer."
743 (org-src-in-org-buffer (save-buffer)))
745 (declare-function org-babel-tangle
"ob-tangle" (&optional only-this-block target-file lang
))
747 (defun org-src-tangle (arg)
748 "Tangle the parent buffer."
750 (org-src-in-org-buffer (org-babel-tangle arg
)))
752 (defun org-src-mode-configure-edit-buffer ()
753 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
754 (org-add-hook 'kill-buffer-hook
755 #'(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
756 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
758 (setq buffer-offer-save t
)
759 (setq buffer-file-name
760 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
761 "[" (buffer-name) "]"))
762 (if (featurep 'xemacs
)
764 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
765 (setq write-contents-hooks
'(org-edit-src-save)))
766 (setq write-contents-functions
'(org-edit-src-save))))
767 (setq buffer-read-only t
))))
769 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
772 (defun org-src-associate-babel-session (info)
773 "Associate edit buffer with comint session."
775 (let ((session (cdr (assoc :session
(nth 2 info
)))))
776 (and session
(not (string= session
"none"))
777 (org-babel-comint-buffer-livep session
)
778 ((lambda (f) (and (fboundp f
) (funcall f session
)))
779 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
781 (defun org-src-babel-configure-edit-buffer ()
782 (when org-src-babel-info
783 (org-src-associate-babel-session org-src-babel-info
)))
785 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
786 (defmacro org-src-do-at-code-block
(&rest body
)
787 "Execute a command from an edit buffer in the Org-mode buffer."
788 `(let ((beg-marker org-edit-src-beg-marker
))
790 (with-current-buffer (marker-buffer beg-marker
)
791 (goto-char (marker-position beg-marker
))
793 (def-edebug-spec org-src-do-at-code-block
(body))
795 (defun org-src-do-key-sequence-at-code-block (&optional key
)
796 "Execute key sequence at code block in the source Org buffer.
797 The command bound to KEY in the Org-babel key map is executed
798 remotely with point temporarily at the start of the code block in
801 This command is not bound to a key by default, to avoid conflicts
802 with language major mode bindings. To bind it to C-c @ in all
803 language major modes, you could use
805 (add-hook 'org-src-mode-hook
806 (lambda () (define-key org-src-mode-map \"\\C-c@\"
807 'org-src-do-key-sequence-at-code-block)))
809 In that case, for example, C-c @ t issued in code edit buffers
810 would tangle the current Org code block, C-c @ e would execute
811 the block and C-c @ h would display the other available
813 (interactive "kOrg-babel key: ")
814 (if (equal key
(kbd "C-g")) (keyboard-quit)
816 (org-src-do-at-code-block
818 (lookup-key org-babel-map key
)))))
820 (defcustom org-src-tab-acts-natively nil
821 "If non-nil, the effect of TAB in a code block is as if it were
822 issued in the language major mode buffer."
827 (defun org-src-native-tab-command-maybe ()
828 "Perform language-specific TAB action.
829 Alter code block according to effect of TAB in the language major
831 (and org-src-tab-acts-natively
832 (not (equal this-command
'org-shifttab
))
833 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
834 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
836 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
838 (defun org-src-font-lock-fontify-block (lang start end
)
840 This function is called by emacs automatic fontification, as long
841 as `org-src-fontify-natively' is non-nil. For manual
842 fontification of code blocks see `org-src-fontify-block' and
843 `org-src-fontify-buffer'"
844 (let ((lang-mode (org-src-get-lang-mode lang
)))
845 (if (fboundp lang-mode
)
846 (let ((string (buffer-substring-no-properties start end
))
847 (modified (buffer-modified-p))
848 (org-buffer (current-buffer)) pos next
)
849 (remove-text-properties start end
'(face nil
))
852 (concat " org-src-fontification:" (symbol-name lang-mode
)))
853 (delete-region (point-min) (point-max))
854 (insert string
" ") ;; so there's a final property change
855 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
856 (font-lock-fontify-buffer)
857 (setq pos
(point-min))
858 (while (setq next
(next-single-property-change pos
'face
))
860 (+ start
(1- pos
)) (1- (+ start next
)) 'face
861 (get-text-property pos
'face
) org-buffer
)
865 '(font-lock-fontified t fontified t font-lock-multiline t
))
866 (set-buffer-modified-p modified
)))))
868 (defun org-src-fontify-block ()
869 "Fontify code block at point."
872 (let ((org-src-fontify-natively t
)
873 (info (org-edit-src-find-region-and-lang)))
874 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
876 (defun org-src-fontify-buffer ()
877 "Fontify all code blocks in the current buffer."
879 (org-babel-map-src-blocks nil
880 (org-src-fontify-block)))
882 (defun org-src-get-lang-mode (lang)
883 "Return major mode that should be used for LANG.
884 LANG is a string, and the returned major mode is a symbol."
887 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
888 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
892 ;;; org-src.el ends here