Skip to content

Commit

Permalink
重构代码,增强自动格式化c++代码的功能 (close #1)
Browse files Browse the repository at this point in the history
* 代码被移动到一个单独的local-package中
+ 根目录下找不到配置文件时自动提供一个默认的格式化策略
  • Loading branch information
Liu233w authored and Liu's DeskTop committed Nov 3, 2016
1 parent 8a100c1 commit 5040d6f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
86 changes: 86 additions & 0 deletions layers/liu233w/local/auto-clang-format/auto-clang-format.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
;;; auto-clang-format.el --- 在键入分号或右大括号的时候自动调用 clang-format 进行排版 -*- lexical-binding: t; -*-

;; Copyright (C) 2016 Liu233w

;; Author: Liu233w <wwwlsmcom@outlook.com>
;; Keywords: languages, convenience

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; 在键入分号或右大括号的时候自动调用 clang-format 进行排版

;;; Code:

(require 'clang-format)

(defvar acf-clang-format-style
"{BasedOnStyle: LLVM, IndentWidth: 4, BreakBeforeBraces: Allman,
AllowShortFunctionsOnASingleLine: false}"
"默认的style,在找不到默认的style文件时提供")

(defun acf--get-clang-format-config (source)
"source是源文件地址,为nil时表示没有地址。如果在源文件目录或其上级目录中可以找到
clang-format配置文件,返回\"file\",否则返回`acf-clang-format-style'的内容。"
(if (and source (or (locate-dominating-file source ".clang-format")
(locate-dominating-file source "_clang-format")))
"file"
acf-clang-format-style))

;;;###autoload
(defun acf-semi-clang-format ()
"format by clang-format when enter ';'"
(interactive)
(command-execute #'c-electric-semi&comma)
(clang-format-region (line-beginning-position 0)
(line-beginning-position 2)
(acf--get-clang-format-config
(buffer-file-name))))

;;;###autoload
(defun acf-brace-clang-format ()
"format by clang-format when enter '}'"
(interactive)
(command-execute #'c-electric-brace)
(let ((end-position (point))
begin-position (scan-lists (point) -1 0))
(clang-format-region begin-position
end-position
(acf--get-clang-format-config
(buffer-file-name)))))

(defvar acf-clang-format-executable
"clang-format"
"clang-format 可执行文件的名字。")

;;;###autoload
(defun acf-enable-auto-clang-format ()
"启动 auto-clang-format"
(when (executable-find acf-clang-format-executable)
;;使用clang-format作为默认排版工具
(local-set-key (kbd "C-M-\\")
#'(lambda (beg end)
(interactive "r")
(clang-format-region beg end
(acf--get-clang-format-config
(buffer-file-name)))))
;;当插入分号时自动对当前行排版
(local-set-key (kbd ";")
'acf-semi-clang-format)
(local-set-key (kbd "}")
'acf-brace-clang-format)))

(provide 'auto-clang-format)
;;; auto-clang-format.el ends here
41 changes: 9 additions & 32 deletions layers/liu233w/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
(python :location built-in)
(dubcaps-mode :location local)
(cc-mode :location built-in)
(auto-clang-format :location local)
(dired :location built-in)
(linum :location built-in)
(emacs-lisp :location built-in)
Expand Down Expand Up @@ -595,40 +596,16 @@ Each entry is either:
;; (list liu233w//company-clang-additional-clang-args-before)
;; args
;; (list liu233w//company-clang-additional-clang-args-after))))))

(with-eval-after-load 'cc-mode
(defun liu233w/semi-clang-format (args)
"format by clang-format when enter ';'"
(interactive "*P")
(c-electric-semi&comma args)
(clang-format-region (line-beginning-position 0) (line-beginning-position 2))
)

(defun liu233w/brace-clang-format (args)
"format by clang-format when enter '}'"
(interactive "*P")
(c-electric-brace args)
(let ((end-position (point))
begin-position)
(save-excursion
(evil-jump-item)
(setf begin-position (point)))
(clang-format-region begin-position end-position))
)

;;add clang-format support
(add-hook 'c++-mode-hook
(lambda ()
(when (executable-find "clang-format")
;;使用clang-format作为默认排版工具
(local-set-key (kbd "C-M-\\") 'clang-format)
;;当插入分号时自动对当前行排版
(local-set-key (kbd ";")
'liu233w/semi-clang-format)
(local-set-key (kbd "}")
'liu233w/brace-clang-format)))))
)

(defun liu233w/init-auto-clang-format ()
"在输入分号或右大括号的时候自动排版。"
(use-package auto-clang-format
:defer t
:commands acf-enable-auto-clang-format
:init
(add-hook 'c++-mode-hook #'acf-enable-auto-clang-format)))

(defun liu233w/post-init-dired ()
;;在dired中使用enter时只使用同一个缓冲区
(put 'dired-find-alternate-file 'disabled nil)
Expand Down

0 comments on commit 5040d6f

Please sign in to comment.