blob: 619f3f8352fcec865b7607149183a4a65e42f719 [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
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
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))
436 (goto-char here)
437 (beginning-of-line)
438 (delete-horizontal-space)
439 (indent-to (- indent outdent))
Barry Warsawb91b7431995-03-14 15:55:20 +0000440 )))
441
442
Barry Warsaw7ae77681994-12-12 20:38:05 +0000443;;; Functions that execute Python commands in a subprocess
Barry Warsaw7ae77681994-12-12 20:38:05 +0000444(defun py-shell ()
445 "Start an interactive Python interpreter in another window.
446This is like Shell mode, except that Python is running in the window
447instead of a shell. See the `Interactive Shell' and `Shell Mode'
448sections of the Emacs manual for details, especially for the key
449bindings active in the `*Python*' buffer.
450
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000451See the docs for variable `py-scroll-buffer' for info on scrolling
Barry Warsaw7ae77681994-12-12 20:38:05 +0000452behavior in the process window.
453
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000454Warning: Don't use an interactive Python if you change sys.ps1 or
455sys.ps2 from their default values, or if you're running code that
456prints `>>> ' or `... ' at the start of a line. `python-mode' can't
457distinguish your output from Python's output, and assumes that `>>> '
458at the start of a line is a prompt from Python. Similarly, the Emacs
459Shell mode code assumes that both `>>> ' and `... ' at the start of a
460line are Python prompts. Bad things can happen if you fool either
461mode.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000462
463Warning: If you do any editing *in* the process buffer *while* the
464buffer is accepting output from Python, do NOT attempt to `undo' the
465changes. Some of the output (nowhere near the parts you changed!) may
466be lost if you do. This appears to be an Emacs bug, an unfortunate
467interaction between undo and process filters; the same problem exists in
468non-Python process buffers using the default (Emacs-supplied) process
469filter."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000470 ;; BAW - should undo be disabled in the python process buffer, if
471 ;; this bug still exists?
Barry Warsaw7ae77681994-12-12 20:38:05 +0000472 (interactive)
473 (if py-this-is-emacs-19-p
474 (progn
475 (require 'comint)
476 (switch-to-buffer-other-window
477 (make-comint "Python" py-python-command)))
478 (progn
479 (require 'shell)
480 (switch-to-buffer-other-window
481 (make-shell "Python" py-python-command))))
482 (make-local-variable 'shell-prompt-pattern)
483 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
484 (set-process-filter (get-buffer-process (current-buffer))
485 'py-process-filter)
486 (set-syntax-table py-mode-syntax-table))
487
488(defun py-execute-region (start end)
489 "Send the region between START and END to a Python interpreter.
490If there is a *Python* process it is used.
491
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000492Hint: If you want to execute part of a Python file several times
493\(e.g., perhaps you're developing a function and want to flesh it out
494a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
495the region of interest, and send the code to a *Python* process via
496`\\[py-execute-buffer]' instead.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000497
498Following are subtleties to note when using a *Python* process:
499
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000500If a *Python* process is used, the region is copied into a temporary
501file (in directory `py-temp-directory'), and an `execfile' command is
502sent to Python naming that file. If you send regions faster than
503Python can execute them, `python-mode' will save them into distinct
504temp files, and execute the next one in the queue the next time it
505sees a `>>> ' prompt from Python. Each time this happens, the process
506buffer is popped into a window (if it's not already in some window) so
507you can see it, and a comment of the form
Barry Warsaw7ae77681994-12-12 20:38:05 +0000508
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000509 \t## working on region in file <name> ...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000510
511is inserted at the end.
512
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000513Caution: No more than 26 regions can be pending at any given time.
514This limit is (indirectly) inherited from libc's mktemp(3).
515`python-mode' does not try to protect you from exceeding the limit.
516It's extremely unlikely that you'll get anywhere close to the limit in
517practice, unless you're trying to be a jerk <grin>.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000518
519See the `\\[py-shell]' docs for additional warnings."
520 (interactive "r")
521 (or (< start end) (error "Region is empty"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000522 (let ((pyproc (get-process "Python"))
523 fname)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000524 (if (null pyproc)
525 (shell-command-on-region start end py-python-command)
526 ;; else feed it thru a temp file
527 (setq fname (py-make-temp-name))
528 (write-region start end fname nil 'no-msg)
529 (setq py-file-queue (append py-file-queue (list fname)))
530 (if (cdr py-file-queue)
531 (message "File %s queued for execution" fname)
532 ;; else
533 (py-execute-file pyproc fname)))))
534
535(defun py-execute-file (pyproc fname)
536 (py-append-to-process-buffer
537 pyproc
538 (format "## working on region in file %s ...\n" fname))
539 (process-send-string pyproc (format "execfile('%s')\n" fname)))
540
541(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000542 (let ((curbuf (current-buffer))
543 (pbuf (process-buffer pyproc))
544 (pmark (process-mark pyproc))
545 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000546
547 ;; make sure we switch to a different buffer at least once. if we
548 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000549 ;; window, and point is before the end, and lots of output is
550 ;; coming at a fast pace, then (a) simple cursor-movement commands
551 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
552 ;; to have a visible effect (the window just doesn't get updated,
553 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
554 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000555 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000556 ;; #b makes no sense to me at all. #a almost makes sense: unless
557 ;; we actually change buffers, set_buffer_internal in buffer.c
558 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
559 ;; seems to make the Emacs command loop reluctant to update the
560 ;; display. Perhaps the default process filter in process.c's
561 ;; read_process_output has update_mode_lines++ for a similar
562 ;; reason? beats me ...
563
564 ;; BAW - we want to check to see if this still applies
Barry Warsaw7ae77681994-12-12 20:38:05 +0000565 (if (eq curbuf pbuf) ; mysterious ugly hack
566 (set-buffer (get-buffer-create "*scratch*")))
567
568 (set-buffer pbuf)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000569 (let* ((start (point))
570 (goback (< start pmark))
571 (buffer-read-only nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000572 (goto-char pmark)
573 (insert string)
574 (move-marker pmark (point))
575 (setq file-finished
576 (and py-file-queue
577 (equal ">>> "
578 (buffer-substring
579 (prog2 (beginning-of-line) (point)
580 (goto-char pmark))
581 (point)))))
582 (if goback (goto-char start)
583 ;; else
584 (if py-scroll-process-buffer
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000585 (let* ((pop-up-windows t)
586 (pwin (display-buffer pbuf)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000587 (set-window-point pwin (point))))))
588 (set-buffer curbuf)
589 (if file-finished
590 (progn
591 (py-delete-file-silently (car py-file-queue))
592 (setq py-file-queue (cdr py-file-queue))
593 (if py-file-queue
594 (py-execute-file pyproc (car py-file-queue)))))))
595
596(defun py-execute-buffer ()
597 "Send the contents of the buffer to a Python interpreter.
598If there is a *Python* process buffer it is used. If a clipping
599restriction is in effect, only the accessible portion of the buffer is
600sent. A trailing newline will be supplied if needed.
601
602See the `\\[py-execute-region]' docs for an account of some subtleties."
603 (interactive)
604 (py-execute-region (point-min) (point-max)))
605
606
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000607
608;; Functions for Python style indentation
Barry Warsaw7ae77681994-12-12 20:38:05 +0000609(defun py-delete-char ()
610 "Reduce indentation or delete character.
611If point is at the leftmost column, deletes the preceding newline.
612
613Else if point is at the leftmost non-blank character of a line that is
614neither a continuation line nor a non-indenting comment line, or if
615point is at the end of a blank line, reduces the indentation to match
616that of the line that opened the current block of code. The line that
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000617opened the block is displayed in the echo area to help you keep track
618of where you are.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000619
620Else the preceding character is deleted, converting a tab to spaces if
621needed so that only a single column position is deleted."
622 (interactive "*")
623 (if (or (/= (current-indentation) (current-column))
624 (bolp)
625 (py-continuation-line-p)
626 (looking-at "#[^ \t\n]")) ; non-indenting #
627 (backward-delete-char-untabify 1)
628 ;; else indent the same as the colon line that opened the block
629
630 ;; force non-blank so py-goto-block-up doesn't ignore it
631 (insert-char ?* 1)
632 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000633 (let ((base-indent 0) ; indentation of base line
634 (base-text "") ; and text of base line
635 (base-found-p nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000636 (condition-case nil ; in case no enclosing block
637 (save-excursion
638 (py-goto-block-up 'no-mark)
639 (setq base-indent (current-indentation)
640 base-text (py-suck-up-leading-text)
641 base-found-p t))
642 (error nil))
643 (delete-char 1) ; toss the dummy character
644 (delete-horizontal-space)
645 (indent-to base-indent)
646 (if base-found-p
647 (message "Closes block: %s" base-text)))))
648
Barry Warsawfc8a01f1995-03-09 16:07:29 +0000649;; required for pending-del and delsel modes
650(put 'py-delete-char 'delete-selection 'supersede)
651(put 'py-delete-char 'pending-delete 'supersede)
652
Barry Warsaw7ae77681994-12-12 20:38:05 +0000653(defun py-indent-line ()
654 "Fix the indentation of the current line according to Python rules."
655 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000656 (let* ((ci (current-indentation))
657 (move-to-indentation-p (<= (current-column) ci))
Barry Warsawb86bbad1995-03-14 15:56:35 +0000658 (need (py-compute-indentation)))
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000659 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000660 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000661 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000662 (if (/= ci need)
663 (save-excursion
664 (beginning-of-line)
665 (delete-horizontal-space)
666 (indent-to need)))
667 (if move-to-indentation-p (back-to-indentation))))
668
669(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000670 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000671This is just `strives to' because correct indentation can't be computed
672from scratch for Python code. In general, deletes the whitespace before
673point, inserts a newline, and takes an educated guess as to how you want
674the new line indented."
675 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000676 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000677 (if (< ci (current-column)) ; if point beyond indentation
678 (newline-and-indent)
679 ;; else try to act like newline-and-indent "normally" acts
680 (beginning-of-line)
681 (insert-char ?\n 1)
682 (move-to-column ci))))
683
684(defun py-compute-indentation ()
685 (save-excursion
686 (beginning-of-line)
687 (cond
688 ;; are we on a continuation line?
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000689 ((py-continuation-line-p)
690 (let ((startpos (point))
691 (open-bracket-pos (py-nesting-level))
692 endpos searching found)
693 (if open-bracket-pos
694 (progn
695 ;; align with first item in list; else a normal
696 ;; indent beyond the line with the open bracket
697 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
698 ;; is the first list item on the same line?
699 (skip-chars-forward " \t")
700 (if (null (memq (following-char) '(?\n ?# ?\\)))
701 ; yes, so line up with it
702 (current-column)
703 ;; first list item on another line, or doesn't exist yet
704 (forward-line 1)
705 (while (and (< (point) startpos)
706 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
707 (forward-line 1))
708 (if (< (point) startpos)
709 ;; again mimic the first list item
710 (current-indentation)
711 ;; else they're about to enter the first item
712 (goto-char open-bracket-pos)
713 (+ (current-indentation) py-indent-offset))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000714
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000715 ;; else on backslash continuation line
716 (forward-line -1)
717 (if (py-continuation-line-p) ; on at least 3rd line in block
718 (current-indentation) ; so just continue the pattern
719 ;; else started on 2nd line in block, so indent more.
720 ;; if base line is an assignment with a start on a RHS,
721 ;; indent to 2 beyond the leftmost "="; else skip first
722 ;; chunk of non-whitespace characters on base line, + 1 more
723 ;; column
724 (end-of-line)
725 (setq endpos (point) searching t)
726 (back-to-indentation)
727 (setq startpos (point))
728 ;; look at all "=" from left to right, stopping at first
729 ;; one not nested in a list or string
730 (while searching
731 (skip-chars-forward "^=" endpos)
732 (if (= (point) endpos)
733 (setq searching nil)
734 (forward-char 1)
735 (setq state (parse-partial-sexp startpos (point)))
736 (if (and (zerop (car state)) ; not in a bracket
737 (null (nth 3 state))) ; & not in a string
738 (progn
739 (setq searching nil) ; done searching in any case
740 (setq found
741 (not (or
742 (eq (following-char) ?=)
743 (memq (char-after (- (point) 2))
744 '(?< ?> ?!)))))))))
745 (if (or (not found) ; not an assignment
746 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
747 (progn
748 (goto-char startpos)
749 (skip-chars-forward "^ \t\n")))
750 (1+ (current-column))))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000751
752 ;; not on a continuation line
753
754 ;; if at start of restriction, or on a non-indenting comment line,
755 ;; assume they intended whatever's there
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000756 ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
757 (current-indentation))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000758
759 ;; else indentation based on that of the statement that precedes
760 ;; us; use the first line of that statement to establish the base,
761 ;; in case the user forced a non-std indentation for the
762 ;; continuation lines (if any)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000763 (t
764 ;; skip back over blank & non-indenting comment lines
765 ;; note: will skip a blank or non-indenting comment line that
766 ;; happens to be a continuation line too
767 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
768 nil 'move)
769 ;; if we landed inside a string, go to the beginning of that
770 ;; string. this handles triple quoted, multi-line spanning
771 ;; strings.
772 (let ((state (parse-partial-sexp
773 (save-excursion (beginning-of-python-def-or-class)
774 (point))
775 (point))))
776 (if (nth 3 state)
777 (goto-char (nth 2 state))))
778 (py-goto-initial-line)
779 (if (py-statement-opens-block-p)
780 (+ (current-indentation) py-indent-offset)
781 (current-indentation))))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000782
783(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000784 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000785By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000786`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +0000787Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000788`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +0000789their own buffer-local copy), both those currently existing and those
790created later in the Emacs session.
791
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000792Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000793There's no excuse for such foolishness, but sometimes you have to deal
794with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000795`py-indent-offset' to what it thinks it was when they created the
796mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000797
798Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000799looking for a line that opens a block of code. `py-indent-offset' is
800set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +0000801statement following it. If the search doesn't succeed going forward,
802it's tried again going backward."
803 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000804 (let (new-value
805 (start (point))
806 restart
807 (found nil)
808 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000809 (py-goto-initial-line)
810 (while (not (or found (eobp)))
811 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
812 (progn
813 (setq restart (point))
814 (py-goto-initial-line)
815 (if (py-statement-opens-block-p)
816 (setq found t)
817 (goto-char restart)))))
818 (if found
819 ()
820 (goto-char start)
821 (py-goto-initial-line)
822 (while (not (or found (bobp)))
823 (setq found
824 (and
825 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
826 (or (py-goto-initial-line) t) ; always true -- side effect
827 (py-statement-opens-block-p)))))
828 (setq colon-indent (current-indentation)
829 found (and found (zerop (py-next-statement 1)))
830 new-value (- (current-indentation) colon-indent))
831 (goto-char start)
832 (if found
833 (progn
834 (funcall (if global 'kill-local-variable 'make-local-variable)
835 'py-indent-offset)
836 (setq py-indent-offset new-value)
837 (message "%s value of py-indent-offset set to %d"
838 (if global "Global" "Local")
839 py-indent-offset))
840 (error "Sorry, couldn't guess a value for py-indent-offset"))))
841
842(defun py-shift-region (start end count)
843 (save-excursion
844 (goto-char end) (beginning-of-line) (setq end (point))
845 (goto-char start) (beginning-of-line) (setq start (point))
846 (indent-rigidly start end count)))
847
848(defun py-shift-region-left (start end &optional count)
849 "Shift region of Python code to the left.
850The lines from the line containing the start of the current region up
851to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000852shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000853
854If a prefix argument is given, the region is instead shifted by that
855many columns."
856 (interactive "*r\nP") ; region; raw prefix arg
857 (py-shift-region start end
858 (- (prefix-numeric-value
859 (or count py-indent-offset)))))
860
861(defun py-shift-region-right (start end &optional count)
862 "Shift region of Python code to the right.
863The lines from the line containing the start of the current region up
864to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000865shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000866
867If a prefix argument is given, the region is instead shifted by that
868many columns."
869 (interactive "*r\nP") ; region; raw prefix arg
870 (py-shift-region start end (prefix-numeric-value
871 (or count py-indent-offset))))
872
873(defun py-indent-region (start end &optional indent-offset)
874 "Reindent a region of Python code.
875The lines from the line containing the start of the current region up
876to (but not including) the line containing the end of the region are
877reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000878character in the first column, the first line is left alone and the
879rest of the region is reindented with respect to it. Else the entire
880region is reindented with respect to the (closest code or
881indenting-comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000882
883This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000884control structures are introduced or removed, or to reformat code
885using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000886
887If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000888the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +0000889used.
890
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000891Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +0000892is called! This function does not compute proper indentation from
893scratch (that's impossible in Python), it merely adjusts the existing
894indentation to be correct in context.
895
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000896Warning: This function really has no idea what to do with
897non-indenting comment lines, and shifts them as if they were indenting
898comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000899
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000900Special cases: whitespace is deleted from blank lines; continuation
901lines are shifted by the same amount their initial line was shifted,
902in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +0000903initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000904 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +0000905 (save-excursion
906 (goto-char end) (beginning-of-line) (setq end (point-marker))
907 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000908 (let ((py-indent-offset (prefix-numeric-value
909 (or indent-offset py-indent-offset)))
910 (indents '(-1)) ; stack of active indent levels
911 (target-column 0) ; column to which to indent
912 (base-shifted-by 0) ; amount last base line was shifted
913 (indent-base (if (looking-at "[ \t\n]")
914 (py-compute-indentation)
915 0))
916 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000917 (while (< (point) end)
918 (setq ci (current-indentation))
919 ;; figure out appropriate target column
920 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000921 ((or (eq (following-char) ?#) ; comment in column 1
922 (looking-at "[ \t]*$")) ; entirely blank
923 (setq target-column 0))
924 ((py-continuation-line-p) ; shift relative to base line
925 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000926 (t ; new base line
927 (if (> ci (car indents)) ; going deeper; push it
928 (setq indents (cons ci indents))
929 ;; else we should have seen this indent before
930 (setq indents (memq ci indents)) ; pop deeper indents
931 (if (null indents)
932 (error "Bad indentation in region, at line %d"
933 (save-restriction
934 (widen)
935 (1+ (count-lines 1 (point)))))))
936 (setq target-column (+ indent-base
937 (* py-indent-offset
938 (- (length indents) 2))))
939 (setq base-shifted-by (- target-column ci))))
940 ;; shift as needed
941 (if (/= ci target-column)
942 (progn
943 (delete-horizontal-space)
944 (indent-to target-column)))
945 (forward-line 1))))
946 (set-marker end nil))
947
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000948
949;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +0000950(defun py-previous-statement (count)
951 "Go to the start of previous Python statement.
952If the statement at point is the i'th Python statement, goes to the
953start of statement i-COUNT. If there is no such statement, goes to the
954first statement. Returns count of statements left to move.
955`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000956 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +0000957 (if (< count 0) (py-next-statement (- count))
958 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000959 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000960 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000961 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +0000962 (> count 0)
963 (zerop (forward-line -1))
964 (py-goto-statement-at-or-above))
965 (setq count (1- count)))
966 (if (> count 0) (goto-char start)))
967 count))
968
969(defun py-next-statement (count)
970 "Go to the start of next Python statement.
971If the statement at point is the i'th Python statement, goes to the
972start of statement i+COUNT. If there is no such statement, goes to the
973last statement. Returns count of statements left to move. `Statements'
974do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000975 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +0000976 (if (< count 0) (py-previous-statement (- count))
977 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000978 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000979 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000980 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +0000981 (> count 0)
982 (py-goto-statement-below))
983 (setq count (1- count)))
984 (if (> count 0) (goto-char start)))
985 count))
986
987(defun py-goto-block-up (&optional nomark)
988 "Move up to start of current block.
989Go to the statement that starts the smallest enclosing block; roughly
990speaking, this will be the closest preceding statement that ends with a
991colon and is indented less than the statement you started on. If
992successful, also sets the mark to the starting point.
993
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000994`\\[py-mark-block]' can be used afterward to mark the whole code
995block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000996
997If called from a program, the mark will not be set if optional argument
998NOMARK is not nil."
999 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001000 (let ((start (point))
1001 (found nil)
1002 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001003 (py-goto-initial-line)
1004 ;; if on blank or non-indenting comment line, use the preceding stmt
1005 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1006 (progn
1007 (py-goto-statement-at-or-above)
1008 (setq found (py-statement-opens-block-p))))
1009 ;; search back for colon line indented less
1010 (setq initial-indent (current-indentation))
1011 (if (zerop initial-indent)
1012 ;; force fast exit
1013 (goto-char (point-min)))
1014 (while (not (or found (bobp)))
1015 (setq found
1016 (and
1017 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1018 (or (py-goto-initial-line) t) ; always true -- side effect
1019 (< (current-indentation) initial-indent)
1020 (py-statement-opens-block-p))))
1021 (if found
1022 (progn
1023 (or nomark (push-mark start))
1024 (back-to-indentation))
1025 (goto-char start)
1026 (error "Enclosing block not found"))))
1027
1028(defun beginning-of-python-def-or-class (&optional class)
1029 "Move point to start of def (or class, with prefix arg).
1030
1031Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001032arg, looks for a `class' instead. The docs assume the `def' case;
1033just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001034
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001035If point is in a def statement already, and after the `d', simply
1036moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001037
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001038Else (point is not in a def statement, or at or before the `d' of a
1039def statement), searches for the closest preceding def statement, and
1040leaves point at its start. If no such statement can be found, leaves
1041point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001042
1043Returns t iff a def statement is found by these rules.
1044
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001045Note that doing this command repeatedly will take you closer to the
1046start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001047
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001048If you want to mark the current def/class, see
1049`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001050 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001051 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1052 (start-of-line (progn (beginning-of-line) (point)))
1053 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001054 (if (or (/= start-of-stmt start-of-line)
1055 (not at-or-before-p))
1056 (end-of-line)) ; OK to match on this line
1057 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001058 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001059
1060(defun end-of-python-def-or-class (&optional class)
1061 "Move point beyond end of def (or class, with prefix arg) body.
1062
1063By default, looks for an appropriate `def'. If you supply a prefix arg,
1064looks for a `class' instead. The docs assume the `def' case; just
1065substitute `class' for `def' for the other case.
1066
1067If point is in a def statement already, this is the def we use.
1068
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001069Else if the def found by `\\[beginning-of-python-def-or-class]'
1070contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001071
1072Else we search forward for the closest following def, and use that.
1073
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001074If a def can be found by these rules, point is moved to the start of
1075the line immediately following the def block, and the position of the
1076start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001077
1078Else point is moved to the end of the buffer, and nil is returned.
1079
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001080Note that doing this command repeatedly will take you closer to the
1081end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001082
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001083If you want to mark the current def/class, see
1084`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001085 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001086 (let ((start (progn (py-goto-initial-line) (point)))
1087 (which (if class "class" "def"))
1088 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001089 ;; move point to start of appropriate def/class
1090 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1091 (setq state 'at-beginning)
1092 ;; else see if beginning-of-python-def-or-class hits container
1093 (if (and (beginning-of-python-def-or-class class)
1094 (progn (py-goto-beyond-block)
1095 (> (point) start)))
1096 (setq state 'at-end)
1097 ;; else search forward
1098 (goto-char start)
1099 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1100 (progn (setq state 'at-beginning)
1101 (beginning-of-line)))))
1102 (cond
1103 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1104 ((eq state 'at-end) t)
1105 ((eq state 'not-found) nil)
1106 (t (error "internal error in end-of-python-def-or-class")))))
1107
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001108
1109;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001110(defun py-mark-block (&optional extend just-move)
1111 "Mark following block of lines. With prefix arg, mark structure.
1112Easier to use than explain. It sets the region to an `interesting'
1113block of succeeding lines. If point is on a blank line, it goes down to
1114the next non-blank line. That will be the start of the region. The end
1115of the region depends on the kind of line at the start:
1116
1117 - If a comment, the region will include all succeeding comment lines up
1118 to (but not including) the next non-comment line (if any).
1119
1120 - Else if a prefix arg is given, and the line begins one of these
1121 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001122
1123 if elif else try except finally for while def class
1124
Barry Warsaw7ae77681994-12-12 20:38:05 +00001125 the region will be set to the body of the structure, including
1126 following blocks that `belong' to it, but excluding trailing blank
1127 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001128 and all (if any) of the following `except' and `finally' blocks
1129 that belong to the `try' structure will be in the region. Ditto
1130 for if/elif/else, for/else and while/else structures, and (a bit
1131 degenerate, since they're always one-block structures) def and
1132 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001133
1134 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001135 block (see list above), and the block is not a `one-liner' (i.e.,
1136 the statement ends with a colon, not with code), the region will
1137 include all succeeding lines up to (but not including) the next
1138 code statement (if any) that's indented no more than the starting
1139 line, except that trailing blank and comment lines are excluded.
1140 E.g., if the starting line begins a multi-statement `def'
1141 structure, the region will be set to the full function definition,
1142 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001143
1144 - Else the region will include all succeeding lines up to (but not
1145 including) the next blank line, or code or indenting-comment line
1146 indented strictly less than the starting line. Trailing indenting
1147 comment lines are included in this case, but not trailing blank
1148 lines.
1149
1150A msg identifying the location of the mark is displayed in the echo
1151area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1152
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001153If called from a program, optional argument EXTEND plays the role of
1154the prefix arg, and if optional argument JUST-MOVE is not nil, just
1155moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001156 (interactive "P") ; raw prefix arg
1157 (py-goto-initial-line)
1158 ;; skip over blank lines
1159 (while (and
1160 (looking-at "[ \t]*$") ; while blank line
1161 (not (eobp))) ; & somewhere to go
1162 (forward-line 1))
1163 (if (eobp)
1164 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001165 (let ((initial-pos (point))
1166 (initial-indent (current-indentation))
1167 last-pos ; position of last stmt in region
1168 (followers
1169 '((if elif else) (elif elif else) (else)
1170 (try except finally) (except except) (finally)
1171 (for else) (while else)
1172 (def) (class) ) )
1173 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001174
1175 (cond
1176 ;; if comment line, suck up the following comment lines
1177 ((looking-at "[ \t]*#")
1178 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1179 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1180 (setq last-pos (point)))
1181
1182 ;; else if line is a block line and EXTEND given, suck up
1183 ;; the whole structure
1184 ((and extend
1185 (setq first-symbol (py-suck-up-first-keyword) )
1186 (assq first-symbol followers))
1187 (while (and
1188 (or (py-goto-beyond-block) t) ; side effect
1189 (forward-line -1) ; side effect
1190 (setq last-pos (point)) ; side effect
1191 (py-goto-statement-below)
1192 (= (current-indentation) initial-indent)
1193 (setq next-symbol (py-suck-up-first-keyword))
1194 (memq next-symbol (cdr (assq first-symbol followers))))
1195 (setq first-symbol next-symbol)))
1196
1197 ;; else if line *opens* a block, search for next stmt indented <=
1198 ((py-statement-opens-block-p)
1199 (while (and
1200 (setq last-pos (point)) ; always true -- side effect
1201 (py-goto-statement-below)
1202 (> (current-indentation) initial-indent))
1203 nil))
1204
1205 ;; else plain code line; stop at next blank line, or stmt or
1206 ;; indenting comment line indented <
1207 (t
1208 (while (and
1209 (setq last-pos (point)) ; always true -- side effect
1210 (or (py-goto-beyond-final-line) t)
1211 (not (looking-at "[ \t]*$")) ; stop at blank line
1212 (or
1213 (>= (current-indentation) initial-indent)
1214 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1215 nil)))
1216
1217 ;; skip to end of last stmt
1218 (goto-char last-pos)
1219 (py-goto-beyond-final-line)
1220
1221 ;; set mark & display
1222 (if just-move
1223 () ; just return
1224 (push-mark (point) 'no-msg)
1225 (forward-line -1)
1226 (message "Mark set after: %s" (py-suck-up-leading-text))
1227 (goto-char initial-pos))))
1228
1229(defun mark-python-def-or-class (&optional class)
1230 "Set region to body of def (or class, with prefix arg) enclosing point.
1231Pushes the current mark, then point, on the mark ring (all language
1232modes do this, but although it's handy it's never documented ...).
1233
1234In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001235hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1236`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001237
1238And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001239Turned out that was more confusing than useful: the `goto start' and
1240`goto end' commands are usually used to search through a file, and
1241people expect them to act a lot like `search backward' and `search
1242forward' string-search commands. But because Python `def' and `class'
1243can nest to arbitrary levels, finding the smallest def containing
1244point cannot be done via a simple backward search: the def containing
1245point may not be the closest preceding def, or even the closest
1246preceding def that's indented less. The fancy algorithm required is
1247appropriate for the usual uses of this `mark' command, but not for the
1248`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001249
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001250So the def marked by this command may not be the one either of the
1251`goto' commands find: If point is on a blank or non-indenting comment
1252line, moves back to start of the closest preceding code statement or
1253indenting comment line. If this is a `def' statement, that's the def
1254we use. Else searches for the smallest enclosing `def' block and uses
1255that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001256
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001257When an enclosing def is found: The mark is left immediately beyond
1258the last line of the def block. Point is left at the start of the
1259def, except that: if the def is preceded by a number of comment lines
1260followed by (at most) one optional blank line, point is left at the
1261start of the comments; else if the def is preceded by a blank line,
1262point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001263
1264The intent is to mark the containing def/class and its associated
1265documentation, to make moving and duplicating functions and classes
1266pleasant."
1267 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001268 (let ((start (point))
1269 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001270 (push-mark start)
1271 (if (not (py-go-up-tree-to-keyword which))
1272 (progn (goto-char start)
1273 (error "Enclosing %s not found" which))
1274 ;; else enclosing def/class found
1275 (setq start (point))
1276 (py-goto-beyond-block)
1277 (push-mark (point))
1278 (goto-char start)
1279 (if (zerop (forward-line -1)) ; if there is a preceding line
1280 (progn
1281 (if (looking-at "[ \t]*$") ; it's blank
1282 (setq start (point)) ; so reset start point
1283 (goto-char start)) ; else try again
1284 (if (zerop (forward-line -1))
1285 (if (looking-at "[ \t]*#") ; a comment
1286 ;; look back for non-comment line
1287 ;; tricky: note that the regexp matches a blank
1288 ;; line, cuz \n is in the 2nd character class
1289 (and
1290 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1291 (forward-line 1))
1292 ;; no comment, so go back
1293 (goto-char start))))))))
1294
1295(defun py-comment-region (start end &optional uncomment-p)
1296 "Comment out region of code; with prefix arg, uncomment region.
1297The lines from the line containing the start of the current region up
1298to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001299commented out, by inserting the string `py-block-comment-prefix' at
1300the start of each line. With a prefix arg, removes
1301`py-block-comment-prefix' from the start of each line instead."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001302 (interactive "*r\nP") ; region; raw prefix arg
1303 (goto-char end) (beginning-of-line) (setq end (point))
1304 (goto-char start) (beginning-of-line) (setq start (point))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001305 (let ((prefix-len (length py-block-comment-prefix)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001306 (save-excursion
1307 (save-restriction
1308 (narrow-to-region start end)
1309 (while (not (eobp))
1310 (if uncomment-p
1311 (and (string= py-block-comment-prefix
1312 (buffer-substring
1313 (point) (+ (point) prefix-len)))
1314 (delete-char prefix-len))
1315 (insert py-block-comment-prefix))
1316 (forward-line 1))))))
1317
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001318
1319;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001320
1321;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001322;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1323;; out of the right places, along with the keys they're on & current
1324;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00001325(defun py-dump-help-string (str)
1326 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001327 (let ((locals (buffer-local-variables))
1328 funckind funcname func funcdoc
1329 (start 0) mstart end
1330 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001331 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1332 (setq mstart (match-beginning 0) end (match-end 0)
1333 funckind (substring str (match-beginning 1) (match-end 1))
1334 funcname (substring str (match-beginning 2) (match-end 2))
1335 func (intern funcname))
1336 (princ (substitute-command-keys (substring str start mstart)))
1337 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001338 ((equal funckind "c") ; command
1339 (setq funcdoc (documentation func)
1340 keys (concat
1341 "Key(s): "
1342 (mapconcat 'key-description
1343 (where-is-internal func py-mode-map)
1344 ", "))))
1345 ((equal funckind "v") ; variable
1346 (setq funcdoc (substitute-command-keys
1347 (get func 'variable-documentation))
1348 keys (if (assq func locals)
1349 (concat
1350 "Local/Global values: "
1351 (prin1-to-string (symbol-value func))
1352 " / "
1353 (prin1-to-string (default-value func)))
1354 (concat
1355 "Value: "
1356 (prin1-to-string (symbol-value func))))))
1357 (t ; unexpected
1358 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001359 (princ (format "\n-> %s:\t%s\t%s\n\n"
1360 (if (equal funckind "c") "Command" "Variable")
1361 funcname keys))
1362 (princ funcdoc)
1363 (terpri)
1364 (setq start end))
1365 (princ (substitute-command-keys (substring str start))))
1366 (print-help-return-message)))
1367
1368(defun py-describe-mode ()
1369 "Dump long form of Python-mode docs."
1370 (interactive)
1371 (py-dump-help-string "Major mode for editing Python files.
1372Knows about Python indentation, tokens, comments and continuation lines.
1373Paragraphs are separated by blank lines only.
1374
1375Major sections below begin with the string `@'; specific function and
1376variable docs begin with `->'.
1377
1378@EXECUTING PYTHON CODE
1379
1380\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1381\\[py-execute-region]\tsends the current region
1382\\[py-shell]\tstarts a Python interpreter window; this will be used by
1383\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1384%c:py-execute-buffer
1385%c:py-execute-region
1386%c:py-shell
1387
1388@VARIABLES
1389
1390py-indent-offset\tindentation increment
1391py-block-comment-prefix\tcomment string used by py-comment-region
1392
1393py-python-command\tshell command to invoke Python interpreter
1394py-scroll-process-buffer\talways scroll Python process buffer
1395py-temp-directory\tdirectory used for temp files (if needed)
1396
1397py-beep-if-tab-change\tring the bell if tab-width is changed
1398%v:py-indent-offset
1399%v:py-block-comment-prefix
1400%v:py-python-command
1401%v:py-scroll-process-buffer
1402%v:py-temp-directory
1403%v:py-beep-if-tab-change
1404
1405@KINDS OF LINES
1406
1407Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001408preceding line ends with a backslash that's not part of a comment, or
1409the paren/bracket/brace nesting level at the start of the line is
1410non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001411
1412An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001413possibly blanks or tabs), a `comment line' (leftmost non-blank
1414character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001415
1416Comment Lines
1417
1418Although all comment lines are treated alike by Python, Python mode
1419recognizes two kinds that act differently with respect to indentation.
1420
1421An `indenting comment line' is a comment line with a blank, tab or
1422nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001423treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00001424indenting comment line will be indented like the comment line. All
1425other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001426following the initial `#') are `non-indenting comment lines', and
1427their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001428
1429Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001430whenever possible. Non-indenting comment lines are useful in cases
1431like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00001432
1433\ta = b # a very wordy single-line comment that ends up being
1434\t #... continued onto another line
1435
1436\tif a == b:
1437##\t\tprint 'panic!' # old code we've `commented out'
1438\t\treturn a
1439
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001440Since the `#...' and `##' comment lines have a non-whitespace
1441character following the initial `#', Python mode ignores them when
1442computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001443
1444Continuation Lines and Statements
1445
1446The Python-mode commands generally work on statements instead of on
1447individual lines, where a `statement' is a comment or blank line, or a
1448code line and all of its following continuation lines (if any)
1449considered as a single logical unit. The commands in this mode
1450generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001451statement containing point, even if point happens to be in the middle
1452of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001453
1454
1455@INDENTATION
1456
1457Primarily for entering new code:
1458\t\\[indent-for-tab-command]\t indent line appropriately
1459\t\\[py-newline-and-indent]\t insert newline, then indent
1460\t\\[py-delete-char]\t reduce indentation, or delete single character
1461
1462Primarily for reindenting existing code:
1463\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1464\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1465
1466\t\\[py-indent-region]\t reindent region to match its context
1467\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1468\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1469
1470Unlike most programming languages, Python uses indentation, and only
1471indentation, to specify block structure. Hence the indentation supplied
1472automatically by Python-mode is just an educated guess: only you know
1473the block structure you intend, so only you can supply correct
1474indentation.
1475
1476The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1477the indentation of preceding statements. E.g., assuming
1478py-indent-offset is 4, after you enter
1479\tif a > 0: \\[py-newline-and-indent]
1480the cursor will be moved to the position of the `_' (_ is not a
1481character in the file, it's just used here to indicate the location of
1482the cursor):
1483\tif a > 0:
1484\t _
1485If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1486to
1487\tif a > 0:
1488\t c = d
1489\t _
1490Python-mode cannot know whether that's what you intended, or whether
1491\tif a > 0:
1492\t c = d
1493\t_
1494was your intent. In general, Python-mode either reproduces the
1495indentation of the (closest code or indenting-comment) preceding
1496statement, or adds an extra py-indent-offset blanks if the preceding
1497statement has `:' as its last significant (non-whitespace and non-
1498comment) character. If the suggested indentation is too much, use
1499\\[py-delete-char] to reduce it.
1500
1501Continuation lines are given extra indentation. If you don't like the
1502suggested indentation, change it to something you do like, and Python-
1503mode will strive to indent later lines of the statement in the same way.
1504
1505If a line is a continuation line by virtue of being in an unclosed
1506paren/bracket/brace structure (`list', for short), the suggested
1507indentation depends on whether the current line contains the first item
1508in the list. If it does, it's indented py-indent-offset columns beyond
1509the indentation of the line containing the open bracket. If you don't
1510like that, change it by hand. The remaining items in the list will mimic
1511whatever indentation you give to the first item.
1512
1513If a line is a continuation line because the line preceding it ends with
1514a backslash, the third and following lines of the statement inherit their
1515indentation from the line preceding them. The indentation of the second
1516line in the statement depends on the form of the first (base) line: if
1517the base line is an assignment statement with anything more interesting
1518than the backslash following the leftmost assigning `=', the second line
1519is indented two columns beyond that `='. Else it's indented to two
1520columns beyond the leftmost solid chunk of non-whitespace characters on
1521the base line.
1522
1523Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1524repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1525structure you intend.
1526%c:indent-for-tab-command
1527%c:py-newline-and-indent
1528%c:py-delete-char
1529
1530
1531The next function may be handy when editing code you didn't write:
1532%c:py-guess-indent-offset
1533
1534
1535The remaining `indent' functions apply to a region of Python code. They
1536assume the block structure (equals indentation, in Python) of the region
1537is correct, and alter the indentation in various ways while preserving
1538the block structure:
1539%c:py-indent-region
1540%c:py-shift-region-left
1541%c:py-shift-region-right
1542
1543@MARKING & MANIPULATING REGIONS OF CODE
1544
1545\\[py-mark-block]\t mark block of lines
1546\\[mark-python-def-or-class]\t mark smallest enclosing def
1547\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
1548\\[py-comment-region]\t comment out region of code
1549\\[universal-argument] \\[py-comment-region]\t uncomment region of code
1550%c:py-mark-block
1551%c:mark-python-def-or-class
1552%c:py-comment-region
1553
1554@MOVING POINT
1555
1556\\[py-previous-statement]\t move to statement preceding point
1557\\[py-next-statement]\t move to statement following point
1558\\[py-goto-block-up]\t move up to start of current block
1559\\[beginning-of-python-def-or-class]\t move to start of def
1560\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1561\\[end-of-python-def-or-class]\t move to end of def
1562\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1563
1564The first two move to one statement beyond the statement that contains
1565point. A numeric prefix argument tells them to move that many
1566statements instead. Blank lines, comment lines, and continuation lines
1567do not count as `statements' for these commands. So, e.g., you can go
1568to the first code statement in a file by entering
1569\t\\[beginning-of-buffer]\t to move to the top of the file
1570\t\\[py-next-statement]\t to skip over initial comments and blank lines
1571Or do `\\[py-previous-statement]' with a huge prefix argument.
1572%c:py-previous-statement
1573%c:py-next-statement
1574%c:py-goto-block-up
1575%c:beginning-of-python-def-or-class
1576%c:end-of-python-def-or-class
1577
1578@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
1579
1580`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
1581
1582`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
1583overall class and def structure of a module.
1584
1585`\\[back-to-indentation]' moves point to a line's first non-blank character.
1586
1587`\\[indent-relative]' is handy for creating odd indentation.
1588
1589@OTHER EMACS HINTS
1590
1591If you don't like the default value of a variable, change its value to
1592whatever you do like by putting a `setq' line in your .emacs file.
1593E.g., to set the indentation increment to 4, put this line in your
1594.emacs:
1595\t(setq py-indent-offset 4)
1596To see the value of a variable, do `\\[describe-variable]' and enter the variable
1597name at the prompt.
1598
1599When entering a key sequence like `C-c C-n', it is not necessary to
1600release the CONTROL key after doing the `C-c' part -- it suffices to
1601press the CONTROL key, press and release `c' (while still holding down
1602CONTROL), press and release `n' (while still holding down CONTROL), &
1603then release CONTROL.
1604
1605Entering Python mode calls with no arguments the value of the variable
1606`python-mode-hook', if that value exists and is not nil; for backward
1607compatibility it also tries `py-mode-hook'; see the `Hooks' section of
1608the Elisp manual for details.
1609
1610Obscure: When python-mode is first loaded, it looks for all bindings
1611to newline-and-indent in the global keymap, and shadows them with
1612local bindings to py-newline-and-indent."))
1613
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001614
1615;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001616(defvar py-parse-state-re
1617 (concat
1618 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
1619 "\\|"
1620 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001621
Barry Warsaw7ae77681994-12-12 20:38:05 +00001622;; returns the parse state at point (see parse-partial-sexp docs)
1623(defun py-parse-state ()
1624 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001625 (let ((here (point)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001626 ;; back up to the first preceding line (if any; else start of
1627 ;; buffer) that begins with a popular Python keyword, or a non-
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001628 ;; whitespace and non-comment character. These are good places
1629 ;; to start parsing to see whether where we started is at a
1630 ;; non-zero nesting level. It may be slow for people who write
1631 ;; huge code blocks or huge lists ... tough beans.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001632 (re-search-backward py-parse-state-re nil 'move)
1633 (beginning-of-line)
1634 (parse-partial-sexp (point) here))))
1635
1636;; if point is at a non-zero nesting level, returns the number of the
1637;; character that opens the smallest enclosing unclosed list; else
1638;; returns nil.
1639(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001640 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001641 (if (zerop (car status))
1642 nil ; not in a nest
1643 (car (cdr status))))) ; char# of open bracket
1644
1645;; t iff preceding line ends with backslash that's not in a comment
1646(defun py-backslash-continuation-line-p ()
1647 (save-excursion
1648 (beginning-of-line)
1649 (and
1650 ;; use a cheap test first to avoid the regexp if possible
1651 ;; use 'eq' because char-after may return nil
1652 (eq (char-after (- (point) 2)) ?\\ )
1653 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001654 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001655 (looking-at py-continued-re))))
1656
1657;; t iff current line is a continuation line
1658(defun py-continuation-line-p ()
1659 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001660 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001661 (or (py-backslash-continuation-line-p)
1662 (py-nesting-level))))
1663
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001664;; go to initial line of current statement; usually this is the line
1665;; we're on, but if we're on the 2nd or following lines of a
1666;; continuation block, we need to go up to the first line of the
1667;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001668;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001669;; Tricky: We want to avoid quadratic-time behavior for long continued
1670;; blocks, whether of the backslash or open-bracket varieties, or a
1671;; mix of the two. The following manages to do that in the usual
1672;; cases.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001673(defun py-goto-initial-line ()
1674 (let ( open-bracket-pos )
1675 (while (py-continuation-line-p)
1676 (beginning-of-line)
1677 (if (py-backslash-continuation-line-p)
1678 (while (py-backslash-continuation-line-p)
1679 (forward-line -1))
1680 ;; else zip out of nested brackets/braces/parens
1681 (while (setq open-bracket-pos (py-nesting-level))
1682 (goto-char open-bracket-pos)))))
1683 (beginning-of-line))
1684
1685;; go to point right beyond final line of current statement; usually
1686;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001687;; statement we need to skip over the continuation lines. Tricky:
1688;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001689(defun py-goto-beyond-final-line ()
1690 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001691 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001692 (while (and (py-continuation-line-p)
1693 (not (eobp)))
1694 ;; skip over the backslash flavor
1695 (while (and (py-backslash-continuation-line-p)
1696 (not (eobp)))
1697 (forward-line 1))
1698 ;; if in nest, zip to the end of the nest
1699 (setq state (py-parse-state))
1700 (if (and (not (zerop (car state)))
1701 (not (eobp)))
1702 (progn
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001703 ;; BUG ALERT: I could swear, from reading the docs, that
Barry Warsaw7ae77681994-12-12 20:38:05 +00001704 ;; the 3rd argument should be plain 0
1705 (parse-partial-sexp (point) (point-max) (- 0 (car state))
1706 nil state)
1707 (forward-line 1))))))
1708
1709;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001710;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00001711(defun py-statement-opens-block-p ()
1712 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001713 (let ((start (point))
1714 (finish (progn (py-goto-beyond-final-line) (1- (point))))
1715 (searching t)
1716 (answer nil)
1717 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001718 (goto-char start)
1719 (while searching
1720 ;; look for a colon with nothing after it except whitespace, and
1721 ;; maybe a comment
1722 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
1723 finish t)
1724 (if (eq (point) finish) ; note: no `else' clause; just
1725 ; keep searching if we're not at
1726 ; the end yet
1727 ;; sure looks like it opens a block -- but it might
1728 ;; be in a comment
1729 (progn
1730 (setq searching nil) ; search is done either way
1731 (setq state (parse-partial-sexp start
1732 (match-beginning 0)))
1733 (setq answer (not (nth 4 state)))))
1734 ;; search failed: couldn't find another interesting colon
1735 (setq searching nil)))
1736 answer)))
1737
1738;; go to point right beyond final line of block begun by the current
1739;; line. This is the same as where py-goto-beyond-final-line goes
1740;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001741;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00001742(defun py-goto-beyond-block ()
1743 (if (py-statement-opens-block-p)
1744 (py-mark-block nil 'just-move)
1745 (py-goto-beyond-final-line)))
1746
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001747;; go to start of first statement (not blank or comment or
1748;; continuation line) at or preceding point. returns t if there is
1749;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00001750(defun py-goto-statement-at-or-above ()
1751 (py-goto-initial-line)
1752 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001753 ;; skip back over blank & comment lines
1754 ;; note: will skip a blank or comment line that happens to be
1755 ;; a continuation line too
1756 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
1757 (progn (py-goto-initial-line) t)
1758 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001759 t))
1760
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001761;; go to start of first statement (not blank or comment or
1762;; continuation line) following the statement containing point returns
1763;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00001764(defun py-goto-statement-below ()
1765 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001766 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001767 (py-goto-beyond-final-line)
1768 (while (and
1769 (looking-at py-blank-or-comment-re)
1770 (not (eobp)))
1771 (forward-line 1))
1772 (if (eobp)
1773 (progn (goto-char start) nil)
1774 t)))
1775
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001776;; go to start of statement, at or preceding point, starting with
1777;; keyword KEY. Skips blank lines and non-indenting comments upward
1778;; first. If that statement starts with KEY, done, else go back to
1779;; first enclosing block starting with KEY. If successful, leaves
1780;; point at the start of the KEY line & returns t. Else leaves point
1781;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001782(defun py-go-up-tree-to-keyword (key)
1783 ;; skip blanks and non-indenting #
1784 (py-goto-initial-line)
1785 (while (and
1786 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1787 (zerop (forward-line -1))) ; go back
1788 nil)
1789 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001790 (let* ((re (concat "[ \t]*" key "\\b"))
1791 (case-fold-search nil) ; let* so looking-at sees this
1792 (found (looking-at re))
1793 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001794 (while (not (or found dead))
1795 (condition-case nil ; in case no enclosing block
1796 (py-goto-block-up 'no-mark)
1797 (error (setq dead t)))
1798 (or dead (setq found (looking-at re))))
1799 (beginning-of-line)
1800 found))
1801
1802;; return string in buffer from start of indentation to end of line;
1803;; prefix "..." if leading whitespace was skipped
1804(defun py-suck-up-leading-text ()
1805 (save-excursion
1806 (back-to-indentation)
1807 (concat
1808 (if (bolp) "" "...")
1809 (buffer-substring (point) (progn (end-of-line) (point))))))
1810
1811;; assuming point at bolp, return first keyword ([a-z]+) on the line,
1812;; as a Lisp symbol; return nil if none
1813(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001814 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001815 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
1816 (intern (buffer-substring (match-beginning 1) (match-end 1)))
1817 nil)))
1818
1819(defun py-make-temp-name ()
1820 (make-temp-name
1821 (concat (file-name-as-directory py-temp-directory) "python")))
1822
1823(defun py-delete-file-silently (fname)
1824 (condition-case nil
1825 (delete-file fname)
1826 (error nil)))
1827
1828(defun py-kill-emacs-hook ()
1829 ;; delete our temp files
1830 (while py-file-queue
1831 (py-delete-file-silently (car py-file-queue))
1832 (setq py-file-queue (cdr py-file-queue)))
1833 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
1834 ;; run the hook we inherited, if any
1835 (and py-inherited-kill-emacs-hook
1836 (funcall py-inherited-kill-emacs-hook))))
1837
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001838;; make PROCESS's buffer visible, append STRING to it, and force
1839;; display; also make shell-mode believe the user typed this string,
1840;; so that kill-output-from-shell and show-output-from-shell work
1841;; "right"
Barry Warsaw7ae77681994-12-12 20:38:05 +00001842(defun py-append-to-process-buffer (process string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001843 (let ((cbuf (current-buffer))
1844 (pbuf (process-buffer process))
1845 (py-scroll-process-buffer t))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001846 (set-buffer pbuf)
1847 (goto-char (point-max))
1848 (move-marker (process-mark process) (point))
1849 (if (not py-this-is-emacs-19-p)
1850 (move-marker last-input-start (point))) ; muck w/ shell-mode
1851 (funcall (process-filter process) process string)
1852 (if (not py-this-is-emacs-19-p)
1853 (move-marker last-input-end (point))) ; muck w/ shell-mode
1854 (set-buffer cbuf))
1855 (sit-for 0))
1856
Barry Warsaw74d9cc51995-03-08 22:05:16 +00001857(defun py-keep-region-active ()
1858 ;; do whatever is necessary to keep the region active in XEmacs.
1859 ;; Ignore byte-compiler warnings you might see. Also note that
1860 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
1861 ;; require us to take explicit action.
1862 (and (boundp 'zmacs-region-stays)
1863 (setq zmacs-region-stays t)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001864
1865
Barry Warsaw850437a1995-03-08 21:50:28 +00001866(defconst py-version "$Revision$"
1867 "`python-mode' version number.")
1868(defconst py-help-address "bwarsaw@cnri.reston.va.us"
1869 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001870
Barry Warsaw850437a1995-03-08 21:50:28 +00001871(defun py-version ()
1872 "Echo the current version of `python-mode' in the minibuffer."
1873 (interactive)
1874 (message "Using `python-mode' version %s" py-version)
1875 (py-keep-region-active))
1876
1877;; only works under Emacs 19
1878;(eval-when-compile
1879; (require 'reporter))
1880
1881(defun py-submit-bug-report (enhancement-p)
1882 "Submit via mail a bug report on `python-mode'.
1883With \\[universal-argument] just submit an enhancement request."
1884 (interactive
1885 (list (not (y-or-n-p
1886 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00001887 (let ((reporter-prompt-for-summary-p (if enhancement-p
1888 "(Very) brief summary: "
1889 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00001890 (require 'reporter)
1891 (reporter-submit-bug-report
1892 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00001893 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00001894 ;; varlist
1895 (if enhancement-p nil
1896 '(py-python-command
1897 py-indent-offset
1898 py-block-comment-prefix
1899 py-scroll-process-buffer
1900 py-temp-directory
1901 py-beep-if-tab-change))
1902 nil ;pre-hooks
1903 nil ;post-hooks
1904 "Dear Barry,") ;salutation
1905 (if enhancement-p nil
1906 (set-mark (point))
1907 (insert
1908"Please replace this text with a sufficiently large code sample\n\
1909and an exact recipe so that I can reproduce your problem. Failure\n\
1910to do so may mean a greater delay in fixing your bug.\n\n")
1911 (exchange-point-and-mark)
1912 (py-keep-region-active))))
1913
1914
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001915;; arrange to kill temp files when Emacs exists
1916(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
1917 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
1918 ;; have to trust that other people are as respectful of our hook
1919 ;; fiddling as we are of theirs
1920 (if (boundp 'py-inherited-kill-emacs-hook)
1921 ;; we were loaded before -- trust others not to have screwed us
1922 ;; in the meantime (no choice, really)
1923 nil
1924 ;; else arrange for our hook to run theirs
1925 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
1926 (setq kill-emacs-hook 'py-kill-emacs-hook)))
1927
1928
1929
1930(provide 'python-mode)
1931;;; python-mode.el ends here