1 ;;; tildify.el --- adding hard spaces into texts
3 ;; Copyright (C) 1997-2013 Free Software Foundation, Inc.
5 ;; Author: Milan Zamazal <pdm@zamazal.org>
7 ;; Keywords: text, TeX, SGML, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package can be typically used for adding forgotten tildes in TeX
27 ;; sources or adding ` ' sequences in SGML (e.g. HTML) texts.
29 ;; For example, the Czech orthography requires avoiding one letter
30 ;; prepositions at line endings. So they should be connected with the
31 ;; following words by a tilde. Some users forget to do this all the
32 ;; time. The purpose of this program is to check the text and suggest
33 ;; adding of missing tildes on some places. It works in a similar
34 ;; manner to `query-replace-regexp'.
36 ;; The functionality of this program is actually performing query
37 ;; replace on certain regions, but for historical reasons explained
38 ;; above it is called `tildify'.
40 ;; The default variable settings are suited for Czech, so do not try to
41 ;; understand them if you are not familiar with Czech grammar and spelling.
43 ;; The algorithm was inspired by Petr Olšák's program `vlna'. Abilities of
44 ;; `tildify.el' are a little limited; if you have improvement suggestions, let
50 ;;; *** User configuration variables ***
54 "Add hard spaces or other text fragments to text buffers."
58 (defcustom tildify-pattern-alist
59 '((t "\\([,:;(][ \t]*[a]\\|\\<[AIKOSUVZikosuvz]\\)\\([ \t]+\\|[ \t]*\n[ \t]*\\)\\(\\w\\|[([{\\]\\|<[a-zA-Z]\\)" 2))
60 "Alist specifying where to insert hard spaces.
62 Each alist item is of the form (MAJOR-MODE REGEXP NUMBER) or
63 \(MAJOR-MODE . SYMBOL).
65 MAJOR-MODE defines major mode, for which the item applies. It can be either:
66 - a symbol equal to the major mode of the buffer to be fixed
67 - t for default item, this applies to all major modes not defined in another
70 REGEXP is a regular expression matching the part of a text, where a hard space
71 is missing. The regexp is always case sensitive, regardless of the current
72 `case-fold-search' setting.
74 NUMBER defines the number of the REGEXP subexpression which should be replaced
75 by the hard space character.
77 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
78 mode, the item for the mode SYMBOL is looked up in the alist instead."
80 :type
'(repeat (choice (list symbol regexp integer
) (cons symbol symbol
))))
82 (defcustom tildify-string-alist
84 (tex-mode . latex-mode
)
85 (plain-tex-mode . latex-mode
)
86 (sgml-mode .
" ")
87 (xml-mode . sgml-mode
)
88 (html-mode . sgml-mode
)
90 "Alist specifying what is a hard space in the current major mode.
92 Each alist item is of the form (MAJOR-MODE . STRING) or
93 \(MAJOR-MODE . SYMBOL).
95 MAJOR-MODE defines major mode, for which the item applies. It can be either:
96 - a symbol equal to the major mode of the buffer to be fixed
97 - t for default item, this applies to all major modes not defined in another
100 STRING defines the hard space, which is inserted at places defined by
101 `tildify-pattern-alist'. For example it can be \"~\" for TeX or \" \"
104 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
105 mode, the item for the mode SYMBOL is looked up in the alist instead."
107 :type
'(repeat (cons symbol
(choice string symbol
))))
109 (defcustom tildify-ignored-environments-alist
111 ("\\\\\\\\" .
"") ; do not remove this
112 ("\\\\begin{verbatim}" .
"\\\\end{verbatim}")
113 ("\\\\verb\\*?\\(.\\)" .
(1))
114 ("\\$\\$" .
"\\$\\$")
117 ("\\\\[[]" .
"\\\\[]]")
118 ("\\\\begin{math}" .
"\\\\end{math}")
119 ("\\\\begin{displaymath}" .
"\\\\end{displaymath}")
120 ("\\\\begin{equation}" .
"\\\\end{equation}")
121 ("\\\\begin{eqnarray\\*?}" .
"\\\\end{eqnarray\\*?}")
122 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" .
"")
124 (plain-tex-mode . latex-mode
)
126 ("<pre[^>]*>" .
"</pre>")
128 ("<code>" .
"</code>")
129 ("<samp>" .
"</samp>")
132 ("<PRE[^>]*>" .
"</PRE>")
134 ("<CODE>" .
"</CODE>")
135 ("<SAMP>" .
"</SAMP>")
140 (sgml-mode . html-mode
)
142 "Alist specifying ignored structured text environments.
143 Parts of text defined in this alist are skipped without performing hard space
144 insertion on them. These setting allow skipping text parts like verbatim or
145 math environments in TeX or preformatted text in SGML.
147 Each list element is of the form
148 (MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
150 MAJOR-MODE defines major mode, for which the item applies. It can be either:
151 - a symbol equal to the major mode of the buffer to be fixed
152 - t for default item, this applies to all major modes not defined in another
155 BEG-REGEX is a regexp matching beginning of a text part to be skipped.
156 END-REGEX defines end of the corresponding text part and can be either:
157 - a regexp matching the end of the skipped text part
158 - a list of regexps and numbers, which will compose the ending regexp by
159 concatenating themselves, while replacing the numbers with corresponding
160 subexpressions of BEG-REGEX (this is used to solve cases like
161 \\\\verb<character> in TeX)."
163 :type
'(repeat (cons symbol
(choice symbol
(repeat sexp
)))))
166 ;;; *** Internal variables ***
168 (defvar tildify-count nil
169 "Counter for replacements.")
172 ;;; *** Interactive functions ***
175 (defun tildify-region (beg end
)
176 "Add hard spaces in the region between BEG and END.
177 See variables `tildify-pattern-alist', `tildify-string-alist', and
178 `tildify-ignored-environments-alist' for information about configuration
180 This function performs no refilling of the changed text."
182 (setq tildify-count
0)
185 (marker-end (copy-marker end
))
189 (case-fold-search nil
)
190 (regexp (tildify-build-regexp)) ; beginnings of environments
193 ;; Yes, ignored environments exist for the current major mode,
194 ;; tildify just texts outside them
198 (goto-char (point-min))
201 (setq end-env
(tildify-find-env regexp
))
202 (setq z
(copy-marker (if end-env
(1- (point)) (point-max))))
203 (if (>= (marker-position z
) beg
)
205 (or (>= a beg
) (setq a beg
))
206 (or (<= (marker-position z
) (marker-position marker-end
))
208 (setq aux
(tildify-tildify a
(marker-position z
) ask
))
213 (if (>= (marker-position z
) (marker-position marker-end
))
215 (or (>= (point) (marker-position z
))
216 (goto-char (marker-position z
)))
218 (if (re-search-forward end-env nil t
)
219 (if (> (point) (marker-position marker-end
))
222 "End of environment not found: %s" end-env
)
224 ;; No ignored environments, tildify directly
225 (tildify-tildify beg end ask
)))
226 (message "%d spaces replaced." tildify-count
))
229 (defun tildify-buffer ()
230 "Add hard spaces in the current buffer.
231 See variables `tildify-pattern-alist', `tildify-string-alist', and
232 `tildify-ignored-environments-alist' for information about configuration
234 This function performs no refilling of the changed text."
236 (tildify-region (point-min) (point-max)))
239 ;;; *** Auxiliary functions ***
241 (defun tildify-build-regexp ()
242 "Build start of environment regexp."
243 (let ((alist (tildify-mode-alist tildify-ignored-environments-alist
))
246 (setq regexp
(caar alist
))
247 (setq alist
(cdr alist
))
249 (setq regexp
(concat regexp
"\\|" (caar alist
)))
250 (setq alist
(cdr alist
)))
253 (defun tildify-mode-alist (mode-alist &optional mode
)
254 "Return alist item for the MODE-ALIST in the current major MODE."
256 (setq mode major-mode
))
257 (let ((alist (cdr (or (assoc mode mode-alist
)
258 (assoc t mode-alist
)))))
261 (tildify-mode-alist mode-alist alist
)
264 (defun tildify-find-env (regexp)
265 "Find environment using REGEXP.
266 Return regexp for the end of the environment or nil if no environment was
269 (if (re-search-forward regexp nil t
)
270 ;; Build end-env regexp
271 (let ((match (match-string 0))
272 (alist (tildify-mode-alist tildify-ignored-environments-alist
))
275 (while (not (eq (string-match (caar alist
) match
) 0))
276 (setq alist
(cdr alist
))))
277 (if (stringp (setq expression
(cdar alist
)))
282 (setq result
(concat result
283 (if (stringp (setq aux
(car expression
)))
285 (regexp-quote (match-string aux
)))))
286 (setq expression
(cdr expression
)))
288 ;; Return nil if not found
291 (defun tildify-tildify (beg end ask
)
292 "Add tilde characters in the region between BEG and END.
293 This function does not do any further checking except of for comments and
296 If ASK is nil, perform replace without asking user for confirmation.
298 Returns one of symbols: t (all right), nil (quit), force (replace without
302 (let* ((alist (tildify-mode-alist tildify-pattern-alist
))
304 (match-number (cadr alist
))
305 (tilde (tildify-mode-alist tildify-string-alist
))
306 (end-marker (copy-marker end
))
311 (message-log-max nil
))
312 (while (and (not quit
)
313 (re-search-forward regexp
(marker-position end-marker
) t
))
316 (goto-char (match-beginning match-number
))
319 (setq bad-answer nil
)
320 (message "Replace? (yn!q) ")
321 (setq answer
(read-event)))
323 ((or (eq answer ?y
) (eq answer ?
) (eq answer
'space
))
334 (message "Press y, n, !, or q.")
335 (setq bad-answer t
)))
337 (replace-match tilde t t nil match-number
)
338 (setq tildify-count
(1+ tildify-count
))))
355 ;;; tildify.el ends here