1999-10-26 09:48:09 +00:00
|
|
|
;;; tildify.el --- adding hard spaces into texts
|
|
|
|
|
2014-01-01 07:43:34 +00:00
|
|
|
;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
|
1999-10-26 09:48:09 +00:00
|
|
|
|
2001-08-05 09:04:05 +00:00
|
|
|
;; Author: Milan Zamazal <pdm@zamazal.org>
|
2014-06-05 16:42:07 +02:00
|
|
|
;; Michal Nazarewicz <mina86@mina86.com>
|
2014-11-16 17:38:15 +01:00
|
|
|
;; Version: 4.5.5
|
2000-09-10 22:07:06 +00:00
|
|
|
;; Keywords: text, TeX, SGML, wp
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 04:34:22 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1999-10-26 09:48:09 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 04:34:22 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2008-05-06 04:34:22 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This package can be typically used for adding forgotten tildes in TeX
|
|
|
|
;; sources or adding ` ' sequences in SGML (e.g. HTML) texts.
|
|
|
|
;;
|
2006-01-06 12:11:53 +00:00
|
|
|
;; For example, the Czech orthography requires avoiding one letter
|
2000-09-10 22:07:06 +00:00
|
|
|
;; prepositions at line endings. So they should be connected with the
|
|
|
|
;; following words by a tilde. Some users forget to do this all the
|
|
|
|
;; time. The purpose of this program is to check the text and suggest
|
|
|
|
;; adding of missing tildes on some places. It works in a similar
|
|
|
|
;; manner to `query-replace-regexp'.
|
1999-10-26 09:48:09 +00:00
|
|
|
;;
|
2000-09-10 22:07:06 +00:00
|
|
|
;; The functionality of this program is actually performing query
|
|
|
|
;; replace on certain regions, but for historical reasons explained
|
|
|
|
;; above it is called `tildify'.
|
1999-10-26 09:48:09 +00:00
|
|
|
;;
|
|
|
|
;; The default variable settings are suited for Czech, so do not try to
|
|
|
|
;; understand them if you are not familiar with Czech grammar and spelling.
|
|
|
|
;;
|
2013-03-05 09:13:01 -08:00
|
|
|
;; The algorithm was inspired by Petr Olšák's program `vlna'. Abilities of
|
1999-10-26 09:48:09 +00:00
|
|
|
;; `tildify.el' are a little limited; if you have improvement suggestions, let
|
|
|
|
;; me know.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
|
|
;;; *** User configuration variables ***
|
|
|
|
|
|
|
|
|
|
|
|
(defgroup tildify nil
|
2012-04-22 21:58:00 +08:00
|
|
|
"Add hard spaces or other text fragments to text buffers."
|
2000-09-10 22:07:06 +00:00
|
|
|
:version "21.1"
|
1999-10-26 09:48:09 +00:00
|
|
|
:group 'wp)
|
|
|
|
|
|
|
|
(defcustom tildify-pattern-alist
|
|
|
|
'((t "\\([,:;(][ \t]*[a]\\|\\<[AIKOSUVZikosuvz]\\)\\([ \t]+\\|[ \t]*\n[ \t]*\\)\\(\\w\\|[([{\\]\\|<[a-zA-Z]\\)" 2))
|
|
|
|
"Alist specifying where to insert hard spaces.
|
|
|
|
|
|
|
|
Each alist item is of the form (MAJOR-MODE REGEXP NUMBER) or
|
2001-05-25 21:41:44 +00:00
|
|
|
\(MAJOR-MODE . SYMBOL).
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
MAJOR-MODE defines major mode, for which the item applies. It can be either:
|
|
|
|
- a symbol equal to the major mode of the buffer to be fixed
|
|
|
|
- t for default item, this applies to all major modes not defined in another
|
|
|
|
alist item
|
|
|
|
|
|
|
|
REGEXP is a regular expression matching the part of a text, where a hard space
|
|
|
|
is missing. The regexp is always case sensitive, regardless of the current
|
|
|
|
`case-fold-search' setting.
|
|
|
|
|
|
|
|
NUMBER defines the number of the REGEXP subexpression which should be replaced
|
|
|
|
by the hard space character.
|
|
|
|
|
|
|
|
The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
|
2000-09-10 22:07:06 +00:00
|
|
|
mode, the item for the mode SYMBOL is looked up in the alist instead."
|
1999-10-26 09:48:09 +00:00
|
|
|
:group 'tildify
|
2014-06-05 16:40:26 +02:00
|
|
|
:type '(repeat (cons :tag "Entry for major mode"
|
|
|
|
(choice (const :tag "Default" t)
|
|
|
|
(symbol :tag "Major mode"))
|
|
|
|
(choice (list :tag "Regexp"
|
|
|
|
regexp
|
|
|
|
(integer :tag "Group "))
|
|
|
|
(symbol :tag "Like other")))))
|
1999-10-26 09:48:09 +00:00
|
|
|
|
2014-11-16 17:38:15 +01:00
|
|
|
(defcustom tildify-space-string "\u00A0"
|
|
|
|
"Representation of a hard (a.k.a. no-break) space in current major mode.
|
|
|
|
|
|
|
|
Used by `tildify-buffer' in places where space is required but line
|
|
|
|
cannot be broken. For example \"~\" for TeX or \" \" for SGML,
|
|
|
|
HTML and XML modes. A no-break space Unicode character (\"\\u00A0\")
|
|
|
|
might be used for other modes if compatible encoding is used.
|
|
|
|
|
|
|
|
If nil, current major mode has no way to represent a hard space."
|
|
|
|
:version "25.1"
|
|
|
|
:group 'tildify
|
|
|
|
:type '(choice (const :tag "Space character (no hard-space representation)"
|
|
|
|
" ")
|
|
|
|
(const :tag "No-break space (U+00A0)" "\u00A0")
|
|
|
|
(string :tag "Custom string"))
|
|
|
|
:safe t)
|
|
|
|
|
|
|
|
(defcustom tildify-string-alist ()
|
1999-10-26 09:48:09 +00:00
|
|
|
"Alist specifying what is a hard space in the current major mode.
|
|
|
|
|
|
|
|
Each alist item is of the form (MAJOR-MODE . STRING) or
|
2001-05-25 21:41:44 +00:00
|
|
|
\(MAJOR-MODE . SYMBOL).
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
MAJOR-MODE defines major mode, for which the item applies. It can be either:
|
|
|
|
- a symbol equal to the major mode of the buffer to be fixed
|
|
|
|
- t for default item, this applies to all major modes not defined in another
|
|
|
|
alist item
|
|
|
|
|
|
|
|
STRING defines the hard space, which is inserted at places defined by
|
|
|
|
`tildify-pattern-alist'. For example it can be \"~\" for TeX or \" \"
|
|
|
|
for SGML.
|
|
|
|
|
|
|
|
The form (MAJOR-MODE . SYMBOL) defines alias item for MAJOR-MODE. For this
|
2000-09-10 22:07:06 +00:00
|
|
|
mode, the item for the mode SYMBOL is looked up in the alist instead."
|
1999-10-26 09:48:09 +00:00
|
|
|
:group 'tildify
|
2014-06-05 16:40:26 +02:00
|
|
|
:type '(repeat (cons :tag "Entry for major mode"
|
|
|
|
(choice (const :tag "Default" t)
|
|
|
|
(symbol :tag "Major mode"))
|
|
|
|
(choice (const :tag "No-break space (U+00A0)" "\u00A0")
|
|
|
|
(string :tag "String ")
|
|
|
|
(symbol :tag "Like other")))))
|
2014-11-16 17:38:15 +01:00
|
|
|
(make-obsolete-variable 'tildify-string-alist
|
|
|
|
'tildify-space-string "25.1")
|
2003-02-04 13:30:45 +00:00
|
|
|
|
1999-10-26 09:48:09 +00:00
|
|
|
(defcustom tildify-ignored-environments-alist
|
2014-06-05 16:41:32 +02:00
|
|
|
`((latex-mode
|
1999-10-26 09:48:09 +00:00
|
|
|
("\\\\\\\\" . "") ; do not remove this
|
2014-06-05 16:41:32 +02:00
|
|
|
(,(eval-when-compile (concat
|
|
|
|
"\\\\begin{\\("
|
|
|
|
(regexp-opt '("verbatim" "math" "displaymath"
|
|
|
|
"equation" "eqnarray" "eqnarray*"))
|
|
|
|
"\\)}"))
|
|
|
|
. ("\\\\end{" 1 "}"))
|
2001-05-25 21:43:21 +00:00
|
|
|
("\\\\verb\\*?\\(.\\)" . (1))
|
2014-06-05 16:41:32 +02:00
|
|
|
("\\$\\$?" . (0))
|
1999-10-26 09:48:09 +00:00
|
|
|
("\\\\(" . "\\\\)")
|
|
|
|
("\\\\[[]" . "\\\\[]]")
|
|
|
|
("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
|
|
|
|
("%" . "$"))
|
|
|
|
(plain-tex-mode . latex-mode)
|
|
|
|
(html-mode
|
2014-06-05 16:41:32 +02:00
|
|
|
(,(eval-when-compile (concat
|
|
|
|
"<\\("
|
|
|
|
(regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var"
|
|
|
|
"PRE" "DFN" "CODE" "SAMP" "KBD" "VAR"))
|
|
|
|
"\\)\\>[^>]*>"))
|
|
|
|
. ("</" 1 ">"))
|
1999-10-26 09:48:09 +00:00
|
|
|
("<! *--" . "-- *>")
|
|
|
|
("<" . ">"))
|
|
|
|
(sgml-mode . html-mode)
|
2014-06-05 16:41:01 +02:00
|
|
|
(xml-mode
|
|
|
|
("<! *--" . "-- *>")
|
|
|
|
("<" . ">"))
|
2014-06-05 16:41:32 +02:00
|
|
|
(nxml-mode . xml-mode))
|
1999-10-26 09:48:09 +00:00
|
|
|
"Alist specifying ignored structured text environments.
|
|
|
|
Parts of text defined in this alist are skipped without performing hard space
|
|
|
|
insertion on them. These setting allow skipping text parts like verbatim or
|
|
|
|
math environments in TeX or preformatted text in SGML.
|
|
|
|
|
|
|
|
Each list element is of the form
|
|
|
|
(MAJOR-MODE (BEG-REGEX . END-REGEX) (BEG-REGEX . END-REGEX) ... )
|
|
|
|
|
|
|
|
MAJOR-MODE defines major mode, for which the item applies. It can be either:
|
|
|
|
- a symbol equal to the major mode of the buffer to be fixed
|
|
|
|
- t for default item, this applies to all major modes not defined in another
|
|
|
|
alist item
|
|
|
|
|
|
|
|
BEG-REGEX is a regexp matching beginning of a text part to be skipped.
|
|
|
|
END-REGEX defines end of the corresponding text part and can be either:
|
|
|
|
- a regexp matching the end of the skipped text part
|
|
|
|
- a list of regexps and numbers, which will compose the ending regexp by
|
|
|
|
concatenating themselves, while replacing the numbers with corresponding
|
|
|
|
subexpressions of BEG-REGEX (this is used to solve cases like
|
2000-09-10 22:07:06 +00:00
|
|
|
\\\\verb<character> in TeX)."
|
1999-10-26 09:48:09 +00:00
|
|
|
:group 'tildify
|
2014-06-05 16:40:26 +02:00
|
|
|
:type '(repeat
|
|
|
|
(cons :tag "Entry for major mode"
|
|
|
|
(choice (const :tag "Default" t)
|
|
|
|
(symbol :tag "Major mode"))
|
|
|
|
(choice
|
|
|
|
(const :tag "None")
|
|
|
|
(repeat
|
|
|
|
:tag "Environments"
|
|
|
|
(cons :tag "Regexp pair"
|
|
|
|
(regexp :tag "Open ")
|
|
|
|
(choice :tag "Close"
|
|
|
|
(regexp :tag "Regexp")
|
|
|
|
(list :tag "Regexp and groups (concatenated)"
|
|
|
|
(choice (regexp :tag "Regexp")
|
|
|
|
(integer :tag "Group "))))))
|
|
|
|
(symbol :tag "Like other")))))
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; *** Interactive functions ***
|
|
|
|
|
|
|
|
;;;###autoload
|
2014-05-27 21:00:44 -04:00
|
|
|
(defun tildify-region (beg end &optional dont-ask)
|
1999-10-26 09:48:09 +00:00
|
|
|
"Add hard spaces in the region between BEG and END.
|
2014-11-16 17:38:15 +01:00
|
|
|
See variables `tildify-pattern-alist', `tildify-space-string', and
|
1999-10-26 09:48:09 +00:00
|
|
|
`tildify-ignored-environments-alist' for information about configuration
|
|
|
|
parameters.
|
2014-05-27 21:00:44 -04:00
|
|
|
This function performs no refilling of the changed text.
|
|
|
|
If DONT-ASK is set, or called interactively with prefix argument, user
|
|
|
|
won't be prompted for confirmation of each substitution."
|
|
|
|
(interactive "*rP")
|
2014-06-05 16:42:07 +02:00
|
|
|
(let (case-fold-search (count 0) (ask (not dont-ask)))
|
|
|
|
(tildify-foreach-region-outside-env beg end
|
|
|
|
(lambda (beg end)
|
|
|
|
(let ((aux (tildify-tildify beg end ask)))
|
|
|
|
(setq count (+ count (car aux)))
|
|
|
|
(if (not (eq (cdr aux) 'force))
|
|
|
|
(cdr aux)
|
|
|
|
(setq ask nil)
|
|
|
|
t))))
|
|
|
|
(message "%d spaces replaced." count)))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
1999-10-26 09:48:09 +00:00
|
|
|
;;;###autoload
|
2014-05-27 21:00:44 -04:00
|
|
|
(defun tildify-buffer (&optional dont-ask)
|
1999-10-26 09:48:09 +00:00
|
|
|
"Add hard spaces in the current buffer.
|
2014-11-16 17:38:15 +01:00
|
|
|
See variables `tildify-pattern-alist', `tildify-space-string', and
|
1999-10-26 09:48:09 +00:00
|
|
|
`tildify-ignored-environments-alist' for information about configuration
|
|
|
|
parameters.
|
2014-05-27 21:00:44 -04:00
|
|
|
This function performs no refilling of the changed text.
|
|
|
|
If DONT-ASK is set, or called interactively with prefix argument, user
|
|
|
|
won't be prompted for confirmation of each substitution."
|
|
|
|
(interactive "*P")
|
|
|
|
(tildify-region (point-min) (point-max) dont-ask))
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; *** Auxiliary functions ***
|
|
|
|
|
2014-10-17 09:20:51 +02:00
|
|
|
(defun tildify--pick-alist-entry (mode-alist &optional mode)
|
1999-10-26 09:48:09 +00:00
|
|
|
"Return alist item for the MODE-ALIST in the current major MODE."
|
2014-06-05 16:42:07 +02:00
|
|
|
(let ((alist (cdr (or (assoc (or mode major-mode) mode-alist)
|
1999-10-26 09:48:09 +00:00
|
|
|
(assoc t mode-alist)))))
|
|
|
|
(if (and alist
|
|
|
|
(symbolp alist))
|
2014-10-17 09:20:51 +02:00
|
|
|
(tildify--pick-alist-entry mode-alist alist)
|
1999-10-26 09:48:09 +00:00
|
|
|
alist)))
|
2003-02-04 13:30:45 +00:00
|
|
|
|
2014-06-05 16:42:07 +02:00
|
|
|
(defun tildify-foreach-region-outside-env (beg end callback)
|
|
|
|
"Scan region from BEG to END calling CALLBACK on portions out of environments.
|
|
|
|
Call CALLBACK on each region outside of environment to ignore.
|
|
|
|
CALLBACK will only be called for regions which have intersection
|
|
|
|
with [BEG END]. It must be a function that takes two point
|
|
|
|
arguments specifying the region to operate on. Stop scanning the
|
|
|
|
region as soon as CALLBACK returns nil. Environments to ignore
|
|
|
|
are determined from `tildify-ignored-environments-alist'."
|
|
|
|
(declare (indent 2))
|
2014-10-17 09:20:51 +02:00
|
|
|
(let ((pairs (tildify--pick-alist-entry tildify-ignored-environments-alist)))
|
2014-06-05 16:42:07 +02:00
|
|
|
(if (not pairs)
|
|
|
|
(funcall callback beg end)
|
|
|
|
(let ((func (lambda (b e)
|
|
|
|
(let ((b (max b beg)) (e (min e end)))
|
|
|
|
(if (< b e) (funcall callback b e) t))))
|
|
|
|
(beg-re (concat "\\(?:"
|
|
|
|
(mapconcat 'car pairs "\\)\\|\\(?:")
|
|
|
|
"\\)"))
|
|
|
|
p end-re)
|
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
(widen)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (and (< (setq p (point)) end)
|
|
|
|
(if (not (setq end-re
|
|
|
|
(tildify-find-env beg-re pairs)))
|
|
|
|
(progn (funcall func p end) nil)
|
|
|
|
(funcall func p (match-beginning 0))
|
|
|
|
(when (< (point) end)
|
|
|
|
(setq p (point))
|
|
|
|
(re-search-forward end-re nil t)))))))))))
|
|
|
|
|
|
|
|
(defun tildify-find-env (regexp pairs)
|
1999-10-26 09:48:09 +00:00
|
|
|
"Find environment using REGEXP.
|
2014-06-05 16:42:07 +02:00
|
|
|
Return regexp for the end of the environment found in PAIRS or nil if
|
|
|
|
no environment was found."
|
1999-10-26 09:48:09 +00:00
|
|
|
;; Find environment
|
2014-06-05 16:37:45 +02:00
|
|
|
(when (re-search-forward regexp nil t)
|
2014-06-05 16:39:18 +02:00
|
|
|
(save-match-data
|
2014-06-05 16:42:07 +02:00
|
|
|
(let ((match (match-string 0)))
|
|
|
|
(while (not (eq (string-match (caar pairs) match) 0))
|
|
|
|
(setq pairs (cdr pairs)))
|
|
|
|
(let ((expression (cdar pairs)))
|
2014-06-05 16:39:18 +02:00
|
|
|
(if (stringp expression)
|
|
|
|
expression
|
|
|
|
(mapconcat
|
|
|
|
(lambda (expr)
|
|
|
|
(if (stringp expr)
|
|
|
|
expr
|
|
|
|
(regexp-quote (match-string expr match))))
|
|
|
|
expression
|
|
|
|
"")))))))
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
(defun tildify-tildify (beg end ask)
|
|
|
|
"Add tilde characters in the region between BEG and END.
|
|
|
|
This function does not do any further checking except of for comments and
|
|
|
|
macros.
|
|
|
|
|
|
|
|
If ASK is nil, perform replace without asking user for confirmation.
|
|
|
|
|
2014-06-05 16:42:07 +02:00
|
|
|
Returns (count . response) cons where count is number of string
|
|
|
|
replacements done and response is one of symbols: t (all right), nil
|
|
|
|
(quit), force (replace without further questions)."
|
1999-10-26 09:48:09 +00:00
|
|
|
(save-excursion
|
|
|
|
(goto-char beg)
|
2014-10-17 09:20:51 +02:00
|
|
|
(let* ((alist (tildify--pick-alist-entry tildify-pattern-alist))
|
1999-10-26 09:48:09 +00:00
|
|
|
(regexp (car alist))
|
|
|
|
(match-number (cadr alist))
|
2014-11-16 17:38:15 +01:00
|
|
|
(tilde (or (tildify--pick-alist-entry tildify-string-alist)
|
|
|
|
tildify-space-string))
|
1999-10-26 09:48:09 +00:00
|
|
|
(end-marker (copy-marker end))
|
|
|
|
answer
|
|
|
|
bad-answer
|
|
|
|
replace
|
|
|
|
quit
|
2014-06-05 16:42:07 +02:00
|
|
|
(message-log-max nil)
|
|
|
|
(count 0))
|
1999-10-26 09:48:09 +00:00
|
|
|
(while (and (not quit)
|
|
|
|
(re-search-forward regexp (marker-position end-marker) t))
|
|
|
|
(when (or (not ask)
|
|
|
|
(progn
|
|
|
|
(goto-char (match-beginning match-number))
|
|
|
|
(setq bad-answer t)
|
|
|
|
(while bad-answer
|
|
|
|
(setq bad-answer nil)
|
|
|
|
(message "Replace? (yn!q) ")
|
|
|
|
(setq answer (read-event)))
|
|
|
|
(cond
|
|
|
|
((or (eq answer ?y) (eq answer ? ) (eq answer 'space))
|
|
|
|
(setq replace t))
|
|
|
|
((eq answer ?n)
|
|
|
|
(setq replace nil))
|
|
|
|
((eq answer ?!)
|
|
|
|
(setq replace t
|
|
|
|
ask nil))
|
|
|
|
((eq answer ?q)
|
|
|
|
(setq replace nil
|
|
|
|
quit t))
|
|
|
|
(t
|
|
|
|
(message "Press y, n, !, or q.")
|
|
|
|
(setq bad-answer t)))
|
|
|
|
replace))
|
|
|
|
(replace-match tilde t t nil match-number)
|
2014-06-05 16:42:07 +02:00
|
|
|
(setq count (1+ count))))
|
1999-10-26 09:48:09 +00:00
|
|
|
;; Return value
|
2014-06-05 16:42:07 +02:00
|
|
|
(cons count (cond (quit nil)
|
|
|
|
((not ask) 'force)
|
|
|
|
(t t))))))
|
1999-10-26 09:48:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; *** Announce ***
|
|
|
|
|
|
|
|
(provide 'tildify)
|
|
|
|
|
|
|
|
|
|
|
|
;; Local variables:
|
2013-03-05 09:13:01 -08:00
|
|
|
;; coding: utf-8
|
1999-10-26 09:48:09 +00:00
|
|
|
;; End:
|
|
|
|
|
|
|
|
;;; tildify.el ends here
|