1;;; delphi.el --- major mode for editing Delphi source (Object Pascal) in Emacs 2 3;; Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007 4;; Free Software Foundation, Inc. 5 6;; Author: Ray Blaak <blaak@infomatch.com> 7;; Maintainer: FSF (Blaak's email addr bounces, Aug 2005) 8;; Keywords: languages 9 10;; This file is part of GNU Emacs. 11 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. 16 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. 21 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. 25 26;;; Commentary: 27 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. 31;; 32;; (autoload 'delphi-mode "delphi") 33;; (setq auto-mode-alist 34;; (cons '("\\.\\(pas\\|dpr\\|dpk\\)$" . delphi-mode) auto-mode-alist)) 35 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: 39;; 40;; (add-hook 'delphi-mode-hook 'turn-on-font-lock) 41 42;; If font-lock is not loaded by default, you might have to do: 43;; 44;; (autoload 'font-lock-mode "font-lock") 45;; (autoload 'turn-on-font-lock "font-lock") 46;; (setq font-lock-support-mode 'lazy-lock-mode) 47;; 48;; Lazy lock is very necessary for faster screen updates. 49 50;; For good performance, be sure to byte-compile delphi.el, e.g. 51;; 52;; M-x byte-compile-file <give the path to delphi.el when prompted> 53 54;; This will generate delphi.elc, which will be loaded instead of delphi.el 55;; when delphi-mode is autoloaded. 56 57;; When you have entered Delphi mode, you may get more info by pressing 58;; C-h m. 59 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. 64 65;;; Code: 66 67(provide 'delphi) 68 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))) 90 ) 91 92(defgroup delphi nil 93 "Major mode for editing Delphi source in Emacs." 94 :version "21.1" 95 :group 'languages) 96 97(defconst delphi-debug nil 98 "True if in debug mode.") 99 100(defcustom delphi-search-path "." 101 "*Directories to search when finding external units. It is a list of 102directory strings. If only a single directory, it can be a single 103string instead of a list. If a directory ends in \"...\" then that 104directory is recursively searched." 105 :type 'string 106 :group 'delphi) 107 108(defcustom delphi-indent-level 3 109 "*Indentation of Delphi statements with respect to containing block. E.g. 110 111begin 112 // This is an indent of 3. 113end;" 114 :type 'integer 115 :group 'delphi) 116 117(defcustom delphi-compound-block-indent 0 118 "*Extra indentation for blocks in compound statements. E.g. 119 120// block indent = 0 vs // block indent = 2 121if b then if b then 122begin begin 123end else begin end 124end; else 125 begin 126 end;" 127 :type 'integer 128 :group 'delphi) 129 130(defcustom delphi-case-label-indent delphi-indent-level 131 "*Extra indentation for case statement labels. E.g. 132 133// case indent = 0 vs // case indent = 3 134case value of case value of 135v1: process_v1; v1: process_v1; 136v2: process_v2; v2: process_v2; 137else else 138 process_else; process_else; 139end; end;" 140 :type 'integer 141 :group 'delphi) 142 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) 147 148(defcustom delphi-tab-always-indents t 149 "*Non-nil means TAB in Delphi mode should always reindent the current line, 150regardless of where in the line point is when the TAB command is used." 151 :type 'boolean 152 :group 'delphi) 153 154(defcustom delphi-newline-always-indents t 155 "*Non-nil means NEWLINE in Delphi mode should always reindent the current 156line, insert a blank line and move to the default indent column of the blank 157line. If nil, then no indentation occurs, and NEWLINE does the usual 158behavior. This is useful when one needs to do customized indentation that 159differs from the default." 160 :type 'boolean 161 :group 'delphi) 162 163(defcustom delphi-comment-face 'font-lock-comment-face 164 "*Face used to color delphi comments." 165 :type 'face 166 :group 'delphi) 167 168(defcustom delphi-string-face 'font-lock-string-face 169 "*Face used to color delphi strings." 170 :type 'face 171 :group 'delphi) 172 173(defcustom delphi-keyword-face 'font-lock-keyword-face 174 "*Face used to color delphi keywords." 175 :type 'face 176 :group 'delphi) 177 178(defcustom delphi-other-face nil 179 "*Face used to color everything else." 180 :type '(choice (const :tag "None" nil) face) 181 :group 'delphi) 182 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.") 190 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 201 202 ;; These routines should be keywords, if Borland had the balls. 203 break exit) 204 205 ;; We want directives to look like keywords. 206 delphi-directives) 207 "Delphi4 keywords.") 208 209(defconst delphi-previous-terminators `(semicolon comma) 210 "Expression/statement terminators that denote a previous expression.") 211 212(defconst delphi-comments 213 '(comment-single-line comment-multi-line-1 comment-multi-line-2) 214 "Tokens that represent comments.") 215 216(defconst delphi-strings 217 '(string double-quoted-string) 218 "Tokens that represent string literals.") 219 220(defconst delphi-whitespace `(space newline ,@delphi-comments) 221 "Tokens that are considered whitespace.") 222 223(defconst delphi-routine-statements 224 '(procedure function constructor destructor property) 225 "Marks the start of a routine, or routine-ish looking expression.") 226 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 229are followed by an expression.") 230 231(defconst delphi-expr-statements `(case ,@delphi-body-expr-statements) 232 "Expression statements contain expressions after their keyword.") 233 234(defconst delphi-body-statements `(else ,@delphi-body-expr-statements) 235 "Statements that have either a single statement or a block as a body.") 236 237(defconst delphi-expr-delimiters '(then do of) 238 "Expression delimiter tokens.") 239 240(defconst delphi-binary-ops 241 '(plus minus equals not-equals times divides div mod and or xor) 242 "Delphi binary operations.") 243 244(defconst delphi-visibilities '(public private protected published automated) 245 "Class visibilities.") 246 247(defconst delphi-block-statements 248 '(begin try case repeat initialization finalization asm) 249 "Statements that contain multiple substatements.") 250 251(defconst delphi-mid-block-statements 252 `(except finally ,@delphi-visibilities) 253 "Statements that mark mid sections of the enclosing block.") 254 255(defconst delphi-end-block-statements `(end until) 256 "Statements that end block sections.") 257 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.") 261 262(defconst delphi-decl-sections '(type const var label resourcestring) 263 "Denotes the start of a declaration section.") 264 265(defconst delphi-class-types '(class object) 266 "Class types.") 267 268(defconst delphi-composite-types `(,@delphi-class-types record) 269 "Types that contain declarations within them.") 270 271(defconst delphi-unit-sections 272 '(interface implementation program library package) 273 "Unit sections within which the indent is 0.") 274 275(defconst delphi-use-clauses `(uses requires exports contains) 276 "Statements that refer to foreign symbols.") 277 278(defconst delphi-unit-statements 279 `(,@delphi-use-clauses ,@delphi-unit-sections initialization finalization) 280 "Statements indented at level 0.") 281 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.") 286 287(defconst delphi-decl-matchers 288 `(begin ,@delphi-decl-sections) 289 "Statements that should match to declaration statement indentation.") 290 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.") 295 296(defconst delphi-previous-statements 297 `(,@delphi-unit-statements ,@delphi-routine-statements) 298 "Delimits a previous statement.") 299 300(defconst delphi-previous-enclosing-statements 301 `(,@delphi-block-statements ,@delphi-mid-block-statements 302 ,@delphi-decl-sections) 303 "Delimits a previous enclosing statement.") 304 305(defconst delphi-begin-enclosing-tokens 306 `(,@delphi-block-statements ,@delphi-mid-block-statements) 307 "Tokens that a begin token indents from.") 308 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 312routine.") 313 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_") 319 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)))) 327 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))))) 336 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)))) 355 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)) 359 360(defun delphi-string-of (start end) 361 ;; Returns the buffer string from start to end. 362 (buffer-substring-no-properties start end)) 363 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))))) 370 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]) 374 375(defsubst delphi-token-kind (token) 376 ;; Returns the kind symbol of the token. 377 (if token (aref token 0) nil)) 378 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))) 382 383(defsubst delphi-token-start (token) 384 ;; Returns the start point of the token. 385 (if token (aref token 1) (point-min))) 386 387(defsubst delphi-token-end (token) 388 ;; Returns the end point of the token. 389 (if token (aref token 2) (point-min))) 390 391(defun delphi-set-token-start (token start) 392 ;; Sets the start point of the token. 393 (if token (aset token 1 start))) 394 395(defun delphi-set-token-end (token end) 396 ;; Sets the end point of the token. 397 (if token (aset token 2 end))) 398 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 "")) 404 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)))) 408 409(defun delphi-column-of (p) 410 ;; Returns the column of the point p. 411 (save-excursion (goto-char p) (current-column))) 412 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))) 419 420(defvar delphi-progress-last-reported-point nil 421 "The last point at which progress was reported.") 422 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.") 429 430(defun delphi-progress-start () 431 ;; Initializes progress reporting. 432 (setq delphi-progress-last-reported-point nil)) 433 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)))) 441 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)) 448 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)))))) 455 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)) 465 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))) 471 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))) 476 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 . "\""))))) 485 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 . "\""))))) 494 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]"))))) 505 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)))) 512 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. 518 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))))) 525 526 ;; Special case: strings cannot span lines. 527 (and (delphi-is kind delphi-strings) (eq ?\n (char-after (1- p))))))) 528 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)))))) 539 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)))) 549 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))) 556 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)))) 569 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)))) 584 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))) 588 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))))))) 596 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))) 600 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)))) 606 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)) 622 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)) 627 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)))) 639 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))) 646 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)) 651 652 ((delphi-space-token-at p)) 653 654 ((delphi-word-token-at p)) 655 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)) 670 671 ((delphi-explicit-token-at p "<>" 'not-equals)) 672 673 ((delphi-point-token-at p 'punctuation))))) 674 675(defun delphi-current-token () 676 ;; Returns the delphi source token under the current point. 677 (delphi-token-at (point))) 678 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))) 687 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))) 696 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)) 704 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)))) 712 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))))) 722 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))))) 740 741(defvar delphi-ignore-changes t 742 "Internal flag to control if the delphi-mode responds to buffer changes. 743Defaults to t in case the delphi-after-change function is called on a 744non-delphi buffer. Set to nil in a delphi buffer. To override, just do: 745 (let ((delphi-ignore-changes t)) ...)") 746 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))))) 759 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))) 774 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))) 789 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)) 802 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))) 816 817 ;; Stop at the beginning of the line or an open group. 818 ((delphi-is kind '(newline open-group)) (throw 'done nil)) 819 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))) 825 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)) 842 843 ;; Skip over ()/[] groups. 844 ((eq 'close-group kind) (setq token (delphi-group-start token))) 845 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))) 852 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)))) 860 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)) 867 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)) 884 885 ;; Skip over the inheritance list. 886 ((eq 'open-group token-kind) (setq token (delphi-group-end token))) 887 888 ;; Only allow "of" and whitespace, and an identifier 889 ((delphi-is token-kind `(of word ,@delphi-whitespace))) 890 891 ;; Otherwise we are not in a simple class declaration. 892 ((throw 'done nil))) 893 (setq token (delphi-next-token token))))))) 894 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))) 907 908 ;; Regular block start found. 909 ((delphi-is token-kind delphi-block-statements) (throw 'done token)) 910 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))) 914 ) 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))) 920 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))) 933 934 ;; Skip over any nested blocks. 935 ((delphi-is token-kind delphi-end-block-statements) 936 (setq token (delphi-block-start token))) 937 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))) 943 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)) 948 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))) 955 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))))) 965 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))) 983 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))) 1001 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)))))) 1010 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)) 1024 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)) 1029 1030 ;; Indent according to the comment's content start. 1031 ((delphi-column-of (delphi-comment-content-start comment))))))) 1032 )) 1033 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)) 1048 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))) 1054 1055 ;; Nothing else is. 1056 ((throw 'done nil))) 1057 (setq token (delphi-previous-token token))) 1058 nil)))) 1059 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))))) 1072 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))) 1087 1088 ;; Skip over any ()/[] groups. 1089 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) 1090 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)))) 1098 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)) 1110 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))) 1118 1119 ;; Ignore whitespace. 1120 ((delphi-is token-kind delphi-whitespace)) 1121 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)) 1125 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)))) 1132 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)))) 1145 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))) 1149 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)))) 1159 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)))) 1170 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))) 1174 ) 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))) 1180 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))) 1197 1198 ;; Skip over any ()/[] groups. 1199 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) 1200 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)))) 1209 1210 ;; Remember if we have encountered any forward routine declarations. 1211 ((eq 'forward token-kind) 1212 (setq nested-block-count (1+ nested-block-count))) 1213 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))) 1218 1219 ;; Remember if we have encountered any statement terminators. 1220 ((eq 'semicolon token-kind) (setq last-terminator token)) 1221 1222 ;; Remember if we have encountered any expression delimiters. 1223 ((delphi-is token-kind delphi-expr-delimiters) 1224 (setq expr-delimited token)) 1225 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))) 1232 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))) 1242 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))) 1247 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))) 1253 1254 ;; Unit statements mean we indent right to the left. 1255 ((delphi-is token-kind delphi-unit-statements) (throw 'done 0)) 1256 ) 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))) 1262 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. 1284 0 1285 delphi-indent-level)))) 1286 1287 ;; Skip over any ()/[] groups. 1288 ((eq 'close-group token-kind) (setq token (delphi-group-start token))) 1289 1290 ;; Skip over any nested blocks. 1291 ((delphi-is token-kind delphi-end-block-statements) 1292 (setq token (delphi-block-start token))) 1293 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)) 1300 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)) 1309 1310 ((and last-token (delphi-is from-kind delphi-binary-ops)) 1311 ;; Align binary ops with the expression. 1312 (delphi-indent-of last-token)) 1313 1314 (last-token 1315 ;; Indent in from the expression. 1316 (delphi-indent-of last-token delphi-indent-level)) 1317 1318 ;; Indent in from the statement's keyword. 1319 ((delphi-indent-of token delphi-indent-level))))) 1320 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)))) 1333 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))) 1341 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))) 1347 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))) 1352 1353 ;; In unit sections we indent right to the left. 1354 ((delphi-is token-kind delphi-unit-sections) (throw 'done 0)) 1355 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)) 1364 1365 (last-token 1366 ;; Indent in from the expression. 1367 (delphi-indent-of last-token delphi-indent-level)) 1368 1369 ;; No enclosing expression; use the previous statment's 1370 ;; indent. 1371 ((delphi-previous-indent-of token))))) 1372 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)) 1383 1384 ((+ (delphi-section-indent-of token) delphi-indent-level))))) 1385 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))) 1389 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))) 1394 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))) 1399 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)))) 1411 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))) 1427 1428 ;; Assignments (:=) we skip over to get a normal indent. 1429 ((eq (delphi-token-kind last-token) 'equals)) 1430 1431 ;; Otherwise indent in from the equals. 1432 ((throw 'done 1433 (delphi-indent-of before-equals delphi-indent-level))))) 1434 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)) 1439 ) 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))) 1445 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))) 1459 1460 ((delphi-is token-kind delphi-unit-statements) 0) 1461 1462 ((delphi-is token-kind delphi-comments) 1463 ;; In a comment. 1464 (delphi-comment-indent-of token)) 1465 1466 ((delphi-is token-kind delphi-decl-matchers) 1467 ;; Use a previous section/routine's indent. 1468 (delphi-section-indent-of token)) 1469 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)) 1479 1480 ;; Otherwise just indent to the block start. 1481 ((delphi-stmt-line-indent-of block-start 0))))) 1482 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)) 1486 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))) 1492 1493(defun delphi-indent-line () 1494 "Indent the current line according to the current language construct. If 1495before 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)))) 1518 1519(defvar delphi-mode-abbrev-table nil 1520 "Abbrev table in use in delphi-mode buffers.") 1521(define-abbrev-table 'delphi-mode-abbrev-table ()) 1522 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)))) 1528 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)))) 1537 1538;; Debugging helpers: 1539 1540(defvar delphi-debug-buffer nil 1541 "Buffer to write delphi-mode debug messages to. Created on demand.") 1542 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")))) 1551 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)) 1559 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)))) 1564 1565(defun delphi-debug-goto-point (p) 1566 (interactive "NGoto char: ") 1567 (goto-char p)) 1568 1569(defun delphi-debug-goto-next-token () 1570 (interactive) 1571 (goto-char (delphi-token-start (delphi-next-token (delphi-current-token))))) 1572 1573(defun delphi-debug-goto-previous-token () 1574 (interactive) 1575 (goto-char 1576 (delphi-token-start (delphi-previous-token (delphi-current-token))))) 1577 1578(defun delphi-debug-show-current-string (from to) 1579 (interactive "r") 1580 (delphi-debug-log "String: %S" (buffer-substring from to))) 1581 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)))) 1588 1589(defun delphi-debug-unparse-buffer () 1590 (interactive) 1591 (delphi-set-text-properties (point-min) (point-max) nil)) 1592 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")))) 1600 1601(defun delphi-debug-parse-window () 1602 (interactive) 1603 (delphi-debug-parse-region (window-start) (window-end))) 1604 1605(defun delphi-debug-parse-buffer () 1606 (interactive) 1607 (delphi-debug-parse-region (point-min) (point-max))) 1608 1609(defun delphi-debug-fontify-window () 1610 (interactive) 1611 (delphi-fontify-region (window-start) (window-end) t)) 1612 1613(defun delphi-debug-fontify-buffer () 1614 (interactive) 1615 (delphi-fontify-region (point-min) (point-max) t)) 1616 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"))) 1626 1627(defun delphi-debug-tokenize-buffer () 1628 (interactive) 1629 (delphi-debug-tokenize-region (point-min) (point-max))) 1630 1631(defun delphi-debug-tokenize-window () 1632 (interactive) 1633 (delphi-debug-tokenize-region (window-start) (window-end))) 1634 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))) 1648 1649 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"))) 1659 1660 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)))) 1665 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))))) 1670 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) 1686 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)) 1695 1696 ;; Not found. 1697 nil)))) 1698 1699 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))) 1713 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 ".")) 1720 1721 ((stringp delphi-search-path) 1722 (delphi-find-unit-in-directory unit delphi-search-path)) 1723 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)) 1730 1731(defun delphi-find-unit (unit) 1732 "Finds the specified delphi source file according to `delphi-search-path'. 1733If 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)) 1745 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")) 1750 1751(defun delphi-find-current-xdef () 1752 "Find the definition of the identifier under the current point, searching 1753in external units if necessary (as listed in the current unit's use clause). 1754The set of directories to search for a unit is specified by the global variable 1755delphi-search-path." 1756 (interactive) 1757 (error "delphi-find-current-xdef: not implemented yet")) 1758 1759(defun delphi-find-current-body () 1760 "Find the body of the identifier under the current point, assuming 1761it is a routine." 1762 (interactive) 1763 (error "delphi-find-current-body: not implemented yet")) 1764 1765(defun delphi-fill-comment () 1766 "Fills the text of the current comment, according to `fill-column'. 1767An 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))) 1799 1800 ;; Advance our marked point after inserted spaces. 1801 (set-marker-insertion-type marked-point t) 1802 1803 ;; Ensure we can modify the buffer 1804 (goto-char content-start) 1805 (insert " ") 1806 (delete-char -1) 1807 1808 (narrow-to-region content-start comment-end) 1809 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)))) 1817 1818 ;; add an extra line to prevent the fill from doing it for us. 1819 (goto-char (point-max)) 1820 (insert "\n") 1821 1822 ;; Fill the comment contents. 1823 (let ((fill-column (- fill-column content-indent))) 1824 (fill-region (point-min) (point-max))) 1825 1826 (goto-char (point-max)) 1827 (delete-char -1) 1828 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)))) 1838 1839 (setq comment-end (point-max)) 1840 (widen) 1841 1842 ;; Restore our position 1843 (goto-char marked-point) 1844 (set-marker marked-point nil) 1845 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))))))) 1850 1851(defun delphi-new-comment-line () 1852 "If in a // comment, does a newline, indented such that one is still in the 1853comment 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))))) 1868 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))) 1878 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.") 1888 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) 1906 )) 1907 kmap) 1908 "Keystrokes for delphi-mode debug commands.") 1909 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.") 1926 1927(defconst delphi-mode-syntax-table (make-syntax-table) 1928 "Delphi mode's syntax table. It is just a standard syntax table. 1929This is ok since we do our own keyword/comment/string face coloring.") 1930 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. 1938 1939M-x indent-region also works for indenting a whole region. 1940 1941Customization: 1942 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. 1960 1961Coloring: 1962 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. 1971 1972Turning on Delphi mode calls the value of the variable delphi-mode-hook with 1973no 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") 1979 1980 (setq local-abbrev-table delphi-mode-abbrev-table) 1981 (set-syntax-table delphi-mode-syntax-table) 1982 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))) 1995 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) 1999 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)))) 2007 2008 (run-mode-hooks 'delphi-mode-hook)) 2009 2010;;; arch-tag: 410e192d-e9b5-4397-ad62-12340fc3fa41 2011;;; delphi.el ends here 2012