{"id":156,"date":"2011-03-08T08:17:06","date_gmt":"2011-03-08T16:17:06","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=156"},"modified":"2011-03-08T08:19:58","modified_gmt":"2011-03-08T16:19:58","slug":"learning-and-applying-coding-style-from-google-in-emacs","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/03\/learning-and-applying-coding-style-from-google-in-emacs\/","title":{"rendered":"\u5b66\u4e60\u548c\u5e94\u7528Google C++ Coding Style Guide (in Emacs)"},"content":{"rendered":"<p>Learning and applying coding style from Google (in Emacs)<\/p>\n<p>\u4eca\u5929\u674e\u5f00\u590d\u7684\u535a\u5ba2\u8f6c\u53d1\u4e86\u4e00\u6761\u56f4\u8116\uff0c\u5927\u610f\u662f\uff1aGoogle C++ Coding Style\u662f\u6700\u597d\u7684Coding Style\uff0c\u6ca1\u6709\u4e4b\u4e00\u3002<br \/>\n\u4ee5\u524d\u867d\u7136\u7ecfPaul\u63d0\u793a\u8fc7\uff0c\u4f46\u5f53\u65f6\u7684\u6211\u5e76\u6ca1\u6709\u63d0\u8d77\u8db3\u591f\u7684\u91cd\u89c6\uff0c\u73b0\u5728\u91cd\u65b0\u770b\u4e86\u4e00\u4e0b\uff0c\u53d7\u76ca\u532a\u6d45\u3002<br \/>\n<a href=\"http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/cppguide.xml\">http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/cppguide.xml Google C++ Style Guide<br \/>\n<\/a><\/p>\n<p>\u8fd9\u4e2aStyle Guide\u4e0d\u4ec5\u63d0\u793a\u5e76\u89c4\u5b9a\u4e86\u5982\u4f55\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u8fd8\u5bf9C++\u4e2d\u7684\u7279\u6027\u505a\u4e86\u5bf9\u6bd4\u548c\u4f7f\u7528\u5efa\u8bae\u3002\u4f8b\u5982\uff1a<br \/>\n(1) \u5bf9\u4e8e\u5f15\u7528(reference)\u7684\u53c2\u6570\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f7f\u7528const\u6765\u505a\u4fee\u9970\uff0c\u5426\u5219\u6211\u4eec\u4e0d\u77e5\u9053\u7a0b\u5e8f\u4e2d\u4fee\u6539\u7684\u503c\u4f1a\u4e0d\u4f1a\u6709\u8d85\u51fa\u8be5\u51fd\u6570\u7684\u4f5c\u7528\uff1b<br \/>\n(2) \u5bf9\u4e8e\u8981\u4fee\u6539\u503c\u7684\u53c2\u6570\uff0c\u5219\u63d0\u5021\u4f7f\u7528\u6307\u9488\u3002<br \/>\n(3) \u8fd8\u6709\uff0c\u5bf9\u4e8e\u6784\u9020\u51fd\u6570Constructor\uff0cGoogle \u8ba4\u4e3a\u5e94\u4f7f\u7528explicit\u5173\u952e\u5b57\u6765\u4fee\u9970\uff0c\u8fd9\u6837\u9632\u6b62\u7f16\u8bd1\u5668\u505a\u9690\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u540c\u65f6\u6784\u9020\u51fd\u6570\u5fc5\u987b\u660e\u786e\u7684\u5199\u51fa\u6765\uff0c\u5373\u4f7f\u662f\u4e0edefault\u7684\u5f62\u5f0f\u662f\u76f8\u540c\u7684\u3002<br \/>\n(4) \u5bf9\u4e8e\u5934\u6587\u4ef6\u7684\u5f15\u7528\uff0cGoogle \u5efa\u8bae\u4f7f\u7528Forward declaration\uff08\u63d0\u524d\u58f0\u660e\uff09\u6765\u51cf\u5c11\u5305\u542b\u8fc7\u591a\u7684\u6587\u4ef6\u6b21\u6570<\/p>\n<p>\u5728\u8fd9\u4e2aStyle Guide\u4e4b\u4e2d\uff0c\u8fd8\u63d0\u5230\u4e86\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177cpplint.py, \u8fd9\u4e2aPython\u5de5\u5177\u53ef\u4ee5\u5bf9\u7167Google Style Guide\u53bb\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\u662f\u5426\u7b26\u5408\u89c4\u8303\u3002\u6211\u662f\u4e00\u4e2aEmacs fans\uff0c\u90a3\u4e48\u5982\u4f55\u628aGoogle Style Guide \u548c \u6211\u4eec\u7684.emacs\u914d\u7f6e\u7ed3\u5408\u8d77\u6765\u5462\uff1f<br \/>\n\u9996\u5148\u4f60\u9700\u8981\u4e0b\u8f7d\u8fd9\u4e2a\u6587\u4ef6\uff1a <a href=\"http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/google-c-style.el\">google-c-style.el<\/a>\u3002<br \/>\n\u4e4b\u540e\u8003\u8651\u5230\u6211\u6240\u5728\u7684\u5c0f\u7ec4\u8981\u6c42Tab-indent\u662f4\u4e2a\u7a7a\u683c\uff0c\u5e76\u4e14\u8981\u6c42\u7528\u7a7a\u683c\u66ff\u4ee3Tab\uff0c\u90a3\u4e48\u6211\u4eec\u7684.emacs\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(require 'cc-mode)\r\n(require 'google-c-style)\r\n(defun my-build-tab-stop-list (width)\r\n  (let ((num-tab-stops (\/ 80 width))\r\n\t(counter 1)\r\n\t(ls nil))\r\n    (while (&lt;= counter num-tab-stops)\r\n      (setq ls (cons (* width counter) ls))\r\n      (setq counter (1+ counter)))\r\n    (set (make-local-variable 'tab-stop-list) (nreverse ls))))\r\n(defun my-c-mode-common-hook ()\r\n  (c-set-style &quot;google&quot;)\r\n  (setq tab-width 4) ;; change this to taste, this is what K&amp;R uses :)\r\n  (my-build-tab-stop-list tab-width)\r\n  (setq c-basic-offset tab-width)\r\n  (setq indent-tabs-mode nil) ;; force only spaces for indentation\r\n  (local-set-key &quot;\\C-o&quot; 'ff-get-other-file)\r\n  (c-set-offset 'substatement-open 0)\r\n  (c-set-offset 'arglist-intro c-lineup-arglist-intro-after-paren)\r\n  )\r\n;; google sytle is defined in above function\r\n(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)\r\n(add-hook 'c++-mode-common-hook 'my-c-mode-common-hook)\r\n<\/pre>\n<p>\u5982\u4f55\u914d\u7f6e.emacs\u91cc\u5e76\u52a0\u5165cpplint\u529f\u80fd\u5462\uff1f\u53ea\u9700\u5728.emacs\u91cc\u52a0\u5165\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(defun cpplint ()\r\n  &quot;check source code format according to Google Style Guide&quot;\r\n  (interactive)\r\n  (compilation-start (concat &quot;python ~\/bin\/cpplint.py &quot; (buffer-file-name))))\r\n<\/pre>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u6267\u884cM-x cpplint\u5c31\u53ef\u4ee5\u5f97\u5230\u683c\u5f0f\u68c0\u67e5\u7684\u7ed3\u679c\u3002\u7528C-x `\uff0c\u6216\u8005M-g n, M-g p\u53ef\u4ee5\u4e0a\u4e0b\u5207\u6362\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u529f\u80fd\u662f\u5982\u4f55\u5c06\u591a\u4e2a\u6e90\u6587\u4ef6\u901a\u8fc7Emacs\u6765\u8c03\u6574\u683c\u5f0f\u7684(batch indent source code)\uff1f<br \/>\n\u53c2\u8003 <a href=\"http:\/\/www.cslab.pepperdine.edu\/warford\/BatchIndentationEmacs.html\">Batch Indentation with Emacs<\/a>\uff0c\u6587\u4e2d\u63d0\u5230\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u9ed8\u8ba4\u7684\u4ee3\u7801\u98ce\u683c\u6765\u683c\u5f0f\u5316\u6e90\u6587\u4ef6\u3002<br \/>\n\u4f46\u6211\u4eec\u7684\u76ee\u6807\u662f\u7528Google\u98ce\u683c\u6765\u683c\u5f0f\u8bdd\u4ee3\u7801\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u7a0d\u4f5c\u6539\u53d8\uff1a<br \/>\n~\/bin\/emacs-format-function<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n;;; File: emacs-format-file\r\n;;; Stan Warford\r\n;;; 17 May 2006\r\n;; from: http:\/\/www.cslab.pepperdine.edu\/warford\/BatchIndentationEmacs.html\r\n\r\n;; Adopt by Xiaowei Zhan 2011 from .emacs\r\n(require 'google-c-style)\r\n(defun emacs-format-function ()\r\n  &quot;Format the whole buffer.&quot;\r\n  (setq tab-width 4) ;; change this to taste, this is what K&amp;R uses :)\r\n  (setq c-basic-offset tab-width)\r\n  (c-set-offset 'substatement-open 0)\r\n  ;; next line is strange, I copied it from .emacs, but it cannot find c-lineup-arglist-intro-after-paren\r\n  ;; however, disable this line seems working as well.\r\n  ;;(c-set-offset 'arglist-intro c-lineup-arglist-intro-after-paren) \r\n  (indent-region (point-min) (point-max) nil)\r\n  (untabify (point-min) (point-max))\r\n  (save-buffer)\r\n  )\r\n<\/pre>\n<p>~\/bin\/my-indent<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n# File: my-indent\r\n# Opens a set of files in emacs and executes the emacs-format-function.\r\n# Assumes the function named emacs-format-function is defined in the\r\n# file named emacs-format-file.\r\n\r\nif &#x5B; $# == 0 ]\r\nthen\r\n   echo &quot;my-indent requires at least one argument.&quot; 1&gt;&amp;2\r\n   echo &quot;Usage: my-indent files-to-indent&quot; 1&gt;&amp;2\r\n   exit 1\r\nfi\r\nwhile &#x5B; $# -ge 1 ]\r\ndo\r\n   if &#x5B; -d $1 ]\r\n   then\r\n      echo &quot;Argument of my-indent $1 cannot be a directory.&quot; 1&gt;&amp;2\r\n      exit 1\r\n   fi\r\n   # Check for existence of file:\r\n   ls $1 2&gt; \/dev\/null | grep $1 &gt; \/dev\/null\r\n   if &#x5B; $? != 0 ]\r\n   then\r\n      echo &quot;my-indent: $1 not found.&quot; 1&gt;&amp;2\r\n      exit 1\r\n   fi\r\n   echo &quot;Indenting $1 with emacs in batch mode&quot;\r\n   emacs -batch $1 -l ~\/emacs\/google-c-style.el -l ~\/bin\/emacs-format-file -f emacs-format-function\r\n   echo\r\n   shift 1\r\ndone\r\nexit 0\r\n\r\n#from http:\/\/www.cslab.pepperdine.edu\/warford\/BatchIndentationEmacs.html\r\n<\/pre>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662fGoogle\u9664\u4e86C++ Style Guide, \u8fd8\u63d0\u4f9bJavaScript Style Guide, Objective-C Style Guide, and Python Style Guide\u3002\u5bf9\u8fd9\u4e9b\u8bed\u8a00\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u5230<a href=\"http:\/\/code.google.com\/p\/google-styleguide\/\">Google Style Guide<\/a>\u53d1\u6398\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning and applying coding style from Google (in Emacs) \u4eca\u5929\u674e\u5f00\u590d\u7684\u535a\u5ba2\u8f6c\u53d1\u4e86\u4e00\u6761\u56f4\u8116\uff0c\u5927\u610f\u662f\uff1aGoogle C++ Coding Style\u662f\u6700\u597d\u7684Coding Style\uff0c\u6ca1\u6709\u4e4b\u4e00\u3002 \u4ee5\u524d\u867d\u7136\u7ecfPaul\u63d0\u793a\u8fc7\uff0c\u4f46\u5f53\u65f6\u7684\u6211\u5e76\u6ca1\u6709\u63d0\u8d77\u8db3\u591f\u7684\u91cd\u89c6\uff0c\u73b0\u5728\u91cd\u65b0\u770b\u4e86\u4e00\u4e0b\uff0c\u53d7\u76ca\u532a\u6d45\u3002 http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/cppguide.xml Google C++ Style Guide \u8fd9\u4e2aStyle Guide\u4e0d\u4ec5\u63d0\u793a\u5e76\u89c4\u5b9a\u4e86\u5982\u4f55\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u8fd8\u5bf9C++\u4e2d\u7684\u7279\u6027\u505a\u4e86\u5bf9\u6bd4\u548c\u4f7f\u7528\u5efa\u8bae\u3002\u4f8b\u5982\uff1a (1) \u5bf9\u4e8e\u5f15\u7528(reference)\u7684\u53c2\u6570\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f7f\u7528const\u6765\u505a\u4fee\u9970\uff0c\u5426\u5219\u6211\u4eec\u4e0d\u77e5\u9053\u7a0b\u5e8f\u4e2d\u4fee\u6539\u7684\u503c\u4f1a\u4e0d\u4f1a\u6709\u8d85\u51fa\u8be5\u51fd\u6570\u7684\u4f5c\u7528\uff1b (2) \u5bf9\u4e8e\u8981\u4fee\u6539\u503c\u7684\u53c2\u6570\uff0c\u5219\u63d0\u5021\u4f7f\u7528\u6307\u9488\u3002 (3) \u8fd8\u6709\uff0c\u5bf9\u4e8e\u6784\u9020\u51fd\u6570Constructor\uff0cGoogle \u8ba4\u4e3a\u5e94\u4f7f\u7528explicit\u5173\u952e\u5b57\u6765\u4fee\u9970\uff0c\u8fd9\u6837\u9632\u6b62\u7f16\u8bd1\u5668\u505a\u9690\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u540c\u65f6\u6784\u9020\u51fd\u6570\u5fc5\u987b\u660e\u786e\u7684\u5199\u51fa\u6765\uff0c\u5373\u4f7f\u662f\u4e0edefault\u7684\u5f62\u5f0f\u662f\u76f8\u540c\u7684\u3002 (4) \u5bf9\u4e8e\u5934\u6587\u4ef6\u7684\u5f15\u7528\uff0cGoogle \u5efa\u8bae\u4f7f\u7528Forward declaration\uff08\u63d0\u524d\u58f0\u660e\uff09\u6765\u51cf\u5c11\u5305\u542b\u8fc7\u591a\u7684\u6587\u4ef6\u6b21\u6570 \u5728\u8fd9\u4e2aStyle Guide\u4e4b\u4e2d\uff0c\u8fd8\u63d0\u5230\u4e86\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177cpplint.py, \u8fd9\u4e2aPython\u5de5\u5177\u53ef\u4ee5\u5bf9\u7167Google Style Guide\u53bb\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\u662f\u5426\u7b26\u5408\u89c4\u8303\u3002\u6211\u662f\u4e00\u4e2aEmacs fans\uff0c\u90a3\u4e48\u5982\u4f55\u628aGoogle Style Guide \u548c \u6211\u4eec\u7684.emacs\u914d\u7f6e\u7ed3\u5408\u8d77\u6765\u5462\uff1f \u9996\u5148\u4f60\u9700\u8981\u4e0b\u8f7d\u8fd9\u4e2a\u6587\u4ef6\uff1a google-c-style.el\u3002 \u4e4b\u540e\u8003\u8651\u5230\u6211\u6240\u5728\u7684\u5c0f\u7ec4\u8981\u6c42Tab-indent\u662f4\u4e2a\u7a7a\u683c\uff0c\u5e76\u4e14\u8981\u6c42\u7528\u7a7a\u683c\u66ff\u4ee3Tab\uff0c\u90a3\u4e48\u6211\u4eec\u7684.emacs\u914d\u7f6e\u5982\u4e0b\uff1a (require &#8216;cc-mode) (require &#8216;google-c-style) (defun my-build-tab-stop-list (width) (let ((num-tab-stops [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[33,31,142,32],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-code","tag-emacs","tag-google","tag-code","tag-style"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}