Revision: miles@gnu.org--gnu-2005/gnus--devo--0--patch-182
[gnus] / lisp / sieve-mode.el
1 ;;; sieve-mode.el --- Sieve code editing commands for Emacs
2
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; This file contain editing mode functions and font-lock support for
27 ;; editing Sieve scripts.  It sets up C-mode with support for
28 ;; sieve-style #-comments and a lightly hacked syntax table.  It was
29 ;; strongly influenced by awk-mode.el.
30 ;;
31 ;; Put something similar to the following in your .emacs to use this file:
32 ;;
33 ;; (load "~/lisp/sieve")
34 ;; (setq auto-mode-alist (cons '("\\.siv\\'" . sieve-mode) auto-mode-alist))
35 ;;
36 ;; References:
37 ;;
38 ;; RFC 3028,
39 ;; "Sieve: A Mail Filtering Language",
40 ;; by Tim Showalter.
41 ;;
42 ;; Release history:
43 ;;
44 ;; 2001-03-02 version 1.0 posted to gnu.emacs.sources
45 ;;            version 1.1 change file extension into ".siv" (official one)
46 ;;                        added keymap and menubar to hook into sieve-manage
47 ;; 2001-10-31 version 1.2 committed to Oort Gnus
48
49 ;;; Code:
50
51 (autoload 'sieve-manage "sieve")
52 (autoload 'sieve-upload "sieve")
53 (require 'easymenu)
54 (eval-when-compile
55   (require 'font-lock))
56
57 (defgroup sieve nil
58   "Sieve."
59   :group 'languages)
60
61 (defcustom sieve-mode-hook nil
62   "Hook run in sieve mode buffers."
63   :group 'sieve
64   :type 'hook)
65
66 ;; Font-lock
67
68 (defvar sieve-control-commands-face 'sieve-control-commands
69   "Face name used for Sieve Control Commands.")
70
71 (defface sieve-control-commands
72   '((((type tty) (class color)) (:foreground "blue" :weight light))
73     (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
74     (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
75     (((class color) (background light)) (:foreground "Orchid"))
76     (((class color) (background dark)) (:foreground "LightSteelBlue"))
77     (t (:bold t)))
78   "Face used for Sieve Control Commands."
79   :group 'sieve)
80 ;; backward-compatibility alias
81 (put 'sieve-control-commands-face 'face-alias 'sieve-control-commands)
82
83 (defvar sieve-action-commands-face 'sieve-action-commands
84   "Face name used for Sieve Action Commands.")
85
86 (defface sieve-action-commands
87   '((((type tty) (class color)) (:foreground "blue" :weight bold))
88     (((class color) (background light)) (:foreground "Blue"))
89     (((class color) (background dark)) (:foreground "LightSkyBlue"))
90     (t (:inverse-video t :bold t)))
91   "Face used for Sieve Action Commands."
92   :group 'sieve)
93 ;; backward-compatibility alias
94 (put 'sieve-action-commands-face 'face-alias 'sieve-action-commands)
95
96 (defvar sieve-test-commands-face 'sieve-test-commands
97   "Face name used for Sieve Test Commands.")
98
99 (defface sieve-test-commands
100   '((((type tty) (class color)) (:foreground "magenta"))
101     (((class grayscale) (background light))
102      (:foreground "LightGray" :bold t :underline t))
103     (((class grayscale) (background dark))
104      (:foreground "Gray50" :bold t :underline t))
105     (((class color) (background light)) (:foreground "CadetBlue"))
106     (((class color) (background dark)) (:foreground "Aquamarine"))
107     (t (:bold t :underline t)))
108   "Face used for Sieve Test Commands."
109   :group 'sieve)
110 ;; backward-compatibility alias
111 (put 'sieve-test-commands-face 'face-alias 'sieve-test-commands)
112
113 (defvar sieve-tagged-arguments-face 'sieve-tagged-arguments
114   "Face name used for Sieve Tagged Arguments.")
115
116 (defface sieve-tagged-arguments
117   '((((type tty) (class color)) (:foreground "cyan" :weight bold))
118     (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
119     (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
120     (((class color) (background light)) (:foreground "Purple"))
121     (((class color) (background dark)) (:foreground "Cyan"))
122     (t (:bold t)))
123   "Face used for Sieve Tagged Arguments."
124   :group 'sieve)
125 ;; backward-compatibility alias
126 (put 'sieve-tagged-arguments-face 'face-alias 'sieve-tagged-arguments)
127
128
129 (defconst sieve-font-lock-keywords
130   (eval-when-compile
131     (list
132      ;; control commands
133      (cons (regexp-opt '("require" "if" "else" "elsif" "stop"))
134            'sieve-control-commands-face)
135      ;; action commands
136      (cons (regexp-opt '("fileinto" "redirect" "reject" "keep" "discard"))
137            'sieve-action-commands-face)
138      ;; test commands
139      (cons (regexp-opt '("address" "allof" "anyof" "exists" "false"
140                          "true" "header" "not" "size" "envelope"))
141            'sieve-test-commands-face)
142      (cons "\\Sw+:\\sw+"
143            'sieve-tagged-arguments-face))))
144
145 ;; Syntax table
146
147 (defvar sieve-mode-syntax-table nil
148   "Syntax table in use in sieve-mode buffers.")
149
150 (if sieve-mode-syntax-table
151     ()
152   (setq sieve-mode-syntax-table (make-syntax-table))
153   (modify-syntax-entry ?\\ "\\" sieve-mode-syntax-table)
154   (modify-syntax-entry ?\n ">   " sieve-mode-syntax-table)
155   (modify-syntax-entry ?\f ">   " sieve-mode-syntax-table)
156   (modify-syntax-entry ?\# "<   " sieve-mode-syntax-table)
157   (modify-syntax-entry ?/ "." sieve-mode-syntax-table)
158   (modify-syntax-entry ?* "." sieve-mode-syntax-table)
159   (modify-syntax-entry ?+ "." sieve-mode-syntax-table)
160   (modify-syntax-entry ?- "." sieve-mode-syntax-table)
161   (modify-syntax-entry ?= "." sieve-mode-syntax-table)
162   (modify-syntax-entry ?% "." sieve-mode-syntax-table)
163   (modify-syntax-entry ?< "." sieve-mode-syntax-table)
164   (modify-syntax-entry ?> "." sieve-mode-syntax-table)
165   (modify-syntax-entry ?& "." sieve-mode-syntax-table)
166   (modify-syntax-entry ?| "." sieve-mode-syntax-table)
167   (modify-syntax-entry ?_ "_" sieve-mode-syntax-table)
168   (modify-syntax-entry ?\' "\"" sieve-mode-syntax-table))
169
170 ;; Key map definition
171
172 (defvar sieve-mode-map
173   (let ((map (make-sparse-keymap)))
174     (define-key map "\C-c\C-l" 'sieve-upload)
175     (define-key map "\C-c\C-c" 'sieve-upload-and-bury)
176     (define-key map "\C-c\C-m" 'sieve-manage)
177     map)
178   "Key map used in sieve mode.")
179
180 ;; Menu definition
181
182 (defvar sieve-mode-menu nil
183   "Menubar used in sieve mode.")
184
185 ;; Code for Sieve editing mode.
186
187 ;;;###autoload
188 (define-derived-mode sieve-mode c-mode "Sieve"
189   "Major mode for editing Sieve code.
190 This is much like C mode except for the syntax of comments.  Its keymap
191 inherits from C mode's and it has the same variables for customizing
192 indentation.  It has its own abbrev table and its own syntax table.
193
194 Turning on Sieve mode runs `sieve-mode-hook'."
195   (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
196   (set (make-local-variable 'paragraph-separate) paragraph-start)
197   (set (make-local-variable 'comment-start) "#")
198   (set (make-local-variable 'comment-end) "")
199   ;;(set (make-local-variable 'comment-start-skip) "\\(^\\|\\s-\\);?#+ *")
200   (set (make-local-variable 'comment-start-skip) "#+ *")
201   (unless (featurep 'xemacs)
202     (set (make-local-variable 'font-lock-defaults)
203          '(sieve-font-lock-keywords nil nil ((?_ . "w")))))
204   (easy-menu-add-item nil nil sieve-mode-menu))
205
206 ;; Menu
207
208 (easy-menu-define sieve-mode-menu sieve-mode-map
209   "Sieve Menu."
210   '("Sieve"
211     ["Upload script" sieve-upload t]
212     ["Manage scripts on server" sieve-manage t]))
213
214 (provide 'sieve-mode)
215
216 ;;; arch-tag: 3b8ab76d-065d-4c52-b1e8-ab2ec21f2ace
217 ;; sieve-mode.el ends here