1 ;;; latin-unity.el --- Identify equivalent characters in the ISO Latin sets
3 ;; Copyright (C) 2002 Free Software Foundation, Inc
5 ;; Author: Stephen J. Turnbull
6 ;; Keywords: mule, charsets
7 ;; Created: 2002 January 17
8 ;; Last-modified: 2003 August 9
10 ;; This file is part of XEmacs.
12 ;; XEmacs 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 2, or (at your option)
17 ;; XEmacs 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 XEmacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
30 ;; Mule bogusly considers the various ISO-8859 extended character sets
31 ;; as disjoint, when ISO 8859 itself clearly considers them to be subsets
32 ;; of a larger character set. This library provides functions which
33 ;; determine the list of coding systems which can encode all of the
34 ;; characters in the buffer.
36 ;; The companion package `latin-euro-standards' provides the 'iso-8859-13,
37 ;; 'iso-8859-14, 'iso-8859-15, and 'iso-8859-16 coding systems charsets
38 ;; and coding systems.
42 (provide 'latin-unity)
47 (require 'latin-unity-vars)
49 (if (or (fboundp 'character-to-unicode) ; XEmacs post-21.5.5
50 (fboundp 'char-to-ucs)) ; Mule-UCS already loaded
51 (require 'latin-unity-tables "latin-unity-utils")
52 (require 'latin-unity-tables)) ; doesn't require Unicode support
55 ;;; User customization
57 (defgroup latin-unity nil
58 "Handle equivalent ISO-8859 characters properly (identify them) on output."
61 (defcustom latin-unity-preapproved-coding-system-list
62 '(buffer-default preferred)
63 "*List of coding systems used without querying the user if feasible.
65 The first feasible coding system in this list is used. The special values
66 'preferred and 'buffer-default may be present:
68 buffer-default Use the coding system used by `write-region', if feasible.
69 preferred Use the coding system specified by `prefer-coding-system'
72 \"Feasible\" means that all characters in the buffer can be represented by
73 the coding system. Coding systems in `latin-unity-ucs-list' are always
74 considered feasible. Other feasible coding systems are computed by
75 `latin-unity-representations-feasible-region'.
77 Most users will want at least one ISO 8859 coding system in this list, as
78 otherwise pure ASCII files will not be preapproved. (This is a bug, due
79 to the limitation of applicability of this package to Latin and universal.
80 The condition that an ISO 8859 coding system be included will be satisfied
81 implicitly by 'buffer-default or 'preferred for most users, but it can be
82 annoying for users of ISO 2022 or EUC coding systems.)
84 Note that the first universal coding system in this list shadows all other
85 coding systems. In particular, if your preferred coding system is a universal
86 coding system, and 'preferred is a member of this list, latin-unity will
87 blithely convert all your files to that coding system. This is considered a
88 feature, but it may surprise most users. Users who don't like this behavior
89 should move 'preferred to `latin-unity-preferred-coding-system-list'."
90 :type '(repeat symbol)
93 (defcustom latin-unity-preferred-coding-system-list
94 '(iso-8859-1 iso-8859-15 iso-8859-2 iso-8859-3 iso-8859-4 iso-8859-9)
95 "*List of coding systems suggested the user if feasible.
97 If no coding system in `latin-unity-preapproved-coding-system-list' is
98 feasible, this list will be recommended to the user, followed by the
99 `latin-unity-ucs-list'. (Since the user makes the choice, including
100 universal coding systems in this list is redundant.)
101 The first coding system in this list is the default choice.
103 The special values 'preferred and 'buffer-default may be present:
104 buffer-default Use the coding system used by `write-region', if feasible.
105 preferred Use the coding system specified by `prefer-coding-system'
108 \"Feasible\" means that all characters in the buffer can be represented by
109 the coding system. Coding systems in `latin-unity-ucs-list' are always
110 considered feasible. Other feasible coding systems are computed by
111 `latin-unity-representations-feasible-region'."
112 :type '(repeat symbol)
115 (defcustom latin-unity-ucs-list '(utf-8 iso-2022-7 ctext escape-quoted)
116 "*List of coding systems considered to be universal.
118 A universal coding system can represent all characters by definition.
120 Order matters; coding systems earlier in the list will be preferred
121 when recommending a coding system. These coding systems will not be
122 used without querying the user (unless they are also present in
123 `latin-unity-preapproved-coding-system-list'), and follow the
124 `latin-unity-preferred-coding-system-list' in the list of suggested
127 If none of the preferred coding systems are feasible, the first in
128 this list will be the default.
130 Notes on certain coding systems: If `escape-quoted' is not a member of
131 this list, you will be unable to autosave files or byte-compile Mule
134 latin-unity does not try to be \"smart\" about general ISO 2022 coding
135 systems, such as ISO-2022-JP. (They are not recognized as equivalent
136 to `iso-2022-7'.) If your preferred coding system is one of these,
137 consider adding it to `latin-unity-ucs-list'. Note that choice of 7-bit
138 UCSes for saving files will have the side effect that ISO 8859 files will
139 be saved in 7-bit form with ISO 2022 escape sequences. `ctext', i.e. X
140 Compound Text, is exceptional in that it will preserve ISO 8859/1. `ctext'
141 will convert files encoded in other ISO 8859 coding systems to 7-bit form."
142 :type '(repeat symbol)
145 (defcustom latin-unity-charset-alias-alist
146 '((latin-1 . latin-iso8859-1)
147 (latin-2 . latin-iso8859-2)
148 (latin-3 . latin-iso8859-3)
149 (latin-4 . latin-iso8859-4)
150 (latin-5 . latin-iso8859-9)
151 (latin-7 . latin-iso8859-13)
152 (latin-9 . latin-iso8859-15)
153 (latin-10 . latin-iso8859-16))
154 "*Alist mapping aliases (symbols) to Mule charset names (symbols).
156 Both aliases and names are symbols.
157 Aliases of unsupported charsets will be treated as if the charset name had
158 been entered directly (normally an error will be signaled for the Mule
160 :type '(repeat (cons symbol symbol))
163 (defcustom latin-unity-coding-system-alias-alist nil
164 "*Alist mapping aliases to Mule coding system names.
166 Both aliases and names are symbols.
167 Aliases of unsupported coding systems will be treated as if the coding system
168 name had been entered directly (normally an error will be signaled)."
169 :type '(repeat (cons symbol symbol))
172 ;; Needed because 'iso-8859-1 is type 'no-conversion, NOT type 'iso2022
173 (defcustom latin-unity-iso-8859-1-aliases '(iso-8859-1)
174 "List of coding systems to be treated as aliases of ISO 8859/1.
176 Not a user variable. Customize input of coding systems or charsets via
177 `latin-unity-coding-system-alias-alist' or `latin-unity-charset-alias-alist'."
178 :type '(repeat symbol)
181 (defcustom latin-unity-coding-system-list-buffer
182 " *latin-unity coding system preferences*"
183 "Name of buffer used to display codings systems by priority."
187 (defcustom latin-unity-hack-cookies-enabled-p t
188 "If non-nil, `latin-unity-sanity-check' validates coding cookies."
192 (defcustom latin-unity-like-to-live-dangerously nil
193 "Convert failure to remap buffer from error to warning.
195 Note that in ordinary editing, the buffer will normally remain, so the user
196 can re-save in a feasible coding system. However, many subsystems such as
197 MUAs may destroy the buffer immediately after disposing of the contents."
201 (defcustom latin-unity-may-set-coding-flag 'ask
202 "When may latin-unity reset `buffer-file-coding-system'?
204 nil never change it, return silently
205 t always change it, return silently
206 regrets-only never change it, warn that coding system is inappropriate
207 warn always change it, warn user of change
208 ask ask user's permission."
210 (const nil :doc "never change, return silently")
211 (const t :doc "always change, return silently")
213 :doc "never change, warn that coding system is inappropriate")
214 (const warn :doc "always change, warn user of change")
215 (const ask :doc "ask user's permission"))
223 (defun latin-unity-install ()
224 "Set up hooks and initialize variables for latin-unity.
225 Currently affects `write-region-pre-hook' and no variables."
229 (add-hook 'write-region-pre-hook 'latin-unity-sanity-check))
232 (defun latin-unity-uninstall ()
233 "Clean up hooks and void variables used by latin-unity.
234 Currently affects `write-region-pre-hook' and no variables."
238 (remove-hook 'write-region-pre-hook 'latin-unity-sanity-check))
243 ;; Internal variables
245 (defvar latin-unity-coding-cookies-found 0
246 "Internal variable.")
250 ;; Mule is _so_ losing. Coding system objects should generally be hidden
251 ;; from lookup functions, etc.
252 (defsubst latin-unity-massage-name (x coding-system)
253 "Return the name of the coding system referred to by symbol X.
255 X may be 'buffer-default, 'preferred, a coding system object, or a symbol
256 naming a coding system. CODING-SYSTEM determines the interpretation of
257 'buffer-default, and `coding-priority-list' that of 'preferred.
259 Does not check the aliases variables."
261 (cond ((eq x 'buffer-default) coding-system)
263 (coding-system-name ; #### nil arg -> binary, is this OK?
264 (coding-category-system (car (coding-priority-list)))))
267 ;; Think about using l-u-massage-name.
268 ;; Maybe (if (coding-system-alias-p cs) (coding-system-aliasee cs) cs)?
269 ;; But watch out, check what happens if an eol variant is derived from an
270 ;; alias. Also, note that `define-coding-system-alias' is relatively recent.
271 ;; Most "aliases" (including all the ones I know for 'iso-8859-1 :-( ) are
272 ;; made with `copy-coding-system', not d-c-s-a.
273 (defsubst latin-unity-base-name (cs)
274 "Return the base name of the coding system object or symbol CS.
276 The base name is a symbol naming the similar coding system with no EOL
278 (coding-system-name (coding-system-base (find-coding-system cs))))
280 (defun latin-unity-buffer-charsets-string (buffer &optional begin end)
281 "Insert a string listing the charsets found in BUFFER in the current buffer.
283 Returns the list of charsets.
285 By default the entire buffer is considered (and narrowing is ignored).
286 Optional arguments BEGIN and END may be provided to restrict the region
287 considered. The returned string is prefixed with a single space.
289 This is a debugging function; don't depend on its behavior."
290 (mapc (lambda (cs) (insert (format " %s" cs)))
295 (let ((begin (or begin (point-min)))
296 (end (or end (point-max))))
297 ;; this function is slow, at least in 21.4!
298 (charsets-in-region begin end))))))
300 (defsubst latin-unity-charset-feasible-system (charset bvector buffer-default)
301 "Return a feasible coding-system based on CHARSET and BVECTOR, or nil.
303 BVECTOR is a bit vector encoding feasible Latin charsets (ie, not ASCII).
304 If CHARSET is feasible, look it up in `latin-unity-cset-codesys-alist',
305 otherwise return nil."
306 (when latin-unity-debug (message "%s" charset))
307 (let ((sys (cdr (assq charset latin-unity-cset-codesys-alist))))
308 (and (memq sys (mapcar (lambda (x)
309 (latin-unity-massage-name x buffer-default))
310 latin-unity-preferred-coding-system-list))
311 ;; User may have specified systems unavailable in this XEmacs
312 (find-coding-system sys)
313 (/= 0 (logand (get charset 'latin-unity-flag-bit) bvector))
316 (defsubst latin-unity-coding-system-latin-charset (coding-system)
317 "Return the Latin charset used by CODING-SYSTEM, or nil, if none."
318 (or (car (rassq coding-system latin-unity-cset-codesys-alist))
320 (eq (coding-system-type coding-system) 'iso2022)
321 (coding-system-property coding-system 'charset-g1))))
324 (defun latin-unity-list-coding-systems (display-excluded)
325 "Display the coding systems listed by priority and group.
327 With prefix argument, also display otherwise excluded coding systems.
329 See also `latin-unity-preapproved-coding-systems',
330 `latin-unity-preferred-coding-systems', and `latin-unity-ucs-list'."
335 (pop-to-buffer (get-buffer-create
336 latin-unity-coding-system-list-buffer))
338 (let ((start (point)))
340 (insert "Pre-approved coding systems:\n ")
341 (mapc (lambda (codesys) (insert (format " %s" codesys)))
342 latin-unity-preapproved-coding-system-list)
343 (fill-region start (point))
345 (insert "\nSuggested coding systems:\n ")
347 (mapc (lambda (codesys) (insert (format " %s" codesys)))
348 latin-unity-preferred-coding-system-list)
349 (fill-region start (point))
351 (insert "\nUniversal coding systems:\n ")
353 (mapc (lambda (codesys) (insert (format " %s" codesys)))
354 latin-unity-ucs-list)
355 (fill-region start (point))
357 (when display-excluded
358 ;; Should arrange to only display excluded ones!
359 (insert "\nAll coding systems:\n ")
361 (mapc (lambda (codesys) (insert (format " %s" codesys)))
362 (coding-system-list))
363 (fill-region start (point))))))
365 ;; Accessors for character and charset equivalences
367 (defsubst latin-unity-feasible-charsets (character)
368 "Return the set (bit-vector) of charsets that can represent CHARACTER.
369 Accessor for `latin-unity-equivalences'."
370 (aref (get-char-table character latin-unity-equivalences) 0))
372 (defsubst latin-unity-equivalent-character (character charset)
373 "Return the code point representing CHARACTER in CHARSET.
374 Accessor for `latin-unity-equivalences'."
375 (aref (get-char-table character latin-unity-equivalences)
376 (get charset 'latin-unity-index)))
378 ;; Buffer coding system feasibility
381 (defun latin-unity-representations-feasible-buffer ()
382 "Apply latin-unity-representations-feasible-region to the current buffer."
384 (latin-unity-representations-feasible-region (point-min)
388 ;; latin-unity-representations-feasible-region
390 ;; The basic algorithm is to map over the region, compute the set of
391 ;; charsets that can represent each character (the "feasible charset"),
392 ;; and take the intersection of those sets.
394 ;; The current implementation takes advantage of the fact that ASCII
395 ;; characters are common and cannot change asciisets. Then using
396 ;; skip-chars-forward makes motion over ASCII subregions very fast.
398 ;; Easy optimizations would be to (1) append observed characters to the
399 ;; characters-to-skip string, and (2) to fail immediately on detection of
400 ;; a non-Latin character. (Avoid kludgy implementations of (2) which don't
401 ;; admit generalization as we add more character sets to unify.)
403 ;; This same strategy could be applied generally by precomputing classes
404 ;; of characters equivalent according to their effect on latinsets, and
405 ;; adding a whole class to the skip-chars-forward string once a member is
408 ;; Probably efficiency is a function of the number of characters matched,
409 ;; or maybe the length of the match string? With "skip-category-forward"
410 ;; over a precomputed category table it should be really fast. In practice
411 ;; for Latin character sets there are only 29 classes.
414 (defun latin-unity-representations-feasible-region (begin end &optional buf)
415 "Return character sets that can represent the text from BEGIN to END in BUF.
417 BUF defaults to the current buffer. Called interactively, will be
418 applied to the region. Function assumes BEGIN <= END.
420 The return value is a cons. The car is the list of character sets
421 that can individually represent all of the non-ASCII portion of the
422 buffer, and the cdr is the list of character sets that can
423 individually represent all of the ASCII portion."
426 (let* ((asciisets (logior (get 'ascii 'latin-unity-flag-bit)
427 (get 'latin-jisx0201 'latin-unity-flag-bit)))
428 (latinsets (logand (lognot asciisets) latin-unity-all-flags)))
430 (set-buffer (or buf (current-buffer)))
433 ;; autosave may pass us nil arguments. Force both to be nil, or
434 ;; both to be integer-or-marker-p.
435 (let ((begin (or begin (and (null end) (point-min))))
436 (end (or end (and (null begin) (point-max)))))
438 ;; The characters skipped here can't change asciisets.
439 ;; Note that to generalize this we would need to have a notion of
440 ;; classes of characters which do not change the representability.
441 ;; One thing we can do is to add the character itself.
442 (skip-chars-forward latin-unity-ascii-and-jis-roman)
443 (while (< (point) end)
444 (let* ((ch (char-after))
445 (cs (car (split-char ch))))
446 (cond ((or (eq cs 'latin-jisx0201)
449 (logand asciisets (latin-unity-feasible-charsets ch)
453 (logand latinsets (latin-unity-feasible-charsets ch)
456 ;; The characters skipped here can't change asciisets
457 (skip-chars-forward latin-unity-ascii-and-jis-roman)))))
458 (cons latinsets asciisets)))
461 ;; #### possibly it would be faster to do this in the previous function
462 ;; charsets-in-region unusable; before 21.5.27 it's in Lisp and very slow. :-(
463 (defun latin-unity-representations-present-region (begin end &optional buffer)
464 "Return a cons of two bit vectors giving character sets in region.
466 The car indicates which Latin characters sets were found, the cdr the ASCII
467 character sets. BUFFER defaults to the current buffer."
473 (set-buffer (or buffer (current-buffer)))
475 ;; autosave may pass us nil arguments. Force both to be nil, or
476 ;; both to be integer-or-marker-p.
477 ;; #### implementation differs from l-u-r-f-r
478 (narrow-to-region (or begin (and (null end) (point-min)))
479 (or end (and (null begin) (point-max))))
480 (goto-char (point-min))
482 (let* ((ch (char-after))
483 (cs (car (split-char ch)))
484 (flag (get cs 'latin-unity-flag-bit 0)))
487 (setq skipchars (concat "\000-\177" skipchars))
488 (setq asets (logior flag asets)))
489 ((eq cs 'latin-jisx0201)
490 ;; #### get this someday
491 ;;(setq skipchars (concat skipchars latin-unity-latin-jisx0201))
492 (setq skipchars (concat skipchars (list ch)))
493 (setq asets (logior flag asets)))
495 ;; C1 characters map to themselves in all ISO 8859 coding
496 ;; systems. So we ignore them in the feasibility computation.
497 ;; #### It would be nice to unify Windows-12xx charsets among
498 ;; themselves. Then add a local variable c1sets, and treat
499 ;; skipchars as in the default clause, below. This requires
500 ;; deciding how to treat C1 characters when graphic characters
501 ;; are also present in the same range. What Would Gates Do?
502 ;; This will also require an interface change. This function
503 ;; will need to return (lsets c1set asets), which is why I'm
504 ;; not doing the generalization yet.
506 ;; minor optimization
507 (setq skipchars (concat "\200-\237" skipchars)))
509 ;; #### actually we can do the whole charset here
510 ;; precompute and set a property on the cs symbol
511 (setq skipchars (concat skipchars (list ch)))
513 (setq lsets (logior latin-unity-non-latin-bit-flag lsets))
514 (setq lsets (logior flag lsets))))))
515 ;; The characters skipped here can't change asciisets
516 (skip-chars-forward skipchars))))
519 (defun latin-unity-maybe-set-coding-system (coding-system current)
520 "Set the `buffer-file-coding-system' to CODING-SYSTEM if not same as CURRENT.
521 Exact behavior depends on `latin-unity-may-set-coding-flag'.
522 Return value is not currently useful."
523 ;; we'd like the `message's below to be `warn', but `warn' is too obtrusive
524 ;(message "new %s; current %s" coding-system current)
525 (message "new %s; current %s" (latin-unity-base-name coding-system)
526 (latin-unity-base-name current))
527 (case latin-unity-may-set-coding-flag
530 (set-buffer-file-coding-system coding-system))
532 (message "Specified coding system used to save, but default not changed."))
534 (set-buffer-file-coding-system coding-system)
535 (message "Specified coding system used to save, and default changed."))
537 (cond ((eq (latin-unity-base-name coding-system)
538 (latin-unity-base-name current))
539 (message (concat "Specified coding system used."
540 " Default has same base and was not changed.")))
541 ((y-or-n-p (format "Change default coding system to %s? "
543 (set-buffer-file-coding-system coding-system)
544 (message "Specified coding system used, and default changed."))
546 "Specified coding system used, but default not changed."))))
548 (message (format "Unknown value for latin-unity-may-set-coding-flag: %s."
549 latin-unity-may-set-coding-flag)))))
551 ;; #### I see nothing useful to be done with APPEND.
552 ;; FILENAME, VISIT, or LOCKNAME could be used to default the coding system,
553 ;; but this would conflict with the semantics of `write-region'.
554 ;; #### The efficiency of this function can clearly be improved.
555 ;; #### This function should be set up to call the check functions in a
556 ;; condition-case, and call out to error handlers. Then tests could be
557 ;; written more easily.
560 (defun latin-unity-sanity-check (begin end filename append visit lockname
561 &optional coding-system)
562 "Check if CODING-SYSTEM can represent all characters between BEGIN and END.
564 If not, attempt to remap Latin characters to a single Latin-N set.
566 For compatibility with old broken versions of `write-region', CODING-SYSTEM
567 defaults to `buffer-file-coding-system'. FILENAME, APPEND, VISIT, and
568 LOCKNAME are ignored.
570 Return nil if buffer-file-coding-system is not (ISO-2022-compatible) Latin.
571 If buffer-file-coding-system is safe for the charsets actually present in
572 the buffer, return it. Otherwise, ask the user to choose a coding system,
573 and return that. If the user is asked to choose, possibly set the
574 `buffer-file-coding-system' depending on `latin-unity-may-set-coding-flag'.
576 This function does _not_ do the safe thing when `buffer-file-coding-system'
577 is nil (= no-conversion). It considers that \"non-Latin\", and passes it on
578 to the Mule detection mechanism. This could result in corruption. So avoid
579 setting `buffer-file-coding-system' to nil or 'no-conversion or 'binary.
581 This function is intended for use as a `write-region-pre-hook'. It does
582 nothing except return nil if `write-region' handlers are inhibited, or if
583 BEGIN is a string (to support the corresponding \"kludgy feature\" of
586 ;; don't do anything if we're in a `write-region' handler
587 ;; #### is nil the right return value if we are?
588 ;; Bypass also on `write-region's "kludgy feature" where BEGIN is a string
589 (if (or (eq inhibit-file-name-operation 'write-region) (stringp begin))
592 (let ((buffer-default
593 ;; theoretically we could look at other write-region-prehooks,
594 ;; but they might write the buffer and we lose bad
595 (coding-system-name ; #### nil arg -> binary, is this OK?
597 buffer-file-coding-system
598 (find-file-coding-system-for-write-from-filename filename))))
599 ;; check what representations are feasible
600 ;; csets == compatible character sets as (latin . ascii)
601 (csets (latin-unity-representations-feasible-region begin end))
602 ;; as an optimization we also check for what's in the buffer
603 ;; psets == present in buffer character sets as (latin . ascii)
604 (psets (latin-unity-representations-present-region begin end)))
606 (when latin-unity-debug (message "%s %s" csets psets) (sit-for 1))
609 ;; try the preapproved systems
611 (let ((systems latin-unity-preapproved-coding-system-list))
612 ;; while always returns nil
614 (let ((sys (latin-unity-massage-name (car systems)
616 (when latin-unity-debug (message "sys is %s" sys))
617 (when (latin-unity-maybe-remap begin end sys
619 (when latin-unity-debug (message "throwing %s" sys))
621 (setq systems (cdr systems)))))))
622 ;; ask the user about the preferred systems
623 ;; #### RFE: It also would be nice if the offending characters
624 ;; were marked in the buffer being checked. Evidently GNU Emacs
625 ;; 20.x could do this.
626 (t (let* ((recommended
627 (latin-unity-recommend-representation begin end csets
629 (codesys (car recommended))
630 ;(charset (cdr recommended)) ; unused?
632 (when latin-unity-debug (message "%s" recommended))
636 ;; universal coding systems
637 ;; #### we might want to unify here if the codesys is ISO 2022
638 ;; but we don't have enough information to decide
639 ((memq (latin-unity-base-name codesys) latin-unity-ucs-list)
640 (unless (eq (latin-unity-base-name codesys)
641 (latin-unity-base-name buffer-default))
642 (latin-unity-maybe-set-coding-system codesys buffer-default))
645 ;; ISO 2022 (including ISO 8859) compatible systems
646 ;; #### maybe we should check for G2 and G3 sets
647 ;; note the special case is necessary, as 'iso-8859-1 is NOT
648 ;; type 'iso2022, it's type 'no-conversion
649 ((or (memq (latin-unity-base-name codesys)
650 latin-unity-iso-8859-1-aliases)
651 (eq (coding-system-type codesys) 'iso2022))
652 ;; #### make sure maybe-remap always returns a coding system
653 ;; #### I thought about like-to-live-dangerously here,
654 ;; but first make sure make sure maybe-remap returns nil
656 (latin-unity-massage-name codesys buffer-default))
657 (when (and (latin-unity-maybe-remap begin end codesys
659 (not (eq (latin-unity-base-name codesys)
660 (latin-unity-base-name buffer-default))))
661 (latin-unity-maybe-set-coding-system codesys buffer-default)
664 ;; other coding systems -- eg Windows 125x, KOI8?
665 ;; #### unimplemented
667 ;; no luck, pass the buck back to `write-region'
668 (latin-unity-like-to-live-dangerously
669 (warn (concat "Passing to default coding system,"
670 " data corruption likely"))
675 "couldn't find a coding system to encode all characters"))
678 (when latin-unity-hack-cookies-enabled-p
679 (setq latin-unity-coding-cookies-found 0)
680 (latin-unity-hack-cookies-prop-line)
681 (latin-unity-hack-cookies-last-page)))))
684 ;; #### maybe this is what we want to test? add a no-ask flag.
685 (defun latin-unity-recommend-representation (begin end feasible buffer-default
687 "Recommend a representation for BEGIN to END from FEASIBLE in BUFFER.
689 Returns a cons of a coding system (which can represent all characters in
690 BUFFER) and a charset (to which all non-ASCII characters in BUFFER can be
691 remapped. (The former will be nil only if `latin-unity-ucs-list' is nil.)
693 FEASIBLE is a bitvector representing the feasible character sets.
694 BUFFER defaults to the current buffer."
696 ;; interactive not useful because of representation of FEASIBLE
697 (unless buffer (setq buffer (current-buffer)))
701 (pop-to-buffer (get-buffer-create latin-unity-help-buffer) t)
704 Choose a coding system to save buffer %s.
705 All preapproved coding systems %s
706 fail to appropriately encode some of the characters present in the buffer."
709 (if (memq x '(preferred buffer-default))
711 (latin-unity-massage-name
714 latin-unity-preapproved-coding-system-list)))
715 ;; #### break this out into a separate function for testing
716 (when latin-unity-debug
717 (insert " Character sets in the buffer are:\n\n ")
718 (latin-unity-buffer-charsets-string buffer))
721 Please pick a coding system. The following are recommended because they can
722 encode any character in the buffer:
726 (let ((sys (latin-unity-charset-feasible-system cs
730 (unless recommended (setq recommended (cons sys cs)))
731 (insert (format " %s" sys)))))
732 latin-unity-character-sets)
733 ;; universal coding systems
735 ;; User may have specified systems unavailable in this XEmacs
736 (when (find-coding-system sys)
737 (unless recommended (setq recommended (cons sys nil)))
738 (insert (format " %s" sys))))
739 latin-unity-ucs-list)
742 Note that if you select a coding system that can not encode some characters
743 in your buffer, those characters will be changed to an arbitrary replacement
744 character, by default `~', on output.
746 Page down for more information on coding systems:
748 utf-8, iso-2022-7, and ctext support all characters safely. iso-2022-7 and
749 ctext are ISO 2022 conforming coding systems for 7-bit and 8-bit environments
750 respectively. Be careful, there is a lot of software that does not understand
751 them. utf-8 (Unicode) may also be unsupported in some environments, but they
752 are becoming fewer all the time. utf-8 is recommended if usable (except for
753 some users of Asian ideographs who need to mix languages).
755 In Mule, most iso-* coding systems are capable of encoding all characters.
756 However, characters outside of the normal range for the coding system require
757 use of ISO 2022 extension techniques and is likely to be unsupported by other
758 software, including software that supports iso-2022-7 or ctext.
760 For a list of coding systems, quit this command and invoke
761 `list-coding-systems'.")
762 (goto-char (point-min))
763 ;; `read-coding-system' never returns a non-symbol
764 (let ((val (read-coding-system (format "Coding system [%s]: "
768 (if (eq val (car recommended))
770 (cons val (latin-unity-coding-system-latin-charset val)))))))
772 ;; this could be a flet in latin-unity-sanity-check
773 ;; -- no, this is what we want to regression test?
774 ;; #### this function's interface needs to change, s/codesys/charset/
775 ;; #### did you update all calls?
776 ;; #### did you update all docs?
777 (defun latin-unity-maybe-remap (begin end codesys feasible
778 &optional present no-error)
779 "Try to remap from BEGIN to END to CODESYS. Return nil on failure.
781 Return CODESYS on success. CODESYS is a real coding system or nil.
782 FEASIBLE is a cons of bitvectors indicating the set of character sets which
783 can represent all non-ASCII characters and ASCII characters, respectively,
784 in the current buffer.
785 PRESENT is a cons of bitvectors indicating the set of non-ASCII and ASCII
786 character sets, respectively, present in the current buffer.
788 Pass NO-ERROR to `latin-unity-remap-region'."
790 ;; may God bless and keep the Mule ... far away from us!
791 ;; #### We can canonicalize here with impunity. Transformations of the
792 ;; characters in the buffer will not change the representation of newline.
793 ;; It's the return value to -sanity-check that possibly needs EOL.
794 (when codesys (setq codesys (latin-unity-base-name codesys)))
795 (when (memq codesys latin-unity-iso-8859-1-aliases)
796 (setq codesys 'iso-8859-1))
798 (when latin-unity-debug
799 (message (format "%s" (list codesys feasible present))))
801 (let ((gr (latin-unity-coding-system-latin-charset codesys)))
802 (when latin-unity-debug (message (format "%s" (list codesys gr))))
805 ;; #### this should be replaced by (latin-unity-ucs-p cs), etc!!
806 ((memq codesys latin-unity-ucs-list) codesys)
807 ;; this is just an optimization, as the next arm should catch it
808 ;; note we can assume ASCII here, as if GL is JIS X 0201 Roman,
809 ;; GR will be JIS X 0201 Katakana
810 ((and (/= (cdr present) 0)
812 (= (get 'ascii 'latin-unity-flag-bit) (cdr present))
813 (= (get gr 'latin-unity-flag-bit 0) (car present)))
815 ;; we represent everything in the buffer with remapping
816 ((and (/= (logand (get 'ascii 'latin-unity-flag-bit) (cdr feasible)) 0)
817 (/= (logand (get gr 'latin-unity-flag-bit 0) (car feasible)) 0))
818 (when latin-unity-debug (message "trying remap"))
819 (latin-unity-remap-region begin end gr codesys no-error))
824 (defun latin-unity-recode-region (begin end wrong-cs right-cs)
825 "Recode characters between BEGIN and END from WRONG-CS to RIGHT-CS.
827 When called interactively, BEGIN and END are set to the beginning and
828 end, respectively, of the active region, and XEmacs prompts for WRONG-CS
831 WRONG-CS and RIGHT-CS are character sets. Characters retain the same code
832 point but the character set is changed. Only characters from WRONG-CS are
833 changed to RIGHT-CS. The identity of the character may change. Note that
834 this could be dangerous, if characters whose identities you do not want
835 changed are included in the region. This function cannot guess which
836 characters you want changed, and which should be left alone.
838 Another way to accomplish this, but using coding systems rather than character
839 sets to specify the desired recoding, is `latin-unity-recode-coding-region'.
840 That function may be faster but is somewhat more dangerous, because it may
841 recode more than one character set.
843 To change from one Mule representation to another without changing identity
844 of any characters, use `latin-unity-remap-region'."
847 (let ((begin (region-beginning))
850 (latin-unity-read-coding-system-or-charset
852 "Current character set: ")
853 (latin-unity-read-coding-system-or-charset
855 "Desired character set: "))))
859 (while (< (point) end)
860 (let ((split (split-char (char-after))))
861 (if (eq (car split) wrong-cs)
862 ;; this order preserves marker and extent endpoints
864 (insert (apply #'make-char (cons right-cs (cdr split))))
870 (defun latin-unity-recode-coding-region (begin end wrong-cs right-cs)
871 "Recode text between BEGIN and END from WRONG-CS to RIGHT-CS.
873 When called interactively, BEGIN and END are set to the beginning and
874 end, respectively, of the active region, and XEmacs prompts for WRONG-CS
877 WRONG-CS and RIGHT-CS are coding systems. Characters retain the same code
878 point but the character set is changed. The identity of characters may change.
879 This is an inherently dangerous function; multilingual text may be recoded in
880 unexpected ways. #### It's also dangerous because the coding systems are not
881 sanity-checked in the current implementation.
883 Another, safer, way to accomplish this, using character sets rather than coding
884 systems to specify the desired recoding, is to use `latin-unity-recode-region.
886 To change from one Mule representation to another without changing identity
887 of any characters, use `latin-unity-remap-region'."
890 (let ((begin (region-beginning))
893 (latin-unity-read-coding-system-or-charset
895 "Current coding system: ")
896 (latin-unity-read-coding-system-or-charset
898 "Desired coding system: "))))
900 (encode-coding-region begin end wrong-cs)
901 (decode-coding-region begin end right-cs))
905 (defun latin-unity-remap-region (begin end character-set
906 &optional coding-system no-error)
907 "Remap characters between BEGIN and END to equivalents in CHARACTER-SET.
908 Optional argument CODING-SYSTEM may be a coding system name (a symbol) or
909 nil. Characters with no equivalent are left as-is.
911 When called interactively, BEGIN and END are set to the beginning and
912 end, respectively, of the active region, and XEmacs prompts for
915 Return CODING-SYSTEM if CODING-SYSTEM can encode all characters in the
916 region, t if CODING-SYSTEM is nil and the coding system with G0 = 'ascii
917 and G1 = CHARACTER-SET can encode all characters, and otherwise nil. Note
918 that a non-null return does _not_ mean it is safe to write the file, only
919 the specified region. (This behavior is useful for multipart MIME encoding
922 Interactively BEGIN and END are set to the current region and the function
923 prompts for CHARACTER-SET. There is no way to specify CODING-SYSTEM, as it
924 has no useful function interactively.
926 Note: by default this function is quite fascist about universal coding
927 systems. It only admits utf-8, iso-2022-7, and ctext. Customize
928 `latin-unity-ucs-list' to change this.
930 This function remaps characters that are artificially distinguished by Mule
931 internal code. It may change the code point as well as the character set.
932 To recode characters that were decoded in the wrong coding system, use
933 `latin-unity-recode-region'."
936 (let ((begin (region-beginning))
939 (latin-unity-read-coding-system-or-charset 'charset
940 "Character set: "))))
944 ;; #### we're not even gonna try if we're in an auto-save
946 (narrow-to-region begin end)
947 (goto-char (point-min))
949 ;; #### RFE: optimize using skip-chars-forward
950 (let* ((ch (char-after))
951 (repch (latin-unity-equivalent-character ch character-set)))
958 (let ((remaining (delq character-set
960 ;; #### this function is slow!
961 (charsets-in-region begin end)))))
962 (when (or remaining latin-unity-debug)
963 (message (format "Could not remap characters from %s to %s"
964 remaining character-set)))
965 (cond ((memq coding-system latin-unity-ucs-list) coding-system)
968 (cdr (assq coding-system latin-unity-cset-codesys-alist))
969 ;; #### Is this the right thing to do here?
971 (t (unless no-error (error 'args-out-of-range
972 "Remap failed; can't save!")))))
976 (defun latin-unity-read-coding-system-or-charset (target-type &optional prompt)
977 "Handle user input of coding system or charset names with guessing.
979 Returns a coding-system name or charset name according to TARGET-TYPE.
980 Prompt with optional PROMPT, which defaults to \"Enter TARGET-TYPE: \".
982 Uses `latin-unity-guess-coding-system' to \"guess\" an appropriate coding
983 system from a charset name and vice versa (via `latin-unity-guess-charset').
984 These functions also consult alias lists."
986 (unless (memq target-type '(coding-system charset))
987 (error 'args-out-of-range "wanted 'coding-system or 'charset"
990 (let ((prompt (if (stringp prompt)
992 (format "Enter %s name: " target-type))))
993 (flet ((typecheck (x)
994 (funcall (intern (format "find-%s" target-type)) x))
996 (funcall (intern (format "latin-unity-guess-%s" target-type)) x)))
997 (let ((obj (intern (completing-read prompt obarray #'typecheck))))
998 (while (not (typecheck obj))
999 (setq obj (guess obj))
1000 (cond ((not (typecheck obj))
1001 (setq obj (intern (completing-read (concat "Oops! " prompt)
1002 obarray #'typecheck))))
1003 ((y-or-n-p (format "Guessing %s. OK? " obj)) obj)
1008 (defun latin-unity-guess-charset (candidate)
1009 "Guess a charset based on the symbol CANDIDATE.
1011 CANDIDATE itself is not tried as the value.
1013 Uses the natural mapping in `latin-unity-cset-codesys-alist', and the values
1014 in `latin-unity-charset-alias-alist'."
1015 (let* ((indirect (cdr (assq candidate
1016 latin-unity-coding-system-alias-alist)))
1018 (cond ((not (symbolp candidate))
1019 (error 'wrong-type-argument "Not a symbol" candidate))
1020 ;; #### Use latin-unity-coding-system-latin-charset here?
1021 ((find-coding-system candidate)
1022 (car (rassq candidate latin-unity-cset-codesys-alist)))
1023 ((find-coding-system indirect)
1024 (car (rassq indirect latin-unity-cset-codesys-alist)))
1025 (t (cdr (assq candidate latin-unity-charset-alias-alist))))))
1026 (when (find-charset charset)
1029 (defun latin-unity-guess-coding-system (candidate)
1030 "Guess a coding system based on the symbol CANDIDATE.
1032 CANDIDATE itself is not tried as the value.
1034 Uses the natural mapping in `latin-unity-cset-codesys-alist', and the values
1035 in `latin-unity-coding-system-alias-alist'.
1037 Returns a symbol naming a coding system, or t to mean \"not a coding system\".
1038 \(Horrible, but Mule interprets nil as a spelling of 'binary.)"
1040 (let* ((indirect (cdr (assq candidate latin-unity-charset-alias-alist)))
1042 (cond ((not (symbolp candidate))
1043 (error 'wrong-type-argument "Not a symbol" candidate))
1044 ((find-charset candidate)
1045 (cdr (assq candidate latin-unity-cset-codesys-alist)))
1046 ((find-charset indirect)
1047 (cdr (assq indirect latin-unity-cset-codesys-alist)))
1048 ((cdr (assq candidate latin-unity-coding-system-alias-alist)))
1050 (when (find-coding-system coding-system)
1053 ;; The logic for the latin-unity-hack-cookies-* searches is of course
1054 ;; ripp'd untimely from the hack-local-variables-* stuff in files.el.
1056 ;; #### probably this function should be hacked like the prop-line version
1057 ;; #### possibly this function should error on syntax errors (this doesn't
1058 ;; prevent loading the file, but does prevent saving it)
1059 (defun latin-unity-hack-cookies-last-page (&optional force)
1060 "Find a coding cookie in the local variables block on the last page.
1061 Warn that XEmacs doesn't support coding cookies there.
1062 If found and it differs from `buffer-file-coding-system', ask the user if
1063 if the coding cookie should be changed. If optional argument FORCE is
1064 non-nil, fix the cookie without prompt.
1065 #### Probably there should be an argument for the coding system to set."
1069 (goto-char (point-max))
1070 (search-backward "
\f" (max (- (point-max) 3000) (point-min)) 'move)
1071 (when (let ((case-fold-search t))
1072 (and (search-forward "\\<Local Variables:" nil t)
1073 (if (not (search-forward "\\<Local Variables:" nil t))
1075 (warn "Two local variables sections found, ignoring.")
1078 problems prefix prefixlen suffix)
1079 ;; The prefix is what comes before "local variables:" in its line.
1080 ;; The suffix is what comes after "local variables:" in its line.
1081 ;; Whitespace immediately preceding "local variables:" _is_ part
1082 ;; of prefix; whitespace immediately following "local variables:"
1083 ;; _is not_ part of suffix. This means that you can have more
1084 ;; indentation than "local variables:" has, but not less, while
1085 ;; you can pad the suffix with whitespace for nice alignment.
1086 (skip-chars-forward " \t")
1088 (setq suffix (buffer-substring (point)
1089 (progn (end-of-line) (point)))))
1090 (goto-char (match-beginning 0))
1093 (buffer-substring (point)
1094 (progn (beginning-of-line) (point)))))
1095 (if prefix (setq prefixlen (length prefix)
1096 prefix (regexp-quote prefix)))
1097 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
1099 ;; Look at next local variable spec.
1100 (if selective-display
1101 (re-search-forward "[\n\C-m]")
1103 ;; Skip the prefix, if any.
1105 (if (looking-at prefix)
1106 (forward-char prefixlen)
1108 (list "Local variables entry is missing the prefix"))))
1109 ;; Find the variable name; strip whitespace.
1110 (skip-chars-forward " \t")
1111 (let (var val head tail)
1112 ;; #### need to use lisp-mode syntax table here;
1113 ;; 2d arg of char-syntax
1114 ;; #### need to hack eol here
1115 (if (or (eql (char-after) ?:)
1116 (not (eql (char-syntax (char-after)) ?_)))
1117 (setq problems (cons "no local variable found" problems))
1118 (setq var (read (current-buffer)))
1119 ;; magic cookies suck AND SWALLOW!
1120 ;; #### I'll be damned if I'll worry about colon-terminated
1121 ;; variable names here -- what if there are MULTIPLE COLONS?
1122 (let ((name (symbol-name var)))
1123 (setq var (if (eql ?: (aref name (1- (length name))))
1124 (forward-char -1) ; back up over colon
1125 (substring name 0 -1)
1127 (skip-chars-forward " \t")
1128 (if (equal (char-after) ?:)
1130 (setq problems (cons "Missing colon in local variables entry"
1132 ;; end: probably doesn't have a value
1133 (if (string-equal "end" var)
1135 ;; read the variable value.
1136 (skip-chars-forward " \t")
1137 ;; check for effective end-of-line
1139 (looking-at "\\s<\\|\\s1\\s2\\|\\s5\\s6"))
1140 (setq problems (cons "no value found for local variable"
1143 ;; #### this can error on a syntax error (eg "( . nil)")
1144 (setq val (read (current-buffer)))
1146 (skip-chars-forward " \t")
1147 (unless (if suffix (looking-at suffix) (eolp))
1149 (cons "Local variables entry has incorrect suffix"
1152 (problems (while problems
1153 (warn (car problems))
1154 (setq problems (cdr problems)))
1155 (setq continue nil))
1156 ((string-match "coding" var)
1157 (warn "Coding cookie in local variables unsupported")
1158 (latin-unity-hack-coding-cookie val head tail force))))
1161 (defun latin-unity-hack-cookies-prop-line (&optional force)
1162 "Find a coding cookie in the first (non-shebang) line of the file.
1163 If found and it differs from `buffer-file-coding-system', ask the user if
1164 if the coding cookie should be changed. If optional argument FORCE is
1165 non-nil, fix the cookie without prompt.
1166 #### Probably there should be an argument for the coding system to set."
1170 (goto-char (point-min))
1171 (skip-chars-forward " \t\n\r") ; does exec(2) gobble leading space?
1172 (let ((end (save-excursion
1173 ;; If the file begins with "#!"
1174 ;; (un*x exec interpreter magic), look
1175 ;; for mode frobs in the first two
1176 ;; lines. You cannot necessarily
1177 ;; put them in the first line of
1178 ;; such a file without screwing up
1179 ;; the interpreter invocation.
1180 (end-of-line (if (looking-at "^#!") 2 1))
1182 ;; Parse the -*- line into the `result' alist.
1183 (let* ((stx (and (search-forward "-*-" end t) (point)))
1184 ;; if there are more than two "-*-", use the first two
1185 (etx (and stx (search-forward "-*-" end t) (- (point) 3))))
1186 ;; insist on correct format and silently ignore otherwise
1189 ((null etx) (warn "unterminated prop line, ignoring"))
1191 (while (re-search-forward "coding:[ \t]*" etx t)
1192 (goto-char (match-end 0))
1193 (let* ((head (point))
1194 (val (read (current-buffer)))
1196 (latin-unity-hack-coding-cookie val head tail force))))))))))
1198 (defun latin-unity-hack-coding-cookie (value begin end &optional force)
1199 "Fixup a coding cookie.
1200 If VALUE differs from `buffer-file-coding-system', ask the user if the
1201 coding cookie found between BEGIN and END should be changed. If optional
1202 argument FORCE is non-nil, fix the cookie without prompt.
1203 #### Probably there should be an argument for the coding system to set."
1204 (setq latin-unity-coding-cookies-found
1205 (1+ latin-unity-coding-cookies-found))
1206 (when (> latin-unity-coding-cookies-found 1)
1207 (warn "%d coding cookies found; you should have only one."
1208 latin-unity-coding-cookies-found))
1209 (let ((bfcs (latin-unity-base-name buffer-file-coding-system)))
1210 (unless (eq value bfcs)
1213 (format "Incorrect coding cookie %S found. Replace with %S? "
1217 (delete-region begin end)
1221 (defun latin-unity-example ()
1222 "An example of the latin-unity package.
1224 At present it just makes a multilingual buffer. To test, setq
1225 buffer-file-coding-system to some value, make the buffer dirty (eg
1226 with RET BackSpace), and save."
1229 (switch-to-buffer (get-buffer-create "latin-unity example"))
1231 (insert "From here ...\n")
1232 (insert "Latin-1: f")
1233 (insert (make-char 'latin-iso8859-1 #xFC)) ; u diaeresis, also in Latin-2
1234 (insert "r\n\nLatin-2: Nik") ; my apologies if I misremembered
1235 (insert (make-char 'latin-iso8859-2 #xB9)) ; s caron, not in Latin-1
1237 (insert (make-char 'latin-iso8859-2 #xE6)) ; c acute, not in Latin-1
1238 (insert "\n... to here is representable in Latin-2 but not Latin-1.\n")
1239 (insert (make-char 'latin-iso8859-1 #xFF)) ; y daieresis, not in Latin-2
1240 (insert "\nFrom top to here is not representable in Latin-[12].\n")
1243 By deleting various portions of the buffer and saving, or by setq'ing
1244 buffer-file-coding-system and saving, you can see how the thing works.
1245 After compiling and loading the file, do (by hand)
1247 M-: (latin-unity-install) RET.
1249 To see a trace of what it's doing (you need to read the code to interpret),
1250 to get more information about character sets in the region, and to enable
1251 some sledgehammer error checks
1253 M-: (setq latin-unity-debug t) RET
1255 To disable the hook, do
1257 M-: (latin-unity-uninstall) RET.
1259 Note: the *install functions are interactive---you can execute with M-x.
1260 I wrote them as above so you can C-x C-e them in this buffer.
1263 ;;; end of latin-unity.el