`

emacs的缩进和自动添加新行

阅读更多
emacs很强大,但是强大是以复杂的配置为前提的!没有配置好的时候,可能它比notepad还要难用。

前一段时间我就被缩进弄得晕头转向,tab经常是缩进5个空格,源码会乱,等等,我都几乎要放弃emacs了。

今天偶然看了看emacs自带的manual中的cc-mode一节,哇,发现新大陆了!不光讲得很细,还有个例子,拷过来就可以用了,那缩进,怎一个酷字了得!

offset参数详情

c-hanging-braces-alist 控制大括号后自动加新行,class-open after 表示在新建class的大括号后自动另起一行, 比如
class newClass {
...
}

这里可以根据自己的喜好修改,通过在想要设置自动新行的大括号前按RET(回车), 然后按C-c,C-s 找出大括号的语义(Syntactic context of brace), 然后在c-hanging-braces-alist中加入对它的设置。比如得到的语义为substatement-open, 就把c-hanging-braces-alist加入(substatement-open after)

如果想要在除了缩进的地方(每行开头)的其他地方使用TAB来插入多个空格, 而不是错定格式。
把(c-tab-always-indent        . t)
改为
(c-tab-always-indent        . nil)

后来又参考了王垠、ann77的主页,将tab和缩进基本搞定,下面是配置内容:

(setq indent-tabs-mode nil)
(setq default-tab-width 4)
(setq tab-width 4)
(setq tab-stop-list ())
(loop for x downfrom 40 to 1 do
      (setq tab-stop-list (cons (* x 4) tab-stop-list)))

(defconst my-c-style
  '((c-tab-always-indent        . t)
    (c-comment-only-line-offset . 0)
    (c-hanging-braces-alist     . ((substatement-open after)
                                   (brace-list-open)))
    (c-hanging-colons-alist     . ((member-init-intro before)
                                   (inher-intro)
                                   (case-label after)
                                   (label after)
                                   (access-label after)))
    (c-cleanup-list             . (scope-operator
                                   empty-defun-braces
                                   defun-close-semi))
    (c-offsets-alist            . ((arglist-close . c-lineup-arglist)
                                   (substatement-open . 0)
                                   (cpp-macro . 0)
                                   (case-label        . 4)
                                   (block-open        . 0)
                                   (knr-argdecl-intro . -)))
    (c-echo-syntactic-information-p . t)
    )
  "My C Programming Style")

;; offset customizations not in my-c-style
(setq c-offsets-alist '((member-init-intro . ++)))

;; Customizations for all modes in CC Mode.
(defun my-c-mode-common-hook ()
  ;; add my personal style and set it for the current buffer
  (c-add-style "PERSONAL" my-c-style t)
  ;; other customizations
  (setq tab-width 4
        ;; this will make sure spaces are used instead of tabs
        indent-tabs-mode nil)
  ;; we like auto-newline and hungry-delete
  (c-toggle-auto-hungry-state 1)
  ;; key bindings for all supported languages.  We can put these in
  ;; c-mode-base-map because c-mode-map, c++-mode-map, objc-mode-map,
  ;; java-mode-map, idl-mode-map, and pike-mode-map inherit from it.
  (define-key c-mode-base-map "\C-m" 'c-context-line-break)
  )

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)





Hanging Semicolons and Commas

— User Option: c-hanging-semi&comma-criteria

    This style variable takes a list of functions; these get called when you type a semicolon or comma. The functions are called in order without arguments. When these functions are entered, point is just after the newly inserted `;' or `,' and they must preserve point (e.g., by using save-excursion). During the call, the variable c-syntactic-context is bound to the syntactic context of the current line1 see Custom Braces. These functions don't insert newlines themselves, rather they direct CC Mode whether or not to do so. They should return one of the following values:

    t
        A newline is to be inserted after the `;' or `,', and no more functions from the list are to be called.
    stop
        No more functions from the list are to be called, and no newline is to be inserted.
    nil
        No determination has been made, and the next function in the list is to be called.

    Note that auto-newlines are never inserted before a semicolon or comma. If every function in the list is called without a determination being made, then no newline is added.

    In AWK mode, this variable is set by default to nil. In the other modes, the default value is a list containing a single function, c-semi&comma-inside-parenlist. This inserts newlines after all semicolons, apart from those separating for-clause statements.

— Function: c-semi&comma-no-newlines-before-nonblanks

    This is an example of a criteria function, provided by CC Mode. It prevents newlines from being inserted after semicolons when there is a non-blank following line. Otherwise, it makes no determination. To use, add this function to the front of the c-hanging-semi&comma-criteria list.

              (defun c-semi&comma-no-newlines-before-nonblanks ()
                (save-excursion
                  (if (and (eq last-command-char ?\;)
                           (zerop (forward-line 1))
                           (not (looking-at "^[ \t]*$")))
                      'stop
                    nil)))
        

— Function: c-semi&comma-inside-parenlist

    — Function: c-semi&comma-no-newlines-for-oneline-inliners

        The function c-semi&comma-inside-parenlist is what prevents newlines from being inserted inside the parenthesis list of for statements. In addition to c-semi&comma-no-newlines-before-nonblanks described above, CC Mode also comes with the criteria function c-semi&comma-no-newlines-for-oneline-inliners, which suppresses newlines after semicolons inside one-line inline method definitions (e.g. in C++ or Java).

用法: 在c-hanging-colons-alist后
(c-hanging-colons-alist . ((case-label)
                               (label after)
                               (access-label after)
                               (member-init-intro before)
                               (inher-intro)))
    (c-hanging-semi&comma-criteria
     . (c-semi&comma-no-newlines-before-nonblanks
        c-semi&comma-inside-parenlist
        c-semi&comma-no-newlines-for-oneline-inliners))
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics