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