Commit | Line | Data |
---|---|---|
56d81fe5 DK |
1 | (defun stgit (dir) |
2 | "Manage stgit patches" | |
3 | (interactive "DDirectory: \n") | |
4 | (switch-to-stgit-buffer dir) | |
5 | (stgit-refresh)) | |
6 | ||
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))) | |
11 | (while (and buffers | |
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 | |
17 | (car buffers) | |
18 | (create-stgit-buffer dir))))) | |
19 | ||
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) | |
27 | (stgit-mode) | |
28 | (setq buffer-read-only t)) | |
29 | buf)) | |
30 | ||
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 | |
35 | (erase-buffer)) | |
36 | (let ((standard-output output-buf)) | |
37 | ,@body) | |
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) | |
41 | ||
42 | (defun stgit-run (&rest args) | |
43 | (apply 'call-process "stg" nil standard-output nil args)) | |
44 | ||
45 | (defun stgit-refresh () | |
46 | "Update the contents of the stgit buffer" | |
47 | (interactive) | |
48 | (let ((inhibit-read-only t) | |
49 | (curline (line-number-at-pos)) | |
50 | (curpatch (stgit-patch-at-point))) | |
51 | (erase-buffer) | |
52 | (insert "Branch: ") | |
53 | (stgit-run "branch") | |
54 | (stgit-run "series") | |
55 | (if curpatch | |
56 | (stgit-goto-patch curpatch) | |
57 | (goto-line curline)))) | |
58 | ||
59 | (defvar stgit-mode-hook nil | |
60 | "Run after `stgit-mode' is setup.") | |
61 | ||
62 | (defvar stgit-mode-map nil | |
63 | "Keymap for StGit major mode.") | |
64 | ||
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)) | |
73 | ||
74 | (defun stgit-mode () | |
75 | "Major mode for interacting with StGit. | |
76 | Commands: | |
77 | \\{stgit-mode-map}" | |
78 | (kill-all-local-variables) | |
79 | (buffer-disable-undo) | |
80 | (setq mode-name "StGit" | |
81 | major-mode 'stgit-mode | |
82 | goal-column 2) | |
83 | (use-local-map stgit-mode-map) | |
84 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
85 | (run-hooks 'stgit-mode-hook)) | |
86 | ||
87 | (defun stgit-patch-at-point () | |
88 | "Return the patch name on the current line" | |
89 | (save-excursion | |
90 | (beginning-of-line) | |
91 | (if (looking-at "[>+-] \\(.*\\)") | |
92 | (match-string 1) | |
93 | nil))) | |
94 | ||
95 | (defun stgit-goto-patch (patch) | |
96 | "Move point to the line containing PATCH" | |
97 | (let ((p (point))) | |
98 | (goto-char (point-min)) | |
99 | (if (re-search-forward (concat "[>+-] " (regexp-quote patch) "$") nil t) | |
100 | (progn (move-to-column goal-column) | |
101 | t) | |
102 | (goto-char p) | |
103 | nil))) | |
104 | ||
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))) | |
109 | (unless old-name | |
110 | (error "No patch on this line")) | |
111 | (stgit-capture-output nil | |
112 | (stgit-run "rename" old-name name)) | |
113 | (stgit-refresh) | |
114 | (stgit-goto-patch name))) | |
115 | ||
116 | (defun stgit-push-next () | |
117 | "Push the patch on the line after pos" | |
118 | (interactive) | |
119 | (forward-line 1) | |
120 | (let ((patch (stgit-patch-at-point))) | |
121 | (stgit-capture-output nil | |
122 | (stgit-run "push" patch)) | |
123 | (stgit-refresh))) | |
124 | ||
125 | (defun stgit-pop () | |
126 | "Pop the patch on the current line" | |
127 | (interactive) | |
128 | (let ((patch (stgit-patch-at-point))) | |
129 | (stgit-capture-output nil | |
130 | (stgit-run "pop" patch)) | |
131 | (stgit-refresh) | |
132 | (previous-line))) | |
133 | ||
134 | (defun stgit-show () | |
135 | "Show the patch on the current line" | |
136 | (interactive) | |
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)) | |
141 | (diff-mode)))) |