;;; common.el --- common stuff -*- lexical-binding: t -*-
;;; Commentary:

;; add directory containing own stuff and helper utilities to load path
(add-to-list 'load-path "~/.emacs.d/lisp")

;; keep ~/.emacs.d clean
(setq no-littering-etc-directory "~/.emacs.d/")
(let ((var "~/.cache/emacs/")
      (etc "~/.emacs.d/"))
  (make-directory var t)
  (setq no-littering-var-directory var)
  (setq lock-file-name-transforms `((".*" ,(concat var "lock-files/") t)))
  (setq custom-file (concat etc "lisp/custom-settings.el")))
(require 'no-littering)
(no-littering-theme-backups)

;; load settings set by customize
(require 'custom-settings)

;; enable integration with pass
(auth-source-pass-enable)
;; copied from https://systemcrafters.net/emacs-tips/using-encrypted-passwords/
(defun pt/lookup-password (&rest keys)
  (let ((result (apply #'auth-source-search keys)))
    (if result
        (funcall (plist-get (car result) :secret))
        nil)))

;; useful stuff
(defmacro fmt (str)
  "Elisp string interpolation.
Example:
(fmt \"My name is #{user-full-name}, I am running Emacs #{(if (display-graphic-p) \\\"with a GUI\\\" \\\"in a terminal\\\".)}\""
  (let ((exprs nil))
    (with-temp-buffer
      (insert str)
      (goto-char 1)
      (while (re-search-forward "#{" nil t 1)
        (let ((here (point))
              (emptyp (eql (char-after) ?})))
          (unless  emptyp (push (read (buffer-substring (point) (progn (forward-sexp 1) (point)))) exprs))
          (delete-region (- here 2) (progn (search-forward "}") (point)))
          (unless emptyp (insert "%s"))
          (ignore-errors (forward-char 1))))
      (append (list 'format (buffer-string)) (reverse exprs)))))

(provide 'common)
;;; common.el ends here
