EDE: Rework config file detection
authorEric Ludlam <eric@siege-engine.com>
Sun, 22 Mar 2015 16:26:14 +0000 (12:26 -0400)
committerDavid Engster <deng@randomsample.de>
Sun, 22 Jan 2017 21:25:04 +0000 (22:25 +0100)
* lisp/cedet/ede/auto.el (ede-calc-fromconfig): New.
  (ede-dirmatch-installed, ede-do-dirmatch): Use above to find config
  file.

lisp/cedet/ede/auto.el

index 6c0e5885cf51e01437b4befb62a1edf8a93181b9..a77de92f679439bba419cd31701f1e70ecefaf7b 100644 (file)
@@ -63,74 +63,61 @@ location is varied dependent on other complex criteria, this class
 can be used to define that match without loading the specific project
 into memory.")
 
-(cl-defmethod ede-dirmatch-installed ((dirmatch ede-project-autoload-dirmatch))
-  "Return non-nil if the tool DIRMATCH might match is installed on the system."
+(cl-defmethod ede-calc-fromconfig ((dirmatch ede-project-autoload-dirmatch))
+  "Calculate the value of :fromconfig from DIRMATCH."
   (let ((fc (oref dirmatch fromconfig)))
+    (cond ((stringp fc) fc)
+         ((functionp fc) (funcall fc))
+         (t (error "Unknown dirmatch object match style.")))
+    ))
 
-    (cond
-     ;; If the thing to match is stored in a config file.
-     ((stringp fc)
-      (file-exists-p fc))
-
-     ;; Add new types of dirmatches here.
-
-     ;; Error for weird stuff
-     (t (error "Unknown dirmatch type.")))))
 
+(cl-defmethod ede-dirmatch-installed ((dirmatch ede-project-autoload-dirmatch))
+  "Return non-nil if the tool DIRMATCH might match is installed on the system."
+  (file-exists-p (ede-calc-fromconfig dirmatch)))
 
 (cl-defmethod ede-do-dirmatch ((dirmatch ede-project-autoload-dirmatch) file)
   "Does DIRMATCH match the filename FILE."
-  (let ((fc (oref dirmatch fromconfig)))
-
-    (cond
-     ;; If the thing to match is stored in a config file.
-     ((stringp fc)
-      (when (file-exists-p fc)
-       (let ((matchstring
-              (if (slot-boundp dirmatch 'configdatastash)
-                  (oref dirmatch configdatastash)
-                nil)))
-         (when (and (not matchstring) (not (slot-boundp dirmatch 'configdatastash)))
-           (save-current-buffer
-             (let* ((buff (get-file-buffer fc))
-                    (readbuff
-                     (let ((find-file-hook nil)) ;; Disable ede from recursing
-                       (find-file-noselect fc))))
-               (set-buffer readbuff)
-               (save-excursion
-                 (goto-char (point-min))
-                 (when (re-search-forward (oref dirmatch configregex) nil t)
-                   (setq matchstring
-                         (match-string (or (oref dirmatch configregexidx) 0)))))
-               (if (not buff) (kill-buffer readbuff))))
-           (when matchstring
-             ;; If this dirmatch only finds subdirs of matchstring, then
-             ;; force matchstring to be a directory.
-             (when (oref dirmatch subdir-only)
-               (setq matchstring (file-name-as-directory matchstring)))
-             ;; Convert matchstring to a regexp
-             (setq matchstring (concat "^" (regexp-quote matchstring)))
-             ;; Stash it for later.
-             (oset dirmatch configdatastash matchstring))
-           ;; Debug
-           ;;(message "Stashing config data for dirmatch %S as %S" (eieio-object-name dirmatch) matchstring)
-           )
-         ;;(message "dirmatch %s against %s" matchstring (expand-file-name file))
-         ;; Match against our discovered string
-         (setq file (file-name-as-directory (expand-file-name file)))
-         (and matchstring (string-match matchstring (expand-file-name file))
-              (or (not (oref dirmatch subdir-only))
-                  (not (= (match-end 0) (length file))))
-              )
-         )))
-
-     ;; Add new matches here
-     ;; ((stringp somenewslot ...)
-     ;;   )
-
-     ;; Error if none others known
-     (t
-      (error "Unknown dirmatch object match style.")))
+  (let ((fc (ede-calc-fromconfig dirmatch)))
+
+    (when (file-exists-p fc)
+      (let ((matchstring
+            (if (slot-boundp dirmatch 'configdatastash)
+                (oref dirmatch configdatastash)
+              nil)))
+       (when (and (not matchstring) (not (slot-boundp dirmatch 'configdatastash)))
+         (save-current-buffer
+           (let* ((buff (get-file-buffer fc))
+                  (readbuff
+                   (let ((find-file-hook nil)) ;; Disable ede from recursing
+                     (find-file-noselect fc))))
+             (set-buffer readbuff)
+             (save-excursion
+               (goto-char (point-min))
+               (when (re-search-forward (oref dirmatch configregex) nil t)
+                 (setq matchstring
+                       (match-string (or (oref dirmatch configregexidx) 0)))))
+             (if (not buff) (kill-buffer readbuff))))
+         (when matchstring
+           ;; If this dirmatch only finds subdirs of matchstring, then
+           ;; force matchstring to be a directory.
+           (when (oref dirmatch subdir-only)
+             (setq matchstring (file-name-as-directory matchstring)))
+           ;; Convert matchstring to a regexp
+           (setq matchstring (concat "^" (regexp-quote matchstring)))
+           ;; Stash it for later.
+           (oset dirmatch configdatastash matchstring))
+         ;; Debug
+         ;;(message "Stashing config data for dirmatch %S as %S" (eieio-object-name dirmatch) matchstring)
+         )
+       ;;(message "dirmatch %s against %s" matchstring (expand-file-name file))
+       ;; Match against our discovered string
+       (setq file (file-name-as-directory (expand-file-name file)))
+       (and matchstring (string-match matchstring (expand-file-name file))
+            (or (not (oref dirmatch subdir-only))
+                (not (= (match-end 0) (length file))))
+            )
+       ))
     ))
 
 (declare-function ede-directory-safe-p "ede")