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