Skip to content

Instantly share code, notes, and snippets.

@sambatyon
Last active February 19, 2019 12:42
Show Gist options
  • Select an option

  • Save sambatyon/39cc4b0aa7c666251200 to your computer and use it in GitHub Desktop.

Select an option

Save sambatyon/39cc4b0aa7c666251200 to your computer and use it in GitHub Desktop.
Latest OS agnosic version
;;; package -- Sumary
;;; Commentary:
;;; Code:
;;; Set load path
(add-to-list 'load-path "~/.emacs.d/custom/")
;;;; PACKAGE SYSTEM
;; For some reason, this must be one of the first things to be loaded.
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/") t)
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/") t)
(package-initialize)
(require 'async)
;; Paradox extends package functionality.
;; see https://github.com/Malabarba/paradox
(require 'paradox)
(paradox-enable)
;;;; OVERRIDE SOME DEFAULT BEHAVIORS
;; Set default tab width
(setq-default default-tab-width 2)
;; Show matching parens.
(setq-default show-paren-delay 0)
(show-paren-mode 1)
;; Highlight current line.
(global-hl-line-mode t)
(when (not (display-graphic-p))
(set-face-background 'hl-line "#1c1c1c"))
(setq read-file-name-completion-ignore-case t)
;; Set macOS specifics
;; see http://ergoemacs.org/emacs/emacs_hyper_super_keys.html
(if (eq system-type 'darwin)
(progn
(setq mac-option-key-is-meta nil)
(setq mac-command-key-is-meta t)
(setq mac-command-modifier 'meta)
(setq mac-option-modifier nil)
(let ((default-directory "/usr/local/share/emacs/site-lisp/"))
(normal-top-level-add-subdirs-to-load-path))
;; Syncs bash path with emacs path
(require 'exec-path-from-shell)
(when (memq window-system '(mac ns))
(exec-path-from-shell-initialize))))
;; Set windows specifics
(if (eq system-type 'windows-nt)
(set-face-attribute 'default nil :family "Consolas" :height 100))
;; Add the path of homebrew
(if (eq system-type 'darwin)
(add-to-list 'exec-path "/usr/local/bin"))
;; If opening in UI mode, use a bigger window than the default.
(when (display-graphic-p)
(if (> (x-display-pixel-width) 1280)
(add-to-list 'default-frame-alist (cons 'width 95))
(add-to-list 'default-frame-alist (cons 'width 80)))
(add-to-list 'default-frame-alist (cons 'height 70)))
;; Disable the bell, too annoying
(setq ring-bell-function 'ignore)
;; Don't create backup~ files.
(setq make-backup-files nil)
;; Don't create #autosave# files.
(setq auto-save-default nil)
;; Treat camel case as subwords
(global-subword-mode 1)
;; Show lines and column numbers by default.
;; In old versions use `global-linum-mode`
(global-display-line-numbers-mode t)
(setq column-number-mode t)
;; Delete trailing whitespace in save
(add-hook 'before-save-hook 'delete-trailing-whitespace)
(setq require-final-newline t)
;; Do not use tabs, use spaces instead.
(setq-default indent-tabs-mode nil)
;; By default Emacs thinks a sentence is a full-stop followed by 2 spaces. Make
;; it a full-stop and 1 space.
(setq sentence-end-double-space nil)
;; Use always one letter responses.
(fset 'yes-or-no-p 'y-or-n-p)
;; Remember when I was in file.
(save-place-mode 1)
;; Set UTF8 as default encoding.
(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
;; 80 columns.
(setq-default whitespace-line-column 80)
(setq-default fill-column 72)
(setq-default comment-column 80)
;; Show ruler at column 80
(require 'fill-column-indicator)
(setq-default fci-rule-column 80)
(setq fci-handle-truncate-lines nil)
(define-globalized-minor-mode global-fci-mode fci-mode (lambda () (fci-mode 1)))
(global-fci-mode 1)
(defun auto-fci-mode (&optional unused)
"Ruler is shown only if the windows is longer.
UNUSED"
(if (> (window-width) fci-rule-column)
(fci-mode 1)
(fci-mode 0)))
(setq-default fci-rule-character-color "#9e9e9e")
(add-hook 'after-change-major-mode-hook 'auto-fci-mode)
(add-hook 'window-configuration-change-hook 'auto-fci-mode)
;; This may be necessary
;; (setq-default fci-rule-color "#e2e2e2")
;; (setq-default fci-rule-width 2)
;;;; AUTOMATIC GENERATED CODE
;; Needs to be before THEMES to avoid warnings.
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-safe-themes
(quote
("eb550fbd9252edf7e9bd2e09c52a0d8268f75a53d3acd13138c5c9aa741784d9"
"39bd37826d815604439e7cec009ad944f5874ca9749fcb5b32c3aa04615c8016"
default)))
'(git-gutter:added-sign "+")
'(git-gutter:deleted-sign "-")
'(git-gutter:lighter " GG")
'(git-gutter:modified-sign "~")
'(package-selected-packages
(quote
(bazel-mode psc-ide haskell-mode magit magit-gerrit magit-org-todos magit-todos rjsx-mode emojify rainbow-delimiters expand-region move-text toml-mode flycheck-inline flycheck elpy ag string-inflection google-this beacon olivetti paradox cargo clang-format cmake-mode company company-c-headers company-go d-mode exec-path-from-shell fill-column-indicator git-gutter go-eldoc go-guru go-mode go-projectile go-rename google-c-style json-mode json-navigator json-reformat json-snatcher markdown-mode projectile protobuf-mode rust-mode yaml-mode))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
;;;; USEFUL FUNCTIONS
;; Insert line above and indent.
;; Some terminals do a bad job sending the right codes to the emacs process.
;; In iTerm2 go to preferences→profiles→keys and add `[13;5u` for C+Enter
;; Also add `[59;5u` for C+;
;; See https://emacs.stackexchange.com/questions/1020/problems-with-keybindings-when-using-terminal/13957#13957
;; http://ascii-table.com/ansi-escape-sequences.php
;; https://www.rapidtables.com/code/text/ascii/ascii-enter.html
;; http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
(defun insert-and-indent-line-above ()
"Insert a new line above and set the cursor in the new line."
(interactive)
(push-mark)
(let*
((ipt (progn (back-to-indentation) (point)))
(bol (progn (move-beginning-of-line 1) (point)))
(indent (buffer-substring bol ipt)))
(newline)
(forward-line -1)
(insert indent)))
(global-set-key [ (control return) ] 'insert-and-indent-line-above)
;;;; PLUGINS
;;; Globaly used plugins
;; TODO: Finally get hunspell to work
;; Set global git gutter mode
;; no matter what, don't set `git-gutter:linum-setup`
;; see https://github.com/syohex/emacs-git-gutter/issues/156
(require 'git-gutter)
(global-git-gutter-mode t)
(global-set-key (kbd "C-x p") 'git-gutter:previous-hunk)
(global-set-key (kbd "C-x n") 'git-gutter:next-hunk)
(require 'magit)
(require 'log-edit)
(require 'vc-git)
(add-to-list 'auto-mode-alist '("COMMIT_EDITMSG\\'" . vc-git-log-edit-mode))
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
;; Projectile
;; see https://docs.projectile.mx/en/latest/configuration/
(require 'projectile)
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
(projectile-mode +1)
;; Shines a light whenever there is a scroll in the screen.
;; Very useful to locate the cursor.
(require 'beacon)
(beacon-mode 1)
(setq beacon-size 40)
(setq beacon-color "#ffff00")
;; Google this: Use C-c / g
(require 'google-this)
(google-this-mode 1)
;; string-inflection: To move between camelCase and snake_case
(require 'string-inflection)
(global-set-key (kbd "C-c C") 'string-inflection-camelcase) ;; CamelCase
(global-set-key (kbd "C-c L") 'string-inflection-lower-camelcase) ;; camelCase
(global-set-key (kbd "C-c S") 'string-inflection-underscore) ;; snake_case
;; The silver searcher support
;; see https://agel.readthedocs.io/en/latest/usage.html
(require 'ag)
;; Move text up and down with M-up and M-down
(require 'move-text)
(move-text-default-bindings)
;; Expands region by semantic units, hard to explain but check this:
;; https://www.youtube.com/watch?time_continue=31&v=_RvHz3vJ3kA
(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)
;; (require 'rainbow-delimiters)
;; (add-hook 'prog-mode-hook #'rainbow-delimiters-mode)
;; Configure HideShow for code folding
;; see https://www.emacswiki.org/emacs/HideShow
(defun toggle-selective-display (column)
"Hide code according to its indentation level.
COLUMN"
(interactive "P")
(set-selective-display
(or column
(unless selective-display
(1+ (current-column))))))
(defun toggle-hiding (column)
"More robust version of `hs-toggle-hiding`.
COLUMN"
(interactive "P")
(if (hs-minor-mode)
(if (condition-case nil
(hs-toggle-hiding)
(error t))
(hs-show-all))
(toggle-selective-display column)))
(setq-default hs-isearch-open t) ;; Open blocks when searching.
(add-hook 'prog-mode-hook 'hs-minor-mode)
(global-set-key (kbd "C-+") 'toggle-hiding) ;; TODO: Needs iTerm2 support
(defun display-code-line-counts (ov)
"Show hidden code in a tool tip.
OV"
(when (eq 'code (overlay-get ov 'hs))
(overlay-put ov 'help-echo
(buffer-substring (overlay-start ov)
(overlay-end ov)))))
(setq-default hs-set-up-overlay 'display-code-line-counts)
(setq-default hs-hide-comments nil) ;; Hide comments too.
(defadvice goto-line (after expand-after-goto-line activate compile)
"Hideshow-expand affected block when using `goto-line` in a collapsed buffer."
(save-excursion
(hs-show-block)))
;; TODO: Get `server` and `with-editor` and get emacs in client server.
;;; Language plugins (Try to order them as they are most used)
;; C++ plugin, your bread and butter.
(require 'google-c-style)
(add-hook 'c-mode-common-hook 'google-set-c-style)
(add-hook 'c-mode-common-hook 'google-make-newline-indent)
;; Clang format
(require 'clang-format)
(global-set-key [C-M-tab] 'clang-format-region)
;; Cmake
(require 'cmake-mode)
;; Bazel
(require 'bazel-mode)
(add-to-list 'auto-mode-alist '("BUILD$" . bazel-mode))
(add-to-list 'auto-mode-alist '("\\.blz$" . bazel-mode))
;; Flycheck ¿Evaluating?
(require 'flycheck)
(require 'cus-edit)
(setq flycheck-display-errors-delay 0.3)
(add-hook 'prog-mode-hook 'flycheck-mode)
(add-hook 'prog-mode-hook 'flycheck-inline-mode)
;; TODO: Add cquery support.
;; Python with Elpy
;; see https://elpy.readthedocs.io/en/latest/ide.html
(require 'elpy)
(setq flycheck-python-flake8-executable "python3")
(setq flycheck-python-pylint-executable "python3")
(setq flycheck-python-pycompile-executable "python3")
(setq elpy-rpc-python-command "python3")
(setq python-shell-interpreter "python3")
(elpy-enable)
;; Shell
(setq-default sh-basic-offset 2)
(setq-default sh-indentation 2)
(add-hook 'shell-mode-hook
'(lambda ()
(define-key shell-mode-map "\C-m" 'newline-and-indent)))
;; Go lang
(require 'go-mode)
(add-to-list 'auto-mode-alist '("\\.go$" . go-mode))
(add-hook 'go-mode-hook
(lambda ()
(add-hook 'before-save-hook 'gofmt-before-save)
;; Company requires https://github.com/nsf/gocode
(company-mode)
(set (make-local-variable 'company-backends) '(company-go))
(setq tab-width 4)
(setq indent-tabs-mode 1)))
;; M⬇
(require 'markdown-mode)
(add-to-list 'auto-mode-alist '("\\.text$" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.markdown$" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md$" . markdown-mode))
;; JSON
(require 'json-mode)
(setq js-indent-level 2)
(add-hook 'js-mode-hook
'(lambda ()
(define-key js-mode-map "\C-m" 'newline-and-indent)))
(add-hook 'json-mode-hook
'(lambda ()
(make-local-variable 'js-indent-level)
(setq js-indent-level 2)))
;; YAML
(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
(add-to-list 'auto-mode-alist '("\\.yaml$" . yaml-mode))
(add-hook 'yaml-mode-hook
'(lambda ()
(define-key yaml-mode-map "\C-m" 'newline-and-indent)))
;; Protobuf
(require 'protobuf-mode)
(add-hook
'protobuf-mode-hook
'(lambda ()
(define-key protobuf-mode-map "\C-m" 'newline-and-indent)))
(defconst my-protobuf-style
'((c-basic-offset . 2)
(indent-tabs-mode . nil)))
(add-hook
'protobuf-mode-hook
(lambda () (c-add-style "my-style" my-protobuf-style t)))
;; React.js support
;; Documentation here https://github.com/felipeochoa/rjsx-mode
(require 'rjsx-mode)
;; Typescript support
;; Documentation https://github.com/ananthakumaran/tide
(defun setup-tide-mode ()
"Enables TIDE mode."
(interactive)
(tide-setup)
(flycheck-mode +1)
(setq flycheck-check-syntax-automatically '(save mode-enabled))
(eldoc-mode +1)
(tide-hl-identifier-mode +1)
(company-mode +1))
(setq company-tooltip-align-annotations t) ;; aligns annotation to the right hand side
(add-hook 'before-save-hook 'tide-format-before-save)
(add-hook 'typescript-mode-hook #'setup-tide-mode)
;; PureScript support
;; Docmentation https://github.com/epost/psc-ide-emacs
(require 'psc-ide)
(add-hook 'purescript-mode-hook
(lambda ()
(psc-ide-mode)
(company-mode)
(flycheck-mode)
(turn-on-purescript-indentation)))
;; Rust
(require 'rust-mode)
(require 'cargo)
;; D
(require 'd-mode)
;; Haskell
(require 'haskell-mode)
;; Olivetti for nice writing environments
;; Incompatible with linum, disable with `display-line-numbers-mode`
(setq-default olivetti-body-width 80)
(require 'olivetti)
(add-hook
'olivetti-mode-hook
'(lambda ()
(progn
(display-line-numbers-mode 0)
(fci-mode 0))))
;; Tom's markup language support
(require 'toml-mode)
;; Configure org mode
(setq-default org-directory
(expand-file-name "org" (expand-file-name "~/OneDrive/Documents")))
(add-hook 'org-mode-hook 'org-indent-mode)
;; TODO Get Bazel support
;;;; THEMES
;; Load Panic theme
(require 'panic-theme)
(load-theme 'panic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment