Skip to content

Commit c138521

Browse files
committed
Add basic drush support.
* Define a drush minor mode * Enable drupal and drush modes if buffer-file-name matches drush * Lookup documentation of symbols matching drush on api.drush.org
1 parent 7d2d703 commit c138521

File tree

1 file changed

+70
-21
lines changed

1 file changed

+70
-21
lines changed

drupal-mode.el

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
:prefix "drupal-"
4545
:group 'languages)
4646

47+
(defgroup drupal-drush nil
48+
"Drush configuration."
49+
:prefix "drupal-drush-"
50+
:group 'drupal)
51+
4752
;; Should we offer to change line endings if needed?
4853
(defcustom drupal-convert-line-ending 'ask
4954
"Should we offer to change line endings if needed?.
@@ -78,6 +83,29 @@ According to http://drupal.org/coding-standards#indenting."
7883
(string :tag "Other" "http://example.com/search?q=%s&version=%v"))
7984
:group 'drupal)
8085

86+
(defcustom drupal-drush-search-url "http://api.drush.org/api/search/%v/%s"
87+
"The URL to search the Drush API.
88+
%v is the Drush major version.
89+
%s is the search term."
90+
:type '(choice (const :tag "Drush.org" "http://api.drush.org/api/search/%v/%s")
91+
(string :tag "Other" "http://example.com/search?q=%s&version=%v"))
92+
:group 'drupal-drush)
93+
94+
(defcustom drupal-drush-program (executable-find "drush")
95+
"Name of the Drush program."
96+
:type 'file
97+
:group 'drupal-drush)
98+
99+
(defcustom drupal-drush-version (ignore-errors
100+
(replace-regexp-in-string
101+
"[\n\r]" ""
102+
(with-output-to-string
103+
(with-current-buffer standard-output
104+
(call-process drupal-drush-program nil (list t nil) nil "--version" "--pipe")))))
105+
"The installed version of Drush."
106+
:type 'string
107+
:group 'drupal-drush)
108+
81109
(defvar drupal-version nil "Drupal version as auto detected.")
82110
(make-variable-buffer-local 'drupal-version)
83111
(put 'drupal-version 'safe-local-variable 'string-or-null-p)
@@ -92,14 +120,19 @@ According to http://drupal.org/coding-standards#indenting."
92120
map)
93121
"Keymap for `drupal-mode'")
94122

123+
(defvar drupal-drush-mode-map
124+
(let ((map (make-sparse-keymap)))
125+
map)
126+
"Keymap for `drupal-drush-mode'")
127+
95128

96129

97130
;;;###autoload
98131
(define-minor-mode drupal-mode
99132
"Advanced minor mode for Drupal development.\n\n\\{drupal-mode-map}"
100133
:group 'drupal
101134
:init-value nil
102-
:lighter "/Drupal"
135+
:lighter " Drupal"
103136
:keymap drupal-mode-map
104137
(drupal-detect-drupal-version)
105138
(when (eq major-mode 'php-mode)
@@ -108,13 +141,21 @@ According to http://drupal.org/coding-standards#indenting."
108141

109142
;; setup TAGS file for etags if it exists in DRUPAL_ROOT
110143
(when (and (boundp 'drupal-root)
111-
(file-exists-p (concat drupal-root "TAGS")))
144+
(file-exists-p (concat drupal-root "TAGS")))
112145
(setq tags-file-name (concat drupal-root "TAGS")))
113146

114147
;; handle line ending and trailing whitespace
115148
(add-hook 'before-save-hook 'drupal-convert-line-ending)
116149
(add-hook 'before-save-hook 'drupal-delete-trailing-whitespace))
117150

151+
(define-minor-mode drupal-drush-mode
152+
"Advanced minor mode for Drupal Drush development.\n\n\\{drupal-drush-mode-map}"
153+
:group 'drupal-drush
154+
:init-value nil
155+
:lighter " Drush"
156+
:keymap drupal-drush-mode-map
157+
(drupal-mode 1))
158+
118159
;; drupal style
119160
(defcustom drupal-style
120161
'((c-basic-offset . 2)
@@ -193,10 +234,16 @@ should save your files with unix style end of line."
193234
(defun drupal-search-documentation ()
194235
"Search Drupal documentation for symbol at point."
195236
(interactive)
196-
(when (symbol-at-point)
197-
(browse-url
198-
(format-spec drupal-search-url `((?v . ,(drupal-major-version drupal-version))
199-
(?s . ,(symbol-at-point)))))))
237+
(let ((symbol (symbol-at-point)))
238+
(when symbol
239+
(if (and drupal-drush-program
240+
(string-match "drush" (symbol-name symbol)))
241+
(browse-url
242+
(format-spec drupal-drush-search-url `((?v . ,(replace-regexp-in-string "\.[0-9]+\\'" ".x" drupal-drush-version))
243+
(?s . ,symbol))))
244+
(browse-url
245+
(format-spec drupal-search-url `((?v . ,(drupal-major-version drupal-version))
246+
(?s . ,symbol))))))))
200247

201248

202249

@@ -208,20 +255,20 @@ should save your files with unix style end of line."
208255
drupal-version
209256
(dolist (file '("modules/system/system.module" "includes/bootstrap.inc" "core/includes/bootstrap.inc"))
210257
(let ((here (or buffer-file-name dired-directory)))
211-
(when here
212-
(let ((dir (locate-dominating-file here file)))
213-
(when dir
214-
(with-current-buffer (find-file-noselect (concat dir file) t)
215-
(save-excursion
216-
(goto-char (point-min))
217-
(when (re-search-forward "\\(define('VERSION',\\|const VERSION =\\) +'\\(.+\\)'" nil t)
218-
(dir-locals-set-class-variables 'drupal-class `((nil . ((drupal-version . ,(match-string-no-properties 2))
219-
(drupal-root . ,dir)))))
220-
(dir-locals-set-directory-class dir 'drupal-class)))
221-
(setq drupal-version (match-string-no-properties 2))
222-
)))
223-
(hack-local-variables)
224-
drupal-version)))))
258+
(when here
259+
(let ((dir (locate-dominating-file here file)))
260+
(when dir
261+
(with-current-buffer (find-file-noselect (concat dir file) t)
262+
(save-excursion
263+
(goto-char (point-min))
264+
(when (re-search-forward "\\(define('VERSION',\\|const VERSION =\\) +'\\(.+\\)'" nil t)
265+
(dir-locals-set-class-variables 'drupal-class `((nil . ((drupal-version . ,(match-string-no-properties 2))
266+
(drupal-root . ,dir)))))
267+
(dir-locals-set-directory-class dir 'drupal-class)))
268+
(setq drupal-version (match-string-no-properties 2))
269+
)))
270+
(hack-local-variables)
271+
drupal-version)))))
225272

226273
(defun drupal-major-version (&optional version)
227274
"Return major version number of version string.
@@ -249,7 +296,9 @@ mode-hook, i.e.
249296
(when (eq major-mode 'php-mode)
250297
(drupal-detect-drupal-version)
251298
(when drupal-version
252-
(drupal-mode 1))))
299+
(drupal-mode 1))
300+
(when (string-match "drush" buffer-file-name)
301+
(drupal-drush-mode 1))))
253302

254303
;;;###autoload
255304
(eval-after-load 'php-mode

0 commit comments

Comments
 (0)