Commit deaa1054 authored by Guido van Rossum's avatar Guido van Rossum

Lots of formatting changes by Barry, and a few real changes.

parent cfec3590
;;; Major mode for editing Python programs, version 1.10 ;;; python-mode.el --- Major mode for editing Python programs
;; by: Tim Peters <tim@ksr.com>
;; after an original idea by: Michael A. Guravage ;; Copyright (C) 1992,1993,1994 Tim Peters
;; Author: 1995 Barry A. Warsaw <bwarsaw@cnri.reston.va.us>
;; 1992-1994 Tim Peters <tim@ksr.com>
;; Maintainer: bwarsaw@cnri.reston.va.us
;; Created: ???
;; Version: 2.6
;; Last Modified: 1995/03/09 16:07:29
;; Keywords: python editing language major-mode
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; ;;
;; This is a major mode for editing Python programs. It was developed
;; by Tim Peters <tim@ksr.com> after an original idea by Michael
;; A. Guravage. Tim doesn't appear to be on the 'net any longer so I
;; have undertaken maintenance of the mode. Here is Tim's original
;; copyright notice:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Copyright (c) 1992,1993,1994 Tim Peters ;; Copyright (c) 1992,1993,1994 Tim Peters
;; ;;
;; This software is provided as-is, without express or implied warranty. ;; This software is provided as-is, without express or implied warranty.
...@@ -9,39 +42,54 @@ ...@@ -9,39 +42,54 @@
;; without fee, for any purpose and by any individual or organization, is ;; without fee, for any purpose and by any individual or organization, is
;; hereby granted, provided that the above copyright notice and this ;; hereby granted, provided that the above copyright notice and this
;; paragraph appear in all copies. ;; paragraph appear in all copies.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; At some point this mode will undergo a rewrite to bring it more in
;; line with GNU Emacs Lisp coding standards. But all in all, the
;; mode works exceedingly well.
;; The following statements, placed in your .emacs file or
;; site-init.el, will cause this file to be autoloaded, and
;; python-mode invoked, when visiting .py files (assuming this file is
;; in your load-path):
;; ;;
;; ;; (autoload 'python-mode "python-mode" "Python editing mode." t)
;; The following statements, placed in your .emacs file or site-init.el,
;; will cause this file to be autoloaded, and python-mode invoked, when
;; visiting .py files (assuming the file is in your load-path):
;;
;; (autoload 'python-mode "python-mode" "" t)
;; (setq auto-mode-alist ;; (setq auto-mode-alist
;; (cons '("\\.py$" . python-mode) auto-mode-alist)) ;; (cons '("\\.py$" . python-mode) auto-mode-alist))
(provide 'python-mode) ;; Here's a brief to do list:
;;
;; 1. Better integration with gud-mode for debugging.
;; 2. Rewrite according to GNU Emacs Lisp standards.
;;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. ;; If you can think of more things you'd like to see, drop me a line.
;;; This seems to be the standard way of checking this. ;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
;;
;; Note that I only test things on XEmacs (currently 19.11). If you
;; port stuff to FSF Emacs 19, or Emacs 18, please send me your
;; patches.
(setq py-this-is-lucid-emacs-p (string-match "Lucid" emacs-version)) ;; LCD Archive Entry:
(setq py-this-is-emacs-19-p ;; python-mode|Barry A. Warsaw|bwarsaw@cnri.reston.va.us
(and ;; |Major mode for editing Python programs
(not py-this-is-lucid-emacs-p) ;; |1995/03/09 16:07:29|2.6|
(string-match "^19\\." emacs-version)))
;;; Code:
;;; Constants and variables
;; user definable variables
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
(defvar py-python-command "python" (defvar py-python-command "python"
"*Shell command used to start Python interpreter.") "*Shell command used to start Python interpreter.")
(defvar py-indent-offset 8 ; argue with Guido <grin> (defvar py-indent-offset 8 ; argue with Guido <grin>
"*Indentation increment. "*Indentation increment.
Note that `\\[py-guess-indent-offset]' can usually guess a good value when you're Note that `\\[py-guess-indent-offset]' can usually guess a good value
editing someone else's Python code.") when you're editing someone else's Python code.")
(defvar py-block-comment-prefix "##" (defvar py-block-comment-prefix "##"
"*String used by py-comment-region to comment out a block of code. "*String used by `py-comment-region' to comment out a block of code.
This should follow the convention for non-indenting comment lines so This should follow the convention for non-indenting comment lines so
that the indentation commands won't get confused (i.e., the string that the indentation commands won't get confused (i.e., the string
should be of the form `#x...' where `x' is not a blank or a tab, and should be of the form `#x...' where `x' is not a blank or a tab, and
...@@ -88,7 +136,7 @@ process mark', but if you know what that means you didn't need to be ...@@ -88,7 +136,7 @@ process mark', but if you know what that means you didn't need to be
told <grin>.") told <grin>.")
(defvar py-temp-directory (defvar py-temp-directory
(let ( (ok '(lambda (x) (let ((ok '(lambda (x)
(and x (and x
(setq x (expand-file-name x)) ; always true (setq x (expand-file-name x)) ; always true
(file-directory-p x) (file-directory-p x)
...@@ -105,6 +153,58 @@ By default, the first directory from this list that exists and that you ...@@ -105,6 +153,58 @@ By default, the first directory from this list that exists and that you
can write into: the value (if any) of the environment variable TMPDIR, can write into: the value (if any) of the environment variable TMPDIR,
/usr/tmp, /tmp, or the current directory.") /usr/tmp, /tmp, or the current directory.")
(defvar py-beep-if-tab-change t
"*Ring the bell if tab-width is changed.
If a comment of the form
\t# vi:set tabsize=<number>:
is found before the first code line when the file is entered, and the
current value of (the general Emacs variable) `tab-width' does not
equal <number>, `tab-width' is set to <number>, a message saying so is
displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
the Emacs bell is also rung as a warning.")
(defvar python-font-lock-keywords
(list
(cons
(concat
"\\<\\("
(mapconcat
'identity
'("access" "and" "break" "continue"
"del" "elif" "else" "except"
"exec" "finally" "for" "from"
"global" "if" "import" "in"
"is" "lambda" "not" "or"
"pass" "print" "raise" "return"
"try" "while" "def" "class"
)
"\\|")
"\\)\\>")
1)
;; functions
'("\\bdef\\s +\\(\\sw+\\)(" 1 font-lock-function-name-face)
;; classes
'("\\bclass\\s +\\(\\sw+\\)[(:]" 1 font-lock-function-name-face)
)
"*Additional keywords to highlight `python-mode' buffers.")
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
;; seems to be the standard way of checking this.
;; BAW - This is *not* the right solution. When at all possible,
;; instead of testing for the version of Emacs, use feature tests.
(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
(setq py-this-is-emacs-19-p
(and
(not py-this-is-lucid-emacs-p)
(string-match "^19\\." emacs-version)))
;; have to bind py-file-queue before installing the kill-emacs hook ;; have to bind py-file-queue before installing the kill-emacs hook
(defvar py-file-queue nil (defvar py-file-queue nil
"Queue of Python temp files awaiting execution. "Queue of Python temp files awaiting execution.
...@@ -112,53 +212,38 @@ Currently-active file is at the head of the list.") ...@@ -112,53 +212,38 @@ Currently-active file is at the head of the list.")
;; define a mode-specific abbrev table for those who use such things ;; define a mode-specific abbrev table for those who use such things
(defvar python-mode-abbrev-table nil (defvar python-mode-abbrev-table nil
"Abbrev table in use in python-mode buffers.") "Abbrev table in use in `python-mode' buffers.")
(define-abbrev-table 'python-mode-abbrev-table nil) (define-abbrev-table 'python-mode-abbrev-table nil)
;; arrange to kill temp files no matter what
(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
;; have to trust that other people are as respectful of our hook
;; fiddling as we are of theirs
(if (boundp 'py-inherited-kill-emacs-hook)
;; we were loaded before -- trust others not to have screwed us
;; in the meantime (no choice, really)
nil
;; else arrange for our hook to run theirs
(setq py-inherited-kill-emacs-hook kill-emacs-hook)
(setq kill-emacs-hook 'py-kill-emacs-hook)))
(defvar python-mode-hook nil (defvar python-mode-hook nil
"*Hook called by `python-mode'.") "*Hook called by `python-mode'.")
;; in previous version of python-mode.el, the hook was incorrectly
;; called py-mode-hook, and was not defvar'd. deprecate its use.
(and (fboundp 'make-obsolete-variable) (and (fboundp 'make-obsolete-variable)
(make-obsolete-variable 'py-mode-hook 'python-mode-hook)) (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
(defvar py-beep-if-tab-change t (defvar py-mode-map ()
"*Ring the bell if tab-width is changed. "Keymap used in `python-mode' buffers.")
If a comment of the form
\t# vi:set tabsize=<number>:
is found before the first code line when the file is entered, and
the current value of (the general Emacs variable) tab-width does not
equal <number>, tab-width is set to <number>, a message saying so is
displayed in the echo area, and if py-beep-if-tab-change is non-nil the
Emacs bell is also rung as a warning.")
(defvar py-mode-map nil "Keymap used in Python mode buffers.")
(if py-mode-map (if py-mode-map
() ()
(setq py-mode-map (make-sparse-keymap)) (setq py-mode-map (make-sparse-keymap))
;; shadow global bindings for newline-and-indent w/ the py- version ;; shadow global bindings for newline-and-indent w/ the py- version.
;; BAW - this is extremely bad form, but I'm not going to change it
;; for now.
(mapcar (function (lambda (key) (mapcar (function (lambda (key)
(define-key (define-key
py-mode-map key 'py-newline-and-indent))) py-mode-map key 'py-newline-and-indent)))
(where-is-internal 'newline-and-indent)) (where-is-internal 'newline-and-indent))
;; BAW - you could do it this way, but its not considered proper
;; major-mode form.
(mapcar (function (mapcar (function
(lambda (x) (lambda (x)
(define-key py-mode-map (car x) (cdr x)))) (define-key py-mode-map (car x) (cdr x))))
'( ("\C-c\C-c" . py-execute-buffer) '(("\C-c\C-c" . py-execute-buffer)
("\C-c|" . py-execute-region) ("\C-c|" . py-execute-region)
("\C-c!" . py-shell) ("\C-c!" . py-shell)
("\177" . py-delete-char) ("\177" . py-delete-char)
...@@ -170,18 +255,25 @@ Emacs bell is also rung as a warning.") ...@@ -170,18 +255,25 @@ Emacs bell is also rung as a warning.")
("\C-c\C-n" . py-next-statement) ("\C-c\C-n" . py-next-statement)
("\C-c\C-p" . py-previous-statement) ("\C-c\C-p" . py-previous-statement)
("\C-c\C-u" . py-goto-block-up) ("\C-c\C-u" . py-goto-block-up)
("\C-c\C-b" . py-mark-block) ("\C-c\C-m" . py-mark-block)
("\C-c#" . py-comment-region) ("\C-c#" . py-comment-region)
("\C-c?" . py-describe-mode) ("\C-c?" . py-describe-mode)
("\C-c\C-hm" . py-describe-mode) ("\C-c\C-hm" . py-describe-mode)
("\e\C-a" . beginning-of-python-def-or-class) ("\e\C-a" . beginning-of-python-def-or-class)
("\e\C-e" . end-of-python-def-or-class) ("\e\C-e" . end-of-python-def-or-class)
( "\e\C-h" . mark-python-def-or-class)))) ( "\e\C-h" . mark-python-def-or-class)))
;; should do all keybindings this way
(define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
(define-key py-mode-map "\C-c\C-v" 'py-version)
)
(defvar py-mode-syntax-table nil
"Syntax table used in `python-mode' buffers.")
(defvar py-mode-syntax-table nil "Python mode syntax table")
(if py-mode-syntax-table (if py-mode-syntax-table
() ()
(setq py-mode-syntax-table (make-syntax-table)) (setq py-mode-syntax-table (make-syntax-table))
;; BAW - again, blech.
(mapcar (function (mapcar (function
(lambda (x) (modify-syntax-entry (lambda (x) (modify-syntax-entry
(car x) (cdr x) py-mode-syntax-table))) (car x) (cdr x) py-mode-syntax-table)))
...@@ -205,7 +297,7 @@ Emacs bell is also rung as a warning.") ...@@ -205,7 +297,7 @@ Emacs bell is also rung as a warning.")
"'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
"\\|" ; or "\\|" ; or
"\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
"regexp matching a Python string literal") "Regexp matching a Python string literal.")
;; this is tricky because a trailing backslash does not mean ;; this is tricky because a trailing backslash does not mean
;; continuation if it's in a comment ;; continuation if it's in a comment
...@@ -213,18 +305,23 @@ Emacs bell is also rung as a warning.") ...@@ -213,18 +305,23 @@ Emacs bell is also rung as a warning.")
(concat (concat
"\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*" "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
"\\\\$") "\\\\$")
"regexp matching Python lines that are continued via backslash") "Regexp matching Python lines that are continued via backslash.")
(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)" (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
"regexp matching blank or comment lines") "Regexp matching blank or comment lines.")
;;; General Functions
;;;###autoload
(defun python-mode () (defun python-mode ()
"Major mode for editing Python files. "Major mode for editing Python files.
Do `\\[py-describe-mode]' for detailed documentation. To submit a problem report, enter `\\[py-submit-bug-report]' from a
Knows about Python indentation, tokens, comments and continuation lines. `python-mode' buffer. Do `\\[py-describe-mode]' for detailed
Paragraphs are separated by blank lines only. documentation. To see what version of `python-mode' you are running,
enter `\\[py-version]'.
This mode knows about Python indentation, tokens, comments and
continuation lines. Paragraphs are separated by blank lines only.
COMMANDS COMMANDS
\\{py-mode-map} \\{py-mode-map}
...@@ -238,16 +335,16 @@ py-temp-directory\tdirectory used for temp files (if needed) ...@@ -238,16 +335,16 @@ py-temp-directory\tdirectory used for temp files (if needed)
py-beep-if-tab-change\tring the bell if tab-width is changed" py-beep-if-tab-change\tring the bell if tab-width is changed"
(interactive) (interactive)
(kill-all-local-variables) (kill-all-local-variables)
(set-syntax-table py-mode-syntax-table)
(setq major-mode 'python-mode (setq major-mode 'python-mode
mode-name "Python" mode-name "Python"
local-abbrev-table python-mode-abbrev-table) local-abbrev-table python-mode-abbrev-table)
(use-local-map py-mode-map) (use-local-map py-mode-map)
(set-syntax-table py-mode-syntax-table) ;; BAW -- style...
(mapcar (function (lambda (x) (mapcar (function (lambda (x)
(make-local-variable (car x)) (make-local-variable (car x))
(set (car x) (cdr x)))) (set (car x) (cdr x))))
'( (paragraph-separate . "^[ \t]*$") '((paragraph-separate . "^[ \t]*$")
(paragraph-start . "^[ \t]*$") (paragraph-start . "^[ \t]*$")
(require-final-newline . t) (require-final-newline . t)
(comment-start . "# ") (comment-start . "# ")
...@@ -255,13 +352,15 @@ py-beep-if-tab-change\tring the bell if tab-width is changed" ...@@ -255,13 +352,15 @@ py-beep-if-tab-change\tring the bell if tab-width is changed"
(comment-column . 40) (comment-column . 40)
(indent-region-function . py-indent-region) (indent-region-function . py-indent-region)
(indent-line-function . py-indent-line))) (indent-line-function . py-indent-line)))
;; hack to allow overriding the tabsize in the file (see tokenizer.c) ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
;;
;; not sure where the magic comment has to be; to save time searching ;; not sure where the magic comment has to be; to save time
;; for a rarity, we give up if it's not found prior to the first ;; searching for a rarity, we give up if it's not found prior to the
;; executable statement ;; first executable statement.
(let ( (case-fold-search nil) ;;
;; BAW - on first glance, this seems like complete hackery. Why was
;; this necessary, and is it still necessary?
(let ((case-fold-search nil)
(start (point)) (start (point))
new-tab-width) new-tab-width)
(if (re-search-forward (if (re-search-forward
...@@ -279,12 +378,13 @@ py-beep-if-tab-change\tring the bell if tab-width is changed" ...@@ -279,12 +378,13 @@ py-beep-if-tab-change\tring the bell if tab-width is changed"
(if py-beep-if-tab-change (beep))))) (if py-beep-if-tab-change (beep)))))
(goto-char start)) (goto-char start))
;; run the mode hook. py-mode-hook use is deprecated
(if python-mode-hook (if python-mode-hook
(run-hooks 'python-mode-hook) (run-hooks 'python-mode-hook)
(run-hooks 'py-mode-hook))) (run-hooks 'py-mode-hook)))
;;; Functions that execute Python commands in a subprocess ;;; Functions that execute Python commands in a subprocess
(defun py-shell () (defun py-shell ()
"Start an interactive Python interpreter in another window. "Start an interactive Python interpreter in another window.
This is like Shell mode, except that Python is running in the window This is like Shell mode, except that Python is running in the window
...@@ -292,16 +392,17 @@ instead of a shell. See the `Interactive Shell' and `Shell Mode' ...@@ -292,16 +392,17 @@ instead of a shell. See the `Interactive Shell' and `Shell Mode'
sections of the Emacs manual for details, especially for the key sections of the Emacs manual for details, especially for the key
bindings active in the `*Python*' buffer. bindings active in the `*Python*' buffer.
See the docs for variable py-scroll-buffer for info on scrolling See the docs for variable `py-scroll-buffer' for info on scrolling
behavior in the process window. behavior in the process window.
Warning: Don't use an interactive Python if you change sys.ps1 or Warning: Don't use an interactive Python if you change sys.ps1 or
sys.ps2 from their default values, or if you're running code that prints sys.ps2 from their default values, or if you're running code that
`>>> ' or `... ' at the start of a line. Python mode can't distinguish prints `>>> ' or `... ' at the start of a line. `python-mode' can't
your output from Python's output, and assumes that `>>> ' at the start distinguish your output from Python's output, and assumes that `>>> '
of a line is a prompt from Python. Similarly, the Emacs Shell mode code at the start of a line is a prompt from Python. Similarly, the Emacs
assumes that both `>>> ' and `... ' at the start of a line are Python Shell mode code assumes that both `>>> ' and `... ' at the start of a
prompts. Bad things can happen if you fool either mode. line are Python prompts. Bad things can happen if you fool either
mode.
Warning: If you do any editing *in* the process buffer *while* the Warning: If you do any editing *in* the process buffer *while* the
buffer is accepting output from Python, do NOT attempt to `undo' the buffer is accepting output from Python, do NOT attempt to `undo' the
...@@ -310,6 +411,8 @@ be lost if you do. This appears to be an Emacs bug, an unfortunate ...@@ -310,6 +411,8 @@ be lost if you do. This appears to be an Emacs bug, an unfortunate
interaction between undo and process filters; the same problem exists in interaction between undo and process filters; the same problem exists in
non-Python process buffers using the default (Emacs-supplied) process non-Python process buffers using the default (Emacs-supplied) process
filter." filter."
;; BAW - should undo be disabled in the python process buffer, if
;; this bug still exists?
(interactive) (interactive)
(if py-this-is-emacs-19-p (if py-this-is-emacs-19-p
(progn (progn
...@@ -330,36 +433,37 @@ filter." ...@@ -330,36 +433,37 @@ filter."
"Send the region between START and END to a Python interpreter. "Send the region between START and END to a Python interpreter.
If there is a *Python* process it is used. If there is a *Python* process it is used.
Hint: If you want to execute part of a Python file several times (e.g., Hint: If you want to execute part of a Python file several times
perhaps you're developing a function and want to flesh it out a bit at a \(e.g., perhaps you're developing a function and want to flesh it out
time), use `\\[narrow-to-region]' to restrict the buffer to the region of interest, a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
and send the code to a *Python* process via `\\[py-execute-buffer]' instead. the region of interest, and send the code to a *Python* process via
`\\[py-execute-buffer]' instead.
Following are subtleties to note when using a *Python* process: Following are subtleties to note when using a *Python* process:
If a *Python* process is used, the region is copied into a temp file (in If a *Python* process is used, the region is copied into a temporary
directory py-temp-directory), and an `execfile' command is sent to file (in directory `py-temp-directory'), and an `execfile' command is
Python naming that file. If you send regions faster than Python can sent to Python naming that file. If you send regions faster than
execute them, Python mode will save them into distinct temp files, and Python can execute them, `python-mode' will save them into distinct
execute the next one in the queue the next time it sees a `>>> ' prompt temp files, and execute the next one in the queue the next time it
from Python. Each time this happens, the process buffer is popped into sees a `>>> ' prompt from Python. Each time this happens, the process
a window (if it's not already in some window) so you can see it, and a buffer is popped into a window (if it's not already in some window) so
comment of the form you can see it, and a comment of the form
\t## working on region in file <name> ... \t## working on region in file <name> ...
is inserted at the end. is inserted at the end.
Caution: No more than 26 regions can be pending at any given time. This Caution: No more than 26 regions can be pending at any given time.
limit is (indirectly) inherited from libc's mktemp(3). Python mode does This limit is (indirectly) inherited from libc's mktemp(3).
not try to protect you from exceeding the limit. It's extremely `python-mode' does not try to protect you from exceeding the limit.
unlikely that you'll get anywhere close to the limit in practice, unless It's extremely unlikely that you'll get anywhere close to the limit in
you're trying to be a jerk <grin>. practice, unless you're trying to be a jerk <grin>.
See the `\\[py-shell]' docs for additional warnings." See the `\\[py-shell]' docs for additional warnings."
(interactive "r") (interactive "r")
(or (< start end) (error "Region is empty")) (or (< start end) (error "Region is empty"))
(let ( (pyproc (get-process "Python")) (let ((pyproc (get-process "Python"))
fname) fname)
(if (null pyproc) (if (null pyproc)
(shell-command-on-region start end py-python-command) (shell-command-on-region start end py-python-command)
...@@ -379,31 +483,34 @@ See the `\\[py-shell]' docs for additional warnings." ...@@ -379,31 +483,34 @@ See the `\\[py-shell]' docs for additional warnings."
(process-send-string pyproc (format "execfile('%s')\n" fname))) (process-send-string pyproc (format "execfile('%s')\n" fname)))
(defun py-process-filter (pyproc string) (defun py-process-filter (pyproc string)
(let ( (curbuf (current-buffer)) (let ((curbuf (current-buffer))
(pbuf (process-buffer pyproc)) (pbuf (process-buffer pyproc))
(pmark (process-mark pyproc)) (pmark (process-mark pyproc))
file-finished) file-finished)
;; make sure we switch to a different buffer at least once. if we ;; make sure we switch to a different buffer at least once. if we
;; *don't* do this, then if the process buffer is in the selected ;; *don't* do this, then if the process buffer is in the selected
;; window, and point is before the end, and lots of output is coming ;; window, and point is before the end, and lots of output is
;; at a fast pace, then (a) simple cursor-movement commands like ;; coming at a fast pace, then (a) simple cursor-movement commands
;; C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time to have ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
;; a visible effect (the window just doesn't get updated, sometimes ;; to have a visible effect (the window just doesn't get updated,
;; for minutes(!)), and (b) it takes about 5x longer to get all the ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
;; process output (until the next python prompt). ;; get all the process output (until the next python prompt).
;; ;;
;; #b makes no sense to me at all. #a almost makes sense: unless we ;; #b makes no sense to me at all. #a almost makes sense: unless
;; actually change buffers, set_buffer_internal in buffer.c doesn't ;; we actually change buffers, set_buffer_internal in buffer.c
;; set windows_or_buffers_changed to 1, & that in turn seems to make ;; doesn't set windows_or_buffers_changed to 1, & that in turn
;; the Emacs command loop reluctant to update the display. Perhaps ;; seems to make the Emacs command loop reluctant to update the
;; the default process filter in process.c's read_process_output has ;; display. Perhaps the default process filter in process.c's
;; update_mode_lines++ for a similar reason? beats me ... ;; read_process_output has update_mode_lines++ for a similar
;; reason? beats me ...
;; BAW - we want to check to see if this still applies
(if (eq curbuf pbuf) ; mysterious ugly hack (if (eq curbuf pbuf) ; mysterious ugly hack
(set-buffer (get-buffer-create "*scratch*"))) (set-buffer (get-buffer-create "*scratch*")))
(set-buffer pbuf) (set-buffer pbuf)
(let* ( (start (point)) (let* ((start (point))
(goback (< start pmark)) (goback (< start pmark))
(buffer-read-only nil)) (buffer-read-only nil))
(goto-char pmark) (goto-char pmark)
...@@ -419,7 +526,7 @@ See the `\\[py-shell]' docs for additional warnings." ...@@ -419,7 +526,7 @@ See the `\\[py-shell]' docs for additional warnings."
(if goback (goto-char start) (if goback (goto-char start)
;; else ;; else
(if py-scroll-process-buffer (if py-scroll-process-buffer
(let* ( (pop-up-windows t) (let* ((pop-up-windows t)
(pwin (display-buffer pbuf))) (pwin (display-buffer pbuf)))
(set-window-point pwin (point)))))) (set-window-point pwin (point))))))
(set-buffer curbuf) (set-buffer curbuf)
...@@ -441,8 +548,8 @@ See the `\\[py-execute-region]' docs for an account of some subtleties." ...@@ -441,8 +548,8 @@ See the `\\[py-execute-region]' docs for an account of some subtleties."
(py-execute-region (point-min) (point-max))) (py-execute-region (point-min) (point-max)))
;;; Functions for Python style indentation
;; Functions for Python style indentation
(defun py-delete-char () (defun py-delete-char ()
"Reduce indentation or delete character. "Reduce indentation or delete character.
If point is at the leftmost column, deletes the preceding newline. If point is at the leftmost column, deletes the preceding newline.
...@@ -451,8 +558,8 @@ Else if point is at the leftmost non-blank character of a line that is ...@@ -451,8 +558,8 @@ Else if point is at the leftmost non-blank character of a line that is
neither a continuation line nor a non-indenting comment line, or if neither a continuation line nor a non-indenting comment line, or if
point is at the end of a blank line, reduces the indentation to match point is at the end of a blank line, reduces the indentation to match
that of the line that opened the current block of code. The line that that of the line that opened the current block of code. The line that
opened the block is displayed in the echo area to help you keep track of opened the block is displayed in the echo area to help you keep track
where you are. of where you are.
Else the preceding character is deleted, converting a tab to spaces if Else the preceding character is deleted, converting a tab to spaces if
needed so that only a single column position is deleted." needed so that only a single column position is deleted."
...@@ -467,7 +574,7 @@ needed so that only a single column position is deleted." ...@@ -467,7 +574,7 @@ needed so that only a single column position is deleted."
;; force non-blank so py-goto-block-up doesn't ignore it ;; force non-blank so py-goto-block-up doesn't ignore it
(insert-char ?* 1) (insert-char ?* 1)
(backward-char) (backward-char)
(let ( (base-indent 0) ; indentation of base line (let ((base-indent 0) ; indentation of base line
(base-text "") ; and text of base line (base-text "") ; and text of base line
(base-found-p nil)) (base-found-p nil))
(condition-case nil ; in case no enclosing block (condition-case nil ; in case no enclosing block
...@@ -483,10 +590,14 @@ needed so that only a single column position is deleted." ...@@ -483,10 +590,14 @@ needed so that only a single column position is deleted."
(if base-found-p (if base-found-p
(message "Closes block: %s" base-text))))) (message "Closes block: %s" base-text)))))
;; required for pending-del and delsel modes
(put 'py-delete-char 'delete-selection 'supersede)
(put 'py-delete-char 'pending-delete 'supersede)
(defun py-indent-line () (defun py-indent-line ()
"Fix the indentation of the current line according to Python rules." "Fix the indentation of the current line according to Python rules."
(interactive) (interactive)
(let* ( (ci (current-indentation)) (let* ((ci (current-indentation))
(move-to-indentation-p (<= (current-column) ci)) (move-to-indentation-p (<= (current-column) ci))
(need (py-compute-indentation)) ) (need (py-compute-indentation)) )
(if (/= ci need) (if (/= ci need)
...@@ -497,13 +608,13 @@ needed so that only a single column position is deleted." ...@@ -497,13 +608,13 @@ needed so that only a single column position is deleted."
(if move-to-indentation-p (back-to-indentation)))) (if move-to-indentation-p (back-to-indentation))))
(defun py-newline-and-indent () (defun py-newline-and-indent ()
"Strives to act like the Emacs newline-and-indent. "Strives to act like the Emacs `newline-and-indent'.
This is just `strives to' because correct indentation can't be computed This is just `strives to' because correct indentation can't be computed
from scratch for Python code. In general, deletes the whitespace before from scratch for Python code. In general, deletes the whitespace before
point, inserts a newline, and takes an educated guess as to how you want point, inserts a newline, and takes an educated guess as to how you want
the new line indented." the new line indented."
(interactive) (interactive)
(let ( (ci (current-indentation)) ) (let ((ci (current-indentation)))
(if (< ci (current-column)) ; if point beyond indentation (if (< ci (current-column)) ; if point beyond indentation
(newline-and-indent) (newline-and-indent)
;; else try to act like newline-and-indent "normally" acts ;; else try to act like newline-and-indent "normally" acts
...@@ -516,8 +627,8 @@ the new line indented." ...@@ -516,8 +627,8 @@ the new line indented."
(beginning-of-line) (beginning-of-line)
(cond (cond
;; are we on a continuation line? ;; are we on a continuation line?
( (py-continuation-line-p) ((py-continuation-line-p)
(let ( (startpos (point)) (let ((startpos (point))
(open-bracket-pos (py-nesting-level)) (open-bracket-pos (py-nesting-level))
endpos searching found) endpos searching found)
(if open-bracket-pos (if open-bracket-pos
...@@ -583,20 +694,22 @@ the new line indented." ...@@ -583,20 +694,22 @@ the new line indented."
;; if at start of restriction, or on a non-indenting comment line, ;; if at start of restriction, or on a non-indenting comment line,
;; assume they intended whatever's there ;; assume they intended whatever's there
( (or (bobp) (looking-at "[ \t]*#[^ \t\n]")) ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
(current-indentation) ) (current-indentation))
;; else indentation based on that of the statement that precedes ;; else indentation based on that of the statement that precedes
;; us; use the first line of that statement to establish the base, ;; us; use the first line of that statement to establish the base,
;; in case the user forced a non-std indentation for the ;; in case the user forced a non-std indentation for the
;; continuation lines (if any) ;; continuation lines (if any)
( t (t
;; skip back over blank & non-indenting comment lines ;; skip back over blank & non-indenting comment lines
;; note: will skip a blank or non-indenting comment line that ;; note: will skip a blank or non-indenting comment line that
;; happens to be a continuation line too ;; happens to be a continuation line too
(re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
nil 'move) nil 'move)
;; if we landed inside a string, go to the beginning of that string ;; if we landed inside a string, go to the beginning of that
;; string. this handles triple quoted, multi-line spanning
;; strings.
(let ((state (parse-partial-sexp (let ((state (parse-partial-sexp
(save-excursion (beginning-of-python-def-or-class) (save-excursion (beginning-of-python-def-or-class)
(point)) (point))
...@@ -609,26 +722,27 @@ the new line indented." ...@@ -609,26 +722,27 @@ the new line indented."
(current-indentation)))))) (current-indentation))))))
(defun py-guess-indent-offset (&optional global) (defun py-guess-indent-offset (&optional global)
"Guess a good value for, and change, py-indent-offset. "Guess a good value for, and change, `py-indent-offset'.
By default (without a prefix arg), makes a buffer-local copy of By default (without a prefix arg), makes a buffer-local copy of
py-indent-offset with the new value. This will not affect any other `py-indent-offset' with the new value. This will not affect any other
Python buffers. With a prefix arg, changes the global value of Python buffers. With a prefix arg, changes the global value of
py-indent-offset. This affects all Python buffers (that don't have `py-indent-offset'. This affects all Python buffers (that don't have
their own buffer-local copy), both those currently existing and those their own buffer-local copy), both those currently existing and those
created later in the Emacs session. created later in the Emacs session.
Some people use a different value for py-indent-offset than you use. Some people use a different value for `py-indent-offset' than you use.
There's no excuse for such foolishness, but sometimes you have to deal There's no excuse for such foolishness, but sometimes you have to deal
with their ugly code anyway. This function examines the file and sets with their ugly code anyway. This function examines the file and sets
py-indent-offset to what it thinks it was when they created the mess. `py-indent-offset' to what it thinks it was when they created the
mess.
Specifically, it searches forward from the statement containing point, Specifically, it searches forward from the statement containing point,
looking for a line that opens a block of code. py-indent-offset is set looking for a line that opens a block of code. `py-indent-offset' is
to the difference in indentation between that line and the Python set to the difference in indentation between that line and the Python
statement following it. If the search doesn't succeed going forward, statement following it. If the search doesn't succeed going forward,
it's tried again going backward." it's tried again going backward."
(interactive "P") ; raw prefix arg (interactive "P") ; raw prefix arg
(let ( new-value (let (new-value
(start (point)) (start (point))
restart restart
(found nil) (found nil)
...@@ -676,7 +790,7 @@ it's tried again going backward." ...@@ -676,7 +790,7 @@ it's tried again going backward."
"Shift region of Python code to the left. "Shift region of Python code to the left.
The lines from the line containing the start of the current region up The lines from the line containing the start of the current region up
to (but not including) the line containing the end of the region are to (but not including) the line containing the end of the region are
shifted to the left, by py-indent-offset columns. shifted to the left, by `py-indent-offset' columns.
If a prefix argument is given, the region is instead shifted by that If a prefix argument is given, the region is instead shifted by that
many columns." many columns."
...@@ -689,7 +803,7 @@ many columns." ...@@ -689,7 +803,7 @@ many columns."
"Shift region of Python code to the right. "Shift region of Python code to the right.
The lines from the line containing the start of the current region up The lines from the line containing the start of the current region up
to (but not including) the line containing the end of the region are to (but not including) the line containing the end of the region are
shifted to the right, by py-indent-offset columns. shifted to the right, by `py-indent-offset' columns.
If a prefix argument is given, the region is instead shifted by that If a prefix argument is given, the region is instead shifted by that
many columns." many columns."
...@@ -702,17 +816,17 @@ many columns." ...@@ -702,17 +816,17 @@ many columns."
The lines from the line containing the start of the current region up The lines from the line containing the start of the current region up
to (but not including) the line containing the end of the region are to (but not including) the line containing the end of the region are
reindented. If the first line of the region has a non-whitespace reindented. If the first line of the region has a non-whitespace
character in the first column, the first line is left alone and the rest character in the first column, the first line is left alone and the
of the region is reindented with respect to it. Else the entire region rest of the region is reindented with respect to it. Else the entire
is reindented with respect to the (closest code or indenting-comment) region is reindented with respect to the (closest code or
statement immediately preceding the region. indenting-comment) statement immediately preceding the region.
This is useful when code blocks are moved or yanked, when enclosing This is useful when code blocks are moved or yanked, when enclosing
control structures are introduced or removed, or to reformat code using control structures are introduced or removed, or to reformat code
a new value for the indentation offset. using a new value for the indentation offset.
If a numeric prefix argument is given, it will be used as the value of If a numeric prefix argument is given, it will be used as the value of
the indentation offset. Else the value of py-indent-offset will be the indentation offset. Else the value of `py-indent-offset' will be
used. used.
Warning: The region must be consistently indented before this function Warning: The region must be consistently indented before this function
...@@ -720,20 +834,19 @@ is called! This function does not compute proper indentation from ...@@ -720,20 +834,19 @@ is called! This function does not compute proper indentation from
scratch (that's impossible in Python), it merely adjusts the existing scratch (that's impossible in Python), it merely adjusts the existing
indentation to be correct in context. indentation to be correct in context.
Warning: This function really has no idea what to do with non-indenting Warning: This function really has no idea what to do with
comment lines, and shifts them as if they were indenting comment lines. non-indenting comment lines, and shifts them as if they were indenting
Fixing this appears to require telepathy. comment lines. Fixing this appears to require telepathy.
Special cases: whitespace is deleted from blank lines; continuation Special cases: whitespace is deleted from blank lines; continuation
lines are shifted by the same amount their initial line was shifted, in lines are shifted by the same amount their initial line was shifted,
order to preserve their relative indentation with respect to their in order to preserve their relative indentation with respect to their
initial line; and comment lines beginning in column 1 are ignored." initial line; and comment lines beginning in column 1 are ignored."
(interactive "*r\nP") ; region; raw prefix arg (interactive "*r\nP") ; region; raw prefix arg
(save-excursion (save-excursion
(goto-char end) (beginning-of-line) (setq end (point-marker)) (goto-char end) (beginning-of-line) (setq end (point-marker))
(goto-char start) (beginning-of-line) (goto-char start) (beginning-of-line)
(let ( (py-indent-offset (prefix-numeric-value (let ((py-indent-offset (prefix-numeric-value
(or indent-offset py-indent-offset))) (or indent-offset py-indent-offset)))
(indents '(-1)) ; stack of active indent levels (indents '(-1)) ; stack of active indent levels
(target-column 0) ; column to which to indent (target-column 0) ; column to which to indent
...@@ -746,10 +859,10 @@ initial line; and comment lines beginning in column 1 are ignored." ...@@ -746,10 +859,10 @@ initial line; and comment lines beginning in column 1 are ignored."
(setq ci (current-indentation)) (setq ci (current-indentation))
;; figure out appropriate target column ;; figure out appropriate target column
(cond (cond
( (or (eq (following-char) ?#) ; comment in column 1 ((or (eq (following-char) ?#) ; comment in column 1
(looking-at "[ \t]*$")) ; entirely blank (looking-at "[ \t]*$")) ; entirely blank
(setq target-column 0)) (setq target-column 0))
( (py-continuation-line-p) ; shift relative to base line ((py-continuation-line-p) ; shift relative to base line
(setq target-column (+ ci base-shifted-by))) (setq target-column (+ ci base-shifted-by)))
(t ; new base line (t ; new base line
(if (> ci (car indents)) ; going deeper; push it (if (> ci (car indents)) ; going deeper; push it
...@@ -773,8 +886,8 @@ initial line; and comment lines beginning in column 1 are ignored." ...@@ -773,8 +886,8 @@ initial line; and comment lines beginning in column 1 are ignored."
(forward-line 1)))) (forward-line 1))))
(set-marker end nil)) (set-marker end nil))
;;; Functions for moving point
;; Functions for moving point
(defun py-previous-statement (count) (defun py-previous-statement (count)
"Go to the start of previous Python statement. "Go to the start of previous Python statement.
If the statement at point is the i'th Python statement, goes to the If the statement at point is the i'th Python statement, goes to the
...@@ -784,7 +897,7 @@ first statement. Returns count of statements left to move. ...@@ -784,7 +897,7 @@ first statement. Returns count of statements left to move.
(interactive "p") ; numeric prefix arg (interactive "p") ; numeric prefix arg
(if (< count 0) (py-next-statement (- count)) (if (< count 0) (py-next-statement (- count))
(py-goto-initial-line) (py-goto-initial-line)
(let ( start ) (let (start)
(while (and (while (and
(setq start (point)) ; always true -- side effect (setq start (point)) ; always true -- side effect
(> count 0) (> count 0)
...@@ -803,7 +916,7 @@ do not include blank, comment, or continuation lines." ...@@ -803,7 +916,7 @@ do not include blank, comment, or continuation lines."
(interactive "p") ; numeric prefix arg (interactive "p") ; numeric prefix arg
(if (< count 0) (py-previous-statement (- count)) (if (< count 0) (py-previous-statement (- count))
(beginning-of-line) (beginning-of-line)
(let ( start ) (let (start)
(while (and (while (and
(setq start (point)) ; always true -- side effect (setq start (point)) ; always true -- side effect
(> count 0) (> count 0)
...@@ -819,12 +932,13 @@ speaking, this will be the closest preceding statement that ends with a ...@@ -819,12 +932,13 @@ speaking, this will be the closest preceding statement that ends with a
colon and is indented less than the statement you started on. If colon and is indented less than the statement you started on. If
successful, also sets the mark to the starting point. successful, also sets the mark to the starting point.
`\\[py-mark-block]' can be used afterward to mark the whole code block, if desired. `\\[py-mark-block]' can be used afterward to mark the whole code
block, if desired.
If called from a program, the mark will not be set if optional argument If called from a program, the mark will not be set if optional argument
NOMARK is not nil." NOMARK is not nil."
(interactive) (interactive)
(let ( (start (point)) (let ((start (point))
(found nil) (found nil)
initial-indent) initial-indent)
(py-goto-initial-line) (py-goto-initial-line)
...@@ -856,25 +970,26 @@ NOMARK is not nil." ...@@ -856,25 +970,26 @@ NOMARK is not nil."
"Move point to start of def (or class, with prefix arg). "Move point to start of def (or class, with prefix arg).
Searches back for the closest preceding `def'. If you supply a prefix Searches back for the closest preceding `def'. If you supply a prefix
arg, looks for a `class' instead. The docs assume the `def' case; just arg, looks for a `class' instead. The docs assume the `def' case;
substitute `class' for `def' for the other case. just substitute `class' for `def' for the other case.
If point is in a def statement already, and after the `d', simply moves If point is in a def statement already, and after the `d', simply
point to the start of the statement. moves point to the start of the statement.
Else (point is not in a def statement, or at or before the `d' of a def Else (point is not in a def statement, or at or before the `d' of a
statement), searches for the closest preceding def statement, and leaves def statement), searches for the closest preceding def statement, and
point at its start. If no such statement can be found, leaves point at leaves point at its start. If no such statement can be found, leaves
the start of the buffer. point at the start of the buffer.
Returns t iff a def statement is found by these rules. Returns t iff a def statement is found by these rules.
Note that doing this command repeatedly will take you closer to the start Note that doing this command repeatedly will take you closer to the
of the buffer each time. start of the buffer each time.
If you want to mark the current def/class, see `\\[mark-python-def-or-class]'." If you want to mark the current def/class, see
`\\[mark-python-def-or-class]'."
(interactive "P") ; raw prefix arg (interactive "P") ; raw prefix arg
(let ( (at-or-before-p (<= (current-column) (current-indentation))) (let ((at-or-before-p (<= (current-column) (current-indentation)))
(start-of-line (progn (beginning-of-line) (point))) (start-of-line (progn (beginning-of-line) (point)))
(start-of-stmt (progn (py-goto-initial-line) (point)))) (start-of-stmt (progn (py-goto-initial-line) (point))))
(if (or (/= start-of-stmt start-of-line) (if (or (/= start-of-stmt start-of-line)
...@@ -892,23 +1007,24 @@ substitute `class' for `def' for the other case. ...@@ -892,23 +1007,24 @@ substitute `class' for `def' for the other case.
If point is in a def statement already, this is the def we use. If point is in a def statement already, this is the def we use.
Else if the def found by `\\[beginning-of-python-def-or-class]' contains the statement you Else if the def found by `\\[beginning-of-python-def-or-class]'
started on, that's the def we use. contains the statement you started on, that's the def we use.
Else we search forward for the closest following def, and use that. Else we search forward for the closest following def, and use that.
If a def can be found by these rules, point is moved to the start of the If a def can be found by these rules, point is moved to the start of
line immediately following the def block, and the position of the start the line immediately following the def block, and the position of the
of the def is returned. start of the def is returned.
Else point is moved to the end of the buffer, and nil is returned. Else point is moved to the end of the buffer, and nil is returned.
Note that doing this command repeatedly will take you closer to the end Note that doing this command repeatedly will take you closer to the
of the buffer each time. end of the buffer each time.
If you want to mark the current def/class, see `\\[mark-python-def-or-class]'." If you want to mark the current def/class, see
`\\[mark-python-def-or-class]'."
(interactive "P") ; raw prefix arg (interactive "P") ; raw prefix arg
(let ( (start (progn (py-goto-initial-line) (point))) (let ((start (progn (py-goto-initial-line) (point)))
(which (if class "class" "def")) (which (if class "class" "def"))
(state 'not-found)) (state 'not-found))
;; move point to start of appropriate def/class ;; move point to start of appropriate def/class
...@@ -930,8 +1046,8 @@ If you want to mark the current def/class, see `\\[mark-python-def-or-class]'." ...@@ -930,8 +1046,8 @@ If you want to mark the current def/class, see `\\[mark-python-def-or-class]'."
((eq state 'not-found) nil) ((eq state 'not-found) nil)
(t (error "internal error in end-of-python-def-or-class"))))) (t (error "internal error in end-of-python-def-or-class")))))
;;; Functions for marking regions
;; Functions for marking regions
(defun py-mark-block (&optional extend just-move) (defun py-mark-block (&optional extend just-move)
"Mark following block of lines. With prefix arg, mark structure. "Mark following block of lines. With prefix arg, mark structure.
Easier to use than explain. It sets the region to an `interesting' Easier to use than explain. It sets the region to an `interesting'
...@@ -944,25 +1060,27 @@ of the region depends on the kind of line at the start: ...@@ -944,25 +1060,27 @@ of the region depends on the kind of line at the start:
- Else if a prefix arg is given, and the line begins one of these - Else if a prefix arg is given, and the line begins one of these
structures: structures:
\tif elif else try except finally for while def class
if elif else try except finally for while def class
the region will be set to the body of the structure, including the region will be set to the body of the structure, including
following blocks that `belong' to it, but excluding trailing blank following blocks that `belong' to it, but excluding trailing blank
and comment lines. E.g., if on a `try' statement, the `try' block and comment lines. E.g., if on a `try' statement, the `try' block
and all (if any) of the following `except' and `finally' blocks that and all (if any) of the following `except' and `finally' blocks
belong to the `try' structure will be in the region. Ditto for that belong to the `try' structure will be in the region. Ditto
if/elif/else, for/else and while/else structures, and (a bit for if/elif/else, for/else and while/else structures, and (a bit
degenerate, since they're always one-block structures) def and class degenerate, since they're always one-block structures) def and
blocks. class blocks.
- Else if no prefix argument is given, and the line begins a Python - Else if no prefix argument is given, and the line begins a Python
block (see list above), and the block is not a `one-liner' (i.e., the block (see list above), and the block is not a `one-liner' (i.e.,
statement ends with a colon, not with code), the region will include the statement ends with a colon, not with code), the region will
all succeeding lines up to (but not including) the next code include all succeeding lines up to (but not including) the next
statement (if any) that's indented no more than the starting line, code statement (if any) that's indented no more than the starting
except that trailing blank and comment lines are excluded. E.g., if line, except that trailing blank and comment lines are excluded.
the starting line begins a multi-statement `def' structure, the E.g., if the starting line begins a multi-statement `def'
region will be set to the full function definition, but without any structure, the region will be set to the full function definition,
trailing `noise' lines. but without any trailing `noise' lines.
- Else the region will include all succeeding lines up to (but not - Else the region will include all succeeding lines up to (but not
including) the next blank line, or code or indenting-comment line including) the next blank line, or code or indenting-comment line
...@@ -973,10 +1091,9 @@ of the region depends on the kind of line at the start: ...@@ -973,10 +1091,9 @@ of the region depends on the kind of line at the start:
A msg identifying the location of the mark is displayed in the echo A msg identifying the location of the mark is displayed in the echo
area; or do `\\[exchange-point-and-mark]' to flip down to the end. area; or do `\\[exchange-point-and-mark]' to flip down to the end.
If called from a program, optional argument EXTEND plays the role of the If called from a program, optional argument EXTEND plays the role of
prefix arg, and if optional argument JUST-MOVE is not nil, just moves to the prefix arg, and if optional argument JUST-MOVE is not nil, just
the end of the block (& does not set mark or display a msg)." moves to the end of the block (& does not set mark or display a msg)."
(interactive "P") ; raw prefix arg (interactive "P") ; raw prefix arg
(py-goto-initial-line) (py-goto-initial-line)
;; skip over blank lines ;; skip over blank lines
...@@ -986,11 +1103,11 @@ the end of the block (& does not set mark or display a msg)." ...@@ -986,11 +1103,11 @@ the end of the block (& does not set mark or display a msg)."
(forward-line 1)) (forward-line 1))
(if (eobp) (if (eobp)
(error "Hit end of buffer without finding a non-blank stmt")) (error "Hit end of buffer without finding a non-blank stmt"))
(let ( (initial-pos (point)) (let ((initial-pos (point))
(initial-indent (current-indentation)) (initial-indent (current-indentation))
last-pos ; position of last stmt in region last-pos ; position of last stmt in region
(followers (followers
'( (if elif else) (elif elif else) (else) '((if elif else) (elif elif else) (else)
(try except finally) (except except) (finally) (try except finally) (except except) (finally)
(for else) (while else) (for else) (while else)
(def) (class) ) ) (def) (class) ) )
...@@ -1056,38 +1173,40 @@ Pushes the current mark, then point, on the mark ring (all language ...@@ -1056,38 +1173,40 @@ Pushes the current mark, then point, on the mark ring (all language
modes do this, but although it's handy it's never documented ...). modes do this, but although it's handy it's never documented ...).
In most Emacs language modes, this function bears at least a In most Emacs language modes, this function bears at least a
hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and `\\[beginning-of-python-def-or-class]'. hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
`\\[beginning-of-python-def-or-class]'.
And in earlier versions of Python mode, all 3 were tightly connected. And in earlier versions of Python mode, all 3 were tightly connected.
Turned out that was more confusing than useful: the `goto start' and Turned out that was more confusing than useful: the `goto start' and
`goto end' commands are usually used to search through a file, and people `goto end' commands are usually used to search through a file, and
expect them to act a lot like `search backward' and `search forward' people expect them to act a lot like `search backward' and `search
string-search commands. But because Python `def' and `class' can nest to forward' string-search commands. But because Python `def' and `class'
arbitrary levels, finding the smallest def containing point cannot be can nest to arbitrary levels, finding the smallest def containing
done via a simple backward search: the def containing point may not be point cannot be done via a simple backward search: the def containing
the closest preceding def, or even the closest preceding def that's point may not be the closest preceding def, or even the closest
indented less. The fancy algorithm required is appropriate for the usual preceding def that's indented less. The fancy algorithm required is
uses of this `mark' command, but not for the `goto' variations. appropriate for the usual uses of this `mark' command, but not for the
`goto' variations.
So the def marked by this command may not be the one either of the `goto'
commands find: If point is on a blank or non-indenting comment line, So the def marked by this command may not be the one either of the
moves back to start of the closest preceding code statement or indenting `goto' commands find: If point is on a blank or non-indenting comment
comment line. If this is a `def' statement, that's the def we use. Else line, moves back to start of the closest preceding code statement or
searches for the smallest enclosing `def' block and uses that. Else indenting comment line. If this is a `def' statement, that's the def
signals an error. we use. Else searches for the smallest enclosing `def' block and uses
that. Else signals an error.
When an enclosing def is found: The mark is left immediately beyond the
last line of the def block. Point is left at the start of the def, When an enclosing def is found: The mark is left immediately beyond
except that: if the def is preceded by a number of comment lines the last line of the def block. Point is left at the start of the
followed by (at most) one optional blank line, point is left at the start def, except that: if the def is preceded by a number of comment lines
of the comments; else if the def is preceded by a blank line, point is followed by (at most) one optional blank line, point is left at the
left at its start. start of the comments; else if the def is preceded by a blank line,
point is left at its start.
The intent is to mark the containing def/class and its associated The intent is to mark the containing def/class and its associated
documentation, to make moving and duplicating functions and classes documentation, to make moving and duplicating functions and classes
pleasant." pleasant."
(interactive "P") ; raw prefix arg (interactive "P") ; raw prefix arg
(let ( (start (point)) (let ((start (point))
(which (if class "class" "def"))) (which (if class "class" "def")))
(push-mark start) (push-mark start)
(if (not (py-go-up-tree-to-keyword which)) (if (not (py-go-up-tree-to-keyword which))
...@@ -1118,13 +1237,13 @@ pleasant." ...@@ -1118,13 +1237,13 @@ pleasant."
"Comment out region of code; with prefix arg, uncomment region. "Comment out region of code; with prefix arg, uncomment region.
The lines from the line containing the start of the current region up The lines from the line containing the start of the current region up
to (but not including) the line containing the end of the region are to (but not including) the line containing the end of the region are
commented out, by inserting the string py-block-comment-prefix at the commented out, by inserting the string `py-block-comment-prefix' at
start of each line. With a prefix arg, removes py-block-comment-prefix the start of each line. With a prefix arg, removes
from the start of each line instead." `py-block-comment-prefix' from the start of each line instead."
(interactive "*r\nP") ; region; raw prefix arg (interactive "*r\nP") ; region; raw prefix arg
(goto-char end) (beginning-of-line) (setq end (point)) (goto-char end) (beginning-of-line) (setq end (point))
(goto-char start) (beginning-of-line) (setq start (point)) (goto-char start) (beginning-of-line) (setq start (point))
(let ( (prefix-len (length py-block-comment-prefix)) ) (let ((prefix-len (length py-block-comment-prefix)) )
(save-excursion (save-excursion
(save-restriction (save-restriction
(narrow-to-region start end) (narrow-to-region start end)
...@@ -1137,15 +1256,16 @@ from the start of each line instead." ...@@ -1137,15 +1256,16 @@ from the start of each line instead."
(insert py-block-comment-prefix)) (insert py-block-comment-prefix))
(forward-line 1)))))) (forward-line 1))))))
;;; Documentation functions
;; Documentation functions
;; dump the long form of the mode blurb; does the usual doc escapes, ;; dump the long form of the mode blurb; does the usual doc escapes,
;; plus lines of the form ^[vc]:name$ to suck variable & command ;; plus lines of the form ^[vc]:name$ to suck variable & command docs
;; docs out of the right places, along with the keys they're on & ;; out of the right places, along with the keys they're on & current
;; current values ;; values
(defun py-dump-help-string (str) (defun py-dump-help-string (str)
(with-output-to-temp-buffer "*Help*" (with-output-to-temp-buffer "*Help*"
(let ( (locals (buffer-local-variables)) (let ((locals (buffer-local-variables))
funckind funcname func funcdoc funckind funcname func funcdoc
(start 0) mstart end (start 0) mstart end
keys ) keys )
...@@ -1156,14 +1276,14 @@ from the start of each line instead." ...@@ -1156,14 +1276,14 @@ from the start of each line instead."
func (intern funcname)) func (intern funcname))
(princ (substitute-command-keys (substring str start mstart))) (princ (substitute-command-keys (substring str start mstart)))
(cond (cond
( (equal funckind "c") ; command ((equal funckind "c") ; command
(setq funcdoc (documentation func) (setq funcdoc (documentation func)
keys (concat keys (concat
"Key(s): " "Key(s): "
(mapconcat 'key-description (mapconcat 'key-description
(where-is-internal func py-mode-map) (where-is-internal func py-mode-map)
", ")))) ", "))))
( (equal funckind "v") ; variable ((equal funckind "v") ; variable
(setq funcdoc (substitute-command-keys (setq funcdoc (substitute-command-keys
(get func 'variable-documentation)) (get func 'variable-documentation))
keys (if (assq func locals) keys (if (assq func locals)
...@@ -1175,7 +1295,7 @@ from the start of each line instead." ...@@ -1175,7 +1295,7 @@ from the start of each line instead."
(concat (concat
"Value: " "Value: "
(prin1-to-string (symbol-value func)))))) (prin1-to-string (symbol-value func))))))
( t ; unexpected (t ; unexpected
(error "Error in py-dump-help-string, tag `%s'" funckind))) (error "Error in py-dump-help-string, tag `%s'" funckind)))
(princ (format "\n-> %s:\t%s\t%s\n\n" (princ (format "\n-> %s:\t%s\t%s\n\n"
(if (equal funckind "c") "Command" "Variable") (if (equal funckind "c") "Command" "Variable")
...@@ -1226,13 +1346,13 @@ py-beep-if-tab-change\tring the bell if tab-width is changed ...@@ -1226,13 +1346,13 @@ py-beep-if-tab-change\tring the bell if tab-width is changed
@KINDS OF LINES @KINDS OF LINES
Each physical line in the file is either a `continuation line' (the Each physical line in the file is either a `continuation line' (the
preceding line ends with a backslash that's not part of a comment, or the preceding line ends with a backslash that's not part of a comment, or
paren/bracket/brace nesting level at the start of the line is non-zero, the paren/bracket/brace nesting level at the start of the line is
or both) or an `initial line' (everything else). non-zero, or both) or an `initial line' (everything else).
An initial line is in turn a `blank line' (contains nothing except An initial line is in turn a `blank line' (contains nothing except
possibly blanks or tabs), a `comment line' (leftmost non-blank character possibly blanks or tabs), a `comment line' (leftmost non-blank
is `#'), or a `code line' (everything else). character is `#'), or a `code line' (everything else).
Comment Lines Comment Lines
...@@ -1244,12 +1364,12 @@ nothing after the initial `#'. The indentation commands (see below) ...@@ -1244,12 +1364,12 @@ nothing after the initial `#'. The indentation commands (see below)
treat these exactly as if they were code lines: a line following an treat these exactly as if they were code lines: a line following an
indenting comment line will be indented like the comment line. All indenting comment line will be indented like the comment line. All
other comment lines (those with a non-whitespace character immediately other comment lines (those with a non-whitespace character immediately
following the initial `#') are `non-indenting comment lines', and their following the initial `#') are `non-indenting comment lines', and
indentation is ignored by the indentation commands. their indentation is ignored by the indentation commands.
Indenting comment lines are by far the usual case, and should be used Indenting comment lines are by far the usual case, and should be used
whenever possible. Non-indenting comment lines are useful in cases like whenever possible. Non-indenting comment lines are useful in cases
these: like these:
\ta = b # a very wordy single-line comment that ends up being \ta = b # a very wordy single-line comment that ends up being
\t #... continued onto another line \t #... continued onto another line
...@@ -1258,9 +1378,9 @@ these: ...@@ -1258,9 +1378,9 @@ these:
##\t\tprint 'panic!' # old code we've `commented out' ##\t\tprint 'panic!' # old code we've `commented out'
\t\treturn a \t\treturn a
Since the `#...' and `##' comment lines have a non-whitespace character Since the `#...' and `##' comment lines have a non-whitespace
following the initial `#', Python mode ignores them when computing the character following the initial `#', Python mode ignores them when
proper indentation for the next line. computing the proper indentation for the next line.
Continuation Lines and Statements Continuation Lines and Statements
...@@ -1269,8 +1389,8 @@ individual lines, where a `statement' is a comment or blank line, or a ...@@ -1269,8 +1389,8 @@ individual lines, where a `statement' is a comment or blank line, or a
code line and all of its following continuation lines (if any) code line and all of its following continuation lines (if any)
considered as a single logical unit. The commands in this mode considered as a single logical unit. The commands in this mode
generally (when it makes sense) automatically move to the start of the generally (when it makes sense) automatically move to the start of the
statement containing point, even if point happens to be in the middle of statement containing point, even if point happens to be in the middle
some continuation line. of some continuation line.
@INDENTATION @INDENTATION
...@@ -1432,23 +1552,24 @@ Obscure: When python-mode is first loaded, it looks for all bindings ...@@ -1432,23 +1552,24 @@ Obscure: When python-mode is first loaded, it looks for all bindings
to newline-and-indent in the global keymap, and shadows them with to newline-and-indent in the global keymap, and shadows them with
local bindings to py-newline-and-indent.")) local bindings to py-newline-and-indent."))
;;; Helper functions
;; Helper functions
(defvar py-parse-state-re (defvar py-parse-state-re
(concat (concat
"^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>" "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
"\\|" "\\|"
"^[^ #\t\n]")) "^[^ #\t\n]"))
;; returns the parse state at point (see parse-partial-sexp docs) ;; returns the parse state at point (see parse-partial-sexp docs)
(defun py-parse-state () (defun py-parse-state ()
(save-excursion (save-excursion
(let ( (here (point)) ) (let ((here (point)) )
;; back up to the first preceding line (if any; else start of ;; back up to the first preceding line (if any; else start of
;; buffer) that begins with a popular Python keyword, or a non- ;; buffer) that begins with a popular Python keyword, or a non-
;; whitespace and non-comment character. These are good places to ;; whitespace and non-comment character. These are good places
;; start parsing to see whether where we started is at a non-zero ;; to start parsing to see whether where we started is at a
;; nesting level. It may be slow for people who write huge code ;; non-zero nesting level. It may be slow for people who write
;; blocks or huge lists ... tough beans. ;; huge code blocks or huge lists ... tough beans.
(re-search-backward py-parse-state-re nil 'move) (re-search-backward py-parse-state-re nil 'move)
(beginning-of-line) (beginning-of-line)
(parse-partial-sexp (point) here)))) (parse-partial-sexp (point) here))))
...@@ -1457,7 +1578,7 @@ local bindings to py-newline-and-indent.")) ...@@ -1457,7 +1578,7 @@ local bindings to py-newline-and-indent."))
;; character that opens the smallest enclosing unclosed list; else ;; character that opens the smallest enclosing unclosed list; else
;; returns nil. ;; returns nil.
(defun py-nesting-level () (defun py-nesting-level ()
(let ( (status (py-parse-state)) ) (let ((status (py-parse-state)) )
(if (zerop (car status)) (if (zerop (car status))
nil ; not in a nest nil ; not in a nest
(car (cdr status))))) ; char# of open bracket (car (cdr status))))) ; char# of open bracket
...@@ -1481,13 +1602,15 @@ local bindings to py-newline-and-indent.")) ...@@ -1481,13 +1602,15 @@ local bindings to py-newline-and-indent."))
(or (py-backslash-continuation-line-p) (or (py-backslash-continuation-line-p)
(py-nesting-level)))) (py-nesting-level))))
;; go to initial line of current statement; usually this is the ;; go to initial line of current statement; usually this is the line
;; line we're on, but if we're on the 2nd or following lines of a ;; we're on, but if we're on the 2nd or following lines of a
;; continuation block, we need to go up to the first line of the block. ;; continuation block, we need to go up to the first line of the
;; block.
;; ;;
;; Tricky: We want to avoid quadratic-time behavior for long continued ;; Tricky: We want to avoid quadratic-time behavior for long continued
;; blocks, whether of the backslash or open-bracket varieties, or a mix ;; blocks, whether of the backslash or open-bracket varieties, or a
;; of the two. The following manages to do that in the usual cases. ;; mix of the two. The following manages to do that in the usual
;; cases.
(defun py-goto-initial-line () (defun py-goto-initial-line ()
(let ( open-bracket-pos ) (let ( open-bracket-pos )
(while (py-continuation-line-p) (while (py-continuation-line-p)
...@@ -1502,11 +1625,11 @@ local bindings to py-newline-and-indent.")) ...@@ -1502,11 +1625,11 @@ local bindings to py-newline-and-indent."))
;; go to point right beyond final line of current statement; usually ;; go to point right beyond final line of current statement; usually
;; this is the start of the next line, but if this is a multi-line ;; this is the start of the next line, but if this is a multi-line
;; statement we need to skip over the continuation lines. ;; statement we need to skip over the continuation lines. Tricky:
;; Tricky: Again we need to be clever to avoid quadratic time behavior. ;; Again we need to be clever to avoid quadratic time behavior.
(defun py-goto-beyond-final-line () (defun py-goto-beyond-final-line ()
(forward-line 1) (forward-line 1)
(let ( state ) (let (state)
(while (and (py-continuation-line-p) (while (and (py-continuation-line-p)
(not (eobp))) (not (eobp)))
;; skip over the backslash flavor ;; skip over the backslash flavor
...@@ -1525,11 +1648,10 @@ local bindings to py-newline-and-indent.")) ...@@ -1525,11 +1648,10 @@ local bindings to py-newline-and-indent."))
(forward-line 1)))))) (forward-line 1))))))
;; t iff statement opens a block == iff it ends with a colon that's ;; t iff statement opens a block == iff it ends with a colon that's
;; not in a comment ;; not in a comment. point should be at the start of a statement
;; point should be at the start of a statement
(defun py-statement-opens-block-p () (defun py-statement-opens-block-p ()
(save-excursion (save-excursion
(let ( (start (point)) (let ((start (point))
(finish (progn (py-goto-beyond-final-line) (1- (point)))) (finish (progn (py-goto-beyond-final-line) (1- (point))))
(searching t) (searching t)
(answer nil) (answer nil)
...@@ -1557,16 +1679,15 @@ local bindings to py-newline-and-indent.")) ...@@ -1557,16 +1679,15 @@ local bindings to py-newline-and-indent."))
;; go to point right beyond final line of block begun by the current ;; go to point right beyond final line of block begun by the current
;; line. This is the same as where py-goto-beyond-final-line goes ;; line. This is the same as where py-goto-beyond-final-line goes
;; unless we're on colon line, in which case we go to the end of the ;; unless we're on colon line, in which case we go to the end of the
;; block. ;; block. assumes point is at bolp
;; assumes point is at bolp
(defun py-goto-beyond-block () (defun py-goto-beyond-block ()
(if (py-statement-opens-block-p) (if (py-statement-opens-block-p)
(py-mark-block nil 'just-move) (py-mark-block nil 'just-move)
(py-goto-beyond-final-line))) (py-goto-beyond-final-line)))
;; go to start of first statement (not blank or comment or continuation ;; go to start of first statement (not blank or comment or
;; line) at or preceding point ;; continuation line) at or preceding point. returns t if there is
;; returns t if there is one, else nil ;; one, else nil
(defun py-goto-statement-at-or-above () (defun py-goto-statement-at-or-above ()
(py-goto-initial-line) (py-goto-initial-line)
(if (looking-at py-blank-or-comment-re) (if (looking-at py-blank-or-comment-re)
...@@ -1578,12 +1699,12 @@ local bindings to py-newline-and-indent.")) ...@@ -1578,12 +1699,12 @@ local bindings to py-newline-and-indent."))
nil) nil)
t)) t))
;; go to start of first statement (not blank or comment or continuation ;; go to start of first statement (not blank or comment or
;; line) following the statement containing point ;; continuation line) following the statement containing point returns
;; returns t if there is one, else nil ;; t if there is one, else nil
(defun py-goto-statement-below () (defun py-goto-statement-below ()
(beginning-of-line) (beginning-of-line)
(let ( (start (point)) ) (let ((start (point)))
(py-goto-beyond-final-line) (py-goto-beyond-final-line)
(while (and (while (and
(looking-at py-blank-or-comment-re) (looking-at py-blank-or-comment-re)
...@@ -1593,12 +1714,12 @@ local bindings to py-newline-and-indent.")) ...@@ -1593,12 +1714,12 @@ local bindings to py-newline-and-indent."))
(progn (goto-char start) nil) (progn (goto-char start) nil)
t))) t)))
;; go to start of statement, at or preceding point, starting with keyword ;; go to start of statement, at or preceding point, starting with
;; KEY. Skips blank lines and non-indenting comments upward first. If ;; keyword KEY. Skips blank lines and non-indenting comments upward
;; that statement starts with KEY, done, else go back to first enclosing ;; first. If that statement starts with KEY, done, else go back to
;; block starting with KEY. ;; first enclosing block starting with KEY. If successful, leaves
;; If successful, leaves point at the start of the KEY line & returns t. ;; point at the start of the KEY line & returns t. Else leaves point
;; Else leaves point at an undefined place & returns nil. ;; at an undefined place & returns nil.
(defun py-go-up-tree-to-keyword (key) (defun py-go-up-tree-to-keyword (key)
;; skip blanks and non-indenting # ;; skip blanks and non-indenting #
(py-goto-initial-line) (py-goto-initial-line)
...@@ -1607,7 +1728,7 @@ local bindings to py-newline-and-indent.")) ...@@ -1607,7 +1728,7 @@ local bindings to py-newline-and-indent."))
(zerop (forward-line -1))) ; go back (zerop (forward-line -1))) ; go back
nil) nil)
(py-goto-initial-line) (py-goto-initial-line)
(let* ( (re (concat "[ \t]*" key "\\b")) (let* ((re (concat "[ \t]*" key "\\b"))
(case-fold-search nil) ; let* so looking-at sees this (case-fold-search nil) ; let* so looking-at sees this
(found (looking-at re)) (found (looking-at re))
(dead nil)) (dead nil))
...@@ -1631,7 +1752,7 @@ local bindings to py-newline-and-indent.")) ...@@ -1631,7 +1752,7 @@ local bindings to py-newline-and-indent."))
;; assuming point at bolp, return first keyword ([a-z]+) on the line, ;; assuming point at bolp, return first keyword ([a-z]+) on the line,
;; as a Lisp symbol; return nil if none ;; as a Lisp symbol; return nil if none
(defun py-suck-up-first-keyword () (defun py-suck-up-first-keyword ()
(let ( (case-fold-search nil) ) (let ((case-fold-search nil))
(if (looking-at "[ \t]*\\([a-z]+\\)\\b") (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
(intern (buffer-substring (match-beginning 1) (match-end 1))) (intern (buffer-substring (match-beginning 1) (match-end 1)))
nil))) nil)))
...@@ -1655,11 +1776,12 @@ local bindings to py-newline-and-indent.")) ...@@ -1655,11 +1776,12 @@ local bindings to py-newline-and-indent."))
(and py-inherited-kill-emacs-hook (and py-inherited-kill-emacs-hook
(funcall py-inherited-kill-emacs-hook)))) (funcall py-inherited-kill-emacs-hook))))
;; make PROCESS's buffer visible, append STRING to it, and force display; ;; make PROCESS's buffer visible, append STRING to it, and force
;; also make shell-mode believe the user typed this string, so that ;; display; also make shell-mode believe the user typed this string,
;; kill-output-from-shell and show-output-from-shell work "right" ;; so that kill-output-from-shell and show-output-from-shell work
;; "right"
(defun py-append-to-process-buffer (process string) (defun py-append-to-process-buffer (process string)
(let ( (cbuf (current-buffer)) (let ((cbuf (current-buffer))
(pbuf (process-buffer process)) (pbuf (process-buffer process))
(py-scroll-process-buffer t)) (py-scroll-process-buffer t))
(set-buffer pbuf) (set-buffer pbuf)
...@@ -1673,5 +1795,76 @@ local bindings to py-newline-and-indent.")) ...@@ -1673,5 +1795,76 @@ local bindings to py-newline-and-indent."))
(set-buffer cbuf)) (set-buffer cbuf))
(sit-for 0)) (sit-for 0))
;; To do: (defun py-keep-region-active ()
;; - support for ptags ;; do whatever is necessary to keep the region active in XEmacs.
;; Ignore byte-compiler warnings you might see. Also note that
;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
;; require us to take explicit action.
(and (boundp 'zmacs-region-stays)
(setq zmacs-region-stays t)))
(defconst py-version "2.6"
"`python-mode' version number.")
(defconst py-help-address "bwarsaw@cnri.reston.va.us"
"Address accepting submission of bug reports.")
(defun py-version ()
"Echo the current version of `python-mode' in the minibuffer."
(interactive)
(message "Using `python-mode' version %s" py-version)
(py-keep-region-active))
;; only works under Emacs 19
;(eval-when-compile
; (require 'reporter))
(defun py-submit-bug-report (enhancement-p)
"Submit via mail a bug report on `python-mode'.
With \\[universal-argument] just submit an enhancement request."
(interactive
(list (not (y-or-n-p
"Is this a bug report? (hit `n' to send other comments) "))))
(let ((reporter-prompt-for-summary-p (not enhancement-p)))
(require 'reporter)
(reporter-submit-bug-report
py-help-address ;address
"python-mode" ;pkgname
;; varlist
(if enhancement-p nil
'(py-python-command
py-indent-offset
py-block-comment-prefix
py-scroll-process-buffer
py-temp-directory
py-beep-if-tab-change))
nil ;pre-hooks
nil ;post-hooks
"Dear Barry,") ;salutation
(if enhancement-p nil
(set-mark (point))
(insert
"Please replace this text with a sufficiently large code sample\n\
and an exact recipe so that I can reproduce your problem. Failure\n\
to do so may mean a greater delay in fixing your bug.\n\n")
(exchange-point-and-mark)
(py-keep-region-active))))
;; arrange to kill temp files when Emacs exists
(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
;; have to trust that other people are as respectful of our hook
;; fiddling as we are of theirs
(if (boundp 'py-inherited-kill-emacs-hook)
;; we were loaded before -- trust others not to have screwed us
;; in the meantime (no choice, really)
nil
;; else arrange for our hook to run theirs
(setq py-inherited-kill-emacs-hook kill-emacs-hook)
(setq kill-emacs-hook 'py-kill-emacs-hook)))
(provide 'python-mode)
;;; python-mode.el ends here
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment