5

Is it possible to display a space with a monospace font, while displaying all other characters with a variable-width font?

I tried

(set-fontset-font t '(#x20 . #x20) (font-spec :family "Source Code Pro"))

but the result was an error message saying

Can't set a font for partial ASCII range

Is this completely impossible, or am I doing the wrong thing?

  • It does seem to work for characters codepoints above ASCII range (0x80 and up), but that doesn't help you. – rekado Dec 24 '14 at 12:34
  • 1
    Have a look at buffer-display-table. This is used by whitespace-mode and its whitespace-display-mappings option (noting specifically the space-mark values for the latter), so to begin with you should just play with those. I suspect you can map a space to a codepoint which you can control? – phils Sep 16 '15 at 12:56
  • @phils That's a really good idea, thanks! buffer-display-table lets me control the width of spaces, by mapping spaces to other Unicode spaces (like #x2007) that are not nearly as thin as my original variable-width spaces, and I can even change the font. Can you please post your comment as an answer, so that I can accept it? – Kirill Sep 16 '15 at 19:48
  • Done. Please feel free to edit your specific solution into either your question or my answer, so that others can see the code. – phils Sep 16 '15 at 21:45
3

Have a look at buffer-display-table.

This is used by whitespace-mode and its whitespace-display-mappings option (noting specifically the space-mark values for the latter), so to begin with you should just play with those.

I suspect you can map a space to a codepoint which you can control?

Edit (Kirill): This is a snippet that illustrates how this answers the question: it can be used to display spaces differently, which is what I wanted to do. It makes every space #x20, which is very narrow in some proportional width fonts, be displayed as a different kind of space, for example as one of #x2000..#x200a, which includes a choice of different space widths - https://www.cs.tut.fi/~jkorpela/chars/spaces.html. It also seems that the font that the new space gets displayed with can be correctly set with set-fontset-font.

(let ((tab (make-display-table))
        ;; try #x2000 .. #x200a, plus there are some other Unicode space characters
        (cp #x2002))
    (aset tab ?  (vector (make-glyph-code cp)))
    (setq-local buffer-display-table tab)
    ;; Compare different fonts' space widths:
    (set-fontset-font t (cons cp cp) (font-spec :family "DejaVu Sans Mono"))
    ;; (set-fontset-font t (cons cp cp) (font-spec :family "Source Code Pro"))
    )
0

Well, that error message indicates that it isn't.

I suspect that you want this because in your opinion variable-width fonts look better, but you also want to use a monospace font for spaces because that would help reducing the effect on indentation.

That seems reasonable - and it might be a good idea to request this feature on emacs-devel.

  • Yes, the error message does seem to say that, but that's not really a definitive answer to this question. I already implemented something like this in propfont-mixed, but the question still stands. – Kirill Jan 18 '15 at 21:27

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.