unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Is there a way of setting a variable only when it exists?
@ 2022-03-14  6:00 Marcin Borkowski
  2022-03-14 12:52 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-14  6:00 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

I'd like to be able to say something like

(setq-safe hello "world")

so that `hello' is set to `"world"' if it is an existing variable (e.g.,
defined by `defvar') and an error is raised if `hello' does not exist.

Rationale: I'm setting an internal Emacs variable.  At the same time,
I submitted a feature request to turn it into a user option, so that
it's possible that in a future version of Emacs it's going to change its
name (it has two dashes in it).  I want to be warned then that I need to
change my `init.el' accordingly.

I assume it would be fairly easy to code such a macro (using `boundp'),
but maybe it exists already?

TIA,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14  6:00 Is there a way of setting a variable only when it exists? Marcin Borkowski
@ 2022-03-14 12:52 ` Eli Zaretskii
  2022-03-14 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-14 22:09 ` Michael Heerdegen
  2 siblings, 0 replies; 37+ messages in thread
From: Eli Zaretskii @ 2022-03-14 12:52 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Mon, 14 Mar 2022 07:00:33 +0100
> 
> I'd like to be able to say something like
> 
> (setq-safe hello "world")
> 
> so that `hello' is set to `"world"' if it is an existing variable (e.g.,
> defined by `defvar') and an error is raised if `hello' does not exist.
> 
> Rationale: I'm setting an internal Emacs variable.  At the same time,
> I submitted a feature request to turn it into a user option, so that
> it's possible that in a future version of Emacs it's going to change its
> name (it has two dashes in it).  I want to be warned then that I need to
> change my `init.el' accordingly.
> 
> I assume it would be fairly easy to code such a macro (using `boundp'),
> but maybe it exists already?

Why not just use boundp?



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14  6:00 Is there a way of setting a variable only when it exists? Marcin Borkowski
  2022-03-14 12:52 ` Eli Zaretskii
@ 2022-03-14 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-14 13:48   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-14 22:09 ` Michael Heerdegen
  2 siblings, 1 reply; 37+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-03-14 13:16 UTC (permalink / raw)
  To: help-gnu-emacs

> I assume it would be fairly easy to code such a macro (using `boundp'),
> but maybe it exists already?

AFAIK it doesn't exist yet.  The reason for it is that it is not
often useful.  Typically there are two cases:

- If the var exist, you want to set it and if not you have no fallback.
  In that case, it is typically harmless to set the var even when it
  doesn't exist, so the code just uses `setq` without bothering to test
  `boundp`.

- If the var exist you want to set it, and if it doesn't you want to do
  something else.  In that case, the something else tends to depend on
  the specifics so (if (boundp 'foo) (setq foo ..) ...) is about s good
  as it gets.


-- Stefan




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-14 13:48   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-15  6:17     ` Marcin Borkowski
  2022-03-15  6:48     ` Jean Louis
  0 siblings, 2 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-14 13:48 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

>> I assume it would be fairly easy to code such a macro
>> (using `boundp'), but maybe it exists already?
>
> AFAIK it doesn't exist yet. The reason for it is that it is
> not often useful. Typically there are two cases:
>
> - If the var exist, you want to set it and if not you have
>   no fallback. In that case, it is typically harmless to set
>   the var even when it doesn't exist, so the code just uses
>   `setq` without bothering to test `boundp`.
>
> - If the var exist you want to set it, and if it doesn't you
>   want to do something else. In that case, the something
>   else tends to depend on the specifics so (if (boundp 'foo)
>   (setq foo ..) ...) is about s good as it gets.

If it exists set it with `setq'. If it doesn't exist, create
and set it ... with `setq'?

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14  6:00 Is there a way of setting a variable only when it exists? Marcin Borkowski
  2022-03-14 12:52 ` Eli Zaretskii
  2022-03-14 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-14 22:09 ` Michael Heerdegen
  2022-03-15  6:16   ` Marcin Borkowski
  2 siblings, 1 reply; 37+ messages in thread
From: Michael Heerdegen @ 2022-03-14 22:09 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> I'd like to be able to say something like
> 
> (setq-safe hello "world")
> 
> so that `hello' is set to `"world"' if it is an existing variable (e.g.,
> defined by `defvar') and an error is raised if `hello' does not exist.

Byte compiling can help with things like name changes.

> I assume it would be fairly easy to code such a macro (using `boundp'),

Maybe rather using `special-variable-p'?

Michael.




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14 22:09 ` Michael Heerdegen
@ 2022-03-15  6:16   ` Marcin Borkowski
  2022-03-15  7:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-16  0:25     ` Michael Heerdegen
  0 siblings, 2 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-15  6:16 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2022-03-14, at 23:09, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> I'd like to be able to say something like
>> 
>> (setq-safe hello "world")
>> 
>> so that `hello' is set to `"world"' if it is an existing variable (e.g.,
>> defined by `defvar') and an error is raised if `hello' does not exist.
>
> Byte compiling can help with things like name changes.

Not necessarily.  My use-case is an internal Emacs variable I'm setting
in my `init.el'; at the same time I submitted a bug report/feature
request to make it a user option, so I assume that it might change its
name in a future Emacs version.

>> I assume it would be fairly easy to code such a macro (using `boundp'),
>
> Maybe rather using `special-variable-p'?

Might be a good idea in this case, yes.

Thanks,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14 13:48   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-15  6:17     ` Marcin Borkowski
  2022-03-15  6:50       ` Emanuel Berg via Users list for the GNU Emacs text editor
                         ` (4 more replies)
  2022-03-15  6:48     ` Jean Louis
  1 sibling, 5 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-15  6:17 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


On 2022-03-14, at 14:48, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>
>>> I assume it would be fairly easy to code such a macro
>>> (using `boundp'), but maybe it exists already?
>>
>> AFAIK it doesn't exist yet. The reason for it is that it is
>> not often useful. Typically there are two cases:
>>
>> - If the var exist, you want to set it and if not you have
>>   no fallback. In that case, it is typically harmless to set
>>   the var even when it doesn't exist, so the code just uses
>>   `setq` without bothering to test `boundp`.
>>
>> - If the var exist you want to set it, and if it doesn't you
>>   want to do something else. In that case, the something
>>   else tends to depend on the specifics so (if (boundp 'foo)
>>   (setq foo ..) ...) is about s good as it gets.
>
> If it exists set it with `setq'. If it doesn't exist, create
> and set it ... with `setq'?

What if it's an internal Emacs variable which might become a user option
one day (I submitted a bug report about it) and then my customization
silently disappears?  It's the "silently" part I want to guard
against...

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-14 13:48   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-15  6:17     ` Marcin Borkowski
@ 2022-03-15  6:48     ` Jean Louis
  1 sibling, 0 replies; 37+ messages in thread
From: Jean Louis @ 2022-03-15  6:48 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2022-03-15 06:16]:
> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
> 
> >> I assume it would be fairly easy to code such a macro
> >> (using `boundp'), but maybe it exists already?
> >
> > AFAIK it doesn't exist yet. The reason for it is that it is
> > not often useful. Typically there are two cases:
> >
> > - If the var exist, you want to set it and if not you have
> >   no fallback. In that case, it is typically harmless to set
> >   the var even when it doesn't exist, so the code just uses
> >   `setq` without bothering to test `boundp`.
> >
> > - If the var exist you want to set it, and if it doesn't you
> >   want to do something else. In that case, the something
> >   else tends to depend on the specifics so (if (boundp 'foo)
> >   (setq foo ..) ...) is about s good as it gets.
> 
> If it exists set it with `setq'. If it doesn't exist, create
> and set it ... with `setq'?

Hahahahha -- exactly.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:17     ` Marcin Borkowski
@ 2022-03-15  6:50       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-15  6:58       ` Jean Louis
                         ` (3 subsequent siblings)
  4 siblings, 0 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-15  6:50 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

>>>   want to do something else. In that case, the something
>>>   else tends to depend on the specifics so (if (boundp
>>>   'foo) (setq foo ..) ...) is about s good as it gets.
>>
>> If it exists set it with `setq'. If it doesn't exist,
>> create and set it ... with `setq'?
>
> What if it's an internal Emacs variable which might become
> a user option one day (I submitted a bug report about it)
> and then my customization silently disappears? It's the
> "silently" part I want to guard against...

Yeah, that whole system ... *mumble*

But here you go,

(let ((var (variable-at-point t))) ; ANY-SYMBOL
  (when (and (custom-variable-p var)
             (get var 'custom-set) )
    (message "user variable") ))

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:17     ` Marcin Borkowski
  2022-03-15  6:50       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-15  6:58       ` Jean Louis
  2022-03-15  7:45         ` Marcin Borkowski
                           ` (2 more replies)
  2022-03-15 15:38       ` Drew Adams
                         ` (2 subsequent siblings)
  4 siblings, 3 replies; 37+ messages in thread
From: Jean Louis @ 2022-03-15  6:58 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs, Emanuel Berg

* Marcin Borkowski <mbork@mbork.pl> [2022-03-15 09:28]:
> What if it's an internal Emacs variable which might become a user option
> one day (I submitted a bug report about it) and then my customization
> silently disappears?  It's the "silently" part I want to guard
> against...

I am trying to understand:

- what is user option?

- you probably mean a variable that cannot be customized by M-x customize?

- because every Emacs variable is user option for me, anything can be
  customized; I do not know if there are any static variables that
  cannot be changed, probably.

Example: (setq emacs-version 1) ⇒ 1 -- so I can change even Emacs version.

Now, if variable becomes in future "user option" most probably your
settings will still be kept in place, your customization will not
silently disappear unless you delete it from your files.

Did I understand it all well?


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:16   ` Marcin Borkowski
@ 2022-03-15  7:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-16  0:25     ` Michael Heerdegen
  1 sibling, 0 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-15  7:05 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

>>> so that `hello' is set to `"world"' if it is an existing
>>> variable (e.g., defined by `defvar') and an error is
>>> raised if `hello' does not exist.
>>
>> Byte compiling can help with things like name changes.
>
> Not necessarily. My use-case is an internal Emacs variable
> I'm setting in my `init.el'; at the same time I submitted
> a bug report/feature request to make it a user option, so
> I assume that it might change its name in a future
> Emacs version.

If that variable change its name the byte-compiler will warn
you since you will be shooting blanks.

Here, try type:

  (setq no-such-var 'oh-la-la)

then byte-compile, the byte-compiler will say:

  In toplevel form:
  geh.el:19:7: Warning: assignment to free variable ‘no-such-var’

BTW I wrote a program ages ago [1] and it tells me in 152
Elisp files just a small bunch of very few variables that I've
ever used `setf' or `setq' on

;; .emacs.el, 204: show-paren-delay
;; erc-incal.el, 34: erc-user-full-name
;; erc-incal.el, 77: erc-header-line-format
;; spell.el, 17: ispell-program-name
;; w3m-incal.el, 26: w3m-tab-width

are actually options.

I don't remember anymore why that was important to know?

I didn't like custom or options back then (and still don't) so
maybe an attitude/political thing but ... yeah, what?

[1] https://dataswamp.org/~incal/emacs-init/custom-vars.el

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:58       ` Jean Louis
@ 2022-03-15  7:45         ` Marcin Borkowski
  2022-03-15  8:12           ` Jean Louis
  2022-03-15  7:49         ` tomas
  2022-03-15 15:33         ` [External] : " Drew Adams
  2 siblings, 1 reply; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-15  7:45 UTC (permalink / raw)
  To: Jean Louis; +Cc: help-gnu-emacs, Emanuel Berg


On 2022-03-15, at 07:58, Jean Louis <bugs@gnu.support> wrote:

> * Marcin Borkowski <mbork@mbork.pl> [2022-03-15 09:28]:
>> What if it's an internal Emacs variable which might become a user option
>> one day (I submitted a bug report about it) and then my customization
>> silently disappears?  It's the "silently" part I want to guard
>> against...
>
> I am trying to understand:
>
> - what is user option?

It is defined in the Emacs manual.

> Now, if variable becomes in future "user option" most probably your
> settings will still be kept in place, your customization will not
> silently disappear unless you delete it from your files.
>
> Did I understand it all well?

No.  If an internal variable (= one whose name contains two dashes)
becomes a user option, its name will most probably change.

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:58       ` Jean Louis
  2022-03-15  7:45         ` Marcin Borkowski
@ 2022-03-15  7:49         ` tomas
  2022-03-15  8:13           ` Jean Louis
  2022-03-15  8:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-15 15:33         ` [External] : " Drew Adams
  2 siblings, 2 replies; 37+ messages in thread
From: tomas @ 2022-03-15  7:49 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: Emanuel Berg

[-- Attachment #1: Type: text/plain, Size: 1213 bytes --]

On Tue, Mar 15, 2022 at 09:58:25AM +0300, Jean Louis wrote:

[...]

> I am trying to understand:
> 
> - what is user option?

Small extract from the Fine Emacs Lisp manual:

  15.3 Defining Customization Variables
  =====================================

  “Customizable variables”, also called “user options”, are global
  Lisp variables whose values can be set through the Customize
  interface.  Unlike other global variables, which are defined
  with ‘defvar’ (*note Defining Variables::), customizable variables
  are defined using the ‘defcustom’ macro.  In addition to calling
  ‘defvar’ as a subroutine, ‘defcustom’ states how the variable
  should be displayed in the Customize interface, the values it is
  allowed to take, etc.

(C-h i is your friend there).

> - you probably mean a variable that cannot be customized by M-x customize?
> 
> - because every Emacs variable is user option for me, anything can be
>   customized; I do not know if there are any static variables that
>   cannot be changed, probably.

You can, of course, make up your own terminology. But this might make
communication wit others... interesting :)

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  7:45         ` Marcin Borkowski
@ 2022-03-15  8:12           ` Jean Louis
  0 siblings, 0 replies; 37+ messages in thread
From: Jean Louis @ 2022-03-15  8:12 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs, Emanuel Berg

* Marcin Borkowski <mbork@mbork.pl> [2022-03-15 10:45]:
> 
> On 2022-03-15, at 07:58, Jean Louis <bugs@gnu.support> wrote:
> 
> > * Marcin Borkowski <mbork@mbork.pl> [2022-03-15 09:28]:
> >> What if it's an internal Emacs variable which might become a user option
> >> one day (I submitted a bug report about it) and then my customization
> >> silently disappears?  It's the "silently" part I want to guard
> >> against...
> >
> > I am trying to understand:
> >
> > - what is user option?
> 
> It is defined in the Emacs manual.

,----
| User Option
|      A user option is a face (q.v.) or a variable (q.v.) that exists so
|      that you can customize Emacs by setting it to a new value.  *Note
|      Easy Customization::.
`----

Definition in glossary is incomplete, as one can fully understand it
only by reading Easy Customization:

> 49.1 Easy Customization Interface
> =================================

> Emacs has many “settings” which you can change.  Most settings are
> “customizable variables” (*note Variables::), which are also called
> “user options”. 

Thus what you mean are customizable variables.

> > Now, if variable becomes in future "user option" most probably your
> > settings will still be kept in place, your customization will not
> > silently disappear unless you delete it from your files.
> >
> > Did I understand it all well?
> 
> No.  If an internal variable (= one whose name contains two dashes)
> becomes a user option, its name will most probably change.

That is what you mean. That is harder to track. You would need to
watch NEWS, manual and `git log' to track such changes.

I am using database and I have some variables used in HTML templates
where by column variables_name has its name and variables_value has
the value. Though each variables has its unique ID. This enables the
possibility to change the variables_name to anything else, and still
retain the tracking to its original ID. Finding previous variable is
obtained by using rudimentary database backed version control system.
Though that is quite different concept, though very clear and
well structured.

Emacs does not have structured way to remember all variables ever
defined so to track them, then to track their previous names and so
on. 

One way to go would be by using recently included SQLite database in
Emacs Development version.

If every internal variable would get its unique ID in the SQLite
database, then renaming variable would be very easy, and its tracking
would become easier. Though it is a different concept that is not live
yet.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  7:49         ` tomas
@ 2022-03-15  8:13           ` Jean Louis
  2022-03-15  8:36             ` tomas
  2022-03-15  8:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 37+ messages in thread
From: Jean Louis @ 2022-03-15  8:13 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs, Emanuel Berg

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-03-15 10:50]:
> > - because every Emacs variable is user option for me, anything can be
> >   customized; I do not know if there are any static variables that
> >   cannot be changed, probably.
> 
> You can, of course, make up your own terminology. But this might make
> communication wit others... interesting :)

No, it was misunderstanding on my side. Now it is clear.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  7:49         ` tomas
  2022-03-15  8:13           ` Jean Louis
@ 2022-03-15  8:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-15  8:14 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

> "Customizable variables", also called "user options", are
> global Lisp variables whose values can be set through the
> Customize interface. Unlike other global variables, which
> are defined with `defvar' [...] customizable variables are
> defined using the `defcustom' macro. In addition to calling
> `defvar' as a subroutine, `defcustom' states how the
> variable should be displayed in the Customize interface, the
> values it is allowed to take, etc.

So an interface is used so that an interface can be displayed
... and those interfaces are ... the same?

You hear how it sounds.

Hands of data! There can be many ways to set data. But after
the data is set nothing should be left _how_ and no special
treatment should later be allowed based on what interface was
used ...

interface |~| data |#| programming logic (code)
           ↑        ↑
           ↑        ↑
           ↑   reinforced
           ↑    concrete
           ↑      wall
         brick
         wall

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  8:13           ` Jean Louis
@ 2022-03-15  8:36             ` tomas
  2022-03-15  9:28               ` Jean Louis
  0 siblings, 1 reply; 37+ messages in thread
From: tomas @ 2022-03-15  8:36 UTC (permalink / raw)
  To: help-gnu-emacs, Emanuel Berg

[-- Attachment #1: Type: text/plain, Size: 591 bytes --]

On Tue, Mar 15, 2022 at 11:13:19AM +0300, Jean Louis wrote:
> * tomas@tuxteam.de <tomas@tuxteam.de> [2022-03-15 10:50]:
> > > - because every Emacs variable is user option for me, anything can be
> > >   customized; I do not know if there are any static variables that
> > >   cannot be changed, probably.
> > 
> > You can, of course, make up your own terminology. But this might make
> > communication wit others... interesting :)
> 
> No, it was misunderstanding on my side. Now it is clear.

Understood. Perhaps that was a lame attempt at humour on my part.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  8:36             ` tomas
@ 2022-03-15  9:28               ` Jean Louis
  2022-03-15 10:01                 ` tomas
  2022-03-15 11:29                 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 37+ messages in thread
From: Jean Louis @ 2022-03-15  9:28 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs, Emanuel Berg

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-03-15 11:38]:
> On Tue, Mar 15, 2022 at 11:13:19AM +0300, Jean Louis wrote:
> > * tomas@tuxteam.de <tomas@tuxteam.de> [2022-03-15 10:50]:
> > > > - because every Emacs variable is user option for me, anything can be
> > > >   customized; I do not know if there are any static variables that
> > > >   cannot be changed, probably.
> > > 
> > > You can, of course, make up your own terminology. But this might make
> > > communication wit others... interesting :)
> > 
> > No, it was misunderstanding on my side. Now it is clear.
> 
> Understood. Perhaps that was a lame attempt at humour on my part.

I totally understand it, people introducing their own understanding of
definitions will have troubles communicating with others, it causes
conflicts.

First we have to agree on what is the accepted definition. There is
nothing wrong in changing such definitions, though it goes gradually
again with agreements among people.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  9:28               ` Jean Louis
@ 2022-03-15 10:01                 ` tomas
  2022-03-15 11:29                 ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 37+ messages in thread
From: tomas @ 2022-03-15 10:01 UTC (permalink / raw)
  To: help-gnu-emacs, Emanuel Berg

[-- Attachment #1: Type: text/plain, Size: 128 bytes --]

On Tue, Mar 15, 2022 at 12:28:25PM +0300, Jean Louis wrote:

[...]

> I totally understand it [...]

Thanks :)

-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  9:28               ` Jean Louis
  2022-03-15 10:01                 ` tomas
@ 2022-03-15 11:29                 ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-15 11:29 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>> Understood. Perhaps that was a lame attempt at humour on
>> my part.
>
> I totally understand it, people introducing their own
> understanding of definitions will have troubles
> communicating with others, it causes conflicts.

...

> First we have to agree on what is the accepted definition.
> There is nothing wrong in changing such definitions, though
> it goes gradually again with agreements among people.

Haha :)

Anyway, here is some custom code I found by doing
C-h f defcustom RET TAB RET ...
(I don't have to do the TAB LOL.
  https://dataswamp.org/~incal/emacs-init/help-incal.el )

OK, here is the custom code. Does it look like anything you
want to hand over your data to for safe-and-sound handling?

Probably the best interface in the world?

We came out of a crazy mind - and walked out on a piece of
paper ...

(defun custom-declare-variable (symbol default doc &rest args)
  "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
DEFAULT should be an expression to evaluate to compute the default value,
not the default value itself.

DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
`standard-value'.  At the same time, SYMBOL's property `force-value' is
set to nil, as the value is no longer rogue."
  (put symbol 'standard-value (purecopy (list default)))
  ;; Maybe this option was rogue in an earlier version.  It no longer is.
  (when (get symbol 'force-value)
    (put symbol 'force-value nil))
  (if (keywordp doc)
      (error "Doc string is missing"))
  (let ((initialize #'custom-initialize-reset)
        (requests nil)
        ;; Whether automatically buffer-local.
        buffer-local)
    (unless (memq :group args)
      (let ((cg (custom-current-group)))
        (when cg
          (custom-add-to-group cg symbol 'custom-variable))))
    (while args
      (let ((keyword (pop args)))
	(unless (symbolp keyword)
	  (error "Junk in args %S" args))
        (unless args
          (error "Keyword %s is missing an argument" keyword))
	(let ((value (pop args)))
          ;; Can't use `pcase' because it is loaded after `custom.el'
          ;; during bootstrap.  See `loadup.el'.
	  (cond ((eq keyword :initialize)
		 (setq initialize value))
		((eq keyword :set)
		 (put symbol 'custom-set value))
		((eq keyword :get)
		 (put symbol 'custom-get value))
		((eq keyword :require)
		 (push value requests))
		((eq keyword :risky)
		 (put symbol 'risky-local-variable value))
		((eq keyword :safe)
		 (put symbol 'safe-local-variable value))
                ((eq keyword :local)
                 (when (memq value '(t permanent))
                   (setq buffer-local t))
                 (when (eq value 'permanent)
                   (put symbol 'permanent-local t)))
		((eq keyword :type)
		 (put symbol 'custom-type (purecopy value)))
		((eq keyword :options)
		 (if (get symbol 'custom-options)
		     ;; Slow safe code to avoid duplicates.
		     (mapc (lambda (option)
			     (custom-add-option symbol option))
			   value)
		   ;; Fast code for the common case.
		   (put symbol 'custom-options (copy-sequence value))))
		(t
		 (custom-handle-keyword symbol keyword value
					'custom-variable))))))
    ;; Set the docstring, record the var on load-history, as well
    ;; as set the special-variable-p flag.
    (internal--define-uninitialized-variable symbol doc)
    (put symbol 'custom-requests requests)
    ;; Do the actual initialization.
    (unless custom-dont-initialize
      (funcall initialize symbol default)
      ;; If there is a value under saved-value that wasn't saved by the user,
      ;; reset it: we used that property to stash the value, but we don't need
      ;; it anymore.
      ;; This can happen given the following:
      ;; 1. The user loaded a theme that had a setting for an unbound
      ;; variable, so we stashed the theme setting under the saved-value
      ;; property in `custom-theme-recalc-variable'.
      ;; 2. Then, Emacs evaluated the defcustom for the option
      ;; (e.g., something required the file where the option is defined).
      ;; If we don't reset it and the user later sets this variable via
      ;; Customize, we might end up saving the theme setting in the custom-file.
      ;; See the test `custom-test-no-saved-value-after-customizing-option'.
      (let ((theme (caar (get symbol 'theme-value))))
        (when (and theme (not (eq theme 'user)) (get symbol 'saved-value))
          (put symbol 'saved-value nil))))
    (when buffer-local
      (make-variable-buffer-local symbol)))
  (run-hooks 'custom-define-hook)
  symbol)

(defmacro defcustom (symbol standard doc &rest args)
  "Declare SYMBOL as a customizable variable.
SYMBOL is the variable name; it should not be quoted.
STANDARD is an expression specifying the variable's standard
value.  It should not be quoted.  It is evaluated once by
`defcustom', and the value is assigned to SYMBOL if the variable
is unbound.  The expression itself is also stored, so that
Customize can re-evaluate it later to get the standard value.
DOC is the variable documentation.

This macro uses `defvar' as a subroutine, which also marks the
variable as \"special\", so that it is always dynamically bound
even when `lexical-binding' is t.

The remaining arguments to `defcustom' should have the form

   [KEYWORD VALUE]...

The following keywords are meaningful:

:type	VALUE should be a widget type for editing the symbol's value.
	Every `defcustom' should specify a value for this keyword.
        See Info node `(elisp) Customization Types' for a list of
        base types and useful composite types.
:options VALUE should be a list of valid members of the widget type.
:initialize
	VALUE should be a function used to initialize the
	variable.  It takes two arguments, the symbol and value
	given in the `defcustom' call.  The default is
	`custom-initialize-reset'.
:set	VALUE should be a function to set the value of the symbol
	when using the Customize user interface.  It takes two arguments,
	the symbol to set and the value to give it.  The function should
	not modify its value argument destructively.  The default choice
	of function is `set-default'.
:get	VALUE should be a function to extract the value of symbol.
	The function takes one argument, a symbol, and should return
	the current value for that symbol.  The default choice of function
	is `default-value'.
:require
	VALUE should be a feature symbol.  If you save a value
	for this option, then when your init file loads the value,
	it does (require VALUE) first.
:set-after VARIABLES
	Specifies that SYMBOL should be set after the list of variables
        VARIABLES when both have been customized.
:risky	Set SYMBOL's `risky-local-variable' property to VALUE.
:safe	Set SYMBOL's `safe-local-variable' property to VALUE.
        See Info node `(elisp) File Local Variables'.
:local  If VALUE is t, mark SYMBOL as automatically buffer-local.
        If VALUE is `permanent', also set SYMBOL's `permanent-local'
        property to t.

The following common keywords are also meaningful.

:group  VALUE should be a customization group.
        Add SYMBOL (or FACE with `defface') to that group.
:link LINK-DATA
        Include an external link after the documentation string for this
        item.  This is a sentence containing an active field which
        references some other documentation.

        There are several alternatives you can use for LINK-DATA:

        (custom-manual INFO-NODE)
             Link to an Info node; INFO-NODE is a string which specifies
             the node name, as in \"(emacs)Top\".

        (info-link INFO-NODE)
             Like `custom-manual' except that the link appears in the
             customization buffer with the Info node name.

        (url-link URL)
             Link to a web page; URL is a string which specifies the URL.

        (emacs-commentary-link LIBRARY)
             Link to the commentary section of LIBRARY.

        (emacs-library-link LIBRARY)
             Link to an Emacs Lisp LIBRARY file.

        (file-link FILE)
             Link to FILE.

        (function-link FUNCTION)
             Link to the documentation of FUNCTION.

        (variable-link VARIABLE)
             Link to the documentation of VARIABLE.

        (custom-group-link GROUP)
             Link to another customization GROUP.

        You can specify the text to use in the customization buffer by
        adding `:tag NAME' after the first element of the LINK-DATA; for
        example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
        Emacs manual which appears in the buffer as `foo'.

        An item can have more than one external link; however, most items
        have none at all.
:version
        VALUE should be a string specifying that the variable was
        first introduced, or its default value was changed, in Emacs
        version VERSION.
:package-version
        VALUE should be a list with the form (PACKAGE . VERSION)
        specifying that the variable was first introduced, or its
        default value was changed, in PACKAGE version VERSION.  This
        keyword takes priority over :version.  For packages which
        are bundled with Emacs releases, the PACKAGE and VERSION
        must appear in the alist `customize-package-emacs-version-alist'.
        Since PACKAGE must be unique and the user might see it in an
        error message, a good choice is the official name of the
        package, such as MH-E or Gnus.
:tag LABEL
        Use LABEL, a string, instead of the item's name, to label the item
        in customization menus and buffers.
:load FILE
        Load file FILE (a string) before displaying this customization
        item.  Loading is done with `load', and only if the file is
        not already loaded.

If SYMBOL has a local binding, then this form affects the local
binding.  This is normally not what you want.  Thus, if you need
to load a file defining variables with this form, or with
`defvar' or `defconst', you should always load that file
_outside_ any bindings for these variables.  (`defvar' and
`defconst' behave similarly in this respect.)

This macro calls `custom-declare-variable'.  If you want to
programmatically alter a customizable variable (for instance, to
write a package that extends the syntax of a variable), you can
call that function directly.

See Info node `(elisp) Customization' in the Emacs Lisp manual
for more information."
  (declare (doc-string 3) (debug (name body))
           (indent defun))
  ;; It is better not to use backquote in this file,
  ;; because that makes a bootstrapping problem
  ;; if you need to recompile all the Lisp files using interpreted code.
  `(custom-declare-variable
    ',symbol
    ,(if lexical-binding
         ;; The STANDARD arg should be an expression that evaluates to
         ;; the standard value.  The use of `eval' for it is spread
         ;; over many different places and hence difficult to
         ;; eliminate, yet we want to make sure that the `standard'
         ;; expression is checked by the byte-compiler, and that
         ;; lexical-binding is obeyed, so quote the expression with
         ;; `lambda' rather than with `quote'.
         ``(funcall #',(lambda () "" ,standard))
       `',standard)
    ,doc
    ,@args))

-- 
underground experts united
https://dataswamp.org/~incal




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

* RE: [External] : Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:58       ` Jean Louis
  2022-03-15  7:45         ` Marcin Borkowski
  2022-03-15  7:49         ` tomas
@ 2022-03-15 15:33         ` Drew Adams
  2 siblings, 0 replies; 37+ messages in thread
From: Drew Adams @ 2022-03-15 15:33 UTC (permalink / raw)
  To: Jean Louis, Marcin Borkowski; +Cc: help-gnu-emacs@gnu.org, Emanuel Berg

> - what is user option?

C-h r
g Glossary
C-s User Option

  User Option
     A user option is a face (q.v.) or a variable (q.v.) that exists so
     that you can customize Emacs by setting it to a new value.  *Note
     Easy Customization::.

Personally, I think that definition isn't up-to-date.

Most places in Emacs, "option", aka "user option", aka
"user variable", aka "custom variable" means what you
can customize using `M-x customize-option'.  That is,
faces are not normally considered options.

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

* RE: [External] : Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:17     ` Marcin Borkowski
  2022-03-15  6:50       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-15  6:58       ` Jean Louis
@ 2022-03-15 15:38       ` Drew Adams
  2022-03-16 18:32         ` Marcin Borkowski
  2022-03-16  0:47       ` Michael Heerdegen
  2022-03-16  6:02       ` Tomas Nordin
  4 siblings, 1 reply; 37+ messages in thread
From: Drew Adams @ 2022-03-15 15:38 UTC (permalink / raw)
  To: Marcin Borkowski, Emanuel Berg; +Cc: help-gnu-emacs@gnu.org

> What if it's an internal Emacs variable which might become a user
> option one day (I submitted a bug report about it) and then my
> customization silently disappears?  It's the "silently" part I
> want to guard against...

What do you mean by "an internal Emacs variable"?

If it's a defvar then Emacs's predefined defvar
has no effect if you defvar the variable before
Emacs does.  And it has no effect if you setq
the var before or after Emacs defvars it (as long
as Emacs doesn't _use_ it before you setq it).

If Emacs later changes it to a defcustom, the
same things apply.  However, if the defcustom has
a :set function then your own setting of it might
no longer be appropriate ("valid").



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:16   ` Marcin Borkowski
  2022-03-15  7:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-16  0:25     ` Michael Heerdegen
  2022-03-16 18:33       ` Marcin Borkowski
  1 sibling, 1 reply; 37+ messages in thread
From: Michael Heerdegen @ 2022-03-16  0:25 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> Not necessarily.  My use-case is an internal Emacs variable I'm setting
> in my `init.el'; at the same time I submitted a bug report/feature
> request to make it a user option, so I assume that it might change its
> name in a future Emacs version.

If that happens, compiling your init.el will then normally give you an
"assignment to free variable" warning, no?

Michael.



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:17     ` Marcin Borkowski
                         ` (2 preceding siblings ...)
  2022-03-15 15:38       ` Drew Adams
@ 2022-03-16  0:47       ` Michael Heerdegen
  2022-03-16 18:33         ` Marcin Borkowski
  2022-03-16  6:02       ` Tomas Nordin
  4 siblings, 1 reply; 37+ messages in thread
From: Michael Heerdegen @ 2022-03-16  0:47 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> What if it's an internal Emacs variable which might become a user option
> one day (I submitted a bug report about it) and then my customization
> silently disappears?

That normally does not happen.  `defcustom' doesn't alter existing
bindings.  You can still `setq' a user option.  There is no problem
unless the "meaning" of the variable values changed in an backward
incompatible way.  I guess nothing can protect against that.

The only notable case I can think of is when things change so that
setting the variable doesn't have the expected effect any more because
the custom setter has gotten additional side effects (more than only
setting the value).  Is that your concern?

Michael.




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-15  6:17     ` Marcin Borkowski
                         ` (3 preceding siblings ...)
  2022-03-16  0:47       ` Michael Heerdegen
@ 2022-03-16  6:02       ` Tomas Nordin
  2022-03-16  6:37         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-16 18:34         ` Marcin Borkowski
  4 siblings, 2 replies; 37+ messages in thread
From: Tomas Nordin @ 2022-03-16  6:02 UTC (permalink / raw)
  To: Marcin Borkowski, Emanuel Berg; +Cc: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> On 2022-03-14, at 14:48, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
>> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>>
>>>> I assume it would be fairly easy to code such a macro
>>>> (using `boundp'), but maybe it exists already?
>>>
>>> AFAIK it doesn't exist yet. The reason for it is that it is
>>> not often useful. Typically there are two cases:
>>>
>>> - If the var exist, you want to set it and if not you have
>>>   no fallback. In that case, it is typically harmless to set
>>>   the var even when it doesn't exist, so the code just uses
>>>   `setq` without bothering to test `boundp`.
>>>
>>> - If the var exist you want to set it, and if it doesn't you
>>>   want to do something else. In that case, the something
>>>   else tends to depend on the specifics so (if (boundp 'foo)
>>>   (setq foo ..) ...) is about s good as it gets.
>>
>> If it exists set it with `setq'. If it doesn't exist, create
>> and set it ... with `setq'?
>
> What if it's an internal Emacs variable which might become a user option
> one day (I submitted a bug report about it) and then my customization
> silently disappears?  It's the "silently" part I want to guard
> against...

What about doing an assert of sorts

(message "some--internal-var is %S" some--internal-var)
(setq some--internal-var 42)

It will crash your init process if some--internal-var is void.

--
Tomas



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16  6:02       ` Tomas Nordin
@ 2022-03-16  6:37         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-16 18:34         ` Marcin Borkowski
  1 sibling, 0 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-16  6:37 UTC (permalink / raw)
  To: help-gnu-emacs

Tomas Nordin wrote:

> What about doing an assert of sorts
>
> (message "some--internal-var is %S" some--internal-var)
> (setq some--internal-var 42)
>
> It will crash your init process if some--internal-var is void.

Byte-compile

  (message "some--internal-var is %S" some--internal-var)

and the byte-compiler will tell you

  Warning: reference to free variable ‘some--internal-var’

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: [External] : Re: Is there a way of setting a variable only when it exists?
  2022-03-15 15:38       ` Drew Adams
@ 2022-03-16 18:32         ` Marcin Borkowski
  0 siblings, 0 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-16 18:32 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs@gnu.org, Emanuel Berg


On 2022-03-15, at 16:38, Drew Adams <drew.adams@oracle.com> wrote:

>> What if it's an internal Emacs variable which might become a user
>> option one day (I submitted a bug report about it) and then my
>> customization silently disappears?  It's the "silently" part I
>> want to guard against...
>
> What do you mean by "an internal Emacs variable"?

As I hinted in my question, one whose name contains a double dash.

> If it's a defvar then Emacs's predefined defvar
> has no effect if you defvar the variable before
> Emacs does.  And it has no effect if you setq
> the var before or after Emacs defvars it (as long
> as Emacs doesn't _use_ it before you setq it).

I know that.

> If Emacs later changes it to a defcustom, the
> same things apply.  However, if the defcustom has
> a :set function then your own setting of it might
> no longer be appropriate ("valid").

If, however, it gets promoted to a user option - and gets renamed -
things look different.

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16  0:25     ` Michael Heerdegen
@ 2022-03-16 18:33       ` Marcin Borkowski
  2022-03-16 19:25         ` Michael Heerdegen
  2022-03-16 20:35         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-16 18:33 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2022-03-16, at 01:25, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> Not necessarily.  My use-case is an internal Emacs variable I'm setting
>> in my `init.el'; at the same time I submitted a bug report/feature
>> request to make it a user option, so I assume that it might change its
>> name in a future Emacs version.
>
> If that happens, compiling your init.el will then normally give you an
> "assignment to free variable" warning, no?

I didn't think about it.  OTOH, why would I compile my init.el?

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16  0:47       ` Michael Heerdegen
@ 2022-03-16 18:33         ` Marcin Borkowski
  0 siblings, 0 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-16 18:33 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2022-03-16, at 01:47, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> What if it's an internal Emacs variable which might become a user option
>> one day (I submitted a bug report about it) and then my customization
>> silently disappears?
>
> That normally does not happen.  `defcustom' doesn't alter existing
> bindings.  You can still `setq' a user option.  There is no problem
> unless the "meaning" of the variable values changed in an backward
> incompatible way.  I guess nothing can protect against that.
>
> The only notable case I can think of is when things change so that
> setting the variable doesn't have the expected effect any more because
> the custom setter has gotten additional side effects (more than only
> setting the value).  Is that your concern?

No, my concern is the variable changing its _name_.

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16  6:02       ` Tomas Nordin
  2022-03-16  6:37         ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-16 18:34         ` Marcin Borkowski
  1 sibling, 0 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-16 18:34 UTC (permalink / raw)
  To: Tomas Nordin; +Cc: help-gnu-emacs, Emanuel Berg


On 2022-03-16, at 07:02, Tomas Nordin <tomasn@posteo.net> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> On 2022-03-14, at 14:48, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>>
>>> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>>>
>>>>> I assume it would be fairly easy to code such a macro
>>>>> (using `boundp'), but maybe it exists already?
>>>>
>>>> AFAIK it doesn't exist yet. The reason for it is that it is
>>>> not often useful. Typically there are two cases:
>>>>
>>>> - If the var exist, you want to set it and if not you have
>>>>   no fallback. In that case, it is typically harmless to set
>>>>   the var even when it doesn't exist, so the code just uses
>>>>   `setq` without bothering to test `boundp`.
>>>>
>>>> - If the var exist you want to set it, and if it doesn't you
>>>>   want to do something else. In that case, the something
>>>>   else tends to depend on the specifics so (if (boundp 'foo)
>>>>   (setq foo ..) ...) is about s good as it gets.
>>>
>>> If it exists set it with `setq'. If it doesn't exist, create
>>> and set it ... with `setq'?
>>
>> What if it's an internal Emacs variable which might become a user option
>> one day (I submitted a bug report about it) and then my customization
>> silently disappears?  It's the "silently" part I want to guard
>> against...
>
> What about doing an assert of sorts
>
> (message "some--internal-var is %S" some--internal-var)
> (setq some--internal-var 42)
>
> It will crash your init process if some--internal-var is void.

Well, that could work, but is far from elegant;-).

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16 18:33       ` Marcin Borkowski
@ 2022-03-16 19:25         ` Michael Heerdegen
  2022-03-18  5:58           ` Marcin Borkowski
  2022-03-16 20:35         ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 37+ messages in thread
From: Michael Heerdegen @ 2022-03-16 19:25 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> I didn't think about it.  OTOH, why would I compile my init.el?

For that reason.  Typos, names in third party packages changed, such
stuff: to notice issues introduced by you or someone else.

Michael.



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16 18:33       ` Marcin Borkowski
  2022-03-16 19:25         ` Michael Heerdegen
@ 2022-03-16 20:35         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-17 20:47           ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-18 17:59           ` Jean Louis
  1 sibling, 2 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-16 20:35 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

>> If that happens, compiling your init.el will then normally
>> give you an "assignment to free variable" warning, no?
>
> I didn't think about it. OTOH, why would I compile my
> init.el?

To get .elc which is better, faster byte-code as well as the
other advantages of code-improvement that the byte-compiler's
messages leads to if you take proper action ...

This has been mentioned many times over the years here not the
least ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16 20:35         ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-03-17 20:47           ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-18  5:59             ` Marcin Borkowski
  2022-03-18 17:59           ` Jean Louis
  1 sibling, 1 reply; 37+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-03-17 20:47 UTC (permalink / raw)
  To: help-gnu-emacs

> To get .elc which is better, faster byte-code as well as the
> other advantages of code-improvement that the byte-compiler's
> messages leads to if you take proper action ...

You can get the warnings messages with `flymake-mode`.  It does run the
compiler internally but doesn't generate the corresponding `.elc` file.


        Stefan




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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16 19:25         ` Michael Heerdegen
@ 2022-03-18  5:58           ` Marcin Borkowski
  0 siblings, 0 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-18  5:58 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2022-03-16, at 20:25, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> I didn't think about it.  OTOH, why would I compile my init.el?
>
> For that reason.  Typos, names in third party packages changed, such
> stuff: to notice issues introduced by you or someone else.

Well, makes sense.  Thanks!

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-17 20:47           ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-18  5:59             ` Marcin Borkowski
  0 siblings, 0 replies; 37+ messages in thread
From: Marcin Borkowski @ 2022-03-18  5:59 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2022-03-17, at 21:47, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

>> To get .elc which is better, faster byte-code as well as the
>> other advantages of code-improvement that the byte-compiler's
>> messages leads to if you take proper action ...
>
> You can get the warnings messages with `flymake-mode`.  It does run the
> compiler internally but doesn't generate the corresponding `.elc` file.

Interesting!  I use flycheck (though not for Elisp), I might be tempted
to use flymake for Elisp now!  Thanks!

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-16 20:35         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-03-17 20:47           ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-18 17:59           ` Jean Louis
  2022-03-19  9:25             ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 37+ messages in thread
From: Jean Louis @ 2022-03-18 17:59 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2022-03-17 21:59]:
> Marcin Borkowski wrote:
> 
> >> If that happens, compiling your init.el will then normally
> >> give you an "assignment to free variable" warning, no?
> >
> > I didn't think about it. OTOH, why would I compile my
> > init.el?
> 
> To get .elc which is better, faster byte-code as well as the
> other advantages of code-improvement that the byte-compiler's
> messages leads to if you take proper action ...
> 
> This has been mentioned many times over the years here not the
> least ...

It's going to be over the years not easily understood. Maybe Emacs
need some better "warning" or instructions how to initiate byte
compiling.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Is there a way of setting a variable only when it exists?
  2022-03-18 17:59           ` Jean Louis
@ 2022-03-19  9:25             ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 37+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-03-19  9:25 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>>>> If that happens, compiling your init.el will then
>>>> normally give you an "assignment to free variable"
>>>> warning, no?
>>>
>>> I didn't think about it. OTOH, why would I compile my
>>> init.el?
>> 
>> To get .elc which is better, faster byte-code as well as
>> the other advantages of code-improvement that the
>> byte-compiler's messages leads to if you take proper action ...
>> 
>> This has been mentioned many times over the years here not
>> the least ...
>
> It's going to be over the years not easily understood.
> Maybe Emacs need some better "warning" or instructions how
> to initiate byte compiling.

Well, the OP has heard these arguments and others before,
many times.

As for introductions, I know only of

  (info "(elisp) Byte Compilation")

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2022-03-19  9:25 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-14  6:00 Is there a way of setting a variable only when it exists? Marcin Borkowski
2022-03-14 12:52 ` Eli Zaretskii
2022-03-14 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-14 13:48   ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-15  6:17     ` Marcin Borkowski
2022-03-15  6:50       ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-15  6:58       ` Jean Louis
2022-03-15  7:45         ` Marcin Borkowski
2022-03-15  8:12           ` Jean Louis
2022-03-15  7:49         ` tomas
2022-03-15  8:13           ` Jean Louis
2022-03-15  8:36             ` tomas
2022-03-15  9:28               ` Jean Louis
2022-03-15 10:01                 ` tomas
2022-03-15 11:29                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-15  8:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-15 15:33         ` [External] : " Drew Adams
2022-03-15 15:38       ` Drew Adams
2022-03-16 18:32         ` Marcin Borkowski
2022-03-16  0:47       ` Michael Heerdegen
2022-03-16 18:33         ` Marcin Borkowski
2022-03-16  6:02       ` Tomas Nordin
2022-03-16  6:37         ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-16 18:34         ` Marcin Borkowski
2022-03-15  6:48     ` Jean Louis
2022-03-14 22:09 ` Michael Heerdegen
2022-03-15  6:16   ` Marcin Borkowski
2022-03-15  7:05     ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-16  0:25     ` Michael Heerdegen
2022-03-16 18:33       ` Marcin Borkowski
2022-03-16 19:25         ` Michael Heerdegen
2022-03-18  5:58           ` Marcin Borkowski
2022-03-16 20:35         ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-03-17 20:47           ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-18  5:59             ` Marcin Borkowski
2022-03-18 17:59           ` Jean Louis
2022-03-19  9:25             ` Emanuel Berg via Users list for the GNU Emacs text editor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).