emacs-config/config-ext.org
2020-02-02 16:47:49 -06:00

17 KiB

Various tools

ledger

  (use-package ledger-mode
    :mode "\\.dat\\'"
    :config
    (defconst new-report-str (concat "%(binary) -f %(ledger-file) --cleared-format "
                                     "'%(justify(scrub(get_at(display_total, 0)), 16, 16 + int(prepend_width), true, color))"
                                     "  %(justify(scrub(get_at(display_total, 1)), 18, 36 + int(prepend_width), true, color))"
                                     "  %(justify(scrub(get_at(display_total, 0) - get_at(display_total, 1)), 18, 36 + int(prepend_width), true, color))"
                                     "    %(latest_cleared ? format_date(latest_cleared) : \"         \")"
                                     "    %(!options.flat ? depth_spacer : \"\")%-(ansify_if(partial_account(options.flat), blue if color))\\n"
                                   "%/%$1  %$2  %$3    %$4\\n"
                                   "%/%(prepend_width ? \" \" * int(prepend_width) : \"\")----------------    ----------------    ----------------    ---------\\n'"
                                   " cleared"))

    (setq ledger-reports
          `(("account" "%(binary) -f %(ledger-file) reg %(account)")
            ("bal" "%(binary) -f %(ledger-file) bal")
            ("reg" "%(binary) -f %(ledger-file) reg")
            ("cleared" ,new-report-str)
            ;; ("cleared" "%(binary) -f %(ledger-file) cleared")
            ("expmonth" "%(binary) -f %(ledger-file) -M reg Expenses")
            ("owedmom" "%(binary) -f %(ledger-file) reg Liabilities")
            ("progress" "%(binary) -f %(ledger-file) reg Assets Equity Liabilities")
            ;; ("food" "%(binary) -f %(ledger-file) --add-budget reg Assets")
            ("payee" "%(binary) -f %(ledger-file) reg @%(payee)")))

    (setq dynamic-reports
          '(("budgetcal" "%(binary) -f ~/MEGA/org/entries/food.ledger --daily --add-budget reg Expenses")))

    (defun ledger-dynamic-report ()
      (interactive)
      (let* ((ledger-reports dynamic-reports)
             (report-name (ledger-report-read-name)))
        (ledger-report report-name nil)))

    (setq ledger-reconcile-buffer-line-format
          "%(date)s %-4(code)s %-30(payee)s %-30(account)s %15(amount)s\n")

    (defun ledger-account-check-dont-include-regexp (orig account)
      (when (= (aref account 0)
               ?^)
        (setq account
              (substring account 1)))))

encryption

  (require 'epa-file)
  (epa-file-enable)
  (setq epa-pinentry-mode 'loopback)
  (setq epa-file-cache-passphrase-for-symmetric-encryption t)
  (setenv "GPG_AGENT_INFO" nil)

  (setq epg-gpg-program "gpg2")
  (setq auth-source-debug t)
  (setq auth-sources '((:source "~/.emacs.d/secrets/.authinfo.gpg")))

email

  (setq user-mail-address "bensonchu457@gmail.com"
        user-full-name "Benson Chu")

  (setq smtpmail-smtp-server "smtp.gmail.com"
        smtpmail-smtp-service 587
        send-mail-function 'smtpmail-send-it
        message-send-mail-function 'smtpmail-send-it)

  (mailcap-add "text/html" "/usr/bin/xdg-open %s ")

  (setq mail-specify-envelope-from t
        message-sendmail-envelope-from 'header
        mail-envelope-from 'header)

  (use-package notmuch
    :commands notmuch
    :bind (:map notmuch-message-mode-map
                ("C-c C-c" . #'my/choose-email-address-and-send)
                :map notmuch-search-mode-map
                ("d" . #'my/notmuch-delete-mail)
                ("u" . #'my/undelete-mail)
                ("f" . #'my/recruiting-mail)
                ("<mouse-1>" . nil)
                :map notmuch-tree-mode-map
                ("N" . #'notmuch-tree-next-sibling)
                ("P" . #'notmuch-tree-prev-sibling)
                ("t" . #'notmuch-tree-toggle-folding-thread)
                ("S-SPC" . #'notmuch-tree-scroll-message-window-back))
    :config
    (custom-set-faces
     '(notmuch-tree-match-tree-face ((t (:family "DejaVu Sans Mono"))) t)
     '(notmuch-tree-no-match-tree-face ((t (:family "DejaVu Sans Mono"))) t))
    (set-face-attribute 'notmuch-search-unread-face nil :foreground "white")
    (set-face-attribute 'notmuch-message-summary-face nil :background "steel blue" :foreground "snow")

    (setq notmuch-search-oldest-first nil
          notmuch-saved-searches
          '((:name "inbox" :query "tag:inbox" :key "i")
            (:name "inbox today" :query "date:7d.. tag:inbox" :key "t")
            (:name "work" :query "tag:work" :key "w")
            (:name "mailing lists" :query "tag:mlist" :key "m")
            (:name "recruiting" :query "tag:recruiting" :key "r")
            (:name "unread" :query "tag:unread" :key "u")
            (:name "flagged" :query "tag:flagged" :key "f")
            (:name "cs" :query "tag:cs" :key "c")
            (:name "receipts" :query "tag:receipts" :key "R")
            ;; (:name "sent" :query "tag:sent" :key "s")
            ;; (:name "drafts" :query "tag:draft" :key "d")
            (:name "all mail" :query "*" :key "a")))

    (defun my/choose-email-address-and-send ()
      (interactive)
      (let ((resp (completing-read "Which email? " '("bchu3@cougarnet.uh.edu" "bensonchu457@gmail.com") nil t "^")))
        (setq smtpmail-smtp-server
              (if (string= resp "bensonchu457@gmail.com")
                  "smtp.gmail.com"
                "smtp.office365.com"))
        (notmuch-mua-send-and-exit)))

    (defun my/notmuch-delete-mail ()
      (interactive)
      (notmuch-search-tag '("+deleted"))
      (notmuch-search-next-thread))

    (defun my/undelete-mail ()
      (interactive)
      (notmuch-search-tag '("-deleted")))

    (defun my/recruiting-mail ()
      (interactive)
      (notmuch-search-tag '("-inbox" "+recruiting"))
      (notmuch-search-next-thread))

    (add-to-list 'notmuch-tagging-keys
                 '("r" ("-inbox" "+recruiting") "Recruiting")))

  (eval-after-load 'notmuch
    '(require 'notmuch-fold))

erc

  (use-package erc)
  (use-package erc-hl-nicks)
  (use-package erc-colorize)
  (require 'netrc)
  (erc-hl-nicks-mode)
  (erc-colorize-mode)
  (setq erc-user-full-name "Benson Chu")
  (setq erc-kill-buffer-on-part t)
  (setq erc-autojoin-channels-alist
        '(("freenode.net" "#emacs" "#org-mode"
           ;; "##linux" "#compilers" "#pltclub" 
           ;; "##cs" "##computerscience" "##programming" "#lisp" "##lisp"
           ;; "#sbcl" "#ecl"
           )))

  (defun get-authinfo (host port)
    (let* ((netrc (netrc-parse (expand-file-name "~/.emacs.d/secrets/.authinfo.gpg")))
           (hostentry (netrc-machine netrc host port)))
      (when hostentry (netrc-get hostentry "password"))))

  (defun freenode-connect (nick password)
    (erc :server "irc.freenode.net" :port 6667
         :password password :nick nick))

  (defun irc-connect ()
    (interactive)
    (when (y-or-n-p "Connect to IRC? ")
      (freenode-connect "pest-ctrl" (get-authinfo "irc.freenode.net" "6667"))))

pdf-tools use isearch

  (use-package pdf-tools)
  (pdf-tools-install)
  (define-key pdf-view-mode-map (kbd "C-s") 'isearch-forward)
  (define-key pdf-view-mode-map (kbd "d") (lambda () (interactive) (pdf-view-next-line-or-next-page 8)))
  (define-key pdf-view-mode-map (kbd "u") (lambda () (interactive) (pdf-view-previous-line-or-previous-page 8)))

freezing time

  (defvar my/frozen-time nil)

  (defvar my/format-time-string-function nil)

  (defun my/current-time ()
    my/frozen-time)

  (defun my/format-time-string (original format-string &optional time zone)
    (apply original
           format-string
           (if time
               time
             my/frozen-time)
           zone))

  (defun my/decode-time (original &optional time zone)
    (apply original
           (if time
               time
             my/frozen-time)
           zone))

  ;; Change and freeze time
  (defun za-warudo ()
    "Freeze `current-time' at the current active or inactive timestamp. If point
  is not on a timestamp, the function prompts for one. If time is not specified,
  either by the timstamp under point or prompt, the time defaults to the
  current HH:MM of today at the selected date."
    (interactive)
    (let* ((org-read-date-prefer-future nil)
           (time (org-read-date t 'totime nil "Input freeze time: ")))
      (setq my/frozen-time (append time '(0 0)))
      (advice-add #'current-time :override #'my/current-time)
      (advice-add #'format-time-string :around #'my/format-time-string)
      (advice-add #'decode-time :around #'my/decode-time)
      (set-face-background 'fringe "firebrick2")
      (message "Toki yo tomare")))

  (define-key *root-map* (kbd "C-z") 'za-warudo)

  ;; Release changed / frozen time
  (defun un-za-warudo ()
    "Release the time frozen by `freeze-time'."
    (interactive)
    (advice-remove #'current-time #'my/current-time)
    (advice-remove #'format-time-string #'my/format-time-string)
    (advice-remove #'decode-time #'my/decode-time)
    (setq my/frozen-time nil)
    (set-face-background 'fringe nil)
    (message "Soshite, toki wa ugoki dasu"))

  (define-key *root-map* (kbd "C-r") 'un-za-warudo)

Programming stuff

lsp

  (use-package lsp-mode
    :commands lsp)

  (use-package lsp-ui
    :after lsp-mode
    :bind (:map lsp-mode-map
                ("M-." . #'lsp-ui-peek-find-definitions)
                ("M-?" . #'lsp-ui-peek-find-references)
                ("M-p" . #'lsp-ui-peek-jump-forward))
    :hook (lsp-mode . lsp-ui-mode)
    :config
    (setq lsp-ui-flycheck-enable t)
    (setq lsp-ui-flycheck-live-reporting t))

  (use-package dap-mode
    :hook ((java-mode . dap-mode)
           (java-mode . dap-ui-mode))
    :bind (:map dap-mode-map
                ("C-c h" . #'dap-hydra)
                ("C-c b" . #'dap-breakpoint-toggle)
                ("C-c d r" . #'dap-java-debug)
                ("C-c d m" . #'dap-java-debug-test-class)
                ("C-c r t" . #'mvn-test)))

Slime mode

  (use-package slime
    :commands slime slime-switch-lisps
    :hook ((inferior-lisp-mode . inferior-slime-mode))
    :config
    (setq inferior-lisp-program "/usr/bin/sbcl")

    (defun slime-switch-lisps (lisp)
      (interactive (list (completing-read "Which lisp? "
                                          '("sbcl" "ecl" "cmucl" "clozure-cl"))))
      (setq inferior-lisp-program lisp))

    (let ((clhs-file "~/quicklisp/clhs-use-local.el"))
      (if (file-exists-p clhs-file)
          (load-file clhs-file)
        (warn "clhs not installed. Please install"))))

  (use-package slime-company
    :after slime company
    :config
    (slime-setup '(slime-fancy slime-asdf slime-company)))

rust

  (use-package cargo)
  (use-package rust-mode)
  (use-package rustic)

golang

  (use-package go-mode
    :hook (go-mode . (lambda ()
                       (add-hook 'before-save-hook 'gofmt-before-save nil t)
                       (setq indent-tabs-mode nil)))
    :config
    ;; This is for lsp to work
    (add-to-list 'exec-path "~/go/bin/"))

python

  (use-package elpy)
  (elpy-enable)
  (use-package ein)
  (add-to-list 'exec-path
               "/home/benson/anaconda3/bin/")

web stuff

  (use-package web-mode
    :commands web-mode
    :init
    (add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.cshtml\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
    :config
    (setq web-mode-auto-close-style 2))

  (use-package js2-mode
    :commands js2-mode
    :init
    (add-to-list 'auto-mode-alist '("\\.js$" . js2-mode)))

colorful compilation buffer

  (require 'ansi-color)
  (defun colorize-compilation-buffer ()
    (let ((buffer-read-only nil))
      (ansi-color-apply-on-region (point-min) (point-max))))
  (add-hook 'compilation-filter-hook 'colorize-compilation-buffer)

Various common files

  (use-package csv-mode
    :commands csv-mode
    :init
    (add-to-list 'auto-mode-alist
                 '("\\.csv$" . csv-mode)))

  (use-package yaml-mode
    :commands yaml-mode
    :init
    (add-to-list 'auto-mode-alist
                 '("\\.yaml$" . yaml-mode)
                 '("\\.yml$" . yaml-mode)))

New

transpose-frame

  (use-package transpose-frame)

e2wm

  (use-package e2wm
    :bind (("M-+" . e2wm:start-management)))

exwm-edit

  (use-package exwm-edit)

Youtube-dl

  (add-to-list 'load-path "~/.emacs.d/submodule/youtube-dl-emacs/")
  (require 'youtube-dl)

  (defun youtube-dl-song (url)
    (interactive
     (list (read-from-minibuffer
            "URL: " (or (thing-at-point 'url)
                        (when interprogram-paste-function
                          (funcall interprogram-paste-function))))))
    (async-shell-command (format "youtube-dl -x -f \"bestaudio[ext=m4a]\" \"%s\"; tageditor -s album=\"youtube-dl\" -f *.m4a" url)))

set-default-directory

  (defun set-default-directory (dir)
    (interactive "f")
    (setq default-directory dir))

World time include Taiwan

  (setq display-time-world-list
        '(("America/Chicago" "Houston")
          ("Asia/Taipei" "Taiwan")))

auto-save files in same directory

  (setq backup-directory-alist `(("." . "~/.emacs.d/backups/")))

  (setq make-backup-files t               ; backup of a file the first time it is saved.
        backup-by-copying t               ; don't clobber symlinks
        version-control t                 ; version numbers for backup files
        kept-old-versions 6               ; oldest versions to keep when a new numbered backup is made (default: 2)
        kept-new-versions 9               ; newest versions to keep when a new numbered backup is made (default: 2)
        auto-save-default t               ; auto-save every buffer that visits a file
        auto-save-timeout 20              ; number of seconds idle time before auto-save (default: 30)
        auto-save-interval 200            ; number of keystrokes between auto-saves (default: 300)
        )

Scroll interval

  (setq scroll-margin 1
        hscroll-margin 2
        hscroll-step 1
        scroll-conservatively 101
        scroll-preserve-screen-position t
        mouse-wheel-scroll-amount '(3)
        mouse-wheel-progressive-speed nil)

Setup convenient headers

  (setq auto-insert-alist
        '(((emacs-lisp-mode . "Emacs lisp mode") nil
           ";;; " (file-name-nondirectory buffer-file-name) " --- " _ " -*- lexical-binding: t -*-\n\n"

           ";; Copyright (C) " (format-time-string "%Y") " Benson Chu\n\n"

           ";; Author: Benson Chu <bensonchu457@gmail.com>\n"
           ";; Created: " (format-time-string "[%Y-%m-%d %H:%M]") "\n\n"

           ";; This file is not part of GNU Emacs\n\n"

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

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

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

           ";;; Commentary:\n\n"

           ";;; Code:\n\n"

           "(provide '" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) ")\n"
           ";;; " (file-name-nondirectory buffer-file-name) " ends here\n")
          ((lisp-mode . "Common Lisp") nil
           "(defpackage :" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) "\n"
           "  (:use :cl :alexandria)\n"
           "  (:export))\n\n"

           "(in-package :" (file-name-sans-extension (file-name-nondirectory buffer-file-name)) ")")))

  (auto-insert-mode)