annotate Misc/python-mode-old.el @ 3720:b341def4eef8 legacy-trunk

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