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