I want to the prettify-symbols-mode functionality, but using strings instead of chars as visual replacements:

;; typical replacement for chars
(push '("lambda" . ?λ) prettify-symbols-alist)

(defun my/add-visual-replacement (whatwith)
  ;; answer to my question
  ;; maybe do something with `prettify-symbols-alist'

;; usage
(with-current-buffer "micódigo.py"
    (my/add-visual-replacement '("print" . "imprime")))

so, with cursor outside of the replacement, shows the string replacement:

def mifunción():

But will show the true string when the cursor is on it:

def mifunción():

Best approach that I'm trying to understand is using reference-point-alist (as was done here), but I don't fully understand it.

  • I was proposing what would the usage be like. I edited to contrast with current prettify-symbols-mode use. Aug 10, 2017 at 21:25
  • Perhaps you could use defalias instead of prettify for this? That won't show the "true string" but lets you use whatever symbol aliases you want, and you can use standard things like C-h f to describe them. For example: (defalias 'tecla 'kbd).
    – glucas
    Aug 12, 2017 at 13:37
  • 1
    I was thinking in creating an auto-translate library in emacs for non-english speakers who want to learn how to program. It wouldn't alter the source code and it would ease the learning process. Inspired by this post Aug 14, 2017 at 12:01
  • OK, in that case aliases are not what you want! Sorry, I don't have a better answer to the question. I suspect you would want to use your own overlays for this rather than doing it on top of prettify, but will let others more familiar with this stuff comment.
    – glucas
    Aug 14, 2017 at 13:06

1 Answer 1


Like glucas, I'm doubtful that prettify-symbols-mode is actually what you want to be using for your auto-translate library; however the question seems useful in general and I was curious about the character composition rules. I came up with the following:

(defun my/add-visual-replacement (from to)
  "Make `prettify-symbols-mode' replace string FROM with string TO.

Updates `prettify-symbols-alist'.  You may need to toggle
`prettify-symbols-mode' to make the changes take effect.

Each character of TO is vertically aligned using the baseline,
such that base-left of the character is aligned with base-right
of the preceding character.  Refer to `reference-point-alist'
for more information."
  (push (cons from (let ((composition nil))
                     (dolist (char (string-to-list to)
                                   (nreverse (cdr composition)))
                       (push char composition)
                       (push '(Br . Bl) composition))))

The resulting composition for a TO value of "abc" will be:

(?a (Br . Bl) ?b (Br . Bl) ?c)

Where the composition rule (Br . Bl) between each pair of characters says that the base-right of the first character will be aligned with the base-left of the second character.

  • This looks perfect to me, but you hint that this is not the correct way to have a "replace words from this dictionary on this buffer". What would you suggest as an overall correct solution (maybe re-phrase my question) Aug 17, 2017 at 14:25
  • I suspect it would be better to be directly using C-h i g (elisp)Text Properties in your library, rather than building on top of prettify-symbols-mode like this. Especially given that you were looking for special behaviour depending on whether point is inside or outside a word, which I don't think (?) is a prettify-symbols-mode feature, in which case you would need to be writing related custom code anyhow.
    – phils
    Aug 17, 2017 at 20:36
  • prettify-symbols-mode is built upon compose-region so it's related to text properties, but both subjects have exclusive features (so I wouldn't say one includes the other). Anyway, I'll do some further research based on this discussion. Thanks for taking the time to explain everything Aug 18, 2017 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.