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-strip-protective-commas
"org" (beg end
))
45 (declare-function org-pop-to-buffer-same-window
46 "org-compat" (&optional buffer-or-name norecord label
))
47 (declare-function org-strip-protective-commas
"org" (beg end
))
48 (declare-function org-base-buffer
"org" (buffer))
50 (defcustom org-edit-src-region-extra nil
51 "Additional regexps to identify regions for editing with `org-edit-src-code'.
52 For examples see the function `org-edit-src-find-region-and-lang'.
53 The regular expression identifying the begin marker should end with a newline,
54 and the regexp marking the end line should start with a newline, to make sure
55 there are kept outside the narrowed region."
56 :group
'org-edit-structure
59 (regexp :tag
"begin regexp")
60 (regexp :tag
"end regexp")
61 (choice :tag
"language"
62 (string :tag
"specify")
63 (integer :tag
"from match group")
64 (const :tag
"from `lang' element")
65 (const :tag
"from `style' element")))))
67 (defcustom org-coderef-label-format
"(ref:%s)"
68 "The default coderef format.
69 This format string will be used to search for coderef labels in literal
70 examples (EXAMPLE and SRC blocks). The format can be overwritten in
71 an individual literal example with the -l option, like
73 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
77 If you want to use this for HTML export, make sure that the format does
78 not introduce special font-locking, and avoid the HTML special
79 characters `<', `>', and `&'. The reason for this restriction is that
80 the labels are searched for only after htmlize has done its job."
81 :group
'org-edit-structure
; FIXME this is not in the right group
84 (defcustom org-edit-fixed-width-region-mode
'artist-mode
85 "The mode that should be used to edit fixed-width regions.
86 These are the regions where each line starts with a colon."
87 :group
'org-edit-structure
91 (const fundamental-mode
)
92 (function :tag
"Other (specify)")))
94 (defcustom org-src-preserve-indentation nil
95 "If non-nil preserve leading whitespace characters on export.
96 If non-nil leading whitespace characters in source code blocks
97 are preserved on export, and when switching between the org
98 buffer and the language mode edit buffer. If this variable is nil
99 then, after editing with \\[org-edit-src-code], the
100 minimum (across-lines) number of leading whitespace characters
101 are removed from all lines, and the code block is uniformly
102 indented according to the value of `org-edit-src-content-indentation'."
103 :group
'org-edit-structure
106 (defcustom org-edit-src-content-indentation
2
107 "Indentation for the content of a source code block.
108 This should be the number of spaces added to the indentation of the #+begin
109 line in order to compute the indentation of the block content after
110 editing it with \\[org-edit-src-code]. Has no effect if
111 `org-src-preserve-indentation' is non-nil."
112 :group
'org-edit-structure
115 (defvar org-src-strip-leading-and-trailing-blank-lines nil
116 "If non-nil, blank lines are removed when exiting the code edit
119 (defcustom org-edit-src-persistent-message t
120 "Non-nil means show persistent exit help message while editing src examples.
121 The message is shown in the header-line, which will be created in the
122 first line of the window showing the editing buffer."
123 :group
'org-edit-structure
126 (defcustom org-src-window-setup
'reorganize-frame
127 "How the source code edit buffer should be displayed.
128 Possible values for this option are:
130 current-window Show edit buffer in the current window, keeping all other
132 other-window Use `switch-to-buffer-other-window' to display edit buffer.
133 reorganize-frame Show only two windows on the current frame, the current
134 window and the edit buffer. When exiting the edit buffer,
135 return to one window.
136 other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
137 Also, when exiting the edit buffer, kill that frame."
138 :group
'org-edit-structure
140 (const current-window
)
143 (const reorganize-frame
)))
145 (defvar org-src-mode-hook nil
146 "Hook run after Org switched a source code snippet to its Emacs mode.
149 - when editing a source code snippet with \"C-c '\".
150 - When formatting a source code snippet for export with htmlize.
152 You may want to use this hook for example to turn off `outline-minor-mode'
153 or similar things which you want to have when editing a source code file,
154 but which mess up the display of a snippet in Org exported files.")
156 (defcustom org-src-lang-modes
157 '(("ocaml" . tuareg
) ("elisp" . emacs-lisp
) ("ditaa" . artist
)
158 ("asymptote" . asy
) ("dot" . fundamental
) ("sqlite" . sql
)
159 ("calc" . fundamental
) ("C" . c
) ("cpp" . c
++)
160 ("screen" . shell-script
))
161 "Alist mapping languages to their major mode.
162 The key is the language name, the value is the string that should
163 be inserted as the name of the major mode. For many languages this is
164 simple, but for language where this is not the case, this variable
165 provides a way to simplify things on the user side.
166 For example, there is no ocaml-mode in Emacs, but the mode to use is
168 :group
'org-edit-structure
171 (string "Language name")
172 (symbol "Major mode"))))
174 ;;; Editing source examples
176 (defvar org-src-mode-map
(make-sparse-keymap))
177 (define-key org-src-mode-map
"\C-c'" 'org-edit-src-exit
)
178 (define-key org-src-mode-map
"\C-x\C-s" 'org-edit-src-save
)
180 (defvar org-edit-src-force-single-line nil
)
181 (defvar org-edit-src-from-org-mode nil
)
182 (defvar org-edit-src-allow-write-back-p t
)
183 (defvar org-edit-src-picture nil
)
184 (defvar org-edit-src-beg-marker nil
)
185 (defvar org-edit-src-end-marker nil
)
186 (defvar org-edit-src-overlay nil
)
187 (defvar org-edit-src-block-indentation nil
)
188 (defvar org-edit-src-saved-temp-window-config nil
)
190 (defvar org-src-ask-before-returning-to-edit-buffer t
191 "If nil, when org-edit-src code is used on a block that already
192 has an active edit buffer, it will switch to that edit buffer
193 immediately; otherwise it will ask whether you want to return
194 to the existing edit buffer.")
196 (defvar org-src-babel-info nil
)
198 (define-minor-mode org-src-mode
199 "Minor mode for language major mode buffers generated by org.
200 This minor mode is turned on in two situations:
201 - when editing a source code snippet with \"C-c '\".
202 - When formatting a source code snippet for export with htmlize.
203 There is a mode hook, and keybindings for `org-edit-src-exit' and
204 `org-edit-src-save'")
206 (defun org-edit-src-code (&optional context code edit-buffer-name
)
207 "Edit the source code example at point.
208 The example is copied to a separate buffer, and that buffer is
209 switched to the correct language mode. When done, exit with
210 \\[org-edit-src-exit]. This will remove the original code in the
211 Org buffer, and replace it with the edited version. Optional
212 argument CONTEXT is used by \\[org-edit-src-save] when calling
213 this function. See \\[org-src-window-setup] to configure the
214 display of windows containing the Org buffer and the code
217 (unless (eq context
'save
)
218 (setq org-edit-src-saved-temp-window-config
(current-window-configuration)))
219 (let* ((mark (and (org-region-active-p) (mark)))
221 (info (org-edit-src-find-region-and-lang))
222 (full-info (org-babel-get-src-block-info 'light
))
223 (org-mode-p (derived-mode-p 'org-mode
)) ;; derived-mode-p is reflexive
226 (allow-write-back-p (null code
))
227 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
228 begline markline markcol line col transmitted-variables
)
231 (setq beg
(move-marker beg
(nth 0 info
))
232 end
(move-marker end
(nth 1 info
))
233 msg
(if allow-write-back-p
234 (substitute-command-keys
235 "Edit, then exit with C-c ' (C-c and single quote)")
236 "Exit with C-c ' (C-c and single quote)")
237 code
(or code
(buffer-substring-no-properties beg end
))
238 lang
(or (cdr (assoc (nth 2 info
) org-src-lang-modes
))
240 lang
(if (symbolp lang
) (symbol-name lang
) lang
)
242 block-nindent
(nth 5 info
)
243 lang-f
(intern (concat lang
"-mode"))
244 begline
(save-excursion (goto-char beg
) (org-current-line))
245 transmitted-variables
246 `((org-edit-src-content-indentation
247 ,org-edit-src-content-indentation
)
248 (org-edit-src-force-single-line ,single
)
249 (org-edit-src-from-org-mode ,org-mode-p
)
250 (org-edit-src-allow-write-back-p ,allow-write-back-p
)
251 (org-src-preserve-indentation ,org-src-preserve-indentation
)
252 (org-src-babel-info ,(org-babel-get-src-block-info 'light
))
253 (org-coderef-label-format
254 ,(or (nth 4 info
) org-coderef-label-format
))
255 (org-edit-src-beg-marker ,beg
)
256 (org-edit-src-end-marker ,end
)
257 (org-edit-src-block-indentation ,block-nindent
)))
258 (if (and mark
(>= mark beg
) (<= mark
(1+ end
)))
259 (save-excursion (goto-char (min mark end
))
260 (setq markline
(org-current-line)
261 markcol
(current-column))))
262 (if (equal lang-f
'table.el-mode
)
263 (setq lang-f
(lambda ()
265 (if (org-bound-and-true-p flyspell-mode
)
268 (org-set-local 'org-edit-src-content-indentation
0))))
269 (unless (functionp lang-f
)
270 (error "No such language mode: %s" lang-f
))
272 (if (> (point) end
) (goto-char end
))
273 (setq line
(org-current-line)
274 col
(current-column)))
275 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
276 (if org-src-ask-before-returning-to-edit-buffer
277 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t
))
278 (org-src-switch-to-buffer buffer
'return
)
280 (with-current-buffer buffer
281 (if (boundp 'org-edit-src-overlay
)
282 (delete-overlay org-edit-src-overlay
)))
283 (kill-buffer buffer
))
284 (setq buffer
(generate-new-buffer
286 (org-src-construct-edit-buffer-name (buffer-name) lang
))))
287 (setq ovl
(make-overlay beg end
))
288 (overlay-put ovl
'edit-buffer buffer
)
289 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
290 (overlay-put ovl
'face
'secondary-selection
)
293 (let ((map (make-sparse-keymap)))
294 (define-key map
[mouse-1
] 'org-edit-src-continue
)
296 (overlay-put ovl
:read-only
"Leave me alone")
297 (setq transmitted-variables
298 (append transmitted-variables
`((org-edit-src-overlay ,ovl
))))
299 (org-src-switch-to-buffer buffer
'edit
)
300 (if (eq single
'macro-definition
)
301 (setq code
(replace-regexp-in-string "\\\\n" "\n" code t t
)))
303 (remove-text-properties (point-min) (point-max)
304 '(display nil invisible nil intangible nil
))
305 (unless (cadr (assq 'org-src-preserve-indentation transmitted-variables
))
306 (setq total-nindent
(or (org-do-remove-indentation) 0)))
307 (let ((org-inhibit-startup t
))
311 (error "Language mode `%s' fails with: %S" lang-f
(nth 1 e
)))))
312 (dolist (pair transmitted-variables
)
313 (org-set-local (car pair
) (cadr pair
)))
314 (if (derived-mode-p 'org-mode
)
316 (goto-char (point-min))
317 (while (re-search-forward "^," nil t
)
318 (if (eq (org-current-line) line
) (setq total-nindent
(1+ total-nindent
)))
320 (org-strip-protective-commas (point-min) (point-max)))
322 (org-goto-line (1+ (- markline begline
)))
324 (if org-src-preserve-indentation markcol
325 (max 0 (- markcol total-nindent
))))
326 (push-mark (point) 'no-message t
)
327 (setq deactivate-mark nil
))
328 (org-goto-line (1+ (- line begline
)))
330 (if org-src-preserve-indentation col
(max 0 (- col total-nindent
))))
332 (set-buffer-modified-p nil
)
333 (setq buffer-file-name nil
)
334 (and org-edit-src-persistent-message
335 (org-set-local 'header-line-format msg
))
336 (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang
))))
337 (when (fboundp edit-prep-func
)
338 (funcall edit-prep-func full-info
))))
341 (defun org-edit-src-continue (e)
344 (let ((buf (get-char-property (point) 'edit-buffer
)))
345 (if buf
(org-src-switch-to-buffer buf
'continue
)
346 (error "Something is wrong here"))))
348 (defun org-src-switch-to-buffer (buffer context
)
349 (case org-src-window-setup
351 (org-pop-to-buffer-same-window buffer
))
353 (switch-to-buffer-other-window buffer
))
357 (let ((frame (selected-frame)))
358 (switch-to-buffer-other-frame buffer
)
359 (delete-frame frame
)))
361 (kill-buffer (current-buffer))
362 (org-pop-to-buffer-same-window buffer
))
364 (switch-to-buffer-other-frame buffer
))))
366 (if (eq context
'edit
) (delete-other-windows))
367 (org-switch-to-buffer-other-window buffer
)
368 (if (eq context
'exit
) (delete-other-windows)))
372 (message "Invalid value %s for org-src-window-setup"
373 (symbol-name org-src-window-setup
))
374 (org-pop-to-buffer-same-window buffer
))))
376 (defun org-src-construct-edit-buffer-name (org-buffer-name lang
)
377 "Construct the buffer name for a source editing buffer."
378 (concat "*Org Src " org-buffer-name
"[ " lang
" ]*"))
380 (defun org-src-edit-buffer-p (&optional buffer
)
381 "Test whether BUFFER (or the current buffer if BUFFER is nil)
382 is a source block editing buffer."
383 (let ((buffer (org-base-buffer (or buffer
(current-buffer)))))
384 (and (buffer-name buffer
)
385 (string-match "\\`*Org Src " (buffer-name buffer
))
386 (local-variable-p 'org-edit-src-beg-marker buffer
)
387 (local-variable-p 'org-edit-src-end-marker buffer
))))
389 (defun org-edit-src-find-buffer (beg end
)
390 "Find a source editing buffer that is already editing the region BEG to END."
394 (with-current-buffer b
395 (if (and (string-match "\\`*Org Src " (buffer-name))
396 (local-variable-p 'org-edit-src-beg-marker
(current-buffer))
397 (local-variable-p 'org-edit-src-end-marker
(current-buffer))
398 (equal beg org-edit-src-beg-marker
)
399 (equal end org-edit-src-end-marker
))
400 (throw 'exit
(current-buffer)))))
404 (defun org-edit-fixed-width-region ()
405 "Edit the fixed-width ascii drawing at point.
406 This must be a region where each line starts with a colon followed by
408 An new buffer is created and the fixed-width region is copied into it,
409 and the buffer is switched into `artist-mode' for editing. When done,
410 exit with \\[org-edit-src-exit]. The edited text will then replace
411 the fragment in the Org-mode buffer."
413 (let ((line (org-current-line))
414 (col (current-column))
416 (msg (substitute-command-keys
417 "Edit, then exit with C-c ' (C-c and single quote)"))
418 (org-mode-p (derived-mode-p 'org-mode
))
421 (preserve-indentation org-src-preserve-indentation
)
422 block-nindent ovl beg1 end1 code begline buffer
)
423 (beginning-of-line 1)
424 (if (looking-at "[ \t]*[^:\n \t]")
426 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
427 (setq beg1
(point) end1 beg1
)
429 (if (re-search-backward "^[ \t]*[^: \t]" nil
'move
)
430 (setq beg1
(point-at-bol 2))
431 (setq beg1
(point))))
433 (if (re-search-forward "^[ \t]*[^: \t]" nil
'move
)
434 (setq end1
(1- (match-beginning 0)))
435 (setq end1
(point))))
436 (org-goto-line line
))
437 (setq beg
(move-marker beg beg1
)
438 end
(move-marker end end1
)
439 code
(buffer-substring-no-properties beg end
)
440 begline
(save-excursion (goto-char beg
) (org-current-line)))
441 (if (and (setq buffer
(org-edit-src-find-buffer beg end
))
442 (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
443 (org-pop-to-buffer-same-window buffer
)
445 (with-current-buffer buffer
446 (if (boundp 'org-edit-src-overlay
)
447 (delete-overlay org-edit-src-overlay
)))
448 (kill-buffer buffer
))
449 (setq buffer
(generate-new-buffer
450 (org-src-construct-edit-buffer-name
451 (buffer-name) "Fixed Width")))
452 (setq ovl
(make-overlay beg end
))
453 (overlay-put ovl
'face
'secondary-selection
)
454 (overlay-put ovl
'edit-buffer buffer
)
455 (overlay-put ovl
'help-echo
"Click with mouse-1 to switch to buffer editing this segment")
456 (overlay-put ovl
'face
'secondary-selection
)
459 (let ((map (make-sparse-keymap)))
460 (define-key map
[mouse-1
] 'org-edit-src-continue
)
462 (overlay-put ovl
:read-only
"Leave me alone")
463 (org-pop-to-buffer-same-window buffer
)
465 (remove-text-properties (point-min) (point-max)
466 '(display nil invisible nil intangible nil
))
467 (setq block-nindent
(or (org-do-remove-indentation) 0))
469 ((eq org-edit-fixed-width-region-mode
'artist-mode
)
472 (t (funcall org-edit-fixed-width-region-mode
)))
473 (set (make-local-variable 'org-edit-src-force-single-line
) nil
)
474 (set (make-local-variable 'org-edit-src-from-org-mode
) org-mode-p
)
475 (set (make-local-variable 'org-edit-src-picture
) t
)
476 (goto-char (point-min))
477 (while (re-search-forward "^[ \t]*: ?" nil t
)
479 (org-goto-line (1+ (- line begline
)))
480 (org-move-to-column (max 0 (- col block-nindent
2)))
481 (org-set-local 'org-edit-src-beg-marker beg
)
482 (org-set-local 'org-edit-src-end-marker end
)
483 (org-set-local 'org-edit-src-overlay ovl
)
484 (org-set-local 'org-edit-src-block-indentation block-nindent
)
485 (org-set-local 'org-edit-src-content-indentation
0)
486 (org-set-local 'org-src-preserve-indentation nil
)
488 (set-buffer-modified-p nil
)
489 (and org-edit-src-persistent-message
490 (org-set-local 'header-line-format msg
)))
494 (defun org-edit-src-find-region-and-lang ()
495 "Find the region and language for a local edit.
496 Return a list with beginning and end of the region, a string representing
497 the language, a switch telling if the content should be in a single line."
500 org-edit-src-region-extra
502 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang
)
503 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style
)
504 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
505 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
506 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
507 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
508 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
509 ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
510 ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
511 ("^[ \t]*#\\+html:" "\n" "html" single-line
)
512 ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
513 ("^[ \t]*#\\+latex:" "\n" "latex" single-line
)
514 ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
515 ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line
)
516 ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
517 ("^[ \t]*#\\+docbook:" "\n" "xml" single-line
)
518 ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
519 "\n" "fundamental" macro-definition
)
520 ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
523 re1 re2 single beg end lang lfmt match-re1 ind entry
)
525 (while (setq entry
(pop re-list
))
526 (setq re1
(car entry
) re2
(nth 1 entry
) lang
(nth 2 entry
)
527 single
(nth 3 entry
))
529 (if (or (looking-at re1
)
530 (re-search-backward re1 nil t
))
532 (setq match-re1
(match-string 0))
533 (setq beg
(match-end 0)
534 lang
(org-edit-src-get-lang lang
)
535 lfmt
(org-edit-src-get-label-format match-re1
)
536 ind
(org-edit-src-get-indentation (match-beginning 0)))
537 (if (and (re-search-forward re2 nil t
)
538 (>= (match-end 0) pos
))
539 (throw 'exit
(list beg
(match-beginning 0)
540 lang single lfmt ind
))))
541 (if (or (looking-at re2
)
542 (re-search-forward re2 nil t
))
544 (setq end
(match-beginning 0))
545 (if (and (re-search-backward re1 nil t
)
546 (<= (match-beginning 0) pos
))
548 (setq lfmt
(org-edit-src-get-label-format
550 ind
(org-edit-src-get-indentation
551 (match-beginning 0)))
553 (list (match-end 0) end
554 (org-edit-src-get-lang lang
)
555 single lfmt ind
)))))))))
556 (when (org-at-table.el-p
)
557 (re-search-backward "^[\t]*[^ \t|\\+]" nil t
)
558 (setq beg
(1+ (point-at-eol)))
560 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t
)
561 (progn (goto-char (point-max)) (newline)))
562 (setq end
(point-at-bol))
563 (setq ind
(org-edit-src-get-indentation beg
))
564 (throw 'exit
(list beg end
'table.el nil nil ind
))))))
566 (defun org-edit-src-get-lang (lang)
567 "Extract the src language."
568 (let ((m (match-string 0)))
570 ((stringp lang
) lang
)
571 ((integerp lang
) (match-string lang
))
572 ((and (eq lang
'lang
)
573 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m
))
575 ((and (eq lang
'style
)
576 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m
))
580 (defun org-edit-src-get-label-format (s)
581 "Extract the label format."
583 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s
)
584 (match-string 1 s
))))
586 (defun org-edit-src-get-indentation (pos)
587 "Count leading whitespace characters on line."
590 (org-get-indentation)))
592 (defun org-edit-src-exit (&optional context
)
593 "Exit special edit and protect problematic lines."
595 (unless (org-bound-and-true-p org-edit-src-from-org-mode
)
596 (error "This is not a sub-editing buffer, something is wrong"))
598 (let* ((beg org-edit-src-beg-marker
)
599 (end org-edit-src-end-marker
)
600 (ovl org-edit-src-overlay
)
601 (buffer (current-buffer))
602 (single (org-bound-and-true-p org-edit-src-force-single-line
))
603 (macro (eq single
'macro-definition
))
604 (total-nindent (+ (or org-edit-src-block-indentation
0)
605 org-edit-src-content-indentation
))
606 (preserve-indentation org-src-preserve-indentation
)
607 (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p
))
608 (delta 0) code line col indent
)
609 (when allow-write-back-p
610 (unless preserve-indentation
(untabify (point-min) (point-max)))
611 (if org-src-strip-leading-and-trailing-blank-lines
613 (goto-char (point-min))
614 (if (looking-at "[ \t\n]*\n") (replace-match ""))
616 (if (re-search-forward "\n[ \t\n]*\\'" nil t
) (replace-match ""))))))
617 (setq line
(if (org-bound-and-true-p org-edit-src-force-single-line
)
620 col
(current-column))
621 (when allow-write-back-p
623 (goto-char (point-min))
624 (if (re-search-forward "\\s-+\\'" nil t
) (replace-match ""))
625 (goto-char (point-min))
627 (while (re-search-forward "\n" nil t
)
629 (replace-match (if macro
"\\n" " ") t t
))
630 (when (and macro
(> cnt
0))
631 (goto-char (point-max)) (insert "\\n")))
632 (goto-char (point-min))
633 (if (looking-at "\\s-*") (replace-match " ")))
634 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
635 (goto-char (point-min))
636 (while (re-search-forward
637 (if (derived-mode-p 'org-mode
) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t
)
638 (if (eq (org-current-line) line
) (setq delta
(1+ delta
)))
639 (replace-match ",\\1")))
640 (when (org-bound-and-true-p org-edit-src-picture
)
641 (setq preserve-indentation nil
)
642 (untabify (point-min) (point-max))
643 (goto-char (point-min))
644 (while (re-search-forward "^" nil t
)
645 (replace-match ": ")))
646 (unless (or single preserve-indentation
(= total-nindent
0))
647 (setq indent
(make-string total-nindent ?\
))
648 (goto-char (point-min))
649 (while (re-search-forward "^" nil t
)
650 (replace-match indent
)))
651 (if (org-bound-and-true-p org-edit-src-picture
)
652 (setq total-nindent
(+ total-nindent
2)))
653 (setq code
(buffer-string))
654 (set-buffer-modified-p nil
))
655 (org-src-switch-to-buffer (marker-buffer beg
) (or context
'exit
))
658 (when allow-write-back-p
659 (delete-region beg end
)
662 (if single
(just-one-space)))
663 (if (memq t
(mapcar (lambda (overlay)
664 (eq (overlay-get overlay
'invisible
)
666 (overlays-at (point))))
667 ;; Block is hidden; put point at start of block
668 (beginning-of-line 0)
669 ;; Block is visible, put point where it was in the code buffer
670 (org-goto-line (1- (+ (org-current-line) line
)))
671 (org-move-to-column (if preserve-indentation col
(+ col total-nindent delta
))))
672 (move-marker beg nil
)
673 (move-marker end nil
))
674 (unless (eq context
'save
)
675 (when org-edit-src-saved-temp-window-config
676 (set-window-configuration org-edit-src-saved-temp-window-config
)
677 (setq org-edit-src-saved-temp-window-config nil
))))
679 (defmacro org-src-in-org-buffer
(&rest body
)
680 `(let ((p (point)) (m (mark)) (ul buffer-undo-list
) msg
)
681 (save-window-excursion
682 (org-edit-src-exit 'save
)
684 (setq msg
(current-message))
685 (if (eq org-src-window-setup
'other-frame
)
686 (let ((org-src-window-setup 'current-window
))
687 (org-edit-src-code 'save
))
688 (org-edit-src-code 'save
)))
689 (setq buffer-undo-list ul
)
690 (push-mark m
'nomessage
)
691 (goto-char (min p
(point-max)))
692 (message (or msg
""))))
693 (def-edebug-spec org-src-in-org-buffer
(body))
695 (defun org-edit-src-save ()
696 "Save parent buffer with current state source-code buffer."
698 (org-src-in-org-buffer (save-buffer)))
700 (declare-function org-babel-tangle
"ob-tangle" (&optional only-this-block target-file lang
))
702 (defun org-src-tangle (arg)
703 "Tangle the parent buffer."
705 (org-src-in-org-buffer (org-babel-tangle arg
)))
707 (defun org-src-mode-configure-edit-buffer ()
708 (when (org-bound-and-true-p org-edit-src-from-org-mode
)
709 (org-add-hook 'kill-buffer-hook
710 #'(lambda () (delete-overlay org-edit-src-overlay
)) nil
'local
)
711 (if (org-bound-and-true-p org-edit-src-allow-write-back-p
)
713 (setq buffer-offer-save t
)
714 (setq buffer-file-name
715 (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker
))
716 "[" (buffer-name) "]"))
717 (if (featurep 'xemacs
)
719 (make-variable-buffer-local 'write-contents-hooks
) ; needed only for 21.4
720 (setq write-contents-hooks
'(org-edit-src-save)))
721 (setq write-contents-functions
'(org-edit-src-save))))
722 (setq buffer-read-only t
))))
724 (org-add-hook 'org-src-mode-hook
'org-src-mode-configure-edit-buffer
)
727 (defun org-src-associate-babel-session (info)
728 "Associate edit buffer with comint session."
730 (let ((session (cdr (assoc :session
(nth 2 info
)))))
731 (and session
(not (string= session
"none"))
732 (org-babel-comint-buffer-livep session
)
733 ((lambda (f) (and (fboundp f
) (funcall f session
)))
734 (intern (format "org-babel-%s-associate-session" (nth 0 info
)))))))
736 (defun org-src-babel-configure-edit-buffer ()
737 (when org-src-babel-info
738 (org-src-associate-babel-session org-src-babel-info
)))
740 (org-add-hook 'org-src-mode-hook
'org-src-babel-configure-edit-buffer
)
741 (defmacro org-src-do-at-code-block
(&rest body
)
742 "Execute a command from an edit buffer in the Org-mode buffer."
743 `(let ((beg-marker org-edit-src-beg-marker
))
745 (with-current-buffer (marker-buffer beg-marker
)
746 (goto-char (marker-position beg-marker
))
748 (def-edebug-spec org-src-do-at-code-block
(body))
750 (defun org-src-do-key-sequence-at-code-block (&optional key
)
751 "Execute key sequence at code block in the source Org buffer.
752 The command bound to KEY in the Org-babel key map is executed
753 remotely with point temporarily at the start of the code block in
756 This command is not bound to a key by default, to avoid conflicts
757 with language major mode bindings. To bind it to C-c @ in all
758 language major modes, you could use
760 (add-hook 'org-src-mode-hook
761 (lambda () (define-key org-src-mode-map \"\\C-c@\"
762 'org-src-do-key-sequence-at-code-block)))
764 In that case, for example, C-c @ t issued in code edit buffers
765 would tangle the current Org code block, C-c @ e would execute
766 the block and C-c @ h would display the other available
768 (interactive "kOrg-babel key: ")
769 (if (equal key
(kbd "C-g")) (keyboard-quit)
771 (org-src-do-at-code-block
773 (lookup-key org-babel-map key
)))))
775 (defcustom org-src-tab-acts-natively nil
776 "If non-nil, the effect of TAB in a code block is as if it were
777 issued in the language major mode buffer."
782 (defun org-src-native-tab-command-maybe ()
783 "Perform language-specific TAB action.
784 Alter code block according to effect of TAB in the language major
786 (and org-src-tab-acts-natively
787 (let ((org-src-strip-leading-and-trailing-blank-lines nil
))
788 (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))))
790 (add-hook 'org-tab-first-hook
'org-src-native-tab-command-maybe
)
792 (defun org-src-font-lock-fontify-block (lang start end
)
794 This function is called by emacs automatic fontification, as long
795 as `org-src-fontify-natively' is non-nil. For manual
796 fontification of code blocks see `org-src-fontify-block' and
797 `org-src-fontify-buffer'"
798 (let ((lang-mode (org-src-get-lang-mode lang
)))
799 (if (fboundp lang-mode
)
800 (let ((string (buffer-substring-no-properties start end
))
801 (modified (buffer-modified-p))
802 (org-buffer (current-buffer)) pos next
)
803 (remove-text-properties start end
'(face nil
))
806 (concat " org-src-fontification:" (symbol-name lang-mode
)))
807 (delete-region (point-min) (point-max))
808 (insert (concat string
" ")) ;; so there's a final property change
809 (unless (eq major-mode lang-mode
) (funcall lang-mode
))
810 (font-lock-fontify-buffer)
811 (setq pos
(point-min))
812 (while (setq next
(next-single-property-change pos
'face
))
814 (+ start
(1- pos
)) (+ start next
) 'face
815 (get-text-property pos
'face
) org-buffer
)
819 '(font-lock-fontified t fontified t font-lock-multiline t
))
820 (set-buffer-modified-p modified
)))))
822 (defun org-src-fontify-block ()
823 "Fontify code block at point."
826 (let ((org-src-fontify-natively t
)
827 (info (org-edit-src-find-region-and-lang)))
828 (font-lock-fontify-region (nth 0 info
) (nth 1 info
)))))
830 (defun org-src-fontify-buffer ()
831 "Fontify all code blocks in the current buffer"
833 (org-babel-map-src-blocks nil
834 (org-src-fontify-block)))
836 (defun org-src-get-lang-mode (lang)
837 "Return major mode that should be used for LANG.
838 LANG is a string, and the returned major mode is a symbol."
841 ((lambda (l) (if (symbolp l
) (symbol-name l
) l
))
842 (or (cdr (assoc lang org-src-lang-modes
)) lang
)) "-mode")))
846 ;;; org-src.el ends here