[1] | 1 | |
---|
| 2 | ;;----------------------------------------------------------- |
---|
| 3 | ;; Copyright Christian Arnault LAL-Orsay CNRS |
---|
| 4 | ;; arnault@lal.in2p3.fr |
---|
| 5 | ;; See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
| 6 | ;;----------------------------------------------------------- |
---|
| 7 | |
---|
| 8 | ;;; @(#) cmt-mode.el --- a major mode for editing CMT configuration files |
---|
| 9 | |
---|
| 10 | ;; $Id: cmt-mode.el,v 1.3 2004/08/25 09:04:27 arnault Exp $ |
---|
| 11 | |
---|
| 12 | ;; Copyright (C) 2004 by Antoine Pérus |
---|
| 13 | |
---|
| 14 | ;; Author: Antoine Pérus <perus@lal.in2p3.fr> |
---|
| 15 | ;; Created: 29 Feb 2004 |
---|
| 16 | ;; Keywords: local, languages, tools |
---|
| 17 | |
---|
| 18 | ;; This file is not part of XEmacs |
---|
| 19 | |
---|
| 20 | ;; This program is free software; you can redistribute it and/or modify |
---|
| 21 | ;; it under the terms of the GNU General Public License as published by |
---|
| 22 | ;; the Free Software Foundation; either version 2, or (at your option) |
---|
| 23 | ;; any later version. |
---|
| 24 | |
---|
| 25 | ;; This program is distributed in the hope that it will be useful, |
---|
| 26 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 27 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 28 | ;; GNU General Public License for more details. |
---|
| 29 | |
---|
| 30 | ;; You should have received a copy of the GNU General Public License |
---|
| 31 | ;; along with this program; see the file COPYING. If not, write to the |
---|
| 32 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 33 | ;; Boston, MA 02111-1307, USA. |
---|
| 34 | |
---|
| 35 | ;;; Commentary: |
---|
| 36 | ;; |
---|
| 37 | ;; This mode adds some services editing the CMT requirements file. |
---|
| 38 | ;; See <http://www.cmtsite.org> for a CMT description. |
---|
| 39 | ;; This is a hack initialy started from apache.el and css-mode.el |
---|
| 40 | ;; The list of keywords was derived from the documentation for CMT; |
---|
| 41 | ;; there is lot of omissions. |
---|
| 42 | ;; |
---|
| 43 | ;; To enable automatic selection of this mode : |
---|
| 44 | ;; |
---|
| 45 | ;; (autoload 'cmt-mode "cmt-mode" "autoloaded" t) |
---|
| 46 | ;; (add-to-list 'auto-mode-alist '("requirements" . cmt-mode)) |
---|
| 47 | ;; |
---|
| 48 | |
---|
| 49 | ;;; Change Log: |
---|
| 50 | ;; |
---|
| 51 | ;; 13/03/04 - New way in definition of cmt-font-lock-keywords with a more |
---|
| 52 | ;; modular definition of keyword groups. Should help too for the next step : |
---|
| 53 | ;; a completion mechanism. |
---|
| 54 | |
---|
| 55 | ;;; Code: |
---|
| 56 | |
---|
| 57 | ;; Requires modules |
---|
| 58 | (eval-when-compile |
---|
| 59 | (defvar byte-compile-dynamic nil) ;silence old Emacs's ByteCompiler |
---|
| 60 | (set (make-local-variable 'byte-compile-dynamic) t)) |
---|
| 61 | |
---|
| 62 | (require 'font-lock) |
---|
| 63 | (require 'custom) |
---|
| 64 | |
---|
| 65 | ;; Variables |
---|
| 66 | |
---|
| 67 | ;;;###autoload |
---|
| 68 | (defgroup cmt nil |
---|
| 69 | "Major mode for editing CMT configuration files." |
---|
| 70 | :prefix "cmt-" |
---|
| 71 | :group 'languages) |
---|
| 72 | |
---|
| 73 | (defcustom cmt-manual-url "http://www.cmtsite.org/CMTDoc.html" |
---|
| 74 | "*URL at which to find the CMT manual." |
---|
| 75 | :type 'string |
---|
| 76 | :group 'cmt) |
---|
| 77 | |
---|
| 78 | ;;;###autoload |
---|
| 79 | (defcustom cmt-file-patterns |
---|
| 80 | (list "requirements") |
---|
| 81 | "*List of file patterns for which to automatically invoke `cmt-mode'." |
---|
| 82 | :type '(repeat (regexp :tag "Pattern")) |
---|
| 83 | :group 'cmt) |
---|
| 84 | |
---|
| 85 | (defcustom cmt-mode-hook nil |
---|
| 86 | "*List of hook functions run by `cmt-mode' (see `run-hooks')." |
---|
| 87 | :type 'hook |
---|
| 88 | :group 'cmt) |
---|
| 89 | |
---|
| 90 | (defvar cmt-mode-version-id |
---|
| 91 | "$Id: cmt-mode.el,v 1.3 2004/08/25 09:04:27 arnault Exp $" |
---|
| 92 | "Latest modification time and version number.") |
---|
| 93 | |
---|
| 94 | (defconst cmt-mode-version "0.4" |
---|
| 95 | "Cmt-mode official version.") |
---|
| 96 | |
---|
| 97 | ;;; --- end of up-to-date-overview configuration ------------------ |
---|
| 98 | |
---|
| 99 | (defvar cmt-mode-syntax-table nil |
---|
| 100 | "Syntax table for `cmt-mode'.") |
---|
| 101 | |
---|
| 102 | ;; Make cmt-mode the default mode for cmt config buffers. |
---|
| 103 | ;;;###autoload |
---|
| 104 | (let ((cmt-file-patterns-temp cmt-file-patterns)) |
---|
| 105 | (while cmt-file-patterns-temp |
---|
| 106 | (add-to-list 'auto-mode-alist |
---|
| 107 | (cons (car cmt-file-patterns-temp) 'cmt-mode)) |
---|
| 108 | (setq cmt-file-patterns-temp (cdr cmt-file-patterns-temp)))) |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | (defconst cmt-symbols |
---|
| 113 | '( |
---|
| 114 | "alias" "action" |
---|
| 115 | ;; environment directives |
---|
| 116 | "set" "set_append" "set_prepend" "set_remove" |
---|
| 117 | ;; path directives |
---|
| 118 | "path" "path_append" "path_prepend" "path_remove" |
---|
| 119 | ;; macro directives |
---|
| 120 | "macro" "macro_append" "macro_prepend" "macro_remove" |
---|
| 121 | ;; setup directives |
---|
| 122 | "setup_script" "cleanup_script" |
---|
| 123 | "setup_strategy" |
---|
| 124 | ) |
---|
| 125 | "A list of CMT symbol keywords.") |
---|
| 126 | |
---|
| 127 | (defconst cmt-patterns |
---|
| 128 | '( |
---|
| 129 | ;; Pattern directives |
---|
| 130 | "pattern" "apply_pattern" "ignore_pattern" "cmtpath_pattern" |
---|
| 131 | ) |
---|
| 132 | "A list of CMT pattern keywords.") |
---|
| 133 | |
---|
| 134 | (defconst cmt-tags |
---|
| 135 | '( |
---|
| 136 | "tag" "apply_tag" "ignore_tag" "tag_exclude" |
---|
| 137 | ) |
---|
| 138 | "A list of CMT tags relative keywords.") |
---|
| 139 | |
---|
| 140 | (defconst cmt-features |
---|
| 141 | '( |
---|
| 142 | ;; core directives |
---|
| 143 | "package" "version" "project" |
---|
| 144 | "author" "manager" |
---|
| 145 | ;; |
---|
| 146 | "use" |
---|
| 147 | ;; |
---|
| 148 | "branches" |
---|
| 149 | ;; |
---|
| 150 | "public" "end_public" "private" "end_private" |
---|
| 151 | ;; |
---|
| 152 | "application" "library" "document" |
---|
| 153 | "make_fragment" |
---|
| 154 | ;; |
---|
| 155 | "include_dirs" "include_path" |
---|
| 156 | ) |
---|
| 157 | "A list of CMT feature keywords.") |
---|
| 158 | |
---|
| 159 | (defconst cmt-options |
---|
| 160 | '( |
---|
| 161 | "-no_share" "-no_static" |
---|
| 162 | "-prototypes" "-no_prototypes" "-check" |
---|
| 163 | "-group" "-suffix" |
---|
| 164 | "-import" "-no_auto_imports" |
---|
| 165 | "-header" "-global" |
---|
| 166 | ) |
---|
| 167 | "A list of CMT options.") |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | (defconst cmt-keywords-alist |
---|
| 171 | (append |
---|
| 172 | (mapcar (lambda(prop) |
---|
| 173 | (cons (concat prop " ") nil)) cmt-symbols) |
---|
| 174 | (mapcar (lambda(prop) |
---|
| 175 | (cons (concat prop " ") nil)) cmt-patterns) |
---|
| 176 | (mapcar (lambda(prop) |
---|
| 177 | (cons (concat prop " ") nil)) cmt-tags) |
---|
| 178 | (mapcar (lambda(prop) |
---|
| 179 | (cons (concat prop " ") nil)) cmt-features) |
---|
| 180 | (mapcar (lambda(prop) |
---|
| 181 | (cons (concat prop "=") nil)) cmt-options) |
---|
| 182 | ) |
---|
| 183 | "An association list of the CMT keywords for completion use.") |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | (defun cmt-list-2-regexp-at-beginning-of-line(altlist) |
---|
| 187 | "Takes a list and returns the regexp ^\\s-*\\(elem1\\|elem2\\|...\\)\\s-" |
---|
| 188 | (let ((regexp "^\\s-*\\(")) |
---|
| 189 | (mapcar (lambda(elem) |
---|
| 190 | (setq regexp (concat regexp elem "\\|"))) |
---|
| 191 | altlist) |
---|
| 192 | (concat (substring regexp 0 -2) ; cutting the last "\\|" |
---|
| 193 | "\\)\\s-") |
---|
| 194 | )) |
---|
| 195 | |
---|
| 196 | (defun cmt-list-2-regexp(altlist) |
---|
| 197 | "Takes a list and returns the regexp \\(elem1\\|elem2\\|...\\)" |
---|
| 198 | (let ((regexp "\\(")) |
---|
| 199 | (mapcar (lambda(elem) |
---|
| 200 | (setq regexp (concat regexp elem "\\|"))) |
---|
| 201 | altlist) |
---|
| 202 | (concat (substring regexp 0 -2) ; cutting the last "\\|" |
---|
| 203 | "\\)") |
---|
| 204 | )) |
---|
| 205 | |
---|
| 206 | ;; Font lock |
---|
| 207 | (defconst cmt-font-lock-keywords |
---|
| 208 | (purecopy |
---|
| 209 | (list |
---|
| 210 | (list "^\\s-*#.*$" 0 'font-lock-comment-face t) |
---|
| 211 | |
---|
| 212 | (cons (cmt-list-2-regexp-at-beginning-of-line cmt-symbols) |
---|
| 213 | font-lock-function-name-face) |
---|
| 214 | |
---|
| 215 | (cons (cmt-list-2-regexp-at-beginning-of-line cmt-patterns) |
---|
| 216 | font-lock-function-name-face) |
---|
| 217 | |
---|
| 218 | (cons (cmt-list-2-regexp-at-beginning-of-line cmt-tags) |
---|
| 219 | font-lock-type-face) |
---|
| 220 | |
---|
| 221 | (cons (cmt-list-2-regexp-at-beginning-of-line cmt-features) |
---|
| 222 | font-lock-keyword-face) |
---|
| 223 | |
---|
| 224 | (cons (cmt-list-2-regexp cmt-options) font-lock-type-face) |
---|
| 225 | |
---|
| 226 | )) |
---|
| 227 | "Expressions to highlight in `cmt-mode' buffers.") |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | ;; Motion code. |
---|
| 232 | |
---|
| 233 | (defconst cmt-pattern-regex |
---|
| 234 | (eval-when-compile |
---|
| 235 | (concat |
---|
| 236 | "^" |
---|
| 237 | "\\(" |
---|
| 238 | "pattern" |
---|
| 239 | "\\)")) |
---|
| 240 | "Regex used to find patterns.") |
---|
| 241 | |
---|
| 242 | (defun cmt-next-pattern () |
---|
| 243 | "Move point to the beginning of the next pattern definition." |
---|
| 244 | (interactive) |
---|
| 245 | (let ((here (point))) |
---|
| 246 | (end-of-line) |
---|
| 247 | (if (re-search-forward cmt-pattern-regex (point-max) t) |
---|
| 248 | (progn (beginning-of-line) t) ; indicate success |
---|
| 249 | (goto-char here) nil))) |
---|
| 250 | |
---|
| 251 | (defun cmt-previous-pattern () |
---|
| 252 | "Move point to the beginning of the previous pattern definition." |
---|
| 253 | (interactive) |
---|
| 254 | (let ((here (point))) |
---|
| 255 | (beginning-of-line) |
---|
| 256 | (if (re-search-backward cmt-pattern-regex (point-min) t) |
---|
| 257 | (progn (beginning-of-line) t) ; indicate success |
---|
| 258 | (goto-char here) nil))) |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | (defun cmt-next-feature () |
---|
| 262 | "Move point to the beginning of the next feature." |
---|
| 263 | (interactive) |
---|
| 264 | (let ((here (point))) |
---|
| 265 | (end-of-line) |
---|
| 266 | (if (re-search-forward |
---|
| 267 | (cmt-list-2-regexp-at-beginning-of-line cmt-features) |
---|
| 268 | (point-max) t) |
---|
| 269 | (progn (beginning-of-line) t) ; indicate success |
---|
| 270 | (goto-char here) nil))) |
---|
| 271 | |
---|
| 272 | (defun cmt-previous-feature () |
---|
| 273 | "Move point to the beginning of the previous feature." |
---|
| 274 | (interactive) |
---|
| 275 | (let ((here (point))) |
---|
| 276 | (beginning-of-line) |
---|
| 277 | (if (re-search-backward |
---|
| 278 | (cmt-list-2-regexp-at-beginning-of-line cmt-features) |
---|
| 279 | (point-min) t) |
---|
| 280 | (progn (beginning-of-line) t) ; indicate success |
---|
| 281 | (goto-char here) nil))) |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | (defconst cmt-menubar-menu |
---|
| 285 | '("Cmt" |
---|
| 286 | ["Move to Next Pattern" cmt-next-pattern t] |
---|
| 287 | ["Move to Previous Pattern" cmt-previous-pattern t] |
---|
| 288 | "---" |
---|
| 289 | ["Move to Next Feature" cmt-next-feature t] |
---|
| 290 | ["Move to Previous Feature" cmt-previous-feature t] |
---|
| 291 | )) |
---|
| 292 | |
---|
| 293 | (defconst cmt-popup-menu |
---|
| 294 | (cons "CMT Mode Commands" |
---|
| 295 | (cdr cmt-menubar-menu))) |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | ;;=============== |
---|
| 301 | |
---|
| 302 | ;;; Property completion |
---|
| 303 | ; This code is stolen from ans1-mode.el. |
---|
| 304 | |
---|
| 305 | (defun cmt-completion () |
---|
| 306 | "Do a completion with `cmt-keywords-alist'." |
---|
| 307 | (interactive) |
---|
| 308 | (let* ((end (point)) |
---|
| 309 | (buffer-syntax (syntax-table)) |
---|
| 310 | (beg (unwind-protect |
---|
| 311 | (save-excursion |
---|
| 312 | (set-syntax-table cmt-mode-syntax-table) |
---|
| 313 | (skip-syntax-backward "\\w") |
---|
| 314 | (point)) |
---|
| 315 | (set-syntax-table buffer-syntax))) |
---|
| 316 | (pattern (buffer-substring beg end)) |
---|
| 317 | (completion (try-completion pattern cmt-keywords-alist))) |
---|
| 318 | (cond ((eq completion t)) |
---|
| 319 | ((null completion) |
---|
| 320 | (message "Can't find completion for %s " pattern) |
---|
| 321 | (ding)) |
---|
| 322 | ((not (string= pattern completion)) |
---|
| 323 | (delete-region beg end) |
---|
| 324 | (insert completion)) |
---|
| 325 | (t |
---|
| 326 | (with-output-to-temp-buffer "*Completions*" |
---|
| 327 | (display-completion-list |
---|
| 328 | (sort (all-completions pattern |
---|
| 329 | cmt-keywords-alist) |
---|
| 330 | 'string<)))) |
---|
| 331 | ) |
---|
| 332 | ) |
---|
| 333 | ) |
---|
| 334 | |
---|
| 335 | |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | ;;=============== |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | ;; Syntax table |
---|
| 342 | (if cmt-mode-syntax-table |
---|
| 343 | nil |
---|
| 344 | (setq cmt-mode-syntax-table (copy-syntax-table nil)) |
---|
| 345 | ;; underscore considered part of word |
---|
| 346 | (modify-syntax-entry ?_ "w" cmt-mode-syntax-table) |
---|
| 347 | (modify-syntax-entry ?\- "w" cmt-mode-syntax-table) |
---|
| 348 | (modify-syntax-entry ?\" "\"" cmt-mode-syntax-table)) |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | ;; Keymap for cmt-mode. |
---|
| 352 | (defvar cmt-mode-map nil |
---|
| 353 | "Keymap used in `cmt-mode' buffers.") |
---|
| 354 | |
---|
| 355 | (if cmt-mode-map |
---|
| 356 | nil |
---|
| 357 | (setq cmt-mode-map (make-sparse-keymap)) |
---|
| 358 | ;; Set up the keymap |
---|
| 359 | (define-key cmt-mode-map "\M-\t" 'cmt-completion) |
---|
| 360 | (define-key cmt-mode-map "\M-p" 'cmt-previous-pattern) |
---|
| 361 | (define-key cmt-mode-map "\M-n" 'cmt-next-pattern) |
---|
| 362 | (define-key cmt-mode-map "\M-f" 'cmt-next-feature) |
---|
| 363 | (define-key cmt-mode-map "\M-b" 'cmt-previous-feature) |
---|
| 364 | ) |
---|
| 365 | |
---|
| 366 | |
---|
| 367 | ;;;###autoload |
---|
| 368 | (defun cmt-mode () |
---|
| 369 | "Major mode for editing CMT requirements files. |
---|
| 370 | See <http://www.cmtsite.org/CMTDoc.html>. |
---|
| 371 | |
---|
| 372 | \\{cmt-mode-map}" |
---|
| 373 | |
---|
| 374 | (interactive) |
---|
| 375 | |
---|
| 376 | ; Initializing |
---|
| 377 | (kill-all-local-variables) |
---|
| 378 | |
---|
| 379 | (use-local-map cmt-mode-map) |
---|
| 380 | (set-syntax-table cmt-mode-syntax-table) |
---|
| 381 | |
---|
| 382 | ; Setting up font-locking |
---|
| 383 | (make-local-variable 'font-lock-defaults) |
---|
| 384 | (setq font-lock-defaults '(cmt-font-lock-keywords nil t nil nil)) |
---|
| 385 | |
---|
| 386 | ; Comment stuff. |
---|
| 387 | (make-local-variable 'comment-start) |
---|
| 388 | (make-local-variable 'comment-end) |
---|
| 389 | (make-local-variable 'comment-start-skip) |
---|
| 390 | (make-local-variable 'comment-column) |
---|
| 391 | |
---|
| 392 | (setq comment-start "#" |
---|
| 393 | comment-end "" |
---|
| 394 | comment-start-skip "#\\W*" |
---|
| 395 | comment-column 48) |
---|
| 396 | |
---|
| 397 | ;; Set menu |
---|
| 398 | ;; XEmacs addition |
---|
| 399 | (setq mode-popup-menu cmt-popup-menu) |
---|
| 400 | (if (featurep 'menubar) |
---|
| 401 | (progn |
---|
| 402 | ;; make a local copy of the menubar, so our mode doesn't |
---|
| 403 | ;; change the global menubar |
---|
| 404 | (set-buffer-menubar current-menubar) |
---|
| 405 | (add-submenu nil cmt-menubar-menu))) |
---|
| 406 | |
---|
| 407 | (setq mode-name "CMT" |
---|
| 408 | major-mode 'cmt-mode) |
---|
| 409 | (run-hooks 'cmt-mode-hook)) |
---|
| 410 | |
---|
| 411 | |
---|
| 412 | ;; Provides |
---|
| 413 | (provide 'cmt-mode) |
---|
| 414 | |
---|
| 415 | ;;; cmt-mode.el ends here |
---|