Remove non-free old and crusty clearcase pkg
[packages] / mule-packages / latin-unity / latin-unity.el
1 ;;; latin-unity.el --- Identify equivalent characters in the ISO Latin sets
2
3 ;; Copyright (C) 2002 Free Software Foundation, Inc
4
5 ;; Author: Stephen J. Turnbull
6 ;; Keywords: mule, charsets
7 ;; Created: 2002 January 17
8 ;; Last-modified: 2003 August 9
9
10 ;; This file is part of XEmacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27
28 ;;; Commentary:
29
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.
35
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.
39
40 ;;; Code:
41
42 (provide 'latin-unity)
43
44
45 ;;; Requires
46
47 (require 'latin-unity-vars)
48
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
53
54
55 ;;; User customization
56
57 (defgroup latin-unity nil
58   "Handle equivalent ISO-8859 characters properly (identify them) on output."
59   :group 'mule)
60
61 (defcustom latin-unity-preapproved-coding-system-list
62   '(buffer-default preferred)
63   "*List of coding systems used without querying the user if feasible.
64
65 The first feasible coding system in this list is used.  The special values
66 'preferred and 'buffer-default may be present:
67
68   buffer-default  Use the coding system used by `write-region', if feasible.
69   preferred       Use the coding system specified by `prefer-coding-system'
70                   if feasible.
71
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'.
76
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.)
83
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)
91   :group 'latin-unity)
92
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.
96
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.
102
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'
106                   if feasible.
107
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)
113   :group 'latin-unity)
114
115 (defcustom latin-unity-ucs-list '(utf-8 iso-2022-7 ctext escape-quoted)
116   "*List of coding systems considered to be universal.
117
118 A universal coding system can represent all characters by definition.
119
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
125 coding systems.
126
127 If none of the preferred coding systems are feasible, the first in
128 this list will be the default.
129
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
132 Lisp files.
133
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)
143   :group 'latin-unity)
144
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).
155
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
159 charset)."
160   :type '(repeat (cons symbol symbol))
161   :group 'latin-unity)
162
163 (defcustom latin-unity-coding-system-alias-alist nil
164   "*Alist mapping aliases to Mule coding system names.
165
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))
170   :group 'latin-unity)
171
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.
175
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)
179   :group 'latin-unity)
180
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."
184   :type 'string
185   :group 'latin-unity)
186
187 (defcustom latin-unity-hack-cookies-enabled-p t
188   "If non-nil, `latin-unity-sanity-check' validates coding cookies."
189   :type 'boolean
190   :group 'latin-unity)
191
192 (defcustom latin-unity-like-to-live-dangerously nil
193   "Convert failure to remap buffer from error to warning.
194
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."
198   :type 'boolean
199   :group 'latin-unity)
200
201 (defcustom latin-unity-may-set-coding-flag 'ask
202   "When may latin-unity reset `buffer-file-coding-system'?
203
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."
209   :type '(choice
210           (const nil  :doc "never change, return silently")
211           (const t    :doc "always change, return silently")
212           (const regrets-only
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"))
216   :group 'latin-unity)
217
218 ;;; User interface
219
220 ;; Install/uninstall
221
222 ;;;###autoload
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."
226
227   (interactive)
228
229   (add-hook 'write-region-pre-hook 'latin-unity-sanity-check))
230
231 ;;;###autoload
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."
235
236   (interactive)
237
238   (remove-hook 'write-region-pre-hook 'latin-unity-sanity-check))
239
240
241 ;;; Implementation
242
243 ;; Internal variables
244
245 (defvar latin-unity-coding-cookies-found 0
246   "Internal variable.")
247
248 ;; Utilities
249
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.
254
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.
258
259 Does not check the aliases variables."
260   (coding-system-name
261    (cond ((eq x 'buffer-default) coding-system)
262          ((eq x 'preferred)
263           (coding-system-name ; #### nil arg -> binary, is this OK?
264            (coding-category-system (car (coding-priority-list)))))
265          (t x))))
266
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.
275
276 The base name is a symbol naming the similar coding system with no EOL
277 convention."
278   (coding-system-name (coding-system-base (find-coding-system cs))))
279
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.
282
283 Returns the list of charsets.
284
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.
288
289 This is a debugging function; don't depend on its behavior."
290   (mapc (lambda (cs) (insert (format " %s" cs)))
291         (save-excursion
292           (set-buffer buffer)
293           (save-restriction
294             (widen)
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))))))
299
300 (defsubst latin-unity-charset-feasible-system (charset bvector buffer-default)
301   "Return a feasible coding-system based on CHARSET and BVECTOR, or nil.
302
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))
314          sys)))
315
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))
319       (and coding-system
320            (eq (coding-system-type coding-system) 'iso2022)
321            (coding-system-property coding-system 'charset-g1))))
322
323
324 (defun latin-unity-list-coding-systems (display-excluded)
325   "Display the coding systems listed by priority and group.
326
327 With prefix argument, also display otherwise excluded coding systems.
328
329 See also `latin-unity-preapproved-coding-systems',
330 `latin-unity-preferred-coding-systems', and `latin-unity-ucs-list'."
331
332   (interactive "_P")
333
334   (save-excursion
335     (pop-to-buffer (get-buffer-create
336                     latin-unity-coding-system-list-buffer))
337     (erase-buffer)
338     (let ((start (point)))
339
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))
344
345       (insert "\nSuggested coding systems:\n ")
346       (setq start (point))
347       (mapc (lambda (codesys) (insert (format " %s" codesys)))
348             latin-unity-preferred-coding-system-list)
349       (fill-region start (point))
350
351       (insert "\nUniversal coding systems:\n ")
352       (setq start (point))
353       (mapc (lambda (codesys) (insert (format " %s" codesys)))
354             latin-unity-ucs-list)
355       (fill-region start (point))
356
357       (when display-excluded
358         ;; Should arrange to only display excluded ones!
359         (insert "\nAll coding systems:\n ")
360         (setq start (point))
361         (mapc (lambda (codesys) (insert (format " %s" codesys)))
362               (coding-system-list))
363         (fill-region start (point))))))
364
365 ;; Accessors for character and charset equivalences
366
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))
371
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)))
377
378 ;; Buffer coding system feasibility
379
380 ;;;###autoload
381 (defun latin-unity-representations-feasible-buffer ()
382   "Apply latin-unity-representations-feasible-region to the current buffer."
383   (interactive)
384   (latin-unity-representations-feasible-region (point-min)
385                                                (point-max)
386                                                (current-buffer)))
387
388 ;; latin-unity-representations-feasible-region
389 ;;
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.
393 ;;
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.
397 ;;
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.)
402 ;;
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
406 ;; found.
407 ;;
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.
412
413 ;;;###autoload
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.
416
417 BUF defaults to the current buffer.  Called interactively, will be
418 applied to the region.  Function assumes BEGIN <= END.
419
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."
424
425   (interactive "r")
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)))
429     (save-excursion
430       (set-buffer (or buf (current-buffer)))
431       (save-restriction
432         (widen)
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)))))
437           (goto-char begin)
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)
447                          (eq cs 'ascii))
448                      (setq asciisets
449                            (logand asciisets (latin-unity-feasible-charsets ch)
450                                    )))
451                     (t
452                      (setq latinsets
453                            (logand latinsets (latin-unity-feasible-charsets ch)
454                                    )))))
455             (forward-char)
456             ;; The characters skipped here can't change asciisets
457             (skip-chars-forward latin-unity-ascii-and-jis-roman)))))
458     (cons latinsets asciisets)))
459
460
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.
465
466 The car indicates which Latin characters sets were found, the cdr the ASCII
467 character sets.  BUFFER defaults to the current buffer."
468
469   (let ((lsets 0)
470         (asets 0)
471         (skipchars ""))
472     (save-excursion
473       (set-buffer (or buffer (current-buffer)))
474       (save-restriction
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))
481         (while (not (eobp))
482           (let* ((ch (char-after))
483                  (cs (car (split-char ch)))
484                  (flag (get cs 'latin-unity-flag-bit 0)))
485             (cond
486              ((eq cs 'ascii)
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)))
494              ;; Control-1 hack
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.
505              ((eq cs 'control-1)
506               ;; minor optimization
507               (setq skipchars (concat "\200-\237" skipchars)))
508              (t
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)))
512               (if (= flag 0)
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))))
517     (cons lsets asets)))
518
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
528     ((nil))
529     ((t)
530      (set-buffer-file-coding-system coding-system))
531     ((regrets-only)
532      (message "Specified coding system used to save, but default not changed."))
533     ((warn)
534      (set-buffer-file-coding-system coding-system)
535      (message "Specified coding system used to save, and default changed."))
536     ((ask)
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? "
542                               coding-system))
543             (set-buffer-file-coding-system coding-system)
544             (message "Specified coding system used, and default changed."))
545            (t (message
546                "Specified coding system used, but default not changed."))))
547     (otherwise
548      (message (format "Unknown value for latin-unity-may-set-coding-flag: %s."
549                       latin-unity-may-set-coding-flag)))))
550
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.
558
559 ;;;###autoload
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.
563
564 If not, attempt to remap Latin characters to a single Latin-N set.
565
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.
569
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'.
575
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.
580
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
584 `write-region')."
585
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))
590       nil
591     (prog1
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?
596             (or coding-system
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)))
605
606         (when latin-unity-debug (message "%s %s" csets psets) (sit-for 1))
607
608         (cond
609          ;; try the preapproved systems
610          ((catch 'done
611             (let ((systems latin-unity-preapproved-coding-system-list))
612               ;; while always returns nil
613               (while systems
614                 (let ((sys (latin-unity-massage-name (car systems)
615                                                      buffer-default)))
616                   (when latin-unity-debug (message "sys is %s" sys))
617                   (when (latin-unity-maybe-remap begin end sys
618                                                  csets psets t)
619                     (when latin-unity-debug (message "throwing %s" sys))
620                     (throw 'done 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
628                                                           buffer-default))
629                    (codesys (car recommended))
630                    ;(charset (cdr recommended)) ; unused?
631                    )
632               (when latin-unity-debug (message "%s" recommended))
633               ;; compute return
634               (cond
635
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))
643                 codesys)
644
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
655                 (setq codesys
656                       (latin-unity-massage-name codesys buffer-default))
657                 (when (and (latin-unity-maybe-remap begin end codesys
658                                                     csets psets nil)
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)
662                   codesys))
663
664                ;; other coding systems -- eg Windows 125x, KOI8?
665                ;; #### unimplemented
666
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"))
671                 (ding)
672                 nil)
673                (t (error
674                    'coding-system-error
675                    "couldn't find a coding system to encode all characters"))
676                )))
677          ))
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)))))
682
683
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
686                                              &optional buffer)
687   "Recommend a representation for BEGIN to END from FEASIBLE in BUFFER.
688
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.)
692
693 FEASIBLE is a bitvector representing the feasible character sets.
694 BUFFER defaults to the current buffer."
695
696   ;; interactive not useful because of representation of FEASIBLE
697   (unless buffer (setq buffer (current-buffer)))
698
699   (let (recommended)
700     (save-excursion
701       (pop-to-buffer (get-buffer-create latin-unity-help-buffer) t)
702       (erase-buffer)
703       (insert (format "\
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."
707                       (buffer-name buffer)
708                       (mapcar (lambda (x)
709                                 (if (memq x '(preferred buffer-default))
710                                     (format "%s==%s" x
711                                             (latin-unity-massage-name
712                                              x buffer-default))
713                                   x))
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))
719       (insert "
720
721 Please pick a coding system.  The following are recommended because they can
722 encode any character in the buffer:
723
724    ")
725       (mapc (lambda (cs)
726               (let ((sys (latin-unity-charset-feasible-system cs
727                                                               (car feasible)
728                                                               buffer-default)))
729                 (when sys
730                   (unless recommended (setq recommended (cons sys cs)))
731                   (insert (format " %s" sys)))))
732             latin-unity-character-sets)
733       ;; universal coding systems
734       (mapc (lambda (sys)
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)
740       (insert "
741
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.
745
746 Page down for more information on coding systems:
747
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).
754
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.
759
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]: "
765                                              (car recommended))
766                                      (car recommended))))
767         (delete-window)
768         (if (eq val (car recommended))
769             recommended
770           (cons val (latin-unity-coding-system-latin-charset val)))))))
771
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.
780
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.
787
788 Pass NO-ERROR to `latin-unity-remap-region'."
789
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))
797
798   (when latin-unity-debug
799     (message (format "%s" (list codesys feasible present))))
800
801   (let ((gr (latin-unity-coding-system-latin-charset codesys)))
802     (when latin-unity-debug (message (format "%s" (list codesys gr))))
803     (cond
804      ((null codesys) nil)
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)
811            (/= (car present) 0)
812            (= (get 'ascii 'latin-unity-flag-bit) (cdr present))
813            (= (get gr 'latin-unity-flag-bit 0) (car present)))
814       codesys)
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))
820      (t nil))))
821
822
823 ;;;###autoload
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.
826
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
829 and RIGHT-CS.
830
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.
837
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.
842
843 To change from one Mule representation to another without changing identity
844 of any characters, use `latin-unity-remap-region'."
845
846   (interactive
847    (let ((begin (region-beginning))
848          (end (region-end)))
849      (list begin end
850            (latin-unity-read-coding-system-or-charset
851             'charset
852             "Current character set: ")
853            (latin-unity-read-coding-system-or-charset
854             'charset
855             "Desired character set: "))))
856
857   (save-excursion
858     (goto-char begin)
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
863             (progn
864               (insert (apply #'make-char (cons right-cs (cdr split))))
865               (delete-char))
866           (forward-char))))))
867
868
869 ;;;###autoload
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.
872
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
875 and RIGHT-CS.
876
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.
882
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.
885
886 To change from one Mule representation to another without changing identity
887 of any characters, use `latin-unity-remap-region'."
888
889   (interactive
890    (let ((begin (region-beginning))
891          (end (region-end)))
892      (list begin end
893            (latin-unity-read-coding-system-or-charset
894             'coding-system
895             "Current coding system: ")
896            (latin-unity-read-coding-system-or-charset
897             'coding-system
898             "Desired coding system: "))))
899
900   (encode-coding-region begin end wrong-cs)
901   (decode-coding-region begin end right-cs))
902
903
904 ;;;###autoload
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.
910
911 When called interactively, BEGIN and END are set to the beginning and
912 end, respectively, of the active region, and XEmacs prompts for
913 CHARACTER-SET.
914
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
920 and the like.)
921
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.
925
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.
929
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'."
934
935   (interactive
936    (let ((begin (region-beginning))
937          (end (region-end)))
938      (list begin end
939            (latin-unity-read-coding-system-or-charset 'charset
940                                                       "Character set: "))))
941
942   (save-excursion
943     (save-restriction
944       ;; #### we're not even gonna try if we're in an auto-save
945       (when begin
946         (narrow-to-region begin end)
947         (goto-char (point-min))
948         (while (not (eobp))
949           ;; #### RFE: optimize using skip-chars-forward
950           (let* ((ch (char-after))
951                  (repch (latin-unity-equivalent-character ch character-set)))
952             (if (or (not repch)
953                     (= repch ch))
954                 (forward-char 1)
955               (insert repch)
956               (delete-char 1))))
957
958         (let ((remaining (delq character-set
959                                (delq 'ascii
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)
966                 ((null remaining)
967                  (or coding-system
968                      (cdr (assq coding-system latin-unity-cset-codesys-alist))
969                      ;; #### Is this the right thing to do here?
970                      t))
971                 (t (unless no-error (error 'args-out-of-range
972                                            "Remap failed; can't save!")))))
973         ))))
974
975
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.
978
979 Returns a coding-system name or charset name according to TARGET-TYPE.
980 Prompt with optional PROMPT, which defaults to \"Enter TARGET-TYPE: \".
981
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."
985
986   (unless (memq target-type '(coding-system charset))
987     (error 'args-out-of-range "wanted 'coding-system or 'charset"
988            target-type))
989
990   (let ((prompt (if (stringp prompt)
991                     prompt
992                   (format "Enter %s name: " target-type))))
993     (flet ((typecheck (x)
994              (funcall (intern (format "find-%s" target-type)) x))
995            (guess (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)
1004                 (t (setq obj t))))
1005         obj))))
1006
1007
1008 (defun latin-unity-guess-charset (candidate)
1009   "Guess a charset based on the symbol CANDIDATE.
1010
1011 CANDIDATE itself is not tried as the value.
1012
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)))
1017          (charset
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)
1027       charset)))
1028
1029 (defun latin-unity-guess-coding-system (candidate)
1030   "Guess a coding system based on the symbol CANDIDATE.
1031
1032 CANDIDATE itself is not tried as the value.
1033
1034 Uses the natural mapping in `latin-unity-cset-codesys-alist', and the values
1035 in `latin-unity-coding-system-alias-alist'.
1036
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.)"
1039
1040   (let* ((indirect (cdr (assq candidate latin-unity-charset-alias-alist)))
1041          (coding-system
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)))
1049                 (t t))))
1050     (when (find-coding-system coding-system)
1051       coding-system)))
1052
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.
1055
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."
1066   (save-excursion
1067     (save-restriction
1068       (widen)
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))
1074                        t
1075                      (warn "Two local variables sections found, ignoring.")
1076                      nil)))
1077         (let ((continue t)
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")
1087           (or (eolp)
1088               (setq suffix (buffer-substring (point)
1089                                              (progn (end-of-line) (point)))))
1090           (goto-char (match-beginning 0))
1091           (or (bolp)
1092               (setq prefix
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) "$")))
1098           (while continue
1099             ;; Look at next local variable spec.
1100             (if selective-display
1101                 (re-search-forward "[\n\C-m]")
1102               (forward-line 1))
1103             ;; Skip the prefix, if any.
1104             (if prefix
1105                 (if (looking-at prefix)
1106                     (forward-char prefixlen)
1107                   (setq problems
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)
1126                               name)))
1127                 (skip-chars-forward " \t")
1128                 (if (equal (char-after) ?:)
1129                     (forward-char 1)
1130                   (setq problems (cons "Missing colon in local variables entry"
1131                                        problems))))
1132               ;; end: probably doesn't have a value
1133               (if (string-equal "end" var)
1134                   (setq continue nil)
1135                 ;; read the variable value.
1136                 (skip-chars-forward " \t")
1137                 ;; check for effective end-of-line
1138                 (if (or (eolp)
1139                         (looking-at "\\s<\\|\\s1\\s2\\|\\s5\\s6"))
1140                     (setq problems (cons "no value found for local variable"
1141                                          problems))
1142                   (setq head (point))
1143                   ;; #### this can error on a syntax error (eg "( . nil)")
1144                   (setq val (read (current-buffer)))
1145                   (setq tail (point))
1146                   (skip-chars-forward " \t")
1147                   (unless (if suffix (looking-at suffix) (eolp))
1148                     (setq problems
1149                           (cons "Local variables entry has incorrect suffix"
1150                                 problems))))
1151                 (cond
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))))
1159               )))))))
1160
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."
1167   (save-excursion
1168     (save-restriction
1169       (widen)
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))
1181                    (point))))
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
1187           (cond
1188            ((null stx) nil)
1189            ((null etx) (warn "unterminated prop line, ignoring"))
1190            (t (goto-char stx)
1191               (while (re-search-forward "coding:[ \t]*" etx t)
1192                 (goto-char (match-end 0))
1193                 (let* ((head (point))
1194                        (val (read (current-buffer)))
1195                        (tail (point)))
1196                   (latin-unity-hack-coding-cookie val head tail force))))))))))
1197
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)
1211       (when (or force
1212                 (y-or-n-p
1213                  (format "Incorrect coding cookie %S found.  Replace with %S? "
1214                          value bfcs)))
1215         (save-excursion
1216           (goto-char begin)
1217           (delete-region begin end)
1218           (insert bfcs))))))
1219
1220 ;;;###autoload  
1221 (defun latin-unity-example ()
1222   "An example of the latin-unity package.
1223
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."
1227
1228   (interactive)
1229   (switch-to-buffer (get-buffer-create "latin-unity example"))
1230   (erase-buffer)
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
1236   (insert ?i)
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")
1241
1242   (insert "
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)
1246
1247 M-: (latin-unity-install) RET.
1248
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
1252
1253 M-: (setq latin-unity-debug t) RET
1254
1255 To disable the hook, do
1256
1257 M-: (latin-unity-uninstall) RET.
1258
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.
1261 "))
1262
1263 ;;; end of latin-unity.el