]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-hooks.el
Add faces for the VC modeline state indicator
[gnu-emacs] / lisp / vc / vc-hooks.el
1 ;;; vc-hooks.el --- resident support for version-control
2
3 ;; Copyright (C) 1992-1996, 1998-2014 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Package: vc
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; This is the always-loaded portion of VC. It takes care of
27 ;; VC-related activities that are done when you visit a file, so that
28 ;; vc.el itself is loaded only when you use a VC command. See the
29 ;; commentary of vc.el.
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl-lib))
34
35 ;; Faces
36
37 (defgroup vc-state-faces nil
38 "Faces used in the mode line by the VC state indicator."
39 :group 'vc-faces
40 :group 'mode-line
41 :version "25.1")
42
43 (defface vc-state-base-face
44 '((default :inherit mode-line))
45 "Base face for VC state indicator."
46 :group 'vc-faces
47 :group 'mode-line
48 :version "25.1")
49
50 (defface vc-up-to-date-state
51 '((default :inherit vc-state-base-face))
52 "Face for VC modeline state when the file is up to date."
53 :version "25.1"
54 :group 'vc-faces)
55
56 (defface vc-needs-update-state
57 '((default :inherit vc-state-base-face))
58 "Face for VC modeline state when the file needs update."
59 :version "25.1"
60 :group 'vc-faces)
61
62 (defface vc-locked-state
63 '((default :inherit vc-state-base-face))
64 "Face for VC modeline state when the file locked."
65 :version "25.1"
66 :group 'vc-faces)
67
68 (defface vc-locally-added-state
69 '((default :inherit vc-state-base-face))
70 "Face for VC modeline state when the file is locally added."
71 :version "25.1"
72 :group 'vc-faces)
73
74 (defface vc-conflict-state
75 '((default :inherit vc-state-base-face))
76 "Face for VC modeline state when the file contains merge conflicts."
77 :version "25.1"
78 :group 'vc-faces)
79
80 (defface vc-removed-state
81 '((default :inherit vc-state-base-face))
82 "Face for VC modeline state when the file was removed from the VC system."
83 :version "25.1"
84 :group 'vc-faces)
85
86 (defface vc-missing-state
87 '((default :inherit vc-state-base-face))
88 "Face for VC modeline state when the file is missing from the file system."
89 :version "25.1"
90 :group 'vc-faces)
91
92 (defface vc-edited-state
93 '((default :inherit vc-state-base-face))
94 "Face for VC modeline state when the file is up to date."
95 :version "25.1"
96 :group 'vc-faces)
97
98 ;; Customization Variables (the rest is in vc.el)
99
100 (defcustom vc-ignore-dir-regexp
101 ;; Stop SMB, automounter, AFS, and DFS host lookups.
102 locate-dominating-stop-dir-regexp
103 "Regexp matching directory names that are not under VC's control.
104 The default regexp prevents fruitless and time-consuming attempts
105 to determine the VC status in directories in which filenames are
106 interpreted as hostnames."
107 :type 'regexp
108 :group 'vc)
109
110 (defcustom vc-handled-backends '(RCS CVS SVN SCCS Bzr Git Hg Mtn Arch)
111 ;; RCS, CVS, SVN and SCCS come first because they are per-dir
112 ;; rather than per-tree. RCS comes first because of the multibackend
113 ;; support intended to use RCS for local commits (with a remote CVS server).
114 "List of version control backends for which VC will be used.
115 Entries in this list will be tried in order to determine whether a
116 file is under that sort of version control.
117 Removing an entry from the list prevents VC from being activated
118 when visiting a file managed by that backend.
119 An empty list disables VC altogether."
120 :type '(repeat symbol)
121 :version "23.1"
122 :group 'vc)
123
124 ;; Note: we don't actually have a darcs back end yet.
125 ;; Also, Meta-CVS (corresponding to MCVS) is unsupported.
126 (defcustom vc-directory-exclusion-list (purecopy '("SCCS" "RCS" "CVS" "MCVS"
127 ".svn" ".git" ".hg" ".bzr"
128 "_MTN" "_darcs" "{arch}"))
129 "List of directory names to be ignored when walking directory trees."
130 :type '(repeat string)
131 :group 'vc)
132
133 (defcustom vc-make-backup-files nil
134 "If non-nil, backups of registered files are made as with other files.
135 If nil (the default), files covered by version control don't get backups."
136 :type 'boolean
137 :group 'vc
138 :group 'backup)
139
140 (defcustom vc-follow-symlinks 'ask
141 "What to do if visiting a symbolic link to a file under version control.
142 Editing such a file through the link bypasses the version control system,
143 which is dangerous and probably not what you want.
144
145 If this variable is t, VC follows the link and visits the real file,
146 telling you about it in the echo area. If it is `ask', VC asks for
147 confirmation whether it should follow the link. If nil, the link is
148 visited and a warning displayed."
149 :type '(choice (const :tag "Ask for confirmation" ask)
150 (const :tag "Visit link and warn" nil)
151 (const :tag "Follow link" t))
152 :group 'vc)
153
154 (defcustom vc-display-status t
155 "If non-nil, display revision number and lock status in mode line.
156 Otherwise, not displayed."
157 :type 'boolean
158 :group 'vc)
159
160
161 (defcustom vc-consult-headers t
162 "If non-nil, identify work files by searching for version headers."
163 :type 'boolean
164 :group 'vc)
165
166 (defcustom vc-keep-workfiles t
167 "Whether to keep work files on disk after commits, on a locking VCS.
168 This variable has no effect on modern merging-based version
169 control systems."
170 :type 'boolean
171 :group 'vc)
172
173 ;; If you fix bug#11490, probably you can set this back to nil.
174 (defcustom vc-mistrust-permissions t
175 "If non-nil, don't assume permissions/ownership track version-control status.
176 If nil, do rely on the permissions.
177 See also variable `vc-consult-headers'."
178 :version "24.3" ; nil->t, bug#11490
179 :type 'boolean
180 :group 'vc)
181
182 (defun vc-mistrust-permissions (file)
183 "Internal access function to variable `vc-mistrust-permissions' for FILE."
184 (or (eq vc-mistrust-permissions 't)
185 (and vc-mistrust-permissions
186 (funcall vc-mistrust-permissions
187 (vc-backend-subdirectory-name file)))))
188
189 (defcustom vc-stay-local 'only-file
190 "Non-nil means use local operations when possible for remote repositories.
191 This avoids slow queries over the network and instead uses heuristics
192 and past information to determine the current status of a file.
193
194 If value is the symbol `only-file', `vc-dir' will connect to the
195 server, but heuristics will be used to determine the status for
196 all other VC operations.
197
198 The value can also be a regular expression or list of regular
199 expressions to match against the host name of a repository; then VC
200 only stays local for hosts that match it. Alternatively, the value
201 can be a list of regular expressions where the first element is the
202 symbol `except'; then VC always stays local except for hosts matched
203 by these regular expressions."
204 :type '(choice
205 (const :tag "Always stay local" t)
206 (const :tag "Only for file operations" only-file)
207 (const :tag "Don't stay local" nil)
208 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
209 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
210 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
211 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
212 :version "23.1"
213 :group 'vc)
214
215 (defun vc-stay-local-p (file &optional backend)
216 "Return non-nil if VC should stay local when handling FILE.
217 This uses the `repository-hostname' backend operation.
218 If FILE is a list of files, return non-nil if any of them
219 individually should stay local."
220 (if (listp file)
221 (delq nil (mapcar (lambda (arg) (vc-stay-local-p arg backend)) file))
222 (setq backend (or backend (vc-backend file)))
223 (let* ((sym (vc-make-backend-sym backend 'stay-local))
224 (stay-local (if (boundp sym) (symbol-value sym) vc-stay-local)))
225 (if (symbolp stay-local) stay-local
226 (let ((dirname (if (file-directory-p file)
227 (directory-file-name file)
228 (file-name-directory file))))
229 (eq 'yes
230 (or (vc-file-getprop dirname 'vc-stay-local-p)
231 (vc-file-setprop
232 dirname 'vc-stay-local-p
233 (let ((hostname (vc-call-backend
234 backend 'repository-hostname dirname)))
235 (if (not hostname)
236 'no
237 (let ((default t))
238 (if (eq (car-safe stay-local) 'except)
239 (setq default nil stay-local (cdr stay-local)))
240 (when (consp stay-local)
241 (setq stay-local
242 (mapconcat 'identity stay-local "\\|")))
243 (if (if (string-match stay-local hostname)
244 default (not default))
245 'yes 'no))))))))))))
246
247 ;;; This is handled specially now.
248 ;; Tell Emacs about this new kind of minor mode
249 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
250
251 ;;;###autoload
252 (put 'vc-mode 'risky-local-variable t)
253 (make-variable-buffer-local 'vc-mode)
254 (put 'vc-mode 'permanent-local t)
255
256 ;;; We signal this error when we try to do something a VC backend
257 ;;; doesn't support. Two arguments: the method that's not supported
258 ;;; and the backend
259 (define-error 'vc-not-supported "VC method not implemented for backend")
260
261 (defun vc-mode (&optional _arg)
262 ;; Dummy function for C-h m
263 "Version Control minor mode.
264 This minor mode is automatically activated whenever you visit a file under
265 control of one of the revision control systems in `vc-handled-backends'.
266 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
267 \\{vc-prefix-map}")
268
269 (defmacro vc-error-occurred (&rest body)
270 `(condition-case nil (progn ,@body nil) (error t)))
271
272 ;; We need a notion of per-file properties because the version
273 ;; control state of a file is expensive to derive --- we compute
274 ;; them when the file is initially found, keep them up to date
275 ;; during any subsequent VC operations, and forget them when
276 ;; the buffer is killed.
277
278 (defvar vc-file-prop-obarray (make-vector 17 0)
279 "Obarray for per-file properties.")
280
281 (defvar vc-touched-properties nil)
282
283 (defun vc-file-setprop (file property value)
284 "Set per-file VC PROPERTY for FILE to VALUE."
285 (if (and vc-touched-properties
286 (not (memq property vc-touched-properties)))
287 (setq vc-touched-properties (append (list property)
288 vc-touched-properties)))
289 (put (intern file vc-file-prop-obarray) property value))
290
291 (defun vc-file-getprop (file property)
292 "Get per-file VC PROPERTY for FILE."
293 (get (intern file vc-file-prop-obarray) property))
294
295 (defun vc-file-clearprops (file)
296 "Clear all VC properties of FILE."
297 (if (boundp 'vc-parent-buffer)
298 (kill-local-variable 'vc-parent-buffer))
299 (setplist (intern file vc-file-prop-obarray) nil))
300
301 \f
302 ;; We keep properties on each symbol naming a backend as follows:
303 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
304
305 (defun vc-make-backend-sym (backend sym)
306 "Return BACKEND-specific version of VC symbol SYM."
307 (intern (concat "vc-" (downcase (symbol-name backend))
308 "-" (symbol-name sym))))
309
310 (defun vc-find-backend-function (backend fun)
311 "Return BACKEND-specific implementation of FUN.
312 If there is no such implementation, return the default implementation;
313 if that doesn't exist either, return nil."
314 (let ((f (vc-make-backend-sym backend fun)))
315 (if (fboundp f) f
316 ;; Load vc-BACKEND.el if needed.
317 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
318 (if (fboundp f) f
319 (let ((def (vc-make-backend-sym 'default fun)))
320 (if (fboundp def) (cons def backend) nil))))))
321
322 (defun vc-call-backend (backend function-name &rest args)
323 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
324 Calls
325
326 (apply 'vc-BACKEND-FUN ARGS)
327
328 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
329 and else calls
330
331 (apply 'vc-default-FUN BACKEND ARGS)
332
333 It is usually called via the `vc-call' macro."
334 (let ((f (assoc function-name (get backend 'vc-functions))))
335 (if f (setq f (cdr f))
336 (setq f (vc-find-backend-function backend function-name))
337 (push (cons function-name f) (get backend 'vc-functions)))
338 (cond
339 ((null f)
340 (signal 'vc-not-supported (list function-name backend)))
341 ((consp f) (apply (car f) (cdr f) args))
342 (t (apply f args)))))
343
344 (defmacro vc-call (fun file &rest args)
345 "A convenience macro for calling VC backend functions.
346 Functions called by this macro must accept FILE as the first argument.
347 ARGS specifies any additional arguments. FUN should be unquoted.
348 BEWARE!! FILE is evaluated twice!!"
349 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
350 \f
351 (defsubst vc-parse-buffer (pattern i)
352 "Find PATTERN in the current buffer and return its Ith submatch."
353 (goto-char (point-min))
354 (if (re-search-forward pattern nil t)
355 (match-string i)))
356
357 (defun vc-insert-file (file &optional limit blocksize)
358 "Insert the contents of FILE into the current buffer.
359
360 Optional argument LIMIT is a regexp. If present, the file is inserted
361 in chunks of size BLOCKSIZE (default 8 kByte), until the first
362 occurrence of LIMIT is found. Anything from the start of that occurrence
363 to the end of the buffer is then deleted. The function returns
364 non-nil if FILE exists and its contents were successfully inserted."
365 (erase-buffer)
366 (when (file-exists-p file)
367 (if (not limit)
368 (insert-file-contents file)
369 (unless blocksize (setq blocksize 8192))
370 (let ((filepos 0))
371 (while
372 (and (< 0 (cadr (insert-file-contents
373 file nil filepos (cl-incf filepos blocksize))))
374 (progn (beginning-of-line)
375 (let ((pos (re-search-forward limit nil 'move)))
376 (when pos (delete-region (match-beginning 0)
377 (point-max)))
378 (not pos)))))))
379 (set-buffer-modified-p nil)
380 t))
381
382 (defun vc-find-root (file witness)
383 "Find the root of a checked out project.
384 The function walks up the directory tree from FILE looking for WITNESS.
385 If WITNESS if not found, return nil, otherwise return the root."
386 (let ((locate-dominating-stop-dir-regexp
387 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp)))
388 (locate-dominating-file file witness)))
389
390 ;; Access functions to file properties
391 ;; (Properties should be _set_ using vc-file-setprop, but
392 ;; _retrieved_ only through these functions, which decide
393 ;; if the property is already known or not. A property should
394 ;; only be retrieved by vc-file-getprop if there is no
395 ;; access function.)
396
397 ;; properties indicating the backend being used for FILE
398
399 (defun vc-registered (file)
400 "Return non-nil if FILE is registered in a version control system.
401
402 This function performs the check each time it is called. To rely
403 on the result of a previous call, use `vc-backend' instead. If the
404 file was previously registered under a certain backend, then that
405 backend is tried first."
406 (let (handler)
407 (cond
408 ((and (file-name-directory file)
409 (string-match vc-ignore-dir-regexp (file-name-directory file)))
410 nil)
411 ((and (boundp 'file-name-handler-alist)
412 (setq handler (find-file-name-handler file 'vc-registered)))
413 ;; handler should set vc-backend and return t if registered
414 (funcall handler 'vc-registered file))
415 (t
416 ;; There is no file name handler.
417 ;; Try vc-BACKEND-registered for each handled BACKEND.
418 (catch 'found
419 (let ((backend (vc-file-getprop file 'vc-backend)))
420 (mapc
421 (lambda (b)
422 (and (vc-call-backend b 'registered file)
423 (vc-file-setprop file 'vc-backend b)
424 (throw 'found t)))
425 (if (or (not backend) (eq backend 'none))
426 vc-handled-backends
427 (cons backend vc-handled-backends))))
428 ;; File is not registered.
429 (vc-file-setprop file 'vc-backend 'none)
430 nil)))))
431
432 (defun vc-backend (file-or-list)
433 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
434 If the argument is a list, the files must all have the same back end."
435 ;; `file' can be nil in several places (typically due to the use of
436 ;; code like (vc-backend buffer-file-name)).
437 (cond ((stringp file-or-list)
438 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
439 ;; Note that internally, Emacs remembers unregistered
440 ;; files by setting the property to `none'.
441 (cond ((eq property 'none) nil)
442 (property)
443 ;; vc-registered sets the vc-backend property
444 (t (if (vc-registered file-or-list)
445 (vc-file-getprop file-or-list 'vc-backend)
446 nil)))))
447 ((and file-or-list (listp file-or-list))
448 (vc-backend (car file-or-list)))
449 (t
450 nil)))
451
452
453 (defun vc-backend-subdirectory-name (file)
454 "Return where the repository for the current directory is kept."
455 (symbol-name (vc-backend file)))
456
457 (defun vc-name (file)
458 "Return the master name of FILE.
459 If the file is not registered, or the master name is not known, return nil."
460 ;; TODO: This should ultimately become obsolete, at least up here
461 ;; in vc-hooks.
462 (or (vc-file-getprop file 'vc-name)
463 ;; force computation of the property by calling
464 ;; vc-BACKEND-registered explicitly
465 (let ((backend (vc-backend file)))
466 (if (and backend
467 (vc-call-backend backend 'registered file))
468 (vc-file-getprop file 'vc-name)))))
469
470 (defun vc-checkout-model (backend files)
471 "Indicate how FILES are checked out.
472
473 If FILES are not registered, this function always returns nil.
474 For registered files, the possible values are:
475
476 'implicit FILES are always writable, and checked out `implicitly'
477 when the user saves the first changes to the file.
478
479 'locking FILES are read-only if up-to-date; user must type
480 \\[vc-next-action] before editing. Strict locking
481 is assumed.
482
483 'announce FILES are read-only if up-to-date; user must type
484 \\[vc-next-action] before editing. But other users
485 may be editing at the same time."
486 (vc-call-backend backend 'checkout-model files))
487
488 (defun vc-user-login-name (file)
489 "Return the name under which the user accesses the given FILE."
490 (or (and (eq (string-match tramp-file-name-regexp file) 0)
491 ;; tramp case: execute "whoami" via tramp
492 (let ((default-directory (file-name-directory file))
493 process-file-side-effects)
494 (with-temp-buffer
495 (if (not (zerop (process-file "whoami" nil t)))
496 ;; fall through if "whoami" didn't work
497 nil
498 ;; remove trailing newline
499 (delete-region (1- (point-max)) (point-max))
500 (buffer-string)))))
501 ;; normal case
502 (user-login-name)
503 ;; if user-login-name is nil, return the UID as a string
504 (number-to-string (user-uid))))
505
506 (defun vc-state (file &optional backend)
507 "Return the version control state of FILE.
508
509 A return of nil from this function means we have no information on the
510 status of this file. Otherwise, the value returned is one of:
511
512 'up-to-date The working file is unmodified with respect to the
513 latest version on the current branch, and not locked.
514
515 'edited The working file has been edited by the user. If
516 locking is used for the file, this state means that
517 the current version is locked by the calling user.
518 This status should *not* be reported for files
519 which have a changed mtime but the same content
520 as the repo copy.
521
522 USER The current version of the working file is locked by
523 some other USER (a string).
524
525 'needs-update The file has not been edited by the user, but there is
526 a more recent version on the current branch stored
527 in the repository.
528
529 'needs-merge The file has been edited by the user, and there is also
530 a more recent version on the current branch stored in
531 the repository. This state can only occur if locking
532 is not used for the file.
533
534 'unlocked-changes The working version of the file is not locked,
535 but the working file has been changed with respect
536 to that version. This state can only occur for files
537 with locking; it represents an erroneous condition that
538 should be resolved by the user (vc-next-action will
539 prompt the user to do it).
540
541 'added Scheduled to go into the repository on the next commit.
542 Often represented by vc-working-revision = \"0\" in VCSes
543 with monotonic IDs like Subversion and Mercurial.
544
545 'removed Scheduled to be deleted from the repository on next commit.
546
547 'conflict The file contains conflicts as the result of a merge.
548 For now the conflicts are text conflicts. In the
549 future this might be extended to deal with metadata
550 conflicts too.
551
552 'missing The file is not present in the file system, but the VC
553 system still tracks it.
554
555 'ignored The file showed up in a dir-status listing with a flag
556 indicating the version-control system is ignoring it,
557 Note: This property is not set reliably (some VCSes
558 don't have useful directory-status commands) so assume
559 that any file with vc-state nil might be ignorable
560 without VC knowing it.
561
562 'unregistered The file is not under version control."
563
564 ;; Note: in Emacs 22 and older, return of nil meant the file was
565 ;; unregistered. This is potentially a source of
566 ;; backward-compatibility bugs.
567
568 ;; FIXME: New (sub)states needed (?):
569 ;; - `copied' and `moved' (might be handled by `removed' and `added')
570 (or (vc-file-getprop file 'vc-state)
571 (when (> (length file) 0) ;Why?? --Stef
572 (setq backend (or backend (vc-backend file)))
573 (when backend
574 (vc-state-refresh file backend)))))
575
576 (defun vc-state-refresh (file backend)
577 "Quickly recompute the `state' of FILE."
578 (vc-file-setprop
579 file 'vc-state
580 (vc-call-backend backend 'state-heuristic file)))
581
582 (defsubst vc-up-to-date-p (file)
583 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
584 (eq (vc-state file) 'up-to-date))
585
586 (defun vc-default-state-heuristic (backend file)
587 "Default implementation of vc-BACKEND-state-heuristic.
588 It simply calls the real state computation function `vc-BACKEND-state'
589 and does not employ any heuristic at all."
590 (vc-call-backend backend 'state file))
591
592 (defun vc-workfile-unchanged-p (file)
593 "Return non-nil if FILE has not changed since the last checkout."
594 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
595 (lastmod (nth 5 (file-attributes file))))
596 ;; This is a shortcut for determining when the workfile is
597 ;; unchanged. It can fail under some circumstances; see the
598 ;; discussion in bug#694.
599 (if (and checkout-time
600 ;; Tramp and Ange-FTP return this when they don't know the time.
601 (not (equal lastmod '(0 0))))
602 (equal checkout-time lastmod)
603 (let ((unchanged (vc-call workfile-unchanged-p file)))
604 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
605 unchanged))))
606
607 (defun vc-default-workfile-unchanged-p (backend file)
608 "Check if FILE is unchanged by diffing against the repository version.
609 Return non-nil if FILE is unchanged."
610 (zerop (condition-case err
611 ;; If the implementation supports it, let the output
612 ;; go to *vc*, not *vc-diff*, since this is an internal call.
613 (vc-call-backend backend 'diff (list file) nil nil "*vc*")
614 (wrong-number-of-arguments
615 ;; If this error came from the above call to vc-BACKEND-diff,
616 ;; try again without the optional buffer argument (for
617 ;; backward compatibility). Otherwise, resignal.
618 (if (or (not (eq (cadr err)
619 (indirect-function
620 (vc-find-backend-function backend 'diff))))
621 (not (eq (cl-caddr err) 4)))
622 (signal (car err) (cdr err))
623 (vc-call-backend backend 'diff (list file)))))))
624
625 (defun vc-working-revision (file &optional backend)
626 "Return the repository version from which FILE was checked out.
627 If FILE is not registered, this function always returns nil."
628 (or (vc-file-getprop file 'vc-working-revision)
629 (progn
630 (setq backend (or backend (vc-backend file)))
631 (when backend
632 (vc-file-setprop file 'vc-working-revision
633 (vc-call-backend backend 'working-revision file))))))
634
635 ;; Backward compatibility.
636 (define-obsolete-function-alias
637 'vc-workfile-version 'vc-working-revision "23.1")
638 (defun vc-default-working-revision (backend file)
639 (message
640 "`working-revision' not found: using the old `workfile-version' instead")
641 (vc-call-backend backend 'workfile-version file))
642
643 (defun vc-default-registered (backend file)
644 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
645 (let ((sym (vc-make-backend-sym backend 'master-templates)))
646 (unless (get backend 'vc-templates-grabbed)
647 (put backend 'vc-templates-grabbed t))
648 (let ((result (vc-check-master-templates file (symbol-value sym))))
649 (if (stringp result)
650 (vc-file-setprop file 'vc-name result)
651 nil)))) ; Not registered
652
653 (defun vc-possible-master (s dirname basename)
654 (cond
655 ((stringp s) (format s dirname basename))
656 ((functionp s)
657 ;; The template is a function to invoke. If the
658 ;; function returns non-nil, that means it has found a
659 ;; master. For backward compatibility, we also handle
660 ;; the case that the function throws a 'found atom
661 ;; and a pair (cons MASTER-FILE BACKEND).
662 (let ((result (catch 'found (funcall s dirname basename))))
663 (if (consp result) (car result) result)))))
664
665 (defun vc-check-master-templates (file templates)
666 "Return non-nil if there is a master corresponding to FILE.
667
668 TEMPLATES is a list of strings or functions. If an element is a
669 string, it must be a control string as required by `format', with two
670 string placeholders, such as \"%sRCS/%s,v\". The directory part of
671 FILE is substituted for the first placeholder, the basename of FILE
672 for the second. If a file with the resulting name exists, it is taken
673 as the master of FILE, and returned.
674
675 If an element of TEMPLATES is a function, it is called with the
676 directory part and the basename of FILE as arguments. It should
677 return non-nil if it finds a master; that value is then returned by
678 this function."
679 (let ((dirname (or (file-name-directory file) ""))
680 (basename (file-name-nondirectory file)))
681 (catch 'found
682 (mapcar
683 (lambda (s)
684 (let ((trial (vc-possible-master s dirname basename)))
685 (when (and trial (file-exists-p trial)
686 ;; Make sure the file we found with name
687 ;; TRIAL is not the source file itself.
688 ;; That can happen with RCS-style names if
689 ;; the file name is truncated (e.g. to 14
690 ;; chars). See if either directory or
691 ;; attributes differ.
692 (or (not (string= dirname
693 (file-name-directory trial)))
694 (not (equal (file-attributes file)
695 (file-attributes trial)))))
696 (throw 'found trial))))
697 templates))))
698
699
700 ;; toggle-read-only is obsolete since 24.3, but since vc-t-r-o was made
701 ;; obsolete earlier, it is ok for the latter to be an alias to the former,
702 ;; since the latter will be removed first. We can't just make it
703 ;; an alias for read-only-mode, since that is not 100% the same.
704 (defalias 'vc-toggle-read-only 'toggle-read-only)
705 (make-obsolete 'vc-toggle-read-only
706 "use `read-only-mode' instead (or `toggle-read-only' in older versions of Emacs)."
707 "24.1")
708
709 (defun vc-default-make-version-backups-p (_backend _file)
710 "Return non-nil if unmodified versions should be backed up locally.
711 The default is to switch off this feature."
712 nil)
713
714 (defun vc-version-backup-file-name (file &optional rev manual regexp)
715 "Return a backup file name for REV or the current version of FILE.
716 If MANUAL is non-nil it means that a name for backups created by
717 the user should be returned; if REGEXP is non-nil that means to return
718 a regexp for matching all such backup files, regardless of the version."
719 (if regexp
720 (concat (regexp-quote (file-name-nondirectory file))
721 "\\.~.+" (unless manual "\\.") "~")
722 (expand-file-name (concat (file-name-nondirectory file)
723 ".~" (subst-char-in-string
724 ?/ ?_ (or rev (vc-working-revision file)))
725 (unless manual ".") "~")
726 (file-name-directory file))))
727
728 (defun vc-delete-automatic-version-backups (file)
729 "Delete all existing automatic version backups for FILE."
730 (condition-case nil
731 (mapc
732 'delete-file
733 (directory-files (or (file-name-directory file) default-directory) t
734 (vc-version-backup-file-name file nil nil t)))
735 ;; Don't fail when the directory doesn't exist.
736 (file-error nil)))
737
738 (defun vc-make-version-backup (file)
739 "Make a backup copy of FILE, which is assumed in sync with the repository.
740 Before doing that, check if there are any old backups and get rid of them."
741 (unless (and (fboundp 'msdos-long-file-names)
742 (not (with-no-warnings (msdos-long-file-names))))
743 (vc-delete-automatic-version-backups file)
744 (condition-case nil
745 (copy-file file (vc-version-backup-file-name file)
746 nil 'keep-date)
747 ;; It's ok if it doesn't work (e.g. directory not writable),
748 ;; since this is just for efficiency.
749 (file-error
750 (message
751 (concat "Warning: Cannot make version backup; "
752 "diff/revert therefore not local"))))))
753
754 (defun vc-before-save ()
755 "Function to be called by `basic-save-buffer' (in files.el)."
756 ;; If the file on disk is still in sync with the repository,
757 ;; and version backups should be made, copy the file to
758 ;; another name. This enables local diffs and local reverting.
759 (let ((file buffer-file-name)
760 backend)
761 (ignore-errors ;Be careful not to prevent saving the file.
762 (unless (file-exists-p file)
763 (vc-file-clearprops file))
764 (and (setq backend (vc-backend file))
765 (vc-up-to-date-p file)
766 (eq (vc-checkout-model backend (list file)) 'implicit)
767 (vc-call-backend backend 'make-version-backups-p file)
768 (vc-make-version-backup file)))))
769
770 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
771
772 (defvar vc-dir-buffers nil "List of vc-dir buffers.")
773
774 (defun vc-after-save ()
775 "Function to be called by `basic-save-buffer' (in files.el)."
776 ;; If the file in the current buffer is under version control,
777 ;; up-to-date, and locking is not used for the file, set
778 ;; the state to 'edited and redisplay the mode line.
779 (let* ((file buffer-file-name)
780 (backend (vc-backend file)))
781 (cond
782 ((null backend))
783 ((eq (vc-checkout-model backend (list file)) 'implicit)
784 ;; If the file was saved in the same second in which it was
785 ;; checked out, clear the checkout-time to avoid confusion.
786 (if (equal (vc-file-getprop file 'vc-checkout-time)
787 (nth 5 (file-attributes file)))
788 (vc-file-setprop file 'vc-checkout-time nil))
789 (if (vc-state-refresh file backend)
790 (vc-mode-line file backend)))
791 ;; If we saved an unlocked file on a locking based VCS, that
792 ;; file is not longer up-to-date.
793 ((eq (vc-file-getprop file 'vc-state) 'up-to-date)
794 (vc-file-setprop file 'vc-state nil)))
795 ;; Resynch *vc-dir* buffers, if any are present.
796 (when vc-dir-buffers
797 (vc-dir-resynch-file file))))
798
799 (defvar vc-menu-entry
800 `(menu-item ,(purecopy "Version Control") vc-menu-map
801 :filter vc-menu-map-filter))
802
803 (when (boundp 'menu-bar-tools-menu)
804 ;; We do not need to worry here about the placement of this entry
805 ;; because menu-bar.el has already created the proper spot for us
806 ;; and this will simply use it.
807 (define-key menu-bar-tools-menu [vc] vc-menu-entry))
808
809 (defconst vc-mode-line-map
810 (let ((map (make-sparse-keymap)))
811 (define-key map [mode-line down-mouse-1] vc-menu-entry)
812 map))
813
814 (defun vc-mode-line (file &optional backend)
815 "Set `vc-mode' to display type of version control for FILE.
816 The value is set in the current buffer, which should be the buffer
817 visiting FILE.
818 If BACKEND is passed use it as the VC backend when computing the result."
819 (interactive (list buffer-file-name))
820 (setq backend (or backend (vc-backend file)))
821 (if (not backend)
822 (setq vc-mode nil)
823 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
824 (ml-echo (get-text-property 0 'help-echo ml-string)))
825 (setq vc-mode
826 (concat
827 " "
828 (if (null vc-display-status)
829 (symbol-name backend)
830 (propertize
831 ml-string
832 'mouse-face 'mode-line-highlight
833 'help-echo
834 (concat (or ml-echo
835 (format "File under the %s version control system"
836 backend))
837 "\nmouse-1: Version Control menu")
838 'local-map vc-mode-line-map)))))
839 ;; If the user is root, and the file is not owner-writable,
840 ;; then pretend that we can't write it
841 ;; even though we can (because root can write anything).
842 ;; This way, even root cannot modify a file that isn't locked.
843 (and (equal file buffer-file-name)
844 (not buffer-read-only)
845 (zerop (user-real-uid))
846 (zerop (logand (file-modes buffer-file-name) 128))
847 (setq buffer-read-only t)))
848 (force-mode-line-update)
849 backend)
850
851 (defun vc-default-mode-line-string (backend file)
852 "Return a string for `vc-mode-line' to put in the mode line for FILE.
853 Format:
854
855 \"BACKEND-REV\" if the file is up-to-date
856 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
857 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
858 \"BACKEND@REV\" if the file was locally added
859 \"BACKEND!REV\" if the file contains conflicts or was removed
860 \"BACKEND?REV\" if the file is under VC, but is missing
861
862 This function assumes that the file is registered."
863 (let* ((backend-name (symbol-name backend))
864 (state (vc-state file backend))
865 (state-echo nil)
866 (face nil)
867 (rev (vc-working-revision file backend)))
868 (propertize
869 (cond ((or (eq state 'up-to-date)
870 (eq state 'needs-update))
871 (setq state-echo "Up to date file")
872 (setq face 'vc-up-to-date-state)
873 (concat backend-name "-" rev))
874 ((stringp state)
875 (setq state-echo (concat "File locked by" state))
876 (setq face 'vc-locked-state)
877 (concat backend-name ":" state ":" rev))
878 ((eq state 'added)
879 (setq state-echo "Locally added file")
880 (setq face 'vc-locally-added-state)
881 (concat backend-name "@" rev))
882 ((eq state 'conflict)
883 (setq state-echo "File contains conflicts after the last merge")
884 (setq face 'vc-conflict-state)
885 (concat backend-name "!" rev))
886 ((eq state 'removed)
887 (setq state-echo "File removed from the VC system")
888 (setq face 'vc-removed-state)
889 (concat backend-name "!" rev))
890 ((eq state 'missing)
891 (setq state-echo "File tracked by the VC system, but missing from the file system")
892 (setq face 'vc-missing-state)
893 (concat backend-name "?" rev))
894 (t
895 ;; Not just for the 'edited state, but also a fallback
896 ;; for all other states. Think about different symbols
897 ;; for 'needs-update and 'needs-merge.
898 (setq state-echo "Locally modified file")
899 (setq face 'vc-edited-state)
900 (concat backend-name ":" rev)))
901 'face face
902 'help-echo (concat state-echo " under the " backend-name
903 " version control system"))))
904
905 (defun vc-follow-link ()
906 "If current buffer visits a symbolic link, visit the real file.
907 If the real file is already visited in another buffer, make that buffer
908 current, and kill the buffer that visits the link."
909 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
910 (this-buffer (current-buffer)))
911 (if (eq true-buffer this-buffer)
912 (let ((truename buffer-file-truename))
913 (kill-buffer this-buffer)
914 ;; In principle, we could do something like set-visited-file-name.
915 ;; However, it can't be exactly the same as set-visited-file-name.
916 ;; I'm not going to work out the details right now. -- rms.
917 (set-buffer (find-file-noselect truename)))
918 (set-buffer true-buffer)
919 (kill-buffer this-buffer))))
920
921 (defun vc-default-find-file-hook (_backend)
922 nil)
923
924 (defun vc-find-file-hook ()
925 "Function for `find-file-hook' activating VC mode if appropriate."
926 ;; Recompute whether file is version controlled,
927 ;; if user has killed the buffer and revisited.
928 (when vc-mode
929 (setq vc-mode nil))
930 (when buffer-file-name
931 (vc-file-clearprops buffer-file-name)
932 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
933 (add-hook 'vc-mode-line-hook 'vc-mode-line nil t)
934 (let (backend)
935 (cond
936 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
937 ;; Compute the state and put it in the mode line.
938 (vc-mode-line buffer-file-name backend)
939 (unless vc-make-backup-files
940 ;; Use this variable, not make-backup-files,
941 ;; because this is for things that depend on the file name.
942 (set (make-local-variable 'backup-inhibited) t))
943 ;; Let the backend setup any buffer-local things he needs.
944 (vc-call-backend backend 'find-file-hook))
945 ((let* ((truename (and buffer-file-truename
946 (expand-file-name buffer-file-truename)))
947 (link-type (and truename
948 (not (equal buffer-file-name truename))
949 (vc-backend truename))))
950 (cond ((not link-type) nil) ;Nothing to do.
951 ((eq vc-follow-symlinks nil)
952 (message
953 "Warning: symbolic link to %s-controlled source file" link-type))
954 ((or (not (eq vc-follow-symlinks 'ask))
955 ;; Assume we cannot ask, default to yes.
956 noninteractive
957 ;; Copied from server-start. Seems like there should
958 ;; be a better way to ask "can we get user input?"...
959 (and (daemonp)
960 (null (cdr (frame-list)))
961 (eq (selected-frame) terminal-frame))
962 ;; If we already visited this file by following
963 ;; the link, don't ask again if we try to visit
964 ;; it again. GUD does that, and repeated questions
965 ;; are painful.
966 (get-file-buffer
967 (abbreviate-file-name
968 (file-chase-links buffer-file-name))))
969
970 (vc-follow-link)
971 (message "Followed link to %s" buffer-file-name)
972 (vc-find-file-hook))
973 (t
974 (if (yes-or-no-p (format
975 "Symbolic link to %s-controlled source file; follow link? " link-type))
976 (progn (vc-follow-link)
977 (message "Followed link to %s" buffer-file-name)
978 (vc-find-file-hook))
979 (message
980 "Warning: editing through the link bypasses version control")
981 )))))))))
982
983 (add-hook 'find-file-hook 'vc-find-file-hook)
984
985 (defun vc-kill-buffer-hook ()
986 "Discard VC info about a file when we kill its buffer."
987 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
988
989 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
990
991 ;; Now arrange for (autoloaded) bindings of the main package.
992 ;; Bindings for this have to go in the global map, as we'll often
993 ;; want to call them from random buffers.
994
995 ;; Autoloading works fine, but it prevents shortcuts from appearing
996 ;; in the menu because they don't exist yet when the menu is built.
997 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
998 (defvar vc-prefix-map
999 (let ((map (make-sparse-keymap)))
1000 (define-key map "a" 'vc-update-change-log)
1001 (define-key map "b" 'vc-switch-backend)
1002 (define-key map "c" 'vc-rollback)
1003 (define-key map "d" 'vc-dir)
1004 (define-key map "g" 'vc-annotate)
1005 (define-key map "G" 'vc-ignore)
1006 (define-key map "h" 'vc-insert-headers)
1007 (define-key map "i" 'vc-register)
1008 (define-key map "l" 'vc-print-log)
1009 (define-key map "L" 'vc-print-root-log)
1010 (define-key map "I" 'vc-log-incoming)
1011 (define-key map "O" 'vc-log-outgoing)
1012 (define-key map "m" 'vc-merge)
1013 (define-key map "r" 'vc-retrieve-tag)
1014 (define-key map "s" 'vc-create-tag)
1015 (define-key map "u" 'vc-revert)
1016 (define-key map "v" 'vc-next-action)
1017 (define-key map "+" 'vc-update)
1018 (define-key map "=" 'vc-diff)
1019 (define-key map "D" 'vc-root-diff)
1020 (define-key map "~" 'vc-revision-other-window)
1021 map))
1022 (fset 'vc-prefix-map vc-prefix-map)
1023 (define-key ctl-x-map "v" 'vc-prefix-map)
1024
1025 (defvar vc-menu-map
1026 (let ((map (make-sparse-keymap "Version Control")))
1027 ;;(define-key map [show-files]
1028 ;; '("Show Files under VC" . (vc-directory t)))
1029 (bindings--define-key map [vc-retrieve-tag]
1030 '(menu-item "Retrieve Tag" vc-retrieve-tag
1031 :help "Retrieve tagged version or branch"))
1032 (bindings--define-key map [vc-create-tag]
1033 '(menu-item "Create Tag" vc-create-tag
1034 :help "Create version tag"))
1035 (bindings--define-key map [separator1] menu-bar-separator)
1036 (bindings--define-key map [vc-annotate]
1037 '(menu-item "Annotate" vc-annotate
1038 :help "Display the edit history of the current file using colors"))
1039 (bindings--define-key map [vc-rename-file]
1040 '(menu-item "Rename File" vc-rename-file
1041 :help "Rename file"))
1042 (bindings--define-key map [vc-revision-other-window]
1043 '(menu-item "Show Other Version" vc-revision-other-window
1044 :help "Visit another version of the current file in another window"))
1045 (bindings--define-key map [vc-diff]
1046 '(menu-item "Compare with Base Version" vc-diff
1047 :help "Compare file set with the base version"))
1048 (bindings--define-key map [vc-root-diff]
1049 '(menu-item "Compare Tree with Base Version" vc-root-diff
1050 :help "Compare current tree with the base version"))
1051 (bindings--define-key map [vc-update-change-log]
1052 '(menu-item "Update ChangeLog" vc-update-change-log
1053 :help "Find change log file and add entries from recent version control logs"))
1054 (bindings--define-key map [vc-log-out]
1055 '(menu-item "Show Outgoing Log" vc-log-outgoing
1056 :help "Show a log of changes that will be sent with a push operation"))
1057 (bindings--define-key map [vc-log-in]
1058 '(menu-item "Show Incoming Log" vc-log-incoming
1059 :help "Show a log of changes that will be received with a pull operation"))
1060 (bindings--define-key map [vc-print-log]
1061 '(menu-item "Show History" vc-print-log
1062 :help "List the change log of the current file set in a window"))
1063 (bindings--define-key map [vc-print-root-log]
1064 '(menu-item "Show Top of the Tree History " vc-print-root-log
1065 :help "List the change log for the current tree in a window"))
1066 (bindings--define-key map [separator2] menu-bar-separator)
1067 (bindings--define-key map [vc-insert-header]
1068 '(menu-item "Insert Header" vc-insert-headers
1069 :help "Insert headers into a file for use with a version control system.
1070 "))
1071 (bindings--define-key map [undo]
1072 '(menu-item "Undo Last Check-In" vc-rollback
1073 :enable (let ((backend (if buffer-file-name
1074 (vc-backend buffer-file-name))))
1075 (or (not backend)
1076 (vc-find-backend-function backend 'rollback)))
1077 :help "Remove the most recent changeset committed to the repository"))
1078 (bindings--define-key map [vc-revert]
1079 '(menu-item "Revert to Base Version" vc-revert
1080 :help "Revert working copies of the selected file set to their repository contents"))
1081 (bindings--define-key map [vc-update]
1082 '(menu-item "Update to Latest Version" vc-update
1083 :help "Update the current fileset's files to their tip revisions"))
1084 (bindings--define-key map [vc-next-action]
1085 '(menu-item "Check In/Out" vc-next-action
1086 :help "Do the next logical version control operation on the current fileset"))
1087 (bindings--define-key map [vc-register]
1088 '(menu-item "Register" vc-register
1089 :help "Register file set into a version control system"))
1090 (bindings--define-key map [vc-ignore]
1091 '(menu-item "Ignore File..." vc-ignore
1092 :help "Ignore a file under current version control system"))
1093 (bindings--define-key map [vc-dir]
1094 '(menu-item "VC Dir" vc-dir
1095 :help "Show the VC status of files in a directory"))
1096 map))
1097
1098 (defalias 'vc-menu-map vc-menu-map)
1099
1100 (declare-function vc-responsible-backend "vc" (file))
1101
1102 (defun vc-menu-map-filter (orig-binding)
1103 (if (and (symbolp orig-binding) (fboundp orig-binding))
1104 (setq orig-binding (indirect-function orig-binding)))
1105 (let ((ext-binding
1106 (when vc-mode
1107 (vc-call-backend
1108 (if buffer-file-name
1109 (vc-backend buffer-file-name)
1110 (vc-responsible-backend default-directory))
1111 'extra-menu))))
1112 ;; Give the VC backend a chance to add menu entries
1113 ;; specific for that backend.
1114 (if (null ext-binding)
1115 orig-binding
1116 (append orig-binding
1117 '((ext-menu-separator "--"))
1118 ext-binding))))
1119
1120 (defun vc-default-extra-menu (_backend)
1121 nil)
1122
1123 (provide 'vc-hooks)
1124
1125 ;;; vc-hooks.el ends here