Try/try.el

44 lines
1.6 KiB
EmacsLisp
Raw Normal View History

2014-11-13 22:20:08 +00:00
;;; -*- lexical-binding: t -*-
(require 'package)
(require 'url)
(defvar try-tmp-dir "/tmp/")
(defun try-raw-link-p (url)
2014-11-13 23:21:29 +00:00
"Returns non-nil if this looks like an URL to a .el file."
2014-11-13 22:20:08 +00:00
(string-match-p "[^:]*://\\([^?\r\n]+\\).*\.el?$" url))
(defun try-raw-link (url)
(with-current-buffer (url-retrieve-synchronously url)
(condition-case nil
(progn
(eval-region (search-forward-regexp "^$") (point-max))
2014-11-13 23:21:29 +00:00
(let ((str (car (last (split-string url "/")))))
(message "Trying %s!" str)))
2014-11-13 22:20:08 +00:00
((debug error)
(message "Could not parse %s" url) nil))))
(defun try-compose (f g)
2014-11-13 23:21:29 +00:00
"Compose two functions."
2014-11-13 22:20:08 +00:00
(lambda (&rest x) (funcall f (apply g x))))
(defun try ()
2014-11-13 23:21:29 +00:00
"Try out a package from your `package-archives' or pass a URL
to a raw .el file. Packages are stored in `try-tmp-dir' and raw
.el files are not stored at all."
2014-11-13 22:20:08 +00:00
(interactive)
(let ((url-or-package (completing-read
"url or package: "
(mapcar (try-compose 'symbol-name 'car)
package-archive-contents))))
(cond ((try-raw-link-p url-or-package) (try-raw-link url-or-package))
2014-11-13 23:21:29 +00:00
((assq (intern url-or-package) package-archive-contents)
(let ((package-user-dir try-tmp-dir)
(package-alist nil))
(package-install (intern url-or-package))
(message "Trying %s!" url-or-package)))
(t (message (concat "Couldn't find a sensible way to try this. "
"Try running `package-refresh-contents'!"))))))
2014-11-13 22:20:08 +00:00
(provide 'try)