Emacs cc-mode缩进设置的问题

Vim、Emacs配置和使用
回复
marazone
帖子: 67
注册时间: 2012-02-27 13:51
系统: Fedora 17
来自: 湖南长沙
联系:

Emacs cc-mode缩进设置的问题

#1

帖子 marazone » 2013-07-04 8:56

我的Emacs基本照抄purcell的设置(https://github.com/purcell/emacs.d),可惜里面没有C/C++语言的设置,于是我自己找了一个配置文件添加了进去:

代码: 全选

(defun c-wx-lineup-topmost-intro-cont (langelem)
  (save-excursion
    (beginning-of-line)
    (if (re-search-forward "EVT_" (line-end-position) t)
      'c-basic-offset
      (c-lineup-topmost-intro-cont langelem))))

;;===== hack gud-mode begin
;; move the cursor to the end of last line if it's gud-mode
(defun hack-gud-mode ()
  (when (string= major-mode "gud-mode")
    (goto-char (point-max))))

(defadvice switch-to-buffer (after switch-to-buffer-after activate)
  (hack-gud-mode))

;; from switch-window is from 3rd party plugin switch windows.el
(defadvice switch-window (after switch-window-after activate)
  (hack-gud-mode))

;; windmove-do-window-select is from windmove.el
(defadvice windmove-do-window-select (after windmove-do-window-select-after activate)
  (hack-gud-mode))
;; ==== end

;C/C++ SECTION
(defun my-c-mode-hook ()
  ;; @see http://stackoverflow.com/questions/3509919/ \
  ;; emacs-c-opening-corresponding-header-file
  (local-set-key (kbd "C-x C-o") 'ff-find-other-file)
  (local-set-key "\M-f" 'c-forward-into-nomenclature)
  (local-set-key "\M-b" 'c-backward-into-nomenclature)
  (setq cc-search-directories '("." "/usr/include" "/usr/local/include/*" "../*/include" "$WXWIN/include"))
  (setq c-default-style "k&r"
        c-basic-offset 4
        indent-tabs-mode t
        default-tab-width 4)
;  (setq c-style-variables-are-local-p nil)
  ;give me NO newline automatically after electric expressions are entered
 ; (setq c-auto-newline nil)

  ; @see http://xugx2007.blogspot.com.au/2007/06/benjamin-rutts-emacs-c-development-tips.html
  (setq compilation-window-height 8)
  (setq compilation-finish-function
        (lambda (buf str)
          (if (string-match "exited abnormally" str)
              ;;there were errors
              (message "compilation errors, press C-x ` to visit")
              ;;no errors, make the compilation window go away in 0.5 seconds
            (when (string-match "*compilation*" (buffer-name buf))
              ;; @see http://emacswiki.org/emacs/ModeCompile#toc2
              (bury-buffer "*compilation*")
              (winner-undo)
              (message "NO COMPILATION ERRORS!")
              ))))

  ;if (0)          becomes        if (0)
  ;    {                          {
  ;       ;                           ;
  ;    }                          }
  (c-set-offset 'substatement-open '0)

  ;first arg of arglist to functions: tabbed in once
  ;(default was c-lineup-arglist-intro-after-paren)
  (c-set-offset 'arglist-intro '+)

  ;second line of arglist to functions: tabbed in once
  ;(default was c-lineup-arglist)
  (c-set-offset 'arglist-cont-nonempty '+)

  ;switch/case:  make each case line indent from switch
  (c-set-offset 'case-label '+)

  ;make the ENTER key indent next line properly
  (local-set-key "\C-m" 'newline-and-indent)

  ;syntax-highlight aggressively
  ;(setq font-lock-support-mode 'lazy-lock-mode)
  (setq lazy-lock-defer-contextually t)
  (setq lazy-lock-defer-time 0)

  ;make DEL take all previous whitespace with it
  (c-toggle-hungry-state 1)

  ;make open-braces after a case: statement indent to 0 (default was '+)
  (c-set-offset 'statement-case-open 0)

  ;make a #define be left-aligned
  (setq c-electric-pound-behavior (quote (alignleft)))

  ;wxwdigets stuff
  (c-set-offset 'topmost-intro-cont 'c-wx-lineup-topmost-intro-cont)

  ;do not impose restriction that all lines not top-level be indented at least
  ;1 (was imposed by gnu style by default)
  (setq c-label-minimum-indentation 0)

  ;(setq gtags-suggested-key-mapping t)
  ;(gtags-mode 1)

 ; (require 'fic-mode)
;  (add-hook 'c++-mode-hook 'turn-on-fic-mode)

  ; @see https://github.com/seanfisk/cmake-flymake
  ; make sure you project use cmake
;  (flymake-mode)
  ; (cppcm-reload-all)

  )
;; donot use c-mode-common-hook or cc-mode-hook because many major-modes use this hook
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'c++-mode-hook 'my-c-mode-hook)

(provide 'init-cc-mode)

里面有设定缩进的部分:

代码: 全选

  ;if (0)          becomes        if (0)
  ;    {                          {
  ;       ;                           ;
  ;    }                          }
  (c-set-offset 'substatement-open 0)
但是,打出括号,回车之后,默认缩进效果还是如下:
缩进问题
缩进问题
Selection_004.png (10.28 KiB) 查看 3309 次
缩进问题
缩进问题
Selection_004.png (10.28 KiB) 查看 3309 次
经过手动按TAB可以变成如下效果:

代码: 全选

#include <stdio.h>

int main()
{
    
}
请问,我的设置哪里有问题呢?该怎么改正?多谢各位了!
头像
tonghuix
帖子: 355
注册时间: 2010-10-08 0:55

Re: Emacs cc-mode缩进设置的问题

#2

帖子 tonghuix » 2013-07-14 2:11

代码: 全选

(setq c-default-style "k&r"    
        c-basic-offset 4
        indent-tabs-mode t
        default-tab-width 4)

怀疑问题出在这里
/***
* @tonghuix 佟辉 Tong Hui
* @brief 启智开源 编码自由
* @brief Open Source Open Mind, Coding for free!
* @website: https://tonghuix.io
* @weibo http://weibo.com/234826309
* @Twitter http://twitter.com/tonghuix
* @G+ http://gplus.to/tonghuix
*/
czopp
帖子: 162
注册时间: 2006-05-22 12:55

Re: Emacs cc-mode缩进设置的问题

#3

帖子 czopp » 2013-07-24 16:08

看了一下这个配置,基本属于作者为自己的习惯做了一些设定,就是设定某些变量的值。
个人感觉没有必要去抄过来。
;make the ENTER key indent next line properly
(local-set-key "\C-m" 'newline-and-indent)
这个就略显不妥,也许在cc-mode中还能用。
但重要的时24.x版本中electric-indent-mode (minor mode)可以在几乎所有的progmode中使用,
即当敲下回车时自动缩进当前行,然后新起一行。

如果k&r style不是必须的,而默认的gnu style是你想要的,
那么default cc-mode + electric-indent-mode 基本就是很理想的状态了,且仅需要一行配置

代码: 全选

(electric-indent-mode 1)
前提是emacs需要24.x版本。

关于你的问题,感觉你想要的缩进效果就是gnu style (默认)的。
所以,去掉那些不必要的配置吧。
回复