Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.3 KiB

  1. ;;; ibuffer-tramp.el --- Group ibuffer's list by TRAMP connection
  2. ;;
  3. ;; Copyright (C) 2011 Svend Sorensen
  4. ;;
  5. ;; Author: Svend Sorensen <svend@ciffer.net>
  6. ;; Keywords: convenience
  7. ;; Package-Version: 1.0.0
  8. ;; Package-Commit: bcad0bda3a67f55d1be936bf8fa9ef735fe1e3f3
  9. ;; X-URL: http://github.com/svend/ibuffer-tramp
  10. ;; URL: http://github.com/svend/ibuffer-tramp
  11. ;; Version: DEV
  12. ;
  13. ;; This program is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;;
  18. ;; This program is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;;
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; This code is heavily based on Steve Purcell's ibuffer-vc
  29. ;; (http://github.com/purcell/ibuffer-vc).
  30. ;;
  31. ;; Adds functionality to ibuffer for grouping buffers by their TRAMP
  32. ;; connection.
  33. ;;
  34. ;;; Use:
  35. ;;
  36. ;; To group buffers by TRAMP connection:
  37. ;;
  38. ;; M-x ibuffer-tramp-set-filter-groups-by-tramp-connection
  39. ;;
  40. ;; or, make this the default:
  41. ;;
  42. ;; (add-hook 'ibuffer-hook
  43. ;; (lambda ()
  44. ;; (ibuffer-tramp-set-filter-groups-by-tramp-connection)
  45. ;; (ibuffer-do-sort-by-alphabetic)))
  46. ;;
  47. ;; Alternatively, use `ibuffer-tramp-generate-filter-groups-by-tramp-connection'
  48. ;; to programmatically obtain a list of filter groups that you can
  49. ;; combine with your own custom groups.
  50. ;;
  51. ;;; Code:
  52. ;; requires
  53. (require 'ibuffer)
  54. (require 'ibuf-ext)
  55. (require 'tramp)
  56. (eval-when-compile
  57. (require 'cl))
  58. (defun ibuffer-tramp-connection (buf)
  59. "Return a cons cell (method . host), or nil if the file is not
  60. using a TRAMP connection"
  61. (let ((file-name (with-current-buffer buf (or buffer-file-name default-directory))))
  62. (when (tramp-tramp-file-p file-name)
  63. (let ((method (tramp-file-name-method (tramp-dissect-file-name file-name)))
  64. (host (tramp-file-name-host (tramp-dissect-file-name file-name))))
  65. (cons method host)))))
  66. ;;;###autoload
  67. (defun ibuffer-tramp-generate-filter-groups-by-tramp-connection ()
  68. "Create a set of ibuffer filter groups based on the TRAMP connection of buffers"
  69. (let ((roots (ibuffer-remove-duplicates
  70. (delq nil (mapcar 'ibuffer-tramp-connection (buffer-list))))))
  71. (mapcar (lambda (tramp-connection)
  72. (cons (format "%s:%s" (car tramp-connection) (cdr tramp-connection))
  73. `((tramp-connection . ,tramp-connection))))
  74. roots)))
  75. (define-ibuffer-filter tramp-connection
  76. "Toggle current view to buffers with TRAMP connection QUALIFIER."
  77. (:description "TRAMP connection"
  78. :reader (read-from-minibuffer "Filter by TRAMP connection (regexp): "))
  79. (ibuffer-awhen (ibuffer-tramp-connection buf)
  80. (equal qualifier it)))
  81. ;;;###autoload
  82. (defun ibuffer-tramp-set-filter-groups-by-tramp-connection ()
  83. "Set the current filter groups to filter by TRAMP connection."
  84. (interactive)
  85. (setq ibuffer-filter-groups (ibuffer-tramp-generate-filter-groups-by-tramp-connection))
  86. (ibuffer-update nil t))
  87. (provide 'ibuffer-tramp)
  88. ;;; ibuffer-tramp.el ends here