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