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