 ;;; init.el --- my basic emacs setup for sane defaults
 
+;; Time-stamp: <2020-09-15 15:48:18 rovalent>
 ;; Copyright 2020 Ronald Valente
 
-;; Author: Ronald Valente
-;; Version: 2.0.0
-;; Created: 2019-04-04
-;; Update: 2020-07-04
-
 ;;; Commentary:
 
 ;; The idea here is to set a minimum number of options to improve the
-;; initial ergonomics of Emacs, at least for my personal usage.
+;; initial ergonomics of Emacs, at least in my opinion for my usage.
 
 ;;; Code:
 
-;; Start - Keep Use Package At the Top
-;; Safer Way to Check Packages
-;; https://glyph.twistedmatrix.com/2015/11/editor-malware.html
+;; Always load newest byte code
+(setq load-prefer-newer t)
+
+;; reduce the frequency of garbage collection by making it happen on
+;; each 50MB of allocated data (the default is on every 0.76MB)
+(setq gc-cons-threshold 50000000)
+
+;; warn when opening files bigger than 100MB
+(setq large-file-warning-threshold 100000000)
+
+;; Minimal UI right at startup
+(scroll-bar-mode -1)                          ;; don't display scroll bar
+(tool-bar-mode -1)                            ;; disable toolbar
+(tooltip-mode -1)                             ;; disable tooltips
+(menu-bar-mode -1)                            ;; disable menu bar
+(setq inhibit-startup-message t)              ;; hide the startup message
+
+;; set initial and default frame size for emacs to be half the screen
+(if (display-graphic-p)
+    (progn
+      (setq initial-frame-alist
+	    '((tool-bar-lines . 0)
+	      (width . 142) ; chars
+	      (height . 73) ; lines
+	      (left . 0)
+	      (top . 0)))
+      (setq default-frame-alist
+	    '((tool-bar-lines . 0)
+	      (width . 142)
+	      (height . 73)
+	      (left . 0)
+	      (top . 0))))
+  (progn
+    (setq initial-frame-alist '((tool-bar-lines . 0)))
+    (setq default-frame-alist '((tool-bar-lines . 0)))))
+
+;; Setup package, use-package, and repositories
 (require 'package)
-(setq package-archives
-      '(("gnu" . "https://elpa.gnu.org/packages/")
-        ("melpa-stable" . "https://stable.melpa.org/packages/")
-        ("org" . "https://orgmode.org/elpa/")
-        ("melpa" . "https://melpa.org/packages/"))
-      tls-checktrust t
-      tls-program '("gnutls-cli --x509cafile %t -p %p %h")
-      gnutls-verify-error t)
+(setq package-archives '(("melpa" . "https://melpa.org/packages/")
+                         ("melpa-stable" . "https://stable.melpa.org/packages/")
+			 ("org" . "https://orgmode.org/elpa/")
+			 ("elpa" . "https://elpa.gnu.org/packages/")))
 
 (package-initialize)
 
-(setq use-package-always-ensure nil)
-
-(unless (require 'use-package nil t)
-  (if (not (yes-or-no-p (concat "Refresh packages, install use-package and"
-                                " other packages used by init file? ")))
-      (error "you need to install use-package first")
-    (package-refresh-contents)
-    (package-install 'use-package)
-    (require 'use-package)
-    (setq use-package-always-ensure t)))
-
-;; Minimal UI
-(scroll-bar-mode -1)
-(tool-bar-mode -1)
-(tooltip-mode -1)
-(menu-bar-mode -1)
+(unless package-archive-contents
+  (package-refresh-contents))
+
+(unless (package-installed-p 'use-package)
+  (package-install 'use-package))
+
+(require 'use-package)
+
+;; Enable auto-update of packages
+(use-package auto-package-update
+  :ensure t
+  :config
+  (setq auto-package-update-delete-old-versions t)
+  (setq auto-package-update-hide-results t)
+  (auto-package-update-maybe))
+
+;; Update environment from shell on macOS
+(use-package exec-path-from-shell
+  :if (memq window-system '(mac ns x))
+  :ensure t
+  :config
+  (exec-path-from-shell-initialize))
+
+;; Additional UI Changes and Themes
+(use-package all-the-icons
+  :ensure t)
+
+(use-package doom-themes
+  :ensure t
+  :config
+  (load-theme 'doom-one t)
+  (doom-themes-visual-bell-config)
+  (setq doom-themes-treemacs-theme "doom-colors")
+  (doom-themes-treemacs-config)
+  (doom-themes-org-config))
+
+(use-package doom-modeline
+  :ensure t
+  :init (doom-modeline-mode 1))
+
+;; Write backup files to own directory
+(setq backup-directory-alist
+      `(("." . ,(expand-file-name
+                 (concat user-emacs-directory "backups")))))
+
+;; Make backups of files, even when they're in version control
+(setq vc-make-backup-files t    ; backup even if files are under version control
+      backup-by-copying t       ; don't clobber symlinks
+      delete-old-versions -1    ; don't remove any backups
+      version-control t         ; enforce numeric backup versions
+      )
+
+;; Sane defaults
+(setq auto-revert-interval 1)                 ;; Refresh buffers fast
+(setq echo-keystrokes 0.1)                    ;; Show keystrokes asap
+(setq initial-scratch-message nil)            ;; Clean scratch buffer
+(setq initial-major-mode 'org-mode)           ;; Set scratch buffer to be org-mode
+
 (save-place-mode 1)                           ;; return to the last place we were in the file
 (show-paren-mode t)                           ;; show matching parens
 (delete-selection-mode t)                     ;; delete the selection with a keypress like every other editor
-(blink-cursor-mode -1)                        ;; the blinking cursor is nothing, but an annoyance
-(global-linum-mode t)                         ;; enable line numbers globally
-(line-number-mode t)
-(column-number-mode t)
-(size-indication-mode t)
-(setq inhibit-startup-message t)              ;; hide the startup message
-(setq ring-bell-function 'ignore)             ;; disable the annoying bell
+(size-indication-mode t)                      ;; display the size of the buffer
+(global-hl-line-mode t)                       ;; highlight the current line we're on
 (setq show-trailing-whitespace t)             ;; display trailing whitespace
 (setq require-final-newline t)                ;; Newline at the end of the file, always
-(setq-default tab-width 4
-              indent-tabs-mode nil)           ;; Use spaces instead of tabs, tabs are 4 spaces
-(setq initial-major-mode 'org-mode)           ;; Set scratch buffer to be org-mode
-
-
-;; revert buffers automatically when underlying files are changed externally
-(global-auto-revert-mode t)
-
-;; Fancy titlebar for macOS
-;; Removes the dark gray titlebar from macOS window
-(add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
-(add-to-list 'default-frame-alist '(ns-appearance . darl))
-(setq frame-title-format nil)
-
-(set-face-attribute 'default nil :font "Fira Code Retina-12")
-(set-frame-font "Fira Code Retina-12" nil t)
-
-;; more useful frame title
-(setq frame-title-format
-      '((:eval (if (buffer-file-name)
-                   (abbreviate-file-name (buffer-file-name))
-                 "%b"))))
+(global-auto-revert-mode t)                   ;; automatically revert buffer if changes are made outside of emacs
+
+(column-number-mode)
+(global-display-line-numbers-mode)
+
+(setq-default indent-tabs-mode nil)   ;; don't use tabs to indent
+(setq-default tab-width 8)            ;; but maintain correct appearance
+
+;; fancy title bar
+(when (memq window-system '(mac ns))
+  (add-to-list 'default-frame-alist '(ns-appearance . dark)) ; nil for dark text
+  (add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
+  (setq ns-use-proxy-icon nil))
+
+;; improve scrolling on macOS
+(setq mouse-wheel-scroll-amount (quote (0.01)))
+(setq mouse-wheel-progressive-speed nil)
+
+;; setup font with better retina support
+(set-face-attribute 'default nil :font "Fira Code Retina" :height 180)
+
+;; Configure ~escape~ to exit out of a command sequence.
+(global-set-key (kbd "<escape>") 'keyboard-escape-quit)
+
+;; Remove odd undo binding, and prepare it for search with swiper
+(global-unset-key (kbd "C-/"))
+
+;; write to "Time-stamp: <>" as long as it is within the first 8 lines of the file
+(add-hook 'before-save-hook 'time-stamp)
+
+;; always remove trailing whitespace
+(add-hook 'before-save-hook 'delete-trailing-whitespace)
 
 ;; enable y/n answers
 (fset 'yes-or-no-p 'y-or-n-p)
 
-;; Make emacs more performant
-(setq gc-cons-threshold 50000000)             ;; run gc every 50mb instead of 0.7mb
-(setq large-file-warning-threshold 100000000) ;; warn when opening files larger than 100MB
-
-;; nice scrolling
-(setq scroll-margin 0
-      scroll-conservatively 100000
-      scroll-preserve-screen-position 1)
-
-;; Better package browser
-(use-package paradox
-  :ensure t
-  :config
-  (paradox-enable))
-
-;; Better integration with macOS shell
-(use-package exec-path-from-shell
-  :ensure t
-  :config
-  (when (memq window-system '(mac ns x))
-    (exec-path-from-shell-initialize)
-    (exec-path-from-shell-copy-env "GOPATH")))
-
-;; hide minor modes to keep modeline clean
-(use-package diminish
-  :ensure t)
-
-;; Install Doom Themes
-(use-package doom-themes
-  :ensure t
-  :config
-  ;; Global settings (defaults)
-  (setq doom-themes-enable-bold t    ; if nil, bold is universally disabled
-        doom-themes-enable-italic t) ; if nil, italics is universally disabled
-  (load-theme 'doom-one-light t)
-  ;; Enable flashing mode-line on errors
-  (doom-themes-visual-bell-config)
-  ;; Enable custom treemacs theme
-  (setq doom-themes-treemacs-theme "doom-colors") ; use the colorful treemacs theme
-  (doom-themes-treemacs-config)
-  ;; Corrects (and improves) org-mode's native fontification.
-  (doom-themes-org-config))
-
-(use-package paren
-  :config
-  (show-paren-mode t))
-
-;; completion using ivy
+;; remove the whole line when Ctrl-K is pressed, instead of emptying it
+(setq kill-whole-line t)
+
+;; start find-file at your home directory
+(setq default-directory "~/")
+
 (use-package ivy
   :ensure t
-  :diminish (ivy-mode)
-  :bind (("C-x b" . ivy-switch-buffer))
-  :config
-  (ivy-mode t)
+  :bind ("C-x b" . ivy-switch-buffer)
+  :config
+  (ivy-mode 1)
   (setq ivy-use-virtual-buffers t)
   (setq ivy-display-style 'fancy))
 

(use-package projectile
  :ensure t
  :diminish projectile-mode
  :config
  (projectile-mode +1)
  (define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
  (setq projectile-project-search-path '("~/code/")))

(use-package treemacs
  :ensure t
  :bind
  (:map global-map
        ("C-<f8>" . treemacs-select-window)

  :after treemacs projectile
  :ensure t)

(use-package treemacs-magit
  :after treemacs magit
  :ensure t)

;; summary screen on open
(use-package dashboard
  :ensure t
  :init
  (setq dashboard-show-shortcuts    t
        dashboard-set-heading-icons t
        dashboard-set-file-icons    t
        dashboard-set-init-info     t
        dashboard-set-footer        nil)
  :config
  (dashboard-setup-startup-hook)
  (setq dashboard-items '((recents   . 10)
                          (agenda    . 5)
                          (projects  . 10))))

;; org mode configuration
(use-package org
  :ensure t
  :pin org
  :config
  (setq org-src-fontify-natively t)
  (setq org-log-done t)
  (setq org-confirm-babel-evaluate nil)
  (setq org-src-tab-acts-natively t)
  (setq org-cycle-separator-lines 1)
  (setq org-ellipsis " ▼")
  (setq org-directory "~/Documents/org")
  (setq org-agenda-files '("~/Documents/org/")))

;; Enable support for Golang
(use-package ob-go
  :ensure t)

;; org language support
(org-babel-do-load-languages
 'org-babel-load-languages
 '((C          . t)
   (sql        . t)
   (shell      . t)
   (sqlite     . t)
   (go         . t)
   (emacs-lisp . nil)))

;; Setup web-mode for gohtml and html
(use-package web-mode
