Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / textmodes / enriched.el
index 531fe7d..19dd446 100644 (file)
@@ -1,16 +1,17 @@
 ;;; enriched.el --- read and save files in text/enriched format
 
-;; Copyright (c) 1994, 1995, 1996, 2002 Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
 ;; Author: Boris Goldowsky <boris@gnu.org>
 ;; Keywords: wp, faces
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs 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, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,9 +19,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -47,7 +46,7 @@
 ;;;
 
 (defgroup enriched nil
-  "Read and save files in text/enriched format"
+  "Read and save files in text/enriched format."
   :group 'wp)
 
 (defcustom enriched-verbose t
@@ -65,7 +64,7 @@
 (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.
+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)
@@ -102,7 +101,7 @@ expression, which is evaluated to get the string to insert.")
 (defconst enriched-annotation-regexp "<\\(/\\)?\\([-A-Za-z0-9]+\\)>"
   "Regular expression matching enriched-text annotations.")
 
-(defconst enriched-translations
+(defvar enriched-translations
   '((face          (bold-italic "bold" "italic")
                   (bold        "bold")
                   (italic      "italic")
@@ -141,7 +140,6 @@ Any property that is neither on this list nor dealt with by
 
 ;;; Internal variables
 
-
 (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
@@ -155,6 +153,17 @@ them and their old values to `enriched-old-bindings'."
 The value is a list of \(VAR VALUE VAR VALUE...).")
 (make-variable-buffer-local 'enriched-old-bindings)
 
+;; The next variable is buffer local if and only if Enriched mode is
+;; enabled.  The buffer local value records whether
+;; `default-text-properties' should remain buffer local when disabling
+;; Enriched mode.  For technical reasons, the default value should be t.
+(defvar enriched-default-text-properties-local-flag t)
+
+;; Technical internal variable.  Bound to t if `enriched-mode' is
+;; being rerun by a major mode to allow it to restore buffer-local
+;; variables and to correctly update `enriched-old-bindings'.
+(defvar enriched-rerun-flag nil)
+
 ;;;
 ;;; Define the mode
 ;;;
@@ -165,7 +174,7 @@ The value is a list of \(VAR VALUE VAR VALUE...).")
   "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'.
+Turning the mode on or off runs `enriched-mode-hook'.
 
 More information about Enriched mode is available in the file
 etc/enriched.doc in the Emacs distribution directory.
@@ -173,31 +182,37 @@ etc/enriched.doc in the Emacs distribution directory.
 Commands:
 
 \\{enriched-mode-map}"
-  nil " Enriched" nil
+  :group 'enriched :lighter " Enriched"
   (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)
+          (set (pop enriched-old-bindings) (pop enriched-old-bindings)))
+        (unless enriched-default-text-properties-local-flag
+          (kill-local-variable 'default-text-properties))
+        (kill-local-variable 'enriched-default-text-properties-local-flag)
+        (unless use-hard-newlines (use-hard-newlines 0)))
+
+       ((and (memq 'text/enriched buffer-file-format)
+             (not enriched-rerun-flag))
         ;; Mode already on; do nothing.
         nil)
 
        (t                              ; Turn mode on
-        (push 'text/enriched buffer-file-format)
+        (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)
+                    'default-text-properties default-text-properties
+                    'use-hard-newlines use-hard-newlines))
+        (make-local-variable 'enriched-default-text-properties-local-flag)
+        (setq enriched-default-text-properties-local-flag
+              (local-variable-p 'default-text-properties))
         (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)
+        (setq buffer-display-table  enriched-display-table)
+        (use-hard-newlines 1 (if enriched-rerun-flag 'never nil))
         (let ((sticky (plist-get default-text-properties 'front-sticky))
               (p enriched-par-props))
           (dolist (x p)
@@ -207,6 +222,20 @@ Commands:
                     (plist-put default-text-properties
                                'front-sticky sticky)))))))
 
+(defun enriched-before-change-major-mode ()
+  (when enriched-mode
+    (while enriched-old-bindings
+      (set (pop enriched-old-bindings) (pop enriched-old-bindings)))))
+
+(add-hook 'change-major-mode-hook 'enriched-before-change-major-mode)
+
+(defun enriched-after-change-major-mode ()
+  (when enriched-mode
+    (let ((enriched-rerun-flag t))
+      (enriched-mode 1))))
+
+(add-hook 'after-change-major-mode-hook 'enriched-after-change-major-mode)
+
 ;;;
 ;;; Keybindings
 ;;;
@@ -222,14 +251,15 @@ Commands:
          (cons (cons 'enriched-mode enriched-mode-map)
                minor-mode-map-alist)))
 
-(define-key enriched-mode-map "\C-a" 'beginning-of-line-text)
+(define-key enriched-mode-map [remap move-beginning-of-line] 'beginning-of-line-text)
 (define-key enriched-mode-map "\C-m" 'reindent-then-newline-and-indent)
-(define-key enriched-mode-map "\C-j" 'reindent-then-newline-and-indent)
+(define-key enriched-mode-map
+  [remap newline-and-indent] 'reindent-then-newline-and-indent)
 (define-key enriched-mode-map "\M-j" 'facemenu-justification-menu)
 (define-key enriched-mode-map "\M-S" 'set-justification-center)
 (define-key enriched-mode-map "\C-x\t" 'increase-left-margin)
-(define-key enriched-mode-map "\C-c\C-l" 'set-left-margin)
-(define-key enriched-mode-map "\C-c\C-r" 'set-right-margin)
+(define-key enriched-mode-map "\C-c[" 'set-left-margin)
+(define-key enriched-mode-map "\C-c]" 'set-right-margin)
 
 ;;;
 ;;; Some functions dealing with text-properties, especially indentation
@@ -472,5 +502,5 @@ the range of text to assign text property SYMBOL with value VALUE."
       (message "Warning: invalid <x-display> parameter %s" param))
     (list start end 'display prop)))
 
-;;; arch-tag: 05cae488-3fea-45cd-ac29-5b02cb64e42b
+;; arch-tag: 05cae488-3fea-45cd-ac29-5b02cb64e42b
 ;;; enriched.el ends here