X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/21733e4f154f8830fa568a347a0d6dbd59793c2b..bb8097b96b074e55ff05dc379b394dbdbdc82f3e:/lisp/newcomment.el diff --git a/lisp/newcomment.el b/lisp/newcomment.el index bcb5f721ae..251c160534 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el @@ -435,18 +435,23 @@ If UNP is non-nil, unquote nested comment markers." ;;;; Navigation ;;;; -(defvar comment-use-global-state nil +(defvar comment-use-global-state t "Non-nil means that the global syntactic context is used. More specifically, it means that `syntax-ppss' is used to find out whether -point is within a string or not. Major modes whose syntax is faithfully -described by the syntax-tables can set this to non-nil so comment markers -in strings will not confuse Emacs.") +point is within a string or not. Major modes whose syntax is not faithfully +described by the syntax-tables (or where `font-lock-syntax-table' is radically +different from the main syntax table) can set this to nil, +then `syntax-ppss' cache won't be used in comment-related routines.") + +(make-obsolete-variable 'comment-use-global-state 'comment-use-syntax "24.4") (defun comment-search-forward (limit &optional noerror) "Find a comment start between point and LIMIT. Moves point to inside the comment and returns the position of the comment-starter. If no comment is found, moves point to LIMIT -and raises an error or returns nil if NOERROR is non-nil." +and raises an error or returns nil if NOERROR is non-nil. + +Ensure that `comment-normalize-vars' has been called before you use this." (if (not comment-use-syntax) (if (re-search-forward comment-start-skip limit noerror) (or (match-end 1) (match-beginning 0)) @@ -484,7 +489,9 @@ and raises an error or returns nil if NOERROR is non-nil." "Find a comment start between LIMIT and point. Moves point to inside the comment and returns the position of the comment-starter. If no comment is found, moves point to LIMIT -and raises an error or returns nil if NOERROR is non-nil." +and raises an error or returns nil if NOERROR is non-nil. + +Ensure that `comment-normalize-vars' has been called before you use this." ;; FIXME: If a comment-start appears inside a comment, we may erroneously ;; stop there. This can be rather bad in general, but since ;; comment-search-backward is only used to find the comment-column (in @@ -511,30 +518,36 @@ and raises an error or returns nil if NOERROR is non-nil." "Find the beginning of the enclosing comment. Returns nil if not inside a comment, else moves point and returns the same as `comment-search-backward'." - ;; HACK ATTACK! - ;; We should really test `in-string-p' but that can be expensive. - (unless (eq (get-text-property (point) 'face) 'font-lock-string-face) - (let ((pt (point)) - (cs (comment-search-backward nil t))) - (when cs - (if (save-excursion - (goto-char cs) - (and - ;; For modes where comment-start and comment-end are the same, - ;; the search above may have found a `ce' rather than a `cs'. - (or (if comment-end-skip (not (looking-at comment-end-skip))) - ;; Maybe font-lock knows that it's a `cs'? - (eq (get-text-property (match-end 0) 'face) - 'font-lock-comment-face) - (unless (eq (get-text-property (point) 'face) - 'font-lock-comment-face) - ;; Let's assume it's a `cs' if we're on the same line. - (>= (line-end-position) pt))) - ;; Make sure that PT is not past the end of the comment. - (if (comment-forward 1) (> (point) pt) (eobp)))) - cs - (goto-char pt) - nil))))) + (if (and comment-use-syntax comment-use-global-state) + (let ((state (syntax-ppss))) + (when (nth 4 state) + (goto-char (nth 8 state)) + (prog1 (point) + (when (looking-at comment-start-skip) + (goto-char (match-end 0)))))) + ;; Can't rely on the syntax table, let's guess based on font-lock. + (unless (eq (get-text-property (point) 'face) 'font-lock-string-face) + (let ((pt (point)) + (cs (comment-search-backward nil t))) + (when cs + (if (save-excursion + (goto-char cs) + (and + ;; For modes where comment-start and comment-end are the same, + ;; the search above may have found a `ce' rather than a `cs'. + (or (if comment-end-skip (not (looking-at comment-end-skip))) + ;; Maybe font-lock knows that it's a `cs'? + (eq (get-text-property (match-end 0) 'face) + 'font-lock-comment-face) + (unless (eq (get-text-property (point) 'face) + 'font-lock-comment-face) + ;; Let's assume it's a `cs' if we're on the same line. + (>= (line-end-position) pt))) + ;; Make sure that PT is not past the end of the comment. + (if (comment-forward 1) (> (point) pt) (eobp)))) + cs + (goto-char pt) + nil)))))) (defun comment-forward (&optional n) "Skip forward over N comments.