@@ -558,69 +558,85 @@ Heavily based on `message-beginning-of-line' from Gnus."
558558
559559
560560
561+ (defvar drupal-local-variables (make-hash-table :test 'equal )
562+ " Drupal local variables hash table." )
563+
561564; ; Detect Drupal and Drupal version
562565(defun drupal-detect-drupal-version ()
563566 " Detect if the buffer is part of a Drupal project.
564567If part of a Drupal project also detect the version of Drupal and
565568the location of DRUPAL_ROOT."
566569 (interactive )
567- (hack-local-variables )
570+ (drupal- hack-local-variables)
568571 (when (or (not drupal-version)
569572 (not drupal-rootdir))
570573 (dolist (file '(" modules/system/system.module" " includes/bootstrap.inc" " core/lib/Drupal.php" ))
571574 (let ((here (or buffer-file-name default-directory)))
572575 (when here
573576 (let ((dir (locate-dominating-file here file)))
574577 (when dir
575- (with-current-buffer (find-file-noselect (concat dir file) t )
576- (save-excursion
577- (widen )
578- (goto-char (point-min ))
579- (when (re-search-forward " \\ (define('VERSION',\\ |const VERSION =\\ ) +'\\ (.+\\ )'" nil t )
580- (dir-locals-set-class-variables 'drupal-site `((nil . ((drupal-version . ,(match-string-no-properties 2 ))
581- (drupal-rootdir . , dir )))))
582- (dir-locals-set-directory-class dir 'drupal-site )))
583- (setq drupal-version (match-string-no-properties 2 ))))))))
584- (hack-local-variables ))
578+ (with-temp-buffer
579+ (insert-file-contents-literally (concat dir file))
580+ (goto-char (point-min ))
581+ (when (re-search-forward " \\ (define('VERSION',\\ |const VERSION =\\ ) +'\\ (.+\\ )'" nil t )
582+ (setq drupal-version (match-string-no-properties 2 ))
583+ (puthash (expand-file-name dir) `((drupal-version . , drupal-version )
584+ (drupal-rootdir . , dir ))
585+ drupal-local-variables)))))))))
586+ (drupal-hack-local-variables)
585587 (let ((module (drupal-locate-dominating-module (or buffer-file-name default-directory) t ))
586588 (version drupal-version)
587589 (module-name nil )
588590 (module-version nil )
589591 (project nil ))
590592 (when module
591- (with-current-buffer (find-file-noselect module t )
592- (save-excursion
593- (widen )
594- (goto-char (point-min ))
595- (when (and (not drupal-version)
596- (re-search-forward " ^core *=" nil t ))
597- (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
598- (setq version (match-string-no-properties 1 )))
599- (goto-char (point-min ))
600- (when (re-search-forward " ^name *=" nil t )
601- (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
602- (setq module-name (match-string-no-properties 1 )))
603- (goto-char (point-min ))
604- (when (re-search-forward " ^version *=" nil t )
605- (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
606- (setq module-version (match-string-no-properties 1 )))
607- (goto-char (point-min ))
608- (when (re-search-forward " ^project *=" nil t )
609- (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
610- (setq project (match-string-no-properties 1 )))
611- (when (and (string= project " drupal" )
612- (string= module-version " VERSION" ))
613- (setq module-version version))))
614- (dir-locals-set-class-variables 'drupal-module `((nil . ((drupal-module . ,(file-name-nondirectory
615- (file-name-sans-extension module)))
616- (drupal-version . , version )
617- (drupal-module-name . , module-name )
618- (drupal-module-version . , module-version )
619- (drupal-project . , project )))))
620- (dir-locals-set-directory-class (file-name-directory module) 'drupal-module )))
621- (hack-local-variables )
593+ (with-temp-buffer
594+ (insert-file-contents-literally module)
595+ (goto-char (point-min ))
596+ (when (and (not drupal-version)
597+ (re-search-forward " ^core *=" nil t ))
598+ (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
599+ (setq version (match-string-no-properties 1 )))
600+ (goto-char (point-min ))
601+ (when (re-search-forward " ^name *=" nil t )
602+ (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
603+ (setq module-name (match-string-no-properties 1 )))
604+ (goto-char (point-min ))
605+ (when (re-search-forward " ^version *=" nil t )
606+ (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
607+ (setq module-version (match-string-no-properties 1 )))
608+ (goto-char (point-min ))
609+ (when (re-search-forward " ^project *=" nil t )
610+ (re-search-forward " *\" ?\\ ([^\" ]+\\ )\" ?" (point-at-eol ) t )
611+ (setq project (match-string-no-properties 1 )))
612+ (when (and (string= project " drupal" )
613+ (string= module-version " VERSION" ))
614+ (setq module-version version))
615+ (puthash (expand-file-name (file-name-directory module)) `((drupal-module . ,(file-name-nondirectory
616+ (file-name-sans-extension module)))
617+ (drupal-version . , version )
618+ (drupal-module-name . , module-name )
619+ (drupal-module-version . , module-version )
620+ (drupal-project . , project ))
621+ drupal-local-variables))))
622+ (drupal-hack-local-variables)
622623 drupal-version)
623624
625+ (defun drupal-hack-local-variables ()
626+ " Drupal hack `drupal-local-variables' as buffer local variables."
627+ (interactive )
628+ (let ((dir (expand-file-name (or (file-name-directory buffer-file-name) default-directory)))
629+ matches)
630+ (maphash (lambda (key value )
631+ (when (string-match (concat " ^" (regexp-quote key)) dir)
632+ (add-to-list 'matches key)))
633+ drupal-local-variables)
634+ (sort matches #' (lambda (a b ) (> (string-width a) (string-width b))))
635+ (dolist (elem matches)
636+ (let ((vars (gethash elem drupal-local-variables)))
637+ (dolist (var vars)
638+ (set (make-local-variable (car var)) (cdr-safe var)))))))
639+
624640(defun drupal-locate-dominating-module (file &optional info-file-location )
625641 " Look up the directory hierarchy from FILE for a Drupal module root.
626642Stop at the first parent where a matching module is found and
0 commit comments