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