| 1 | ;;; generic-mode.el --- A meta-mode which makes it easy to create small |
| 2 | ;; modes with basic comment and font-lock support |
| 3 | ;; |
| 4 | ;; Copyright (C) 1997 Free Software Foundation, Inc. |
| 5 | ;; |
| 6 | ;; Author: Peter Breton |
| 7 | ;; Created: Fri Sep 27 1996 |
| 8 | ;; Keywords: generic, comment, font-lock |
| 9 | |
| 10 | ;; This file is part of GNU Emacs. |
| 11 | |
| 12 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation; either version 2, or (at your option) |
| 15 | ;; any later version. |
| 16 | |
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;; GNU General Public License for more details. |
| 21 | |
| 22 | ;; You should have received a copy of the GNU General Public License |
| 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 25 | ;; Boston, MA 02111-1307, USA. |
| 26 | |
| 27 | ;; Purpose: |
| 28 | |
| 29 | ;; Meta-mode to create simple major modes |
| 30 | ;; with basic comment and font-lock support |
| 31 | |
| 32 | ;;; Commentary: |
| 33 | |
| 34 | ;; INTRODUCTION: |
| 35 | |
| 36 | ;; Generic-mode is a meta-mode which can be used to define small modes |
| 37 | ;; which provide basic comment and font-lock support. These modes are |
| 38 | ;; intended for the many configuration files and such which are too small |
| 39 | ;; for a "real" mode, but still have a regular syntax, comment characters |
| 40 | ;; and the like. |
| 41 | ;; |
| 42 | ;; Each generic mode can define the following: |
| 43 | ;; |
| 44 | ;; * List of comment-characters. The entries in this list should be |
| 45 | ;; either a character, a one or two character string or a cons pair. |
| 46 | ;; If the entry is a character or a one-character string |
| 47 | ;; LIMITATIONS: Emacs does not support comment strings of more than |
| 48 | ;; two characters in length. |
| 49 | ;; |
| 50 | ;; * List of keywords to font-lock. Each keyword should be a string. |
| 51 | ;; If you have additional keywords which should be highlighted in a face |
| 52 | ;; different from 'font-lock-keyword-face', you can use the convenience |
| 53 | ;; function 'generic-make-keywords-list' (which see), and add the |
| 54 | ;; result to the following list: |
| 55 | ;; |
| 56 | ;; * Additional expressions to font-lock. This should be a list of |
| 57 | ;; expressions, each of which should be of the same form |
| 58 | ;; as those in 'font-lock-defaults-alist'. |
| 59 | ;; |
| 60 | ;; * List of regular expressions to be placed in auto-mode-alist. |
| 61 | ;; |
| 62 | ;; * List of functions to call to do some additional setup |
| 63 | ;; |
| 64 | ;; This should pretty much cover basic functionality; if you need much |
| 65 | ;; more than this, or you find yourself writing extensive customizations, |
| 66 | ;; perhaps you should be writing a major mode instead! |
| 67 | ;; |
| 68 | ;; LOCAL VARIABLES: |
| 69 | ;; |
| 70 | ;; To put a file into generic mode using local variables, use a line |
| 71 | ;; like this in a Local Variables block: |
| 72 | ;; |
| 73 | ;; mode: default-generic |
| 74 | ;; |
| 75 | ;; Do NOT use "mode: generic"! |
| 76 | ;; See also "AUTOMATICALLY ENTERING GENERIC MODE" below. |
| 77 | ;; |
| 78 | ;; DEFINING NEW GENERIC MODES: |
| 79 | ;; |
| 80 | ;; Use the 'define-generic-mode' function to define new modes. |
| 81 | ;; For example: |
| 82 | ;; |
| 83 | ;; (require 'generic-mode) |
| 84 | ;; (define-generic-mode 'foo-generic-mode |
| 85 | ;; (list ?% ) |
| 86 | ;; (list "keyword") |
| 87 | ;; nil |
| 88 | ;; (list "\.FOO") |
| 89 | ;; (list 'foo-setup-function)) |
| 90 | ;; |
| 91 | ;; defines a new generic-mode 'foo-generic-mode', which has '%' as a |
| 92 | ;; comment character, and "keyword" as a keyword. When files which end in |
| 93 | ;; '.FOO' are loaded, Emacs will go into foo-generic-mode and call |
| 94 | ;; foo-setup-function. You can also use the function 'foo-generic-mode' |
| 95 | ;; (which is interactive) to put a buffer into foo-generic-mode. |
| 96 | ;; |
| 97 | ;; AUTOMATICALLY ENTERING GENERIC MODE: |
| 98 | ;; |
| 99 | ;; Generic-mode provides a hook which automatically puts a |
| 100 | ;; file into default-generic-mode if the first few lines of a file in |
| 101 | ;; fundamental mode start with a hash comment character. To disable |
| 102 | ;; this functionality, set the variable 'generic-use-find-file-hook' |
| 103 | ;; to nil BEFORE loading generic-mode. See the variables |
| 104 | ;; 'generic-lines-to-scan' and 'generic-find-file-regexp' for customization |
| 105 | ;; options. |
| 106 | ;; |
| 107 | ;; GOTCHAS: |
| 108 | ;; |
| 109 | ;; Be careful that your font-lock definitions are correct. Getting them |
| 110 | ;; wrong can cause emacs to continually attempt to fontify! This problem |
| 111 | ;; is not specific to generic-mode. |
| 112 | ;; |
| 113 | |
| 114 | ;; Credit for suggestions, brainstorming, patches and bug-fixes: |
| 115 | ;; ACorreir@pervasive-sw.com (Alfred Correira) |
| 116 | |
| 117 | ;;; Change log: |
| 118 | ;; $Log: generic.el,v $ |
| 119 | ;; Revision 1.1 1997/06/15 07:01:26 rms |
| 120 | ;; Initial revision |
| 121 | ;; |
| 122 | ;; Revision 1.6 1996/11/01 17:27:47 peter |
| 123 | ;; Changed the function generic-function-name to return a string instead |
| 124 | ;; of a symbol. Generic-mode now uses this for the mode's name |
| 125 | ;; |
| 126 | ;; Revision 1.5 1996/11/01 16:45:20 peter |
| 127 | ;; Added GPL and LCD information. |
| 128 | ;; Updated documentation |
| 129 | ;; Added generic-find-file-regexp variable |
| 130 | ;; Added generic-make-keywords-list function |
| 131 | ;; |
| 132 | ;; Revision 1.4 1996/10/19 12:16:59 peter |
| 133 | ;; Small bug fixes: fontlock -> font-lock |
| 134 | ;; New entries are added to the end of auto-mode-alist |
| 135 | ;; Generic-font-lock-defaults are set to nil, not (list nil) |
| 136 | ;; Comment-regexp in generic-mode-find-file-hook changed to allow optional |
| 137 | ;; blank lines |
| 138 | ;; |
| 139 | ;; Revision 1.3 1996/10/17 08:24:25 peter |
| 140 | ;; Added generic-mode-find-file-hook and associated variables |
| 141 | ;; |
| 142 | ;; Revision 1.2 1996/10/17 01:00:45 peter |
| 143 | ;; Moved from a data-centered approach (generic-mode-alist) to |
| 144 | ;; a function-based one (define-generic-mode) |
| 145 | ;; |
| 146 | ;; Revision 1.1 1996/10/10 11:37:36 peter |
| 147 | ;; Initial revision |
| 148 | ;; |
| 149 | |
| 150 | ;;; Code: |
| 151 | |
| 152 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 153 | ;; Variables |
| 154 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 155 | |
| 156 | (make-variable-buffer-local |
| 157 | (defvar generic-font-lock-defaults nil |
| 158 | "Global defaults for font-lock in a generic mode.")) |
| 159 | |
| 160 | (make-variable-buffer-local |
| 161 | (defvar generic-mode-name 'default-generic-mode |
| 162 | "The name of the generic mode. |
| 163 | This is the car of one of the items in `generic-mode-alist'. |
| 164 | This variable is buffer-local.")) |
| 165 | |
| 166 | (make-variable-buffer-local |
| 167 | (defvar generic-comment-list nil |
| 168 | "List of comment characters for a generic mode.")) |
| 169 | |
| 170 | (make-variable-buffer-local |
| 171 | (defvar generic-keywords-list nil |
| 172 | "List of keywords for a generic mode.")) |
| 173 | |
| 174 | (make-variable-buffer-local |
| 175 | (defvar generic-font-lock-expressions nil |
| 176 | "List of font-lock expressions for a generic mode.")) |
| 177 | |
| 178 | (make-variable-buffer-local |
| 179 | (defvar generic-mode-function-list nil |
| 180 | "List of customization functions to call for a generic mode.")) |
| 181 | |
| 182 | (make-variable-buffer-local |
| 183 | (defvar generic-mode-syntax-table nil |
| 184 | "Syntax table for use in a generic mode.")) |
| 185 | |
| 186 | (defvar generic-mode-alist nil |
| 187 | "An association list for generic-mode. |
| 188 | Each entry in the list looks like this: |
| 189 | |
| 190 | NAME COMMENT-LIST KEYWORD-LIST FONT-LOCK-LIST AUTO-MODE-LIST FUNCTION-LIST. |
| 191 | |
| 192 | Do not add entries to this list directly; use `define-generic-mode' |
| 193 | instead (which see).") |
| 194 | |
| 195 | (defvar generic-use-find-file-hook t |
| 196 | "*If non-nil, add a hook to enter default-generic-mode automatically |
| 197 | if the first few lines of a file in fundamental mode start with a hash |
| 198 | comment character.") |
| 199 | |
| 200 | (defvar generic-lines-to-scan 3 |
| 201 | "*Number of lines that `generic-mode-find-file-hook' looks at |
| 202 | when deciding whether to enter generic-mode automatically. |
| 203 | This variable should be set to a small positive number.") |
| 204 | |
| 205 | (defvar generic-find-file-regexp "#.*\n\\(.*\n\\)?" |
| 206 | "*Regular expression used by `generic-mode-find-file-hook' |
| 207 | to determine if files in fundamental mode should be put into |
| 208 | `default-generic-mode' instead.") |
| 209 | |
| 210 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 211 | ;; Inline functions |
| 212 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 213 | |
| 214 | (defsubst generic-read-type () |
| 215 | (completing-read |
| 216 | "Generic Type: " |
| 217 | (mapcar |
| 218 | '(lambda (elt) (list (symbol-name (car elt)))) |
| 219 | generic-mode-alist) nil t)) |
| 220 | |
| 221 | ;; Basic sanity checks. It does *not* check whether the elements of the lists |
| 222 | ;; are of the correct type. |
| 223 | (defsubst generic-mode-sanity-check (name comment-list keyword-list |
| 224 | font-lock-list auto-mode-list |
| 225 | function-list &optional description) |
| 226 | (if (not (symbolp name)) |
| 227 | (error "%s is not a symbol" (princ name))) |
| 228 | |
| 229 | (mapcar '(lambda (elt) |
| 230 | (if (not (listp elt)) |
| 231 | (error "%s is not a list" (princ elt)))) |
| 232 | (list comment-list keyword-list font-lock-list |
| 233 | auto-mode-list function-list)) |
| 234 | |
| 235 | (if (not (or (null description) (stringp description))) |
| 236 | (error "Description must be a string or nil")) |
| 237 | ) |
| 238 | |
| 239 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 240 | ;; Functions |
| 241 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 242 | |
| 243 | ;;;### autoload |
| 244 | (defun define-generic-mode (name comment-list keyword-list font-lock-list |
| 245 | auto-mode-list function-list |
| 246 | &optional description) |
| 247 | "Create a new generic mode with NAME. |
| 248 | NAME should be a symbol; its string representation is used as the function |
| 249 | name. If DESCRIPTION is provided, it is used as the docstring for the new |
| 250 | function. |
| 251 | |
| 252 | COMMENT-LIST is a list, whose entries are either a single character, |
| 253 | a one or two character string or a cons pair. If the entry is a character |
| 254 | or a one-character string, it is added to the mode's syntax table with |
| 255 | comment-start syntax. If the entry is a cons pair, the elements of the |
| 256 | pair are considered to be comment-start and comment-end respectively. |
| 257 | Note that Emacs has limitations regarding comment characters. |
| 258 | |
| 259 | KEYWORD-LIST is a list of keywords to highlight with `font-lock-keyword-face'. |
| 260 | Each keyword should be a string. |
| 261 | |
| 262 | FONT-LOCK-LIST is a list of additional expressions to highlight. Each entry |
| 263 | in the list should have the same form as an entry in `font-lock-defaults-alist' |
| 264 | |
| 265 | AUTO-MODE-LIST is a list of regular expressions to add to auto-mode-alist. |
| 266 | These regexps are added to auto-mode-alist as soon as `define-generic-mode' |
| 267 | is called; any old regexps with the same name are removed. To modify the |
| 268 | auto-mode-alist expressions, use `alter-generic-mode-auto-mode' (which see). |
| 269 | |
| 270 | FUNCTION-LIST is a list of functions to call to do some additional setup. |
| 271 | |
| 272 | See the file generic-extras.el for some examples of `define-generic-mode'." |
| 273 | |
| 274 | ;; Basic sanity check |
| 275 | (generic-mode-sanity-check name |
| 276 | comment-list keyword-list font-lock-list |
| 277 | auto-mode-list function-list description) |
| 278 | |
| 279 | ;; Remove any old entry |
| 280 | (setq generic-mode-alist |
| 281 | (delq (assq name generic-mode-alist) |
| 282 | generic-mode-alist)) |
| 283 | |
| 284 | ;; Add a new entry |
| 285 | (setq generic-mode-alist |
| 286 | (append |
| 287 | (list |
| 288 | (list |
| 289 | name comment-list keyword-list font-lock-list |
| 290 | auto-mode-list function-list |
| 291 | )) |
| 292 | generic-mode-alist)) |
| 293 | |
| 294 | ;; Add it to auto-mode-alist |
| 295 | (generic-add-to-auto-mode name auto-mode-list t) |
| 296 | |
| 297 | ;; Define a function for it |
| 298 | (generic-create-generic-function name description) |
| 299 | ) |
| 300 | |
| 301 | (defun generic-add-to-auto-mode (mode auto-mode-list |
| 302 | &optional remove-old prepend) |
| 303 | "Add the entries for mode to `auto-mode-alist'. |
| 304 | If remove-old is non-nil, removes old entries first. If prepend is |
| 305 | non-nil, prepends entries to auto-mode-alist; otherwise, appends them." |
| 306 | |
| 307 | (if (not (listp auto-mode-list)) |
| 308 | (error "%s is not a list" (princ auto-mode-list))) |
| 309 | |
| 310 | (let ((new-mode (intern (symbol-name mode)))) |
| 311 | (if remove-old |
| 312 | (let ((auto-mode-entry)) |
| 313 | (while (setq auto-mode-entry (rassq new-mode auto-mode-alist)) |
| 314 | (setq auto-mode-alist |
| 315 | (delq auto-mode-entry |
| 316 | auto-mode-alist))))) |
| 317 | |
| 318 | (mapcar '(lambda (entry) |
| 319 | (generic-add-auto-mode-entry new-mode entry prepend)) |
| 320 | auto-mode-list))) |
| 321 | |
| 322 | (defun generic-add-auto-mode-entry (name entry &optional prepend) |
| 323 | "Add a new entry to the end of auto-mode-alist. |
| 324 | If prepend is non-nil, add the entry to the front of the list." |
| 325 | (let ((new-entry (list (cons entry name)))) |
| 326 | (setq auto-mode-alist |
| 327 | (if prepend |
| 328 | (append new-entry auto-mode-alist) |
| 329 | (append auto-mode-alist new-entry))))) |
| 330 | |
| 331 | (defun generic-create-generic-function (name &optional description) |
| 332 | "Create a generic mode function with NAME. |
| 333 | If DESCRIPTION is provided, it is used as the docstring." |
| 334 | (let ((symname (symbol-name name))) |
| 335 | (fset (intern symname) |
| 336 | (list 'lambda nil |
| 337 | (or description |
| 338 | (concat "Generic mode for type " symname)) |
| 339 | (list 'interactive) |
| 340 | (list 'generic-mode-with-type (list 'quote name)))))) |
| 341 | |
| 342 | (defun generic-mode-with-type (&optional mode) |
| 343 | "Go into the generic-mode MODE." |
| 344 | (let* ((type (or mode generic-mode-name)) |
| 345 | (generic-mode-list (assoc type generic-mode-alist)) |
| 346 | ) |
| 347 | |
| 348 | (if (not generic-mode-list) |
| 349 | (error "Can't find generic-mode information for type %s" |
| 350 | (princ generic-mode-name))) |
| 351 | |
| 352 | ;; Put this after the point where we read generic-mode-name! |
| 353 | (kill-all-local-variables) |
| 354 | |
| 355 | (setq |
| 356 | generic-mode-name type |
| 357 | generic-comment-list (nth 1 generic-mode-list) |
| 358 | generic-keywords-list (nth 2 generic-mode-list) |
| 359 | generic-font-lock-expressions (nth 3 generic-mode-list) |
| 360 | generic-mode-function-list (nth 5 generic-mode-list) |
| 361 | major-mode 'generic-mode |
| 362 | mode-name (symbol-name type) |
| 363 | ) |
| 364 | |
| 365 | (generic-mode-set-comments generic-comment-list) |
| 366 | |
| 367 | ;; Font-lock functionality |
| 368 | ;; Font-lock-defaults are always set even if there are no keywords |
| 369 | ;; or font-lock expressions, so comments can be highlighted. |
| 370 | (setq generic-font-lock-defaults nil) |
| 371 | (generic-mode-set-font-lock generic-keywords-list |
| 372 | generic-font-lock-expressions) |
| 373 | (make-local-variable 'font-lock-defaults) |
| 374 | (setq font-lock-defaults (list 'generic-font-lock-defaults nil)) |
| 375 | |
| 376 | ;; Call a list of functions |
| 377 | (if generic-mode-function-list |
| 378 | (mapcar 'funcall generic-mode-function-list)) |
| 379 | ) |
| 380 | ) |
| 381 | |
| 382 | ;;;###autoload |
| 383 | (defun generic-mode (type) |
| 384 | "A mode to do basic comment and font-lock functionality |
| 385 | for files which are too small to warrant their own mode, but have |
| 386 | comment characters, keywords, and the like. |
| 387 | |
| 388 | To define a generic-mode, use the function `define-generic-mode'. |
| 389 | To alter an existing generic-mode, use the `alter-generic-mode-' |
| 390 | convenience functions. |
| 391 | Some generic modes are defined in generic-extras.el" |
| 392 | (interactive |
| 393 | (list (generic-read-type))) |
| 394 | (generic-mode-with-type (intern type))) |
| 395 | |
| 396 | ;;; Comment Functionality |
| 397 | (defun generic-mode-set-comments (comment-list) |
| 398 | "Set up comment functionality for generic mode." |
| 399 | (if (null comment-list) |
| 400 | nil |
| 401 | (let ((generic-mode-syntax-table (make-syntax-table))) |
| 402 | (make-local-variable 'comment-start) |
| 403 | (make-local-variable 'comment-start-skip) |
| 404 | (make-local-variable 'comment-end) |
| 405 | (mapcar 'generic-mode-set-a-comment comment-list) |
| 406 | (set-syntax-table generic-mode-syntax-table)))) |
| 407 | |
| 408 | (defun generic-mode-set-a-comment (comment) |
| 409 | (and (char-or-string-p comment) |
| 410 | (if (stringp comment) |
| 411 | (cond |
| 412 | ((eq (length comment) 1) |
| 413 | (generic-mode-set-comment-char |
| 414 | (string-to-char comment))) |
| 415 | ((eq (length comment) 2) |
| 416 | (generic-mode-set-comment-string comment)) |
| 417 | (t |
| 418 | (error "Character string %s must be one or two characters long" |
| 419 | comment)) |
| 420 | ) |
| 421 | (generic-mode-set-comment-char comment))) |
| 422 | (if (consp comment) |
| 423 | (generic-mode-set-comment-pair comment))) |
| 424 | |
| 425 | (defun generic-mode-set-comment-char (comment-char) |
| 426 | "Set the given character as a comment character for generic mode." |
| 427 | (if (not comment-char) |
| 428 | nil |
| 429 | (setq |
| 430 | comment-end "" |
| 431 | comment-start (char-to-string comment-char) |
| 432 | comment-start-skip (concat comment-start "+ *") |
| 433 | ) |
| 434 | |
| 435 | (modify-syntax-entry comment-char "<" |
| 436 | generic-mode-syntax-table) |
| 437 | (modify-syntax-entry ?\n ">" |
| 438 | generic-mode-syntax-table))) |
| 439 | |
| 440 | (defun generic-mode-set-comment-string (comment-string) |
| 441 | "Set the given string as a comment string for generic mode." |
| 442 | (if (not comment-string) |
| 443 | nil |
| 444 | (setq |
| 445 | comment-end "" |
| 446 | comment-start comment-string |
| 447 | comment-start-skip (concat comment-start " *") |
| 448 | ) |
| 449 | |
| 450 | (let ((first (elt comment-string 0)) |
| 451 | (second (elt comment-string 1))) |
| 452 | ;; C++ style comments |
| 453 | (if (char-equal first second) |
| 454 | (progn |
| 455 | (modify-syntax-entry first "<12b" |
| 456 | generic-mode-syntax-table) |
| 457 | (modify-syntax-entry ?\n ">b" |
| 458 | generic-mode-syntax-table))) |
| 459 | ;; Some other two character string |
| 460 | (modify-syntax-entry first "<1" |
| 461 | generic-mode-syntax-table) |
| 462 | (modify-syntax-entry second "<2" |
| 463 | generic-mode-syntax-table) |
| 464 | (modify-syntax-entry ?\n ">" |
| 465 | generic-mode-syntax-table)))) |
| 466 | |
| 467 | (defun generic-mode-set-comment-pair (comment-pair) |
| 468 | "Set the given comment pair as a comment start and end for generic mode." |
| 469 | (let ((generic-comment-start (car comment-pair)) |
| 470 | (generic-comment-end (cdr comment-pair)) |
| 471 | ) |
| 472 | (setq |
| 473 | comment-end generic-comment-end |
| 474 | comment-start generic-comment-start |
| 475 | comment-start-skip (concat generic-comment-start " *") |
| 476 | ) |
| 477 | |
| 478 | ;; Sanity checks |
| 479 | (if (not (and (stringp generic-comment-start) |
| 480 | (stringp generic-comment-end))) |
| 481 | (error "Elements of cons pair must be strings")) |
| 482 | (if (not (and (equal (length generic-comment-start) 2) |
| 483 | (equal (length generic-comment-end) 2))) |
| 484 | (error "Start and end must be exactly two characters long")) |
| 485 | |
| 486 | (let ((first (elt generic-comment-start 0)) |
| 487 | (second (elt generic-comment-start 1)) |
| 488 | (third (elt generic-comment-end 0)) |
| 489 | (fourth (elt generic-comment-end 1)) |
| 490 | ) |
| 491 | |
| 492 | (modify-syntax-entry first ". 1" generic-mode-syntax-table) |
| 493 | (modify-syntax-entry second ". 2" generic-mode-syntax-table) |
| 494 | |
| 495 | (modify-syntax-entry |
| 496 | third |
| 497 | (concat |
| 498 | "." |
| 499 | (cond |
| 500 | ((char-equal first third) " 13") |
| 501 | ((char-equal second third) " 23") |
| 502 | (t " 3")) |
| 503 | ) |
| 504 | generic-mode-syntax-table) |
| 505 | |
| 506 | (modify-syntax-entry |
| 507 | fourth |
| 508 | (concat |
| 509 | "." |
| 510 | (cond |
| 511 | ((char-equal first fourth) " 14") |
| 512 | ((char-equal second fourth) " 24") |
| 513 | (t " 4")) |
| 514 | ) |
| 515 | generic-mode-syntax-table) |
| 516 | ))) |
| 517 | |
| 518 | (defun generic-mode-set-font-lock (keywords font-lock-expressions) |
| 519 | "Set up font-lock functionality for generic mode." |
| 520 | (let ((generic-font-lock-expressions)) |
| 521 | ;; Keywords |
| 522 | (if keywords |
| 523 | (setq |
| 524 | generic-font-lock-expressions |
| 525 | (append |
| 526 | (list |
| 527 | (list |
| 528 | (concat |
| 529 | "\\(\\<" |
| 530 | (mapconcat 'identity keywords "\\>\\|\\<") |
| 531 | "\\>\\)") |
| 532 | 1 'font-lock-keyword-face)) |
| 533 | generic-font-lock-expressions))) |
| 534 | ;; Other font-lock expressions |
| 535 | (if font-lock-expressions |
| 536 | (setq generic-font-lock-expressions |
| 537 | (append |
| 538 | font-lock-expressions |
| 539 | generic-font-lock-expressions))) |
| 540 | (if (not (or font-lock-expressions keywords)) |
| 541 | nil |
| 542 | (setq generic-font-lock-defaults generic-font-lock-expressions)) |
| 543 | )) |
| 544 | |
| 545 | ;; Support for [KEYWORD] constructs found in INF, INI and Samba files |
| 546 | (defun generic-bracket-support () |
| 547 | (setq imenu-generic-expression |
| 548 | '((nil "^\\[\\(.*\\)\\]" 1)) |
| 549 | imenu-case-fold-search t)) |
| 550 | |
| 551 | ;; This generic mode is always defined |
| 552 | (define-generic-mode 'default-generic-mode (list ?#) nil nil nil nil) |
| 553 | |
| 554 | ;; A more general solution would allow us to enter generic-mode for |
| 555 | ;; *any* comment character, but would require us to synthesize a new |
| 556 | ;; generic-mode on the fly. I think this gives us most of what we |
| 557 | ;; want. |
| 558 | (defun generic-mode-find-file-hook () |
| 559 | "Hook to enter default-generic-mode automatically |
| 560 | if the first few lines of a file in fundamental-mode start with a hash |
| 561 | comment character. This hook will be installed if the variable |
| 562 | `generic-use-find-file-hook' is non-nil. The variable `generic-lines-to-scan' |
| 563 | determines the number of lines to look at." |
| 564 | (if (not (eq major-mode 'fundamental-mode)) |
| 565 | nil |
| 566 | (if (or (> 1 generic-lines-to-scan) |
| 567 | (< 50 generic-lines-to-scan)) |
| 568 | (error "Variable `generic-lines-to-scan' should be set to a small" |
| 569 | " positive number")) |
| 570 | (let ((comment-regexp "") |
| 571 | (count 0) |
| 572 | ) |
| 573 | (while (< count generic-lines-to-scan) |
| 574 | (setq comment-regexp (concat comment-regexp |
| 575 | generic-find-file-regexp)) |
| 576 | (setq count (1+ count))) |
| 577 | (save-excursion |
| 578 | (goto-char (point-min)) |
| 579 | (if (looking-at comment-regexp) |
| 580 | (generic-mode-with-type 'default-generic-mode)))))) |
| 581 | |
| 582 | (defun generic-mode-ini-file-find-file-hook () |
| 583 | "Hook to enter default-generic-mode automatically |
| 584 | if the first few lines of a file in fundamental-mode look like an INI file. |
| 585 | This hook is NOT installed by default." |
| 586 | (if (not (eq major-mode 'fundamental-mode)) |
| 587 | nil |
| 588 | (save-excursion |
| 589 | (goto-char (point-min)) |
| 590 | (if (looking-at "^\\s-*\\[.*\\]") |
| 591 | (generic-mode-with-type 'ini-generic-mode))))) |
| 592 | |
| 593 | (and generic-use-find-file-hook |
| 594 | (add-hook 'find-file-hooks 'generic-mode-find-file-hook)) |
| 595 | |
| 596 | (defun generic-make-keywords-list (keywords-list face &optional prefix suffix) |
| 597 | "Return a regular expression matching the specified keywords. |
| 598 | The regexp is highlighted with FACE." |
| 599 | ;; Sanity checks |
| 600 | ;; Don't check here; face may not be defined yet |
| 601 | ;; (if (not (facep face)) |
| 602 | ;; (error "Face %s is not defined" (princ face))) |
| 603 | (if (not (listp keywords-list)) |
| 604 | (error "Keywords argument must be a list of strings")) |
| 605 | (list |
| 606 | (concat |
| 607 | (or prefix "") |
| 608 | "\\(\\<" |
| 609 | (mapconcat 'identity keywords-list "\\>\\|\\<") |
| 610 | "\\>\\)" |
| 611 | (or suffix "") |
| 612 | ) 1 face)) |
| 613 | |
| 614 | (provide 'generic-mode) |
| 615 | |
| 616 | ;;; generic-mode.el ends here |