Mercurial > hg > xemacs-beta
view lisp/cl.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 | 9c872f33ecbe |
line wrap: on
line source
;;; cl.el --- Common Lisp extensions for XEmacs Lisp ;; Copyright (C) 1993, 1997 Free Software Foundation, Inc. ;; Author: Dave Gillespie <daveg@synaptics.com> ;; Maintainer: XEmacs Development Team ;; Version: 2.02 ;; Keywords: extensions, dumped, lisp ;; 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 the portions of the Common Lisp extensions ;; package which should always be present. ;;; Future notes: ;; Once Emacs 19 becomes standard, many things in this package which are ;; messy for reasons of compatibility can be greatly simplified. For now, ;; I prefer to maintain one unified version. ;;; Change Log: ;; Version 2.02 (30 Jul 93): ;; * Added "cl-compat.el" file, extra compatibility with old package. ;; * Added `lexical-let' and `lexical-let*'. ;; * Added `define-modify-macro', `callf', and `callf2'. ;; * Added `ignore-errors'. ;; * Changed `(setf (nthcdr N PLACE) X)' to work when N is zero. ;; * Merged `*gentemp-counter*' into `*gensym-counter*'. ;; * Extended `subseq' to allow negative START and END like `substring'. ;; * Added `in-ref', `across-ref', `elements of-ref' loop clauses. ;; * Added `concat', `vconcat' loop clauses. ;; * Cleaned up a number of compiler warnings. ;; Version 2.01 (7 Jul 93): ;; * Added support for FSF version of Emacs 19. ;; * Added `add-hook' for Emacs 18 users. ;; * Added `defsubst*' and `symbol-macrolet'. ;; * Added `maplist', `mapc', `mapl', `mapcan', `mapcon'. ;; * Added `map', `concatenate', `reduce', `merge'. ;; * Added `revappend', `nreconc', `tailp', `tree-equal'. ;; * Added `assert', `check-type', `typecase', `typep', and `deftype'. ;; * Added destructuring and `&environment' support to `defmacro*'. ;; * Added destructuring to `loop', and added the following clauses: ;; `elements', `frames', `overlays', `intervals', `buffers', `key-seqs'. ;; * Renamed `delete' to `delete*' and `remove' to `remove*'. ;; * Completed support for all keywords in `remove*', `substitute', etc. ;; * Added `most-positive-float' and company. ;; * Fixed hash tables to work with latest Lucid Emacs. ;; * `proclaim' forms are no longer compile-time-evaluating; use `declaim'. ;; * Syntax for `warn' declarations has changed. ;; * Improved implementation of `random*'. ;; * Moved most sequence functions to a new file, cl-seq.el. ;; * Moved `eval-when' into cl-macs.el. ;; * Moved `pushnew' and `adjoin' to cl.el for most common cases. ;; * Moved `provide' forms down to ends of files. ;; * Changed expansion of `pop' to something that compiles to better code. ;; * Changed so that no patch is required for Emacs 19 byte compiler. ;; * Made more things dependent on `optimize' declarations. ;; * Added a partial implementation of struct print functions. ;; * Miscellaneous minor changes. ;; Version 2.00: ;; * First public release of this package. ;;; Code: (defvar cl-emacs-type (cond ((or (and (fboundp 'epoch::version) (symbol-value 'epoch::version)) (string-lessp emacs-version "19")) 18) ((string-match "XEmacs" emacs-version) 'lucid) (t 19))) (or (fboundp 'defalias) (fset 'defalias 'fset)) (defvar cl-optimize-speed 1) (defvar cl-optimize-safety 1) ;;; Keywords used in this package. ;;; XEmacs - keywords are done in Fintern(). ;;; ;;; (defconst :test ':test) ;;; (defconst :test-not ':test-not) ;;; (defconst :key ':key) ;;; (defconst :start ':start) ;;; (defconst :start1 ':start1) ;;; (defconst :start2 ':start2) ;;; (defconst :end ':end) ;;; (defconst :end1 ':end1) ;;; (defconst :end2 ':end2) ;;; (defconst :count ':count) ;;; (defconst :initial-value ':initial-value) ;;; (defconst :size ':size) ;;; (defconst :from-end ':from-end) ;;; (defconst :rehash-size ':rehash-size) ;;; (defconst :rehash-threshold ':rehash-threshold) ;;; (defconst :allow-other-keys ':allow-other-keys) (defvar custom-print-functions nil "This is a list of functions that format user objects for printing. Each function is called in turn with three arguments: the object, the stream, and the print level (currently ignored). If it is able to print the object it returns true; otherwise it returns nil and the printer proceeds to the next function on the list. This variable is not used at present, but it is defined in hopes that a future Emacs interpreter will be able to use it.") ;;; Predicates. (defun eql (a b) ; See compiler macro in cl-macs.el "Return t if the two args are the same Lisp object. Floating-point numbers of equal value are `eql', but they may not be `eq'." (if (floatp a) (equal a b) (eq a b))) ;;; Generalized variables. These macros are defined here so that they ;;; can safely be used in .emacs files. (defmacro incf (place &optional x) "(incf PLACE [X]): increment PLACE by X (1 by default). PLACE may be a symbol, or any generalized variable allowed by `setf'. The return value is the incremented value of PLACE." (if (symbolp place) (list 'setq place (if x (list '+ place x) (list '1+ place))) ;; XEmacs byte-compiler optimizes (+ FOO 1) to (1+ FOO), so this ;; is OK. (list 'callf '+ place (or x 1)))) (defmacro decf (place &optional x) "(decf PLACE [X]): decrement PLACE by X (1 by default). PLACE may be a symbol, or any generalized variable allowed by `setf'. The return value is the decremented value of PLACE." (if (symbolp place) (list 'setq place (if x (list '- place x) (list '1- place))) (list 'callf '- place (or x 1)))) (defmacro pop (place) "(pop PLACE): remove and return the head of the list stored in PLACE. Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more careful about evaluating each argument only once and in the right order. PLACE may be a symbol, or any generalized variable allowed by `setf'." (if (symbolp place) `(car (prog1 ,place (setq ,place (cdr ,place)))) (cl-do-pop place))) (defmacro push (x place) "(push X PLACE): insert X at the head of the list stored in PLACE. Analogous to (setf PLACE (cons X PLACE)), though more careful about evaluating each argument only once and in the right order. PLACE may be a symbol, or any generalized variable allowed by `setf'." (if (symbolp place) `(setq ,place (cons ,x ,place)) (list 'callf2 'cons x place))) (defmacro pushnew (x place &rest keys) "(pushnew X PLACE): insert X at the head of the list if not already there. Like (push X PLACE), except that the list is unmodified if X is `eql' to an element already on the list. Keywords supported: :test :test-not :key" (if (symbolp place) (list 'setq place (list* 'adjoin x place keys)) (list* 'callf2 'adjoin x place keys))) (defun cl-set-elt (seq n val) (if (listp seq) (setcar (nthcdr n seq) val) (aset seq n val))) (defun cl-set-nthcdr (n list x) (if (<= n 0) x (setcdr (nthcdr (1- n) list) x) list)) (defun cl-set-buffer-substring (start end val) (save-excursion (delete-region start end) (goto-char start) (insert val) val)) (defun cl-set-substring (str start end val) (if end (if (< end 0) (incf end (length str))) (setq end (length str))) (if (< start 0) (incf start str)) (concat (and (> start 0) (substring str 0 start)) val (and (< end (length str)) (substring str end)))) ;;; Control structures. ;; The macros `when' and `unless' are so useful that we want them to ;; ALWAYS be available. So they've been moved from cl.el to eval.c. ;; Note: FSF Emacs moved them to subr.el in FSF 20. (defun cl-map-extents (&rest cl-args) ;; XEmacs: This used to check for overlays first, but that's wrong ;; because of the new compatibility library. *duh* (cond ((fboundp 'map-extents) (apply 'map-extents cl-args)) ((fboundp 'next-overlay-at) (apply 'cl-map-overlays cl-args)))) ;;; Blocks and exits. (defalias 'cl-block-wrapper 'identity) (defalias 'cl-block-throw 'throw) ;;; Multiple values. True multiple values are not supported, or even ;;; simulated. Instead, multiple-value-bind and friends simply expect ;;; the target form to return the values as a list. (defalias 'values 'list) (defalias 'values-list 'identity) (defalias 'multiple-value-list 'identity) (defalias 'multiple-value-call 'apply) ; only works for one arg (defalias 'nth-value 'nth) ;;; Macros. (defvar cl-macro-environment nil) ;; XEmacs: we renamed the internal function to macroexpand-internal ;; to avoid doc-file problems. (defvar cl-old-macroexpand (prog1 (symbol-function 'macroexpand-internal) (defalias 'macroexpand 'cl-macroexpand))) (defun cl-macroexpand (cl-macro &optional cl-env) "Return result of expanding macros at top level of FORM. If FORM is not a macro call, it is returned unchanged. Otherwise, the macro is expanded and the expansion is considered in place of FORM. When a non-macro-call results, it is returned. The second optional arg ENVIRONMENT specifies an environment of macro definitions to shadow the loaded ones for use in file byte-compilation." (let ((cl-macro-environment cl-env)) (while (progn (setq cl-macro (funcall cl-old-macroexpand cl-macro cl-env)) (and (symbolp cl-macro) (cdr (assq (symbol-name cl-macro) cl-env)))) (setq cl-macro (cadr (assq (symbol-name cl-macro) cl-env)))) cl-macro)) ;;; Declarations. (defvar cl-compiling-file nil) (defun cl-compiling-file () (or cl-compiling-file ;; XEmacs change ; (and (boundp 'outbuffer) (bufferp (symbol-value 'outbuffer)) ; (equal (buffer-name (symbol-value 'outbuffer)) ; " *Compiler Output*")) (and (boundp 'byte-compile-outbuffer) (bufferp (symbol-value 'byte-compile-outbuffer)) (equal (buffer-name (symbol-value 'byte-compile-outbuffer)) " *Compiler Output*")) )) (defvar cl-proclaims-deferred nil) (defun proclaim (spec) (if (fboundp 'cl-do-proclaim) (cl-do-proclaim spec t) (push spec cl-proclaims-deferred)) nil) (defmacro declaim (&rest specs) (let ((body (mapcar (function (lambda (x) (list 'proclaim (list 'quote x)))) specs))) (if (cl-compiling-file) (list* 'eval-when '(compile load eval) body) (cons 'progn body)))) ; avoid loading cl-macs.el for eval-when ;;; Symbols. (defun cl-random-time () (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0)) (while (>= (decf i) 0) (setq v (+ (* v 3) (aref time i)))) v)) (defvar *gensym-counter* (* (logand (cl-random-time) 1023) 100)) (defun gensym (&optional arg) "Generate a new uninterned symbol. The name is made by appending a number to PREFIX, default \"G\"." (let ((prefix (if (stringp arg) arg "G")) (num (if (integerp arg) arg (prog1 *gensym-counter* (setq *gensym-counter* (1+ *gensym-counter*)))))) (make-symbol (format "%s%d" prefix num)))) (defun gentemp (&optional arg) "Generate a new interned symbol with a unique name. The name is made by appending a number to PREFIX, default \"G\"." (let ((prefix (if (stringp arg) arg "G")) name) (while (intern-soft (setq name (format "%s%d" prefix *gensym-counter*))) (setq *gensym-counter* (1+ *gensym-counter*))) (intern name))) ;;; Numbers. (defun floatp-safe (object) "Return t if OBJECT is a floating point number." (floatp object)) (defun plusp (number) "Return t if NUMBER is positive." (> number 0)) (defun minusp (number) "Return t if NUMBER is negative." (< number 0)) (defun oddp (integer) "Return t if INTEGER is odd." (eq (logand integer 1) 1)) (defun evenp (integer) "Return t if INTEGER is even." (eq (logand integer 1) 0)) (defun cl-abs (number) "Return the absolute value of NUMBER." (if (>= number 0) number (- number))) (or (fboundp 'abs) (defalias 'abs 'cl-abs)) ; This is built-in to Emacs 19 (defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time))) ;;; We use `eval' in case VALBITS differs from compile-time to load-time. (defconst most-positive-fixnum (eval '(lsh -1 -1)) "The integer closest in value to positive infinity.") (defconst most-negative-fixnum (eval '(- -1 (lsh -1 -1))) "The integer closest in value to negative infinity.") ;;; The following are set by code in cl-extra.el (defconst most-positive-float nil "The float closest in value to positive infinity.") (defconst most-negative-float nil "The float closest in value to negative infinity.") (defconst least-positive-float nil "The positive float closest in value to 0.") (defconst least-negative-float nil "The negative float closest in value to 0.") (defconst least-positive-normalized-float nil) (defconst least-negative-normalized-float nil) (defconst float-epsilon nil) (defconst float-negative-epsilon nil) ;;; Sequence functions. (defalias 'copy-seq 'copy-sequence) (defun mapcar* (cl-func cl-x &rest cl-rest) "Apply FUNCTION to each element of SEQ, and make a list of the results. If there are several SEQs, FUNCTION is called with that many arguments, and mapping stops as soon as the shortest list runs out. With just one SEQ, this is like `mapcar'. With several, it is like the Common Lisp `mapcar' function extended to arbitrary sequence types." (if cl-rest (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest))) (cl-mapcar-many cl-func (cons cl-x cl-rest)) (let ((cl-res nil) (cl-y (car cl-rest))) (while (and cl-x cl-y) (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res)) (nreverse cl-res))) (mapcar cl-func cl-x))) ;;; List functions. ;; These functions are made known to the byte-compiler by cl-macs.el ;; and turned into efficient car and cdr bytecodes. (defalias 'first 'car) (defalias 'rest 'cdr) (defalias 'endp 'null) (defun second (x) "Return the second element of the list LIST." (car (cdr x))) (defun third (x) "Return the third element of the list LIST." (car (cdr (cdr x)))) (defun fourth (x) "Return the fourth element of the list LIST." (nth 3 x)) (defun fifth (x) "Return the fifth element of the list LIST." (nth 4 x)) (defun sixth (x) "Return the sixth element of the list LIST." (nth 5 x)) (defun seventh (x) "Return the seventh element of the list LIST." (nth 6 x)) (defun eighth (x) "Return the eighth element of the list LIST." (nth 7 x)) (defun ninth (x) "Return the ninth element of the list LIST." (nth 8 x)) (defun tenth (x) "Return the tenth element of the list LIST." (nth 9 x)) (defun caar (x) "Return the `car' of the `car' of X." (car (car x))) (defun cadr (x) "Return the `car' of the `cdr' of X." (car (cdr x))) (defun cdar (x) "Return the `cdr' of the `car' of X." (cdr (car x))) (defun cddr (x) "Return the `cdr' of the `cdr' of X." (cdr (cdr x))) (defun caaar (x) "Return the `car' of the `car' of the `car' of X." (car (car (car x)))) (defun caadr (x) "Return the `car' of the `car' of the `cdr' of X." (car (car (cdr x)))) (defun cadar (x) "Return the `car' of the `cdr' of the `car' of X." (car (cdr (car x)))) (defun caddr (x) "Return the `car' of the `cdr' of the `cdr' of X." (car (cdr (cdr x)))) (defun cdaar (x) "Return the `cdr' of the `car' of the `car' of X." (cdr (car (car x)))) (defun cdadr (x) "Return the `cdr' of the `car' of the `cdr' of X." (cdr (car (cdr x)))) (defun cddar (x) "Return the `cdr' of the `cdr' of the `car' of X." (cdr (cdr (car x)))) (defun cdddr (x) "Return the `cdr' of the `cdr' of the `cdr' of X." (cdr (cdr (cdr x)))) (defun caaaar (x) "Return the `car' of the `car' of the `car' of the `car' of X." (car (car (car (car x))))) (defun caaadr (x) "Return the `car' of the `car' of the `car' of the `cdr' of X." (car (car (car (cdr x))))) (defun caadar (x) "Return the `car' of the `car' of the `cdr' of the `car' of X." (car (car (cdr (car x))))) (defun caaddr (x) "Return the `car' of the `car' of the `cdr' of the `cdr' of X." (car (car (cdr (cdr x))))) (defun cadaar (x) "Return the `car' of the `cdr' of the `car' of the `car' of X." (car (cdr (car (car x))))) (defun cadadr (x) "Return the `car' of the `cdr' of the `car' of the `cdr' of X." (car (cdr (car (cdr x))))) (defun caddar (x) "Return the `car' of the `cdr' of the `cdr' of the `car' of X." (car (cdr (cdr (car x))))) (defun cadddr (x) "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X." (car (cdr (cdr (cdr x))))) (defun cdaaar (x) "Return the `cdr' of the `car' of the `car' of the `car' of X." (cdr (car (car (car x))))) (defun cdaadr (x) "Return the `cdr' of the `car' of the `car' of the `cdr' of X." (cdr (car (car (cdr x))))) (defun cdadar (x) "Return the `cdr' of the `car' of the `cdr' of the `car' of X." (cdr (car (cdr (car x))))) (defun cdaddr (x) "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X." (cdr (car (cdr (cdr x))))) (defun cddaar (x) "Return the `cdr' of the `cdr' of the `car' of the `car' of X." (cdr (cdr (car (car x))))) (defun cddadr (x) "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X." (cdr (cdr (car (cdr x))))) (defun cdddar (x) "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X." (cdr (cdr (cdr (car x))))) (defun cddddr (x) "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X." (cdr (cdr (cdr (cdr x))))) ;;; `last' is implemented as a C primitive, as of 1998-11 ;(defun last (x &optional n) ; "Return the last link in the list LIST. ;With optional argument N, return Nth-to-last link (default 1)." ; (if n ; (let ((m 0) (p x)) ; (while (consp p) (incf m) (pop p)) ; (if (<= n 0) p ; (if (< n m) (nthcdr (- m n) x) x))) ; (while (consp (cdr x)) (pop x)) ; x)) ;;; `butlast' is implemented as a C primitive, as of 1998-11 ;;; `nbutlast' is implemented as a C primitive, as of 1998-11 ;(defun butlast (x &optional n) ; "Return a copy of LIST with the last N elements removed." ; (if (and n (<= n 0)) x ; (nbutlast (copy-sequence x) n))) ;(defun nbutlast (x &optional n) ; "Modify LIST to remove the last N elements." ; (let ((m (length x))) ; (or n (setq n 1)) ; (and (< n m) ; (progn ; (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil)) ; x)))) (defun list* (arg &rest rest) ; See compiler macro in cl-macs.el "Return a new list with specified args as elements, cons'd to last arg. Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to `(cons A (cons B (cons C D)))'." (cond ((not rest) arg) ((not (cdr rest)) (cons arg (car rest))) (t (let* ((n (length rest)) (copy (copy-sequence rest)) (last (nthcdr (- n 2) copy))) (setcdr last (car (cdr last))) (cons arg copy))))) (defun ldiff (list sublist) "Return a copy of LIST with the tail SUBLIST removed." (let ((res nil)) (while (and (consp list) (not (eq list sublist))) (push (pop list) res)) (nreverse res))) ;;; `copy-list' is implemented as a C primitive, as of 1998-11 ;(defun copy-list (list) ; "Return a copy of a list, which may be a dotted list. ;The elements of the list are not copied, just the list structure itself." ; (if (consp list) ; (let ((res nil)) ; (while (consp list) (push (pop list) res)) ; (prog1 (nreverse res) (setcdr res list))) ; (car list))) (defun cl-maclisp-member (item list) (while (and list (not (equal item (car list)))) (setq list (cdr list))) list) ;;; Define an Emacs 19-compatible `member' for the benefit of Emacs 18 users. (or (and (fboundp 'member) (subrp (symbol-function 'member))) (defalias 'member 'cl-maclisp-member)) (defalias 'cl-member 'memq) ; for compatibility with old CL package (defalias 'cl-floor 'floor*) (defalias 'cl-ceiling 'ceiling*) (defalias 'cl-truncate 'truncate*) (defalias 'cl-round 'round*) (defalias 'cl-mod 'mod*) (defun adjoin (cl-item cl-list &rest cl-keys) ; See compiler macro in cl-macs "Return ITEM consed onto the front of LIST only if it's not already there. Otherwise, return LIST unmodified. Keywords supported: :test :test-not :key" (cond ((or (equal cl-keys '(:test eq)) (and (null cl-keys) (not (numberp cl-item)))) (if (memq cl-item cl-list) cl-list (cons cl-item cl-list))) ((or (equal cl-keys '(:test equal)) (null cl-keys)) (if (member cl-item cl-list) cl-list (cons cl-item cl-list))) (t (apply 'cl-adjoin cl-item cl-list cl-keys)))) (defun subst (cl-new cl-old cl-tree &rest cl-keys) "Substitute NEW for OLD everywhere in TREE (non-destructively). Return a copy of TREE with all elements `eql' to OLD replaced by NEW. Keywords supported: :test :test-not :key" (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old)))) (apply 'sublis (list (cons cl-old cl-new)) cl-tree cl-keys) (cl-do-subst cl-new cl-old cl-tree))) (defun cl-do-subst (cl-new cl-old cl-tree) (cond ((eq cl-tree cl-old) cl-new) ((consp cl-tree) (let ((a (cl-do-subst cl-new cl-old (car cl-tree))) (d (cl-do-subst cl-new cl-old (cdr cl-tree)))) (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree))) cl-tree (cons a d)))) (t cl-tree))) (defun acons (a b c) "Return a new alist created by adding (KEY . VALUE) to ALIST." (cons (cons a b) c)) (defun pairlis (a b &optional c) (nconc (mapcar* 'cons a b) c)) ;;; Miscellaneous. ;; XEmacs change (define-error 'cl-assertion-failed "Assertion failed") ;;; This is defined in Emacs 19; define it here for Emacs 18 users. (defun cl-add-hook (hook func &optional append) "Add to hook variable HOOK the function FUNC. FUNC is not added if it already appears on the list stored in HOOK." (let ((old (and (boundp hook) (symbol-value hook)))) (and (listp old) (not (eq (car old) 'lambda)) (setq old (list old))) (and (not (member func old)) (set hook (if append (nconc old (list func)) (cons func old)))))) (or (fboundp 'add-hook) (defalias 'add-hook 'cl-add-hook)) ;; XEmacs change ;(load "cl-defs") ;;; Define data for indentation and edebug. (mapcar #'(lambda (entry) (mapcar #'(lambda (func) (put func 'lisp-indent-function (nth 1 entry)) (put func 'lisp-indent-hook (nth 1 entry)) (or (get func 'edebug-form-spec) (put func 'edebug-form-spec (nth 2 entry)))) (car entry))) '(((defun* defmacro*) defun) ((function*) nil (&or symbolp ([&optional 'macro] 'lambda (&rest sexp) &rest form))) ((eval-when) 1 (sexp &rest form)) ((when unless) 1 (&rest form)) ((declare) nil (&rest sexp)) ((the) 1 (sexp &rest form)) ((case ecase typecase etypecase) 1 (form &rest (sexp &rest form))) ((block return-from) 1 (sexp &rest form)) ((return) nil (&optional form)) ((do do*) 2 ((&rest &or symbolp (symbolp &optional form form)) (form &rest form) &rest form)) ((dolist dotimes) 1 ((symbolp form &rest form) &rest form)) ((do-symbols) 1 ((symbolp form &optional form form) &rest form)) ((do-all-symbols) 1 ((symbolp form &optional form) &rest form)) ((psetq setf psetf) nil edebug-setq-form) ((progv) 2 (&rest form)) ((flet labels macrolet) 1 ((&rest (sexp sexp &rest form)) &rest form)) ((symbol-macrolet lexical-let lexical-let*) 1 ((&rest &or symbolp (symbolp form)) &rest form)) ((multiple-value-bind) 2 ((&rest symbolp) &rest form)) ((multiple-value-setq) 1 ((&rest symbolp) &rest form)) ((incf decf remf pop push pushnew shiftf rotatef) nil (&rest form)) ((letf letf*) 1 ((&rest (&rest form)) &rest form)) ((callf destructuring-bind) 2 (sexp form &rest form)) ((callf2) 3 (sexp form form &rest form)) ((loop) defun (&rest &or symbolp form)) ((ignore-errors) 0 (&rest form)))) ;;; This goes here so that cl-macs can find it if it loads right now. (provide 'cl-19) ; usage: (require 'cl-19 "cl") ;;; Things to do after byte-compiler is loaded. ;;; As a side effect, we cause cl-macs to be loaded when compiling, so ;;; that the compiler-macros defined there will be present. (defvar cl-hacked-flag nil) (defun cl-hack-byte-compiler () (if (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form)) (progn (when (not (fboundp 'cl-compile-time-init)) (load "cl-macs" nil t)) (cl-compile-time-init) ; in cl-macs.el (setq cl-hacked-flag t)))) ;;; Try it now in case the compiler has already been loaded. (cl-hack-byte-compiler) ;;; Also make a hook in case compiler is loaded after this file. ;;; The compiler doesn't call any hooks when it loads or runs, but ;;; we can take advantage of the fact that emacs-lisp-mode will be ;;; called when the compiler reads in the file to be compiled. ;;; BUG: If the first compilation is `byte-compile' rather than ;;; `byte-compile-file', we lose. Oh, well. (add-hook 'emacs-lisp-mode-hook 'cl-hack-byte-compiler) ;;; The following ensures that packages which expect the old-style cl.el ;;; will be happy with this one. (provide 'cl) (provide 'mini-cl) ; for Epoch (run-hooks 'cl-load-hook) ;;; cl.el ends here