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