pcl-c-style.el,在Emacs编辑器编写PCL自动格式化编码

pcl-c-style.el,在Emacs编辑器编写PCL自动格式化编码:

pcl-c-style.el里面的内容如下:
;;; pcl-c-style.el — Pcl’s C/C++ style for c-mode
;;
;; Keywords: c, tools
;;
;;; pcl-c-style.el
;;
;;; Software License Agreement (BSD License)
;;
;; Point Cloud Library (PCL) - www.pointclouds.org
;; Copyright © 2012, Willow Garage, Inc.
;;
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; * Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; * Redistributions in binary form must reproduce the above
;; copyright notice, this list of conditions and the following
;; disclaimer in the documentation and/or other materials provided
;; with the distribution.
;; * Neither the name of Willow Garage, Inc. nor the names of its
;; contributors may be used to endorse or promote products derived
;; from this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
;; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
;; COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
;; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
;; ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.
;;
;;; Commentary:
;;
;; Provides the pcl C/C++ coding style. You may wish to add
;; pcl-set-c-style' to yourc-mode-common-hook’ after requiring this
;; file. For example:
;;
;; (add-hook 'c-mode-common-hook 'pcl-set-c-style)
;;

;; Consider files with “.h” extension as c++
(setq auto-mode-alist (cons ‘("\.h\’" . c+±mode) auto-mode-alist))

;; Require cc-mode
(require 'cc-mode)

;; Define c++ access keys
(setq c-C+±access-key “\<\(slots\|signals\|private\|protected\|public\)\>[ \t][(slots\|signals)][ \t]*:”)

;; An inline function that return true if we are inside a class
(defsubst pcl-at-inclass-topmost-intro (sintax)
(eq (caar sintax) 'inclass))

;; Indent after access keys if any
;; This code was adapted from here
;; http://lists.gnu.org/archive/html/help-gnu-emacs/2009-09/msg00593.html
(defun pcl-indent-after-access-label-maybe (langelem)
"Give an extra level of indentation to class members under an access
specifier, e.g.:

public
A(); <========== extra level of indentation.

This should really be properly implemented in CC Mode, but it’s not."
(and
(let (m-type)
(when
(save-excursion
(back-to-indentation)
;; Go back one “statement” at a time till we reach a label or
;; something which isn’t an inclass-topmost-intro
(while
(and (eq (setq m-type (c-beginning-of-statement-1)) 'previous)
(pcl-at-inclass-topmost-intro (c-guess-basic-syntax))))
;; Have we found “private:”, “public”: or “protected”?
(and (eq m-type 'label)
(looking-at c-C+±access-key)))
(save-excursion
(back-to-indentation)
c-basic-offset)))))

(defconst pcl-c-style
`(“gnu”
(c-recognize-knr-p . nil)
(c-enable-xemacs-performance-kludge-p . t) ; speed up indentation in XEmacs
(c-basic-offset . 2)
;; (c-tab-always-indent . t)
(c-comment-only-line-offset . 0)
(c-hanging-braces-alist . ((defun-open before)
(defun-open after)
(namespace-open before)
(namespace-open after)))
(c-hanging-colons-alist . ((member-init-intro before)
(inher-intro)))
(c-cleanup-list . (scope-operator
empty-defun-braces
defun-close-semi
space-before-funcall))
(c-offsets-alist
(inline-open . +)
(inclass . +)
(access-label . 0)
(topmost-intro . pcl-indent-after-access-label-maybe)
(topmost-intro-cont 0)
(statement-cont . 0)
(substatement-open . 0)
(substatement-label . 0)
(statement-case-open . 0)
(case-label . +)
))
“PCL C/C++ Programming Style”)

(defun pcl-set-c-style ()
“Set the current buffer’s c-style to PCL C/C++ Programming
Style. Meant to be added to `c-mode-common-hook’.”
(interactive)
(setq tab-width 2
;; this will make sure spaces are used instead of tabs
indent-tabs-mode nil)
(c-add-style “PCL” pcl-c-style t)
(c-toggle-auto-newline 1)
(c-toggle-hungry-state 1))

;; Switch from *. to . and vice versa
(defun switch-cpp-or-hpp-to-h ()
(interactive)
(when (string-match "\(.*\)/\(.*\)\.\([.]
\)KaTeX parse error: Got function '\newline' with no arguments as superscript at position 1: \̲n̲e̲w̲l̲i̲n̲e̲" path)
(let ((pcl-root (match-string 1 path))
(subsys (match-string 2 path)))
(if (file-exists-p (concat pcl-root “/” subsys “/include/pcl/” subsys “/” name “.h”))
(find-file (concat pcl-root “/” subsys “/include/pcl/” subsys “/” name “.h”)))))
(cond ((file-exists-p (concat name “.hpp”))
(find-file (concat name “.hpp”))
)))
((string-match suffix “h”)
(message “in h”)
(cond ((file-exists-p (concat path “/impl/” name “.hpp”))
(find-file (concat path “/impl/” name “.hpp”))
)
((file-exists-p (concat name “.cpp”))
(find-file (concat name “.cpp”))
)))
((string-match suffix “hpp”)
(message “in hpp”)
(cond ((file-exists-p (concat path “/…/” name “.h”))
(find-file (concat path “/…/” name “.h”))
)
))
))))

(provide 'pcl-c-style)
;;; pcl-c-style.el

可以自己建一个文件pcl-c-style.el,将上面的内容输进去,也可以从本文下载。

最后在C/C++hook下添加下面代码

(load-file “/location/to/pcl-c-style.el”)
(add-hook 'c-mode-common-hook 'pcl-set-c-style)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值