tildify.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. ;;; tildify.el --- adding hard spaces into texts
  2. ;; Copyright (C) 1997-2012 Free Software Foundation, Inc.
  3. ;; Author: Milan Zamazal <pdm@zamazal.org>
  4. ;; Version: 4.5
  5. ;; Keywords: text, TeX, SGML, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package can be typically used for adding forgotten tildes in TeX
  19. ;; sources or adding `&nbsp;' sequences in SGML (e.g. HTML) texts.
  20. ;;
  21. ;; For example, the Czech orthography requires avoiding one letter
  22. ;; prepositions at line endings. So they should be connected with the
  23. ;; following words by a tilde. Some users forget to do this all the
  24. ;; time. The purpose of this program is to check the text and suggest
  25. ;; adding of missing tildes on some places. It works in a similar
  26. ;; manner to `query-replace-regexp'.
  27. ;;
  28. ;; The functionality of this program is actually performing query
  29. ;; replace on certain regions, but for historical reasons explained
  30. ;; above it is called `tildify'.
  31. ;;
  32. ;; The default variable settings are suited for Czech, so do not try to
  33. ;; understand them if you are not familiar with Czech grammar and spelling.
  34. ;;
  35. ;; The algorithm was inspired by Petr Ol¹ák's program `vlna'. Abilities of
  36. ;; `tildify.el' are a little limited; if you have improvement suggestions, let
  37. ;; me know.
  38. ;;; Code:
  39. ;;; *** User configuration variables ***
  40. (defgroup tildify nil
  41. "Adding missing hard spaces or other text fragments into texts."
  42. :version "21.1"
  43. :group 'wp)
  44. (defcustom tildify-pattern-alist
  45. '((t "\\([,:;(][ \t]*[a]\\|\\<[AIKOSUVZikosuvz]\\)\\([ \t]+\\|[ \t]*\n[ \t]*\\)\\(\\w\\|[([{\\]\\|<[a-zA-Z]\\)" 2))
  46. "Alist specifying where to insert hard spaces.
  47. Each alist item is of the form (MAJOR-MODE REGEXP NUMBER) or
  48. \(MAJOR-MODE . SYMBOL).
  49. MAJOR-MODE defines major mode, for which the item applies. It can be either:
  50. - a symbol equal to the major mode of the buffer to be fixed
  51. - t for default item, this applies to all major modes not defined in another
  52. alist item
  53. REGEXP is a regular expression matching the part of a text, where a hard space
  54. is missing. The regexp is always case sensitive, regardless of the current
  55. `case-fold-search' setting.
  56. NUMBER defines the number of the REGEXP subexpression which should be replaced
  57. by the hard space character.
  58. The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
  59. mode, the item for the mode SYMBOL is looked up in the alist instead."
  60. :group 'tildify
  61. :type '(repeat (choice (list symbol regexp integer) (cons symbol symbol))))
  62. (defcustom tildify-string-alist
  63. '((latex-mode . "~")
  64. (tex-mode . latex-mode)
  65. (plain-tex-mode . latex-mode)
  66. (sgml-mode . "&nbsp;")
  67. (xml-mode . sgml-mode)
  68. (html-mode . sgml-mode)
  69. (t . " "))
  70. "Alist specifying what is a hard space in the current major mode.
  71. Each alist item is of the form (MAJOR-MODE . STRING) or
  72. \(MAJOR-MODE . SYMBOL).
  73. MAJOR-MODE defines major mode, for which the item applies. It can be either:
  74. - a symbol equal to the major mode of the buffer to be fixed
  75. - t for default item, this applies to all major modes not defined in another
  76. alist item
  77. STRING defines the hard space, which is inserted at places defined by
  78. `tildify-pattern-alist'. For example it can be \"~\" for TeX or \"&nbsp;\"
  79. for SGML.
  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."
  82. :group 'tildify
  83. :type '(repeat (cons symbol (choice string symbol))))
  84. (defcustom tildify-ignored-environments-alist
  85. '((latex-mode
  86. ("\\\\\\\\" . "") ; do not remove this
  87. ("\\\\begin{verbatim}" . "\\\\end{verbatim}")
  88. ("\\\\verb\\*?\\(.\\)" . (1))
  89. ("\\$\\$" . "\\$\\$")
  90. ("\\$" . "\\$")
  91. ("\\\\(" . "\\\\)")
  92. ("\\\\[[]" . "\\\\[]]")
  93. ("\\\\begin{math}" . "\\\\end{math}")
  94. ("\\\\begin{displaymath}" . "\\\\end{displaymath}")
  95. ("\\\\begin{equation}" . "\\\\end{equation}")
  96. ("\\\\begin{eqnarray\\*?}" . "\\\\end{eqnarray\\*?}")
  97. ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
  98. ("%" . "$"))
  99. (plain-tex-mode . latex-mode)
  100. (html-mode
  101. ("<pre[^>]*>" . "</pre>")
  102. ("<dfn>" . "</dfn>")
  103. ("<code>" . "</code>")
  104. ("<samp>" . "</samp>")
  105. ("<kbd>" . "</kbd>")
  106. ("<var>" . "</var>")
  107. ("<PRE[^>]*>" . "</PRE>")
  108. ("<DFN>" . "</DFN>")
  109. ("<CODE>" . "</CODE>")
  110. ("<SAMP>" . "</SAMP>")
  111. ("<KBD>" . "</KBD>")
  112. ("<VAR>" . "</VAR>")
  113. ("<! *--" . "-- *>")
  114. ("<" . ">"))
  115. (sgml-mode . html-mode)
  116. (t nil))
  117. "Alist specifying ignored structured text environments.
  118. Parts of text defined in this alist are skipped without performing hard space
  119. insertion on them. These setting allow skipping text parts like verbatim or
  120. math environments in TeX or preformatted text in SGML.
  121. Each list element is of the form
  122. (MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
  123. MAJOR-MODE defines major mode, for which the item applies. It can be either:
  124. - a symbol equal to the major mode of the buffer to be fixed
  125. - t for default item, this applies to all major modes not defined in another
  126. alist item
  127. BEG-REGEX is a regexp matching beginning of a text part to be skipped.
  128. END-REGEX defines end of the corresponding text part and can be either:
  129. - a regexp matching the end of the skipped text part
  130. - a list of regexps and numbers, which will compose the ending regexp by
  131. concatenating themselves, while replacing the numbers with corresponding
  132. subexpressions of BEG-REGEX (this is used to solve cases like
  133. \\\\verb<character> in TeX)."
  134. :group 'tildify
  135. :type '(repeat (cons symbol (choice symbol (repeat sexp)))))
  136. ;;; *** Internal variables ***
  137. (defvar tildify-count nil
  138. "Counter for replacements.")
  139. ;;; *** Interactive functions ***
  140. ;;;###autoload
  141. (defun tildify-region (beg end)
  142. "Add hard spaces in the region between BEG and END.
  143. See variables `tildify-pattern-alist', `tildify-string-alist', and
  144. `tildify-ignored-environments-alist' for information about configuration
  145. parameters.
  146. This function performs no refilling of the changed text."
  147. (interactive "*r")
  148. (setq tildify-count 0)
  149. (let (a
  150. z
  151. (marker-end (copy-marker end))
  152. end-env
  153. finish
  154. (ask t)
  155. (case-fold-search nil)
  156. (regexp (tildify-build-regexp)) ; beginnings of environments
  157. aux)
  158. (if regexp
  159. ;; Yes, ignored environments exist for the current major mode,
  160. ;; tildify just texts outside them
  161. (save-excursion
  162. (save-restriction
  163. (widen)
  164. (goto-char (point-min))
  165. (while (not finish)
  166. (setq a (point))
  167. (setq end-env (tildify-find-env regexp))
  168. (setq z (copy-marker (if end-env (1- (point)) (point-max))))
  169. (if (>= (marker-position z) beg)
  170. (progn
  171. (or (>= a beg) (setq a beg))
  172. (or (<= (marker-position z) (marker-position marker-end))
  173. (setq z marker-end))
  174. (setq aux (tildify-tildify a (marker-position z) ask))
  175. (if (eq aux 'force)
  176. (setq ask nil)
  177. (if (eq aux nil)
  178. (setq finish t)))))
  179. (if (>= (marker-position z) (marker-position marker-end))
  180. (setq finish t))
  181. (or (>= (point) (marker-position z))
  182. (goto-char (marker-position z)))
  183. (if (not finish)
  184. (if (re-search-forward end-env nil t)
  185. (if (> (point) (marker-position marker-end))
  186. (setq finish t))
  187. (message
  188. "End of environment not found: %s" end-env)
  189. (setq finish t))))))
  190. ;; No ignored environments, tildify directly
  191. (tildify-tildify beg end ask)))
  192. (message "%d spaces replaced." tildify-count))
  193. ;;;###autoload
  194. (defun tildify-buffer ()
  195. "Add hard spaces in the current buffer.
  196. See variables `tildify-pattern-alist', `tildify-string-alist', and
  197. `tildify-ignored-environments-alist' for information about configuration
  198. parameters.
  199. This function performs no refilling of the changed text."
  200. (interactive "*")
  201. (tildify-region (point-min) (point-max)))
  202. ;;; *** Auxiliary functions ***
  203. (defun tildify-build-regexp ()
  204. "Build start of environment regexp."
  205. (let ((alist (tildify-mode-alist tildify-ignored-environments-alist))
  206. regexp)
  207. (when alist
  208. (setq regexp (caar alist))
  209. (setq alist (cdr alist))
  210. (while alist
  211. (setq regexp (concat regexp "\\|" (caar alist)))
  212. (setq alist (cdr alist)))
  213. regexp)))
  214. (defun tildify-mode-alist (mode-alist &optional mode)
  215. "Return alist item for the MODE-ALIST in the current major MODE."
  216. (if (null mode)
  217. (setq mode major-mode))
  218. (let ((alist (cdr (or (assoc mode mode-alist)
  219. (assoc t mode-alist)))))
  220. (if (and alist
  221. (symbolp alist))
  222. (tildify-mode-alist mode-alist alist)
  223. alist)))
  224. (defun tildify-find-env (regexp)
  225. "Find environment using REGEXP.
  226. Return regexp for the end of the environment or nil if no environment was
  227. found."
  228. ;; Find environment
  229. (if (re-search-forward regexp nil t)
  230. ;; Build end-env regexp
  231. (let ((match (match-string 0))
  232. (alist (tildify-mode-alist tildify-ignored-environments-alist))
  233. expression)
  234. (save-match-data
  235. (while (not (eq (string-match (caar alist) match) 0))
  236. (setq alist (cdr alist))))
  237. (if (stringp (setq expression (cdar alist)))
  238. expression
  239. (let ((result "")
  240. aux)
  241. (while expression
  242. (setq result (concat result
  243. (if (stringp (setq aux (car expression)))
  244. expression
  245. (regexp-quote (match-string aux)))))
  246. (setq expression (cdr expression)))
  247. result)))
  248. ;; Return nil if not found
  249. nil))
  250. (defun tildify-tildify (beg end ask)
  251. "Add tilde characters in the region between BEG and END.
  252. This function does not do any further checking except of for comments and
  253. macros.
  254. If ASK is nil, perform replace without asking user for confirmation.
  255. Returns one of symbols: t (all right), nil (quit), force (replace without
  256. further questions)."
  257. (save-excursion
  258. (goto-char beg)
  259. (let* ((alist (tildify-mode-alist tildify-pattern-alist))
  260. (regexp (car alist))
  261. (match-number (cadr alist))
  262. (tilde (tildify-mode-alist tildify-string-alist))
  263. (end-marker (copy-marker end))
  264. answer
  265. bad-answer
  266. replace
  267. quit
  268. (message-log-max nil))
  269. (while (and (not quit)
  270. (re-search-forward regexp (marker-position end-marker) t))
  271. (when (or (not ask)
  272. (progn
  273. (goto-char (match-beginning match-number))
  274. (setq bad-answer t)
  275. (while bad-answer
  276. (setq bad-answer nil)
  277. (message "Replace? (yn!q) ")
  278. (setq answer (read-event)))
  279. (cond
  280. ((or (eq answer ?y) (eq answer ? ) (eq answer 'space))
  281. (setq replace t))
  282. ((eq answer ?n)
  283. (setq replace nil))
  284. ((eq answer ?!)
  285. (setq replace t
  286. ask nil))
  287. ((eq answer ?q)
  288. (setq replace nil
  289. quit t))
  290. (t
  291. (message "Press y, n, !, or q.")
  292. (setq bad-answer t)))
  293. replace))
  294. (replace-match tilde t t nil match-number)
  295. (setq tildify-count (1+ tildify-count))))
  296. ;; Return value
  297. (cond
  298. (quit nil)
  299. ((not ask) 'force)
  300. (t t)))))
  301. ;;; *** Announce ***
  302. (provide 'tildify)
  303. ;; Local variables:
  304. ;; coding: iso-latin-2
  305. ;; End:
  306. ;;; tildify.el ends here