8

I work with a lot of tabular data files and use stripe-buffer.el make them easier to read. But I also work with more than one, so I'd like to make it easier to distinguish among them as well and want to change the stripe colors per buffer.

I can change the face globally using:

(modify-face 'stripe-highlight "black" "#cce7ff")

Is there a way to do this locally to a single buffer?

wdkrnls
  • 3,657
  • 2
  • 27
  • 46
  • Possible duplicate: [Use a different color theme for eww buffers](http://emacs.stackexchange.com/q/3622/50). – Malabarba Jan 11 '15 at 21:29
  • Related: [How to customize syntax highlight for just a given mode](https://emacs.stackexchange.com/questions/2957/how-to-customize-syntax-highlight-for-just-a-given-mode/2968) – erikstokes Jan 12 '15 at 00:55

2 Answers2

17

As noted by @phils in the comment below, the Emacs manual suggests not modifying the face-remapping-alist directly due to possible unintended side effects: . . .to avoid trampling on remappings applied elsewhere. Thus, (setq-local face-remapping-alist '((stripe-highlight (:background "white" :foreground "black")))) is not considered to be the preferred method. Instead, the Emacs manual suggests using a function such as face-remap-add-relative:

(face-remap-add-relative 'stripe-highlight '(:foreground "black" :background "yellow"))

Here is a link to additional Emacs documentation on this issue: http://www.gnu.org/software/emacs/manual/html_node/elisp/Face-Remapping.html

lawlist
  • 18,826
  • 5
  • 37
  • 118
  • Nice; the variable docstring even mentions the buffer-local scenario as a use-case for this. – phils Jan 11 '15 at 22:40
  • `C-h i g (elisp) Face Remapping` documents API functions intended for buffer-local modifications, and indicates that they are preferable to manipulating the alist directly. You'll be able to improve your answer that way. – phils Jan 11 '15 at 22:53
  • 1
    @phils -- thank you for suggesting the use of `face-remap-add-relative` as a means of modifying the `face-remapping-alist`. I have edited the answer accordingly. – lawlist Jan 11 '15 at 23:50
  • One more example: `(face-remap-add-relative 'default '(:family "Arial"))`. – Adobe Jan 12 '15 at 10:15
  • And also a menu invoked with *Shift + mouse1* (``, aka `mouse-appearance-menu`) has an item called "Change Buffer Font". – Adobe Jan 12 '15 at 10:17
  • Are you sure it should always work? `(face-remap-add-relative 'font-lock-keyword-face '((:weight 'bold)))` doesn't make it bold for me. – Hi-Angel Sep 06 '17 at 23:01
  • 1
    @Hi-Angel -- remove the single quote in front of the word `bold` and try again. :) – lawlist Sep 07 '17 at 05:46
  • Wonderful! I was ripping my hair out with frustration yesterday trying to get faces to update every time I switched buffers (incl. switching windows/frames), when what I really wanted was buffer-local face customizations. Eventually I conceived of that as an option, so now after I found yet another corner-case that caused a face update in `post-command-hook` to not actually take effect until another input key came in, web search brought me here. Immediately problem solved. – mtraceur Apr 25 '23 at 23:14
1

(edit: lawlist has a much better answer, but I'll leave this answer in case the information is useful to anyone.)

Face specs are properties in the face symbol's plist -- e.g. (get 'stripe-highlight 'face-defface-spec) -- and I don't believe the notion of buffer-local symbol properties exists. That's a special feature of the value slot (i.e. values of variables).

What some libraries do is assign their faces to variables, and then use the variable everywhere that the face is needed.

You can then create buffer-local values for those variables, which has the same effect that you're looking for.

This requires the code using the face to have been written that way, however.

phils
  • 48,657
  • 3
  • 76
  • 115