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