]> code.delx.au - gnu-emacs-elpa/blobdiff - company-cmake.el
Merge pull request #461 from rlph/patch-1
[gnu-emacs-elpa] / company-cmake.el
index d6db7e44c323e95300f8dc01a1cc2847ec3eb4c7..010df32e43d25f4193bd66e12a6b11473a29895e 100644 (file)
@@ -1,6 +1,6 @@
-;;; company-cmake.el --- company-mode completion back-end for CMake
+;;; company-cmake.el --- company-mode completion backend for CMake
 
-;; Copyright (C) 2013  Free Software Foundation, Inc.
+;; Copyright (C) 2013-2014  Free Software Foundation, Inc.
 
 ;; Author: Chen Bin <chenbin DOT sh AT gmail>
 ;; Version: 0.2
@@ -29,7 +29,7 @@
 (require 'cl-lib)
 
 (defgroup company-cmake nil
-  "Completion back-end for CMake."
+  "Completion backend for CMake."
   :group 'company)
 
 (defcustom company-cmake-executable
@@ -51,63 +51,65 @@ They affect which types of symbols we get completion candidates for.")
 (defvar company-cmake-modes '(cmake-mode)
   "Major modes in which cmake may complete.")
 
-(defvar company-cmake-candidates-cache nil
+(defvar company-cmake--candidates-cache nil
   "Cache for the raw candidates.")
 
 (defvar company-cmake--meta-command-cache nil
   "Cache for command arguments to retrieve descriptions for the candidates.")
 
 (defun company-cmake--replace-tags (rlt)
-  (setq rlt (replace-regexp-in-string "\\(.*\\)<LANG>\\(.*\\)"
-                                      (mapconcat 'identity '("\\1CXX\\2" "\\1C\\2" "\\1Fortran\\2") "\n")
-                                      rlt))
-  (setq rlt (replace-regexp-in-string "\\(.*\\)<CONFIG>\\(.*\\)"
-                                      (mapconcat 'identity '("\\1DEBUG\\2" "\\1RELEASE\\2" "\\1RELWITHDEBINFO\\2" "\\1MINSIZEREL\\2") "\n")
-                                      rlt))
+  (setq rlt (replace-regexp-in-string
+             "\\(.*?\\(IS_GNU\\)?\\)<LANG>\\(.*\\)"
+             (lambda (_match)
+               (mapconcat 'identity
+                          (if (match-beginning 2)
+                              '("\\1CXX\\3" "\\1C\\3" "\\1G77\\3")
+                            '("\\1CXX\\3" "\\1C\\3" "\\1Fortran\\3"))
+                          "\n"))
+             rlt t))
+  (setq rlt (replace-regexp-in-string
+             "\\(.*\\)<CONFIG>\\(.*\\)"
+             (mapconcat 'identity '("\\1DEBUG\\2" "\\1RELEASE\\2"
+                                    "\\1RELWITHDEBINFO\\2" "\\1MINSIZEREL\\2")
+                        "\n")
+             rlt))
   rlt)
 
 (defun company-cmake--fill-candidates-cache (arg)
   "Fill candidates cache if needed."
   (let (rlt)
-    (unless company-cmake-candidates-cache
-      (setq company-cmake-candidates-cache (make-hash-table :test 'equal)))
+    (unless company-cmake--candidates-cache
+      (setq company-cmake--candidates-cache (make-hash-table :test 'equal)))
 
     ;; If hash is empty, fill it.
-    (unless (gethash arg company-cmake-candidates-cache)
+    (unless (gethash arg company-cmake--candidates-cache)
       (with-temp-buffer
-        (setq res (call-process company-cmake-executable nil t nil arg))
-        (unless (eq 0 res)
-          (message "cmake executable exited with error=%d" res))
+        (let ((res (call-process company-cmake-executable nil t nil arg)))
+          (unless (zerop res)
+            (message "cmake executable exited with error=%d" res)))
         (setq rlt (buffer-string)))
       (setq rlt (company-cmake--replace-tags rlt))
-      (puthash arg rlt company-cmake-candidates-cache))
+      (puthash arg rlt company-cmake--candidates-cache))
     ))
 
-(defun company-cmake-find-match (pattern line)
-  (let (match)
-     ;; General Flags
-     (if (string-match pattern line)
-      (if (setq match (match-string 1 line))
-        (puthash match cmd company-cmake--meta-command-cache)))
-    match))
-
-(defun company-cmake-parse (prefix content cmd)
+(defun company-cmake--parse (prefix content cmd)
   (let ((start 0)
         (pattern (format company-cmake--completion-pattern
                          (regexp-quote prefix)
                          (if (zerop (length prefix)) "+" "*")))
         (lines (split-string content "\n"))
-        (lang-patterns ())
         match
         rlt)
     (dolist (line lines)
-      (if (setq match (company-cmake-find-match pattern line))
-          (push match rlt)))
+      (when (string-match pattern line)
+        (let ((match (match-string 1 line)))
+          (when match
+            (puthash match cmd company-cmake--meta-command-cache)
+            (push match rlt)))))
     rlt))
 
 (defun company-cmake--candidates (prefix)
-  (let ((res 0)
-        results
+  (let (results
         cmd-opts
         str)
 
@@ -118,19 +120,19 @@ They affect which types of symbols we get completion candidates for.")
       (company-cmake--fill-candidates-cache arg)
       (setq cmd-opts (replace-regexp-in-string "-list$" "" arg) )
 
-      (setq str (gethash arg company-cmake-candidates-cache))
-      (if str
-        (setq results (nconc results (company-cmake-parse prefix str cmd-opts)))
-        ))
+      (setq str (gethash arg company-cmake--candidates-cache))
+      (when str
+        (setq results (nconc results
+                             (company-cmake--parse prefix str cmd-opts)))))
     results))
 
 (defun company-cmake--unexpand-candidate (candidate)
   (cond
    ((string-match "^CMAKE_\\(C\\|CXX\\|Fortran\\)\\(_.*\\)$" candidate)
-    (setq candidate (concat "CMAKE_<LANG>_" (match-string 2 candidate))))
+    (setq candidate (concat "CMAKE_<LANG>" (match-string 2 candidate))))
 
    ;; C flags
-   ((string-match "^\\(.*_\\)IS_GNU\\(C\\|CXX\\|Fortran\\)$" candidate)
+   ((string-match "^\\(.*_\\)IS_GNU\\(C\\|CXX\\|G77\\)$" candidate)
     (setq candidate (concat (match-string 1 candidate) "IS_GNU<LANG>")))
 
    ;; C flags
@@ -176,7 +178,7 @@ They affect which types of symbols we get completion candidates for.")
                                        (point-max))))))
 
 (defun company-cmake (command &optional arg &rest ignored)
-  "`company-mode' completion back-end for CMake.
+  "`company-mode' completion backend for CMake.
 CMake is a cross-platform, open-source make system."
   (interactive (list 'interactive))
   (cl-case command