Fix minor bug introduced in 'Terminate vc-disable-async-diff'
[emacs.git] / lisp / vc / vc-mtn.el
blob85aaf3dc5428067b76a90002fa46717301537003
1 ;;; vc-mtn.el --- VC backend for Monotone -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: vc
7 ;; Package: vc
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
28 ;;; TODO:
30 ;; - The `previous-version' VC method needs to be supported, 'D' in
31 ;; log-view-mode uses it.
33 ;;; Code:
35 (eval-when-compile (require 'vc))
37 (defgroup vc-mtn nil
38 "VC Monotone (mtn) backend."
39 :version "24.1"
40 :group 'vc)
42 (defcustom vc-mtn-diff-switches t
43 "String or list of strings specifying switches for monotone diff under VC.
44 If nil, use the value of `vc-diff-switches'. If t, use no switches."
45 :type '(choice (const :tag "Unspecified" nil)
46 (const :tag "None" t)
47 (string :tag "Argument String")
48 (repeat :tag "Argument List" :value ("") string))
49 :version "23.1"
50 :group 'vc-mtn)
52 (define-obsolete-variable-alias 'vc-mtn-command 'vc-mtn-program "23.1")
53 (defcustom vc-mtn-program "mtn"
54 "Name of the monotone executable."
55 :type 'string
56 :group 'vc-mtn)
58 ;; Clear up the cache to force vc-call to check again and discover
59 ;; new functions when we reload this file.
60 (put 'Mtn 'vc-functions nil)
62 (unless (executable-find vc-mtn-program)
63 ;; vc-mtn.el is 100% non-functional without the `mtn' executable.
64 (setq vc-handled-backends (delq 'Mtn vc-handled-backends)))
66 ;;;###autoload
67 (defconst vc-mtn-admin-dir "_MTN" "Name of the monotone directory.")
68 ;;;###autoload
69 (defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format")
70 "Name of the monotone directory's format file.")
72 ;;;###autoload (defun vc-mtn-registered (file)
73 ;;;###autoload (if (vc-find-root file vc-mtn-admin-format)
74 ;;;###autoload (progn
75 ;;;###autoload (load "vc-mtn" nil t)
76 ;;;###autoload (vc-mtn-registered file))))
78 (defun vc-mtn-revision-granularity () 'repository)
79 (defun vc-mtn-checkout-model (_files) 'implicit)
81 (defun vc-mtn-root (file)
82 (setq file (if (file-directory-p file)
83 (file-name-as-directory file)
84 (file-name-directory file)))
85 (or (vc-file-getprop file 'vc-mtn-root)
86 (vc-file-setprop file 'vc-mtn-root
87 (vc-find-root file vc-mtn-admin-format))))
89 (defun vc-mtn-find-admin-dir (file)
90 "Return the administrative directory of FILE."
91 (expand-file-name vc-mtn-admin-dir (vc-mtn-root file)))
93 (defun vc-mtn-registered (file)
94 (let ((root (vc-mtn-root file)))
95 (when root
96 (vc-mtn-state file))))
98 (defun vc-mtn-command (buffer okstatus files &rest flags)
99 "A wrapper around `vc-do-command' for use in vc-mtn.el."
100 (let ((process-environment
101 ;; Avoid localization of messages so we can parse the output.
102 (cons "LC_MESSAGES=C" process-environment)))
103 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program
104 files flags)))
106 (defun vc-mtn-state (file)
107 ;; If `mtn' fails or returns status>0, or if the search files, just
108 ;; return nil.
109 (ignore-errors
110 (with-temp-buffer
111 (vc-mtn-command t 0 file "status")
112 (goto-char (point-min))
113 (re-search-forward
114 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
115 (cond ((match-end 1) 'edited)
116 ((match-end 2) 'added)
117 (t 'up-to-date)))))
119 (defun vc-mtn-after-dir-status (update-function)
120 (let (result)
121 (goto-char (point-min))
122 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)" nil t)
123 (while (re-search-forward
124 "^ \\(?:\\(patched \\)\\|\\(added \\)\\)\\(.*\\)$" nil t)
125 (cond ((match-end 1) (push (list (match-string 3) 'edited) result))
126 ((match-end 2) (push (list (match-string 3) 'added) result))))
127 (funcall update-function result)))
129 ;; -dir-status called from vc-dir, which loads vc, which loads vc-dispatcher.
130 (declare-function vc-exec-after "vc-dispatcher" (code))
132 (defun vc-mtn-dir-status (dir update-function)
133 (vc-mtn-command (current-buffer) 'async dir "status")
134 (vc-run-delayed
135 (vc-mtn-after-dir-status update-function)))
137 (defun vc-mtn-working-revision (file)
138 ;; If `mtn' fails or returns status>0, or if the search fails, just
139 ;; return nil.
140 (ignore-errors
141 (with-temp-buffer
142 (vc-mtn-command t 0 file "status")
143 (goto-char (point-min))
144 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
145 (match-string 2))))
147 (defun vc-mtn-workfile-branch (file)
148 ;; If `mtn' fails or returns status>0, or if the search files, just
149 ;; return nil.
150 (ignore-errors
151 (with-temp-buffer
152 (vc-mtn-command t 0 file "status")
153 (goto-char (point-min))
154 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
155 (match-string 1))))
157 ;; Mode-line rewrite code copied from vc-arch.el.
159 (defcustom vc-mtn-mode-line-rewrite
160 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
161 "Rewrite rules to shorten Mtn's revision names on the mode-line."
162 :type '(repeat (cons regexp string))
163 :version "22.2"
164 :group 'vc-mtn)
166 (defun vc-mtn-mode-line-string (file)
167 "Return a string for `vc-mode-line' to put in the mode line for FILE."
168 (let ((branch (vc-mtn-workfile-branch file)))
169 (dolist (rule vc-mtn-mode-line-rewrite)
170 (if (string-match (car rule) branch)
171 (setq branch (replace-match (cdr rule) t nil branch))))
172 (format "Mtn%c%s"
173 (pcase (vc-state file)
174 ((or `up-to-date `needs-update) ?-)
175 (`added ?@)
176 (_ ?:))
177 branch)))
179 (defun vc-mtn-register (files &optional _comment)
180 (vc-mtn-command nil 0 files "add"))
182 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
184 (declare-function log-edit-extract-headers "log-edit" (headers string))
186 (defun vc-mtn-checkin (files comment)
187 (apply 'vc-mtn-command nil 0 files
188 (nconc (list "commit" "-m")
189 (log-edit-extract-headers '(("Author" . "--author")
190 ("Date" . "--date"))
191 comment))))
193 (defun vc-mtn-find-revision (file rev buffer)
194 (vc-mtn-command buffer 0 file "cat" "-r" rev))
196 ;; (defun vc-mtn-checkout (file &optional rev)
197 ;; )
199 (defun vc-mtn-revert (file &optional contents-done)
200 (unless contents-done
201 (vc-mtn-command nil 0 file "revert")))
203 ;; (defun vc-mtn-rollback (files)
204 ;; )
206 (defun vc-mtn-print-log (files buffer &optional _shortlog start-revision limit)
207 "Print commit logs associated with FILES into specified BUFFER.
208 _SHORTLOG is ignored.
209 If START-REVISION is non-nil, it is the newest revision to show.
210 If LIMIT is non-nil, show no more than this many entries."
211 (apply 'vc-mtn-command buffer 0 files "log"
212 (append
213 (when start-revision (list "--from" (format "%s" start-revision)))
214 (when limit (list "--last" (format "%s" limit))))))
216 (defvar log-view-message-re)
217 (defvar log-view-file-re)
218 (defvar log-view-font-lock-keywords)
219 (defvar log-view-per-file-logs)
221 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
222 ;; Don't match anything.
223 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
224 (set (make-local-variable 'log-view-per-file-logs) nil)
225 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
226 ;; in the ChangeLog text.
227 (set (make-local-variable 'log-view-message-re)
228 "^[ |/]+Revision: \\([0-9a-f]+\\)")
229 (require 'add-log) ;For change-log faces.
230 (set (make-local-variable 'log-view-font-lock-keywords)
231 (append log-view-font-lock-keywords
232 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
233 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
235 ;; (defun vc-mtn-show-log-entry (revision)
236 ;; )
238 (autoload 'vc-switches "vc")
240 (defun vc-mtn-diff (files &optional async rev1 rev2 buffer)
241 "Get a difference report using monotone between two revisions of FILES."
242 (apply 'vc-mtn-command (or buffer "*vc-diff*")
243 (if async 'async 1)
244 files "diff"
245 (append
246 (vc-switches 'mtn 'diff)
247 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
249 (defun vc-mtn-annotate-command (file buf &optional rev)
250 (apply 'vc-mtn-command buf 'async file "annotate"
251 (if rev (list "-r" rev))))
253 (declare-function vc-annotate-convert-time "vc-annotate" (time))
255 (defconst vc-mtn-annotate-full-re
256 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
257 (defconst vc-mtn-annotate-any-re
258 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
260 (defun vc-mtn-annotate-time ()
261 (when (looking-at vc-mtn-annotate-any-re)
262 (goto-char (match-end 0))
263 (let ((year (match-string 2)))
264 (if (not year)
265 ;; Look for the date on a previous line.
266 (save-excursion
267 (get-text-property (1- (previous-single-property-change
268 (point) 'vc-mtn-time nil (point-min)))
269 'vc-mtn-time))
270 (let ((time (vc-annotate-convert-time
271 (encode-time 0 0 0
272 (string-to-number (match-string 4))
273 (string-to-number (match-string 3))
274 (string-to-number year)
275 t))))
276 (let ((inhibit-read-only t)
277 (inhibit-modification-hooks t))
278 (put-text-property (match-beginning 0) (match-end 0)
279 'vc-mtn-time time))
280 time)))))
282 (defun vc-mtn-annotate-extract-revision-at-line ()
283 (save-excursion
284 (when (or (looking-at vc-mtn-annotate-full-re)
285 (re-search-backward vc-mtn-annotate-full-re nil t))
286 (match-string 1))))
288 ;;; Revision completion.
290 (defun vc-mtn-list-tags ()
291 (with-temp-buffer
292 (vc-mtn-command t 0 nil "list" "tags")
293 (goto-char (point-min))
294 (let ((tags ()))
295 (while (re-search-forward "^[^ ]+" nil t)
296 (push (match-string 0) tags))
297 tags)))
299 (defun vc-mtn-list-branches ()
300 (with-temp-buffer
301 (vc-mtn-command t 0 nil "list" "branches")
302 (goto-char (point-min))
303 (let ((branches ()))
304 (while (re-search-forward "^.+" nil t)
305 (push (match-string 0) branches))
306 branches)))
308 (defun vc-mtn-list-revision-ids (prefix)
309 (with-temp-buffer
310 (vc-mtn-command t 0 nil "complete" "revision" prefix)
311 (goto-char (point-min))
312 (let ((ids ()))
313 (while (re-search-forward "^.+" nil t)
314 (push (match-string 0) ids))
315 ids)))
317 (defun vc-mtn-revision-completion-table (_files)
318 ;; What about using `files'?!? --Stef
319 (lambda (string pred action)
320 (cond
321 ;; Special chars for composite selectors.
322 ((string-match ".*[^\\]\\(\\\\\\\\\\)*[/|;(]" string)
323 (completion-table-with-context (substring string 0 (match-end 0))
324 (vc-mtn-revision-completion-table nil)
325 (substring string (match-end 0))
326 pred action))
327 ;; "Tag" selectors.
328 ((string-match "\\`t:" string)
329 (complete-with-action action
330 (mapcar (lambda (tag) (concat "t:" tag))
331 (vc-mtn-list-tags))
332 string pred))
333 ;; "Branch" or "Head" selectors.
334 ((string-match "\\`[hb]:" string)
335 (let ((prefix (match-string 0 string)))
336 (complete-with-action action
337 (mapcar (lambda (tag) (concat prefix tag))
338 (vc-mtn-list-branches))
339 string pred)))
340 ;; "ID" selectors.
341 ((string-match "\\`i:" string)
342 (complete-with-action action
343 (mapcar (lambda (tag) (concat "i:" tag))
344 (vc-mtn-list-revision-ids
345 (substring string (match-end 0))))
346 string pred))
348 (complete-with-action action
349 '("t:" "b:" "h:" "i:"
350 ;; Completion not implemented for these.
351 "c:" "a:" "k:" "d:" "m:" "e:" "l:" "i:" "p:"
352 ;; These have no arg to complete.
353 "u:" "w:"
354 ;; Selector functions.
355 "difference(" "lca(" "max(" "ancestors("
356 "descendants(" "parents(" "children("
357 "pick(")
358 string pred)))))
362 (provide 'vc-mtn)
364 ;;; vc-mtn.el ends here