annotate Misc/python-mode.el @ 2498:ddf60bc7ed30 legacy-trunk

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