7

I'm using the new emacs 24.4 prettify-symbols-mode, but it isn't behaving consistently.

I turn it on with:

(prettify-symbols-mode t)
(global-prettify-symbols-mode t)

And I'm trying to change the way python source code looks with the follownig setup:

(add-hook 'python-mode-hook
          (lambda ()
            (push '("**2" . ?²) prettify-symbols-alist)
            (push '("_x" . ?ᵪ) prettify-symbols-alist)
            (push '("sum" . ?∑) prettify-symbols-alist)))

This works, but not consistently. For example,

x**2 x_x and sum(x) all look good.

The inconsistent part is that x**23 maintains the superscript 2, but the 3 is regular. But if I say x**2+2, or x**2, then the **2 is not pretty (and it should be). I'd like the 2 to only be superscript when it is followed by certain characters (space, plus, comma, etc.). Alternatively, not superscript if followed by [0-9].

Meanwhile, sum behaves correctly. sum(, sum**2, sum+, and others are pretty (as they should be) and summer is not (correctly).

Basil
  • 12,019
  • 43
  • 69
mankoff
  • 4,108
  • 1
  • 22
  • 39
  • The documentation for prettify-symbols-alist says: *Each element looks like (SYMBOL . CHARACTER), where the symbol matching SYMBOL (a string, not a regexp) will be shown as CHARACTER instead.*, so you won't be able to use regular expressions. –  Nov 21 '14 at 10:36
  • 1
    Please use `M-x report-emacs-bug` to report this problem, which will probably require extending prettify-symbols-mode. – Stefan Nov 21 '14 at 14:17

3 Answers3

5

The closest I have gotten to replicating this is to customize the way emacs sets the font for characters for each mode I want to change

(add-to-list 'font-lock-extra-managed-props 'display)

(font-lock-add-keywords 'latex-mode ;; you can change the mode here
                        '(("\\(wordone\\).*?wordtwo" 1 '(face nil display "∑"))))

This will replace wordone with as long as it is on the same line as wordtwo when font-lock-mode is enabled in latex-mode

The 1 refers to regexp group one in the string denoted by \\( \\)

KhalfaniW
  • 337
  • 4
  • 8
  • Although this does work, adding this to my configuration (using Doom Emacs) breaks `image-mode` for me -- images will not display. Just a warning for anyone stumbling across this. – eeowaa Feb 16 '23 at 20:28
4

According to the documentation for prettify-symbols-alist, you cannot use regular expressions here:

Each element looks like (SYMBOL . CHARACTER), where the symbol matching SYMBOL (a string, not a regexp) will be shown as CHARACTER instead.

pretty-symbols-mode uses regexp-opt internally, which creates a big regular expression from all the strings in the car position of each of the alist pairs. If you really want regular expressions, you are probably better off defining font-lock keywords yourself, outside of the confines of prettify-symbols-mode.

0

@rekado is right.

Each element looks like (SYMBOL . CHARACTER), where the symbol matching SYMBOL (a string, not a regexp) will be shown as CHARACTER instead.

but there is more

CHARACTER can be a character, or it can be a list or vector, in which case it will be used to compose the new symbol as per the third argument of ‘compose-region’.

So it can let you do some cool stuffs like y superscript star index i

(setq prettify-symbols-alist
      '(("yy" . (?y (tr . Bc) 1645 (br . cr) ?i))))

see the C-h compose-region. I'm not going to lie it took me a long time to figure out how to do it and I still have many questions but my understanding of the doc says that the N, are the characters. 0, 2, 4, and the N+1 are the composition rules. But these composition rules seems to be reversed. For example 'Bc' is for 'y' and 'tr' is for '1645'.

DJJ
  • 732
  • 5
  • 19