3 (interactive "DDirectory: \n")
4 (switch-to-stgit-buffer dir)
7 (defun switch-to-stgit-buffer (dir)
8 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
9 (setq dir (file-name-as-directory dir))
10 (let ((buffers (buffer-list)))
12 (not (with-current-buffer (car buffers)
13 (and (eq major-mode 'stgit-mode)
14 (string= default-directory dir)))))
15 (setq buffers (cdr buffers)))
16 (switch-to-buffer (if buffers
18 (create-stgit-buffer dir)))))
20 (defun create-stgit-buffer (dir)
21 "Create a buffer for showing StGit patches.
22 Argument DIR is the repository path."
23 (let ((buf (create-file-buffer (concat dir "*stgit*")))
24 (inhibit-read-only t))
25 (with-current-buffer buf
26 (setq default-directory dir)
28 (setq buffer-read-only t))
31 (defmacro stgit-capture-output (name &rest body)
32 "Capture StGit output and show it in a window at the end"
33 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*"))))
34 (with-current-buffer output-buf
36 (let ((standard-output output-buf))
38 (if (with-current-buffer output-buf (< (point-min) (point-max)))
39 (display-buffer output-buf t))))
40 (put 'stgit-capture-output 'lisp-indent-function 1)
42 (defun stgit-run (&rest args)
43 (apply 'call-process "stg" nil standard-output nil args))
45 (defun stgit-refresh ()
46 "Update the contents of the stgit buffer"
48 (let ((inhibit-read-only t)
49 (curline (line-number-at-pos))
50 (curpatch (stgit-patch-at-point)))
56 (stgit-goto-patch curpatch)
57 (goto-line curline))))
59 (defvar stgit-mode-hook nil
60 "Run after `stgit-mode' is setup.")
62 (defvar stgit-mode-map nil
63 "Keymap for StGit major mode.")
65 (unless stgit-mode-map
66 (setq stgit-mode-map (make-keymap))
67 (suppress-keymap stgit-mode-map)
68 (define-key stgit-mode-map "g" 'stgit-refresh)
69 (define-key stgit-mode-map "r" 'stgit-rename)
70 (define-key stgit-mode-map ">" 'stgit-push-next)
71 (define-key stgit-mode-map "<" 'stgit-pop)
72 (define-key stgit-mode-map "=" 'stgit-show))
75 "Major mode for interacting with StGit.
78 (kill-all-local-variables)
80 (setq mode-name "StGit"
81 major-mode 'stgit-mode
83 (use-local-map stgit-mode-map)
84 (set (make-local-variable 'list-buffers-directory) default-directory)
85 (run-hooks 'stgit-mode-hook))
87 (defun stgit-patch-at-point ()
88 "Return the patch name on the current line"
91 (if (looking-at "[>+-] \\(.*\\)")
95 (defun stgit-goto-patch (patch)
96 "Move point to the line containing PATCH"
98 (goto-char (point-min))
99 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) "$") nil t)
100 (progn (move-to-column goal-column)
105 (defun stgit-rename (name)
106 "Rename the patch under point"
107 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
108 (let ((old-name (stgit-patch-at-point)))
110 (error "No patch on this line"))
111 (stgit-capture-output nil
112 (stgit-run "rename" old-name name))
114 (stgit-goto-patch name)))
116 (defun stgit-push-next ()
117 "Push the patch on the line after pos"
120 (let ((patch (stgit-patch-at-point)))
121 (stgit-capture-output nil
122 (stgit-run "push" patch))
126 "Pop the patch on the current line"
128 (let ((patch (stgit-patch-at-point)))
129 (stgit-capture-output nil
130 (stgit-run "pop" patch))
135 "Show the patch on the current line"
137 (stgit-capture-output "*StGit patch*"
138 (stgit-run "show" (stgit-patch-at-point))
139 (with-current-buffer standard-output
140 (goto-char (point-min))