summaryrefslogtreecommitdiff
path: root/support/iso-tex/iso-tex.el
blob: 1ee4d54d99b8b027fde3b9b590eef1f38d410f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
;;; iso-tex.el --- Translating TeX to ISO-8859/1 while editing a file
;;*****************************************************************************
;; $Id: iso-tex.el,v 1.21 1997/05/15 08:15:12 gerd Exp gerd $
;;*****************************************************************************
;;
;; Description:
;;	When using (La)TeX it is highly desirable to display national
;;	characters contained in the ISO-8859/1 character set. Editing
;;	files with ISO-8859/1 characters is enabled in GNU Emacs since
;;	version 19.
;;
;;	Two ways can be envisaged. The first one is to teach TeX the
;;	meaning of the extended character set (e.g. via a sty file in
;;	LaTeX). This solution has the disadvantage that it may collide
;;	with other extended character sets.
;;
;;	The second solution is used in iso-tex.el. The extended
;;	character set is used only temporarily during editing of a
;;	file in Emacs. The file processed by TeX contains pure ASCII
;;	representations of the extended characters (if possible).
;;
;;	iso-tex.el provides a minor mode which anchors itself in
;;	various hooks to perform translations when reading or writing
;;	files.
;;
;;	No provisions are made to insert ISO-8859/1 characters since
;;	other packages are availlable for this purpose.
;;
;;
;; Installation:
;;	1. Ensure that iso-tex.el is on the load-path.
;;	2. For efficiency it might be desirable to byte-compile 
;;	   iso-tex.el.
;;	3. Put the following in your .emacs file or a similar place
;;	   where it is loaded when needed.
;;
;;	   (autoload 'iso-tex-minor-mode
;;		     "iso-tex"
;;		     "Translate TeX to ISO 8859/1 while visiting a file."
;;		     t)
;;
;;	4. Enable the iso-tex minor mode for the appropriate
;;	   files. This depends on the major mode you use for editing
;;	   (La)TeX files. For this purpose you can use the entry hook
;;	   of this mode. E.g.
;;
;;	   (setq TeX-mode-hook 
;;		 (function (lambda () (interactive)
;;			     (iso-tex-minor-mode 1)
;;				; and other initializations
;;				; ...
;;			     )))
;;
;;	   Alternatively tex-mode-hook, latex-mode-hook, or LaTeX-mode-hook
;;	   might be places to perform the initialization.
;;
;; For users of german.sty:
;;
;;	Instead of the autoload command mentioned under point 3. use the
;;	following initializations. They cause the german.sty umlaut
;;	variants to be inserted.
;;
;;	(load "iso-tex")
;;      (define-iso-tex "Ä" "\"A"	"\"A"	) ; german.sty
;;      (define-iso-tex "Ë" "\"E"	"\"E"	) ; german.sty
;;      (define-iso-tex "Ï" "\"I"	"\"I"	) ; german.sty
;;      (define-iso-tex "Ö" "\"O"	"\"O"	) ; german.sty
;;      (define-iso-tex "Ü" "\"U"	"\"U"	) ; german.sty
;;      (define-iso-tex "ß" "\"s"	"\\ss" "\\3" ) ; german.sty
;;      (define-iso-tex "ä" "\"a"	"\"a"	) ; german.sty
;;      (define-iso-tex "ë" "\"e"	"\\\"e" ) ; german.sty
;;      (define-iso-tex "ï" "\"i"	"\\\"\\i") ; german.sty
;;      (define-iso-tex "ö" "\"o"	"\\\"o"	) ; german.sty
;;      (define-iso-tex "ü" "\"u"	"\\\"u"	) ; german.sty
;;
;;
;;	The same trick can be used to redefine any string
;;	representation of the ISO characters. E.g. 
;;
;;      (define-iso-tex "µ" "\\MYmu "	"\\MYmu" )
;;
;;	inserts the string "\MYmu" for µ. \MYmu can now be defined to
;;	produce a µ in any environment. E.g. in LaTeX by
;;
;;	\newcommand\MYmu{\mbox{\(\mu\)}}
;;
;;
;; Bugs and Problems:
;;	- Some of the TeX sequences which are inserted by iso-tex are
;;	  not defined in plain (La)TeX. Don't use them or provide defs.
;;
;;	- Some of the TeX sequences which are inserted by iso-tex are
;;	  only defined inside/outside math environments. Be careful!
;;
;;	- There might be problems when saving a narrowed buffer.
;;	- Point might not be restored properly.
;;
;;	- There are quite a few ways to code accented characters in
;;	  TeX. Only some are captured in this program.
;;
;;	- Writing of a region is not supported. There seems to be no
;;	  appropriate hook.
;;
;;
;; To do:
;;	- iso-tex can be used for a wider range of translations when
;;	  reading and writing. Maybe it's worth extracting those
;;	  routines which are more general and make iso-tex a sample
;;	  instance of the general routines.
;;
;;
;; Changes:
;;	- undefine-iso-tex function added to undo the effects of
;;	  define-iso-tex.
;;      - allowing non-braced expansion. This is the new default.
;;	  set the variable iso-tex-extra-braces to t to get the old
;;	  behaviour.
;;	  If you are still running Emacs 18.* you might have to make
;;	  buffer-substring-no-properties an alias to buffer-substring.
;;
;;	- using the macros from the textcomp package for the formerly
;;	  undefined characters.
;;	- Understand more variants of combinations with \i.
;;
;;	- solitary diaresis is now translated into a macro instead of pure
;;	  TeX code which may cause trouble.
;;
;;	- enhanced patterns for recognition of accented characters in TeX
;;	- german.sty (partially) supported
;;	- minor bugs fixed to generate accented i.
;;
;;	- after-save-hooks seems to be after-save-hook now.
;;
;;	- german.sty support partially removed because it interfered e.g.
;;	  with BibTeX strings.
;;
;;	- after-save-hooks and after-save-hook are both set.
;;
;;	- Default TeX sequences changed to conform to BibTeX coding standart.
;;
;;
;; Author:	
;;	Gerd Neugebauer
;;	Mainzer Str. 8
;;	56321 Rhens (Germany)
;;
;;	Net: gerd@informatik.uni-koblenz.de
;;	     gerd@imn.th-leipzig.de				(old)
;;	     gerd@intellektik.informatik.th-darmstadt.de	(ancient)
;;
;;*****************************************************************************
;; LCD Archive Entry:
;; iso-tex|Gerd Neugebauer|gerd@informatik.uni-koblenz.de|
;; Translating TeX to ISO-8859/1 while editing a file.|
;; $Date: 1997/05/15 08:15:12 $|$Revision: 1.21 $||
;;*****************************************************************************
;;
;; Copyright (C) 1994-1996 Gerd Neugebauer
;;
;; iso-tex.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU General Public
;; License for full details.
;;
;; Everyone is granted permission to copy, modify and redistribute
;; iso-tex.el, but only under the conditions described in the
;; GNU General Public License.	 A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.	Among other things, the copyright notice
;; and this notice must be preserved on all copies.
;;

;;;----------------------------------------------------------------------------
;;; Variable definitions and initializations

(defvar iso-tex-minor-mode nil
  "Variable indicating when iso-tex-minor-mode is active.")

(or (assq 'iso-tex-minor-mode minor-mode-alist)
    (setq minor-mode-alist (cons '(iso-tex-minor-mode " ISO-TeX")
				 minor-mode-alist)))

(defvar iso-tex-minor-mode-initialized nil
  "Variable indicating if iso-tex-minor-mode is already initialized in this
buffer. This variable is buffer local.")

(make-variable-buffer-local 'iso-tex-minor-mode-initialized)

(defvar tex-2-iso-regex
  (concat
   "\\\\\\([\"'`^~]\\([a-zA-Z]\\({}\\)?\\|\\\\i[ \t]*\\|{\\\\i}\\|{}\\)" 
       "\\|c{[a-zA-Z]?}"
       "\\|[a-zA-Z]+[ \t]*\\({}\\)?"
       "\\)"
   "\\|{\\(\\\\\\([\"'`^~]\\([a-zA-Z]\\|\\\\i\\|{\\\\i}\\)?\\({}\\)?" 
              "\\|c{[a-zA-Z]?}"
	      "\\|[a-zA-Z]+"
	      "\\)"
       "\\|[!?]`"
       "\\)}"
   "\\|\"[a-zA-Z]" ; for german.sty
   "\\|[!?]`"
  )
  "Regular expression to pre-select substrings to be translated by tex-2-iso.")
(defvar iso-2-tex-regex "[€-ÿ]"
  "Regular expression to pre-select substrings to be translated by iso-2-tex.")
(defvar iso-2-tex-alist nil
  "Alist of CHAR.STRING pairs used by iso-2-tex.")
(defvar tex-2-iso-alist nil
  "Alist of STRING.ISO-CHAR pairs used by tex-2-iso.")

(defun define-iso-tex (char &optional string &rest names)
  "Define a translation between ISO-8859/1 characters and TeX sequences.
CHAR is the ISO-8859/1 character code as a single letter string.
If STRING is non nil then it is used as representation of CHAR.
The optional remaining arguments are used to translate TeX sequences to
characters."
  (if string (setq iso-2-tex-alist (cons 
				    (cons (string-to-char char) string) 
				    iso-2-tex-alist)))
  (setq names (cons string names))
  (while names
    (setq tex-2-iso-alist (cons (cons (car names) char) 
				tex-2-iso-alist))
    (setq names (cdr names))
  )
)

(defun undefine-iso-tex (char &optional string &rest names)
  "Remove definitions from the translation tables. This reverses the effect of
define-iso-tex. 

CHAR is the ISO-8859/1 character code as a single letter string.
If STRING is non nil then it is used as representation of CHAR.
The optional remaining arguments are used to translate TeX sequences to
characters.

If CHAR is NIL then only the translation from TeX to iso is disabled.
"
  (let (al)
    (and char
	 (setq al (assoc (string-to-char char) iso-2-tex-alist))
	 (rplaca al nil))
    (setq names (cons string names))
    (while names
      (and (setq al (assoc (car names) tex-2-iso-alist))
	   (rplaca al nil))
      (setq names (cdr names))
    )
  )
)

(if (null iso-2-tex-alist)
    (progn
      (define-iso-tex "¡" "!`"			)
      (define-iso-tex "¢" "\\textcent"		) ; from tc
      (define-iso-tex "£" "\\pounds"		)
      (define-iso-tex "¤" "\\textcurrency"	) ; from tc
      (define-iso-tex "¥" "\\textyen"		) ; from tc
      (define-iso-tex "¦" "\\textbrokenbar"	) ; from tc
      (define-iso-tex "§" "\\S"			)
      (define-iso-tex "¨" "\\texthighdiaeresis"	) ; from tc
      (define-iso-tex "©" "\\copyright"		)
      (define-iso-tex "ª" "\\textordfemenine"	) ; from tc
      (define-iso-tex "«" "\\ll"		) ; math mode only
      (define-iso-tex "¬" "\\neg"		) ; math mode only
      (define-iso-tex "­" "\\-" 	) ; It's too hard to distinguish from -
      (define-iso-tex "®" "\\textregistered"	) ; from tc
      (define-iso-tex "¯" "\\textmacron"	) ; from tc
      (define-iso-tex "°" "\\textdegree"	) ; from tc
      (define-iso-tex "±" "\\pm"		) ; math mode only
      (define-iso-tex "²" "\\texttwosuperior"	) ; from tc
      (define-iso-tex "³" "\\textthreesuperior"	) ; from tc
      (define-iso-tex "´" "\\'{}" "\\'"		)
      (define-iso-tex "µ" "\\mu"		) ; math mode only
      (define-iso-tex "¶" "\\P"			)
      (define-iso-tex "·" "\\cdot"		)
      (define-iso-tex "¸" "\\c{}" "\\c"		)
      (define-iso-tex "¹" "\\textonesuperior"	) ; from tc
      (define-iso-tex "º" "\\textordmasculine"	) ; usually undefined
      (define-iso-tex "»" "\\gg"		) ; math mode only
      (define-iso-tex "¼" "\\textonequater"	) ; from tc
      (define-iso-tex "½" "\\textonehalf"	) ; from tc
      (define-iso-tex "¾" "\\textthreequaters"	) ; from tc
      (define-iso-tex "¿" "?`"			)
      (define-iso-tex "À" "\\`A"		)
      (define-iso-tex "Á" "\\'A"		)
      (define-iso-tex "Â" "\\^A"		)
      (define-iso-tex "Ã" "\\~A"		)
      (define-iso-tex "Ä" "\\\"A" 		) ; "\"A" german.sty
      (define-iso-tex "Å" "\\AA"		)
      (define-iso-tex "Æ" "\\AE"		)
      (define-iso-tex "Ç" "\\c{C}"		)
      (define-iso-tex "È" "\\`E"		)
      (define-iso-tex "É" "\\'E"		)
      (define-iso-tex "Ê" "\\^E"		)
      (define-iso-tex "Ë" "\\\"E" 		) ; "\"E" german.sty
      (define-iso-tex "Ì" "\\`I"		)
      (define-iso-tex "Í" "\\'I"		)
      (define-iso-tex "Î" "\\^I"		)
      (define-iso-tex "Ï" "\\\"I" 		) ; "\"I" german.sty
      (define-iso-tex "Ð" "\\Dstroke"		) ; usually undefined
      (define-iso-tex "Ñ" "\\~N"		)
      (define-iso-tex "Ò" "\\`O"		)
      (define-iso-tex "Ó" "\\'O"		)
      (define-iso-tex "Ô" "\\^O"		)
      (define-iso-tex "Õ" "\\~O"		)
      (define-iso-tex "Ö" "\\\"O" 		) ; "\"O" german.sty
      (define-iso-tex "×" "\\times"		)
      (define-iso-tex "Ø" "\\O"			)
      (define-iso-tex "Ù" "\\`U"		)
      (define-iso-tex "Ú" "\\'U"		)
      (define-iso-tex "Û" "\\^U"		)
      (define-iso-tex "Ü" "\\\"U" 		) ; "\"U" german.sty
      (define-iso-tex "Ý" "\\'Y"		)
      (define-iso-tex "Þ" "\\Thorn"		) ; usually undefined
      (define-iso-tex "ß" "\\ss"  "\\3"		) ; "\"s" german.sty
      (define-iso-tex "à" "\\`a"		)
      (define-iso-tex "á" "\\'a"		)
      (define-iso-tex "â" "\\^a"		)
      (define-iso-tex "ã" "\\~a"		)
      (define-iso-tex "ä" "\\\"a" 		) ; "\"a" german.sty
      (define-iso-tex "å" "\\aa"		)
      (define-iso-tex "æ" "\\ae"		)
      (define-iso-tex "ç" "\\c{c}"		)
      (define-iso-tex "è" "\\`e"		)
      (define-iso-tex "é" "\\'e"		)
      (define-iso-tex "ê" "\\^e"		)
      (define-iso-tex "ë" "\\\"e" 		) ; "\"e" german.sty
      (define-iso-tex "ì" "\\`{\\i}" "\\`\\i"	)
      (define-iso-tex "í" "\\'{\\i}" "\\'\\i"	)
      (define-iso-tex "î" "\\^{\\i}" "\\^\\i"	)
      (define-iso-tex "ï" "\\\"{\\i}" "\\\"\\i"	); "\"i" german.sty
      (define-iso-tex "ð" "\\dstroke"		) ; usually undefined
      (define-iso-tex "ñ" "\\~n"		)
      (define-iso-tex "ò" "\\`o"		)
      (define-iso-tex "ó" "\\'o"		)
      (define-iso-tex "ô" "\\^o"		)
      (define-iso-tex "õ" "\\~o"		)
      (define-iso-tex "ö" "\\\"o"	 	) ; "\"o" german.sty
      (define-iso-tex "÷" "\\div"		)
      (define-iso-tex "ø" "\\o"			)
      (define-iso-tex "ù" "\\`u"		)
      (define-iso-tex "ú" "\\'u"		)
      (define-iso-tex "û" "\\^u"		)
      (define-iso-tex "ü" "\\\"u" 		) ; "\"u" german.sty
      (define-iso-tex "ý" "\\'y"		)
      (define-iso-tex "þ" "\\thorn"		) ; usually undefined
      (define-iso-tex "ÿ" "\\\"y"		)
))


;;;----------------------------------------------------------------------------
;;; Definition of the minor mode

(defun iso-tex-minor-mode (&optional arg)
  "Minor mode to translate TeX sequences into ISO 8859/1 characters while 
visiting a file.
Provisions are made to translate them back when writing."
  (interactive)

  (if (null iso-tex-minor-mode-initialized)
    (progn
      (setq iso-tex-minor-mode-initialized t)
      (add-hook 'write-contents-hooks 'iso-tex-write)
      ;; 
      (make-local-variable 'after-save-hook)
      (add-hook 'after-save-hook 'iso-tex-after-write)
      ;; There seem to be two versions of this hook around
      ;; Horrible to use such undocumented features :-) 
      (make-local-variable 'after-save-hooks)
      (add-hook 'after-save-hooks 'iso-tex-after-write)
      ;;
      (make-local-variable 'iso-tex-minor-mode)
    )
  )
  (setq iso-tex-minor-mode
	(if (null arg) (not iso-tex-minor-mode)
	  (> (prefix-numeric-value arg) 0)))
  (if iso-tex-minor-mode (tex-2-iso) (iso-2-tex) )
)

(defun iso-tex-write ()
  "Function anchored in the local-write-file-hooks. It is not removed but 
disabled with the iso-tex-minor-mode."
  (if iso-tex-minor-mode (iso-2-tex))
)

(defun iso-tex-after-write ()
  "Function anchored in the after-save-hooks. It is not removed but 
disabled with the iso-tex-minor-mode."
  (if iso-tex-minor-mode (tex-2-iso))
)

(defvar iso-tex-extra-braces nil
  "This variable indicates wether extra braces are needed around the strings
which are inserted by iso-2-tex. This variable is local in any buffer.")
(make-variable-buffer-local 'iso-tex-extra-braces)


(defun iso-2-tex ()
  "Translate ISO-8859/1 extended characters into TeX sequences.
The variable iso-2-tex-regex is used to preselect a character which is then
translated using the variable iso-2-tex-alist.
Use the function define-iso-tex instead of setting iso-2-tex-alist."

  (let ((buffer-read-only nil) 
	(state (buffer-modified-p)))
    (save-excursion
      (goto-char (point-min))
      (while (search-forward-regexp iso-2-tex-regex (point-max) t)
	(let ((new (assq (string-to-char 
			   (buffer-substring 
			    (- (point) 1) 
			    (point))) 
			  iso-2-tex-alist)))
	  (if new
	      (progn (delete-backward-char 1)
		     (cond
		      (iso-tex-extra-braces (insert "{" (cdr new) "}"))
		      ((string-match "\\\\[a-zA-Z]+$" (cdr new))
		       (insert (cdr new) "{}"))
		      (t		    (insert (cdr new)) )))
	  )
	)
      )
    ) 
    (set-buffer-modified-p state)
  ) 
  nil
)

(defun tex-2-iso ()
  "Translate TeX sequences into ISO-8859/1 extended characters.
The variable tex-2-iso-regex is used to preselect a character which is then
translated using the variable tex-2-iso-alist.
Use the function define-iso-tex instead of setting tex-2-iso-alist."

  (let ((buffer-read-only nil) 
	(state (buffer-modified-p)))
    (save-excursion
      (goto-char (point-min))
      (while (search-forward-regexp tex-2-iso-regex (point-max) t)
	(let ((hit (buffer-substring-no-properties (match-beginning 0) (match-end 0)))
	      (b (match-beginning 0))
	      (e (match-end 0)) )
					; Apply some simplifications
					; to reduce the numer of
					; entries in the alist
	  (cond
	    ( (string-match "^\\(\\\\['`^\"]\\({\\\\i}\\|\\\\i\\)\\({}\\|[ \t]+\\)\\)$" hit )
	      (setq hit (substring hit (match-beginning 1) (match-end 1)))
	    )
	    ( (string-match "^\\(\\\\[a-zA-Z0-9'`^\"]*\\)\\({}\\|[ \t]+\\)$" hit)
	      (setq hit (substring hit (match-beginning 1) (match-end 1)))
	    )
	    ( (string-match "^{\\(.*\\)}$" hit )
	      (setq hit (substring hit (match-beginning 1) (match-end 1)))
	    )
	    ( (string-match "^\\(\\\\['`^\"]\\){\\(.\\)}$" hit )
	      (setq hit (concat 
			 (substring hit (match-beginning 1) (match-end 1))
			 (substring hit (match-beginning 2) (match-end 2))))
	    )
	  )
	  (setq hit (assoc hit tex-2-iso-alist))
	  (if hit
	      (progn
		(delete-region b e)
		(insert (cdr hit))
	      )
	  )
	)
      )
    )
    (set-buffer-modified-p state)
  )
  nil
)


;  ¡¢£¤¥¦§
; ¨©ª«¬­®¯
; °±²³´µ¶·
; ¸¹º»¼½¾¿
; ÀÁÂÃÄÅÆÇ
; ÈÉÊËÌÍÎÏ
; ÐÑÒÓÔÕÖ×
; ØÙÚÛÜÝÞß
; àáâãäåæç
; èéêëìíîï
; ðñòóôõö÷
; øùúûüýþÿ