| 1 |
;;;; psgml-charent.el |
| 2 |
;;; Last edited: 1999-12-18 18:54:53 lenst |
| 3 |
;;; $Id: psgml-charent.el,v 1.7 2002/04/25 20:50:27 lenst Exp $ |
| 4 |
|
| 5 |
;; Copyright (C) 1994 Lennart Staflin |
| 6 |
|
| 7 |
;; Author: Steinar Bang, Falch Hurtigtrykk as., Oslo, 940711 |
| 8 |
;; Lennart Staflin <[email protected]> |
| 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 |
;; This should either use iso-cvt or do better with a multilingual set of entities |
| 31 |
|
| 32 |
|
| 33 |
;;;; Code: |
| 34 |
|
| 35 |
(provide 'psgml-charent) |
| 36 |
(require 'psgml-parse) |
| 37 |
(eval-when-compile (require 'cl)) |
| 38 |
|
| 39 |
|
| 40 |
;;;; Variable declarations |
| 41 |
|
| 42 |
(defvar sgml-display-char-list-filename |
| 43 |
(expand-file-name "iso88591.map" |
| 44 |
(file-name-directory (locate-library "psgml"))) |
| 45 |
"*Name of file holding relations between character codes and character |
| 46 |
names of displayable characters") |
| 47 |
|
| 48 |
(defvar sgml-display-char-alist-cache nil) |
| 49 |
|
| 50 |
|
| 51 |
;;;; Function declarations |
| 52 |
|
| 53 |
(defun sgml-display-char-alist () |
| 54 |
"Return the current display character alist. |
| 55 |
Alist with entity name as key and display character as content." |
| 56 |
(unless (file-exists-p sgml-display-char-list-filename) |
| 57 |
(error "No display char file: %s" |
| 58 |
sgml-display-char-list-filename)) |
| 59 |
(sgml-cache-catalog sgml-display-char-list-filename |
| 60 |
'sgml-display-char-alist-cache |
| 61 |
(function sgml-read-display-char-alist))) |
| 62 |
|
| 63 |
(defun sgml-read-display-char-alist () |
| 64 |
(let (key disp-char alist) |
| 65 |
(while (re-search-forward "^\\([0-9]+\\)[ \t]+\\(.+\\)$" nil t) |
| 66 |
(setq key (buffer-substring (match-beginning 2) (match-end 2))) |
| 67 |
(setq disp-char (string-to-number (buffer-substring (match-beginning 1) |
| 68 |
(match-end 1)))) |
| 69 |
(if (fboundp 'unibyte-char-to-multibyte) |
| 70 |
(setq disp-char (unibyte-char-to-multibyte disp-char))) |
| 71 |
(setq disp-char (char-to-string disp-char)) |
| 72 |
(push (cons key disp-char) |
| 73 |
alist)) |
| 74 |
alist)) |
| 75 |
|
| 76 |
(defun sgml-charent-to-dispchar-alist () |
| 77 |
"Association list to hold relations of the type |
| 78 |
(CHARACTER-NAME . CHARACTER) |
| 79 |
where |
| 80 |
CHARACTER-NAME is a string holding a character name |
| 81 |
CHARACTER is a string holding a single displayable character" |
| 82 |
(sgml-need-dtd) |
| 83 |
(let ((display-chars (sgml-display-char-alist)) |
| 84 |
(alist nil)) |
| 85 |
(sgml-map-entities |
| 86 |
(function |
| 87 |
(lambda (entity) |
| 88 |
(let ((char (cdr (assoc (sgml-entity-text entity) |
| 89 |
display-chars)))) |
| 90 |
(when char |
| 91 |
(push (cons (sgml-entity-name entity) char) alist))))) |
| 92 |
(sgml-dtd-entities sgml-dtd-info)) |
| 93 |
|
| 94 |
alist)) |
| 95 |
|
| 96 |
|
| 97 |
(defun sgml-charent-to-display-char () |
| 98 |
"Replace character entities with their display character equivalents" |
| 99 |
(interactive) |
| 100 |
(let ((charent-to-char |
| 101 |
(sgml-charent-to-dispchar-alist)) |
| 102 |
charent replacement) |
| 103 |
(save-excursion |
| 104 |
(goto-char (point-min)) |
| 105 |
(sgml-with-parser-syntax |
| 106 |
(while (re-search-forward "&\\(\\w\\(\\w\\|\\s_\\)*\\);?" nil t) |
| 107 |
(setq charent (buffer-substring-no-properties |
| 108 |
(match-beginning 1) (match-end 1))) |
| 109 |
(if (setq replacement (cdr (assoc charent charent-to-char))) |
| 110 |
(replace-match replacement t t))))))) |
| 111 |
|
| 112 |
(defun sgml-display-char-to-charent () |
| 113 |
"Replace displayable characters with their character entity equivalents" |
| 114 |
(interactive) |
| 115 |
(let ((case-fold-search nil)) |
| 116 |
(save-excursion |
| 117 |
(loop for pair in (sgml-charent-to-dispchar-alist) |
| 118 |
do (goto-char (point-min)) |
| 119 |
(while (search-forward (cdr pair) nil t) |
| 120 |
(replace-match (concat "&" (car pair) ";") t t)))))) |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
;;; psgml-charent.el ends here |