delphi.el 79 KB

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