]> code.delx.au - gnu-emacs-elpa/blob - company-cmake.el
Clean up a little
[gnu-emacs-elpa] / company-cmake.el
1 ;;; company-cmake.el --- company-mode completion back-end for CMake
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Chen Bin <chenbin DOT sh AT gmail>
6 ;; Version: 0.2
7
8 ;; This program is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22 ;;
23 ;; company-cmake offers completions for module names, variable names and
24 ;; commands used by CMake. And their descriptions.
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'cl-lib)
30
31 (defgroup company-cmake nil
32 "Completion back-end for CMake."
33 :group 'company)
34
35 (defcustom company-cmake-executable
36 (executable-find "cmake")
37 "Location of cmake executable."
38 :type 'file)
39
40 (defvar company-cmake-executable-arguments
41 '("--help-command-list"
42 "--help-module-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.")
46
47 (defvar company-cmake--completion-pattern
48 "^\\(%s[a-zA-Z0-9_<>]%s\\)$"
49 "Regexp to match the candidates.")
50
51 (defvar company-cmake-modes '(cmake-mode)
52 "Major modes in which cmake may complete.")
53
54 (defvar company-cmake--candidates-cache nil
55 "Cache for the raw candidates.")
56
57 (defvar company-cmake--meta-command-cache nil
58 "Cache for command arguments to retrieve descriptions for the candidates.")
59
60 (defun company-cmake--replace-tags (rlt)
61 (setq rlt (replace-regexp-in-string
62 "\\(.*\\)<LANG>\\(.*\\)"
63 (mapconcat 'identity '("\\1CXX\\2" "\\1C\\2" "\\1Fortran\\2") "\n")
64 rlt))
65 (setq rlt (replace-regexp-in-string
66 "\\(.*\\)<CONFIG>\\(.*\\)"
67 (mapconcat 'identity '("\\1DEBUG\\2" "\\1RELEASE\\2"
68 "\\1RELWITHDEBINFO\\2" "\\1MINSIZEREL\\2")
69 "\n")
70 rlt))
71 rlt)
72
73 (defun company-cmake--fill-candidates-cache (arg)
74 "Fill candidates cache if needed."
75 (let (rlt)
76 (unless company-cmake--candidates-cache
77 (setq company-cmake--candidates-cache (make-hash-table :test 'equal)))
78
79 ;; If hash is empty, fill it.
80 (unless (gethash arg company-cmake--candidates-cache)
81 (with-temp-buffer
82 (setq res (call-process company-cmake-executable nil t nil arg))
83 (unless (eq 0 res)
84 (message "cmake executable exited with error=%d" res))
85 (setq rlt (buffer-string)))
86 (setq rlt (company-cmake--replace-tags rlt))
87 (puthash arg rlt company-cmake--candidates-cache))
88 ))
89
90 (defun company-cmake-find-match (pattern line)
91 (let (match)
92 ;; General Flags
93 (if (string-match pattern line)
94 (if (setq match (match-string 1 line))
95 (puthash match cmd company-cmake--meta-command-cache)))
96 match))
97
98 (defun company-cmake--parse (prefix content cmd)
99 (let ((start 0)
100 (pattern (format company-cmake--completion-pattern
101 (regexp-quote prefix)
102 (if (zerop (length prefix)) "+" "*")))
103 (lines (split-string content "\n"))
104 (lang-patterns ())
105 match
106 rlt)
107 (dolist (line lines)
108 (if (setq match (company-cmake-find-match pattern line))
109 (push match rlt)))
110 rlt))
111
112 (defun company-cmake--candidates (prefix)
113 (let ((res 0)
114 results
115 cmd-opts
116 str)
117
118 (unless company-cmake--meta-command-cache
119 (setq company-cmake--meta-command-cache (make-hash-table :test 'equal)))
120
121 (dolist (arg company-cmake-executable-arguments)
122 (company-cmake--fill-candidates-cache arg)
123 (setq cmd-opts (replace-regexp-in-string "-list$" "" arg) )
124
125 (setq str (gethash arg company-cmake--candidates-cache))
126 (when str
127 (setq results (nconc results
128 (company-cmake--parse prefix str cmd-opts)))))
129 results))
130
131 (defun company-cmake--unexpand-candidate (candidate)
132 (cond
133 ((string-match "^CMAKE_\\(C\\|CXX\\|Fortran\\)\\(_.*\\)$" candidate)
134 (setq candidate (concat "CMAKE_<LANG>_" (match-string 2 candidate))))
135
136 ;; C flags
137 ((string-match "^\\(.*_\\)IS_GNU\\(C\\|CXX\\|Fortran\\)$" candidate)
138 (setq candidate (concat (match-string 1 candidate) "IS_GNU<LANG>")))
139
140 ;; C flags
141 ((string-match "^\\(.*_\\)OVERRIDE_\\(C\\|CXX\\|Fortran\\)$" candidate)
142 (setq candidate (concat (match-string 1 candidate) "OVERRIDE_<LANG>")))
143
144 ((string-match "^\\(.*\\)\\(_DEBUG\\|_RELEASE\\|_RELWITHDEBINFO\\|_MINSIZEREL\\)\\(.*\\)$" candidate)
145 (setq candidate (concat (match-string 1 candidate)
146 "_<CONFIG>"
147 (match-string 3 candidate)))))
148 candidate)
149
150 (defun company-cmake--meta (candidate)
151 (let ((cmd-opts (gethash candidate company-cmake--meta-command-cache))
152 result)
153 (setq candidate (company-cmake--unexpand-candidate candidate))
154
155 ;; Don't cache the documentation of every candidate (command)
156 ;; Cache in this case will cost too much memory.
157 (with-temp-buffer
158 (call-process company-cmake-executable nil t nil cmd-opts candidate)
159 ;; Go to the third line, trim it and return the result.
160 ;; Tested with cmake 2.8.9.
161 (goto-char (point-min))
162 (forward-line 2)
163 (setq result (buffer-substring-no-properties (line-beginning-position)
164 (line-end-position)))
165 (setq result (replace-regexp-in-string "^[ \t\n\r]+" "" result))
166 result)))
167
168 (defun company-cmake--doc-buffer (candidate)
169 (let ((cmd-opts (gethash candidate company-cmake--meta-command-cache)))
170
171 (setq candidate (company-cmake--unexpand-candidate candidate))
172 (with-temp-buffer
173 (call-process company-cmake-executable nil t nil cmd-opts candidate)
174 ;; Go to the third line, trim it and return the doc buffer.
175 ;; Tested with cmake 2.8.9.
176 (goto-char (point-min))
177 (forward-line 2)
178 (company-doc-buffer
179 (buffer-substring-no-properties (line-beginning-position)
180 (point-max))))))
181
182 (defun company-cmake (command &optional arg &rest ignored)
183 "`company-mode' completion back-end for CMake.
184 CMake is a cross-platform, open-source make system."
185 (interactive (list 'interactive))
186 (cl-case command
187 (interactive (company-begin-backend 'company-cmake))
188 (init (when (memq major-mode company-cmake-modes)
189 (unless company-cmake-executable
190 (error "Company found no cmake executable"))))
191 (prefix (and (memq major-mode company-cmake-modes)
192 (not (company-in-string-or-comment))
193 (company-grab-symbol)))
194 (candidates (company-cmake--candidates arg))
195 (meta (company-cmake--meta arg))
196 (doc-buffer (company-cmake--doc-buffer arg))
197 ))
198
199 (provide 'company-cmake)
200 ;;; company-cmake.el ends here