]> git.cworth.org Git - nogit/commitdiff
Add code for integrating nogit with emacs
authorCarl Worth <cworth@cworth.org>
Sun, 5 Jul 2020 18:23:32 +0000 (11:23 -0700)
committerCarl Worth <cworth@cworth.org>
Sun, 5 Jul 2020 18:23:32 +0000 (11:23 -0700)
With hooks for invoking "nogit sync" whenever a nogit-controlled file
is opened, edited, or saved.

README

diff --git a/README b/README
index 0923e5c999d4498b63f11ef4133ec376d9f52bd0..fcf7d32ae95ddf8855e64d1d06d1c3998500aa92 100644 (file)
--- a/README
+++ b/README
@@ -35,6 +35,43 @@ sync" should not ever ask for user input, so it's appropriate to call
 "nogit sync" from an autoamted system, (such as a text editor's hook
 when saving a file).
 
+Integration with emacs
+======================
+If you're an emacs user, here is some code you could put into your
+.emacs file to have "nogit sync" called automatically for you for any
+nogit-controlled files whenever you open one, begin editing it, or
+save it:
+
+  ; Run "nogit sync" if there is a .noggit directory here.
+  ;
+  ; Note: There is an important protection built into this implementation:
+  ;
+  ;       We bind 'in-nogit-sync and test whether it's bound to avoid
+  ;       infinite recursion. This could otherwise come about because
+  ;       the revert-buffer function could trigger the find-file-hook
+  ;       and recurse.
+  (defun nogit-sync-if-configured ()
+    (interactive)
+    (if (and
+        (buffer-file-name)
+         (file-exists-p (format "%s/../.nogit" (buffer-file-name)))
+         (not (boundp 'in-nogit-sync))
+         )
+        (let ((in-nogit-sync t))
+          (message (substring (shell-command-to-string "nogit sync") 0 -1))
+          (revert-buffer nil t)
+        )
+      )
+    )
+
+  ; Arrange to run "nogit sync" when the user loads a nogit-controlled file,
+  ; starts editing it for the first time, or saves it.
+  (add-hook 'find-file-hook 'nogit-sync-if-configured)
+  (add-hook 'first-change-hook 'nogit-sync-if-configured)
+  (add-hook 'after-save-hook 'nogit-sync-if-configured)
+
+Preparing git repositories for use with nogit
+=============================================
 If you're interested in setting up a parent and child repository for
 use with nogit, here's what you'll want to do:
 
@@ -61,7 +98,6 @@ use with nogit, here's what you'll want to do:
 
 Motivation
 ==========
-
 I originally came up with nogit when I started maintaining a simple
 TODO file for a project which had a code implementation split across
 multiple code repositories. I was intentionally keeping my TODO file