1 ;;; tildify.el --- adding hard spaces into texts
3 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
5 ;; Author: Milan Zamazal <pdm@zamazal.org>
6 ;; Michal Nazarewicz <mina86@mina86.com>
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 "Add hard spaces or other text fragments to text buffers."
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 (cons :tag
"Entry for major mode"
82 (choice (const :tag
"Default" t
)
83 (symbol :tag
"Major mode"))
84 (choice (list :tag
"Regexp"
86 (integer :tag
"Group "))
87 (symbol :tag
"Like other")))))
89 (defcustom tildify-string-alist
91 (tex-mode . latex-mode
)
92 (plain-tex-mode . latex-mode
)
93 (sgml-mode .
" ")
94 (html-mode . sgml-mode
)
95 (xml-mode .
" ") ; XML does not define use numeric reference
96 (nxml-mode . xml-mode
)
98 "Alist specifying what is a hard space in the current major mode.
100 Each alist item is of the form (MAJOR-MODE . STRING) or
101 \(MAJOR-MODE . SYMBOL).
103 MAJOR-MODE defines major mode, for which the item applies. It can be either:
104 - a symbol equal to the major mode of the buffer to be fixed
105 - t for default item, this applies to all major modes not defined in another
108 STRING defines the hard space, which is inserted at places defined by
109 `tildify-pattern-alist'. For example it can be \"~\" for TeX or \" \"
112 The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
113 mode, the item for the mode SYMBOL is looked up in the alist instead."
115 :type
'(repeat (cons :tag
"Entry for major mode"
116 (choice (const :tag
"Default" t
)
117 (symbol :tag
"Major mode"))
118 (choice (const :tag
"No-break space (U+00A0)" "\u00A0")
119 (string :tag
"String ")
120 (symbol :tag
"Like other")))))
122 (defcustom tildify-ignored-environments-alist
124 ("\\\\\\\\" .
"") ; do not remove this
125 (,(eval-when-compile (concat
127 (regexp-opt '("verbatim" "math" "displaymath"
128 "equation" "eqnarray" "eqnarray*"))
130 .
("\\\\end{" 1 "}"))
131 ("\\\\verb\\*?\\(.\\)" .
(1))
134 ("\\\\[[]" .
"\\\\[]]")
135 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" .
"")
137 (plain-tex-mode . latex-mode
)
139 (,(eval-when-compile (concat
141 (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var"
142 "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR"))
147 (sgml-mode . html-mode
)
151 (nxml-mode . xml-mode
))
152 "Alist specifying ignored structured text environments.
153 Parts of text defined in this alist are skipped without performing hard space
154 insertion on them. These setting allow skipping text parts like verbatim or
155 math environments in TeX or preformatted text in SGML.
157 Each list element is of the form
158 (MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
160 MAJOR-MODE defines major mode, for which the item applies. It can be either:
161 - a symbol equal to the major mode of the buffer to be fixed
162 - t for default item, this applies to all major modes not defined in another
165 BEG-REGEX is a regexp matching beginning of a text part to be skipped.
166 END-REGEX defines end of the corresponding text part and can be either:
167 - a regexp matching the end of the skipped text part
168 - a list of regexps and numbers, which will compose the ending regexp by
169 concatenating themselves, while replacing the numbers with corresponding
170 subexpressions of BEG-REGEX (this is used to solve cases like
171 \\\\verb<character> in TeX)."
174 (cons :tag
"Entry for major mode"
175 (choice (const :tag
"Default" t
)
176 (symbol :tag
"Major mode"))
181 (cons :tag
"Regexp pair"
182 (regexp :tag
"Open ")
184 (regexp :tag
"Regexp")
185 (list :tag
"Regexp and groups (concatenated)"
186 (choice (regexp :tag
"Regexp")
187 (integer :tag
"Group "))))))
188 (symbol :tag
"Like other")))))
191 ;;; *** Interactive functions ***
194 (defun tildify-region (beg end
&optional dont-ask
)
195 "Add hard spaces in the region between BEG and END.
196 See variables `tildify-pattern-alist', `tildify-string-alist', and
197 `tildify-ignored-environments-alist' for information about configuration
199 This function performs no refilling of the changed text.
200 If DONT-ASK is set, or called interactively with prefix argument, user
201 won't be prompted for confirmation of each substitution."
203 (let (case-fold-search (count 0) (ask (not dont-ask
)))
204 (tildify-foreach-region-outside-env beg end
206 (let ((aux (tildify-tildify beg end ask
)))
207 (setq count
(+ count
(car aux
)))
208 (if (not (eq (cdr aux
) 'force
))
212 (message "%d spaces replaced." count
)))
215 (defun tildify-buffer (&optional dont-ask
)
216 "Add hard spaces in the current buffer.
217 See variables `tildify-pattern-alist', `tildify-string-alist', and
218 `tildify-ignored-environments-alist' for information about configuration
220 This function performs no refilling of the changed text.
221 If DONT-ASK is set, or called interactively with prefix argument, user
222 won't be prompted for confirmation of each substitution."
224 (tildify-region (point-min) (point-max) dont-ask
))
227 ;;; *** Auxiliary functions ***
229 (defun tildify--pick-alist-entry (mode-alist &optional mode
)
230 "Return alist item for the MODE-ALIST in the current major MODE."
231 (let ((alist (cdr (or (assoc (or mode major-mode
) mode-alist
)
232 (assoc t mode-alist
)))))
235 (tildify--pick-alist-entry mode-alist alist
)
238 (defun tildify-foreach-region-outside-env (beg end callback
)
239 "Scan region from BEG to END calling CALLBACK on portions out of environments.
240 Call CALLBACK on each region outside of environment to ignore.
241 CALLBACK will only be called for regions which have intersection
242 with [BEG END]. It must be a function that takes two point
243 arguments specifying the region to operate on. Stop scanning the
244 region as soon as CALLBACK returns nil. Environments to ignore
245 are determined from `tildify-ignored-environments-alist'."
247 (let ((pairs (tildify--pick-alist-entry tildify-ignored-environments-alist
)))
249 (funcall callback beg end
)
250 (let ((func (lambda (b e
)
251 (let ((b (max b beg
)) (e (min e end
)))
252 (if (< b e
) (funcall callback b e
) t
))))
253 (beg-re (concat "\\(?:"
254 (mapconcat 'car pairs
"\\)\\|\\(?:")
260 (goto-char (point-min))
261 (while (and (< (setq p
(point)) end
)
262 (if (not (setq end-re
263 (tildify-find-env beg-re pairs
)))
264 (progn (funcall func p end
) nil
)
265 (funcall func p
(match-beginning 0))
266 (when (< (point) end
)
268 (re-search-forward end-re nil t
)))))))))))
270 (defun tildify-find-env (regexp pairs
)
271 "Find environment using REGEXP.
272 Return regexp for the end of the environment found in PAIRS or nil if
273 no environment was found."
275 (when (re-search-forward regexp nil t
)
277 (let ((match (match-string 0)))
278 (while (not (eq (string-match (caar pairs
) match
) 0))
279 (setq pairs
(cdr pairs
)))
280 (let ((expression (cdar pairs
)))
281 (if (stringp expression
)
287 (regexp-quote (match-string expr match
))))
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 (count . response) cons where count is number of string
299 replacements done and response is one of symbols: t (all right), nil
300 (quit), force (replace without further questions)."
303 (let* ((alist (tildify--pick-alist-entry tildify-pattern-alist
))
305 (match-number (cadr alist
))
306 (tilde (tildify--pick-alist-entry tildify-string-alist
))
307 (end-marker (copy-marker end
))
312 (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 count
(1+ count
))))
342 (cons count
(cond (quit nil
)
356 ;;; tildify.el ends here