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