blob: 4dd1de647e158189503233d1f8e7464711b9f7d1 [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
553 (define-key py-mode-map "\n" 'py-newline-and-indent)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000554 ;; shadow global bindings for newline-and-indent w/ the py- version.
555 ;; BAW - this is extremely bad form, but I'm not going to change it
556 ;; for now.
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000557 (mapcar #'(lambda (key)
558 (define-key py-mode-map key 'py-newline-and-indent))
559 (where-is-internal 'newline-and-indent))
560 )
561
562(defvar py-mode-output-map nil
563 "Keymap used in *Python Output* buffers*")
564(if py-mode-output-map
565 nil
566 (setq py-mode-output-map (make-sparse-keymap))
567 (define-key py-mode-output-map [button2] 'py-mouseto-exception)
568 (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
569 ;; TBD: Disable all self-inserting keys. This is bogus, we should
570 ;; really implement this as *Python Output* buffer being read-only
571 (mapcar #' (lambda (key)
572 (define-key py-mode-output-map key
573 #'(lambda () (interactive) (beep))))
574 (where-is-internal 'self-insert-command))
Barry Warsaw850437a1995-03-08 21:50:28 +0000575 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000576
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000577(defvar py-mode-syntax-table nil
578 "Syntax table used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000579(if py-mode-syntax-table
Barry Warsawc72c11c1997-08-09 06:42:08 +0000580 nil
Barry Warsaw7ae77681994-12-12 20:38:05 +0000581 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsawc72c11c1997-08-09 06:42:08 +0000582 (modify-syntax-entry ?\( "()" py-mode-syntax-table)
583 (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 ;; Add operator symbols misassigned in the std table
589 (modify-syntax-entry ?\$ "." py-mode-syntax-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 ;; For historical reasons, underscore is word class instead of
601 ;; symbol class. GNU conventions say it should be symbol class, but
602 ;; there's a natural conflict between what major mode authors want
603 ;; and what users expect from `forward-word' and `backward-word'.
604 ;; Guido and I have hashed this out and have decided to keep
605 ;; underscore in word class. If you're tempted to change it, try
606 ;; binding M-f and M-b to py-forward-into-nomenclature and
607 ;; py-backward-into-nomenclature instead.
608 (modify-syntax-entry ?\_ "w" py-mode-syntax-table)
609 ;; Both single quote and double quote are string delimiters
610 (modify-syntax-entry ?\' "\"" py-mode-syntax-table)
611 (modify-syntax-entry ?\" "\"" py-mode-syntax-table)
612 ;; backquote is open and close paren
613 (modify-syntax-entry ?\` "$" py-mode-syntax-table)
614 ;; comment delimiters
615 (modify-syntax-entry ?\# "<" py-mode-syntax-table)
616 (modify-syntax-entry ?\n ">" py-mode-syntax-table)
617 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000618
Barry Warsawb3e81d51996-09-04 15:12:42 +0000619
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000620
Barry Warsaw42f707f1996-07-29 21:05:05 +0000621;; Menu definitions, only relevent if you have the easymenu.el package
622;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
Barry Warsaw5490a061996-08-06 15:43:33 +0000623(defvar py-menu nil
624 "Menu for Python Mode.
Barry Warsawc72c11c1997-08-09 06:42:08 +0000625This menu will get created automatically if you have the `easymenu'
626package. Note that the latest X/Emacs releases contain this package.")
Barry Warsaw5490a061996-08-06 15:43:33 +0000627
Barry Warsawc72c11c1997-08-09 06:42:08 +0000628(and (py-safe (require 'easymenu) t)
629 (easy-menu-define
630 py-menu py-mode-map "Python Mode menu"
631 '("Python"
632 ["Comment Out Region" py-comment-region (mark)]
633 ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)]
634 "-"
635 ["Mark current block" py-mark-block t]
Barry Warsaw2518c671997-11-05 00:51:08 +0000636 ["Mark current def" py-mark-def-or-class t]
637 ["Mark current class" (py-mark-def-or-class t) t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000638 "-"
639 ["Shift region left" py-shift-region-left (mark)]
640 ["Shift region right" py-shift-region-right (mark)]
641 "-"
642 ["Execute buffer" py-execute-buffer t]
643 ["Execute region" py-execute-region (mark)]
644 ["Start interpreter..." py-shell t]
645 "-"
646 ["Go to start of block" py-goto-block-up t]
647 ["Go to start of class" (beginning-of-python-def-or-class t) t]
648 ["Move to end of class" (end-of-python-def-or-class t) t]
649 ["Move to start of def" beginning-of-python-def-or-class t]
650 ["Move to end of def" end-of-python-def-or-class t]
651 "-"
652 ["Describe mode" py-describe-mode t]
653 )))
Barry Warsaw42f707f1996-07-29 21:05:05 +0000654
Barry Warsaw81437461996-08-01 19:48:02 +0000655
656
657;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp>
658(defvar imenu-example--python-class-regexp
659 (concat ; <<classes>>
660 "\\(" ;
661 "^[ \t]*" ; newline and maybe whitespace
662 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
663 ; possibly multiple superclasses
664 "\\([ \t]*\\((\\([a-zA-Z0-9_, \t\n]\\)*)\\)?\\)"
665 "[ \t]*:" ; and the final :
666 "\\)" ; >>classes<<
667 )
668 "Regexp for Python classes for use with the imenu package."
669 )
670
671(defvar imenu-example--python-method-regexp
672 (concat ; <<methods and functions>>
673 "\\(" ;
674 "^[ \t]*" ; new line and maybe whitespace
675 "\\(def[ \t]+" ; function definitions start with def
676 "\\([a-zA-Z0-9_]+\\)" ; name is here
677 ; function arguments...
678 "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))"
679 "\\)" ; end of def
680 "[ \t]*:" ; and then the :
681 "\\)" ; >>methods and functions<<
682 )
683 "Regexp for Python methods/functions for use with the imenu package."
684 )
685
686(defvar imenu-example--python-method-no-arg-parens '(2 8)
687 "Indicies into groups of the Python regexp for use with imenu.
688
689Using these values will result in smaller imenu lists, as arguments to
690functions are not listed.
691
692See the variable `imenu-example--python-show-method-args-p' for more
693information.")
694
695(defvar imenu-example--python-method-arg-parens '(2 7)
696 "Indicies into groups of the Python regexp for use with imenu.
697Using these values will result in large imenu lists, as arguments to
698functions are listed.
699
700See the variable `imenu-example--python-show-method-args-p' for more
701information.")
702
703;; Note that in this format, this variable can still be used with the
704;; imenu--generic-function. Otherwise, there is no real reason to have
705;; it.
706(defvar imenu-example--generic-python-expression
707 (cons
708 (concat
709 imenu-example--python-class-regexp
710 "\\|" ; or...
711 imenu-example--python-method-regexp
712 )
713 imenu-example--python-method-no-arg-parens)
714 "Generic Python expression which may be used directly with imenu.
715Used by setting the variable `imenu-generic-expression' to this value.
716Also, see the function \\[imenu-example--create-python-index] for a
717better alternative for finding the index.")
718
719;; These next two variables are used when searching for the python
720;; class/definitions. Just saving some time in accessing the
721;; generic-python-expression, really.
Barry Warsaw01af4011996-09-04 14:57:22 +0000722(defvar imenu-example--python-generic-regexp nil)
723(defvar imenu-example--python-generic-parens nil)
Barry Warsaw81437461996-08-01 19:48:02 +0000724
725
Barry Warsaw81437461996-08-01 19:48:02 +0000726(defun imenu-example--create-python-index ()
727 "Python interface function for imenu package.
728Finds all python classes and functions/methods. Calls function
729\\[imenu-example--create-python-index-engine]. See that function for
730the details of how this works."
731 (setq imenu-example--python-generic-regexp
732 (car imenu-example--generic-python-expression))
733 (setq imenu-example--python-generic-parens
734 (if imenu-example--python-show-method-args-p
735 imenu-example--python-method-arg-parens
736 imenu-example--python-method-no-arg-parens))
737 (goto-char (point-min))
738 (imenu-example--create-python-index-engine nil))
739
740(defun imenu-example--create-python-index-engine (&optional start-indent)
741 "Function for finding imenu definitions in Python.
742
743Finds all definitions (classes, methods, or functions) in a Python
744file for the imenu package.
745
746Returns a possibly nested alist of the form
747
748 (INDEX-NAME . INDEX-POSITION)
749
750The second element of the alist may be an alist, producing a nested
751list as in
752
753 (INDEX-NAME . INDEX-ALIST)
754
755This function should not be called directly, as it calls itself
756recursively and requires some setup. Rather this is the engine for
757the function \\[imenu-example--create-python-index].
758
759It works recursively by looking for all definitions at the current
760indention level. When it finds one, it adds it to the alist. If it
761finds a definition at a greater indentation level, it removes the
762previous definition from the alist. In it's place it adds all
763definitions found at the next indentation level. When it finds a
764definition that is less indented then the current level, it retuns the
765alist it has created thus far.
766
767The optional argument START-INDENT indicates the starting indentation
768at which to continue looking for Python classes, methods, or
769functions. If this is not supplied, the function uses the indentation
770of the first definition found."
771 (let ((index-alist '())
772 (sub-method-alist '())
773 looking-p
774 def-name prev-name
775 cur-indent def-pos
776 (class-paren (first imenu-example--python-generic-parens))
777 (def-paren (second imenu-example--python-generic-parens)))
778 (setq looking-p
779 (re-search-forward imenu-example--python-generic-regexp
780 (point-max) t))
781 (while looking-p
782 (save-excursion
783 ;; used to set def-name to this value but generic-extract-name is
784 ;; new to imenu-1.14. this way it still works with imenu-1.11
785 ;;(imenu--generic-extract-name imenu-example--python-generic-parens))
786 (let ((cur-paren (if (match-beginning class-paren)
787 class-paren def-paren)))
788 (setq def-name
Barry Warsawc8520351997-11-26 06:14:40 +0000789 (buffer-substring-no-properties (match-beginning cur-paren)
790 (match-end cur-paren))))
Barry Warsaw81437461996-08-01 19:48:02 +0000791 (beginning-of-line)
792 (setq cur-indent (current-indentation)))
793
794 ;; HACK: want to go to the next correct definition location. we
795 ;; explicitly list them here. would be better to have them in a
796 ;; list.
797 (setq def-pos
798 (or (match-beginning class-paren)
799 (match-beginning def-paren)))
800
801 ;; if we don't have a starting indent level, take this one
802 (or start-indent
803 (setq start-indent cur-indent))
804
805 ;; if we don't have class name yet, take this one
806 (or prev-name
807 (setq prev-name def-name))
808
809 ;; what level is the next definition on? must be same, deeper
810 ;; or shallower indentation
811 (cond
812 ;; at the same indent level, add it to the list...
813 ((= start-indent cur-indent)
814
815 ;; if we don't have push, use the following...
816 ;;(setf index-alist (cons (cons def-name def-pos) index-alist))
817 (push (cons def-name def-pos) index-alist))
818
819 ;; deeper indented expression, recur...
820 ((< start-indent cur-indent)
821
822 ;; the point is currently on the expression we're supposed to
823 ;; start on, so go back to the last expression. The recursive
824 ;; call will find this place again and add it to the correct
825 ;; list
826 (re-search-backward imenu-example--python-generic-regexp
827 (point-min) 'move)
828 (setq sub-method-alist (imenu-example--create-python-index-engine
829 cur-indent))
830
831 (if sub-method-alist
832 ;; we put the last element on the index-alist on the start
833 ;; of the submethod alist so the user can still get to it.
834 (let ((save-elmt (pop index-alist)))
Barry Warsawc8520351997-11-26 06:14:40 +0000835 (push (cons prev-name
Barry Warsaw81437461996-08-01 19:48:02 +0000836 (cons save-elmt sub-method-alist))
837 index-alist))))
838
839 ;; found less indented expression, we're done.
840 (t
841 (setq looking-p nil)
842 (re-search-backward imenu-example--python-generic-regexp
843 (point-min) t)))
844 (setq prev-name def-name)
845 (and looking-p
846 (setq looking-p
847 (re-search-forward imenu-example--python-generic-regexp
848 (point-max) 'move))))
849 (nreverse index-alist)))
850
Barry Warsaw42f707f1996-07-29 21:05:05 +0000851
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000852;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000853(defun python-mode ()
854 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000855To submit a problem report, enter `\\[py-submit-bug-report]' from a
856`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
857documentation. To see what version of `python-mode' you are running,
858enter `\\[py-version]'.
859
860This mode knows about Python indentation, tokens, comments and
861continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000862
863COMMANDS
864\\{py-mode-map}
865VARIABLES
866
Barry Warsaw42f707f1996-07-29 21:05:05 +0000867py-indent-offset\t\tindentation increment
868py-block-comment-prefix\t\tcomment string used by comment-region
869py-python-command\t\tshell command to invoke Python interpreter
870py-scroll-process-buffer\t\talways scroll Python process buffer
871py-temp-directory\t\tdirectory used for temp files (if needed)
872py-beep-if-tab-change\t\tring the bell if tab-width is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +0000873 (interactive)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000874 ;; set up local variables
Barry Warsaw7ae77681994-12-12 20:38:05 +0000875 (kill-all-local-variables)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000876 (make-local-variable 'font-lock-defaults)
877 (make-local-variable 'paragraph-separate)
878 (make-local-variable 'paragraph-start)
879 (make-local-variable 'require-final-newline)
880 (make-local-variable 'comment-start)
Barry Warsaw5c8bef11996-12-17 21:56:10 +0000881 (make-local-variable 'comment-end)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000882 (make-local-variable 'comment-start-skip)
883 (make-local-variable 'comment-column)
884 (make-local-variable 'indent-region-function)
885 (make-local-variable 'indent-line-function)
Barry Warsawb3e81d51996-09-04 15:12:42 +0000886 (make-local-variable 'add-log-current-defun-function)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000887 ;;
Barry Warsaw7ae77681994-12-12 20:38:05 +0000888 (set-syntax-table py-mode-syntax-table)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000889 (setq major-mode 'python-mode
890 mode-name "Python"
891 local-abbrev-table python-mode-abbrev-table
Barry Warsaw5c38bf61997-12-02 22:01:04 +0000892 font-lock-defaults '(python-font-lock-keywords)
Barry Warsaw615d4a41996-09-04 14:14:10 +0000893 paragraph-separate "^[ \t]*$"
894 paragraph-start "^[ \t]*$"
895 require-final-newline t
896 comment-start "# "
Barry Warsaw5c8bef11996-12-17 21:56:10 +0000897 comment-end ""
Barry Warsaw615d4a41996-09-04 14:14:10 +0000898 comment-start-skip "# *"
899 comment-column 40
Barry Warsaw550a02e1996-09-04 14:23:00 +0000900 indent-region-function 'py-indent-region
901 indent-line-function 'py-indent-line
Barry Warsawb3e81d51996-09-04 15:12:42 +0000902 ;; tell add-log.el how to find the current function/method/variable
903 add-log-current-defun-function 'py-current-defun
Barry Warsawb1f89511996-09-03 16:38:30 +0000904 )
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000905 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +0000906 ;; add the menu
907 (if py-menu
908 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +0000909 ;; Emacs 19 requires this
Barry Warsawc72c11c1997-08-09 06:42:08 +0000910 (if (boundp 'comment-multi-line)
Barry Warsaw57697af1995-09-14 20:01:14 +0000911 (setq comment-multi-line nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000912 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000913 ;;
914 ;; not sure where the magic comment has to be; to save time
915 ;; searching for a rarity, we give up if it's not found prior to the
916 ;; first executable statement.
917 ;;
918 ;; BAW - on first glance, this seems like complete hackery. Why was
919 ;; this necessary, and is it still necessary?
920 (let ((case-fold-search nil)
921 (start (point))
922 new-tab-width)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000923 (if (re-search-forward
924 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
925 (prog2 (py-next-statement 1) (point) (goto-char 1))
926 t)
927 (progn
928 (setq new-tab-width
929 (string-to-int
930 (buffer-substring (match-beginning 1) (match-end 1))))
931 (if (= tab-width new-tab-width)
932 nil
933 (setq tab-width new-tab-width)
934 (message "Caution: tab-width changed to %d" new-tab-width)
935 (if py-beep-if-tab-change (beep)))))
936 (goto-char start))
937
Barry Warsaw755c6711996-08-01 20:02:55 +0000938 ;; install imenu
Barry Warsaw27ee1151997-12-03 05:03:44 +0000939 (if (py-safe (require 'imenu))
940 (progn
941 (make-variable-buffer-local 'imenu-create-index-function)
942 (setq imenu-create-index-function
943 (function imenu-example--create-python-index))
944 (setq imenu-generic-expression
945 imenu-example--generic-python-expression)
946 (if (fboundp 'imenu-add-to-menubar)
947 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
948 ))
Barry Warsaw755c6711996-08-01 20:02:55 +0000949
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000950 ;; run the mode hook. py-mode-hook use is deprecated
Barry Warsaw7ae77681994-12-12 20:38:05 +0000951 (if python-mode-hook
952 (run-hooks 'python-mode-hook)
953 (run-hooks 'py-mode-hook)))
954
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000955
Barry Warsawb91b7431995-03-14 15:55:20 +0000956;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000957(defun py-outdent-p ()
958 ;; returns non-nil if the current line should outdent one level
959 (save-excursion
960 (and (progn (back-to-indentation)
961 (looking-at py-outdent-re))
962 (progn (backward-to-indentation 1)
963 (while (or (looking-at py-blank-or-comment-re)
964 (bobp))
965 (backward-to-indentation 1))
966 (not (looking-at py-no-outdent-re)))
967 )))
968
Barry Warsawb91b7431995-03-14 15:55:20 +0000969(defun py-electric-colon (arg)
970 "Insert a colon.
971In certain cases the line is outdented appropriately. If a numeric
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000972argument is provided, that many colons are inserted non-electrically.
973Electric behavior is inhibited inside a string or comment."
Barry Warsawb91b7431995-03-14 15:55:20 +0000974 (interactive "P")
975 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000976 ;; are we in a string or comment?
977 (if (save-excursion
978 (let ((pps (parse-partial-sexp (save-excursion
979 (beginning-of-python-def-or-class)
980 (point))
981 (point))))
982 (not (or (nth 3 pps) (nth 4 pps)))))
983 (save-excursion
984 (let ((here (point))
985 (outdent 0)
Barry Warsaw7cb505c1996-10-23 20:44:59 +0000986 (indent (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000987 (if (and (not arg)
988 (py-outdent-p)
989 (= indent (save-excursion
Barry Warsawa7661821996-08-02 16:22:43 +0000990 (py-next-statement -1)
Barry Warsaw7cb505c1996-10-23 20:44:59 +0000991 (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000992 )
993 (setq outdent py-indent-offset))
994 ;; Don't indent, only outdent. This assumes that any lines that
995 ;; are already outdented relative to py-compute-indentation were
996 ;; put there on purpose. Its highly annoying to have `:' indent
997 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
998 ;; there a better way to determine this???
999 (if (< (current-indentation) indent) nil
1000 (goto-char here)
1001 (beginning-of-line)
1002 (delete-horizontal-space)
1003 (indent-to (- indent outdent))
1004 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +00001005
1006
Barry Warsawa97a3f31997-11-04 18:47:06 +00001007;; Python subprocess utilities and filters
1008(defun py-execute-file (proc filename)
1009 ;; Send a properly formatted execfile('FILENAME') to the underlying
1010 ;; Python interpreter process FILENAME. Make that process's buffer
1011 ;; visible and force display. Also make comint believe the user
1012 ;; typed this string so that kill-output-from-shell does The Right
1013 ;; Thing.
1014 (let ((curbuf (current-buffer))
1015 (procbuf (process-buffer proc))
1016 (comint-scroll-to-bottom-on-output t)
1017 (msg (format "## working on region in file %s...\n" filename))
1018 (cmd (format "execfile('%s')\n" filename)))
1019 (unwind-protect
1020 (progn
1021 (set-buffer procbuf)
1022 (goto-char (point-max))
1023 (move-marker (process-mark proc) (point))
1024 (funcall (process-filter proc) proc msg))
1025 (set-buffer curbuf))
1026 (process-send-string proc cmd)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001027
1028(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001029 (let ((curbuf (current-buffer))
1030 (pbuf (process-buffer pyproc))
1031 (pmark (process-mark pyproc))
1032 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001033 ;; make sure we switch to a different buffer at least once. if we
1034 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001035 ;; window, and point is before the end, and lots of output is
1036 ;; coming at a fast pace, then (a) simple cursor-movement commands
1037 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
1038 ;; to have a visible effect (the window just doesn't get updated,
1039 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
1040 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001041 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001042 ;; #b makes no sense to me at all. #a almost makes sense: unless
1043 ;; we actually change buffers, set_buffer_internal in buffer.c
1044 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
1045 ;; seems to make the Emacs command loop reluctant to update the
1046 ;; display. Perhaps the default process filter in process.c's
1047 ;; read_process_output has update_mode_lines++ for a similar
1048 ;; reason? beats me ...
1049
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001050 (unwind-protect
1051 ;; make sure current buffer is restored
1052 ;; BAW - we want to check to see if this still applies
1053 (progn
1054 ;; mysterious ugly hack
1055 (if (eq curbuf pbuf)
1056 (set-buffer (get-buffer-create "*scratch*")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001057
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001058 (set-buffer pbuf)
1059 (let* ((start (point))
1060 (goback (< start pmark))
1061 (goend (and (not goback) (= start (point-max))))
1062 (buffer-read-only nil))
1063 (goto-char pmark)
1064 (insert string)
1065 (move-marker pmark (point))
1066 (setq file-finished
1067 (and py-file-queue
1068 (equal ">>> "
1069 (buffer-substring
1070 (prog2 (beginning-of-line) (point)
1071 (goto-char pmark))
1072 (point)))))
1073 (if goback (goto-char start)
1074 ;; else
1075 (if py-scroll-process-buffer
1076 (let* ((pop-up-windows t)
1077 (pwin (display-buffer pbuf)))
1078 (set-window-point pwin (point)))))
1079 (set-buffer curbuf)
1080 (if file-finished
1081 (progn
Barry Warsawf4710561997-11-26 21:00:36 +00001082 (py-safe (delete-file (car py-file-queue)))
Barry Warsaw7a73ef81996-09-30 23:00:40 +00001083 (setq py-file-queue (cdr py-file-queue))
1084 (if py-file-queue
1085 (py-execute-file pyproc (car py-file-queue)))))
1086 (and goend
1087 (progn (set-buffer pbuf)
1088 (goto-char (point-max))))
1089 ))
1090 (set-buffer curbuf))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001091
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001092(defun py-postprocess-output-buffer (buf)
Barry Warsaw9981d221997-12-03 05:25:48 +00001093 (let (line file bol)
1094 (save-excursion
1095 (set-buffer buf)
1096 (beginning-of-buffer)
1097 (while (re-search-forward py-traceback-line-re nil t)
1098 (setq file (match-string 1)
1099 line (string-to-int (match-string 2))
1100 bol (py-point 'bol))
1101 (py-highlight-line bol (py-point 'eol) file line))
1102 (when (and py-jump-on-exception line)
1103 (beep)
1104 (py-jump-to-exception file line))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001105 )))
1106
Barry Warsaw9981d221997-12-03 05:25:48 +00001107
Barry Warsawa97a3f31997-11-04 18:47:06 +00001108
1109;;; Subprocess commands
1110
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001111;; only used when (memq 'broken-temp-names py-emacs-features)
1112(defvar py-serial-number 0)
1113(defvar py-exception-buffer nil)
1114(defconst py-output-buffer "*Python Output*")
1115
Barry Warsawa97a3f31997-11-04 18:47:06 +00001116;;;###autoload
1117(defun py-shell ()
1118 "Start an interactive Python interpreter in another window.
1119This is like Shell mode, except that Python is running in the window
1120instead of a shell. See the `Interactive Shell' and `Shell Mode'
1121sections of the Emacs manual for details, especially for the key
1122bindings active in the `*Python*' buffer.
1123
1124See the docs for variable `py-scroll-buffer' for info on scrolling
1125behavior in the process window.
1126
1127Warning: Don't use an interactive Python if you change sys.ps1 or
1128sys.ps2 from their default values, or if you're running code that
1129prints `>>> ' or `... ' at the start of a line. `python-mode' can't
1130distinguish your output from Python's output, and assumes that `>>> '
1131at the start of a line is a prompt from Python. Similarly, the Emacs
1132Shell mode code assumes that both `>>> ' and `... ' at the start of a
1133line are Python prompts. Bad things can happen if you fool either
1134mode.
1135
1136Warning: If you do any editing *in* the process buffer *while* the
1137buffer is accepting output from Python, do NOT attempt to `undo' the
1138changes. Some of the output (nowhere near the parts you changed!) may
1139be lost if you do. This appears to be an Emacs bug, an unfortunate
1140interaction between undo and process filters; the same problem exists in
1141non-Python process buffers using the default (Emacs-supplied) process
1142filter."
1143 ;; BAW - should undo be disabled in the python process buffer, if
1144 ;; this bug still exists?
1145 (interactive)
1146 (require 'comint)
Barry Warsaw2518c671997-11-05 00:51:08 +00001147 (switch-to-buffer-other-window
1148 (make-comint "Python" py-python-command nil "-i"))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001149 (make-local-variable 'comint-prompt-regexp)
1150 (setq comint-prompt-regexp "^>>> \\|^[.][.][.] ")
1151 (set-process-filter (get-buffer-process (current-buffer)) 'py-process-filter)
1152 (set-syntax-table py-mode-syntax-table)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001153 ;; set up keybindings for this subshell
1154 (local-set-key [tab] 'self-insert-command)
1155 (local-set-key "\C-c-" 'py-up-exception)
1156 (local-set-key "\C-c=" 'py-down-exception)
1157 )
Barry Warsawa97a3f31997-11-04 18:47:06 +00001158
Barry Warsawa97a3f31997-11-04 18:47:06 +00001159(defun py-clear-queue ()
1160 "Clear the queue of temporary files waiting to execute."
1161 (interactive)
1162 (let ((n (length py-file-queue)))
1163 (mapcar 'delete-file py-file-queue)
1164 (setq py-file-queue nil)
1165 (message "%d pending files de-queued." n)))
1166
1167(defun py-execute-region (start end &optional async)
1168 "Execute the the region in a Python interpreter.
1169The region is first copied into a temporary file (in the directory
1170`py-temp-directory'). If there is no Python interpreter shell
1171running, this file is executed synchronously using
1172`shell-command-on-region'. If the program is long running, use an
1173optional \\[universal-argument] to run the command asynchronously in
1174its own buffer.
1175
1176If the Python interpreter shell is running, the region is execfile()'d
1177in that shell. If you try to execute regions too quickly,
1178`python-mode' will queue them up and execute them one at a time when
1179it sees a `>>> ' prompt from Python. Each time this happens, the
1180process buffer is popped into a window (if it's not already in some
1181window) so you can see it, and a comment of the form
1182
1183 \t## working on region in file <name>...
1184
1185is inserted at the end. See also the command `py-clear-queue'."
1186 (interactive "r\nP")
1187 (or (< start end)
1188 (error "Region is empty"))
1189 (let* ((proc (get-process "Python"))
Barry Warsaw5e21cb01997-11-05 18:41:11 +00001190 (temp (if (memq 'broken-temp-names py-emacs-features)
1191 (prog1
1192 (format "python-%d" py-serial-number)
1193 (setq py-serial-number (1+ py-serial-number)))
1194 (make-temp-name "python")))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001195 (file (concat (file-name-as-directory py-temp-directory) temp)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001196 (write-region start end file nil 'nomsg)
1197 (cond
1198 ;; always run the code in it's own asynchronous subprocess
1199 (async
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001200 (let* ((buf (generate-new-buffer-name py-output-buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001201 (start-process "Python" buf py-python-command "-u" file)
1202 (pop-to-buffer buf)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001203 (py-postprocess-output-buffer buf)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001204 ))
1205 ;; if the Python interpreter shell is running, queue it up for
1206 ;; execution there.
1207 (proc
1208 ;; use the existing python shell
1209 (if (not py-file-queue)
1210 (py-execute-file proc file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001211 (message "File %s queued for execution" file))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001212 (push file py-file-queue)
1213 (setq py-exception-buffer (cons file (current-buffer))))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001214 (t
1215 ;; otherwise either run it synchronously in a subprocess
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001216 (shell-command-on-region start end py-python-command py-output-buffer)
1217 (setq py-exception-buffer (current-buffer))
1218 (py-postprocess-output-buffer py-output-buffer)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001219 ))))
1220
1221;; Code execution command
1222(defun py-execute-buffer (&optional async)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001223 "Send the contents of the buffer to a Python interpreter.
Barry Warsawc0ecb531998-01-20 21:43:34 +00001224If the file local variable `py-master-file' is non-nil, execute the
1225named file instead of the buffer's file.
1226
Barry Warsaw7ae77681994-12-12 20:38:05 +00001227If there is a *Python* process buffer it is used. If a clipping
1228restriction is in effect, only the accessible portion of the buffer is
1229sent. A trailing newline will be supplied if needed.
1230
1231See the `\\[py-execute-region]' docs for an account of some subtleties."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001232 (interactive "P")
Barry Warsawc0ecb531998-01-20 21:43:34 +00001233 (if py-master-file
1234 (let* ((filename (expand-file-name py-master-file))
1235 (buffer (or (get-file-buffer filename)
1236 (find-file-noselect filename))))
1237 (set-buffer buffer)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001238 (py-execute-region (point-min) (point-max) async))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001239
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001240
1241
1242(defun py-jump-to-exception (file line)
1243 (let ((buffer (cond ((string-equal file "<stdin>")
1244 py-exception-buffer)
1245 ((and (consp py-exception-buffer)
1246 (string-equal file (car py-exception-buffer)))
1247 (cdr py-exception-buffer))
1248 ((py-safe (find-file-noselect file)))
1249 ;; could not figure out what file the exception
1250 ;; is pointing to, so prompt for it
1251 (t (find-file (read-file-name "Exception file: "
1252 nil
1253 file t))))))
1254 (pop-to-buffer buffer)
1255 (goto-line line)
1256 (message "Jumping to exception in file %s on line %d" file line)))
1257
1258(defun py-mouseto-exception (event)
1259 (interactive "e")
1260 (cond
1261 ((fboundp 'event-point)
1262 ;; XEmacs
1263 (let* ((point (event-point event))
1264 (buffer (event-buffer event))
1265 (e (and point buffer (extent-at point buffer 'py-exc-info)))
1266 (info (and e (extent-property e 'py-exc-info))))
1267 (message "Event point: %d, info: %s" point info)
1268 (and info
1269 (py-jump-to-exception (car info) (cdr info)))
1270 ))
1271 ;; Emacs -- Please port this!
1272 ))
1273
1274(defun py-goto-exception ()
1275 "Go to the line indicated by the traceback."
1276 (interactive)
1277 (let (file line)
1278 (save-excursion
1279 (beginning-of-line)
1280 (if (looking-at py-traceback-line-re)
1281 (setq file (match-string 1)
1282 line (string-to-int (match-string 2)))))
1283 (if (not file)
1284 (error "Not on a traceback line."))
1285 (py-jump-to-exception file line)))
1286
1287(defun py-find-next-exception (start buffer searchdir errwhere)
1288 ;; Go to start position in buffer, search in the specified
1289 ;; direction, and jump to the exception found. If at the end of the
1290 ;; exception, print error message
1291 (let (file line)
1292 (save-excursion
1293 (set-buffer buffer)
1294 (goto-char (py-point start))
1295 (if (funcall searchdir py-traceback-line-re nil t)
1296 (setq file (match-string 1)
1297 line (string-to-int (match-string 2)))))
1298 (if (and file line)
1299 (py-jump-to-exception file line)
1300 (error "%s of traceback" errwhere))))
1301
1302(defun py-down-exception (&optional bottom)
1303 "Go to the next line down in the traceback.
1304With optional \\[universal-argument], jump to the bottom (innermost)
1305exception in the exception stack."
1306 (interactive "P")
1307 (let* ((proc (get-process "Python"))
1308 (buffer (if proc "*Python*" py-output-buffer)))
1309 (if bottom
1310 (py-find-next-exception 'eob buffer 're-search-backward "Bottom")
1311 (py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
1312
1313(defun py-up-exception (&optional top)
1314 "Go to the previous line up in the traceback.
1315With optional \\[universal-argument], jump to the top (outermost)
1316exception in the exception stack."
1317 (interactive "P")
1318 (let* ((proc (get-process "Python"))
1319 (buffer (if proc "*Python*" py-output-buffer)))
1320 (if top
1321 (py-find-next-exception 'bob buffer 're-search-forward "Top")
Barry Warsawffbc17d1997-11-27 20:08:14 +00001322 (py-find-next-exception 'bol buffer 're-search-backward "Top"))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001323
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001324
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001325;; Electric deletion
1326(defun py-electric-backspace (arg)
1327 "Deletes preceding character or levels of indentation.
1328Deletion is performed by calling the function in `py-backspace-function'
Barry Warsawb0539931996-12-17 22:05:07 +00001329with a single argument (the number of characters to delete).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001330
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001331If point is at the leftmost column, deletes the preceding newline.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001332
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001333Otherwise, if point is at the leftmost non-whitespace character of a
1334line that is neither a continuation line nor a non-indenting comment
1335line, or if point is at the end of a blank line, this command reduces
1336the indentation to match that of the line that opened the current
1337block of code. The line that opened the block is displayed in the
1338echo area to help you keep track of where you are. With numeric arg,
1339outdents that many blocks (but not past column zero).
1340
1341Otherwise the preceding character is deleted, converting a tab to
1342spaces if needed so that only a single column position is deleted.
1343Numeric argument deletes that many preceding characters."
Barry Warsaw03970731996-07-03 23:12:52 +00001344 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +00001345 (if (or (/= (current-indentation) (current-column))
1346 (bolp)
1347 (py-continuation-line-p)
Barry Warsaw6e527d21996-08-01 15:57:48 +00001348 (not py-honor-comment-indentation)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001349 (looking-at "#[^ \t\n]")) ; non-indenting #
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001350 (funcall py-backspace-function arg)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001351 ;; else indent the same as the colon line that opened the block
Barry Warsaw7ae77681994-12-12 20:38:05 +00001352 ;; force non-blank so py-goto-block-up doesn't ignore it
1353 (insert-char ?* 1)
1354 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001355 (let ((base-indent 0) ; indentation of base line
1356 (base-text "") ; and text of base line
1357 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +00001358 (save-excursion
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001359 (while (< 0 arg)
Barry Warsaw03970731996-07-03 23:12:52 +00001360 (condition-case nil ; in case no enclosing block
1361 (progn
1362 (py-goto-block-up 'no-mark)
1363 (setq base-indent (current-indentation)
1364 base-text (py-suck-up-leading-text)
1365 base-found-p t))
1366 (error nil))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001367 (setq arg (1- arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001368 (delete-char 1) ; toss the dummy character
1369 (delete-horizontal-space)
1370 (indent-to base-indent)
1371 (if base-found-p
1372 (message "Closes block: %s" base-text)))))
1373
Barry Warsawfc8a01f1995-03-09 16:07:29 +00001374
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001375(defun py-electric-delete (arg)
1376 "Deletes preceding or following character or levels of whitespace.
1377
1378The behavior of this function depends on the variable
1379`delete-key-deletes-forward'. If this variable is nil (or does not
1380exist, as in older Emacsen), then this function behaves identical to
1381\\[c-electric-backspace].
1382
1383If `delete-key-deletes-forward' is non-nil and is supported in your
1384Emacs, then deletion occurs in the forward direction, by calling the
1385function in `py-delete-function'."
1386 (interactive "*p")
1387 (if (and (boundp 'delete-key-deletes-forward)
1388 delete-key-deletes-forward)
1389 (funcall py-delete-function arg)
1390 ;; else
1391 (py-electric-backspace arg)))
1392
1393;; required for pending-del and delsel modes
1394(put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
1395(put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del
1396(put 'py-electric-delete 'delete-selection 'supersede) ;delsel
1397(put 'py-electric-delete 'pending-delete 'supersede) ;pending-del
1398
1399
1400
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001401(defun py-indent-line (&optional arg)
1402 "Fix the indentation of the current line according to Python rules.
1403With \\[universal-argument], ignore outdenting rules for block
1404closing statements (e.g. return, raise, break, continue, pass)
1405
1406This function is normally bound to `indent-line-function' so
1407\\[indent-for-tab-command] will call it."
1408 (interactive "P")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001409 (let* ((ci (current-indentation))
1410 (move-to-indentation-p (<= (current-column) ci))
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001411 (need (py-compute-indentation (not arg))))
Barry Warsaw4f009fb1995-03-14 20:53:08 +00001412 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001413 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +00001414 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001415 (if (/= ci need)
1416 (save-excursion
1417 (beginning-of-line)
1418 (delete-horizontal-space)
1419 (indent-to need)))
1420 (if move-to-indentation-p (back-to-indentation))))
1421
1422(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001423 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001424This is just `strives to' because correct indentation can't be computed
1425from scratch for Python code. In general, deletes the whitespace before
1426point, inserts a newline, and takes an educated guess as to how you want
1427the new line indented."
1428 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001429 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001430 (if (< ci (current-column)) ; if point beyond indentation
1431 (newline-and-indent)
1432 ;; else try to act like newline-and-indent "normally" acts
1433 (beginning-of-line)
1434 (insert-char ?\n 1)
1435 (move-to-column ci))))
1436
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001437(defun py-compute-indentation (honor-block-close-p)
1438 ;; implements all the rules for indentation computation. when
1439 ;; honor-block-close-p is non-nil, statements such as return, raise,
1440 ;; break, continue, and pass force one level of outdenting.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001441 (save-excursion
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001442 (let* ((bod (py-point 'bod))
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001443 (pps (parse-partial-sexp bod (point)))
1444 (boipps (parse-partial-sexp bod (py-point 'boi))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001445 (beginning-of-line)
1446 (cond
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001447 ;; are we inside a multi-line string or comment?
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00001448 ((or (and (nth 3 pps) (nth 3 boipps))
1449 (and (nth 4 pps) (nth 4 boipps)))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001450 (save-excursion
1451 (if (not py-align-multiline-strings-p) 0
1452 ;; skip back over blank & non-indenting comment lines
1453 ;; note: will skip a blank or non-indenting comment line
1454 ;; that happens to be a continuation line too
1455 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1456 (back-to-indentation)
1457 (current-column))))
1458 ;; are we on a continuation line?
1459 ((py-continuation-line-p)
1460 (let ((startpos (point))
1461 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001462 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001463 (if open-bracket-pos
1464 (progn
1465 ;; align with first item in list; else a normal
1466 ;; indent beyond the line with the open bracket
1467 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1468 ;; is the first list item on the same line?
1469 (skip-chars-forward " \t")
1470 (if (null (memq (following-char) '(?\n ?# ?\\)))
1471 ; yes, so line up with it
1472 (current-column)
1473 ;; first list item on another line, or doesn't exist yet
1474 (forward-line 1)
1475 (while (and (< (point) startpos)
1476 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1477 (forward-line 1))
1478 (if (< (point) startpos)
1479 ;; again mimic the first list item
1480 (current-indentation)
1481 ;; else they're about to enter the first item
1482 (goto-char open-bracket-pos)
1483 (+ (current-indentation) py-indent-offset))))
1484
1485 ;; else on backslash continuation line
1486 (forward-line -1)
1487 (if (py-continuation-line-p) ; on at least 3rd line in block
1488 (current-indentation) ; so just continue the pattern
1489 ;; else started on 2nd line in block, so indent more.
1490 ;; if base line is an assignment with a start on a RHS,
1491 ;; indent to 2 beyond the leftmost "="; else skip first
1492 ;; chunk of non-whitespace characters on base line, + 1 more
1493 ;; column
1494 (end-of-line)
1495 (setq endpos (point) searching t)
1496 (back-to-indentation)
1497 (setq startpos (point))
1498 ;; look at all "=" from left to right, stopping at first
1499 ;; one not nested in a list or string
1500 (while searching
1501 (skip-chars-forward "^=" endpos)
1502 (if (= (point) endpos)
1503 (setq searching nil)
1504 (forward-char 1)
1505 (setq state (parse-partial-sexp startpos (point)))
1506 (if (and (zerop (car state)) ; not in a bracket
1507 (null (nth 3 state))) ; & not in a string
1508 (progn
1509 (setq searching nil) ; done searching in any case
1510 (setq found
1511 (not (or
1512 (eq (following-char) ?=)
1513 (memq (char-after (- (point) 2))
1514 '(?< ?> ?!)))))))))
1515 (if (or (not found) ; not an assignment
1516 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1517 (progn
1518 (goto-char startpos)
1519 (skip-chars-forward "^ \t\n")))
1520 (1+ (current-column))))))
1521
1522 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001523 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001524
Barry Warsawa7891711996-08-01 15:53:09 +00001525 ;; Dfn: "Indenting comment line". A line containing only a
1526 ;; comment, but which is treated like a statement for
1527 ;; indentation calculation purposes. Such lines are only
1528 ;; treated specially by the mode; they are not treated
1529 ;; specially by the Python interpreter.
1530
1531 ;; The rules for indenting comment lines are a line where:
1532 ;; - the first non-whitespace character is `#', and
1533 ;; - the character following the `#' is whitespace, and
1534 ;; - the line is outdented with respect to (i.e. to the left
1535 ;; of) the indentation of the preceding non-blank line.
1536
1537 ;; The first non-blank line following an indenting comment
1538 ;; line is given the same amount of indentation as the
1539 ;; indenting comment line.
1540
1541 ;; All other comment-only lines are ignored for indentation
1542 ;; purposes.
1543
1544 ;; Are we looking at a comment-only line which is *not* an
1545 ;; indenting comment line? If so, we assume that its been
1546 ;; placed at the desired indentation, so leave it alone.
1547 ;; Indenting comment lines are aligned as statements down
1548 ;; below.
1549 ((and (looking-at "[ \t]*#[^ \t\n]")
1550 ;; NOTE: this test will not be performed in older Emacsen
1551 (fboundp 'forward-comment)
1552 (<= (current-indentation)
1553 (save-excursion
1554 (forward-comment (- (point-max)))
1555 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001556 (current-indentation))
1557
1558 ;; else indentation based on that of the statement that
1559 ;; precedes us; use the first line of that statement to
1560 ;; establish the base, in case the user forced a non-std
1561 ;; indentation for the continuation lines (if any)
1562 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001563 ;; skip back over blank & non-indenting comment lines note:
1564 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001565 ;; happens to be a continuation line too. use fast Emacs 19
1566 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001567 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001568 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001569 (forward-comment (- (point-max)))
Barry Warsaw6d627751996-03-06 18:41:38 +00001570 (let (done)
1571 (while (not done)
1572 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
1573 nil 'move)
1574 (setq done (or (eq py-honor-comment-indentation t)
1575 (bobp)
1576 (/= (following-char) ?#)
1577 (not (zerop (current-column)))))
1578 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001579 ;; if we landed inside a string, go to the beginning of that
1580 ;; string. this handles triple quoted, multi-line spanning
1581 ;; strings.
1582 (py-goto-initial-line)
Barry Warsawf831d811996-07-31 20:42:59 +00001583 (+ (current-indentation)
1584 (if (py-statement-opens-block-p)
1585 py-indent-offset
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001586 (if (and honor-block-close-p (py-statement-closes-block-p))
Barry Warsawf831d811996-07-31 20:42:59 +00001587 (- py-indent-offset)
1588 0)))
1589 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001590
1591(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001592 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001593By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001594`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +00001595Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001596`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +00001597their own buffer-local copy), both those currently existing and those
1598created later in the Emacs session.
1599
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001600Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001601There's no excuse for such foolishness, but sometimes you have to deal
1602with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001603`py-indent-offset' to what it thinks it was when they created the
1604mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001605
1606Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001607looking for a line that opens a block of code. `py-indent-offset' is
1608set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001609statement following it. If the search doesn't succeed going forward,
1610it's tried again going backward."
1611 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001612 (let (new-value
1613 (start (point))
1614 restart
1615 (found nil)
1616 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001617 (py-goto-initial-line)
1618 (while (not (or found (eobp)))
1619 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1620 (progn
1621 (setq restart (point))
1622 (py-goto-initial-line)
1623 (if (py-statement-opens-block-p)
1624 (setq found t)
1625 (goto-char restart)))))
1626 (if found
1627 ()
1628 (goto-char start)
1629 (py-goto-initial-line)
1630 (while (not (or found (bobp)))
1631 (setq found
1632 (and
1633 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1634 (or (py-goto-initial-line) t) ; always true -- side effect
1635 (py-statement-opens-block-p)))))
1636 (setq colon-indent (current-indentation)
1637 found (and found (zerop (py-next-statement 1)))
1638 new-value (- (current-indentation) colon-indent))
1639 (goto-char start)
1640 (if found
1641 (progn
1642 (funcall (if global 'kill-local-variable 'make-local-variable)
1643 'py-indent-offset)
1644 (setq py-indent-offset new-value)
1645 (message "%s value of py-indent-offset set to %d"
1646 (if global "Global" "Local")
1647 py-indent-offset))
1648 (error "Sorry, couldn't guess a value for py-indent-offset"))))
1649
1650(defun py-shift-region (start end count)
1651 (save-excursion
1652 (goto-char end) (beginning-of-line) (setq end (point))
1653 (goto-char start) (beginning-of-line) (setq start (point))
1654 (indent-rigidly start end count)))
1655
1656(defun py-shift-region-left (start end &optional count)
1657 "Shift region of Python code to the left.
1658The lines from the line containing the start of the current region up
1659to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001660shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001661
1662If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001663many columns. With no active region, outdent only the current line.
1664You cannot outdent the region if any line is already at column zero."
1665 (interactive
1666 (let ((p (point))
1667 (m (mark))
1668 (arg current-prefix-arg))
1669 (if m
1670 (list (min p m) (max p m) arg)
1671 (list p (save-excursion (forward-line 1) (point)) arg))))
1672 ;; if any line is at column zero, don't shift the region
1673 (save-excursion
1674 (goto-char start)
1675 (while (< (point) end)
1676 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00001677 (if (and (zerop (current-column))
1678 (not (looking-at "\\s *$")))
Barry Warsawdea4a291996-07-03 22:59:12 +00001679 (error "Region is at left edge."))
1680 (forward-line 1)))
1681 (py-shift-region start end (- (prefix-numeric-value
1682 (or count py-indent-offset))))
1683 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001684
1685(defun py-shift-region-right (start end &optional count)
1686 "Shift region of Python code to the right.
1687The lines from the line containing the start of the current region up
1688to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001689shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001690
1691If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001692many columns. With no active region, indent only the current line."
1693 (interactive
1694 (let ((p (point))
1695 (m (mark))
1696 (arg current-prefix-arg))
1697 (if m
1698 (list (min p m) (max p m) arg)
1699 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001700 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00001701 (or count py-indent-offset)))
1702 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001703
1704(defun py-indent-region (start end &optional indent-offset)
1705 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00001706
Barry Warsaw7ae77681994-12-12 20:38:05 +00001707The lines from the line containing the start of the current region up
1708to (but not including) the line containing the end of the region are
1709reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001710character in the first column, the first line is left alone and the
1711rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00001712region is reindented with respect to the (closest code or indenting
1713comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001714
1715This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001716control structures are introduced or removed, or to reformat code
1717using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001718
1719If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001720the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00001721used.
1722
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001723Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00001724is called! This function does not compute proper indentation from
1725scratch (that's impossible in Python), it merely adjusts the existing
1726indentation to be correct in context.
1727
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001728Warning: This function really has no idea what to do with
1729non-indenting comment lines, and shifts them as if they were indenting
1730comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001731
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001732Special cases: whitespace is deleted from blank lines; continuation
1733lines are shifted by the same amount their initial line was shifted,
1734in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00001735initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001736 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001737 (save-excursion
1738 (goto-char end) (beginning-of-line) (setq end (point-marker))
1739 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001740 (let ((py-indent-offset (prefix-numeric-value
1741 (or indent-offset py-indent-offset)))
1742 (indents '(-1)) ; stack of active indent levels
1743 (target-column 0) ; column to which to indent
1744 (base-shifted-by 0) ; amount last base line was shifted
1745 (indent-base (if (looking-at "[ \t\n]")
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001746 (py-compute-indentation t)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001747 0))
1748 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001749 (while (< (point) end)
1750 (setq ci (current-indentation))
1751 ;; figure out appropriate target column
1752 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001753 ((or (eq (following-char) ?#) ; comment in column 1
1754 (looking-at "[ \t]*$")) ; entirely blank
1755 (setq target-column 0))
1756 ((py-continuation-line-p) ; shift relative to base line
1757 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001758 (t ; new base line
1759 (if (> ci (car indents)) ; going deeper; push it
1760 (setq indents (cons ci indents))
1761 ;; else we should have seen this indent before
1762 (setq indents (memq ci indents)) ; pop deeper indents
1763 (if (null indents)
1764 (error "Bad indentation in region, at line %d"
1765 (save-restriction
1766 (widen)
1767 (1+ (count-lines 1 (point)))))))
1768 (setq target-column (+ indent-base
1769 (* py-indent-offset
1770 (- (length indents) 2))))
1771 (setq base-shifted-by (- target-column ci))))
1772 ;; shift as needed
1773 (if (/= ci target-column)
1774 (progn
1775 (delete-horizontal-space)
1776 (indent-to target-column)))
1777 (forward-line 1))))
1778 (set-marker end nil))
1779
Barry Warsawa7891711996-08-01 15:53:09 +00001780(defun py-comment-region (beg end &optional arg)
1781 "Like `comment-region' but uses double hash (`#') comment starter."
1782 (interactive "r\nP")
Barry Warsaw3fcaf611996-08-01 20:11:51 +00001783 (let ((comment-start py-block-comment-prefix))
Barry Warsawa7891711996-08-01 15:53:09 +00001784 (comment-region beg end arg)))
1785
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001786
1787;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00001788(defun py-previous-statement (count)
1789 "Go to the start of previous Python statement.
1790If the statement at point is the i'th Python statement, goes to the
1791start of statement i-COUNT. If there is no such statement, goes to the
1792first statement. Returns count of statements left to move.
1793`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001794 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001795 (if (< count 0) (py-next-statement (- count))
1796 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001797 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001798 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001799 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001800 (> count 0)
1801 (zerop (forward-line -1))
1802 (py-goto-statement-at-or-above))
1803 (setq count (1- count)))
1804 (if (> count 0) (goto-char start)))
1805 count))
1806
1807(defun py-next-statement (count)
1808 "Go to the start of next Python statement.
1809If the statement at point is the i'th Python statement, goes to the
1810start of statement i+COUNT. If there is no such statement, goes to the
1811last statement. Returns count of statements left to move. `Statements'
1812do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001813 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001814 (if (< count 0) (py-previous-statement (- count))
1815 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001816 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001817 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001818 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001819 (> count 0)
1820 (py-goto-statement-below))
1821 (setq count (1- count)))
1822 (if (> count 0) (goto-char start)))
1823 count))
1824
1825(defun py-goto-block-up (&optional nomark)
1826 "Move up to start of current block.
1827Go to the statement that starts the smallest enclosing block; roughly
1828speaking, this will be the closest preceding statement that ends with a
1829colon and is indented less than the statement you started on. If
1830successful, also sets the mark to the starting point.
1831
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001832`\\[py-mark-block]' can be used afterward to mark the whole code
1833block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001834
1835If called from a program, the mark will not be set if optional argument
1836NOMARK is not nil."
1837 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001838 (let ((start (point))
1839 (found nil)
1840 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001841 (py-goto-initial-line)
1842 ;; if on blank or non-indenting comment line, use the preceding stmt
1843 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1844 (progn
1845 (py-goto-statement-at-or-above)
1846 (setq found (py-statement-opens-block-p))))
1847 ;; search back for colon line indented less
1848 (setq initial-indent (current-indentation))
1849 (if (zerop initial-indent)
1850 ;; force fast exit
1851 (goto-char (point-min)))
1852 (while (not (or found (bobp)))
1853 (setq found
1854 (and
1855 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1856 (or (py-goto-initial-line) t) ; always true -- side effect
1857 (< (current-indentation) initial-indent)
1858 (py-statement-opens-block-p))))
1859 (if found
1860 (progn
1861 (or nomark (push-mark start))
1862 (back-to-indentation))
1863 (goto-char start)
1864 (error "Enclosing block not found"))))
1865
1866(defun beginning-of-python-def-or-class (&optional class)
1867 "Move point to start of def (or class, with prefix arg).
1868
1869Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001870arg, looks for a `class' instead. The docs assume the `def' case;
1871just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001872
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001873If point is in a def statement already, and after the `d', simply
1874moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001875
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001876Else (point is not in a def statement, or at or before the `d' of a
1877def statement), searches for the closest preceding def statement, and
1878leaves point at its start. If no such statement can be found, leaves
1879point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001880
1881Returns t iff a def statement is found by these rules.
1882
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001883Note that doing this command repeatedly will take you closer to the
1884start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001885
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001886If you want to mark the current def/class, see
Barry Warsaw2518c671997-11-05 00:51:08 +00001887`\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001888 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001889 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1890 (start-of-line (progn (beginning-of-line) (point)))
1891 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001892 (if (or (/= start-of-stmt start-of-line)
1893 (not at-or-before-p))
1894 (end-of-line)) ; OK to match on this line
1895 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001896 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001897
1898(defun end-of-python-def-or-class (&optional class)
1899 "Move point beyond end of def (or class, with prefix arg) body.
1900
1901By default, looks for an appropriate `def'. If you supply a prefix arg,
1902looks for a `class' instead. The docs assume the `def' case; just
1903substitute `class' for `def' for the other case.
1904
1905If point is in a def statement already, this is the def we use.
1906
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001907Else if the def found by `\\[beginning-of-python-def-or-class]'
1908contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001909
1910Else we search forward for the closest following def, and use that.
1911
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001912If a def can be found by these rules, point is moved to the start of
1913the line immediately following the def block, and the position of the
1914start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001915
1916Else point is moved to the end of the buffer, and nil is returned.
1917
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001918Note that doing this command repeatedly will take you closer to the
1919end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001920
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001921If you want to mark the current def/class, see
Barry Warsaw2518c671997-11-05 00:51:08 +00001922`\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001923 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001924 (let ((start (progn (py-goto-initial-line) (point)))
1925 (which (if class "class" "def"))
1926 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001927 ;; move point to start of appropriate def/class
1928 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1929 (setq state 'at-beginning)
1930 ;; else see if beginning-of-python-def-or-class hits container
1931 (if (and (beginning-of-python-def-or-class class)
1932 (progn (py-goto-beyond-block)
1933 (> (point) start)))
1934 (setq state 'at-end)
1935 ;; else search forward
1936 (goto-char start)
1937 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1938 (progn (setq state 'at-beginning)
1939 (beginning-of-line)))))
1940 (cond
1941 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1942 ((eq state 'at-end) t)
1943 ((eq state 'not-found) nil)
1944 (t (error "internal error in end-of-python-def-or-class")))))
1945
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001946
1947;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001948(defun py-mark-block (&optional extend just-move)
1949 "Mark following block of lines. With prefix arg, mark structure.
1950Easier to use than explain. It sets the region to an `interesting'
1951block of succeeding lines. If point is on a blank line, it goes down to
1952the next non-blank line. That will be the start of the region. The end
1953of the region depends on the kind of line at the start:
1954
1955 - If a comment, the region will include all succeeding comment lines up
1956 to (but not including) the next non-comment line (if any).
1957
1958 - Else if a prefix arg is given, and the line begins one of these
1959 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001960
1961 if elif else try except finally for while def class
1962
Barry Warsaw7ae77681994-12-12 20:38:05 +00001963 the region will be set to the body of the structure, including
1964 following blocks that `belong' to it, but excluding trailing blank
1965 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001966 and all (if any) of the following `except' and `finally' blocks
1967 that belong to the `try' structure will be in the region. Ditto
1968 for if/elif/else, for/else and while/else structures, and (a bit
1969 degenerate, since they're always one-block structures) def and
1970 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001971
1972 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001973 block (see list above), and the block is not a `one-liner' (i.e.,
1974 the statement ends with a colon, not with code), the region will
1975 include all succeeding lines up to (but not including) the next
1976 code statement (if any) that's indented no more than the starting
1977 line, except that trailing blank and comment lines are excluded.
1978 E.g., if the starting line begins a multi-statement `def'
1979 structure, the region will be set to the full function definition,
1980 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001981
1982 - Else the region will include all succeeding lines up to (but not
1983 including) the next blank line, or code or indenting-comment line
1984 indented strictly less than the starting line. Trailing indenting
1985 comment lines are included in this case, but not trailing blank
1986 lines.
1987
1988A msg identifying the location of the mark is displayed in the echo
1989area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1990
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001991If called from a program, optional argument EXTEND plays the role of
1992the prefix arg, and if optional argument JUST-MOVE is not nil, just
1993moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001994 (interactive "P") ; raw prefix arg
1995 (py-goto-initial-line)
1996 ;; skip over blank lines
1997 (while (and
1998 (looking-at "[ \t]*$") ; while blank line
1999 (not (eobp))) ; & somewhere to go
2000 (forward-line 1))
2001 (if (eobp)
2002 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002003 (let ((initial-pos (point))
2004 (initial-indent (current-indentation))
2005 last-pos ; position of last stmt in region
2006 (followers
2007 '((if elif else) (elif elif else) (else)
2008 (try except finally) (except except) (finally)
2009 (for else) (while else)
2010 (def) (class) ) )
2011 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002012
2013 (cond
2014 ;; if comment line, suck up the following comment lines
2015 ((looking-at "[ \t]*#")
2016 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
2017 (re-search-backward "^[ \t]*#") ; and back to last comment in block
2018 (setq last-pos (point)))
2019
2020 ;; else if line is a block line and EXTEND given, suck up
2021 ;; the whole structure
2022 ((and extend
2023 (setq first-symbol (py-suck-up-first-keyword) )
2024 (assq first-symbol followers))
2025 (while (and
2026 (or (py-goto-beyond-block) t) ; side effect
2027 (forward-line -1) ; side effect
2028 (setq last-pos (point)) ; side effect
2029 (py-goto-statement-below)
2030 (= (current-indentation) initial-indent)
2031 (setq next-symbol (py-suck-up-first-keyword))
2032 (memq next-symbol (cdr (assq first-symbol followers))))
2033 (setq first-symbol next-symbol)))
2034
2035 ;; else if line *opens* a block, search for next stmt indented <=
2036 ((py-statement-opens-block-p)
2037 (while (and
2038 (setq last-pos (point)) ; always true -- side effect
2039 (py-goto-statement-below)
2040 (> (current-indentation) initial-indent))
2041 nil))
2042
2043 ;; else plain code line; stop at next blank line, or stmt or
2044 ;; indenting comment line indented <
2045 (t
2046 (while (and
2047 (setq last-pos (point)) ; always true -- side effect
2048 (or (py-goto-beyond-final-line) t)
2049 (not (looking-at "[ \t]*$")) ; stop at blank line
2050 (or
2051 (>= (current-indentation) initial-indent)
2052 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
2053 nil)))
2054
2055 ;; skip to end of last stmt
2056 (goto-char last-pos)
2057 (py-goto-beyond-final-line)
2058
2059 ;; set mark & display
2060 (if just-move
2061 () ; just return
2062 (push-mark (point) 'no-msg)
2063 (forward-line -1)
2064 (message "Mark set after: %s" (py-suck-up-leading-text))
2065 (goto-char initial-pos))))
2066
Barry Warsaw2518c671997-11-05 00:51:08 +00002067(defun py-mark-def-or-class (&optional class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002068 "Set region to body of def (or class, with prefix arg) enclosing point.
2069Pushes the current mark, then point, on the mark ring (all language
2070modes do this, but although it's handy it's never documented ...).
2071
2072In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002073hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
2074`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002075
2076And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002077Turned out that was more confusing than useful: the `goto start' and
2078`goto end' commands are usually used to search through a file, and
2079people expect them to act a lot like `search backward' and `search
2080forward' string-search commands. But because Python `def' and `class'
2081can nest to arbitrary levels, finding the smallest def containing
2082point cannot be done via a simple backward search: the def containing
2083point may not be the closest preceding def, or even the closest
2084preceding def that's indented less. The fancy algorithm required is
2085appropriate for the usual uses of this `mark' command, but not for the
2086`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002087
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002088So the def marked by this command may not be the one either of the
2089`goto' commands find: If point is on a blank or non-indenting comment
2090line, moves back to start of the closest preceding code statement or
2091indenting comment line. If this is a `def' statement, that's the def
2092we use. Else searches for the smallest enclosing `def' block and uses
2093that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002094
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002095When an enclosing def is found: The mark is left immediately beyond
2096the last line of the def block. Point is left at the start of the
2097def, except that: if the def is preceded by a number of comment lines
2098followed by (at most) one optional blank line, point is left at the
2099start of the comments; else if the def is preceded by a blank line,
2100point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002101
2102The intent is to mark the containing def/class and its associated
2103documentation, to make moving and duplicating functions and classes
2104pleasant."
2105 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002106 (let ((start (point))
2107 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002108 (push-mark start)
2109 (if (not (py-go-up-tree-to-keyword which))
2110 (progn (goto-char start)
2111 (error "Enclosing %s not found" which))
2112 ;; else enclosing def/class found
2113 (setq start (point))
2114 (py-goto-beyond-block)
2115 (push-mark (point))
2116 (goto-char start)
2117 (if (zerop (forward-line -1)) ; if there is a preceding line
2118 (progn
2119 (if (looking-at "[ \t]*$") ; it's blank
2120 (setq start (point)) ; so reset start point
2121 (goto-char start)) ; else try again
2122 (if (zerop (forward-line -1))
2123 (if (looking-at "[ \t]*#") ; a comment
2124 ;; look back for non-comment line
2125 ;; tricky: note that the regexp matches a blank
2126 ;; line, cuz \n is in the 2nd character class
2127 (and
2128 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
2129 (forward-line 1))
2130 ;; no comment, so go back
Barry Warsaw4da6bd51997-11-26 06:00:26 +00002131 (goto-char start)))))))
2132 (exchange-point-and-mark)
2133 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002134
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002135;; ripped from cc-mode
2136(defun py-forward-into-nomenclature (&optional arg)
2137 "Move forward to end of a nomenclature section or word.
2138With arg, to it arg times.
2139
2140A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2141 (interactive "p")
2142 (let ((case-fold-search nil))
2143 (if (> arg 0)
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002144 (re-search-forward
2145 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
2146 (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002147 (while (and (< arg 0)
2148 (re-search-backward
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002149 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002150 (point-min) 0))
2151 (forward-char 1)
2152 (setq arg (1+ arg)))))
2153 (py-keep-region-active))
2154
2155(defun py-backward-into-nomenclature (&optional arg)
2156 "Move backward to beginning of a nomenclature section or word.
2157With optional ARG, move that many times. If ARG is negative, move
2158forward.
2159
2160A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2161 (interactive "p")
2162 (py-forward-into-nomenclature (- arg))
2163 (py-keep-region-active))
2164
2165
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002166
2167;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002168
2169;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002170;; plus lines of the form ^[vc]:name$ to suck variable & command docs
2171;; out of the right places, along with the keys they're on & current
2172;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00002173(defun py-dump-help-string (str)
2174 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002175 (let ((locals (buffer-local-variables))
2176 funckind funcname func funcdoc
2177 (start 0) mstart end
2178 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002179 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
2180 (setq mstart (match-beginning 0) end (match-end 0)
2181 funckind (substring str (match-beginning 1) (match-end 1))
2182 funcname (substring str (match-beginning 2) (match-end 2))
2183 func (intern funcname))
2184 (princ (substitute-command-keys (substring str start mstart)))
2185 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002186 ((equal funckind "c") ; command
2187 (setq funcdoc (documentation func)
2188 keys (concat
2189 "Key(s): "
2190 (mapconcat 'key-description
2191 (where-is-internal func py-mode-map)
2192 ", "))))
2193 ((equal funckind "v") ; variable
Barry Warsaw604cefa1996-09-03 18:17:04 +00002194 (setq funcdoc (documentation-property func 'variable-documentation)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002195 keys (if (assq func locals)
2196 (concat
2197 "Local/Global values: "
2198 (prin1-to-string (symbol-value func))
2199 " / "
2200 (prin1-to-string (default-value func)))
2201 (concat
2202 "Value: "
2203 (prin1-to-string (symbol-value func))))))
2204 (t ; unexpected
2205 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002206 (princ (format "\n-> %s:\t%s\t%s\n\n"
2207 (if (equal funckind "c") "Command" "Variable")
2208 funcname keys))
2209 (princ funcdoc)
2210 (terpri)
2211 (setq start end))
2212 (princ (substitute-command-keys (substring str start))))
2213 (print-help-return-message)))
2214
2215(defun py-describe-mode ()
2216 "Dump long form of Python-mode docs."
2217 (interactive)
2218 (py-dump-help-string "Major mode for editing Python files.
2219Knows about Python indentation, tokens, comments and continuation lines.
2220Paragraphs are separated by blank lines only.
2221
2222Major sections below begin with the string `@'; specific function and
2223variable docs begin with `->'.
2224
2225@EXECUTING PYTHON CODE
2226
2227\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
2228\\[py-execute-region]\tsends the current region
2229\\[py-shell]\tstarts a Python interpreter window; this will be used by
2230\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
2231%c:py-execute-buffer
2232%c:py-execute-region
2233%c:py-shell
2234
2235@VARIABLES
2236
2237py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00002238py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002239
2240py-python-command\tshell command to invoke Python interpreter
2241py-scroll-process-buffer\talways scroll Python process buffer
2242py-temp-directory\tdirectory used for temp files (if needed)
2243
2244py-beep-if-tab-change\tring the bell if tab-width is changed
2245%v:py-indent-offset
2246%v:py-block-comment-prefix
2247%v:py-python-command
2248%v:py-scroll-process-buffer
2249%v:py-temp-directory
2250%v:py-beep-if-tab-change
2251
2252@KINDS OF LINES
2253
2254Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002255preceding line ends with a backslash that's not part of a comment, or
2256the paren/bracket/brace nesting level at the start of the line is
2257non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002258
2259An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002260possibly blanks or tabs), a `comment line' (leftmost non-blank
2261character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00002262
2263Comment Lines
2264
2265Although all comment lines are treated alike by Python, Python mode
2266recognizes two kinds that act differently with respect to indentation.
2267
2268An `indenting comment line' is a comment line with a blank, tab or
2269nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002270treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00002271indenting comment line will be indented like the comment line. All
2272other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002273following the initial `#') are `non-indenting comment lines', and
2274their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002275
2276Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002277whenever possible. Non-indenting comment lines are useful in cases
2278like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00002279
2280\ta = b # a very wordy single-line comment that ends up being
2281\t #... continued onto another line
2282
2283\tif a == b:
2284##\t\tprint 'panic!' # old code we've `commented out'
2285\t\treturn a
2286
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002287Since the `#...' and `##' comment lines have a non-whitespace
2288character following the initial `#', Python mode ignores them when
2289computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002290
2291Continuation Lines and Statements
2292
2293The Python-mode commands generally work on statements instead of on
2294individual lines, where a `statement' is a comment or blank line, or a
2295code line and all of its following continuation lines (if any)
2296considered as a single logical unit. The commands in this mode
2297generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002298statement containing point, even if point happens to be in the middle
2299of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002300
2301
2302@INDENTATION
2303
2304Primarily for entering new code:
2305\t\\[indent-for-tab-command]\t indent line appropriately
2306\t\\[py-newline-and-indent]\t insert newline, then indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002307\t\\[py-electric-backspace]\t reduce indentation, or delete single character
Barry Warsaw7ae77681994-12-12 20:38:05 +00002308
2309Primarily for reindenting existing code:
2310\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
2311\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
2312
2313\t\\[py-indent-region]\t reindent region to match its context
2314\t\\[py-shift-region-left]\t shift region left by py-indent-offset
2315\t\\[py-shift-region-right]\t shift region right by py-indent-offset
2316
2317Unlike most programming languages, Python uses indentation, and only
2318indentation, to specify block structure. Hence the indentation supplied
2319automatically by Python-mode is just an educated guess: only you know
2320the block structure you intend, so only you can supply correct
2321indentation.
2322
2323The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
2324the indentation of preceding statements. E.g., assuming
2325py-indent-offset is 4, after you enter
2326\tif a > 0: \\[py-newline-and-indent]
2327the cursor will be moved to the position of the `_' (_ is not a
2328character in the file, it's just used here to indicate the location of
2329the cursor):
2330\tif a > 0:
2331\t _
2332If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
2333to
2334\tif a > 0:
2335\t c = d
2336\t _
2337Python-mode cannot know whether that's what you intended, or whether
2338\tif a > 0:
2339\t c = d
2340\t_
2341was your intent. In general, Python-mode either reproduces the
2342indentation of the (closest code or indenting-comment) preceding
2343statement, or adds an extra py-indent-offset blanks if the preceding
2344statement has `:' as its last significant (non-whitespace and non-
2345comment) character. If the suggested indentation is too much, use
Barry Warsaw27ee1151997-12-03 05:03:44 +00002346\\[py-electric-backspace] to reduce it.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002347
2348Continuation lines are given extra indentation. If you don't like the
2349suggested indentation, change it to something you do like, and Python-
2350mode will strive to indent later lines of the statement in the same way.
2351
2352If a line is a continuation line by virtue of being in an unclosed
2353paren/bracket/brace structure (`list', for short), the suggested
2354indentation depends on whether the current line contains the first item
2355in the list. If it does, it's indented py-indent-offset columns beyond
2356the indentation of the line containing the open bracket. If you don't
2357like that, change it by hand. The remaining items in the list will mimic
2358whatever indentation you give to the first item.
2359
2360If a line is a continuation line because the line preceding it ends with
2361a backslash, the third and following lines of the statement inherit their
2362indentation from the line preceding them. The indentation of the second
2363line in the statement depends on the form of the first (base) line: if
2364the base line is an assignment statement with anything more interesting
2365than the backslash following the leftmost assigning `=', the second line
2366is indented two columns beyond that `='. Else it's indented to two
2367columns beyond the leftmost solid chunk of non-whitespace characters on
2368the base line.
2369
2370Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
2371repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
2372structure you intend.
2373%c:indent-for-tab-command
2374%c:py-newline-and-indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00002375%c:py-electric-backspace
Barry Warsaw7ae77681994-12-12 20:38:05 +00002376
2377
2378The next function may be handy when editing code you didn't write:
2379%c:py-guess-indent-offset
2380
2381
2382The remaining `indent' functions apply to a region of Python code. They
2383assume the block structure (equals indentation, in Python) of the region
2384is correct, and alter the indentation in various ways while preserving
2385the block structure:
2386%c:py-indent-region
2387%c:py-shift-region-left
2388%c:py-shift-region-right
2389
2390@MARKING & MANIPULATING REGIONS OF CODE
2391
2392\\[py-mark-block]\t mark block of lines
Barry Warsaw2518c671997-11-05 00:51:08 +00002393\\[py-mark-def-or-class]\t mark smallest enclosing def
2394\\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002395\\[comment-region]\t comment out region of code
2396\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00002397%c:py-mark-block
Barry Warsaw2518c671997-11-05 00:51:08 +00002398%c:py-mark-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00002399%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00002400
2401@MOVING POINT
2402
2403\\[py-previous-statement]\t move to statement preceding point
2404\\[py-next-statement]\t move to statement following point
2405\\[py-goto-block-up]\t move up to start of current block
2406\\[beginning-of-python-def-or-class]\t move to start of def
2407\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
2408\\[end-of-python-def-or-class]\t move to end of def
2409\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
2410
2411The first two move to one statement beyond the statement that contains
2412point. A numeric prefix argument tells them to move that many
2413statements instead. Blank lines, comment lines, and continuation lines
2414do not count as `statements' for these commands. So, e.g., you can go
2415to the first code statement in a file by entering
2416\t\\[beginning-of-buffer]\t to move to the top of the file
2417\t\\[py-next-statement]\t to skip over initial comments and blank lines
2418Or do `\\[py-previous-statement]' with a huge prefix argument.
2419%c:py-previous-statement
2420%c:py-next-statement
2421%c:py-goto-block-up
2422%c:beginning-of-python-def-or-class
2423%c:end-of-python-def-or-class
2424
2425@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2426
2427`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2428
2429`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2430overall class and def structure of a module.
2431
2432`\\[back-to-indentation]' moves point to a line's first non-blank character.
2433
2434`\\[indent-relative]' is handy for creating odd indentation.
2435
2436@OTHER EMACS HINTS
2437
2438If you don't like the default value of a variable, change its value to
2439whatever you do like by putting a `setq' line in your .emacs file.
2440E.g., to set the indentation increment to 4, put this line in your
2441.emacs:
2442\t(setq py-indent-offset 4)
2443To see the value of a variable, do `\\[describe-variable]' and enter the variable
2444name at the prompt.
2445
2446When entering a key sequence like `C-c C-n', it is not necessary to
2447release the CONTROL key after doing the `C-c' part -- it suffices to
2448press the CONTROL key, press and release `c' (while still holding down
2449CONTROL), press and release `n' (while still holding down CONTROL), &
2450then release CONTROL.
2451
2452Entering Python mode calls with no arguments the value of the variable
2453`python-mode-hook', if that value exists and is not nil; for backward
2454compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2455the Elisp manual for details.
2456
2457Obscure: When python-mode is first loaded, it looks for all bindings
2458to newline-and-indent in the global keymap, and shadows them with
2459local bindings to py-newline-and-indent."))
2460
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002461
2462;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002463(defvar py-parse-state-re
2464 (concat
2465 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2466 "\\|"
2467 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002468
Barry Warsaw7ae77681994-12-12 20:38:05 +00002469;; returns the parse state at point (see parse-partial-sexp docs)
2470(defun py-parse-state ()
2471 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002472 (let ((here (point))
Barry Warsaw170ffa71996-07-31 20:57:22 +00002473 pps done ci)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002474 (while (not done)
2475 ;; back up to the first preceding line (if any; else start of
2476 ;; buffer) that begins with a popular Python keyword, or a
2477 ;; non- whitespace and non-comment character. These are good
2478 ;; places to start parsing to see whether where we started is
2479 ;; at a non-zero nesting level. It may be slow for people who
2480 ;; write huge code blocks or huge lists ... tough beans.
2481 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw170ffa71996-07-31 20:57:22 +00002482 (setq ci (current-indentation))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002483 (beginning-of-line)
2484 (save-excursion
2485 (setq pps (parse-partial-sexp (point) here)))
2486 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw170ffa71996-07-31 20:57:22 +00002487 (setq done (or (zerop ci)
2488 (not (nth 3 pps))
2489 (bobp)))
2490 )
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002491 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002492
2493;; if point is at a non-zero nesting level, returns the number of the
2494;; character that opens the smallest enclosing unclosed list; else
2495;; returns nil.
2496(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002497 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002498 (if (zerop (car status))
2499 nil ; not in a nest
2500 (car (cdr status))))) ; char# of open bracket
2501
2502;; t iff preceding line ends with backslash that's not in a comment
2503(defun py-backslash-continuation-line-p ()
2504 (save-excursion
2505 (beginning-of-line)
2506 (and
2507 ;; use a cheap test first to avoid the regexp if possible
2508 ;; use 'eq' because char-after may return nil
2509 (eq (char-after (- (point) 2)) ?\\ )
2510 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002511 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002512 (looking-at py-continued-re))))
2513
2514;; t iff current line is a continuation line
2515(defun py-continuation-line-p ()
2516 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002517 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002518 (or (py-backslash-continuation-line-p)
2519 (py-nesting-level))))
2520
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002521;; go to initial line of current statement; usually this is the line
2522;; we're on, but if we're on the 2nd or following lines of a
2523;; continuation block, we need to go up to the first line of the
2524;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002525;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002526;; Tricky: We want to avoid quadratic-time behavior for long continued
2527;; blocks, whether of the backslash or open-bracket varieties, or a
2528;; mix of the two. The following manages to do that in the usual
2529;; cases.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002530(defun py-goto-initial-line ()
2531 (let ( open-bracket-pos )
2532 (while (py-continuation-line-p)
2533 (beginning-of-line)
2534 (if (py-backslash-continuation-line-p)
2535 (while (py-backslash-continuation-line-p)
2536 (forward-line -1))
2537 ;; else zip out of nested brackets/braces/parens
2538 (while (setq open-bracket-pos (py-nesting-level))
2539 (goto-char open-bracket-pos)))))
2540 (beginning-of-line))
2541
2542;; go to point right beyond final line of current statement; usually
2543;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002544;; statement we need to skip over the continuation lines. Tricky:
2545;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002546(defun py-goto-beyond-final-line ()
2547 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002548 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002549 (while (and (py-continuation-line-p)
2550 (not (eobp)))
2551 ;; skip over the backslash flavor
2552 (while (and (py-backslash-continuation-line-p)
2553 (not (eobp)))
2554 (forward-line 1))
2555 ;; if in nest, zip to the end of the nest
2556 (setq state (py-parse-state))
2557 (if (and (not (zerop (car state)))
2558 (not (eobp)))
2559 (progn
Barry Warsawaffc0ca1997-11-03 16:59:38 +00002560 (parse-partial-sexp (point) (point-max) 0 nil state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002561 (forward-line 1))))))
2562
2563;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002564;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00002565(defun py-statement-opens-block-p ()
2566 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002567 (let ((start (point))
2568 (finish (progn (py-goto-beyond-final-line) (1- (point))))
2569 (searching t)
2570 (answer nil)
2571 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002572 (goto-char start)
2573 (while searching
2574 ;; look for a colon with nothing after it except whitespace, and
2575 ;; maybe a comment
2576 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
2577 finish t)
2578 (if (eq (point) finish) ; note: no `else' clause; just
2579 ; keep searching if we're not at
2580 ; the end yet
2581 ;; sure looks like it opens a block -- but it might
2582 ;; be in a comment
2583 (progn
2584 (setq searching nil) ; search is done either way
2585 (setq state (parse-partial-sexp start
2586 (match-beginning 0)))
2587 (setq answer (not (nth 4 state)))))
2588 ;; search failed: couldn't find another interesting colon
2589 (setq searching nil)))
2590 answer)))
2591
Barry Warsawf831d811996-07-31 20:42:59 +00002592(defun py-statement-closes-block-p ()
2593 ;; true iff the current statement `closes' a block == the line
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002594 ;; starts with `return', `raise', `break', `continue', and `pass'.
2595 ;; doesn't catch embedded statements
Barry Warsawf831d811996-07-31 20:42:59 +00002596 (let ((here (point)))
2597 (back-to-indentation)
2598 (prog1
Barry Warsawaffc0ca1997-11-03 16:59:38 +00002599 (looking-at (concat py-block-closing-keywords-re "\\>"))
Barry Warsawf831d811996-07-31 20:42:59 +00002600 (goto-char here))))
2601
Barry Warsaw7ae77681994-12-12 20:38:05 +00002602;; go to point right beyond final line of block begun by the current
2603;; line. This is the same as where py-goto-beyond-final-line goes
2604;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002605;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00002606(defun py-goto-beyond-block ()
2607 (if (py-statement-opens-block-p)
2608 (py-mark-block nil 'just-move)
2609 (py-goto-beyond-final-line)))
2610
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002611;; go to start of first statement (not blank or comment or
2612;; continuation line) at or preceding point. returns t if there is
2613;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002614(defun py-goto-statement-at-or-above ()
2615 (py-goto-initial-line)
2616 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002617 ;; skip back over blank & comment lines
2618 ;; note: will skip a blank or comment line that happens to be
2619 ;; a continuation line too
2620 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
2621 (progn (py-goto-initial-line) t)
2622 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002623 t))
2624
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002625;; go to start of first statement (not blank or comment or
2626;; continuation line) following the statement containing point returns
2627;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002628(defun py-goto-statement-below ()
2629 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002630 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002631 (py-goto-beyond-final-line)
2632 (while (and
2633 (looking-at py-blank-or-comment-re)
2634 (not (eobp)))
2635 (forward-line 1))
2636 (if (eobp)
2637 (progn (goto-char start) nil)
2638 t)))
2639
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002640;; go to start of statement, at or preceding point, starting with
2641;; keyword KEY. Skips blank lines and non-indenting comments upward
2642;; first. If that statement starts with KEY, done, else go back to
2643;; first enclosing block starting with KEY. If successful, leaves
2644;; point at the start of the KEY line & returns t. Else leaves point
2645;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002646(defun py-go-up-tree-to-keyword (key)
2647 ;; skip blanks and non-indenting #
2648 (py-goto-initial-line)
2649 (while (and
2650 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2651 (zerop (forward-line -1))) ; go back
2652 nil)
2653 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002654 (let* ((re (concat "[ \t]*" key "\\b"))
2655 (case-fold-search nil) ; let* so looking-at sees this
2656 (found (looking-at re))
2657 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002658 (while (not (or found dead))
2659 (condition-case nil ; in case no enclosing block
2660 (py-goto-block-up 'no-mark)
2661 (error (setq dead t)))
2662 (or dead (setq found (looking-at re))))
2663 (beginning-of-line)
2664 found))
2665
2666;; return string in buffer from start of indentation to end of line;
2667;; prefix "..." if leading whitespace was skipped
2668(defun py-suck-up-leading-text ()
2669 (save-excursion
2670 (back-to-indentation)
2671 (concat
2672 (if (bolp) "" "...")
2673 (buffer-substring (point) (progn (end-of-line) (point))))))
2674
2675;; assuming point at bolp, return first keyword ([a-z]+) on the line,
2676;; as a Lisp symbol; return nil if none
2677(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002678 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002679 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
2680 (intern (buffer-substring (match-beginning 1) (match-end 1)))
2681 nil)))
2682
Barry Warsawb3e81d51996-09-04 15:12:42 +00002683(defun py-current-defun ()
2684 ;; tell add-log.el how to find the current function/method/variable
2685 (save-excursion
2686 (if (re-search-backward py-defun-start-re nil t)
2687 (or (match-string 3)
2688 (let ((method (match-string 2)))
2689 (if (and (not (zerop (length (match-string 1))))
2690 (re-search-backward py-class-start-re nil t))
2691 (concat (match-string 1) "." method)
2692 method)))
2693 nil)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002694
2695
Barry Warsawfec75d61995-07-05 23:26:15 +00002696(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00002697 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002698
Barry Warsaw850437a1995-03-08 21:50:28 +00002699(defun py-version ()
2700 "Echo the current version of `python-mode' in the minibuffer."
2701 (interactive)
2702 (message "Using `python-mode' version %s" py-version)
2703 (py-keep-region-active))
2704
2705;; only works under Emacs 19
2706;(eval-when-compile
2707; (require 'reporter))
2708
2709(defun py-submit-bug-report (enhancement-p)
2710 "Submit via mail a bug report on `python-mode'.
2711With \\[universal-argument] just submit an enhancement request."
2712 (interactive
2713 (list (not (y-or-n-p
2714 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002715 (let ((reporter-prompt-for-summary-p (if enhancement-p
2716 "(Very) brief summary: "
2717 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00002718 (require 'reporter)
2719 (reporter-submit-bug-report
2720 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002721 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00002722 ;; varlist
2723 (if enhancement-p nil
2724 '(py-python-command
2725 py-indent-offset
2726 py-block-comment-prefix
2727 py-scroll-process-buffer
2728 py-temp-directory
2729 py-beep-if-tab-change))
2730 nil ;pre-hooks
2731 nil ;post-hooks
2732 "Dear Barry,") ;salutation
2733 (if enhancement-p nil
2734 (set-mark (point))
2735 (insert
2736"Please replace this text with a sufficiently large code sample\n\
2737and an exact recipe so that I can reproduce your problem. Failure\n\
2738to do so may mean a greater delay in fixing your bug.\n\n")
2739 (exchange-point-and-mark)
2740 (py-keep-region-active))))
2741
2742
Barry Warsaw47384781997-11-26 05:27:45 +00002743(defun py-kill-emacs-hook ()
2744 (mapcar #'(lambda (filename)
2745 (py-safe (delete-file filename)))
2746 py-file-queue))
2747
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002748;; arrange to kill temp files when Emacs exists
Barry Warsawc72c11c1997-08-09 06:42:08 +00002749(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002750
2751
2752
2753(provide 'python-mode)
2754;;; python-mode.el ends here