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