if 语句缩进不工作 emacs
if-statement Indentation not working emacs
我是 emacs 新手。我一直在为 emacs 使用这个配置:
(setq column-number-mode t)
(setq c-default-style "linux"
c-basic-offset 4)
(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.
'(custom-enabled-themes (quote (wheatgrass))))
(custom-set-faces
;; custom-set-faces 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.
)
通常,emacs 会自动正确处理缩进。但是,对于 if 语句,即使我可以在 emacs 中看到正确的缩进,但在其他编辑器中也不会显示该选项卡。
我一直在编辑一个*.c文件,模式是"C/l mode defined in ‘cc-mode.el’. Major mode for editing C code"。
举个例子:
如果我使用以下代码在 emacs 中创建一个 c 文件:
int main() {
int x = 1;
if (x > 0)
x++;
else
x--;
printf("Value of x: %d\n", x);
return 0;
}
如果我在不同的编辑器中可视化这段代码,或者我将它从 emacs 复制到另一个编辑器,输出如下:
int main() {
int x = 1;
if (x > 0)
x++;
else
x--;
printf("Value of x: %d\n", x);
return 0;
}
基本上,即使它看起来正确地缩进了 if 语句,它也没有在 if 语句中添加任何制表符。
问题是什么?我该如何解决?
请注意确保方向正确,但是您可以检查制表符是否是问题的根源。命令:
C-u M-x untabify
将所有制表符转换为空格。
我建议尝试:
- 将此 untabify 命令应用于您的代码缓冲区,
- 保存,
- 使用其他文本编辑器打开文件,
如果您现在得到正确的缩进,我们就确定了问题的根源:制表符。
一个可能的解决方法是系统地使用空格而不是制表符。你可以看看:How to force spaces instead of tabs regardless of major mode?
Emacs 混合制表符和空格进行缩进,而您的其他编辑器使用不同的制表符宽度。
查看您的示例,我可以确定在 Emacs 中 tab-width
是 8,c-basic-offset
是 4,而 indent-tabs-mode
是 t。
如果我们为该配置所需的空格数 (s) and/or 制表符 (t) 添加前缀:
0 |int main() {
4s| int x = 1;
|
4s| if (x > 0)
1t| x++;
4s| else
1t| x--;
|
4s| printf("Value of x: %d\n", x);
|
4s| return 0;
0 |}
在您的其他编辑器中,您的制表符宽度显然只相当于 4 个空格,因此“4s”和“1t”显示为相同的宽度。
如果你 M-x set-variable RET tab-width RET 4 RET
在 Emacs 中,你会看到同样的事情。
n.b。 M-x whitespace-mode
可以为您可视化缩进字符。
为了获得最佳的跨编辑器兼容性,要么只使用空格进行缩进——indent-tabs-mode
设置为 nil
——或者如果你更喜欢制表符,那么你应该确保 tab-width
和c-basic-offset
是相同的值,这意味着每个级别的缩进都将是一个制表符(至少在缩进有特定数量的语言中,而不是与任意其他代码对齐)。
看来你确实需要标签,所以试试这个配置:
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
"Custom `c-mode' behaviours."
(setq c-basic-offset 4)
(setq tab-width c-basic-offset)
(setq indent-tabs-mode t))
如果您只使用空格,那么您只需禁用 indent-tabs-mode
:
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
"Custom `c-mode' behaviours."
(setq indent-tabs-mode nil))
或者您可以 默认 禁用它,而不是针对特定的主要模式。 indent-tabs-mode
始终是缓冲区局部值,但如果您更改其默认值,那么这将影响所有未明确设置它的未来缓冲区:
(setq-default indent-tabs-mode nil)
我是 emacs 新手。我一直在为 emacs 使用这个配置:
(setq column-number-mode t)
(setq c-default-style "linux"
c-basic-offset 4)
(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.
'(custom-enabled-themes (quote (wheatgrass))))
(custom-set-faces
;; custom-set-faces 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.
)
通常,emacs 会自动正确处理缩进。但是,对于 if 语句,即使我可以在 emacs 中看到正确的缩进,但在其他编辑器中也不会显示该选项卡。
我一直在编辑一个*.c文件,模式是"C/l mode defined in ‘cc-mode.el’. Major mode for editing C code"。
举个例子: 如果我使用以下代码在 emacs 中创建一个 c 文件:
int main() {
int x = 1;
if (x > 0)
x++;
else
x--;
printf("Value of x: %d\n", x);
return 0;
}
如果我在不同的编辑器中可视化这段代码,或者我将它从 emacs 复制到另一个编辑器,输出如下:
int main() {
int x = 1;
if (x > 0)
x++;
else
x--;
printf("Value of x: %d\n", x);
return 0;
}
基本上,即使它看起来正确地缩进了 if 语句,它也没有在 if 语句中添加任何制表符。
问题是什么?我该如何解决?
请注意确保方向正确,但是您可以检查制表符是否是问题的根源。命令:
C-u M-x untabify
将所有制表符转换为空格。
我建议尝试:
- 将此 untabify 命令应用于您的代码缓冲区,
- 保存,
- 使用其他文本编辑器打开文件,
如果您现在得到正确的缩进,我们就确定了问题的根源:制表符。
一个可能的解决方法是系统地使用空格而不是制表符。你可以看看:How to force spaces instead of tabs regardless of major mode?
Emacs 混合制表符和空格进行缩进,而您的其他编辑器使用不同的制表符宽度。
查看您的示例,我可以确定在 Emacs 中 tab-width
是 8,c-basic-offset
是 4,而 indent-tabs-mode
是 t。
如果我们为该配置所需的空格数 (s) and/or 制表符 (t) 添加前缀:
0 |int main() {
4s| int x = 1;
|
4s| if (x > 0)
1t| x++;
4s| else
1t| x--;
|
4s| printf("Value of x: %d\n", x);
|
4s| return 0;
0 |}
在您的其他编辑器中,您的制表符宽度显然只相当于 4 个空格,因此“4s”和“1t”显示为相同的宽度。
如果你 M-x set-variable RET tab-width RET 4 RET
在 Emacs 中,你会看到同样的事情。
n.b。 M-x whitespace-mode
可以为您可视化缩进字符。
为了获得最佳的跨编辑器兼容性,要么只使用空格进行缩进——indent-tabs-mode
设置为 nil
——或者如果你更喜欢制表符,那么你应该确保 tab-width
和c-basic-offset
是相同的值,这意味着每个级别的缩进都将是一个制表符(至少在缩进有特定数量的语言中,而不是与任意其他代码对齐)。
看来你确实需要标签,所以试试这个配置:
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
"Custom `c-mode' behaviours."
(setq c-basic-offset 4)
(setq tab-width c-basic-offset)
(setq indent-tabs-mode t))
如果您只使用空格,那么您只需禁用 indent-tabs-mode
:
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
"Custom `c-mode' behaviours."
(setq indent-tabs-mode nil))
或者您可以 默认 禁用它,而不是针对特定的主要模式。 indent-tabs-mode
始终是缓冲区局部值,但如果您更改其默认值,那么这将影响所有未明确设置它的未来缓冲区:
(setq-default indent-tabs-mode nil)