Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.5 KiB

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