Subject: bug#22980: Something looks fishy in prettify-symbols-default-compose-p (it depends on (point))

From: Clément Pit--Claudel <[email-masked]>
Date: Thu, 10 Mar 2016 12:30:19 -0500

The docs of prettify-symbols-default-compose-p and prettify-symbols-compose-predicate don't suggest that the function should depend on the point, but it looks like it does:

    (defun prettify-symbols-default-compose-p (start end _match)
      "Return true iff the symbol MATCH should be composed.
    The symbol starts at position START and ends at position END.
    This is the default for `prettify-symbols-compose-predicate'
    which is suitable for most programming languages such as C or Lisp."
      ;; Check that the chars should really be composed into a symbol.
      (message "%S %S %S %S" (point) start end _match)
      (print (let* ((syntaxes-beg (if (memq (char-syntax (char-after start)) '(?w ?_))
                                      '(?w ?_) '(?. ?\\)))
                    (syntaxes-end (if (memq (char-syntax (char-before end)) '(?w ?_))
                                      '(?w ?_) '(?. ?\\))))
               (not (or (print (memq (char-syntax (or (char-before start) ?\s)) syntaxes-beg))
                        (print (memq (char-syntax (or (char-after end) ?\s)) syntaxes-end))
                        (print (nth 8 (syntax-ppss))))))))
                                      ^ here

    (defvar-local prettify-symbols-compose-predicate
      #'prettify-symbols-default-compose-p
      "A predicate for deciding if the currently matched symbol is to be composed.
    The matched symbol is the car of one entry in `prettify-symbols-alist'.
    The predicate receives the match's start and end positions as well
    as the match-string as arguments.")

This means that calling (prettify-symbols-default-compose-p 1 2 nil) returns nil or t in the following example returns nil or t depending on the point in the following example:

  (* example *)
  ^ column 1 starts here

This makes it unreliable to prettify ‘*’ as ‘×’ in OCaml, for example.

Fixing this issue is not entirely trivial: calling syntax-ppss on end will work in some cases, and in other cases it would be better on beg. This is because for a two-characters comment starter, syntax-ppss is inconsistent in what it call the “inside” of the comment:

   (* comment *)
    ^ outside  ^ inside

This is a follow up to this ML thread: https://lists.gnu.org/archive/html/emacs-devel/2016-03/msg00415.html