0

I have this face definition that applies globally:

(set-face-attribute 'variable-pitch nil :family "Charis SIL Compact" :height 180)

The problem is that Elfeed and Mu4e also use the variable pitch mode and I don't want that.

I would like to create a custom function called distraction-free-writing that is bound to a key and when that key is pressed to activate the variable pitch mode in the current Org buffer and also set olivetti-body-width to a value of 60.

For fixed pitched font the olivetti value is set to 80, however variable pitch font is more condensed, so 80 characters fit in a 60 olivetti width.

Is it possible to make that face attribute local for a function?

1 Answer 1

1

I don't understand exactly what you want to do, but maybe something like this:

(defvar-local my-distraction-free-writing-remap nil)

(defun my-distraction-free-writing ()
  (interactive)
  (if my-distraction-free-writing-remap
      (progn
        (face-remap-remove-relative
         my-distraction-free-writing-remap)
        (setq olivetti-body-width 80)
        (olivetti-mode 1)
        (setq my-distraction-free-writing-remap nil))
    (setq my-distraction-free-writing-remap
          (face-remap-add-relative 'variable-pitch
                                   :family "Charis SIL Compact"
                                   :height 180))
    (variable-pitch-mode 1)    
    (setq olivetti-body-width 60)
    (olivetti-mode 1)))

With face-remap-add-relative you can remap face attributes buffer-locally. So this redefines the face variable-pitch in the current buffer to have the given attributes, then turns on variable-pitch-mode and sets olivetti-body-width.

To bind it to a key: (keymap-set org-mode-map "<f8>" #'my-distraction-free-writing).

10
  • Thank you. This is what I was looking for. I just had to add (olivetti-mode 1) otherwise the new width don't take effect. If I want this to be applied to all Org buffer do I need to attach the function to the org-mode hook? Commented Feb 22, 2023 at 14:13
  • I may ask too much but if I decide to bind it to a key, is it possible to make it toggle. For example if I press F8 to call the function as is now, but when I press F8 again to switch back to the previous fixed-pitch font "Hack" and set back olivetti to a width of 80. Commented Feb 22, 2023 at 14:19
  • 1
    Yes, you need to attach it to the hook. I changed the function so that now it should toggle. Commented Feb 22, 2023 at 16:56
  • 1
    What is the problem? The keybinding has no effect? It works for me. Try another binding, maybe something is overwriting yours. Commented Feb 22, 2023 at 17:19
  • 1
    OK. I'll make an edit to reflect that. The variable is used both for toggling and to store the active face remapping as this makes removing it again easier. Commented Feb 22, 2023 at 17:48

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.