Commit | Line | Data |
---|---|---|
3a59f3db KH |
1 | ;; stgit.el: An emacs mode for StGit |
2 | ;; | |
3 | ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se> | |
4 | ;; | |
5 | ;; To install: put this file on the load-path and place the following | |
6 | ;; in your .emacs file: | |
7 | ;; | |
8 | ;; (require 'stgit) | |
9 | ;; | |
10 | ;; To start: `M-x stgit' | |
11 | ||
56d81fe5 DK |
12 | (defun stgit (dir) |
13 | "Manage stgit patches" | |
14 | (interactive "DDirectory: \n") | |
15 | (switch-to-stgit-buffer dir) | |
16 | (stgit-refresh)) | |
17 | ||
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))) | |
22 | (while (and buffers | |
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 | |
28 | (car buffers) | |
29 | (create-stgit-buffer dir))))) | |
30 | ||
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) | |
38 | (stgit-mode) | |
39 | (setq buffer-read-only t)) | |
40 | buf)) | |
41 | ||
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 | |
46 | (erase-buffer)) | |
47 | (let ((standard-output output-buf)) | |
48 | ,@body) | |
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) | |
52 | ||
53 | (defun stgit-run (&rest args) | |
54 | (apply 'call-process "stg" nil standard-output nil args)) | |
55 | ||
56 | (defun stgit-refresh () | |
57 | "Update the contents of the stgit buffer" | |
58 | (interactive) | |
59 | (let ((inhibit-read-only t) | |
60 | (curline (line-number-at-pos)) | |
61 | (curpatch (stgit-patch-at-point))) | |
62 | (erase-buffer) | |
63 | (insert "Branch: ") | |
64 | (stgit-run "branch") | |
2870f8b8 | 65 | (stgit-run "series" "--description") |
56d81fe5 DK |
66 | (if curpatch |
67 | (stgit-goto-patch curpatch) | |
68 | (goto-line curline)))) | |
69 | ||
70 | (defvar stgit-mode-hook nil | |
71 | "Run after `stgit-mode' is setup.") | |
72 | ||
73 | (defvar stgit-mode-map nil | |
74 | "Keymap for StGit major mode.") | |
75 | ||
76 | (unless stgit-mode-map | |
77 | (setq stgit-mode-map (make-keymap)) | |
78 | (suppress-keymap stgit-mode-map) | |
0663524d KH |
79 | (define-key stgit-mode-map "?" 'stgit-help) |
80 | (define-key stgit-mode-map "h" 'stgit-help) | |
56d81fe5 DK |
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) | |
82a12e6e | 84 | (define-key stgit-mode-map "<" 'stgit-pop-next) |
f9182fca | 85 | (define-key stgit-mode-map "P" 'stgit-push-or-pop) |
c7adf5ef | 86 | (define-key stgit-mode-map "G" 'stgit-goto) |
56d81fe5 DK |
87 | (define-key stgit-mode-map "=" 'stgit-show)) |
88 | ||
89 | (defun stgit-mode () | |
90 | "Major mode for interacting with StGit. | |
91 | Commands: | |
92 | \\{stgit-mode-map}" | |
93 | (kill-all-local-variables) | |
94 | (buffer-disable-undo) | |
95 | (setq mode-name "StGit" | |
96 | major-mode 'stgit-mode | |
97 | goal-column 2) | |
98 | (use-local-map stgit-mode-map) | |
99 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
2870f8b8 | 100 | (set-variable 'truncate-lines 't) |
56d81fe5 DK |
101 | (run-hooks 'stgit-mode-hook)) |
102 | ||
103 | (defun stgit-patch-at-point () | |
104 | "Return the patch name on the current line" | |
105 | (save-excursion | |
106 | (beginning-of-line) | |
2870f8b8 | 107 | (if (looking-at "[>+-] \\([^ ]*\\)") |
56d81fe5 DK |
108 | (match-string 1) |
109 | nil))) | |
110 | ||
111 | (defun stgit-goto-patch (patch) | |
112 | "Move point to the line containing PATCH" | |
113 | (let ((p (point))) | |
114 | (goto-char (point-min)) | |
2870f8b8 | 115 | (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t) |
56d81fe5 DK |
116 | (progn (move-to-column goal-column) |
117 | t) | |
118 | (goto-char p) | |
119 | nil))) | |
120 | ||
121 | (defun stgit-rename (name) | |
122 | "Rename the patch under point" | |
123 | (interactive (list (read-string "Patch name: " (stgit-patch-at-point)))) | |
124 | (let ((old-name (stgit-patch-at-point))) | |
125 | (unless old-name | |
126 | (error "No patch on this line")) | |
127 | (stgit-capture-output nil | |
128 | (stgit-run "rename" old-name name)) | |
129 | (stgit-refresh) | |
130 | (stgit-goto-patch name))) | |
131 | ||
132 | (defun stgit-push-next () | |
82a12e6e | 133 | "Push the first unapplied patch" |
56d81fe5 | 134 | (interactive) |
82a12e6e KH |
135 | (stgit-capture-output nil (stgit-run "push")) |
136 | (stgit-refresh)) | |
56d81fe5 | 137 | |
82a12e6e KH |
138 | (defun stgit-pop-next () |
139 | "Pop the topmost applied patch" | |
56d81fe5 | 140 | (interactive) |
82a12e6e KH |
141 | (stgit-capture-output nil (stgit-run "pop")) |
142 | (stgit-refresh)) | |
56d81fe5 | 143 | |
f9182fca KH |
144 | (defun stgit-applied-at-point () |
145 | "Is the patch on the current line applied?" | |
146 | (save-excursion | |
147 | (beginning-of-line) | |
148 | (looking-at "[>+]"))) | |
149 | ||
150 | (defun stgit-push-or-pop () | |
151 | "Push or pop the patch on the current line" | |
152 | (interactive) | |
153 | (let ((patch (stgit-patch-at-point)) | |
154 | (applied (stgit-applied-at-point))) | |
155 | (stgit-capture-output nil | |
156 | (stgit-run (if applied "pop" "push") patch)) | |
157 | (stgit-refresh))) | |
158 | ||
c7adf5ef KH |
159 | (defun stgit-goto () |
160 | "Go to the patch on the current line" | |
161 | (interactive) | |
162 | (let ((patch (stgit-patch-at-point))) | |
163 | (stgit-capture-output nil | |
164 | (stgit-run "goto" patch)) | |
165 | (stgit-refresh))) | |
166 | ||
56d81fe5 DK |
167 | (defun stgit-show () |
168 | "Show the patch on the current line" | |
169 | (interactive) | |
170 | (stgit-capture-output "*StGit patch*" | |
171 | (stgit-run "show" (stgit-patch-at-point)) | |
172 | (with-current-buffer standard-output | |
173 | (goto-char (point-min)) | |
174 | (diff-mode)))) | |
0663524d KH |
175 | |
176 | (defun stgit-help () | |
177 | "Display help for the StGit mode." | |
178 | (interactive) | |
179 | (describe-function 'stgit-mode)) | |
3a59f3db KH |
180 | |
181 | (provide 'stgit) |