blob: bd69739ae0b6be4d560ea5f4b54c841a23b600d1 [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))
963 (progn (backward-to-indentation 1)
964 (while (or (looking-at py-blank-or-comment-re)
965 (bobp))
966 (backward-to-indentation 1))
967 (not (looking-at py-no-outdent-re)))
968 )))
969
Barry Warsawb91b7431995-03-14 15:55:20 +0000970(defun py-electric-colon (arg)
971 "Insert a colon.
972In certain cases the line is outdented appropriately. If a numeric
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000973argument is provided, that many colons are inserted non-electrically.
974Electric behavior is inhibited inside a string or comment."
Barry Warsawb91b7431995-03-14 15:55:20 +0000975 (interactive "P")
976 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000977 ;; are we in a string or comment?
978 (if (save-excursion
979 (let ((pps (parse-partial-sexp (save-excursion
980 (beginning-of-python-def-or-class)
981 (point))
982 (point))))
983 (not (or (nth 3 pps) (nth 4 pps)))))
984 (save-excursion
985 (let ((here (point))
986 (outdent 0)
Barry Warsaw7cb505c1996-10-23 20:44:59 +0000987 (indent (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000988 (if (and (not arg)
989 (py-outdent-p)
990 (= indent (save-excursion
Barry Warsawa7661821996-08-02 16:22:43 +0000991 (py-next-statement -1)
Barry Warsaw7cb505c1996-10-23 20:44:59 +0000992 (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000993 )
994 (setq outdent py-indent-offset))
995 ;; Don't indent, only outdent. This assumes that any lines that
996 ;; are already outdented relative to py-compute-indentation were
997 ;; put there on purpose. Its highly annoying to have `:' indent
998 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
999 ;; there a better way to determine this???
1000 (if (< (current-indentation) indent) nil
1001 (goto-char here)
1002 (beginning-of-line)
1003 (delete-horizontal-space)
1004 (indent-to (- indent outdent))
1005 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +00001006
1007
Barry Warsawa97a3f31997-11-04 18:47:06 +00001008;; Python subprocess utilities and filters
1009(defun py-execute-file (proc filename)
1010 ;; Send a properly formatted execfile('FILENAME') to the underlying
1011 ;; Python interpreter process FILENAME. Make that process's buffer
1012 ;; visible and force display. Also make comint believe the user
1013 ;; typed this string so that kill-output-from-shell does The Right
1014 ;; Thing.
1015 (let ((curbuf (current-buffer))
1016 (procbuf (process-buffer proc))
1017 (comint-scroll-to-bottom-on-output t)
1018 (msg (format "## working on region in file %s...\n" filename))
1019 (cmd (format "execfile('%s')\n" filename)))
1020 (unwind-protect
1021 (progn
1022 (set-buffer procbuf)
1023 (goto-char (point-max))
1024 (move-marker (process-mark proc) (point))
1025 (funcall (process-filter proc) proc msg))
1026 (set-buffer curbuf))
1027 (process-send-string proc cmd)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001028
1029(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001030 (let ((curbuf (current-buffer))
1031 (pbuf (process-buffer pyproc))
1032 (pmark (process-mark pyproc))
1033 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001034 ;; make sure we switch to a different buffer at least once. if we
1035 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001036 ;; window, and point is before the end, and lots of output is
1037 ;; coming at a fast pace, then (a) simple cursor-movement commands
1038 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
1039 ;; to have a visible effect (the window just doesn't get updated,
1040 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
1041 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001042 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001043 ;; #b makes no sense to me at all. #a almost makes sense: unless
1044 ;; we actually change buffers, set_buffer_internal in buffer.c
1045 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
1046 ;; seems to make the Emacs command loop reluctant to update the
1047 ;; display. Perhaps the default process filter in process.c's
1048 ;; read_process_output has update_mode_lines++ for a similar
1049 ;; reason? beats me ...
1050
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001051 (unwind-protect
1052 ;; make sure current buffer is restored
1053 ;; BAW - we want to check to see if this still applies
1054 (progn
1055 ;; mysterious ugly hack
1056 (if (eq curbuf pbuf)
1057 (set-buffer (get-buffer-create "*scratch*")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001058
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001059 (set-buffer pbuf)
1060 (let* ((start (point))
1061 (goback (< start pmark))
1062 (goend (and (not goback) (= start (point-max))))
1063 (buffer-read-only nil))
1064 (goto-char pmark)
1065 (insert string)
1066 (move-marker pmark (point))
1067 (setq file-finished
1068 (and py-file-queue
1069 (equal ">>> "
1070 (buffer-substring
1071 (prog2 (beginning-of-line) (point)
1072 (goto-char pmark))
1073 (point)))))
1074 (if goback (goto-char start)
1075 ;; else
1076 (if py-scroll-process-buffer
1077 (let* ((pop-up-windows t)
1078 (pwin (display-buffer pbuf)))
1079 (set-window-point pwin (point)))))
1080 (set-buffer curbuf)
1081 (if file-finished
1082 (progn
Barry Warsawf4710561997-11-26 21:00:36 +00001083 (py-safe (delete-file (car py-file-queue)))
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001084 (setq py-file-queue (cdr py-file-queue))
1085 (if py-file-queue
1086 (py-execute-file pyproc (car py-file-queue)))))
1087 (and goend
1088 (progn (set-buffer pbuf)
1089 (goto-char (point-max))))
1090 ))
1091 (set-buffer curbuf))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001092
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001093(defun py-postprocess-output-buffer (buf)
Barry Warsaw9981d221997-12-03 05:25:48 +00001094 (let (line file bol)
1095 (save-excursion
1096 (set-buffer buf)
1097 (beginning-of-buffer)
1098 (while (re-search-forward py-traceback-line-re nil t)
1099 (setq file (match-string 1)
1100 line (string-to-int (match-string 2))
1101 bol (py-point 'bol))
1102 (py-highlight-line bol (py-point 'eol) file line))
1103 (when (and py-jump-on-exception line)
1104 (beep)
1105 (py-jump-to-exception file line))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001106 )))
1107
Barry Warsaw9981d221997-12-03 05:25:48 +00001108
Barry Warsawa97a3f31997-11-04 18:47:06 +00001109
1110;;; Subprocess commands
1111
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001112;; only used when (memq 'broken-temp-names py-emacs-features)
1113(defvar py-serial-number 0)
1114(defvar py-exception-buffer nil)
1115(defconst py-output-buffer "*Python Output*")
1116
Barry Warsawa97a3f31997-11-04 18:47:06 +00001117;;;###autoload
1118(defun py-shell ()
1119 "Start an interactive Python interpreter in another window.
1120This is like Shell mode, except that Python is running in the window
1121instead of a shell. See the `Interactive Shell' and `Shell Mode'
1122sections of the Emacs manual for details, especially for the key
1123bindings active in the `*Python*' buffer.
1124
1125See the docs for variable `py-scroll-buffer' for info on scrolling
1126behavior in the process window.
1127
1128Warning: Don't use an interactive Python if you change sys.ps1 or
1129sys.ps2 from their default values, or if you're running code that
1130prints `>>> ' or `... ' at the start of a line. `python-mode' can't
1131distinguish your output from Python's output, and assumes that `>>> '
1132at the start of a line is a prompt from Python. Similarly, the Emacs
1133Shell mode code assumes that both `>>> ' and `... ' at the start of a
1134line are Python prompts. Bad things can happen if you fool either
1135mode.
1136
1137Warning: If you do any editing *in* the process buffer *while* the
1138buffer is accepting output from Python, do NOT attempt to `undo' the
1139changes. Some of the output (nowhere near the parts you changed!) may
1140be lost if you do. This appears to be an Emacs bug, an unfortunate
1141interaction between undo and process filters; the same problem exists in
1142non-Python process buffers using the default (Emacs-supplied) process
1143filter."
1144 ;; BAW - should undo be disabled in the python process buffer, if
1145 ;; this bug still exists?
1146 (interactive)
1147 (require 'comint)
Barry Warsaw2518c671997-11-05 00:51:08 +00001148 (switch-to-buffer-other-window
1149 (make-comint "Python" py-python-command nil "-i"))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001150 (make-local-variable 'comint-prompt-regexp)
1151 (setq comint-prompt-regexp "^>>> \\|^[.][.][.] ")
1152 (set-process-filter (get-buffer-process (current-buffer)) 'py-process-filter)
1153 (set-syntax-table py-mode-syntax-table)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001154 ;; set up keybindings for this subshell
1155 (local-set-key [tab] 'self-insert-command)
1156 (local-set-key "\C-c-" 'py-up-exception)
1157 (local-set-key "\C-c=" 'py-down-exception)
1158 )
Barry Warsawa97a3f31997-11-04 18:47:06 +00001159
Barry Warsawa97a3f31997-11-04 18:47:06 +00001160(defun py-clear-queue ()
1161 "Clear the queue of temporary files waiting to execute."
1162 (interactive)
1163 (let ((n (length py-file-queue)))
1164 (mapcar 'delete-file py-file-queue)
1165 (setq py-file-queue nil)
1166 (message "%d pending files de-queued." n)))
1167
1168(defun py-execute-region (start end &optional async)
1169 "Execute the the region in a Python interpreter.
1170The region is first copied into a temporary file (in the directory
1171`py-temp-directory'). If there is no Python interpreter shell
1172running, this file is executed synchronously using
1173`shell-command-on-region'. If the program is long running, use an
1174optional \\[universal-argument] to run the command asynchronously in
1175its own buffer.
1176
1177If the Python interpreter shell is running, the region is execfile()'d
1178in that shell. If you try to execute regions too quickly,
1179`python-mode' will queue them up and execute them one at a time when
1180it sees a `>>> ' prompt from Python. Each time this happens, the
1181process buffer is popped into a window (if it's not already in some
1182window) so you can see it, and a comment of the form
1183
1184 \t## working on region in file <name>...
1185
1186is inserted at the end. See also the command `py-clear-queue'."
1187 (interactive "r\nP")
1188 (or (< start end)
1189 (error "Region is empty"))
1190 (let* ((proc (get-process "Python"))
Barry Warsaw5e21cb01997-11-05 18:41:11 +00001191 (temp (if (memq 'broken-temp-names py-emacs-features)
1192 (prog1
1193 (format "python-%d" py-serial-number)
1194 (setq py-serial-number (1+ py-serial-number)))
1195 (make-temp-name "python")))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001196 (file (concat (file-name-as-directory py-temp-directory) temp)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001197 (write-region start end file nil 'nomsg)
1198 (cond
1199 ;; always run the code in it's own asynchronous subprocess
1200 (async
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001201 (let* ((buf (generate-new-buffer-name py-output-buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001202 (start-process "Python" buf py-python-command "-u" file)
1203 (pop-to-buffer buf)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001204 (py-postprocess-output-buffer buf)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001205 ))
1206 ;; if the Python interpreter shell is running, queue it up for
1207 ;; execution there.
1208 (proc
1209 ;; use the existing python shell
1210 (if (not py-file-queue)
1211 (py-execute-file proc file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001212 (message "File %s queued for execution" file))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001213 (push file py-file-queue)
1214 (setq py-exception-buffer (cons file (current-buffer))))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001215 (t
1216 ;; otherwise either run it synchronously in a subprocess
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001217 (shell-command-on-region start end py-python-command py-output-buffer)
1218 (setq py-exception-buffer (current-buffer))
1219 (py-postprocess-output-buffer py-output-buffer)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001220 ))))
1221
1222;; Code execution command
1223(defun py-execute-buffer (&optional async)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001224 "Send the contents of the buffer to a Python interpreter.
Barry Warsawc0ecb531998-01-20 21:43:34 +00001225If the file local variable `py-master-file' is non-nil, execute the
1226named file instead of the buffer's file.
1227
Barry Warsaw7ae77681994-12-12 20:38:05 +00001228If there is a *Python* process buffer it is used. If a clipping
1229restriction is in effect, only the accessible portion of the buffer is
1230sent. A trailing newline will be supplied if needed.
1231
1232See the `\\[py-execute-region]' docs for an account of some subtleties."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001233 (interactive "P")
Barry Warsawc0ecb531998-01-20 21:43:34 +00001234 (if py-master-file
1235 (let* ((filename (expand-file-name py-master-file))
1236 (buffer (or (get-file-buffer filename)
1237 (find-file-noselect filename))))
1238 (set-buffer buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001239 (py-execute-region (point-min) (point-max) async))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001240
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001241
1242
1243(defun py-jump-to-exception (file line)
1244 (let ((buffer (cond ((string-equal file "<stdin>")
1245 py-exception-buffer)
1246 ((and (consp py-exception-buffer)
1247 (string-equal file (car py-exception-buffer)))
1248 (cdr py-exception-buffer))
1249 ((py-safe (find-file-noselect file)))
1250 ;; could not figure out what file the exception
1251 ;; is pointing to, so prompt for it
1252 (t (find-file (read-file-name "Exception file: "
1253 nil
1254 file t))))))
1255 (pop-to-buffer buffer)
1256 (goto-line line)
1257 (message "Jumping to exception in file %s on line %d" file line)))
1258
1259(defun py-mouseto-exception (event)
1260 (interactive "e")
1261 (cond
1262 ((fboundp 'event-point)
1263 ;; XEmacs
1264 (let* ((point (event-point event))
1265 (buffer (event-buffer event))
1266 (e (and point buffer (extent-at point buffer 'py-exc-info)))
1267 (info (and e (extent-property e 'py-exc-info))))
1268 (message "Event point: %d, info: %s" point info)
1269 (and info
1270 (py-jump-to-exception (car info) (cdr info)))
1271 ))
1272 ;; Emacs -- Please port this!
1273 ))
1274
1275(defun py-goto-exception ()
1276 "Go to the line indicated by the traceback."
1277 (interactive)
1278 (let (file line)
1279 (save-excursion
1280 (beginning-of-line)
1281 (if (looking-at py-traceback-line-re)
1282 (setq file (match-string 1)
1283 line (string-to-int (match-string 2)))))
1284 (if (not file)
1285 (error "Not on a traceback line."))
1286 (py-jump-to-exception file line)))
1287
1288(defun py-find-next-exception (start buffer searchdir errwhere)
1289 ;; Go to start position in buffer, search in the specified
1290 ;; direction, and jump to the exception found. If at the end of the
1291 ;; exception, print error message
1292 (let (file line)
1293 (save-excursion
1294 (set-buffer buffer)
1295 (goto-char (py-point start))
1296 (if (funcall searchdir py-traceback-line-re nil t)
1297 (setq file (match-string 1)
1298 line (string-to-int (match-string 2)))))
1299 (if (and file line)
1300 (py-jump-to-exception file line)
1301 (error "%s of traceback" errwhere))))
1302
1303(defun py-down-exception (&optional bottom)
1304 "Go to the next line down in the traceback.
1305With optional \\[universal-argument], jump to the bottom (innermost)
1306exception in the exception stack."
1307 (interactive "P")
1308 (let* ((proc (get-process "Python"))
1309 (buffer (if proc "*Python*" py-output-buffer)))
1310 (if bottom
1311 (py-find-next-exception 'eob buffer 're-search-backward "Bottom")
1312 (py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
1313
1314(defun py-up-exception (&optional top)
1315 "Go to the previous line up in the traceback.
1316With optional \\[universal-argument], jump to the top (outermost)
1317exception in the exception stack."
1318 (interactive "P")
1319 (let* ((proc (get-process "Python"))
1320 (buffer (if proc "*Python*" py-output-buffer)))
1321 (if top
1322 (py-find-next-exception 'bob buffer 're-search-forward "Top")
Barry Warsawffbc17d1997-11-27 20:08:14 +00001323 (py-find-next-exception 'bol buffer 're-search-backward "Top"))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001324
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001325
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001326;; Electric deletion
1327(defun py-electric-backspace (arg)
1328 "Deletes preceding character or levels of indentation.
1329Deletion is performed by calling the function in `py-backspace-function'
Barry Warsawb0539931996-12-17 22:05:07 +00001330with a single argument (the number of characters to delete).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001331
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001332If point is at the leftmost column, deletes the preceding newline.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001333
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001334Otherwise, if point is at the leftmost non-whitespace character of a
1335line that is neither a continuation line nor a non-indenting comment
1336line, or if point is at the end of a blank line, this command reduces
1337the indentation to match that of the line that opened the current
1338block of code. The line that opened the block is displayed in the
1339echo area to help you keep track of where you are. With numeric arg,
1340outdents that many blocks (but not past column zero).
1341
1342Otherwise the preceding character is deleted, converting a tab to
1343spaces if needed so that only a single column position is deleted.
1344Numeric argument deletes that many preceding characters."
Barry Warsaw03970731996-07-03 23:12:52 +00001345 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +00001346 (if (or (/= (current-indentation) (current-column))
1347 (bolp)
1348 (py-continuation-line-p)
Barry Warsaw6e527d21996-08-01 15:57:48 +00001349 (not py-honor-comment-indentation)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001350 (looking-at "#[^ \t\n]")) ; non-indenting #
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001351 (funcall py-backspace-function arg)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001352 ;; else indent the same as the colon line that opened the block
Barry Warsaw7ae77681994-12-12 20:38:05 +00001353 ;; force non-blank so py-goto-block-up doesn't ignore it
1354 (insert-char ?* 1)
1355 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001356 (let ((base-indent 0) ; indentation of base line
1357 (base-text "") ; and text of base line
1358 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +00001359 (save-excursion
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001360 (while (< 0 arg)
Barry Warsaw03970731996-07-03 23:12:52 +00001361 (condition-case nil ; in case no enclosing block
1362 (progn
1363 (py-goto-block-up 'no-mark)
1364 (setq base-indent (current-indentation)
1365 base-text (py-suck-up-leading-text)
1366 base-found-p t))
1367 (error nil))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001368 (setq arg (1- arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001369 (delete-char 1) ; toss the dummy character
1370 (delete-horizontal-space)
1371 (indent-to base-indent)
1372 (if base-found-p
1373 (message "Closes block: %s" base-text)))))
1374
Barry Warsawfc8a01f1995-03-09 16:07:29 +00001375
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001376(defun py-electric-delete (arg)
1377 "Deletes preceding or following character or levels of whitespace.
1378
1379The behavior of this function depends on the variable
1380`delete-key-deletes-forward'. If this variable is nil (or does not
1381exist, as in older Emacsen), then this function behaves identical to
1382\\[c-electric-backspace].
1383
1384If `delete-key-deletes-forward' is non-nil and is supported in your
1385Emacs, then deletion occurs in the forward direction, by calling the
1386function in `py-delete-function'."
1387 (interactive "*p")
1388 (if (and (boundp 'delete-key-deletes-forward)
1389 delete-key-deletes-forward)
1390 (funcall py-delete-function arg)
1391 ;; else
1392 (py-electric-backspace arg)))
1393
1394;; required for pending-del and delsel modes
1395(put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
1396(put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del
1397(put 'py-electric-delete 'delete-selection 'supersede) ;delsel
1398(put 'py-electric-delete 'pending-delete 'supersede) ;pending-del
1399
1400
1401
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001402(defun py-indent-line (&optional arg)
1403 "Fix the indentation of the current line according to Python rules.
1404With \\[universal-argument], ignore outdenting rules for block
1405closing statements (e.g. return, raise, break, continue, pass)
1406
1407This function is normally bound to `indent-line-function' so
1408\\[indent-for-tab-command] will call it."
1409 (interactive "P")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001410 (let* ((ci (current-indentation))
1411 (move-to-indentation-p (<= (current-column) ci))
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001412 (need (py-compute-indentation (not arg))))
Barry Warsaw4f009fb1995-03-14 20:53:08 +00001413 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001414 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +00001415 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001416 (if (/= ci need)
1417 (save-excursion
1418 (beginning-of-line)
1419 (delete-horizontal-space)
1420 (indent-to need)))
1421 (if move-to-indentation-p (back-to-indentation))))
1422
1423(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001424 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001425This is just `strives to' because correct indentation can't be computed
1426from scratch for Python code. In general, deletes the whitespace before
1427point, inserts a newline, and takes an educated guess as to how you want
1428the new line indented."
1429 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001430 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001431 (if (< ci (current-column)) ; if point beyond indentation
1432 (newline-and-indent)
1433 ;; else try to act like newline-and-indent "normally" acts
1434 (beginning-of-line)
1435 (insert-char ?\n 1)
1436 (move-to-column ci))))
1437
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001438(defun py-compute-indentation (honor-block-close-p)
1439 ;; implements all the rules for indentation computation. when
1440 ;; honor-block-close-p is non-nil, statements such as return, raise,
1441 ;; break, continue, and pass force one level of outdenting.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001442 (save-excursion
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001443 (let* ((bod (py-point 'bod))
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001444 (pps (parse-partial-sexp bod (point)))
1445 (boipps (parse-partial-sexp bod (py-point 'boi))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001446 (beginning-of-line)
1447 (cond
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001448 ;; are we inside a multi-line string or comment?
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001449 ((or (and (nth 3 pps) (nth 3 boipps))
1450 (and (nth 4 pps) (nth 4 boipps)))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001451 (save-excursion
1452 (if (not py-align-multiline-strings-p) 0
1453 ;; skip back over blank & non-indenting comment lines
1454 ;; note: will skip a blank or non-indenting comment line
1455 ;; that happens to be a continuation line too
1456 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1457 (back-to-indentation)
1458 (current-column))))
1459 ;; are we on a continuation line?
1460 ((py-continuation-line-p)
1461 (let ((startpos (point))
1462 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001463 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001464 (if open-bracket-pos
1465 (progn
1466 ;; align with first item in list; else a normal
1467 ;; indent beyond the line with the open bracket
1468 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1469 ;; is the first list item on the same line?
1470 (skip-chars-forward " \t")
1471 (if (null (memq (following-char) '(?\n ?# ?\\)))
1472 ; yes, so line up with it
1473 (current-column)
1474 ;; first list item on another line, or doesn't exist yet
1475 (forward-line 1)
1476 (while (and (< (point) startpos)
1477 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1478 (forward-line 1))
1479 (if (< (point) startpos)
1480 ;; again mimic the first list item
1481 (current-indentation)
1482 ;; else they're about to enter the first item
1483 (goto-char open-bracket-pos)
1484 (+ (current-indentation) py-indent-offset))))
1485
1486 ;; else on backslash continuation line
1487 (forward-line -1)
1488 (if (py-continuation-line-p) ; on at least 3rd line in block
1489 (current-indentation) ; so just continue the pattern
1490 ;; else started on 2nd line in block, so indent more.
1491 ;; if base line is an assignment with a start on a RHS,
1492 ;; indent to 2 beyond the leftmost "="; else skip first
1493 ;; chunk of non-whitespace characters on base line, + 1 more
1494 ;; column
1495 (end-of-line)
1496 (setq endpos (point) searching t)
1497 (back-to-indentation)
1498 (setq startpos (point))
1499 ;; look at all "=" from left to right, stopping at first
1500 ;; one not nested in a list or string
1501 (while searching
1502 (skip-chars-forward "^=" endpos)
1503 (if (= (point) endpos)
1504 (setq searching nil)
1505 (forward-char 1)
1506 (setq state (parse-partial-sexp startpos (point)))
1507 (if (and (zerop (car state)) ; not in a bracket
1508 (null (nth 3 state))) ; & not in a string
1509 (progn
1510 (setq searching nil) ; done searching in any case
1511 (setq found
1512 (not (or
1513 (eq (following-char) ?=)
1514 (memq (char-after (- (point) 2))
1515 '(?< ?> ?!)))))))))
1516 (if (or (not found) ; not an assignment
1517 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1518 (progn
1519 (goto-char startpos)
1520 (skip-chars-forward "^ \t\n")))
1521 (1+ (current-column))))))
1522
1523 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001524 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001525
Barry Warsawa7891711996-08-01 15:53:09 +00001526 ;; Dfn: "Indenting comment line". A line containing only a
1527 ;; comment, but which is treated like a statement for
1528 ;; indentation calculation purposes. Such lines are only
1529 ;; treated specially by the mode; they are not treated
1530 ;; specially by the Python interpreter.
1531
1532 ;; The rules for indenting comment lines are a line where:
1533 ;; - the first non-whitespace character is `#', and
1534 ;; - the character following the `#' is whitespace, and
1535 ;; - the line is outdented with respect to (i.e. to the left
1536 ;; of) the indentation of the preceding non-blank line.
1537
1538 ;; The first non-blank line following an indenting comment
1539 ;; line is given the same amount of indentation as the
1540 ;; indenting comment line.
1541
1542 ;; All other comment-only lines are ignored for indentation
1543 ;; purposes.
1544
1545 ;; Are we looking at a comment-only line which is *not* an
1546 ;; indenting comment line? If so, we assume that its been
1547 ;; placed at the desired indentation, so leave it alone.
1548 ;; Indenting comment lines are aligned as statements down
1549 ;; below.
1550 ((and (looking-at "[ \t]*#[^ \t\n]")
1551 ;; NOTE: this test will not be performed in older Emacsen
1552 (fboundp 'forward-comment)
1553 (<= (current-indentation)
1554 (save-excursion
1555 (forward-comment (- (point-max)))
1556 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001557 (current-indentation))
1558
1559 ;; else indentation based on that of the statement that
1560 ;; precedes us; use the first line of that statement to
1561 ;; establish the base, in case the user forced a non-std
1562 ;; indentation for the continuation lines (if any)
1563 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001564 ;; skip back over blank & non-indenting comment lines note:
1565 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001566 ;; happens to be a continuation line too. use fast Emacs 19
1567 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001568 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001569 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001570 (forward-comment (- (point-max)))
Barry Warsaw6d627751996-03-06 18:41:38 +00001571 (let (done)
1572 (while (not done)
1573 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
1574 nil 'move)
1575 (setq done (or (eq py-honor-comment-indentation t)
1576 (bobp)
1577 (/= (following-char) ?#)
1578 (not (zerop (current-column)))))
1579 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001580 ;; if we landed inside a string, go to the beginning of that
1581 ;; string. this handles triple quoted, multi-line spanning
1582 ;; strings.
Barry Warsawc210e691998-01-20 22:52:56 +00001583 (let ((skip (nth 3 (parse-partial-sexp bod (point)))))
1584 (while skip
1585 (py-safe (search-backward (make-string 1 skip)))
1586 (setq skip (nth 3 (parse-partial-sexp bod (point))))))
1587 ;; now skip backward over continued lines
Barry Warsaw095e9c61995-09-19 20:01:42 +00001588 (py-goto-initial-line)
Barry Warsawf831d811996-07-31 20:42:59 +00001589 (+ (current-indentation)
1590 (if (py-statement-opens-block-p)
1591 py-indent-offset
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001592 (if (and honor-block-close-p (py-statement-closes-block-p))
Barry Warsawf831d811996-07-31 20:42:59 +00001593 (- py-indent-offset)
1594 0)))
1595 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001596
1597(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001598 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001599By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001600`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +00001601Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001602`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +00001603their own buffer-local copy), both those currently existing and those
1604created later in the Emacs session.
1605
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001606Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001607There's no excuse for such foolishness, but sometimes you have to deal
1608with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001609`py-indent-offset' to what it thinks it was when they created the
1610mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001611
1612Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001613looking for a line that opens a block of code. `py-indent-offset' is
1614set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001615statement following it. If the search doesn't succeed going forward,
1616it's tried again going backward."
1617 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001618 (let (new-value
1619 (start (point))
1620 restart
1621 (found nil)
1622 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001623 (py-goto-initial-line)
1624 (while (not (or found (eobp)))
1625 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1626 (progn
1627 (setq restart (point))
1628 (py-goto-initial-line)
1629 (if (py-statement-opens-block-p)
1630 (setq found t)
1631 (goto-char restart)))))
1632 (if found
1633 ()
1634 (goto-char start)
1635 (py-goto-initial-line)
1636 (while (not (or found (bobp)))
1637 (setq found
1638 (and
1639 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1640 (or (py-goto-initial-line) t) ; always true -- side effect
1641 (py-statement-opens-block-p)))))
1642 (setq colon-indent (current-indentation)
1643 found (and found (zerop (py-next-statement 1)))
1644 new-value (- (current-indentation) colon-indent))
1645 (goto-char start)
1646 (if found
1647 (progn
1648 (funcall (if global 'kill-local-variable 'make-local-variable)
1649 'py-indent-offset)
1650 (setq py-indent-offset new-value)
1651 (message "%s value of py-indent-offset set to %d"
1652 (if global "Global" "Local")
1653 py-indent-offset))
1654 (error "Sorry, couldn't guess a value for py-indent-offset"))))
1655
1656(defun py-shift-region (start end count)
1657 (save-excursion
1658 (goto-char end) (beginning-of-line) (setq end (point))
1659 (goto-char start) (beginning-of-line) (setq start (point))
1660 (indent-rigidly start end count)))
1661
1662(defun py-shift-region-left (start end &optional count)
1663 "Shift region of Python code to the left.
1664The lines from the line containing the start of the current region up
1665to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001666shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001667
1668If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001669many columns. With no active region, outdent only the current line.
1670You cannot outdent the region if any line is already at column zero."
1671 (interactive
1672 (let ((p (point))
1673 (m (mark))
1674 (arg current-prefix-arg))
1675 (if m
1676 (list (min p m) (max p m) arg)
1677 (list p (save-excursion (forward-line 1) (point)) arg))))
1678 ;; if any line is at column zero, don't shift the region
1679 (save-excursion
1680 (goto-char start)
1681 (while (< (point) end)
1682 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00001683 (if (and (zerop (current-column))
1684 (not (looking-at "\\s *$")))
Barry Warsawdea4a291996-07-03 22:59:12 +00001685 (error "Region is at left edge."))
1686 (forward-line 1)))
1687 (py-shift-region start end (- (prefix-numeric-value
1688 (or count py-indent-offset))))
1689 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001690
1691(defun py-shift-region-right (start end &optional count)
1692 "Shift region of Python code to the right.
1693The lines from the line containing the start of the current region up
1694to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001695shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001696
1697If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001698many columns. With no active region, indent only the current line."
1699 (interactive
1700 (let ((p (point))
1701 (m (mark))
1702 (arg current-prefix-arg))
1703 (if m
1704 (list (min p m) (max p m) arg)
1705 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001706 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00001707 (or count py-indent-offset)))
1708 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001709
1710(defun py-indent-region (start end &optional indent-offset)
1711 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00001712
Barry Warsaw7ae77681994-12-12 20:38:05 +00001713The lines from the line containing the start of the current region up
1714to (but not including) the line containing the end of the region are
1715reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001716character in the first column, the first line is left alone and the
1717rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00001718region is reindented with respect to the (closest code or indenting
1719comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001720
1721This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001722control structures are introduced or removed, or to reformat code
1723using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001724
1725If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001726the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00001727used.
1728
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001729Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00001730is called! This function does not compute proper indentation from
1731scratch (that's impossible in Python), it merely adjusts the existing
1732indentation to be correct in context.
1733
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001734Warning: This function really has no idea what to do with
1735non-indenting comment lines, and shifts them as if they were indenting
1736comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001737
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001738Special cases: whitespace is deleted from blank lines; continuation
1739lines are shifted by the same amount their initial line was shifted,
1740in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00001741initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001742 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001743 (save-excursion
1744 (goto-char end) (beginning-of-line) (setq end (point-marker))
1745 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001746 (let ((py-indent-offset (prefix-numeric-value
1747 (or indent-offset py-indent-offset)))
1748 (indents '(-1)) ; stack of active indent levels
1749 (target-column 0) ; column to which to indent
1750 (base-shifted-by 0) ; amount last base line was shifted
1751 (indent-base (if (looking-at "[ \t\n]")
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001752 (py-compute-indentation t)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001753 0))
1754 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001755 (while (< (point) end)
1756 (setq ci (current-indentation))
1757 ;; figure out appropriate target column
1758 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001759 ((or (eq (following-char) ?#) ; comment in column 1
1760 (looking-at "[ \t]*$")) ; entirely blank
1761 (setq target-column 0))
1762 ((py-continuation-line-p) ; shift relative to base line
1763 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001764 (t ; new base line
1765 (if (> ci (car indents)) ; going deeper; push it
1766 (setq indents (cons ci indents))
1767 ;; else we should have seen this indent before
1768 (setq indents (memq ci indents)) ; pop deeper indents
1769 (if (null indents)
1770 (error "Bad indentation in region, at line %d"
1771 (save-restriction
1772 (widen)
1773 (1+ (count-lines 1 (point)))))))
1774 (setq target-column (+ indent-base
1775 (* py-indent-offset
1776 (- (length indents) 2))))
1777 (setq base-shifted-by (- target-column ci))))
1778 ;; shift as needed
1779 (if (/= ci target-column)
1780 (progn
1781 (delete-horizontal-space)
1782 (indent-to target-column)))
1783 (forward-line 1))))
1784 (set-marker end nil))
1785
Barry Warsawa7891711996-08-01 15:53:09 +00001786(defun py-comment-region (beg end &optional arg)
1787 "Like `comment-region' but uses double hash (`#') comment starter."
1788 (interactive "r\nP")
Barry Warsaw3fcaf611996-08-01 20:11:51 +00001789 (let ((comment-start py-block-comment-prefix))
Barry Warsawa7891711996-08-01 15:53:09 +00001790 (comment-region beg end arg)))
1791
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001792
1793;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00001794(defun py-previous-statement (count)
1795 "Go to the start of previous Python statement.
1796If the statement at point is the i'th Python statement, goes to the
1797start of statement i-COUNT. If there is no such statement, goes to the
1798first statement. Returns count of statements left to move.
1799`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001800 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001801 (if (< count 0) (py-next-statement (- count))
1802 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001803 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001804 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001805 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001806 (> count 0)
1807 (zerop (forward-line -1))
1808 (py-goto-statement-at-or-above))
1809 (setq count (1- count)))
1810 (if (> count 0) (goto-char start)))
1811 count))
1812
1813(defun py-next-statement (count)
1814 "Go to the start of next Python statement.
1815If the statement at point is the i'th Python statement, goes to the
1816start of statement i+COUNT. If there is no such statement, goes to the
1817last statement. Returns count of statements left to move. `Statements'
1818do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001819 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001820 (if (< count 0) (py-previous-statement (- count))
1821 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001822 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001823 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001824 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001825 (> count 0)
1826 (py-goto-statement-below))
1827 (setq count (1- count)))
1828 (if (> count 0) (goto-char start)))
1829 count))
1830
1831(defun py-goto-block-up (&optional nomark)
1832 "Move up to start of current block.
1833Go to the statement that starts the smallest enclosing block; roughly
1834speaking, this will be the closest preceding statement that ends with a
1835colon and is indented less than the statement you started on. If
1836successful, also sets the mark to the starting point.
1837
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001838`\\[py-mark-block]' can be used afterward to mark the whole code
1839block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001840
1841If called from a program, the mark will not be set if optional argument
1842NOMARK is not nil."
1843 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001844 (let ((start (point))
1845 (found nil)
1846 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001847 (py-goto-initial-line)
1848 ;; if on blank or non-indenting comment line, use the preceding stmt
1849 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1850 (progn
1851 (py-goto-statement-at-or-above)
1852 (setq found (py-statement-opens-block-p))))
1853 ;; search back for colon line indented less
1854 (setq initial-indent (current-indentation))
1855 (if (zerop initial-indent)
1856 ;; force fast exit
1857 (goto-char (point-min)))
1858 (while (not (or found (bobp)))
1859 (setq found
1860 (and
1861 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1862 (or (py-goto-initial-line) t) ; always true -- side effect
1863 (< (current-indentation) initial-indent)
1864 (py-statement-opens-block-p))))
1865 (if found
1866 (progn
1867 (or nomark (push-mark start))
1868 (back-to-indentation))
1869 (goto-char start)
1870 (error "Enclosing block not found"))))
1871
1872(defun beginning-of-python-def-or-class (&optional class)
1873 "Move point to start of def (or class, with prefix arg).
1874
1875Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001876arg, looks for a `class' instead. The docs assume the `def' case;
1877just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001878
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001879If point is in a def statement already, and after the `d', simply
1880moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001881
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001882Else (point is not in a def statement, or at or before the `d' of a
1883def statement), searches for the closest preceding def statement, and
1884leaves point at its start. If no such statement can be found, leaves
1885point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001886
1887Returns t iff a def statement is found by these rules.
1888
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001889Note that doing this command repeatedly will take you closer to the
1890start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001891
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001892If you want to mark the current def/class, see
Barry Warsaw2518c671997-11-05 00:51:08 +00001893`\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001894 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001895 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1896 (start-of-line (progn (beginning-of-line) (point)))
1897 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001898 (if (or (/= start-of-stmt start-of-line)
1899 (not at-or-before-p))
1900 (end-of-line)) ; OK to match on this line
1901 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001902 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001903
1904(defun end-of-python-def-or-class (&optional class)
1905 "Move point beyond end of def (or class, with prefix arg) body.
1906
1907By default, looks for an appropriate `def'. If you supply a prefix arg,
1908looks for a `class' instead. The docs assume the `def' case; just
1909substitute `class' for `def' for the other case.
1910
1911If point is in a def statement already, this is the def we use.
1912
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001913Else if the def found by `\\[beginning-of-python-def-or-class]'
1914contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001915
1916Else we search forward for the closest following def, and use that.
1917
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001918If a def can be found by these rules, point is moved to the start of
1919the line immediately following the def block, and the position of the
1920start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001921
1922Else point is moved to the end of the buffer, and nil is returned.
1923
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001924Note that doing this command repeatedly will take you closer to the
1925end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001926
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001927If you want to mark the current def/class, see
Barry Warsaw2518c671997-11-05 00:51:08 +00001928`\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001929 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001930 (let ((start (progn (py-goto-initial-line) (point)))
1931 (which (if class "class" "def"))
1932 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001933 ;; move point to start of appropriate def/class
1934 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1935 (setq state 'at-beginning)
1936 ;; else see if beginning-of-python-def-or-class hits container
1937 (if (and (beginning-of-python-def-or-class class)
1938 (progn (py-goto-beyond-block)
1939 (> (point) start)))
1940 (setq state 'at-end)
1941 ;; else search forward
1942 (goto-char start)
1943 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1944 (progn (setq state 'at-beginning)
1945 (beginning-of-line)))))
1946 (cond
1947 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1948 ((eq state 'at-end) t)
1949 ((eq state 'not-found) nil)
1950 (t (error "internal error in end-of-python-def-or-class")))))
1951
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001952
1953;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001954(defun py-mark-block (&optional extend just-move)
1955 "Mark following block of lines. With prefix arg, mark structure.
1956Easier to use than explain. It sets the region to an `interesting'
1957block of succeeding lines. If point is on a blank line, it goes down to
1958the next non-blank line. That will be the start of the region. The end
1959of the region depends on the kind of line at the start:
1960
1961 - If a comment, the region will include all succeeding comment lines up
1962 to (but not including) the next non-comment line (if any).
1963
1964 - Else if a prefix arg is given, and the line begins one of these
1965 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001966
1967 if elif else try except finally for while def class
1968
Barry Warsaw7ae77681994-12-12 20:38:05 +00001969 the region will be set to the body of the structure, including
1970 following blocks that `belong' to it, but excluding trailing blank
1971 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001972 and all (if any) of the following `except' and `finally' blocks
1973 that belong to the `try' structure will be in the region. Ditto
1974 for if/elif/else, for/else and while/else structures, and (a bit
1975 degenerate, since they're always one-block structures) def and
1976 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001977
1978 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001979 block (see list above), and the block is not a `one-liner' (i.e.,
1980 the statement ends with a colon, not with code), the region will
1981 include all succeeding lines up to (but not including) the next
1982 code statement (if any) that's indented no more than the starting
1983 line, except that trailing blank and comment lines are excluded.
1984 E.g., if the starting line begins a multi-statement `def'
1985 structure, the region will be set to the full function definition,
1986 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001987
1988 - Else the region will include all succeeding lines up to (but not
1989 including) the next blank line, or code or indenting-comment line
1990 indented strictly less than the starting line. Trailing indenting
1991 comment lines are included in this case, but not trailing blank
1992 lines.
1993
1994A msg identifying the location of the mark is displayed in the echo
1995area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1996
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001997If called from a program, optional argument EXTEND plays the role of
1998the prefix arg, and if optional argument JUST-MOVE is not nil, just
1999moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002000 (interactive "P") ; raw prefix arg
2001 (py-goto-initial-line)
2002 ;; skip over blank lines
2003 (while (and
2004 (looking-at "[ \t]*$") ; while blank line
2005 (not (eobp))) ; & somewhere to go
2006 (forward-line 1))
2007 (if (eobp)
2008 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002009 (let ((initial-pos (point))
2010 (initial-indent (current-indentation))
2011 last-pos ; position of last stmt in region
2012 (followers
2013 '((if elif else) (elif elif else) (else)
2014 (try except finally) (except except) (finally)
2015 (for else) (while else)
2016 (def) (class) ) )
2017 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002018
2019 (cond
2020 ;; if comment line, suck up the following comment lines
2021 ((looking-at "[ \t]*#")
2022 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
2023 (re-search-backward "^[ \t]*#") ; and back to last comment in block
2024 (setq last-pos (point)))
2025
2026 ;; else if line is a block line and EXTEND given, suck up
2027 ;; the whole structure
2028 ((and extend
2029 (setq first-symbol (py-suck-up-first-keyword) )
2030 (assq first-symbol followers))
2031 (while (and
2032 (or (py-goto-beyond-block) t) ; side effect
2033 (forward-line -1) ; side effect
2034 (setq last-pos (point)) ; side effect
2035 (py-goto-statement-below)
2036 (= (current-indentation) initial-indent)
2037 (setq next-symbol (py-suck-up-first-keyword))
2038 (memq next-symbol (cdr (assq first-symbol followers))))
2039 (setq first-symbol next-symbol)))
2040
2041 ;; else if line *opens* a block, search for next stmt indented <=
2042 ((py-statement-opens-block-p)
2043 (while (and
2044 (setq last-pos (point)) ; always true -- side effect
2045 (py-goto-statement-below)
2046 (> (current-indentation) initial-indent))
2047 nil))
2048
2049 ;; else plain code line; stop at next blank line, or stmt or
2050 ;; indenting comment line indented <
2051 (t
2052 (while (and
2053 (setq last-pos (point)) ; always true -- side effect
2054 (or (py-goto-beyond-final-line) t)
2055 (not (looking-at "[ \t]*$")) ; stop at blank line
2056 (or
2057 (>= (current-indentation) initial-indent)
2058 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
2059 nil)))
2060
2061 ;; skip to end of last stmt
2062 (goto-char last-pos)
2063 (py-goto-beyond-final-line)
2064
2065 ;; set mark & display
2066 (if just-move
2067 () ; just return
2068 (push-mark (point) 'no-msg)
2069 (forward-line -1)
2070 (message "Mark set after: %s" (py-suck-up-leading-text))
2071 (goto-char initial-pos))))
2072
Barry Warsaw2518c671997-11-05 00:51:08 +00002073(defun py-mark-def-or-class (&optional class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002074 "Set region to body of def (or class, with prefix arg) enclosing point.
2075Pushes the current mark, then point, on the mark ring (all language
2076modes do this, but although it's handy it's never documented ...).
2077
2078In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002079hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
2080`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002081
2082And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002083Turned out that was more confusing than useful: the `goto start' and
2084`goto end' commands are usually used to search through a file, and
2085people expect them to act a lot like `search backward' and `search
2086forward' string-search commands. But because Python `def' and `class'
2087can nest to arbitrary levels, finding the smallest def containing
2088point cannot be done via a simple backward search: the def containing
2089point may not be the closest preceding def, or even the closest
2090preceding def that's indented less. The fancy algorithm required is
2091appropriate for the usual uses of this `mark' command, but not for the
2092`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002093
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002094So the def marked by this command may not be the one either of the
2095`goto' commands find: If point is on a blank or non-indenting comment
2096line, moves back to start of the closest preceding code statement or
2097indenting comment line. If this is a `def' statement, that's the def
2098we use. Else searches for the smallest enclosing `def' block and uses
2099that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002100
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002101When an enclosing def is found: The mark is left immediately beyond
2102the last line of the def block. Point is left at the start of the
2103def, except that: if the def is preceded by a number of comment lines
2104followed by (at most) one optional blank line, point is left at the
2105start of the comments; else if the def is preceded by a blank line,
2106point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002107
2108The intent is to mark the containing def/class and its associated
2109documentation, to make moving and duplicating functions and classes
2110pleasant."
2111 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002112 (let ((start (point))
2113 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002114 (push-mark start)
2115 (if (not (py-go-up-tree-to-keyword which))
2116 (progn (goto-char start)
2117 (error "Enclosing %s not found" which))
2118 ;; else enclosing def/class found
2119 (setq start (point))
2120 (py-goto-beyond-block)
2121 (push-mark (point))
2122 (goto-char start)
2123 (if (zerop (forward-line -1)) ; if there is a preceding line
2124 (progn
2125 (if (looking-at "[ \t]*$") ; it's blank
2126 (setq start (point)) ; so reset start point
2127 (goto-char start)) ; else try again
2128 (if (zerop (forward-line -1))
2129 (if (looking-at "[ \t]*#") ; a comment
2130 ;; look back for non-comment line
2131 ;; tricky: note that the regexp matches a blank
2132 ;; line, cuz \n is in the 2nd character class
2133 (and
2134 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
2135 (forward-line 1))
2136 ;; no comment, so go back
Barry Warsaw4da6bd51997-11-26 06:00:26 +00002137 (goto-char start)))))))
2138 (exchange-point-and-mark)
2139 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002140
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002141;; ripped from cc-mode
2142(defun py-forward-into-nomenclature (&optional arg)
2143 "Move forward to end of a nomenclature section or word.
2144With arg, to it arg times.
2145
2146A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2147 (interactive "p")
2148 (let ((case-fold-search nil))
2149 (if (> arg 0)
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002150 (re-search-forward
2151 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
2152 (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002153 (while (and (< arg 0)
2154 (re-search-backward
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002155 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002156 (point-min) 0))
2157 (forward-char 1)
2158 (setq arg (1+ arg)))))
2159 (py-keep-region-active))
2160
2161(defun py-backward-into-nomenclature (&optional arg)
2162 "Move backward to beginning of a nomenclature section or word.
2163With optional ARG, move that many times. If ARG is negative, move
2164forward.
2165
2166A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2167 (interactive "p")
2168 (py-forward-into-nomenclature (- arg))
2169 (py-keep-region-active))
2170
2171
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002172
2173;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002174
2175;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002176;; plus lines of the form ^[vc]:name$ to suck variable & command docs
2177;; out of the right places, along with the keys they're on & current
2178;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00002179(defun py-dump-help-string (str)
2180 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002181 (let ((locals (buffer-local-variables))
2182 funckind funcname func funcdoc
2183 (start 0) mstart end
2184 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002185 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
2186 (setq mstart (match-beginning 0) end (match-end 0)
2187 funckind (substring str (match-beginning 1) (match-end 1))
2188 funcname (substring str (match-beginning 2) (match-end 2))
2189 func (intern funcname))
2190 (princ (substitute-command-keys (substring str start mstart)))
2191 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002192 ((equal funckind "c") ; command
2193 (setq funcdoc (documentation func)
2194 keys (concat
2195 "Key(s): "
2196 (mapconcat 'key-description
2197 (where-is-internal func py-mode-map)
2198 ", "))))
2199 ((equal funckind "v") ; variable
Barry Warsaw604cefa1996-09-03 18:17:04 +00002200 (setq funcdoc (documentation-property func 'variable-documentation)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002201 keys (if (assq func locals)
2202 (concat
2203 "Local/Global values: "
2204 (prin1-to-string (symbol-value func))
2205 " / "
2206 (prin1-to-string (default-value func)))
2207 (concat
2208 "Value: "
2209 (prin1-to-string (symbol-value func))))))
2210 (t ; unexpected
2211 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002212 (princ (format "\n-> %s:\t%s\t%s\n\n"
2213 (if (equal funckind "c") "Command" "Variable")
2214 funcname keys))
2215 (princ funcdoc)
2216 (terpri)
2217 (setq start end))
2218 (princ (substitute-command-keys (substring str start))))
2219 (print-help-return-message)))
2220
2221(defun py-describe-mode ()
2222 "Dump long form of Python-mode docs."
2223 (interactive)
2224 (py-dump-help-string "Major mode for editing Python files.
2225Knows about Python indentation, tokens, comments and continuation lines.
2226Paragraphs are separated by blank lines only.
2227
2228Major sections below begin with the string `@'; specific function and
2229variable docs begin with `->'.
2230
2231@EXECUTING PYTHON CODE
2232
2233\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
2234\\[py-execute-region]\tsends the current region
2235\\[py-shell]\tstarts a Python interpreter window; this will be used by
2236\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
2237%c:py-execute-buffer
2238%c:py-execute-region
2239%c:py-shell
2240
2241@VARIABLES
2242
2243py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00002244py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002245
2246py-python-command\tshell command to invoke Python interpreter
2247py-scroll-process-buffer\talways scroll Python process buffer
2248py-temp-directory\tdirectory used for temp files (if needed)
2249
2250py-beep-if-tab-change\tring the bell if tab-width is changed
2251%v:py-indent-offset
2252%v:py-block-comment-prefix
2253%v:py-python-command
2254%v:py-scroll-process-buffer
2255%v:py-temp-directory
2256%v:py-beep-if-tab-change
2257
2258@KINDS OF LINES
2259
2260Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002261preceding line ends with a backslash that's not part of a comment, or
2262the paren/bracket/brace nesting level at the start of the line is
2263non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002264
2265An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002266possibly blanks or tabs), a `comment line' (leftmost non-blank
2267character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002268
2269Comment Lines
2270
2271Although all comment lines are treated alike by Python, Python mode
2272recognizes two kinds that act differently with respect to indentation.
2273
2274An `indenting comment line' is a comment line with a blank, tab or
2275nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002276treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00002277indenting comment line will be indented like the comment line. All
2278other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002279following the initial `#') are `non-indenting comment lines', and
2280their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002281
2282Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002283whenever possible. Non-indenting comment lines are useful in cases
2284like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00002285
2286\ta = b # a very wordy single-line comment that ends up being
2287\t #... continued onto another line
2288
2289\tif a == b:
2290##\t\tprint 'panic!' # old code we've `commented out'
2291\t\treturn a
2292
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002293Since the `#...' and `##' comment lines have a non-whitespace
2294character following the initial `#', Python mode ignores them when
2295computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002296
2297Continuation Lines and Statements
2298
2299The Python-mode commands generally work on statements instead of on
2300individual lines, where a `statement' is a comment or blank line, or a
2301code line and all of its following continuation lines (if any)
2302considered as a single logical unit. The commands in this mode
2303generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002304statement containing point, even if point happens to be in the middle
2305of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002306
2307
2308@INDENTATION
2309
2310Primarily for entering new code:
2311\t\\[indent-for-tab-command]\t indent line appropriately
2312\t\\[py-newline-and-indent]\t insert newline, then indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002313\t\\[py-electric-backspace]\t reduce indentation, or delete single character
Barry Warsaw7ae77681994-12-12 20:38:05 +00002314
2315Primarily for reindenting existing code:
2316\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
2317\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
2318
2319\t\\[py-indent-region]\t reindent region to match its context
2320\t\\[py-shift-region-left]\t shift region left by py-indent-offset
2321\t\\[py-shift-region-right]\t shift region right by py-indent-offset
2322
2323Unlike most programming languages, Python uses indentation, and only
2324indentation, to specify block structure. Hence the indentation supplied
2325automatically by Python-mode is just an educated guess: only you know
2326the block structure you intend, so only you can supply correct
2327indentation.
2328
2329The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
2330the indentation of preceding statements. E.g., assuming
2331py-indent-offset is 4, after you enter
2332\tif a > 0: \\[py-newline-and-indent]
2333the cursor will be moved to the position of the `_' (_ is not a
2334character in the file, it's just used here to indicate the location of
2335the cursor):
2336\tif a > 0:
2337\t _
2338If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
2339to
2340\tif a > 0:
2341\t c = d
2342\t _
2343Python-mode cannot know whether that's what you intended, or whether
2344\tif a > 0:
2345\t c = d
2346\t_
2347was your intent. In general, Python-mode either reproduces the
2348indentation of the (closest code or indenting-comment) preceding
2349statement, or adds an extra py-indent-offset blanks if the preceding
2350statement has `:' as its last significant (non-whitespace and non-
2351comment) character. If the suggested indentation is too much, use
Barry Warsaw27ee1151997-12-03 05:03:44 +00002352\\[py-electric-backspace] to reduce it.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002353
2354Continuation lines are given extra indentation. If you don't like the
2355suggested indentation, change it to something you do like, and Python-
2356mode will strive to indent later lines of the statement in the same way.
2357
2358If a line is a continuation line by virtue of being in an unclosed
2359paren/bracket/brace structure (`list', for short), the suggested
2360indentation depends on whether the current line contains the first item
2361in the list. If it does, it's indented py-indent-offset columns beyond
2362the indentation of the line containing the open bracket. If you don't
2363like that, change it by hand. The remaining items in the list will mimic
2364whatever indentation you give to the first item.
2365
2366If a line is a continuation line because the line preceding it ends with
2367a backslash, the third and following lines of the statement inherit their
2368indentation from the line preceding them. The indentation of the second
2369line in the statement depends on the form of the first (base) line: if
2370the base line is an assignment statement with anything more interesting
2371than the backslash following the leftmost assigning `=', the second line
2372is indented two columns beyond that `='. Else it's indented to two
2373columns beyond the leftmost solid chunk of non-whitespace characters on
2374the base line.
2375
2376Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
2377repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
2378structure you intend.
2379%c:indent-for-tab-command
2380%c:py-newline-and-indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002381%c:py-electric-backspace
Barry Warsaw7ae77681994-12-12 20:38:05 +00002382
2383
2384The next function may be handy when editing code you didn't write:
2385%c:py-guess-indent-offset
2386
2387
2388The remaining `indent' functions apply to a region of Python code. They
2389assume the block structure (equals indentation, in Python) of the region
2390is correct, and alter the indentation in various ways while preserving
2391the block structure:
2392%c:py-indent-region
2393%c:py-shift-region-left
2394%c:py-shift-region-right
2395
2396@MARKING & MANIPULATING REGIONS OF CODE
2397
2398\\[py-mark-block]\t mark block of lines
Barry Warsaw2518c671997-11-05 00:51:08 +00002399\\[py-mark-def-or-class]\t mark smallest enclosing def
2400\\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002401\\[comment-region]\t comment out region of code
2402\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00002403%c:py-mark-block
Barry Warsaw2518c671997-11-05 00:51:08 +00002404%c:py-mark-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002405%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002406
2407@MOVING POINT
2408
2409\\[py-previous-statement]\t move to statement preceding point
2410\\[py-next-statement]\t move to statement following point
2411\\[py-goto-block-up]\t move up to start of current block
2412\\[beginning-of-python-def-or-class]\t move to start of def
2413\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
2414\\[end-of-python-def-or-class]\t move to end of def
2415\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
2416
2417The first two move to one statement beyond the statement that contains
2418point. A numeric prefix argument tells them to move that many
2419statements instead. Blank lines, comment lines, and continuation lines
2420do not count as `statements' for these commands. So, e.g., you can go
2421to the first code statement in a file by entering
2422\t\\[beginning-of-buffer]\t to move to the top of the file
2423\t\\[py-next-statement]\t to skip over initial comments and blank lines
2424Or do `\\[py-previous-statement]' with a huge prefix argument.
2425%c:py-previous-statement
2426%c:py-next-statement
2427%c:py-goto-block-up
2428%c:beginning-of-python-def-or-class
2429%c:end-of-python-def-or-class
2430
2431@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2432
2433`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2434
2435`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2436overall class and def structure of a module.
2437
2438`\\[back-to-indentation]' moves point to a line's first non-blank character.
2439
2440`\\[indent-relative]' is handy for creating odd indentation.
2441
2442@OTHER EMACS HINTS
2443
2444If you don't like the default value of a variable, change its value to
2445whatever you do like by putting a `setq' line in your .emacs file.
2446E.g., to set the indentation increment to 4, put this line in your
2447.emacs:
2448\t(setq py-indent-offset 4)
2449To see the value of a variable, do `\\[describe-variable]' and enter the variable
2450name at the prompt.
2451
2452When entering a key sequence like `C-c C-n', it is not necessary to
2453release the CONTROL key after doing the `C-c' part -- it suffices to
2454press the CONTROL key, press and release `c' (while still holding down
2455CONTROL), press and release `n' (while still holding down CONTROL), &
2456then release CONTROL.
2457
2458Entering Python mode calls with no arguments the value of the variable
2459`python-mode-hook', if that value exists and is not nil; for backward
2460compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2461the Elisp manual for details.
2462
2463Obscure: When python-mode is first loaded, it looks for all bindings
2464to newline-and-indent in the global keymap, and shadows them with
2465local bindings to py-newline-and-indent."))
2466
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002467
2468;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002469(defvar py-parse-state-re
2470 (concat
2471 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2472 "\\|"
2473 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002474
Barry Warsaw7ae77681994-12-12 20:38:05 +00002475;; returns the parse state at point (see parse-partial-sexp docs)
2476(defun py-parse-state ()
2477 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002478 (let ((here (point))
Barry Warsaw170ffa71996-07-31 20:57:22 +00002479 pps done ci)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002480 (while (not done)
2481 ;; back up to the first preceding line (if any; else start of
2482 ;; buffer) that begins with a popular Python keyword, or a
2483 ;; non- whitespace and non-comment character. These are good
2484 ;; places to start parsing to see whether where we started is
2485 ;; at a non-zero nesting level. It may be slow for people who
2486 ;; write huge code blocks or huge lists ... tough beans.
2487 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw170ffa71996-07-31 20:57:22 +00002488 (setq ci (current-indentation))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002489 (beginning-of-line)
2490 (save-excursion
2491 (setq pps (parse-partial-sexp (point) here)))
2492 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw170ffa71996-07-31 20:57:22 +00002493 (setq done (or (zerop ci)
2494 (not (nth 3 pps))
2495 (bobp)))
2496 )
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002497 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002498
2499;; if point is at a non-zero nesting level, returns the number of the
2500;; character that opens the smallest enclosing unclosed list; else
2501;; returns nil.
2502(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002503 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002504 (if (zerop (car status))
2505 nil ; not in a nest
2506 (car (cdr status))))) ; char# of open bracket
2507
2508;; t iff preceding line ends with backslash that's not in a comment
2509(defun py-backslash-continuation-line-p ()
2510 (save-excursion
2511 (beginning-of-line)
2512 (and
2513 ;; use a cheap test first to avoid the regexp if possible
2514 ;; use 'eq' because char-after may return nil
2515 (eq (char-after (- (point) 2)) ?\\ )
2516 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002517 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002518 (looking-at py-continued-re))))
2519
2520;; t iff current line is a continuation line
2521(defun py-continuation-line-p ()
2522 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002523 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002524 (or (py-backslash-continuation-line-p)
2525 (py-nesting-level))))
2526
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002527;; go to initial line of current statement; usually this is the line
2528;; we're on, but if we're on the 2nd or following lines of a
2529;; continuation block, we need to go up to the first line of the
2530;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002531;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002532;; Tricky: We want to avoid quadratic-time behavior for long continued
2533;; blocks, whether of the backslash or open-bracket varieties, or a
2534;; mix of the two. The following manages to do that in the usual
2535;; cases.
Barry Warsawc210e691998-01-20 22:52:56 +00002536;;
2537;; Also, if we're sitting inside a triple quoted string, this will
2538;; drop us at the line that begins the string.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002539(defun py-goto-initial-line ()
Barry Warsaw9ec9fbc1998-01-21 05:15:57 +00002540 (let (open-bracket-pos)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002541 (while (py-continuation-line-p)
2542 (beginning-of-line)
2543 (if (py-backslash-continuation-line-p)
2544 (while (py-backslash-continuation-line-p)
2545 (forward-line -1))
2546 ;; else zip out of nested brackets/braces/parens
2547 (while (setq open-bracket-pos (py-nesting-level))
2548 (goto-char open-bracket-pos)))))
2549 (beginning-of-line))
2550
2551;; go to point right beyond final line of current statement; usually
2552;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002553;; statement we need to skip over the continuation lines. Tricky:
2554;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002555(defun py-goto-beyond-final-line ()
2556 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002557 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002558 (while (and (py-continuation-line-p)
2559 (not (eobp)))
2560 ;; skip over the backslash flavor
2561 (while (and (py-backslash-continuation-line-p)
2562 (not (eobp)))
2563 (forward-line 1))
2564 ;; if in nest, zip to the end of the nest
2565 (setq state (py-parse-state))
2566 (if (and (not (zerop (car state)))
2567 (not (eobp)))
2568 (progn
Barry Warsawaffc0ca1997-11-03 16:59:38 +00002569 (parse-partial-sexp (point) (point-max) 0 nil state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002570 (forward-line 1))))))
2571
2572;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002573;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00002574(defun py-statement-opens-block-p ()
2575 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002576 (let ((start (point))
2577 (finish (progn (py-goto-beyond-final-line) (1- (point))))
2578 (searching t)
2579 (answer nil)
2580 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002581 (goto-char start)
2582 (while searching
2583 ;; look for a colon with nothing after it except whitespace, and
2584 ;; maybe a comment
2585 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
2586 finish t)
2587 (if (eq (point) finish) ; note: no `else' clause; just
2588 ; keep searching if we're not at
2589 ; the end yet
2590 ;; sure looks like it opens a block -- but it might
2591 ;; be in a comment
2592 (progn
2593 (setq searching nil) ; search is done either way
2594 (setq state (parse-partial-sexp start
2595 (match-beginning 0)))
2596 (setq answer (not (nth 4 state)))))
2597 ;; search failed: couldn't find another interesting colon
2598 (setq searching nil)))
2599 answer)))
2600
Barry Warsawf831d811996-07-31 20:42:59 +00002601(defun py-statement-closes-block-p ()
2602 ;; true iff the current statement `closes' a block == the line
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002603 ;; starts with `return', `raise', `break', `continue', and `pass'.
2604 ;; doesn't catch embedded statements
Barry Warsawf831d811996-07-31 20:42:59 +00002605 (let ((here (point)))
2606 (back-to-indentation)
2607 (prog1
Barry Warsawaffc0ca1997-11-03 16:59:38 +00002608 (looking-at (concat py-block-closing-keywords-re "\\>"))
Barry Warsawf831d811996-07-31 20:42:59 +00002609 (goto-char here))))
2610
Barry Warsaw7ae77681994-12-12 20:38:05 +00002611;; go to point right beyond final line of block begun by the current
2612;; line. This is the same as where py-goto-beyond-final-line goes
2613;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002614;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00002615(defun py-goto-beyond-block ()
2616 (if (py-statement-opens-block-p)
2617 (py-mark-block nil 'just-move)
2618 (py-goto-beyond-final-line)))
2619
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002620;; go to start of first statement (not blank or comment or
2621;; continuation line) at or preceding point. returns t if there is
2622;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002623(defun py-goto-statement-at-or-above ()
2624 (py-goto-initial-line)
2625 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002626 ;; skip back over blank & comment lines
2627 ;; note: will skip a blank or comment line that happens to be
2628 ;; a continuation line too
2629 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
2630 (progn (py-goto-initial-line) t)
2631 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002632 t))
2633
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002634;; go to start of first statement (not blank or comment or
2635;; continuation line) following the statement containing point returns
2636;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002637(defun py-goto-statement-below ()
2638 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002639 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002640 (py-goto-beyond-final-line)
2641 (while (and
2642 (looking-at py-blank-or-comment-re)
2643 (not (eobp)))
2644 (forward-line 1))
2645 (if (eobp)
2646 (progn (goto-char start) nil)
2647 t)))
2648
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002649;; go to start of statement, at or preceding point, starting with
2650;; keyword KEY. Skips blank lines and non-indenting comments upward
2651;; first. If that statement starts with KEY, done, else go back to
2652;; first enclosing block starting with KEY. If successful, leaves
2653;; point at the start of the KEY line & returns t. Else leaves point
2654;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002655(defun py-go-up-tree-to-keyword (key)
2656 ;; skip blanks and non-indenting #
2657 (py-goto-initial-line)
2658 (while (and
2659 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2660 (zerop (forward-line -1))) ; go back
2661 nil)
2662 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002663 (let* ((re (concat "[ \t]*" key "\\b"))
2664 (case-fold-search nil) ; let* so looking-at sees this
2665 (found (looking-at re))
2666 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002667 (while (not (or found dead))
2668 (condition-case nil ; in case no enclosing block
2669 (py-goto-block-up 'no-mark)
2670 (error (setq dead t)))
2671 (or dead (setq found (looking-at re))))
2672 (beginning-of-line)
2673 found))
2674
2675;; return string in buffer from start of indentation to end of line;
2676;; prefix "..." if leading whitespace was skipped
2677(defun py-suck-up-leading-text ()
2678 (save-excursion
2679 (back-to-indentation)
2680 (concat
2681 (if (bolp) "" "...")
2682 (buffer-substring (point) (progn (end-of-line) (point))))))
2683
2684;; assuming point at bolp, return first keyword ([a-z]+) on the line,
2685;; as a Lisp symbol; return nil if none
2686(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002687 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002688 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
2689 (intern (buffer-substring (match-beginning 1) (match-end 1)))
2690 nil)))
2691
Barry Warsawb3e81d51996-09-04 15:12:42 +00002692(defun py-current-defun ()
2693 ;; tell add-log.el how to find the current function/method/variable
2694 (save-excursion
2695 (if (re-search-backward py-defun-start-re nil t)
2696 (or (match-string 3)
2697 (let ((method (match-string 2)))
2698 (if (and (not (zerop (length (match-string 1))))
2699 (re-search-backward py-class-start-re nil t))
2700 (concat (match-string 1) "." method)
2701 method)))
2702 nil)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002703
2704
Barry Warsawfec75d61995-07-05 23:26:15 +00002705(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00002706 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002707
Barry Warsaw850437a1995-03-08 21:50:28 +00002708(defun py-version ()
2709 "Echo the current version of `python-mode' in the minibuffer."
2710 (interactive)
2711 (message "Using `python-mode' version %s" py-version)
2712 (py-keep-region-active))
2713
2714;; only works under Emacs 19
2715;(eval-when-compile
2716; (require 'reporter))
2717
2718(defun py-submit-bug-report (enhancement-p)
2719 "Submit via mail a bug report on `python-mode'.
2720With \\[universal-argument] just submit an enhancement request."
2721 (interactive
2722 (list (not (y-or-n-p
2723 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002724 (let ((reporter-prompt-for-summary-p (if enhancement-p
2725 "(Very) brief summary: "
2726 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00002727 (require 'reporter)
2728 (reporter-submit-bug-report
2729 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002730 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00002731 ;; varlist
2732 (if enhancement-p nil
2733 '(py-python-command
2734 py-indent-offset
2735 py-block-comment-prefix
2736 py-scroll-process-buffer
2737 py-temp-directory
2738 py-beep-if-tab-change))
2739 nil ;pre-hooks
2740 nil ;post-hooks
2741 "Dear Barry,") ;salutation
2742 (if enhancement-p nil
2743 (set-mark (point))
2744 (insert
2745"Please replace this text with a sufficiently large code sample\n\
2746and an exact recipe so that I can reproduce your problem. Failure\n\
2747to do so may mean a greater delay in fixing your bug.\n\n")
2748 (exchange-point-and-mark)
2749 (py-keep-region-active))))
2750
2751
Barry Warsaw47384781997-11-26 05:27:45 +00002752(defun py-kill-emacs-hook ()
2753 (mapcar #'(lambda (filename)
2754 (py-safe (delete-file filename)))
2755 py-file-queue))
2756
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002757;; arrange to kill temp files when Emacs exists
Barry Warsawc72c11c1997-08-09 06:42:08 +00002758(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002759
2760
2761
2762(provide 'python-mode)
2763;;; python-mode.el ends here