dotfiles/emacs/.emacs.d/config/init-lib.el

144 lines
4.0 KiB
EmacsLisp
Raw Normal View History

2021-02-21 03:00:23 +00:00
;; -*- lexical-binding: t; -*-
2021-02-21 13:20:46 +00:00
;; Helper packages
(use-package deferred
2021-02-23 17:14:20 +00:00
:commands (deferred:next
deferred:nextc
deferred:error
deferred:cancel
deferred:watch
deferred:wait
deferred:$
deferred:loop
deferred:parallel
deferred:earlier
deferred:call
deferred:apply
deferred:process
deferred:process-buffer
deferred:wait-idle
deferred:url-retrieve
deferred:url-get
deferred:url-post
deferred:new
deferred:succeed
deferred:fail
deferred:callback
deferred:callback-post
deferred:errorback
deferred:errorback-post
deferred:try
deferred:timeout
deferred:process))
2021-02-21 13:20:46 +00:00
2023-05-01 14:13:29 +00:00
(use-package s)
2021-02-21 13:20:46 +00:00
2023-05-01 14:13:29 +00:00
(use-package dash)
2021-02-21 13:20:46 +00:00
2023-05-01 14:13:29 +00:00
(use-package dash-functional)
2021-02-21 13:20:46 +00:00
2023-05-01 14:13:29 +00:00
(use-package f)
2021-02-21 13:20:46 +00:00
(use-package request
:commands request)
2023-05-01 14:13:29 +00:00
(use-package ht)
2021-02-21 13:20:46 +00:00
2021-02-21 03:00:23 +00:00
;; Elisp utilities
(defun make-process-sentinel (success err)
"Makes a process sentinel that calls `success` on success and `err` on error"
(lambda (proc event)
(cond ((string-match-p "finished" event) (funcall success))
(t (funcall err)))))
(defun make-success-err-msg-sentinel (buf success-msg err-msg &optional kill-on-err)
(make-process-sentinel
(lambda ()
(message success-msg)
(kill-buffer buf))
(lambda ()
(message err-msg)
(when kill-on-err
(kill-buffer buf)))))
2021-02-21 13:20:46 +00:00
(cl-defun extract-vars-from-env-file (file &key dir)
"Extracts an alist of variable name to value from
a bash script that exports environment variables."
(let ((file (expand-file-name file))
(var-re "\\(.+?\\)=\\(.+\\)$")
(env '()))
(with-temp-buffer
(cd (expand-file-name (or dir (file-name-directory file))))
(insert (shell-command-to-string (concat "source "
(shell-quote-argument file)
" > /dev/null && env")))
(goto-char (point-min))
(save-match-data
(while (re-search-forward var-re nil t)
(push (cons (match-string 1) (match-string 2)) env))))
env))
(defun source-env-file (file)
(interactive "fFile: \n")
(let ((env (extract-vars-from-env-file file)))
(dolist (binding env)
(setenv (car binding) (cdr binding)))))
(cl-defmacro with-env-from-file (file &rest body)
(declare (indent 1))
(let ((env-var (make-symbol "the-env"))
(path-var (make-symbol "the-path")))
`(let* ((,env-var (extract-vars-from-env-file ,file))
(,path-var (assoc "PATH" ,env-var))
(exec-path
(if ,path-var
(append (split-string (cdr ,path-var) ":") exec-path)
exec-path))
(process-environment
(append
(mapcar
(lambda (elt) (format "%s=%s" (car elt) (cdr elt)))
,env-var)
process-environment)))
,@body)))
(cl-defun call-with-env-from-file (file callback &key dir)
(let* ((env (extract-vars-from-env-file file :dir dir))
(path (assoc "PATH" env))
(exec-path
(if path
(append (split-string (cdr path) ":") exec-path)
exec-path))
(process-environment
(append (mapcar (lambda (elt) (format "%s=%s" (car elt) (cdr elt))) env)
process-environment)))
(funcall callback)))
(defmacro with-env (env &rest body)
(declare (indent 1))
`(let* ((process-environment
(append
(mapcar
(lambda (elt) (format "%s=%s" (car elt) (cdr elt)))
,env)
process-environment)))
2024-03-20 16:03:04 +00:00
,@body))
(use-package shut-up
:commands (shut-up))
2021-02-21 13:20:46 +00:00
2024-04-09 03:34:24 +00:00
(defun urandom (len)
"Generate a random string of length LEN using /dev/urandom."
(interactive "nLength: ")
(let ((rand (shell-command-to-string (format "head -c %d /dev/urandom | base64" len))))
(when (called-interactively-p 'any)
(kill-new (message rand)))
rand))
2024-07-03 19:21:40 +00:00
(defmacro dbg (form)
"Print the value of FORM and return it."
`(progn
(message (format "%s: %s" ',form ,form))
,form))
2021-02-21 03:00:23 +00:00
(provide 'init-lib)