Show More
@@ -1,490 +1,491 b'' | |||||
1 | ;;; ipython.el --- Adds support for IPython to python-mode.el |
|
1 | ;;; ipython.el --- Adds support for IPython to python-mode.el | |
2 |
|
2 | |||
3 | ;; Copyright (C) 2002, 2003, 2004, 2005 Alexander Schmolck |
|
3 | ;; Copyright (C) 2002, 2003, 2004, 2005 Alexander Schmolck | |
4 | ;; Author: Alexander Schmolck |
|
4 | ;; Author: Alexander Schmolck | |
5 | ;; Keywords: ipython python languages oop |
|
5 | ;; Keywords: ipython python languages oop | |
6 | ;; URL: http://ipython.scipy.org |
|
6 | ;; URL: http://ipython.scipy.org | |
7 | ;; Compatibility: Emacs21, XEmacs21 |
|
7 | ;; Compatibility: Emacs21, XEmacs21 | |
8 | ;; FIXME: #$@! INPUT RING |
|
8 | ;; FIXME: #$@! INPUT RING | |
9 | (defconst ipython-version "$Revision: 2927 $" |
|
9 | (defconst ipython-version "$Revision: 2927 $" | |
10 | "VC version number.") |
|
10 | "VC version number.") | |
11 |
|
11 | |||
12 |
;;; Commentary |
|
12 | ;;; Commentary | |
13 | ;; This library makes all the functionality python-mode has when running with |
|
13 | ;; This library makes all the functionality python-mode has when running with | |
14 | ;; the normal python-interpreter available for ipython, too. It also enables a |
|
14 | ;; the normal python-interpreter available for ipython, too. It also enables a | |
15 | ;; persistent py-shell command history across sessions (if you exit python |
|
15 | ;; persistent py-shell command history across sessions (if you exit python | |
16 | ;; with C-d in py-shell) and defines the command `ipython-to-doctest', which |
|
16 | ;; with C-d in py-shell) and defines the command `ipython-to-doctest', which | |
17 | ;; can be used to convert bits of a ipython session into something that can be |
|
17 | ;; can be used to convert bits of a ipython session into something that can be | |
18 | ;; used for doctests. To install, put this file somewhere in your emacs |
|
18 | ;; used for doctests. To install, put this file somewhere in your emacs | |
19 | ;; `load-path' [1] and add the following line to your ~/.emacs file (the first |
|
19 | ;; `load-path' [1] and add the following line to your ~/.emacs file (the first | |
20 | ;; line only needed if the default (``"ipython"``) is wrong):: |
|
20 | ;; line only needed if the default (``"ipython"``) is wrong):: | |
21 | ;; |
|
21 | ;; | |
22 | ;; (setq ipython-command "/SOME-PATH/ipython") |
|
22 | ;; (setq ipython-command "/SOME-PATH/ipython") | |
23 | ;; (require 'ipython) |
|
23 | ;; (require 'ipython) | |
24 | ;; |
|
24 | ;; | |
25 | ;; Ipython will be set as the default python shell, but only if the ipython |
|
25 | ;; Ipython will be set as the default python shell, but only if the ipython | |
26 | ;; executable is in the path. For ipython sessions autocompletion with <tab> |
|
26 | ;; executable is in the path. For ipython sessions autocompletion with <tab> | |
27 | ;; is also enabled (experimental feature!). Please also note that all the |
|
27 | ;; is also enabled (experimental feature!). Please also note that all the | |
28 | ;; terminal functions in py-shell are handled by emacs's comint, **not** by |
|
28 | ;; terminal functions in py-shell are handled by emacs's comint, **not** by | |
29 | ;; (i)python, so importing readline etc. will have 0 effect. |
|
29 | ;; (i)python, so importing readline etc. will have 0 effect. | |
30 | ;; |
|
30 | ;; | |
31 | ;; To start an interactive ipython session run `py-shell' with ``M-x py-shell`` |
|
31 | ;; To start an interactive ipython session run `py-shell' with ``M-x py-shell`` | |
32 | ;; (or the default keybinding ``C-c C-!``). |
|
32 | ;; (or the default keybinding ``C-c C-!``). | |
33 | ;; |
|
33 | ;; | |
34 | ;; You can customize the arguments passed to the IPython instance at startup by |
|
34 | ;; You can customize the arguments passed to the IPython instance at startup by | |
35 | ;; setting the ``py-python-command-args`` variable. For example, to start |
|
35 | ;; setting the ``py-python-command-args`` variable. For example, to start | |
36 |
;; always in ``pylab`` mode with hardcoded light-background colors, you can |
|
36 | ;; always in ``pylab`` mode with hardcoded light-background colors, you can | |
|
37 | ;; use:: | |||
|
38 | ;; | |||
37 | ;; (setq py-python-command-args '("-pylab" "-colors" "LightBG")) |
|
39 | ;; (setq py-python-command-args '("-pylab" "-colors" "LightBG")) | |
38 | ;; |
|
40 | ;; | |
39 | ;; |
|
41 | ;; | |
40 | ;; NOTE: This mode is currently somewhat alpha and although I hope that it |
|
42 | ;; NOTE: This mode is currently somewhat alpha and although I hope that it | |
41 | ;; will work fine for most cases, doing certain things (like the |
|
43 | ;; will work fine for most cases, doing certain things (like the | |
42 | ;; autocompletion and a decent scheme to switch between python interpreters) |
|
44 | ;; autocompletion and a decent scheme to switch between python interpreters) | |
43 | ;; properly will also require changes to ipython that will likely have to wait |
|
45 | ;; properly will also require changes to ipython that will likely have to wait | |
44 | ;; for a larger rewrite scheduled some time in the future. |
|
46 | ;; for a larger rewrite scheduled some time in the future. | |
45 |
;; |
|
47 | ;; | |
46 | ;; Also note that you currently NEED THE CVS VERSION OF PYTHON.EL. |
|
|||
47 | ;; |
|
48 | ;; | |
48 | ;; Further note that I don't know whether this runs under windows or not and |
|
49 | ;; Further note that I don't know whether this runs under windows or not and | |
49 | ;; that if it doesn't I can't really help much, not being afflicted myself. |
|
50 | ;; that if it doesn't I can't really help much, not being afflicted myself. | |
50 | ;; |
|
51 | ;; | |
51 | ;; |
|
52 | ;; | |
52 | ;; Hints for effective usage |
|
53 | ;; Hints for effective usage | |
53 | ;; ------------------------- |
|
54 | ;; ------------------------- | |
54 | ;; |
|
55 | ;; | |
55 | ;; - IMO the best feature by far of the ipython/emacs combo is how much easier |
|
56 | ;; - IMO the best feature by far of the ipython/emacs combo is how much easier | |
56 | ;; it makes it to find and fix bugs thanks to the ``%pdb on or %debug``/ |
|
57 | ;; it makes it to find and fix bugs thanks to the ``%pdb on or %debug``/ | |
57 | ;; pdbtrack combo. Try it: first in the ipython to shell do ``%pdb on`` then |
|
58 | ;; pdbtrack combo. Try it: first in the ipython to shell do ``%pdb on`` then | |
58 | ;; do something that will raise an exception (FIXME nice example), or type |
|
59 | ;; do something that will raise an exception (FIXME nice example), or type | |
59 | ;; ``%debug`` after the exception has been raised. YOu'll be amazed at how |
|
60 | ;; ``%debug`` after the exception has been raised. YOu'll be amazed at how | |
60 | ;; easy it is to inspect the live objects in each stack frames and to jump to |
|
61 | ;; easy it is to inspect the live objects in each stack frames and to jump to | |
61 | ;; the corresponding sourcecode locations as you walk up and down the stack |
|
62 | ;; the corresponding sourcecode locations as you walk up and down the stack | |
62 | ;; trace (even without ``%pdb on`` you can always use ``C-c -`` |
|
63 | ;; trace (even without ``%pdb on`` you can always use ``C-c -`` | |
63 | ;; (`py-up-exception') to jump to the corresponding source code locations). |
|
64 | ;; (`py-up-exception') to jump to the corresponding source code locations). | |
64 | ;; |
|
65 | ;; | |
65 | ;; - emacs gives you much more powerful commandline editing and output searching |
|
66 | ;; - emacs gives you much more powerful commandline editing and output searching | |
66 | ;; capabilities than ipython-standalone -- isearch is your friend if you |
|
67 | ;; capabilities than ipython-standalone -- isearch is your friend if you | |
67 | ;; quickly want to print 'DEBUG ...' to stdout out etc. |
|
68 | ;; quickly want to print 'DEBUG ...' to stdout out etc. | |
68 | ;; |
|
69 | ;; | |
69 | ;; - This is not really specific to ipython, but for more convenient history |
|
70 | ;; - This is not really specific to ipython, but for more convenient history | |
70 | ;; access you might want to add something like the following to *the beggining* |
|
71 | ;; access you might want to add something like the following to *the beggining* | |
71 | ;; of your ``.emacs`` (if you want behavior that's more similar to stand-alone |
|
72 | ;; of your ``.emacs`` (if you want behavior that's more similar to stand-alone | |
72 | ;; ipython, you can change ``meta p`` etc. for ``control p``):: |
|
73 | ;; ipython, you can change ``meta p`` etc. for ``control p``):: | |
73 | ;; |
|
74 | ;; | |
74 | ;; (require 'comint) |
|
75 | ;; (require 'comint) | |
75 |
;; (define-key comint-mode-map [(meta p)] |
|
76 | ;; (define-key comint-mode-map [(meta p)] | |
76 | ;; 'comint-previous-matching-input-from-input) |
|
77 | ;; 'comint-previous-matching-input-from-input) | |
77 |
;; (define-key comint-mode-map [(meta n)] |
|
78 | ;; (define-key comint-mode-map [(meta n)] | |
78 | ;; 'comint-next-matching-input-from-input) |
|
79 | ;; 'comint-next-matching-input-from-input) | |
79 | ;; (define-key comint-mode-map [(control meta n)] |
|
80 | ;; (define-key comint-mode-map [(control meta n)] | |
80 | ;; 'comint-next-input) |
|
81 | ;; 'comint-next-input) | |
81 | ;; (define-key comint-mode-map [(control meta p)] |
|
82 | ;; (define-key comint-mode-map [(control meta p)] | |
82 | ;; 'comint-previous-input) |
|
83 | ;; 'comint-previous-input) | |
83 | ;; |
|
84 | ;; | |
84 | ;; - Be aware that if you customize py-python-command previously, this value |
|
85 | ;; - Be aware that if you customize py-python-command previously, this value | |
85 | ;; will override what ipython.el does (because loading the customization |
|
86 | ;; will override what ipython.el does (because loading the customization | |
86 | ;; variables comes later). |
|
87 | ;; variables comes later). | |
87 | ;; |
|
88 | ;; | |
88 | ;; Please send comments and feedback to the ipython-list |
|
89 | ;; Please send comments and feedback to the ipython-list | |
89 | ;; (<ipython-user@scipy.org>) where I (a.s.) or someone else will try to |
|
90 | ;; (<ipython-user@scipy.org>) where I (a.s.) or someone else will try to | |
90 |
;; answer them (it helps if you specify your emacs version, OS etc; |
|
91 | ;; answer them (it helps if you specify your emacs version, OS etc; | |
91 | ;; familiarity with <http://www.catb.org/~esr/faqs/smart-questions.html> might |
|
92 | ;; familiarity with <http://www.catb.org/~esr/faqs/smart-questions.html> might | |
92 | ;; speed up things further). |
|
93 | ;; speed up things further). | |
93 | ;; |
|
94 | ;; | |
94 | ;; Footnotes: |
|
95 | ;; Footnotes: | |
95 | ;; |
|
96 | ;; | |
96 | ;; [1] If you don't know what `load-path' is, C-h v load-path will tell |
|
97 | ;; [1] If you don't know what `load-path' is, C-h v load-path will tell | |
97 | ;; you; if required you can also add a new directory. So assuming that |
|
98 | ;; you; if required you can also add a new directory. So assuming that | |
98 | ;; ipython.el resides in ~/el/, put this in your emacs: |
|
99 | ;; ipython.el resides in ~/el/, put this in your emacs: | |
99 | ;; |
|
100 | ;; | |
100 | ;; |
|
101 | ;; | |
101 | ;; (add-to-list 'load-path "~/el") |
|
102 | ;; (add-to-list 'load-path "~/el") | |
102 | ;; (setq ipython-command "/some-path/ipython") |
|
103 | ;; (setq ipython-command "/some-path/ipython") | |
103 | ;; (require 'ipython) |
|
104 | ;; (require 'ipython) | |
104 | ;; |
|
105 | ;; | |
105 | ;; |
|
106 | ;; | |
106 | ;; |
|
107 | ;; | |
107 | ;; |
|
108 | ;; | |
108 | ;; TODO: |
|
109 | ;; TODO: | |
109 | ;; - do autocompletion properly |
|
110 | ;; - do autocompletion properly | |
110 | ;; - implement a proper switching between python interpreters |
|
111 | ;; - implement a proper switching between python interpreters | |
111 | ;; |
|
112 | ;; | |
112 | ;; BUGS: |
|
113 | ;; BUGS: | |
113 | ;; - neither:: |
|
114 | ;; - neither:: | |
114 | ;; |
|
115 | ;; | |
115 | ;; (py-shell "-c print 'FOOBAR'") |
|
116 | ;; (py-shell "-c print 'FOOBAR'") | |
116 | ;; |
|
117 | ;; | |
117 | ;; nor:: |
|
118 | ;; nor:: | |
118 | ;; |
|
119 | ;; | |
119 |
;; (let ((py-python-command-args (append py-python-command-args |
|
120 | ;; (let ((py-python-command-args (append py-python-command-args | |
120 | ;; '("-c" "print 'FOOBAR'")))) |
|
121 | ;; '("-c" "print 'FOOBAR'")))) | |
121 | ;; (py-shell)) |
|
122 | ;; (py-shell)) | |
122 | ;; |
|
123 | ;; | |
123 | ;; seem to print anything as they should |
|
124 | ;; seem to print anything as they should | |
124 | ;; |
|
125 | ;; | |
125 | ;; - look into init priority issues with `py-python-command' (if it's set |
|
126 | ;; - look into init priority issues with `py-python-command' (if it's set | |
126 | ;; via custom) |
|
127 | ;; via custom) | |
127 |
|
128 | |||
128 |
|
129 | |||
129 | ;;; Code |
|
130 | ;;; Code | |
130 | (require 'cl) |
|
131 | (require 'cl) | |
131 | (require 'shell) |
|
132 | (require 'shell) | |
132 | (require 'executable) |
|
133 | (require 'executable) | |
133 | (require 'ansi-color) |
|
134 | (require 'ansi-color) | |
134 |
|
135 | |||
135 | (defcustom ipython-command "ipython" |
|
136 | (defcustom ipython-command "ipython" | |
136 | "*Shell command used to start ipython." |
|
137 | "*Shell command used to start ipython." | |
137 |
:type 'string |
|
138 | :type 'string | |
138 | :group 'python) |
|
139 | :group 'python) | |
139 |
|
140 | |||
140 | ;; Users can set this to nil |
|
141 | ;; Users can set this to nil | |
141 | (defvar py-shell-initial-switch-buffers t |
|
142 | (defvar py-shell-initial-switch-buffers t | |
142 | "If nil, don't switch to the *Python* buffer on the first call to |
|
143 | "If nil, don't switch to the *Python* buffer on the first call to | |
143 | `py-shell'.") |
|
144 | `py-shell'.") | |
144 |
|
145 | |||
145 | (defvar ipython-backup-of-py-python-command nil |
|
146 | (defvar ipython-backup-of-py-python-command nil | |
146 | "HACK") |
|
147 | "HACK") | |
147 |
|
148 | |||
148 |
|
149 | |||
149 | (defvar ipython-de-input-prompt-regexp "\\(?: |
|
150 | (defvar ipython-de-input-prompt-regexp "\\(?: | |
150 | In \\[[0-9]+\\]: *.* |
|
151 | In \\[[0-9]+\\]: *.* | |
151 | ----+> \\(.* |
|
152 | ----+> \\(.* | |
152 | \\)[\n]?\\)\\|\\(?: |
|
153 | \\)[\n]?\\)\\|\\(?: | |
153 | In \\[[0-9]+\\]: *\\(.* |
|
154 | In \\[[0-9]+\\]: *\\(.* | |
154 | \\)\\)\\|^[ ]\\{3\\}[.]\\{3,\\}: *\\(.* |
|
155 | \\)\\)\\|^[ ]\\{3\\}[.]\\{3,\\}: *\\(.* | |
155 | \\)" |
|
156 | \\)" | |
156 | "A regular expression to match the IPython input prompt and the python |
|
157 | "A regular expression to match the IPython input prompt and the python | |
157 | command after it. The first match group is for a command that is rewritten, |
|
158 | command after it. The first match group is for a command that is rewritten, | |
158 | the second for a 'normal' command, and the third for a multiline command.") |
|
159 | the second for a 'normal' command, and the third for a multiline command.") | |
159 | (defvar ipython-de-output-prompt-regexp "^Out\\[[0-9]+\\]: " |
|
160 | (defvar ipython-de-output-prompt-regexp "^Out\\[[0-9]+\\]: " | |
160 | "A regular expression to match the output prompt of IPython.") |
|
161 | "A regular expression to match the output prompt of IPython.") | |
161 |
|
162 | |||
162 |
|
163 | |||
163 | (if (not (executable-find ipython-command)) |
|
164 | (if (not (executable-find ipython-command)) | |
164 | (message (format "Can't find executable %s - ipython.el *NOT* activated!!!" |
|
165 | (message (format "Can't find executable %s - ipython.el *NOT* activated!!!" | |
165 | ipython-command)) |
|
166 | ipython-command)) | |
166 | ;; XXX load python-mode, so that we can screw around with its variables |
|
167 | ;; XXX load python-mode, so that we can screw around with its variables | |
167 | ;; this has the disadvantage that python-mode is loaded even if no |
|
168 | ;; this has the disadvantage that python-mode is loaded even if no | |
168 | ;; python-file is ever edited etc. but it means that `py-shell' works |
|
169 | ;; python-file is ever edited etc. but it means that `py-shell' works | |
169 | ;; without loading a python-file first. Obviously screwing around with |
|
170 | ;; without loading a python-file first. Obviously screwing around with | |
170 | ;; python-mode's variables like this is a mess, but well. |
|
171 | ;; python-mode's variables like this is a mess, but well. | |
171 | (require 'python-mode) |
|
172 | (require 'python-mode) | |
172 | ;; turn on ansi colors for ipython and activate completion |
|
173 | ;; turn on ansi colors for ipython and activate completion | |
173 | (defun ipython-shell-hook () |
|
174 | (defun ipython-shell-hook () | |
174 | ;; the following is to synchronize dir-changes |
|
175 | ;; the following is to synchronize dir-changes | |
175 | (make-local-variable 'shell-dirstack) |
|
176 | (make-local-variable 'shell-dirstack) | |
176 | (setq shell-dirstack nil) |
|
177 | (setq shell-dirstack nil) | |
177 | (make-local-variable 'shell-last-dir) |
|
178 | (make-local-variable 'shell-last-dir) | |
178 | (setq shell-last-dir nil) |
|
179 | (setq shell-last-dir nil) | |
179 | (make-local-variable 'shell-dirtrackp) |
|
180 | (make-local-variable 'shell-dirtrackp) | |
180 | (setq shell-dirtrackp t) |
|
181 | (setq shell-dirtrackp t) | |
181 | (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t) |
|
182 | (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t) | |
182 |
|
183 | |||
183 | (ansi-color-for-comint-mode-on) |
|
184 | (ansi-color-for-comint-mode-on) | |
184 | (define-key py-shell-map [tab] 'ipython-complete) |
|
185 | (define-key py-shell-map [tab] 'ipython-complete) | |
185 | ;; Add this so that tab-completion works both in X11 frames and inside |
|
186 | ;; Add this so that tab-completion works both in X11 frames and inside | |
186 | ;; terminals (such as when emacs is called with -nw). |
|
187 | ;; terminals (such as when emacs is called with -nw). | |
187 | (define-key py-shell-map "\t" 'ipython-complete) |
|
188 | (define-key py-shell-map "\t" 'ipython-complete) | |
188 | ;;XXX this is really just a cheap hack, it only completes symbols in the |
|
189 | ;;XXX this is really just a cheap hack, it only completes symbols in the | |
189 | ;;interactive session -- useful nonetheless. |
|
190 | ;;interactive session -- useful nonetheless. | |
190 | (define-key py-mode-map [(meta tab)] 'ipython-complete) |
|
191 | (define-key py-mode-map [(meta tab)] 'ipython-complete) | |
191 |
|
192 | |||
192 | ) |
|
193 | ) | |
193 | (add-hook 'py-shell-hook 'ipython-shell-hook) |
|
194 | (add-hook 'py-shell-hook 'ipython-shell-hook) | |
194 | ;; Regular expression that describes tracebacks for IPython in context and |
|
195 | ;; Regular expression that describes tracebacks for IPython in context and | |
195 |
;; verbose mode. |
|
196 | ;; verbose mode. | |
196 |
|
197 | |||
197 | ;;Adapt python-mode settings for ipython. |
|
198 | ;;Adapt python-mode settings for ipython. | |
198 | ;; (this works for %xmode 'verbose' or 'context') |
|
199 | ;; (this works for %xmode 'verbose' or 'context') | |
199 |
|
200 | |||
200 |
;; XXX putative regexps for syntax errors; unfortunately the |
|
201 | ;; XXX putative regexps for syntax errors; unfortunately the | |
201 | ;; current python-mode traceback-line-re scheme is too primitive, |
|
202 | ;; current python-mode traceback-line-re scheme is too primitive, | |
202 | ;; so it's either matching syntax errors, *or* everything else |
|
203 | ;; so it's either matching syntax errors, *or* everything else | |
203 | ;; (XXX: should ask Fernando for a change) |
|
204 | ;; (XXX: should ask Fernando for a change) | |
204 | ;;"^ File \"\\(.*?\\)\", line \\([0-9]+\\).*\n.*\n.*\nSyntaxError:" |
|
205 | ;;"^ File \"\\(.*?\\)\", line \\([0-9]+\\).*\n.*\n.*\nSyntaxError:" | |
205 | ;;^ File \"\\(.*?\\)\", line \\([0-9]+\\)" |
|
206 | ;;^ File \"\\(.*?\\)\", line \\([0-9]+\\)" | |
206 |
|
207 | |||
207 | (setq py-traceback-line-re |
|
208 | (setq py-traceback-line-re | |
208 | "\\(^[^\t >].+?\\.py\\).*\n +[0-9]+[^\00]*?\n-+> \\([0-9]+\\)+") |
|
209 | "\\(^[^\t >].+?\\.py\\).*\n +[0-9]+[^\00]*?\n-+> \\([0-9]+\\)+") | |
209 |
|
210 | |||
210 |
|
211 | |||
211 | ;; Recognize the ipython pdb, whose prompt is 'ipdb>' or 'ipydb>' |
|
212 | ;; Recognize the ipython pdb, whose prompt is 'ipdb>' or 'ipydb>' | |
212 | ;;instead of '(Pdb)' |
|
213 | ;;instead of '(Pdb)' | |
213 | (setq py-pdbtrack-input-prompt "\n[(<]*[Ii]?[Pp]y?db[>)]+ ") |
|
214 | (setq py-pdbtrack-input-prompt "\n[(<]*[Ii]?[Pp]y?db[>)]+ ") | |
214 | (setq pydb-pydbtrack-input-prompt "\n[(]*ipydb[>)]+ ") |
|
215 | (setq pydb-pydbtrack-input-prompt "\n[(]*ipydb[>)]+ ") | |
215 |
|
216 | |||
216 | (setq py-shell-input-prompt-1-regexp "^In \\[[0-9]+\\]: *" |
|
217 | (setq py-shell-input-prompt-1-regexp "^In \\[[0-9]+\\]: *" | |
217 | py-shell-input-prompt-2-regexp "^ [.][.][.]+: *" ) |
|
218 | py-shell-input-prompt-2-regexp "^ [.][.][.]+: *" ) | |
218 | ;; select a suitable color-scheme |
|
219 | ;; select a suitable color-scheme | |
219 | (unless (member "-colors" py-python-command-args) |
|
220 | (unless (member "-colors" py-python-command-args) | |
220 |
(setq py-python-command-args |
|
221 | (setq py-python-command-args | |
221 |
(nconc py-python-command-args |
|
222 | (nconc py-python-command-args | |
222 | (list "-colors" |
|
223 | (list "-colors" | |
223 |
(cond |
|
224 | (cond | |
224 | ((eq frame-background-mode 'dark) |
|
225 | ((eq frame-background-mode 'dark) | |
225 | "Linux") |
|
226 | "Linux") | |
226 | ((eq frame-background-mode 'light) |
|
227 | ((eq frame-background-mode 'light) | |
227 | "LightBG") |
|
228 | "LightBG") | |
228 | (t ; default (backg-mode isn't always set by XEmacs) |
|
229 | (t ; default (backg-mode isn't always set by XEmacs) | |
229 | "LightBG")))))) |
|
230 | "LightBG")))))) | |
230 | (unless (equal ipython-backup-of-py-python-command py-python-command) |
|
231 | (unless (equal ipython-backup-of-py-python-command py-python-command) | |
231 | (setq ipython-backup-of-py-python-command py-python-command)) |
|
232 | (setq ipython-backup-of-py-python-command py-python-command)) | |
232 | (setq py-python-command ipython-command)) |
|
233 | (setq py-python-command ipython-command)) | |
233 |
|
234 | |||
234 |
|
235 | |||
235 | ;; MODIFY py-shell so that it loads the editing history |
|
236 | ;; MODIFY py-shell so that it loads the editing history | |
236 | (defadvice py-shell (around py-shell-with-history) |
|
237 | (defadvice py-shell (around py-shell-with-history) | |
237 | "Add persistent command-history support (in |
|
238 | "Add persistent command-history support (in | |
238 | $PYTHONHISTORY (or \"~/.ipython/history\", if we use IPython)). Also, if |
|
239 | $PYTHONHISTORY (or \"~/.ipython/history\", if we use IPython)). Also, if | |
239 | `py-shell-initial-switch-buffers' is nil, it only switches to *Python* if that |
|
240 | `py-shell-initial-switch-buffers' is nil, it only switches to *Python* if that | |
240 | buffer already exists." |
|
241 | buffer already exists." | |
241 | (if (comint-check-proc "*Python*") |
|
242 | (if (comint-check-proc "*Python*") | |
242 | ad-do-it |
|
243 | ad-do-it | |
243 | (setq comint-input-ring-file-name |
|
244 | (setq comint-input-ring-file-name | |
244 | (if (string-equal py-python-command ipython-command) |
|
245 | (if (string-equal py-python-command ipython-command) | |
245 | (concat (or (getenv "IPYTHONDIR") "~/.ipython") "/history") |
|
246 | (concat (or (getenv "IPYTHONDIR") "~/.ipython") "/history") | |
246 | (or (getenv "PYTHONHISTORY") "~/.python-history.py"))) |
|
247 | (or (getenv "PYTHONHISTORY") "~/.python-history.py"))) | |
247 | (comint-read-input-ring t) |
|
248 | (comint-read-input-ring t) | |
248 | (let ((buf (current-buffer))) |
|
249 | (let ((buf (current-buffer))) | |
249 | ad-do-it |
|
250 | ad-do-it | |
250 | (unless py-shell-initial-switch-buffers |
|
251 | (unless py-shell-initial-switch-buffers | |
251 | (switch-to-buffer-other-window buf))))) |
|
252 | (switch-to-buffer-other-window buf))))) | |
252 | (ad-activate 'py-shell) |
|
253 | (ad-activate 'py-shell) | |
253 | ;; (defadvice py-execute-region (before py-execute-buffer-ensure-process) |
|
254 | ;; (defadvice py-execute-region (before py-execute-buffer-ensure-process) | |
254 | ;; "HACK: test that ipython is already running before executing something. |
|
255 | ;; "HACK: test that ipython is already running before executing something. | |
255 | ;; Doing this properly seems not worth the bother (unless people actually |
|
256 | ;; Doing this properly seems not worth the bother (unless people actually | |
256 | ;; request it)." |
|
257 | ;; request it)." | |
257 | ;; (unless (comint-check-proc "*Python*") |
|
258 | ;; (unless (comint-check-proc "*Python*") | |
258 | ;; (error "Sorry you have to first do M-x py-shell to send something to ipython."))) |
|
259 | ;; (error "Sorry you have to first do M-x py-shell to send something to ipython."))) | |
259 | ;; (ad-activate 'py-execute-region) |
|
260 | ;; (ad-activate 'py-execute-region) | |
260 |
|
261 | |||
261 | (defadvice py-execute-region (around py-execute-buffer-ensure-process) |
|
262 | (defadvice py-execute-region (around py-execute-buffer-ensure-process) | |
262 | "HACK: if `py-shell' is not active or ASYNC is explicitly desired, fall back |
|
263 | "HACK: if `py-shell' is not active or ASYNC is explicitly desired, fall back | |
263 |
to python instead of ipython." |
|
264 | to python instead of ipython." | |
264 | (let ((py-which-shell (if (and (comint-check-proc "*Python*") (not async)) |
|
265 | (let ((py-which-shell (if (and (comint-check-proc "*Python*") (not async)) | |
265 | py-python-command |
|
266 | py-python-command | |
266 | ipython-backup-of-py-python-command))) |
|
267 | ipython-backup-of-py-python-command))) | |
267 | ad-do-it)) |
|
268 | ad-do-it)) | |
268 | (ad-activate 'py-execute-region) |
|
269 | (ad-activate 'py-execute-region) | |
269 |
|
270 | |||
270 | (defun ipython-to-doctest (start end) |
|
271 | (defun ipython-to-doctest (start end) | |
271 | "Transform a cut-and-pasted bit from an IPython session into something that |
|
272 | "Transform a cut-and-pasted bit from an IPython session into something that | |
272 | looks like it came from a normal interactive python session, so that it can |
|
273 | looks like it came from a normal interactive python session, so that it can | |
273 | be used in doctests. Example: |
|
274 | be used in doctests. Example: | |
274 |
|
275 | |||
275 |
|
276 | |||
276 | In [1]: import sys |
|
277 | In [1]: import sys | |
277 |
|
278 | |||
278 | In [2]: sys.stdout.write 'Hi!\n' |
|
279 | In [2]: sys.stdout.write 'Hi!\n' | |
279 | ------> sys.stdout.write ('Hi!\n') |
|
280 | ------> sys.stdout.write ('Hi!\n') | |
280 | Hi! |
|
281 | Hi! | |
281 |
|
282 | |||
282 | In [3]: 3 + 4 |
|
283 | In [3]: 3 + 4 | |
283 | Out[3]: 7 |
|
284 | Out[3]: 7 | |
284 |
|
285 | |||
285 | gets converted to: |
|
286 | gets converted to: | |
286 |
|
287 | |||
287 | >>> import sys |
|
288 | >>> import sys | |
288 | >>> sys.stdout.write ('Hi!\n') |
|
289 | >>> sys.stdout.write ('Hi!\n') | |
289 | Hi! |
|
290 | Hi! | |
290 | >>> 3 + 4 |
|
291 | >>> 3 + 4 | |
291 | 7 |
|
292 | 7 | |
292 |
|
293 | |||
293 | " |
|
294 | " | |
294 | (interactive "*r\n") |
|
295 | (interactive "*r\n") | |
295 | ;(message (format "###DEBUG s:%de:%d" start end)) |
|
296 | ;(message (format "###DEBUG s:%de:%d" start end)) | |
296 | (save-excursion |
|
297 | (save-excursion | |
297 | (save-match-data |
|
298 | (save-match-data | |
298 |
;; replace ``In [3]: bla`` with ``>>> bla`` and |
|
299 | ;; replace ``In [3]: bla`` with ``>>> bla`` and | |
299 | ;; ``... : bla`` with ``... bla`` |
|
300 | ;; ``... : bla`` with ``... bla`` | |
300 | (goto-char start) |
|
301 | (goto-char start) | |
301 | (while (re-search-forward ipython-de-input-prompt-regexp end t) |
|
302 | (while (re-search-forward ipython-de-input-prompt-regexp end t) | |
302 | ;(message "finding 1") |
|
303 | ;(message "finding 1") | |
303 | (cond ((match-string 3) ;continued |
|
304 | (cond ((match-string 3) ;continued | |
304 | (replace-match "... \\3" t nil)) |
|
305 | (replace-match "... \\3" t nil)) | |
305 | (t |
|
306 | (t | |
306 | (replace-match ">>> \\1\\2" t nil)))) |
|
307 | (replace-match ">>> \\1\\2" t nil)))) | |
307 | ;; replace `` |
|
308 | ;; replace `` | |
308 | (goto-char start) |
|
309 | (goto-char start) | |
309 | (while (re-search-forward ipython-de-output-prompt-regexp end t) |
|
310 | (while (re-search-forward ipython-de-output-prompt-regexp end t) | |
310 | (replace-match "" t nil))))) |
|
311 | (replace-match "" t nil))))) | |
311 |
|
312 | |||
312 |
(defvar ipython-completion-command-string |
|
313 | (defvar ipython-completion-command-string | |
313 | "print ';'.join(__IP.Completer.all_completions('%s')) #PYTHON-MODE SILENT\n" |
|
314 | "print ';'.join(__IP.Completer.all_completions('%s')) #PYTHON-MODE SILENT\n" | |
314 | "The string send to ipython to query for all possible completions") |
|
315 | "The string send to ipython to query for all possible completions") | |
315 |
|
316 | |||
316 |
|
317 | |||
317 | ;; xemacs doesn't have `comint-preoutput-filter-functions' so we'll try the |
|
318 | ;; xemacs doesn't have `comint-preoutput-filter-functions' so we'll try the | |
318 | ;; following wonderful hack to work around this case |
|
319 | ;; following wonderful hack to work around this case | |
319 | (if (featurep 'xemacs) |
|
320 | (if (featurep 'xemacs) | |
320 | ;;xemacs |
|
321 | ;;xemacs | |
321 | (defun ipython-complete () |
|
322 | (defun ipython-complete () | |
322 | "Try to complete the python symbol before point. Only knows about the stuff |
|
323 | "Try to complete the python symbol before point. Only knows about the stuff | |
323 | in the current *Python* session." |
|
324 | in the current *Python* session." | |
324 | (interactive) |
|
325 | (interactive) | |
325 | (let* ((ugly-return nil) |
|
326 | (let* ((ugly-return nil) | |
326 | (sep ";") |
|
327 | (sep ";") | |
327 | (python-process (or (get-buffer-process (current-buffer)) |
|
328 | (python-process (or (get-buffer-process (current-buffer)) | |
328 | ;XXX hack for .py buffers |
|
329 | ;XXX hack for .py buffers | |
329 | (get-process py-which-bufname))) |
|
330 | (get-process py-which-bufname))) | |
330 | ;; XXX currently we go backwards to find the beginning of an |
|
331 | ;; XXX currently we go backwards to find the beginning of an | |
331 | ;; expression part; a more powerful approach in the future might be |
|
332 | ;; expression part; a more powerful approach in the future might be | |
332 | ;; to let ipython have the complete line, so that context can be used |
|
333 | ;; to let ipython have the complete line, so that context can be used | |
333 | ;; to do things like filename completion etc. |
|
334 | ;; to do things like filename completion etc. | |
334 | (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_." (point-at-bol)) |
|
335 | (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_." (point-at-bol)) | |
335 | (point))) |
|
336 | (point))) | |
336 | (end (point)) |
|
337 | (end (point)) | |
337 | (pattern (buffer-substring-no-properties beg end)) |
|
338 | (pattern (buffer-substring-no-properties beg end)) | |
338 | (completions nil) |
|
339 | (completions nil) | |
339 | (completion-table nil) |
|
340 | (completion-table nil) | |
340 | completion |
|
341 | completion | |
341 | (comint-output-filter-functions |
|
342 | (comint-output-filter-functions | |
342 |
(append comint-output-filter-functions |
|
343 | (append comint-output-filter-functions | |
343 | '(ansi-color-filter-apply |
|
344 | '(ansi-color-filter-apply | |
344 |
(lambda (string) |
|
345 | (lambda (string) | |
345 | ;(message (format "DEBUG filtering: %s" string)) |
|
346 | ;(message (format "DEBUG filtering: %s" string)) | |
346 | (setq ugly-return (concat ugly-return string)) |
|
347 | (setq ugly-return (concat ugly-return string)) | |
347 |
(delete-region comint-last-output-start |
|
348 | (delete-region comint-last-output-start | |
348 | (process-mark (get-buffer-process (current-buffer))))))))) |
|
349 | (process-mark (get-buffer-process (current-buffer))))))))) | |
349 | ;(message (format "#DEBUG pattern: '%s'" pattern)) |
|
350 | ;(message (format "#DEBUG pattern: '%s'" pattern)) | |
350 |
(process-send-string python-process |
|
351 | (process-send-string python-process | |
351 | (format ipython-completion-command-string pattern)) |
|
352 | (format ipython-completion-command-string pattern)) | |
352 | (accept-process-output python-process) |
|
353 | (accept-process-output python-process) | |
353 | ;(message (format "DEBUG return: %s" ugly-return)) |
|
354 | ;(message (format "DEBUG return: %s" ugly-return)) | |
354 |
(setq completions |
|
355 | (setq completions | |
355 | (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep)) |
|
356 | (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep)) | |
356 | (setq completion-table (loop for str in completions |
|
357 | (setq completion-table (loop for str in completions | |
357 | collect (list str nil))) |
|
358 | collect (list str nil))) | |
358 | (setq completion (try-completion pattern completion-table)) |
|
359 | (setq completion (try-completion pattern completion-table)) | |
359 | (cond ((eq completion t)) |
|
360 | (cond ((eq completion t)) | |
360 | ((null completion) |
|
361 | ((null completion) | |
361 | (message "Can't find completion for \"%s\"" pattern) |
|
362 | (message "Can't find completion for \"%s\"" pattern) | |
362 | (ding)) |
|
363 | (ding)) | |
363 | ((not (string= pattern completion)) |
|
364 | ((not (string= pattern completion)) | |
364 | (delete-region beg end) |
|
365 | (delete-region beg end) | |
365 | (insert completion)) |
|
366 | (insert completion)) | |
366 | (t |
|
367 | (t | |
367 | (message "Making completion list...") |
|
368 | (message "Making completion list...") | |
368 | (with-output-to-temp-buffer "*Python Completions*" |
|
369 | (with-output-to-temp-buffer "*Python Completions*" | |
369 | (display-completion-list (all-completions pattern completion-table))) |
|
370 | (display-completion-list (all-completions pattern completion-table))) | |
370 | (message "Making completion list...%s" "done"))))) |
|
371 | (message "Making completion list...%s" "done"))))) | |
371 | ;; emacs |
|
372 | ;; emacs | |
372 | (defun ipython-complete () |
|
373 | (defun ipython-complete () | |
373 | "Try to complete the python symbol before point. Only knows about the stuff |
|
374 | "Try to complete the python symbol before point. Only knows about the stuff | |
374 | in the current *Python* session." |
|
375 | in the current *Python* session." | |
375 | (interactive) |
|
376 | (interactive) | |
376 | (let* ((ugly-return nil) |
|
377 | (let* ((ugly-return nil) | |
377 | (sep ";") |
|
378 | (sep ";") | |
378 | (python-process (or (get-buffer-process (current-buffer)) |
|
379 | (python-process (or (get-buffer-process (current-buffer)) | |
379 | ;XXX hack for .py buffers |
|
380 | ;XXX hack for .py buffers | |
380 | (get-process py-which-bufname))) |
|
381 | (get-process py-which-bufname))) | |
381 | ;; XXX currently we go backwards to find the beginning of an |
|
382 | ;; XXX currently we go backwards to find the beginning of an | |
382 | ;; expression part; a more powerful approach in the future might be |
|
383 | ;; expression part; a more powerful approach in the future might be | |
383 | ;; to let ipython have the complete line, so that context can be used |
|
384 | ;; to let ipython have the complete line, so that context can be used | |
384 | ;; to do things like filename completion etc. |
|
385 | ;; to do things like filename completion etc. | |
385 | (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_./" (point-at-bol)) |
|
386 | (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_./" (point-at-bol)) | |
386 |
(point))) |
|
387 | (point))) | |
387 | (end (point)) |
|
388 | (end (point)) | |
388 | (pattern (buffer-substring-no-properties beg end)) |
|
389 | (pattern (buffer-substring-no-properties beg end)) | |
389 | (completions nil) |
|
390 | (completions nil) | |
390 | (completion-table nil) |
|
391 | (completion-table nil) | |
391 | completion |
|
392 | completion | |
392 | (comint-preoutput-filter-functions |
|
393 | (comint-preoutput-filter-functions | |
393 |
(append comint-preoutput-filter-functions |
|
394 | (append comint-preoutput-filter-functions | |
394 | '(ansi-color-filter-apply |
|
395 | '(ansi-color-filter-apply | |
395 |
(lambda (string) |
|
396 | (lambda (string) | |
396 | (setq ugly-return (concat ugly-return string)) |
|
397 | (setq ugly-return (concat ugly-return string)) | |
397 | ""))))) |
|
398 | ""))))) | |
398 |
(process-send-string python-process |
|
399 | (process-send-string python-process | |
399 | (format ipython-completion-command-string pattern)) |
|
400 | (format ipython-completion-command-string pattern)) | |
400 | (accept-process-output python-process) |
|
401 | (accept-process-output python-process) | |
401 |
(setq completions |
|
402 | (setq completions | |
402 | (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep)) |
|
403 | (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep)) | |
403 | ;(message (format "DEBUG completions: %S" completions)) |
|
404 | ;(message (format "DEBUG completions: %S" completions)) | |
404 | (setq completion-table (loop for str in completions |
|
405 | (setq completion-table (loop for str in completions | |
405 | collect (list str nil))) |
|
406 | collect (list str nil))) | |
406 | (setq completion (try-completion pattern completion-table)) |
|
407 | (setq completion (try-completion pattern completion-table)) | |
407 | (cond ((eq completion t)) |
|
408 | (cond ((eq completion t)) | |
408 | ((null completion) |
|
409 | ((null completion) | |
409 | (message "Can't find completion for \"%s\"" pattern) |
|
410 | (message "Can't find completion for \"%s\"" pattern) | |
410 | (ding)) |
|
411 | (ding)) | |
411 | ((not (string= pattern completion)) |
|
412 | ((not (string= pattern completion)) | |
412 | (delete-region beg end) |
|
413 | (delete-region beg end) | |
413 | (insert completion)) |
|
414 | (insert completion)) | |
414 | (t |
|
415 | (t | |
415 | (message "Making completion list...") |
|
416 | (message "Making completion list...") | |
416 | (with-output-to-temp-buffer "*IPython Completions*" |
|
417 | (with-output-to-temp-buffer "*IPython Completions*" | |
417 | (display-completion-list (all-completions pattern completion-table))) |
|
418 | (display-completion-list (all-completions pattern completion-table))) | |
418 | (message "Making completion list...%s" "done"))))) |
|
419 | (message "Making completion list...%s" "done"))))) | |
419 | ) |
|
420 | ) | |
420 |
|
421 | |||
421 | ;;; autoindent support: patch sent in by Jin Liu <m.liu.jin@gmail.com>, |
|
422 | ;;; autoindent support: patch sent in by Jin Liu <m.liu.jin@gmail.com>, | |
422 | ;;; originally written by doxgen@newsmth.net |
|
423 | ;;; originally written by doxgen@newsmth.net | |
423 | ;;; Minor modifications by fperez for xemacs compatibility. |
|
424 | ;;; Minor modifications by fperez for xemacs compatibility. | |
424 |
|
425 | |||
425 | (defvar ipython-autoindent t |
|
426 | (defvar ipython-autoindent t | |
426 | "If non-nil, enable autoindent for IPython shell through python-mode.") |
|
427 | "If non-nil, enable autoindent for IPython shell through python-mode.") | |
427 |
|
428 | |||
428 | (defvar ipython-indenting-buffer-name "*IPython Indentation Calculation*" |
|
429 | (defvar ipython-indenting-buffer-name "*IPython Indentation Calculation*" | |
429 | "Temporary buffer for indenting multiline statement.") |
|
430 | "Temporary buffer for indenting multiline statement.") | |
430 |
|
431 | |||
431 | (defun ipython-get-indenting-buffer () |
|
432 | (defun ipython-get-indenting-buffer () | |
432 | "Return a temporary buffer set in python-mode. Create one if necessary." |
|
433 | "Return a temporary buffer set in python-mode. Create one if necessary." | |
433 | (let ((buf (get-buffer-create ipython-indenting-buffer-name))) |
|
434 | (let ((buf (get-buffer-create ipython-indenting-buffer-name))) | |
434 | (set-buffer buf) |
|
435 | (set-buffer buf) | |
435 | (unless (eq major-mode 'python-mode) |
|
436 | (unless (eq major-mode 'python-mode) | |
436 | (python-mode)) |
|
437 | (python-mode)) | |
437 | buf)) |
|
438 | buf)) | |
438 |
|
439 | |||
439 | (defvar ipython-indentation-string nil |
|
440 | (defvar ipython-indentation-string nil | |
440 | "Indentation for the next line in a multiline statement.") |
|
441 | "Indentation for the next line in a multiline statement.") | |
441 |
|
442 | |||
442 | (defun ipython-send-and-indent () |
|
443 | (defun ipython-send-and-indent () | |
443 | "Send the current line to IPython, and calculate the indentation for |
|
444 | "Send the current line to IPython, and calculate the indentation for | |
444 | the next line." |
|
445 | the next line." | |
445 | (interactive) |
|
446 | (interactive) | |
446 | (if ipython-autoindent |
|
447 | (if ipython-autoindent | |
447 | (let ((line (buffer-substring (point-at-bol) (point))) |
|
448 | (let ((line (buffer-substring (point-at-bol) (point))) | |
448 | (after-prompt1) |
|
449 | (after-prompt1) | |
449 | (after-prompt2)) |
|
450 | (after-prompt2)) | |
450 | (save-excursion |
|
451 | (save-excursion | |
451 | (comint-bol t) |
|
452 | (comint-bol t) | |
452 | (if (looking-at py-shell-input-prompt-1-regexp) |
|
453 | (if (looking-at py-shell-input-prompt-1-regexp) | |
453 | (setq after-prompt1 t) |
|
454 | (setq after-prompt1 t) | |
454 | (setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp))) |
|
455 | (setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp))) | |
455 | (with-current-buffer (ipython-get-indenting-buffer) |
|
456 | (with-current-buffer (ipython-get-indenting-buffer) | |
456 | (when after-prompt1 |
|
457 | (when after-prompt1 | |
457 | (erase-buffer)) |
|
458 | (erase-buffer)) | |
458 | (when (or after-prompt1 after-prompt2) |
|
459 | (when (or after-prompt1 after-prompt2) | |
459 | (delete-region (point-at-bol) (point)) |
|
460 | (delete-region (point-at-bol) (point)) | |
460 | (insert line) |
|
461 | (insert line) | |
461 | (newline-and-indent)))))) |
|
462 | (newline-and-indent)))))) | |
462 | ;; send input line to ipython interpreter |
|
463 | ;; send input line to ipython interpreter | |
463 | (comint-send-input)) |
|
464 | (comint-send-input)) | |
464 |
|
465 | |||
465 | (defun ipython-indentation-hook (string) |
|
466 | (defun ipython-indentation-hook (string) | |
466 | "Insert indentation string if py-shell-input-prompt-2-regexp |
|
467 | "Insert indentation string if py-shell-input-prompt-2-regexp | |
467 | matches last process output." |
|
468 | matches last process output." | |
468 | (let* ((start-marker (or comint-last-output-start |
|
469 | (let* ((start-marker (or comint-last-output-start | |
469 | (point-min-marker))) |
|
470 | (point-min-marker))) | |
470 | (end-marker (process-mark (get-buffer-process (current-buffer)))) |
|
471 | (end-marker (process-mark (get-buffer-process (current-buffer)))) | |
471 | (text (ansi-color-filter-apply (buffer-substring start-marker end-marker)))) |
|
472 | (text (ansi-color-filter-apply (buffer-substring start-marker end-marker)))) | |
472 | ;; XXX if `text' matches both pattern, it MUST be the last prompt-2 |
|
473 | ;; XXX if `text' matches both pattern, it MUST be the last prompt-2 | |
473 | (when (and (string-match py-shell-input-prompt-2-regexp text) |
|
474 | (when (and (string-match py-shell-input-prompt-2-regexp text) | |
474 | (not (string-match "\n$" text))) |
|
475 | (not (string-match "\n$" text))) | |
475 | (with-current-buffer (ipython-get-indenting-buffer) |
|
476 | (with-current-buffer (ipython-get-indenting-buffer) | |
476 | (setq ipython-indentation-string |
|
477 | (setq ipython-indentation-string | |
477 | (buffer-substring (point-at-bol) (point)))) |
|
478 | (buffer-substring (point-at-bol) (point)))) | |
478 | (goto-char end-marker) |
|
479 | (goto-char end-marker) | |
479 | (insert ipython-indentation-string) |
|
480 | (insert ipython-indentation-string) | |
480 | (setq ipython-indentation-string nil)))) |
|
481 | (setq ipython-indentation-string nil)))) | |
481 |
|
482 | |||
482 | (add-hook 'py-shell-hook |
|
483 | (add-hook 'py-shell-hook | |
483 | (lambda () |
|
484 | (lambda () | |
484 | (add-hook 'comint-output-filter-functions |
|
485 | (add-hook 'comint-output-filter-functions | |
485 | 'ipython-indentation-hook))) |
|
486 | 'ipython-indentation-hook))) | |
486 |
|
487 | |||
487 | (define-key py-shell-map (kbd "RET") 'ipython-send-and-indent) |
|
488 | (define-key py-shell-map (kbd "RET") 'ipython-send-and-indent) | |
488 | ;;; / end autoindent support |
|
489 | ;;; / end autoindent support | |
489 |
|
490 | |||
490 | (provide 'ipython) |
|
491 | (provide 'ipython) |
@@ -1,238 +1,243 b'' | |||||
1 | .. _initial config: |
|
1 | .. _initial config: | |
2 |
|
2 | |||
3 | ========================================= |
|
3 | ========================================= | |
4 | Initial configuration of your environment |
|
4 | Initial configuration of your environment | |
5 | ========================================= |
|
5 | ========================================= | |
6 |
|
6 | |||
7 | This section will help you set various things in your environment for |
|
7 | This section will help you set various things in your environment for | |
8 | your IPython sessions to be as efficient as possible. All of IPython's |
|
8 | your IPython sessions to be as efficient as possible. All of IPython's | |
9 | configuration information, along with several example files, is stored |
|
9 | configuration information, along with several example files, is stored | |
10 | in a directory named by default $HOME/.ipython. You can change this by |
|
10 | in a directory named by default $HOME/.ipython. You can change this by | |
11 | defining the environment variable IPYTHONDIR, or at runtime with the |
|
11 | defining the environment variable IPYTHONDIR, or at runtime with the | |
12 | command line option -ipythondir. |
|
12 | command line option -ipythondir. | |
13 |
|
13 | |||
14 | If all goes well, the first time you run IPython it should |
|
14 | If all goes well, the first time you run IPython it should | |
15 | automatically create a user copy of the config directory for you, |
|
15 | automatically create a user copy of the config directory for you, | |
16 | based on its builtin defaults. You can look at the files it creates to |
|
16 | based on its builtin defaults. You can look at the files it creates to | |
17 | learn more about configuring the system. The main file you will modify |
|
17 | learn more about configuring the system. The main file you will modify | |
18 | to configure IPython's behavior is called ipythonrc (with a .ini |
|
18 | to configure IPython's behavior is called ipythonrc (with a .ini | |
19 |
extension under Windows), included for reference in `ipythonrc`_ |
|
19 | extension under Windows), included for reference in `ipythonrc`_ | |
20 | section. This file is very commented and has many variables you can |
|
20 | section. This file is very commented and has many variables you can | |
21 | change to suit your taste, you can find more details in |
|
21 | change to suit your taste, you can find more details in | |
22 | Sec. customization_. Here we discuss the basic things you will want to |
|
22 | Sec. customization_. Here we discuss the basic things you will want to | |
23 | make sure things are working properly from the beginning. |
|
23 | make sure things are working properly from the beginning. | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | .. _Accessing help: |
|
26 | .. _Accessing help: | |
27 |
|
27 | |||
28 | Access to the Python help system |
|
28 | Access to the Python help system | |
29 | ================================ |
|
29 | ================================ | |
30 |
|
30 | |||
31 | This is true for Python in general (not just for IPython): you should |
|
31 | This is true for Python in general (not just for IPython): you should | |
32 | have an environment variable called PYTHONDOCS pointing to the directory |
|
32 | have an environment variable called PYTHONDOCS pointing to the directory | |
33 | where your HTML Python documentation lives. In my system it's |
|
33 | where your HTML Python documentation lives. In my system it's | |
34 | /usr/share/doc/python-docs-2.3.4/html, check your local details or ask |
|
34 | /usr/share/doc/python-docs-2.3.4/html, check your local details or ask | |
35 | your systems administrator. |
|
35 | your systems administrator. | |
36 |
|
36 | |||
37 | This is the directory which holds the HTML version of the Python |
|
37 | This is the directory which holds the HTML version of the Python | |
38 | manuals. Unfortunately it seems that different Linux distributions |
|
38 | manuals. Unfortunately it seems that different Linux distributions | |
39 | package these files differently, so you may have to look around a bit. |
|
39 | package these files differently, so you may have to look around a bit. | |
40 | Below I show the contents of this directory on my system for reference:: |
|
40 | Below I show the contents of this directory on my system for reference:: | |
41 |
|
41 | |||
42 |
[html]> ls |
|
42 | [html]> ls | |
43 | about.dat acks.html dist/ ext/ index.html lib/ modindex.html |
|
43 | about.dat acks.html dist/ ext/ index.html lib/ modindex.html | |
44 | stdabout.dat tut/ about.html api/ doc/ icons/ inst/ mac/ ref/ style.css |
|
44 | stdabout.dat tut/ about.html api/ doc/ icons/ inst/ mac/ ref/ style.css | |
45 |
|
45 | |||
46 | You should really make sure this variable is correctly set so that |
|
46 | You should really make sure this variable is correctly set so that | |
47 | Python's pydoc-based help system works. It is a powerful and convenient |
|
47 | Python's pydoc-based help system works. It is a powerful and convenient | |
48 | system with full access to the Python manuals and all modules accessible |
|
48 | system with full access to the Python manuals and all modules accessible | |
49 | to you. |
|
49 | to you. | |
50 |
|
50 | |||
51 | Under Windows it seems that pydoc finds the documentation automatically, |
|
51 | Under Windows it seems that pydoc finds the documentation automatically, | |
52 | so no extra setup appears necessary. |
|
52 | so no extra setup appears necessary. | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | Editor |
|
55 | Editor | |
56 | ====== |
|
56 | ====== | |
57 |
|
57 | |||
58 | The %edit command (and its alias %ed) will invoke the editor set in your |
|
58 | The %edit command (and its alias %ed) will invoke the editor set in your | |
59 | environment as EDITOR. If this variable is not set, it will default to |
|
59 | environment as EDITOR. If this variable is not set, it will default to | |
60 | vi under Linux/Unix and to notepad under Windows. You may want to set |
|
60 | vi under Linux/Unix and to notepad under Windows. You may want to set | |
61 | this variable properly and to a lightweight editor which doesn't take |
|
61 | this variable properly and to a lightweight editor which doesn't take | |
62 | too long to start (that is, something other than a new instance of |
|
62 | too long to start (that is, something other than a new instance of | |
63 | Emacs). This way you can edit multi-line code quickly and with the power |
|
63 | Emacs). This way you can edit multi-line code quickly and with the power | |
64 | of a real editor right inside IPython. |
|
64 | of a real editor right inside IPython. | |
65 |
|
65 | |||
66 | If you are a dedicated Emacs user, you should set up the Emacs server so |
|
66 | If you are a dedicated Emacs user, you should set up the Emacs server so | |
67 | that new requests are handled by the original process. This means that |
|
67 | that new requests are handled by the original process. This means that | |
68 | almost no time is spent in handling the request (assuming an Emacs |
|
68 | almost no time is spent in handling the request (assuming an Emacs | |
69 | process is already running). For this to work, you need to set your |
|
69 | process is already running). For this to work, you need to set your | |
70 | EDITOR environment variable to 'emacsclient'. The code below, supplied |
|
70 | EDITOR environment variable to 'emacsclient'. The code below, supplied | |
71 | by Francois Pinard, can then be used in your .emacs file to enable the |
|
71 | by Francois Pinard, can then be used in your .emacs file to enable the | |
72 | server:: |
|
72 | server:: | |
73 |
|
73 | |||
74 |
(defvar server-buffer-clients) |
|
74 | (defvar server-buffer-clients) | |
75 |
(when (and (fboundp 'server-start) (string-equal (getenv "TERM") 'xterm)) |
|
75 | (when (and (fboundp 'server-start) (string-equal (getenv "TERM") 'xterm)) | |
76 |
(server-start) |
|
76 | (server-start) | |
77 |
(defun fp-kill-server-with-buffer-routine () |
|
77 | (defun fp-kill-server-with-buffer-routine () | |
78 |
(and server-buffer-clients (server-done))) |
|
78 | (and server-buffer-clients (server-done))) | |
79 | (add-hook 'kill-buffer-hook 'fp-kill-server-with-buffer-routine)) |
|
79 | (add-hook 'kill-buffer-hook 'fp-kill-server-with-buffer-routine)) | |
80 |
|
80 | |||
81 | You can also set the value of this editor via the commmand-line option |
|
81 | You can also set the value of this editor via the commmand-line option | |
82 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
82 | '-editor' or in your ipythonrc file. This is useful if you wish to use | |
83 | specifically for IPython an editor different from your typical default |
|
83 | specifically for IPython an editor different from your typical default | |
84 | (and for Windows users who tend to use fewer environment variables). |
|
84 | (and for Windows users who tend to use fewer environment variables). | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | Color |
|
87 | Color | |
88 | ===== |
|
88 | ===== | |
89 |
|
89 | |||
90 | The default IPython configuration has most bells and whistles turned on |
|
90 | The default IPython configuration has most bells and whistles turned on | |
91 | (they're pretty safe). But there's one that may cause problems on some |
|
91 | (they're pretty safe). But there's one that may cause problems on some | |
92 | systems: the use of color on screen for displaying information. This is |
|
92 | systems: the use of color on screen for displaying information. This is | |
93 | very useful, since IPython can show prompts and exception tracebacks |
|
93 | very useful, since IPython can show prompts and exception tracebacks | |
94 | with various colors, display syntax-highlighted source code, and in |
|
94 | with various colors, display syntax-highlighted source code, and in | |
95 | general make it easier to visually parse information. |
|
95 | general make it easier to visually parse information. | |
96 |
|
96 | |||
97 | The following terminals seem to handle the color sequences fine: |
|
97 | The following terminals seem to handle the color sequences fine: | |
98 |
|
98 | |||
99 | * Linux main text console, KDE Konsole, Gnome Terminal, E-term, |
|
99 | * Linux main text console, KDE Konsole, Gnome Terminal, E-term, | |
100 | rxvt, xterm. |
|
100 | rxvt, xterm. | |
101 | * CDE terminal (tested under Solaris). This one boldfaces light colors. |
|
101 | * CDE terminal (tested under Solaris). This one boldfaces light colors. | |
102 | * (X)Emacs buffers. See the emacs_ section for more details on |
|
102 | * (X)Emacs buffers. See the emacs_ section for more details on | |
103 | using IPython with (X)Emacs. |
|
103 | using IPython with (X)Emacs. | |
104 | * A Windows (XP/2k) command prompt with pyreadline_. |
|
104 | * A Windows (XP/2k) command prompt with pyreadline_. | |
105 | * A Windows (XP/2k) CygWin shell. Although some users have reported |
|
105 | * A Windows (XP/2k) CygWin shell. Although some users have reported | |
106 | problems; it is not clear whether there is an issue for everyone |
|
106 | problems; it is not clear whether there is an issue for everyone | |
107 | or only under specific configurations. If you have full color |
|
107 | or only under specific configurations. If you have full color | |
108 | support under cygwin, please post to the IPython mailing list so |
|
108 | support under cygwin, please post to the IPython mailing list so | |
109 | this issue can be resolved for all users. |
|
109 | this issue can be resolved for all users. | |
110 |
|
110 | |||
111 | These have shown problems: |
|
111 | These have shown problems: | |
112 |
|
112 | |||
113 | * Windows command prompt in WinXP/2k logged into a Linux machine via |
|
113 | * Windows command prompt in WinXP/2k logged into a Linux machine via | |
114 | telnet or ssh. |
|
114 | telnet or ssh. | |
115 | * Windows native command prompt in WinXP/2k, without Gary Bishop's |
|
115 | * Windows native command prompt in WinXP/2k, without Gary Bishop's | |
116 | extensions. Once Gary's readline library is installed, the normal |
|
116 | extensions. Once Gary's readline library is installed, the normal | |
117 | WinXP/2k command prompt works perfectly. |
|
117 | WinXP/2k command prompt works perfectly. | |
118 |
|
118 | |||
119 | Currently the following color schemes are available: |
|
119 | Currently the following color schemes are available: | |
120 |
|
120 | |||
121 | * NoColor: uses no color escapes at all (all escapes are empty '' '' |
|
121 | * NoColor: uses no color escapes at all (all escapes are empty '' '' | |
122 | strings). This 'scheme' is thus fully safe to use in any terminal. |
|
122 | strings). This 'scheme' is thus fully safe to use in any terminal. | |
123 | * Linux: works well in Linux console type environments: dark |
|
123 | * Linux: works well in Linux console type environments: dark | |
124 | background with light fonts. It uses bright colors for |
|
124 | background with light fonts. It uses bright colors for | |
125 | information, so it is difficult to read if you have a light |
|
125 | information, so it is difficult to read if you have a light | |
126 | colored background. |
|
126 | colored background. | |
127 | * LightBG: the basic colors are similar to those in the Linux scheme |
|
127 | * LightBG: the basic colors are similar to those in the Linux scheme | |
128 | but darker. It is easy to read in terminals with light backgrounds. |
|
128 | but darker. It is easy to read in terminals with light backgrounds. | |
129 |
|
129 | |||
130 | IPython uses colors for two main groups of things: prompts and |
|
130 | IPython uses colors for two main groups of things: prompts and | |
131 | tracebacks which are directly printed to the terminal, and the object |
|
131 | tracebacks which are directly printed to the terminal, and the object | |
132 | introspection system which passes large sets of data through a pager. |
|
132 | introspection system which passes large sets of data through a pager. | |
133 |
|
133 | |||
134 |
|
134 | |||
135 | Input/Output prompts and exception tracebacks |
|
135 | Input/Output prompts and exception tracebacks | |
136 | ============================================= |
|
136 | ============================================= | |
137 |
|
137 | |||
138 | You can test whether the colored prompts and tracebacks work on your |
|
138 | You can test whether the colored prompts and tracebacks work on your | |
139 | system interactively by typing '%colors Linux' at the prompt (use |
|
139 | system interactively by typing '%colors Linux' at the prompt (use | |
140 | '%colors LightBG' if your terminal has a light background). If the input |
|
140 | '%colors LightBG' if your terminal has a light background). If the input | |
141 | prompt shows garbage like:: |
|
141 | prompt shows garbage like:: | |
142 |
|
142 | |||
143 | [0;32mIn [[1;32m1[0;32m]: [0;00m |
|
143 | [0;32mIn [[1;32m1[0;32m]: [0;00m | |
144 |
|
144 | |||
145 | instead of (in color) something like:: |
|
145 | instead of (in color) something like:: | |
146 |
|
146 | |||
147 | In [1]: |
|
147 | In [1]: | |
148 |
|
148 | |||
149 | this means that your terminal doesn't properly handle color escape |
|
149 | this means that your terminal doesn't properly handle color escape | |
150 | sequences. You can go to a 'no color' mode by typing '%colors NoColor'. |
|
150 | sequences. You can go to a 'no color' mode by typing '%colors NoColor'. | |
151 |
|
151 | |||
152 | You can try using a different terminal emulator program (Emacs users, |
|
152 | You can try using a different terminal emulator program (Emacs users, | |
153 | see below). To permanently set your color preferences, edit the file |
|
153 | see below). To permanently set your color preferences, edit the file | |
154 | $HOME/.ipython/ipythonrc and set the colors option to the desired value. |
|
154 | $HOME/.ipython/ipythonrc and set the colors option to the desired value. | |
155 |
|
155 | |||
156 |
|
156 | |||
157 | Object details (types, docstrings, source code, etc.) |
|
157 | Object details (types, docstrings, source code, etc.) | |
158 | ===================================================== |
|
158 | ===================================================== | |
159 |
|
159 | |||
160 | IPython has a set of special functions for studying the objects you |
|
160 | IPython has a set of special functions for studying the objects you | |
161 | are working with, discussed in detail in Sec. `dynamic object |
|
161 | are working with, discussed in detail in Sec. `dynamic object | |
162 | information`_. But this system relies on passing information which is |
|
162 | information`_. But this system relies on passing information which is | |
163 | longer than your screen through a data pager, such as the common Unix |
|
163 | longer than your screen through a data pager, such as the common Unix | |
164 | less and more programs. In order to be able to see this information in |
|
164 | less and more programs. In order to be able to see this information in | |
165 | color, your pager needs to be properly configured. I strongly |
|
165 | color, your pager needs to be properly configured. I strongly | |
166 | recommend using less instead of more, as it seems that more simply can |
|
166 | recommend using less instead of more, as it seems that more simply can | |
167 | not understand colored text correctly. |
|
167 | not understand colored text correctly. | |
168 |
|
168 | |||
169 | In order to configure less as your default pager, do the following: |
|
169 | In order to configure less as your default pager, do the following: | |
170 |
|
170 | |||
171 | 1. Set the environment PAGER variable to less. |
|
171 | 1. Set the environment PAGER variable to less. | |
172 | 2. Set the environment LESS variable to -r (plus any other options |
|
172 | 2. Set the environment LESS variable to -r (plus any other options | |
173 | you always want to pass to less by default). This tells less to |
|
173 | you always want to pass to less by default). This tells less to | |
174 | properly interpret control sequences, which is how color |
|
174 | properly interpret control sequences, which is how color | |
175 | information is given to your terminal. |
|
175 | information is given to your terminal. | |
176 |
|
176 | |||
177 | For the csh or tcsh shells, add to your ~/.cshrc file the lines:: |
|
177 | For the csh or tcsh shells, add to your ~/.cshrc file the lines:: | |
178 |
|
178 | |||
179 |
setenv PAGER less |
|
179 | setenv PAGER less | |
180 | setenv LESS -r |
|
180 | setenv LESS -r | |
181 |
|
181 | |||
182 | There is similar syntax for other Unix shells, look at your system |
|
182 | There is similar syntax for other Unix shells, look at your system | |
183 | documentation for details. |
|
183 | documentation for details. | |
184 |
|
184 | |||
185 | If you are on a system which lacks proper data pagers (such as Windows), |
|
185 | If you are on a system which lacks proper data pagers (such as Windows), | |
186 | IPython will use a very limited builtin pager. |
|
186 | IPython will use a very limited builtin pager. | |
187 |
|
187 | |||
188 | .. _emacs: |
|
188 | .. _emacs: | |
189 |
|
189 | |||
190 | (X)Emacs configuration |
|
190 | (X)Emacs configuration | |
191 | ====================== |
|
191 | ====================== | |
192 |
|
192 | |||
193 | Thanks to the work of Alexander Schmolck and Prabhu Ramachandran, |
|
193 | Thanks to the work of Alexander Schmolck and Prabhu Ramachandran, | |
194 | currently (X)Emacs and IPython get along very well. |
|
194 | currently (X)Emacs and IPython get along very well. | |
195 |
|
195 | |||
196 | Important note: You will need to use a recent enough version of |
|
196 | Important note: You will need to use a recent enough version of | |
197 | python-mode.el, along with the file ipython.el. You can check that the |
|
197 | python-mode.el, along with the file ipython.el. You can check that the | |
198 | version you have of python-mode.el is new enough by either looking at |
|
198 | version you have of python-mode.el is new enough by either looking at | |
199 | the revision number in the file itself, or asking for it in (X)Emacs via |
|
199 | the revision number in the file itself, or asking for it in (X)Emacs via | |
200 | M-x py-version. Versions 4.68 and newer contain the necessary fixes for |
|
200 | M-x py-version. Versions 4.68 and newer contain the necessary fixes for | |
201 | proper IPython support. |
|
201 | proper IPython support. | |
202 |
|
202 | |||
203 | The file ipython.el is included with the IPython distribution, in the |
|
203 | The file ipython.el is included with the IPython distribution, in the | |
204 | documentation directory (where this manual resides in PDF and HTML |
|
204 | documentation directory (where this manual resides in PDF and HTML | |
205 | formats). |
|
205 | formats). | |
206 |
|
206 | |||
207 | Once you put these files in your Emacs path, all you need in your .emacs |
|
207 | Once you put these files in your Emacs path, all you need in your .emacs | |
208 | file is:: |
|
208 | file is:: | |
209 |
|
209 | |||
210 |
(require 'ipython) |
|
210 | (require 'ipython) | |
211 |
|
211 | |||
212 | This should give you full support for executing code snippets via |
|
212 | This should give you full support for executing code snippets via | |
213 | IPython, opening IPython as your Python shell via C-c !, etc. |
|
213 | IPython, opening IPython as your Python shell via ``C-c !``, etc. | |
|
214 | ||||
|
215 | You can customize the arguments passed to the IPython instance at startup by | |||
|
216 | setting the ``py-python-command-args`` variable. For example, to start always | |||
|
217 | in ``pylab`` mode with hardcoded light-background colors, you can use:: | |||
|
218 | ||||
|
219 | (setq py-python-command-args '("-pylab" "-colors" "LightBG")) | |||
214 |
|
220 | |||
215 | If you happen to get garbage instead of colored prompts as described in |
|
221 | If you happen to get garbage instead of colored prompts as described in | |
216 | the previous section, you may need to set also in your .emacs file:: |
|
222 | the previous section, you may need to set also in your .emacs file:: | |
217 |
|
223 | |||
218 |
(setq ansi-color-for-comint-mode t) |
|
224 | (setq ansi-color-for-comint-mode t) | |
219 |
|
||||
220 |
|
225 | |||
221 | Notes: |
|
226 | Notes: | |
222 |
|
227 | |||
223 | * There is one caveat you should be aware of: you must start the |
|
228 | * There is one caveat you should be aware of: you must start the | |
224 | IPython shell before attempting to execute any code regions via |
|
229 | IPython shell before attempting to execute any code regions via | |
225 | ``C-c |``. Simply type C-c ! to start IPython before passing any code |
|
230 | ``C-c |``. Simply type C-c ! to start IPython before passing any code | |
226 | regions to the interpreter, and you shouldn't experience any |
|
231 | regions to the interpreter, and you shouldn't experience any | |
227 | problems. |
|
232 | problems. | |
228 | This is due to a bug in Python itself, which has been fixed for |
|
233 | This is due to a bug in Python itself, which has been fixed for | |
229 | Python 2.3, but exists as of Python 2.2.2 (reported as SF bug [ |
|
234 | Python 2.3, but exists as of Python 2.2.2 (reported as SF bug [ | |
230 | 737947 ]). |
|
235 | 737947 ]). | |
231 | * The (X)Emacs support is maintained by Alexander Schmolck, so all |
|
236 | * The (X)Emacs support is maintained by Alexander Schmolck, so all | |
232 | comments/requests should be directed to him through the IPython |
|
237 | comments/requests should be directed to him through the IPython | |
233 | mailing lists. |
|
238 | mailing lists. | |
234 | * This code is still somewhat experimental so it's a bit rough |
|
239 | * This code is still somewhat experimental so it's a bit rough | |
235 | around the edges (although in practice, it works quite well). |
|
240 | around the edges (although in practice, it works quite well). | |
236 | * Be aware that if you customize py-python-command previously, this |
|
241 | * Be aware that if you customize py-python-command previously, this | |
237 | value will override what ipython.el does (because loading the |
|
242 | value will override what ipython.el does (because loading the | |
238 | customization variables comes later). No newline at end of file |
|
243 | customization variables comes later). |
General Comments 0
You need to be logged in to leave comments.
Login now