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