Mercurial > hg > xemacs-beta
view lisp/cl-extra.el @ 665:fdefd0186b75
[xemacs-hg @ 2001-09-20 06:28:42 by ben]
The great integral types renaming.
The purpose of this is to rationalize the names used for various
integral types, so that they match their intended uses and follow
consist conventions, and eliminate types that were not semantically
different from each other.
The conventions are:
-- All integral types that measure quantities of anything are
signed. Some people disagree vociferously with this, but their
arguments are mostly theoretical, and are vastly outweighed by
the practical headaches of mixing signed and unsigned values,
and more importantly by the far increased likelihood of
inadvertent bugs: Because of the broken "viral" nature of
unsigned quantities in C (operations involving mixed
signed/unsigned are done unsigned, when exactly the opposite is
nearly always wanted), even a single error in declaring a
quantity unsigned that should be signed, or even the even more
subtle error of comparing signed and unsigned values and
forgetting the necessary cast, can be catastrophic, as
comparisons will yield wrong results. -Wsign-compare is turned
on specifically to catch this, but this tends to result in a
great number of warnings when mixing signed and unsigned, and
the casts are annoying. More has been written on this
elsewhere.
-- All such quantity types just mentioned boil down to EMACS_INT,
which is 32 bits on 32-bit machines and 64 bits on 64-bit
machines. This is guaranteed to be the same size as Lisp
objects of type `int', and (as far as I can tell) of size_t
(unsigned!) and ssize_t. The only type below that is not an
EMACS_INT is Hashcode, which is an unsigned value of the same
size as EMACS_INT.
-- Type names should be relatively short (no more than 10
characters or so), with the first letter capitalized and no
underscores if they can at all be avoided.
-- "count" == a zero-based measurement of some quantity. Includes
sizes, offsets, and indexes.
-- "bpos" == a one-based measurement of a position in a buffer.
"Charbpos" and "Bytebpos" count text in the buffer, rather than
bytes in memory; thus Bytebpos does not directly correspond to
the memory representation. Use "Membpos" for this.
-- "Char" refers to internal-format characters, not to the C type
"char", which is really a byte.
-- For the actual name changes, see the script below.
I ran the following script to do the conversion. (NOTE: This script
is idempotent. You can safely run it multiple times and it will
not screw up previous results -- in fact, it will do nothing if
nothing has changed. Thus, it can be run repeatedly as necessary
to handle patches coming in from old workspaces, or old branches.)
There are two tags, just before and just after the change:
`pre-integral-type-rename' and `post-integral-type-rename'. When
merging code from the main trunk into a branch, the best thing to
do is first merge up to `pre-integral-type-rename', then apply the
script and associated changes, then merge from
`post-integral-type-change' to the present. (Alternatively, just do
the merging in one operation; but you may then have a lot of
conflicts needing to be resolved by hand.)
Script `fixtypes.sh' follows:
----------------------------------- cut ------------------------------------
files="*.[ch] s/*.h m/*.h config.h.in ../configure.in Makefile.in.in ../lib-src/*.[ch] ../lwlib/*.[ch]"
gr Memory_Count Bytecount $files
gr Lstream_Data_Count Bytecount $files
gr Element_Count Elemcount $files
gr Hash_Code Hashcode $files
gr extcount bytecount $files
gr bufpos charbpos $files
gr bytind bytebpos $files
gr memind membpos $files
gr bufbyte intbyte $files
gr Extcount Bytecount $files
gr Bufpos Charbpos $files
gr Bytind Bytebpos $files
gr Memind Membpos $files
gr Bufbyte Intbyte $files
gr EXTCOUNT BYTECOUNT $files
gr BUFPOS CHARBPOS $files
gr BYTIND BYTEBPOS $files
gr MEMIND MEMBPOS $files
gr BUFBYTE INTBYTE $files
gr MEMORY_COUNT BYTECOUNT $files
gr LSTREAM_DATA_COUNT BYTECOUNT $files
gr ELEMENT_COUNT ELEMCOUNT $files
gr HASH_CODE HASHCODE $files
----------------------------------- cut ------------------------------------
`fixtypes.sh' is a Bourne-shell script; it uses 'gr':
----------------------------------- cut ------------------------------------
#!/bin/sh
# Usage is like this:
# gr FROM TO FILES ...
# globally replace FROM with TO in FILES. FROM and TO are regular expressions.
# backup files are stored in the `backup' directory.
from="$1"
to="$2"
shift 2
echo ${1+"$@"} | xargs global-replace "s/$from/$to/g"
----------------------------------- cut ------------------------------------
`gr' in turn uses a Perl script to do its real work,
`global-replace', which follows:
----------------------------------- cut ------------------------------------
: #-*- Perl -*-
### global-modify --- modify the contents of a file by a Perl expression
## Copyright (C) 1999 Martin Buchholz.
## Copyright (C) 2001 Ben Wing.
## Authors: Martin Buchholz <martin@xemacs.org>, Ben Wing <ben@xemacs.org>
## Maintainer: Ben Wing <ben@xemacs.org>
## Current Version: 1.0, May 5, 2001
# 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, 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
# 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 XEmacs; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
eval 'exec perl -w -S $0 ${1+"$@"}'
if 0;
use strict;
use FileHandle;
use Carp;
use Getopt::Long;
use File::Basename;
(my $myName = $0) =~ s@.*/@@; my $usage="
Usage: $myName [--help] [--backup-dir=DIR] [--line-mode] [--hunk-mode]
PERLEXPR FILE ...
Globally modify a file, either line by line or in one big hunk.
Typical usage is like this:
[with GNU print, GNU xargs: guaranteed to handle spaces, quotes, etc.
in file names]
find . -name '*.[ch]' -print0 | xargs -0 $0 's/\bCONST\b/const/g'\n
[with non-GNU print, xargs]
find . -name '*.[ch]' -print | xargs $0 's/\bCONST\b/const/g'\n
The file is read in, either line by line (with --line-mode specified)
or in one big hunk (with --hunk-mode specified; it's the default), and
the Perl expression is then evalled with \$_ set to the line or hunk of
text, including the terminating newline if there is one. It should
destructively modify the value there, storing the changed result in \$_.
Files in which any modifications are made are backed up to the directory
specified using --backup-dir, or to `backup' by default. To disable this,
use --backup-dir= with no argument.
Hunk mode is the default because it is MUCH MUCH faster than line-by-line.
Use line-by-line only when it matters, e.g. you want to do a replacement
only once per line (the default without the `g' argument). Conversely,
when using hunk mode, *ALWAYS* use `g'; otherwise, you will only make one
replacement in the entire file!
";
my %options = ();
$Getopt::Long::ignorecase = 0;
&GetOptions (
\%options,
'help', 'backup-dir=s', 'line-mode', 'hunk-mode',
);
die $usage if $options{"help"} or @ARGV <= 1;
my $code = shift;
die $usage if grep (-d || ! -w, @ARGV);
sub SafeOpen {
open ((my $fh = new FileHandle), $_[0]);
confess "Can't open $_[0]: $!" if ! defined $fh;
return $fh;
}
sub SafeClose {
close $_[0] or confess "Can't close $_[0]: $!";
}
sub FileContents {
my $fh = SafeOpen ("< $_[0]");
my $olddollarslash = $/;
local $/ = undef;
my $contents = <$fh>;
$/ = $olddollarslash;
return $contents;
}
sub WriteStringToFile {
my $fh = SafeOpen ("> $_[0]");
binmode $fh;
print $fh $_[1] or confess "$_[0]: $!\n";
SafeClose $fh;
}
foreach my $file (@ARGV) {
my $changed_p = 0;
my $new_contents = "";
if ($options{"line-mode"}) {
my $fh = SafeOpen $file;
while (<$fh>) {
my $save_line = $_;
eval $code;
$changed_p = 1 if $save_line ne $_;
$new_contents .= $_;
}
} else {
my $orig_contents = $_ = FileContents $file;
eval $code;
if ($_ ne $orig_contents) {
$changed_p = 1;
$new_contents = $_;
}
}
if ($changed_p) {
my $backdir = $options{"backup-dir"};
$backdir = "backup" if !defined ($backdir);
if ($backdir) {
my ($name, $path, $suffix) = fileparse ($file, "");
my $backfulldir = $path . $backdir;
my $backfile = "$backfulldir/$name";
mkdir $backfulldir, 0755 unless -d $backfulldir;
print "modifying $file (original saved in $backfile)\n";
rename $file, $backfile;
}
WriteStringToFile ($file, $new_contents);
}
}
----------------------------------- cut ------------------------------------
In addition to those programs, I needed to fix up a few other
things, particularly relating to the duplicate definitions of
types, now that some types merged with others. Specifically:
1. in lisp.h, removed duplicate declarations of Bytecount. The
changed code should now look like this: (In each code snippet
below, the first and last lines are the same as the original, as
are all lines outside of those lines. That allows you to locate
the section to be replaced, and replace the stuff in that
section, verifying that there isn't anything new added that
would need to be kept.)
--------------------------------- snip -------------------------------------
/* Counts of bytes or chars */
typedef EMACS_INT Bytecount;
typedef EMACS_INT Charcount;
/* Counts of elements */
typedef EMACS_INT Elemcount;
/* Hash codes */
typedef unsigned long Hashcode;
/* ------------------------ dynamic arrays ------------------- */
--------------------------------- snip -------------------------------------
2. in lstream.h, removed duplicate declaration of Bytecount.
Rewrote the comment about this type. The changed code should
now look like this:
--------------------------------- snip -------------------------------------
#endif
/* The have been some arguments over the what the type should be that
specifies a count of bytes in a data block to be written out or read in,
using Lstream_read(), Lstream_write(), and related functions.
Originally it was long, which worked fine; Martin "corrected" these to
size_t and ssize_t on the grounds that this is theoretically cleaner and
is in keeping with the C standards. Unfortunately, this practice is
horribly error-prone due to design flaws in the way that mixed
signed/unsigned arithmetic happens. In fact, by doing this change,
Martin introduced a subtle but fatal error that caused the operation of
sending large mail messages to the SMTP server under Windows to fail.
By putting all values back to be signed, avoiding any signed/unsigned
mixing, the bug immediately went away. The type then in use was
Lstream_Data_Count, so that it be reverted cleanly if a vote came to
that. Now it is Bytecount.
Some earlier comments about why the type must be signed: This MUST BE
SIGNED, since it also is used in functions that return the number of
bytes actually read to or written from in an operation, and these
functions can return -1 to signal error.
Note that the standard Unix read() and write() functions define the
count going in as a size_t, which is UNSIGNED, and the count going
out as an ssize_t, which is SIGNED. This is a horrible design
flaw. Not only is it highly likely to lead to logic errors when a
-1 gets interpreted as a large positive number, but operations are
bound to fail in all sorts of horrible ways when a number in the
upper-half of the size_t range is passed in -- this number is
unrepresentable as an ssize_t, so code that checks to see how many
bytes are actually written (which is mandatory if you are dealing
with certain types of devices) will get completely screwed up.
--ben
*/
typedef enum lstream_buffering
--------------------------------- snip -------------------------------------
3. in dumper.c, there are four places, all inside of switch()
statements, where XD_BYTECOUNT appears twice as a case tag. In
each case, the two case blocks contain identical code, and you
should *REMOVE THE SECOND* and leave the first.
author | ben |
---|---|
date | Thu, 20 Sep 2001 06:31:11 +0000 |
parents | 023b83f4e54b |
children | 79940b592197 |
line wrap: on
line source
;;; cl-extra.el --- Common Lisp extensions for XEmacs Lisp (part two) ;; Copyright (C) 1993 Free Software Foundation, Inc. ;; Author: Dave Gillespie <daveg@synaptics.com> ;; Maintainer: XEmacs Development Team ;; Version: 2.02 ;; Keywords: extensions, dumped ;; This file is part of XEmacs. ;; XEmacs 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. ;; XEmacs 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 XEmacs; see the file COPYING. If not, write to the Free ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ;; 02111-1307, USA. ;;; Synched up with: FSF 19.34. ;;; Commentary: ;; This file is dumped with XEmacs. ;; These are extensions to Emacs Lisp that provide a degree of ;; Common Lisp compatibility, beyond what is already built-in ;; in Emacs Lisp. ;; ;; This package was written by Dave Gillespie; it is a complete ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. ;; ;; This package works with Emacs 18, Emacs 19, and XEmacs/Lucid Emacs 19. ;; ;; Bug reports, comments, and suggestions are welcome! ;; This file contains portions of the Common Lisp extensions ;; package which are autoloaded since they are relatively obscure. ;; See cl.el for Change Log. ;;; Code: (eval-when-compile (require 'obsolete)) (or (memq 'cl-19 features) (error "Tried to load `cl-extra' before `cl'!")) ;;; We define these here so that this file can compile without having ;;; loaded the cl.el file already. (defmacro cl-push (x place) (list 'setq place (list 'cons x place))) (defmacro cl-pop (place) (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))) (defvar cl-emacs-type) ;;; Type coercion. (defun coerce (x type) "Coerce OBJECT to type TYPE. TYPE is a Common Lisp type specifier." (cond ((eq type 'list) (if (listp x) x (append x nil))) ((eq type 'vector) (if (vectorp x) x (vconcat x))) ((eq type 'string) (if (stringp x) x (concat x))) ((eq type 'array) (if (arrayp x) x (vconcat x))) ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0)) ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type)) ((and (eq type 'character) (char-int-p x)) (int-char x)) ((and (eq type 'integer) (characterp x)) (char-int x)) ((eq type 'float) (float x)) ((eq type 'bit-vector) (if (bit-vector-p x) x (apply 'bit-vector (append x nil)))) ((eq type 'weak-list) (if (weak-list-p x) x (let ((wl (make-weak-list))) (set-weak-list-list wl (if (listp x) x (append x nil))) wl))) ((typep x type) x) (t (error "Can't coerce %s to type %s" x type)))) ;;; Predicates. (defun equalp (x y) "Return t if two Lisp objects have similar structures and contents. This is like `equal', except that it accepts numerically equal numbers of different types (float vs. integer), and also compares strings case-insensitively." (cond ((eq x y) t) ((stringp x) (and (stringp y) (= (length x) (length y)) (or (string-equal x y) (string-equal (downcase x) (downcase y))))) ; lazy but simple! ((characterp x) (and (characterp y) (or (char-equal x y) (char-equal (downcase x) (downcase y))))) ((numberp x) (and (numberp y) (= x y))) ((consp x) ;; XEmacs change (while (and (consp x) (consp y) (equalp (car x) (car y))) (cl-pop x) (cl-pop y)) (and (not (consp x)) (equalp x y))) ((vectorp x) (and (vectorp y) (= (length x) (length y)) (let ((i (length x))) (while (and (>= (setq i (1- i)) 0) (equalp (aref x i) (aref y i)))) (< i 0)))) (t (equal x y)))) ;;; Control structures. (defun cl-mapcar-many (cl-func cl-seqs) (if (cdr (cdr cl-seqs)) (let* ((cl-res nil) (cl-n (apply 'min (mapcar 'length cl-seqs))) (cl-i 0) (cl-args (copy-sequence cl-seqs)) cl-p1 cl-p2) (setq cl-seqs (copy-sequence cl-seqs)) (while (< cl-i cl-n) (setq cl-p1 cl-seqs cl-p2 cl-args) (while cl-p1 (setcar cl-p2 (if (consp (car cl-p1)) (prog1 (car (car cl-p1)) (setcar cl-p1 (cdr (car cl-p1)))) (aref (car cl-p1) cl-i))) (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))) (cl-push (apply cl-func cl-args) cl-res) (setq cl-i (1+ cl-i))) (nreverse cl-res)) (let ((cl-res nil) (cl-x (car cl-seqs)) (cl-y (nth 1 cl-seqs))) (let ((cl-n (min (length cl-x) (length cl-y))) (cl-i -1)) (while (< (setq cl-i (1+ cl-i)) cl-n) (cl-push (funcall cl-func (if (consp cl-x) (cl-pop cl-x) (aref cl-x cl-i)) (if (consp cl-y) (cl-pop cl-y) (aref cl-y cl-i))) cl-res))) (nreverse cl-res)))) (defun map (cl-type cl-func cl-seq &rest cl-rest) "Map a function across one or more sequences, returning a sequence. TYPE is the sequence type to return, FUNC is the function, and SEQS are the argument sequences." (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest))) (and cl-type (coerce cl-res cl-type)))) (defun maplist (cl-func cl-list &rest cl-rest) "Map FUNC to each sublist of LIST or LISTS. Like `mapcar', except applies to lists and their cdr's rather than to the elements themselves." (if cl-rest (let ((cl-res nil) (cl-args (cons cl-list (copy-sequence cl-rest))) cl-p) (while (not (memq nil cl-args)) (cl-push (apply cl-func cl-args) cl-res) (setq cl-p cl-args) (while cl-p (setcar cl-p (cdr (cl-pop cl-p)) ))) (nreverse cl-res)) (let ((cl-res nil)) (while cl-list (cl-push (funcall cl-func cl-list) cl-res) (setq cl-list (cdr cl-list))) (nreverse cl-res)))) (defun mapc (cl-func cl-seq &rest cl-rest) "Like `mapcar', but does not accumulate values returned by the function." (if cl-rest (apply 'map nil cl-func cl-seq cl-rest) ;; XEmacs change: in the simplest case we call mapc-internal, ;; which really doesn't accumulate any results. (mapc-internal cl-func cl-seq)) cl-seq) (defun mapl (cl-func cl-list &rest cl-rest) "Like `maplist', but does not accumulate values returned by the function." (if cl-rest (apply 'maplist cl-func cl-list cl-rest) (let ((cl-p cl-list)) (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p))))) cl-list) (defun mapcan (cl-func cl-seq &rest cl-rest) "Like `mapcar', but nconc's together the values returned by the function." (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest))) (defun mapcon (cl-func cl-list &rest cl-rest) "Like `maplist', but nconc's together the values returned by the function." (apply 'nconc (apply 'maplist cl-func cl-list cl-rest))) (defun some (cl-pred cl-seq &rest cl-rest) "Return true if PREDICATE is true of any element of SEQ or SEQs. If so, return the true (non-nil) value returned by PREDICATE." (if (or cl-rest (nlistp cl-seq)) (catch 'cl-some (apply 'map nil (function (lambda (&rest cl-x) (let ((cl-res (apply cl-pred cl-x))) (if cl-res (throw 'cl-some cl-res))))) cl-seq cl-rest) nil) (let ((cl-x nil)) (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq)))))) cl-x))) (defun every (cl-pred cl-seq &rest cl-rest) "Return true if PREDICATE is true of every element of SEQ or SEQs." (if (or cl-rest (nlistp cl-seq)) (catch 'cl-every (apply 'map nil (function (lambda (&rest cl-x) (or (apply cl-pred cl-x) (throw 'cl-every nil)))) cl-seq cl-rest) t) (while (and cl-seq (funcall cl-pred (car cl-seq))) (setq cl-seq (cdr cl-seq))) (null cl-seq))) (defun notany (cl-pred cl-seq &rest cl-rest) "Return true if PREDICATE is false of every element of SEQ or SEQs." (not (apply 'some cl-pred cl-seq cl-rest))) (defun notevery (cl-pred cl-seq &rest cl-rest) "Return true if PREDICATE is false of some element of SEQ or SEQs." (not (apply 'every cl-pred cl-seq cl-rest))) ;;; Support for `loop'. (defun cl-map-keymap (cl-func cl-map) (while (symbolp cl-map) (setq cl-map (symbol-function cl-map))) (if (eq cl-emacs-type 'lucid) (funcall 'map-keymap cl-func cl-map) (if (listp cl-map) (let ((cl-p cl-map)) (while (consp (setq cl-p (cdr cl-p))) (cond ((consp (car cl-p)) (funcall cl-func (car (car cl-p)) (cdr (car cl-p)))) ((vectorp (car cl-p)) (cl-map-keymap cl-func (car cl-p))) ((eq (car cl-p) 'keymap) (setq cl-p nil))))) (let ((cl-i -1)) (while (< (setq cl-i (1+ cl-i)) (length cl-map)) (if (aref cl-map cl-i) (funcall cl-func cl-i (aref cl-map cl-i)))))))) (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) (or cl-base (setq cl-base (copy-sequence (if (eq cl-emacs-type 18) "0" [0])))) (cl-map-keymap (function (lambda (cl-key cl-bind) (aset cl-base (1- (length cl-base)) cl-key) (if (keymapp cl-bind) (cl-map-keymap-recursively cl-func-rec cl-bind (funcall (if (eq cl-emacs-type 18) 'concat 'vconcat) cl-base (list 0))) (funcall cl-func-rec cl-base cl-bind)))) cl-map)) (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) (or cl-what (setq cl-what (current-buffer))) (if (bufferp cl-what) (let (cl-mark cl-mark2 (cl-next t) cl-next2) (save-excursion (set-buffer cl-what) (setq cl-mark (copy-marker (or cl-start (point-min)))) (setq cl-mark2 (and cl-end (copy-marker cl-end)))) (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) (setq cl-next (and (fboundp 'next-property-change) (if cl-prop (next-single-property-change cl-mark cl-prop cl-what) (next-property-change cl-mark cl-what))) cl-next2 (or cl-next (save-excursion (set-buffer cl-what) (point-max)))) (funcall cl-func (prog1 (marker-position cl-mark) (set-marker cl-mark cl-next2)) (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) (or cl-start (setq cl-start 0)) (or cl-end (setq cl-end (length cl-what))) (while (< cl-start cl-end) (let ((cl-next (or (and (fboundp 'next-property-change) (if cl-prop (next-single-property-change cl-start cl-prop cl-what) (next-property-change cl-start cl-what))) cl-end))) (funcall cl-func cl-start (min cl-next cl-end)) (setq cl-start cl-next))))) (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) (or cl-buffer (setq cl-buffer (current-buffer))) (with-fboundp '(overlay-start overlay-end overlays-at next-overlay-change) (if-fboundp 'overlay-lists ;; This is the preferred algorithm, though overlay-lists is ;; undocumented. (let (cl-ovl) (save-excursion (set-buffer cl-buffer) (setq cl-ovl (overlay-lists)) (if cl-start (setq cl-start (copy-marker cl-start))) (if cl-end (setq cl-end (copy-marker cl-end)))) (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) (while (and cl-ovl (or (not (overlay-start (car cl-ovl))) (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) (and cl-start (<= (overlay-end (car cl-ovl)) cl-start)) (not (funcall cl-func (car cl-ovl) cl-arg)))) (setq cl-ovl (cdr cl-ovl))) (if cl-start (set-marker cl-start nil)) (if cl-end (set-marker cl-end nil))) ;; This alternate algorithm fails to find zero-length overlays. (let ((cl-mark (save-excursion (set-buffer cl-buffer) (copy-marker (or cl-start (point-min))))) (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer) (copy-marker cl-end)))) cl-pos cl-ovl) (while (save-excursion (and (setq cl-pos (marker-position cl-mark)) (< cl-pos (or cl-mark2 (point-max))) (progn (set-buffer cl-buffer) (setq cl-ovl (overlays-at cl-pos)) (set-marker cl-mark (next-overlay-change cl-pos))))) (while (and cl-ovl (or (/= (overlay-start (car cl-ovl)) cl-pos) (not (and (funcall cl-func (car cl-ovl) cl-arg) (set-marker cl-mark nil))))) (setq cl-ovl (cdr cl-ovl)))) (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))) ;;; Support for `setf'. (defun cl-set-frame-visible-p (frame val) (cond ((null val) (make-frame-invisible frame)) ((eq val 'icon) (iconify-frame frame)) (t (make-frame-visible frame))) val) ;;; Support for `progv'. (defvar cl-progv-save) (defun cl-progv-before (syms values) (while syms (cl-push (if (boundp (car syms)) (cons (car syms) (symbol-value (car syms))) (car syms)) cl-progv-save) (if values (set (cl-pop syms) (cl-pop values)) (makunbound (cl-pop syms))))) (defun cl-progv-after () (while cl-progv-save (if (consp (car cl-progv-save)) (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) (makunbound (car cl-progv-save))) (cl-pop cl-progv-save))) ;;; Numbers. (defun gcd (&rest args) "Return the greatest common divisor of the arguments." (let ((a (abs (or (cl-pop args) 0)))) (while args (let ((b (abs (cl-pop args)))) (while (> b 0) (setq b (% a (setq a b)))))) a)) (defun lcm (&rest args) "Return the least common multiple of the arguments." (if (memq 0 args) 0 (let ((a (abs (or (cl-pop args) 1)))) (while args (let ((b (abs (cl-pop args)))) (setq a (* (/ a (gcd a b)) b)))) a))) (defun isqrt (a) "Return the integer square root of the argument." (if (and (integerp a) (> a 0)) ;; XEmacs change (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000) ((>= a 100) 100) (t 10))) g2) (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) (setq g g2)) g) (if (eq a 0) 0 (signal 'arith-error nil)))) (defun cl-expt (x y) "Return X raised to the power of Y. Works only for integer arguments." (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0)) (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2))))) (or (and (fboundp 'expt) (subrp (symbol-function 'expt))) (defalias 'expt 'cl-expt)) (defun floor* (x &optional y) "Return a list of the floor of X and the fractional part of X. With two arguments, return floor and remainder of their quotient." (let ((q (floor x y))) (list q (- x (if y (* y q) q))))) (defun ceiling* (x &optional y) "Return a list of the ceiling of X and the fractional part of X. With two arguments, return ceiling and remainder of their quotient." (let ((res (floor* x y))) (if (= (car (cdr res)) 0) res (list (1+ (car res)) (- (car (cdr res)) (or y 1)))))) (defun truncate* (x &optional y) "Return a list of the integer part of X and the fractional part of X. With two arguments, return truncation and remainder of their quotient." (if (eq (>= x 0) (or (null y) (>= y 0))) (floor* x y) (ceiling* x y))) (defun round* (x &optional y) "Return a list of X rounded to the nearest integer and the remainder. With two arguments, return rounding and remainder of their quotient." (if y (if (and (integerp x) (integerp y)) (let* ((hy (/ y 2)) (res (floor* (+ x hy) y))) (if (and (= (car (cdr res)) 0) (= (+ hy hy) y) (/= (% (car res) 2) 0)) (list (1- (car res)) hy) (list (car res) (- (car (cdr res)) hy)))) (let ((q (round (/ x y)))) (list q (- x (* q y))))) (if (integerp x) (list x 0) (let ((q (round x))) (list q (- x q)))))) (defun mod* (x y) "The remainder of X divided by Y, with the same sign as Y." (nth 1 (floor* x y))) (defun rem* (x y) "The remainder of X divided by Y, with the same sign as X." (nth 1 (truncate* x y))) (defun signum (a) "Return 1 if A is positive, -1 if negative, 0 if zero." (cond ((> a 0) 1) ((< a 0) -1) (t 0))) ;; Random numbers. (defvar *random-state*) (defun random* (lim &optional state) "Return a random nonnegative number less than LIM, an integer or float. Optional second arg STATE is a random-state object." (or state (setq state *random-state*)) ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. (let ((vec (aref state 3))) (if (integerp vec) (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1)) (aset state 3 (setq vec (make-vector 55 nil))) (aset vec 0 j) (while (> (setq i (% (+ i 21) 55)) 0) (aset vec i (setq j (prog1 k (setq k (- j k)))))) (while (< (setq i (1+ i)) 200) (random* 2 state)))) (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) (j (aset state 2 (% (1+ (aref state 2)) 55))) (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) (if (integerp lim) (if (<= lim 512) (% n lim) (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) (let ((mask 1023)) (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) (if (< (setq n (logand n mask)) lim) n (random* lim state)))) (* (/ n '8388608e0) lim))))) (defun make-random-state (&optional state) "Return a copy of random-state STATE, or of `*random-state*' if omitted. If STATE is t, return a new state object seeded from the time of day." (cond ((null state) (make-random-state *random-state*)) ((vectorp state) (cl-copy-tree state t)) ((integerp state) (vector 'cl-random-state-tag -1 30 state)) (t (make-random-state (cl-random-time))))) (defun random-state-p (object) "Return t if OBJECT is a random-state object." (and (vectorp object) (= (length object) 4) (eq (aref object 0) 'cl-random-state-tag))) ;; Implementation limits. (defun cl-finite-do (func a b) (condition-case nil (let ((res (funcall func a b))) ; check for IEEE infinity (and (numberp res) (/= res (/ res 2)) res)) (arith-error nil))) (defvar most-positive-float) (defvar most-negative-float) (defvar least-positive-float) (defvar least-negative-float) (defvar least-positive-normalized-float) (defvar least-negative-normalized-float) (defvar float-epsilon) (defvar float-negative-epsilon) (defun cl-float-limits () (or most-positive-float (not (numberp '2e1)) (let ((x '2e0) y z) ;; Find maximum exponent (first two loops are optimizations) (while (cl-finite-do '* x x) (setq x (* x x))) (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) (while (cl-finite-do '+ x x) (setq x (+ x x))) (setq z x y (/ x 2)) ;; Now fill in 1's in the mantissa. (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) (setq x (+ x y) y (/ y 2))) (setq most-positive-float x most-negative-float (- x)) ;; Divide down until mantissa starts rounding. (setq x (/ x z) y (/ 16 z) x (* x y)) (while (condition-case nil (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) (arith-error nil)) (setq x (/ x 2) y (/ y 2))) (setq least-positive-normalized-float y least-negative-normalized-float (- y)) ;; Divide down until value underflows to zero. (setq x (/ 1 z) y x) (while (condition-case nil (> (/ x 2) 0) (arith-error nil)) (setq x (/ x 2))) (setq least-positive-float x least-negative-float (- x)) (setq x '1e0) (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) (setq float-epsilon (* x 2)) (setq x '1e0) (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) (setq float-negative-epsilon (* x 2)))) nil) ;;; Sequence functions. ;XEmacs -- our built-in is more powerful. ;(defun subseq (seq start &optional end) ; "Return the subsequence of SEQ from START to END. ;If END is omitted, it defaults to the length of the sequence. ;If START or END is negative, it counts from the end." ; (if (stringp seq) (substring seq start end) ; (let (len) ; (and end (< end 0) (setq end (+ end (setq len (length seq))))) ; (if (< start 0) (setq start (+ start (or len (setq len (length seq)))))) ; (cond ((listp seq) ; (if (> start 0) (setq seq (nthcdr start seq))) ; (if end ; (let ((res nil)) ; (while (>= (setq end (1- end)) start) ; (cl-push (cl-pop seq) res)) ; (nreverse res)) ; (copy-sequence seq))) ; (t ; (or end (setq end (or len (length seq)))) ; (let ((res (make-vector (max (- end start) 0) nil)) ; (i 0)) ; (while (< start end) ; (aset res i (aref seq start)) ; (setq i (1+ i) start (1+ start))) ; res)))))) (defun concatenate (type &rest seqs) "Concatenate, into a sequence of type TYPE, the argument SEQUENCES." (case type (vector (apply 'vconcat seqs)) (string (apply 'concat seqs)) (list (apply 'append (append seqs '(nil)))) (t (error "Not a sequence type name: %s" type)))) ;;; List functions. (defun revappend (x y) "Equivalent to (append (reverse X) Y)." (nconc (reverse x) y)) (defun nreconc (x y) "Equivalent to (nconc (nreverse X) Y)." (nconc (nreverse x) y)) (defun list-length (x) "Return the length of a list. Return nil if list is circular." (let ((n 0) (fast x) (slow x)) (while (and (cdr fast) (not (and (eq fast slow) (> n 0)))) (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow))) (if fast (if (cdr fast) nil (1+ n)) n))) (defun tailp (sublist list) "Return true if SUBLIST is a tail of LIST." (while (and (consp list) (not (eq sublist list))) (setq list (cdr list))) (if (numberp sublist) (equal sublist list) (eq sublist list))) (defun cl-copy-tree (tree &optional vecp) "Make a copy of TREE. If TREE is a cons cell, this recursively copies both its car and its cdr. Contrast to copy-sequence, which copies only along the cdrs. With second argument VECP, this copies vectors as well as conses." (if (consp tree) (let ((p (setq tree (copy-list tree)))) (while (consp p) (if (or (consp (car p)) (and vecp (vectorp (car p)))) (setcar p (cl-copy-tree (car p) vecp))) (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp))) (cl-pop p))) (if (and vecp (vectorp tree)) (let ((i (length (setq tree (copy-sequence tree))))) (while (>= (setq i (1- i)) 0) (aset tree i (cl-copy-tree (aref tree i) vecp)))))) tree) (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree))) (defalias 'copy-tree 'cl-copy-tree)) ;;; Property lists. ;; XEmacs: our `get' groks DEFAULT. (defalias 'get* 'get) (defalias 'getf 'plist-get) (defun cl-set-getf (plist tag val) (let ((p plist)) (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p)))) (if p (progn (setcar (cdr p) val) plist) (list* tag val plist)))) (defun cl-do-remf (plist tag) (let ((p (cdr plist))) (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p)))) (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t)))) ;;; Hash tables. ;; The `regular' Common Lisp hash-table stuff has been moved into C. ;; Only backward compatibility stuff remains here. (defun make-hashtable (size &optional test) (make-hash-table :test test :size size)) (defun make-weak-hashtable (size &optional test) (make-hash-table :test test :size size :weakness t)) (defun make-key-weak-hashtable (size &optional test) (make-hash-table :test test :size size :weakness 'key)) (defun make-value-weak-hashtable (size &optional test) (make-hash-table :test test :size size :weakness 'value)) (define-obsolete-function-alias 'hashtablep 'hash-table-p) (define-obsolete-function-alias 'hashtable-fullness 'hash-table-count) (define-obsolete-function-alias 'hashtable-test-function 'hash-table-test) (define-obsolete-function-alias 'hashtable-type 'hash-table-type) (define-obsolete-function-alias 'hashtable-size 'hash-table-size) (define-obsolete-function-alias 'copy-hashtable 'copy-hash-table) (make-obsolete 'make-hashtable 'make-hash-table) (make-obsolete 'make-weak-hashtable 'make-hash-table) (make-obsolete 'make-key-weak-hashtable 'make-hash-table) (make-obsolete 'make-value-weak-hashtable 'make-hash-table) (make-obsolete 'hash-table-type 'hash-table-weakness) (when (fboundp 'x-keysym-hash-table) (make-obsolete 'x-keysym-hashtable 'x-keysym-hash-table)) ;; Compatibility stuff for old kludgy cl.el hash table implementation (defvar cl-builtin-gethash (symbol-function 'gethash)) (defvar cl-builtin-remhash (symbol-function 'remhash)) (defvar cl-builtin-clrhash (symbol-function 'clrhash)) (defvar cl-builtin-maphash (symbol-function 'maphash)) (defalias 'cl-gethash 'gethash) (defalias 'cl-puthash 'puthash) (defalias 'cl-remhash 'remhash) (defalias 'cl-clrhash 'clrhash) (defalias 'cl-maphash 'maphash) ;;; Some debugging aids. (defun cl-prettyprint (form) "Insert a pretty-printed rendition of a Lisp FORM in current buffer." (let ((pt (point)) last) (insert "\n" (prin1-to-string form) "\n") (setq last (point)) (goto-char (1+ pt)) (while (search-forward "(quote " last t) (delete-backward-char 7) (insert "'") (forward-sexp) (delete-char 1)) (goto-char (1+ pt)) (cl-do-prettyprint))) (defun cl-do-prettyprint () (skip-chars-forward " ") (if (looking-at "(") (let ((skip (or (looking-at "((") (looking-at "(prog") (looking-at "(unwind-protect ") (looking-at "(function (") (looking-at "(cl-block-wrapper "))) (two (or (looking-at "(defun ") (looking-at "(defmacro "))) (let (or (looking-at "(let\\*? ") (looking-at "(while "))) (set (looking-at "(p?set[qf] "))) (if (or skip let (progn (forward-sexp) (and (>= (current-column) 78) (progn (backward-sexp) t)))) (let ((nl t)) (forward-char 1) (cl-do-prettyprint) (or skip (looking-at ")") (cl-do-prettyprint)) (or (not two) (looking-at ")") (cl-do-prettyprint)) (while (not (looking-at ")")) (if set (setq nl (not nl))) (if nl (insert "\n")) (lisp-indent-line) (cl-do-prettyprint)) (forward-char 1)))) (forward-sexp))) (defvar cl-macroexpand-cmacs nil) (defvar cl-closure-vars nil) (defun cl-macroexpand-all (form &optional env) "Expand all macro calls through a Lisp FORM. This also does some trivial optimizations to make the form prettier." (while (or (not (eq form (setq form (macroexpand form env)))) (and cl-macroexpand-cmacs (not (eq form (setq form (compiler-macroexpand form))))))) (cond ((not (consp form)) form) ((memq (car form) '(let let*)) (if (null (nth 1 form)) (cl-macroexpand-all (cons 'progn (cddr form)) env) (let ((letf nil) (res nil) (lets (cadr form))) (while lets (cl-push (if (consp (car lets)) (let ((exp (cl-macroexpand-all (caar lets) env))) (or (symbolp exp) (setq letf t)) (cons exp (cl-macroexpand-body (cdar lets) env))) (let ((exp (cl-macroexpand-all (car lets) env))) (if (symbolp exp) exp (setq letf t) (list exp nil)))) res) (setq lets (cdr lets))) (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) (nreverse res) (cl-macroexpand-body (cddr form) env))))) ((eq (car form) 'cond) (cons (car form) (mapcar (function (lambda (x) (cl-macroexpand-body x env))) (cdr form)))) ((eq (car form) 'condition-case) (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) (mapcar (function (lambda (x) (cons (car x) (cl-macroexpand-body (cdr x) env)))) (cdddr form)))) ((memq (car form) '(quote function)) (if (eq (car-safe (nth 1 form)) 'lambda) (let ((body (cl-macroexpand-body (cddadr form) env))) (if (and cl-closure-vars (eq (car form) 'function) (cl-expr-contains-any body cl-closure-vars)) (let* ((new (mapcar 'gensym cl-closure-vars)) (sub (pairlis cl-closure-vars new)) (decls nil)) (while (or (stringp (car body)) (eq (car-safe (car body)) 'interactive)) (cl-push (list 'quote (cl-pop body)) decls)) (put (car (last cl-closure-vars)) 'used t) (append (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) (sublis sub (nreverse decls)) (list (list* 'list '(quote apply) (list 'list '(quote quote) (list 'function (list* 'lambda (append new (cadadr form)) (sublis sub body)))) (nconc (mapcar (function (lambda (x) (list 'list '(quote quote) x))) cl-closure-vars) '((quote --cl-rest--))))))) (list (car form) (list* 'lambda (cadadr form) body)))) (let ((found (assq (cadr form) env))) (if (eq (cadr (caddr found)) 'cl-labels-args) (cl-macroexpand-all (cadr (caddr (cadddr found))) env) form)))) ((memq (car form) '(defun defmacro)) (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env))) ((and (eq (car form) 'progn) (not (cddr form))) (cl-macroexpand-all (nth 1 form) env)) ((eq (car form) 'setq) (let* ((args (cl-macroexpand-body (cdr form) env)) (p args)) (while (and p (symbolp (car p))) (setq p (cddr p))) (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args)))) (t (cons (car form) (cl-macroexpand-body (cdr form) env))))) (defun cl-macroexpand-body (body &optional env) (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body)) (defun cl-prettyexpand (form &optional full) (message "Expanding...") (let ((cl-macroexpand-cmacs full) (cl-compiling-file full) (byte-compile-macro-environment nil)) (setq form (cl-macroexpand-all form (and (not full) '((block) (eval-when))))) (message "Formatting...") (prog1 (cl-prettyprint form) (message "")))) (run-hooks 'cl-extra-load-hook) (provide 'cl-extra) ;;; cl-extra.el ends here