dotfiles/.emacs.d/conf.d/001-utilities.el

181 lines
6.0 KiB
EmacsLisp

(require 'grep)
(defvar jwall:hg-grep-search-history nil "History list for hg grep search.")
(defvar jwall:hg-grep-location-history nil "History list for hg grep search location.")
(defun jwall:set-window-width (&optional columns)
"Make selected window COLUMNS wide.
Interactively, if no argument is given, make selected window 80
columns wide.
"
(interactive "p")
(let* ((cols (if (or (not columns) (> 0 columns)) 80 columns)))
(shrink-window-horizontally (- (window-width) cols))))
(defun jwall:revert-all-buffers ()
"Revert all buffers with edits"
(interactive)
(let* ((bs (buffer-list)))
(dolist (b bs)
(save-window-excursion
(switch-to-buffer b)
; if the buffer is visiting a real file and modified
(if (and (buffer-file-name)
(buffer-modified-p b))
; revert the buffer
(revert-buffer b))))))
;; Toggle window dedication
;; props to Lucas Bergman and stackoverflow user Frank Klotz
(defun jwall:toggle-window-dedicated (buffer-name)
"Toggle whether the current active window is dedicated or not
Specifically, I use this on my grep or project viewing buffer to
keep Emacs from clobbering it with random file buffers created by
emacsclient or help invocations."
(interactive "b")
(message
(if (let (window (get-buffer-window (get-buffer buffer-name)))
(set-window-dedicated-p window
(not (window-dedicated-p window))))
"Window '%s' is dedicated"
"Window '%s' is normal")
(current-buffer)))
(global-set-key [pause] 'jwall:toggle-window-dedicated)
;(require 'auto-complete-config)
;(add-to-list 'ac-dictionary-directories "~/.emacs.d/site-lisp/ac-dict")
;(ac-config-default)
(defun jwall:unwrap-paragraph ()
"Takes a multi-line paragraph and makes it into a single line of text."
(interactive)
(let ((fill-column (point-max)))
(fill-paragraph nil)))
(defun jwall:wrap-paragraph (&optional columns)
"Takes a single line paragraph and makes it into multiple lines of text.
Passing 0 or nil as the columns means we use the current window width."
(interactive "ncolumns:")
(let* ((fill-column (if
(or (not columns)
(= 0 columns))
(window-width)
columns)))
(fill-paragraph nil)))
(defvar jwall:hg-command (executable-find "hg"))
(defvar jwall:git-command (executable-find "git"))
(defun jwall:git-root ()
"Get the root directory of the git repo."
(interactive)
(shell-command-to-string (jwall:git-command " rev-parse --show-toplevel")))
(defun jwall:hg-root ()
"Get the root directory of the hg repo."
(interactive)
(let ((result (shell-command-to-string (concat jwall:hg-command " root"))))
(substring result 0 (- (length result) 1))))
;; git grep
(defun jwall:git-grep (search)
"Uses git to preform a grep."
(interactive "MSearch String: ")
(let* ((cmd (concat jwall:git-command " grep -n --break " search)))
(grep cmd)))
(defun jwall:git-grep-all (search)
"Uses git to preform a grep."
(interactive "MSearch String: ")
(let* ((root (jwall:git-root))
(args (concat search " " root)))
(jwall:git-grep args)))
;; hg grep
(defun jwall:hg-grep (search location)
"Uses hg to perform a grep."
(interactive
(progn
(list
(read-shell-command "Search String: "
(thing-at-point 'word)
'jwall:hg-grep-search-history)
(read-shell-command "Location: "
(jwall:hg-root)
'jwall:hg-grep-location-history))))
(with-temp-buffer
(cd location)
(let* ((cmd (concat jwall:hg-command " grep -n -r tip " search " | sed 's/:[^:]*:/:/'")))
(grep cmd))))
(defun jwall:hg-grep-all (search)
"Uses hg to preform a grep."
(interactive "MSearch String: ")
(let* ((root (jwall:hg-root))
(args (concat search " " root)))
(jwall:hg-grep args)))
(defun jwall:word-at-point ()
"Return the word at this point"
(thing-at-point 'word))
;; File/Dir utility functions.
(defun folder-dirs (dir)
(delete-if-not
(lambda (f)
(and (file-directory-p (concat dir "/" f))
(not (or (string= f "..")
(string= f ".")))))
(directory-files dir)))
(defun files-in-dir (dir)
(delete-if (lambda (f) (file-directory-p f))
(directory-files dir)))
(defun walk-up-dirs (dir func)
"Walk up directories until func returns 't.
Applies func to each file in the directory."
(let* ((files (files-in-dir dir))
(cand (reduce (lambda (acc file) (if (funcall func file)
(progn
(cons file acc))
acc))
files :initial-value '())))
(if cand
(progn
dir)
(let* ((newdir (expand-file-name (concat dir "/.."))))
(if (not (string= newdir "/"))
(walk-up-dirs newdir func)
nil)))))
(defun walk-down-dirs (dir func)
"Walk all sub-directories until func returns 't.
Applies func to each file in the directory."
(let* ((files (files-in-dir dir))
(cand (reduce (lambda (acc file) (if (funcall func (concat dir "/" file))
(progn
(cons file acc))
acc))
files :initial-value '()))
(dirs (folder-dirs dir)))
(mapc (lambda (d) (walk-down-dirs (concat dir "/" d) func)) dirs)))
(defun jwall:cleanup-dos-endings (buffer)
"Automate M-% C-q C-m RET C-q C-j RET"
(interactive "*b")
(save-excursion
(goto-char (point-min))
(while (search-forward (string ?\C-m) nil t)
(replace-match "" nil t))))
(require 'string-inflection)
(global-set-key (kbd "C-c i") 'string-inflection-cycle)
(global-set-key (kbd "C-c p") 'string-inflection-camelcase)
(global-set-key (kbd "C-c c") 'string-inflection-lower-camelcase)
(global-set-key (kbd "C-c j") 'string-inflection-java-style-cycle)