Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

271 lines (246 sloc) 10.307 kB
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Initialization file for function definitions
; Author: Matthew Darling
; First started: June 2nd, 2013
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Close windows holding killed buffers
; Taken from: http://emacswiki.org/emacs/misc-cmds.el
; The main file isn't available in the package manager,
; and provides a ton of extra stuff I don't need.
;;;###autoload
(defun kill-buffer-and-its-windows (buffer)
"Kill BUFFER and delete its windows. Default is `current-buffer'.
BUFFER may be either a buffer or its name (a string)."
(interactive (list (read-buffer "Kill buffer: " (current-buffer) 'existing)))
(setq buffer (get-buffer buffer))
(if (buffer-live-p buffer) ; Kill live buffer only.
(let ((wins (get-buffer-window-list buffer nil t))) ; On all frames.
(when (and (buffer-modified-p buffer)
(fboundp '1on1-flash-ding-minibuffer-frame))
(1on1-flash-ding-minibuffer-frame t)) ; Defined in `oneonone.el'.
(when (kill-buffer buffer) ; Only delete windows if buffer killed.
(dolist (win wins) ; (User might keep buffer if modified.)
(when (window-live-p win) (delete-window win)))))
(when (called-interactively-p "any")
(error "Cannot kill buffer. Not a live buffer: `%s'" buffer))))
;;; Kill star buffers when closing them
; http://superuser.com/a/466109
(defadvice quit-window (before quit-window-always-kill)
"When running `quit-window', always kill the buffer."
(ad-set-arg 0 t))
(ad-activate 'quit-window)
;;; Send the cursor to the start of actual text on the line, or the real start
;;;###autoload
(defun indentation-or-beginning ()
(interactive)
(if this-command-keys-shift-translated
(unless mark-active (push-mark nil t t))
(when (and mark-active cua--last-region-shifted)
(deactivate-mark)))
(if (= (point) (progn (back-to-indentation) (point)))
(beginning-of-visual-line)))
;; If Emacs crashes, you get bugged about the old process - that's dumb
; Taken from: http://www.emacswiki.org/emacs/DeskTop
;;;###autoload
(defun emacs-process-p (pid)
"If pid is the process ID of an emacs process, return t, else nil.
Also returns nil if pid is nil."
(when pid
(let ((attributes (process-attributes pid)) (cmd))
(dolist (attr attributes)
(if (string= "comm" (car attr))
(setq cmd (cdr attr))))
(if (and cmd (or (string= "emacs" cmd) (string= "emacs.exe" cmd))) t))))
(defadvice desktop-owner (after pry-from-cold-dead-hands activate)
"Don't allow dead emacsen to own the desktop file."
(when (not (emacs-process-p ad-return-value))
(setq ad-return-value nil)))
;;; Have a key to copy rather than cut
;;;###autoload
(defun copy-line (&optional arg)
"Do a kill-line but copy rather than kill. This function directly calls
kill-line, so see documentation of kill-line for how to use it including prefix
argument and relevant variables. This function works by temporarily making the
buffer read-only, so I suggest setting kill-read-only-ok to t."
(interactive "P")
(read-only-mode 1)
(kill-line arg)
(read-only-mode 0))
;;;A version of fill-paragraph that also can unfill
;taken from http://ergoemacs.org/emacs/modernization_fill-paragraph.html
;;;###autoload
(defun compact-uncompact-block ()
"Remove or add line ending chars on current paragraph.
This command is similar to a toggle of `fill-paragraph'.
When there is a text selection, act on the region."
(interactive)
;; This command symbol has a property ?'stateIsCompact-p?.
(let (currentStateIsCompact (bigFillColumnVal 4333999) (deactivate-mark nil))
(save-excursion
;; Determine whether the text is currently compact.
(setq currentStateIsCompact
(if (eq last-command this-command)
(get this-command 'stateIsCompact-p)
(if (> (- (line-end-position) (line-beginning-position))
fill-column) t nil)))
(if (region-active-p)
(if currentStateIsCompact
(fill-region (region-beginning) (region-end))
(let ((fill-column bigFillColumnVal))
(fill-region (region-beginning) (region-end))) )
(if currentStateIsCompact
(fill-paragraph nil)
(let ((fill-column bigFillColumnVal))
(fill-paragraph nil)) ) )
(put this-command 'stateIsCompact-p (if currentStateIsCompact nil t)))))
;;; So these functions will act like kill-word, except spaces and special
; characters don't count - thus "something <something" would require four calls
; TODO: Modify subword-forward-regexp and subword-backward-regexp instead
; See: http://stackoverflow.com/a/10449642
;; (defun kill-syntax (&optional arg)
;; "Kill ARG sets of syntax characters after point."
;; (interactive "p")
;; (let ((arg (or arg 1))
;; (inc (if (and arg (< arg 0)) 1 -1))
;; (opoint (point)))
;; (while (not (= arg 0))
;; (if (> arg 0)
;; (skip-syntax-forward (string (char-syntax (char-after))))
;; (skip-syntax-backward (string (char-syntax (char-before)))))
;; (setq arg (+ arg inc)))
;; (kill-region opoint (point))))
;; (defun kill-syntax-backward (&optional arg)
;; "Kill ARG sets of syntax characters preceding point."
;; (interactive "p")
;; (kill-syntax (- 0 (or arg 1))))
;;; Filter the recent files list with ido, except this one works
; From Damien Cassou http://www.masteringemacs.org/articles/2011/01/27/find-files-faster-recent-files-package/#comment-397
; Improved by Steve Purcell http://www.reddit.com/r/emacs/comments/21a4p9/use_recentf_and_ido_together/cgc8f6b
;;;###autoload
(defun recentf-ido-find-file ()
"Use ido to select a recently opened file from the `recentf-list'"
(interactive)
(find-file
(ido-completing-read "Recentf open: "
(mapcar 'abbreviate-file-name recentf-list)
nil t)))
;;;###autoload
(defun imenu-elisp-sections ()
(setq imenu-prev-index-position-function nil)
(add-to-list 'imenu-generic-expression '("Sections" "^;;; \\(.+\\)$" 1) t))
;;;###autoload
(defun init-imenu (p)
(interactive "P")
(find-file-existing "~/.emacs.d/init.el")
(widen)
(helm-imenu)
(if p (init-narrow-to-section)))
;;;###autoload
(defun init-narrow-to-section ()
(interactive)
(save-excursion
(beginning-of-line)
(unless (looking-at "^;;;;")
(re-search-backward "^;;;;" nil t))
(push-mark)
(next-line)
(re-search-forward "^;;;;" nil t)
(previous-line)
(narrow-to-region (region-beginning) (region-end))))
;;;###autoload
(defun resume-try()
"Tries to resume a buffer if the file exists and
adds `save-current-configuration' if it is loaded"
(if (file-exists-p revive:configuration-file)
(let ()
(resume)
(add-hook 'kill-emacs-hook 'save-current-configuration))))
;;; Make sure revive doesn't save it's configuration on exit when wiping
;;;###autoload
(defun wipe-try()
"Wipes the revive configuration and removes
the `save-current-configuration' from the exit hook"
(wipe)
(if (file-exists-p revive:configuration-file)
(delete-file revive:configuration-file))
(remove-hook 'kill-emacs-hook 'save-current-configuration))
;;; Folds lines with a greater indentation than the current linw
;http://stackoverflow.com/a/4459159
;;;###autoload
(defun aj-toggle-fold ()
"Toggle fold all lines larger than indentation on current line"
(interactive)
(let ((col 1))
(save-excursion
(back-to-indentation)
(setq col (+ 1 (current-column)))
(set-selective-display
(if selective-display nil (or col 1))))))
;;; A bunch of functions for dealing with the files behind buffers
; Added after reading http://rawsyntax.com/blog/learn-emacs-delete-file-and-buffer/
; Though I prefer Magnar's delete function: http://whattheemacsd.com//file-defuns.el-02.html
;;;###autoload
(defun delete-file-and-buffer ()
"Removes file connected to current buffer and kills buffer."
(interactive)
(let ((filename (buffer-file-name))
(buffer (current-buffer))
(name (buffer-name)))
(if (not (and filename (file-exists-p filename)))
(ido-kill-buffer)
(when (yes-or-no-p "Are you sure you want to remove this file? ")
(delete-file filename)
(kill-buffer buffer)
(message "File '%s' successfully removed" filename)))))
;;;###autoload
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to new-name."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
;;;###autoload
(defun move-buffer-file (dir)
"Moves both current buffer and file it's visiting to dir."
(interactive "DNew directory: ")
(let* ((name (buffer-name))
(filename (buffer-file-name))
(dir
(if (string-match dir "\\(?:/\\|\\\\)$")
(substring dir 0 -1) dir))
(newname (concat dir "/" name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(progn
(copy-file filename newname 1)
(delete-file filename)
(set-visited-file-name newname)
(set-buffer-modified-p nil) t))))
;;; Some functions to improve Magit
; From Magnar Sveen http://whattheemacsd.com/setup-magit.el-02.html
;;;###autoload
(defun magit-toggle-whitespace ()
(interactive)
(if (member "-w" magit-diff-options)
(magit-dont-ignore-whitespace)
(magit-ignore-whitespace)))
;;;###autoload
(defun magit-ignore-whitespace ()
(interactive)
(add-to-list 'magit-diff-options "-w")
(magit-refresh))
;;;###autoload
(defun magit-dont-ignore-whitespace ()
(interactive)
(setq magit-diff-options (remove "-w" magit-diff-options))
(magit-refresh))
(provide 'mattd-defuns)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Jump to Line
Something went wrong with that request. Please try again.