Emacs压痕难度

Gau*_*ier 8 emacs indentation

我真的想切换到emacs,但学习设置环境真的很痛苦.每个人都说它值得,所以我继续说.

我希望我的c代码以这种方式实现:

if(asdf)
{
    asdr = 1;
}
Run Code Online (Sandbox Code Playgroud)

根据当前的标准(我知道,不要让我开始),可能是:

if(asdf) {
    asdr = 1;
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法将缩进大小从2更改,它总是看起来像GNU标准:

if(asdf)
  {
    asdr = 1;
  }
Run Code Online (Sandbox Code Playgroud)

,我不喜欢.这是我在.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)
Run Code Online (Sandbox Code Playgroud)

这没有用,我还有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)
Run Code Online (Sandbox Code Playgroud)

Ste*_*wig 6

该EmacsWiki有一个很好的文章IndentingC.

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


Che*_*eso 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)
    ))
  ))
Run Code Online (Sandbox Code Playgroud)

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

  • 0 暗示,保持缩进与前一行相同
  • + 暗示,增加缩进,增加一个级别
  • - 暗示,减少缩进,一个额外的水平

您也可以使用++, - 等.在.emacs文件中添加类似的东西来定义样式.然后,要在编辑C文件时自动使用该样式,请c-set=style在c模式挂钩中使用,如下所示:

(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)
Run Code Online (Sandbox Code Playgroud)

接下来的问题是,如何确定您需要为任何特定情况设置哪个命名的c-offset?有一个应用程序.好的,不是应用程序,而是elisp功能.

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

替代文字http://i44.tinypic.com/262nlup.jpg


Dou*_*oug 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"上述命令更改为样式名称(作为字符串)。尝试使用 thestroustruppython 内置样式来按照您想要的方式格式化 if 语句。