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