Find file
Fetching contributors…
Cannot retrieve contributors at this time
271 lines (246 sloc) 10.1 KB
;;; Initialization file for function definitions
; Author: Matthew Darling
; First started: June 2nd, 2013
;;; Close windows holding killed buffers
; Taken from:
; The main file isn't available in the package manager,
; and provides a ton of extra stuff I don't need.
(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
(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
(defun indentation-or-beginning ()
(if this-command-keys-shift-translated
(unless mark-active (push-mark nil t t))
(when (and mark-active cua--last-region-shifted)
(if (= (point) (progn (back-to-indentation) (point)))
;; If Emacs crashes, you get bugged about the old process - that's dumb
; Taken from:
(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
(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
(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."
;; This command symbol has a property ?'stateIsCompact-p?.
(let (currentStateIsCompact (bigFillColumnVal 4333999) (deactivate-mark nil))
;; 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:
;; (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
; Improved by Steve Purcell
(defun recentf-ido-find-file ()
"Use ido to select a recently opened file from the `recentf-list'"
(ido-completing-read "Recentf open: "
(mapcar 'abbreviate-file-name recentf-list)
nil t)))
(defun imenu-elisp-sections ()
(setq imenu-prev-index-position-function nil)
(add-to-list 'imenu-generic-expression '("Sections" "^;;; \\(.+\\)$" 1) t))
(defun init-imenu (p)
(interactive "P")
(find-file-existing "~/.emacs.d/init.el")
(if p (init-narrow-to-section)))
(defun init-narrow-to-section ()
(unless (looking-at "^;;;;")
(re-search-backward "^;;;;" nil t))
(re-search-forward "^;;;;" nil t)
(narrow-to-region (region-beginning) (region-end))))
(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 ()
(add-hook 'kill-emacs-hook 'save-current-configuration))))
;;; Make sure revive doesn't save it's configuration on exit when wiping
(defun wipe-try()
"Wipes the revive configuration and removes
the `save-current-configuration' from the exit hook"
(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
(defun aj-toggle-fold ()
"Toggle fold all lines larger than indentation on current line"
(let ((col 1))
(setq col (+ 1 (current-column)))
(if selective-display nil (or col 1))))))
;;; A bunch of functions for dealing with the files behind buffers
; Added after reading
; Though I prefer Magnar's delete function:
(defun delete-file-and-buffer ()
"Removes file connected to current buffer and kills buffer."
(let ((filename (buffer-file-name))
(buffer (current-buffer))
(name (buffer-name)))
(if (not (and filename (file-exists-p filename)))
(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)))))
(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)
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
(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))
(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)
(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
(defun magit-toggle-whitespace ()
(if (member "-w" magit-diff-options)
(defun magit-ignore-whitespace ()
(add-to-list 'magit-diff-options "-w")
(defun magit-dont-ignore-whitespace ()
(setq magit-diff-options (remove "-w" magit-diff-options))
(provide 'mattd-defuns)