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