buffer-defuns.el 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ;;; Code:
  2. ;; Buffer-related defuns
  3. (require 's)
  4. ;;;###autoload
  5. (defun distopico:enable--recreate-scratch-buffer ()
  6. "Make sure scratch has the correct `initial-major-mode' and custom actions."
  7. (with-current-buffer "*scratch*"
  8. (funcall initial-major-mode)
  9. (add-hook 'kill-buffer-query-functions 'distopico:recreate--scratch-buffer nil t)))
  10. ;;;###autoload
  11. (defun distopico:recreate--scratch-buffer ()
  12. "If the *scratch* buffer is killed, recreate it automatically.
  13. FROM: Morten Welind: http://www.geocrawler.com/archives/3/338/1994/6/0/1877802/"
  14. ;; The next line is just in case someone calls this manually
  15. (set-buffer (get-buffer-create "*scratch*"))
  16. ;; Kill the current (*scratch*) buffer
  17. (remove-hook 'kill-buffer-query-functions 'distopico:recreate--scratch-buffer t)
  18. (kill-buffer (current-buffer))
  19. ;; Make a brand new *scratch* buffer
  20. (distopico:create-scratch-buffer)
  21. ;; Should return nil to avoid loop
  22. nil)
  23. ;;;###autoload
  24. (defun distopico:create-scratch-buffer nil
  25. "Create a new scratch buffer to work in.
  26. Could be *scratch* - *scratchX*."
  27. (interactive)
  28. (let ((n 0)
  29. bufname)
  30. (while (progn
  31. (setq bufname (concat "*scratch"
  32. (if (= n 0) "" (int-to-string n))
  33. "*"))
  34. (setq n (1+ n))
  35. (get-buffer bufname)))
  36. (switch-to-buffer (get-buffer-create bufname))
  37. (message (format "Created %s" bufname))
  38. (distopico:enable--recreate-scratch-buffer)
  39. (insert initial-scratch-message)))
  40. (defun split-window-right-and-move-there-dammit ()
  41. (interactive)
  42. (split-window-right)
  43. (windmove-right))
  44. (defun kill-this-buffer-and-pane ()
  45. "If there are multiple windows, then close this pane and kill the buffer in it also.
  46. from: http://www.emacswiki.org/emacs/KillingBuffers"
  47. (interactive)
  48. (kill-this-buffer)
  49. (if (not (one-window-p))
  50. (delete-window)))
  51. (defun kill-next-buffer-and-pane ()
  52. "If there are multiple windows, then close the other pane and kill the buffer in it also.
  53. from: http://www.emacswiki.org/emacs/KillingBuffers"
  54. (interactive)
  55. (other-window 1)
  56. (kill-this-buffer)
  57. (if (not (one-window-p))
  58. (delete-window)))
  59. (defun toggle-window-split ()
  60. (interactive)
  61. (if (= (count-windows) 2)
  62. (let* ((this-win-buffer (window-buffer))
  63. (next-win-buffer (window-buffer (next-window)))
  64. (this-win-edges (window-edges (selected-window)))
  65. (next-win-edges (window-edges (next-window)))
  66. (this-win-2nd (not (and (<= (car this-win-edges)
  67. (car next-win-edges))
  68. (<= (cadr this-win-edges)
  69. (cadr next-win-edges)))))
  70. (splitter
  71. (if (= (car this-win-edges)
  72. (car (window-edges (next-window))))
  73. 'split-window-horizontally
  74. 'split-window-vertically)))
  75. (delete-other-windows)
  76. (let ((first-win (selected-window)))
  77. (funcall splitter)
  78. (if this-win-2nd (other-window 1))
  79. (set-window-buffer (selected-window) this-win-buffer)
  80. (set-window-buffer (next-window) next-win-buffer)
  81. (select-window first-win)
  82. (if this-win-2nd (other-window 1))))))
  83. (defun rotate-windows ()
  84. "Rotate your windows"
  85. (interactive)
  86. (cond ((not (> (count-windows)1))
  87. (message "You can't rotate a single window!"))
  88. (t
  89. (setq i 1)
  90. (setq numWindows (count-windows))
  91. (while (< i numWindows)
  92. (let* (
  93. (w1 (elt (window-list) i))
  94. (w2 (elt (window-list) (+ (% i numWindows) 1)))
  95. (b1 (window-buffer w1))
  96. (b2 (window-buffer w2))
  97. (s1 (window-start w1))
  98. (s2 (window-start w2))
  99. )
  100. (set-window-buffer w1 b2)
  101. (set-window-buffer w2 b1)
  102. (set-window-start w1 s2)
  103. (set-window-start w2 s1)
  104. (setq i (1+ i)))))))
  105. (defun get-buffers-with-major-mode (mode)
  106. "Returns list of buffers with major-mode MODE or derived from MODE."
  107. (cl-loop
  108. for buf in (buffer-list)
  109. if (and (buffer-live-p buf)
  110. (with-current-buffer buf
  111. (derived-mode-p mode)))
  112. collect buf))
  113. (defun transpose-windows (arg)
  114. "Transpose the buffers shown in two windows."
  115. (interactive "p")
  116. (let ((selector (if (>= arg 0)
  117. 'next-window
  118. 'previous-window)))
  119. (while (/= arg 0)
  120. (let ((this-win (window-buffer))
  121. (next-win (window-buffer (funcall selector))))
  122. (set-window-buffer (selected-window) next-win)
  123. (set-window-buffer (funcall selector) this-win)
  124. (select-window (funcall selector)))
  125. (setq arg (if (plusp arg)
  126. (1- arg)
  127. (1+ arg))))))
  128. (defun untabify-buffer ()
  129. (interactive)
  130. (untabify (point-min) (point-max)))
  131. (defun indent-buffer ()
  132. (interactive)
  133. (indent-region (point-min) (point-max)))
  134. (defun cleanup-buffer ()
  135. "Perform a bunch of operations on the whitespace content of a buffer.
  136. Including indent-buffer, which should not be called automatically on save."
  137. (interactive)
  138. (untabify-buffer)
  139. (indent-buffer))
  140. (defun file-name-with-one-directory (file-name)
  141. (concat (cadr (reverse (split-string file-name "/"))) "/"
  142. (file-name-nondirectory file-name)))
  143. (defvar user-home-directory (concat (expand-file-name "~") "/"))
  144. ;;Recent open
  145. (defun recentf--file-no-prefix (file-name)
  146. (cons (s-chop-prefix user-home-directory file-name) file-name))
  147. (defun recentf-find-file ()
  148. "Find a recent file using ido."
  149. (interactive)
  150. (let* ((recent-files (mapcar 'recentf--file-no-prefix recentf-list))
  151. (files (mapcar 'car recent-files))
  152. (file (completing-read "Choose recent file: " files)))
  153. (find-file (cdr (assoc file recent-files)))))
  154. (defun ido-recentf-open ()
  155. "Use `ido-completing-read' to \\[find-file] a recent file."
  156. (interactive)
  157. (let* ((recent-files (mapcar 'recentf--file-no-prefix recentf-list))
  158. (files (mapcar 'car recent-files))
  159. (file (ido-completing-read "Find recent file:: " files)))
  160. (find-file (cdr (assoc file recent-files)))))
  161. (defun switch-to-minibuffer ()
  162. "Make the active minibuffer the selected window."
  163. (interactive)
  164. (when (active-minibuffer-window)
  165. (select-window (active-minibuffer-window))))
  166. (defun switch-to-previous-buffer ()
  167. "Switch to previously open buffer.
  168. Repeated invocations toggle between the two most recently open buffers
  169. from: http://emacsredux.com/blog/2013/04/28/switch-to-previous-buffer/"
  170. (interactive)
  171. (switch-to-buffer (other-buffer (current-buffer) 1)))
  172. (defun open-buffer-delete-others(buffer-name register-name action &optional open-same-window)
  173. "Open `BUFFER-NAME' in fullscreen and delete other windows.
  174. `BUFFER-NAME': NAME of buffer, `NAME-REGISTER': name to register current window
  175. action: the action execute, optional, open-same-window"
  176. (if (not (equal (buffer-name) buffer-name))
  177. (progn
  178. (window-configuration-to-register register-name)
  179. (let ((name-buffer (get-buffer buffer-name)))
  180. (if name-buffer
  181. (if open-same-window
  182. (switch-to-buffer name-buffer)
  183. (switch-to-buffer-other-window name-buffer))
  184. (funcall action)))
  185. (delete-other-windows))))
  186. (defun bury-buffer-restore-prev (register-name)
  187. "Restore the previous window configuration by `REGISTER-NAME' and bury buffer."
  188. (interactive)
  189. (bury-buffer)
  190. (jump-to-register-and-remove register-name))
  191. (defun jump-to-register-and-remove (register-name)
  192. "Jump to `REGISTER-NAME' if exists and remove register."
  193. (let ((register (get-register register-name)))
  194. (when register
  195. (jump-to-register register-name)
  196. (delete-register register-name))))
  197. (defun delete-register (register-name)
  198. "Delete `REGISTER-NAME' if exists."
  199. (let ((register (get-register register-name)))
  200. (when register
  201. (setq register-alist (delete (cons register-name register) register-alist)))))
  202. ;; code from https://gist.github.com/anonymous/1061884
  203. (defun auto-byte-recompile ()
  204. "Recompile if the current buffer is `emacs-lisp-mode'.
  205. If exists an `.elc' file corresponding to the current buffer file,
  206. then recompile the file."
  207. (interactive)
  208. (when (and (eq major-mode 'emacs-lisp-mode)
  209. (file-exists-p (byte-compile-dest-file buffer-file-name)))
  210. (byte-compile-file buffer-file-name)))
  211. ;; Compiled lips files
  212. (defun byte-compile-init-dir ()
  213. "Byte-compile all your dotfiles."
  214. (interactive)
  215. (byte-recompile-directory user-emacs-directory 0))
  216. (defun byte-compile-when-save()
  217. "When save, recompile it if already have compile file"
  218. (make-local-variable 'after-save-hook)
  219. (add-hook 'after-save-hook
  220. (lambda ()
  221. (if (file-exists-p (concat buffer-file-name "c"))
  222. (byte-compile-file buffer-file-name)))))
  223. (provide 'buffer-defuns)