syntax.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. ;;; syntax.el --- helper functions to find syntactic context
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: internal
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; The main exported function is `syntax-ppss'. You might also need
  18. ;; to call `syntax-ppss-flush-cache' or to add it to
  19. ;; before-change-functions'(although this is automatically done by
  20. ;; syntax-ppss when needed, but that might fail if syntax-ppss is
  21. ;; called in a context where before-change-functions is temporarily
  22. ;; let-bound to nil).
  23. ;;; Todo:
  24. ;; - do something about the case where the syntax-table is changed.
  25. ;; This typically happens with tex-mode and its `$' operator.
  26. ;; - new functions `syntax-state', ... to replace uses of parse-partial-state
  27. ;; with something higher-level (similar to syntax-ppss-context).
  28. ;; - interaction with mmm-mode.
  29. ;;; Code:
  30. ;; Note: PPSS stands for `parse-partial-sexp state'
  31. (eval-when-compile (require 'cl))
  32. (defvar font-lock-beginning-of-syntax-function)
  33. ;;; Applying syntax-table properties where needed.
  34. (defvar syntax-propertize-function nil
  35. ;; Rather than a -functions hook, this is a -function because it's easier
  36. ;; to do a single scan than several scans: with multiple scans, one cannot
  37. ;; assume that the text before point has been propertized, so syntax-ppss
  38. ;; gives unreliable results (and stores them in its cache to boot, so we'd
  39. ;; have to flush that cache between each function, and we couldn't use
  40. ;; syntax-ppss-flush-cache since that would not only flush the cache but also
  41. ;; reset syntax-propertize--done which should not be done in this case).
  42. "Mode-specific function to apply the syntax-table properties.
  43. Called with two arguments: START and END.
  44. This function can call `syntax-ppss' on any position before END, but it
  45. should not call `syntax-ppss-flush-cache', which means that it should not
  46. call `syntax-ppss' on some position and later modify the buffer on some
  47. earlier position.")
  48. (defvar syntax-propertize-chunk-size 500)
  49. (defvar syntax-propertize-extend-region-functions
  50. '(syntax-propertize-wholelines)
  51. "Special hook run just before proceeding to propertize a region.
  52. This is used to allow major modes to help `syntax-propertize' find safe buffer
  53. positions as beginning and end of the propertized region. Its most common use
  54. is to solve the problem of /identification/ of multiline elements by providing
  55. a function that tries to find such elements and move the boundaries such that
  56. they do not fall in the middle of one.
  57. Each function is called with two arguments (START and END) and it should return
  58. either a cons (NEW-START . NEW-END) or nil if no adjustment should be made.
  59. These functions are run in turn repeatedly until they all return nil.
  60. Put first the functions more likely to cause a change and cheaper to compute.")
  61. ;; Mark it as a special hook which doesn't use any global setting
  62. ;; (i.e. doesn't obey the element t in the buffer-local value).
  63. (make-variable-buffer-local 'syntax-propertize-extend-region-functions)
  64. (defun syntax-propertize-wholelines (start end)
  65. (goto-char start)
  66. (cons (line-beginning-position)
  67. (progn (goto-char end)
  68. (if (bolp) (point) (line-beginning-position 2)))))
  69. (defun syntax-propertize-multiline (beg end)
  70. "Let `syntax-propertize' pay attention to the syntax-multiline property."
  71. (when (and (> beg (point-min))
  72. (get-text-property (1- beg) 'syntax-multiline))
  73. (setq beg (or (previous-single-property-change beg 'syntax-multiline)
  74. (point-min))))
  75. ;;
  76. (when (get-text-property end 'font-lock-multiline)
  77. (setq end (or (text-property-any end (point-max)
  78. 'syntax-multiline nil)
  79. (point-max))))
  80. (cons beg end))
  81. (defvar syntax-propertize--done -1
  82. "Position up to which syntax-table properties have been set.")
  83. (make-variable-buffer-local 'syntax-propertize--done)
  84. (defun syntax-propertize--shift-groups (re n)
  85. (replace-regexp-in-string
  86. "\\\\(\\?\\([0-9]+\\):"
  87. (lambda (s)
  88. (replace-match
  89. (number-to-string (+ n (string-to-number (match-string 1 s))))
  90. t t s 1))
  91. re t t))
  92. (defmacro syntax-propertize-precompile-rules (&rest rules)
  93. "Return a precompiled form of RULES to pass to `syntax-propertize-rules'.
  94. The arg RULES can be of the same form as in `syntax-propertize-rules'.
  95. The return value is an object that can be passed as a rule to
  96. `syntax-propertize-rules'.
  97. I.e. this is useful only when you want to share rules among several
  98. syntax-propertize-functions."
  99. (declare (debug syntax-propertize-rules))
  100. ;; Precompile? Yeah, right!
  101. ;; Seriously, tho, this is a macro for 2 reasons:
  102. ;; - we could indeed do some pre-compilation at some point in the future,
  103. ;; e.g. fi/when we switch to a DFA-based implementation of
  104. ;; syntax-propertize-rules.
  105. ;; - this lets Edebug properly annotate the expressions inside RULES.
  106. `',rules)
  107. (defmacro syntax-propertize-rules (&rest rules)
  108. "Make a function that applies RULES for use in `syntax-propertize-function'.
  109. The function will scan the buffer, applying the rules where they match.
  110. The buffer is scanned a single time, like \"lex\" would, rather than once
  111. per rule.
  112. Each RULE can be a symbol, in which case that symbol's value should be,
  113. at macro-expansion time, a precompiled set of rules, as returned
  114. by `syntax-propertize-precompile-rules'.
  115. Otherwise, RULE should have the form (REGEXP HIGHLIGHT1 ... HIGHLIGHTn), where
  116. REGEXP is an expression (evaluated at time of macro-expansion) that returns
  117. a regexp, and where HIGHLIGHTs have the form (NUMBER SYNTAX) which means to
  118. apply the property SYNTAX to the chars matched by the subgroup NUMBER
  119. of the regular expression, if NUMBER did match.
  120. SYNTAX is an expression that returns a value to apply as `syntax-table'
  121. property. Some expressions are handled specially:
  122. - if SYNTAX is a string, then it is converted with `string-to-syntax';
  123. - if SYNTAX has the form (prog1 EXP . EXPS) then the value returned by EXP
  124. will be applied to the buffer before running EXPS and if EXP is a string it
  125. is also converted with `string-to-syntax'.
  126. The SYNTAX expression is responsible to save the `match-data' if needed
  127. for subsequent HIGHLIGHTs.
  128. Also SYNTAX is free to move point, in which case RULES may not be applied to
  129. some parts of the text or may be applied several times to other parts.
  130. Note: back-references in REGEXPs do not work."
  131. (declare (debug (&rest &or symbolp ;FIXME: edebug this eval step.
  132. (form &rest
  133. (numberp
  134. [&or stringp ;FIXME: Use &wrap
  135. ("prog1" [&or stringp def-form] def-body)
  136. def-form])))))
  137. (let ((newrules nil))
  138. (while rules
  139. (if (symbolp (car rules))
  140. (setq rules (append (symbol-value (pop rules)) rules))
  141. (push (pop rules) newrules)))
  142. (setq rules (nreverse newrules)))
  143. (let* ((offset 0)
  144. (branches '())
  145. ;; We'd like to use a real DFA-based lexer, usually, but since Emacs
  146. ;; doesn't have one yet, we fallback on building one large regexp
  147. ;; and use groups to determine which branch of the regexp matched.
  148. (re
  149. (mapconcat
  150. (lambda (rule)
  151. (let* ((orig-re (eval (car rule)))
  152. (re orig-re))
  153. (when (and (assq 0 rule) (cdr rules))
  154. ;; If there's more than 1 rule, and the rule want to apply
  155. ;; highlight to match 0, create an extra group to be able to
  156. ;; tell when *this* match 0 has succeeded.
  157. (incf offset)
  158. (setq re (concat "\\(" re "\\)")))
  159. (setq re (syntax-propertize--shift-groups re offset))
  160. (let ((code '())
  161. (condition
  162. (cond
  163. ((assq 0 rule) (if (zerop offset) t
  164. `(match-beginning ,offset)))
  165. ((null (cddr rule))
  166. `(match-beginning ,(+ offset (car (cadr rule)))))
  167. (t
  168. `(or ,@(mapcar
  169. (lambda (case)
  170. `(match-beginning ,(+ offset (car case))))
  171. (cdr rule))))))
  172. (nocode t)
  173. (offset offset))
  174. ;; If some of the subgroup rules include Elisp code, then we
  175. ;; need to set the match-data so it's consistent with what the
  176. ;; code expects. If not, then we can simply use shifted
  177. ;; offset in our own code.
  178. (unless (zerop offset)
  179. (dolist (case (cdr rule))
  180. (unless (stringp (cadr case))
  181. (setq nocode nil)))
  182. (unless nocode
  183. (push `(let ((md (match-data 'ints)))
  184. ;; Keep match 0 as is, but shift everything else.
  185. (setcdr (cdr md) (nthcdr ,(* (1+ offset) 2) md))
  186. (set-match-data md))
  187. code)
  188. (setq offset 0)))
  189. ;; Now construct the code for each subgroup rules.
  190. (dolist (case (cdr rule))
  191. (assert (null (cddr case)))
  192. (let* ((gn (+ offset (car case)))
  193. (action (nth 1 case))
  194. (thiscode
  195. (cond
  196. ((stringp action)
  197. `((put-text-property
  198. (match-beginning ,gn) (match-end ,gn)
  199. 'syntax-table
  200. ',(string-to-syntax action))))
  201. ((eq (car-safe action) 'ignore)
  202. (cdr action))
  203. ((eq (car-safe action) 'prog1)
  204. (if (stringp (nth 1 action))
  205. `((put-text-property
  206. (match-beginning ,gn) (match-end ,gn)
  207. 'syntax-table
  208. ',(string-to-syntax (nth 1 action)))
  209. ,@(nthcdr 2 action))
  210. `((let ((mb (match-beginning ,gn))
  211. (me (match-end ,gn))
  212. (syntax ,(nth 1 action)))
  213. (if syntax
  214. (put-text-property
  215. mb me 'syntax-table syntax))
  216. ,@(nthcdr 2 action)))))
  217. (t
  218. `((let ((mb (match-beginning ,gn))
  219. (me (match-end ,gn))
  220. (syntax ,action))
  221. (if syntax
  222. (put-text-property
  223. mb me 'syntax-table syntax))))))))
  224. (if (or (not (cddr rule)) (zerop gn))
  225. (setq code (nconc (nreverse thiscode) code))
  226. (push `(if (match-beginning ,gn)
  227. ;; Try and generate clean code with no
  228. ;; extraneous progn.
  229. ,(if (null (cdr thiscode))
  230. (car thiscode)
  231. `(progn ,@thiscode)))
  232. code))))
  233. (push (cons condition (nreverse code))
  234. branches))
  235. (incf offset (regexp-opt-depth orig-re))
  236. re))
  237. rules
  238. "\\|")))
  239. `(lambda (start end)
  240. (goto-char start)
  241. (while (and (< (point) end)
  242. (re-search-forward ,re end t))
  243. (cond ,@(nreverse branches))))))
  244. (defun syntax-propertize-via-font-lock (keywords)
  245. "Propertize for syntax in START..END using font-lock syntax.
  246. KEYWORDS obeys the format used in `font-lock-syntactic-keywords'.
  247. The return value is a function suitable for `syntax-propertize-function'."
  248. (lexical-let ((keywords keywords))
  249. (lambda (start end)
  250. (with-no-warnings
  251. (let ((font-lock-syntactic-keywords keywords))
  252. (font-lock-fontify-syntactic-keywords-region start end)
  253. ;; In case it was eval'd/compiled.
  254. (setq keywords font-lock-syntactic-keywords))))))
  255. (defun syntax-propertize (pos)
  256. "Ensure that syntax-table properties are set until POS."
  257. (when (and syntax-propertize-function
  258. (< syntax-propertize--done pos))
  259. ;; (message "Needs to syntax-propertize from %s to %s"
  260. ;; syntax-propertize--done pos)
  261. (set (make-local-variable 'parse-sexp-lookup-properties) t)
  262. (save-excursion
  263. (with-silent-modifications
  264. (let* ((start (max syntax-propertize--done (point-min)))
  265. (end (max pos
  266. (min (point-max)
  267. (+ start syntax-propertize-chunk-size))))
  268. (funs syntax-propertize-extend-region-functions))
  269. (while funs
  270. (let ((new (funcall (pop funs) start end)))
  271. (if (or (null new)
  272. (and (>= (car new) start) (<= (cdr new) end)))
  273. nil
  274. (setq start (car new))
  275. (setq end (cdr new))
  276. ;; If there's been a change, we should go through the
  277. ;; list again since this new position may
  278. ;; warrant a different answer from one of the funs we've
  279. ;; already seen.
  280. (unless (eq funs
  281. (cdr syntax-propertize-extend-region-functions))
  282. (setq funs syntax-propertize-extend-region-functions)))))
  283. ;; Move the limit before calling the function, so the function
  284. ;; can use syntax-ppss.
  285. (setq syntax-propertize--done end)
  286. ;; (message "syntax-propertizing from %s to %s" start end)
  287. (remove-text-properties start end
  288. '(syntax-table nil syntax-multiline nil))
  289. (funcall syntax-propertize-function start end))))))
  290. ;;; Incrementally compute and memoize parser state.
  291. (defsubst syntax-ppss-depth (ppss)
  292. (nth 0 ppss))
  293. (defun syntax-ppss-toplevel-pos (ppss)
  294. "Get the latest syntactically outermost position found in a syntactic scan.
  295. PPSS is a scan state, as returned by `parse-partial-sexp' or `syntax-ppss'.
  296. An \"outermost position\" means one that it is outside of any syntactic entity:
  297. outside of any parentheses, comments, or strings encountered in the scan.
  298. If no such position is recorded in PPSS (because the end of the scan was
  299. itself at the outermost level), return nil."
  300. ;; BEWARE! We rely on the undocumented 9th field. The 9th field currently
  301. ;; contains the list of positions of the enclosing open-parens.
  302. ;; I.e. those positions are outside of any string/comment and the first of
  303. ;; those is outside of any paren (i.e. corresponds to a nil ppss).
  304. ;; If this list is empty but we are in a string or comment, then the 8th
  305. ;; field contains a similar "toplevel" position.
  306. (or (car (nth 9 ppss))
  307. (nth 8 ppss)))
  308. (defsubst syntax-ppss-context (ppss)
  309. (cond
  310. ((nth 3 ppss) 'string)
  311. ((nth 4 ppss) 'comment)
  312. (t nil)))
  313. (defvar syntax-ppss-max-span 20000
  314. "Threshold below which cache info is deemed unnecessary.
  315. We try to make sure that cache entries are at least this far apart
  316. from each other, to avoid keeping too much useless info.")
  317. (defvar syntax-begin-function nil
  318. "Function to move back outside of any comment/string/paren.
  319. This function should move the cursor back to some syntactically safe
  320. point (where the PPSS is equivalent to nil).")
  321. (defvar syntax-ppss-cache nil
  322. "List of (POS . PPSS) pairs, in decreasing POS order.")
  323. (make-variable-buffer-local 'syntax-ppss-cache)
  324. (defvar syntax-ppss-last nil
  325. "Cache of (LAST-POS . LAST-PPSS).")
  326. (make-variable-buffer-local 'syntax-ppss-last)
  327. (defalias 'syntax-ppss-after-change-function 'syntax-ppss-flush-cache)
  328. (defun syntax-ppss-flush-cache (beg &rest ignored)
  329. "Flush the cache of `syntax-ppss' starting at position BEG."
  330. ;; Set syntax-propertize to refontify anything past beg.
  331. (setq syntax-propertize--done (min beg syntax-propertize--done))
  332. ;; Flush invalid cache entries.
  333. (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
  334. (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
  335. ;; Throw away `last' value if made invalid.
  336. (when (< beg (or (car syntax-ppss-last) 0))
  337. ;; If syntax-begin-function jumped to BEG, then the old state at BEG can
  338. ;; depend on the text after BEG (which is presumably changed). So if
  339. ;; BEG=(car (nth 10 syntax-ppss-last)) don't reuse that data because the
  340. ;; assumed nil state at BEG may not be valid any more.
  341. (if (<= beg (or (syntax-ppss-toplevel-pos (cdr syntax-ppss-last))
  342. (nth 3 syntax-ppss-last)
  343. 0))
  344. (setq syntax-ppss-last nil)
  345. (setcar syntax-ppss-last nil)))
  346. ;; Unregister if there's no cache left. Sadly this doesn't work
  347. ;; because `before-change-functions' is temporarily bound to nil here.
  348. ;; (unless syntax-ppss-cache
  349. ;; (remove-hook 'before-change-functions 'syntax-ppss-flush-cache t))
  350. )
  351. (defvar syntax-ppss-stats
  352. [(0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (1 . 2500.0)])
  353. (defun syntax-ppss-stats ()
  354. (mapcar (lambda (x)
  355. (condition-case nil
  356. (cons (car x) (truncate (/ (cdr x) (car x))))
  357. (error nil)))
  358. syntax-ppss-stats))
  359. (defun syntax-ppss (&optional pos)
  360. "Parse-Partial-Sexp State at POS, defaulting to point.
  361. The returned value is the same as that of `parse-partial-sexp'
  362. run from point-min to POS except that values at positions 2 and 6
  363. in the returned list (counting from 0) cannot be relied upon.
  364. Point is at POS when this function returns."
  365. ;; Default values.
  366. (unless pos (setq pos (point)))
  367. (syntax-propertize pos)
  368. ;;
  369. (let ((old-ppss (cdr syntax-ppss-last))
  370. (old-pos (car syntax-ppss-last))
  371. (ppss nil)
  372. (pt-min (point-min)))
  373. (if (and old-pos (> old-pos pos)) (setq old-pos nil))
  374. ;; Use the OLD-POS if usable and close. Don't update the `last' cache.
  375. (condition-case nil
  376. (if (and old-pos (< (- pos old-pos)
  377. ;; The time to use syntax-begin-function and
  378. ;; find PPSS is assumed to be about 2 * distance.
  379. (* 2 (/ (cdr (aref syntax-ppss-stats 5))
  380. (1+ (car (aref syntax-ppss-stats 5)))))))
  381. (progn
  382. (incf (car (aref syntax-ppss-stats 0)))
  383. (incf (cdr (aref syntax-ppss-stats 0)) (- pos old-pos))
  384. (parse-partial-sexp old-pos pos nil nil old-ppss))
  385. (cond
  386. ;; Use OLD-PPSS if possible and close enough.
  387. ((and (not old-pos) old-ppss
  388. ;; If `pt-min' is too far from `pos', we could try to use
  389. ;; other positions in (nth 9 old-ppss), but that doesn't
  390. ;; seem to happen in practice and it would complicate this
  391. ;; code (and the before-change-function code even more).
  392. ;; But maybe it would be useful in "degenerate" cases such
  393. ;; as when the whole file is wrapped in a set
  394. ;; of parentheses.
  395. (setq pt-min (or (syntax-ppss-toplevel-pos old-ppss)
  396. (nth 2 old-ppss)))
  397. (<= pt-min pos) (< (- pos pt-min) syntax-ppss-max-span))
  398. (incf (car (aref syntax-ppss-stats 1)))
  399. (incf (cdr (aref syntax-ppss-stats 1)) (- pos pt-min))
  400. (setq ppss (parse-partial-sexp pt-min pos)))
  401. ;; The OLD-* data can't be used. Consult the cache.
  402. (t
  403. (let ((cache-pred nil)
  404. (cache syntax-ppss-cache)
  405. (pt-min (point-min))
  406. ;; I differentiate between PT-MIN and PT-BEST because
  407. ;; I feel like it might be important to ensure that the
  408. ;; cache is only filled with 100% sure data (whereas
  409. ;; syntax-begin-function might return incorrect data).
  410. ;; Maybe that's just stupid.
  411. (pt-best (point-min))
  412. (ppss-best nil))
  413. ;; look for a usable cache entry.
  414. (while (and cache (< pos (caar cache)))
  415. (setq cache-pred cache)
  416. (setq cache (cdr cache)))
  417. (if cache (setq pt-min (caar cache) ppss (cdar cache)))
  418. ;; Setup the before-change function if necessary.
  419. (unless (or syntax-ppss-cache syntax-ppss-last)
  420. (add-hook 'before-change-functions
  421. 'syntax-ppss-flush-cache t t))
  422. ;; Use the best of OLD-POS and CACHE.
  423. (if (or (not old-pos) (< old-pos pt-min))
  424. (setq pt-best pt-min ppss-best ppss)
  425. (incf (car (aref syntax-ppss-stats 4)))
  426. (incf (cdr (aref syntax-ppss-stats 4)) (- pos old-pos))
  427. (setq pt-best old-pos ppss-best old-ppss))
  428. ;; Use the `syntax-begin-function' if available.
  429. ;; We could try using that function earlier, but:
  430. ;; - The result might not be 100% reliable, so it's better to use
  431. ;; the cache if available.
  432. ;; - The function might be slow.
  433. ;; - If this function almost always finds a safe nearby spot,
  434. ;; the cache won't be populated, so consulting it is cheap.
  435. (when (and (not syntax-begin-function)
  436. (boundp 'font-lock-beginning-of-syntax-function)
  437. font-lock-beginning-of-syntax-function)
  438. (set (make-local-variable 'syntax-begin-function)
  439. font-lock-beginning-of-syntax-function))
  440. (when (and syntax-begin-function
  441. (progn (goto-char pos)
  442. (funcall syntax-begin-function)
  443. ;; Make sure it's better.
  444. (> (point) pt-best))
  445. ;; Simple sanity checks.
  446. (< (point) pos) ; backward-paragraph can fail here.
  447. (not (memq (get-text-property (point) 'face)
  448. '(font-lock-string-face font-lock-doc-face
  449. font-lock-comment-face))))
  450. (incf (car (aref syntax-ppss-stats 5)))
  451. (incf (cdr (aref syntax-ppss-stats 5)) (- pos (point)))
  452. (setq pt-best (point) ppss-best nil))
  453. (cond
  454. ;; Quick case when we found a nearby pos.
  455. ((< (- pos pt-best) syntax-ppss-max-span)
  456. (incf (car (aref syntax-ppss-stats 2)))
  457. (incf (cdr (aref syntax-ppss-stats 2)) (- pos pt-best))
  458. (setq ppss (parse-partial-sexp pt-best pos nil nil ppss-best)))
  459. ;; Slow case: compute the state from some known position and
  460. ;; populate the cache so we won't need to do it again soon.
  461. (t
  462. (incf (car (aref syntax-ppss-stats 3)))
  463. (incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
  464. ;; If `pt-min' is too far, add a few intermediate entries.
  465. (while (> (- pos pt-min) (* 2 syntax-ppss-max-span))
  466. (setq ppss (parse-partial-sexp
  467. pt-min (setq pt-min (/ (+ pt-min pos) 2))
  468. nil nil ppss))
  469. (let ((pair (cons pt-min ppss)))
  470. (if cache-pred
  471. (push pair (cdr cache-pred))
  472. (push pair syntax-ppss-cache))))
  473. ;; Compute the actual return value.
  474. (setq ppss (parse-partial-sexp pt-min pos nil nil ppss))
  475. ;; Debugging check.
  476. ;; (let ((real-ppss (parse-partial-sexp (point-min) pos)))
  477. ;; (setcar (last ppss 4) 0)
  478. ;; (setcar (last real-ppss 4) 0)
  479. ;; (setcar (last ppss 8) nil)
  480. ;; (setcar (last real-ppss 8) nil)
  481. ;; (unless (equal ppss real-ppss)
  482. ;; (message "!!Syntax: %s != %s" ppss real-ppss)
  483. ;; (setq ppss real-ppss)))
  484. ;; Store it in the cache.
  485. (let ((pair (cons pos ppss)))
  486. (if cache-pred
  487. (if (> (- (caar cache-pred) pos) syntax-ppss-max-span)
  488. (push pair (cdr cache-pred))
  489. (setcar cache-pred pair))
  490. (if (or (null syntax-ppss-cache)
  491. (> (- (caar syntax-ppss-cache) pos)
  492. syntax-ppss-max-span))
  493. (push pair syntax-ppss-cache)
  494. (setcar syntax-ppss-cache pair)))))))))
  495. (setq syntax-ppss-last (cons pos ppss))
  496. ppss)
  497. (args-out-of-range
  498. ;; If the buffer is more narrowed than when we built the cache,
  499. ;; we may end up calling parse-partial-sexp with a position before
  500. ;; point-min. In that case, just parse from point-min assuming
  501. ;; a nil state.
  502. (parse-partial-sexp (point-min) pos)))))
  503. ;; Debugging functions
  504. (defun syntax-ppss-debug ()
  505. (let ((pt nil)
  506. (min-diffs nil))
  507. (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
  508. (when pt (push (- pt (car x)) min-diffs))
  509. (setq pt (car x)))
  510. min-diffs))
  511. ;; XEmacs compatibility functions
  512. ;; (defun buffer-syntactic-context (&optional buffer)
  513. ;; "Syntactic context at point in BUFFER.
  514. ;; Either of `string', `comment' or `nil'.
  515. ;; This is an XEmacs compatibility function."
  516. ;; (with-current-buffer (or buffer (current-buffer))
  517. ;; (syntax-ppss-context (syntax-ppss))))
  518. ;; (defun buffer-syntactic-context-depth (&optional buffer)
  519. ;; "Syntactic parenthesis depth at point in BUFFER.
  520. ;; This is an XEmacs compatibility function."
  521. ;; (with-current-buffer (or buffer (current-buffer))
  522. ;; (syntax-ppss-depth (syntax-ppss))))
  523. (provide 'syntax)
  524. ;;; syntax.el ends here