* progmodes/meta-mode.el (meta-complete-symbol):
[emacs.git] / lisp / vc-mtn.el
blob05be5c8ef11617c9e26ea31b8d989c2070820cf7
1 ;;; vc-mtn.el --- VC backend for Monotone
3 ;; Copyright (C) 2007, 2008, 2009 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 ;;; TODO:
29 ;; - The `previous-version' VC method needs to be supported, 'D' in
30 ;; log-view-mode uses it.
32 ;;; Code:
34 (eval-when-compile (require 'cl) (require 'vc))
36 (defcustom vc-mtn-diff-switches t
37 "String or list of strings specifying switches for monotone diff under VC.
38 If nil, use the value of `vc-diff-switches'. If t, use no switches."
39 :type '(choice (const :tag "Unspecified" nil)
40 (const :tag "None" t)
41 (string :tag "Argument String")
42 (repeat :tag "Argument List" :value ("") string))
43 :version "23.1"
44 :group 'vc)
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)
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)
56 (unless (executable-find vc-mtn-program)
57 ;; vc-mtn.el is 100% non-functional without the `mtn' executable.
58 (setq vc-handled-backends (delq 'Mtn vc-handled-backends)))
60 ;;;###autoload
61 (defconst vc-mtn-admin-dir "_MTN")
62 ;;;###autoload
63 (defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format"))
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))))
71 (defun vc-mtn-revision-granularity () 'repository)
72 (defun vc-mtn-checkout-model (files) 'implicit)
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))))
83 (defun vc-mtn-registered (file)
84 (let ((root (vc-mtn-root file)))
85 (when root
86 (vc-mtn-state file))))
88 (defun vc-mtn-command (buffer okstatus files &rest flags)
89 "A wrapper around `vc-do-command' for use in vc-mtn.el."
90 (let ((process-environment
91 ;; Avoid localization of messages so we can parse the output.
92 (cons "LC_MESSAGES=C" process-environment)))
93 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program
94 files flags)))
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))
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)))))
109 (defun vc-mtn-after-dir-status (update-function)
110 (let (result)
111 (goto-char (point-min))
112 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)" nil t)
113 (while (re-search-forward
114 "^ \\(?:\\(patched \\)\\|\\(added \\)\\)\\(.*\\)$" nil t)
115 (cond ((match-end 1) (push (list (match-string 3) 'edited) result))
116 ((match-end 2) (push (list (match-string 3) 'added) result))))
117 (funcall update-function result)))
119 (defun vc-mtn-dir-status (dir update-function)
120 (vc-mtn-command (current-buffer) 'async dir "status")
121 (vc-exec-after
122 `(vc-mtn-after-dir-status (quote ,update-function))))
124 (defun vc-mtn-working-revision (file)
125 ;; If `mtn' fails or returns status>0, or if the search fails, just
126 ;; return nil.
127 (ignore-errors
128 (with-temp-buffer
129 (vc-mtn-command t 0 file "status")
130 (goto-char (point-min))
131 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
132 (match-string 2))))
134 (defun vc-mtn-workfile-branch (file)
135 ;; If `mtn' fails or returns status>0, or if the search files, just
136 ;; return nil.
137 (ignore-errors
138 (with-temp-buffer
139 (vc-mtn-command t 0 file "status")
140 (goto-char (point-min))
141 (re-search-forward "Current branch: \\(.*\\)\nChanges against parent \\(.*\\)")
142 (match-string 1))))
144 (defun vc-mtn-workfile-unchanged-p (file)
145 (not (eq (vc-mtn-state file) 'edited)))
147 ;; Mode-line rewrite code copied from vc-arch.el.
149 (defcustom vc-mtn-mode-line-rewrite
150 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
151 "Rewrite rules to shorten Mtn's revision names on the mode-line."
152 :type '(repeat (cons regexp string))
153 :version "22.2"
154 :group 'vc)
156 (defun vc-mtn-mode-line-string (file)
157 "Return string for placement in modeline by `vc-mode-line' for FILE."
158 (let ((branch (vc-mtn-workfile-branch file)))
159 (dolist (rule vc-mtn-mode-line-rewrite)
160 (if (string-match (car rule) branch)
161 (setq branch (replace-match (cdr rule) t nil branch))))
162 (format "Mtn%c%s"
163 (case (vc-state file)
164 ((up-to-date needs-update) ?-)
165 (added ?@)
166 (t ?:))
167 branch)))
169 (defun vc-mtn-register (files &optional rev comment)
170 (vc-mtn-command nil 0 files "add"))
172 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
173 (defun vc-mtn-could-register (file) (vc-mtn-root file))
175 (defun vc-mtn-checkin (files rev comment)
176 (vc-mtn-command nil 0 files "commit" "-m" comment))
178 (defun vc-mtn-find-revision (file rev buffer)
179 (vc-mtn-command buffer 0 file "cat" "-r" rev))
181 ;; (defun vc-mtn-checkout (file &optional editable rev)
182 ;; )
184 (defun vc-mtn-revert (file &optional contents-done)
185 (unless contents-done
186 (vc-mtn-command nil 0 file "revert")))
188 ;; (defun vc-mtn-roolback (files)
189 ;; )
191 (defun vc-mtn-print-log (files buffer &optional shortlog limit)
192 (apply 'vc-mtn-command buffer 0 files "log"
193 (when limit (list "--last" (format "%s" limit)))))
195 (defvar log-view-message-re)
196 (defvar log-view-file-re)
197 (defvar log-view-font-lock-keywords)
198 (defvar log-view-per-file-logs)
200 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
201 ;; Don't match anything.
202 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
203 (set (make-local-variable 'log-view-per-file-logs) nil)
204 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
205 ;; in the ChangeLog text.
206 (set (make-local-variable 'log-view-message-re)
207 "^[ |/]+Revision: \\([0-9a-f]+\\)")
208 (require 'add-log) ;For change-log faces.
209 (set (make-local-variable 'log-view-font-lock-keywords)
210 (append log-view-font-lock-keywords
211 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
212 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
214 ;; (defun vc-mtn-show-log-entry (revision)
215 ;; )
217 (defun vc-mtn-diff (files &optional rev1 rev2 buffer)
218 "Get a difference report using monotone between two revisions of FILES."
219 (apply 'vc-mtn-command (or buffer "*vc-diff*") 1 files "diff"
220 (append
221 (vc-switches 'mtn 'diff)
222 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
224 (defun vc-mtn-annotate-command (file buf &optional rev)
225 (apply 'vc-mtn-command buf 'async file "annotate"
226 (if rev (list "-r" rev))))
228 (declare-function vc-annotate-convert-time "vc-annotate" (time))
230 (defconst vc-mtn-annotate-full-re
231 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
232 (defconst vc-mtn-annotate-any-re
233 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
235 (defun vc-mtn-annotate-time ()
236 (when (looking-at vc-mtn-annotate-any-re)
237 (goto-char (match-end 0))
238 (let ((year (match-string 2)))
239 (if (not year)
240 ;; Look for the date on a previous line.
241 (save-excursion
242 (get-text-property (1- (previous-single-property-change
243 (point) 'vc-mtn-time nil (point-min)))
244 'vc-mtn-time))
245 (let ((time (vc-annotate-convert-time
246 (encode-time 0 0 0
247 (string-to-number (match-string 4))
248 (string-to-number (match-string 3))
249 (string-to-number year)
250 t))))
251 (let ((inhibit-read-only t)
252 (inhibit-modification-hooks t))
253 (put-text-property (match-beginning 0) (match-end 0)
254 'vc-mtn-time time))
255 time)))))
257 (defun vc-mtn-annotate-extract-revision-at-line ()
258 (save-excursion
259 (when (or (looking-at vc-mtn-annotate-full-re)
260 (re-search-backward vc-mtn-annotate-full-re nil t))
261 (match-string 1))))
263 ;;; Revision completion.
265 (defun vc-mtn-list-tags ()
266 (with-temp-buffer
267 (vc-mtn-command t 0 nil "list" "tags")
268 (goto-char (point-min))
269 (let ((tags ()))
270 (while (re-search-forward "^[^ ]+" nil t)
271 (push (match-string 0) tags))
272 tags)))
274 (defun vc-mtn-list-branches ()
275 (with-temp-buffer
276 (vc-mtn-command t 0 nil "list" "branches")
277 (goto-char (point-min))
278 (let ((branches ()))
279 (while (re-search-forward "^.+" nil t)
280 (push (match-string 0) branches))
281 branches)))
283 (defun vc-mtn-list-revision-ids (prefix)
284 (with-temp-buffer
285 (vc-mtn-command t 0 nil "complete" "revision" prefix)
286 (goto-char (point-min))
287 (let ((ids ()))
288 (while (re-search-forward "^.+" nil t)
289 (push (match-string 0) ids))
290 ids)))
292 (defun vc-mtn-revision-completion-table (files)
293 ;; TODO: Implement completion for for selectors
294 ;; TODO: Implement completion for composite selectors.
295 (lexical-let ((files files))
296 ;; What about using `files'?!? --Stef
297 (lambda (string pred action)
298 (cond
299 ;; "Tag" selectors.
300 ((string-match "\\`t:" string)
301 (complete-with-action action
302 (mapcar (lambda (tag) (concat "t:" tag))
303 (vc-mtn-list-tags))
304 string pred))
305 ;; "Branch" selectors.
306 ((string-match "\\`b:" string)
307 (complete-with-action action
308 (mapcar (lambda (tag) (concat "b:" tag))
309 (vc-mtn-list-branches))
310 string pred))
311 ;; "Head" selectors. Not sure how they differ from "branch" selectors.
312 ((string-match "\\`h:" string)
313 (complete-with-action action
314 (mapcar (lambda (tag) (concat "h:" tag))
315 (vc-mtn-list-branches))
316 string pred))
317 ;; "ID" selectors.
318 ((string-match "\\`i:" string)
319 (complete-with-action action
320 (mapcar (lambda (tag) (concat "i:" tag))
321 (vc-mtn-list-revision-ids
322 (substring string (match-end 0))))
323 string pred))
325 (complete-with-action action
326 '("t:" "b:" "h:" "i:"
327 ;; Completion not implemented for these.
328 "a:" "c:" "d:" "e:" "l:")
329 string pred))))))
333 (provide 'vc-mtn)
335 ;; arch-tag: 2b89ffbc-cbb8-405a-9080-2eafd4becb70
336 ;;; vc-mtn.el ends here