package.el 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. ;;; package.el --- Simple package system for Emacs -*- lexical-binding:t -*-
  2. ;; Copyright (C) 2007-2017 Free Software Foundation, Inc.
  3. ;; Author: Tom Tromey <tromey@redhat.com>
  4. ;; Daniel Hackney <dan@haxney.org>
  5. ;; Created: 10 Mar 2007
  6. ;; Version: 1.1.0
  7. ;; Keywords: tools
  8. ;; Package-Requires: ((tabulated-list "1.0"))
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; The idea behind package.el is to be able to download packages and
  22. ;; install them. Packages are versioned and have versioned
  23. ;; dependencies. Furthermore, this supports built-in packages which
  24. ;; may or may not be newer than user-specified packages. This makes
  25. ;; it possible to upgrade Emacs and automatically disable packages
  26. ;; which have moved from external to core. (Note though that we don't
  27. ;; currently register any of these, so this feature does not actually
  28. ;; work.)
  29. ;; A package is described by its name and version. The distribution
  30. ;; format is either a tar file or a single .el file.
  31. ;; A tar file should be named "NAME-VERSION.tar". The tar file must
  32. ;; unpack into a directory named after the package and version:
  33. ;; "NAME-VERSION". It must contain a file named "PACKAGE-pkg.el"
  34. ;; which consists of a call to define-package. It may also contain a
  35. ;; "dir" file and the info files it references.
  36. ;; A .el file is named "NAME-VERSION.el" in the remote archive, but is
  37. ;; installed as simply "NAME.el" in a directory named "NAME-VERSION".
  38. ;; The downloader downloads all dependent packages. By default,
  39. ;; packages come from the official GNU sources, but others may be
  40. ;; added by customizing the `package-archives' alist. Packages get
  41. ;; byte-compiled at install time.
  42. ;; At activation time we will set up the load-path and the info path,
  43. ;; and we will load the package's autoloads. If a package's
  44. ;; dependencies are not available, we will not activate that package.
  45. ;; Conceptually a package has multiple state transitions:
  46. ;;
  47. ;; * Download. Fetching the package from ELPA.
  48. ;; * Install. Untar the package, or write the .el file, into
  49. ;; ~/.emacs.d/elpa/ directory.
  50. ;; * Autoload generation.
  51. ;; * Byte compile. Currently this phase is done during install,
  52. ;; but we may change this.
  53. ;; * Activate. Evaluate the autoloads for the package to make it
  54. ;; available to the user.
  55. ;; * Load. Actually load the package and run some code from it.
  56. ;; Other external functions you may want to use:
  57. ;;
  58. ;; M-x list-packages
  59. ;; Enters a mode similar to buffer-menu which lets you manage
  60. ;; packages. You can choose packages for install (mark with "i",
  61. ;; then "x" to execute) or deletion (not implemented yet), and you
  62. ;; can see what packages are available. This will automatically
  63. ;; fetch the latest list of packages from ELPA.
  64. ;;
  65. ;; M-x package-install-from-buffer
  66. ;; Install a package consisting of a single .el file that appears
  67. ;; in the current buffer. This only works for packages which
  68. ;; define a Version header properly; package.el also supports the
  69. ;; extension headers Package-Version (in case Version is an RCS id
  70. ;; or similar), and Package-Requires (if the package requires other
  71. ;; packages).
  72. ;;
  73. ;; M-x package-install-file
  74. ;; Install a package from the indicated file. The package can be
  75. ;; either a tar file or a .el file. A tar file must contain an
  76. ;; appropriately-named "-pkg.el" file; a .el file must be properly
  77. ;; formatted as with package-install-from-buffer.
  78. ;;; Thanks:
  79. ;;; (sorted by sort-lines):
  80. ;; Jim Blandy <jimb@red-bean.com>
  81. ;; Karl Fogel <kfogel@red-bean.com>
  82. ;; Kevin Ryde <user42@zip.com.au>
  83. ;; Lawrence Mitchell
  84. ;; Michael Olson <mwolson@member.fsf.org>
  85. ;; Sebastian Tennant <sebyte@smolny.plus.com>
  86. ;; Stefan Monnier <monnier@iro.umontreal.ca>
  87. ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
  88. ;; Phil Hagelberg <phil@hagelb.org>
  89. ;;; ToDo:
  90. ;; - putting info dirs at the start of the info path means
  91. ;; users see a weird ordering of categories. OTOH we want to
  92. ;; override later entries. maybe emacs needs to enforce
  93. ;; the standard layout?
  94. ;; - put bytecode in a separate directory tree
  95. ;; - perhaps give users a way to recompile their bytecode
  96. ;; or do it automatically when emacs changes
  97. ;; - give users a way to know whether a package is installed ok
  98. ;; - give users a way to view a package's documentation when it
  99. ;; only appears in the .el
  100. ;; - use/extend checkdoc so people can tell if their package will work
  101. ;; - "installed" instead of a blank in the status column
  102. ;; - tramp needs its files to be compiled in a certain order.
  103. ;; how to handle this? fix tramp?
  104. ;; - maybe we need separate .elc directories for various emacs versions
  105. ;; and also emacs-vs-xemacs. That way conditional compilation can
  106. ;; work. But would this break anything?
  107. ;; - William Xu suggests being able to open a package file without
  108. ;; installing it
  109. ;; - Interface with desktop.el so that restarting after an install
  110. ;; works properly
  111. ;; - Use hierarchical layout. PKG/etc PKG/lisp PKG/info
  112. ;; ... except maybe lisp?
  113. ;; - It may be nice to have a macro that expands to the package's
  114. ;; private data dir, aka ".../etc". Or, maybe data-directory
  115. ;; needs to be a list (though this would be less nice)
  116. ;; a few packages want this, eg sokoban
  117. ;; - Allow multiple versions on the server, so that if a user doesn't
  118. ;; meet the requirements for the most recent version they can still
  119. ;; install an older one.
  120. ;; - Allow optional package dependencies
  121. ;; then if we require 'bbdb', bbdb-specific lisp in lisp/bbdb
  122. ;; and just don't compile to add to load path ...?
  123. ;; - Our treatment of the info path is somewhat bogus
  124. ;;; Code:
  125. (eval-when-compile (require 'subr-x))
  126. (eval-when-compile (require 'cl-lib))
  127. (eval-when-compile (require 'epg)) ;For setf accessors.
  128. (require 'seq)
  129. (require 'tabulated-list)
  130. (require 'macroexp)
  131. (require 'url-handlers)
  132. (defgroup package nil
  133. "Manager for Emacs Lisp packages."
  134. :group 'applications
  135. :version "24.1")
  136. ;;; Customization options
  137. ;;;###autoload
  138. (defcustom package-enable-at-startup t
  139. "Whether to activate installed packages when Emacs starts.
  140. If non-nil, packages are activated after reading the init file
  141. and before `after-init-hook'. Activation is not done if
  142. `user-init-file' is nil (e.g. Emacs was started with \"-q\").
  143. Even if the value is nil, you can type \\[package-initialize] to
  144. activate the package system at any time."
  145. :type 'boolean
  146. :version "24.1")
  147. (defcustom package-load-list '(all)
  148. "List of packages for `package-initialize' to load.
  149. Each element in this list should be a list (NAME VERSION), or the
  150. symbol `all'. The symbol `all' says to load the latest installed
  151. versions of all packages not specified by other elements.
  152. For an element (NAME VERSION), NAME is a package name (a symbol).
  153. VERSION should be t, a string, or nil.
  154. If VERSION is t, the most recent version is activated.
  155. If VERSION is a string, only that version is ever loaded.
  156. Any other version, even if newer, is silently ignored.
  157. Hence, the package is \"held\" at that version.
  158. If VERSION is nil, the package is not loaded (it is \"disabled\")."
  159. :type '(repeat (choice (const all)
  160. (list :tag "Specific package"
  161. (symbol :tag "Package name")
  162. (choice :tag "Version"
  163. (const :tag "disable" nil)
  164. (const :tag "most recent" t)
  165. (string :tag "specific version")))))
  166. :risky t
  167. :version "24.1")
  168. (defcustom package-archives `(("gnu" .
  169. ,(format "http%s://elpa.gnu.org/packages/"
  170. (if (gnutls-available-p) "s" ""))))
  171. "An alist of archives from which to fetch.
  172. The default value points to the GNU Emacs package repository.
  173. Each element has the form (ID . LOCATION).
  174. ID is an archive name, as a string.
  175. LOCATION specifies the base location for the archive.
  176. If it starts with \"http(s):\", it is treated as an HTTP(S) URL;
  177. otherwise it should be an absolute directory name.
  178. (Other types of URL are currently not supported.)
  179. Only add locations that you trust, since fetching and installing
  180. a package can run arbitrary code."
  181. :type '(alist :key-type (string :tag "Archive name")
  182. :value-type (string :tag "URL or directory name"))
  183. :risky t
  184. :version "26.1") ; gnutls test
  185. (defcustom package-menu-hide-low-priority 'archive
  186. "If non-nil, hide low priority packages from the packages menu.
  187. A package is considered low priority if there's another version
  188. of it available such that:
  189. (a) the archive of the other package is higher priority than
  190. this one, as per `package-archive-priorities';
  191. or
  192. (b) they both have the same archive priority but the other
  193. package has a higher version number.
  194. This variable has three possible values:
  195. nil: no packages are hidden;
  196. `archive': only criterion (a) is used;
  197. t: both criteria are used.
  198. This variable has no effect if `package-menu--hide-packages' is
  199. nil, so it can be toggled with \\<package-menu-mode-map> \\[package-menu-toggle-hiding]."
  200. :type '(choice (const :tag "Don't hide anything" nil)
  201. (const :tag "Hide per package-archive-priorities"
  202. archive)
  203. (const :tag "Hide per archive and version number" t))
  204. :version "25.1")
  205. (defcustom package-archive-priorities nil
  206. "An alist of priorities for packages.
  207. Each element has the form (ARCHIVE-ID . PRIORITY).
  208. When installing packages, the package with the highest version
  209. number from the archive with the highest priority is
  210. selected. When higher versions are available from archives with
  211. lower priorities, the user has to select those manually.
  212. Archives not in this list have the priority 0.
  213. See also `package-menu-hide-low-priority'."
  214. :type '(alist :key-type (string :tag "Archive name")
  215. :value-type (integer :tag "Priority (default is 0)"))
  216. :risky t
  217. :version "25.1")
  218. (defcustom package-pinned-packages nil
  219. "An alist of packages that are pinned to specific archives.
  220. This can be useful if you have multiple package archives enabled,
  221. and want to control which archive a given package gets installed from.
  222. Each element of the alist has the form (PACKAGE . ARCHIVE), where:
  223. PACKAGE is a symbol representing a package
  224. ARCHIVE is a string representing an archive (it should be the car of
  225. an element in `package-archives', e.g. \"gnu\").
  226. Adding an entry to this variable means that only ARCHIVE will be
  227. considered as a source for PACKAGE. If other archives provide PACKAGE,
  228. they are ignored (for this package). If ARCHIVE does not contain PACKAGE,
  229. the package will be unavailable."
  230. :type '(alist :key-type (symbol :tag "Package")
  231. :value-type (string :tag "Archive name"))
  232. ;; I don't really see why this is risky...
  233. ;; I suppose it could prevent you receiving updates for a package,
  234. ;; via an entry (PACKAGE . NON-EXISTING). Which could be an issue
  235. ;; if PACKAGE has a known vulnerability that is fixed in newer versions.
  236. :risky t
  237. :version "24.4")
  238. (defcustom package-user-dir (locate-user-emacs-file "elpa")
  239. "Directory containing the user's Emacs Lisp packages.
  240. The directory name should be absolute.
  241. Apart from this directory, Emacs also looks for system-wide
  242. packages in `package-directory-list'."
  243. :type 'directory
  244. :risky t
  245. :version "24.1")
  246. (defcustom package-directory-list
  247. ;; Defaults are subdirs named "elpa" in the site-lisp dirs.
  248. (let (result)
  249. (dolist (f load-path)
  250. (and (stringp f)
  251. (equal (file-name-nondirectory f) "site-lisp")
  252. (push (expand-file-name "elpa" f) result)))
  253. (nreverse result))
  254. "List of additional directories containing Emacs Lisp packages.
  255. Each directory name should be absolute.
  256. These directories contain packages intended for system-wide; in
  257. contrast, `package-user-dir' contains packages for personal use."
  258. :type '(repeat directory)
  259. :risky t
  260. :version "24.1")
  261. (declare-function epg-find-configuration "epg-config"
  262. (protocol &optional no-cache program-alist))
  263. (defcustom package-gnupghome-dir (expand-file-name "gnupg" package-user-dir)
  264. "Directory containing GnuPG keyring or nil.
  265. This variable specifies the GnuPG home directory used by package.
  266. That directory is passed via the option \"--homedir\" to GnuPG.
  267. If nil, do not use the option \"--homedir\", but stick with GnuPG's
  268. default directory."
  269. :type `(choice
  270. (const
  271. :tag "Default Emacs package management GnuPG home directory"
  272. ,(expand-file-name "gnupg" package-user-dir))
  273. (const
  274. :tag "Default GnuPG directory (GnuPG option --homedir not used)"
  275. nil)
  276. (directory :tag "A specific GnuPG --homedir"))
  277. :risky t
  278. :version "26.1")
  279. (defcustom package-check-signature
  280. (if (and (require 'epg-config)
  281. (epg-find-configuration 'OpenPGP))
  282. 'allow-unsigned)
  283. "Non-nil means to check package signatures when installing.
  284. The value `allow-unsigned' means to still install a package even if
  285. it is unsigned.
  286. This also applies to the \"archive-contents\" file that lists the
  287. contents of the archive."
  288. :type '(choice (const nil :tag "Never")
  289. (const allow-unsigned :tag "Allow unsigned")
  290. (const t :tag "Check always"))
  291. :risky t
  292. :version "24.4")
  293. (defcustom package-unsigned-archives nil
  294. "List of archives where we do not check for package signatures."
  295. :type '(repeat (string :tag "Archive name"))
  296. :risky t
  297. :version "24.4")
  298. (defcustom package-selected-packages nil
  299. "Store here packages installed explicitly by user.
  300. This variable is fed automatically by Emacs when installing a new package.
  301. This variable is used by `package-autoremove' to decide
  302. which packages are no longer needed.
  303. You can use it to (re)install packages on other machines
  304. by running `package-install-selected-packages'.
  305. To check if a package is contained in this list here, use
  306. `package--user-selected-p', as it may populate the variable with
  307. a sane initial value."
  308. :version "25.1"
  309. :type '(repeat symbol))
  310. (defcustom package-menu-async t
  311. "If non-nil, package-menu will use async operations when possible.
  312. Currently, only the refreshing of archive contents supports
  313. asynchronous operations. Package transactions are still done
  314. synchronously."
  315. :type 'boolean
  316. :version "25.1")
  317. ;;; `package-desc' object definition
  318. ;; This is the struct used internally to represent packages.
  319. ;; Functions that deal with packages should generally take this object
  320. ;; as an argument. In some situations (e.g. commands that query the
  321. ;; user) it makes sense to take the package name as a symbol instead,
  322. ;; but keep in mind there could be multiple `package-desc's with the
  323. ;; same name.
  324. (defvar package--default-summary "No description available.")
  325. (cl-defstruct (package-desc
  326. ;; Rename the default constructor from `make-package-desc'.
  327. (:constructor package-desc-create)
  328. ;; Has the same interface as the old `define-package',
  329. ;; which is still used in the "foo-pkg.el" files. Extra
  330. ;; options can be supported by adding additional keys.
  331. (:constructor
  332. package-desc-from-define
  333. (name-string version-string &optional summary requirements
  334. &rest rest-plist
  335. &aux
  336. (name (intern name-string))
  337. (version (version-to-list version-string))
  338. (reqs (mapcar #'(lambda (elt)
  339. (list (car elt)
  340. (version-to-list (cadr elt))))
  341. (if (eq 'quote (car requirements))
  342. (nth 1 requirements)
  343. requirements)))
  344. (kind (plist-get rest-plist :kind))
  345. (archive (plist-get rest-plist :archive))
  346. (extras (let (alist)
  347. (while rest-plist
  348. (unless (memq (car rest-plist) '(:kind :archive))
  349. (let ((value (cadr rest-plist)))
  350. (when value
  351. (push (cons (car rest-plist)
  352. (if (eq (car-safe value) 'quote)
  353. (cadr value)
  354. value))
  355. alist))))
  356. (setq rest-plist (cddr rest-plist)))
  357. alist)))))
  358. "Structure containing information about an individual package.
  359. Slots:
  360. `name' Name of the package, as a symbol.
  361. `version' Version of the package, as a version list.
  362. `summary' Short description of the package, typically taken from
  363. the first line of the file.
  364. `reqs' Requirements of the package. A list of (PACKAGE
  365. VERSION-LIST) naming the dependent package and the minimum
  366. required version.
  367. `kind' The distribution format of the package. Currently, it is
  368. either `single' or `tar'.
  369. `archive' The name of the archive (as a string) whence this
  370. package came.
  371. `dir' The directory where the package is installed (if installed),
  372. `builtin' if it is built-in, or nil otherwise.
  373. `extras' Optional alist of additional keyword-value pairs.
  374. `signed' Flag to indicate that the package is signed by provider."
  375. name
  376. version
  377. (summary package--default-summary)
  378. reqs
  379. kind
  380. archive
  381. dir
  382. extras
  383. signed)
  384. (defun package--from-builtin (bi-desc)
  385. (package-desc-create :name (pop bi-desc)
  386. :version (package--bi-desc-version bi-desc)
  387. :summary (package--bi-desc-summary bi-desc)
  388. :dir 'builtin))
  389. ;; Pseudo fields.
  390. (defun package-version-join (vlist)
  391. "Return the version string corresponding to the list VLIST.
  392. This is, approximately, the inverse of `version-to-list'.
  393. \(Actually, it returns only one of the possible inverses, since
  394. `version-to-list' is a many-to-one operation.)"
  395. (if (null vlist)
  396. ""
  397. (let ((str-list (list "." (int-to-string (car vlist)))))
  398. (dolist (num (cdr vlist))
  399. (cond
  400. ((>= num 0)
  401. (push (int-to-string num) str-list)
  402. (push "." str-list))
  403. ((< num -4)
  404. (error "Invalid version list `%s'" vlist))
  405. (t
  406. ;; pre, or beta, or alpha
  407. (cond ((equal "." (car str-list))
  408. (pop str-list))
  409. ((not (string-match "[0-9]+" (car str-list)))
  410. (error "Invalid version list `%s'" vlist)))
  411. (push (cond ((= num -1) "pre")
  412. ((= num -2) "beta")
  413. ((= num -3) "alpha")
  414. ((= num -4) "snapshot"))
  415. str-list))))
  416. (if (equal "." (car str-list))
  417. (pop str-list))
  418. (apply 'concat (nreverse str-list)))))
  419. (defun package-desc-full-name (pkg-desc)
  420. (format "%s-%s"
  421. (package-desc-name pkg-desc)
  422. (package-version-join (package-desc-version pkg-desc))))
  423. (defun package-desc-suffix (pkg-desc)
  424. (pcase (package-desc-kind pkg-desc)
  425. (`single ".el")
  426. (`tar ".tar")
  427. (`dir "")
  428. (kind (error "Unknown package kind: %s" kind))))
  429. (defun package-desc--keywords (pkg-desc)
  430. (let ((keywords (cdr (assoc :keywords (package-desc-extras pkg-desc)))))
  431. (if (eq (car-safe keywords) 'quote)
  432. (nth 1 keywords)
  433. keywords)))
  434. (defun package-desc-priority (p)
  435. "Return the priority of the archive of package-desc object P."
  436. (package-archive-priority (package-desc-archive p)))
  437. ;; Package descriptor format used in finder-inf.el and package--builtins.
  438. (cl-defstruct (package--bi-desc
  439. (:constructor package-make-builtin (version summary))
  440. (:type vector))
  441. version
  442. reqs
  443. summary)
  444. ;;; Installed packages
  445. ;; The following variables store information about packages present in
  446. ;; the system. The most important of these is `package-alist'. The
  447. ;; command `package-initialize' is also closely related to this
  448. ;; section, but it is left for a later section because it also affects
  449. ;; other stuff.
  450. (defvar package--builtins nil
  451. "Alist of built-in packages.
  452. The actual value is initialized by loading the library
  453. `finder-inf'; this is not done until it is needed, e.g. by the
  454. function `package-built-in-p'.
  455. Each element has the form (PKG . PACKAGE-BI-DESC), where PKG is a package
  456. name (a symbol) and DESC is a `package--bi-desc' structure.")
  457. (put 'package--builtins 'risky-local-variable t)
  458. (defvar package-alist nil
  459. "Alist of all packages available for activation.
  460. Each element has the form (PKG . DESCS), where PKG is a package
  461. name (a symbol) and DESCS is a non-empty list of `package-desc' structure,
  462. sorted by decreasing versions.
  463. This variable is set automatically by `package-load-descriptor',
  464. called via `package-initialize'. To change which packages are
  465. loaded and/or activated, customize `package-load-list'.")
  466. (put 'package-alist 'risky-local-variable t)
  467. (defvar package-activated-list nil
  468. ;; FIXME: This should implicitly include all builtin packages.
  469. "List of the names of currently activated packages.")
  470. (put 'package-activated-list 'risky-local-variable t)
  471. ;;;; Populating `package-alist'.
  472. ;; The following functions are called on each installed package by
  473. ;; `package-load-all-descriptors', which ultimately populates the
  474. ;; `package-alist' variable.
  475. (defun package-process-define-package (exp)
  476. (when (eq (car-safe exp) 'define-package)
  477. (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
  478. (name (package-desc-name new-pkg-desc))
  479. (version (package-desc-version new-pkg-desc))
  480. (old-pkgs (assq name package-alist)))
  481. (if (null old-pkgs)
  482. ;; If there's no old package, just add this to `package-alist'.
  483. (push (list name new-pkg-desc) package-alist)
  484. ;; If there is, insert the new package at the right place in the list.
  485. (while
  486. (if (and (cdr old-pkgs)
  487. (version-list-< version
  488. (package-desc-version (cadr old-pkgs))))
  489. (setq old-pkgs (cdr old-pkgs))
  490. (push new-pkg-desc (cdr old-pkgs))
  491. nil)))
  492. new-pkg-desc)))
  493. (defun package-load-descriptor (pkg-dir)
  494. "Load the description file in directory PKG-DIR."
  495. (let ((pkg-file (expand-file-name (package--description-file pkg-dir)
  496. pkg-dir))
  497. (signed-file (concat pkg-dir ".signed")))
  498. (when (file-exists-p pkg-file)
  499. (with-temp-buffer
  500. (insert-file-contents pkg-file)
  501. (goto-char (point-min))
  502. (let ((pkg-desc (or (package-process-define-package
  503. (read (current-buffer)))
  504. (error "Can't find define-package in %s" pkg-file))))
  505. (setf (package-desc-dir pkg-desc) pkg-dir)
  506. (if (file-exists-p signed-file)
  507. (setf (package-desc-signed pkg-desc) t))
  508. pkg-desc)))))
  509. (defun package-load-all-descriptors ()
  510. "Load descriptors for installed Emacs Lisp packages.
  511. This looks for package subdirectories in `package-user-dir' and
  512. `package-directory-list'. The variable `package-load-list'
  513. controls which package subdirectories may be loaded.
  514. In each valid package subdirectory, this function loads the
  515. description file containing a call to `define-package', which
  516. updates `package-alist'."
  517. (dolist (dir (cons package-user-dir package-directory-list))
  518. (when (file-directory-p dir)
  519. (dolist (subdir (directory-files dir))
  520. (unless (equal subdir "..")
  521. (let ((pkg-dir (expand-file-name subdir dir)))
  522. (when (file-directory-p pkg-dir)
  523. (package-load-descriptor pkg-dir))))))))
  524. (defun define-package (_name-string _version-string
  525. &optional _docstring _requirements
  526. &rest _extra-properties)
  527. "Define a new package.
  528. NAME-STRING is the name of the package, as a string.
  529. VERSION-STRING is the version of the package, as a string.
  530. DOCSTRING is a short description of the package, a string.
  531. REQUIREMENTS is a list of dependencies on other packages.
  532. Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
  533. where OTHER-VERSION is a string.
  534. EXTRA-PROPERTIES is currently unused."
  535. ;; FIXME: Placeholder! Should we keep it?
  536. (error "Don't call me!"))
  537. ;;; Package activation
  538. ;; Section for functions used by `package-activate', which see.
  539. (defun package-disabled-p (pkg-name version)
  540. "Return whether PKG-NAME at VERSION can be activated.
  541. The decision is made according to `package-load-list'.
  542. Return nil if the package can be activated.
  543. Return t if the package is completely disabled.
  544. Return the max version (as a string) if the package is held at a lower version."
  545. (let ((force (assq pkg-name package-load-list)))
  546. (cond ((null force) (not (memq 'all package-load-list)))
  547. ((null (setq force (cadr force))) t) ; disabled
  548. ((eq force t) nil)
  549. ((stringp force) ; held
  550. (unless (version-list-= version (version-to-list force))
  551. force))
  552. (t (error "Invalid element in `package-load-list'")))))
  553. (defun package-built-in-p (package &optional min-version)
  554. "Return non-nil if PACKAGE is built-in to Emacs.
  555. Optional arg MIN-VERSION, if non-nil, should be a version list
  556. specifying the minimum acceptable version."
  557. (if (package-desc-p package) ;; was built-in and then was converted
  558. (eq 'builtin (package-desc-dir package))
  559. (let ((bi (assq package package--builtin-versions)))
  560. (cond
  561. (bi (version-list-<= min-version (cdr bi)))
  562. ((remove 0 min-version) nil)
  563. (t
  564. (require 'finder-inf nil t) ; For `package--builtins'.
  565. (assq package package--builtins))))))
  566. (defun package--autoloads-file-name (pkg-desc)
  567. "Return the absolute name of the autoloads file, sans extension.
  568. PKG-DESC is a `package-desc' object."
  569. (expand-file-name
  570. (format "%s-autoloads" (package-desc-name pkg-desc))
  571. (package-desc-dir pkg-desc)))
  572. (defun package--activate-autoloads-and-load-path (pkg-desc)
  573. "Load the autoloads file and add package dir to `load-path'.
  574. PKG-DESC is a `package-desc' object."
  575. (let* ((old-lp load-path)
  576. (pkg-dir (package-desc-dir pkg-desc))
  577. (pkg-dir-dir (file-name-as-directory pkg-dir)))
  578. (with-demoted-errors "Error loading autoloads: %s"
  579. (load (package--autoloads-file-name pkg-desc) nil t))
  580. (when (and (eq old-lp load-path)
  581. (not (or (member pkg-dir load-path)
  582. (member pkg-dir-dir load-path))))
  583. ;; Old packages don't add themselves to the `load-path', so we have to
  584. ;; do it ourselves.
  585. (push pkg-dir load-path))))
  586. (defvar Info-directory-list)
  587. (declare-function info-initialize "info" ())
  588. (defun package--load-files-for-activation (pkg-desc reload)
  589. "Load files for activating a package given by PKG-DESC.
  590. Load the autoloads file, and ensure `load-path' is setup. If
  591. RELOAD is non-nil, also load all files in the package that
  592. correspond to previously loaded files."
  593. (let* ((loaded-files-list (when reload
  594. (package--list-loaded-files (package-desc-dir pkg-desc)))))
  595. ;; Add to load path, add autoloads, and activate the package.
  596. (package--activate-autoloads-and-load-path pkg-desc)
  597. ;; Call `load' on all files in `package-desc-dir' already present in
  598. ;; `load-history'. This is done so that macros in these files are updated
  599. ;; to their new definitions. If another package is being installed which
  600. ;; depends on this new definition, not doing this update would cause
  601. ;; compilation errors and break the installation.
  602. (with-demoted-errors "Error in package--load-files-for-activation: %s"
  603. (mapc (lambda (feature) (load feature nil t))
  604. ;; Skip autoloads file since we already evaluated it above.
  605. (remove (file-truename (package--autoloads-file-name pkg-desc))
  606. loaded-files-list)))))
  607. (defun package-activate-1 (pkg-desc &optional reload deps)
  608. "Activate package given by PKG-DESC, even if it was already active.
  609. If DEPS is non-nil, also activate its dependencies (unless they
  610. are already activated).
  611. If RELOAD is non-nil, also `load' any files inside the package which
  612. correspond to previously loaded files (those returned by
  613. `package--list-loaded-files')."
  614. (let* ((name (package-desc-name pkg-desc))
  615. (pkg-dir (package-desc-dir pkg-desc)))
  616. (unless pkg-dir
  617. (error "Internal error: unable to find directory for `%s'"
  618. (package-desc-full-name pkg-desc)))
  619. (catch 'exit
  620. ;; Activate its dependencies recursively.
  621. ;; FIXME: This doesn't check whether the activated version is the
  622. ;; required version.
  623. (when deps
  624. (dolist (req (package-desc-reqs pkg-desc))
  625. (unless (package-activate (car req))
  626. (message "Unable to activate package `%s'.\nRequired package `%s-%s' is unavailable"
  627. name (car req) (package-version-join (cadr req)))
  628. (throw 'exit nil))))
  629. (package--load-files-for-activation pkg-desc reload)
  630. ;; Add info node.
  631. (when (file-exists-p (expand-file-name "dir" pkg-dir))
  632. ;; FIXME: not the friendliest, but simple.
  633. (require 'info)
  634. (info-initialize)
  635. (push pkg-dir Info-directory-list))
  636. (push name package-activated-list)
  637. ;; Don't return nil.
  638. t)))
  639. (declare-function find-library-name "find-func" (library))
  640. (defun package--list-loaded-files (dir)
  641. "Recursively list all files in DIR which correspond to loaded features.
  642. Returns the `file-name-sans-extension' of each file, relative to
  643. DIR, sorted by most recently loaded last."
  644. (let* ((history (delq nil
  645. (mapcar (lambda (x)
  646. (let ((f (car x)))
  647. (and f (file-name-sans-extension f))))
  648. load-history)))
  649. (dir (file-truename dir))
  650. ;; List all files that have already been loaded.
  651. (list-of-conflicts
  652. (delq
  653. nil
  654. (mapcar
  655. (lambda (x) (let* ((file (file-relative-name x dir))
  656. ;; Previously loaded file, if any.
  657. (previous
  658. (ignore-errors
  659. (file-name-sans-extension
  660. (file-truename (find-library-name file)))))
  661. (pos (when previous (member previous history))))
  662. ;; Return (RELATIVE-FILENAME . HISTORY-POSITION)
  663. (when pos
  664. (cons (file-name-sans-extension file) (length pos)))))
  665. (directory-files-recursively dir "\\`[^\\.].*\\.el\\'")))))
  666. ;; Turn the list of (FILENAME . POS) back into a list of features. Files in
  667. ;; subdirectories are returned relative to DIR (so not actually features).
  668. (let ((default-directory (file-name-as-directory dir)))
  669. (mapcar (lambda (x) (file-truename (car x)))
  670. (sort list-of-conflicts
  671. ;; Sort the files by ascending HISTORY-POSITION.
  672. (lambda (x y) (< (cdr x) (cdr y))))))))
  673. ;;;; `package-activate'
  674. ;; This function activates a newer version of a package if an older
  675. ;; one was already activated. It also loads a features of this
  676. ;; package which were already loaded.
  677. (defun package-activate (package &optional force)
  678. "Activate the package named PACKAGE.
  679. If FORCE is true, (re-)activate it if it's already activated.
  680. Newer versions are always activated, regardless of FORCE."
  681. (let ((pkg-descs (cdr (assq package package-alist))))
  682. ;; Check if PACKAGE is available in `package-alist'.
  683. (while
  684. (when pkg-descs
  685. (let ((available-version (package-desc-version (car pkg-descs))))
  686. (or (package-disabled-p package available-version)
  687. ;; Prefer a builtin package.
  688. (package-built-in-p package available-version))))
  689. (setq pkg-descs (cdr pkg-descs)))
  690. (cond
  691. ;; If no such package is found, maybe it's built-in.
  692. ((null pkg-descs)
  693. (package-built-in-p package))
  694. ;; If the package is already activated, just return t.
  695. ((and (memq package package-activated-list) (not force))
  696. t)
  697. ;; Otherwise, proceed with activation.
  698. (t (package-activate-1 (car pkg-descs) nil 'deps)))))
  699. ;;; Installation -- Local operations
  700. ;; This section contains a variety of features regarding installing a
  701. ;; package to/from disk. This includes autoload generation,
  702. ;; unpacking, compiling, as well as defining a package from the
  703. ;; current buffer.
  704. ;;;; Unpacking
  705. (defvar tar-parse-info)
  706. (declare-function tar-untar-buffer "tar-mode" ())
  707. (declare-function tar-header-name "tar-mode" (tar-header) t)
  708. (declare-function tar-header-link-type "tar-mode" (tar-header) t)
  709. (defun package-untar-buffer (dir)
  710. "Untar the current buffer.
  711. This uses `tar-untar-buffer' from Tar mode. All files should
  712. untar into a directory named DIR; otherwise, signal an error."
  713. (require 'tar-mode)
  714. (tar-mode)
  715. ;; Make sure everything extracts into DIR.
  716. (let ((regexp (concat "\\`" (regexp-quote (expand-file-name dir)) "/"))
  717. (case-fold-search (file-name-case-insensitive-p dir)))
  718. (dolist (tar-data tar-parse-info)
  719. (let ((name (expand-file-name (tar-header-name tar-data))))
  720. (or (string-match regexp name)
  721. ;; Tarballs created by some utilities don't list
  722. ;; directories with a trailing slash (Bug#13136).
  723. (and (string-equal dir name)
  724. (eq (tar-header-link-type tar-data) 5))
  725. (error "Package does not untar cleanly into directory %s/" dir)))))
  726. (tar-untar-buffer))
  727. (defun package--alist-to-plist-args (alist)
  728. (mapcar 'macroexp-quote
  729. (apply #'nconc
  730. (mapcar (lambda (pair) (list (car pair) (cdr pair))) alist))))
  731. (defun package-unpack (pkg-desc)
  732. "Install the contents of the current buffer as a package."
  733. (let* ((name (package-desc-name pkg-desc))
  734. (dirname (package-desc-full-name pkg-desc))
  735. (pkg-dir (expand-file-name dirname package-user-dir)))
  736. (pcase (package-desc-kind pkg-desc)
  737. (`dir
  738. (make-directory pkg-dir t)
  739. (let ((file-list
  740. (directory-files
  741. default-directory 'full "\\`[^.].*\\.el\\'" 'nosort)))
  742. (dolist (source-file file-list)
  743. (let ((target-el-file
  744. (expand-file-name (file-name-nondirectory source-file) pkg-dir)))
  745. (copy-file source-file target-el-file t)))
  746. ;; Now that the files have been installed, this package is
  747. ;; indistinguishable from a `tar' or a `single'. Let's make
  748. ;; things simple by ensuring we're one of them.
  749. (setf (package-desc-kind pkg-desc)
  750. (if (> (length file-list) 1) 'tar 'single))))
  751. (`tar
  752. (make-directory package-user-dir t)
  753. ;; FIXME: should we delete PKG-DIR if it exists?
  754. (let* ((default-directory (file-name-as-directory package-user-dir)))
  755. (package-untar-buffer dirname)))
  756. (`single
  757. (let ((el-file (expand-file-name (format "%s.el" name) pkg-dir)))
  758. (make-directory pkg-dir t)
  759. (package--write-file-no-coding el-file)))
  760. (kind (error "Unknown package kind: %S" kind)))
  761. (package--make-autoloads-and-stuff pkg-desc pkg-dir)
  762. ;; Update package-alist.
  763. (let ((new-desc (package-load-descriptor pkg-dir)))
  764. (unless (equal (package-desc-full-name new-desc)
  765. (package-desc-full-name pkg-desc))
  766. (error "The retrieved package (`%s') doesn't match what the archive offered (`%s')"
  767. (package-desc-full-name new-desc) (package-desc-full-name pkg-desc)))
  768. ;; Activation has to be done before compilation, so that if we're
  769. ;; upgrading and macros have changed we load the new definitions
  770. ;; before compiling.
  771. (when (package-activate-1 new-desc :reload :deps)
  772. ;; FIXME: Compilation should be done as a separate, optional, step.
  773. ;; E.g. for multi-package installs, we should first install all packages
  774. ;; and then compile them.
  775. (package--compile new-desc)
  776. ;; After compilation, load again any files loaded by
  777. ;; `activate-1', so that we use the byte-compiled definitions.
  778. (package--load-files-for-activation new-desc :reload)))
  779. pkg-dir))
  780. (defun package-generate-description-file (pkg-desc pkg-file)
  781. "Create the foo-pkg.el file for single-file packages."
  782. (let* ((name (package-desc-name pkg-desc)))
  783. (let ((print-level nil)
  784. (print-quoted t)
  785. (print-length nil))
  786. (write-region
  787. (concat
  788. ";;; -*- no-byte-compile: t -*-\n"
  789. (prin1-to-string
  790. (nconc
  791. (list 'define-package
  792. (symbol-name name)
  793. (package-version-join (package-desc-version pkg-desc))
  794. (package-desc-summary pkg-desc)
  795. (let ((requires (package-desc-reqs pkg-desc)))
  796. (list 'quote
  797. ;; Turn version lists into string form.
  798. (mapcar
  799. (lambda (elt)
  800. (list (car elt)
  801. (package-version-join (cadr elt))))
  802. requires))))
  803. (package--alist-to-plist-args
  804. (package-desc-extras pkg-desc))))
  805. "\n")
  806. nil pkg-file nil 'silent))))
  807. ;;;; Autoload
  808. (declare-function autoload-rubric "autoload" (file &optional type feature))
  809. (defun package-autoload-ensure-default-file (file)
  810. "Make sure that the autoload file FILE exists and if not create it."
  811. (unless (file-exists-p file)
  812. (require 'autoload)
  813. (write-region (autoload-rubric file "package" nil) nil file nil 'silent))
  814. file)
  815. (defvar generated-autoload-file)
  816. (defvar autoload-timestamps)
  817. (defvar version-control)
  818. (defun package-generate-autoloads (name pkg-dir)
  819. (let* ((auto-name (format "%s-autoloads.el" name))
  820. ;;(ignore-name (concat name "-pkg.el"))
  821. (generated-autoload-file (expand-file-name auto-name pkg-dir))
  822. ;; We don't need 'em, and this makes the output reproducible.
  823. (autoload-timestamps nil)
  824. ;; Silence `autoload-generate-file-autoloads'.
  825. (noninteractive inhibit-message)
  826. (backup-inhibited t)
  827. (version-control 'never))
  828. (package-autoload-ensure-default-file generated-autoload-file)
  829. (update-directory-autoloads pkg-dir)
  830. (let ((buf (find-buffer-visiting generated-autoload-file)))
  831. (when buf (kill-buffer buf)))
  832. auto-name))
  833. (defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
  834. "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
  835. (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
  836. (let ((desc-file (expand-file-name (package--description-file pkg-dir)
  837. pkg-dir)))
  838. (unless (file-exists-p desc-file)
  839. (package-generate-description-file pkg-desc desc-file)))
  840. ;; FIXME: Create foo.info and dir file from foo.texi?
  841. )
  842. ;;;; Compilation
  843. (defvar warning-minimum-level)
  844. (defun package--compile (pkg-desc)
  845. "Byte-compile installed package PKG-DESC.
  846. This assumes that `pkg-desc' has already been activated with
  847. `package-activate-1'."
  848. (let ((warning-minimum-level :error)
  849. (save-silently inhibit-message)
  850. (load-path load-path))
  851. (byte-recompile-directory (package-desc-dir pkg-desc) 0 t)))
  852. ;;;; Inferring package from current buffer
  853. (defun package-read-from-string (str)
  854. "Read a Lisp expression from STR.
  855. Signal an error if the entire string was not used."
  856. (let* ((read-data (read-from-string str))
  857. (more-left
  858. (condition-case nil
  859. ;; The call to `ignore' suppresses a compiler warning.
  860. (progn (ignore (read-from-string
  861. (substring str (cdr read-data))))
  862. t)
  863. (end-of-file nil))))
  864. (if more-left
  865. (error "Can't read whole string")
  866. (car read-data))))
  867. (defun package--prepare-dependencies (deps)
  868. "Turn DEPS into an acceptable list of dependencies.
  869. Any parts missing a version string get a default version string
  870. of \"0\" (meaning any version) and an appropriate level of lists
  871. is wrapped around any parts requiring it."
  872. (cond
  873. ((not (listp deps))
  874. (error "Invalid requirement specifier: %S" deps))
  875. (t (mapcar (lambda (dep)
  876. (cond
  877. ((symbolp dep) `(,dep "0"))
  878. ((stringp dep)
  879. (error "Invalid requirement specifier: %S" dep))
  880. ((and (listp dep) (null (cdr dep)))
  881. (list (car dep) "0"))
  882. (t dep)))
  883. deps))))
  884. (declare-function lm-header "lisp-mnt" (header))
  885. (declare-function lm-homepage "lisp-mnt" (&optional file))
  886. (declare-function lm-maintainer "lisp-mnt" (&optional file))
  887. (declare-function lm-authors "lisp-mnt" (&optional file))
  888. (defun package-buffer-info ()
  889. "Return a `package-desc' describing the package in the current buffer.
  890. If the buffer does not contain a conforming package, signal an
  891. error. If there is a package, narrow the buffer to the file's
  892. boundaries."
  893. (goto-char (point-min))
  894. (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
  895. (error "Package lacks a file header"))
  896. (let ((file-name (match-string-no-properties 1))
  897. (desc (match-string-no-properties 2))
  898. (start (line-beginning-position)))
  899. (unless (search-forward (concat ";;; " file-name ".el ends here"))
  900. (error "Package lacks a terminating comment"))
  901. ;; Try to include a trailing newline.
  902. (forward-line)
  903. (narrow-to-region start (point))
  904. (require 'lisp-mnt)
  905. ;; Use some headers we've invented to drive the process.
  906. (let* ((requires-str (lm-header "package-requires"))
  907. ;; Prefer Package-Version; if defined, the package author
  908. ;; probably wants us to use it. Otherwise try Version.
  909. (pkg-version
  910. (or (package-strip-rcs-id (lm-header "package-version"))
  911. (package-strip-rcs-id (lm-header "version"))))
  912. (homepage (lm-homepage)))
  913. (unless pkg-version
  914. (error
  915. "Package lacks a \"Version\" or \"Package-Version\" header"))
  916. (package-desc-from-define
  917. file-name pkg-version desc
  918. (if requires-str
  919. (package--prepare-dependencies
  920. (package-read-from-string requires-str)))
  921. :kind 'single
  922. :url homepage
  923. :maintainer (lm-maintainer)
  924. :authors (lm-authors)))))
  925. (defun package--read-pkg-desc (kind)
  926. "Read a `define-package' form in current buffer.
  927. Return the pkg-desc, with desc-kind set to KIND."
  928. (goto-char (point-min))
  929. (unwind-protect
  930. (let* ((pkg-def-parsed (read (current-buffer)))
  931. (pkg-desc
  932. (when (eq (car pkg-def-parsed) 'define-package)
  933. (apply #'package-desc-from-define
  934. (append (cdr pkg-def-parsed))))))
  935. (when pkg-desc
  936. (setf (package-desc-kind pkg-desc) kind)
  937. pkg-desc))))
  938. (declare-function tar-get-file-descriptor "tar-mode" (file))
  939. (declare-function tar--extract "tar-mode" (descriptor))
  940. (defun package-tar-file-info ()
  941. "Find package information for a tar file.
  942. The return result is a `package-desc'."
  943. (cl-assert (derived-mode-p 'tar-mode))
  944. (let* ((dir-name (file-name-directory
  945. (tar-header-name (car tar-parse-info))))
  946. (desc-file (package--description-file dir-name))
  947. (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
  948. (unless tar-desc
  949. (error "No package descriptor file found"))
  950. (with-current-buffer (tar--extract tar-desc)
  951. (unwind-protect
  952. (or (package--read-pkg-desc 'tar)
  953. (error "Can't find define-package in %s"
  954. (tar-header-name tar-desc)))
  955. (kill-buffer (current-buffer))))))
  956. (defun package-dir-info ()
  957. "Find package information for a directory.
  958. The return result is a `package-desc'."
  959. (cl-assert (derived-mode-p 'dired-mode))
  960. (let* ((desc-file (package--description-file default-directory)))
  961. (if (file-readable-p desc-file)
  962. (with-temp-buffer
  963. (insert-file-contents desc-file)
  964. (package--read-pkg-desc 'dir))
  965. (let ((files (directory-files default-directory t "\\.el\\'" t))
  966. info)
  967. (while files
  968. (with-temp-buffer
  969. (insert-file-contents (pop files))
  970. ;; When we find the file with the data,
  971. (when (setq info (ignore-errors (package-buffer-info)))
  972. ;; stop looping,
  973. (setq files nil)
  974. ;; set the 'dir kind,
  975. (setf (package-desc-kind info) 'dir))))
  976. (unless info
  977. (error "No .el files with package headers in `%s'" default-directory))
  978. ;; and return the info.
  979. info))))
  980. ;;; Communicating with Archives
  981. ;; Set of low-level functions for communicating with archives and
  982. ;; signature checking.
  983. (defun package--write-file-no-coding (file-name)
  984. (let ((buffer-file-coding-system 'no-conversion))
  985. (write-region (point-min) (point-max) file-name nil 'silent)))
  986. (declare-function url-http-file-exists-p "url-http" (url))
  987. (defun package--archive-file-exists-p (location file)
  988. (let ((http (string-match "\\`https?:" location)))
  989. (if http
  990. (progn
  991. (require 'url-http)
  992. (url-http-file-exists-p (concat location file)))
  993. (file-exists-p (expand-file-name file location)))))
  994. (declare-function epg-make-context "epg"
  995. (&optional protocol armor textmode include-certs
  996. cipher-algorithm
  997. digest-algorithm
  998. compress-algorithm))
  999. (declare-function epg-verify-string "epg" (context signature
  1000. &optional signed-text))
  1001. (declare-function epg-context-result-for "epg" (context name))
  1002. (declare-function epg-signature-status "epg" (signature) t)
  1003. (declare-function epg-signature-to-string "epg" (signature))
  1004. (defun package--display-verify-error (context sig-file)
  1005. (unless (equal (epg-context-error-output context) "")
  1006. (with-output-to-temp-buffer "*Error*"
  1007. (with-current-buffer standard-output
  1008. (if (epg-context-result-for context 'verify)
  1009. (insert (format "Failed to verify signature %s:\n" sig-file)
  1010. (mapconcat #'epg-signature-to-string
  1011. (epg-context-result-for context 'verify)
  1012. "\n"))
  1013. (insert (format "Error while verifying signature %s:\n" sig-file)))
  1014. (insert "\nCommand output:\n" (epg-context-error-output context))))))
  1015. (defmacro package--with-work-buffer (location file &rest body)
  1016. "Run BODY in a buffer containing the contents of FILE at LOCATION.
  1017. LOCATION is the base location of a package archive, and should be
  1018. one of the URLs (or file names) specified in `package-archives'.
  1019. FILE is the name of a file relative to that base location.
  1020. This macro retrieves FILE from LOCATION into a temporary buffer,
  1021. and evaluates BODY while that buffer is current. This work
  1022. buffer is killed afterwards. Return the last value in BODY."
  1023. (declare (indent 2) (debug t)
  1024. (obsolete package--with-response-buffer "25.1"))
  1025. `(with-temp-buffer
  1026. (if (string-match-p "\\`https?:" ,location)
  1027. (url-insert-file-contents (concat ,location ,file))
  1028. (unless (file-name-absolute-p ,location)
  1029. (error "Archive location %s is not an absolute file name"
  1030. ,location))
  1031. (insert-file-contents (expand-file-name ,file ,location)))
  1032. ,@body))
  1033. (cl-defmacro package--with-response-buffer (url &rest body &key async file error-form noerror &allow-other-keys)
  1034. "Access URL and run BODY in a buffer containing the response.
  1035. Point is after the headers when BODY runs.
  1036. FILE, if provided, is added to URL.
  1037. URL can be a local file name, which must be absolute.
  1038. ASYNC, if non-nil, runs the request asynchronously.
  1039. ERROR-FORM is run only if a connection error occurs. If NOERROR
  1040. is non-nil, don't propagate connection errors (does not apply to
  1041. errors signaled by ERROR-FORM or by BODY).
  1042. \(fn URL &key ASYNC FILE ERROR-FORM NOERROR &rest BODY)"
  1043. (declare (indent defun) (debug t))
  1044. (while (keywordp (car body))
  1045. (setq body (cdr (cdr body))))
  1046. (macroexp-let2* nil ((url-1 url)
  1047. (noerror-1 noerror))
  1048. (let ((url-sym (make-symbol "url"))
  1049. (b-sym (make-symbol "b-sym")))
  1050. `(cl-macrolet ((unless-error (body-2 &rest before-body)
  1051. (let ((err (make-symbol "err")))
  1052. `(with-temp-buffer
  1053. (when (condition-case ,err
  1054. (progn ,@before-body t)
  1055. ,(list 'error ',error-form
  1056. (list 'unless ',noerror-1
  1057. `(signal (car ,err) (cdr ,err)))))
  1058. ,@body-2)))))
  1059. (if (string-match-p "\\`https?:" ,url-1)
  1060. (let ((,url-sym (concat ,url-1 ,file)))
  1061. (if ,async
  1062. (unless-error nil
  1063. (url-retrieve ,url-sym
  1064. (lambda (status)
  1065. (let ((,b-sym (current-buffer)))
  1066. (require 'url-handlers)
  1067. (unless-error ,body
  1068. (when-let ((er (plist-get status :error)))
  1069. (error "Error retrieving: %s %S" ,url-sym er))
  1070. (with-current-buffer ,b-sym
  1071. (goto-char (point-min))
  1072. (unless (search-forward-regexp "^\r?\n\r?" nil 'noerror)
  1073. (error "Error retrieving: %s %S" ,url-sym "incomprehensible buffer")))
  1074. (url-insert-buffer-contents ,b-sym ,url-sym)
  1075. (kill-buffer ,b-sym)
  1076. (goto-char (point-min)))))
  1077. nil
  1078. 'silent))
  1079. (unless-error ,body (url-insert-file-contents ,url-sym))))
  1080. (unless-error ,body
  1081. (let ((url (expand-file-name ,file ,url-1)))
  1082. (unless (file-name-absolute-p url)
  1083. (error "Location %s is not a url nor an absolute file name" url))
  1084. (insert-file-contents url))))))))
  1085. (define-error 'bad-signature "Failed to verify signature")
  1086. (defun package--check-signature-content (content string &optional sig-file)
  1087. "Check signature CONTENT against STRING.
  1088. SIG-FILE is the name of the signature file, used when signaling
  1089. errors."
  1090. (let ((context (epg-make-context 'OpenPGP)))
  1091. (when package-gnupghome-dir
  1092. (setf (epg-context-home-directory context) package-gnupghome-dir))
  1093. (condition-case error
  1094. (epg-verify-string context content string)
  1095. (error (package--display-verify-error context sig-file)
  1096. (signal 'bad-signature error)))
  1097. (let (good-signatures had-fatal-error)
  1098. ;; The .sig file may contain multiple signatures. Success if one
  1099. ;; of the signatures is good.
  1100. (dolist (sig (epg-context-result-for context 'verify))
  1101. (if (eq (epg-signature-status sig) 'good)
  1102. (push sig good-signatures)
  1103. ;; If package-check-signature is allow-unsigned, don't
  1104. ;; signal error when we can't verify signature because of
  1105. ;; missing public key. Other errors are still treated as
  1106. ;; fatal (bug#17625).
  1107. (unless (and (eq package-check-signature 'allow-unsigned)
  1108. (eq (epg-signature-status sig) 'no-pubkey))
  1109. (setq had-fatal-error t))))
  1110. (when (or (null good-signatures) had-fatal-error)
  1111. (package--display-verify-error context sig-file)
  1112. (signal 'bad-signature (list sig-file)))
  1113. good-signatures)))
  1114. (defun package--check-signature (location file &optional string async callback unwind)
  1115. "Check signature of the current buffer.
  1116. Download the signature file from LOCATION by appending \".sig\"
  1117. to FILE.
  1118. GnuPG keyring location depends on `package-gnupghome-dir'.
  1119. STRING is the string to verify, it defaults to `buffer-string'.
  1120. If ASYNC is non-nil, the download of the signature file is
  1121. done asynchronously.
  1122. If the signature does not verify, signal an error.
  1123. If the signature is verified and CALLBACK was provided, `funcall'
  1124. CALLBACK with the list of good signatures as argument (the list
  1125. can be empty).
  1126. If no signatures file is found, and `package-check-signature' is
  1127. `allow-unsigned', call CALLBACK with a nil argument.
  1128. Otherwise, an error is signaled.
  1129. UNWIND, if provided, is a function to be called after everything
  1130. else, even if an error is signaled."
  1131. (let ((sig-file (concat file ".sig"))
  1132. (string (or string (buffer-string))))
  1133. (package--with-response-buffer location :file sig-file
  1134. :async async :noerror t
  1135. ;; Connection error is assumed to mean "no sig-file".
  1136. :error-form (let ((allow-unsigned (eq package-check-signature 'allow-unsigned)))
  1137. (when (and callback allow-unsigned)
  1138. (funcall callback nil))
  1139. (when unwind (funcall unwind))
  1140. (unless allow-unsigned
  1141. (error "Unsigned file `%s' at %s" file location)))
  1142. ;; OTOH, an error here means "bad signature", which we never
  1143. ;; suppress. (Bug#22089)
  1144. (unwind-protect
  1145. (let ((sig (package--check-signature-content (buffer-substring (point) (point-max))
  1146. string sig-file)))
  1147. (when callback (funcall callback sig))
  1148. sig)
  1149. (when unwind (funcall unwind))))))
  1150. ;;; Packages on Archives
  1151. ;; The following variables store information about packages available
  1152. ;; from archives. The most important of these is
  1153. ;; `package-archive-contents' which is initially populated by the
  1154. ;; function `package-read-all-archive-contents' from a cache on disk.
  1155. ;; The `package-initialize' command is also closely related to this
  1156. ;; section, but it has its own section.
  1157. (defconst package-archive-version 1
  1158. "Version number of the package archive understood by this file.
  1159. Lower version numbers than this will probably be understood as well.")
  1160. ;; We don't prime the cache since it tends to get out of date.
  1161. (defvar package-archive-contents nil
  1162. "Cache of the contents of the Emacs Lisp Package Archive.
  1163. This is an alist mapping package names (symbols) to
  1164. non-empty lists of `package-desc' structures.")
  1165. (put 'package-archive-contents 'risky-local-variable t)
  1166. (defvar package--compatibility-table nil
  1167. "Hash table connecting package names to their compatibility.
  1168. Each key is a symbol, the name of a package.
  1169. The value is either nil, representing an incompatible package, or
  1170. a version list, representing the highest compatible version of
  1171. that package which is available.
  1172. A package is considered incompatible if it requires an Emacs
  1173. version higher than the one being used. To check for package
  1174. \(in)compatibility, don't read this table directly, use
  1175. `package--incompatible-p' which also checks dependencies.")
  1176. (defun package--build-compatibility-table ()
  1177. "Build `package--compatibility-table' with `package--mapc'."
  1178. ;; Initialize the list of built-ins.
  1179. (require 'finder-inf nil t)
  1180. ;; Build compat table.
  1181. (setq package--compatibility-table (make-hash-table :test 'eq))
  1182. (package--mapc #'package--add-to-compatibility-table))
  1183. (defun package--add-to-compatibility-table (pkg)
  1184. "If PKG is compatible (without dependencies), add to the compatibility table.
  1185. PKG is a package-desc object.
  1186. Only adds if its version is higher than what's already stored in
  1187. the table."
  1188. (unless (package--incompatible-p pkg 'shallow)
  1189. (let* ((name (package-desc-name pkg))
  1190. (version (or (package-desc-version pkg) '(0)))
  1191. (table-version (gethash name package--compatibility-table)))
  1192. (when (or (not table-version)
  1193. (version-list-< table-version version))
  1194. (puthash name version package--compatibility-table)))))
  1195. ;; Package descriptor objects used inside the "archive-contents" file.
  1196. ;; Changing this defstruct implies changing the format of the
  1197. ;; "archive-contents" files.
  1198. (cl-defstruct (package--ac-desc
  1199. (:constructor package-make-ac-desc (version reqs summary kind extras))
  1200. (:copier nil)
  1201. (:type vector))
  1202. version reqs summary kind extras)
  1203. (defun package--append-to-alist (pkg-desc alist)
  1204. "Append an entry for PKG-DESC to the start of ALIST and return it.
  1205. This entry takes the form (`package-desc-name' PKG-DESC).
  1206. If ALIST already has an entry with this name, destructively add
  1207. PKG-DESC to the cdr of this entry instead, sorted by version
  1208. number."
  1209. (let* ((name (package-desc-name pkg-desc))
  1210. (priority-version (package-desc-priority-version pkg-desc))
  1211. (existing-packages (assq name alist)))
  1212. (if (not existing-packages)
  1213. (cons (list name pkg-desc)
  1214. alist)
  1215. (while (if (and (cdr existing-packages)
  1216. (version-list-< priority-version
  1217. (package-desc-priority-version
  1218. (cadr existing-packages))))
  1219. (setq existing-packages (cdr existing-packages))
  1220. (push pkg-desc (cdr existing-packages))
  1221. nil))
  1222. alist)))
  1223. (defun package--add-to-archive-contents (package archive)
  1224. "Add the PACKAGE from the given ARCHIVE if necessary.
  1225. PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
  1226. Also, add the originating archive to the `package-desc' structure."
  1227. (let* ((name (car package))
  1228. (version (package--ac-desc-version (cdr package)))
  1229. (pkg-desc
  1230. (package-desc-create
  1231. :name name
  1232. :version version
  1233. :reqs (package--ac-desc-reqs (cdr package))
  1234. :summary (package--ac-desc-summary (cdr package))
  1235. :kind (package--ac-desc-kind (cdr package))
  1236. :archive archive
  1237. :extras (and (> (length (cdr package)) 4)
  1238. ;; Older archive-contents files have only 4
  1239. ;; elements here.
  1240. (package--ac-desc-extras (cdr package)))))
  1241. (pinned-to-archive (assoc name package-pinned-packages)))
  1242. ;; Skip entirely if pinned to another archive.
  1243. (when (not (and pinned-to-archive
  1244. (not (equal (cdr pinned-to-archive) archive))))
  1245. (setq package-archive-contents
  1246. (package--append-to-alist pkg-desc package-archive-contents)))))
  1247. (defun package--read-archive-file (file)
  1248. "Re-read archive file FILE, if it exists.
  1249. Will return the data from the file, or nil if the file does not exist.
  1250. Will throw an error if the archive version is too new."
  1251. (let ((filename (expand-file-name file package-user-dir)))
  1252. (when (file-exists-p filename)
  1253. (with-temp-buffer
  1254. (let ((coding-system-for-read 'utf-8))
  1255. (insert-file-contents filename))
  1256. (let ((contents (read (current-buffer))))
  1257. (if (> (car contents) package-archive-version)
  1258. (error "Package archive version %d is higher than %d"
  1259. (car contents) package-archive-version))
  1260. (cdr contents))))))
  1261. (defun package-read-archive-contents (archive)
  1262. "Re-read archive contents for ARCHIVE.
  1263. If successful, set the variable `package-archive-contents'.
  1264. If the archive version is too new, signal an error."
  1265. ;; Version 1 of 'archive-contents' is identical to our internal
  1266. ;; representation.
  1267. (let* ((contents-file (format "archives/%s/archive-contents" archive))
  1268. (contents (package--read-archive-file contents-file)))
  1269. (when contents
  1270. (dolist (package contents)
  1271. (package--add-to-archive-contents package archive)))))
  1272. (defvar package--old-archive-priorities nil
  1273. "Store currently used `package-archive-priorities'.
  1274. This is the value of `package-archive-priorities' last time
  1275. `package-read-all-archive-contents' was called. It can be used
  1276. by arbitrary functions to decide whether it is necessary to call
  1277. it again.")
  1278. (defun package-read-all-archive-contents ()
  1279. "Re-read `archive-contents', if it exists.
  1280. If successful, set `package-archive-contents'."
  1281. (setq package-archive-contents nil)
  1282. (setq package--old-archive-priorities package-archive-priorities)
  1283. (dolist (archive package-archives)
  1284. (package-read-archive-contents (car archive))))
  1285. ;;;; Package Initialize
  1286. ;; A bit of a milestone. This brings together some of the above
  1287. ;; sections and populates all relevant lists of packages from contents
  1288. ;; available on disk.
  1289. (defvar package--initialized nil)
  1290. (defvar package--init-file-ensured nil
  1291. "Whether we know the init file has package-initialize.")
  1292. ;;;###autoload
  1293. (defun package-initialize (&optional no-activate)
  1294. "Load Emacs Lisp packages, and activate them.
  1295. The variable `package-load-list' controls which packages to load.
  1296. If optional arg NO-ACTIVATE is non-nil, don't activate packages.
  1297. If `user-init-file' does not mention `(package-initialize)', add
  1298. it to the file.
  1299. If called as part of loading `user-init-file', set
  1300. `package-enable-at-startup' to nil, to prevent accidentally
  1301. loading packages twice.
  1302. It is not necessary to adjust `load-path' or `require' the
  1303. individual packages after calling `package-initialize' -- this is
  1304. taken care of by `package-initialize'."
  1305. (interactive)
  1306. (setq package-alist nil)
  1307. (if after-init-time
  1308. (package--ensure-init-file)
  1309. ;; If `package-initialize' is before we finished loading the init
  1310. ;; file, it's obvious we don't need to ensure-init.
  1311. (setq package--init-file-ensured t
  1312. ;; And likely we don't need to run it again after init.
  1313. package-enable-at-startup nil))
  1314. (package-load-all-descriptors)
  1315. (package-read-all-archive-contents)
  1316. (unless no-activate
  1317. (dolist (elt package-alist)
  1318. (condition-case err
  1319. (package-activate (car elt))
  1320. ;; Don't let failure of activation of a package arbitrarily stop
  1321. ;; activation of further packages.
  1322. (error (message "%s" (error-message-string err))))))
  1323. (setq package--initialized t)
  1324. ;; This uses `package--mapc' so it must be called after
  1325. ;; `package--initialized' is t.
  1326. (package--build-compatibility-table))
  1327. ;;;; Populating `package-archive-contents' from archives
  1328. ;; This subsection populates the variables listed above from the
  1329. ;; actual archives, instead of from a local cache.
  1330. (defvar package--downloads-in-progress nil
  1331. "List of in-progress asynchronous downloads.")
  1332. (declare-function epg-import-keys-from-file "epg" (context keys))
  1333. ;;;###autoload
  1334. (defun package-import-keyring (&optional file)
  1335. "Import keys from FILE."
  1336. (interactive "fFile: ")
  1337. (setq file (expand-file-name file))
  1338. (let ((context (epg-make-context 'OpenPGP)))
  1339. (when package-gnupghome-dir
  1340. (with-file-modes 448
  1341. (make-directory package-gnupghome-dir t))
  1342. (setf (epg-context-home-directory context) package-gnupghome-dir))
  1343. (message "Importing %s..." (file-name-nondirectory file))
  1344. (epg-import-keys-from-file context file)
  1345. (message "Importing %s...done" (file-name-nondirectory file))))
  1346. (defvar package--post-download-archives-hook nil
  1347. "Hook run after the archive contents are downloaded.
  1348. Don't run this hook directly. It is meant to be run as part of
  1349. `package--update-downloads-in-progress'.")
  1350. (put 'package--post-download-archives-hook 'risky-local-variable t)
  1351. (defun package--update-downloads-in-progress (entry)
  1352. "Remove ENTRY from `package--downloads-in-progress'.
  1353. Once it's empty, run `package--post-download-archives-hook'."
  1354. ;; Keep track of the downloading progress.
  1355. (setq package--downloads-in-progress
  1356. (remove entry package--downloads-in-progress))
  1357. ;; If this was the last download, run the hook.
  1358. (unless package--downloads-in-progress
  1359. (package-read-all-archive-contents)
  1360. (package--build-compatibility-table)
  1361. ;; We message before running the hook, so the hook can give
  1362. ;; messages as well.
  1363. (message "Package refresh done")
  1364. (run-hooks 'package--post-download-archives-hook)))
  1365. (defun package--download-one-archive (archive file &optional async)
  1366. "Retrieve an archive file FILE from ARCHIVE, and cache it.
  1367. ARCHIVE should be a cons cell of the form (NAME . LOCATION),
  1368. similar to an entry in `package-alist'. Save the cached copy to
  1369. \"archives/NAME/FILE\" in `package-user-dir'."
  1370. (package--with-response-buffer (cdr archive) :file file
  1371. :async async
  1372. :error-form (package--update-downloads-in-progress archive)
  1373. (let* ((location (cdr archive))
  1374. (name (car archive))
  1375. (content (buffer-string))
  1376. (dir (expand-file-name (format "archives/%s" name) package-user-dir))
  1377. (local-file (expand-file-name file dir)))
  1378. (when (listp (read-from-string content))
  1379. (make-directory dir t)
  1380. (if (or (not package-check-signature)
  1381. (member name package-unsigned-archives))
  1382. ;; If we don't care about the signature, save the file and
  1383. ;; we're done.
  1384. (progn (write-region content nil local-file nil 'silent)
  1385. (package--update-downloads-in-progress archive))
  1386. ;; If we care, check it (perhaps async) and *then* write the file.
  1387. (package--check-signature
  1388. location file content async
  1389. ;; This function will be called after signature checking.
  1390. (lambda (&optional good-sigs)
  1391. (write-region content nil local-file nil 'silent)
  1392. ;; Write out good signatures into archive-contents.signed file.
  1393. (when good-sigs
  1394. (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
  1395. nil (concat local-file ".signed") nil 'silent)))
  1396. (lambda () (package--update-downloads-in-progress archive))))))))
  1397. (defun package--download-and-read-archives (&optional async)
  1398. "Download descriptions of all `package-archives' and read them.
  1399. This populates `package-archive-contents'. If ASYNC is non-nil,
  1400. perform the downloads asynchronously."
  1401. ;; The downloaded archive contents will be read as part of
  1402. ;; `package--update-downloads-in-progress'.
  1403. (dolist (archive package-archives)
  1404. (cl-pushnew archive package--downloads-in-progress
  1405. :test #'equal))
  1406. (dolist (archive package-archives)
  1407. (condition-case-unless-debug nil
  1408. (package--download-one-archive archive "archive-contents" async)
  1409. (error (message "Failed to download `%s' archive."
  1410. (car archive))))))
  1411. ;;;###autoload
  1412. (defun package-refresh-contents (&optional async)
  1413. "Download descriptions of all configured ELPA packages.
  1414. For each archive configured in the variable `package-archives',
  1415. inform Emacs about the latest versions of all packages it offers,
  1416. and make them available for download.
  1417. Optional argument ASYNC specifies whether to perform the
  1418. downloads in the background."
  1419. (interactive)
  1420. (unless (file-exists-p package-user-dir)
  1421. (make-directory package-user-dir t))
  1422. (let ((default-keyring (expand-file-name "package-keyring.gpg"
  1423. data-directory))
  1424. (inhibit-message async))
  1425. (when (and package-check-signature (file-exists-p default-keyring))
  1426. (condition-case-unless-debug error
  1427. (package-import-keyring default-keyring)
  1428. (error (message "Cannot import default keyring: %S" (cdr error))))))
  1429. (package--download-and-read-archives async))
  1430. ;;; Dependency Management
  1431. ;; Calculating the full transaction necessary for an installation,
  1432. ;; keeping track of which packages were installed strictly as
  1433. ;; dependencies, and determining which packages cannot be removed
  1434. ;; because they are dependencies.
  1435. (defun package-compute-transaction (packages requirements &optional seen)
  1436. "Return a list of packages to be installed, including PACKAGES.
  1437. PACKAGES should be a list of `package-desc'.
  1438. REQUIREMENTS should be a list of additional requirements; each
  1439. element in this list should have the form (PACKAGE VERSION-LIST),
  1440. where PACKAGE is a package name and VERSION-LIST is the required
  1441. version of that package.
  1442. This function recursively computes the requirements of the
  1443. packages in REQUIREMENTS, and returns a list of all the packages
  1444. that must be installed. Packages that are already installed are
  1445. not included in this list.
  1446. SEEN is used internally to detect infinite recursion."
  1447. ;; FIXME: We really should use backtracking to explore the whole
  1448. ;; search space (e.g. if foo require bar-1.3, and bar-1.4 requires toto-1.1
  1449. ;; whereas bar-1.3 requires toto-1.0 and the user has put a hold on toto-1.0:
  1450. ;; the current code might fail to see that it could install foo by using the
  1451. ;; older bar-1.3).
  1452. (dolist (elt requirements)
  1453. (let* ((next-pkg (car elt))
  1454. (next-version (cadr elt))
  1455. (already ()))
  1456. (dolist (pkg packages)
  1457. (if (eq next-pkg (package-desc-name pkg))
  1458. (setq already pkg)))
  1459. (when already
  1460. (if (version-list-<= next-version (package-desc-version already))
  1461. ;; `next-pkg' is already in `packages', but its position there
  1462. ;; means it might be installed too late: remove it from there, so
  1463. ;; we re-add it (along with its dependencies) at an earlier place
  1464. ;; below (bug#16994).
  1465. (if (memq already seen) ;Avoid inf-loop on dependency cycles.
  1466. (message "Dependency cycle going through %S"
  1467. (package-desc-full-name already))
  1468. (setq packages (delq already packages))
  1469. (setq already nil))
  1470. (error "Need package `%s-%s', but only %s is being installed"
  1471. next-pkg (package-version-join next-version)
  1472. (package-version-join (package-desc-version already)))))
  1473. (cond
  1474. (already nil)
  1475. ((package-installed-p next-pkg next-version) nil)
  1476. (t
  1477. ;; A package is required, but not installed. It might also be
  1478. ;; blocked via `package-load-list'.
  1479. (let ((pkg-descs (cdr (assq next-pkg package-archive-contents)))
  1480. (found nil)
  1481. (found-something nil)
  1482. (problem nil))
  1483. (while (and pkg-descs (not found))
  1484. (let* ((pkg-desc (pop pkg-descs))
  1485. (version (package-desc-version pkg-desc))
  1486. (disabled (package-disabled-p next-pkg version)))
  1487. (cond
  1488. ((version-list-< version next-version)
  1489. ;; pkg-descs is sorted by priority, not version, so
  1490. ;; don't error just yet.
  1491. (unless found-something
  1492. (setq found-something (package-version-join version))))
  1493. (disabled
  1494. (unless problem
  1495. (setq problem
  1496. (if (stringp disabled)
  1497. (format-message
  1498. "Package `%s' held at version %s, but version %s required"
  1499. next-pkg disabled
  1500. (package-version-join next-version))
  1501. (format-message "Required package `%s' is disabled"
  1502. next-pkg)))))
  1503. (t (setq found pkg-desc)))))
  1504. (unless found
  1505. (cond
  1506. (problem (error "%s" problem))
  1507. (found-something
  1508. (error "Need package `%s-%s', but only %s is available"
  1509. next-pkg (package-version-join next-version)
  1510. found-something))
  1511. (t (error "Package `%s-%s' is unavailable"
  1512. next-pkg (package-version-join next-version)))))
  1513. (setq packages
  1514. (package-compute-transaction (cons found packages)
  1515. (package-desc-reqs found)
  1516. (cons found seen))))))))
  1517. packages)
  1518. (defun package--find-non-dependencies ()
  1519. "Return a list of installed packages which are not dependencies.
  1520. Finds all packages in `package-alist' which are not dependencies
  1521. of any other packages.
  1522. Used to populate `package-selected-packages'."
  1523. (let ((dep-list
  1524. (delete-dups
  1525. (apply #'append
  1526. (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
  1527. package-alist)))))
  1528. (cl-loop for p in package-alist
  1529. for name = (car p)
  1530. unless (memq name dep-list)
  1531. collect name)))
  1532. (defun package--save-selected-packages (&optional value)
  1533. "Set and save `package-selected-packages' to VALUE."
  1534. (when value
  1535. (setq package-selected-packages value))
  1536. (if after-init-time
  1537. (let ((save-silently inhibit-message))
  1538. (customize-save-variable 'package-selected-packages package-selected-packages))
  1539. (add-hook 'after-init-hook #'package--save-selected-packages)))
  1540. (defun package--user-selected-p (pkg)
  1541. "Return non-nil if PKG is a package was installed by the user.
  1542. PKG is a package name.
  1543. This looks into `package-selected-packages', populating it first
  1544. if it is still empty."
  1545. (unless (consp package-selected-packages)
  1546. (package--save-selected-packages (package--find-non-dependencies)))
  1547. (memq pkg package-selected-packages))
  1548. (defun package--get-deps (pkg &optional only)
  1549. (let* ((pkg-desc (cadr (assq pkg package-alist)))
  1550. (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
  1551. for name = (car p)
  1552. when (assq name package-alist)
  1553. collect name))
  1554. (indirect-deps (unless (eq only 'direct)
  1555. (delete-dups
  1556. (cl-loop for p in direct-deps
  1557. append (package--get-deps p))))))
  1558. (cl-case only
  1559. (direct direct-deps)
  1560. (separate (list direct-deps indirect-deps))
  1561. (indirect indirect-deps)
  1562. (t (delete-dups (append direct-deps indirect-deps))))))
  1563. (defun package--removable-packages ()
  1564. "Return a list of names of packages no longer needed.
  1565. These are packages which are neither contained in
  1566. `package-selected-packages' nor a dependency of one that is."
  1567. (let ((needed (cl-loop for p in package-selected-packages
  1568. if (assq p package-alist)
  1569. ;; `p' and its dependencies are needed.
  1570. append (cons p (package--get-deps p)))))
  1571. (cl-loop for p in (mapcar #'car package-alist)
  1572. unless (memq p needed)
  1573. collect p)))
  1574. (defun package--used-elsewhere-p (pkg-desc &optional pkg-list all)
  1575. "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
  1576. Return the first package found in PKG-LIST of which PKG is a
  1577. dependency. If ALL is non-nil, return all such packages instead.
  1578. When not specified, PKG-LIST defaults to `package-alist'
  1579. with PKG-DESC entry removed."
  1580. (unless (string= (package-desc-status pkg-desc) "obsolete")
  1581. (let* ((pkg (package-desc-name pkg-desc))
  1582. (alist (or pkg-list
  1583. (remove (assq pkg package-alist)
  1584. package-alist))))
  1585. (if all
  1586. (cl-loop for p in alist
  1587. if (assq pkg (package-desc-reqs (cadr p)))
  1588. collect (cadr p))
  1589. (cl-loop for p in alist thereis
  1590. (and (assq pkg (package-desc-reqs (cadr p)))
  1591. (cadr p)))))))
  1592. (defun package--sort-deps-in-alist (package only)
  1593. "Return a list of dependencies for PACKAGE sorted by dependency.
  1594. PACKAGE is included as the first element of the returned list.
  1595. ONLY is an alist associating package names to package objects.
  1596. Only these packages will be in the return value an their cdrs are
  1597. destructively set to nil in ONLY."
  1598. (let ((out))
  1599. (dolist (dep (package-desc-reqs package))
  1600. (when-let ((cell (assq (car dep) only))
  1601. (dep-package (cdr-safe cell)))
  1602. (setcdr cell nil)
  1603. (setq out (append (package--sort-deps-in-alist dep-package only)
  1604. out))))
  1605. (cons package out)))
  1606. (defun package--sort-by-dependence (package-list)
  1607. "Return PACKAGE-LIST sorted by dependence.
  1608. That is, any element of the returned list is guaranteed to not
  1609. directly depend on any elements that come before it.
  1610. PACKAGE-LIST is a list of `package-desc' objects.
  1611. Indirect dependencies are guaranteed to be returned in order only
  1612. if all the in-between dependencies are also in PACKAGE-LIST."
  1613. (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
  1614. out-list)
  1615. (dolist (cell alist out-list)
  1616. ;; `package--sort-deps-in-alist' destructively changes alist, so
  1617. ;; some cells might already be empty. We check this here.
  1618. (when-let ((pkg-desc (cdr cell)))
  1619. (setcdr cell nil)
  1620. (setq out-list
  1621. (append (package--sort-deps-in-alist pkg-desc alist)
  1622. out-list))))))
  1623. ;;; Installation Functions
  1624. ;; As opposed to the previous section (which listed some underlying
  1625. ;; functions necessary for installation), this one contains the actual
  1626. ;; functions that install packages. The package itself can be
  1627. ;; installed in a variety of ways (archives, buffer, file), but
  1628. ;; requirements (dependencies) are always satisfied by looking in
  1629. ;; `package-archive-contents'.
  1630. (defun package-archive-base (desc)
  1631. "Return the archive containing the package NAME."
  1632. (cdr (assoc (package-desc-archive desc) package-archives)))
  1633. (defun package-install-from-archive (pkg-desc)
  1634. "Download and install a tar package."
  1635. ;; This won't happen, unless the archive is doing something wrong.
  1636. (when (eq (package-desc-kind pkg-desc) 'dir)
  1637. (error "Can't install directory package from archive"))
  1638. (let* ((location (package-archive-base pkg-desc))
  1639. (file (concat (package-desc-full-name pkg-desc)
  1640. (package-desc-suffix pkg-desc))))
  1641. (package--with-response-buffer location :file file
  1642. (if (or (not package-check-signature)
  1643. (member (package-desc-archive pkg-desc)
  1644. package-unsigned-archives))
  1645. ;; If we don't care about the signature, unpack and we're
  1646. ;; done.
  1647. (let ((save-silently t))
  1648. (package-unpack pkg-desc))
  1649. ;; If we care, check it and *then* write the file.
  1650. (let ((content (buffer-string)))
  1651. (package--check-signature
  1652. location file content nil
  1653. ;; This function will be called after signature checking.
  1654. (lambda (&optional good-sigs)
  1655. ;; Signature checked, unpack now.
  1656. (with-temp-buffer (insert content)
  1657. (let ((save-silently t))
  1658. (package-unpack pkg-desc)))
  1659. ;; Here the package has been installed successfully, mark it as
  1660. ;; signed if appropriate.
  1661. (when good-sigs
  1662. ;; Write out good signatures into NAME-VERSION.signed file.
  1663. (write-region (mapconcat #'epg-signature-to-string good-sigs "\n")
  1664. nil
  1665. (expand-file-name
  1666. (concat (package-desc-full-name pkg-desc) ".signed")
  1667. package-user-dir)
  1668. nil 'silent)
  1669. ;; Update the old pkg-desc which will be shown on the description buffer.
  1670. (setf (package-desc-signed pkg-desc) t)
  1671. ;; Update the new (activated) pkg-desc as well.
  1672. (when-let ((pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist))))
  1673. (setf (package-desc-signed (car pkg-descs)) t))))))))))
  1674. (defun package-installed-p (package &optional min-version)
  1675. "Return non-nil if PACKAGE, of MIN-VERSION or newer, is installed.
  1676. If PACKAGE is a symbol, it is the package name and MIN-VERSION
  1677. should be a version list.
  1678. If PACKAGE is a `package-desc' object, MIN-VERSION is ignored."
  1679. (unless package--initialized (error "package.el is not yet initialized!"))
  1680. (if (package-desc-p package)
  1681. (let ((dir (package-desc-dir package)))
  1682. (and (stringp dir)
  1683. (file-exists-p dir)))
  1684. (or
  1685. (let ((pkg-descs (cdr (assq package package-alist))))
  1686. (and pkg-descs
  1687. (version-list-<= min-version
  1688. (package-desc-version (car pkg-descs)))))
  1689. ;; Also check built-in packages.
  1690. (package-built-in-p package min-version))))
  1691. (defun package-download-transaction (packages)
  1692. "Download and install all the packages in PACKAGES.
  1693. PACKAGES should be a list of `package-desc'.
  1694. This function assumes that all package requirements in
  1695. PACKAGES are satisfied, i.e. that PACKAGES is computed
  1696. using `package-compute-transaction'."
  1697. (mapc #'package-install-from-archive packages))
  1698. (defun package--ensure-init-file ()
  1699. "Ensure that the user's init file has `package-initialize'.
  1700. `package-initialize' doesn't have to be called, as long as it is
  1701. present somewhere in the file, even as a comment. If it is not,
  1702. add a call to it along with some explanatory comments."
  1703. ;; Don't mess with the init-file from "emacs -Q".
  1704. (when (and (stringp user-init-file)
  1705. (not package--init-file-ensured)
  1706. (file-readable-p user-init-file)
  1707. (file-writable-p user-init-file))
  1708. (let* ((buffer (find-buffer-visiting user-init-file))
  1709. buffer-name
  1710. (contains-init
  1711. (if buffer
  1712. (with-current-buffer buffer
  1713. (save-excursion
  1714. (save-restriction
  1715. (widen)
  1716. (goto-char (point-min))
  1717. (re-search-forward "(package-initialize\\_>" nil 'noerror))))
  1718. ;; Don't visit the file if we don't have to.
  1719. (with-temp-buffer
  1720. (insert-file-contents user-init-file)
  1721. (goto-char (point-min))
  1722. (re-search-forward "(package-initialize\\_>" nil 'noerror)))))
  1723. (unless contains-init
  1724. (with-current-buffer (or buffer
  1725. (let ((delay-mode-hooks t)
  1726. (find-file-visit-truename t))
  1727. (find-file-noselect user-init-file)))
  1728. (when buffer
  1729. (setq buffer-name (buffer-file-name))
  1730. (set-visited-file-name (file-chase-links user-init-file)))
  1731. (save-excursion
  1732. (save-restriction
  1733. (widen)
  1734. (goto-char (point-min))
  1735. (while (and (looking-at-p "[[:blank:]]*\\(;\\|$\\)")
  1736. (not (eobp)))
  1737. (forward-line 1))
  1738. (insert
  1739. "\n"
  1740. ";; Added by Package.el. This must come before configurations of\n"
  1741. ";; installed packages. Don't delete this line. If you don't want it,\n"
  1742. ";; just comment it out by adding a semicolon to the start of the line.\n"
  1743. ";; You may delete these explanatory comments.\n"
  1744. "(package-initialize)\n")
  1745. (unless (looking-at-p "$")
  1746. (insert "\n"))
  1747. (let ((file-precious-flag t))
  1748. (save-buffer))
  1749. (if buffer
  1750. (progn
  1751. (set-visited-file-name buffer-name)
  1752. (set-buffer-modified-p nil))
  1753. (kill-buffer (current-buffer)))))))))
  1754. (setq package--init-file-ensured t))
  1755. ;;;###autoload
  1756. (defun package-install (pkg &optional dont-select)
  1757. "Install the package PKG.
  1758. PKG can be a `package-desc' or a symbol naming one of the available packages
  1759. in an archive in `package-archives'. Interactively, prompt for its name.
  1760. If called interactively or if DONT-SELECT nil, add PKG to
  1761. `package-selected-packages'.
  1762. If PKG is a `package-desc' and it is already installed, don't try
  1763. to install it but still mark it as selected."
  1764. (interactive
  1765. (progn
  1766. ;; Initialize the package system to get the list of package
  1767. ;; symbols for completion.
  1768. (unless package--initialized
  1769. (package-initialize t))
  1770. (unless package-archive-contents
  1771. (package-refresh-contents))
  1772. (list (intern (completing-read
  1773. "Install package: "
  1774. (delq nil
  1775. (mapcar (lambda (elt)
  1776. (unless (package-installed-p (car elt))
  1777. (symbol-name (car elt))))
  1778. package-archive-contents))
  1779. nil t))
  1780. nil)))
  1781. (add-hook 'post-command-hook #'package-menu--post-refresh)
  1782. (let ((name (if (package-desc-p pkg)
  1783. (package-desc-name pkg)
  1784. pkg)))
  1785. (unless (or dont-select (package--user-selected-p name))
  1786. (package--save-selected-packages
  1787. (cons name package-selected-packages)))
  1788. (if-let ((transaction
  1789. (if (package-desc-p pkg)
  1790. (unless (package-installed-p pkg)
  1791. (package-compute-transaction (list pkg)
  1792. (package-desc-reqs pkg)))
  1793. (package-compute-transaction () (list (list pkg))))))
  1794. (package-download-transaction transaction)
  1795. (message "`%s' is already installed" name))))
  1796. (defun package-strip-rcs-id (str)
  1797. "Strip RCS version ID from the version string STR.
  1798. If the result looks like a dotted numeric version, return it.
  1799. Otherwise return nil."
  1800. (when str
  1801. (when (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
  1802. (setq str (substring str (match-end 0))))
  1803. (ignore-errors
  1804. (if (version-to-list str) str))))
  1805. (declare-function lm-homepage "lisp-mnt" (&optional file))
  1806. ;;;###autoload
  1807. (defun package-install-from-buffer ()
  1808. "Install a package from the current buffer.
  1809. The current buffer is assumed to be a single .el or .tar file or
  1810. a directory. These must follow the packaging guidelines (see
  1811. info node `(elisp)Packaging').
  1812. Specially, if current buffer is a directory, the -pkg.el
  1813. description file is not mandatory, in which case the information
  1814. is derived from the main .el file in the directory.
  1815. Downloads and installs required packages as needed."
  1816. (interactive)
  1817. (let* ((pkg-desc
  1818. (cond
  1819. ((derived-mode-p 'dired-mode)
  1820. ;; This is the only way a package-desc object with a `dir'
  1821. ;; desc-kind can be created. Such packages can't be
  1822. ;; uploaded or installed from archives, they can only be
  1823. ;; installed from local buffers or directories.
  1824. (package-dir-info))
  1825. ((derived-mode-p 'tar-mode)
  1826. (package-tar-file-info))
  1827. (t
  1828. (save-excursion
  1829. (package-buffer-info)))))
  1830. (name (package-desc-name pkg-desc)))
  1831. ;; Download and install the dependencies.
  1832. (let* ((requires (package-desc-reqs pkg-desc))
  1833. (transaction (package-compute-transaction nil requires)))
  1834. (package-download-transaction transaction))
  1835. ;; Install the package itself.
  1836. (package-unpack pkg-desc)
  1837. (unless (package--user-selected-p name)
  1838. (package--save-selected-packages
  1839. (cons name package-selected-packages)))
  1840. pkg-desc))
  1841. ;;;###autoload
  1842. (defun package-install-file (file)
  1843. "Install a package from a file.
  1844. The file can either be a tar file, an Emacs Lisp file, or a
  1845. directory."
  1846. (interactive "fPackage file name: ")
  1847. (with-temp-buffer
  1848. (if (file-directory-p file)
  1849. (progn
  1850. (setq default-directory file)
  1851. (dired-mode))
  1852. (insert-file-contents-literally file)
  1853. (when (string-match "\\.tar\\'" file) (tar-mode)))
  1854. (package-install-from-buffer)))
  1855. ;;;###autoload
  1856. (defun package-install-selected-packages ()
  1857. "Ensure packages in `package-selected-packages' are installed.
  1858. If some packages are not installed propose to install them."
  1859. (interactive)
  1860. ;; We don't need to populate `package-selected-packages' before
  1861. ;; using here, because the outcome is the same either way (nothing
  1862. ;; gets installed).
  1863. (if (not package-selected-packages)
  1864. (message "`package-selected-packages' is empty, nothing to install")
  1865. (let* ((not-installed (seq-remove #'package-installed-p package-selected-packages))
  1866. (available (seq-filter (lambda (p) (assq p package-archive-contents)) not-installed))
  1867. (difference (- (length not-installed) (length available))))
  1868. (cond
  1869. (available
  1870. (when (y-or-n-p
  1871. (format "%s packages will be installed:\n%s, proceed?"
  1872. (length available)
  1873. (mapconcat #'symbol-name available ", ")))
  1874. (mapc (lambda (p) (package-install p 'dont-select)) available)))
  1875. ((> difference 0)
  1876. (message "%s packages are not available (the rest already installed), maybe you need to `M-x package-refresh-contents'"
  1877. difference))
  1878. (t
  1879. (message "All your packages are already installed"))))))
  1880. ;;; Package Deletion
  1881. (defun package--newest-p (pkg)
  1882. "Return non-nil if PKG is the newest package with its name."
  1883. (equal (cadr (assq (package-desc-name pkg) package-alist))
  1884. pkg))
  1885. (defun package-delete (pkg-desc &optional force nosave)
  1886. "Delete package PKG-DESC.
  1887. Argument PKG-DESC is a full description of package as vector.
  1888. Interactively, prompt the user for the package name and version.
  1889. When package is used elsewhere as dependency of another package,
  1890. refuse deleting it and return an error.
  1891. If prefix argument FORCE is non-nil, package will be deleted even
  1892. if it is used elsewhere.
  1893. If NOSAVE is non-nil, the package is not removed from
  1894. `package-selected-packages'."
  1895. (interactive
  1896. (progn
  1897. ;; Initialize the package system to get the list of package
  1898. ;; symbols for completion.
  1899. (unless package--initialized
  1900. (package-initialize t))
  1901. (let* ((package-table
  1902. (mapcar
  1903. (lambda (p) (cons (package-desc-full-name p) p))
  1904. (delq nil
  1905. (mapcar (lambda (p) (unless (package-built-in-p p) p))
  1906. (apply #'append (mapcar #'cdr package-alist))))))
  1907. (package-name (completing-read "Delete package: "
  1908. (mapcar #'car package-table)
  1909. nil t)))
  1910. (list (cdr (assoc package-name package-table))
  1911. current-prefix-arg nil))))
  1912. (let ((dir (package-desc-dir pkg-desc))
  1913. (name (package-desc-name pkg-desc))
  1914. pkg-used-elsewhere-by)
  1915. ;; If the user is trying to delete this package, they definitely
  1916. ;; don't want it marked as selected, so we remove it from
  1917. ;; `package-selected-packages' even if it can't be deleted.
  1918. (when (and (null nosave)
  1919. (package--user-selected-p name)
  1920. ;; Don't deselect if this is an older version of an
  1921. ;; upgraded package.
  1922. (package--newest-p pkg-desc))
  1923. (package--save-selected-packages (remove name package-selected-packages)))
  1924. (cond ((not (string-prefix-p (file-name-as-directory
  1925. (expand-file-name package-user-dir))
  1926. (expand-file-name dir)))
  1927. ;; Don't delete "system" packages.
  1928. (error "Package `%s' is a system package, not deleting"
  1929. (package-desc-full-name pkg-desc)))
  1930. ((and (null force)
  1931. (setq pkg-used-elsewhere-by
  1932. (package--used-elsewhere-p pkg-desc)))
  1933. ;; Don't delete packages used as dependency elsewhere.
  1934. (error "Package `%s' is used by `%s' as dependency, not deleting"
  1935. (package-desc-full-name pkg-desc)
  1936. (package-desc-name pkg-used-elsewhere-by)))
  1937. (t
  1938. (add-hook 'post-command-hook #'package-menu--post-refresh)
  1939. (delete-directory dir t)
  1940. ;; Remove NAME-VERSION.signed and NAME-readme.txt files.
  1941. (dolist (suffix '(".signed" "readme.txt"))
  1942. (let* ((version (package-version-join (package-desc-version pkg-desc)))
  1943. (file (concat (if (string= suffix ".signed")
  1944. dir
  1945. (substring dir 0 (- (length version))))
  1946. suffix)))
  1947. (when (file-exists-p file)
  1948. (delete-file file))))
  1949. ;; Update package-alist.
  1950. (let ((pkgs (assq name package-alist)))
  1951. (delete pkg-desc pkgs)
  1952. (unless (cdr pkgs)
  1953. (setq package-alist (delq pkgs package-alist))))
  1954. (message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
  1955. ;;;###autoload
  1956. (defun package-reinstall (pkg)
  1957. "Reinstall package PKG.
  1958. PKG should be either a symbol, the package name, or a `package-desc'
  1959. object."
  1960. (interactive (list (intern (completing-read
  1961. "Reinstall package: "
  1962. (mapcar #'symbol-name
  1963. (mapcar #'car package-alist))))))
  1964. (package-delete
  1965. (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
  1966. 'force 'nosave)
  1967. (package-install pkg 'dont-select))
  1968. ;;;###autoload
  1969. (defun package-autoremove ()
  1970. "Remove packages that are no more needed.
  1971. Packages that are no more needed by other packages in
  1972. `package-selected-packages' and their dependencies
  1973. will be deleted."
  1974. (interactive)
  1975. ;; If `package-selected-packages' is nil, it would make no sense to
  1976. ;; try to populate it here, because then `package-autoremove' will
  1977. ;; do absolutely nothing.
  1978. (when (or package-selected-packages
  1979. (yes-or-no-p
  1980. (format-message
  1981. "`package-selected-packages' is empty! Really remove ALL packages? ")))
  1982. (let ((removable (package--removable-packages)))
  1983. (if removable
  1984. (when (y-or-n-p
  1985. (format "%s packages will be deleted:\n%s, proceed? "
  1986. (length removable)
  1987. (mapconcat #'symbol-name removable ", ")))
  1988. (mapc (lambda (p)
  1989. (package-delete (cadr (assq p package-alist)) t))
  1990. removable))
  1991. (message "Nothing to autoremove")))))
  1992. ;;;; Package description buffer.
  1993. ;;;###autoload
  1994. (defun describe-package (package)
  1995. "Display the full documentation of PACKAGE (a symbol)."
  1996. (interactive
  1997. (let* ((guess (or (function-called-at-point)
  1998. (symbol-at-point))))
  1999. (require 'finder-inf nil t)
  2000. ;; Load the package list if necessary (but don't activate them).
  2001. (unless package--initialized
  2002. (package-initialize t))
  2003. (let ((packages (append (mapcar 'car package-alist)
  2004. (mapcar 'car package-archive-contents)
  2005. (mapcar 'car package--builtins))))
  2006. (unless (memq guess packages)
  2007. (setq guess nil))
  2008. (setq packages (mapcar 'symbol-name packages))
  2009. (let ((val
  2010. (completing-read (if guess
  2011. (format "Describe package (default %s): "
  2012. guess)
  2013. "Describe package: ")
  2014. packages nil t nil nil (when guess
  2015. (symbol-name guess)))))
  2016. (list (intern val))))))
  2017. (if (not (or (package-desc-p package) (and package (symbolp package))))
  2018. (message "No package specified")
  2019. (help-setup-xref (list #'describe-package package)
  2020. (called-interactively-p 'interactive))
  2021. (with-help-window (help-buffer)
  2022. (with-current-buffer standard-output
  2023. (describe-package-1 package)))))
  2024. (defface package-help-section-name
  2025. '((t :inherit (bold font-lock-function-name-face)))
  2026. "Face used on section names in package description buffers."
  2027. :version "25.1")
  2028. (defun package--print-help-section (name &rest strings)
  2029. "Print \"NAME: \", right aligned to the 13th column.
  2030. If more STRINGS are provided, insert them followed by a newline.
  2031. Otherwise no newline is inserted."
  2032. (declare (indent 1))
  2033. (insert (make-string (max 0 (- 11 (string-width name))) ?\s)
  2034. (propertize (concat name ": ") 'font-lock-face 'package-help-section-name))
  2035. (when strings
  2036. (apply #'insert strings)
  2037. (insert "\n")))
  2038. (declare-function lm-commentary "lisp-mnt" (&optional file))
  2039. (defun describe-package-1 (pkg)
  2040. (require 'lisp-mnt)
  2041. (let* ((desc (or
  2042. (if (package-desc-p pkg) pkg)
  2043. (cadr (assq pkg package-alist))
  2044. (let ((built-in (assq pkg package--builtins)))
  2045. (if built-in
  2046. (package--from-builtin built-in)
  2047. (cadr (assq pkg package-archive-contents))))))
  2048. (name (if desc (package-desc-name desc) pkg))
  2049. (pkg-dir (if desc (package-desc-dir desc)))
  2050. (reqs (if desc (package-desc-reqs desc)))
  2051. (required-by (if desc (package--used-elsewhere-p desc nil 'all)))
  2052. (version (if desc (package-desc-version desc)))
  2053. (archive (if desc (package-desc-archive desc)))
  2054. (extras (and desc (package-desc-extras desc)))
  2055. (homepage (cdr (assoc :url extras)))
  2056. (keywords (if desc (package-desc--keywords desc)))
  2057. (built-in (eq pkg-dir 'builtin))
  2058. (installable (and archive (not built-in)))
  2059. (status (if desc (package-desc-status desc) "orphan"))
  2060. (incompatible-reason (package--incompatible-p desc))
  2061. (signed (if desc (package-desc-signed desc))))
  2062. (when (string= status "avail-obso")
  2063. (setq status "available obsolete"))
  2064. (when incompatible-reason
  2065. (setq status "incompatible"))
  2066. (prin1 name)
  2067. (princ " is ")
  2068. (princ (if (memq (aref status 0) '(?a ?e ?i ?o ?u)) "an " "a "))
  2069. (princ status)
  2070. (princ " package.\n\n")
  2071. (package--print-help-section "Status")
  2072. (cond (built-in
  2073. (insert (propertize (capitalize status)
  2074. 'font-lock-face 'package-status-built-in)
  2075. "."))
  2076. (pkg-dir
  2077. (insert (propertize (if (member status '("unsigned" "dependency"))
  2078. "Installed"
  2079. (capitalize status))
  2080. 'font-lock-face 'package-status-built-in))
  2081. (insert (substitute-command-keys " in `"))
  2082. (let ((dir (abbreviate-file-name
  2083. (file-name-as-directory
  2084. (if (file-in-directory-p pkg-dir package-user-dir)
  2085. (file-relative-name pkg-dir package-user-dir)
  2086. pkg-dir)))))
  2087. (help-insert-xref-button dir 'help-package-def pkg-dir))
  2088. (if (and (package-built-in-p name)
  2089. (not (package-built-in-p name version)))
  2090. (insert (substitute-command-keys
  2091. "',\n shadowing a ")
  2092. (propertize "built-in package"
  2093. 'font-lock-face 'package-status-built-in))
  2094. (insert (substitute-command-keys "'")))
  2095. (if signed
  2096. (insert ".")
  2097. (insert " (unsigned)."))
  2098. (when (and (package-desc-p desc)
  2099. (not required-by)
  2100. (member status '("unsigned" "installed")))
  2101. (insert " ")
  2102. (package-make-button "Delete"
  2103. 'action #'package-delete-button-action
  2104. 'package-desc desc)))
  2105. (incompatible-reason
  2106. (insert (propertize "Incompatible" 'font-lock-face font-lock-warning-face)
  2107. " because it depends on ")
  2108. (if (stringp incompatible-reason)
  2109. (insert "Emacs " incompatible-reason ".")
  2110. (insert "uninstallable packages.")))
  2111. (installable
  2112. (insert (capitalize status))
  2113. (insert " from " (format "%s" archive))
  2114. (insert " -- ")
  2115. (package-make-button
  2116. "Install"
  2117. 'action 'package-install-button-action
  2118. 'package-desc desc))
  2119. (t (insert (capitalize status) ".")))
  2120. (insert "\n")
  2121. (unless (and pkg-dir (not archive)) ; Installed pkgs don't have archive.
  2122. (package--print-help-section "Archive"
  2123. (or archive "n/a")))
  2124. (and version
  2125. (package--print-help-section "Version"
  2126. (package-version-join version)))
  2127. (when desc
  2128. (package--print-help-section "Summary"
  2129. (package-desc-summary desc)))
  2130. (setq reqs (if desc (package-desc-reqs desc)))
  2131. (when reqs
  2132. (package--print-help-section "Requires")
  2133. (let ((first t))
  2134. (dolist (req reqs)
  2135. (let* ((name (car req))
  2136. (vers (cadr req))
  2137. (text (format "%s-%s" (symbol-name name)
  2138. (package-version-join vers)))
  2139. (reason (if (and (listp incompatible-reason)
  2140. (assq name incompatible-reason))
  2141. " (not available)" "")))
  2142. (cond (first (setq first nil))
  2143. ((>= (+ 2 (current-column) (length text) (length reason))
  2144. (window-width))
  2145. (insert ",\n "))
  2146. (t (insert ", ")))
  2147. (help-insert-xref-button text 'help-package name)
  2148. (insert reason)))
  2149. (insert "\n")))
  2150. (when required-by
  2151. (package--print-help-section "Required by")
  2152. (let ((first t))
  2153. (dolist (pkg required-by)
  2154. (let ((text (package-desc-full-name pkg)))
  2155. (cond (first (setq first nil))
  2156. ((>= (+ 2 (current-column) (length text))
  2157. (window-width))
  2158. (insert ",\n "))
  2159. (t (insert ", ")))
  2160. (help-insert-xref-button text 'help-package
  2161. (package-desc-name pkg))))
  2162. (insert "\n")))
  2163. (when homepage
  2164. ;; Prefer https for the homepage of packages on gnu.org.
  2165. (if (string-match-p "^http://\\(elpa\\|www\\)\\.gnu\\.org/" homepage)
  2166. (let ((gnu (cdr (assoc "gnu" package-archives))))
  2167. (and gnu (string-match-p "^https" gnu)
  2168. (setq homepage
  2169. (replace-regexp-in-string "^http" "https" homepage)))))
  2170. (package--print-help-section "Homepage")
  2171. (help-insert-xref-button homepage 'help-url homepage)
  2172. (insert "\n"))
  2173. (when keywords
  2174. (package--print-help-section "Keywords")
  2175. (dolist (k keywords)
  2176. (package-make-button
  2177. k
  2178. 'package-keyword k
  2179. 'action 'package-keyword-button-action)
  2180. (insert " "))
  2181. (insert "\n"))
  2182. (let* ((all-pkgs (append (cdr (assq name package-alist))
  2183. (cdr (assq name package-archive-contents))
  2184. (let ((bi (assq name package--builtins)))
  2185. (if bi (list (package--from-builtin bi))))))
  2186. (other-pkgs (delete desc all-pkgs)))
  2187. (when other-pkgs
  2188. (package--print-help-section "Other versions"
  2189. (mapconcat (lambda (opkg)
  2190. (let* ((ov (package-desc-version opkg))
  2191. (dir (package-desc-dir opkg))
  2192. (from (or (package-desc-archive opkg)
  2193. (if (stringp dir) "installed" dir))))
  2194. (if (not ov) (format "%s" from)
  2195. (format "%s (%s)"
  2196. (make-text-button (package-version-join ov) nil
  2197. 'font-lock-face 'link
  2198. 'follow-link t
  2199. 'action
  2200. (lambda (_button)
  2201. (describe-package opkg)))
  2202. from))))
  2203. other-pkgs ", ")
  2204. ".")))
  2205. (insert "\n")
  2206. (if built-in
  2207. ;; For built-in packages, insert the commentary.
  2208. (let ((fn (locate-file (format "%s.el" name) load-path
  2209. load-file-rep-suffixes))
  2210. (opoint (point)))
  2211. (insert (or (lm-commentary fn) ""))
  2212. (save-excursion
  2213. (goto-char opoint)
  2214. (when (re-search-forward "^;;; Commentary:\n" nil t)
  2215. (replace-match ""))
  2216. (while (re-search-forward "^\\(;+ ?\\)" nil t)
  2217. (replace-match ""))))
  2218. (let* ((basename (format "%s-readme.txt" name))
  2219. (readme (expand-file-name basename package-user-dir))
  2220. readme-string)
  2221. ;; For elpa packages, try downloading the commentary. If that
  2222. ;; fails, try an existing readme file in `package-user-dir'.
  2223. (cond ((and (package-desc-archive desc)
  2224. (package--with-response-buffer (package-archive-base desc)
  2225. :file basename :noerror t
  2226. (save-excursion
  2227. (goto-char (point-max))
  2228. (unless (bolp)
  2229. (insert ?\n)))
  2230. (write-region nil nil
  2231. (expand-file-name readme package-user-dir)
  2232. nil 'silent)
  2233. (setq readme-string (buffer-string))
  2234. t))
  2235. (insert readme-string))
  2236. ((file-readable-p readme)
  2237. (insert-file-contents readme)
  2238. (goto-char (point-max))))))))
  2239. (defun package-install-button-action (button)
  2240. (let ((pkg-desc (button-get button 'package-desc)))
  2241. (when (y-or-n-p (format-message "Install package `%s'? "
  2242. (package-desc-full-name pkg-desc)))
  2243. (package-install pkg-desc nil)
  2244. (revert-buffer nil t)
  2245. (goto-char (point-min)))))
  2246. (defun package-delete-button-action (button)
  2247. (let ((pkg-desc (button-get button 'package-desc)))
  2248. (when (y-or-n-p (format-message "Delete package `%s'? "
  2249. (package-desc-full-name pkg-desc)))
  2250. (package-delete pkg-desc)
  2251. (revert-buffer nil t)
  2252. (goto-char (point-min)))))
  2253. (defun package-keyword-button-action (button)
  2254. (let ((pkg-keyword (button-get button 'package-keyword)))
  2255. (package-show-package-list t (list pkg-keyword))))
  2256. (defun package-make-button (text &rest props)
  2257. (let ((button-text (if (display-graphic-p) text (concat "[" text "]")))
  2258. (button-face (if (display-graphic-p)
  2259. '(:box (:line-width 2 :color "dark grey")
  2260. :background "light grey"
  2261. :foreground "black")
  2262. 'link)))
  2263. (apply 'insert-text-button button-text 'face button-face 'follow-link t
  2264. props)))
  2265. ;;;; Package menu mode.
  2266. (defvar package-menu-mode-map
  2267. (let ((map (make-sparse-keymap)))
  2268. (set-keymap-parent map tabulated-list-mode-map)
  2269. (define-key map "\C-m" 'package-menu-describe-package)
  2270. (define-key map "u" 'package-menu-mark-unmark)
  2271. (define-key map "\177" 'package-menu-backup-unmark)
  2272. (define-key map "d" 'package-menu-mark-delete)
  2273. (define-key map "i" 'package-menu-mark-install)
  2274. (define-key map "U" 'package-menu-mark-upgrades)
  2275. (define-key map "r" 'package-menu-refresh)
  2276. (define-key map "f" 'package-menu-filter)
  2277. (define-key map "~" 'package-menu-mark-obsolete-for-deletion)
  2278. (define-key map "x" 'package-menu-execute)
  2279. (define-key map "h" 'package-menu-quick-help)
  2280. (define-key map "H" #'package-menu-hide-package)
  2281. (define-key map "?" 'package-menu-describe-package)
  2282. (define-key map "(" #'package-menu-toggle-hiding)
  2283. map)
  2284. "Local keymap for `package-menu-mode' buffers.")
  2285. (easy-menu-define package-menu-mode-menu package-menu-mode-map
  2286. "Menu for `package-menu-mode'."
  2287. `("Package"
  2288. ["Describe Package" package-menu-describe-package :help "Display information about this package"]
  2289. ["Help" package-menu-quick-help :help "Show short key binding help for package-menu-mode"]
  2290. "--"
  2291. ["Refresh Package List" package-menu-refresh
  2292. :help "Redownload the ELPA archive"
  2293. :active (not package--downloads-in-progress)]
  2294. ["Redisplay buffer" revert-buffer :help "Update the buffer with current list of packages"]
  2295. ["Execute Marked Actions" package-menu-execute :help "Perform all the marked actions"]
  2296. "--"
  2297. ["Mark All Available Upgrades" package-menu-mark-upgrades
  2298. :help "Mark packages that have a newer version for upgrading"
  2299. :active (not package--downloads-in-progress)]
  2300. ["Mark All Obsolete for Deletion" package-menu-mark-obsolete-for-deletion :help "Mark all obsolete packages for deletion"]
  2301. ["Mark for Install" package-menu-mark-install :help "Mark a package for installation and move to the next line"]
  2302. ["Mark for Deletion" package-menu-mark-delete :help "Mark a package for deletion and move to the next line"]
  2303. ["Unmark" package-menu-mark-unmark :help "Clear any marks on a package and move to the next line"]
  2304. "--"
  2305. ["Filter Package List" package-menu-filter :help "Filter package selection (q to go back)"]
  2306. ["Hide by Regexp" package-menu-hide-package :help "Permanently hide all packages matching a regexp"]
  2307. ["Display Older Versions" package-menu-toggle-hiding
  2308. :style toggle :selected (not package-menu--hide-packages)
  2309. :help "Display package even if a newer version is already installed"]
  2310. "--"
  2311. ["Quit" quit-window :help "Quit package selection"]
  2312. ["Customize" (customize-group 'package)]))
  2313. (defvar package-menu--new-package-list nil
  2314. "List of newly-available packages since `list-packages' was last called.")
  2315. (defvar package-menu--transaction-status nil
  2316. "Mode-line status of ongoing package transaction.")
  2317. (define-derived-mode package-menu-mode tabulated-list-mode "Package Menu"
  2318. "Major mode for browsing a list of packages.
  2319. Letters do not insert themselves; instead, they are commands.
  2320. \\<package-menu-mode-map>
  2321. \\{package-menu-mode-map}"
  2322. (setq mode-line-process '((package--downloads-in-progress ":Loading")
  2323. (package-menu--transaction-status
  2324. package-menu--transaction-status)))
  2325. (setq tabulated-list-format
  2326. `[("Package" 18 package-menu--name-predicate)
  2327. ("Version" 13 nil)
  2328. ("Status" 10 package-menu--status-predicate)
  2329. ,@(if (cdr package-archives)
  2330. '(("Archive" 10 package-menu--archive-predicate)))
  2331. ("Description" 0 nil)])
  2332. (setq tabulated-list-padding 2)
  2333. (setq tabulated-list-sort-key (cons "Status" nil))
  2334. (add-hook 'tabulated-list-revert-hook 'package-menu--refresh nil t)
  2335. (tabulated-list-init-header))
  2336. (defmacro package--push (pkg-desc status listname)
  2337. "Convenience macro for `package-menu--generate'.
  2338. If the alist stored in the symbol LISTNAME lacks an entry for a
  2339. package PKG-DESC, add one. The alist is keyed with PKG-DESC."
  2340. `(unless (assoc ,pkg-desc ,listname)
  2341. ;; FIXME: Should we move status into pkg-desc?
  2342. (push (cons ,pkg-desc ,status) ,listname)))
  2343. (defvar package-list-unversioned nil
  2344. "If non-nil include packages that don't have a version in `list-package'.")
  2345. (defvar package-list-unsigned nil
  2346. "If non-nil, mention in the list which packages were installed w/o signature.")
  2347. (defvar package--emacs-version-list (version-to-list emacs-version)
  2348. "`emacs-version', as a list.")
  2349. (defun package--incompatible-p (pkg &optional shallow)
  2350. "Return non-nil if PKG has no chance of being installable.
  2351. PKG is a `package-desc' object.
  2352. If SHALLOW is non-nil, this only checks if PKG depends on a
  2353. higher `emacs-version' than the one being used. Otherwise, also
  2354. checks the viability of dependencies, according to
  2355. `package--compatibility-table'.
  2356. If PKG requires an incompatible Emacs version, the return value
  2357. is this version (as a string).
  2358. If PKG requires incompatible packages, the return value is a list
  2359. of these dependencies, similar to the list returned by
  2360. `package-desc-reqs'."
  2361. (let* ((reqs (package-desc-reqs pkg))
  2362. (version (cadr (assq 'emacs reqs))))
  2363. (if (and version (version-list-< package--emacs-version-list version))
  2364. (package-version-join version)
  2365. (unless shallow
  2366. (let (out)
  2367. (dolist (dep (package-desc-reqs pkg) out)
  2368. (let ((dep-name (car dep)))
  2369. (unless (eq 'emacs dep-name)
  2370. (let ((cv (gethash dep-name package--compatibility-table)))
  2371. (when (version-list-< (or cv '(0)) (or (cadr dep) '(0)))
  2372. (push dep out)))))))))))
  2373. (defun package-desc-status (pkg-desc)
  2374. (let* ((name (package-desc-name pkg-desc))
  2375. (dir (package-desc-dir pkg-desc))
  2376. (lle (assq name package-load-list))
  2377. (held (cadr lle))
  2378. (version (package-desc-version pkg-desc))
  2379. (signed (or (not package-list-unsigned)
  2380. (package-desc-signed pkg-desc))))
  2381. (cond
  2382. ((eq dir 'builtin) "built-in")
  2383. ((and lle (null held)) "disabled")
  2384. ((stringp held)
  2385. (let ((hv (if (stringp held) (version-to-list held))))
  2386. (cond
  2387. ((version-list-= version hv) "held")
  2388. ((version-list-< version hv) "obsolete")
  2389. (t "disabled"))))
  2390. (dir ;One of the installed packages.
  2391. (cond
  2392. ((not (file-exists-p dir)) "deleted")
  2393. ;; Not inside `package-user-dir'.
  2394. ((not (file-in-directory-p dir package-user-dir)) "external")
  2395. ((eq pkg-desc (cadr (assq name package-alist)))
  2396. (if (not signed) "unsigned"
  2397. (if (package--user-selected-p name)
  2398. "installed" "dependency")))
  2399. (t "obsolete")))
  2400. ((package--incompatible-p pkg-desc) "incompat")
  2401. (t
  2402. (let* ((ins (cadr (assq name package-alist)))
  2403. (ins-v (if ins (package-desc-version ins))))
  2404. (cond
  2405. ;; Installed obsolete packages are handled in the `dir'
  2406. ;; clause above. Here we handle available obsolete, which
  2407. ;; are displayed depending on `package-menu--hide-packages'.
  2408. ((and ins (version-list-<= version ins-v)) "avail-obso")
  2409. (t
  2410. (if (memq name package-menu--new-package-list)
  2411. "new" "available"))))))))
  2412. (defvar package-menu--hide-packages t
  2413. "Whether available obsolete packages should be hidden.
  2414. Can be toggled with \\<package-menu-mode-map> \\[package-menu-toggle-hiding].
  2415. Installed obsolete packages are always displayed.")
  2416. (defun package-menu-toggle-hiding ()
  2417. "Toggle visibility of obsolete available packages."
  2418. (interactive)
  2419. (unless (derived-mode-p 'package-menu-mode)
  2420. (user-error "The current buffer is not a Package Menu"))
  2421. (setq package-menu--hide-packages
  2422. (not package-menu--hide-packages))
  2423. (message "%s packages" (if package-menu--hide-packages
  2424. "Hiding obsolete or unwanted"
  2425. "Displaying all"))
  2426. (revert-buffer nil 'no-confirm))
  2427. (defun package--remove-hidden (pkg-list)
  2428. "Filter PKG-LIST according to `package-archive-priorities'.
  2429. PKG-LIST must be a list of `package-desc' objects, all with the
  2430. same name, sorted by decreasing `package-desc-priority-version'.
  2431. Return a list of packages tied for the highest priority according
  2432. to their archives."
  2433. (when pkg-list
  2434. ;; Variable toggled with `package-menu-toggle-hiding'.
  2435. (if (not package-menu--hide-packages)
  2436. pkg-list
  2437. (let ((installed (cadr (assq (package-desc-name (car pkg-list))
  2438. package-alist))))
  2439. (when installed
  2440. (setq pkg-list
  2441. (let ((ins-version (package-desc-version installed)))
  2442. (cl-remove-if (lambda (p) (version-list-< (package-desc-version p)
  2443. ins-version))
  2444. pkg-list))))
  2445. (let ((filtered-by-priority
  2446. (cond
  2447. ((not package-menu-hide-low-priority)
  2448. pkg-list)
  2449. ((eq package-menu-hide-low-priority 'archive)
  2450. (let* ((max-priority most-negative-fixnum)
  2451. (out))
  2452. (while pkg-list
  2453. (let ((p (pop pkg-list)))
  2454. (let ((priority (package-desc-priority p)))
  2455. (if (< priority max-priority)
  2456. (setq pkg-list nil)
  2457. (push p out)
  2458. (setq max-priority priority)))))
  2459. (nreverse out)))
  2460. (pkg-list
  2461. (list (car pkg-list))))))
  2462. (if (not installed)
  2463. filtered-by-priority
  2464. (let ((ins-version (package-desc-version installed)))
  2465. (cl-remove-if (lambda (p) (version-list-= (package-desc-version p)
  2466. ins-version))
  2467. filtered-by-priority))))))))
  2468. (defcustom package-hidden-regexps nil
  2469. "List of regexps matching the name of packages to hide.
  2470. If the name of a package matches any of these regexps it is
  2471. omitted from the package menu. To toggle this, type \\[package-menu-toggle-hiding].
  2472. Values can be interactively added to this list by typing
  2473. \\[package-menu-hide-package] on a package"
  2474. :version "25.1"
  2475. :type '(repeat (regexp :tag "Hide packages with name matching")))
  2476. (defun package-menu--refresh (&optional packages keywords)
  2477. "Re-populate the `tabulated-list-entries'.
  2478. PACKAGES should be nil or t, which means to display all known packages.
  2479. KEYWORDS should be nil or a list of keywords."
  2480. ;; Construct list of (PKG-DESC . STATUS).
  2481. (unless packages (setq packages t))
  2482. (let ((hidden-names (mapconcat #'identity package-hidden-regexps "\\|"))
  2483. info-list)
  2484. ;; Installed packages:
  2485. (dolist (elt package-alist)
  2486. (let ((name (car elt)))
  2487. (when (or (eq packages t) (memq name packages))
  2488. (dolist (pkg (cdr elt))
  2489. (when (package--has-keyword-p pkg keywords)
  2490. (push pkg info-list))))))
  2491. ;; Built-in packages:
  2492. (dolist (elt package--builtins)
  2493. (let ((pkg (package--from-builtin elt))
  2494. (name (car elt)))
  2495. (when (not (eq name 'emacs)) ; Hide the `emacs' package.
  2496. (when (and (package--has-keyword-p pkg keywords)
  2497. (or package-list-unversioned
  2498. (package--bi-desc-version (cdr elt)))
  2499. (or (eq packages t) (memq name packages)))
  2500. (push pkg info-list)))))
  2501. ;; Available and disabled packages:
  2502. (unless (equal package--old-archive-priorities package-archive-priorities)
  2503. (package-read-all-archive-contents))
  2504. (dolist (elt package-archive-contents)
  2505. (let ((name (car elt)))
  2506. ;; To be displayed it must be in PACKAGES;
  2507. (when (and (or (eq packages t) (memq name packages))
  2508. ;; and we must either not be hiding anything,
  2509. (or (not package-menu--hide-packages)
  2510. (not package-hidden-regexps)
  2511. ;; or just not hiding this specific package.
  2512. (not (string-match hidden-names (symbol-name name)))))
  2513. ;; Hide available-obsolete or low-priority packages.
  2514. (dolist (pkg (package--remove-hidden (cdr elt)))
  2515. (when (package--has-keyword-p pkg keywords)
  2516. (push pkg info-list))))))
  2517. ;; Print the result.
  2518. (tabulated-list-init-header)
  2519. (setq tabulated-list-entries
  2520. (mapcar #'package-menu--print-info-simple info-list))))
  2521. (defun package-all-keywords ()
  2522. "Collect all package keywords"
  2523. (let ((key-list))
  2524. (package--mapc (lambda (desc)
  2525. (setq key-list (append (package-desc--keywords desc)
  2526. key-list))))
  2527. key-list))
  2528. (defun package--mapc (function &optional packages)
  2529. "Call FUNCTION for all known PACKAGES.
  2530. PACKAGES can be nil or t, which means to display all known
  2531. packages, or a list of packages.
  2532. Built-in packages are converted with `package--from-builtin'."
  2533. (unless packages (setq packages t))
  2534. (let (name)
  2535. ;; Installed packages:
  2536. (dolist (elt package-alist)
  2537. (setq name (car elt))
  2538. (when (or (eq packages t) (memq name packages))
  2539. (mapc function (cdr elt))))
  2540. ;; Built-in packages:
  2541. (dolist (elt package--builtins)
  2542. (setq name (car elt))
  2543. (when (and (not (eq name 'emacs)) ; Hide the `emacs' package.
  2544. (or package-list-unversioned
  2545. (package--bi-desc-version (cdr elt)))
  2546. (or (eq packages t) (memq name packages)))
  2547. (funcall function (package--from-builtin elt))))
  2548. ;; Available and disabled packages:
  2549. (dolist (elt package-archive-contents)
  2550. (setq name (car elt))
  2551. (when (or (eq packages t) (memq name packages))
  2552. (dolist (pkg (cdr elt))
  2553. ;; Hide obsolete packages.
  2554. (unless (package-installed-p (package-desc-name pkg)
  2555. (package-desc-version pkg))
  2556. (funcall function pkg)))))))
  2557. (defun package--has-keyword-p (desc &optional keywords)
  2558. "Test if package DESC has any of the given KEYWORDS.
  2559. When none are given, the package matches."
  2560. (if keywords
  2561. (let ((desc-keywords (and desc (package-desc--keywords desc)))
  2562. found)
  2563. (while (and (not found) keywords)
  2564. (let ((k (pop keywords)))
  2565. (setq found
  2566. (or (string= k (concat "arc:" (package-desc-archive desc)))
  2567. (string= k (concat "status:" (package-desc-status desc)))
  2568. (member k desc-keywords)))))
  2569. found)
  2570. t))
  2571. (defun package-menu--generate (remember-pos packages &optional keywords)
  2572. "Populate the Package Menu.
  2573. If REMEMBER-POS is non-nil, keep point on the same entry.
  2574. PACKAGES should be t, which means to display all known packages,
  2575. or a list of package names (symbols) to display.
  2576. With KEYWORDS given, only packages with those keywords are
  2577. shown."
  2578. (package-menu--refresh packages keywords)
  2579. (setf (car (aref tabulated-list-format 0))
  2580. (if keywords
  2581. (let ((filters (mapconcat 'identity keywords ",")))
  2582. (concat "Package[" filters "]"))
  2583. "Package"))
  2584. (if keywords
  2585. (define-key package-menu-mode-map "q" 'package-show-package-list)
  2586. (define-key package-menu-mode-map "q" 'quit-window))
  2587. (tabulated-list-init-header)
  2588. (tabulated-list-print remember-pos))
  2589. (defun package-menu--print-info (pkg)
  2590. "Return a package entry suitable for `tabulated-list-entries'.
  2591. PKG has the form (PKG-DESC . STATUS).
  2592. Return (PKG-DESC [NAME VERSION STATUS DOC])."
  2593. (package-menu--print-info-simple (car pkg)))
  2594. (make-obsolete 'package-menu--print-info
  2595. 'package-menu--print-info-simple "25.1")
  2596. ;;; Package menu faces
  2597. (defface package-name
  2598. '((t :inherit link))
  2599. "Face used on package names in the package menu."
  2600. :version "25.1")
  2601. (defface package-description
  2602. '((t :inherit default))
  2603. "Face used on package description summaries in the package menu."
  2604. :version "25.1")
  2605. ;; Shame this hyphenates "built-in", when "font-lock-builtin-face" doesn't.
  2606. (defface package-status-built-in
  2607. '((t :inherit font-lock-builtin-face))
  2608. "Face used on the status and version of built-in packages."
  2609. :version "25.1")
  2610. (defface package-status-external
  2611. '((t :inherit package-status-built-in))
  2612. "Face used on the status and version of external packages."
  2613. :version "25.1")
  2614. (defface package-status-available
  2615. '((t :inherit default))
  2616. "Face used on the status and version of available packages."
  2617. :version "25.1")
  2618. (defface package-status-new
  2619. '((t :inherit (bold package-status-available)))
  2620. "Face used on the status and version of new packages."
  2621. :version "25.1")
  2622. (defface package-status-held
  2623. '((t :inherit font-lock-constant-face))
  2624. "Face used on the status and version of held packages."
  2625. :version "25.1")
  2626. (defface package-status-disabled
  2627. '((t :inherit font-lock-warning-face))
  2628. "Face used on the status and version of disabled packages."
  2629. :version "25.1")
  2630. (defface package-status-installed
  2631. '((t :inherit font-lock-comment-face))
  2632. "Face used on the status and version of installed packages."
  2633. :version "25.1")
  2634. (defface package-status-dependency
  2635. '((t :inherit package-status-installed))
  2636. "Face used on the status and version of dependency packages."
  2637. :version "25.1")
  2638. (defface package-status-unsigned
  2639. '((t :inherit font-lock-warning-face))
  2640. "Face used on the status and version of unsigned packages."
  2641. :version "25.1")
  2642. (defface package-status-incompat
  2643. '((t :inherit error))
  2644. "Face used on the status and version of incompat packages."
  2645. :version "25.1")
  2646. (defface package-status-avail-obso
  2647. '((t :inherit package-status-incompat))
  2648. "Face used on the status and version of avail-obso packages."
  2649. :version "25.1")
  2650. ;;; Package menu printing
  2651. (defun package-menu--print-info-simple (pkg)
  2652. "Return a package entry suitable for `tabulated-list-entries'.
  2653. PKG is a `package-desc' object.
  2654. Return (PKG-DESC [NAME VERSION STATUS DOC])."
  2655. (let* ((status (package-desc-status pkg))
  2656. (face (pcase status
  2657. (`"built-in" 'package-status-built-in)
  2658. (`"external" 'package-status-external)
  2659. (`"available" 'package-status-available)
  2660. (`"avail-obso" 'package-status-avail-obso)
  2661. (`"new" 'package-status-new)
  2662. (`"held" 'package-status-held)
  2663. (`"disabled" 'package-status-disabled)
  2664. (`"installed" 'package-status-installed)
  2665. (`"dependency" 'package-status-dependency)
  2666. (`"unsigned" 'package-status-unsigned)
  2667. (`"incompat" 'package-status-incompat)
  2668. (_ 'font-lock-warning-face)))) ; obsolete.
  2669. (list pkg
  2670. `[(,(symbol-name (package-desc-name pkg))
  2671. face package-name
  2672. font-lock-face package-name
  2673. follow-link t
  2674. package-desc ,pkg
  2675. action package-menu-describe-package)
  2676. ,(propertize (package-version-join
  2677. (package-desc-version pkg))
  2678. 'font-lock-face face)
  2679. ,(propertize status 'font-lock-face face)
  2680. ,@(if (cdr package-archives)
  2681. (list (propertize (or (package-desc-archive pkg) "")
  2682. 'font-lock-face face)))
  2683. ,(propertize (package-desc-summary pkg)
  2684. 'font-lock-face 'package-description)])))
  2685. (defvar package-menu--old-archive-contents nil
  2686. "`package-archive-contents' before the latest refresh.")
  2687. (defun package-menu-refresh ()
  2688. "Download the Emacs Lisp package archive.
  2689. This fetches the contents of each archive specified in
  2690. `package-archives', and then refreshes the package menu."
  2691. (interactive)
  2692. (unless (derived-mode-p 'package-menu-mode)
  2693. (user-error "The current buffer is not a Package Menu"))
  2694. (setq package-menu--old-archive-contents package-archive-contents)
  2695. (setq package-menu--new-package-list nil)
  2696. (package-refresh-contents package-menu-async))
  2697. (defun package-menu-hide-package ()
  2698. "Hide a package under point.
  2699. If optional arg BUTTON is non-nil, describe its associated package."
  2700. (interactive)
  2701. (declare (interactive-only "change `package-hidden-regexps' instead."))
  2702. (let* ((name (when (derived-mode-p 'package-menu-mode)
  2703. (concat "\\`" (regexp-quote (symbol-name (package-desc-name
  2704. (tabulated-list-get-id)))))))
  2705. (re (read-string "Hide packages matching regexp: " name)))
  2706. ;; Test if it is valid.
  2707. (string-match re "")
  2708. (push re package-hidden-regexps)
  2709. (customize-save-variable 'package-hidden-regexps package-hidden-regexps)
  2710. (package-menu--post-refresh)
  2711. (let ((hidden
  2712. (cl-remove-if-not (lambda (e) (string-match re (symbol-name (car e))))
  2713. package-archive-contents)))
  2714. (message (substitute-command-keys
  2715. (concat "Hiding %s packages, type `\\[package-menu-toggle-hiding]'"
  2716. " to toggle or `\\[customize-variable] RET package-hidden-regexps'"
  2717. " to customize it"))
  2718. (length hidden)))))
  2719. (defun package-menu-describe-package (&optional button)
  2720. "Describe the current package.
  2721. If optional arg BUTTON is non-nil, describe its associated package."
  2722. (interactive)
  2723. (let ((pkg-desc (if button (button-get button 'package-desc)
  2724. (tabulated-list-get-id))))
  2725. (if pkg-desc
  2726. (describe-package pkg-desc)
  2727. (user-error "No package here"))))
  2728. ;; fixme numeric argument
  2729. (defun package-menu-mark-delete (&optional _num)
  2730. "Mark a package for deletion and move to the next line."
  2731. (interactive "p")
  2732. (if (member (package-menu-get-status)
  2733. '("installed" "dependency" "obsolete" "unsigned"))
  2734. (tabulated-list-put-tag "D" t)
  2735. (forward-line)))
  2736. (defun package-menu-mark-install (&optional _num)
  2737. "Mark a package for installation and move to the next line."
  2738. (interactive "p")
  2739. (if (member (package-menu-get-status) '("available" "avail-obso" "new" "dependency"))
  2740. (tabulated-list-put-tag "I" t)
  2741. (forward-line)))
  2742. (defun package-menu-mark-unmark (&optional _num)
  2743. "Clear any marks on a package and move to the next line."
  2744. (interactive "p")
  2745. (tabulated-list-put-tag " " t))
  2746. (defun package-menu-backup-unmark ()
  2747. "Back up one line and clear any marks on that package."
  2748. (interactive)
  2749. (forward-line -1)
  2750. (tabulated-list-put-tag " "))
  2751. (defun package-menu-mark-obsolete-for-deletion ()
  2752. "Mark all obsolete packages for deletion."
  2753. (interactive)
  2754. (save-excursion
  2755. (goto-char (point-min))
  2756. (while (not (eobp))
  2757. (if (equal (package-menu-get-status) "obsolete")
  2758. (tabulated-list-put-tag "D" t)
  2759. (forward-line 1)))))
  2760. (defvar package--quick-help-keys
  2761. '(("install," "delete," "unmark," ("execute" . 1))
  2762. ("next," "previous")
  2763. ("Hide-package," "(-toggle-hidden")
  2764. ("refresh-contents," "g-redisplay," "filter," "help")))
  2765. (defun package--prettify-quick-help-key (desc)
  2766. "Prettify DESC to be displayed as a help menu."
  2767. (if (listp desc)
  2768. (if (listp (cdr desc))
  2769. (mapconcat #'package--prettify-quick-help-key desc " ")
  2770. (let ((place (cdr desc))
  2771. (out (car desc)))
  2772. (add-text-properties place (1+ place)
  2773. '(face (bold font-lock-warning-face))
  2774. out)
  2775. out))
  2776. (package--prettify-quick-help-key (cons desc 0))))
  2777. (defun package-menu-quick-help ()
  2778. "Show short key binding help for `package-menu-mode'.
  2779. The full list of keys can be viewed with \\[describe-mode]."
  2780. (interactive)
  2781. (message (mapconcat #'package--prettify-quick-help-key
  2782. package--quick-help-keys "\n")))
  2783. (define-obsolete-function-alias
  2784. 'package-menu-view-commentary 'package-menu-describe-package "24.1")
  2785. (defun package-menu-get-status ()
  2786. (let* ((id (tabulated-list-get-id))
  2787. (entry (and id (assoc id tabulated-list-entries))))
  2788. (if entry
  2789. (aref (cadr entry) 2)
  2790. "")))
  2791. (defun package-archive-priority (archive)
  2792. "Return the priority of ARCHIVE.
  2793. The archive priorities are specified in
  2794. `package-archive-priorities'. If not given there, the priority
  2795. defaults to 0."
  2796. (or (cdr (assoc archive package-archive-priorities))
  2797. 0))
  2798. (defun package-desc-priority-version (pkg-desc)
  2799. "Return the version PKG-DESC with the archive priority prepended.
  2800. This allows for easy comparison of package versions from
  2801. different archives if archive priorities are meant to be taken in
  2802. consideration."
  2803. (cons (package-desc-priority pkg-desc)
  2804. (package-desc-version pkg-desc)))
  2805. (defun package-menu--find-upgrades ()
  2806. (let (installed available upgrades)
  2807. ;; Build list of installed/available packages in this buffer.
  2808. (dolist (entry tabulated-list-entries)
  2809. ;; ENTRY is (PKG-DESC [NAME VERSION STATUS DOC])
  2810. (let ((pkg-desc (car entry))
  2811. (status (aref (cadr entry) 2)))
  2812. (cond ((member status '("installed" "dependency" "unsigned"))
  2813. (push pkg-desc installed))
  2814. ((member status '("available" "new"))
  2815. (setq available (package--append-to-alist pkg-desc available))))))
  2816. ;; Loop through list of installed packages, finding upgrades.
  2817. (dolist (pkg-desc installed)
  2818. (let* ((name (package-desc-name pkg-desc))
  2819. (avail-pkg (cadr (assq name available))))
  2820. (and avail-pkg
  2821. (version-list-< (package-desc-priority-version pkg-desc)
  2822. (package-desc-priority-version avail-pkg))
  2823. (push (cons name avail-pkg) upgrades))))
  2824. upgrades))
  2825. (defvar package-menu--mark-upgrades-pending nil
  2826. "Whether mark-upgrades is waiting for a refresh to finish.")
  2827. (defun package-menu--mark-upgrades-1 ()
  2828. "Mark all upgradable packages in the Package Menu.
  2829. Implementation of `package-menu-mark-upgrades'."
  2830. (unless (derived-mode-p 'package-menu-mode)
  2831. (error "The current buffer is not a Package Menu"))
  2832. (setq package-menu--mark-upgrades-pending nil)
  2833. (let ((upgrades (package-menu--find-upgrades)))
  2834. (if (null upgrades)
  2835. (message "No packages to upgrade.")
  2836. (widen)
  2837. (save-excursion
  2838. (goto-char (point-min))
  2839. (while (not (eobp))
  2840. (let* ((pkg-desc (tabulated-list-get-id))
  2841. (upgrade (cdr (assq (package-desc-name pkg-desc) upgrades))))
  2842. (cond ((null upgrade)
  2843. (forward-line 1))
  2844. ((equal pkg-desc upgrade)
  2845. (package-menu-mark-install))
  2846. (t
  2847. (package-menu-mark-delete))))))
  2848. (message "%d package%s marked for upgrading."
  2849. (length upgrades)
  2850. (if (= (length upgrades) 1) "" "s")))))
  2851. (defun package-menu-mark-upgrades ()
  2852. "Mark all upgradable packages in the Package Menu.
  2853. For each installed package with a newer version available, place
  2854. an (I)nstall flag on the available version and a (D)elete flag on
  2855. the installed version. A subsequent \\[package-menu-execute]
  2856. call will upgrade the package.
  2857. If there's an async refresh operation in progress, the flags will
  2858. be placed as part of `package-menu--post-refresh' instead of
  2859. immediately."
  2860. (interactive)
  2861. (if (not package--downloads-in-progress)
  2862. (package-menu--mark-upgrades-1)
  2863. (setq package-menu--mark-upgrades-pending t)
  2864. (message "Waiting for refresh to finish...")))
  2865. (defun package-menu--list-to-prompt (packages)
  2866. "Return a string listing PACKAGES that's usable in a prompt.
  2867. PACKAGES is a list of `package-desc' objects.
  2868. Formats the returned string to be usable in a minibuffer
  2869. prompt (see `package-menu--prompt-transaction-p')."
  2870. (cond
  2871. ;; None
  2872. ((not packages) "")
  2873. ;; More than 1
  2874. ((cdr packages)
  2875. (format "these %d packages (%s)"
  2876. (length packages)
  2877. (mapconcat #'package-desc-full-name packages ", ")))
  2878. ;; Exactly 1
  2879. (t (format-message "package `%s'"
  2880. (package-desc-full-name (car packages))))))
  2881. (defun package-menu--prompt-transaction-p (delete install upgrade)
  2882. "Prompt the user about DELETE, INSTALL, and UPGRADE.
  2883. DELETE, INSTALL, and UPGRADE are lists of `package-desc' objects.
  2884. Either may be nil, but not all."
  2885. (y-or-n-p
  2886. (concat
  2887. (when delete "Delete ")
  2888. (package-menu--list-to-prompt delete)
  2889. (when (and delete install)
  2890. (if upgrade "; " "; and "))
  2891. (when install "Install ")
  2892. (package-menu--list-to-prompt install)
  2893. (when (and upgrade (or install delete)) "; and ")
  2894. (when upgrade "Upgrade ")
  2895. (package-menu--list-to-prompt upgrade)
  2896. "? ")))
  2897. (defun package-menu--partition-transaction (install delete)
  2898. "Return an alist describing an INSTALL DELETE transaction.
  2899. Alist contains three entries, upgrade, delete, and install, each
  2900. with a list of package names.
  2901. The upgrade entry contains any `package-desc' objects in INSTALL
  2902. whose name coincides with an object in DELETE. The delete and
  2903. the install entries are the same as DELETE and INSTALL with such
  2904. objects removed."
  2905. (let* ((upg (cl-intersection install delete :key #'package-desc-name))
  2906. (ins (cl-set-difference install upg :key #'package-desc-name))
  2907. (del (cl-set-difference delete upg :key #'package-desc-name)))
  2908. `((delete . ,del) (install . ,ins) (upgrade . ,upg))))
  2909. (defun package-menu--perform-transaction (install-list delete-list)
  2910. "Install packages in INSTALL-LIST and delete DELETE-LIST."
  2911. (if install-list
  2912. (let ((status-format (format ":Installing %%d/%d"
  2913. (length install-list)))
  2914. (i 0)
  2915. (package-menu--transaction-status))
  2916. (dolist (pkg install-list)
  2917. (setq package-menu--transaction-status
  2918. (format status-format (cl-incf i)))
  2919. (force-mode-line-update)
  2920. (redisplay 'force)
  2921. ;; Don't mark as selected, `package-menu-execute' already
  2922. ;; does that.
  2923. (package-install pkg 'dont-select))))
  2924. (let ((package-menu--transaction-status ":Deleting"))
  2925. (force-mode-line-update)
  2926. (redisplay 'force)
  2927. (dolist (elt (package--sort-by-dependence delete-list))
  2928. (condition-case-unless-debug err
  2929. (let ((inhibit-message package-menu-async))
  2930. (package-delete elt nil 'nosave))
  2931. (error (message "Error trying to delete `%s': %S"
  2932. (package-desc-full-name elt)
  2933. err))))))
  2934. (defun package--update-selected-packages (add remove)
  2935. "Update the `package-selected-packages' list according to ADD and REMOVE.
  2936. ADD and REMOVE must be disjoint lists of package names (or
  2937. `package-desc' objects) to be added and removed to the selected
  2938. packages list, respectively."
  2939. (dolist (p add)
  2940. (cl-pushnew (if (package-desc-p p) (package-desc-name p) p)
  2941. package-selected-packages))
  2942. (dolist (p remove)
  2943. (setq package-selected-packages
  2944. (remove (if (package-desc-p p) (package-desc-name p) p)
  2945. package-selected-packages)))
  2946. (when (or add remove)
  2947. (package--save-selected-packages package-selected-packages)))
  2948. (defun package-menu-execute (&optional noquery)
  2949. "Perform marked Package Menu actions.
  2950. Packages marked for installation are downloaded and installed;
  2951. packages marked for deletion are removed.
  2952. Optional argument NOQUERY non-nil means do not ask the user to confirm."
  2953. (interactive)
  2954. (unless (derived-mode-p 'package-menu-mode)
  2955. (error "The current buffer is not in Package Menu mode"))
  2956. (let (install-list delete-list cmd pkg-desc)
  2957. (save-excursion
  2958. (goto-char (point-min))
  2959. (while (not (eobp))
  2960. (setq cmd (char-after))
  2961. (unless (eq cmd ?\s)
  2962. ;; This is the key PKG-DESC.
  2963. (setq pkg-desc (tabulated-list-get-id))
  2964. (cond ((eq cmd ?D)
  2965. (push pkg-desc delete-list))
  2966. ((eq cmd ?I)
  2967. (push pkg-desc install-list))))
  2968. (forward-line)))
  2969. (unless (or delete-list install-list)
  2970. (user-error "No operations specified"))
  2971. (let-alist (package-menu--partition-transaction install-list delete-list)
  2972. (when (or noquery
  2973. (package-menu--prompt-transaction-p .delete .install .upgrade))
  2974. (let ((message-template
  2975. (concat "Package menu: Operation %s ["
  2976. (when .delete (format "Delet__ %s" (length .delete)))
  2977. (when (and .delete .install) "; ")
  2978. (when .install (format "Install__ %s" (length .install)))
  2979. (when (and .upgrade (or .install .delete)) "; ")
  2980. (when .upgrade (format "Upgrad__ %s" (length .upgrade)))
  2981. "]")))
  2982. (message (replace-regexp-in-string "__" "ing" message-template) "started")
  2983. ;; Packages being upgraded are not marked as selected.
  2984. (package--update-selected-packages .install .delete)
  2985. (package-menu--perform-transaction install-list delete-list)
  2986. (when package-selected-packages
  2987. (if-let ((removable (package--removable-packages)))
  2988. (message "Package menu: Operation finished. %d packages %s"
  2989. (length removable)
  2990. (substitute-command-keys
  2991. "are no longer needed, type `\\[package-autoremove]' to remove them"))
  2992. (message (replace-regexp-in-string "__" "ed" message-template)
  2993. "finished"))))))))
  2994. (defun package-menu--version-predicate (A B)
  2995. (let ((vA (or (aref (cadr A) 1) '(0)))
  2996. (vB (or (aref (cadr B) 1) '(0))))
  2997. (if (version-list-= vA vB)
  2998. (package-menu--name-predicate A B)
  2999. (version-list-< vA vB))))
  3000. (defun package-menu--status-predicate (A B)
  3001. (let ((sA (aref (cadr A) 2))
  3002. (sB (aref (cadr B) 2)))
  3003. (cond ((string= sA sB)
  3004. (package-menu--name-predicate A B))
  3005. ((string= sA "new") t)
  3006. ((string= sB "new") nil)
  3007. ((string-prefix-p "avail" sA)
  3008. (if (string-prefix-p "avail" sB)
  3009. (package-menu--name-predicate A B)
  3010. t))
  3011. ((string-prefix-p "avail" sB) nil)
  3012. ((string= sA "installed") t)
  3013. ((string= sB "installed") nil)
  3014. ((string= sA "dependency") t)
  3015. ((string= sB "dependency") nil)
  3016. ((string= sA "unsigned") t)
  3017. ((string= sB "unsigned") nil)
  3018. ((string= sA "held") t)
  3019. ((string= sB "held") nil)
  3020. ((string= sA "external") t)
  3021. ((string= sB "external") nil)
  3022. ((string= sA "built-in") t)
  3023. ((string= sB "built-in") nil)
  3024. ((string= sA "obsolete") t)
  3025. ((string= sB "obsolete") nil)
  3026. ((string= sA "incompat") t)
  3027. ((string= sB "incompat") nil)
  3028. (t (string< sA sB)))))
  3029. (defun package-menu--description-predicate (A B)
  3030. (let ((dA (aref (cadr A) 3))
  3031. (dB (aref (cadr B) 3)))
  3032. (if (string= dA dB)
  3033. (package-menu--name-predicate A B)
  3034. (string< dA dB))))
  3035. (defun package-menu--name-predicate (A B)
  3036. (string< (symbol-name (package-desc-name (car A)))
  3037. (symbol-name (package-desc-name (car B)))))
  3038. (defun package-menu--archive-predicate (A B)
  3039. (string< (or (package-desc-archive (car A)) "")
  3040. (or (package-desc-archive (car B)) "")))
  3041. (defun package-menu--populate-new-package-list ()
  3042. "Decide which packages are new in `package-archives-contents'.
  3043. Store this list in `package-menu--new-package-list'."
  3044. ;; Find which packages are new.
  3045. (when package-menu--old-archive-contents
  3046. (dolist (elt package-archive-contents)
  3047. (unless (assq (car elt) package-menu--old-archive-contents)
  3048. (push (car elt) package-menu--new-package-list)))
  3049. (setq package-menu--old-archive-contents nil)))
  3050. (defun package-menu--find-and-notify-upgrades ()
  3051. "Notify the user of upgradable packages."
  3052. (when-let ((upgrades (package-menu--find-upgrades)))
  3053. (message "%d package%s can be upgraded; type `%s' to mark %s for upgrading."
  3054. (length upgrades)
  3055. (if (= (length upgrades) 1) "" "s")
  3056. (substitute-command-keys "\\[package-menu-mark-upgrades]")
  3057. (if (= (length upgrades) 1) "it" "them"))))
  3058. (defun package-menu--post-refresh ()
  3059. "If there's a *Packages* buffer, revert it and check for new packages and upgrades.
  3060. Do nothing if there's no *Packages* buffer.
  3061. This function is called after `package-refresh-contents' and it
  3062. is added to `post-command-hook' by any function which alters the
  3063. package database (`package-install' and `package-delete'). When
  3064. run, it removes itself from `post-command-hook'."
  3065. (remove-hook 'post-command-hook #'package-menu--post-refresh)
  3066. (let ((buf (get-buffer "*Packages*")))
  3067. (when (buffer-live-p buf)
  3068. (with-current-buffer buf
  3069. (package-menu--populate-new-package-list)
  3070. (run-hooks 'tabulated-list-revert-hook)
  3071. (tabulated-list-print 'remember 'update)))))
  3072. (defun package-menu--mark-or-notify-upgrades ()
  3073. "If there's a *Packages* buffer, check for upgrades and possibly mark them.
  3074. Do nothing if there's no *Packages* buffer. If there are
  3075. upgrades, mark them if `package-menu--mark-upgrades-pending' is
  3076. non-nil, otherwise just notify the user that there are upgrades.
  3077. This function is called after `package-refresh-contents'."
  3078. (let ((buf (get-buffer "*Packages*")))
  3079. (when (buffer-live-p buf)
  3080. (with-current-buffer buf
  3081. (if package-menu--mark-upgrades-pending
  3082. (package-menu--mark-upgrades-1)
  3083. (package-menu--find-and-notify-upgrades))))))
  3084. ;;;###autoload
  3085. (defun list-packages (&optional no-fetch)
  3086. "Display a list of packages.
  3087. This first fetches the updated list of packages before
  3088. displaying, unless a prefix argument NO-FETCH is specified.
  3089. The list is displayed in a buffer named `*Packages*'."
  3090. (interactive "P")
  3091. (require 'finder-inf nil t)
  3092. ;; Initialize the package system if necessary.
  3093. (unless package--initialized
  3094. (package-initialize t))
  3095. ;; Integrate the package-menu with updating the archives.
  3096. (add-hook 'package--post-download-archives-hook
  3097. #'package-menu--post-refresh)
  3098. (add-hook 'package--post-download-archives-hook
  3099. #'package-menu--mark-or-notify-upgrades 'append)
  3100. ;; Generate the Package Menu.
  3101. (let ((buf (get-buffer-create "*Packages*")))
  3102. (with-current-buffer buf
  3103. (package-menu-mode)
  3104. ;; Fetch the remote list of packages.
  3105. (unless no-fetch (package-menu-refresh))
  3106. ;; If we're not async, this would be redundant.
  3107. (when package-menu-async
  3108. (package-menu--generate nil t)))
  3109. ;; The package menu buffer has keybindings. If the user types
  3110. ;; `M-x list-packages', that suggests it should become current.
  3111. (switch-to-buffer buf)))
  3112. ;;;###autoload
  3113. (defalias 'package-list-packages 'list-packages)
  3114. ;; Used in finder.el
  3115. (defun package-show-package-list (&optional packages keywords)
  3116. "Display PACKAGES in a *Packages* buffer.
  3117. This is similar to `list-packages', but it does not fetch the
  3118. updated list of packages, and it only displays packages with
  3119. names in PACKAGES (which should be a list of symbols).
  3120. When KEYWORDS are given, only packages with those KEYWORDS are
  3121. shown."
  3122. (interactive)
  3123. (require 'finder-inf nil t)
  3124. (let* ((buf (get-buffer-create "*Packages*"))
  3125. (win (get-buffer-window buf)))
  3126. (with-current-buffer buf
  3127. (package-menu-mode)
  3128. (package-menu--generate nil packages keywords))
  3129. (if win
  3130. (select-window win)
  3131. (switch-to-buffer buf))))
  3132. ;; package-menu--generate rebinds "q" on the fly, so we have to
  3133. ;; hard-code the binding in the doc-string here.
  3134. (defun package-menu-filter (keyword)
  3135. "Filter the *Packages* buffer.
  3136. Show only those items that relate to the specified KEYWORD.
  3137. KEYWORD can be a string or a list of strings. If it is a list, a
  3138. package will be displayed if it matches any of the keywords.
  3139. Interactively, it is a list of strings separated by commas.
  3140. To restore the full package list, type `q'."
  3141. (interactive
  3142. (list (completing-read-multiple
  3143. "Keywords (comma separated): " (package-all-keywords))))
  3144. (package-show-package-list t (if (stringp keyword)
  3145. (list keyword)
  3146. keyword)))
  3147. (defun package-list-packages-no-fetch ()
  3148. "Display a list of packages.
  3149. Does not fetch the updated list of packages before displaying.
  3150. The list is displayed in a buffer named `*Packages*'."
  3151. (interactive)
  3152. (list-packages t))
  3153. (provide 'package)
  3154. ;;; package.el ends here