blob: 2894b9baebf360b6b1047738b159fc9b051e4e67 [file] [log] [blame]
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
5;; Author: 1995 Barry A. Warsaw <bwarsaw@cnri.reston.va.us>
6;; 1992-1994 Tim Peters <tim@ksr.com>
7;; Maintainer: bwarsaw@cnri.reston.va.us
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00008;; Created: Feb 1992
Guido van Rossum1c1fbf81995-03-14 21:33:10 +00009;; Version: 2.12
10;; Last Modified: 1995/03/14 20:53:08
Guido van Rossumdeaa1051995-03-10 16:17:03 +000011;; Keywords: python editing language major-mode
12
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +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.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000018
19;;; Commentary:
Guido van Rossuma7925f11994-01-26 10:20:16 +000020;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +000021;; This is a major mode for editing Python programs. It was developed
22;; by Tim Peters <tim@ksr.com> after an original idea by Michael
23;; A. Guravage. Tim doesn't appear to be on the 'net any longer so I
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000024;; have undertaken maintenance of the mode.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000025
26;; At some point this mode will undergo a rewrite to bring it more in
27;; line with GNU Emacs Lisp coding standards. But all in all, the
28;; mode works exceedingly well.
29
30;; The following statements, placed in your .emacs file or
31;; site-init.el, will cause this file to be autoloaded, and
32;; python-mode invoked, when visiting .py files (assuming this file is
33;; in your load-path):
Guido van Rossuma7925f11994-01-26 10:20:16 +000034;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +000035;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Guido van Rossuma7925f11994-01-26 10:20:16 +000036;; (setq auto-mode-alist
37;; (cons '("\\.py$" . python-mode) auto-mode-alist))
38
Guido van Rossum1d5645d1995-03-14 21:31:47 +000039;; Here's a brief list of recent additions/improvements:
40;;
41;; - Wrapping and indentation within triple quote strings should work
42;; properly now.
43;; - `Standard' bug reporting mechanism (use C-c C-b)
44;; - py-mark-block was moved to C-c C-m
45;; - C-c C-v shows you the python-mode version
46;; - a basic python-font-lock-keywords has been added for Emacs 19
47;; font-lock colorizations.
48;; - proper interaction with pending-del and del-sel modes.
49;; - New py-electric-colon (:) command for improved outdenting. Also
50;; py-indent-line (TAB) should handle outdented lines better.
51
Guido van Rossumdeaa1051995-03-10 16:17:03 +000052;; Here's a brief to do list:
53;;
Guido van Rossum1d5645d1995-03-14 21:31:47 +000054;; - Better integration with gud-mode for debugging.
55;; - Rewrite according to GNU Emacs Lisp standards.
56;; - py-delete-char should obey numeric arguments.
57;; - even better support for outdenting. Guido suggests outdents of
58;; at least one level after a return, raise, break, or continue
59;; statement.
Guido van Rossuma7925f11994-01-26 10:20:16 +000060
Guido van Rossumdeaa1051995-03-10 16:17:03 +000061;; If you can think of more things you'd like to see, drop me a line.
62;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
63;;
64;; Note that I only test things on XEmacs (currently 19.11). If you
65;; port stuff to FSF Emacs 19, or Emacs 18, please send me your
66;; patches.
Guido van Rossuma7925f11994-01-26 10:20:16 +000067
Guido van Rossumdeaa1051995-03-10 16:17:03 +000068;; LCD Archive Entry:
69;; python-mode|Barry A. Warsaw|bwarsaw@cnri.reston.va.us
70;; |Major mode for editing Python programs
Guido van Rossum1c1fbf81995-03-14 21:33:10 +000071;; |1995/03/14 20:53:08|2.12|
Guido van Rossuma7925f11994-01-26 10:20:16 +000072
Guido van Rossumdeaa1051995-03-10 16:17:03 +000073;;; Code:
74
75
76;; user definable variables
77;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Guido van Rossuma7925f11994-01-26 10:20:16 +000078
79(defvar py-python-command "python"
80 "*Shell command used to start Python interpreter.")
81
82(defvar py-indent-offset 8 ; argue with Guido <grin>
83 "*Indentation increment.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000084Note that `\\[py-guess-indent-offset]' can usually guess a good value
85when you're editing someone else's Python code.")
Guido van Rossuma7925f11994-01-26 10:20:16 +000086
Guido van Rossuma7925f11994-01-26 10:20:16 +000087(defvar py-block-comment-prefix "##"
Guido van Rossumdeaa1051995-03-10 16:17:03 +000088 "*String used by `py-comment-region' to comment out a block of code.
Guido van Rossuma7925f11994-01-26 10:20:16 +000089This should follow the convention for non-indenting comment lines so
90that the indentation commands won't get confused (i.e., the string
91should be of the form `#x...' where `x' is not a blank or a tab, and
92`...' is arbitrary).")
93
94(defvar py-scroll-process-buffer t
95 "*Scroll Python process buffer as output arrives.
96If nil, the Python process buffer acts, with respect to scrolling, like
97Shell-mode buffers normally act. This is surprisingly complicated and
98so won't be explained here; in fact, you can't get the whole story
99without studying the Emacs C code.
100
101If non-nil, the behavior is different in two respects (which are
102slightly inaccurate in the interest of brevity):
103
104 - If the buffer is in a window, and you left point at its end, the
105 window will scroll as new output arrives, and point will move to the
106 buffer's end, even if the window is not the selected window (that
107 being the one the cursor is in). The usual behavior for shell-mode
108 windows is not to scroll, and to leave point where it was, if the
109 buffer is in a window other than the selected window.
110
111 - If the buffer is not visible in any window, and you left point at
112 its end, the buffer will be popped into a window as soon as more
113 output arrives. This is handy if you have a long-running
114 computation and don't want to tie up screen area waiting for the
115 output. The usual behavior for a shell-mode buffer is to stay
116 invisible until you explicitly visit it.
117
118Note the `and if you left point at its end' clauses in both of the
119above: you can `turn off' the special behaviors while output is in
120progress, by visiting the Python buffer and moving point to anywhere
121besides the end. Then the buffer won't scroll, point will remain where
122you leave it, and if you hide the buffer it will stay hidden until you
123visit it again. You can enable and disable the special behaviors as
124often as you like, while output is in progress, by (respectively) moving
125point to, or away from, the end of the buffer.
126
127Warning: If you expect a large amount of output, you'll probably be
128happier setting this option to nil.
129
130Obscure: `End of buffer' above should really say `at or beyond the
131process mark', but if you know what that means you didn't need to be
132told <grin>.")
133
134(defvar py-temp-directory
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000135 (let ((ok '(lambda (x)
136 (and x
137 (setq x (expand-file-name x)) ; always true
138 (file-directory-p x)
139 (file-writable-p x)
140 x))))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000141 (or (funcall ok (getenv "TMPDIR"))
142 (funcall ok "/usr/tmp")
143 (funcall ok "/tmp")
144 (funcall ok ".")
145 (error
146 "Couldn't find a usable temp directory -- set py-temp-directory")))
147 "*Directory used for temp files created by a *Python* process.
148By default, the first directory from this list that exists and that you
149can write into: the value (if any) of the environment variable TMPDIR,
150/usr/tmp, /tmp, or the current directory.")
151
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000152(defvar py-beep-if-tab-change t
153 "*Ring the bell if tab-width is changed.
154If a comment of the form
155
156 \t# vi:set tabsize=<number>:
157
158is found before the first code line when the file is entered, and the
159current value of (the general Emacs variable) `tab-width' does not
160equal <number>, `tab-width' is set to <number>, a message saying so is
161displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
162the Emacs bell is also rung as a warning.")
163
164(defvar python-font-lock-keywords
165 (list
166 (cons
167 (concat
168 "\\<\\("
169 (mapconcat
170 'identity
171 '("access" "and" "break" "continue"
172 "del" "elif" "else" "except"
173 "exec" "finally" "for" "from"
174 "global" "if" "import" "in"
175 "is" "lambda" "not" "or"
176 "pass" "print" "raise" "return"
177 "try" "while" "def" "class"
178 )
179 "\\|")
180 "\\)\\>")
181 1)
182 ;; functions
183 '("\\bdef\\s +\\(\\sw+\\)(" 1 font-lock-function-name-face)
184 ;; classes
185 '("\\bclass\\s +\\(\\sw+\\)[(:]" 1 font-lock-function-name-face)
186 )
187 "*Additional keywords to highlight `python-mode' buffers.")
188
189
190;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
192
193;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
194;; seems to be the standard way of checking this.
195;; BAW - This is *not* the right solution. When at all possible,
196;; instead of testing for the version of Emacs, use feature tests.
197
198(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
199(setq py-this-is-emacs-19-p
200 (and
201 (not py-this-is-lucid-emacs-p)
202 (string-match "^19\\." emacs-version)))
203
Guido van Rossuma7925f11994-01-26 10:20:16 +0000204;; have to bind py-file-queue before installing the kill-emacs hook
205(defvar py-file-queue nil
206 "Queue of Python temp files awaiting execution.
207Currently-active file is at the head of the list.")
208
209;; define a mode-specific abbrev table for those who use such things
210(defvar python-mode-abbrev-table nil
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000211 "Abbrev table in use in `python-mode' buffers.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000212(define-abbrev-table 'python-mode-abbrev-table nil)
213
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000214(defvar python-mode-hook nil
215 "*Hook called by `python-mode'.")
216
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000217;; in previous version of python-mode.el, the hook was incorrectly
218;; called py-mode-hook, and was not defvar'd. deprecate its use.
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000219(and (fboundp 'make-obsolete-variable)
220 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
221
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000222(defvar py-mode-map ()
223 "Keymap used in `python-mode' buffers.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000224
Guido van Rossuma7925f11994-01-26 10:20:16 +0000225(if py-mode-map
226 ()
227 (setq py-mode-map (make-sparse-keymap))
228
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000229 ;; shadow global bindings for newline-and-indent w/ the py- version.
230 ;; BAW - this is extremely bad form, but I'm not going to change it
231 ;; for now.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000232 (mapcar (function (lambda (key)
233 (define-key
234 py-mode-map key 'py-newline-and-indent)))
235 (where-is-internal 'newline-and-indent))
236
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000237 ;; BAW - you could do it this way, but its not considered proper
238 ;; major-mode form.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000239 (mapcar (function
240 (lambda (x)
241 (define-key py-mode-map (car x) (cdr x))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000242 '((":" . py-electric-colon)
243 ("\C-c\C-c" . py-execute-buffer)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000244 ("\C-c|" . py-execute-region)
245 ("\C-c!" . py-shell)
246 ("\177" . py-delete-char)
247 ("\n" . py-newline-and-indent)
248 ("\C-c:" . py-guess-indent-offset)
249 ("\C-c\t" . py-indent-region)
250 ("\C-c<" . py-shift-region-left)
251 ("\C-c>" . py-shift-region-right)
252 ("\C-c\C-n" . py-next-statement)
253 ("\C-c\C-p" . py-previous-statement)
254 ("\C-c\C-u" . py-goto-block-up)
255 ("\C-c\C-m" . py-mark-block)
256 ("\C-c#" . py-comment-region)
257 ("\C-c?" . py-describe-mode)
258 ("\C-c\C-hm" . py-describe-mode)
259 ("\e\C-a" . beginning-of-python-def-or-class)
260 ("\e\C-e" . end-of-python-def-or-class)
261 ( "\e\C-h" . mark-python-def-or-class)))
262 ;; should do all keybindings this way
263 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
264 (define-key py-mode-map "\C-c\C-v" 'py-version)
265 )
Guido van Rossuma7925f11994-01-26 10:20:16 +0000266
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000267(defvar py-mode-syntax-table nil
268 "Syntax table used in `python-mode' buffers.")
269
Guido van Rossuma7925f11994-01-26 10:20:16 +0000270(if py-mode-syntax-table
271 ()
272 (setq py-mode-syntax-table (make-syntax-table))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000273 ;; BAW - again, blech.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000274 (mapcar (function
275 (lambda (x) (modify-syntax-entry
276 (car x) (cdr x) py-mode-syntax-table)))
277 '(( ?\( . "()" ) ( ?\) . ")(" )
278 ( ?\[ . "(]" ) ( ?\] . ")[" )
279 ( ?\{ . "(}" ) ( ?\} . "){" )
280 ;; fix operator symbols misassigned in the std table
281 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
282 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
283 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
284 ( ?\> . "." ) ( ?\| . "." )
285 ( ?\_ . "w" ) ; underscore is legit in names
286 ( ?\' . "\"") ; single quote is string quote
287 ( ?\" . "\"" ) ; double quote is string quote too
288 ( ?\` . "$") ; backquote is open and close paren
289 ( ?\# . "<") ; hash starts comment
290 ( ?\n . ">")))) ; newline ends comment
291
Guido van Rossume531e4b1994-04-16 08:29:27 +0000292(defconst py-stringlit-re
293 (concat
294 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
295 "\\|" ; or
296 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000297 "Regexp matching a Python string literal.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000298
299;; this is tricky because a trailing backslash does not mean
300;; continuation if it's in a comment
301(defconst py-continued-re
302 (concat
Guido van Rossume531e4b1994-04-16 08:29:27 +0000303 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
Guido van Rossuma7925f11994-01-26 10:20:16 +0000304 "\\\\$")
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000305 "Regexp matching Python lines that are continued via backslash.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000306
307(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000308 "Regexp matching blank or comment lines.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000310(defconst py-outdent-re
311 (concat "\\(" (mapconcat 'identity
312 '("else:"
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000313 "except\\(\\s +.*\\)?:"
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000314 "finally:"
315 "elif\\s +.*:")
316 "\\|")
317 "\\)")
318 "Regexp matching clauses to be outdented one level.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000319
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000320(defconst py-no-outdent-re
321 (concat "\\(" (mapconcat 'identity
322 '("try\\s +.*:"
323 "except\\(\\s +.*\\)?:"
324 "while\\s +.*:"
325 "for\\s +.*:"
326 "if\\s +.*:"
327 "elif\\s +.*:")
328 "\\|")
329 "\\)")
330 "Regexp matching lines to not outdent after.")
331
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000332
333;;;###autoload
Guido van Rossuma7925f11994-01-26 10:20:16 +0000334(defun python-mode ()
335 "Major mode for editing Python files.
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000336To submit a problem report, enter `\\[py-submit-bug-report]' from a
337`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
338documentation. To see what version of `python-mode' you are running,
339enter `\\[py-version]'.
340
341This mode knows about Python indentation, tokens, comments and
342continuation lines. Paragraphs are separated by blank lines only.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000343
344COMMANDS
345\\{py-mode-map}
346VARIABLES
347
348py-indent-offset\tindentation increment
Guido van Rossuma7925f11994-01-26 10:20:16 +0000349py-block-comment-prefix\tcomment string used by py-comment-region
350py-python-command\tshell command to invoke Python interpreter
351py-scroll-process-buffer\talways scroll Python process buffer
352py-temp-directory\tdirectory used for temp files (if needed)
353py-beep-if-tab-change\tring the bell if tab-width is changed"
354 (interactive)
355 (kill-all-local-variables)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000356 (set-syntax-table py-mode-syntax-table)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000357 (setq major-mode 'python-mode
358 mode-name "Python"
359 local-abbrev-table python-mode-abbrev-table)
360 (use-local-map py-mode-map)
361 ;; BAW -- style...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000362 (mapcar (function (lambda (x)
363 (make-local-variable (car x))
364 (set (car x) (cdr x))))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000365 '((paragraph-separate . "^[ \t]*$")
366 (paragraph-start . "^[ \t]*$")
367 (require-final-newline . t)
368 (comment-start . "# ")
369 (comment-start-skip . "# *")
370 (comment-column . 40)
371 (indent-region-function . py-indent-region)
372 (indent-line-function . py-indent-line)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000373 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000374 ;;
375 ;; not sure where the magic comment has to be; to save time
376 ;; searching for a rarity, we give up if it's not found prior to the
377 ;; first executable statement.
378 ;;
379 ;; BAW - on first glance, this seems like complete hackery. Why was
380 ;; this necessary, and is it still necessary?
381 (let ((case-fold-search nil)
382 (start (point))
383 new-tab-width)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000384 (if (re-search-forward
385 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
386 (prog2 (py-next-statement 1) (point) (goto-char 1))
387 t)
388 (progn
389 (setq new-tab-width
390 (string-to-int
391 (buffer-substring (match-beginning 1) (match-end 1))))
392 (if (= tab-width new-tab-width)
393 nil
394 (setq tab-width new-tab-width)
395 (message "Caution: tab-width changed to %d" new-tab-width)
396 (if py-beep-if-tab-change (beep)))))
397 (goto-char start))
398
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000399 ;; run the mode hook. py-mode-hook use is deprecated
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000400 (if python-mode-hook
401 (run-hooks 'python-mode-hook)
402 (run-hooks 'py-mode-hook)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000403
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000404
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000405;; electric characters
406(defun py-electric-colon (arg)
407 "Insert a colon.
408In certain cases the line is outdented appropriately. If a numeric
409argument is provided, that many colons are inserted non-electrically."
410 (interactive "P")
411 (self-insert-command (prefix-numeric-value arg))
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000412 (save-excursion
413 (let ((here (point))
414 (outdent 0)
415 (indent (py-compute-indentation)))
416 (if (and (not arg)
417 (progn
418 (back-to-indentation)
419 (looking-at py-outdent-re))
420 (prog2
421 (backward-to-indentation 1)
422 (not (looking-at py-no-outdent-re))
423 (goto-char here))
424 (= indent (progn
425 (forward-line -1)
426 (py-compute-indentation)))
427 )
428 (setq outdent py-indent-offset))
429 (goto-char here)
430 (beginning-of-line)
431 (delete-horizontal-space)
432 (indent-to (- indent outdent))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000433 )))
434
435
Guido van Rossuma7925f11994-01-26 10:20:16 +0000436;;; Functions that execute Python commands in a subprocess
Guido van Rossuma7925f11994-01-26 10:20:16 +0000437(defun py-shell ()
438 "Start an interactive Python interpreter in another window.
439This is like Shell mode, except that Python is running in the window
440instead of a shell. See the `Interactive Shell' and `Shell Mode'
441sections of the Emacs manual for details, especially for the key
442bindings active in the `*Python*' buffer.
443
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000444See the docs for variable `py-scroll-buffer' for info on scrolling
Guido van Rossuma7925f11994-01-26 10:20:16 +0000445behavior in the process window.
446
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000447Warning: Don't use an interactive Python if you change sys.ps1 or
448sys.ps2 from their default values, or if you're running code that
449prints `>>> ' or `... ' at the start of a line. `python-mode' can't
450distinguish your output from Python's output, and assumes that `>>> '
451at the start of a line is a prompt from Python. Similarly, the Emacs
452Shell mode code assumes that both `>>> ' and `... ' at the start of a
453line are Python prompts. Bad things can happen if you fool either
454mode.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000455
456Warning: If you do any editing *in* the process buffer *while* the
457buffer is accepting output from Python, do NOT attempt to `undo' the
458changes. Some of the output (nowhere near the parts you changed!) may
459be lost if you do. This appears to be an Emacs bug, an unfortunate
460interaction between undo and process filters; the same problem exists in
461non-Python process buffers using the default (Emacs-supplied) process
462filter."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000463 ;; BAW - should undo be disabled in the python process buffer, if
464 ;; this bug still exists?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000465 (interactive)
466 (if py-this-is-emacs-19-p
467 (progn
468 (require 'comint)
469 (switch-to-buffer-other-window
470 (make-comint "Python" py-python-command)))
471 (progn
472 (require 'shell)
473 (switch-to-buffer-other-window
474 (make-shell "Python" py-python-command))))
475 (make-local-variable 'shell-prompt-pattern)
476 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
477 (set-process-filter (get-buffer-process (current-buffer))
478 'py-process-filter)
479 (set-syntax-table py-mode-syntax-table))
480
481(defun py-execute-region (start end)
482 "Send the region between START and END to a Python interpreter.
483If there is a *Python* process it is used.
484
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000485Hint: If you want to execute part of a Python file several times
486\(e.g., perhaps you're developing a function and want to flesh it out
487a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
488the region of interest, and send the code to a *Python* process via
489`\\[py-execute-buffer]' instead.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000490
491Following are subtleties to note when using a *Python* process:
492
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000493If a *Python* process is used, the region is copied into a temporary
494file (in directory `py-temp-directory'), and an `execfile' command is
495sent to Python naming that file. If you send regions faster than
496Python can execute them, `python-mode' will save them into distinct
497temp files, and execute the next one in the queue the next time it
498sees a `>>> ' prompt from Python. Each time this happens, the process
499buffer is popped into a window (if it's not already in some window) so
500you can see it, and a comment of the form
Guido van Rossuma7925f11994-01-26 10:20:16 +0000501
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000502 \t## working on region in file <name> ...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000503
504is inserted at the end.
505
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000506Caution: No more than 26 regions can be pending at any given time.
507This limit is (indirectly) inherited from libc's mktemp(3).
508`python-mode' does not try to protect you from exceeding the limit.
509It's extremely unlikely that you'll get anywhere close to the limit in
510practice, unless you're trying to be a jerk <grin>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000511
512See the `\\[py-shell]' docs for additional warnings."
513 (interactive "r")
514 (or (< start end) (error "Region is empty"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000515 (let ((pyproc (get-process "Python"))
516 fname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000517 (if (null pyproc)
518 (shell-command-on-region start end py-python-command)
519 ;; else feed it thru a temp file
520 (setq fname (py-make-temp-name))
521 (write-region start end fname nil 'no-msg)
522 (setq py-file-queue (append py-file-queue (list fname)))
523 (if (cdr py-file-queue)
524 (message "File %s queued for execution" fname)
525 ;; else
526 (py-execute-file pyproc fname)))))
527
528(defun py-execute-file (pyproc fname)
529 (py-append-to-process-buffer
530 pyproc
531 (format "## working on region in file %s ...\n" fname))
532 (process-send-string pyproc (format "execfile('%s')\n" fname)))
533
534(defun py-process-filter (pyproc string)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000535 (let ((curbuf (current-buffer))
536 (pbuf (process-buffer pyproc))
537 (pmark (process-mark pyproc))
538 file-finished)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000539
540 ;; make sure we switch to a different buffer at least once. if we
541 ;; *don't* do this, then if the process buffer is in the selected
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000542 ;; window, and point is before the end, and lots of output is
543 ;; coming at a fast pace, then (a) simple cursor-movement commands
544 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
545 ;; to have a visible effect (the window just doesn't get updated,
546 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
547 ;; get all the process output (until the next python prompt).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000548 ;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000549 ;; #b makes no sense to me at all. #a almost makes sense: unless
550 ;; we actually change buffers, set_buffer_internal in buffer.c
551 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
552 ;; seems to make the Emacs command loop reluctant to update the
553 ;; display. Perhaps the default process filter in process.c's
554 ;; read_process_output has update_mode_lines++ for a similar
555 ;; reason? beats me ...
556
557 ;; BAW - we want to check to see if this still applies
Guido van Rossuma7925f11994-01-26 10:20:16 +0000558 (if (eq curbuf pbuf) ; mysterious ugly hack
559 (set-buffer (get-buffer-create "*scratch*")))
560
561 (set-buffer pbuf)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000562 (let* ((start (point))
563 (goback (< start pmark))
564 (buffer-read-only nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000565 (goto-char pmark)
566 (insert string)
567 (move-marker pmark (point))
568 (setq file-finished
569 (and py-file-queue
570 (equal ">>> "
571 (buffer-substring
572 (prog2 (beginning-of-line) (point)
573 (goto-char pmark))
574 (point)))))
575 (if goback (goto-char start)
576 ;; else
577 (if py-scroll-process-buffer
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000578 (let* ((pop-up-windows t)
579 (pwin (display-buffer pbuf)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000580 (set-window-point pwin (point))))))
581 (set-buffer curbuf)
582 (if file-finished
583 (progn
584 (py-delete-file-silently (car py-file-queue))
585 (setq py-file-queue (cdr py-file-queue))
586 (if py-file-queue
587 (py-execute-file pyproc (car py-file-queue)))))))
588
589(defun py-execute-buffer ()
590 "Send the contents of the buffer to a Python interpreter.
591If there is a *Python* process buffer it is used. If a clipping
592restriction is in effect, only the accessible portion of the buffer is
593sent. A trailing newline will be supplied if needed.
594
595See the `\\[py-execute-region]' docs for an account of some subtleties."
596 (interactive)
597 (py-execute-region (point-min) (point-max)))
598
599
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000600
601;; Functions for Python style indentation
Guido van Rossuma7925f11994-01-26 10:20:16 +0000602(defun py-delete-char ()
603 "Reduce indentation or delete character.
604If point is at the leftmost column, deletes the preceding newline.
605
606Else if point is at the leftmost non-blank character of a line that is
607neither a continuation line nor a non-indenting comment line, or if
608point is at the end of a blank line, reduces the indentation to match
609that of the line that opened the current block of code. The line that
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000610opened the block is displayed in the echo area to help you keep track
611of where you are.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000612
613Else the preceding character is deleted, converting a tab to spaces if
614needed so that only a single column position is deleted."
615 (interactive "*")
616 (if (or (/= (current-indentation) (current-column))
617 (bolp)
618 (py-continuation-line-p)
619 (looking-at "#[^ \t\n]")) ; non-indenting #
620 (backward-delete-char-untabify 1)
621 ;; else indent the same as the colon line that opened the block
622
623 ;; force non-blank so py-goto-block-up doesn't ignore it
624 (insert-char ?* 1)
625 (backward-char)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000626 (let ((base-indent 0) ; indentation of base line
627 (base-text "") ; and text of base line
628 (base-found-p nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000629 (condition-case nil ; in case no enclosing block
630 (save-excursion
631 (py-goto-block-up 'no-mark)
632 (setq base-indent (current-indentation)
633 base-text (py-suck-up-leading-text)
634 base-found-p t))
635 (error nil))
636 (delete-char 1) ; toss the dummy character
637 (delete-horizontal-space)
638 (indent-to base-indent)
639 (if base-found-p
640 (message "Closes block: %s" base-text)))))
641
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000642;; required for pending-del and delsel modes
643(put 'py-delete-char 'delete-selection 'supersede)
644(put 'py-delete-char 'pending-delete 'supersede)
645
Guido van Rossuma7925f11994-01-26 10:20:16 +0000646(defun py-indent-line ()
647 "Fix the indentation of the current line according to Python rules."
648 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000649 (let* ((ci (current-indentation))
650 (move-to-indentation-p (<= (current-column) ci))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000651 (need (py-compute-indentation)))
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000652 ;; see if we need to outdent
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000653 (if (save-excursion
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000654 (and (progn (back-to-indentation)
655 (looking-at py-outdent-re))
656 (progn (backward-to-indentation 1)
657 (not (looking-at py-no-outdent-re)))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000658 (setq need (- need py-indent-offset)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000659 (if (/= ci need)
660 (save-excursion
661 (beginning-of-line)
662 (delete-horizontal-space)
663 (indent-to need)))
664 (if move-to-indentation-p (back-to-indentation))))
665
666(defun py-newline-and-indent ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000667 "Strives to act like the Emacs `newline-and-indent'.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000668This is just `strives to' because correct indentation can't be computed
669from scratch for Python code. In general, deletes the whitespace before
670point, inserts a newline, and takes an educated guess as to how you want
671the new line indented."
672 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000673 (let ((ci (current-indentation)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000674 (if (< ci (current-column)) ; if point beyond indentation
675 (newline-and-indent)
676 ;; else try to act like newline-and-indent "normally" acts
677 (beginning-of-line)
678 (insert-char ?\n 1)
679 (move-to-column ci))))
680
681(defun py-compute-indentation ()
682 (save-excursion
683 (beginning-of-line)
684 (cond
685 ;; are we on a continuation line?
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000686 ((py-continuation-line-p)
687 (let ((startpos (point))
688 (open-bracket-pos (py-nesting-level))
689 endpos searching found)
690 (if open-bracket-pos
691 (progn
692 ;; align with first item in list; else a normal
693 ;; indent beyond the line with the open bracket
694 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
695 ;; is the first list item on the same line?
696 (skip-chars-forward " \t")
697 (if (null (memq (following-char) '(?\n ?# ?\\)))
698 ; yes, so line up with it
699 (current-column)
700 ;; first list item on another line, or doesn't exist yet
701 (forward-line 1)
702 (while (and (< (point) startpos)
703 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
704 (forward-line 1))
705 (if (< (point) startpos)
706 ;; again mimic the first list item
707 (current-indentation)
708 ;; else they're about to enter the first item
709 (goto-char open-bracket-pos)
710 (+ (current-indentation) py-indent-offset))))
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +0000711
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000712 ;; else on backslash continuation line
713 (forward-line -1)
714 (if (py-continuation-line-p) ; on at least 3rd line in block
715 (current-indentation) ; so just continue the pattern
716 ;; else started on 2nd line in block, so indent more.
717 ;; if base line is an assignment with a start on a RHS,
718 ;; indent to 2 beyond the leftmost "="; else skip first
719 ;; chunk of non-whitespace characters on base line, + 1 more
720 ;; column
721 (end-of-line)
722 (setq endpos (point) searching t)
723 (back-to-indentation)
724 (setq startpos (point))
725 ;; look at all "=" from left to right, stopping at first
726 ;; one not nested in a list or string
727 (while searching
728 (skip-chars-forward "^=" endpos)
729 (if (= (point) endpos)
730 (setq searching nil)
731 (forward-char 1)
732 (setq state (parse-partial-sexp startpos (point)))
733 (if (and (zerop (car state)) ; not in a bracket
734 (null (nth 3 state))) ; & not in a string
735 (progn
736 (setq searching nil) ; done searching in any case
737 (setq found
738 (not (or
739 (eq (following-char) ?=)
740 (memq (char-after (- (point) 2))
741 '(?< ?> ?!)))))))))
742 (if (or (not found) ; not an assignment
743 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
744 (progn
745 (goto-char startpos)
746 (skip-chars-forward "^ \t\n")))
747 (1+ (current-column))))))
Guido van Rossume531e4b1994-04-16 08:29:27 +0000748
Guido van Rossuma7925f11994-01-26 10:20:16 +0000749 ;; not on a continuation line
750
751 ;; if at start of restriction, or on a non-indenting comment line,
752 ;; assume they intended whatever's there
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000753 ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
754 (current-indentation))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000755
756 ;; else indentation based on that of the statement that precedes
757 ;; us; use the first line of that statement to establish the base,
758 ;; in case the user forced a non-std indentation for the
759 ;; continuation lines (if any)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000760 (t
761 ;; skip back over blank & non-indenting comment lines
762 ;; note: will skip a blank or non-indenting comment line that
763 ;; happens to be a continuation line too
764 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
765 nil 'move)
766 ;; if we landed inside a string, go to the beginning of that
767 ;; string. this handles triple quoted, multi-line spanning
768 ;; strings.
769 (let ((state (parse-partial-sexp
770 (save-excursion (beginning-of-python-def-or-class)
771 (point))
772 (point))))
773 (if (nth 3 state)
774 (goto-char (nth 2 state))))
775 (py-goto-initial-line)
776 (if (py-statement-opens-block-p)
777 (+ (current-indentation) py-indent-offset)
778 (current-indentation))))))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000779
780(defun py-guess-indent-offset (&optional global)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000781 "Guess a good value for, and change, `py-indent-offset'.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000782By default (without a prefix arg), makes a buffer-local copy of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000783`py-indent-offset' with the new value. This will not affect any other
Guido van Rossuma7925f11994-01-26 10:20:16 +0000784Python buffers. With a prefix arg, changes the global value of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000785`py-indent-offset'. This affects all Python buffers (that don't have
Guido van Rossuma7925f11994-01-26 10:20:16 +0000786their own buffer-local copy), both those currently existing and those
787created later in the Emacs session.
788
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000789Some people use a different value for `py-indent-offset' than you use.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000790There's no excuse for such foolishness, but sometimes you have to deal
791with their ugly code anyway. This function examines the file and sets
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000792`py-indent-offset' to what it thinks it was when they created the
793mess.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000794
795Specifically, it searches forward from the statement containing point,
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000796looking for a line that opens a block of code. `py-indent-offset' is
797set to the difference in indentation between that line and the Python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000798statement following it. If the search doesn't succeed going forward,
799it's tried again going backward."
800 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000801 (let (new-value
802 (start (point))
803 restart
804 (found nil)
805 colon-indent)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000806 (py-goto-initial-line)
807 (while (not (or found (eobp)))
808 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
809 (progn
810 (setq restart (point))
811 (py-goto-initial-line)
812 (if (py-statement-opens-block-p)
813 (setq found t)
814 (goto-char restart)))))
815 (if found
816 ()
817 (goto-char start)
818 (py-goto-initial-line)
819 (while (not (or found (bobp)))
820 (setq found
821 (and
822 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
823 (or (py-goto-initial-line) t) ; always true -- side effect
824 (py-statement-opens-block-p)))))
825 (setq colon-indent (current-indentation)
826 found (and found (zerop (py-next-statement 1)))
827 new-value (- (current-indentation) colon-indent))
828 (goto-char start)
829 (if found
830 (progn
831 (funcall (if global 'kill-local-variable 'make-local-variable)
832 'py-indent-offset)
833 (setq py-indent-offset new-value)
834 (message "%s value of py-indent-offset set to %d"
835 (if global "Global" "Local")
836 py-indent-offset))
837 (error "Sorry, couldn't guess a value for py-indent-offset"))))
838
839(defun py-shift-region (start end count)
840 (save-excursion
841 (goto-char end) (beginning-of-line) (setq end (point))
842 (goto-char start) (beginning-of-line) (setq start (point))
843 (indent-rigidly start end count)))
844
845(defun py-shift-region-left (start end &optional count)
846 "Shift region of Python code to the left.
847The lines from the line containing the start of the current region up
848to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000849shifted to the left, by `py-indent-offset' columns.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000850
851If a prefix argument is given, the region is instead shifted by that
852many columns."
853 (interactive "*r\nP") ; region; raw prefix arg
854 (py-shift-region start end
855 (- (prefix-numeric-value
856 (or count py-indent-offset)))))
857
858(defun py-shift-region-right (start end &optional count)
859 "Shift region of Python code to the right.
860The lines from the line containing the start of the current region up
861to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000862shifted to the right, by `py-indent-offset' columns.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000863
864If a prefix argument is given, the region is instead shifted by that
865many columns."
866 (interactive "*r\nP") ; region; raw prefix arg
867 (py-shift-region start end (prefix-numeric-value
868 (or count py-indent-offset))))
869
870(defun py-indent-region (start end &optional indent-offset)
871 "Reindent a region of Python code.
872The lines from the line containing the start of the current region up
873to (but not including) the line containing the end of the region are
874reindented. If the first line of the region has a non-whitespace
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000875character in the first column, the first line is left alone and the
876rest of the region is reindented with respect to it. Else the entire
877region is reindented with respect to the (closest code or
878indenting-comment) statement immediately preceding the region.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000879
880This is useful when code blocks are moved or yanked, when enclosing
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000881control structures are introduced or removed, or to reformat code
882using a new value for the indentation offset.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000883
884If a numeric prefix argument is given, it will be used as the value of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000885the indentation offset. Else the value of `py-indent-offset' will be
Guido van Rossuma7925f11994-01-26 10:20:16 +0000886used.
887
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000888Warning: The region must be consistently indented before this function
Guido van Rossuma7925f11994-01-26 10:20:16 +0000889is called! This function does not compute proper indentation from
890scratch (that's impossible in Python), it merely adjusts the existing
891indentation to be correct in context.
892
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000893Warning: This function really has no idea what to do with
894non-indenting comment lines, and shifts them as if they were indenting
895comment lines. Fixing this appears to require telepathy.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000896
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000897Special cases: whitespace is deleted from blank lines; continuation
898lines are shifted by the same amount their initial line was shifted,
899in order to preserve their relative indentation with respect to their
Guido van Rossuma7925f11994-01-26 10:20:16 +0000900initial line; and comment lines beginning in column 1 are ignored."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000901 (interactive "*r\nP") ; region; raw prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000902 (save-excursion
903 (goto-char end) (beginning-of-line) (setq end (point-marker))
904 (goto-char start) (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000905 (let ((py-indent-offset (prefix-numeric-value
906 (or indent-offset py-indent-offset)))
907 (indents '(-1)) ; stack of active indent levels
908 (target-column 0) ; column to which to indent
909 (base-shifted-by 0) ; amount last base line was shifted
910 (indent-base (if (looking-at "[ \t\n]")
911 (py-compute-indentation)
912 0))
913 ci)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000914 (while (< (point) end)
915 (setq ci (current-indentation))
916 ;; figure out appropriate target column
917 (cond
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000918 ((or (eq (following-char) ?#) ; comment in column 1
919 (looking-at "[ \t]*$")) ; entirely blank
920 (setq target-column 0))
921 ((py-continuation-line-p) ; shift relative to base line
922 (setq target-column (+ ci base-shifted-by)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000923 (t ; new base line
924 (if (> ci (car indents)) ; going deeper; push it
925 (setq indents (cons ci indents))
926 ;; else we should have seen this indent before
927 (setq indents (memq ci indents)) ; pop deeper indents
928 (if (null indents)
929 (error "Bad indentation in region, at line %d"
930 (save-restriction
931 (widen)
932 (1+ (count-lines 1 (point)))))))
933 (setq target-column (+ indent-base
934 (* py-indent-offset
935 (- (length indents) 2))))
936 (setq base-shifted-by (- target-column ci))))
937 ;; shift as needed
938 (if (/= ci target-column)
939 (progn
940 (delete-horizontal-space)
941 (indent-to target-column)))
942 (forward-line 1))))
943 (set-marker end nil))
944
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000945
946;; Functions for moving point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000947(defun py-previous-statement (count)
948 "Go to the start of previous Python statement.
949If the statement at point is the i'th Python statement, goes to the
950start of statement i-COUNT. If there is no such statement, goes to the
951first statement. Returns count of statements left to move.
952`Statements' do not include blank, comment, or continuation lines."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000953 (interactive "p") ; numeric prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000954 (if (< count 0) (py-next-statement (- count))
955 (py-goto-initial-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000956 (let (start)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000957 (while (and
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000958 (setq start (point)) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +0000959 (> count 0)
960 (zerop (forward-line -1))
961 (py-goto-statement-at-or-above))
962 (setq count (1- count)))
963 (if (> count 0) (goto-char start)))
964 count))
965
966(defun py-next-statement (count)
967 "Go to the start of next Python statement.
968If the statement at point is the i'th Python statement, goes to the
969start of statement i+COUNT. If there is no such statement, goes to the
970last statement. Returns count of statements left to move. `Statements'
971do not include blank, comment, or continuation lines."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000972 (interactive "p") ; numeric prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000973 (if (< count 0) (py-previous-statement (- count))
974 (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000975 (let (start)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000976 (while (and
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000977 (setq start (point)) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +0000978 (> count 0)
979 (py-goto-statement-below))
980 (setq count (1- count)))
981 (if (> count 0) (goto-char start)))
982 count))
983
984(defun py-goto-block-up (&optional nomark)
985 "Move up to start of current block.
986Go to the statement that starts the smallest enclosing block; roughly
987speaking, this will be the closest preceding statement that ends with a
988colon and is indented less than the statement you started on. If
989successful, also sets the mark to the starting point.
990
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000991`\\[py-mark-block]' can be used afterward to mark the whole code
992block, if desired.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000993
994If called from a program, the mark will not be set if optional argument
995NOMARK is not nil."
996 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000997 (let ((start (point))
998 (found nil)
999 initial-indent)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001000 (py-goto-initial-line)
1001 ;; if on blank or non-indenting comment line, use the preceding stmt
1002 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1003 (progn
1004 (py-goto-statement-at-or-above)
1005 (setq found (py-statement-opens-block-p))))
1006 ;; search back for colon line indented less
1007 (setq initial-indent (current-indentation))
1008 (if (zerop initial-indent)
1009 ;; force fast exit
1010 (goto-char (point-min)))
1011 (while (not (or found (bobp)))
1012 (setq found
1013 (and
1014 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1015 (or (py-goto-initial-line) t) ; always true -- side effect
1016 (< (current-indentation) initial-indent)
1017 (py-statement-opens-block-p))))
1018 (if found
1019 (progn
1020 (or nomark (push-mark start))
1021 (back-to-indentation))
1022 (goto-char start)
1023 (error "Enclosing block not found"))))
1024
1025(defun beginning-of-python-def-or-class (&optional class)
1026 "Move point to start of def (or class, with prefix arg).
1027
1028Searches back for the closest preceding `def'. If you supply a prefix
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001029arg, looks for a `class' instead. The docs assume the `def' case;
1030just substitute `class' for `def' for the other case.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001031
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001032If point is in a def statement already, and after the `d', simply
1033moves point to the start of the statement.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001034
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001035Else (point is not in a def statement, or at or before the `d' of a
1036def statement), searches for the closest preceding def statement, and
1037leaves point at its start. If no such statement can be found, leaves
1038point at the start of the buffer.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001039
1040Returns t iff a def statement is found by these rules.
1041
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001042Note that doing this command repeatedly will take you closer to the
1043start of the buffer each time.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001044
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001045If you want to mark the current def/class, see
1046`\\[mark-python-def-or-class]'."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001047 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001048 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1049 (start-of-line (progn (beginning-of-line) (point)))
1050 (start-of-stmt (progn (py-goto-initial-line) (point))))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001051 (if (or (/= start-of-stmt start-of-line)
1052 (not at-or-before-p))
1053 (end-of-line)) ; OK to match on this line
1054 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001055 nil 'move)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001056
1057(defun end-of-python-def-or-class (&optional class)
1058 "Move point beyond end of def (or class, with prefix arg) body.
1059
1060By default, looks for an appropriate `def'. If you supply a prefix arg,
1061looks for a `class' instead. The docs assume the `def' case; just
1062substitute `class' for `def' for the other case.
1063
1064If point is in a def statement already, this is the def we use.
1065
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001066Else if the def found by `\\[beginning-of-python-def-or-class]'
1067contains the statement you started on, that's the def we use.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001068
1069Else we search forward for the closest following def, and use that.
1070
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001071If a def can be found by these rules, point is moved to the start of
1072the line immediately following the def block, and the position of the
1073start of the def is returned.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001074
1075Else point is moved to the end of the buffer, and nil is returned.
1076
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001077Note that doing this command repeatedly will take you closer to the
1078end of the buffer each time.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001079
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001080If you want to mark the current def/class, see
1081`\\[mark-python-def-or-class]'."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001082 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001083 (let ((start (progn (py-goto-initial-line) (point)))
1084 (which (if class "class" "def"))
1085 (state 'not-found))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001086 ;; move point to start of appropriate def/class
1087 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1088 (setq state 'at-beginning)
1089 ;; else see if beginning-of-python-def-or-class hits container
1090 (if (and (beginning-of-python-def-or-class class)
1091 (progn (py-goto-beyond-block)
1092 (> (point) start)))
1093 (setq state 'at-end)
1094 ;; else search forward
1095 (goto-char start)
1096 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1097 (progn (setq state 'at-beginning)
1098 (beginning-of-line)))))
1099 (cond
1100 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1101 ((eq state 'at-end) t)
1102 ((eq state 'not-found) nil)
1103 (t (error "internal error in end-of-python-def-or-class")))))
1104
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001105
1106;; Functions for marking regions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001107(defun py-mark-block (&optional extend just-move)
1108 "Mark following block of lines. With prefix arg, mark structure.
1109Easier to use than explain. It sets the region to an `interesting'
1110block of succeeding lines. If point is on a blank line, it goes down to
1111the next non-blank line. That will be the start of the region. The end
1112of the region depends on the kind of line at the start:
1113
1114 - If a comment, the region will include all succeeding comment lines up
1115 to (but not including) the next non-comment line (if any).
1116
1117 - Else if a prefix arg is given, and the line begins one of these
1118 structures:
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001119
1120 if elif else try except finally for while def class
1121
Guido van Rossuma7925f11994-01-26 10:20:16 +00001122 the region will be set to the body of the structure, including
1123 following blocks that `belong' to it, but excluding trailing blank
1124 and comment lines. E.g., if on a `try' statement, the `try' block
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001125 and all (if any) of the following `except' and `finally' blocks
1126 that belong to the `try' structure will be in the region. Ditto
1127 for if/elif/else, for/else and while/else structures, and (a bit
1128 degenerate, since they're always one-block structures) def and
1129 class blocks.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001130
1131 - Else if no prefix argument is given, and the line begins a Python
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001132 block (see list above), and the block is not a `one-liner' (i.e.,
1133 the statement ends with a colon, not with code), the region will
1134 include all succeeding lines up to (but not including) the next
1135 code statement (if any) that's indented no more than the starting
1136 line, except that trailing blank and comment lines are excluded.
1137 E.g., if the starting line begins a multi-statement `def'
1138 structure, the region will be set to the full function definition,
1139 but without any trailing `noise' lines.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001140
1141 - Else the region will include all succeeding lines up to (but not
1142 including) the next blank line, or code or indenting-comment line
1143 indented strictly less than the starting line. Trailing indenting
1144 comment lines are included in this case, but not trailing blank
1145 lines.
1146
1147A msg identifying the location of the mark is displayed in the echo
1148area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1149
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001150If called from a program, optional argument EXTEND plays the role of
1151the prefix arg, and if optional argument JUST-MOVE is not nil, just
1152moves to the end of the block (& does not set mark or display a msg)."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001153 (interactive "P") ; raw prefix arg
1154 (py-goto-initial-line)
1155 ;; skip over blank lines
1156 (while (and
1157 (looking-at "[ \t]*$") ; while blank line
1158 (not (eobp))) ; & somewhere to go
1159 (forward-line 1))
1160 (if (eobp)
1161 (error "Hit end of buffer without finding a non-blank stmt"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001162 (let ((initial-pos (point))
1163 (initial-indent (current-indentation))
1164 last-pos ; position of last stmt in region
1165 (followers
1166 '((if elif else) (elif elif else) (else)
1167 (try except finally) (except except) (finally)
1168 (for else) (while else)
1169 (def) (class) ) )
1170 first-symbol next-symbol)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001171
1172 (cond
1173 ;; if comment line, suck up the following comment lines
1174 ((looking-at "[ \t]*#")
1175 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1176 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1177 (setq last-pos (point)))
1178
1179 ;; else if line is a block line and EXTEND given, suck up
1180 ;; the whole structure
1181 ((and extend
1182 (setq first-symbol (py-suck-up-first-keyword) )
1183 (assq first-symbol followers))
1184 (while (and
1185 (or (py-goto-beyond-block) t) ; side effect
1186 (forward-line -1) ; side effect
1187 (setq last-pos (point)) ; side effect
1188 (py-goto-statement-below)
1189 (= (current-indentation) initial-indent)
1190 (setq next-symbol (py-suck-up-first-keyword))
1191 (memq next-symbol (cdr (assq first-symbol followers))))
1192 (setq first-symbol next-symbol)))
1193
1194 ;; else if line *opens* a block, search for next stmt indented <=
1195 ((py-statement-opens-block-p)
1196 (while (and
1197 (setq last-pos (point)) ; always true -- side effect
1198 (py-goto-statement-below)
1199 (> (current-indentation) initial-indent))
1200 nil))
1201
1202 ;; else plain code line; stop at next blank line, or stmt or
1203 ;; indenting comment line indented <
1204 (t
1205 (while (and
1206 (setq last-pos (point)) ; always true -- side effect
1207 (or (py-goto-beyond-final-line) t)
1208 (not (looking-at "[ \t]*$")) ; stop at blank line
1209 (or
1210 (>= (current-indentation) initial-indent)
1211 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1212 nil)))
1213
1214 ;; skip to end of last stmt
1215 (goto-char last-pos)
1216 (py-goto-beyond-final-line)
1217
1218 ;; set mark & display
1219 (if just-move
1220 () ; just return
1221 (push-mark (point) 'no-msg)
1222 (forward-line -1)
1223 (message "Mark set after: %s" (py-suck-up-leading-text))
1224 (goto-char initial-pos))))
1225
1226(defun mark-python-def-or-class (&optional class)
1227 "Set region to body of def (or class, with prefix arg) enclosing point.
1228Pushes the current mark, then point, on the mark ring (all language
1229modes do this, but although it's handy it's never documented ...).
1230
1231In most Emacs language modes, this function bears at least a
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001232hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1233`\\[beginning-of-python-def-or-class]'.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001234
1235And in earlier versions of Python mode, all 3 were tightly connected.
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001236Turned out that was more confusing than useful: the `goto start' and
1237`goto end' commands are usually used to search through a file, and
1238people expect them to act a lot like `search backward' and `search
1239forward' string-search commands. But because Python `def' and `class'
1240can nest to arbitrary levels, finding the smallest def containing
1241point cannot be done via a simple backward search: the def containing
1242point may not be the closest preceding def, or even the closest
1243preceding def that's indented less. The fancy algorithm required is
1244appropriate for the usual uses of this `mark' command, but not for the
1245`goto' variations.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001246
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001247So the def marked by this command may not be the one either of the
1248`goto' commands find: If point is on a blank or non-indenting comment
1249line, moves back to start of the closest preceding code statement or
1250indenting comment line. If this is a `def' statement, that's the def
1251we use. Else searches for the smallest enclosing `def' block and uses
1252that. Else signals an error.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001253
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001254When an enclosing def is found: The mark is left immediately beyond
1255the last line of the def block. Point is left at the start of the
1256def, except that: if the def is preceded by a number of comment lines
1257followed by (at most) one optional blank line, point is left at the
1258start of the comments; else if the def is preceded by a blank line,
1259point is left at its start.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001260
1261The intent is to mark the containing def/class and its associated
1262documentation, to make moving and duplicating functions and classes
1263pleasant."
1264 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001265 (let ((start (point))
1266 (which (if class "class" "def")))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001267 (push-mark start)
1268 (if (not (py-go-up-tree-to-keyword which))
1269 (progn (goto-char start)
1270 (error "Enclosing %s not found" which))
1271 ;; else enclosing def/class found
1272 (setq start (point))
1273 (py-goto-beyond-block)
1274 (push-mark (point))
1275 (goto-char start)
1276 (if (zerop (forward-line -1)) ; if there is a preceding line
1277 (progn
1278 (if (looking-at "[ \t]*$") ; it's blank
1279 (setq start (point)) ; so reset start point
1280 (goto-char start)) ; else try again
1281 (if (zerop (forward-line -1))
1282 (if (looking-at "[ \t]*#") ; a comment
1283 ;; look back for non-comment line
1284 ;; tricky: note that the regexp matches a blank
1285 ;; line, cuz \n is in the 2nd character class
1286 (and
1287 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1288 (forward-line 1))
1289 ;; no comment, so go back
1290 (goto-char start))))))))
1291
1292(defun py-comment-region (start end &optional uncomment-p)
1293 "Comment out region of code; with prefix arg, uncomment region.
1294The lines from the line containing the start of the current region up
1295to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001296commented out, by inserting the string `py-block-comment-prefix' at
1297the start of each line. With a prefix arg, removes
1298`py-block-comment-prefix' from the start of each line instead."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001299 (interactive "*r\nP") ; region; raw prefix arg
1300 (goto-char end) (beginning-of-line) (setq end (point))
1301 (goto-char start) (beginning-of-line) (setq start (point))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001302 (let ((prefix-len (length py-block-comment-prefix)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001303 (save-excursion
1304 (save-restriction
1305 (narrow-to-region start end)
1306 (while (not (eobp))
1307 (if uncomment-p
1308 (and (string= py-block-comment-prefix
1309 (buffer-substring
1310 (point) (+ (point) prefix-len)))
1311 (delete-char prefix-len))
1312 (insert py-block-comment-prefix))
1313 (forward-line 1))))))
1314
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001315
1316;; Documentation functions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001317
1318;; dump the long form of the mode blurb; does the usual doc escapes,
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001319;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1320;; out of the right places, along with the keys they're on & current
1321;; values
Guido van Rossuma7925f11994-01-26 10:20:16 +00001322(defun py-dump-help-string (str)
1323 (with-output-to-temp-buffer "*Help*"
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001324 (let ((locals (buffer-local-variables))
1325 funckind funcname func funcdoc
1326 (start 0) mstart end
1327 keys )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001328 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1329 (setq mstart (match-beginning 0) end (match-end 0)
1330 funckind (substring str (match-beginning 1) (match-end 1))
1331 funcname (substring str (match-beginning 2) (match-end 2))
1332 func (intern funcname))
1333 (princ (substitute-command-keys (substring str start mstart)))
1334 (cond
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001335 ((equal funckind "c") ; command
1336 (setq funcdoc (documentation func)
1337 keys (concat
1338 "Key(s): "
1339 (mapconcat 'key-description
1340 (where-is-internal func py-mode-map)
1341 ", "))))
1342 ((equal funckind "v") ; variable
1343 (setq funcdoc (substitute-command-keys
1344 (get func 'variable-documentation))
1345 keys (if (assq func locals)
1346 (concat
1347 "Local/Global values: "
1348 (prin1-to-string (symbol-value func))
1349 " / "
1350 (prin1-to-string (default-value func)))
1351 (concat
1352 "Value: "
1353 (prin1-to-string (symbol-value func))))))
1354 (t ; unexpected
1355 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001356 (princ (format "\n-> %s:\t%s\t%s\n\n"
1357 (if (equal funckind "c") "Command" "Variable")
1358 funcname keys))
1359 (princ funcdoc)
1360 (terpri)
1361 (setq start end))
1362 (princ (substitute-command-keys (substring str start))))
1363 (print-help-return-message)))
1364
1365(defun py-describe-mode ()
1366 "Dump long form of Python-mode docs."
1367 (interactive)
1368 (py-dump-help-string "Major mode for editing Python files.
1369Knows about Python indentation, tokens, comments and continuation lines.
1370Paragraphs are separated by blank lines only.
1371
1372Major sections below begin with the string `@'; specific function and
1373variable docs begin with `->'.
1374
1375@EXECUTING PYTHON CODE
1376
1377\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1378\\[py-execute-region]\tsends the current region
1379\\[py-shell]\tstarts a Python interpreter window; this will be used by
1380\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1381%c:py-execute-buffer
1382%c:py-execute-region
1383%c:py-shell
1384
1385@VARIABLES
1386
1387py-indent-offset\tindentation increment
Guido van Rossuma7925f11994-01-26 10:20:16 +00001388py-block-comment-prefix\tcomment string used by py-comment-region
1389
1390py-python-command\tshell command to invoke Python interpreter
1391py-scroll-process-buffer\talways scroll Python process buffer
1392py-temp-directory\tdirectory used for temp files (if needed)
1393
1394py-beep-if-tab-change\tring the bell if tab-width is changed
1395%v:py-indent-offset
Guido van Rossuma7925f11994-01-26 10:20:16 +00001396%v:py-block-comment-prefix
1397%v:py-python-command
1398%v:py-scroll-process-buffer
1399%v:py-temp-directory
1400%v:py-beep-if-tab-change
1401
1402@KINDS OF LINES
1403
1404Each physical line in the file is either a `continuation line' (the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001405preceding line ends with a backslash that's not part of a comment, or
1406the paren/bracket/brace nesting level at the start of the line is
1407non-zero, or both) or an `initial line' (everything else).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001408
1409An initial line is in turn a `blank line' (contains nothing except
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001410possibly blanks or tabs), a `comment line' (leftmost non-blank
1411character is `#'), or a `code line' (everything else).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001412
1413Comment Lines
1414
1415Although all comment lines are treated alike by Python, Python mode
1416recognizes two kinds that act differently with respect to indentation.
1417
1418An `indenting comment line' is a comment line with a blank, tab or
1419nothing after the initial `#'. The indentation commands (see below)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001420treat these exactly as if they were code lines: a line following an
Guido van Rossuma7925f11994-01-26 10:20:16 +00001421indenting comment line will be indented like the comment line. All
1422other comment lines (those with a non-whitespace character immediately
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001423following the initial `#') are `non-indenting comment lines', and
1424their indentation is ignored by the indentation commands.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001425
1426Indenting comment lines are by far the usual case, and should be used
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001427whenever possible. Non-indenting comment lines are useful in cases
1428like these:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001429
1430\ta = b # a very wordy single-line comment that ends up being
1431\t #... continued onto another line
1432
1433\tif a == b:
1434##\t\tprint 'panic!' # old code we've `commented out'
1435\t\treturn a
1436
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001437Since the `#...' and `##' comment lines have a non-whitespace
1438character following the initial `#', Python mode ignores them when
1439computing the proper indentation for the next line.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001440
1441Continuation Lines and Statements
1442
1443The Python-mode commands generally work on statements instead of on
1444individual lines, where a `statement' is a comment or blank line, or a
1445code line and all of its following continuation lines (if any)
1446considered as a single logical unit. The commands in this mode
1447generally (when it makes sense) automatically move to the start of the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001448statement containing point, even if point happens to be in the middle
1449of some continuation line.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001450
Guido van Rossuma7925f11994-01-26 10:20:16 +00001451
1452@INDENTATION
1453
1454Primarily for entering new code:
1455\t\\[indent-for-tab-command]\t indent line appropriately
1456\t\\[py-newline-and-indent]\t insert newline, then indent
1457\t\\[py-delete-char]\t reduce indentation, or delete single character
1458
1459Primarily for reindenting existing code:
1460\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1461\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1462
1463\t\\[py-indent-region]\t reindent region to match its context
1464\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1465\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1466
1467Unlike most programming languages, Python uses indentation, and only
1468indentation, to specify block structure. Hence the indentation supplied
1469automatically by Python-mode is just an educated guess: only you know
1470the block structure you intend, so only you can supply correct
1471indentation.
1472
1473The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1474the indentation of preceding statements. E.g., assuming
1475py-indent-offset is 4, after you enter
1476\tif a > 0: \\[py-newline-and-indent]
1477the cursor will be moved to the position of the `_' (_ is not a
1478character in the file, it's just used here to indicate the location of
1479the cursor):
1480\tif a > 0:
1481\t _
1482If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1483to
1484\tif a > 0:
1485\t c = d
1486\t _
1487Python-mode cannot know whether that's what you intended, or whether
1488\tif a > 0:
1489\t c = d
1490\t_
1491was your intent. In general, Python-mode either reproduces the
1492indentation of the (closest code or indenting-comment) preceding
1493statement, or adds an extra py-indent-offset blanks if the preceding
1494statement has `:' as its last significant (non-whitespace and non-
1495comment) character. If the suggested indentation is too much, use
1496\\[py-delete-char] to reduce it.
1497
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +00001498Continuation lines are given extra indentation. If you don't like the
1499suggested indentation, change it to something you do like, and Python-
1500mode will strive to indent later lines of the statement in the same way.
1501
1502If a line is a continuation line by virtue of being in an unclosed
1503paren/bracket/brace structure (`list', for short), the suggested
Guido van Rossum9274e2d1994-04-26 07:35:17 +00001504indentation depends on whether the current line contains the first item
1505in the list. If it does, it's indented py-indent-offset columns beyond
1506the indentation of the line containing the open bracket. If you don't
1507like that, change it by hand. The remaining items in the list will mimic
1508whatever indentation you give to the first item.
Guido van Rossume531e4b1994-04-16 08:29:27 +00001509
1510If a line is a continuation line because the line preceding it ends with
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +00001511a backslash, the third and following lines of the statement inherit their
1512indentation from the line preceding them. The indentation of the second
1513line in the statement depends on the form of the first (base) line: if
1514the base line is an assignment statement with anything more interesting
1515than the backslash following the leftmost assigning `=', the second line
1516is indented two columns beyond that `='. Else it's indented to two
1517columns beyond the leftmost solid chunk of non-whitespace characters on
1518the base line.
Guido van Rossume531e4b1994-04-16 08:29:27 +00001519
Guido van Rossuma7925f11994-01-26 10:20:16 +00001520Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1521repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1522structure you intend.
1523%c:indent-for-tab-command
1524%c:py-newline-and-indent
1525%c:py-delete-char
1526
1527
1528The next function may be handy when editing code you didn't write:
1529%c:py-guess-indent-offset
1530
1531
1532The remaining `indent' functions apply to a region of Python code. They
1533assume the block structure (equals indentation, in Python) of the region
1534is correct, and alter the indentation in various ways while preserving
1535the block structure:
1536%c:py-indent-region
1537%c:py-shift-region-left
1538%c:py-shift-region-right
1539
1540@MARKING & MANIPULATING REGIONS OF CODE
1541
1542\\[py-mark-block]\t mark block of lines
1543\\[mark-python-def-or-class]\t mark smallest enclosing def
1544\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
1545\\[py-comment-region]\t comment out region of code
1546\\[universal-argument] \\[py-comment-region]\t uncomment region of code
1547%c:py-mark-block
1548%c:mark-python-def-or-class
1549%c:py-comment-region
1550
1551@MOVING POINT
1552
1553\\[py-previous-statement]\t move to statement preceding point
1554\\[py-next-statement]\t move to statement following point
1555\\[py-goto-block-up]\t move up to start of current block
1556\\[beginning-of-python-def-or-class]\t move to start of def
1557\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1558\\[end-of-python-def-or-class]\t move to end of def
1559\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1560
1561The first two move to one statement beyond the statement that contains
1562point. A numeric prefix argument tells them to move that many
1563statements instead. Blank lines, comment lines, and continuation lines
1564do not count as `statements' for these commands. So, e.g., you can go
1565to the first code statement in a file by entering
1566\t\\[beginning-of-buffer]\t to move to the top of the file
1567\t\\[py-next-statement]\t to skip over initial comments and blank lines
1568Or do `\\[py-previous-statement]' with a huge prefix argument.
1569%c:py-previous-statement
1570%c:py-next-statement
1571%c:py-goto-block-up
1572%c:beginning-of-python-def-or-class
1573%c:end-of-python-def-or-class
1574
1575@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
1576
1577`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
1578
1579`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
1580overall class and def structure of a module.
1581
1582`\\[back-to-indentation]' moves point to a line's first non-blank character.
1583
1584`\\[indent-relative]' is handy for creating odd indentation.
1585
1586@OTHER EMACS HINTS
1587
1588If you don't like the default value of a variable, change its value to
1589whatever you do like by putting a `setq' line in your .emacs file.
1590E.g., to set the indentation increment to 4, put this line in your
1591.emacs:
1592\t(setq py-indent-offset 4)
1593To see the value of a variable, do `\\[describe-variable]' and enter the variable
1594name at the prompt.
1595
1596When entering a key sequence like `C-c C-n', it is not necessary to
1597release the CONTROL key after doing the `C-c' part -- it suffices to
1598press the CONTROL key, press and release `c' (while still holding down
1599CONTROL), press and release `n' (while still holding down CONTROL), &
1600then release CONTROL.
1601
1602Entering Python mode calls with no arguments the value of the variable
Guido van Rossuma67bb7e1994-11-10 23:01:59 +00001603`python-mode-hook', if that value exists and is not nil; for backward
1604compatibility it also tries `py-mode-hook'; see the `Hooks' section of
1605the Elisp manual for details.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001606
1607Obscure: When python-mode is first loaded, it looks for all bindings
1608to newline-and-indent in the global keymap, and shadows them with
1609local bindings to py-newline-and-indent."))
1610
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001611
1612;; Helper functions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001613(defvar py-parse-state-re
1614 (concat
1615 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
1616 "\\|"
1617 "^[^ #\t\n]"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001618
Guido van Rossuma7925f11994-01-26 10:20:16 +00001619;; returns the parse state at point (see parse-partial-sexp docs)
1620(defun py-parse-state ()
1621 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001622 (let ((here (point)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001623 ;; back up to the first preceding line (if any; else start of
1624 ;; buffer) that begins with a popular Python keyword, or a non-
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001625 ;; whitespace and non-comment character. These are good places
1626 ;; to start parsing to see whether where we started is at a
1627 ;; non-zero nesting level. It may be slow for people who write
1628 ;; huge code blocks or huge lists ... tough beans.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001629 (re-search-backward py-parse-state-re nil 'move)
1630 (beginning-of-line)
1631 (parse-partial-sexp (point) here))))
1632
1633;; if point is at a non-zero nesting level, returns the number of the
1634;; character that opens the smallest enclosing unclosed list; else
1635;; returns nil.
1636(defun py-nesting-level ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001637 (let ((status (py-parse-state)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001638 (if (zerop (car status))
1639 nil ; not in a nest
1640 (car (cdr status))))) ; char# of open bracket
1641
1642;; t iff preceding line ends with backslash that's not in a comment
1643(defun py-backslash-continuation-line-p ()
1644 (save-excursion
1645 (beginning-of-line)
1646 (and
1647 ;; use a cheap test first to avoid the regexp if possible
1648 ;; use 'eq' because char-after may return nil
1649 (eq (char-after (- (point) 2)) ?\\ )
1650 ;; make sure; since eq test passed, there is a preceding line
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001651 (forward-line -1) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +00001652 (looking-at py-continued-re))))
1653
1654;; t iff current line is a continuation line
1655(defun py-continuation-line-p ()
1656 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001657 (beginning-of-line)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001658 (or (py-backslash-continuation-line-p)
1659 (py-nesting-level))))
1660
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001661;; go to initial line of current statement; usually this is the line
1662;; we're on, but if we're on the 2nd or following lines of a
1663;; continuation block, we need to go up to the first line of the
1664;; block.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001665;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001666;; Tricky: We want to avoid quadratic-time behavior for long continued
1667;; blocks, whether of the backslash or open-bracket varieties, or a
1668;; mix of the two. The following manages to do that in the usual
1669;; cases.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001670(defun py-goto-initial-line ()
1671 (let ( open-bracket-pos )
1672 (while (py-continuation-line-p)
1673 (beginning-of-line)
1674 (if (py-backslash-continuation-line-p)
1675 (while (py-backslash-continuation-line-p)
1676 (forward-line -1))
1677 ;; else zip out of nested brackets/braces/parens
1678 (while (setq open-bracket-pos (py-nesting-level))
1679 (goto-char open-bracket-pos)))))
1680 (beginning-of-line))
1681
1682;; go to point right beyond final line of current statement; usually
1683;; this is the start of the next line, but if this is a multi-line
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001684;; statement we need to skip over the continuation lines. Tricky:
1685;; Again we need to be clever to avoid quadratic time behavior.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001686(defun py-goto-beyond-final-line ()
1687 (forward-line 1)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001688 (let (state)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001689 (while (and (py-continuation-line-p)
1690 (not (eobp)))
1691 ;; skip over the backslash flavor
1692 (while (and (py-backslash-continuation-line-p)
1693 (not (eobp)))
1694 (forward-line 1))
1695 ;; if in nest, zip to the end of the nest
1696 (setq state (py-parse-state))
1697 (if (and (not (zerop (car state)))
1698 (not (eobp)))
1699 (progn
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001700 ;; BUG ALERT: I could swear, from reading the docs, that
Guido van Rossuma7925f11994-01-26 10:20:16 +00001701 ;; the 3rd argument should be plain 0
1702 (parse-partial-sexp (point) (point-max) (- 0 (car state))
1703 nil state)
1704 (forward-line 1))))))
1705
1706;; t iff statement opens a block == iff it ends with a colon that's
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001707;; not in a comment. point should be at the start of a statement
Guido van Rossuma7925f11994-01-26 10:20:16 +00001708(defun py-statement-opens-block-p ()
1709 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001710 (let ((start (point))
1711 (finish (progn (py-goto-beyond-final-line) (1- (point))))
1712 (searching t)
1713 (answer nil)
1714 state)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001715 (goto-char start)
1716 (while searching
1717 ;; look for a colon with nothing after it except whitespace, and
1718 ;; maybe a comment
1719 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
1720 finish t)
1721 (if (eq (point) finish) ; note: no `else' clause; just
1722 ; keep searching if we're not at
1723 ; the end yet
1724 ;; sure looks like it opens a block -- but it might
1725 ;; be in a comment
1726 (progn
1727 (setq searching nil) ; search is done either way
1728 (setq state (parse-partial-sexp start
1729 (match-beginning 0)))
1730 (setq answer (not (nth 4 state)))))
1731 ;; search failed: couldn't find another interesting colon
1732 (setq searching nil)))
1733 answer)))
1734
1735;; go to point right beyond final line of block begun by the current
1736;; line. This is the same as where py-goto-beyond-final-line goes
1737;; unless we're on colon line, in which case we go to the end of the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001738;; block. assumes point is at bolp
Guido van Rossuma7925f11994-01-26 10:20:16 +00001739(defun py-goto-beyond-block ()
1740 (if (py-statement-opens-block-p)
1741 (py-mark-block nil 'just-move)
1742 (py-goto-beyond-final-line)))
1743
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001744;; go to start of first statement (not blank or comment or
1745;; continuation line) at or preceding point. returns t if there is
1746;; one, else nil
Guido van Rossuma7925f11994-01-26 10:20:16 +00001747(defun py-goto-statement-at-or-above ()
1748 (py-goto-initial-line)
1749 (if (looking-at py-blank-or-comment-re)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001750 ;; skip back over blank & comment lines
1751 ;; note: will skip a blank or comment line that happens to be
1752 ;; a continuation line too
1753 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
1754 (progn (py-goto-initial-line) t)
1755 nil)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001756 t))
1757
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001758;; go to start of first statement (not blank or comment or
1759;; continuation line) following the statement containing point returns
1760;; t if there is one, else nil
Guido van Rossuma7925f11994-01-26 10:20:16 +00001761(defun py-goto-statement-below ()
1762 (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001763 (let ((start (point)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001764 (py-goto-beyond-final-line)
1765 (while (and
1766 (looking-at py-blank-or-comment-re)
1767 (not (eobp)))
1768 (forward-line 1))
1769 (if (eobp)
1770 (progn (goto-char start) nil)
1771 t)))
1772
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001773;; go to start of statement, at or preceding point, starting with
1774;; keyword KEY. Skips blank lines and non-indenting comments upward
1775;; first. If that statement starts with KEY, done, else go back to
1776;; first enclosing block starting with KEY. If successful, leaves
1777;; point at the start of the KEY line & returns t. Else leaves point
1778;; at an undefined place & returns nil.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001779(defun py-go-up-tree-to-keyword (key)
1780 ;; skip blanks and non-indenting #
1781 (py-goto-initial-line)
1782 (while (and
1783 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1784 (zerop (forward-line -1))) ; go back
1785 nil)
1786 (py-goto-initial-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001787 (let* ((re (concat "[ \t]*" key "\\b"))
1788 (case-fold-search nil) ; let* so looking-at sees this
1789 (found (looking-at re))
1790 (dead nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001791 (while (not (or found dead))
1792 (condition-case nil ; in case no enclosing block
1793 (py-goto-block-up 'no-mark)
1794 (error (setq dead t)))
1795 (or dead (setq found (looking-at re))))
1796 (beginning-of-line)
1797 found))
1798
1799;; return string in buffer from start of indentation to end of line;
1800;; prefix "..." if leading whitespace was skipped
1801(defun py-suck-up-leading-text ()
1802 (save-excursion
1803 (back-to-indentation)
1804 (concat
1805 (if (bolp) "" "...")
1806 (buffer-substring (point) (progn (end-of-line) (point))))))
1807
1808;; assuming point at bolp, return first keyword ([a-z]+) on the line,
1809;; as a Lisp symbol; return nil if none
1810(defun py-suck-up-first-keyword ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001811 (let ((case-fold-search nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001812 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
1813 (intern (buffer-substring (match-beginning 1) (match-end 1)))
1814 nil)))
1815
1816(defun py-make-temp-name ()
1817 (make-temp-name
1818 (concat (file-name-as-directory py-temp-directory) "python")))
1819
1820(defun py-delete-file-silently (fname)
1821 (condition-case nil
1822 (delete-file fname)
1823 (error nil)))
1824
1825(defun py-kill-emacs-hook ()
1826 ;; delete our temp files
1827 (while py-file-queue
1828 (py-delete-file-silently (car py-file-queue))
1829 (setq py-file-queue (cdr py-file-queue)))
Guido van Rossum581d1721994-04-28 08:31:52 +00001830 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001831 ;; run the hook we inherited, if any
1832 (and py-inherited-kill-emacs-hook
1833 (funcall py-inherited-kill-emacs-hook))))
1834
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001835;; make PROCESS's buffer visible, append STRING to it, and force
1836;; display; also make shell-mode believe the user typed this string,
1837;; so that kill-output-from-shell and show-output-from-shell work
1838;; "right"
Guido van Rossuma7925f11994-01-26 10:20:16 +00001839(defun py-append-to-process-buffer (process string)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001840 (let ((cbuf (current-buffer))
1841 (pbuf (process-buffer process))
1842 (py-scroll-process-buffer t))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001843 (set-buffer pbuf)
1844 (goto-char (point-max))
1845 (move-marker (process-mark process) (point))
1846 (if (not py-this-is-emacs-19-p)
1847 (move-marker last-input-start (point))) ; muck w/ shell-mode
1848 (funcall (process-filter process) process string)
1849 (if (not py-this-is-emacs-19-p)
1850 (move-marker last-input-end (point))) ; muck w/ shell-mode
1851 (set-buffer cbuf))
1852 (sit-for 0))
1853
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001854(defun py-keep-region-active ()
1855 ;; do whatever is necessary to keep the region active in XEmacs.
1856 ;; Ignore byte-compiler warnings you might see. Also note that
1857 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
1858 ;; require us to take explicit action.
1859 (and (boundp 'zmacs-region-stays)
1860 (setq zmacs-region-stays t)))
1861
1862
Guido van Rossum1c1fbf81995-03-14 21:33:10 +00001863(defconst py-version "2.12"
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001864 "`python-mode' version number.")
1865(defconst py-help-address "bwarsaw@cnri.reston.va.us"
1866 "Address accepting submission of bug reports.")
1867
1868(defun py-version ()
1869 "Echo the current version of `python-mode' in the minibuffer."
1870 (interactive)
1871 (message "Using `python-mode' version %s" py-version)
1872 (py-keep-region-active))
1873
1874;; only works under Emacs 19
1875;(eval-when-compile
1876; (require 'reporter))
1877
1878(defun py-submit-bug-report (enhancement-p)
1879 "Submit via mail a bug report on `python-mode'.
1880With \\[universal-argument] just submit an enhancement request."
1881 (interactive
1882 (list (not (y-or-n-p
1883 "Is this a bug report? (hit `n' to send other comments) "))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001884 (let ((reporter-prompt-for-summary-p (if enhancement-p
1885 "(Very) brief summary: "
1886 t)))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001887 (require 'reporter)
1888 (reporter-submit-bug-report
1889 py-help-address ;address
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001890 (concat "python-mode " py-version) ;pkgname
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001891 ;; varlist
1892 (if enhancement-p nil
1893 '(py-python-command
1894 py-indent-offset
1895 py-block-comment-prefix
1896 py-scroll-process-buffer
1897 py-temp-directory
1898 py-beep-if-tab-change))
1899 nil ;pre-hooks
1900 nil ;post-hooks
1901 "Dear Barry,") ;salutation
1902 (if enhancement-p nil
1903 (set-mark (point))
1904 (insert
1905"Please replace this text with a sufficiently large code sample\n\
1906and an exact recipe so that I can reproduce your problem. Failure\n\
1907to do so may mean a greater delay in fixing your bug.\n\n")
1908 (exchange-point-and-mark)
1909 (py-keep-region-active))))
1910
1911
1912;; arrange to kill temp files when Emacs exists
1913(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
1914 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
1915 ;; have to trust that other people are as respectful of our hook
1916 ;; fiddling as we are of theirs
1917 (if (boundp 'py-inherited-kill-emacs-hook)
1918 ;; we were loaded before -- trust others not to have screwed us
1919 ;; in the meantime (no choice, really)
1920 nil
1921 ;; else arrange for our hook to run theirs
1922 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
1923 (setq kill-emacs-hook 'py-kill-emacs-hook)))
1924
1925
1926
1927(provide 'python-mode)
1928;;; python-mode.el ends here