This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/my-funcs.el
2013-01-12 11:58:50 -05:00

149 lines
4.8 KiB
EmacsLisp

;; from magnars
(defun eval-and-replace ()
"Replace the preceding sexp with its value."
(interactive)
(backward-kill-sexp)
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(current-buffer))
(error (message "Invalid expression")
(insert (current-kill 0)))))
;; from https://gist.github.com/3402786
(defun toggle-maximize-buffer () "Maximize buffer"
(interactive)
(if (= 1 (length (window-list)))
(jump-to-register '_)
(progn
(set-register '_ (list (current-window-configuration)))
(delete-other-windows))))
;:http://emacswiki.org/emacs/TransposeWindows
(defun rotate-windows ()
"Rotate your windows"
(interactive)
(cond
((not (> (count-windows) 1))
(message "You can't rotate a single window!"))
(t
(let ((i 1)
(num-windows (count-windows)))
(while (< i num-windows)
(let* ((w1 (elt (window-list) i))
(w2 (elt (window-list) (+ (% i num-windows) 1)))
(b1 (window-buffer w1))
(b2 (window-buffer w2))
(s1 (window-start w1))
(s2 (window-start w2)))
(set-window-buffer w1 b2)
(set-window-buffer w2 b1)
(set-window-start w1 s2)
(set-window-start w2 s1)
(setq i (1+ i))))))))
;; from magnars
(defun rename-current-buffer-file ()
"Renames current buffer and file it is visiting."
(interactive)
(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 (read-file-name "New name: " filename)))
(cond ((get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name))
(t
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))))
;; from magnars
(defun delete-current-buffer-file ()
"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)))))
;; from magnars
(defun find-or-create-file-at-point ()
"Guesses what parts of the buffer under point is a file name and opens it."
(interactive)
(find-file (file-name-at-point)))
;; from magnars
(defun find-or-create-file-at-point-other-window ()
"Guesses what parts of the buffer under point is a file name and opens it."
(interactive)
(find-file-other-window (file-name-at-point)))
;; from magnars
(defun file-name-at-point ()
(save-excursion
(let* ((file-name-regexp "[./a-zA-Z0-9\-_~]")
(start (progn
(while (looking-back file-name-regexp)
(forward-char -1))
(point)))
(end (progn
(while (looking-at file-name-regexp)
(forward-char 1))
(point))))
(buffer-substring start end))))
;; from magnars
(defun touch-buffer-file ()
(interactive)
(insert " ")
(backward-delete-char 1)
(save-buffer))
;; from magnars
(defun sudo-edit (&optional arg)
(interactive "p")
(if (or arg (not buffer-file-name))
(find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
;; found at http://emacswiki.org/emacs/KillingBuffers
(defun kill-other-buffers ()
"Kill all other buffers."
(interactive)
(let (name (buffer-name))
(when (yes-or-no-p (format "Killing all buffers except \"%s\" ? " buffer-file-name))
(mapc 'kill-buffer (delq (current-buffer) (buffer-list)))
(message "Buffers deleted!"))))
;; evenly split windows horizontally
(defun evenly-split-window-right ()
"Evenly split frame horizontally."
(interactive)
(split-window-right)
(balance-windows))
;; evenly split windows vertically
(defun evenly-split-window-below ()
"Evenly split frame vertically."
(interactive)
(split-window-below)
(balance-windows))
;; from http://dfan.org/blog/2009/02/19/emacs-dedicated-windows/
(defun toggle-current-window-dedication ()
"Toggle dedication state of a window."
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(provide 'my-funcs)