Mercurial > emacs
annotate lisp/progmodes/delphi.el @ 58456:1eaf1ae3f842
*** empty log message ***
author | Jay Belanger <jay.p.belanger@gmail.com> |
---|---|
date | Tue, 23 Nov 2004 06:18:51 +0000 |
parents | e8824c4f5f7e |
children | 8fb97ebd6910 8e5779acd195 |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
35624
diff
changeset
|
1 ;;; delphi.el --- major mode for editing Delphi source (Object Pascal) in Emacs |
25072 | 2 |
3 ;; Copyright (C) 1998, 1999 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Ray Blaak <blaak@infomatch.com> | |
6 ;; Keywords: languages | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify it under |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
11 ;; the terms of the GNU General Public License as published by the Free |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
12 ;; Software Foundation; either version 2, or (at your option) any later |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
13 ;; version. |
25072 | 14 |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, but WITHOUT ANY |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
16 ;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
17 ;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
18 ;; details. |
25072 | 19 |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License along with |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
21 ;; GNU Emacs; see the file COPYING. If not, write to the Free Software |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
22 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25072 | 23 |
24 ;;; Commentary: | |
25 | |
26 ;; To enter Delphi mode when you find a Delphi source file, one must override | |
27 ;; the auto-mode-alist to associate Delphi with .pas (and .dpr and .dpk) | |
28 ;; files. Emacs, by default, will otherwise enter Pascal mode. E.g. | |
29 ;; | |
30 ;; (autoload 'delphi-mode "delphi") | |
31 ;; (setq auto-mode-alist | |
32 ;; (cons '("\\.\\(pas\\|dpr\\|dpk\\)$" . delphi-mode) auto-mode-alist)) | |
33 | |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
34 ;; To get keyword, comment, and string literal coloring, be sure that font-lock |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
35 ;; is running. One can manually do M-x font-lock-mode in a Delphi buffer, or |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
36 ;; one can put in .emacs: |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
37 ;; |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
38 ;; (add-hook 'delphi-mode-hook 'turn-on-font-lock) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
39 |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
40 ;; If font-lock is not loaded by default, you might have to do: |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
41 ;; |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
42 ;; (autoload 'font-lock-mode "font-lock") |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
43 ;; (autoload 'turn-on-font-lock "font-lock") |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
44 ;; (setq font-lock-support-mode 'lazy-lock-mode) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
45 ;; |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
46 ;; Lazy lock is very necessary for faster screen updates. |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
47 |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
48 ;; For good performance, be sure to byte-compile delphi.el, e.g. |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
49 ;; |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
50 ;; M-x byte-compile-file <give the path to delphi.el when prompted> |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
51 |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
52 ;; This will generate delphi.elc, which will be loaded instead of delphi.el |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
53 ;; when delphi-mode is autoloaded. |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
54 |
25072 | 55 ;; When you have entered Delphi mode, you may get more info by pressing |
56 ;; C-h m. | |
57 | |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
58 ;; This delphi mode implementation is fairly tolerant of syntax errors, relying |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
59 ;; as much as possible on the indentation of the previous statement. This also |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
60 ;; makes it faster and simpler, since there is less searching for properly |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
61 ;; constructed beginnings. |
25072 | 62 |
63 ;;; Code: | |
64 | |
65 (provide 'delphi) | |
66 | |
67 (eval-and-compile | |
68 ;; Allow execution on pre Emacs 20 versions. | |
69 (or (fboundp 'when) | |
70 (defmacro when (test &rest body) | |
71 `(if ,test (progn ,@body)))) | |
72 (or (fboundp 'unless) | |
73 (defmacro unless (test &rest body) | |
74 `(if (not ,test) (progn ,@body)))) | |
75 (or (fboundp 'defgroup) | |
76 (defmacro defgroup (group val docs &rest group-attributes) | |
77 `(defvar ,group ,val ,docs))) | |
78 (or (fboundp 'defcustom) | |
79 (defmacro defcustom (val-name val docs &rest custom-attributes) | |
80 `(defvar ,val-name ,val ,docs))) | |
81 (or (fboundp 'cadr) | |
82 (defmacro cadr (list) `(car (cdr ,list)))) | |
83 (or (fboundp 'cddr) | |
84 (defmacro cddr (list) `(cdr (cdr ,list)))) | |
85 (or (fboundp 'with-current-buffer) | |
86 (defmacro with-current-buffer (buf &rest forms) | |
87 `(save-excursion (set-buffer ,buf) ,@forms))) | |
88 ) | |
89 | |
90 (defgroup delphi nil | |
91 "Major mode for editing Delphi source in Emacs" | |
27551
2dd8115a9f72
(delphi): Add :version to defgroup.
Dave Love <fx@gnu.org>
parents:
25317
diff
changeset
|
92 :version "21.1" |
25072 | 93 :group 'languages) |
94 | |
95 (defconst delphi-debug nil | |
96 "True if in debug mode.") | |
97 | |
98 (defcustom delphi-search-path "." | |
99 "*Directories to search when finding external units. It is a list of | |
100 directory strings. If only a single directory, it can be a single | |
101 string instead of a list. If a directory ends in \"...\" then that | |
102 directory is recursively searched." | |
103 :type 'string | |
104 :group 'delphi) | |
105 | |
106 (defcustom delphi-indent-level 3 | |
107 "*Indentation of Delphi statements with respect to containing block. E.g. | |
108 | |
109 begin | |
110 // This is an indent of 3. | |
111 end;" | |
112 :type 'integer | |
113 :group 'delphi) | |
114 | |
115 (defcustom delphi-compound-block-indent 0 | |
116 "*Extra indentation for blocks in compound statements. E.g. | |
117 | |
118 // block indent = 0 vs // block indent = 2 | |
119 if b then if b then | |
120 begin begin | |
121 end else begin end | |
122 end; else | |
123 begin | |
124 end;" | |
125 :type 'integer | |
126 :group 'delphi) | |
127 | |
128 (defcustom delphi-case-label-indent delphi-indent-level | |
129 "*Extra indentation for case statement labels. E.g. | |
130 | |
131 // case indent = 0 vs // case indent = 3 | |
132 case value of case value of | |
133 v1: process_v1; v1: process_v1; | |
134 v2: process_v2; v2: process_v2; | |
135 else else | |
136 process_else; process_else; | |
137 end; end;" | |
138 :type 'integer | |
139 :group 'delphi) | |
140 | |
141 (defcustom delphi-verbose t ; nil | |
142 "*If true then delphi token processing progress is reported to the user." | |
143 :type 'boolean | |
144 :group 'delphi) | |
145 | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
146 (defcustom delphi-tab-always-indents t |
25072 | 147 "*Non-nil means TAB in Delphi mode should always reindent the current line, |
148 regardless of where in the line point is when the TAB command is used." | |
149 :type 'boolean | |
150 :group 'delphi) | |
151 | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
152 (defcustom delphi-newline-always-indents t |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
153 "*Non-nil means NEWLINE in Delphi mode should always reindent the current |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
154 line, insert a blank line and move to the default indent column of the blank |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
155 line. If nil, then no indentation occurs, and NEWLINE does the usual |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
156 behaviour. This is useful when one needs to do customized indentation that |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
157 differs from the default." |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
158 :type 'boolean |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
159 :group 'delphi) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
160 |
25072 | 161 (defcustom delphi-comment-face 'font-lock-comment-face |
162 "*Face used to color delphi comments." | |
35624
6878c61d1877
(delphi-comment-face, delphi-string-face)
Dave Love <fx@gnu.org>
parents:
27647
diff
changeset
|
163 :type 'face |
25072 | 164 :group 'delphi) |
165 | |
166 (defcustom delphi-string-face 'font-lock-string-face | |
167 "*Face used to color delphi strings." | |
35624
6878c61d1877
(delphi-comment-face, delphi-string-face)
Dave Love <fx@gnu.org>
parents:
27647
diff
changeset
|
168 :type 'face |
25072 | 169 :group 'delphi) |
170 | |
171 (defcustom delphi-keyword-face 'font-lock-keyword-face | |
172 "*Face used to color delphi keywords." | |
35624
6878c61d1877
(delphi-comment-face, delphi-string-face)
Dave Love <fx@gnu.org>
parents:
27647
diff
changeset
|
173 :type 'face |
25072 | 174 :group 'delphi) |
175 | |
176 (defcustom delphi-other-face nil | |
177 "*Face used to color everything else." | |
35624
6878c61d1877
(delphi-comment-face, delphi-string-face)
Dave Love <fx@gnu.org>
parents:
27647
diff
changeset
|
178 :type 'face |
25072 | 179 :group 'delphi) |
180 | |
181 (defconst delphi-directives | |
182 '(absolute abstract assembler automated cdecl default dispid dynamic | |
183 export external far forward index inline message name near nodefault | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
184 overload override pascal private protected public published read readonly |
25072 | 185 register reintroduce resident resourcestring safecall stdcall stored |
186 virtual write writeonly) | |
187 "Delphi4 directives.") | |
188 | |
189 (defconst delphi-keywords | |
190 (append | |
191 '(;; Keywords. | |
192 and array as asm at begin case class const constructor contains | |
193 destructor dispinterface div do downto else end except exports | |
194 file finalization finally for function goto if implementation implements | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
195 in inherited initialization interface is label library mod nil not |
25072 | 196 of object on or out package packed procedure program property |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
197 raise record repeat requires result self set shl shr then threadvar |
25072 | 198 to try type unit uses until var while with xor |
199 | |
200 ;; These routines should be keywords, if Borland had the balls. | |
201 break exit) | |
202 | |
203 ;; We want directives to look like keywords. | |
204 delphi-directives) | |
205 "Delphi4 keywords.") | |
206 | |
207 (defconst delphi-previous-terminators `(semicolon comma) | |
208 "Expression/statement terminators that denote a previous expression.") | |
209 | |
210 (defconst delphi-comments | |
211 '(comment-single-line comment-multi-line-1 comment-multi-line-2) | |
212 "Tokens that represent comments.") | |
213 | |
214 (defconst delphi-strings | |
215 '(string double-quoted-string) | |
216 "Tokens that represent string literals.") | |
217 | |
218 (defconst delphi-whitespace `(space newline ,@delphi-comments) | |
219 "Tokens that are considered whitespace.") | |
220 | |
221 (defconst delphi-routine-statements | |
222 '(procedure function constructor destructor property) | |
223 "Marks the start of a routine, or routine-ish looking expression.") | |
224 | |
225 (defconst delphi-body-expr-statements '(if while for on) | |
226 "Statements that have either a single statement or a block as a body and also | |
227 are followed by an expression.") | |
228 | |
229 (defconst delphi-expr-statements `(case ,@delphi-body-expr-statements) | |
230 "Expression statements contain expressions after their keyword.") | |
231 | |
232 (defconst delphi-body-statements `(else ,@delphi-body-expr-statements) | |
233 "Statements that have either a single statement or a block as a body.") | |
234 | |
235 (defconst delphi-expr-delimiters '(then do of) | |
236 "Expression delimiter tokens.") | |
237 | |
238 (defconst delphi-binary-ops | |
239 '(plus minus equals not-equals times divides div mod and or xor) | |
240 "Delphi binary operations.") | |
241 | |
242 (defconst delphi-visibilities '(public private protected published automated) | |
243 "Class visibilities.") | |
244 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
245 (defconst delphi-block-statements |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
246 '(begin try case repeat initialization finalization asm) |
25072 | 247 "Statements that contain multiple substatements.") |
248 | |
249 (defconst delphi-mid-block-statements | |
250 `(except finally ,@delphi-visibilities) | |
251 "Statements that mark mid sections of the enclosing block.") | |
252 | |
253 (defconst delphi-end-block-statements `(end until) | |
254 "Statements that end block sections.") | |
255 | |
256 (defconst delphi-match-block-statements | |
257 `(,@delphi-end-block-statements ,@delphi-mid-block-statements) | |
258 "Statements that match the indentation of the parent block.") | |
259 | |
27647
c8d739c1aaf0
Make resourcestring a declaration region, like const and var.
Ray Blaak <blaak@infomatch.com>
parents:
27551
diff
changeset
|
260 (defconst delphi-decl-sections '(type const var label resourcestring) |
25072 | 261 "Denotes the start of a declaration section.") |
262 | |
263 (defconst delphi-class-types '(class object) | |
264 "Class types.") | |
265 | |
266 (defconst delphi-composite-types `(,@delphi-class-types record) | |
267 "Types that contain declarations within them.") | |
268 | |
269 (defconst delphi-unit-sections | |
270 '(interface implementation program library package) | |
271 "Unit sections within which the indent is 0.") | |
272 | |
273 (defconst delphi-use-clauses `(uses requires exports contains) | |
274 "Statements that refer to foreign symbols.") | |
275 | |
276 (defconst delphi-unit-statements | |
277 `(,@delphi-use-clauses ,@delphi-unit-sections initialization finalization) | |
278 "Statements indented at level 0.") | |
279 | |
280 (defconst delphi-decl-delimiters | |
281 `(,@delphi-decl-sections ,@delphi-unit-statements | |
282 ,@delphi-routine-statements) | |
283 "Statements that a declaration statement should align with.") | |
284 | |
285 (defconst delphi-decl-matchers | |
286 `(begin ,@delphi-decl-sections) | |
287 "Statements that should match to declaration statement indentation.") | |
288 | |
289 (defconst delphi-enclosing-statements | |
290 `(,@delphi-block-statements ,@delphi-mid-block-statements | |
291 ,@delphi-decl-sections ,@delphi-use-clauses ,@delphi-routine-statements) | |
292 "Delimits an enclosing statement.") | |
293 | |
294 (defconst delphi-previous-statements | |
295 `(,@delphi-unit-statements ,@delphi-routine-statements) | |
296 "Delimits a previous statement.") | |
297 | |
298 (defconst delphi-previous-enclosing-statements | |
299 `(,@delphi-block-statements ,@delphi-mid-block-statements | |
300 ,@delphi-decl-sections) | |
301 "Delimits a previous enclosing statement.") | |
302 | |
303 (defconst delphi-begin-enclosing-tokens | |
304 `(,@delphi-block-statements ,@delphi-mid-block-statements) | |
305 "Tokens that a begin token indents from.") | |
306 | |
307 (defconst delphi-begin-previous-tokens | |
308 `(,@delphi-decl-sections ,@delphi-routine-statements) | |
309 "Tokens that a begin token aligns with, but only if not part of a nested | |
310 routine.") | |
311 | |
312 (defconst delphi-space-chars "\000-\011\013- ") ; all except \n | |
313 (defconst delphi-non-space-chars (concat "^" delphi-space-chars)) | |
314 (defconst delphi-spaces-re (concat "[" delphi-space-chars "]*")) | |
315 (defconst delphi-leading-spaces-re (concat "^" delphi-spaces-re)) | |
316 (defconst delphi-word-chars "a-zA-Z0-9_") | |
317 | |
318 (defmacro delphi-save-match-data (&rest forms) | |
319 ;; Executes the forms such that the current match data is preserved, so as | |
320 ;; not to disturb any existing search results. | |
321 `(let ((data (match-data))) | |
322 (unwind-protect | |
323 (progn ,@forms) | |
324 (set-match-data data)))) | |
325 | |
326 (defmacro delphi-save-excursion (&rest forms) | |
327 ;; Executes the forms such that any movements have no effect, including | |
328 ;; searches. | |
329 `(save-excursion | |
330 (delphi-save-match-data | |
331 (let ((inhibit-point-motion-hooks t) | |
332 (deactivate-mark nil)) | |
333 (progn ,@forms))))) | |
334 | |
335 (defmacro delphi-save-state (&rest forms) | |
336 ;; Executes the forms such that any buffer modifications do not have any side | |
337 ;; effects beyond the buffer's actual content changes. | |
338 `(let ((delphi-ignore-changes t) | |
339 (old-supersession-threat | |
340 (symbol-function 'ask-user-about-supersession-threat)) | |
341 (buffer-read-only nil) | |
342 (inhibit-read-only t) | |
343 (buffer-undo-list t) | |
344 (before-change-functions nil) | |
345 (after-change-functions nil) | |
346 (modified (buffer-modified-p))) | |
347 ;; Disable any queries about editing obsolete files. | |
348 (fset 'ask-user-about-supersession-threat (lambda (fn))) | |
349 (unwind-protect | |
350 (progn ,@forms) | |
351 (set-buffer-modified-p modified) | |
352 (fset 'ask-user-about-supersession-threat old-supersession-threat)))) | |
353 | |
354 (defsubst delphi-is (element in-set) | |
355 ;; If the element is in the set, the element cdr is returned, otherwise nil. | |
356 (memq element in-set)) | |
357 | |
358 (defun delphi-string-of (start end) | |
359 ;; Returns the buffer string from start to end. | |
360 (buffer-substring-no-properties start end)) | |
361 | |
362 (defun delphi-looking-at-string (p s) | |
363 ;; True if point p marks the start of string s. s is not a regular | |
364 ;; expression. | |
365 (let ((limit (+ p (length s)))) | |
366 (and (<= limit (point-max)) | |
367 (string= s (delphi-string-of p limit))))) | |
368 | |
369 (defun delphi-token-of (kind start end) | |
370 ;; Constructs a token from a kind symbol and its start/end points. | |
371 `[,kind ,start ,end]) | |
372 | |
373 (defsubst delphi-token-kind (token) | |
374 ;; Returns the kind symbol of the token. | |
375 (if token (aref token 0) nil)) | |
376 | |
377 (defun delphi-set-token-kind (token to-kind) | |
378 ;; Sets the kind symbol of the token. | |
379 (if token (aset token 0 to-kind))) | |
380 | |
381 (defsubst delphi-token-start (token) | |
382 ;; Returns the start point of the token. | |
383 (if token (aref token 1) (point-min))) | |
384 | |
385 (defsubst delphi-token-end (token) | |
386 ;; Returns the end point of the token. | |
387 (if token (aref token 2) (point-min))) | |
388 | |
389 (defun delphi-set-token-start (token start) | |
390 ;; Sets the start point of the token. | |
391 (if token (aset token 1 start))) | |
392 | |
393 (defun delphi-set-token-end (token end) | |
394 ;; Sets the end point of the token. | |
395 (if token (aset token 2 end))) | |
396 | |
397 (defun delphi-token-string (token) | |
398 ;; Returns the string image of the token. | |
399 (if token | |
400 (delphi-string-of (delphi-token-start token) (delphi-token-end token)) | |
401 "")) | |
402 | |
403 (defun delphi-in-token (p token) | |
404 ;; Returns true if the point p is within the token's start/end points. | |
405 (and (<= (delphi-token-start token) p) (< p (delphi-token-end token)))) | |
406 | |
407 (defun delphi-column-of (p) | |
408 ;; Returns the column of the point p. | |
409 (save-excursion (goto-char p) (current-column))) | |
410 | |
411 (defun delphi-face-of (token-kind) | |
412 ;; Returns the face property appropriate for the token kind. | |
413 (cond ((delphi-is token-kind delphi-comments) delphi-comment-face) | |
414 ((delphi-is token-kind delphi-strings) delphi-string-face) | |
415 ((delphi-is token-kind delphi-keywords) delphi-keyword-face) | |
416 (delphi-other-face))) | |
417 | |
418 (defvar delphi-progress-last-reported-point nil | |
419 "The last point at which progress was reported.") | |
420 | |
421 (defconst delphi-parsing-progress-step 16384 | |
422 "Number of chars to process before the next parsing progress report.") | |
423 (defconst delphi-scanning-progress-step 2048 | |
424 "Number of chars to process before the next scanning progress report.") | |
425 (defconst delphi-fontifying-progress-step delphi-scanning-progress-step | |
426 "Number of chars to process before the next fontification progress report.") | |
427 | |
428 (defun delphi-progress-start () | |
429 ;; Initializes progress reporting. | |
430 (setq delphi-progress-last-reported-point nil)) | |
431 | |
432 (defun delphi-progress-done (&rest msgs) | |
433 ;; Finalizes progress reporting. | |
434 (setq delphi-progress-last-reported-point nil) | |
435 (when delphi-verbose | |
436 (if (null msgs) | |
437 (message "") | |
438 (apply #'message msgs)))) | |
439 | |
440 (defun delphi-step-progress (p desc step-size) | |
441 ;; If enough distance has elapsed since the last reported point, then report | |
442 ;; the current progress to the user. | |
443 (cond ((null delphi-progress-last-reported-point) | |
444 ;; This is the first progress step. | |
445 (setq delphi-progress-last-reported-point p)) | |
446 | |
447 ((and delphi-verbose | |
448 (>= (abs (- p delphi-progress-last-reported-point)) step-size)) | |
449 ;; Report the percentage complete. | |
450 (setq delphi-progress-last-reported-point p) | |
451 (message "%s %s ... %d%%" | |
452 desc (buffer-name) (/ (* 100 p) (point-max)))))) | |
453 | |
454 (defun delphi-next-line-start (&optional from-point) | |
455 ;; Returns the first point of the next line. | |
456 (let ((curr-point (point)) | |
457 (next nil)) | |
458 (if from-point (goto-char from-point)) | |
459 (end-of-line) | |
460 (setq next (min (1+ (point)) (point-max))) | |
461 (goto-char curr-point) | |
462 next)) | |
463 | |
464 (defun delphi-set-text-properties (from to properties) | |
465 ;; Like `set-text-properties', except we do not consider this to be a buffer | |
466 ;; modification. | |
467 (delphi-save-state | |
468 (set-text-properties from to properties))) | |
469 | |
470 (defun delphi-literal-kind (p) | |
471 ;; Returns the literal kind the point p is in (or nil if not in a literal). | |
472 (if (and (<= (point-min) p) (<= p (point-max))) | |
473 (get-text-property p 'token))) | |
474 | |
475 (defun delphi-literal-start-pattern (literal-kind) | |
476 ;; Returns the start pattern of the literal kind. | |
477 (cdr (assoc literal-kind | |
478 '((comment-single-line . "//") | |
479 (comment-multi-line-1 . "{") | |
480 (comment-multi-line-2 . "(*") | |
481 (string . "'") | |
482 (double-quoted-string . "\""))))) | |
483 | |
484 (defun delphi-literal-end-pattern (literal-kind) | |
485 ;; Returns the end pattern of the literal kind. | |
486 (cdr (assoc literal-kind | |
487 '((comment-single-line . "\n") | |
488 (comment-multi-line-1 . "}") | |
489 (comment-multi-line-2 . "*)") | |
490 (string . "'") | |
491 (double-quoted-string . "\""))))) | |
492 | |
493 (defun delphi-literal-stop-pattern (literal-kind) | |
494 ;; Returns the pattern that delimits end of the search for the literal kind. | |
495 ;; These are regular expressions. | |
496 (cdr (assoc literal-kind | |
497 '((comment-single-line . "\n") | |
498 (comment-multi-line-1 . "}") | |
499 (comment-multi-line-2 . "\\*)") | |
500 ;; Strings cannot span lines. | |
501 (string . "['\n]") | |
502 (double-quoted-string . "[\"\n]"))))) | |
503 | |
504 (defun delphi-is-literal-start (p) | |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
505 ;; True if the point p is at the start point of a (completed) literal. |
25072 | 506 (let* ((kind (delphi-literal-kind p)) |
507 (pattern (delphi-literal-start-pattern kind))) | |
508 (or (null kind) ; Non-literals are considered as start points. | |
509 (delphi-looking-at-string p pattern)))) | |
510 | |
511 (defun delphi-is-literal-end (p) | |
512 ;; True if the point p is at the end point of a (completed) literal. | |
513 (let* ((kind (delphi-literal-kind (1- p))) | |
514 (pattern (delphi-literal-end-pattern kind))) | |
515 (or (null kind) ; Non-literals are considered as end points. | |
516 | |
517 (and (delphi-looking-at-string (- p (length pattern)) pattern) | |
518 (or (not (delphi-is kind delphi-strings)) | |
519 ;; Special case: string delimiters are start/end ambiguous. | |
520 ;; We have an end only if there is some string content (at | |
521 ;; least a starting delimiter). | |
522 (not (delphi-is-literal-end (1- p))))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
523 |
25072 | 524 ;; Special case: strings cannot span lines. |
525 (and (delphi-is kind delphi-strings) (eq ?\n (char-after (1- p))))))) | |
526 | |
527 (defun delphi-is-stable-literal (p) | |
528 ;; True if the point p marks a stable point. That is, a point outside of a | |
529 ;; literal region, inside of a literal region, or adjacent to completed | |
530 ;; literal regions. | |
531 (let ((at-start (delphi-is-literal-start p)) | |
532 (at-end (delphi-is-literal-end p))) | |
533 (or (>= p (point-max)) | |
534 (and at-start at-end) | |
535 (and (not at-start) (not at-end) | |
536 (eq (delphi-literal-kind (1- p)) (delphi-literal-kind p)))))) | |
537 | |
538 (defun delphi-complete-literal (literal-kind limit) | |
539 ;; Continues the search for a literal's true end point and returns the | |
540 ;; point past the end pattern (if found) or the limit (if not found). | |
541 (let ((pattern (delphi-literal-stop-pattern literal-kind))) | |
542 (if (not (stringp pattern)) | |
543 (error "Invalid literal kind %S" literal-kind) | |
544 ;; Search up to the limit. | |
545 (re-search-forward pattern limit 'goto-limit-on-fail) | |
546 (point)))) | |
547 | |
548 (defun delphi-literal-text-properties (kind) | |
549 ;; Creates a list of text properties for the literal kind. | |
550 (if (and (boundp 'font-lock-mode) | |
551 font-lock-mode) | |
552 (list 'token kind 'face (delphi-face-of kind) 'lazy-lock t) | |
553 (list 'token kind))) | |
554 | |
555 (defun delphi-parse-next-literal (limit) | |
556 ;; Searches for the next literal region (i.e. comment or string) and sets the | |
557 ;; the point to its end (or the limit, if not found). The literal region is | |
558 ;; marked as such with a text property, to speed up tokenizing during face | |
559 ;; coloring and indentation scanning. | |
560 (let ((search-start (point))) | |
561 (cond ((not (delphi-is-literal-end search-start)) | |
562 ;; We are completing an incomplete literal. | |
563 (let ((kind (delphi-literal-kind (1- search-start)))) | |
564 (delphi-complete-literal kind limit) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
565 (delphi-set-text-properties |
25072 | 566 search-start (point) (delphi-literal-text-properties kind)))) |
567 | |
568 ((re-search-forward | |
569 "\\(//\\)\\|\\({\\)\\|\\((\\*\\)\\|\\('\\)\\|\\(\"\\)" | |
570 limit 'goto-limit-on-fail) | |
571 ;; We found the start of a new literal. Find its end and mark it. | |
572 (let ((kind (cond ((match-beginning 1) 'comment-single-line) | |
573 ((match-beginning 2) 'comment-multi-line-1) | |
574 ((match-beginning 3) 'comment-multi-line-2) | |
575 ((match-beginning 4) 'string) | |
576 ((match-beginning 5) 'double-quoted-string))) | |
577 (start (match-beginning 0))) | |
578 (delphi-set-text-properties search-start start nil) | |
579 (delphi-complete-literal kind limit) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
580 (delphi-set-text-properties |
25072 | 581 start (point) (delphi-literal-text-properties kind)))) |
582 | |
583 ;; Nothing found. Mark it as a non-literal. | |
584 ((delphi-set-text-properties search-start limit nil))) | |
585 (delphi-step-progress (point) "Parsing" delphi-parsing-progress-step))) | |
586 | |
587 (defun delphi-literal-token-at (p) | |
588 ;; Returns the literal token surrounding the point p, or nil if none. | |
589 (let ((kind (delphi-literal-kind p))) | |
590 (when kind | |
591 (let ((start (previous-single-property-change (1+ p) 'token)) | |
592 (end (next-single-property-change p 'token))) | |
593 (delphi-token-of kind (or start (point-min)) (or end (point-max))))))) | |
594 | |
595 (defun delphi-point-token-at (p kind) | |
596 ;; Returns the single character token at the point p. | |
597 (delphi-token-of kind p (1+ p))) | |
598 | |
599 (defsubst delphi-char-token-at (p char kind) | |
600 ;; Returns the token at the point p that describes the specified character. | |
601 ;; If not actually over such a character, nil is returned. | |
602 (when (eq char (char-after p)) | |
603 (delphi-token-of kind p (1+ p)))) | |
604 | |
605 (defun delphi-charset-token-at (p charset kind) | |
606 ;; Returns the token surrounding point p that contains only members of the | |
607 ;; character set. | |
608 (let ((currp (point)) | |
609 (end nil) | |
610 (start nil) | |
611 (token nil)) | |
612 (goto-char p) | |
613 (when (> (skip-chars-forward charset) 0) | |
614 (setq end (point)) | |
615 (goto-char (1+ p)) | |
616 (skip-chars-backward charset) | |
617 (setq token (delphi-token-of kind (point) end))) | |
618 (goto-char currp) | |
619 token)) | |
620 | |
621 (defun delphi-space-token-at (p) | |
622 ;; If point p is surrounded by space characters, then return the token of the | |
623 ;; contiguous spaces. | |
624 (delphi-charset-token-at p delphi-space-chars 'space)) | |
625 | |
626 (defun delphi-word-token-at (p) | |
627 ;; If point p is over a word (i.e. identifier characters), then return a word | |
628 ;; token. If the word is actually a keyword, then return the keyword token. | |
629 (let ((word (delphi-charset-token-at p delphi-word-chars 'word))) | |
630 (when word | |
631 (let* ((word-image (downcase (delphi-token-string word))) | |
632 (keyword (intern-soft word-image))) | |
633 (when (and (or keyword (string= "nil" word-image)) | |
634 (delphi-is keyword delphi-keywords)) | |
635 (delphi-set-token-kind word keyword)) | |
636 word)))) | |
637 | |
638 (defun delphi-explicit-token-at (p token-string kind) | |
639 ;; If point p is anywhere in the token string then returns the resulting | |
640 ;; token. | |
641 (let ((token (delphi-charset-token-at p token-string kind))) | |
642 (when (and token (string= token-string (delphi-token-string token))) | |
643 token))) | |
644 | |
645 (defun delphi-token-at (p) | |
646 ;; Returns the token from parsing text at point p. | |
647 (when (and (<= (point-min) p) (<= p (point-max))) | |
648 (cond ((delphi-literal-token-at p)) | |
649 | |
650 ((delphi-space-token-at p)) | |
651 | |
652 ((delphi-word-token-at p)) | |
653 | |
654 ((delphi-char-token-at p ?\( 'open-group)) | |
655 ((delphi-char-token-at p ?\) 'close-group)) | |
656 ((delphi-char-token-at p ?\[ 'open-group)) | |
657 ((delphi-char-token-at p ?\] 'close-group)) | |
658 ((delphi-char-token-at p ?\n 'newline)) | |
659 ((delphi-char-token-at p ?\; 'semicolon)) | |
660 ((delphi-char-token-at p ?. 'dot)) | |
661 ((delphi-char-token-at p ?, 'comma)) | |
662 ((delphi-char-token-at p ?= 'equals)) | |
663 ((delphi-char-token-at p ?+ 'plus)) | |
664 ((delphi-char-token-at p ?- 'minus)) | |
665 ((delphi-char-token-at p ?* 'times)) | |
666 ((delphi-char-token-at p ?/ 'divides)) | |
667 ((delphi-char-token-at p ?: 'colon)) | |
668 | |
669 ((delphi-explicit-token-at p "<>" 'not-equals)) | |
670 | |
671 ((delphi-point-token-at p 'punctuation))))) | |
672 | |
673 (defun delphi-current-token () | |
674 ;; Returns the delphi source token under the current point. | |
675 (delphi-token-at (point))) | |
676 | |
677 (defun delphi-next-token (token) | |
678 ;; Returns the token after the specified token. | |
679 (when token | |
680 (let ((next (delphi-token-at (delphi-token-end token)))) | |
681 (if next | |
682 (delphi-step-progress (delphi-token-start next) "Scanning" | |
683 delphi-scanning-progress-step)) | |
684 next))) | |
685 | |
686 (defun delphi-previous-token (token) | |
687 ;; Returns the token before the specified token. | |
688 (when token | |
689 (let ((previous (delphi-token-at (1- (delphi-token-start token))))) | |
690 (if previous | |
691 (delphi-step-progress (delphi-token-start previous) "Scanning" | |
692 delphi-scanning-progress-step)) | |
693 previous))) | |
694 | |
695 (defun delphi-next-visible-token (token) | |
696 ;; Returns the first non-space token after the specified token. | |
697 (let (next-token) | |
698 (while (progn | |
699 (setq next-token (delphi-next-token token)) | |
700 (delphi-is (delphi-token-kind next-token) '(space newline)))) | |
701 next-token)) | |
702 | |
703 (defun delphi-parse-region (from to) | |
704 ;; Parses the literal tokens in the region. The point is set to "to". | |
705 (save-restriction | |
706 (widen) | |
707 (goto-char from) | |
708 (while (< (point) to) | |
709 (delphi-parse-next-literal to)))) | |
710 | |
711 (defun delphi-parse-region-until-stable (from to) | |
712 ;; Parses at least the literal tokens in the region. After that, parsing | |
713 ;; continues as long as obsolete literal regions are encountered. The point | |
714 ;; is set to the encountered stable point. | |
715 (save-restriction | |
716 (widen) | |
717 (delphi-parse-region from to) | |
718 (while (not (delphi-is-stable-literal (point))) | |
719 (delphi-parse-next-literal (point-max))))) | |
720 | |
721 (defun delphi-fontify-region (from to &optional verbose) | |
722 ;; Colors the text in the region according to Delphi rules. | |
723 (delphi-save-excursion | |
724 (delphi-save-state | |
725 (let ((p from) | |
726 (delphi-verbose verbose) | |
727 (token nil)) | |
728 (delphi-progress-start) | |
729 (while (< p to) | |
730 ;; Color the token and move past it. | |
731 (setq token (delphi-token-at p)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
732 (add-text-properties |
25072 | 733 (delphi-token-start token) (delphi-token-end token) |
734 (list 'face (delphi-face-of (delphi-token-kind token)) 'lazy-lock t)) | |
735 (setq p (delphi-token-end token)) | |
736 (delphi-step-progress p "Fontifying" delphi-fontifying-progress-step)) | |
737 (delphi-progress-done))))) | |
738 | |
51360
f48326c934af
(large-file-warning-threshold): Add type, groups.
John Paul Wallington <jpw@pobox.com>
parents:
49598
diff
changeset
|
739 (defvar delphi-ignore-changes t |
25072 | 740 "Internal flag to control if the delphi-mode responds to buffer changes. |
741 Defaults to t in case the delphi-after-change function is called on a | |
742 non-delphi buffer. Set to nil in a delphi buffer. To override, just do: | |
743 (let ((delphi-ignore-changes t)) ...)") | |
744 | |
745 (defun delphi-after-change (change-start change-end old-length) | |
746 ;; Called when the buffer has changed. Reparses the changed region. | |
747 (unless delphi-ignore-changes | |
748 (let ((delphi-ignore-changes t)) ; Prevent recursive calls. | |
749 (delphi-save-excursion | |
750 (delphi-progress-start) | |
751 ;; Reparse at least from the token previous to the change to the end of | |
752 ;; line after the change. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
753 (delphi-parse-region-until-stable |
25072 | 754 (delphi-token-start (delphi-token-at (1- change-start))) |
755 (progn (goto-char change-end) (end-of-line) (point))) | |
756 (delphi-progress-done))))) | |
757 | |
758 (defun delphi-group-start (from-token) | |
759 ;; Returns the token that denotes the start of the ()/[] group. | |
760 (let ((token (delphi-previous-token from-token)) | |
761 (token-kind nil)) | |
762 (catch 'done | |
763 (while token | |
764 (setq token-kind (delphi-token-kind token)) | |
765 (cond | |
766 ;; Skip over nested groups. | |
767 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) | |
768 ((eq 'open-group token-kind) (throw 'done token))) | |
769 (setq token (delphi-previous-token token))) | |
770 ;; Start not found. | |
771 nil))) | |
772 | |
773 (defun delphi-group-end (from-token) | |
774 ;; Returns the token that denotes the end of the ()/[] group. | |
775 (let ((token (delphi-next-token from-token)) | |
776 (token-kind nil)) | |
777 (catch 'done | |
778 (while token | |
779 (setq token-kind (delphi-token-kind token)) | |
780 (cond | |
781 ;; Skip over nested groups. | |
782 ((eq 'open-group token-kind) (setq token (delphi-group-end token))) | |
783 ((eq 'close-group token-kind) (throw 'done token))) | |
784 (setq token (delphi-next-token token))) | |
785 ;; end not found. | |
786 nil))) | |
787 | |
788 (defun delphi-indent-of (token &optional offset) | |
789 ;; Returns the start column of the token, plus any offset. | |
790 (let ((indent (+ (delphi-column-of (delphi-token-start token)) | |
791 (if offset offset 0)))) | |
792 (when delphi-debug | |
793 (delphi-debug-log | |
794 (concat "\n Indent of: %S %S" | |
795 "\n column: %d indent: %d offset: %d") | |
796 token (delphi-token-string token) | |
797 (delphi-column-of (delphi-token-start token)) | |
798 indent (if offset offset 0))) | |
799 indent)) | |
800 | |
801 (defun delphi-line-indent-of (from-token &optional offset &rest terminators) | |
802 ;; Returns the column of first non-space character on the token's line, plus | |
803 ;; any offset. We also stop if one of the terminators or an open ( or [ is | |
804 ;; encountered. | |
805 (let ((token (delphi-previous-token from-token)) | |
806 (last-token from-token) | |
807 (kind nil)) | |
808 (catch 'done | |
809 (while token | |
810 (setq kind (delphi-token-kind token)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
811 (cond |
25072 | 812 ;; Skip over ()/[] groups. |
813 ((eq 'close-group kind) (setq token (delphi-group-start token))) | |
814 | |
815 ;; Stop at the beginning of the line or an open group. | |
816 ((delphi-is kind '(newline open-group)) (throw 'done nil)) | |
817 | |
818 ;; Stop at one of the specified terminators. | |
819 ((delphi-is kind terminators) (throw 'done nil))) | |
820 (unless (delphi-is kind delphi-whitespace) (setq last-token token)) | |
821 (setq token (delphi-previous-token token)))) | |
822 (delphi-indent-of last-token offset))) | |
823 | |
824 (defun delphi-stmt-line-indent-of (from-token &optional offset) | |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
825 ;; Like `delphi-line-indent-of' except is also stops on a use clause, and |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
826 ;; colons that precede statements (i.e. case labels). |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
827 (let ((token (delphi-previous-token from-token)) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
828 (last-token from-token) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
829 (kind nil)) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
830 (catch 'done |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
831 (while token |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
832 (setq kind (delphi-token-kind token)) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
833 (cond |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
834 ((and (eq 'colon kind) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
835 (delphi-is (delphi-token-kind last-token) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
836 `(,@delphi-block-statements |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
837 ,@delphi-expr-statements))) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
838 ;; We hit a label followed by a statement. Indent to the statement. |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
839 (throw 'done nil)) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
840 |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
841 ;; Skip over ()/[] groups. |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
842 ((eq 'close-group kind) (setq token (delphi-group-start token))) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
843 |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
844 ((delphi-is kind `(newline open-group ,@delphi-use-clauses)) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
845 ;; Stop at the beginning of the line, an open group, or a use clause |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
846 (throw 'done nil))) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
847 (unless (delphi-is kind delphi-whitespace) (setq last-token token)) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
848 (setq token (delphi-previous-token token)))) |
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
849 (delphi-indent-of last-token offset))) |
25072 | 850 |
851 (defun delphi-open-group-indent (token last-token &optional offset) | |
852 ;; Returns the indent relative to an unmatched ( or [. | |
853 (when (eq 'open-group (delphi-token-kind token)) | |
854 (if last-token | |
855 (delphi-indent-of last-token offset) | |
856 ;; There is nothing following the ( or [. Indent from its line. | |
857 (delphi-stmt-line-indent-of token delphi-indent-level)))) | |
858 | |
859 (defun delphi-composite-type-start (token last-token) | |
860 ;; Returns true (actually the last-token) if the pair equals (= class) or (= | |
861 ;; record), and nil otherwise. | |
862 (if (and (eq 'equals (delphi-token-kind token)) | |
863 (delphi-is (delphi-token-kind last-token) delphi-composite-types)) | |
864 last-token)) | |
865 | |
866 (defun delphi-is-simple-class-type (at-token limit-token) | |
867 ;; True if at-token is the start of a simple class type. E.g. | |
868 ;; class of TClass; | |
869 ;; class (TBaseClass); | |
870 ;; class; | |
871 (when (delphi-is (delphi-token-kind at-token) delphi-class-types) | |
872 (catch 'done | |
873 ;; Scan until the semi colon. | |
874 (let ((token (delphi-next-token at-token)) | |
875 (token-kind nil) | |
876 (limit (delphi-token-start limit-token))) | |
877 (while (and token (<= (delphi-token-start token) limit)) | |
878 (setq token-kind (delphi-token-kind token)) | |
879 (cond | |
880 ;; A semicolon delimits the search. | |
881 ((eq 'semicolon token-kind) (throw 'done token)) | |
882 | |
883 ;; Skip over the inheritance list. | |
884 ((eq 'open-group token-kind) (setq token (delphi-group-end token))) | |
885 | |
886 ;; Only allow "of" and whitespace, and an identifier | |
887 ((delphi-is token-kind `(of word ,@delphi-whitespace))) | |
888 | |
889 ;; Otherwise we are not in a simple class declaration. | |
890 ((throw 'done nil))) | |
891 (setq token (delphi-next-token token))))))) | |
892 | |
893 (defun delphi-block-start (from-token &optional stop-on-class) | |
894 ;; Returns the token that denotes the start of the block. | |
895 (let ((token (delphi-previous-token from-token)) | |
896 (last-token nil) | |
897 (token-kind nil)) | |
898 (catch 'done | |
899 (while token | |
900 (setq token-kind (delphi-token-kind token)) | |
901 (cond | |
902 ;; Skip over nested blocks. | |
903 ((delphi-is token-kind delphi-end-block-statements) | |
904 (setq token (delphi-block-start token))) | |
905 | |
906 ;; Regular block start found. | |
907 ((delphi-is token-kind delphi-block-statements) (throw 'done token)) | |
908 | |
909 ;; A class/record start also begins a block. | |
910 ((delphi-composite-type-start token last-token) | |
911 (throw 'done (if stop-on-class last-token token))) | |
912 ) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
913 (unless (delphi-is token-kind delphi-whitespace) |
25072 | 914 (setq last-token token)) |
915 (setq token (delphi-previous-token token))) | |
916 ;; Start not found. | |
917 nil))) | |
918 | |
919 (defun delphi-else-start (from-else) | |
920 ;; Returns the token of the if or case statement. | |
921 (let ((token (delphi-previous-token from-else)) | |
922 (token-kind nil) | |
923 (semicolon-count 0) | |
924 (if-count 0)) | |
925 (catch 'done | |
926 (while token | |
927 (setq token-kind (delphi-token-kind token)) | |
928 (cond | |
929 ;; Skip over nested groups. | |
930 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) | |
931 | |
932 ;; Skip over any nested blocks. | |
933 ((delphi-is token-kind delphi-end-block-statements) | |
934 (setq token (delphi-block-start token))) | |
935 | |
936 ((eq 'semicolon token-kind) | |
937 ;; Semicolon means we are looking for an enclosing if, unless we | |
938 ;; are in a case statement. Keep counts of the semicolons and decide | |
939 ;; later. | |
940 (setq semicolon-count (1+ semicolon-count))) | |
941 | |
942 ((and (eq 'if token-kind) (= semicolon-count 0)) | |
943 ;; We only can match an if when there have been no intervening | |
944 ;; semicolons. | |
945 (throw 'done token)) | |
946 | |
947 ((eq 'case token-kind) | |
948 ;; We have hit a case statement start. | |
949 (throw 'done token))) | |
950 (setq token (delphi-previous-token token))) | |
951 ;; No if or case statement found. | |
952 nil))) | |
953 | |
954 (defun delphi-comment-content-start (comment) | |
955 ;; Returns the point of the first non-space character in the comment. | |
956 (let ((kind (delphi-token-kind comment))) | |
957 (when (delphi-is kind delphi-comments) | |
958 (delphi-save-excursion | |
959 (goto-char (+ (delphi-token-start comment) | |
960 (length (delphi-literal-start-pattern kind)))) | |
961 (skip-chars-forward delphi-space-chars) | |
962 (point))))) | |
963 | |
964 (defun delphi-comment-block-start (comment) | |
965 ;; Returns the starting comment token of a contiguous // comment block. If | |
966 ;; the comment is multiline (i.e. {...} or (*...*)), the original comment is | |
967 ;; returned. | |
968 (if (not (eq 'comment-single-line (delphi-token-kind comment))) | |
969 comment | |
970 ;; Scan until we run out of // comments. | |
971 (let ((prev-comment comment) | |
972 (start-comment comment) | |
973 (kind nil)) | |
974 (while (let ((kind (delphi-token-kind prev-comment))) | |
975 (cond ((eq kind 'space)) | |
976 ((eq kind 'comment-single-line) | |
977 (setq start-comment prev-comment)) | |
978 (t nil))) | |
979 (setq prev-comment (delphi-previous-token prev-comment))) | |
980 start-comment))) | |
981 | |
982 (defun delphi-comment-block-end (comment) | |
983 ;; Returns the end comment token of a contiguous // comment block. If the | |
984 ;; comment is multiline (i.e. {...} or (*...*)), the original comment is | |
985 ;; returned. | |
986 (if (not (eq 'comment-single-line (delphi-token-kind comment))) | |
987 comment | |
988 ;; Scan until we run out of // comments. | |
989 (let ((next-comment comment) | |
990 (end-comment comment) | |
991 (kind nil)) | |
992 (while (let ((kind (delphi-token-kind next-comment))) | |
993 (cond ((eq kind 'space)) | |
994 ((eq kind 'comment-single-line) | |
995 (setq end-comment next-comment)) | |
996 (t nil))) | |
997 (setq next-comment (delphi-next-token next-comment))) | |
998 end-comment))) | |
999 | |
1000 (defun delphi-on-first-comment-line (comment) | |
1001 ;; Returns true if the current point is on the first line of the comment. | |
1002 (save-excursion | |
1003 (let ((comment-start (delphi-token-start comment)) | |
1004 (current-point (point))) | |
1005 (goto-char comment-start) | |
1006 (end-of-line) | |
1007 (and (<= comment-start current-point) (<= current-point (point)))))) | |
1008 | |
1009 (defun delphi-comment-indent-of (comment) | |
1010 ;; Returns the correct indentation for the comment. | |
1011 (let ((start-comment (delphi-comment-block-start comment))) | |
1012 (if (and (eq start-comment comment) | |
1013 (delphi-on-first-comment-line comment)) | |
1014 ;; Indent as a statement. | |
1015 (delphi-enclosing-indent-of comment) | |
1016 (save-excursion | |
1017 (let ((kind (delphi-token-kind comment))) | |
1018 (beginning-of-line) | |
1019 (cond ((eq 'comment-single-line kind) | |
1020 ;; Indent to the first comment in the // block. | |
1021 (delphi-indent-of start-comment)) | |
1022 | |
1023 ((looking-at (concat delphi-leading-spaces-re | |
1024 (delphi-literal-stop-pattern kind))) | |
1025 ;; Indent multi-line comment terminators to the comment start. | |
1026 (delphi-indent-of comment)) | |
1027 | |
1028 ;; Indent according to the comment's content start. | |
1029 ((delphi-column-of (delphi-comment-content-start comment))))))) | |
1030 )) | |
1031 | |
1032 (defun delphi-is-use-clause-end (at-token last-token last-colon from-kind) | |
1033 ;; True if we are after the end of a uses type clause. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1034 (when (and last-token |
25072 | 1035 (not last-colon) |
1036 (eq 'comma (delphi-token-kind at-token)) | |
1037 (eq 'semicolon from-kind)) | |
1038 ;; Scan for the uses statement, just to be sure. | |
1039 (let ((token (delphi-previous-token at-token)) | |
1040 (token-kind nil)) | |
1041 (catch 'done | |
1042 (while token | |
1043 (setq token-kind (delphi-token-kind token)) | |
1044 (cond ((delphi-is token-kind delphi-use-clauses) | |
1045 (throw 'done t)) | |
1046 | |
1047 ;; Whitespace, identifiers, strings, "in" keyword, and commas | |
1048 ;; are allowed in use clauses. | |
1049 ((or (delphi-is token-kind '(word comma in newline)) | |
1050 (delphi-is token-kind delphi-whitespace) | |
1051 (delphi-is token-kind delphi-strings))) | |
1052 | |
1053 ;; Nothing else is. | |
1054 ((throw 'done nil))) | |
1055 (setq token (delphi-previous-token token))) | |
1056 nil)))) | |
1057 | |
1058 (defun delphi-is-block-after-expr-statement (token) | |
1059 ;; Returns true if we have a block token trailing an expression delimiter (of | |
1060 ;; presumably an expression statement). | |
1061 (when (delphi-is (delphi-token-kind token) delphi-block-statements) | |
1062 (let ((previous (delphi-previous-token token)) | |
1063 (previous-kind nil)) | |
1064 (while (progn | |
1065 (setq previous-kind (delphi-token-kind previous)) | |
1066 (eq previous-kind 'space)) | |
1067 (setq previous (delphi-previous-token previous))) | |
1068 (or (delphi-is previous-kind delphi-expr-delimiters) | |
1069 (eq previous-kind 'else))))) | |
1070 | |
1071 (defun delphi-previous-indent-of (from-token) | |
1072 ;; Returns the indentation of the previous statement of the token. | |
1073 (let ((token (delphi-previous-token from-token)) | |
1074 (token-kind nil) | |
1075 (from-kind (delphi-token-kind from-token)) | |
1076 (last-colon nil) | |
1077 (last-token nil)) | |
1078 (catch 'done | |
1079 (while token | |
1080 (setq token-kind (delphi-token-kind token)) | |
1081 (cond | |
1082 ;; An open ( or [ always is an indent point. | |
1083 ((eq 'open-group token-kind) | |
1084 (throw 'done (delphi-open-group-indent token last-token))) | |
1085 | |
1086 ;; Skip over any ()/[] groups. | |
1087 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) | |
1088 | |
1089 ((delphi-is token-kind delphi-end-block-statements) | |
1090 (if (eq 'newline (delphi-token-kind (delphi-previous-token token))) | |
1091 ;; We can stop at an end token that is right up against the | |
1092 ;; margin. | |
1093 (throw 'done 0) | |
1094 ;; Otherwise, skip over any nested blocks. | |
1095 (setq token (delphi-block-start token)))) | |
1096 | |
1097 ;; Special case: if we encounter a ", word;" then we assume that we | |
1098 ;; are in some kind of uses clause, and thus indent to column 0. This | |
1099 ;; works because no other constructs are known to have that form. | |
1100 ;; This fixes the irritating case of having indents after a uses | |
1101 ;; clause look like: | |
1102 ;; uses | |
1103 ;; someUnit, | |
1104 ;; someOtherUnit; | |
1105 ;; // this should be at column 0! | |
1106 ((delphi-is-use-clause-end token last-token last-colon from-kind) | |
1107 (throw 'done 0)) | |
1108 | |
1109 ;; A previous terminator means we can stop. If we are on a directive, | |
1110 ;; however, then we are not actually encountering a new statement. | |
1111 ((and last-token | |
1112 (delphi-is token-kind delphi-previous-terminators) | |
1113 (not (delphi-is (delphi-token-kind last-token) | |
1114 delphi-directives))) | |
1115 (throw 'done (delphi-stmt-line-indent-of last-token 0))) | |
1116 | |
1117 ;; Ignore whitespace. | |
1118 ((delphi-is token-kind delphi-whitespace)) | |
1119 | |
1120 ;; Remember any ':' we encounter, since that affects how we indent to | |
1121 ;; a case statement. | |
1122 ((eq 'colon token-kind) (setq last-colon token)) | |
1123 | |
1124 ;; A case statement delimits a previous statement. We indent labels | |
1125 ;; specially. | |
1126 ((eq 'case token-kind) | |
1127 (throw 'done | |
1128 (if last-colon (delphi-line-indent-of last-colon) | |
1129 (delphi-line-indent-of token delphi-case-label-indent)))) | |
1130 | |
1131 ;; If we are in a use clause then commas mark an enclosing rather than | |
1132 ;; a previous statement. | |
1133 ((delphi-is token-kind delphi-use-clauses) | |
1134 (throw 'done | |
1135 (if (eq 'comma from-kind) | |
1136 (if last-token | |
1137 ;; Indent to first unit in use clause. | |
1138 (delphi-indent-of last-token) | |
1139 ;; Indent from use clause keyword. | |
1140 (delphi-line-indent-of token delphi-indent-level)) | |
1141 ;; Indent to use clause keyword. | |
1142 (delphi-line-indent-of token)))) | |
1143 | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1144 ;; Assembly sections always indent in from the asm keyword. |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1145 ((eq token-kind 'asm) |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1146 (throw 'done (delphi-stmt-line-indent-of token delphi-indent-level))) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1147 |
25072 | 1148 ;; An enclosing statement delimits a previous statement. |
1149 ;; We try to use the existing indent of the previous statement, | |
1150 ;; otherwise we calculate from the enclosing statement. | |
1151 ((delphi-is token-kind delphi-previous-enclosing-statements) | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1152 (throw 'done (if last-token |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1153 ;; Otherwise indent to the last token |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1154 (delphi-line-indent-of last-token) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1155 ;; Just indent from the enclosing keyword |
25072 | 1156 (delphi-line-indent-of token delphi-indent-level)))) |
1157 | |
1158 ;; A class or record declaration also delimits a previous statement. | |
1159 ((delphi-composite-type-start token last-token) | |
1160 (throw | |
1161 'done | |
1162 (if (delphi-is-simple-class-type last-token from-token) | |
1163 ;; c = class; or c = class of T; are previous statements. | |
1164 (delphi-line-indent-of token) | |
1165 ;; Otherwise c = class ... or r = record ... are enclosing | |
1166 ;; statements. | |
1167 (delphi-line-indent-of last-token delphi-indent-level)))) | |
1168 | |
1169 ;; We have a definite previous statement delimiter. | |
1170 ((delphi-is token-kind delphi-previous-statements) | |
1171 (throw 'done (delphi-stmt-line-indent-of token 0))) | |
1172 ) | |
1173 (unless (delphi-is token-kind delphi-whitespace) | |
1174 (setq last-token token)) | |
1175 (setq token (delphi-previous-token token))) | |
1176 ;; We ran out of tokens. Indent to column 0. | |
1177 0))) | |
1178 | |
1179 (defun delphi-section-indent-of (section-token) | |
1180 ;; Returns the indentation appropriate for begin/var/const/type/label | |
1181 ;; tokens. | |
1182 (let* ((token (delphi-previous-token section-token)) | |
1183 (token-kind nil) | |
1184 (last-token nil) | |
1185 (nested-block-count 0) | |
1186 (expr-delimited nil) | |
1187 (last-terminator nil)) | |
1188 (catch 'done | |
1189 (while token | |
1190 (setq token-kind (delphi-token-kind token)) | |
1191 (cond | |
1192 ;; Always stop at unmatched ( or [. | |
1193 ((eq token-kind 'open-group) | |
1194 (throw 'done (delphi-open-group-indent token last-token))) | |
1195 | |
1196 ;; Skip over any ()/[] groups. | |
1197 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) | |
1198 | |
1199 ((delphi-is token-kind delphi-end-block-statements) | |
1200 (if (eq 'newline (delphi-token-kind (delphi-previous-token token))) | |
1201 ;; We can stop at an end token that is right up against the | |
1202 ;; margin. | |
1203 (throw 'done 0) | |
1204 ;; Otherwise, skip over any nested blocks. | |
1205 (setq token (delphi-block-start token) | |
1206 nested-block-count (1+ nested-block-count)))) | |
1207 | |
1208 ;; Remember if we have encountered any forward routine declarations. | |
1209 ((eq 'forward token-kind) | |
1210 (setq nested-block-count (1+ nested-block-count))) | |
1211 | |
1212 ;; Mark the completion of a nested routine traversal. | |
1213 ((and (delphi-is token-kind delphi-routine-statements) | |
1214 (> nested-block-count 0)) | |
1215 (setq nested-block-count (1- nested-block-count))) | |
1216 | |
1217 ;; Remember if we have encountered any statement terminators. | |
1218 ((eq 'semicolon token-kind) (setq last-terminator token)) | |
1219 | |
1220 ;; Remember if we have encountered any expression delimiters. | |
1221 ((delphi-is token-kind delphi-expr-delimiters) | |
1222 (setq expr-delimited token)) | |
1223 | |
1224 ;; Enclosing body statements are delimiting. We indent the compound | |
1225 ;; bodies specially. | |
1226 ((and (not last-terminator) | |
1227 (delphi-is token-kind delphi-body-statements)) | |
1228 (throw 'done | |
1229 (delphi-stmt-line-indent-of token delphi-compound-block-indent))) | |
1230 | |
1231 ;; An enclosing ":" means a label. | |
1232 ((and (eq 'colon token-kind) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1233 (delphi-is (delphi-token-kind section-token) |
25072 | 1234 delphi-block-statements) |
1235 (not last-terminator) | |
1236 (not expr-delimited) | |
1237 (not (eq 'equals (delphi-token-kind last-token)))) | |
1238 (throw 'done | |
1239 (delphi-stmt-line-indent-of token delphi-indent-level))) | |
1240 | |
1241 ;; Block and mid block tokens are always enclosing | |
1242 ((delphi-is token-kind delphi-begin-enclosing-tokens) | |
1243 (throw 'done | |
25161
ae69eef1b98b
checked in with -k by blaak at 1999/08/03 06:17:58
Ray Blaak <blaak@infomatch.com>
parents:
25072
diff
changeset
|
1244 (delphi-stmt-line-indent-of token delphi-indent-level))) |
25072 | 1245 |
1246 ;; Declaration sections and routines are delimiters, unless they | |
1247 ;; are part of a nested routine. | |
1248 ((and (delphi-is token-kind delphi-decl-delimiters) | |
1249 (= 0 nested-block-count)) | |
1250 (throw 'done (delphi-line-indent-of token 0))) | |
1251 | |
1252 ;; Unit statements mean we indent right to the left. | |
1253 ((delphi-is token-kind delphi-unit-statements) (throw 'done 0)) | |
1254 ) | |
1255 (unless (delphi-is token-kind delphi-whitespace) | |
1256 (setq last-token token)) | |
1257 (setq token (delphi-previous-token token))) | |
1258 ;; We ran out of tokens. Indent to column 0. | |
1259 0))) | |
1260 | |
1261 (defun delphi-enclosing-indent-of (from-token) | |
1262 ;; Returns the indentation offset from the enclosing statement of the token. | |
1263 (let ((token (delphi-previous-token from-token)) | |
1264 (from-kind (delphi-token-kind from-token)) | |
1265 (token-kind nil) | |
1266 (stmt-start nil) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1267 (last-token nil) |
25072 | 1268 (equals-encountered nil) |
1269 (before-equals nil) | |
1270 (expr-delimited nil)) | |
1271 (catch 'done | |
1272 (while token | |
1273 (setq token-kind (delphi-token-kind token)) | |
1274 (cond | |
1275 ;; An open ( or [ always is an indent point. | |
1276 ((eq 'open-group token-kind) | |
1277 (throw 'done | |
1278 (delphi-open-group-indent | |
1279 token last-token | |
1280 (if (delphi-is from-kind delphi-binary-ops) | |
1281 ;; Keep binary operations aligned with the open group. | |
1282 0 | |
1283 delphi-indent-level)))) | |
1284 | |
1285 ;; Skip over any ()/[] groups. | |
1286 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) | |
1287 | |
1288 ;; Skip over any nested blocks. | |
1289 ((delphi-is token-kind delphi-end-block-statements) | |
1290 (setq token (delphi-block-start token))) | |
1291 | |
1292 ;; An expression delimiter affects indentation depending on whether | |
1293 ;; the point is before or after it. Remember that we encountered one. | |
1294 ;; Also remember the last encountered token, since if it exists it | |
1295 ;; should be the actual indent point. | |
1296 ((delphi-is token-kind delphi-expr-delimiters) | |
1297 (setq expr-delimited token stmt-start last-token)) | |
1298 | |
1299 ;; With a non-delimited expression statement we indent after the | |
1300 ;; statement's keyword, unless we are on the delimiter itself. | |
1301 ((and (not expr-delimited) | |
1302 (delphi-is token-kind delphi-expr-statements)) | |
1303 (throw 'done | |
1304 (cond ((delphi-is from-kind delphi-expr-delimiters) | |
1305 ;; We are indenting a delimiter. Indent to the statement. | |
1306 (delphi-stmt-line-indent-of token 0)) | |
1307 | |
1308 ((and last-token (delphi-is from-kind delphi-binary-ops)) | |
1309 ;; Align binary ops with the expression. | |
1310 (delphi-indent-of last-token)) | |
1311 | |
1312 (last-token | |
1313 ;; Indent in from the expression. | |
1314 (delphi-indent-of last-token delphi-indent-level)) | |
1315 | |
1316 ;; Indent in from the statement's keyword. | |
1317 ((delphi-indent-of token delphi-indent-level))))) | |
1318 | |
1319 ;; A delimited case statement indents the label according to | |
1320 ;; a special rule. | |
1321 ((eq 'case token-kind) | |
1322 (throw 'done | |
1323 (if stmt-start | |
1324 ;; We are not actually indenting to the case statement, | |
1325 ;; but are within a label expression. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1326 (delphi-stmt-line-indent-of |
25072 | 1327 stmt-start delphi-indent-level) |
1328 ;; Indent from the case keyword. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1329 (delphi-stmt-line-indent-of |
25072 | 1330 token delphi-case-label-indent)))) |
1331 | |
1332 ;; Body expression statements are enclosing. Indent from the | |
1333 ;; statement's keyword, unless we have a non-block statement following | |
1334 ;; it. | |
1335 ((delphi-is token-kind delphi-body-expr-statements) | |
1336 (throw 'done | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1337 (delphi-stmt-line-indent-of |
25072 | 1338 (or stmt-start token) delphi-indent-level))) |
1339 | |
1340 ;; An else statement is enclosing, but it doesn't have an expression. | |
1341 ;; Thus we take into account last-token instead of stmt-start. | |
1342 ((eq 'else token-kind) | |
1343 (throw 'done (delphi-stmt-line-indent-of | |
1344 (or last-token token) delphi-indent-level))) | |
1345 | |
1346 ;; We indent relative to an enclosing declaration section. | |
1347 ((delphi-is token-kind delphi-decl-sections) | |
1348 (throw 'done (delphi-indent-of (if last-token last-token token) | |
1349 delphi-indent-level))) | |
1350 | |
1351 ;; In unit sections we indent right to the left. | |
1352 ((delphi-is token-kind delphi-unit-sections) (throw 'done 0)) | |
1353 | |
1354 ;; A previous terminator means we can stop. | |
1355 ((delphi-is token-kind delphi-previous-terminators) | |
1356 (throw 'done | |
1357 (cond ((and last-token | |
1358 (eq 'comma token-kind) | |
1359 (delphi-is from-kind delphi-binary-ops)) | |
1360 ;; Align binary ops with the expression. | |
1361 (delphi-indent-of last-token)) | |
1362 | |
1363 (last-token | |
1364 ;; Indent in from the expression. | |
1365 (delphi-indent-of last-token delphi-indent-level)) | |
1366 | |
1367 ;; No enclosing expression; use the previous statment's | |
1368 ;; indent. | |
1369 ((delphi-previous-indent-of token))))) | |
1370 | |
1371 ;; A block statement after an expression delimiter has its start | |
1372 ;; column as the expression statement. E.g. | |
1373 ;; if (a = b) | |
1374 ;; and (a != c) then begin | |
1375 ;; //... | |
1376 ;; end; | |
1377 ;; Remember it for when we encounter the expression statement start. | |
1378 ((delphi-is-block-after-expr-statement token) | |
1379 (throw 'done | |
1380 (cond (last-token (delphi-indent-of last-token delphi-indent-level)) | |
1381 | |
1382 ((+ (delphi-section-indent-of token) delphi-indent-level))))) | |
1383 | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1384 ;; Assembly sections always indent in from the asm keyword. |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1385 ((eq token-kind 'asm) |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1386 (throw 'done (delphi-stmt-line-indent-of token delphi-indent-level))) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1387 |
25072 | 1388 ;; Stop at an enclosing statement and indent from it. |
1389 ((delphi-is token-kind delphi-enclosing-statements) | |
1390 (throw 'done (delphi-stmt-line-indent-of | |
1391 (or last-token token) delphi-indent-level))) | |
1392 | |
1393 ;; A class/record declaration is also enclosing. | |
1394 ((delphi-composite-type-start token last-token) | |
1395 (throw 'done | |
1396 (delphi-line-indent-of last-token delphi-indent-level))) | |
1397 | |
1398 ;; A ":" we indent relative to its line beginning. If we are in a | |
1399 ;; parameter list, then stop also if we hit a ";". | |
1400 ((and (eq token-kind 'colon) | |
1401 (not expr-delimited) | |
1402 (not (delphi-is from-kind delphi-expr-delimiters)) | |
1403 (not equals-encountered) | |
1404 (not (eq from-kind 'equals))) | |
1405 (throw 'done | |
1406 (if last-token | |
1407 (delphi-indent-of last-token delphi-indent-level) | |
1408 (delphi-line-indent-of token delphi-indent-level 'semicolon)))) | |
1409 | |
1410 ;; If the ":" was not processed above and we have token after the "=", | |
1411 ;; then indent from the "=". Ignore :=, however. | |
1412 ((and (eq token-kind 'colon) equals-encountered before-equals) | |
1413 (cond | |
1414 ;; Ignore binary ops for now. It would do, for example: | |
1415 ;; val := 1 + 2 | |
1416 ;; + 3; | |
1417 ;; which is good, but also | |
1418 ;; val := Foo | |
1419 ;; (foo, args) | |
1420 ;; + 2; | |
1421 ;; which doesn't look right. | |
1422 ;;;; Align binary ops with the before token. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1423 ;;((delphi-is from-kind delphi-binary-ops) |
25072 | 1424 ;;(throw 'done (delphi-indent-of before-equals 0))) |
1425 | |
1426 ;; Assignments (:=) we skip over to get a normal indent. | |
1427 ((eq (delphi-token-kind last-token) 'equals)) | |
1428 | |
1429 ;; Otherwise indent in from the equals. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1430 ((throw 'done |
25072 | 1431 (delphi-indent-of before-equals delphi-indent-level))))) |
1432 | |
1433 ;; Remember any "=" we encounter if it has not already been processed. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1434 ((eq token-kind 'equals) |
25072 | 1435 (setq equals-encountered token |
1436 before-equals last-token)) | |
1437 ) | |
1438 (unless (delphi-is token-kind delphi-whitespace) | |
1439 (setq last-token token)) | |
1440 (setq token (delphi-previous-token token))) | |
1441 ;; We ran out of tokens. Indent to column 0. | |
1442 0))) | |
1443 | |
1444 (defun delphi-corrected-indentation () | |
1445 ;; Returns the corrected indentation for the current line. | |
1446 (delphi-save-excursion | |
1447 (delphi-progress-start) | |
1448 ;; Move to the first token on the line. | |
1449 (beginning-of-line) | |
1450 (skip-chars-forward delphi-space-chars) | |
1451 (let* ((token (delphi-current-token)) | |
1452 (token-kind (delphi-token-kind token)) | |
1453 (indent | |
1454 (cond ((eq 'close-group token-kind) | |
1455 ;; Indent to the matching start ( or [. | |
1456 (delphi-indent-of (delphi-group-start token))) | |
1457 | |
1458 ((delphi-is token-kind delphi-unit-statements) 0) | |
1459 | |
1460 ((delphi-is token-kind delphi-comments) | |
1461 ;; In a comment. | |
1462 (delphi-comment-indent-of token)) | |
1463 | |
1464 ((delphi-is token-kind delphi-decl-matchers) | |
1465 ;; Use a previous section/routine's indent. | |
1466 (delphi-section-indent-of token)) | |
1467 | |
1468 ((delphi-is token-kind delphi-match-block-statements) | |
1469 ;; Use the block's indentation. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41810
diff
changeset
|
1470 (let ((block-start |
25072 | 1471 (delphi-block-start token 'stop-on-class))) |
1472 (cond | |
1473 ;; When trailing a body statement, indent to | |
1474 ;; the statement's keyword. | |
1475 ((delphi-is-block-after-expr-statement block-start) | |
1476 (delphi-section-indent-of block-start)) | |
1477 | |
1478 ;; Otherwise just indent to the block start. | |
1479 ((delphi-stmt-line-indent-of block-start 0))))) | |
1480 | |
1481 ((eq 'else token-kind) | |
1482 ;; Find the start of the if or case statement. | |
1483 (delphi-stmt-line-indent-of (delphi-else-start token) 0)) | |
1484 | |
1485 ;; Otherwise indent in from enclosing statement. | |
1486 ((delphi-enclosing-indent-of | |
1487 (if token token (delphi-token-at (1- (point))))))))) | |
1488 (delphi-progress-done) | |
1489 indent))) | |
1490 | |
1491 (defun delphi-indent-line () | |
1492 "Indent the current line according to the current language construct. If | |
1493 before the indent, the point is moved to the indent." | |
1494 (interactive) | |
1495 (delphi-save-match-data | |
1496 (let ((marked-point (point-marker)) ; Maintain our position reliably. | |
1497 (new-point nil) | |
1498 (line-start nil) | |
1499 (old-indent 0) | |
1500 (new-indent 0)) | |
1501 (beginning-of-line) | |
1502 (setq line-start (point)) | |
1503 (skip-chars-forward delphi-space-chars) | |
1504 (setq old-indent (current-column)) | |
1505 (setq new-indent (delphi-corrected-indentation)) | |
1506 (if (< marked-point (point)) | |
1507 ;; If before the indent column, then move to it. | |
1508 (set-marker marked-point (point))) | |
1509 ;; Advance our marked point after inserted spaces. | |
1510 (set-marker-insertion-type marked-point t) | |
1511 (when (/= old-indent new-indent) | |
1512 (delete-region line-start (point)) | |
1513 (insert (make-string new-indent ?\ ))) | |
1514 (goto-char marked-point) | |
1515 (set-marker marked-point nil)))) | |
1516 | |
1517 (defvar delphi-mode-abbrev-table nil | |
1518 "Abbrev table in use in delphi-mode buffers.") | |
1519 (define-abbrev-table 'delphi-mode-abbrev-table ()) | |
1520 | |
1521 (defmacro delphi-ensure-buffer (buffer-var buffer-name) | |
1522 ;; Ensures there exists a buffer of the specified name in the specified | |
1523 ;; variable. | |
1524 `(when (not (buffer-live-p ,buffer-var)) | |
1525 (setq ,buffer-var (get-buffer-create ,buffer-name)))) | |
1526 | |
1527 (defun delphi-log-msg (to-buffer the-msg) | |
1528 ;; Writes a message to the end of the specified buffer. | |
1529 (with-current-buffer to-buffer | |
1530 (save-selected-window | |
1531 (switch-to-buffer-other-window to-buffer) | |
1532 (goto-char (point-max)) | |
1533 (set-window-dot (get-buffer-window to-buffer) (point)) | |
1534 (insert the-msg)))) | |
1535 | |
1536 ;; Debugging helpers: | |
1537 | |
1538 (defvar delphi-debug-buffer nil | |
1539 "Buffer to write delphi-mode debug messages to. Created on demand.") | |
1540 | |
1541 (defun delphi-debug-log (format-string &rest args) | |
1542 ;; Writes a message to the log buffer. | |
1543 (when delphi-debug | |
1544 (delphi-ensure-buffer delphi-debug-buffer "*Delphi Debug Log*") | |
1545 (delphi-log-msg delphi-debug-buffer | |
1546 (concat (format-time-string "%H:%M:%S " (current-time)) | |
1547 (apply #'format (cons format-string args)) | |
1548 "\n")))) | |
1549 | |
1550 (defun delphi-debug-token-string (token) | |
1551 (let* ((image (delphi-token-string token)) | |
1552 (has-newline (string-match "^\\([^\n]*\\)\n\\(.+\\)?$" image))) | |
1553 (when has-newline | |
1554 (setq image (concat (match-string 1 image) | |
1555 (if (match-beginning 2) "...")))) | |
1556 image)) | |
1557 | |
1558 (defun delphi-debug-show-current-token () | |
1559 (interactive) | |
1560 (let ((token (delphi-current-token))) | |
1561 (delphi-debug-log "Token: %S %S" token (delphi-debug-token-string token)))) | |
1562 | |
1563 (defun delphi-debug-goto-point (p) | |
1564 (interactive "NGoto char: ") | |
1565 (goto-char p)) | |
1566 | |
1567 (defun delphi-debug-goto-next-token () | |
1568 (interactive) | |
1569 (goto-char (delphi-token-start (delphi-next-token (delphi-current-token))))) | |
1570 | |
1571 (defun delphi-debug-goto-previous-token () | |
1572 (interactive) | |
1573 (goto-char | |
1574 (delphi-token-start (delphi-previous-token (delphi-current-token))))) | |
1575 | |
1576 (defun delphi-debug-show-current-string (from to) | |
1577 (interactive "r") | |
1578 (delphi-debug-log "String: %S" (buffer-substring from to))) | |
1579 | |
1580 (defun delphi-debug-show-is-stable () | |
1581 (interactive) | |
1582 (delphi-debug-log "stable: %S prev: %S next: %S" | |
1583 (delphi-is-stable-literal (point)) | |
1584 (delphi-literal-kind (1- (point))) | |
1585 (delphi-literal-kind (point)))) | |
1586 | |
1587 (defun delphi-debug-unparse-buffer () | |
1588 (interactive) | |
1589 (delphi-set-text-properties (point-min) (point-max) nil)) | |
1590 | |
1591 (defun delphi-debug-parse-region (from to) | |
1592 (interactive "r") | |
1593 (let ((delphi-verbose t)) | |
1594 (delphi-save-excursion | |
1595 (delphi-progress-start) | |
1596 (delphi-parse-region from to) | |
1597 (delphi-progress-done "Parsing done")))) | |
1598 | |
1599 (defun delphi-debug-parse-window () | |
1600 (interactive) | |
1601 (delphi-debug-parse-region (window-start) (window-end))) | |
1602 | |
1603 (defun delphi-debug-parse-buffer () | |
1604 (interactive) | |
1605 (delphi-debug-parse-region (point-min) (point-max))) | |
1606 | |
1607 (defun delphi-debug-fontify-window () | |
1608 (interactive) | |
1609 (delphi-fontify-region (window-start) (window-end) t)) | |
1610 | |
1611 (defun delphi-debug-fontify-buffer () | |
1612 (interactive) | |
1613 (delphi-fontify-region (point-min) (point-max) t)) | |
1614 | |
1615 (defun delphi-debug-tokenize-region (from to) | |
1616 (interactive) | |
1617 (delphi-save-excursion | |
1618 (delphi-progress-start) | |
1619 (goto-char from) | |
1620 (while (< (point) to) | |
1621 (goto-char (delphi-token-end (delphi-current-token))) | |
1622 (delphi-step-progress (point) "Tokenizing" delphi-scanning-progress-step)) | |
1623 (delphi-progress-done "Tokenizing done"))) | |
1624 | |
1625 (defun delphi-debug-tokenize-buffer () | |
1626 (interactive) | |
1627 (delphi-debug-tokenize-region (point-min) (point-max))) | |
1628 | |
1629 (defun delphi-debug-tokenize-window () | |
1630 (interactive) | |
1631 (delphi-debug-tokenize-region (window-start) (window-end))) | |
1632 | |
1633 (defun delphi-newline () | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1634 "Terminate the current line with a newline and indent the next, unless |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1635 `delphi-newline-always-indents' is nil, in which case no reindenting occurs." |
25072 | 1636 (interactive) |
1637 ;; Remove trailing spaces | |
1638 (delete-horizontal-space) | |
1639 (newline) | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1640 (when delphi-newline-always-indents |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1641 ;; Indent both the (now) previous and current line first. |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1642 (save-excursion |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1643 (previous-line 1) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1644 (delphi-indent-line)) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1645 (delphi-indent-line))) |
25072 | 1646 |
1647 | |
1648 (defun delphi-tab () | |
1649 "Indent the current line or insert a TAB, depending on the value of | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1650 `delphi-tab-always-indents' and the current line position." |
25072 | 1651 (interactive) |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1652 (if (or delphi-tab-always-indents ; We are always indenting |
25072 | 1653 ;; Or we are before the first non-space character on the line. |
1654 (save-excursion (skip-chars-backward delphi-space-chars) (bolp))) | |
1655 (delphi-indent-line) | |
1656 (insert "\t"))) | |
1657 | |
1658 | |
1659 (defun delphi-is-directory (path) | |
1660 ;; True if the specified path is an existing directory. | |
1661 (let ((attributes (file-attributes path))) | |
1662 (and attributes (car attributes)))) | |
1663 | |
1664 (defun delphi-is-file (path) | |
1665 ;; True if the specified file exists as a file. | |
1666 (let ((attributes (file-attributes path))) | |
1667 (and attributes (null (car attributes))))) | |
1668 | |
1669 (defun delphi-search-directory (unit dir &optional recurse) | |
1670 ;; Searches for the unit in the specified directory. If recurse is true, then | |
1671 ;; the directory is recursively searched. File name comparison is done in a | |
1672 ;; case insensitive manner. | |
1673 (when (delphi-is-directory dir) | |
1674 (let ((files (directory-files dir)) | |
1675 (unit-file (downcase unit))) | |
1676 (catch 'done | |
1677 ;; Search for the file. | |
1678 (mapcar #'(lambda (file) | |
1679 (let ((path (concat dir "/" file))) | |
1680 (if (and (string= unit-file (downcase file)) | |
1681 (delphi-is-file path)) | |
1682 (throw 'done path)))) | |
1683 files) | |
1684 | |
1685 ;; Not found. Search subdirectories. | |
1686 (when recurse | |
1687 (mapcar #'(lambda (subdir) | |
1688 (unless (member subdir '("." "..")) | |
1689 (let ((path (delphi-search-directory | |
1690 unit (concat dir "/" subdir) recurse))) | |
1691 (if path (throw 'done path))))) | |
1692 files)) | |
1693 | |
1694 ;; Not found. | |
1695 nil)))) | |
1696 | |
1697 | |
1698 (defun delphi-find-unit-in-directory (unit dir) | |
1699 ;; Searches for the unit in the specified directory. If the directory ends | |
1700 ;; in \"...\", then it is recursively searched. | |
1701 (let ((dir-name dir) | |
1702 (recurse nil)) | |
1703 ;; Check if we need to recursively search the directory. | |
1704 (if (string-match "^\\(.+\\)\\.\\.\\.$" dir-name) | |
1705 (setq dir-name (match-string 1 dir-name) | |
1706 recurse t)) | |
1707 ;; Ensure the trailing slash is removed. | |
1708 (if (string-match "^\\(.+\\)[\\\\/]$" dir-name) | |
1709 (setq dir-name (match-string 1 dir-name))) | |
1710 (delphi-search-directory unit dir-name recurse))) | |
1711 | |
1712 (defun delphi-find-unit-file (unit) | |
1713 ;; Finds the specified delphi source file according to `delphi-search-path'. | |
1714 ;; If found, the full path is returned, otherwise nil is returned. | |
1715 (catch 'done | |
1716 (cond ((null delphi-search-path) | |
1717 (delphi-find-unit-in-directory unit ".")) | |
1718 | |
1719 ((stringp delphi-search-path) | |
1720 (delphi-find-unit-in-directory unit delphi-search-path)) | |
1721 | |
1722 ((mapcar | |
1723 #'(lambda (dir) | |
1724 (let ((file (delphi-find-unit-in-directory unit dir))) | |
1725 (if file (throw 'done file)))) | |
1726 delphi-search-path))) | |
1727 nil)) | |
1728 | |
1729 (defun delphi-find-unit (unit) | |
1730 "Finds the specified delphi source file according to `delphi-search-path'. | |
1731 If no extension is specified, .pas is assumed. Creates a buffer for the unit." | |
1732 (interactive "sDelphi unit name: ") | |
1733 (let* ((unit-file (if (string-match "^\\(.*\\)\\.[a-z]+$" unit) | |
1734 unit | |
1735 (concat unit ".pas"))) | |
1736 (file (delphi-find-unit-file unit-file))) | |
1737 (if (null file) | |
1738 (error "unit not found: %s" unit-file) | |
1739 (find-file file) | |
1740 (if (not (eq major-mode 'delphi-mode)) | |
1741 (delphi-mode))) | |
1742 file)) | |
1743 | |
1744 (defun delphi-find-current-def () | |
1745 "Find the definition of the identifier under the current point." | |
1746 (interactive) | |
1747 (error "delphi-find-current-def: not implemented yet")) | |
1748 | |
1749 (defun delphi-find-current-xdef () | |
1750 "Find the definition of the identifier under the current point, searching | |
1751 in external units if necessary (as listed in the current unit's use clause). | |
1752 The set of directories to search for a unit is specified by the global variable | |
1753 delphi-search-path." | |
1754 (interactive) | |
1755 (error "delphi-find-current-xdef: not implemented yet")) | |
1756 | |
1757 (defun delphi-find-current-body () | |
1758 "Find the body of the identifier under the current point, assuming | |
1759 it is a routine." | |
1760 (interactive) | |
1761 (error "delphi-find-current-body: not implemented yet")) | |
1762 | |
1763 (defun delphi-fill-comment () | |
1764 "Fills the text of the current comment, according to `fill-column'. | |
1765 An error is raised if not in a comment." | |
1766 (interactive) | |
1767 (save-excursion | |
1768 (let* ((comment (delphi-current-token)) | |
1769 (comment-kind (delphi-token-kind comment))) | |
1770 (if (not (delphi-is comment-kind delphi-comments)) | |
1771 (error "Not in a comment") | |
1772 (let* ((start-comment (delphi-comment-block-start comment)) | |
1773 (end-comment (delphi-comment-block-end comment)) | |
1774 (comment-start (delphi-token-start start-comment)) | |
1775 (comment-end (delphi-token-end end-comment)) | |
1776 (content-start (delphi-comment-content-start start-comment)) | |
1777 (content-indent (delphi-column-of content-start)) | |
1778 (content-prefix (make-string content-indent ?\ )) | |
1779 (content-prefix-re delphi-leading-spaces-re) | |
1780 (p nil) | |
1781 (marked-point (point-marker))) ; Maintain our position reliably. | |
1782 (when (eq 'comment-single-line comment-kind) | |
1783 ;; // style comments need more work. | |
1784 (setq content-prefix | |
1785 (let ((comment-indent (delphi-column-of comment-start))) | |
1786 (concat (make-string comment-indent ?\ ) "//" | |
1787 (make-string (- content-indent comment-indent 2) | |
1788 ?\ ))) | |
1789 content-prefix-re (concat delphi-leading-spaces-re | |
1790 "//" | |
1791 delphi-spaces-re) | |
1792 comment-end (if (delphi-is-literal-end comment-end) | |
1793 ;; Don't include the trailing newline. | |
1794 (1- comment-end) | |
1795 comment-end))) | |
1796 | |
1797 ;; Advance our marked point after inserted spaces. | |
1798 (set-marker-insertion-type marked-point t) | |
1799 | |
1800 ;; Ensure we can modify the buffer | |
1801 (goto-char content-start) | |
1802 (insert " ") | |
1803 (delete-char -1) | |
1804 | |
1805 (narrow-to-region content-start comment-end) | |
1806 | |
1807 ;; Strip off the comment prefixes | |
1808 (setq p (point-min)) | |
1809 (while (when (< p (point-max)) | |
1810 (goto-char p) | |
1811 (re-search-forward content-prefix-re nil t)) | |
1812 (replace-match "" nil nil) | |
1813 (setq p (1+ (point)))) | |
1814 | |
1815 ;; add an extra line to prevent the fill from doing it for us. | |
1816 (goto-char (point-max)) | |
1817 (insert "\n") | |
1818 | |
1819 ;; Fill the comment contents. | |
1820 (let ((fill-column (- fill-column content-indent))) | |
1821 (fill-region (point-min) (point-max))) | |
1822 | |
1823 (goto-char (point-max)) | |
1824 (delete-char -1) | |
1825 | |
1826 ;; Restore comment prefixes. | |
1827 (goto-char (point-min)) | |
1828 (end-of-line) ; Don't reset the first line. | |
1829 (setq p (point)) | |
1830 (while (when (< p (point-max)) | |
1831 (goto-char p) | |
1832 (re-search-forward "^" nil t)) | |
1833 (replace-match content-prefix nil nil) | |
1834 (setq p (1+ (point)))) | |
1835 | |
1836 (setq comment-end (point-max)) | |
1837 (widen) | |
1838 | |
1839 ;; Restore our position | |
1840 (goto-char marked-point) | |
1841 (set-marker marked-point nil) | |
1842 | |
1843 ;; React to the entire fill change as a whole. | |
1844 (delphi-progress-start) | |
1845 (delphi-parse-region comment-start comment-end) | |
1846 (delphi-progress-done)))))) | |
1847 | |
1848 (defun delphi-new-comment-line () | |
1849 "If in a // comment, does a newline, indented such that one is still in the | |
1850 comment block. If not in a // comment, just does a normal newline." | |
1851 (interactive) | |
1852 (let ((comment (delphi-current-token))) | |
1853 (if (not (eq 'comment-single-line (delphi-token-kind comment))) | |
1854 ;; Not in a // comment. Just do the normal newline. | |
1855 (delphi-newline) | |
1856 (let* ((start-comment (delphi-comment-block-start comment)) | |
1857 (comment-start (delphi-token-start start-comment)) | |
1858 (content-start (delphi-comment-content-start start-comment)) | |
1859 (prefix | |
1860 (concat (make-string (delphi-column-of comment-start) ?\ ) "//" | |
1861 (make-string (- content-start comment-start 2) ?\ )))) | |
1862 (delete-horizontal-space) | |
1863 (newline) | |
1864 (insert prefix))))) | |
1865 | |
1866 (defun delphi-match-token (token limit) | |
1867 ;; Sets the match region used by (match-string 0) and friends to the token's | |
1868 ;; region. Sets the current point to the end of the token (or limit). | |
1869 (set-match-data nil) | |
1870 (if token | |
1871 (let ((end (min (delphi-token-end token) limit))) | |
1872 (set-match-data (list (delphi-token-start token) end)) | |
1873 (goto-char end) | |
1874 token))) | |
1875 | |
1876 (defconst delphi-font-lock-defaults | |
1877 '(nil ; We have our own fontify routine, so keywords don't apply. | |
1878 t ; Syntactic fontification doesn't apply. | |
1879 nil ; Don't care about case since we don't use regexps to find tokens. | |
1880 nil ; Syntax alists don't apply. | |
1881 nil ; Syntax begin movement doesn't apply | |
1882 (font-lock-fontify-region-function . delphi-fontify-region) | |
1883 (font-lock-verbose . delphi-fontifying-progress-step)) | |
1884 "Delphi mode font-lock defaults. Syntactic fontification is ignored.") | |
1885 | |
1886 (defvar delphi-debug-mode-map | |
1887 (let ((kmap (make-sparse-keymap))) | |
1888 (mapcar #'(lambda (binding) (define-key kmap (car binding) (cadr binding))) | |
1889 '(("n" delphi-debug-goto-next-token) | |
1890 ("p" delphi-debug-goto-previous-token) | |
1891 ("t" delphi-debug-show-current-token) | |
1892 ("T" delphi-debug-tokenize-buffer) | |
1893 ("W" delphi-debug-tokenize-window) | |
1894 ("g" delphi-debug-goto-point) | |
1895 ("s" delphi-debug-show-current-string) | |
1896 ("a" delphi-debug-parse-buffer) | |
1897 ("w" delphi-debug-parse-window) | |
1898 ("f" delphi-debug-fontify-window) | |
1899 ("F" delphi-debug-fontify-buffer) | |
1900 ("r" delphi-debug-parse-region) | |
1901 ("c" delphi-debug-unparse-buffer) | |
1902 ("x" delphi-debug-show-is-stable) | |
1903 )) | |
1904 kmap) | |
1905 "Keystrokes for delphi-mode debug commands.") | |
1906 | |
1907 (defvar delphi-mode-map | |
1908 (let ((kmap (make-sparse-keymap))) | |
1909 (mapcar #'(lambda (binding) (define-key kmap (car binding) (cadr binding))) | |
1910 (list '("\r" delphi-newline) | |
1911 '("\t" delphi-tab) | |
1912 '("\177" backward-delete-char-untabify) | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1913 ;; '("\C-cd" delphi-find-current-def) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1914 ;; '("\C-cx" delphi-find-current-xdef) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1915 ;; '("\C-cb" delphi-find-current-body) |
25072 | 1916 '("\C-cu" delphi-find-unit) |
1917 '("\M-q" delphi-fill-comment) | |
1918 '("\M-j" delphi-new-comment-line) | |
1919 ;; Debug bindings: | |
1920 (list "\C-c\C-d" delphi-debug-mode-map))) | |
1921 kmap) | |
1922 "Keymap used in Delphi mode.") | |
1923 | |
1924 (defconst delphi-mode-syntax-table (make-syntax-table) | |
1925 "Delphi mode's syntax table. It is just a standard syntax table. | |
1926 This is ok since we do our own keyword/comment/string face coloring.") | |
1927 | |
1928 ;;;###autoload | |
1929 (defun delphi-mode (&optional skip-initial-parsing) | |
1930 "Major mode for editing Delphi code. \\<delphi-mode-map> | |
1931 \\[delphi-tab]\t- Indents the current line for Delphi code. | |
1932 \\[delphi-find-unit]\t- Search for a Delphi source file. | |
1933 \\[delphi-fill-comment]\t- Fill the current comment. | |
1934 \\[delphi-new-comment-line]\t- If in a // comment, do a new comment line. | |
1935 | |
1936 M-x indent-region also works for indenting a whole region. | |
1937 | |
1938 Customization: | |
1939 | |
1940 `delphi-indent-level' (default 3) | |
1941 Indentation of Delphi statements with respect to containing block. | |
1942 `delphi-compound-block-indent' (default 0) | |
1943 Extra indentation for blocks in compound statements. | |
1944 `delphi-case-label-indent' (default 0) | |
1945 Extra indentation for case statement labels. | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1946 `delphi-tab-always-indents' (default t) |
25072 | 1947 Non-nil means TAB in Delphi mode should always reindent the current line, |
1948 regardless of where in the line point is when the TAB command is used. | |
25317
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1949 `delphi-newline-always-indents' (default t) |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1950 Non-nil means NEWLINE in Delphi mode should always reindent the current |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1951 line, insert a blank line and move to the default indent column of the |
06136c6979f1
checked in with -k by blaak at 1999/08/18 05:08:39
Ray Blaak <blaak@infomatch.com>
parents:
25161
diff
changeset
|
1952 blank line. |
25072 | 1953 `delphi-search-path' (default .) |
1954 Directories to search when finding external units. | |
1955 `delphi-verbose' (default nil) | |
1956 If true then delphi token processing progress is reported to the user. | |
1957 | |
1958 Coloring: | |
1959 | |
1960 `delphi-comment-face' (default font-lock-comment-face) | |
1961 Face used to color delphi comments. | |
1962 `delphi-string-face' (default font-lock-string-face) | |
1963 Face used to color delphi strings. | |
1964 `delphi-keyword-face' (default font-lock-keyword-face) | |
1965 Face used to color delphi keywords. | |
1966 `delphi-other-face' (default nil) | |
1967 Face used to color everything else. | |
1968 | |
1969 Turning on Delphi mode calls the value of the variable delphi-mode-hook with | |
1970 no args, if that value is non-nil." | |
1971 (interactive) | |
1972 (kill-all-local-variables) | |
1973 (use-local-map delphi-mode-map) | |
1974 (setq major-mode 'delphi-mode) | |
1975 (setq mode-name "Delphi") | |
1976 | |
1977 (setq local-abbrev-table delphi-mode-abbrev-table) | |
1978 (set-syntax-table delphi-mode-syntax-table) | |
1979 | |
1980 ;; Buffer locals: | |
1981 (mapcar #'(lambda (var) | |
1982 (let ((var-symb (car var)) | |
1983 (var-val (cadr var))) | |
1984 (make-local-variable var-symb) | |
1985 (set var-symb var-val))) | |
1986 (list '(indent-line-function delphi-indent-line) | |
1987 '(comment-indent-function delphi-indent-line) | |
1988 '(case-fold-search t) | |
1989 '(delphi-progress-last-reported-point nil) | |
1990 '(delphi-ignore-changes nil) | |
1991 (list 'font-lock-defaults delphi-font-lock-defaults))) | |
1992 | |
1993 ;; We need to keep track of changes to the buffer to determine if we need | |
1994 ;; to retokenize changed text. | |
1995 (add-hook 'after-change-functions 'delphi-after-change nil t) | |
1996 | |
1997 (widen) | |
1998 (unless skip-initial-parsing | |
1999 (delphi-save-excursion | |
2000 (let ((delphi-verbose t)) | |
2001 (delphi-progress-start) | |
2002 (delphi-parse-region (point-min) (point-max)) | |
2003 (delphi-progress-done)))) | |
2004 | |
2005 (run-hooks 'delphi-mode-hook)) | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
35624
diff
changeset
|
2006 |
52401 | 2007 ;;; arch-tag: 410e192d-e9b5-4397-ad62-12340fc3fa41 |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
35624
diff
changeset
|
2008 ;;; delphi.el ends here |