--- /dev/null
+;;; float.el --- obsolete floating point arithmetic package
+
+;; Copyright (C) 1986 Free Software Foundation, Inc.
+
+;; Author: Bill Rosenblatt
+;; Maintainer: FSF
+;; Keywords: extensions
+
+;; This file is part of GNU Emacs.
+
+;; 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.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; 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.
+
+;;; Commentary:
+
+;; Floating point numbers are represented by dot-pairs (mant . exp)
+;; where mant is the 24-bit signed integral mantissa and exp is the
+;; base 2 exponent.
+;;
+;; Emacs LISP supports a 24-bit signed integer data type, which has a
+;; range of -(2**23) to +(2**23)-1, or -8388608 to 8388607 decimal.
+;; This gives six significant decimal digit accuracy. Exponents can
+;; be anything in the range -(2**23) to +(2**23)-1.
+;;
+;; User interface:
+;; function f converts from integer to floating point
+;; function string-to-float converts from string to floating point
+;; function fint converts a floating point to integer (with truncation)
+;; function float-to-string converts from floating point to string
+;;
+;; Caveats:
+;; - Exponents outside of the range of +/-100 or so will cause certain
+;; functions (especially conversion routines) to take forever.
+;; - Very little checking is done for fixed point overflow/underflow.
+;; - No checking is done for over/underflow of the exponent
+;; (hardly necessary when exponent can be 2**23).
+;;
+;;
+;; Bill Rosenblatt
+;; June 20, 1986
+;;
+
+;;; Code:
+
+;; fundamental implementation constants
+(defconst exp-base 2
+ "Base of exponent in this floating point representation.")
+
+(defconst mantissa-bits 24
+ "Number of significant bits in this floating point representation.")
+
+(defconst decimal-digits 6
+ "Number of decimal digits expected to be accurate.")
+
+(defconst expt-digits 2
+ "Maximum permitted digits in a scientific notation exponent.")
+
+;; other constants
+(defconst maxbit (1- mantissa-bits)
+ "Number of highest bit")
+
+(defconst mantissa-maxval (1- (ash 1 maxbit))
+ "Maximum permissible value of mantissa")
+
+(defconst mantissa-minval (ash 1 maxbit)
+ "Minimum permissible value of mantissa")
+
+(defconst floating-point-regexp
+ "^[ \t]*\\(-?\\)\\([0-9]*\\)\
+\\(\\.\\([0-9]*\\)\\|\\)\
+\\(\\(\\([Ee]\\)\\(-?\\)\\([0-9][0-9]*\\)\\)\\|\\)[ \t]*$"
+ "Regular expression to match floating point numbers. Extract matches:
+1 - minus sign
+2 - integer part
+4 - fractional part
+8 - minus sign for power of ten
+9 - power of ten
+")
+
+(defconst high-bit-mask (ash 1 maxbit)
+ "Masks all bits except the high-order (sign) bit.")
+
+(defconst second-bit-mask (ash 1 (1- maxbit))
+ "Masks all bits except the highest-order magnitude bit")
+
+;; various useful floating point constants
+(defconst _f0 '(0 . 1))
+
+(defconst _f1/2 '(4194304 . -23))
+
+(defconst _f1 '(4194304 . -22))
+
+(defconst _f10 '(5242880 . -19))
+
+;; support for decimal conversion routines
+(defvar powers-of-10 (make-vector (1+ decimal-digits) _f1))
+(aset powers-of-10 1 _f10)
+(aset powers-of-10 2 '(6553600 . -16))
+(aset powers-of-10 3 '(8192000 . -13))
+(aset powers-of-10 4 '(5120000 . -9))
+(aset powers-of-10 5 '(6400000 . -6))
+(aset powers-of-10 6 '(8000000 . -3))
+
+(defconst all-decimal-digs-minval (aref powers-of-10 (1- decimal-digits)))
+(defconst highest-power-of-10 (aref powers-of-10 decimal-digits))
+
+(defun fashl (fnum) ; floating-point arithmetic shift left
+ (cons (ash (car fnum) 1) (1- (cdr fnum))))
+
+(defun fashr (fnum) ; floating point arithmetic shift right
+ (cons (ash (car fnum) -1) (1+ (cdr fnum))))
+
+(defun normalize (fnum)
+ (if (> (car fnum) 0) ; make sure next-to-highest bit is set
+ (while (zerop (logand (car fnum) second-bit-mask))
+ (setq fnum (fashl fnum)))
+ (if (< (car fnum) 0) ; make sure highest bit is set
+ (while (zerop (logand (car fnum) high-bit-mask))
+ (setq fnum (fashl fnum)))
+ (setq fnum _f0))) ; "standard 0"
+ fnum)
+
+(defun abs (n) ; integer absolute value
+ (if (>= n 0) n (- n)))
+
+(defun fabs (fnum) ; re-normalize after taking abs value
+ (normalize (cons (abs (car fnum)) (cdr fnum))))
+
+(defun xor (a b) ; logical exclusive or
+ (and (or a b) (not (and a b))))
+
+(defun same-sign (a b) ; two f-p numbers have same sign?
+ (not (xor (natnump (car a)) (natnump (car b)))))
+
+(defun extract-match (str i) ; used after string-match
+ (condition-case ()
+ (substring str (match-beginning i) (match-end i))
+ (error "")))
+
+;; support for the multiplication function
+(defconst halfword-bits (/ mantissa-bits 2)) ; bits in a halfword
+(defconst masklo (1- (ash 1 halfword-bits))) ; isolate the lower halfword
+(defconst maskhi (lognot masklo)) ; isolate the upper halfword
+(defconst round-limit (ash 1 (/ halfword-bits 2)))
+
+(defun hihalf (n) ; return high halfword, shifted down
+ (ash (logand n maskhi) (- halfword-bits)))
+
+(defun lohalf (n) ; return low halfword
+ (logand n masklo))
+
+;; Visible functions
+
+;; Arithmetic functions
+(defun f+ (a1 a2)
+ "Returns the sum of two floating point numbers."
+ (let ((f1 (fmax a1 a2))
+ (f2 (fmin a1 a2)))
+ (if (same-sign a1 a2)
+ (setq f1 (fashr f1) ; shift right to avoid overflow
+ f2 (fashr f2)))
+ (normalize
+ (cons (+ (car f1) (ash (car f2) (- (cdr f2) (cdr f1))))
+ (cdr f1)))))
+
+(defun f- (a1 &optional a2) ; unary or binary minus
+ "Returns the difference of two floating point numbers."
+ (if a2
+ (f+ a1 (f- a2))
+ (normalize (cons (- (car a1)) (cdr a1)))))
+
+(defun f* (a1 a2) ; multiply in halfword chunks
+ "Returns the product of two floating point numbers."
+ (let* ((i1 (car (fabs a1)))
+ (i2 (car (fabs a2)))
+ (sign (not (same-sign a1 a2)))
+ (prodlo (+ (hihalf (* (lohalf i1) (lohalf i2)))
+ (lohalf (* (hihalf i1) (lohalf i2)))
+ (lohalf (* (lohalf i1) (hihalf i2)))))
+ (prodhi (+ (* (hihalf i1) (hihalf i2))
+ (hihalf (* (hihalf i1) (lohalf i2)))
+ (hihalf (* (lohalf i1) (hihalf i2)))
+ (hihalf prodlo))))
+ (if (> (lohalf prodlo) round-limit)
+ (setq prodhi (1+ prodhi))) ; round off truncated bits
+ (normalize
+ (cons (if sign (- prodhi) prodhi)
+ (+ (cdr (fabs a1)) (cdr (fabs a2)) mantissa-bits)))))
+
+(defun f/ (a1 a2) ; SLOW subtract-and-shift algorithm
+ "Returns the quotient of two floating point numbers."
+ (if (zerop (car a2)) ; if divide by 0
+ (signal 'arith-error (list "attempt to divide by zero" a1 a2))
+ (let ((bits (1- maxbit))
+ (quotient 0)
+ (dividend (car (fabs a1)))
+ (divisor (car (fabs a2)))
+ (sign (not (same-sign a1 a2))))
+ (while (natnump bits)
+ (if (< (- dividend divisor) 0)
+ (setq quotient (ash quotient 1))
+ (setq quotient (1+ (ash quotient 1))
+ dividend (- dividend divisor)))
+ (setq dividend (ash dividend 1)
+ bits (1- bits)))
+ (normalize
+ (cons (if sign (- quotient) quotient)
+ (- (cdr (fabs a1)) (cdr (fabs a2)) (1- maxbit)))))))
+
+(defun f% (a1 a2)
+ "Returns the remainder of first floating point number divided by second."
+ (f- a1 (f* (ftrunc (f/ a1 a2)) a2)))
+
+
+;; Comparison functions
+(defun f= (a1 a2)
+ "Returns t if two floating point numbers are equal, nil otherwise."
+ (equal a1 a2))
+
+(defun f> (a1 a2)
+ "Returns t if first floating point number is greater than second,
+nil otherwise."
+ (cond ((and (natnump (car a1)) (< (car a2) 0))
+ t) ; a1 nonnegative, a2 negative
+ ((and (> (car a1) 0) (<= (car a2) 0))
+ t) ; a1 positive, a2 nonpositive
+ ((and (<= (car a1) 0) (natnump (car a2)))
+ nil) ; a1 nonpos, a2 nonneg
+ ((/= (cdr a1) (cdr a2)) ; same signs. exponents differ
+ (> (cdr a1) (cdr a2))) ; compare the mantissas.
+ (t
+ (> (car a1) (car a2))))) ; same exponents.
+
+(defun f>= (a1 a2)
+ "Returns t if first floating point number is greater than or equal to
+second, nil otherwise."
+ (or (f> a1 a2) (f= a1 a2)))
+
+(defun f< (a1 a2)
+ "Returns t if first floating point number is less than second,
+nil otherwise."
+ (not (f>= a1 a2)))
+
+(defun f<= (a1 a2)
+ "Returns t if first floating point number is less than or equal to
+second, nil otherwise."
+ (not (f> a1 a2)))
+
+(defun f/= (a1 a2)
+ "Returns t if first floating point number is not equal to second,
+nil otherwise."
+ (not (f= a1 a2)))
+
+(defun fmin (a1 a2)
+ "Returns the minimum of two floating point numbers."
+ (if (f< a1 a2) a1 a2))
+
+(defun fmax (a1 a2)
+ "Returns the maximum of two floating point numbers."
+ (if (f> a1 a2) a1 a2))
+
+(defun fzerop (fnum)
+ "Returns t if the floating point number is zero, nil otherwise."
+ (= (car fnum) 0))
+
+(defun floatp (fnum)
+ "Returns t if the arg is a floating point number, nil otherwise."
+ (and (consp fnum) (integerp (car fnum)) (integerp (cdr fnum))))
+
+;; Conversion routines
+(defun f (int)
+ "Convert the integer argument to floating point, like a C cast operator."
+ (normalize (cons int '0)))
+
+(defun int-to-hex-string (int)
+ "Convert the integer argument to a C-style hexadecimal string."
+ (let ((shiftval -20)
+ (str "0x")
+ (hex-chars "0123456789ABCDEF"))
+ (while (<= shiftval 0)
+ (setq str (concat str (char-to-string
+ (aref hex-chars
+ (logand (lsh int shiftval) 15))))
+ shiftval (+ shiftval 4)))
+ str))
+
+(defun ftrunc (fnum) ; truncate fractional part
+ "Truncate the fractional part of a floating point number."
+ (cond ((natnump (cdr fnum)) ; it's all integer, return number as is
+ fnum)
+ ((<= (cdr fnum) (- maxbit)) ; it's all fractional, return 0
+ '(0 . 1))
+ (t ; otherwise mask out fractional bits
+ (let ((mant (car fnum)) (exp (cdr fnum)))
+ (normalize
+ (cons (if (natnump mant) ; if negative, use absolute value
+ (ash (ash mant exp) (- exp))
+ (- (ash (ash (- mant) exp) (- exp))))
+ exp))))))
+
+(defun fint (fnum) ; truncate and convert to integer
+ "Convert the floating point number to integer, with truncation,
+like a C cast operator."
+ (let* ((tf (ftrunc fnum)) (tint (car tf)) (texp (cdr tf)))
+ (cond ((>= texp mantissa-bits) ; too high, return "maxint"
+ mantissa-maxval)
+ ((<= texp (- mantissa-bits)) ; too low, return "minint"
+ mantissa-minval)
+ (t ; in range
+ (ash tint texp))))) ; shift so that exponent is 0
+
+(defun float-to-string (fnum &optional sci)
+ "Convert the floating point number to a decimal string.
+Optional second argument non-nil means use scientific notation."
+ (let* ((value (fabs fnum)) (sign (< (car fnum) 0))
+ (power 0) (result 0) (str "")
+ (temp 0) (pow10 _f1))
+
+ (if (f= fnum _f0)
+ "0"
+ (if (f>= value _f1) ; find largest power of 10 <= value
+ (progn ; value >= 1, power is positive
+ (while (f<= (setq temp (f* pow10 highest-power-of-10)) value)
+ (setq pow10 temp
+ power (+ power decimal-digits)))
+ (while (f<= (setq temp (f* pow10 _f10)) value)
+ (setq pow10 temp
+ power (1+ power))))
+ (progn ; value < 1, power is negative
+ (while (f> (setq temp (f/ pow10 highest-power-of-10)) value)
+ (setq pow10 temp
+ power (- power decimal-digits)))
+ (while (f> pow10 value)
+ (setq pow10 (f/ pow10 _f10)
+ power (1- power)))))
+ ; get value in range 100000 to 999999
+ (setq value (f* (f/ value pow10) all-decimal-digs-minval)
+ result (ftrunc value))
+ (let (int)
+ (if (f> (f- value result) _f1/2) ; round up if remainder > 0.5
+ (setq int (1+ (fint result)))
+ (setq int (fint result)))
+ (setq str (int-to-string int))
+ (if (>= int 1000000)
+ (setq power (1+ power))))
+
+ (if sci ; scientific notation
+ (setq str (concat (substring str 0 1) "." (substring str 1)
+ "E" (int-to-string power)))
+
+ ; regular decimal string
+ (cond ((>= power (1- decimal-digits))
+ ; large power, append zeroes
+ (let ((zeroes (- power decimal-digits)))
+ (while (natnump zeroes)
+ (setq str (concat str "0")
+ zeroes (1- zeroes)))))
+
+ ; negative power, prepend decimal
+ ((< power 0) ; point and zeroes
+ (let ((zeroes (- (- power) 2)))
+ (while (natnump zeroes)
+ (setq str (concat "0" str)
+ zeroes (1- zeroes)))
+ (setq str (concat "0." str))))
+
+ (t ; in range, insert decimal point
+ (setq str (concat
+ (substring str 0 (1+ power))
+ "."
+ (substring str (1+ power)))))))
+
+ (if sign ; if negative, prepend minus sign
+ (concat "-" str)
+ str))))
+
+
+;; string to float conversion.
+;; accepts scientific notation, but ignores anything after the first two
+;; digits of the exponent.
+(defun string-to-float (str)
+ "Convert the string to a floating point number.
+Accepts a decimal string in scientific notation, with exponent preceded
+by either E or e. Only the six most significant digits of the integer
+and fractional parts are used; only the first two digits of the exponent
+are used. Negative signs preceding both the decimal number and the exponent
+are recognized."
+
+ (if (string-match floating-point-regexp str 0)
+ (let (power)
+ (f*
+ ; calculate the mantissa
+ (let* ((int-subst (extract-match str 2))
+ (fract-subst (extract-match str 4))
+ (digit-string (concat int-subst fract-subst))
+ (mant-sign (equal (extract-match str 1) "-"))
+ (leading-0s 0) (round-up nil))
+
+ ; get rid of leading 0's
+ (setq power (- (length int-subst) decimal-digits))
+ (while (and (< leading-0s (length digit-string))
+ (= (aref digit-string leading-0s) ?0))
+ (setq leading-0s (1+ leading-0s)))
+ (setq power (- power leading-0s)
+ digit-string (substring digit-string leading-0s))
+
+ ; if more than 6 digits, round off
+ (if (> (length digit-string) decimal-digits)
+ (setq round-up (>= (aref digit-string decimal-digits) ?5)
+ digit-string (substring digit-string 0 decimal-digits))
+ (setq power (+ power (- decimal-digits (length digit-string)))))
+
+ ; round up and add minus sign, if necessary
+ (f (* (+ (string-to-int digit-string)
+ (if round-up 1 0))
+ (if mant-sign -1 1))))
+
+ ; calculate the exponent (power of ten)
+ (let* ((expt-subst (extract-match str 9))
+ (expt-sign (equal (extract-match str 8) "-"))
+ (expt 0) (chunks 0) (tens 0) (exponent _f1)
+ (func 'f*))
+
+ (setq expt (+ (* (string-to-int
+ (substring expt-subst 0
+ (min expt-digits (length expt-subst))))
+ (if expt-sign -1 1))
+ power))
+ (if (< expt 0) ; if power of 10 negative
+ (setq expt (- expt) ; take abs val of exponent
+ func 'f/)) ; and set up to divide, not multiply
+
+ (setq chunks (/ expt decimal-digits)
+ tens (% expt decimal-digits))
+ ; divide or multiply by "chunks" of 10**6
+ (while (> chunks 0)
+ (setq exponent (funcall func exponent highest-power-of-10)
+ chunks (1- chunks)))
+ ; divide or multiply by remaining power of ten
+ (funcall func exponent (aref powers-of-10 tens)))))
+
+ _f0)) ; if invalid, return 0
+
+(provide 'float)
+
+;;; float.el ends here
--- /dev/null
+;;; options.el --- edit Options command for Emacs
+
+;; Copyright (C) 1985 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+
+;; This file is part of GNU Emacs.
+
+;; 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.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; 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.
+
+;;; Commentary:
+
+;; This code provides functions to list and edit the values of all global
+;; option variables known to loaded Emacs Lisp code. There are two entry
+;; points, `list-options' and `edit' options'. The latter enters a major
+;; mode specifically for editing option values. Do `M-x describe-mode' in
+;; that context for more details.
+
+;; The customization buffer feature is intended to make this obsolete.
+
+;;; Code:
+
+;;;###autoload
+(defun list-options ()
+ "Display a list of Emacs user options, with values and documentation.
+It is now better to use Customize instead."
+ (interactive)
+ (with-output-to-temp-buffer "*List Options*"
+ (let (vars)
+ (mapatoms (function (lambda (sym)
+ (if (user-variable-p sym)
+ (setq vars (cons sym vars))))))
+ (setq vars (sort vars 'string-lessp))
+ (while vars
+ (let ((sym (car vars)))
+ (when (boundp sym)
+ (princ ";; ")
+ (prin1 sym)
+ (princ ":\n\t")
+ (prin1 (symbol-value sym))
+ (terpri)
+ (princ (substitute-command-keys
+ (documentation-property sym 'variable-documentation)))
+ (princ "\n;;\n"))
+ (setq vars (cdr vars))))
+ (with-current-buffer "*List Options*"
+ (Edit-options-mode)
+ (setq buffer-read-only t)))))
+
+;;;###autoload
+(defun edit-options ()
+ "Edit a list of Emacs user option values.
+Selects a buffer containing such a list,
+in which there are commands to set the option values.
+Type \\[describe-mode] in that buffer for a list of commands.
+
+The Custom feature is intended to make this obsolete."
+ (interactive)
+ (list-options)
+ (pop-to-buffer "*List Options*"))
+
+(defvar Edit-options-mode-map
+ (let ((map (make-keymap)))
+ (define-key map "s" 'Edit-options-set)
+ (define-key map "x" 'Edit-options-toggle)
+ (define-key map "1" 'Edit-options-t)
+ (define-key map "0" 'Edit-options-nil)
+ (define-key map "p" 'backward-paragraph)
+ (define-key map " " 'forward-paragraph)
+ (define-key map "n" 'forward-paragraph)
+ map)
+ "")
+
+;; Edit Options mode is suitable only for specially formatted data.
+(put 'Edit-options-mode 'mode-class 'special)
+
+(defun Edit-options-mode ()
+ "\\<Edit-options-mode-map>\
+Major mode for editing Emacs user option settings.
+Special commands are:
+\\[Edit-options-set] -- set variable point points at. New value read using minibuffer.
+\\[Edit-options-toggle] -- toggle variable, t -> nil, nil -> t.
+\\[Edit-options-t] -- set variable to t.
+\\[Edit-options-nil] -- set variable to nil.
+Changed values made by these commands take effect immediately.
+
+Each variable description is a paragraph.
+For convenience, the characters \\[backward-paragraph] and \\[forward-paragraph] move back and forward by paragraphs."
+ (kill-all-local-variables)
+ (set-syntax-table emacs-lisp-mode-syntax-table)
+ (use-local-map Edit-options-mode-map)
+ (make-local-variable 'paragraph-separate)
+ (setq paragraph-separate "[^\^@-\^?]")
+ (make-local-variable 'paragraph-start)
+ (setq paragraph-start "\t")
+ (setq truncate-lines t)
+ (setq major-mode 'Edit-options-mode)
+ (setq mode-name "Options")
+ (run-hooks 'Edit-options-mode-hook))
+
+(defun Edit-options-set () (interactive)
+ (Edit-options-modify
+ (lambda (var) (eval-minibuffer (concat "New " (symbol-name var) ": ")))))
+
+(defun Edit-options-toggle () (interactive)
+ (Edit-options-modify (lambda (var) (not (symbol-value var)))))
+
+(defun Edit-options-t () (interactive)
+ (Edit-options-modify (lambda (var) t)))
+
+(defun Edit-options-nil () (interactive)
+ (Edit-options-modify (lambda (var) nil)))
+
+(defun Edit-options-modify (modfun)
+ (save-excursion
+ (let ((buffer-read-only nil) var pos)
+ (re-search-backward "^;; \\|\\`")
+ (forward-char 3)
+ (setq pos (point))
+ (save-restriction
+ (narrow-to-region pos (progn (end-of-line) (1- (point))))
+ (goto-char pos)
+ (setq var (read (current-buffer))))
+ (goto-char pos)
+ (forward-line 1)
+ (forward-char 1)
+ (save-excursion
+ (set var (funcall modfun var)))
+ (kill-sexp 1)
+ (prin1 (symbol-value var) (current-buffer)))))
+
+(provide 'options)
+
+;;; options.el ends here
--- /dev/null
+;;; scribe.el --- scribe mode, and its idiosyncratic commands
+
+;; Copyright (C) 1985 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: wp
+
+;; This file is part of GNU Emacs.
+
+;; 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.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; 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.
+
+;;; Commentary:
+
+;; A major mode for editing source in written for the Scribe text formatter.
+;; Knows about Scribe syntax and standard layout rules. The command to
+;; run Scribe on a buffer is bogus; someone interested should fix it.
+
+;;; Code:
+
+(defgroup scribe nil
+ "Scribe mode."
+ :prefix "scribe-"
+ :group 'wp)
+
+(defvar scribe-mode-syntax-table nil
+ "Syntax table used while in scribe mode.")
+
+(defvar scribe-mode-abbrev-table nil
+ "Abbrev table used while in scribe mode.")
+
+(defcustom scribe-fancy-paragraphs nil
+ "*Non-nil makes Scribe mode use a different style of paragraph separation."
+ :type 'boolean
+ :group 'scribe)
+
+(defcustom scribe-electric-quote nil
+ "*Non-nil makes insert of double quote use `` or '' depending on context."
+ :type 'boolean
+ :group 'scribe)
+
+(defcustom scribe-electric-parenthesis nil
+ "*Non-nil makes parenthesis char ( (]}> ) automatically insert its close
+if typed after an @Command form."
+ :type 'boolean
+ :group 'scribe)
+
+(defconst scribe-open-parentheses "[({<"
+ "Open parenthesis characters for Scribe.")
+
+(defconst scribe-close-parentheses "])}>"
+ "Close parenthesis characters for Scribe.
+These should match up with `scribe-open-parenthesis'.")
+
+(if (null scribe-mode-syntax-table)
+ (let ((st (syntax-table)))
+ (unwind-protect
+ (progn
+ (setq scribe-mode-syntax-table (copy-syntax-table
+ text-mode-syntax-table))
+ (set-syntax-table scribe-mode-syntax-table)
+ (modify-syntax-entry ?\" " ")
+ (modify-syntax-entry ?\\ " ")
+ (modify-syntax-entry ?@ "w ")
+ (modify-syntax-entry ?< "(> ")
+ (modify-syntax-entry ?> ")< ")
+ (modify-syntax-entry ?[ "(] ")
+ (modify-syntax-entry ?] ")[ ")
+ (modify-syntax-entry ?{ "(} ")
+ (modify-syntax-entry ?} "){ ")
+ (modify-syntax-entry ?' "w "))
+ (set-syntax-table st))))
+
+(defvar scribe-mode-map nil)
+
+(if scribe-mode-map
+ nil
+ (setq scribe-mode-map (make-sparse-keymap))
+ (define-key scribe-mode-map "\t" 'scribe-tab)
+ (define-key scribe-mode-map "\e\t" 'tab-to-tab-stop)
+ (define-key scribe-mode-map "\es" 'center-line)
+ (define-key scribe-mode-map "\e}" 'up-list)
+ (define-key scribe-mode-map "\eS" 'center-paragraph)
+ (define-key scribe-mode-map "\"" 'scribe-insert-quote)
+ (define-key scribe-mode-map "(" 'scribe-parenthesis)
+ (define-key scribe-mode-map "[" 'scribe-parenthesis)
+ (define-key scribe-mode-map "{" 'scribe-parenthesis)
+ (define-key scribe-mode-map "<" 'scribe-parenthesis)
+ (define-key scribe-mode-map "\C-c\C-c" 'scribe-chapter)
+ (define-key scribe-mode-map "\C-c\C-t" 'scribe-section)
+ (define-key scribe-mode-map "\C-c\C-s" 'scribe-subsection)
+ (define-key scribe-mode-map "\C-c\C-v" 'scribe-insert-environment)
+ (define-key scribe-mode-map "\C-c\C-e" 'scribe-bracket-region-be)
+ (define-key scribe-mode-map "\C-c[" 'scribe-begin)
+ (define-key scribe-mode-map "\C-c]" 'scribe-end)
+ (define-key scribe-mode-map "\C-c\C-i" 'scribe-italicize-word)
+ (define-key scribe-mode-map "\C-c\C-b" 'scribe-bold-word)
+ (define-key scribe-mode-map "\C-c\C-u" 'scribe-underline-word))
+
+;;;###autoload
+(define-derived-mode scribe-mode text-mode "Scribe"
+ "Major mode for editing files of Scribe (a text formatter) source.
+Scribe-mode is similar to text-mode, with a few extra commands added.
+\\{scribe-mode-map}
+
+Interesting variables:
+
+`scribe-fancy-paragraphs'
+ Non-nil makes Scribe mode use a different style of paragraph separation.
+
+`scribe-electric-quote'
+ Non-nil makes insert of double quote use `` or '' depending on context.
+
+`scribe-electric-parenthesis'
+ Non-nil makes an open-parenthesis char (one of `([<{')
+ automatically insert its close if typed after an @Command form."
+ (set (make-local-variable 'comment-start) "@Comment[")
+ (set (make-local-variable 'comment-start-skip) (concat "@Comment[" scribe-open-parentheses "]"))
+ (set (make-local-variable 'comment-column) 0)
+ (set (make-local-variable 'comment-end) "]")
+ (set (make-local-variable 'paragraph-start)
+ (concat "\\([\n\f]\\)\\|\\(@\\w+["
+ scribe-open-parentheses
+ "].*["
+ scribe-close-parentheses
+ "]$\\)"))
+ (set (make-local-variable 'paragraph-separate)
+ (if scribe-fancy-paragraphs paragraph-start "$"))
+ (set (make-local-variable 'sentence-end)
+ "\\([.?!]\\|@:\\)[]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*")
+ (set (make-local-variable 'compile-command)
+ (concat "scribe " (buffer-file-name))))
+
+(defun scribe-tab ()
+ (interactive)
+ (insert "@\\"))
+
+;; This algorithm could probably be improved somewhat.
+;; Right now, it loses seriously...
+
+(defun scribe ()
+ "Run Scribe on the current buffer."
+ (interactive)
+ (call-interactively 'compile))
+
+(defun scribe-envelop-word (string count)
+ "Surround current word with Scribe construct @STRING[...].
+COUNT specifies how many words to surround. A negative count means
+to skip backward."
+ (let ((spos (point)) (epos (point)) (ccoun 0) noparens)
+ (if (not (zerop count))
+ (progn (if (= (char-syntax (preceding-char)) ?w)
+ (forward-sexp (min -1 count)))
+ (setq spos (point))
+ (if (looking-at (concat "@\\w[" scribe-open-parentheses "]"))
+ (forward-char 2)
+ (goto-char epos)
+ (skip-chars-backward "\\W")
+ (forward-char -1))
+ (forward-sexp (max count 1))
+ (setq epos (point))))
+ (goto-char spos)
+ (while (and (< ccoun (length scribe-open-parentheses))
+ (save-excursion
+ (or (search-forward (char-to-string
+ (aref scribe-open-parentheses ccoun))
+ epos t)
+ (search-forward (char-to-string
+ (aref scribe-close-parentheses ccoun))
+ epos t)))
+ (setq ccoun (1+ ccoun))))
+ (if (>= ccoun (length scribe-open-parentheses))
+ (progn (goto-char epos)
+ (insert "@end(" string ")")
+ (goto-char spos)
+ (insert "@begin(" string ")"))
+ (goto-char epos)
+ (insert (aref scribe-close-parentheses ccoun))
+ (goto-char spos)
+ (insert "@" string (aref scribe-open-parentheses ccoun))
+ (goto-char epos)
+ (forward-char 3)
+ (skip-chars-forward scribe-close-parentheses))))
+
+(defun scribe-underline-word (count)
+ "Underline COUNT words around point by means of Scribe constructs."
+ (interactive "p")
+ (scribe-envelop-word "u" count))
+
+(defun scribe-bold-word (count)
+ "Boldface COUNT words around point by means of Scribe constructs."
+ (interactive "p")
+ (scribe-envelop-word "b" count))
+
+(defun scribe-italicize-word (count)
+ "Italicize COUNT words around point by means of Scribe constructs."
+ (interactive "p")
+ (scribe-envelop-word "i" count))
+
+(defun scribe-begin ()
+ (interactive)
+ (insert "\n")
+ (forward-char -1)
+ (scribe-envelop-word "Begin" 0)
+ (re-search-forward (concat "[" scribe-open-parentheses "]")))
+
+(defun scribe-end ()
+ (interactive)
+ (insert "\n")
+ (forward-char -1)
+ (scribe-envelop-word "End" 0)
+ (re-search-forward (concat "[" scribe-open-parentheses "]")))
+
+(defun scribe-chapter ()
+ (interactive)
+ (insert "\n")
+ (forward-char -1)
+ (scribe-envelop-word "Chapter" 0)
+ (re-search-forward (concat "[" scribe-open-parentheses "]")))
+
+(defun scribe-section ()
+ (interactive)
+ (insert "\n")
+ (forward-char -1)
+ (scribe-envelop-word "Section" 0)
+ (re-search-forward (concat "[" scribe-open-parentheses "]")))
+
+(defun scribe-subsection ()
+ (interactive)
+ (insert "\n")
+ (forward-char -1)
+ (scribe-envelop-word "SubSection" 0)
+ (re-search-forward (concat "[" scribe-open-parentheses "]")))
+
+(defun scribe-bracket-region-be (env min max)
+ (interactive "sEnvironment: \nr")
+ (save-excursion
+ (goto-char max)
+ (insert "@end(" env ")\n")
+ (goto-char min)
+ (insert "@begin(" env ")\n")))
+
+(defun scribe-insert-environment (env)
+ (interactive "sEnvironment: ")
+ (scribe-bracket-region-be env (point) (point))
+ (forward-line 1)
+ (insert ?\n)
+ (forward-char -1))
+
+(defun scribe-insert-quote (count)
+ "Insert ``, '' or \" according to preceding character.
+If `scribe-electric-quote' is non-nil, insert ``, '' or \" according
+to preceding character. With numeric arg N, always insert N \" characters.
+Else just insert \"."
+ (interactive "P")
+ (if (or count (not scribe-electric-quote))
+ (self-insert-command (prefix-numeric-value count))
+ (let (lastfore lastback lastquote)
+ (insert
+ (cond
+ ((= (preceding-char) ?\\) ?\")
+ ((bobp) "``")
+ (t
+ (setq lastfore (save-excursion (and (search-backward
+ "``" (- (point) 1000) t)
+ (point)))
+ lastback (save-excursion (and (search-backward
+ "''" (- (point) 1000) t)
+ (point)))
+ lastquote (save-excursion (and (search-backward
+ "\"" (- (point) 100) t)
+ (point))))
+ (if (not lastquote)
+ (cond ((not lastfore) "``")
+ ((not lastback) "''")
+ ((> lastfore lastback) "''")
+ (t "``"))
+ (cond ((and (not lastback) (not lastfore)) "\"")
+ ((and lastback (not lastfore) (> lastquote lastback)) "\"")
+ ((and lastback (not lastfore) (> lastback lastquote)) "``")
+ ((and lastfore (not lastback) (> lastquote lastfore)) "\"")
+ ((and lastfore (not lastback) (> lastfore lastquote)) "''")
+ ((and (> lastquote lastfore) (> lastquote lastback)) "\"")
+ ((> lastfore lastback) "''")
+ (t "``")))))))))
+
+(defun scribe-parenthesis (count)
+ "If scribe-electric-parenthesis is non-nil, insertion of an open-parenthesis
+character inserts the following close parenthesis character if the
+preceding text is of the form @Command."
+ (interactive "P")
+ (self-insert-command (prefix-numeric-value count))
+ (let (at-command paren-char point-save)
+ (if (or count (not scribe-electric-parenthesis))
+ nil
+ (save-excursion
+ (forward-char -1)
+ (setq point-save (point))
+ (skip-chars-backward (concat "^ \n\t\f" scribe-open-parentheses))
+ (setq at-command (and (equal (following-char) ?@)
+ (/= (point) (1- point-save)))))
+ (if (and at-command
+ (setq paren-char
+ (string-match (regexp-quote
+ (char-to-string (preceding-char)))
+ scribe-open-parentheses)))
+ (save-excursion
+ (insert (aref scribe-close-parentheses paren-char)))))))
+
+(provide 'scribe)
+
+;;; scribe.el ends here