psgml / psgml-charent.el

steve 77820ec 
youngs 586b4d7 
steve 77820ec 

youngs 586b4d7 

steve 77820ec 

andreasj 0762976 
youngs 586b4d7 

steve 77820ec 

steveb 218ea90 

steve 77820ec 

;;;; psgml-charent.el
;;; Last edited: 1999-12-18 18:54:53 lenst
;;; $Id$

;; Copyright (C) 1994 Lennart Staflin

;; Author: Steinar Bang, Falch Hurtigtrykk as., Oslo, 940711
;;	Lennart Staflin <>
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;;; Commentary:

;;  Functions to convert character entities into displayable characters
;;  and displayable characters back into character entities.

;;;; Code:

(provide 'psgml-charent)
(require 'psgml-parse)

;;;; Variable declarations

(defvar sgml-display-char-list-filename
  (expand-file-name ""
                     (or (locate-data-directory "psgml")
                         (locate-data-directory "sgml"))))
  "*Name of file holding relations between character codes and character
names of displayable characters")

(defvar sgml-display-char-alist-cache nil)

;;;; Function declarations

(defun sgml-display-char-alist ()
  "Return the current display character alist.
Alist with entity name as key and display character as content."
  (unless (file-exists-p sgml-display-char-list-filename)
    (error "No display char file: %s"
  (sgml-cache-catalog sgml-display-char-list-filename 
		      (function sgml-read-display-char-alist)))

(defun sgml-read-display-char-alist ()
  (let (key disp-char alist)
    (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\(.+\\)$" nil t)
      (setq key (buffer-substring (match-beginning 2) (match-end 2)))
      (setq disp-char
	      (buffer-substring (match-beginning 1) (match-end 1)))))
      (push (cons key disp-char)

(defun sgml-charent-to-dispchar-alist ()
  "Association list to hold relations of the type
     CHARACTER-NAME is a string holding a character name
     CHARACTER      is a string holding a single displayable character"
  (let ((display-chars (sgml-display-char-alist))
	(alist nil))
      (lambda (entity)
	(let ((char (cdr (assoc (sgml-entity-text entity)
	  (when char
	    (push (cons (sgml-entity-name entity) char) alist)))))
     (sgml-dtd-entities sgml-dtd-info))

(defun sgml-charent-to-display-char ()
  "Replace character entities with their display character equivalents"
  (let ((charent-to-char
	charent replacement)
      (goto-char (point-min))
       (while (re-search-forward "&\\(\\w\\(\\w\\|\\s_\\)*\\);?" nil t)
	 (setq charent (buffer-substring
                        (match-beginning 1) (match-end 1)))
	 (if (setq replacement (cdr (assoc charent charent-to-char)))
	     (replace-match replacement t t)))))))

(defun sgml-display-char-to-charent ()
  "Replace displayable characters with their character entity equivalents"
  (let ((case-fold-search nil))
      (loop for pair in (sgml-charent-to-dispchar-alist)
	do (goto-char (point-min))
	(while (search-forward (cdr pair) nil t)
	  (replace-match (concat "&" (car pair) ";") t t))))))

;;; psgml-charent.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.