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

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

问题描述

我有一个关于 Emacs Lisp 的问题.setqsetq-default 有什么区别?

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

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

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

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

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.

我的理解有问题吗?

例如:

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

  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.

我在 init.el 文件中写了 (setq tab-width 4).当我运行 tab-width C-x C-e 时,它显示8"(当前模式是文本").但是,当我使用 (setq-default tab-width 4) 时,它显示4".我无法解释这种现象.

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.

推荐答案

Emacs 中的某些变量是缓冲区本地的",这意味着允许每个缓冲区为该变量拥有一个单独的值,该值覆盖全局默认值.tab-width 是缓冲区局部变量的一个很好的例子.

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.

如果变量是缓冲区本地的,则 setq 设置它在当前缓冲区中的本地值,setq-default 设置全局默认值.

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

如果一个变量不是缓冲区本地的,那么 setqsetq-default 做同样的事情.

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

在您的情况 2 中,(setq tab-width 4) 在当前缓冲区中将 tab-width 的缓冲区本地值设置为 4,保留全局默认值tab-width 的值仍然是 8,所以当你在一个没有本地值的不同缓冲区中计算 tab-width 时,你看到的是 8.然后,当你设置默认值为 4,该缓冲区将其选中,因为它仍然没有本地值.

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天全站免登陆