annotate Misc/python-mode-old.el @ 2525:16ea151aeec3 legacy-trunk

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