annotate Misc/python-mode.el @ 3937:decd13f6629d legacy-trunk

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