【问题标题】:Emacs indentation difficultyEmacs 缩进难度
【发布时间】:2010-03-17 11:59:17
【问题描述】:

我真的很想改用 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)

【问题讨论】:

    标签: emacs indentation


    【解决方案1】:

    EmacsWiki 在IndentingC 上有一篇不错的文章。

    事实上,EmacsWiki 对所有内容都有很好的文章。我不想在 Emacs 出现之前就学习它。

    【讨论】:

    • (setq c-default-style "k&r") 工作!我不知道为什么道格写的那一行没有。也许我把它放在我的 .emacs 中的错误位置。但是当我使用它时,制表符仍然是制表符,尽管我告诉它用(setq-default indent-tabs-mode nil)的空格替换它们。
    • 是的。更改我的 init.el 文件时,通常我会打开一个 emacs 实例以编辑 init.el 文件,然后打开和关闭另一个实例以测试更改。
    【解决方案2】:

    emacs cc-mode 中的缩进由一组偏移量控制,每个偏移量都可以根据附加到偏移量的名称进行设置。

    if 语句后的左花括号有一个偏移量,带有一个名称。如果更改偏移值,则开卷曲的缩进会有所不同。同样,函数声明后的开卷曲也有一个命名的偏移量。宏中的续行有一个命名的偏移量。 switch 语句中的 case 标签,执行 while 循环。它们都有偏移量。有无数个,全部存储在一个名为 c-offsets-alist 的变量中。

    其他地方提到的样式提供了一个“基本偏移量”,通常是 2、4 或 8 个空格,然后是 c-offsets-alist 的值。每种样式也有一个名称,您可以从其他样式派生自定义样式。像这样:

    (c-add-style "myCStyle"
      '("bsd"  ; this must be defined elsewhere - it is in cc-modes.el
      (c-basic-offset . 4)
      (c-echo-syntactic-information-p . t)
      (c-comment-only-line-offset . (0 . 0))
      (c-offsets-alist . (
        (c                     . c-lineup-C-comments)
        (statement-case-open   . 0)
        (case-label            . +)
        (substatement-open     . 0)
        ))
      ))
    

    c-offsets-alist 中每个命名偏移的值是以下之一:

    • 0 暗示,保持与上一行相同的缩进
    • + 暗示,增加缩进,增加一级
    • - 暗示,减少缩进,增加一级

    您还可以使用 ++、-- 等。在您的 .emacs 文件中放置类似的内容以定义样式。然后,要在编辑 C 文件时自动使用该样式,请在 c 模式挂钩中使用 c-set=style,如下所示:

    (defun doug-c-mode-hook-fn ()
      (cond (window-system
         ;; use my defined style for all C modules
         (c-set-style "myCStyle")
         ;; never convert leading spaces to tabs
         (setq indent-tabs-mode nil)
           ....
      )))
    
    (add-hook 'c-mode-hook 'doug-c-mode-hook-fn)
    

    下一个问题是,如何确定您需要为任何特定情况设置哪些命名的 c 偏移量?有一个应用程序。好的,不是应用程序,而是 elisp 函数。

    M-x c-set-offset
    显示 emacs 在语法上认为它在 cc-mode 源文件中的位置。要使用它, 将光标放在您想了解偏移名称的位置,或更改偏移。然后调用这个函数。它会告诉您当前句法元素的偏移量的名称。
    如果您在 c-offsets-alist 中为您选择的样式更改该命名元素的值,这通常在 .emacs 或等效文件中完成,那么缩进 所有源模块的设置都为真。

    【讨论】:

    • 感谢您的详细解答。那是为了创造一种新的风格。如果我想从一种风格开始(例如 K&R),然后修改它以添加 (setq indent-tabs-mode nil)
    • 你可以使用 c-mode 钩子来做到这一点。请参阅我上面为doug-c-mode-hook-fn 写的内容。只需将(c-set-style "myCStyle") 替换为(c-set-style "k&r")。有关更多样式,请参见 cc-styles.el。
    【解决方案3】:

    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" 更改为样式的名称(作为字符串)。尝试使用 stroustruppython 内置样式来格式化你想要的 if 语句。

    【讨论】:

    • 我试图插入你写的行和加载文件,但我仍然看到 gnu 标准。我也试过“stroustrup”和“python”,没有区别。
    • 您是否有正在加载并覆盖您的设置的自定义文件?另外,我不确定是否将 setq-default 与用户样式变量一起使用 - 你可以尝试普通的 setq 吗? (但这并不能解释为什么 stroustrup/python 不起作用。)
    • 我不这么认为。我在原始帖子中添加了整个 .emacs,我认为它可能会有所帮助。有了“用户”,我几乎得到了我想要的。制表符没有被空格替换,我不明白为什么。
    • setq 代替 setq-default 没有区别。
    • init 文件上的加载文件不足以看到它的效果,我不得不重新打开缓冲区。对不起,那是愚蠢的!我试过(setq c-default-style "k&r")。这很好用。它有 5 个空格 (??) 的缩进,我将其更改为 setq-default c-basic-indent 4)。现在工作,谢谢!
    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    相关资源
    最近更新 更多