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