2012-12-30 17:18:08 +00:00
|
|
|
;; 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)))))
|
|
|
|
|
2012-12-20 21:51:11 +00:00
|
|
|
;; 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))))
|
|
|
|
|
2013-02-03 20:58:25 +00:00
|
|
|
;; from magnars modified bt ffevotte for dedicated windows support
|
|
|
|
(defun rotate-windows (count)
|
|
|
|
"Rotate your windows.
|
|
|
|
Dedicated windows are left untouched. Giving a negative prefix
|
2013-02-20 19:06:31 +00:00
|
|
|
argument takes the kindows rotate backwards."
|
2013-02-03 20:58:25 +00:00
|
|
|
(interactive "p")
|
|
|
|
(let* ((non-dedicated-windows (remove-if 'window-dedicated-p (window-list)))
|
|
|
|
(num-windows (length non-dedicated-windows))
|
|
|
|
(i 0)
|
|
|
|
(step (+ num-windows count)))
|
|
|
|
(cond ((not (> num-windows 1))
|
|
|
|
(message "You can't rotate a single window!"))
|
|
|
|
(t
|
|
|
|
(dotimes (counter (- num-windows 1))
|
|
|
|
(let* ((next-i (% (+ step i) num-windows))
|
|
|
|
|
|
|
|
(w1 (elt non-dedicated-windows i))
|
|
|
|
(w2 (elt non-dedicated-windows next-i))
|
|
|
|
|
|
|
|
(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 next-i)))))))
|
2012-12-18 05:48:12 +00:00
|
|
|
|
2013-02-20 19:06:31 +00:00
|
|
|
(defun rotate-windows-backward (count)
|
2013-02-19 23:12:10 +00:00
|
|
|
"Rotate your windows backward."
|
2013-02-20 19:06:31 +00:00
|
|
|
(interactive "p")
|
|
|
|
(rotate-windows (* -1 count)))
|
2013-02-19 23:12:10 +00:00
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(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))))))))
|
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(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)))))
|
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(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)))
|
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(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)))
|
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(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))))
|
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(defun touch-buffer-file ()
|
|
|
|
(interactive)
|
|
|
|
(insert " ")
|
|
|
|
(backward-delete-char 1)
|
|
|
|
(save-buffer))
|
|
|
|
|
2012-12-30 17:18:08 +00:00
|
|
|
;; from magnars
|
2012-12-29 20:39:39 +00:00
|
|
|
(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))))
|
|
|
|
|
2013-01-02 22:08:45 +00:00
|
|
|
;; found at http://emacswiki.org/emacs/KillingBuffers
|
|
|
|
(defun kill-other-buffers ()
|
|
|
|
"Kill all other buffers."
|
|
|
|
(interactive)
|
2013-01-03 20:34:42 +00:00
|
|
|
(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!"))))
|
2013-01-02 22:08:45 +00:00
|
|
|
|
2013-01-07 21:59:36 +00:00
|
|
|
;; evenly split windows horizontally
|
|
|
|
(defun evenly-split-window-right ()
|
2013-01-08 16:09:21 +00:00
|
|
|
"Evenly split frame horizontally."
|
2013-01-07 21:59:36 +00:00
|
|
|
(interactive)
|
|
|
|
(split-window-right)
|
|
|
|
(balance-windows))
|
|
|
|
;; evenly split windows vertically
|
|
|
|
(defun evenly-split-window-below ()
|
2013-01-08 16:09:21 +00:00
|
|
|
"Evenly split frame vertically."
|
2013-01-07 21:59:36 +00:00
|
|
|
(interactive)
|
|
|
|
(split-window-below)
|
|
|
|
(balance-windows))
|
|
|
|
|
2013-01-08 16:09:21 +00:00
|
|
|
;; 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))))
|
|
|
|
|
2013-02-20 04:13:55 +00:00
|
|
|
;; from http://blog.everythingtastesbetterwithchilli.com/2013/02/18/electric-punctuation-in-emacs/
|
|
|
|
(defun electric-punctuation ()
|
|
|
|
"Tidy up whitespace around punctuation: delete any preceding
|
|
|
|
whitespace and insert one space afterwards. Idea stolen from
|
|
|
|
the SwiftKey android keyboard."
|
|
|
|
(interactive)
|
|
|
|
(when (looking-back "\s+" nil t)
|
|
|
|
(delete-region (match-beginning 0) (match-end 0)))
|
|
|
|
(call-interactively 'self-insert-command)
|
|
|
|
(just-one-space))
|
|
|
|
|
2013-01-04 06:40:59 +00:00
|
|
|
(provide 'my-funcs)
|