Rewrite `b/*scratch*' function in init file
[~bandali/configs] / .emacs.d / init.el
index 781b7f0..459dbe8 100644 (file)
@@ -1,6 +1,6 @@
 ;;; init.el --- bandali's emacs configuration -*- lexical-binding: t -*-
 
-;; Copyright (C) 2018-2021  Amin Bandali <bandali@gnu.org>
+;; 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
 \f
 ;;; whoami
 
-(setq ;; user-full-name "bandali"
+(setq user-full-name "Amin Bandali"
       user-mail-address "bandali@gnu.org")
 
 \f
-;;; csetq (`custom' setq)
-
-(require 'cl-lib)
-
-(defmacro csetq (&rest args)
-  "Set the value of user option VAR to VALUE.
-
-More generally, you can use multiple variables and values, as in
-  (csetq VAR VALUE VAR VALUE...)
-This sets each user option VAR's value to the corresponding VALUE.
-
-\(fn [VAR VALUE]...)"
-  (declare (debug setq))
-  `(progn
-     ,@(cl-loop for (var value) on args by 'cddr
-                collect
-                `(funcall (or (get ',var 'custom-set) #'set-default)
-                          ',var ,value))))
-
-\f
 ;;; Package management
 
 ;; variables of interest:
@@ -135,28 +115,21 @@ This sets each user option VAR's value to the corresponding VALUE.
 ;;   (package-generate-description-file d "refinery-theme-pkg.el"))
 (run-with-idle-timer 0.01 nil #'require 'package)
 (with-eval-after-load 'package
-  (when (= (length package-archives) 1)
-    (csetq
-     package-archives
-     `(,@package-archives
-       ;; ("bndl" . "https://p.bndl.org/elpa/")
-       ("org" . "https://orgmode.org/elpa/"))
-     package-load-list
-     '(;; GNU ELPA
-       (debbugs "0.26")
-       (delight "1.7")
-       (orgalist "1.13")
-       (rt-liberation "1.31")
-       (yasnippet "0.14.0")
-       (expand-region "0.11.0")
-       (emms "6.2")
-       ;; bndl
-       ;; (refinery-theme "0.1.1")
-       ;; Org ELPA
-       (org-plus-contrib "20201109"))))
-  (package-initialize))
-
-(csetq package-archive-upload-base "/ssh:caffeine:~/www/p/elpa")
+  ;; (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")
+  ;;    (expand-region "0.11.0")
+  ;;    (rt-liberation "2.4")
+  ;;    (yasnippet "0.14.0")))
+(package-initialize))
+
+(setq package-archive-upload-base "/ssh:caffeine:~/www/p/elpa")
 
 \f
 ;;; Initial setup
@@ -184,7 +157,7 @@ This sets each user option VAR's value to the corresponding VALUE.
   "Expand filename FILE relative to `b/lisp-dir'."
   (expand-file-name (convert-standard-filename file) b/lisp-dir))
 
-(csetq
+(setq
  auto-save-list-file-prefix (b/var "auto-save/sessions/")
  nsm-settings-file (b/var "nsm-settings.el"))
 
@@ -219,7 +192,8 @@ This sets each user option VAR's value to the corresponding VALUE.
 
 ;;;; C-level customizations
 
-(csetq
+(setq
+ ;; line-spacing 3
  ;; completion case sensitivity
  completion-ignore-case t
  read-buffer-completion-ignore-case t
@@ -246,7 +220,11 @@ This sets each user option VAR's value to the corresponding VALUE.
  indent-tabs-mode nil
  tab-width 4)
 
-(set-fontset-font t 'arabic "Vazir")
+(when (display-graphic-p)
+  (set-fontset-font t 'arabic "Vazir"))
+;; ;; (set-frame-font "Drafting Mono-14:weight=light" nil t)
+;; (set-frame-font "Drafting Mono:pixelsize=16" nil t)
+;; (set-face-attribute 'bold nil :weight 'semi-bold)
 
 ;;;; Elisp-level customizations
 
@@ -275,25 +253,19 @@ This sets each user option VAR's value to the corresponding VALUE.
 ;; (define-key icomplete-fido-mode-map
 ;;   (kbd "DEL") #'b/icomplete-fido-backward-updir))
 
-;; (with-eval-after-load 'subr
-;;   (keyboard-translate ?\( ?\[)
-;;   (keyboard-translate ?\) ?\])
-;;   (keyboard-translate ?\[ ?\()
-;;   (keyboard-translate ?\] ?\))
-
-;;   ;; (keyboard-translate ?\( ?\()
-;;   ;; (keyboard-translate ?\) ?\))
-;;   ;; (keyboard-translate ?\[ ?\[)
-;;   ;; (keyboard-translate ?\] ?\])
-;; )
+;; subr
+;; (keyboard-translate ?\( ?\[)
+;; (keyboard-translate ?\) ?\])
+;; (keyboard-translate ?\[ ?\()
+;; (keyboard-translate ?\] ?\))
 
 ;; minibuffer
-(csetq read-file-name-completion-ignore-case t)
+(setq read-file-name-completion-ignore-case t)
 
 ;; startup
 ;; don't need to see the startup echo area message
 (advice-add #'display-startup-echo-area-message :override #'ignore)
-(csetq
+(setq
  ;; i want *scratch* as my startup buffer
  initial-buffer-choice t
  ;; i don't need the default hint
@@ -307,7 +279,7 @@ This sets each user option VAR's value to the corresponding VALUE.
  inhibit-startup-echo-area-message user-login-name)
 
 ;; files
-(csetq
+(setq
  ;; backups (C-h v make-backup-files RET)
  backup-by-copying t
  backup-directory-alist (list (cons "." (b/var "backup/")))
@@ -323,32 +295,38 @@ This sets each user option VAR's value to the corresponding VALUE.
 
 ;; novice
 ;; disable disabled commands
-(csetq disabled-command-function nil)
+(setq disabled-command-function nil)
 
 ;; lazy-person-friendly yes/no prompts
 (defalias 'yes-or-no-p #'y-or-n-p)
 
 ;; autorevert: enable automatic reloading of changed buffers and files
-(csetq auto-revert-verbose nil
-       global-auto-revert-non-file-buffers nil)
+(setq
+ ;; auto-revert-verbose nil
+ global-auto-revert-non-file-buffers nil)
 (require 'autorevert)
 (global-auto-revert-mode 1)
 
 ;; time and battery in mode-line
 (run-with-idle-timer 0.1 nil #'require 'time)
 (with-eval-after-load 'time
-  (csetq
+  (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)
+   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
-  (csetq battery-mode-line-format " %p%% %t")
+  (setq battery-mode-line-format " %p%% %t")
   (display-battery-mode))
 
 ;; (with-eval-after-load 'fringe
@@ -359,6 +337,14 @@ This sets each user option VAR's value to the corresponding VALUE.
 (require 'winner)
 (winner-mode 1)
 
+(run-with-idle-timer 0.1 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'
@@ -382,7 +368,7 @@ This sets each user option VAR's value to the corresponding VALUE.
   (ad-activate 'compilation-start))
 
 ;; isearch
-(csetq
+(setq
  ;; allow scrolling in Isearch
  isearch-allow-scroll t
  isearch-lazy-count t
@@ -394,67 +380,69 @@ This sets each user option VAR's value to the corresponding VALUE.
 
 ;; replace
 ;; uncomment to extend the above behaviour to query-replace
-;; (csetq replace-char-fold t)
+;; (setq replace-char-fold t)
 
 ;; vc
 (global-set-key (kbd "C-x v C-=") #'vc-ediff)
 
 (with-eval-after-load 'vc-git
-  (csetq vc-git-print-log-follow t
-         vc-git-show-stash 0))
+  (setq vc-git-print-log-follow t
+        vc-git-show-stash 0))
 
-(csetq ediff-window-setup-function 'ediff-setup-windows-plain
-       ediff-split-window-function 'split-window-horizontally)
+(setq ediff-window-setup-function 'ediff-setup-windows-plain
+      ediff-split-window-function 'split-window-horizontally)
 (with-eval-after-load 'ediff
   (add-hook 'ediff-after-quit-hook-internal #'winner-undo))
 
 ;; face-remap
-(csetq
+(setq
  ;; gentler font resizing
  text-scale-mode-step 1.05)
 
 (run-with-idle-timer 0.4 nil #'require 'mwheel)
-(csetq 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
+(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))
 
 ;; epg-config
-(csetq
+(setq
  epg-gpg-program (executable-find "gpg")
  ;; ask for GPG passphrase in minibuffer
  ;; this will fail if gpg>=2.1 is not available
  epg-pinentry-mode 'loopback)
 
-;; (require 'pinentry)
-;; workaround for systemd-based distros:
-;; (setq pinentry--socket-dir server-socket-dir)
-;; (pinentry-start)
-
 ;; auth-source
-(csetq
+(setq
  auth-sources '("~/.authinfo.gpg")
  authinfo-hidden (regexp-opt '("password" "client-secret" "token")))
 
 ;; info
 (with-eval-after-load 'info
-  (add-to-list
-   'Info-directory-list
-   (expand-file-name
-    (convert-standard-filename "info/") source-directory)))
+  (setq
+   Info-directory-list
+   `(,@Info-directory-list
+     ,(expand-file-name
+       (convert-standard-filename "info/") source-directory)
+     "/usr/share/info/")))
 
 ;; faces
 (when (display-graphic-p)
   (with-eval-after-load 'faces
     (let* ((grey "#e7e7e7")
-           (darker-grey "#d9d9d9")
-           (box ;; 'unspecified
-                `(:line-width -1 :style released-button)))
+           ;; (darker-grey "#d9d9d9")
+           ;; (box ;; 'unspecified
+           ;;      `(;; :line-width -1
+           ;;        :style released-button))
+           )
+      ;; (set-face-attribute 'fixed-pitch nil :family "Source Code Pro")
+      (set-face-attribute 'fixed-pitch nil :family "Inconsolata")
       (set-face-attribute 'mode-line nil
-                          :background grey :box box)
+                          :background grey ;; :box box
+                          :inherit 'fixed-pitch)
       ;; (set-face-attribute 'mode-line-inactive nil
       ;;                     :background darker-grey :box box)
       )))
@@ -535,6 +523,12 @@ Make N (default: 1) copies of the current line or region."
         (dotimes (_ (abs n1))
           (insert text))))))
 
+(defun b/invert-default-face ()
+  "Invert the `default' face (swap its background and foreground).
+Effectively a very simple light/dark theme toggle switch."
+  (interactive)
+  (invert-face 'default))
+
 \f
 ;;; General key bindings
 
@@ -544,6 +538,7 @@ Make N (default: 1) copies of the current 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 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)
@@ -592,8 +587,8 @@ Make N (default: 1) copies of the current line or region."
 ;; (require 'bandali-theme)
 
 ;; recently opened files
-(csetq recentf-max-saved-items 2000
-       recentf-save-file (b/var "recentf-save.el"))
+(setq recentf-max-saved-items 2000
+      recentf-save-file (b/var "recentf-save.el"))
 (run-with-idle-timer 0.2 nil #'require 'recentf)
 (with-eval-after-load 'recentf
   ;; (add-to-list 'recentf-keep #'file-remote-p)
@@ -625,7 +620,7 @@ Make N (default: 1) copies of the current line or region."
 
 (with-eval-after-load 'help
   (temp-buffer-resize-mode)
-  (csetq help-window-select t))
+  (setq help-window-select t))
 
 (with-eval-after-load 'help-mode
   ;; local key bindings
@@ -633,8 +628,8 @@ Make N (default: 1) copies of the current line or region."
   (define-key help-mode-map (kbd "n") #'forward-button))
 
 (with-eval-after-load 'tramp
-  (csetq tramp-auto-save-directory (b/var "tramp/auto-save/")
-         tramp-persistency-file-name (b/var "tramp/persistency.el"))
+  (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
@@ -643,13 +638,13 @@ Make N (default: 1) copies of the current line or region."
 (with-eval-after-load 'doc-view
   (define-key doc-view-mode-map (kbd "M-RET") #'image-previous-line))
 
-(csetq shr-max-width 80)
+(setq shr-max-width 80)
 
 ;; Email (with Gnus, message, and smtpmail)
 (require 'bandali-gnus)
 (require 'bandali-message)
 ;; (with-eval-after-load 'smtpmail
-;;   (csetq smtpmail-queue-mail t
+;;   (setq smtpmail-queue-mail t
 ;;          smtpmail-queue-dir (concat b/maildir "queue/")))
 
 ;; IRC (with ERC)
@@ -658,8 +653,8 @@ Make N (default: 1) copies of the current line or region."
 ;; 'paste' service (aka scp + web server)
 (add-to-list 'load-path (b/lisp "scpaste"))
 (with-eval-after-load 'scpaste
-  (csetq scpaste-http-destination "https://p.bndl.org"
-         scpaste-scp-destination "p:~"))
+  (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 a p p") #'scpaste)
@@ -671,7 +666,7 @@ Make N (default: 1) copies of the current line or region."
 ;; display Lisp objects at point in the echo area
 (when (version< "25" emacs-version)
   (with-eval-after-load 'eldoc
-    (csetq eldoc-minor-mode-string " eldoc")
+    (setq eldoc-minor-mode-string " eldoc")
     (global-eldoc-mode)))
 
 ;; highlight matching parens
@@ -681,7 +676,7 @@ Make N (default: 1) copies of the current line or region."
 ;; (require 'elec-pair)
 ;; (electric-pair-mode)
 
-(csetq
+(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
@@ -693,17 +688,17 @@ Make N (default: 1) copies of the current line or region."
 
 ;; save minibuffer history
 (require 'savehist)
-(csetq savehist-file (b/var "savehist.el"))
+(setq savehist-file (b/var "savehist.el"))
 (savehist-mode)
 (add-to-list 'savehist-additional-variables 'kill-ring)
 
 ;; automatically save place in files
 (when (version< "25" emacs-version)
-  (csetq save-place-file (b/var "save-place.el"))
+  (setq save-place-file (b/var "save-place.el"))
   (save-place-mode))
 
 (defun indicate-buffer-boundaries-left ()
-    (csetq indicate-buffer-boundaries 'left))
+  (setq indicate-buffer-boundaries 'left))
 (with-eval-after-load 'prog-mode
   (global-prettify-symbols-mode))
 (add-hook 'prog-mode-hook #'indicate-buffer-boundaries-left)
@@ -717,17 +712,17 @@ Make N (default: 1) copies of the current line or region."
 (add-to-list 'auto-mode-alist '("\\.bashrc$" . sh-mode))
 
 (with-eval-after-load 'flyspell
-  (csetq flyspell-mode-line-string " fly"))
+  (setq flyspell-mode-line-string " fly"))
 
 ;; ispell
 ;; 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
-;;   (csetq ispell-local-dictionary-alist
-;;          `((nil "[[:alpha:]]" "[^[:alpha:]]"
-;;                 "['\x2019]" nil ("-B") nil utf-8))
-;;          ispell-program-name (executable-find "hunspell"))
+;;   (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
@@ -746,7 +741,7 @@ Make N (default: 1) copies of the current line or region."
 ;;               #'endless/replace-quote))
 
 ;; abbrev
-(csetq abbrev-file-name (b/etc "abbrev.el"))
+(setq abbrev-file-name (b/etc "abbrev.el"))
 (add-hook 'text-mode-hook #'abbrev-mode)
 
 \f
@@ -761,7 +756,7 @@ Make N (default: 1) copies of the current line or region."
 (add-to-list 'load-path (b/lisp "alloy-mode"))
 (autoload 'alloy-mode "alloy-mode" nil t)
 (with-eval-after-load 'alloy-mode
-  (csetq alloy-basic-offset 2)
+  (setq alloy-basic-offset 2)
   ;; (defun b/alloy-simple-indent (start end)
   ;;   (interactive "r")
   ;;   ;; (if (region-active-p)
@@ -783,23 +778,23 @@ Make N (default: 1) copies of the current line or region."
 ;; (run-with-idle-timer 0.4 nil #'require 'lean-mode)
 ;; (with-eval-after-load 'lean-mode
 ;;   (require 'lean-input)
-;;   (csetq default-input-method "Lean"
-;;          lean-input-tweak-all '(lean-input-compose
-;;                                 (lean-input-prepend "/")
-;;                                 (lean-input-nonempty))
-;;          lean-input-user-translations '(("/" "/")))
+;;   (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
-  (csetq sgml-basic-offset 0))
+  (setq sgml-basic-offset 0))
 
 (with-eval-after-load 'css-mode
-  (csetq css-indent-offset 2))
+  (setq css-indent-offset 2))
 
 ;; auctex
-;; (csetq font-latex-fontify-sectioning 'color)
+;; (setq font-latex-fontify-sectioning 'color)
 
 (with-eval-after-load 'tex-mode
   (cl-delete-if
@@ -812,16 +807,15 @@ Make N (default: 1) copies of the current line or region."
 ;;; Emacs enhancements & auxiliary packages
 
 (with-eval-after-load 'man
-  (csetq Man-width 80))
+  (setq Man-width 80))
 
 (defun b/*scratch* ()
   "Switch to `*scratch*' buffer, creating it if it does not exist."
   (interactive)
-  (switch-to-buffer
-   (or (get-buffer "*scratch*")
-       (with-current-buffer (get-buffer-create "*scratch*")
-         (set-buffer-major-mode (current-buffer))
-         (current-buffer)))))
+  (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))))
 (global-set-key (kbd "C-c s") #'b/*scratch*)
 
 ;; ,----
@@ -871,8 +865,8 @@ Make N (default: 1) copies of the current line or region."
                     "yasnippet" (cmd))
 
   (defconst yas-verbosity-cur yas-verbosity)
-  (setq yas-verbosity 2)
-  (csetq yas-snippet-dirs `(,(b/etc "yasnippet/snippets")))
+  (setq yas-verbosity 2
+        yas-snippet-dirs `(,(b/etc "yasnippet/snippets")))
   ;; (add-to-list 'yas-snippet-dirs "~/src/git/guix/etc/snippets" t)
   (yas-reload-all)
   (setq yas-verbosity yas-verbosity-cur)
@@ -905,17 +899,17 @@ Make N (default: 1) copies of the current line or region."
                                '("gnuzilla"))))
 
 ;; url and url-cache
-(csetq
+(setq
  url-configuration-directory (b/var "url/configuration/")
  url-cache-directory (b/var "url/cache/"))
 
 ;; eww
-(csetq eww-download-directory (file-name-as-directory
-                               (getenv "XDG_DOWNLOAD_DIR")))
+(setq eww-download-directory (file-name-as-directory
+                              (getenv "XDG_DOWNLOAD_DIR")))
 (global-set-key (kbd "C-c a e w") #'eww)
 
 ;; ;; org-ref
-;; (csetq
+;; (setq
 ;;  reftex-default-bibliography '("~/usr/org/references.bib")
 ;;  org-ref-default-bibliography '("~/usr/org/references.bib")
 ;;  org-ref-bibliography-notes "~/usr/org/notes.org"
@@ -924,7 +918,7 @@ Make N (default: 1) copies of the current line or region."
 ;; fill-column-indicator ?
 
 ;; window
-(csetq split-width-threshold 150)
+(setq split-width-threshold 150)
 (global-set-key (kbd "C-c w s l")
                 (lambda ()
                   (interactive)
@@ -957,8 +951,8 @@ Make N (default: 1) copies of the current line or region."
 ;; dmenu
 (add-to-list 'load-path (b/lisp "dmenu"))
 (with-eval-after-load 'dmenu
-  (csetq dmenu-prompt-string "run: "
-         dmenu-save-file (b/var "dmenu-items")))
+  (setq dmenu-prompt-string "run: "
+        dmenu-save-file (b/var "dmenu-items")))
 (autoload 'dmenu "dmenu" nil t)
 
 ;; eosd ?
@@ -974,6 +968,9 @@ Make N (default: 1) copies of the current line or region."
 ;; po-mode
 (require 'bandali-po)
 
+(with-eval-after-load 'emms
+  (setq emms-directory (b/var "emms")))
+
 \f
 ;;; Post initialization