(toplevel): Provide `descr-text'.
[bpt/emacs.git] / lisp / enriched.el
index 8cc4736..e74cb6b 100644 (file)
@@ -1,8 +1,8 @@
 ;;; enriched.el --- read and save files in text/enriched format
 
-;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
+;; Copyright (c) 1994, 1995, 1996, 2002 Free Software Foundation, Inc.
 
-;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
+;; Author: Boris Goldowsky <boris@gnu.org>
 ;; Keywords: wp, faces
 
 ;; This file is part of GNU Emacs.
 ;;; Variables controlling the display
 ;;;
 
-(defvar enriched-verbose t
-  "*If non-nil, give status messages when reading and writing files.")
+(defgroup enriched nil
+  "Read and save files in text/enriched format"
+  :group 'wp)
+
+(defcustom enriched-verbose t
+  "*If non-nil, give status messages when reading and writing files."
+  :type 'boolean
+  :group 'enriched)
 
 ;;;
 ;;; Set up faces & display table
 ;;;
 
-;; A slight cheat - all emacs's faces are fixed-width.  
-;; The idea is just to pick one that looks different from the default.
-(if (internal-find-face 'fixed)
-    nil
-  (make-face 'fixed)
-  (if window-system
-      (set-face-font 'fixed
-                    (car (or (x-list-fonts "*fixed-medium*" 
-                                           'default (selected-frame))
-                             (x-list-fonts "*fixed*" 
-                                           'default (selected-frame)))))))
-                             
-(if (internal-find-face 'excerpt)
-    nil
-  (make-face 'excerpt)
-  (if window-system
-      (make-face-italic 'excerpt nil t)))
+;; Emacs doesn't have a "fixed" face by default, since all faces currently
+;; have to be fixed-width.  So we just pick one that looks different from the
+;; default.
+(defface fixed
+  '((t (:weight bold)))
+  "Face used for text that must be shown in fixed width.
+Currently, emacs can only display fixed-width fonts, but this may change.
+This face is used for text specifically marked as fixed-width, for example
+in text/enriched files."
+  :group 'enriched)
+
+(defface excerpt
+  '((t (:slant italic)))
+  "Face used for text that is an excerpt from another document.
+This is used in Enriched mode for text explicitly marked as an excerpt."
+  :group 'enriched)
 
 (defconst enriched-display-table (or (copy-sequence standard-display-table)
                                     (make-display-table)))
@@ -94,7 +99,7 @@ expression, which is evaluated to get the string to insert.")
 (defconst enriched-annotation-format "<%s%s>"
   "General format of enriched-text annotations.")
 
-(defconst enriched-annotation-regexp "<\\(/\\)?\\([-A-za-z0-9]+\\)>"
+(defconst enriched-annotation-regexp "<\\(/\\)?\\([-A-Za-z0-9]+\\)>"
   "Regular expression matching enriched-text annotations.")
 
 (defconst enriched-translations
@@ -112,12 +117,14 @@ expression, which is evaluated to get the string to insert.")
                   (right       "flushright")
                   (left        "flushleft")
                   (full        "flushboth")
-                  (center      "center")) 
+                  (center      "center"))
     (PARAMETER     (t           "param")) ; Argument of preceding annotation
     ;; The following are not part of the standard:
     (FUNCTION      (enriched-decode-foreground "x-color")
-                  (enriched-decode-background "x-bg-color"))
+                  (enriched-decode-background "x-bg-color")
+                  (enriched-decode-display-prop "x-display"))
     (read-only     (t           "x-read-only"))
+    (display      (nil         enriched-handle-display-prop))
     (unknown       (nil         format-annotate-value))
 ;   (font-size     (2           "bigger")       ; unimplemented
 ;                 (-2          "smaller"))
@@ -134,20 +141,14 @@ Any property that is neither on this list nor dealt with by
 
 ;;; Internal variables
 
-(defvar enriched-mode nil
-  "True if Enriched mode is in use.")
-(make-variable-buffer-local 'enriched-mode)
-
-(if (not (assq 'enriched-mode minor-mode-alist))
-    (setq minor-mode-alist
-         (cons '(enriched-mode " Enriched")
-               minor-mode-alist)))
 
-(defvar enriched-mode-hook nil
-  "Functions to run when entering Enriched mode.
+(defcustom enriched-mode-hook nil
+  "Hook run after entering/leaving Enriched mode.
 If you set variables in this hook, you should arrange for them to be restored
 to their old values if you leave Enriched mode.  One way to do this is to add
-them and their old values to `enriched-old-bindings'.")
+them and their old values to `enriched-old-bindings'."
+  :type 'hook
+  :group 'enriched)
 
 (defvar enriched-old-bindings nil
   "Store old variable values that we change when entering mode.
@@ -158,59 +159,53 @@ The value is a list of \(VAR VALUE VAR VALUE...).")
 ;;; Define the mode
 ;;;
 
+(put 'enriched-mode 'permanent-local t)
 ;;;###autoload
-(defun enriched-mode (&optional arg)
+(define-minor-mode enriched-mode
   "Minor mode for editing text/enriched files.
 These are files with embedded formatting information in the MIME standard
 text/enriched format.
 Turning the mode on runs `enriched-mode-hook'.
 
-More information about Enriched mode is available in the file 
-etc/enriched.doc  in the Emacs distribution directory.
+More information about Enriched mode is available in the file
+etc/enriched.doc in the Emacs distribution directory.
 
 Commands:
 
-\\<enriched-mode-map>\\{enriched-mode-map}"
-  (interactive "P")
-  (let ((mod (buffer-modified-p)))
-    (cond ((or (<= (prefix-numeric-value arg) 0)
-              (and enriched-mode (null arg)))
-          ;; Turn mode off
-          (setq enriched-mode nil)
-          (setq buffer-file-format (delq 'text/enriched buffer-file-format))
-          ;; restore old variable values
-          (while enriched-old-bindings
-            (funcall 'set (car enriched-old-bindings)
-                     (car (cdr enriched-old-bindings)))
-            (setq enriched-old-bindings (cdr (cdr enriched-old-bindings)))))
-
-         (enriched-mode nil)           ; Mode already on; do nothing.
-
-         (t (setq enriched-mode t)     ; Turn mode on
-            (add-to-list 'buffer-file-format 'text/enriched)
-            ;; Save old variable values before we change them.
-            ;; These will be restored if we exit Enriched mode.
-            (setq enriched-old-bindings
-                  (list 'buffer-display-table buffer-display-table
-                        'indent-line-function indent-line-function
-                        'default-text-properties default-text-properties))
-            (make-local-variable 'indent-line-function)
-            (make-local-variable 'default-text-properties)
-            (setq indent-line-function 'indent-to-left-margin
-                  buffer-display-table  enriched-display-table)
-            (use-hard-newlines 1 nil)
-            (let ((sticky (plist-get default-text-properties 'front-sticky))
-                  (p enriched-par-props))
-              (while p
-                (add-to-list 'sticky (car p))
-                (setq p (cdr p)))
-              (if sticky
-                  (setq default-text-properties
-                        (plist-put default-text-properties
-                                   'front-sticky sticky))))
-            (run-hooks 'enriched-mode-hook)))
-    (set-buffer-modified-p mod)
-    (force-mode-line-update)))
+\\{enriched-mode-map}"
+  nil " Enriched" nil
+  (cond ((null enriched-mode)
+        ;; Turn mode off
+        (setq buffer-file-format (delq 'text/enriched buffer-file-format))
+        ;; restore old variable values
+        (while enriched-old-bindings
+          (set (pop enriched-old-bindings) (pop enriched-old-bindings))))
+
+       ((memq 'text/enriched buffer-file-format)
+        ;; Mode already on; do nothing.
+        nil)
+
+       (t                              ; Turn mode on
+        (push 'text/enriched buffer-file-format)
+        ;; Save old variable values before we change them.
+        ;; These will be restored if we exit Enriched mode.
+        (setq enriched-old-bindings
+              (list 'buffer-display-table buffer-display-table
+                    'indent-line-function indent-line-function
+                    'default-text-properties default-text-properties))
+        (make-local-variable 'indent-line-function)
+        (make-local-variable 'default-text-properties)
+        (setq indent-line-function 'indent-to-left-margin ;WHY??  -sm
+              buffer-display-table  enriched-display-table)
+        (use-hard-newlines 1 nil)
+        (let ((sticky (plist-get default-text-properties 'front-sticky))
+              (p enriched-par-props))
+          (dolist (x p)
+            (add-to-list 'sticky x))
+          (if sticky
+              (setq default-text-properties
+                    (plist-put default-text-properties
+                               'front-sticky sticky)))))))
 
 ;;;
 ;;; Keybindings
@@ -293,7 +288,7 @@ the region, and the START and END of each region."
     (unjustify-region)
     (goto-char from)
     (format-replace-strings '(("<" . "<<")))
-    (format-insert-annotations 
+    (format-insert-annotations
      (format-annotate-region from (point-max) enriched-translations
                             'enriched-make-annotation enriched-ignore))
     (goto-char from)
@@ -315,40 +310,52 @@ the region, and the START and END of each region."
     ;; Return new end.
     (point-max)))
 
-(defun enriched-make-annotation (name positive)
-  "Format an annotation called NAME.
-If POSITIVE is non-nil, this is the opening annotation, if nil, this is the
-matching close."
-  (cond ((stringp name)
-        (format enriched-annotation-format (if positive "" "/") name))
+(defun enriched-make-annotation (internal-ann positive)
+  "Format an annotation INTERNAL-ANN.
+INTERNAL-ANN may be a string, for a flag, or a list of the form (PARAM VALUE).
+If POSITIVE is non-nil, this is the opening annotation;
+if nil, the matching close."
+  (cond ((stringp internal-ann)
+        (format enriched-annotation-format (if positive "" "/") internal-ann))
        ;; Otherwise it is an annotation with parameters, represented as a list
        (positive
-        (let ((item (car name))
-              (params (cdr name)))
+        (let ((item (car internal-ann))
+              (params (cdr internal-ann)))
           (concat (format enriched-annotation-format "" item)
                   (mapconcat (lambda (i) (concat "<param>" i "</param>"))
                              params ""))))
-       (t (format enriched-annotation-format "/" (car name)))))
+       (t (format enriched-annotation-format "/" (car internal-ann)))))
 
 (defun enriched-encode-other-face (old new)
   "Generate annotations for random face change.
 One annotation each for foreground color, background color, italic, etc."
   (cons (and old (enriched-face-ans old))
        (and new (enriched-face-ans new))))
-           
+
 (defun enriched-face-ans (face)
-  "Return annotations specifying FACE."
-  (cond ((string-match "^fg:" (symbol-name face))
-        (list (list "x-color" (substring (symbol-name face) 3))))
-       ((string-match "^bg:" (symbol-name face))
-        (list (list "x-bg-color" (substring (symbol-name face) 3))))
-       ((let* ((fg (face-foreground face))
-               (bg (face-background face))
+  "Return annotations specifying FACE.
+FACE may be a list of faces instead of a single face;
+it can also be anything allowed as an element of a list
+which can be the value of the `face' text property."
+  (cond ((and (consp face) (eq (car face) 'foreground-color))
+        (list (list "x-color" (cdr face))))
+       ((and (consp face) (eq (car face) 'background-color))
+        (list (list "x-bg-color" (cdr face))))
+       ((and (listp face) (eq (car face) :foreground))
+        (list (list "x-color" (cadr face))))
+       ((and (listp face) (eq (car face) :background))
+        (list (list "x-bg-color" (cadr face))))
+       ((listp face)
+        (apply 'append (mapcar 'enriched-face-ans face)))
+       ((let* ((fg (face-attribute face :foreground))
+               (bg (face-attribute face :background))
                (props (face-font face t))
                (ans (cdr (format-annotate-single-property-change
                           'face nil props enriched-translations))))
-          (if fg (setq ans (cons (list "x-color" fg) ans)))
-          (if bg (setq ans (cons (list "x-bg-color" bg) ans)))
+          (unless (eq fg 'unspecified)
+            (setq ans (cons (list "x-color" fg) ans)))
+          (unless (eq bg 'unspecified)
+            (setq ans (cons (list "x-bg-color" bg) ans)))
           ans))))
 
 ;;;
@@ -401,12 +408,12 @@ Return value is \(begin end name positive-p), or nil if none was found."
        (delete-char 1)
       ;; A single < that does not start an annotation is an error,
       ;; which we note and then ignore.
-      (message "Warning: malformed annotation in file at %s" 
+      (message "Warning: malformed annotation in file at %s"
               (1- (point)))))
   (if (not (eobp))
       (let* ((beg (match-beginning 0))
             (end (match-end 0))
-            (name (downcase (buffer-substring 
+            (name (downcase (buffer-substring
                              (match-beginning 2) (match-end 2))))
             (pos (not (match-beginning 1))))
        (list beg end name pos))))
@@ -425,27 +432,43 @@ Return value is \(begin end name positive-p), or nil if none was found."
       (delete-char 1)))
 
 (defun enriched-decode-foreground (from to &optional color)
-  (let ((face (intern (concat "fg:" color))))
-    (cond ((null color)
-          (message "Warning: no color specified for <x-color>"))
-         ((internal-find-face face))
-         ((and window-system (facemenu-get-face face)))
-         (window-system
-          (message "Warning: color `%s' is not defined" color))
-         ((make-face face)
-          (message "Warning: color `%s' can't be displayed" color)))
-    (list from to 'face face)))
+  (if color
+      (list from to 'face (list ':foreground color))
+    (message "Warning: no color specified for <x-color>")
+    nil))
 
 (defun enriched-decode-background (from to &optional color)
-  (let ((face (intern (concat "bg:" color))))
-    (cond ((null color)
-          (message "Warning: no color specified for <x-bg-color>"))
-         ((internal-find-face face))
-         ((and window-system (facemenu-get-face face)))
-         (window-system
-          (message "Warning: color `%s' is not defined" color))
-         ((make-face face)
-          (message "Warning: color `%s' can't be displayed" color)))
-    (list from to 'face face)))
+  (if color
+      (list from to 'face (list ':background color))
+    (message "Warning: no color specified for <x-bg-color>")
+    nil))
+\f
+;;; Handling the `display' property.
+
+
+(defun enriched-handle-display-prop (old new)
+  "Return a list of annotations for a change in the `display' property.
+OLD is the old value of the property, NEW is the new value.  Value
+is a list `(CLOSE OPEN)', where CLOSE is a list of annotations to
+close and OPEN a list of annotations to open.  Each of these lists
+has the form `(ANNOTATION PARAM ...)'."
+  (let ((annotation "x-display")
+       (param (prin1-to-string (or old new))))
+    (if (null old)
+        (cons nil (list (list annotation param)))
+      (cons (list (list annotation param)) nil))))
+
+(defun enriched-decode-display-prop (start end &optional param)
+  "Decode a `display' property for text between START and END.
+PARAM is a `<param>' found for the property.
+Value is a list `(START END SYMBOL VALUE)' with START and END denoting
+the range of text to assign text property SYMBOL with value VALUE "
+  (let ((prop (when (stringp param)
+               (condition-case ()
+                   (car (read-from-string param))
+                 (error nil)))))
+    (unless prop
+      (message "Warning: invalid <x-display> parameter %s" param))
+    (list start end 'display prop)))
 
 ;;; enriched.el ends here