1 ;; stgit.el: An emacs mode for StGit
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
10 ;; To start: `M-x stgit'
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
15 (switch-to-stgit-buffer dir)
18 (defun switch-to-stgit-buffer (dir)
19 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
20 (setq dir (file-name-as-directory dir))
21 (let ((buffers (buffer-list)))
23 (not (with-current-buffer (car buffers)
24 (and (eq major-mode 'stgit-mode)
25 (string= default-directory dir)))))
26 (setq buffers (cdr buffers)))
27 (switch-to-buffer (if buffers
29 (create-stgit-buffer dir)))))
31 (defun create-stgit-buffer (dir)
32 "Create a buffer for showing StGit patches.
33 Argument DIR is the repository path."
34 (let ((buf (create-file-buffer (concat dir "*stgit*")))
35 (inhibit-read-only t))
36 (with-current-buffer buf
37 (setq default-directory dir)
39 (setq buffer-read-only t))
42 (defmacro stgit-capture-output (name &rest body)
43 "Capture StGit output and show it in a window at the end"
44 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
45 (stgit-dir default-directory)
46 (inhibit-read-only t))
47 (with-current-buffer output-buf
49 (setq default-directory stgit-dir)
50 (setq buffer-read-only t))
51 (let ((standard-output output-buf))
53 (with-current-buffer output-buf
54 (set-buffer-modified-p nil)
55 (setq buffer-read-only t)
56 (if (< (point-min) (point-max))
57 (display-buffer output-buf t)))))
58 (put 'stgit-capture-output 'lisp-indent-function 1)
60 (defun stgit-run (&rest args)
61 (apply 'call-process "stg" nil standard-output nil args))
63 (defun stgit-refresh ()
64 "Update the contents of the stgit buffer"
66 (let ((inhibit-read-only t)
67 (curline (line-number-at-pos))
68 (curpatch (stgit-patch-at-point)))
72 (stgit-run "series" "--description")
74 (stgit-goto-patch curpatch)
75 (goto-line curline))))
77 (defvar stgit-mode-hook nil
78 "Run after `stgit-mode' is setup.")
80 (defvar stgit-mode-map nil
81 "Keymap for StGit major mode.")
83 (unless stgit-mode-map
84 (setq stgit-mode-map (make-keymap))
85 (suppress-keymap stgit-mode-map)
86 (define-key stgit-mode-map "?" 'stgit-help)
87 (define-key stgit-mode-map "h" 'stgit-help)
88 (define-key stgit-mode-map "p" 'previous-line)
89 (define-key stgit-mode-map "n" 'next-line)
90 (define-key stgit-mode-map "g" 'stgit-refresh)
91 (define-key stgit-mode-map "r" 'stgit-rename)
92 (define-key stgit-mode-map ">" 'stgit-push-next)
93 (define-key stgit-mode-map "<" 'stgit-pop-next)
94 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
95 (define-key stgit-mode-map "G" 'stgit-goto)
96 (define-key stgit-mode-map "=" 'stgit-show))
99 "Major mode for interacting with StGit.
102 (kill-all-local-variables)
103 (buffer-disable-undo)
104 (setq mode-name "StGit"
105 major-mode 'stgit-mode
107 (use-local-map stgit-mode-map)
108 (set (make-local-variable 'list-buffers-directory) default-directory)
109 (set-variable 'truncate-lines 't)
110 (run-hooks 'stgit-mode-hook))
112 (defun stgit-patch-at-point ()
113 "Return the patch name on the current line"
116 (if (looking-at "[>+-] \\([^ ]*\\)")
120 (defun stgit-goto-patch (patch)
121 "Move point to the line containing PATCH"
123 (goto-char (point-min))
124 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t)
125 (progn (move-to-column goal-column)
130 (defun stgit-rename (name)
131 "Rename the patch under point"
132 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
133 (let ((old-name (stgit-patch-at-point)))
135 (error "No patch on this line"))
136 (stgit-capture-output nil
137 (stgit-run "rename" old-name name))
139 (stgit-goto-patch name)))
141 (defun stgit-push-next ()
142 "Push the first unapplied patch"
144 (stgit-capture-output nil (stgit-run "push"))
147 (defun stgit-pop-next ()
148 "Pop the topmost applied patch"
150 (stgit-capture-output nil (stgit-run "pop"))
153 (defun stgit-applied-at-point ()
154 "Is the patch on the current line applied?"
157 (looking-at "[>+]")))
159 (defun stgit-push-or-pop ()
160 "Push or pop the patch on the current line"
162 (let ((patch (stgit-patch-at-point))
163 (applied (stgit-applied-at-point)))
164 (stgit-capture-output nil
165 (stgit-run (if applied "pop" "push") patch))
169 "Go to the patch on the current line"
171 (let ((patch (stgit-patch-at-point)))
172 (stgit-capture-output nil
173 (stgit-run "goto" patch))
177 "Show the patch on the current line"
179 (stgit-capture-output "*StGit patch*"
180 (stgit-run "show" (stgit-patch-at-point))
181 (with-current-buffer standard-output
182 (goto-char (point-min))
186 "Display help for the StGit mode."
188 (describe-function 'stgit-mode))