comparison lisp/font-lock.el @ 17468:94face85736e

(font-lock-fontify-syntactically-region): Use new features of parse-partial-sexp instead of doing regexp search.
author Richard M. Stallman <rms@gnu.org>
date Tue, 15 Apr 1997 05:00:36 +0000
parents a2d3b455c6f4
children 83dabcc0a796
comparison
equal deleted inserted replaced
17467:98c47e7857f3 17468:94face85736e
1025 (defvar font-lock-cache-position nil) 1025 (defvar font-lock-cache-position nil)
1026 1026
1027 (defun font-lock-fontify-syntactically-region (start end &optional loudly) 1027 (defun font-lock-fontify-syntactically-region (start end &optional loudly)
1028 "Put proper face on each string and comment between START and END. 1028 "Put proper face on each string and comment between START and END.
1029 START should be at the beginning of a line." 1029 START should be at the beginning of a line."
1030 (let ((synstart (cond (font-lock-comment-start-regexp 1030 (let (state prev here comment
1031 (concat "\\s\"\\|" font-lock-comment-start-regexp)) 1031 (cache (marker-position font-lock-cache-position)))
1032 (comment-start-skip
1033 (concat "\\s\"\\|" comment-start-skip))
1034 (t
1035 "\\s\"")))
1036 (cache (marker-position font-lock-cache-position))
1037 state prev here beg)
1038 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name))) 1032 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
1039 (goto-char start) 1033 (goto-char start)
1040 ;; 1034 ;;
1041 ;; Find the state at the `beginning-of-line' before `start'. 1035 ;; Find the state at the `beginning-of-line' before `start'.
1042 (if (eq start cache) 1036 (if (eq start cache)
1056 ;; Cache the state and position of `start'. 1050 ;; Cache the state and position of `start'.
1057 (setq font-lock-cache-state state) 1051 (setq font-lock-cache-state state)
1058 (set-marker font-lock-cache-position start)) 1052 (set-marker font-lock-cache-position start))
1059 ;; 1053 ;;
1060 ;; If the region starts inside a string, show the extent of it. 1054 ;; If the region starts inside a string, show the extent of it.
1061 (when (nth 3 state) 1055 (when (or (nth 4 state) (nth 3 state))
1062 (setq here (point)) 1056 (setq comment (nth 4 state) here (point))
1063 (while (and (re-search-forward "\\s\"" end 'move) 1057 (setq state (parse-partial-sexp (point) end
1064 ;; Verify the state so we don't get fooled by quoting. 1058 nil nil state 'syntax-table))
1065 (nth 3 (parse-partial-sexp here (point) nil nil state)))) 1059 (put-text-property here (point) 'face
1066 (put-text-property here (point) 'face font-lock-string-face) 1060 (if comment
1067 (setq state (parse-partial-sexp here (point) nil nil state))) 1061 font-lock-comment-face
1068 ;; 1062 font-lock-string-face)))
1069 ;; Likewise for a comment.
1070 (when (or (nth 4 state) (nth 7 state))
1071 (let ((comstart (cond (font-lock-comment-start-regexp
1072 font-lock-comment-start-regexp)
1073 (comment-start-skip
1074 (concat "\\s<\\|" comment-start-skip))
1075 (t
1076 "\\s<")))
1077 (count 1))
1078 (setq here (point))
1079 (condition-case nil
1080 (save-restriction
1081 (narrow-to-region (point-min) end)
1082 ;; Go back to the real start of the comment.
1083 (re-search-backward comstart)
1084 (forward-comment 1)
1085 ;; If there is more than one comment type, then the previous
1086 ;; comment start might not be the real comment start.
1087 ;; For example, in C++ code, `here' might be on a line following
1088 ;; a // comment that is actually within a /* */ comment.
1089 (while (<= (point) here)
1090 (goto-char here)
1091 (re-search-backward comstart nil nil (incf count))
1092 (forward-comment 1))
1093 ;; Go back to the real end of the comment.
1094 (skip-chars-backward " \t"))
1095 (error (goto-char end)))
1096 (put-text-property here (point) 'face font-lock-comment-face)
1097 (setq state (parse-partial-sexp here (point) nil nil state))))
1098 ;; 1063 ;;
1099 ;; Find each interesting place between here and `end'. 1064 ;; Find each interesting place between here and `end'.
1100 (while (and (< (point) end) 1065 (while (and (< (point) end)
1101 (setq prev (point)) 1066 (progn
1102 (re-search-forward synstart end t) 1067 (setq prev (point)
1103 (setq state (parse-partial-sexp prev (point) nil nil state))) 1068 state (parse-partial-sexp (point) end
1104 (cond ((nth 3 state) 1069 nil nil state 'syntax-table))
1105 ;; 1070 (or (nth 3 state) (nth 4 state))))
1106 ;; Found a real string start. 1071 (setq here (nth 8 state) comment (nth 4 state))
1107 (setq here (point) beg (match-beginning 0)) 1072 (setq state (parse-partial-sexp (point) end
1108 (condition-case nil 1073 nil nil state 'syntax-table))
1109 (save-restriction 1074 (put-text-property here (point) 'face
1110 (narrow-to-region (point-min) end) 1075 (if comment
1111 (goto-char (scan-sexps beg 1))) 1076 font-lock-comment-face
1112 (error (goto-char end))) 1077 font-lock-string-face))
1113 (put-text-property beg (point) 'face font-lock-string-face) 1078 ;;
1114 (setq state (parse-partial-sexp here (point) nil nil state))) 1079 ;; Make sure `prev' is non-nil after the loop
1115 ((or (nth 4 state) (nth 7 state)) 1080 ;; only if it was set on the very last iteration.
1116 ;; 1081 (setq prev nil))))
1117 ;; Found a real comment start.
1118 (setq here (point) beg (or (match-end 1) (match-beginning 0)))
1119 (goto-char beg)
1120 (condition-case nil
1121 (save-restriction
1122 (narrow-to-region (point-min) end)
1123 (forward-comment 1)
1124 (skip-chars-backward " \t"))
1125 (error (goto-char end)))
1126 (put-text-property beg (point) 'face font-lock-comment-face)
1127 (setq state (parse-partial-sexp here (point) nil nil state)))))))
1128 1082
1129 ;;; End of Syntactic fontification functions. 1083 ;;; End of Syntactic fontification functions.
1130 1084
1131 ;;; Additional text property functions. 1085 ;;; Additional text property functions.
1132 1086