1 ;;; company-cmake.el --- company-mode completion back-end for CMake
3 ;; Copyright (C) 2013 Chen Bin
5 ;; Author: Chen Bin <chenbin DOT sh AT gmail>
8 ;; This file is NOT part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; company-cmake offers completions for module names, variable names and
26 ;; commands used by CMake. And their descriptions.
31 (defgroup company-cmake nil
32 "Completion back-end for CMake."
35 (defcustom company-cmake-executable
36 (executable-find "cmake")
37 "Location of cmake executable."
40 (defvar company-cmake-executable-arguments
41 '("--help-command-list"
43 "--help-variable-list")
44 "The arguments we pass to cmake, separately.
45 They affect which types of symbols we get completion candidates for.")
47 (defvar company-cmake--completion-pattern
48 "^\\(%s[a-zA-Z0-9_]%s\\)$"
49 "Regexp to match the candidates.")
51 (defvar company-cmake-modes '(cmake-mode)
52 "Major modes in which cmake may complete.")
54 (defvar company-cmake--meta-command-cache nil
55 "Cache for command arguments to retrieve descriptions for the candidates.")
57 (defun company-cmake--parse-output (prefix cmd)
58 "Analyze the temp buffer and collect lines."
59 (goto-char (point-min))
60 (let ((pattern (format company-cmake--completion-pattern
62 (if (zerop (length prefix)) "+" "*")))
63 (case-fold-search nil)
65 (while (re-search-forward pattern nil t)
66 (setq match (match-string-no-properties 1))
67 (puthash match cmd company-cmake--meta-command-cache)
71 (defun company-cmake--candidates (prefix)
75 (setq company-cmake--meta-command-cache (make-hash-table :test 'equal))
76 (dolist (arg company-cmake-executable-arguments)
78 (setq res (call-process company-cmake-executable nil t nil arg))
80 (message "cmake executable exited with error=%d" res))
81 (setq cmd (replace-regexp-in-string "-list$" "" arg) )
82 (setq results (nconc results (company-cmake--parse-output prefix cmd)))))
85 (defun company-cmake--meta (prefix)
86 (let ((cmd-opts (gethash prefix company-cmake--meta-command-cache))
89 (call-process company-cmake-executable nil t nil cmd-opts prefix)
90 ;; Go to the third line, trim it and return the result.
91 ;; Tested with cmake 2.8.9.
92 (goto-char (point-min))
94 (setq result (buffer-substring-no-properties (line-beginning-position)
96 (setq result (replace-regexp-in-string "^[ \t\n\r]+" "" result))
99 (defun company-cmake-doc-buffer (candidates)
100 (let ((cmd-opts (gethash prefix company-cmake--meta-command-cache))
103 (call-process company-cmake-executable nil t nil cmd-opts prefix)
104 ;; Go to the third line, trim it and return the result.
105 ;; Tested with cmake 2.8.9.
106 (goto-char (point-min))
108 (setq result (buffer-substring-no-properties (line-beginning-position)
112 (defun company-cmake (command &optional arg &rest ignored)
113 "`company-mode' completion back-end for CMake.
114 CMake is a cross-platform, open-source make system."
115 (interactive (list 'interactive))
117 (interactive (company-begin-backend 'company-cmake))
118 (init (when (memq major-mode company-cmake-modes)
119 (unless company-cmake-executable
120 (error "Company found no cmake executable"))))
121 (prefix (and (memq major-mode company-cmake-modes)
122 (not (company-in-string-or-comment))
123 (company-grab-symbol)))
124 (candidates (company-cmake--candidates arg))
125 (meta (company-cmake--meta arg))
126 (doc-buffer (company-cmake-doc-buffer arg))
129 (provide 'company-cmake)
130 ;;; company-cmake.el ends here