如何使用`setcdr`与缓冲区局部变量 [英] How to use `setcdr` with buffer-local variables
问题描述
我正在使用 setcdr
的行为,这似乎表明它不局限于在不同缓冲区中包含相同名称的缓冲区局部变量。我已经尝试使用 with-current-buffer
,但这并不能解决问题。
I am experiencing behavior using setcdr
that would seem to suggest that it is not confined to buffer-local variables containing the same name in different buffers. I have tried using with-current-buffer
, but that does not correct the issue.
例如, code> buffer-A 和 buffer-B
都包含一个名为 variable-one
,这是一个
用于将 cons
单元格 - 例如(overlay-string t)
code> setcdr cdr
值设置为 t
或 nil
。
For example, buffer-A
and buffer-B
both contain a local-variable named variable-one
, which is a cons
cell -- e.g., (overlay-string t)
, and setcdr
is being used to set the cdr
value to either t
or nil
.
有没有办法确保 setcdr
仅限于修改缓冲区本地值?
Is there a way to ensure that setcdr
remains confined to modifying only the buffer-local value?
推荐答案
不,那里是列表的cdr的缓冲区本地值。
No, there is no buffer-local value for the cdr of the list.
如果两个变量指向同一列表,并修改列表,那么您将看到变量。
If two variables point at the same list, and you modify the list, then you will see the effect in both the variables.
与缓冲区局部变量无关,以及将列表分配给变量的所有内容。
That has nothing to do with buffer-local variables, and everything to do with what it means to assign a list to a variable.
如果你想要两个变量要指向不同的列表,那么您需要复制整个列表。
If you want the two variables to point to different lists, then you need to copy the entire list.
您可以使用(复制序列LIST)
复制列表。或者(附加LIST nil)
也是常见的(作为附加复制所有除了最后的参数)。
You can use (copy-sequence LIST)
to copy a list. Or (append LIST nil)
is also common (as append copies all except the final argument).
n.b。这些功能创建一个新的列表结构,但列表中的值仍然是原始对象。通常这就是所需要的,但是如果您确实需要列表内容完全独立,那么请改用 copy-tree
。
n.b. Those functions create a new list structure, but the values in the list are still the original objects. Typically that's all that's required, but if you do need the list contents to be entirely independent of one another, then use copy-tree
instead.
这篇关于如何使用`setcdr`与缓冲区局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!