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