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