HowTo xemacsIndentation

From OpenFOAMWiki
Revision as of 14:56, 4 December 2019 by Volker Weißmann (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The mighty XEmacs/Emacs-editor can be set up to automatically support the indentation-style favored by the OpenFOAM-developers.

The following two ELisp-snipplets have to be inserted into a file where your (X)Emacs will execute them at start-up (~/.emacs or similar - consult your documentation)

It has been split into two parts in order to give all users at a site

  • the possibility to use this style (and maintain it in a central place)
  • have the possibilty to not use this style

If you're working on a single-user machine or your admin is non-cooperative both snipplets can go to your personal initialization file.

1 Site-wide initialization

This part goes into a file that is executed for every user (your SysAdmin knows how to do that and will be glad to help you):

 
;; for OpenFOAM
(c-add-style "OpenFOAM_HGW"
	     '(
     (c-basic-offset . 4)
     (c-tab-always-indent . t)
     (indent-tabs-mode . nil)
     (c-comment-only-line-offset . (0 . 0))
     (c-indent-comments-syntactically-p . t)
     (c-block-comments-indent-p nil)
     (c-cleanup-list .
         '((defun-close-semi) (list-close-comma) (scope-operator)))
     (c-backslash-column . 48)
     (c-offsets-alist .
     (
     (c . +)                     ;; inside a multi-line C style block  comment
     (defun-open . 0)            ;; brace that opens a function definition
     (defun-close . 0)           ;; brace that closes a function definition
     (defun-block-intro . +)     ;; the first line in a top-level defun
     (class-open . 0)            ;; brace that opens a class definition
     (class-close . 0)           ;; brace that closes a class definition
     (inline-open . +)           ;; brace that opens an in-class inline method
     (inline-close . 0)          ;; brace that closes an in-class inline method
     (topmost-intro . 0)         ;; the first line in a topmost construct
                                 ;; definition
     (topmost-intro-cont . 0)    ;; topmost definition continuation lines
     (member-init-intro . +)     ;; first line in a member initialization list
     (member-init-cont . 0)      ;; subsequent member initialization list lines
     (inher-intro . 0)           ;; first line of a multiple inheritance list
     (inher-cont . +)            ;; subsequent multiple inheritance lines
     (block-open . 0)            ;; statement block open brace
     (block-close . 0)           ;; statement block close brace
     (brace-list-open . 0)       ;; open brace of an enum or static array list
     (brace-list-close . 0)      ;; open brace of an enum or static array list
     (brace-list-intro . +)      ;; first line in an enum or static array list
     (brace-list-entry . 0)      ;; subsequent lines in an enum or static array
                                 ;; list
     (statement . 0)             ;; a C/C++/ObjC statement
     (statement-cont . +)        ;; a continuation of a C/C++/ObjC statement
     (statement-block-intro . +) ;; the first line in a new statement block
     (statement-case-intro . +)  ;; the first line in a case `block'
     (statement-case-open . 0)   ;; the first line in a case `block'
                                 ;; starting with brace
     (substatement . +)          ;; the first line after an if/while/for/do/else
     (substatement-open . 0)     ;; the brace that opens a substatement block
     (case-label . +)            ;; a case or default label
     (access-label . -)          ;; C++ private/protected/public access label
     (label . -)                 ;; any non-special C/C++/ObjC label
     (do-while-closure . 0)      ;; the `while' that ends a do/while construct
     (else-clause . 0)           ;; the `else' of an if/else construct
     (comment-intro . 0)         ;; line containing only a comment introduction
     (arglist-intro . +)         ;; the first line in an argument list
     (arglist-cont . 0)          ;; subsequent argument list lines when no
                                 ;; subsequent argument list lines
                                 ;; when no the
                                 ;; arglist opening paren
     (arglist-cont-nonempty . 0) ;; subsequent argument list lines when at
                                 ;; subsequent argument list lines
                                 ;; when at line
                                 ;; as the arglist opening paren
     (arglist-close . 0)         ;; line as the arglist opening paren
     (stream-op . +)             ;; lines continuing a stream operator construct
     (inclass . +)               ;; the construct is nested inside a class
                                 ;; definition
     (cpp-macro . +)             ;; the construct is nested inside a class
                                 ;; definition
     (friend . 0)                ;; a C++ friend declaration
     )
     )
     )
)
 
(defun openfoam-hgw-c-mode-hook ()
  (c-set-style "OpenFOAM_HGW")
  )

2 Per-user initialization

Users that want to use that style as a default only have to add this line to their initialization-file (of course this can be done for the whole site if you don't want to give people the choice):

 
(add-hook 'c-mode-common-hook 'openfoam-hgw-c-mode-hook)

3 Manual activation

If the style is not activated for a user automatically it can be activated by calling the c-set-style-function in Emacs and selecting it.

4 GNU Emacs

4.1 Namespace indentation

It has been reported that GNU Emacs has different presets for namespaces that XEmacs. To have namespaces correctly indented in GNU Emacs add that following settings to the alist above:

 
(namespace-open  . 0)
(namespace-close . 0)
(innamespace     . 0)
 

4.2 C preprocessor statement indentation

When using GNU Emacs, the following indentation setting

 
     (cpp-macro . +)             ;; the construct is nested inside a class
                                 ;; definition

will generate an automatic indentation of the "#include" statements, which is rather annoying. The following setting might be a better alternative:

 
     (cpp-macro . c-lineup-cpp-define)  ;; indent accroding to construct preceding macro