##// END OF EJS Templates
Add support for autoindent inside (X)emacs.
fperez -
Show More
@@ -1,7 +1,11 b''
1 1 2006-05-24 Fernando Perez <Fernando.Perez@colorado.edu>
2 2
3 3 * ipython.el: fix the py-pdbtrack-input-prompt variable so that
4 python-mode recognizes our debugger mode.
4 python-mode recognizes our debugger mode. Add support for
5 autoindent inside (X)emacs. After a patch sent in by Jin Liu
6 <m.liu.jin-AT-gmail.com> originally written by
7 doxgen-AT-newsmth.net (with minor modifications for xemacs
8 compatibility)
5 9
6 10 * IPython/Debugger.py (Pdb.format_stack_entry): fix formatting of
7 11 tracebacks when walking the stack so that the stack tracking system
@@ -6,7 +6,7 b''
6 6 ;; URL: http://ipython.scipy.org
7 7 ;; Compatibility: Emacs21, XEmacs21
8 8 ;; FIXME: #$@! INPUT RING
9 (defconst ipython-version "$Revision: 1324 $"
9 (defconst ipython-version "$Revision: 1325 $"
10 10 "VC version number.")
11 11
12 12 ;;; Commentary
@@ -405,4 +405,81 b' in the current *Python* session."'
405 405 (message "Making completion list...%s" "done")))))
406 406 )
407 407
408 ;;; autoindent support: patch sent in by Jin Liu <m.liu.jin@gmail.com>,
409 ;;; originally written by doxgen@newsmth.net
410 ;;; Minor modifications by fperez for xemacs compatibility.
411
412 (defvar ipython-autoindent t
413 "If non-nil, enable autoindent for IPython shell through python-mode.")
414
415 (defvar ipython-indenting-buffer-name "*IPython Indentation Calculation*"
416 "Temporary buffer for indenting multiline statement.")
417
418 (defun ipython-get-indenting-buffer ()
419 "Return a temporary buffer set in python-mode. Create one if necessary."
420 (let ((buf (get-buffer-create ipython-indenting-buffer-name)))
421 (set-buffer buf)
422 (unless (eq major-mode 'python-mode)
423 (python-mode))
424 buf))
425
426 (defvar ipython-indentation-string nil
427 "Indentation for the next line in a multiline statement.")
428
429 (defun ipython-send-and-indent ()
430 "Send the current line to IPython, and calculate the indentation for
431 the next line."
432 (interactive)
433 (if ipython-autoindent
434 (let ((line (buffer-substring (point-at-bol) (point)))
435 (after-prompt1)
436 (after-prompt2))
437 (save-excursion
438 (comint-bol t)
439 (if (looking-at py-shell-input-prompt-1-regexp)
440 (setq after-prompt1 t)
441 (setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp)))
442 (with-current-buffer (ipython-get-indenting-buffer)
443 (when after-prompt1
444 (erase-buffer))
445 (when (or after-prompt1 after-prompt2)
446 (delete-region (point-at-bol) (point))
447 (insert line)
448 (newline-and-indent))))))
449 ;; send input line to ipython interpreter
450 (comint-send-input))
451
452 (defun ipython-indentation-hook (string)
453 "Insert indentation string if py-shell-input-prompt-2-regexp
454 matches last process output."
455 (let* ((start-marker (or comint-last-output-start
456 (point-min-marker)))
457 (end-marker (process-mark (get-buffer-process (current-buffer))))
458 (text (ansi-color-filter-apply (buffer-substring start-marker end-marker))))
459 (progn
460 ;; XXX if `text' matches both pattern, it MUST be the last prompt-2
461 (cond ((and (string-match "\n$" text) (string-match
462 py-shell-input-prompt-2-regexp text))
463 (with-current-buffer (ipython-get-indenting-buffer)
464 (erase-buffer)))
465 ;; still a prompt-2
466 ((string-match py-shell-input-prompt-2-regexp text)
467 (with-current-buffer (ipython-get-indenting-buffer)
468 (setq ipython-indentation-string
469 (buffer-substring (point-at-bol) (point))))
470 (unless (eq ipython-indentation-string nil)
471 (message "ipython-indentation-hook: %s#%s##" text
472 ipython-indentation-string))
473 (goto-char end-marker)
474 (insert ipython-indentation-string)))
475 (setq ipython-indentation-string nil))))
476
477 (add-hook 'py-shell-hook
478 (lambda ()
479 (add-hook 'comint-output-filter-functions
480 'ipython-indentation-hook)))
481
482 (define-key py-shell-map (kbd "RET") 'ipython-send-and-indent)
483 ;;; / end autoindent support
484
408 485 (provide 'ipython)
General Comments 0
You need to be logged in to leave comments. Login now