]> code.delx.au - gnu-emacs/blobdiff - lisp/generic-x.el
*** empty log message ***
[gnu-emacs] / lisp / generic-x.el
index 019456aae6bd042d7d123e88d95386421fdaa9b3..a13103edb3d701eefb6dac6e4f5fc8607ae72750 100644 (file)
 ;; You can also send in new modes; if the file types a reasonably common,
 ;; we would like to install them.
 ;;
+;; DEFAULT GENERIC MODE:
+;;
+;; This file provides a hook which automatically puts a file into
+;; `default-generic-mode' if the first few lines of a file in
+;; fundamental mode start with a hash comment character.  To disable
+;; this functionality, set the variable `generic-use-find-file-hook'
+;; to nil BEFORE loading generic-x.  See the variables
+;; `generic-lines-to-scan' and `generic-find-file-regexp' for
+;; customization options.
+;;
 ;; PROBLEMS WHEN USED WITH FOLDING MODE:
 ;;
 ;; [The following relates to the obsolete selective-display technique.
 (require 'font-lock)
 
 (defgroup generic-x nil
-  "Extra modes for generic mode."
+  "A collection of generic modes."
   :prefix "generic-"
-  :group 'generic
+  :group 'data
   :version "20.3")
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Default-Generic mode
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defcustom generic-use-find-file-hook t
+  "*If non-nil, add a hook to enter `default-generic-mode' automatically.
+This is done if the first few lines of a file in fundamental mode
+start with a hash comment character."
+  :group 'generic-x
+  :type  'boolean)
+
+(defcustom generic-lines-to-scan 3
+  "*Number of lines that `generic-mode-find-file-hook' looks at.
+Relevant when deciding whether to enter Default-Generic mode automatically.
+This variable should be set to a small positive number."
+  :group 'generic-x
+  :type  'integer)
+
+(defcustom generic-find-file-regexp "^#"
+  "*Regular expression used by `generic-mode-find-file-hook'.
+Files in fundamental mode whose first few lines contain a match
+for this regexp, should be put into Default-Generic mode instead.
+The number of lines tested for the matches is specified by the
+value of the variable `generic-lines-to-scan', which see."
+  :group 'generic-x
+  :type  'regexp)
+
+(defcustom generic-ignore-files-regexp "[Tt][Aa][Gg][Ss]\\'"
+  "*Regular expression used by `generic-mode-find-file-hook'.
+Files whose names match this regular expression should not be put
+into Default-Generic mode, even if they have lines which match
+the regexp in `generic-find-file-regexp'.  If the value is nil,
+`generic-mode-find-file-hook' does not check the file names."
+  :group 'generic-x
+  :type  '(choice (const :tag "Don't check file names" nil) regexp))
+
+;; This generic mode is always defined
+(define-generic-mode default-generic-mode (list ?#) nil nil nil nil :group 'generic)
+
+;; A more general solution would allow us to enter generic-mode for
+;; *any* comment character, but would require us to synthesize a new
+;; generic-mode on the fly. I think this gives us most of what we
+;; want.
+(defun generic-mode-find-file-hook ()
+  "Hook function to enter Default-Generic mode automatically.
+
+Done if the first few lines of a file in Fundamental mode start
+with a match for the regexp in `generic-find-file-regexp', unless
+the file's name matches the regexp which is the value of the
+variable `generic-ignore-files-regexp'.
+
+This hook will be installed if the variable
+`generic-use-find-file-hook' is non-nil.  The variable
+`generic-lines-to-scan' determines the number of lines to look at."
+  (when (and (eq major-mode 'fundamental-mode)
+            (or (null generic-ignore-files-regexp)
+                (not (string-match
+                      generic-ignore-files-regexp
+                      (file-name-sans-versions buffer-file-name)))))
+    (save-excursion
+      (goto-char (point-min))
+      (when (re-search-forward generic-find-file-regexp
+                              (save-excursion
+                                (forward-line generic-lines-to-scan)
+                                (point)) t)
+       (goto-char (point-min))
+       (default-generic-mode)))))
+
+(and generic-use-find-file-hook
+    (add-hook 'find-file-hook 'generic-mode-find-file-hook))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Other Generic modes
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defcustom generic-extras-enable-list nil
   "*List of generic modes to enable by default.
 Each entry in the list should be a symbol.  The variables
@@ -150,10 +235,6 @@ generic-x to enable the specified modes."
                     etc-fstab-generic-mode)
                   generic-extras-enable-list)))
 
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Generic-modes
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 ;;; Apache
 (when (memq 'apache-conf-generic-mode generic-extras-enable-list)
 
@@ -284,8 +365,21 @@ generic-x to enable the specified modes."
       (setq imenu-generic-expression
            '((nil "^\\[\\(.*\\)\\]" 1)
              ("*Variables*" "^\\s-*\\([^=]+\\)\\s-*=" 1))))))
-  "Generic mode for MS-Windows INI files."
-  :group 'generic-x))
+  "Generic mode for MS-Windows INI files.
+You can use `ini-generic-mode-find-file-hook' to enter this mode
+automatically for INI files whose names do not end in \".ini\"."
+  :group 'generic-x)
+
+(defun ini-generic-mode-find-file-hook ()
+  "Hook function to enter Ini-Generic mode automatically for INI files.
+Done if the first few lines of a file in Fundamental mode look
+like an INI file.  You can add this hook to `find-file-hook'."
+  (and (eq major-mode 'fundamental-mode)
+       (save-excursion
+        (goto-char (point-min))
+        (and (looking-at "^\\s-*\\[.*\\]")
+             (ini-generic-mode)))))
+(defalias 'generic-mode-ini-file-find-file-hook 'ini-generic-mode-find-file-hook))
 
 ;;; Windows REG files
 ;;; Unfortunately, Windows 95 and Windows NT have different REG file syntax!
@@ -396,6 +490,7 @@ generic-x to enable the specified modes."
     (compile
      (concat (w32-shell-name) " -c " (buffer-file-name)))))
 
+(eval-when-compile (require 'comint))
 (defun bat-generic-mode-run-as-comint ()
   "Run the current BAT file in a comint buffer."
   (interactive)
@@ -785,7 +880,7 @@ generic-x to enable the specified modes."
      '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
        (1 font-lock-constant-face)
        (2 font-lock-variable-name-face nil t))))
-    '("\\.[rR][cC]$")
+    '("\\.[rR][cC]\\'")
     nil
     "Generic mode for MS-Windows Resource files."
     :group 'generic-x))
@@ -1406,7 +1501,7 @@ generic-x to enable the specified modes."
      (generic-make-keywords-list
       installshield-funarg-constants-list
       font-lock-variable-name-face "[^_]" "[^_]"))) ; is this face the best choice?
-  '("\\.[rR][uU][lL]$")
+  '("\\.[rR][uU][lL]\\'")
   '(generic-rul-mode-setup-function)
   "Generic mode for InstallShield RUL files."
   :group 'generic-x)
@@ -1584,8 +1679,8 @@ generic-x to enable the specified modes."
     "auto"
     "ignore")
   '(("^\\([/-A-Za-z0-9_]+\\)\\s-+\\([/-A-Za-z0-9_]+\\)"
-     (1 font-lock-type-face)
-     (2 font-lock-variable-name-face)))
+     (1 font-lock-type-face t)
+     (2 font-lock-variable-name-face t)))
   '("/etc/[v]*fstab\\'")
   (list
    (function
@@ -1612,20 +1707,20 @@ generic-x to enable the specified modes."
 (defface show-tabs-tab-face
   '((((class grayscale) (background light)) (:background "DimGray"   :weight bold))
     (((class grayscale) (background dark))  (:background "LightGray" :weight bold))
-    (((class color)     (background light)) (:background "red"))
-    (((class color)     (background dark))  (:background "red"))
+    (((class color)     (min-colors 88))    (:background "red1"))
+    (((class color))                        (:background "red"))
     (t (:weight bold)))
   "Font Lock mode face used to highlight TABs."
-  :group 'show-tabs)
+  :group 'generic-x)
 
 (defface show-tabs-space-face
   '((((class grayscale) (background light)) (:background "DimGray"   :weight bold))
     (((class grayscale) (background dark))  (:background "LightGray" :weight bold))
-    (((class color)     (background light)) (:background "yellow"))
-    (((class color)     (background dark))  (:background "yellow"))
+    (((class color)     (min-colors 88))    (:background "yellow1"))
+    (((class color))                        (:background "yellow"))
     (t (:weight bold)))
   "Font Lock mode face used to highlight spaces."
-  :group 'show-tabs)
+  :group 'generic-x)
 
 (define-generic-mode show-tabs-generic-mode
   nil ;; no comment char
@@ -1667,7 +1762,7 @@ generic-x to enable the specified modes."
   ;; List of additional font-lock-expressions
   '(("\\([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\\)" 1 font-lock-constant-face)
     ("^\\([.A-Za-z0-9]+\\)"                 1 font-lock-variable-name-face))
-  ;; List of additional automode-alist expressions
+  ;; List of additional auto-mode-alist expressions
   nil
   ;; List of set up functions to call
   nil
@@ -1688,7 +1783,7 @@ generic-x to enable the specified modes."
   '("nameserver" "domain" "search" "sortlist" "options")
   ;; List of additional font-lock-expressions
   nil
-  ;; List of additional automode-alist expressions
+  ;; List of additional auto-mode-alist expressions
   '("/etc/resolv[e]?.conf\\'")
   ;; List of set up functions to call
   nil