;;; init.el --- bandali's emacs configuration -*- lexical-binding: t -*-

;; Copyright (C) 2018-2022  Amin Bandali <bandali@gnu.org>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; GNU Emacs configuration of bandali, free software activist,
;; computing scientist, and GNU maintainer and volunteer.

;; Over the years, I've taken inspiration from configurations of many
;; great people.  Some that I can remember off the top of my head are:
;;
;; - https://github.com/dieggsy/dotfiles
;; - https://github.com/dakra/dmacs
;; - http://pages.sachachua.com/.emacs.d/Sacha.html
;; - https://github.com/dakrone/eos
;; - http://doc.rix.si/cce/cce.html
;; - https://github.com/jwiegley/dot-emacs
;; - https://github.com/wasamasa/dotemacs
;; - https://github.com/hlissner/doom-emacs

;;; Code:

;; whoami
(setq user-full-name "Amin Bandali"
      user-mail-address "bandali@gnu.org")


;;; Package management

;; variables of interest:
;;   package-archive-priorities
;;   package-load-list
;;   package-pinned-packages

;; (let* ((b (find-file-noselect "refinery-theme.el"))
;;        (d (with-current-buffer b (package-buffer-info))))
;;   (package-generate-description-file d "refinery-theme-pkg.el"))
(run-with-idle-timer 0.01 nil #'require 'package)
(with-eval-after-load 'package
  ;; (setq
  ;;  ;; package-archives
  ;;  ;; `(,@package-archives
  ;;  ;;   ("bndl" . "https://p.bndl.org/elpa/"))
  ;;  package-load-list
  ;;  '(;; GNU ELPA
  ;;    (debbugs "0.29")
  ;;    (delight "1.7")
  ;;    (emms "7.7")
  ;;    (rt-liberation "2.4")))
  (package-initialize))

(setq package-archive-upload-base "/ssh:caffeine:~/www/p/elpa")


;;; Initial setup

;; Keep ~/.emacs.d clean.
(defvar b/etc-dir
  (expand-file-name
   (convert-standard-filename "etc/") user-emacs-directory)
  "The directory where packages place their configuration files.")
(defvar b/var-dir
  (expand-file-name
   (convert-standard-filename "var/") user-emacs-directory)
  "The directory where packages place their persistent data files.")
(defvar b/lisp-dir
  (expand-file-name
   (convert-standard-filename "lisp/") user-emacs-directory)
  "The directory where packages place their persistent data files.")
(defun b/etc (file)
  "Expand filename FILE relative to `b/etc-dir'."
  (expand-file-name (convert-standard-filename file) b/etc-dir))
(defun b/var (file)
  "Expand filename FILE relative to `b/var-dir'."
  (expand-file-name (convert-standard-filename file) b/var-dir))
(defun b/lisp (file)
  "Expand filename FILE relative to `b/lisp-dir'."
  (expand-file-name (convert-standard-filename file) b/lisp-dir))

;; Separate custom file (don't want it mixing with init.el).
(with-eval-after-load 'custom
  (setq custom-file (b/etc "custom.el"))
  (when (file-exists-p custom-file)
    (load custom-file))
  ;; Only one custom theme at a time.
  ;; (defadvice load-theme (before clear-previous-themes activate)
  ;;   "Clear existing theme settings instead of layering them"
  ;;   (mapc #'disable-theme custom-enabled-themes))
  )

;; Load the secrets file if it exists, otherwise show a warning.
;; (with-demoted-errors
;;     (load (b/etc "secrets")))

;; Start up emacs server:
;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html
(run-with-idle-timer 0.5 nil #'require 'server)
(with-eval-after-load 'server
  (declare-function server-edit "server")
  (global-set-key (kbd "C-c F D") #'server-edit)
  (declare-function server-running-p "server")
  (or (server-running-p) (server-mode)))


;;; Defaults

;;;; C-level customizations

(setq
 ;; line-spacing 3
 completion-ignore-case t
 read-buffer-completion-ignore-case t
 enable-recursive-minibuffers t
 resize-mini-windows t
 message-log-max 20000
 mode-line-compact t
 ;; mouse-autoselect-window t
 scroll-conservatively 15
 scroll-preserve-screen-position 1
 ;; I don't feel like jumping out of my chair every now and again;
 ;; so...don't *BEEP* at me, Emacs. =)
 ring-bell-function 'ignore)

(setq-default
 ;; Case-sensitive search (and `dabbrev-expand').
 ;; case-fold-search nil
 indent-tabs-mode nil  ; always use space for indentation
 tab-width 4
 indicate-buffer-boundaries 'left)

;; Lazy-person-friendly yes/no prompts.
(defalias 'yes-or-no-p #'y-or-n-p)

(when (display-graphic-p)
  ;; (set-frame-font "Source Code Pro-10.5:weight=medium" nil t)
  ;; (set-frame-font "FreeSans" nil t)
  (set-fontset-font t 'arabic "Vazir"))

;;;; Elisp-level customizations

(with-eval-after-load 'minibuffer
  (setq read-file-name-completion-ignore-case t))

;; `startup'
(setq auto-save-list-file-prefix (b/var "auto-save/sessions/"))

(with-eval-after-load 'files
  (setq
   ;; backups (C-h v make-backup-files RET)
   backup-by-copying t
   backup-directory-alist (list (cons "." (b/var "backup/")))
   version-control t
   delete-old-versions t
   ;; auto-save
   auto-save-file-name-transforms `((".*" ,(b/var "auto-save/") t))
   ;; insert newline at the end of files
   ;; require-final-newline t
   ;; open read-only file buffers in view-mode
   ;; (enables niceties like `q' for quit)
   view-read-only t))

;; `novice'
(setq disabled-command-function nil)

;; `subr'
;; (keyboard-translate ?\( ?\[)
;; (keyboard-translate ?\) ?\])
;; (keyboard-translate ?\[ ?\()
;; (keyboard-translate ?\] ?\))

(run-with-idle-timer 0.1 nil #'require 'autorevert)
(with-eval-after-load 'autorevert
  (setq
   ;; auto-revert-verbose nil
   global-auto-revert-non-file-buffers nil)
  (global-auto-revert-mode 1))

(run-with-idle-timer 0.1 nil #'require 'time)
(with-eval-after-load 'time
  (setq
   display-time-default-load-average nil
   display-time-format " %a %b %-e %-l:%M%P"
   display-time-mail-icon '(image :type xpm
                                  :file "gnus/gnus-pointer.xpm"
                                  :ascent center)
   display-time-use-mail-icon t
   zoneinfo-style-world-list
   `(,@zoneinfo-style-world-list
     ("Etc/UTC" "UTC")
     ("Asia/Tehran" "Tehran")
     ("Australia/Melbourne" "Melbourne")))
  (display-time-mode))

(run-with-idle-timer 0.1 nil #'require 'battery)
(with-eval-after-load 'battery
  (setq battery-mode-line-format " %p%% %t")
  (display-battery-mode))

;; (with-eval-after-load 'fringe
;;   ;; smaller fringe
;;   (fringe-mode '(3 . 1)))

(run-with-idle-timer 0.5 nil #'require 'winner)
(with-eval-after-load 'winner
  (winner-mode 1))

(run-with-idle-timer 0.5 nil #'require 'windmove)
(with-eval-after-load 'windmove
  (setq windmove-wrap-around t)
  (global-set-key (kbd "M-H") #'windmove-left)
  (global-set-key (kbd "M-L") #'windmove-right)
  (global-set-key (kbd "M-K") #'windmove-up)
  (global-set-key (kbd "M-J") #'windmove-down))

(with-eval-after-load 'compile
  ;; don't display *compilation* buffer on success.  based on
  ;; https://stackoverflow.com/a/17788551, with changes to use `cl-letf'
  ;; instead of the now obsolete `flet'.
  (defun b/compilation-finish-function (buffer outstr)
    (unless (string-match "finished" outstr)
      (switch-to-buffer-other-window buffer))
    t)

  (setq compilation-finish-functions #'b/compilation-finish-function)

  (require 'cl-macs)

  (defadvice compilation-start
      (around inhibit-display
              (command &optional mode name-function highlight-regexp))
    (if (not (string-match "^\\(find\\|grep\\)" command))
        (cl-letf (((symbol-function 'display-buffer) #'ignore))
          (save-window-excursion ad-do-it))
      ad-do-it))
  (ad-activate 'compilation-start))

(with-eval-after-load 'isearch
  (setq
   ;; Allow scrolling in Isearch.
   isearch-allow-scroll t
   isearch-lazy-count t
   ;; Search for non-ASCII characters: i’d like non-ASCII characters
   ;; such as ‘’“”«»‹›áⓐ𝒶 to be selected when I search for their ASCII
   ;; counterpart.  Shoutout to
   ;; http://endlessparentheses.com/new-in-emacs-25-1-easily-search-non-ascii-characters.html
   search-default-mode #'char-fold-to-regexp))

;; (with-eval-after-load 'replace
;;   ;; Uncomment to extend the above behaviour to query-replace.
;;   (setq replace-char-fold t))

;; `vc'
(global-set-key (kbd "C-x v C-=") #'vc-ediff)

(with-eval-after-load 'vc-git
  (setq vc-git-print-log-follow t
        vc-git-show-stash 0))

(with-eval-after-load 'ediff
  (setq ediff-window-setup-function 'ediff-setup-windows-plain
        ediff-split-window-function 'split-window-horizontally)
  (add-hook 'ediff-after-quit-hook-internal #'winner-undo))

(with-eval-after-load 'face-remap
  (setq
   ;; Gentler font resizing.
   text-scale-mode-step 1.05))

(run-with-idle-timer 0.4 nil #'require 'mwheel)
(with-eval-after-load 'mwheel
  (setq
   mouse-wheel-scroll-amount '(1 ((shift) . 1)) ; one line at a time
   mouse-wheel-progressive-speed nil    ; don't accelerate scrolling
   mouse-wheel-follow-mouse t))         ; scroll window under mouse

(run-with-idle-timer 0.4 nil #'require 'pixel-scroll)
(with-eval-after-load 'pixel-scroll
  (pixel-scroll-mode 1))

(with-eval-after-load 'epg-config
  (setq
   epg-gpg-program (executable-find "gpg")
   ;; Ask for GPG passphrase in minibuffer.
   ;; Will fail if gpg >= 2.1 is not available.
   epg-pinentry-mode 'loopback))

(with-eval-after-load 'auth-source
  (setq
   auth-sources '("~/.authinfo.gpg")
   authinfo-hidden
   (regexp-opt '("password" "client-secret" "token"))))

(with-eval-after-load 'info
  (setq
   Info-directory-list
   `(,@Info-directory-list
     ,(expand-file-name
       (convert-standard-filename "info/") source-directory)
     "/usr/share/info/")))

(when (display-graphic-p)
  (with-eval-after-load 'faces
    (let ((grey "#e7e7e7"))
      (set-face-attribute 'fixed-pitch nil
                          :font "Source Code Pro"
                          :weight 'medium)
      (set-face-attribute 'mode-line nil
                          :background grey
                          :inherit 'fixed-pitch))))

(when (version< emacs-version "28")
  ;; Manually make some `mode-line' spaces smaller.  Emacs 28 (and
  ;; above) does a terrific job at this out of the box when
  ;; `mode-line-compact' is set to t (see above)."
  (setq-default
   mode-line-format
   (mapcar
    (lambda (x)
      (if (and (stringp x)
               (or (string= x "   ")
                   (string= x "  ")))
          " "
        x))
    mode-line-format)
   mode-line-buffer-identification
   (propertized-buffer-identification "%10b")))


;;; Useful utilities

(defun b/add-elisp-section ()
  (interactive)
  (insert "\n")
  (forward-line -1)
  (insert "\n\n;;; "))

(defun b/insert-asterism ()
  "Insert a centred asterism."
  (interactive)
  (let ((asterism "* * *"))
    (insert
     (concat
      "\n"
      (make-string
       (floor (/ (- fill-column (length asterism)) 2))
       ?\s)
      asterism
      "\n"))))

(defun b/start-process (program &rest args)
  "Same as `start-process', but doesn't bother about name and buffer."
  (let ((process-name (concat program "_process"))
        (buffer-name  (generate-new-buffer-name
                       (concat program "_output"))))
    (apply #'start-process
           process-name buffer-name program args)))

(defun b/no-mouse-autoselect-window ()
  "Conveniently disable `focus-follows-mouse'.
For disabling the behaviour for certain buffers and/or modes."
  (make-local-variable 'mouse-autoselect-window)
  (setq mouse-autoselect-window nil))

;; (defun b/move-indentation-or-beginning-of-line (arg)
;;   "Move to the indentation or to the beginning of line."
;;   (interactive "^p")
;;   ;; (if (bolp)
;;   ;;     (back-to-indentation)
;;   ;;   (move-beginning-of-line arg))
;;   (if (= (point)
;;          (progn (back-to-indentation)
;;                 (point)))
;;       (move-beginning-of-line arg)))

(defun b/join-line-top ()
  "Like `join-line', but join next line to the current line."
  (interactive)
  (join-line 1))

(defun b/*scratch* ()
  "Switch to `*scratch*' buffer, creating it if it does not
 exist."
  (interactive)
  (let ((fun (if (functionp #'get-scratch-buffer-create)
                 #'get-scratch-buffer-create ; (version<= "29" emacs-version)
               #'startup--get-buffer-create-scratch))) ; (version< emacs-version "29")
    (switch-to-buffer (funcall fun))))

(defun b/duplicate-line-or-region (&optional n)
  "Duplicate the current line, or region (if active).
Make N (default: 1) copies of the current line or region."
  (interactive "*p")
  (let ((u-r-p (use-region-p))          ; if region is active
        (n1 (or n 1)))
    (save-excursion
      (let ((text
             (if u-r-p
                 (buffer-substring (region-beginning) (region-end))
               (prog1 (thing-at-point 'line)
                 (end-of-line)
                 (if (eobp)
                     (newline)
                   (forward-line 1))))))
        (dotimes (_ (abs n1))
          (insert text))))))

(defun b/invert-default-face (arg)
  "Invert the `default' and `mode-line' faces for the current frame.
Swap the background and foreground for the two `default' and
`mode-line' faces, effectively acting like a simple light/dark
theme toggle.  If prefix argument ARG is given, invert the faces
for all frames."
  (interactive "P")
  (let ((frame (unless arg
                 (selected-frame))))
    (invert-face 'default frame)
    (invert-face 'mode-line frame))
  (when (fboundp #'exwm-systemtray--refresh-background-color)
    (exwm-systemtray--refresh-background-color 'remap)))

(defun b/export-frame ()
  (interactive)
  ;; TODO: ask for fn and/or take as arg
  (let* ((fn (make-temp-file "emacs" nil ".pdf"))
         (data (x-export-frames nil 'pdf)))
    (with-temp-file fn
      (insert data))
    (kill-new fn)
    (message fn)))


;;; General key bindings

;; (global-set-key (kbd "C-a") #'b/move-indentation-or-beginning-of-line)
(global-set-key (kbd "C-c i") #'ielm)
(global-set-key (kbd "C-c d") #'b/duplicate-line-or-region)
(global-set-key (kbd "C-c j") #'b/join-line-top)
(global-set-key (kbd "C-S-j") #'b/join-line-top)
(global-set-key (kbd "C-c s c") #'b/*scratch*)
(global-set-key (kbd "C-c x") #'execute-extended-command)
(global-set-key (kbd "C-c v") #'b/invert-default-face)

;; evaling and macro-expanding
(global-set-key (kbd "C-c e b") #'eval-buffer)
(global-set-key (kbd "C-c e e") #'eval-last-sexp)
(global-set-key (kbd "C-c e m") #'pp-macroexpand-last-sexp)
(global-set-key (kbd "C-c e r") #'eval-region)

;; emacs things
(global-set-key (kbd "C-c e i") #'emacs-init-time)
(global-set-key (kbd "C-c e u") #'emacs-uptime)
(global-set-key (kbd "C-c e v") #'emacs-version)

;; finding
(global-set-key (kbd "C-c f .") #'find-file)
(global-set-key (kbd "C-c f d") #'find-name-dired)
(global-set-key (kbd "C-c f l") #'find-library)
(global-set-key (kbd "C-c f p") #'find-file-at-point)

;; frames
(global-set-key (kbd "C-c F m") #'make-frame-command)
(global-set-key (kbd "C-c F d") #'delete-frame)

;; help/describe
(global-set-key (kbd "C-S-h F") #'describe-face)

(define-key emacs-lisp-mode-map (kbd "C-<return>") #'b/add-elisp-section)

(when (display-graphic-p)
  (global-unset-key (kbd "C-z")))


;;; Essential packages

(add-to-list
 'load-path
 (expand-file-name
  (convert-standard-filename "lisp") user-emacs-directory))

(when
    (and
     (display-graphic-p)
     ;; we're not running in another WM/DE
     (not (getenv "XDG_CURRENT_DESKTOP"))
     (member (system-name) '("chaman" "langa")))
  (require 'bandali-exwm)
  (global-set-key (kbd "C-x b") #'exwm-workspace-switch-to-buffer))

(require 'bandali-org)

;; (require 'bandali-theme)

;; recently opened files
(run-with-idle-timer 0.2 nil #'require 'recentf)
(with-eval-after-load 'recentf
  (setq
   recentf-max-saved-items 2000
   recentf-save-file (b/var "recentf-save.el"))
  ;; (add-to-list 'recentf-keep #'file-remote-p)
  (recentf-mode)

  (defun b/recentf-open ()
  "Use `completing-read' to \\[find-file] a recent file."
  (interactive)
  (find-file
   (completing-read "Find recent file: " recentf-list)))
  (global-set-key (kbd "C-c f r") #'b/recentf-open))

;; (define-key minibuffer-local-completion-map
;;   "\t" #'minibuffer-force-complete)

;; (with-eval-after-load 'icomplete

;; (setq icomplete-on-del-error-function #'abort-recursive-edit)

;; (defun b/icomplete-fido-backward-updir ()
;;   "Delete char before or go up directory, like `ido-mode'."
;;   (interactive)
;;   (if (and (eq (char-before) ?/)
;;            (eq (icomplete--category) 'file))
;;       (save-excursion
;;         (goto-char (1- (point)))
;;         (when (search-backward "/" (point-min) t)
;;           (delete-region (1+ (point)) (point-max))))
;;     (condition-case nil
;;         (call-interactively #'delete-backward-char)
;;       (error
;;        (when icomplete-on-del-error-function
;;          (funcall icomplete-on-del-error-function))))))

;; (define-key icomplete-fido-mode-map
;;   (kbd "DEL") #'b/icomplete-fido-backward-updir))

;; (fido-mode 1)
;; (defun b/icomplete--fido-mode-setup ()
;;   "Customizations to `fido-mode''s minibuffer."
;;   (when (and icomplete-mode (icomplete-simple-completing-p))
;;     (setq-local
;;      ;; icomplete-compute-delay 0.1
;;      ;; icomplete-hide-common-prefix t
;;      icomplete-separator " · "
;;      completion-styles '(basic substring partial-completion flex))))
;; (add-hook 'minibuffer-setup-hook #'b/icomplete--fido-mode-setup 1)

(require 'bandali-eshell)

(require 'bandali-ibuffer)

(require 'bandali-dired)

(with-eval-after-load 'help
  (temp-buffer-resize-mode)
  (setq help-window-select t))

(with-eval-after-load 'help-mode
  (define-key help-mode-map (kbd "p") #'backward-button)
  (define-key help-mode-map (kbd "n") #'forward-button))

(with-eval-after-load 'tramp
  (setq tramp-auto-save-directory (b/var "tramp/auto-save/")
        tramp-persistency-file-name (b/var "tramp/persistency.el"))
  (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:"))
  (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil))
  (add-to-list 'tramp-default-proxies-alist
               (list (regexp-quote (system-name)) nil nil)))

(with-eval-after-load 'doc-view
  (define-key doc-view-mode-map (kbd "M-RET") #'image-previous-line))

(setq shr-max-width 80)

;; Email (with Gnus, message, and smtpmail)
(require 'bandali-gnus)
(require 'bandali-message)
;; (with-eval-after-load 'smtpmail
;;   (setq smtpmail-queue-mail t
;;          smtpmail-queue-dir (concat b/maildir "queue/")))

;; IRC (with ERC)
(require 'bandali-erc)

;; 'paste' service (aka scp + web server)
(add-to-list 'load-path (b/lisp "scpaste"))
(with-eval-after-load 'scpaste
  (setq scpaste-http-destination "https://p.bndl.org"
        scpaste-scp-destination "p:~"))
(autoload 'scpaste "scpaste" nil t)
(autoload 'scpaste-region "scpaste" nil t)
(global-set-key (kbd "C-c p p") #'scpaste)
(global-set-key (kbd "C-c p r") #'scpaste-region)


;;; Editing

(when (featurep 'eldoc)
  ;; Display Lisp objects at point in the echo area.
  (with-eval-after-load 'eldoc
    (setq eldoc-minor-mode-string " eldoc")
    (global-eldoc-mode)))

;; highlight matching parens
(run-with-idle-timer 0.2 nil #'require 'paren)
(with-eval-after-load 'paren
  (show-paren-mode))

;; (run-with-idle-timer 0.2 nil #'require 'elec-pair)
;; (with-eval-after-load 'elec-pair
;;   (electric-pair-mode))

(with-eval-after-load 'simple
  (setq
   ;; Save what I copy into clipboard from other applications into Emacs'
   ;; kill-ring, which would allow me to still be able to easily access
   ;; it in case I kill (cut or copy) something else inside Emacs before
   ;; yanking (pasting) what I'd originally intended to.
   save-interprogram-paste-before-kill t)
  (column-number-mode 1)
  (line-number-mode 1))

(run-with-idle-timer 0.2 nil #'require 'savehist)
(with-eval-after-load 'savehist
  ;; Save minibuffer history.
  (setq savehist-file (b/var "savehist.el"))
  (savehist-mode)
  (add-to-list 'savehist-additional-variables 'kill-ring))

;; Automatically save place in files.
(run-with-idle-timer 0.2 nil #'require 'saveplace nil 'noerror)
(with-eval-after-load 'saveplace
  (setq save-place-file (b/var "save-place.el"))
  (save-place-mode))

(with-eval-after-load 'prog-mode
  (global-prettify-symbols-mode))

(add-to-list 'auto-mode-alist '("\\.*rc$" . conf-mode))
(add-to-list 'auto-mode-alist '("\\.bashrc$" . sh-mode))

(with-eval-after-load 'flyspell
  (setq flyspell-mode-line-string " fly"))

(with-eval-after-load 'text-mode
  (add-hook 'text-mode-hook #'flyspell-mode)
  (define-key text-mode-map (kbd "C-<return>") #'b/insert-asterism))

;; ;; http://endlessparentheses.com/ispell-and-apostrophes.html
;; (run-with-idle-timer 0.6 nil #'require 'ispell)
;; (with-eval-after-load 'ispell
;;   ;; ’ can be part of a word.
;;   (setq ispell-local-dictionary-alist
;;         `((nil "[[:alpha:]]" "[^[:alpha:]]"
;;                "['\x2019]" nil ("-B") nil utf-8))
;;         ispell-program-name (executable-find "hunspell"))
;;   ;; Don't send ’ to the subprocess.
;;   (defun endless/replace-apostrophe (args)
;;     (cons (replace-regexp-in-string
;;            "’" "'" (car args))
;;           (cdr args)))
;;   (advice-add #'ispell-send-string :filter-args
;;               #'endless/replace-apostrophe)
;;   ;; Convert ' back to ’ from the subprocess.
;;   (defun endless/replace-quote (args)
;;     (if (not (derived-mode-p 'org-mode))
;;         args
;;       (cons (replace-regexp-in-string
;;              "'" "’" (car args))
;;             (cdr args))))
;;   (advice-add #'ispell-parse-output :filter-args
;;               #'endless/replace-quote))

(with-eval-after-load 'abbrev
  (setq abbrev-file-name (b/etc "abbrev.el")))
(add-hook 'text-mode-hook #'abbrev-mode)


;;; Programming modes

(with-eval-after-load 'lisp-mode
  (defun indent-spaces-mode ()
    (setq indent-tabs-mode nil))
  (add-hook 'lisp-interaction-mode-hook #'indent-spaces-mode))

(add-to-list 'load-path (b/lisp "alloy-mode"))
(autoload 'alloy-mode "alloy-mode" nil t)
(with-eval-after-load 'alloy-mode
  (setq alloy-basic-offset 2)
  ;; (defun b/alloy-simple-indent (start end)
  ;;   (interactive "r")
  ;;   ;; (if (region-active-p)
  ;;   ;;     (indent-rigidly start end alloy-basic-offset)
  ;;   ;;   (if (bolp)
  ;;   ;;       (indent-rigidly (line-beginning-position)
  ;;   ;;                       (line-end-position)
  ;;   ;;                       alloy-basic-offset)))
  ;;   (indent-to (+ (current-column) alloy-basic-offset)))
  (define-key alloy-mode-map (kbd "RET") #'electric-newline-and-maybe-indent)
  ;; (define-key alloy-mode-map (kbd "TAB") #'b/alloy-simple-indent)
  (define-key alloy-mode-map (kbd "TAB") #'indent-for-tab-command))
(add-to-list 'auto-mode-alist '("\\.\\(als\\|dsh\\)\\'" . alloy-mode))
(add-hook 'alloy-mode-hook (lambda nil (setq-local indent-tabs-mode nil)))

;; (eval-when-compile (defvar lean-mode-map))
;; (run-with-idle-timer 0.4 nil #'require 'lean-mode)
;; (with-eval-after-load 'lean-mode
;;   (require 'lean-input)
;;   (setq default-input-method "Lean"
;;         lean-input-tweak-all '(lean-input-compose
;;                                (lean-input-prepend "/")
;;                                (lean-input-nonempty))
;;         lean-input-user-translations '(("/" "/")))
;;   (lean-input-setup)
;;   ;; local key bindings
;;   (define-key lean-mode-map (kbd "S-SPC") #'company-complete))

(with-eval-after-load 'sgml-mode
  (setq sgml-basic-offset 0))

(with-eval-after-load 'css-mode
  (setq css-indent-offset 2))

;; (with-eval-after-load 'auctex
;;   (setq font-latex-fontify-sectioning 'color))

(with-eval-after-load 'tex-mode
  (cl-delete-if
   (lambda (p) (string-match "^---?" (car p)))
   tex--prettify-symbols-alist))
(add-hook 'tex-mode-hook #'auto-fill-mode)
(add-hook 'tex-mode-hook #'flyspell-mode)

(run-with-idle-timer 0.5 nil #'require 'cmake-mode)
(with-eval-after-load 'cmake-mode
  ;; (setq cmake-tab-width 4)
  (add-to-list 'load-path (b/lisp "cmake-font-lock"))
  (run-with-idle-timer 0.5 nil #'require 'cmake-font-lock))


;;; Emacs enhancements & auxiliary packages

(with-eval-after-load 'nsm
  (setq nsm-settings-file (b/var "nsm-settings.el")))

(with-eval-after-load 'man
  (setq Man-width 80))

;; ,----
;; | make pretty boxed quotes like this
;; `----
(run-with-idle-timer 0.6 nil #'require 'boxquote)
(with-eval-after-load 'boxquote
  (defvar b/boxquote-prefix-map)
  (define-prefix-command 'b/boxquote-prefix-map)
  (global-set-key (kbd "C-c q") 'b/boxquote-prefix-map)
  (define-key b/boxquote-prefix-map (kbd "b")   #'boxquote-buffer)
  (define-key b/boxquote-prefix-map (kbd "B")   #'boxquote-insert-buffer)
  (define-key b/boxquote-prefix-map (kbd "d")   #'boxquote-defun)
  (define-key b/boxquote-prefix-map (kbd "F")   #'boxquote-insert-file)
  (define-key b/boxquote-prefix-map (kbd "hf")  #'boxquote-describe-function)
  (define-key b/boxquote-prefix-map (kbd "hk")  #'boxquote-describe-key)
  (define-key b/boxquote-prefix-map (kbd "hv")  #'boxquote-describe-variable)
  (define-key b/boxquote-prefix-map (kbd "hw")  #'boxquote-where-is)
  (define-key b/boxquote-prefix-map (kbd "k")   #'boxquote-kill)
  (define-key b/boxquote-prefix-map (kbd "p")   #'boxquote-paragraph)
  (define-key b/boxquote-prefix-map (kbd "q")   #'boxquote-boxquote)
  (define-key b/boxquote-prefix-map (kbd "r")   #'boxquote-region)
  (define-key b/boxquote-prefix-map (kbd "s")   #'boxquote-shell-command)
  (define-key b/boxquote-prefix-map (kbd "t")   #'boxquote-text)
  (define-key b/boxquote-prefix-map (kbd "T")   #'boxquote-title)
  (define-key b/boxquote-prefix-map (kbd "u")   #'boxquote-unbox)
  (define-key b/boxquote-prefix-map (kbd "U")   #'boxquote-unbox-region)
  (define-key b/boxquote-prefix-map (kbd "y")   #'boxquote-yank)
  (define-key b/boxquote-prefix-map (kbd "M-q") #'boxquote-fill-paragraph)
  (define-key b/boxquote-prefix-map (kbd "M-w") #'boxquote-kill-ring-save))

;; `debbugs'
(global-set-key (kbd "C-c D d") #'debbugs-gnu)
(global-set-key (kbd "C-c D b") #'debbugs-gnu-bugs)
(global-set-key (kbd "C-c D e")         ; bug-gnu-emacs
                (lambda ()
                  (interactive)         
                  (setq debbugs-gnu-current-suppress t)
                  (debbugs-gnu debbugs-gnu-default-severities
                               '("emacs"))))
(global-set-key (kbd "C-c D g")         ; bug-gnuzilla
                (lambda ()
                  (interactive)
                  (setq debbugs-gnu-current-suppress t)
                  (debbugs-gnu debbugs-gnu-default-severities
                               '("gnuzilla"))))

(with-eval-after-load 'url
  (setq url-configuration-directory (b/var "url/configuration/")))

(with-eval-after-load 'url-cache
  (setq url-cache-directory (b/var "url/cache/")))

(with-eval-after-load 'eww
  (setq
   eww-download-directory
   (file-name-as-directory (getenv "XDG_DOWNLOAD_DIR"))))
(global-set-key (kbd "C-c e w") #'eww)

;; (with-eval-after-load 'org-ref
;;   (setq
;;    reftex-default-bibliography '("~/usr/org/references.bib")
;;    org-ref-default-bibliography '("~/usr/org/references.bib")
;;    org-ref-bibliography-notes "~/usr/org/notes.org"
;;    org-ref-pdf-directory "~/usr/org/bibtex-pdfs/"))

(run-with-idle-timer
 0.2 nil #'require 'display-fill-column-indicator nil 'noerror)
(with-eval-after-load 'display-fill-column-indicator
  (global-display-fill-column-indicator-mode 1))

(with-eval-after-load 'window
  (setq split-width-threshold 150)
  (global-set-key (kbd "C-c w s l")
                  (lambda ()
                    (interactive)
                    (split-window-right)
                    (other-window 1)))
  (global-set-key (kbd "C-c w s j")
                  (lambda ()
                    (interactive)
                    (split-window-below)
                    (other-window 1)))
  (global-set-key (kbd "C-c w q") #'quit-window))

;; Uncomment to disable reftex-cite's default choice of previous word.
;; (with-eval-after-load 'reftex
;;   (require 'reftex-cite)
;;   (defun reftex-get-bibkey-default ()
;;     "If the cursor is in a citation macro, return the word before
;;  the macro."
;;     (let* ((macro (reftex-what-macro 1)))
;;       (save-excursion
;;         (when (and macro (string-match "cite" (car macro)))
;;           (goto-char (cdr macro)))
;;         (reftex-this-word)))))
(add-hook 'latex-mode-hook #'reftex-mode)

(add-to-list 'load-path (b/lisp "dmenu"))
(with-eval-after-load 'dmenu
  (setq dmenu-prompt-string "run: "
        dmenu-save-file (b/var "dmenu-items")))
(autoload 'dmenu "dmenu" nil t)

(run-with-idle-timer 0.5 nil #'require 'delight)
(with-eval-after-load 'delight
  (delight 'auto-fill-function " f" "simple")
  (delight 'abbrev-mode "" "abbrev")
  (delight 'mml-mode " mml" "mml"))

(require 'bandali-po)

(with-eval-after-load 'emms
  (setq emms-directory (b/var "emms")))

(add-to-list 'load-path (b/lisp "ffs"))
(run-with-idle-timer 0.5 nil #'require 'ffs)
(with-eval-after-load 'ffs
  (global-set-key (kbd "C-c f s") #'ffs))

;;; init.el ends here