annotate Misc/python-mode.el @ 3220:f1cc247fff13 legacy-trunk

(py-indent-offset): this should be buffer local.
author Barry Warsaw <barry@python.org>
date Thu, 12 Oct 1995 21:15:49 +0000
parents 3aab925ebfb0
children 78e1953fc9b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1 ;;; python-mode.el --- Major mode for editing Python programs
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
3 ;; Copyright (C) 1992,1993,1994 Tim Peters
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
4
2799
4a998b1a331f #changed all email address to go through python.org
Barry Warsaw <barry@python.org>
parents: 2798
diff changeset
5 ;; Author: 1995 Barry A. Warsaw
4a998b1a331f #changed all email address to go through python.org
Barry Warsaw <barry@python.org>
parents: 2798
diff changeset
6 ;; 1992-1994 Tim Peters
4a998b1a331f #changed all email address to go through python.org
Barry Warsaw <barry@python.org>
parents: 2798
diff changeset
7 ;; Maintainer: python-mode@python.org
2524
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
8 ;; Created: Feb 1992
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
9 ;; Version: $Revision$
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
10 ;; Last Modified: $Date$
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
11 ;; Keywords: python editing language major-mode
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
12
2524
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
13 ;; This software is provided as-is, without express or implied
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
14 ;; warranty. Permission to use, copy, modify, distribute or sell this
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
15 ;; software, without fee, for any purpose and by any individual or
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
16 ;; organization, is hereby granted, provided that the above copyright
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
17 ;; notice and this paragraph appear in all copies.
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
18
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
19 ;;; Commentary:
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 ;;
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
21 ;; This is a major mode for editing Python programs. It was developed
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
22 ;; by Tim Peters <tim@ksr.com> after an original idea by Michael
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
23 ;; A. Guravage. Tim doesn't appear to be on the 'net any longer so I
2524
6001448fab89 #updated copywrite notice by removing the GPL language according to
Barry Warsaw <barry@python.org>
parents: 2513
diff changeset
24 ;; have undertaken maintenance of the mode.
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
25
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
26 ;; At some point this mode will undergo a rewrite to bring it more in
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
27 ;; line with GNU Emacs Lisp coding standards. But all in all, the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
28 ;; mode works exceedingly well.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
29
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
30 ;; The following statements, placed in your .emacs file or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
31 ;; site-init.el, will cause this file to be autoloaded, and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
32 ;; python-mode invoked, when visiting .py files (assuming this file is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
33 ;; in your load-path):
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
34 ;;
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
35 ;; (autoload 'python-mode "python-mode" "Python editing mode." t)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
36 ;; (setq auto-mode-alist
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
37 ;; (cons '("\\.py$" . python-mode) auto-mode-alist))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
38
2543
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
39 ;; Here's a brief list of recent additions/improvements:
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
40 ;;
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
41 ;; - Wrapping and indentation within triple quote strings should work
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
42 ;; properly now.
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
43 ;; - `Standard' bug reporting mechanism (use C-c C-b)
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
44 ;; - py-mark-block was moved to C-c C-m
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
45 ;; - C-c C-v shows you the python-mode version
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
46 ;; - a basic python-font-lock-keywords has been added for Emacs 19
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
47 ;; font-lock colorizations.
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
48 ;; - proper interaction with pending-del and del-sel modes.
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
49 ;; - New py-electric-colon (:) command for improved outdenting. Also
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
50 ;; py-indent-line (TAB) should handle outdented lines better.
2556
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
51 ;; - New commands py-outdent-left (C-c C-l) and py-indent-right (C-c C-r)
2543
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
52
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
53 ;; Here's a brief to do list:
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
54 ;;
2543
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
55 ;; - Better integration with gud-mode for debugging.
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
56 ;; - Rewrite according to GNU Emacs Lisp standards.
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
57 ;; - py-delete-char should obey numeric arguments.
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
58 ;; - even better support for outdenting. Guido suggests outdents of
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
59 ;; at least one level after a return, raise, break, or continue
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
60 ;; statement.
2711
9feb0b0b50b6 #updated to-do list
Barry Warsaw <barry@python.org>
parents: 2587
diff changeset
61 ;; - de-electrify colon inside literals (e.g. comments and strings)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
62
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
63 ;; If you can think of more things you'd like to see, drop me a line.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
64 ;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
65 ;;
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
66 ;; Note that I only test things on XEmacs (currently 19.11). If you
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
67 ;; port stuff to FSF Emacs 19, or Emacs 18, please send me your
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
68 ;; patches.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
69
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
70 ;; LCD Archive Entry:
2799
4a998b1a331f #changed all email address to go through python.org
Barry Warsaw <barry@python.org>
parents: 2798
diff changeset
71 ;; python-mode|Barry A. Warsaw|python-mode@python.org
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
72 ;; |Major mode for editing Python programs
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
73 ;; |$Date$|$Revision$|
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
74
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
75 ;;; Code:
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
76
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
77
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
78 ;; user definable variables
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
79 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
80
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 (defvar py-python-command "python"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
82 "*Shell command used to start Python interpreter.")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
83
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
84 (defvar py-indent-offset 8 ; argue with Guido <grin>
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
85 "*Indentation increment.
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
86 Note that `\\[py-guess-indent-offset]' can usually guess a good value
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
87 when you're editing someone else's Python code.")
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
88
3114
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
89 (defvar py-align-multiline-strings-p t
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
90 "*Flag describing how multiline triple quoted strings are aligned.
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
91 When this flag is non-nil, continuation lines are lined up under the
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
92 preceding line's indentation. When this flag is nil, continuation
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
93 lines are aligned to column zero.")
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
94
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
95 (defvar py-block-comment-prefix "##"
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
96 "*String used by `py-comment-region' to comment out a block of code.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
97 This should follow the convention for non-indenting comment lines so
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
98 that the indentation commands won't get confused (i.e., the string
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
99 should be of the form `#x...' where `x' is not a blank or a tab, and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
100 `...' is arbitrary).")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
101
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
102 (defvar py-scroll-process-buffer t
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
103 "*Scroll Python process buffer as output arrives.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
104 If nil, the Python process buffer acts, with respect to scrolling, like
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 Shell-mode buffers normally act. This is surprisingly complicated and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
106 so won't be explained here; in fact, you can't get the whole story
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
107 without studying the Emacs C code.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
108
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
109 If non-nil, the behavior is different in two respects (which are
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
110 slightly inaccurate in the interest of brevity):
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
111
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
112 - If the buffer is in a window, and you left point at its end, the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
113 window will scroll as new output arrives, and point will move to the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
114 buffer's end, even if the window is not the selected window (that
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
115 being the one the cursor is in). The usual behavior for shell-mode
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
116 windows is not to scroll, and to leave point where it was, if the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
117 buffer is in a window other than the selected window.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
118
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
119 - If the buffer is not visible in any window, and you left point at
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
120 its end, the buffer will be popped into a window as soon as more
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
121 output arrives. This is handy if you have a long-running
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
122 computation and don't want to tie up screen area waiting for the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
123 output. The usual behavior for a shell-mode buffer is to stay
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
124 invisible until you explicitly visit it.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
125
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
126 Note the `and if you left point at its end' clauses in both of the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
127 above: you can `turn off' the special behaviors while output is in
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
128 progress, by visiting the Python buffer and moving point to anywhere
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
129 besides the end. Then the buffer won't scroll, point will remain where
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
130 you leave it, and if you hide the buffer it will stay hidden until you
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
131 visit it again. You can enable and disable the special behaviors as
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
132 often as you like, while output is in progress, by (respectively) moving
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
133 point to, or away from, the end of the buffer.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
134
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
135 Warning: If you expect a large amount of output, you'll probably be
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
136 happier setting this option to nil.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
137
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
138 Obscure: `End of buffer' above should really say `at or beyond the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
139 process mark', but if you know what that means you didn't need to be
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
140 told <grin>.")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
141
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
142 (defvar py-temp-directory
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
143 (let ((ok '(lambda (x)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
144 (and x
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
145 (setq x (expand-file-name x)) ; always true
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
146 (file-directory-p x)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
147 (file-writable-p x)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
148 x))))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
149 (or (funcall ok (getenv "TMPDIR"))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
150 (funcall ok "/usr/tmp")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
151 (funcall ok "/tmp")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
152 (funcall ok ".")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
153 (error
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
154 "Couldn't find a usable temp directory -- set py-temp-directory")))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
155 "*Directory used for temp files created by a *Python* process.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
156 By default, the first directory from this list that exists and that you
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
157 can write into: the value (if any) of the environment variable TMPDIR,
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
158 /usr/tmp, /tmp, or the current directory.")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
159
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
160 (defvar py-beep-if-tab-change t
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
161 "*Ring the bell if tab-width is changed.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
162 If a comment of the form
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
163
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
164 \t# vi:set tabsize=<number>:
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
165
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
166 is found before the first code line when the file is entered, and the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
167 current value of (the general Emacs variable) `tab-width' does not
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
168 equal <number>, `tab-width' is set to <number>, a message saying so is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
169 displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
170 the Emacs bell is also rung as a warning.")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
171
2797
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
172 ;; These were the previous font-lock keywords, but I think I now
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
173 ;; prefer the ones from XEmacs 19.12's font-lock.el. I've merged the
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
174 ;; two into the new definition below.
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
175 ;;
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
176 ;;(defvar python-font-lock-keywords
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
177 ;; (list
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
178 ;; (cons
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
179 ;; (concat
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
180 ;; "\\<\\("
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
181 ;; (mapconcat
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
182 ;; 'identity
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
183 ;; '("access" "and" "break" "continue"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
184 ;; "del" "elif" "else" "except"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
185 ;; "exec" "finally" "for" "from"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
186 ;; "global" "if" "import" "in"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
187 ;; "is" "lambda" "not" "or"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
188 ;; "pass" "print" "raise" "return"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
189 ;; "try" "while" "def" "class"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
190 ;; )
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
191 ;; "\\|")
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
192 ;; "\\)\\>")
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
193 ;; 1)
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
194 ;; ;; functions
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
195 ;; '("\\bdef\\s +\\(\\sw+\\)(" 1 font-lock-function-name-face)
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
196 ;; ;; classes
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
197 ;; '("\\bclass\\s +\\(\\sw+\\)[(:]" 1 font-lock-function-name-face)
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
198 ;; )
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
199 ;; "*Additional keywords to highlight `python-mode' buffers.")
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
200
2797
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
201 ;; These are taken from XEmacs 19.12's font-lock.el file, but have the
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
202 ;; more complete list of keywords from the previous definition in
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
203 ;; python-mode.el. There are a few other minor stylistic changes as
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
204 ;; well.
2760
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
205 ;;
2797
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
206 (defvar python-font-lock-keywords
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
207 (list
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
208 (cons (concat
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
209 "\\b\\("
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
210 (mapconcat
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
211 'identity
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
212 '("access" "and" "break" "continue"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
213 "del" "elif" "else:" "except"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
214 "except:" "exec" "finally:" "for"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
215 "from" "global" "if" "import"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
216 "in" "is" "lambda" "not"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
217 "or" "pass" "print" "raise"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
218 "return" "try:" "while"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
219 )
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
220 "\\|")
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
221 "\\)[ \n\t(]")
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
222 1)
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
223 ;; classes
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
224 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
225 1 font-lock-type-face)
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
226 ;; functions
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
227 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
228 1 font-lock-function-name-face)
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
229 )
7f2c0e7ef842 (python-font-lock-keywords): merged with XEmacs 19.12 font-lock.el
Barry Warsaw <barry@python.org>
parents: 2796
diff changeset
230 "*Additional expressions to highlight in Python mode.")
2760
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
231
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
232 ;; R Lindsay Todd <toddr@rpi.edu> suggests these changes to the
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
233 ;; original keywords, which wouldn't be necessary if we go with the
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
234 ;; XEmacs defaults, but which I agree makes sense without them.
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
235 ;;
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
236 ;; functions
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
237 ;; '("\\bdef\\s +\\(\\sw+\\)\\s *(" 1 font-lock-function-name-face)
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
238 ;; classes
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
239 ;; '("\\bclass\\s +\\(\\sw+\\)\\s *[(:]" 1 font-lock-type-face)
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
240
1b9e90353529 Added tbd's on python-font-lock-keywords
Barry Warsaw <barry@python.org>
parents: 2711
diff changeset
241
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
242
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
243 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
244 ;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
245
3220
f1cc247fff13 (py-indent-offset): this should be buffer local.
Barry Warsaw <barry@python.org>
parents: 3114
diff changeset
246 (make-variable-buffer-local 'py-indent-offset)
f1cc247fff13 (py-indent-offset): this should be buffer local.
Barry Warsaw <barry@python.org>
parents: 3114
diff changeset
247
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
248 ;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
249 ;; seems to be the standard way of checking this.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
250 ;; BAW - This is *not* the right solution. When at all possible,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
251 ;; instead of testing for the version of Emacs, use feature tests.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
252
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
253 (setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
254 (setq py-this-is-emacs-19-p
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
255 (and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
256 (not py-this-is-lucid-emacs-p)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
257 (string-match "^19\\." emacs-version)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
258
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
259 ;; have to bind py-file-queue before installing the kill-emacs hook
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
260 (defvar py-file-queue nil
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
261 "Queue of Python temp files awaiting execution.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
262 Currently-active file is at the head of the list.")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
263
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
264 ;; define a mode-specific abbrev table for those who use such things
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
265 (defvar python-mode-abbrev-table nil
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
266 "Abbrev table in use in `python-mode' buffers.")
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
267 (define-abbrev-table 'python-mode-abbrev-table nil)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
268
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
269 (defvar python-mode-hook nil
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
270 "*Hook called by `python-mode'.")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
271
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
272 ;; in previous version of python-mode.el, the hook was incorrectly
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
273 ;; called py-mode-hook, and was not defvar'd. deprecate its use.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
274 (and (fboundp 'make-obsolete-variable)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
275 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
276
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
277 (defvar py-mode-map ()
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
278 "Keymap used in `python-mode' buffers.")
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
279
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
280 (if py-mode-map
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
281 ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
282 (setq py-mode-map (make-sparse-keymap))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
283
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
284 ;; shadow global bindings for newline-and-indent w/ the py- version.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
285 ;; BAW - this is extremely bad form, but I'm not going to change it
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
286 ;; for now.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
287 (mapcar (function (lambda (key)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
288 (define-key
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
289 py-mode-map key 'py-newline-and-indent)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
290 (where-is-internal 'newline-and-indent))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
291
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
292 ;; BAW - you could do it this way, but its not considered proper
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
293 ;; major-mode form.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
294 (mapcar (function
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
295 (lambda (x)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
296 (define-key py-mode-map (car x) (cdr x))))
2535
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
297 '((":" . py-electric-colon)
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
298 ("\C-c\C-c" . py-execute-buffer)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
299 ("\C-c|" . py-execute-region)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
300 ("\C-c!" . py-shell)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
301 ("\177" . py-delete-char)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
302 ("\n" . py-newline-and-indent)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
303 ("\C-c:" . py-guess-indent-offset)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
304 ("\C-c\t" . py-indent-region)
2556
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
305 ("\C-c\C-l" . py-outdent-left)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
306 ("\C-c\C-r" . py-indent-right)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
307 ("\C-c<" . py-shift-region-left)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
308 ("\C-c>" . py-shift-region-right)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
309 ("\C-c\C-n" . py-next-statement)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
310 ("\C-c\C-p" . py-previous-statement)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
311 ("\C-c\C-u" . py-goto-block-up)
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
312 ("\C-c\C-m" . py-mark-block)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
313 ("\C-c#" . py-comment-region)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
314 ("\C-c?" . py-describe-mode)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
315 ("\C-c\C-hm" . py-describe-mode)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
316 ("\e\C-a" . beginning-of-python-def-or-class)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
317 ("\e\C-e" . end-of-python-def-or-class)
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
318 ( "\e\C-h" . mark-python-def-or-class)))
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
319 ;; should do all keybindings this way
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
320 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
321 (define-key py-mode-map "\C-c\C-v" 'py-version)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
322 )
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
323
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
324 (defvar py-mode-syntax-table nil
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
325 "Syntax table used in `python-mode' buffers.")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
326
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
327 (if py-mode-syntax-table
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
328 ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
329 (setq py-mode-syntax-table (make-syntax-table))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
330 ;; BAW - again, blech.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
331 (mapcar (function
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
332 (lambda (x) (modify-syntax-entry
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
333 (car x) (cdr x) py-mode-syntax-table)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
334 '(( ?\( . "()" ) ( ?\) . ")(" )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
335 ( ?\[ . "(]" ) ( ?\] . ")[" )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
336 ( ?\{ . "(}" ) ( ?\} . "){" )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
337 ;; fix operator symbols misassigned in the std table
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
338 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
339 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
340 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
341 ( ?\> . "." ) ( ?\| . "." )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
342 ( ?\_ . "w" ) ; underscore is legit in names
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
343 ( ?\' . "\"") ; single quote is string quote
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
344 ( ?\" . "\"" ) ; double quote is string quote too
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
345 ( ?\` . "$") ; backquote is open and close paren
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
346 ( ?\# . "<") ; hash starts comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
347 ( ?\n . ">")))) ; newline ends comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
348
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
349 (defconst py-stringlit-re
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
350 (concat
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
351 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
352 "\\|" ; or
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
353 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
354 "Regexp matching a Python string literal.")
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
355
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
356 ;; this is tricky because a trailing backslash does not mean
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
357 ;; continuation if it's in a comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
358 (defconst py-continued-re
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
359 (concat
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
360 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
361 "\\\\$")
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
362 "Regexp matching Python lines that are continued via backslash.")
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
363
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
364 (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
365 "Regexp matching blank or comment lines.")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
366
2537
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
367 (defconst py-outdent-re
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
368 (concat "\\(" (mapconcat 'identity
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
369 '("else:"
2544
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
370 "except\\(\\s +.*\\)?:"
2537
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
371 "finally:"
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
372 "elif\\s +.*:")
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
373 "\\|")
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
374 "\\)")
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
375 "Regexp matching clauses to be outdented one level.")
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
376
2544
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
377 (defconst py-no-outdent-re
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
378 (concat "\\(" (mapconcat 'identity
2548
2cac6c5066da (py-no-outdent-re): fixed the regexp for try: clauses
Barry Warsaw <barry@python.org>
parents: 2547
diff changeset
379 '("try:"
2544
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
380 "except\\(\\s +.*\\)?:"
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
381 "while\\s +.*:"
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
382 "for\\s +.*:"
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
383 "if\\s +.*:"
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
384 "elif\\s +.*:")
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
385 "\\|")
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
386 "\\)")
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
387 "Regexp matching lines to not outdent after.")
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
388
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
389
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
390 ;;;###autoload
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
391 (defun python-mode ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
392 "Major mode for editing Python files.
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
393 To submit a problem report, enter `\\[py-submit-bug-report]' from a
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
394 `python-mode' buffer. Do `\\[py-describe-mode]' for detailed
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
395 documentation. To see what version of `python-mode' you are running,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
396 enter `\\[py-version]'.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
397
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
398 This mode knows about Python indentation, tokens, comments and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
399 continuation lines. Paragraphs are separated by blank lines only.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
400
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
401 COMMANDS
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
402 \\{py-mode-map}
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
403 VARIABLES
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
404
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
405 py-indent-offset\tindentation increment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
406 py-block-comment-prefix\tcomment string used by py-comment-region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
407 py-python-command\tshell command to invoke Python interpreter
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
408 py-scroll-process-buffer\talways scroll Python process buffer
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
409 py-temp-directory\tdirectory used for temp files (if needed)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
410 py-beep-if-tab-change\tring the bell if tab-width is changed"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
411 (interactive)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
412 (kill-all-local-variables)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
413 (set-syntax-table py-mode-syntax-table)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
414 (setq major-mode 'python-mode
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
415 mode-name "Python"
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
416 local-abbrev-table python-mode-abbrev-table)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
417 (use-local-map py-mode-map)
3094
5a1fbd21fc2a (python-mode): comment-multi-line must be nil for Emacs 19
Barry Warsaw <barry@python.org>
parents: 3093
diff changeset
418 ;; Emacs 19 requires this
5a1fbd21fc2a (python-mode): comment-multi-line must be nil for Emacs 19
Barry Warsaw <barry@python.org>
parents: 3093
diff changeset
419 (if (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p)
5a1fbd21fc2a (python-mode): comment-multi-line must be nil for Emacs 19
Barry Warsaw <barry@python.org>
parents: 3093
diff changeset
420 (setq comment-multi-line nil))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
421 ;; BAW -- style...
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
422 (mapcar (function (lambda (x)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
423 (make-local-variable (car x))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
424 (set (car x) (cdr x))))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
425 '((paragraph-separate . "^[ \t]*$")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
426 (paragraph-start . "^[ \t]*$")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
427 (require-final-newline . t)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
428 (comment-start . "# ")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
429 (comment-start-skip . "# *")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
430 (comment-column . 40)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
431 (indent-region-function . py-indent-region)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
432 (indent-line-function . py-indent-line)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
433 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
434 ;;
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
435 ;; not sure where the magic comment has to be; to save time
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
436 ;; searching for a rarity, we give up if it's not found prior to the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
437 ;; first executable statement.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
438 ;;
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
439 ;; BAW - on first glance, this seems like complete hackery. Why was
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
440 ;; this necessary, and is it still necessary?
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
441 (let ((case-fold-search nil)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
442 (start (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
443 new-tab-width)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
444 (if (re-search-forward
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
445 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
446 (prog2 (py-next-statement 1) (point) (goto-char 1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
447 t)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
448 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
449 (setq new-tab-width
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
450 (string-to-int
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
451 (buffer-substring (match-beginning 1) (match-end 1))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
452 (if (= tab-width new-tab-width)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
453 nil
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
454 (setq tab-width new-tab-width)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
455 (message "Caution: tab-width changed to %d" new-tab-width)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
456 (if py-beep-if-tab-change (beep)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
457 (goto-char start))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
458
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
459 ;; run the mode hook. py-mode-hook use is deprecated
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
460 (if python-mode-hook
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
461 (run-hooks 'python-mode-hook)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
462 (run-hooks 'py-mode-hook)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
463
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
464
2535
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
465 ;; electric characters
2547
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
466 (defun py-outdent-p ()
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
467 ;; returns non-nil if the current line should outdent one level
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
468 (save-excursion
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
469 (and (progn (back-to-indentation)
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
470 (looking-at py-outdent-re))
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
471 (progn (backward-to-indentation 1)
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
472 (while (or (looking-at py-blank-or-comment-re)
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
473 (bobp))
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
474 (backward-to-indentation 1))
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
475 (not (looking-at py-no-outdent-re)))
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
476 )))
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
477
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
478
2535
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
479 (defun py-electric-colon (arg)
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
480 "Insert a colon.
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
481 In certain cases the line is outdented appropriately. If a numeric
3095
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
482 argument is provided, that many colons are inserted non-electrically.
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
483 Electric behavior is inhibited inside a string or comment."
2535
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
484 (interactive "P")
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
485 (self-insert-command (prefix-numeric-value arg))
3095
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
486 ;; are we in a string or comment?
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
487 (if (save-excursion
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
488 (let ((pps (parse-partial-sexp (save-excursion
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
489 (beginning-of-python-def-or-class)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
490 (point))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
491 (point))))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
492 (not (or (nth 3 pps) (nth 4 pps)))))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
493 (save-excursion
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
494 (let ((here (point))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
495 (outdent 0)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
496 (indent (py-compute-indentation)))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
497 (if (and (not arg)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
498 (py-outdent-p)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
499 (= indent (save-excursion
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
500 (forward-line -1)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
501 (py-compute-indentation)))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
502 )
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
503 (setq outdent py-indent-offset))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
504 ;; Don't indent, only outdent. This assumes that any lines that
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
505 ;; are already outdented relative to py-compute-indentation were
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
506 ;; put there on purpose. Its highly annoying to have `:' indent
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
507 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
508 ;; there a better way to determine this???
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
509 (if (< (current-indentation) indent) nil
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
510 (goto-char here)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
511 (beginning-of-line)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
512 (delete-horizontal-space)
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
513 (indent-to (- indent outdent))
c1d77d74fe35 (py-electric-colon): turn off electric behavior inside strings and
Barry Warsaw <barry@python.org>
parents: 3094
diff changeset
514 )))))
2535
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
515
2556
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
516 (defun py-indent-right (arg)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
517 "Indent the line by one `py-indent-offset' level.
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
518 With numeric arg, indent by that many levels. You cannot indent
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
519 farther right than the distance the line would be indented by
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
520 \\[py-indent-line]."
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
521 (interactive "p")
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
522 (let ((col (current-indentation))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
523 (want (* arg py-indent-offset))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
524 (indent (py-compute-indentation))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
525 (pos (- (point-max) (point)))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
526 (bol (save-excursion (beginning-of-line) (point))))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
527 (if (<= (+ col want) indent)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
528 (progn
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
529 (beginning-of-line)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
530 (delete-horizontal-space)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
531 (indent-to (+ col want))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
532 (if (> (- (point-max) pos) (point))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
533 (goto-char (- (point-max) pos)))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
534 ))))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
535
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
536 (defun py-outdent-left (arg)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
537 "Outdent the line by one `py-indent-offset' level.
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
538 With numeric arg, outdent by that many levels. You cannot outdent
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
539 farther left than column zero."
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
540 (interactive "p")
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
541 (let ((col (current-indentation))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
542 (want (* arg py-indent-offset))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
543 (pos (- (point-max) (point)))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
544 (bol (save-excursion (beginning-of-line) (point))))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
545 (if (<= 0 (- col want))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
546 (progn
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
547 (beginning-of-line)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
548 (delete-horizontal-space)
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
549 (indent-to (- col want))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
550 (if (> (- (point-max) pos) (point))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
551 (goto-char (- (point-max) pos)))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
552 ))))
f9b07915a061 (py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
Barry Warsaw <barry@python.org>
parents: 2549
diff changeset
553
2535
875173f3f31c (py-electric-colon): new command
Barry Warsaw <barry@python.org>
parents: 2524
diff changeset
554
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
555 ;;; Functions that execute Python commands in a subprocess
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
556 (defun py-shell ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
557 "Start an interactive Python interpreter in another window.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
558 This is like Shell mode, except that Python is running in the window
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
559 instead of a shell. See the `Interactive Shell' and `Shell Mode'
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
560 sections of the Emacs manual for details, especially for the key
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
561 bindings active in the `*Python*' buffer.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
562
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
563 See the docs for variable `py-scroll-buffer' for info on scrolling
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
564 behavior in the process window.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
565
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
566 Warning: Don't use an interactive Python if you change sys.ps1 or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
567 sys.ps2 from their default values, or if you're running code that
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
568 prints `>>> ' or `... ' at the start of a line. `python-mode' can't
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
569 distinguish your output from Python's output, and assumes that `>>> '
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
570 at the start of a line is a prompt from Python. Similarly, the Emacs
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
571 Shell mode code assumes that both `>>> ' and `... ' at the start of a
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
572 line are Python prompts. Bad things can happen if you fool either
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
573 mode.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
574
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
575 Warning: If you do any editing *in* the process buffer *while* the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
576 buffer is accepting output from Python, do NOT attempt to `undo' the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
577 changes. Some of the output (nowhere near the parts you changed!) may
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
578 be lost if you do. This appears to be an Emacs bug, an unfortunate
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
579 interaction between undo and process filters; the same problem exists in
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
580 non-Python process buffers using the default (Emacs-supplied) process
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
581 filter."
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
582 ;; BAW - should undo be disabled in the python process buffer, if
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
583 ;; this bug still exists?
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
584 (interactive)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
585 (if py-this-is-emacs-19-p
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
586 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
587 (require 'comint)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
588 (switch-to-buffer-other-window
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
589 (make-comint "Python" py-python-command)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
590 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
591 (require 'shell)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
592 (switch-to-buffer-other-window
2795
ea38c363de58 (py-shell): cope with make-comint instead of make-shell
Barry Warsaw <barry@python.org>
parents: 2760
diff changeset
593 (apply (if (boundp 'make-shell) 'make-shell 'make-comint)
ea38c363de58 (py-shell): cope with make-comint instead of make-shell
Barry Warsaw <barry@python.org>
parents: 2760
diff changeset
594 "Python" py-python-command nil))))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
595 (make-local-variable 'shell-prompt-pattern)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
596 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
597 (set-process-filter (get-buffer-process (current-buffer))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
598 'py-process-filter)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
599 (set-syntax-table py-mode-syntax-table))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
600
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
601 (defun py-execute-region (start end)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
602 "Send the region between START and END to a Python interpreter.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
603 If there is a *Python* process it is used.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
604
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
605 Hint: If you want to execute part of a Python file several times
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
606 \(e.g., perhaps you're developing a function and want to flesh it out
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
607 a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
608 the region of interest, and send the code to a *Python* process via
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
609 `\\[py-execute-buffer]' instead.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
610
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
611 Following are subtleties to note when using a *Python* process:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
612
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
613 If a *Python* process is used, the region is copied into a temporary
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
614 file (in directory `py-temp-directory'), and an `execfile' command is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
615 sent to Python naming that file. If you send regions faster than
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
616 Python can execute them, `python-mode' will save them into distinct
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
617 temp files, and execute the next one in the queue the next time it
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
618 sees a `>>> ' prompt from Python. Each time this happens, the process
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
619 buffer is popped into a window (if it's not already in some window) so
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
620 you can see it, and a comment of the form
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
621
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
622 \t## working on region in file <name> ...
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
623
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
624 is inserted at the end.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
625
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
626 Caution: No more than 26 regions can be pending at any given time.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
627 This limit is (indirectly) inherited from libc's mktemp(3).
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
628 `python-mode' does not try to protect you from exceeding the limit.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
629 It's extremely unlikely that you'll get anywhere close to the limit in
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
630 practice, unless you're trying to be a jerk <grin>.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
631
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
632 See the `\\[py-shell]' docs for additional warnings."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
633 (interactive "r")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
634 (or (< start end) (error "Region is empty"))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
635 (let ((pyproc (get-process "Python"))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
636 fname)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
637 (if (null pyproc)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
638 (shell-command-on-region start end py-python-command)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
639 ;; else feed it thru a temp file
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
640 (setq fname (py-make-temp-name))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
641 (write-region start end fname nil 'no-msg)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
642 (setq py-file-queue (append py-file-queue (list fname)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
643 (if (cdr py-file-queue)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
644 (message "File %s queued for execution" fname)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
645 ;; else
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
646 (py-execute-file pyproc fname)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
647
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
648 (defun py-execute-file (pyproc fname)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
649 (py-append-to-process-buffer
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
650 pyproc
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
651 (format "## working on region in file %s ...\n" fname))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
652 (process-send-string pyproc (format "execfile('%s')\n" fname)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
653
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
654 (defun py-process-filter (pyproc string)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
655 (let ((curbuf (current-buffer))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
656 (pbuf (process-buffer pyproc))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
657 (pmark (process-mark pyproc))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
658 file-finished)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
659
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
660 ;; make sure we switch to a different buffer at least once. if we
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
661 ;; *don't* do this, then if the process buffer is in the selected
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
662 ;; window, and point is before the end, and lots of output is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
663 ;; coming at a fast pace, then (a) simple cursor-movement commands
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
664 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
665 ;; to have a visible effect (the window just doesn't get updated,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
666 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
667 ;; get all the process output (until the next python prompt).
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
668 ;;
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
669 ;; #b makes no sense to me at all. #a almost makes sense: unless
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
670 ;; we actually change buffers, set_buffer_internal in buffer.c
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
671 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
672 ;; seems to make the Emacs command loop reluctant to update the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
673 ;; display. Perhaps the default process filter in process.c's
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
674 ;; read_process_output has update_mode_lines++ for a similar
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
675 ;; reason? beats me ...
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
676
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
677 ;; BAW - we want to check to see if this still applies
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
678 (if (eq curbuf pbuf) ; mysterious ugly hack
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
679 (set-buffer (get-buffer-create "*scratch*")))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
680
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
681 (set-buffer pbuf)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
682 (let* ((start (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
683 (goback (< start pmark))
2796
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
684 (goend (and (not goback) (= start (point-max))))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
685 (buffer-read-only nil))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
686 (goto-char pmark)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
687 (insert string)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
688 (move-marker pmark (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
689 (setq file-finished
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
690 (and py-file-queue
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
691 (equal ">>> "
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
692 (buffer-substring
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
693 (prog2 (beginning-of-line) (point)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
694 (goto-char pmark))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
695 (point)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
696 (if goback (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
697 ;; else
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
698 (if py-scroll-process-buffer
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
699 (let* ((pop-up-windows t)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
700 (pwin (display-buffer pbuf)))
2796
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
701 (set-window-point pwin (point)))))
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
702 (set-buffer curbuf)
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
703 (if file-finished
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
704 (progn
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
705 (py-delete-file-silently (car py-file-queue))
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
706 (setq py-file-queue (cdr py-file-queue))
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
707 (if py-file-queue
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
708 (py-execute-file pyproc (car py-file-queue)))))
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
709 (and goend
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
710 (progn (set-buffer pbuf)
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
711 (goto-char (point-max))))
ebc1714dc738 (py-process-filter): if command takes a long time, input typed before
Barry Warsaw <barry@python.org>
parents: 2795
diff changeset
712 )))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
713
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
714 (defun py-execute-buffer ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
715 "Send the contents of the buffer to a Python interpreter.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
716 If there is a *Python* process buffer it is used. If a clipping
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
717 restriction is in effect, only the accessible portion of the buffer is
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
718 sent. A trailing newline will be supplied if needed.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
719
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
720 See the `\\[py-execute-region]' docs for an account of some subtleties."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
721 (interactive)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
722 (py-execute-region (point-min) (point-max)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
723
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
724
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
725
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
726 ;; Functions for Python style indentation
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
727 (defun py-delete-char ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
728 "Reduce indentation or delete character.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
729 If point is at the leftmost column, deletes the preceding newline.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
730
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
731 Else if point is at the leftmost non-blank character of a line that is
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
732 neither a continuation line nor a non-indenting comment line, or if
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
733 point is at the end of a blank line, reduces the indentation to match
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
734 that of the line that opened the current block of code. The line that
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
735 opened the block is displayed in the echo area to help you keep track
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
736 of where you are.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
737
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
738 Else the preceding character is deleted, converting a tab to spaces if
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
739 needed so that only a single column position is deleted."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
740 (interactive "*")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
741 (if (or (/= (current-indentation) (current-column))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
742 (bolp)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
743 (py-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
744 (looking-at "#[^ \t\n]")) ; non-indenting #
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
745 (backward-delete-char-untabify 1)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
746 ;; else indent the same as the colon line that opened the block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
747
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
748 ;; force non-blank so py-goto-block-up doesn't ignore it
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
749 (insert-char ?* 1)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
750 (backward-char)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
751 (let ((base-indent 0) ; indentation of base line
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
752 (base-text "") ; and text of base line
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
753 (base-found-p nil))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
754 (condition-case nil ; in case no enclosing block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
755 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
756 (py-goto-block-up 'no-mark)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
757 (setq base-indent (current-indentation)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
758 base-text (py-suck-up-leading-text)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
759 base-found-p t))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
760 (error nil))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
761 (delete-char 1) ; toss the dummy character
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
762 (delete-horizontal-space)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
763 (indent-to base-indent)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
764 (if base-found-p
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
765 (message "Closes block: %s" base-text)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
766
2513
56e25114d438 (py-delete-char): must have 'supersede value for 'delete-selection and
Barry Warsaw <barry@python.org>
parents: 2499
diff changeset
767 ;; required for pending-del and delsel modes
56e25114d438 (py-delete-char): must have 'supersede value for 'delete-selection and
Barry Warsaw <barry@python.org>
parents: 2499
diff changeset
768 (put 'py-delete-char 'delete-selection 'supersede)
56e25114d438 (py-delete-char): must have 'supersede value for 'delete-selection and
Barry Warsaw <barry@python.org>
parents: 2499
diff changeset
769 (put 'py-delete-char 'pending-delete 'supersede)
56e25114d438 (py-delete-char): must have 'supersede value for 'delete-selection and
Barry Warsaw <barry@python.org>
parents: 2499
diff changeset
770
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
771 (defun py-indent-line ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
772 "Fix the indentation of the current line according to Python rules."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
773 (interactive)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
774 (let* ((ci (current-indentation))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
775 (move-to-indentation-p (<= (current-column) ci))
2536
a14fc2d04c06 #(py-indent-line): cosmetic
Barry Warsaw <barry@python.org>
parents: 2535
diff changeset
776 (need (py-compute-indentation)))
2544
aabbfb61e388 (py-no-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2543
diff changeset
777 ;; see if we need to outdent
2547
3281a931a1d9 (py-outdent-p): new function
Barry Warsaw <barry@python.org>
parents: 2544
diff changeset
778 (if (py-outdent-p)
2537
6fcd0b022fa7 (py-outdent-re): new constant
Barry Warsaw <barry@python.org>
parents: 2536
diff changeset
779 (setq need (- need py-indent-offset)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
780 (if (/= ci need)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
781 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
782 (beginning-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
783 (delete-horizontal-space)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
784 (indent-to need)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
785 (if move-to-indentation-p (back-to-indentation))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
786
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
787 (defun py-newline-and-indent ()
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
788 "Strives to act like the Emacs `newline-and-indent'.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
789 This is just `strives to' because correct indentation can't be computed
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
790 from scratch for Python code. In general, deletes the whitespace before
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
791 point, inserts a newline, and takes an educated guess as to how you want
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
792 the new line indented."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
793 (interactive)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
794 (let ((ci (current-indentation)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
795 (if (< ci (current-column)) ; if point beyond indentation
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
796 (newline-and-indent)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
797 ;; else try to act like newline-and-indent "normally" acts
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
798 (beginning-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
799 (insert-char ?\n 1)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
800 (move-to-column ci))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
801
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
802 (defun py-compute-indentation ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
803 (save-excursion
3114
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
804 (let ((pps (parse-partial-sexp (save-excursion
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
805 (beginning-of-python-def-or-class)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
806 (point))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
807 (point))))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
808 (beginning-of-line)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
809 (cond
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
810 ;; are we inside a string or comment?
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
811 ((or (nth 3 pps) (nth 4 pps))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
812 (save-excursion
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
813 (if (not py-align-multiline-strings-p) 0
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
814 ;; skip back over blank & non-indenting comment lines
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
815 ;; note: will skip a blank or non-indenting comment line
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
816 ;; that happens to be a continuation line too
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
817 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
818 (back-to-indentation)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
819 (current-column))))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
820 ;; are we on a continuation line?
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
821 ((py-continuation-line-p)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
822 (let ((startpos (point))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
823 (open-bracket-pos (py-nesting-level))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
824 endpos searching found)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
825 (if open-bracket-pos
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
826 (progn
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
827 ;; align with first item in list; else a normal
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
828 ;; indent beyond the line with the open bracket
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
829 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
830 ;; is the first list item on the same line?
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
831 (skip-chars-forward " \t")
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
832 (if (null (memq (following-char) '(?\n ?# ?\\)))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
833 ; yes, so line up with it
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
834 (current-column)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
835 ;; first list item on another line, or doesn't exist yet
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
836 (forward-line 1)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
837 (while (and (< (point) startpos)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
838 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
839 (forward-line 1))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
840 (if (< (point) startpos)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
841 ;; again mimic the first list item
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
842 (current-indentation)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
843 ;; else they're about to enter the first item
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
844 (goto-char open-bracket-pos)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
845 (+ (current-indentation) py-indent-offset))))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
846
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
847 ;; else on backslash continuation line
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
848 (forward-line -1)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
849 (if (py-continuation-line-p) ; on at least 3rd line in block
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
850 (current-indentation) ; so just continue the pattern
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
851 ;; else started on 2nd line in block, so indent more.
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
852 ;; if base line is an assignment with a start on a RHS,
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
853 ;; indent to 2 beyond the leftmost "="; else skip first
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
854 ;; chunk of non-whitespace characters on base line, + 1 more
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
855 ;; column
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
856 (end-of-line)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
857 (setq endpos (point) searching t)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
858 (back-to-indentation)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
859 (setq startpos (point))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
860 ;; look at all "=" from left to right, stopping at first
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
861 ;; one not nested in a list or string
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
862 (while searching
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
863 (skip-chars-forward "^=" endpos)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
864 (if (= (point) endpos)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
865 (setq searching nil)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
866 (forward-char 1)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
867 (setq state (parse-partial-sexp startpos (point)))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
868 (if (and (zerop (car state)) ; not in a bracket
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
869 (null (nth 3 state))) ; & not in a string
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
870 (progn
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
871 (setq searching nil) ; done searching in any case
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
872 (setq found
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
873 (not (or
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
874 (eq (following-char) ?=)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
875 (memq (char-after (- (point) 2))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
876 '(?< ?> ?!)))))))))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
877 (if (or (not found) ; not an assignment
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
878 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
879 (progn
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
880 (goto-char startpos)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
881 (skip-chars-forward "^ \t\n")))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
882 (1+ (current-column))))))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
883
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
884 ;; not on a continuation line
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
885
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
886 ;; if at start of restriction, or on a non-indenting comment
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
887 ;; line, assume they intended whatever's there
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
888 ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
889 (current-indentation))
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
890
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
891 ;; else indentation based on that of the statement that
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
892 ;; precedes us; use the first line of that statement to
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
893 ;; establish the base, in case the user forced a non-std
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
894 ;; indentation for the continuation lines (if any)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
895 (t
3093
b3a8013cb4fa (py-compute-indentation): when inside a comment, indent to under the
Barry Warsaw <barry@python.org>
parents: 2799
diff changeset
896 ;; skip back over blank & non-indenting comment lines note:
b3a8013cb4fa (py-compute-indentation): when inside a comment, indent to under the
Barry Warsaw <barry@python.org>
parents: 2799
diff changeset
897 ;; will skip a blank or non-indenting comment line that
b3a8013cb4fa (py-compute-indentation): when inside a comment, indent to under the
Barry Warsaw <barry@python.org>
parents: 2799
diff changeset
898 ;; happens to be a continuation line too
b3a8013cb4fa (py-compute-indentation): when inside a comment, indent to under the
Barry Warsaw <barry@python.org>
parents: 2799
diff changeset
899 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
3114
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
900 ;; if we landed inside a string, go to the beginning of that
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
901 ;; string. this handles triple quoted, multi-line spanning
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
902 ;; strings.
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
903 (py-goto-initial-line)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
904 (if (py-statement-opens-block-p)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
905 (+ (current-indentation) py-indent-offset)
3aab925ebfb0 (py-align-multiline-strings-p): new variable
Barry Warsaw <barry@python.org>
parents: 3095
diff changeset
906 (current-indentation)))))))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
907
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
908 (defun py-guess-indent-offset (&optional global)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
909 "Guess a good value for, and change, `py-indent-offset'.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
910 By default (without a prefix arg), makes a buffer-local copy of
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
911 `py-indent-offset' with the new value. This will not affect any other
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
912 Python buffers. With a prefix arg, changes the global value of
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
913 `py-indent-offset'. This affects all Python buffers (that don't have
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
914 their own buffer-local copy), both those currently existing and those
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
915 created later in the Emacs session.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
916
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
917 Some people use a different value for `py-indent-offset' than you use.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
918 There's no excuse for such foolishness, but sometimes you have to deal
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
919 with their ugly code anyway. This function examines the file and sets
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
920 `py-indent-offset' to what it thinks it was when they created the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
921 mess.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
922
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
923 Specifically, it searches forward from the statement containing point,
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
924 looking for a line that opens a block of code. `py-indent-offset' is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
925 set to the difference in indentation between that line and the Python
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
926 statement following it. If the search doesn't succeed going forward,
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
927 it's tried again going backward."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
928 (interactive "P") ; raw prefix arg
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
929 (let (new-value
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
930 (start (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
931 restart
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
932 (found nil)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
933 colon-indent)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
934 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
935 (while (not (or found (eobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
936 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
937 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
938 (setq restart (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
939 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
940 (if (py-statement-opens-block-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
941 (setq found t)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
942 (goto-char restart)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
943 (if found
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
944 ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
945 (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
946 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
947 (while (not (or found (bobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
948 (setq found
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
949 (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
950 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
951 (or (py-goto-initial-line) t) ; always true -- side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
952 (py-statement-opens-block-p)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
953 (setq colon-indent (current-indentation)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
954 found (and found (zerop (py-next-statement 1)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
955 new-value (- (current-indentation) colon-indent))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
956 (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
957 (if found
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
958 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
959 (funcall (if global 'kill-local-variable 'make-local-variable)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
960 'py-indent-offset)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
961 (setq py-indent-offset new-value)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
962 (message "%s value of py-indent-offset set to %d"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
963 (if global "Global" "Local")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
964 py-indent-offset))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
965 (error "Sorry, couldn't guess a value for py-indent-offset"))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
966
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
967 (defun py-shift-region (start end count)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
968 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
969 (goto-char end) (beginning-of-line) (setq end (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
970 (goto-char start) (beginning-of-line) (setq start (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
971 (indent-rigidly start end count)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
972
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
973 (defun py-shift-region-left (start end &optional count)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
974 "Shift region of Python code to the left.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
975 The lines from the line containing the start of the current region up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
976 to (but not including) the line containing the end of the region are
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
977 shifted to the left, by `py-indent-offset' columns.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
978
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
979 If a prefix argument is given, the region is instead shifted by that
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
980 many columns."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
981 (interactive "*r\nP") ; region; raw prefix arg
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
982 (py-shift-region start end
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
983 (- (prefix-numeric-value
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
984 (or count py-indent-offset)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
985
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
986 (defun py-shift-region-right (start end &optional count)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
987 "Shift region of Python code to the right.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
988 The lines from the line containing the start of the current region up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
989 to (but not including) the line containing the end of the region are
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
990 shifted to the right, by `py-indent-offset' columns.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
991
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
992 If a prefix argument is given, the region is instead shifted by that
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
993 many columns."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
994 (interactive "*r\nP") ; region; raw prefix arg
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
995 (py-shift-region start end (prefix-numeric-value
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
996 (or count py-indent-offset))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
997
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
998 (defun py-indent-region (start end &optional indent-offset)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
999 "Reindent a region of Python code.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1000 The lines from the line containing the start of the current region up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1001 to (but not including) the line containing the end of the region are
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1002 reindented. If the first line of the region has a non-whitespace
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1003 character in the first column, the first line is left alone and the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1004 rest of the region is reindented with respect to it. Else the entire
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1005 region is reindented with respect to the (closest code or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1006 indenting-comment) statement immediately preceding the region.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1007
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1008 This is useful when code blocks are moved or yanked, when enclosing
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1009 control structures are introduced or removed, or to reformat code
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1010 using a new value for the indentation offset.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1011
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1012 If a numeric prefix argument is given, it will be used as the value of
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1013 the indentation offset. Else the value of `py-indent-offset' will be
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1014 used.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1015
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1016 Warning: The region must be consistently indented before this function
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1017 is called! This function does not compute proper indentation from
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1018 scratch (that's impossible in Python), it merely adjusts the existing
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1019 indentation to be correct in context.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1020
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1021 Warning: This function really has no idea what to do with
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1022 non-indenting comment lines, and shifts them as if they were indenting
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1023 comment lines. Fixing this appears to require telepathy.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1024
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1025 Special cases: whitespace is deleted from blank lines; continuation
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1026 lines are shifted by the same amount their initial line was shifted,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1027 in order to preserve their relative indentation with respect to their
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1028 initial line; and comment lines beginning in column 1 are ignored."
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1029 (interactive "*r\nP") ; region; raw prefix arg
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1030 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1031 (goto-char end) (beginning-of-line) (setq end (point-marker))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1032 (goto-char start) (beginning-of-line)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1033 (let ((py-indent-offset (prefix-numeric-value
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1034 (or indent-offset py-indent-offset)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1035 (indents '(-1)) ; stack of active indent levels
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1036 (target-column 0) ; column to which to indent
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1037 (base-shifted-by 0) ; amount last base line was shifted
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1038 (indent-base (if (looking-at "[ \t\n]")
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1039 (py-compute-indentation)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1040 0))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1041 ci)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1042 (while (< (point) end)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1043 (setq ci (current-indentation))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1044 ;; figure out appropriate target column
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1045 (cond
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1046 ((or (eq (following-char) ?#) ; comment in column 1
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1047 (looking-at "[ \t]*$")) ; entirely blank
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1048 (setq target-column 0))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1049 ((py-continuation-line-p) ; shift relative to base line
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1050 (setq target-column (+ ci base-shifted-by)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1051 (t ; new base line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1052 (if (> ci (car indents)) ; going deeper; push it
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1053 (setq indents (cons ci indents))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1054 ;; else we should have seen this indent before
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1055 (setq indents (memq ci indents)) ; pop deeper indents
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1056 (if (null indents)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1057 (error "Bad indentation in region, at line %d"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1058 (save-restriction
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1059 (widen)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1060 (1+ (count-lines 1 (point)))))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1061 (setq target-column (+ indent-base
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1062 (* py-indent-offset
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1063 (- (length indents) 2))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1064 (setq base-shifted-by (- target-column ci))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1065 ;; shift as needed
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1066 (if (/= ci target-column)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1067 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1068 (delete-horizontal-space)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1069 (indent-to target-column)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1070 (forward-line 1))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1071 (set-marker end nil))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1072
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1073
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1074 ;; Functions for moving point
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1075 (defun py-previous-statement (count)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1076 "Go to the start of previous Python statement.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1077 If the statement at point is the i'th Python statement, goes to the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1078 start of statement i-COUNT. If there is no such statement, goes to the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1079 first statement. Returns count of statements left to move.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1080 `Statements' do not include blank, comment, or continuation lines."
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1081 (interactive "p") ; numeric prefix arg
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1082 (if (< count 0) (py-next-statement (- count))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1083 (py-goto-initial-line)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1084 (let (start)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1085 (while (and
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1086 (setq start (point)) ; always true -- side effect
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1087 (> count 0)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1088 (zerop (forward-line -1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1089 (py-goto-statement-at-or-above))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1090 (setq count (1- count)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1091 (if (> count 0) (goto-char start)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1092 count))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1093
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1094 (defun py-next-statement (count)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1095 "Go to the start of next Python statement.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1096 If the statement at point is the i'th Python statement, goes to the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1097 start of statement i+COUNT. If there is no such statement, goes to the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1098 last statement. Returns count of statements left to move. `Statements'
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1099 do not include blank, comment, or continuation lines."
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1100 (interactive "p") ; numeric prefix arg
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1101 (if (< count 0) (py-previous-statement (- count))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1102 (beginning-of-line)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1103 (let (start)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1104 (while (and
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1105 (setq start (point)) ; always true -- side effect
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1106 (> count 0)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1107 (py-goto-statement-below))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1108 (setq count (1- count)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1109 (if (> count 0) (goto-char start)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1110 count))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1111
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1112 (defun py-goto-block-up (&optional nomark)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1113 "Move up to start of current block.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1114 Go to the statement that starts the smallest enclosing block; roughly
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1115 speaking, this will be the closest preceding statement that ends with a
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1116 colon and is indented less than the statement you started on. If
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1117 successful, also sets the mark to the starting point.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1118
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1119 `\\[py-mark-block]' can be used afterward to mark the whole code
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1120 block, if desired.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1121
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1122 If called from a program, the mark will not be set if optional argument
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1123 NOMARK is not nil."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1124 (interactive)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1125 (let ((start (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1126 (found nil)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1127 initial-indent)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1128 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1129 ;; if on blank or non-indenting comment line, use the preceding stmt
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1130 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1131 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1132 (py-goto-statement-at-or-above)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1133 (setq found (py-statement-opens-block-p))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1134 ;; search back for colon line indented less
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1135 (setq initial-indent (current-indentation))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1136 (if (zerop initial-indent)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1137 ;; force fast exit
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1138 (goto-char (point-min)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1139 (while (not (or found (bobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1140 (setq found
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1141 (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1142 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1143 (or (py-goto-initial-line) t) ; always true -- side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1144 (< (current-indentation) initial-indent)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1145 (py-statement-opens-block-p))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1146 (if found
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1147 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1148 (or nomark (push-mark start))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1149 (back-to-indentation))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1150 (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1151 (error "Enclosing block not found"))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1152
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1153 (defun beginning-of-python-def-or-class (&optional class)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1154 "Move point to start of def (or class, with prefix arg).
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1155
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1156 Searches back for the closest preceding `def'. If you supply a prefix
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1157 arg, looks for a `class' instead. The docs assume the `def' case;
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1158 just substitute `class' for `def' for the other case.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1159
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1160 If point is in a def statement already, and after the `d', simply
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1161 moves point to the start of the statement.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1162
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1163 Else (point is not in a def statement, or at or before the `d' of a
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1164 def statement), searches for the closest preceding def statement, and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1165 leaves point at its start. If no such statement can be found, leaves
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1166 point at the start of the buffer.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1167
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1168 Returns t iff a def statement is found by these rules.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1169
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1170 Note that doing this command repeatedly will take you closer to the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1171 start of the buffer each time.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1172
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1173 If you want to mark the current def/class, see
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1174 `\\[mark-python-def-or-class]'."
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1175 (interactive "P") ; raw prefix arg
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1176 (let ((at-or-before-p (<= (current-column) (current-indentation)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1177 (start-of-line (progn (beginning-of-line) (point)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1178 (start-of-stmt (progn (py-goto-initial-line) (point))))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1179 (if (or (/= start-of-stmt start-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1180 (not at-or-before-p))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1181 (end-of-line)) ; OK to match on this line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1182 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1183 nil 'move)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1184
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1185 (defun end-of-python-def-or-class (&optional class)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1186 "Move point beyond end of def (or class, with prefix arg) body.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1187
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1188 By default, looks for an appropriate `def'. If you supply a prefix arg,
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1189 looks for a `class' instead. The docs assume the `def' case; just
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1190 substitute `class' for `def' for the other case.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1191
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1192 If point is in a def statement already, this is the def we use.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1193
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1194 Else if the def found by `\\[beginning-of-python-def-or-class]'
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1195 contains the statement you started on, that's the def we use.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1196
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1197 Else we search forward for the closest following def, and use that.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1198
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1199 If a def can be found by these rules, point is moved to the start of
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1200 the line immediately following the def block, and the position of the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1201 start of the def is returned.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1202
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1203 Else point is moved to the end of the buffer, and nil is returned.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1204
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1205 Note that doing this command repeatedly will take you closer to the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1206 end of the buffer each time.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1207
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1208 If you want to mark the current def/class, see
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1209 `\\[mark-python-def-or-class]'."
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1210 (interactive "P") ; raw prefix arg
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1211 (let ((start (progn (py-goto-initial-line) (point)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1212 (which (if class "class" "def"))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1213 (state 'not-found))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1214 ;; move point to start of appropriate def/class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1215 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1216 (setq state 'at-beginning)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1217 ;; else see if beginning-of-python-def-or-class hits container
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1218 (if (and (beginning-of-python-def-or-class class)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1219 (progn (py-goto-beyond-block)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1220 (> (point) start)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1221 (setq state 'at-end)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1222 ;; else search forward
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1223 (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1224 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1225 (progn (setq state 'at-beginning)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1226 (beginning-of-line)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1227 (cond
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1228 ((eq state 'at-beginning) (py-goto-beyond-block) t)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1229 ((eq state 'at-end) t)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1230 ((eq state 'not-found) nil)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1231 (t (error "internal error in end-of-python-def-or-class")))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1232
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1233
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1234 ;; Functions for marking regions
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1235 (defun py-mark-block (&optional extend just-move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1236 "Mark following block of lines. With prefix arg, mark structure.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1237 Easier to use than explain. It sets the region to an `interesting'
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1238 block of succeeding lines. If point is on a blank line, it goes down to
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1239 the next non-blank line. That will be the start of the region. The end
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1240 of the region depends on the kind of line at the start:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1241
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1242 - If a comment, the region will include all succeeding comment lines up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1243 to (but not including) the next non-comment line (if any).
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1244
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1245 - Else if a prefix arg is given, and the line begins one of these
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1246 structures:
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1247
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1248 if elif else try except finally for while def class
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1249
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1250 the region will be set to the body of the structure, including
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1251 following blocks that `belong' to it, but excluding trailing blank
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1252 and comment lines. E.g., if on a `try' statement, the `try' block
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1253 and all (if any) of the following `except' and `finally' blocks
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1254 that belong to the `try' structure will be in the region. Ditto
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1255 for if/elif/else, for/else and while/else structures, and (a bit
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1256 degenerate, since they're always one-block structures) def and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1257 class blocks.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1258
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1259 - Else if no prefix argument is given, and the line begins a Python
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1260 block (see list above), and the block is not a `one-liner' (i.e.,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1261 the statement ends with a colon, not with code), the region will
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1262 include all succeeding lines up to (but not including) the next
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1263 code statement (if any) that's indented no more than the starting
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1264 line, except that trailing blank and comment lines are excluded.
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1265 E.g., if the starting line begins a multi-statement `def'
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1266 structure, the region will be set to the full function definition,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1267 but without any trailing `noise' lines.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1268
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1269 - Else the region will include all succeeding lines up to (but not
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1270 including) the next blank line, or code or indenting-comment line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1271 indented strictly less than the starting line. Trailing indenting
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1272 comment lines are included in this case, but not trailing blank
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1273 lines.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1274
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1275 A msg identifying the location of the mark is displayed in the echo
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1276 area; or do `\\[exchange-point-and-mark]' to flip down to the end.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1277
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1278 If called from a program, optional argument EXTEND plays the role of
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1279 the prefix arg, and if optional argument JUST-MOVE is not nil, just
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1280 moves to the end of the block (& does not set mark or display a msg)."
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1281 (interactive "P") ; raw prefix arg
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1282 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1283 ;; skip over blank lines
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1284 (while (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1285 (looking-at "[ \t]*$") ; while blank line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1286 (not (eobp))) ; & somewhere to go
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1287 (forward-line 1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1288 (if (eobp)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1289 (error "Hit end of buffer without finding a non-blank stmt"))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1290 (let ((initial-pos (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1291 (initial-indent (current-indentation))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1292 last-pos ; position of last stmt in region
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1293 (followers
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1294 '((if elif else) (elif elif else) (else)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1295 (try except finally) (except except) (finally)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1296 (for else) (while else)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1297 (def) (class) ) )
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1298 first-symbol next-symbol)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1299
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1300 (cond
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1301 ;; if comment line, suck up the following comment lines
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1302 ((looking-at "[ \t]*#")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1303 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1304 (re-search-backward "^[ \t]*#") ; and back to last comment in block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1305 (setq last-pos (point)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1306
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1307 ;; else if line is a block line and EXTEND given, suck up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1308 ;; the whole structure
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1309 ((and extend
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1310 (setq first-symbol (py-suck-up-first-keyword) )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1311 (assq first-symbol followers))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1312 (while (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1313 (or (py-goto-beyond-block) t) ; side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1314 (forward-line -1) ; side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1315 (setq last-pos (point)) ; side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1316 (py-goto-statement-below)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1317 (= (current-indentation) initial-indent)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1318 (setq next-symbol (py-suck-up-first-keyword))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1319 (memq next-symbol (cdr (assq first-symbol followers))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1320 (setq first-symbol next-symbol)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1321
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1322 ;; else if line *opens* a block, search for next stmt indented <=
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1323 ((py-statement-opens-block-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1324 (while (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1325 (setq last-pos (point)) ; always true -- side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1326 (py-goto-statement-below)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1327 (> (current-indentation) initial-indent))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1328 nil))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1329
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1330 ;; else plain code line; stop at next blank line, or stmt or
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1331 ;; indenting comment line indented <
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1332 (t
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1333 (while (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1334 (setq last-pos (point)) ; always true -- side effect
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1335 (or (py-goto-beyond-final-line) t)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1336 (not (looking-at "[ \t]*$")) ; stop at blank line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1337 (or
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1338 (>= (current-indentation) initial-indent)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1339 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1340 nil)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1341
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1342 ;; skip to end of last stmt
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1343 (goto-char last-pos)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1344 (py-goto-beyond-final-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1345
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1346 ;; set mark & display
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1347 (if just-move
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1348 () ; just return
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1349 (push-mark (point) 'no-msg)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1350 (forward-line -1)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1351 (message "Mark set after: %s" (py-suck-up-leading-text))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1352 (goto-char initial-pos))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1353
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1354 (defun mark-python-def-or-class (&optional class)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1355 "Set region to body of def (or class, with prefix arg) enclosing point.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1356 Pushes the current mark, then point, on the mark ring (all language
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1357 modes do this, but although it's handy it's never documented ...).
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1358
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1359 In most Emacs language modes, this function bears at least a
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1360 hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1361 `\\[beginning-of-python-def-or-class]'.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1362
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1363 And in earlier versions of Python mode, all 3 were tightly connected.
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1364 Turned out that was more confusing than useful: the `goto start' and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1365 `goto end' commands are usually used to search through a file, and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1366 people expect them to act a lot like `search backward' and `search
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1367 forward' string-search commands. But because Python `def' and `class'
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1368 can nest to arbitrary levels, finding the smallest def containing
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1369 point cannot be done via a simple backward search: the def containing
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1370 point may not be the closest preceding def, or even the closest
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1371 preceding def that's indented less. The fancy algorithm required is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1372 appropriate for the usual uses of this `mark' command, but not for the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1373 `goto' variations.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1374
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1375 So the def marked by this command may not be the one either of the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1376 `goto' commands find: If point is on a blank or non-indenting comment
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1377 line, moves back to start of the closest preceding code statement or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1378 indenting comment line. If this is a `def' statement, that's the def
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1379 we use. Else searches for the smallest enclosing `def' block and uses
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1380 that. Else signals an error.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1381
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1382 When an enclosing def is found: The mark is left immediately beyond
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1383 the last line of the def block. Point is left at the start of the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1384 def, except that: if the def is preceded by a number of comment lines
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1385 followed by (at most) one optional blank line, point is left at the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1386 start of the comments; else if the def is preceded by a blank line,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1387 point is left at its start.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1388
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1389 The intent is to mark the containing def/class and its associated
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1390 documentation, to make moving and duplicating functions and classes
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1391 pleasant."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1392 (interactive "P") ; raw prefix arg
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1393 (let ((start (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1394 (which (if class "class" "def")))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1395 (push-mark start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1396 (if (not (py-go-up-tree-to-keyword which))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1397 (progn (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1398 (error "Enclosing %s not found" which))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1399 ;; else enclosing def/class found
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1400 (setq start (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1401 (py-goto-beyond-block)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1402 (push-mark (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1403 (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1404 (if (zerop (forward-line -1)) ; if there is a preceding line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1405 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1406 (if (looking-at "[ \t]*$") ; it's blank
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1407 (setq start (point)) ; so reset start point
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1408 (goto-char start)) ; else try again
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1409 (if (zerop (forward-line -1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1410 (if (looking-at "[ \t]*#") ; a comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1411 ;; look back for non-comment line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1412 ;; tricky: note that the regexp matches a blank
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1413 ;; line, cuz \n is in the 2nd character class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1414 (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1415 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1416 (forward-line 1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1417 ;; no comment, so go back
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1418 (goto-char start))))))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1419
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1420 (defun py-comment-region (start end &optional uncomment-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1421 "Comment out region of code; with prefix arg, uncomment region.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1422 The lines from the line containing the start of the current region up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1423 to (but not including) the line containing the end of the region are
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1424 commented out, by inserting the string `py-block-comment-prefix' at
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1425 the start of each line. With a prefix arg, removes
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1426 `py-block-comment-prefix' from the start of each line instead."
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1427 (interactive "*r\nP") ; region; raw prefix arg
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1428 (goto-char end) (beginning-of-line) (setq end (point))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1429 (goto-char start) (beginning-of-line) (setq start (point))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1430 (let ((prefix-len (length py-block-comment-prefix)) )
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1431 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1432 (save-restriction
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1433 (narrow-to-region start end)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1434 (while (not (eobp))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1435 (if uncomment-p
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1436 (and (string= py-block-comment-prefix
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1437 (buffer-substring
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1438 (point) (+ (point) prefix-len)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1439 (delete-char prefix-len))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1440 (insert py-block-comment-prefix))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1441 (forward-line 1))))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1442
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1443
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1444 ;; Documentation functions
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1445
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1446 ;; dump the long form of the mode blurb; does the usual doc escapes,
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1447 ;; plus lines of the form ^[vc]:name$ to suck variable & command docs
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1448 ;; out of the right places, along with the keys they're on & current
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1449 ;; values
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1450 (defun py-dump-help-string (str)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1451 (with-output-to-temp-buffer "*Help*"
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1452 (let ((locals (buffer-local-variables))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1453 funckind funcname func funcdoc
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1454 (start 0) mstart end
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1455 keys )
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1456 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1457 (setq mstart (match-beginning 0) end (match-end 0)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1458 funckind (substring str (match-beginning 1) (match-end 1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1459 funcname (substring str (match-beginning 2) (match-end 2))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1460 func (intern funcname))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1461 (princ (substitute-command-keys (substring str start mstart)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1462 (cond
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1463 ((equal funckind "c") ; command
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1464 (setq funcdoc (documentation func)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1465 keys (concat
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1466 "Key(s): "
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1467 (mapconcat 'key-description
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1468 (where-is-internal func py-mode-map)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1469 ", "))))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1470 ((equal funckind "v") ; variable
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1471 (setq funcdoc (substitute-command-keys
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1472 (get func 'variable-documentation))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1473 keys (if (assq func locals)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1474 (concat
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1475 "Local/Global values: "
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1476 (prin1-to-string (symbol-value func))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1477 " / "
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1478 (prin1-to-string (default-value func)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1479 (concat
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1480 "Value: "
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1481 (prin1-to-string (symbol-value func))))))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1482 (t ; unexpected
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1483 (error "Error in py-dump-help-string, tag `%s'" funckind)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1484 (princ (format "\n-> %s:\t%s\t%s\n\n"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1485 (if (equal funckind "c") "Command" "Variable")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1486 funcname keys))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1487 (princ funcdoc)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1488 (terpri)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1489 (setq start end))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1490 (princ (substitute-command-keys (substring str start))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1491 (print-help-return-message)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1492
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1493 (defun py-describe-mode ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1494 "Dump long form of Python-mode docs."
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1495 (interactive)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1496 (py-dump-help-string "Major mode for editing Python files.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1497 Knows about Python indentation, tokens, comments and continuation lines.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1498 Paragraphs are separated by blank lines only.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1499
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1500 Major sections below begin with the string `@'; specific function and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1501 variable docs begin with `->'.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1502
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1503 @EXECUTING PYTHON CODE
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1504
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1505 \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1506 \\[py-execute-region]\tsends the current region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1507 \\[py-shell]\tstarts a Python interpreter window; this will be used by
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1508 \tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1509 %c:py-execute-buffer
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1510 %c:py-execute-region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1511 %c:py-shell
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1512
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1513 @VARIABLES
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1514
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1515 py-indent-offset\tindentation increment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1516 py-block-comment-prefix\tcomment string used by py-comment-region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1517
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1518 py-python-command\tshell command to invoke Python interpreter
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1519 py-scroll-process-buffer\talways scroll Python process buffer
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1520 py-temp-directory\tdirectory used for temp files (if needed)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1521
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1522 py-beep-if-tab-change\tring the bell if tab-width is changed
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1523 %v:py-indent-offset
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1524 %v:py-block-comment-prefix
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1525 %v:py-python-command
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1526 %v:py-scroll-process-buffer
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1527 %v:py-temp-directory
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1528 %v:py-beep-if-tab-change
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1529
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1530 @KINDS OF LINES
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1531
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1532 Each physical line in the file is either a `continuation line' (the
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1533 preceding line ends with a backslash that's not part of a comment, or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1534 the paren/bracket/brace nesting level at the start of the line is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1535 non-zero, or both) or an `initial line' (everything else).
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1536
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1537 An initial line is in turn a `blank line' (contains nothing except
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1538 possibly blanks or tabs), a `comment line' (leftmost non-blank
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1539 character is `#'), or a `code line' (everything else).
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1540
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1541 Comment Lines
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1542
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1543 Although all comment lines are treated alike by Python, Python mode
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1544 recognizes two kinds that act differently with respect to indentation.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1545
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1546 An `indenting comment line' is a comment line with a blank, tab or
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1547 nothing after the initial `#'. The indentation commands (see below)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1548 treat these exactly as if they were code lines: a line following an
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1549 indenting comment line will be indented like the comment line. All
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1550 other comment lines (those with a non-whitespace character immediately
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1551 following the initial `#') are `non-indenting comment lines', and
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1552 their indentation is ignored by the indentation commands.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1553
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1554 Indenting comment lines are by far the usual case, and should be used
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1555 whenever possible. Non-indenting comment lines are useful in cases
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1556 like these:
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1557
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1558 \ta = b # a very wordy single-line comment that ends up being
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1559 \t #... continued onto another line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1560
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1561 \tif a == b:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1562 ##\t\tprint 'panic!' # old code we've `commented out'
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1563 \t\treturn a
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1564
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1565 Since the `#...' and `##' comment lines have a non-whitespace
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1566 character following the initial `#', Python mode ignores them when
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1567 computing the proper indentation for the next line.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1568
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1569 Continuation Lines and Statements
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1570
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1571 The Python-mode commands generally work on statements instead of on
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1572 individual lines, where a `statement' is a comment or blank line, or a
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1573 code line and all of its following continuation lines (if any)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1574 considered as a single logical unit. The commands in this mode
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1575 generally (when it makes sense) automatically move to the start of the
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1576 statement containing point, even if point happens to be in the middle
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1577 of some continuation line.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1578
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1579
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1580 @INDENTATION
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1581
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1582 Primarily for entering new code:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1583 \t\\[indent-for-tab-command]\t indent line appropriately
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1584 \t\\[py-newline-and-indent]\t insert newline, then indent
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1585 \t\\[py-delete-char]\t reduce indentation, or delete single character
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1586
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1587 Primarily for reindenting existing code:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1588 \t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1589 \t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1590
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1591 \t\\[py-indent-region]\t reindent region to match its context
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1592 \t\\[py-shift-region-left]\t shift region left by py-indent-offset
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1593 \t\\[py-shift-region-right]\t shift region right by py-indent-offset
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1594
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1595 Unlike most programming languages, Python uses indentation, and only
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1596 indentation, to specify block structure. Hence the indentation supplied
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1597 automatically by Python-mode is just an educated guess: only you know
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1598 the block structure you intend, so only you can supply correct
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1599 indentation.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1600
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1601 The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1602 the indentation of preceding statements. E.g., assuming
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1603 py-indent-offset is 4, after you enter
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1604 \tif a > 0: \\[py-newline-and-indent]
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1605 the cursor will be moved to the position of the `_' (_ is not a
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1606 character in the file, it's just used here to indicate the location of
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1607 the cursor):
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1608 \tif a > 0:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1609 \t _
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1610 If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1611 to
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1612 \tif a > 0:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1613 \t c = d
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1614 \t _
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1615 Python-mode cannot know whether that's what you intended, or whether
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1616 \tif a > 0:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1617 \t c = d
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1618 \t_
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1619 was your intent. In general, Python-mode either reproduces the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1620 indentation of the (closest code or indenting-comment) preceding
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1621 statement, or adds an extra py-indent-offset blanks if the preceding
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1622 statement has `:' as its last significant (non-whitespace and non-
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1623 comment) character. If the suggested indentation is too much, use
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1624 \\[py-delete-char] to reduce it.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1625
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1626 Continuation lines are given extra indentation. If you don't like the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1627 suggested indentation, change it to something you do like, and Python-
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1628 mode will strive to indent later lines of the statement in the same way.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1629
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1630 If a line is a continuation line by virtue of being in an unclosed
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1631 paren/bracket/brace structure (`list', for short), the suggested
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1632 indentation depends on whether the current line contains the first item
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1633 in the list. If it does, it's indented py-indent-offset columns beyond
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1634 the indentation of the line containing the open bracket. If you don't
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1635 like that, change it by hand. The remaining items in the list will mimic
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1636 whatever indentation you give to the first item.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1637
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1638 If a line is a continuation line because the line preceding it ends with
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1639 a backslash, the third and following lines of the statement inherit their
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1640 indentation from the line preceding them. The indentation of the second
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1641 line in the statement depends on the form of the first (base) line: if
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1642 the base line is an assignment statement with anything more interesting
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1643 than the backslash following the leftmost assigning `=', the second line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1644 is indented two columns beyond that `='. Else it's indented to two
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1645 columns beyond the leftmost solid chunk of non-whitespace characters on
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1646 the base line.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1647
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1648 Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1649 repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1650 structure you intend.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1651 %c:indent-for-tab-command
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1652 %c:py-newline-and-indent
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1653 %c:py-delete-char
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1654
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1655
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1656 The next function may be handy when editing code you didn't write:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1657 %c:py-guess-indent-offset
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1658
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1659
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1660 The remaining `indent' functions apply to a region of Python code. They
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1661 assume the block structure (equals indentation, in Python) of the region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1662 is correct, and alter the indentation in various ways while preserving
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1663 the block structure:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1664 %c:py-indent-region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1665 %c:py-shift-region-left
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1666 %c:py-shift-region-right
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1667
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1668 @MARKING & MANIPULATING REGIONS OF CODE
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1669
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1670 \\[py-mark-block]\t mark block of lines
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1671 \\[mark-python-def-or-class]\t mark smallest enclosing def
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1672 \\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1673 \\[py-comment-region]\t comment out region of code
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1674 \\[universal-argument] \\[py-comment-region]\t uncomment region of code
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1675 %c:py-mark-block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1676 %c:mark-python-def-or-class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1677 %c:py-comment-region
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1678
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1679 @MOVING POINT
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1680
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1681 \\[py-previous-statement]\t move to statement preceding point
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1682 \\[py-next-statement]\t move to statement following point
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1683 \\[py-goto-block-up]\t move up to start of current block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1684 \\[beginning-of-python-def-or-class]\t move to start of def
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1685 \\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1686 \\[end-of-python-def-or-class]\t move to end of def
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1687 \\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1688
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1689 The first two move to one statement beyond the statement that contains
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1690 point. A numeric prefix argument tells them to move that many
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1691 statements instead. Blank lines, comment lines, and continuation lines
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1692 do not count as `statements' for these commands. So, e.g., you can go
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1693 to the first code statement in a file by entering
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1694 \t\\[beginning-of-buffer]\t to move to the top of the file
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1695 \t\\[py-next-statement]\t to skip over initial comments and blank lines
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1696 Or do `\\[py-previous-statement]' with a huge prefix argument.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1697 %c:py-previous-statement
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1698 %c:py-next-statement
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1699 %c:py-goto-block-up
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1700 %c:beginning-of-python-def-or-class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1701 %c:end-of-python-def-or-class
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1702
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1703 @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1704
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1705 `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1706
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1707 `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1708 overall class and def structure of a module.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1709
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1710 `\\[back-to-indentation]' moves point to a line's first non-blank character.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1711
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1712 `\\[indent-relative]' is handy for creating odd indentation.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1713
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1714 @OTHER EMACS HINTS
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1715
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1716 If you don't like the default value of a variable, change its value to
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1717 whatever you do like by putting a `setq' line in your .emacs file.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1718 E.g., to set the indentation increment to 4, put this line in your
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1719 .emacs:
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1720 \t(setq py-indent-offset 4)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1721 To see the value of a variable, do `\\[describe-variable]' and enter the variable
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1722 name at the prompt.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1723
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1724 When entering a key sequence like `C-c C-n', it is not necessary to
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1725 release the CONTROL key after doing the `C-c' part -- it suffices to
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1726 press the CONTROL key, press and release `c' (while still holding down
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1727 CONTROL), press and release `n' (while still holding down CONTROL), &
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1728 then release CONTROL.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1729
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1730 Entering Python mode calls with no arguments the value of the variable
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1731 `python-mode-hook', if that value exists and is not nil; for backward
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1732 compatibility it also tries `py-mode-hook'; see the `Hooks' section of
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1733 the Elisp manual for details.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1734
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1735 Obscure: When python-mode is first loaded, it looks for all bindings
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1736 to newline-and-indent in the global keymap, and shadows them with
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1737 local bindings to py-newline-and-indent."))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1738
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1739
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1740 ;; Helper functions
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1741 (defvar py-parse-state-re
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1742 (concat
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1743 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1744 "\\|"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1745 "^[^ #\t\n]"))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1746
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1747 ;; returns the parse state at point (see parse-partial-sexp docs)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1748 (defun py-parse-state ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1749 (save-excursion
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1750 (let ((here (point)) )
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1751 ;; back up to the first preceding line (if any; else start of
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1752 ;; buffer) that begins with a popular Python keyword, or a non-
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1753 ;; whitespace and non-comment character. These are good places
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1754 ;; to start parsing to see whether where we started is at a
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1755 ;; non-zero nesting level. It may be slow for people who write
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1756 ;; huge code blocks or huge lists ... tough beans.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1757 (re-search-backward py-parse-state-re nil 'move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1758 (beginning-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1759 (parse-partial-sexp (point) here))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1760
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1761 ;; if point is at a non-zero nesting level, returns the number of the
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1762 ;; character that opens the smallest enclosing unclosed list; else
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1763 ;; returns nil.
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1764 (defun py-nesting-level ()
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1765 (let ((status (py-parse-state)) )
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1766 (if (zerop (car status))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1767 nil ; not in a nest
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1768 (car (cdr status))))) ; char# of open bracket
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1769
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1770 ;; t iff preceding line ends with backslash that's not in a comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1771 (defun py-backslash-continuation-line-p ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1772 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1773 (beginning-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1774 (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1775 ;; use a cheap test first to avoid the regexp if possible
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1776 ;; use 'eq' because char-after may return nil
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1777 (eq (char-after (- (point) 2)) ?\\ )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1778 ;; make sure; since eq test passed, there is a preceding line
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1779 (forward-line -1) ; always true -- side effect
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1780 (looking-at py-continued-re))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1781
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1782 ;; t iff current line is a continuation line
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1783 (defun py-continuation-line-p ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1784 (save-excursion
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1785 (beginning-of-line)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1786 (or (py-backslash-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1787 (py-nesting-level))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1788
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1789 ;; go to initial line of current statement; usually this is the line
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1790 ;; we're on, but if we're on the 2nd or following lines of a
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1791 ;; continuation block, we need to go up to the first line of the
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1792 ;; block.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1793 ;;
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1794 ;; Tricky: We want to avoid quadratic-time behavior for long continued
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1795 ;; blocks, whether of the backslash or open-bracket varieties, or a
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1796 ;; mix of the two. The following manages to do that in the usual
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1797 ;; cases.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1798 (defun py-goto-initial-line ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1799 (let ( open-bracket-pos )
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1800 (while (py-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1801 (beginning-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1802 (if (py-backslash-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1803 (while (py-backslash-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1804 (forward-line -1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1805 ;; else zip out of nested brackets/braces/parens
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1806 (while (setq open-bracket-pos (py-nesting-level))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1807 (goto-char open-bracket-pos)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1808 (beginning-of-line))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1809
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1810 ;; go to point right beyond final line of current statement; usually
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1811 ;; this is the start of the next line, but if this is a multi-line
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1812 ;; statement we need to skip over the continuation lines. Tricky:
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1813 ;; Again we need to be clever to avoid quadratic time behavior.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1814 (defun py-goto-beyond-final-line ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1815 (forward-line 1)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1816 (let (state)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1817 (while (and (py-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1818 (not (eobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1819 ;; skip over the backslash flavor
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1820 (while (and (py-backslash-continuation-line-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1821 (not (eobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1822 (forward-line 1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1823 ;; if in nest, zip to the end of the nest
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1824 (setq state (py-parse-state))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1825 (if (and (not (zerop (car state)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1826 (not (eobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1827 (progn
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1828 ;; BUG ALERT: I could swear, from reading the docs, that
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1829 ;; the 3rd argument should be plain 0
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1830 (parse-partial-sexp (point) (point-max) (- 0 (car state))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1831 nil state)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1832 (forward-line 1))))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1833
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1834 ;; t iff statement opens a block == iff it ends with a colon that's
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1835 ;; not in a comment. point should be at the start of a statement
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1836 (defun py-statement-opens-block-p ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1837 (save-excursion
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1838 (let ((start (point))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1839 (finish (progn (py-goto-beyond-final-line) (1- (point))))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1840 (searching t)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1841 (answer nil)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1842 state)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1843 (goto-char start)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1844 (while searching
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1845 ;; look for a colon with nothing after it except whitespace, and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1846 ;; maybe a comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1847 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1848 finish t)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1849 (if (eq (point) finish) ; note: no `else' clause; just
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1850 ; keep searching if we're not at
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1851 ; the end yet
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1852 ;; sure looks like it opens a block -- but it might
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1853 ;; be in a comment
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1854 (progn
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1855 (setq searching nil) ; search is done either way
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1856 (setq state (parse-partial-sexp start
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1857 (match-beginning 0)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1858 (setq answer (not (nth 4 state)))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1859 ;; search failed: couldn't find another interesting colon
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1860 (setq searching nil)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1861 answer)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1862
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1863 ;; go to point right beyond final line of block begun by the current
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1864 ;; line. This is the same as where py-goto-beyond-final-line goes
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1865 ;; unless we're on colon line, in which case we go to the end of the
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1866 ;; block. assumes point is at bolp
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1867 (defun py-goto-beyond-block ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1868 (if (py-statement-opens-block-p)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1869 (py-mark-block nil 'just-move)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1870 (py-goto-beyond-final-line)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1871
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1872 ;; go to start of first statement (not blank or comment or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1873 ;; continuation line) at or preceding point. returns t if there is
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1874 ;; one, else nil
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1875 (defun py-goto-statement-at-or-above ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1876 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1877 (if (looking-at py-blank-or-comment-re)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1878 ;; skip back over blank & comment lines
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1879 ;; note: will skip a blank or comment line that happens to be
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1880 ;; a continuation line too
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1881 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1882 (progn (py-goto-initial-line) t)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1883 nil)
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1884 t))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1885
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1886 ;; go to start of first statement (not blank or comment or
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1887 ;; continuation line) following the statement containing point returns
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1888 ;; t if there is one, else nil
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1889 (defun py-goto-statement-below ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1890 (beginning-of-line)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1891 (let ((start (point)))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1892 (py-goto-beyond-final-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1893 (while (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1894 (looking-at py-blank-or-comment-re)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1895 (not (eobp)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1896 (forward-line 1))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1897 (if (eobp)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1898 (progn (goto-char start) nil)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1899 t)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1900
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1901 ;; go to start of statement, at or preceding point, starting with
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1902 ;; keyword KEY. Skips blank lines and non-indenting comments upward
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1903 ;; first. If that statement starts with KEY, done, else go back to
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1904 ;; first enclosing block starting with KEY. If successful, leaves
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1905 ;; point at the start of the KEY line & returns t. Else leaves point
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1906 ;; at an undefined place & returns nil.
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1907 (defun py-go-up-tree-to-keyword (key)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1908 ;; skip blanks and non-indenting #
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1909 (py-goto-initial-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1910 (while (and
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1911 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1912 (zerop (forward-line -1))) ; go back
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1913 nil)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1914 (py-goto-initial-line)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1915 (let* ((re (concat "[ \t]*" key "\\b"))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1916 (case-fold-search nil) ; let* so looking-at sees this
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1917 (found (looking-at re))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1918 (dead nil))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1919 (while (not (or found dead))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1920 (condition-case nil ; in case no enclosing block
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1921 (py-goto-block-up 'no-mark)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1922 (error (setq dead t)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1923 (or dead (setq found (looking-at re))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1924 (beginning-of-line)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1925 found))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1926
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1927 ;; return string in buffer from start of indentation to end of line;
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1928 ;; prefix "..." if leading whitespace was skipped
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1929 (defun py-suck-up-leading-text ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1930 (save-excursion
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1931 (back-to-indentation)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1932 (concat
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1933 (if (bolp) "" "...")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1934 (buffer-substring (point) (progn (end-of-line) (point))))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1935
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1936 ;; assuming point at bolp, return first keyword ([a-z]+) on the line,
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1937 ;; as a Lisp symbol; return nil if none
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1938 (defun py-suck-up-first-keyword ()
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1939 (let ((case-fold-search nil))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1940 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1941 (intern (buffer-substring (match-beginning 1) (match-end 1)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1942 nil)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1943
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1944 (defun py-make-temp-name ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1945 (make-temp-name
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1946 (concat (file-name-as-directory py-temp-directory) "python")))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1947
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1948 (defun py-delete-file-silently (fname)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1949 (condition-case nil
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1950 (delete-file fname)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1951 (error nil)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1952
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1953 (defun py-kill-emacs-hook ()
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1954 ;; delete our temp files
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1955 (while py-file-queue
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1956 (py-delete-file-silently (car py-file-queue))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1957 (setq py-file-queue (cdr py-file-queue)))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1958 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1959 ;; run the hook we inherited, if any
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1960 (and py-inherited-kill-emacs-hook
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1961 (funcall py-inherited-kill-emacs-hook))))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1962
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1963 ;; make PROCESS's buffer visible, append STRING to it, and force
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1964 ;; display; also make shell-mode believe the user typed this string,
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1965 ;; so that kill-output-from-shell and show-output-from-shell work
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1966 ;; "right"
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1967 (defun py-append-to-process-buffer (process string)
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1968 (let ((cbuf (current-buffer))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1969 (pbuf (process-buffer process))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1970 (py-scroll-process-buffer t))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1971 (set-buffer pbuf)
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1972 (goto-char (point-max))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1973 (move-marker (process-mark process) (point))
2798
59c2acb3e702 (py-append-to-process-buffer): XEmacs doesn't have last-input-start or
Barry Warsaw <barry@python.org>
parents: 2797
diff changeset
1974 (if (not (or py-this-is-emacs-19-p
59c2acb3e702 (py-append-to-process-buffer): XEmacs doesn't have last-input-start or
Barry Warsaw <barry@python.org>
parents: 2797
diff changeset
1975 py-this-is-lucid-emacs-p))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1976 (move-marker last-input-start (point))) ; muck w/ shell-mode
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1977 (funcall (process-filter process) process string)
2798
59c2acb3e702 (py-append-to-process-buffer): XEmacs doesn't have last-input-start or
Barry Warsaw <barry@python.org>
parents: 2797
diff changeset
1978 (if (not (or py-this-is-emacs-19-p
59c2acb3e702 (py-append-to-process-buffer): XEmacs doesn't have last-input-start or
Barry Warsaw <barry@python.org>
parents: 2797
diff changeset
1979 py-this-is-lucid-emacs-p))
2018
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1980 (move-marker last-input-end (point))) ; muck w/ shell-mode
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1981 (set-buffer cbuf))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1982 (sit-for 0))
5a0775ce7b31 Initial revision
Barry Warsaw <barry@python.org>
parents:
diff changeset
1983
2498
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1984 (defun py-keep-region-active ()
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1985 ;; do whatever is necessary to keep the region active in XEmacs.
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1986 ;; Ignore byte-compiler warnings you might see. Also note that
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1987 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1988 ;; require us to take explicit action.
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1989 (and (boundp 'zmacs-region-stays)
ddf60bc7ed30 (py-keep-region-active): new function.
Barry Warsaw <barry@python.org>
parents: 2497
diff changeset
1990 (setq zmacs-region-stays t)))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1991
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
1992
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
1993 (defconst py-version "$Revision$"
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
1994 "`python-mode' version number.")
2799
4a998b1a331f #changed all email address to go through python.org
Barry Warsaw <barry@python.org>
parents: 2798
diff changeset
1995 (defconst py-help-address "python-mode@python.org"
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
1996 "Address accepting submission of bug reports.")
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
1997
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
1998 (defun py-version ()
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
1999 "Echo the current version of `python-mode' in the minibuffer."
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2000 (interactive)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2001 (message "Using `python-mode' version %s" py-version)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2002 (py-keep-region-active))
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2003
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2004 ;; only works under Emacs 19
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2005 ;(eval-when-compile
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2006 ; (require 'reporter))
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2007
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2008 (defun py-submit-bug-report (enhancement-p)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2009 "Submit via mail a bug report on `python-mode'.
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2010 With \\[universal-argument] just submit an enhancement request."
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2011 (interactive
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2012 (list (not (y-or-n-p
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2013 "Is this a bug report? (hit `n' to send other comments) "))))
2543
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
2014 (let ((reporter-prompt-for-summary-p (if enhancement-p
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
2015 "(Very) brief summary: "
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
2016 t)))
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2017 (require 'reporter)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2018 (reporter-submit-bug-report
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2019 py-help-address ;address
2543
1a448dd84edc (py-submit-bug-report): fixed summary querying
Barry Warsaw <barry@python.org>
parents: 2537
diff changeset
2020 (concat "python-mode " py-version) ;pkgname
2495
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2021 ;; varlist
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2022 (if enhancement-p nil
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2023 '(py-python-command
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2024 py-indent-offset
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2025 py-block-comment-prefix
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2026 py-scroll-process-buffer
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2027 py-temp-directory
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2028 py-beep-if-tab-change))
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2029 nil ;pre-hooks
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2030 nil ;post-hooks
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2031 "Dear Barry,") ;salutation
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2032 (if enhancement-p nil
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2033 (set-mark (point))
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2034 (insert
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2035 "Please replace this text with a sufficiently large code sample\n\
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2036 and an exact recipe so that I can reproduce your problem. Failure\n\
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2037 to do so may mean a greater delay in fixing your bug.\n\n")
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2038 (exchange-point-and-mark)
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2039 (py-keep-region-active))))
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2040
718c65d0e12d (py-mode-map): py-mark-block moved to C-c C-m (was C-c C-b).
Barry Warsaw <barry@python.org>
parents: 2493
diff changeset
2041
2493
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2042 ;; arrange to kill temp files when Emacs exists
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2043 (if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2044 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2045 ;; have to trust that other people are as respectful of our hook
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2046 ;; fiddling as we are of theirs
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2047 (if (boundp 'py-inherited-kill-emacs-hook)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2048 ;; we were loaded before -- trust others not to have screwed us
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2049 ;; in the meantime (no choice, really)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2050 nil
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2051 ;; else arrange for our hook to run theirs
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2052 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2053 (setq kill-emacs-hook 'py-kill-emacs-hook)))
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2054
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2055
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2056
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2057 (provide 'python-mode)
d9481354fdc3 #initial stylistic rewrite
Barry Warsaw <barry@python.org>
parents: 2041
diff changeset
2058 ;;; python-mode.el ends here