Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 58 additions & 42 deletions drupal-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -558,69 +558,85 @@ Heavily based on `message-beginning-of-line' from Gnus."



(defvar drupal-local-variables (make-hash-table :test 'equal)
"Drupal local variables hash table.")

;; Detect Drupal and Drupal version
(defun drupal-detect-drupal-version ()
"Detect if the buffer is part of a Drupal project.
If part of a Drupal project also detect the version of Drupal and
the location of DRUPAL_ROOT."
(interactive)
(hack-local-variables)
(drupal-hack-local-variables)
(when (or (not drupal-version)
(not drupal-rootdir))
(dolist (file '("modules/system/system.module" "includes/bootstrap.inc" "core/lib/Drupal.php"))
(let ((here (or buffer-file-name default-directory)))
(when here
(let ((dir (locate-dominating-file here file)))
(when dir
(with-current-buffer (find-file-noselect (concat dir file) t)
(save-excursion
(widen)
(goto-char (point-min))
(when (re-search-forward "\\(define('VERSION',\\|const VERSION =\\) +'\\(.+\\)'" nil t)
(dir-locals-set-class-variables 'drupal-site `((nil . ((drupal-version . ,(match-string-no-properties 2))
(drupal-rootdir . ,dir)))))
(dir-locals-set-directory-class dir 'drupal-site)))
(setq drupal-version (match-string-no-properties 2))))))))
(hack-local-variables))
(with-temp-buffer
(insert-file-contents-literally (concat dir file))
(goto-char (point-min))
(when (re-search-forward "\\(define('VERSION',\\|const VERSION =\\) +'\\(.+\\)'" nil t)
(setq drupal-version (match-string-no-properties 2))
(puthash (expand-file-name dir) `((drupal-version . ,drupal-version)
(drupal-rootdir . ,dir))
drupal-local-variables)))))))))
(drupal-hack-local-variables)
(let ((module (drupal-locate-dominating-module (or buffer-file-name default-directory) t))
(version drupal-version)
(module-name nil)
(module-version nil)
(project nil))
(when module
(with-current-buffer (find-file-noselect module t)
(save-excursion
(widen)
(goto-char (point-min))
(when (and (not drupal-version)
(re-search-forward "^core *=" nil t))
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq version (match-string-no-properties 1)))
(goto-char (point-min))
(when (re-search-forward "^name *=" nil t)
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq module-name (match-string-no-properties 1)))
(goto-char (point-min))
(when (re-search-forward "^version *=" nil t)
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq module-version (match-string-no-properties 1)))
(goto-char (point-min))
(when (re-search-forward "^project *=" nil t)
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq project (match-string-no-properties 1)))
(when (and (string= project "drupal")
(string= module-version "VERSION"))
(setq module-version version))))
(dir-locals-set-class-variables 'drupal-module `((nil . ((drupal-module . ,(file-name-nondirectory
(file-name-sans-extension module)))
(drupal-version . ,version)
(drupal-module-name . ,module-name)
(drupal-module-version . ,module-version)
(drupal-project . ,project)))))
(dir-locals-set-directory-class (file-name-directory module) 'drupal-module)))
(hack-local-variables)
(with-temp-buffer
(insert-file-contents-literally module)
(goto-char (point-min))
(when (and (not drupal-version)
(re-search-forward "^core *=" nil t))
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq version (match-string-no-properties 1)))
(goto-char (point-min))
(when (re-search-forward "^name *=" nil t)
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq module-name (match-string-no-properties 1)))
(goto-char (point-min))
(when (re-search-forward "^version *=" nil t)
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq module-version (match-string-no-properties 1)))
(goto-char (point-min))
(when (re-search-forward "^project *=" nil t)
(re-search-forward " *\"?\\([^\"]+\\)\"?" (point-at-eol) t)
(setq project (match-string-no-properties 1)))
(when (and (string= project "drupal")
(string= module-version "VERSION"))
(setq module-version version))
(puthash (expand-file-name (file-name-directory module)) `((drupal-module . ,(file-name-nondirectory
(file-name-sans-extension module)))
(drupal-version . ,version)
(drupal-module-name . ,module-name)
(drupal-module-version . ,module-version)
(drupal-project . ,project))
drupal-local-variables))))
(drupal-hack-local-variables)
drupal-version)

(defun drupal-hack-local-variables ()
"Drupal hack `drupal-local-variables' as buffer local variables."
(interactive)
(let ((dir (expand-file-name (or (file-name-directory buffer-file-name) default-directory)))
matches)
(maphash (lambda (key value)
(when (string-match (concat "^" (regexp-quote key)) dir)
(add-to-list 'matches key)))
drupal-local-variables)
(sort matches #'(lambda (a b) (> (string-width a) (string-width b))))
(dolist (elem matches)
(let ((vars (gethash elem drupal-local-variables)))
(dolist (var vars)
(set (make-local-variable (car var)) (cdr-safe var)))))))

(defun drupal-locate-dominating-module (file &optional info-file-location)
"Look up the directory hierarchy from FILE for a Drupal module root.
Stop at the first parent where a matching module is found and
Expand Down