##// END OF EJS Templates
Update docs for ipython.el....
Fernando Perez -
Show More
@@ -1,483 +1,490 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
35 ;; setting the ``py-python-command-args`` variable. For example, to start
36 ;; always in ``pylab`` mode with hardcoded light-background colors, you can use:
37 ;; (setq py-python-command-args '("-pylab" "-colors" "LightBG"))
38 ;;
39 ;;
34 ;; NOTE: This mode is currently somewhat alpha and although I hope that it
40 ;; NOTE: This mode is currently somewhat alpha and although I hope that it
35 ;; will work fine for most cases, doing certain things (like the
41 ;; will work fine for most cases, doing certain things (like the
36 ;; autocompletion and a decent scheme to switch between python interpreters)
42 ;; autocompletion and a decent scheme to switch between python interpreters)
37 ;; properly will also require changes to ipython that will likely have to wait
43 ;; properly will also require changes to ipython that will likely have to wait
38 ;; for a larger rewrite scheduled some time in the future.
44 ;; for a larger rewrite scheduled some time in the future.
39 ;;
45 ;;
40 ;; Also note that you currently NEED THE CVS VERSION OF PYTHON.EL.
46 ;; Also note that you currently NEED THE CVS VERSION OF PYTHON.EL.
41 ;;
47 ;;
42 ;; Further note that I don't know whether this runs under windows or not and
48 ;; Further note that I don't know whether this runs under windows or not and
43 ;; that if it doesn't I can't really help much, not being afflicted myself.
49 ;; that if it doesn't I can't really help much, not being afflicted myself.
44 ;;
50 ;;
45 ;;
51 ;;
46 ;; Hints for effective usage
52 ;; Hints for effective usage
47 ;; -------------------------
53 ;; -------------------------
48 ;;
54 ;;
49 ;; - IMO the best feature by far of the ipython/emacs combo is how much easier it
55 ;; - IMO the best feature by far of the ipython/emacs combo is how much easier
50 ;; makes it to find and fix bugs thanks to the ``%pdb on``/ pdbtrack combo. Try
56 ;; it makes it to find and fix bugs thanks to the ``%pdb on or %debug``/
51 ;; it: first in the ipython to shell do ``%pdb on`` then do something that will
57 ;; pdbtrack combo. Try it: first in the ipython to shell do ``%pdb on`` then
52 ;; raise an exception (FIXME nice example) -- and be amazed how easy it is to
58 ;; do something that will raise an exception (FIXME nice example), or type
53 ;; inspect the live objects in each stack frames and to jump to the
59 ;; ``%debug`` after the exception has been raised. YOu'll be amazed at how
54 ;; corresponding sourcecode locations as you walk up and down the stack trace
60 ;; easy it is to inspect the live objects in each stack frames and to jump to
55 ;; (even without ``%pdb on`` you can always use ``C-c -`` (`py-up-exception')
61 ;; the corresponding sourcecode locations as you walk up and down the stack
56 ;; to jump to the corresponding source code locations).
62 ;; trace (even without ``%pdb on`` you can always use ``C-c -``
63 ;; (`py-up-exception') to jump to the corresponding source code locations).
57 ;;
64 ;;
58 ;; - emacs gives you much more powerful commandline editing and output searching
65 ;; - emacs gives you much more powerful commandline editing and output searching
59 ;; capabilities than ipython-standalone -- isearch is your friend if you
66 ;; capabilities than ipython-standalone -- isearch is your friend if you
60 ;; quickly want to print 'DEBUG ...' to stdout out etc.
67 ;; quickly want to print 'DEBUG ...' to stdout out etc.
61 ;;
68 ;;
62 ;; - This is not really specific to ipython, but for more convenient history
69 ;; - This is not really specific to ipython, but for more convenient history
63 ;; access you might want to add something like the following to *the beggining*
70 ;; access you might want to add something like the following to *the beggining*
64 ;; of your ``.emacs`` (if you want behavior that's more similar to stand-alone
71 ;; of your ``.emacs`` (if you want behavior that's more similar to stand-alone
65 ;; ipython, you can change ``meta p`` etc. for ``control p``)::
72 ;; ipython, you can change ``meta p`` etc. for ``control p``)::
66 ;;
73 ;;
67 ;; (require 'comint)
74 ;; (require 'comint)
68 ;; (define-key comint-mode-map [(meta p)]
75 ;; (define-key comint-mode-map [(meta p)]
69 ;; 'comint-previous-matching-input-from-input)
76 ;; 'comint-previous-matching-input-from-input)
70 ;; (define-key comint-mode-map [(meta n)]
77 ;; (define-key comint-mode-map [(meta n)]
71 ;; 'comint-next-matching-input-from-input)
78 ;; 'comint-next-matching-input-from-input)
72 ;; (define-key comint-mode-map [(control meta n)]
79 ;; (define-key comint-mode-map [(control meta n)]
73 ;; 'comint-next-input)
80 ;; 'comint-next-input)
74 ;; (define-key comint-mode-map [(control meta p)]
81 ;; (define-key comint-mode-map [(control meta p)]
75 ;; 'comint-previous-input)
82 ;; 'comint-previous-input)
76 ;;
83 ;;
77 ;; - Be aware that if you customize py-python-command previously, this value
84 ;; - Be aware that if you customize py-python-command previously, this value
78 ;; will override what ipython.el does (because loading the customization
85 ;; will override what ipython.el does (because loading the customization
79 ;; variables comes later).
86 ;; variables comes later).
80 ;;
87 ;;
81 ;; Please send comments and feedback to the ipython-list
88 ;; Please send comments and feedback to the ipython-list
82 ;; (<ipython-user@scipy.net>) where I (a.s.) or someone else will try to
89 ;; (<ipython-user@scipy.org>) where I (a.s.) or someone else will try to
83 ;; answer them (it helps if you specify your emacs version, OS etc;
90 ;; answer them (it helps if you specify your emacs version, OS etc;
84 ;; familiarity with <http://www.catb.org/~esr/faqs/smart-questions.html> might
91 ;; familiarity with <http://www.catb.org/~esr/faqs/smart-questions.html> might
85 ;; speed up things further).
92 ;; speed up things further).
86 ;;
93 ;;
87 ;; Footnotes:
94 ;; Footnotes:
88 ;;
95 ;;
89 ;; [1] If you don't know what `load-path' is, C-h v load-path will tell
96 ;; [1] If you don't know what `load-path' is, C-h v load-path will tell
90 ;; you; if required you can also add a new directory. So assuming that
97 ;; you; if required you can also add a new directory. So assuming that
91 ;; ipython.el resides in ~/el/, put this in your emacs:
98 ;; ipython.el resides in ~/el/, put this in your emacs:
92 ;;
99 ;;
93 ;;
100 ;;
94 ;; (add-to-list 'load-path "~/el")
101 ;; (add-to-list 'load-path "~/el")
95 ;; (setq ipython-command "/some-path/ipython")
102 ;; (setq ipython-command "/some-path/ipython")
96 ;; (require 'ipython)
103 ;; (require 'ipython)
97 ;;
104 ;;
98 ;;
105 ;;
99 ;;
106 ;;
100 ;;
107 ;;
101 ;; TODO:
108 ;; TODO:
102 ;; - do autocompletion properly
109 ;; - do autocompletion properly
103 ;; - implement a proper switching between python interpreters
110 ;; - implement a proper switching between python interpreters
104 ;;
111 ;;
105 ;; BUGS:
112 ;; BUGS:
106 ;; - neither::
113 ;; - neither::
107 ;;
114 ;;
108 ;; (py-shell "-c print 'FOOBAR'")
115 ;; (py-shell "-c print 'FOOBAR'")
109 ;;
116 ;;
110 ;; nor::
117 ;; nor::
111 ;;
118 ;;
112 ;; (let ((py-python-command-args (append py-python-command-args
119 ;; (let ((py-python-command-args (append py-python-command-args
113 ;; '("-c" "print 'FOOBAR'"))))
120 ;; '("-c" "print 'FOOBAR'"))))
114 ;; (py-shell))
121 ;; (py-shell))
115 ;;
122 ;;
116 ;; seem to print anything as they should
123 ;; seem to print anything as they should
117 ;;
124 ;;
118 ;; - look into init priority issues with `py-python-command' (if it's set
125 ;; - look into init priority issues with `py-python-command' (if it's set
119 ;; via custom)
126 ;; via custom)
120
127
121
128
122 ;;; Code
129 ;;; Code
123 (require 'cl)
130 (require 'cl)
124 (require 'shell)
131 (require 'shell)
125 (require 'executable)
132 (require 'executable)
126 (require 'ansi-color)
133 (require 'ansi-color)
127
134
128 (defcustom ipython-command "ipython"
135 (defcustom ipython-command "ipython"
129 "*Shell command used to start ipython."
136 "*Shell command used to start ipython."
130 :type 'string
137 :type 'string
131 :group 'python)
138 :group 'python)
132
139
133 ;; Users can set this to nil
140 ;; Users can set this to nil
134 (defvar py-shell-initial-switch-buffers t
141 (defvar py-shell-initial-switch-buffers t
135 "If nil, don't switch to the *Python* buffer on the first call to
142 "If nil, don't switch to the *Python* buffer on the first call to
136 `py-shell'.")
143 `py-shell'.")
137
144
138 (defvar ipython-backup-of-py-python-command nil
145 (defvar ipython-backup-of-py-python-command nil
139 "HACK")
146 "HACK")
140
147
141
148
142 (defvar ipython-de-input-prompt-regexp "\\(?:
149 (defvar ipython-de-input-prompt-regexp "\\(?:
143 In \\[[0-9]+\\]: *.*
150 In \\[[0-9]+\\]: *.*
144 ----+> \\(.*
151 ----+> \\(.*
145 \\)[\n]?\\)\\|\\(?:
152 \\)[\n]?\\)\\|\\(?:
146 In \\[[0-9]+\\]: *\\(.*
153 In \\[[0-9]+\\]: *\\(.*
147 \\)\\)\\|^[ ]\\{3\\}[.]\\{3,\\}: *\\(.*
154 \\)\\)\\|^[ ]\\{3\\}[.]\\{3,\\}: *\\(.*
148 \\)"
155 \\)"
149 "A regular expression to match the IPython input prompt and the python
156 "A regular expression to match the IPython input prompt and the python
150 command after it. The first match group is for a command that is rewritten,
157 command after it. The first match group is for a command that is rewritten,
151 the second for a 'normal' command, and the third for a multiline command.")
158 the second for a 'normal' command, and the third for a multiline command.")
152 (defvar ipython-de-output-prompt-regexp "^Out\\[[0-9]+\\]: "
159 (defvar ipython-de-output-prompt-regexp "^Out\\[[0-9]+\\]: "
153 "A regular expression to match the output prompt of IPython.")
160 "A regular expression to match the output prompt of IPython.")
154
161
155
162
156 (if (not (executable-find ipython-command))
163 (if (not (executable-find ipython-command))
157 (message (format "Can't find executable %s - ipython.el *NOT* activated!!!"
164 (message (format "Can't find executable %s - ipython.el *NOT* activated!!!"
158 ipython-command))
165 ipython-command))
159 ;; XXX load python-mode, so that we can screw around with its variables
166 ;; XXX load python-mode, so that we can screw around with its variables
160 ;; this has the disadvantage that python-mode is loaded even if no
167 ;; this has the disadvantage that python-mode is loaded even if no
161 ;; python-file is ever edited etc. but it means that `py-shell' works
168 ;; python-file is ever edited etc. but it means that `py-shell' works
162 ;; without loading a python-file first. Obviously screwing around with
169 ;; without loading a python-file first. Obviously screwing around with
163 ;; python-mode's variables like this is a mess, but well.
170 ;; python-mode's variables like this is a mess, but well.
164 (require 'python-mode)
171 (require 'python-mode)
165 ;; turn on ansi colors for ipython and activate completion
172 ;; turn on ansi colors for ipython and activate completion
166 (defun ipython-shell-hook ()
173 (defun ipython-shell-hook ()
167 ;; the following is to synchronize dir-changes
174 ;; the following is to synchronize dir-changes
168 (make-local-variable 'shell-dirstack)
175 (make-local-variable 'shell-dirstack)
169 (setq shell-dirstack nil)
176 (setq shell-dirstack nil)
170 (make-local-variable 'shell-last-dir)
177 (make-local-variable 'shell-last-dir)
171 (setq shell-last-dir nil)
178 (setq shell-last-dir nil)
172 (make-local-variable 'shell-dirtrackp)
179 (make-local-variable 'shell-dirtrackp)
173 (setq shell-dirtrackp t)
180 (setq shell-dirtrackp t)
174 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
181 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
175
182
176 (ansi-color-for-comint-mode-on)
183 (ansi-color-for-comint-mode-on)
177 (define-key py-shell-map [tab] 'ipython-complete)
184 (define-key py-shell-map [tab] 'ipython-complete)
178 ;; Add this so that tab-completion works both in X11 frames and inside
185 ;; Add this so that tab-completion works both in X11 frames and inside
179 ;; terminals (such as when emacs is called with -nw).
186 ;; terminals (such as when emacs is called with -nw).
180 (define-key py-shell-map "\t" 'ipython-complete)
187 (define-key py-shell-map "\t" 'ipython-complete)
181 ;;XXX this is really just a cheap hack, it only completes symbols in the
188 ;;XXX this is really just a cheap hack, it only completes symbols in the
182 ;;interactive session -- useful nonetheless.
189 ;;interactive session -- useful nonetheless.
183 (define-key py-mode-map [(meta tab)] 'ipython-complete)
190 (define-key py-mode-map [(meta tab)] 'ipython-complete)
184
191
185 )
192 )
186 (add-hook 'py-shell-hook 'ipython-shell-hook)
193 (add-hook 'py-shell-hook 'ipython-shell-hook)
187 ;; Regular expression that describes tracebacks for IPython in context and
194 ;; Regular expression that describes tracebacks for IPython in context and
188 ;; verbose mode.
195 ;; verbose mode.
189
196
190 ;;Adapt python-mode settings for ipython.
197 ;;Adapt python-mode settings for ipython.
191 ;; (this works for %xmode 'verbose' or 'context')
198 ;; (this works for %xmode 'verbose' or 'context')
192
199
193 ;; XXX putative regexps for syntax errors; unfortunately the
200 ;; XXX putative regexps for syntax errors; unfortunately the
194 ;; current python-mode traceback-line-re scheme is too primitive,
201 ;; current python-mode traceback-line-re scheme is too primitive,
195 ;; so it's either matching syntax errors, *or* everything else
202 ;; so it's either matching syntax errors, *or* everything else
196 ;; (XXX: should ask Fernando for a change)
203 ;; (XXX: should ask Fernando for a change)
197 ;;"^ File \"\\(.*?\\)\", line \\([0-9]+\\).*\n.*\n.*\nSyntaxError:"
204 ;;"^ File \"\\(.*?\\)\", line \\([0-9]+\\).*\n.*\n.*\nSyntaxError:"
198 ;;^ File \"\\(.*?\\)\", line \\([0-9]+\\)"
205 ;;^ File \"\\(.*?\\)\", line \\([0-9]+\\)"
199
206
200 (setq py-traceback-line-re
207 (setq py-traceback-line-re
201 "\\(^[^\t >].+?\\.py\\).*\n +[0-9]+[^\00]*?\n-+> \\([0-9]+\\)+")
208 "\\(^[^\t >].+?\\.py\\).*\n +[0-9]+[^\00]*?\n-+> \\([0-9]+\\)+")
202
209
203
210
204 ;; Recognize the ipython pdb, whose prompt is 'ipdb>' or 'ipydb>'
211 ;; Recognize the ipython pdb, whose prompt is 'ipdb>' or 'ipydb>'
205 ;;instead of '(Pdb)'
212 ;;instead of '(Pdb)'
206 (setq py-pdbtrack-input-prompt "\n[(<]*[Ii]?[Pp]y?db[>)]+ ")
213 (setq py-pdbtrack-input-prompt "\n[(<]*[Ii]?[Pp]y?db[>)]+ ")
207 (setq pydb-pydbtrack-input-prompt "\n[(]*ipydb[>)]+ ")
214 (setq pydb-pydbtrack-input-prompt "\n[(]*ipydb[>)]+ ")
208
215
209 (setq py-shell-input-prompt-1-regexp "^In \\[[0-9]+\\]: *"
216 (setq py-shell-input-prompt-1-regexp "^In \\[[0-9]+\\]: *"
210 py-shell-input-prompt-2-regexp "^ [.][.][.]+: *" )
217 py-shell-input-prompt-2-regexp "^ [.][.][.]+: *" )
211 ;; select a suitable color-scheme
218 ;; select a suitable color-scheme
212 (unless (member "-colors" py-python-command-args)
219 (unless (member "-colors" py-python-command-args)
213 (setq py-python-command-args
220 (setq py-python-command-args
214 (nconc py-python-command-args
221 (nconc py-python-command-args
215 (list "-colors"
222 (list "-colors"
216 (cond
223 (cond
217 ((eq frame-background-mode 'dark)
224 ((eq frame-background-mode 'dark)
218 "Linux")
225 "Linux")
219 ((eq frame-background-mode 'light)
226 ((eq frame-background-mode 'light)
220 "LightBG")
227 "LightBG")
221 (t ; default (backg-mode isn't always set by XEmacs)
228 (t ; default (backg-mode isn't always set by XEmacs)
222 "LightBG"))))))
229 "LightBG"))))))
223 (unless (equal ipython-backup-of-py-python-command py-python-command)
230 (unless (equal ipython-backup-of-py-python-command py-python-command)
224 (setq ipython-backup-of-py-python-command py-python-command))
231 (setq ipython-backup-of-py-python-command py-python-command))
225 (setq py-python-command ipython-command))
232 (setq py-python-command ipython-command))
226
233
227
234
228 ;; MODIFY py-shell so that it loads the editing history
235 ;; MODIFY py-shell so that it loads the editing history
229 (defadvice py-shell (around py-shell-with-history)
236 (defadvice py-shell (around py-shell-with-history)
230 "Add persistent command-history support (in
237 "Add persistent command-history support (in
231 $PYTHONHISTORY (or \"~/.ipython/history\", if we use IPython)). Also, if
238 $PYTHONHISTORY (or \"~/.ipython/history\", if we use IPython)). Also, if
232 `py-shell-initial-switch-buffers' is nil, it only switches to *Python* if that
239 `py-shell-initial-switch-buffers' is nil, it only switches to *Python* if that
233 buffer already exists."
240 buffer already exists."
234 (if (comint-check-proc "*Python*")
241 (if (comint-check-proc "*Python*")
235 ad-do-it
242 ad-do-it
236 (setq comint-input-ring-file-name
243 (setq comint-input-ring-file-name
237 (if (string-equal py-python-command ipython-command)
244 (if (string-equal py-python-command ipython-command)
238 (concat (or (getenv "IPYTHONDIR") "~/.ipython") "/history")
245 (concat (or (getenv "IPYTHONDIR") "~/.ipython") "/history")
239 (or (getenv "PYTHONHISTORY") "~/.python-history.py")))
246 (or (getenv "PYTHONHISTORY") "~/.python-history.py")))
240 (comint-read-input-ring t)
247 (comint-read-input-ring t)
241 (let ((buf (current-buffer)))
248 (let ((buf (current-buffer)))
242 ad-do-it
249 ad-do-it
243 (unless py-shell-initial-switch-buffers
250 (unless py-shell-initial-switch-buffers
244 (switch-to-buffer-other-window buf)))))
251 (switch-to-buffer-other-window buf)))))
245 (ad-activate 'py-shell)
252 (ad-activate 'py-shell)
246 ;; (defadvice py-execute-region (before py-execute-buffer-ensure-process)
253 ;; (defadvice py-execute-region (before py-execute-buffer-ensure-process)
247 ;; "HACK: test that ipython is already running before executing something.
254 ;; "HACK: test that ipython is already running before executing something.
248 ;; Doing this properly seems not worth the bother (unless people actually
255 ;; Doing this properly seems not worth the bother (unless people actually
249 ;; request it)."
256 ;; request it)."
250 ;; (unless (comint-check-proc "*Python*")
257 ;; (unless (comint-check-proc "*Python*")
251 ;; (error "Sorry you have to first do M-x py-shell to send something to ipython.")))
258 ;; (error "Sorry you have to first do M-x py-shell to send something to ipython.")))
252 ;; (ad-activate 'py-execute-region)
259 ;; (ad-activate 'py-execute-region)
253
260
254 (defadvice py-execute-region (around py-execute-buffer-ensure-process)
261 (defadvice py-execute-region (around py-execute-buffer-ensure-process)
255 "HACK: if `py-shell' is not active or ASYNC is explicitly desired, fall back
262 "HACK: if `py-shell' is not active or ASYNC is explicitly desired, fall back
256 to python instead of ipython."
263 to python instead of ipython."
257 (let ((py-which-shell (if (and (comint-check-proc "*Python*") (not async))
264 (let ((py-which-shell (if (and (comint-check-proc "*Python*") (not async))
258 py-python-command
265 py-python-command
259 ipython-backup-of-py-python-command)))
266 ipython-backup-of-py-python-command)))
260 ad-do-it))
267 ad-do-it))
261 (ad-activate 'py-execute-region)
268 (ad-activate 'py-execute-region)
262
269
263 (defun ipython-to-doctest (start end)
270 (defun ipython-to-doctest (start end)
264 "Transform a cut-and-pasted bit from an IPython session into something that
271 "Transform a cut-and-pasted bit from an IPython session into something that
265 looks like it came from a normal interactive python session, so that it can
272 looks like it came from a normal interactive python session, so that it can
266 be used in doctests. Example:
273 be used in doctests. Example:
267
274
268
275
269 In [1]: import sys
276 In [1]: import sys
270
277
271 In [2]: sys.stdout.write 'Hi!\n'
278 In [2]: sys.stdout.write 'Hi!\n'
272 ------> sys.stdout.write ('Hi!\n')
279 ------> sys.stdout.write ('Hi!\n')
273 Hi!
280 Hi!
274
281
275 In [3]: 3 + 4
282 In [3]: 3 + 4
276 Out[3]: 7
283 Out[3]: 7
277
284
278 gets converted to:
285 gets converted to:
279
286
280 >>> import sys
287 >>> import sys
281 >>> sys.stdout.write ('Hi!\n')
288 >>> sys.stdout.write ('Hi!\n')
282 Hi!
289 Hi!
283 >>> 3 + 4
290 >>> 3 + 4
284 7
291 7
285
292
286 "
293 "
287 (interactive "*r\n")
294 (interactive "*r\n")
288 ;(message (format "###DEBUG s:%de:%d" start end))
295 ;(message (format "###DEBUG s:%de:%d" start end))
289 (save-excursion
296 (save-excursion
290 (save-match-data
297 (save-match-data
291 ;; replace ``In [3]: bla`` with ``>>> bla`` and
298 ;; replace ``In [3]: bla`` with ``>>> bla`` and
292 ;; ``... : bla`` with ``... bla``
299 ;; ``... : bla`` with ``... bla``
293 (goto-char start)
300 (goto-char start)
294 (while (re-search-forward ipython-de-input-prompt-regexp end t)
301 (while (re-search-forward ipython-de-input-prompt-regexp end t)
295 ;(message "finding 1")
302 ;(message "finding 1")
296 (cond ((match-string 3) ;continued
303 (cond ((match-string 3) ;continued
297 (replace-match "... \\3" t nil))
304 (replace-match "... \\3" t nil))
298 (t
305 (t
299 (replace-match ">>> \\1\\2" t nil))))
306 (replace-match ">>> \\1\\2" t nil))))
300 ;; replace ``
307 ;; replace ``
301 (goto-char start)
308 (goto-char start)
302 (while (re-search-forward ipython-de-output-prompt-regexp end t)
309 (while (re-search-forward ipython-de-output-prompt-regexp end t)
303 (replace-match "" t nil)))))
310 (replace-match "" t nil)))))
304
311
305 (defvar ipython-completion-command-string
312 (defvar ipython-completion-command-string
306 "print ';'.join(__IP.Completer.all_completions('%s')) #PYTHON-MODE SILENT\n"
313 "print ';'.join(__IP.Completer.all_completions('%s')) #PYTHON-MODE SILENT\n"
307 "The string send to ipython to query for all possible completions")
314 "The string send to ipython to query for all possible completions")
308
315
309
316
310 ;; xemacs doesn't have `comint-preoutput-filter-functions' so we'll try the
317 ;; xemacs doesn't have `comint-preoutput-filter-functions' so we'll try the
311 ;; following wonderful hack to work around this case
318 ;; following wonderful hack to work around this case
312 (if (featurep 'xemacs)
319 (if (featurep 'xemacs)
313 ;;xemacs
320 ;;xemacs
314 (defun ipython-complete ()
321 (defun ipython-complete ()
315 "Try to complete the python symbol before point. Only knows about the stuff
322 "Try to complete the python symbol before point. Only knows about the stuff
316 in the current *Python* session."
323 in the current *Python* session."
317 (interactive)
324 (interactive)
318 (let* ((ugly-return nil)
325 (let* ((ugly-return nil)
319 (sep ";")
326 (sep ";")
320 (python-process (or (get-buffer-process (current-buffer))
327 (python-process (or (get-buffer-process (current-buffer))
321 ;XXX hack for .py buffers
328 ;XXX hack for .py buffers
322 (get-process py-which-bufname)))
329 (get-process py-which-bufname)))
323 ;; XXX currently we go backwards to find the beginning of an
330 ;; XXX currently we go backwards to find the beginning of an
324 ;; expression part; a more powerful approach in the future might be
331 ;; expression part; a more powerful approach in the future might be
325 ;; to let ipython have the complete line, so that context can be used
332 ;; to let ipython have the complete line, so that context can be used
326 ;; to do things like filename completion etc.
333 ;; to do things like filename completion etc.
327 (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_." (point-at-bol))
334 (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_." (point-at-bol))
328 (point)))
335 (point)))
329 (end (point))
336 (end (point))
330 (pattern (buffer-substring-no-properties beg end))
337 (pattern (buffer-substring-no-properties beg end))
331 (completions nil)
338 (completions nil)
332 (completion-table nil)
339 (completion-table nil)
333 completion
340 completion
334 (comint-output-filter-functions
341 (comint-output-filter-functions
335 (append comint-output-filter-functions
342 (append comint-output-filter-functions
336 '(ansi-color-filter-apply
343 '(ansi-color-filter-apply
337 (lambda (string)
344 (lambda (string)
338 ;(message (format "DEBUG filtering: %s" string))
345 ;(message (format "DEBUG filtering: %s" string))
339 (setq ugly-return (concat ugly-return string))
346 (setq ugly-return (concat ugly-return string))
340 (delete-region comint-last-output-start
347 (delete-region comint-last-output-start
341 (process-mark (get-buffer-process (current-buffer)))))))))
348 (process-mark (get-buffer-process (current-buffer)))))))))
342 ;(message (format "#DEBUG pattern: '%s'" pattern))
349 ;(message (format "#DEBUG pattern: '%s'" pattern))
343 (process-send-string python-process
350 (process-send-string python-process
344 (format ipython-completion-command-string pattern))
351 (format ipython-completion-command-string pattern))
345 (accept-process-output python-process)
352 (accept-process-output python-process)
346 ;(message (format "DEBUG return: %s" ugly-return))
353 ;(message (format "DEBUG return: %s" ugly-return))
347 (setq completions
354 (setq completions
348 (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))
355 (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))
349 (setq completion-table (loop for str in completions
356 (setq completion-table (loop for str in completions
350 collect (list str nil)))
357 collect (list str nil)))
351 (setq completion (try-completion pattern completion-table))
358 (setq completion (try-completion pattern completion-table))
352 (cond ((eq completion t))
359 (cond ((eq completion t))
353 ((null completion)
360 ((null completion)
354 (message "Can't find completion for \"%s\"" pattern)
361 (message "Can't find completion for \"%s\"" pattern)
355 (ding))
362 (ding))
356 ((not (string= pattern completion))
363 ((not (string= pattern completion))
357 (delete-region beg end)
364 (delete-region beg end)
358 (insert completion))
365 (insert completion))
359 (t
366 (t
360 (message "Making completion list...")
367 (message "Making completion list...")
361 (with-output-to-temp-buffer "*Python Completions*"
368 (with-output-to-temp-buffer "*Python Completions*"
362 (display-completion-list (all-completions pattern completion-table)))
369 (display-completion-list (all-completions pattern completion-table)))
363 (message "Making completion list...%s" "done")))))
370 (message "Making completion list...%s" "done")))))
364 ;; emacs
371 ;; emacs
365 (defun ipython-complete ()
372 (defun ipython-complete ()
366 "Try to complete the python symbol before point. Only knows about the stuff
373 "Try to complete the python symbol before point. Only knows about the stuff
367 in the current *Python* session."
374 in the current *Python* session."
368 (interactive)
375 (interactive)
369 (let* ((ugly-return nil)
376 (let* ((ugly-return nil)
370 (sep ";")
377 (sep ";")
371 (python-process (or (get-buffer-process (current-buffer))
378 (python-process (or (get-buffer-process (current-buffer))
372 ;XXX hack for .py buffers
379 ;XXX hack for .py buffers
373 (get-process py-which-bufname)))
380 (get-process py-which-bufname)))
374 ;; XXX currently we go backwards to find the beginning of an
381 ;; XXX currently we go backwards to find the beginning of an
375 ;; expression part; a more powerful approach in the future might be
382 ;; expression part; a more powerful approach in the future might be
376 ;; to let ipython have the complete line, so that context can be used
383 ;; to let ipython have the complete line, so that context can be used
377 ;; to do things like filename completion etc.
384 ;; to do things like filename completion etc.
378 (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_./" (point-at-bol))
385 (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_./" (point-at-bol))
379 (point)))
386 (point)))
380 (end (point))
387 (end (point))
381 (pattern (buffer-substring-no-properties beg end))
388 (pattern (buffer-substring-no-properties beg end))
382 (completions nil)
389 (completions nil)
383 (completion-table nil)
390 (completion-table nil)
384 completion
391 completion
385 (comint-preoutput-filter-functions
392 (comint-preoutput-filter-functions
386 (append comint-preoutput-filter-functions
393 (append comint-preoutput-filter-functions
387 '(ansi-color-filter-apply
394 '(ansi-color-filter-apply
388 (lambda (string)
395 (lambda (string)
389 (setq ugly-return (concat ugly-return string))
396 (setq ugly-return (concat ugly-return string))
390 "")))))
397 "")))))
391 (process-send-string python-process
398 (process-send-string python-process
392 (format ipython-completion-command-string pattern))
399 (format ipython-completion-command-string pattern))
393 (accept-process-output python-process)
400 (accept-process-output python-process)
394 (setq completions
401 (setq completions
395 (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))
402 (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))
396 ;(message (format "DEBUG completions: %S" completions))
403 ;(message (format "DEBUG completions: %S" completions))
397 (setq completion-table (loop for str in completions
404 (setq completion-table (loop for str in completions
398 collect (list str nil)))
405 collect (list str nil)))
399 (setq completion (try-completion pattern completion-table))
406 (setq completion (try-completion pattern completion-table))
400 (cond ((eq completion t))
407 (cond ((eq completion t))
401 ((null completion)
408 ((null completion)
402 (message "Can't find completion for \"%s\"" pattern)
409 (message "Can't find completion for \"%s\"" pattern)
403 (ding))
410 (ding))
404 ((not (string= pattern completion))
411 ((not (string= pattern completion))
405 (delete-region beg end)
412 (delete-region beg end)
406 (insert completion))
413 (insert completion))
407 (t
414 (t
408 (message "Making completion list...")
415 (message "Making completion list...")
409 (with-output-to-temp-buffer "*IPython Completions*"
416 (with-output-to-temp-buffer "*IPython Completions*"
410 (display-completion-list (all-completions pattern completion-table)))
417 (display-completion-list (all-completions pattern completion-table)))
411 (message "Making completion list...%s" "done")))))
418 (message "Making completion list...%s" "done")))))
412 )
419 )
413
420
414 ;;; autoindent support: patch sent in by Jin Liu <m.liu.jin@gmail.com>,
421 ;;; autoindent support: patch sent in by Jin Liu <m.liu.jin@gmail.com>,
415 ;;; originally written by doxgen@newsmth.net
422 ;;; originally written by doxgen@newsmth.net
416 ;;; Minor modifications by fperez for xemacs compatibility.
423 ;;; Minor modifications by fperez for xemacs compatibility.
417
424
418 (defvar ipython-autoindent t
425 (defvar ipython-autoindent t
419 "If non-nil, enable autoindent for IPython shell through python-mode.")
426 "If non-nil, enable autoindent for IPython shell through python-mode.")
420
427
421 (defvar ipython-indenting-buffer-name "*IPython Indentation Calculation*"
428 (defvar ipython-indenting-buffer-name "*IPython Indentation Calculation*"
422 "Temporary buffer for indenting multiline statement.")
429 "Temporary buffer for indenting multiline statement.")
423
430
424 (defun ipython-get-indenting-buffer ()
431 (defun ipython-get-indenting-buffer ()
425 "Return a temporary buffer set in python-mode. Create one if necessary."
432 "Return a temporary buffer set in python-mode. Create one if necessary."
426 (let ((buf (get-buffer-create ipython-indenting-buffer-name)))
433 (let ((buf (get-buffer-create ipython-indenting-buffer-name)))
427 (set-buffer buf)
434 (set-buffer buf)
428 (unless (eq major-mode 'python-mode)
435 (unless (eq major-mode 'python-mode)
429 (python-mode))
436 (python-mode))
430 buf))
437 buf))
431
438
432 (defvar ipython-indentation-string nil
439 (defvar ipython-indentation-string nil
433 "Indentation for the next line in a multiline statement.")
440 "Indentation for the next line in a multiline statement.")
434
441
435 (defun ipython-send-and-indent ()
442 (defun ipython-send-and-indent ()
436 "Send the current line to IPython, and calculate the indentation for
443 "Send the current line to IPython, and calculate the indentation for
437 the next line."
444 the next line."
438 (interactive)
445 (interactive)
439 (if ipython-autoindent
446 (if ipython-autoindent
440 (let ((line (buffer-substring (point-at-bol) (point)))
447 (let ((line (buffer-substring (point-at-bol) (point)))
441 (after-prompt1)
448 (after-prompt1)
442 (after-prompt2))
449 (after-prompt2))
443 (save-excursion
450 (save-excursion
444 (comint-bol t)
451 (comint-bol t)
445 (if (looking-at py-shell-input-prompt-1-regexp)
452 (if (looking-at py-shell-input-prompt-1-regexp)
446 (setq after-prompt1 t)
453 (setq after-prompt1 t)
447 (setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp)))
454 (setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp)))
448 (with-current-buffer (ipython-get-indenting-buffer)
455 (with-current-buffer (ipython-get-indenting-buffer)
449 (when after-prompt1
456 (when after-prompt1
450 (erase-buffer))
457 (erase-buffer))
451 (when (or after-prompt1 after-prompt2)
458 (when (or after-prompt1 after-prompt2)
452 (delete-region (point-at-bol) (point))
459 (delete-region (point-at-bol) (point))
453 (insert line)
460 (insert line)
454 (newline-and-indent))))))
461 (newline-and-indent))))))
455 ;; send input line to ipython interpreter
462 ;; send input line to ipython interpreter
456 (comint-send-input))
463 (comint-send-input))
457
464
458 (defun ipython-indentation-hook (string)
465 (defun ipython-indentation-hook (string)
459 "Insert indentation string if py-shell-input-prompt-2-regexp
466 "Insert indentation string if py-shell-input-prompt-2-regexp
460 matches last process output."
467 matches last process output."
461 (let* ((start-marker (or comint-last-output-start
468 (let* ((start-marker (or comint-last-output-start
462 (point-min-marker)))
469 (point-min-marker)))
463 (end-marker (process-mark (get-buffer-process (current-buffer))))
470 (end-marker (process-mark (get-buffer-process (current-buffer))))
464 (text (ansi-color-filter-apply (buffer-substring start-marker end-marker))))
471 (text (ansi-color-filter-apply (buffer-substring start-marker end-marker))))
465 ;; XXX if `text' matches both pattern, it MUST be the last prompt-2
472 ;; XXX if `text' matches both pattern, it MUST be the last prompt-2
466 (when (and (string-match py-shell-input-prompt-2-regexp text)
473 (when (and (string-match py-shell-input-prompt-2-regexp text)
467 (not (string-match "\n$" text)))
474 (not (string-match "\n$" text)))
468 (with-current-buffer (ipython-get-indenting-buffer)
475 (with-current-buffer (ipython-get-indenting-buffer)
469 (setq ipython-indentation-string
476 (setq ipython-indentation-string
470 (buffer-substring (point-at-bol) (point))))
477 (buffer-substring (point-at-bol) (point))))
471 (goto-char end-marker)
478 (goto-char end-marker)
472 (insert ipython-indentation-string)
479 (insert ipython-indentation-string)
473 (setq ipython-indentation-string nil))))
480 (setq ipython-indentation-string nil))))
474
481
475 (add-hook 'py-shell-hook
482 (add-hook 'py-shell-hook
476 (lambda ()
483 (lambda ()
477 (add-hook 'comint-output-filter-functions
484 (add-hook 'comint-output-filter-functions
478 'ipython-indentation-hook)))
485 'ipython-indentation-hook)))
479
486
480 (define-key py-shell-map (kbd "RET") 'ipython-send-and-indent)
487 (define-key py-shell-map (kbd "RET") 'ipython-send-and-indent)
481 ;;; / end autoindent support
488 ;;; / end autoindent support
482
489
483 (provide 'ipython)
490 (provide 'ipython)
General Comments 0
You need to be logged in to leave comments. Login now