Emacs压痕难度

时间:2010-03-17 11:59:17

标签: emacs indentation

我真的想切换到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)

3 个答案:

答案 0 :(得分:6)

EmacsWiki在IndentingC上有一篇很好的文章。

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

答案 1 :(得分:6)

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

if语句后面的开放卷曲有一个名字的偏移量。如果更改偏移值,则开卷将以不同方式缩进。同样,函数声明后的开卷也有一个命名的偏移量。宏中的行连续具有命名的偏移量。 switch语句中的case标签,while while循环。他们都有补偿。有大量的'em,都存储在一个名为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-offset?有一个应用程序。好的,不是应用程序,而是elisp功能。

  

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

alt text http://i44.tinypic.com/262nlup.jpg

答案 2 :(得分: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语句的格式。