123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034 |
- (provide 'delphi)
- (defgroup delphi nil
- "Major mode for editing Delphi source in Emacs."
- :version "21.1"
- :group 'languages)
- (defconst delphi-debug nil
- "True if in debug mode.")
- (defcustom delphi-search-path "."
- "*Directories to search when finding external units.
- It is a list of directory strings. If only a single directory,
- it can be a single string instead of a list. If a directory
- ends in \"...\" then that directory is recursively searched."
- :type 'string
- :group 'delphi)
- (defcustom delphi-indent-level 3
- "*Indentation of Delphi statements with respect to containing block.
- E.g.
- begin
- // This is an indent of 3.
- end;"
- :type 'integer
- :group 'delphi)
- (defcustom delphi-compound-block-indent 0
- "*Extra indentation for blocks in compound statements. E.g.
- // block indent = 0 vs // block indent = 2
- if b then if b then
- begin begin
- end else begin end
- end; else
- begin
- end;"
- :type 'integer
- :group 'delphi)
- (defcustom delphi-case-label-indent delphi-indent-level
- "*Extra indentation for case statement labels. E.g.
- // case indent = 0 vs // case indent = 3
- case value of case value of
- v1: process_v1; v1: process_v1;
- v2: process_v2; v2: process_v2;
- else else
- process_else; process_else;
- end; end;"
- :type 'integer
- :group 'delphi)
- (defcustom delphi-verbose t
- "*If true then Delphi token processing progress is reported to the user."
- :type 'boolean
- :group 'delphi)
- (defcustom delphi-tab-always-indents t
- "*Non-nil means TAB in Delphi mode should always reindent the current line,
- regardless of where in the line point is when the TAB command is used."
- :type 'boolean
- :group 'delphi)
- (defcustom delphi-newline-always-indents t
- "*Non-nil means NEWLINE in Delphi mode should always reindent the current
- line, insert a blank line and move to the default indent column of the blank
- line. If nil, then no indentation occurs, and NEWLINE does the usual
- behavior. This is useful when one needs to do customized indentation that
- differs from the default."
- :type 'boolean
- :group 'delphi)
- (defcustom delphi-comment-face 'font-lock-comment-face
- "*Face used to color Delphi comments."
- :type 'face
- :group 'delphi)
- (defcustom delphi-string-face 'font-lock-string-face
- "*Face used to color Delphi strings."
- :type 'face
- :group 'delphi)
- (defcustom delphi-keyword-face 'font-lock-keyword-face
- "*Face used to color Delphi keywords."
- :type 'face
- :group 'delphi)
- (defcustom delphi-other-face nil
- "*Face used to color everything else."
- :type '(choice (const :tag "None" nil) face)
- :group 'delphi)
- (defconst delphi-directives
- '(absolute abstract assembler automated cdecl default dispid dynamic
- export external far forward index inline message name near nodefault
- overload override pascal private protected public published read readonly
- register reintroduce resident resourcestring safecall stdcall stored
- virtual write writeonly)
- "Delphi4 directives.")
- (defconst delphi-keywords
- (append
- '(
- and array as asm at begin case class const constructor contains
- destructor dispinterface div do downto else end except exports
- file finalization finally for function goto if implementation implements
- in inherited initialization interface is label library mod nil not
- of object on or out package packed procedure program property
- raise record repeat requires result self set shl shr then threadvar
- to try type unit uses until var while with xor
-
- break exit)
-
- delphi-directives)
- "Delphi4 keywords.")
- (defconst delphi-previous-terminators `(semicolon comma)
- "Expression/statement terminators that denote a previous expression.")
- (defconst delphi-comments
- '(-single-line comment-multi-line-1 comment-multi-line-2)
- "Tokens that represent comments.")
- (defconst delphi-strings
- '(string double-quoted-string)
- "Tokens that represent string literals.")
- (defconst delphi-whitespace `(space newline ,@delphi-comments)
- "Tokens that are considered whitespace.")
- (defconst delphi-routine-statements
- '(procedure function constructor destructor property)
- "Marks the start of a routine, or routine-ish looking expression.")
- (defconst delphi-body-expr-statements '(if while for on)
- "Statements that have either a single statement or a block as a body and also
- are followed by an expression.")
- (defconst delphi-expr-statements `(case ,@delphi-body-expr-statements)
- "Expression statements contain expressions after their keyword.")
- (defconst delphi-body-statements `(else ,@delphi-body-expr-statements)
- "Statements that have either a single statement or a block as a body.")
- (defconst delphi-expr-delimiters '(then do of)
- "Expression delimiter tokens.")
- (defconst delphi-binary-ops
- '(plus minus equals not-equals times divides div mod and or xor)
- "Delphi binary operations.")
- (defconst delphi-visibilities '(public private protected published automated)
- "Class visibilities.")
- (defconst delphi-block-statements
- '(begin try case repeat initialization finalization asm)
- "Statements that contain multiple substatements.")
- (defconst delphi-mid-block-statements
- `(except finally ,@delphi-visibilities)
- "Statements that mark mid sections of the enclosing block.")
- (defconst delphi-end-block-statements `(end until)
- "Statements that end block sections.")
- (defconst delphi-match-block-statements
- `(,@delphi-end-block-statements ,@delphi-mid-block-statements)
- "Statements that match the indentation of the parent block.")
- (defconst delphi-decl-sections '(type const var label resourcestring)
- "Denotes the start of a declaration section.")
- (defconst delphi-interface-types '(dispinterface interface)
- "Interface types.")
- (defconst delphi-class-types '(class object)
- "Class types.")
- (defconst delphi-composite-types
- `(,@delphi-class-types ,@delphi-interface-types record)
- "Types that contain declarations within them.")
- (defconst delphi-unit-sections
- '(interface implementation program library package)
- "Unit sections within which the indent is 0.")
- (defconst delphi-use-clauses `(uses requires exports contains)
- "Statements that refer to foreign symbols.")
- (defconst delphi-unit-statements
- `(,@delphi-use-clauses ,@delphi-unit-sections initialization finalization)
- "Statements indented at level 0.")
- (defconst delphi-decl-delimiters
- `(,@delphi-decl-sections ,@delphi-unit-statements
- ,@delphi-routine-statements)
- "Statements that a declaration statement should align with.")
- (defconst delphi-decl-matchers
- `(begin ,@delphi-decl-sections)
- "Statements that should match to declaration statement indentation.")
- (defconst delphi-enclosing-statements
- `(,@delphi-block-statements ,@delphi-mid-block-statements
- ,@delphi-decl-sections ,@delphi-use-clauses ,@delphi-routine-statements)
- "Delimits an enclosing statement.")
- (defconst delphi-previous-statements
- `(,@delphi-unit-statements ,@delphi-routine-statements)
- "Delimits a previous statement.")
- (defconst delphi-previous-enclosing-statements
- `(,@delphi-block-statements ,@delphi-mid-block-statements
- ,@delphi-decl-sections)
- "Delimits a previous enclosing statement.")
- (defconst delphi-begin-enclosing-tokens
- `(,@delphi-block-statements ,@delphi-mid-block-statements)
- "Tokens that a begin token indents from.")
- (defconst delphi-begin-previous-tokens
- `(,@delphi-decl-sections ,@delphi-routine-statements)
- "Tokens that a begin token aligns with, but only if not part of a nested
- routine.")
- (defconst delphi-space-chars "\000-\011\013- ")
- (defconst delphi-non-space-chars (concat "^" delphi-space-chars))
- (defconst delphi-spaces-re (concat "[" delphi-space-chars "]*"))
- (defconst delphi-leading-spaces-re (concat "^" delphi-spaces-re))
- (defconst delphi-word-chars "a-zA-Z0-9_")
- (defmacro delphi-save-match-data (&rest forms)
-
-
- `(let ((data (match-data)))
- (unwind-protect
- (progn ,@forms)
- (set-match-data data))))
- (defmacro delphi-save-excursion (&rest forms)
-
-
- `(save-excursion
- (delphi-save-match-data
- (let ((inhibit-point-motion-hooks t)
- (deactivate-mark nil))
- (progn ,@forms)))))
- (defmacro delphi-save-state (&rest forms)
-
-
- `(let ((delphi-ignore-changes t)
- (old-supersession-threat
- (symbol-function 'ask-user-about-supersession-threat))
- (buffer-read-only nil)
- (inhibit-read-only t)
- (buffer-undo-list t)
- (before-change-functions nil)
- (after-change-functions nil)
- (modified (buffer-modified-p)))
-
- (fset 'ask-user-about-supersession-threat (lambda (_fn)))
- (unwind-protect
- (progn ,@forms)
- (set-buffer-modified-p modified)
- (fset 'ask-user-about-supersession-threat old-supersession-threat))))
- (defsubst delphi-is (element in-set)
-
- (memq element in-set))
- (defun delphi-string-of (start end)
-
- (buffer-substring-no-properties start end))
- (defun delphi-looking-at-string (p s)
-
-
- (let ((limit (+ p (length s))))
- (and (<= limit (point-max))
- (string= s (delphi-string-of p limit)))))
- (defun delphi-token-of (kind start end)
-
- `[,kind ,start ,end])
- (defsubst delphi-token-kind (token)
-
- (if token (aref token 0) nil))
- (defun delphi-set-token-kind (token to-kind)
-
- (if token (aset token 0 to-kind)))
- (defsubst delphi-token-start (token)
-
- (if token (aref token 1) (point-min)))
- (defsubst delphi-token-end (token)
-
- (if token (aref token 2) (point-min)))
- (defun delphi-set-token-start (token start)
-
- (if token (aset token 1 start)))
- (defun delphi-set-token-end (token end)
-
- (if token (aset token 2 end)))
- (defun delphi-token-string (token)
-
- (if token
- (delphi-string-of (delphi-token-start token) (delphi-token-end token))
- ""))
- (defun delphi-in-token (p token)
-
- (and (<= (delphi-token-start token) p) (< p (delphi-token-end token))))
- (defun delphi-column-of (p)
-
- (save-excursion (goto-char p) (current-column)))
- (defun delphi-face-of (token-kind)
-
- (cond ((delphi-is token-kind delphi-comments) delphi-comment-face)
- ((delphi-is token-kind delphi-strings) delphi-string-face)
- ((delphi-is token-kind delphi-keywords) delphi-keyword-face)
- (delphi-other-face)))
- (defvar delphi-progress-last-reported-point nil
- "The last point at which progress was reported.")
- (defconst delphi-parsing-progress-step 16384
- "Number of chars to process before the next parsing progress report.")
- (defconst delphi-scanning-progress-step 2048
- "Number of chars to process before the next scanning progress report.")
- (defconst delphi-fontifying-progress-step delphi-scanning-progress-step
- "Number of chars to process before the next fontification progress report.")
- (defun delphi-progress-start ()
-
- (setq delphi-progress-last-reported-point nil))
- (defun delphi-progress-done (&rest msgs)
-
- (setq delphi-progress-last-reported-point nil)
- (when delphi-verbose
- (if (null msgs)
- (message "")
- (apply #'message msgs))))
- (defun delphi-step-progress (p desc step-size)
-
-
- (cond ((null delphi-progress-last-reported-point)
-
- (setq delphi-progress-last-reported-point p))
- ((and delphi-verbose
- (>= (abs (- p delphi-progress-last-reported-point)) step-size))
-
- (setq delphi-progress-last-reported-point p)
- (message "%s %s ... %d%%"
- desc (buffer-name) (/ (* 100 p) (point-max))))))
- (defun delphi-next-line-start (&optional from-point)
-
- (let ((curr-point (point))
- (next nil))
- (if from-point (goto-char from-point))
- (end-of-line)
- (setq next (min (1+ (point)) (point-max)))
- (goto-char curr-point)
- next))
- (defvar delphi-ignore-changes t
- "Internal flag to control if the Delphi mode responds to buffer changes.
- Defaults to t in case the `delphi-after-change' function is called on a
- non-Delphi buffer. Set to nil in a Delphi buffer. To override, just do:
- (let ((delphi-ignore-changes t)) ...)")
- (defun delphi-set-text-properties (from to properties)
-
-
- (delphi-save-state
- (set-text-properties from to properties)))
- (defun delphi-literal-kind (p)
-
- (if (and (<= (point-min) p) (<= p (point-max)))
- (get-text-property p 'token)))
- (defun delphi-literal-start-pattern (literal-kind)
-
- (cdr (assoc literal-kind
- '((-single-line . "//")
- (-multi-line-1 . "{")
- (-multi-line-2 . "(*")
- (string . "'")
- (double-quoted-string . "\"")))))
- (defun delphi-literal-end-pattern (literal-kind)
-
- (cdr (assoc literal-kind
- '((-single-line . "\n")
- (-multi-line-1 . "}")
- (-multi-line-2 . "*)")
- (string . "'")
- (double-quoted-string . "\"")))))
- (defun delphi-literal-stop-pattern (literal-kind)
-
-
- (cdr (assoc literal-kind
- '((-single-line . "\n")
- (-multi-line-1 . "}")
- (-multi-line-2 . "\\*)")
-
- (string . "['\n]")
- (double-quoted-string . "[\"\n]")))))
- (defun delphi-is-literal-start (p)
-
- (let* ((kind (delphi-literal-kind p))
- (pattern (delphi-literal-start-pattern kind)))
- (or (null kind)
- (delphi-looking-at-string p pattern))))
- (defun delphi-is-literal-end (p)
-
- (let* ((kind (delphi-literal-kind (1- p)))
- (pattern (delphi-literal-end-pattern kind)))
- (or (null kind)
- (and (delphi-looking-at-string (- p (length pattern)) pattern)
- (or (not (delphi-is kind delphi-strings))
-
-
-
- (not (delphi-is-literal-end (1- p)))))
-
- (and (delphi-is kind delphi-strings) (eq ?\n (char-after (1- p)))))))
- (defun delphi-is-stable-literal (p)
-
-
-
- (let ((at-start (delphi-is-literal-start p))
- (at-end (delphi-is-literal-end p)))
- (or (>= p (point-max))
- (and at-start at-end)
- (and (not at-start) (not at-end)
- (eq (delphi-literal-kind (1- p)) (delphi-literal-kind p))))))
- (defun delphi-complete-literal (literal-kind limit)
-
-
- (let ((pattern (delphi-literal-stop-pattern literal-kind)))
- (if (not (stringp pattern))
- (error "Invalid literal kind %S" literal-kind)
-
- (re-search-forward pattern limit 'goto-limit-on-fail)
- (point))))
- (defun delphi-literal-text-properties (kind)
-
- (if (and (boundp 'font-lock-mode)
- font-lock-mode)
- (list 'token kind 'face (delphi-face-of kind) 'lazy-lock t)
- (list 'token kind)))
- (defun delphi-parse-next-literal (limit)
-
-
-
-
- (let ((search-start (point)))
- (cond ((not (delphi-is-literal-end search-start))
-
- (let ((kind (delphi-literal-kind (1- search-start))))
- (delphi-complete-literal kind limit)
- (delphi-set-text-properties
- search-start (point) (delphi-literal-text-properties kind))))
- ((re-search-forward
- "\\(//\\)\\|\\({\\)\\|\\((\\*\\)\\|\\('\\)\\|\\(\"\\)"
- limit 'goto-limit-on-fail)
-
- (let ((kind (cond ((match-beginning 1) 'comment-single-line)
- ((match-beginning 2) 'comment-multi-line-1)
- ((match-beginning 3) 'comment-multi-line-2)
- ((match-beginning 4) 'string)
- ((match-beginning 5) 'double-quoted-string)))
- (start (match-beginning 0)))
- (delphi-set-text-properties search-start start nil)
- (delphi-complete-literal kind limit)
- (delphi-set-text-properties
- start (point) (delphi-literal-text-properties kind))))
-
- ((delphi-set-text-properties search-start limit nil)))
- (delphi-step-progress (point) "Parsing" delphi-parsing-progress-step)))
- (defun delphi-literal-token-at (p)
-
- (let ((kind (delphi-literal-kind p)))
- (when kind
- (let ((start (previous-single-property-change (1+ p) 'token))
- (end (next-single-property-change p 'token)))
- (delphi-token-of kind (or start (point-min)) (or end (point-max)))))))
- (defun delphi-point-token-at (p kind)
-
- (delphi-token-of kind p (1+ p)))
- (defsubst delphi-char-token-at (p char kind)
-
-
- (when (eq char (char-after p))
- (delphi-token-of kind p (1+ p))))
- (defun delphi-charset-token-at (p charset kind)
-
-
- (let ((currp (point))
- (end nil)
- (token nil))
- (goto-char p)
- (when (> (skip-chars-forward charset) 0)
- (setq end (point))
- (goto-char (1+ p))
- (skip-chars-backward charset)
- (setq token (delphi-token-of kind (point) end)))
- (goto-char currp)
- token))
- (defun delphi-space-token-at (p)
-
-
- (delphi-charset-token-at p delphi-space-chars 'space))
- (defun delphi-word-token-at (p)
-
-
- (let ((word (delphi-charset-token-at p delphi-word-chars 'word)))
- (when word
- (let* ((word-image (downcase (delphi-token-string word)))
- (keyword (intern-soft word-image)))
- (when (and (or keyword (string= "nil" word-image))
- (delphi-is keyword delphi-keywords))
- (delphi-set-token-kind word keyword))
- word))))
- (defun delphi-explicit-token-at (p token-string kind)
-
-
- (let ((token (delphi-charset-token-at p token-string kind)))
- (when (and token (string= token-string (delphi-token-string token)))
- token)))
- (defun delphi-token-at (p)
-
- (when (and (<= (point-min) p) (<= p (point-max)))
- (cond ((delphi-char-token-at p ?\n 'newline))
- ((delphi-literal-token-at p))
- ((delphi-space-token-at p))
- ((delphi-word-token-at p))
- ((delphi-char-token-at p ?\( 'open-group))
- ((delphi-char-token-at p ?\) 'close-group))
- ((delphi-char-token-at p ?\[ 'open-group))
- ((delphi-char-token-at p ?\] 'close-group))
- ((delphi-char-token-at p ?\
- ((delphi-char-token-at p ?. 'dot))
- ((delphi-char-token-at p ?, 'comma))
- ((delphi-char-token-at p ?= 'equals))
- ((delphi-char-token-at p ?+ 'plus))
- ((delphi-char-token-at p ?- 'minus))
- ((delphi-char-token-at p ?* 'times))
- ((delphi-char-token-at p ?/ 'divides))
- ((delphi-char-token-at p ?: 'colon))
- ((delphi-explicit-token-at p "<>" 'not-equals))
- ((delphi-point-token-at p 'punctuation)))))
- (defun delphi-current-token ()
-
- (delphi-token-at (point)))
- (defun delphi-next-token (token)
-
- (when token
- (let ((next (delphi-token-at (delphi-token-end token))))
- (if next
- (delphi-step-progress (delphi-token-start next) "Scanning"
- delphi-scanning-progress-step))
- next)))
- (defun delphi-previous-token (token)
-
- (when token
- (let ((previous (delphi-token-at (1- (delphi-token-start token)))))
- (if previous
- (delphi-step-progress (delphi-token-start previous) "Scanning"
- delphi-scanning-progress-step))
- previous)))
- (defun delphi-next-visible-token (token)
-
- (let (next-token)
- (while (progn
- (setq next-token (delphi-next-token token))
- (delphi-is (delphi-token-kind next-token) '(space newline))))
- next-token))
- (defun delphi-parse-region (from to)
-
- (save-restriction
- (widen)
- (goto-char from)
- (while (< (point) to)
- (delphi-parse-next-literal to))))
- (defun delphi-parse-region-until-stable (from to)
-
-
-
- (save-restriction
- (widen)
- (delphi-parse-region from to)
- (while (not (delphi-is-stable-literal (point)))
- (delphi-parse-next-literal (point-max)))))
- (defun delphi-fontify-region (from to &optional verbose)
-
- (delphi-save-excursion
- (delphi-save-state
- (let ((p from)
- (delphi-verbose verbose)
- (token nil))
- (delphi-progress-start)
- (while (< p to)
-
- (setq token (delphi-token-at p))
- (add-text-properties
- (delphi-token-start token) (delphi-token-end token)
- (list 'face (delphi-face-of (delphi-token-kind token)) 'lazy-lock t))
- (setq p (delphi-token-end token))
- (delphi-step-progress p "Fontifying" delphi-fontifying-progress-step))
- (delphi-progress-done)))))
- (defun delphi-after-change (change-start change-end _old-length)
-
- (unless delphi-ignore-changes
- (let ((delphi-ignore-changes t))
- (delphi-save-excursion
- (delphi-progress-start)
-
-
- (delphi-parse-region-until-stable
- (delphi-token-start (delphi-token-at (1- change-start)))
- (progn (goto-char change-end) (end-of-line) (point)))
- (delphi-progress-done)))))
- (defun delphi-group-start (from-token)
-
- (let ((token (delphi-previous-token from-token))
- (token-kind nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq 'close-group token-kind) (setq token (delphi-group-start token)))
- ((eq 'open-group token-kind) (throw 'done token)))
- (setq token (delphi-previous-token token)))
-
- nil)))
- (defun delphi-group-end (from-token)
-
- (let ((token (delphi-next-token from-token))
- (token-kind nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq 'open-group token-kind) (setq token (delphi-group-end token)))
- ((eq 'close-group token-kind) (throw 'done token)))
- (setq token (delphi-next-token token)))
-
- nil)))
- (defun delphi-indent-of (token &optional offset)
-
- (let ((indent (+ (delphi-column-of (delphi-token-start token))
- (if offset offset 0))))
- (when delphi-debug
- (delphi-debug-log
- (concat "\n Indent of: %S %S"
- "\n column: %d indent: %d offset: %d")
- token (delphi-token-string token)
- (delphi-column-of (delphi-token-start token))
- indent (if offset offset 0)))
- indent))
- (defun delphi-line-indent-of (from-token &optional offset &rest terminators)
-
-
-
- (let ((token (delphi-previous-token from-token))
- (last-token from-token)
- (kind nil))
- (catch 'done
- (while token
- (setq kind (delphi-token-kind token))
- (cond
-
- ((eq 'close-group kind) (setq token (delphi-group-start token)))
-
- ((delphi-is kind '(newline open-group)) (throw 'done nil))
-
- ((delphi-is kind terminators) (throw 'done nil)))
- (unless (delphi-is kind delphi-whitespace) (setq last-token token))
- (setq token (delphi-previous-token token))))
- (delphi-indent-of last-token offset)))
- (defun delphi-stmt-line-indent-of (from-token &optional offset)
-
-
- (let ((token (delphi-previous-token from-token))
- (last-token from-token)
- (kind nil))
- (catch 'done
- (while token
- (setq kind (delphi-token-kind token))
- (cond
- ((and (eq 'colon kind)
- (delphi-is (delphi-token-kind last-token)
- `(,@delphi-block-statements
- ,@delphi-expr-statements)))
-
- (throw 'done nil))
-
- ((eq 'close-group kind) (setq token (delphi-group-start token)))
- ((delphi-is kind `(newline open-group ,@delphi-use-clauses))
-
- (throw 'done nil)))
- (unless (delphi-is kind delphi-whitespace) (setq last-token token))
- (setq token (delphi-previous-token token))))
- (delphi-indent-of last-token offset)))
- (defun delphi-open-group-indent (token last-token &optional offset)
-
- (when (eq 'open-group (delphi-token-kind token))
- (if last-token
- (delphi-indent-of last-token offset)
-
- (delphi-stmt-line-indent-of token delphi-indent-level))))
- (defun delphi-composite-type-start (token last-token)
-
-
-
- (if (and (eq 'equals (delphi-token-kind token))
- (delphi-is (delphi-token-kind last-token) delphi-composite-types))
- last-token))
- (defun delphi-is-simple-class-type (at-token limit-token)
-
-
-
-
- (when (delphi-is (delphi-token-kind at-token) delphi-class-types)
- (catch 'done
-
- (let ((token (delphi-next-token at-token))
- (token-kind nil)
- (limit (delphi-token-start limit-token)))
- (while (and token (<= (delphi-token-start token) limit))
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq 'semicolon token-kind) (throw 'done token))
-
- ((eq 'open-group token-kind) (setq token (delphi-group-end token)))
-
- ((delphi-is token-kind `(of word ,@delphi-whitespace)))
-
- ((throw 'done nil)))
- (setq token (delphi-next-token token)))))))
- (defun delphi-block-start (from-token &optional stop-on-class)
-
- (let ((token (delphi-previous-token from-token))
- (last-token nil)
- (token-kind nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((delphi-is token-kind delphi-end-block-statements)
- (setq token (delphi-block-start token)))
-
- ((delphi-is token-kind delphi-block-statements)
- (throw 'done
-
-
-
-
- (if (eq 'case token-kind)
- (let ((enclosing-token
- (delphi-block-start token
- 'stop-on-class)))
- (if
- (eq 'record
- (delphi-token-kind enclosing-token))
- (if stop-on-class
- enclosing-token
- (delphi-previous-token enclosing-token))
- token))
- token)))
-
- ((delphi-composite-type-start token last-token)
- (throw 'done (if stop-on-class last-token token)))
- )
- (unless (delphi-is token-kind delphi-whitespace)
- (setq last-token token))
- (setq token (delphi-previous-token token)))
-
- nil)))
- (defun delphi-else-start (from-else)
-
- (let ((token (delphi-previous-token from-else))
- (token-kind nil)
- (semicolon-count 0))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq 'close-group token-kind) (setq token (delphi-group-start token)))
-
- ((delphi-is token-kind delphi-end-block-statements)
- (setq token (delphi-block-start token)))
- ((eq 'semicolon token-kind)
-
-
-
- (setq semicolon-count (1+ semicolon-count)))
- ((and (eq 'if token-kind) (= semicolon-count 0))
-
-
- (throw 'done token))
- ((eq 'case token-kind)
-
- (throw 'done token)))
- (setq token (delphi-previous-token token)))
-
- nil)))
- (defun delphi-comment-content-start ()
-
- (let ((kind (delphi-token-kind comment)))
- (when (delphi-is kind delphi-comments)
- (delphi-save-excursion
- (goto-char (+ (delphi-token-start comment)
- (length (delphi-literal-start-pattern kind))))
- (skip-chars-forward delphi-space-chars)
- (point)))))
- (defun delphi-comment-block-start ()
-
-
-
- (if (not (eq 'comment-single-line (delphi-token-kind comment)))
- comment
-
- (let ((prev-comment comment)
- (start-comment comment))
- (while (let ((kind (delphi-token-kind prev-comment)))
- (cond ((eq kind 'space))
- ((eq kind 'comment-single-line)
- (setq start-comment prev-comment))
- (t nil)))
- (setq prev-comment (delphi-previous-token prev-comment)))
- start-comment)))
- (defun delphi-comment-block-end ()
-
-
-
- (if (not (eq 'comment-single-line (delphi-token-kind comment)))
- comment
-
- (let ((next-comment comment)
- (end-comment comment))
- (while (let ((kind (delphi-token-kind next-comment)))
- (cond ((eq kind 'space))
- ((eq kind 'comment-single-line)
- (setq end-comment next-comment))
- (t nil)))
- (setq next-comment (delphi-next-token next-comment)))
- end-comment)))
- (defun delphi-on-first-comment-line ()
-
- (save-excursion
- (let ((-start (delphi-token-start comment))
- (current-point (point)))
- (goto-char comment-start)
- (end-of-line)
- (and (<= comment-start current-point) (<= current-point (point))))))
- (defun delphi-comment-indent-of ()
-
- (let ((start-comment (delphi-comment-block-start comment)))
- (if (and (eq start-comment comment)
- (delphi-on-first-comment-line comment))
-
- (delphi-enclosing-indent-of comment)
- (save-excursion
- (let ((kind (delphi-token-kind comment)))
- (beginning-of-line)
- (cond ((eq 'comment-single-line kind)
-
- (delphi-indent-of start-comment))
- ((looking-at (concat delphi-leading-spaces-re
- (delphi-literal-stop-pattern kind)))
-
- (delphi-indent-of comment))
-
- ((delphi-column-of (delphi-comment-content-start comment)))))))
- ))
- (defun delphi-is-use-clause-end (at-token last-token last-colon from-kind)
-
- (when (and last-token
- (not last-colon)
- (eq 'comma (delphi-token-kind at-token))
- (eq 'semicolon from-kind))
-
- (let ((token (delphi-previous-token at-token))
- (token-kind nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond ((delphi-is token-kind delphi-use-clauses)
- (throw 'done t))
-
-
- ((or (delphi-is token-kind '(word comma in newline))
- (delphi-is token-kind delphi-whitespace)
- (delphi-is token-kind delphi-strings)))
-
- ((throw 'done nil)))
- (setq token (delphi-previous-token token)))
- nil))))
- (defun delphi-is-block-after-expr-statement (token)
-
-
- (when (delphi-is (delphi-token-kind token) delphi-block-statements)
- (let ((previous (delphi-previous-token token))
- (previous-kind nil))
- (while (progn
- (setq previous-kind (delphi-token-kind previous))
- (eq previous-kind 'space))
- (setq previous (delphi-previous-token previous)))
- (or (delphi-is previous-kind delphi-expr-delimiters)
- (eq previous-kind 'else)))))
- (defun delphi-previous-indent-of (from-token)
-
- (let ((token (delphi-previous-token from-token))
- (token-kind nil)
- (from-kind (delphi-token-kind from-token))
- (last-colon nil)
- (last-of nil)
- (last-token nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq 'open-group token-kind)
- (throw 'done (delphi-open-group-indent token last-token)))
-
- ((eq 'close-group token-kind) (setq token (delphi-group-start token)))
- ((delphi-is token-kind delphi-end-block-statements)
- (if (eq 'newline (delphi-token-kind (delphi-previous-token token)))
-
-
- (throw 'done 0)
-
- (setq token (delphi-block-start token))))
-
-
-
-
-
-
-
-
-
- ((delphi-is-use-clause-end token last-token last-colon from-kind)
- (throw 'done 0))
-
-
- ((and last-token
- (delphi-is token-kind delphi-previous-terminators)
- (not (delphi-is (delphi-token-kind last-token)
- delphi-directives)))
- (throw 'done (delphi-stmt-line-indent-of last-token 0)))
-
- ((delphi-is token-kind delphi-whitespace))
-
-
-
- ((eq 'of token-kind)
- (setq last-of token))
-
-
-
- ((eq 'colon token-kind)
- (unless last-of (setq last-colon token)))
-
-
- ((eq 'case token-kind)
- (throw 'done
- (if last-colon (delphi-line-indent-of last-colon)
- (delphi-line-indent-of token delphi-case-label-indent))))
-
-
- ((delphi-is token-kind delphi-use-clauses)
- (throw 'done
- (if (eq 'comma from-kind)
- (if last-token
-
- (delphi-indent-of last-token)
-
- (delphi-line-indent-of token delphi-indent-level))
-
- (delphi-line-indent-of token))))
-
- ((eq token-kind 'asm)
- (throw 'done (delphi-stmt-line-indent-of token delphi-indent-level)))
-
-
-
- ((delphi-is token-kind delphi-previous-enclosing-statements)
- (throw 'done (if last-token
-
- (delphi-line-indent-of last-token)
-
- (delphi-line-indent-of token delphi-indent-level))))
-
- ((delphi-composite-type-start token last-token)
- (throw
- 'done
- (if (delphi-is-simple-class-type last-token from-token)
-
- (delphi-line-indent-of token)
-
-
- (delphi-line-indent-of last-token delphi-indent-level))))
-
- ((delphi-is token-kind delphi-previous-statements)
- (throw 'done (delphi-stmt-line-indent-of token 0)))
- )
- (unless (delphi-is token-kind delphi-whitespace)
- (setq last-token token))
- (setq token (delphi-previous-token token)))
-
- 0)))
- (defun delphi-section-indent-of (section-token)
-
-
- (let* ((token (delphi-previous-token section-token))
- (token-kind nil)
- (last-token nil)
- (nested-block-count 0)
- (expr-delimited nil)
- (last-terminator nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq token-kind 'open-group)
- (throw 'done (delphi-open-group-indent token last-token)))
-
- ((eq 'close-group token-kind) (setq token (delphi-group-start token)))
- ((delphi-is token-kind delphi-end-block-statements)
- (if (eq 'newline (delphi-token-kind (delphi-previous-token token)))
-
-
- (throw 'done 0)
-
- (setq token (delphi-block-start token)
- nested-block-count (1+ nested-block-count))))
-
- ((eq 'forward token-kind)
- (setq nested-block-count (1+ nested-block-count)))
-
- ((and (delphi-is token-kind delphi-routine-statements)
- (> nested-block-count 0))
- (setq nested-block-count (1- nested-block-count)))
-
- ((eq 'semicolon token-kind) (setq last-terminator token))
-
- ((delphi-is token-kind delphi-expr-delimiters)
- (setq expr-delimited token))
-
-
- ((and (not last-terminator)
- (delphi-is token-kind delphi-body-statements))
- (throw 'done
- (delphi-stmt-line-indent-of token delphi-compound-block-indent)))
-
- ((and (eq 'colon token-kind)
- (delphi-is (delphi-token-kind section-token)
- delphi-block-statements)
- (not last-terminator)
- (not expr-delimited)
- (not (eq 'equals (delphi-token-kind last-token))))
- (throw 'done
- (delphi-stmt-line-indent-of token delphi-indent-level)))
-
- ((delphi-is token-kind delphi-begin-enclosing-tokens)
- (throw 'done
- (delphi-stmt-line-indent-of token delphi-indent-level)))
-
-
- ((and (delphi-is token-kind delphi-decl-delimiters)
- (= 0 nested-block-count))
- (throw 'done (delphi-line-indent-of token 0)))
-
- ((delphi-is token-kind delphi-unit-statements) (throw 'done 0))
- )
- (unless (delphi-is token-kind delphi-whitespace)
- (setq last-token token))
- (setq token (delphi-previous-token token)))
-
- 0)))
- (defun delphi-enclosing-indent-of (from-token)
-
- (let ((token (delphi-previous-token from-token))
- (from-kind (delphi-token-kind from-token))
- (token-kind nil)
- (stmt-start nil)
- (last-token nil)
- (equals-encountered nil)
- (before-equals nil)
- (expr-delimited nil))
- (catch 'done
- (while token
- (setq token-kind (delphi-token-kind token))
- (cond
-
- ((eq 'open-group token-kind)
- (throw 'done
- (delphi-open-group-indent
- token last-token
- (if (delphi-is from-kind delphi-binary-ops)
-
- 0
- delphi-indent-level))))
-
- ((eq 'close-group token-kind) (setq token (delphi-group-start token)))
-
- ((delphi-is token-kind delphi-end-block-statements)
- (setq token (delphi-block-start token)))
-
-
-
-
- ((delphi-is token-kind delphi-expr-delimiters)
- (setq expr-delimited token stmt-start last-token))
-
-
- ((and (not expr-delimited)
- (delphi-is token-kind delphi-expr-statements))
- (throw 'done
- (cond ((delphi-is from-kind delphi-expr-delimiters)
-
- (delphi-stmt-line-indent-of token 0))
- ((and last-token (delphi-is from-kind delphi-binary-ops))
-
- (delphi-indent-of last-token))
- (last-token
-
- (delphi-indent-of last-token delphi-indent-level))
-
- ((delphi-indent-of token delphi-indent-level)))))
-
-
- ((eq 'case token-kind)
- (throw 'done
- (if stmt-start
-
-
- (delphi-stmt-line-indent-of
- stmt-start delphi-indent-level)
-
- (delphi-stmt-line-indent-of
- token delphi-case-label-indent))))
-
-
-
- ((delphi-is token-kind delphi-body-expr-statements)
- (throw 'done
- (delphi-stmt-line-indent-of
- (or stmt-start token) delphi-indent-level)))
-
-
- ((eq 'else token-kind)
- (throw 'done (delphi-stmt-line-indent-of
- (or last-token token) delphi-indent-level)))
-
- ((delphi-is token-kind delphi-decl-sections)
- (throw 'done (delphi-indent-of (if last-token last-token token)
- delphi-indent-level)))
-
- ((delphi-is token-kind delphi-unit-sections)
- (throw 'done
-
-
- (if (delphi-is token-kind delphi-interface-types)
- (progn
-
- (while (progn
- (setq last-token token
- token (delphi-previous-token token)
- token-kind (delphi-token-kind token))
- (and token
- (delphi-is token-kind
- delphi-whitespace))))
-
-
-
-
- (if (and token (eq token-kind 'equals))
- (delphi-line-indent-of last-token
- delphi-indent-level)
- 0))
- 0)))
-
- ((delphi-is token-kind delphi-previous-terminators)
- (throw 'done
- (cond ((and last-token
- (eq 'comma token-kind)
- (delphi-is from-kind delphi-binary-ops))
-
- (delphi-indent-of last-token))
- (last-token
-
- (delphi-indent-of last-token delphi-indent-level))
-
-
- ((delphi-previous-indent-of token)))))
-
-
-
-
-
-
-
- ((delphi-is-block-after-expr-statement token)
- (throw 'done
- (cond (last-token (delphi-indent-of last-token delphi-indent-level))
- ((+ (delphi-section-indent-of token) delphi-indent-level)))))
-
- ((eq token-kind 'asm)
- (throw 'done (delphi-stmt-line-indent-of token delphi-indent-level)))
-
- ((delphi-is token-kind delphi-enclosing-statements)
- (throw 'done (delphi-stmt-line-indent-of
- (or last-token token) delphi-indent-level)))
-
- ((delphi-composite-type-start token last-token)
- (throw 'done
- (delphi-line-indent-of last-token delphi-indent-level)))
-
-
- ((and (eq token-kind 'colon)
- (not expr-delimited)
- (not (delphi-is from-kind delphi-expr-delimiters))
- (not equals-encountered)
- (not (eq from-kind 'equals)))
- (throw 'done
- (if last-token
- (delphi-indent-of last-token delphi-indent-level)
- (delphi-line-indent-of token delphi-indent-level 'semicolon))))
-
-
- ((and (eq token-kind 'colon) equals-encountered before-equals)
- (cond
-
-
-
-
-
-
-
-
-
-
-
-
- ((eq (delphi-token-kind last-token) 'equals))
-
- ((throw 'done
- (delphi-indent-of before-equals delphi-indent-level)))))
-
- ((eq token-kind 'equals)
- (setq equals-encountered token
- before-equals last-token))
- )
- (unless (delphi-is token-kind delphi-whitespace)
- (setq last-token token))
- (setq token (delphi-previous-token token)))
-
- 0)))
- (defun delphi-corrected-indentation ()
-
- (delphi-save-excursion
- (delphi-progress-start)
-
- (beginning-of-line)
- (skip-chars-forward delphi-space-chars)
- (let* ((token (delphi-current-token))
- (token-kind (delphi-token-kind token))
- (indent
- (cond ((eq 'close-group token-kind)
-
- (delphi-indent-of (delphi-group-start token)))
- ((delphi-is token-kind delphi-unit-statements) 0)
- ((delphi-is token-kind delphi-comments)
-
- (delphi-comment-indent-of token))
- ((delphi-is token-kind delphi-decl-matchers)
-
- (delphi-section-indent-of token))
- ((delphi-is token-kind delphi-match-block-statements)
-
- (let ((block-start
- (delphi-block-start token 'stop-on-class)))
- (cond
-
-
- ((delphi-is-block-after-expr-statement block-start)
- (delphi-section-indent-of block-start))
-
- ((delphi-stmt-line-indent-of block-start 0)))))
- ((eq 'else token-kind)
-
- (delphi-stmt-line-indent-of (delphi-else-start token) 0))
-
- ((delphi-enclosing-indent-of
- (if token token (delphi-token-at (1- (point)))))))))
- (delphi-progress-done)
- indent)))
- (defun delphi-indent-line ()
- "Indent the current line according to the current language construct.
- If before the indent, the point is moved to the indent."
- (interactive)
- (delphi-save-match-data
- (let ((marked-point (point-marker))
- (line-start nil)
- (old-indent 0)
- (new-indent 0))
- (beginning-of-line)
- (setq line-start (point))
- (skip-chars-forward delphi-space-chars)
- (setq old-indent (current-column))
- (setq new-indent (delphi-corrected-indentation))
- (if (< marked-point (point))
-
- (set-marker marked-point (point)))
-
- (set-marker-insertion-type marked-point t)
- (when (/= old-indent new-indent)
- (delete-region line-start (point))
- (insert (make-string new-indent ?\s)))
- (goto-char marked-point)
- (set-marker marked-point nil))))
- (defvar delphi-mode-abbrev-table nil
- "Abbrev table in use in Delphi mode buffers.")
- (define-abbrev-table 'delphi-mode-abbrev-table ())
- (defmacro delphi-ensure-buffer (buffer-var buffer-name)
-
-
- `(when (not (buffer-live-p ,buffer-var))
- (setq ,buffer-var (get-buffer-create ,buffer-name))))
- (defun delphi-log-msg (to-buffer the-msg)
-
- (with-current-buffer to-buffer
- (save-selected-window
- (switch-to-buffer-other-window to-buffer)
- (goto-char (point-max))
- (set-window-point (get-buffer-window to-buffer) (point))
- (insert the-msg))))
- (defvar delphi-debug-buffer nil
- "Buffer to write Delphi mode debug messages to. Created on demand.")
- (defun delphi-debug-log (format-string &rest args)
-
- (when delphi-debug
- (delphi-ensure-buffer delphi-debug-buffer "*Delphi Debug Log*")
- (delphi-log-msg delphi-debug-buffer
- (concat (format-time-string "%H:%M:%S " (current-time))
- (apply #'format (cons format-string args))
- "\n"))))
- (defun delphi-debug-token-string (token)
- (let* ((image (delphi-token-string token))
- (has-newline (string-match "^\\([^\n]*\\)\n\\(.+\\)?$" image)))
- (when has-newline
- (setq image (concat (match-string 1 image)
- (if (match-beginning 2) "..."))))
- image))
- (defun delphi-debug-show-current-token ()
- (interactive)
- (let ((token (delphi-current-token)))
- (delphi-debug-log "Token: %S %S" token (delphi-debug-token-string token))))
- (defun delphi-debug-goto-point (p)
- (interactive "NGoto char: ")
- (goto-char p))
- (defun delphi-debug-goto-next-token ()
- (interactive)
- (goto-char (delphi-token-start (delphi-next-token (delphi-current-token)))))
- (defun delphi-debug-goto-previous-token ()
- (interactive)
- (goto-char
- (delphi-token-start (delphi-previous-token (delphi-current-token)))))
- (defun delphi-debug-show-current-string (from to)
- (interactive "r")
- (delphi-debug-log "String: %S" (buffer-substring from to)))
- (defun delphi-debug-show-is-stable ()
- (interactive)
- (delphi-debug-log "stable: %S prev: %S next: %S"
- (delphi-is-stable-literal (point))
- (delphi-literal-kind (1- (point)))
- (delphi-literal-kind (point))))
- (defun delphi-debug-unparse-buffer ()
- (interactive)
- (delphi-set-text-properties (point-min) (point-max) nil))
- (defun delphi-debug-parse-region (from to)
- (interactive "r")
- (let ((delphi-verbose t))
- (delphi-save-excursion
- (delphi-progress-start)
- (delphi-parse-region from to)
- (delphi-progress-done "Parsing done"))))
- (defun delphi-debug-parse-window ()
- (interactive)
- (delphi-debug-parse-region (window-start) (window-end)))
- (defun delphi-debug-parse-buffer ()
- (interactive)
- (delphi-debug-parse-region (point-min) (point-max)))
- (defun delphi-debug-fontify-window ()
- (interactive)
- (delphi-fontify-region (window-start) (window-end) t))
- (defun delphi-debug-fontify-buffer ()
- (interactive)
- (delphi-fontify-region (point-min) (point-max) t))
- (defun delphi-debug-tokenize-region (from to)
- (interactive)
- (delphi-save-excursion
- (delphi-progress-start)
- (goto-char from)
- (while (< (point) to)
- (goto-char (delphi-token-end (delphi-current-token)))
- (delphi-step-progress (point) "Tokenizing" delphi-scanning-progress-step))
- (delphi-progress-done "Tokenizing done")))
- (defun delphi-debug-tokenize-buffer ()
- (interactive)
- (delphi-debug-tokenize-region (point-min) (point-max)))
- (defun delphi-debug-tokenize-window ()
- (interactive)
- (delphi-debug-tokenize-region (window-start) (window-end)))
- (defun delphi-newline ()
- "Terminate the current line with a newline and indent the next, unless
- `delphi-newline-always-indents' is nil, in which case no reindenting occurs."
- (interactive)
-
- (delete-horizontal-space)
- (newline)
- (when delphi-newline-always-indents
-
- (save-excursion
- (forward-line -1)
- (delphi-indent-line))
- (delphi-indent-line)))
- (defun delphi-tab ()
- "Indent the region, when Transient Mark mode is enabled and the region is
- active. Otherwise, indent the current line or insert a TAB, depending on the
- value of `delphi-tab-always-indents' and the current line position."
- (interactive)
- (cond ((use-region-p)
-
-
- (indent-region (region-beginning) (region-end)))
- ((or delphi-tab-always-indents
- (save-excursion (skip-chars-backward delphi-space-chars) (bolp)))
-
-
-
- (delphi-indent-line))
- (t
-
- (insert "\t"))))
- (defun delphi-is-directory (path)
-
- (let ((attributes (file-attributes path)))
- (and attributes (car attributes))))
- (defun delphi-is-file (path)
-
- (let ((attributes (file-attributes path)))
- (and attributes (null (car attributes)))))
- (defun delphi-search-directory (unit dir &optional recurse)
-
-
-
- (when (delphi-is-directory dir)
- (let ((files (directory-files dir))
- (unit-file (downcase unit)))
- (catch 'done
-
- (mapc #'(lambda (file)
- (let ((path (concat dir "/" file)))
- (if (and (string= unit-file (downcase file))
- (delphi-is-file path))
- (throw 'done path))))
- files)
-
- (when recurse
- (mapc #'(lambda (subdir)
- (unless (member subdir '("." ".."))
- (let ((path (delphi-search-directory
- unit (concat dir "/" subdir) recurse)))
- (if path (throw 'done path)))))
- files))
-
- nil))))
- (defun delphi-find-unit-in-directory (unit dir)
-
-
- (let ((dir-name dir)
- (recurse nil))
-
- (if (string-match "^\\(.+\\)\\.\\.\\.$" dir-name)
- (setq dir-name (match-string 1 dir-name)
- recurse t))
-
- (if (string-match "^\\(.+\\)[\\\\/]$" dir-name)
- (setq dir-name (match-string 1 dir-name)))
- (delphi-search-directory unit dir-name recurse)))
- (defun delphi-find-unit-file (unit)
-
-
- (catch 'done
- (cond ((null delphi-search-path)
- (delphi-find-unit-in-directory unit "."))
- ((stringp delphi-search-path)
- (delphi-find-unit-in-directory unit delphi-search-path))
- ((mapc
- #'(lambda (dir)
- (let ((file (delphi-find-unit-in-directory unit dir)))
- (if file (throw 'done file))))
- delphi-search-path)))
- nil))
- (defun delphi-find-unit (unit)
- "Find the specified Delphi source file according to `delphi-search-path'.
- If no extension is specified, .pas is assumed. Creates a buffer for the unit."
- (interactive "sDelphi unit name: ")
- (let* ((unit-file (if (string-match "^\\(.*\\)\\.[a-z]+$" unit)
- unit
- (concat unit ".pas")))
- (file (delphi-find-unit-file unit-file)))
- (if (null file)
- (error "unit not found: %s" unit-file)
- (find-file file)
- (if (not (derived-mode-p 'delphi-mode))
- (delphi-mode)))
- file))
- (defun delphi-find-current-def ()
- "Find the definition of the identifier under the current point."
- (interactive)
- (error "delphi-find-current-def: not implemented yet"))
- (defun delphi-find-current-xdef ()
- "Find the definition of the identifier under the current point, searching
- in external units if necessary (as listed in the current unit's use clause).
- The set of directories to search for a unit is specified by the global variable
- `delphi-search-path'."
- (interactive)
- (error "delphi-find-current-xdef: not implemented yet"))
- (defun delphi-find-current-body ()
- "Find the body of the identifier under the current point, assuming
- it is a routine."
- (interactive)
- (error "delphi-find-current-body: not implemented yet"))
- (defun delphi-fill-comment ()
- "Fill the text of the current comment, according to `fill-column'.
- An error is raised if not in a comment."
- (interactive)
- (save-excursion
- (save-restriction
- (let* (( (delphi-current-token))
- (-kind (delphi-token-kind comment)))
- (if (not (delphi-is comment-kind delphi-comments))
- (error "Not in a comment")
- (let* ((start-comment (delphi-comment-block-start comment))
- (end-comment (delphi-comment-block-end comment))
- (-start (delphi-token-start start-comment))
- (-end (delphi-token-end end-comment))
- (content-start (delphi-comment-content-start start-comment))
- (content-indent (delphi-column-of content-start))
- (content-prefix (make-string content-indent ?\s))
- (content-prefix-re delphi-leading-spaces-re)
- (p nil)
- (marked-point (point-marker)))
- (when (eq 'comment-single-line comment-kind)
-
- (setq content-prefix
- (let ((-indent (delphi-column-of comment-start)))
- (concat (make-string comment-indent ?\s) "//"
- (make-string (- content-indent comment-indent 2)
- ?\s)))
- content-prefix-re (concat delphi-leading-spaces-re
- "//"
- delphi-spaces-re)
- comment-end (if (delphi-is-literal-end comment-end)
-
- (1- comment-end)
- comment-end)))
-
- (set-marker-insertion-type marked-point t)
-
- (goto-char content-start)
- (insert " ")
- (delete-char -1)
- (narrow-to-region content-start comment-end)
-
- (setq p (point-min))
- (while (when (< p (point-max))
- (goto-char p)
- (re-search-forward content-prefix-re nil t))
- (replace-match "" nil nil)
- (setq p (1+ (point))))
-
- (goto-char (point-max))
- (insert "\n")
-
- (let ((fill-column (- fill-column content-indent)))
- (fill-region (point-min) (point-max)))
- (goto-char (point-max))
- (delete-char -1)
-
- (goto-char (point-min))
- (end-of-line)
- (setq p (point))
- (while (when (< p (point-max))
- (goto-char p)
- (re-search-forward "^" nil t))
- (replace-match content-prefix nil nil)
- (setq p (1+ (point))))
- (setq comment-end (point-max))
- (widen)
-
- (goto-char marked-point)
- (set-marker marked-point nil)
-
- (delphi-progress-start)
- (delphi-parse-region comment-start comment-end)
- (delphi-progress-done)))))))
- (defun delphi-new-comment-line ()
- "If in a // comment, do a newline, indented such that one is still in the
- comment block. If not in a // comment, just does a normal newline."
- (interactive)
- (let (( (delphi-current-token)))
- (if (not (eq 'comment-single-line (delphi-token-kind comment)))
-
- (delphi-newline)
- (let* ((start-comment (delphi-comment-block-start comment))
- (-start (delphi-token-start start-comment))
- (content-start (delphi-comment-content-start start-comment))
- (prefix
- (concat (make-string (delphi-column-of comment-start) ?\s) "//"
- (make-string (- content-start comment-start 2) ?\s))))
- (delete-horizontal-space)
- (newline)
- (insert prefix)))))
- (defun delphi-match-token (token limit)
-
-
- (set-match-data nil)
- (if token
- (let ((end (min (delphi-token-end token) limit)))
- (set-match-data (list (delphi-token-start token) end))
- (goto-char end)
- token)))
- (defconst delphi-font-lock-defaults
- '(nil
- t
- nil
- nil
- nil
- (font-lock-fontify-region-function . delphi-fontify-region)
- (font-lock-verbose . delphi-fontifying-progress-step))
- "Delphi mode font-lock defaults. Syntactic fontification is ignored.")
- (defvar delphi-debug-mode-map
- (let ((kmap (make-sparse-keymap)))
- (mapc #'(lambda (binding) (define-key kmap (car binding) (cadr binding)))
- '(("n" delphi-debug-goto-next-token)
- ("p" delphi-debug-goto-previous-token)
- ("t" delphi-debug-show-current-token)
- ("T" delphi-debug-tokenize-buffer)
- ("W" delphi-debug-tokenize-window)
- ("g" delphi-debug-goto-point)
- ("s" delphi-debug-show-current-string)
- ("a" delphi-debug-parse-buffer)
- ("w" delphi-debug-parse-window)
- ("f" delphi-debug-fontify-window)
- ("F" delphi-debug-fontify-buffer)
- ("r" delphi-debug-parse-region)
- ("c" delphi-debug-unparse-buffer)
- ("x" delphi-debug-show-is-stable)
- ))
- kmap)
- "Keystrokes for Delphi mode debug commands.")
- (defvar delphi-mode-map
- (let ((kmap (make-sparse-keymap)))
- (mapc #'(lambda (binding) (define-key kmap (car binding) (cadr binding)))
- (list '("\r" delphi-newline)
- '("\t" delphi-tab)
- '("\177" backward-delete-char-untabify)
- '("\C-cu" delphi-find-unit)
- '("\M-q" delphi-fill-comment)
- '("\M-j" delphi-new-comment-line)
-
- (list "\C-c\C-d" delphi-debug-mode-map)))
- kmap)
- "Keymap used in Delphi mode.")
- (define-derived-mode delphi-mode prog-mode "Delphi"
- "Major mode for editing Delphi code. \\<delphi-mode-map>
- \\[delphi-tab]\t- Indents the current line (or region, if Transient Mark mode
- \t is enabled and the region is active) of Delphi code.
- \\[delphi-find-unit]\t- Search for a Delphi source file.
- \\[delphi-fill-comment]\t- Fill the current comment.
- \\[delphi-new-comment-line]\t- If in a // comment, do a new comment line.
- \\[indent-region] also works for indenting a whole region.
- Customization:
- `delphi-indent-level' (default 3)
- Indentation of Delphi statements with respect to containing block.
- `delphi-compound-block-indent' (default 0)
- Extra indentation for blocks in compound statements.
- `delphi-case-label-indent' (default 0)
- Extra indentation for case statement labels.
- `delphi-tab-always-indents' (default t)
- Non-nil means TAB in Delphi mode should always reindent the current line,
- regardless of where in the line point is when the TAB command is used.
- `delphi-newline-always-indents' (default t)
- Non-nil means NEWLINE in Delphi mode should always reindent the current
- line, insert a blank line and move to the default indent column of the
- blank line.
- `delphi-search-path' (default .)
- Directories to search when finding external units.
- `delphi-verbose' (default nil)
- If true then Delphi token processing progress is reported to the user.
- Coloring:
- `delphi-comment-face' (default font-lock-comment-face)
- Face used to color Delphi comments.
- `delphi-string-face' (default font-lock-string-face)
- Face used to color Delphi strings.
- `delphi-keyword-face' (default font-lock-keyword-face)
- Face used to color Delphi keywords.
- `delphi-other-face' (default nil)
- Face used to color everything else.
- Turning on Delphi mode calls the value of the variable `delphi-mode-hook'
- with no args, if that value is non-nil."
-
- (mapc #'(lambda (var)
- (let ((var-symb (car var))
- (var-val (cadr var)))
- (set (make-local-variable var-symb) var-val)))
- (list '(indent-line-function delphi-indent-line)
- '(-indent-function delphi-indent-line)
- '(case-fold-search t)
- '(delphi-progress-last-reported-point nil)
- '(delphi-ignore-changes nil)
- (list 'font-lock-defaults delphi-font-lock-defaults)))
-
-
- (add-hook 'after-change-functions 'delphi-after-change nil t)
- (widen)
- (delphi-save-excursion
- (let ((delphi-verbose t))
- (delphi-progress-start)
- (delphi-parse-region (point-min) (point-max))
- (delphi-progress-done)))
- (run-mode-hooks 'delphi-mode-hook))
|