]> code.delx.au - gnu-emacs/blob - lisp/net/sieve-mode.el
; Merge from origin/emacs-25
[gnu-emacs] / lisp / net / sieve-mode.el
1 ;;; sieve-mode.el --- Sieve code editing commands for Emacs
2
3 ;; Copyright (C) 2001-2016 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 3 of the License, or
12 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
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.
28 ;;
29 ;; Put something similar to the following in your .emacs to use this file:
30 ;;
31 ;; (load "~/lisp/sieve")
32 ;; (setq auto-mode-alist (cons '("\\.siv\\'" . sieve-mode) auto-mode-alist))
33 ;;
34 ;; References:
35 ;;
36 ;; RFC 3028,
37 ;; "Sieve: A Mail Filtering Language",
38 ;; by Tim Showalter.
39 ;;
40 ;; Release history:
41 ;;
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
46
47 ;;; Code:
48
49 (autoload 'sieve-manage "sieve")
50 (autoload 'sieve-upload "sieve")
51 (eval-when-compile
52 (require 'font-lock))
53
54 (defgroup sieve nil
55 "Sieve."
56 :group 'languages)
57
58 (defcustom sieve-mode-hook nil
59 "Hook run in sieve mode buffers."
60 :type 'hook)
61
62 ;; Font-lock
63
64 (defvar sieve-control-commands-face 'sieve-control-commands
65 "Face name used for Sieve Control Commands.")
66
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"))
73 (t (:bold t)))
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")
78
79 (defvar sieve-action-commands-face 'sieve-action-commands
80 "Face name used for Sieve Action Commands.")
81
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")
91
92 (defvar sieve-test-commands-face 'sieve-test-commands
93 "Face name used for Sieve Test Commands.")
94
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")
108
109 (defvar sieve-tagged-arguments-face 'sieve-tagged-arguments
110 "Face name used for Sieve Tagged Arguments.")
111
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"))
118 (t (:bold t)))
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")
123
124
125 (defconst sieve-font-lock-keywords
126 (eval-when-compile
127 (list
128 ;; control commands
129 (cons (regexp-opt '("require" "if" "else" "elsif" "stop")
130 'words)
131 'sieve-control-commands-face)
132 ;; action commands
133 (cons (regexp-opt '("fileinto" "redirect" "reject" "keep" "discard")
134 'words)
135 'sieve-action-commands-face)
136 ;; test commands
137 (cons (regexp-opt '("address" "allof" "anyof" "exists" "false"
138 "true" "header" "not" "size" "envelope"
139 "body")
140 'words)
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
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)
165 st)
166 "Syntax table in use in sieve-mode buffers.")
167
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-kill)
175 (define-key map "\C-c\C-m" 'sieve-manage)
176 map)
177 "Key map used in sieve mode.")
178
179 ;; Menu
180
181 (easy-menu-define sieve-mode-menu sieve-mode-map
182 "Sieve Menu."
183 '("Sieve"
184 ["Upload script" sieve-upload t]
185 ["Manage scripts on server" sieve-manage t]))
186
187 ;; Code for Sieve editing mode.
188
189
190 (defun sieve-syntax-propertize (beg end)
191 (goto-char beg)
192 (sieve-syntax-propertize-text end)
193 (funcall
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\\)"
200 (1 ">")
201 (2 (prog1 (unless (save-excursion
202 (nth 8 (syntax-ppss (match-beginning 0))))
203 (string-to-syntax "|"))
204 (sieve-syntax-propertize-text end)))))
205 beg end))
206
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 "|")))))
213
214 ;;;###autoload
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.
220
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))
233
234 (provide 'sieve-mode)
235
236 ;; sieve-mode.el ends here