Emacs - override indentation

后端 未结 6 1203
悲&欢浪女
悲&欢浪女 2020-12-29 23:24

I have a multiply nested namespace:

namespace first {namespace second {namespace third {
              // emacs indents three times
    // I want to intend h         


        
相关标签:
6条回答
  • 2020-12-30 00:03

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

    0 讨论(0)
  • 2020-12-30 00:08

    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;
        }
    }}
    

    )

    0 讨论(0)
  • 2020-12-30 00:14

    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)
    
    0 讨论(0)
  • 2020-12-30 00:16

    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.

    0 讨论(0)
  • 2020-12-30 00:26

    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'.

    0 讨论(0)
  • 2020-12-30 00:26

    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

    0 讨论(0)
提交回复
热议问题