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