jeudi 3 mars 2016

Guess C indent rules based on the buffer content

Guess basic indent rules from the buffer content. You can add this to the c-mode hook, works good enough.

(defun guess-c-indent-rules ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (cond
     ;; check GNU first (1-level indent is 2 space)
     ((search-forward-regexp (rx bol "  " (or "if" "do" "while" "for" "return")) nil t)
      (message "GNU style detected, setting it...")
      (c-set-style "gnu"))

     ;; linux style tab indent (samba)
     ((search-forward-regexp (rx bol (+ "\t") (or "if" "do" "while" "for"))  nil t)
      (message "indenting with 8-spaces tabs detected, linux style...")
      (c-set-style "linux")
      (setq indent-tabs-mode t
     c-basic-offset 8))

     ;; 4 space mode
     ((search-forward-regexp (rx bol (+ "    ") (or "if" "do" "while" "for")) nil t)
      (message "indenting with 4 spaces...")
      (setq indent-tabs-mode nil
     c-basic-offset 4))
     (t
      (message "cannot guess indentation, you're on your own!")))))

Aucun commentaire:

Enregistrer un commentaire