1 ;;; tildify.el --- adding hard spaces into texts
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Milan Zamazal <pdm@zamazal.org>
8 ;; Keywords: text, TeX, SGML, wp
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This package can be typically used for adding forgotten tildes in TeX
28 ;; sources or adding ` ' sequences in SGML (e.g. HTML) texts.
30 ;; For example, the Czech orthography requires avoiding one letter
31 ;; prepositions at line endings. So they should be connected with the
32 ;; following words by a tilde. Some users forget to do this all the
33 ;; time. The purpose of this program is to check the text and suggest
34 ;; adding of missing tildes on some places. It works in a similar
35 ;; manner to `query-replace-regexp'.
37 ;; The functionality of this program is actually performing query
38 ;; replace on certain regions, but for historical reasons explained
39 ;; above it is called `tildify'.
41 ;; The default variable settings are suited for Czech, so do not try to
42 ;; understand them if you are not familiar with Czech grammar and spelling.
44 ;; The algorithm was inspired by Petr Ol¹ák's program `vlna'. Abilities of
45 ;; `tildify.el' are a little limited; if you have improvement suggestions, let
51 ;;; *** User configuration variables ***
55 "Adding missing hard spaces or other text fragments into texts."
59 (defcustom tildify-pattern-alist
60 '((t "\\([,:;(][ \t]*[a]\\|\\<[AIKOSUVZikosuvz]\\)\\([ \t]+\\|[ \t]*\n[ \t]*\\)\\(\\w\\|[([{\\]\\|<[a-zA-Z]\\)" 2))
61 "Alist specifying where to insert hard spaces.
63 Each alist item is of the form (MAJOR-MODE REGEXP NUMBER) or
64 \(MAJOR-MODE . SYMBOL).
66 MAJOR-MODE defines major mode, for which the item applies. It can be either:
67 - a symbol equal to the major mode of the buffer to be fixed
68 - t for default item, this applies to all major modes not defined in another
71 REGEXP is a regular expression matching the part of a text, where a hard space
72 is missing. The regexp is always case sensitive, regardless of the current
73 `case-fold-search' setting.
75 NUMBER defines the number of the REGEXP subexpression which should be replaced
76 by the hard space character.
78 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
79 mode, the item for the mode SYMBOL is looked up in the alist instead."
81 :type
'(repeat (choice (list symbol regexp integer
) (cons symbol symbol
))))
83 (defcustom tildify-string-alist
85 (tex-mode . latex-mode
)
86 (plain-tex-mode . latex-mode
)
87 (sgml-mode .
" ")
88 (xml-mode . sgml-mode
)
89 (html-mode . sgml-mode
)
91 "Alist specifying what is a hard space in the current major mode.
93 Each alist item is of the form (MAJOR-MODE . STRING) or
94 \(MAJOR-MODE . SYMBOL).
96 MAJOR-MODE defines major mode, for which the item applies. It can be either:
97 - a symbol equal to the major mode of the buffer to be fixed
98 - t for default item, this applies to all major modes not defined in another
101 STRING defines the hard space, which is inserted at places defined by
102 `tildify-pattern-alist'. For example it can be \"~\" for TeX or \" \"
105 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
106 mode, the item for the mode SYMBOL is looked up in the alist instead."
108 :type
'(repeat (cons symbol
(choice string symbol
))))
110 (defcustom tildify-ignored-environments-alist
112 ("\\\\\\\\" .
"") ; do not remove this
113 ("\\\\begin{verbatim}" .
"\\\\end{verbatim}")
114 ("\\\\verb\\*?\\(.\\)" .
(1))
115 ("\\$\\$" .
"\\$\\$")
118 ("\\\\[[]" .
"\\\\[]]")
119 ("\\\\begin{math}" .
"\\\\end{math}")
120 ("\\\\begin{displaymath}" .
"\\\\end{displaymath}")
121 ("\\\\begin{equation}" .
"\\\\end{equation}")
122 ("\\\\begin{eqnarray\\*?}" .
"\\\\end{eqnarray\\*?}")
123 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" .
"")
125 (plain-tex-mode . latex-mode
)
127 ("<pre[^>]*>" .
"</pre>")
129 ("<code>" .
"</code>")
130 ("<samp>" .
"</samp>")
133 ("<PRE[^>]*>" .
"</PRE>")
135 ("<CODE>" .
"</CODE>")
136 ("<SAMP>" .
"</SAMP>")
141 (sgml-mode . html-mode
)
143 "Alist specifying ignored structured text environments.
144 Parts of text defined in this alist are skipped without performing hard space
145 insertion on them. These setting allow skipping text parts like verbatim or
146 math environments in TeX or preformatted text in SGML.
148 Each list element is of the form
149 (MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
151 MAJOR-MODE defines major mode, for which the item applies. It can be either:
152 - a symbol equal to the major mode of the buffer to be fixed
153 - t for default item, this applies to all major modes not defined in another
156 BEG-REGEX is a regexp matching beginning of a text part to be skipped.
157 END-REGEX defines end of the corresponding text part and can be either:
158 - a regexp matching the end of the skipped text part
159 - a list of regexps and numbers, which will compose the ending regexp by
160 concatenating themselves, while replacing the numbers with corresponding
161 subexpressions of BEG-REGEX (this is used to solve cases like
162 \\\\verb<character> in TeX)."
164 :type
'(repeat (cons symbol
(choice symbol
(repeat sexp
)))))
167 ;;; *** Internal variables ***
169 (defvar tildify-count nil
170 "Counter for replacements.")
173 ;;; *** Interactive functions ***
176 (defun tildify-region (beg end
)
177 "Add hard spaces in the region between BEG and END.
178 See variables `tildify-pattern-alist', `tildify-string-alist', and
179 `tildify-ignored-environments-alist' for information about configuration
181 This function performs no refilling of the changed text."
183 (setq tildify-count
0)
186 (marker-end (copy-marker end
))
190 (case-fold-search nil
)
191 (regexp (tildify-build-regexp)) ; beginnings of environments
194 ;; Yes, ignored environments exist for the current major mode,
195 ;; tildify just texts outside them
199 (goto-char (point-min))
202 (setq end-env
(tildify-find-env regexp
))
203 (setq z
(copy-marker (if end-env
(1- (point)) (point-max))))
204 (if (>= (marker-position z
) beg
)
206 (or (>= a beg
) (setq a beg
))
207 (or (<= (marker-position z
) (marker-position marker-end
))
209 (setq aux
(tildify-tildify a
(marker-position z
) ask
))
214 (if (>= (marker-position z
) (marker-position marker-end
))
216 (or (>= (point) (marker-position z
))
217 (goto-char (marker-position z
)))
219 (if (re-search-forward end-env nil t
)
220 (if (> (point) (marker-position marker-end
))
223 "End of environment not found: %s" end-env
)
225 ;; No ignored environments, tildify directly
226 (tildify-tildify beg end ask
)))
227 (message "%d spaces replaced." tildify-count
))
230 (defun tildify-buffer ()
231 "Add hard spaces in the current buffer.
232 See variables `tildify-pattern-alist', `tildify-string-alist', and
233 `tildify-ignored-environments-alist' for information about configuration
235 This function performs no refilling of the changed text."
237 (tildify-region (point-min) (point-max)))
240 ;;; *** Auxiliary functions ***
242 (defun tildify-build-regexp ()
243 "Build start of environment regexp."
244 (let ((alist (tildify-mode-alist tildify-ignored-environments-alist
))
247 (setq regexp
(caar alist
))
248 (setq alist
(cdr alist
))
250 (setq regexp
(concat regexp
"\\|" (caar alist
)))
251 (setq alist
(cdr alist
)))
254 (defun tildify-mode-alist (mode-alist &optional mode
)
255 "Return alist item for the MODE-ALIST in the current major MODE."
257 (setq mode major-mode
))
258 (let ((alist (cdr (or (assoc mode mode-alist
)
259 (assoc t mode-alist
)))))
262 (tildify-mode-alist mode-alist alist
)
265 (defun tildify-find-env (regexp)
266 "Find environment using REGEXP.
267 Return regexp for the end of the environment or nil if no environment was
270 (if (re-search-forward regexp nil t
)
271 ;; Build end-env regexp
272 (let ((match (match-string 0))
273 (alist (tildify-mode-alist tildify-ignored-environments-alist
))
276 (while (not (eq (string-match (caar alist
) match
) 0))
277 (setq alist
(cdr alist
))))
278 (if (stringp (setq expression
(cdar alist
)))
283 (setq result
(concat result
284 (if (stringp (setq aux
(car expression
)))
286 (regexp-quote (match-string aux
)))))
287 (setq expression
(cdr expression
)))
289 ;; Return nil if not found
292 (defun tildify-tildify (beg end ask
)
293 "Add tilde characters in the region between BEG and END.
294 This function does not do any further checking except of for comments and
297 If ASK is nil, perform replace without asking user for confirmation.
299 Returns one of symbols: t (all right), nil (quit), force (replace without
303 (let* ((alist (tildify-mode-alist tildify-pattern-alist
))
305 (match-number (cadr alist
))
306 (tilde (tildify-mode-alist tildify-string-alist
))
307 (end-marker (copy-marker end
))
312 (message-log-max nil
))
313 (while (and (not quit
)
314 (re-search-forward regexp
(marker-position end-marker
) t
))
317 (goto-char (match-beginning match-number
))
320 (setq bad-answer nil
)
321 (message "Replace? (yn!q) ")
322 (setq answer
(read-event)))
324 ((or (eq answer ?y
) (eq answer ?
) (eq answer
'space
))
335 (message "Press y, n, !, or q.")
336 (setq bad-answer t
)))
338 (replace-match tilde t t nil match-number
)
339 (setq tildify-count
(1+ tildify-count
))))
353 ;; coding: iso-latin-2
356 ;; arch-tag: fc9b05a6-7355-4639-8170-dcf57853ba22
357 ;;; tildify.el ends here