blob: aaf5c9eb9f45332e8518fd4d5aabbdd53cce2f80 [file] [log] [blame]
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
Barry Warsaw71534602001-02-20 23:07:56 +00005;; Author: 1995-2001 Barry A. Warsaw
Barry Warsawfec75d61995-07-05 23:26:15 +00006;; 1992-1994 Tim Peters
Barry Warsawa97a3f31997-11-04 18:47:06 +00007;; Maintainer: python-mode@python.org
8;; Created: Feb 1992
9;; Keywords: python languages oop
Barry Warsaw7b0f5681995-03-08 21:33:04 +000010
Barry Warsaw19b1c612001-07-06 20:27:29 +000011(defconst py-version "$Revision$"
Barry Warsawc72c11c1997-08-09 06:42:08 +000012 "`python-mode' version number.")
13
Barry Warsawcfec3591995-03-10 15:58:16 +000014;; This software is provided as-is, without express or implied
15;; warranty. Permission to use, copy, modify, distribute or sell this
16;; software, without fee, for any purpose and by any individual or
17;; organization, is hereby granted, provided that the above copyright
18;; notice and this paragraph appear in all copies.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000019
20;;; Commentary:
Barry Warsaw755c6711996-08-01 20:02:55 +000021
Barry Warsaw7b0f5681995-03-08 21:33:04 +000022;; This is a major mode for editing Python programs. It was developed
Barry Warsaw261f87d1996-08-20 19:57:34 +000023;; by Tim Peters after an original idea by Michael A. Guravage. Tim
Barry Warsawf7039e21998-08-20 22:10:46 +000024;; subsequently left the net; in 1995, Barry Warsaw inherited the mode
Barry Warsaw38e21e71998-10-27 22:09:25 +000025;; and is the current maintainer. Tim's now back but disavows all
26;; responsibility for the mode. Smart Tim :-)
Barry Warsaw7b0f5681995-03-08 21:33:04 +000027
Barry Warsaw4f577d22001-04-11 20:23:17 +000028;; pdbtrack support contributed by Ken Manheimer, April 2001.
29
30;; This version of python-mode.el has only been tested with XEmacs
31;; 21.1.14 and Emacs 20.7 as these are the latest versions of these
32;; Emacsen as of this writing (11-Apr-2001). I have no intent to test
33;; it with earlier Emacsen, but I will accept patches if they are
34;; small and reasonable. Please use the SourceForge Python project to
35;; submit bugs or patches:
36;;
37;; http://sourceforge.net/projects/python
Barry Warsaw37231521997-12-11 17:23:13 +000038
39;; FOR MORE INFORMATION:
40
Barry Warsaw4f577d22001-04-11 20:23:17 +000041;; There is some information on python-mode.el at
42
Barry Warsawf7039e21998-08-20 22:10:46 +000043;; http://www.python.org/emacs/python-mode/
44;;
Barry Warsaw4f577d22001-04-11 20:23:17 +000045;; but this link is fairly out of date, due to the current difficulty
46;; in updating that site. It does contain links to other packages that
47;; you might find useful, such as pdb interfaces, OO-Browser links,
48;; etc. Eventually, we'll be able to update it much more easily.
Barry Warsaw37231521997-12-11 17:23:13 +000049
50;; BUG REPORTING:
Barry Warsaw7ae77681994-12-12 20:38:05 +000051
Barry Warsaw4f577d22001-04-11 20:23:17 +000052;; As mentioned above, please use the SourceForge Python project for
53;; submitting bug reports or patches. The old recommendation, to use
54;; C-c C-b will still work, but those reports have a higher chance of
55;; getting buried in my mailbox. Please include a complete, but
Barry Warsawaffc0ca1997-11-03 16:59:38 +000056;; concise code sample and a recipe for reproducing the bug. Send
57;; suggestions and other comments to python-mode@python.org.
58
59;; When in a Python mode buffer, do a C-h m for more help. It's
Barry Warsaw37231521997-12-11 17:23:13 +000060;; doubtful that a texinfo manual would be very useful, but if you
61;; want to contribute one, I'll certainly accept it!
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000062
Barry Warsaw7b0f5681995-03-08 21:33:04 +000063;;; Code:
64
Barry Warsaw6dfbe5d1998-08-20 21:51:27 +000065(require 'comint)
Barry Warsawc72c11c1997-08-09 06:42:08 +000066(require 'custom)
Barry Warsaw4f577d22001-04-11 20:23:17 +000067(require 'cl)
Barry Warsawc72c11c1997-08-09 06:42:08 +000068
Barry Warsaw7b0f5681995-03-08 21:33:04 +000069
70;; user definable variables
71;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +000072
Barry Warsawc72c11c1997-08-09 06:42:08 +000073(defgroup python nil
74 "Support for the Python programming language, <http://www.python.org/>"
Barry Warsaw5ed843f1999-07-28 22:06:06 +000075 :group 'languages
76 :prefix "py-")
Barry Warsaw7ae77681994-12-12 20:38:05 +000077
Barry Warsawc72c11c1997-08-09 06:42:08 +000078(defcustom py-python-command "python"
79 "*Shell command used to start Python interpreter."
80 :type 'string
81 :group 'python)
82
Barry Warsawa2398801998-04-09 23:28:20 +000083(defcustom py-jpython-command "jpython"
84 "*Shell command used to start the JPython interpreter."
85 :type 'string
Barry Warsaw5ed843f1999-07-28 22:06:06 +000086 :group 'python
87 :tag "JPython Command")
Barry Warsawa2398801998-04-09 23:28:20 +000088
Barry Warsawaa384fd1999-02-16 23:36:16 +000089(defcustom py-default-interpreter 'cpython
90 "*Which Python interpreter is used by default.
91The value for this variable can be either `cpython' or `jpython'.
92
93When the value is `cpython', the variables `py-python-command' and
94`py-python-command-args' are consulted to determine the interpreter
95and arguments to use.
96
97When the value is `jpython', the variables `py-jpython-command' and
98`py-jpython-command-args' are consulted to determine the interpreter
99and arguments to use.
100
Barry Warsaw11f21561999-07-28 21:59:43 +0000101Note that this variable is consulted only the first time that a Python
102mode buffer is visited during an Emacs session. After that, use
Barry Warsawaa384fd1999-02-16 23:36:16 +0000103\\[py-toggle-shells] to change the interpreter shell."
104 :type '(choice (const :tag "Python (a.k.a. CPython)" cpython)
105 (const :tag "JPython" jpython))
106 :group 'python)
107
Barry Warsaw8f972b71998-02-05 20:45:49 +0000108(defcustom py-python-command-args '("-i")
109 "*List of string arguments to be used when starting a Python shell."
110 :type '(repeat string)
111 :group 'python)
112
Barry Warsawa2398801998-04-09 23:28:20 +0000113(defcustom py-jpython-command-args '("-i")
114 "*List of string arguments to be used when starting a JPython shell."
115 :type '(repeat string)
Barry Warsaw5ed843f1999-07-28 22:06:06 +0000116 :group 'python
117 :tag "JPython Command Args")
Barry Warsawa2398801998-04-09 23:28:20 +0000118
Barry Warsawc72c11c1997-08-09 06:42:08 +0000119(defcustom py-indent-offset 4
Barry Warsaw41a05c71998-08-10 16:33:12 +0000120 "*Amount of offset per level of indentation.
121`\\[py-guess-indent-offset]' can usually guess a good value when
122you're editing someone else's Python code."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000123 :type 'integer
124 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000125
Barry Warsawfd4c9e82001-06-18 23:40:35 +0000126(defcustom py-continuation-offset 4
Barry Warsaw40fb4522001-07-06 20:07:13 +0000127 "*Additional amount of offset to give for some continuation lines.
Barry Warsawfd4c9e82001-06-18 23:40:35 +0000128Continuation lines are those that immediately follow a backslash
Barry Warsaw40fb4522001-07-06 20:07:13 +0000129terminated line. Only those continuation lines for a block opening
130statement are given this extra offset."
Barry Warsawfd4c9e82001-06-18 23:40:35 +0000131 :type 'integer
132 :group 'python)
133
Barry Warsaw742a5111998-03-13 17:29:15 +0000134(defcustom py-smart-indentation t
135 "*Should `python-mode' try to automagically set some indentation variables?
136When this variable is non-nil, two things happen when a buffer is set
137to `python-mode':
138
Barry Warsawc0d2d511999-06-03 22:18:59 +0000139 1. `py-indent-offset' is guessed from existing code in the buffer.
Barry Warsaw639eea61998-03-16 18:12:13 +0000140 Only guessed values between 2 and 8 are considered. If a valid
Barry Warsaw742a5111998-03-13 17:29:15 +0000141 guess can't be made (perhaps because you are visiting a new
Barry Warsaw639eea61998-03-16 18:12:13 +0000142 file), then the value in `py-indent-offset' is used.
Barry Warsaw742a5111998-03-13 17:29:15 +0000143
Barry Warsaw639eea61998-03-16 18:12:13 +0000144 2. `indent-tabs-mode' is turned off if `py-indent-offset' does not
145 equal `tab-width' (`indent-tabs-mode' is never turned on by
146 Python mode). This means that for newly written code, tabs are
147 only inserted in indentation if one tab is one indentation
148 level, otherwise only spaces are used.
Barry Warsaw742a5111998-03-13 17:29:15 +0000149
Barry Warsaw8046bef1998-03-13 20:04:52 +0000150Note that both these settings occur *after* `python-mode-hook' is run,
151so if you want to defeat the automagic configuration, you must also
152set `py-smart-indentation' to nil in your `python-mode-hook'."
Barry Warsaw742a5111998-03-13 17:29:15 +0000153 :type 'boolean
154 :group 'python)
155
Barry Warsawc72c11c1997-08-09 06:42:08 +0000156(defcustom py-align-multiline-strings-p t
157 "*Flag describing how multi-line triple quoted strings are aligned.
Barry Warsaw095e9c61995-09-19 20:01:42 +0000158When this flag is non-nil, continuation lines are lined up under the
159preceding line's indentation. When this flag is nil, continuation
Barry Warsawc72c11c1997-08-09 06:42:08 +0000160lines are aligned to column zero."
161 :type '(choice (const :tag "Align under preceding line" t)
162 (const :tag "Align to column zero" nil))
163 :group 'python)
Barry Warsaw095e9c61995-09-19 20:01:42 +0000164
Barry Warsaw218eb751998-09-22 19:51:47 +0000165(defcustom py-block-comment-prefix "##"
Barry Warsaw867a32a1996-03-07 18:30:26 +0000166 "*String used by \\[comment-region] to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000167This should follow the convention for non-indenting comment lines so
168that the indentation commands won't get confused (i.e., the string
169should be of the form `#x...' where `x' is not a blank or a tab, and
Barry Warsaw218eb751998-09-22 19:51:47 +0000170`...' is arbitrary). However, this string should not end in whitespace."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000171 :type 'string
172 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000173
Barry Warsawc72c11c1997-08-09 06:42:08 +0000174(defcustom py-honor-comment-indentation t
Barry Warsaw6d627751996-03-06 18:41:38 +0000175 "*Controls how comment lines influence subsequent indentation.
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000176
Barry Warsaw6d627751996-03-06 18:41:38 +0000177When nil, all comment lines are skipped for indentation purposes, and
Barry Warsawc72c11c1997-08-09 06:42:08 +0000178if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond).
Barry Warsaw6d627751996-03-06 18:41:38 +0000179
180When t, lines that begin with a single `#' are a hint to subsequent
181line indentation. If the previous line is such a comment line (as
Barry Warsaw145ab1c1998-05-19 14:49:49 +0000182opposed to one that starts with `py-block-comment-prefix'), then its
Barry Warsaw6d627751996-03-06 18:41:38 +0000183indentation is used as a hint for this line's indentation. Lines that
184begin with `py-block-comment-prefix' are ignored for indentation
185purposes.
186
Barry Warsawd36cfe42002-03-15 16:46:46 +0000187When not nil or t, comment lines that begin with a single `#' are used
188as indentation hints, unless the comment character is in column zero."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000189 :type '(choice
190 (const :tag "Skip all comment lines (fast)" nil)
191 (const :tag "Single # `sets' indentation for next line" t)
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000192 (const :tag "Single # `sets' indentation except at column zero"
193 other)
Barry Warsawc72c11c1997-08-09 06:42:08 +0000194 )
195 :group 'python)
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000196
Barry Warsaw516b6201997-08-09 06:43:20 +0000197(defcustom py-temp-directory
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000198 (let ((ok '(lambda (x)
199 (and x
200 (setq x (expand-file-name x)) ; always true
201 (file-directory-p x)
202 (file-writable-p x)
203 x))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000204 (or (funcall ok (getenv "TMPDIR"))
205 (funcall ok "/usr/tmp")
206 (funcall ok "/tmp")
Barry Warsawd1648372002-03-18 18:53:56 +0000207 (funcall ok "/var/tmp")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000208 (funcall ok ".")
209 (error
Barry Warsaw6c6db0a1998-02-25 16:33:56 +0000210 "Couldn't find a usable temp directory -- set `py-temp-directory'")))
Barry Warsawd1648372002-03-18 18:53:56 +0000211 "*Directory used for temporary files created by a *Python* process.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000212By default, the first directory from this list that exists and that you
Barry Warsawd1648372002-03-18 18:53:56 +0000213can write into: the value (if any) of the environment variable TMPDIR,
214/usr/tmp, /tmp, /var/tmp, or the current directory."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000215 :type 'string
216 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000217
Barry Warsaw516b6201997-08-09 06:43:20 +0000218(defcustom py-beep-if-tab-change t
Barry Warsaw41a05c71998-08-10 16:33:12 +0000219 "*Ring the bell if `tab-width' is changed.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000220If a comment of the form
221
222 \t# vi:set tabsize=<number>:
223
224is found before the first code line when the file is entered, and the
225current value of (the general Emacs variable) `tab-width' does not
226equal <number>, `tab-width' is set to <number>, a message saying so is
227displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
Barry Warsawc72c11c1997-08-09 06:42:08 +0000228the Emacs bell is also rung as a warning."
229 :type 'boolean
230 :group 'python)
231
Barry Warsaw9981d221997-12-03 05:25:48 +0000232(defcustom py-jump-on-exception t
233 "*Jump to innermost exception frame in *Python Output* buffer.
Barry Warsaw12c92941998-08-10 21:44:37 +0000234When this variable is non-nil and an exception occurs when running
Barry Warsaw9981d221997-12-03 05:25:48 +0000235Python code synchronously in a subprocess, jump immediately to the
Barry Warsaw12c92941998-08-10 21:44:37 +0000236source code of the innermost traceback frame."
Barry Warsaw3bfed5b1998-05-19 16:25:04 +0000237 :type 'boolean
238 :group 'python)
239
240(defcustom py-ask-about-save t
241 "If not nil, ask about which buffers to save before executing some code.
242Otherwise, all modified buffers are saved without asking."
243 :type 'boolean
244 :group 'python)
Barry Warsaw9981d221997-12-03 05:25:48 +0000245
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000246(defcustom py-backspace-function 'backward-delete-char-untabify
247 "*Function called by `py-electric-backspace' when deleting backwards."
248 :type 'function
249 :group 'python)
250
251(defcustom py-delete-function 'delete-char
252 "*Function called by `py-electric-delete' when deleting forwards."
253 :type 'function
254 :group 'python)
255
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000256(defcustom py-imenu-show-method-args-p nil
257 "*Controls echoing of arguments of functions & methods in the Imenu buffer.
258When non-nil, arguments are printed."
259 :type 'boolean
260 :group 'python)
261(make-variable-buffer-local 'py-indent-offset)
262
Barry Warsaw4f577d22001-04-11 20:23:17 +0000263(defcustom py-pdbtrack-do-tracking-p t
264 "*Controls whether the pdbtrack feature is enabled or not.
265When non-nil, pdbtrack is enabled in all comint-based buffers,
266e.g. shell buffers and the *Python* buffer. When using pdb to debug a
267Python program, pdbtrack notices the pdb prompt and displays the
268source file and line that the program is stopped at, much the same way
269as gud-mode does for debugging C programs with gdb."
270 :type 'boolean
271 :group 'python)
272(make-variable-buffer-local 'py-pdbtrack-do-tracking-p)
273
274(defcustom py-pdbtrack-minor-mode-string " PDB"
275 "*String to use in the minor mode list when pdbtrack is enabled."
276 :type 'string
277 :group 'python)
278
Barry Warsawc0ecb531998-01-20 21:43:34 +0000279;; Not customizable
280(defvar py-master-file nil
281 "If non-nil, execute the named file instead of the buffer's file.
Barry Warsaw50b3eb61998-02-25 15:57:47 +0000282The intent is to allow you to set this variable in the file's local
Barry Warsawc0ecb531998-01-20 21:43:34 +0000283variable section, e.g.:
284
285 # Local Variables:
286 # py-master-file: \"master.py\"
287 # End:
288
Barry Warsaw41a05c71998-08-10 16:33:12 +0000289so that typing \\[py-execute-buffer] in that buffer executes the named
Barry Warsaw1bbc0311998-10-27 21:54:56 +0000290master file instead of the buffer's file. If the file name has a
291relative path, the value of variable `default-directory' for the
292buffer is prepended to come up with a file name.")
Barry Warsawc0ecb531998-01-20 21:43:34 +0000293(make-variable-buffer-local 'py-master-file)
294
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000295
Barry Warsawc72c11c1997-08-09 06:42:08 +0000296
297;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
298;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
299
Barry Warsaw5e21cb01997-11-05 18:41:11 +0000300(defconst py-emacs-features
301 (let (features)
Barry Warsaw5e21cb01997-11-05 18:41:11 +0000302 features)
Barry Warsawc12c62e1997-09-04 04:18:07 +0000303 "A list of features extant in the Emacs you are using.
Barry Warsaw6ae21ad1997-11-06 14:36:49 +0000304There are many flavors of Emacs out there, with different levels of
305support for features needed by `python-mode'.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000306
Barry Warsawfb07f401997-02-24 03:37:22 +0000307(defvar python-font-lock-keywords
Barry Warsawb8f11661997-11-06 14:35:15 +0000308 (let ((kw1 (mapconcat 'identity
309 '("and" "assert" "break" "class"
310 "continue" "def" "del" "elif"
311 "else" "except" "exec" "for"
312 "from" "global" "if" "import"
313 "in" "is" "lambda" "not"
314 "or" "pass" "print" "raise"
Barry Warsawe275c422001-06-19 18:24:42 +0000315 "return" "while" "yield"
Barry Warsawb8f11661997-11-06 14:35:15 +0000316 )
317 "\\|"))
318 (kw2 (mapconcat 'identity
319 '("else:" "except:" "finally:" "try:")
320 "\\|"))
321 )
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000322 (list
Barry Warsawef3c8911997-11-05 18:55:50 +0000323 ;; keywords
Barry Warsawb8f11661997-11-06 14:35:15 +0000324 (cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
325 ;; block introducing keywords with immediately following colons.
326 ;; Yes "except" is in both lists.
327 (cons (concat "\\b\\(" kw2 "\\)[ \n\t(]") 1)
Barry Warsawe0c182f2000-12-27 17:41:47 +0000328 ;; `as' but only in "import foo as bar"
329 '("[ \t]*\\(\\bfrom\\b.*\\)?\\bimport\\b.*\\b\\(as\\)\\b" . 2)
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000330 ;; classes
331 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
332 1 font-lock-type-face)
333 ;; functions
334 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
335 1 font-lock-function-name-face)
336 ))
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000337 "Additional expressions to highlight in Python mode.")
Barry Warsawfb07f401997-02-24 03:37:22 +0000338(put 'python-mode 'font-lock-defaults '(python-font-lock-keywords))
339
Barry Warsawe467bfb1997-11-26 05:40:58 +0000340;; have to bind py-file-queue before installing the kill-emacs-hook
Barry Warsaw7ae77681994-12-12 20:38:05 +0000341(defvar py-file-queue nil
342 "Queue of Python temp files awaiting execution.
343Currently-active file is at the head of the list.")
344
Barry Warsaw4f577d22001-04-11 20:23:17 +0000345(defvar py-pdbtrack-is-tracking-p nil)
346
347
Barry Warsawc72c11c1997-08-09 06:42:08 +0000348
349;; Constants
350
Barry Warsawc72c11c1997-08-09 06:42:08 +0000351(defconst py-stringlit-re
352 (concat
Barry Warsaw751f4931998-05-19 16:06:21 +0000353 ;; These fail if backslash-quote ends the string (not worth
354 ;; fixing?). They precede the short versions so that the first two
355 ;; quotes don't look like an empty short string.
356 ;;
357 ;; (maybe raw), long single quoted triple quoted strings (SQTQ),
358 ;; with potential embedded single quotes
359 "[rR]?'''[^']*\\(\\('[^']\\|''[^']\\)[^']*\\)*'''"
360 "\\|"
361 ;; (maybe raw), long double quoted triple quoted strings (DQTQ),
362 ;; with potential embedded double quotes
363 "[rR]?\"\"\"[^\"]*\\(\\(\"[^\"]\\|\"\"[^\"]\\)[^\"]*\\)*\"\"\""
364 "\\|"
365 "[rR]?'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
Barry Warsawc72c11c1997-08-09 06:42:08 +0000366 "\\|" ; or
Barry Warsaw751f4931998-05-19 16:06:21 +0000367 "[rR]?\"\\([^\"\n\\]\\|\\\\.\\)*\"" ; double-quoted
Barry Warsaw41a05c71998-08-10 16:33:12 +0000368 )
369 "Regular expression matching a Python string literal.")
Barry Warsaw751f4931998-05-19 16:06:21 +0000370
Barry Warsawc72c11c1997-08-09 06:42:08 +0000371(defconst py-continued-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000372 ;; This is tricky because a trailing backslash does not mean
373 ;; continuation if it's in a comment
Barry Warsawc72c11c1997-08-09 06:42:08 +0000374 (concat
375 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
Barry Warsaw41a05c71998-08-10 16:33:12 +0000376 "\\\\$")
377 "Regular expression matching Python backslash continuation lines.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000378
Barry Warsaw41a05c71998-08-10 16:33:12 +0000379(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Barry Warsaw71c3adb1998-08-10 16:34:33 +0000380 "Regular expression matching a blank or comment line.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000381
Barry Warsawc72c11c1997-08-09 06:42:08 +0000382(defconst py-outdent-re
383 (concat "\\(" (mapconcat 'identity
384 '("else:"
385 "except\\(\\s +.*\\)?:"
386 "finally:"
387 "elif\\s +.*:")
388 "\\|")
Barry Warsaw41a05c71998-08-10 16:33:12 +0000389 "\\)")
390 "Regular expression matching statements to be dedented one level.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000391
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000392(defconst py-block-closing-keywords-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000393 "\\(return\\|raise\\|break\\|continue\\|pass\\)"
394 "Regular expression matching keywords which typically close a block.")
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000395
Barry Warsawc72c11c1997-08-09 06:42:08 +0000396(defconst py-no-outdent-re
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000397 (concat
398 "\\("
399 (mapconcat 'identity
400 (list "try:"
401 "except\\(\\s +.*\\)?:"
402 "while\\s +.*:"
403 "for\\s +.*:"
404 "if\\s +.*:"
405 "elif\\s +.*:"
406 (concat py-block-closing-keywords-re "[ \t\n]")
407 )
408 "\\|")
Barry Warsaw41a05c71998-08-10 16:33:12 +0000409 "\\)")
410 "Regular expression matching lines not to dedent after.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000411
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000412(defconst py-defun-start-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000413 "^\\([ \t]*\\)def[ \t]+\\([a-zA-Z_0-9]+\\)\\|\\(^[a-zA-Z_0-9]+\\)[ \t]*="
414 ;; If you change this, you probably have to change py-current-defun
415 ;; as well. This is only used by py-current-defun to find the name
416 ;; for add-log.el.
Barry Warsaw71c3adb1998-08-10 16:34:33 +0000417 "Regular expression matching a function, method, or variable assignment.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000418
Barry Warsaw41a05c71998-08-10 16:33:12 +0000419(defconst py-class-start-re "^class[ \t]*\\([a-zA-Z_0-9]+\\)"
420 ;; If you change this, you probably have to change py-current-defun
421 ;; as well. This is only used by py-current-defun to find the name
422 ;; for add-log.el.
423 "Regular expression for finding a class name.")
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000424
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000425(defconst py-traceback-line-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000426 "[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)"
427 "Regular expression that describes tracebacks.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000428
Barry Warsaw4f577d22001-04-11 20:23:17 +0000429;; pdbtrack contants
430(defconst py-pdbtrack-stack-entry-regexp
431 "> \\([^(]+\\)(\\([0-9]+\\))[?a-zA-Z0-9_]+()"
432 "Regular expression pdbtrack uses to find a stack trace entry.")
433
434(defconst py-pdbtrack-input-prompt "\n[(<]?pdb[>)]? "
435 "Regular expression pdbtrack uses to recognize a pdb prompt.")
436
437(defconst py-pdbtrack-track-range 10000
438 "Max number of characters from end of buffer to search for stack entry.")
439
Barry Warsawc72c11c1997-08-09 06:42:08 +0000440
441
Barry Warsawc72c11c1997-08-09 06:42:08 +0000442;; Major mode boilerplate
443
Barry Warsaw7ae77681994-12-12 20:38:05 +0000444;; define a mode-specific abbrev table for those who use such things
445(defvar python-mode-abbrev-table nil
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000446 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000447(define-abbrev-table 'python-mode-abbrev-table nil)
448
Barry Warsaw7ae77681994-12-12 20:38:05 +0000449(defvar python-mode-hook nil
450 "*Hook called by `python-mode'.")
451
Barry Warsaw145ab1c1998-05-19 14:49:49 +0000452;; In previous version of python-mode.el, the hook was incorrectly
453;; called py-mode-hook, and was not defvar'd. Deprecate its use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000454(and (fboundp 'make-obsolete-variable)
455 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
456
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000457(defvar py-mode-map ()
458 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000459(if py-mode-map
Barry Warsawc72c11c1997-08-09 06:42:08 +0000460 nil
Barry Warsaw7ae77681994-12-12 20:38:05 +0000461 (setq py-mode-map (make-sparse-keymap))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000462 ;; electric keys
463 (define-key py-mode-map ":" 'py-electric-colon)
464 ;; indentation level modifiers
465 (define-key py-mode-map "\C-c\C-l" 'py-shift-region-left)
466 (define-key py-mode-map "\C-c\C-r" 'py-shift-region-right)
467 (define-key py-mode-map "\C-c<" 'py-shift-region-left)
468 (define-key py-mode-map "\C-c>" 'py-shift-region-right)
469 ;; subprocess commands
470 (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
Barry Warsaw820273d1998-05-19 15:54:45 +0000471 (define-key py-mode-map "\C-c\C-m" 'py-execute-import-or-reload)
Barry Warsaw1d0364b1998-05-19 16:15:26 +0000472 (define-key py-mode-map "\C-c\C-s" 'py-execute-string)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000473 (define-key py-mode-map "\C-c|" 'py-execute-region)
Barry Warsaw820273d1998-05-19 15:54:45 +0000474 (define-key py-mode-map "\e\C-x" 'py-execute-def-or-class)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000475 (define-key py-mode-map "\C-c!" 'py-shell)
Barry Warsawa2398801998-04-09 23:28:20 +0000476 (define-key py-mode-map "\C-c\C-t" 'py-toggle-shells)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000477 ;; Caution! Enter here at your own risk. We are trying to support
478 ;; several behaviors and it gets disgusting. :-( This logic ripped
479 ;; largely from CC Mode.
480 ;;
481 ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
482 ;; backwards deletion behavior to DEL, which both Delete and
483 ;; Backspace get translated to. There's no way to separate this
484 ;; behavior in a clean way, so deal with it! Besides, it's been
485 ;; this way since the dawn of time.
486 (if (not (boundp 'delete-key-deletes-forward))
487 (define-key py-mode-map "\177" 'py-electric-backspace)
488 ;; However, XEmacs 20 actually achieved enlightenment. It is
489 ;; possible to sanely define both backward and forward deletion
490 ;; behavior under X separately (TTYs are forever beyond hope, but
491 ;; who cares? XEmacs 20 does the right thing with these too).
492 (define-key py-mode-map [delete] 'py-electric-delete)
493 (define-key py-mode-map [backspace] 'py-electric-backspace))
Barry Warsaw8c4a8de1997-11-26 20:30:33 +0000494 ;; Separate M-BS from C-M-h. The former should remain
495 ;; backward-kill-word.
496 (define-key py-mode-map [(control meta h)] 'py-mark-def-or-class)
Barry Warsaw2518c671997-11-05 00:51:08 +0000497 (define-key py-mode-map "\C-c\C-k" 'py-mark-block)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000498 ;; Miscellaneous
499 (define-key py-mode-map "\C-c:" 'py-guess-indent-offset)
500 (define-key py-mode-map "\C-c\t" 'py-indent-region)
Barry Warsaw4f577d22001-04-11 20:23:17 +0000501 (define-key py-mode-map "\C-c\C-d" 'py-pdbtrack-toggle-stack-tracking)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000502 (define-key py-mode-map "\C-c\C-n" 'py-next-statement)
503 (define-key py-mode-map "\C-c\C-p" 'py-previous-statement)
504 (define-key py-mode-map "\C-c\C-u" 'py-goto-block-up)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000505 (define-key py-mode-map "\C-c#" 'py-comment-region)
506 (define-key py-mode-map "\C-c?" 'py-describe-mode)
507 (define-key py-mode-map "\C-c\C-hm" 'py-describe-mode)
Barry Warsawab0e86c1998-05-19 15:31:46 +0000508 (define-key py-mode-map "\e\C-a" 'py-beginning-of-def-or-class)
509 (define-key py-mode-map "\e\C-e" 'py-end-of-def-or-class)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000510 (define-key py-mode-map "\C-c-" 'py-up-exception)
511 (define-key py-mode-map "\C-c=" 'py-down-exception)
Barry Warsawf8ddb6a1999-01-18 21:49:39 +0000512 ;; stuff that is `standard' but doesn't interface well with
513 ;; python-mode, which forces us to rebind to special commands
514 (define-key py-mode-map "\C-xnd" 'py-narrow-to-defun)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000515 ;; information
516 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
517 (define-key py-mode-map "\C-c\C-v" 'py-version)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000518 ;; shadow global bindings for newline-and-indent w/ the py- version.
519 ;; BAW - this is extremely bad form, but I'm not going to change it
520 ;; for now.
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000521 (mapcar #'(lambda (key)
522 (define-key py-mode-map key 'py-newline-and-indent))
523 (where-is-internal 'newline-and-indent))
Barry Warsawf19feb81999-01-21 17:06:11 +0000524 ;; Force RET to be py-newline-and-indent even if it didn't get
525 ;; mapped by the above code. motivation: Emacs' default binding for
526 ;; RET is `newline' and C-j is `newline-and-indent'. Most Pythoneers
527 ;; expect RET to do a `py-newline-and-indent' and any Emacsers who
528 ;; dislike this are probably knowledgeable enough to do a rebind.
529 ;; However, we do *not* change C-j since many Emacsers have already
530 ;; swapped RET and C-j and they don't want C-j bound to `newline' to
531 ;; change.
532 (define-key py-mode-map "\C-m" 'py-newline-and-indent)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000533 )
534
535(defvar py-mode-output-map nil
Barry Warsaw41a05c71998-08-10 16:33:12 +0000536 "Keymap used in *Python Output* buffers.")
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000537(if py-mode-output-map
538 nil
539 (setq py-mode-output-map (make-sparse-keymap))
540 (define-key py-mode-output-map [button2] 'py-mouseto-exception)
541 (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
542 ;; TBD: Disable all self-inserting keys. This is bogus, we should
543 ;; really implement this as *Python Output* buffer being read-only
544 (mapcar #' (lambda (key)
545 (define-key py-mode-output-map key
546 #'(lambda () (interactive) (beep))))
547 (where-is-internal 'self-insert-command))
Barry Warsaw850437a1995-03-08 21:50:28 +0000548 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000549
Barry Warsaw6dfbe5d1998-08-20 21:51:27 +0000550(defvar py-shell-map nil
551 "Keymap used in *Python* shell buffers.")
552(if py-shell-map
553 nil
554 (setq py-shell-map (copy-keymap comint-mode-map))
555 (define-key py-shell-map [tab] 'tab-to-tab-stop)
556 (define-key py-shell-map "\C-c-" 'py-up-exception)
557 (define-key py-shell-map "\C-c=" 'py-down-exception)
558 )
559
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000560(defvar py-mode-syntax-table nil
561 "Syntax table used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000562(if py-mode-syntax-table
Barry Warsawc72c11c1997-08-09 06:42:08 +0000563 nil
Barry Warsaw7ae77681994-12-12 20:38:05 +0000564 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsawc72c11c1997-08-09 06:42:08 +0000565 (modify-syntax-entry ?\( "()" py-mode-syntax-table)
566 (modify-syntax-entry ?\) ")(" py-mode-syntax-table)
567 (modify-syntax-entry ?\[ "(]" py-mode-syntax-table)
568 (modify-syntax-entry ?\] ")[" py-mode-syntax-table)
569 (modify-syntax-entry ?\{ "(}" py-mode-syntax-table)
570 (modify-syntax-entry ?\} "){" py-mode-syntax-table)
571 ;; Add operator symbols misassigned in the std table
572 (modify-syntax-entry ?\$ "." py-mode-syntax-table)
573 (modify-syntax-entry ?\% "." py-mode-syntax-table)
574 (modify-syntax-entry ?\& "." py-mode-syntax-table)
575 (modify-syntax-entry ?\* "." py-mode-syntax-table)
576 (modify-syntax-entry ?\+ "." py-mode-syntax-table)
577 (modify-syntax-entry ?\- "." py-mode-syntax-table)
578 (modify-syntax-entry ?\/ "." py-mode-syntax-table)
579 (modify-syntax-entry ?\< "." py-mode-syntax-table)
580 (modify-syntax-entry ?\= "." py-mode-syntax-table)
581 (modify-syntax-entry ?\> "." py-mode-syntax-table)
582 (modify-syntax-entry ?\| "." py-mode-syntax-table)
583 ;; For historical reasons, underscore is word class instead of
584 ;; symbol class. GNU conventions say it should be symbol class, but
585 ;; there's a natural conflict between what major mode authors want
586 ;; and what users expect from `forward-word' and `backward-word'.
587 ;; Guido and I have hashed this out and have decided to keep
588 ;; underscore in word class. If you're tempted to change it, try
589 ;; binding M-f and M-b to py-forward-into-nomenclature and
Barry Warsawaa384fd1999-02-16 23:36:16 +0000590 ;; py-backward-into-nomenclature instead. This doesn't help in all
591 ;; situations where you'd want the different behavior
592 ;; (e.g. backward-kill-word).
Barry Warsawc72c11c1997-08-09 06:42:08 +0000593 (modify-syntax-entry ?\_ "w" py-mode-syntax-table)
594 ;; Both single quote and double quote are string delimiters
595 (modify-syntax-entry ?\' "\"" py-mode-syntax-table)
596 (modify-syntax-entry ?\" "\"" py-mode-syntax-table)
597 ;; backquote is open and close paren
598 (modify-syntax-entry ?\` "$" py-mode-syntax-table)
599 ;; comment delimiters
600 (modify-syntax-entry ?\# "<" py-mode-syntax-table)
601 (modify-syntax-entry ?\n ">" py-mode-syntax-table)
602 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000603
Barry Warsawb3e81d51996-09-04 15:12:42 +0000604
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000605
Barry Warsawbc3760b1998-09-14 16:16:18 +0000606;; Utilities
607
608(defmacro py-safe (&rest body)
609 "Safely execute BODY, return nil if an error occurred."
610 (` (condition-case nil
611 (progn (,@ body))
612 (error nil))))
613
614(defsubst py-keep-region-active ()
615 "Keep the region active in XEmacs."
616 ;; Ignore byte-compiler warnings you might see. Also note that
617 ;; FSF's Emacs 19 does it differently; its policy doesn't require us
618 ;; to take explicit action.
619 (and (boundp 'zmacs-region-stays)
620 (setq zmacs-region-stays t)))
621
622(defsubst py-point (position)
623 "Returns the value of point at certain commonly referenced POSITIONs.
624POSITION can be one of the following symbols:
625
626 bol -- beginning of line
627 eol -- end of line
628 bod -- beginning of def or class
629 eod -- end of def or class
630 bob -- beginning of buffer
631 eob -- end of buffer
632 boi -- back to indentation
633 bos -- beginning of statement
634
635This function does not modify point or mark."
636 (let ((here (point)))
637 (cond
638 ((eq position 'bol) (beginning-of-line))
639 ((eq position 'eol) (end-of-line))
640 ((eq position 'bod) (py-beginning-of-def-or-class))
641 ((eq position 'eod) (py-end-of-def-or-class))
642 ;; Kind of funny, I know, but useful for py-up-exception.
643 ((eq position 'bob) (beginning-of-buffer))
644 ((eq position 'eob) (end-of-buffer))
645 ((eq position 'boi) (back-to-indentation))
646 ((eq position 'bos) (py-goto-initial-line))
647 (t (error "Unknown buffer position requested: %s" position))
648 )
649 (prog1
650 (point)
651 (goto-char here))))
652
653(defsubst py-highlight-line (from to file line)
654 (cond
655 ((fboundp 'make-extent)
656 ;; XEmacs
657 (let ((e (make-extent from to)))
658 (set-extent-property e 'mouse-face 'highlight)
659 (set-extent-property e 'py-exc-info (cons file line))
660 (set-extent-property e 'keymap py-mode-output-map)))
661 (t
662 ;; Emacs -- Please port this!
663 )
664 ))
665
666(defun py-in-literal (&optional lim)
667 "Return non-nil if point is in a Python literal (a comment or string).
668Optional argument LIM indicates the beginning of the containing form,
669i.e. the limit on how far back to scan."
670 ;; This is the version used for non-XEmacs, which has a nicer
671 ;; interface.
672 ;;
673 ;; WARNING: Watch out for infinite recursion.
674 (let* ((lim (or lim (py-point 'bod)))
675 (state (parse-partial-sexp lim (point))))
676 (cond
677 ((nth 3 state) 'string)
678 ((nth 4 state) 'comment)
679 (t nil))))
680
681;; XEmacs has a built-in function that should make this much quicker.
682;; In this case, lim is ignored
683(defun py-fast-in-literal (&optional lim)
684 "Fast version of `py-in-literal', used only by XEmacs.
685Optional LIM is ignored."
686 ;; don't have to worry about context == 'block-comment
687 (buffer-syntactic-context))
688
689(if (fboundp 'buffer-syntactic-context)
690 (defalias 'py-in-literal 'py-fast-in-literal))
691
692
693
Barry Warsaw42f707f1996-07-29 21:05:05 +0000694;; Menu definitions, only relevent if you have the easymenu.el package
695;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
Barry Warsaw5490a061996-08-06 15:43:33 +0000696(defvar py-menu nil
697 "Menu for Python Mode.
Barry Warsawc72c11c1997-08-09 06:42:08 +0000698This menu will get created automatically if you have the `easymenu'
699package. Note that the latest X/Emacs releases contain this package.")
Barry Warsaw5490a061996-08-06 15:43:33 +0000700
Barry Warsawc72c11c1997-08-09 06:42:08 +0000701(and (py-safe (require 'easymenu) t)
702 (easy-menu-define
703 py-menu py-mode-map "Python Mode menu"
704 '("Python"
705 ["Comment Out Region" py-comment-region (mark)]
706 ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)]
707 "-"
708 ["Mark current block" py-mark-block t]
Barry Warsaw2518c671997-11-05 00:51:08 +0000709 ["Mark current def" py-mark-def-or-class t]
710 ["Mark current class" (py-mark-def-or-class t) t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000711 "-"
712 ["Shift region left" py-shift-region-left (mark)]
713 ["Shift region right" py-shift-region-right (mark)]
714 "-"
Barry Warsaw820273d1998-05-19 15:54:45 +0000715 ["Import/reload file" py-execute-import-or-reload t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000716 ["Execute buffer" py-execute-buffer t]
717 ["Execute region" py-execute-region (mark)]
Barry Warsaw820273d1998-05-19 15:54:45 +0000718 ["Execute def or class" py-execute-def-or-class (mark)]
Barry Warsaw1d0364b1998-05-19 16:15:26 +0000719 ["Execute string" py-execute-string t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000720 ["Start interpreter..." py-shell t]
721 "-"
722 ["Go to start of block" py-goto-block-up t]
Barry Warsawab0e86c1998-05-19 15:31:46 +0000723 ["Go to start of class" (py-beginning-of-def-or-class t) t]
724 ["Move to end of class" (py-end-of-def-or-class t) t]
725 ["Move to start of def" py-beginning-of-def-or-class t]
726 ["Move to end of def" py-end-of-def-or-class t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000727 "-"
728 ["Describe mode" py-describe-mode t]
729 )))
Barry Warsaw42f707f1996-07-29 21:05:05 +0000730
Barry Warsaw81437461996-08-01 19:48:02 +0000731
732
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000733;; Imenu definitions
734(defvar py-imenu-class-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000735 (concat ; <<classes>>
736 "\\(" ;
737 "^[ \t]*" ; newline and maybe whitespace
738 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
739 ; possibly multiple superclasses
Barry Warsaw3179fe01998-04-04 21:36:53 +0000740 "\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)"
Barry Warsaw81437461996-08-01 19:48:02 +0000741 "[ \t]*:" ; and the final :
742 "\\)" ; >>classes<<
743 )
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000744 "Regexp for Python classes for use with the Imenu package."
Barry Warsaw81437461996-08-01 19:48:02 +0000745 )
746
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000747(defvar py-imenu-method-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000748 (concat ; <<methods and functions>>
749 "\\(" ;
750 "^[ \t]*" ; new line and maybe whitespace
751 "\\(def[ \t]+" ; function definitions start with def
752 "\\([a-zA-Z0-9_]+\\)" ; name is here
753 ; function arguments...
Barry Warsaw1d5f9881998-10-28 04:08:13 +0000754;; "[ \t]*(\\([-+/a-zA-Z0-9_=,\* \t\n.()\"'#]*\\))"
755 "[ \t]*(\\([^:#]*\\))"
Barry Warsaw81437461996-08-01 19:48:02 +0000756 "\\)" ; end of def
757 "[ \t]*:" ; and then the :
758 "\\)" ; >>methods and functions<<
759 )
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000760 "Regexp for Python methods/functions for use with the Imenu package."
Barry Warsaw81437461996-08-01 19:48:02 +0000761 )
762
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000763(defvar py-imenu-method-no-arg-parens '(2 8)
764 "Indices into groups of the Python regexp for use with Imenu.
Barry Warsaw81437461996-08-01 19:48:02 +0000765
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000766Using these values will result in smaller Imenu lists, as arguments to
Barry Warsaw81437461996-08-01 19:48:02 +0000767functions are not listed.
768
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000769See the variable `py-imenu-show-method-args-p' for more
Barry Warsaw81437461996-08-01 19:48:02 +0000770information.")
771
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000772(defvar py-imenu-method-arg-parens '(2 7)
Barry Warsaw1bbc0311998-10-27 21:54:56 +0000773 "Indices into groups of the Python regexp for use with imenu.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000774Using these values will result in large Imenu lists, as arguments to
Barry Warsaw81437461996-08-01 19:48:02 +0000775functions are listed.
776
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000777See the variable `py-imenu-show-method-args-p' for more
Barry Warsaw81437461996-08-01 19:48:02 +0000778information.")
779
780;; Note that in this format, this variable can still be used with the
781;; imenu--generic-function. Otherwise, there is no real reason to have
782;; it.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000783(defvar py-imenu-generic-expression
Barry Warsaw81437461996-08-01 19:48:02 +0000784 (cons
785 (concat
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000786 py-imenu-class-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000787 "\\|" ; or...
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000788 py-imenu-method-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000789 )
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000790 py-imenu-method-no-arg-parens)
791 "Generic Python expression which may be used directly with Imenu.
Barry Warsaw81437461996-08-01 19:48:02 +0000792Used by setting the variable `imenu-generic-expression' to this value.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000793Also, see the function \\[py-imenu-create-index] for a better
794alternative for finding the index.")
Barry Warsaw81437461996-08-01 19:48:02 +0000795
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000796;; These next two variables are used when searching for the Python
Barry Warsaw81437461996-08-01 19:48:02 +0000797;; class/definitions. Just saving some time in accessing the
798;; generic-python-expression, really.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000799(defvar py-imenu-generic-regexp nil)
800(defvar py-imenu-generic-parens nil)
Barry Warsaw81437461996-08-01 19:48:02 +0000801
802
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000803(defun py-imenu-create-index-function ()
804 "Python interface function for the Imenu package.
805Finds all Python classes and functions/methods. Calls function
806\\[py-imenu-create-index-engine]. See that function for the details
807of how this works."
808 (setq py-imenu-generic-regexp (car py-imenu-generic-expression)
809 py-imenu-generic-parens (if py-imenu-show-method-args-p
810 py-imenu-method-arg-parens
811 py-imenu-method-no-arg-parens))
Barry Warsaw81437461996-08-01 19:48:02 +0000812 (goto-char (point-min))
Barry Warsaw1d5f9881998-10-28 04:08:13 +0000813 ;; Warning: When the buffer has no classes or functions, this will
814 ;; return nil, which seems proper according to the Imenu API, but
815 ;; causes an error in the XEmacs port of Imenu. Sigh.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000816 (py-imenu-create-index-engine nil))
Barry Warsaw81437461996-08-01 19:48:02 +0000817
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000818(defun py-imenu-create-index-engine (&optional start-indent)
819 "Function for finding Imenu definitions in Python.
Barry Warsaw81437461996-08-01 19:48:02 +0000820
821Finds all definitions (classes, methods, or functions) in a Python
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000822file for the Imenu package.
Barry Warsaw81437461996-08-01 19:48:02 +0000823
824Returns a possibly nested alist of the form
825
826 (INDEX-NAME . INDEX-POSITION)
827
828The second element of the alist may be an alist, producing a nested
829list as in
830
831 (INDEX-NAME . INDEX-ALIST)
832
833This function should not be called directly, as it calls itself
834recursively and requires some setup. Rather this is the engine for
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000835the function \\[py-imenu-create-index-function].
Barry Warsaw81437461996-08-01 19:48:02 +0000836
837It works recursively by looking for all definitions at the current
838indention level. When it finds one, it adds it to the alist. If it
839finds a definition at a greater indentation level, it removes the
Barry Warsaw145ab1c1998-05-19 14:49:49 +0000840previous definition from the alist. In its place it adds all
Barry Warsaw81437461996-08-01 19:48:02 +0000841definitions found at the next indentation level. When it finds a
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000842definition that is less indented then the current level, it returns
843the alist it has created thus far.
Barry Warsaw81437461996-08-01 19:48:02 +0000844
845The optional argument START-INDENT indicates the starting indentation
846at which to continue looking for Python classes, methods, or
847functions. If this is not supplied, the function uses the indentation
848of the first definition found."
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000849 (let (index-alist
850 sub-method-alist
Barry Warsaw81437461996-08-01 19:48:02 +0000851 looking-p
852 def-name prev-name
853 cur-indent def-pos
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000854 (class-paren (first py-imenu-generic-parens))
855 (def-paren (second py-imenu-generic-parens)))
Barry Warsaw81437461996-08-01 19:48:02 +0000856 (setq looking-p
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000857 (re-search-forward py-imenu-generic-regexp (point-max) t))
Barry Warsaw81437461996-08-01 19:48:02 +0000858 (while looking-p
859 (save-excursion
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000860 ;; used to set def-name to this value but generic-extract-name
861 ;; is new to imenu-1.14. this way it still works with
862 ;; imenu-1.11
863 ;;(imenu--generic-extract-name py-imenu-generic-parens))
Barry Warsaw81437461996-08-01 19:48:02 +0000864 (let ((cur-paren (if (match-beginning class-paren)
865 class-paren def-paren)))
866 (setq def-name
Barry Warsawc8520351997-11-26 06:14:40 +0000867 (buffer-substring-no-properties (match-beginning cur-paren)
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000868 (match-end cur-paren))))
Barry Warsaw93c88cc1998-08-18 02:00:44 +0000869 (save-match-data
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000870 (py-beginning-of-def-or-class 'either))
Barry Warsaw81437461996-08-01 19:48:02 +0000871 (beginning-of-line)
872 (setq cur-indent (current-indentation)))
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000873 ;; HACK: want to go to the next correct definition location. We
874 ;; explicitly list them here but it would be better to have them
875 ;; in a list.
Barry Warsaw81437461996-08-01 19:48:02 +0000876 (setq def-pos
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000877 (or (match-beginning class-paren)
878 (match-beginning def-paren)))
Barry Warsaw81437461996-08-01 19:48:02 +0000879 ;; if we don't have a starting indent level, take this one
880 (or start-indent
881 (setq start-indent cur-indent))
Barry Warsaw81437461996-08-01 19:48:02 +0000882 ;; if we don't have class name yet, take this one
883 (or prev-name
884 (setq prev-name def-name))
Barry Warsaw81437461996-08-01 19:48:02 +0000885 ;; what level is the next definition on? must be same, deeper
886 ;; or shallower indentation
887 (cond
888 ;; at the same indent level, add it to the list...
889 ((= start-indent cur-indent)
Barry Warsaw81437461996-08-01 19:48:02 +0000890 (push (cons def-name def-pos) index-alist))
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000891 ;; deeper indented expression, recurse
Barry Warsaw81437461996-08-01 19:48:02 +0000892 ((< start-indent cur-indent)
Barry Warsaw81437461996-08-01 19:48:02 +0000893 ;; the point is currently on the expression we're supposed to
894 ;; start on, so go back to the last expression. The recursive
895 ;; call will find this place again and add it to the correct
896 ;; list
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000897 (re-search-backward py-imenu-generic-regexp (point-min) 'move)
898 (setq sub-method-alist (py-imenu-create-index-engine cur-indent))
Barry Warsaw81437461996-08-01 19:48:02 +0000899 (if sub-method-alist
900 ;; we put the last element on the index-alist on the start
901 ;; of the submethod alist so the user can still get to it.
902 (let ((save-elmt (pop index-alist)))
Barry Warsawc8520351997-11-26 06:14:40 +0000903 (push (cons prev-name
Barry Warsaw81437461996-08-01 19:48:02 +0000904 (cons save-elmt sub-method-alist))
905 index-alist))))
Barry Warsaw81437461996-08-01 19:48:02 +0000906 ;; found less indented expression, we're done.
907 (t
908 (setq looking-p nil)
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000909 (re-search-backward py-imenu-generic-regexp (point-min) t)))
910 ;; end-cond
Barry Warsaw81437461996-08-01 19:48:02 +0000911 (setq prev-name def-name)
912 (and looking-p
913 (setq looking-p
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000914 (re-search-forward py-imenu-generic-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000915 (point-max) 'move))))
916 (nreverse index-alist)))
917
Barry Warsaw42f707f1996-07-29 21:05:05 +0000918
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000919;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000920(defun python-mode ()
921 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000922To submit a problem report, enter `\\[py-submit-bug-report]' from a
923`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
924documentation. To see what version of `python-mode' you are running,
925enter `\\[py-version]'.
926
927This mode knows about Python indentation, tokens, comments and
928continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000929
930COMMANDS
931\\{py-mode-map}
932VARIABLES
933
Barry Warsaw42f707f1996-07-29 21:05:05 +0000934py-indent-offset\t\tindentation increment
Barry Warsaw41a05c71998-08-10 16:33:12 +0000935py-block-comment-prefix\t\tcomment string used by `comment-region'
Barry Warsaw42f707f1996-07-29 21:05:05 +0000936py-python-command\t\tshell command to invoke Python interpreter
Barry Warsaw42f707f1996-07-29 21:05:05 +0000937py-temp-directory\t\tdirectory used for temp files (if needed)
Barry Warsaw41a05c71998-08-10 16:33:12 +0000938py-beep-if-tab-change\t\tring the bell if `tab-width' is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +0000939 (interactive)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000940 ;; set up local variables
Barry Warsaw7ae77681994-12-12 20:38:05 +0000941 (kill-all-local-variables)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000942 (make-local-variable 'font-lock-defaults)
943 (make-local-variable 'paragraph-separate)
944 (make-local-variable 'paragraph-start)
945 (make-local-variable 'require-final-newline)
946 (make-local-variable 'comment-start)
Barry Warsaw5c8bef11996-12-17 21:56:10 +0000947 (make-local-variable 'comment-end)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000948 (make-local-variable 'comment-start-skip)
949 (make-local-variable 'comment-column)
Barry Warsaw003932a1998-07-07 15:11:24 +0000950 (make-local-variable 'comment-indent-function)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000951 (make-local-variable 'indent-region-function)
952 (make-local-variable 'indent-line-function)
Barry Warsawb3e81d51996-09-04 15:12:42 +0000953 (make-local-variable 'add-log-current-defun-function)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000954 ;;
Barry Warsaw7ae77681994-12-12 20:38:05 +0000955 (set-syntax-table py-mode-syntax-table)
Barry Warsaw003932a1998-07-07 15:11:24 +0000956 (setq major-mode 'python-mode
957 mode-name "Python"
958 local-abbrev-table python-mode-abbrev-table
959 font-lock-defaults '(python-font-lock-keywords)
960 paragraph-separate "^[ \t]*$"
961 paragraph-start "^[ \t]*$"
962 require-final-newline t
963 comment-start "# "
964 comment-end ""
965 comment-start-skip "# *"
966 comment-column 40
967 comment-indent-function 'py-comment-indent-function
968 indent-region-function 'py-indent-region
969 indent-line-function 'py-indent-line
Barry Warsawb3e81d51996-09-04 15:12:42 +0000970 ;; tell add-log.el how to find the current function/method/variable
971 add-log-current-defun-function 'py-current-defun
Barry Warsawb1f89511996-09-03 16:38:30 +0000972 )
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000973 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +0000974 ;; add the menu
975 (if py-menu
976 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +0000977 ;; Emacs 19 requires this
Barry Warsawc72c11c1997-08-09 06:42:08 +0000978 (if (boundp 'comment-multi-line)
Barry Warsaw57697af1995-09-14 20:01:14 +0000979 (setq comment-multi-line nil))
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000980 ;; Install Imenu if available
Barry Warsaw742a5111998-03-13 17:29:15 +0000981 (when (py-safe (require 'imenu))
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000982 (setq imenu-create-index-function #'py-imenu-create-index-function)
983 (setq imenu-generic-expression py-imenu-generic-expression)
Barry Warsaw742a5111998-03-13 17:29:15 +0000984 (if (fboundp 'imenu-add-to-menubar)
985 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
986 )
987 ;; Run the mode hook. Note that py-mode-hook is deprecated.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000988 (if python-mode-hook
989 (run-hooks 'python-mode-hook)
Barry Warsaw742a5111998-03-13 17:29:15 +0000990 (run-hooks 'py-mode-hook))
991 ;; Now do the automagical guessing
992 (if py-smart-indentation
993 (let ((offset py-indent-offset))
Barry Warsaw145ab1c1998-05-19 14:49:49 +0000994 ;; It's okay if this fails to guess a good value
Barry Warsaw742a5111998-03-13 17:29:15 +0000995 (if (and (py-safe (py-guess-indent-offset))
996 (<= py-indent-offset 8)
997 (>= py-indent-offset 2))
998 (setq offset py-indent-offset))
999 (setq py-indent-offset offset)
Barry Warsaw639eea61998-03-16 18:12:13 +00001000 ;; Only turn indent-tabs-mode off if tab-width !=
1001 ;; py-indent-offset. Never turn it on, because the user must
1002 ;; have explicitly turned it off.
1003 (if (/= tab-width py-indent-offset)
1004 (setq indent-tabs-mode nil))
Barry Warsaw11f21561999-07-28 21:59:43 +00001005 ))
1006 ;; Set the default shell if not already set
1007 (when (null py-which-shell)
1008 (py-toggle-shells py-default-interpreter))
1009 )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001010
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001011
Barry Warsawb91b7431995-03-14 15:55:20 +00001012;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001013(defun py-outdent-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00001014 "Returns non-nil if the current line should dedent one level."
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001015 (save-excursion
1016 (and (progn (back-to-indentation)
1017 (looking-at py-outdent-re))
Barry Warsaw1a1c6bb1999-01-09 17:22:38 +00001018 ;; short circuit infloop on illegal construct
1019 (not (bobp))
Barry Warsawf06777d1998-01-21 05:36:18 +00001020 (progn (forward-line -1)
1021 (py-goto-initial-line)
1022 (back-to-indentation)
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001023 (while (or (looking-at py-blank-or-comment-re)
1024 (bobp))
1025 (backward-to-indentation 1))
1026 (not (looking-at py-no-outdent-re)))
1027 )))
1028
Barry Warsawb91b7431995-03-14 15:55:20 +00001029(defun py-electric-colon (arg)
1030 "Insert a colon.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001031In certain cases the line is dedented appropriately. If a numeric
1032argument ARG is provided, that many colons are inserted
1033non-electrically. Electric behavior is inhibited inside a string or
1034comment."
Barry Warsawb91b7431995-03-14 15:55:20 +00001035 (interactive "P")
1036 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001037 ;; are we in a string or comment?
1038 (if (save-excursion
1039 (let ((pps (parse-partial-sexp (save-excursion
Barry Warsawab0e86c1998-05-19 15:31:46 +00001040 (py-beginning-of-def-or-class)
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001041 (point))
1042 (point))))
1043 (not (or (nth 3 pps) (nth 4 pps)))))
1044 (save-excursion
1045 (let ((here (point))
1046 (outdent 0)
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001047 (indent (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001048 (if (and (not arg)
1049 (py-outdent-p)
1050 (= indent (save-excursion
Barry Warsawa7661821996-08-02 16:22:43 +00001051 (py-next-statement -1)
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001052 (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001053 )
1054 (setq outdent py-indent-offset))
Barry Warsaw41a05c71998-08-10 16:33:12 +00001055 ;; Don't indent, only dedent. This assumes that any lines
1056 ;; that are already dedented relative to
1057 ;; py-compute-indentation were put there on purpose. It's
1058 ;; highly annoying to have `:' indent for you. Use TAB, C-c
1059 ;; C-l or C-c C-r to adjust. TBD: Is there a better way to
1060 ;; determine this???
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001061 (if (< (current-indentation) indent) nil
1062 (goto-char here)
1063 (beginning-of-line)
1064 (delete-horizontal-space)
1065 (indent-to (- indent outdent))
1066 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +00001067
1068
Barry Warsawa97a3f31997-11-04 18:47:06 +00001069;; Python subprocess utilities and filters
1070(defun py-execute-file (proc filename)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001071 "Send to Python interpreter process PROC \"execfile('FILENAME')\".
1072Make that process's buffer visible and force display. Also make
1073comint believe the user typed this string so that
1074`kill-output-from-shell' does The Right Thing."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001075 (let ((curbuf (current-buffer))
1076 (procbuf (process-buffer proc))
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001077; (comint-scroll-to-bottom-on-output t)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001078 (msg (format "## working on region in file %s...\n" filename))
Barry Warsaw02e5f691998-09-24 23:48:40 +00001079 (cmd (format "execfile(r'%s')\n" filename)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001080 (unwind-protect
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001081 (save-excursion
Barry Warsawa97a3f31997-11-04 18:47:06 +00001082 (set-buffer procbuf)
1083 (goto-char (point-max))
1084 (move-marker (process-mark proc) (point))
1085 (funcall (process-filter proc) proc msg))
1086 (set-buffer curbuf))
1087 (process-send-string proc cmd)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001088
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001089(defun py-comint-output-filter-function (string)
1090 "Watch output for Python prompt and exec next file waiting in queue.
1091This function is appropriate for `comint-output-filter-functions'."
Barry Warsaw014e0e21998-11-17 19:24:47 +00001092 ;; TBD: this should probably use split-string
Barry Warsaw4f94c731998-09-25 19:40:10 +00001093 (when (and (or (string-equal string ">>> ")
Barry Warsaw4f94c731998-09-25 19:40:10 +00001094 (and (>= (length string) 5)
1095 (string-equal (substring string -5) "\n>>> ")))
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001096 py-file-queue)
1097 (py-safe (delete-file (car py-file-queue)))
1098 (setq py-file-queue (cdr py-file-queue))
1099 (if py-file-queue
1100 (let ((pyproc (get-buffer-process (current-buffer))))
1101 (py-execute-file pyproc (car py-file-queue))))
1102 ))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001103
Barry Warsaw4f577d22001-04-11 20:23:17 +00001104(defun py-pdbtrack-overlay-arrow (activation)
1105 "Activate or de arrow at beginning-of-line in current buffer."
1106 ;; This was derived/simplified from edebug-overlay-arrow
1107 (cond (activation
1108 (setq overlay-arrow-position (make-marker))
1109 (setq overlay-arrow-string "=>")
1110 (set-marker overlay-arrow-position (py-point 'bol) (current-buffer))
1111 (setq py-pdbtrack-is-tracking-p t))
1112 (overlay-arrow-position
1113 (setq overlay-arrow-position nil)
1114 (setq py-pdbtrack-is-tracking-p nil))
1115 ))
1116
1117(defun py-pdbtrack-track-stack-file (text)
1118 "Show the file indicated by the pdb stack entry line, in a separate window.
1119
1120Activity is disabled if the buffer-local variable
1121`py-pdbtrack-do-tracking-p' is nil.
1122
1123We depend on the pdb input prompt matching `py-pdbtrack-input-prompt'
1124at the beginning of the line."
1125 ;; Instead of trying to piece things together from partial text
1126 ;; (which can be almost useless depending on Emacs version), we
1127 ;; monitor to the point where we have the next pdb prompt, and then
1128 ;; check all text from comint-last-input-end to process-mark.
1129 ;;
1130 ;; KLM: It might be nice to provide an optional override, so this
1131 ;; routine could be fed debugger output strings as the text
1132 ;; argument, for deliberate application elsewhere.
1133 ;;
1134 ;; KLM: We're very conservative about clearing the overlay arrow, to
1135 ;; minimize residue. This means, for instance, that executing other
1136 ;; pdb commands wipes out the highlight.
1137 (let* ((origbuf (current-buffer))
1138 (currproc (get-buffer-process origbuf)))
1139 (if (not (and currproc py-pdbtrack-do-tracking-p))
1140 (py-pdbtrack-overlay-arrow nil)
1141 (let* (;(origdir default-directory)
1142 (procmark (process-mark currproc))
1143 (block (buffer-substring (max comint-last-input-end
1144 (- procmark
1145 py-pdbtrack-track-range))
1146 procmark))
1147 fname lineno)
1148 (if (not (string-match (concat py-pdbtrack-input-prompt "$") block))
1149 (py-pdbtrack-overlay-arrow nil)
1150 (if (not (string-match
1151 (concat ".*" py-pdbtrack-stack-entry-regexp ".*")
1152 block))
1153 (py-pdbtrack-overlay-arrow nil)
1154 (setq fname (match-string 1 block)
1155 lineno (match-string 2 block))
1156 (if (file-exists-p fname)
1157 (progn
1158 (find-file-other-window fname)
1159 (goto-line (string-to-int lineno))
Barry Warsawc8c1a5b2001-04-11 22:27:41 +00001160 (message "pdbtrack: line %s, file %s" lineno fname)
Barry Warsaw4f577d22001-04-11 20:23:17 +00001161 (py-pdbtrack-overlay-arrow t)
1162 (pop-to-buffer origbuf t) )
1163 (if (= (elt fname 0) ?\<)
Barry Warsawc8c1a5b2001-04-11 22:27:41 +00001164 (message "pdbtrack: (Non-file source: '%s')" fname)
1165 (message "pdbtrack: File not found: %s" fname))
Barry Warsaw4f577d22001-04-11 20:23:17 +00001166 )))))))
1167
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001168(defun py-postprocess-output-buffer (buf)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001169 "Highlight exceptions found in BUF.
1170If an exception occurred return t, otherwise return nil. BUF must exist."
Barry Warsawf9b99f41998-03-26 16:08:59 +00001171 (let (line file bol err-p)
Barry Warsaw9981d221997-12-03 05:25:48 +00001172 (save-excursion
1173 (set-buffer buf)
1174 (beginning-of-buffer)
1175 (while (re-search-forward py-traceback-line-re nil t)
1176 (setq file (match-string 1)
1177 line (string-to-int (match-string 2))
1178 bol (py-point 'bol))
Barry Warsawf9b99f41998-03-26 16:08:59 +00001179 (py-highlight-line bol (py-point 'eol) file line)))
1180 (when (and py-jump-on-exception line)
1181 (beep)
1182 (py-jump-to-exception file line)
1183 (setq err-p t))
1184 err-p))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001185
Barry Warsaw9981d221997-12-03 05:25:48 +00001186
Barry Warsawa97a3f31997-11-04 18:47:06 +00001187
1188;;; Subprocess commands
1189
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001190;; only used when (memq 'broken-temp-names py-emacs-features)
1191(defvar py-serial-number 0)
1192(defvar py-exception-buffer nil)
1193(defconst py-output-buffer "*Python Output*")
Barry Warsawa2398801998-04-09 23:28:20 +00001194(make-variable-buffer-local 'py-output-buffer)
1195
1196;; for toggling between CPython and JPython
Barry Warsawaa384fd1999-02-16 23:36:16 +00001197(defvar py-which-shell nil)
Barry Warsawa2398801998-04-09 23:28:20 +00001198(defvar py-which-args py-python-command-args)
1199(defvar py-which-bufname "Python")
1200(make-variable-buffer-local 'py-which-shell)
1201(make-variable-buffer-local 'py-which-args)
1202(make-variable-buffer-local 'py-which-bufname)
1203
1204(defun py-toggle-shells (arg)
1205 "Toggles between the CPython and JPython shells.
Barry Warsaw11f21561999-07-28 21:59:43 +00001206
Barry Warsaw41a05c71998-08-10 16:33:12 +00001207With positive argument ARG (interactively \\[universal-argument]),
1208uses the CPython shell, with negative ARG uses the JPython shell, and
Barry Warsaw11f21561999-07-28 21:59:43 +00001209with a zero argument, toggles the shell.
1210
1211Programmatically, ARG can also be one of the symbols `cpython' or
1212`jpython', equivalent to positive arg and negative arg respectively."
Barry Warsawa2398801998-04-09 23:28:20 +00001213 (interactive "P")
1214 ;; default is to toggle
1215 (if (null arg)
1216 (setq arg 0))
Barry Warsaw11f21561999-07-28 21:59:43 +00001217 ;; preprocess arg
1218 (cond
1219 ((equal arg 0)
1220 ;; toggle
1221 (if (string-equal py-which-bufname "Python")
1222 (setq arg -1)
1223 (setq arg 1)))
1224 ((equal arg 'cpython) (setq arg 1))
1225 ((equal arg 'jpython) (setq arg -1)))
Barry Warsawa2398801998-04-09 23:28:20 +00001226 (let (msg)
1227 (cond
1228 ((< 0 arg)
1229 ;; set to CPython
1230 (setq py-which-shell py-python-command
1231 py-which-args py-python-command-args
1232 py-which-bufname "Python"
1233 msg "CPython"
1234 mode-name "Python"))
1235 ((> 0 arg)
1236 (setq py-which-shell py-jpython-command
1237 py-which-args py-jpython-command-args
1238 py-which-bufname "JPython"
1239 msg "JPython"
1240 mode-name "JPython"))
1241 )
Barry Warsawea609c11998-04-10 16:08:26 +00001242 (message "Using the %s shell" msg)
Barry Warsawa2398801998-04-09 23:28:20 +00001243 (setq py-output-buffer (format "*%s Output*" py-which-bufname))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001244
Barry Warsawa97a3f31997-11-04 18:47:06 +00001245;;;###autoload
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001246(defun py-shell (&optional argprompt)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001247 "Start an interactive Python interpreter in another window.
1248This is like Shell mode, except that Python is running in the window
1249instead of a shell. See the `Interactive Shell' and `Shell Mode'
1250sections of the Emacs manual for details, especially for the key
1251bindings active in the `*Python*' buffer.
1252
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001253With optional \\[universal-argument], the user is prompted for the
1254flags to pass to the Python interpreter. This has no effect when this
1255command is used to switch to an existing process, only when a new
1256process is started. If you use this, you will probably want to ensure
1257that the current arguments are retained (they will be included in the
1258prompt). This argument is ignored when this function is called
1259programmatically, or when running in Emacs 19.34 or older.
1260
Barry Warsawa2398801998-04-09 23:28:20 +00001261Note: You can toggle between using the CPython interpreter and the
1262JPython interpreter by hitting \\[py-toggle-shells]. This toggles
1263buffer local variables which control whether all your subshell
1264interactions happen to the `*JPython*' or `*Python*' buffers (the
1265latter is the name used for the CPython buffer).
1266
Barry Warsawa97a3f31997-11-04 18:47:06 +00001267Warning: Don't use an interactive Python if you change sys.ps1 or
1268sys.ps2 from their default values, or if you're running code that
1269prints `>>> ' or `... ' at the start of a line. `python-mode' can't
1270distinguish your output from Python's output, and assumes that `>>> '
1271at the start of a line is a prompt from Python. Similarly, the Emacs
1272Shell mode code assumes that both `>>> ' and `... ' at the start of a
1273line are Python prompts. Bad things can happen if you fool either
1274mode.
1275
1276Warning: If you do any editing *in* the process buffer *while* the
1277buffer is accepting output from Python, do NOT attempt to `undo' the
1278changes. Some of the output (nowhere near the parts you changed!) may
1279be lost if you do. This appears to be an Emacs bug, an unfortunate
1280interaction between undo and process filters; the same problem exists in
1281non-Python process buffers using the default (Emacs-supplied) process
1282filter."
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001283 (interactive "P")
Barry Warsaw50765ab1999-08-10 21:49:00 +00001284 ;; Set the default shell if not already set
1285 (when (null py-which-shell)
1286 (py-toggle-shells py-default-interpreter))
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001287 (let ((args py-which-args))
1288 (when (and argprompt
1289 (interactive-p)
1290 (fboundp 'split-string))
1291 ;; TBD: Perhaps force "-i" in the final list?
1292 (setq args (split-string
1293 (read-string (concat py-which-bufname
1294 " arguments: ")
1295 (concat
1296 (mapconcat 'identity py-which-args " ") " ")
1297 ))))
1298 (switch-to-buffer-other-window
1299 (apply 'make-comint py-which-bufname py-which-shell nil args))
1300 (make-local-variable 'comint-prompt-regexp)
1301 (setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ")
1302 (add-hook 'comint-output-filter-functions
1303 'py-comint-output-filter-function)
Barry Warsaw4f577d22001-04-11 20:23:17 +00001304 ;; pdbtrack
1305 (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
1306 (setq py-pdbtrack-do-tracking-p t)
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001307 (set-syntax-table py-mode-syntax-table)
1308 (use-local-map py-shell-map)
1309 ))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001310
Barry Warsawa97a3f31997-11-04 18:47:06 +00001311(defun py-clear-queue ()
1312 "Clear the queue of temporary files waiting to execute."
1313 (interactive)
1314 (let ((n (length py-file-queue)))
1315 (mapcar 'delete-file py-file-queue)
1316 (setq py-file-queue nil)
1317 (message "%d pending files de-queued." n)))
1318
Barry Warsaw820273d1998-05-19 15:54:45 +00001319
Barry Warsawa97a3f31997-11-04 18:47:06 +00001320(defun py-execute-region (start end &optional async)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001321 "Execute the region in a Python interpreter.
1322
Barry Warsawa97a3f31997-11-04 18:47:06 +00001323The region is first copied into a temporary file (in the directory
1324`py-temp-directory'). If there is no Python interpreter shell
1325running, this file is executed synchronously using
Barry Warsaw41a05c71998-08-10 16:33:12 +00001326`shell-command-on-region'. If the program is long running, use
1327\\[universal-argument] to run the command asynchronously in its own
1328buffer.
1329
1330When this function is used programmatically, arguments START and END
1331specify the region to execute, and optional third argument ASYNC, if
1332non-nil, specifies to run the command asynchronously in its own
1333buffer.
Barry Warsawa97a3f31997-11-04 18:47:06 +00001334
1335If the Python interpreter shell is running, the region is execfile()'d
1336in that shell. If you try to execute regions too quickly,
1337`python-mode' will queue them up and execute them one at a time when
1338it sees a `>>> ' prompt from Python. Each time this happens, the
1339process buffer is popped into a window (if it's not already in some
1340window) so you can see it, and a comment of the form
1341
1342 \t## working on region in file <name>...
1343
1344is inserted at the end. See also the command `py-clear-queue'."
1345 (interactive "r\nP")
1346 (or (< start end)
1347 (error "Region is empty"))
Barry Warsaw014e0e21998-11-17 19:24:47 +00001348 (let* ((proc (get-process py-which-bufname))
Barry Warsaw5e21cb01997-11-05 18:41:11 +00001349 (temp (if (memq 'broken-temp-names py-emacs-features)
Barry Warsaw1b344241998-08-07 22:24:16 +00001350 (let
1351 ((sn py-serial-number)
1352 (pid (and (fboundp 'emacs-pid) (emacs-pid))))
1353 (setq py-serial-number (1+ py-serial-number))
1354 (if pid
1355 (format "python-%d-%d" sn pid)
1356 (format "python-%d" sn)))
Barry Warsaw2f32fbb1998-02-25 16:45:43 +00001357 (make-temp-name "python-")))
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001358 (file (expand-file-name temp py-temp-directory))
1359 (cur (current-buffer))
1360 (buf (get-buffer-create file)))
1361 ;; Write the contents of the buffer, watching out for indented regions.
1362 (save-excursion
1363 (goto-char start)
Barry Warsaw99eadf42000-06-23 20:24:25 +00001364 (let ((needs-if (/= (py-point 'bol) (py-point 'boi))))
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001365 (set-buffer buf)
Barry Warsaw99eadf42000-06-23 20:24:25 +00001366 (when needs-if
1367 (insert "if 1:\n"))
1368 (insert-buffer-substring cur start end)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001369 (cond
Barry Warsaw145ab1c1998-05-19 14:49:49 +00001370 ;; always run the code in its own asynchronous subprocess
Barry Warsawa97a3f31997-11-04 18:47:06 +00001371 (async
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001372 ;; User explicitly wants this to run in its own async subprocess
1373 (save-excursion
1374 (set-buffer buf)
1375 (write-region (point-min) (point-max) file nil 'nomsg))
Barry Warsaw34d83171998-11-20 03:04:07 +00001376 (let* ((buf (generate-new-buffer-name py-output-buffer))
1377 ;; TBD: a horrible hack, but why create new Custom variables?
1378 (arg (if (string-equal py-which-bufname "Python")
1379 "-u" "")))
1380 (start-process py-which-bufname buf py-which-shell arg file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001381 (pop-to-buffer buf)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001382 (py-postprocess-output-buffer buf)
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001383 ;; TBD: clean up the temporary file!
Barry Warsawa97a3f31997-11-04 18:47:06 +00001384 ))
1385 ;; if the Python interpreter shell is running, queue it up for
1386 ;; execution there.
1387 (proc
1388 ;; use the existing python shell
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001389 (save-excursion
1390 (set-buffer buf)
1391 (write-region (point-min) (point-max) file nil 'nomsg))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001392 (if (not py-file-queue)
1393 (py-execute-file proc file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001394 (message "File %s queued for execution" file))
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001395 (setq py-file-queue (append py-file-queue (list file)))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001396 (setq py-exception-buffer (cons file (current-buffer))))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001397 (t
Barry Warsaw34d83171998-11-20 03:04:07 +00001398 ;; TBD: a horrible hack, buy why create new Custom variables?
1399 (let ((cmd (concat py-which-shell
1400 (if (string-equal py-which-bufname "JPython")
1401 " -" ""))))
1402 ;; otherwise either run it synchronously in a subprocess
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001403 (save-excursion
1404 (set-buffer buf)
1405 (shell-command-on-region (point-min) (point-max)
1406 cmd py-output-buffer))
Barry Warsaw34d83171998-11-20 03:04:07 +00001407 ;; shell-command-on-region kills the output buffer if it never
1408 ;; existed and there's no output from the command
1409 (if (not (get-buffer py-output-buffer))
1410 (message "No output.")
1411 (setq py-exception-buffer (current-buffer))
1412 (let ((err-p (py-postprocess-output-buffer py-output-buffer)))
1413 (pop-to-buffer py-output-buffer)
1414 (if err-p
1415 (pop-to-buffer py-exception-buffer)))
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001416 ))
1417 ;; TBD: delete the buffer
1418 )
Barry Warsaw71534602001-02-20 23:07:56 +00001419 )
1420 ;; Clean up after ourselves.
1421 (kill-buffer buf)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001422
Barry Warsaw820273d1998-05-19 15:54:45 +00001423
1424;; Code execution commands
Barry Warsawa97a3f31997-11-04 18:47:06 +00001425(defun py-execute-buffer (&optional async)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001426 "Send the contents of the buffer to a Python interpreter.
Barry Warsawc0ecb531998-01-20 21:43:34 +00001427If the file local variable `py-master-file' is non-nil, execute the
1428named file instead of the buffer's file.
1429
Barry Warsaw7ae77681994-12-12 20:38:05 +00001430If there is a *Python* process buffer it is used. If a clipping
1431restriction is in effect, only the accessible portion of the buffer is
1432sent. A trailing newline will be supplied if needed.
1433
Barry Warsaw41a05c71998-08-10 16:33:12 +00001434See the `\\[py-execute-region]' docs for an account of some
1435subtleties, including the use of the optional ASYNC argument."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001436 (interactive "P")
Barry Warsawc0ecb531998-01-20 21:43:34 +00001437 (if py-master-file
1438 (let* ((filename (expand-file-name py-master-file))
1439 (buffer (or (get-file-buffer filename)
1440 (find-file-noselect filename))))
1441 (set-buffer buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001442 (py-execute-region (point-min) (point-max) async))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001443
Barry Warsaw820273d1998-05-19 15:54:45 +00001444(defun py-execute-import-or-reload (&optional async)
1445 "Import the current buffer's file in a Python interpreter.
1446
1447If the file has already been imported, then do reload instead to get
Barry Warsaw7c29b231998-07-07 17:45:38 +00001448the latest version.
1449
1450If the file's name does not end in \".py\", then do execfile instead.
1451
1452If the current buffer is not visiting a file, do `py-execute-buffer'
1453instead.
1454
1455If the file local variable `py-master-file' is non-nil, import or
1456reload the named file instead of the buffer's file. The file may be
1457saved based on the value of `py-execute-import-or-reload-save-p'.
Barry Warsaw820273d1998-05-19 15:54:45 +00001458
Barry Warsaw41a05c71998-08-10 16:33:12 +00001459See the `\\[py-execute-region]' docs for an account of some
1460subtleties, including the use of the optional ASYNC argument.
Barry Warsaw820273d1998-05-19 15:54:45 +00001461
Barry Warsaw7c29b231998-07-07 17:45:38 +00001462This may be preferable to `\\[py-execute-buffer]' because:
Barry Warsaw820273d1998-05-19 15:54:45 +00001463
1464 - Definitions stay in their module rather than appearing at top
1465 level, where they would clutter the global namespace and not affect
1466 uses of qualified names (MODULE.NAME).
1467
1468 - The Python debugger gets line number information about the functions."
1469 (interactive "P")
1470 ;; Check file local variable py-master-file
1471 (if py-master-file
1472 (let* ((filename (expand-file-name py-master-file))
1473 (buffer (or (get-file-buffer filename)
1474 (find-file-noselect filename))))
1475 (set-buffer buffer)))
1476 (let ((file (buffer-file-name (current-buffer))))
1477 (if file
1478 (progn
Barry Warsaw3bfed5b1998-05-19 16:25:04 +00001479 ;; Maybe save some buffers
1480 (save-some-buffers (not py-ask-about-save) nil)
Barry Warsaw820273d1998-05-19 15:54:45 +00001481 (py-execute-string
1482 (if (string-match "\\.py$" file)
1483 (let ((f (file-name-sans-extension
1484 (file-name-nondirectory file))))
1485 (format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
1486 f f f))
Barry Warsaw02e5f691998-09-24 23:48:40 +00001487 (format "execfile(r'%s')\n" file))
Barry Warsaw820273d1998-05-19 15:54:45 +00001488 async))
1489 ;; else
1490 (py-execute-buffer async))))
1491
1492
1493(defun py-execute-def-or-class (&optional async)
1494 "Send the current function or class definition to a Python interpreter.
1495
1496If there is a *Python* process buffer it is used.
1497
Barry Warsaw41a05c71998-08-10 16:33:12 +00001498See the `\\[py-execute-region]' docs for an account of some
1499subtleties, including the use of the optional ASYNC argument."
Barry Warsaw820273d1998-05-19 15:54:45 +00001500 (interactive "P")
1501 (save-excursion
1502 (py-mark-def-or-class)
1503 ;; mark is before point
1504 (py-execute-region (mark) (point) async)))
1505
1506
1507(defun py-execute-string (string &optional async)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001508 "Send the argument STRING to a Python interpreter.
Barry Warsaw820273d1998-05-19 15:54:45 +00001509
1510If there is a *Python* process buffer it is used.
1511
Barry Warsaw41a05c71998-08-10 16:33:12 +00001512See the `\\[py-execute-region]' docs for an account of some
1513subtleties, including the use of the optional ASYNC argument."
Barry Warsaw820273d1998-05-19 15:54:45 +00001514 (interactive "sExecute Python command: ")
1515 (save-excursion
1516 (set-buffer (get-buffer-create
1517 (generate-new-buffer-name " *Python Command*")))
1518 (insert string)
1519 (py-execute-region (point-min) (point-max) async)))
1520
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001521
1522
1523(defun py-jump-to-exception (file line)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001524 "Jump to the Python code in FILE at LINE."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001525 (let ((buffer (cond ((string-equal file "<stdin>")
Barry Warsawebc7b7a1998-05-19 15:01:06 +00001526 (if (consp py-exception-buffer)
1527 (cdr py-exception-buffer)
1528 py-exception-buffer))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001529 ((and (consp py-exception-buffer)
1530 (string-equal file (car py-exception-buffer)))
1531 (cdr py-exception-buffer))
1532 ((py-safe (find-file-noselect file)))
1533 ;; could not figure out what file the exception
1534 ;; is pointing to, so prompt for it
1535 (t (find-file (read-file-name "Exception file: "
1536 nil
1537 file t))))))
1538 (pop-to-buffer buffer)
Barry Warsawebc7b7a1998-05-19 15:01:06 +00001539 ;; Force Python mode
Barry Warsaw820273d1998-05-19 15:54:45 +00001540 (if (not (eq major-mode 'python-mode))
Barry Warsawebc7b7a1998-05-19 15:01:06 +00001541 (python-mode))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001542 (goto-line line)
1543 (message "Jumping to exception in file %s on line %d" file line)))
1544
1545(defun py-mouseto-exception (event)
Barry Warsaw12c92941998-08-10 21:44:37 +00001546 "Jump to the code which caused the Python exception at EVENT.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001547EVENT is usually a mouse click."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001548 (interactive "e")
1549 (cond
1550 ((fboundp 'event-point)
1551 ;; XEmacs
1552 (let* ((point (event-point event))
1553 (buffer (event-buffer event))
1554 (e (and point buffer (extent-at point buffer 'py-exc-info)))
1555 (info (and e (extent-property e 'py-exc-info))))
1556 (message "Event point: %d, info: %s" point info)
1557 (and info
1558 (py-jump-to-exception (car info) (cdr info)))
1559 ))
1560 ;; Emacs -- Please port this!
1561 ))
1562
1563(defun py-goto-exception ()
1564 "Go to the line indicated by the traceback."
1565 (interactive)
1566 (let (file line)
1567 (save-excursion
1568 (beginning-of-line)
1569 (if (looking-at py-traceback-line-re)
1570 (setq file (match-string 1)
1571 line (string-to-int (match-string 2)))))
1572 (if (not file)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001573 (error "Not on a traceback line"))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001574 (py-jump-to-exception file line)))
1575
1576(defun py-find-next-exception (start buffer searchdir errwhere)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001577 "Find the next Python exception and jump to the code that caused it.
1578START is the buffer position in BUFFER from which to begin searching
1579for an exception. SEARCHDIR is a function, either
1580`re-search-backward' or `re-search-forward' indicating the direction
1581to search. ERRWHERE is used in an error message if the limit (top or
1582bottom) of the trackback stack is encountered."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001583 (let (file line)
1584 (save-excursion
1585 (set-buffer buffer)
1586 (goto-char (py-point start))
1587 (if (funcall searchdir py-traceback-line-re nil t)
1588 (setq file (match-string 1)
1589 line (string-to-int (match-string 2)))))
1590 (if (and file line)
1591 (py-jump-to-exception file line)
1592 (error "%s of traceback" errwhere))))
1593
1594(defun py-down-exception (&optional bottom)
1595 "Go to the next line down in the traceback.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001596With \\[univeral-argument] (programmatically, optional argument
1597BOTTOM), jump to the bottom (innermost) exception in the exception
1598stack."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001599 (interactive "P")
1600 (let* ((proc (get-process "Python"))
1601 (buffer (if proc "*Python*" py-output-buffer)))
1602 (if bottom
1603 (py-find-next-exception 'eob buffer 're-search-backward "Bottom")
1604 (py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
1605
1606(defun py-up-exception (&optional top)
1607 "Go to the previous line up in the traceback.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001608With \\[universal-argument] (programmatically, optional argument TOP)
1609jump to the top (outermost) exception in the exception stack."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001610 (interactive "P")
1611 (let* ((proc (get-process "Python"))
1612 (buffer (if proc "*Python*" py-output-buffer)))
1613 (if top
1614 (py-find-next-exception 'bob buffer 're-search-forward "Top")
Barry Warsawffbc17d1997-11-27 20:08:14 +00001615 (py-find-next-exception 'bol buffer 're-search-backward "Top"))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001616
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001617
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001618;; Electric deletion
1619(defun py-electric-backspace (arg)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001620 "Delete preceding character or levels of indentation.
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001621Deletion is performed by calling the function in `py-backspace-function'
Barry Warsawb0539931996-12-17 22:05:07 +00001622with a single argument (the number of characters to delete).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001623
Barry Warsaw41a05c71998-08-10 16:33:12 +00001624If point is at the leftmost column, delete the preceding newline.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001625
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001626Otherwise, if point is at the leftmost non-whitespace character of a
1627line that is neither a continuation line nor a non-indenting comment
1628line, or if point is at the end of a blank line, this command reduces
1629the indentation to match that of the line that opened the current
1630block of code. The line that opened the block is displayed in the
Barry Warsaw41a05c71998-08-10 16:33:12 +00001631echo area to help you keep track of where you are. With
1632\\[universal-argument] dedents that many blocks (but not past column
1633zero).
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001634
1635Otherwise the preceding character is deleted, converting a tab to
1636spaces if needed so that only a single column position is deleted.
Barry Warsawfa2def21999-05-24 21:43:37 +00001637\\[universal-argument] specifies how many characters to delete;
1638default is 1.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001639
1640When used programmatically, argument ARG specifies the number of
1641blocks to dedent, or the number of characters to delete, as indicated
1642above."
Barry Warsaw03970731996-07-03 23:12:52 +00001643 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +00001644 (if (or (/= (current-indentation) (current-column))
1645 (bolp)
1646 (py-continuation-line-p)
Barry Warsawfa2def21999-05-24 21:43:37 +00001647; (not py-honor-comment-indentation)
1648; (looking-at "#[^ \t\n]") ; non-indenting #
1649 )
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001650 (funcall py-backspace-function arg)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001651 ;; else indent the same as the colon line that opened the block
Barry Warsaw7ae77681994-12-12 20:38:05 +00001652 ;; force non-blank so py-goto-block-up doesn't ignore it
1653 (insert-char ?* 1)
1654 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001655 (let ((base-indent 0) ; indentation of base line
1656 (base-text "") ; and text of base line
1657 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +00001658 (save-excursion
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001659 (while (< 0 arg)
Barry Warsaw03970731996-07-03 23:12:52 +00001660 (condition-case nil ; in case no enclosing block
1661 (progn
1662 (py-goto-block-up 'no-mark)
1663 (setq base-indent (current-indentation)
1664 base-text (py-suck-up-leading-text)
1665 base-found-p t))
1666 (error nil))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001667 (setq arg (1- arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001668 (delete-char 1) ; toss the dummy character
1669 (delete-horizontal-space)
1670 (indent-to base-indent)
1671 (if base-found-p
1672 (message "Closes block: %s" base-text)))))
1673
Barry Warsawfc8a01f1995-03-09 16:07:29 +00001674
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001675(defun py-electric-delete (arg)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001676 "Delete preceding or following character or levels of whitespace.
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001677
1678The behavior of this function depends on the variable
1679`delete-key-deletes-forward'. If this variable is nil (or does not
Barry Warsaw41a05c71998-08-10 16:33:12 +00001680exist, as in older Emacsen and non-XEmacs versions), then this
1681function behaves identically to \\[c-electric-backspace].
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001682
1683If `delete-key-deletes-forward' is non-nil and is supported in your
1684Emacs, then deletion occurs in the forward direction, by calling the
Barry Warsaw41a05c71998-08-10 16:33:12 +00001685function in `py-delete-function'.
1686
1687\\[universal-argument] (programmatically, argument ARG) specifies the
1688number of characters to delete (default is 1)."
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001689 (interactive "*p")
Barry Warsaw1d7b0fa1999-01-15 02:12:31 +00001690 (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
1691 (delete-forward-p))
1692 (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
1693 delete-key-deletes-forward))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001694 (funcall py-delete-function arg)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001695 (py-electric-backspace arg)))
1696
1697;; required for pending-del and delsel modes
1698(put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
1699(put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del
1700(put 'py-electric-delete 'delete-selection 'supersede) ;delsel
1701(put 'py-electric-delete 'pending-delete 'supersede) ;pending-del
1702
1703
1704
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001705(defun py-indent-line (&optional arg)
1706 "Fix the indentation of the current line according to Python rules.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001707With \\[universal-argument] (programmatically, the optional argument
1708ARG non-nil), ignore dedenting rules for block closing statements
1709(e.g. return, raise, break, continue, pass)
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001710
1711This function is normally bound to `indent-line-function' so
1712\\[indent-for-tab-command] will call it."
1713 (interactive "P")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001714 (let* ((ci (current-indentation))
1715 (move-to-indentation-p (<= (current-column) ci))
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001716 (need (py-compute-indentation (not arg))))
Barry Warsaw41a05c71998-08-10 16:33:12 +00001717 ;; see if we need to dedent
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001718 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +00001719 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001720 (if (/= ci need)
1721 (save-excursion
1722 (beginning-of-line)
1723 (delete-horizontal-space)
1724 (indent-to need)))
1725 (if move-to-indentation-p (back-to-indentation))))
1726
1727(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001728 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001729This is just `strives to' because correct indentation can't be computed
1730from scratch for Python code. In general, deletes the whitespace before
1731point, inserts a newline, and takes an educated guess as to how you want
1732the new line indented."
1733 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001734 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001735 (if (< ci (current-column)) ; if point beyond indentation
1736 (newline-and-indent)
1737 ;; else try to act like newline-and-indent "normally" acts
1738 (beginning-of-line)
1739 (insert-char ?\n 1)
1740 (move-to-column ci))))
1741
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001742(defun py-compute-indentation (honor-block-close-p)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001743 "Compute Python indentation.
1744When HONOR-BLOCK-CLOSE-P is non-nil, statements such as `return',
1745`raise', `break', `continue', and `pass' force one level of
1746dedenting."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001747 (save-excursion
Barry Warsawf64b4051998-02-12 16:52:14 +00001748 (beginning-of-line)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001749 (let* ((bod (py-point 'bod))
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001750 (pps (parse-partial-sexp bod (point)))
Barry Warsaw9c1696c1998-12-15 04:36:22 +00001751 (boipps (parse-partial-sexp bod (py-point 'boi)))
1752 placeholder)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001753 (cond
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001754 ;; are we inside a multi-line string or comment?
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001755 ((or (and (nth 3 pps) (nth 3 boipps))
1756 (and (nth 4 pps) (nth 4 boipps)))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001757 (save-excursion
1758 (if (not py-align-multiline-strings-p) 0
1759 ;; skip back over blank & non-indenting comment lines
1760 ;; note: will skip a blank or non-indenting comment line
1761 ;; that happens to be a continuation line too
1762 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1763 (back-to-indentation)
1764 (current-column))))
1765 ;; are we on a continuation line?
1766 ((py-continuation-line-p)
1767 (let ((startpos (point))
1768 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001769 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001770 (if open-bracket-pos
1771 (progn
1772 ;; align with first item in list; else a normal
1773 ;; indent beyond the line with the open bracket
1774 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1775 ;; is the first list item on the same line?
1776 (skip-chars-forward " \t")
1777 (if (null (memq (following-char) '(?\n ?# ?\\)))
1778 ; yes, so line up with it
1779 (current-column)
1780 ;; first list item on another line, or doesn't exist yet
1781 (forward-line 1)
1782 (while (and (< (point) startpos)
1783 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1784 (forward-line 1))
Barry Warsaw92166d91998-04-01 21:59:41 +00001785 (if (and (< (point) startpos)
1786 (/= startpos
1787 (save-excursion
1788 (goto-char (1+ open-bracket-pos))
Barry Warsaw77d1fce1998-04-16 20:04:59 +00001789 (forward-comment (point-max))
Barry Warsaw92166d91998-04-01 21:59:41 +00001790 (point))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001791 ;; again mimic the first list item
1792 (current-indentation)
1793 ;; else they're about to enter the first item
1794 (goto-char open-bracket-pos)
Barry Warsaw9c1696c1998-12-15 04:36:22 +00001795 (setq placeholder (point))
1796 (py-goto-initial-line)
1797 (py-goto-beginning-of-tqs
1798 (save-excursion (nth 3 (parse-partial-sexp
1799 placeholder (point)))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001800 (+ (current-indentation) py-indent-offset))))
1801
1802 ;; else on backslash continuation line
1803 (forward-line -1)
1804 (if (py-continuation-line-p) ; on at least 3rd line in block
1805 (current-indentation) ; so just continue the pattern
1806 ;; else started on 2nd line in block, so indent more.
1807 ;; if base line is an assignment with a start on a RHS,
1808 ;; indent to 2 beyond the leftmost "="; else skip first
1809 ;; chunk of non-whitespace characters on base line, + 1 more
1810 ;; column
1811 (end-of-line)
Barry Warsawfd4c9e82001-06-18 23:40:35 +00001812 (setq endpos (point)
1813 searching t)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001814 (back-to-indentation)
1815 (setq startpos (point))
1816 ;; look at all "=" from left to right, stopping at first
1817 ;; one not nested in a list or string
1818 (while searching
1819 (skip-chars-forward "^=" endpos)
1820 (if (= (point) endpos)
1821 (setq searching nil)
1822 (forward-char 1)
1823 (setq state (parse-partial-sexp startpos (point)))
1824 (if (and (zerop (car state)) ; not in a bracket
1825 (null (nth 3 state))) ; & not in a string
1826 (progn
1827 (setq searching nil) ; done searching in any case
1828 (setq found
1829 (not (or
1830 (eq (following-char) ?=)
1831 (memq (char-after (- (point) 2))
1832 '(?< ?> ?!)))))))))
1833 (if (or (not found) ; not an assignment
1834 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1835 (progn
1836 (goto-char startpos)
1837 (skip-chars-forward "^ \t\n")))
Barry Warsaw40fb4522001-07-06 20:07:13 +00001838 ;; if this is a continuation for a block opening
1839 ;; statement, add some extra offset.
1840 (+ (current-column) (if (py-statement-opens-block-p)
1841 py-continuation-offset 0)
1842 1)
Barry Warsawfd4c9e82001-06-18 23:40:35 +00001843 ))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001844
1845 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001846 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001847
Barry Warsawa7891711996-08-01 15:53:09 +00001848 ;; Dfn: "Indenting comment line". A line containing only a
1849 ;; comment, but which is treated like a statement for
1850 ;; indentation calculation purposes. Such lines are only
1851 ;; treated specially by the mode; they are not treated
1852 ;; specially by the Python interpreter.
1853
1854 ;; The rules for indenting comment lines are a line where:
1855 ;; - the first non-whitespace character is `#', and
1856 ;; - the character following the `#' is whitespace, and
Barry Warsaw41a05c71998-08-10 16:33:12 +00001857 ;; - the line is dedented with respect to (i.e. to the left
Barry Warsawa7891711996-08-01 15:53:09 +00001858 ;; of) the indentation of the preceding non-blank line.
1859
1860 ;; The first non-blank line following an indenting comment
1861 ;; line is given the same amount of indentation as the
1862 ;; indenting comment line.
1863
1864 ;; All other comment-only lines are ignored for indentation
1865 ;; purposes.
1866
1867 ;; Are we looking at a comment-only line which is *not* an
Barry Warsaw145ab1c1998-05-19 14:49:49 +00001868 ;; indenting comment line? If so, we assume that it's been
Barry Warsawa7891711996-08-01 15:53:09 +00001869 ;; placed at the desired indentation, so leave it alone.
1870 ;; Indenting comment lines are aligned as statements down
1871 ;; below.
1872 ((and (looking-at "[ \t]*#[^ \t\n]")
1873 ;; NOTE: this test will not be performed in older Emacsen
1874 (fboundp 'forward-comment)
1875 (<= (current-indentation)
1876 (save-excursion
1877 (forward-comment (- (point-max)))
1878 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001879 (current-indentation))
1880
1881 ;; else indentation based on that of the statement that
1882 ;; precedes us; use the first line of that statement to
1883 ;; establish the base, in case the user forced a non-std
1884 ;; indentation for the continuation lines (if any)
1885 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001886 ;; skip back over blank & non-indenting comment lines note:
1887 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001888 ;; happens to be a continuation line too. use fast Emacs 19
1889 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001890 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001891 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001892 (forward-comment (- (point-max)))
Barry Warsaw218eb751998-09-22 19:51:47 +00001893 (let ((prefix-re (concat py-block-comment-prefix "[ \t]*"))
1894 done)
Barry Warsaw6d627751996-03-06 18:41:38 +00001895 (while (not done)
Barry Warsaw12c92941998-08-10 21:44:37 +00001896 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#\\)" nil 'move)
1897 (setq done (or (bobp)
1898 (and (eq py-honor-comment-indentation t)
1899 (save-excursion
1900 (back-to-indentation)
Barry Warsaw218eb751998-09-22 19:51:47 +00001901 (not (looking-at prefix-re))
Barry Warsaw12c92941998-08-10 21:44:37 +00001902 ))
1903 (and (not (eq py-honor-comment-indentation t))
1904 (save-excursion
1905 (back-to-indentation)
Barry Warsawd36cfe42002-03-15 16:46:46 +00001906 (and (not (looking-at prefix-re))
1907 (or (looking-at "[^#]")
1908 (not (zerop (current-column)))
1909 ))
1910 ))
Barry Warsaw12c92941998-08-10 21:44:37 +00001911 ))
Barry Warsaw6d627751996-03-06 18:41:38 +00001912 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001913 ;; if we landed inside a string, go to the beginning of that
1914 ;; string. this handles triple quoted, multi-line spanning
1915 ;; strings.
Barry Warsaw9c1696c1998-12-15 04:36:22 +00001916 (py-goto-beginning-of-tqs (nth 3 (parse-partial-sexp bod (point))))
Barry Warsawc210e691998-01-20 22:52:56 +00001917 ;; now skip backward over continued lines
Barry Warsaw9c1696c1998-12-15 04:36:22 +00001918 (setq placeholder (point))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001919 (py-goto-initial-line)
Barry Warsaw9c1696c1998-12-15 04:36:22 +00001920 ;; we may *now* have landed in a TQS, so find the beginning of
1921 ;; this string.
1922 (py-goto-beginning-of-tqs
1923 (save-excursion (nth 3 (parse-partial-sexp
1924 placeholder (point)))))
Barry Warsawf831d811996-07-31 20:42:59 +00001925 (+ (current-indentation)
1926 (if (py-statement-opens-block-p)
1927 py-indent-offset
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001928 (if (and honor-block-close-p (py-statement-closes-block-p))
Barry Warsawf831d811996-07-31 20:42:59 +00001929 (- py-indent-offset)
1930 0)))
1931 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001932
1933(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001934 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001935
1936By default, make a buffer-local copy of `py-indent-offset' with the
1937new value, so that other Python buffers are not affected. With
1938\\[universal-argument] (programmatically, optional argument GLOBAL),
1939change the global value of `py-indent-offset'. This affects all
1940Python buffers (that don't have their own buffer-local copy), both
1941those currently existing and those created later in the Emacs session.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001942
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001943Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001944There's no excuse for such foolishness, but sometimes you have to deal
1945with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001946`py-indent-offset' to what it thinks it was when they created the
1947mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001948
1949Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001950looking for a line that opens a block of code. `py-indent-offset' is
1951set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001952statement following it. If the search doesn't succeed going forward,
1953it's tried again going backward."
1954 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001955 (let (new-value
1956 (start (point))
Barry Warsawe908b6b1998-03-19 22:48:02 +00001957 (restart (point))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001958 (found nil)
1959 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001960 (py-goto-initial-line)
1961 (while (not (or found (eobp)))
Barry Warsawe908b6b1998-03-19 22:48:02 +00001962 (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1963 (not (py-in-literal restart)))
1964 (setq restart (point))
1965 (py-goto-initial-line)
1966 (if (py-statement-opens-block-p)
1967 (setq found t)
1968 (goto-char restart))))
1969 (unless found
Barry Warsaw7ae77681994-12-12 20:38:05 +00001970 (goto-char start)
1971 (py-goto-initial-line)
1972 (while (not (or found (bobp)))
Barry Warsawe908b6b1998-03-19 22:48:02 +00001973 (setq found (and
1974 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1975 (or (py-goto-initial-line) t) ; always true -- side effect
1976 (py-statement-opens-block-p)))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001977 (setq colon-indent (current-indentation)
1978 found (and found (zerop (py-next-statement 1)))
1979 new-value (- (current-indentation) colon-indent))
1980 (goto-char start)
Barry Warsawe908b6b1998-03-19 22:48:02 +00001981 (if (not found)
1982 (error "Sorry, couldn't guess a value for py-indent-offset")
1983 (funcall (if global 'kill-local-variable 'make-local-variable)
1984 'py-indent-offset)
1985 (setq py-indent-offset new-value)
Barry Warsawd35c2551998-09-25 00:08:38 +00001986 (or noninteractive
1987 (message "%s value of py-indent-offset set to %d"
1988 (if global "Global" "Local")
1989 py-indent-offset)))
Barry Warsawe908b6b1998-03-19 22:48:02 +00001990 ))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001991
Barry Warsaw003932a1998-07-07 15:11:24 +00001992(defun py-comment-indent-function ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00001993 "Python version of `comment-indent-function'."
1994 ;; This is required when filladapt is turned off. Without it, when
1995 ;; filladapt is not used, comments which start in column zero
1996 ;; cascade one character to the right
Barry Warsaw003932a1998-07-07 15:11:24 +00001997 (save-excursion
1998 (beginning-of-line)
1999 (let ((eol (py-point 'eol)))
2000 (and comment-start-skip
2001 (re-search-forward comment-start-skip eol t)
2002 (setq eol (match-beginning 0)))
2003 (goto-char eol)
2004 (skip-chars-backward " \t")
2005 (max comment-column (+ (current-column) (if (bolp) 0 1)))
2006 )))
2007
Barry Warsawf8ddb6a1999-01-18 21:49:39 +00002008(defun py-narrow-to-defun (&optional class)
2009 "Make text outside current defun invisible.
2010The defun visible is the one that contains point or follows point.
2011Optional CLASS is passed directly to `py-beginning-of-def-or-class'."
2012 (interactive "P")
2013 (save-excursion
2014 (widen)
2015 (py-end-of-def-or-class class)
2016 (let ((end (point)))
2017 (py-beginning-of-def-or-class class)
2018 (narrow-to-region (point) end))))
2019
Barry Warsaw003932a1998-07-07 15:11:24 +00002020
Barry Warsaw7ae77681994-12-12 20:38:05 +00002021(defun py-shift-region (start end count)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002022 "Indent lines from START to END by COUNT spaces."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002023 (save-excursion
Barry Warsaw41a05c71998-08-10 16:33:12 +00002024 (goto-char end)
2025 (beginning-of-line)
2026 (setq end (point))
2027 (goto-char start)
2028 (beginning-of-line)
2029 (setq start (point))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002030 (indent-rigidly start end count)))
2031
2032(defun py-shift-region-left (start end &optional count)
2033 "Shift region of Python code to the left.
2034The lines from the line containing the start of the current region up
2035to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002036shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002037
2038If a prefix argument is given, the region is instead shifted by that
Barry Warsaw41a05c71998-08-10 16:33:12 +00002039many columns. With no active region, dedent only the current line.
2040You cannot dedent the region if any line is already at column zero."
Barry Warsawdea4a291996-07-03 22:59:12 +00002041 (interactive
2042 (let ((p (point))
2043 (m (mark))
2044 (arg current-prefix-arg))
2045 (if m
2046 (list (min p m) (max p m) arg)
2047 (list p (save-excursion (forward-line 1) (point)) arg))))
2048 ;; if any line is at column zero, don't shift the region
2049 (save-excursion
2050 (goto-char start)
2051 (while (< (point) end)
2052 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00002053 (if (and (zerop (current-column))
2054 (not (looking-at "\\s *$")))
Barry Warsaw41a05c71998-08-10 16:33:12 +00002055 (error "Region is at left edge"))
Barry Warsawdea4a291996-07-03 22:59:12 +00002056 (forward-line 1)))
2057 (py-shift-region start end (- (prefix-numeric-value
2058 (or count py-indent-offset))))
2059 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002060
2061(defun py-shift-region-right (start end &optional count)
2062 "Shift region of Python code to the right.
2063The lines from the line containing the start of the current region up
2064to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002065shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002066
2067If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00002068many columns. With no active region, indent only the current line."
2069 (interactive
2070 (let ((p (point))
2071 (m (mark))
2072 (arg current-prefix-arg))
2073 (if m
2074 (list (min p m) (max p m) arg)
2075 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002076 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00002077 (or count py-indent-offset)))
2078 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002079
2080(defun py-indent-region (start end &optional indent-offset)
2081 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00002082
Barry Warsaw7ae77681994-12-12 20:38:05 +00002083The lines from the line containing the start of the current region up
2084to (but not including) the line containing the end of the region are
2085reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002086character in the first column, the first line is left alone and the
2087rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00002088region is reindented with respect to the (closest code or indenting
2089comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002090
2091This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002092control structures are introduced or removed, or to reformat code
2093using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002094
2095If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002096the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00002097used.
2098
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002099Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00002100is called! This function does not compute proper indentation from
2101scratch (that's impossible in Python), it merely adjusts the existing
2102indentation to be correct in context.
2103
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002104Warning: This function really has no idea what to do with
2105non-indenting comment lines, and shifts them as if they were indenting
2106comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002107
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002108Special cases: whitespace is deleted from blank lines; continuation
2109lines are shifted by the same amount their initial line was shifted,
2110in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00002111initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002112 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00002113 (save-excursion
2114 (goto-char end) (beginning-of-line) (setq end (point-marker))
2115 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002116 (let ((py-indent-offset (prefix-numeric-value
2117 (or indent-offset py-indent-offset)))
2118 (indents '(-1)) ; stack of active indent levels
2119 (target-column 0) ; column to which to indent
2120 (base-shifted-by 0) ; amount last base line was shifted
2121 (indent-base (if (looking-at "[ \t\n]")
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002122 (py-compute-indentation t)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002123 0))
2124 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002125 (while (< (point) end)
2126 (setq ci (current-indentation))
2127 ;; figure out appropriate target column
2128 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002129 ((or (eq (following-char) ?#) ; comment in column 1
2130 (looking-at "[ \t]*$")) ; entirely blank
2131 (setq target-column 0))
2132 ((py-continuation-line-p) ; shift relative to base line
2133 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002134 (t ; new base line
2135 (if (> ci (car indents)) ; going deeper; push it
2136 (setq indents (cons ci indents))
2137 ;; else we should have seen this indent before
2138 (setq indents (memq ci indents)) ; pop deeper indents
2139 (if (null indents)
2140 (error "Bad indentation in region, at line %d"
2141 (save-restriction
2142 (widen)
2143 (1+ (count-lines 1 (point)))))))
2144 (setq target-column (+ indent-base
2145 (* py-indent-offset
2146 (- (length indents) 2))))
2147 (setq base-shifted-by (- target-column ci))))
2148 ;; shift as needed
2149 (if (/= ci target-column)
2150 (progn
2151 (delete-horizontal-space)
2152 (indent-to target-column)))
2153 (forward-line 1))))
2154 (set-marker end nil))
2155
Barry Warsawa7891711996-08-01 15:53:09 +00002156(defun py-comment-region (beg end &optional arg)
2157 "Like `comment-region' but uses double hash (`#') comment starter."
2158 (interactive "r\nP")
Barry Warsaw3fcaf611996-08-01 20:11:51 +00002159 (let ((comment-start py-block-comment-prefix))
Barry Warsawa7891711996-08-01 15:53:09 +00002160 (comment-region beg end arg)))
2161
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002162
2163;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00002164(defun py-previous-statement (count)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002165 "Go to the start of the COUNTth preceding Python statement.
2166By default, goes to the previous statement. If there is no such
2167statement, goes to the first statement. Return count of statements
2168left to move. `Statements' do not include blank, comment, or
2169continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002170 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00002171 (if (< count 0) (py-next-statement (- count))
2172 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002173 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002174 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002175 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002176 (> count 0)
2177 (zerop (forward-line -1))
2178 (py-goto-statement-at-or-above))
2179 (setq count (1- count)))
2180 (if (> count 0) (goto-char start)))
2181 count))
2182
2183(defun py-next-statement (count)
2184 "Go to the start of next Python statement.
2185If the statement at point is the i'th Python statement, goes to the
2186start of statement i+COUNT. If there is no such statement, goes to the
2187last statement. Returns count of statements left to move. `Statements'
2188do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002189 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00002190 (if (< count 0) (py-previous-statement (- count))
2191 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002192 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002193 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002194 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002195 (> count 0)
2196 (py-goto-statement-below))
2197 (setq count (1- count)))
2198 (if (> count 0) (goto-char start)))
2199 count))
2200
2201(defun py-goto-block-up (&optional nomark)
2202 "Move up to start of current block.
2203Go to the statement that starts the smallest enclosing block; roughly
2204speaking, this will be the closest preceding statement that ends with a
2205colon and is indented less than the statement you started on. If
2206successful, also sets the mark to the starting point.
2207
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002208`\\[py-mark-block]' can be used afterward to mark the whole code
2209block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002210
2211If called from a program, the mark will not be set if optional argument
2212NOMARK is not nil."
2213 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002214 (let ((start (point))
2215 (found nil)
2216 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002217 (py-goto-initial-line)
2218 ;; if on blank or non-indenting comment line, use the preceding stmt
2219 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2220 (progn
2221 (py-goto-statement-at-or-above)
2222 (setq found (py-statement-opens-block-p))))
2223 ;; search back for colon line indented less
2224 (setq initial-indent (current-indentation))
2225 (if (zerop initial-indent)
2226 ;; force fast exit
2227 (goto-char (point-min)))
2228 (while (not (or found (bobp)))
2229 (setq found
2230 (and
2231 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2232 (or (py-goto-initial-line) t) ; always true -- side effect
2233 (< (current-indentation) initial-indent)
2234 (py-statement-opens-block-p))))
2235 (if found
2236 (progn
2237 (or nomark (push-mark start))
2238 (back-to-indentation))
2239 (goto-char start)
2240 (error "Enclosing block not found"))))
2241
Barry Warsawab0e86c1998-05-19 15:31:46 +00002242(defun py-beginning-of-def-or-class (&optional class count)
2243 "Move point to start of `def' or `class'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002244
2245Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsawab0e86c1998-05-19 15:31:46 +00002246arg, looks for a `class' instead. The docs below assume the `def'
2247case; just substitute `class' for `def' for the other case.
Barry Warsaw7c29b231998-07-07 17:45:38 +00002248Programmatically, if CLASS is `either', then moves to either `class'
2249or `def'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002250
Barry Warsawab0e86c1998-05-19 15:31:46 +00002251When second optional argument is given programmatically, move to the
2252COUNTth start of `def'.
2253
2254If point is in a `def' statement already, and after the `d', simply
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002255moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002256
Barry Warsawab0e86c1998-05-19 15:31:46 +00002257Otherwise (i.e. when point is not in a `def' statement, or at or
2258before the `d' of a `def' statement), searches for the closest
2259preceding `def' statement, and leaves point at its start. If no such
2260statement can be found, leaves point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002261
Barry Warsawab0e86c1998-05-19 15:31:46 +00002262Returns t iff a `def' statement is found by these rules.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002263
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002264Note that doing this command repeatedly will take you closer to the
2265start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002266
Barry Warsaw7c29b231998-07-07 17:45:38 +00002267To mark the current `def', see `\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002268 (interactive "P") ; raw prefix arg
Barry Warsaw6839d3a1998-10-28 00:10:45 +00002269 (setq count (or count 1))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002270 (let ((at-or-before-p (<= (current-column) (current-indentation)))
Barry Warsawab0e86c1998-05-19 15:31:46 +00002271 (start-of-line (goto-char (py-point 'bol)))
2272 (start-of-stmt (goto-char (py-point 'bos)))
Barry Warsaw7c29b231998-07-07 17:45:38 +00002273 (start-re (cond ((eq class 'either) "^[ \t]*\\(class\\|def\\)\\>")
2274 (class "^[ \t]*class\\>")
2275 (t "^[ \t]*def\\>")))
Barry Warsawab0e86c1998-05-19 15:31:46 +00002276 )
2277 ;; searching backward
2278 (if (and (< 0 count)
2279 (or (/= start-of-stmt start-of-line)
2280 (not at-or-before-p)))
2281 (end-of-line))
2282 ;; search forward
2283 (if (and (> 0 count)
2284 (zerop (current-column))
2285 (looking-at start-re))
2286 (end-of-line))
Barry Warsawddc46961999-07-27 21:40:02 +00002287 (if (re-search-backward start-re nil 'move count)
2288 (goto-char (match-beginning 0)))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002289
Barry Warsawab0e86c1998-05-19 15:31:46 +00002290;; Backwards compatibility
2291(defalias 'beginning-of-python-def-or-class 'py-beginning-of-def-or-class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002292
Barry Warsawab0e86c1998-05-19 15:31:46 +00002293(defun py-end-of-def-or-class (&optional class count)
2294 "Move point beyond end of `def' or `class' body.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002295
Barry Warsawab0e86c1998-05-19 15:31:46 +00002296By default, looks for an appropriate `def'. If you supply a prefix
2297arg, looks for a `class' instead. The docs below assume the `def'
2298case; just substitute `class' for `def' for the other case.
Barry Warsaw7c29b231998-07-07 17:45:38 +00002299Programmatically, if CLASS is `either', then moves to either `class'
2300or `def'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002301
Barry Warsawab0e86c1998-05-19 15:31:46 +00002302When second optional argument is given programmatically, move to the
2303COUNTth end of `def'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002304
Barry Warsawab0e86c1998-05-19 15:31:46 +00002305If point is in a `def' statement already, this is the `def' we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002306
Barry Warsawab0e86c1998-05-19 15:31:46 +00002307Else, if the `def' found by `\\[py-beginning-of-def-or-class]'
2308contains the statement you started on, that's the `def' we use.
2309
2310Otherwise, we search forward for the closest following `def', and use that.
2311
2312If a `def' can be found by these rules, point is moved to the start of
2313the line immediately following the `def' block, and the position of the
2314start of the `def' is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002315
2316Else point is moved to the end of the buffer, and nil is returned.
2317
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002318Note that doing this command repeatedly will take you closer to the
2319end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002320
Barry Warsaw7c29b231998-07-07 17:45:38 +00002321To mark the current `def', see `\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002322 (interactive "P") ; raw prefix arg
Barry Warsawab0e86c1998-05-19 15:31:46 +00002323 (if (and count (/= count 1))
2324 (py-beginning-of-def-or-class (- 1 count)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002325 (let ((start (progn (py-goto-initial-line) (point)))
Barry Warsaw7c29b231998-07-07 17:45:38 +00002326 (which (cond ((eq class 'either) "\\(class\\|def\\)")
2327 (class "class")
2328 (t "def")))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002329 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002330 ;; move point to start of appropriate def/class
2331 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
2332 (setq state 'at-beginning)
Barry Warsawab0e86c1998-05-19 15:31:46 +00002333 ;; else see if py-beginning-of-def-or-class hits container
2334 (if (and (py-beginning-of-def-or-class class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002335 (progn (py-goto-beyond-block)
2336 (> (point) start)))
2337 (setq state 'at-end)
2338 ;; else search forward
2339 (goto-char start)
2340 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
2341 (progn (setq state 'at-beginning)
2342 (beginning-of-line)))))
2343 (cond
2344 ((eq state 'at-beginning) (py-goto-beyond-block) t)
2345 ((eq state 'at-end) t)
2346 ((eq state 'not-found) nil)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002347 (t (error "Internal error in `py-end-of-def-or-class'")))))
Barry Warsawab0e86c1998-05-19 15:31:46 +00002348
2349;; Backwards compabitility
Barry Warsaw820273d1998-05-19 15:54:45 +00002350(defalias 'end-of-python-def-or-class 'py-end-of-def-or-class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002351
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002352
2353;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002354(defun py-mark-block (&optional extend just-move)
2355 "Mark following block of lines. With prefix arg, mark structure.
2356Easier to use than explain. It sets the region to an `interesting'
2357block of succeeding lines. If point is on a blank line, it goes down to
2358the next non-blank line. That will be the start of the region. The end
2359of the region depends on the kind of line at the start:
2360
2361 - If a comment, the region will include all succeeding comment lines up
2362 to (but not including) the next non-comment line (if any).
2363
2364 - Else if a prefix arg is given, and the line begins one of these
2365 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002366
2367 if elif else try except finally for while def class
2368
Barry Warsaw7ae77681994-12-12 20:38:05 +00002369 the region will be set to the body of the structure, including
2370 following blocks that `belong' to it, but excluding trailing blank
2371 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002372 and all (if any) of the following `except' and `finally' blocks
2373 that belong to the `try' structure will be in the region. Ditto
2374 for if/elif/else, for/else and while/else structures, and (a bit
2375 degenerate, since they're always one-block structures) def and
2376 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002377
2378 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002379 block (see list above), and the block is not a `one-liner' (i.e.,
2380 the statement ends with a colon, not with code), the region will
2381 include all succeeding lines up to (but not including) the next
2382 code statement (if any) that's indented no more than the starting
2383 line, except that trailing blank and comment lines are excluded.
2384 E.g., if the starting line begins a multi-statement `def'
2385 structure, the region will be set to the full function definition,
2386 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002387
2388 - Else the region will include all succeeding lines up to (but not
2389 including) the next blank line, or code or indenting-comment line
2390 indented strictly less than the starting line. Trailing indenting
2391 comment lines are included in this case, but not trailing blank
2392 lines.
2393
2394A msg identifying the location of the mark is displayed in the echo
2395area; or do `\\[exchange-point-and-mark]' to flip down to the end.
2396
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002397If called from a program, optional argument EXTEND plays the role of
2398the prefix arg, and if optional argument JUST-MOVE is not nil, just
2399moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002400 (interactive "P") ; raw prefix arg
2401 (py-goto-initial-line)
2402 ;; skip over blank lines
2403 (while (and
2404 (looking-at "[ \t]*$") ; while blank line
2405 (not (eobp))) ; & somewhere to go
2406 (forward-line 1))
2407 (if (eobp)
2408 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002409 (let ((initial-pos (point))
2410 (initial-indent (current-indentation))
2411 last-pos ; position of last stmt in region
2412 (followers
2413 '((if elif else) (elif elif else) (else)
2414 (try except finally) (except except) (finally)
2415 (for else) (while else)
2416 (def) (class) ) )
2417 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002418
2419 (cond
2420 ;; if comment line, suck up the following comment lines
2421 ((looking-at "[ \t]*#")
2422 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
2423 (re-search-backward "^[ \t]*#") ; and back to last comment in block
2424 (setq last-pos (point)))
2425
2426 ;; else if line is a block line and EXTEND given, suck up
2427 ;; the whole structure
2428 ((and extend
2429 (setq first-symbol (py-suck-up-first-keyword) )
2430 (assq first-symbol followers))
2431 (while (and
2432 (or (py-goto-beyond-block) t) ; side effect
2433 (forward-line -1) ; side effect
2434 (setq last-pos (point)) ; side effect
2435 (py-goto-statement-below)
2436 (= (current-indentation) initial-indent)
2437 (setq next-symbol (py-suck-up-first-keyword))
2438 (memq next-symbol (cdr (assq first-symbol followers))))
2439 (setq first-symbol next-symbol)))
2440
2441 ;; else if line *opens* a block, search for next stmt indented <=
2442 ((py-statement-opens-block-p)
2443 (while (and
2444 (setq last-pos (point)) ; always true -- side effect
2445 (py-goto-statement-below)
2446 (> (current-indentation) initial-indent))
2447 nil))
2448
2449 ;; else plain code line; stop at next blank line, or stmt or
2450 ;; indenting comment line indented <
2451 (t
2452 (while (and
2453 (setq last-pos (point)) ; always true -- side effect
2454 (or (py-goto-beyond-final-line) t)
2455 (not (looking-at "[ \t]*$")) ; stop at blank line
2456 (or
2457 (>= (current-indentation) initial-indent)
2458 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
2459 nil)))
2460
2461 ;; skip to end of last stmt
2462 (goto-char last-pos)
2463 (py-goto-beyond-final-line)
2464
2465 ;; set mark & display
2466 (if just-move
2467 () ; just return
2468 (push-mark (point) 'no-msg)
2469 (forward-line -1)
2470 (message "Mark set after: %s" (py-suck-up-leading-text))
2471 (goto-char initial-pos))))
2472
Barry Warsaw2518c671997-11-05 00:51:08 +00002473(defun py-mark-def-or-class (&optional class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002474 "Set region to body of def (or class, with prefix arg) enclosing point.
2475Pushes the current mark, then point, on the mark ring (all language
2476modes do this, but although it's handy it's never documented ...).
2477
2478In most Emacs language modes, this function bears at least a
Barry Warsawab0e86c1998-05-19 15:31:46 +00002479hallucinogenic resemblance to `\\[py-end-of-def-or-class]' and
2480`\\[py-beginning-of-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002481
2482And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002483Turned out that was more confusing than useful: the `goto start' and
2484`goto end' commands are usually used to search through a file, and
2485people expect them to act a lot like `search backward' and `search
2486forward' string-search commands. But because Python `def' and `class'
2487can nest to arbitrary levels, finding the smallest def containing
2488point cannot be done via a simple backward search: the def containing
2489point may not be the closest preceding def, or even the closest
2490preceding def that's indented less. The fancy algorithm required is
2491appropriate for the usual uses of this `mark' command, but not for the
2492`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002493
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002494So the def marked by this command may not be the one either of the
2495`goto' commands find: If point is on a blank or non-indenting comment
2496line, moves back to start of the closest preceding code statement or
2497indenting comment line. If this is a `def' statement, that's the def
2498we use. Else searches for the smallest enclosing `def' block and uses
2499that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002500
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002501When an enclosing def is found: The mark is left immediately beyond
2502the last line of the def block. Point is left at the start of the
2503def, except that: if the def is preceded by a number of comment lines
2504followed by (at most) one optional blank line, point is left at the
2505start of the comments; else if the def is preceded by a blank line,
2506point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002507
2508The intent is to mark the containing def/class and its associated
2509documentation, to make moving and duplicating functions and classes
2510pleasant."
2511 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002512 (let ((start (point))
Barry Warsaw7c29b231998-07-07 17:45:38 +00002513 (which (cond ((eq class 'either) "\\(class\\|def\\)")
2514 (class "class")
2515 (t "def"))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002516 (push-mark start)
2517 (if (not (py-go-up-tree-to-keyword which))
2518 (progn (goto-char start)
Barry Warsaw7c29b231998-07-07 17:45:38 +00002519 (error "Enclosing %s not found"
2520 (if (eq class 'either)
2521 "def or class"
2522 which)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002523 ;; else enclosing def/class found
2524 (setq start (point))
2525 (py-goto-beyond-block)
2526 (push-mark (point))
2527 (goto-char start)
2528 (if (zerop (forward-line -1)) ; if there is a preceding line
2529 (progn
2530 (if (looking-at "[ \t]*$") ; it's blank
2531 (setq start (point)) ; so reset start point
2532 (goto-char start)) ; else try again
2533 (if (zerop (forward-line -1))
2534 (if (looking-at "[ \t]*#") ; a comment
2535 ;; look back for non-comment line
2536 ;; tricky: note that the regexp matches a blank
2537 ;; line, cuz \n is in the 2nd character class
2538 (and
2539 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
2540 (forward-line 1))
2541 ;; no comment, so go back
Barry Warsaw4da6bd51997-11-26 06:00:26 +00002542 (goto-char start)))))))
2543 (exchange-point-and-mark)
2544 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002545
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002546;; ripped from cc-mode
2547(defun py-forward-into-nomenclature (&optional arg)
2548 "Move forward to end of a nomenclature section or word.
Barry Warsaw41a05c71998-08-10 16:33:12 +00002549With \\[universal-argument] (programmatically, optional argument ARG),
2550do it that many times.
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002551
2552A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2553 (interactive "p")
2554 (let ((case-fold-search nil))
2555 (if (> arg 0)
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002556 (re-search-forward
2557 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
2558 (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002559 (while (and (< arg 0)
2560 (re-search-backward
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002561 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002562 (point-min) 0))
2563 (forward-char 1)
2564 (setq arg (1+ arg)))))
2565 (py-keep-region-active))
2566
2567(defun py-backward-into-nomenclature (&optional arg)
2568 "Move backward to beginning of a nomenclature section or word.
2569With optional ARG, move that many times. If ARG is negative, move
2570forward.
2571
2572A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2573 (interactive "p")
2574 (py-forward-into-nomenclature (- arg))
2575 (py-keep-region-active))
2576
2577
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002578
Barry Warsaw4f577d22001-04-11 20:23:17 +00002579;; pdbtrack functions
2580(defun py-pdbtrack-toggle-stack-tracking (arg)
2581 (interactive "P")
2582 (if (not (get-buffer-process (current-buffer)))
2583 (error "No process associated with buffer '%s'" (current-buffer)))
2584 ;; missing or 0 is toggle, >0 turn on, <0 turn off
2585 (if (or (not arg)
2586 (zerop (setq arg (prefix-numeric-value arg))))
2587 (setq py-pdbtrack-do-tracking-p (not py-pdbtrack-do-tracking-p))
2588 (setq py-pdbtrack-do-tracking-p (> arg 0)))
2589 (message "%sabled Python's pdbtrack"
2590 (if py-pdbtrack-do-tracking-p "En" "Dis")))
2591
2592(defun turn-on-pdbtrack ()
2593 (interactive)
2594 (py-pdbtrack-toggle-stack-tracking 1))
2595
2596(defun turn-off-pdbtrack ()
2597 (interactive)
2598 (py-pdbtrack-toggle-stack-tracking 0))
2599
2600
2601
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002602;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002603
2604;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002605;; plus lines of the form ^[vc]:name$ to suck variable & command docs
2606;; out of the right places, along with the keys they're on & current
2607;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00002608(defun py-dump-help-string (str)
2609 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002610 (let ((locals (buffer-local-variables))
2611 funckind funcname func funcdoc
2612 (start 0) mstart end
2613 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002614 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
2615 (setq mstart (match-beginning 0) end (match-end 0)
2616 funckind (substring str (match-beginning 1) (match-end 1))
2617 funcname (substring str (match-beginning 2) (match-end 2))
2618 func (intern funcname))
2619 (princ (substitute-command-keys (substring str start mstart)))
2620 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002621 ((equal funckind "c") ; command
2622 (setq funcdoc (documentation func)
2623 keys (concat
2624 "Key(s): "
2625 (mapconcat 'key-description
2626 (where-is-internal func py-mode-map)
2627 ", "))))
2628 ((equal funckind "v") ; variable
Barry Warsaw604cefa1996-09-03 18:17:04 +00002629 (setq funcdoc (documentation-property func 'variable-documentation)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002630 keys (if (assq func locals)
2631 (concat
2632 "Local/Global values: "
2633 (prin1-to-string (symbol-value func))
2634 " / "
2635 (prin1-to-string (default-value func)))
2636 (concat
2637 "Value: "
2638 (prin1-to-string (symbol-value func))))))
2639 (t ; unexpected
2640 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002641 (princ (format "\n-> %s:\t%s\t%s\n\n"
2642 (if (equal funckind "c") "Command" "Variable")
2643 funcname keys))
2644 (princ funcdoc)
2645 (terpri)
2646 (setq start end))
2647 (princ (substitute-command-keys (substring str start))))
2648 (print-help-return-message)))
2649
2650(defun py-describe-mode ()
2651 "Dump long form of Python-mode docs."
2652 (interactive)
2653 (py-dump-help-string "Major mode for editing Python files.
2654Knows about Python indentation, tokens, comments and continuation lines.
2655Paragraphs are separated by blank lines only.
2656
2657Major sections below begin with the string `@'; specific function and
2658variable docs begin with `->'.
2659
2660@EXECUTING PYTHON CODE
2661
Barry Warsaw820273d1998-05-19 15:54:45 +00002662\\[py-execute-import-or-reload]\timports or reloads the file in the Python interpreter
Barry Warsaw7ae77681994-12-12 20:38:05 +00002663\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
2664\\[py-execute-region]\tsends the current region
Barry Warsaw820273d1998-05-19 15:54:45 +00002665\\[py-execute-def-or-class]\tsends the current function or class definition
2666\\[py-execute-string]\tsends an arbitrary string
Barry Warsaw7ae77681994-12-12 20:38:05 +00002667\\[py-shell]\tstarts a Python interpreter window; this will be used by
Barry Warsaw820273d1998-05-19 15:54:45 +00002668\tsubsequent Python execution commands
2669%c:py-execute-import-or-reload
Barry Warsaw7ae77681994-12-12 20:38:05 +00002670%c:py-execute-buffer
2671%c:py-execute-region
Barry Warsaw820273d1998-05-19 15:54:45 +00002672%c:py-execute-def-or-class
2673%c:py-execute-string
Barry Warsaw7ae77681994-12-12 20:38:05 +00002674%c:py-shell
2675
2676@VARIABLES
2677
2678py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00002679py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002680
2681py-python-command\tshell command to invoke Python interpreter
Barry Warsaw7ae77681994-12-12 20:38:05 +00002682py-temp-directory\tdirectory used for temp files (if needed)
2683
2684py-beep-if-tab-change\tring the bell if tab-width is changed
2685%v:py-indent-offset
2686%v:py-block-comment-prefix
2687%v:py-python-command
Barry Warsaw7ae77681994-12-12 20:38:05 +00002688%v:py-temp-directory
2689%v:py-beep-if-tab-change
2690
2691@KINDS OF LINES
2692
2693Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002694preceding line ends with a backslash that's not part of a comment, or
2695the paren/bracket/brace nesting level at the start of the line is
2696non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002697
2698An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002699possibly blanks or tabs), a `comment line' (leftmost non-blank
2700character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002701
2702Comment Lines
2703
2704Although all comment lines are treated alike by Python, Python mode
2705recognizes two kinds that act differently with respect to indentation.
2706
2707An `indenting comment line' is a comment line with a blank, tab or
2708nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002709treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00002710indenting comment line will be indented like the comment line. All
2711other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002712following the initial `#') are `non-indenting comment lines', and
2713their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002714
2715Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002716whenever possible. Non-indenting comment lines are useful in cases
2717like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00002718
2719\ta = b # a very wordy single-line comment that ends up being
2720\t #... continued onto another line
2721
2722\tif a == b:
2723##\t\tprint 'panic!' # old code we've `commented out'
2724\t\treturn a
2725
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002726Since the `#...' and `##' comment lines have a non-whitespace
2727character following the initial `#', Python mode ignores them when
2728computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002729
2730Continuation Lines and Statements
2731
2732The Python-mode commands generally work on statements instead of on
2733individual lines, where a `statement' is a comment or blank line, or a
2734code line and all of its following continuation lines (if any)
2735considered as a single logical unit. The commands in this mode
2736generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002737statement containing point, even if point happens to be in the middle
2738of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002739
2740
2741@INDENTATION
2742
2743Primarily for entering new code:
2744\t\\[indent-for-tab-command]\t indent line appropriately
2745\t\\[py-newline-and-indent]\t insert newline, then indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002746\t\\[py-electric-backspace]\t reduce indentation, or delete single character
Barry Warsaw7ae77681994-12-12 20:38:05 +00002747
2748Primarily for reindenting existing code:
2749\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
2750\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
2751
2752\t\\[py-indent-region]\t reindent region to match its context
2753\t\\[py-shift-region-left]\t shift region left by py-indent-offset
2754\t\\[py-shift-region-right]\t shift region right by py-indent-offset
2755
2756Unlike most programming languages, Python uses indentation, and only
2757indentation, to specify block structure. Hence the indentation supplied
2758automatically by Python-mode is just an educated guess: only you know
2759the block structure you intend, so only you can supply correct
2760indentation.
2761
2762The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
2763the indentation of preceding statements. E.g., assuming
2764py-indent-offset is 4, after you enter
2765\tif a > 0: \\[py-newline-and-indent]
2766the cursor will be moved to the position of the `_' (_ is not a
2767character in the file, it's just used here to indicate the location of
2768the cursor):
2769\tif a > 0:
2770\t _
2771If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
2772to
2773\tif a > 0:
2774\t c = d
2775\t _
2776Python-mode cannot know whether that's what you intended, or whether
2777\tif a > 0:
2778\t c = d
2779\t_
2780was your intent. In general, Python-mode either reproduces the
2781indentation of the (closest code or indenting-comment) preceding
2782statement, or adds an extra py-indent-offset blanks if the preceding
2783statement has `:' as its last significant (non-whitespace and non-
2784comment) character. If the suggested indentation is too much, use
Barry Warsaw27ee1151997-12-03 05:03:44 +00002785\\[py-electric-backspace] to reduce it.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002786
2787Continuation lines are given extra indentation. If you don't like the
2788suggested indentation, change it to something you do like, and Python-
2789mode will strive to indent later lines of the statement in the same way.
2790
2791If a line is a continuation line by virtue of being in an unclosed
2792paren/bracket/brace structure (`list', for short), the suggested
2793indentation depends on whether the current line contains the first item
2794in the list. If it does, it's indented py-indent-offset columns beyond
2795the indentation of the line containing the open bracket. If you don't
2796like that, change it by hand. The remaining items in the list will mimic
2797whatever indentation you give to the first item.
2798
2799If a line is a continuation line because the line preceding it ends with
2800a backslash, the third and following lines of the statement inherit their
2801indentation from the line preceding them. The indentation of the second
2802line in the statement depends on the form of the first (base) line: if
2803the base line is an assignment statement with anything more interesting
2804than the backslash following the leftmost assigning `=', the second line
2805is indented two columns beyond that `='. Else it's indented to two
2806columns beyond the leftmost solid chunk of non-whitespace characters on
2807the base line.
2808
2809Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
2810repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
2811structure you intend.
2812%c:indent-for-tab-command
2813%c:py-newline-and-indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002814%c:py-electric-backspace
Barry Warsaw7ae77681994-12-12 20:38:05 +00002815
2816
2817The next function may be handy when editing code you didn't write:
2818%c:py-guess-indent-offset
2819
2820
2821The remaining `indent' functions apply to a region of Python code. They
2822assume the block structure (equals indentation, in Python) of the region
2823is correct, and alter the indentation in various ways while preserving
2824the block structure:
2825%c:py-indent-region
2826%c:py-shift-region-left
2827%c:py-shift-region-right
2828
2829@MARKING & MANIPULATING REGIONS OF CODE
2830
2831\\[py-mark-block]\t mark block of lines
Barry Warsaw2518c671997-11-05 00:51:08 +00002832\\[py-mark-def-or-class]\t mark smallest enclosing def
2833\\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002834\\[comment-region]\t comment out region of code
2835\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00002836%c:py-mark-block
Barry Warsaw2518c671997-11-05 00:51:08 +00002837%c:py-mark-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002838%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002839
2840@MOVING POINT
2841
2842\\[py-previous-statement]\t move to statement preceding point
2843\\[py-next-statement]\t move to statement following point
2844\\[py-goto-block-up]\t move up to start of current block
Barry Warsawab0e86c1998-05-19 15:31:46 +00002845\\[py-beginning-of-def-or-class]\t move to start of def
2846\\[universal-argument] \\[py-beginning-of-def-or-class]\t move to start of class
2847\\[py-end-of-def-or-class]\t move to end of def
2848\\[universal-argument] \\[py-end-of-def-or-class]\t move to end of class
Barry Warsaw7ae77681994-12-12 20:38:05 +00002849
2850The first two move to one statement beyond the statement that contains
2851point. A numeric prefix argument tells them to move that many
2852statements instead. Blank lines, comment lines, and continuation lines
2853do not count as `statements' for these commands. So, e.g., you can go
2854to the first code statement in a file by entering
2855\t\\[beginning-of-buffer]\t to move to the top of the file
2856\t\\[py-next-statement]\t to skip over initial comments and blank lines
2857Or do `\\[py-previous-statement]' with a huge prefix argument.
2858%c:py-previous-statement
2859%c:py-next-statement
2860%c:py-goto-block-up
Barry Warsawab0e86c1998-05-19 15:31:46 +00002861%c:py-beginning-of-def-or-class
2862%c:py-end-of-def-or-class
Barry Warsaw7ae77681994-12-12 20:38:05 +00002863
2864@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2865
2866`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2867
2868`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2869overall class and def structure of a module.
2870
2871`\\[back-to-indentation]' moves point to a line's first non-blank character.
2872
2873`\\[indent-relative]' is handy for creating odd indentation.
2874
2875@OTHER EMACS HINTS
2876
2877If you don't like the default value of a variable, change its value to
2878whatever you do like by putting a `setq' line in your .emacs file.
2879E.g., to set the indentation increment to 4, put this line in your
2880.emacs:
2881\t(setq py-indent-offset 4)
2882To see the value of a variable, do `\\[describe-variable]' and enter the variable
2883name at the prompt.
2884
2885When entering a key sequence like `C-c C-n', it is not necessary to
2886release the CONTROL key after doing the `C-c' part -- it suffices to
2887press the CONTROL key, press and release `c' (while still holding down
2888CONTROL), press and release `n' (while still holding down CONTROL), &
2889then release CONTROL.
2890
2891Entering Python mode calls with no arguments the value of the variable
2892`python-mode-hook', if that value exists and is not nil; for backward
2893compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2894the Elisp manual for details.
2895
2896Obscure: When python-mode is first loaded, it looks for all bindings
2897to newline-and-indent in the global keymap, and shadows them with
2898local bindings to py-newline-and-indent."))
2899
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002900
2901;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002902(defvar py-parse-state-re
2903 (concat
2904 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2905 "\\|"
2906 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002907
Barry Warsaw7ae77681994-12-12 20:38:05 +00002908(defun py-parse-state ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00002909 "Return the parse state at point (see `parse-partial-sexp' docs)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002910 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002911 (let ((here (point))
Barry Warsaw68d486c2001-02-24 00:09:17 +00002912 in-listcomp pps done)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002913 (while (not done)
2914 ;; back up to the first preceding line (if any; else start of
2915 ;; buffer) that begins with a popular Python keyword, or a
2916 ;; non- whitespace and non-comment character. These are good
2917 ;; places to start parsing to see whether where we started is
2918 ;; at a non-zero nesting level. It may be slow for people who
2919 ;; write huge code blocks or huge lists ... tough beans.
2920 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw68d486c2001-02-24 00:09:17 +00002921 ;; Watch out for landing inside a list comprehension
2922 (save-excursion
2923 (if (and (looking-at "[ \t]*\\<\\(if\\|for\\)\\>")
2924 (py-safe (progn (up-list -1) t))
2925 (eq (char-after) ?\[))
2926 (setq in-listcomp (point))
2927 (setq in-listcomp nil)))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002928 (beginning-of-line)
Barry Warsawf64b4051998-02-12 16:52:14 +00002929 ;; In XEmacs, we have a much better way to test for whether
2930 ;; we're in a triple-quoted string or not. Emacs does not
Barry Warsaw145ab1c1998-05-19 14:49:49 +00002931 ;; have this built-in function, which is its loss because
Barry Warsawf64b4051998-02-12 16:52:14 +00002932 ;; without scanning from the beginning of the buffer, there's
2933 ;; no accurate way to determine this otherwise.
2934 (if (not (fboundp 'buffer-syntactic-context))
2935 ;; Emacs
Barry Warsaw585f7331998-04-01 21:13:51 +00002936 (progn
2937 (save-excursion (setq pps (parse-partial-sexp (point) here)))
Barry Warsawf64b4051998-02-12 16:52:14 +00002938 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw742a5111998-03-13 17:29:15 +00002939 (setq done (or (not (nth 3 pps))
Barry Warsaw53db8591999-05-24 19:57:32 +00002940 (bobp)))
2941 ;; Just go ahead and short circuit the test back to the
2942 ;; beginning of the buffer. This will be slow, but not
2943 ;; nearly as slow as looping through many
2944 ;; re-search-backwards.
2945 (if (not done)
2946 (goto-char (point-min))))
Barry Warsawf64b4051998-02-12 16:52:14 +00002947 ;; XEmacs
2948 (setq done (or (not (buffer-syntactic-context))
2949 (bobp)))
Barry Warsaw68d486c2001-02-24 00:09:17 +00002950 (when in-listcomp
2951 (goto-char in-listcomp)
2952 (setq done nil))
Barry Warsawf64b4051998-02-12 16:52:14 +00002953 (when done
2954 (setq pps (parse-partial-sexp (point) here)))
2955 ))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002956 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002957
Barry Warsaw7ae77681994-12-12 20:38:05 +00002958(defun py-nesting-level ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00002959 "Return the buffer position of the last unclosed enclosing list.
2960If nesting level is zero, return nil."
Barry Warsawf64b4051998-02-12 16:52:14 +00002961 (let ((status (py-parse-state)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002962 (if (zerop (car status))
2963 nil ; not in a nest
2964 (car (cdr status))))) ; char# of open bracket
2965
Barry Warsaw7ae77681994-12-12 20:38:05 +00002966(defun py-backslash-continuation-line-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00002967 "Return t iff preceding line ends with backslash that is not in a comment."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002968 (save-excursion
2969 (beginning-of-line)
2970 (and
2971 ;; use a cheap test first to avoid the regexp if possible
2972 ;; use 'eq' because char-after may return nil
2973 (eq (char-after (- (point) 2)) ?\\ )
2974 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002975 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002976 (looking-at py-continued-re))))
2977
Barry Warsaw7ae77681994-12-12 20:38:05 +00002978(defun py-continuation-line-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00002979 "Return t iff current line is a continuation line."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002980 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002981 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002982 (or (py-backslash-continuation-line-p)
2983 (py-nesting-level))))
2984
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002985(defun py-goto-beginning-of-tqs (delim)
2986 "Go to the beginning of the triple quoted string we find ourselves in.
2987DELIM is the TQS string delimiter character we're searching backwards
2988for."
Barry Warsaw3c34bb32000-10-27 05:00:25 +00002989 (let ((skip (and delim (make-string 1 delim)))
2990 (continue t))
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002991 (when skip
2992 (save-excursion
Barry Warsaw3c34bb32000-10-27 05:00:25 +00002993 (while continue
2994 (py-safe (search-backward skip))
2995 (setq continue (and (not (bobp))
2996 (= (char-before) ?\\))))
2997 (if (and (= (char-before) delim)
2998 (= (char-before (1- (point))) delim))
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002999 (setq skip (make-string 3 delim))))
3000 ;; we're looking at a triple-quoted string
3001 (py-safe (search-backward skip)))))
3002
Barry Warsaw7ae77681994-12-12 20:38:05 +00003003(defun py-goto-initial-line ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003004 "Go to the initial line of the current statement.
3005Usually this is the line we're on, but if we're on the 2nd or
3006following lines of a continuation block, we need to go up to the first
3007line of the block."
3008 ;; Tricky: We want to avoid quadratic-time behavior for long
3009 ;; continued blocks, whether of the backslash or open-bracket
3010 ;; varieties, or a mix of the two. The following manages to do that
3011 ;; in the usual cases.
3012 ;;
3013 ;; Also, if we're sitting inside a triple quoted string, this will
3014 ;; drop us at the line that begins the string.
Barry Warsaw9ec9fbc1998-01-21 05:15:57 +00003015 (let (open-bracket-pos)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003016 (while (py-continuation-line-p)
3017 (beginning-of-line)
3018 (if (py-backslash-continuation-line-p)
3019 (while (py-backslash-continuation-line-p)
3020 (forward-line -1))
3021 ;; else zip out of nested brackets/braces/parens
3022 (while (setq open-bracket-pos (py-nesting-level))
3023 (goto-char open-bracket-pos)))))
3024 (beginning-of-line))
3025
Barry Warsaw7ae77681994-12-12 20:38:05 +00003026(defun py-goto-beyond-final-line ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003027 "Go to the point just beyond the fine line of the current statement.
3028Usually this is the start of the next line, but if this is a
3029multi-line statement we need to skip over the continuation lines."
3030 ;; Tricky: Again we need to be clever to avoid quadratic time
3031 ;; behavior.
3032 ;;
3033 ;; XXX: Not quite the right solution, but deals with multi-line doc
3034 ;; strings
Barry Warsaw751f4931998-05-19 16:06:21 +00003035 (if (looking-at (concat "[ \t]*\\(" py-stringlit-re "\\)"))
3036 (goto-char (match-end 0)))
3037 ;;
Barry Warsaw7ae77681994-12-12 20:38:05 +00003038 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003039 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003040 (while (and (py-continuation-line-p)
3041 (not (eobp)))
3042 ;; skip over the backslash flavor
3043 (while (and (py-backslash-continuation-line-p)
3044 (not (eobp)))
3045 (forward-line 1))
3046 ;; if in nest, zip to the end of the nest
3047 (setq state (py-parse-state))
3048 (if (and (not (zerop (car state)))
3049 (not (eobp)))
3050 (progn
Barry Warsawaffc0ca1997-11-03 16:59:38 +00003051 (parse-partial-sexp (point) (point-max) 0 nil state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003052 (forward-line 1))))))
3053
Barry Warsaw7ae77681994-12-12 20:38:05 +00003054(defun py-statement-opens-block-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003055 "Return t iff the current statement opens a block.
3056I.e., iff it ends with a colon that is not in a comment. Point should
3057be at the start of a statement."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003058 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003059 (let ((start (point))
3060 (finish (progn (py-goto-beyond-final-line) (1- (point))))
3061 (searching t)
3062 (answer nil)
3063 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003064 (goto-char start)
3065 (while searching
3066 ;; look for a colon with nothing after it except whitespace, and
3067 ;; maybe a comment
3068 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
3069 finish t)
3070 (if (eq (point) finish) ; note: no `else' clause; just
3071 ; keep searching if we're not at
3072 ; the end yet
3073 ;; sure looks like it opens a block -- but it might
3074 ;; be in a comment
3075 (progn
3076 (setq searching nil) ; search is done either way
3077 (setq state (parse-partial-sexp start
3078 (match-beginning 0)))
3079 (setq answer (not (nth 4 state)))))
3080 ;; search failed: couldn't find another interesting colon
3081 (setq searching nil)))
3082 answer)))
3083
Barry Warsawf831d811996-07-31 20:42:59 +00003084(defun py-statement-closes-block-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003085 "Return t iff the current statement closes a block.
3086I.e., if the line starts with `return', `raise', `break', `continue',
3087and `pass'. This doesn't catch embedded statements."
Barry Warsawf831d811996-07-31 20:42:59 +00003088 (let ((here (point)))
Barry Warsawa8f99ba1999-05-24 18:37:57 +00003089 (py-goto-initial-line)
Barry Warsawc0d2d511999-06-03 22:18:59 +00003090 (back-to-indentation)
Barry Warsawf831d811996-07-31 20:42:59 +00003091 (prog1
Barry Warsawaffc0ca1997-11-03 16:59:38 +00003092 (looking-at (concat py-block-closing-keywords-re "\\>"))
Barry Warsawf831d811996-07-31 20:42:59 +00003093 (goto-char here))))
3094
Barry Warsaw7ae77681994-12-12 20:38:05 +00003095(defun py-goto-beyond-block ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003096 "Go to point just beyond the final line of block begun by the current line.
3097This is the same as where `py-goto-beyond-final-line' goes unless
3098we're on colon line, in which case we go to the end of the block.
3099Assumes point is at the beginning of the line."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003100 (if (py-statement-opens-block-p)
3101 (py-mark-block nil 'just-move)
3102 (py-goto-beyond-final-line)))
3103
Barry Warsaw7ae77681994-12-12 20:38:05 +00003104(defun py-goto-statement-at-or-above ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003105 "Go to the start of the first statement at or preceding point.
3106Return t if there is such a statement, otherwise nil. `Statement'
3107does not include blank lines, comments, or continuation lines."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003108 (py-goto-initial-line)
3109 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003110 ;; skip back over blank & comment lines
3111 ;; note: will skip a blank or comment line that happens to be
3112 ;; a continuation line too
3113 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
3114 (progn (py-goto-initial-line) t)
3115 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003116 t))
3117
Barry Warsaw7ae77681994-12-12 20:38:05 +00003118(defun py-goto-statement-below ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003119 "Go to start of the first statement following the statement containing point.
3120Return t if there is such a statement, otherwise nil. `Statement'
3121does not include blank lines, comments, or continuation lines."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003122 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003123 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003124 (py-goto-beyond-final-line)
3125 (while (and
3126 (looking-at py-blank-or-comment-re)
3127 (not (eobp)))
3128 (forward-line 1))
3129 (if (eobp)
3130 (progn (goto-char start) nil)
3131 t)))
3132
Barry Warsaw7ae77681994-12-12 20:38:05 +00003133(defun py-go-up-tree-to-keyword (key)
Barry Warsaw41a05c71998-08-10 16:33:12 +00003134 "Go to begining of statement starting with KEY, at or preceding point.
3135
3136KEY is a regular expression describing a Python keyword. Skip blank
3137lines and non-indenting comments. If the statement found starts with
3138KEY, then stop, otherwise go back to first enclosing block starting
3139with KEY. If successful, leave point at the start of the KEY line and
3140return t. Otherwise, leav point at an undefined place and return nil."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003141 ;; skip blanks and non-indenting #
3142 (py-goto-initial-line)
3143 (while (and
3144 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
3145 (zerop (forward-line -1))) ; go back
3146 nil)
3147 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003148 (let* ((re (concat "[ \t]*" key "\\b"))
3149 (case-fold-search nil) ; let* so looking-at sees this
3150 (found (looking-at re))
3151 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003152 (while (not (or found dead))
3153 (condition-case nil ; in case no enclosing block
3154 (py-goto-block-up 'no-mark)
3155 (error (setq dead t)))
3156 (or dead (setq found (looking-at re))))
3157 (beginning-of-line)
3158 found))
3159
Barry Warsaw7ae77681994-12-12 20:38:05 +00003160(defun py-suck-up-leading-text ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003161 "Return string in buffer from start of indentation to end of line.
3162Prefix with \"...\" if leading whitespace was skipped."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003163 (save-excursion
3164 (back-to-indentation)
3165 (concat
3166 (if (bolp) "" "...")
3167 (buffer-substring (point) (progn (end-of-line) (point))))))
3168
Barry Warsaw7ae77681994-12-12 20:38:05 +00003169(defun py-suck-up-first-keyword ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003170 "Return first keyword on the line as a Lisp symbol.
3171`Keyword' is defined (essentially) as the regular expression
3172([a-z]+). Returns nil if none was found."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003173 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003174 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
3175 (intern (buffer-substring (match-beginning 1) (match-end 1)))
3176 nil)))
3177
Barry Warsawb3e81d51996-09-04 15:12:42 +00003178(defun py-current-defun ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003179 "Python value for `add-log-current-defun-function'.
3180This tells add-log.el how to find the current function/method/variable."
Barry Warsawb3e81d51996-09-04 15:12:42 +00003181 (save-excursion
3182 (if (re-search-backward py-defun-start-re nil t)
3183 (or (match-string 3)
3184 (let ((method (match-string 2)))
3185 (if (and (not (zerop (length (match-string 1))))
3186 (re-search-backward py-class-start-re nil t))
3187 (concat (match-string 1) "." method)
3188 method)))
3189 nil)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003190
3191
Barry Warsawfec75d61995-07-05 23:26:15 +00003192(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00003193 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003194
Barry Warsaw850437a1995-03-08 21:50:28 +00003195(defun py-version ()
3196 "Echo the current version of `python-mode' in the minibuffer."
3197 (interactive)
3198 (message "Using `python-mode' version %s" py-version)
3199 (py-keep-region-active))
3200
3201;; only works under Emacs 19
3202;(eval-when-compile
3203; (require 'reporter))
3204
3205(defun py-submit-bug-report (enhancement-p)
3206 "Submit via mail a bug report on `python-mode'.
Barry Warsaw41a05c71998-08-10 16:33:12 +00003207With \\[universal-argument] (programmatically, argument ENHANCEMENT-P
3208non-nil) just submit an enhancement request."
Barry Warsaw850437a1995-03-08 21:50:28 +00003209 (interactive
3210 (list (not (y-or-n-p
Barry Warsaw41a05c71998-08-10 16:33:12 +00003211 "Is this a bug report (hit `n' to send other comments)? "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00003212 (let ((reporter-prompt-for-summary-p (if enhancement-p
3213 "(Very) brief summary: "
3214 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00003215 (require 'reporter)
3216 (reporter-submit-bug-report
3217 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00003218 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00003219 ;; varlist
3220 (if enhancement-p nil
3221 '(py-python-command
3222 py-indent-offset
3223 py-block-comment-prefix
Barry Warsaw850437a1995-03-08 21:50:28 +00003224 py-temp-directory
3225 py-beep-if-tab-change))
3226 nil ;pre-hooks
3227 nil ;post-hooks
3228 "Dear Barry,") ;salutation
3229 (if enhancement-p nil
3230 (set-mark (point))
3231 (insert
3232"Please replace this text with a sufficiently large code sample\n\
3233and an exact recipe so that I can reproduce your problem. Failure\n\
3234to do so may mean a greater delay in fixing your bug.\n\n")
3235 (exchange-point-and-mark)
3236 (py-keep-region-active))))
3237
3238
Barry Warsaw47384781997-11-26 05:27:45 +00003239(defun py-kill-emacs-hook ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003240 "Delete files in `py-file-queue'.
3241These are Python temporary files awaiting execution."
Barry Warsaw47384781997-11-26 05:27:45 +00003242 (mapcar #'(lambda (filename)
3243 (py-safe (delete-file filename)))
3244 py-file-queue))
3245
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003246;; arrange to kill temp files when Emacs exists
Barry Warsawc72c11c1997-08-09 06:42:08 +00003247(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
Barry Warsaw4f577d22001-04-11 20:23:17 +00003248(add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
3249
3250;; Add a designator to the minor mode strings
3251(or (assq 'py-pdbtrack-minor-mode-string minor-mode-alist)
3252 (push '(py-pdbtrack-is-tracking-p py-pdbtrack-minor-mode-string)
3253 minor-mode-alist))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003254
3255
3256
3257(provide 'python-mode)
3258;;; python-mode.el ends here