]> code.delx.au - gnu-emacs-elpa/blob - company-cmake.el
Use cl-lib
[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.1
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--meta-command-cache nil
55 "Cache for command arguments to retrieve descriptions for the candidates.")
56
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
61 (regexp-quote prefix)
62 (if (zerop (length prefix)) "+" "*")))
63 (case-fold-search nil)
64 lines match)
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)
68 (push match lines))
69 lines))
70
71 (defun company-cmake--candidates (prefix)
72 (let ((res 0)
73 results
74 cmd)
75 (setq company-cmake--meta-command-cache (make-hash-table :test 'equal))
76 (dolist (arg company-cmake-executable-arguments)
77 (with-temp-buffer
78 (setq res (call-process company-cmake-executable nil t nil arg))
79 (unless (eq 0 res)
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)))))
83 results))
84
85 (defun company-cmake--meta (prefix)
86 (let ((cmd-opts (gethash prefix company-cmake--meta-command-cache))
87 result)
88 (with-temp-buffer
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))
93 (forward-line 2)
94 (setq result (buffer-substring-no-properties (line-beginning-position)
95 (line-end-position)))
96 (setq result (replace-regexp-in-string "^[ \t\n\r]+" "" result))
97 result)))
98
99 (defun company-cmake--doc-buffer (prefix)
100 (let ((cmd-opts (gethash prefix company-cmake--meta-command-cache)))
101 (with-temp-buffer
102 (call-process company-cmake-executable nil t nil cmd-opts prefix)
103 ;; Go to the third line, trim it and return the doc buffer.
104 ;; Tested with cmake 2.8.9.
105 (goto-char (point-min))
106 (forward-line 2)
107 (company-doc-buffer
108 (buffer-substring-no-properties (line-beginning-position)
109 (point-max))))))
110
111 (defun company-cmake (command &optional arg &rest ignored)
112 "`company-mode' completion back-end for CMake.
113 CMake is a cross-platform, open-source make system."
114 (interactive (list 'interactive))
115 (cl-case command
116 (interactive (company-begin-backend 'company-cmake))
117 (init (when (memq major-mode company-cmake-modes)
118 (unless company-cmake-executable
119 (error "Company found no cmake executable"))))
120 (prefix (and (memq major-mode company-cmake-modes)
121 (not (company-in-string-or-comment))
122 (company-grab-symbol)))
123 (candidates (company-cmake--candidates arg))
124 (meta (company-cmake--meta arg))
125 (doc-buffer (company-cmake--doc-buffer arg))
126 ))
127
128 (provide 'company-cmake)
129 ;;; company-cmake.el ends here