]> code.delx.au - gnu-emacs-elpa/blob - company-cmake.el
Bundle company-cmake
[gnu-emacs-elpa] / company-cmake.el
1 ;;; company-cmake.el --- company-mode completion back-end for CMake
2
3 ;; Copyright (C) 2013 Chen Bin
4
5 ;; Author: Chen Bin <chenbin DOT sh AT gmail>
6 ;; Version: 0.1
7
8 ;; This file is NOT part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24 ;;
25 ;; company-cmake offers completions for module names, variable names and
26 ;; commands used by CMake. And their descriptions.
27
28 ;;; Code:
29 (require 'company)
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 (candidates)
100 (let ((cmd-opts (gethash prefix company-cmake--meta-command-cache))
101 result)
102 (with-temp-buffer
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))
107 (forward-line 2)
108 (setq result (buffer-substring-no-properties (line-beginning-position)
109 (point-max)))
110 result)))
111
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))
116 (case command
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))
127 ))
128
129 (provide 'company-cmake)
130 ;;; company-cmake.el ends here