On Mon, Aug 25, 2014 at 10:44:12AM -0700, Jarno Rajahalme wrote:
> Btw, is there an Emacs setting that I could use to indent function
> calls like this by pressing the tab-key? Mine wants the parameters to
> be indented after the opening parenthesis.

I'll respond to the review bits later (I'm at VMworld today), but for
now, here's my C style file for Emacs.

It actually looks like I just use Emacs' "bsd" style for OVS code (note
the string match against "nicira" in the filename in blp-c-setup).

(add-hook 'c-mode-hook 'blp-c-setup)
(add-hook 'c++-mode-hook 'blp-c++-setup)
(add-hook 'java-mode-hook 'blp-java-setup)
(add-hook 'c-mode-common-hook 'blp-c-common-setup)

;; Always put # in column 0.
(setq c-electric-pound-behavior '(alignleft))

;; Returns true if the last character typed by the user was a
;; semicolon.  (This is a gross hack.)
(defun newline-if-semi ()
  (= last-command-char ?\;))

;; My style for C, based on GNU style.
(defconst blp-c-style
  '((c-block-comment-prefix . "  ")
    (c-basic-offset . 2)

    ;; Put opening { on line by itself.
    (c-hanging-braces-alist . ((brace-list-open . (before after))))

    ;; Insert a newline after a typed semicolon if the line after this
    ;; one is blank and we're not inside a set of parentheses.
    ;; Never insert a newline after a comma.
    (c-hanging-semi&comma-criteria . (c-semi&comma-no-newlines-before-nonblanks
                                      c-semi&comma-inside-parenlist
                                      newline-if-semi))

    ;; Taken mostly unchanged from cc-styles.el.
    (c-offsets-alist . ((statement-block-intro . +)
                        (knr-argdecl-intro . 5)
                        (substatement-open . +)
                        (label . 0)
                        (statement-case-open . +)
                        (statement-cont . +)
                        (arglist-intro . c-lineup-arglist-intro-after-paren)
                        (arglist-close . c-lineup-arglist)

                        ;; typedef struct foo
                        ;;   {
                        ;;     /* ... */
                        ;;   }
                        ;; bar;
                        (class-open . 2)
                        (class-close . 2)

                        ;; enum foo
                        ;;   {
                        ;;     /* ... */
                        ;;   };
                        (brace-list-open . 2)
                        (brace-list-close . 0)

                        ;; long_function_name ("abc", "def",
                        ;;                     "ghi");
                        ;; long_function_name (
                        ;;     "abc", "def",
                        ;;     "ghi")
                        (arglist-intro . +))))
  "blp's C style, based on GNU style")

;; These ease some situations in C++ classes, so that I don't have to
;; type spaces in some places; e.g., in `class X {' I can skip typing
;; the space before `{' and in a one-line inline function definition
;; `void foo() { bar(); }' I can skip the space before and after the
;; `{' (in fact the space before `}' as well, but not due to this
;; code).
;;;; Commented out because this causes trailing spaces on lines.
;;;; Need to revisit this if I ever start writing lots of C++ again.
;; (defadvice c-electric-brace (before space-before-brace activate)
;;   "Automatically add a space before braces."
;;   (when (not (c-in-literal))
;;     (just-one-space)))
;; (defadvice c-electric-brace (after space-after-brace activate)
;;   "Automatically add a space after braces in some situations."
;;   (when (not (c-in-literal))
;;     (just-one-space)
;;     (c-indent-line)))

;; My C++ style.  I don't like GNU style so much for C++, so this is
;; quite different from my C style.
(defconst blp-c++-style
  '((fill-column . 79)                  ; added by blp
    (show-trailing-whitespace . t)      ; added by blp

    (c-block-comment-prefix . "* ")     ; added by blp
    (comment-start . "/* ")             ; added by blp
    (comment-end . "*/")                ; added by blp
    
    (c-basic-offset . 4)

    ;; Clean up some common constructs.
    (c-cleanup-list . (brace-else-brace   ; } else {
                       brace-elseif-brace ; } else if (x) {
                       brace-catch-brace  ; } catch (...) {
                       empty-defun-braces ; { }
                       defun-close-semi   ; };
                       list-close-comma   ; },
                       scope-operator))   ; ::


    ;; Hang some braces but not others
    (c-hanging-braces-alist . ((class-open . (before after))   ; class 
                                                               ; {
                               (inline-open)                   ;   int foo() {
                               (inline-close after)            ;   }
                               (brace-list-open)               ; enum {
                               (brace-entry-open)              ;   {
                               (substatement-open after)       ; for (..) {
                               (namespace-open after)          ; namespace foo {
                               (block-close . c-snug-do-while) ; } while (...);
                               (extern-lang-open after)))      ; extern "C" {

    ;; class A
    ;;   : B {
    (c-hanging-colons-alist . ((inher-intro before)))

    ;; Like C, but suppress newlines after semicolons in short inline
    ;; member functions, too.
    (c-hanging-semi&comma-criteria
     . (c-semi&comma-no-newlines-before-nonblanks
        c-semi&comma-no-newlines-for-oneline-inliners
        c-semi&comma-inside-parenlist
        newline-if-semi))

    ;; Mostly the same as Stroustrup style.
    (c-offsets-alist . ((statement-block-intro . +)
                        (substatement-open . 0)
                        (label . 0)
                        (statement-cont . +)

                        ;; long_function_name ("abc", "def",
                        ;;                     "ghi");
                        ;; long_function_name (
                        ;;     "abc", "def",
                        ;;     "ghi")
                        (arglist-intro . +)

                        ;; namespace foo {
                        ;; int x;
                        ;; }
                        (innamespace . 0)

                        ;; extern "C" {
                        ;; #include <foo.h>
                        ;; }
                        (inextern-lang . 0))))
  "blp's C++ style, based on Stroustrup style")

;; My Java style, which is not very original.
(defconst blp-java-style
  '((c-block-comment-prefix . "* ")

    ;; Hang lots of braces.
    (c-hanging-braces-alist . ((class-open after)                  ; class X {
                               (class-close before after)          ; }
                               (defun-open after)                  ; int foo () 
{
                               (defun-close before after)          ; }
                               (inline-open after)                 ; int foo () 
{
                               (inline-close before after)         ; }
                               (block-open after)                  ; if (x) {
                               (block-close before after)          ; }
                               (substatement-open after)           ; if (x) {
                               (statement-case-open after)         ; case 1: {
                               (inexpr-class-open after)           ; new X() {
                               (inexpr-class-close before after))) ; }

    ;; Same as C.
    (c-hanging-semi&comma-criteria . (c-semi&comma-no-newlines-before-nonblanks
                                      c-semi&comma-inside-parenlist
                                      newline-if-semi))

    ;; Cleanups.
    (c-cleanup-list . (brace-else-brace   ; } else {
                       brace-elseif-brace ; } else if (...) {
                       brace-catch-brace  ; } catch (...) {
                       defun-close-semi   ; };
                       list-close-comma)) ; },

    ;; long_function_name ("abc", "def",
    ;;                     "ghi");
    ;; long_function_name (
    ;;     "abc", "def",
    ;;     "ghi")
    (c-offsets-alist . ((arglist-intro . +))))
  "blp's Java style")

;; Same as ccmode default "linux" style except as noted.
(defconst blp-linux-style
  '((fill-column . 79)                  ; added by blp
    (show-trailing-whitespace . t)      ; added by blp
    (indent-tabs-mode . t)              ; added by blp
    (c-block-comment-prefix . "* ")     ; added by blp
    (c-basic-offset  . 8)
    (c-comment-only-line-offset . 0)
    (c-hanging-braces-alist . ((brace-list-open)
                               (brace-entry-open)
                               (substatement-open after)
                               (block-close . c-snug-do-while)))
    (c-cleanup-list . (brace-else-brace))
    (c-offsets-alist . ((statement-block-intro . +)
                        (knr-argdecl-intro     . 0)
                        (substatement-open     . 0)
                        (substatement-label    . 0)
                        (label                 . 0)
                        (statement-cont        . +)))))

;; Setup wanted for all C-like modes.
(defun blp-c-common-setup ()
  ;; Even though indentation is not in multiples of 8 spaces, the
  ;; width of a physical tab character is.  This way the standard Unix
  ;; system utilities, not to mention standard terminals, do sensible
  ;; things.
  (setq tab-width 8)

  ;; Do not indent with tab characters.
  (setq indent-tabs-mode nil)

  ;; RFC 1855 compliant.
  (setq fill-column 79)

  (auto-fill-mode t)
  (turn-on-font-lock)
  (c-toggle-auto-hungry-state 1)

  (set (make-local-variable 'dabbrev-case-fold-search) nil)

  ;; Simplifies braced blocks: just type M-{ at the beginning, type
  ;; some statements, type M-} to end it, and no need to worry about
  ;; anything else.
  (local-set-key [?\M-{] 'insert-brace)
  (local-set-key [?\M-}] 'move-past-brace-and-reindent))

;;; Language-specific setups.
(defun blp-c-setup ()
  (c-add-style "blp" blp-c-style t)
  (c-add-style "linux" blp-linux-style nil)
  (let ((fn (buffer-file-name)))
    (when (not (stringp fn))
      (setq fn ""))
    (cond
     ((string-match "\\(datapath\\|linux\\).*/" fn)
      (c-set-style "linux"))
     ((string-match "nicira\\|vigil\\|openflow\\|ovs\\|openvswitch" fn)
      (c-set-style "bsd")
      (setq c-basic-offset 4)
      (setq tab-width 4)
      (setq fill-column 79))
     (t (c-set-style "blp"))))
  (setq show-trailing-whitespace t))
(defun blp-c++-setup ()
  (c-add-style "blp" blp-c++-style t))
(defun blp-java-setup ()
  ;; When working on Collective stuff, omit the space between a
  ;; function name and left parenthesis, otherwise include it.
  (when (let ((fn (buffer-file-name)))
          (when (not (stringp fn))
            (setq fn ""))
          (string-match "\\bwork\\b\\|collective" fn))
    (set (make-local-variable 'parens-require-spaces) nil))
  (c-add-style "blp" blp-java-style t)

  ;; Auto fill mode doesn't work well for Java.
  (auto-fill-mode nil))

;; Inserts a pair of braces {}, properly spacing them, and puts the
;; cursor between them.
(defun insert-brace ()
  (interactive)
  (setq last-command-event ?{)
  (c-electric-brace nil)
  (c-indent-line)
  (save-excursion
    (insert "\n\n}")
    (c-indent-command)))
;; (defun insert-brace ()
;;   (interactive)
;;   (setq last-command-event ?{)
;;   (c-electric-brace nil)
;;   (c-indent-line)
;;   (save-excursion
;;     (insert "\n\n")
;;     (let ((blink-matching-paren nil))
;;       (setq last-command-event ?})
;;       (c-electric-brace nil))))

;; Cleans up blank lines, moves past the next }, and reindents.
(defun move-past-brace-and-reindent ()
  (interactive)
  (delete-all-blank-lines)
  (search-forward "}")
  (let ((save-point))
    (save-excursion
      (insert "\n")
      (c-indent-line)
      (setq save-point (point)))
    (funcall blink-paren-function)
    (goto-char save-point)))
(defun delete-all-blank-lines ()
  "Delete all surrounding blank lines."
  (interactive "*")
  (save-excursion
    (beginning-of-line)
    (if (looking-at "[ \t]*$")
        (delete-region (if (re-search-backward "[^ \t\n]" nil t)
                           (progn (forward-line 1) (point))
                         (point-min))
                       (if (re-search-forward "[^ \t\n]" nil t)
                           (progn (beginning-of-line) (point))
                         (point-max))))))
_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to