blob: e9aa6bddbff55f77e10552a515386dd1068a3fdd [file] [log] [blame]
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
5;; Author: 1995 Barry A. Warsaw <bwarsaw@cnri.reston.va.us>
6;; 1992-1994 Tim Peters <tim@ksr.com>
7;; Maintainer: bwarsaw@cnri.reston.va.us
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00008;; Created: Feb 1992
Guido van Rossum2ed53541995-03-15 19:57:14 +00009;; Version: 2.16
10;; Last Modified: 1995/03/15 16:23:59
Guido van Rossumdeaa1051995-03-10 16:17:03 +000011;; Keywords: python editing language major-mode
12
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000013;; This software is provided as-is, without express or implied
14;; warranty. Permission to use, copy, modify, distribute or sell this
15;; software, without fee, for any purpose and by any individual or
16;; organization, is hereby granted, provided that the above copyright
17;; notice and this paragraph appear in all copies.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000018
19;;; Commentary:
Guido van Rossuma7925f11994-01-26 10:20:16 +000020;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +000021;; This is a major mode for editing Python programs. It was developed
22;; by Tim Peters <tim@ksr.com> after an original idea by Michael
23;; A. Guravage. Tim doesn't appear to be on the 'net any longer so I
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000024;; have undertaken maintenance of the mode.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000025
26;; At some point this mode will undergo a rewrite to bring it more in
27;; line with GNU Emacs Lisp coding standards. But all in all, the
28;; mode works exceedingly well.
29
30;; The following statements, placed in your .emacs file or
31;; site-init.el, will cause this file to be autoloaded, and
32;; python-mode invoked, when visiting .py files (assuming this file is
33;; in your load-path):
Guido van Rossuma7925f11994-01-26 10:20:16 +000034;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +000035;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Guido van Rossuma7925f11994-01-26 10:20:16 +000036;; (setq auto-mode-alist
37;; (cons '("\\.py$" . python-mode) auto-mode-alist))
38
Guido van Rossum1d5645d1995-03-14 21:31:47 +000039;; Here's a brief list of recent additions/improvements:
40;;
41;; - Wrapping and indentation within triple quote strings should work
42;; properly now.
43;; - `Standard' bug reporting mechanism (use C-c C-b)
44;; - py-mark-block was moved to C-c C-m
45;; - C-c C-v shows you the python-mode version
46;; - a basic python-font-lock-keywords has been added for Emacs 19
47;; font-lock colorizations.
48;; - proper interaction with pending-del and del-sel modes.
49;; - New py-electric-colon (:) command for improved outdenting. Also
50;; py-indent-line (TAB) should handle outdented lines better.
Guido van Rossum2ed53541995-03-15 19:57:14 +000051;; - New commands py-outdent-left (C-c C-l) and py-indent-right (C-c C-r)
Guido van Rossum1d5645d1995-03-14 21:31:47 +000052
Guido van Rossumdeaa1051995-03-10 16:17:03 +000053;; Here's a brief to do list:
54;;
Guido van Rossum1d5645d1995-03-14 21:31:47 +000055;; - Better integration with gud-mode for debugging.
56;; - Rewrite according to GNU Emacs Lisp standards.
57;; - py-delete-char should obey numeric arguments.
58;; - even better support for outdenting. Guido suggests outdents of
59;; at least one level after a return, raise, break, or continue
60;; statement.
Guido van Rossuma7925f11994-01-26 10:20:16 +000061
Guido van Rossumdeaa1051995-03-10 16:17:03 +000062;; If you can think of more things you'd like to see, drop me a line.
63;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
64;;
65;; Note that I only test things on XEmacs (currently 19.11). If you
66;; port stuff to FSF Emacs 19, or Emacs 18, please send me your
67;; patches.
Guido van Rossuma7925f11994-01-26 10:20:16 +000068
Guido van Rossumdeaa1051995-03-10 16:17:03 +000069;; LCD Archive Entry:
70;; python-mode|Barry A. Warsaw|bwarsaw@cnri.reston.va.us
71;; |Major mode for editing Python programs
Guido van Rossum2ed53541995-03-15 19:57:14 +000072;; |1995/03/15 16:23:59|2.16|
Guido van Rossuma7925f11994-01-26 10:20:16 +000073
Guido van Rossumdeaa1051995-03-10 16:17:03 +000074;;; Code:
75
76
77;; user definable variables
78;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Guido van Rossuma7925f11994-01-26 10:20:16 +000079
80(defvar py-python-command "python"
81 "*Shell command used to start Python interpreter.")
82
83(defvar py-indent-offset 8 ; argue with Guido <grin>
84 "*Indentation increment.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000085Note that `\\[py-guess-indent-offset]' can usually guess a good value
86when you're editing someone else's Python code.")
Guido van Rossuma7925f11994-01-26 10:20:16 +000087
Guido van Rossuma7925f11994-01-26 10:20:16 +000088(defvar py-block-comment-prefix "##"
Guido van Rossumdeaa1051995-03-10 16:17:03 +000089 "*String used by `py-comment-region' to comment out a block of code.
Guido van Rossuma7925f11994-01-26 10:20:16 +000090This should follow the convention for non-indenting comment lines so
91that the indentation commands won't get confused (i.e., the string
92should be of the form `#x...' where `x' is not a blank or a tab, and
93`...' is arbitrary).")
94
95(defvar py-scroll-process-buffer t
96 "*Scroll Python process buffer as output arrives.
97If nil, the Python process buffer acts, with respect to scrolling, like
98Shell-mode buffers normally act. This is surprisingly complicated and
99so won't be explained here; in fact, you can't get the whole story
100without studying the Emacs C code.
101
102If non-nil, the behavior is different in two respects (which are
103slightly inaccurate in the interest of brevity):
104
105 - If the buffer is in a window, and you left point at its end, the
106 window will scroll as new output arrives, and point will move to the
107 buffer's end, even if the window is not the selected window (that
108 being the one the cursor is in). The usual behavior for shell-mode
109 windows is not to scroll, and to leave point where it was, if the
110 buffer is in a window other than the selected window.
111
112 - If the buffer is not visible in any window, and you left point at
113 its end, the buffer will be popped into a window as soon as more
114 output arrives. This is handy if you have a long-running
115 computation and don't want to tie up screen area waiting for the
116 output. The usual behavior for a shell-mode buffer is to stay
117 invisible until you explicitly visit it.
118
119Note the `and if you left point at its end' clauses in both of the
120above: you can `turn off' the special behaviors while output is in
121progress, by visiting the Python buffer and moving point to anywhere
122besides the end. Then the buffer won't scroll, point will remain where
123you leave it, and if you hide the buffer it will stay hidden until you
124visit it again. You can enable and disable the special behaviors as
125often as you like, while output is in progress, by (respectively) moving
126point to, or away from, the end of the buffer.
127
128Warning: If you expect a large amount of output, you'll probably be
129happier setting this option to nil.
130
131Obscure: `End of buffer' above should really say `at or beyond the
132process mark', but if you know what that means you didn't need to be
133told <grin>.")
134
135(defvar py-temp-directory
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000136 (let ((ok '(lambda (x)
137 (and x
138 (setq x (expand-file-name x)) ; always true
139 (file-directory-p x)
140 (file-writable-p x)
141 x))))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000142 (or (funcall ok (getenv "TMPDIR"))
143 (funcall ok "/usr/tmp")
144 (funcall ok "/tmp")
145 (funcall ok ".")
146 (error
147 "Couldn't find a usable temp directory -- set py-temp-directory")))
148 "*Directory used for temp files created by a *Python* process.
149By default, the first directory from this list that exists and that you
150can write into: the value (if any) of the environment variable TMPDIR,
151/usr/tmp, /tmp, or the current directory.")
152
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000153(defvar py-beep-if-tab-change t
154 "*Ring the bell if tab-width is changed.
155If a comment of the form
156
157 \t# vi:set tabsize=<number>:
158
159is found before the first code line when the file is entered, and the
160current value of (the general Emacs variable) `tab-width' does not
161equal <number>, `tab-width' is set to <number>, a message saying so is
162displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
163the Emacs bell is also rung as a warning.")
164
165(defvar python-font-lock-keywords
166 (list
167 (cons
168 (concat
169 "\\<\\("
170 (mapconcat
171 'identity
172 '("access" "and" "break" "continue"
173 "del" "elif" "else" "except"
174 "exec" "finally" "for" "from"
175 "global" "if" "import" "in"
176 "is" "lambda" "not" "or"
177 "pass" "print" "raise" "return"
178 "try" "while" "def" "class"
179 )
180 "\\|")
181 "\\)\\>")
182 1)
183 ;; functions
184 '("\\bdef\\s +\\(\\sw+\\)(" 1 font-lock-function-name-face)
185 ;; classes
186 '("\\bclass\\s +\\(\\sw+\\)[(:]" 1 font-lock-function-name-face)
187 )
188 "*Additional keywords to highlight `python-mode' buffers.")
189
190
191;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
193
194;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
195;; seems to be the standard way of checking this.
196;; BAW - This is *not* the right solution. When at all possible,
197;; instead of testing for the version of Emacs, use feature tests.
198
199(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
200(setq py-this-is-emacs-19-p
201 (and
202 (not py-this-is-lucid-emacs-p)
203 (string-match "^19\\." emacs-version)))
204
Guido van Rossuma7925f11994-01-26 10:20:16 +0000205;; have to bind py-file-queue before installing the kill-emacs hook
206(defvar py-file-queue nil
207 "Queue of Python temp files awaiting execution.
208Currently-active file is at the head of the list.")
209
210;; define a mode-specific abbrev table for those who use such things
211(defvar python-mode-abbrev-table nil
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000212 "Abbrev table in use in `python-mode' buffers.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000213(define-abbrev-table 'python-mode-abbrev-table nil)
214
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000215(defvar python-mode-hook nil
216 "*Hook called by `python-mode'.")
217
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000218;; in previous version of python-mode.el, the hook was incorrectly
219;; called py-mode-hook, and was not defvar'd. deprecate its use.
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000220(and (fboundp 'make-obsolete-variable)
221 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
222
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000223(defvar py-mode-map ()
224 "Keymap used in `python-mode' buffers.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000225
Guido van Rossuma7925f11994-01-26 10:20:16 +0000226(if py-mode-map
227 ()
228 (setq py-mode-map (make-sparse-keymap))
229
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000230 ;; shadow global bindings for newline-and-indent w/ the py- version.
231 ;; BAW - this is extremely bad form, but I'm not going to change it
232 ;; for now.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000233 (mapcar (function (lambda (key)
234 (define-key
235 py-mode-map key 'py-newline-and-indent)))
236 (where-is-internal 'newline-and-indent))
237
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000238 ;; BAW - you could do it this way, but its not considered proper
239 ;; major-mode form.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000240 (mapcar (function
241 (lambda (x)
242 (define-key py-mode-map (car x) (cdr x))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000243 '((":" . py-electric-colon)
244 ("\C-c\C-c" . py-execute-buffer)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000245 ("\C-c|" . py-execute-region)
246 ("\C-c!" . py-shell)
247 ("\177" . py-delete-char)
248 ("\n" . py-newline-and-indent)
249 ("\C-c:" . py-guess-indent-offset)
250 ("\C-c\t" . py-indent-region)
Guido van Rossum2ed53541995-03-15 19:57:14 +0000251 ("\C-c\C-l" . py-outdent-left)
252 ("\C-c\C-r" . py-indent-right)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000253 ("\C-c<" . py-shift-region-left)
254 ("\C-c>" . py-shift-region-right)
255 ("\C-c\C-n" . py-next-statement)
256 ("\C-c\C-p" . py-previous-statement)
257 ("\C-c\C-u" . py-goto-block-up)
258 ("\C-c\C-m" . py-mark-block)
259 ("\C-c#" . py-comment-region)
260 ("\C-c?" . py-describe-mode)
261 ("\C-c\C-hm" . py-describe-mode)
262 ("\e\C-a" . beginning-of-python-def-or-class)
263 ("\e\C-e" . end-of-python-def-or-class)
264 ( "\e\C-h" . mark-python-def-or-class)))
265 ;; should do all keybindings this way
266 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
267 (define-key py-mode-map "\C-c\C-v" 'py-version)
268 )
Guido van Rossuma7925f11994-01-26 10:20:16 +0000269
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000270(defvar py-mode-syntax-table nil
271 "Syntax table used in `python-mode' buffers.")
272
Guido van Rossuma7925f11994-01-26 10:20:16 +0000273(if py-mode-syntax-table
274 ()
275 (setq py-mode-syntax-table (make-syntax-table))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000276 ;; BAW - again, blech.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000277 (mapcar (function
278 (lambda (x) (modify-syntax-entry
279 (car x) (cdr x) py-mode-syntax-table)))
280 '(( ?\( . "()" ) ( ?\) . ")(" )
281 ( ?\[ . "(]" ) ( ?\] . ")[" )
282 ( ?\{ . "(}" ) ( ?\} . "){" )
283 ;; fix operator symbols misassigned in the std table
284 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
285 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
286 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
287 ( ?\> . "." ) ( ?\| . "." )
288 ( ?\_ . "w" ) ; underscore is legit in names
289 ( ?\' . "\"") ; single quote is string quote
290 ( ?\" . "\"" ) ; double quote is string quote too
291 ( ?\` . "$") ; backquote is open and close paren
292 ( ?\# . "<") ; hash starts comment
293 ( ?\n . ">")))) ; newline ends comment
294
Guido van Rossume531e4b1994-04-16 08:29:27 +0000295(defconst py-stringlit-re
296 (concat
297 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
298 "\\|" ; or
299 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000300 "Regexp matching a Python string literal.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000301
302;; this is tricky because a trailing backslash does not mean
303;; continuation if it's in a comment
304(defconst py-continued-re
305 (concat
Guido van Rossume531e4b1994-04-16 08:29:27 +0000306 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
Guido van Rossuma7925f11994-01-26 10:20:16 +0000307 "\\\\$")
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000308 "Regexp matching Python lines that are continued via backslash.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
310(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000311 "Regexp matching blank or comment lines.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000312
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000313(defconst py-outdent-re
314 (concat "\\(" (mapconcat 'identity
315 '("else:"
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000316 "except\\(\\s +.*\\)?:"
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000317 "finally:"
318 "elif\\s +.*:")
319 "\\|")
320 "\\)")
321 "Regexp matching clauses to be outdented one level.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000322
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000323(defconst py-no-outdent-re
324 (concat "\\(" (mapconcat 'identity
Guido van Rossum2ed53541995-03-15 19:57:14 +0000325 '("try:"
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000326 "except\\(\\s +.*\\)?:"
327 "while\\s +.*:"
328 "for\\s +.*:"
329 "if\\s +.*:"
330 "elif\\s +.*:")
331 "\\|")
332 "\\)")
333 "Regexp matching lines to not outdent after.")
334
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000335
336;;;###autoload
Guido van Rossuma7925f11994-01-26 10:20:16 +0000337(defun python-mode ()
338 "Major mode for editing Python files.
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000339To submit a problem report, enter `\\[py-submit-bug-report]' from a
340`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
341documentation. To see what version of `python-mode' you are running,
342enter `\\[py-version]'.
343
344This mode knows about Python indentation, tokens, comments and
345continuation lines. Paragraphs are separated by blank lines only.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000346
347COMMANDS
348\\{py-mode-map}
349VARIABLES
350
351py-indent-offset\tindentation increment
Guido van Rossuma7925f11994-01-26 10:20:16 +0000352py-block-comment-prefix\tcomment string used by py-comment-region
353py-python-command\tshell command to invoke Python interpreter
354py-scroll-process-buffer\talways scroll Python process buffer
355py-temp-directory\tdirectory used for temp files (if needed)
356py-beep-if-tab-change\tring the bell if tab-width is changed"
357 (interactive)
358 (kill-all-local-variables)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000359 (set-syntax-table py-mode-syntax-table)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000360 (setq major-mode 'python-mode
361 mode-name "Python"
362 local-abbrev-table python-mode-abbrev-table)
363 (use-local-map py-mode-map)
364 ;; BAW -- style...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000365 (mapcar (function (lambda (x)
366 (make-local-variable (car x))
367 (set (car x) (cdr x))))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000368 '((paragraph-separate . "^[ \t]*$")
369 (paragraph-start . "^[ \t]*$")
370 (require-final-newline . t)
371 (comment-start . "# ")
372 (comment-start-skip . "# *")
373 (comment-column . 40)
374 (indent-region-function . py-indent-region)
375 (indent-line-function . py-indent-line)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000376 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000377 ;;
378 ;; not sure where the magic comment has to be; to save time
379 ;; searching for a rarity, we give up if it's not found prior to the
380 ;; first executable statement.
381 ;;
382 ;; BAW - on first glance, this seems like complete hackery. Why was
383 ;; this necessary, and is it still necessary?
384 (let ((case-fold-search nil)
385 (start (point))
386 new-tab-width)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000387 (if (re-search-forward
388 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
389 (prog2 (py-next-statement 1) (point) (goto-char 1))
390 t)
391 (progn
392 (setq new-tab-width
393 (string-to-int
394 (buffer-substring (match-beginning 1) (match-end 1))))
395 (if (= tab-width new-tab-width)
396 nil
397 (setq tab-width new-tab-width)
398 (message "Caution: tab-width changed to %d" new-tab-width)
399 (if py-beep-if-tab-change (beep)))))
400 (goto-char start))
401
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000402 ;; run the mode hook. py-mode-hook use is deprecated
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000403 (if python-mode-hook
404 (run-hooks 'python-mode-hook)
405 (run-hooks 'py-mode-hook)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000406
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000407
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000408;; electric characters
Guido van Rossumd97cc371995-03-15 19:55:55 +0000409(defun py-outdent-p ()
410 ;; returns non-nil if the current line should outdent one level
411 (save-excursion
412 (and (progn (back-to-indentation)
413 (looking-at py-outdent-re))
414 (progn (backward-to-indentation 1)
415 (while (or (looking-at py-blank-or-comment-re)
416 (bobp))
417 (backward-to-indentation 1))
418 (not (looking-at py-no-outdent-re)))
419 )))
420
421
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000422(defun py-electric-colon (arg)
423 "Insert a colon.
424In certain cases the line is outdented appropriately. If a numeric
425argument is provided, that many colons are inserted non-electrically."
426 (interactive "P")
427 (self-insert-command (prefix-numeric-value arg))
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000428 (save-excursion
429 (let ((here (point))
430 (outdent 0)
431 (indent (py-compute-indentation)))
432 (if (and (not arg)
Guido van Rossumd97cc371995-03-15 19:55:55 +0000433 (py-outdent-p)
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000434 (= indent (progn
435 (forward-line -1)
436 (py-compute-indentation)))
437 )
438 (setq outdent py-indent-offset))
Guido van Rossum2ed53541995-03-15 19:57:14 +0000439 ;; electric colon won't re-indent lines that start in column
440 ;; zero. you'd have to use TAB for that. TBD: Is there a
441 ;; better way to determine this???
442 (if (zerop (current-indentation)) nil
443 (goto-char here)
444 (beginning-of-line)
445 (delete-horizontal-space)
446 (indent-to (- indent outdent))
447 ))))
448
449(defun py-indent-right (arg)
450 "Indent the line by one `py-indent-offset' level.
451With numeric arg, indent by that many levels. You cannot indent
452farther right than the distance the line would be indented by
453\\[py-indent-line]."
454 (interactive "p")
455 (let ((col (current-indentation))
456 (want (* arg py-indent-offset))
457 (indent (py-compute-indentation))
458 (pos (- (point-max) (point)))
459 (bol (save-excursion (beginning-of-line) (point))))
460 (if (<= (+ col want) indent)
461 (progn
462 (beginning-of-line)
463 (delete-horizontal-space)
464 (indent-to (+ col want))
465 (if (> (- (point-max) pos) (point))
466 (goto-char (- (point-max) pos)))
467 ))))
468
469(defun py-outdent-left (arg)
470 "Outdent the line by one `py-indent-offset' level.
471With numeric arg, outdent by that many levels. You cannot outdent
472farther left than column zero."
473 (interactive "p")
474 (let ((col (current-indentation))
475 (want (* arg py-indent-offset))
476 (pos (- (point-max) (point)))
477 (bol (save-excursion (beginning-of-line) (point))))
478 (if (<= 0 (- col want))
479 (progn
480 (beginning-of-line)
481 (delete-horizontal-space)
482 (indent-to (- col want))
483 (if (> (- (point-max) pos) (point))
484 (goto-char (- (point-max) pos)))
485 ))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000486
487
Guido van Rossuma7925f11994-01-26 10:20:16 +0000488;;; Functions that execute Python commands in a subprocess
Guido van Rossuma7925f11994-01-26 10:20:16 +0000489(defun py-shell ()
490 "Start an interactive Python interpreter in another window.
491This is like Shell mode, except that Python is running in the window
492instead of a shell. See the `Interactive Shell' and `Shell Mode'
493sections of the Emacs manual for details, especially for the key
494bindings active in the `*Python*' buffer.
495
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000496See the docs for variable `py-scroll-buffer' for info on scrolling
Guido van Rossuma7925f11994-01-26 10:20:16 +0000497behavior in the process window.
498
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000499Warning: Don't use an interactive Python if you change sys.ps1 or
500sys.ps2 from their default values, or if you're running code that
501prints `>>> ' or `... ' at the start of a line. `python-mode' can't
502distinguish your output from Python's output, and assumes that `>>> '
503at the start of a line is a prompt from Python. Similarly, the Emacs
504Shell mode code assumes that both `>>> ' and `... ' at the start of a
505line are Python prompts. Bad things can happen if you fool either
506mode.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000507
508Warning: If you do any editing *in* the process buffer *while* the
509buffer is accepting output from Python, do NOT attempt to `undo' the
510changes. Some of the output (nowhere near the parts you changed!) may
511be lost if you do. This appears to be an Emacs bug, an unfortunate
512interaction between undo and process filters; the same problem exists in
513non-Python process buffers using the default (Emacs-supplied) process
514filter."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000515 ;; BAW - should undo be disabled in the python process buffer, if
516 ;; this bug still exists?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000517 (interactive)
518 (if py-this-is-emacs-19-p
519 (progn
520 (require 'comint)
521 (switch-to-buffer-other-window
522 (make-comint "Python" py-python-command)))
523 (progn
524 (require 'shell)
525 (switch-to-buffer-other-window
526 (make-shell "Python" py-python-command))))
527 (make-local-variable 'shell-prompt-pattern)
528 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
529 (set-process-filter (get-buffer-process (current-buffer))
530 'py-process-filter)
531 (set-syntax-table py-mode-syntax-table))
532
533(defun py-execute-region (start end)
534 "Send the region between START and END to a Python interpreter.
535If there is a *Python* process it is used.
536
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000537Hint: If you want to execute part of a Python file several times
538\(e.g., perhaps you're developing a function and want to flesh it out
539a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
540the region of interest, and send the code to a *Python* process via
541`\\[py-execute-buffer]' instead.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000542
543Following are subtleties to note when using a *Python* process:
544
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000545If a *Python* process is used, the region is copied into a temporary
546file (in directory `py-temp-directory'), and an `execfile' command is
547sent to Python naming that file. If you send regions faster than
548Python can execute them, `python-mode' will save them into distinct
549temp files, and execute the next one in the queue the next time it
550sees a `>>> ' prompt from Python. Each time this happens, the process
551buffer is popped into a window (if it's not already in some window) so
552you can see it, and a comment of the form
Guido van Rossuma7925f11994-01-26 10:20:16 +0000553
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000554 \t## working on region in file <name> ...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000555
556is inserted at the end.
557
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000558Caution: No more than 26 regions can be pending at any given time.
559This limit is (indirectly) inherited from libc's mktemp(3).
560`python-mode' does not try to protect you from exceeding the limit.
561It's extremely unlikely that you'll get anywhere close to the limit in
562practice, unless you're trying to be a jerk <grin>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000563
564See the `\\[py-shell]' docs for additional warnings."
565 (interactive "r")
566 (or (< start end) (error "Region is empty"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000567 (let ((pyproc (get-process "Python"))
568 fname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000569 (if (null pyproc)
570 (shell-command-on-region start end py-python-command)
571 ;; else feed it thru a temp file
572 (setq fname (py-make-temp-name))
573 (write-region start end fname nil 'no-msg)
574 (setq py-file-queue (append py-file-queue (list fname)))
575 (if (cdr py-file-queue)
576 (message "File %s queued for execution" fname)
577 ;; else
578 (py-execute-file pyproc fname)))))
579
580(defun py-execute-file (pyproc fname)
581 (py-append-to-process-buffer
582 pyproc
583 (format "## working on region in file %s ...\n" fname))
584 (process-send-string pyproc (format "execfile('%s')\n" fname)))
585
586(defun py-process-filter (pyproc string)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000587 (let ((curbuf (current-buffer))
588 (pbuf (process-buffer pyproc))
589 (pmark (process-mark pyproc))
590 file-finished)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000591
592 ;; make sure we switch to a different buffer at least once. if we
593 ;; *don't* do this, then if the process buffer is in the selected
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000594 ;; window, and point is before the end, and lots of output is
595 ;; coming at a fast pace, then (a) simple cursor-movement commands
596 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
597 ;; to have a visible effect (the window just doesn't get updated,
598 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
599 ;; get all the process output (until the next python prompt).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000600 ;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000601 ;; #b makes no sense to me at all. #a almost makes sense: unless
602 ;; we actually change buffers, set_buffer_internal in buffer.c
603 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
604 ;; seems to make the Emacs command loop reluctant to update the
605 ;; display. Perhaps the default process filter in process.c's
606 ;; read_process_output has update_mode_lines++ for a similar
607 ;; reason? beats me ...
608
609 ;; BAW - we want to check to see if this still applies
Guido van Rossuma7925f11994-01-26 10:20:16 +0000610 (if (eq curbuf pbuf) ; mysterious ugly hack
611 (set-buffer (get-buffer-create "*scratch*")))
612
613 (set-buffer pbuf)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000614 (let* ((start (point))
615 (goback (< start pmark))
616 (buffer-read-only nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000617 (goto-char pmark)
618 (insert string)
619 (move-marker pmark (point))
620 (setq file-finished
621 (and py-file-queue
622 (equal ">>> "
623 (buffer-substring
624 (prog2 (beginning-of-line) (point)
625 (goto-char pmark))
626 (point)))))
627 (if goback (goto-char start)
628 ;; else
629 (if py-scroll-process-buffer
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000630 (let* ((pop-up-windows t)
631 (pwin (display-buffer pbuf)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000632 (set-window-point pwin (point))))))
633 (set-buffer curbuf)
634 (if file-finished
635 (progn
636 (py-delete-file-silently (car py-file-queue))
637 (setq py-file-queue (cdr py-file-queue))
638 (if py-file-queue
639 (py-execute-file pyproc (car py-file-queue)))))))
640
641(defun py-execute-buffer ()
642 "Send the contents of the buffer to a Python interpreter.
643If there is a *Python* process buffer it is used. If a clipping
644restriction is in effect, only the accessible portion of the buffer is
645sent. A trailing newline will be supplied if needed.
646
647See the `\\[py-execute-region]' docs for an account of some subtleties."
648 (interactive)
649 (py-execute-region (point-min) (point-max)))
650
651
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000652
653;; Functions for Python style indentation
Guido van Rossuma7925f11994-01-26 10:20:16 +0000654(defun py-delete-char ()
655 "Reduce indentation or delete character.
656If point is at the leftmost column, deletes the preceding newline.
657
658Else if point is at the leftmost non-blank character of a line that is
659neither a continuation line nor a non-indenting comment line, or if
660point is at the end of a blank line, reduces the indentation to match
661that of the line that opened the current block of code. The line that
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000662opened the block is displayed in the echo area to help you keep track
663of where you are.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000664
665Else the preceding character is deleted, converting a tab to spaces if
666needed so that only a single column position is deleted."
667 (interactive "*")
668 (if (or (/= (current-indentation) (current-column))
669 (bolp)
670 (py-continuation-line-p)
671 (looking-at "#[^ \t\n]")) ; non-indenting #
672 (backward-delete-char-untabify 1)
673 ;; else indent the same as the colon line that opened the block
674
675 ;; force non-blank so py-goto-block-up doesn't ignore it
676 (insert-char ?* 1)
677 (backward-char)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000678 (let ((base-indent 0) ; indentation of base line
679 (base-text "") ; and text of base line
680 (base-found-p nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000681 (condition-case nil ; in case no enclosing block
682 (save-excursion
683 (py-goto-block-up 'no-mark)
684 (setq base-indent (current-indentation)
685 base-text (py-suck-up-leading-text)
686 base-found-p t))
687 (error nil))
688 (delete-char 1) ; toss the dummy character
689 (delete-horizontal-space)
690 (indent-to base-indent)
691 (if base-found-p
692 (message "Closes block: %s" base-text)))))
693
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000694;; required for pending-del and delsel modes
695(put 'py-delete-char 'delete-selection 'supersede)
696(put 'py-delete-char 'pending-delete 'supersede)
697
Guido van Rossuma7925f11994-01-26 10:20:16 +0000698(defun py-indent-line ()
699 "Fix the indentation of the current line according to Python rules."
700 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000701 (let* ((ci (current-indentation))
702 (move-to-indentation-p (<= (current-column) ci))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000703 (need (py-compute-indentation)))
Guido van Rossum1c1fbf81995-03-14 21:33:10 +0000704 ;; see if we need to outdent
Guido van Rossumd97cc371995-03-15 19:55:55 +0000705 (if (py-outdent-p)
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000706 (setq need (- need py-indent-offset)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000707 (if (/= ci need)
708 (save-excursion
709 (beginning-of-line)
710 (delete-horizontal-space)
711 (indent-to need)))
712 (if move-to-indentation-p (back-to-indentation))))
713
714(defun py-newline-and-indent ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000715 "Strives to act like the Emacs `newline-and-indent'.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000716This is just `strives to' because correct indentation can't be computed
717from scratch for Python code. In general, deletes the whitespace before
718point, inserts a newline, and takes an educated guess as to how you want
719the new line indented."
720 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000721 (let ((ci (current-indentation)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000722 (if (< ci (current-column)) ; if point beyond indentation
723 (newline-and-indent)
724 ;; else try to act like newline-and-indent "normally" acts
725 (beginning-of-line)
726 (insert-char ?\n 1)
727 (move-to-column ci))))
728
729(defun py-compute-indentation ()
730 (save-excursion
731 (beginning-of-line)
732 (cond
733 ;; are we on a continuation line?
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000734 ((py-continuation-line-p)
735 (let ((startpos (point))
736 (open-bracket-pos (py-nesting-level))
737 endpos searching found)
738 (if open-bracket-pos
739 (progn
740 ;; align with first item in list; else a normal
741 ;; indent beyond the line with the open bracket
742 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
743 ;; is the first list item on the same line?
744 (skip-chars-forward " \t")
745 (if (null (memq (following-char) '(?\n ?# ?\\)))
746 ; yes, so line up with it
747 (current-column)
748 ;; first list item on another line, or doesn't exist yet
749 (forward-line 1)
750 (while (and (< (point) startpos)
751 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
752 (forward-line 1))
753 (if (< (point) startpos)
754 ;; again mimic the first list item
755 (current-indentation)
756 ;; else they're about to enter the first item
757 (goto-char open-bracket-pos)
758 (+ (current-indentation) py-indent-offset))))
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +0000759
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000760 ;; else on backslash continuation line
761 (forward-line -1)
762 (if (py-continuation-line-p) ; on at least 3rd line in block
763 (current-indentation) ; so just continue the pattern
764 ;; else started on 2nd line in block, so indent more.
765 ;; if base line is an assignment with a start on a RHS,
766 ;; indent to 2 beyond the leftmost "="; else skip first
767 ;; chunk of non-whitespace characters on base line, + 1 more
768 ;; column
769 (end-of-line)
770 (setq endpos (point) searching t)
771 (back-to-indentation)
772 (setq startpos (point))
773 ;; look at all "=" from left to right, stopping at first
774 ;; one not nested in a list or string
775 (while searching
776 (skip-chars-forward "^=" endpos)
777 (if (= (point) endpos)
778 (setq searching nil)
779 (forward-char 1)
780 (setq state (parse-partial-sexp startpos (point)))
781 (if (and (zerop (car state)) ; not in a bracket
782 (null (nth 3 state))) ; & not in a string
783 (progn
784 (setq searching nil) ; done searching in any case
785 (setq found
786 (not (or
787 (eq (following-char) ?=)
788 (memq (char-after (- (point) 2))
789 '(?< ?> ?!)))))))))
790 (if (or (not found) ; not an assignment
791 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
792 (progn
793 (goto-char startpos)
794 (skip-chars-forward "^ \t\n")))
795 (1+ (current-column))))))
Guido van Rossume531e4b1994-04-16 08:29:27 +0000796
Guido van Rossuma7925f11994-01-26 10:20:16 +0000797 ;; not on a continuation line
798
799 ;; if at start of restriction, or on a non-indenting comment line,
800 ;; assume they intended whatever's there
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000801 ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
802 (current-indentation))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000803
804 ;; else indentation based on that of the statement that precedes
805 ;; us; use the first line of that statement to establish the base,
806 ;; in case the user forced a non-std indentation for the
807 ;; continuation lines (if any)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000808 (t
809 ;; skip back over blank & non-indenting comment lines
810 ;; note: will skip a blank or non-indenting comment line that
811 ;; happens to be a continuation line too
812 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
813 nil 'move)
814 ;; if we landed inside a string, go to the beginning of that
815 ;; string. this handles triple quoted, multi-line spanning
816 ;; strings.
817 (let ((state (parse-partial-sexp
818 (save-excursion (beginning-of-python-def-or-class)
819 (point))
820 (point))))
821 (if (nth 3 state)
822 (goto-char (nth 2 state))))
823 (py-goto-initial-line)
824 (if (py-statement-opens-block-p)
825 (+ (current-indentation) py-indent-offset)
826 (current-indentation))))))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000827
828(defun py-guess-indent-offset (&optional global)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000829 "Guess a good value for, and change, `py-indent-offset'.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000830By default (without a prefix arg), makes a buffer-local copy of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000831`py-indent-offset' with the new value. This will not affect any other
Guido van Rossuma7925f11994-01-26 10:20:16 +0000832Python buffers. With a prefix arg, changes the global value of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000833`py-indent-offset'. This affects all Python buffers (that don't have
Guido van Rossuma7925f11994-01-26 10:20:16 +0000834their own buffer-local copy), both those currently existing and those
835created later in the Emacs session.
836
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000837Some people use a different value for `py-indent-offset' than you use.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000838There's no excuse for such foolishness, but sometimes you have to deal
839with their ugly code anyway. This function examines the file and sets
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000840`py-indent-offset' to what it thinks it was when they created the
841mess.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000842
843Specifically, it searches forward from the statement containing point,
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000844looking for a line that opens a block of code. `py-indent-offset' is
845set to the difference in indentation between that line and the Python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000846statement following it. If the search doesn't succeed going forward,
847it's tried again going backward."
848 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000849 (let (new-value
850 (start (point))
851 restart
852 (found nil)
853 colon-indent)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000854 (py-goto-initial-line)
855 (while (not (or found (eobp)))
856 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
857 (progn
858 (setq restart (point))
859 (py-goto-initial-line)
860 (if (py-statement-opens-block-p)
861 (setq found t)
862 (goto-char restart)))))
863 (if found
864 ()
865 (goto-char start)
866 (py-goto-initial-line)
867 (while (not (or found (bobp)))
868 (setq found
869 (and
870 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
871 (or (py-goto-initial-line) t) ; always true -- side effect
872 (py-statement-opens-block-p)))))
873 (setq colon-indent (current-indentation)
874 found (and found (zerop (py-next-statement 1)))
875 new-value (- (current-indentation) colon-indent))
876 (goto-char start)
877 (if found
878 (progn
879 (funcall (if global 'kill-local-variable 'make-local-variable)
880 'py-indent-offset)
881 (setq py-indent-offset new-value)
882 (message "%s value of py-indent-offset set to %d"
883 (if global "Global" "Local")
884 py-indent-offset))
885 (error "Sorry, couldn't guess a value for py-indent-offset"))))
886
887(defun py-shift-region (start end count)
888 (save-excursion
889 (goto-char end) (beginning-of-line) (setq end (point))
890 (goto-char start) (beginning-of-line) (setq start (point))
891 (indent-rigidly start end count)))
892
893(defun py-shift-region-left (start end &optional count)
894 "Shift region of Python code to the left.
895The lines from the line containing the start of the current region up
896to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000897shifted to the left, by `py-indent-offset' columns.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000898
899If a prefix argument is given, the region is instead shifted by that
900many columns."
901 (interactive "*r\nP") ; region; raw prefix arg
902 (py-shift-region start end
903 (- (prefix-numeric-value
904 (or count py-indent-offset)))))
905
906(defun py-shift-region-right (start end &optional count)
907 "Shift region of Python code to the right.
908The lines from the line containing the start of the current region up
909to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000910shifted to the right, by `py-indent-offset' columns.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000911
912If a prefix argument is given, the region is instead shifted by that
913many columns."
914 (interactive "*r\nP") ; region; raw prefix arg
915 (py-shift-region start end (prefix-numeric-value
916 (or count py-indent-offset))))
917
918(defun py-indent-region (start end &optional indent-offset)
919 "Reindent a region of Python code.
920The lines from the line containing the start of the current region up
921to (but not including) the line containing the end of the region are
922reindented. If the first line of the region has a non-whitespace
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000923character in the first column, the first line is left alone and the
924rest of the region is reindented with respect to it. Else the entire
925region is reindented with respect to the (closest code or
926indenting-comment) statement immediately preceding the region.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000927
928This is useful when code blocks are moved or yanked, when enclosing
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000929control structures are introduced or removed, or to reformat code
930using a new value for the indentation offset.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000931
932If a numeric prefix argument is given, it will be used as the value of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000933the indentation offset. Else the value of `py-indent-offset' will be
Guido van Rossuma7925f11994-01-26 10:20:16 +0000934used.
935
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000936Warning: The region must be consistently indented before this function
Guido van Rossuma7925f11994-01-26 10:20:16 +0000937is called! This function does not compute proper indentation from
938scratch (that's impossible in Python), it merely adjusts the existing
939indentation to be correct in context.
940
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000941Warning: This function really has no idea what to do with
942non-indenting comment lines, and shifts them as if they were indenting
943comment lines. Fixing this appears to require telepathy.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000944
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000945Special cases: whitespace is deleted from blank lines; continuation
946lines are shifted by the same amount their initial line was shifted,
947in order to preserve their relative indentation with respect to their
Guido van Rossuma7925f11994-01-26 10:20:16 +0000948initial line; and comment lines beginning in column 1 are ignored."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000949 (interactive "*r\nP") ; region; raw prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000950 (save-excursion
951 (goto-char end) (beginning-of-line) (setq end (point-marker))
952 (goto-char start) (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000953 (let ((py-indent-offset (prefix-numeric-value
954 (or indent-offset py-indent-offset)))
955 (indents '(-1)) ; stack of active indent levels
956 (target-column 0) ; column to which to indent
957 (base-shifted-by 0) ; amount last base line was shifted
958 (indent-base (if (looking-at "[ \t\n]")
959 (py-compute-indentation)
960 0))
961 ci)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000962 (while (< (point) end)
963 (setq ci (current-indentation))
964 ;; figure out appropriate target column
965 (cond
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000966 ((or (eq (following-char) ?#) ; comment in column 1
967 (looking-at "[ \t]*$")) ; entirely blank
968 (setq target-column 0))
969 ((py-continuation-line-p) ; shift relative to base line
970 (setq target-column (+ ci base-shifted-by)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000971 (t ; new base line
972 (if (> ci (car indents)) ; going deeper; push it
973 (setq indents (cons ci indents))
974 ;; else we should have seen this indent before
975 (setq indents (memq ci indents)) ; pop deeper indents
976 (if (null indents)
977 (error "Bad indentation in region, at line %d"
978 (save-restriction
979 (widen)
980 (1+ (count-lines 1 (point)))))))
981 (setq target-column (+ indent-base
982 (* py-indent-offset
983 (- (length indents) 2))))
984 (setq base-shifted-by (- target-column ci))))
985 ;; shift as needed
986 (if (/= ci target-column)
987 (progn
988 (delete-horizontal-space)
989 (indent-to target-column)))
990 (forward-line 1))))
991 (set-marker end nil))
992
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000993
994;; Functions for moving point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000995(defun py-previous-statement (count)
996 "Go to the start of previous Python statement.
997If the statement at point is the i'th Python statement, goes to the
998start of statement i-COUNT. If there is no such statement, goes to the
999first statement. Returns count of statements left to move.
1000`Statements' do not include blank, comment, or continuation lines."
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001001 (interactive "p") ; numeric prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +00001002 (if (< count 0) (py-next-statement (- count))
1003 (py-goto-initial-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001004 (let (start)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001005 (while (and
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001006 (setq start (point)) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +00001007 (> count 0)
1008 (zerop (forward-line -1))
1009 (py-goto-statement-at-or-above))
1010 (setq count (1- count)))
1011 (if (> count 0) (goto-char start)))
1012 count))
1013
1014(defun py-next-statement (count)
1015 "Go to the start of next Python statement.
1016If the statement at point is the i'th Python statement, goes to the
1017start of statement i+COUNT. If there is no such statement, goes to the
1018last statement. Returns count of statements left to move. `Statements'
1019do not include blank, comment, or continuation lines."
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001020 (interactive "p") ; numeric prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +00001021 (if (< count 0) (py-previous-statement (- count))
1022 (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001023 (let (start)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001024 (while (and
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001025 (setq start (point)) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +00001026 (> count 0)
1027 (py-goto-statement-below))
1028 (setq count (1- count)))
1029 (if (> count 0) (goto-char start)))
1030 count))
1031
1032(defun py-goto-block-up (&optional nomark)
1033 "Move up to start of current block.
1034Go to the statement that starts the smallest enclosing block; roughly
1035speaking, this will be the closest preceding statement that ends with a
1036colon and is indented less than the statement you started on. If
1037successful, also sets the mark to the starting point.
1038
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001039`\\[py-mark-block]' can be used afterward to mark the whole code
1040block, if desired.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001041
1042If called from a program, the mark will not be set if optional argument
1043NOMARK is not nil."
1044 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001045 (let ((start (point))
1046 (found nil)
1047 initial-indent)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001048 (py-goto-initial-line)
1049 ;; if on blank or non-indenting comment line, use the preceding stmt
1050 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1051 (progn
1052 (py-goto-statement-at-or-above)
1053 (setq found (py-statement-opens-block-p))))
1054 ;; search back for colon line indented less
1055 (setq initial-indent (current-indentation))
1056 (if (zerop initial-indent)
1057 ;; force fast exit
1058 (goto-char (point-min)))
1059 (while (not (or found (bobp)))
1060 (setq found
1061 (and
1062 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1063 (or (py-goto-initial-line) t) ; always true -- side effect
1064 (< (current-indentation) initial-indent)
1065 (py-statement-opens-block-p))))
1066 (if found
1067 (progn
1068 (or nomark (push-mark start))
1069 (back-to-indentation))
1070 (goto-char start)
1071 (error "Enclosing block not found"))))
1072
1073(defun beginning-of-python-def-or-class (&optional class)
1074 "Move point to start of def (or class, with prefix arg).
1075
1076Searches back for the closest preceding `def'. If you supply a prefix
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001077arg, looks for a `class' instead. The docs assume the `def' case;
1078just substitute `class' for `def' for the other case.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001079
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001080If point is in a def statement already, and after the `d', simply
1081moves point to the start of the statement.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001082
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001083Else (point is not in a def statement, or at or before the `d' of a
1084def statement), searches for the closest preceding def statement, and
1085leaves point at its start. If no such statement can be found, leaves
1086point at the start of the buffer.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001087
1088Returns t iff a def statement is found by these rules.
1089
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001090Note that doing this command repeatedly will take you closer to the
1091start of the buffer each time.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001092
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001093If you want to mark the current def/class, see
1094`\\[mark-python-def-or-class]'."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001095 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001096 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1097 (start-of-line (progn (beginning-of-line) (point)))
1098 (start-of-stmt (progn (py-goto-initial-line) (point))))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001099 (if (or (/= start-of-stmt start-of-line)
1100 (not at-or-before-p))
1101 (end-of-line)) ; OK to match on this line
1102 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001103 nil 'move)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001104
1105(defun end-of-python-def-or-class (&optional class)
1106 "Move point beyond end of def (or class, with prefix arg) body.
1107
1108By default, looks for an appropriate `def'. If you supply a prefix arg,
1109looks for a `class' instead. The docs assume the `def' case; just
1110substitute `class' for `def' for the other case.
1111
1112If point is in a def statement already, this is the def we use.
1113
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001114Else if the def found by `\\[beginning-of-python-def-or-class]'
1115contains the statement you started on, that's the def we use.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001116
1117Else we search forward for the closest following def, and use that.
1118
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001119If a def can be found by these rules, point is moved to the start of
1120the line immediately following the def block, and the position of the
1121start of the def is returned.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001122
1123Else point is moved to the end of the buffer, and nil is returned.
1124
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001125Note that doing this command repeatedly will take you closer to the
1126end of the buffer each time.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001127
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001128If you want to mark the current def/class, see
1129`\\[mark-python-def-or-class]'."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001130 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001131 (let ((start (progn (py-goto-initial-line) (point)))
1132 (which (if class "class" "def"))
1133 (state 'not-found))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001134 ;; move point to start of appropriate def/class
1135 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1136 (setq state 'at-beginning)
1137 ;; else see if beginning-of-python-def-or-class hits container
1138 (if (and (beginning-of-python-def-or-class class)
1139 (progn (py-goto-beyond-block)
1140 (> (point) start)))
1141 (setq state 'at-end)
1142 ;; else search forward
1143 (goto-char start)
1144 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1145 (progn (setq state 'at-beginning)
1146 (beginning-of-line)))))
1147 (cond
1148 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1149 ((eq state 'at-end) t)
1150 ((eq state 'not-found) nil)
1151 (t (error "internal error in end-of-python-def-or-class")))))
1152
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001153
1154;; Functions for marking regions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001155(defun py-mark-block (&optional extend just-move)
1156 "Mark following block of lines. With prefix arg, mark structure.
1157Easier to use than explain. It sets the region to an `interesting'
1158block of succeeding lines. If point is on a blank line, it goes down to
1159the next non-blank line. That will be the start of the region. The end
1160of the region depends on the kind of line at the start:
1161
1162 - If a comment, the region will include all succeeding comment lines up
1163 to (but not including) the next non-comment line (if any).
1164
1165 - Else if a prefix arg is given, and the line begins one of these
1166 structures:
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001167
1168 if elif else try except finally for while def class
1169
Guido van Rossuma7925f11994-01-26 10:20:16 +00001170 the region will be set to the body of the structure, including
1171 following blocks that `belong' to it, but excluding trailing blank
1172 and comment lines. E.g., if on a `try' statement, the `try' block
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001173 and all (if any) of the following `except' and `finally' blocks
1174 that belong to the `try' structure will be in the region. Ditto
1175 for if/elif/else, for/else and while/else structures, and (a bit
1176 degenerate, since they're always one-block structures) def and
1177 class blocks.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001178
1179 - Else if no prefix argument is given, and the line begins a Python
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001180 block (see list above), and the block is not a `one-liner' (i.e.,
1181 the statement ends with a colon, not with code), the region will
1182 include all succeeding lines up to (but not including) the next
1183 code statement (if any) that's indented no more than the starting
1184 line, except that trailing blank and comment lines are excluded.
1185 E.g., if the starting line begins a multi-statement `def'
1186 structure, the region will be set to the full function definition,
1187 but without any trailing `noise' lines.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001188
1189 - Else the region will include all succeeding lines up to (but not
1190 including) the next blank line, or code or indenting-comment line
1191 indented strictly less than the starting line. Trailing indenting
1192 comment lines are included in this case, but not trailing blank
1193 lines.
1194
1195A msg identifying the location of the mark is displayed in the echo
1196area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1197
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001198If called from a program, optional argument EXTEND plays the role of
1199the prefix arg, and if optional argument JUST-MOVE is not nil, just
1200moves to the end of the block (& does not set mark or display a msg)."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001201 (interactive "P") ; raw prefix arg
1202 (py-goto-initial-line)
1203 ;; skip over blank lines
1204 (while (and
1205 (looking-at "[ \t]*$") ; while blank line
1206 (not (eobp))) ; & somewhere to go
1207 (forward-line 1))
1208 (if (eobp)
1209 (error "Hit end of buffer without finding a non-blank stmt"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001210 (let ((initial-pos (point))
1211 (initial-indent (current-indentation))
1212 last-pos ; position of last stmt in region
1213 (followers
1214 '((if elif else) (elif elif else) (else)
1215 (try except finally) (except except) (finally)
1216 (for else) (while else)
1217 (def) (class) ) )
1218 first-symbol next-symbol)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001219
1220 (cond
1221 ;; if comment line, suck up the following comment lines
1222 ((looking-at "[ \t]*#")
1223 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1224 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1225 (setq last-pos (point)))
1226
1227 ;; else if line is a block line and EXTEND given, suck up
1228 ;; the whole structure
1229 ((and extend
1230 (setq first-symbol (py-suck-up-first-keyword) )
1231 (assq first-symbol followers))
1232 (while (and
1233 (or (py-goto-beyond-block) t) ; side effect
1234 (forward-line -1) ; side effect
1235 (setq last-pos (point)) ; side effect
1236 (py-goto-statement-below)
1237 (= (current-indentation) initial-indent)
1238 (setq next-symbol (py-suck-up-first-keyword))
1239 (memq next-symbol (cdr (assq first-symbol followers))))
1240 (setq first-symbol next-symbol)))
1241
1242 ;; else if line *opens* a block, search for next stmt indented <=
1243 ((py-statement-opens-block-p)
1244 (while (and
1245 (setq last-pos (point)) ; always true -- side effect
1246 (py-goto-statement-below)
1247 (> (current-indentation) initial-indent))
1248 nil))
1249
1250 ;; else plain code line; stop at next blank line, or stmt or
1251 ;; indenting comment line indented <
1252 (t
1253 (while (and
1254 (setq last-pos (point)) ; always true -- side effect
1255 (or (py-goto-beyond-final-line) t)
1256 (not (looking-at "[ \t]*$")) ; stop at blank line
1257 (or
1258 (>= (current-indentation) initial-indent)
1259 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1260 nil)))
1261
1262 ;; skip to end of last stmt
1263 (goto-char last-pos)
1264 (py-goto-beyond-final-line)
1265
1266 ;; set mark & display
1267 (if just-move
1268 () ; just return
1269 (push-mark (point) 'no-msg)
1270 (forward-line -1)
1271 (message "Mark set after: %s" (py-suck-up-leading-text))
1272 (goto-char initial-pos))))
1273
1274(defun mark-python-def-or-class (&optional class)
1275 "Set region to body of def (or class, with prefix arg) enclosing point.
1276Pushes the current mark, then point, on the mark ring (all language
1277modes do this, but although it's handy it's never documented ...).
1278
1279In most Emacs language modes, this function bears at least a
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001280hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1281`\\[beginning-of-python-def-or-class]'.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001282
1283And in earlier versions of Python mode, all 3 were tightly connected.
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001284Turned out that was more confusing than useful: the `goto start' and
1285`goto end' commands are usually used to search through a file, and
1286people expect them to act a lot like `search backward' and `search
1287forward' string-search commands. But because Python `def' and `class'
1288can nest to arbitrary levels, finding the smallest def containing
1289point cannot be done via a simple backward search: the def containing
1290point may not be the closest preceding def, or even the closest
1291preceding def that's indented less. The fancy algorithm required is
1292appropriate for the usual uses of this `mark' command, but not for the
1293`goto' variations.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001294
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001295So the def marked by this command may not be the one either of the
1296`goto' commands find: If point is on a blank or non-indenting comment
1297line, moves back to start of the closest preceding code statement or
1298indenting comment line. If this is a `def' statement, that's the def
1299we use. Else searches for the smallest enclosing `def' block and uses
1300that. Else signals an error.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001301
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001302When an enclosing def is found: The mark is left immediately beyond
1303the last line of the def block. Point is left at the start of the
1304def, except that: if the def is preceded by a number of comment lines
1305followed by (at most) one optional blank line, point is left at the
1306start of the comments; else if the def is preceded by a blank line,
1307point is left at its start.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001308
1309The intent is to mark the containing def/class and its associated
1310documentation, to make moving and duplicating functions and classes
1311pleasant."
1312 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001313 (let ((start (point))
1314 (which (if class "class" "def")))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001315 (push-mark start)
1316 (if (not (py-go-up-tree-to-keyword which))
1317 (progn (goto-char start)
1318 (error "Enclosing %s not found" which))
1319 ;; else enclosing def/class found
1320 (setq start (point))
1321 (py-goto-beyond-block)
1322 (push-mark (point))
1323 (goto-char start)
1324 (if (zerop (forward-line -1)) ; if there is a preceding line
1325 (progn
1326 (if (looking-at "[ \t]*$") ; it's blank
1327 (setq start (point)) ; so reset start point
1328 (goto-char start)) ; else try again
1329 (if (zerop (forward-line -1))
1330 (if (looking-at "[ \t]*#") ; a comment
1331 ;; look back for non-comment line
1332 ;; tricky: note that the regexp matches a blank
1333 ;; line, cuz \n is in the 2nd character class
1334 (and
1335 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1336 (forward-line 1))
1337 ;; no comment, so go back
1338 (goto-char start))))))))
1339
1340(defun py-comment-region (start end &optional uncomment-p)
1341 "Comment out region of code; with prefix arg, uncomment region.
1342The lines from the line containing the start of the current region up
1343to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001344commented out, by inserting the string `py-block-comment-prefix' at
1345the start of each line. With a prefix arg, removes
1346`py-block-comment-prefix' from the start of each line instead."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001347 (interactive "*r\nP") ; region; raw prefix arg
1348 (goto-char end) (beginning-of-line) (setq end (point))
1349 (goto-char start) (beginning-of-line) (setq start (point))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001350 (let ((prefix-len (length py-block-comment-prefix)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001351 (save-excursion
1352 (save-restriction
1353 (narrow-to-region start end)
1354 (while (not (eobp))
1355 (if uncomment-p
1356 (and (string= py-block-comment-prefix
1357 (buffer-substring
1358 (point) (+ (point) prefix-len)))
1359 (delete-char prefix-len))
1360 (insert py-block-comment-prefix))
1361 (forward-line 1))))))
1362
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001363
1364;; Documentation functions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001365
1366;; dump the long form of the mode blurb; does the usual doc escapes,
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001367;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1368;; out of the right places, along with the keys they're on & current
1369;; values
Guido van Rossuma7925f11994-01-26 10:20:16 +00001370(defun py-dump-help-string (str)
1371 (with-output-to-temp-buffer "*Help*"
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001372 (let ((locals (buffer-local-variables))
1373 funckind funcname func funcdoc
1374 (start 0) mstart end
1375 keys )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001376 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1377 (setq mstart (match-beginning 0) end (match-end 0)
1378 funckind (substring str (match-beginning 1) (match-end 1))
1379 funcname (substring str (match-beginning 2) (match-end 2))
1380 func (intern funcname))
1381 (princ (substitute-command-keys (substring str start mstart)))
1382 (cond
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001383 ((equal funckind "c") ; command
1384 (setq funcdoc (documentation func)
1385 keys (concat
1386 "Key(s): "
1387 (mapconcat 'key-description
1388 (where-is-internal func py-mode-map)
1389 ", "))))
1390 ((equal funckind "v") ; variable
1391 (setq funcdoc (substitute-command-keys
1392 (get func 'variable-documentation))
1393 keys (if (assq func locals)
1394 (concat
1395 "Local/Global values: "
1396 (prin1-to-string (symbol-value func))
1397 " / "
1398 (prin1-to-string (default-value func)))
1399 (concat
1400 "Value: "
1401 (prin1-to-string (symbol-value func))))))
1402 (t ; unexpected
1403 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001404 (princ (format "\n-> %s:\t%s\t%s\n\n"
1405 (if (equal funckind "c") "Command" "Variable")
1406 funcname keys))
1407 (princ funcdoc)
1408 (terpri)
1409 (setq start end))
1410 (princ (substitute-command-keys (substring str start))))
1411 (print-help-return-message)))
1412
1413(defun py-describe-mode ()
1414 "Dump long form of Python-mode docs."
1415 (interactive)
1416 (py-dump-help-string "Major mode for editing Python files.
1417Knows about Python indentation, tokens, comments and continuation lines.
1418Paragraphs are separated by blank lines only.
1419
1420Major sections below begin with the string `@'; specific function and
1421variable docs begin with `->'.
1422
1423@EXECUTING PYTHON CODE
1424
1425\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1426\\[py-execute-region]\tsends the current region
1427\\[py-shell]\tstarts a Python interpreter window; this will be used by
1428\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1429%c:py-execute-buffer
1430%c:py-execute-region
1431%c:py-shell
1432
1433@VARIABLES
1434
1435py-indent-offset\tindentation increment
Guido van Rossuma7925f11994-01-26 10:20:16 +00001436py-block-comment-prefix\tcomment string used by py-comment-region
1437
1438py-python-command\tshell command to invoke Python interpreter
1439py-scroll-process-buffer\talways scroll Python process buffer
1440py-temp-directory\tdirectory used for temp files (if needed)
1441
1442py-beep-if-tab-change\tring the bell if tab-width is changed
1443%v:py-indent-offset
Guido van Rossuma7925f11994-01-26 10:20:16 +00001444%v:py-block-comment-prefix
1445%v:py-python-command
1446%v:py-scroll-process-buffer
1447%v:py-temp-directory
1448%v:py-beep-if-tab-change
1449
1450@KINDS OF LINES
1451
1452Each physical line in the file is either a `continuation line' (the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001453preceding line ends with a backslash that's not part of a comment, or
1454the paren/bracket/brace nesting level at the start of the line is
1455non-zero, or both) or an `initial line' (everything else).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001456
1457An initial line is in turn a `blank line' (contains nothing except
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001458possibly blanks or tabs), a `comment line' (leftmost non-blank
1459character is `#'), or a `code line' (everything else).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001460
1461Comment Lines
1462
1463Although all comment lines are treated alike by Python, Python mode
1464recognizes two kinds that act differently with respect to indentation.
1465
1466An `indenting comment line' is a comment line with a blank, tab or
1467nothing after the initial `#'. The indentation commands (see below)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001468treat these exactly as if they were code lines: a line following an
Guido van Rossuma7925f11994-01-26 10:20:16 +00001469indenting comment line will be indented like the comment line. All
1470other comment lines (those with a non-whitespace character immediately
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001471following the initial `#') are `non-indenting comment lines', and
1472their indentation is ignored by the indentation commands.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001473
1474Indenting comment lines are by far the usual case, and should be used
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001475whenever possible. Non-indenting comment lines are useful in cases
1476like these:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001477
1478\ta = b # a very wordy single-line comment that ends up being
1479\t #... continued onto another line
1480
1481\tif a == b:
1482##\t\tprint 'panic!' # old code we've `commented out'
1483\t\treturn a
1484
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001485Since the `#...' and `##' comment lines have a non-whitespace
1486character following the initial `#', Python mode ignores them when
1487computing the proper indentation for the next line.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001488
1489Continuation Lines and Statements
1490
1491The Python-mode commands generally work on statements instead of on
1492individual lines, where a `statement' is a comment or blank line, or a
1493code line and all of its following continuation lines (if any)
1494considered as a single logical unit. The commands in this mode
1495generally (when it makes sense) automatically move to the start of the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001496statement containing point, even if point happens to be in the middle
1497of some continuation line.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001498
Guido van Rossuma7925f11994-01-26 10:20:16 +00001499
1500@INDENTATION
1501
1502Primarily for entering new code:
1503\t\\[indent-for-tab-command]\t indent line appropriately
1504\t\\[py-newline-and-indent]\t insert newline, then indent
1505\t\\[py-delete-char]\t reduce indentation, or delete single character
1506
1507Primarily for reindenting existing code:
1508\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1509\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1510
1511\t\\[py-indent-region]\t reindent region to match its context
1512\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1513\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1514
1515Unlike most programming languages, Python uses indentation, and only
1516indentation, to specify block structure. Hence the indentation supplied
1517automatically by Python-mode is just an educated guess: only you know
1518the block structure you intend, so only you can supply correct
1519indentation.
1520
1521The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1522the indentation of preceding statements. E.g., assuming
1523py-indent-offset is 4, after you enter
1524\tif a > 0: \\[py-newline-and-indent]
1525the cursor will be moved to the position of the `_' (_ is not a
1526character in the file, it's just used here to indicate the location of
1527the cursor):
1528\tif a > 0:
1529\t _
1530If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1531to
1532\tif a > 0:
1533\t c = d
1534\t _
1535Python-mode cannot know whether that's what you intended, or whether
1536\tif a > 0:
1537\t c = d
1538\t_
1539was your intent. In general, Python-mode either reproduces the
1540indentation of the (closest code or indenting-comment) preceding
1541statement, or adds an extra py-indent-offset blanks if the preceding
1542statement has `:' as its last significant (non-whitespace and non-
1543comment) character. If the suggested indentation is too much, use
1544\\[py-delete-char] to reduce it.
1545
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +00001546Continuation lines are given extra indentation. If you don't like the
1547suggested indentation, change it to something you do like, and Python-
1548mode will strive to indent later lines of the statement in the same way.
1549
1550If a line is a continuation line by virtue of being in an unclosed
1551paren/bracket/brace structure (`list', for short), the suggested
Guido van Rossum9274e2d1994-04-26 07:35:17 +00001552indentation depends on whether the current line contains the first item
1553in the list. If it does, it's indented py-indent-offset columns beyond
1554the indentation of the line containing the open bracket. If you don't
1555like that, change it by hand. The remaining items in the list will mimic
1556whatever indentation you give to the first item.
Guido van Rossume531e4b1994-04-16 08:29:27 +00001557
1558If a line is a continuation line because the line preceding it ends with
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +00001559a backslash, the third and following lines of the statement inherit their
1560indentation from the line preceding them. The indentation of the second
1561line in the statement depends on the form of the first (base) line: if
1562the base line is an assignment statement with anything more interesting
1563than the backslash following the leftmost assigning `=', the second line
1564is indented two columns beyond that `='. Else it's indented to two
1565columns beyond the leftmost solid chunk of non-whitespace characters on
1566the base line.
Guido van Rossume531e4b1994-04-16 08:29:27 +00001567
Guido van Rossuma7925f11994-01-26 10:20:16 +00001568Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1569repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1570structure you intend.
1571%c:indent-for-tab-command
1572%c:py-newline-and-indent
1573%c:py-delete-char
1574
1575
1576The next function may be handy when editing code you didn't write:
1577%c:py-guess-indent-offset
1578
1579
1580The remaining `indent' functions apply to a region of Python code. They
1581assume the block structure (equals indentation, in Python) of the region
1582is correct, and alter the indentation in various ways while preserving
1583the block structure:
1584%c:py-indent-region
1585%c:py-shift-region-left
1586%c:py-shift-region-right
1587
1588@MARKING & MANIPULATING REGIONS OF CODE
1589
1590\\[py-mark-block]\t mark block of lines
1591\\[mark-python-def-or-class]\t mark smallest enclosing def
1592\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
1593\\[py-comment-region]\t comment out region of code
1594\\[universal-argument] \\[py-comment-region]\t uncomment region of code
1595%c:py-mark-block
1596%c:mark-python-def-or-class
1597%c:py-comment-region
1598
1599@MOVING POINT
1600
1601\\[py-previous-statement]\t move to statement preceding point
1602\\[py-next-statement]\t move to statement following point
1603\\[py-goto-block-up]\t move up to start of current block
1604\\[beginning-of-python-def-or-class]\t move to start of def
1605\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1606\\[end-of-python-def-or-class]\t move to end of def
1607\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1608
1609The first two move to one statement beyond the statement that contains
1610point. A numeric prefix argument tells them to move that many
1611statements instead. Blank lines, comment lines, and continuation lines
1612do not count as `statements' for these commands. So, e.g., you can go
1613to the first code statement in a file by entering
1614\t\\[beginning-of-buffer]\t to move to the top of the file
1615\t\\[py-next-statement]\t to skip over initial comments and blank lines
1616Or do `\\[py-previous-statement]' with a huge prefix argument.
1617%c:py-previous-statement
1618%c:py-next-statement
1619%c:py-goto-block-up
1620%c:beginning-of-python-def-or-class
1621%c:end-of-python-def-or-class
1622
1623@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
1624
1625`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
1626
1627`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
1628overall class and def structure of a module.
1629
1630`\\[back-to-indentation]' moves point to a line's first non-blank character.
1631
1632`\\[indent-relative]' is handy for creating odd indentation.
1633
1634@OTHER EMACS HINTS
1635
1636If you don't like the default value of a variable, change its value to
1637whatever you do like by putting a `setq' line in your .emacs file.
1638E.g., to set the indentation increment to 4, put this line in your
1639.emacs:
1640\t(setq py-indent-offset 4)
1641To see the value of a variable, do `\\[describe-variable]' and enter the variable
1642name at the prompt.
1643
1644When entering a key sequence like `C-c C-n', it is not necessary to
1645release the CONTROL key after doing the `C-c' part -- it suffices to
1646press the CONTROL key, press and release `c' (while still holding down
1647CONTROL), press and release `n' (while still holding down CONTROL), &
1648then release CONTROL.
1649
1650Entering Python mode calls with no arguments the value of the variable
Guido van Rossuma67bb7e1994-11-10 23:01:59 +00001651`python-mode-hook', if that value exists and is not nil; for backward
1652compatibility it also tries `py-mode-hook'; see the `Hooks' section of
1653the Elisp manual for details.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001654
1655Obscure: When python-mode is first loaded, it looks for all bindings
1656to newline-and-indent in the global keymap, and shadows them with
1657local bindings to py-newline-and-indent."))
1658
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001659
1660;; Helper functions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001661(defvar py-parse-state-re
1662 (concat
1663 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
1664 "\\|"
1665 "^[^ #\t\n]"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001666
Guido van Rossuma7925f11994-01-26 10:20:16 +00001667;; returns the parse state at point (see parse-partial-sexp docs)
1668(defun py-parse-state ()
1669 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001670 (let ((here (point)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001671 ;; back up to the first preceding line (if any; else start of
1672 ;; buffer) that begins with a popular Python keyword, or a non-
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001673 ;; whitespace and non-comment character. These are good places
1674 ;; to start parsing to see whether where we started is at a
1675 ;; non-zero nesting level. It may be slow for people who write
1676 ;; huge code blocks or huge lists ... tough beans.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001677 (re-search-backward py-parse-state-re nil 'move)
1678 (beginning-of-line)
1679 (parse-partial-sexp (point) here))))
1680
1681;; if point is at a non-zero nesting level, returns the number of the
1682;; character that opens the smallest enclosing unclosed list; else
1683;; returns nil.
1684(defun py-nesting-level ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001685 (let ((status (py-parse-state)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001686 (if (zerop (car status))
1687 nil ; not in a nest
1688 (car (cdr status))))) ; char# of open bracket
1689
1690;; t iff preceding line ends with backslash that's not in a comment
1691(defun py-backslash-continuation-line-p ()
1692 (save-excursion
1693 (beginning-of-line)
1694 (and
1695 ;; use a cheap test first to avoid the regexp if possible
1696 ;; use 'eq' because char-after may return nil
1697 (eq (char-after (- (point) 2)) ?\\ )
1698 ;; make sure; since eq test passed, there is a preceding line
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001699 (forward-line -1) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +00001700 (looking-at py-continued-re))))
1701
1702;; t iff current line is a continuation line
1703(defun py-continuation-line-p ()
1704 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001705 (beginning-of-line)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001706 (or (py-backslash-continuation-line-p)
1707 (py-nesting-level))))
1708
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001709;; go to initial line of current statement; usually this is the line
1710;; we're on, but if we're on the 2nd or following lines of a
1711;; continuation block, we need to go up to the first line of the
1712;; block.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001713;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001714;; Tricky: We want to avoid quadratic-time behavior for long continued
1715;; blocks, whether of the backslash or open-bracket varieties, or a
1716;; mix of the two. The following manages to do that in the usual
1717;; cases.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001718(defun py-goto-initial-line ()
1719 (let ( open-bracket-pos )
1720 (while (py-continuation-line-p)
1721 (beginning-of-line)
1722 (if (py-backslash-continuation-line-p)
1723 (while (py-backslash-continuation-line-p)
1724 (forward-line -1))
1725 ;; else zip out of nested brackets/braces/parens
1726 (while (setq open-bracket-pos (py-nesting-level))
1727 (goto-char open-bracket-pos)))))
1728 (beginning-of-line))
1729
1730;; go to point right beyond final line of current statement; usually
1731;; this is the start of the next line, but if this is a multi-line
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001732;; statement we need to skip over the continuation lines. Tricky:
1733;; Again we need to be clever to avoid quadratic time behavior.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001734(defun py-goto-beyond-final-line ()
1735 (forward-line 1)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001736 (let (state)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001737 (while (and (py-continuation-line-p)
1738 (not (eobp)))
1739 ;; skip over the backslash flavor
1740 (while (and (py-backslash-continuation-line-p)
1741 (not (eobp)))
1742 (forward-line 1))
1743 ;; if in nest, zip to the end of the nest
1744 (setq state (py-parse-state))
1745 (if (and (not (zerop (car state)))
1746 (not (eobp)))
1747 (progn
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001748 ;; BUG ALERT: I could swear, from reading the docs, that
Guido van Rossuma7925f11994-01-26 10:20:16 +00001749 ;; the 3rd argument should be plain 0
1750 (parse-partial-sexp (point) (point-max) (- 0 (car state))
1751 nil state)
1752 (forward-line 1))))))
1753
1754;; t iff statement opens a block == iff it ends with a colon that's
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001755;; not in a comment. point should be at the start of a statement
Guido van Rossuma7925f11994-01-26 10:20:16 +00001756(defun py-statement-opens-block-p ()
1757 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001758 (let ((start (point))
1759 (finish (progn (py-goto-beyond-final-line) (1- (point))))
1760 (searching t)
1761 (answer nil)
1762 state)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001763 (goto-char start)
1764 (while searching
1765 ;; look for a colon with nothing after it except whitespace, and
1766 ;; maybe a comment
1767 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
1768 finish t)
1769 (if (eq (point) finish) ; note: no `else' clause; just
1770 ; keep searching if we're not at
1771 ; the end yet
1772 ;; sure looks like it opens a block -- but it might
1773 ;; be in a comment
1774 (progn
1775 (setq searching nil) ; search is done either way
1776 (setq state (parse-partial-sexp start
1777 (match-beginning 0)))
1778 (setq answer (not (nth 4 state)))))
1779 ;; search failed: couldn't find another interesting colon
1780 (setq searching nil)))
1781 answer)))
1782
1783;; go to point right beyond final line of block begun by the current
1784;; line. This is the same as where py-goto-beyond-final-line goes
1785;; unless we're on colon line, in which case we go to the end of the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001786;; block. assumes point is at bolp
Guido van Rossuma7925f11994-01-26 10:20:16 +00001787(defun py-goto-beyond-block ()
1788 (if (py-statement-opens-block-p)
1789 (py-mark-block nil 'just-move)
1790 (py-goto-beyond-final-line)))
1791
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001792;; go to start of first statement (not blank or comment or
1793;; continuation line) at or preceding point. returns t if there is
1794;; one, else nil
Guido van Rossuma7925f11994-01-26 10:20:16 +00001795(defun py-goto-statement-at-or-above ()
1796 (py-goto-initial-line)
1797 (if (looking-at py-blank-or-comment-re)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001798 ;; skip back over blank & comment lines
1799 ;; note: will skip a blank or comment line that happens to be
1800 ;; a continuation line too
1801 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
1802 (progn (py-goto-initial-line) t)
1803 nil)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001804 t))
1805
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001806;; go to start of first statement (not blank or comment or
1807;; continuation line) following the statement containing point returns
1808;; t if there is one, else nil
Guido van Rossuma7925f11994-01-26 10:20:16 +00001809(defun py-goto-statement-below ()
1810 (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001811 (let ((start (point)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001812 (py-goto-beyond-final-line)
1813 (while (and
1814 (looking-at py-blank-or-comment-re)
1815 (not (eobp)))
1816 (forward-line 1))
1817 (if (eobp)
1818 (progn (goto-char start) nil)
1819 t)))
1820
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001821;; go to start of statement, at or preceding point, starting with
1822;; keyword KEY. Skips blank lines and non-indenting comments upward
1823;; first. If that statement starts with KEY, done, else go back to
1824;; first enclosing block starting with KEY. If successful, leaves
1825;; point at the start of the KEY line & returns t. Else leaves point
1826;; at an undefined place & returns nil.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001827(defun py-go-up-tree-to-keyword (key)
1828 ;; skip blanks and non-indenting #
1829 (py-goto-initial-line)
1830 (while (and
1831 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1832 (zerop (forward-line -1))) ; go back
1833 nil)
1834 (py-goto-initial-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001835 (let* ((re (concat "[ \t]*" key "\\b"))
1836 (case-fold-search nil) ; let* so looking-at sees this
1837 (found (looking-at re))
1838 (dead nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001839 (while (not (or found dead))
1840 (condition-case nil ; in case no enclosing block
1841 (py-goto-block-up 'no-mark)
1842 (error (setq dead t)))
1843 (or dead (setq found (looking-at re))))
1844 (beginning-of-line)
1845 found))
1846
1847;; return string in buffer from start of indentation to end of line;
1848;; prefix "..." if leading whitespace was skipped
1849(defun py-suck-up-leading-text ()
1850 (save-excursion
1851 (back-to-indentation)
1852 (concat
1853 (if (bolp) "" "...")
1854 (buffer-substring (point) (progn (end-of-line) (point))))))
1855
1856;; assuming point at bolp, return first keyword ([a-z]+) on the line,
1857;; as a Lisp symbol; return nil if none
1858(defun py-suck-up-first-keyword ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001859 (let ((case-fold-search nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001860 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
1861 (intern (buffer-substring (match-beginning 1) (match-end 1)))
1862 nil)))
1863
1864(defun py-make-temp-name ()
1865 (make-temp-name
1866 (concat (file-name-as-directory py-temp-directory) "python")))
1867
1868(defun py-delete-file-silently (fname)
1869 (condition-case nil
1870 (delete-file fname)
1871 (error nil)))
1872
1873(defun py-kill-emacs-hook ()
1874 ;; delete our temp files
1875 (while py-file-queue
1876 (py-delete-file-silently (car py-file-queue))
1877 (setq py-file-queue (cdr py-file-queue)))
Guido van Rossum581d1721994-04-28 08:31:52 +00001878 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001879 ;; run the hook we inherited, if any
1880 (and py-inherited-kill-emacs-hook
1881 (funcall py-inherited-kill-emacs-hook))))
1882
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001883;; make PROCESS's buffer visible, append STRING to it, and force
1884;; display; also make shell-mode believe the user typed this string,
1885;; so that kill-output-from-shell and show-output-from-shell work
1886;; "right"
Guido van Rossuma7925f11994-01-26 10:20:16 +00001887(defun py-append-to-process-buffer (process string)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001888 (let ((cbuf (current-buffer))
1889 (pbuf (process-buffer process))
1890 (py-scroll-process-buffer t))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001891 (set-buffer pbuf)
1892 (goto-char (point-max))
1893 (move-marker (process-mark process) (point))
1894 (if (not py-this-is-emacs-19-p)
1895 (move-marker last-input-start (point))) ; muck w/ shell-mode
1896 (funcall (process-filter process) process string)
1897 (if (not py-this-is-emacs-19-p)
1898 (move-marker last-input-end (point))) ; muck w/ shell-mode
1899 (set-buffer cbuf))
1900 (sit-for 0))
1901
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001902(defun py-keep-region-active ()
1903 ;; do whatever is necessary to keep the region active in XEmacs.
1904 ;; Ignore byte-compiler warnings you might see. Also note that
1905 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
1906 ;; require us to take explicit action.
1907 (and (boundp 'zmacs-region-stays)
1908 (setq zmacs-region-stays t)))
1909
1910
Guido van Rossum2ed53541995-03-15 19:57:14 +00001911(defconst py-version "2.16"
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001912 "`python-mode' version number.")
1913(defconst py-help-address "bwarsaw@cnri.reston.va.us"
1914 "Address accepting submission of bug reports.")
1915
1916(defun py-version ()
1917 "Echo the current version of `python-mode' in the minibuffer."
1918 (interactive)
1919 (message "Using `python-mode' version %s" py-version)
1920 (py-keep-region-active))
1921
1922;; only works under Emacs 19
1923;(eval-when-compile
1924; (require 'reporter))
1925
1926(defun py-submit-bug-report (enhancement-p)
1927 "Submit via mail a bug report on `python-mode'.
1928With \\[universal-argument] just submit an enhancement request."
1929 (interactive
1930 (list (not (y-or-n-p
1931 "Is this a bug report? (hit `n' to send other comments) "))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001932 (let ((reporter-prompt-for-summary-p (if enhancement-p
1933 "(Very) brief summary: "
1934 t)))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001935 (require 'reporter)
1936 (reporter-submit-bug-report
1937 py-help-address ;address
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001938 (concat "python-mode " py-version) ;pkgname
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001939 ;; varlist
1940 (if enhancement-p nil
1941 '(py-python-command
1942 py-indent-offset
1943 py-block-comment-prefix
1944 py-scroll-process-buffer
1945 py-temp-directory
1946 py-beep-if-tab-change))
1947 nil ;pre-hooks
1948 nil ;post-hooks
1949 "Dear Barry,") ;salutation
1950 (if enhancement-p nil
1951 (set-mark (point))
1952 (insert
1953"Please replace this text with a sufficiently large code sample\n\
1954and an exact recipe so that I can reproduce your problem. Failure\n\
1955to do so may mean a greater delay in fixing your bug.\n\n")
1956 (exchange-point-and-mark)
1957 (py-keep-region-active))))
1958
1959
1960;; arrange to kill temp files when Emacs exists
1961(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
1962 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
1963 ;; have to trust that other people are as respectful of our hook
1964 ;; fiddling as we are of theirs
1965 (if (boundp 'py-inherited-kill-emacs-hook)
1966 ;; we were loaded before -- trust others not to have screwed us
1967 ;; in the meantime (no choice, really)
1968 nil
1969 ;; else arrange for our hook to run theirs
1970 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
1971 (setq kill-emacs-hook 'py-kill-emacs-hook)))
1972
1973
1974
1975(provide 'python-mode)
1976;;; python-mode.el ends here