1 ;;; sieve-mode.el --- Sieve code editing commands for Emacs
3 ;; Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; This file contain editing mode functions and font-lock support for
25 ;; editing Sieve scripts. It sets up C-mode with support for
26 ;; sieve-style #-comments and a lightly hacked syntax table. It was
27 ;; strongly influenced by awk-mode.el.
29 ;; Put something similar to the following in your .emacs to use this file:
31 ;; (load "~/lisp/sieve")
32 ;; (setq auto-mode-alist (cons '("\\.siv\\'" . sieve-mode) auto-mode-alist))
37 ;; "Sieve: A Mail Filtering Language",
42 ;; 2001-03-02 version 1.0 posted to gnu.emacs.sources
43 ;; version 1.1 change file extension into ".siv" (official one)
44 ;; added keymap and menubar to hook into sieve-manage
45 ;; 2001-10-31 version 1.2 committed to Oort Gnus
49 (autoload 'sieve-manage "sieve")
50 (autoload 'sieve-upload "sieve")
58 (defcustom sieve-mode-hook nil
59 "Hook run in sieve mode buffers."
64 (defvar sieve-control-commands-face 'sieve-control-commands
65 "Face name used for Sieve Control Commands.")
67 (defface sieve-control-commands
68 '((((type tty) (class color)) (:foreground "blue" :weight light))
69 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
70 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
71 (((class color) (background light)) (:foreground "Orchid"))
72 (((class color) (background dark)) (:foreground "LightSteelBlue"))
74 "Face used for Sieve Control Commands.")
75 ;; backward-compatibility alias
76 (put 'sieve-control-commands-face 'face-alias 'sieve-control-commands)
77 (put 'sieve-control-commands-face 'obsolete-face "22.1")
79 (defvar sieve-action-commands-face 'sieve-action-commands
80 "Face name used for Sieve Action Commands.")
82 (defface sieve-action-commands
83 '((((type tty) (class color)) (:foreground "blue" :weight bold))
84 (((class color) (background light)) (:foreground "Blue"))
85 (((class color) (background dark)) (:foreground "LightSkyBlue"))
86 (t (:inverse-video t :bold t)))
87 "Face used for Sieve Action Commands.")
88 ;; backward-compatibility alias
89 (put 'sieve-action-commands-face 'face-alias 'sieve-action-commands)
90 (put 'sieve-action-commands-face 'obsolete-face "22.1")
92 (defvar sieve-test-commands-face 'sieve-test-commands
93 "Face name used for Sieve Test Commands.")
95 (defface sieve-test-commands
96 '((((type tty) (class color)) (:foreground "magenta"))
97 (((class grayscale) (background light))
98 (:foreground "LightGray" :bold t :underline t))
99 (((class grayscale) (background dark))
100 (:foreground "Gray50" :bold t :underline t))
101 (((class color) (background light)) (:foreground "CadetBlue"))
102 (((class color) (background dark)) (:foreground "Aquamarine"))
103 (t (:bold t :underline t)))
104 "Face used for Sieve Test Commands.")
105 ;; backward-compatibility alias
106 (put 'sieve-test-commands-face 'face-alias 'sieve-test-commands)
107 (put 'sieve-test-commands-face 'obsolete-face "22.1")
109 (defvar sieve-tagged-arguments-face 'sieve-tagged-arguments
110 "Face name used for Sieve Tagged Arguments.")
112 (defface sieve-tagged-arguments
113 '((((type tty) (class color)) (:foreground "cyan" :weight bold))
114 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
115 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
116 (((class color) (background light)) (:foreground "Purple"))
117 (((class color) (background dark)) (:foreground "Cyan"))
119 "Face used for Sieve Tagged Arguments.")
120 ;; backward-compatibility alias
121 (put 'sieve-tagged-arguments-face 'face-alias 'sieve-tagged-arguments)
122 (put 'sieve-tagged-arguments-face 'obsolete-face "22.1")
125 (defconst sieve-font-lock-keywords
129 (cons (regexp-opt '("require" "if" "else" "elsif" "stop")
131 'sieve-control-commands-face)
133 (cons (regexp-opt '("fileinto" "redirect" "reject" "keep" "discard")
135 'sieve-action-commands-face)
137 (cons (regexp-opt '("address" "allof" "anyof" "exists" "false"
138 "true" "header" "not" "size" "envelope"
141 'sieve-test-commands-face)
143 'sieve-tagged-arguments-face))))
147 (defvar sieve-mode-syntax-table
148 (let ((st (make-syntax-table)))
149 (modify-syntax-entry ?\\ "\\" st)
150 (modify-syntax-entry ?\n "> " st)
151 (modify-syntax-entry ?\f "> " st)
152 (modify-syntax-entry ?\# "< " st)
153 (modify-syntax-entry ?/ ". 14" st)
154 (modify-syntax-entry ?* ". 23b" st)
155 (modify-syntax-entry ?+ "." st)
156 (modify-syntax-entry ?- "." st)
157 (modify-syntax-entry ?= "." st)
158 (modify-syntax-entry ?% "." st)
159 (modify-syntax-entry ?< "." st)
160 (modify-syntax-entry ?> "." st)
161 (modify-syntax-entry ?& "." st)
162 (modify-syntax-entry ?| "." st)
163 (modify-syntax-entry ?_ "_" st)
164 (modify-syntax-entry ?\' "\"" st)
166 "Syntax table in use in sieve-mode buffers.")
169 ;; Key map definition
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-kill)
175 (define-key map "\C-c\C-m" 'sieve-manage)
177 "Key map used in sieve mode.")
181 (easy-menu-define sieve-mode-menu sieve-mode-map
184 ["Upload script" sieve-upload t]
185 ["Manage scripts on server" sieve-manage t]))
187 ;; Code for Sieve editing mode.
190 (defun sieve-syntax-propertize (beg end)
192 (sieve-syntax-propertize-text end)
194 (syntax-propertize-rules
195 ;; FIXME: When there's a "text:" with a # comment, the \n plays dual role:
196 ;; it closes the comment and starts the string. This is problematic for us
197 ;; since syntax-table entries can either close a comment or
198 ;; delimit a string, but not both.
199 ("\\_<text:[ \t]*\\(?:#.*\\(.\\)\\)?\\(\n\\)"
201 (2 (prog1 (unless (save-excursion
202 (nth 8 (syntax-ppss (match-beginning 0))))
203 (string-to-syntax "|"))
204 (sieve-syntax-propertize-text end)))))
207 (defun sieve-syntax-propertize-text (end)
208 (let ((ppss (syntax-ppss)))
209 (when (and (eq t (nth 3 ppss))
210 (re-search-forward "^\\.\\(\n\\)" end 'move))
211 (put-text-property (match-beginning 1) (match-end 1)
212 'syntax-table (string-to-syntax "|")))))
215 (define-derived-mode sieve-mode c-mode "Sieve"
216 "Major mode for editing Sieve code.
217 This is much like C mode except for the syntax of comments. Its keymap
218 inherits from C mode's and it has the same variables for customizing
219 indentation. It has its own abbrev table and its own syntax table.
221 Turning on Sieve mode runs `sieve-mode-hook'."
222 (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
223 (set (make-local-variable 'paragraph-separate) paragraph-start)
224 (set (make-local-variable 'comment-start) "#")
225 (set (make-local-variable 'comment-end) "")
226 ;;(set (make-local-variable 'comment-start-skip) "\\(^\\|\\s-\\);?#+ *")
227 (set (make-local-variable 'comment-start-skip) "#+ *")
228 (set (make-local-variable 'syntax-propertize-function)
229 #'sieve-syntax-propertize)
230 (set (make-local-variable 'font-lock-defaults)
231 '(sieve-font-lock-keywords nil nil ((?_ . "w"))))
232 (easy-menu-add-item nil nil sieve-mode-menu))
234 (provide 'sieve-mode)
236 ;; sieve-mode.el ends here