diff options
| author | Carsten Dominik | 2010-11-11 22:10:19 -0600 |
|---|---|---|
| committer | Carsten Dominik | 2010-11-11 22:10:19 -0600 |
| commit | afe98dfa700de5cf0493e8bf95b7d894e2734e47 (patch) | |
| tree | 92a812b353bb09c1286e8a44fb552de9f1af3384 /lisp/org/org-src.el | |
| parent | df26e1f58a7e484b7ed500ea48d0e1c49345ffbf (diff) | |
| download | emacs-afe98dfa700de5cf0493e8bf95b7d894e2734e47.tar.gz emacs-afe98dfa700de5cf0493e8bf95b7d894e2734e47.zip | |
Install org-mode version 7.3
Diffstat (limited to 'lisp/org/org-src.el')
| -rw-r--r-- | lisp/org/org-src.el | 189 |
1 files changed, 170 insertions, 19 deletions
diff --git a/lisp/org/org-src.el b/lisp/org/org-src.el index 453f3b0b534..c4f0065ec34 100644 --- a/lisp/org/org-src.el +++ b/lisp/org/org-src.el | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | ;; Dan Davison <davison at stats dot ox dot ac dot uk> | 8 | ;; Dan Davison <davison at stats dot ox dot ac dot uk> |
| 9 | ;; Keywords: outlines, hypermedia, calendar, wp | 9 | ;; Keywords: outlines, hypermedia, calendar, wp |
| 10 | ;; Homepage: http://orgmode.org | 10 | ;; Homepage: http://orgmode.org |
| 11 | ;; Version: 7.01 | 11 | ;; Version: 7.3 |
| 12 | ;; | 12 | ;; |
| 13 | ;; This file is part of GNU Emacs. | 13 | ;; This file is part of GNU Emacs. |
| 14 | ;; | 14 | ;; |
| @@ -34,6 +34,8 @@ | |||
| 34 | 34 | ||
| 35 | (require 'org-macs) | 35 | (require 'org-macs) |
| 36 | (require 'org-compat) | 36 | (require 'org-compat) |
| 37 | (require 'ob-keys) | ||
| 38 | (require 'ob-comint) | ||
| 37 | (eval-when-compile | 39 | (eval-when-compile |
| 38 | (require 'cl)) | 40 | (require 'cl)) |
| 39 | 41 | ||
| @@ -107,6 +109,10 @@ editing it with \\[org-edit-src-code]. Has no effect if | |||
| 107 | :group 'org-edit-structure | 109 | :group 'org-edit-structure |
| 108 | :type 'integer) | 110 | :type 'integer) |
| 109 | 111 | ||
| 112 | (defvar org-src-strip-leading-and-trailing-blank-lines nil | ||
| 113 | "If non-nil, blank lines are removed when exiting the code edit | ||
| 114 | buffer.") | ||
| 115 | |||
| 110 | (defcustom org-edit-src-persistent-message t | 116 | (defcustom org-edit-src-persistent-message t |
| 111 | "Non-nil means show persistent exit help message while editing src examples. | 117 | "Non-nil means show persistent exit help message while editing src examples. |
| 112 | The message is shown in the header-line, which will be created in the | 118 | The message is shown in the header-line, which will be created in the |
| @@ -147,7 +153,8 @@ but which mess up the display of a snippet in Org exported files.") | |||
| 147 | 153 | ||
| 148 | (defcustom org-src-lang-modes | 154 | (defcustom org-src-lang-modes |
| 149 | '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist) | 155 | '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist) |
| 150 | ("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql)) | 156 | ("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql) |
| 157 | ("calc" . fundamental)) | ||
| 151 | "Alist mapping languages to their major mode. | 158 | "Alist mapping languages to their major mode. |
| 152 | The key is the language name, the value is the string that should | 159 | The key is the language name, the value is the string that should |
| 153 | be inserted as the name of the major mode. For many languages this is | 160 | be inserted as the name of the major mode. For many languages this is |
| @@ -165,6 +172,7 @@ For example, there is no ocaml-mode in Emacs, but the mode to use is | |||
| 165 | 172 | ||
| 166 | (defvar org-src-mode-map (make-sparse-keymap)) | 173 | (defvar org-src-mode-map (make-sparse-keymap)) |
| 167 | (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit) | 174 | (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit) |
| 175 | |||
| 168 | (defvar org-edit-src-force-single-line nil) | 176 | (defvar org-edit-src-force-single-line nil) |
| 169 | (defvar org-edit-src-from-org-mode nil) | 177 | (defvar org-edit-src-from-org-mode nil) |
| 170 | (defvar org-edit-src-allow-write-back-p t) | 178 | (defvar org-edit-src-allow-write-back-p t) |
| @@ -181,6 +189,8 @@ For example, there is no ocaml-mode in Emacs, but the mode to use is | |||
| 181 | immediately; otherwise it will ask whether you want to return | 189 | immediately; otherwise it will ask whether you want to return |
| 182 | to the existing edit buffer.") | 190 | to the existing edit buffer.") |
| 183 | 191 | ||
| 192 | (defvar org-src-babel-info nil) | ||
| 193 | |||
| 184 | (define-minor-mode org-src-mode | 194 | (define-minor-mode org-src-mode |
| 185 | "Minor mode for language major mode buffers generated by org. | 195 | "Minor mode for language major mode buffers generated by org. |
| 186 | This minor mode is turned on in two situations: | 196 | This minor mode is turned on in two situations: |
| @@ -189,26 +199,30 @@ This minor mode is turned on in two situations: | |||
| 189 | There is a mode hook, and keybindings for `org-edit-src-exit' and | 199 | There is a mode hook, and keybindings for `org-edit-src-exit' and |
| 190 | `org-edit-src-save'") | 200 | `org-edit-src-save'") |
| 191 | 201 | ||
| 192 | (defun org-edit-src-code (&optional context code edit-buffer-name) | 202 | (defun org-edit-src-code (&optional context code edit-buffer-name quietp) |
| 193 | "Edit the source code example at point. | 203 | "Edit the source code example at point. |
| 194 | The example is copied to a separate buffer, and that buffer is switched | 204 | The example is copied to a separate buffer, and that buffer is |
| 195 | to the correct language mode. When done, exit with \\[org-edit-src-exit]. | 205 | switched to the correct language mode. When done, exit with |
| 196 | This will remove the original code in the Org buffer, and replace it with | 206 | \\[org-edit-src-exit]. This will remove the original code in the |
| 197 | the edited version. Optional argument CONTEXT is used by | 207 | Org buffer, and replace it with the edited version. Optional |
| 198 | \\[org-edit-src-save] when calling this function." | 208 | argument CONTEXT is used by \\[org-edit-src-save] when calling |
| 209 | this function. See \\[org-src-window-setup] to configure the | ||
| 210 | display of windows containing the Org buffer and the code | ||
| 211 | buffer." | ||
| 199 | (interactive) | 212 | (interactive) |
| 200 | (unless (eq context 'save) | 213 | (unless (eq context 'save) |
| 201 | (setq org-edit-src-saved-temp-window-config (current-window-configuration))) | 214 | (setq org-edit-src-saved-temp-window-config (current-window-configuration))) |
| 202 | (let ((line (org-current-line)) | 215 | (let ((mark (and (org-region-active-p) (mark))) |
| 203 | (col (current-column)) | ||
| 204 | (case-fold-search t) | 216 | (case-fold-search t) |
| 205 | (info (org-edit-src-find-region-and-lang)) | 217 | (info (org-edit-src-find-region-and-lang)) |
| 218 | (babel-info (org-babel-get-src-block-info 'light)) | ||
| 206 | (org-mode-p (eq major-mode 'org-mode)) | 219 | (org-mode-p (eq major-mode 'org-mode)) |
| 207 | (beg (make-marker)) | 220 | (beg (make-marker)) |
| 208 | (end (make-marker)) | 221 | (end (make-marker)) |
| 209 | (preserve-indentation org-src-preserve-indentation) | 222 | (preserve-indentation org-src-preserve-indentation) |
| 210 | (allow-write-back-p (null code)) | 223 | (allow-write-back-p (null code)) |
| 211 | block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg) | 224 | block-nindent total-nindent ovl lang lang-f single lfmt buffer msg |
| 225 | begline markline markcol line col) | ||
| 212 | (if (not info) | 226 | (if (not info) |
| 213 | nil | 227 | nil |
| 214 | (setq beg (move-marker beg (nth 0 info)) | 228 | (setq beg (move-marker beg (nth 0 info)) |
| @@ -226,6 +240,10 @@ the edited version. Optional argument CONTEXT is used by | |||
| 226 | block-nindent (nth 5 info) | 240 | block-nindent (nth 5 info) |
| 227 | lang-f (intern (concat lang "-mode")) | 241 | lang-f (intern (concat lang "-mode")) |
| 228 | begline (save-excursion (goto-char beg) (org-current-line))) | 242 | begline (save-excursion (goto-char beg) (org-current-line))) |
| 243 | (if (and mark (>= mark beg) (<= mark end)) | ||
| 244 | (save-excursion (goto-char mark) | ||
| 245 | (setq markline (org-current-line) | ||
| 246 | markcol (current-column)))) | ||
| 229 | (if (equal lang-f 'table.el-mode) | 247 | (if (equal lang-f 'table.el-mode) |
| 230 | (setq lang-f (lambda () | 248 | (setq lang-f (lambda () |
| 231 | (text-mode) | 249 | (text-mode) |
| @@ -235,7 +253,10 @@ the edited version. Optional argument CONTEXT is used by | |||
| 235 | (org-set-local 'org-edit-src-content-indentation 0)))) | 253 | (org-set-local 'org-edit-src-content-indentation 0)))) |
| 236 | (unless (functionp lang-f) | 254 | (unless (functionp lang-f) |
| 237 | (error "No such language mode: %s" lang-f)) | 255 | (error "No such language mode: %s" lang-f)) |
| 238 | (org-goto-line line) | 256 | (save-excursion |
| 257 | (if (> (point) end) (goto-char end)) | ||
| 258 | (setq line (org-current-line) | ||
| 259 | col (current-column))) | ||
| 239 | (if (and (setq buffer (org-edit-src-find-buffer beg end)) | 260 | (if (and (setq buffer (org-edit-src-find-buffer beg end)) |
| 240 | (if org-src-ask-before-returning-to-edit-buffer | 261 | (if org-src-ask-before-returning-to-edit-buffer |
| 241 | (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t)) | 262 | (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t)) |
| @@ -267,11 +288,16 @@ the edited version. Optional argument CONTEXT is used by | |||
| 267 | (unless preserve-indentation | 288 | (unless preserve-indentation |
| 268 | (setq total-nindent (or (org-do-remove-indentation) 0))) | 289 | (setq total-nindent (or (org-do-remove-indentation) 0))) |
| 269 | (let ((org-inhibit-startup t)) | 290 | (let ((org-inhibit-startup t)) |
| 270 | (funcall lang-f)) | 291 | (condition-case e |
| 292 | (funcall lang-f) | ||
| 293 | (error | ||
| 294 | (error "Language mode `%s' fails with: %S" lang-f (nth 1 e))))) | ||
| 271 | (set (make-local-variable 'org-edit-src-force-single-line) single) | 295 | (set (make-local-variable 'org-edit-src-force-single-line) single) |
| 272 | (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p) | 296 | (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p) |
| 273 | (set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p) | 297 | (set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p) |
| 274 | (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation) | 298 | (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation) |
| 299 | (when babel-info | ||
| 300 | (set (make-local-variable 'org-src-babel-info) babel-info)) | ||
| 275 | (when lfmt | 301 | (when lfmt |
| 276 | (set (make-local-variable 'org-coderef-label-format) lfmt)) | 302 | (set (make-local-variable 'org-coderef-label-format) lfmt)) |
| 277 | (when org-mode-p | 303 | (when org-mode-p |
| @@ -279,6 +305,12 @@ the edited version. Optional argument CONTEXT is used by | |||
| 279 | (while (re-search-forward "^," nil t) | 305 | (while (re-search-forward "^," nil t) |
| 280 | (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent))) | 306 | (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent))) |
| 281 | (replace-match ""))) | 307 | (replace-match ""))) |
| 308 | (when markline | ||
| 309 | (org-goto-line (1+ (- markline begline))) | ||
| 310 | (org-move-to-column | ||
| 311 | (if preserve-indentation markcol (max 0 (- markcol total-nindent)))) | ||
| 312 | (push-mark (point) 'no-message t) | ||
| 313 | (setq deactivate-mark nil)) | ||
| 282 | (org-goto-line (1+ (- line begline))) | 314 | (org-goto-line (1+ (- line begline))) |
| 283 | (org-move-to-column | 315 | (org-move-to-column |
| 284 | (if preserve-indentation col (max 0 (- col total-nindent)))) | 316 | (if preserve-indentation col (max 0 (- col total-nindent)))) |
| @@ -290,7 +322,7 @@ the edited version. Optional argument CONTEXT is used by | |||
| 290 | (set-buffer-modified-p nil) | 322 | (set-buffer-modified-p nil) |
| 291 | (and org-edit-src-persistent-message | 323 | (and org-edit-src-persistent-message |
| 292 | (org-set-local 'header-line-format msg))) | 324 | (org-set-local 'header-line-format msg))) |
| 293 | (message "%s" msg) | 325 | (unless quietp (message "%s" msg)) |
| 294 | t))) | 326 | t))) |
| 295 | 327 | ||
| 296 | (defun org-edit-src-continue (e) | 328 | (defun org-edit-src-continue (e) |
| @@ -321,6 +353,8 @@ the edited version. Optional argument CONTEXT is used by | |||
| 321 | (if (eq context 'edit) (delete-other-windows)) | 353 | (if (eq context 'edit) (delete-other-windows)) |
| 322 | (org-switch-to-buffer-other-window buffer) | 354 | (org-switch-to-buffer-other-window buffer) |
| 323 | (if (eq context 'exit) (delete-other-windows))) | 355 | (if (eq context 'exit) (delete-other-windows))) |
| 356 | ('switch-invisibly | ||
| 357 | (set-buffer buffer)) | ||
| 324 | (t | 358 | (t |
| 325 | (message "Invalid value %s for org-src-window-setup" | 359 | (message "Invalid value %s for org-src-window-setup" |
| 326 | (symbol-name org-src-window-setup)) | 360 | (symbol-name org-src-window-setup)) |
| @@ -552,11 +586,12 @@ the language, a switch telling if the content should be in a single line." | |||
| 552 | (delta 0) code line col indent) | 586 | (delta 0) code line col indent) |
| 553 | (when allow-write-back-p | 587 | (when allow-write-back-p |
| 554 | (unless preserve-indentation (untabify (point-min) (point-max))) | 588 | (unless preserve-indentation (untabify (point-min) (point-max))) |
| 555 | (save-excursion | 589 | (if org-src-strip-leading-and-trailing-blank-lines |
| 556 | (goto-char (point-min)) | 590 | (save-excursion |
| 557 | (if (looking-at "[ \t\n]*\n") (replace-match "")) | 591 | (goto-char (point-min)) |
| 558 | (unless macro | 592 | (if (looking-at "[ \t\n]*\n") (replace-match "")) |
| 559 | (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))))) | 593 | (unless macro |
| 594 | (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))))) | ||
| 560 | (setq line (if (org-bound-and-true-p org-edit-src-force-single-line) | 595 | (setq line (if (org-bound-and-true-p org-edit-src-force-single-line) |
| 561 | 1 | 596 | 1 |
| 562 | (org-current-line)) | 597 | (org-current-line)) |
| @@ -654,6 +689,122 @@ the language, a switch telling if the content should be in a single line." | |||
| 654 | 689 | ||
| 655 | (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer) | 690 | (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer) |
| 656 | 691 | ||
| 692 | |||
| 693 | (defun org-src-associate-babel-session (info) | ||
| 694 | "Associate edit buffer with comint session." | ||
| 695 | (interactive) | ||
| 696 | (let ((session (cdr (assoc :session (nth 2 info))))) | ||
| 697 | (and session (not (string= session "none")) | ||
| 698 | (org-babel-comint-buffer-livep session) | ||
| 699 | ((lambda (f) (and (fboundp f) (funcall f session))) | ||
| 700 | (intern (format "org-babel-%s-associate-session" (nth 0 info))))))) | ||
| 701 | |||
| 702 | (defun org-src-babel-configure-edit-buffer () | ||
| 703 | (when org-src-babel-info | ||
| 704 | (org-src-associate-babel-session org-src-babel-info))) | ||
| 705 | |||
| 706 | (org-add-hook 'org-src-mode-hook 'org-src-babel-configure-edit-buffer) | ||
| 707 | (defmacro org-src-do-at-code-block (&rest body) | ||
| 708 | "Execute a command from an edit buffer in the Org-mode buffer." | ||
| 709 | `(let ((beg-marker org-edit-src-beg-marker)) | ||
| 710 | (if beg-marker | ||
| 711 | (with-current-buffer (marker-buffer beg-marker) | ||
| 712 | (goto-char (marker-position beg-marker)) | ||
| 713 | ,@body)))) | ||
| 714 | |||
| 715 | (defun org-src-do-key-sequence-at-code-block (&optional key) | ||
| 716 | "Execute key sequence at code block in the source Org buffer. | ||
| 717 | The command bound to KEY in the Org-babel key map is executed | ||
| 718 | remotely with point temporarily at the start of the code block in | ||
| 719 | the Org buffer. | ||
| 720 | |||
| 721 | This command is not bound to a key by default, to avoid conflicts | ||
| 722 | with language major mode bindings. To bind it to C-c @ in all | ||
| 723 | language major modes, you could use | ||
| 724 | |||
| 725 | (add-hook 'org-src-mode-hook | ||
| 726 | (lambda () (define-key org-src-mode-map \"\\C-c@\" | ||
| 727 | 'org-src-do-key-sequence-at-code-block))) | ||
| 728 | |||
| 729 | In that case, for example, C-c @ t issued in code edit buffers | ||
| 730 | would tangle the current Org code block, C-c @ e would execute | ||
| 731 | the block and C-c @ h would display the other available | ||
| 732 | Org-babel commands." | ||
| 733 | (interactive "kOrg-babel key: ") | ||
| 734 | (if (equal key (kbd "C-g")) (keyboard-quit) | ||
| 735 | (org-edit-src-save) | ||
| 736 | (org-src-do-at-code-block | ||
| 737 | (call-interactively | ||
| 738 | (lookup-key org-babel-map key))))) | ||
| 739 | |||
| 740 | (defcustom org-src-tab-acts-natively nil | ||
| 741 | "If non-nil, the effect of TAB in a code block is as if it were | ||
| 742 | issued in the language major mode buffer." | ||
| 743 | :type 'boolean | ||
| 744 | :group 'org-babel) | ||
| 745 | |||
| 746 | (defun org-src-native-tab-command-maybe () | ||
| 747 | "Perform language-specific TAB action. | ||
| 748 | Alter code block according to effect of TAB in the language major | ||
| 749 | mode." | ||
| 750 | (and org-src-tab-acts-natively | ||
| 751 | (let ((org-src-strip-leading-and-trailing-blank-lines nil)) | ||
| 752 | (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))) | ||
| 753 | |||
| 754 | (add-hook 'org-tab-first-hook 'org-src-native-tab-command-maybe) | ||
| 755 | |||
| 756 | (defun org-src-font-lock-fontify-block (lang start end) | ||
| 757 | "Fontify code block. | ||
| 758 | This function is called by emacs automatic fontification, as long | ||
| 759 | as `org-src-fontify-natively' is non-nil. For manual | ||
| 760 | fontification of code blocks see `org-src-fontify-block' and | ||
| 761 | `org-src-fontify-buffer'" | ||
| 762 | (let* ((lang-mode (org-src-get-lang-mode lang)) | ||
| 763 | (string (buffer-substring-no-properties start end)) | ||
| 764 | (modified (buffer-modified-p)) | ||
| 765 | (org-buffer (current-buffer)) pos next) | ||
| 766 | (remove-text-properties start end '(face nil)) | ||
| 767 | (with-current-buffer | ||
| 768 | (get-buffer-create | ||
| 769 | (concat " org-src-fontification:" (symbol-name lang-mode))) | ||
| 770 | (delete-region (point-min) (point-max)) | ||
| 771 | (insert string) | ||
| 772 | (unless (eq major-mode lang-mode) (funcall lang-mode)) | ||
| 773 | (font-lock-fontify-buffer) | ||
| 774 | (setq pos (point-min)) | ||
| 775 | (while (setq next (next-single-property-change pos 'face)) | ||
| 776 | (put-text-property | ||
| 777 | (+ start (1- pos)) (+ start next) 'face | ||
| 778 | (get-text-property pos 'face) org-buffer) | ||
| 779 | (setq pos next))) | ||
| 780 | (add-text-properties | ||
| 781 | start end | ||
| 782 | '(font-lock-fontified t fontified t font-lock-multiline t)) | ||
| 783 | (set-buffer-modified-p modified)) | ||
| 784 | t) ;; Tell `org-fontify-meta-lines-and-blocks' that we fontified | ||
| 785 | |||
| 786 | (defun org-src-fontify-block () | ||
| 787 | "Fontify code block at point." | ||
| 788 | (interactive) | ||
| 789 | (save-excursion | ||
| 790 | (let ((org-src-fontify-natively t) | ||
| 791 | (info (org-edit-src-find-region-and-lang))) | ||
| 792 | (font-lock-fontify-region (nth 0 info) (nth 1 info))))) | ||
| 793 | |||
| 794 | (defun org-src-fontify-buffer () | ||
| 795 | "Fontify all code blocks in the current buffer" | ||
| 796 | (interactive) | ||
| 797 | (org-babel-map-src-blocks nil | ||
| 798 | (org-src-fontify-block))) | ||
| 799 | |||
| 800 | (defun org-src-get-lang-mode (lang) | ||
| 801 | "Return major mode that should be used for LANG. | ||
| 802 | LANG is a string, and the returned major mode is a symbol." | ||
| 803 | (intern | ||
| 804 | (concat | ||
| 805 | ((lambda (l) (if (symbolp l) (symbol-name l) l)) | ||
| 806 | (or (cdr (assoc lang org-src-lang-modes)) lang)) "-mode"))) | ||
| 807 | |||
| 657 | (provide 'org-src) | 808 | (provide 'org-src) |
| 658 | 809 | ||
| 659 | ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8 | 810 | ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8 |