Add a minor mode and keybindings to sy-git.
authorSteve Youngs <steve@sxemacs.org>
Fri, 20 Oct 2017 02:26:57 +0000 (12:26 +1000)
committerSteve Youngs <steve@sxemacs.org>
Fri, 20 Oct 2017 02:26:57 +0000 (12:26 +1000)
With this changeset the goodies in sy-git.el are only available in buffers
of files that are under git control.  And new keybindings were added:

  `C-x G a' -- #'sy-git-add-log-entry
  `C-x G b' -- #'sy-git-blame
  `C-x G =' -- #'sy-git-diff
  `C-x G d' -- #'sy-git-diff
  `C-x G e' -- #'sy-git-ediff
  `C-x G l' -- #'sy-git-log

The global binding for #'sy-git-add-log-entry has been removed.

* sy-git.el (sy-git-mode-map): New.
(sy-git-mode): New minor mode.
(sy-git-activate-maybe): New, for use in `find-file-hooks'. Will
only activate if the visited file is under git control.

Signed-off-by: Steve Youngs <steve@sxemacs.org>
sy-git.el

index 7c8b63e..e71fdee 100644 (file)
--- a/sy-git.el
+++ b/sy-git.el
@@ -5,7 +5,7 @@
 ;; Author:     Steve Youngs <steve@sxemacs.org>
 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
 ;; Created:    <2015-07-05>
-;; Time-stamp: <Thursday Oct 19, 2017 21:43:53 steve>
+;; Time-stamp: <Friday Oct 20, 2017 12:14:13 steve>
 ;; Homepage:   http://git.sxemacs.org/slh
 ;; Keywords:   git, tools, convenience
 
@@ -294,8 +294,31 @@ Example post-commit:
       (unless (search-forward newhead nil t)
         (insert newhead "\n\n\n")))))
 
-
-(global-set-key [(control x) G a] #'sy-git-add-log-entry) 
+;;; keymap/mode/hook stuffs
+(defvar sy-git-mode-map
+  (let ((map (make-sparse-keymap)))
+    (set-keymap-name map 'sy-git-mode-map)
+    (define-key map [(control ?x) ?G ?a] #'sy-git-add-log-entry)
+    (define-key map [(control ?x) ?G ?=] #'sy-git-diff)
+    (define-key map [(control ?x) ?G ?d] #'sy-git-diff)
+    (define-key map [(control ?x) ?G ?e] #'sy-git-ediff)
+    (define-key map [(control ?x) ?G ?l] #'sy-git-log)
+    (define-key map [(control ?x) ?G ?b] #'sy-git-blame)
+    map)
+  "Keys for use in sy-git-mode.")
+
+(define-minor-mode sy-git-mode
+  "A minor mode to pretty much do nothing but hold keymap.
+\\{sy-git-mode-map}."
+  :lighter " SY/Git"
+  :keymap 'sy-git-mode-map)
+
+(defun sy-git-activate-maybe ()
+  "Maybe turn on `sy-git-mode'."
+  (and (eq (vc-file-getprop (buffer-file-name) 'vc-backend) 'GIT)
+       (sy-git-mode)))
+
+(add-hook 'find-file-hooks #'sy-git-activate-maybe 'append)
 
 (provide 'sy-git)
 ;;; sy-git.el ends here