2009-02-19 Carsten Dominik <dominik@science.uva.nl>
[bpt/emacs.git] / lisp / vc-mtn.el
CommitLineData
b1dc6d44
SM
1;;; vc-mtn.el --- VC backend for Monotone
2
ae940284 3;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
b1dc6d44
SM
4
5;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6;; Keywords:
7
eb3fa2cf
GM
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
b1dc6d44 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
b1dc6d44 14
eb3fa2cf 15;; GNU Emacs is distributed in the hope that it will be useful,
b1dc6d44
SM
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b1dc6d44
SM
22
23;;; Commentary:
24
25;;
26
469ca403
DN
27;;; TODO:
28
29;; - The `previous-version' VC method needs to be supported, 'D' in
30;; log-view-mode uses it.
31
b1dc6d44
SM
32;;; Code:
33
34(eval-when-compile (require 'cl) (require 'vc))
35
a857238c
GM
36(defcustom vc-mtn-diff-switches t
37 "String or list of strings specifying switches for monotone diff under VC.
633883e7 38If nil, use the value of `vc-diff-switches'. If t, use no switches."
a857238c
GM
39 :type '(choice (const :tag "Unspecified" nil)
40 (const :tag "None" t)
41 (string :tag "Argument String")
633883e7 42 (repeat :tag "Argument List" :value ("") string))
a857238c
GM
43 :version "23.1"
44 :group 'vc)
45
46(define-obsolete-variable-alias 'vc-mtn-command 'vc-mtn-program "23.1")
47(defcustom vc-mtn-program "mtn"
48 "Name of the monotone executable."
49 :type 'string
50 :group 'vc)
51
b1dc6d44
SM
52;; Clear up the cache to force vc-call to check again and discover
53;; new functions when we reload this file.
54(put 'Mtn 'vc-functions nil)
55
a857238c 56(unless (executable-find vc-mtn-program)
b1dc6d44
SM
57 ;; vc-mtn.el is 100% non-functional without the `mtn' executable.
58 (setq vc-handled-backends (delq 'Mtn vc-handled-backends)))
59
60;;;###autoload
61(defconst vc-mtn-admin-dir "_MTN")
62;;;###autoload
63(defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format"))
64
65;;;###autoload (defun vc-mtn-registered (file)
66;;;###autoload (if (vc-find-root file vc-mtn-admin-format)
67;;;###autoload (progn
68;;;###autoload (load "vc-mtn")
69;;;###autoload (vc-mtn-registered file))))
70
71(defun vc-mtn-revision-granularity () 'repository)
70e2f6c7 72(defun vc-mtn-checkout-model (files) 'implicit)
b1dc6d44
SM
73
74(defun vc-mtn-root (file)
75 (setq file (if (file-directory-p file)
76 (file-name-as-directory file)
77 (file-name-directory file)))
78 (or (vc-file-getprop file 'vc-mtn-root)
79 (vc-file-setprop file 'vc-mtn-root
80 (vc-find-root file vc-mtn-admin-format))))
81
82
83(defun vc-mtn-registered (file)
84 (let ((root (vc-mtn-root file)))
85 (when root
86 (vc-mtn-state file))))
87
88(defun vc-mtn-command (buffer okstatus files &rest flags)
89 "A wrapper around `vc-do-command' for use in vc-mtn.el."
ce4025c7
SM
90 (let ((process-environment
91 ;; Avoid localization of messages so we can parse the output.
92 (cons "LC_MESSAGES=C" process-environment)))
a857238c
GM
93 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program
94 files flags)))
b1dc6d44
SM
95
96(defun vc-mtn-state (file)
97 ;; If `mtn' fails or returns status>0, or if the search files, just
98 ;; return nil.
99 (ignore-errors
100 (with-temp-buffer
101 (vc-mtn-command t 0 file "status")
102 (goto-char (point-min))
3281a821
DN
103 (re-search-forward
104 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
105 (cond ((match-end 1) 'edited)
106 ((match-end 2) 'added)
107 (t 'up-to-date)))))
b1dc6d44 108
ac3f4c6f 109(defun vc-mtn-working-revision (file)
b1dc6d44
SM
110 ;; If `mtn' fails or returns status>0, or if the search fails, just
111 ;; return nil.
112 (ignore-errors
113 (with-temp-buffer
114 (vc-mtn-command t 0 file "status")
115 (goto-char (point-min))
116 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
117 (match-string 2))))
118
119(defun vc-mtn-workfile-branch (file)
120 ;; If `mtn' fails or returns status>0, or if the search files, just
121 ;; return nil.
122 (ignore-errors
123 (with-temp-buffer
124 (vc-mtn-command t 0 file "status")
125 (goto-char (point-min))
126 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
127 (match-string 1))))
128
129(defun vc-mtn-workfile-unchanged-p (file)
130 (not (eq (vc-mtn-state file) 'edited)))
131
132;; Mode-line rewrite code copied from vc-arch.el.
133
134(defcustom vc-mtn-mode-line-rewrite
135 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
136 "Rewrite rules to shorten Mtn's revision names on the mode-line."
137 :type '(repeat (cons regexp string))
8e788369 138 :version "22.2"
b1dc6d44
SM
139 :group 'vc)
140
141(defun vc-mtn-mode-line-string (file)
142 "Return string for placement in modeline by `vc-mode-line' for FILE."
143 (let ((branch (vc-mtn-workfile-branch file)))
144 (dolist (rule vc-mtn-mode-line-rewrite)
145 (if (string-match (car rule) branch)
146 (setq branch (replace-match (cdr rule) t nil branch))))
147 (format "Mtn%c%s"
148 (case (vc-state file)
3702367b 149 ((up-to-date needs-update) ?-)
b1dc6d44
SM
150 (added ?@)
151 (t ?:))
152 branch)))
153
154(defun vc-mtn-register (files &optional rest)
155 (vc-mtn-command nil 0 files "add"))
156
157(defun vc-mtn-responsible-p (file) (vc-mtn-root file))
158(defun vc-mtn-could-register (file) (vc-mtn-root file))
159
160(defun vc-mtn-checkin (files rev comment)
161 (vc-mtn-command nil 0 files "commit" "-m" comment))
162
ac3f4c6f 163(defun vc-mtn-find-revision (file rev buffer)
b1dc6d44
SM
164 (vc-mtn-command buffer 0 file "cat" "-r" rev))
165
166;; (defun vc-mtn-checkout (file &optional editable rev)
167;; )
168
169(defun vc-mtn-revert (file &optional contents-done)
170 (unless contents-done
171 (vc-mtn-command nil 0 file "revert")))
172
173;; (defun vc-mtn-roolback (files)
174;; )
175
176(defun vc-mtn-print-log (files &optional buffer)
177 (vc-mtn-command buffer 0 files "log"))
178
97546017
DN
179(defvar log-view-message-re)
180(defvar log-view-file-re)
181(defvar log-view-font-lock-keywords)
469ca403 182(defvar log-view-per-file-logs)
97546017 183
b1dc6d44 184(define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
469ca403
DN
185 ;; Don't match anything.
186 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
187 (set (make-local-variable 'log-view-per-file-logs) nil)
b1dc6d44
SM
188 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
189 ;; in the ChangeLog text.
190 (set (make-local-variable 'log-view-message-re)
191 "^[ |/]+Revision: \\([0-9a-f]+\\)")
192 (require 'add-log) ;For change-log faces.
193 (set (make-local-variable 'log-view-font-lock-keywords)
194 (append log-view-font-lock-keywords
195 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
196 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
197
5b5afd50 198;; (defun vc-mtn-show-log-entry (revision)
b1dc6d44
SM
199;; )
200
b1dc6d44 201(defun vc-mtn-diff (files &optional rev1 rev2 buffer)
a857238c 202 "Get a difference report using monotone between two revisions of FILES."
b1dc6d44 203 (apply 'vc-mtn-command (or buffer "*vc-diff*") 1 files "diff"
a857238c 204 (append
39ba78ef 205 (vc-switches 'mtn 'diff)
a857238c 206 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
b1dc6d44
SM
207
208(defun vc-mtn-annotate-command (file buf &optional rev)
209 (apply 'vc-mtn-command buf 0 file "annotate"
210 (if rev (list "-r" rev))))
211
f8bd9ac6
DN
212(declare-function vc-annotate-convert-time "vc-annotate" (time))
213
b1dc6d44
SM
214(defconst vc-mtn-annotate-full-re
215 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
216(defconst vc-mtn-annotate-any-re
217 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
218
219(defun vc-mtn-annotate-time ()
220 (when (looking-at vc-mtn-annotate-any-re)
221 (goto-char (match-end 0))
222 (let ((year (match-string 2)))
223 (if (not year)
224 ;; Look for the date on a previous line.
225 (save-excursion
226 (get-text-property (1- (previous-single-property-change
227 (point) 'vc-mtn-time nil (point-min)))
228 'vc-mtn-time))
229 (let ((time (vc-annotate-convert-time
230 (encode-time 0 0 0
231 (string-to-number (match-string 4))
232 (string-to-number (match-string 3))
233 (string-to-number year)
234 t))))
235 (let ((inhibit-read-only t)
236 (inhibit-modification-hooks t))
237 (put-text-property (match-beginning 0) (match-end 0)
238 'vc-mtn-time time))
239 time)))))
240
241(defun vc-mtn-annotate-extract-revision-at-line ()
242 (save-excursion
243 (when (or (looking-at vc-mtn-annotate-full-re)
244 (re-search-backward vc-mtn-annotate-full-re nil t))
245 (match-string 1))))
246
247;;; Revision completion.
248
249(defun vc-mtn-list-tags ()
250 (with-temp-buffer
251 (vc-mtn-command t 0 nil "list" "tags")
252 (goto-char (point-min))
253 (let ((tags ()))
254 (while (re-search-forward "^[^ ]+" nil t)
255 (push (match-string 0) tags))
256 tags)))
257
258(defun vc-mtn-list-branches ()
259 (with-temp-buffer
260 (vc-mtn-command t 0 nil "list" "branches")
261 (goto-char (point-min))
262 (let ((branches ()))
263 (while (re-search-forward "^.+" nil t)
264 (push (match-string 0) branches))
265 branches)))
266
267(defun vc-mtn-list-revision-ids (prefix)
268 (with-temp-buffer
269 (vc-mtn-command t 0 nil "complete" "revision" prefix)
270 (goto-char (point-min))
271 (let ((ids ()))
272 (while (re-search-forward "^.+" nil t)
273 (push (match-string 0) ids))
274 ids)))
275
844b90ae 276(defun vc-mtn-revision-completion-table (files)
b1dc6d44
SM
277 ;; TODO: Implement completion for for selectors
278 ;; TODO: Implement completion for composite selectors.
844b90ae
SM
279 (lexical-let ((files files))
280 ;; What about using `files'?!? --Stef
b1dc6d44
SM
281 (lambda (string pred action)
282 (cond
283 ;; "Tag" selectors.
284 ((string-match "\\`t:" string)
285 (complete-with-action action
286 (mapcar (lambda (tag) (concat "t:" tag))
287 (vc-mtn-list-tags))
288 string pred))
289 ;; "Branch" selectors.
290 ((string-match "\\`b:" string)
291 (complete-with-action action
292 (mapcar (lambda (tag) (concat "b:" tag))
293 (vc-mtn-list-branches))
294 string pred))
295 ;; "Head" selectors. Not sure how they differ from "branch" selectors.
296 ((string-match "\\`h:" string)
297 (complete-with-action action
298 (mapcar (lambda (tag) (concat "h:" tag))
299 (vc-mtn-list-branches))
300 string pred))
301 ;; "ID" selectors.
302 ((string-match "\\`i:" string)
303 (complete-with-action action
304 (mapcar (lambda (tag) (concat "i:" tag))
305 (vc-mtn-list-revision-ids
306 (substring string (match-end 0))))
307 string pred))
308 (t
309 (complete-with-action action
310 '("t:" "b:" "h:" "i:"
311 ;; Completion not implemented for these.
312 "a:" "c:" "d:" "e:" "l:")
313 string pred))))))
eb3fa2cf
GM
314
315
b1dc6d44
SM
316
317(provide 'vc-mtn)
318
319;; arch-tag: 2b89ffbc-cbb8-405a-9080-2eafd4becb70
320;;; vc-mtn.el ends here