(checkdoc-this-string-valid-engine):
[emacs.git] / lisp / vc-mtn.el
blob4a415bbf34a94ad7e56565fcd5e894871c76b75b
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 part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 (files) '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 (let ((process-environment
71 ;; Avoid localization of messages so we can parse the output.
72 (cons "LC_MESSAGES=C" process-environment)))
73 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-command files flags)))
75 (defun vc-mtn-state (file)
76 ;; If `mtn' fails or returns status>0, or if the search files, just
77 ;; return nil.
78 (ignore-errors
79 (with-temp-buffer
80 (vc-mtn-command t 0 file "status")
81 (goto-char (point-min))
82 (re-search-forward
83 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
84 (cond ((match-end 1) 'edited)
85 ((match-end 2) 'added)
86 (t 'up-to-date)))))
88 (defun vc-mtn-working-revision (file)
89 ;; If `mtn' fails or returns status>0, or if the search fails, just
90 ;; return nil.
91 (ignore-errors
92 (with-temp-buffer
93 (vc-mtn-command t 0 file "status")
94 (goto-char (point-min))
95 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
96 (match-string 2))))
98 (defun vc-mtn-workfile-branch (file)
99 ;; If `mtn' fails or returns status>0, or if the search files, just
100 ;; return nil.
101 (ignore-errors
102 (with-temp-buffer
103 (vc-mtn-command t 0 file "status")
104 (goto-char (point-min))
105 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
106 (match-string 1))))
108 (defun vc-mtn-workfile-unchanged-p (file)
109 (not (eq (vc-mtn-state file) 'edited)))
111 ;; Mode-line rewrite code copied from vc-arch.el.
113 (defcustom vc-mtn-mode-line-rewrite
114 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
115 "Rewrite rules to shorten Mtn's revision names on the mode-line."
116 :type '(repeat (cons regexp string))
117 :version "22.2"
118 :group 'vc)
120 (defun vc-mtn-mode-line-string (file)
121 "Return string for placement in modeline by `vc-mode-line' for FILE."
122 (let ((branch (vc-mtn-workfile-branch file)))
123 (dolist (rule vc-mtn-mode-line-rewrite)
124 (if (string-match (car rule) branch)
125 (setq branch (replace-match (cdr rule) t nil branch))))
126 (format "Mtn%c%s"
127 (case (vc-state file)
128 ((up-to-date needs-update) ?-)
129 (added ?@)
130 (t ?:))
131 branch)))
133 (defun vc-mtn-register (files &optional rest)
134 (vc-mtn-command nil 0 files "add"))
136 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
137 (defun vc-mtn-could-register (file) (vc-mtn-root file))
139 (defun vc-mtn-checkin (files rev comment)
140 (vc-mtn-command nil 0 files "commit" "-m" comment))
142 (defun vc-mtn-find-revision (file rev buffer)
143 (vc-mtn-command buffer 0 file "cat" "-r" rev))
145 ;; (defun vc-mtn-checkout (file &optional editable rev)
146 ;; )
148 (defun vc-mtn-revert (file &optional contents-done)
149 (unless contents-done
150 (vc-mtn-command nil 0 file "revert")))
152 ;; (defun vc-mtn-roolback (files)
153 ;; )
155 (defun vc-mtn-print-log (files &optional buffer)
156 (vc-mtn-command buffer 0 files "log"))
158 (defvar log-view-message-re)
159 (defvar log-view-file-re)
160 (defvar log-view-font-lock-keywords)
162 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
163 ;; TODO: Not sure what to do about file markers for now.
164 (set (make-local-variable 'log-view-file-re) "\\'\\`")
165 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
166 ;; in the ChangeLog text.
167 (set (make-local-variable 'log-view-message-re)
168 "^[ |/]+Revision: \\([0-9a-f]+\\)")
169 (require 'add-log) ;For change-log faces.
170 (set (make-local-variable 'log-view-font-lock-keywords)
171 (append log-view-font-lock-keywords
172 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
173 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
175 ;; (defun vc-mtn-show-log-entry (revision)
176 ;; )
178 (defun vc-mtn-diff (files &optional rev1 rev2 buffer)
179 (apply 'vc-mtn-command (or buffer "*vc-diff*") 1 files "diff"
180 (append (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
182 (defun vc-mtn-annotate-command (file buf &optional rev)
183 (apply 'vc-mtn-command buf 0 file "annotate"
184 (if rev (list "-r" rev))))
186 (defconst vc-mtn-annotate-full-re
187 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
188 (defconst vc-mtn-annotate-any-re
189 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
191 (defun vc-mtn-annotate-time ()
192 (when (looking-at vc-mtn-annotate-any-re)
193 (goto-char (match-end 0))
194 (let ((year (match-string 2)))
195 (if (not year)
196 ;; Look for the date on a previous line.
197 (save-excursion
198 (get-text-property (1- (previous-single-property-change
199 (point) 'vc-mtn-time nil (point-min)))
200 'vc-mtn-time))
201 (let ((time (vc-annotate-convert-time
202 (encode-time 0 0 0
203 (string-to-number (match-string 4))
204 (string-to-number (match-string 3))
205 (string-to-number year)
206 t))))
207 (let ((inhibit-read-only t)
208 (inhibit-modification-hooks t))
209 (put-text-property (match-beginning 0) (match-end 0)
210 'vc-mtn-time time))
211 time)))))
213 (defun vc-mtn-annotate-extract-revision-at-line ()
214 (save-excursion
215 (when (or (looking-at vc-mtn-annotate-full-re)
216 (re-search-backward vc-mtn-annotate-full-re nil t))
217 (match-string 1))))
219 ;;; Revision completion.
221 (defun vc-mtn-list-tags ()
222 (with-temp-buffer
223 (vc-mtn-command t 0 nil "list" "tags")
224 (goto-char (point-min))
225 (let ((tags ()))
226 (while (re-search-forward "^[^ ]+" nil t)
227 (push (match-string 0) tags))
228 tags)))
230 (defun vc-mtn-list-branches ()
231 (with-temp-buffer
232 (vc-mtn-command t 0 nil "list" "branches")
233 (goto-char (point-min))
234 (let ((branches ()))
235 (while (re-search-forward "^.+" nil t)
236 (push (match-string 0) branches))
237 branches)))
239 (defun vc-mtn-list-revision-ids (prefix)
240 (with-temp-buffer
241 (vc-mtn-command t 0 nil "complete" "revision" prefix)
242 (goto-char (point-min))
243 (let ((ids ()))
244 (while (re-search-forward "^.+" nil t)
245 (push (match-string 0) ids))
246 ids)))
248 (defun vc-mtn-revision-completion-table (files)
249 ;; TODO: Implement completion for for selectors
250 ;; TODO: Implement completion for composite selectors.
251 (lexical-let ((files files))
252 ;; What about using `files'?!? --Stef
253 (lambda (string pred action)
254 (cond
255 ;; "Tag" selectors.
256 ((string-match "\\`t:" string)
257 (complete-with-action action
258 (mapcar (lambda (tag) (concat "t:" tag))
259 (vc-mtn-list-tags))
260 string pred))
261 ;; "Branch" selectors.
262 ((string-match "\\`b:" string)
263 (complete-with-action action
264 (mapcar (lambda (tag) (concat "b:" tag))
265 (vc-mtn-list-branches))
266 string pred))
267 ;; "Head" selectors. Not sure how they differ from "branch" selectors.
268 ((string-match "\\`h:" string)
269 (complete-with-action action
270 (mapcar (lambda (tag) (concat "h:" tag))
271 (vc-mtn-list-branches))
272 string pred))
273 ;; "ID" selectors.
274 ((string-match "\\`i:" string)
275 (complete-with-action action
276 (mapcar (lambda (tag) (concat "i:" tag))
277 (vc-mtn-list-revision-ids
278 (substring string (match-end 0))))
279 string pred))
281 (complete-with-action action
282 '("t:" "b:" "h:" "i:"
283 ;; Completion not implemented for these.
284 "a:" "c:" "d:" "e:" "l:")
285 string pred))))))
289 (provide 'vc-mtn)
291 ;; arch-tag: 2b89ffbc-cbb8-405a-9080-2eafd4becb70
292 ;;; vc-mtn.el ends here