all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Omar Polo <op@omarpolo.com>
To: Emanuel Berg <moasenwood@zoho.eu>
Cc: help-gnu-emacs@gnu.org
Subject: Re: don't understand setq-default
Date: Sun, 18 Apr 2021 16:59:38 +0200	[thread overview]
Message-ID: <87blaby6xx.fsf@omarpolo.com> (raw)
In-Reply-To: <8735vnwu8z.fsf@zoho.eu>


Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:

>>> OK, that explains it, buffer local values, I can't even
>>> keep track of the buffers...
>>
>> I have used it 8 times, maybe change to setq to force
>> everywhere...
>
> Worst thing that can happen, it will show, where I need even
> more force...
>
> Wait, I sense there is something wrong with this thinking...
>
> with setq, global var
>
> with setq-default default val for var if not buffer-local
>
> how do you look for the buffer-local var?
>
> I take it that takes precedence?
>
> yeah, it is the compass needle all over that acts on the
> GLOBAL Earth's magnet, only put the local magnet next to
> the compass, proximity overrules size
>
> you set the buffer-locals or they set themselves thru hooks
> or implications from setq "automatically when set"
>
> how do I know my globals - me thinking them are - aren't
> actually as they gets trapped in some Elisp file where
> I eval'd them which has nothing to do with it, even?
> Eval a config file for ERC, all that goes into the Elisp
> file LOL.
>
> How do you tell a var should be buffer-local when set? Nah,
> I don't want to know. They are not part of my strategy.
> But please tell my all buzzwords so I can go on pruning
> my Elisp, always a good feeling :)

[ keep in mind that I'm not really an expert elisp hacker ]

Try to keep it simple.  buffer-local variables sometimes are useful.
Let's take the indent-tabs-mode variable for instance: you set it to t
to enable hard tabs, or nil if you don't.  For some modes you may want
to use tabs (e.g. C, or Go), while for others spaces (may) be mandatory
(i.e. python), so a global variable won't do it  (you won't be able to
switch between C and python files without messing up the indentation :P)

Another example: I'm writing a chat application in elisp.  I have a,
say, toxe-friend-name that holds the name of the chat you've opened.
It's useful to keep this a buffer-local, so it gets a different value
per chat-buffer, and the elisp code is simple.

Sometimes this don't make sense: emacs-version probably doesn't need to
be buffer local, so judgement is needed.

C-h v tells you when a variable becomes buffer-local when set, or if
it's local to the current buffer, so you may want to check that.  It
even tells you its "default" and "local" value, which can get useful.

You get the buffer-local value when you're running a bit of elisp inside
that buffer.  (I don't know how to retrieve the global value, but a
short trip to the manual should tell me that)

So, for example:

	(defvar my-var 5)

	my-var
	;; => 5

	(with-current-buffer (get-buffer-create "test")
	  (message "my-var is %d" my-var)
	  (make-local-variable 'my-var)
	  (setq my-var 7)
	  (message "my-var is %d" my-var)
	  (setq-default my-var 'foo)
	  (message "my-var is %d" my-var))
	;; my-var is 5
	;; my-var is 7
	;; my-var is 7

	my-var
	;; => foo

HTH



  reply	other threads:[~2021-04-18 14:59 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-18 11:40 don't understand setq-default Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-18 13:02 ` Omar Polo
2021-04-18 13:39   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-18 13:40     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-18 14:19       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-18 14:59         ` Omar Polo [this message]
2021-04-19  3:06           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19  3:23             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19  3:33               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19  6:40                 ` Omar Polo
2021-04-19 13:39                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19 12:49             ` Thibaut Verron
2021-04-19 17:29               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-18 21:59         ` Jean Louis
2021-04-18 22:28           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19  6:44             ` Jean Louis
2021-04-19 17:17               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19  3:49           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-19  6:52             ` Jean Louis
2021-04-19 17:26               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-20  5:44                 ` Rounding percentages... was setq-default Jean Louis
2021-04-30  1:04                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-04-18 21:55 ` don't understand setq-default Jean Louis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87blaby6xx.fsf@omarpolo.com \
    --to=op@omarpolo.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=moasenwood@zoho.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.