annotate lisp/modes/python-mode.el @ 157:6b37e6ddd302 r20-3b5

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