global-set-key 设置切换缩进?

global-set-key settings to switch indentation?

在我自己的 C/C++ 编码中,我更喜欢 2-space 缩进,但是当我与其他人一起工作时,我受制于需要 3-[=28 的风格指南=] 缩进。在我的 .emacs 文件(粘贴在下面)中,我使用 custom-set-variables 将其设置为 2,我可以在 运行 .emacs 时更改它:

M-x customize-set-variable
Set variable: c-basic-offset 
[integer] [radio] Set customized value for c-basic-offset to: 3

(除了为什么是customize-set-variable而不是custom-set-variables?另外,它只有效每隔一次;我第一次这样做时,输入 'c-basic-offset' 就完成了(并且 c-basic-offset 设置为 1)。下次我这样做时,它会提示我输入什么值将其设置为——这是怎么回事?)

所以我可以解决这个问题,但是需要输入很多内容,我不想记住它。

我曾经搜索过如何将F5设置为M-x revert-buffer;我需要在我的 .emacs 文件中放入什么,以便我可以让 F2 和 F3 键将 c-basic-offset 更改为 2 和 3,因为操作不是简单的无参数 emacs 元命令?

仅供参考,这是我认为我目前在我的 .emacs 文件中的相关部分:

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.    
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(c-basic-offset 2)
 '(fill-column 80)
 '(global-auto-revert-mode t)
 '(indent-tabs-mode nil)
 '(inhibit-startup-screen t)
 '(initial-buffer-choice nil)
 '(initial-scratch-message nil))


(global-set-key [f5] 'revert-buffer)

What do I need to put into my .emacs file so I can have the F2 and F3 keys change c-basic-offset to 2 and 3, since the action is not a simple no-argument emacs meta command?

(defun set-offset-2 ()
  (interactive)
  (setq-default c-basic-offset 2))
(defun set-offset-3 ()
  (interactive)
  (setq-default c-basic-offset 3))

(global-set-key [f2] 'set-offset-2)
(global-set-key [f3] 'set-offset-3)

except why is it customize-set-variable instead of custom-set-variables?

调用 customize-set-variable 会提示用户更改一个变量,然后更新 .emacs 顶部的列表。 custom-set-variables 获取该列表并应用所有这些变量。

Also, it only works every other time; the first time I do it, after I enter 'c-basic-offset' it is just done (and c-basic-offset is set to 1). The next time I do it, it prompts me for what value to set it to -- what's up with that?)

奇怪的是我得到了同样的错误。不知道这是怎么回事。

我个人不使用 customize-set-variable。相反,我明确地调用了 setq-default 函数。例如,以下内容等同于您之前发布的内容:

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.    
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
  )

(setq-default c-basic-offset 2)
(setq-default fill-column 80)
(setq-default global-auto-revert-mode t)
(setq-default indent-tabs-mode nil)
(setq-default inhibit-startup-screen t)
(setq-default initial-buffer-choice nil)
(setq-default initial-scratch-message nil)

如果您想应用这些更改,只需将光标放在括号的末尾:

(setq-default c-basic-offset 2)█

然后输入 C-x C-e.