cl-compat.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; cl-compat.el --- Common Lisp extensions for GNU Emacs Lisp (compatibility)
  2. ;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Gillespie <daveg@synaptics.com>
  4. ;; Version: 2.02
  5. ;; Keywords: extensions
  6. ;; Obsolete-since: 23.3
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file has been obsolete since Emacs 23.3.
  20. ;; These are extensions to Emacs Lisp that provide a degree of
  21. ;; Common Lisp compatibility, beyond what is already built-in
  22. ;; in Emacs Lisp.
  23. ;;
  24. ;; This package was written by Dave Gillespie; it is a complete
  25. ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
  26. ;;
  27. ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
  28. ;;
  29. ;; Bug reports, comments, and suggestions are welcome!
  30. ;; This file contains emulations of internal routines of the older
  31. ;; CL package which users may have called directly from their code.
  32. ;; Use (require 'cl-compat) to get these routines.
  33. ;; See cl.el for Change Log.
  34. ;;; Code:
  35. ;; This used to be:
  36. ;; (or (featurep 'cl) (require 'cl))
  37. ;; which just has the effect of fooling the byte-compiler into not
  38. ;; loading cl when compiling. However, that leads to some bogus
  39. ;; compiler warnings. Loading cl when compiling cannot do any harm,
  40. ;; because for a long time bootstrap-emacs contained 'cl, due to being
  41. ;; dumped from uncompiled files that eval-when-compile'd cl. So every
  42. ;; file was compiled with 'cl loaded.
  43. (require 'cl)
  44. ;;; Keyword routines not supported by new package.
  45. (defmacro defkeyword (x &optional doc)
  46. (list* 'defconst x (list 'quote x) (and doc (list doc))))
  47. (defun keyword-of (sym)
  48. (or (keywordp sym) (keywordp (intern (format ":%s" sym)))))
  49. ;;; Multiple values. Note that the new package uses a different
  50. ;;; convention for multiple values. The following definitions
  51. ;;; emulate the old convention; all function names have been changed
  52. ;;; by capitalizing the first letter: Values, Multiple-value-*,
  53. ;;; to avoid conflict with the new-style definitions in cl-macs.
  54. (defvar *mvalues-values* nil)
  55. (defun Values (&rest val-forms)
  56. (setq *mvalues-values* val-forms)
  57. (car val-forms))
  58. (defun Values-list (val-forms)
  59. (apply 'values val-forms))
  60. (defmacro Multiple-value-list (form)
  61. (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form))
  62. '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*)
  63. (list *mvalues-temp*))))
  64. (defmacro Multiple-value-call (function &rest args)
  65. (declare (indent 1))
  66. (list 'apply function
  67. (cons 'append
  68. (mapcar (function (lambda (x) (list 'Multiple-value-list x)))
  69. args))))
  70. (defmacro Multiple-value-bind (vars form &rest body)
  71. (declare (indent 2))
  72. (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body))
  73. (defmacro Multiple-value-setq (vars form)
  74. (declare (indent 2))
  75. (list 'multiple-value-setq vars (list 'Multiple-value-list form)))
  76. (defmacro Multiple-value-prog1 (form &rest body)
  77. (declare (indent 1))
  78. (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body)))
  79. ;;; Routines for parsing keyword arguments.
  80. (defun build-klist (arglist keys &optional allow-others)
  81. (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist))))
  82. (or allow-others
  83. (let ((bad (set-difference (mapcar 'car res) keys)))
  84. (if bad (error "Bad keywords: %s not in %s" bad keys))))
  85. res))
  86. (defun extract-from-klist (klist key &optional def)
  87. (let ((res (assq key klist))) (if res (cdr res) def)))
  88. (defun keyword-argument-supplied-p (klist key)
  89. (assq key klist))
  90. (defun elt-satisfies-test-p (item elt klist)
  91. (let ((test-not (cdr (assq ':test-not klist)))
  92. (test (cdr (assq ':test klist)))
  93. (key (cdr (assq ':key klist))))
  94. (if key (setq elt (funcall key elt)))
  95. (if test-not (not (funcall test-not item elt))
  96. (funcall (or test 'eql) item elt))))
  97. ;;; Rounding functions with old-style multiple value returns.
  98. (defun cl-floor (a &optional b) (Values-list (floor* a b)))
  99. (defun cl-ceiling (a &optional b) (Values-list (ceiling* a b)))
  100. (defun cl-round (a &optional b) (Values-list (round* a b)))
  101. (defun cl-truncate (a &optional b) (Values-list (truncate* a b)))
  102. (defun safe-idiv (a b)
  103. (let* ((q (/ (abs a) (abs b)))
  104. (s (* (signum a) (signum b))))
  105. (Values q (- a (* s q b)) s)))
  106. ;; Internal routines.
  107. (defun pair-with-newsyms (oldforms)
  108. (let ((newsyms (mapcar (lambda (x) (make-symbol "--cl-var--")) oldforms)))
  109. (Values (mapcar* 'list newsyms oldforms) newsyms)))
  110. (defun zip-lists (evens odds)
  111. (mapcan 'list evens odds))
  112. (defun unzip-lists (list)
  113. (let ((e nil) (o nil))
  114. (while list
  115. (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list)))
  116. (Values (nreverse e) (nreverse o))))
  117. (defun reassemble-argslists (list)
  118. (let ((n (apply 'min (mapcar 'length list))) (res nil))
  119. (while (>= (setq n (1- n)) 0)
  120. (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res)))
  121. res))
  122. (defun duplicate-symbols-p (list)
  123. (let ((res nil))
  124. (while list
  125. (if (memq (car list) (cdr list)) (setq res (cons (car list) res)))
  126. (setq list (cdr list)))
  127. res))
  128. ;;; Setf internals.
  129. (defun setnth (n list x)
  130. (setcar (nthcdr n list) x))
  131. (defun setnthcdr (n list x)
  132. (setcdr (nthcdr (1- n) list) x))
  133. (defun setelt (seq n x)
  134. (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x)))
  135. ;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms,
  136. ;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms,
  137. ;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify,
  138. ;;; all names with embedded `$'.
  139. (provide 'cl-compat)
  140. ;; Local variables:
  141. ;; byte-compile-warnings: (not cl-functions)
  142. ;; End:
  143. ;;; cl-compat.el ends here