all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* how to save a face in Lisp?
@ 2007-09-30 23:35 Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2007-09-30 23:35 UTC (permalink / raw)
  To: Help-Gnu-Emacs

In Lisp, how can I save a face to the user's custom-file after it has been
changed (or to cause it to be saved when Emacs is quit)?

For a variable, it's enough to call `customize-save-variable', but I don't
know the equivalent for faces. Perhaps it would be enough to `put' the spec
as property `saved-face' for the face? Or as property `defface-spec'? I'm
not sure how this stuff works.

[Obviously, this code would only be invoked by the user, aware that s?he was
saving the changed face. I would not save settings behind the user's back.]

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

* Re: how to save a face in Lisp?
       [not found] <mailman.1515.1191195346.18990.help-gnu-emacs@gnu.org>
@ 2007-10-02  4:17 ` Steven L.
  2007-10-02 14:04   ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Steven L. @ 2007-10-02  4:17 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:
> In Lisp, how can I save a face to the user's custom-file after it has been
> changed (or to cause it to be saved when Emacs is quit)?

The function custom-set-faces works for me.


-- 
Steven L.
Email:  sdlitvin@earthlinkNOSPAM.net
Remove the NOSPAM before replying to me.

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

* RE: how to save a face in Lisp?
  2007-10-02  4:17 ` Steven L.
@ 2007-10-02 14:04   ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2007-10-02 14:04 UTC (permalink / raw)
  To: Steven L., help-gnu-emacs

> > In Lisp, how can I save a face to the user's custom-file after
> > it has been changed (or to cause it to be saved when Emacs is quit)?
>
> The function custom-set-faces works for me.

It requires the face's SPEC also as input:

  "The arguments should be a list where each entry has the form:
    (FACE SPEC [NOW [COMMENT]])
   SPEC is stored as the saved value for FACE..."

I'm looking for something that takes just the face as arg and saves it. IOW,
I don't want to define the face; I just want to save it.

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

* Re: how to save a face in Lisp?
       [not found] <mailman.1599.1191333917.18990.help-gnu-emacs@gnu.org>
@ 2007-10-03  4:24 ` Steven L.
  2007-10-03  7:41   ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Steven L. @ 2007-10-03  4:24 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Drew Adams wrote:
>>> In Lisp, how can I save a face to the user's custom-file after
>>> it has been changed (or to cause it to be saved when Emacs is quit)?
>> The function custom-set-faces works for me.
> 
> It requires the face's SPEC also as input:
> 
>   "The arguments should be a list where each entry has the form:
>     (FACE SPEC [NOW [COMMENT]])
>    SPEC is stored as the saved value for FACE..."
> 
> I'm looking for something that takes just the face as arg and saves it. IOW,
> I don't want to define the face; I just want to save it.

You don't have to define the face manually.  But you have to write Elisp 
code to find the current values of the attributes of an existing face, 
and write those values to a custom-file which can be read back the next 
time you start Emacs.

Example:  If you execute the following in a Lisp-Interaction buffer:
(describe-face 'default)
you will see all the default face's attributes whose *current* values 
you have to save to a file:  :family, :width, :height, :slant, 
:foreground, :background, etc.

A function to inspect each attribute in turn is called 'face-attribute

So again, for the default face, you can get each value:

(face-attribute 'default :family)

(face-attribute 'default :height)

(face-attribute 'default :background)

And so on.  Get the value of each attribute of the face you're 
interested in, and then write those values to a file and you can read 
them back in at any time.


-- 
Steven L.
Email:  sdlitvin@earthlinkNOSPAM.net
Remove the NOSPAM before replying to me.

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

* RE: how to save a face in Lisp?
  2007-10-03  4:24 ` Steven L.
@ 2007-10-03  7:41   ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2007-10-03  7:41 UTC (permalink / raw)
  To: help-gnu-emacs

> >>> In Lisp, how can I save a face to the user's custom-file after
> >>> it has been changed (or to cause it to be saved when Emacs is quit)?
> >>
> >> The function custom-set-faces works for me.
> >
> > It requires the face's SPEC also as input:
> >
> >   "The arguments should be a list where each entry has the form:
> >     (FACE SPEC [NOW [COMMENT]])
> >    SPEC is stored as the saved value for FACE..."
> >
> > I'm looking for something that takes just the face as arg and
> > saves it. IOW, I don't want to define the face; I just want to save it.

I could have added that, anyway, `custom-set-face' does not write to the
custom-file and save it; `custom-set-face' is just the form that needs to be
written to (or updated in) the file.

> You don't have to define the face manually.  But you have to write Elisp
> code to find the current values of the attributes of an existing face,
> and write those values to a custom-file which can be read back the next
> time you start Emacs.
>
> Example:  If you execute the following in a Lisp-Interaction buffer:
> (describe-face 'default)
> you will see all the default face's attributes whose *current* values
> you have to save to a file:  :family, :width, :height, :slant,
> :foreground, :background, etc.
>
> A function to inspect each attribute in turn is called 'face-attribute
> So again, for the default face, you can get each value:
> (face-attribute 'default :family)
> (face-attribute 'default :height)
> (face-attribute 'default :background)
>
> And so on.  Get the value of each attribute of the face you're
> interested in, and then write those values to a file and you can read
> them back in at any time.

I want to save an _arbitrary_ existing face with a Lisp function:

(defun save-face (face)
  "Update user's `custom-file' with current definition of FACE."
 ...)

Is there an existing function to do that? If not, how to go about it?

What you suggest would save the current attributes of the face. That would
be like writing `custom-set-face' with this spec: (list (list t
(custom-face-attributes-get face nil))).

What I want is to save the face spec as customized by the user (or as
defined by defface if never customized), but updated to reflect the face's
current attributes (what `custom-face-attributes-get' provides). IOW, just
what would be written in the `custom-set-faces' sexp if the face had been
modified by the user in Customize and saved by the user in Customize.

The function I'm looking for would do what `custom-save-all' does with
`custom-save-faces', but for only one face. It would of course update any
existing `custom-set-faces' in the custom-file, not add another one (there
should be only one). And it would write out the face regardless of whether
Customize thought the face had been customized - for example, even if it
were changed by `modify-face'.

There is function `custom-face-save', but it takes a widget as arg, not a
face, and it operates strictly in the context of Customize. Based on its
code, I guess I could just put nil as the face's `customized-face' property,
put its current definition as its `saved-face' property, and then call
`custom-save-all'. IIUC, that is also (analogous to) how
`customize-save-variable' works.

But how can I get that complete current definition (spec) to use for
property `saved-face'? Using `face-user-default-spec' I can get the user's
saved definition of the face (or the defface definition if there is no
custom definition). And using `face-attr-construct' I can get the face's
current global definition (which is only part of a spec, not a complete
spec). But how do I merge those two to get a complete defface-style spec
that reflects the current attribute values? That is what needs to be written
into the `custom-set-faces' sexp, AFAIK.

For example, suppose this is the definition of face `foo':

(defface foo
    '((((background dark)) (:foreground "DarkGreen"))
      (t (:foreground "Red")))
  "...")

And suppose this has been evaluated, temporarily "customizing" the face
outside of Customize:

(set-face-attribute 'foo nil :foreground "Yellow")

And suppose that the background mode is not `dark'. Then the spec that needs
to be written if this face is saved is this:

 '((((background dark)) (:foreground "DarkGreen"))
   (t (:foreground "Yellow")))

Getting that updated spec seems to be the stumbling block. (Is there a
function that gives that?) The Customize code has that ready to hand,
because of its code that handles the face change by the user.

(Also, I'd want to add some code to only save the face if it had actually
changed. And ideally I'd like something that works also with Emacs 20, which
doesn't have function `face-user-default-spec'.)

The custom code is difficult for me to follow (widgetry leaves me numb), and
this all seems terribly complicated. For a user variable, it's simple: just
call `customize-save-variable'. Why is there no `customize-save-face'? Of
course, for `customize-save-variable' too you need to pass the variable's
value to save, and the hard part for the face seems to be getting a complete
updated spec for it.

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

* Re: how to save a face in Lisp?
       [not found] <mailman.1658.1191397285.18990.help-gnu-emacs@gnu.org>
@ 2007-10-03  9:12 ` Johan Bockgård
  2007-10-03 14:55   ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Johan Bockgård @ 2007-10-03  9:12 UTC (permalink / raw)
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

> For example, suppose this is the definition of face `foo':
>
> (defface foo
>     '((((background dark)) (:foreground "DarkGreen"))
>       (t (:foreground "Red")))
>   "...")
>
> And suppose this has been evaluated, temporarily "customizing" the face
> outside of Customize:
>
> (set-face-attribute 'foo nil :foreground "Yellow")
>
> And suppose that the background mode is not `dark'. Then the spec that needs
> to be written if this face is saved is this:
>
>  '((((background dark)) (:foreground "DarkGreen"))
>    (t (:foreground "Yellow")))
>

(set-face-attribute FACE nil ...) overrides all other settings of FACE,
so I don't think the `(background dark)' part should be saved.

-- 
Johan Bockgård

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

* RE: how to save a face in Lisp?
  2007-10-03  9:12 ` how to save a face in Lisp? Johan Bockgård
@ 2007-10-03 14:55   ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2007-10-03 14:55 UTC (permalink / raw)
  To: Johan "Bockgård", help-gnu-emacs

> > For example, suppose this is the definition of face `foo':
> >
> > (defface foo
> >     '((((background dark)) (:foreground "DarkGreen"))
> >       (t (:foreground "Red")))
> >   "...")
> >
> > And suppose this has been evaluated, temporarily "customizing" the face
> > outside of Customize:
> >
> > (set-face-attribute 'foo nil :foreground "Yellow")
> >
> > And suppose that the background mode is not `dark'. Then the 
> > spec that needs to be written if this face is saved is this:
> >
> >  '((((background dark)) (:foreground "DarkGreen"))
> >    (t (:foreground "Yellow")))
> 
> (set-face-attribute FACE nil ...) overrides all other settings of FACE,
> so I don't think the `(background dark)' part should be saved.

Regardless of the fact that `set-face-attribute' does that for the current session, I want the effect to be exactly what it would be "if the face had been modified by the user in Customize and saved by the user in Customize."

But perhaps even in the latter case only the equivalent of the `set-face-attribute' effect is saved, not the whole (updated) spec. I was thinking that what was in the custom file was a complete defface spec that covers all background modes that were defined in the original defface spec, but it looks like it is just the part of that spec that the user actually customized.

However, it does include the entire alist entry (DISPLAY ATTS), not just ATTS. Here, for example, is a spec from my `custom-file':

 '(link ((((class color) (min-colors 88) (background light))
           (:foreground "blue1"))))

`face-attr-construct' gives me only ATTS. In this case, it would give me only (:foreground "blue1").

Depending on what the ATTS are, it would be possible to look up the corresponding DISPLAY in `face-user-default-spec'. Or I could try to construct DISPLAY from the current background mode and terminal type (color support etc.), but there is no way, without consulting the spec from `face-user-default-spec', to know whether DISPLAY should be `t' or, say, ((class color) (min-colors 88) (background light)).

I'm surprised that customizing in a currently light-background session would lose any dark-background information for the customized face. That doesn't seem right. You might want to use the same custom-file in different settings, with different background modes.

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

end of thread, other threads:[~2007-10-03 14:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1658.1191397285.18990.help-gnu-emacs@gnu.org>
2007-10-03  9:12 ` how to save a face in Lisp? Johan Bockgård
2007-10-03 14:55   ` Drew Adams
     [not found] <mailman.1599.1191333917.18990.help-gnu-emacs@gnu.org>
2007-10-03  4:24 ` Steven L.
2007-10-03  7:41   ` Drew Adams
     [not found] <mailman.1515.1191195346.18990.help-gnu-emacs@gnu.org>
2007-10-02  4:17 ` Steven L.
2007-10-02 14:04   ` Drew Adams
2007-09-30 23:35 Drew Adams

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.