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