1995-03-08 17:33:04 -04:00
|
|
|
|
;;; python-mode.el --- Major mode for editing Python programs
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 1992,1993,1994 Tim Peters
|
|
|
|
|
|
2001-02-20 19:07:56 -04:00
|
|
|
|
;; Author: 1995-2001 Barry A. Warsaw
|
1995-07-05 20:26:15 -03:00
|
|
|
|
;; 1992-1994 Tim Peters
|
1997-11-04 14:47:06 -04:00
|
|
|
|
;; Maintainer: python-mode@python.org
|
|
|
|
|
;; Created: Feb 1992
|
|
|
|
|
;; Keywords: python languages oop
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
2001-07-06 17:27:29 -03:00
|
|
|
|
(defconst py-version "$Revision$"
|
1997-08-09 03:42:08 -03:00
|
|
|
|
"`python-mode' version number.")
|
|
|
|
|
|
1995-03-10 11:58:16 -04:00
|
|
|
|
;; This software is provided as-is, without express or implied
|
|
|
|
|
;; warranty. Permission to use, copy, modify, distribute or sell this
|
|
|
|
|
;; software, without fee, for any purpose and by any individual or
|
|
|
|
|
;; organization, is hereby granted, provided that the above copyright
|
|
|
|
|
;; notice and this paragraph appear in all copies.
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
1996-08-01 17:02:55 -03:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
;; This is a major mode for editing Python programs. It was developed
|
1996-08-20 16:57:34 -03:00
|
|
|
|
;; by Tim Peters after an original idea by Michael A. Guravage. Tim
|
1998-08-20 19:10:46 -03:00
|
|
|
|
;; subsequently left the net; in 1995, Barry Warsaw inherited the mode
|
1998-10-27 18:09:25 -04:00
|
|
|
|
;; and is the current maintainer. Tim's now back but disavows all
|
|
|
|
|
;; responsibility for the mode. Smart Tim :-)
|
1997-12-11 13:23:13 -04:00
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
;; pdbtrack support contributed by Ken Manheimer, April 2001.
|
|
|
|
|
|
|
|
|
|
;; This version of python-mode.el has only been tested with XEmacs
|
|
|
|
|
;; 21.1.14 and Emacs 20.7 as these are the latest versions of these
|
|
|
|
|
;; Emacsen as of this writing (11-Apr-2001). I have no intent to test
|
|
|
|
|
;; it with earlier Emacsen, but I will accept patches if they are
|
|
|
|
|
;; small and reasonable. Please use the SourceForge Python project to
|
|
|
|
|
;; submit bugs or patches:
|
|
|
|
|
;;
|
|
|
|
|
;; http://sourceforge.net/projects/python
|
1997-12-11 13:23:13 -04:00
|
|
|
|
|
1998-08-20 19:10:46 -03:00
|
|
|
|
;; FOR MORE INFORMATION:
|
1997-12-11 13:23:13 -04:00
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
;; There is some information on python-mode.el at
|
|
|
|
|
|
1998-08-20 19:10:46 -03:00
|
|
|
|
;; http://www.python.org/emacs/python-mode/
|
1997-11-03 12:59:38 -04:00
|
|
|
|
;;
|
2001-04-11 17:23:17 -03:00
|
|
|
|
;; but this link is fairly out of date, due to the current difficulty
|
|
|
|
|
;; in updating that site. It does contain links to other packages that
|
|
|
|
|
;; you might find useful, such as pdb interfaces, OO-Browser links,
|
|
|
|
|
;; etc. Eventually, we'll be able to update it much more easily.
|
1997-12-11 13:23:13 -04:00
|
|
|
|
|
|
|
|
|
;; BUG REPORTING:
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
;; As mentioned above, please use the SourceForge Python project for
|
|
|
|
|
;; submitting bug reports or patches. The old recommendation, to use
|
|
|
|
|
;; C-c C-b will still work, but those reports have a higher chance of
|
|
|
|
|
;; getting buried in my mailbox. Please include a complete, but
|
1997-11-03 12:59:38 -04:00
|
|
|
|
;; concise code sample and a recipe for reproducing the bug. Send
|
|
|
|
|
;; suggestions and other comments to python-mode@python.org.
|
|
|
|
|
|
|
|
|
|
;; When in a Python mode buffer, do a C-h m for more help. It's
|
1997-12-11 13:23:13 -04:00
|
|
|
|
;; doubtful that a texinfo manual would be very useful, but if you
|
|
|
|
|
;; want to contribute one, I'll certainly accept it!
|
1995-03-14 14:32:54 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
;;; Code:
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-08-20 18:51:27 -03:00
|
|
|
|
(require 'comint)
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(require 'custom)
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(require 'cl)
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;; user definable variables
|
|
|
|
|
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(defgroup python nil
|
|
|
|
|
"Support for the Python programming language, <http://www.python.org/>"
|
1999-07-28 19:06:06 -03:00
|
|
|
|
:group 'languages
|
|
|
|
|
:prefix "py-")
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(defcustom py-python-command "python"
|
|
|
|
|
"*Shell command used to start Python interpreter."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(defcustom py-jpython-command "jpython"
|
|
|
|
|
"*Shell command used to start the JPython interpreter."
|
|
|
|
|
:type 'string
|
1999-07-28 19:06:06 -03:00
|
|
|
|
:group 'python
|
|
|
|
|
:tag "JPython Command")
|
1998-04-09 20:28:20 -03:00
|
|
|
|
|
1999-02-16 19:36:16 -04:00
|
|
|
|
(defcustom py-default-interpreter 'cpython
|
|
|
|
|
"*Which Python interpreter is used by default.
|
|
|
|
|
The value for this variable can be either `cpython' or `jpython'.
|
|
|
|
|
|
|
|
|
|
When the value is `cpython', the variables `py-python-command' and
|
|
|
|
|
`py-python-command-args' are consulted to determine the interpreter
|
|
|
|
|
and arguments to use.
|
|
|
|
|
|
|
|
|
|
When the value is `jpython', the variables `py-jpython-command' and
|
|
|
|
|
`py-jpython-command-args' are consulted to determine the interpreter
|
|
|
|
|
and arguments to use.
|
|
|
|
|
|
1999-07-28 18:59:43 -03:00
|
|
|
|
Note that this variable is consulted only the first time that a Python
|
|
|
|
|
mode buffer is visited during an Emacs session. After that, use
|
1999-02-16 19:36:16 -04:00
|
|
|
|
\\[py-toggle-shells] to change the interpreter shell."
|
|
|
|
|
:type '(choice (const :tag "Python (a.k.a. CPython)" cpython)
|
|
|
|
|
(const :tag "JPython" jpython))
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1998-02-05 16:45:49 -04:00
|
|
|
|
(defcustom py-python-command-args '("-i")
|
|
|
|
|
"*List of string arguments to be used when starting a Python shell."
|
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(defcustom py-jpython-command-args '("-i")
|
|
|
|
|
"*List of string arguments to be used when starting a JPython shell."
|
|
|
|
|
:type '(repeat string)
|
1999-07-28 19:06:06 -03:00
|
|
|
|
:group 'python
|
|
|
|
|
:tag "JPython Command Args")
|
1998-04-09 20:28:20 -03:00
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(defcustom py-indent-offset 4
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"*Amount of offset per level of indentation.
|
|
|
|
|
`\\[py-guess-indent-offset]' can usually guess a good value when
|
|
|
|
|
you're editing someone else's Python code."
|
1997-08-09 03:42:08 -03:00
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'python)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
2001-06-18 20:40:35 -03:00
|
|
|
|
(defcustom py-continuation-offset 4
|
2001-07-06 17:07:13 -03:00
|
|
|
|
"*Additional amount of offset to give for some continuation lines.
|
2001-06-18 20:40:35 -03:00
|
|
|
|
Continuation lines are those that immediately follow a backslash
|
2001-07-06 17:07:13 -03:00
|
|
|
|
terminated line. Only those continuation lines for a block opening
|
|
|
|
|
statement are given this extra offset."
|
2001-06-18 20:40:35 -03:00
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1998-03-13 13:29:15 -04:00
|
|
|
|
(defcustom py-smart-indentation t
|
|
|
|
|
"*Should `python-mode' try to automagically set some indentation variables?
|
|
|
|
|
When this variable is non-nil, two things happen when a buffer is set
|
|
|
|
|
to `python-mode':
|
|
|
|
|
|
1999-06-03 19:18:59 -03:00
|
|
|
|
1. `py-indent-offset' is guessed from existing code in the buffer.
|
1998-03-16 14:12:13 -04:00
|
|
|
|
Only guessed values between 2 and 8 are considered. If a valid
|
1998-03-13 13:29:15 -04:00
|
|
|
|
guess can't be made (perhaps because you are visiting a new
|
1998-03-16 14:12:13 -04:00
|
|
|
|
file), then the value in `py-indent-offset' is used.
|
1998-03-13 13:29:15 -04:00
|
|
|
|
|
1998-03-16 14:12:13 -04:00
|
|
|
|
2. `indent-tabs-mode' is turned off if `py-indent-offset' does not
|
|
|
|
|
equal `tab-width' (`indent-tabs-mode' is never turned on by
|
|
|
|
|
Python mode). This means that for newly written code, tabs are
|
|
|
|
|
only inserted in indentation if one tab is one indentation
|
|
|
|
|
level, otherwise only spaces are used.
|
1998-03-13 13:29:15 -04:00
|
|
|
|
|
1998-03-13 16:04:52 -04:00
|
|
|
|
Note that both these settings occur *after* `python-mode-hook' is run,
|
|
|
|
|
so if you want to defeat the automagic configuration, you must also
|
|
|
|
|
set `py-smart-indentation' to nil in your `python-mode-hook'."
|
1998-03-13 13:29:15 -04:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(defcustom py-align-multiline-strings-p t
|
|
|
|
|
"*Flag describing how multi-line triple quoted strings are aligned.
|
1995-09-19 17:01:42 -03:00
|
|
|
|
When this flag is non-nil, continuation lines are lined up under the
|
|
|
|
|
preceding line's indentation. When this flag is nil, continuation
|
1997-08-09 03:42:08 -03:00
|
|
|
|
lines are aligned to column zero."
|
|
|
|
|
:type '(choice (const :tag "Align under preceding line" t)
|
|
|
|
|
(const :tag "Align to column zero" nil))
|
|
|
|
|
:group 'python)
|
1995-09-19 17:01:42 -03:00
|
|
|
|
|
1998-09-22 16:51:47 -03:00
|
|
|
|
(defcustom py-block-comment-prefix "##"
|
1996-03-07 14:30:26 -04:00
|
|
|
|
"*String used by \\[comment-region] to comment out a block of code.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
This should follow the convention for non-indenting comment lines so
|
|
|
|
|
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
|
1998-09-22 16:51:47 -03:00
|
|
|
|
`...' is arbitrary). However, this string should not end in whitespace."
|
1997-08-09 03:42:08 -03:00
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(defcustom py-honor-comment-indentation t
|
1996-03-06 14:41:38 -04:00
|
|
|
|
"*Controls how comment lines influence subsequent indentation.
|
1996-03-05 12:28:07 -04:00
|
|
|
|
|
1996-03-06 14:41:38 -04:00
|
|
|
|
When nil, all comment lines are skipped for indentation purposes, and
|
1997-08-09 03:42:08 -03:00
|
|
|
|
if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond).
|
1996-03-06 14:41:38 -04:00
|
|
|
|
|
|
|
|
|
When t, lines that begin with a single `#' are a hint to subsequent
|
|
|
|
|
line indentation. If the previous line is such a comment line (as
|
1998-05-19 11:49:49 -03:00
|
|
|
|
opposed to one that starts with `py-block-comment-prefix'), then its
|
1996-03-06 14:41:38 -04:00
|
|
|
|
indentation is used as a hint for this line's indentation. Lines that
|
|
|
|
|
begin with `py-block-comment-prefix' are ignored for indentation
|
|
|
|
|
purposes.
|
|
|
|
|
|
2002-03-15 12:46:46 -04:00
|
|
|
|
When not nil or t, comment lines that begin with a single `#' are used
|
|
|
|
|
as indentation hints, unless the comment character is in column zero."
|
1997-08-09 03:42:08 -03:00
|
|
|
|
:type '(choice
|
|
|
|
|
(const :tag "Skip all comment lines (fast)" nil)
|
|
|
|
|
(const :tag "Single # `sets' indentation for next line" t)
|
1997-11-03 12:59:38 -04:00
|
|
|
|
(const :tag "Single # `sets' indentation except at column zero"
|
|
|
|
|
other)
|
1997-08-09 03:42:08 -03:00
|
|
|
|
)
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1997-08-09 03:43:20 -03:00
|
|
|
|
(defcustom py-temp-directory
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((ok '(lambda (x)
|
|
|
|
|
(and x
|
|
|
|
|
(setq x (expand-file-name x)) ; always true
|
|
|
|
|
(file-directory-p x)
|
|
|
|
|
(file-writable-p x)
|
|
|
|
|
x))))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(or (funcall ok (getenv "TMPDIR"))
|
|
|
|
|
(funcall ok "/usr/tmp")
|
|
|
|
|
(funcall ok "/tmp")
|
|
|
|
|
(funcall ok ".")
|
|
|
|
|
(error
|
1998-02-25 12:33:56 -04:00
|
|
|
|
"Couldn't find a usable temp directory -- set `py-temp-directory'")))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
"*Directory used for temp files created by a *Python* process.
|
|
|
|
|
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,
|
1997-08-09 03:42:08 -03:00
|
|
|
|
/usr/tmp, /tmp, or the current directory."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1997-08-09 03:43:20 -03:00
|
|
|
|
(defcustom py-beep-if-tab-change t
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"*Ring the bell if `tab-width' is changed.
|
1995-03-08 17:33:04 -04:00
|
|
|
|
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
|
1997-08-09 03:42:08 -03:00
|
|
|
|
the Emacs bell is also rung as a warning."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1997-12-03 01:25:48 -04:00
|
|
|
|
(defcustom py-jump-on-exception t
|
|
|
|
|
"*Jump to innermost exception frame in *Python Output* buffer.
|
1998-08-10 18:44:37 -03:00
|
|
|
|
When this variable is non-nil and an exception occurs when running
|
1997-12-03 01:25:48 -04:00
|
|
|
|
Python code synchronously in a subprocess, jump immediately to the
|
1998-08-10 18:44:37 -03:00
|
|
|
|
source code of the innermost traceback frame."
|
1998-05-19 13:25:04 -03:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
|
|
|
|
(defcustom py-ask-about-save t
|
|
|
|
|
"If not nil, ask about which buffers to save before executing some code.
|
|
|
|
|
Otherwise, all modified buffers are saved without asking."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python)
|
1997-12-03 01:25:48 -04:00
|
|
|
|
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(defcustom py-backspace-function 'backward-delete-char-untabify
|
|
|
|
|
"*Function called by `py-electric-backspace' when deleting backwards."
|
|
|
|
|
:type 'function
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
|
|
|
|
(defcustom py-delete-function 'delete-char
|
|
|
|
|
"*Function called by `py-electric-delete' when deleting forwards."
|
|
|
|
|
:type 'function
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defcustom py-imenu-show-method-args-p nil
|
|
|
|
|
"*Controls echoing of arguments of functions & methods in the Imenu buffer.
|
|
|
|
|
When non-nil, arguments are printed."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python)
|
|
|
|
|
(make-variable-buffer-local 'py-indent-offset)
|
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(defcustom py-pdbtrack-do-tracking-p t
|
|
|
|
|
"*Controls whether the pdbtrack feature is enabled or not.
|
|
|
|
|
When non-nil, pdbtrack is enabled in all comint-based buffers,
|
|
|
|
|
e.g. shell buffers and the *Python* buffer. When using pdb to debug a
|
|
|
|
|
Python program, pdbtrack notices the pdb prompt and displays the
|
|
|
|
|
source file and line that the program is stopped at, much the same way
|
|
|
|
|
as gud-mode does for debugging C programs with gdb."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python)
|
|
|
|
|
(make-variable-buffer-local 'py-pdbtrack-do-tracking-p)
|
|
|
|
|
|
|
|
|
|
(defcustom py-pdbtrack-minor-mode-string " PDB"
|
|
|
|
|
"*String to use in the minor mode list when pdbtrack is enabled."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
1998-01-20 17:43:34 -04:00
|
|
|
|
;; Not customizable
|
|
|
|
|
(defvar py-master-file nil
|
|
|
|
|
"If non-nil, execute the named file instead of the buffer's file.
|
1998-02-25 11:57:47 -04:00
|
|
|
|
The intent is to allow you to set this variable in the file's local
|
1998-01-20 17:43:34 -04:00
|
|
|
|
variable section, e.g.:
|
|
|
|
|
|
|
|
|
|
# Local Variables:
|
|
|
|
|
# py-master-file: \"master.py\"
|
|
|
|
|
# End:
|
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
so that typing \\[py-execute-buffer] in that buffer executes the named
|
1998-10-27 17:54:56 -04:00
|
|
|
|
master file instead of the buffer's file. If the file name has a
|
|
|
|
|
relative path, the value of variable `default-directory' for the
|
|
|
|
|
buffer is prepended to come up with a file name.")
|
1998-01-20 17:43:34 -04:00
|
|
|
|
(make-variable-buffer-local 'py-master-file)
|
|
|
|
|
|
1997-11-04 15:21:50 -04:00
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
|
|
|
|
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
|
|
|
|
|
|
1997-11-05 14:41:11 -04:00
|
|
|
|
(defconst py-emacs-features
|
|
|
|
|
(let (features)
|
|
|
|
|
features)
|
1997-09-04 01:18:07 -03:00
|
|
|
|
"A list of features extant in the Emacs you are using.
|
1997-11-06 10:36:49 -04:00
|
|
|
|
There are many flavors of Emacs out there, with different levels of
|
|
|
|
|
support for features needed by `python-mode'.")
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
1997-02-23 23:37:22 -04:00
|
|
|
|
(defvar python-font-lock-keywords
|
1997-11-06 10:35:15 -04:00
|
|
|
|
(let ((kw1 (mapconcat 'identity
|
|
|
|
|
'("and" "assert" "break" "class"
|
|
|
|
|
"continue" "def" "del" "elif"
|
|
|
|
|
"else" "except" "exec" "for"
|
|
|
|
|
"from" "global" "if" "import"
|
|
|
|
|
"in" "is" "lambda" "not"
|
|
|
|
|
"or" "pass" "print" "raise"
|
2001-06-19 15:24:42 -03:00
|
|
|
|
"return" "while" "yield"
|
1997-11-06 10:35:15 -04:00
|
|
|
|
)
|
|
|
|
|
"\\|"))
|
|
|
|
|
(kw2 (mapconcat 'identity
|
|
|
|
|
'("else:" "except:" "finally:" "try:")
|
|
|
|
|
"\\|"))
|
|
|
|
|
)
|
1996-03-04 20:44:31 -04:00
|
|
|
|
(list
|
1997-11-05 14:55:50 -04:00
|
|
|
|
;; keywords
|
1997-11-06 10:35:15 -04:00
|
|
|
|
(cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
|
|
|
|
|
;; block introducing keywords with immediately following colons.
|
|
|
|
|
;; Yes "except" is in both lists.
|
|
|
|
|
(cons (concat "\\b\\(" kw2 "\\)[ \n\t(]") 1)
|
2000-12-27 13:41:47 -04:00
|
|
|
|
;; `as' but only in "import foo as bar"
|
|
|
|
|
'("[ \t]*\\(\\bfrom\\b.*\\)?\\bimport\\b.*\\b\\(as\\)\\b" . 2)
|
1996-03-04 20:44:31 -04:00
|
|
|
|
;; classes
|
|
|
|
|
'("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
|
|
|
|
|
1 font-lock-type-face)
|
|
|
|
|
;; functions
|
|
|
|
|
'("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
|
|
|
|
|
1 font-lock-function-name-face)
|
|
|
|
|
))
|
1996-03-06 16:32:27 -04:00
|
|
|
|
"Additional expressions to highlight in Python mode.")
|
1997-02-23 23:37:22 -04:00
|
|
|
|
(put 'python-mode 'font-lock-defaults '(python-font-lock-keywords))
|
|
|
|
|
|
1997-11-26 01:40:58 -04:00
|
|
|
|
;; have to bind py-file-queue before installing the kill-emacs-hook
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defvar py-file-queue nil
|
|
|
|
|
"Queue of Python temp files awaiting execution.
|
|
|
|
|
Currently-active file is at the head of the list.")
|
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(defvar py-pdbtrack-is-tracking-p nil)
|
|
|
|
|
|
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
|
|
|
|
;; Constants
|
|
|
|
|
|
|
|
|
|
(defconst py-stringlit-re
|
|
|
|
|
(concat
|
1998-05-19 13:06:21 -03:00
|
|
|
|
;; These fail if backslash-quote ends the string (not worth
|
|
|
|
|
;; fixing?). They precede the short versions so that the first two
|
|
|
|
|
;; quotes don't look like an empty short string.
|
|
|
|
|
;;
|
|
|
|
|
;; (maybe raw), long single quoted triple quoted strings (SQTQ),
|
|
|
|
|
;; with potential embedded single quotes
|
|
|
|
|
"[rR]?'''[^']*\\(\\('[^']\\|''[^']\\)[^']*\\)*'''"
|
|
|
|
|
"\\|"
|
|
|
|
|
;; (maybe raw), long double quoted triple quoted strings (DQTQ),
|
|
|
|
|
;; with potential embedded double quotes
|
|
|
|
|
"[rR]?\"\"\"[^\"]*\\(\\(\"[^\"]\\|\"\"[^\"]\\)[^\"]*\\)*\"\"\""
|
|
|
|
|
"\\|"
|
|
|
|
|
"[rR]?'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
|
1997-08-09 03:42:08 -03:00
|
|
|
|
"\\|" ; or
|
1998-05-19 13:06:21 -03:00
|
|
|
|
"[rR]?\"\\([^\"\n\\]\\|\\\\.\\)*\"" ; double-quoted
|
1998-08-10 13:33:12 -03:00
|
|
|
|
)
|
|
|
|
|
"Regular expression matching a Python string literal.")
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
|
|
|
|
(defconst py-continued-re
|
1998-08-10 13:33:12 -03:00
|
|
|
|
;; This is tricky because a trailing backslash does not mean
|
|
|
|
|
;; continuation if it's in a comment
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(concat
|
|
|
|
|
"\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"\\\\$")
|
|
|
|
|
"Regular expression matching Python backslash continuation lines.")
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
|
1998-08-10 13:34:33 -03:00
|
|
|
|
"Regular expression matching a blank or comment line.")
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
|
|
|
|
(defconst py-outdent-re
|
|
|
|
|
(concat "\\(" (mapconcat 'identity
|
|
|
|
|
'("else:"
|
|
|
|
|
"except\\(\\s +.*\\)?:"
|
|
|
|
|
"finally:"
|
|
|
|
|
"elif\\s +.*:")
|
|
|
|
|
"\\|")
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"\\)")
|
|
|
|
|
"Regular expression matching statements to be dedented one level.")
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
1997-11-03 12:59:38 -04:00
|
|
|
|
(defconst py-block-closing-keywords-re
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"\\(return\\|raise\\|break\\|continue\\|pass\\)"
|
|
|
|
|
"Regular expression matching keywords which typically close a block.")
|
1997-11-03 12:59:38 -04:00
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(defconst py-no-outdent-re
|
1997-11-03 12:59:38 -04:00
|
|
|
|
(concat
|
|
|
|
|
"\\("
|
|
|
|
|
(mapconcat 'identity
|
|
|
|
|
(list "try:"
|
|
|
|
|
"except\\(\\s +.*\\)?:"
|
|
|
|
|
"while\\s +.*:"
|
|
|
|
|
"for\\s +.*:"
|
|
|
|
|
"if\\s +.*:"
|
|
|
|
|
"elif\\s +.*:"
|
|
|
|
|
(concat py-block-closing-keywords-re "[ \t\n]")
|
|
|
|
|
)
|
|
|
|
|
"\\|")
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"\\)")
|
|
|
|
|
"Regular expression matching lines not to dedent after.")
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(defconst py-defun-start-re
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"^\\([ \t]*\\)def[ \t]+\\([a-zA-Z_0-9]+\\)\\|\\(^[a-zA-Z_0-9]+\\)[ \t]*="
|
|
|
|
|
;; If you change this, you probably have to change py-current-defun
|
|
|
|
|
;; as well. This is only used by py-current-defun to find the name
|
|
|
|
|
;; for add-log.el.
|
1998-08-10 13:34:33 -03:00
|
|
|
|
"Regular expression matching a function, method, or variable assignment.")
|
1998-08-10 13:33:12 -03:00
|
|
|
|
|
|
|
|
|
(defconst py-class-start-re "^class[ \t]*\\([a-zA-Z_0-9]+\\)"
|
|
|
|
|
;; If you change this, you probably have to change py-current-defun
|
|
|
|
|
;; as well. This is only used by py-current-defun to find the name
|
|
|
|
|
;; for add-log.el.
|
|
|
|
|
"Regular expression for finding a class name.")
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
|
|
|
|
|
(defconst py-traceback-line-re
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)"
|
|
|
|
|
"Regular expression that describes tracebacks.")
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
;; pdbtrack contants
|
|
|
|
|
(defconst py-pdbtrack-stack-entry-regexp
|
|
|
|
|
"> \\([^(]+\\)(\\([0-9]+\\))[?a-zA-Z0-9_]+()"
|
|
|
|
|
"Regular expression pdbtrack uses to find a stack trace entry.")
|
|
|
|
|
|
|
|
|
|
(defconst py-pdbtrack-input-prompt "\n[(<]?pdb[>)]? "
|
|
|
|
|
"Regular expression pdbtrack uses to recognize a pdb prompt.")
|
|
|
|
|
|
|
|
|
|
(defconst py-pdbtrack-track-range 10000
|
|
|
|
|
"Max number of characters from end of buffer to search for stack entry.")
|
|
|
|
|
|
1997-08-09 03:42:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Major mode boilerplate
|
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
;; define a mode-specific abbrev table for those who use such things
|
|
|
|
|
(defvar python-mode-abbrev-table nil
|
1995-03-08 17:33:04 -04:00
|
|
|
|
"Abbrev table in use in `python-mode' buffers.")
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(define-abbrev-table 'python-mode-abbrev-table nil)
|
|
|
|
|
|
|
|
|
|
(defvar python-mode-hook nil
|
|
|
|
|
"*Hook called by `python-mode'.")
|
|
|
|
|
|
1998-05-19 11:49:49 -03:00
|
|
|
|
;; In previous version of python-mode.el, the hook was incorrectly
|
|
|
|
|
;; called py-mode-hook, and was not defvar'd. Deprecate its use.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(and (fboundp 'make-obsolete-variable)
|
|
|
|
|
(make-obsolete-variable 'py-mode-hook 'python-mode-hook))
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(defvar py-mode-map ()
|
|
|
|
|
"Keymap used in `python-mode' buffers.")
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if py-mode-map
|
1997-08-09 03:42:08 -03:00
|
|
|
|
nil
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(setq py-mode-map (make-sparse-keymap))
|
1997-11-04 15:21:50 -04:00
|
|
|
|
;; electric keys
|
|
|
|
|
(define-key py-mode-map ":" 'py-electric-colon)
|
|
|
|
|
;; indentation level modifiers
|
|
|
|
|
(define-key py-mode-map "\C-c\C-l" 'py-shift-region-left)
|
|
|
|
|
(define-key py-mode-map "\C-c\C-r" 'py-shift-region-right)
|
|
|
|
|
(define-key py-mode-map "\C-c<" 'py-shift-region-left)
|
|
|
|
|
(define-key py-mode-map "\C-c>" 'py-shift-region-right)
|
|
|
|
|
;; subprocess commands
|
|
|
|
|
(define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(define-key py-mode-map "\C-c\C-m" 'py-execute-import-or-reload)
|
1998-05-19 13:15:26 -03:00
|
|
|
|
(define-key py-mode-map "\C-c\C-s" 'py-execute-string)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(define-key py-mode-map "\C-c|" 'py-execute-region)
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(define-key py-mode-map "\e\C-x" 'py-execute-def-or-class)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(define-key py-mode-map "\C-c!" 'py-shell)
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(define-key py-mode-map "\C-c\C-t" 'py-toggle-shells)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
;; Caution! Enter here at your own risk. We are trying to support
|
|
|
|
|
;; several behaviors and it gets disgusting. :-( This logic ripped
|
|
|
|
|
;; largely from CC Mode.
|
|
|
|
|
;;
|
|
|
|
|
;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
|
|
|
|
|
;; backwards deletion behavior to DEL, which both Delete and
|
|
|
|
|
;; Backspace get translated to. There's no way to separate this
|
|
|
|
|
;; behavior in a clean way, so deal with it! Besides, it's been
|
|
|
|
|
;; this way since the dawn of time.
|
|
|
|
|
(if (not (boundp 'delete-key-deletes-forward))
|
|
|
|
|
(define-key py-mode-map "\177" 'py-electric-backspace)
|
|
|
|
|
;; However, XEmacs 20 actually achieved enlightenment. It is
|
|
|
|
|
;; possible to sanely define both backward and forward deletion
|
|
|
|
|
;; behavior under X separately (TTYs are forever beyond hope, but
|
|
|
|
|
;; who cares? XEmacs 20 does the right thing with these too).
|
|
|
|
|
(define-key py-mode-map [delete] 'py-electric-delete)
|
|
|
|
|
(define-key py-mode-map [backspace] 'py-electric-backspace))
|
1997-11-26 16:30:33 -04:00
|
|
|
|
;; Separate M-BS from C-M-h. The former should remain
|
|
|
|
|
;; backward-kill-word.
|
|
|
|
|
(define-key py-mode-map [(control meta h)] 'py-mark-def-or-class)
|
1997-11-04 20:51:08 -04:00
|
|
|
|
(define-key py-mode-map "\C-c\C-k" 'py-mark-block)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
;; Miscellaneous
|
|
|
|
|
(define-key py-mode-map "\C-c:" 'py-guess-indent-offset)
|
|
|
|
|
(define-key py-mode-map "\C-c\t" 'py-indent-region)
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(define-key py-mode-map "\C-c\C-d" 'py-pdbtrack-toggle-stack-tracking)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(define-key py-mode-map "\C-c\C-n" 'py-next-statement)
|
|
|
|
|
(define-key py-mode-map "\C-c\C-p" 'py-previous-statement)
|
|
|
|
|
(define-key py-mode-map "\C-c\C-u" 'py-goto-block-up)
|
|
|
|
|
(define-key py-mode-map "\C-c#" 'py-comment-region)
|
|
|
|
|
(define-key py-mode-map "\C-c?" 'py-describe-mode)
|
|
|
|
|
(define-key py-mode-map "\C-c\C-hm" 'py-describe-mode)
|
1998-05-19 12:31:46 -03:00
|
|
|
|
(define-key py-mode-map "\e\C-a" 'py-beginning-of-def-or-class)
|
|
|
|
|
(define-key py-mode-map "\e\C-e" 'py-end-of-def-or-class)
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(define-key py-mode-map "\C-c-" 'py-up-exception)
|
|
|
|
|
(define-key py-mode-map "\C-c=" 'py-down-exception)
|
1999-01-18 17:49:39 -04:00
|
|
|
|
;; stuff that is `standard' but doesn't interface well with
|
|
|
|
|
;; python-mode, which forces us to rebind to special commands
|
|
|
|
|
(define-key py-mode-map "\C-xnd" 'py-narrow-to-defun)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
;; information
|
|
|
|
|
(define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
|
|
|
|
|
(define-key py-mode-map "\C-c\C-v" 'py-version)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
;; 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.
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(mapcar #'(lambda (key)
|
|
|
|
|
(define-key py-mode-map key 'py-newline-and-indent))
|
|
|
|
|
(where-is-internal 'newline-and-indent))
|
1999-01-21 13:06:11 -04:00
|
|
|
|
;; Force RET to be py-newline-and-indent even if it didn't get
|
|
|
|
|
;; mapped by the above code. motivation: Emacs' default binding for
|
|
|
|
|
;; RET is `newline' and C-j is `newline-and-indent'. Most Pythoneers
|
|
|
|
|
;; expect RET to do a `py-newline-and-indent' and any Emacsers who
|
|
|
|
|
;; dislike this are probably knowledgeable enough to do a rebind.
|
|
|
|
|
;; However, we do *not* change C-j since many Emacsers have already
|
|
|
|
|
;; swapped RET and C-j and they don't want C-j bound to `newline' to
|
|
|
|
|
;; change.
|
|
|
|
|
(define-key py-mode-map "\C-m" 'py-newline-and-indent)
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
(defvar py-mode-output-map nil
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Keymap used in *Python Output* buffers.")
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(if py-mode-output-map
|
|
|
|
|
nil
|
|
|
|
|
(setq py-mode-output-map (make-sparse-keymap))
|
|
|
|
|
(define-key py-mode-output-map [button2] 'py-mouseto-exception)
|
|
|
|
|
(define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
|
|
|
|
|
;; TBD: Disable all self-inserting keys. This is bogus, we should
|
|
|
|
|
;; really implement this as *Python Output* buffer being read-only
|
|
|
|
|
(mapcar #' (lambda (key)
|
|
|
|
|
(define-key py-mode-output-map key
|
|
|
|
|
#'(lambda () (interactive) (beep))))
|
|
|
|
|
(where-is-internal 'self-insert-command))
|
1995-03-08 17:50:28 -04:00
|
|
|
|
)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
1998-08-20 18:51:27 -03:00
|
|
|
|
(defvar py-shell-map nil
|
|
|
|
|
"Keymap used in *Python* shell buffers.")
|
|
|
|
|
(if py-shell-map
|
|
|
|
|
nil
|
|
|
|
|
(setq py-shell-map (copy-keymap comint-mode-map))
|
|
|
|
|
(define-key py-shell-map [tab] 'tab-to-tab-stop)
|
|
|
|
|
(define-key py-shell-map "\C-c-" 'py-up-exception)
|
|
|
|
|
(define-key py-shell-map "\C-c=" 'py-down-exception)
|
|
|
|
|
)
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(defvar py-mode-syntax-table nil
|
|
|
|
|
"Syntax table used in `python-mode' buffers.")
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if py-mode-syntax-table
|
1997-08-09 03:42:08 -03:00
|
|
|
|
nil
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(setq py-mode-syntax-table (make-syntax-table))
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(modify-syntax-entry ?\( "()" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\) ")(" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\[ "(]" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\] ")[" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\{ "(}" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\} "){" py-mode-syntax-table)
|
|
|
|
|
;; Add operator symbols misassigned in the std table
|
|
|
|
|
(modify-syntax-entry ?\$ "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\% "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\& "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\* "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\+ "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\- "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\/ "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\< "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\= "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\> "." py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\| "." py-mode-syntax-table)
|
|
|
|
|
;; For historical reasons, underscore is word class instead of
|
|
|
|
|
;; symbol class. GNU conventions say it should be symbol class, but
|
|
|
|
|
;; there's a natural conflict between what major mode authors want
|
|
|
|
|
;; and what users expect from `forward-word' and `backward-word'.
|
|
|
|
|
;; Guido and I have hashed this out and have decided to keep
|
|
|
|
|
;; underscore in word class. If you're tempted to change it, try
|
|
|
|
|
;; binding M-f and M-b to py-forward-into-nomenclature and
|
1999-02-16 19:36:16 -04:00
|
|
|
|
;; py-backward-into-nomenclature instead. This doesn't help in all
|
|
|
|
|
;; situations where you'd want the different behavior
|
|
|
|
|
;; (e.g. backward-kill-word).
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(modify-syntax-entry ?\_ "w" py-mode-syntax-table)
|
|
|
|
|
;; Both single quote and double quote are string delimiters
|
|
|
|
|
(modify-syntax-entry ?\' "\"" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\" "\"" py-mode-syntax-table)
|
|
|
|
|
;; backquote is open and close paren
|
|
|
|
|
(modify-syntax-entry ?\` "$" py-mode-syntax-table)
|
|
|
|
|
;; comment delimiters
|
|
|
|
|
(modify-syntax-entry ?\# "<" py-mode-syntax-table)
|
|
|
|
|
(modify-syntax-entry ?\n ">" py-mode-syntax-table)
|
|
|
|
|
)
|
1996-09-04 12:12:42 -03:00
|
|
|
|
|
|
|
|
|
|
1998-09-14 13:16:18 -03:00
|
|
|
|
|
|
|
|
|
;; Utilities
|
|
|
|
|
|
|
|
|
|
(defmacro py-safe (&rest body)
|
|
|
|
|
"Safely execute BODY, return nil if an error occurred."
|
|
|
|
|
(` (condition-case nil
|
|
|
|
|
(progn (,@ body))
|
|
|
|
|
(error nil))))
|
|
|
|
|
|
|
|
|
|
(defsubst py-keep-region-active ()
|
|
|
|
|
"Keep the region active in XEmacs."
|
|
|
|
|
;; Ignore byte-compiler warnings you might see. Also note that
|
|
|
|
|
;; FSF's Emacs 19 does it differently; its policy doesn't require us
|
|
|
|
|
;; to take explicit action.
|
|
|
|
|
(and (boundp 'zmacs-region-stays)
|
|
|
|
|
(setq zmacs-region-stays t)))
|
|
|
|
|
|
|
|
|
|
(defsubst py-point (position)
|
|
|
|
|
"Returns the value of point at certain commonly referenced POSITIONs.
|
|
|
|
|
POSITION can be one of the following symbols:
|
|
|
|
|
|
|
|
|
|
bol -- beginning of line
|
|
|
|
|
eol -- end of line
|
|
|
|
|
bod -- beginning of def or class
|
|
|
|
|
eod -- end of def or class
|
|
|
|
|
bob -- beginning of buffer
|
|
|
|
|
eob -- end of buffer
|
|
|
|
|
boi -- back to indentation
|
|
|
|
|
bos -- beginning of statement
|
|
|
|
|
|
|
|
|
|
This function does not modify point or mark."
|
|
|
|
|
(let ((here (point)))
|
|
|
|
|
(cond
|
|
|
|
|
((eq position 'bol) (beginning-of-line))
|
|
|
|
|
((eq position 'eol) (end-of-line))
|
|
|
|
|
((eq position 'bod) (py-beginning-of-def-or-class))
|
|
|
|
|
((eq position 'eod) (py-end-of-def-or-class))
|
|
|
|
|
;; Kind of funny, I know, but useful for py-up-exception.
|
|
|
|
|
((eq position 'bob) (beginning-of-buffer))
|
|
|
|
|
((eq position 'eob) (end-of-buffer))
|
|
|
|
|
((eq position 'boi) (back-to-indentation))
|
|
|
|
|
((eq position 'bos) (py-goto-initial-line))
|
|
|
|
|
(t (error "Unknown buffer position requested: %s" position))
|
|
|
|
|
)
|
|
|
|
|
(prog1
|
|
|
|
|
(point)
|
|
|
|
|
(goto-char here))))
|
|
|
|
|
|
|
|
|
|
(defsubst py-highlight-line (from to file line)
|
|
|
|
|
(cond
|
|
|
|
|
((fboundp 'make-extent)
|
|
|
|
|
;; XEmacs
|
|
|
|
|
(let ((e (make-extent from to)))
|
|
|
|
|
(set-extent-property e 'mouse-face 'highlight)
|
|
|
|
|
(set-extent-property e 'py-exc-info (cons file line))
|
|
|
|
|
(set-extent-property e 'keymap py-mode-output-map)))
|
|
|
|
|
(t
|
|
|
|
|
;; Emacs -- Please port this!
|
|
|
|
|
)
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
(defun py-in-literal (&optional lim)
|
|
|
|
|
"Return non-nil if point is in a Python literal (a comment or string).
|
|
|
|
|
Optional argument LIM indicates the beginning of the containing form,
|
|
|
|
|
i.e. the limit on how far back to scan."
|
|
|
|
|
;; This is the version used for non-XEmacs, which has a nicer
|
|
|
|
|
;; interface.
|
|
|
|
|
;;
|
|
|
|
|
;; WARNING: Watch out for infinite recursion.
|
|
|
|
|
(let* ((lim (or lim (py-point 'bod)))
|
|
|
|
|
(state (parse-partial-sexp lim (point))))
|
|
|
|
|
(cond
|
|
|
|
|
((nth 3 state) 'string)
|
|
|
|
|
((nth 4 state) 'comment)
|
|
|
|
|
(t nil))))
|
|
|
|
|
|
|
|
|
|
;; XEmacs has a built-in function that should make this much quicker.
|
|
|
|
|
;; In this case, lim is ignored
|
|
|
|
|
(defun py-fast-in-literal (&optional lim)
|
|
|
|
|
"Fast version of `py-in-literal', used only by XEmacs.
|
|
|
|
|
Optional LIM is ignored."
|
|
|
|
|
;; don't have to worry about context == 'block-comment
|
|
|
|
|
(buffer-syntactic-context))
|
|
|
|
|
|
|
|
|
|
(if (fboundp 'buffer-syntactic-context)
|
|
|
|
|
(defalias 'py-in-literal 'py-fast-in-literal))
|
|
|
|
|
|
|
|
|
|
|
1996-07-29 18:05:05 -03:00
|
|
|
|
|
|
|
|
|
;; Menu definitions, only relevent if you have the easymenu.el package
|
|
|
|
|
;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
|
1996-08-06 12:43:33 -03:00
|
|
|
|
(defvar py-menu nil
|
|
|
|
|
"Menu for Python Mode.
|
1997-08-09 03:42:08 -03:00
|
|
|
|
This menu will get created automatically if you have the `easymenu'
|
|
|
|
|
package. Note that the latest X/Emacs releases contain this package.")
|
|
|
|
|
|
|
|
|
|
(and (py-safe (require 'easymenu) t)
|
|
|
|
|
(easy-menu-define
|
|
|
|
|
py-menu py-mode-map "Python Mode menu"
|
|
|
|
|
'("Python"
|
|
|
|
|
["Comment Out Region" py-comment-region (mark)]
|
|
|
|
|
["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)]
|
|
|
|
|
"-"
|
|
|
|
|
["Mark current block" py-mark-block t]
|
1997-11-04 20:51:08 -04:00
|
|
|
|
["Mark current def" py-mark-def-or-class t]
|
|
|
|
|
["Mark current class" (py-mark-def-or-class t) t]
|
1997-08-09 03:42:08 -03:00
|
|
|
|
"-"
|
|
|
|
|
["Shift region left" py-shift-region-left (mark)]
|
|
|
|
|
["Shift region right" py-shift-region-right (mark)]
|
|
|
|
|
"-"
|
1998-05-19 12:54:45 -03:00
|
|
|
|
["Import/reload file" py-execute-import-or-reload t]
|
1997-08-09 03:42:08 -03:00
|
|
|
|
["Execute buffer" py-execute-buffer t]
|
|
|
|
|
["Execute region" py-execute-region (mark)]
|
1998-05-19 12:54:45 -03:00
|
|
|
|
["Execute def or class" py-execute-def-or-class (mark)]
|
1998-05-19 13:15:26 -03:00
|
|
|
|
["Execute string" py-execute-string t]
|
1997-08-09 03:42:08 -03:00
|
|
|
|
["Start interpreter..." py-shell t]
|
|
|
|
|
"-"
|
|
|
|
|
["Go to start of block" py-goto-block-up t]
|
1998-05-19 12:31:46 -03:00
|
|
|
|
["Go to start of class" (py-beginning-of-def-or-class t) t]
|
|
|
|
|
["Move to end of class" (py-end-of-def-or-class t) t]
|
|
|
|
|
["Move to start of def" py-beginning-of-def-or-class t]
|
|
|
|
|
["Move to end of def" py-end-of-def-or-class t]
|
1997-08-09 03:42:08 -03:00
|
|
|
|
"-"
|
|
|
|
|
["Describe mode" py-describe-mode t]
|
|
|
|
|
)))
|
1996-07-29 18:05:05 -03:00
|
|
|
|
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
;; Imenu definitions
|
|
|
|
|
(defvar py-imenu-class-regexp
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(concat ; <<classes>>
|
|
|
|
|
"\\(" ;
|
|
|
|
|
"^[ \t]*" ; newline and maybe whitespace
|
|
|
|
|
"\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
|
|
|
|
|
; possibly multiple superclasses
|
1998-04-04 17:36:53 -04:00
|
|
|
|
"\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)"
|
1996-08-01 16:48:02 -03:00
|
|
|
|
"[ \t]*:" ; and the final :
|
|
|
|
|
"\\)" ; >>classes<<
|
|
|
|
|
)
|
1998-10-27 20:10:45 -04:00
|
|
|
|
"Regexp for Python classes for use with the Imenu package."
|
1996-08-01 16:48:02 -03:00
|
|
|
|
)
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defvar py-imenu-method-regexp
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(concat ; <<methods and functions>>
|
|
|
|
|
"\\(" ;
|
|
|
|
|
"^[ \t]*" ; new line and maybe whitespace
|
|
|
|
|
"\\(def[ \t]+" ; function definitions start with def
|
|
|
|
|
"\\([a-zA-Z0-9_]+\\)" ; name is here
|
|
|
|
|
; function arguments...
|
1998-10-28 00:08:13 -04:00
|
|
|
|
;; "[ \t]*(\\([-+/a-zA-Z0-9_=,\* \t\n.()\"'#]*\\))"
|
|
|
|
|
"[ \t]*(\\([^:#]*\\))"
|
1996-08-01 16:48:02 -03:00
|
|
|
|
"\\)" ; end of def
|
|
|
|
|
"[ \t]*:" ; and then the :
|
|
|
|
|
"\\)" ; >>methods and functions<<
|
|
|
|
|
)
|
1998-10-27 20:10:45 -04:00
|
|
|
|
"Regexp for Python methods/functions for use with the Imenu package."
|
1996-08-01 16:48:02 -03:00
|
|
|
|
)
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defvar py-imenu-method-no-arg-parens '(2 8)
|
|
|
|
|
"Indices into groups of the Python regexp for use with Imenu.
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
Using these values will result in smaller Imenu lists, as arguments to
|
1996-08-01 16:48:02 -03:00
|
|
|
|
functions are not listed.
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
See the variable `py-imenu-show-method-args-p' for more
|
1996-08-01 16:48:02 -03:00
|
|
|
|
information.")
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defvar py-imenu-method-arg-parens '(2 7)
|
1998-10-27 17:54:56 -04:00
|
|
|
|
"Indices into groups of the Python regexp for use with imenu.
|
1998-10-27 20:10:45 -04:00
|
|
|
|
Using these values will result in large Imenu lists, as arguments to
|
1996-08-01 16:48:02 -03:00
|
|
|
|
functions are listed.
|
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
See the variable `py-imenu-show-method-args-p' for more
|
1996-08-01 16:48:02 -03:00
|
|
|
|
information.")
|
|
|
|
|
|
|
|
|
|
;; Note that in this format, this variable can still be used with the
|
|
|
|
|
;; imenu--generic-function. Otherwise, there is no real reason to have
|
|
|
|
|
;; it.
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defvar py-imenu-generic-expression
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(cons
|
|
|
|
|
(concat
|
1998-10-27 20:10:45 -04:00
|
|
|
|
py-imenu-class-regexp
|
1996-08-01 16:48:02 -03:00
|
|
|
|
"\\|" ; or...
|
1998-10-27 20:10:45 -04:00
|
|
|
|
py-imenu-method-regexp
|
1996-08-01 16:48:02 -03:00
|
|
|
|
)
|
1998-10-27 20:10:45 -04:00
|
|
|
|
py-imenu-method-no-arg-parens)
|
|
|
|
|
"Generic Python expression which may be used directly with Imenu.
|
1996-08-01 16:48:02 -03:00
|
|
|
|
Used by setting the variable `imenu-generic-expression' to this value.
|
1998-10-27 20:10:45 -04:00
|
|
|
|
Also, see the function \\[py-imenu-create-index] for a better
|
|
|
|
|
alternative for finding the index.")
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
;; These next two variables are used when searching for the Python
|
1996-08-01 16:48:02 -03:00
|
|
|
|
;; class/definitions. Just saving some time in accessing the
|
|
|
|
|
;; generic-python-expression, really.
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defvar py-imenu-generic-regexp nil)
|
|
|
|
|
(defvar py-imenu-generic-parens nil)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun py-imenu-create-index-function ()
|
|
|
|
|
"Python interface function for the Imenu package.
|
|
|
|
|
Finds all Python classes and functions/methods. Calls function
|
|
|
|
|
\\[py-imenu-create-index-engine]. See that function for the details
|
|
|
|
|
of how this works."
|
|
|
|
|
(setq py-imenu-generic-regexp (car py-imenu-generic-expression)
|
|
|
|
|
py-imenu-generic-parens (if py-imenu-show-method-args-p
|
|
|
|
|
py-imenu-method-arg-parens
|
|
|
|
|
py-imenu-method-no-arg-parens))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(goto-char (point-min))
|
1998-10-28 00:08:13 -04:00
|
|
|
|
;; Warning: When the buffer has no classes or functions, this will
|
|
|
|
|
;; return nil, which seems proper according to the Imenu API, but
|
|
|
|
|
;; causes an error in the XEmacs port of Imenu. Sigh.
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(py-imenu-create-index-engine nil))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(defun py-imenu-create-index-engine (&optional start-indent)
|
|
|
|
|
"Function for finding Imenu definitions in Python.
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
|
|
|
|
Finds all definitions (classes, methods, or functions) in a Python
|
1998-10-27 20:10:45 -04:00
|
|
|
|
file for the Imenu package.
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
|
|
|
|
Returns a possibly nested alist of the form
|
|
|
|
|
|
|
|
|
|
(INDEX-NAME . INDEX-POSITION)
|
|
|
|
|
|
|
|
|
|
The second element of the alist may be an alist, producing a nested
|
|
|
|
|
list as in
|
|
|
|
|
|
|
|
|
|
(INDEX-NAME . INDEX-ALIST)
|
|
|
|
|
|
|
|
|
|
This function should not be called directly, as it calls itself
|
|
|
|
|
recursively and requires some setup. Rather this is the engine for
|
1998-10-27 20:10:45 -04:00
|
|
|
|
the function \\[py-imenu-create-index-function].
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
|
|
|
|
It works recursively by looking for all definitions at the current
|
|
|
|
|
indention level. When it finds one, it adds it to the alist. If it
|
|
|
|
|
finds a definition at a greater indentation level, it removes the
|
1998-05-19 11:49:49 -03:00
|
|
|
|
previous definition from the alist. In its place it adds all
|
1996-08-01 16:48:02 -03:00
|
|
|
|
definitions found at the next indentation level. When it finds a
|
1998-10-27 20:10:45 -04:00
|
|
|
|
definition that is less indented then the current level, it returns
|
|
|
|
|
the alist it has created thus far.
|
1996-08-01 16:48:02 -03:00
|
|
|
|
|
|
|
|
|
The optional argument START-INDENT indicates the starting indentation
|
|
|
|
|
at which to continue looking for Python classes, methods, or
|
|
|
|
|
functions. If this is not supplied, the function uses the indentation
|
|
|
|
|
of the first definition found."
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(let (index-alist
|
|
|
|
|
sub-method-alist
|
1996-08-01 16:48:02 -03:00
|
|
|
|
looking-p
|
|
|
|
|
def-name prev-name
|
|
|
|
|
cur-indent def-pos
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(class-paren (first py-imenu-generic-parens))
|
|
|
|
|
(def-paren (second py-imenu-generic-parens)))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(setq looking-p
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(re-search-forward py-imenu-generic-regexp (point-max) t))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(while looking-p
|
|
|
|
|
(save-excursion
|
1998-10-27 20:10:45 -04:00
|
|
|
|
;; used to set def-name to this value but generic-extract-name
|
|
|
|
|
;; is new to imenu-1.14. this way it still works with
|
|
|
|
|
;; imenu-1.11
|
|
|
|
|
;;(imenu--generic-extract-name py-imenu-generic-parens))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(let ((cur-paren (if (match-beginning class-paren)
|
|
|
|
|
class-paren def-paren)))
|
|
|
|
|
(setq def-name
|
1997-11-26 02:14:40 -04:00
|
|
|
|
(buffer-substring-no-properties (match-beginning cur-paren)
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(match-end cur-paren))))
|
1998-08-17 23:00:44 -03:00
|
|
|
|
(save-match-data
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(py-beginning-of-def-or-class 'either))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(setq cur-indent (current-indentation)))
|
1998-10-27 20:10:45 -04:00
|
|
|
|
;; HACK: want to go to the next correct definition location. We
|
|
|
|
|
;; explicitly list them here but it would be better to have them
|
|
|
|
|
;; in a list.
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(setq def-pos
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(or (match-beginning class-paren)
|
|
|
|
|
(match-beginning def-paren)))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
;; if we don't have a starting indent level, take this one
|
|
|
|
|
(or start-indent
|
|
|
|
|
(setq start-indent cur-indent))
|
|
|
|
|
;; if we don't have class name yet, take this one
|
|
|
|
|
(or prev-name
|
|
|
|
|
(setq prev-name def-name))
|
|
|
|
|
;; what level is the next definition on? must be same, deeper
|
|
|
|
|
;; or shallower indentation
|
|
|
|
|
(cond
|
|
|
|
|
;; at the same indent level, add it to the list...
|
|
|
|
|
((= start-indent cur-indent)
|
|
|
|
|
(push (cons def-name def-pos) index-alist))
|
1998-10-27 20:10:45 -04:00
|
|
|
|
;; deeper indented expression, recurse
|
1996-08-01 16:48:02 -03:00
|
|
|
|
((< start-indent cur-indent)
|
|
|
|
|
;; the point is currently on the expression we're supposed to
|
|
|
|
|
;; start on, so go back to the last expression. The recursive
|
|
|
|
|
;; call will find this place again and add it to the correct
|
|
|
|
|
;; list
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(re-search-backward py-imenu-generic-regexp (point-min) 'move)
|
|
|
|
|
(setq sub-method-alist (py-imenu-create-index-engine cur-indent))
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(if sub-method-alist
|
|
|
|
|
;; we put the last element on the index-alist on the start
|
|
|
|
|
;; of the submethod alist so the user can still get to it.
|
|
|
|
|
(let ((save-elmt (pop index-alist)))
|
1997-11-26 02:14:40 -04:00
|
|
|
|
(push (cons prev-name
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(cons save-elmt sub-method-alist))
|
|
|
|
|
index-alist))))
|
|
|
|
|
;; found less indented expression, we're done.
|
|
|
|
|
(t
|
|
|
|
|
(setq looking-p nil)
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(re-search-backward py-imenu-generic-regexp (point-min) t)))
|
|
|
|
|
;; end-cond
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(setq prev-name def-name)
|
|
|
|
|
(and looking-p
|
|
|
|
|
(setq looking-p
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(re-search-forward py-imenu-generic-regexp
|
1996-08-01 16:48:02 -03:00
|
|
|
|
(point-max) 'move))))
|
|
|
|
|
(nreverse index-alist)))
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun python-mode ()
|
|
|
|
|
"Major mode for editing Python files.
|
1995-03-08 17:33:04 -04:00
|
|
|
|
To submit a problem report, enter `\\[py-submit-bug-report]' from a
|
|
|
|
|
`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
|
|
|
|
|
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.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
COMMANDS
|
|
|
|
|
\\{py-mode-map}
|
|
|
|
|
VARIABLES
|
|
|
|
|
|
1996-07-29 18:05:05 -03:00
|
|
|
|
py-indent-offset\t\tindentation increment
|
1998-08-10 13:33:12 -03:00
|
|
|
|
py-block-comment-prefix\t\tcomment string used by `comment-region'
|
1996-07-29 18:05:05 -03:00
|
|
|
|
py-python-command\t\tshell command to invoke Python interpreter
|
|
|
|
|
py-temp-directory\t\tdirectory used for temp files (if needed)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
py-beep-if-tab-change\t\tring the bell if `tab-width' is changed"
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(interactive)
|
1996-09-04 11:14:10 -03:00
|
|
|
|
;; set up local variables
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(kill-all-local-variables)
|
1996-09-04 11:14:10 -03:00
|
|
|
|
(make-local-variable 'font-lock-defaults)
|
|
|
|
|
(make-local-variable 'paragraph-separate)
|
|
|
|
|
(make-local-variable 'paragraph-start)
|
|
|
|
|
(make-local-variable 'require-final-newline)
|
|
|
|
|
(make-local-variable 'comment-start)
|
1996-12-17 17:56:10 -04:00
|
|
|
|
(make-local-variable 'comment-end)
|
1996-09-04 11:14:10 -03:00
|
|
|
|
(make-local-variable 'comment-start-skip)
|
|
|
|
|
(make-local-variable 'comment-column)
|
1998-07-07 12:11:24 -03:00
|
|
|
|
(make-local-variable 'comment-indent-function)
|
1996-09-04 11:14:10 -03:00
|
|
|
|
(make-local-variable 'indent-region-function)
|
|
|
|
|
(make-local-variable 'indent-line-function)
|
1996-09-04 12:12:42 -03:00
|
|
|
|
(make-local-variable 'add-log-current-defun-function)
|
1996-09-04 11:14:10 -03:00
|
|
|
|
;;
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(set-syntax-table py-mode-syntax-table)
|
1998-07-07 12:11:24 -03:00
|
|
|
|
(setq major-mode 'python-mode
|
|
|
|
|
mode-name "Python"
|
|
|
|
|
local-abbrev-table python-mode-abbrev-table
|
|
|
|
|
font-lock-defaults '(python-font-lock-keywords)
|
|
|
|
|
paragraph-separate "^[ \t]*$"
|
|
|
|
|
paragraph-start "^[ \t]*$"
|
|
|
|
|
require-final-newline t
|
|
|
|
|
comment-start "# "
|
|
|
|
|
comment-end ""
|
|
|
|
|
comment-start-skip "# *"
|
|
|
|
|
comment-column 40
|
|
|
|
|
comment-indent-function 'py-comment-indent-function
|
|
|
|
|
indent-region-function 'py-indent-region
|
|
|
|
|
indent-line-function 'py-indent-line
|
1996-09-04 12:12:42 -03:00
|
|
|
|
;; tell add-log.el how to find the current function/method/variable
|
|
|
|
|
add-log-current-defun-function 'py-current-defun
|
1996-09-03 13:38:30 -03:00
|
|
|
|
)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(use-local-map py-mode-map)
|
1996-07-29 18:05:05 -03:00
|
|
|
|
;; add the menu
|
|
|
|
|
(if py-menu
|
|
|
|
|
(easy-menu-add py-menu))
|
1995-09-14 17:01:14 -03:00
|
|
|
|
;; Emacs 19 requires this
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(if (boundp 'comment-multi-line)
|
1995-09-14 17:01:14 -03:00
|
|
|
|
(setq comment-multi-line nil))
|
1998-10-27 20:10:45 -04:00
|
|
|
|
;; Install Imenu if available
|
1998-03-13 13:29:15 -04:00
|
|
|
|
(when (py-safe (require 'imenu))
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(setq imenu-create-index-function #'py-imenu-create-index-function)
|
|
|
|
|
(setq imenu-generic-expression py-imenu-generic-expression)
|
1998-03-13 13:29:15 -04:00
|
|
|
|
(if (fboundp 'imenu-add-to-menubar)
|
|
|
|
|
(imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
|
|
|
|
|
)
|
|
|
|
|
;; Run the mode hook. Note that py-mode-hook is deprecated.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if python-mode-hook
|
|
|
|
|
(run-hooks 'python-mode-hook)
|
1998-03-13 13:29:15 -04:00
|
|
|
|
(run-hooks 'py-mode-hook))
|
|
|
|
|
;; Now do the automagical guessing
|
|
|
|
|
(if py-smart-indentation
|
|
|
|
|
(let ((offset py-indent-offset))
|
1998-05-19 11:49:49 -03:00
|
|
|
|
;; It's okay if this fails to guess a good value
|
1998-03-13 13:29:15 -04:00
|
|
|
|
(if (and (py-safe (py-guess-indent-offset))
|
|
|
|
|
(<= py-indent-offset 8)
|
|
|
|
|
(>= py-indent-offset 2))
|
|
|
|
|
(setq offset py-indent-offset))
|
|
|
|
|
(setq py-indent-offset offset)
|
1998-03-16 14:12:13 -04:00
|
|
|
|
;; Only turn indent-tabs-mode off if tab-width !=
|
|
|
|
|
;; py-indent-offset. Never turn it on, because the user must
|
|
|
|
|
;; have explicitly turned it off.
|
|
|
|
|
(if (/= tab-width py-indent-offset)
|
|
|
|
|
(setq indent-tabs-mode nil))
|
1999-07-28 18:59:43 -03:00
|
|
|
|
))
|
|
|
|
|
;; Set the default shell if not already set
|
|
|
|
|
(when (null py-which-shell)
|
|
|
|
|
(py-toggle-shells py-default-interpreter))
|
|
|
|
|
)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1995-03-14 11:55:20 -04:00
|
|
|
|
|
|
|
|
|
;; electric characters
|
1995-03-14 18:05:53 -04:00
|
|
|
|
(defun py-outdent-p ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Returns non-nil if the current line should dedent one level."
|
1995-03-14 18:05:53 -04:00
|
|
|
|
(save-excursion
|
|
|
|
|
(and (progn (back-to-indentation)
|
|
|
|
|
(looking-at py-outdent-re))
|
1999-01-09 13:22:38 -04:00
|
|
|
|
;; short circuit infloop on illegal construct
|
|
|
|
|
(not (bobp))
|
1998-01-21 01:36:18 -04:00
|
|
|
|
(progn (forward-line -1)
|
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(back-to-indentation)
|
1995-03-14 18:05:53 -04:00
|
|
|
|
(while (or (looking-at py-blank-or-comment-re)
|
|
|
|
|
(bobp))
|
|
|
|
|
(backward-to-indentation 1))
|
|
|
|
|
(not (looking-at py-no-outdent-re)))
|
|
|
|
|
)))
|
|
|
|
|
|
1995-03-14 11:55:20 -04:00
|
|
|
|
(defun py-electric-colon (arg)
|
|
|
|
|
"Insert a colon.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
In certain cases the line is dedented appropriately. If a numeric
|
|
|
|
|
argument ARG is provided, that many colons are inserted
|
|
|
|
|
non-electrically. Electric behavior is inhibited inside a string or
|
|
|
|
|
comment."
|
1995-03-14 11:55:20 -04:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(self-insert-command (prefix-numeric-value arg))
|
1995-09-14 17:57:02 -03:00
|
|
|
|
;; are we in a string or comment?
|
|
|
|
|
(if (save-excursion
|
|
|
|
|
(let ((pps (parse-partial-sexp (save-excursion
|
1998-05-19 12:31:46 -03:00
|
|
|
|
(py-beginning-of-def-or-class)
|
1995-09-14 17:57:02 -03:00
|
|
|
|
(point))
|
|
|
|
|
(point))))
|
|
|
|
|
(not (or (nth 3 pps) (nth 4 pps)))))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(let ((here (point))
|
|
|
|
|
(outdent 0)
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(indent (py-compute-indentation t)))
|
1995-09-14 17:57:02 -03:00
|
|
|
|
(if (and (not arg)
|
|
|
|
|
(py-outdent-p)
|
|
|
|
|
(= indent (save-excursion
|
1996-08-02 13:22:43 -03:00
|
|
|
|
(py-next-statement -1)
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(py-compute-indentation t)))
|
1995-09-14 17:57:02 -03:00
|
|
|
|
)
|
|
|
|
|
(setq outdent py-indent-offset))
|
1998-08-10 13:33:12 -03:00
|
|
|
|
;; Don't indent, only dedent. This assumes that any lines
|
|
|
|
|
;; that are already dedented relative to
|
|
|
|
|
;; py-compute-indentation were put there on purpose. It's
|
|
|
|
|
;; highly annoying to have `:' indent for you. Use TAB, C-c
|
|
|
|
|
;; C-l or C-c C-r to adjust. TBD: Is there a better way to
|
|
|
|
|
;; determine this???
|
1995-09-14 17:57:02 -03:00
|
|
|
|
(if (< (current-indentation) indent) nil
|
|
|
|
|
(goto-char here)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(indent-to (- indent outdent))
|
|
|
|
|
)))))
|
1995-03-14 11:55:20 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
1997-11-04 14:47:06 -04:00
|
|
|
|
;; Python subprocess utilities and filters
|
|
|
|
|
(defun py-execute-file (proc filename)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Send to Python interpreter process PROC \"execfile('FILENAME')\".
|
|
|
|
|
Make that process's buffer visible and force display. Also make
|
|
|
|
|
comint believe the user typed this string so that
|
|
|
|
|
`kill-output-from-shell' does The Right Thing."
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(let ((curbuf (current-buffer))
|
|
|
|
|
(procbuf (process-buffer proc))
|
(py-process-filter): Deleted this function. In order to fix
interactions with newer Emacsen, I've rewritten the way all the
process filters work in the *Python* buffer. We use more of the
comint infrastructure, specifically the default process filter. This
means that scrolling is now handled by the default comint variables
including comint-scroll-to-bottom-on-output. Note that this is
somewhat experimental change!
(py-comint-output-filter-function): Moved to here from the obsolete
py-process-filter function, the logic to pop and exec the next queued
file waiting to be executed.
(py-execute-file): Don't bind comint-scroll-to-bottom-on-output to t,
and save the excursion when inserting the "working on" message. This
lets the standard comint scrolling variables as set by the user,
continue to work.
(python-mode, py-shell, py-describe-mode): Remove description of
py-scroll-process-buffer. Also in py-shell, make
comint-output-filter-functions buffer-local, and add
py-comint-output-filter-function to this hook (instead of setting the
process filter).
(py-scroll-process-buffer): Deleted this variable. See comint
variables including comint-scroll-to-bottom-on-output.
(py-execute-region): When exec files are being queued, push the next
temp file on the end of the list.
(py-submit-bug-report): Removed reporting of py-scroll-process-buffer.
1998-08-20 16:44:51 -03:00
|
|
|
|
; (comint-scroll-to-bottom-on-output t)
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(msg (format "## working on region in file %s...\n" filename))
|
1998-09-24 20:48:40 -03:00
|
|
|
|
(cmd (format "execfile(r'%s')\n" filename)))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(unwind-protect
|
(py-process-filter): Deleted this function. In order to fix
interactions with newer Emacsen, I've rewritten the way all the
process filters work in the *Python* buffer. We use more of the
comint infrastructure, specifically the default process filter. This
means that scrolling is now handled by the default comint variables
including comint-scroll-to-bottom-on-output. Note that this is
somewhat experimental change!
(py-comint-output-filter-function): Moved to here from the obsolete
py-process-filter function, the logic to pop and exec the next queued
file waiting to be executed.
(py-execute-file): Don't bind comint-scroll-to-bottom-on-output to t,
and save the excursion when inserting the "working on" message. This
lets the standard comint scrolling variables as set by the user,
continue to work.
(python-mode, py-shell, py-describe-mode): Remove description of
py-scroll-process-buffer. Also in py-shell, make
comint-output-filter-functions buffer-local, and add
py-comint-output-filter-function to this hook (instead of setting the
process filter).
(py-scroll-process-buffer): Deleted this variable. See comint
variables including comint-scroll-to-bottom-on-output.
(py-execute-region): When exec files are being queued, push the next
temp file on the end of the list.
(py-submit-bug-report): Removed reporting of py-scroll-process-buffer.
1998-08-20 16:44:51 -03:00
|
|
|
|
(save-excursion
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(set-buffer procbuf)
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(move-marker (process-mark proc) (point))
|
|
|
|
|
(funcall (process-filter proc) proc msg))
|
|
|
|
|
(set-buffer curbuf))
|
|
|
|
|
(process-send-string proc cmd)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
(py-process-filter): Deleted this function. In order to fix
interactions with newer Emacsen, I've rewritten the way all the
process filters work in the *Python* buffer. We use more of the
comint infrastructure, specifically the default process filter. This
means that scrolling is now handled by the default comint variables
including comint-scroll-to-bottom-on-output. Note that this is
somewhat experimental change!
(py-comint-output-filter-function): Moved to here from the obsolete
py-process-filter function, the logic to pop and exec the next queued
file waiting to be executed.
(py-execute-file): Don't bind comint-scroll-to-bottom-on-output to t,
and save the excursion when inserting the "working on" message. This
lets the standard comint scrolling variables as set by the user,
continue to work.
(python-mode, py-shell, py-describe-mode): Remove description of
py-scroll-process-buffer. Also in py-shell, make
comint-output-filter-functions buffer-local, and add
py-comint-output-filter-function to this hook (instead of setting the
process filter).
(py-scroll-process-buffer): Deleted this variable. See comint
variables including comint-scroll-to-bottom-on-output.
(py-execute-region): When exec files are being queued, push the next
temp file on the end of the list.
(py-submit-bug-report): Removed reporting of py-scroll-process-buffer.
1998-08-20 16:44:51 -03:00
|
|
|
|
(defun py-comint-output-filter-function (string)
|
|
|
|
|
"Watch output for Python prompt and exec next file waiting in queue.
|
|
|
|
|
This function is appropriate for `comint-output-filter-functions'."
|
1998-11-17 15:24:47 -04:00
|
|
|
|
;; TBD: this should probably use split-string
|
1998-09-25 16:40:10 -03:00
|
|
|
|
(when (and (or (string-equal string ">>> ")
|
|
|
|
|
(and (>= (length string) 5)
|
|
|
|
|
(string-equal (substring string -5) "\n>>> ")))
|
(py-process-filter): Deleted this function. In order to fix
interactions with newer Emacsen, I've rewritten the way all the
process filters work in the *Python* buffer. We use more of the
comint infrastructure, specifically the default process filter. This
means that scrolling is now handled by the default comint variables
including comint-scroll-to-bottom-on-output. Note that this is
somewhat experimental change!
(py-comint-output-filter-function): Moved to here from the obsolete
py-process-filter function, the logic to pop and exec the next queued
file waiting to be executed.
(py-execute-file): Don't bind comint-scroll-to-bottom-on-output to t,
and save the excursion when inserting the "working on" message. This
lets the standard comint scrolling variables as set by the user,
continue to work.
(python-mode, py-shell, py-describe-mode): Remove description of
py-scroll-process-buffer. Also in py-shell, make
comint-output-filter-functions buffer-local, and add
py-comint-output-filter-function to this hook (instead of setting the
process filter).
(py-scroll-process-buffer): Deleted this variable. See comint
variables including comint-scroll-to-bottom-on-output.
(py-execute-region): When exec files are being queued, push the next
temp file on the end of the list.
(py-submit-bug-report): Removed reporting of py-scroll-process-buffer.
1998-08-20 16:44:51 -03:00
|
|
|
|
py-file-queue)
|
|
|
|
|
(py-safe (delete-file (car py-file-queue)))
|
|
|
|
|
(setq py-file-queue (cdr py-file-queue))
|
|
|
|
|
(if py-file-queue
|
|
|
|
|
(let ((pyproc (get-buffer-process (current-buffer))))
|
|
|
|
|
(py-execute-file pyproc (car py-file-queue))))
|
|
|
|
|
))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(defun py-pdbtrack-overlay-arrow (activation)
|
|
|
|
|
"Activate or de arrow at beginning-of-line in current buffer."
|
|
|
|
|
;; This was derived/simplified from edebug-overlay-arrow
|
|
|
|
|
(cond (activation
|
|
|
|
|
(setq overlay-arrow-position (make-marker))
|
|
|
|
|
(setq overlay-arrow-string "=>")
|
|
|
|
|
(set-marker overlay-arrow-position (py-point 'bol) (current-buffer))
|
|
|
|
|
(setq py-pdbtrack-is-tracking-p t))
|
|
|
|
|
(overlay-arrow-position
|
|
|
|
|
(setq overlay-arrow-position nil)
|
|
|
|
|
(setq py-pdbtrack-is-tracking-p nil))
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
(defun py-pdbtrack-track-stack-file (text)
|
|
|
|
|
"Show the file indicated by the pdb stack entry line, in a separate window.
|
|
|
|
|
|
|
|
|
|
Activity is disabled if the buffer-local variable
|
|
|
|
|
`py-pdbtrack-do-tracking-p' is nil.
|
|
|
|
|
|
|
|
|
|
We depend on the pdb input prompt matching `py-pdbtrack-input-prompt'
|
|
|
|
|
at the beginning of the line."
|
|
|
|
|
;; Instead of trying to piece things together from partial text
|
|
|
|
|
;; (which can be almost useless depending on Emacs version), we
|
|
|
|
|
;; monitor to the point where we have the next pdb prompt, and then
|
|
|
|
|
;; check all text from comint-last-input-end to process-mark.
|
|
|
|
|
;;
|
|
|
|
|
;; KLM: It might be nice to provide an optional override, so this
|
|
|
|
|
;; routine could be fed debugger output strings as the text
|
|
|
|
|
;; argument, for deliberate application elsewhere.
|
|
|
|
|
;;
|
|
|
|
|
;; KLM: We're very conservative about clearing the overlay arrow, to
|
|
|
|
|
;; minimize residue. This means, for instance, that executing other
|
|
|
|
|
;; pdb commands wipes out the highlight.
|
|
|
|
|
(let* ((origbuf (current-buffer))
|
|
|
|
|
(currproc (get-buffer-process origbuf)))
|
|
|
|
|
(if (not (and currproc py-pdbtrack-do-tracking-p))
|
|
|
|
|
(py-pdbtrack-overlay-arrow nil)
|
|
|
|
|
(let* (;(origdir default-directory)
|
|
|
|
|
(procmark (process-mark currproc))
|
|
|
|
|
(block (buffer-substring (max comint-last-input-end
|
|
|
|
|
(- procmark
|
|
|
|
|
py-pdbtrack-track-range))
|
|
|
|
|
procmark))
|
|
|
|
|
fname lineno)
|
|
|
|
|
(if (not (string-match (concat py-pdbtrack-input-prompt "$") block))
|
|
|
|
|
(py-pdbtrack-overlay-arrow nil)
|
|
|
|
|
(if (not (string-match
|
|
|
|
|
(concat ".*" py-pdbtrack-stack-entry-regexp ".*")
|
|
|
|
|
block))
|
|
|
|
|
(py-pdbtrack-overlay-arrow nil)
|
|
|
|
|
(setq fname (match-string 1 block)
|
|
|
|
|
lineno (match-string 2 block))
|
|
|
|
|
(if (file-exists-p fname)
|
|
|
|
|
(progn
|
|
|
|
|
(find-file-other-window fname)
|
|
|
|
|
(goto-line (string-to-int lineno))
|
2001-04-11 19:27:41 -03:00
|
|
|
|
(message "pdbtrack: line %s, file %s" lineno fname)
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(py-pdbtrack-overlay-arrow t)
|
|
|
|
|
(pop-to-buffer origbuf t) )
|
|
|
|
|
(if (= (elt fname 0) ?\<)
|
2001-04-11 19:27:41 -03:00
|
|
|
|
(message "pdbtrack: (Non-file source: '%s')" fname)
|
|
|
|
|
(message "pdbtrack: File not found: %s" fname))
|
2001-04-11 17:23:17 -03:00
|
|
|
|
)))))))
|
|
|
|
|
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(defun py-postprocess-output-buffer (buf)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Highlight exceptions found in BUF.
|
|
|
|
|
If an exception occurred return t, otherwise return nil. BUF must exist."
|
1998-03-26 12:08:59 -04:00
|
|
|
|
(let (line file bol err-p)
|
1997-12-03 01:25:48 -04:00
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer buf)
|
|
|
|
|
(beginning-of-buffer)
|
|
|
|
|
(while (re-search-forward py-traceback-line-re nil t)
|
|
|
|
|
(setq file (match-string 1)
|
|
|
|
|
line (string-to-int (match-string 2))
|
|
|
|
|
bol (py-point 'bol))
|
1998-03-26 12:08:59 -04:00
|
|
|
|
(py-highlight-line bol (py-point 'eol) file line)))
|
|
|
|
|
(when (and py-jump-on-exception line)
|
|
|
|
|
(beep)
|
|
|
|
|
(py-jump-to-exception file line)
|
|
|
|
|
(setq err-p t))
|
|
|
|
|
err-p))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
|
1997-12-03 01:25:48 -04:00
|
|
|
|
|
1997-11-04 14:47:06 -04:00
|
|
|
|
|
|
|
|
|
;;; Subprocess commands
|
|
|
|
|
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
;; only used when (memq 'broken-temp-names py-emacs-features)
|
|
|
|
|
(defvar py-serial-number 0)
|
|
|
|
|
(defvar py-exception-buffer nil)
|
|
|
|
|
(defconst py-output-buffer "*Python Output*")
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(make-variable-buffer-local 'py-output-buffer)
|
|
|
|
|
|
|
|
|
|
;; for toggling between CPython and JPython
|
1999-02-16 19:36:16 -04:00
|
|
|
|
(defvar py-which-shell nil)
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(defvar py-which-args py-python-command-args)
|
|
|
|
|
(defvar py-which-bufname "Python")
|
|
|
|
|
(make-variable-buffer-local 'py-which-shell)
|
|
|
|
|
(make-variable-buffer-local 'py-which-args)
|
|
|
|
|
(make-variable-buffer-local 'py-which-bufname)
|
|
|
|
|
|
|
|
|
|
(defun py-toggle-shells (arg)
|
|
|
|
|
"Toggles between the CPython and JPython shells.
|
1999-07-28 18:59:43 -03:00
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
With positive argument ARG (interactively \\[universal-argument]),
|
|
|
|
|
uses the CPython shell, with negative ARG uses the JPython shell, and
|
1999-07-28 18:59:43 -03:00
|
|
|
|
with a zero argument, toggles the shell.
|
|
|
|
|
|
|
|
|
|
Programmatically, ARG can also be one of the symbols `cpython' or
|
|
|
|
|
`jpython', equivalent to positive arg and negative arg respectively."
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(interactive "P")
|
|
|
|
|
;; default is to toggle
|
|
|
|
|
(if (null arg)
|
|
|
|
|
(setq arg 0))
|
1999-07-28 18:59:43 -03:00
|
|
|
|
;; preprocess arg
|
|
|
|
|
(cond
|
|
|
|
|
((equal arg 0)
|
|
|
|
|
;; toggle
|
|
|
|
|
(if (string-equal py-which-bufname "Python")
|
|
|
|
|
(setq arg -1)
|
|
|
|
|
(setq arg 1)))
|
|
|
|
|
((equal arg 'cpython) (setq arg 1))
|
|
|
|
|
((equal arg 'jpython) (setq arg -1)))
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(let (msg)
|
|
|
|
|
(cond
|
|
|
|
|
((< 0 arg)
|
|
|
|
|
;; set to CPython
|
|
|
|
|
(setq py-which-shell py-python-command
|
|
|
|
|
py-which-args py-python-command-args
|
|
|
|
|
py-which-bufname "Python"
|
|
|
|
|
msg "CPython"
|
|
|
|
|
mode-name "Python"))
|
|
|
|
|
((> 0 arg)
|
|
|
|
|
(setq py-which-shell py-jpython-command
|
|
|
|
|
py-which-args py-jpython-command-args
|
|
|
|
|
py-which-bufname "JPython"
|
|
|
|
|
msg "JPython"
|
|
|
|
|
mode-name "JPython"))
|
|
|
|
|
)
|
1998-04-10 13:08:26 -03:00
|
|
|
|
(message "Using the %s shell" msg)
|
1998-04-09 20:28:20 -03:00
|
|
|
|
(setq py-output-buffer (format "*%s Output*" py-which-bufname))))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
|
1997-11-04 14:47:06 -04:00
|
|
|
|
;;;###autoload
|
1999-02-16 19:52:46 -04:00
|
|
|
|
(defun py-shell (&optional argprompt)
|
1997-11-04 14:47:06 -04:00
|
|
|
|
"Start an interactive Python interpreter in another window.
|
|
|
|
|
This is like Shell mode, except that Python is running in the window
|
|
|
|
|
instead of a shell. See the `Interactive Shell' and `Shell Mode'
|
|
|
|
|
sections of the Emacs manual for details, especially for the key
|
|
|
|
|
bindings active in the `*Python*' buffer.
|
|
|
|
|
|
1999-02-16 19:52:46 -04:00
|
|
|
|
With optional \\[universal-argument], the user is prompted for the
|
|
|
|
|
flags to pass to the Python interpreter. This has no effect when this
|
|
|
|
|
command is used to switch to an existing process, only when a new
|
|
|
|
|
process is started. If you use this, you will probably want to ensure
|
|
|
|
|
that the current arguments are retained (they will be included in the
|
|
|
|
|
prompt). This argument is ignored when this function is called
|
|
|
|
|
programmatically, or when running in Emacs 19.34 or older.
|
|
|
|
|
|
1998-04-09 20:28:20 -03:00
|
|
|
|
Note: You can toggle between using the CPython interpreter and the
|
|
|
|
|
JPython interpreter by hitting \\[py-toggle-shells]. This toggles
|
|
|
|
|
buffer local variables which control whether all your subshell
|
|
|
|
|
interactions happen to the `*JPython*' or `*Python*' buffers (the
|
|
|
|
|
latter is the name used for the CPython buffer).
|
|
|
|
|
|
1997-11-04 14:47:06 -04:00
|
|
|
|
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 `>>> ' or `... ' at the start of a line. `python-mode' can't
|
|
|
|
|
distinguish your output from Python's output, and assumes that `>>> '
|
|
|
|
|
at the start of a line is a prompt from Python. Similarly, the Emacs
|
|
|
|
|
Shell mode code assumes that both `>>> ' and `... ' at the start of a
|
|
|
|
|
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
|
|
|
|
|
buffer is accepting output from Python, do NOT attempt to `undo' the
|
|
|
|
|
changes. Some of the output (nowhere near the parts you changed!) may
|
|
|
|
|
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
|
|
|
|
|
non-Python process buffers using the default (Emacs-supplied) process
|
|
|
|
|
filter."
|
1999-02-16 19:52:46 -04:00
|
|
|
|
(interactive "P")
|
1999-08-10 18:49:00 -03:00
|
|
|
|
;; Set the default shell if not already set
|
|
|
|
|
(when (null py-which-shell)
|
|
|
|
|
(py-toggle-shells py-default-interpreter))
|
1999-02-16 19:52:46 -04:00
|
|
|
|
(let ((args py-which-args))
|
|
|
|
|
(when (and argprompt
|
|
|
|
|
(interactive-p)
|
|
|
|
|
(fboundp 'split-string))
|
|
|
|
|
;; TBD: Perhaps force "-i" in the final list?
|
|
|
|
|
(setq args (split-string
|
|
|
|
|
(read-string (concat py-which-bufname
|
|
|
|
|
" arguments: ")
|
|
|
|
|
(concat
|
|
|
|
|
(mapconcat 'identity py-which-args " ") " ")
|
|
|
|
|
))))
|
|
|
|
|
(switch-to-buffer-other-window
|
|
|
|
|
(apply 'make-comint py-which-bufname py-which-shell nil args))
|
|
|
|
|
(make-local-variable 'comint-prompt-regexp)
|
|
|
|
|
(setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ")
|
|
|
|
|
(add-hook 'comint-output-filter-functions
|
|
|
|
|
'py-comint-output-filter-function)
|
2001-04-11 17:23:17 -03:00
|
|
|
|
;; pdbtrack
|
|
|
|
|
(add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
|
|
|
|
|
(setq py-pdbtrack-do-tracking-p t)
|
1999-02-16 19:52:46 -04:00
|
|
|
|
(set-syntax-table py-mode-syntax-table)
|
|
|
|
|
(use-local-map py-shell-map)
|
|
|
|
|
))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
|
|
|
|
|
(defun py-clear-queue ()
|
|
|
|
|
"Clear the queue of temporary files waiting to execute."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((n (length py-file-queue)))
|
|
|
|
|
(mapcar 'delete-file py-file-queue)
|
|
|
|
|
(setq py-file-queue nil)
|
|
|
|
|
(message "%d pending files de-queued." n)))
|
|
|
|
|
|
1998-05-19 12:54:45 -03:00
|
|
|
|
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(defun py-execute-region (start end &optional async)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Execute the region in a Python interpreter.
|
|
|
|
|
|
1997-11-04 14:47:06 -04:00
|
|
|
|
The region is first copied into a temporary file (in the directory
|
|
|
|
|
`py-temp-directory'). If there is no Python interpreter shell
|
|
|
|
|
running, this file is executed synchronously using
|
1998-08-10 13:33:12 -03:00
|
|
|
|
`shell-command-on-region'. If the program is long running, use
|
|
|
|
|
\\[universal-argument] to run the command asynchronously in its own
|
|
|
|
|
buffer.
|
|
|
|
|
|
|
|
|
|
When this function is used programmatically, arguments START and END
|
|
|
|
|
specify the region to execute, and optional third argument ASYNC, if
|
|
|
|
|
non-nil, specifies to run the command asynchronously in its own
|
|
|
|
|
buffer.
|
1997-11-04 14:47:06 -04:00
|
|
|
|
|
|
|
|
|
If the Python interpreter shell is running, the region is execfile()'d
|
|
|
|
|
in that shell. If you try to execute regions too quickly,
|
|
|
|
|
`python-mode' will queue them up and execute them one at a time when
|
|
|
|
|
it sees a `>>> ' prompt from Python. Each time this happens, the
|
|
|
|
|
process buffer is popped into a window (if it's not already in some
|
|
|
|
|
window) so you can see it, and a comment of the form
|
|
|
|
|
|
|
|
|
|
\t## working on region in file <name>...
|
|
|
|
|
|
|
|
|
|
is inserted at the end. See also the command `py-clear-queue'."
|
|
|
|
|
(interactive "r\nP")
|
|
|
|
|
(or (< start end)
|
|
|
|
|
(error "Region is empty"))
|
1998-11-17 15:24:47 -04:00
|
|
|
|
(let* ((proc (get-process py-which-bufname))
|
1997-11-05 14:41:11 -04:00
|
|
|
|
(temp (if (memq 'broken-temp-names py-emacs-features)
|
1998-08-07 19:24:16 -03:00
|
|
|
|
(let
|
|
|
|
|
((sn py-serial-number)
|
|
|
|
|
(pid (and (fboundp 'emacs-pid) (emacs-pid))))
|
|
|
|
|
(setq py-serial-number (1+ py-serial-number))
|
|
|
|
|
(if pid
|
|
|
|
|
(format "python-%d-%d" sn pid)
|
|
|
|
|
(format "python-%d" sn)))
|
1998-02-25 12:45:43 -04:00
|
|
|
|
(make-temp-name "python-")))
|
2000-05-23 02:47:43 -03:00
|
|
|
|
(file (expand-file-name temp py-temp-directory))
|
|
|
|
|
(cur (current-buffer))
|
|
|
|
|
(buf (get-buffer-create file)))
|
|
|
|
|
;; Write the contents of the buffer, watching out for indented regions.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char start)
|
2000-06-23 17:24:25 -03:00
|
|
|
|
(let ((needs-if (/= (py-point 'bol) (py-point 'boi))))
|
2000-05-23 02:47:43 -03:00
|
|
|
|
(set-buffer buf)
|
2000-06-23 17:24:25 -03:00
|
|
|
|
(when needs-if
|
|
|
|
|
(insert "if 1:\n"))
|
|
|
|
|
(insert-buffer-substring cur start end)))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(cond
|
1998-05-19 11:49:49 -03:00
|
|
|
|
;; always run the code in its own asynchronous subprocess
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(async
|
2000-05-23 02:47:43 -03:00
|
|
|
|
;; User explicitly wants this to run in its own async subprocess
|
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer buf)
|
|
|
|
|
(write-region (point-min) (point-max) file nil 'nomsg))
|
1998-11-19 23:04:07 -04:00
|
|
|
|
(let* ((buf (generate-new-buffer-name py-output-buffer))
|
|
|
|
|
;; TBD: a horrible hack, but why create new Custom variables?
|
|
|
|
|
(arg (if (string-equal py-which-bufname "Python")
|
|
|
|
|
"-u" "")))
|
|
|
|
|
(start-process py-which-bufname buf py-which-shell arg file)
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(pop-to-buffer buf)
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(py-postprocess-output-buffer buf)
|
2000-05-23 02:47:43 -03:00
|
|
|
|
;; TBD: clean up the temporary file!
|
1997-11-04 14:47:06 -04:00
|
|
|
|
))
|
|
|
|
|
;; if the Python interpreter shell is running, queue it up for
|
|
|
|
|
;; execution there.
|
|
|
|
|
(proc
|
|
|
|
|
;; use the existing python shell
|
2000-05-23 02:47:43 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer buf)
|
|
|
|
|
(write-region (point-min) (point-max) file nil 'nomsg))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(if (not py-file-queue)
|
|
|
|
|
(py-execute-file proc file)
|
|
|
|
|
(message "File %s queued for execution" file))
|
(py-process-filter): Deleted this function. In order to fix
interactions with newer Emacsen, I've rewritten the way all the
process filters work in the *Python* buffer. We use more of the
comint infrastructure, specifically the default process filter. This
means that scrolling is now handled by the default comint variables
including comint-scroll-to-bottom-on-output. Note that this is
somewhat experimental change!
(py-comint-output-filter-function): Moved to here from the obsolete
py-process-filter function, the logic to pop and exec the next queued
file waiting to be executed.
(py-execute-file): Don't bind comint-scroll-to-bottom-on-output to t,
and save the excursion when inserting the "working on" message. This
lets the standard comint scrolling variables as set by the user,
continue to work.
(python-mode, py-shell, py-describe-mode): Remove description of
py-scroll-process-buffer. Also in py-shell, make
comint-output-filter-functions buffer-local, and add
py-comint-output-filter-function to this hook (instead of setting the
process filter).
(py-scroll-process-buffer): Deleted this variable. See comint
variables including comint-scroll-to-bottom-on-output.
(py-execute-region): When exec files are being queued, push the next
temp file on the end of the list.
(py-submit-bug-report): Removed reporting of py-scroll-process-buffer.
1998-08-20 16:44:51 -03:00
|
|
|
|
(setq py-file-queue (append py-file-queue (list file)))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(setq py-exception-buffer (cons file (current-buffer))))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(t
|
1998-11-19 23:04:07 -04:00
|
|
|
|
;; TBD: a horrible hack, buy why create new Custom variables?
|
|
|
|
|
(let ((cmd (concat py-which-shell
|
|
|
|
|
(if (string-equal py-which-bufname "JPython")
|
|
|
|
|
" -" ""))))
|
|
|
|
|
;; otherwise either run it synchronously in a subprocess
|
2000-05-23 02:47:43 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer buf)
|
|
|
|
|
(shell-command-on-region (point-min) (point-max)
|
|
|
|
|
cmd py-output-buffer))
|
1998-11-19 23:04:07 -04:00
|
|
|
|
;; shell-command-on-region kills the output buffer if it never
|
|
|
|
|
;; existed and there's no output from the command
|
|
|
|
|
(if (not (get-buffer py-output-buffer))
|
|
|
|
|
(message "No output.")
|
|
|
|
|
(setq py-exception-buffer (current-buffer))
|
|
|
|
|
(let ((err-p (py-postprocess-output-buffer py-output-buffer)))
|
|
|
|
|
(pop-to-buffer py-output-buffer)
|
|
|
|
|
(if err-p
|
|
|
|
|
(pop-to-buffer py-exception-buffer)))
|
2000-05-23 02:47:43 -03:00
|
|
|
|
))
|
|
|
|
|
;; TBD: delete the buffer
|
|
|
|
|
)
|
2001-02-20 19:07:56 -04:00
|
|
|
|
)
|
|
|
|
|
;; Clean up after ourselves.
|
|
|
|
|
(kill-buffer buf)))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
|
1998-05-19 12:54:45 -03:00
|
|
|
|
|
|
|
|
|
;; Code execution commands
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(defun py-execute-buffer (&optional async)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
"Send the contents of the buffer to a Python interpreter.
|
1998-01-20 17:43:34 -04:00
|
|
|
|
If the file local variable `py-master-file' is non-nil, execute the
|
|
|
|
|
named file instead of the buffer's file.
|
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
If there is a *Python* process buffer it is used. If a clipping
|
|
|
|
|
restriction is in effect, only the accessible portion of the buffer is
|
|
|
|
|
sent. A trailing newline will be supplied if needed.
|
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
See the `\\[py-execute-region]' docs for an account of some
|
|
|
|
|
subtleties, including the use of the optional ASYNC argument."
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(interactive "P")
|
1998-01-20 17:43:34 -04:00
|
|
|
|
(if py-master-file
|
|
|
|
|
(let* ((filename (expand-file-name py-master-file))
|
|
|
|
|
(buffer (or (get-file-buffer filename)
|
|
|
|
|
(find-file-noselect filename))))
|
|
|
|
|
(set-buffer buffer)))
|
1997-11-04 14:47:06 -04:00
|
|
|
|
(py-execute-region (point-min) (point-max) async))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(defun py-execute-import-or-reload (&optional async)
|
|
|
|
|
"Import the current buffer's file in a Python interpreter.
|
|
|
|
|
|
|
|
|
|
If the file has already been imported, then do reload instead to get
|
1998-07-07 14:45:38 -03:00
|
|
|
|
the latest version.
|
|
|
|
|
|
|
|
|
|
If the file's name does not end in \".py\", then do execfile instead.
|
|
|
|
|
|
|
|
|
|
If the current buffer is not visiting a file, do `py-execute-buffer'
|
|
|
|
|
instead.
|
|
|
|
|
|
|
|
|
|
If the file local variable `py-master-file' is non-nil, import or
|
|
|
|
|
reload the named file instead of the buffer's file. The file may be
|
|
|
|
|
saved based on the value of `py-execute-import-or-reload-save-p'.
|
1998-05-19 12:54:45 -03:00
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
See the `\\[py-execute-region]' docs for an account of some
|
|
|
|
|
subtleties, including the use of the optional ASYNC argument.
|
1998-05-19 12:54:45 -03:00
|
|
|
|
|
1998-07-07 14:45:38 -03:00
|
|
|
|
This may be preferable to `\\[py-execute-buffer]' because:
|
1998-05-19 12:54:45 -03:00
|
|
|
|
|
|
|
|
|
- Definitions stay in their module rather than appearing at top
|
|
|
|
|
level, where they would clutter the global namespace and not affect
|
|
|
|
|
uses of qualified names (MODULE.NAME).
|
|
|
|
|
|
|
|
|
|
- The Python debugger gets line number information about the functions."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
;; Check file local variable py-master-file
|
|
|
|
|
(if py-master-file
|
|
|
|
|
(let* ((filename (expand-file-name py-master-file))
|
|
|
|
|
(buffer (or (get-file-buffer filename)
|
|
|
|
|
(find-file-noselect filename))))
|
|
|
|
|
(set-buffer buffer)))
|
|
|
|
|
(let ((file (buffer-file-name (current-buffer))))
|
|
|
|
|
(if file
|
|
|
|
|
(progn
|
1998-05-19 13:25:04 -03:00
|
|
|
|
;; Maybe save some buffers
|
|
|
|
|
(save-some-buffers (not py-ask-about-save) nil)
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(py-execute-string
|
|
|
|
|
(if (string-match "\\.py$" file)
|
|
|
|
|
(let ((f (file-name-sans-extension
|
|
|
|
|
(file-name-nondirectory file))))
|
|
|
|
|
(format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
|
|
|
|
|
f f f))
|
1998-09-24 20:48:40 -03:00
|
|
|
|
(format "execfile(r'%s')\n" file))
|
1998-05-19 12:54:45 -03:00
|
|
|
|
async))
|
|
|
|
|
;; else
|
|
|
|
|
(py-execute-buffer async))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun py-execute-def-or-class (&optional async)
|
|
|
|
|
"Send the current function or class definition to a Python interpreter.
|
|
|
|
|
|
|
|
|
|
If there is a *Python* process buffer it is used.
|
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
See the `\\[py-execute-region]' docs for an account of some
|
|
|
|
|
subtleties, including the use of the optional ASYNC argument."
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(py-mark-def-or-class)
|
|
|
|
|
;; mark is before point
|
|
|
|
|
(py-execute-region (mark) (point) async)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun py-execute-string (string &optional async)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Send the argument STRING to a Python interpreter.
|
1998-05-19 12:54:45 -03:00
|
|
|
|
|
|
|
|
|
If there is a *Python* process buffer it is used.
|
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
See the `\\[py-execute-region]' docs for an account of some
|
|
|
|
|
subtleties, including the use of the optional ASYNC argument."
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(interactive "sExecute Python command: ")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer (get-buffer-create
|
|
|
|
|
(generate-new-buffer-name " *Python Command*")))
|
|
|
|
|
(insert string)
|
|
|
|
|
(py-execute-region (point-min) (point-max) async)))
|
|
|
|
|
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun py-jump-to-exception (file line)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Jump to the Python code in FILE at LINE."
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(let ((buffer (cond ((string-equal file "<stdin>")
|
1998-05-19 12:01:06 -03:00
|
|
|
|
(if (consp py-exception-buffer)
|
|
|
|
|
(cdr py-exception-buffer)
|
|
|
|
|
py-exception-buffer))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
((and (consp py-exception-buffer)
|
|
|
|
|
(string-equal file (car py-exception-buffer)))
|
|
|
|
|
(cdr py-exception-buffer))
|
|
|
|
|
((py-safe (find-file-noselect file)))
|
|
|
|
|
;; could not figure out what file the exception
|
|
|
|
|
;; is pointing to, so prompt for it
|
|
|
|
|
(t (find-file (read-file-name "Exception file: "
|
|
|
|
|
nil
|
|
|
|
|
file t))))))
|
|
|
|
|
(pop-to-buffer buffer)
|
1998-05-19 12:01:06 -03:00
|
|
|
|
;; Force Python mode
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(if (not (eq major-mode 'python-mode))
|
1998-05-19 12:01:06 -03:00
|
|
|
|
(python-mode))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(goto-line line)
|
|
|
|
|
(message "Jumping to exception in file %s on line %d" file line)))
|
|
|
|
|
|
|
|
|
|
(defun py-mouseto-exception (event)
|
1998-08-10 18:44:37 -03:00
|
|
|
|
"Jump to the code which caused the Python exception at EVENT.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
EVENT is usually a mouse click."
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(interactive "e")
|
|
|
|
|
(cond
|
|
|
|
|
((fboundp 'event-point)
|
|
|
|
|
;; XEmacs
|
|
|
|
|
(let* ((point (event-point event))
|
|
|
|
|
(buffer (event-buffer event))
|
|
|
|
|
(e (and point buffer (extent-at point buffer 'py-exc-info)))
|
|
|
|
|
(info (and e (extent-property e 'py-exc-info))))
|
|
|
|
|
(message "Event point: %d, info: %s" point info)
|
|
|
|
|
(and info
|
|
|
|
|
(py-jump-to-exception (car info) (cdr info)))
|
|
|
|
|
))
|
|
|
|
|
;; Emacs -- Please port this!
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
(defun py-goto-exception ()
|
|
|
|
|
"Go to the line indicated by the traceback."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let (file line)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(if (looking-at py-traceback-line-re)
|
|
|
|
|
(setq file (match-string 1)
|
|
|
|
|
line (string-to-int (match-string 2)))))
|
|
|
|
|
(if (not file)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
(error "Not on a traceback line"))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(py-jump-to-exception file line)))
|
|
|
|
|
|
|
|
|
|
(defun py-find-next-exception (start buffer searchdir errwhere)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Find the next Python exception and jump to the code that caused it.
|
|
|
|
|
START is the buffer position in BUFFER from which to begin searching
|
|
|
|
|
for an exception. SEARCHDIR is a function, either
|
|
|
|
|
`re-search-backward' or `re-search-forward' indicating the direction
|
|
|
|
|
to search. ERRWHERE is used in an error message if the limit (top or
|
|
|
|
|
bottom) of the trackback stack is encountered."
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(let (file line)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer buffer)
|
|
|
|
|
(goto-char (py-point start))
|
|
|
|
|
(if (funcall searchdir py-traceback-line-re nil t)
|
|
|
|
|
(setq file (match-string 1)
|
|
|
|
|
line (string-to-int (match-string 2)))))
|
|
|
|
|
(if (and file line)
|
|
|
|
|
(py-jump-to-exception file line)
|
|
|
|
|
(error "%s of traceback" errwhere))))
|
|
|
|
|
|
|
|
|
|
(defun py-down-exception (&optional bottom)
|
|
|
|
|
"Go to the next line down in the traceback.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
With \\[univeral-argument] (programmatically, optional argument
|
|
|
|
|
BOTTOM), jump to the bottom (innermost) exception in the exception
|
|
|
|
|
stack."
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(let* ((proc (get-process "Python"))
|
|
|
|
|
(buffer (if proc "*Python*" py-output-buffer)))
|
|
|
|
|
(if bottom
|
|
|
|
|
(py-find-next-exception 'eob buffer 're-search-backward "Bottom")
|
|
|
|
|
(py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
|
|
|
|
|
|
|
|
|
|
(defun py-up-exception (&optional top)
|
|
|
|
|
"Go to the previous line up in the traceback.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
With \\[universal-argument] (programmatically, optional argument TOP)
|
|
|
|
|
jump to the top (outermost) exception in the exception stack."
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(let* ((proc (get-process "Python"))
|
|
|
|
|
(buffer (if proc "*Python*" py-output-buffer)))
|
|
|
|
|
(if top
|
|
|
|
|
(py-find-next-exception 'bob buffer 're-search-forward "Top")
|
1997-11-27 16:08:14 -04:00
|
|
|
|
(py-find-next-exception 'bol buffer 're-search-backward "Top"))))
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
1997-11-04 15:21:50 -04:00
|
|
|
|
;; Electric deletion
|
|
|
|
|
(defun py-electric-backspace (arg)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Delete preceding character or levels of indentation.
|
1997-11-04 15:21:50 -04:00
|
|
|
|
Deletion is performed by calling the function in `py-backspace-function'
|
|
|
|
|
with a single argument (the number of characters to delete).
|
1996-12-17 18:05:07 -04:00
|
|
|
|
|
1998-08-10 13:33:12 -03:00
|
|
|
|
If point is at the leftmost column, delete the preceding newline.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1997-11-04 15:21:50 -04:00
|
|
|
|
Otherwise, if point is at the leftmost non-whitespace character of a
|
|
|
|
|
line that is neither a continuation line nor a non-indenting comment
|
|
|
|
|
line, or if point is at the end of a blank line, this command reduces
|
|
|
|
|
the indentation to match that of the line that opened the current
|
|
|
|
|
block of code. The line that opened the block is displayed in the
|
1998-08-10 13:33:12 -03:00
|
|
|
|
echo area to help you keep track of where you are. With
|
|
|
|
|
\\[universal-argument] dedents that many blocks (but not past column
|
|
|
|
|
zero).
|
1997-11-04 15:21:50 -04:00
|
|
|
|
|
|
|
|
|
Otherwise the preceding character is deleted, converting a tab to
|
|
|
|
|
spaces if needed so that only a single column position is deleted.
|
1999-05-24 18:43:37 -03:00
|
|
|
|
\\[universal-argument] specifies how many characters to delete;
|
|
|
|
|
default is 1.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
|
|
|
|
|
When used programmatically, argument ARG specifies the number of
|
|
|
|
|
blocks to dedent, or the number of characters to delete, as indicated
|
|
|
|
|
above."
|
1996-07-03 20:12:52 -03:00
|
|
|
|
(interactive "*p")
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (or (/= (current-indentation) (current-column))
|
|
|
|
|
(bolp)
|
|
|
|
|
(py-continuation-line-p)
|
1999-05-24 18:43:37 -03:00
|
|
|
|
; (not py-honor-comment-indentation)
|
|
|
|
|
; (looking-at "#[^ \t\n]") ; non-indenting #
|
|
|
|
|
)
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(funcall py-backspace-function arg)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
;; else indent the same as the colon line that opened the block
|
|
|
|
|
;; force non-blank so py-goto-block-up doesn't ignore it
|
|
|
|
|
(insert-char ?* 1)
|
|
|
|
|
(backward-char)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((base-indent 0) ; indentation of base line
|
|
|
|
|
(base-text "") ; and text of base line
|
|
|
|
|
(base-found-p nil))
|
1996-07-03 20:12:52 -03:00
|
|
|
|
(save-excursion
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(while (< 0 arg)
|
1996-07-03 20:12:52 -03:00
|
|
|
|
(condition-case nil ; in case no enclosing block
|
|
|
|
|
(progn
|
|
|
|
|
(py-goto-block-up 'no-mark)
|
|
|
|
|
(setq base-indent (current-indentation)
|
|
|
|
|
base-text (py-suck-up-leading-text)
|
|
|
|
|
base-found-p t))
|
|
|
|
|
(error nil))
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(setq arg (1- arg))))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(delete-char 1) ; toss the dummy character
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(indent-to base-indent)
|
|
|
|
|
(if base-found-p
|
|
|
|
|
(message "Closes block: %s" base-text)))))
|
|
|
|
|
|
1997-11-04 15:21:50 -04:00
|
|
|
|
|
|
|
|
|
(defun py-electric-delete (arg)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Delete preceding or following character or levels of whitespace.
|
1997-11-04 15:21:50 -04:00
|
|
|
|
|
|
|
|
|
The behavior of this function depends on the variable
|
|
|
|
|
`delete-key-deletes-forward'. If this variable is nil (or does not
|
1998-08-10 13:33:12 -03:00
|
|
|
|
exist, as in older Emacsen and non-XEmacs versions), then this
|
|
|
|
|
function behaves identically to \\[c-electric-backspace].
|
1997-11-04 15:21:50 -04:00
|
|
|
|
|
|
|
|
|
If `delete-key-deletes-forward' is non-nil and is supported in your
|
|
|
|
|
Emacs, then deletion occurs in the forward direction, by calling the
|
1998-08-10 13:33:12 -03:00
|
|
|
|
function in `py-delete-function'.
|
|
|
|
|
|
|
|
|
|
\\[universal-argument] (programmatically, argument ARG) specifies the
|
|
|
|
|
number of characters to delete (default is 1)."
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(interactive "*p")
|
1999-01-14 22:12:31 -04:00
|
|
|
|
(if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
|
|
|
|
|
(delete-forward-p))
|
|
|
|
|
(and (boundp 'delete-key-deletes-forward) ;XEmacs 20
|
|
|
|
|
delete-key-deletes-forward))
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(funcall py-delete-function arg)
|
|
|
|
|
(py-electric-backspace arg)))
|
|
|
|
|
|
1995-03-09 12:07:29 -04:00
|
|
|
|
;; required for pending-del and delsel modes
|
1997-11-04 15:21:50 -04:00
|
|
|
|
(put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
|
|
|
|
|
(put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del
|
|
|
|
|
(put 'py-electric-delete 'delete-selection 'supersede) ;delsel
|
|
|
|
|
(put 'py-electric-delete 'pending-delete 'supersede) ;pending-del
|
|
|
|
|
|
1995-03-09 12:07:29 -04:00
|
|
|
|
|
1997-11-04 15:21:50 -04:00
|
|
|
|
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(defun py-indent-line (&optional arg)
|
|
|
|
|
"Fix the indentation of the current line according to Python rules.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
With \\[universal-argument] (programmatically, the optional argument
|
|
|
|
|
ARG non-nil), ignore dedenting rules for block closing statements
|
|
|
|
|
(e.g. return, raise, break, continue, pass)
|
1996-10-23 17:44:59 -03:00
|
|
|
|
|
|
|
|
|
This function is normally bound to `indent-line-function' so
|
|
|
|
|
\\[indent-for-tab-command] will call it."
|
|
|
|
|
(interactive "P")
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let* ((ci (current-indentation))
|
|
|
|
|
(move-to-indentation-p (<= (current-column) ci))
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(need (py-compute-indentation (not arg))))
|
1998-08-10 13:33:12 -03:00
|
|
|
|
;; see if we need to dedent
|
1995-03-14 18:05:53 -04:00
|
|
|
|
(if (py-outdent-p)
|
1995-03-14 12:32:55 -04:00
|
|
|
|
(setq need (- need py-indent-offset)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (/= ci need)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(indent-to need)))
|
|
|
|
|
(if move-to-indentation-p (back-to-indentation))))
|
|
|
|
|
|
|
|
|
|
(defun py-newline-and-indent ()
|
1995-03-08 17:33:04 -04:00
|
|
|
|
"Strives to act like the Emacs `newline-and-indent'.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
This is just `strives to' because correct indentation can't be computed
|
|
|
|
|
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
|
|
|
|
|
the new line indented."
|
|
|
|
|
(interactive)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((ci (current-indentation)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (< ci (current-column)) ; if point beyond indentation
|
|
|
|
|
(newline-and-indent)
|
|
|
|
|
;; else try to act like newline-and-indent "normally" acts
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(insert-char ?\n 1)
|
|
|
|
|
(move-to-column ci))))
|
|
|
|
|
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(defun py-compute-indentation (honor-block-close-p)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Compute Python indentation.
|
|
|
|
|
When HONOR-BLOCK-CLOSE-P is non-nil, statements such as `return',
|
|
|
|
|
`raise', `break', `continue', and `pass' force one level of
|
|
|
|
|
dedenting."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
1998-02-12 12:52:14 -04:00
|
|
|
|
(beginning-of-line)
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
(let* ((bod (py-point 'bod))
|
1997-11-26 16:58:48 -04:00
|
|
|
|
(pps (parse-partial-sexp bod (point)))
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(boipps (parse-partial-sexp bod (py-point 'boi)))
|
|
|
|
|
placeholder)
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(cond
|
Added comment about where to find details on python-mode.el, pointing
to the Web site.
(py-defun-start-re, py-class-start-re): Changed to defconst.
(py-traceback-line-re): Regular expression describing what traceback
lines look like.
(py-point): New defsubst copied from CC Mode.
(py-highlight-line): Function which does the work of making a
traceback line mouseable. This only works on XEmacs. Someone familar
with Emacs text properties and such will have to do that port.
(py-mode-map): Added C-c- bound to py-up-exception and C-c= bound to
py-down-exception. Also, more concise form for mapcar.
(py-mode-output-map): New keymap for the *Python Output* buffer which
only has keybindings for py-mouseto-exception and py-goto-exception.
All other self-insert-command's are bound to beep. This is actually
bogus because the buffer should really be made read-only and the
functions that insert in that buffer should bind inhibit-read-only.
Also, this map should be bound to highlighted extents in a *Python*
shell buffer, but this stuff hasn't been migrated into there.
(py-postprocess-output-buffer): New function which extentifies the
*Python Output* buffer. The bogosities are that this only runs when
the synchronous process in the buffer is finished (so it doesn't work
for async procs), and it should also be merged into py-process-filter
so the *Python* shell gets mouseable too.
(py-shell): Added C-c- and C-c= to the comint buffer's keymap. The
bogosity is that py-goto-exception should also be bound, but it cannot
be bound to C-cC-c (since that interferes with
comint-interrupt-subjob's typical binding). Also, traceback lines
aren't mouseable in this buffer.
(py-execute-region): Support for traceback jumping. This really is
quite a kludge, but necessary based on the way all this stuff works.
There's bound to be broken interactions here.
(py-jump-to-exception, py-mouseto-exception, py-goto-exception,
py-find-next-exception, py-down-exception, py-up-exception): All new
commands and functions to implement traceback jumping.
(py-compute-indentation): Hope this change doesn't get lost in all the
noise above!!!! This fixes broken non-indentation of a line when TAB
is hit inside a string that isn't a multi-line string.
1997-11-25 21:04:44 -04:00
|
|
|
|
;; are we inside a multi-line string or comment?
|
1997-11-26 16:58:48 -04:00
|
|
|
|
((or (and (nth 3 pps) (nth 3 boipps))
|
|
|
|
|
(and (nth 4 pps) (nth 4 boipps)))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(if (not py-align-multiline-strings-p) 0
|
|
|
|
|
;; skip back over blank & non-indenting comment lines
|
|
|
|
|
;; note: will skip a blank or non-indenting comment line
|
|
|
|
|
;; that happens to be a continuation line too
|
|
|
|
|
(re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(current-column))))
|
|
|
|
|
;; are we on a continuation line?
|
|
|
|
|
((py-continuation-line-p)
|
|
|
|
|
(let ((startpos (point))
|
|
|
|
|
(open-bracket-pos (py-nesting-level))
|
1996-08-01 15:17:14 -03:00
|
|
|
|
endpos searching found state)
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(if open-bracket-pos
|
|
|
|
|
(progn
|
|
|
|
|
;; align with first item in list; else a normal
|
|
|
|
|
;; indent beyond the line with the open bracket
|
|
|
|
|
(goto-char (1+ open-bracket-pos)) ; just beyond bracket
|
|
|
|
|
;; is the first list item on the same line?
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(if (null (memq (following-char) '(?\n ?# ?\\)))
|
|
|
|
|
; yes, so line up with it
|
|
|
|
|
(current-column)
|
|
|
|
|
;; first list item on another line, or doesn't exist yet
|
|
|
|
|
(forward-line 1)
|
|
|
|
|
(while (and (< (point) startpos)
|
|
|
|
|
(looking-at "[ \t]*[#\n\\\\]")) ; skip noise
|
|
|
|
|
(forward-line 1))
|
1998-04-01 17:59:41 -04:00
|
|
|
|
(if (and (< (point) startpos)
|
|
|
|
|
(/= startpos
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (1+ open-bracket-pos))
|
1998-04-16 17:04:59 -03:00
|
|
|
|
(forward-comment (point-max))
|
1998-04-01 17:59:41 -04:00
|
|
|
|
(point))))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
;; again mimic the first list item
|
|
|
|
|
(current-indentation)
|
|
|
|
|
;; else they're about to enter the first item
|
|
|
|
|
(goto-char open-bracket-pos)
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(setq placeholder (point))
|
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(py-goto-beginning-of-tqs
|
|
|
|
|
(save-excursion (nth 3 (parse-partial-sexp
|
|
|
|
|
placeholder (point)))))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(+ (current-indentation) py-indent-offset))))
|
|
|
|
|
|
|
|
|
|
;; else on backslash continuation line
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(if (py-continuation-line-p) ; on at least 3rd line in block
|
|
|
|
|
(current-indentation) ; so just continue the pattern
|
|
|
|
|
;; else started on 2nd line in block, so indent more.
|
|
|
|
|
;; if base line is an assignment with a start on a RHS,
|
|
|
|
|
;; indent to 2 beyond the leftmost "="; else skip first
|
|
|
|
|
;; chunk of non-whitespace characters on base line, + 1 more
|
|
|
|
|
;; column
|
|
|
|
|
(end-of-line)
|
2001-06-18 20:40:35 -03:00
|
|
|
|
(setq endpos (point)
|
|
|
|
|
searching t)
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(setq startpos (point))
|
|
|
|
|
;; look at all "=" from left to right, stopping at first
|
|
|
|
|
;; one not nested in a list or string
|
|
|
|
|
(while searching
|
|
|
|
|
(skip-chars-forward "^=" endpos)
|
|
|
|
|
(if (= (point) endpos)
|
|
|
|
|
(setq searching nil)
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(setq state (parse-partial-sexp startpos (point)))
|
|
|
|
|
(if (and (zerop (car state)) ; not in a bracket
|
|
|
|
|
(null (nth 3 state))) ; & not in a string
|
|
|
|
|
(progn
|
|
|
|
|
(setq searching nil) ; done searching in any case
|
|
|
|
|
(setq found
|
|
|
|
|
(not (or
|
|
|
|
|
(eq (following-char) ?=)
|
|
|
|
|
(memq (char-after (- (point) 2))
|
|
|
|
|
'(?< ?> ?!)))))))))
|
|
|
|
|
(if (or (not found) ; not an assignment
|
|
|
|
|
(looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
|
|
|
|
|
(progn
|
|
|
|
|
(goto-char startpos)
|
|
|
|
|
(skip-chars-forward "^ \t\n")))
|
2001-07-06 17:07:13 -03:00
|
|
|
|
;; if this is a continuation for a block opening
|
|
|
|
|
;; statement, add some extra offset.
|
|
|
|
|
(+ (current-column) (if (py-statement-opens-block-p)
|
|
|
|
|
py-continuation-offset 0)
|
|
|
|
|
1)
|
2001-06-18 20:40:35 -03:00
|
|
|
|
))))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
|
|
|
|
|
;; not on a continuation line
|
1996-08-01 12:53:09 -03:00
|
|
|
|
((bobp) (current-indentation))
|
|
|
|
|
|
|
|
|
|
;; Dfn: "Indenting comment line". A line containing only a
|
|
|
|
|
;; comment, but which is treated like a statement for
|
|
|
|
|
;; indentation calculation purposes. Such lines are only
|
|
|
|
|
;; treated specially by the mode; they are not treated
|
|
|
|
|
;; specially by the Python interpreter.
|
|
|
|
|
|
|
|
|
|
;; The rules for indenting comment lines are a line where:
|
|
|
|
|
;; - the first non-whitespace character is `#', and
|
|
|
|
|
;; - the character following the `#' is whitespace, and
|
1998-08-10 13:33:12 -03:00
|
|
|
|
;; - the line is dedented with respect to (i.e. to the left
|
1996-08-01 12:53:09 -03:00
|
|
|
|
;; of) the indentation of the preceding non-blank line.
|
|
|
|
|
|
|
|
|
|
;; The first non-blank line following an indenting comment
|
|
|
|
|
;; line is given the same amount of indentation as the
|
|
|
|
|
;; indenting comment line.
|
|
|
|
|
|
|
|
|
|
;; All other comment-only lines are ignored for indentation
|
|
|
|
|
;; purposes.
|
|
|
|
|
|
|
|
|
|
;; Are we looking at a comment-only line which is *not* an
|
1998-05-19 11:49:49 -03:00
|
|
|
|
;; indenting comment line? If so, we assume that it's been
|
1996-08-01 12:53:09 -03:00
|
|
|
|
;; placed at the desired indentation, so leave it alone.
|
|
|
|
|
;; Indenting comment lines are aligned as statements down
|
|
|
|
|
;; below.
|
|
|
|
|
((and (looking-at "[ \t]*#[^ \t\n]")
|
|
|
|
|
;; NOTE: this test will not be performed in older Emacsen
|
|
|
|
|
(fboundp 'forward-comment)
|
|
|
|
|
(<= (current-indentation)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(forward-comment (- (point-max)))
|
|
|
|
|
(current-indentation))))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(current-indentation))
|
|
|
|
|
|
|
|
|
|
;; else indentation based on that of the statement that
|
|
|
|
|
;; precedes us; use the first line of that statement to
|
|
|
|
|
;; establish the base, in case the user forced a non-std
|
|
|
|
|
;; indentation for the continuation lines (if any)
|
|
|
|
|
(t
|
1995-09-14 15:49:11 -03:00
|
|
|
|
;; skip back over blank & non-indenting comment lines note:
|
|
|
|
|
;; will skip a blank or non-indenting comment line that
|
1996-03-04 13:15:40 -04:00
|
|
|
|
;; happens to be a continuation line too. use fast Emacs 19
|
|
|
|
|
;; function if it's there.
|
1996-03-06 14:41:38 -04:00
|
|
|
|
(if (and (eq py-honor-comment-indentation nil)
|
1996-03-05 12:28:07 -04:00
|
|
|
|
(fboundp 'forward-comment))
|
1996-03-04 13:15:40 -04:00
|
|
|
|
(forward-comment (- (point-max)))
|
1998-09-22 16:51:47 -03:00
|
|
|
|
(let ((prefix-re (concat py-block-comment-prefix "[ \t]*"))
|
|
|
|
|
done)
|
1996-03-06 14:41:38 -04:00
|
|
|
|
(while (not done)
|
1998-08-10 18:44:37 -03:00
|
|
|
|
(re-search-backward "^[ \t]*\\([^ \t\n#]\\|#\\)" nil 'move)
|
|
|
|
|
(setq done (or (bobp)
|
|
|
|
|
(and (eq py-honor-comment-indentation t)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(back-to-indentation)
|
1998-09-22 16:51:47 -03:00
|
|
|
|
(not (looking-at prefix-re))
|
1998-08-10 18:44:37 -03:00
|
|
|
|
))
|
|
|
|
|
(and (not (eq py-honor-comment-indentation t))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(back-to-indentation)
|
2002-03-15 12:46:46 -04:00
|
|
|
|
(and (not (looking-at prefix-re))
|
|
|
|
|
(or (looking-at "[^#]")
|
|
|
|
|
(not (zerop (current-column)))
|
|
|
|
|
))
|
|
|
|
|
))
|
1998-08-10 18:44:37 -03:00
|
|
|
|
))
|
1996-03-06 14:41:38 -04:00
|
|
|
|
)))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
;; if we landed inside a string, go to the beginning of that
|
|
|
|
|
;; string. this handles triple quoted, multi-line spanning
|
|
|
|
|
;; strings.
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(py-goto-beginning-of-tqs (nth 3 (parse-partial-sexp bod (point))))
|
1998-01-20 18:52:56 -04:00
|
|
|
|
;; now skip backward over continued lines
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(setq placeholder (point))
|
1995-09-19 17:01:42 -03:00
|
|
|
|
(py-goto-initial-line)
|
1998-12-15 00:36:22 -04:00
|
|
|
|
;; we may *now* have landed in a TQS, so find the beginning of
|
|
|
|
|
;; this string.
|
|
|
|
|
(py-goto-beginning-of-tqs
|
|
|
|
|
(save-excursion (nth 3 (parse-partial-sexp
|
|
|
|
|
placeholder (point)))))
|
1996-07-31 17:42:59 -03:00
|
|
|
|
(+ (current-indentation)
|
|
|
|
|
(if (py-statement-opens-block-p)
|
|
|
|
|
py-indent-offset
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(if (and honor-block-close-p (py-statement-closes-block-p))
|
1996-07-31 17:42:59 -03:00
|
|
|
|
(- py-indent-offset)
|
|
|
|
|
0)))
|
|
|
|
|
)))))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
(defun py-guess-indent-offset (&optional global)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
"Guess a good value for, and change, `py-indent-offset'.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
|
|
|
|
|
By default, make a buffer-local copy of `py-indent-offset' with the
|
|
|
|
|
new value, so that other Python buffers are not affected. With
|
|
|
|
|
\\[universal-argument] (programmatically, optional argument GLOBAL),
|
|
|
|
|
change the global value of `py-indent-offset'. This affects all
|
|
|
|
|
Python buffers (that don't have their own buffer-local copy), both
|
|
|
|
|
those currently existing and those created later in the Emacs session.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Some people use a different value for `py-indent-offset' than you use.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
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
|
1995-03-08 17:33:04 -04:00
|
|
|
|
`py-indent-offset' to what it thinks it was when they created the
|
|
|
|
|
mess.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Specifically, it searches forward from the statement containing point,
|
1995-03-08 17:33:04 -04:00
|
|
|
|
looking for a line that opens a block of code. `py-indent-offset' is
|
|
|
|
|
set to the difference in indentation between that line and the Python
|
1994-12-12 16:38:05 -04:00
|
|
|
|
statement following it. If the search doesn't succeed going forward,
|
|
|
|
|
it's tried again going backward."
|
|
|
|
|
(interactive "P") ; raw prefix arg
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let (new-value
|
|
|
|
|
(start (point))
|
1998-03-19 18:48:02 -04:00
|
|
|
|
(restart (point))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(found nil)
|
|
|
|
|
colon-indent)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(while (not (or found (eobp)))
|
1998-03-19 18:48:02 -04:00
|
|
|
|
(when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
|
|
|
|
|
(not (py-in-literal restart)))
|
|
|
|
|
(setq restart (point))
|
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(if (py-statement-opens-block-p)
|
|
|
|
|
(setq found t)
|
|
|
|
|
(goto-char restart))))
|
|
|
|
|
(unless found
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(goto-char start)
|
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(while (not (or found (bobp)))
|
1998-03-19 18:48:02 -04:00
|
|
|
|
(setq found (and
|
|
|
|
|
(re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
|
|
|
|
|
(or (py-goto-initial-line) t) ; always true -- side effect
|
|
|
|
|
(py-statement-opens-block-p)))))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(setq colon-indent (current-indentation)
|
|
|
|
|
found (and found (zerop (py-next-statement 1)))
|
|
|
|
|
new-value (- (current-indentation) colon-indent))
|
|
|
|
|
(goto-char start)
|
1998-03-19 18:48:02 -04:00
|
|
|
|
(if (not found)
|
|
|
|
|
(error "Sorry, couldn't guess a value for py-indent-offset")
|
|
|
|
|
(funcall (if global 'kill-local-variable 'make-local-variable)
|
|
|
|
|
'py-indent-offset)
|
|
|
|
|
(setq py-indent-offset new-value)
|
1998-09-24 21:08:38 -03:00
|
|
|
|
(or noninteractive
|
|
|
|
|
(message "%s value of py-indent-offset set to %d"
|
|
|
|
|
(if global "Global" "Local")
|
|
|
|
|
py-indent-offset)))
|
1998-03-19 18:48:02 -04:00
|
|
|
|
))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-07-07 12:11:24 -03:00
|
|
|
|
(defun py-comment-indent-function ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Python version of `comment-indent-function'."
|
|
|
|
|
;; This is required when filladapt is turned off. Without it, when
|
|
|
|
|
;; filladapt is not used, comments which start in column zero
|
|
|
|
|
;; cascade one character to the right
|
1998-07-07 12:11:24 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(let ((eol (py-point 'eol)))
|
|
|
|
|
(and comment-start-skip
|
|
|
|
|
(re-search-forward comment-start-skip eol t)
|
|
|
|
|
(setq eol (match-beginning 0)))
|
|
|
|
|
(goto-char eol)
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
(max comment-column (+ (current-column) (if (bolp) 0 1)))
|
|
|
|
|
)))
|
|
|
|
|
|
1999-01-18 17:49:39 -04:00
|
|
|
|
(defun py-narrow-to-defun (&optional class)
|
|
|
|
|
"Make text outside current defun invisible.
|
|
|
|
|
The defun visible is the one that contains point or follows point.
|
|
|
|
|
Optional CLASS is passed directly to `py-beginning-of-def-or-class'."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(widen)
|
|
|
|
|
(py-end-of-def-or-class class)
|
|
|
|
|
(let ((end (point)))
|
|
|
|
|
(py-beginning-of-def-or-class class)
|
|
|
|
|
(narrow-to-region (point) end))))
|
|
|
|
|
|
1998-07-07 12:11:24 -03:00
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-shift-region (start end count)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Indent lines from START to END by COUNT spaces."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
1998-08-10 13:33:12 -03:00
|
|
|
|
(goto-char end)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(setq end (point))
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(setq start (point))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(indent-rigidly start end count)))
|
|
|
|
|
|
|
|
|
|
(defun py-shift-region-left (start end &optional count)
|
|
|
|
|
"Shift region of Python code to the left.
|
|
|
|
|
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
|
1995-03-08 17:33:04 -04:00
|
|
|
|
shifted to the left, by `py-indent-offset' columns.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
If a prefix argument is given, the region is instead shifted by that
|
1998-08-10 13:33:12 -03:00
|
|
|
|
many columns. With no active region, dedent only the current line.
|
|
|
|
|
You cannot dedent the region if any line is already at column zero."
|
1996-07-03 19:59:12 -03:00
|
|
|
|
(interactive
|
|
|
|
|
(let ((p (point))
|
|
|
|
|
(m (mark))
|
|
|
|
|
(arg current-prefix-arg))
|
|
|
|
|
(if m
|
|
|
|
|
(list (min p m) (max p m) arg)
|
|
|
|
|
(list p (save-excursion (forward-line 1) (point)) arg))))
|
|
|
|
|
;; if any line is at column zero, don't shift the region
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(while (< (point) end)
|
|
|
|
|
(back-to-indentation)
|
1996-07-23 12:03:16 -03:00
|
|
|
|
(if (and (zerop (current-column))
|
|
|
|
|
(not (looking-at "\\s *$")))
|
1998-08-10 13:33:12 -03:00
|
|
|
|
(error "Region is at left edge"))
|
1996-07-03 19:59:12 -03:00
|
|
|
|
(forward-line 1)))
|
|
|
|
|
(py-shift-region start end (- (prefix-numeric-value
|
|
|
|
|
(or count py-indent-offset))))
|
|
|
|
|
(py-keep-region-active))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
(defun py-shift-region-right (start end &optional count)
|
|
|
|
|
"Shift region of Python code to the right.
|
|
|
|
|
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
|
1995-03-08 17:33:04 -04:00
|
|
|
|
shifted to the right, by `py-indent-offset' columns.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
If a prefix argument is given, the region is instead shifted by that
|
1996-07-03 19:59:12 -03:00
|
|
|
|
many columns. With no active region, indent only the current line."
|
|
|
|
|
(interactive
|
|
|
|
|
(let ((p (point))
|
|
|
|
|
(m (mark))
|
|
|
|
|
(arg current-prefix-arg))
|
|
|
|
|
(if m
|
|
|
|
|
(list (min p m) (max p m) arg)
|
|
|
|
|
(list p (save-excursion (forward-line 1) (point)) arg))))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(py-shift-region start end (prefix-numeric-value
|
1996-07-03 19:59:12 -03:00
|
|
|
|
(or count py-indent-offset)))
|
|
|
|
|
(py-keep-region-active))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
(defun py-indent-region (start end &optional indent-offset)
|
|
|
|
|
"Reindent a region of Python code.
|
1996-03-07 14:30:26 -04:00
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
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
|
|
|
|
|
reindented. If the first line of the region has a non-whitespace
|
1995-03-08 17:33:04 -04:00
|
|
|
|
character in the first column, the first line is left alone and the
|
|
|
|
|
rest of the region is reindented with respect to it. Else the entire
|
1996-03-07 14:30:26 -04:00
|
|
|
|
region is reindented with respect to the (closest code or indenting
|
|
|
|
|
comment) statement immediately preceding the region.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
This is useful when code blocks are moved or yanked, when enclosing
|
1995-03-08 17:33:04 -04:00
|
|
|
|
control structures are introduced or removed, or to reformat code
|
|
|
|
|
using a new value for the indentation offset.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
If a numeric prefix argument is given, it will be used as the value of
|
1995-03-08 17:33:04 -04:00
|
|
|
|
the indentation offset. Else the value of `py-indent-offset' will be
|
1994-12-12 16:38:05 -04:00
|
|
|
|
used.
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Warning: The region must be consistently indented before this function
|
1994-12-12 16:38:05 -04:00
|
|
|
|
is called! This function does not compute proper indentation from
|
|
|
|
|
scratch (that's impossible in Python), it merely adjusts the existing
|
|
|
|
|
indentation to be correct in context.
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Warning: This function really has no idea what to do with
|
|
|
|
|
non-indenting comment lines, and shifts them as if they were indenting
|
|
|
|
|
comment lines. Fixing this appears to require telepathy.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Special cases: whitespace is deleted from blank lines; continuation
|
|
|
|
|
lines are shifted by the same amount their initial line was shifted,
|
|
|
|
|
in order to preserve their relative indentation with respect to their
|
1994-12-12 16:38:05 -04:00
|
|
|
|
initial line; and comment lines beginning in column 1 are ignored."
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(interactive "*r\nP") ; region; raw prefix arg
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char end) (beginning-of-line) (setq end (point-marker))
|
|
|
|
|
(goto-char start) (beginning-of-line)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((py-indent-offset (prefix-numeric-value
|
|
|
|
|
(or indent-offset py-indent-offset)))
|
|
|
|
|
(indents '(-1)) ; stack of active indent levels
|
|
|
|
|
(target-column 0) ; column to which to indent
|
|
|
|
|
(base-shifted-by 0) ; amount last base line was shifted
|
|
|
|
|
(indent-base (if (looking-at "[ \t\n]")
|
1996-10-23 17:44:59 -03:00
|
|
|
|
(py-compute-indentation t)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
0))
|
|
|
|
|
ci)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (< (point) end)
|
|
|
|
|
(setq ci (current-indentation))
|
|
|
|
|
;; figure out appropriate target column
|
|
|
|
|
(cond
|
1995-03-08 17:33:04 -04:00
|
|
|
|
((or (eq (following-char) ?#) ; comment in column 1
|
|
|
|
|
(looking-at "[ \t]*$")) ; entirely blank
|
|
|
|
|
(setq target-column 0))
|
|
|
|
|
((py-continuation-line-p) ; shift relative to base line
|
|
|
|
|
(setq target-column (+ ci base-shifted-by)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(t ; new base line
|
|
|
|
|
(if (> ci (car indents)) ; going deeper; push it
|
|
|
|
|
(setq indents (cons ci indents))
|
|
|
|
|
;; else we should have seen this indent before
|
|
|
|
|
(setq indents (memq ci indents)) ; pop deeper indents
|
|
|
|
|
(if (null indents)
|
|
|
|
|
(error "Bad indentation in region, at line %d"
|
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(1+ (count-lines 1 (point)))))))
|
|
|
|
|
(setq target-column (+ indent-base
|
|
|
|
|
(* py-indent-offset
|
|
|
|
|
(- (length indents) 2))))
|
|
|
|
|
(setq base-shifted-by (- target-column ci))))
|
|
|
|
|
;; shift as needed
|
|
|
|
|
(if (/= ci target-column)
|
|
|
|
|
(progn
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(indent-to target-column)))
|
|
|
|
|
(forward-line 1))))
|
|
|
|
|
(set-marker end nil))
|
|
|
|
|
|
1996-08-01 12:53:09 -03:00
|
|
|
|
(defun py-comment-region (beg end &optional arg)
|
|
|
|
|
"Like `comment-region' but uses double hash (`#') comment starter."
|
|
|
|
|
(interactive "r\nP")
|
1996-08-01 17:11:51 -03:00
|
|
|
|
(let ((comment-start py-block-comment-prefix))
|
1996-08-01 12:53:09 -03:00
|
|
|
|
(comment-region beg end arg)))
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;; Functions for moving point
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-previous-statement (count)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to the start of the COUNTth preceding Python statement.
|
|
|
|
|
By default, goes to the previous statement. If there is no such
|
|
|
|
|
statement, goes to the first statement. Return count of statements
|
|
|
|
|
left to move. `Statements' do not include blank, comment, or
|
|
|
|
|
continuation lines."
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(interactive "p") ; numeric prefix arg
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (< count 0) (py-next-statement (- count))
|
|
|
|
|
(py-goto-initial-line)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let (start)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (and
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(setq start (point)) ; always true -- side effect
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(> count 0)
|
|
|
|
|
(zerop (forward-line -1))
|
|
|
|
|
(py-goto-statement-at-or-above))
|
|
|
|
|
(setq count (1- count)))
|
|
|
|
|
(if (> count 0) (goto-char start)))
|
|
|
|
|
count))
|
|
|
|
|
|
|
|
|
|
(defun py-next-statement (count)
|
|
|
|
|
"Go to the start of next Python statement.
|
|
|
|
|
If the statement at point is the i'th Python statement, goes to the
|
|
|
|
|
start of statement i+COUNT. If there is no such statement, goes to the
|
|
|
|
|
last statement. Returns count of statements left to move. `Statements'
|
|
|
|
|
do not include blank, comment, or continuation lines."
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(interactive "p") ; numeric prefix arg
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (< count 0) (py-previous-statement (- count))
|
|
|
|
|
(beginning-of-line)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let (start)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (and
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(setq start (point)) ; always true -- side effect
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(> count 0)
|
|
|
|
|
(py-goto-statement-below))
|
|
|
|
|
(setq count (1- count)))
|
|
|
|
|
(if (> count 0) (goto-char start)))
|
|
|
|
|
count))
|
|
|
|
|
|
|
|
|
|
(defun py-goto-block-up (&optional nomark)
|
|
|
|
|
"Move up to start of current block.
|
|
|
|
|
Go to the statement that starts the smallest enclosing block; roughly
|
|
|
|
|
speaking, this will be the closest preceding statement that ends with a
|
|
|
|
|
colon and is indented less than the statement you started on. If
|
|
|
|
|
successful, also sets the mark to the starting point.
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
`\\[py-mark-block]' can be used afterward to mark the whole code
|
|
|
|
|
block, if desired.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
If called from a program, the mark will not be set if optional argument
|
|
|
|
|
NOMARK is not nil."
|
|
|
|
|
(interactive)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((start (point))
|
|
|
|
|
(found nil)
|
|
|
|
|
initial-indent)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
;; if on blank or non-indenting comment line, use the preceding stmt
|
|
|
|
|
(if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
|
|
|
|
|
(progn
|
|
|
|
|
(py-goto-statement-at-or-above)
|
|
|
|
|
(setq found (py-statement-opens-block-p))))
|
|
|
|
|
;; search back for colon line indented less
|
|
|
|
|
(setq initial-indent (current-indentation))
|
|
|
|
|
(if (zerop initial-indent)
|
|
|
|
|
;; force fast exit
|
|
|
|
|
(goto-char (point-min)))
|
|
|
|
|
(while (not (or found (bobp)))
|
|
|
|
|
(setq found
|
|
|
|
|
(and
|
|
|
|
|
(re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
|
|
|
|
|
(or (py-goto-initial-line) t) ; always true -- side effect
|
|
|
|
|
(< (current-indentation) initial-indent)
|
|
|
|
|
(py-statement-opens-block-p))))
|
|
|
|
|
(if found
|
|
|
|
|
(progn
|
|
|
|
|
(or nomark (push-mark start))
|
|
|
|
|
(back-to-indentation))
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(error "Enclosing block not found"))))
|
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
(defun py-beginning-of-def-or-class (&optional class count)
|
|
|
|
|
"Move point to start of `def' or `class'.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Searches back for the closest preceding `def'. If you supply a prefix
|
1998-05-19 12:31:46 -03:00
|
|
|
|
arg, looks for a `class' instead. The docs below assume the `def'
|
|
|
|
|
case; just substitute `class' for `def' for the other case.
|
1998-07-07 14:45:38 -03:00
|
|
|
|
Programmatically, if CLASS is `either', then moves to either `class'
|
|
|
|
|
or `def'.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
When second optional argument is given programmatically, move to the
|
|
|
|
|
COUNTth start of `def'.
|
|
|
|
|
|
|
|
|
|
If point is in a `def' statement already, and after the `d', simply
|
1995-03-08 17:33:04 -04:00
|
|
|
|
moves point to the start of the statement.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
Otherwise (i.e. when point is not in a `def' statement, or at or
|
|
|
|
|
before the `d' of a `def' statement), searches for the closest
|
|
|
|
|
preceding `def' statement, and leaves point at its start. If no such
|
|
|
|
|
statement can be found, leaves point at the start of the buffer.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
Returns t iff a `def' statement is found by these rules.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Note that doing this command repeatedly will take you closer to the
|
|
|
|
|
start of the buffer each time.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-07-07 14:45:38 -03:00
|
|
|
|
To mark the current `def', see `\\[py-mark-def-or-class]'."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(interactive "P") ; raw prefix arg
|
1998-10-27 20:10:45 -04:00
|
|
|
|
(setq count (or count 1))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((at-or-before-p (<= (current-column) (current-indentation)))
|
1998-05-19 12:31:46 -03:00
|
|
|
|
(start-of-line (goto-char (py-point 'bol)))
|
|
|
|
|
(start-of-stmt (goto-char (py-point 'bos)))
|
1998-07-07 14:45:38 -03:00
|
|
|
|
(start-re (cond ((eq class 'either) "^[ \t]*\\(class\\|def\\)\\>")
|
|
|
|
|
(class "^[ \t]*class\\>")
|
|
|
|
|
(t "^[ \t]*def\\>")))
|
1998-05-19 12:31:46 -03:00
|
|
|
|
)
|
|
|
|
|
;; searching backward
|
|
|
|
|
(if (and (< 0 count)
|
|
|
|
|
(or (/= start-of-stmt start-of-line)
|
|
|
|
|
(not at-or-before-p)))
|
|
|
|
|
(end-of-line))
|
|
|
|
|
;; search forward
|
|
|
|
|
(if (and (> 0 count)
|
|
|
|
|
(zerop (current-column))
|
|
|
|
|
(looking-at start-re))
|
|
|
|
|
(end-of-line))
|
1999-07-27 18:40:02 -03:00
|
|
|
|
(if (re-search-backward start-re nil 'move count)
|
|
|
|
|
(goto-char (match-beginning 0)))))
|
1998-05-19 12:31:46 -03:00
|
|
|
|
|
|
|
|
|
;; Backwards compatibility
|
|
|
|
|
(defalias 'beginning-of-python-def-or-class 'py-beginning-of-def-or-class)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
(defun py-end-of-def-or-class (&optional class count)
|
|
|
|
|
"Move point beyond end of `def' or `class' body.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
By default, looks for an appropriate `def'. If you supply a prefix
|
|
|
|
|
arg, looks for a `class' instead. The docs below assume the `def'
|
|
|
|
|
case; just substitute `class' for `def' for the other case.
|
1998-07-07 14:45:38 -03:00
|
|
|
|
Programmatically, if CLASS is `either', then moves to either `class'
|
|
|
|
|
or `def'.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
When second optional argument is given programmatically, move to the
|
|
|
|
|
COUNTth end of `def'.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
If point is in a `def' statement already, this is the `def' we use.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
Else, if the `def' found by `\\[py-beginning-of-def-or-class]'
|
|
|
|
|
contains the statement you started on, that's the `def' we use.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-05-19 12:31:46 -03:00
|
|
|
|
Otherwise, 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 line immediately following the `def' block, and the position of the
|
|
|
|
|
start of the `def' is returned.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Else point is moved to the end of the buffer, and nil is returned.
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Note that doing this command repeatedly will take you closer to the
|
|
|
|
|
end of the buffer each time.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1998-07-07 14:45:38 -03:00
|
|
|
|
To mark the current `def', see `\\[py-mark-def-or-class]'."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(interactive "P") ; raw prefix arg
|
1998-05-19 12:31:46 -03:00
|
|
|
|
(if (and count (/= count 1))
|
|
|
|
|
(py-beginning-of-def-or-class (- 1 count)))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((start (progn (py-goto-initial-line) (point)))
|
1998-07-07 14:45:38 -03:00
|
|
|
|
(which (cond ((eq class 'either) "\\(class\\|def\\)")
|
|
|
|
|
(class "class")
|
|
|
|
|
(t "def")))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(state 'not-found))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
;; move point to start of appropriate def/class
|
|
|
|
|
(if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
|
|
|
|
|
(setq state 'at-beginning)
|
1998-05-19 12:31:46 -03:00
|
|
|
|
;; else see if py-beginning-of-def-or-class hits container
|
|
|
|
|
(if (and (py-beginning-of-def-or-class class)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(progn (py-goto-beyond-block)
|
|
|
|
|
(> (point) start)))
|
|
|
|
|
(setq state 'at-end)
|
|
|
|
|
;; else search forward
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
|
|
|
|
|
(progn (setq state 'at-beginning)
|
|
|
|
|
(beginning-of-line)))))
|
|
|
|
|
(cond
|
|
|
|
|
((eq state 'at-beginning) (py-goto-beyond-block) t)
|
|
|
|
|
((eq state 'at-end) t)
|
|
|
|
|
((eq state 'not-found) nil)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
(t (error "Internal error in `py-end-of-def-or-class'")))))
|
1998-05-19 12:31:46 -03:00
|
|
|
|
|
|
|
|
|
;; Backwards compabitility
|
1998-05-19 12:54:45 -03:00
|
|
|
|
(defalias 'end-of-python-def-or-class 'py-end-of-def-or-class)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;; Functions for marking regions
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-mark-block (&optional extend just-move)
|
|
|
|
|
"Mark following block of lines. With prefix arg, mark structure.
|
|
|
|
|
Easier to use than explain. It sets the region to an `interesting'
|
|
|
|
|
block of succeeding lines. If point is on a blank line, it goes down to
|
|
|
|
|
the next non-blank line. That will be the start of the region. The end
|
|
|
|
|
of the region depends on the kind of line at the start:
|
|
|
|
|
|
|
|
|
|
- If a comment, the region will include all succeeding comment lines up
|
|
|
|
|
to (but not including) the next non-comment line (if any).
|
|
|
|
|
|
|
|
|
|
- Else if a prefix arg is given, and the line begins one of these
|
|
|
|
|
structures:
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
if elif else try except finally for while def class
|
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
the region will be set to the body of the structure, including
|
|
|
|
|
following blocks that `belong' to it, but excluding trailing blank
|
|
|
|
|
and comment lines. E.g., if on a `try' statement, the `try' block
|
1995-03-08 17:33:04 -04:00
|
|
|
|
and all (if any) of the following `except' and `finally' blocks
|
|
|
|
|
that belong to the `try' structure will be in the region. Ditto
|
|
|
|
|
for if/elif/else, for/else and while/else structures, and (a bit
|
|
|
|
|
degenerate, since they're always one-block structures) def and
|
|
|
|
|
class blocks.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
- Else if no prefix argument is given, and the line begins a Python
|
1995-03-08 17:33:04 -04:00
|
|
|
|
block (see list above), and the block is not a `one-liner' (i.e.,
|
|
|
|
|
the statement ends with a colon, not with code), the region will
|
|
|
|
|
include all succeeding lines up to (but not including) the next
|
|
|
|
|
code statement (if any) that's indented no more than the starting
|
|
|
|
|
line, except that trailing blank and comment lines are excluded.
|
|
|
|
|
E.g., if the starting line begins a multi-statement `def'
|
|
|
|
|
structure, the region will be set to the full function definition,
|
|
|
|
|
but without any trailing `noise' lines.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
- Else the region will include all succeeding lines up to (but not
|
|
|
|
|
including) the next blank line, or code or indenting-comment line
|
|
|
|
|
indented strictly less than the starting line. Trailing indenting
|
|
|
|
|
comment lines are included in this case, but not trailing blank
|
|
|
|
|
lines.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
If called from a program, optional argument EXTEND plays the role of
|
|
|
|
|
the prefix arg, and if optional argument JUST-MOVE is not nil, just
|
|
|
|
|
moves to the end of the block (& does not set mark or display a msg)."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(interactive "P") ; raw prefix arg
|
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
;; skip over blank lines
|
|
|
|
|
(while (and
|
|
|
|
|
(looking-at "[ \t]*$") ; while blank line
|
|
|
|
|
(not (eobp))) ; & somewhere to go
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(if (eobp)
|
|
|
|
|
(error "Hit end of buffer without finding a non-blank stmt"))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((initial-pos (point))
|
|
|
|
|
(initial-indent (current-indentation))
|
|
|
|
|
last-pos ; position of last stmt in region
|
|
|
|
|
(followers
|
|
|
|
|
'((if elif else) (elif elif else) (else)
|
|
|
|
|
(try except finally) (except except) (finally)
|
|
|
|
|
(for else) (while else)
|
|
|
|
|
(def) (class) ) )
|
|
|
|
|
first-symbol next-symbol)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
;; if comment line, suck up the following comment lines
|
|
|
|
|
((looking-at "[ \t]*#")
|
|
|
|
|
(re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
|
|
|
|
|
(re-search-backward "^[ \t]*#") ; and back to last comment in block
|
|
|
|
|
(setq last-pos (point)))
|
|
|
|
|
|
|
|
|
|
;; else if line is a block line and EXTEND given, suck up
|
|
|
|
|
;; the whole structure
|
|
|
|
|
((and extend
|
|
|
|
|
(setq first-symbol (py-suck-up-first-keyword) )
|
|
|
|
|
(assq first-symbol followers))
|
|
|
|
|
(while (and
|
|
|
|
|
(or (py-goto-beyond-block) t) ; side effect
|
|
|
|
|
(forward-line -1) ; side effect
|
|
|
|
|
(setq last-pos (point)) ; side effect
|
|
|
|
|
(py-goto-statement-below)
|
|
|
|
|
(= (current-indentation) initial-indent)
|
|
|
|
|
(setq next-symbol (py-suck-up-first-keyword))
|
|
|
|
|
(memq next-symbol (cdr (assq first-symbol followers))))
|
|
|
|
|
(setq first-symbol next-symbol)))
|
|
|
|
|
|
|
|
|
|
;; else if line *opens* a block, search for next stmt indented <=
|
|
|
|
|
((py-statement-opens-block-p)
|
|
|
|
|
(while (and
|
|
|
|
|
(setq last-pos (point)) ; always true -- side effect
|
|
|
|
|
(py-goto-statement-below)
|
|
|
|
|
(> (current-indentation) initial-indent))
|
|
|
|
|
nil))
|
|
|
|
|
|
|
|
|
|
;; else plain code line; stop at next blank line, or stmt or
|
|
|
|
|
;; indenting comment line indented <
|
|
|
|
|
(t
|
|
|
|
|
(while (and
|
|
|
|
|
(setq last-pos (point)) ; always true -- side effect
|
|
|
|
|
(or (py-goto-beyond-final-line) t)
|
|
|
|
|
(not (looking-at "[ \t]*$")) ; stop at blank line
|
|
|
|
|
(or
|
|
|
|
|
(>= (current-indentation) initial-indent)
|
|
|
|
|
(looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
|
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
|
|
;; skip to end of last stmt
|
|
|
|
|
(goto-char last-pos)
|
|
|
|
|
(py-goto-beyond-final-line)
|
|
|
|
|
|
|
|
|
|
;; set mark & display
|
|
|
|
|
(if just-move
|
|
|
|
|
() ; just return
|
|
|
|
|
(push-mark (point) 'no-msg)
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(message "Mark set after: %s" (py-suck-up-leading-text))
|
|
|
|
|
(goto-char initial-pos))))
|
|
|
|
|
|
1997-11-04 20:51:08 -04:00
|
|
|
|
(defun py-mark-def-or-class (&optional class)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
"Set region to body of def (or class, with prefix arg) enclosing point.
|
|
|
|
|
Pushes the current mark, then point, on the mark ring (all language
|
|
|
|
|
modes do this, but although it's handy it's never documented ...).
|
|
|
|
|
|
|
|
|
|
In most Emacs language modes, this function bears at least a
|
1998-05-19 12:31:46 -03:00
|
|
|
|
hallucinogenic resemblance to `\\[py-end-of-def-or-class]' and
|
|
|
|
|
`\\[py-beginning-of-def-or-class]'.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
And in earlier versions of Python mode, all 3 were tightly connected.
|
1995-03-08 17:33:04 -04:00
|
|
|
|
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 expect them to act a lot like `search backward' and `search
|
|
|
|
|
forward' string-search commands. But because Python `def' and `class'
|
|
|
|
|
can nest to arbitrary levels, finding the smallest def containing
|
|
|
|
|
point cannot be done via a simple backward search: the def containing
|
|
|
|
|
point may not be the closest preceding def, or even the closest
|
|
|
|
|
preceding def that's indented less. The fancy algorithm required is
|
|
|
|
|
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, moves back to start of the closest preceding code statement or
|
|
|
|
|
indenting comment line. If this is a `def' statement, that's the def
|
|
|
|
|
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, except that: if the def is preceded by a number of comment lines
|
|
|
|
|
followed by (at most) one optional blank line, point is left at the
|
|
|
|
|
start of the comments; else if the def is preceded by a blank line,
|
|
|
|
|
point is left at its start.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
The intent is to mark the containing def/class and its associated
|
|
|
|
|
documentation, to make moving and duplicating functions and classes
|
|
|
|
|
pleasant."
|
|
|
|
|
(interactive "P") ; raw prefix arg
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((start (point))
|
1998-07-07 14:45:38 -03:00
|
|
|
|
(which (cond ((eq class 'either) "\\(class\\|def\\)")
|
|
|
|
|
(class "class")
|
|
|
|
|
(t "def"))))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(push-mark start)
|
|
|
|
|
(if (not (py-go-up-tree-to-keyword which))
|
|
|
|
|
(progn (goto-char start)
|
1998-07-07 14:45:38 -03:00
|
|
|
|
(error "Enclosing %s not found"
|
|
|
|
|
(if (eq class 'either)
|
|
|
|
|
"def or class"
|
|
|
|
|
which)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
;; else enclosing def/class found
|
|
|
|
|
(setq start (point))
|
|
|
|
|
(py-goto-beyond-block)
|
|
|
|
|
(push-mark (point))
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(if (zerop (forward-line -1)) ; if there is a preceding line
|
|
|
|
|
(progn
|
|
|
|
|
(if (looking-at "[ \t]*$") ; it's blank
|
|
|
|
|
(setq start (point)) ; so reset start point
|
|
|
|
|
(goto-char start)) ; else try again
|
|
|
|
|
(if (zerop (forward-line -1))
|
|
|
|
|
(if (looking-at "[ \t]*#") ; a comment
|
|
|
|
|
;; look back for non-comment line
|
|
|
|
|
;; tricky: note that the regexp matches a blank
|
|
|
|
|
;; line, cuz \n is in the 2nd character class
|
|
|
|
|
(and
|
|
|
|
|
(re-search-backward "^[ \t]*[^ \t#]" nil 'move)
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
;; no comment, so go back
|
1997-11-26 02:00:26 -04:00
|
|
|
|
(goto-char start)))))))
|
|
|
|
|
(exchange-point-and-mark)
|
|
|
|
|
(py-keep-region-active))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
1996-07-24 15:26:53 -03:00
|
|
|
|
;; ripped from cc-mode
|
|
|
|
|
(defun py-forward-into-nomenclature (&optional arg)
|
|
|
|
|
"Move forward to end of a nomenclature section or word.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
With \\[universal-argument] (programmatically, optional argument ARG),
|
|
|
|
|
do it that many times.
|
1996-07-24 15:26:53 -03:00
|
|
|
|
|
|
|
|
|
A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(let ((case-fold-search nil))
|
|
|
|
|
(if (> arg 0)
|
1996-08-05 18:53:02 -03:00
|
|
|
|
(re-search-forward
|
|
|
|
|
"\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
|
|
|
|
|
(point-max) t arg)
|
1996-07-24 15:26:53 -03:00
|
|
|
|
(while (and (< arg 0)
|
|
|
|
|
(re-search-backward
|
1996-08-05 18:53:02 -03:00
|
|
|
|
"\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
|
1996-07-24 15:26:53 -03:00
|
|
|
|
(point-min) 0))
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(setq arg (1+ arg)))))
|
|
|
|
|
(py-keep-region-active))
|
|
|
|
|
|
|
|
|
|
(defun py-backward-into-nomenclature (&optional arg)
|
|
|
|
|
"Move backward to beginning of a nomenclature section or word.
|
|
|
|
|
With optional ARG, move that many times. If ARG is negative, move
|
|
|
|
|
forward.
|
|
|
|
|
|
|
|
|
|
A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(py-forward-into-nomenclature (- arg))
|
|
|
|
|
(py-keep-region-active))
|
|
|
|
|
|
|
|
|
|
|
2001-04-11 17:23:17 -03:00
|
|
|
|
|
|
|
|
|
;; pdbtrack functions
|
|
|
|
|
(defun py-pdbtrack-toggle-stack-tracking (arg)
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(if (not (get-buffer-process (current-buffer)))
|
|
|
|
|
(error "No process associated with buffer '%s'" (current-buffer)))
|
|
|
|
|
;; missing or 0 is toggle, >0 turn on, <0 turn off
|
|
|
|
|
(if (or (not arg)
|
|
|
|
|
(zerop (setq arg (prefix-numeric-value arg))))
|
|
|
|
|
(setq py-pdbtrack-do-tracking-p (not py-pdbtrack-do-tracking-p))
|
|
|
|
|
(setq py-pdbtrack-do-tracking-p (> arg 0)))
|
|
|
|
|
(message "%sabled Python's pdbtrack"
|
|
|
|
|
(if py-pdbtrack-do-tracking-p "En" "Dis")))
|
|
|
|
|
|
|
|
|
|
(defun turn-on-pdbtrack ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(py-pdbtrack-toggle-stack-tracking 1))
|
|
|
|
|
|
|
|
|
|
(defun turn-off-pdbtrack ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(py-pdbtrack-toggle-stack-tracking 0))
|
|
|
|
|
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;; Documentation functions
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
;; dump the long form of the mode blurb; does the usual doc escapes,
|
1995-03-08 17:33:04 -04:00
|
|
|
|
;; plus lines of the form ^[vc]:name$ to suck variable & command docs
|
|
|
|
|
;; out of the right places, along with the keys they're on & current
|
|
|
|
|
;; values
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-dump-help-string (str)
|
|
|
|
|
(with-output-to-temp-buffer "*Help*"
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((locals (buffer-local-variables))
|
|
|
|
|
funckind funcname func funcdoc
|
|
|
|
|
(start 0) mstart end
|
|
|
|
|
keys )
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
|
|
|
|
|
(setq mstart (match-beginning 0) end (match-end 0)
|
|
|
|
|
funckind (substring str (match-beginning 1) (match-end 1))
|
|
|
|
|
funcname (substring str (match-beginning 2) (match-end 2))
|
|
|
|
|
func (intern funcname))
|
|
|
|
|
(princ (substitute-command-keys (substring str start mstart)))
|
|
|
|
|
(cond
|
1995-03-08 17:33:04 -04:00
|
|
|
|
((equal funckind "c") ; command
|
|
|
|
|
(setq funcdoc (documentation func)
|
|
|
|
|
keys (concat
|
|
|
|
|
"Key(s): "
|
|
|
|
|
(mapconcat 'key-description
|
|
|
|
|
(where-is-internal func py-mode-map)
|
|
|
|
|
", "))))
|
|
|
|
|
((equal funckind "v") ; variable
|
1996-09-03 15:17:04 -03:00
|
|
|
|
(setq funcdoc (documentation-property func 'variable-documentation)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
keys (if (assq func locals)
|
|
|
|
|
(concat
|
|
|
|
|
"Local/Global values: "
|
|
|
|
|
(prin1-to-string (symbol-value func))
|
|
|
|
|
" / "
|
|
|
|
|
(prin1-to-string (default-value func)))
|
|
|
|
|
(concat
|
|
|
|
|
"Value: "
|
|
|
|
|
(prin1-to-string (symbol-value func))))))
|
|
|
|
|
(t ; unexpected
|
|
|
|
|
(error "Error in py-dump-help-string, tag `%s'" funckind)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(princ (format "\n-> %s:\t%s\t%s\n\n"
|
|
|
|
|
(if (equal funckind "c") "Command" "Variable")
|
|
|
|
|
funcname keys))
|
|
|
|
|
(princ funcdoc)
|
|
|
|
|
(terpri)
|
|
|
|
|
(setq start end))
|
|
|
|
|
(princ (substitute-command-keys (substring str start))))
|
|
|
|
|
(print-help-return-message)))
|
|
|
|
|
|
|
|
|
|
(defun py-describe-mode ()
|
|
|
|
|
"Dump long form of Python-mode docs."
|
|
|
|
|
(interactive)
|
|
|
|
|
(py-dump-help-string "Major mode for editing Python files.
|
|
|
|
|
Knows about Python indentation, tokens, comments and continuation lines.
|
|
|
|
|
Paragraphs are separated by blank lines only.
|
|
|
|
|
|
|
|
|
|
Major sections below begin with the string `@'; specific function and
|
|
|
|
|
variable docs begin with `->'.
|
|
|
|
|
|
|
|
|
|
@EXECUTING PYTHON CODE
|
|
|
|
|
|
1998-05-19 12:54:45 -03:00
|
|
|
|
\\[py-execute-import-or-reload]\timports or reloads the file in the Python interpreter
|
1994-12-12 16:38:05 -04:00
|
|
|
|
\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
|
|
|
|
|
\\[py-execute-region]\tsends the current region
|
1998-05-19 12:54:45 -03:00
|
|
|
|
\\[py-execute-def-or-class]\tsends the current function or class definition
|
|
|
|
|
\\[py-execute-string]\tsends an arbitrary string
|
1994-12-12 16:38:05 -04:00
|
|
|
|
\\[py-shell]\tstarts a Python interpreter window; this will be used by
|
1998-05-19 12:54:45 -03:00
|
|
|
|
\tsubsequent Python execution commands
|
|
|
|
|
%c:py-execute-import-or-reload
|
1994-12-12 16:38:05 -04:00
|
|
|
|
%c:py-execute-buffer
|
|
|
|
|
%c:py-execute-region
|
1998-05-19 12:54:45 -03:00
|
|
|
|
%c:py-execute-def-or-class
|
|
|
|
|
%c:py-execute-string
|
1994-12-12 16:38:05 -04:00
|
|
|
|
%c:py-shell
|
|
|
|
|
|
|
|
|
|
@VARIABLES
|
|
|
|
|
|
|
|
|
|
py-indent-offset\tindentation increment
|
1996-07-29 18:05:05 -03:00
|
|
|
|
py-block-comment-prefix\tcomment string used by comment-region
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
py-python-command\tshell command to invoke Python interpreter
|
|
|
|
|
py-temp-directory\tdirectory used for temp files (if needed)
|
|
|
|
|
|
|
|
|
|
py-beep-if-tab-change\tring the bell if tab-width is changed
|
|
|
|
|
%v:py-indent-offset
|
|
|
|
|
%v:py-block-comment-prefix
|
|
|
|
|
%v:py-python-command
|
|
|
|
|
%v:py-temp-directory
|
|
|
|
|
%v:py-beep-if-tab-change
|
|
|
|
|
|
|
|
|
|
@KINDS OF LINES
|
|
|
|
|
|
|
|
|
|
Each physical line in the file is either a `continuation line' (the
|
1995-03-08 17:33:04 -04:00
|
|
|
|
preceding line ends with a backslash that's not part of a comment, or
|
|
|
|
|
the paren/bracket/brace nesting level at the start of the line is
|
|
|
|
|
non-zero, or both) or an `initial line' (everything else).
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
An initial line is in turn a `blank line' (contains nothing except
|
1995-03-08 17:33:04 -04:00
|
|
|
|
possibly blanks or tabs), a `comment line' (leftmost non-blank
|
|
|
|
|
character is `#'), or a `code line' (everything else).
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Comment Lines
|
|
|
|
|
|
|
|
|
|
Although all comment lines are treated alike by Python, Python mode
|
|
|
|
|
recognizes two kinds that act differently with respect to indentation.
|
|
|
|
|
|
|
|
|
|
An `indenting comment line' is a comment line with a blank, tab or
|
|
|
|
|
nothing after the initial `#'. The indentation commands (see below)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
treat these exactly as if they were code lines: a line following an
|
1994-12-12 16:38:05 -04:00
|
|
|
|
indenting comment line will be indented like the comment line. All
|
|
|
|
|
other comment lines (those with a non-whitespace character immediately
|
1995-03-08 17:33:04 -04:00
|
|
|
|
following the initial `#') are `non-indenting comment lines', and
|
|
|
|
|
their indentation is ignored by the indentation commands.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Indenting comment lines are by far the usual case, and should be used
|
1995-03-08 17:33:04 -04:00
|
|
|
|
whenever possible. Non-indenting comment lines are useful in cases
|
|
|
|
|
like these:
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
\ta = b # a very wordy single-line comment that ends up being
|
|
|
|
|
\t #... continued onto another line
|
|
|
|
|
|
|
|
|
|
\tif a == b:
|
|
|
|
|
##\t\tprint 'panic!' # old code we've `commented out'
|
|
|
|
|
\t\treturn a
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
Since the `#...' and `##' comment lines have a non-whitespace
|
|
|
|
|
character following the initial `#', Python mode ignores them when
|
|
|
|
|
computing the proper indentation for the next line.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Continuation Lines and Statements
|
|
|
|
|
|
|
|
|
|
The Python-mode commands generally work on statements instead of on
|
|
|
|
|
individual lines, where a `statement' is a comment or blank line, or a
|
|
|
|
|
code line and all of its following continuation lines (if any)
|
|
|
|
|
considered as a single logical unit. The commands in this mode
|
|
|
|
|
generally (when it makes sense) automatically move to the start of the
|
1995-03-08 17:33:04 -04:00
|
|
|
|
statement containing point, even if point happens to be in the middle
|
|
|
|
|
of some continuation line.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@INDENTATION
|
|
|
|
|
|
|
|
|
|
Primarily for entering new code:
|
|
|
|
|
\t\\[indent-for-tab-command]\t indent line appropriately
|
|
|
|
|
\t\\[py-newline-and-indent]\t insert newline, then indent
|
1997-12-03 01:03:44 -04:00
|
|
|
|
\t\\[py-electric-backspace]\t reduce indentation, or delete single character
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Primarily for reindenting existing code:
|
|
|
|
|
\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
|
|
|
|
|
\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
|
|
|
|
|
|
|
|
|
|
\t\\[py-indent-region]\t reindent region to match its context
|
|
|
|
|
\t\\[py-shift-region-left]\t shift region left by py-indent-offset
|
|
|
|
|
\t\\[py-shift-region-right]\t shift region right by py-indent-offset
|
|
|
|
|
|
|
|
|
|
Unlike most programming languages, Python uses indentation, and only
|
|
|
|
|
indentation, to specify block structure. Hence the indentation supplied
|
|
|
|
|
automatically by Python-mode is just an educated guess: only you know
|
|
|
|
|
the block structure you intend, so only you can supply correct
|
|
|
|
|
indentation.
|
|
|
|
|
|
|
|
|
|
The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
|
|
|
|
|
the indentation of preceding statements. E.g., assuming
|
|
|
|
|
py-indent-offset is 4, after you enter
|
|
|
|
|
\tif a > 0: \\[py-newline-and-indent]
|
|
|
|
|
the cursor will be moved to the position of the `_' (_ is not a
|
|
|
|
|
character in the file, it's just used here to indicate the location of
|
|
|
|
|
the cursor):
|
|
|
|
|
\tif a > 0:
|
|
|
|
|
\t _
|
|
|
|
|
If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
|
|
|
|
|
to
|
|
|
|
|
\tif a > 0:
|
|
|
|
|
\t c = d
|
|
|
|
|
\t _
|
|
|
|
|
Python-mode cannot know whether that's what you intended, or whether
|
|
|
|
|
\tif a > 0:
|
|
|
|
|
\t c = d
|
|
|
|
|
\t_
|
|
|
|
|
was your intent. In general, Python-mode either reproduces the
|
|
|
|
|
indentation of the (closest code or indenting-comment) preceding
|
|
|
|
|
statement, or adds an extra py-indent-offset blanks if the preceding
|
|
|
|
|
statement has `:' as its last significant (non-whitespace and non-
|
|
|
|
|
comment) character. If the suggested indentation is too much, use
|
1997-12-03 01:03:44 -04:00
|
|
|
|
\\[py-electric-backspace] to reduce it.
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
Continuation lines are given extra indentation. If you don't like the
|
|
|
|
|
suggested indentation, change it to something you do like, and Python-
|
|
|
|
|
mode will strive to indent later lines of the statement in the same way.
|
|
|
|
|
|
|
|
|
|
If a line is a continuation line by virtue of being in an unclosed
|
|
|
|
|
paren/bracket/brace structure (`list', for short), the suggested
|
|
|
|
|
indentation depends on whether the current line contains the first item
|
|
|
|
|
in the list. If it does, it's indented py-indent-offset columns beyond
|
|
|
|
|
the indentation of the line containing the open bracket. If you don't
|
|
|
|
|
like that, change it by hand. The remaining items in the list will mimic
|
|
|
|
|
whatever indentation you give to the first item.
|
|
|
|
|
|
|
|
|
|
If a line is a continuation line because the line preceding it ends with
|
|
|
|
|
a backslash, the third and following lines of the statement inherit their
|
|
|
|
|
indentation from the line preceding them. The indentation of the second
|
|
|
|
|
line in the statement depends on the form of the first (base) line: if
|
|
|
|
|
the base line is an assignment statement with anything more interesting
|
|
|
|
|
than the backslash following the leftmost assigning `=', the second line
|
|
|
|
|
is indented two columns beyond that `='. Else it's indented to two
|
|
|
|
|
columns beyond the leftmost solid chunk of non-whitespace characters on
|
|
|
|
|
the base line.
|
|
|
|
|
|
|
|
|
|
Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
|
|
|
|
|
repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
|
|
|
|
|
structure you intend.
|
|
|
|
|
%c:indent-for-tab-command
|
|
|
|
|
%c:py-newline-and-indent
|
1997-12-03 01:03:44 -04:00
|
|
|
|
%c:py-electric-backspace
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The next function may be handy when editing code you didn't write:
|
|
|
|
|
%c:py-guess-indent-offset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The remaining `indent' functions apply to a region of Python code. They
|
|
|
|
|
assume the block structure (equals indentation, in Python) of the region
|
|
|
|
|
is correct, and alter the indentation in various ways while preserving
|
|
|
|
|
the block structure:
|
|
|
|
|
%c:py-indent-region
|
|
|
|
|
%c:py-shift-region-left
|
|
|
|
|
%c:py-shift-region-right
|
|
|
|
|
|
|
|
|
|
@MARKING & MANIPULATING REGIONS OF CODE
|
|
|
|
|
|
|
|
|
|
\\[py-mark-block]\t mark block of lines
|
1997-11-04 20:51:08 -04:00
|
|
|
|
\\[py-mark-def-or-class]\t mark smallest enclosing def
|
|
|
|
|
\\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
|
1996-07-29 18:05:05 -03:00
|
|
|
|
\\[comment-region]\t comment out region of code
|
|
|
|
|
\\[universal-argument] \\[comment-region]\t uncomment region of code
|
1994-12-12 16:38:05 -04:00
|
|
|
|
%c:py-mark-block
|
1997-11-04 20:51:08 -04:00
|
|
|
|
%c:py-mark-def-or-class
|
1996-07-29 18:05:05 -03:00
|
|
|
|
%c:comment-region
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
@MOVING POINT
|
|
|
|
|
|
|
|
|
|
\\[py-previous-statement]\t move to statement preceding point
|
|
|
|
|
\\[py-next-statement]\t move to statement following point
|
|
|
|
|
\\[py-goto-block-up]\t move up to start of current block
|
1998-05-19 12:31:46 -03:00
|
|
|
|
\\[py-beginning-of-def-or-class]\t move to start of def
|
|
|
|
|
\\[universal-argument] \\[py-beginning-of-def-or-class]\t move to start of class
|
|
|
|
|
\\[py-end-of-def-or-class]\t move to end of def
|
|
|
|
|
\\[universal-argument] \\[py-end-of-def-or-class]\t move to end of class
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
The first two move to one statement beyond the statement that contains
|
|
|
|
|
point. A numeric prefix argument tells them to move that many
|
|
|
|
|
statements instead. Blank lines, comment lines, and continuation lines
|
|
|
|
|
do not count as `statements' for these commands. So, e.g., you can go
|
|
|
|
|
to the first code statement in a file by entering
|
|
|
|
|
\t\\[beginning-of-buffer]\t to move to the top of the file
|
|
|
|
|
\t\\[py-next-statement]\t to skip over initial comments and blank lines
|
|
|
|
|
Or do `\\[py-previous-statement]' with a huge prefix argument.
|
|
|
|
|
%c:py-previous-statement
|
|
|
|
|
%c:py-next-statement
|
|
|
|
|
%c:py-goto-block-up
|
1998-05-19 12:31:46 -03:00
|
|
|
|
%c:py-beginning-of-def-or-class
|
|
|
|
|
%c:py-end-of-def-or-class
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
|
|
|
|
|
|
|
|
|
|
`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
|
|
|
|
|
|
|
|
|
|
`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
|
|
|
|
|
overall class and def structure of a module.
|
|
|
|
|
|
|
|
|
|
`\\[back-to-indentation]' moves point to a line's first non-blank character.
|
|
|
|
|
|
|
|
|
|
`\\[indent-relative]' is handy for creating odd indentation.
|
|
|
|
|
|
|
|
|
|
@OTHER EMACS HINTS
|
|
|
|
|
|
|
|
|
|
If you don't like the default value of a variable, change its value to
|
|
|
|
|
whatever you do like by putting a `setq' line in your .emacs file.
|
|
|
|
|
E.g., to set the indentation increment to 4, put this line in your
|
|
|
|
|
.emacs:
|
|
|
|
|
\t(setq py-indent-offset 4)
|
|
|
|
|
To see the value of a variable, do `\\[describe-variable]' and enter the variable
|
|
|
|
|
name at the prompt.
|
|
|
|
|
|
|
|
|
|
When entering a key sequence like `C-c C-n', it is not necessary to
|
|
|
|
|
release the CONTROL key after doing the `C-c' part -- it suffices to
|
|
|
|
|
press the CONTROL key, press and release `c' (while still holding down
|
|
|
|
|
CONTROL), press and release `n' (while still holding down CONTROL), &
|
|
|
|
|
then release CONTROL.
|
|
|
|
|
|
|
|
|
|
Entering Python mode calls with no arguments the value of the variable
|
|
|
|
|
`python-mode-hook', if that value exists and is not nil; for backward
|
|
|
|
|
compatibility it also tries `py-mode-hook'; see the `Hooks' section of
|
|
|
|
|
the Elisp manual for details.
|
|
|
|
|
|
|
|
|
|
Obscure: When python-mode is first loaded, it looks for all bindings
|
|
|
|
|
to newline-and-indent in the global keymap, and shadows them with
|
|
|
|
|
local bindings to py-newline-and-indent."))
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
;; Helper functions
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defvar py-parse-state-re
|
|
|
|
|
(concat
|
|
|
|
|
"^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
|
|
|
|
|
"\\|"
|
|
|
|
|
"^[^ #\t\n]"))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-parse-state ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return the parse state at point (see `parse-partial-sexp' docs)."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
1996-04-05 20:00:19 -04:00
|
|
|
|
(let ((here (point))
|
2001-02-23 20:09:17 -04:00
|
|
|
|
in-listcomp pps done)
|
1996-04-05 20:00:19 -04:00
|
|
|
|
(while (not done)
|
|
|
|
|
;; back up to the first preceding line (if any; else start of
|
|
|
|
|
;; buffer) that begins with a popular Python keyword, or a
|
|
|
|
|
;; non- whitespace and non-comment character. These are good
|
|
|
|
|
;; places to start parsing to see whether where we started is
|
|
|
|
|
;; at a non-zero nesting level. It may be slow for people who
|
|
|
|
|
;; write huge code blocks or huge lists ... tough beans.
|
|
|
|
|
(re-search-backward py-parse-state-re nil 'move)
|
2001-02-23 20:09:17 -04:00
|
|
|
|
;; Watch out for landing inside a list comprehension
|
|
|
|
|
(save-excursion
|
|
|
|
|
(if (and (looking-at "[ \t]*\\<\\(if\\|for\\)\\>")
|
|
|
|
|
(py-safe (progn (up-list -1) t))
|
|
|
|
|
(eq (char-after) ?\[))
|
|
|
|
|
(setq in-listcomp (point))
|
|
|
|
|
(setq in-listcomp nil)))
|
1996-04-05 20:00:19 -04:00
|
|
|
|
(beginning-of-line)
|
1998-02-12 12:52:14 -04:00
|
|
|
|
;; In XEmacs, we have a much better way to test for whether
|
|
|
|
|
;; we're in a triple-quoted string or not. Emacs does not
|
1998-05-19 11:49:49 -03:00
|
|
|
|
;; have this built-in function, which is its loss because
|
1998-02-12 12:52:14 -04:00
|
|
|
|
;; without scanning from the beginning of the buffer, there's
|
|
|
|
|
;; no accurate way to determine this otherwise.
|
|
|
|
|
(if (not (fboundp 'buffer-syntactic-context))
|
|
|
|
|
;; Emacs
|
1998-04-01 17:13:51 -04:00
|
|
|
|
(progn
|
|
|
|
|
(save-excursion (setq pps (parse-partial-sexp (point) here)))
|
1998-02-12 12:52:14 -04:00
|
|
|
|
;; make sure we don't land inside a triple-quoted string
|
1998-03-13 13:29:15 -04:00
|
|
|
|
(setq done (or (not (nth 3 pps))
|
1999-05-24 16:57:32 -03:00
|
|
|
|
(bobp)))
|
|
|
|
|
;; Just go ahead and short circuit the test back to the
|
|
|
|
|
;; beginning of the buffer. This will be slow, but not
|
|
|
|
|
;; nearly as slow as looping through many
|
|
|
|
|
;; re-search-backwards.
|
|
|
|
|
(if (not done)
|
|
|
|
|
(goto-char (point-min))))
|
1998-02-12 12:52:14 -04:00
|
|
|
|
;; XEmacs
|
|
|
|
|
(setq done (or (not (buffer-syntactic-context))
|
|
|
|
|
(bobp)))
|
2001-02-23 20:09:17 -04:00
|
|
|
|
(when in-listcomp
|
|
|
|
|
(goto-char in-listcomp)
|
|
|
|
|
(setq done nil))
|
1998-02-12 12:52:14 -04:00
|
|
|
|
(when done
|
|
|
|
|
(setq pps (parse-partial-sexp (point) here)))
|
|
|
|
|
))
|
1996-04-05 20:00:19 -04:00
|
|
|
|
pps)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
|
|
|
|
|
(defun py-nesting-level ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return the buffer position of the last unclosed enclosing list.
|
|
|
|
|
If nesting level is zero, return nil."
|
1998-02-12 12:52:14 -04:00
|
|
|
|
(let ((status (py-parse-state)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (zerop (car status))
|
|
|
|
|
nil ; not in a nest
|
|
|
|
|
(car (cdr status))))) ; char# of open bracket
|
|
|
|
|
|
|
|
|
|
(defun py-backslash-continuation-line-p ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return t iff preceding line ends with backslash that is not in a comment."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(and
|
|
|
|
|
;; use a cheap test first to avoid the regexp if possible
|
|
|
|
|
;; use 'eq' because char-after may return nil
|
|
|
|
|
(eq (char-after (- (point) 2)) ?\\ )
|
|
|
|
|
;; make sure; since eq test passed, there is a preceding line
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(forward-line -1) ; always true -- side effect
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(looking-at py-continued-re))))
|
|
|
|
|
|
|
|
|
|
(defun py-continuation-line-p ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return t iff current line is a continuation line."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(beginning-of-line)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(or (py-backslash-continuation-line-p)
|
|
|
|
|
(py-nesting-level))))
|
|
|
|
|
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(defun py-goto-beginning-of-tqs (delim)
|
|
|
|
|
"Go to the beginning of the triple quoted string we find ourselves in.
|
|
|
|
|
DELIM is the TQS string delimiter character we're searching backwards
|
|
|
|
|
for."
|
2000-10-27 02:00:25 -03:00
|
|
|
|
(let ((skip (and delim (make-string 1 delim)))
|
|
|
|
|
(continue t))
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(when skip
|
|
|
|
|
(save-excursion
|
2000-10-27 02:00:25 -03:00
|
|
|
|
(while continue
|
|
|
|
|
(py-safe (search-backward skip))
|
|
|
|
|
(setq continue (and (not (bobp))
|
|
|
|
|
(= (char-before) ?\\))))
|
|
|
|
|
(if (and (= (char-before) delim)
|
|
|
|
|
(= (char-before (1- (point))) delim))
|
1998-12-15 00:36:22 -04:00
|
|
|
|
(setq skip (make-string 3 delim))))
|
|
|
|
|
;; we're looking at a triple-quoted string
|
|
|
|
|
(py-safe (search-backward skip)))))
|
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-goto-initial-line ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to the initial line of the current statement.
|
|
|
|
|
Usually this is the line 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."
|
|
|
|
|
;; Tricky: We want to avoid quadratic-time behavior for long
|
|
|
|
|
;; continued blocks, whether of the backslash or open-bracket
|
|
|
|
|
;; varieties, or a mix of the two. The following manages to do that
|
|
|
|
|
;; in the usual cases.
|
|
|
|
|
;;
|
|
|
|
|
;; Also, if we're sitting inside a triple quoted string, this will
|
|
|
|
|
;; drop us at the line that begins the string.
|
1998-01-21 01:15:57 -04:00
|
|
|
|
(let (open-bracket-pos)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (py-continuation-line-p)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(if (py-backslash-continuation-line-p)
|
|
|
|
|
(while (py-backslash-continuation-line-p)
|
|
|
|
|
(forward-line -1))
|
|
|
|
|
;; else zip out of nested brackets/braces/parens
|
|
|
|
|
(while (setq open-bracket-pos (py-nesting-level))
|
|
|
|
|
(goto-char open-bracket-pos)))))
|
|
|
|
|
(beginning-of-line))
|
|
|
|
|
|
|
|
|
|
(defun py-goto-beyond-final-line ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to the point just beyond the fine line of the current statement.
|
|
|
|
|
Usually this is the start of the next line, but if this is a
|
|
|
|
|
multi-line statement we need to skip over the continuation lines."
|
|
|
|
|
;; Tricky: Again we need to be clever to avoid quadratic time
|
|
|
|
|
;; behavior.
|
|
|
|
|
;;
|
|
|
|
|
;; XXX: Not quite the right solution, but deals with multi-line doc
|
|
|
|
|
;; strings
|
1998-05-19 13:06:21 -03:00
|
|
|
|
(if (looking-at (concat "[ \t]*\\(" py-stringlit-re "\\)"))
|
|
|
|
|
(goto-char (match-end 0)))
|
|
|
|
|
;;
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(forward-line 1)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let (state)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (and (py-continuation-line-p)
|
|
|
|
|
(not (eobp)))
|
|
|
|
|
;; skip over the backslash flavor
|
|
|
|
|
(while (and (py-backslash-continuation-line-p)
|
|
|
|
|
(not (eobp)))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
;; if in nest, zip to the end of the nest
|
|
|
|
|
(setq state (py-parse-state))
|
|
|
|
|
(if (and (not (zerop (car state)))
|
|
|
|
|
(not (eobp)))
|
|
|
|
|
(progn
|
1997-11-03 12:59:38 -04:00
|
|
|
|
(parse-partial-sexp (point) (point-max) 0 nil state)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(forward-line 1))))))
|
|
|
|
|
|
|
|
|
|
(defun py-statement-opens-block-p ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return t iff the current statement opens a block.
|
|
|
|
|
I.e., iff it ends with a colon that is not in a comment. Point should
|
|
|
|
|
be at the start of a statement."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((start (point))
|
|
|
|
|
(finish (progn (py-goto-beyond-final-line) (1- (point))))
|
|
|
|
|
(searching t)
|
|
|
|
|
(answer nil)
|
|
|
|
|
state)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(goto-char start)
|
|
|
|
|
(while searching
|
|
|
|
|
;; look for a colon with nothing after it except whitespace, and
|
|
|
|
|
;; maybe a comment
|
|
|
|
|
(if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
|
|
|
|
|
finish t)
|
|
|
|
|
(if (eq (point) finish) ; note: no `else' clause; just
|
|
|
|
|
; keep searching if we're not at
|
|
|
|
|
; the end yet
|
|
|
|
|
;; sure looks like it opens a block -- but it might
|
|
|
|
|
;; be in a comment
|
|
|
|
|
(progn
|
|
|
|
|
(setq searching nil) ; search is done either way
|
|
|
|
|
(setq state (parse-partial-sexp start
|
|
|
|
|
(match-beginning 0)))
|
|
|
|
|
(setq answer (not (nth 4 state)))))
|
|
|
|
|
;; search failed: couldn't find another interesting colon
|
|
|
|
|
(setq searching nil)))
|
|
|
|
|
answer)))
|
|
|
|
|
|
1996-07-31 17:42:59 -03:00
|
|
|
|
(defun py-statement-closes-block-p ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return t iff the current statement closes a block.
|
|
|
|
|
I.e., if the line starts with `return', `raise', `break', `continue',
|
|
|
|
|
and `pass'. This doesn't catch embedded statements."
|
1996-07-31 17:42:59 -03:00
|
|
|
|
(let ((here (point)))
|
1999-05-24 15:37:57 -03:00
|
|
|
|
(py-goto-initial-line)
|
1999-06-03 19:18:59 -03:00
|
|
|
|
(back-to-indentation)
|
1996-07-31 17:42:59 -03:00
|
|
|
|
(prog1
|
1997-11-03 12:59:38 -04:00
|
|
|
|
(looking-at (concat py-block-closing-keywords-re "\\>"))
|
1996-07-31 17:42:59 -03:00
|
|
|
|
(goto-char here))))
|
|
|
|
|
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(defun py-goto-beyond-block ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to point just beyond the final line of block begun by the current 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 block.
|
|
|
|
|
Assumes point is at the beginning of the line."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (py-statement-opens-block-p)
|
|
|
|
|
(py-mark-block nil 'just-move)
|
|
|
|
|
(py-goto-beyond-final-line)))
|
|
|
|
|
|
|
|
|
|
(defun py-goto-statement-at-or-above ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to the start of the first statement at or preceding point.
|
|
|
|
|
Return t if there is such a statement, otherwise nil. `Statement'
|
|
|
|
|
does not include blank lines, comments, or continuation lines."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(if (looking-at py-blank-or-comment-re)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
;; skip back over blank & comment lines
|
|
|
|
|
;; note: will skip a blank or comment line that happens to be
|
|
|
|
|
;; a continuation line too
|
|
|
|
|
(if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
|
|
|
|
|
(progn (py-goto-initial-line) t)
|
|
|
|
|
nil)
|
1994-12-12 16:38:05 -04:00
|
|
|
|
t))
|
|
|
|
|
|
|
|
|
|
(defun py-goto-statement-below ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to start of the first statement following the statement containing point.
|
|
|
|
|
Return t if there is such a statement, otherwise nil. `Statement'
|
|
|
|
|
does not include blank lines, comments, or continuation lines."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(beginning-of-line)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((start (point)))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(py-goto-beyond-final-line)
|
|
|
|
|
(while (and
|
|
|
|
|
(looking-at py-blank-or-comment-re)
|
|
|
|
|
(not (eobp)))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(if (eobp)
|
|
|
|
|
(progn (goto-char start) nil)
|
|
|
|
|
t)))
|
|
|
|
|
|
|
|
|
|
(defun py-go-up-tree-to-keyword (key)
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Go to begining of statement starting with KEY, at or preceding point.
|
|
|
|
|
|
|
|
|
|
KEY is a regular expression describing a Python keyword. Skip blank
|
|
|
|
|
lines and non-indenting comments. If the statement found starts with
|
|
|
|
|
KEY, then stop, otherwise go back to first enclosing block starting
|
|
|
|
|
with KEY. If successful, leave point at the start of the KEY line and
|
|
|
|
|
return t. Otherwise, leav point at an undefined place and return nil."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
;; skip blanks and non-indenting #
|
|
|
|
|
(py-goto-initial-line)
|
|
|
|
|
(while (and
|
|
|
|
|
(looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
|
|
|
|
|
(zerop (forward-line -1))) ; go back
|
|
|
|
|
nil)
|
|
|
|
|
(py-goto-initial-line)
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let* ((re (concat "[ \t]*" key "\\b"))
|
|
|
|
|
(case-fold-search nil) ; let* so looking-at sees this
|
|
|
|
|
(found (looking-at re))
|
|
|
|
|
(dead nil))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(while (not (or found dead))
|
|
|
|
|
(condition-case nil ; in case no enclosing block
|
|
|
|
|
(py-goto-block-up 'no-mark)
|
|
|
|
|
(error (setq dead t)))
|
|
|
|
|
(or dead (setq found (looking-at re))))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
found))
|
|
|
|
|
|
|
|
|
|
(defun py-suck-up-leading-text ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return string in buffer from start of indentation to end of line.
|
|
|
|
|
Prefix with \"...\" if leading whitespace was skipped."
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(save-excursion
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(concat
|
|
|
|
|
(if (bolp) "" "...")
|
|
|
|
|
(buffer-substring (point) (progn (end-of-line) (point))))))
|
|
|
|
|
|
|
|
|
|
(defun py-suck-up-first-keyword ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Return first keyword on the line as a Lisp symbol.
|
|
|
|
|
`Keyword' is defined (essentially) as the regular expression
|
|
|
|
|
([a-z]+). Returns nil if none was found."
|
1995-03-08 17:33:04 -04:00
|
|
|
|
(let ((case-fold-search nil))
|
1994-12-12 16:38:05 -04:00
|
|
|
|
(if (looking-at "[ \t]*\\([a-z]+\\)\\b")
|
|
|
|
|
(intern (buffer-substring (match-beginning 1) (match-end 1)))
|
|
|
|
|
nil)))
|
|
|
|
|
|
1996-09-04 12:12:42 -03:00
|
|
|
|
(defun py-current-defun ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Python value for `add-log-current-defun-function'.
|
|
|
|
|
This tells add-log.el how to find the current function/method/variable."
|
1996-09-04 12:12:42 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(if (re-search-backward py-defun-start-re nil t)
|
|
|
|
|
(or (match-string 3)
|
|
|
|
|
(let ((method (match-string 2)))
|
|
|
|
|
(if (and (not (zerop (length (match-string 1))))
|
|
|
|
|
(re-search-backward py-class-start-re nil t))
|
|
|
|
|
(concat (match-string 1) "." method)
|
|
|
|
|
method)))
|
|
|
|
|
nil)))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
|
1995-07-05 20:26:15 -03:00
|
|
|
|
(defconst py-help-address "python-mode@python.org"
|
1995-03-08 17:50:28 -04:00
|
|
|
|
"Address accepting submission of bug reports.")
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
1995-03-08 17:50:28 -04:00
|
|
|
|
(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'.
|
1998-08-10 13:33:12 -03:00
|
|
|
|
With \\[universal-argument] (programmatically, argument ENHANCEMENT-P
|
|
|
|
|
non-nil) just submit an enhancement request."
|
1995-03-08 17:50:28 -04:00
|
|
|
|
(interactive
|
|
|
|
|
(list (not (y-or-n-p
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Is this a bug report (hit `n' to send other comments)? "))))
|
1995-03-14 14:32:54 -04:00
|
|
|
|
(let ((reporter-prompt-for-summary-p (if enhancement-p
|
|
|
|
|
"(Very) brief summary: "
|
|
|
|
|
t)))
|
1995-03-08 17:50:28 -04:00
|
|
|
|
(require 'reporter)
|
|
|
|
|
(reporter-submit-bug-report
|
|
|
|
|
py-help-address ;address
|
1995-03-14 14:32:54 -04:00
|
|
|
|
(concat "python-mode " py-version) ;pkgname
|
1995-03-08 17:50:28 -04:00
|
|
|
|
;; varlist
|
|
|
|
|
(if enhancement-p nil
|
|
|
|
|
'(py-python-command
|
|
|
|
|
py-indent-offset
|
|
|
|
|
py-block-comment-prefix
|
|
|
|
|
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))))
|
|
|
|
|
|
|
|
|
|
|
1997-11-26 01:27:45 -04:00
|
|
|
|
(defun py-kill-emacs-hook ()
|
1998-08-10 13:33:12 -03:00
|
|
|
|
"Delete files in `py-file-queue'.
|
|
|
|
|
These are Python temporary files awaiting execution."
|
1997-11-26 01:27:45 -04:00
|
|
|
|
(mapcar #'(lambda (filename)
|
|
|
|
|
(py-safe (delete-file filename)))
|
|
|
|
|
py-file-queue))
|
|
|
|
|
|
1995-03-08 17:33:04 -04:00
|
|
|
|
;; arrange to kill temp files when Emacs exists
|
1997-08-09 03:42:08 -03:00
|
|
|
|
(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
|
2001-04-11 17:23:17 -03:00
|
|
|
|
(add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
|
|
|
|
|
|
|
|
|
|
;; Add a designator to the minor mode strings
|
|
|
|
|
(or (assq 'py-pdbtrack-minor-mode-string minor-mode-alist)
|
|
|
|
|
(push '(py-pdbtrack-is-tracking-p py-pdbtrack-minor-mode-string)
|
|
|
|
|
minor-mode-alist))
|
1995-03-08 17:33:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(provide 'python-mode)
|
|
|
|
|
;;; python-mode.el ends here
|