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