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-get-indentation
"org" (&optional line
))
43 (declare-function org-switch-to-buffer-other-window
"org" (&rest args
))
44 (declare-function org-pop-to-buffer-same-window
45 "org-compat" (&optional buffer-or-name norecord label
))
47 (defcustom org-edit-src-region-extra nil
48 "Additional regexps to identify regions for editing with `org-edit-src-code'.
49 For examples see the function `org-edit-src-find-region-and-lang'.
50 The regular expression identifying the begin marker should end with a newline,
51 and the regexp marking the end line should start with a newline, to make sure
52 there are kept outside the narrowed region."
53 :group
'org-edit-structure
56 (regexp :tag
"begin regexp")
57 (regexp :tag
"end regexp")
58 (choice :tag
"language"
59 (string :tag
"specify")
60 (integer :tag
"from match group")
61 (const :tag
"from `lang' element")
62 (const :tag
"from `style' element")))))
64 (defcustom org-coderef-label-format
"(ref:%s)"
65 "The default coderef format.
66 This format string will be used to search for coderef labels in literal
67 examples (EXAMPLE and SRC blocks). The format can be overwritten in
68 an individual literal example with the -l option, like
70 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
74 If you want to use this for HTML export, make sure that the format does
75 not introduce special font-locking, and avoid the HTML special
76 characters `<', `>', and `&'. The reason for this restriction is that
77 the labels are searched for only after htmlize has done its job."
78 :group
'org-edit-structure
; FIXME this is not in the right group
81 (defcustom org-edit-fixed-width-region-mode
'artist-mode
82 "The mode that should be used to edit fixed-width regions.
83 These are the regions where each line starts with a colon."
84 :group
'org-edit-structure
88 (const fundamental-mode
)
89 (function :tag
"Other (specify)")))
91 (defcustom org-src-preserve-indentation nil
92 "If non-nil preserve leading whitespace characters on export.
93 If non-nil leading whitespace characters in source code blocks
94 are preserved on export, and when switching between the org
95 buffer and the language mode edit buffer. If this variable is nil
96 then, after editing with \\[org-edit-src-code], the
97 minimum (across-lines) number of leading whitespace characters
98 are removed from all lines, and the code block is uniformly
99 indented according to the value of `org-edit-src-content-indentation'."
100 :group
'org-edit-structure
103 (defcustom org-edit-src-content-indentation
2
104 "Indentation for the content of a source code block.
105 This should be the number of spaces added to the indentation of the #+begin
106 line in order to compute the indentation of the block content after
107 editing it with \\[org-edit-src-code]. Has no effect if
108 `org-src-preserve-indentation' is non-nil."
109 :group
'org-edit-structure
112 (defvar org-src-strip-leading-and-trailing-blank-lines nil
113 "If non-nil, blank lines are removed when exiting the code edit
116 (defcustom org-edit-src-persistent-message t
117 "Non-nil means show persistent exit help message while editing src examples.
118 The message is shown in the header-line, which will be created in the
119 first line of the window showing the editing buffer."
120 :group
'org-edit-structure
123 (defcustom org-src-window-setup
'reorganize-frame
124 "How the source code edit buffer should be displayed.
125 Possible values for this option are:
127 current-window Show edit buffer in the current window, keeping all other
129 other-window Use `switch-to-buffer-other-window' to display edit buffer.
130 reorganize-frame Show only two windows on the current frame, the current
131 window and the edit buffer. When exiting the edit buffer,
132 return to one window.
133 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
134 Also, when exiting the edit buffer, kill that frame."
135 :group
'org-edit-structure
137 (const current-window
)
140 (const reorganize-frame
)))
142 (defvar org-src-mode-hook nil
143 "Hook run after Org switched a source code snippet to its Emacs mode.
146 - when editing a source code snippet with \"C-c '\".
147 - When formatting a source code snippet for export with htmlize.
149 You may want to use this hook for example to turn off `outline-minor-mode'
150 or similar things which you want to have when editing a source code file,
151 but which mess up the display of a snippet in Org exported files.")
153 (defcustom org-src-lang-modes
154 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
155 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
156 ("calc" . fundamental
) ("C" . c
) ("cpp" . c
++)
157 ("screen" . shell-script
))
158 "Alist mapping languages to their major mode.
159 The key is the language name, the value is the string that should
160 be inserted as the name of the major mode. For many languages this is
161 simple, but for language where this is not the case, this variable
162 provides a way to simplify things on the user side.
163 For example, there is no ocaml-mode in Emacs, but the mode to use is
165 :group
'org-edit-structure
168 (string "Language name")
169 (symbol "Major mode"))))
171 ;;; Editing source examples
173 (defvar org-src-mode-map
(make-sparse-keymap))
174 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
175 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
177 (defvar org-edit-src-force-single-line nil
)
178 (defvar org-edit-src-from-org-mode nil
)
179 (defvar org-edit-src-allow-write-back-p t
)
180 (defvar org-edit-src-picture nil
)
181 (defvar org-edit-src-beg-marker nil
)
182 (defvar org-edit-src-end-marker nil
)
183 (defvar org-edit-src-overlay nil
)
184 (defvar org-edit-src-block-indentation nil
)
185 (defvar org-edit-src-saved-temp-window-config nil
)
187 (defvar org-src-ask-before-returning-to-edit-buffer t
188 "If nil, when org-edit-src code is used on a block that already
189 has an active edit buffer, it will switch to that edit buffer
190 immediately; otherwise it will ask whether you want to return
191 to the existing edit buffer.")
193 (defvar org-src-babel-info nil
)
195 (define-minor-mode org-src-mode
196 "Minor mode for language major mode buffers generated by org.
197 This minor mode is turned on in two situations:
198 - when editing a source code snippet with \"C-c '\".
199 - When formatting a source code snippet for export with htmlize.
200 There is a mode hook, and keybindings for `org-edit-src-exit' and
201 `org-edit-src-save'")
203 (defun org-edit-src-code (&optional context code edit-buffer-name
)
204 "Edit the source code example at point.
205 The example is copied to a separate buffer, and that buffer is
206 switched to the correct language mode. When done, exit with
207 \\[org-edit-src-exit]. This will remove the original code in the
208 Org buffer, and replace it with the edited version. Optional
209 argument CONTEXT is used by \\[org-edit-src-save] when calling
210 this function. See \\[org-src-window-setup] to configure the
211 display of windows containing the Org buffer and the code
214 (unless (eq context
'save
)
215 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
216 (let ((mark (and (org-region-active-p) (mark)))
218 (info (org-edit-src-find-region-and-lang))
219 (full-info (org-babel-get-src-block-info 'light
))
220 (org-mode-p (derived-mode-p 'org-mode
)) ;; derived-mode-p is reflexive
223 (allow-write-back-p (null code
))
224 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
225 begline markline markcol line col transmitted-variables
)
228 (setq beg
(move-marker beg
(nth 0 info
))
229 end
(move-marker end
(nth 1 info
))
230 msg
(if allow-write-back-p
231 (substitute-command-keys
232 "Edit, then exit with C-c ' (C-c and single quote)")
233 "Exit with C-c ' (C-c and single quote)")
234 code
(or code
(buffer-substring-no-properties beg end
))
235 lang
(or (cdr (assoc (nth 2 info
) org-src-lang-modes
))
237 lang
(if (symbolp lang
) (symbol-name lang
) lang
)
239 block-nindent
(nth 5 info
)
240 lang-f
(intern (concat lang
"-mode"))
241 begline
(save-excursion (goto-char beg
) (org-current-line))
242 transmitted-variables
243 `((org-edit-src-content-indentation
244 ,org-edit-src-content-indentation
)
245 (org-edit-src-force-single-line ,single
)
246 (org-edit-src-from-org-mode ,org-mode-p
)
247 (org-edit-src-allow-write-back-p ,allow-write-back-p
)
248 (org-src-preserve-indentation ,org-src-preserve-indentation
)
249 (org-src-babel-info ,(org-babel-get-src-block-info 'light
))
250 (org-coderef-label-format
251 ,(or (nth 4 info
) org-coderef-label-format
))
252 (org-edit-src-beg-marker ,beg
)
253 (org-edit-src-end-marker ,end
)
254 (org-edit-src-block-indentation ,block-nindent
)))
255 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
256 (save-excursion (goto-char (min mark end
))
257 (setq markline
(org-current-line)
258 markcol
(current-column))))
259 (if (equal lang-f
'table.el-mode
)
260 (setq lang-f
(lambda ()
262 (if (org-bound-and-true-p flyspell-mode
)
265 (org-set-local 'org-edit-src-content-indentation
0))))
266 (unless (functionp lang-f
)
267 (error "No such language mode: %s" lang-f
))
269 (if (> (point) end
) (goto-char end
))
270 (setq line
(org-current-line)
271 col
(current-column)))
272 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
273 (if org-src-ask-before-returning-to-edit-buffer
274 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t
))
275 (org-src-switch-to-buffer buffer
'return
)
277 (with-current-buffer buffer
278 (if (boundp 'org-edit-src-overlay
)
279 (delete-overlay org-edit-src-overlay
)))
280 (kill-buffer buffer
))
281 (setq buffer
(generate-new-buffer
283 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
284 (setq ovl
(make-overlay beg end
))
285 (overlay-put ovl
'edit-buffer buffer
)
286 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
287 (overlay-put ovl
'face
'secondary-selection
)
290 (let ((map (make-sparse-keymap)))
291 (define-key map
[mouse-1
] 'org-edit-src-continue
)
293 (overlay-put ovl
:read-only
"Leave me alone")
294 (setq transmitted-variables
295 (append transmitted-variables
`((org-edit-src-overlay ,ovl
))))
296 (org-src-switch-to-buffer buffer
'edit
)
297 (if (eq single
'macro-definition
)
298 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
300 (remove-text-properties (point-min) (point-max)
301 '(display nil invisible nil intangible nil
))
302 (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables
))
303 (setq total-nindent
(or (org-do-remove-indentation) 0)))
304 (let ((org-inhibit-startup t
))
308 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
309 (dolist (pair transmitted-variables
)
310 (org-set-local (car pair
) (cadr pair
)))
311 (if (eq major-mode
'org-mode
)
313 (goto-char (point-min))
314 (while (re-search-forward "^," nil t
)
315 (if (eq (org-current-line) line
) (setq total-nindent
(1+ total-nindent
)))
317 (org-strip-protective-commas (point-min) (point-max)))
319 (org-goto-line (1+ (- markline begline
)))
321 (if org-src-preserve-indentation markcol
322 (max 0 (- markcol total-nindent
))))
323 (push-mark (point) 'no-message t
)
324 (setq deactivate-mark nil
))
325 (org-goto-line (1+ (- line begline
)))
327 (if org-src-preserve-indentation col
(max 0 (- col total-nindent
))))
329 (set-buffer-modified-p nil
)
330 (setq buffer-file-name nil
)
331 (and org-edit-src-persistent-message
332 (org-set-local 'header-line-format msg
))
333 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
334 (when (fboundp edit-prep-func
)
335 (funcall edit-prep-func full-info
))))
338 (defun org-edit-src-continue (e)
341 (let ((buf (get-char-property (point) 'edit-buffer
)))
342 (if buf
(org-src-switch-to-buffer buf
'continue
)
343 (error "Something is wrong here"))))
345 (defun org-src-switch-to-buffer (buffer context
)
346 (case org-src-window-setup
348 (org-pop-to-buffer-same-window buffer
))
350 (switch-to-buffer-other-window buffer
))
354 (let ((frame (selected-frame)))
355 (switch-to-buffer-other-frame buffer
)
356 (delete-frame frame
)))
358 (kill-buffer (current-buffer))
359 (org-pop-to-buffer-same-window buffer
))
361 (switch-to-buffer-other-frame buffer
))))
363 (if (eq context
'edit
) (delete-other-windows))
364 (org-switch-to-buffer-other-window buffer
)
365 (if (eq context
'exit
) (delete-other-windows)))
369 (message "Invalid value %s for org-src-window-setup"
370 (symbol-name org-src-window-setup
))
371 (org-pop-to-buffer-same-window buffer
))))
373 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
374 "Construct the buffer name for a source editing buffer."
375 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
377 (defun org-edit-src-find-buffer (beg end
)
378 "Find a source editing buffer that is already editing the region BEG to END."
382 (with-current-buffer b
383 (if (and (string-match "\\`*Org Src " (buffer-name))
384 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
385 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
386 (equal beg org-edit-src-beg-marker
)
387 (equal end org-edit-src-end-marker
))
388 (throw 'exit
(current-buffer)))))
392 (defun org-edit-fixed-width-region ()
393 "Edit the fixed-width ascii drawing at point.
394 This must be a region where each line starts with a colon followed by
396 An new buffer is created and the fixed-width region is copied into it,
397 and the buffer is switched into `artist-mode' for editing. When done,
398 exit with \\[org-edit-src-exit]. The edited text will then replace
399 the fragment in the Org-mode buffer."
401 (let ((line (org-current-line))
402 (col (current-column))
404 (msg (substitute-command-keys
405 "Edit, then exit with C-c ' (C-c and single quote)"))
406 (org-mode-p (eq major-mode
'org-mode
))
409 (preserve-indentation org-src-preserve-indentation
)
410 block-nindent ovl beg1 end1 code begline buffer
)
411 (beginning-of-line 1)
412 (if (looking-at "[ \t]*[^:\n \t]")
414 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
415 (setq beg1
(point) end1 beg1
)
417 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
418 (setq beg1
(point-at-bol 2))
419 (setq beg1
(point))))
421 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
422 (setq end1
(1- (match-beginning 0)))
423 (setq end1
(point))))
424 (org-goto-line line
))
425 (setq beg
(move-marker beg beg1
)
426 end
(move-marker end end1
)
427 code
(buffer-substring-no-properties beg end
)
428 begline
(save-excursion (goto-char beg
) (org-current-line)))
429 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
430 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
431 (org-pop-to-buffer-same-window buffer
)
433 (with-current-buffer buffer
434 (if (boundp 'org-edit-src-overlay
)
435 (delete-overlay org-edit-src-overlay
)))
436 (kill-buffer buffer
))
437 (setq buffer
(generate-new-buffer
438 (org-src-construct-edit-buffer-name
439 (buffer-name) "Fixed Width")))
440 (setq ovl
(make-overlay beg end
))
441 (overlay-put ovl
'face
'secondary-selection
)
442 (overlay-put ovl
'edit-buffer buffer
)
443 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
444 (overlay-put ovl
'face
'secondary-selection
)
447 (let ((map (make-sparse-keymap)))
448 (define-key map
[mouse-1
] 'org-edit-src-continue
)
450 (overlay-put ovl
:read-only
"Leave me alone")
451 (org-pop-to-buffer-same-window buffer
)
453 (remove-text-properties (point-min) (point-max)
454 '(display nil invisible nil intangible nil
))
455 (setq block-nindent
(or (org-do-remove-indentation) 0))
457 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
460 (t (funcall org-edit-fixed-width-region-mode
)))
461 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
462 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
463 (set (make-local-variable 'org-edit-src-picture
) t
)
464 (goto-char (point-min))
465 (while (re-search-forward "^[ \t]*: ?" nil t
)
467 (org-goto-line (1+ (- line begline
)))
468 (org-move-to-column (max 0 (- col block-nindent
2)))
469 (org-set-local 'org-edit-src-beg-marker beg
)
470 (org-set-local 'org-edit-src-end-marker end
)
471 (org-set-local 'org-edit-src-overlay ovl
)
472 (org-set-local 'org-edit-src-block-indentation block-nindent
)
473 (org-set-local 'org-edit-src-content-indentation
0)
474 (org-set-local 'org-src-preserve-indentation nil
)
476 (set-buffer-modified-p nil
)
477 (and org-edit-src-persistent-message
478 (org-set-local 'header-line-format msg
)))
482 (defun org-edit-src-find-region-and-lang ()
483 "Find the region and language for a local edit.
484 Return a list with beginning and end of the region, a string representing
485 the language, a switch telling if the content should be in a single line."
488 org-edit-src-region-extra
490 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
491 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
492 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
493 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
494 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
495 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
496 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
497 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
498 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
499 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
500 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
501 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
502 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
503 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
504 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
505 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
506 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
507 "\n" "fundamental" macro-definition
)
508 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
511 re1 re2 single beg end lang lfmt match-re1 ind entry
)
513 (while (setq entry
(pop re-list
))
514 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
515 single
(nth 3 entry
))
517 (if (or (looking-at re1
)
518 (re-search-backward re1 nil t
))
520 (setq match-re1
(match-string 0))
521 (setq beg
(match-end 0)
522 lang
(org-edit-src-get-lang lang
)
523 lfmt
(org-edit-src-get-label-format match-re1
)
524 ind
(org-edit-src-get-indentation (match-beginning 0)))
525 (if (and (re-search-forward re2 nil t
)
526 (>= (match-end 0) pos
))
527 (throw 'exit
(list beg
(match-beginning 0)
528 lang single lfmt ind
))))
529 (if (or (looking-at re2
)
530 (re-search-forward re2 nil t
))
532 (setq end
(match-beginning 0))
533 (if (and (re-search-backward re1 nil t
)
534 (<= (match-beginning 0) pos
))
536 (setq lfmt
(org-edit-src-get-label-format
538 ind
(org-edit-src-get-indentation
539 (match-beginning 0)))
541 (list (match-end 0) end
542 (org-edit-src-get-lang lang
)
543 single lfmt ind
)))))))))
544 (when (org-at-table.el-p
)
545 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
546 (setq beg
(1+ (point-at-eol)))
548 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
549 (progn (goto-char (point-max)) (newline)))
550 (setq end
(point-at-bol))
551 (setq ind
(org-edit-src-get-indentation beg
))
552 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
554 (defun org-edit-src-get-lang (lang)
555 "Extract the src language."
556 (let ((m (match-string 0)))
558 ((stringp lang
) lang
)
559 ((integerp lang
) (match-string lang
))
560 ((and (eq lang
'lang
)
561 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
563 ((and (eq lang
'style
)
564 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
568 (defun org-edit-src-get-label-format (s)
569 "Extract the label format."
571 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
572 (match-string 1 s
))))
574 (defun org-edit-src-get-indentation (pos)
575 "Count leading whitespace characters on line."
578 (org-get-indentation)))
580 (defun org-edit-src-exit (&optional context
)
581 "Exit special edit and protect problematic lines."
583 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
584 (error "This is not a sub-editing buffer, something is wrong"))
586 (let* ((beg org-edit-src-beg-marker
)
587 (end org-edit-src-end-marker
)
588 (ovl org-edit-src-overlay
)
589 (buffer (current-buffer))
590 (single (org-bound-and-true-p org-edit-src-force-single-line
))
591 (macro (eq single
'macro-definition
))
592 (total-nindent (+ (or org-edit-src-block-indentation
0)
593 org-edit-src-content-indentation
))
594 (preserve-indentation org-src-preserve-indentation
)
595 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
596 (delta 0) code line col indent
)
597 (when allow-write-back-p
598 (unless preserve-indentation
(untabify (point-min) (point-max)))
599 (if org-src-strip-leading-and-trailing-blank-lines
601 (goto-char (point-min))
602 (if (looking-at "[ \t\n]*\n") (replace-match ""))
604 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
605 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
608 col
(current-column))
609 (when allow-write-back-p
611 (goto-char (point-min))
612 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
613 (goto-char (point-min))
615 (while (re-search-forward "\n" nil t
)
617 (replace-match (if macro
"\\n" " ") t t
))
618 (when (and macro
(> cnt
0))
619 (goto-char (point-max)) (insert "\\n")))
620 (goto-char (point-min))
621 (if (looking-at "\\s-*") (replace-match " ")))
622 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
623 (goto-char (point-min))
624 (while (re-search-forward
625 (if (eq major-mode
'org-mode
) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
626 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
627 (replace-match ",\\1")))
628 (when (org-bound-and-true-p org-edit-src-picture
)
629 (setq preserve-indentation nil
)
630 (untabify (point-min) (point-max))
631 (goto-char (point-min))
632 (while (re-search-forward "^" nil t
)
633 (replace-match ": ")))
634 (unless (or single preserve-indentation
(= total-nindent
0))
635 (setq indent
(make-string total-nindent ?\
))
636 (goto-char (point-min))
637 (while (re-search-forward "^" nil t
)
638 (replace-match indent
)))
639 (if (org-bound-and-true-p org-edit-src-picture
)
640 (setq total-nindent
(+ total-nindent
2)))
641 (setq code
(buffer-string))
642 (set-buffer-modified-p nil
))
643 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
646 (when allow-write-back-p
647 (delete-region beg end
)
650 (if single
(just-one-space)))
651 (if (memq t
(mapcar (lambda (overlay)
652 (eq (overlay-get overlay
'invisible
)
654 (overlays-at (point))))
655 ;; Block is hidden; put point at start of block
656 (beginning-of-line 0)
657 ;; Block is visible, put point where it was in the code buffer
658 (org-goto-line (1- (+ (org-current-line) line
)))
659 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
660 (move-marker beg nil
)
661 (move-marker end nil
))
662 (unless (eq context
'save
)
663 (when org-edit-src-saved-temp-window-config
664 (set-window-configuration org-edit-src-saved-temp-window-config
)
665 (setq org-edit-src-saved-temp-window-config nil
))))
667 (defmacro org-src-in-org-buffer
(&rest body
)
668 `(let ((p (point)) (m (mark)) (ul buffer-undo-list
) msg
)
669 (save-window-excursion
670 (org-edit-src-exit 'save
)
672 (setq msg
(current-message))
673 (if (eq org-src-window-setup
'other-frame
)
674 (let ((org-src-window-setup 'current-window
))
675 (org-edit-src-code 'save
))
676 (org-edit-src-code 'save
)))
677 (setq buffer-undo-list ul
)
678 (push-mark m
'nomessage
)
679 (goto-char (min p
(point-max)))
680 (message (or msg
""))))
681 (def-edebug-spec org-src-in-org-buffer
(body))
683 (defun org-edit-src-save ()
684 "Save parent buffer with current state source-code buffer."
686 (org-src-in-org-buffer (save-buffer)))
688 (defun org-src-tangle (arg)
689 "Tangle the parent buffer."
691 (org-src-in-org-buffer (org-babel-tangle arg
)))
693 (defun org-src-mode-configure-edit-buffer ()
694 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
695 (org-add-hook 'kill-buffer-hook
696 #'(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
697 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
699 (setq buffer-offer-save t
)
700 (setq buffer-file-name
701 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
702 "[" (buffer-name) "]"))
703 (if (featurep 'xemacs
)
705 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
706 (setq write-contents-hooks
'(org-edit-src-save)))
707 (setq write-contents-functions
'(org-edit-src-save))))
708 (setq buffer-read-only t
))))
710 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
713 (defun org-src-associate-babel-session (info)
714 "Associate edit buffer with comint session."
716 (let ((session (cdr (assoc :session
(nth 2 info
)))))
717 (and session
(not (string= session
"none"))
718 (org-babel-comint-buffer-livep session
)
719 ((lambda (f) (and (fboundp f
) (funcall f session
)))
720 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
722 (defun org-src-babel-configure-edit-buffer ()
723 (when org-src-babel-info
724 (org-src-associate-babel-session org-src-babel-info
)))
726 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
727 (defmacro org-src-do-at-code-block
(&rest body
)
728 "Execute a command from an edit buffer in the Org-mode buffer."
729 `(let ((beg-marker org-edit-src-beg-marker
))
731 (with-current-buffer (marker-buffer beg-marker
)
732 (goto-char (marker-position beg-marker
))
734 (def-edebug-spec org-src-do-at-code-block
(body))
736 (defun org-src-do-key-sequence-at-code-block (&optional key
)
737 "Execute key sequence at code block in the source Org buffer.
738 The command bound to KEY in the Org-babel key map is executed
739 remotely with point temporarily at the start of the code block in
742 This command is not bound to a key by default, to avoid conflicts
743 with language major mode bindings. To bind it to C-c @ in all
744 language major modes, you could use
746 (add-hook 'org-src-mode-hook
747 (lambda () (define-key org-src-mode-map \"\\C-c@\"
748 'org-src-do-key-sequence-at-code-block)))
750 In that case, for example, C-c @ t issued in code edit buffers
751 would tangle the current Org code block, C-c @ e would execute
752 the block and C-c @ h would display the other available
754 (interactive "kOrg-babel key: ")
755 (if (equal key
(kbd "C-g")) (keyboard-quit)
757 (org-src-do-at-code-block
759 (lookup-key org-babel-map key
)))))
761 (defcustom org-src-tab-acts-natively nil
762 "If non-nil, the effect of TAB in a code block is as if it were
763 issued in the language major mode buffer."
768 (defun org-src-native-tab-command-maybe ()
769 "Perform language-specific TAB action.
770 Alter code block according to effect of TAB in the language major
772 (and org-src-tab-acts-natively
773 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
774 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
776 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
778 (defun org-src-font-lock-fontify-block (lang start end
)
780 This function is called by emacs automatic fontification, as long
781 as `org-src-fontify-natively' is non-nil. For manual
782 fontification of code blocks see `org-src-fontify-block' and
783 `org-src-fontify-buffer'"
784 (let ((lang-mode (org-src-get-lang-mode lang
)))
785 (if (fboundp lang-mode
)
786 (let ((string (buffer-substring-no-properties start end
))
787 (modified (buffer-modified-p))
788 (org-buffer (current-buffer)) pos next
)
789 (remove-text-properties start end
'(face nil
))
792 (concat " org-src-fontification:" (symbol-name lang-mode
)))
793 (delete-region (point-min) (point-max))
794 (insert (concat string
" ")) ;; so there's a final property change
795 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
796 (font-lock-fontify-buffer)
797 (setq pos
(point-min))
798 (while (setq next
(next-single-property-change pos
'face
))
800 (+ start
(1- pos
)) (+ start next
) 'face
801 (get-text-property pos
'face
) org-buffer
)
805 '(font-lock-fontified t fontified t font-lock-multiline t
))
806 (set-buffer-modified-p modified
)))))
808 (defun org-src-fontify-block ()
809 "Fontify code block at point."
812 (let ((org-src-fontify-natively t
)
813 (info (org-edit-src-find-region-and-lang)))
814 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
816 (defun org-src-fontify-buffer ()
817 "Fontify all code blocks in the current buffer"
819 (org-babel-map-src-blocks nil
820 (org-src-fontify-block)))
822 (defun org-src-get-lang-mode (lang)
823 "Return major mode that should be used for LANG.
824 LANG is a string, and the returned major mode is a symbol."
827 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
828 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
832 ;;; org-src.el ends here