Guest User

Untitled

a guest
May 25th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. ;;; Commentary:
  2.  
  3. ;; Kills buffers that I never use, and that annoy me when completion
  4. ;; offers their names.
  5.  
  6. ;; Note that this may be overkill (ha); given that I use "ido", it
  7. ;; might suffice to simply add something to ido-make-buffer-list-hook,
  8. ;; that puts these buffers at the end of the list.
  9.  
  10. ;; This code all depends on a buffer-local variable named
  11. ;; buffer-creation-time. That variable doesn't (yet) exist in
  12. ;; anyone's Emacs but mine, since I hacked it in myself.
  13. (if (fboundp 'buffer-creation-time)
  14. (progn
  15. (defun kill-them-buffers ()
  16. (mapc
  17. (lambda (p)
  18. (flet ((time->string
  19. (time)
  20. (format-seconds "%Y, %D, %H, %M, %z%S" (float-time time))))
  21. (let ((victim (get-buffer (car p))))
  22. (when victim
  23. ;; if the buffer is not old enough, spare its life.
  24. (let ((allotted-lifespan (cdr p))
  25. (age (time-subtract
  26. (current-time)
  27. (with-current-buffer victim
  28.  
  29. ;; Ideally we'd consider the
  30. ;; last-modification time of the buffer
  31. ;; too, and kill it only if _both_ those
  32. ;; times are old. But we don't keep
  33. ;; track of that time (yet :-).
  34. (buffer-creation-time)))))
  35.  
  36. (if (time-less-p allotted-lifespan age)
  37. (progn
  38. ;; if it's displayed in some window, nix the window.
  39. (let ((window-victim (get-buffer-window victim t)))
  40. (while window-victim
  41. (delete-window window-victim)
  42. (setq window-victim (get-buffer-window victim t))))
  43. (message "Killing buffer %s -- it's %s old (which is more than %s)!!"
  44. victim
  45. (time->string age)
  46. (time->string allotted-lifespan))
  47. (kill-buffer victim))
  48.  
  49. (message "Spared buffer %s -- it's only %s old"
  50. victim
  51. (time->string age))))))))
  52.  
  53. ;; These are buffers whose names make them likely to get in my
  54. ;; way when I'm looking for some other, similarly-named,
  55. ;; buffer. Ido probably makes this problem worse.
  56. `(
  57. ;; Times are pairs of (HIGH LOW) seconds; see 'current-time'
  58. ("*Completions*" . (0 3))
  59. ("*Shell Command Output*" . (0 120))
  60. ("*vc-diff*" . (0 60))
  61. )))
  62.  
  63. (setq *reaper-timer*
  64. (run-with-idle-timer 5 t 'kill-them-buffers))
  65.  
  66. (when nil
  67. (cancel-timer *reaper-timer*)))
  68. (message "There's no function named buffer-creation-time, so I cannot start the reaper."))
Add Comment
Please, Sign In to add comment