cl-macs.el 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. ;;; cl-macs.el --- Common Lisp macros
  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. ;; Package: emacs
  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. ;; These are extensions to Emacs Lisp that provide a degree of
  20. ;; Common Lisp compatibility, beyond what is already built-in
  21. ;; in Emacs Lisp.
  22. ;;
  23. ;; This package was written by Dave Gillespie; it is a complete
  24. ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
  25. ;;
  26. ;; Bug reports, comments, and suggestions are welcome!
  27. ;; This file contains the portions of the Common Lisp extensions
  28. ;; package which should be autoloaded, but need only be present
  29. ;; if the compiler or interpreter is used---this file is not
  30. ;; necessary for executing compiled code.
  31. ;; See cl.el for Change Log.
  32. ;;; Code:
  33. (require 'cl)
  34. (defmacro cl-pop2 (place)
  35. (list 'prog1 (list 'car (list 'cdr place))
  36. (list 'setq place (list 'cdr (list 'cdr place)))))
  37. (put 'cl-pop2 'edebug-form-spec 'edebug-sexps)
  38. (defvar cl-optimize-safety)
  39. (defvar cl-optimize-speed)
  40. ;; This kludge allows macros which use cl-transform-function-property
  41. ;; to be called at compile-time.
  42. (require
  43. (progn
  44. (or (fboundp 'cl-transform-function-property)
  45. (defalias 'cl-transform-function-property
  46. (function (lambda (n p f)
  47. (list 'put (list 'quote n) (list 'quote p)
  48. (list 'function (cons 'lambda f)))))))
  49. (car (or features (setq features (list 'cl-kludge))))))
  50. ;;; Initialization.
  51. (defvar cl-old-bc-file-form nil)
  52. ;;; Some predicates for analyzing Lisp forms. These are used by various
  53. ;;; macro expanders to optimize the results in certain common cases.
  54. (defconst cl-simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max
  55. car-safe cdr-safe progn prog1 prog2))
  56. (defconst cl-safe-funcs '(* / % length memq list vector vectorp
  57. < > <= >= = error))
  58. ;;; Check if no side effects, and executes quickly.
  59. (defun cl-simple-expr-p (x &optional size)
  60. (or size (setq size 10))
  61. (if (and (consp x) (not (memq (car x) '(quote function function*))))
  62. (and (symbolp (car x))
  63. (or (memq (car x) cl-simple-funcs)
  64. (get (car x) 'side-effect-free))
  65. (progn
  66. (setq size (1- size))
  67. (while (and (setq x (cdr x))
  68. (setq size (cl-simple-expr-p (car x) size))))
  69. (and (null x) (>= size 0) size)))
  70. (and (> size 0) (1- size))))
  71. (defun cl-simple-exprs-p (xs)
  72. (while (and xs (cl-simple-expr-p (car xs)))
  73. (setq xs (cdr xs)))
  74. (not xs))
  75. ;;; Check if no side effects.
  76. (defun cl-safe-expr-p (x)
  77. (or (not (and (consp x) (not (memq (car x) '(quote function function*)))))
  78. (and (symbolp (car x))
  79. (or (memq (car x) cl-simple-funcs)
  80. (memq (car x) cl-safe-funcs)
  81. (get (car x) 'side-effect-free))
  82. (progn
  83. (while (and (setq x (cdr x)) (cl-safe-expr-p (car x))))
  84. (null x)))))
  85. ;;; Check if constant (i.e., no side effects or dependencies).
  86. (defun cl-const-expr-p (x)
  87. (cond ((consp x)
  88. (or (eq (car x) 'quote)
  89. (and (memq (car x) '(function function*))
  90. (or (symbolp (nth 1 x))
  91. (and (eq (car-safe (nth 1 x)) 'lambda) 'func)))))
  92. ((symbolp x) (and (memq x '(nil t)) t))
  93. (t t)))
  94. (defun cl-const-exprs-p (xs)
  95. (while (and xs (cl-const-expr-p (car xs)))
  96. (setq xs (cdr xs)))
  97. (not xs))
  98. (defun cl-const-expr-val (x)
  99. (and (eq (cl-const-expr-p x) t) (if (consp x) (nth 1 x) x)))
  100. (defun cl-expr-access-order (x v)
  101. ;; This apparently tries to return nil iff the expression X evaluates
  102. ;; the variables V in the same order as they appear in V (so as to
  103. ;; be able to replace those vars with the expressions they're bound
  104. ;; to).
  105. ;; FIXME: This is very naive, it doesn't even check to see if those
  106. ;; variables appear more than once.
  107. (if (cl-const-expr-p x) v
  108. (if (consp x)
  109. (progn
  110. (while (setq x (cdr x)) (setq v (cl-expr-access-order (car x) v)))
  111. v)
  112. (if (eq x (car v)) (cdr v) '(t)))))
  113. ;;; Count number of times X refers to Y. Return nil for 0 times.
  114. (defun cl-expr-contains (x y)
  115. (cond ((equal y x) 1)
  116. ((and (consp x) (not (memq (car-safe x) '(quote function function*))))
  117. (let ((sum 0))
  118. (while x
  119. (setq sum (+ sum (or (cl-expr-contains (pop x) y) 0))))
  120. (and (> sum 0) sum)))
  121. (t nil)))
  122. (defun cl-expr-contains-any (x y)
  123. (while (and y (not (cl-expr-contains x (car y)))) (pop y))
  124. y)
  125. ;;; Check whether X may depend on any of the symbols in Y.
  126. (defun cl-expr-depends-p (x y)
  127. (and (not (cl-const-expr-p x))
  128. (or (not (cl-safe-expr-p x)) (cl-expr-contains-any x y))))
  129. ;;; Symbols.
  130. (defvar *gensym-counter*)
  131. ;;;###autoload
  132. (defun gensym (&optional prefix)
  133. "Generate a new uninterned symbol.
  134. The name is made by appending a number to PREFIX, default \"G\"."
  135. (let ((pfix (if (stringp prefix) prefix "G"))
  136. (num (if (integerp prefix) prefix
  137. (prog1 *gensym-counter*
  138. (setq *gensym-counter* (1+ *gensym-counter*))))))
  139. (make-symbol (format "%s%d" pfix num))))
  140. ;;;###autoload
  141. (defun gentemp (&optional prefix)
  142. "Generate a new interned symbol with a unique name.
  143. The name is made by appending a number to PREFIX, default \"G\"."
  144. (let ((pfix (if (stringp prefix) prefix "G"))
  145. name)
  146. (while (intern-soft (setq name (format "%s%d" pfix *gensym-counter*)))
  147. (setq *gensym-counter* (1+ *gensym-counter*)))
  148. (intern name)))
  149. ;;; Program structure.
  150. ;;;###autoload
  151. (defmacro defun* (name args &rest body)
  152. "Define NAME as a function.
  153. Like normal `defun', except ARGLIST allows full Common Lisp conventions,
  154. and BODY is implicitly surrounded by (block NAME ...).
  155. \(fn NAME ARGLIST [DOCSTRING] BODY...)"
  156. (let* ((res (cl-transform-lambda (cons args body) name))
  157. (form (list* 'defun name (cdr res))))
  158. (if (car res) (list 'progn (car res) form) form)))
  159. ;;;###autoload
  160. (defmacro defmacro* (name args &rest body)
  161. "Define NAME as a macro.
  162. Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
  163. and BODY is implicitly surrounded by (block NAME ...).
  164. \(fn NAME ARGLIST [DOCSTRING] BODY...)"
  165. (let* ((res (cl-transform-lambda (cons args body) name))
  166. (form (list* 'defmacro name (cdr res))))
  167. (if (car res) (list 'progn (car res) form) form)))
  168. ;;;###autoload
  169. (defmacro function* (func)
  170. "Introduce a function.
  171. Like normal `function', except that if argument is a lambda form,
  172. its argument list allows full Common Lisp conventions."
  173. (if (eq (car-safe func) 'lambda)
  174. (let* ((res (cl-transform-lambda (cdr func) 'cl-none))
  175. (form (list 'function (cons 'lambda (cdr res)))))
  176. (if (car res) (list 'progn (car res) form) form))
  177. (list 'function func)))
  178. (defun cl-transform-function-property (func prop form)
  179. (let ((res (cl-transform-lambda form func)))
  180. (append '(progn) (cdr (cdr (car res)))
  181. (list (list 'put (list 'quote func) (list 'quote prop)
  182. (list 'function (cons 'lambda (cdr res))))))))
  183. (defconst lambda-list-keywords
  184. '(&optional &rest &key &allow-other-keys &aux &whole &body &environment))
  185. (defvar cl-macro-environment nil
  186. "Keep the list of currently active macros.
  187. It is a list of elements of the form either:
  188. - (SYMBOL . FUNCTION) where FUNCTION is the macro expansion function.
  189. - (SYMBOL-NAME . EXPANSION) where SYMBOL-NAME is the name of a symbol macro.")
  190. (defvar bind-block) (defvar bind-defs) (defvar bind-enquote)
  191. (defvar bind-inits) (defvar bind-lets) (defvar bind-forms)
  192. (declare-function help-add-fundoc-usage "help-fns" (docstring arglist))
  193. (defun cl--make-usage-var (x)
  194. "X can be a var or a (destructuring) lambda-list."
  195. (cond
  196. ((symbolp x) (make-symbol (upcase (symbol-name x))))
  197. ((consp x) (cl--make-usage-args x))
  198. (t x)))
  199. (defun cl--make-usage-args (arglist)
  200. ;; `orig-args' can contain &cl-defs (an internal
  201. ;; CL thingy I don't understand), so remove it.
  202. (let ((x (memq '&cl-defs arglist)))
  203. (when x (setq arglist (delq (car x) (remq (cadr x) arglist)))))
  204. (let ((state nil))
  205. (mapcar (lambda (x)
  206. (cond
  207. ((symbolp x)
  208. (if (eq ?\& (aref (symbol-name x) 0))
  209. (setq state x)
  210. (make-symbol (upcase (symbol-name x)))))
  211. ((not (consp x)) x)
  212. ((memq state '(nil &rest)) (cl--make-usage-args x))
  213. (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR).
  214. (list*
  215. (if (and (consp (car x)) (eq state '&key))
  216. (list (caar x) (cl--make-usage-var (nth 1 (car x))))
  217. (cl--make-usage-var (car x)))
  218. (nth 1 x) ;INITFORM.
  219. (cl--make-usage-args (nthcdr 2 x)) ;SVAR.
  220. ))))
  221. arglist)))
  222. (defun cl-transform-lambda (form bind-block)
  223. (let* ((args (car form)) (body (cdr form)) (orig-args args)
  224. (bind-defs nil) (bind-enquote nil)
  225. (bind-inits nil) (bind-lets nil) (bind-forms nil)
  226. (header nil) (simple-args nil))
  227. (while (or (stringp (car body))
  228. (memq (car-safe (car body)) '(interactive declare)))
  229. (push (pop body) header))
  230. (setq args (if (listp args) (copy-list args) (list '&rest args)))
  231. (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
  232. (if (setq bind-defs (cadr (memq '&cl-defs args)))
  233. (setq args (delq '&cl-defs (delq bind-defs args))
  234. bind-defs (cadr bind-defs)))
  235. (if (setq bind-enquote (memq '&cl-quote args))
  236. (setq args (delq '&cl-quote args)))
  237. (if (memq '&whole args) (error "&whole not currently implemented"))
  238. (let* ((p (memq '&environment args)) (v (cadr p)))
  239. (if p (setq args (nconc (delq (car p) (delq v args))
  240. (list '&aux (list v 'cl-macro-environment))))))
  241. (while (and args (symbolp (car args))
  242. (not (memq (car args) '(nil &rest &body &key &aux)))
  243. (not (and (eq (car args) '&optional)
  244. (or bind-defs (consp (cadr args))))))
  245. (push (pop args) simple-args))
  246. (or (eq bind-block 'cl-none)
  247. (setq body (list (list* 'block bind-block body))))
  248. (if (null args)
  249. (list* nil (nreverse simple-args) (nconc (nreverse header) body))
  250. (if (memq '&optional simple-args) (push '&optional args))
  251. (cl-do-arglist args nil (- (length simple-args)
  252. (if (memq '&optional simple-args) 1 0)))
  253. (setq bind-lets (nreverse bind-lets))
  254. (list* (and bind-inits (list* 'eval-when '(compile load eval)
  255. (nreverse bind-inits)))
  256. (nconc (nreverse simple-args)
  257. (list '&rest (car (pop bind-lets))))
  258. (nconc (let ((hdr (nreverse header)))
  259. ;; Macro expansion can take place in the middle of
  260. ;; apparently harmless computation, so it should not
  261. ;; touch the match-data.
  262. (save-match-data
  263. (require 'help-fns)
  264. (cons (help-add-fundoc-usage
  265. (if (stringp (car hdr)) (pop hdr))
  266. (format "%S"
  267. (cons 'fn
  268. (cl--make-usage-args orig-args))))
  269. hdr)))
  270. (list (nconc (list 'let* bind-lets)
  271. (nreverse bind-forms) body)))))))
  272. (defun cl-do-arglist (args expr &optional num) ; uses bind-*
  273. (if (nlistp args)
  274. (if (or (memq args lambda-list-keywords) (not (symbolp args)))
  275. (error "Invalid argument name: %s" args)
  276. (push (list args expr) bind-lets))
  277. (setq args (copy-list args))
  278. (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
  279. (let ((p (memq '&body args))) (if p (setcar p '&rest)))
  280. (if (memq '&environment args) (error "&environment used incorrectly"))
  281. (let ((save-args args)
  282. (restarg (memq '&rest args))
  283. (safety (if (cl-compiling-file) cl-optimize-safety 3))
  284. (keys nil)
  285. (laterarg nil) (exactarg nil) minarg)
  286. (or num (setq num 0))
  287. (if (listp (cadr restarg))
  288. (setq restarg (make-symbol "--cl-rest--"))
  289. (setq restarg (cadr restarg)))
  290. (push (list restarg expr) bind-lets)
  291. (if (eq (car args) '&whole)
  292. (push (list (cl-pop2 args) restarg) bind-lets))
  293. (let ((p args))
  294. (setq minarg restarg)
  295. (while (and p (not (memq (car p) lambda-list-keywords)))
  296. (or (eq p args) (setq minarg (list 'cdr minarg)))
  297. (setq p (cdr p)))
  298. (if (memq (car p) '(nil &aux))
  299. (setq minarg (list '= (list 'length restarg)
  300. (length (ldiff args p)))
  301. exactarg (not (eq args p)))))
  302. (while (and args (not (memq (car args) lambda-list-keywords)))
  303. (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car)
  304. restarg)))
  305. (cl-do-arglist
  306. (pop args)
  307. (if (or laterarg (= safety 0)) poparg
  308. (list 'if minarg poparg
  309. (list 'signal '(quote wrong-number-of-arguments)
  310. (list 'list (and (not (eq bind-block 'cl-none))
  311. (list 'quote bind-block))
  312. (list 'length restarg)))))))
  313. (setq num (1+ num) laterarg t))
  314. (while (and (eq (car args) '&optional) (pop args))
  315. (while (and args (not (memq (car args) lambda-list-keywords)))
  316. (let ((arg (pop args)))
  317. (or (consp arg) (setq arg (list arg)))
  318. (if (cddr arg) (cl-do-arglist (nth 2 arg) (list 'and restarg t)))
  319. (let ((def (if (cdr arg) (nth 1 arg)
  320. (or (car bind-defs)
  321. (nth 1 (assq (car arg) bind-defs)))))
  322. (poparg (list 'pop restarg)))
  323. (and def bind-enquote (setq def (list 'quote def)))
  324. (cl-do-arglist (car arg)
  325. (if def (list 'if restarg poparg def) poparg))
  326. (setq num (1+ num))))))
  327. (if (eq (car args) '&rest)
  328. (let ((arg (cl-pop2 args)))
  329. (if (consp arg) (cl-do-arglist arg restarg)))
  330. (or (eq (car args) '&key) (= safety 0) exactarg
  331. (push (list 'if restarg
  332. (list 'signal '(quote wrong-number-of-arguments)
  333. (list 'list
  334. (and (not (eq bind-block 'cl-none))
  335. (list 'quote bind-block))
  336. (list '+ num (list 'length restarg)))))
  337. bind-forms)))
  338. (while (and (eq (car args) '&key) (pop args))
  339. (while (and args (not (memq (car args) lambda-list-keywords)))
  340. (let ((arg (pop args)))
  341. (or (consp arg) (setq arg (list arg)))
  342. (let* ((karg (if (consp (car arg)) (caar arg)
  343. (intern (format ":%s" (car arg)))))
  344. (varg (if (consp (car arg)) (cadar arg) (car arg)))
  345. (def (if (cdr arg) (cadr arg)
  346. (or (car bind-defs) (cadr (assq varg bind-defs)))))
  347. (look (list 'memq (list 'quote karg) restarg)))
  348. (and def bind-enquote (setq def (list 'quote def)))
  349. (if (cddr arg)
  350. (let* ((temp (or (nth 2 arg) (make-symbol "--cl-var--")))
  351. (val (list 'car (list 'cdr temp))))
  352. (cl-do-arglist temp look)
  353. (cl-do-arglist varg
  354. (list 'if temp
  355. (list 'prog1 val (list 'setq temp t))
  356. def)))
  357. (cl-do-arglist
  358. varg
  359. (list 'car
  360. (list 'cdr
  361. (if (null def)
  362. look
  363. (list 'or look
  364. (if (eq (cl-const-expr-p def) t)
  365. (list
  366. 'quote
  367. (list nil (cl-const-expr-val def)))
  368. (list 'list nil def))))))))
  369. (push karg keys)))))
  370. (setq keys (nreverse keys))
  371. (or (and (eq (car args) '&allow-other-keys) (pop args))
  372. (null keys) (= safety 0)
  373. (let* ((var (make-symbol "--cl-keys--"))
  374. (allow '(:allow-other-keys))
  375. (check (list
  376. 'while var
  377. (list
  378. 'cond
  379. (list (list 'memq (list 'car var)
  380. (list 'quote (append keys allow)))
  381. (list 'setq var (list 'cdr (list 'cdr var))))
  382. (list (list 'car
  383. (list 'cdr
  384. (list 'memq (cons 'quote allow)
  385. restarg)))
  386. (list 'setq var nil))
  387. (list t
  388. (list
  389. 'error
  390. (format "Keyword argument %%s not one of %s"
  391. keys)
  392. (list 'car var)))))))
  393. (push (list 'let (list (list var restarg)) check) bind-forms)))
  394. (while (and (eq (car args) '&aux) (pop args))
  395. (while (and args (not (memq (car args) lambda-list-keywords)))
  396. (if (consp (car args))
  397. (if (and bind-enquote (cadar args))
  398. (cl-do-arglist (caar args)
  399. (list 'quote (cadr (pop args))))
  400. (cl-do-arglist (caar args) (cadr (pop args))))
  401. (cl-do-arglist (pop args) nil))))
  402. (if args (error "Malformed argument list %s" save-args)))))
  403. (defun cl-arglist-args (args)
  404. (if (nlistp args) (list args)
  405. (let ((res nil) (kind nil) arg)
  406. (while (consp args)
  407. (setq arg (pop args))
  408. (if (memq arg lambda-list-keywords) (setq kind arg)
  409. (if (eq arg '&cl-defs) (pop args)
  410. (and (consp arg) kind (setq arg (car arg)))
  411. (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg)))
  412. (setq res (nconc res (cl-arglist-args arg))))))
  413. (nconc res (and args (list args))))))
  414. ;;;###autoload
  415. (defmacro destructuring-bind (args expr &rest body)
  416. (let* ((bind-lets nil) (bind-forms nil) (bind-inits nil)
  417. (bind-defs nil) (bind-block 'cl-none) (bind-enquote nil))
  418. (cl-do-arglist (or args '(&aux)) expr)
  419. (append '(progn) bind-inits
  420. (list (nconc (list 'let* (nreverse bind-lets))
  421. (nreverse bind-forms) body)))))
  422. ;;; The `eval-when' form.
  423. (defvar cl-not-toplevel nil)
  424. ;;;###autoload
  425. (defmacro eval-when (when &rest body)
  426. "Control when BODY is evaluated.
  427. If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
  428. If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
  429. If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
  430. \(fn (WHEN...) BODY...)"
  431. (if (and (fboundp 'cl-compiling-file) (cl-compiling-file)
  432. (not cl-not-toplevel) (not (boundp 'for-effect))) ; horrible kludge
  433. (let ((comp (or (memq 'compile when) (memq :compile-toplevel when)))
  434. (cl-not-toplevel t))
  435. (if (or (memq 'load when) (memq :load-toplevel when))
  436. (if comp (cons 'progn (mapcar 'cl-compile-time-too body))
  437. (list* 'if nil nil body))
  438. (progn (if comp (eval (cons 'progn body))) nil)))
  439. (and (or (memq 'eval when) (memq :execute when))
  440. (cons 'progn body))))
  441. (defun cl-compile-time-too (form)
  442. (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler))
  443. (setq form (macroexpand
  444. form (cons '(eval-when) byte-compile-macro-environment))))
  445. (cond ((eq (car-safe form) 'progn)
  446. (cons 'progn (mapcar 'cl-compile-time-too (cdr form))))
  447. ((eq (car-safe form) 'eval-when)
  448. (let ((when (nth 1 form)))
  449. (if (or (memq 'eval when) (memq :execute when))
  450. (list* 'eval-when (cons 'compile when) (cddr form))
  451. form)))
  452. (t (eval form) form)))
  453. ;;;###autoload
  454. (defmacro load-time-value (form &optional read-only)
  455. "Like `progn', but evaluates the body at load time.
  456. The result of the body appears to the compiler as a quoted constant."
  457. (if (cl-compiling-file)
  458. (let* ((temp (gentemp "--cl-load-time--"))
  459. (set (list 'set (list 'quote temp) form)))
  460. (if (and (fboundp 'byte-compile-file-form-defmumble)
  461. (boundp 'this-kind) (boundp 'that-one))
  462. (fset 'byte-compile-file-form
  463. (list 'lambda '(form)
  464. (list 'fset '(quote byte-compile-file-form)
  465. (list 'quote
  466. (symbol-function 'byte-compile-file-form)))
  467. (list 'byte-compile-file-form (list 'quote set))
  468. '(byte-compile-file-form form)))
  469. (print set (symbol-value 'byte-compile--outbuffer)))
  470. (list 'symbol-value (list 'quote temp)))
  471. (list 'quote (eval form))))
  472. ;;; Conditional control structures.
  473. ;;;###autoload
  474. (defmacro case (expr &rest clauses)
  475. "Eval EXPR and choose among clauses on that value.
  476. Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
  477. against each key in each KEYLIST; the corresponding BODY is evaluated.
  478. If no clause succeeds, case returns nil. A single atom may be used in
  479. place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
  480. allowed only in the final clause, and matches if no other keys match.
  481. Key values are compared by `eql'.
  482. \n(fn EXPR (KEYLIST BODY...)...)"
  483. (let* ((temp (if (cl-simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
  484. (head-list nil)
  485. (body (cons
  486. 'cond
  487. (mapcar
  488. (function
  489. (lambda (c)
  490. (cons (cond ((memq (car c) '(t otherwise)) t)
  491. ((eq (car c) 'ecase-error-flag)
  492. (list 'error "ecase failed: %s, %s"
  493. temp (list 'quote (reverse head-list))))
  494. ((listp (car c))
  495. (setq head-list (append (car c) head-list))
  496. (list 'member* temp (list 'quote (car c))))
  497. (t
  498. (if (memq (car c) head-list)
  499. (error "Duplicate key in case: %s"
  500. (car c)))
  501. (push (car c) head-list)
  502. (list 'eql temp (list 'quote (car c)))))
  503. (or (cdr c) '(nil)))))
  504. clauses))))
  505. (if (eq temp expr) body
  506. (list 'let (list (list temp expr)) body))))
  507. ;;;###autoload
  508. (defmacro ecase (expr &rest clauses)
  509. "Like `case', but error if no case fits.
  510. `otherwise'-clauses are not allowed.
  511. \n(fn EXPR (KEYLIST BODY...)...)"
  512. (list* 'case expr (append clauses '((ecase-error-flag)))))
  513. ;;;###autoload
  514. (defmacro typecase (expr &rest clauses)
  515. "Evals EXPR, chooses among clauses on that value.
  516. Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
  517. satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
  518. typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
  519. final clause, and matches if no other keys match.
  520. \n(fn EXPR (TYPE BODY...)...)"
  521. (let* ((temp (if (cl-simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
  522. (type-list nil)
  523. (body (cons
  524. 'cond
  525. (mapcar
  526. (function
  527. (lambda (c)
  528. (cons (cond ((eq (car c) 'otherwise) t)
  529. ((eq (car c) 'ecase-error-flag)
  530. (list 'error "etypecase failed: %s, %s"
  531. temp (list 'quote (reverse type-list))))
  532. (t
  533. (push (car c) type-list)
  534. (cl-make-type-test temp (car c))))
  535. (or (cdr c) '(nil)))))
  536. clauses))))
  537. (if (eq temp expr) body
  538. (list 'let (list (list temp expr)) body))))
  539. ;;;###autoload
  540. (defmacro etypecase (expr &rest clauses)
  541. "Like `typecase', but error if no case fits.
  542. `otherwise'-clauses are not allowed.
  543. \n(fn EXPR (TYPE BODY...)...)"
  544. (list* 'typecase expr (append clauses '((ecase-error-flag)))))
  545. ;;; Blocks and exits.
  546. ;;;###autoload
  547. (defmacro block (name &rest body)
  548. "Define a lexically-scoped block named NAME.
  549. NAME may be any symbol. Code inside the BODY forms can call `return-from'
  550. to jump prematurely out of the block. This differs from `catch' and `throw'
  551. in two respects: First, the NAME is an unevaluated symbol rather than a
  552. quoted symbol or other form; and second, NAME is lexically rather than
  553. dynamically scoped: Only references to it within BODY will work. These
  554. references may appear inside macro expansions, but not inside functions
  555. called from BODY."
  556. (if (cl-safe-expr-p (cons 'progn body)) (cons 'progn body)
  557. (list 'cl-block-wrapper
  558. (list* 'catch (list 'quote (intern (format "--cl-block-%s--" name)))
  559. body))))
  560. ;;;###autoload
  561. (defmacro return (&optional result)
  562. "Return from the block named nil.
  563. This is equivalent to `(return-from nil RESULT)'."
  564. (list 'return-from nil result))
  565. ;;;###autoload
  566. (defmacro return-from (name &optional result)
  567. "Return from the block named NAME.
  568. This jumps out to the innermost enclosing `(block NAME ...)' form,
  569. returning RESULT from that form (or nil if RESULT is omitted).
  570. This is compatible with Common Lisp, but note that `defun' and
  571. `defmacro' do not create implicit blocks as they do in Common Lisp."
  572. (let ((name2 (intern (format "--cl-block-%s--" name))))
  573. (list 'cl-block-throw (list 'quote name2) result)))
  574. ;;; The "loop" macro.
  575. (defvar loop-args) (defvar loop-accum-var) (defvar loop-accum-vars)
  576. (defvar loop-bindings) (defvar loop-body) (defvar loop-destr-temps)
  577. (defvar loop-finally) (defvar loop-finish-flag) (defvar loop-first-flag)
  578. (defvar loop-initially) (defvar loop-map-form) (defvar loop-name)
  579. (defvar loop-result) (defvar loop-result-explicit)
  580. (defvar loop-result-var) (defvar loop-steps) (defvar loop-symbol-macs)
  581. ;;;###autoload
  582. (defmacro loop (&rest loop-args)
  583. "The Common Lisp `loop' macro.
  584. Valid clauses are:
  585. for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM,
  586. for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR,
  587. for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND,
  588. always COND, never COND, thereis COND, collect EXPR into VAR,
  589. append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR,
  590. count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR,
  591. if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
  592. unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
  593. do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR,
  594. finally return EXPR, named NAME.
  595. \(fn CLAUSE...)"
  596. (if (not (memq t (mapcar 'symbolp (delq nil (delq t (copy-list loop-args))))))
  597. (list 'block nil (list* 'while t loop-args))
  598. (let ((loop-name nil) (loop-bindings nil)
  599. (loop-body nil) (loop-steps nil)
  600. (loop-result nil) (loop-result-explicit nil)
  601. (loop-result-var nil) (loop-finish-flag nil)
  602. (loop-accum-var nil) (loop-accum-vars nil)
  603. (loop-initially nil) (loop-finally nil)
  604. (loop-map-form nil) (loop-first-flag nil)
  605. (loop-destr-temps nil) (loop-symbol-macs nil))
  606. (setq loop-args (append loop-args '(cl-end-loop)))
  607. (while (not (eq (car loop-args) 'cl-end-loop)) (cl-parse-loop-clause))
  608. (if loop-finish-flag
  609. (push `((,loop-finish-flag t)) loop-bindings))
  610. (if loop-first-flag
  611. (progn (push `((,loop-first-flag t)) loop-bindings)
  612. (push `(setq ,loop-first-flag nil) loop-steps)))
  613. (let* ((epilogue (nconc (nreverse loop-finally)
  614. (list (or loop-result-explicit loop-result))))
  615. (ands (cl-loop-build-ands (nreverse loop-body)))
  616. (while-body (nconc (cadr ands) (nreverse loop-steps)))
  617. (body (append
  618. (nreverse loop-initially)
  619. (list (if loop-map-form
  620. (list 'block '--cl-finish--
  621. (subst
  622. (if (eq (car ands) t) while-body
  623. (cons `(or ,(car ands)
  624. (return-from --cl-finish--
  625. nil))
  626. while-body))
  627. '--cl-map loop-map-form))
  628. (list* 'while (car ands) while-body)))
  629. (if loop-finish-flag
  630. (if (equal epilogue '(nil)) (list loop-result-var)
  631. `((if ,loop-finish-flag
  632. (progn ,@epilogue) ,loop-result-var)))
  633. epilogue))))
  634. (if loop-result-var (push (list loop-result-var) loop-bindings))
  635. (while loop-bindings
  636. (if (cdar loop-bindings)
  637. (setq body (list (cl-loop-let (pop loop-bindings) body t)))
  638. (let ((lets nil))
  639. (while (and loop-bindings
  640. (not (cdar loop-bindings)))
  641. (push (car (pop loop-bindings)) lets))
  642. (setq body (list (cl-loop-let lets body nil))))))
  643. (if loop-symbol-macs
  644. (setq body (list (list* 'symbol-macrolet loop-symbol-macs body))))
  645. (list* 'block loop-name body)))))
  646. (defun cl-parse-loop-clause () ; uses loop-*
  647. (let ((word (pop loop-args))
  648. (hash-types '(hash-key hash-keys hash-value hash-values))
  649. (key-types '(key-code key-codes key-seq key-seqs
  650. key-binding key-bindings)))
  651. (cond
  652. ((null loop-args)
  653. (error "Malformed `loop' macro"))
  654. ((eq word 'named)
  655. (setq loop-name (pop loop-args)))
  656. ((eq word 'initially)
  657. (if (memq (car loop-args) '(do doing)) (pop loop-args))
  658. (or (consp (car loop-args)) (error "Syntax error on `initially' clause"))
  659. (while (consp (car loop-args))
  660. (push (pop loop-args) loop-initially)))
  661. ((eq word 'finally)
  662. (if (eq (car loop-args) 'return)
  663. (setq loop-result-explicit (or (cl-pop2 loop-args) '(quote nil)))
  664. (if (memq (car loop-args) '(do doing)) (pop loop-args))
  665. (or (consp (car loop-args)) (error "Syntax error on `finally' clause"))
  666. (if (and (eq (caar loop-args) 'return) (null loop-name))
  667. (setq loop-result-explicit (or (nth 1 (pop loop-args)) '(quote nil)))
  668. (while (consp (car loop-args))
  669. (push (pop loop-args) loop-finally)))))
  670. ((memq word '(for as))
  671. (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
  672. (ands nil))
  673. (while
  674. ;; Use `gensym' rather than `make-symbol'. It's important that
  675. ;; (not (eq (symbol-name var1) (symbol-name var2))) because
  676. ;; these vars get added to the cl-macro-environment.
  677. (let ((var (or (pop loop-args) (gensym "--cl-var--"))))
  678. (setq word (pop loop-args))
  679. (if (eq word 'being) (setq word (pop loop-args)))
  680. (if (memq word '(the each)) (setq word (pop loop-args)))
  681. (if (memq word '(buffer buffers))
  682. (setq word 'in loop-args (cons '(buffer-list) loop-args)))
  683. (cond
  684. ((memq word '(from downfrom upfrom to downto upto
  685. above below by))
  686. (push word loop-args)
  687. (if (memq (car loop-args) '(downto above))
  688. (error "Must specify `from' value for downward loop"))
  689. (let* ((down (or (eq (car loop-args) 'downfrom)
  690. (memq (caddr loop-args) '(downto above))))
  691. (excl (or (memq (car loop-args) '(above below))
  692. (memq (caddr loop-args) '(above below))))
  693. (start (and (memq (car loop-args) '(from upfrom downfrom))
  694. (cl-pop2 loop-args)))
  695. (end (and (memq (car loop-args)
  696. '(to upto downto above below))
  697. (cl-pop2 loop-args)))
  698. (step (and (eq (car loop-args) 'by) (cl-pop2 loop-args)))
  699. (end-var (and (not (cl-const-expr-p end))
  700. (make-symbol "--cl-var--")))
  701. (step-var (and (not (cl-const-expr-p step))
  702. (make-symbol "--cl-var--"))))
  703. (and step (numberp step) (<= step 0)
  704. (error "Loop `by' value is not positive: %s" step))
  705. (push (list var (or start 0)) loop-for-bindings)
  706. (if end-var (push (list end-var end) loop-for-bindings))
  707. (if step-var (push (list step-var step)
  708. loop-for-bindings))
  709. (if end
  710. (push (list
  711. (if down (if excl '> '>=) (if excl '< '<=))
  712. var (or end-var end)) loop-body))
  713. (push (list var (list (if down '- '+) var
  714. (or step-var step 1)))
  715. loop-for-steps)))
  716. ((memq word '(in in-ref on))
  717. (let* ((on (eq word 'on))
  718. (temp (if (and on (symbolp var))
  719. var (make-symbol "--cl-var--"))))
  720. (push (list temp (pop loop-args)) loop-for-bindings)
  721. (push (list 'consp temp) loop-body)
  722. (if (eq word 'in-ref)
  723. (push (list var (list 'car temp)) loop-symbol-macs)
  724. (or (eq temp var)
  725. (progn
  726. (push (list var nil) loop-for-bindings)
  727. (push (list var (if on temp (list 'car temp)))
  728. loop-for-sets))))
  729. (push (list temp
  730. (if (eq (car loop-args) 'by)
  731. (let ((step (cl-pop2 loop-args)))
  732. (if (and (memq (car-safe step)
  733. '(quote function
  734. function*))
  735. (symbolp (nth 1 step)))
  736. (list (nth 1 step) temp)
  737. (list 'funcall step temp)))
  738. (list 'cdr temp)))
  739. loop-for-steps)))
  740. ((eq word '=)
  741. (let* ((start (pop loop-args))
  742. (then (if (eq (car loop-args) 'then) (cl-pop2 loop-args) start)))
  743. (push (list var nil) loop-for-bindings)
  744. (if (or ands (eq (car loop-args) 'and))
  745. (progn
  746. (push `(,var
  747. (if ,(or loop-first-flag
  748. (setq loop-first-flag
  749. (make-symbol "--cl-var--")))
  750. ,start ,var))
  751. loop-for-sets)
  752. (push (list var then) loop-for-steps))
  753. (push (list var
  754. (if (eq start then) start
  755. `(if ,(or loop-first-flag
  756. (setq loop-first-flag
  757. (make-symbol "--cl-var--")))
  758. ,start ,then)))
  759. loop-for-sets))))
  760. ((memq word '(across across-ref))
  761. (let ((temp-vec (make-symbol "--cl-vec--"))
  762. (temp-idx (make-symbol "--cl-idx--")))
  763. (push (list temp-vec (pop loop-args)) loop-for-bindings)
  764. (push (list temp-idx -1) loop-for-bindings)
  765. (push (list '< (list 'setq temp-idx (list '1+ temp-idx))
  766. (list 'length temp-vec)) loop-body)
  767. (if (eq word 'across-ref)
  768. (push (list var (list 'aref temp-vec temp-idx))
  769. loop-symbol-macs)
  770. (push (list var nil) loop-for-bindings)
  771. (push (list var (list 'aref temp-vec temp-idx))
  772. loop-for-sets))))
  773. ((memq word '(element elements))
  774. (let ((ref (or (memq (car loop-args) '(in-ref of-ref))
  775. (and (not (memq (car loop-args) '(in of)))
  776. (error "Expected `of'"))))
  777. (seq (cl-pop2 loop-args))
  778. (temp-seq (make-symbol "--cl-seq--"))
  779. (temp-idx (if (eq (car loop-args) 'using)
  780. (if (and (= (length (cadr loop-args)) 2)
  781. (eq (caadr loop-args) 'index))
  782. (cadr (cl-pop2 loop-args))
  783. (error "Bad `using' clause"))
  784. (make-symbol "--cl-idx--"))))
  785. (push (list temp-seq seq) loop-for-bindings)
  786. (push (list temp-idx 0) loop-for-bindings)
  787. (if ref
  788. (let ((temp-len (make-symbol "--cl-len--")))
  789. (push (list temp-len (list 'length temp-seq))
  790. loop-for-bindings)
  791. (push (list var (list 'elt temp-seq temp-idx))
  792. loop-symbol-macs)
  793. (push (list '< temp-idx temp-len) loop-body))
  794. (push (list var nil) loop-for-bindings)
  795. (push (list 'and temp-seq
  796. (list 'or (list 'consp temp-seq)
  797. (list '< temp-idx
  798. (list 'length temp-seq))))
  799. loop-body)
  800. (push (list var (list 'if (list 'consp temp-seq)
  801. (list 'pop temp-seq)
  802. (list 'aref temp-seq temp-idx)))
  803. loop-for-sets))
  804. (push (list temp-idx (list '1+ temp-idx))
  805. loop-for-steps)))
  806. ((memq word hash-types)
  807. (or (memq (car loop-args) '(in of)) (error "Expected `of'"))
  808. (let* ((table (cl-pop2 loop-args))
  809. (other (if (eq (car loop-args) 'using)
  810. (if (and (= (length (cadr loop-args)) 2)
  811. (memq (caadr loop-args) hash-types)
  812. (not (eq (caadr loop-args) word)))
  813. (cadr (cl-pop2 loop-args))
  814. (error "Bad `using' clause"))
  815. (make-symbol "--cl-var--"))))
  816. (if (memq word '(hash-value hash-values))
  817. (setq var (prog1 other (setq other var))))
  818. (setq loop-map-form
  819. `(maphash (lambda (,var ,other) . --cl-map) ,table))))
  820. ((memq word '(symbol present-symbol external-symbol
  821. symbols present-symbols external-symbols))
  822. (let ((ob (and (memq (car loop-args) '(in of)) (cl-pop2 loop-args))))
  823. (setq loop-map-form
  824. `(mapatoms (lambda (,var) . --cl-map) ,ob))))
  825. ((memq word '(overlay overlays extent extents))
  826. (let ((buf nil) (from nil) (to nil))
  827. (while (memq (car loop-args) '(in of from to))
  828. (cond ((eq (car loop-args) 'from) (setq from (cl-pop2 loop-args)))
  829. ((eq (car loop-args) 'to) (setq to (cl-pop2 loop-args)))
  830. (t (setq buf (cl-pop2 loop-args)))))
  831. (setq loop-map-form
  832. `(cl-map-extents
  833. (lambda (,var ,(make-symbol "--cl-var--"))
  834. (progn . --cl-map) nil)
  835. ,buf ,from ,to))))
  836. ((memq word '(interval intervals))
  837. (let ((buf nil) (prop nil) (from nil) (to nil)
  838. (var1 (make-symbol "--cl-var1--"))
  839. (var2 (make-symbol "--cl-var2--")))
  840. (while (memq (car loop-args) '(in of property from to))
  841. (cond ((eq (car loop-args) 'from) (setq from (cl-pop2 loop-args)))
  842. ((eq (car loop-args) 'to) (setq to (cl-pop2 loop-args)))
  843. ((eq (car loop-args) 'property)
  844. (setq prop (cl-pop2 loop-args)))
  845. (t (setq buf (cl-pop2 loop-args)))))
  846. (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
  847. (setq var1 (car var) var2 (cdr var))
  848. (push (list var (list 'cons var1 var2)) loop-for-sets))
  849. (setq loop-map-form
  850. `(cl-map-intervals
  851. (lambda (,var1 ,var2) . --cl-map)
  852. ,buf ,prop ,from ,to))))
  853. ((memq word key-types)
  854. (or (memq (car loop-args) '(in of)) (error "Expected `of'"))
  855. (let ((map (cl-pop2 loop-args))
  856. (other (if (eq (car loop-args) 'using)
  857. (if (and (= (length (cadr loop-args)) 2)
  858. (memq (caadr loop-args) key-types)
  859. (not (eq (caadr loop-args) word)))
  860. (cadr (cl-pop2 loop-args))
  861. (error "Bad `using' clause"))
  862. (make-symbol "--cl-var--"))))
  863. (if (memq word '(key-binding key-bindings))
  864. (setq var (prog1 other (setq other var))))
  865. (setq loop-map-form
  866. `(,(if (memq word '(key-seq key-seqs))
  867. 'cl-map-keymap-recursively 'map-keymap)
  868. (lambda (,var ,other) . --cl-map) ,map))))
  869. ((memq word '(frame frames screen screens))
  870. (let ((temp (make-symbol "--cl-var--")))
  871. (push (list var '(selected-frame))
  872. loop-for-bindings)
  873. (push (list temp nil) loop-for-bindings)
  874. (push (list 'prog1 (list 'not (list 'eq var temp))
  875. (list 'or temp (list 'setq temp var)))
  876. loop-body)
  877. (push (list var (list 'next-frame var))
  878. loop-for-steps)))
  879. ((memq word '(window windows))
  880. (let ((scr (and (memq (car loop-args) '(in of)) (cl-pop2 loop-args)))
  881. (temp (make-symbol "--cl-var--"))
  882. (minip (make-symbol "--cl-minip--")))
  883. (push (list var (if scr
  884. (list 'frame-selected-window scr)
  885. '(selected-window)))
  886. loop-for-bindings)
  887. ;; If we started in the minibuffer, we need to
  888. ;; ensure that next-window will bring us back there
  889. ;; at some point. (Bug#7492).
  890. ;; (Consider using walk-windows instead of loop if
  891. ;; you care about such things.)
  892. (push (list minip `(minibufferp (window-buffer ,var)))
  893. loop-for-bindings)
  894. (push (list temp nil) loop-for-bindings)
  895. (push (list 'prog1 (list 'not (list 'eq var temp))
  896. (list 'or temp (list 'setq temp var)))
  897. loop-body)
  898. (push (list var (list 'next-window var minip))
  899. loop-for-steps)))
  900. (t
  901. (let ((handler (and (symbolp word)
  902. (get word 'cl-loop-for-handler))))
  903. (if handler
  904. (funcall handler var)
  905. (error "Expected a `for' preposition, found %s" word)))))
  906. (eq (car loop-args) 'and))
  907. (setq ands t)
  908. (pop loop-args))
  909. (if (and ands loop-for-bindings)
  910. (push (nreverse loop-for-bindings) loop-bindings)
  911. (setq loop-bindings (nconc (mapcar 'list loop-for-bindings)
  912. loop-bindings)))
  913. (if loop-for-sets
  914. (push (list 'progn
  915. (cl-loop-let (nreverse loop-for-sets) 'setq ands)
  916. t) loop-body))
  917. (if loop-for-steps
  918. (push (cons (if ands 'psetq 'setq)
  919. (apply 'append (nreverse loop-for-steps)))
  920. loop-steps))))
  921. ((eq word 'repeat)
  922. (let ((temp (make-symbol "--cl-var--")))
  923. (push (list (list temp (pop loop-args))) loop-bindings)
  924. (push (list '>= (list 'setq temp (list '1- temp)) 0) loop-body)))
  925. ((memq word '(collect collecting))
  926. (let ((what (pop loop-args))
  927. (var (cl-loop-handle-accum nil 'nreverse)))
  928. (if (eq var loop-accum-var)
  929. (push (list 'progn (list 'push what var) t) loop-body)
  930. (push (list 'progn
  931. (list 'setq var (list 'nconc var (list 'list what)))
  932. t) loop-body))))
  933. ((memq word '(nconc nconcing append appending))
  934. (let ((what (pop loop-args))
  935. (var (cl-loop-handle-accum nil 'nreverse)))
  936. (push (list 'progn
  937. (list 'setq var
  938. (if (eq var loop-accum-var)
  939. (list 'nconc
  940. (list (if (memq word '(nconc nconcing))
  941. 'nreverse 'reverse)
  942. what)
  943. var)
  944. (list (if (memq word '(nconc nconcing))
  945. 'nconc 'append)
  946. var what))) t) loop-body)))
  947. ((memq word '(concat concating))
  948. (let ((what (pop loop-args))
  949. (var (cl-loop-handle-accum "")))
  950. (push (list 'progn (list 'callf 'concat var what) t) loop-body)))
  951. ((memq word '(vconcat vconcating))
  952. (let ((what (pop loop-args))
  953. (var (cl-loop-handle-accum [])))
  954. (push (list 'progn (list 'callf 'vconcat var what) t) loop-body)))
  955. ((memq word '(sum summing))
  956. (let ((what (pop loop-args))
  957. (var (cl-loop-handle-accum 0)))
  958. (push (list 'progn (list 'incf var what) t) loop-body)))
  959. ((memq word '(count counting))
  960. (let ((what (pop loop-args))
  961. (var (cl-loop-handle-accum 0)))
  962. (push (list 'progn (list 'if what (list 'incf var)) t) loop-body)))
  963. ((memq word '(minimize minimizing maximize maximizing))
  964. (let* ((what (pop loop-args))
  965. (temp (if (cl-simple-expr-p what) what (make-symbol "--cl-var--")))
  966. (var (cl-loop-handle-accum nil))
  967. (func (intern (substring (symbol-name word) 0 3)))
  968. (set (list 'setq var (list 'if var (list func var temp) temp))))
  969. (push (list 'progn (if (eq temp what) set
  970. (list 'let (list (list temp what)) set))
  971. t) loop-body)))
  972. ((eq word 'with)
  973. (let ((bindings nil))
  974. (while (progn (push (list (pop loop-args)
  975. (and (eq (car loop-args) '=) (cl-pop2 loop-args)))
  976. bindings)
  977. (eq (car loop-args) 'and))
  978. (pop loop-args))
  979. (push (nreverse bindings) loop-bindings)))
  980. ((eq word 'while)
  981. (push (pop loop-args) loop-body))
  982. ((eq word 'until)
  983. (push (list 'not (pop loop-args)) loop-body))
  984. ((eq word 'always)
  985. (or loop-finish-flag (setq loop-finish-flag (make-symbol "--cl-flag--")))
  986. (push (list 'setq loop-finish-flag (pop loop-args)) loop-body)
  987. (setq loop-result t))
  988. ((eq word 'never)
  989. (or loop-finish-flag (setq loop-finish-flag (make-symbol "--cl-flag--")))
  990. (push (list 'setq loop-finish-flag (list 'not (pop loop-args)))
  991. loop-body)
  992. (setq loop-result t))
  993. ((eq word 'thereis)
  994. (or loop-finish-flag (setq loop-finish-flag (make-symbol "--cl-flag--")))
  995. (or loop-result-var (setq loop-result-var (make-symbol "--cl-var--")))
  996. (push (list 'setq loop-finish-flag
  997. (list 'not (list 'setq loop-result-var (pop loop-args))))
  998. loop-body))
  999. ((memq word '(if when unless))
  1000. (let* ((cond (pop loop-args))
  1001. (then (let ((loop-body nil))
  1002. (cl-parse-loop-clause)
  1003. (cl-loop-build-ands (nreverse loop-body))))
  1004. (else (let ((loop-body nil))
  1005. (if (eq (car loop-args) 'else)
  1006. (progn (pop loop-args) (cl-parse-loop-clause)))
  1007. (cl-loop-build-ands (nreverse loop-body))))
  1008. (simple (and (eq (car then) t) (eq (car else) t))))
  1009. (if (eq (car loop-args) 'end) (pop loop-args))
  1010. (if (eq word 'unless) (setq then (prog1 else (setq else then))))
  1011. (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then))
  1012. (if simple (nth 1 else) (list (nth 2 else))))))
  1013. (if (cl-expr-contains form 'it)
  1014. (let ((temp (make-symbol "--cl-var--")))
  1015. (push (list temp) loop-bindings)
  1016. (setq form (list* 'if (list 'setq temp cond)
  1017. (subst temp 'it form))))
  1018. (setq form (list* 'if cond form)))
  1019. (push (if simple (list 'progn form t) form) loop-body))))
  1020. ((memq word '(do doing))
  1021. (let ((body nil))
  1022. (or (consp (car loop-args)) (error "Syntax error on `do' clause"))
  1023. (while (consp (car loop-args)) (push (pop loop-args) body))
  1024. (push (cons 'progn (nreverse (cons t body))) loop-body)))
  1025. ((eq word 'return)
  1026. (or loop-finish-flag (setq loop-finish-flag (make-symbol "--cl-var--")))
  1027. (or loop-result-var (setq loop-result-var (make-symbol "--cl-var--")))
  1028. (push (list 'setq loop-result-var (pop loop-args)
  1029. loop-finish-flag nil) loop-body))
  1030. (t
  1031. (let ((handler (and (symbolp word) (get word 'cl-loop-handler))))
  1032. (or handler (error "Expected a loop keyword, found %s" word))
  1033. (funcall handler))))
  1034. (if (eq (car loop-args) 'and)
  1035. (progn (pop loop-args) (cl-parse-loop-clause)))))
  1036. (defun cl-loop-let (specs body par) ; uses loop-*
  1037. (let ((p specs) (temps nil) (new nil))
  1038. (while (and p (or (symbolp (car-safe (car p))) (null (cadar p))))
  1039. (setq p (cdr p)))
  1040. (and par p
  1041. (progn
  1042. (setq par nil p specs)
  1043. (while p
  1044. (or (cl-const-expr-p (cadar p))
  1045. (let ((temp (make-symbol "--cl-var--")))
  1046. (push (list temp (cadar p)) temps)
  1047. (setcar (cdar p) temp)))
  1048. (setq p (cdr p)))))
  1049. (while specs
  1050. (if (and (consp (car specs)) (listp (caar specs)))
  1051. (let* ((spec (caar specs)) (nspecs nil)
  1052. (expr (cadr (pop specs)))
  1053. (temp (cdr (or (assq spec loop-destr-temps)
  1054. (car (push (cons spec (or (last spec 0)
  1055. (make-symbol "--cl-var--")))
  1056. loop-destr-temps))))))
  1057. (push (list temp expr) new)
  1058. (while (consp spec)
  1059. (push (list (pop spec)
  1060. (and expr (list (if spec 'pop 'car) temp)))
  1061. nspecs))
  1062. (setq specs (nconc (nreverse nspecs) specs)))
  1063. (push (pop specs) new)))
  1064. (if (eq body 'setq)
  1065. (let ((set (cons (if par 'psetq 'setq) (apply 'nconc (nreverse new)))))
  1066. (if temps (list 'let* (nreverse temps) set) set))
  1067. (list* (if par 'let 'let*)
  1068. (nconc (nreverse temps) (nreverse new)) body))))
  1069. (defun cl-loop-handle-accum (def &optional func) ; uses loop-*
  1070. (if (eq (car loop-args) 'into)
  1071. (let ((var (cl-pop2 loop-args)))
  1072. (or (memq var loop-accum-vars)
  1073. (progn (push (list (list var def)) loop-bindings)
  1074. (push var loop-accum-vars)))
  1075. var)
  1076. (or loop-accum-var
  1077. (progn
  1078. (push (list (list (setq loop-accum-var (make-symbol "--cl-var--")) def))
  1079. loop-bindings)
  1080. (setq loop-result (if func (list func loop-accum-var)
  1081. loop-accum-var))
  1082. loop-accum-var))))
  1083. (defun cl-loop-build-ands (clauses)
  1084. (let ((ands nil)
  1085. (body nil))
  1086. (while clauses
  1087. (if (and (eq (car-safe (car clauses)) 'progn)
  1088. (eq (car (last (car clauses))) t))
  1089. (if (cdr clauses)
  1090. (setq clauses (cons (nconc (butlast (car clauses))
  1091. (if (eq (car-safe (cadr clauses))
  1092. 'progn)
  1093. (cdadr clauses)
  1094. (list (cadr clauses))))
  1095. (cddr clauses)))
  1096. (setq body (cdr (butlast (pop clauses)))))
  1097. (push (pop clauses) ands)))
  1098. (setq ands (or (nreverse ands) (list t)))
  1099. (list (if (cdr ands) (cons 'and ands) (car ands))
  1100. body
  1101. (let ((full (if body
  1102. (append ands (list (cons 'progn (append body '(t)))))
  1103. ands)))
  1104. (if (cdr full) (cons 'and full) (car full))))))
  1105. ;;; Other iteration control structures.
  1106. ;;;###autoload
  1107. (defmacro do (steps endtest &rest body)
  1108. "The Common Lisp `do' loop.
  1109. \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
  1110. (cl-expand-do-loop steps endtest body nil))
  1111. ;;;###autoload
  1112. (defmacro do* (steps endtest &rest body)
  1113. "The Common Lisp `do*' loop.
  1114. \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
  1115. (cl-expand-do-loop steps endtest body t))
  1116. (defun cl-expand-do-loop (steps endtest body star)
  1117. (list 'block nil
  1118. (list* (if star 'let* 'let)
  1119. (mapcar (function (lambda (c)
  1120. (if (consp c) (list (car c) (nth 1 c)) c)))
  1121. steps)
  1122. (list* 'while (list 'not (car endtest))
  1123. (append body
  1124. (let ((sets (mapcar
  1125. (function
  1126. (lambda (c)
  1127. (and (consp c) (cdr (cdr c))
  1128. (list (car c) (nth 2 c)))))
  1129. steps)))
  1130. (setq sets (delq nil sets))
  1131. (and sets
  1132. (list (cons (if (or star (not (cdr sets)))
  1133. 'setq 'psetq)
  1134. (apply 'append sets)))))))
  1135. (or (cdr endtest) '(nil)))))
  1136. ;;;###autoload
  1137. (defmacro dolist (spec &rest body)
  1138. "Loop over a list.
  1139. Evaluate BODY with VAR bound to each `car' from LIST, in turn.
  1140. Then evaluate RESULT to get return value, default nil.
  1141. An implicit nil block is established around the loop.
  1142. \(fn (VAR LIST [RESULT]) BODY...)"
  1143. (let ((temp (make-symbol "--cl-dolist-temp--")))
  1144. ;; FIXME: Copy&pasted from subr.el.
  1145. `(block nil
  1146. ;; This is not a reliable test, but it does not matter because both
  1147. ;; semantics are acceptable, tho one is slightly faster with dynamic
  1148. ;; scoping and the other is slightly faster (and has cleaner semantics)
  1149. ;; with lexical scoping.
  1150. ,(if lexical-binding
  1151. `(let ((,temp ,(nth 1 spec)))
  1152. (while ,temp
  1153. (let ((,(car spec) (car ,temp)))
  1154. ,@body
  1155. (setq ,temp (cdr ,temp))))
  1156. ,@(if (cdr (cdr spec))
  1157. ;; FIXME: This let often leads to "unused var" warnings.
  1158. `((let ((,(car spec) nil)) ,@(cdr (cdr spec))))))
  1159. `(let ((,temp ,(nth 1 spec))
  1160. ,(car spec))
  1161. (while ,temp
  1162. (setq ,(car spec) (car ,temp))
  1163. ,@body
  1164. (setq ,temp (cdr ,temp)))
  1165. ,@(if (cdr (cdr spec))
  1166. `((setq ,(car spec) nil) ,@(cddr spec))))))))
  1167. ;;;###autoload
  1168. (defmacro dotimes (spec &rest body)
  1169. "Loop a certain number of times.
  1170. Evaluate BODY with VAR bound to successive integers from 0, inclusive,
  1171. to COUNT, exclusive. Then evaluate RESULT to get return value, default
  1172. nil.
  1173. \(fn (VAR COUNT [RESULT]) BODY...)"
  1174. (let ((temp (make-symbol "--cl-dotimes-temp--"))
  1175. (end (nth 1 spec)))
  1176. ;; FIXME: Copy&pasted from subr.el.
  1177. `(block nil
  1178. ;; This is not a reliable test, but it does not matter because both
  1179. ;; semantics are acceptable, tho one is slightly faster with dynamic
  1180. ;; scoping and the other has cleaner semantics.
  1181. ,(if lexical-binding
  1182. (let ((counter '--dotimes-counter--))
  1183. `(let ((,temp ,end)
  1184. (,counter 0))
  1185. (while (< ,counter ,temp)
  1186. (let ((,(car spec) ,counter))
  1187. ,@body)
  1188. (setq ,counter (1+ ,counter)))
  1189. ,@(if (cddr spec)
  1190. ;; FIXME: This let often leads to "unused var" warnings.
  1191. `((let ((,(car spec) ,counter)) ,@(cddr spec))))))
  1192. `(let ((,temp ,end)
  1193. (,(car spec) 0))
  1194. (while (< ,(car spec) ,temp)
  1195. ,@body
  1196. (incf ,(car spec)))
  1197. ,@(cdr (cdr spec)))))))
  1198. ;;;###autoload
  1199. (defmacro do-symbols (spec &rest body)
  1200. "Loop over all symbols.
  1201. Evaluate BODY with VAR bound to each interned symbol, or to each symbol
  1202. from OBARRAY.
  1203. \(fn (VAR [OBARRAY [RESULT]]) BODY...)"
  1204. ;; Apparently this doesn't have an implicit block.
  1205. (list 'block nil
  1206. (list 'let (list (car spec))
  1207. (list* 'mapatoms
  1208. (list 'function (list* 'lambda (list (car spec)) body))
  1209. (and (cadr spec) (list (cadr spec))))
  1210. (caddr spec))))
  1211. ;;;###autoload
  1212. (defmacro do-all-symbols (spec &rest body)
  1213. (list* 'do-symbols (list (car spec) nil (cadr spec)) body))
  1214. ;;; Assignments.
  1215. ;;;###autoload
  1216. (defmacro psetq (&rest args)
  1217. "Set SYMs to the values VALs in parallel.
  1218. This is like `setq', except that all VAL forms are evaluated (in order)
  1219. before assigning any symbols SYM to the corresponding values.
  1220. \(fn SYM VAL SYM VAL ...)"
  1221. (cons 'psetf args))
  1222. ;;; Binding control structures.
  1223. ;;;###autoload
  1224. (defmacro progv (symbols values &rest body)
  1225. "Bind SYMBOLS to VALUES dynamically in BODY.
  1226. The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
  1227. Each symbol in the first list is bound to the corresponding value in the
  1228. second list (or made unbound if VALUES is shorter than SYMBOLS); then the
  1229. BODY forms are executed and their result is returned. This is much like
  1230. a `let' form, except that the list of symbols can be computed at run-time."
  1231. (list 'let '((cl-progv-save nil))
  1232. (list 'unwind-protect
  1233. (list* 'progn (list 'cl-progv-before symbols values) body)
  1234. '(cl-progv-after))))
  1235. ;;; This should really have some way to shadow 'byte-compile properties, etc.
  1236. ;;;###autoload
  1237. (defmacro flet (bindings &rest body)
  1238. "Make temporary function definitions.
  1239. This is an analogue of `let' that operates on the function cell of FUNC
  1240. rather than its value cell. The FORMs are evaluated with the specified
  1241. function definitions in place, then the definitions are undone (the FUNCs
  1242. go back to their previous definitions, or lack thereof).
  1243. \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
  1244. (list* 'letf*
  1245. (mapcar
  1246. (function
  1247. (lambda (x)
  1248. (if (or (and (fboundp (car x))
  1249. (eq (car-safe (symbol-function (car x))) 'macro))
  1250. (cdr (assq (car x) cl-macro-environment)))
  1251. (error "Use `labels', not `flet', to rebind macro names"))
  1252. (let ((func (list 'function*
  1253. (list 'lambda (cadr x)
  1254. (list* 'block (car x) (cddr x))))))
  1255. (when (cl-compiling-file)
  1256. ;; Bug#411. It would be nice to fix this.
  1257. (and (get (car x) 'byte-compile)
  1258. (error "Byte-compiling a redefinition of `%s' \
  1259. will not work - use `labels' instead" (symbol-name (car x))))
  1260. ;; FIXME This affects the rest of the file, when it
  1261. ;; should be restricted to the flet body.
  1262. (and (boundp 'byte-compile-function-environment)
  1263. (push (cons (car x) (eval func))
  1264. byte-compile-function-environment)))
  1265. (list (list 'symbol-function (list 'quote (car x))) func))))
  1266. bindings)
  1267. body))
  1268. ;;;###autoload
  1269. (defmacro labels (bindings &rest body)
  1270. "Make temporary function bindings.
  1271. This is like `flet', except the bindings are lexical instead of dynamic.
  1272. Unlike `flet', this macro is fully compliant with the Common Lisp standard.
  1273. \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
  1274. (let ((vars nil) (sets nil) (cl-macro-environment cl-macro-environment))
  1275. (while bindings
  1276. ;; Use `gensym' rather than `make-symbol'. It's important that
  1277. ;; (not (eq (symbol-name var1) (symbol-name var2))) because these
  1278. ;; vars get added to the cl-macro-environment.
  1279. (let ((var (gensym "--cl-var--")))
  1280. (push var vars)
  1281. (push (list 'function* (cons 'lambda (cdar bindings))) sets)
  1282. (push var sets)
  1283. (push (list (car (pop bindings)) 'lambda '(&rest cl-labels-args)
  1284. (list 'list* '(quote funcall) (list 'quote var)
  1285. 'cl-labels-args))
  1286. cl-macro-environment)))
  1287. (cl-macroexpand-all (list* 'lexical-let vars (cons (cons 'setq sets) body))
  1288. cl-macro-environment)))
  1289. ;; The following ought to have a better definition for use with newer
  1290. ;; byte compilers.
  1291. ;;;###autoload
  1292. (defmacro macrolet (bindings &rest body)
  1293. "Make temporary macro definitions.
  1294. This is like `flet', but for macros instead of functions.
  1295. \(fn ((NAME ARGLIST BODY...) ...) FORM...)"
  1296. (if (cdr bindings)
  1297. (list 'macrolet
  1298. (list (car bindings)) (list* 'macrolet (cdr bindings) body))
  1299. (if (null bindings) (cons 'progn body)
  1300. (let* ((name (caar bindings))
  1301. (res (cl-transform-lambda (cdar bindings) name)))
  1302. (eval (car res))
  1303. (cl-macroexpand-all (cons 'progn body)
  1304. (cons (list* name 'lambda (cdr res))
  1305. cl-macro-environment))))))
  1306. ;;;###autoload
  1307. (defmacro symbol-macrolet (bindings &rest body)
  1308. "Make symbol macro definitions.
  1309. Within the body FORMs, references to the variable NAME will be replaced
  1310. by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...).
  1311. \(fn ((NAME EXPANSION) ...) FORM...)"
  1312. (if (cdr bindings)
  1313. (list 'symbol-macrolet
  1314. (list (car bindings)) (list* 'symbol-macrolet (cdr bindings) body))
  1315. (if (null bindings) (cons 'progn body)
  1316. (cl-macroexpand-all (cons 'progn body)
  1317. (cons (list (symbol-name (caar bindings))
  1318. (cadar bindings))
  1319. cl-macro-environment)))))
  1320. (defvar cl-closure-vars nil)
  1321. ;;;###autoload
  1322. (defmacro lexical-let (bindings &rest body)
  1323. "Like `let', but lexically scoped.
  1324. The main visible difference is that lambdas inside BODY will create
  1325. lexical closures as in Common Lisp.
  1326. \n(fn BINDINGS BODY)"
  1327. (let* ((cl-closure-vars cl-closure-vars)
  1328. (vars (mapcar (function
  1329. (lambda (x)
  1330. (or (consp x) (setq x (list x)))
  1331. (push (make-symbol (format "--cl-%s--" (car x)))
  1332. cl-closure-vars)
  1333. (set (car cl-closure-vars) [bad-lexical-ref])
  1334. (list (car x) (cadr x) (car cl-closure-vars))))
  1335. bindings))
  1336. (ebody
  1337. (cl-macroexpand-all
  1338. (cons 'progn body)
  1339. (nconc (mapcar (function (lambda (x)
  1340. (list (symbol-name (car x))
  1341. (list 'symbol-value (caddr x))
  1342. t))) vars)
  1343. (list '(defun . cl-defun-expander))
  1344. cl-macro-environment))))
  1345. (if (not (get (car (last cl-closure-vars)) 'used))
  1346. (list 'let (mapcar (function (lambda (x)
  1347. (list (caddr x) (cadr x)))) vars)
  1348. (sublis (mapcar (function (lambda (x)
  1349. (cons (caddr x)
  1350. (list 'quote (caddr x)))))
  1351. vars)
  1352. ebody))
  1353. (list 'let (mapcar (function (lambda (x)
  1354. (list (caddr x)
  1355. (list 'make-symbol
  1356. (format "--%s--" (car x))))))
  1357. vars)
  1358. (apply 'append '(setf)
  1359. (mapcar (function
  1360. (lambda (x)
  1361. (list (list 'symbol-value (caddr x)) (cadr x))))
  1362. vars))
  1363. ebody))))
  1364. ;;;###autoload
  1365. (defmacro lexical-let* (bindings &rest body)
  1366. "Like `let*', but lexically scoped.
  1367. The main visible difference is that lambdas inside BODY, and in
  1368. successive bindings within BINDINGS, will create lexical closures
  1369. as in Common Lisp. This is similar to the behavior of `let*' in
  1370. Common Lisp.
  1371. \n(fn BINDINGS BODY)"
  1372. (if (null bindings) (cons 'progn body)
  1373. (setq bindings (reverse bindings))
  1374. (while bindings
  1375. (setq body (list (list* 'lexical-let (list (pop bindings)) body))))
  1376. (car body)))
  1377. (defun cl-defun-expander (func &rest rest)
  1378. (list 'progn
  1379. (list 'defalias (list 'quote func)
  1380. (list 'function (cons 'lambda rest)))
  1381. (list 'quote func)))
  1382. ;;; Multiple values.
  1383. ;;;###autoload
  1384. (defmacro multiple-value-bind (vars form &rest body)
  1385. "Collect multiple return values.
  1386. FORM must return a list; the BODY is then executed with the first N elements
  1387. of this list bound (`let'-style) to each of the symbols SYM in turn. This
  1388. is analogous to the Common Lisp `multiple-value-bind' macro, using lists to
  1389. simulate true multiple return values. For compatibility, (values A B C) is
  1390. a synonym for (list A B C).
  1391. \(fn (SYM...) FORM BODY)"
  1392. (let ((temp (make-symbol "--cl-var--")) (n -1))
  1393. (list* 'let* (cons (list temp form)
  1394. (mapcar (function
  1395. (lambda (v)
  1396. (list v (list 'nth (setq n (1+ n)) temp))))
  1397. vars))
  1398. body)))
  1399. ;;;###autoload
  1400. (defmacro multiple-value-setq (vars form)
  1401. "Collect multiple return values.
  1402. FORM must return a list; the first N elements of this list are stored in
  1403. each of the symbols SYM in turn. This is analogous to the Common Lisp
  1404. `multiple-value-setq' macro, using lists to simulate true multiple return
  1405. values. For compatibility, (values A B C) is a synonym for (list A B C).
  1406. \(fn (SYM...) FORM)"
  1407. (cond ((null vars) (list 'progn form nil))
  1408. ((null (cdr vars)) (list 'setq (car vars) (list 'car form)))
  1409. (t
  1410. (let* ((temp (make-symbol "--cl-var--")) (n 0))
  1411. (list 'let (list (list temp form))
  1412. (list 'prog1 (list 'setq (pop vars) (list 'car temp))
  1413. (cons 'setq (apply 'nconc
  1414. (mapcar (function
  1415. (lambda (v)
  1416. (list v (list
  1417. 'nth
  1418. (setq n (1+ n))
  1419. temp))))
  1420. vars)))))))))
  1421. ;;; Declarations.
  1422. ;;;###autoload
  1423. (defmacro locally (&rest body) (cons 'progn body))
  1424. ;;;###autoload
  1425. (defmacro the (type form) form)
  1426. (defvar cl-proclaim-history t) ; for future compilers
  1427. (defvar cl-declare-stack t) ; for future compilers
  1428. (defun cl-do-proclaim (spec hist)
  1429. (and hist (listp cl-proclaim-history) (push spec cl-proclaim-history))
  1430. (cond ((eq (car-safe spec) 'special)
  1431. (if (boundp 'byte-compile-bound-variables)
  1432. (setq byte-compile-bound-variables
  1433. (append (cdr spec) byte-compile-bound-variables))))
  1434. ((eq (car-safe spec) 'inline)
  1435. (while (setq spec (cdr spec))
  1436. (or (memq (get (car spec) 'byte-optimizer)
  1437. '(nil byte-compile-inline-expand))
  1438. (error "%s already has a byte-optimizer, can't make it inline"
  1439. (car spec)))
  1440. (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
  1441. ((eq (car-safe spec) 'notinline)
  1442. (while (setq spec (cdr spec))
  1443. (if (eq (get (car spec) 'byte-optimizer)
  1444. 'byte-compile-inline-expand)
  1445. (put (car spec) 'byte-optimizer nil))))
  1446. ((eq (car-safe spec) 'optimize)
  1447. (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
  1448. '((0 nil) (1 t) (2 t) (3 t))))
  1449. (safety (assq (nth 1 (assq 'safety (cdr spec)))
  1450. '((0 t) (1 t) (2 t) (3 nil)))))
  1451. (if speed (setq cl-optimize-speed (car speed)
  1452. byte-optimize (nth 1 speed)))
  1453. (if safety (setq cl-optimize-safety (car safety)
  1454. byte-compile-delete-errors (nth 1 safety)))))
  1455. ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings))
  1456. (while (setq spec (cdr spec))
  1457. (if (consp (car spec))
  1458. (if (eq (cadar spec) 0)
  1459. (byte-compile-disable-warning (caar spec))
  1460. (byte-compile-enable-warning (caar spec)))))))
  1461. nil)
  1462. ;;; Process any proclamations made before cl-macs was loaded.
  1463. (defvar cl-proclaims-deferred)
  1464. (let ((p (reverse cl-proclaims-deferred)))
  1465. (while p (cl-do-proclaim (pop p) t))
  1466. (setq cl-proclaims-deferred nil))
  1467. ;;;###autoload
  1468. (defmacro declare (&rest specs)
  1469. "Declare SPECS about the current function while compiling.
  1470. For instance
  1471. \(declare (warn 0))
  1472. will turn off byte-compile warnings in the function.
  1473. See Info node `(cl)Declarations' for details."
  1474. (if (cl-compiling-file)
  1475. (while specs
  1476. (if (listp cl-declare-stack) (push (car specs) cl-declare-stack))
  1477. (cl-do-proclaim (pop specs) nil)))
  1478. nil)
  1479. ;;; Generalized variables.
  1480. ;;;###autoload
  1481. (defmacro define-setf-method (func args &rest body)
  1482. "Define a `setf' method.
  1483. This method shows how to handle `setf's to places of the form (NAME ARGS...).
  1484. The argument forms ARGS are bound according to ARGLIST, as if NAME were
  1485. going to be expanded as a macro, then the BODY forms are executed and must
  1486. return a list of five elements: a temporary-variables list, a value-forms
  1487. list, a store-variables list (of length one), a store-form, and an access-
  1488. form. See `defsetf' for a simpler way to define most setf-methods.
  1489. \(fn NAME ARGLIST BODY...)"
  1490. (append '(eval-when (compile load eval))
  1491. (if (stringp (car body))
  1492. (list (list 'put (list 'quote func) '(quote setf-documentation)
  1493. (pop body))))
  1494. (list (cl-transform-function-property
  1495. func 'setf-method (cons args body)))))
  1496. (defalias 'define-setf-expander 'define-setf-method)
  1497. ;;;###autoload
  1498. (defmacro defsetf (func arg1 &rest args)
  1499. "Define a `setf' method.
  1500. This macro is an easy-to-use substitute for `define-setf-method' that works
  1501. well for simple place forms. In the simple `defsetf' form, `setf's of
  1502. the form (setf (NAME ARGS...) VAL) are transformed to function or macro
  1503. calls of the form (FUNC ARGS... VAL). Example:
  1504. (defsetf aref aset)
  1505. Alternate form: (defsetf NAME ARGLIST (STORE) BODY...).
  1506. Here, the above `setf' call is expanded by binding the argument forms ARGS
  1507. according to ARGLIST, binding the value form VAL to STORE, then executing
  1508. BODY, which must return a Lisp form that does the necessary `setf' operation.
  1509. Actually, ARGLIST and STORE may be bound to temporary variables which are
  1510. introduced automatically to preserve proper execution order of the arguments.
  1511. Example:
  1512. (defsetf nth (n x) (v) (list 'setcar (list 'nthcdr n x) v))
  1513. \(fn NAME [FUNC | ARGLIST (STORE) BODY...])"
  1514. (if (and (listp arg1) (consp args))
  1515. (let* ((largs nil) (largsr nil)
  1516. (temps nil) (tempsr nil)
  1517. (restarg nil) (rest-temps nil)
  1518. (store-var (car (prog1 (car args) (setq args (cdr args)))))
  1519. (store-temp (intern (format "--%s--temp--" store-var)))
  1520. (lets1 nil) (lets2 nil)
  1521. (docstr nil) (p arg1))
  1522. (if (stringp (car args))
  1523. (setq docstr (prog1 (car args) (setq args (cdr args)))))
  1524. (while (and p (not (eq (car p) '&aux)))
  1525. (if (eq (car p) '&rest)
  1526. (setq p (cdr p) restarg (car p))
  1527. (or (memq (car p) '(&optional &key &allow-other-keys))
  1528. (setq largs (cons (if (consp (car p)) (car (car p)) (car p))
  1529. largs)
  1530. temps (cons (intern (format "--%s--temp--" (car largs)))
  1531. temps))))
  1532. (setq p (cdr p)))
  1533. (setq largs (nreverse largs) temps (nreverse temps))
  1534. (if restarg
  1535. (setq largsr (append largs (list restarg))
  1536. rest-temps (intern (format "--%s--temp--" restarg))
  1537. tempsr (append temps (list rest-temps)))
  1538. (setq largsr largs tempsr temps))
  1539. (let ((p1 largs) (p2 temps))
  1540. (while p1
  1541. (setq lets1 (cons `(,(car p2)
  1542. (make-symbol ,(format "--cl-%s--" (car p1))))
  1543. lets1)
  1544. lets2 (cons (list (car p1) (car p2)) lets2)
  1545. p1 (cdr p1) p2 (cdr p2))))
  1546. (if restarg (setq lets2 (cons (list restarg rest-temps) lets2)))
  1547. `(define-setf-method ,func ,arg1
  1548. ,@(and docstr (list docstr))
  1549. (let*
  1550. ,(nreverse
  1551. (cons `(,store-temp
  1552. (make-symbol ,(format "--cl-%s--" store-var)))
  1553. (if restarg
  1554. `((,rest-temps
  1555. (mapcar (lambda (_) (make-symbol "--cl-var--"))
  1556. ,restarg))
  1557. ,@lets1)
  1558. lets1)))
  1559. (list ; 'values
  1560. (,(if restarg 'list* 'list) ,@tempsr)
  1561. (,(if restarg 'list* 'list) ,@largsr)
  1562. (list ,store-temp)
  1563. (let*
  1564. ,(nreverse
  1565. (cons (list store-var store-temp)
  1566. lets2))
  1567. ,@args)
  1568. (,(if restarg 'list* 'list)
  1569. ,@(cons (list 'quote func) tempsr))))))
  1570. `(defsetf ,func (&rest args) (store)
  1571. ,(let ((call `(cons ',arg1
  1572. (append args (list store)))))
  1573. (if (car args)
  1574. `(list 'progn ,call store)
  1575. call)))))
  1576. ;;; Some standard place types from Common Lisp.
  1577. (defsetf aref aset)
  1578. (defsetf car setcar)
  1579. (defsetf cdr setcdr)
  1580. (defsetf caar (x) (val) (list 'setcar (list 'car x) val))
  1581. (defsetf cadr (x) (val) (list 'setcar (list 'cdr x) val))
  1582. (defsetf cdar (x) (val) (list 'setcdr (list 'car x) val))
  1583. (defsetf cddr (x) (val) (list 'setcdr (list 'cdr x) val))
  1584. (defsetf elt (seq n) (store)
  1585. (list 'if (list 'listp seq) (list 'setcar (list 'nthcdr n seq) store)
  1586. (list 'aset seq n store)))
  1587. (defsetf get put)
  1588. (defsetf get* (x y &optional d) (store) (list 'put x y store))
  1589. (defsetf gethash (x h &optional d) (store) (list 'puthash x store h))
  1590. (defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store))
  1591. (defsetf subseq (seq start &optional end) (new)
  1592. (list 'progn (list 'replace seq new :start1 start :end1 end) new))
  1593. (defsetf symbol-function fset)
  1594. (defsetf symbol-plist setplist)
  1595. (defsetf symbol-value set)
  1596. ;;; Various car/cdr aliases. Note that `cadr' is handled specially.
  1597. (defsetf first setcar)
  1598. (defsetf second (x) (store) (list 'setcar (list 'cdr x) store))
  1599. (defsetf third (x) (store) (list 'setcar (list 'cddr x) store))
  1600. (defsetf fourth (x) (store) (list 'setcar (list 'cdddr x) store))
  1601. (defsetf fifth (x) (store) (list 'setcar (list 'nthcdr 4 x) store))
  1602. (defsetf sixth (x) (store) (list 'setcar (list 'nthcdr 5 x) store))
  1603. (defsetf seventh (x) (store) (list 'setcar (list 'nthcdr 6 x) store))
  1604. (defsetf eighth (x) (store) (list 'setcar (list 'nthcdr 7 x) store))
  1605. (defsetf ninth (x) (store) (list 'setcar (list 'nthcdr 8 x) store))
  1606. (defsetf tenth (x) (store) (list 'setcar (list 'nthcdr 9 x) store))
  1607. (defsetf rest setcdr)
  1608. ;;; Some more Emacs-related place types.
  1609. (defsetf buffer-file-name set-visited-file-name t)
  1610. (defsetf buffer-modified-p (&optional buf) (flag)
  1611. (list 'with-current-buffer buf
  1612. (list 'set-buffer-modified-p flag)))
  1613. (defsetf buffer-name rename-buffer t)
  1614. (defsetf buffer-string () (store)
  1615. (list 'progn '(erase-buffer) (list 'insert store)))
  1616. (defsetf buffer-substring cl-set-buffer-substring)
  1617. (defsetf current-buffer set-buffer)
  1618. (defsetf current-case-table set-case-table)
  1619. (defsetf current-column move-to-column t)
  1620. (defsetf current-global-map use-global-map t)
  1621. (defsetf current-input-mode () (store)
  1622. (list 'progn (list 'apply 'set-input-mode store) store))
  1623. (defsetf current-local-map use-local-map t)
  1624. (defsetf current-window-configuration set-window-configuration t)
  1625. (defsetf default-file-modes set-default-file-modes t)
  1626. (defsetf default-value set-default)
  1627. (defsetf documentation-property put)
  1628. (defsetf face-background (f &optional s) (x) (list 'set-face-background f x s))
  1629. (defsetf face-background-pixmap (f &optional s) (x)
  1630. (list 'set-face-background-pixmap f x s))
  1631. (defsetf face-font (f &optional s) (x) (list 'set-face-font f x s))
  1632. (defsetf face-foreground (f &optional s) (x) (list 'set-face-foreground f x s))
  1633. (defsetf face-underline-p (f &optional s) (x)
  1634. (list 'set-face-underline-p f x s))
  1635. (defsetf file-modes set-file-modes t)
  1636. (defsetf frame-height set-screen-height t)
  1637. (defsetf frame-parameters modify-frame-parameters t)
  1638. (defsetf frame-visible-p cl-set-frame-visible-p)
  1639. (defsetf frame-width set-screen-width t)
  1640. (defsetf frame-parameter set-frame-parameter t)
  1641. (defsetf terminal-parameter set-terminal-parameter)
  1642. (defsetf getenv setenv t)
  1643. (defsetf get-register set-register)
  1644. (defsetf global-key-binding global-set-key)
  1645. (defsetf keymap-parent set-keymap-parent)
  1646. (defsetf local-key-binding local-set-key)
  1647. (defsetf mark set-mark t)
  1648. (defsetf mark-marker set-mark t)
  1649. (defsetf marker-position set-marker t)
  1650. (defsetf match-data set-match-data t)
  1651. (defsetf mouse-position (scr) (store)
  1652. (list 'set-mouse-position scr (list 'car store) (list 'cadr store)
  1653. (list 'cddr store)))
  1654. (defsetf overlay-get overlay-put)
  1655. (defsetf overlay-start (ov) (store)
  1656. (list 'progn (list 'move-overlay ov store (list 'overlay-end ov)) store))
  1657. (defsetf overlay-end (ov) (store)
  1658. (list 'progn (list 'move-overlay ov (list 'overlay-start ov) store) store))
  1659. (defsetf point goto-char)
  1660. (defsetf point-marker goto-char t)
  1661. (defsetf point-max () (store)
  1662. (list 'progn (list 'narrow-to-region '(point-min) store) store))
  1663. (defsetf point-min () (store)
  1664. (list 'progn (list 'narrow-to-region store '(point-max)) store))
  1665. (defsetf process-buffer set-process-buffer)
  1666. (defsetf process-filter set-process-filter)
  1667. (defsetf process-sentinel set-process-sentinel)
  1668. (defsetf process-get process-put)
  1669. (defsetf read-mouse-position (scr) (store)
  1670. (list 'set-mouse-position scr (list 'car store) (list 'cdr store)))
  1671. (defsetf screen-height set-screen-height t)
  1672. (defsetf screen-width set-screen-width t)
  1673. (defsetf selected-window select-window)
  1674. (defsetf selected-screen select-screen)
  1675. (defsetf selected-frame select-frame)
  1676. (defsetf standard-case-table set-standard-case-table)
  1677. (defsetf syntax-table set-syntax-table)
  1678. (defsetf visited-file-modtime set-visited-file-modtime t)
  1679. (defsetf window-buffer set-window-buffer t)
  1680. (defsetf window-display-table set-window-display-table t)
  1681. (defsetf window-dedicated-p set-window-dedicated-p t)
  1682. (defsetf window-height () (store)
  1683. (list 'progn (list 'enlarge-window (list '- store '(window-height))) store))
  1684. (defsetf window-hscroll set-window-hscroll)
  1685. (defsetf window-parameter set-window-parameter)
  1686. (defsetf window-point set-window-point)
  1687. (defsetf window-start set-window-start)
  1688. (defsetf window-width () (store)
  1689. (list 'progn (list 'enlarge-window (list '- store '(window-width)) t) store))
  1690. (defsetf x-get-secondary-selection x-own-secondary-selection t)
  1691. (defsetf x-get-selection x-own-selection t)
  1692. ;; This is a hack that allows (setf (eq a 7) B) to mean either
  1693. ;; (setq a 7) or (setq a nil) depending on whether B is nil or not.
  1694. ;; This is useful when you have control over the PLACE but not over
  1695. ;; the VALUE, as is the case in define-minor-mode's :variable.
  1696. (define-setf-method eq (place val)
  1697. (let ((method (get-setf-method place cl-macro-environment))
  1698. (val-temp (make-symbol "--eq-val--"))
  1699. (store-temp (make-symbol "--eq-store--")))
  1700. (list (append (nth 0 method) (list val-temp))
  1701. (append (nth 1 method) (list val))
  1702. (list store-temp)
  1703. `(let ((,(car (nth 2 method))
  1704. (if ,store-temp ,val-temp (not ,val-temp))))
  1705. ,(nth 3 method) ,store-temp)
  1706. `(eq ,(nth 4 method) ,val-temp))))
  1707. ;;; More complex setf-methods.
  1708. ;; These should take &environment arguments, but since full arglists aren't
  1709. ;; available while compiling cl-macs, we fake it by referring to the global
  1710. ;; variable cl-macro-environment directly.
  1711. (define-setf-method apply (func arg1 &rest rest)
  1712. (or (and (memq (car-safe func) '(quote function function*))
  1713. (symbolp (car-safe (cdr-safe func))))
  1714. (error "First arg to apply in setf is not (function SYM): %s" func))
  1715. (let* ((form (cons (nth 1 func) (cons arg1 rest)))
  1716. (method (get-setf-method form cl-macro-environment)))
  1717. (list (car method) (nth 1 method) (nth 2 method)
  1718. (cl-setf-make-apply (nth 3 method) (cadr func) (car method))
  1719. (cl-setf-make-apply (nth 4 method) (cadr func) (car method)))))
  1720. (defun cl-setf-make-apply (form func temps)
  1721. (if (eq (car form) 'progn)
  1722. (list* 'progn (cl-setf-make-apply (cadr form) func temps) (cddr form))
  1723. (or (equal (last form) (last temps))
  1724. (error "%s is not suitable for use with setf-of-apply" func))
  1725. (list* 'apply (list 'quote (car form)) (cdr form))))
  1726. (define-setf-method nthcdr (n place)
  1727. (let ((method (get-setf-method place cl-macro-environment))
  1728. (n-temp (make-symbol "--cl-nthcdr-n--"))
  1729. (store-temp (make-symbol "--cl-nthcdr-store--")))
  1730. (list (cons n-temp (car method))
  1731. (cons n (nth 1 method))
  1732. (list store-temp)
  1733. (list 'let (list (list (car (nth 2 method))
  1734. (list 'cl-set-nthcdr n-temp (nth 4 method)
  1735. store-temp)))
  1736. (nth 3 method) store-temp)
  1737. (list 'nthcdr n-temp (nth 4 method)))))
  1738. (define-setf-method getf (place tag &optional def)
  1739. (let ((method (get-setf-method place cl-macro-environment))
  1740. (tag-temp (make-symbol "--cl-getf-tag--"))
  1741. (def-temp (make-symbol "--cl-getf-def--"))
  1742. (store-temp (make-symbol "--cl-getf-store--")))
  1743. (list (append (car method) (list tag-temp def-temp))
  1744. (append (nth 1 method) (list tag def))
  1745. (list store-temp)
  1746. (list 'let (list (list (car (nth 2 method))
  1747. (list 'cl-set-getf (nth 4 method)
  1748. tag-temp store-temp)))
  1749. (nth 3 method) store-temp)
  1750. (list 'getf (nth 4 method) tag-temp def-temp))))
  1751. (define-setf-method substring (place from &optional to)
  1752. (let ((method (get-setf-method place cl-macro-environment))
  1753. (from-temp (make-symbol "--cl-substring-from--"))
  1754. (to-temp (make-symbol "--cl-substring-to--"))
  1755. (store-temp (make-symbol "--cl-substring-store--")))
  1756. (list (append (car method) (list from-temp to-temp))
  1757. (append (nth 1 method) (list from to))
  1758. (list store-temp)
  1759. (list 'let (list (list (car (nth 2 method))
  1760. (list 'cl-set-substring (nth 4 method)
  1761. from-temp to-temp store-temp)))
  1762. (nth 3 method) store-temp)
  1763. (list 'substring (nth 4 method) from-temp to-temp))))
  1764. ;;; Getting and optimizing setf-methods.
  1765. ;;;###autoload
  1766. (defun get-setf-method (place &optional env)
  1767. "Return a list of five values describing the setf-method for PLACE.
  1768. PLACE may be any Lisp form which can appear as the PLACE argument to
  1769. a macro like `setf' or `incf'."
  1770. (if (symbolp place)
  1771. (let ((temp (make-symbol "--cl-setf--")))
  1772. (list nil nil (list temp) (list 'setq place temp) place))
  1773. (or (and (symbolp (car place))
  1774. (let* ((func (car place))
  1775. (name (symbol-name func))
  1776. (method (get func 'setf-method))
  1777. (case-fold-search nil))
  1778. (or (and method
  1779. (let ((cl-macro-environment env))
  1780. (setq method (apply method (cdr place))))
  1781. (if (and (consp method) (= (length method) 5))
  1782. method
  1783. (error "Setf-method for %s returns malformed method"
  1784. func)))
  1785. (and (string-match-p "\\`c[ad][ad][ad]?[ad]?r\\'" name)
  1786. (get-setf-method (compiler-macroexpand place)))
  1787. (and (eq func 'edebug-after)
  1788. (get-setf-method (nth (1- (length place)) place)
  1789. env)))))
  1790. (if (eq place (setq place (macroexpand place env)))
  1791. (if (and (symbolp (car place)) (fboundp (car place))
  1792. (symbolp (symbol-function (car place))))
  1793. (get-setf-method (cons (symbol-function (car place))
  1794. (cdr place)) env)
  1795. (error "No setf-method known for %s" (car place)))
  1796. (get-setf-method place env)))))
  1797. (defun cl-setf-do-modify (place opt-expr)
  1798. (let* ((method (get-setf-method place cl-macro-environment))
  1799. (temps (car method)) (values (nth 1 method))
  1800. (lets nil) (subs nil)
  1801. (optimize (and (not (eq opt-expr 'no-opt))
  1802. (or (and (not (eq opt-expr 'unsafe))
  1803. (cl-safe-expr-p opt-expr))
  1804. (cl-setf-simple-store-p (car (nth 2 method))
  1805. (nth 3 method)))))
  1806. (simple (and optimize (consp place) (cl-simple-exprs-p (cdr place)))))
  1807. (while values
  1808. (if (or simple (cl-const-expr-p (car values)))
  1809. (push (cons (pop temps) (pop values)) subs)
  1810. (push (list (pop temps) (pop values)) lets)))
  1811. (list (nreverse lets)
  1812. (cons (car (nth 2 method)) (sublis subs (nth 3 method)))
  1813. (sublis subs (nth 4 method)))))
  1814. (defun cl-setf-do-store (spec val)
  1815. (let ((sym (car spec))
  1816. (form (cdr spec)))
  1817. (if (or (cl-const-expr-p val)
  1818. (and (cl-simple-expr-p val) (eq (cl-expr-contains form sym) 1))
  1819. (cl-setf-simple-store-p sym form))
  1820. (subst val sym form)
  1821. (list 'let (list (list sym val)) form))))
  1822. (defun cl-setf-simple-store-p (sym form)
  1823. (and (consp form) (eq (cl-expr-contains form sym) 1)
  1824. (eq (nth (1- (length form)) form) sym)
  1825. (symbolp (car form)) (fboundp (car form))
  1826. (not (eq (car-safe (symbol-function (car form))) 'macro))))
  1827. ;;; The standard modify macros.
  1828. ;;;###autoload
  1829. (defmacro setf (&rest args)
  1830. "Set each PLACE to the value of its VAL.
  1831. This is a generalized version of `setq'; the PLACEs may be symbolic
  1832. references such as (car x) or (aref x i), as well as plain symbols.
  1833. For example, (setf (cadar x) y) is equivalent to (setcar (cdar x) y).
  1834. The return value is the last VAL in the list.
  1835. \(fn PLACE VAL PLACE VAL ...)"
  1836. (if (cdr (cdr args))
  1837. (let ((sets nil))
  1838. (while args (push (list 'setf (pop args) (pop args)) sets))
  1839. (cons 'progn (nreverse sets)))
  1840. (if (symbolp (car args))
  1841. (and args (cons 'setq args))
  1842. (let* ((method (cl-setf-do-modify (car args) (nth 1 args)))
  1843. (store (cl-setf-do-store (nth 1 method) (nth 1 args))))
  1844. (if (car method) (list 'let* (car method) store) store)))))
  1845. ;;;###autoload
  1846. (defmacro psetf (&rest args)
  1847. "Set PLACEs to the values VALs in parallel.
  1848. This is like `setf', except that all VAL forms are evaluated (in order)
  1849. before assigning any PLACEs to the corresponding values.
  1850. \(fn PLACE VAL PLACE VAL ...)"
  1851. (let ((p args) (simple t) (vars nil))
  1852. (while p
  1853. (if (or (not (symbolp (car p))) (cl-expr-depends-p (nth 1 p) vars))
  1854. (setq simple nil))
  1855. (if (memq (car p) vars)
  1856. (error "Destination duplicated in psetf: %s" (car p)))
  1857. (push (pop p) vars)
  1858. (or p (error "Odd number of arguments to psetf"))
  1859. (pop p))
  1860. (if simple
  1861. (list 'progn (cons 'setf args) nil)
  1862. (setq args (reverse args))
  1863. (let ((expr (list 'setf (cadr args) (car args))))
  1864. (while (setq args (cddr args))
  1865. (setq expr (list 'setf (cadr args) (list 'prog1 (car args) expr))))
  1866. (list 'progn expr nil)))))
  1867. ;;;###autoload
  1868. (defun cl-do-pop (place)
  1869. (if (cl-simple-expr-p place)
  1870. (list 'prog1 (list 'car place) (list 'setf place (list 'cdr place)))
  1871. (let* ((method (cl-setf-do-modify place t))
  1872. (temp (make-symbol "--cl-pop--")))
  1873. (list 'let*
  1874. (append (car method)
  1875. (list (list temp (nth 2 method))))
  1876. (list 'prog1
  1877. (list 'car temp)
  1878. (cl-setf-do-store (nth 1 method) (list 'cdr temp)))))))
  1879. ;;;###autoload
  1880. (defmacro remf (place tag)
  1881. "Remove TAG from property list PLACE.
  1882. PLACE may be a symbol, or any generalized variable allowed by `setf'.
  1883. The form returns true if TAG was found and removed, nil otherwise."
  1884. (let* ((method (cl-setf-do-modify place t))
  1885. (tag-temp (and (not (cl-const-expr-p tag)) (make-symbol "--cl-remf-tag--")))
  1886. (val-temp (and (not (cl-simple-expr-p place))
  1887. (make-symbol "--cl-remf-place--")))
  1888. (ttag (or tag-temp tag))
  1889. (tval (or val-temp (nth 2 method))))
  1890. (list 'let*
  1891. (append (car method)
  1892. (and val-temp (list (list val-temp (nth 2 method))))
  1893. (and tag-temp (list (list tag-temp tag))))
  1894. (list 'if (list 'eq ttag (list 'car tval))
  1895. (list 'progn
  1896. (cl-setf-do-store (nth 1 method) (list 'cddr tval))
  1897. t)
  1898. (list 'cl-do-remf tval ttag)))))
  1899. ;;;###autoload
  1900. (defmacro shiftf (place &rest args)
  1901. "Shift left among PLACEs.
  1902. Example: (shiftf A B C) sets A to B, B to C, and returns the old A.
  1903. Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
  1904. \(fn PLACE... VAL)"
  1905. (cond
  1906. ((null args) place)
  1907. ((symbolp place) `(prog1 ,place (setq ,place (shiftf ,@args))))
  1908. (t
  1909. (let ((method (cl-setf-do-modify place 'unsafe)))
  1910. `(let* ,(car method)
  1911. (prog1 ,(nth 2 method)
  1912. ,(cl-setf-do-store (nth 1 method) `(shiftf ,@args))))))))
  1913. ;;;###autoload
  1914. (defmacro rotatef (&rest args)
  1915. "Rotate left among PLACEs.
  1916. Example: (rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
  1917. Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
  1918. \(fn PLACE...)"
  1919. (if (not (memq nil (mapcar 'symbolp args)))
  1920. (and (cdr args)
  1921. (let ((sets nil)
  1922. (first (car args)))
  1923. (while (cdr args)
  1924. (setq sets (nconc sets (list (pop args) (car args)))))
  1925. (nconc (list 'psetf) sets (list (car args) first))))
  1926. (let* ((places (reverse args))
  1927. (temp (make-symbol "--cl-rotatef--"))
  1928. (form temp))
  1929. (while (cdr places)
  1930. (let ((method (cl-setf-do-modify (pop places) 'unsafe)))
  1931. (setq form (list 'let* (car method)
  1932. (list 'prog1 (nth 2 method)
  1933. (cl-setf-do-store (nth 1 method) form))))))
  1934. (let ((method (cl-setf-do-modify (car places) 'unsafe)))
  1935. (list 'let* (append (car method) (list (list temp (nth 2 method))))
  1936. (cl-setf-do-store (nth 1 method) form) nil)))))
  1937. ;;;###autoload
  1938. (defmacro letf (bindings &rest body)
  1939. "Temporarily bind to PLACEs.
  1940. This is the analogue of `let', but with generalized variables (in the
  1941. sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
  1942. VALUE, then the BODY forms are executed. On exit, either normally or
  1943. because of a `throw' or error, the PLACEs are set back to their original
  1944. values. Note that this macro is *not* available in Common Lisp.
  1945. As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
  1946. the PLACE is not modified before executing BODY.
  1947. \(fn ((PLACE VALUE) ...) BODY...)"
  1948. (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings)))
  1949. (list* 'let bindings body)
  1950. (let ((lets nil) (sets nil)
  1951. (unsets nil) (rev (reverse bindings)))
  1952. (while rev
  1953. (let* ((place (if (symbolp (caar rev))
  1954. (list 'symbol-value (list 'quote (caar rev)))
  1955. (caar rev)))
  1956. (value (cadar rev))
  1957. (method (cl-setf-do-modify place 'no-opt))
  1958. (save (make-symbol "--cl-letf-save--"))
  1959. (bound (and (memq (car place) '(symbol-value symbol-function))
  1960. (make-symbol "--cl-letf-bound--")))
  1961. (temp (and (not (cl-const-expr-p value)) (cdr bindings)
  1962. (make-symbol "--cl-letf-val--"))))
  1963. (setq lets (nconc (car method)
  1964. (if bound
  1965. (list (list bound
  1966. (list (if (eq (car place)
  1967. 'symbol-value)
  1968. 'boundp 'fboundp)
  1969. (nth 1 (nth 2 method))))
  1970. (list save (list 'and bound
  1971. (nth 2 method))))
  1972. (list (list save (nth 2 method))))
  1973. (and temp (list (list temp value)))
  1974. lets)
  1975. body (list
  1976. (list 'unwind-protect
  1977. (cons 'progn
  1978. (if (cdr (car rev))
  1979. (cons (cl-setf-do-store (nth 1 method)
  1980. (or temp value))
  1981. body)
  1982. body))
  1983. (if bound
  1984. (list 'if bound
  1985. (cl-setf-do-store (nth 1 method) save)
  1986. (list (if (eq (car place) 'symbol-value)
  1987. 'makunbound 'fmakunbound)
  1988. (nth 1 (nth 2 method))))
  1989. (cl-setf-do-store (nth 1 method) save))))
  1990. rev (cdr rev))))
  1991. (list* 'let* lets body))))
  1992. ;;;###autoload
  1993. (defmacro letf* (bindings &rest body)
  1994. "Temporarily bind to PLACEs.
  1995. This is the analogue of `let*', but with generalized variables (in the
  1996. sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
  1997. VALUE, then the BODY forms are executed. On exit, either normally or
  1998. because of a `throw' or error, the PLACEs are set back to their original
  1999. values. Note that this macro is *not* available in Common Lisp.
  2000. As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
  2001. the PLACE is not modified before executing BODY.
  2002. \(fn ((PLACE VALUE) ...) BODY...)"
  2003. (if (null bindings)
  2004. (cons 'progn body)
  2005. (setq bindings (reverse bindings))
  2006. (while bindings
  2007. (setq body (list (list* 'letf (list (pop bindings)) body))))
  2008. (car body)))
  2009. ;;;###autoload
  2010. (defmacro callf (func place &rest args)
  2011. "Set PLACE to (FUNC PLACE ARGS...).
  2012. FUNC should be an unquoted function name. PLACE may be a symbol,
  2013. or any generalized variable allowed by `setf'.
  2014. \(fn FUNC PLACE ARGS...)"
  2015. (let* ((method (cl-setf-do-modify place (cons 'list args)))
  2016. (rargs (cons (nth 2 method) args)))
  2017. (list 'let* (car method)
  2018. (cl-setf-do-store (nth 1 method)
  2019. (if (symbolp func) (cons func rargs)
  2020. (list* 'funcall (list 'function func)
  2021. rargs))))))
  2022. ;;;###autoload
  2023. (defmacro callf2 (func arg1 place &rest args)
  2024. "Set PLACE to (FUNC ARG1 PLACE ARGS...).
  2025. Like `callf', but PLACE is the second argument of FUNC, not the first.
  2026. \(fn FUNC ARG1 PLACE ARGS...)"
  2027. (if (and (cl-safe-expr-p arg1) (cl-simple-expr-p place) (symbolp func))
  2028. (list 'setf place (list* func arg1 place args))
  2029. (let* ((method (cl-setf-do-modify place (cons 'list args)))
  2030. (temp (and (not (cl-const-expr-p arg1)) (make-symbol "--cl-arg1--")))
  2031. (rargs (list* (or temp arg1) (nth 2 method) args)))
  2032. (list 'let* (append (and temp (list (list temp arg1))) (car method))
  2033. (cl-setf-do-store (nth 1 method)
  2034. (if (symbolp func) (cons func rargs)
  2035. (list* 'funcall (list 'function func)
  2036. rargs)))))))
  2037. ;;;###autoload
  2038. (defmacro define-modify-macro (name arglist func &optional doc)
  2039. "Define a `setf'-like modify macro.
  2040. If NAME is called, it combines its PLACE argument with the other arguments
  2041. from ARGLIST using FUNC: (define-modify-macro incf (&optional (n 1)) +)"
  2042. (if (memq '&key arglist) (error "&key not allowed in define-modify-macro"))
  2043. (let ((place (make-symbol "--cl-place--")))
  2044. (list 'defmacro* name (cons place arglist) doc
  2045. (list* (if (memq '&rest arglist) 'list* 'list)
  2046. '(quote callf) (list 'quote func) place
  2047. (cl-arglist-args arglist)))))
  2048. ;;; Structures.
  2049. ;;;###autoload
  2050. (defmacro defstruct (struct &rest descs)
  2051. "Define a struct type.
  2052. This macro defines a new data type called NAME that stores data
  2053. in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
  2054. copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
  2055. You can use the accessors to set the corresponding slots, via `setf'.
  2056. NAME may instead take the form (NAME OPTIONS...), where each
  2057. OPTION is either a single keyword or (KEYWORD VALUE).
  2058. See Info node `(cl)Structures' for a list of valid keywords.
  2059. Each SLOT may instead take the form (SLOT SLOT-OPTS...), where
  2060. SLOT-OPTS are keyword-value pairs for that slot. Currently, only
  2061. one keyword is supported, `:read-only'. If this has a non-nil
  2062. value, that slot cannot be set via `setf'.
  2063. \(fn NAME SLOTS...)"
  2064. (let* ((name (if (consp struct) (car struct) struct))
  2065. (opts (cdr-safe struct))
  2066. (slots nil)
  2067. (defaults nil)
  2068. (conc-name (concat (symbol-name name) "-"))
  2069. (constructor (intern (format "make-%s" name)))
  2070. (constrs nil)
  2071. (copier (intern (format "copy-%s" name)))
  2072. (predicate (intern (format "%s-p" name)))
  2073. (print-func nil) (print-auto nil)
  2074. (safety (if (cl-compiling-file) cl-optimize-safety 3))
  2075. (include nil)
  2076. (tag (intern (format "cl-struct-%s" name)))
  2077. (tag-symbol (intern (format "cl-struct-%s-tags" name)))
  2078. (include-descs nil)
  2079. (side-eff nil)
  2080. (type nil)
  2081. (named nil)
  2082. (forms nil)
  2083. pred-form pred-check)
  2084. (if (stringp (car descs))
  2085. (push (list 'put (list 'quote name) '(quote structure-documentation)
  2086. (pop descs)) forms))
  2087. (setq descs (cons '(cl-tag-slot)
  2088. (mapcar (function (lambda (x) (if (consp x) x (list x))))
  2089. descs)))
  2090. (while opts
  2091. (let ((opt (if (consp (car opts)) (caar opts) (car opts)))
  2092. (args (cdr-safe (pop opts))))
  2093. (cond ((eq opt :conc-name)
  2094. (if args
  2095. (setq conc-name (if (car args)
  2096. (symbol-name (car args)) ""))))
  2097. ((eq opt :constructor)
  2098. (if (cdr args)
  2099. (progn
  2100. ;; If this defines a constructor of the same name as
  2101. ;; the default one, don't define the default.
  2102. (if (eq (car args) constructor)
  2103. (setq constructor nil))
  2104. (push args constrs))
  2105. (if args (setq constructor (car args)))))
  2106. ((eq opt :copier)
  2107. (if args (setq copier (car args))))
  2108. ((eq opt :predicate)
  2109. (if args (setq predicate (car args))))
  2110. ((eq opt :include)
  2111. (setq include (car args)
  2112. include-descs (mapcar (function
  2113. (lambda (x)
  2114. (if (consp x) x (list x))))
  2115. (cdr args))))
  2116. ((eq opt :print-function)
  2117. (setq print-func (car args)))
  2118. ((eq opt :type)
  2119. (setq type (car args)))
  2120. ((eq opt :named)
  2121. (setq named t))
  2122. ((eq opt :initial-offset)
  2123. (setq descs (nconc (make-list (car args) '(cl-skip-slot))
  2124. descs)))
  2125. (t
  2126. (error "Slot option %s unrecognized" opt)))))
  2127. (if print-func
  2128. (setq print-func (list 'progn
  2129. (list 'funcall (list 'function print-func)
  2130. 'cl-x 'cl-s 'cl-n) t))
  2131. (or type (and include (not (get include 'cl-struct-print)))
  2132. (setq print-auto t
  2133. print-func (and (or (not (or include type)) (null print-func))
  2134. (list 'progn
  2135. (list 'princ (format "#S(%s" name)
  2136. 'cl-s))))))
  2137. (if include
  2138. (let ((inc-type (get include 'cl-struct-type))
  2139. (old-descs (get include 'cl-struct-slots)))
  2140. (or inc-type (error "%s is not a struct name" include))
  2141. (and type (not (eq (car inc-type) type))
  2142. (error ":type disagrees with :include for %s" name))
  2143. (while include-descs
  2144. (setcar (memq (or (assq (caar include-descs) old-descs)
  2145. (error "No slot %s in included struct %s"
  2146. (caar include-descs) include))
  2147. old-descs)
  2148. (pop include-descs)))
  2149. (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs))
  2150. type (car inc-type)
  2151. named (assq 'cl-tag-slot descs))
  2152. (if (cadr inc-type) (setq tag name named t))
  2153. (let ((incl include))
  2154. (while incl
  2155. (push (list 'pushnew (list 'quote tag)
  2156. (intern (format "cl-struct-%s-tags" incl)))
  2157. forms)
  2158. (setq incl (get incl 'cl-struct-include)))))
  2159. (if type
  2160. (progn
  2161. (or (memq type '(vector list))
  2162. (error "Invalid :type specifier: %s" type))
  2163. (if named (setq tag name)))
  2164. (setq type 'vector named 'true)))
  2165. (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
  2166. (push (list 'defvar tag-symbol) forms)
  2167. (setq pred-form (and named
  2168. (let ((pos (- (length descs)
  2169. (length (memq (assq 'cl-tag-slot descs)
  2170. descs)))))
  2171. (if (eq type 'vector)
  2172. (list 'and '(vectorp cl-x)
  2173. (list '>= '(length cl-x) (length descs))
  2174. (list 'memq (list 'aref 'cl-x pos)
  2175. tag-symbol))
  2176. (if (= pos 0)
  2177. (list 'memq '(car-safe cl-x) tag-symbol)
  2178. (list 'and '(consp cl-x)
  2179. (list 'memq (list 'nth pos 'cl-x)
  2180. tag-symbol))))))
  2181. pred-check (and pred-form (> safety 0)
  2182. (if (and (eq (caadr pred-form) 'vectorp)
  2183. (= safety 1))
  2184. (cons 'and (cdddr pred-form)) pred-form)))
  2185. (let ((pos 0) (descp descs))
  2186. (while descp
  2187. (let* ((desc (pop descp))
  2188. (slot (car desc)))
  2189. (if (memq slot '(cl-tag-slot cl-skip-slot))
  2190. (progn
  2191. (push nil slots)
  2192. (push (and (eq slot 'cl-tag-slot) (list 'quote tag))
  2193. defaults))
  2194. (if (assq slot descp)
  2195. (error "Duplicate slots named %s in %s" slot name))
  2196. (let ((accessor (intern (format "%s%s" conc-name slot))))
  2197. (push slot slots)
  2198. (push (nth 1 desc) defaults)
  2199. (push (list*
  2200. 'defsubst* accessor '(cl-x)
  2201. (append
  2202. (and pred-check
  2203. (list (list 'or pred-check
  2204. `(error "%s accessing a non-%s"
  2205. ',accessor ',name))))
  2206. (list (if (eq type 'vector) (list 'aref 'cl-x pos)
  2207. (if (= pos 0) '(car cl-x)
  2208. (list 'nth pos 'cl-x)))))) forms)
  2209. (push (cons accessor t) side-eff)
  2210. (push (list 'define-setf-method accessor '(cl-x)
  2211. (if (cadr (memq :read-only (cddr desc)))
  2212. (list 'progn '(ignore cl-x)
  2213. `(error "%s is a read-only slot"
  2214. ',accessor))
  2215. ;; If cl is loaded only for compilation,
  2216. ;; the call to cl-struct-setf-expander would
  2217. ;; cause a warning because it may not be
  2218. ;; defined at run time. Suppress that warning.
  2219. (list 'with-no-warnings
  2220. (list 'cl-struct-setf-expander 'cl-x
  2221. (list 'quote name) (list 'quote accessor)
  2222. (and pred-check (list 'quote pred-check))
  2223. pos))))
  2224. forms)
  2225. (if print-auto
  2226. (nconc print-func
  2227. (list (list 'princ (format " %s" slot) 'cl-s)
  2228. (list 'prin1 (list accessor 'cl-x) 'cl-s)))))))
  2229. (setq pos (1+ pos))))
  2230. (setq slots (nreverse slots)
  2231. defaults (nreverse defaults))
  2232. (and predicate pred-form
  2233. (progn (push (list 'defsubst* predicate '(cl-x)
  2234. (if (eq (car pred-form) 'and)
  2235. (append pred-form '(t))
  2236. (list 'and pred-form t))) forms)
  2237. (push (cons predicate 'error-free) side-eff)))
  2238. (and copier
  2239. (progn (push (list 'defun copier '(x) '(copy-sequence x)) forms)
  2240. (push (cons copier t) side-eff)))
  2241. (if constructor
  2242. (push (list constructor
  2243. (cons '&key (delq nil (copy-sequence slots))))
  2244. constrs))
  2245. (while constrs
  2246. (let* ((name (caar constrs))
  2247. (args (cadr (pop constrs)))
  2248. (anames (cl-arglist-args args))
  2249. (make (mapcar* (function (lambda (s d) (if (memq s anames) s d)))
  2250. slots defaults)))
  2251. (push (list 'defsubst* name
  2252. (list* '&cl-defs (list 'quote (cons nil descs)) args)
  2253. (cons type make)) forms)
  2254. (if (cl-safe-expr-p (cons 'progn (mapcar 'second descs)))
  2255. (push (cons name t) side-eff))))
  2256. (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
  2257. (if print-func
  2258. (push `(push
  2259. ;; The auto-generated function does not pay attention to
  2260. ;; the depth argument cl-n.
  2261. (lambda (cl-x cl-s ,(if print-auto '_cl-n 'cl-n))
  2262. (and ,pred-form ,print-func))
  2263. custom-print-functions)
  2264. forms))
  2265. (push (list 'setq tag-symbol (list 'list (list 'quote tag))) forms)
  2266. (push (list* 'eval-when '(compile load eval)
  2267. (list 'put (list 'quote name) '(quote cl-struct-slots)
  2268. (list 'quote descs))
  2269. (list 'put (list 'quote name) '(quote cl-struct-type)
  2270. (list 'quote (list type (eq named t))))
  2271. (list 'put (list 'quote name) '(quote cl-struct-include)
  2272. (list 'quote include))
  2273. (list 'put (list 'quote name) '(quote cl-struct-print)
  2274. print-auto)
  2275. (mapcar (function (lambda (x)
  2276. (list 'put (list 'quote (car x))
  2277. '(quote side-effect-free)
  2278. (list 'quote (cdr x)))))
  2279. side-eff))
  2280. forms)
  2281. (cons 'progn (nreverse (cons (list 'quote name) forms)))))
  2282. ;;;###autoload
  2283. (defun cl-struct-setf-expander (x name accessor pred-form pos)
  2284. (let* ((temp (make-symbol "--cl-x--")) (store (make-symbol "--cl-store--")))
  2285. (list (list temp) (list x) (list store)
  2286. (append '(progn)
  2287. (and pred-form
  2288. (list (list 'or (subst temp 'cl-x pred-form)
  2289. (list 'error
  2290. (format
  2291. "%s storing a non-%s" accessor name)))))
  2292. (list (if (eq (car (get name 'cl-struct-type)) 'vector)
  2293. (list 'aset temp pos store)
  2294. (list 'setcar
  2295. (if (<= pos 5)
  2296. (let ((xx temp))
  2297. (while (>= (setq pos (1- pos)) 0)
  2298. (setq xx (list 'cdr xx)))
  2299. xx)
  2300. (list 'nthcdr pos temp))
  2301. store))))
  2302. (list accessor temp))))
  2303. ;;; Types and assertions.
  2304. ;;;###autoload
  2305. (defmacro deftype (name arglist &rest body)
  2306. "Define NAME as a new data type.
  2307. The type name can then be used in `typecase', `check-type', etc."
  2308. (list 'eval-when '(compile load eval)
  2309. (cl-transform-function-property
  2310. name 'cl-deftype-handler (cons (list* '&cl-defs ''('*) arglist) body))))
  2311. (defun cl-make-type-test (val type)
  2312. (if (symbolp type)
  2313. (cond ((get type 'cl-deftype-handler)
  2314. (cl-make-type-test val (funcall (get type 'cl-deftype-handler))))
  2315. ((memq type '(nil t)) type)
  2316. ((eq type 'null) `(null ,val))
  2317. ((eq type 'atom) `(atom ,val))
  2318. ((eq type 'float) `(floatp-safe ,val))
  2319. ((eq type 'real) `(numberp ,val))
  2320. ((eq type 'fixnum) `(integerp ,val))
  2321. ;; FIXME: Should `character' accept things like ?\C-\M-a ? -stef
  2322. ((memq type '(character string-char)) `(characterp ,val))
  2323. (t
  2324. (let* ((name (symbol-name type))
  2325. (namep (intern (concat name "p"))))
  2326. (if (fboundp namep) (list namep val)
  2327. (list (intern (concat name "-p")) val)))))
  2328. (cond ((get (car type) 'cl-deftype-handler)
  2329. (cl-make-type-test val (apply (get (car type) 'cl-deftype-handler)
  2330. (cdr type))))
  2331. ((memq (car type) '(integer float real number))
  2332. (delq t (list 'and (cl-make-type-test val (car type))
  2333. (if (memq (cadr type) '(* nil)) t
  2334. (if (consp (cadr type)) (list '> val (caadr type))
  2335. (list '>= val (cadr type))))
  2336. (if (memq (caddr type) '(* nil)) t
  2337. (if (consp (caddr type)) (list '< val (caaddr type))
  2338. (list '<= val (caddr type)))))))
  2339. ((memq (car type) '(and or not))
  2340. (cons (car type)
  2341. (mapcar (function (lambda (x) (cl-make-type-test val x)))
  2342. (cdr type))))
  2343. ((memq (car type) '(member member*))
  2344. (list 'and (list 'member* val (list 'quote (cdr type))) t))
  2345. ((eq (car type) 'satisfies) (list (cadr type) val))
  2346. (t (error "Bad type spec: %s" type)))))
  2347. ;;;###autoload
  2348. (defun typep (object type) ; See compiler macro below.
  2349. "Check that OBJECT is of type TYPE.
  2350. TYPE is a Common Lisp-style type specifier."
  2351. (eval (cl-make-type-test 'object type)))
  2352. ;;;###autoload
  2353. (defmacro check-type (form type &optional string)
  2354. "Verify that FORM is of type TYPE; signal an error if not.
  2355. STRING is an optional description of the desired type."
  2356. (and (or (not (cl-compiling-file))
  2357. (< cl-optimize-speed 3) (= cl-optimize-safety 3))
  2358. (let* ((temp (if (cl-simple-expr-p form 3)
  2359. form (make-symbol "--cl-var--")))
  2360. (body (list 'or (cl-make-type-test temp type)
  2361. (list 'signal '(quote wrong-type-argument)
  2362. (list 'list (or string (list 'quote type))
  2363. temp (list 'quote form))))))
  2364. (if (eq temp form) (list 'progn body nil)
  2365. (list 'let (list (list temp form)) body nil)))))
  2366. ;;;###autoload
  2367. (defmacro assert (form &optional show-args string &rest args)
  2368. "Verify that FORM returns non-nil; signal an error if not.
  2369. Second arg SHOW-ARGS means to include arguments of FORM in message.
  2370. Other args STRING and ARGS... are arguments to be passed to `error'.
  2371. They are not evaluated unless the assertion fails. If STRING is
  2372. omitted, a default message listing FORM itself is used."
  2373. (and (or (not (cl-compiling-file))
  2374. (< cl-optimize-speed 3) (= cl-optimize-safety 3))
  2375. (let ((sargs (and show-args
  2376. (delq nil (mapcar
  2377. (lambda (x)
  2378. (unless (cl-const-expr-p x)
  2379. x))
  2380. (cdr form))))))
  2381. (list 'progn
  2382. (list 'or form
  2383. (if string
  2384. (list* 'error string (append sargs args))
  2385. (list 'signal '(quote cl-assertion-failed)
  2386. (list* 'list (list 'quote form) sargs))))
  2387. nil))))
  2388. ;;; Compiler macros.
  2389. ;;;###autoload
  2390. (defmacro define-compiler-macro (func args &rest body)
  2391. "Define a compiler-only macro.
  2392. This is like `defmacro', but macro expansion occurs only if the call to
  2393. FUNC is compiled (i.e., not interpreted). Compiler macros should be used
  2394. for optimizing the way calls to FUNC are compiled; the form returned by
  2395. BODY should do the same thing as a call to the normal function called
  2396. FUNC, though possibly more efficiently. Note that, like regular macros,
  2397. compiler macros are expanded repeatedly until no further expansions are
  2398. possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
  2399. original function call alone by declaring an initial `&whole foo' parameter
  2400. and then returning foo."
  2401. (let ((p args) (res nil))
  2402. (while (consp p) (push (pop p) res))
  2403. (setq args (nconc (nreverse res) (and p (list '&rest p)))))
  2404. (list 'eval-when '(compile load eval)
  2405. (cl-transform-function-property
  2406. func 'cl-compiler-macro
  2407. (cons (if (memq '&whole args) (delq '&whole args)
  2408. (cons '_cl-whole-arg args)) body))
  2409. (list 'or (list 'get (list 'quote func) '(quote byte-compile))
  2410. (list 'progn
  2411. (list 'put (list 'quote func) '(quote byte-compile)
  2412. '(quote cl-byte-compile-compiler-macro))
  2413. ;; This is so that describe-function can locate
  2414. ;; the macro definition.
  2415. (list 'let
  2416. (list (list
  2417. 'file
  2418. (or buffer-file-name
  2419. (and (boundp 'byte-compile-current-file)
  2420. (stringp byte-compile-current-file)
  2421. byte-compile-current-file))))
  2422. (list 'if 'file
  2423. (list 'put (list 'quote func)
  2424. '(quote compiler-macro-file)
  2425. '(purecopy (file-name-nondirectory file)))))))))
  2426. ;;;###autoload
  2427. (defun compiler-macroexpand (form)
  2428. (while
  2429. (let ((func (car-safe form)) (handler nil))
  2430. (while (and (symbolp func)
  2431. (not (setq handler (get func 'cl-compiler-macro)))
  2432. (fboundp func)
  2433. (or (not (eq (car-safe (symbol-function func)) 'autoload))
  2434. (load (nth 1 (symbol-function func)))))
  2435. (setq func (symbol-function func)))
  2436. (and handler
  2437. (not (eq form (setq form (apply handler form (cdr form))))))))
  2438. form)
  2439. (defun cl-byte-compile-compiler-macro (form)
  2440. (if (eq form (setq form (compiler-macroexpand form)))
  2441. (byte-compile-normal-call form)
  2442. (byte-compile-form form)))
  2443. ;; Optimize away unused block-wrappers.
  2444. (defvar cl-active-block-names nil)
  2445. (define-compiler-macro cl-block-wrapper (cl-form)
  2446. (let* ((cl-entry (cons (nth 1 (nth 1 cl-form)) nil))
  2447. (cl-active-block-names (cons cl-entry cl-active-block-names))
  2448. (cl-body (macroexpand-all ;Performs compiler-macro expansions.
  2449. (cons 'progn (cddr cl-form))
  2450. macroexpand-all-environment)))
  2451. ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able
  2452. ;; to indicate that this return value is already fully expanded.
  2453. (if (cdr cl-entry)
  2454. `(catch ,(nth 1 cl-form) ,@(cdr cl-body))
  2455. cl-body)))
  2456. (define-compiler-macro cl-block-throw (cl-tag cl-value)
  2457. (let ((cl-found (assq (nth 1 cl-tag) cl-active-block-names)))
  2458. (if cl-found (setcdr cl-found t)))
  2459. `(throw ,cl-tag ,cl-value))
  2460. ;;;###autoload
  2461. (defmacro defsubst* (name args &rest body)
  2462. "Define NAME as a function.
  2463. Like `defun', except the function is automatically declared `inline',
  2464. ARGLIST allows full Common Lisp conventions, and BODY is implicitly
  2465. surrounded by (block NAME ...).
  2466. \(fn NAME ARGLIST [DOCSTRING] BODY...)"
  2467. (let* ((argns (cl-arglist-args args)) (p argns)
  2468. (pbody (cons 'progn body))
  2469. (unsafe (not (cl-safe-expr-p pbody))))
  2470. (while (and p (eq (cl-expr-contains args (car p)) 1)) (pop p))
  2471. (list 'progn
  2472. (if p nil ; give up if defaults refer to earlier args
  2473. (list 'define-compiler-macro name
  2474. (if (memq '&key args)
  2475. (list* '&whole 'cl-whole '&cl-quote args)
  2476. (cons '&cl-quote args))
  2477. (list* 'cl-defsubst-expand (list 'quote argns)
  2478. (list 'quote (list* 'block name body))
  2479. ;; We used to pass `simple' as
  2480. ;; (not (or unsafe (cl-expr-access-order pbody argns)))
  2481. ;; But this is much too simplistic since it
  2482. ;; does not pay attention to the argvs (and
  2483. ;; cl-expr-access-order itself is also too naive).
  2484. nil
  2485. (and (memq '&key args) 'cl-whole) unsafe argns)))
  2486. (list* 'defun* name args body))))
  2487. (defun cl-defsubst-expand (argns body simple whole unsafe &rest argvs)
  2488. (if (and whole (not (cl-safe-expr-p (cons 'progn argvs)))) whole
  2489. (if (cl-simple-exprs-p argvs) (setq simple t))
  2490. (let* ((substs ())
  2491. (lets (delq nil
  2492. (mapcar* (function
  2493. (lambda (argn argv)
  2494. (if (or simple (cl-const-expr-p argv))
  2495. (progn (push (cons argn argv) substs)
  2496. (and unsafe (list argn argv)))
  2497. (list argn argv))))
  2498. argns argvs))))
  2499. ;; FIXME: `sublis/subst' will happily substitute the symbol
  2500. ;; `argn' in places where it's not used as a reference
  2501. ;; to a variable.
  2502. ;; FIXME: `sublis/subst' will happily copy `argv' to a different
  2503. ;; scope, leading to name capture.
  2504. (setq body (cond ((null substs) body)
  2505. ((null (cdr substs))
  2506. (subst (cdar substs) (caar substs) body))
  2507. (t (sublis substs body))))
  2508. (if lets (list 'let lets body) body))))
  2509. ;; Compile-time optimizations for some functions defined in this package.
  2510. ;; Note that cl.el arranges to force cl-macs to be loaded at compile-time,
  2511. ;; mainly to make sure these macros will be present.
  2512. (put 'eql 'byte-compile nil)
  2513. (define-compiler-macro eql (&whole form a b)
  2514. (cond ((eq (cl-const-expr-p a) t)
  2515. (let ((val (cl-const-expr-val a)))
  2516. (if (and (numberp val) (not (integerp val)))
  2517. (list 'equal a b)
  2518. (list 'eq a b))))
  2519. ((eq (cl-const-expr-p b) t)
  2520. (let ((val (cl-const-expr-val b)))
  2521. (if (and (numberp val) (not (integerp val)))
  2522. (list 'equal a b)
  2523. (list 'eq a b))))
  2524. ((cl-simple-expr-p a 5)
  2525. (list 'if (list 'numberp a)
  2526. (list 'equal a b)
  2527. (list 'eq a b)))
  2528. ((and (cl-safe-expr-p a)
  2529. (cl-simple-expr-p b 5))
  2530. (list 'if (list 'numberp b)
  2531. (list 'equal a b)
  2532. (list 'eq a b)))
  2533. (t form)))
  2534. (define-compiler-macro member* (&whole form a list &rest keys)
  2535. (let ((test (and (= (length keys) 2) (eq (car keys) :test)
  2536. (cl-const-expr-val (nth 1 keys)))))
  2537. (cond ((eq test 'eq) (list 'memq a list))
  2538. ((eq test 'equal) (list 'member a list))
  2539. ((or (null keys) (eq test 'eql)) (list 'memql a list))
  2540. (t form))))
  2541. (define-compiler-macro assoc* (&whole form a list &rest keys)
  2542. (let ((test (and (= (length keys) 2) (eq (car keys) :test)
  2543. (cl-const-expr-val (nth 1 keys)))))
  2544. (cond ((eq test 'eq) (list 'assq a list))
  2545. ((eq test 'equal) (list 'assoc a list))
  2546. ((and (eq (cl-const-expr-p a) t) (or (null keys) (eq test 'eql)))
  2547. (if (floatp-safe (cl-const-expr-val a))
  2548. (list 'assoc a list) (list 'assq a list)))
  2549. (t form))))
  2550. (define-compiler-macro adjoin (&whole form a list &rest keys)
  2551. (if (and (cl-simple-expr-p a) (cl-simple-expr-p list)
  2552. (not (memq :key keys)))
  2553. (list 'if (list* 'member* a list keys) list (list 'cons a list))
  2554. form))
  2555. (define-compiler-macro list* (arg &rest others)
  2556. (let* ((args (reverse (cons arg others)))
  2557. (form (car args)))
  2558. (while (setq args (cdr args))
  2559. (setq form (list 'cons (car args) form)))
  2560. form))
  2561. (define-compiler-macro get* (sym prop &optional def)
  2562. (if def
  2563. (list 'getf (list 'symbol-plist sym) prop def)
  2564. (list 'get sym prop)))
  2565. (define-compiler-macro typep (&whole form val type)
  2566. (if (cl-const-expr-p type)
  2567. (let ((res (cl-make-type-test val (cl-const-expr-val type))))
  2568. (if (or (memq (cl-expr-contains res val) '(nil 1))
  2569. (cl-simple-expr-p val)) res
  2570. (let ((temp (make-symbol "--cl-var--")))
  2571. (list 'let (list (list temp val)) (subst temp val res)))))
  2572. form))
  2573. (mapc (lambda (y)
  2574. (put (car y) 'side-effect-free t)
  2575. (put (car y) 'byte-compile 'cl-byte-compile-compiler-macro)
  2576. (put (car y) 'cl-compiler-macro
  2577. `(lambda (w x)
  2578. ,(if (symbolp (cadr y))
  2579. `(list ',(cadr y)
  2580. (list ',(caddr y) x))
  2581. (cons 'list (cdr y))))))
  2582. '((first 'car x) (second 'cadr x) (third 'caddr x) (fourth 'cadddr x)
  2583. (fifth 'nth 4 x) (sixth 'nth 5 x) (seventh 'nth 6 x)
  2584. (eighth 'nth 7 x) (ninth 'nth 8 x) (tenth 'nth 9 x)
  2585. (rest 'cdr x) (endp 'null x) (plusp '> x 0) (minusp '< x 0)
  2586. (caaar car caar) (caadr car cadr) (cadar car cdar)
  2587. (caddr car cddr) (cdaar cdr caar) (cdadr cdr cadr)
  2588. (cddar cdr cdar) (cdddr cdr cddr) (caaaar car caaar)
  2589. (caaadr car caadr) (caadar car cadar) (caaddr car caddr)
  2590. (cadaar car cdaar) (cadadr car cdadr) (caddar car cddar)
  2591. (cadddr car cdddr) (cdaaar cdr caaar) (cdaadr cdr caadr)
  2592. (cdadar cdr cadar) (cdaddr cdr caddr) (cddaar cdr cdaar)
  2593. (cddadr cdr cdadr) (cdddar cdr cddar) (cddddr cdr cdddr) ))
  2594. ;;; Things that are inline.
  2595. (proclaim '(inline floatp-safe acons map concatenate notany notevery
  2596. cl-set-elt revappend nreconc gethash))
  2597. ;;; Things that are side-effect-free.
  2598. (mapc (lambda (x) (put x 'side-effect-free t))
  2599. '(oddp evenp signum last butlast ldiff pairlis gcd lcm
  2600. isqrt floor* ceiling* truncate* round* mod* rem* subseq
  2601. list-length get* getf))
  2602. ;;; Things that are side-effect-and-error-free.
  2603. (mapc (lambda (x) (put x 'side-effect-free 'error-free))
  2604. '(eql floatp-safe list* subst acons equalp random-state-p
  2605. copy-tree sublis))
  2606. (run-hooks 'cl-macs-load-hook)
  2607. ;; Local variables:
  2608. ;; byte-compile-dynamic: t
  2609. ;; byte-compile-warnings: (not cl-functions)
  2610. ;; generated-autoload-file: "cl-loaddefs.el"
  2611. ;; End:
  2612. ;;; cl-macs.el ends here