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