2010-03-17 125 views
8

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

回答

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語句的方式。

+0

我試圖插入你寫的線路和負載文件,但我還是看到了GNU標準。 我也試過用「stroustrup」和「python」,沒有什麼區別。 – Gauthier 2010-03-17 12:28:21

+0

您是否有正在加載的自定義文件並覆蓋您的設置? 另外,我不確定使用setq-default與用戶風格的變量 - 你可以嘗試普通的setq? (這並不能解釋爲什麼stroustrup/python無法正常工作) – Doug 2010-03-17 12:41:03

+0

我不這麼認爲。我在原始文章中添加了我的整個.emacs,我認爲這可能會有所幫助。 用「用戶」我幾乎得到我想要的。標籤不能被空格替換,我不明白爲什麼。 – Gauthier 2010-03-17 13:14:18

6

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

事實上,EmacsWiki在各方面都有不錯的文章。我不想在Emacs存在之前學會它。

+0

'(setq c-default-style「k&r」)'工作!我不知道爲什麼Doug寫的那一行沒有。也許我把它放在我的.emacs中的錯誤位置。 但是,當我使用它時,標籤保留標籤字符,雖然我告訴它用'(setq-default indent-tabs-mode nil)'用空格替換它們。 – Gauthier 2010-03-17 12:46:00

+0

yap。當改變我的初始化。el文件,通常我打開一個emacs實例來編輯init.el文件,然後打開並關閉另一個實例以測試更改。 – Cheeso 2010-03-17 14:22:36

6

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言下之意,保持縮進一樣前行
  • +言下之意,增加縮進,一個額外的水平
  • -暗示,下降ase縮進,一個額外的級別

您還可以使用++, - 等等。在.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模式源文件。要使用它, 將光標置於您想要了解偏移量名稱的位置,或更改偏移量。然後調用這個函數。它會告訴你當前語法元素的偏移量的名稱。
如果您更改所選樣式的c-offset-alist中該已命名元素的值(通常以.emacs或等效值完成的操作),則所有源模塊的縮進 設置均爲true。

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

+0

謝謝你的詳細解答。這是爲了創造一種新風格。如果我想從一種風格(比如說K&R)開始,並修改它以添加'(setq indent-tabs-mode nil)'? – Gauthier 2010-03-17 13:46:18

+0

你可以使用c-mode鉤子來做到這一點。看看我上面寫的'doug-c-mode-hook-fn'。只需用'(c-set-style「k&r」)替換'(c-set-style「myCStyle」)''。有關更多樣式,請參閱cc-styles.el。 – Cheeso 2010-03-17 14:05:39