Gee, what're we gonna do tonight, Brain?

This commit is contained in:
Henrik Lissner 2014-11-29 20:21:03 -05:00
parent 4df3c32a8d
commit 3a79705475
44 changed files with 1806 additions and 1237 deletions

View file

@ -1,10 +1,6 @@
(provide 'my-commands)
;;;; Defun Commands ;;;;;;;;;;;;;;;;;;;;
(defun my:git-gutter-refresh ()
(interactive)
(git-gutter+-refresh))
(defun my:minibuffer-quit ()
"Abort recursive edit. In Delete Selection mode, if the mark is
active, just deactivate it; then it takes a second \\[keyboard-quit]
@ -15,62 +11,32 @@ to abort the minibuffer."
(when (get-buffer "*Completions*") (delete-windows-on "*Completions*"))
(abort-recursive-edit)))
;; File navigation defuns
(defun my:goto-symbol (&optional symbol-list)
"Refresh imenu and jump to a place in the buffer using Ido."
(interactive)
(unless (featurep 'imenu)
(require 'imenu nil t))
(cond
((not symbol-list)
(let ((ido-mode ido-mode)
(ido-enable-flex-matching
(if (boundp 'ido-enable-flex-matching)
ido-enable-flex-matching t))
name-and-pos symbol-names position)
(unless ido-mode
(ido-mode 1)
(setq ido-enable-flex-matching t))
(while (progn
(imenu--cleanup)
(setq imenu--index-alist nil)
(my:goto-symbol (imenu--make-index-alist))
(setq selected-symbol
(ido-completing-read "Symbol? " symbol-names))
(string= (car imenu--rescan-item) selected-symbol)))
(unless (and (boundp 'mark-active) mark-active)
(push-mark nil t nil))
(setq position (cdr (assoc selected-symbol name-and-pos)))
(cond
((overlayp position)
(goto-char (overlay-start position)))
(t
(goto-char position)))))
((listp symbol-list)
(dolist (symbol symbol-list)
(let (name position)
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(my:goto-symbol symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position
(get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name)
(string= (car imenu--rescan-item) name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
;; Buffer defuns
(defun my:kill-dired-buffers ()
(interactive)
(mapc (lambda (buffer)
(when (eq 'dired-mode (buffer-local-value 'major-mode buffer))
(kill-buffer buffer)))
(buffer-list)))
(defun my:kill-other-buffers ()
"Kill left-over temporary, dired or buried special buffers"
(interactive)
(mapc (lambda (buffer)
(let ((buffer-name (buffer-name buffer)))
(when (and (not (s-matches? buffer-name "\\*\\(scratch\\|Messages\\)\\*"))
(or (eq 'dired-mode (buffer-local-value 'major-mode buffer))
(s-matches? "^ ?\\*.+\\*$" buffer-name))
(not (get-buffer-window buffer)))
(kill-buffer buffer))))
(buffer-list)))
;; Inspired by http://demonastery.org/2013/04/emacs-evil-narrow-region/
(defun my:narrow-to-region-indirect (start end)
"Restrict editing in this buffer to the current region, indirectly."
(interactive "r")
(deactivate-mark)
(let ((buf (clone-indirect-buffer nil nil)))
(with-current-buffer buf
(narrow-to-region start end))
(switch-to-buffer buf)))
(defun my:kill-persp ()
(interactive)
(persp-kill (persp-name persp-curr)))
;;;; Tmux defuns ;;;;;;;;;;;;;;;;;
(defun my:tmux-send (command)
@ -86,7 +52,7 @@ to abort the minibuffer."
(stringp appName)
(not (string= "" appName)))
(setq appName (concat "-a " appName ".app")))
(shell-command (concat "open " appName " " (shell-quote-argument path))))
(async-shell-command (concat "open " appName " " (shell-quote-argument path))))
(defun my:open-with (appName)
(interactive "sApp name: ")
@ -102,58 +68,44 @@ to abort the minibuffer."
;; Ex-commands ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(evil-define-command my:ex:msg-buffer () :repeat nil
(interactive)
(view-echo-area-messages)
(switch-to-buffer-other-window "*Messages*")
;; Force text-mode for unfettered evil-mode & my keymappings
(text-mode))
(evil-define-command my:ex:kill-buffers (&optional bang) :repeat nil
(evil-define-command ex:kill-buffers (&optional bang) :repeat nil
(interactive "<!>")
(let ((buffers (if bang
(cdr (buffer-list (current-buffer)))
(buffer-list))))
(delete-other-windows)
(tags-reset-tags-tables)
(mapc 'kill-buffer buffers)))
(if (and bang (projectile-project-p))
(projectile-kill-buffers)
(mapc 'kill-buffer (buffer-list)))
(delete-other-windows))
(evil-define-command my:ex:init-files (&optional bang) :repeat nil
(evil-define-command ex:init-files (&optional bang) :repeat nil
(interactive "<!>")
(if bang
(ido-find-file-in-dir *init-dir)
(ido-find-file-in-dir *dir)))
(evil-define-command my:ex:notes () :repeat nil
(evil-define-command ex:notes () :repeat nil
(interactive)
(ido-find-file-in-dir org-directory))
(evil-define-command my:ex:snippets (&optional bang) :repeat nil
(interactive "<!>")
(if bang
(yas-new-snippet)
(yas-visit-snippet-file)))
;; Projects
(evil-define-command my:ex:ag-search (search &optional bang) :repeat nil
(evil-define-command ex:ag-search (search &optional bang regex) :repeat nil
(interactive "<a><!>")
(let ((root (my/project-root bang)))
(ag search root)))
(if search
(if regex (ag-regexp search root) (ag search root))
(helm-do-ag root))))
(evil-define-command my:ex:ag-regex-search (search &optional bang) :repeat nil
(evil-define-command ex:ag-regex-search (search &optional bang) :repeat nil
(interactive "<a><!>")
(let ((root (my/project-root bang)))
(ag-regexp search root)))
(ex:ag-search search bang t))
;; Run a command. If <bang>, then only type command into tmux
(evil-define-command my:ex:tmux-send (command &optional bang) :repeat nil
(evil-define-command ex:tmux-send (command &optional bang) :repeat nil
(interactive "<sh><!>")
(let ((cmd-format (if bang "%s" "C-u %s Enter")))
(my:tmux-send (format cmd-format (shell-quote-argument command)))
(when (evil-ex-p)
(message "[Tmux] %s" command))))
(evil-define-command my:ex:tmux-chdir (&optional path bang) :repeat nil
(evil-define-command ex:tmux-chdir (&optional path bang) :repeat nil
(interactive "<f><!>")
(let ((dir (shell-quote-argument
(if (and path
@ -161,36 +113,65 @@ to abort the minibuffer."
(file-directory-p path))
(file-truename path)
(my/project-root bang)))))
(my:ex:tmux-run (format "cd %s" dir))
(ex:tmux-send (format "cd %s" dir))
(when (evil-ex-p)
(message "[Tmux] cd %s" dir))))
(evil-define-command my:ex:byte-compile-all (&optional bang) :repeat nil
(evil-define-command ex:byte-compile-all (&optional bang) :repeat nil
(interactive "<!>")
(byte-recompile-file (expand-file-name "init.el" *dir) bang 0)
(byte-recompile-directory *init-dir 0 bang)
(byte-recompile-directory *elisp-dir 0 bang))
(require 'async-bytecomp)
;; (async-byte-recompile-directory *dir 0 bang)
(byte-recompile-directory *dir 0 bang))
(evil-define-command my:ex:mru () :repeat nil
"Find a recent file using ido."
(interactive)
(let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file (find-file file))))
(evil-define-command my:ex:build (arguments &optional bang) :repeat nil
(evil-define-command ex:build (arguments &optional bang) :repeat nil
(interactive "<a><!>")
(my:build arguments))
(evil-define-command my:ex:cd (dir) :repeat nil
(evil-define-command ex:cd (dir) :repeat nil
(interactive "<f>")
(cd (if (zerop (length dir)) "~" dir)))
(defun --save-exit() (save-buffer) (kill-buffer) (remove-hook 'yas-after-exit-snippet-hook '--save-exit))
(evil-define-command ex:create-file (path &optional bang) :repeat nil
"Deploy files (and their associated templates) quickly. Will prompt
you to fill in each snippet field before buffer closes unless BANG is
provided."
(interactive "<f><!>")
(let ((dir (f-dirname path))
(fullpath (f-full path))
(is-auto t))
(when (and bang (not (f-exists? dir))) (f-mkdir dir))
(if (f-exists? dir)
(if (f-exists? fullpath)
(error "File already exists: %s" path)
(find-file fullpath)
(add-hook 'yas-after-exit-snippet-hook '--save-exit)
(if bang (--save-exit)))
(error "Directory doesn't exist: %s" dir))))
(evil-define-command ex:rename-this-file (new-name &optional bang) :repeat nil
"Renames current buffer and file it is visiting."
(interactive "<f><!>")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(error "Buffer '%s' is not visiting a file!" name)
(let ((new-name (if new-name new-name (read-file-name "New name: " filename))))
(if (get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)
(when bang
(delete-file filename))
(message "File '%s' successfully renamed to '%s'"
name (file-name-nondirectory new-name)))))))
;;;
(evil-define-operator my:ex:scratch-buffer (beg end)
:motion nil
(evil-define-operator ex:scratch-buffer (beg end)
:move-point nil
:type inclusive
:repeat nil
(interactive "<r>")
(let ((mode major-mode)
(text (when (and beg end) (buffer-substring beg end))))
@ -198,11 +179,9 @@ to abort the minibuffer."
(if text (insert text))
(funcall mode)))
(evil-define-operator my:ex:org-capture (beg end)
:motion nil
(evil-define-operator ex:org-capture (beg end)
:move-point nil
:type inclusive
:repeat nil
(interactive "<r>")
(let ((mode major-mode)
(text (when (and beg end) (buffer-substring beg end))))
@ -210,7 +189,7 @@ to abort the minibuffer."
(org-capture-string text)
(org-capture))))
(evil-define-operator my:ex:retab (beg end)
(evil-define-operator ex:retab (beg end)
:motion nil
:move-point nil
:type line
@ -224,18 +203,15 @@ spaces-to-tabs, depending on `indent-tab-mode'. Untested."
(tabify beg end)
(untabify beg end)))
(evil-define-operator my:ex:run-code (beg end) :repeat nil
:motion nil
(evil-define-operator ex:run-code (beg end)
:move-point nil
:type exclusive
:repeat nil
(interactive "<r>")
(cond ((and beg end)
(my:run-code-region beg end))
(t
(my:run-code-buffer))))
(evil-define-operator my:ex:send-region-to-repl (beg end &optional bang) :repeat nil
(evil-define-operator ex:send-region-to-repl (beg end &optional bang)
:motion nil
:move-point nil
:type exclusive
@ -245,3 +221,28 @@ spaces-to-tabs, depending on `indent-tab-mode'. Untested."
(my:send-region-to-repl beg end))
(t
(my:switch-to-repl))))
(evil-define-operator ex:snippets (beg end &optional name)
:motion nil
:move-point nil
:type exclusive
:repeat nil
"Create a new snippet (called `name'), or select from all the
current mode's snippets to edit."
(interactive "<r><a>")
(cond ((and beg end)
(yas-insert-snippet))
(t
(if name
(find-file (concat *snippets-dir (symbol-name major-mode) "/" name))
(yas-visit-snippet-file)))))
(evil-define-operator ex:narrow-indirect (beg end)
:motion nil
:move-point nil
:type exclusive
:repeat nil
"Indirectly narrow the region from BEG to END."
(interactive "<r>")
(evil-normal-state)
(narrow-to-region-indirect beg end))