]> code.delx.au - gnu-emacs-elpa/blob - packages/wcheck-mode/README.md
Merge commit '37c46180280f10fa5120a017acd04f7022d124e4'
[gnu-emacs-elpa] / packages / wcheck-mode / README.md
1 Wcheck Mode
2 ===========
3
4 **General-purpose text-checker interface for Emacs text editor**
5
6
7 Introduction
8 ------------
9
10 Wcheck mode is a general-purpose text-checker interface for [Emacs][]
11 text editor. Wcheck mode is a minor mode which provides an on-the-fly
12 text checker. It checks the visible text area, as you type, and possibly
13 highlights some parts of it. What is checked and how are all
14 configurable.
15
16 Wcheck mode can use external programs or Emacs Lisp functions for
17 checking text. For example, Wcheck mode can be used with spell-checker
18 programs such as Ispell, Enchant and Hunspell, but actually any tool
19 that can receive text from standard input stream and send text to
20 standard output can be used. Wcheck mode sends parts of buffer's content
21 to an external program or an Emacs Lisp function and, based on their
22 output, decides if some parts of text should be marked in the buffer.
23
24 [Emacs]: http://www.gnu.org/software/emacs/
25
26
27 Features
28 --------
29
30 In Wcheck mode's configuration different configuration units are called
31 _languages_. In terms of a spelling checker it is natural to think of
32 them as different human languages. Wcheck mode is not limited to that,
33 though. Language is just a configuration unit for a specific text
34 checking purpose.
35
36 Each language can use its own checker engine (external program or a
37 function), command-line arguments and other settings, such as the
38 regular expressions and syntax table that are used to match words (or
39 other text elements) in Emacs buffer. User can choose which _face_ is
40 used to mark text elements in buffer.
41
42 User can create language-specific and major mode specific settings
43 defining which _faces_ to read or skip in buffers. A typical use for
44 this feature is to spell-check only those areas in buffer which are
45 written in the target language. For example, in email messages usually
46 the message body and Subject header are important enough to spell-check.
47 In programming modes user could spell-check only documentation strings
48 and comments (or the opposite if you want to use Wcheck mode to check
49 keywords and syntax of the programming language itself).
50
51 Wcheck mode can also be configured to offer any kind of actions for
52 marked text. Actions are presented to user through a menu which is
53 activated either by (1) clicking the right mouse button on a marked text
54 or (2) executing interactive command `wcheck-actions` while the cursor
55 (the point) is on a marked text.
56
57 If you use Wcheck mode as a spelling checker then it's natural to
58 configure an action menu that offers spelling suggestions for misspelled
59 words. The action menu could also have an option to add marked word to
60 spell-checker's dictionary, so that the word is recognized in the
61 future. That's only one application for Wcheck mode, though. Wcheck mode
62 can be configured to find almost any kind of text elements from buffer,
63 mark them, and offer any kind of actions for marked text.
64
65
66 How does it compare to other spell-checkers?
67 --------------------------------------------
68
69 The open design makes Wcheck mode (internally) quite different from
70 spell-checkers like [Flyspell][] mode and [Speck][] mode. They are
71 specific tools for spell-checking through Ispell or compatible program
72 and are therefore very much tied to Ispell's features and command-line
73 interface. This can be useful if you want to use Ispell or fully
74 compatible program for spell-checking natural languages. However, not
75 all human languages can be supported through Ispell and there can also
76 be other kind of text-checking needs.
77
78 The motivation behind Wcheck mode is to offer more general-purpose and
79 configurable interface for text checking. It can be configured to work
80 with almost anything: user's custom shell, Awk or Perl scripts, Lisp
81 functions or other checkers and text filters. Even if you only need a
82 spelling checker for human languages Wcheck mode can be a good choice.
83 It has more configuration possibilities than other spell-checkers and
84 the on-the-fly checker performs very well. It's a true real-time
85 checker.
86
87 [Flyspell]: http://www.emacswiki.org/emacs/FlySpell
88 [Speck]: http://www.emacswiki.org/SpeckMode
89
90
91 Install
92 -------
93
94 You can install Wcheck mode through [Marmalade][] or [Melpa][] package
95 archive. Alternatively you can put `wcheck-mode.el` file to some
96 directory in your Emacs's `load-path` and add the following lines to
97 Emacs's initialization file (`~/.emacs` or `~/.emacs.d/init.el`):
98
99 (autoload 'wcheck-mode "wcheck-mode"
100 "Toggle wcheck-mode." t)
101 (autoload 'wcheck-change-language "wcheck-mode"
102 "Switch wcheck-mode languages." t)
103 (autoload 'wcheck-actions "wcheck-mode"
104 "Open actions menu." t)
105 (autoload 'wcheck-jump-forward "wcheck-mode"
106 "Move point forward to next marked text area." t)
107 (autoload 'wcheck-jump-backward "wcheck-mode"
108 "Move point backward to previous marked text area." t)
109
110 [Marmalade]: http://marmalade-repo.org/
111 [Melpa]: http://melpa.milkbox.net/
112
113
114 Configuration and basic usage
115 -----------------------------
116
117 The internal documentation of variable `wcheck-language-data` has a
118 complete description on how to configure Wcheck mode language data. For
119 easy configuration you can use the options in the customize group named
120 _wcheck_ (`M-x customize-group RET wcheck RET`).
121
122 It might be convenient to bind Wcheck mode commands to some easily
123 accessible keys, for example:
124
125 (global-set-key (kbd "C-c s") 'wcheck-mode)
126 (global-set-key (kbd "C-c l") 'wcheck-change-language)
127 (global-set-key (kbd "C-c c") 'wcheck-actions)
128 (global-set-key (kbd "C-c n") 'wcheck-jump-forward)
129 (global-set-key (kbd "C-c p") 'wcheck-jump-backward)
130
131 Interactive command `wcheck-mode` toggles the text-checker minor mode
132 for the current buffer. Command `wcheck-change-language` is used to
133 switch languages and command `wcheck-actions` (or the right mouse
134 button) opens an actions menu for marked text. Commands
135 `wcheck-jump-forward` and `wcheck-jump-backward` jump to next or
136 previous marked text area.
137
138 A note for Emacs Lisp programmers: Emacs Lisp function
139 `wcheck-marked-text-at` returns information about marked text at a
140 buffer position. Programmers can use it to perform any kind of actions
141 for marked text. Function `wcheck-query-language-data` can be used for
142 querying effective configuration data for any language.
143
144
145 Examples
146 --------
147
148 Here are some examples on how you can fill the `wcheck-language-data`
149 variable. The value is a list of language configurations:
150
151 (setq wcheck-language-data
152 '(("language"
153 ...)
154 ("another language"
155 ...)))
156
157 Perhaps the most common use for Wcheck mode is to spell-check human
158 languages with Ispell (or compatible) spelling checker. Let's start with
159 examples on how to configure that.
160
161 The following settings configure two languages which are named "British
162 English" and "Finnish". The former language uses Ispell program as the
163 spell-checker engine. The latter uses Enchant which has an
164 Ispell-compatible command-line interface. Both languages use Wcheck
165 mode's actions feature to offer spelling suggestions for misspelled
166 words. Since both spelling checkers print spelling suggestions in the
167 Ispell format we use built-in function
168 `wcheck-parser-ispell-suggestions` to parse the output and populate the
169 actions (spelling suggestions) menu for user.
170
171 ("British English"
172 (program . "/usr/bin/ispell")
173 (args "-l" "-d" "british")
174 (action-program . "/usr/bin/ispell")
175 (action-args "-a" "-d" "british")
176 (action-parser . wcheck-parser-ispell-suggestions))
177
178 ("Finnish"
179 (program . "/usr/bin/enchant")
180 (args "-l" "-d" "fi")
181 (syntax . my-finnish-syntax-table)
182 (action-program . "/usr/bin/enchant")
183 (action-args "-a" "-d" "fi")
184 (action-parser . wcheck-parser-ispell-suggestions))
185
186 The "Finnish" language above used a special syntax table called
187 `my-finnish-syntax-table`. It could be defined like this:
188
189 (defvar my-finnish-syntax-table
190 (copy-syntax-table text-mode-syntax-table))
191
192 (modify-syntax-entry ?- "w" my-finnish-syntax-table)
193
194 It copies `text-mode-syntax-table` (which Wcheck mode uses by default)
195 and sets the syntactic meaning of the ASCII hyphen character (-) to a
196 word character ("w"). Wcheck mode and its regular expression search will
197 use that syntax table when scanning buffers' content in that language.
198
199 Below is an example on how to add an "Add to dictionary" feature to the
200 actions menu, among spelling suggestions. First, there's the language
201 configuration. The example below is similar to the "British English"
202 configuration above except that Enchant spell-checker is used and
203 `action-parser` is a custom function (which will be defined later).
204
205 ("British English"
206 (program . "/usr/bin/enchant")
207 (args "-l" "-d" "en_GB")
208 (action-program . "/usr/bin/enchant")
209 (action-args "-a" "-d" "en_GB")
210 (action-parser . enchant-suggestions-menu))
211
212 The action parser is custom function `enchant-suggestions-menu`. It will
213 call `wcheck-parser-ispell-suggestions` and then add "Add to dictionary"
214 option in the front of the spelling suggestions list. Choosing that
215 option from the actions menu will call function
216 `enchant-add-to-dictionary` (will be defined later).
217
218 (defun enchant-suggestions-menu (marked-text)
219 (cons (cons "[Add to dictionary]" 'enchant-add-to-dictionary)
220 (wcheck-parser-ispell-suggestions)))
221
222 Now we need to define the function `enchant-add-to-dictionary`. Below is
223 an example that works in GNU/Linux systems with Enchant spell-checker.
224 With small modifications it should work with other spelling checkers and
225 operating systems.
226
227 For British English language the user dictionary file is
228 `~/.config/enchant/en_GB.dic`. The language code is extracted
229 automatically from `wcheck-language-data` variable, so the function
230 works with any Enchant language. Note that adding a word to a dictionary
231 file doesn't have effect on the current spell-checking session. The
232 Enchant program must be restarted.
233
234 (defvar enchant-dictionaries-dir "~/.config/enchant")
235
236 (defun enchant-add-to-dictionary (marked-text)
237 (let* ((word (aref marked-text 0))
238 (language (aref marked-text 4))
239 (file (let ((code (nth 1 (member "-d" (wcheck-query-language-data
240 language 'action-args)))))
241 (when (stringp code)
242 (concat (file-name-as-directory enchant-dictionaries-dir)
243 code ".dic")))))
244
245 (when (and file (file-writable-p file))
246 (with-temp-buffer
247 (insert word) (newline)
248 (append-to-file (point-min) (point-max) file)
249 (message "Added word \"%s\" to the %s dictionary"
250 word language)))))
251
252 Spell-checking human languages is not the only application for Wcheck
253 mode. The following configuration adds language called "Trailing
254 whitespace" which finds and marks all trailing whitespace characters
255 (spaces and tabs) on buffer's lines. It uses regular expressions to
256 match the whitespace. The checker program is the Emacs Lisp function
257 `identity` which just returns its argument unchanged. The
258 `action-program` option and feature is used to build an action menu with
259 just one option: remove the whitespace. It replaces the original
260 whitespace string with empty string.
261
262 ("Trailing whitespace"
263 (program . identity)
264 (action-program . (lambda (marked-text)
265 (list (cons "Remove whitespace" ""))))
266 (face . highlight)
267 (regexp-start . "")
268 (regexp-body . "[ \t]+")
269 (regexp-end . "$")
270 (regexp-discard . "")
271 (read-or-skip-faces
272 (nil)))
273
274 Sometimes it's useful to highlight only a small number of keywords in
275 buffer. The following example adds a language called "Highlight FIXMEs"
276 which marks only "FIXME" words. FIXME is some programmers' convention to
277 put reminders in source code that some parts are not complete yet and
278 will be fixed or completed later. In source code files such keywords are
279 written in program's comments only, not in the actual code, so we use
280 `read-or-skip-faces` feature to scan only the comments. This example
281 configures it for `emacs-lisp-mode` and `c-mode`. In all other major
282 modes FIXMEs are marked everywhere.
283
284 ("Highlight FIXMEs"
285 (program . (lambda (strings)
286 (when (member "FIXME" strings)
287 (list "FIXME"))))
288 (face . highlight)
289 (read-or-skip-faces
290 ((emacs-lisp-mode c-mode) read font-lock-comment-face)
291 (nil)))
292
293 The following example adds a language "email" for highlighting email
294 addresses in buffer and creating an action menu which has option to
295 start composing mail to that address. Here's the language configuration:
296
297 ("email"
298 (program . email-address-detect)
299 (face . highlight)
300 (case-fold . t)
301 (regexp-start . "\\<")
302 (regexp-body . "\\S-+@\\S-+")
303 (regexp-end . "\\>")
304 (regexp-discard . "")
305 (action-program . email-action-menu)
306 (read-or-skip-faces
307 (nil)))
308
309 Then the needed functions:
310
311 (defun email-address-detect (strings)
312 (let (addresses)
313 (dolist (string strings addresses)
314 (when (string-match "\\<[a-z.-]+\\>@\\<[a-z.-]+\\>" string)
315 (push (match-string-no-properties 0 string) addresses)))))
316
317 (defun email-action-menu (marked-text)
318 (list (cons (concat "Mail to <" (aref marked-text 0) ">")
319 (lambda (marked-text)
320 (compose-mail (aref marked-text 0))))))
321
322 Note that detecting all valid email addresses is difficult and a much
323 more advanced parser is needed for that. Feel free to replace the
324 detection function with a better one.
325
326
327 The source code repository
328 --------------------------
329
330 GitHub repository URL: <https://github.com/tlikonen/wcheck-mode>
331
332 The branch named _master_ is the release branch and it should always be
333 safe to use. New features and experimental code are developed in other
334 branches and possibly merged to _master_ when they are ready.
335
336
337 Copyright and license
338 ---------------------
339
340 Copyright (C) 2009-2014 Teemu Likonen <<tlikonen@iki.fi>>
341
342 This program is free software: you can redistribute it and/or modify it
343 under the terms of the GNU General Public License as published by the
344 Free Software Foundation, either version 3 of the License, or (at your
345 option) any later version.
346
347 This program is distributed in the hope that it will be useful, but
348 WITHOUT ANY WARRANTY; without even the implied warranty of
349 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
350 Public License for more details.
351
352 The license text: <http://www.gnu.org/licenses/gpl-3.0.html>