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