Emacs Lisp中setq和setq-default之间的区别 [英] The difference between setq and setq-default in Emacs Lisp

查看:617
本文介绍了Emacs Lisp中setq和setq-default之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Emacs Lisp的问题。 setq setq-default 之间有什么区别?我很困惑



教程说 setq 本地缓冲区中生效,而 setq-default 会影响所有缓冲区。



例如,如果我在 init.el 中写了(setq a-var a-vars-value)我发现在启动Emacs并打开一个新的缓冲区之后, a-var 也在那里,它的值是 a-vars-value 。我以为这不应该在那里。 setq setq-default 之间似乎没有区别。



我的理解有问题吗?



非常感谢。



例如:

1)我在 init.el 文件中写了(setq hello 123),而我在shell中运行 emacs abuffer ,然后输入hello Cx Ce,它显示123。当我在所有新的缓冲区中运行时也会发生这种情况。



2)我在 init.el 文件中写了(setq tab-width 4)当我运行标签宽度C-x C-e 时,它显示8(当前模式是文本)。但是,当我使用(setq-default tab-width 4)时,显示4。我不能解释这个现象。

解决方案

Emacs中的一些变量是buffer-local,这意味着每个缓冲区都被允许要为覆盖全局默认值的该变量设置一个单独的值。



如果变量是缓冲区本地的,然后 setq 在当前缓冲区中设置其本地值, setq-default 设置全局默认值。



如果变量不是本地缓冲区,那么 setq setq-default 做同样的事情。



在你的情况下,(setq tab-width 4)设置缓冲区当前缓冲区中的本地值 tab-width 为4,保留全局默认值 tab-width still在8时,所以当您在不具有本地值的不同缓冲区中评估 tab-width 时,会看到8.然后,当将默认值设置为4时缓冲区拿起来,因为它还没有本地值。


I have a question about Emacs Lisp. What is the difference between setq and setq-default? I am very confused about it.

Tutorials say setq takes effect in the local buffer while setq-default affects all buffers.

For example, if I wrote (setq a-var a-vars-value) in init.el, I found after starting Emacs and opening a new buffer, the a-var is also there and its value is a-vars-value. I thought it was not supposed to be there. It seems there is no difference between setq and setq-default.

Is there something wrong with my understanding?

Thank you very much.

For example:
1) I wrote (setq hello 123) in the init.el file, and I run emacs abuffer in the shell, then I input "hello C-x C-e", it shows "123". The same happens when I run this in all new buffers.

2) I wrote (setq tab-width 4) in the init.el file. When I run tab-width C-x C-e, it shows "8" (Current mode is 'Text'). However, when I use (setq-default tab-width 4), it show "4". I can't explain this phenomenon.

解决方案

Some variables in Emacs are "buffer-local", meaning that each buffer is allowed to have a separate value for that variable that overrides the global default. tab-width is a good example of a buffer-local variable.

If a variable is buffer-local, then setq sets its local value in the current buffer and setq-default sets the global default value.

If a variable is not buffer-local, then setq and setq-default do the same thing.

In your case 2, (setq tab-width 4) set the buffer-local value of tab-width to 4 in the current buffer, leaving the global default value of tab-width still at 8, so when you evaluated tab-width in a different buffer that had no local value, you saw that 8. Then, when you set the default value to 4, that buffer picked it up, since it still had no local value.

这篇关于Emacs Lisp中setq和setq-default之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆