blob: 2128d89c2680e905a9e8c25c5de4bfc81792007e [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 Warsaw4669d7e1996-03-22 16:13:24 +00005;; Author: 1995-1996 Barry A. Warsaw
Barry Warsawfec75d61995-07-05 23:26:15 +00006;; 1992-1994 Tim Peters
7;; Maintainer: python-mode@python.org
Barry Warsawcfec3591995-03-10 15:58:16 +00008;; Created: Feb 1992
Barry Warsaw7b0f5681995-03-08 21:33:04 +00009;; Version: $Revision$
10;; Last Modified: $Date$
Barry Warsaw4669d7e1996-03-22 16:13:24 +000011;; Keywords: python languages oop
Barry Warsaw7b0f5681995-03-08 21:33:04 +000012
Barry Warsawcfec3591995-03-10 15:58:16 +000013;; This software is provided as-is, without express or implied
14;; warranty. Permission to use, copy, modify, distribute or sell this
15;; software, without fee, for any purpose and by any individual or
16;; organization, is hereby granted, provided that the above copyright
17;; notice and this paragraph appear in all copies.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000018
19;;; Commentary:
Barry Warsaw755c6711996-08-01 20:02:55 +000020
Barry Warsaw7b0f5681995-03-08 21:33:04 +000021;; This is a major mode for editing Python programs. It was developed
Barry Warsawcf34d2a1996-08-06 15:57:58 +000022;; by Tim Peters after an original idea by Michael A. Guravage. In
23;; 1995, Barry Warsaw inherited the mode after Tim left the net, and
24;; is the current maintainer.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000025
26;; At some point this mode will undergo a rewrite to bring it more in
Barry Warsaw755c6711996-08-01 20:02:55 +000027;; line with GNU Emacs Lisp coding standards, and to wax all the Emacs
28;; 18 support. But all in all, the mode works exceedingly well, and
29;; I've simply been tweaking it as I go along. Ain't it wonderful
30;; that Python has a much more sane syntax than C? (or <shudder> C++?!
31;; :-). I can say that; I maintain cc-mode!
Barry Warsaw7b0f5681995-03-08 21:33:04 +000032
33;; The following statements, placed in your .emacs file or
34;; site-init.el, will cause this file to be autoloaded, and
35;; python-mode invoked, when visiting .py files (assuming this file is
36;; in your load-path):
Barry Warsaw7ae77681994-12-12 20:38:05 +000037;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +000038;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Barry Warsaw7ae77681994-12-12 20:38:05 +000039;; (setq auto-mode-alist
40;; (cons '("\\.py$" . python-mode) auto-mode-alist))
Barry Warsaw44b72201996-07-05 20:11:35 +000041;;
42;; If you want font-lock support for Python source code (a.k.a. syntax
43;; coloring, highlighting), add this to your .emacs file:
44;;
45;; (add-hook 'python-mode-hook 'turn-on-font-lock)
Barry Warsawc08a9491996-07-31 22:27:58 +000046;;
47;; But you better be sure you're version of Emacs supports
48;; font-lock-mode! As of this writing, the latest Emacs and XEmacs
49;; 19's do.
Barry Warsaw7ae77681994-12-12 20:38:05 +000050
Barry Warsaw3fcaf611996-08-01 20:11:51 +000051;; Here's a brief list of recent additions/improvements/changes:
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000052;;
Barry Warsaw3fcaf611996-08-01 20:11:51 +000053;; - Wrapping and indentation within triple quote strings now works.
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000054;; - `Standard' bug reporting mechanism (use C-c C-b)
55;; - py-mark-block was moved to C-c C-m
56;; - C-c C-v shows you the python-mode version
Barry Warsaw3fcaf611996-08-01 20:11:51 +000057;; - a basic python-font-lock-keywords has been added for (X)Emacs 19
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000058;; - proper interaction with pending-del and del-sel modes.
Barry Warsaw3fcaf611996-08-01 20:11:51 +000059;; - Better support for outdenting: py-electric-colon (:) and
60;; py-indent-line (TAB) improvements; one level of outdentation
61;; added after a return, raise, break, or continue statement
62;; - New py-electric-colon (:) command for improved outdenting Also
63;; py-indent-line (TAB) should handle outdented lines better
Barry Warsaw03970731996-07-03 23:12:52 +000064;; - improved (I think) C-c > and C-c <
Barry Warsaw9e5a9c81996-07-24 18:26:53 +000065;; - py-(forward|backward)-into-nomenclature, not bound, but useful on
66;; M-f and M-b respectively.
Barry Warsaw3fcaf611996-08-01 20:11:51 +000067;; - integration with imenu by Perry A. Stoll <stoll@atr-sw.atr.co.jp>
68;; - py-indent-offset now defaults to 4
69;; - new variable py-honor-comment-indentation
70;; - comment-region bound to C-c #
71;; - py-delete-char obeys numeric arguments
72;; - Small modification to rule for "indenting comment lines", such
73;; lines must now also be indented less than or equal to the
74;; indentation of the previous statement.
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000075
Barry Warsaw7b0f5681995-03-08 21:33:04 +000076;; Here's a brief to do list:
77;;
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000078;; - Better integration with gud-mode for debugging.
79;; - Rewrite according to GNU Emacs Lisp standards.
Barry Warsaw5c0d00f1996-07-31 21:30:21 +000080;; - possibly force indent-tabs-mode == nil, and add a
81;; write-file-hooks that runs untabify on the whole buffer (to work
82;; around potential tab/space mismatch problems). In practice this
83;; hasn't been a problem... yet.
Barry Warsaw9e277db1996-07-31 22:33:40 +000084;; - have py-execute-region on indented code act as if the region is
85;; left justified. Avoids syntax errors.
Barry Warsaw7ae77681994-12-12 20:38:05 +000086
Barry Warsaw7b0f5681995-03-08 21:33:04 +000087;; If you can think of more things you'd like to see, drop me a line.
88;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
89;;
Barry Warsaw3fcaf611996-08-01 20:11:51 +000090;; Note that I only test things on XEmacs 19 and to some degree on
91;; Emacs 19. If you port stuff to FSF Emacs 19, or Emacs 18, please
92;; send me your patches. Byte compiler complaints can probably be
93;; safely ignored.
Barry Warsaw7ae77681994-12-12 20:38:05 +000094
Barry Warsaw7b0f5681995-03-08 21:33:04 +000095;;; Code:
96
97
98;; user definable variables
99;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +0000100
101(defvar py-python-command "python"
102 "*Shell command used to start Python interpreter.")
103
Barry Warsaw17914f41995-11-03 18:25:15 +0000104(defvar py-indent-offset 4
Barry Warsaw7ae77681994-12-12 20:38:05 +0000105 "*Indentation increment.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000106Note that `\\[py-guess-indent-offset]' can usually guess a good value
107when you're editing someone else's Python code.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000108
Barry Warsaw095e9c61995-09-19 20:01:42 +0000109(defvar py-align-multiline-strings-p t
110 "*Flag describing how multiline triple quoted strings are aligned.
111When this flag is non-nil, continuation lines are lined up under the
112preceding line's indentation. When this flag is nil, continuation
113lines are aligned to column zero.")
114
Barry Warsaw3fcaf611996-08-01 20:11:51 +0000115(defvar py-block-comment-prefix "## "
Barry Warsaw867a32a1996-03-07 18:30:26 +0000116 "*String used by \\[comment-region] to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000117This should follow the convention for non-indenting comment lines so
118that the indentation commands won't get confused (i.e., the string
119should be of the form `#x...' where `x' is not a blank or a tab, and
120`...' is arbitrary).")
121
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000122(defvar py-honor-comment-indentation t
Barry Warsaw6d627751996-03-06 18:41:38 +0000123 "*Controls how comment lines influence subsequent indentation.
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000124
Barry Warsaw6d627751996-03-06 18:41:38 +0000125When nil, all comment lines are skipped for indentation purposes, and
126in Emacs 19, a faster algorithm is used.
127
128When t, lines that begin with a single `#' are a hint to subsequent
129line indentation. If the previous line is such a comment line (as
130opposed to one that starts with `py-block-comment-prefix'), then it's
131indentation is used as a hint for this line's indentation. Lines that
132begin with `py-block-comment-prefix' are ignored for indentation
133purposes.
134
135When not nil or t, comment lines that begin with a `#' are used as
136indentation hints, unless the comment character is in column zero.")
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000137
Barry Warsaw7ae77681994-12-12 20:38:05 +0000138(defvar py-scroll-process-buffer t
139 "*Scroll Python process buffer as output arrives.
140If nil, the Python process buffer acts, with respect to scrolling, like
141Shell-mode buffers normally act. This is surprisingly complicated and
142so won't be explained here; in fact, you can't get the whole story
143without studying the Emacs C code.
144
145If non-nil, the behavior is different in two respects (which are
146slightly inaccurate in the interest of brevity):
147
148 - If the buffer is in a window, and you left point at its end, the
149 window will scroll as new output arrives, and point will move to the
150 buffer's end, even if the window is not the selected window (that
151 being the one the cursor is in). The usual behavior for shell-mode
152 windows is not to scroll, and to leave point where it was, if the
153 buffer is in a window other than the selected window.
154
155 - If the buffer is not visible in any window, and you left point at
156 its end, the buffer will be popped into a window as soon as more
157 output arrives. This is handy if you have a long-running
158 computation and don't want to tie up screen area waiting for the
159 output. The usual behavior for a shell-mode buffer is to stay
160 invisible until you explicitly visit it.
161
162Note the `and if you left point at its end' clauses in both of the
163above: you can `turn off' the special behaviors while output is in
164progress, by visiting the Python buffer and moving point to anywhere
165besides the end. Then the buffer won't scroll, point will remain where
166you leave it, and if you hide the buffer it will stay hidden until you
167visit it again. You can enable and disable the special behaviors as
168often as you like, while output is in progress, by (respectively) moving
169point to, or away from, the end of the buffer.
170
171Warning: If you expect a large amount of output, you'll probably be
172happier setting this option to nil.
173
174Obscure: `End of buffer' above should really say `at or beyond the
175process mark', but if you know what that means you didn't need to be
176told <grin>.")
177
178(defvar py-temp-directory
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000179 (let ((ok '(lambda (x)
180 (and x
181 (setq x (expand-file-name x)) ; always true
182 (file-directory-p x)
183 (file-writable-p x)
184 x))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000185 (or (funcall ok (getenv "TMPDIR"))
186 (funcall ok "/usr/tmp")
187 (funcall ok "/tmp")
188 (funcall ok ".")
189 (error
190 "Couldn't find a usable temp directory -- set py-temp-directory")))
191 "*Directory used for temp files created by a *Python* process.
192By default, the first directory from this list that exists and that you
193can write into: the value (if any) of the environment variable TMPDIR,
194/usr/tmp, /tmp, or the current directory.")
195
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000196(defvar py-beep-if-tab-change t
197 "*Ring the bell if tab-width is changed.
198If a comment of the form
199
200 \t# vi:set tabsize=<number>:
201
202is found before the first code line when the file is entered, and the
203current value of (the general Emacs variable) `tab-width' does not
204equal <number>, `tab-width' is set to <number>, a message saying so is
205displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
206the Emacs bell is also rung as a warning.")
207
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000208(defconst python-font-lock-keywords
Barry Warsaw44b72201996-07-05 20:11:35 +0000209 (let* ((keywords '("access" "and" "break" "class"
210 "continue" "def" "del" "elif"
211 "else:" "except" "except:" "exec"
212 "finally:" "for" "from" "global"
213 "if" "import" "in" "is"
214 "lambda" "not" "or" "pass"
215 "print" "raise" "return" "try:"
216 "while"
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000217 ))
218 (kwregex (mapconcat 'identity keywords "\\|")))
219 (list
220 ;; keywords not at beginning of line
221 (cons (concat "\\s-\\(" kwregex "\\)[ \n\t(]") 1)
222 ;; keywords at beginning of line. i don't think regexps are
223 ;; powerful enough to handle these two cases in one regexp.
224 ;; prove me wrong!
225 (cons (concat "^\\(" kwregex "\\)[ \n\t(]") 1)
226 ;; classes
227 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
228 1 font-lock-type-face)
229 ;; functions
230 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
231 1 font-lock-function-name-face)
232 ))
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000233 "Additional expressions to highlight in Python mode.")
Barry Warsawb01b4fa1995-06-20 18:55:34 +0000234
Barry Warsaw81437461996-08-01 19:48:02 +0000235(defvar imenu-example--python-show-method-args-p nil
236 "*Controls echoing of arguments of functions & methods in the imenu buffer.
237When non-nil, arguments are printed.")
238
239
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000240
241;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
242;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
243
Barry Warsaw52bc17c1995-10-12 21:15:49 +0000244(make-variable-buffer-local 'py-indent-offset)
245
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000246;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
247;; seems to be the standard way of checking this.
248;; BAW - This is *not* the right solution. When at all possible,
249;; instead of testing for the version of Emacs, use feature tests.
250
251(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
252(setq py-this-is-emacs-19-p
253 (and
254 (not py-this-is-lucid-emacs-p)
255 (string-match "^19\\." emacs-version)))
256
Barry Warsaw7ae77681994-12-12 20:38:05 +0000257;; have to bind py-file-queue before installing the kill-emacs hook
258(defvar py-file-queue nil
259 "Queue of Python temp files awaiting execution.
260Currently-active file is at the head of the list.")
261
262;; define a mode-specific abbrev table for those who use such things
263(defvar python-mode-abbrev-table nil
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000264 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000265(define-abbrev-table 'python-mode-abbrev-table nil)
266
Barry Warsaw7ae77681994-12-12 20:38:05 +0000267(defvar python-mode-hook nil
268 "*Hook called by `python-mode'.")
269
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000270;; in previous version of python-mode.el, the hook was incorrectly
271;; called py-mode-hook, and was not defvar'd. deprecate its use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000272(and (fboundp 'make-obsolete-variable)
273 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
274
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000275(defvar py-mode-map ()
276 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000277
Barry Warsaw7ae77681994-12-12 20:38:05 +0000278(if py-mode-map
279 ()
280 (setq py-mode-map (make-sparse-keymap))
281
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000282 ;; shadow global bindings for newline-and-indent w/ the py- version.
283 ;; BAW - this is extremely bad form, but I'm not going to change it
284 ;; for now.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000285 (mapcar (function (lambda (key)
286 (define-key
287 py-mode-map key 'py-newline-and-indent)))
288 (where-is-internal 'newline-and-indent))
289
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000290 ;; BAW - you could do it this way, but its not considered proper
291 ;; major-mode form.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000292 (mapcar (function
293 (lambda (x)
294 (define-key py-mode-map (car x) (cdr x))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000295 '((":" . py-electric-colon)
296 ("\C-c\C-c" . py-execute-buffer)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000297 ("\C-c|" . py-execute-region)
298 ("\C-c!" . py-shell)
299 ("\177" . py-delete-char)
300 ("\n" . py-newline-and-indent)
301 ("\C-c:" . py-guess-indent-offset)
302 ("\C-c\t" . py-indent-region)
Barry Warsawdea4a291996-07-03 22:59:12 +0000303 ("\C-c\C-l" . py-shift-region-left)
304 ("\C-c\C-r" . py-shift-region-right)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000305 ("\C-c<" . py-shift-region-left)
306 ("\C-c>" . py-shift-region-right)
307 ("\C-c\C-n" . py-next-statement)
308 ("\C-c\C-p" . py-previous-statement)
309 ("\C-c\C-u" . py-goto-block-up)
Barry Warsaw850437a1995-03-08 21:50:28 +0000310 ("\C-c\C-m" . py-mark-block)
Barry Warsawa7891711996-08-01 15:53:09 +0000311 ("\C-c#" . py-comment-region)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000312 ("\C-c?" . py-describe-mode)
313 ("\C-c\C-hm" . py-describe-mode)
314 ("\e\C-a" . beginning-of-python-def-or-class)
315 ("\e\C-e" . end-of-python-def-or-class)
Barry Warsaw850437a1995-03-08 21:50:28 +0000316 ( "\e\C-h" . mark-python-def-or-class)))
317 ;; should do all keybindings this way
318 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
319 (define-key py-mode-map "\C-c\C-v" 'py-version)
320 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000321
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000322(defvar py-mode-syntax-table nil
323 "Syntax table used in `python-mode' buffers.")
324
Barry Warsaw7ae77681994-12-12 20:38:05 +0000325(if py-mode-syntax-table
326 ()
327 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000328 ;; BAW - again, blech.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000329 (mapcar (function
330 (lambda (x) (modify-syntax-entry
331 (car x) (cdr x) py-mode-syntax-table)))
332 '(( ?\( . "()" ) ( ?\) . ")(" )
333 ( ?\[ . "(]" ) ( ?\] . ")[" )
334 ( ?\{ . "(}" ) ( ?\} . "){" )
335 ;; fix operator symbols misassigned in the std table
336 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
337 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
338 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
339 ( ?\> . "." ) ( ?\| . "." )
Barry Warsawfb349421996-07-24 18:32:08 +0000340 ;; for historical reasons, underscore is word class
341 ;; instead of symbol class. it should be symbol class,
342 ;; but if you're tempted to change it, try binding M-f and
343 ;; M-b to py-forward-into-nomenclature and
344 ;; py-backward-into-nomenclature instead. -baw
Barry Warsaw8e9d7d71996-07-03 23:15:51 +0000345 ( ?\_ . "w" ) ; underscore is legit in words
Barry Warsaw7ae77681994-12-12 20:38:05 +0000346 ( ?\' . "\"") ; single quote is string quote
347 ( ?\" . "\"" ) ; double quote is string quote too
348 ( ?\` . "$") ; backquote is open and close paren
349 ( ?\# . "<") ; hash starts comment
350 ( ?\n . ">")))) ; newline ends comment
351
352(defconst py-stringlit-re
353 (concat
354 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
355 "\\|" ; or
356 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000357 "Regexp matching a Python string literal.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000358
359;; this is tricky because a trailing backslash does not mean
360;; continuation if it's in a comment
361(defconst py-continued-re
362 (concat
363 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
364 "\\\\$")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000365 "Regexp matching Python lines that are continued via backslash.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000366
367(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000368 "Regexp matching blank or comment lines.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000369
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000370(defconst py-outdent-re
371 (concat "\\(" (mapconcat 'identity
372 '("else:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000373 "except\\(\\s +.*\\)?:"
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000374 "finally:"
375 "elif\\s +.*:")
376 "\\|")
377 "\\)")
378 "Regexp matching clauses to be outdented one level.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000379
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000380(defconst py-no-outdent-re
381 (concat "\\(" (mapconcat 'identity
Barry Warsaw464c94a1995-03-14 23:25:44 +0000382 '("try:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000383 "except\\(\\s +.*\\)?:"
384 "while\\s +.*:"
385 "for\\s +.*:"
386 "if\\s +.*:"
387 "elif\\s +.*:")
388 "\\|")
389 "\\)")
390 "Regexp matching lines to not outdent after.")
391
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000392
Barry Warsaw42f707f1996-07-29 21:05:05 +0000393;; Menu definitions, only relevent if you have the easymenu.el package
394;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
Barry Warsaw5490a061996-08-06 15:43:33 +0000395(defvar py-menu nil
396 "Menu for Python Mode.
397
398This menu will get created automatically if you have the easymenu
399package. Note that the latest XEmacs 19 and Emacs 19 versions contain
400this package.")
401
Barry Warsaw42f707f1996-07-29 21:05:05 +0000402(if (condition-case nil
403 (require 'easymenu)
404 (error nil))
405 (easy-menu-define
406 py-menu py-mode-map "Python Mode menu"
407 '("Python"
408 ["Comment Out Region" comment-region (mark)]
409 ["Uncomment Region" (comment-region (point) (mark) '(4)) (mark)]
410 "-"
411 ["Mark current block" py-mark-block t]
412 ["Mark current def" mark-python-def-or-class t]
413 ["Mark current class" (mark-python-def-or-class t) t]
414 "-"
415 ["Shift region left" py-shift-region-left (mark)]
416 ["Shift region right" py-shift-region-right (mark)]
417 "-"
418 ["Execute buffer" py-execute-buffer t]
419 ["Execute region" py-execute-region (mark)]
420 ["Start interpreter..." py-shell t]
421 "-"
422 ["Go to start of block" py-goto-block-up t]
423 ["Go to start of class" (beginning-of-python-def-or-class t) t]
424 ["Move to end of class" (end-of-python-def-or-class t) t]
425 ["Move to start of def" beginning-of-python-def-or-class t]
426 ["Move to end of def" end-of-python-def-or-class t]
427 "-"
428 ["Describe mode" py-describe-mode t]
429 )))
430
Barry Warsaw81437461996-08-01 19:48:02 +0000431
432
433;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp>
434(defvar imenu-example--python-class-regexp
435 (concat ; <<classes>>
436 "\\(" ;
437 "^[ \t]*" ; newline and maybe whitespace
438 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
439 ; possibly multiple superclasses
440 "\\([ \t]*\\((\\([a-zA-Z0-9_, \t\n]\\)*)\\)?\\)"
441 "[ \t]*:" ; and the final :
442 "\\)" ; >>classes<<
443 )
444 "Regexp for Python classes for use with the imenu package."
445 )
446
447(defvar imenu-example--python-method-regexp
448 (concat ; <<methods and functions>>
449 "\\(" ;
450 "^[ \t]*" ; new line and maybe whitespace
451 "\\(def[ \t]+" ; function definitions start with def
452 "\\([a-zA-Z0-9_]+\\)" ; name is here
453 ; function arguments...
454 "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))"
455 "\\)" ; end of def
456 "[ \t]*:" ; and then the :
457 "\\)" ; >>methods and functions<<
458 )
459 "Regexp for Python methods/functions for use with the imenu package."
460 )
461
462(defvar imenu-example--python-method-no-arg-parens '(2 8)
463 "Indicies into groups of the Python regexp for use with imenu.
464
465Using these values will result in smaller imenu lists, as arguments to
466functions are not listed.
467
468See the variable `imenu-example--python-show-method-args-p' for more
469information.")
470
471(defvar imenu-example--python-method-arg-parens '(2 7)
472 "Indicies into groups of the Python regexp for use with imenu.
473Using these values will result in large imenu lists, as arguments to
474functions are listed.
475
476See the variable `imenu-example--python-show-method-args-p' for more
477information.")
478
479;; Note that in this format, this variable can still be used with the
480;; imenu--generic-function. Otherwise, there is no real reason to have
481;; it.
482(defvar imenu-example--generic-python-expression
483 (cons
484 (concat
485 imenu-example--python-class-regexp
486 "\\|" ; or...
487 imenu-example--python-method-regexp
488 )
489 imenu-example--python-method-no-arg-parens)
490 "Generic Python expression which may be used directly with imenu.
491Used by setting the variable `imenu-generic-expression' to this value.
492Also, see the function \\[imenu-example--create-python-index] for a
493better alternative for finding the index.")
494
495;; These next two variables are used when searching for the python
496;; class/definitions. Just saving some time in accessing the
497;; generic-python-expression, really.
498(defvar imenu-example--python-generic-regexp)
499(defvar imenu-example--python-generic-parens)
500
501
502;;;###autoload
503(eval-when-compile
504 ;; Imenu isn't used in XEmacs, so just ignore load errors
505 (condition-case ()
506 (progn
507 (require 'cl)
508 (require 'imenu))
509 (error nil)))
510
511(defun imenu-example--create-python-index ()
512 "Python interface function for imenu package.
513Finds all python classes and functions/methods. Calls function
514\\[imenu-example--create-python-index-engine]. See that function for
515the details of how this works."
516 (setq imenu-example--python-generic-regexp
517 (car imenu-example--generic-python-expression))
518 (setq imenu-example--python-generic-parens
519 (if imenu-example--python-show-method-args-p
520 imenu-example--python-method-arg-parens
521 imenu-example--python-method-no-arg-parens))
522 (goto-char (point-min))
523 (imenu-example--create-python-index-engine nil))
524
525(defun imenu-example--create-python-index-engine (&optional start-indent)
526 "Function for finding imenu definitions in Python.
527
528Finds all definitions (classes, methods, or functions) in a Python
529file for the imenu package.
530
531Returns a possibly nested alist of the form
532
533 (INDEX-NAME . INDEX-POSITION)
534
535The second element of the alist may be an alist, producing a nested
536list as in
537
538 (INDEX-NAME . INDEX-ALIST)
539
540This function should not be called directly, as it calls itself
541recursively and requires some setup. Rather this is the engine for
542the function \\[imenu-example--create-python-index].
543
544It works recursively by looking for all definitions at the current
545indention level. When it finds one, it adds it to the alist. If it
546finds a definition at a greater indentation level, it removes the
547previous definition from the alist. In it's place it adds all
548definitions found at the next indentation level. When it finds a
549definition that is less indented then the current level, it retuns the
550alist it has created thus far.
551
552The optional argument START-INDENT indicates the starting indentation
553at which to continue looking for Python classes, methods, or
554functions. If this is not supplied, the function uses the indentation
555of the first definition found."
556 (let ((index-alist '())
557 (sub-method-alist '())
558 looking-p
559 def-name prev-name
560 cur-indent def-pos
561 (class-paren (first imenu-example--python-generic-parens))
562 (def-paren (second imenu-example--python-generic-parens)))
563 (setq looking-p
564 (re-search-forward imenu-example--python-generic-regexp
565 (point-max) t))
566 (while looking-p
567 (save-excursion
568 ;; used to set def-name to this value but generic-extract-name is
569 ;; new to imenu-1.14. this way it still works with imenu-1.11
570 ;;(imenu--generic-extract-name imenu-example--python-generic-parens))
571 (let ((cur-paren (if (match-beginning class-paren)
572 class-paren def-paren)))
573 (setq def-name
574 (buffer-substring (match-beginning cur-paren)
575 (match-end cur-paren))))
576 (beginning-of-line)
577 (setq cur-indent (current-indentation)))
578
579 ;; HACK: want to go to the next correct definition location. we
580 ;; explicitly list them here. would be better to have them in a
581 ;; list.
582 (setq def-pos
583 (or (match-beginning class-paren)
584 (match-beginning def-paren)))
585
586 ;; if we don't have a starting indent level, take this one
587 (or start-indent
588 (setq start-indent cur-indent))
589
590 ;; if we don't have class name yet, take this one
591 (or prev-name
592 (setq prev-name def-name))
593
594 ;; what level is the next definition on? must be same, deeper
595 ;; or shallower indentation
596 (cond
597 ;; at the same indent level, add it to the list...
598 ((= start-indent cur-indent)
599
600 ;; if we don't have push, use the following...
601 ;;(setf index-alist (cons (cons def-name def-pos) index-alist))
602 (push (cons def-name def-pos) index-alist))
603
604 ;; deeper indented expression, recur...
605 ((< start-indent cur-indent)
606
607 ;; the point is currently on the expression we're supposed to
608 ;; start on, so go back to the last expression. The recursive
609 ;; call will find this place again and add it to the correct
610 ;; list
611 (re-search-backward imenu-example--python-generic-regexp
612 (point-min) 'move)
613 (setq sub-method-alist (imenu-example--create-python-index-engine
614 cur-indent))
615
616 (if sub-method-alist
617 ;; we put the last element on the index-alist on the start
618 ;; of the submethod alist so the user can still get to it.
619 (let ((save-elmt (pop index-alist)))
620 (push (cons (imenu-create-submenu-name prev-name)
621 (cons save-elmt sub-method-alist))
622 index-alist))))
623
624 ;; found less indented expression, we're done.
625 (t
626 (setq looking-p nil)
627 (re-search-backward imenu-example--python-generic-regexp
628 (point-min) t)))
629 (setq prev-name def-name)
630 (and looking-p
631 (setq looking-p
632 (re-search-forward imenu-example--python-generic-regexp
633 (point-max) 'move))))
634 (nreverse index-alist)))
635
Barry Warsaw42f707f1996-07-29 21:05:05 +0000636
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000637;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000638(defun python-mode ()
639 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000640To submit a problem report, enter `\\[py-submit-bug-report]' from a
641`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
642documentation. To see what version of `python-mode' you are running,
643enter `\\[py-version]'.
644
645This mode knows about Python indentation, tokens, comments and
646continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000647
648COMMANDS
649\\{py-mode-map}
650VARIABLES
651
Barry Warsaw42f707f1996-07-29 21:05:05 +0000652py-indent-offset\t\tindentation increment
653py-block-comment-prefix\t\tcomment string used by comment-region
654py-python-command\t\tshell command to invoke Python interpreter
655py-scroll-process-buffer\t\talways scroll Python process buffer
656py-temp-directory\t\tdirectory used for temp files (if needed)
657py-beep-if-tab-change\t\tring the bell if tab-width is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +0000658 (interactive)
659 (kill-all-local-variables)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000660 (set-syntax-table py-mode-syntax-table)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000661 (setq major-mode 'python-mode
662 mode-name "Python"
663 local-abbrev-table python-mode-abbrev-table)
664 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +0000665 ;; add the menu
666 (if py-menu
667 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +0000668 ;; Emacs 19 requires this
669 (if (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p)
670 (setq comment-multi-line nil))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000671 ;; BAW -- style...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000672 (mapcar (function (lambda (x)
673 (make-local-variable (car x))
674 (set (car x) (cdr x))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000675 '((paragraph-separate . "^[ \t]*$")
676 (paragraph-start . "^[ \t]*$")
677 (require-final-newline . t)
Barry Warsawa7891711996-08-01 15:53:09 +0000678 (comment-start . "# ")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000679 (comment-start-skip . "# *")
680 (comment-column . 40)
681 (indent-region-function . py-indent-region)
682 (indent-line-function . py-indent-line)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000683 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000684 ;;
685 ;; not sure where the magic comment has to be; to save time
686 ;; searching for a rarity, we give up if it's not found prior to the
687 ;; first executable statement.
688 ;;
689 ;; BAW - on first glance, this seems like complete hackery. Why was
690 ;; this necessary, and is it still necessary?
691 (let ((case-fold-search nil)
692 (start (point))
693 new-tab-width)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000694 (if (re-search-forward
695 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
696 (prog2 (py-next-statement 1) (point) (goto-char 1))
697 t)
698 (progn
699 (setq new-tab-width
700 (string-to-int
701 (buffer-substring (match-beginning 1) (match-end 1))))
702 (if (= tab-width new-tab-width)
703 nil
704 (setq tab-width new-tab-width)
705 (message "Caution: tab-width changed to %d" new-tab-width)
706 (if py-beep-if-tab-change (beep)))))
707 (goto-char start))
708
Barry Warsaw755c6711996-08-01 20:02:55 +0000709 ;; install imenu
710 (setq imenu-create-index-function
711 (function imenu-example--create-python-index))
712 (if (fboundp 'imenu-add-to-menubar)
713 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
714
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000715 ;; run the mode hook. py-mode-hook use is deprecated
Barry Warsaw7ae77681994-12-12 20:38:05 +0000716 (if python-mode-hook
717 (run-hooks 'python-mode-hook)
718 (run-hooks 'py-mode-hook)))
719
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000720
Barry Warsaw826255b1996-03-22 16:09:34 +0000721(defun py-keep-region-active ()
Barry Warsawce60bc71996-08-01 18:17:14 +0000722 ;; do whatever is necessary to keep the region active in XEmacs.
723 ;; Ignore byte-compiler warnings you might see. Also note that
724 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
725 ;; require us to take explicit action.
Barry Warsaw826255b1996-03-22 16:09:34 +0000726 (and (boundp 'zmacs-region-stays)
727 (setq zmacs-region-stays t)))
728
Barry Warsawce60bc71996-08-01 18:17:14 +0000729
Barry Warsawb91b7431995-03-14 15:55:20 +0000730;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000731(defun py-outdent-p ()
732 ;; returns non-nil if the current line should outdent one level
733 (save-excursion
734 (and (progn (back-to-indentation)
735 (looking-at py-outdent-re))
736 (progn (backward-to-indentation 1)
737 (while (or (looking-at py-blank-or-comment-re)
738 (bobp))
739 (backward-to-indentation 1))
740 (not (looking-at py-no-outdent-re)))
741 )))
742
743
Barry Warsawb91b7431995-03-14 15:55:20 +0000744(defun py-electric-colon (arg)
745 "Insert a colon.
746In certain cases the line is outdented appropriately. If a numeric
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000747argument is provided, that many colons are inserted non-electrically.
748Electric behavior is inhibited inside a string or comment."
Barry Warsawb91b7431995-03-14 15:55:20 +0000749 (interactive "P")
750 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000751 ;; are we in a string or comment?
752 (if (save-excursion
753 (let ((pps (parse-partial-sexp (save-excursion
754 (beginning-of-python-def-or-class)
755 (point))
756 (point))))
757 (not (or (nth 3 pps) (nth 4 pps)))))
758 (save-excursion
759 (let ((here (point))
760 (outdent 0)
761 (indent (py-compute-indentation)))
762 (if (and (not arg)
763 (py-outdent-p)
764 (= indent (save-excursion
Barry Warsawa7661821996-08-02 16:22:43 +0000765 (py-next-statement -1)
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000766 (py-compute-indentation)))
767 )
768 (setq outdent py-indent-offset))
769 ;; Don't indent, only outdent. This assumes that any lines that
770 ;; are already outdented relative to py-compute-indentation were
771 ;; put there on purpose. Its highly annoying to have `:' indent
772 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
773 ;; there a better way to determine this???
774 (if (< (current-indentation) indent) nil
775 (goto-char here)
776 (beginning-of-line)
777 (delete-horizontal-space)
778 (indent-to (- indent outdent))
779 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000780
781
Barry Warsaw7ae77681994-12-12 20:38:05 +0000782;;; Functions that execute Python commands in a subprocess
Barry Warsaw7ae77681994-12-12 20:38:05 +0000783(defun py-shell ()
784 "Start an interactive Python interpreter in another window.
785This is like Shell mode, except that Python is running in the window
786instead of a shell. See the `Interactive Shell' and `Shell Mode'
787sections of the Emacs manual for details, especially for the key
788bindings active in the `*Python*' buffer.
789
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000790See the docs for variable `py-scroll-buffer' for info on scrolling
Barry Warsaw7ae77681994-12-12 20:38:05 +0000791behavior in the process window.
792
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000793Warning: Don't use an interactive Python if you change sys.ps1 or
794sys.ps2 from their default values, or if you're running code that
795prints `>>> ' or `... ' at the start of a line. `python-mode' can't
796distinguish your output from Python's output, and assumes that `>>> '
797at the start of a line is a prompt from Python. Similarly, the Emacs
798Shell mode code assumes that both `>>> ' and `... ' at the start of a
799line are Python prompts. Bad things can happen if you fool either
800mode.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000801
802Warning: If you do any editing *in* the process buffer *while* the
803buffer is accepting output from Python, do NOT attempt to `undo' the
804changes. Some of the output (nowhere near the parts you changed!) may
805be lost if you do. This appears to be an Emacs bug, an unfortunate
806interaction between undo and process filters; the same problem exists in
807non-Python process buffers using the default (Emacs-supplied) process
808filter."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000809 ;; BAW - should undo be disabled in the python process buffer, if
810 ;; this bug still exists?
Barry Warsaw7ae77681994-12-12 20:38:05 +0000811 (interactive)
812 (if py-this-is-emacs-19-p
813 (progn
814 (require 'comint)
815 (switch-to-buffer-other-window
816 (make-comint "Python" py-python-command)))
817 (progn
818 (require 'shell)
819 (switch-to-buffer-other-window
Barry Warsaw9fbcc6a1996-01-23 22:52:02 +0000820 (apply (if (fboundp 'make-shell) 'make-shell 'make-comint)
Barry Warsaw6e98f331995-07-05 22:06:50 +0000821 "Python" py-python-command nil))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000822 (make-local-variable 'shell-prompt-pattern)
823 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
824 (set-process-filter (get-buffer-process (current-buffer))
825 'py-process-filter)
826 (set-syntax-table py-mode-syntax-table))
827
828(defun py-execute-region (start end)
829 "Send the region between START and END to a Python interpreter.
830If there is a *Python* process it is used.
831
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000832Hint: If you want to execute part of a Python file several times
833\(e.g., perhaps you're developing a function and want to flesh it out
834a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
835the region of interest, and send the code to a *Python* process via
836`\\[py-execute-buffer]' instead.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000837
838Following are subtleties to note when using a *Python* process:
839
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000840If a *Python* process is used, the region is copied into a temporary
841file (in directory `py-temp-directory'), and an `execfile' command is
842sent to Python naming that file. If you send regions faster than
843Python can execute them, `python-mode' will save them into distinct
844temp files, and execute the next one in the queue the next time it
845sees a `>>> ' prompt from Python. Each time this happens, the process
846buffer is popped into a window (if it's not already in some window) so
847you can see it, and a comment of the form
Barry Warsaw7ae77681994-12-12 20:38:05 +0000848
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000849 \t## working on region in file <name> ...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000850
851is inserted at the end.
852
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000853Caution: No more than 26 regions can be pending at any given time.
854This limit is (indirectly) inherited from libc's mktemp(3).
855`python-mode' does not try to protect you from exceeding the limit.
856It's extremely unlikely that you'll get anywhere close to the limit in
857practice, unless you're trying to be a jerk <grin>.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000858
859See the `\\[py-shell]' docs for additional warnings."
860 (interactive "r")
861 (or (< start end) (error "Region is empty"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000862 (let ((pyproc (get-process "Python"))
863 fname)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000864 (if (null pyproc)
865 (shell-command-on-region start end py-python-command)
866 ;; else feed it thru a temp file
867 (setq fname (py-make-temp-name))
868 (write-region start end fname nil 'no-msg)
869 (setq py-file-queue (append py-file-queue (list fname)))
870 (if (cdr py-file-queue)
871 (message "File %s queued for execution" fname)
872 ;; else
873 (py-execute-file pyproc fname)))))
874
875(defun py-execute-file (pyproc fname)
876 (py-append-to-process-buffer
877 pyproc
878 (format "## working on region in file %s ...\n" fname))
879 (process-send-string pyproc (format "execfile('%s')\n" fname)))
880
881(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000882 (let ((curbuf (current-buffer))
883 (pbuf (process-buffer pyproc))
884 (pmark (process-mark pyproc))
885 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000886
887 ;; make sure we switch to a different buffer at least once. if we
888 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000889 ;; window, and point is before the end, and lots of output is
890 ;; coming at a fast pace, then (a) simple cursor-movement commands
891 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
892 ;; to have a visible effect (the window just doesn't get updated,
893 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
894 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000895 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000896 ;; #b makes no sense to me at all. #a almost makes sense: unless
897 ;; we actually change buffers, set_buffer_internal in buffer.c
898 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
899 ;; seems to make the Emacs command loop reluctant to update the
900 ;; display. Perhaps the default process filter in process.c's
901 ;; read_process_output has update_mode_lines++ for a similar
902 ;; reason? beats me ...
903
904 ;; BAW - we want to check to see if this still applies
Barry Warsaw7ae77681994-12-12 20:38:05 +0000905 (if (eq curbuf pbuf) ; mysterious ugly hack
906 (set-buffer (get-buffer-create "*scratch*")))
907
908 (set-buffer pbuf)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000909 (let* ((start (point))
910 (goback (< start pmark))
Barry Warsawe64bfee1995-07-05 22:27:23 +0000911 (goend (and (not goback) (= start (point-max))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000912 (buffer-read-only nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000913 (goto-char pmark)
914 (insert string)
915 (move-marker pmark (point))
916 (setq file-finished
917 (and py-file-queue
918 (equal ">>> "
919 (buffer-substring
920 (prog2 (beginning-of-line) (point)
921 (goto-char pmark))
922 (point)))))
923 (if goback (goto-char start)
924 ;; else
925 (if py-scroll-process-buffer
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000926 (let* ((pop-up-windows t)
927 (pwin (display-buffer pbuf)))
Barry Warsawe64bfee1995-07-05 22:27:23 +0000928 (set-window-point pwin (point)))))
929 (set-buffer curbuf)
930 (if file-finished
931 (progn
932 (py-delete-file-silently (car py-file-queue))
933 (setq py-file-queue (cdr py-file-queue))
934 (if py-file-queue
935 (py-execute-file pyproc (car py-file-queue)))))
936 (and goend
937 (progn (set-buffer pbuf)
938 (goto-char (point-max))))
939 )))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000940
941(defun py-execute-buffer ()
942 "Send the contents of the buffer to a Python interpreter.
943If there is a *Python* process buffer it is used. If a clipping
944restriction is in effect, only the accessible portion of the buffer is
945sent. A trailing newline will be supplied if needed.
946
947See the `\\[py-execute-region]' docs for an account of some subtleties."
948 (interactive)
949 (py-execute-region (point-min) (point-max)))
950
951
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000952
953;; Functions for Python style indentation
Barry Warsaw03970731996-07-03 23:12:52 +0000954(defun py-delete-char (count)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000955 "Reduce indentation or delete character.
956If point is at the leftmost column, deletes the preceding newline.
957
958Else if point is at the leftmost non-blank character of a line that is
959neither a continuation line nor a non-indenting comment line, or if
960point is at the end of a blank line, reduces the indentation to match
961that of the line that opened the current block of code. The line that
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000962opened the block is displayed in the echo area to help you keep track
Barry Warsaw03970731996-07-03 23:12:52 +0000963of where you are. With numeric count, outdents that many blocks (but
964not past column zero).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000965
966Else the preceding character is deleted, converting a tab to spaces if
Barry Warsaw03970731996-07-03 23:12:52 +0000967needed so that only a single column position is deleted. Numeric
968argument delets that many characters."
969 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000970 (if (or (/= (current-indentation) (current-column))
971 (bolp)
972 (py-continuation-line-p)
Barry Warsaw6e527d21996-08-01 15:57:48 +0000973 (not py-honor-comment-indentation)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000974 (looking-at "#[^ \t\n]")) ; non-indenting #
Barry Warsaw03970731996-07-03 23:12:52 +0000975 (backward-delete-char-untabify count)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000976 ;; else indent the same as the colon line that opened the block
977
978 ;; force non-blank so py-goto-block-up doesn't ignore it
979 (insert-char ?* 1)
980 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000981 (let ((base-indent 0) ; indentation of base line
982 (base-text "") ; and text of base line
983 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +0000984 (save-excursion
985 (while (< 0 count)
986 (condition-case nil ; in case no enclosing block
987 (progn
988 (py-goto-block-up 'no-mark)
989 (setq base-indent (current-indentation)
990 base-text (py-suck-up-leading-text)
991 base-found-p t))
992 (error nil))
993 (setq count (1- count))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000994 (delete-char 1) ; toss the dummy character
995 (delete-horizontal-space)
996 (indent-to base-indent)
997 (if base-found-p
998 (message "Closes block: %s" base-text)))))
999
Barry Warsawfc8a01f1995-03-09 16:07:29 +00001000;; required for pending-del and delsel modes
1001(put 'py-delete-char 'delete-selection 'supersede)
1002(put 'py-delete-char 'pending-delete 'supersede)
1003
Barry Warsaw7ae77681994-12-12 20:38:05 +00001004(defun py-indent-line ()
1005 "Fix the indentation of the current line according to Python rules."
1006 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001007 (let* ((ci (current-indentation))
1008 (move-to-indentation-p (<= (current-column) ci))
Barry Warsawb86bbad1995-03-14 15:56:35 +00001009 (need (py-compute-indentation)))
Barry Warsaw4f009fb1995-03-14 20:53:08 +00001010 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001011 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +00001012 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001013 (if (/= ci need)
1014 (save-excursion
1015 (beginning-of-line)
1016 (delete-horizontal-space)
1017 (indent-to need)))
1018 (if move-to-indentation-p (back-to-indentation))))
1019
1020(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001021 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001022This is just `strives to' because correct indentation can't be computed
1023from scratch for Python code. In general, deletes the whitespace before
1024point, inserts a newline, and takes an educated guess as to how you want
1025the new line indented."
1026 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001027 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001028 (if (< ci (current-column)) ; if point beyond indentation
1029 (newline-and-indent)
1030 ;; else try to act like newline-and-indent "normally" acts
1031 (beginning-of-line)
1032 (insert-char ?\n 1)
1033 (move-to-column ci))))
1034
1035(defun py-compute-indentation ()
1036 (save-excursion
Barry Warsaw095e9c61995-09-19 20:01:42 +00001037 (let ((pps (parse-partial-sexp (save-excursion
1038 (beginning-of-python-def-or-class)
1039 (point))
1040 (point))))
1041 (beginning-of-line)
1042 (cond
1043 ;; are we inside a string or comment?
1044 ((or (nth 3 pps) (nth 4 pps))
1045 (save-excursion
1046 (if (not py-align-multiline-strings-p) 0
1047 ;; skip back over blank & non-indenting comment lines
1048 ;; note: will skip a blank or non-indenting comment line
1049 ;; that happens to be a continuation line too
1050 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1051 (back-to-indentation)
1052 (current-column))))
1053 ;; are we on a continuation line?
1054 ((py-continuation-line-p)
1055 (let ((startpos (point))
1056 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001057 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001058 (if open-bracket-pos
1059 (progn
1060 ;; align with first item in list; else a normal
1061 ;; indent beyond the line with the open bracket
1062 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1063 ;; is the first list item on the same line?
1064 (skip-chars-forward " \t")
1065 (if (null (memq (following-char) '(?\n ?# ?\\)))
1066 ; yes, so line up with it
1067 (current-column)
1068 ;; first list item on another line, or doesn't exist yet
1069 (forward-line 1)
1070 (while (and (< (point) startpos)
1071 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1072 (forward-line 1))
1073 (if (< (point) startpos)
1074 ;; again mimic the first list item
1075 (current-indentation)
1076 ;; else they're about to enter the first item
1077 (goto-char open-bracket-pos)
1078 (+ (current-indentation) py-indent-offset))))
1079
1080 ;; else on backslash continuation line
1081 (forward-line -1)
1082 (if (py-continuation-line-p) ; on at least 3rd line in block
1083 (current-indentation) ; so just continue the pattern
1084 ;; else started on 2nd line in block, so indent more.
1085 ;; if base line is an assignment with a start on a RHS,
1086 ;; indent to 2 beyond the leftmost "="; else skip first
1087 ;; chunk of non-whitespace characters on base line, + 1 more
1088 ;; column
1089 (end-of-line)
1090 (setq endpos (point) searching t)
1091 (back-to-indentation)
1092 (setq startpos (point))
1093 ;; look at all "=" from left to right, stopping at first
1094 ;; one not nested in a list or string
1095 (while searching
1096 (skip-chars-forward "^=" endpos)
1097 (if (= (point) endpos)
1098 (setq searching nil)
1099 (forward-char 1)
1100 (setq state (parse-partial-sexp startpos (point)))
1101 (if (and (zerop (car state)) ; not in a bracket
1102 (null (nth 3 state))) ; & not in a string
1103 (progn
1104 (setq searching nil) ; done searching in any case
1105 (setq found
1106 (not (or
1107 (eq (following-char) ?=)
1108 (memq (char-after (- (point) 2))
1109 '(?< ?> ?!)))))))))
1110 (if (or (not found) ; not an assignment
1111 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1112 (progn
1113 (goto-char startpos)
1114 (skip-chars-forward "^ \t\n")))
1115 (1+ (current-column))))))
1116
1117 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001118 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001119
Barry Warsawa7891711996-08-01 15:53:09 +00001120 ;; Dfn: "Indenting comment line". A line containing only a
1121 ;; comment, but which is treated like a statement for
1122 ;; indentation calculation purposes. Such lines are only
1123 ;; treated specially by the mode; they are not treated
1124 ;; specially by the Python interpreter.
1125
1126 ;; The rules for indenting comment lines are a line where:
1127 ;; - the first non-whitespace character is `#', and
1128 ;; - the character following the `#' is whitespace, and
1129 ;; - the line is outdented with respect to (i.e. to the left
1130 ;; of) the indentation of the preceding non-blank line.
1131
1132 ;; The first non-blank line following an indenting comment
1133 ;; line is given the same amount of indentation as the
1134 ;; indenting comment line.
1135
1136 ;; All other comment-only lines are ignored for indentation
1137 ;; purposes.
1138
1139 ;; Are we looking at a comment-only line which is *not* an
1140 ;; indenting comment line? If so, we assume that its been
1141 ;; placed at the desired indentation, so leave it alone.
1142 ;; Indenting comment lines are aligned as statements down
1143 ;; below.
1144 ((and (looking-at "[ \t]*#[^ \t\n]")
1145 ;; NOTE: this test will not be performed in older Emacsen
1146 (fboundp 'forward-comment)
1147 (<= (current-indentation)
1148 (save-excursion
1149 (forward-comment (- (point-max)))
1150 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001151 (current-indentation))
1152
1153 ;; else indentation based on that of the statement that
1154 ;; precedes us; use the first line of that statement to
1155 ;; establish the base, in case the user forced a non-std
1156 ;; indentation for the continuation lines (if any)
1157 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001158 ;; skip back over blank & non-indenting comment lines note:
1159 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001160 ;; happens to be a continuation line too. use fast Emacs 19
1161 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001162 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001163 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001164 (forward-comment (- (point-max)))
Barry Warsaw6d627751996-03-06 18:41:38 +00001165 (let (done)
1166 (while (not done)
1167 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
1168 nil 'move)
1169 (setq done (or (eq py-honor-comment-indentation t)
1170 (bobp)
1171 (/= (following-char) ?#)
1172 (not (zerop (current-column)))))
1173 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001174 ;; if we landed inside a string, go to the beginning of that
1175 ;; string. this handles triple quoted, multi-line spanning
1176 ;; strings.
1177 (py-goto-initial-line)
Barry Warsawf831d811996-07-31 20:42:59 +00001178 (+ (current-indentation)
1179 (if (py-statement-opens-block-p)
1180 py-indent-offset
1181 (if (py-statement-closes-block-p)
1182 (- py-indent-offset)
1183 0)))
1184 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001185
1186(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001187 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001188By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001189`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +00001190Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001191`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +00001192their own buffer-local copy), both those currently existing and those
1193created later in the Emacs session.
1194
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001195Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001196There's no excuse for such foolishness, but sometimes you have to deal
1197with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001198`py-indent-offset' to what it thinks it was when they created the
1199mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001200
1201Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001202looking for a line that opens a block of code. `py-indent-offset' is
1203set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001204statement following it. If the search doesn't succeed going forward,
1205it's tried again going backward."
1206 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001207 (let (new-value
1208 (start (point))
1209 restart
1210 (found nil)
1211 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001212 (py-goto-initial-line)
1213 (while (not (or found (eobp)))
1214 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1215 (progn
1216 (setq restart (point))
1217 (py-goto-initial-line)
1218 (if (py-statement-opens-block-p)
1219 (setq found t)
1220 (goto-char restart)))))
1221 (if found
1222 ()
1223 (goto-char start)
1224 (py-goto-initial-line)
1225 (while (not (or found (bobp)))
1226 (setq found
1227 (and
1228 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1229 (or (py-goto-initial-line) t) ; always true -- side effect
1230 (py-statement-opens-block-p)))))
1231 (setq colon-indent (current-indentation)
1232 found (and found (zerop (py-next-statement 1)))
1233 new-value (- (current-indentation) colon-indent))
1234 (goto-char start)
1235 (if found
1236 (progn
1237 (funcall (if global 'kill-local-variable 'make-local-variable)
1238 'py-indent-offset)
1239 (setq py-indent-offset new-value)
1240 (message "%s value of py-indent-offset set to %d"
1241 (if global "Global" "Local")
1242 py-indent-offset))
1243 (error "Sorry, couldn't guess a value for py-indent-offset"))))
1244
1245(defun py-shift-region (start end count)
1246 (save-excursion
1247 (goto-char end) (beginning-of-line) (setq end (point))
1248 (goto-char start) (beginning-of-line) (setq start (point))
1249 (indent-rigidly start end count)))
1250
1251(defun py-shift-region-left (start end &optional count)
1252 "Shift region of Python code to the left.
1253The lines from the line containing the start of the current region up
1254to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001255shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001256
1257If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001258many columns. With no active region, outdent only the current line.
1259You cannot outdent the region if any line is already at column zero."
1260 (interactive
1261 (let ((p (point))
1262 (m (mark))
1263 (arg current-prefix-arg))
1264 (if m
1265 (list (min p m) (max p m) arg)
1266 (list p (save-excursion (forward-line 1) (point)) arg))))
1267 ;; if any line is at column zero, don't shift the region
1268 (save-excursion
1269 (goto-char start)
1270 (while (< (point) end)
1271 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00001272 (if (and (zerop (current-column))
1273 (not (looking-at "\\s *$")))
Barry Warsawdea4a291996-07-03 22:59:12 +00001274 (error "Region is at left edge."))
1275 (forward-line 1)))
1276 (py-shift-region start end (- (prefix-numeric-value
1277 (or count py-indent-offset))))
1278 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001279
1280(defun py-shift-region-right (start end &optional count)
1281 "Shift region of Python code to the right.
1282The lines from the line containing the start of the current region up
1283to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001284shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001285
1286If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001287many columns. With no active region, indent only the current line."
1288 (interactive
1289 (let ((p (point))
1290 (m (mark))
1291 (arg current-prefix-arg))
1292 (if m
1293 (list (min p m) (max p m) arg)
1294 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001295 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00001296 (or count py-indent-offset)))
1297 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001298
1299(defun py-indent-region (start end &optional indent-offset)
1300 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00001301
Barry Warsaw7ae77681994-12-12 20:38:05 +00001302The lines from the line containing the start of the current region up
1303to (but not including) the line containing the end of the region are
1304reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001305character in the first column, the first line is left alone and the
1306rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00001307region is reindented with respect to the (closest code or indenting
1308comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001309
1310This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001311control structures are introduced or removed, or to reformat code
1312using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001313
1314If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001315the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00001316used.
1317
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001318Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00001319is called! This function does not compute proper indentation from
1320scratch (that's impossible in Python), it merely adjusts the existing
1321indentation to be correct in context.
1322
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001323Warning: This function really has no idea what to do with
1324non-indenting comment lines, and shifts them as if they were indenting
1325comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001326
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001327Special cases: whitespace is deleted from blank lines; continuation
1328lines are shifted by the same amount their initial line was shifted,
1329in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00001330initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001331 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001332 (save-excursion
1333 (goto-char end) (beginning-of-line) (setq end (point-marker))
1334 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001335 (let ((py-indent-offset (prefix-numeric-value
1336 (or indent-offset py-indent-offset)))
1337 (indents '(-1)) ; stack of active indent levels
1338 (target-column 0) ; column to which to indent
1339 (base-shifted-by 0) ; amount last base line was shifted
1340 (indent-base (if (looking-at "[ \t\n]")
1341 (py-compute-indentation)
1342 0))
1343 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001344 (while (< (point) end)
1345 (setq ci (current-indentation))
1346 ;; figure out appropriate target column
1347 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001348 ((or (eq (following-char) ?#) ; comment in column 1
1349 (looking-at "[ \t]*$")) ; entirely blank
1350 (setq target-column 0))
1351 ((py-continuation-line-p) ; shift relative to base line
1352 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001353 (t ; new base line
1354 (if (> ci (car indents)) ; going deeper; push it
1355 (setq indents (cons ci indents))
1356 ;; else we should have seen this indent before
1357 (setq indents (memq ci indents)) ; pop deeper indents
1358 (if (null indents)
1359 (error "Bad indentation in region, at line %d"
1360 (save-restriction
1361 (widen)
1362 (1+ (count-lines 1 (point)))))))
1363 (setq target-column (+ indent-base
1364 (* py-indent-offset
1365 (- (length indents) 2))))
1366 (setq base-shifted-by (- target-column ci))))
1367 ;; shift as needed
1368 (if (/= ci target-column)
1369 (progn
1370 (delete-horizontal-space)
1371 (indent-to target-column)))
1372 (forward-line 1))))
1373 (set-marker end nil))
1374
Barry Warsawa7891711996-08-01 15:53:09 +00001375(defun py-comment-region (beg end &optional arg)
1376 "Like `comment-region' but uses double hash (`#') comment starter."
1377 (interactive "r\nP")
Barry Warsaw3fcaf611996-08-01 20:11:51 +00001378 (let ((comment-start py-block-comment-prefix))
Barry Warsawa7891711996-08-01 15:53:09 +00001379 (comment-region beg end arg)))
1380
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001381
1382;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00001383(defun py-previous-statement (count)
1384 "Go to the start of previous Python statement.
1385If the statement at point is the i'th Python statement, goes to the
1386start of statement i-COUNT. If there is no such statement, goes to the
1387first statement. Returns count of statements left to move.
1388`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001389 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001390 (if (< count 0) (py-next-statement (- count))
1391 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001392 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001393 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001394 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001395 (> count 0)
1396 (zerop (forward-line -1))
1397 (py-goto-statement-at-or-above))
1398 (setq count (1- count)))
1399 (if (> count 0) (goto-char start)))
1400 count))
1401
1402(defun py-next-statement (count)
1403 "Go to the start of next Python statement.
1404If the statement at point is the i'th Python statement, goes to the
1405start of statement i+COUNT. If there is no such statement, goes to the
1406last statement. Returns count of statements left to move. `Statements'
1407do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001408 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001409 (if (< count 0) (py-previous-statement (- count))
1410 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001411 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001412 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001413 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001414 (> count 0)
1415 (py-goto-statement-below))
1416 (setq count (1- count)))
1417 (if (> count 0) (goto-char start)))
1418 count))
1419
1420(defun py-goto-block-up (&optional nomark)
1421 "Move up to start of current block.
1422Go to the statement that starts the smallest enclosing block; roughly
1423speaking, this will be the closest preceding statement that ends with a
1424colon and is indented less than the statement you started on. If
1425successful, also sets the mark to the starting point.
1426
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001427`\\[py-mark-block]' can be used afterward to mark the whole code
1428block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001429
1430If called from a program, the mark will not be set if optional argument
1431NOMARK is not nil."
1432 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001433 (let ((start (point))
1434 (found nil)
1435 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001436 (py-goto-initial-line)
1437 ;; if on blank or non-indenting comment line, use the preceding stmt
1438 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1439 (progn
1440 (py-goto-statement-at-or-above)
1441 (setq found (py-statement-opens-block-p))))
1442 ;; search back for colon line indented less
1443 (setq initial-indent (current-indentation))
1444 (if (zerop initial-indent)
1445 ;; force fast exit
1446 (goto-char (point-min)))
1447 (while (not (or found (bobp)))
1448 (setq found
1449 (and
1450 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1451 (or (py-goto-initial-line) t) ; always true -- side effect
1452 (< (current-indentation) initial-indent)
1453 (py-statement-opens-block-p))))
1454 (if found
1455 (progn
1456 (or nomark (push-mark start))
1457 (back-to-indentation))
1458 (goto-char start)
1459 (error "Enclosing block not found"))))
1460
1461(defun beginning-of-python-def-or-class (&optional class)
1462 "Move point to start of def (or class, with prefix arg).
1463
1464Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001465arg, looks for a `class' instead. The docs assume the `def' case;
1466just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001467
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001468If point is in a def statement already, and after the `d', simply
1469moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001470
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001471Else (point is not in a def statement, or at or before the `d' of a
1472def statement), searches for the closest preceding def statement, and
1473leaves point at its start. If no such statement can be found, leaves
1474point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001475
1476Returns t iff a def statement is found by these rules.
1477
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001478Note that doing this command repeatedly will take you closer to the
1479start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001480
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001481If you want to mark the current def/class, see
1482`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001483 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001484 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1485 (start-of-line (progn (beginning-of-line) (point)))
1486 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001487 (if (or (/= start-of-stmt start-of-line)
1488 (not at-or-before-p))
1489 (end-of-line)) ; OK to match on this line
1490 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001491 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001492
1493(defun end-of-python-def-or-class (&optional class)
1494 "Move point beyond end of def (or class, with prefix arg) body.
1495
1496By default, looks for an appropriate `def'. If you supply a prefix arg,
1497looks for a `class' instead. The docs assume the `def' case; just
1498substitute `class' for `def' for the other case.
1499
1500If point is in a def statement already, this is the def we use.
1501
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001502Else if the def found by `\\[beginning-of-python-def-or-class]'
1503contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001504
1505Else we search forward for the closest following def, and use that.
1506
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001507If a def can be found by these rules, point is moved to the start of
1508the line immediately following the def block, and the position of the
1509start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001510
1511Else point is moved to the end of the buffer, and nil is returned.
1512
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001513Note that doing this command repeatedly will take you closer to the
1514end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001515
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001516If you want to mark the current def/class, see
1517`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001518 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001519 (let ((start (progn (py-goto-initial-line) (point)))
1520 (which (if class "class" "def"))
1521 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001522 ;; move point to start of appropriate def/class
1523 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1524 (setq state 'at-beginning)
1525 ;; else see if beginning-of-python-def-or-class hits container
1526 (if (and (beginning-of-python-def-or-class class)
1527 (progn (py-goto-beyond-block)
1528 (> (point) start)))
1529 (setq state 'at-end)
1530 ;; else search forward
1531 (goto-char start)
1532 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1533 (progn (setq state 'at-beginning)
1534 (beginning-of-line)))))
1535 (cond
1536 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1537 ((eq state 'at-end) t)
1538 ((eq state 'not-found) nil)
1539 (t (error "internal error in end-of-python-def-or-class")))))
1540
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001541
1542;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001543(defun py-mark-block (&optional extend just-move)
1544 "Mark following block of lines. With prefix arg, mark structure.
1545Easier to use than explain. It sets the region to an `interesting'
1546block of succeeding lines. If point is on a blank line, it goes down to
1547the next non-blank line. That will be the start of the region. The end
1548of the region depends on the kind of line at the start:
1549
1550 - If a comment, the region will include all succeeding comment lines up
1551 to (but not including) the next non-comment line (if any).
1552
1553 - Else if a prefix arg is given, and the line begins one of these
1554 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001555
1556 if elif else try except finally for while def class
1557
Barry Warsaw7ae77681994-12-12 20:38:05 +00001558 the region will be set to the body of the structure, including
1559 following blocks that `belong' to it, but excluding trailing blank
1560 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001561 and all (if any) of the following `except' and `finally' blocks
1562 that belong to the `try' structure will be in the region. Ditto
1563 for if/elif/else, for/else and while/else structures, and (a bit
1564 degenerate, since they're always one-block structures) def and
1565 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001566
1567 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001568 block (see list above), and the block is not a `one-liner' (i.e.,
1569 the statement ends with a colon, not with code), the region will
1570 include all succeeding lines up to (but not including) the next
1571 code statement (if any) that's indented no more than the starting
1572 line, except that trailing blank and comment lines are excluded.
1573 E.g., if the starting line begins a multi-statement `def'
1574 structure, the region will be set to the full function definition,
1575 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001576
1577 - Else the region will include all succeeding lines up to (but not
1578 including) the next blank line, or code or indenting-comment line
1579 indented strictly less than the starting line. Trailing indenting
1580 comment lines are included in this case, but not trailing blank
1581 lines.
1582
1583A msg identifying the location of the mark is displayed in the echo
1584area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1585
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001586If called from a program, optional argument EXTEND plays the role of
1587the prefix arg, and if optional argument JUST-MOVE is not nil, just
1588moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001589 (interactive "P") ; raw prefix arg
1590 (py-goto-initial-line)
1591 ;; skip over blank lines
1592 (while (and
1593 (looking-at "[ \t]*$") ; while blank line
1594 (not (eobp))) ; & somewhere to go
1595 (forward-line 1))
1596 (if (eobp)
1597 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001598 (let ((initial-pos (point))
1599 (initial-indent (current-indentation))
1600 last-pos ; position of last stmt in region
1601 (followers
1602 '((if elif else) (elif elif else) (else)
1603 (try except finally) (except except) (finally)
1604 (for else) (while else)
1605 (def) (class) ) )
1606 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001607
1608 (cond
1609 ;; if comment line, suck up the following comment lines
1610 ((looking-at "[ \t]*#")
1611 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1612 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1613 (setq last-pos (point)))
1614
1615 ;; else if line is a block line and EXTEND given, suck up
1616 ;; the whole structure
1617 ((and extend
1618 (setq first-symbol (py-suck-up-first-keyword) )
1619 (assq first-symbol followers))
1620 (while (and
1621 (or (py-goto-beyond-block) t) ; side effect
1622 (forward-line -1) ; side effect
1623 (setq last-pos (point)) ; side effect
1624 (py-goto-statement-below)
1625 (= (current-indentation) initial-indent)
1626 (setq next-symbol (py-suck-up-first-keyword))
1627 (memq next-symbol (cdr (assq first-symbol followers))))
1628 (setq first-symbol next-symbol)))
1629
1630 ;; else if line *opens* a block, search for next stmt indented <=
1631 ((py-statement-opens-block-p)
1632 (while (and
1633 (setq last-pos (point)) ; always true -- side effect
1634 (py-goto-statement-below)
1635 (> (current-indentation) initial-indent))
1636 nil))
1637
1638 ;; else plain code line; stop at next blank line, or stmt or
1639 ;; indenting comment line indented <
1640 (t
1641 (while (and
1642 (setq last-pos (point)) ; always true -- side effect
1643 (or (py-goto-beyond-final-line) t)
1644 (not (looking-at "[ \t]*$")) ; stop at blank line
1645 (or
1646 (>= (current-indentation) initial-indent)
1647 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1648 nil)))
1649
1650 ;; skip to end of last stmt
1651 (goto-char last-pos)
1652 (py-goto-beyond-final-line)
1653
1654 ;; set mark & display
1655 (if just-move
1656 () ; just return
1657 (push-mark (point) 'no-msg)
1658 (forward-line -1)
1659 (message "Mark set after: %s" (py-suck-up-leading-text))
1660 (goto-char initial-pos))))
1661
1662(defun mark-python-def-or-class (&optional class)
1663 "Set region to body of def (or class, with prefix arg) enclosing point.
1664Pushes the current mark, then point, on the mark ring (all language
1665modes do this, but although it's handy it's never documented ...).
1666
1667In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001668hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1669`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001670
1671And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001672Turned out that was more confusing than useful: the `goto start' and
1673`goto end' commands are usually used to search through a file, and
1674people expect them to act a lot like `search backward' and `search
1675forward' string-search commands. But because Python `def' and `class'
1676can nest to arbitrary levels, finding the smallest def containing
1677point cannot be done via a simple backward search: the def containing
1678point may not be the closest preceding def, or even the closest
1679preceding def that's indented less. The fancy algorithm required is
1680appropriate for the usual uses of this `mark' command, but not for the
1681`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001682
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001683So the def marked by this command may not be the one either of the
1684`goto' commands find: If point is on a blank or non-indenting comment
1685line, moves back to start of the closest preceding code statement or
1686indenting comment line. If this is a `def' statement, that's the def
1687we use. Else searches for the smallest enclosing `def' block and uses
1688that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001689
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001690When an enclosing def is found: The mark is left immediately beyond
1691the last line of the def block. Point is left at the start of the
1692def, except that: if the def is preceded by a number of comment lines
1693followed by (at most) one optional blank line, point is left at the
1694start of the comments; else if the def is preceded by a blank line,
1695point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001696
1697The intent is to mark the containing def/class and its associated
1698documentation, to make moving and duplicating functions and classes
1699pleasant."
1700 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001701 (let ((start (point))
1702 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001703 (push-mark start)
1704 (if (not (py-go-up-tree-to-keyword which))
1705 (progn (goto-char start)
1706 (error "Enclosing %s not found" which))
1707 ;; else enclosing def/class found
1708 (setq start (point))
1709 (py-goto-beyond-block)
1710 (push-mark (point))
1711 (goto-char start)
1712 (if (zerop (forward-line -1)) ; if there is a preceding line
1713 (progn
1714 (if (looking-at "[ \t]*$") ; it's blank
1715 (setq start (point)) ; so reset start point
1716 (goto-char start)) ; else try again
1717 (if (zerop (forward-line -1))
1718 (if (looking-at "[ \t]*#") ; a comment
1719 ;; look back for non-comment line
1720 ;; tricky: note that the regexp matches a blank
1721 ;; line, cuz \n is in the 2nd character class
1722 (and
1723 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1724 (forward-line 1))
1725 ;; no comment, so go back
1726 (goto-char start))))))))
1727
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001728;; ripped from cc-mode
1729(defun py-forward-into-nomenclature (&optional arg)
1730 "Move forward to end of a nomenclature section or word.
1731With arg, to it arg times.
1732
1733A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
1734 (interactive "p")
1735 (let ((case-fold-search nil))
1736 (if (> arg 0)
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00001737 (re-search-forward
1738 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
1739 (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001740 (while (and (< arg 0)
1741 (re-search-backward
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00001742 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001743 (point-min) 0))
1744 (forward-char 1)
1745 (setq arg (1+ arg)))))
1746 (py-keep-region-active))
1747
1748(defun py-backward-into-nomenclature (&optional arg)
1749 "Move backward to beginning of a nomenclature section or word.
1750With optional ARG, move that many times. If ARG is negative, move
1751forward.
1752
1753A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
1754 (interactive "p")
1755 (py-forward-into-nomenclature (- arg))
1756 (py-keep-region-active))
1757
1758
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001759
1760;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001761
1762;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001763;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1764;; out of the right places, along with the keys they're on & current
1765;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00001766(defun py-dump-help-string (str)
1767 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001768 (let ((locals (buffer-local-variables))
1769 funckind funcname func funcdoc
1770 (start 0) mstart end
1771 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001772 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1773 (setq mstart (match-beginning 0) end (match-end 0)
1774 funckind (substring str (match-beginning 1) (match-end 1))
1775 funcname (substring str (match-beginning 2) (match-end 2))
1776 func (intern funcname))
1777 (princ (substitute-command-keys (substring str start mstart)))
1778 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001779 ((equal funckind "c") ; command
1780 (setq funcdoc (documentation func)
1781 keys (concat
1782 "Key(s): "
1783 (mapconcat 'key-description
1784 (where-is-internal func py-mode-map)
1785 ", "))))
1786 ((equal funckind "v") ; variable
1787 (setq funcdoc (substitute-command-keys
1788 (get func 'variable-documentation))
1789 keys (if (assq func locals)
1790 (concat
1791 "Local/Global values: "
1792 (prin1-to-string (symbol-value func))
1793 " / "
1794 (prin1-to-string (default-value func)))
1795 (concat
1796 "Value: "
1797 (prin1-to-string (symbol-value func))))))
1798 (t ; unexpected
1799 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001800 (princ (format "\n-> %s:\t%s\t%s\n\n"
1801 (if (equal funckind "c") "Command" "Variable")
1802 funcname keys))
1803 (princ funcdoc)
1804 (terpri)
1805 (setq start end))
1806 (princ (substitute-command-keys (substring str start))))
1807 (print-help-return-message)))
1808
1809(defun py-describe-mode ()
1810 "Dump long form of Python-mode docs."
1811 (interactive)
1812 (py-dump-help-string "Major mode for editing Python files.
1813Knows about Python indentation, tokens, comments and continuation lines.
1814Paragraphs are separated by blank lines only.
1815
1816Major sections below begin with the string `@'; specific function and
1817variable docs begin with `->'.
1818
1819@EXECUTING PYTHON CODE
1820
1821\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1822\\[py-execute-region]\tsends the current region
1823\\[py-shell]\tstarts a Python interpreter window; this will be used by
1824\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1825%c:py-execute-buffer
1826%c:py-execute-region
1827%c:py-shell
1828
1829@VARIABLES
1830
1831py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00001832py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00001833
1834py-python-command\tshell command to invoke Python interpreter
1835py-scroll-process-buffer\talways scroll Python process buffer
1836py-temp-directory\tdirectory used for temp files (if needed)
1837
1838py-beep-if-tab-change\tring the bell if tab-width is changed
1839%v:py-indent-offset
1840%v:py-block-comment-prefix
1841%v:py-python-command
1842%v:py-scroll-process-buffer
1843%v:py-temp-directory
1844%v:py-beep-if-tab-change
1845
1846@KINDS OF LINES
1847
1848Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001849preceding line ends with a backslash that's not part of a comment, or
1850the paren/bracket/brace nesting level at the start of the line is
1851non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001852
1853An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001854possibly blanks or tabs), a `comment line' (leftmost non-blank
1855character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001856
1857Comment Lines
1858
1859Although all comment lines are treated alike by Python, Python mode
1860recognizes two kinds that act differently with respect to indentation.
1861
1862An `indenting comment line' is a comment line with a blank, tab or
1863nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001864treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00001865indenting comment line will be indented like the comment line. All
1866other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001867following the initial `#') are `non-indenting comment lines', and
1868their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001869
1870Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001871whenever possible. Non-indenting comment lines are useful in cases
1872like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00001873
1874\ta = b # a very wordy single-line comment that ends up being
1875\t #... continued onto another line
1876
1877\tif a == b:
1878##\t\tprint 'panic!' # old code we've `commented out'
1879\t\treturn a
1880
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001881Since the `#...' and `##' comment lines have a non-whitespace
1882character following the initial `#', Python mode ignores them when
1883computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001884
1885Continuation Lines and Statements
1886
1887The Python-mode commands generally work on statements instead of on
1888individual lines, where a `statement' is a comment or blank line, or a
1889code line and all of its following continuation lines (if any)
1890considered as a single logical unit. The commands in this mode
1891generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001892statement containing point, even if point happens to be in the middle
1893of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001894
1895
1896@INDENTATION
1897
1898Primarily for entering new code:
1899\t\\[indent-for-tab-command]\t indent line appropriately
1900\t\\[py-newline-and-indent]\t insert newline, then indent
1901\t\\[py-delete-char]\t reduce indentation, or delete single character
1902
1903Primarily for reindenting existing code:
1904\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1905\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1906
1907\t\\[py-indent-region]\t reindent region to match its context
1908\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1909\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1910
1911Unlike most programming languages, Python uses indentation, and only
1912indentation, to specify block structure. Hence the indentation supplied
1913automatically by Python-mode is just an educated guess: only you know
1914the block structure you intend, so only you can supply correct
1915indentation.
1916
1917The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1918the indentation of preceding statements. E.g., assuming
1919py-indent-offset is 4, after you enter
1920\tif a > 0: \\[py-newline-and-indent]
1921the cursor will be moved to the position of the `_' (_ is not a
1922character in the file, it's just used here to indicate the location of
1923the cursor):
1924\tif a > 0:
1925\t _
1926If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1927to
1928\tif a > 0:
1929\t c = d
1930\t _
1931Python-mode cannot know whether that's what you intended, or whether
1932\tif a > 0:
1933\t c = d
1934\t_
1935was your intent. In general, Python-mode either reproduces the
1936indentation of the (closest code or indenting-comment) preceding
1937statement, or adds an extra py-indent-offset blanks if the preceding
1938statement has `:' as its last significant (non-whitespace and non-
1939comment) character. If the suggested indentation is too much, use
1940\\[py-delete-char] to reduce it.
1941
1942Continuation lines are given extra indentation. If you don't like the
1943suggested indentation, change it to something you do like, and Python-
1944mode will strive to indent later lines of the statement in the same way.
1945
1946If a line is a continuation line by virtue of being in an unclosed
1947paren/bracket/brace structure (`list', for short), the suggested
1948indentation depends on whether the current line contains the first item
1949in the list. If it does, it's indented py-indent-offset columns beyond
1950the indentation of the line containing the open bracket. If you don't
1951like that, change it by hand. The remaining items in the list will mimic
1952whatever indentation you give to the first item.
1953
1954If a line is a continuation line because the line preceding it ends with
1955a backslash, the third and following lines of the statement inherit their
1956indentation from the line preceding them. The indentation of the second
1957line in the statement depends on the form of the first (base) line: if
1958the base line is an assignment statement with anything more interesting
1959than the backslash following the leftmost assigning `=', the second line
1960is indented two columns beyond that `='. Else it's indented to two
1961columns beyond the leftmost solid chunk of non-whitespace characters on
1962the base line.
1963
1964Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1965repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1966structure you intend.
1967%c:indent-for-tab-command
1968%c:py-newline-and-indent
1969%c:py-delete-char
1970
1971
1972The next function may be handy when editing code you didn't write:
1973%c:py-guess-indent-offset
1974
1975
1976The remaining `indent' functions apply to a region of Python code. They
1977assume the block structure (equals indentation, in Python) of the region
1978is correct, and alter the indentation in various ways while preserving
1979the block structure:
1980%c:py-indent-region
1981%c:py-shift-region-left
1982%c:py-shift-region-right
1983
1984@MARKING & MANIPULATING REGIONS OF CODE
1985
1986\\[py-mark-block]\t mark block of lines
1987\\[mark-python-def-or-class]\t mark smallest enclosing def
1988\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00001989\\[comment-region]\t comment out region of code
1990\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00001991%c:py-mark-block
1992%c:mark-python-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00001993%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00001994
1995@MOVING POINT
1996
1997\\[py-previous-statement]\t move to statement preceding point
1998\\[py-next-statement]\t move to statement following point
1999\\[py-goto-block-up]\t move up to start of current block
2000\\[beginning-of-python-def-or-class]\t move to start of def
2001\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
2002\\[end-of-python-def-or-class]\t move to end of def
2003\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
2004
2005The first two move to one statement beyond the statement that contains
2006point. A numeric prefix argument tells them to move that many
2007statements instead. Blank lines, comment lines, and continuation lines
2008do not count as `statements' for these commands. So, e.g., you can go
2009to the first code statement in a file by entering
2010\t\\[beginning-of-buffer]\t to move to the top of the file
2011\t\\[py-next-statement]\t to skip over initial comments and blank lines
2012Or do `\\[py-previous-statement]' with a huge prefix argument.
2013%c:py-previous-statement
2014%c:py-next-statement
2015%c:py-goto-block-up
2016%c:beginning-of-python-def-or-class
2017%c:end-of-python-def-or-class
2018
2019@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2020
2021`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2022
2023`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2024overall class and def structure of a module.
2025
2026`\\[back-to-indentation]' moves point to a line's first non-blank character.
2027
2028`\\[indent-relative]' is handy for creating odd indentation.
2029
2030@OTHER EMACS HINTS
2031
2032If you don't like the default value of a variable, change its value to
2033whatever you do like by putting a `setq' line in your .emacs file.
2034E.g., to set the indentation increment to 4, put this line in your
2035.emacs:
2036\t(setq py-indent-offset 4)
2037To see the value of a variable, do `\\[describe-variable]' and enter the variable
2038name at the prompt.
2039
2040When entering a key sequence like `C-c C-n', it is not necessary to
2041release the CONTROL key after doing the `C-c' part -- it suffices to
2042press the CONTROL key, press and release `c' (while still holding down
2043CONTROL), press and release `n' (while still holding down CONTROL), &
2044then release CONTROL.
2045
2046Entering Python mode calls with no arguments the value of the variable
2047`python-mode-hook', if that value exists and is not nil; for backward
2048compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2049the Elisp manual for details.
2050
2051Obscure: When python-mode is first loaded, it looks for all bindings
2052to newline-and-indent in the global keymap, and shadows them with
2053local bindings to py-newline-and-indent."))
2054
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002055
2056;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002057(defvar py-parse-state-re
2058 (concat
2059 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2060 "\\|"
2061 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002062
Barry Warsaw7ae77681994-12-12 20:38:05 +00002063;; returns the parse state at point (see parse-partial-sexp docs)
2064(defun py-parse-state ()
2065 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002066 (let ((here (point))
Barry Warsaw170ffa71996-07-31 20:57:22 +00002067 pps done ci)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002068 (while (not done)
2069 ;; back up to the first preceding line (if any; else start of
2070 ;; buffer) that begins with a popular Python keyword, or a
2071 ;; non- whitespace and non-comment character. These are good
2072 ;; places to start parsing to see whether where we started is
2073 ;; at a non-zero nesting level. It may be slow for people who
2074 ;; write huge code blocks or huge lists ... tough beans.
2075 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw170ffa71996-07-31 20:57:22 +00002076 (setq ci (current-indentation))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002077 (beginning-of-line)
2078 (save-excursion
2079 (setq pps (parse-partial-sexp (point) here)))
2080 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw170ffa71996-07-31 20:57:22 +00002081 (setq done (or (zerop ci)
2082 (not (nth 3 pps))
2083 (bobp)))
2084 )
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002085 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002086
2087;; if point is at a non-zero nesting level, returns the number of the
2088;; character that opens the smallest enclosing unclosed list; else
2089;; returns nil.
2090(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002091 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002092 (if (zerop (car status))
2093 nil ; not in a nest
2094 (car (cdr status))))) ; char# of open bracket
2095
2096;; t iff preceding line ends with backslash that's not in a comment
2097(defun py-backslash-continuation-line-p ()
2098 (save-excursion
2099 (beginning-of-line)
2100 (and
2101 ;; use a cheap test first to avoid the regexp if possible
2102 ;; use 'eq' because char-after may return nil
2103 (eq (char-after (- (point) 2)) ?\\ )
2104 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002105 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002106 (looking-at py-continued-re))))
2107
2108;; t iff current line is a continuation line
2109(defun py-continuation-line-p ()
2110 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002111 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002112 (or (py-backslash-continuation-line-p)
2113 (py-nesting-level))))
2114
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002115;; go to initial line of current statement; usually this is the line
2116;; we're on, but if we're on the 2nd or following lines of a
2117;; continuation block, we need to go up to the first line of the
2118;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002119;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002120;; Tricky: We want to avoid quadratic-time behavior for long continued
2121;; blocks, whether of the backslash or open-bracket varieties, or a
2122;; mix of the two. The following manages to do that in the usual
2123;; cases.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002124(defun py-goto-initial-line ()
2125 (let ( open-bracket-pos )
2126 (while (py-continuation-line-p)
2127 (beginning-of-line)
2128 (if (py-backslash-continuation-line-p)
2129 (while (py-backslash-continuation-line-p)
2130 (forward-line -1))
2131 ;; else zip out of nested brackets/braces/parens
2132 (while (setq open-bracket-pos (py-nesting-level))
2133 (goto-char open-bracket-pos)))))
2134 (beginning-of-line))
2135
2136;; go to point right beyond final line of current statement; usually
2137;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002138;; statement we need to skip over the continuation lines. Tricky:
2139;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002140(defun py-goto-beyond-final-line ()
2141 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002142 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002143 (while (and (py-continuation-line-p)
2144 (not (eobp)))
2145 ;; skip over the backslash flavor
2146 (while (and (py-backslash-continuation-line-p)
2147 (not (eobp)))
2148 (forward-line 1))
2149 ;; if in nest, zip to the end of the nest
2150 (setq state (py-parse-state))
2151 (if (and (not (zerop (car state)))
2152 (not (eobp)))
2153 (progn
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002154 ;; BUG ALERT: I could swear, from reading the docs, that
Barry Warsaw7ae77681994-12-12 20:38:05 +00002155 ;; the 3rd argument should be plain 0
2156 (parse-partial-sexp (point) (point-max) (- 0 (car state))
2157 nil state)
2158 (forward-line 1))))))
2159
2160;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002161;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00002162(defun py-statement-opens-block-p ()
2163 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002164 (let ((start (point))
2165 (finish (progn (py-goto-beyond-final-line) (1- (point))))
2166 (searching t)
2167 (answer nil)
2168 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002169 (goto-char start)
2170 (while searching
2171 ;; look for a colon with nothing after it except whitespace, and
2172 ;; maybe a comment
2173 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
2174 finish t)
2175 (if (eq (point) finish) ; note: no `else' clause; just
2176 ; keep searching if we're not at
2177 ; the end yet
2178 ;; sure looks like it opens a block -- but it might
2179 ;; be in a comment
2180 (progn
2181 (setq searching nil) ; search is done either way
2182 (setq state (parse-partial-sexp start
2183 (match-beginning 0)))
2184 (setq answer (not (nth 4 state)))))
2185 ;; search failed: couldn't find another interesting colon
2186 (setq searching nil)))
2187 answer)))
2188
Barry Warsawf831d811996-07-31 20:42:59 +00002189(defun py-statement-closes-block-p ()
2190 ;; true iff the current statement `closes' a block == the line
2191 ;; starts with `return', `raise', `break' or `continue'. doesn't
2192 ;; catch embedded statements
2193 (let ((here (point)))
2194 (back-to-indentation)
2195 (prog1
2196 (looking-at "\\(return\\|raise\\|break\\|continue\\)\\>")
2197 (goto-char here))))
2198
Barry Warsaw7ae77681994-12-12 20:38:05 +00002199;; go to point right beyond final line of block begun by the current
2200;; line. This is the same as where py-goto-beyond-final-line goes
2201;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002202;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00002203(defun py-goto-beyond-block ()
2204 (if (py-statement-opens-block-p)
2205 (py-mark-block nil 'just-move)
2206 (py-goto-beyond-final-line)))
2207
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002208;; go to start of first statement (not blank or comment or
2209;; continuation line) at or preceding point. returns t if there is
2210;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002211(defun py-goto-statement-at-or-above ()
2212 (py-goto-initial-line)
2213 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002214 ;; skip back over blank & comment lines
2215 ;; note: will skip a blank or comment line that happens to be
2216 ;; a continuation line too
2217 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
2218 (progn (py-goto-initial-line) t)
2219 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002220 t))
2221
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002222;; go to start of first statement (not blank or comment or
2223;; continuation line) following the statement containing point returns
2224;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002225(defun py-goto-statement-below ()
2226 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002227 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002228 (py-goto-beyond-final-line)
2229 (while (and
2230 (looking-at py-blank-or-comment-re)
2231 (not (eobp)))
2232 (forward-line 1))
2233 (if (eobp)
2234 (progn (goto-char start) nil)
2235 t)))
2236
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002237;; go to start of statement, at or preceding point, starting with
2238;; keyword KEY. Skips blank lines and non-indenting comments upward
2239;; first. If that statement starts with KEY, done, else go back to
2240;; first enclosing block starting with KEY. If successful, leaves
2241;; point at the start of the KEY line & returns t. Else leaves point
2242;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002243(defun py-go-up-tree-to-keyword (key)
2244 ;; skip blanks and non-indenting #
2245 (py-goto-initial-line)
2246 (while (and
2247 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2248 (zerop (forward-line -1))) ; go back
2249 nil)
2250 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002251 (let* ((re (concat "[ \t]*" key "\\b"))
2252 (case-fold-search nil) ; let* so looking-at sees this
2253 (found (looking-at re))
2254 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002255 (while (not (or found dead))
2256 (condition-case nil ; in case no enclosing block
2257 (py-goto-block-up 'no-mark)
2258 (error (setq dead t)))
2259 (or dead (setq found (looking-at re))))
2260 (beginning-of-line)
2261 found))
2262
2263;; return string in buffer from start of indentation to end of line;
2264;; prefix "..." if leading whitespace was skipped
2265(defun py-suck-up-leading-text ()
2266 (save-excursion
2267 (back-to-indentation)
2268 (concat
2269 (if (bolp) "" "...")
2270 (buffer-substring (point) (progn (end-of-line) (point))))))
2271
2272;; assuming point at bolp, return first keyword ([a-z]+) on the line,
2273;; as a Lisp symbol; return nil if none
2274(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002275 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002276 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
2277 (intern (buffer-substring (match-beginning 1) (match-end 1)))
2278 nil)))
2279
2280(defun py-make-temp-name ()
2281 (make-temp-name
2282 (concat (file-name-as-directory py-temp-directory) "python")))
2283
2284(defun py-delete-file-silently (fname)
2285 (condition-case nil
2286 (delete-file fname)
2287 (error nil)))
2288
2289(defun py-kill-emacs-hook ()
2290 ;; delete our temp files
2291 (while py-file-queue
2292 (py-delete-file-silently (car py-file-queue))
2293 (setq py-file-queue (cdr py-file-queue)))
2294 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
2295 ;; run the hook we inherited, if any
2296 (and py-inherited-kill-emacs-hook
2297 (funcall py-inherited-kill-emacs-hook))))
2298
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002299;; make PROCESS's buffer visible, append STRING to it, and force
2300;; display; also make shell-mode believe the user typed this string,
2301;; so that kill-output-from-shell and show-output-from-shell work
2302;; "right"
Barry Warsaw7ae77681994-12-12 20:38:05 +00002303(defun py-append-to-process-buffer (process string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002304 (let ((cbuf (current-buffer))
2305 (pbuf (process-buffer process))
2306 (py-scroll-process-buffer t))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002307 (set-buffer pbuf)
2308 (goto-char (point-max))
2309 (move-marker (process-mark process) (point))
Barry Warsaw4dba7e21995-07-05 23:01:43 +00002310 (if (not (or py-this-is-emacs-19-p
2311 py-this-is-lucid-emacs-p))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002312 (move-marker last-input-start (point))) ; muck w/ shell-mode
2313 (funcall (process-filter process) process string)
Barry Warsaw4dba7e21995-07-05 23:01:43 +00002314 (if (not (or py-this-is-emacs-19-p
2315 py-this-is-lucid-emacs-p))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002316 (move-marker last-input-end (point))) ; muck w/ shell-mode
2317 (set-buffer cbuf))
2318 (sit-for 0))
2319
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002320
2321
Barry Warsaw850437a1995-03-08 21:50:28 +00002322(defconst py-version "$Revision$"
2323 "`python-mode' version number.")
Barry Warsawfec75d61995-07-05 23:26:15 +00002324(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00002325 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002326
Barry Warsaw850437a1995-03-08 21:50:28 +00002327(defun py-version ()
2328 "Echo the current version of `python-mode' in the minibuffer."
2329 (interactive)
2330 (message "Using `python-mode' version %s" py-version)
2331 (py-keep-region-active))
2332
2333;; only works under Emacs 19
2334;(eval-when-compile
2335; (require 'reporter))
2336
2337(defun py-submit-bug-report (enhancement-p)
2338 "Submit via mail a bug report on `python-mode'.
2339With \\[universal-argument] just submit an enhancement request."
2340 (interactive
2341 (list (not (y-or-n-p
2342 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002343 (let ((reporter-prompt-for-summary-p (if enhancement-p
2344 "(Very) brief summary: "
2345 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00002346 (require 'reporter)
2347 (reporter-submit-bug-report
2348 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002349 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00002350 ;; varlist
2351 (if enhancement-p nil
2352 '(py-python-command
2353 py-indent-offset
2354 py-block-comment-prefix
2355 py-scroll-process-buffer
2356 py-temp-directory
2357 py-beep-if-tab-change))
2358 nil ;pre-hooks
2359 nil ;post-hooks
2360 "Dear Barry,") ;salutation
2361 (if enhancement-p nil
2362 (set-mark (point))
2363 (insert
2364"Please replace this text with a sufficiently large code sample\n\
2365and an exact recipe so that I can reproduce your problem. Failure\n\
2366to do so may mean a greater delay in fixing your bug.\n\n")
2367 (exchange-point-and-mark)
2368 (py-keep-region-active))))
2369
2370
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002371;; arrange to kill temp files when Emacs exists
2372(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
2373 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
2374 ;; have to trust that other people are as respectful of our hook
2375 ;; fiddling as we are of theirs
2376 (if (boundp 'py-inherited-kill-emacs-hook)
2377 ;; we were loaded before -- trust others not to have screwed us
2378 ;; in the meantime (no choice, really)
2379 nil
2380 ;; else arrange for our hook to run theirs
2381 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
2382 (setq kill-emacs-hook 'py-kill-emacs-hook)))
2383
2384
2385
2386(provide 'python-mode)
2387;;; python-mode.el ends here