Skip to content

Instantly share code, notes, and snippets.

@steenhansen
Last active September 1, 2019 05:47
Show Gist options
  • Save steenhansen/ea700363622b984c6ec27df614d3b98f to your computer and use it in GitHub Desktop.
Save steenhansen/ea700363622b984c6ec27df614d3b98f to your computer and use it in GitHub Desktop.
emacs-init
;;;;
;; Packages
;;;;
;; Define package repositories
(require 'package)
(add-to-list 'package-archives
'("tromey" . "http://tromey.com/elpa/") t)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(add-to-list 'package-archives
'("melpa-stable" . "http://stable.melpa.org/packages/") t)
(add-to-list 'package-pinned-packages '(cider . "melpa-stable") t)
(add-to-list 'package-pinned-packages '(magit . "melpa-stable") t)
;; Load and activate emacs packages. Do this first so that the
;; packages are loaded before you start trying to modify them.
;; This also sets the load path.
(package-initialize)
;; Download the ELPA archive description if needed.
;; This informs Emacs about the latest versions of all packages, and
;; makes them available for download.
(when (not package-archive-contents)
(package-refresh-contents))
;; Define he following variables to remove the compile-log warnings
;; when defining ido-ubiquitous
;; (defvar ido-cur-item nil)
;; (defvar ido-default-item nil)
;; (defvar ido-cur-list nil)
;; (defvar predicate nil)
;; (defvar inherit-input-method nil)
;; The packages you want installed. You can also install these
;; manually with M-x package-install
;; Add in your own as you wish:
(defvar my-packages
'(;; makes handling lisp expressions much, much easier
;; Cheatsheet: http://www.emacswiki.org/emacs/PareditCheatsheet
paredit
;; key bindings and code colorization for Clojure
;; https://github.com/clojure-emacs/clojure-mode
clojure-mode
;; extra syntax highlighting for clojure
clojure-mode-extra-font-locking
;; integration with a Clojure REPL
;; https://github.com/clojure-emacs/cider
cider
;; allow ido usage in as many contexts as possible. see
;; customizations/navigation.el line 23 for a description
;; of ido
ido-completing-read+
;; Enhances M-x to allow easier execution of commands. Provides
;; a filterable list of possible commands in the minibuffer
;; http://www.emacswiki.org/emacs/Smex
smex
;; colorful parenthesis matching
rainbow-delimiters
;; edit html tags like sexps
tagedit
;; git integration
magit))
;; On OS X, an Emacs instance started from the graphical user
;; interface will have a different environment than a shell in a
;; terminal window, because OS X does not run a shell during the
;; login. Obviously this will lead to unexpected results when
;; calling external utilities like make from Emacs.
;; This library works around this problem by copying important
;; environment variables from the user's shell.
;; https://github.com/purcell/exec-path-from-shell
(if (eq system-type 'darwin)
(add-to-list 'my-packages 'exec-path-from-shell))
(dolist (p my-packages)
(when (not (package-installed-p p))
(package-install p)))
;; Place downloaded elisp files in ~/.emacs.d/vendor. You'll then be able
;; to load them.
;;
;; For example, if you download yaml-mode.el to ~/.emacs.d/vendor,
;; then you can add the following code to this file:
;;
;; (require 'yaml-mode)
;; (add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
;;
;; Adding this code will make Emacs enter yaml mode whenever you open
;; a .yml file
(add-to-list 'load-path "~/.emacs.d/vendor")
;;;;
;; Customization
;;;;
;; Add a directory to our load path so that when you `load` things
;; below, Emacs knows where to look for the corresponding file.
(add-to-list 'load-path "~/.emacs.d/customizations")
;; Sets up exec-path-from-shell so that Emacs will use the correct
;; environment variables
(load "shell-integration.el")
;; These customizations make it easier for you to navigate files,
;; switch buffers, and choose options from the minibuffer.
(load "navigation.el")
;; These customizations change the way emacs looks and disable/enable
;; some user interface elements
(load "ui.el")
;; These customizations make editing a bit nicer.
(load "editing.el")
;; Hard-to-categorize customizations
(load "misc.el")
;; For editing lisps
(load "elisp-editing.el")
;; Langauage-specific
(load "setup-clojure.el")
(load "setup-js.el")
(require 'hiwin)
(hiwin-activate) ;; hiwin-mode active blacker background
(set-face-background 'hiwin-face "gray10")
(menu-bar-mode 1)
(tool-bar-mode -1)
(setq ibuffer-expert t)
(setq ibuffer-show-empty-filter-groups nil)
(setq-default ibuffer-default-sorting-mode 'mode-name)
(setq ibuffer-saved-filter-groups
(quote (("default"
("CIDER" (or (name . "^\\*cider-repl.*")
(name . "^\\*cider-error\\*$")))
(" CORE" (filename . "core.*\\.clj"))
(" Src" (filename . "^.*\\/src\\/.*\\.clj"))
(" Test" (filename . "^.*\\/test\\/.*\\.*"))
(" PROC" (name . "^Procfile$") )
(" Proj" (filename . "clj"))
(" Eden" (filename . "edn"))
(" Html" (filename . "html"))
(" Css " (filename . "css"))
(" Text" (filename . "txt"))
(" Md " (filename . "md"))
("dired" (mode . dired-mode))
("emacs" (or (name . "^\\*scratch\\*$")
(name . "^\\*Messages\\*$")))))))
(add-hook 'ibuffer-mode-hook
'(lambda ()
(ibuffer-auto-mode 1)
(ibuffer-switch-to-saved-filter-groups "default")))
;(add-to-list 'ibuffer-never-show-predicates "^\\:") ;; hide ":d:/..."
(add-to-list 'ibuffer-never-show-predicates "*dashboard*")
(add-to-list 'ibuffer-never-show-predicates "*nrepl")
(setq cider-ns-save-files-on-refresh t)
(setq cider-repl-require-ns-on-set t)
(setq compilation-auto-jump-to-first-error t)
(setq cider-auto-select-error-buffer nil)
(cua-mode t) ; https://www.emacswiki.org/emacs/CuaMode
(setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands
(transient-mark-mode 1) ;; No region when it is not highlighted
(setq cua-keep-region-after-copy t) ;; Standard Windows behaviour
(setq column-number-mode t)
(defun sidebar-toggle ()
"Toggle both `dired-sidebar' and `ibuffer-sidebar'."
(interactive)
(dired-sidebar-toggle-sidebar)
(ibuffer-sidebar-toggle-sidebar))
(require 'highlight-symbol)
(require 'kmb) ; elpa version doesn't work in windows :-(
(require 'delsel) ; delete is normal now
(defun change-dired-sidebar () ; https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Name-Components.html
"move dired-sidebar root to current buffer"
(interactive)
(dired-sidebar-switch-to-dir (file-name-directory default-directory)))
(require 'dashboard)
(dashboard-setup-startup-hook) ; https://github.com/emacs-dashboard/emacs-dashboard
(setq dashboard-items '((recents . 10)
(bookmarks . 10)))
(require 'zoom-frm) ; https://www.emacswiki.org/emacs/zoom-frm.el
(eval-after-load 'flycheck '(flycheck-clojure-setup)) ;https://github.com/clojure-emacs/squiggly-clojure
(add-hook 'after-init-hook #'global-flycheck-mode)
(eval-after-load 'flycheck
'(setq flycheck-display-errors-function #'flycheck-pos-tip-error-messages))
(centaur-tabs-mode t)
(setq centaur-tabs-modified-marker "O")
(setq centaur-tabs-style "bar")
(setq centaur-tabs-height 32)
(setq centaur-tabs-set-icons t)
(setq centaur-tabs-set-bar 'over)
(setq centaur-tabs-set-modified-marker t)
(centaur-tabs-headline-match)
(setq centaur-tabs-set-icons t) ; https://github.com/ema2159/centaur-tabs/blob/master/README.org
(run-with-idle-timer 2 t (lambda () (save-some-buffers t))) ; https://www.gnu.org/software/emacs/manual/html_node/elisp/Idle-Timers.html
(defun hide-errors-reload ()
"erase *cider-error* buffer, then reload ALL buffers. So can tell if there is NO CURRENT error by the absence of *cider-error* buffer"
(interactive)
(kmb-kill-matching-buffers-no-ask "\*cider-error\*")
(cider-ns-reload-all)) ; https://cider.readthedocs.io/en/latest/miscellaneous_features/#reloading-code
;; scroll one line at a time (less "jumpy" than defaults)
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1))) ;; one line at a time
(setq mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
(setq mouse-wheel-follow-mouse 't) ;; scroll window under mouse
(setq scroll-step 1) ;; keyboard scroll one line at a time
(scroll-bar-mode t)
(defun comment-or-uncomment-line-or-region ()
"Comments or uncomments the current line or region."
(interactive)
(if (region-active-p)
(comment-or-uncomment-region (region-beginning) (region-end))
(comment-or-uncomment-region (line-beginning-position) (line-end-position))))
(defun kill-search-buffs ()
(interactive)
(dolist (buffer (buffer-list))
(let ((name (buffer-name buffer)))
(if (string-prefix-p "*ag " name)
(kill-buffer buffer)))))
(defun kill-non-core-cljs ()
(interactive)
(dolist (buffer (buffer-list))
(let ((name (buffer-name buffer)))
(if (string-suffix-p ".clj" name)
(if (not (string-prefix-p "core" name) )
(kill-buffer buffer))))))
(global-set-key [f1] 'hide-errors-reload)
(global-set-key [f2] 'cider-load-buffer)
(global-set-key [f3] 'cider-repl-clear-buffer)
(global-set-key [f4] 'cider-repl-set-ns)
(global-set-key [f5] 'cider-jack-in)
(global-set-key [f6] 'delete-window)
(global-set-key [f7] 'split-window-below)
(global-set-key [f8] 'split-window-right)
(global-set-key [f9] 'comment-or-uncomment-line-or-region)
(global-set-key [f10] 'cider-format-buffer)
;(global-set-key [f11] 'view-full-screen)
(global-set-key [f12] 'paredit-mode)
(require 'key-chord) ; https://www.emacswiki.org/emacs/KeyChord
(key-chord-mode 1)
(key-chord-define-global "KK" 'kill-whole-line)
(key-chord-define-global "kk" 'kill-whole-line)
(key-chord-define-global "--" 'zoom-frm-out)
(key-chord-define-global "==" 'zoom-frm-in)
(key-chord-define-global "^^" 'change-dired-sidebar)
(key-chord-define-global "\\\\" 'kill-search-buffs )
(key-chord-define-global "''" 'kill-non-core-cljs )
(key-chord-define-global "``" 'highlight-symbol)
(key-chord-define-global "QQ" 'ag)
(key-chord-define-global "qq" 'ag) ; https://agel.readthedocs.io/en/latest/usage.html#running-a-search
(key-chord-define-global "AA" 'mark-whole-buffer)
(key-chord-define-global "aa" 'mark-whole-buffer)
(key-chord-define-global "ZZ" 'sidebar-toggle)
(key-chord-define-global "zz" 'sidebar-toggle) ; https://github.com/jojojames/ibuffer-sidebar
;;;;; start mouse-1 click menu ;;;;;;;;;;;;;;;;;;;;;
(defun menu-core-clj(a-buff)
"format mouse-buffer entry as a clojure file, highlight if starts with 'core'"
(format "%1$s%2$s%3$s%4$s%5$s"
(with-current-buffer a-buff
(if buffer-read-only "%" ""))
(if (buffer-modified-p a-buff) "*" "")
(if (string-prefix-p "test" (buffer-name a-buff) ) " " "") ; test_file.clj
(buffer-name a-buff)
(if (string-prefix-p "core" (buffer-name a-buff) ) " ***" ""))) ; core.clj ***
(defun menu-buff-good (a-buff)
"ignore emacs buffers on mouse-buffer-menu"
(if (or (equal (buffer-name a-buff) "*dashboard*" )
(equal (buffer-name a-buff) "*scratch*")
(string-prefix-p "*nrepl-server _clojure/" (buffer-name a-buff) ) ; ignore these buffers in buffer menu
(string-prefix-p ":" (buffer-name a-buff) )
(equal (buffer-name a-buff) "*:Buffers:*")
(equal (buffer-name a-buff) "*Messages*"))
nil
t))
(defun menu-no-case-sort (a-buff-1 a-buff-2)
"ensure mouse-buffer-menu is sorted with no-case"
(string> (downcase (buffer-name a-buff-1)) (downcase (buffer-name a-buff-2))))
(defun menu-is-ag (a-buff)
"is the buffer an ag-search buffer?"
(if (string-prefix-p "*ag " (buffer-name a-buff) )
t
nil))
(defun menu-is-clj (a-buff)
"is the buffer a clojure buffer?"
(if (string-suffix-p ".clj" (buffer-name a-buff) )
t
nil))
(defun menu-no-start-blank (a-buff)
"ignore buffers starting with a blank space"
(if (/= (aref (buffer-name a-buff) 0) ?\s)
t
nil))
(defun menu-default(a-buff)
"format mouse-buffer entry as a default file"
(format " %1$s%2$s%3$s%4$s"
(with-current-buffer a-buff
(if buffer-read-only "%" ""))
(if (buffer-modified-p a-buff) "*" "") ; readme.txt ---
(buffer-name a-buff)
" ---"))
(defun menu-ag-search(a-buff)
"format mouse-buffer entry as an ag-search"
(format "%1$s"
(concat "~~~search : " (nth 1 (split-string (buffer-name a-buff) "text:\\| dir:")))))
;; https://superuser.com/questions/578521/can-i-customize-the-format-of-emacs-buffers-menu
(defun mouse-buffer-menu-alist (the-buffers)
(let ( (mouse-click-menu )
(the-dashboard ))
(setq sorted-buffs (sort the-buffers `menu-no-case-sort))
(while sorted-buffs
(let ((a-buff (car sorted-buffs)))
(if (eq "*dashboard*" (buffer-name a-buff))
(setq the-dashboard a-buff))
(if (menu-buff-good a-buff)
(if (menu-no-start-blank a-buff)
(setq mouse-click-menu
(cons
(cons (cond ((menu-is-ag a-buff) (menu-ag-search a-buff))
((menu-is-clj a-buff) (menu-core-clj a-buff))
(t (menu-default a-buff)))
a-buff)
mouse-click-menu)))))
(setq sorted-buffs (cdr sorted-buffs)))
(if (eq 0 (length mouse-click-menu))
(setq mouse-click-menu
(cons (cons "*dashboard*"
the-dashboard)
mouse-click-menu)))
mouse-click-menu))
(setq mouse-buffer-menu-mode-mult 1234) ;; deter sub-menus on control-click
;;;;; end mouse-1 click menu ;;;;;;;;;;;;;;;;;;;;;
(defface my-compile-face
'((t (:foreground "black" :background "white")))
"Compile buffer highlighting of busy signal so can see when done"
:group 'faces)
;; https://www.gnu.org/software/emacs/manual/html_node/eintr/Mode-Line.html
(setq-default mode-line-format
(quote("- "
(:eval (if (eq buffer-file-name nil) (propertize "[" 'face 'my-compile-face))) ; for compiling signal first
mode-line-process
(:eval (if (eq buffer-file-name nil) (propertize "]" 'face 'my-compile-face)))
" "
mode-line-mule-info
" "
mode-line-modified
" "
(:eval (substring (system-name) 0 (string-match "\\..+" (system-name))))
"~"
default-directory)))
; https://raw.githubusercontent.com/technomancy/clojure-mode/master/clojure-mode.el
(modify-syntax-entry ?\/ "'" clojure-mode-syntax-table) ; one-word/another-word have / be word boundry
(set-face-foreground 'linum "DarkSlateGray")
; F1:reload-cider-all F2:reload-file
; F3:cider-clear F4:cider-set-ns F5:cider-jack-in
;
; F6:del-win F7:ver-split F8:hor-split F9:comment-code
; F10:format-code F11:full-screen F12:para-mode
; highlight-and-
; -count-text:~~ C-<SPC>:start mark --:zoom-out ==:zoom-in
; search-text:QQ MOUSE-3:end region KK:kill-line \\:kill-searches
; select-all:AA '':kill-non-core.cljs
; sidebar-toggle:ZZ C-x C-f:create file C-x C-e:Execute eLisp
;;;;;;;;;;;;;;;;;;;;;;;;; current project ;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq default-directory "D:/_clojure/stat/src" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment