Improve TTY library detection
[sxemacs] / lisp / packages.el
1 ;;; packages.el --- Low level support for SXEmacs packages
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 2002, 2003 Ben Wing.
5 ;; Copyright (C) 2004 - 2015 Steve Youngs
6
7 ;; Author: Steven L Baur <steve@xemacs.org>
8 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
9 ;; Keywords: internal, lisp, dumped
10
11 ;; This file is part of SXEmacs.
12
13 ;; SXEmacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; SXEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Synched up with: Not in FSF
27
28 ;;; Commentary:
29
30 ;; This file is dumped with SXEmacs.
31
32 ;; This file provides low level facilities for XEmacs startup --
33 ;; particularly regarding the package setup.  This code has to run in
34 ;; what we call "bare temacs" -- i.e. XEmacs without the usual Lisp
35 ;; environment.  Pay special attention:
36
37 ;; - not to use the `lambda' macro.  Use #'(lambda ...) instead.
38 ;;   (this goes for any package loaded before `subr.el'.)
39 ;;
40 ;; - not to use macros, because they are not yet available (and this
41 ;;   file must be loadable uncompiled.)  Built in macros, such as
42 ;;   `when' and `unless' are fine, of course.
43 ;;
44 ;; - not to use `defcustom'.  If you must add user-customizable
45 ;;   variables here, use `defvar', and add the variable to
46 ;;   `cus-start.el'.
47
48 ;; Because of all this, make sure that the stuff you put here really
49 ;; belongs here.
50
51 ;; This file requires find-paths.el.
52 \f
53 ;;; Code:
54
55 ;;; Package versioning
56
57 (defvar packages-package-list nil
58   "Database of installed packages and version numbers")
59
60 (defvar packages-hierarchy-depth 1
61   "Depth of package hierarchies.")
62
63 (defvar packages-load-path-depth 1
64   "Depth of load-path search in package hierarchies.")
65
66 (defvar packages-data-path-depth 1
67   "Depth of data-path search in package hierarchies.")
68
69 (defvar early-packages nil
70   "Packages early in the load path.")
71
72 (defvar early-package-load-path nil
73   "Load path for packages early in the load path.")
74
75 (defvar late-packages nil
76   "Packages late in the load path.")
77
78 (defvar late-package-load-path nil
79   "Load path for packages late in the load path.")
80
81 (defvar last-packages nil
82   "Packages last in the load path.")
83
84 (defvar last-package-load-path nil
85   "Load path for packages last in the load path.")
86
87 (defun packages-find-user-topdir ()
88   "Return the top of the user's local package hierachy.
89
90 This should be the equivalent of `$XDG_DATA_HOME/sxemacs'.  But will
91 fall back to the old legacy directory, `~/.sxemacs', if that is the
92 value of `user-init-directory'.  It may also be in the location set
93 via the `-user-pkgs-directory' command line arg."
94   (let* ((legacy (getenv "SXE_USE_LEGACY"))
95          (xdg (getenv "XDG_DATA_HOME"))
96          (xdgdir (or (and xdg
97                           (paths-construct-path `(,xdg "sxemacs")))
98                      (paths-construct-path
99                       `(,(user-home-directory)
100                             ".local" "share" "sxemacs"))))
101          warndirs)
102     ;; Set it, if not already.
103     (when (null user-packages-topdir)
104       (if (or legacy
105               (string= (expand-file-name "~/.sxemacs")
106                        (expand-file-name user-init-directory)))
107           (setq user-packages-topdir user-init-directory)
108         (setq user-packages-topdir (file-name-as-directory xdgdir))))
109     ;; Create if needed.
110     (unless (paths-file-readable-directory-p user-packages-topdir)
111       (make-directory-path user-packages-topdir))
112     ;; Warn if a user has pkg dirs in 2 or more of ~/.sxemacs,
113     ;; XDG_CONFIG_HOME, XDG_DATA_HOME, but not for symlinked dirs.
114     (let* ((regexp #r"\(s?xemacs\|site\|mule\)-packages")
115            (cpkgd (or (and (getenv "XDG_CONFIG_HOME")
116                            (paths-construct-path
117                             `(,(getenv "XDG_CONFIG_HOME") "sxemacs")))
118                       (paths-construct-path
119                        `(,(user-home-directory) ".config" "sxemacs")))))
120       ;; ~/.sxemacs
121       (when (and (not (file-symlink-p "~/.sxemacs"))
122                  (file-directory-p "~/.sxemacs")
123                  (directory-files "~/.sxemacs" nil regexp nil 'subdir))
124         (push "~/.sxemacs" warndirs))
125       ;; XDG_DATA_HOME
126       (when (and (not (file-symlink-p xdgdir))
127                  (file-directory-p xdgdir)
128                  (directory-files xdgdir nil regexp nil 'subdir))
129         (push xdgdir warndirs))
130       ;; XDG_CONFIG_HOME
131       (when (and (not (file-symlink-p cpkgd))
132                  (file-directory-p cpkgd)
133                  (directory-files cpkgd nil regexp nil 'subdir))
134         (push cpkgd warndirs))
135       (when (> (length warndirs) 1)
136         (lwarn 'multi-pkgd nil
137           "Multiple user package hierarchies detected!!
138
139 The following directories contain what appears to be package subdirs:
140 %S.
141
142 Currently in use: `%s'
143
144 This, in itself, is not necessarily a problem, but it may mean that
145 some of your packages won't be visible to this SXEmacs instance.  You
146 should keep all of your local packages in a single location.
147
148 See `display-warning-suppressed-classes' to suppress this warning"
149           warndirs user-packages-topdir)))
150     ;; Return the dir
151     user-packages-topdir))
152
153 (defun packages-compute-package-locations (user-packages-topdir)
154   "Compute locations of the various package directories.
155
156 Argument USER-PACKAGES-TOPDIR is the top of the user's local package
157 hierarchy.  It would normally be the equivalent of
158 `$XDG_DATA_HOME/sxemacs'.
159
160 This is a list, each of whose elements describes one directory.  A
161 directory description is a three-element list.
162
163 The first element is either an absolute path or a subdirectory
164 in the XEmacs hierarchy.
165
166 The second element is one of the symbols EARLY, LATE, LAST,
167 depending on the load-path segment the hierarchy is supposed to
168 show up in.
169
170 The third element is a thunk which, if it returns NIL, causes the
171 directory to be ignored."
172   (list
173    (list (paths-construct-path (list user-packages-topdir "site-packages"))
174          'early #'(lambda () t))
175    (list (paths-construct-path (list user-packages-topdir "sxemacs-packages"))
176          'early #'(lambda () t))
177    (list (paths-construct-path (list user-packages-topdir "infodock-packages"))
178          'early #'(lambda () (featurep 'infodock)))
179    (list (paths-construct-path (list user-packages-topdir "mule-packages"))
180          'early #'(lambda () (featurep 'mule)))
181    (list (paths-construct-path (list user-packages-topdir "xemacs-packages"))
182          'early #'(lambda () t))
183    (list "site-packages"     'late  #'(lambda () t))
184    (list "sxemacs-packages"  'late  #'(lambda () t))
185    (list "infodock-packages" 'late  #'(lambda () (featurep 'infodock)))
186    (list "mule-packages"     'late  #'(lambda () (featurep 'mule)))
187    (list "xemacs-packages"   'late  #'(lambda () t))))
188
189 (defun package-get-key-1 (info key)
190   "Locate keyword `key' in list."
191   (cond ((null info)
192          nil)
193         ((eq (car info) key)
194          (nth 1 info))
195         (t (package-get-key-1 (cddr info) key))))
196
197 (defun package-get-key (name key)
198   "Get info `key' from package `name'."
199   (let ((info (assq name packages-package-list)))
200     (when info
201       (package-get-key-1 (cdr info) key))))
202
203 (defun package-provide (name &rest attributes)
204   (let ((info (if (and attributes (floatp (car attributes)))
205                   (list :version (car attributes))
206                 attributes)))
207     (setq packages-package-list
208           (cons (cons name info) (remassq name packages-package-list)))))
209
210 (defun package-suppress (package file form)
211   "Set up a package-suppress condition FORM for FILE in PACKAGE.
212 When SXEmacs searches for a file in the load path, it will ignore FILE
213 if FORM evaluates to non-nil."
214   (setq load-suppress-alist
215         (acons (expand-file-name file (file-dirname load-file-name)) form
216                load-suppress-alist)))
217
218 (defun package-require (name version)
219   (let ((pkg (assq name packages-package-list)))
220     (cond ((null pkg)
221            (error 'invalid-state
222                   (format "Package %s has not been loaded into this XEmacsen"
223                           name)))
224           ((< (package-get-key name :version) version)
225            (error 'search-failed
226                   (format "Need version %g of package %s, got version %g"
227                           version name (package-get-key name :version))))
228           (t t))))
229
230 (defun package-delete-name (name)
231   (let (pkg)
232     ;; Delete ALL versions of package.
233     ;; This is pretty memory-intensive, as we use copy-alist when deleting
234     ;; package entries, to prevent side-effects in functions that call this
235     ;; one.
236     (while (setq pkg (assq name packages-package-list))
237       (setq packages-package-list (delete pkg (copy-alist
238                                                packages-package-list))))))
239
240 ;;; Build time stuff
241
242 (defvar autoload-file-name "auto-autoloads.el"
243   "Filename that autoloads are expected to be found in.")
244
245 ;; Moved from help.el.
246 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
247 ;; primitive, which should make it lightning-fast.
248
249 (defun locate-library (library &optional nosuffix path interactive-call)
250   "Show the precise file name of Emacs library LIBRARY.
251 This command searches the directories in `load-path' like `M-x load-library'
252 to find the file that `M-x load-library RET LIBRARY RET' would load.
253 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
254 to the specified name LIBRARY.
255
256 If the optional third arg PATH is specified, that list of directories
257 is used instead of `load-path'."
258   (interactive (list (read-library-name "Locate library: ")
259                      nil nil
260                      t))
261   (let ((result
262          (locate-file
263           library
264           (or path load-path)
265           (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
266                      (and (boundp 'find-file-hooks)
267                           (member 'crypt-find-file-hook find-file-hooks)))
268                  ;; Compression involved.
269                  (if nosuffix
270                      '("" ".gz" ".Z" ".bz2")
271                    '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
272                      ".el" ".el.gz" ".el.Z" ".el.bz2"
273                      "" ".gz" ".Z" ".bz2")))
274                 (t
275                  ;; No compression.
276                  (if nosuffix
277                      ""
278                    '(".elc" ".el" "")))))))
279     (and interactive-call
280          (if result
281              (message "Library is file %s" result)
282            (message "No library %s in search path" library)))
283     result))
284
285 (defun packages-add-suffix (str)
286   (if (null (string-match #r"\.el\'" str))
287       (concat str ".elc")
288     str))
289
290 (defun packages-list-autoloads-path ()
291   "List autoloads from precomputed load-path."
292   (let ((path load-path)
293         autoloads)
294     (while path
295       (if (file-exists-p (concat (car path)
296                                  autoload-file-name))
297           (setq autoloads (cons (concat (car path)
298                                         autoload-file-name)
299                                 autoloads)))
300       (setq path (cdr path)))
301     autoloads))
302
303 (defun packages-list-autoloads (source-directory)
304   "List autoload files in (what will be) the normal lisp search path.
305 This function is used during build to find where the global symbol files so
306 they can be perused for their useful information."
307   (let ((files (directory-files (file-name-as-directory source-directory)
308                                 t ".*"))
309         file autolist)
310     ;; (print (prin1-to-string source-directory))
311     ;; (print (prin1-to-string files))
312     (while (setq file (car-safe files))
313       (if (and (file-directory-p file)
314                (file-exists-p (concat (file-name-as-directory file)
315                                       autoload-file-name)))
316           (setq autolist (cons (concat (file-name-as-directory file)
317                                        autoload-file-name)
318                                autolist)))
319       (setq files (cdr files)))
320     autolist))
321
322 ;; The following function cannot be called from a bare temacs
323 (defun packages-new-autoloads ()
324   "Return autoloads files that have been added or modified since XEmacs dump."
325   (require 'loadhist)
326   (let ((me (concat invocation-directory invocation-name))
327         (path load-path)
328         result dir)
329     (while path
330       (setq dir (file-truename (car path)))
331       (let ((autoload-file (file-name-sans-extension (concat
332                                                       dir
333                                                       autoload-file-name))))
334         ;; Check for:
335         ;; 1.  An auto-autoload file that hasn't provided a feature (because
336         ;;     it has been installed since XEmacs was dumped).
337         ;; 2.  auto-autoload.el being newer than the executable
338         ;; 3.  auto-autoload.elc being newer than the executable (the .el
339         ;;     could be missing or compressed)
340         (when (or (and (null (file-provides autoload-file))
341                        (or (file-exists-p (concat autoload-file ".elc"))
342                            (file-exists-p (concat autoload-file ".el"))))
343                   (and (file-newer-than-file-p (concat autoload-file ".el") me)
344                        (setq autoload-file (concat autoload-file ".el")))
345                   (and (file-newer-than-file-p (concat autoload-file
346                                                        ".elc")
347                                                me)
348                        (setq autoload-file (concat autoload-file ".elc"))))
349           (push autoload-file result)))
350       (setq path (cdr path)))
351     result))
352
353 ;; The following function cannot be called from a bare temacs
354 (defun packages-reload-autoloads ()
355   "Reload new or updated auto-autoloads files.
356 This is an extremely dangerous function to call after the user-init-files
357 is run.  Don't call it or you'll be sorry."
358   (let ((autoload-list (packages-new-autoloads)))
359     (while autoload-list
360       (let* ((autoload-file (car autoload-list))
361              (feature (car-safe (file-provides autoload-file))))
362         (when feature
363           ;; (message "(unload-feature %S)" feature)
364           (unload-feature feature))
365         (condition-case nil
366             (load autoload-file)
367           (t nil)))
368       (setq autoload-list (cdr autoload-list)))))
369
370 ;; Data-directory is really a list now.  Provide something to search it for
371 ;; directories.
372
373 (defun locate-data-directory-list (name &optional dir-list)
374   "Locate the matching list of directories in a search path DIR-LIST.
375 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
376   (unless dir-list
377     (setq dir-list data-directory-list))
378   (let (found found-dir found-dir-list)
379     (while dir-list
380       (setq found (file-name-as-directory (concat (car dir-list) name))
381             found-dir (file-directory-p found))
382       (and found-dir
383            (setq found-dir-list (cons found found-dir-list)))
384       (setq dir-list (cdr dir-list)))
385     (nreverse found-dir-list)))
386
387 ;; Data-directory is really a list now.  Provide something to search it for
388 ;; a directory.
389
390 (defun locate-data-directory (name &optional dir-list)
391   "Locate a directory in a search path DIR-LIST (a list of directories).
392 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
393   (unless dir-list
394     (setq dir-list data-directory-list))
395   (let (found found-dir)
396     (while (and (null found-dir) dir-list)
397       (setq found (file-name-as-directory (concat (car dir-list) name))
398             found-dir (file-directory-p found))
399       (or found-dir
400           (setq found nil))
401       (setq dir-list (cdr dir-list)))
402     found))
403
404 ;; Data-directory is really a list now.  Provide something to search it for
405 ;; files.
406
407 (defun locate-data-file (name &optional dir-list)
408   "Locate a file in a search path DIR-LIST (a list of directories).
409 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
410 This function is basically a wrapper over `locate-file'."
411   (locate-file name (or dir-list data-directory-list)))
412
413 ;; Path setup
414
415 (defun packages-find-package-directories (roots base)
416   "Find a set of package directories."
417   ;; make sure paths-find-version-directory and paths-find-site-directory
418   ;; don't both pick up version-independent directories ...
419   (let ((version-directory (paths-find-version-archindep-directory
420                             roots base nil nil t))
421         (site-directory (paths-find-site-archindep-directory roots base)))
422     (paths-uniq-append
423      (and version-directory (list version-directory))
424      (and site-directory (list site-directory)))))
425
426 (defvar packages-special-base-regexp #r"^\(etc\|info\|man\|lisp\|lib-src\|bin\|pkginfo\)$"
427   "Special subdirectories of packages.")
428
429 (defvar packages-no-package-hierarchy-regexp
430   (concat "\\(" paths-version-control-filename-regexp "\\)"
431           "\\|"
432           "\\(" packages-special-base-regexp "\\)")
433   "Directories which can't be the roots of package hierarchies.")
434
435 (defun packages-find-packages-in-directories (directories)
436   "Find all packages underneath directories in DIRECTORIES."
437   (paths-find-recursive-path directories
438                              packages-hierarchy-depth
439                              packages-no-package-hierarchy-regexp))
440
441 (defun packages-split-path (path)
442   "Split PATH at \"\", return pair with two components.
443 The second component is shared with PATH."
444   (let ((reverse-tail '())
445         (rest path))
446     (while (and rest (null (string-equal "" (car rest))))
447       (setq reverse-tail (cons (car rest) reverse-tail))
448       (setq rest (cdr rest)))
449     (if (null rest)
450         (cons path nil)
451       (cons (nreverse reverse-tail) (cdr rest)))))
452
453 (defun packages-split-package-path (package-path)
454   "Split up PACKAGE-PATH into early, late and last components.
455 The separation is by \"\" components.
456 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
457   ;; When in doubt, it's late
458   (let* ((stuff (packages-split-path package-path))
459          (early (and (cdr stuff) (car stuff)))
460          (late+last (or (cdr stuff) (car stuff)))
461          (stuff (packages-split-path late+last))
462          (late (car stuff))
463          (last (cdr stuff)))
464     (list (packages-find-packages-in-directories early)
465           (packages-find-packages-in-directories late)
466           (packages-find-packages-in-directories last))))
467
468 (defun packages-deconstruct (list consumer)
469   "Deconstruct LIST and feed it to CONSUMER."
470   (apply consumer list))
471
472 (defun packages-find-packages-by-name (roots name)
473   "Find a package hierarchy by its name."
474   (packages-find-packages-in-directories
475    (if (and (file-name-absolute-p name)
476             (file-name-directory (expand-file-name name)))
477        (list (file-name-as-directory (expand-file-name name)))
478     (packages-find-package-directories roots name))))
479
480 (defun packages-find-packages-at-time
481   (roots package-locations time &optional default)
482   "Find packages at given time.
483 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
484 TIME is either 'EARLY, 'LATE, or 'LAST.
485 DEFAULT is a default list of packages."
486   (or default
487       (let ((packages '()))
488         (while package-locations
489           (packages-deconstruct
490            (car package-locations)
491            #'(lambda (name a-time thunk)
492                (if (and (eq time a-time)
493                         (funcall thunk))
494                    (setq packages
495                          (nconc packages
496                                 (packages-find-packages-by-name roots name))))))
497           (setq package-locations (cdr package-locations)))
498         packages)))
499
500 (defun packages-find-packages (roots package-locations)
501   "Find the packages."
502   (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
503     (if envvar-value
504         (packages-split-package-path (paths-decode-directory-path envvar-value))
505       (packages-deconstruct
506        (packages-split-package-path configure-package-path)
507        #'(lambda (configure-early-packages
508                   configure-late-packages
509                   configure-last-packages)
510            (list (packages-find-packages-at-time roots package-locations 'early
511                                                  configure-early-packages)
512                  (packages-find-packages-at-time roots package-locations 'late
513                                                  configure-late-packages)
514                  (packages-find-packages-at-time roots package-locations 'last
515                                                  configure-last-packages)))))))
516
517 (defun packages-find-package-library-path (packages suffixes)
518   "Construct a path into a component of the packages hierarchy.
519 PACKAGES is a list of package directories.
520 SUFFIXES is a list of names of package subdirectories to look for."
521   (let ((directories
522          (apply
523           #'nconc
524           (mapcar #'(lambda (package)
525                       (mapcar #'(lambda (suffix)
526                                   (file-name-as-directory (concat package suffix)))
527                               suffixes))
528                   packages))))
529     (paths-directories-which-exist directories)))
530
531 (defun packages-find-package-load-path (packages)
532   "Construct the load-path component for packages.
533 PACKAGES is a list of package directories."
534   (paths-find-recursive-load-path
535    (packages-find-package-library-path packages
536                                        '("lisp"))
537    packages-load-path-depth))
538
539 (defun packages-find-package-exec-path (packages)
540   "Construct the exec-path component for packages.
541 PACKAGES is a list of package directories."
542   (packages-find-package-library-path packages
543                                       (list (paths-construct-path
544                                              (list "bin" system-configuration))
545                                             "lib-src")))
546
547 (defun packages-find-package-info-path (packages)
548   "Construct the info-path component for packages.
549 PACKAGES is a list of package directories."
550   (packages-find-package-library-path packages '("info")))
551
552 (defun packages-find-package-data-path (packages)
553   "Construct the data-path component for packages.
554 PACKAGES is a list of package directories."
555   (paths-find-recursive-load-path
556    (packages-find-package-library-path packages
557                                        '("etc"))
558    packages-data-path-depth))
559
560 ;; Loading package initialization files
561
562 (defun packages-load-package-lisps (package-load-path base)
563   "Load all Lisp files of a certain name along a load path.
564 BASE is the base name of the files."
565   (mapcar #'(lambda (dir)
566             (let ((file-name (expand-file-name base dir)))
567               (condition-case error
568                   (load file-name t t)
569                 (error
570                  (warn (format "Autoload error in: %s:\n\t%s"
571                                file-name
572                                (with-output-to-string
573                                  (display-error error nil))))))))
574         package-load-path))
575
576 (defun packages-load-package-auto-autoloads (package-load-path)
577   "Load auto-autoload files along a load path."
578   (packages-load-package-lisps package-load-path
579                                (file-name-sans-extension autoload-file-name)))
580
581 (defun packages-handle-package-dumped-lisps (handle package-load-path)
582   "Load dumped-lisp.el files along a load path.
583 Call HANDLE on each file off definitions of PACKAGE-LISP there."
584   (mapcar #'(lambda (dir)
585             (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
586               (if (file-exists-p file-name)
587                   (let (package-lisp
588                         ;; 20.4 packages could set this
589                         preloaded-file-list)
590                     (load file-name)
591                     ;; dumped-lisp.el could have set this ...
592                     (if package-lisp
593                         (mapcar #'(lambda (base)
594                                   (funcall handle base))
595                               package-lisp))))))
596         package-load-path))
597
598 (defun packages-load-package-dumped-lisps (package-load-path)
599   "Load dumped-lisp.el files along a load path.
600 Also load files off PACKAGE-LISP definitions there."
601   (packages-handle-package-dumped-lisps #'load package-load-path))
602
603 (defun packages-collect-package-dumped-lisps (package-load-path)
604   "Load dumped-lisp.el files along a load path.
605 Return list of files off PACKAGE-LISP definitions there."
606   (let ((*files* '()))
607     (packages-handle-package-dumped-lisps
608      #'(lambda (file)
609          (setq *files* (cons file *files*)))
610      package-load-path)
611     (reverse *files*)))
612
613 (provide 'packages)
614
615 ;;; packages.el ends here