From d6801b6ed4df5a77022ec655b7996035095d4497 2006-05-24 20:59:18 From: fperez Date: 2006-05-24 20:59:18 Subject: [PATCH] Add support for autoindent inside (X)emacs. --- diff --git a/doc/ChangeLog b/doc/ChangeLog index 17c1c6b..8c9ccad 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,7 +1,11 @@ 2006-05-24 Fernando Perez * ipython.el: fix the py-pdbtrack-input-prompt variable so that - python-mode recognizes our debugger mode. + python-mode recognizes our debugger mode. Add support for + autoindent inside (X)emacs. After a patch sent in by Jin Liu + originally written by + doxgen-AT-newsmth.net (with minor modifications for xemacs + compatibility) * IPython/Debugger.py (Pdb.format_stack_entry): fix formatting of tracebacks when walking the stack so that the stack tracking system diff --git a/doc/ipython.el b/doc/ipython.el index 9eb3c22..fbc9ce9 100644 --- a/doc/ipython.el +++ b/doc/ipython.el @@ -6,7 +6,7 @@ ;; URL: http://ipython.scipy.org ;; Compatibility: Emacs21, XEmacs21 ;; FIXME: #$@! INPUT RING -(defconst ipython-version "$Revision: 1324 $" +(defconst ipython-version "$Revision: 1325 $" "VC version number.") ;;; Commentary @@ -405,4 +405,81 @@ in the current *Python* session." (message "Making completion list...%s" "done"))))) ) +;;; autoindent support: patch sent in by Jin Liu , +;;; originally written by doxgen@newsmth.net +;;; Minor modifications by fperez for xemacs compatibility. + +(defvar ipython-autoindent t + "If non-nil, enable autoindent for IPython shell through python-mode.") + +(defvar ipython-indenting-buffer-name "*IPython Indentation Calculation*" + "Temporary buffer for indenting multiline statement.") + +(defun ipython-get-indenting-buffer () + "Return a temporary buffer set in python-mode. Create one if necessary." + (let ((buf (get-buffer-create ipython-indenting-buffer-name))) + (set-buffer buf) + (unless (eq major-mode 'python-mode) + (python-mode)) + buf)) + +(defvar ipython-indentation-string nil + "Indentation for the next line in a multiline statement.") + +(defun ipython-send-and-indent () + "Send the current line to IPython, and calculate the indentation for +the next line." + (interactive) + (if ipython-autoindent + (let ((line (buffer-substring (point-at-bol) (point))) + (after-prompt1) + (after-prompt2)) + (save-excursion + (comint-bol t) + (if (looking-at py-shell-input-prompt-1-regexp) + (setq after-prompt1 t) + (setq after-prompt2 (looking-at py-shell-input-prompt-2-regexp))) + (with-current-buffer (ipython-get-indenting-buffer) + (when after-prompt1 + (erase-buffer)) + (when (or after-prompt1 after-prompt2) + (delete-region (point-at-bol) (point)) + (insert line) + (newline-and-indent)))))) + ;; send input line to ipython interpreter + (comint-send-input)) + +(defun ipython-indentation-hook (string) + "Insert indentation string if py-shell-input-prompt-2-regexp +matches last process output." + (let* ((start-marker (or comint-last-output-start + (point-min-marker))) + (end-marker (process-mark (get-buffer-process (current-buffer)))) + (text (ansi-color-filter-apply (buffer-substring start-marker end-marker)))) + (progn + ;; XXX if `text' matches both pattern, it MUST be the last prompt-2 + (cond ((and (string-match "\n$" text) (string-match +py-shell-input-prompt-2-regexp text)) + (with-current-buffer (ipython-get-indenting-buffer) + (erase-buffer))) + ;; still a prompt-2 + ((string-match py-shell-input-prompt-2-regexp text) + (with-current-buffer (ipython-get-indenting-buffer) + (setq ipython-indentation-string + (buffer-substring (point-at-bol) (point)))) + (unless (eq ipython-indentation-string nil) + (message "ipython-indentation-hook: %s#%s##" text +ipython-indentation-string)) + (goto-char end-marker) + (insert ipython-indentation-string))) + (setq ipython-indentation-string nil)))) + +(add-hook 'py-shell-hook + (lambda () + (add-hook 'comint-output-filter-functions + 'ipython-indentation-hook))) + +(define-key py-shell-map (kbd "RET") 'ipython-send-and-indent) +;;; / end autoindent support + (provide 'ipython)