[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


From: Thien-Thi Nguyen
Subject: git-show-branch.el
Date: Thu, 07 Jul 2011 10:05:25 +0200

Greetings earthlings,

Do you use Git?  Are you a branch lover?
You know it!  You show it!

The primary value of this code IMHO is NOT the output, but rather the
keymap, upon which you can hang all sorts of ref-based commands, kind of
like a tree trunk of enquiry and reflection.  But that's no surprise for
branch lovers.  For example, here is the func that ‘more-vc-git-show’
and ‘more-vc-git-rebase’ (both not yet published) use:

(defun more-vc-ref-at-point ()
  "Return the reference at point as a string.
This is essentially the `symbol-at-point' under Emacs Lisp syntax."
  (with-syntax-table emacs-lisp-mode-syntax-table
    (substring-no-properties (thing-at-point 'symbol))))

The reason ‘more-vc-ref-at-point’ is not and most likely will never be
part of git-show-branch.el is to keep the latter simple.


;;; git-show-branch.el
;;; Copyright (C) 2011 Thien-Thi Nguyen
;;; This file is part of ttn's personal elisp library, released under
;;; the terms of the GNU General Public License as published by the
;;; Free Software Foundation; either version 3, or (at your option) any
;;; later version.  There is NO WARRANTY.  See file COPYING for details.
;;; Description: Show "git show-branch" output in a buffer.

(require 'view)
(require 'vc-git)
(require 'set-keys)

(defvar git-show-branch-map (copy-keymap view-mode-map)
  "Keymap for buffer prepared by `git-show-branch'.")

(defvar git-show-branch-all nil)

(defun git-show-branch-all ()
  "Toggle variable `git-show-branch-all'.
If the current buffer is named \"*gb* ...\", that is,
presumed to be created by `git-show-branch', revert it."
  (message "git-show-branch-all: %S"
           (setq git-show-branch-all
                 (not git-show-branch-all)))
  (when (string-match "^\\*gb\\* " (or (buffer-name) ""))
    (revert-buffer t t)))

(defun git-show-branch (dir)
  "Display \"git show-branch\" output for DIR in a buffer.
Actually, use its top directory, the one returned by `vc-git-root'.
Interactively, try `default-directory' first, and query if no
top directory is found.

You can use commands from keymap `git-show-branch-map' in that buffer."
  (interactive (list (or (vc-git-root default-directory)
                         (read-directory-name "Directory: "))))
  (setq dir (file-name-as-directory dir))
  (switch-to-buffer (get-buffer-create (format "*gb* %s" dir)))
  (setq default-directory dir)
  (set (make-local-variable 'revert-buffer-function)
       (lambda (ignore-auto noconfirm)
         (git-show-branch default-directory)))
  (shell-command (format "git show-branch %s"
                         (if git-show-branch-all "-a" ""))
  (insert "Directory: " dir "\n\n")
  (view-mode 1)
  (use-local-map git-show-branch-map))

;;; load-time actions

(define-keys git-show-branch-map
  "\C-c\C-a" 'git-show-branch-all
  [remap View-quit] 'bury-buffer)

;;; that's it!

(provide 'git-show-branch)

;;; git-show-branch.el ends here

reply via email to

[Prev in Thread] Current Thread [Next in Thread]