Emacs Reformatting

From Apache OpenOffice Wiki
Jump to: navigation, search

If you've setup a proper c-mode in Emacs (see here), indenting single lines or whole regions of code is usually only a matter of pressing tab, or executing (indent-region) (mapped to M-C-\ by default).

For the more complex parts, a customized align-regexp has proven useful:

(defun ooo-align-func (begin end)
  "
Align method or function declaration, where the arguments are spread across multiple lines.

Example:

void test( const this& aVar,
          that& aVar,
        those aVar,
       const them& aVar );

is aligned like that:

void test( const this& aVar,
           that&       aVar,
           those       aVar,
           const them& aVar );

Works on the current region.
"
  (interactive "r")
  ;; Works like this: second last argument of align-regexp selects
  ;; third pair of bracket expression as the one to align. The prefix
  ;; (either spaces or text followed by an open bracket) is just
  ;; skipped.  After that, the first expression (expressions are
  ;; separated by commas) aligns the type part, the second one the
  ;; variable part.
  (align-regexp "\\(.*(\\|\\s-+\\)\\(\\(const\\)*\\s-*\\S-+\\),\\(.*(\\|\\s-+\\)\\(\\w+\\s-*\\(\\,\\|)\\)\\s-*$\\)" begin end 1 ?  2 0))  
(defun ooo-align-var-decl (begin end)
  "
Align variables in a multiple-line declaration block.

Example:

 const this& aVar;
     that& aVar;
    those aVar;
   const them& aVar;
 theirs aVar(hello); 
   theirs aVar = hello;

is aligned like that:

 const this& aVar;
 that&       aVar;
 those       aVar;
 const them& aVar;
 theirs      aVar(hello); 
 theirs      aVar = hello;

Works on the current region.
"
  (interactive "r")
  (align-regexp "\\(const\\)*\\s-*\\S-+,\\w+\\s-*\\(;\\|\\s-*=.*;\\|\\s-*(.*)\\s-*;\\)\\s-*$" begin end 1))  
Personal tools