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