Emacs缩进困难
我真的很想切换到 emacs,但是学习设置环境真的很痛苦。 大家都说值得,所以我就继续。
我希望我的 C 代码以这种方式实现:
if(asdf)
{
asdr = 1;
}
根据当前标准(我知道,别让我开始),可能是:
if(asdf) {
asdr = 1;
}
我似乎无法更改缩进大小从 2,它总是看起来像 GNU标准:
if(asdf)
{
asdr = 1;
}
,我不喜欢。 这是我在 .emacs 中放入的内容:
; Warn in C for while();, if(x=0), ...
(global-cwarn-mode 1)
; no electric mode in c
(c-toggle-electric-state -1)
; indent the current line only if the cursor is at the beginning of the line
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-indent 4)
; These commands I read about on the web, but they don't work?
;(highlight-tabs)
;(highlight-trailing_whitespace)
这没有帮助,我仍然有 GNU 缩进。 有人吗?
--- 编辑以添加我的整个 .emacs(实际上是 ~/.emacs.d/init.el)
; directory to put various el files into
(add-to-list 'load-path "C:/Program/emacs-22.3/includes")
; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".rhtml$" . html-mode) auto-mode-alist))
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))
(set (make-local-variable 'indent-tabs-mode) 'nil)
(set (make-local-variable 'tab-width) 2)
(imenu-add-to-menubar "IMENU")
(define-key ruby-mode-map "\C-m" 'newline-and-indent) ;Not sure if this line is 100% right but it works!
(require 'ruby-electric)
(ruby-electric-mode t)
))
; Install mode-compile to give friendlier compiling support!
(autoload 'mode-compile "mode-compile"
"Command to compile current buffer file based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
"Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)
(show-paren-mode 1)
; Color theme
(require 'color-theme)
(color-theme-pok-wog)
;;Emacs.pane.menubar.* does not seem to work?
;Emacs.pane.menubar.background: darkGrey
;Emacs.pane.menubar.foreground: black
; Default font 9 pt
(set-face-attribute 'default nil :height 80)
(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-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.
'(font-lock-comment-face ((t (:foreground "limegreen" :slant oblique))))
'(font-lock-preprocessor-face ((t (:inherit font-lock-builtin-face :foreground "orange" :weight bold)))))
(global-set-key [C-tab] 'other-window)
(global-set-key [C-S-tab] (lambda () (interactive) (other-window -1)))
(defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux
kernel."
(interactive)
(c-mode)
(setq c-indent-level 8)
(setq c-brace-imaginary-offset 0)
(setq c-brace-offset -8)
(setq c-argdecl-indent 8)
(setq c-label-offset -8)
(setq c-continued-statement-offset 8)
(setq indent-tabs-mode nil)
(setq tab-width 8))
; Warn in C for while();, if(x=0), ...
(global-cwarn-mode 1)
; no electric mode in c
(c-toggle-electric-state -1)
; indent the current line only if the cursor is at the beginning of the line
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-indent 4)
; These commands I read about on the web, but they don't work?
;(highlight-tabs)
;(highlight-trailing_whitespace)
(setq indent-tabs-mode nil)
(setq c-default-style "user")
;; Remove lull: scroll bar, tool bar, menu bar.
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
;; restore window size as it was at previous use
(defun restore-saved-window-size()
(unless (load "~/.emacs.d/whsettings" t nil t)
(setq saved-window-size '(80 30)))
(nconc default-frame-alist `((width . ,(car saved-window-size))
(height . ,(cadr saved-window-size)))))
(restore-saved-window-size)
(defun save-window-size-if-changed (&optional unused)
(let ((original-window-size `(,(frame-width) ,(frame-height))))
(unless (equal original-window-size saved-window-size)
(with-temp-buffer
(setq saved-window-size original-window-size)
(insert (concat "(setq saved-window-size '"
(prin1-to-string saved-window-size) ")"))
(write-file "~/.emacs.d/whsettings")))))
(add-hook 'window-size-change-functions 'save-window-size-if-changed)
;; Ack as a replacment for grep
(global-set-key "\M-s" 'ack)
(require 'ack)
I'm really trying to switch to emacs, but learning to setup the environment is a real pain.
Everybody says it's worth it, so I just continue.
I want my c code to be implemented that way:
if(asdf)
{
asdr = 1;
}
Depending on the current standard (I know, don't get me started), could be:
if(asdf) {
asdr = 1;
}
I can't seem to change the indentation size from 2, it always looks like the GNU standard:
if(asdf)
{
asdr = 1;
}
, which I dislike.
Here is what I have put in my .emacs:
; Warn in C for while();, if(x=0), ...
(global-cwarn-mode 1)
; no electric mode in c
(c-toggle-electric-state -1)
; indent the current line only if the cursor is at the beginning of the line
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-indent 4)
; These commands I read about on the web, but they don't work?
;(highlight-tabs)
;(highlight-trailing_whitespace)
This did not help, I have still the GNU indent.
Anyone?
--- EDIT TO ADD MY WHOLE .emacs (actually ~/.emacs.d/init.el)
; directory to put various el files into
(add-to-list 'load-path "C:/Program/emacs-22.3/includes")
; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '(".rhtml$" . html-mode) auto-mode-alist))
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))
(set (make-local-variable 'indent-tabs-mode) 'nil)
(set (make-local-variable 'tab-width) 2)
(imenu-add-to-menubar "IMENU")
(define-key ruby-mode-map "\C-m" 'newline-and-indent) ;Not sure if this line is 100% right but it works!
(require 'ruby-electric)
(ruby-electric-mode t)
))
; Install mode-compile to give friendlier compiling support!
(autoload 'mode-compile "mode-compile"
"Command to compile current buffer file based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
"Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)
(show-paren-mode 1)
; Color theme
(require 'color-theme)
(color-theme-pok-wog)
;;Emacs.pane.menubar.* does not seem to work?
;Emacs.pane.menubar.background: darkGrey
;Emacs.pane.menubar.foreground: black
; Default font 9 pt
(set-face-attribute 'default nil :height 80)
(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-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.
'(font-lock-comment-face ((t (:foreground "limegreen" :slant oblique))))
'(font-lock-preprocessor-face ((t (:inherit font-lock-builtin-face :foreground "orange" :weight bold)))))
(global-set-key [C-tab] 'other-window)
(global-set-key [C-S-tab] (lambda () (interactive) (other-window -1)))
(defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux
kernel."
(interactive)
(c-mode)
(setq c-indent-level 8)
(setq c-brace-imaginary-offset 0)
(setq c-brace-offset -8)
(setq c-argdecl-indent 8)
(setq c-label-offset -8)
(setq c-continued-statement-offset 8)
(setq indent-tabs-mode nil)
(setq tab-width 8))
; Warn in C for while();, if(x=0), ...
(global-cwarn-mode 1)
; no electric mode in c
(c-toggle-electric-state -1)
; indent the current line only if the cursor is at the beginning of the line
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-indent 4)
; These commands I read about on the web, but they don't work?
;(highlight-tabs)
;(highlight-trailing_whitespace)
(setq indent-tabs-mode nil)
(setq c-default-style "user")
;; Remove lull: scroll bar, tool bar, menu bar.
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
;; restore window size as it was at previous use
(defun restore-saved-window-size()
(unless (load "~/.emacs.d/whsettings" t nil t)
(setq saved-window-size '(80 30)))
(nconc default-frame-alist `((width . ,(car saved-window-size))
(height . ,(cadr saved-window-size)))))
(restore-saved-window-size)
(defun save-window-size-if-changed (&optional unused)
(let ((original-window-size `(,(frame-width) ,(frame-height))))
(unless (equal original-window-size saved-window-size)
(with-temp-buffer
(setq saved-window-size original-window-size)
(insert (concat "(setq saved-window-size '"
(prin1-to-string saved-window-size) ")"))
(write-file "~/.emacs.d/whsettings")))))
(add-hook 'window-size-change-functions 'save-window-size-if-changed)
;; Ack as a replacment for grep
(global-set-key "\M-s" 'ack)
(require 'ack)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
EmacsWiki 有一篇关于 IndentingC 的好文章。
事实上,EmacsWiki 上有关于所有内容的精彩文章。我不想在 Emacs 出现之前就学会它。
The EmacsWiki has a nice article on IndentingC.
In fact, EmacsWiki has nice articles on everything. I wouldn't want to have learned Emacs before it existed.
emacs cc 模式中的缩进由一组偏移量控制,每个偏移量都可以根据附加到偏移量的名称进行设置。
if 语句后的开卷曲有一个偏移量和一个名称。如果更改偏移值,开卷曲的缩进将会有所不同。同样,函数声明后的开卷曲也有一个命名的偏移量。宏中的行延续有一个命名的偏移量。 switch 语句中的 case 标签,do while 循环。它们都有偏移量。它们有无数个,全部存储在名为
c-offsets-alist
的变量中。其他地方提到的样式提供了一个“基本偏移量”,通常是 2、4 或 8 个空格,然后是
c-offsets-alist
的值。每个样式还有一个名称,您可以从其他样式派生自定义样式。像这样:c-offsets-alist
中每个命名偏移量的值是以下之一:0
意味着,保持缩进与上一行相同+
表示,增加缩进,额外一级-
表示,减少缩进,额外一级您还可以使用 ++、-- 等。在 .emacs 文件中放置类似的内容来定义样式。然后,要在编辑 C 文件时自动采用该样式,请在 c-mode 挂钩中使用
c-set=style
,如下所示:下一个问题是,如何确定哪个命名的 c-您需要为任何特定情况设置偏移量?有一个应用程序可以做到这一点。好吧,不是一个应用程序,而是一个 elisp 函数。
Indenting in emacs cc-mode is governed by a set of offsets, each of which can be set, according to a name attached to the offset.
The open-curly after an if statement has an offset, with a name. If you change the offset value, the open-curly will be indented differently. Likewise, the open-curly after a function declaration, has a named offset. Line continuations in a macro have a named offset. Case labels in a switch statement, do while loops. They all have offsets. There are a zillion of 'em, all stored in a variable called
c-offsets-alist
.The styles mentioned in other places provide a "basic offset", which is normally 2, 4, or 8 spaces, and then a value for
c-offsets-alist
. Each style also has a name, and you can derive custom styles from other styles. Like this:The value for each named offset in
c-offsets-alist
is one of:0
implying, keep the indent the same as the previous line+
implying, increase the indent, one additional level-
implying, decrease the indent, one additional levelYou can also use ++, --, and so on. Put something like that, to define a style, in your .emacs file. Then, to automatically employ that style when editing C files, use
c-set=style
in a c-mode hook, like this:The next question is, how does one determine which of the named c-offsets you need to set for any particular situation? There's an app for that. ok, not an app, but a elisp function.
您的默认设置可能会被
cc-mode
的样式功能覆盖。尝试像这样初始化默认样式:
(setq c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "user")))
您应该能够将前面的行粘贴到 .emacs 文件中,或自定义
c-default-style
变量。默认将(other "gnu")
作为列表的最后一个元素,这意味着所有非 Java 和非 awk 文件都会获得gnu
样式,而不是您可以使用setq
进行设置。特殊的user
样式从您手动设置的样式变量中初始化。另一种选择是选择一种内置样式而不是自己定义它,或者使用
c-add-style
函数创建您自己的样式。为此,请将上述命令中的"user"
更改为样式名称(作为字符串)。尝试使用stroustrup
或python
内置样式来按照您想要的方式格式化 if 语句。Your defaults are possibly being overridden by
cc-mode
's style feature.Try initialising the default style like this:
(setq c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "user")))
You should be able to paste the preceding line into your .emacs file, or customize the
c-default-style
variable. The default has(other "gnu")
as the last element of the list, which means that all non-Java and non-awk files get thegnu
style instead of what you set withsetq
. The specialuser
style gets initialised from your manually-set style variables.Another option is to pick one of the built-in styles instead of defining it yourself, or to create your own style using the
c-add-style
function. To do this, change"user"
in the above command to the name of the style (as a string). Try the thestroustrup
orpython
built-in styles to format if-statements how you want.