|
| 1 | +;;; drupal-mode.el --- Advanced minor mode for developing in Drupal |
| 2 | + |
| 3 | +;; Copyright (C) 2012 Arne Jørgensen |
| 4 | + |
| 5 | +;; Author: Arne Jørgensen <[email protected]> |
| 6 | +;; URL: https://github.com/arnested/drupal-mode |
| 7 | +;; Created: January 17, 2012 |
| 8 | +;; Version: 0.1.0 |
| 9 | +;; Package-Requires: ((php-mode "1.5.0")) |
| 10 | +;; Keywords: programming, php, drupal |
| 11 | + |
| 12 | +;; This file is NOT part of GNU Emacs. |
| 13 | + |
| 14 | +;; GNU Emacs is free software: you can redistribute it and/or modify |
| 15 | +;; it under the terms of the GNU General Public License as published by |
| 16 | +;; the Free Software Foundation, either version 3 of the License, or |
| 17 | +;; (at your option) any later version. |
| 18 | + |
| 19 | +;; GNU Emacs is distributed in the hope that it will be useful, |
| 20 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +;; GNU General Public License for more details. |
| 23 | + |
| 24 | +;; You should have received a copy of the GNU General Public License |
| 25 | +;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 26 | + |
| 27 | +;;; Commentary: |
| 28 | + |
| 29 | +;; Drupal mode is an advanced minor mode for developing in Drupal. |
| 30 | + |
| 31 | +;; Drupal mode is based on top of PHP mode and defines among other |
| 32 | +;; things indentation etc. to match Drupal Coding Standards. |
| 33 | + |
| 34 | +;;; Code: |
| 35 | + |
| 36 | +(require 'php-mode) |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +;; Customization |
| 41 | +(defgroup drupal nil |
| 42 | + "Drupal configuration." |
| 43 | + :prefix "drupal-" |
| 44 | + :group 'languages) |
| 45 | + |
| 46 | +;; Should we offer to change line endings if needed? |
| 47 | +(defcustom drupal-convert-line-ending 'ask |
| 48 | + "Should we offer to change line endings if needed?. |
| 49 | +According to http://drupal.org/coding-standards#indenting." |
| 50 | + :type `(choice |
| 51 | + :tag " we offer to change line endings if needed?" |
| 52 | + (const :tag "Always" t) |
| 53 | + (const :tag "Never" nil) |
| 54 | + (const :tag "Ask" ask)) |
| 55 | + :group 'drupal) |
| 56 | +(make-variable-buffer-local 'drupal-convert-line-ending) |
| 57 | + |
| 58 | +;; Should we delete trailing white space? |
| 59 | +(defcustom drupal-delete-trailing-whitespace t |
| 60 | + "Should we delete trailing white space?. |
| 61 | +According to http://drupal.org/coding-standards#indenting." |
| 62 | + :type `(choice |
| 63 | + :tag " we offer to delete trailing whitespace." |
| 64 | + (const :tag "Always" t) |
| 65 | + (const :tag "Never" nil)) |
| 66 | + :group 'drupal) |
| 67 | + |
| 68 | +;; |
| 69 | +(defcustom drupal-search-url "http://api.drupal.org/api/search/%s/%s" |
| 70 | + "The URL to search the Drupal API. |
| 71 | +First parameter is the Drupal version. Second parameter is the search term." |
| 72 | + :type 'string |
| 73 | + :group 'drupal) |
| 74 | + |
| 75 | +(defvar drupal-version nil "Drupal version as auto detected.") |
| 76 | +(make-variable-buffer-local 'drupal-version) |
| 77 | +(put 'drupal-version 'safe-local-variable 'string-or-null-p) |
| 78 | + |
| 79 | +(defvar drupal-mode-map |
| 80 | + (let ((map (make-sparse-keymap))) |
| 81 | + (define-key map "\C-cdf" 'drupal-search-documentation) |
| 82 | + map) |
| 83 | + "Keymap for `drupal-mode'") |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +;;;###autoload |
| 88 | +(define-minor-mode drupal-mode |
| 89 | + "Major mode for developing Drupal modules and code.\n\n\\{drupal-mode-map}" |
| 90 | + :group 'drupal |
| 91 | + :init-value nil |
| 92 | + :lighter "/Drupal" |
| 93 | + :keymap drupal-mode-map |
| 94 | + (drupal-detect-drupal-version) |
| 95 | + (when (eq major-mode 'php-mode) |
| 96 | + (c-add-language 'drupal-mode 'c-mode) |
| 97 | + (c-set-style "drupal")) |
| 98 | + |
| 99 | + (add-hook 'before-save-hook 'drupal-convert-line-ending) |
| 100 | + (add-hook 'before-save-hook 'drupal-delete-trailing-whitespace)) |
| 101 | + |
| 102 | +;; drupal style |
| 103 | +(defcustom drupal-style |
| 104 | + '((c-basic-offset . 2) |
| 105 | + (fill-column . 80) |
| 106 | + (show-trailing-whitespace . t) |
| 107 | + (indent-tabs-mode . nil) |
| 108 | + (require-final-newline . t) |
| 109 | + (c-offsets-alist . ((arglist-close . 0) |
| 110 | + (arglist-cont-nonempty . c-lineup-math) |
| 111 | + (arglist-intro . +) |
| 112 | + (case-label . +) |
| 113 | + (comment-intro . 0))) |
| 114 | + ) |
| 115 | + "Drupal coding style. |
| 116 | +According to http://drupal.org/coding-standards#indenting." |
| 117 | + :group 'drupal) |
| 118 | + |
| 119 | +(c-add-style "drupal" drupal-style) |
| 120 | +(add-to-list 'c-default-style '(drupal-mode . "drupal")) |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +(defun drupal-mode-manual () |
| 125 | + "Go to the Drupal Mode info page." |
| 126 | + (interactive) |
| 127 | + (info-display-manual "drupal-mode")) |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +;; Make a menu keymap (with a prompt string) |
| 132 | +;; and make it the menu bar item's definition. |
| 133 | +(define-key drupal-mode-map [menu-bar] (make-sparse-keymap)) |
| 134 | +(define-key drupal-mode-map [menu-bar drupal] |
| 135 | + (cons "Drupal" (make-sparse-keymap "Drupal"))) |
| 136 | + |
| 137 | +;; Define specific subcommands in this menu. |
| 138 | +(define-key drupal-mode-map |
| 139 | + [menu-bar drupal customize] |
| 140 | + '("Customize Drupal Mode" . (lambda () (interactive) (customize-group 'drupal)))) |
| 141 | +(define-key drupal-mode-map |
| 142 | + [menu-bar drupal manual] |
| 143 | + '("Drupal Mode manual" . drupal-mode-manual)) |
| 144 | +(define-key drupal-mode-map |
| 145 | + [menu-bar drupal search-documentation] |
| 146 | + '("Search documentation" . drupal-search-documentation)) |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +;; utility functions |
| 151 | + |
| 152 | +(defun drupal-convert-line-ending () |
| 153 | + "Convert to unix style line ending. |
| 154 | +According to http://drupal.org/coding-standards#indenting you |
| 155 | +should save your files with unix style end of line." |
| 156 | + (when (and (eq major-mode 'drupal-mode) |
| 157 | + drupal-convert-line-ending |
| 158 | + (/= (coding-system-eol-type buffer-file-coding-system) 0)) |
| 159 | + (if (or (eq drupal-convert-line-ending t) |
| 160 | + (y-or-n-p "Convert to unix style line endings?")) |
| 161 | + (progn |
| 162 | + (message "Coding system conversion") |
| 163 | + (set-buffer-file-coding-system 'unix)) |
| 164 | + (progn |
| 165 | + (setq drupal-convert-line-ending nil))))) |
| 166 | + |
| 167 | +(defun drupal-delete-trailing-whitespace () |
| 168 | + "Delete trailing whitespace if in drupal mode." |
| 169 | + (when (and (eq major-mode 'drupal-mode) |
| 170 | + drupal-delete-trailing-whitespace) |
| 171 | + (delete-trailing-whitespace))) |
| 172 | + |
| 173 | +(defun drupal-search-documentation () |
| 174 | + "Search Drupal documentation for symbol at point." |
| 175 | + (interactive) |
| 176 | + (browse-url(format drupal-search-url (drupal-major-version drupal-version) (symbol-at-point)))) |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +;; Detect Drupal and Drupal version |
| 181 | +(defun drupal-detect-drupal-version () |
| 182 | + "Detect if the buffer is part of a Drupal project." |
| 183 | + (interactive) |
| 184 | + (if drupal-version |
| 185 | + drupal-version |
| 186 | + (dolist (file '("modules/system/system.module" "includes/bootstrap.inc" "core/includes/bootstrap.inc")) |
| 187 | + (let ((dir (locate-dominating-file buffer-file-name file))) |
| 188 | + (when dir |
| 189 | + (with-current-buffer (find-file-noselect (concat dir file) t) |
| 190 | + (save-excursion |
| 191 | + (goto-char (point-min)) |
| 192 | + (when (re-search-forward "\\(define('VERSION',\\|const VERSION =\\) +'\\(.+\\)'" nil t) |
| 193 | + (dir-locals-set-class-variables 'drupal-class `((nil . ((drupal-version . ,(match-string-no-properties 2)))))) |
| 194 | + (dir-locals-set-directory-class dir 'drupal-class))) |
| 195 | + (setq drupal-version (match-string-no-properties 2)) |
| 196 | + ) |
| 197 | + (hack-local-variables)))) |
| 198 | + drupal-version)) |
| 199 | + |
| 200 | +(defun drupal-major-version (&optional version) |
| 201 | + "Return major version number of version string. |
| 202 | +If major version number is 4 - return both major and minor." |
| 203 | + (unless version |
| 204 | + (setq version (drupal-detect-drupal-version))) |
| 205 | + (let ((version-list (split-string version "\\."))) |
| 206 | + (if (= (string-to-number (car version-list)) 4) |
| 207 | + (format "%s.%s" (car version-list) (cadr version-list)) |
| 208 | + (car version-list)))) |
| 209 | + |
| 210 | +;;;###autoload |
| 211 | +(defun drupal-mode-bootstrap () |
| 212 | + "Activate Drupal minor mode if major mode is supported. |
| 213 | +The command will activate `drupal-mode' if the current major mode |
| 214 | +is a mode supported by `drupal-mode' (currently only |
| 215 | +`php-mode'). |
| 216 | +
|
| 217 | +The function is suitable for adding to the supported major modes |
| 218 | +mode-hook, i.e. |
| 219 | +
|
| 220 | +(eval-after-load 'php-mode |
| 221 | + '(add-hook 'php-mode-hook 'drupal-mode-bootstrap)) |
| 222 | +" |
| 223 | + (when (eq major-mode 'php-mode) |
| 224 | + (drupal-detect-drupal-version) |
| 225 | + (when drupal-version |
| 226 | + (drupal-mode 1)))) |
| 227 | + |
| 228 | +;;;###autoload |
| 229 | +(eval-after-load 'php-mode |
| 230 | + '(add-hook 'php-mode-hook 'drupal-mode-bootstrap)) |
| 231 | + |
| 232 | +;;;###autoload |
| 233 | +(progn |
| 234 | + (add-to-list 'auto-mode-alist '("\\.\\(module\\|test\\|install\\|theme\\|tpl\\.php\\)$" . php-mode)) |
| 235 | + (add-to-list 'auto-mode-alist '("\\.info$" . conf-windows-mode))) |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | +(provide 'drupal-mode) |
| 240 | + |
| 241 | +;; Local Variables: |
| 242 | +;; coding: utf-8 |
| 243 | +;; End: |
| 244 | + |
| 245 | +;;; drupal-mode.el ends here |
0 commit comments