1 ;;; tildify.el --- adding hard spaces into texts
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This package can be typically used for adding forgotten tildes in TeX
30 ;; sources or adding ` ' sequences in SGML (e.g. HTML) texts.
32 ;; For example, the Czech orthography requires avoiding one letter
33 ;; prepositions at line endings. So they should be connected with the
34 ;; following words by a tilde. Some users forget to do this all the
35 ;; time. The purpose of this program is to check the text and suggest
36 ;; adding of missing tildes on some places. It works in a similar
37 ;; manner to `query-replace-regexp'.
39 ;; The functionality of this program is actually performing query
40 ;; replace on certain regions, but for historical reasons explained
41 ;; above it is called `tildify'.
43 ;; The default variable settings are suited for Czech, so do not try to
44 ;; understand them if you are not familiar with Czech grammar and spelling.
46 ;; The algorithm was inspired by Petr Ol¹ák's program `vlna'. Abilities of
47 ;; `tildify.el' are a little limited; if you have improvement suggestions, let
53 ;;; *** User configuration variables ***
57 "Adding missing hard spaces or other text fragments into texts."
61 (defcustom tildify-pattern-alist
62 '((t "\\([,:;(][ \t]*[a]\\|\\<[AIKOSUVZikosuvz]\\)\\([ \t]+\\|[ \t]*\n[ \t]*\\)\\(\\w\\|[([{\\]\\|<[a-zA-Z]\\)" 2))
63 "Alist specifying where to insert hard spaces.
65 Each alist item is of the form (MAJOR-MODE REGEXP NUMBER) or
66 \(MAJOR-MODE . SYMBOL).
68 MAJOR-MODE defines major mode, for which the item applies. It can be either:
69 - a symbol equal to the major mode of the buffer to be fixed
70 - t for default item, this applies to all major modes not defined in another
73 REGEXP is a regular expression matching the part of a text, where a hard space
74 is missing. The regexp is always case sensitive, regardless of the current
75 `case-fold-search' setting.
77 NUMBER defines the number of the REGEXP subexpression which should be replaced
78 by the hard space character.
80 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
81 mode, the item for the mode SYMBOL is looked up in the alist instead."
83 :type
'(repeat (choice (list symbol regexp integer
) (cons symbol symbol
))))
85 (defcustom tildify-string-alist
87 (tex-mode . latex-mode
)
88 (plain-tex-mode . latex-mode
)
89 (sgml-mode .
" ")
90 (xml-mode . sgml-mode
)
91 (html-mode . sgml-mode
)
93 "Alist specifying what is a hard space in the current major mode.
95 Each alist item is of the form (MAJOR-MODE . STRING) or
96 \(MAJOR-MODE . SYMBOL).
98 MAJOR-MODE defines major mode, for which the item applies. It can be either:
99 - a symbol equal to the major mode of the buffer to be fixed
100 - t for default item, this applies to all major modes not defined in another
103 STRING defines the hard space, which is inserted at places defined by
104 `tildify-pattern-alist'. For example it can be \"~\" for TeX or \" \"
107 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
108 mode, the item for the mode SYMBOL is looked up in the alist instead."
110 :type
'(repeat (cons symbol
(choice string symbol
))))
112 (defcustom tildify-ignored-environments-alist
114 ("\\\\\\\\" .
"") ; do not remove this
115 ("\\\\begin{verbatim}" .
"\\\\end{verbatim}")
116 ("\\\\verb\\*?\\(.\\)" .
(1))
117 ("\\$\\$" .
"\\$\\$")
120 ("\\\\[[]" .
"\\\\[]]")
121 ("\\\\begin{math}" .
"\\\\end{math}")
122 ("\\\\begin{displaymath}" .
"\\\\end{displaymath}")
123 ("\\\\begin{equation}" .
"\\\\end{equation}")
124 ("\\\\begin{eqnarray\\*?}" .
"\\\\end{eqnarray\\*?}")
125 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" .
"")
127 (plain-tex-mode . latex-mode
)
129 ("<pre[^>]*>" .
"</pre>")
131 ("<code>" .
"</code>")
132 ("<samp>" .
"</samp>")
135 ("<PRE[^>]*>" .
"</PRE>")
137 ("<CODE>" .
"</CODE>")
138 ("<SAMP>" .
"</SAMP>")
143 (sgml-mode . html-mode
)
145 "Alist specifying ignored structured text environments.
146 Parts of text defined in this alist are skipped without performing hard space
147 insertion on them. These setting allow skipping text parts like verbatim or
148 math environments in TeX or preformatted text in SGML.
150 Each list element is of the form
151 (MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
153 MAJOR-MODE defines major mode, for which the item applies. It can be either:
154 - a symbol equal to the major mode of the buffer to be fixed
155 - t for default item, this applies to all major modes not defined in another
158 BEG-REGEX is a regexp matching beginning of a text part to be skipped.
159 END-REGEX defines end of the corresponding text part and can be either:
160 - a regexp matching the end of the skipped text part
161 - a list of regexps and numbers, which will compose the ending regexp by
162 concatenating themselves, while replacing the numbers with corresponding
163 subexpressions of BEG-REGEX (this is used to solve cases like
164 \\\\verb<character> in TeX)."
166 :type
'(repeat (cons symbol
(choice symbol
(repeat sexp
)))))
169 ;;; *** Internal variables ***
171 (defvar tildify-count nil
172 "Counter for replacements.")
175 ;;; *** Interactive functions ***
178 (defun tildify-region (beg end
)
179 "Add hard spaces in the region between BEG and END.
180 See variables `tildify-pattern-alist', `tildify-string-alist', and
181 `tildify-ignored-environments-alist' for information about configuration
183 This function performs no refilling of the changed text."
185 (setq tildify-count
0)
188 (marker-end (copy-marker end
))
192 (case-fold-search nil
)
193 (regexp (tildify-build-regexp)) ; beginnings of environments
196 ;; Yes, ignored environments exist for the current major mode,
197 ;; tildify just texts outside them
201 (goto-char (point-min))
204 (setq end-env
(tildify-find-env regexp
))
205 (setq z
(copy-marker (if end-env
(1- (point)) (point-max))))
206 (if (>= (marker-position z
) beg
)
208 (or (>= a beg
) (setq a beg
))
209 (or (<= (marker-position z
) (marker-position marker-end
))
211 (setq aux
(tildify-tildify a
(marker-position z
) ask
))
216 (if (>= (marker-position z
) (marker-position marker-end
))
218 (or (>= (point) (marker-position z
))
219 (goto-char (marker-position z
)))
221 (if (re-search-forward end-env nil t
)
222 (if (> (point) (marker-position marker-end
))
225 "End of environment not found: %s" end-env
)
227 ;; No ignored environments, tildify directly
228 (tildify-tildify beg end ask
)))
229 (message "%d spaces replaced." tildify-count
))
232 (defun tildify-buffer ()
233 "Add hard spaces in the current buffer.
234 See variables `tildify-pattern-alist', `tildify-string-alist', and
235 `tildify-ignored-environments-alist' for information about configuration
237 This function performs no refilling of the changed text."
239 (tildify-region (point-min) (point-max)))
242 ;;; *** Auxiliary functions ***
244 (defun tildify-build-regexp ()
245 "Build start of environment regexp."
246 (let ((alist (tildify-mode-alist tildify-ignored-environments-alist
))
249 (setq regexp
(caar alist
))
250 (setq alist
(cdr alist
))
252 (setq regexp
(concat regexp
"\\|" (caar alist
)))
253 (setq alist
(cdr alist
)))
256 (defun tildify-mode-alist (mode-alist &optional mode
)
257 "Return alist item for the MODE-ALIST in the current major MODE."
259 (setq mode major-mode
))
260 (let ((alist (cdr (or (assoc mode mode-alist
)
261 (assoc t mode-alist
)))))
264 (tildify-mode-alist mode-alist alist
)
267 (defun tildify-find-env (regexp)
268 "Find environment using REGEXP.
269 Return regexp for the end of the environment or nil if no environment was
272 (if (re-search-forward regexp nil t
)
273 ;; Build end-env regexp
274 (let ((match (match-string 0))
275 (alist (tildify-mode-alist tildify-ignored-environments-alist
))
278 (while (not (eq (string-match (caar alist
) match
) 0))
279 (setq alist
(cdr alist
))))
280 (if (stringp (setq expression
(cdar alist
)))
285 (setq result
(concat result
286 (if (stringp (setq aux
(car expression
)))
288 (regexp-quote (match-string aux
)))))
289 (setq expression
(cdr expression
)))
291 ;; Return nil if not found
294 (defun tildify-tildify (beg end ask
)
295 "Add tilde characters in the region between BEG and END.
296 This function does not do any further checking except of for comments and
299 If ASK is nil, perform replace without asking user for confirmation.
301 Returns one of symbols: t (all right), nil (quit), force (replace without
305 (let* ((alist (tildify-mode-alist tildify-pattern-alist
))
307 (match-number (cadr alist
))
308 (tilde (tildify-mode-alist tildify-string-alist
))
309 (end-marker (copy-marker end
))
314 (message-log-max nil
))
315 (while (and (not quit
)
316 (re-search-forward regexp
(marker-position end-marker
) t
))
319 (goto-char (match-beginning match-number
))
322 (setq bad-answer nil
)
323 (message "Replace? (yn!q) ")
324 (setq answer
(read-event)))
326 ((or (eq answer ?y
) (eq answer ?
) (eq answer
'space
))
337 (message "Press y, n, !, or q.")
338 (setq bad-answer t
)))
340 (replace-match tilde t t nil match-number
)
341 (setq tildify-count
(1+ tildify-count
))))
355 ;; coding: iso-latin-2
358 ;;; arch-tag: fc9b05a6-7355-4639-8170-dcf57853ba22
359 ;;; tildify.el ends here