comparison lisp/psgml/psgml-charent.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;;; psgml-charent.el
2 ;;; Last edited: Mon Nov 28 22:18:09 1994 by lenst@lysistrate (Lennart Staflin)
3 ;;; $Id: psgml-charent.el,v 1.1.1.1 1996/12/18 03:35:18 steve Exp $
4
5 ;; Copyright (C) 1994 Lennart Staflin
6
7 ;; Author: Steinar Bang, Falch Hurtigtrykk as., Oslo, 940711
8 ;; Lennart Staflin <lenst@lysator.liu.se>
9 ;;
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License
12 ;; as published by the Free Software Foundation; either version 2
13 ;; of the License, or (at your option) any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, write to the Free Software
22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24
25 ;;;; Commentary:
26
27 ;; Functions to convert character entities into displayable characters
28 ;; and displayable characters back into character entities.
29
30
31 ;;;; Code:
32
33 (provide 'psgml-charent)
34
35
36 ;;;; Variable declarations
37
38 (defvar sgml-display-char-list-filename
39 (expand-file-name sgml-data-directory "iso88591.map")
40 "*Name of file holding relations between character codes and character
41 names of displayable characters")
42
43 (defvar sgml-display-char-alist-cache nil)
44
45
46 ;;;; Function declarations
47
48 (defun sgml-display-char-alist ()
49 "Return the current display character alist.
50 Alist with entity name as key and display character as content."
51 (unless (file-exists-p sgml-display-char-list-filename)
52 (error "No display char file: %s"
53 sgml-display-char-list-filename))
54 (sgml-cache-catalog sgml-display-char-list-filename
55 'sgml-display-char-alist-cache
56 (function sgml-read-display-char-alist)))
57
58 (defun sgml-read-display-char-alist ()
59 (let (key disp-char alist)
60 (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\(.+\\)$" nil t)
61 (setq key (buffer-substring (match-beginning 2) (match-end 2)))
62 (setq disp-char
63 (char-to-string
64 (string-to-number
65 (buffer-substring (match-beginning 1) (match-end 1)))))
66 (push (cons key disp-char)
67 alist))
68 alist))
69
70 (defun sgml-charent-to-dispchar-alist ()
71 "Association list to hold relations of the type
72 (CHARACTER-NAME . CHARACTER)
73 where
74 CHARACTER-NAME is a string holding a character name
75 CHARACTER is a string holding a single displayable character"
76 (sgml-need-dtd)
77 (let ((display-chars (sgml-display-char-alist))
78 (alist nil))
79 (sgml-map-entities
80 (function
81 (lambda (entity)
82 (let ((char (cdr (assoc (sgml-entity-text entity)
83 display-chars))))
84 (when char
85 (push (cons (sgml-entity-name entity) char) alist)))))
86 (sgml-dtd-entities sgml-dtd-info))
87
88 alist))
89
90
91 (defun sgml-charent-to-display-char ()
92 "Replace character entities with their display character equivalents"
93 (interactive)
94 (let ((charent-to-char
95 (sgml-charent-to-dispchar-alist))
96 charent replacement)
97 (save-excursion
98 (goto-char (point-min))
99 (sgml-with-parser-syntax
100 (while (re-search-forward "&\\(\\w\\(\\w\\|\\s_\\)+\\);?" nil t)
101 (setq charent (buffer-substring (match-beginning 1) (match-end 1)))
102 (if (setq replacement (cdr (assoc charent charent-to-char)))
103 (replace-match replacement t t)))))))
104
105 (defun sgml-display-char-to-charent ()
106 "Replace displayable characters with their character entity equivalents"
107 (interactive)
108 (let ((case-fold-search nil))
109 (save-excursion
110 (loop for pair in (sgml-charent-to-dispchar-alist)
111 do (goto-char (point-min))
112 (while (search-forward (cdr pair) nil t)
113 (replace-match (concat "&" (car pair) ";") t t))))))
114
115
116
117
118 (require 'psgml-parse)
119
120 ;;; psgml-charent.el ends here