Emacs - override indentation

怎甘沉沦 提交于 2019-11-30 06:35:40
Jürgen Hötzel

Use an an absolute indentation column inside namespace:

(defconst my-cc-style
  '("gnu"
    (c-offsets-alist . ((innamespace . [4])))))

(c-add-style "my-cc-style" my-cc-style)

Then use c-set-style to use your own style.

Note that this only works in c++-mode, c-mode doesn't know 'innamespace'.

With c++-mode in Emacs 23, I had to do like this:

(defun my-c-setup ()
   (c-set-offset 'innamespace [4]))
(add-hook 'c++-mode-hook 'my-c-setup)

To disable the indentation in namespaces altogether, change [4] to 0.

OK so this seems to work in both emacs 21 and 22 at least:

(defun followed-by (cases)
  (cond ((null cases) nil)
        ((assq (car cases) 
               (cdr (memq c-syntactic-element c-syntactic-context))) t)
        (t (followed-by (cdr cases)))))

(c-add-style  "foo"      
              `(( other . personalizations )
        (c-offsets-alist
         ( more . stuff )
         (innamespace
          . (lambda (x) 
          (if (followed-by 
               '(innamespace namespace-close)) 0 '+))))))

(The first solution doesn't support constructs like

namespace X { namespace Y {
    class A;
    namespace Z {
        class B;
    }
}}

)

If you simply want to input a literal tab, rather than changing emacs' indentation scheme, C-q TAB should work.

Unfortunately, I don't think emacs has a separate style for a namespace inside another namespace. If you go to the inner line and do C-c, C-o, you can change the topmost-intro style, and if you run customize-variable c-offsets-alist you can edit all the different indentation options emacs has, but one doesn't exist for your specific use case. You would need to write it manually in elisp

This works for me, inherit from cc-mode and replace the name space indenting to 0, aka, disable it's indent.

(defconst my-cc-style
  '("cc-mode"
    (c-offsets-alist . ((innamespace . [0])))))

(c-add-style "my-cc-mode" my-cc-style)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!