blob: ec2b5cb321b835a3e75215658db858ef7e2cf550 [file] [log] [blame]
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
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
Barry Warsawcfec3591995-03-10 15:58:16 +00008;; Created: Feb 1992
Barry Warsaw7b0f5681995-03-08 21:33:04 +00009;; Version: $Revision$
10;; Last Modified: $Date$
11;; Keywords: python editing language major-mode
12
Barry Warsawcfec3591995-03-10 15:58:16 +000013;; This software is provided as-is, without express or implied
14;; warranty. Permission to use, copy, modify, distribute or sell this
15;; software, without fee, for any purpose and by any individual or
16;; organization, is hereby granted, provided that the above copyright
17;; notice and this paragraph appear in all copies.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000018
19;;; Commentary:
Barry Warsaw7ae77681994-12-12 20:38:05 +000020;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +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
Barry Warsawcfec3591995-03-10 15:58:16 +000024;; have undertaken maintenance of the mode.
Barry Warsaw7b0f5681995-03-08 21:33:04 +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):
Barry Warsaw7ae77681994-12-12 20:38:05 +000034;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +000035;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Barry Warsaw7ae77681994-12-12 20:38:05 +000036;; (setq auto-mode-alist
37;; (cons '("\\.py$" . python-mode) auto-mode-alist))
38
Barry Warsawb5e0ecb1995-03-14 18:32:54 +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
Barry Warsaw7b0f5681995-03-08 21:33:04 +000052;; Here's a brief to do list:
53;;
Barry Warsawb5e0ecb1995-03-14 18:32:54 +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.
Barry Warsaw7ae77681994-12-12 20:38:05 +000060
Barry Warsaw7b0f5681995-03-08 21:33:04 +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.
Barry Warsaw7ae77681994-12-12 20:38:05 +000067
Barry Warsaw7b0f5681995-03-08 21:33:04 +000068;; LCD Archive Entry:
69;; python-mode|Barry A. Warsaw|bwarsaw@cnri.reston.va.us
70;; |Major mode for editing Python programs
71;; |$Date$|$Revision$|
Barry Warsaw7ae77681994-12-12 20:38:05 +000072
Barry Warsaw7b0f5681995-03-08 21:33:04 +000073;;; Code:
74
75
76;; user definable variables
77;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +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.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000084Note that `\\[py-guess-indent-offset]' can usually guess a good value
85when you're editing someone else's Python code.")
Barry Warsaw7ae77681994-12-12 20:38:05 +000086
87(defvar py-block-comment-prefix "##"
Barry Warsaw7b0f5681995-03-08 21:33:04 +000088 "*String used by `py-comment-region' to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +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
Barry Warsaw7b0f5681995-03-08 21:33:04 +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))))
Barry Warsaw7ae77681994-12-12 20:38:05 +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
Barry Warsaw7b0f5681995-03-08 21:33:04 +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
Barry Warsaw4f005cf1995-03-08 22:02:44 +0000164(defvar python-font-lock-keywords
Barry Warsaw65bc7a71995-03-08 22:25:47 +0000165 (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 )
Barry Warsawc723b751995-03-08 22:03:16 +0000187 "*Additional keywords to highlight `python-mode' buffers.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000188
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
Barry Warsaw7ae77681994-12-12 20:38:05 +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
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000211 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000212(define-abbrev-table 'python-mode-abbrev-table nil)
213
Barry Warsaw7ae77681994-12-12 20:38:05 +0000214(defvar python-mode-hook nil
215 "*Hook called by `python-mode'.")
216
Barry Warsaw7b0f5681995-03-08 21:33:04 +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.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000219(and (fboundp 'make-obsolete-variable)
220 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
221
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000222(defvar py-mode-map ()
223 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000224
Barry Warsaw7ae77681994-12-12 20:38:05 +0000225(if py-mode-map
226 ()
227 (setq py-mode-map (make-sparse-keymap))
228
Barry Warsaw7b0f5681995-03-08 21:33:04 +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.
Barry Warsaw7ae77681994-12-12 20:38:05 +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
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000237 ;; BAW - you could do it this way, but its not considered proper
238 ;; major-mode form.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000239 (mapcar (function
240 (lambda (x)
241 (define-key py-mode-map (car x) (cdr x))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000242 '((":" . py-electric-colon)
243 ("\C-c\C-c" . py-execute-buffer)
Barry Warsaw7b0f5681995-03-08 21:33:04 +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)
Barry Warsaw850437a1995-03-08 21:50:28 +0000255 ("\C-c\C-m" . py-mark-block)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000256 ("\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)
Barry Warsaw850437a1995-03-08 21:50:28 +0000261 ( "\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 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000266
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000267(defvar py-mode-syntax-table nil
268 "Syntax table used in `python-mode' buffers.")
269
Barry Warsaw7ae77681994-12-12 20:38:05 +0000270(if py-mode-syntax-table
271 ()
272 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000273 ;; BAW - again, blech.
Barry Warsaw7ae77681994-12-12 20:38:05 +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
292(defconst py-stringlit-re
293 (concat
294 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
295 "\\|" ; or
296 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000297 "Regexp matching a Python string literal.")
Barry Warsaw7ae77681994-12-12 20:38:05 +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
303 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
304 "\\\\$")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000305 "Regexp matching Python lines that are continued via backslash.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000306
307(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000308 "Regexp matching blank or comment lines.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000309
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000310(defconst py-outdent-re
311 (concat "\\(" (mapconcat 'identity
312 '("else:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000313 "except\\(\\s +.*\\)?:"
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000314 "finally:"
315 "elif\\s +.*:")
316 "\\|")
317 "\\)")
318 "Regexp matching clauses to be outdented one level.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000319
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000320(defconst py-no-outdent-re
321 (concat "\\(" (mapconcat 'identity
Barry Warsaw464c94a1995-03-14 23:25:44 +0000322 '("try:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000323 "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
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000332
333;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000334(defun python-mode ()
335 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +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.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000343
344COMMANDS
345\\{py-mode-map}
346VARIABLES
347
348py-indent-offset\tindentation increment
349py-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)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000356 (set-syntax-table py-mode-syntax-table)
Barry Warsaw7b0f5681995-03-08 21:33:04 +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...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000362 (mapcar (function (lambda (x)
363 (make-local-variable (car x))
364 (set (car x) (cdr x))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +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)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000373 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Barry Warsaw7b0f5681995-03-08 21:33:04 +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)
Barry Warsaw7ae77681994-12-12 20:38:05 +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
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000399 ;; run the mode hook. py-mode-hook use is deprecated
Barry Warsaw7ae77681994-12-12 20:38:05 +0000400 (if python-mode-hook
401 (run-hooks 'python-mode-hook)
402 (run-hooks 'py-mode-hook)))
403
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000404
Barry Warsawb91b7431995-03-14 15:55:20 +0000405;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000406(defun py-outdent-p ()
407 ;; returns non-nil if the current line should outdent one level
408 (save-excursion
409 (and (progn (back-to-indentation)
410 (looking-at py-outdent-re))
411 (progn (backward-to-indentation 1)
412 (while (or (looking-at py-blank-or-comment-re)
413 (bobp))
414 (backward-to-indentation 1))
415 (not (looking-at py-no-outdent-re)))
416 )))
417
418
Barry Warsawb91b7431995-03-14 15:55:20 +0000419(defun py-electric-colon (arg)
420 "Insert a colon.
421In certain cases the line is outdented appropriately. If a numeric
422argument is provided, that many colons are inserted non-electrically."
423 (interactive "P")
424 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000425 (save-excursion
426 (let ((here (point))
427 (outdent 0)
428 (indent (py-compute-indentation)))
429 (if (and (not arg)
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000430 (py-outdent-p)
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000431 (= indent (progn
432 (forward-line -1)
433 (py-compute-indentation)))
434 )
435 (setq outdent py-indent-offset))
Barry Warsaw9b623b31995-03-14 23:59:07 +0000436 ;; electric colon won't re-indent lines that start in column
437 ;; zero. you'd have to use TAB for that. TBD: Is there a
438 ;; better way to determine this???
439 (if (zerop (current-indentation)) nil
440 (goto-char here)
441 (beginning-of-line)
442 (delete-horizontal-space)
443 (indent-to (- indent outdent))
444 ))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000445
446
Barry Warsaw7ae77681994-12-12 20:38:05 +0000447;;; Functions that execute Python commands in a subprocess
Barry Warsaw7ae77681994-12-12 20:38:05 +0000448(defun py-shell ()
449 "Start an interactive Python interpreter in another window.
450This is like Shell mode, except that Python is running in the window
451instead of a shell. See the `Interactive Shell' and `Shell Mode'
452sections of the Emacs manual for details, especially for the key
453bindings active in the `*Python*' buffer.
454
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000455See the docs for variable `py-scroll-buffer' for info on scrolling
Barry Warsaw7ae77681994-12-12 20:38:05 +0000456behavior in the process window.
457
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000458Warning: Don't use an interactive Python if you change sys.ps1 or
459sys.ps2 from their default values, or if you're running code that
460prints `>>> ' or `... ' at the start of a line. `python-mode' can't
461distinguish your output from Python's output, and assumes that `>>> '
462at the start of a line is a prompt from Python. Similarly, the Emacs
463Shell mode code assumes that both `>>> ' and `... ' at the start of a
464line are Python prompts. Bad things can happen if you fool either
465mode.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000466
467Warning: If you do any editing *in* the process buffer *while* the
468buffer is accepting output from Python, do NOT attempt to `undo' the
469changes. Some of the output (nowhere near the parts you changed!) may
470be lost if you do. This appears to be an Emacs bug, an unfortunate
471interaction between undo and process filters; the same problem exists in
472non-Python process buffers using the default (Emacs-supplied) process
473filter."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000474 ;; BAW - should undo be disabled in the python process buffer, if
475 ;; this bug still exists?
Barry Warsaw7ae77681994-12-12 20:38:05 +0000476 (interactive)
477 (if py-this-is-emacs-19-p
478 (progn
479 (require 'comint)
480 (switch-to-buffer-other-window
481 (make-comint "Python" py-python-command)))
482 (progn
483 (require 'shell)
484 (switch-to-buffer-other-window
485 (make-shell "Python" py-python-command))))
486 (make-local-variable 'shell-prompt-pattern)
487 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
488 (set-process-filter (get-buffer-process (current-buffer))
489 'py-process-filter)
490 (set-syntax-table py-mode-syntax-table))
491
492(defun py-execute-region (start end)
493 "Send the region between START and END to a Python interpreter.
494If there is a *Python* process it is used.
495
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000496Hint: If you want to execute part of a Python file several times
497\(e.g., perhaps you're developing a function and want to flesh it out
498a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
499the region of interest, and send the code to a *Python* process via
500`\\[py-execute-buffer]' instead.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000501
502Following are subtleties to note when using a *Python* process:
503
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000504If a *Python* process is used, the region is copied into a temporary
505file (in directory `py-temp-directory'), and an `execfile' command is
506sent to Python naming that file. If you send regions faster than
507Python can execute them, `python-mode' will save them into distinct
508temp files, and execute the next one in the queue the next time it
509sees a `>>> ' prompt from Python. Each time this happens, the process
510buffer is popped into a window (if it's not already in some window) so
511you can see it, and a comment of the form
Barry Warsaw7ae77681994-12-12 20:38:05 +0000512
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000513 \t## working on region in file <name> ...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000514
515is inserted at the end.
516
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000517Caution: No more than 26 regions can be pending at any given time.
518This limit is (indirectly) inherited from libc's mktemp(3).
519`python-mode' does not try to protect you from exceeding the limit.
520It's extremely unlikely that you'll get anywhere close to the limit in
521practice, unless you're trying to be a jerk <grin>.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000522
523See the `\\[py-shell]' docs for additional warnings."
524 (interactive "r")
525 (or (< start end) (error "Region is empty"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000526 (let ((pyproc (get-process "Python"))
527 fname)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000528 (if (null pyproc)
529 (shell-command-on-region start end py-python-command)
530 ;; else feed it thru a temp file
531 (setq fname (py-make-temp-name))
532 (write-region start end fname nil 'no-msg)
533 (setq py-file-queue (append py-file-queue (list fname)))
534 (if (cdr py-file-queue)
535 (message "File %s queued for execution" fname)
536 ;; else
537 (py-execute-file pyproc fname)))))
538
539(defun py-execute-file (pyproc fname)
540 (py-append-to-process-buffer
541 pyproc
542 (format "## working on region in file %s ...\n" fname))
543 (process-send-string pyproc (format "execfile('%s')\n" fname)))
544
545(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000546 (let ((curbuf (current-buffer))
547 (pbuf (process-buffer pyproc))
548 (pmark (process-mark pyproc))
549 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000550
551 ;; make sure we switch to a different buffer at least once. if we
552 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000553 ;; window, and point is before the end, and lots of output is
554 ;; coming at a fast pace, then (a) simple cursor-movement commands
555 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
556 ;; to have a visible effect (the window just doesn't get updated,
557 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
558 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000559 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000560 ;; #b makes no sense to me at all. #a almost makes sense: unless
561 ;; we actually change buffers, set_buffer_internal in buffer.c
562 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
563 ;; seems to make the Emacs command loop reluctant to update the
564 ;; display. Perhaps the default process filter in process.c's
565 ;; read_process_output has update_mode_lines++ for a similar
566 ;; reason? beats me ...
567
568 ;; BAW - we want to check to see if this still applies
Barry Warsaw7ae77681994-12-12 20:38:05 +0000569 (if (eq curbuf pbuf) ; mysterious ugly hack
570 (set-buffer (get-buffer-create "*scratch*")))
571
572 (set-buffer pbuf)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000573 (let* ((start (point))
574 (goback (< start pmark))
575 (buffer-read-only nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000576 (goto-char pmark)
577 (insert string)
578 (move-marker pmark (point))
579 (setq file-finished
580 (and py-file-queue
581 (equal ">>> "
582 (buffer-substring
583 (prog2 (beginning-of-line) (point)
584 (goto-char pmark))
585 (point)))))
586 (if goback (goto-char start)
587 ;; else
588 (if py-scroll-process-buffer
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000589 (let* ((pop-up-windows t)
590 (pwin (display-buffer pbuf)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000591 (set-window-point pwin (point))))))
592 (set-buffer curbuf)
593 (if file-finished
594 (progn
595 (py-delete-file-silently (car py-file-queue))
596 (setq py-file-queue (cdr py-file-queue))
597 (if py-file-queue
598 (py-execute-file pyproc (car py-file-queue)))))))
599
600(defun py-execute-buffer ()
601 "Send the contents of the buffer to a Python interpreter.
602If there is a *Python* process buffer it is used. If a clipping
603restriction is in effect, only the accessible portion of the buffer is
604sent. A trailing newline will be supplied if needed.
605
606See the `\\[py-execute-region]' docs for an account of some subtleties."
607 (interactive)
608 (py-execute-region (point-min) (point-max)))
609
610
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000611
612;; Functions for Python style indentation
Barry Warsaw7ae77681994-12-12 20:38:05 +0000613(defun py-delete-char ()
614 "Reduce indentation or delete character.
615If point is at the leftmost column, deletes the preceding newline.
616
617Else if point is at the leftmost non-blank character of a line that is
618neither a continuation line nor a non-indenting comment line, or if
619point is at the end of a blank line, reduces the indentation to match
620that of the line that opened the current block of code. The line that
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000621opened the block is displayed in the echo area to help you keep track
622of where you are.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000623
624Else the preceding character is deleted, converting a tab to spaces if
625needed so that only a single column position is deleted."
626 (interactive "*")
627 (if (or (/= (current-indentation) (current-column))
628 (bolp)
629 (py-continuation-line-p)
630 (looking-at "#[^ \t\n]")) ; non-indenting #
631 (backward-delete-char-untabify 1)
632 ;; else indent the same as the colon line that opened the block
633
634 ;; force non-blank so py-goto-block-up doesn't ignore it
635 (insert-char ?* 1)
636 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000637 (let ((base-indent 0) ; indentation of base line
638 (base-text "") ; and text of base line
639 (base-found-p nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000640 (condition-case nil ; in case no enclosing block
641 (save-excursion
642 (py-goto-block-up 'no-mark)
643 (setq base-indent (current-indentation)
644 base-text (py-suck-up-leading-text)
645 base-found-p t))
646 (error nil))
647 (delete-char 1) ; toss the dummy character
648 (delete-horizontal-space)
649 (indent-to base-indent)
650 (if base-found-p
651 (message "Closes block: %s" base-text)))))
652
Barry Warsawfc8a01f1995-03-09 16:07:29 +0000653;; required for pending-del and delsel modes
654(put 'py-delete-char 'delete-selection 'supersede)
655(put 'py-delete-char 'pending-delete 'supersede)
656
Barry Warsaw7ae77681994-12-12 20:38:05 +0000657(defun py-indent-line ()
658 "Fix the indentation of the current line according to Python rules."
659 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000660 (let* ((ci (current-indentation))
661 (move-to-indentation-p (<= (current-column) ci))
Barry Warsawb86bbad1995-03-14 15:56:35 +0000662 (need (py-compute-indentation)))
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000663 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000664 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000665 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000666 (if (/= ci need)
667 (save-excursion
668 (beginning-of-line)
669 (delete-horizontal-space)
670 (indent-to need)))
671 (if move-to-indentation-p (back-to-indentation))))
672
673(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000674 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000675This is just `strives to' because correct indentation can't be computed
676from scratch for Python code. In general, deletes the whitespace before
677point, inserts a newline, and takes an educated guess as to how you want
678the new line indented."
679 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000680 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000681 (if (< ci (current-column)) ; if point beyond indentation
682 (newline-and-indent)
683 ;; else try to act like newline-and-indent "normally" acts
684 (beginning-of-line)
685 (insert-char ?\n 1)
686 (move-to-column ci))))
687
688(defun py-compute-indentation ()
689 (save-excursion
690 (beginning-of-line)
691 (cond
692 ;; are we on a continuation line?
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000693 ((py-continuation-line-p)
694 (let ((startpos (point))
695 (open-bracket-pos (py-nesting-level))
696 endpos searching found)
697 (if open-bracket-pos
698 (progn
699 ;; align with first item in list; else a normal
700 ;; indent beyond the line with the open bracket
701 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
702 ;; is the first list item on the same line?
703 (skip-chars-forward " \t")
704 (if (null (memq (following-char) '(?\n ?# ?\\)))
705 ; yes, so line up with it
706 (current-column)
707 ;; first list item on another line, or doesn't exist yet
708 (forward-line 1)
709 (while (and (< (point) startpos)
710 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
711 (forward-line 1))
712 (if (< (point) startpos)
713 ;; again mimic the first list item
714 (current-indentation)
715 ;; else they're about to enter the first item
716 (goto-char open-bracket-pos)
717 (+ (current-indentation) py-indent-offset))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000718
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000719 ;; else on backslash continuation line
720 (forward-line -1)
721 (if (py-continuation-line-p) ; on at least 3rd line in block
722 (current-indentation) ; so just continue the pattern
723 ;; else started on 2nd line in block, so indent more.
724 ;; if base line is an assignment with a start on a RHS,
725 ;; indent to 2 beyond the leftmost "="; else skip first
726 ;; chunk of non-whitespace characters on base line, + 1 more
727 ;; column
728 (end-of-line)
729 (setq endpos (point) searching t)
730 (back-to-indentation)
731 (setq startpos (point))
732 ;; look at all "=" from left to right, stopping at first
733 ;; one not nested in a list or string
734 (while searching
735 (skip-chars-forward "^=" endpos)
736 (if (= (point) endpos)
737 (setq searching nil)
738 (forward-char 1)
739 (setq state (parse-partial-sexp startpos (point)))
740 (if (and (zerop (car state)) ; not in a bracket
741 (null (nth 3 state))) ; & not in a string
742 (progn
743 (setq searching nil) ; done searching in any case
744 (setq found
745 (not (or
746 (eq (following-char) ?=)
747 (memq (char-after (- (point) 2))
748 '(?< ?> ?!)))))))))
749 (if (or (not found) ; not an assignment
750 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
751 (progn
752 (goto-char startpos)
753 (skip-chars-forward "^ \t\n")))
754 (1+ (current-column))))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000755
756 ;; not on a continuation line
757
758 ;; if at start of restriction, or on a non-indenting comment line,
759 ;; assume they intended whatever's there
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000760 ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
761 (current-indentation))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000762
763 ;; else indentation based on that of the statement that precedes
764 ;; us; use the first line of that statement to establish the base,
765 ;; in case the user forced a non-std indentation for the
766 ;; continuation lines (if any)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000767 (t
768 ;; skip back over blank & non-indenting comment lines
769 ;; note: will skip a blank or non-indenting comment line that
770 ;; happens to be a continuation line too
771 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
772 nil 'move)
773 ;; if we landed inside a string, go to the beginning of that
774 ;; string. this handles triple quoted, multi-line spanning
775 ;; strings.
776 (let ((state (parse-partial-sexp
777 (save-excursion (beginning-of-python-def-or-class)
778 (point))
779 (point))))
780 (if (nth 3 state)
781 (goto-char (nth 2 state))))
782 (py-goto-initial-line)
783 (if (py-statement-opens-block-p)
784 (+ (current-indentation) py-indent-offset)
785 (current-indentation))))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000786
787(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000788 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000789By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000790`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +0000791Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000792`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +0000793their own buffer-local copy), both those currently existing and those
794created later in the Emacs session.
795
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000796Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000797There's no excuse for such foolishness, but sometimes you have to deal
798with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000799`py-indent-offset' to what it thinks it was when they created the
800mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000801
802Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000803looking for a line that opens a block of code. `py-indent-offset' is
804set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +0000805statement following it. If the search doesn't succeed going forward,
806it's tried again going backward."
807 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000808 (let (new-value
809 (start (point))
810 restart
811 (found nil)
812 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000813 (py-goto-initial-line)
814 (while (not (or found (eobp)))
815 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
816 (progn
817 (setq restart (point))
818 (py-goto-initial-line)
819 (if (py-statement-opens-block-p)
820 (setq found t)
821 (goto-char restart)))))
822 (if found
823 ()
824 (goto-char start)
825 (py-goto-initial-line)
826 (while (not (or found (bobp)))
827 (setq found
828 (and
829 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
830 (or (py-goto-initial-line) t) ; always true -- side effect
831 (py-statement-opens-block-p)))))
832 (setq colon-indent (current-indentation)
833 found (and found (zerop (py-next-statement 1)))
834 new-value (- (current-indentation) colon-indent))
835 (goto-char start)
836 (if found
837 (progn
838 (funcall (if global 'kill-local-variable 'make-local-variable)
839 'py-indent-offset)
840 (setq py-indent-offset new-value)
841 (message "%s value of py-indent-offset set to %d"
842 (if global "Global" "Local")
843 py-indent-offset))
844 (error "Sorry, couldn't guess a value for py-indent-offset"))))
845
846(defun py-shift-region (start end count)
847 (save-excursion
848 (goto-char end) (beginning-of-line) (setq end (point))
849 (goto-char start) (beginning-of-line) (setq start (point))
850 (indent-rigidly start end count)))
851
852(defun py-shift-region-left (start end &optional count)
853 "Shift region of Python code to the left.
854The lines from the line containing the start of the current region up
855to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000856shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000857
858If a prefix argument is given, the region is instead shifted by that
859many columns."
860 (interactive "*r\nP") ; region; raw prefix arg
861 (py-shift-region start end
862 (- (prefix-numeric-value
863 (or count py-indent-offset)))))
864
865(defun py-shift-region-right (start end &optional count)
866 "Shift region of Python code to the right.
867The lines from the line containing the start of the current region up
868to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000869shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000870
871If a prefix argument is given, the region is instead shifted by that
872many columns."
873 (interactive "*r\nP") ; region; raw prefix arg
874 (py-shift-region start end (prefix-numeric-value
875 (or count py-indent-offset))))
876
877(defun py-indent-region (start end &optional indent-offset)
878 "Reindent a region of Python code.
879The lines from the line containing the start of the current region up
880to (but not including) the line containing the end of the region are
881reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000882character in the first column, the first line is left alone and the
883rest of the region is reindented with respect to it. Else the entire
884region is reindented with respect to the (closest code or
885indenting-comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000886
887This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000888control structures are introduced or removed, or to reformat code
889using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000890
891If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000892the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +0000893used.
894
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000895Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +0000896is called! This function does not compute proper indentation from
897scratch (that's impossible in Python), it merely adjusts the existing
898indentation to be correct in context.
899
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000900Warning: This function really has no idea what to do with
901non-indenting comment lines, and shifts them as if they were indenting
902comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000903
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000904Special cases: whitespace is deleted from blank lines; continuation
905lines are shifted by the same amount their initial line was shifted,
906in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +0000907initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000908 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +0000909 (save-excursion
910 (goto-char end) (beginning-of-line) (setq end (point-marker))
911 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000912 (let ((py-indent-offset (prefix-numeric-value
913 (or indent-offset py-indent-offset)))
914 (indents '(-1)) ; stack of active indent levels
915 (target-column 0) ; column to which to indent
916 (base-shifted-by 0) ; amount last base line was shifted
917 (indent-base (if (looking-at "[ \t\n]")
918 (py-compute-indentation)
919 0))
920 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000921 (while (< (point) end)
922 (setq ci (current-indentation))
923 ;; figure out appropriate target column
924 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000925 ((or (eq (following-char) ?#) ; comment in column 1
926 (looking-at "[ \t]*$")) ; entirely blank
927 (setq target-column 0))
928 ((py-continuation-line-p) ; shift relative to base line
929 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000930 (t ; new base line
931 (if (> ci (car indents)) ; going deeper; push it
932 (setq indents (cons ci indents))
933 ;; else we should have seen this indent before
934 (setq indents (memq ci indents)) ; pop deeper indents
935 (if (null indents)
936 (error "Bad indentation in region, at line %d"
937 (save-restriction
938 (widen)
939 (1+ (count-lines 1 (point)))))))
940 (setq target-column (+ indent-base
941 (* py-indent-offset
942 (- (length indents) 2))))
943 (setq base-shifted-by (- target-column ci))))
944 ;; shift as needed
945 (if (/= ci target-column)
946 (progn
947 (delete-horizontal-space)
948 (indent-to target-column)))
949 (forward-line 1))))
950 (set-marker end nil))
951
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000952
953;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +0000954(defun py-previous-statement (count)
955 "Go to the start of previous Python statement.
956If the statement at point is the i'th Python statement, goes to the
957start of statement i-COUNT. If there is no such statement, goes to the
958first statement. Returns count of statements left to move.
959`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000960 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +0000961 (if (< count 0) (py-next-statement (- count))
962 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000963 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000964 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000965 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +0000966 (> count 0)
967 (zerop (forward-line -1))
968 (py-goto-statement-at-or-above))
969 (setq count (1- count)))
970 (if (> count 0) (goto-char start)))
971 count))
972
973(defun py-next-statement (count)
974 "Go to the start of next Python statement.
975If the statement at point is the i'th Python statement, goes to the
976start of statement i+COUNT. If there is no such statement, goes to the
977last statement. Returns count of statements left to move. `Statements'
978do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000979 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +0000980 (if (< count 0) (py-previous-statement (- count))
981 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000982 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000983 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000984 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +0000985 (> count 0)
986 (py-goto-statement-below))
987 (setq count (1- count)))
988 (if (> count 0) (goto-char start)))
989 count))
990
991(defun py-goto-block-up (&optional nomark)
992 "Move up to start of current block.
993Go to the statement that starts the smallest enclosing block; roughly
994speaking, this will be the closest preceding statement that ends with a
995colon and is indented less than the statement you started on. If
996successful, also sets the mark to the starting point.
997
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000998`\\[py-mark-block]' can be used afterward to mark the whole code
999block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001000
1001If called from a program, the mark will not be set if optional argument
1002NOMARK is not nil."
1003 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001004 (let ((start (point))
1005 (found nil)
1006 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001007 (py-goto-initial-line)
1008 ;; if on blank or non-indenting comment line, use the preceding stmt
1009 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1010 (progn
1011 (py-goto-statement-at-or-above)
1012 (setq found (py-statement-opens-block-p))))
1013 ;; search back for colon line indented less
1014 (setq initial-indent (current-indentation))
1015 (if (zerop initial-indent)
1016 ;; force fast exit
1017 (goto-char (point-min)))
1018 (while (not (or found (bobp)))
1019 (setq found
1020 (and
1021 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1022 (or (py-goto-initial-line) t) ; always true -- side effect
1023 (< (current-indentation) initial-indent)
1024 (py-statement-opens-block-p))))
1025 (if found
1026 (progn
1027 (or nomark (push-mark start))
1028 (back-to-indentation))
1029 (goto-char start)
1030 (error "Enclosing block not found"))))
1031
1032(defun beginning-of-python-def-or-class (&optional class)
1033 "Move point to start of def (or class, with prefix arg).
1034
1035Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001036arg, looks for a `class' instead. The docs assume the `def' case;
1037just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001038
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001039If point is in a def statement already, and after the `d', simply
1040moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001041
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001042Else (point is not in a def statement, or at or before the `d' of a
1043def statement), searches for the closest preceding def statement, and
1044leaves point at its start. If no such statement can be found, leaves
1045point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001046
1047Returns t iff a def statement is found by these rules.
1048
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001049Note that doing this command repeatedly will take you closer to the
1050start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001051
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001052If you want to mark the current def/class, see
1053`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001054 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001055 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1056 (start-of-line (progn (beginning-of-line) (point)))
1057 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001058 (if (or (/= start-of-stmt start-of-line)
1059 (not at-or-before-p))
1060 (end-of-line)) ; OK to match on this line
1061 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001062 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001063
1064(defun end-of-python-def-or-class (&optional class)
1065 "Move point beyond end of def (or class, with prefix arg) body.
1066
1067By default, looks for an appropriate `def'. If you supply a prefix arg,
1068looks for a `class' instead. The docs assume the `def' case; just
1069substitute `class' for `def' for the other case.
1070
1071If point is in a def statement already, this is the def we use.
1072
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001073Else if the def found by `\\[beginning-of-python-def-or-class]'
1074contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001075
1076Else we search forward for the closest following def, and use that.
1077
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001078If a def can be found by these rules, point is moved to the start of
1079the line immediately following the def block, and the position of the
1080start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001081
1082Else point is moved to the end of the buffer, and nil is returned.
1083
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001084Note that doing this command repeatedly will take you closer to the
1085end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001086
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001087If you want to mark the current def/class, see
1088`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001089 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001090 (let ((start (progn (py-goto-initial-line) (point)))
1091 (which (if class "class" "def"))
1092 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001093 ;; move point to start of appropriate def/class
1094 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1095 (setq state 'at-beginning)
1096 ;; else see if beginning-of-python-def-or-class hits container
1097 (if (and (beginning-of-python-def-or-class class)
1098 (progn (py-goto-beyond-block)
1099 (> (point) start)))
1100 (setq state 'at-end)
1101 ;; else search forward
1102 (goto-char start)
1103 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1104 (progn (setq state 'at-beginning)
1105 (beginning-of-line)))))
1106 (cond
1107 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1108 ((eq state 'at-end) t)
1109 ((eq state 'not-found) nil)
1110 (t (error "internal error in end-of-python-def-or-class")))))
1111
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001112
1113;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001114(defun py-mark-block (&optional extend just-move)
1115 "Mark following block of lines. With prefix arg, mark structure.
1116Easier to use than explain. It sets the region to an `interesting'
1117block of succeeding lines. If point is on a blank line, it goes down to
1118the next non-blank line. That will be the start of the region. The end
1119of the region depends on the kind of line at the start:
1120
1121 - If a comment, the region will include all succeeding comment lines up
1122 to (but not including) the next non-comment line (if any).
1123
1124 - Else if a prefix arg is given, and the line begins one of these
1125 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001126
1127 if elif else try except finally for while def class
1128
Barry Warsaw7ae77681994-12-12 20:38:05 +00001129 the region will be set to the body of the structure, including
1130 following blocks that `belong' to it, but excluding trailing blank
1131 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001132 and all (if any) of the following `except' and `finally' blocks
1133 that belong to the `try' structure will be in the region. Ditto
1134 for if/elif/else, for/else and while/else structures, and (a bit
1135 degenerate, since they're always one-block structures) def and
1136 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001137
1138 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001139 block (see list above), and the block is not a `one-liner' (i.e.,
1140 the statement ends with a colon, not with code), the region will
1141 include all succeeding lines up to (but not including) the next
1142 code statement (if any) that's indented no more than the starting
1143 line, except that trailing blank and comment lines are excluded.
1144 E.g., if the starting line begins a multi-statement `def'
1145 structure, the region will be set to the full function definition,
1146 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001147
1148 - Else the region will include all succeeding lines up to (but not
1149 including) the next blank line, or code or indenting-comment line
1150 indented strictly less than the starting line. Trailing indenting
1151 comment lines are included in this case, but not trailing blank
1152 lines.
1153
1154A msg identifying the location of the mark is displayed in the echo
1155area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1156
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001157If called from a program, optional argument EXTEND plays the role of
1158the prefix arg, and if optional argument JUST-MOVE is not nil, just
1159moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001160 (interactive "P") ; raw prefix arg
1161 (py-goto-initial-line)
1162 ;; skip over blank lines
1163 (while (and
1164 (looking-at "[ \t]*$") ; while blank line
1165 (not (eobp))) ; & somewhere to go
1166 (forward-line 1))
1167 (if (eobp)
1168 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001169 (let ((initial-pos (point))
1170 (initial-indent (current-indentation))
1171 last-pos ; position of last stmt in region
1172 (followers
1173 '((if elif else) (elif elif else) (else)
1174 (try except finally) (except except) (finally)
1175 (for else) (while else)
1176 (def) (class) ) )
1177 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001178
1179 (cond
1180 ;; if comment line, suck up the following comment lines
1181 ((looking-at "[ \t]*#")
1182 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1183 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1184 (setq last-pos (point)))
1185
1186 ;; else if line is a block line and EXTEND given, suck up
1187 ;; the whole structure
1188 ((and extend
1189 (setq first-symbol (py-suck-up-first-keyword) )
1190 (assq first-symbol followers))
1191 (while (and
1192 (or (py-goto-beyond-block) t) ; side effect
1193 (forward-line -1) ; side effect
1194 (setq last-pos (point)) ; side effect
1195 (py-goto-statement-below)
1196 (= (current-indentation) initial-indent)
1197 (setq next-symbol (py-suck-up-first-keyword))
1198 (memq next-symbol (cdr (assq first-symbol followers))))
1199 (setq first-symbol next-symbol)))
1200
1201 ;; else if line *opens* a block, search for next stmt indented <=
1202 ((py-statement-opens-block-p)
1203 (while (and
1204 (setq last-pos (point)) ; always true -- side effect
1205 (py-goto-statement-below)
1206 (> (current-indentation) initial-indent))
1207 nil))
1208
1209 ;; else plain code line; stop at next blank line, or stmt or
1210 ;; indenting comment line indented <
1211 (t
1212 (while (and
1213 (setq last-pos (point)) ; always true -- side effect
1214 (or (py-goto-beyond-final-line) t)
1215 (not (looking-at "[ \t]*$")) ; stop at blank line
1216 (or
1217 (>= (current-indentation) initial-indent)
1218 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1219 nil)))
1220
1221 ;; skip to end of last stmt
1222 (goto-char last-pos)
1223 (py-goto-beyond-final-line)
1224
1225 ;; set mark & display
1226 (if just-move
1227 () ; just return
1228 (push-mark (point) 'no-msg)
1229 (forward-line -1)
1230 (message "Mark set after: %s" (py-suck-up-leading-text))
1231 (goto-char initial-pos))))
1232
1233(defun mark-python-def-or-class (&optional class)
1234 "Set region to body of def (or class, with prefix arg) enclosing point.
1235Pushes the current mark, then point, on the mark ring (all language
1236modes do this, but although it's handy it's never documented ...).
1237
1238In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001239hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1240`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001241
1242And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001243Turned out that was more confusing than useful: the `goto start' and
1244`goto end' commands are usually used to search through a file, and
1245people expect them to act a lot like `search backward' and `search
1246forward' string-search commands. But because Python `def' and `class'
1247can nest to arbitrary levels, finding the smallest def containing
1248point cannot be done via a simple backward search: the def containing
1249point may not be the closest preceding def, or even the closest
1250preceding def that's indented less. The fancy algorithm required is
1251appropriate for the usual uses of this `mark' command, but not for the
1252`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001253
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001254So the def marked by this command may not be the one either of the
1255`goto' commands find: If point is on a blank or non-indenting comment
1256line, moves back to start of the closest preceding code statement or
1257indenting comment line. If this is a `def' statement, that's the def
1258we use. Else searches for the smallest enclosing `def' block and uses
1259that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001260
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001261When an enclosing def is found: The mark is left immediately beyond
1262the last line of the def block. Point is left at the start of the
1263def, except that: if the def is preceded by a number of comment lines
1264followed by (at most) one optional blank line, point is left at the
1265start of the comments; else if the def is preceded by a blank line,
1266point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001267
1268The intent is to mark the containing def/class and its associated
1269documentation, to make moving and duplicating functions and classes
1270pleasant."
1271 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001272 (let ((start (point))
1273 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001274 (push-mark start)
1275 (if (not (py-go-up-tree-to-keyword which))
1276 (progn (goto-char start)
1277 (error "Enclosing %s not found" which))
1278 ;; else enclosing def/class found
1279 (setq start (point))
1280 (py-goto-beyond-block)
1281 (push-mark (point))
1282 (goto-char start)
1283 (if (zerop (forward-line -1)) ; if there is a preceding line
1284 (progn
1285 (if (looking-at "[ \t]*$") ; it's blank
1286 (setq start (point)) ; so reset start point
1287 (goto-char start)) ; else try again
1288 (if (zerop (forward-line -1))
1289 (if (looking-at "[ \t]*#") ; a comment
1290 ;; look back for non-comment line
1291 ;; tricky: note that the regexp matches a blank
1292 ;; line, cuz \n is in the 2nd character class
1293 (and
1294 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1295 (forward-line 1))
1296 ;; no comment, so go back
1297 (goto-char start))))))))
1298
1299(defun py-comment-region (start end &optional uncomment-p)
1300 "Comment out region of code; with prefix arg, uncomment region.
1301The lines from the line containing the start of the current region up
1302to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001303commented out, by inserting the string `py-block-comment-prefix' at
1304the start of each line. With a prefix arg, removes
1305`py-block-comment-prefix' from the start of each line instead."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001306 (interactive "*r\nP") ; region; raw prefix arg
1307 (goto-char end) (beginning-of-line) (setq end (point))
1308 (goto-char start) (beginning-of-line) (setq start (point))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001309 (let ((prefix-len (length py-block-comment-prefix)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001310 (save-excursion
1311 (save-restriction
1312 (narrow-to-region start end)
1313 (while (not (eobp))
1314 (if uncomment-p
1315 (and (string= py-block-comment-prefix
1316 (buffer-substring
1317 (point) (+ (point) prefix-len)))
1318 (delete-char prefix-len))
1319 (insert py-block-comment-prefix))
1320 (forward-line 1))))))
1321
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001322
1323;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001324
1325;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001326;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1327;; out of the right places, along with the keys they're on & current
1328;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00001329(defun py-dump-help-string (str)
1330 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001331 (let ((locals (buffer-local-variables))
1332 funckind funcname func funcdoc
1333 (start 0) mstart end
1334 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001335 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1336 (setq mstart (match-beginning 0) end (match-end 0)
1337 funckind (substring str (match-beginning 1) (match-end 1))
1338 funcname (substring str (match-beginning 2) (match-end 2))
1339 func (intern funcname))
1340 (princ (substitute-command-keys (substring str start mstart)))
1341 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001342 ((equal funckind "c") ; command
1343 (setq funcdoc (documentation func)
1344 keys (concat
1345 "Key(s): "
1346 (mapconcat 'key-description
1347 (where-is-internal func py-mode-map)
1348 ", "))))
1349 ((equal funckind "v") ; variable
1350 (setq funcdoc (substitute-command-keys
1351 (get func 'variable-documentation))
1352 keys (if (assq func locals)
1353 (concat
1354 "Local/Global values: "
1355 (prin1-to-string (symbol-value func))
1356 " / "
1357 (prin1-to-string (default-value func)))
1358 (concat
1359 "Value: "
1360 (prin1-to-string (symbol-value func))))))
1361 (t ; unexpected
1362 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001363 (princ (format "\n-> %s:\t%s\t%s\n\n"
1364 (if (equal funckind "c") "Command" "Variable")
1365 funcname keys))
1366 (princ funcdoc)
1367 (terpri)
1368 (setq start end))
1369 (princ (substitute-command-keys (substring str start))))
1370 (print-help-return-message)))
1371
1372(defun py-describe-mode ()
1373 "Dump long form of Python-mode docs."
1374 (interactive)
1375 (py-dump-help-string "Major mode for editing Python files.
1376Knows about Python indentation, tokens, comments and continuation lines.
1377Paragraphs are separated by blank lines only.
1378
1379Major sections below begin with the string `@'; specific function and
1380variable docs begin with `->'.
1381
1382@EXECUTING PYTHON CODE
1383
1384\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1385\\[py-execute-region]\tsends the current region
1386\\[py-shell]\tstarts a Python interpreter window; this will be used by
1387\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1388%c:py-execute-buffer
1389%c:py-execute-region
1390%c:py-shell
1391
1392@VARIABLES
1393
1394py-indent-offset\tindentation increment
1395py-block-comment-prefix\tcomment string used by py-comment-region
1396
1397py-python-command\tshell command to invoke Python interpreter
1398py-scroll-process-buffer\talways scroll Python process buffer
1399py-temp-directory\tdirectory used for temp files (if needed)
1400
1401py-beep-if-tab-change\tring the bell if tab-width is changed
1402%v:py-indent-offset
1403%v:py-block-comment-prefix
1404%v:py-python-command
1405%v:py-scroll-process-buffer
1406%v:py-temp-directory
1407%v:py-beep-if-tab-change
1408
1409@KINDS OF LINES
1410
1411Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001412preceding line ends with a backslash that's not part of a comment, or
1413the paren/bracket/brace nesting level at the start of the line is
1414non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001415
1416An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001417possibly blanks or tabs), a `comment line' (leftmost non-blank
1418character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001419
1420Comment Lines
1421
1422Although all comment lines are treated alike by Python, Python mode
1423recognizes two kinds that act differently with respect to indentation.
1424
1425An `indenting comment line' is a comment line with a blank, tab or
1426nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001427treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00001428indenting comment line will be indented like the comment line. All
1429other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001430following the initial `#') are `non-indenting comment lines', and
1431their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001432
1433Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001434whenever possible. Non-indenting comment lines are useful in cases
1435like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00001436
1437\ta = b # a very wordy single-line comment that ends up being
1438\t #... continued onto another line
1439
1440\tif a == b:
1441##\t\tprint 'panic!' # old code we've `commented out'
1442\t\treturn a
1443
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001444Since the `#...' and `##' comment lines have a non-whitespace
1445character following the initial `#', Python mode ignores them when
1446computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001447
1448Continuation Lines and Statements
1449
1450The Python-mode commands generally work on statements instead of on
1451individual lines, where a `statement' is a comment or blank line, or a
1452code line and all of its following continuation lines (if any)
1453considered as a single logical unit. The commands in this mode
1454generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001455statement containing point, even if point happens to be in the middle
1456of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001457
1458
1459@INDENTATION
1460
1461Primarily for entering new code:
1462\t\\[indent-for-tab-command]\t indent line appropriately
1463\t\\[py-newline-and-indent]\t insert newline, then indent
1464\t\\[py-delete-char]\t reduce indentation, or delete single character
1465
1466Primarily for reindenting existing code:
1467\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1468\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1469
1470\t\\[py-indent-region]\t reindent region to match its context
1471\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1472\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1473
1474Unlike most programming languages, Python uses indentation, and only
1475indentation, to specify block structure. Hence the indentation supplied
1476automatically by Python-mode is just an educated guess: only you know
1477the block structure you intend, so only you can supply correct
1478indentation.
1479
1480The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1481the indentation of preceding statements. E.g., assuming
1482py-indent-offset is 4, after you enter
1483\tif a > 0: \\[py-newline-and-indent]
1484the cursor will be moved to the position of the `_' (_ is not a
1485character in the file, it's just used here to indicate the location of
1486the cursor):
1487\tif a > 0:
1488\t _
1489If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1490to
1491\tif a > 0:
1492\t c = d
1493\t _
1494Python-mode cannot know whether that's what you intended, or whether
1495\tif a > 0:
1496\t c = d
1497\t_
1498was your intent. In general, Python-mode either reproduces the
1499indentation of the (closest code or indenting-comment) preceding
1500statement, or adds an extra py-indent-offset blanks if the preceding
1501statement has `:' as its last significant (non-whitespace and non-
1502comment) character. If the suggested indentation is too much, use
1503\\[py-delete-char] to reduce it.
1504
1505Continuation lines are given extra indentation. If you don't like the
1506suggested indentation, change it to something you do like, and Python-
1507mode will strive to indent later lines of the statement in the same way.
1508
1509If a line is a continuation line by virtue of being in an unclosed
1510paren/bracket/brace structure (`list', for short), the suggested
1511indentation depends on whether the current line contains the first item
1512in the list. If it does, it's indented py-indent-offset columns beyond
1513the indentation of the line containing the open bracket. If you don't
1514like that, change it by hand. The remaining items in the list will mimic
1515whatever indentation you give to the first item.
1516
1517If a line is a continuation line because the line preceding it ends with
1518a backslash, the third and following lines of the statement inherit their
1519indentation from the line preceding them. The indentation of the second
1520line in the statement depends on the form of the first (base) line: if
1521the base line is an assignment statement with anything more interesting
1522than the backslash following the leftmost assigning `=', the second line
1523is indented two columns beyond that `='. Else it's indented to two
1524columns beyond the leftmost solid chunk of non-whitespace characters on
1525the base line.
1526
1527Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1528repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1529structure you intend.
1530%c:indent-for-tab-command
1531%c:py-newline-and-indent
1532%c:py-delete-char
1533
1534
1535The next function may be handy when editing code you didn't write:
1536%c:py-guess-indent-offset
1537
1538
1539The remaining `indent' functions apply to a region of Python code. They
1540assume the block structure (equals indentation, in Python) of the region
1541is correct, and alter the indentation in various ways while preserving
1542the block structure:
1543%c:py-indent-region
1544%c:py-shift-region-left
1545%c:py-shift-region-right
1546
1547@MARKING & MANIPULATING REGIONS OF CODE
1548
1549\\[py-mark-block]\t mark block of lines
1550\\[mark-python-def-or-class]\t mark smallest enclosing def
1551\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
1552\\[py-comment-region]\t comment out region of code
1553\\[universal-argument] \\[py-comment-region]\t uncomment region of code
1554%c:py-mark-block
1555%c:mark-python-def-or-class
1556%c:py-comment-region
1557
1558@MOVING POINT
1559
1560\\[py-previous-statement]\t move to statement preceding point
1561\\[py-next-statement]\t move to statement following point
1562\\[py-goto-block-up]\t move up to start of current block
1563\\[beginning-of-python-def-or-class]\t move to start of def
1564\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1565\\[end-of-python-def-or-class]\t move to end of def
1566\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1567
1568The first two move to one statement beyond the statement that contains
1569point. A numeric prefix argument tells them to move that many
1570statements instead. Blank lines, comment lines, and continuation lines
1571do not count as `statements' for these commands. So, e.g., you can go
1572to the first code statement in a file by entering
1573\t\\[beginning-of-buffer]\t to move to the top of the file
1574\t\\[py-next-statement]\t to skip over initial comments and blank lines
1575Or do `\\[py-previous-statement]' with a huge prefix argument.
1576%c:py-previous-statement
1577%c:py-next-statement
1578%c:py-goto-block-up
1579%c:beginning-of-python-def-or-class
1580%c:end-of-python-def-or-class
1581
1582@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
1583
1584`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
1585
1586`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
1587overall class and def structure of a module.
1588
1589`\\[back-to-indentation]' moves point to a line's first non-blank character.
1590
1591`\\[indent-relative]' is handy for creating odd indentation.
1592
1593@OTHER EMACS HINTS
1594
1595If you don't like the default value of a variable, change its value to
1596whatever you do like by putting a `setq' line in your .emacs file.
1597E.g., to set the indentation increment to 4, put this line in your
1598.emacs:
1599\t(setq py-indent-offset 4)
1600To see the value of a variable, do `\\[describe-variable]' and enter the variable
1601name at the prompt.
1602
1603When entering a key sequence like `C-c C-n', it is not necessary to
1604release the CONTROL key after doing the `C-c' part -- it suffices to
1605press the CONTROL key, press and release `c' (while still holding down
1606CONTROL), press and release `n' (while still holding down CONTROL), &
1607then release CONTROL.
1608
1609Entering Python mode calls with no arguments the value of the variable
1610`python-mode-hook', if that value exists and is not nil; for backward
1611compatibility it also tries `py-mode-hook'; see the `Hooks' section of
1612the Elisp manual for details.
1613
1614Obscure: When python-mode is first loaded, it looks for all bindings
1615to newline-and-indent in the global keymap, and shadows them with
1616local bindings to py-newline-and-indent."))
1617
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001618
1619;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001620(defvar py-parse-state-re
1621 (concat
1622 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
1623 "\\|"
1624 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001625
Barry Warsaw7ae77681994-12-12 20:38:05 +00001626;; returns the parse state at point (see parse-partial-sexp docs)
1627(defun py-parse-state ()
1628 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001629 (let ((here (point)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001630 ;; back up to the first preceding line (if any; else start of
1631 ;; buffer) that begins with a popular Python keyword, or a non-
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001632 ;; whitespace and non-comment character. These are good places
1633 ;; to start parsing to see whether where we started is at a
1634 ;; non-zero nesting level. It may be slow for people who write
1635 ;; huge code blocks or huge lists ... tough beans.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001636 (re-search-backward py-parse-state-re nil 'move)
1637 (beginning-of-line)
1638 (parse-partial-sexp (point) here))))
1639
1640;; if point is at a non-zero nesting level, returns the number of the
1641;; character that opens the smallest enclosing unclosed list; else
1642;; returns nil.
1643(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001644 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001645 (if (zerop (car status))
1646 nil ; not in a nest
1647 (car (cdr status))))) ; char# of open bracket
1648
1649;; t iff preceding line ends with backslash that's not in a comment
1650(defun py-backslash-continuation-line-p ()
1651 (save-excursion
1652 (beginning-of-line)
1653 (and
1654 ;; use a cheap test first to avoid the regexp if possible
1655 ;; use 'eq' because char-after may return nil
1656 (eq (char-after (- (point) 2)) ?\\ )
1657 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001658 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001659 (looking-at py-continued-re))))
1660
1661;; t iff current line is a continuation line
1662(defun py-continuation-line-p ()
1663 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001664 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001665 (or (py-backslash-continuation-line-p)
1666 (py-nesting-level))))
1667
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001668;; go to initial line of current statement; usually this is the line
1669;; we're on, but if we're on the 2nd or following lines of a
1670;; continuation block, we need to go up to the first line of the
1671;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001672;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001673;; Tricky: We want to avoid quadratic-time behavior for long continued
1674;; blocks, whether of the backslash or open-bracket varieties, or a
1675;; mix of the two. The following manages to do that in the usual
1676;; cases.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001677(defun py-goto-initial-line ()
1678 (let ( open-bracket-pos )
1679 (while (py-continuation-line-p)
1680 (beginning-of-line)
1681 (if (py-backslash-continuation-line-p)
1682 (while (py-backslash-continuation-line-p)
1683 (forward-line -1))
1684 ;; else zip out of nested brackets/braces/parens
1685 (while (setq open-bracket-pos (py-nesting-level))
1686 (goto-char open-bracket-pos)))))
1687 (beginning-of-line))
1688
1689;; go to point right beyond final line of current statement; usually
1690;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001691;; statement we need to skip over the continuation lines. Tricky:
1692;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001693(defun py-goto-beyond-final-line ()
1694 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001695 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001696 (while (and (py-continuation-line-p)
1697 (not (eobp)))
1698 ;; skip over the backslash flavor
1699 (while (and (py-backslash-continuation-line-p)
1700 (not (eobp)))
1701 (forward-line 1))
1702 ;; if in nest, zip to the end of the nest
1703 (setq state (py-parse-state))
1704 (if (and (not (zerop (car state)))
1705 (not (eobp)))
1706 (progn
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001707 ;; BUG ALERT: I could swear, from reading the docs, that
Barry Warsaw7ae77681994-12-12 20:38:05 +00001708 ;; the 3rd argument should be plain 0
1709 (parse-partial-sexp (point) (point-max) (- 0 (car state))
1710 nil state)
1711 (forward-line 1))))))
1712
1713;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001714;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00001715(defun py-statement-opens-block-p ()
1716 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001717 (let ((start (point))
1718 (finish (progn (py-goto-beyond-final-line) (1- (point))))
1719 (searching t)
1720 (answer nil)
1721 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001722 (goto-char start)
1723 (while searching
1724 ;; look for a colon with nothing after it except whitespace, and
1725 ;; maybe a comment
1726 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
1727 finish t)
1728 (if (eq (point) finish) ; note: no `else' clause; just
1729 ; keep searching if we're not at
1730 ; the end yet
1731 ;; sure looks like it opens a block -- but it might
1732 ;; be in a comment
1733 (progn
1734 (setq searching nil) ; search is done either way
1735 (setq state (parse-partial-sexp start
1736 (match-beginning 0)))
1737 (setq answer (not (nth 4 state)))))
1738 ;; search failed: couldn't find another interesting colon
1739 (setq searching nil)))
1740 answer)))
1741
1742;; go to point right beyond final line of block begun by the current
1743;; line. This is the same as where py-goto-beyond-final-line goes
1744;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001745;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00001746(defun py-goto-beyond-block ()
1747 (if (py-statement-opens-block-p)
1748 (py-mark-block nil 'just-move)
1749 (py-goto-beyond-final-line)))
1750
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001751;; go to start of first statement (not blank or comment or
1752;; continuation line) at or preceding point. returns t if there is
1753;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00001754(defun py-goto-statement-at-or-above ()
1755 (py-goto-initial-line)
1756 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001757 ;; skip back over blank & comment lines
1758 ;; note: will skip a blank or comment line that happens to be
1759 ;; a continuation line too
1760 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
1761 (progn (py-goto-initial-line) t)
1762 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001763 t))
1764
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001765;; go to start of first statement (not blank or comment or
1766;; continuation line) following the statement containing point returns
1767;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00001768(defun py-goto-statement-below ()
1769 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001770 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001771 (py-goto-beyond-final-line)
1772 (while (and
1773 (looking-at py-blank-or-comment-re)
1774 (not (eobp)))
1775 (forward-line 1))
1776 (if (eobp)
1777 (progn (goto-char start) nil)
1778 t)))
1779
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001780;; go to start of statement, at or preceding point, starting with
1781;; keyword KEY. Skips blank lines and non-indenting comments upward
1782;; first. If that statement starts with KEY, done, else go back to
1783;; first enclosing block starting with KEY. If successful, leaves
1784;; point at the start of the KEY line & returns t. Else leaves point
1785;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001786(defun py-go-up-tree-to-keyword (key)
1787 ;; skip blanks and non-indenting #
1788 (py-goto-initial-line)
1789 (while (and
1790 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1791 (zerop (forward-line -1))) ; go back
1792 nil)
1793 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001794 (let* ((re (concat "[ \t]*" key "\\b"))
1795 (case-fold-search nil) ; let* so looking-at sees this
1796 (found (looking-at re))
1797 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001798 (while (not (or found dead))
1799 (condition-case nil ; in case no enclosing block
1800 (py-goto-block-up 'no-mark)
1801 (error (setq dead t)))
1802 (or dead (setq found (looking-at re))))
1803 (beginning-of-line)
1804 found))
1805
1806;; return string in buffer from start of indentation to end of line;
1807;; prefix "..." if leading whitespace was skipped
1808(defun py-suck-up-leading-text ()
1809 (save-excursion
1810 (back-to-indentation)
1811 (concat
1812 (if (bolp) "" "...")
1813 (buffer-substring (point) (progn (end-of-line) (point))))))
1814
1815;; assuming point at bolp, return first keyword ([a-z]+) on the line,
1816;; as a Lisp symbol; return nil if none
1817(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001818 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001819 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
1820 (intern (buffer-substring (match-beginning 1) (match-end 1)))
1821 nil)))
1822
1823(defun py-make-temp-name ()
1824 (make-temp-name
1825 (concat (file-name-as-directory py-temp-directory) "python")))
1826
1827(defun py-delete-file-silently (fname)
1828 (condition-case nil
1829 (delete-file fname)
1830 (error nil)))
1831
1832(defun py-kill-emacs-hook ()
1833 ;; delete our temp files
1834 (while py-file-queue
1835 (py-delete-file-silently (car py-file-queue))
1836 (setq py-file-queue (cdr py-file-queue)))
1837 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
1838 ;; run the hook we inherited, if any
1839 (and py-inherited-kill-emacs-hook
1840 (funcall py-inherited-kill-emacs-hook))))
1841
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001842;; make PROCESS's buffer visible, append STRING to it, and force
1843;; display; also make shell-mode believe the user typed this string,
1844;; so that kill-output-from-shell and show-output-from-shell work
1845;; "right"
Barry Warsaw7ae77681994-12-12 20:38:05 +00001846(defun py-append-to-process-buffer (process string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001847 (let ((cbuf (current-buffer))
1848 (pbuf (process-buffer process))
1849 (py-scroll-process-buffer t))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001850 (set-buffer pbuf)
1851 (goto-char (point-max))
1852 (move-marker (process-mark process) (point))
1853 (if (not py-this-is-emacs-19-p)
1854 (move-marker last-input-start (point))) ; muck w/ shell-mode
1855 (funcall (process-filter process) process string)
1856 (if (not py-this-is-emacs-19-p)
1857 (move-marker last-input-end (point))) ; muck w/ shell-mode
1858 (set-buffer cbuf))
1859 (sit-for 0))
1860
Barry Warsaw74d9cc51995-03-08 22:05:16 +00001861(defun py-keep-region-active ()
1862 ;; do whatever is necessary to keep the region active in XEmacs.
1863 ;; Ignore byte-compiler warnings you might see. Also note that
1864 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
1865 ;; require us to take explicit action.
1866 (and (boundp 'zmacs-region-stays)
1867 (setq zmacs-region-stays t)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001868
1869
Barry Warsaw850437a1995-03-08 21:50:28 +00001870(defconst py-version "$Revision$"
1871 "`python-mode' version number.")
1872(defconst py-help-address "bwarsaw@cnri.reston.va.us"
1873 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001874
Barry Warsaw850437a1995-03-08 21:50:28 +00001875(defun py-version ()
1876 "Echo the current version of `python-mode' in the minibuffer."
1877 (interactive)
1878 (message "Using `python-mode' version %s" py-version)
1879 (py-keep-region-active))
1880
1881;; only works under Emacs 19
1882;(eval-when-compile
1883; (require 'reporter))
1884
1885(defun py-submit-bug-report (enhancement-p)
1886 "Submit via mail a bug report on `python-mode'.
1887With \\[universal-argument] just submit an enhancement request."
1888 (interactive
1889 (list (not (y-or-n-p
1890 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00001891 (let ((reporter-prompt-for-summary-p (if enhancement-p
1892 "(Very) brief summary: "
1893 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00001894 (require 'reporter)
1895 (reporter-submit-bug-report
1896 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00001897 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00001898 ;; varlist
1899 (if enhancement-p nil
1900 '(py-python-command
1901 py-indent-offset
1902 py-block-comment-prefix
1903 py-scroll-process-buffer
1904 py-temp-directory
1905 py-beep-if-tab-change))
1906 nil ;pre-hooks
1907 nil ;post-hooks
1908 "Dear Barry,") ;salutation
1909 (if enhancement-p nil
1910 (set-mark (point))
1911 (insert
1912"Please replace this text with a sufficiently large code sample\n\
1913and an exact recipe so that I can reproduce your problem. Failure\n\
1914to do so may mean a greater delay in fixing your bug.\n\n")
1915 (exchange-point-and-mark)
1916 (py-keep-region-active))))
1917
1918
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001919;; arrange to kill temp files when Emacs exists
1920(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
1921 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
1922 ;; have to trust that other people are as respectful of our hook
1923 ;; fiddling as we are of theirs
1924 (if (boundp 'py-inherited-kill-emacs-hook)
1925 ;; we were loaded before -- trust others not to have screwed us
1926 ;; in the meantime (no choice, really)
1927 nil
1928 ;; else arrange for our hook to run theirs
1929 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
1930 (setq kill-emacs-hook 'py-kill-emacs-hook)))
1931
1932
1933
1934(provide 'python-mode)
1935;;; python-mode.el ends here