2

Default Emacs settings show non-breaking spaces as a kind of underscore. This is very useful because these otherwise invisible characters would break code execution in many programming languages, and would be very hard to debug.

The setting, documented here, can be changed with the variable nobreak-char-display. Org-mode also gives ways to specify file local variable values.

What I would like to achieve, though, is hiding these non-breaking spaces (which are very usual in the French language) everywhere in my org-mode file except in code blocks, where I need them to be plainly visible. To give an illustration, I want the non-breaking space before the interrogation point below hidden, but the other two visible.

Is there any way to achieve that?

Non-breaking spaces inside and outside code blocks

1 Answer 1

1

You could add the non-breaking space as a new font-lock keyword to python-mode:

;; I'm defining a new face, though any face with underline should work
(defface my-nobreak-space
  '((t :inherit default :underline t))
  "My non-breaking space face.")

(font-lock-add-keywords 'python-mode '(("\u00a0" . 'my-nobreak-space)))
(add-hook 'org-mode-hook #'(lambda () (setq-local nobreak-char-display nil)))

Before: Before

After: After

You'll need to add the keyword to each major-mode language you use in org.

2
  • Thank you for this idea! I tried setting up your solution. It works perfectly for Python, but for C/C++, I can't get it to work. I tried (font-lock-add-keywords 'cc-mode '(("\u00a0" . 'my-nobreak-space))). Also, can you explain why you need to define a face, if, quote, "any face with an underline should work"? Are there faces without underlines ?
    – kotchwane
    Commented Jun 30, 2020 at 16:03
  • Try (font-lock-add-keywords 'c++-mode '(("\u00a0" . 'my-nobreak-space))). I didn't need to define a face, but it allows me more flexibility. Most standard faces don't have the underline attribute set, and for those that do, themes/customization might turn if off. By defining my own face, the display of the character is less prone to unexpected changes.
    – Mark
    Commented Jun 30, 2020 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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