1;;; -*- emacs-lisp -*- 2;;; 3;;; ruby-style.el - 4;;; 5;;; C/C++ mode style for Ruby. 6;;; 7;;; $Author: nobu $ 8;;; created at: Thu Apr 26 13:54:01 JST 2007 9;;; 10;;; To switch to the "ruby" style automatically if it looks like a 11;;; source file of ruby, add ruby-style-c-mode to c-mode-hook: 12;;; 13;;; (require 'ruby-style) 14;;; (add-hook 'c-mode-hook 'ruby-style-c-mode) 15;;; (add-hook 'c++-mode-hook 'ruby-style-c-mode) 16;;; 17;;; Customize the c-default-style variable to set the default style 18;;; for each CC major mode. 19 20(defconst ruby-style-revision "$Revision: 30485 $" 21 "Ruby style revision string.") 22 23(defconst ruby-style-version 24 (and 25 (string-match "[0-9.]+" ruby-style-revision) 26 (substring ruby-style-revision (match-beginning 0) (match-end 0))) 27 "Ruby style version number.") 28 29(defun ruby-style-case-indent (x) 30 (save-excursion 31 (back-to-indentation) 32 (unless (progn (backward-up-list) (back-to-indentation) 33 (> (point) (cdr x))) 34 (goto-char (cdr x)) 35 (if (looking-at "\\<case\\|default\\>") '*)))) 36 37(defun ruby-style-label-indent (x) 38 (save-excursion 39 (back-to-indentation) 40 (unless (progn (backward-up-list) (back-to-indentation) 41 (>= (point) (cdr x))) 42 (goto-char (cdr x)) 43 (condition-case () 44 (progn 45 (backward-up-list) 46 (backward-sexp 2) 47 (if (looking-at "\\<switch\\>") '/)) 48 (error))))) 49 50(require 'cc-styles) 51(c-add-style 52 "ruby" 53 '("bsd" 54 (c-basic-offset . 4) 55 (tab-width . 8) 56 (indent-tabs-mode . t) 57 (setq show-trailing-whitespace t) 58 (c-offsets-alist 59 (case-label . *) 60 (label . (ruby-style-label-indent *)) 61 (statement-case-intro . *) 62 (statement-case-open . *) 63 (statement-block-intro . (ruby-style-case-indent +)) 64 (access-label /) 65 ))) 66 67(defun ruby-style-c-mode () 68 (interactive) 69 (if (or (let ((name (buffer-file-name))) (and name (string-match "/ruby\\>" name))) 70 (save-excursion 71 (goto-char (point-min)) 72 (let ((head (progn (forward-line 100) (point))) 73 (case-fold-search nil)) 74 (goto-char (point-min)) 75 (re-search-forward "Copyright (C) .* Yukihiro Matsumoto" head t)))) 76 (c-set-style "ruby"))) 77 78(provide 'ruby-style) 79