Commit | Line | Data |
---|---|---|
ab87fc3d | 1 | ;;; ogonek.el --- change the encoding of Polish diacritics |
12365826 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1997-1998, 2001-2011 Free Software Foundation, Inc. |
12365826 | 4 | |
eb2ffb18 GM |
5 | ;; Author: W{\l}odek Bzyl |
6 | ;; Ryszard Kubiak | |
7 | ;; Maintainer: Ryszard Kubiak <rysiek@ipipan.gda.pl> | |
12365826 RS |
8 | ;; Keywords: i18n |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
4936186e | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
12365826 | 13 | ;; it under the terms of the GNU General Public License as published by |
4936186e GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
12365826 RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
4936186e | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
12365826 RS |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; To use this library load it using | |
28 | ;; M-x load-library [enter] ogonek | |
29 | ;; Then, you may get a short info by calling one of | |
a1506d29 | 30 | ;; M-x ogonek-jak -- in Polish |
12365826 RS |
31 | ;; M-x ogonek-how -- in English " |
32 | ||
3413eafa SE |
33 | ;;; Code: |
34 | ||
35 | (defgroup ogonek nil | |
36 | "Change the encoding of Polish diacritic characters." | |
37 | :prefix "ogonek-" | |
38 | :group 'i18n) | |
39 | ||
12365826 | 40 | (defconst ogonek-name-encoding-alist |
a1506d29 JB |
41 | '(("ascii" . (?A ?C ?E ?L ?N ?O ?S ?Z ?Z |
42 | ?a ?c ?e ?l ?n ?o ?s ?z ?z)) | |
43 | ("iso8859-2" . (161 198 202 163 209 211 166 172 175 | |
44 | 177 230 234 179 241 243 182 188 191)) | |
45 | ("mazovia" . (143 149 144 156 165 163 152 160 161 | |
12365826 | 46 | 134 141 145 146 164 162 158 166 167)) |
a1506d29 | 47 | ("windows-EE" . (165 198 202 163 209 211 140 143 175 |
12365826 | 48 | 185 230 234 179 241 243 156 159 191)) |
a1506d29 | 49 | ("windows-PL" . (165 198 202 163 209 211 140 143 175 |
12365826 | 50 | 185 230 234 179 241 243 156 159 191)) |
a1506d29 | 51 | ("latin-2" . (164 143 168 157 227 224 151 141 189 |
12365826 | 52 | 165 134 169 136 228 162 152 171 190)) |
a1506d29 | 53 | ("CP852" . (164 143 168 157 227 224 151 141 189 |
12365826 | 54 | 165 134 169 136 228 162 152 171 190)) |
a1506d29 | 55 | ("MeX" . (129 130 134 138 139 211 145 153 155 |
12365826 | 56 | 161 162 166 170 171 243 177 185 187)) |
a1506d29 | 57 | ("CorelDraw" . (197 242 201 163 209 211 255 225 237 |
12365826 | 58 | 229 236 230 198 241 243 165 170 186)) |
a1506d29 | 59 | ("Amiga" . (194 202 203 206 207 211 212 218 219 |
12365826 | 60 | 226 234 235 238 239 243 244 250 251)) |
a1506d29 | 61 | ("Mac" . (132 140 162 252 193 238 229 143 251 |
12365826 | 62 | 136 141 171 184 196 151 230 144 253)) |
a1506d29 | 63 | ) |
12365826 | 64 | "The constant `ogonek-name-encoding-alist' is a list of (NAME.LIST) pairs. |
f23515e1 RS |
65 | Each LIST contains codes for 18 Polish diacritic characters. The codes |
66 | are given in the following order: | |
12365826 RS |
67 | Aogonek Cacute Eogonek Lslash Nacute Oacute Sacute Zacute Zdotaccent |
68 | aogonek cacute eogonek lslash nacute oacute sacute zacute zdotaccent.") | |
69 | ||
f23515e1 | 70 | ;; ------ A Little Info in Polish --------------- |
12365826 RS |
71 | |
72 | (defconst ogonek-informacja | |
f23515e1 | 73 | " FUNKCJE INTERAKCYJNE UDOST/EPNIANE PRZEZ BIBLIOTEK/E `ogonek'. |
12365826 | 74 | |
f23515e1 | 75 | Je/sli czytasz ten tekst, to albo przegl/adasz plik /xr/od/lowy |
a1506d29 JB |
76 | biblioteki `ogonek.el', albo wywo/la/le/s polecenie `ogonek-jak'. |
77 | W drugim przypadku mo/zesz usun/a/c tekst z ekranu, stosuj/ac | |
12365826 RS |
78 | polecenie `M-x kill-buffer'. |
79 | ||
f23515e1 | 80 | Niniejsza biblioteka dostarcza funkcji do zmiany kodowania polskich |
12365826 RS |
81 | znak/ow diakrytycznych. Funkcje te mo/zna pogrupowa/c nast/epuj/aco. |
82 | ||
a1506d29 JB |
83 | 1. `ogonek-recode-region' oraz `ogonek-recode-buffer' |
84 | przekodowuj/a zaznaczony fragment wzgl/ednie ca/ly buffor. | |
12365826 | 85 | Po wywo/laniu interakcyjnym funkcji zadawane s/a |
f23515e1 | 86 | pytania o parametry przekodowania: nazw/e kodowania |
a1506d29 JB |
87 | w tek/scie /xr/od/lowym i nazw/e kodowania docelowego. |
88 | Poni/zsze przyk/lady powinny wyja/sni/c, jakich parametr/ow | |
12365826 RS |
89 | oczekuj/a wymienione funkcje: |
90 | ||
f23515e1 | 91 | (ogonek-recode-region (poczatek-fragmentu) (koniec-fragmentu) |
12365826 | 92 | nazwa-kodowania-w-tekscie-zrodlowym nazwa-kodowania-docelowa) |
a1506d29 JB |
93 | (ogonek-recode-buffer |
94 | nazwa-kodowania-w-tekscie-zrodlowym nazwa-kodowania-docelowa) | |
12365826 | 95 | |
f23515e1 RS |
96 | 2. `ogonek-prefixify-region' oraz `ogonek-prefixify-buffer' |
97 | s/lu/z/a do wprowadzania notacji prefiksowej. | |
12365826 | 98 | |
f23515e1 | 99 | (ogonek-prefixify-region (poczatek-fragmentu) (koniec-fragmentu) |
12365826 | 100 | nazwa-kodowania-w-tekscie-zrodlowym znak-prefiksu) |
a1506d29 JB |
101 | (ogonek-prefixify-buffer |
102 | nazwa-kodowania-w-tekscie-zrodlowym znak-prefiksu) | |
12365826 | 103 | |
a1506d29 | 104 | 3. `ogonek-deprefixify-region' oraz `ogonek-deprefixify-buffer' |
f23515e1 | 105 | s/lu/z/a do usuwania notacji prefiksowej. |
a1506d29 | 106 | |
f23515e1 | 107 | (ogonek-deprefixify-region (poczatek-fragmentu) (koniec-fragmentu) |
12365826 | 108 | znak-prefiksu nazwa-kodowania-docelowa) |
a1506d29 JB |
109 | (ogonek-prefixify-buffer |
110 | znak-prefiksu nazwa-kodowania-docelowa) | |
12365826 RS |
111 | |
112 | U/zycie klawisza TAB w trybie interakcyjnym powoduje wy/swietlenie | |
f23515e1 | 113 | listy dopuszczalnych nazw kod/ow, pami/etanych w sta/lej |
a1506d29 | 114 | `ogonek-name-encoding-alist'. |
12365826 | 115 | |
a1506d29 | 116 | Funkcje biblioteki odwo/luj/a si/e do pi/eciu zmiennych, kt/ore |
12365826 | 117 | przechowuj/a podpowiedzi do zadawanych pyta/n. Nazwy tych zmiennych |
a1506d29 | 118 | oraz ich warto/sci domy/slne s/a nast/epuj/ace: |
12365826 RS |
119 | |
120 | ogonek-from-encoding iso8859-2 | |
f23515e1 | 121 | ogonek-to-encoding ascii |
12365826 RS |
122 | ogonek-prefix-char / |
123 | ogonek-prefix-from-encoding iso8859-2 | |
124 | ogonek-prefix-to-encoding iso8859-2 | |
125 | ||
126 | Powy/zsze warto/sci domy/slne mo/zna zmieni/c przez umieszczenie w pliku | |
127 | konfiguracyjnym `~/.emacs' odpowiednich przypisa/n, na przyk/lad: | |
128 | ||
f23515e1 RS |
129 | (setq ogonek-prefix-char ?/) |
130 | (setq ogonek-prefix-to-encoding \"iso8859-2\") | |
12365826 RS |
131 | |
132 | Zamiast wczytywania ca/lej biblioteki `ogonek.el' mo/zna w pliku | |
a1506d29 | 133 | `~/.emacs' za/z/ada/c wczytania wybranych funkcji, na dodatek dopiero |
f23515e1 | 134 | w chwili ich rzeczywistego u/zycia: |
12365826 | 135 | |
f23515e1 RS |
136 | (autoload 'ogonek-jak \"ogonek\") |
137 | (autoload 'ogonek-recode-region \"ogonek\") | |
138 | (autoload 'ogonek-prefixify-region \"ogonek\") | |
139 | (autoload 'ogonek-deprefixify-region \"ogonek\") | |
12365826 RS |
140 | |
141 | Cz/esto wyst/epuj/ace kombinacje wywo/la/n funkcji mo/zna dla wygody | |
142 | skr/oci/c i przypisa/c klawiszom. Oto praktyczne przyk/lady: | |
143 | ||
f23515e1 RS |
144 | (defun deprefixify-iso8859-2-region (start end) |
145 | (interactive \"*r\") | |
146 | (ogonek-deprefixify-region start end ?/ \"iso8859-2\")) | |
147 | (global-set-key \"\\C-cd\" 'deprefixify-iso8859-2-region) ; ctrl-c d | |
12365826 | 148 | |
a1506d29 | 149 | (defun mazovia-to-iso8859-2 (start end) |
f23515e1 RS |
150 | (interactive \"*r\") |
151 | (ogonek-recode-region start end \"mazovia\" \"iso8859-2\")) | |
152 | (global-set-key \"\\C-cr\" 'mazovia-to-iso8859-2) ; ctrl-c r | |
12365826 | 153 | |
f23515e1 RS |
154 | (defun prefixify-iso8859-2-region (start end) |
155 | (interactive \"*r\") | |
156 | (ogonek-prefixify-region start end \"iso8859-2\" ?/)) | |
157 | (global-set-key \"\\C-cp\" 'prefixify-iso8859-2-region) ; ctrl-c p | |
12365826 RS |
158 | |
159 | Ka/zd/a operacj/e przekodowania mo/zna w ca/lo/sci odwo/la/c | |
160 | przez wykonanie polecenia `undo'.") | |
161 | ||
162 | (defun ogonek-jak () | |
f23515e1 | 163 | "Display `ogonek-informacja' in an auxiliary *ogonek-jak* buffer." |
12365826 RS |
164 | (interactive) |
165 | (set-buffer (get-buffer-create " *ogonek-jak*")) | |
166 | (insert ogonek-informacja) | |
167 | (switch-to-buffer " *ogonek-jak*") | |
35861220 | 168 | (goto-char (point-min))) |
12365826 | 169 | |
f23515e1 | 170 | ;; ------ A Little Info in English -------- |
12365826 RS |
171 | |
172 | (defconst ogonek-information | |
f23515e1 | 173 | " THE INTERACTIVE FUNCTIONS PROVIDED BY THE LIBRARY `ogonek'. |
12365826 RS |
174 | |
175 | If you read this text then you are either looking at the library's | |
f23515e1 | 176 | source text or you have called the `ogonek-how' command. In the |
12365826 RS |
177 | latter case you may remove this text using `M-x kill-buffer'. |
178 | ||
179 | The library provides functions for changing the encoding of Polish | |
180 | diacritic characters, the ones with an `ogonek' below or above them. | |
f23515e1 RS |
181 | The functions come in the following groups. |
182 | ||
183 | 1. `ogonek-recode-region' and `ogonek-recode-buffer' to change | |
a1506d29 JB |
184 | between one-character encodings, such as `iso-8859-2', `mazovia', |
185 | plain `ascii' or `TeX'. As the names suggest you may recode | |
186 | either the entire current buffer or just a marked region | |
187 | in it. You may use the functions interactively as commands. | |
188 | Once you call a command you will be asked about the code | |
189 | currently used in your text and the target encoding, the one | |
190 | you want to get. The following example shows a non-interactive | |
191 | use of the functions in a program. This also illustrates what | |
f23515e1 RS |
192 | type of parameters the functions expect to be called with: |
193 | ||
a1506d29 | 194 | (ogonek-recode-region |
f23515e1 | 195 | (region-beginning) (region-end) from-code-name to-code-name) |
a1506d29 | 196 | (ogonek-recode-buffer from-code-name to-code-name) |
12365826 | 197 | |
a1506d29 | 198 | 2. `ogonek-prefixify-region' and `ogonek-prefixify-buffer' for |
f23515e1 | 199 | introducing prefix notation: |
12365826 | 200 | |
a1506d29 | 201 | (ogonek-prefixify-region |
f23515e1 | 202 | (region-beginning) (region-end) from-code-name prefix-char) |
a1506d29 JB |
203 | (ogonek-prefixify-buffer from-code-name prefix-char) |
204 | ||
205 | 3. `ogonek-deprefixify-region' and `ogonek-deprefixify-buffer' for | |
f23515e1 | 206 | removing prefix notation: |
a1506d29 JB |
207 | |
208 | (ogonek-deprefixify-region | |
f23515e1 | 209 | (region-beginning) (region-end) prefix-char to-code-name) |
a1506d29 | 210 | (ogonek-prefixify-buffer prefix-char to-code-name) |
12365826 | 211 | |
f23515e1 | 212 | The TAB character used in interactive mode makes `emacs' |
a1506d29 | 213 | display the list of encodings recognized by the library. The list |
f23515e1 | 214 | is stored in the constant `ogonek-name-encoding-alist'. |
12365826 | 215 | |
f23515e1 | 216 | The `ogonek' functions refer to five variables in which the suggested |
a1506d29 | 217 | answers to dialogue questions are stored. The variables and their |
f23515e1 | 218 | default values are: |
a1506d29 | 219 | |
12365826 | 220 | ogonek-from-encoding iso8859-2 |
f23515e1 RS |
221 | ogonek-to-encoding ascii |
222 | ogonek-prefix-char / | |
12365826 RS |
223 | ogonek-prefix-from-encoding iso8859-2 |
224 | ogonek-prefix-to-encoding iso8859-2 | |
225 | ||
a1506d29 | 226 | The above default values can be changed by placing appropriate settings |
12365826 RS |
227 | in the '~/.emacs' file: |
228 | ||
f23515e1 RS |
229 | (setq ogonek-prefix-char ?/) |
230 | (setq ogonek-prefix-to-encoding \"iso8859-2\") | |
12365826 | 231 | |
f23515e1 RS |
232 | Instead of loading the whole library `ogonek' it may be better to |
233 | autoload the needed functions, for example by placing in `~/.emacs': | |
12365826 | 234 | |
f23515e1 RS |
235 | (autoload 'ogonek-how \"ogonek\") |
236 | (autoload 'ogonek-recode-region \"ogonek\") | |
237 | (autoload 'ogonek-prefixify-region \"ogonek\") | |
238 | (autoload 'ogonek-deprefixify-region \"ogonek\") | |
12365826 RS |
239 | |
240 | The most frequent function calls can be abbreviated and assigned to | |
241 | keyboard keys. Here are a few practical examples: | |
242 | ||
f23515e1 RS |
243 | (defun deprefixify-iso8859-2-region (start end) |
244 | (interactive \"*r\") | |
245 | (ogonek-deprefixify-region start end ?/ \"iso8859-2\")) | |
246 | (global-set-key \"\\C-cd\" 'deprefixify-iso8859-2-region) ; ctrl-c d | |
12365826 | 247 | |
a1506d29 | 248 | (defun mazovia-to-iso8859-2 (start end) |
f23515e1 RS |
249 | (interactive \"*r\") |
250 | (ogonek-recode-region start end \"mazovia\" \"iso8859-2\")) | |
251 | (global-set-key \"\\C-cr\" 'mazovia-to-iso8859-2) ; ctrl-c r | |
12365826 | 252 | |
f23515e1 RS |
253 | (defun prefixify-iso8859-2-region (start end) |
254 | (interactive \"*r\") | |
255 | (ogonek-prefixify-region start end \"iso8859-2\" ?/)) | |
256 | (global-set-key \"\\C-cp\" 'prefixify-iso8859-2-region) ; ctrl-c p | |
12365826 | 257 | |
f23515e1 | 258 | Each recoding operation can be called off using the `undo' command.") |
12365826 RS |
259 | |
260 | (defun ogonek-how () | |
f23515e1 | 261 | "Display `ogonek-information' in an auxiliary *recode-how* buffer." |
12365826 | 262 | (interactive "*") |
f23515e1 | 263 | (set-buffer (get-buffer-create " *ogonek-how*")) |
12365826 | 264 | (insert ogonek-information) |
f23515e1 | 265 | (switch-to-buffer " *ogonek-how*") |
35861220 | 266 | (goto-char (point-min))) |
12365826 | 267 | |
f23515e1 | 268 | ;; ---- Variables keeping the suggested answers to dialogue questions ----- |
3413eafa | 269 | (defvar ogonek-encoding-choices |
a1506d29 | 270 | (cons 'choice |
3413eafa SE |
271 | (mapcar (lambda (x) (list 'const (car x))) |
272 | ogonek-name-encoding-alist)) | |
273 | "List of ogonek encodings. Used only for customization.") | |
274 | (defcustom ogonek-from-encoding "iso8859-2" | |
1fc7dabf | 275 | "Encoding in the source file of recoding." |
3413eafa SE |
276 | :type ogonek-encoding-choices |
277 | :group 'ogonek) | |
278 | (defcustom ogonek-to-encoding "ascii" | |
1fc7dabf | 279 | "Encoding in the target file of recoding." |
3413eafa SE |
280 | :type ogonek-encoding-choices |
281 | :group 'ogonek) | |
282 | (defcustom ogonek-prefix-char ?/ | |
1fc7dabf | 283 | "Prefix character for prefix encodings." |
3413eafa SE |
284 | :type 'character |
285 | :group 'ogonek) | |
286 | (defcustom ogonek-prefix-from-encoding "iso8859-2" | |
1fc7dabf | 287 | "Encoding in the source file subject to prefixifation." |
3413eafa SE |
288 | :type ogonek-encoding-choices |
289 | :group 'ogonek) | |
290 | (defcustom ogonek-prefix-to-encoding "iso8859-2" | |
1fc7dabf | 291 | "Encoding in the target file subject to deprefixifation." |
3413eafa SE |
292 | :type ogonek-encoding-choices |
293 | :group 'ogonek) | |
12365826 | 294 | |
f23515e1 | 295 | ;; ---- Auxiliary functions for reading parameters in interactive mode ---- |
12365826 RS |
296 | |
297 | (defun ogonek-read-encoding (prompt default-name-var) | |
f23515e1 | 298 | "Read encoding name with completion based on `ogonek-name-encoding-alist'. |
b9d67a77 | 299 | Store the name in the parameter-variable DEFAULT-NAME-VAR. |
f23515e1 | 300 | PROMPT is a string to be shown when the user is asked for a name." |
a1506d29 JB |
301 | (let ((encoding |
302 | (completing-read | |
12365826 RS |
303 | (format "%s (default %s): " prompt (eval default-name-var)) |
304 | ogonek-name-encoding-alist nil t))) | |
f23515e1 | 305 | ;; change the default name to the one just read |
a1506d29 JB |
306 | (set default-name-var |
307 | (if (string= encoding "") (eval default-name-var) encoding)) | |
f23515e1 | 308 | ;; return the new default as the name you read |
12365826 RS |
309 | (eval default-name-var))) |
310 | ||
311 | (defun ogonek-read-prefix (prompt default-prefix-var) | |
f23515e1 | 312 | "Read a prefix character for prefix notation. |
a1506d29 | 313 | The result is stored in the variable DEFAULT-PREFIX-VAR. |
f23515e1 | 314 | PROMPT is a string to be shown when the user is asked for a new prefix." |
a1506d29 JB |
315 | (let ((prefix-string |
316 | (read-string | |
317 | (format "%s (default %s): " prompt | |
12365826 RS |
318 | (char-to-string (eval default-prefix-var)))))) |
319 | (if (> (length prefix-string) 1) | |
a1506d29 | 320 | (error "! Only one character expected") |
f23515e1 | 321 | ;; set the default prefix character to the one just read |
12365826 | 322 | (set default-prefix-var |
a1506d29 JB |
323 | (if (string= prefix-string "") |
324 | (eval default-prefix-var) | |
12365826 | 325 | (string-to-char prefix-string))) |
f23515e1 | 326 | ;; the new default prefix is the function's result: |
12365826 RS |
327 | (eval default-prefix-var)))) |
328 | ||
329 | (defun ogonek-lookup-encoding (encoding) | |
f23515e1 | 330 | "Pick up an association for ENCODING in `ogonek-name-encoding-alist'. |
a1506d29 | 331 | Before returning a result test whether the string ENCODING is in |
f23515e1 | 332 | the list `ogonek-name-encoding-alist'" |
12365826 RS |
333 | (let ((code-list (assoc encoding ogonek-name-encoding-alist))) |
334 | (if (null code-list) | |
e8af40ee | 335 | (error "! Name `%s' not known in `ogonek-name-encoding-alist'" |
12365826 RS |
336 | encoding) |
337 | (cdr code-list)))) | |
338 | ||
f23515e1 | 339 | ;; ---- An auxiliary function for zipping two lists of equal length ---- |
12365826 RS |
340 | |
341 | (defun ogonek-zip-lists (xs ys) | |
a1506d29 JB |
342 | "Build a list of pairs from lists XS and YS of the same length." |
343 | (let ((pairs nil)) | |
12365826 RS |
344 | (while xs |
345 | (setq pairs (cons (cons (car xs) (car ys)) pairs)) | |
346 | (setq xs (cdr xs)) | |
347 | (setq ys (cdr ys))) | |
f23515e1 | 348 | ;; `pairs' are the function's result |
12365826 RS |
349 | pairs)) |
350 | ||
f23515e1 | 351 | ;; ---- An auxiliary function building a one-to-one recoding table ----- |
12365826 | 352 | |
a1506d29 | 353 | (defun ogonek-build-table (recoding-pairs) |
f23515e1 RS |
354 | "Build a table required by Emacs's `translate-region' function. |
355 | RECODING-PAIRS is a list of character pairs for which recoding | |
a1506d29 | 356 | is not an identity. |
12365826 RS |
357 | By using the built-in `translate-region' function |
358 | we gain better performance compared to converting characters | |
359 | by a hand-written routine as it is done for prefix encodings." | |
360 | (let ((table (make-string 256 0)) | |
361 | (i 0)) | |
a1506d29 JB |
362 | (while (< i 256) |
363 | (aset table i i) | |
12365826 | 364 | (setq i (1+ i))) |
f23515e1 | 365 | ;; make changes in `table' according to `recoding-pairs' |
12365826 RS |
366 | (while recoding-pairs |
367 | (aset table (car (car recoding-pairs)) (cdr (car recoding-pairs))) | |
368 | (setq recoding-pairs (cdr recoding-pairs))) | |
f23515e1 | 369 | ;; return the table just built |
12365826 RS |
370 | table)) |
371 | ||
f23515e1 RS |
372 | ;; ---- Commands for one-to-one recoding ------------------------------- |
373 | ||
a1506d29 | 374 | (defun ogonek-recode-region (start end from-encoding to-encoding) |
f23515e1 | 375 | "Recode text in a marked region in one-to-one manner. |
a1506d29 JB |
376 | When called interactively ask the user for the names of the FROM- |
377 | and TO- encodings." | |
12365826 | 378 | (interactive (progn (barf-if-buffer-read-only) |
a1506d29 JB |
379 | (list |
380 | (region-beginning) | |
12365826 RS |
381 | (region-end) |
382 | (ogonek-read-encoding "From code" 'ogonek-from-encoding) | |
383 | (ogonek-read-encoding "To code" 'ogonek-to-encoding)))) | |
384 | (save-excursion | |
a1506d29 | 385 | (translate-region |
12365826 | 386 | start end |
a1506d29 | 387 | (ogonek-build-table |
12365826 RS |
388 | (ogonek-zip-lists |
389 | (ogonek-lookup-encoding from-encoding) | |
390 | (ogonek-lookup-encoding to-encoding)))))) | |
391 | ||
392 | (defun ogonek-recode-buffer (from-encoding to-encoding) | |
f23515e1 | 393 | "Call `ogonek-recode-region' on the entire buffer. |
a1506d29 | 394 | When called interactively ask the user for the names of the FROM- |
f23515e1 | 395 | and TO- encodings." |
12365826 | 396 | (interactive (progn (barf-if-buffer-read-only) |
a1506d29 | 397 | (list |
12365826 RS |
398 | (ogonek-read-encoding "From code" 'ogonek-from-encoding) |
399 | (ogonek-read-encoding "To code" 'ogonek-to-encoding)))) | |
a1506d29 | 400 | (ogonek-recode-region |
12365826 RS |
401 | (point-min) (point-max) from-encoding to-encoding)) |
402 | ||
f23515e1 | 403 | ;; ---- Recoding with prefix notation ------------------------------- |
12365826 | 404 | |
a1506d29 JB |
405 | (defconst ogonek-prefix-code '(?A ?C ?E ?L ?N ?O ?S ?X ?Z |
406 | ?a ?c ?e ?l ?n ?o ?s ?x ?z)) | |
407 | ||
12365826 | 408 | (defun ogonek-prefixify-region (start end from-encoding prefix-char) |
f23515e1 | 409 | "In a region, replace FROM-encoded Polish characters with PREFIX pairs. |
a1506d29 JB |
410 | A PREFIX pair generated consists of PREFIX-CHAR and the respective |
411 | character listed in the `ogonek-prefix-code' constant. | |
f23515e1 | 412 | PREFIX-CHAR itself gets doubled." |
12365826 | 413 | (interactive (progn (barf-if-buffer-read-only) |
a1506d29 JB |
414 | (list |
415 | (region-beginning) | |
416 | (region-end) | |
12365826 RS |
417 | (ogonek-read-encoding "From code" 'ogonek-prefix-from-encoding) |
418 | (ogonek-read-prefix "Prefix character" 'ogonek-prefix-char)))) | |
a1506d29 | 419 | (let* |
12365826 | 420 | ((from-code (ogonek-lookup-encoding from-encoding)) |
f23515e1 RS |
421 | (to-code ogonek-prefix-code) |
422 | (recoding-pairs ; `ogonek-prefix-char' added for doubling | |
a1506d29 JB |
423 | (ogonek-zip-lists |
424 | (cons prefix-char from-code) | |
12365826 RS |
425 | (cons prefix-char to-code)))) |
426 | (save-excursion | |
427 | (goto-char start) | |
428 | (while (< (point) end) | |
429 | (let ((pair (assoc (following-char) recoding-pairs))) | |
430 | (if (null pair) | |
f23515e1 | 431 | ;; not a Polish character -- skip it |
12365826 | 432 | (forward-char 1) |
f23515e1 | 433 | ;; Polish character -- insert a prefix pair instead |
12365826 RS |
434 | (delete-char 1) |
435 | (insert ogonek-prefix-char) | |
436 | (insert (cdr pair)) | |
f23515e1 | 437 | ;; the region is now one character longer |
12365826 RS |
438 | (setq end (1+ end)))))))) |
439 | ||
440 | (defun ogonek-prefixify-buffer (from-encoding prefix-char) | |
441 | "Call `ogonek-prefixify-region' on the entire buffer." | |
442 | (interactive (progn (barf-if-buffer-read-only) | |
a1506d29 | 443 | (list |
12365826 RS |
444 | (ogonek-read-encoding "From code" 'ogonek-prefix-from-encoding) |
445 | (ogonek-read-prefix "Prefix character" 'ogonek-prefix-char)))) | |
a1506d29 | 446 | (ogonek-prefixify-region |
12365826 RS |
447 | (point-min) (point-max) from-encoding prefix-char)) |
448 | ||
449 | (defun ogonek-deprefixify-region (start end prefix-char to-encoding) | |
f23515e1 | 450 | "In a region, replace PREFIX pairs with their corresponding TO-encodings. |
a1506d29 | 451 | PREFIX-CHAR followed by a Polish character from the `ogonek-prefix-code' |
f23515e1 | 452 | list is replaced with the corresponding TO-encoded character. A doubled |
a1506d29 JB |
453 | PREFIX-CHAR gets replaced with a single one. A combination of PREFIX-CHAR |
454 | followed by a non-Polish character, that is one not listed in the | |
f23515e1 | 455 | `ogonek-prefix-code' constant, is left unchanged." |
12365826 | 456 | (interactive (progn (barf-if-buffer-read-only) |
a1506d29 | 457 | (list (region-beginning) |
12365826 | 458 | (region-end) |
a1506d29 | 459 | (ogonek-read-prefix |
12365826 | 460 | "Prefix character" 'ogonek-prefix-char) |
a1506d29 | 461 | (ogonek-read-encoding |
12365826 | 462 | "To code" 'ogonek-prefix-to-encoding)))) |
a1506d29 | 463 | (let* |
f23515e1 | 464 | ((from-code ogonek-prefix-code) |
12365826 | 465 | (to-code (ogonek-lookup-encoding to-encoding)) |
a1506d29 JB |
466 | (recoding-pairs |
467 | (ogonek-zip-lists | |
468 | (cons prefix-char from-code) | |
12365826 RS |
469 | (cons prefix-char to-code)))) |
470 | (save-excursion | |
471 | (goto-char start) | |
472 | (while (< (point) end) | |
473 | (forward-char 1) | |
474 | (if (or (not (= (preceding-char) prefix-char)) (= (point) end)) | |
f23515e1 | 475 | ;; non-prefix character or the end-of-region -- do nothing |
12365826 | 476 | () |
f23515e1 | 477 | ;; now, we can check the next character |
12365826 | 478 | (let ((pair (assoc (following-char) recoding-pairs))) |
a1506d29 | 479 | (if (null pair) |
f23515e1 RS |
480 | ;; `following-char' is not a Polish character nor it is |
481 | ;; `prefix-char' since the one is among `recoding-pairs' | |
a1506d29 | 482 | (forward-char 1) |
f23515e1 RS |
483 | ;; else prefix followed by a Polish character has been found |
484 | ;; replace it by the corresponding Polish character | |
12365826 RS |
485 | (backward-char 1) |
486 | (delete-char 2) | |
487 | (insert (cdr pair)) | |
f23515e1 | 488 | ;; the region got shorter by one character |
12365826 RS |
489 | (setq end (1- end))))))))) |
490 | ||
491 | (defun ogonek-deprefixify-buffer (prefix-char to-encoding) | |
492 | "Call `ogonek-deprefixify-region' on the entire buffer." | |
493 | (interactive (progn (barf-if-buffer-read-only) | |
a1506d29 | 494 | (list |
12365826 RS |
495 | (ogonek-read-prefix "Prefix character" 'ogonek-prefix-char) |
496 | (ogonek-read-encoding "To code" 'ogonek-prefix-to-encoding)))) | |
a1506d29 | 497 | (ogonek-deprefixify-region |
12365826 RS |
498 | (point-min) (point-max) prefix-char to-encoding)) |
499 | ||
500 | (provide 'ogonek) | |
501 | ||
502 | ;;; ogonek.el ends here |