1 ;;; tildify.el --- adding hard spaces into texts
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2002 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This package can be typically used for adding forgotten tildes in TeX
29 ;; sources or adding ` ' sequences in SGML (e.g. HTML) texts.
31 ;; For example, the Czech ortography requires avoiding one letter
32 ;; prepositions at line endings. So they should be connected with the
33 ;; following words by a tilde. Some users forget to do this all the
34 ;; time. The purpose of this program is to check the text and suggest
35 ;; adding of missing tildes on some places. It works in a similar
36 ;; manner to `query-replace-regexp'.
38 ;; The functionality of this program is actually performing query
39 ;; replace on certain regions, but for historical reasons explained
40 ;; above it is called `tildify'.
42 ;; The default variable settings are suited for Czech, so do not try to
43 ;; understand them if you are not familiar with Czech grammar and spelling.
45 ;; The algorithm was inspired by Petr Ol¹ák's program `vlna'. Abbilities of
46 ;; `tildify.el' are a little limited; if you have improvement suggestions, let
52 ;;; *** User configuration variables ***
56 "Adding missing hard spaces or other text fragments into texts."
60 (defcustom tildify-pattern-alist
61 '((t "\\([,:;(][ \t]*[a]\\|\\<[AIKOSUVZikosuvz]\\)\\([ \t]+\\|[ \t]*\n[ \t]*\\)\\(\\w\\|[([{\\]\\|<[a-zA-Z]\\)" 2))
62 "Alist specifying where to insert hard spaces.
64 Each alist item is of the form (MAJOR-MODE REGEXP NUMBER) or
65 \(MAJOR-MODE . SYMBOL).
67 MAJOR-MODE defines major mode, for which the item applies. It can be either:
68 - a symbol equal to the major mode of the buffer to be fixed
69 - t for default item, this applies to all major modes not defined in another
72 REGEXP is a regular expression matching the part of a text, where a hard space
73 is missing. The regexp is always case sensitive, regardless of the current
74 `case-fold-search' setting.
76 NUMBER defines the number of the REGEXP subexpression which should be replaced
77 by the hard space character.
79 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
80 mode, the item for the mode SYMBOL is looked up in the alist instead."
82 :type
'(repeat (choice (list symbol regexp integer
) (cons symbol symbol
))))
84 (defcustom tildify-string-alist
86 (tex-mode . latex-mode
)
87 (plain-tex-mode . latex-mode
)
88 (sgml-mode .
" ")
89 (xml-mode . sgml-mode
)
90 (html-mode . sgml-mode
)
92 "Alist specifying what is a hard space in the current major mode.
94 Each alist item is of the form (MAJOR-MODE . STRING) or
95 \(MAJOR-MODE . SYMBOL).
97 MAJOR-MODE defines major mode, for which the item applies. It can be either:
98 - a symbol equal to the major mode of the buffer to be fixed
99 - t for default item, this applies to all major modes not defined in another
102 STRING defines the hard space, which is inserted at places defined by
103 `tildify-pattern-alist'. For example it can be \"~\" for TeX or \" \"
106 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
107 mode, the item for the mode SYMBOL is looked up in the alist instead."
109 :type
'(repeat (cons symbol
(choice string symbol
))))
111 (defcustom tildify-ignored-environments-alist
113 ("\\\\\\\\" .
"") ; do not remove this
114 ("\\\\begin{verbatim}" .
"\\\\end{verbatim}")
115 ("\\\\verb\\*?\\(.\\)" .
(1))
116 ("\\$\\$" .
"\\$\\$")
119 ("\\\\[[]" .
"\\\\[]]")
120 ("\\\\begin{math}" .
"\\\\end{math}")
121 ("\\\\begin{displaymath}" .
"\\\\end{displaymath}")
122 ("\\\\begin{equation}" .
"\\\\end{equation}")
123 ("\\\\begin{eqnarray\\*?}" .
"\\\\end{eqnarray\\*?}")
124 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" .
"")
126 (plain-tex-mode . latex-mode
)
128 ("<pre[^>]*>" .
"</pre>")
130 ("<code>" .
"</code>")
131 ("<samp>" .
"</samp>")
134 ("<PRE[^>]*>" .
"</PRE>")
136 ("<CODE>" .
"</CODE>")
137 ("<SAMP>" .
"</SAMP>")
142 (sgml-mode . html-mode
)
144 "Alist specifying ignored structured text environments.
145 Parts of text defined in this alist are skipped without performing hard space
146 insertion on them. These setting allow skipping text parts like verbatim or
147 math environments in TeX or preformatted text in SGML.
149 Each list element is of the form
150 (MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
152 MAJOR-MODE defines major mode, for which the item applies. It can be either:
153 - a symbol equal to the major mode of the buffer to be fixed
154 - t for default item, this applies to all major modes not defined in another
157 BEG-REGEX is a regexp matching beginning of a text part to be skipped.
158 END-REGEX defines end of the corresponding text part and can be either:
159 - a regexp matching the end of the skipped text part
160 - a list of regexps and numbers, which will compose the ending regexp by
161 concatenating themselves, while replacing the numbers with corresponding
162 subexpressions of BEG-REGEX (this is used to solve cases like
163 \\\\verb<character> in TeX)."
165 :type
'(repeat (cons symbol
(choice symbol
(repeat sexp
)))))
168 ;;; *** Internal variables ***
170 (defvar tildify-count nil
171 "Counter for replacements.")
174 ;;; *** Interactive functions ***
177 (defun tildify-region (beg end
)
178 "Add hard spaces in the region between BEG and END.
179 See variables `tildify-pattern-alist', `tildify-string-alist', and
180 `tildify-ignored-environments-alist' for information about configuration
182 This function performs no refilling of the changed text."
184 (setq tildify-count
0)
187 (marker-end (copy-marker end
))
191 (case-fold-search nil
)
192 (regexp (tildify-build-regexp)) ; beginnings of environments
195 ;; Yes, ignored environments exist for the current major mode,
196 ;; tildify just texts outside them
200 (goto-char (point-min))
203 (setq end-env
(tildify-find-env regexp
))
204 (setq z
(copy-marker (if end-env
(1- (point)) (point-max))))
205 (if (>= (marker-position z
) beg
)
207 (or (>= a beg
) (setq a beg
))
208 (or (<= (marker-position z
) (marker-position marker-end
))
210 (setq aux
(tildify-tildify a
(marker-position z
) ask
))
215 (if (>= (marker-position z
) (marker-position marker-end
))
217 (or (>= (point) (marker-position z
))
218 (goto-char (marker-position z
)))
220 (if (re-search-forward end-env nil t
)
221 (if (> (point) (marker-position marker-end
))
224 (format "End of environment not found: %s" end-env
))
226 ;; No ignored environments, tildify directly
227 (tildify-tildify beg end ask
)))
228 (message (format "%d spaces replaced." tildify-count
)))
231 (defun tildify-buffer ()
232 "Add hard spaces in the current buffer.
233 See variables `tildify-pattern-alist', `tildify-string-alist', and
234 `tildify-ignored-environments-alist' for information about configuration
236 This function performs no refilling of the changed text."
238 (tildify-region (point-min) (point-max)))
241 ;;; *** Auxiliary functions ***
243 (defun tildify-build-regexp ()
244 "Build start of environment regexp."
245 (let ((alist (tildify-mode-alist tildify-ignored-environments-alist
))
248 (setq regexp
(caar alist
))
249 (setq alist
(cdr alist
))
251 (setq regexp
(concat regexp
"\\|" (caar alist
)))
252 (setq alist
(cdr alist
)))
255 (defun tildify-mode-alist (mode-alist &optional mode
)
256 "Return alist item for the MODE-ALIST in the current major MODE."
258 (setq mode major-mode
))
259 (let ((alist (cdr (or (assoc mode mode-alist
)
260 (assoc t mode-alist
)))))
263 (tildify-mode-alist mode-alist alist
)
266 (defun tildify-find-env (regexp)
267 "Find environment using REGEXP.
268 Return regexp for the end of the environment or nil if no environment was
271 (if (re-search-forward regexp nil t
)
272 ;; Build end-env regexp
273 (let ((match (match-string 0))
274 (alist (tildify-mode-alist tildify-ignored-environments-alist
))
277 (while (not (eq (string-match (caar alist
) match
) 0))
278 (setq alist
(cdr alist
))))
279 (if (stringp (setq expression
(cdar alist
)))
284 (setq result
(concat result
285 (if (stringp (setq aux
(car expression
)))
287 (regexp-quote (match-string aux
)))))
288 (setq expression
(cdr expression
)))
290 ;; Return nil if not found
293 (defun tildify-tildify (beg end ask
)
294 "Add tilde characters in the region between BEG and END.
295 This function does not do any further checking except of for comments and
298 If ASK is nil, perform replace without asking user for confirmation.
300 Returns one of symbols: t (all right), nil (quit), force (replace without
304 (let* ((alist (tildify-mode-alist tildify-pattern-alist
))
306 (match-number (cadr alist
))
307 (tilde (tildify-mode-alist tildify-string-alist
))
308 (end-marker (copy-marker end
))
313 (message-log-max nil
))
314 (while (and (not quit
)
315 (re-search-forward regexp
(marker-position end-marker
) t
))
318 (goto-char (match-beginning match-number
))
321 (setq bad-answer nil
)
322 (message "Replace? (yn!q) ")
323 (setq answer
(read-event)))
325 ((or (eq answer ?y
) (eq answer ?
) (eq answer
'space
))
336 (message "Press y, n, !, or q.")
337 (setq bad-answer t
)))
339 (replace-match tilde t t nil match-number
)
340 (setq tildify-count
(1+ tildify-count
))))
354 ;; coding: iso-latin-2
357 ;;; arch-tag: fc9b05a6-7355-4639-8170-dcf57853ba22
358 ;;; tildify.el ends here