blob: 1cff2d9d0f38885068aa904955e517ec3662ca78 [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 Warsaw2ccda501997-01-30 19:50:39 +00005;; Author: 1995-1997 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 Warsaw5e21cb01997-11-05 18:41:11 +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
24;; subsequently left the net; in 1995, Barry Warsaw inherited the
25;; mode and is the current maintainer.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000026
Barry Warsaw37231521997-12-11 17:23:13 +000027;; COMPATIBILITY:
Barry Warsaw7b0f5681995-03-08 21:33:04 +000028
Barry Warsaw37231521997-12-11 17:23:13 +000029;; This version of python-mode.el is no longer compatible with Emacs
30;; 18. For a gabazillion reasons, I highly recommend upgrading to
31;; X/Emacs 19 or X/Emacs 20. I recommend at least Emacs 19.34 or
32;; XEmacs 19.15. Any of the v20 X/Emacsen should be fine.
Barry Warsaw673d05f1997-12-02 21:51:57 +000033
Barry Warsaw37231521997-12-11 17:23:13 +000034;; NOTE TO FSF EMACS USERS:
35
36;; You may need to acquire the Custom library -- this applies to users
37;; of Emacs 19.34 and NTEmacs based on 19.34, but not to Emacs 20
38;; users. You must also byte-compile this file before use -- this
39;; applies to FSF's Emacs 19.34, 20.x, and NTEmacs based on 19.34.
40;; None of this applies to XEmacs (although byte compilation is still
41;; recommended). You will also need to add the following to your
42;; .emacs file so that the .py files come up in python-mode:
Barry Warsaw7ae77681994-12-12 20:38:05 +000043;;
Barry Warsawaffc0ca1997-11-03 16:59:38 +000044;; (autoload 'python-mode "python-mode" "Python editing mode." t)
45;; (setq auto-mode-alist
46;; (cons '("\\.py$" . python-mode) auto-mode-alist))
47;; (setq interpreter-mode-alist
48;; (cons '("python" . python-mode) interpreter-mode-alist))
Barry Warsaw44b72201996-07-05 20:11:35 +000049;;
Barry Warsawaffc0ca1997-11-03 16:59:38 +000050;; Assuming python-mode.el is on your load-path, it will be invoked
51;; when you visit a .py file, or a file with a first line that looks
52;; like:
53;;
54;; #! /usr/bin/env python
55
Barry Warsaw37231521997-12-11 17:23:13 +000056;; NOTE TO XEMACS USERS:
57
58;; An older version of this file was distributed with XEmacs 19.15,
59;; 19.16 and 20.3. By default, in XEmacs when you visit a .py file,
60;; the buffer is put in Python mode. Likewise for executable scripts
61;; with the word `python' on the first line. You shouldn't need to do
62;; much except make sure this new version is earlier in your
63;; load-path, and byte-compile this file.
64
65;; FOR MORE INFORMATION:
66
67;; Please see <http://www.python.org/ftp/emacs/pmdetails.html> for the
68;; latest information and compatibility notes.
69
70;; BUG REPORTING:
Barry Warsaw7ae77681994-12-12 20:38:05 +000071
Barry Warsawaffc0ca1997-11-03 16:59:38 +000072;; To submit bug reports, use C-c C-b. Please include a complete, but
73;; concise code sample and a recipe for reproducing the bug. Send
74;; suggestions and other comments to python-mode@python.org.
75
76;; When in a Python mode buffer, do a C-h m for more help. It's
Barry Warsaw37231521997-12-11 17:23:13 +000077;; doubtful that a texinfo manual would be very useful, but if you
78;; want to contribute one, I'll certainly accept it!
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000079
Barry Warsaw5ea20d51997-12-06 00:00:47 +000080;; If you are using XEmacs, you may also want to check out OO-Browser
81;; that comes bundled with it, including documentation in the info
82;; pages. For GNU Emacs you have to install it yourself. To read
83;; more about OO-Browser, follow these links:
84
85;; http://www.python.org/workshops/1996-06/papers/h.pasanen/oobr_contents.html
86;; http://www.infodock.com/manuals/alt-oobr-cover.html
87
Barry Warsaw37231521997-12-11 17:23:13 +000088;; You may also want to take a look at Harri Pasanen's "Python Library
89;; Reference Hot-Key Help System for XEmacs (or PLRHKHSX for short ;),
90;; version 1.0"
Barry Warsaw7b0f5681995-03-08 21:33:04 +000091;;
Barry Warsaw37231521997-12-11 17:23:13 +000092;; <http://www.iki.fi/hpa/>
93
94;; TO DO LIST:
95
96;; - Better integration with pdb.py and gud-mode for debugging.
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000097;; - Rewrite according to GNU Emacs Lisp standards.
Barry Warsaw5c0d00f1996-07-31 21:30:21 +000098;; - possibly force indent-tabs-mode == nil, and add a
99;; write-file-hooks that runs untabify on the whole buffer (to work
100;; around potential tab/space mismatch problems). In practice this
101;; hasn't been a problem... yet.
Barry Warsaw9e277db1996-07-31 22:33:40 +0000102;; - have py-execute-region on indented code act as if the region is
Barry Warsaw37231521997-12-11 17:23:13 +0000103;; left justified. Avoids syntax errors.
104;; - add a py-goto-block-down, bound to C-c C-d
Barry Warsaw7ae77681994-12-12 20:38:05 +0000105
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000106;;; Code:
107
Barry Warsawc72c11c1997-08-09 06:42:08 +0000108(require 'custom)
Barry Warsaw8529ebb1997-12-01 20:03:12 +0000109(eval-when-compile
Barry Warsaw673d05f1997-12-02 21:51:57 +0000110 (require 'cl)
111 (require 'custom)
112 ;; Stock Emacs 19.34 has a broken/old Custom library that does more
113 ;; harm than good
114 (or (fboundp 'defcustom)
115 (error "STOP! STOP! STOP! STOP!
116
117The Custom library was not found or is out of date. A more current
118version is required. Please download and install the latest version
119of the Custom library from:
120
121 <http://www.dina.kvl.dk/~abraham/custom/>
122
123See the Python Mode home page for details:
124
125 <http://www.python.org/ftp/emacs/>
126")))
127
Barry Warsawc72c11c1997-08-09 06:42:08 +0000128
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000129
130;; user definable variables
131;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +0000132
Barry Warsawc72c11c1997-08-09 06:42:08 +0000133(defgroup python nil
134 "Support for the Python programming language, <http://www.python.org/>"
135 :group 'languages)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000136
Barry Warsawc72c11c1997-08-09 06:42:08 +0000137(defcustom py-python-command "python"
138 "*Shell command used to start Python interpreter."
139 :type 'string
140 :group 'python)
141
142(defcustom py-indent-offset 4
143 "*Amount of offset per level of indentation
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000144Note that `\\[py-guess-indent-offset]' can usually guess a good value
Barry Warsawc72c11c1997-08-09 06:42:08 +0000145when you're editing someone else's Python code."
146 :type 'integer
147 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000148
Barry Warsawc72c11c1997-08-09 06:42:08 +0000149(defcustom py-align-multiline-strings-p t
150 "*Flag describing how multi-line triple quoted strings are aligned.
Barry Warsaw095e9c61995-09-19 20:01:42 +0000151When this flag is non-nil, continuation lines are lined up under the
152preceding line's indentation. When this flag is nil, continuation
Barry Warsawc72c11c1997-08-09 06:42:08 +0000153lines are aligned to column zero."
154 :type '(choice (const :tag "Align under preceding line" t)
155 (const :tag "Align to column zero" nil))
156 :group 'python)
Barry Warsaw095e9c61995-09-19 20:01:42 +0000157
Barry Warsawc72c11c1997-08-09 06:42:08 +0000158(defcustom py-block-comment-prefix "## "
Barry Warsaw867a32a1996-03-07 18:30:26 +0000159 "*String used by \\[comment-region] to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000160This should follow the convention for non-indenting comment lines so
161that the indentation commands won't get confused (i.e., the string
162should be of the form `#x...' where `x' is not a blank or a tab, and
Barry Warsawc72c11c1997-08-09 06:42:08 +0000163`...' is arbitrary)."
164 :type 'string
165 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000166
Barry Warsawc72c11c1997-08-09 06:42:08 +0000167(defcustom py-honor-comment-indentation t
Barry Warsaw6d627751996-03-06 18:41:38 +0000168 "*Controls how comment lines influence subsequent indentation.
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000169
Barry Warsaw6d627751996-03-06 18:41:38 +0000170When nil, all comment lines are skipped for indentation purposes, and
Barry Warsawc72c11c1997-08-09 06:42:08 +0000171if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond).
Barry Warsaw6d627751996-03-06 18:41:38 +0000172
173When t, lines that begin with a single `#' are a hint to subsequent
174line indentation. If the previous line is such a comment line (as
175opposed to one that starts with `py-block-comment-prefix'), then it's
176indentation is used as a hint for this line's indentation. Lines that
177begin with `py-block-comment-prefix' are ignored for indentation
178purposes.
179
180When not nil or t, comment lines that begin with a `#' are used as
Barry Warsawc72c11c1997-08-09 06:42:08 +0000181indentation hints, unless the comment character is in column zero."
182 :type '(choice
183 (const :tag "Skip all comment lines (fast)" nil)
184 (const :tag "Single # `sets' indentation for next line" t)
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000185 (const :tag "Single # `sets' indentation except at column zero"
186 other)
Barry Warsawc72c11c1997-08-09 06:42:08 +0000187 )
188 :group 'python)
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000189
Barry Warsawc72c11c1997-08-09 06:42:08 +0000190(defcustom py-scroll-process-buffer t
Barry Warsaw7ae77681994-12-12 20:38:05 +0000191 "*Scroll Python process buffer as output arrives.
192If nil, the Python process buffer acts, with respect to scrolling, like
193Shell-mode buffers normally act. This is surprisingly complicated and
194so won't be explained here; in fact, you can't get the whole story
195without studying the Emacs C code.
196
197If non-nil, the behavior is different in two respects (which are
198slightly inaccurate in the interest of brevity):
199
200 - If the buffer is in a window, and you left point at its end, the
201 window will scroll as new output arrives, and point will move to the
202 buffer's end, even if the window is not the selected window (that
203 being the one the cursor is in). The usual behavior for shell-mode
204 windows is not to scroll, and to leave point where it was, if the
205 buffer is in a window other than the selected window.
206
207 - If the buffer is not visible in any window, and you left point at
208 its end, the buffer will be popped into a window as soon as more
209 output arrives. This is handy if you have a long-running
210 computation and don't want to tie up screen area waiting for the
211 output. The usual behavior for a shell-mode buffer is to stay
212 invisible until you explicitly visit it.
213
214Note the `and if you left point at its end' clauses in both of the
215above: you can `turn off' the special behaviors while output is in
216progress, by visiting the Python buffer and moving point to anywhere
217besides the end. Then the buffer won't scroll, point will remain where
218you leave it, and if you hide the buffer it will stay hidden until you
219visit it again. You can enable and disable the special behaviors as
220often as you like, while output is in progress, by (respectively) moving
221point to, or away from, the end of the buffer.
222
223Warning: If you expect a large amount of output, you'll probably be
224happier setting this option to nil.
225
226Obscure: `End of buffer' above should really say `at or beyond the
227process mark', but if you know what that means you didn't need to be
Barry Warsawc72c11c1997-08-09 06:42:08 +0000228told <grin>."
229 :type 'boolean
230 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000231
Barry Warsaw516b6201997-08-09 06:43:20 +0000232(defcustom py-temp-directory
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000233 (let ((ok '(lambda (x)
234 (and x
235 (setq x (expand-file-name x)) ; always true
236 (file-directory-p x)
237 (file-writable-p x)
238 x))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000239 (or (funcall ok (getenv "TMPDIR"))
240 (funcall ok "/usr/tmp")
241 (funcall ok "/tmp")
242 (funcall ok ".")
243 (error
244 "Couldn't find a usable temp directory -- set py-temp-directory")))
245 "*Directory used for temp files created by a *Python* process.
246By default, the first directory from this list that exists and that you
247can write into: the value (if any) of the environment variable TMPDIR,
Barry Warsawc72c11c1997-08-09 06:42:08 +0000248/usr/tmp, /tmp, or the current directory."
249 :type 'string
250 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000251
Barry Warsaw516b6201997-08-09 06:43:20 +0000252(defcustom py-beep-if-tab-change t
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000253 "*Ring the bell if tab-width is changed.
254If a comment of the form
255
256 \t# vi:set tabsize=<number>:
257
258is found before the first code line when the file is entered, and the
259current value of (the general Emacs variable) `tab-width' does not
260equal <number>, `tab-width' is set to <number>, a message saying so is
261displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
Barry Warsawc72c11c1997-08-09 06:42:08 +0000262the Emacs bell is also rung as a warning."
263 :type 'boolean
264 :group 'python)
265
Barry Warsaw9981d221997-12-03 05:25:48 +0000266(defcustom py-jump-on-exception t
267 "*Jump to innermost exception frame in *Python Output* buffer.
268When this variable is non-nil and ane exception occurs when running
269Python code synchronously in a subprocess, jump immediately to the
270source code of the innermost frame.")
271
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000272(defcustom py-backspace-function 'backward-delete-char-untabify
273 "*Function called by `py-electric-backspace' when deleting backwards."
274 :type 'function
275 :group 'python)
276
277(defcustom py-delete-function 'delete-char
278 "*Function called by `py-electric-delete' when deleting forwards."
279 :type 'function
280 :group 'python)
281
Barry Warsawc0ecb531998-01-20 21:43:34 +0000282;; Not customizable
283(defvar py-master-file nil
284 "If non-nil, execute the named file instead of the buffer's file.
285The intent is to allow someone to set this variable file local
286variable section, e.g.:
287
288 # Local Variables:
289 # py-master-file: \"master.py\"
290 # End:
291
292so that hitting \\[py-execute-buffer] in that buffer executes the
293named master file instead of the buffer's file. Note that if the file
294name has a relative path, the `default-directory' for the file is
295prepended to come up with a buffer name.")
296(make-variable-buffer-local 'py-master-file)
297
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000298
Barry Warsawc72c11c1997-08-09 06:42:08 +0000299
300;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
301;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
302
Barry Warsaw5e21cb01997-11-05 18:41:11 +0000303(defconst py-emacs-features
304 (let (features)
305 ;; NTEmacs 19.34.6 has a broken make-temp-name; it always returns
306 ;; the same string.
307 (let ((tmp1 (make-temp-name ""))
308 (tmp2 (make-temp-name "")))
309 (if (string-equal tmp1 tmp2)
310 (push 'broken-temp-names features)))
311 ;; return the features
312 features)
Barry Warsawc12c62e1997-09-04 04:18:07 +0000313 "A list of features extant in the Emacs you are using.
Barry Warsaw6ae21ad1997-11-06 14:36:49 +0000314There are many flavors of Emacs out there, with different levels of
315support for features needed by `python-mode'.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000316
Barry Warsawfb07f401997-02-24 03:37:22 +0000317(defvar python-font-lock-keywords
Barry Warsawb8f11661997-11-06 14:35:15 +0000318 (let ((kw1 (mapconcat 'identity
319 '("and" "assert" "break" "class"
320 "continue" "def" "del" "elif"
321 "else" "except" "exec" "for"
322 "from" "global" "if" "import"
323 "in" "is" "lambda" "not"
324 "or" "pass" "print" "raise"
325 "return" "while"
326 )
327 "\\|"))
328 (kw2 (mapconcat 'identity
329 '("else:" "except:" "finally:" "try:")
330 "\\|"))
331 )
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000332 (list
Barry Warsawef3c8911997-11-05 18:55:50 +0000333 ;; keywords
Barry Warsawb8f11661997-11-06 14:35:15 +0000334 (cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
335 ;; block introducing keywords with immediately following colons.
336 ;; Yes "except" is in both lists.
337 (cons (concat "\\b\\(" kw2 "\\)[ \n\t(]") 1)
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000338 ;; classes
339 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
340 1 font-lock-type-face)
341 ;; functions
342 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
343 1 font-lock-function-name-face)
344 ))
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000345 "Additional expressions to highlight in Python mode.")
Barry Warsawfb07f401997-02-24 03:37:22 +0000346(put 'python-mode 'font-lock-defaults '(python-font-lock-keywords))
347
Barry Warsawb01b4fa1995-06-20 18:55:34 +0000348
Barry Warsaw81437461996-08-01 19:48:02 +0000349(defvar imenu-example--python-show-method-args-p nil
350 "*Controls echoing of arguments of functions & methods in the imenu buffer.
351When non-nil, arguments are printed.")
352
Barry Warsaw52bc17c1995-10-12 21:15:49 +0000353(make-variable-buffer-local 'py-indent-offset)
354
Barry Warsawe467bfb1997-11-26 05:40:58 +0000355;; have to bind py-file-queue before installing the kill-emacs-hook
Barry Warsaw7ae77681994-12-12 20:38:05 +0000356(defvar py-file-queue nil
357 "Queue of Python temp files awaiting execution.
358Currently-active file is at the head of the list.")
359
Barry Warsawc72c11c1997-08-09 06:42:08 +0000360
361;; Constants
362
363;; Regexp matching a Python string literal
364(defconst py-stringlit-re
365 (concat
366 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
367 "\\|" ; or
368 "\"\\([^\"\n\\]\\|\\\\.\\)*\"")) ; double-quoted
369
370;; Regexp matching Python lines that are continued via backslash.
371;; This is tricky because a trailing backslash does not mean
372;; continuation if it's in a comment
373(defconst py-continued-re
374 (concat
375 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
376 "\\\\$"))
377
378;; Regexp matching blank or comment lines.
379(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)")
380
381;; Regexp matching clauses to be outdented one level.
382(defconst py-outdent-re
383 (concat "\\(" (mapconcat 'identity
384 '("else:"
385 "except\\(\\s +.*\\)?:"
386 "finally:"
387 "elif\\s +.*:")
388 "\\|")
389 "\\)"))
390
391
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000392;; Regexp matching keywords which typically close a block
393(defconst py-block-closing-keywords-re
394 "\\(return\\|raise\\|break\\|continue\\|pass\\)")
395
Barry Warsawc72c11c1997-08-09 06:42:08 +0000396;; Regexp matching lines to not outdent after.
397(defconst py-no-outdent-re
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000398 (concat
399 "\\("
400 (mapconcat 'identity
401 (list "try:"
402 "except\\(\\s +.*\\)?:"
403 "while\\s +.*:"
404 "for\\s +.*:"
405 "if\\s +.*:"
406 "elif\\s +.*:"
407 (concat py-block-closing-keywords-re "[ \t\n]")
408 )
409 "\\|")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000410 "\\)"))
411
412;; Regexp matching a function, method or variable assignment. If you
413;; change this, you probably have to change `py-current-defun' as
414;; well. This is only used by `py-current-defun' to find the name for
415;; add-log.el.
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000416(defconst py-defun-start-re
Barry Warsawc72c11c1997-08-09 06:42:08 +0000417 "^\\([ \t]*\\)def[ \t]+\\([a-zA-Z_0-9]+\\)\\|\\(^[a-zA-Z_0-9]+\\)[ \t]*=")
418
419;; Regexp for finding a class name. If you change this, you probably
420;; have to change `py-current-defun' as well. This is only used by
421;; `py-current-defun' to find the name for add-log.el.
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000422(defconst py-class-start-re "^class[ \t]*\\([a-zA-Z_0-9]+\\)")
423
424;; Regexp that describes tracebacks
425(defconst py-traceback-line-re
Barry Warsawffbc17d1997-11-27 20:08:14 +0000426 "[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000427
428
429
430;; Utilities
431
432(defmacro py-safe (&rest body)
433 ;; safely execute BODY, return nil if an error occurred
434 (` (condition-case nil
435 (progn (,@ body))
436 (error nil))))
437
438(defsubst py-keep-region-active ()
439 ;; Do whatever is necessary to keep the region active in XEmacs.
440 ;; Ignore byte-compiler warnings you might see. Also note that
441 ;; FSF's Emacs 19 does it differently; its policy doesn't require us
442 ;; to take explicit action.
443 (and (boundp 'zmacs-region-stays)
444 (setq zmacs-region-stays t)))
445
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000446(defsubst py-point (position)
447 ;; Returns the value of point at certain commonly referenced POSITIONs.
448 ;; POSITION can be one of the following symbols:
449 ;;
450 ;; bol -- beginning of line
451 ;; eol -- end of line
452 ;; bod -- beginning of defun
453 ;; boi -- back to indentation
454 ;;
455 ;; This function does not modify point or mark.
456 (let ((here (point)))
457 (cond
458 ((eq position 'bol) (beginning-of-line))
459 ((eq position 'eol) (end-of-line))
460 ((eq position 'bod) (beginning-of-python-def-or-class))
461 ((eq position 'bob) (beginning-of-buffer))
462 ((eq position 'eob) (end-of-buffer))
463 ((eq position 'boi) (back-to-indentation))
464 (t (error "unknown buffer position requested: %s" position))
465 )
466 (prog1
467 (point)
468 (goto-char here))))
469
470(defsubst py-highlight-line (from to file line)
471 (cond
472 ((fboundp 'make-extent)
473 ;; XEmacs
474 (let ((e (make-extent from to)))
475 (set-extent-property e 'mouse-face 'highlight)
476 (set-extent-property e 'py-exc-info (cons file line))
477 (set-extent-property e 'keymap py-mode-output-map)))
478 (t
479 ;; Emacs -- Please port this!
480 )
481 ))
482
Barry Warsawc72c11c1997-08-09 06:42:08 +0000483
484;; Major mode boilerplate
485
Barry Warsaw7ae77681994-12-12 20:38:05 +0000486;; define a mode-specific abbrev table for those who use such things
487(defvar python-mode-abbrev-table nil
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000488 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000489(define-abbrev-table 'python-mode-abbrev-table nil)
490
Barry Warsaw7ae77681994-12-12 20:38:05 +0000491(defvar python-mode-hook nil
492 "*Hook called by `python-mode'.")
493
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000494;; in previous version of python-mode.el, the hook was incorrectly
495;; called py-mode-hook, and was not defvar'd. deprecate its use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000496(and (fboundp 'make-obsolete-variable)
497 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
498
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000499(defvar py-mode-map ()
500 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000501(if py-mode-map
Barry Warsawc72c11c1997-08-09 06:42:08 +0000502 nil
Barry Warsaw7ae77681994-12-12 20:38:05 +0000503 (setq py-mode-map (make-sparse-keymap))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000504 ;; electric keys
505 (define-key py-mode-map ":" 'py-electric-colon)
506 ;; indentation level modifiers
507 (define-key py-mode-map "\C-c\C-l" 'py-shift-region-left)
508 (define-key py-mode-map "\C-c\C-r" 'py-shift-region-right)
509 (define-key py-mode-map "\C-c<" 'py-shift-region-left)
510 (define-key py-mode-map "\C-c>" 'py-shift-region-right)
511 ;; subprocess commands
512 (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
513 (define-key py-mode-map "\C-c|" 'py-execute-region)
514 (define-key py-mode-map "\C-c!" 'py-shell)
515 ;; Caution! Enter here at your own risk. We are trying to support
516 ;; several behaviors and it gets disgusting. :-( This logic ripped
517 ;; largely from CC Mode.
518 ;;
519 ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
520 ;; backwards deletion behavior to DEL, which both Delete and
521 ;; Backspace get translated to. There's no way to separate this
522 ;; behavior in a clean way, so deal with it! Besides, it's been
523 ;; this way since the dawn of time.
524 (if (not (boundp 'delete-key-deletes-forward))
525 (define-key py-mode-map "\177" 'py-electric-backspace)
526 ;; However, XEmacs 20 actually achieved enlightenment. It is
527 ;; possible to sanely define both backward and forward deletion
528 ;; behavior under X separately (TTYs are forever beyond hope, but
529 ;; who cares? XEmacs 20 does the right thing with these too).
530 (define-key py-mode-map [delete] 'py-electric-delete)
531 (define-key py-mode-map [backspace] 'py-electric-backspace))
Barry Warsaw8c4a8de1997-11-26 20:30:33 +0000532 ;; Separate M-BS from C-M-h. The former should remain
533 ;; backward-kill-word.
534 (define-key py-mode-map [(control meta h)] 'py-mark-def-or-class)
Barry Warsaw2518c671997-11-05 00:51:08 +0000535 (define-key py-mode-map "\C-c\C-k" 'py-mark-block)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000536 ;; Miscellaneous
537 (define-key py-mode-map "\C-c:" 'py-guess-indent-offset)
538 (define-key py-mode-map "\C-c\t" 'py-indent-region)
539 (define-key py-mode-map "\C-c\C-n" 'py-next-statement)
540 (define-key py-mode-map "\C-c\C-p" 'py-previous-statement)
541 (define-key py-mode-map "\C-c\C-u" 'py-goto-block-up)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000542 (define-key py-mode-map "\C-c#" 'py-comment-region)
543 (define-key py-mode-map "\C-c?" 'py-describe-mode)
544 (define-key py-mode-map "\C-c\C-hm" 'py-describe-mode)
545 (define-key py-mode-map "\e\C-a" 'beginning-of-python-def-or-class)
546 (define-key py-mode-map "\e\C-e" 'end-of-python-def-or-class)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000547 (define-key py-mode-map "\C-c-" 'py-up-exception)
548 (define-key py-mode-map "\C-c=" 'py-down-exception)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000549 ;; information
550 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
551 (define-key py-mode-map "\C-c\C-v" 'py-version)
552 ;; py-newline-and-indent mappings
Barry Warsaw82aecb91998-01-21 05:14:24 +0000553 (define-key py-mode-map "\n" 'py-newline-and-indent)
554 (define-key py-mode-map "\C-m" 'py-newline-and-indent)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000555 ;; shadow global bindings for newline-and-indent w/ the py- version.
556 ;; BAW - this is extremely bad form, but I'm not going to change it
557 ;; for now.
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000558 (mapcar #'(lambda (key)
559 (define-key py-mode-map key 'py-newline-and-indent))
560 (where-is-internal 'newline-and-indent))
561 )
562
563(defvar py-mode-output-map nil
564 "Keymap used in *Python Output* buffers*")
565(if py-mode-output-map
566 nil
567 (setq py-mode-output-map (make-sparse-keymap))
568 (define-key py-mode-output-map [button2] 'py-mouseto-exception)
569 (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
570 ;; TBD: Disable all self-inserting keys. This is bogus, we should
571 ;; really implement this as *Python Output* buffer being read-only
572 (mapcar #' (lambda (key)
573 (define-key py-mode-output-map key
574 #'(lambda () (interactive) (beep))))
575 (where-is-internal 'self-insert-command))
Barry Warsaw850437a1995-03-08 21:50:28 +0000576 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000577
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000578(defvar py-mode-syntax-table nil
579 "Syntax table used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000580(if py-mode-syntax-table
Barry Warsawc72c11c1997-08-09 06:42:08 +0000581 nil
Barry Warsaw7ae77681994-12-12 20:38:05 +0000582 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsawc72c11c1997-08-09 06:42:08 +0000583 (modify-syntax-entry ?\( "()" py-mode-syntax-table)
584 (modify-syntax-entry ?\) ")(" py-mode-syntax-table)
585 (modify-syntax-entry ?\[ "(]" py-mode-syntax-table)
586 (modify-syntax-entry ?\] ")[" py-mode-syntax-table)
587 (modify-syntax-entry ?\{ "(}" py-mode-syntax-table)
588 (modify-syntax-entry ?\} "){" py-mode-syntax-table)
589 ;; Add operator symbols misassigned in the std table
590 (modify-syntax-entry ?\$ "." py-mode-syntax-table)
591 (modify-syntax-entry ?\% "." py-mode-syntax-table)
592 (modify-syntax-entry ?\& "." py-mode-syntax-table)
593 (modify-syntax-entry ?\* "." py-mode-syntax-table)
594 (modify-syntax-entry ?\+ "." py-mode-syntax-table)
595 (modify-syntax-entry ?\- "." py-mode-syntax-table)
596 (modify-syntax-entry ?\/ "." py-mode-syntax-table)
597 (modify-syntax-entry ?\< "." py-mode-syntax-table)
598 (modify-syntax-entry ?\= "." py-mode-syntax-table)
599 (modify-syntax-entry ?\> "." py-mode-syntax-table)
600 (modify-syntax-entry ?\| "." py-mode-syntax-table)
601 ;; For historical reasons, underscore is word class instead of
602 ;; symbol class. GNU conventions say it should be symbol class, but
603 ;; there's a natural conflict between what major mode authors want
604 ;; and what users expect from `forward-word' and `backward-word'.
605 ;; Guido and I have hashed this out and have decided to keep
606 ;; underscore in word class. If you're tempted to change it, try
607 ;; binding M-f and M-b to py-forward-into-nomenclature and
608 ;; py-backward-into-nomenclature instead.
609 (modify-syntax-entry ?\_ "w" py-mode-syntax-table)
610 ;; Both single quote and double quote are string delimiters
611 (modify-syntax-entry ?\' "\"" py-mode-syntax-table)
612 (modify-syntax-entry ?\" "\"" py-mode-syntax-table)
613 ;; backquote is open and close paren
614 (modify-syntax-entry ?\` "$" py-mode-syntax-table)
615 ;; comment delimiters
616 (modify-syntax-entry ?\# "<" py-mode-syntax-table)
617 (modify-syntax-entry ?\n ">" py-mode-syntax-table)
618 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000619
Barry Warsawb3e81d51996-09-04 15:12:42 +0000620
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000621
Barry Warsaw42f707f1996-07-29 21:05:05 +0000622;; Menu definitions, only relevent if you have the easymenu.el package
623;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
Barry Warsaw5490a061996-08-06 15:43:33 +0000624(defvar py-menu nil
625 "Menu for Python Mode.
Barry Warsawc72c11c1997-08-09 06:42:08 +0000626This menu will get created automatically if you have the `easymenu'
627package. Note that the latest X/Emacs releases contain this package.")
Barry Warsaw5490a061996-08-06 15:43:33 +0000628
Barry Warsawc72c11c1997-08-09 06:42:08 +0000629(and (py-safe (require 'easymenu) t)
630 (easy-menu-define
631 py-menu py-mode-map "Python Mode menu"
632 '("Python"
633 ["Comment Out Region" py-comment-region (mark)]
634 ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)]
635 "-"
636 ["Mark current block" py-mark-block t]
Barry Warsaw2518c671997-11-05 00:51:08 +0000637 ["Mark current def" py-mark-def-or-class t]
638 ["Mark current class" (py-mark-def-or-class t) t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000639 "-"
640 ["Shift region left" py-shift-region-left (mark)]
641 ["Shift region right" py-shift-region-right (mark)]
642 "-"
643 ["Execute buffer" py-execute-buffer t]
644 ["Execute region" py-execute-region (mark)]
645 ["Start interpreter..." py-shell t]
646 "-"
647 ["Go to start of block" py-goto-block-up t]
648 ["Go to start of class" (beginning-of-python-def-or-class t) t]
649 ["Move to end of class" (end-of-python-def-or-class t) t]
650 ["Move to start of def" beginning-of-python-def-or-class t]
651 ["Move to end of def" end-of-python-def-or-class t]
652 "-"
653 ["Describe mode" py-describe-mode t]
654 )))
Barry Warsaw42f707f1996-07-29 21:05:05 +0000655
Barry Warsaw81437461996-08-01 19:48:02 +0000656
657
658;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp>
659(defvar imenu-example--python-class-regexp
660 (concat ; <<classes>>
661 "\\(" ;
662 "^[ \t]*" ; newline and maybe whitespace
663 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
664 ; possibly multiple superclasses
665 "\\([ \t]*\\((\\([a-zA-Z0-9_, \t\n]\\)*)\\)?\\)"
666 "[ \t]*:" ; and the final :
667 "\\)" ; >>classes<<
668 )
669 "Regexp for Python classes for use with the imenu package."
670 )
671
672(defvar imenu-example--python-method-regexp
673 (concat ; <<methods and functions>>
674 "\\(" ;
675 "^[ \t]*" ; new line and maybe whitespace
676 "\\(def[ \t]+" ; function definitions start with def
677 "\\([a-zA-Z0-9_]+\\)" ; name is here
678 ; function arguments...
679 "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))"
680 "\\)" ; end of def
681 "[ \t]*:" ; and then the :
682 "\\)" ; >>methods and functions<<
683 )
684 "Regexp for Python methods/functions for use with the imenu package."
685 )
686
687(defvar imenu-example--python-method-no-arg-parens '(2 8)
688 "Indicies into groups of the Python regexp for use with imenu.
689
690Using these values will result in smaller imenu lists, as arguments to
691functions are not listed.
692
693See the variable `imenu-example--python-show-method-args-p' for more
694information.")
695
696(defvar imenu-example--python-method-arg-parens '(2 7)
697 "Indicies into groups of the Python regexp for use with imenu.
698Using these values will result in large imenu lists, as arguments to
699functions are listed.
700
701See the variable `imenu-example--python-show-method-args-p' for more
702information.")
703
704;; Note that in this format, this variable can still be used with the
705;; imenu--generic-function. Otherwise, there is no real reason to have
706;; it.
707(defvar imenu-example--generic-python-expression
708 (cons
709 (concat
710 imenu-example--python-class-regexp
711 "\\|" ; or...
712 imenu-example--python-method-regexp
713 )
714 imenu-example--python-method-no-arg-parens)
715 "Generic Python expression which may be used directly with imenu.
716Used by setting the variable `imenu-generic-expression' to this value.
717Also, see the function \\[imenu-example--create-python-index] for a
718better alternative for finding the index.")
719
720;; These next two variables are used when searching for the python
721;; class/definitions. Just saving some time in accessing the
722;; generic-python-expression, really.
Barry Warsaw01af4011996-09-04 14:57:22 +0000723(defvar imenu-example--python-generic-regexp nil)
724(defvar imenu-example--python-generic-parens nil)
Barry Warsaw81437461996-08-01 19:48:02 +0000725
726
Barry Warsaw81437461996-08-01 19:48:02 +0000727(defun imenu-example--create-python-index ()
728 "Python interface function for imenu package.
729Finds all python classes and functions/methods. Calls function
730\\[imenu-example--create-python-index-engine]. See that function for
731the details of how this works."
732 (setq imenu-example--python-generic-regexp
733 (car imenu-example--generic-python-expression))
734 (setq imenu-example--python-generic-parens
735 (if imenu-example--python-show-method-args-p
736 imenu-example--python-method-arg-parens
737 imenu-example--python-method-no-arg-parens))
738 (goto-char (point-min))
739 (imenu-example--create-python-index-engine nil))
740
741(defun imenu-example--create-python-index-engine (&optional start-indent)
742 "Function for finding imenu definitions in Python.
743
744Finds all definitions (classes, methods, or functions) in a Python
745file for the imenu package.
746
747Returns a possibly nested alist of the form
748
749 (INDEX-NAME . INDEX-POSITION)
750
751The second element of the alist may be an alist, producing a nested
752list as in
753
754 (INDEX-NAME . INDEX-ALIST)
755
756This function should not be called directly, as it calls itself
757recursively and requires some setup. Rather this is the engine for
758the function \\[imenu-example--create-python-index].
759
760It works recursively by looking for all definitions at the current
761indention level. When it finds one, it adds it to the alist. If it
762finds a definition at a greater indentation level, it removes the
763previous definition from the alist. In it's place it adds all
764definitions found at the next indentation level. When it finds a
765definition that is less indented then the current level, it retuns the
766alist it has created thus far.
767
768The optional argument START-INDENT indicates the starting indentation
769at which to continue looking for Python classes, methods, or
770functions. If this is not supplied, the function uses the indentation
771of the first definition found."
772 (let ((index-alist '())
773 (sub-method-alist '())
774 looking-p
775 def-name prev-name
776 cur-indent def-pos
777 (class-paren (first imenu-example--python-generic-parens))
778 (def-paren (second imenu-example--python-generic-parens)))
779 (setq looking-p
780 (re-search-forward imenu-example--python-generic-regexp
781 (point-max) t))
782 (while looking-p
783 (save-excursion
784 ;; used to set def-name to this value but generic-extract-name is
785 ;; new to imenu-1.14. this way it still works with imenu-1.11
786 ;;(imenu--generic-extract-name imenu-example--python-generic-parens))
787 (let ((cur-paren (if (match-beginning class-paren)
788 class-paren def-paren)))
789 (setq def-name
Barry Warsawc8520351997-11-26 06:14:40 +0000790 (buffer-substring-no-properties (match-beginning cur-paren)
791 (match-end cur-paren))))
Barry Warsaw81437461996-08-01 19:48:02 +0000792 (beginning-of-line)
793 (setq cur-indent (current-indentation)))
794
795 ;; HACK: want to go to the next correct definition location. we
796 ;; explicitly list them here. would be better to have them in a
797 ;; list.
798 (setq def-pos
799 (or (match-beginning class-paren)
800 (match-beginning def-paren)))
801
802 ;; if we don't have a starting indent level, take this one
803 (or start-indent
804 (setq start-indent cur-indent))
805
806 ;; if we don't have class name yet, take this one
807 (or prev-name
808 (setq prev-name def-name))
809
810 ;; what level is the next definition on? must be same, deeper
811 ;; or shallower indentation
812 (cond
813 ;; at the same indent level, add it to the list...
814 ((= start-indent cur-indent)
815
816 ;; if we don't have push, use the following...
817 ;;(setf index-alist (cons (cons def-name def-pos) index-alist))
818 (push (cons def-name def-pos) index-alist))
819
820 ;; deeper indented expression, recur...
821 ((< start-indent cur-indent)
822
823 ;; the point is currently on the expression we're supposed to
824 ;; start on, so go back to the last expression. The recursive
825 ;; call will find this place again and add it to the correct
826 ;; list
827 (re-search-backward imenu-example--python-generic-regexp
828 (point-min) 'move)
829 (setq sub-method-alist (imenu-example--create-python-index-engine
830 cur-indent))
831
832 (if sub-method-alist
833 ;; we put the last element on the index-alist on the start
834 ;; of the submethod alist so the user can still get to it.
835 (let ((save-elmt (pop index-alist)))
Barry Warsawc8520351997-11-26 06:14:40 +0000836 (push (cons prev-name
Barry Warsaw81437461996-08-01 19:48:02 +0000837 (cons save-elmt sub-method-alist))
838 index-alist))))
839
840 ;; found less indented expression, we're done.
841 (t
842 (setq looking-p nil)
843 (re-search-backward imenu-example--python-generic-regexp
844 (point-min) t)))
845 (setq prev-name def-name)
846 (and looking-p
847 (setq looking-p
848 (re-search-forward imenu-example--python-generic-regexp
849 (point-max) 'move))))
850 (nreverse index-alist)))
851
Barry Warsaw42f707f1996-07-29 21:05:05 +0000852
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000853;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000854(defun python-mode ()
855 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000856To submit a problem report, enter `\\[py-submit-bug-report]' from a
857`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
858documentation. To see what version of `python-mode' you are running,
859enter `\\[py-version]'.
860
861This mode knows about Python indentation, tokens, comments and
862continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000863
864COMMANDS
865\\{py-mode-map}
866VARIABLES
867
Barry Warsaw42f707f1996-07-29 21:05:05 +0000868py-indent-offset\t\tindentation increment
869py-block-comment-prefix\t\tcomment string used by comment-region
870py-python-command\t\tshell command to invoke Python interpreter
871py-scroll-process-buffer\t\talways scroll Python process buffer
872py-temp-directory\t\tdirectory used for temp files (if needed)
873py-beep-if-tab-change\t\tring the bell if tab-width is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +0000874 (interactive)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000875 ;; set up local variables
Barry Warsaw7ae77681994-12-12 20:38:05 +0000876 (kill-all-local-variables)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000877 (make-local-variable 'font-lock-defaults)
878 (make-local-variable 'paragraph-separate)
879 (make-local-variable 'paragraph-start)
880 (make-local-variable 'require-final-newline)
881 (make-local-variable 'comment-start)
Barry Warsaw5c8bef11996-12-17 21:56:10 +0000882 (make-local-variable 'comment-end)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000883 (make-local-variable 'comment-start-skip)
884 (make-local-variable 'comment-column)
885 (make-local-variable 'indent-region-function)
886 (make-local-variable 'indent-line-function)
Barry Warsawb3e81d51996-09-04 15:12:42 +0000887 (make-local-variable 'add-log-current-defun-function)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000888 ;;
Barry Warsaw7ae77681994-12-12 20:38:05 +0000889 (set-syntax-table py-mode-syntax-table)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000890 (setq major-mode 'python-mode
891 mode-name "Python"
892 local-abbrev-table python-mode-abbrev-table
Barry Warsaw5c38bf61997-12-02 22:01:04 +0000893 font-lock-defaults '(python-font-lock-keywords)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000894 paragraph-separate "^[ \t]*$"
895 paragraph-start "^[ \t]*$"
896 require-final-newline t
897 comment-start "# "
Barry Warsaw5c8bef11996-12-17 21:56:10 +0000898 comment-end ""
Barry Warsaw615d4a41996-09-04 14:14:10 +0000899 comment-start-skip "# *"
900 comment-column 40
Barry Warsaw550a02e1996-09-04 14:23:00 +0000901 indent-region-function 'py-indent-region
902 indent-line-function 'py-indent-line
Barry Warsawb3e81d51996-09-04 15:12:42 +0000903 ;; tell add-log.el how to find the current function/method/variable
904 add-log-current-defun-function 'py-current-defun
Barry Warsawb1f89511996-09-03 16:38:30 +0000905 )
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000906 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +0000907 ;; add the menu
908 (if py-menu
909 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +0000910 ;; Emacs 19 requires this
Barry Warsawc72c11c1997-08-09 06:42:08 +0000911 (if (boundp 'comment-multi-line)
Barry Warsaw57697af1995-09-14 20:01:14 +0000912 (setq comment-multi-line nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000913 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000914 ;;
915 ;; not sure where the magic comment has to be; to save time
916 ;; searching for a rarity, we give up if it's not found prior to the
917 ;; first executable statement.
918 ;;
919 ;; BAW - on first glance, this seems like complete hackery. Why was
920 ;; this necessary, and is it still necessary?
921 (let ((case-fold-search nil)
922 (start (point))
923 new-tab-width)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000924 (if (re-search-forward
925 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
926 (prog2 (py-next-statement 1) (point) (goto-char 1))
927 t)
928 (progn
929 (setq new-tab-width
930 (string-to-int
931 (buffer-substring (match-beginning 1) (match-end 1))))
932 (if (= tab-width new-tab-width)
933 nil
934 (setq tab-width new-tab-width)
935 (message "Caution: tab-width changed to %d" new-tab-width)
936 (if py-beep-if-tab-change (beep)))))
937 (goto-char start))
938
Barry Warsaw755c6711996-08-01 20:02:55 +0000939 ;; install imenu
Barry Warsaw27ee1151997-12-03 05:03:44 +0000940 (if (py-safe (require 'imenu))
941 (progn
942 (make-variable-buffer-local 'imenu-create-index-function)
943 (setq imenu-create-index-function
944 (function imenu-example--create-python-index))
945 (setq imenu-generic-expression
946 imenu-example--generic-python-expression)
947 (if (fboundp 'imenu-add-to-menubar)
948 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
949 ))
Barry Warsaw755c6711996-08-01 20:02:55 +0000950
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000951 ;; run the mode hook. py-mode-hook use is deprecated
Barry Warsaw7ae77681994-12-12 20:38:05 +0000952 (if python-mode-hook
953 (run-hooks 'python-mode-hook)
954 (run-hooks 'py-mode-hook)))
955
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000956
Barry Warsawb91b7431995-03-14 15:55:20 +0000957;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000958(defun py-outdent-p ()
959 ;; returns non-nil if the current line should outdent one level
960 (save-excursion
961 (and (progn (back-to-indentation)
962 (looking-at py-outdent-re))
Barry Warsawf06777d1998-01-21 05:36:18 +0000963 (progn (forward-line -1)
964 (py-goto-initial-line)
965 (back-to-indentation)
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000966 (while (or (looking-at py-blank-or-comment-re)
967 (bobp))
968 (backward-to-indentation 1))
969 (not (looking-at py-no-outdent-re)))
970 )))
971
Barry Warsawb91b7431995-03-14 15:55:20 +0000972(defun py-electric-colon (arg)
973 "Insert a colon.
974In certain cases the line is outdented appropriately. If a numeric
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000975argument is provided, that many colons are inserted non-electrically.
976Electric behavior is inhibited inside a string or comment."
Barry Warsawb91b7431995-03-14 15:55:20 +0000977 (interactive "P")
978 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000979 ;; are we in a string or comment?
980 (if (save-excursion
981 (let ((pps (parse-partial-sexp (save-excursion
982 (beginning-of-python-def-or-class)
983 (point))
984 (point))))
985 (not (or (nth 3 pps) (nth 4 pps)))))
986 (save-excursion
987 (let ((here (point))
988 (outdent 0)
Barry Warsaw7cb505c1996-10-23 20:44:59 +0000989 (indent (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000990 (if (and (not arg)
991 (py-outdent-p)
992 (= indent (save-excursion
Barry Warsawa7661821996-08-02 16:22:43 +0000993 (py-next-statement -1)
Barry Warsaw7cb505c1996-10-23 20:44:59 +0000994 (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000995 )
996 (setq outdent py-indent-offset))
997 ;; Don't indent, only outdent. This assumes that any lines that
998 ;; are already outdented relative to py-compute-indentation were
999 ;; put there on purpose. Its highly annoying to have `:' indent
1000 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
1001 ;; there a better way to determine this???
1002 (if (< (current-indentation) indent) nil
1003 (goto-char here)
1004 (beginning-of-line)
1005 (delete-horizontal-space)
1006 (indent-to (- indent outdent))
1007 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +00001008
1009
Barry Warsawa97a3f31997-11-04 18:47:06 +00001010;; Python subprocess utilities and filters
1011(defun py-execute-file (proc filename)
1012 ;; Send a properly formatted execfile('FILENAME') to the underlying
1013 ;; Python interpreter process FILENAME. Make that process's buffer
1014 ;; visible and force display. Also make comint believe the user
1015 ;; typed this string so that kill-output-from-shell does The Right
1016 ;; Thing.
1017 (let ((curbuf (current-buffer))
1018 (procbuf (process-buffer proc))
1019 (comint-scroll-to-bottom-on-output t)
1020 (msg (format "## working on region in file %s...\n" filename))
1021 (cmd (format "execfile('%s')\n" filename)))
1022 (unwind-protect
1023 (progn
1024 (set-buffer procbuf)
1025 (goto-char (point-max))
1026 (move-marker (process-mark proc) (point))
1027 (funcall (process-filter proc) proc msg))
1028 (set-buffer curbuf))
1029 (process-send-string proc cmd)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001030
1031(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001032 (let ((curbuf (current-buffer))
1033 (pbuf (process-buffer pyproc))
1034 (pmark (process-mark pyproc))
1035 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001036 ;; make sure we switch to a different buffer at least once. if we
1037 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001038 ;; window, and point is before the end, and lots of output is
1039 ;; coming at a fast pace, then (a) simple cursor-movement commands
1040 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
1041 ;; to have a visible effect (the window just doesn't get updated,
1042 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
1043 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001044 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001045 ;; #b makes no sense to me at all. #a almost makes sense: unless
1046 ;; we actually change buffers, set_buffer_internal in buffer.c
1047 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
1048 ;; seems to make the Emacs command loop reluctant to update the
1049 ;; display. Perhaps the default process filter in process.c's
1050 ;; read_process_output has update_mode_lines++ for a similar
1051 ;; reason? beats me ...
1052
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001053 (unwind-protect
1054 ;; make sure current buffer is restored
1055 ;; BAW - we want to check to see if this still applies
1056 (progn
1057 ;; mysterious ugly hack
1058 (if (eq curbuf pbuf)
1059 (set-buffer (get-buffer-create "*scratch*")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001060
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001061 (set-buffer pbuf)
1062 (let* ((start (point))
1063 (goback (< start pmark))
1064 (goend (and (not goback) (= start (point-max))))
1065 (buffer-read-only nil))
1066 (goto-char pmark)
1067 (insert string)
1068 (move-marker pmark (point))
1069 (setq file-finished
1070 (and py-file-queue
1071 (equal ">>> "
1072 (buffer-substring
1073 (prog2 (beginning-of-line) (point)
1074 (goto-char pmark))
1075 (point)))))
1076 (if goback (goto-char start)
1077 ;; else
1078 (if py-scroll-process-buffer
1079 (let* ((pop-up-windows t)
1080 (pwin (display-buffer pbuf)))
1081 (set-window-point pwin (point)))))
1082 (set-buffer curbuf)
1083 (if file-finished
1084 (progn
Barry Warsawf4710561997-11-26 21:00:36 +00001085 (py-safe (delete-file (car py-file-queue)))
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001086 (setq py-file-queue (cdr py-file-queue))
1087 (if py-file-queue
1088 (py-execute-file pyproc (car py-file-queue)))))
1089 (and goend
1090 (progn (set-buffer pbuf)
1091 (goto-char (point-max))))
1092 ))
1093 (set-buffer curbuf))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001094
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001095(defun py-postprocess-output-buffer (buf)
Barry Warsaw9981d221997-12-03 05:25:48 +00001096 (let (line file bol)
1097 (save-excursion
1098 (set-buffer buf)
1099 (beginning-of-buffer)
1100 (while (re-search-forward py-traceback-line-re nil t)
1101 (setq file (match-string 1)
1102 line (string-to-int (match-string 2))
1103 bol (py-point 'bol))
1104 (py-highlight-line bol (py-point 'eol) file line))
1105 (when (and py-jump-on-exception line)
1106 (beep)
1107 (py-jump-to-exception file line))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001108 )))
1109
Barry Warsaw9981d221997-12-03 05:25:48 +00001110
Barry Warsawa97a3f31997-11-04 18:47:06 +00001111
1112;;; Subprocess commands
1113
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001114;; only used when (memq 'broken-temp-names py-emacs-features)
1115(defvar py-serial-number 0)
1116(defvar py-exception-buffer nil)
1117(defconst py-output-buffer "*Python Output*")
1118
Barry Warsawa97a3f31997-11-04 18:47:06 +00001119;;;###autoload
1120(defun py-shell ()
1121 "Start an interactive Python interpreter in another window.
1122This is like Shell mode, except that Python is running in the window
1123instead of a shell. See the `Interactive Shell' and `Shell Mode'
1124sections of the Emacs manual for details, especially for the key
1125bindings active in the `*Python*' buffer.
1126
1127See the docs for variable `py-scroll-buffer' for info on scrolling
1128behavior in the process window.
1129
1130Warning: Don't use an interactive Python if you change sys.ps1 or
1131sys.ps2 from their default values, or if you're running code that
1132prints `>>> ' or `... ' at the start of a line. `python-mode' can't
1133distinguish your output from Python's output, and assumes that `>>> '
1134at the start of a line is a prompt from Python. Similarly, the Emacs
1135Shell mode code assumes that both `>>> ' and `... ' at the start of a
1136line are Python prompts. Bad things can happen if you fool either
1137mode.
1138
1139Warning: If you do any editing *in* the process buffer *while* the
1140buffer is accepting output from Python, do NOT attempt to `undo' the
1141changes. Some of the output (nowhere near the parts you changed!) may
1142be lost if you do. This appears to be an Emacs bug, an unfortunate
1143interaction between undo and process filters; the same problem exists in
1144non-Python process buffers using the default (Emacs-supplied) process
1145filter."
1146 ;; BAW - should undo be disabled in the python process buffer, if
1147 ;; this bug still exists?
1148 (interactive)
1149 (require 'comint)
Barry Warsaw2518c671997-11-05 00:51:08 +00001150 (switch-to-buffer-other-window
1151 (make-comint "Python" py-python-command nil "-i"))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001152 (make-local-variable 'comint-prompt-regexp)
1153 (setq comint-prompt-regexp "^>>> \\|^[.][.][.] ")
1154 (set-process-filter (get-buffer-process (current-buffer)) 'py-process-filter)
1155 (set-syntax-table py-mode-syntax-table)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001156 ;; set up keybindings for this subshell
1157 (local-set-key [tab] 'self-insert-command)
1158 (local-set-key "\C-c-" 'py-up-exception)
1159 (local-set-key "\C-c=" 'py-down-exception)
1160 )
Barry Warsawa97a3f31997-11-04 18:47:06 +00001161
Barry Warsawa97a3f31997-11-04 18:47:06 +00001162(defun py-clear-queue ()
1163 "Clear the queue of temporary files waiting to execute."
1164 (interactive)
1165 (let ((n (length py-file-queue)))
1166 (mapcar 'delete-file py-file-queue)
1167 (setq py-file-queue nil)
1168 (message "%d pending files de-queued." n)))
1169
1170(defun py-execute-region (start end &optional async)
1171 "Execute the the region in a Python interpreter.
1172The region is first copied into a temporary file (in the directory
1173`py-temp-directory'). If there is no Python interpreter shell
1174running, this file is executed synchronously using
1175`shell-command-on-region'. If the program is long running, use an
1176optional \\[universal-argument] to run the command asynchronously in
1177its own buffer.
1178
1179If the Python interpreter shell is running, the region is execfile()'d
1180in that shell. If you try to execute regions too quickly,
1181`python-mode' will queue them up and execute them one at a time when
1182it sees a `>>> ' prompt from Python. Each time this happens, the
1183process buffer is popped into a window (if it's not already in some
1184window) so you can see it, and a comment of the form
1185
1186 \t## working on region in file <name>...
1187
1188is inserted at the end. See also the command `py-clear-queue'."
1189 (interactive "r\nP")
1190 (or (< start end)
1191 (error "Region is empty"))
1192 (let* ((proc (get-process "Python"))
Barry Warsaw5e21cb01997-11-05 18:41:11 +00001193 (temp (if (memq 'broken-temp-names py-emacs-features)
1194 (prog1
1195 (format "python-%d" py-serial-number)
1196 (setq py-serial-number (1+ py-serial-number)))
1197 (make-temp-name "python")))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001198 (file (concat (file-name-as-directory py-temp-directory) temp)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001199 (write-region start end file nil 'nomsg)
1200 (cond
1201 ;; always run the code in it's own asynchronous subprocess
1202 (async
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001203 (let* ((buf (generate-new-buffer-name py-output-buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001204 (start-process "Python" buf py-python-command "-u" file)
1205 (pop-to-buffer buf)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001206 (py-postprocess-output-buffer buf)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001207 ))
1208 ;; if the Python interpreter shell is running, queue it up for
1209 ;; execution there.
1210 (proc
1211 ;; use the existing python shell
1212 (if (not py-file-queue)
1213 (py-execute-file proc file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001214 (message "File %s queued for execution" file))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001215 (push file py-file-queue)
1216 (setq py-exception-buffer (cons file (current-buffer))))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001217 (t
1218 ;; otherwise either run it synchronously in a subprocess
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001219 (shell-command-on-region start end py-python-command py-output-buffer)
1220 (setq py-exception-buffer (current-buffer))
1221 (py-postprocess-output-buffer py-output-buffer)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001222 ))))
1223
1224;; Code execution command
1225(defun py-execute-buffer (&optional async)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001226 "Send the contents of the buffer to a Python interpreter.
Barry Warsawc0ecb531998-01-20 21:43:34 +00001227If the file local variable `py-master-file' is non-nil, execute the
1228named file instead of the buffer's file.
1229
Barry Warsaw7ae77681994-12-12 20:38:05 +00001230If there is a *Python* process buffer it is used. If a clipping
1231restriction is in effect, only the accessible portion of the buffer is
1232sent. A trailing newline will be supplied if needed.
1233
1234See the `\\[py-execute-region]' docs for an account of some subtleties."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001235 (interactive "P")
Barry Warsawc0ecb531998-01-20 21:43:34 +00001236 (if py-master-file
1237 (let* ((filename (expand-file-name py-master-file))
1238 (buffer (or (get-file-buffer filename)
1239 (find-file-noselect filename))))
1240 (set-buffer buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001241 (py-execute-region (point-min) (point-max) async))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001242
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001243
1244
1245(defun py-jump-to-exception (file line)
1246 (let ((buffer (cond ((string-equal file "<stdin>")
1247 py-exception-buffer)
1248 ((and (consp py-exception-buffer)
1249 (string-equal file (car py-exception-buffer)))
1250 (cdr py-exception-buffer))
1251 ((py-safe (find-file-noselect file)))
1252 ;; could not figure out what file the exception
1253 ;; is pointing to, so prompt for it
1254 (t (find-file (read-file-name "Exception file: "
1255 nil
1256 file t))))))
1257 (pop-to-buffer buffer)
1258 (goto-line line)
1259 (message "Jumping to exception in file %s on line %d" file line)))
1260
1261(defun py-mouseto-exception (event)
1262 (interactive "e")
1263 (cond
1264 ((fboundp 'event-point)
1265 ;; XEmacs
1266 (let* ((point (event-point event))
1267 (buffer (event-buffer event))
1268 (e (and point buffer (extent-at point buffer 'py-exc-info)))
1269 (info (and e (extent-property e 'py-exc-info))))
1270 (message "Event point: %d, info: %s" point info)
1271 (and info
1272 (py-jump-to-exception (car info) (cdr info)))
1273 ))
1274 ;; Emacs -- Please port this!
1275 ))
1276
1277(defun py-goto-exception ()
1278 "Go to the line indicated by the traceback."
1279 (interactive)
1280 (let (file line)
1281 (save-excursion
1282 (beginning-of-line)
1283 (if (looking-at py-traceback-line-re)
1284 (setq file (match-string 1)
1285 line (string-to-int (match-string 2)))))
1286 (if (not file)
1287 (error "Not on a traceback line."))
1288 (py-jump-to-exception file line)))
1289
1290(defun py-find-next-exception (start buffer searchdir errwhere)
1291 ;; Go to start position in buffer, search in the specified
1292 ;; direction, and jump to the exception found. If at the end of the
1293 ;; exception, print error message
1294 (let (file line)
1295 (save-excursion
1296 (set-buffer buffer)
1297 (goto-char (py-point start))
1298 (if (funcall searchdir py-traceback-line-re nil t)
1299 (setq file (match-string 1)
1300 line (string-to-int (match-string 2)))))
1301 (if (and file line)
1302 (py-jump-to-exception file line)
1303 (error "%s of traceback" errwhere))))
1304
1305(defun py-down-exception (&optional bottom)
1306 "Go to the next line down in the traceback.
1307With optional \\[universal-argument], jump to the bottom (innermost)
1308exception in the exception stack."
1309 (interactive "P")
1310 (let* ((proc (get-process "Python"))
1311 (buffer (if proc "*Python*" py-output-buffer)))
1312 (if bottom
1313 (py-find-next-exception 'eob buffer 're-search-backward "Bottom")
1314 (py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
1315
1316(defun py-up-exception (&optional top)
1317 "Go to the previous line up in the traceback.
1318With optional \\[universal-argument], jump to the top (outermost)
1319exception in the exception stack."
1320 (interactive "P")
1321 (let* ((proc (get-process "Python"))
1322 (buffer (if proc "*Python*" py-output-buffer)))
1323 (if top
1324 (py-find-next-exception 'bob buffer 're-search-forward "Top")
Barry Warsawffbc17d1997-11-27 20:08:14 +00001325 (py-find-next-exception 'bol buffer 're-search-backward "Top"))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001326
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001327
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001328;; Electric deletion
1329(defun py-electric-backspace (arg)
1330 "Deletes preceding character or levels of indentation.
1331Deletion is performed by calling the function in `py-backspace-function'
Barry Warsawb0539931996-12-17 22:05:07 +00001332with a single argument (the number of characters to delete).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001333
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001334If point is at the leftmost column, deletes the preceding newline.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001335
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001336Otherwise, if point is at the leftmost non-whitespace character of a
1337line that is neither a continuation line nor a non-indenting comment
1338line, or if point is at the end of a blank line, this command reduces
1339the indentation to match that of the line that opened the current
1340block of code. The line that opened the block is displayed in the
1341echo area to help you keep track of where you are. With numeric arg,
1342outdents that many blocks (but not past column zero).
1343
1344Otherwise the preceding character is deleted, converting a tab to
1345spaces if needed so that only a single column position is deleted.
1346Numeric argument deletes that many preceding characters."
Barry Warsaw03970731996-07-03 23:12:52 +00001347 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +00001348 (if (or (/= (current-indentation) (current-column))
1349 (bolp)
1350 (py-continuation-line-p)
Barry Warsaw6e527d21996-08-01 15:57:48 +00001351 (not py-honor-comment-indentation)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001352 (looking-at "#[^ \t\n]")) ; non-indenting #
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001353 (funcall py-backspace-function arg)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001354 ;; else indent the same as the colon line that opened the block
Barry Warsaw7ae77681994-12-12 20:38:05 +00001355 ;; force non-blank so py-goto-block-up doesn't ignore it
1356 (insert-char ?* 1)
1357 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001358 (let ((base-indent 0) ; indentation of base line
1359 (base-text "") ; and text of base line
1360 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +00001361 (save-excursion
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001362 (while (< 0 arg)
Barry Warsaw03970731996-07-03 23:12:52 +00001363 (condition-case nil ; in case no enclosing block
1364 (progn
1365 (py-goto-block-up 'no-mark)
1366 (setq base-indent (current-indentation)
1367 base-text (py-suck-up-leading-text)
1368 base-found-p t))
1369 (error nil))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001370 (setq arg (1- arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001371 (delete-char 1) ; toss the dummy character
1372 (delete-horizontal-space)
1373 (indent-to base-indent)
1374 (if base-found-p
1375 (message "Closes block: %s" base-text)))))
1376
Barry Warsawfc8a01f1995-03-09 16:07:29 +00001377
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001378(defun py-electric-delete (arg)
1379 "Deletes preceding or following character or levels of whitespace.
1380
1381The behavior of this function depends on the variable
1382`delete-key-deletes-forward'. If this variable is nil (or does not
1383exist, as in older Emacsen), then this function behaves identical to
1384\\[c-electric-backspace].
1385
1386If `delete-key-deletes-forward' is non-nil and is supported in your
1387Emacs, then deletion occurs in the forward direction, by calling the
1388function in `py-delete-function'."
1389 (interactive "*p")
1390 (if (and (boundp 'delete-key-deletes-forward)
1391 delete-key-deletes-forward)
1392 (funcall py-delete-function arg)
1393 ;; else
1394 (py-electric-backspace arg)))
1395
1396;; required for pending-del and delsel modes
1397(put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
1398(put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del
1399(put 'py-electric-delete 'delete-selection 'supersede) ;delsel
1400(put 'py-electric-delete 'pending-delete 'supersede) ;pending-del
1401
1402
1403
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001404(defun py-indent-line (&optional arg)
1405 "Fix the indentation of the current line according to Python rules.
1406With \\[universal-argument], ignore outdenting rules for block
1407closing statements (e.g. return, raise, break, continue, pass)
1408
1409This function is normally bound to `indent-line-function' so
1410\\[indent-for-tab-command] will call it."
1411 (interactive "P")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001412 (let* ((ci (current-indentation))
1413 (move-to-indentation-p (<= (current-column) ci))
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001414 (need (py-compute-indentation (not arg))))
Barry Warsaw4f009fb1995-03-14 20:53:08 +00001415 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001416 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +00001417 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001418 (if (/= ci need)
1419 (save-excursion
1420 (beginning-of-line)
1421 (delete-horizontal-space)
1422 (indent-to need)))
1423 (if move-to-indentation-p (back-to-indentation))))
1424
1425(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001426 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001427This is just `strives to' because correct indentation can't be computed
1428from scratch for Python code. In general, deletes the whitespace before
1429point, inserts a newline, and takes an educated guess as to how you want
1430the new line indented."
1431 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001432 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001433 (if (< ci (current-column)) ; if point beyond indentation
1434 (newline-and-indent)
1435 ;; else try to act like newline-and-indent "normally" acts
1436 (beginning-of-line)
1437 (insert-char ?\n 1)
1438 (move-to-column ci))))
1439
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001440(defun py-compute-indentation (honor-block-close-p)
1441 ;; implements all the rules for indentation computation. when
1442 ;; honor-block-close-p is non-nil, statements such as return, raise,
1443 ;; break, continue, and pass force one level of outdenting.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001444 (save-excursion
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001445 (let* ((bod (py-point 'bod))
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001446 (pps (parse-partial-sexp bod (point)))
1447 (boipps (parse-partial-sexp bod (py-point 'boi))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001448 (beginning-of-line)
1449 (cond
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001450 ;; are we inside a multi-line string or comment?
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001451 ((or (and (nth 3 pps) (nth 3 boipps))
1452 (and (nth 4 pps) (nth 4 boipps)))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001453 (save-excursion
1454 (if (not py-align-multiline-strings-p) 0
1455 ;; skip back over blank & non-indenting comment lines
1456 ;; note: will skip a blank or non-indenting comment line
1457 ;; that happens to be a continuation line too
1458 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1459 (back-to-indentation)
1460 (current-column))))
1461 ;; are we on a continuation line?
1462 ((py-continuation-line-p)
1463 (let ((startpos (point))
1464 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001465 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001466 (if open-bracket-pos
1467 (progn
1468 ;; align with first item in list; else a normal
1469 ;; indent beyond the line with the open bracket
1470 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1471 ;; is the first list item on the same line?
1472 (skip-chars-forward " \t")
1473 (if (null (memq (following-char) '(?\n ?# ?\\)))
1474 ; yes, so line up with it
1475 (current-column)
1476 ;; first list item on another line, or doesn't exist yet
1477 (forward-line 1)
1478 (while (and (< (point) startpos)
1479 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1480 (forward-line 1))
1481 (if (< (point) startpos)
1482 ;; again mimic the first list item
1483 (current-indentation)
1484 ;; else they're about to enter the first item
1485 (goto-char open-bracket-pos)
1486 (+ (current-indentation) py-indent-offset))))
1487
1488 ;; else on backslash continuation line
1489 (forward-line -1)
1490 (if (py-continuation-line-p) ; on at least 3rd line in block
1491 (current-indentation) ; so just continue the pattern
1492 ;; else started on 2nd line in block, so indent more.
1493 ;; if base line is an assignment with a start on a RHS,
1494 ;; indent to 2 beyond the leftmost "="; else skip first
1495 ;; chunk of non-whitespace characters on base line, + 1 more
1496 ;; column
1497 (end-of-line)
1498 (setq endpos (point) searching t)
1499 (back-to-indentation)
1500 (setq startpos (point))
1501 ;; look at all "=" from left to right, stopping at first
1502 ;; one not nested in a list or string
1503 (while searching
1504 (skip-chars-forward "^=" endpos)
1505 (if (= (point) endpos)
1506 (setq searching nil)
1507 (forward-char 1)
1508 (setq state (parse-partial-sexp startpos (point)))
1509 (if (and (zerop (car state)) ; not in a bracket
1510 (null (nth 3 state))) ; & not in a string
1511 (progn
1512 (setq searching nil) ; done searching in any case
1513 (setq found
1514 (not (or
1515 (eq (following-char) ?=)
1516 (memq (char-after (- (point) 2))
1517 '(?< ?> ?!)))))))))
1518 (if (or (not found) ; not an assignment
1519 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1520 (progn
1521 (goto-char startpos)
1522 (skip-chars-forward "^ \t\n")))
1523 (1+ (current-column))))))
1524
1525 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001526 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001527
Barry Warsawa7891711996-08-01 15:53:09 +00001528 ;; Dfn: "Indenting comment line". A line containing only a
1529 ;; comment, but which is treated like a statement for
1530 ;; indentation calculation purposes. Such lines are only
1531 ;; treated specially by the mode; they are not treated
1532 ;; specially by the Python interpreter.
1533
1534 ;; The rules for indenting comment lines are a line where:
1535 ;; - the first non-whitespace character is `#', and
1536 ;; - the character following the `#' is whitespace, and
1537 ;; - the line is outdented with respect to (i.e. to the left
1538 ;; of) the indentation of the preceding non-blank line.
1539
1540 ;; The first non-blank line following an indenting comment
1541 ;; line is given the same amount of indentation as the
1542 ;; indenting comment line.
1543
1544 ;; All other comment-only lines are ignored for indentation
1545 ;; purposes.
1546
1547 ;; Are we looking at a comment-only line which is *not* an
1548 ;; indenting comment line? If so, we assume that its been
1549 ;; placed at the desired indentation, so leave it alone.
1550 ;; Indenting comment lines are aligned as statements down
1551 ;; below.
1552 ((and (looking-at "[ \t]*#[^ \t\n]")
1553 ;; NOTE: this test will not be performed in older Emacsen
1554 (fboundp 'forward-comment)
1555 (<= (current-indentation)
1556 (save-excursion
1557 (forward-comment (- (point-max)))
1558 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001559 (current-indentation))
1560
1561 ;; else indentation based on that of the statement that
1562 ;; precedes us; use the first line of that statement to
1563 ;; establish the base, in case the user forced a non-std
1564 ;; indentation for the continuation lines (if any)
1565 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001566 ;; skip back over blank & non-indenting comment lines note:
1567 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001568 ;; happens to be a continuation line too. use fast Emacs 19
1569 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001570 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001571 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001572 (forward-comment (- (point-max)))
Barry Warsaw6d627751996-03-06 18:41:38 +00001573 (let (done)
1574 (while (not done)
1575 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
1576 nil 'move)
1577 (setq done (or (eq py-honor-comment-indentation t)
1578 (bobp)
1579 (/= (following-char) ?#)
1580 (not (zerop (current-column)))))
1581 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001582 ;; if we landed inside a string, go to the beginning of that
1583 ;; string. this handles triple quoted, multi-line spanning
1584 ;; strings.
Barry Warsawc210e691998-01-20 22:52:56 +00001585 (let ((skip (nth 3 (parse-partial-sexp bod (point)))))
1586 (while skip
1587 (py-safe (search-backward (make-string 1 skip)))
1588 (setq skip (nth 3 (parse-partial-sexp bod (point))))))
1589 ;; now skip backward over continued lines
Barry Warsaw095e9c61995-09-19 20:01:42 +00001590 (py-goto-initial-line)
Barry Warsawf831d811996-07-31 20:42:59 +00001591 (+ (current-indentation)
1592 (if (py-statement-opens-block-p)
1593 py-indent-offset
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001594 (if (and honor-block-close-p (py-statement-closes-block-p))
Barry Warsawf831d811996-07-31 20:42:59 +00001595 (- py-indent-offset)
1596 0)))
1597 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001598
1599(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001600 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001601By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001602`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +00001603Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001604`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +00001605their own buffer-local copy), both those currently existing and those
1606created later in the Emacs session.
1607
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001608Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001609There's no excuse for such foolishness, but sometimes you have to deal
1610with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001611`py-indent-offset' to what it thinks it was when they created the
1612mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001613
1614Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001615looking for a line that opens a block of code. `py-indent-offset' is
1616set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001617statement following it. If the search doesn't succeed going forward,
1618it's tried again going backward."
1619 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001620 (let (new-value
1621 (start (point))
1622 restart
1623 (found nil)
1624 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001625 (py-goto-initial-line)
1626 (while (not (or found (eobp)))
1627 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1628 (progn
1629 (setq restart (point))
1630 (py-goto-initial-line)
1631 (if (py-statement-opens-block-p)
1632 (setq found t)
1633 (goto-char restart)))))
1634 (if found
1635 ()
1636 (goto-char start)
1637 (py-goto-initial-line)
1638 (while (not (or found (bobp)))
1639 (setq found
1640 (and
1641 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1642 (or (py-goto-initial-line) t) ; always true -- side effect
1643 (py-statement-opens-block-p)))))
1644 (setq colon-indent (current-indentation)
1645 found (and found (zerop (py-next-statement 1)))
1646 new-value (- (current-indentation) colon-indent))
1647 (goto-char start)
1648 (if found
1649 (progn
1650 (funcall (if global 'kill-local-variable 'make-local-variable)
1651 'py-indent-offset)
1652 (setq py-indent-offset new-value)
1653 (message "%s value of py-indent-offset set to %d"
1654 (if global "Global" "Local")
1655 py-indent-offset))
1656 (error "Sorry, couldn't guess a value for py-indent-offset"))))
1657
1658(defun py-shift-region (start end count)
1659 (save-excursion
1660 (goto-char end) (beginning-of-line) (setq end (point))
1661 (goto-char start) (beginning-of-line) (setq start (point))
1662 (indent-rigidly start end count)))
1663
1664(defun py-shift-region-left (start end &optional count)
1665 "Shift region of Python code to the left.
1666The lines from the line containing the start of the current region up
1667to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001668shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001669
1670If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001671many columns. With no active region, outdent only the current line.
1672You cannot outdent the region if any line is already at column zero."
1673 (interactive
1674 (let ((p (point))
1675 (m (mark))
1676 (arg current-prefix-arg))
1677 (if m
1678 (list (min p m) (max p m) arg)
1679 (list p (save-excursion (forward-line 1) (point)) arg))))
1680 ;; if any line is at column zero, don't shift the region
1681 (save-excursion
1682 (goto-char start)
1683 (while (< (point) end)
1684 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00001685 (if (and (zerop (current-column))
1686 (not (looking-at "\\s *$")))
Barry Warsawdea4a291996-07-03 22:59:12 +00001687 (error "Region is at left edge."))
1688 (forward-line 1)))
1689 (py-shift-region start end (- (prefix-numeric-value
1690 (or count py-indent-offset))))
1691 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001692
1693(defun py-shift-region-right (start end &optional count)
1694 "Shift region of Python code to the right.
1695The lines from the line containing the start of the current region up
1696to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001697shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001698
1699If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001700many columns. With no active region, indent only the current line."
1701 (interactive
1702 (let ((p (point))
1703 (m (mark))
1704 (arg current-prefix-arg))
1705 (if m
1706 (list (min p m) (max p m) arg)
1707 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001708 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00001709 (or count py-indent-offset)))
1710 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001711
1712(defun py-indent-region (start end &optional indent-offset)
1713 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00001714
Barry Warsaw7ae77681994-12-12 20:38:05 +00001715The lines from the line containing the start of the current region up
1716to (but not including) the line containing the end of the region are
1717reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001718character in the first column, the first line is left alone and the
1719rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00001720region is reindented with respect to the (closest code or indenting
1721comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001722
1723This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001724control structures are introduced or removed, or to reformat code
1725using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001726
1727If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001728the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00001729used.
1730
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001731Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00001732is called! This function does not compute proper indentation from
1733scratch (that's impossible in Python), it merely adjusts the existing
1734indentation to be correct in context.
1735
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001736Warning: This function really has no idea what to do with
1737non-indenting comment lines, and shifts them as if they were indenting
1738comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001739
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001740Special cases: whitespace is deleted from blank lines; continuation
1741lines are shifted by the same amount their initial line was shifted,
1742in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00001743initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001744 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001745 (save-excursion
1746 (goto-char end) (beginning-of-line) (setq end (point-marker))
1747 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001748 (let ((py-indent-offset (prefix-numeric-value
1749 (or indent-offset py-indent-offset)))
1750 (indents '(-1)) ; stack of active indent levels
1751 (target-column 0) ; column to which to indent
1752 (base-shifted-by 0) ; amount last base line was shifted
1753 (indent-base (if (looking-at "[ \t\n]")
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001754 (py-compute-indentation t)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001755 0))
1756 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001757 (while (< (point) end)
1758 (setq ci (current-indentation))
1759 ;; figure out appropriate target column
1760 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001761 ((or (eq (following-char) ?#) ; comment in column 1
1762 (looking-at "[ \t]*$")) ; entirely blank
1763 (setq target-column 0))
1764 ((py-continuation-line-p) ; shift relative to base line
1765 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001766 (t ; new base line
1767 (if (> ci (car indents)) ; going deeper; push it
1768 (setq indents (cons ci indents))
1769 ;; else we should have seen this indent before
1770 (setq indents (memq ci indents)) ; pop deeper indents
1771 (if (null indents)
1772 (error "Bad indentation in region, at line %d"
1773 (save-restriction
1774 (widen)
1775 (1+ (count-lines 1 (point)))))))
1776 (setq target-column (+ indent-base
1777 (* py-indent-offset
1778 (- (length indents) 2))))
1779 (setq base-shifted-by (- target-column ci))))
1780 ;; shift as needed
1781 (if (/= ci target-column)
1782 (progn
1783 (delete-horizontal-space)
1784 (indent-to target-column)))
1785 (forward-line 1))))
1786 (set-marker end nil))
1787
Barry Warsawa7891711996-08-01 15:53:09 +00001788(defun py-comment-region (beg end &optional arg)
1789 "Like `comment-region' but uses double hash (`#') comment starter."
1790 (interactive "r\nP")
Barry Warsaw3fcaf611996-08-01 20:11:51 +00001791 (let ((comment-start py-block-comment-prefix))
Barry Warsawa7891711996-08-01 15:53:09 +00001792 (comment-region beg end arg)))
1793
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001794
1795;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00001796(defun py-previous-statement (count)
1797 "Go to the start of previous Python statement.
1798If the statement at point is the i'th Python statement, goes to the
1799start of statement i-COUNT. If there is no such statement, goes to the
1800first statement. Returns count of statements left to move.
1801`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001802 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001803 (if (< count 0) (py-next-statement (- count))
1804 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001805 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001806 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001807 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001808 (> count 0)
1809 (zerop (forward-line -1))
1810 (py-goto-statement-at-or-above))
1811 (setq count (1- count)))
1812 (if (> count 0) (goto-char start)))
1813 count))
1814
1815(defun py-next-statement (count)
1816 "Go to the start of next Python statement.
1817If the statement at point is the i'th Python statement, goes to the
1818start of statement i+COUNT. If there is no such statement, goes to the
1819last statement. Returns count of statements left to move. `Statements'
1820do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001821 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001822 (if (< count 0) (py-previous-statement (- count))
1823 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001824 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001825 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001826 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001827 (> count 0)
1828 (py-goto-statement-below))
1829 (setq count (1- count)))
1830 (if (> count 0) (goto-char start)))
1831 count))
1832
1833(defun py-goto-block-up (&optional nomark)
1834 "Move up to start of current block.
1835Go to the statement that starts the smallest enclosing block; roughly
1836speaking, this will be the closest preceding statement that ends with a
1837colon and is indented less than the statement you started on. If
1838successful, also sets the mark to the starting point.
1839
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001840`\\[py-mark-block]' can be used afterward to mark the whole code
1841block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001842
1843If called from a program, the mark will not be set if optional argument
1844NOMARK is not nil."
1845 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001846 (let ((start (point))
1847 (found nil)
1848 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001849 (py-goto-initial-line)
1850 ;; if on blank or non-indenting comment line, use the preceding stmt
1851 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1852 (progn
1853 (py-goto-statement-at-or-above)
1854 (setq found (py-statement-opens-block-p))))
1855 ;; search back for colon line indented less
1856 (setq initial-indent (current-indentation))
1857 (if (zerop initial-indent)
1858 ;; force fast exit
1859 (goto-char (point-min)))
1860 (while (not (or found (bobp)))
1861 (setq found
1862 (and
1863 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1864 (or (py-goto-initial-line) t) ; always true -- side effect
1865 (< (current-indentation) initial-indent)
1866 (py-statement-opens-block-p))))
1867 (if found
1868 (progn
1869 (or nomark (push-mark start))
1870 (back-to-indentation))
1871 (goto-char start)
1872 (error "Enclosing block not found"))))
1873
1874(defun beginning-of-python-def-or-class (&optional class)
1875 "Move point to start of def (or class, with prefix arg).
1876
1877Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001878arg, looks for a `class' instead. The docs assume the `def' case;
1879just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001880
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001881If point is in a def statement already, and after the `d', simply
1882moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001883
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001884Else (point is not in a def statement, or at or before the `d' of a
1885def statement), searches for the closest preceding def statement, and
1886leaves point at its start. If no such statement can be found, leaves
1887point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001888
1889Returns t iff a def statement is found by these rules.
1890
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001891Note that doing this command repeatedly will take you closer to the
1892start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001893
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001894If you want to mark the current def/class, see
Barry Warsaw2518c671997-11-05 00:51:08 +00001895`\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001896 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001897 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1898 (start-of-line (progn (beginning-of-line) (point)))
1899 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001900 (if (or (/= start-of-stmt start-of-line)
1901 (not at-or-before-p))
1902 (end-of-line)) ; OK to match on this line
1903 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001904 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001905
1906(defun end-of-python-def-or-class (&optional class)
1907 "Move point beyond end of def (or class, with prefix arg) body.
1908
1909By default, looks for an appropriate `def'. If you supply a prefix arg,
1910looks for a `class' instead. The docs assume the `def' case; just
1911substitute `class' for `def' for the other case.
1912
1913If point is in a def statement already, this is the def we use.
1914
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001915Else if the def found by `\\[beginning-of-python-def-or-class]'
1916contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001917
1918Else we search forward for the closest following def, and use that.
1919
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001920If a def can be found by these rules, point is moved to the start of
1921the line immediately following the def block, and the position of the
1922start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001923
1924Else point is moved to the end of the buffer, and nil is returned.
1925
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001926Note that doing this command repeatedly will take you closer to the
1927end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001928
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001929If you want to mark the current def/class, see
Barry Warsaw2518c671997-11-05 00:51:08 +00001930`\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001931 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001932 (let ((start (progn (py-goto-initial-line) (point)))
1933 (which (if class "class" "def"))
1934 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001935 ;; move point to start of appropriate def/class
1936 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1937 (setq state 'at-beginning)
1938 ;; else see if beginning-of-python-def-or-class hits container
1939 (if (and (beginning-of-python-def-or-class class)
1940 (progn (py-goto-beyond-block)
1941 (> (point) start)))
1942 (setq state 'at-end)
1943 ;; else search forward
1944 (goto-char start)
1945 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1946 (progn (setq state 'at-beginning)
1947 (beginning-of-line)))))
1948 (cond
1949 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1950 ((eq state 'at-end) t)
1951 ((eq state 'not-found) nil)
1952 (t (error "internal error in end-of-python-def-or-class")))))
1953
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001954
1955;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001956(defun py-mark-block (&optional extend just-move)
1957 "Mark following block of lines. With prefix arg, mark structure.
1958Easier to use than explain. It sets the region to an `interesting'
1959block of succeeding lines. If point is on a blank line, it goes down to
1960the next non-blank line. That will be the start of the region. The end
1961of the region depends on the kind of line at the start:
1962
1963 - If a comment, the region will include all succeeding comment lines up
1964 to (but not including) the next non-comment line (if any).
1965
1966 - Else if a prefix arg is given, and the line begins one of these
1967 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001968
1969 if elif else try except finally for while def class
1970
Barry Warsaw7ae77681994-12-12 20:38:05 +00001971 the region will be set to the body of the structure, including
1972 following blocks that `belong' to it, but excluding trailing blank
1973 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001974 and all (if any) of the following `except' and `finally' blocks
1975 that belong to the `try' structure will be in the region. Ditto
1976 for if/elif/else, for/else and while/else structures, and (a bit
1977 degenerate, since they're always one-block structures) def and
1978 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001979
1980 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001981 block (see list above), and the block is not a `one-liner' (i.e.,
1982 the statement ends with a colon, not with code), the region will
1983 include all succeeding lines up to (but not including) the next
1984 code statement (if any) that's indented no more than the starting
1985 line, except that trailing blank and comment lines are excluded.
1986 E.g., if the starting line begins a multi-statement `def'
1987 structure, the region will be set to the full function definition,
1988 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001989
1990 - Else the region will include all succeeding lines up to (but not
1991 including) the next blank line, or code or indenting-comment line
1992 indented strictly less than the starting line. Trailing indenting
1993 comment lines are included in this case, but not trailing blank
1994 lines.
1995
1996A msg identifying the location of the mark is displayed in the echo
1997area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1998
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001999If called from a program, optional argument EXTEND plays the role of
2000the prefix arg, and if optional argument JUST-MOVE is not nil, just
2001moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002002 (interactive "P") ; raw prefix arg
2003 (py-goto-initial-line)
2004 ;; skip over blank lines
2005 (while (and
2006 (looking-at "[ \t]*$") ; while blank line
2007 (not (eobp))) ; & somewhere to go
2008 (forward-line 1))
2009 (if (eobp)
2010 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002011 (let ((initial-pos (point))
2012 (initial-indent (current-indentation))
2013 last-pos ; position of last stmt in region
2014 (followers
2015 '((if elif else) (elif elif else) (else)
2016 (try except finally) (except except) (finally)
2017 (for else) (while else)
2018 (def) (class) ) )
2019 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002020
2021 (cond
2022 ;; if comment line, suck up the following comment lines
2023 ((looking-at "[ \t]*#")
2024 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
2025 (re-search-backward "^[ \t]*#") ; and back to last comment in block
2026 (setq last-pos (point)))
2027
2028 ;; else if line is a block line and EXTEND given, suck up
2029 ;; the whole structure
2030 ((and extend
2031 (setq first-symbol (py-suck-up-first-keyword) )
2032 (assq first-symbol followers))
2033 (while (and
2034 (or (py-goto-beyond-block) t) ; side effect
2035 (forward-line -1) ; side effect
2036 (setq last-pos (point)) ; side effect
2037 (py-goto-statement-below)
2038 (= (current-indentation) initial-indent)
2039 (setq next-symbol (py-suck-up-first-keyword))
2040 (memq next-symbol (cdr (assq first-symbol followers))))
2041 (setq first-symbol next-symbol)))
2042
2043 ;; else if line *opens* a block, search for next stmt indented <=
2044 ((py-statement-opens-block-p)
2045 (while (and
2046 (setq last-pos (point)) ; always true -- side effect
2047 (py-goto-statement-below)
2048 (> (current-indentation) initial-indent))
2049 nil))
2050
2051 ;; else plain code line; stop at next blank line, or stmt or
2052 ;; indenting comment line indented <
2053 (t
2054 (while (and
2055 (setq last-pos (point)) ; always true -- side effect
2056 (or (py-goto-beyond-final-line) t)
2057 (not (looking-at "[ \t]*$")) ; stop at blank line
2058 (or
2059 (>= (current-indentation) initial-indent)
2060 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
2061 nil)))
2062
2063 ;; skip to end of last stmt
2064 (goto-char last-pos)
2065 (py-goto-beyond-final-line)
2066
2067 ;; set mark & display
2068 (if just-move
2069 () ; just return
2070 (push-mark (point) 'no-msg)
2071 (forward-line -1)
2072 (message "Mark set after: %s" (py-suck-up-leading-text))
2073 (goto-char initial-pos))))
2074
Barry Warsaw2518c671997-11-05 00:51:08 +00002075(defun py-mark-def-or-class (&optional class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002076 "Set region to body of def (or class, with prefix arg) enclosing point.
2077Pushes the current mark, then point, on the mark ring (all language
2078modes do this, but although it's handy it's never documented ...).
2079
2080In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002081hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
2082`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002083
2084And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002085Turned out that was more confusing than useful: the `goto start' and
2086`goto end' commands are usually used to search through a file, and
2087people expect them to act a lot like `search backward' and `search
2088forward' string-search commands. But because Python `def' and `class'
2089can nest to arbitrary levels, finding the smallest def containing
2090point cannot be done via a simple backward search: the def containing
2091point may not be the closest preceding def, or even the closest
2092preceding def that's indented less. The fancy algorithm required is
2093appropriate for the usual uses of this `mark' command, but not for the
2094`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002095
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002096So the def marked by this command may not be the one either of the
2097`goto' commands find: If point is on a blank or non-indenting comment
2098line, moves back to start of the closest preceding code statement or
2099indenting comment line. If this is a `def' statement, that's the def
2100we use. Else searches for the smallest enclosing `def' block and uses
2101that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002102
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002103When an enclosing def is found: The mark is left immediately beyond
2104the last line of the def block. Point is left at the start of the
2105def, except that: if the def is preceded by a number of comment lines
2106followed by (at most) one optional blank line, point is left at the
2107start of the comments; else if the def is preceded by a blank line,
2108point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002109
2110The intent is to mark the containing def/class and its associated
2111documentation, to make moving and duplicating functions and classes
2112pleasant."
2113 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002114 (let ((start (point))
2115 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002116 (push-mark start)
2117 (if (not (py-go-up-tree-to-keyword which))
2118 (progn (goto-char start)
2119 (error "Enclosing %s not found" which))
2120 ;; else enclosing def/class found
2121 (setq start (point))
2122 (py-goto-beyond-block)
2123 (push-mark (point))
2124 (goto-char start)
2125 (if (zerop (forward-line -1)) ; if there is a preceding line
2126 (progn
2127 (if (looking-at "[ \t]*$") ; it's blank
2128 (setq start (point)) ; so reset start point
2129 (goto-char start)) ; else try again
2130 (if (zerop (forward-line -1))
2131 (if (looking-at "[ \t]*#") ; a comment
2132 ;; look back for non-comment line
2133 ;; tricky: note that the regexp matches a blank
2134 ;; line, cuz \n is in the 2nd character class
2135 (and
2136 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
2137 (forward-line 1))
2138 ;; no comment, so go back
Barry Warsaw4da6bd51997-11-26 06:00:26 +00002139 (goto-char start)))))))
2140 (exchange-point-and-mark)
2141 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002142
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002143;; ripped from cc-mode
2144(defun py-forward-into-nomenclature (&optional arg)
2145 "Move forward to end of a nomenclature section or word.
2146With arg, to it arg times.
2147
2148A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2149 (interactive "p")
2150 (let ((case-fold-search nil))
2151 (if (> arg 0)
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002152 (re-search-forward
2153 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
2154 (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002155 (while (and (< arg 0)
2156 (re-search-backward
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002157 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002158 (point-min) 0))
2159 (forward-char 1)
2160 (setq arg (1+ arg)))))
2161 (py-keep-region-active))
2162
2163(defun py-backward-into-nomenclature (&optional arg)
2164 "Move backward to beginning of a nomenclature section or word.
2165With optional ARG, move that many times. If ARG is negative, move
2166forward.
2167
2168A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2169 (interactive "p")
2170 (py-forward-into-nomenclature (- arg))
2171 (py-keep-region-active))
2172
2173
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002174
2175;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002176
2177;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002178;; plus lines of the form ^[vc]:name$ to suck variable & command docs
2179;; out of the right places, along with the keys they're on & current
2180;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00002181(defun py-dump-help-string (str)
2182 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002183 (let ((locals (buffer-local-variables))
2184 funckind funcname func funcdoc
2185 (start 0) mstart end
2186 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002187 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
2188 (setq mstart (match-beginning 0) end (match-end 0)
2189 funckind (substring str (match-beginning 1) (match-end 1))
2190 funcname (substring str (match-beginning 2) (match-end 2))
2191 func (intern funcname))
2192 (princ (substitute-command-keys (substring str start mstart)))
2193 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002194 ((equal funckind "c") ; command
2195 (setq funcdoc (documentation func)
2196 keys (concat
2197 "Key(s): "
2198 (mapconcat 'key-description
2199 (where-is-internal func py-mode-map)
2200 ", "))))
2201 ((equal funckind "v") ; variable
Barry Warsaw604cefa1996-09-03 18:17:04 +00002202 (setq funcdoc (documentation-property func 'variable-documentation)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002203 keys (if (assq func locals)
2204 (concat
2205 "Local/Global values: "
2206 (prin1-to-string (symbol-value func))
2207 " / "
2208 (prin1-to-string (default-value func)))
2209 (concat
2210 "Value: "
2211 (prin1-to-string (symbol-value func))))))
2212 (t ; unexpected
2213 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002214 (princ (format "\n-> %s:\t%s\t%s\n\n"
2215 (if (equal funckind "c") "Command" "Variable")
2216 funcname keys))
2217 (princ funcdoc)
2218 (terpri)
2219 (setq start end))
2220 (princ (substitute-command-keys (substring str start))))
2221 (print-help-return-message)))
2222
2223(defun py-describe-mode ()
2224 "Dump long form of Python-mode docs."
2225 (interactive)
2226 (py-dump-help-string "Major mode for editing Python files.
2227Knows about Python indentation, tokens, comments and continuation lines.
2228Paragraphs are separated by blank lines only.
2229
2230Major sections below begin with the string `@'; specific function and
2231variable docs begin with `->'.
2232
2233@EXECUTING PYTHON CODE
2234
2235\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
2236\\[py-execute-region]\tsends the current region
2237\\[py-shell]\tstarts a Python interpreter window; this will be used by
2238\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
2239%c:py-execute-buffer
2240%c:py-execute-region
2241%c:py-shell
2242
2243@VARIABLES
2244
2245py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00002246py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002247
2248py-python-command\tshell command to invoke Python interpreter
2249py-scroll-process-buffer\talways scroll Python process buffer
2250py-temp-directory\tdirectory used for temp files (if needed)
2251
2252py-beep-if-tab-change\tring the bell if tab-width is changed
2253%v:py-indent-offset
2254%v:py-block-comment-prefix
2255%v:py-python-command
2256%v:py-scroll-process-buffer
2257%v:py-temp-directory
2258%v:py-beep-if-tab-change
2259
2260@KINDS OF LINES
2261
2262Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002263preceding line ends with a backslash that's not part of a comment, or
2264the paren/bracket/brace nesting level at the start of the line is
2265non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002266
2267An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002268possibly blanks or tabs), a `comment line' (leftmost non-blank
2269character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002270
2271Comment Lines
2272
2273Although all comment lines are treated alike by Python, Python mode
2274recognizes two kinds that act differently with respect to indentation.
2275
2276An `indenting comment line' is a comment line with a blank, tab or
2277nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002278treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00002279indenting comment line will be indented like the comment line. All
2280other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002281following the initial `#') are `non-indenting comment lines', and
2282their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002283
2284Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002285whenever possible. Non-indenting comment lines are useful in cases
2286like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00002287
2288\ta = b # a very wordy single-line comment that ends up being
2289\t #... continued onto another line
2290
2291\tif a == b:
2292##\t\tprint 'panic!' # old code we've `commented out'
2293\t\treturn a
2294
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002295Since the `#...' and `##' comment lines have a non-whitespace
2296character following the initial `#', Python mode ignores them when
2297computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002298
2299Continuation Lines and Statements
2300
2301The Python-mode commands generally work on statements instead of on
2302individual lines, where a `statement' is a comment or blank line, or a
2303code line and all of its following continuation lines (if any)
2304considered as a single logical unit. The commands in this mode
2305generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002306statement containing point, even if point happens to be in the middle
2307of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002308
2309
2310@INDENTATION
2311
2312Primarily for entering new code:
2313\t\\[indent-for-tab-command]\t indent line appropriately
2314\t\\[py-newline-and-indent]\t insert newline, then indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002315\t\\[py-electric-backspace]\t reduce indentation, or delete single character
Barry Warsaw7ae77681994-12-12 20:38:05 +00002316
2317Primarily for reindenting existing code:
2318\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
2319\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
2320
2321\t\\[py-indent-region]\t reindent region to match its context
2322\t\\[py-shift-region-left]\t shift region left by py-indent-offset
2323\t\\[py-shift-region-right]\t shift region right by py-indent-offset
2324
2325Unlike most programming languages, Python uses indentation, and only
2326indentation, to specify block structure. Hence the indentation supplied
2327automatically by Python-mode is just an educated guess: only you know
2328the block structure you intend, so only you can supply correct
2329indentation.
2330
2331The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
2332the indentation of preceding statements. E.g., assuming
2333py-indent-offset is 4, after you enter
2334\tif a > 0: \\[py-newline-and-indent]
2335the cursor will be moved to the position of the `_' (_ is not a
2336character in the file, it's just used here to indicate the location of
2337the cursor):
2338\tif a > 0:
2339\t _
2340If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
2341to
2342\tif a > 0:
2343\t c = d
2344\t _
2345Python-mode cannot know whether that's what you intended, or whether
2346\tif a > 0:
2347\t c = d
2348\t_
2349was your intent. In general, Python-mode either reproduces the
2350indentation of the (closest code or indenting-comment) preceding
2351statement, or adds an extra py-indent-offset blanks if the preceding
2352statement has `:' as its last significant (non-whitespace and non-
2353comment) character. If the suggested indentation is too much, use
Barry Warsaw27ee1151997-12-03 05:03:44 +00002354\\[py-electric-backspace] to reduce it.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002355
2356Continuation lines are given extra indentation. If you don't like the
2357suggested indentation, change it to something you do like, and Python-
2358mode will strive to indent later lines of the statement in the same way.
2359
2360If a line is a continuation line by virtue of being in an unclosed
2361paren/bracket/brace structure (`list', for short), the suggested
2362indentation depends on whether the current line contains the first item
2363in the list. If it does, it's indented py-indent-offset columns beyond
2364the indentation of the line containing the open bracket. If you don't
2365like that, change it by hand. The remaining items in the list will mimic
2366whatever indentation you give to the first item.
2367
2368If a line is a continuation line because the line preceding it ends with
2369a backslash, the third and following lines of the statement inherit their
2370indentation from the line preceding them. The indentation of the second
2371line in the statement depends on the form of the first (base) line: if
2372the base line is an assignment statement with anything more interesting
2373than the backslash following the leftmost assigning `=', the second line
2374is indented two columns beyond that `='. Else it's indented to two
2375columns beyond the leftmost solid chunk of non-whitespace characters on
2376the base line.
2377
2378Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
2379repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
2380structure you intend.
2381%c:indent-for-tab-command
2382%c:py-newline-and-indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002383%c:py-electric-backspace
Barry Warsaw7ae77681994-12-12 20:38:05 +00002384
2385
2386The next function may be handy when editing code you didn't write:
2387%c:py-guess-indent-offset
2388
2389
2390The remaining `indent' functions apply to a region of Python code. They
2391assume the block structure (equals indentation, in Python) of the region
2392is correct, and alter the indentation in various ways while preserving
2393the block structure:
2394%c:py-indent-region
2395%c:py-shift-region-left
2396%c:py-shift-region-right
2397
2398@MARKING & MANIPULATING REGIONS OF CODE
2399
2400\\[py-mark-block]\t mark block of lines
Barry Warsaw2518c671997-11-05 00:51:08 +00002401\\[py-mark-def-or-class]\t mark smallest enclosing def
2402\\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002403\\[comment-region]\t comment out region of code
2404\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00002405%c:py-mark-block
Barry Warsaw2518c671997-11-05 00:51:08 +00002406%c:py-mark-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002407%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002408
2409@MOVING POINT
2410
2411\\[py-previous-statement]\t move to statement preceding point
2412\\[py-next-statement]\t move to statement following point
2413\\[py-goto-block-up]\t move up to start of current block
2414\\[beginning-of-python-def-or-class]\t move to start of def
2415\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
2416\\[end-of-python-def-or-class]\t move to end of def
2417\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
2418
2419The first two move to one statement beyond the statement that contains
2420point. A numeric prefix argument tells them to move that many
2421statements instead. Blank lines, comment lines, and continuation lines
2422do not count as `statements' for these commands. So, e.g., you can go
2423to the first code statement in a file by entering
2424\t\\[beginning-of-buffer]\t to move to the top of the file
2425\t\\[py-next-statement]\t to skip over initial comments and blank lines
2426Or do `\\[py-previous-statement]' with a huge prefix argument.
2427%c:py-previous-statement
2428%c:py-next-statement
2429%c:py-goto-block-up
2430%c:beginning-of-python-def-or-class
2431%c:end-of-python-def-or-class
2432
2433@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2434
2435`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2436
2437`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2438overall class and def structure of a module.
2439
2440`\\[back-to-indentation]' moves point to a line's first non-blank character.
2441
2442`\\[indent-relative]' is handy for creating odd indentation.
2443
2444@OTHER EMACS HINTS
2445
2446If you don't like the default value of a variable, change its value to
2447whatever you do like by putting a `setq' line in your .emacs file.
2448E.g., to set the indentation increment to 4, put this line in your
2449.emacs:
2450\t(setq py-indent-offset 4)
2451To see the value of a variable, do `\\[describe-variable]' and enter the variable
2452name at the prompt.
2453
2454When entering a key sequence like `C-c C-n', it is not necessary to
2455release the CONTROL key after doing the `C-c' part -- it suffices to
2456press the CONTROL key, press and release `c' (while still holding down
2457CONTROL), press and release `n' (while still holding down CONTROL), &
2458then release CONTROL.
2459
2460Entering Python mode calls with no arguments the value of the variable
2461`python-mode-hook', if that value exists and is not nil; for backward
2462compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2463the Elisp manual for details.
2464
2465Obscure: When python-mode is first loaded, it looks for all bindings
2466to newline-and-indent in the global keymap, and shadows them with
2467local bindings to py-newline-and-indent."))
2468
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002469
2470;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002471(defvar py-parse-state-re
2472 (concat
2473 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2474 "\\|"
2475 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002476
Barry Warsaw7ae77681994-12-12 20:38:05 +00002477;; returns the parse state at point (see parse-partial-sexp docs)
2478(defun py-parse-state ()
2479 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002480 (let ((here (point))
Barry Warsaw170ffa71996-07-31 20:57:22 +00002481 pps done ci)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002482 (while (not done)
2483 ;; back up to the first preceding line (if any; else start of
2484 ;; buffer) that begins with a popular Python keyword, or a
2485 ;; non- whitespace and non-comment character. These are good
2486 ;; places to start parsing to see whether where we started is
2487 ;; at a non-zero nesting level. It may be slow for people who
2488 ;; write huge code blocks or huge lists ... tough beans.
2489 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw170ffa71996-07-31 20:57:22 +00002490 (setq ci (current-indentation))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002491 (beginning-of-line)
2492 (save-excursion
2493 (setq pps (parse-partial-sexp (point) here)))
2494 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw170ffa71996-07-31 20:57:22 +00002495 (setq done (or (zerop ci)
2496 (not (nth 3 pps))
2497 (bobp)))
2498 )
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002499 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002500
2501;; if point is at a non-zero nesting level, returns the number of the
2502;; character that opens the smallest enclosing unclosed list; else
2503;; returns nil.
2504(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002505 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002506 (if (zerop (car status))
2507 nil ; not in a nest
2508 (car (cdr status))))) ; char# of open bracket
2509
2510;; t iff preceding line ends with backslash that's not in a comment
2511(defun py-backslash-continuation-line-p ()
2512 (save-excursion
2513 (beginning-of-line)
2514 (and
2515 ;; use a cheap test first to avoid the regexp if possible
2516 ;; use 'eq' because char-after may return nil
2517 (eq (char-after (- (point) 2)) ?\\ )
2518 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002519 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002520 (looking-at py-continued-re))))
2521
2522;; t iff current line is a continuation line
2523(defun py-continuation-line-p ()
2524 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002525 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002526 (or (py-backslash-continuation-line-p)
2527 (py-nesting-level))))
2528
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002529;; go to initial line of current statement; usually this is the line
2530;; we're on, but if we're on the 2nd or following lines of a
2531;; continuation block, we need to go up to the first line of the
2532;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002533;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002534;; Tricky: We want to avoid quadratic-time behavior for long continued
2535;; blocks, whether of the backslash or open-bracket varieties, or a
2536;; mix of the two. The following manages to do that in the usual
2537;; cases.
Barry Warsawc210e691998-01-20 22:52:56 +00002538;;
2539;; Also, if we're sitting inside a triple quoted string, this will
2540;; drop us at the line that begins the string.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002541(defun py-goto-initial-line ()
Barry Warsaw9ec9fbc1998-01-21 05:15:57 +00002542 (let (open-bracket-pos)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002543 (while (py-continuation-line-p)
2544 (beginning-of-line)
2545 (if (py-backslash-continuation-line-p)
2546 (while (py-backslash-continuation-line-p)
2547 (forward-line -1))
2548 ;; else zip out of nested brackets/braces/parens
2549 (while (setq open-bracket-pos (py-nesting-level))
2550 (goto-char open-bracket-pos)))))
2551 (beginning-of-line))
2552
2553;; go to point right beyond final line of current statement; usually
2554;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002555;; statement we need to skip over the continuation lines. Tricky:
2556;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002557(defun py-goto-beyond-final-line ()
2558 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002559 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002560 (while (and (py-continuation-line-p)
2561 (not (eobp)))
2562 ;; skip over the backslash flavor
2563 (while (and (py-backslash-continuation-line-p)
2564 (not (eobp)))
2565 (forward-line 1))
2566 ;; if in nest, zip to the end of the nest
2567 (setq state (py-parse-state))
2568 (if (and (not (zerop (car state)))
2569 (not (eobp)))
2570 (progn
Barry Warsawaffc0ca1997-11-03 16:59:38 +00002571 (parse-partial-sexp (point) (point-max) 0 nil state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002572 (forward-line 1))))))
2573
2574;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002575;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00002576(defun py-statement-opens-block-p ()
2577 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002578 (let ((start (point))
2579 (finish (progn (py-goto-beyond-final-line) (1- (point))))
2580 (searching t)
2581 (answer nil)
2582 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002583 (goto-char start)
2584 (while searching
2585 ;; look for a colon with nothing after it except whitespace, and
2586 ;; maybe a comment
2587 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
2588 finish t)
2589 (if (eq (point) finish) ; note: no `else' clause; just
2590 ; keep searching if we're not at
2591 ; the end yet
2592 ;; sure looks like it opens a block -- but it might
2593 ;; be in a comment
2594 (progn
2595 (setq searching nil) ; search is done either way
2596 (setq state (parse-partial-sexp start
2597 (match-beginning 0)))
2598 (setq answer (not (nth 4 state)))))
2599 ;; search failed: couldn't find another interesting colon
2600 (setq searching nil)))
2601 answer)))
2602
Barry Warsawf831d811996-07-31 20:42:59 +00002603(defun py-statement-closes-block-p ()
2604 ;; true iff the current statement `closes' a block == the line
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002605 ;; starts with `return', `raise', `break', `continue', and `pass'.
2606 ;; doesn't catch embedded statements
Barry Warsawf831d811996-07-31 20:42:59 +00002607 (let ((here (point)))
2608 (back-to-indentation)
2609 (prog1
Barry Warsawaffc0ca1997-11-03 16:59:38 +00002610 (looking-at (concat py-block-closing-keywords-re "\\>"))
Barry Warsawf831d811996-07-31 20:42:59 +00002611 (goto-char here))))
2612
Barry Warsaw7ae77681994-12-12 20:38:05 +00002613;; go to point right beyond final line of block begun by the current
2614;; line. This is the same as where py-goto-beyond-final-line goes
2615;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002616;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00002617(defun py-goto-beyond-block ()
2618 (if (py-statement-opens-block-p)
2619 (py-mark-block nil 'just-move)
2620 (py-goto-beyond-final-line)))
2621
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002622;; go to start of first statement (not blank or comment or
2623;; continuation line) at or preceding point. returns t if there is
2624;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002625(defun py-goto-statement-at-or-above ()
2626 (py-goto-initial-line)
2627 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002628 ;; skip back over blank & comment lines
2629 ;; note: will skip a blank or comment line that happens to be
2630 ;; a continuation line too
2631 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
2632 (progn (py-goto-initial-line) t)
2633 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002634 t))
2635
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002636;; go to start of first statement (not blank or comment or
2637;; continuation line) following the statement containing point returns
2638;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002639(defun py-goto-statement-below ()
2640 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002641 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002642 (py-goto-beyond-final-line)
2643 (while (and
2644 (looking-at py-blank-or-comment-re)
2645 (not (eobp)))
2646 (forward-line 1))
2647 (if (eobp)
2648 (progn (goto-char start) nil)
2649 t)))
2650
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002651;; go to start of statement, at or preceding point, starting with
2652;; keyword KEY. Skips blank lines and non-indenting comments upward
2653;; first. If that statement starts with KEY, done, else go back to
2654;; first enclosing block starting with KEY. If successful, leaves
2655;; point at the start of the KEY line & returns t. Else leaves point
2656;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002657(defun py-go-up-tree-to-keyword (key)
2658 ;; skip blanks and non-indenting #
2659 (py-goto-initial-line)
2660 (while (and
2661 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2662 (zerop (forward-line -1))) ; go back
2663 nil)
2664 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002665 (let* ((re (concat "[ \t]*" key "\\b"))
2666 (case-fold-search nil) ; let* so looking-at sees this
2667 (found (looking-at re))
2668 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002669 (while (not (or found dead))
2670 (condition-case nil ; in case no enclosing block
2671 (py-goto-block-up 'no-mark)
2672 (error (setq dead t)))
2673 (or dead (setq found (looking-at re))))
2674 (beginning-of-line)
2675 found))
2676
2677;; return string in buffer from start of indentation to end of line;
2678;; prefix "..." if leading whitespace was skipped
2679(defun py-suck-up-leading-text ()
2680 (save-excursion
2681 (back-to-indentation)
2682 (concat
2683 (if (bolp) "" "...")
2684 (buffer-substring (point) (progn (end-of-line) (point))))))
2685
2686;; assuming point at bolp, return first keyword ([a-z]+) on the line,
2687;; as a Lisp symbol; return nil if none
2688(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002689 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002690 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
2691 (intern (buffer-substring (match-beginning 1) (match-end 1)))
2692 nil)))
2693
Barry Warsawb3e81d51996-09-04 15:12:42 +00002694(defun py-current-defun ()
2695 ;; tell add-log.el how to find the current function/method/variable
2696 (save-excursion
2697 (if (re-search-backward py-defun-start-re nil t)
2698 (or (match-string 3)
2699 (let ((method (match-string 2)))
2700 (if (and (not (zerop (length (match-string 1))))
2701 (re-search-backward py-class-start-re nil t))
2702 (concat (match-string 1) "." method)
2703 method)))
2704 nil)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002705
2706
Barry Warsawfec75d61995-07-05 23:26:15 +00002707(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00002708 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002709
Barry Warsaw850437a1995-03-08 21:50:28 +00002710(defun py-version ()
2711 "Echo the current version of `python-mode' in the minibuffer."
2712 (interactive)
2713 (message "Using `python-mode' version %s" py-version)
2714 (py-keep-region-active))
2715
2716;; only works under Emacs 19
2717;(eval-when-compile
2718; (require 'reporter))
2719
2720(defun py-submit-bug-report (enhancement-p)
2721 "Submit via mail a bug report on `python-mode'.
2722With \\[universal-argument] just submit an enhancement request."
2723 (interactive
2724 (list (not (y-or-n-p
2725 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002726 (let ((reporter-prompt-for-summary-p (if enhancement-p
2727 "(Very) brief summary: "
2728 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00002729 (require 'reporter)
2730 (reporter-submit-bug-report
2731 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002732 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00002733 ;; varlist
2734 (if enhancement-p nil
2735 '(py-python-command
2736 py-indent-offset
2737 py-block-comment-prefix
2738 py-scroll-process-buffer
2739 py-temp-directory
2740 py-beep-if-tab-change))
2741 nil ;pre-hooks
2742 nil ;post-hooks
2743 "Dear Barry,") ;salutation
2744 (if enhancement-p nil
2745 (set-mark (point))
2746 (insert
2747"Please replace this text with a sufficiently large code sample\n\
2748and an exact recipe so that I can reproduce your problem. Failure\n\
2749to do so may mean a greater delay in fixing your bug.\n\n")
2750 (exchange-point-and-mark)
2751 (py-keep-region-active))))
2752
2753
Barry Warsaw47384781997-11-26 05:27:45 +00002754(defun py-kill-emacs-hook ()
2755 (mapcar #'(lambda (filename)
2756 (py-safe (delete-file filename)))
2757 py-file-queue))
2758
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002759;; arrange to kill temp files when Emacs exists
Barry Warsawc72c11c1997-08-09 06:42:08 +00002760(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002761
2762
2763
2764(provide 'python-mode)
2765;;; python-mode.el ends here