summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Kost <alezost@gmail.com>2017-01-12 13:13:32 +0300
committerAlex Kost <alezost@gmail.com>2017-01-14 12:33:22 +0300
commit0c62daaa6d01f14fedb6182b96e146d3db957d46 (patch)
tree4dadd90441600d5c681da58892d391a00d243745
parentdoc: Add "Help" chapter (diff)
downloadguix-wigust-0c62daaa6d01f14fedb6182b96e146d3db957d46.tar.gz
elisp/help: Add 'guix-switch-to-buffer' command
* elisp/guix-help.el (guix-define-buffer-function): New custom variable. (guix-define-buffer-by-name, guix-define-buffer-by-mode) (guix-buffer?, guix-buffers): New procedures. (guix-switch-to-buffer): New command. (guix-help-specifications): Add it. * doc/emacs-guix.texi (Help): Document it.
-rw-r--r--doc/emacs-guix.texi11
-rw-r--r--elisp/guix-help.el46
2 files changed, 57 insertions, 0 deletions
diff --git a/doc/emacs-guix.texi b/doc/emacs-guix.texi
index 5be66b6..b2c5ff8 100644
--- a/doc/emacs-guix.texi
+++ b/doc/emacs-guix.texi
@@ -1172,6 +1172,17 @@ Display a buffer with the summary of the available Emacs-Guix
commands. Buttons in this buffer allow you to run commands and to
look at their docstrings and manual entries.
+@findex guix-switch-to-buffer
+@item M-x guix-switch-to-buffer
+Switch to one of the Emacs-Guix buffers. This is like a usual
+@code{switch-to-buffer} (bound to @kbd{C-x b} by default), except it
+completes only Guix buffers names. If you are going to use this
+command, it is probably better to bind it to some key, for example:
+
+@example
+(global-set-key (kbd "C-x B") 'guix-switch-to-buffer)
+@end example
+
@end table
@c ----------------------------------------------------------------
diff --git a/elisp/guix-help.el b/elisp/guix-help.el
index 68150df..f0bfe02 100644
--- a/elisp/guix-help.el
+++ b/elisp/guix-help.el
@@ -23,6 +23,7 @@
;;; Code:
+(require 'dash)
(require 'bui)
(require 'guix nil t)
(require 'guix-utils)
@@ -112,6 +113,7 @@
guix-set-current-profile
guix-pull
guix-apply-manifest
+ guix-switch-to-buffer
(guix-about t nil)
(guix-version t nil))
"List of command specifications for '\\[guix-help]'.
@@ -270,6 +272,50 @@ Switch to `guix-help-buffer-name' buffer if it already exists."
(guix-switch-to-buffer-or-funcall
guix-help-buffer-name #'guix-help-show))
+
+;;; Guix buffers
+
+(defcustom guix-define-buffer-function #'guix-define-buffer-by-name
+ "Function used to define a Guix buffer.
+This function is used by `guix-switch-to-buffer'. It is called
+with a buffer as a single argument and should return non-nil if
+the buffer is the Guix one."
+ :type '(choice (function-item guix-define-buffer-by-name)
+ (function-item guix-define-buffer-by-mode)
+ (function :tag "Other function"))
+ :group 'guix)
+
+(defun guix-define-buffer-by-name (buffer)
+ "Return non-nil if BUFFER name matches Guix buffer names."
+ (string-match-p "\\`*Guix" (buffer-name buffer)))
+
+(defun guix-define-buffer-by-mode (buffer)
+ "Return non-nil if BUFFER major mode is one of the Guix major modes."
+ (with-current-buffer buffer
+ (string-match-p "\\`guix-" (symbol-name major-mode))))
+
+(defun guix-buffer? (buffer)
+ "Return non-nil if BUFFER is a Guix buffer.
+This is a wrapper for `guix-define-buffer-function'."
+ (funcall guix-define-buffer-function buffer))
+
+(defun guix-buffers ()
+ "Return all Guix buffers matching `guix-define-buffer-function'."
+ (-filter #'guix-buffer? (buffer-list)))
+
+;;;###autoload
+(defun guix-switch-to-buffer (buffer)
+ "Switch to BUFFER.
+Interactively, prompt for BUFFER completing only Guix buffer names.
+Guix buffers are defined using `guix-define-buffer-function'."
+ (interactive
+ (let ((buffers (guix-buffers)))
+ (if (null buffers)
+ (user-error "No Guix buffers found")
+ (list (completing-read "Buffer: "
+ (mapcar #'buffer-name buffers))))))
+ (switch-to-buffer buffer))
+
(provide 'guix-help)
;;; guix-help.el ends here