My emacs init file for 2015

I’m going to make it a tradition to post my emacs init file every year and compare it to the year before, although I won’t post the file I used in 2013 and 2014 I can simply state that the biggest change is that I’ve thrown out ECB.

ECB simply didn’t work for any other language than Clojure and PicoLisp and not very well at that plus a working find-tag via jump-to-current-word simply renders the point of ECB moot. However it would be kind of nice to be able to run a command to open a new buffer with links to line numbers for all definitions in the current file, shouldn’t be too hard to implement either, we’ll see if I manage in 2015. ECB is out anyway.

Another simpler TODO for 2015 is fix flymake for PHP so that it doesn’t break down and cry all the time when it encounters error strings it can’t recognize.

Without further ado, here it is:

(require 'package)

(setq package-archives '(("gnu" . "")
                         ("marmalade" . "")
                         ("melpa-stable" . "")))


(defvar my-packages '(better-defaults paredit clojure-mode cider zenburn-theme auto-complete exec-path-from-shell js2-mode yasnippet autopair flx-ido flymake fuzzy smex company projectile web-mode idle-highlight-mode))

(dolist (p my-packages)
  (when (not (package-installed-p p))
    (package-install p)))

(setq tab-width 2
      indent-tabs-mode nil)

(load-theme 'zenburn t)

(add-to-list 'load-path "/home/henrik/.emacs.d/henrik")

(require 'picolisp)
(paredit-mode t)

(add-hook 'clojure-mode-hook 'paredit-mode)

(setq gc-cons-threshold 20000000)

(setq stack-trace-on-error t)

(defun rerepl ()
  (dolist (buffer (buffer-list))
    (when (string-prefix-p "*nrepl" (buffer-name buffer))
      (kill-buffer buffer)))
  (nrepl-jack-in nil))
(defun browser-jump (base-url)
  (let (myword myurl)
    (setq myword
          (if (and transient-mark-mode mark-active)
              (buffer-substring-no-properties (region-beginning) (region-end))
            (thing-at-point 'symbol)))
    (setq myurl (concat base-url myword))
    (browse-url myurl)))

(defun get-current-word ()
  (if (and transient-mark-mode mark-active)
      (buffer-substring-no-properties (region-beginning) (region-end))
    (thing-at-point 'word)))

(defun jump-to-current-word ()
  (find-tag (get-current-word)))

(defun clojure-jump () (interactive) (browser-jump ""))

(defun php-jump ()
  (let (myword myurl)
    (setq myword
          (if (and transient-mark-mode mark-active)
              (buffer-substring-no-properties (region-beginning) (region-end))
            (thing-at-point 'symbol)))
    (setq myurl (concat "" (replace-regexp-in-string "_" "-" myword) ".php"))
    (browse-url myurl)))

(defun picolisp-jump ()
  (let (myword myurl)
    (setq myword
          (if (and transient-mark-mode mark-active)
              (buffer-substring-no-properties (region-beginning) (region-end))
            (thing-at-point 'symbol)))
    (setq myurl (concat "http://localhost/documents/picolisp/ref" (upcase (substring myword 0 1)) ".html#" myword))
    (browse-url myurl)))

(fset 'sqbracket-wrap
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("\213[" 0 "%d")) arg)))

(fset 'curlybrace-wrap
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("\213{" 0 "%d")) arg)))
(defun lisp-enable-paredit-hook () (paredit-mode 1))
(add-hook 'picolisp-mode-hook 'lisp-enable-paredit-hook)
(add-hook 'emacs-lisp-mode-hook 'lisp-enable-paredit-hook)

(setq kill-whole-line t)

(defun refresh-file ()
  (revert-buffer t t t))

(require 'auto-complete)
(require 'auto-complete-config)
(setq ac-auto-start 3)
(add-to-list 'ac-modes 'picolisp-mode)
(add-to-list 'ac-modes 'clojure-mode)
(add-to-list 'ac-modes 'web-mode)
(setq ac-ignore-case t)
(global-set-key (kbd "C-<tab>") 'auto-complete)

(add-to-list 'load-path "/home/henrik/.emacs.d/henrik/completion-ui")
(require 'completion-ui)
(auto-completion-mode nil)
(global-set-key (kbd "s-i") 'complete-etags)

(add-to-list 'auto-mode-alist '("\\.l\\'" . picolisp-mode))
(add-to-list 'auto-mode-alist '("\.cljs$" . clojure-mode))


(setq nrepl-hide-special-buffers t)

(require 'rainbow-delimiters)
(add-hook 'clojure-mode-hook 'rainbow-delimiters-mode)
(add-hook 'picolisp-mode-hook 'rainbow-delimiters-mode)
(add-hook 'emacs-lisb-mode-hook 'rainbow-delimiters-mode)

(setq column-number-mode t)

(defun switch-full-screen ()
  (shell-command "wmctrl -r :ACTIVE: -btoggle,fullscreen"))

;;(global-set-key [f11] 'switch-full-screen)
(global-set-key [f5] 'refresh-file)
(global-set-key (kbd "C-z") 'undo)
(global-set-key (kbd "C-M-m") 'mark-sexp)
(global-set-key (kbd "M-[") 'sqbracket-wrap)
(global-set-key (kbd "M-.") 'jump-to-current-word)
;;(global-set-key [f1] 'menu-bar-mode)
(global-set-key [f6] 'split-window-horizontally)
(global-set-key [f7] 'split-window-vertically)
(global-set-key [f8] 'delete-window)
(global-set-key (kbd "C-c c")        'comment-region)
(global-set-key (kbd "C-c C-u c")    'uncomment-region)
(global-set-key (kbd "C-x C-b") 'buffer-menu)
(global-set-key (kbd "M-x") 'smex)

(eval-after-load 'clojure-mode '(define-key clojure-mode-map (kbd "M-{") 'curlybrace-wrap))
(eval-after-load 'clojure-mode '(define-key clojure-mode-map (kbd "s-d") 'clojure-jump))
(define-key picolisp-mode-map (kbd "s-d") 'picolisp-jump)
(eval-after-load 'web-mode '(define-key web-mode-map (kbd "s-d") 'php-jump))

(autoload 'hide-lines "hide-lines" "Hide lines based on a regexp" t)
(defun hide-docstrings ()
  (hide-matching-regions "^[ ]*\"[\0-\377[:nonascii:]]*?\""))
(global-set-key (kbd "C-c C-h") 'hide-docstrings)
(global-set-key (kbd "C-c C-u h") 'show-all-invisible)

(set-face-foreground 'show-paren-match-face "#FF0000")

(set-face-attribute 'default nil :height 100)

(defun shift-region (distance)
  (let ((mark (mark)))
      (indent-rigidly (region-beginning) (region-end) distance)
      (push-mark mark t t)
      (setq deactivate-mark nil))))

(defun shift-right ()
  (shift-region 2))

(defun shift-left ()
  (shift-region -2))

(global-set-key (kbd "C-c <right>") 'shift-right)
(global-set-key (kbd "C-c <left>") 'shift-left)

;;(require 'yasnippet)
;;(yas-global-mode 1)
;;(global-set-key (kbd "s-y") 'yas-expand)

(require 'autopair)
(autopair-global-mode) ;; enable autopair in all buffers
(add-hook 'web-mode 'autopair-mode) 
(setq autopair-autowrap t)

(require 'flx-ido)
(ido-mode 1)
(ido-everywhere 1)
(flx-ido-mode 1)
;; disable ido faces to see flx highlights.
(setq ido-use-faces nil)

(require 'projectile)
(setq projectile-indexing-method 'native)
(setq projectile-enable-caching t)

(require 'web-mode) 
(add-hook 'web-mode-hook 'autopair-mode)
(add-to-list 'auto-mode-alist '("\\.php$" . web-mode))
(add-to-list 'auto-mode-alist '("\\.css$" . web-mode))
(add-to-list 'auto-mode-alist '("\\.js$" . web-mode))

(add-hook 'web-mode-hook
          #'(lambda ()
              (modify-syntax-entry ?' "\"")

(setq web-mode-markup-indent-offset 2)
(setq web-mode-css-indent-offset 2)
(setq web-mode-code-indent-offset 2)

(defun idlehl-hook ()
  (make-local-variable 'column-number-mode)
  (column-number-mode t)
  (if window-system (hl-line-mode t))
    (idle-highlight-mode t))

(add-hook 'emacs-lisp-mode-hook 'idlehl-hook)
(add-hook 'picolisp-mode-hook 'idlehl-hook)
(add-hook 'clojure-mode-hook 'idlehl-hook)
(add-hook 'web-mode-hook 'idlehl-hook)


(require 'flymake)
(defun flymake-php-init ()
  "Use php to check the syntax of the current file."
  (let* ((temp (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace))
	 (local (file-relative-name temp (file-name-directory buffer-file-name))))
    (list "php" (list "-f" local "-l"))))
(add-to-list 'flymake-err-line-patterns 
  '("\\(Parse\\|Fatal\\) error: +\\(.*?\\) in \\(.*?\\) on line \\([0-9]+\\)$" 3 4 nil 2))
(add-to-list 'flymake-allowed-file-name-masks '("\\.php$" flymake-php-init))
(add-hook 'web-mode-hook (lambda () (flymake-mode 1)))

(dolist (command '(yank yank-pop))
  (eval `(defadvice ,command (after indent-region activate)
           (and (not current-prefix-arg)
                (member major-mode '(emacs-lisp-mode lisp-mode
                                                     clojure-mode    scheme-mode
                                                     haskell-mode    ruby-mode
                                                     rspec-mode      python-mode
                                                     c-mode          c++-mode
                                                     objc-mode       latex-mode
                                                     plain-tex-mode  web-mode
                                                     picolisp-mode    js2-mode))
                (let ((mark-even-if-inactive transient-mark-mode))
                  (indent-region (region-beginning) (region-end) nil))))))

(electric-indent-mode 1)
(set-cursor-color "#aaaaaa")

(setq uniquify-buffer-name-style 'reverse)
(setq inhibit-default-init t)
;;(setq-default frame-title-format "%b (%f)")

(global-set-key "\M-n" "\C-u1\C-v")
(global-set-key "\M-p" "\C-u1\M-v")

;; (defun kill-and-join-forward (&optional arg)
;;   "If at end of line, join with following; otherwise kill line. Deletes whitespace at join."
;;   (interactive "P")
;;   (if (and (eolp) (not (bolp)))
;;       (delete-indentation t)
;;     (kill-line arg)))

(defun move-line-down ()
  (let ((col (current-column)))
      (transpose-lines 1))
    (move-to-column col)))

(defun move-line-up ()
  (let ((col (current-column)))
      (transpose-lines -1))
    (move-to-column col)))

(eval-after-load 'web-mode '(define-key web-mode-map (kbd "<M-down>") 'move-line-down))
(eval-after-load 'web-mode '(define-key web-mode-map (kbd "<M-up>") 'move-line-up))

(defun newline-and-indent-as-above ()
  (let* ((cline (thing-at-point 'line))
         (start (string-match "\\`[ \t]+" cline))
         (end (match-end 0))
         (indent (substring cline start end )))
    (insert indent)))

(global-set-key (kbd "<C-return>") 'newline-and-indent-as-above)

 '(completion-highlight-face ((t (:background "grey" :foreground "white")))))

(defun duplicate-line ()
    (let ((kill-read-only-ok t) deactivate-mark)
      (toggle-read-only 1)
      (toggle-read-only 0)

(global-set-key (kbd "C-d") 'duplicate-line)

(defun load-vs ()
  (let ((bdir "/var/www/videoslots"))
    (cd bdir)
    (shell-command "ctags-exuberant -e -R --languages=php")
    (visit-tags-table "TAGS")    
    (find-file "index.php")))

(defun load-pl ()
  (let ((bdir "/opt/picolisp"))
    (cd bdir)
    (shell-command "ctags-exuberant -e -R --languages=picolisp")
    (visit-tags-table "TAGS")    
    (find-file "scratch.l")))

(setq pop-up-windows nil)

(defun my-display-buffer-function (buf not-this-window)
  (if (and (not pop-up-frames)
           (or not-this-window
               (not (eq (window-buffer (selected-window)) buf)))
           (> (frame-width) 162))
  ;; Note: Some modules sets `pop-up-windows' to t before calling
  ;; `display-buffer' -- Why, oh, why!
  (let ((display-buffer-function nil)
        (pop-up-windows nil))
    (display-buffer buf not-this-window)))

(setq display-buffer-function 'my-display-buffer-function)

(defun do-tab ()
  (insert "  "))

(global-set-key (kbd "s-f") 'other-window)
(global-set-key (kbd "s-s") 'do-tab)
(global-set-key (kbd "<s-down>") (kbd "C-u - 5 C-x ^"))
(global-set-key (kbd "<s-up>") (kbd "C-u 5 C-x ^"))
(global-set-key (kbd "<s-right>") (kbd "C-u 5 C-x }"))
(global-set-key (kbd "<s-left>") (kbd "C-u 5 C-x {"))
(global-set-key "\C-k" 'kill-and-join-forward)

(defun inferior-pl ()
  (cd "/opt/picolisp")
  (run-picolisp "bin/picolisp"))
(autoload 'session-jump-to-last-change "session")
(global-set-key (kbd "C-x C-/") 'session-jump-to-last-change)

Related Posts