;; 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 copy-current-file-path () "Add current file path to kill ring. Limits the filename to project root if possible." (interactive) (let ((filename (buffer-file-name))) (kill-new (if eproject-mode (s-chop-prefix (eproject-root) filename) 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)))) (provide 'funcs)