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 (with-current-buffer output-buf
47 (let ((standard-output output-buf))
49 (if (with-current-buffer output-buf (< (point-min) (point-max)))
50 (display-buffer output-buf t))))
51 (put 'stgit-capture-output 'lisp-indent-function 1)
53 (defun stgit-run (&rest args)
54 (apply 'call-process "stg" nil standard-output nil args))
56 (defun stgit-refresh ()
57 "Update the contents of the stgit buffer"
59 (let ((inhibit-read-only t)
60 (curline (line-number-at-pos))
61 (curpatch (stgit-patch-at-point)))
67 (stgit-goto-patch curpatch)
68 (goto-line curline))))
70 (defvar stgit-mode-hook nil
71 "Run after `stgit-mode' is setup.")
73 (defvar stgit-mode-map nil
74 "Keymap for StGit major mode.")
76 (unless stgit-mode-map
77 (setq stgit-mode-map (make-keymap))
78 (suppress-keymap stgit-mode-map)
79 (define-key stgit-mode-map "?" 'stgit-help)
80 (define-key stgit-mode-map "h" 'stgit-help)
81 (define-key stgit-mode-map "g" 'stgit-refresh)
82 (define-key stgit-mode-map "r" 'stgit-rename)
83 (define-key stgit-mode-map ">" 'stgit-push-next)
84 (define-key stgit-mode-map "<" 'stgit-pop)
85 (define-key stgit-mode-map "=" 'stgit-show))
88 "Major mode for interacting with StGit.
91 (kill-all-local-variables)
93 (setq mode-name "StGit"
94 major-mode 'stgit-mode
96 (use-local-map stgit-mode-map)
97 (set (make-local-variable 'list-buffers-directory) default-directory)
98 (run-hooks 'stgit-mode-hook))
100 (defun stgit-patch-at-point ()
101 "Return the patch name on the current line"
104 (if (looking-at "[>+-] \\(.*\\)")
108 (defun stgit-goto-patch (patch)
109 "Move point to the line containing PATCH"
111 (goto-char (point-min))
112 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) "$") nil t)
113 (progn (move-to-column goal-column)
118 (defun stgit-rename (name)
119 "Rename the patch under point"
120 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
121 (let ((old-name (stgit-patch-at-point)))
123 (error "No patch on this line"))
124 (stgit-capture-output nil
125 (stgit-run "rename" old-name name))
127 (stgit-goto-patch name)))
129 (defun stgit-push-next ()
130 "Push the patch on the line after pos"
133 (let ((patch (stgit-patch-at-point)))
134 (stgit-capture-output nil
135 (stgit-run "push" patch))
139 "Pop the patch on the current line"
141 (let ((patch (stgit-patch-at-point)))
142 (stgit-capture-output nil
143 (stgit-run "pop" patch))
148 "Show the patch on the current line"
150 (stgit-capture-output "*StGit patch*"
151 (stgit-run "show" (stgit-patch-at-point))
152 (with-current-buffer standard-output
153 (goto-char (point-min))
157 "Display help for the StGit mode."
159 (describe-function 'stgit-mode))