Re-indent.
[emacs.git] / lisp / vc-mtn.el
blob6a7f72db44bdbcab34e890494e99f07c062c0eaf
1 ;;; vc-mtn.el --- VC backend for Monotone
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords:
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;;
27 ;;; Code:
29 (eval-when-compile (require 'cl) (require 'vc))
31 ;; Clear up the cache to force vc-call to check again and discover
32 ;; new functions when we reload this file.
33 (put 'Mtn 'vc-functions nil)
35 (defvar vc-mtn-command "mtn")
36 (unless (executable-find vc-mtn-command)
37 ;; vc-mtn.el is 100% non-functional without the `mtn' executable.
38 (setq vc-handled-backends (delq 'Mtn vc-handled-backends)))
40 ;;;###autoload
41 (defconst vc-mtn-admin-dir "_MTN")
42 ;;;###autoload
43 (defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format"))
45 ;;;###autoload (defun vc-mtn-registered (file)
46 ;;;###autoload (if (vc-find-root file vc-mtn-admin-format)
47 ;;;###autoload (progn
48 ;;;###autoload (load "vc-mtn")
49 ;;;###autoload (vc-mtn-registered file))))
51 (defun vc-mtn-revision-granularity () 'repository)
52 (defun vc-mtn-checkout-model (file) 'implicit)
54 (defun vc-mtn-root (file)
55 (setq file (if (file-directory-p file)
56 (file-name-as-directory file)
57 (file-name-directory file)))
58 (or (vc-file-getprop file 'vc-mtn-root)
59 (vc-file-setprop file 'vc-mtn-root
60 (vc-find-root file vc-mtn-admin-format))))
63 (defun vc-mtn-registered (file)
64 (let ((root (vc-mtn-root file)))
65 (when root
66 (vc-mtn-state file))))
68 (defun vc-mtn-command (buffer okstatus files &rest flags)
69 "A wrapper around `vc-do-command' for use in vc-mtn.el."
70 (apply 'vc-do-command buffer okstatus vc-mtn-command files flags))
72 (defun vc-mtn-state (file)
73 ;; If `mtn' fails or returns status>0, or if the search files, just
74 ;; return nil.
75 (ignore-errors
76 (with-temp-buffer
77 (vc-mtn-command t 0 file "status")
78 (goto-char (point-min))
79 (re-search-forward "^ \\(?:patched \\(.*\\)\\|no changes$\\)")
80 (if (match-end 1)
81 'edited
82 'up-to-date))))
84 (defun vc-mtn-working-revision (file)
85 ;; If `mtn' fails or returns status>0, or if the search fails, just
86 ;; return nil.
87 (ignore-errors
88 (with-temp-buffer
89 (vc-mtn-command t 0 file "status")
90 (goto-char (point-min))
91 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
92 (match-string 2))))
94 (defun vc-mtn-workfile-branch (file)
95 ;; If `mtn' fails or returns status>0, or if the search files, just
96 ;; return nil.
97 (ignore-errors
98 (with-temp-buffer
99 (vc-mtn-command t 0 file "status")
100 (goto-char (point-min))
101 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
102 (match-string 1))))
104 (defun vc-mtn-workfile-unchanged-p (file)
105 (not (eq (vc-mtn-state file) 'edited)))
107 ;; Mode-line rewrite code copied from vc-arch.el.
109 (defcustom vc-mtn-mode-line-rewrite
110 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
111 "Rewrite rules to shorten Mtn's revision names on the mode-line."
112 :type '(repeat (cons regexp string))
113 :version "22.2"
114 :group 'vc)
116 (defun vc-mtn-mode-line-string (file)
117 "Return string for placement in modeline by `vc-mode-line' for FILE."
118 (let ((branch (vc-mtn-workfile-branch file)))
119 (dolist (rule vc-mtn-mode-line-rewrite)
120 (if (string-match (car rule) branch)
121 (setq branch (replace-match (cdr rule) t nil branch))))
122 (format "Mtn%c%s"
123 (case (vc-state file)
124 ((up-to-date needs-patch) ?-)
125 (added ?@)
126 (t ?:))
127 branch)))
129 (defun vc-mtn-register (files &optional rest)
130 (vc-mtn-command nil 0 files "add"))
132 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
133 (defun vc-mtn-could-register (file) (vc-mtn-root file))
135 (defun vc-mtn-checkin (files rev comment)
136 (vc-mtn-command nil 0 files "commit" "-m" comment))
138 (defun vc-mtn-find-revision (file rev buffer)
139 (vc-mtn-command buffer 0 file "cat" "-r" rev))
141 ;; (defun vc-mtn-checkout (file &optional editable rev)
142 ;; )
144 (defun vc-mtn-revert (file &optional contents-done)
145 (unless contents-done
146 (vc-mtn-command nil 0 file "revert")))
148 ;; (defun vc-mtn-roolback (files)
149 ;; )
151 (defun vc-mtn-print-log (files &optional buffer)
152 (vc-mtn-command buffer 0 files "log"))
154 (defvar log-view-message-re)
155 (defvar log-view-file-re)
156 (defvar log-view-font-lock-keywords)
158 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
159 ;; TODO: Not sure what to do about file markers for now.
160 (set (make-local-variable 'log-view-file-re) "\\'\\`")
161 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
162 ;; in the ChangeLog text.
163 (set (make-local-variable 'log-view-message-re)
164 "^[ |/]+Revision: \\([0-9a-f]+\\)")
165 (require 'add-log) ;For change-log faces.
166 (set (make-local-variable 'log-view-font-lock-keywords)
167 (append log-view-font-lock-keywords
168 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
169 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
171 ;; (defun vc-mtn-show-log-entry (revision)
172 ;; )
174 (defun vc-mtn-wash-log (file))
176 (defun vc-mtn-diff (files &optional rev1 rev2 buffer)
177 (apply 'vc-mtn-command (or buffer "*vc-diff*") 1 files "diff"
178 (append (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
180 (defun vc-mtn-annotate-command (file buf &optional rev)
181 (apply 'vc-mtn-command buf 0 file "annotate"
182 (if rev (list "-r" rev))))
184 (defconst vc-mtn-annotate-full-re
185 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
186 (defconst vc-mtn-annotate-any-re
187 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
189 (defun vc-mtn-annotate-time ()
190 (when (looking-at vc-mtn-annotate-any-re)
191 (goto-char (match-end 0))
192 (let ((year (match-string 2)))
193 (if (not year)
194 ;; Look for the date on a previous line.
195 (save-excursion
196 (get-text-property (1- (previous-single-property-change
197 (point) 'vc-mtn-time nil (point-min)))
198 'vc-mtn-time))
199 (let ((time (vc-annotate-convert-time
200 (encode-time 0 0 0
201 (string-to-number (match-string 4))
202 (string-to-number (match-string 3))
203 (string-to-number year)
204 t))))
205 (let ((inhibit-read-only t)
206 (inhibit-modification-hooks t))
207 (put-text-property (match-beginning 0) (match-end 0)
208 'vc-mtn-time time))
209 time)))))
211 (defun vc-mtn-annotate-extract-revision-at-line ()
212 (save-excursion
213 (when (or (looking-at vc-mtn-annotate-full-re)
214 (re-search-backward vc-mtn-annotate-full-re nil t))
215 (match-string 1))))
217 ;;; Revision completion.
219 (defun vc-mtn-list-tags ()
220 (with-temp-buffer
221 (vc-mtn-command t 0 nil "list" "tags")
222 (goto-char (point-min))
223 (let ((tags ()))
224 (while (re-search-forward "^[^ ]+" nil t)
225 (push (match-string 0) tags))
226 tags)))
228 (defun vc-mtn-list-branches ()
229 (with-temp-buffer
230 (vc-mtn-command t 0 nil "list" "branches")
231 (goto-char (point-min))
232 (let ((branches ()))
233 (while (re-search-forward "^.+" nil t)
234 (push (match-string 0) branches))
235 branches)))
237 (defun vc-mtn-list-revision-ids (prefix)
238 (with-temp-buffer
239 (vc-mtn-command t 0 nil "complete" "revision" prefix)
240 (goto-char (point-min))
241 (let ((ids ()))
242 (while (re-search-forward "^.+" nil t)
243 (push (match-string 0) ids))
244 ids)))
246 (defun vc-mtn-revision-completion-table (files)
247 ;; TODO: Implement completion for for selectors
248 ;; TODO: Implement completion for composite selectors.
249 (lexical-let ((files files))
250 ;; What about using `files'?!? --Stef
251 (lambda (string pred action)
252 (cond
253 ;; "Tag" selectors.
254 ((string-match "\\`t:" string)
255 (complete-with-action action
256 (mapcar (lambda (tag) (concat "t:" tag))
257 (vc-mtn-list-tags))
258 string pred))
259 ;; "Branch" selectors.
260 ((string-match "\\`b:" string)
261 (complete-with-action action
262 (mapcar (lambda (tag) (concat "b:" tag))
263 (vc-mtn-list-branches))
264 string pred))
265 ;; "Head" selectors. Not sure how they differ from "branch" selectors.
266 ((string-match "\\`h:" string)
267 (complete-with-action action
268 (mapcar (lambda (tag) (concat "h:" tag))
269 (vc-mtn-list-branches))
270 string pred))
271 ;; "ID" selectors.
272 ((string-match "\\`i:" string)
273 (complete-with-action action
274 (mapcar (lambda (tag) (concat "i:" tag))
275 (vc-mtn-list-revision-ids
276 (substring string (match-end 0))))
277 string pred))
279 (complete-with-action action
280 '("t:" "b:" "h:" "i:"
281 ;; Completion not implemented for these.
282 "a:" "c:" "d:" "e:" "l:")
283 string pred))))))
287 (provide 'vc-mtn)
289 ;; arch-tag: 2b89ffbc-cbb8-405a-9080-2eafd4becb70
290 ;;; vc-mtn.el ends here