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