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 "?" 'stgit-help)
69 (define-key stgit-mode-map "h" 'stgit-help)
70 (define-key stgit-mode-map "g" 'stgit-refresh)
71 (define-key stgit-mode-map "r" 'stgit-rename)
72 (define-key stgit-mode-map ">" 'stgit-push-next)
73 (define-key stgit-mode-map "<" 'stgit-pop)
74 (define-key stgit-mode-map "=" 'stgit-show))
77 "Major mode for interacting with StGit.
80 (kill-all-local-variables)
82 (setq mode-name "StGit"
83 major-mode 'stgit-mode
85 (use-local-map stgit-mode-map)
86 (set (make-local-variable 'list-buffers-directory) default-directory)
87 (run-hooks 'stgit-mode-hook))
89 (defun stgit-patch-at-point ()
90 "Return the patch name on the current line"
93 (if (looking-at "[>+-] \\(.*\\)")
97 (defun stgit-goto-patch (patch)
98 "Move point to the line containing PATCH"
100 (goto-char (point-min))
101 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) "$") nil t)
102 (progn (move-to-column goal-column)
107 (defun stgit-rename (name)
108 "Rename the patch under point"
109 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
110 (let ((old-name (stgit-patch-at-point)))
112 (error "No patch on this line"))
113 (stgit-capture-output nil
114 (stgit-run "rename" old-name name))
116 (stgit-goto-patch name)))
118 (defun stgit-push-next ()
119 "Push the patch on the line after pos"
122 (let ((patch (stgit-patch-at-point)))
123 (stgit-capture-output nil
124 (stgit-run "push" patch))
128 "Pop the patch on the current line"
130 (let ((patch (stgit-patch-at-point)))
131 (stgit-capture-output nil
132 (stgit-run "pop" patch))
137 "Show the patch on the current line"
139 (stgit-capture-output "*StGit patch*"
140 (stgit-run "show" (stgit-patch-at-point))
141 (with-current-buffer standard-output
142 (goto-char (point-min))
146 "Display help for the StGit mode."
148 (describe-function 'stgit-mode))