diff options
| author | Bastien Guerry | 2013-11-12 14:06:26 +0100 |
|---|---|---|
| committer | Bastien Guerry | 2013-11-12 14:06:26 +0100 |
| commit | 271672fad74cdbc9065d23d6e6cee1b8540f571b (patch) | |
| tree | d322b956ec0e74ee33b22354ef00839b23b1618d /lisp/org/ob-python.el | |
| parent | f201cf3a8143b0b34b07769fc7d73dd14761b87b (diff) | |
| download | emacs-271672fad74cdbc9065d23d6e6cee1b8540f571b.tar.gz emacs-271672fad74cdbc9065d23d6e6cee1b8540f571b.zip | |
Merge Org version 8.2.3a.
Diffstat (limited to 'lisp/org/ob-python.el')
| -rw-r--r-- | lisp/org/ob-python.el | 93 |
1 files changed, 67 insertions, 26 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el index 79cc53ea0fa..17da109ca0b 100644 --- a/lisp/org/ob-python.el +++ b/lisp/org/ob-python.el | |||
| @@ -28,30 +28,50 @@ | |||
| 28 | 28 | ||
| 29 | ;;; Code: | 29 | ;;; Code: |
| 30 | (require 'ob) | 30 | (require 'ob) |
| 31 | (require 'ob-ref) | ||
| 32 | (require 'ob-comint) | ||
| 33 | (require 'ob-eval) | ||
| 34 | (eval-when-compile (require 'cl)) | 31 | (eval-when-compile (require 'cl)) |
| 35 | 32 | ||
| 36 | (declare-function org-remove-indentation "org" ) | 33 | (declare-function org-remove-indentation "org" ) |
| 37 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) | 34 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) |
| 38 | (declare-function py-toggle-shells "ext:python-mode" (arg)) | 35 | (declare-function py-toggle-shells "ext:python-mode" (arg)) |
| 39 | (declare-function run-python "ext:python" (&optional cmd noshow new)) | 36 | (declare-function run-python "ext:python" (cmd &optional dedicated show)) |
| 40 | 37 | ||
| 41 | (defvar org-babel-tangle-lang-exts) | 38 | (defvar org-babel-tangle-lang-exts) |
| 42 | (add-to-list 'org-babel-tangle-lang-exts '("python" . "py")) | 39 | (add-to-list 'org-babel-tangle-lang-exts '("python" . "py")) |
| 43 | 40 | ||
| 44 | (defvar org-babel-default-header-args:python '()) | 41 | (defvar org-babel-default-header-args:python '()) |
| 45 | 42 | ||
| 46 | (defvar org-babel-python-command "python" | 43 | (defcustom org-babel-python-command "python" |
| 47 | "Name of the command for executing Python code.") | 44 | "Name of the command for executing Python code." |
| 45 | :version "24.4" | ||
| 46 | :package-version '(Org . "8.0") | ||
| 47 | :group 'org-babel | ||
| 48 | :type 'string) | ||
| 48 | 49 | ||
| 49 | (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) | 50 | (defcustom org-babel-python-mode |
| 51 | (if (or (featurep 'xemacs) (featurep 'python-mode)) 'python-mode 'python) | ||
| 50 | "Preferred python mode for use in running python interactively. | 52 | "Preferred python mode for use in running python interactively. |
| 51 | This will typically be either 'python or 'python-mode.") | 53 | This will typically be either 'python or 'python-mode." |
| 54 | :group 'org-babel | ||
| 55 | :version "24.4" | ||
| 56 | :package-version '(Org . "8.0") | ||
| 57 | :type 'function) | ||
| 52 | 58 | ||
| 53 | (defvar org-src-preserve-indentation) | 59 | (defvar org-src-preserve-indentation) |
| 54 | 60 | ||
| 61 | (defcustom org-babel-python-hline-to "None" | ||
| 62 | "Replace hlines in incoming tables with this when translating to python." | ||
| 63 | :group 'org-babel | ||
| 64 | :version "24.4" | ||
| 65 | :package-version '(Org . "8.0") | ||
| 66 | :type 'string) | ||
| 67 | |||
| 68 | (defcustom org-babel-python-None-to 'hline | ||
| 69 | "Replace 'None' in python tables with this before returning." | ||
| 70 | :group 'org-babel | ||
| 71 | :version "24.4" | ||
| 72 | :package-version '(Org . "8.0") | ||
| 73 | :type 'string) | ||
| 74 | |||
| 55 | (defun org-babel-execute:python (body params) | 75 | (defun org-babel-execute:python (body params) |
| 56 | "Execute a block of Python code with Babel. | 76 | "Execute a block of Python code with Babel. |
| 57 | This function is called by `org-babel-execute-src-block'." | 77 | This function is called by `org-babel-execute-src-block'." |
| @@ -114,7 +134,7 @@ specifying a variable of the same value." | |||
| 114 | (if (listp var) | 134 | (if (listp var) |
| 115 | (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") | 135 | (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") |
| 116 | (if (equal var 'hline) | 136 | (if (equal var 'hline) |
| 117 | "None" | 137 | org-babel-python-hline-to |
| 118 | (format | 138 | (format |
| 119 | (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") | 139 | (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") |
| 120 | var)))) | 140 | var)))) |
| @@ -123,14 +143,34 @@ specifying a variable of the same value." | |||
| 123 | "Convert RESULTS into an appropriate elisp value. | 143 | "Convert RESULTS into an appropriate elisp value. |
| 124 | If the results look like a list or tuple, then convert them into an | 144 | If the results look like a list or tuple, then convert them into an |
| 125 | Emacs-lisp table, otherwise return the results as a string." | 145 | Emacs-lisp table, otherwise return the results as a string." |
| 126 | (org-babel-script-escape results)) | 146 | ((lambda (res) |
| 147 | (if (listp res) | ||
| 148 | (mapcar (lambda (el) (if (equal el 'None) | ||
| 149 | org-babel-python-None-to el)) | ||
| 150 | res) | ||
| 151 | res)) | ||
| 152 | (org-babel-script-escape results))) | ||
| 127 | 153 | ||
| 128 | (defvar org-babel-python-buffers '((:default . nil))) | 154 | (defvar org-babel-python-buffers '((:default . "*Python*"))) |
| 129 | 155 | ||
| 130 | (defun org-babel-python-session-buffer (session) | 156 | (defun org-babel-python-session-buffer (session) |
| 131 | "Return the buffer associated with SESSION." | 157 | "Return the buffer associated with SESSION." |
| 132 | (cdr (assoc session org-babel-python-buffers))) | 158 | (cdr (assoc session org-babel-python-buffers))) |
| 133 | 159 | ||
| 160 | (defun org-babel-python-with-earmufs (session) | ||
| 161 | (let ((name (if (stringp session) session (format "%s" session)))) | ||
| 162 | (if (and (string= "*" (substring name 0 1)) | ||
| 163 | (string= "*" (substring name (- (length name) 1)))) | ||
| 164 | name | ||
| 165 | (format "*%s*" name)))) | ||
| 166 | |||
| 167 | (defun org-babel-python-without-earmufs (session) | ||
| 168 | (let ((name (if (stringp session) session (format "%s" session)))) | ||
| 169 | (if (and (string= "*" (substring name 0 1)) | ||
| 170 | (string= "*" (substring name (- (length name) 1)))) | ||
| 171 | (substring name 1 (- (length name) 1)) | ||
| 172 | name))) | ||
| 173 | |||
| 134 | (defvar py-default-interpreter) | 174 | (defvar py-default-interpreter) |
| 135 | (defun org-babel-python-initiate-session-by-key (&optional session) | 175 | (defun org-babel-python-initiate-session-by-key (&optional session) |
| 136 | "Initiate a python session. | 176 | "Initiate a python session. |
| @@ -139,13 +179,20 @@ then create. Return the initialized session." | |||
| 139 | (require org-babel-python-mode) | 179 | (require org-babel-python-mode) |
| 140 | (save-window-excursion | 180 | (save-window-excursion |
| 141 | (let* ((session (if session (intern session) :default)) | 181 | (let* ((session (if session (intern session) :default)) |
| 142 | (python-buffer (org-babel-python-session-buffer session))) | 182 | (python-buffer (org-babel-python-session-buffer session)) |
| 183 | (cmd (if (member system-type '(cygwin windows-nt ms-dos)) | ||
| 184 | (concat org-babel-python-command " -i") | ||
| 185 | org-babel-python-command))) | ||
| 143 | (cond | 186 | (cond |
| 144 | ((and (eq 'python org-babel-python-mode) | 187 | ((and (eq 'python org-babel-python-mode) |
| 145 | (fboundp 'run-python)) ; python.el | 188 | (fboundp 'run-python)) ; python.el |
| 146 | (if (version< "24.1" emacs-version) | 189 | (if (not (version< "24.1" emacs-version)) |
| 147 | (run-python org-babel-python-command) | 190 | (run-python cmd) |
| 148 | (run-python))) | 191 | (unless python-buffer |
| 192 | (setq python-buffer (org-babel-python-with-earmufs session))) | ||
| 193 | (let ((python-shell-buffer-name | ||
| 194 | (org-babel-python-without-earmufs python-buffer))) | ||
| 195 | (run-python cmd)))) | ||
| 149 | ((and (eq 'python-mode org-babel-python-mode) | 196 | ((and (eq 'python-mode org-babel-python-mode) |
| 150 | (fboundp 'py-shell)) ; python-mode.el | 197 | (fboundp 'py-shell)) ; python-mode.el |
| 151 | ;; Make sure that py-which-bufname is initialized, as otherwise | 198 | ;; Make sure that py-which-bufname is initialized, as otherwise |
| @@ -160,7 +207,7 @@ then create. Return the initialized session." | |||
| 160 | (concat "Python-" (symbol-name session)))) | 207 | (concat "Python-" (symbol-name session)))) |
| 161 | (py-which-bufname bufname)) | 208 | (py-which-bufname bufname)) |
| 162 | (py-shell) | 209 | (py-shell) |
| 163 | (setq python-buffer (concat "*" bufname "*")))) | 210 | (setq python-buffer (org-babel-python-with-earmufs bufname)))) |
| 164 | (t | 211 | (t |
| 165 | (error "No function available for running an inferior Python"))) | 212 | (error "No function available for running an inferior Python"))) |
| 166 | (setq org-babel-python-buffers | 213 | (setq org-babel-python-buffers |
| @@ -206,11 +253,8 @@ If RESULT-TYPE equals 'output then return standard output as a | |||
| 206 | string. If RESULT-TYPE equals 'value then return the value of the | 253 | string. If RESULT-TYPE equals 'value then return the value of the |
| 207 | last statement in BODY, as elisp." | 254 | last statement in BODY, as elisp." |
| 208 | ((lambda (raw) | 255 | ((lambda (raw) |
| 209 | (if (or (member "code" result-params) | 256 | (org-babel-result-cond result-params |
| 210 | (member "pp" result-params) | 257 | raw |
| 211 | (and (member "output" result-params) | ||
| 212 | (not (member "table" result-params)))) | ||
| 213 | raw | ||
| 214 | (org-babel-python-table-or-string (org-babel-trim raw)))) | 258 | (org-babel-python-table-or-string (org-babel-trim raw)))) |
| 215 | (case result-type | 259 | (case result-type |
| 216 | (output (org-babel-eval org-babel-python-command | 260 | (output (org-babel-eval org-babel-python-command |
| @@ -259,11 +303,8 @@ last statement in BODY, as elisp." | |||
| 259 | (funcall send-wait)))) | 303 | (funcall send-wait)))) |
| 260 | ((lambda (results) | 304 | ((lambda (results) |
| 261 | (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results) | 305 | (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results) |
| 262 | (if (or (member "code" result-params) | 306 | (org-babel-result-cond result-params |
| 263 | (member "pp" result-params) | 307 | results |
| 264 | (and (member "output" result-params) | ||
| 265 | (not (member "table" result-params)))) | ||
| 266 | results | ||
| 267 | (org-babel-python-table-or-string results)))) | 308 | (org-babel-python-table-or-string results)))) |
| 268 | (case result-type | 309 | (case result-type |
| 269 | (output | 310 | (output |