all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Problem with defining a background color by variable
@ 2022-06-12  8:24 suzee
  2022-06-12  8:34 ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: suzee @ 2022-06-12  8:24 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I want to change the background color of code (and other) blocks in 
org-mode as proposed here:

https://orgmode.org/worg/org-contrib/babel/examples/fontify-src-code-blocks.html#org4218fd0

I use emacs with a theme (gruvbox) and when I literally use the proposed 
code

     (defface org-block-background
       '((t (:background "#FFFFEA")))
       "Face used for the source block background.")

it does work. But I want to use a color, which is derived (made darker 
or lighter) from another color by an emacs lisp function, so basically I 
want something like this:

     (defvar mycolor (color-lighten-name "navy" 20))
     (defface org-block-background
       '((t (:background mycolor)))
       "Face used for the source block background.")

However, when I try this, I get the following error when debugging:

     Debugger entered--Lisp error: (wrong-type-argument stringp mycolor)
       internal-set-lisp-face-attribute(org-block-background :background 
mycolor #<frame emacs@Machine 0x11e7c30>)
       set-face-attribute(org-block-background #<frame emacs@Machine 
0x11e7c30> :background mycolor)
       [...]

Does someone know why it fails? Is it not possible to use a variable for 
the color there?
What can I do to make it work or to achieve my goal in a different way?

I searched the docs and manuals, but didn't find a solution. I also 
checked the type of the variable 'mycolor' and it is in fact a string 
and 'stringp mycolor' returns t, so I don't understand the error 
message. But my knowledge of elisp is very sketchy, so I might miss 
something very basic.


Thanks
suzee



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with defining a background color by variable
  2022-06-12  8:24 Problem with defining a background color by variable suzee
@ 2022-06-12  8:34 ` Eli Zaretskii
  2022-06-12  8:58   ` suzee
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2022-06-12  8:34 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Sun, 12 Jun 2022 10:24:30 +0200
> From: suzee <vronk+cllbrtn.mcs@mailbox.org>
> 
> I want to change the background color of code (and other) blocks in 
> org-mode as proposed here:
> 
> https://orgmode.org/worg/org-contrib/babel/examples/fontify-src-code-blocks.html#org4218fd0
> 
> I use emacs with a theme (gruvbox) and when I literally use the proposed 
> code
> 
>      (defface org-block-background
>        '((t (:background "#FFFFEA")))
>        "Face used for the source block background.")
> 
> it does work. But I want to use a color, which is derived (made darker 
> or lighter) from another color by an emacs lisp function, so basically I 
> want something like this:
> 
>      (defvar mycolor (color-lighten-name "navy" 20))
>      (defface org-block-background
>        '((t (:background mycolor)))
>        "Face used for the source block background.")
> 
> However, when I try this, I get the following error when debugging:
> 
>      Debugger entered--Lisp error: (wrong-type-argument stringp mycolor)
>        internal-set-lisp-face-attribute(org-block-background :background 
> mycolor #<frame emacs@Machine 0x11e7c30>)
>        set-face-attribute(org-block-background #<frame emacs@Machine 
> 0x11e7c30> :background mycolor)
>        [...]
> 
> Does someone know why it fails? Is it not possible to use a variable for 
> the color there?

What is mycolor's value?  When is that value being set, relative to
the time the above defface is processed by Emacs?

> What can I do to make it work or to achieve my goal in a different way?

My suggestion is to use set-face-background instead.  defface is
effective only once, when the face is being defined.  Thereafter you
can use set-face-background to change the background color.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with defining a background color by variable
  2022-06-12  8:34 ` Eli Zaretskii
@ 2022-06-12  8:58   ` suzee
  2022-06-12  9:16     ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: suzee @ 2022-06-12  8:58 UTC (permalink / raw)
  To: help-gnu-emacs

 > What is mycolor's value?  When is that value being set, relative to
 > the time the above defface is processed by Emacs?

It's set right before the defface command and its value is #0000e6 
(checked with message outputs).  My code looks like this:

     (require 'color)
     (defvar mycolor (color-lighten-name "navy" 20))

     (message "My Color value: %s" mycolor)
     (message "My Color Type: %s" (type-of mycolor))
     (message "My Color is of string type: %s" (stringp mycolor))

     (defface org-block-background
       '((t (:background mycolor)))
       "Face used for the source block background.")

     (require 'org)


 >> What can I do to make it work or to achieve my goal in a different way?
 >
 > My suggestion is to use set-face-background instead.  defface is
 > effective only once, when the face is being defined.  Thereafter you
 > can use set-face-background to change the background color.

Ah awesome!  The following does in fact work:

     (set-face-attribute 'org-block-background mycolor)

And it's nice that it allows to change it at any moment.  (I only found 
set-face-attribute by googling and couldn't make it work.)

Thanks a lot! :)

I'd be happy to understand the error message though to gain some 
knowledge about elisp.  Any idea about that?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with defining a background color by variable
  2022-06-12  8:58   ` suzee
@ 2022-06-12  9:16     ` Eli Zaretskii
  2022-06-12  9:41       ` suzee
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2022-06-12  9:16 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Sun, 12 Jun 2022 10:58:35 +0200
> From: suzee <vronk+cllbrtn.mcs@mailbox.org>
> 
>      (require 'color)
>      (defvar mycolor (color-lighten-name "navy" 20))
> 
>      (message "My Color value: %s" mycolor)
>      (message "My Color Type: %s" (type-of mycolor))
>      (message "My Color is of string type: %s" (stringp mycolor))
> 
>      (defface org-block-background
>        '((t (:background mycolor)))
>        "Face used for the source block background.")
> 
>      (require 'org)
> 
> 
>  >> What can I do to make it work or to achieve my goal in a different way?
>  >
>  > My suggestion is to use set-face-background instead.  defface is
>  > effective only once, when the face is being defined.  Thereafter you
>  > can use set-face-background to change the background color.
> 
> Ah awesome!  The following does in fact work:
> 
>      (set-face-attribute 'org-block-background mycolor)
> 
> And it's nice that it allows to change it at any moment.  (I only found 
> set-face-attribute by googling and couldn't make it work.)
> 
> Thanks a lot! :)
> 
> I'd be happy to understand the error message though to gain some 
> knowledge about elisp.  Any idea about that?

I think it's because of the quoting: the entire list is quoted, so the
value of mycolor is not evaluated.  You need to use a smarter quoting,
something like

   `((t (:background ,mycolor)))



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with defining a background color by variable
  2022-06-12  9:16     ` Eli Zaretskii
@ 2022-06-12  9:41       ` suzee
  0 siblings, 0 replies; 5+ messages in thread
From: suzee @ 2022-06-12  9:41 UTC (permalink / raw)
  To: help-gnu-emacs

On 12.06.22 11:16, Eli Zaretskii wrote:
>> I'd be happy to understand the error message though to gain some
>> knowledge about elisp.  Any idea about that?
> 
> I think it's because of the quoting: the entire list is quoted, so the
> value of mycolor is not evaluated.  You need to use a smarter quoting,
> something like
> 
>     `((t (:background ,mycolor)))
> 

Ah yeah that works.  I still have to get my head around the quoting.

Thanks for your help!  Problem solved :]



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-12  9:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-12  8:24 Problem with defining a background color by variable suzee
2022-06-12  8:34 ` Eli Zaretskii
2022-06-12  8:58   ` suzee
2022-06-12  9:16     ` Eli Zaretskii
2022-06-12  9:41       ` suzee

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.