]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-hooks.el
API simplification: remove vc-workfile-unchanged-p from pubic methods.
[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))
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 SRC Bzr Git Hg Mtn Arch)
111 ;; RCS, CVS, SVN, SCCS, and SRC 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 "25.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 ".src" ".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-checkout-model (backend files)
458 "Indicate how FILES are checked out.
459
460 If FILES are not registered, this function always returns nil.
461 For registered files, the possible values are:
462
463 'implicit FILES are always writable, and checked out `implicitly'
464 when the user saves the first changes to the file.
465
466 'locking FILES are read-only if up-to-date; user must type
467 \\[vc-next-action] before editing. Strict locking
468 is assumed.
469
470 'announce FILES are read-only if up-to-date; user must type
471 \\[vc-next-action] before editing. But other users
472 may be editing at the same time."
473 (vc-call-backend backend 'checkout-model files))
474
475 (defun vc-user-login-name (file)
476 "Return the name under which the user accesses the given FILE."
477 (or (and (eq (string-match tramp-file-name-regexp file) 0)
478 ;; tramp case: execute "whoami" via tramp
479 (let ((default-directory (file-name-directory file))
480 process-file-side-effects)
481 (with-temp-buffer
482 (if (not (zerop (process-file "whoami" nil t)))
483 ;; fall through if "whoami" didn't work
484 nil
485 ;; remove trailing newline
486 (delete-region (1- (point-max)) (point-max))
487 (buffer-string)))))
488 ;; normal case
489 (user-login-name)
490 ;; if user-login-name is nil, return the UID as a string
491 (number-to-string (user-uid))))
492
493 (defun vc-state (file &optional backend)
494 "Return the version control state of FILE.
495
496 A return of nil from this function means we have no information on the
497 status of this file. Otherwise, the value returned is one of:
498
499 'up-to-date The working file is unmodified with respect to the
500 latest version on the current branch, and not locked.
501
502 'edited The working file has been edited by the user. If
503 locking is used for the file, this state means that
504 the current version is locked by the calling user.
505 This status should *not* be reported for files
506 which have a changed mtime but the same content
507 as the repo copy.
508
509 USER The current version of the working file is locked by
510 some other USER (a string).
511
512 'needs-update The file has not been edited by the user, but there is
513 a more recent version on the current branch stored
514 in the repository.
515
516 'needs-merge The file has been edited by the user, and there is also
517 a more recent version on the current branch stored in
518 the repository. This state can only occur if locking
519 is not used for the file.
520
521 'unlocked-changes The working version of the file is not locked,
522 but the working file has been changed with respect
523 to that version. This state can only occur for files
524 with locking; it represents an erroneous condition that
525 should be resolved by the user (vc-next-action will
526 prompt the user to do it).
527
528 'added Scheduled to go into the repository on the next commit.
529 Often represented by vc-working-revision = \"0\" in VCSes
530 with monotonic IDs like Subversion and Mercurial.
531
532 'removed Scheduled to be deleted from the repository on next commit.
533
534 'conflict The file contains conflicts as the result of a merge.
535 For now the conflicts are text conflicts. In the
536 future this might be extended to deal with metadata
537 conflicts too.
538
539 'missing The file is not present in the file system, but the VC
540 system still tracks it.
541
542 'ignored The file showed up in a dir-status listing with a flag
543 indicating the version-control system is ignoring it,
544 Note: This property is not set reliably (some VCSes
545 don't have useful directory-status commands) so assume
546 that any file with vc-state nil might be ignorable
547 without VC knowing it.
548
549 'unregistered The file is not under version control."
550
551 ;; Note: in Emacs 22 and older, return of nil meant the file was
552 ;; unregistered. This is potentially a source of
553 ;; backward-compatibility bugs.
554
555 ;; FIXME: New (sub)states needed (?):
556 ;; - `copied' and `moved' (might be handled by `removed' and `added')
557 (or (vc-file-getprop file 'vc-state)
558 (when (> (length file) 0) ;Why?? --Stef
559 (setq backend (or backend (vc-backend file)))
560 (when backend
561 (vc-state-refresh file backend)))))
562
563 (defun vc-state-refresh (file backend)
564 "Quickly recompute the `state' of FILE."
565 (vc-file-setprop
566 file 'vc-state
567 (vc-call-backend backend 'state-heuristic file)))
568
569 (defsubst vc-up-to-date-p (file)
570 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
571 (eq (vc-state file) 'up-to-date))
572
573 (defun vc-default-state-heuristic (backend file)
574 "Default implementation of vc-BACKEND-state-heuristic.
575 It simply calls the real state computation function `vc-BACKEND-state'
576 and does not employ any heuristic at all."
577 (vc-call-backend backend 'state file))
578
579 (defun vc-working-revision (file &optional backend)
580 "Return the repository version from which FILE was checked out.
581 If FILE is not registered, this function always returns nil."
582 (or (vc-file-getprop file 'vc-working-revision)
583 (progn
584 (setq backend (or backend (vc-backend file)))
585 (when backend
586 (vc-file-setprop file 'vc-working-revision
587 (vc-call-backend backend 'working-revision file))))))
588
589 ;; Backward compatibility.
590 (define-obsolete-function-alias
591 'vc-workfile-version 'vc-working-revision "23.1")
592 (defun vc-default-working-revision (backend file)
593 (message
594 "`working-revision' not found: using the old `workfile-version' instead")
595 (vc-call-backend backend 'workfile-version file))
596
597 (defun vc-default-registered (backend file)
598 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
599 (let ((sym (vc-make-backend-sym backend 'master-templates)))
600 (unless (get backend 'vc-templates-grabbed)
601 (put backend 'vc-templates-grabbed t))
602 (let ((result (vc-check-master-templates file (symbol-value sym))))
603 (if (stringp result)
604 (vc-file-setprop file 'vc-master-name result)
605 nil)))) ; Not registered
606
607 ;;;###autoload
608 (defun vc-possible-master (s dirname basename)
609 (cond
610 ((stringp s) (format s dirname basename))
611 ((functionp s)
612 ;; The template is a function to invoke. If the
613 ;; function returns non-nil, that means it has found a
614 ;; master. For backward compatibility, we also handle
615 ;; the case that the function throws a 'found atom
616 ;; and a pair (cons MASTER-FILE BACKEND).
617 (let ((result (catch 'found (funcall s dirname basename))))
618 (if (consp result) (car result) result)))))
619
620 (defun vc-check-master-templates (file templates)
621 "Return non-nil if there is a master corresponding to FILE.
622
623 TEMPLATES is a list of strings or functions. If an element is a
624 string, it must be a control string as required by `format', with two
625 string placeholders, such as \"%sRCS/%s,v\". The directory part of
626 FILE is substituted for the first placeholder, the basename of FILE
627 for the second. If a file with the resulting name exists, it is taken
628 as the master of FILE, and returned.
629
630 If an element of TEMPLATES is a function, it is called with the
631 directory part and the basename of FILE as arguments. It should
632 return non-nil if it finds a master; that value is then returned by
633 this function."
634 (let ((dirname (or (file-name-directory file) ""))
635 (basename (file-name-nondirectory file)))
636 (catch 'found
637 (mapcar
638 (lambda (s)
639 (let ((trial (vc-possible-master s dirname basename)))
640 (when (and trial (file-exists-p trial)
641 ;; Make sure the file we found with name
642 ;; TRIAL is not the source file itself.
643 ;; That can happen with RCS-style names if
644 ;; the file name is truncated (e.g. to 14
645 ;; chars). See if either directory or
646 ;; attributes differ.
647 (or (not (string= dirname
648 (file-name-directory trial)))
649 (not (equal (file-attributes file)
650 (file-attributes trial)))))
651 (throw 'found trial))))
652 templates))))
653
654
655 ;; toggle-read-only is obsolete since 24.3, but since vc-t-r-o was made
656 ;; obsolete earlier, it is ok for the latter to be an alias to the former,
657 ;; since the latter will be removed first. We can't just make it
658 ;; an alias for read-only-mode, since that is not 100% the same.
659 (defalias 'vc-toggle-read-only 'toggle-read-only)
660 (make-obsolete 'vc-toggle-read-only
661 "use `read-only-mode' instead (or `toggle-read-only' in older versions of Emacs)."
662 "24.1")
663
664 (defun vc-default-make-version-backups-p (_backend _file)
665 "Return non-nil if unmodified versions should be backed up locally.
666 The default is to switch off this feature."
667 nil)
668
669 (defun vc-version-backup-file-name (file &optional rev manual regexp)
670 "Return a backup file name for REV or the current version of FILE.
671 If MANUAL is non-nil it means that a name for backups created by
672 the user should be returned; if REGEXP is non-nil that means to return
673 a regexp for matching all such backup files, regardless of the version."
674 (if regexp
675 (concat (regexp-quote (file-name-nondirectory file))
676 "\\.~.+" (unless manual "\\.") "~")
677 (expand-file-name (concat (file-name-nondirectory file)
678 ".~" (subst-char-in-string
679 ?/ ?_ (or rev (vc-working-revision file)))
680 (unless manual ".") "~")
681 (file-name-directory file))))
682
683 (defun vc-delete-automatic-version-backups (file)
684 "Delete all existing automatic version backups for FILE."
685 (condition-case nil
686 (mapc
687 'delete-file
688 (directory-files (or (file-name-directory file) default-directory) t
689 (vc-version-backup-file-name file nil nil t)))
690 ;; Don't fail when the directory doesn't exist.
691 (file-error nil)))
692
693 (defun vc-make-version-backup (file)
694 "Make a backup copy of FILE, which is assumed in sync with the repository.
695 Before doing that, check if there are any old backups and get rid of them."
696 (unless (and (fboundp 'msdos-long-file-names)
697 (not (with-no-warnings (msdos-long-file-names))))
698 (vc-delete-automatic-version-backups file)
699 (condition-case nil
700 (copy-file file (vc-version-backup-file-name file)
701 nil 'keep-date)
702 ;; It's ok if it doesn't work (e.g. directory not writable),
703 ;; since this is just for efficiency.
704 (file-error
705 (message
706 (concat "Warning: Cannot make version backup; "
707 "diff/revert therefore not local"))))))
708
709 (defun vc-before-save ()
710 "Function to be called by `basic-save-buffer' (in files.el)."
711 ;; If the file on disk is still in sync with the repository,
712 ;; and version backups should be made, copy the file to
713 ;; another name. This enables local diffs and local reverting.
714 (let ((file buffer-file-name)
715 backend)
716 (ignore-errors ;Be careful not to prevent saving the file.
717 (unless (file-exists-p file)
718 (vc-file-clearprops file))
719 (and (setq backend (vc-backend file))
720 (vc-up-to-date-p file)
721 (eq (vc-checkout-model backend (list file)) 'implicit)
722 (vc-call-backend backend 'make-version-backups-p file)
723 (vc-make-version-backup file)))))
724
725 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
726
727 (defvar vc-dir-buffers nil "List of vc-dir buffers.")
728
729 (defun vc-after-save ()
730 "Function to be called by `basic-save-buffer' (in files.el)."
731 ;; If the file in the current buffer is under version control,
732 ;; up-to-date, and locking is not used for the file, set
733 ;; the state to 'edited and redisplay the mode line.
734 (let* ((file buffer-file-name)
735 (backend (vc-backend file)))
736 (cond
737 ((null backend))
738 ((eq (vc-checkout-model backend (list file)) 'implicit)
739 ;; If the file was saved in the same second in which it was
740 ;; checked out, clear the checkout-time to avoid confusion.
741 (if (equal (vc-file-getprop file 'vc-checkout-time)
742 (nth 5 (file-attributes file)))
743 (vc-file-setprop file 'vc-checkout-time nil))
744 (if (vc-state-refresh file backend)
745 (vc-mode-line file backend)))
746 ;; If we saved an unlocked file on a locking based VCS, that
747 ;; file is not longer up-to-date.
748 ((eq (vc-file-getprop file 'vc-state) 'up-to-date)
749 (vc-file-setprop file 'vc-state nil)))
750 ;; Resynch *vc-dir* buffers, if any are present.
751 (when vc-dir-buffers
752 (vc-dir-resynch-file file))))
753
754 (defvar vc-menu-entry
755 `(menu-item ,(purecopy "Version Control") vc-menu-map
756 :filter vc-menu-map-filter))
757
758 (when (boundp 'menu-bar-tools-menu)
759 ;; We do not need to worry here about the placement of this entry
760 ;; because menu-bar.el has already created the proper spot for us
761 ;; and this will simply use it.
762 (define-key menu-bar-tools-menu [vc] vc-menu-entry))
763
764 (defconst vc-mode-line-map
765 (let ((map (make-sparse-keymap)))
766 (define-key map [mode-line down-mouse-1] vc-menu-entry)
767 map))
768
769 (defun vc-mode-line (file &optional backend)
770 "Set `vc-mode' to display type of version control for FILE.
771 The value is set in the current buffer, which should be the buffer
772 visiting FILE.
773 If BACKEND is passed use it as the VC backend when computing the result."
774 (interactive (list buffer-file-name))
775 (setq backend (or backend (vc-backend file)))
776 (if (not backend)
777 (setq vc-mode nil)
778 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
779 (ml-echo (get-text-property 0 'help-echo ml-string)))
780 (setq vc-mode
781 (concat
782 " "
783 (if (null vc-display-status)
784 (symbol-name backend)
785 (propertize
786 ml-string
787 'mouse-face 'mode-line-highlight
788 'help-echo
789 (concat (or ml-echo
790 (format "File under the %s version control system"
791 backend))
792 "\nmouse-1: Version Control menu")
793 'local-map vc-mode-line-map)))))
794 ;; If the user is root, and the file is not owner-writable,
795 ;; then pretend that we can't write it
796 ;; even though we can (because root can write anything).
797 ;; This way, even root cannot modify a file that isn't locked.
798 (and (equal file buffer-file-name)
799 (not buffer-read-only)
800 (zerop (user-real-uid))
801 (zerop (logand (file-modes buffer-file-name) 128))
802 (setq buffer-read-only t)))
803 (force-mode-line-update)
804 backend)
805
806 (defun vc-default-mode-line-string (backend file)
807 "Return a string for `vc-mode-line' to put in the mode line for FILE.
808 Format:
809
810 \"BACKEND-REV\" if the file is up-to-date
811 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
812 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
813 \"BACKEND@REV\" if the file was locally added
814 \"BACKEND!REV\" if the file contains conflicts or was removed
815 \"BACKEND?REV\" if the file is under VC, but is missing
816
817 This function assumes that the file is registered."
818 (let* ((backend-name (symbol-name backend))
819 (state (vc-state file backend))
820 (state-echo nil)
821 (face nil)
822 (rev (vc-working-revision file backend)))
823 (propertize
824 (cond ((or (eq state 'up-to-date)
825 (eq state 'needs-update))
826 (setq state-echo "Up to date file")
827 (setq face 'vc-up-to-date-state)
828 (concat backend-name "-" rev))
829 ((stringp state)
830 (setq state-echo (concat "File locked by" state))
831 (setq face 'vc-locked-state)
832 (concat backend-name ":" state ":" rev))
833 ((eq state 'added)
834 (setq state-echo "Locally added file")
835 (setq face 'vc-locally-added-state)
836 (concat backend-name "@" rev))
837 ((eq state 'conflict)
838 (setq state-echo "File contains conflicts after the last merge")
839 (setq face 'vc-conflict-state)
840 (concat backend-name "!" rev))
841 ((eq state 'removed)
842 (setq state-echo "File removed from the VC system")
843 (setq face 'vc-removed-state)
844 (concat backend-name "!" rev))
845 ((eq state 'missing)
846 (setq state-echo "File tracked by the VC system, but missing from the file system")
847 (setq face 'vc-missing-state)
848 (concat backend-name "?" rev))
849 (t
850 ;; Not just for the 'edited state, but also a fallback
851 ;; for all other states. Think about different symbols
852 ;; for 'needs-update and 'needs-merge.
853 (setq state-echo "Locally modified file")
854 (setq face 'vc-edited-state)
855 (concat backend-name ":" rev)))
856 'face face
857 'help-echo (concat state-echo " under the " backend-name
858 " version control system"))))
859
860 (defun vc-follow-link ()
861 "If current buffer visits a symbolic link, visit the real file.
862 If the real file is already visited in another buffer, make that buffer
863 current, and kill the buffer that visits the link."
864 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
865 (this-buffer (current-buffer)))
866 (if (eq true-buffer this-buffer)
867 (let ((truename buffer-file-truename))
868 (kill-buffer this-buffer)
869 ;; In principle, we could do something like set-visited-file-name.
870 ;; However, it can't be exactly the same as set-visited-file-name.
871 ;; I'm not going to work out the details right now. -- rms.
872 (set-buffer (find-file-noselect truename)))
873 (set-buffer true-buffer)
874 (kill-buffer this-buffer))))
875
876 (defun vc-default-find-file-hook (_backend)
877 nil)
878
879 (defun vc-find-file-hook ()
880 "Function for `find-file-hook' activating VC mode if appropriate."
881 ;; Recompute whether file is version controlled,
882 ;; if user has killed the buffer and revisited.
883 (when vc-mode
884 (setq vc-mode nil))
885 (when buffer-file-name
886 (vc-file-clearprops buffer-file-name)
887 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
888 (add-hook 'vc-mode-line-hook 'vc-mode-line nil t)
889 (let (backend)
890 (cond
891 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
892 ;; Compute the state and put it in the mode line.
893 (vc-mode-line buffer-file-name backend)
894 (unless vc-make-backup-files
895 ;; Use this variable, not make-backup-files,
896 ;; because this is for things that depend on the file name.
897 (set (make-local-variable 'backup-inhibited) t))
898 ;; Let the backend setup any buffer-local things he needs.
899 (vc-call-backend backend 'find-file-hook))
900 ((let* ((truename (and buffer-file-truename
901 (expand-file-name buffer-file-truename)))
902 (link-type (and truename
903 (not (equal buffer-file-name truename))
904 (vc-backend truename))))
905 (cond ((not link-type) nil) ;Nothing to do.
906 ((eq vc-follow-symlinks nil)
907 (message
908 "Warning: symbolic link to %s-controlled source file" link-type))
909 ((or (not (eq vc-follow-symlinks 'ask))
910 ;; Assume we cannot ask, default to yes.
911 noninteractive
912 ;; Copied from server-start. Seems like there should
913 ;; be a better way to ask "can we get user input?"...
914 (and (daemonp)
915 (null (cdr (frame-list)))
916 (eq (selected-frame) terminal-frame))
917 ;; If we already visited this file by following
918 ;; the link, don't ask again if we try to visit
919 ;; it again. GUD does that, and repeated questions
920 ;; are painful.
921 (get-file-buffer
922 (abbreviate-file-name
923 (file-chase-links buffer-file-name))))
924
925 (vc-follow-link)
926 (message "Followed link to %s" buffer-file-name)
927 (vc-find-file-hook))
928 (t
929 (if (yes-or-no-p (format
930 "Symbolic link to %s-controlled source file; follow link? " link-type))
931 (progn (vc-follow-link)
932 (message "Followed link to %s" buffer-file-name)
933 (vc-find-file-hook))
934 (message
935 "Warning: editing through the link bypasses version control")
936 )))))))))
937
938 (add-hook 'find-file-hook 'vc-find-file-hook)
939
940 (defun vc-kill-buffer-hook ()
941 "Discard VC info about a file when we kill its buffer."
942 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
943
944 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
945
946 ;; Now arrange for (autoloaded) bindings of the main package.
947 ;; Bindings for this have to go in the global map, as we'll often
948 ;; want to call them from random buffers.
949
950 ;; Autoloading works fine, but it prevents shortcuts from appearing
951 ;; in the menu because they don't exist yet when the menu is built.
952 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
953 (defvar vc-prefix-map
954 (let ((map (make-sparse-keymap)))
955 (define-key map "a" 'vc-update-change-log)
956 (define-key map "b" 'vc-switch-backend)
957 (define-key map "c" 'vc-rollback)
958 (define-key map "d" 'vc-dir)
959 (define-key map "g" 'vc-annotate)
960 (define-key map "G" 'vc-ignore)
961 (define-key map "h" 'vc-insert-headers)
962 (define-key map "i" 'vc-register)
963 (define-key map "l" 'vc-print-log)
964 (define-key map "L" 'vc-print-root-log)
965 (define-key map "I" 'vc-log-incoming)
966 (define-key map "O" 'vc-log-outgoing)
967 (define-key map "m" 'vc-merge)
968 (define-key map "r" 'vc-retrieve-tag)
969 (define-key map "s" 'vc-create-tag)
970 (define-key map "u" 'vc-revert)
971 (define-key map "v" 'vc-next-action)
972 (define-key map "+" 'vc-update)
973 (define-key map "=" 'vc-diff)
974 (define-key map "D" 'vc-root-diff)
975 (define-key map "~" 'vc-revision-other-window)
976 map))
977 (fset 'vc-prefix-map vc-prefix-map)
978 (define-key ctl-x-map "v" 'vc-prefix-map)
979
980 (defvar vc-menu-map
981 (let ((map (make-sparse-keymap "Version Control")))
982 ;;(define-key map [show-files]
983 ;; '("Show Files under VC" . (vc-directory t)))
984 (bindings--define-key map [vc-retrieve-tag]
985 '(menu-item "Retrieve Tag" vc-retrieve-tag
986 :help "Retrieve tagged version or branch"))
987 (bindings--define-key map [vc-create-tag]
988 '(menu-item "Create Tag" vc-create-tag
989 :help "Create version tag"))
990 (bindings--define-key map [separator1] menu-bar-separator)
991 (bindings--define-key map [vc-annotate]
992 '(menu-item "Annotate" vc-annotate
993 :help "Display the edit history of the current file using colors"))
994 (bindings--define-key map [vc-rename-file]
995 '(menu-item "Rename File" vc-rename-file
996 :help "Rename file"))
997 (bindings--define-key map [vc-revision-other-window]
998 '(menu-item "Show Other Version" vc-revision-other-window
999 :help "Visit another version of the current file in another window"))
1000 (bindings--define-key map [vc-diff]
1001 '(menu-item "Compare with Base Version" vc-diff
1002 :help "Compare file set with the base version"))
1003 (bindings--define-key map [vc-root-diff]
1004 '(menu-item "Compare Tree with Base Version" vc-root-diff
1005 :help "Compare current tree with the base version"))
1006 (bindings--define-key map [vc-update-change-log]
1007 '(menu-item "Update ChangeLog" vc-update-change-log
1008 :help "Find change log file and add entries from recent version control logs"))
1009 (bindings--define-key map [vc-log-out]
1010 '(menu-item "Show Outgoing Log" vc-log-outgoing
1011 :help "Show a log of changes that will be sent with a push operation"))
1012 (bindings--define-key map [vc-log-in]
1013 '(menu-item "Show Incoming Log" vc-log-incoming
1014 :help "Show a log of changes that will be received with a pull operation"))
1015 (bindings--define-key map [vc-print-log]
1016 '(menu-item "Show History" vc-print-log
1017 :help "List the change log of the current file set in a window"))
1018 (bindings--define-key map [vc-print-root-log]
1019 '(menu-item "Show Top of the Tree History " vc-print-root-log
1020 :help "List the change log for the current tree in a window"))
1021 (bindings--define-key map [separator2] menu-bar-separator)
1022 (bindings--define-key map [vc-insert-header]
1023 '(menu-item "Insert Header" vc-insert-headers
1024 :help "Insert headers into a file for use with a version control system.
1025 "))
1026 (bindings--define-key map [undo]
1027 '(menu-item "Undo Last Check-In" vc-rollback
1028 :enable (let ((backend (if buffer-file-name
1029 (vc-backend buffer-file-name))))
1030 (or (not backend)
1031 (vc-find-backend-function backend 'rollback)))
1032 :help "Remove the most recent changeset committed to the repository"))
1033 (bindings--define-key map [vc-revert]
1034 '(menu-item "Revert to Base Version" vc-revert
1035 :help "Revert working copies of the selected file set to their repository contents"))
1036 (bindings--define-key map [vc-update]
1037 '(menu-item "Update to Latest Version" vc-update
1038 :help "Update the current fileset's files to their tip revisions"))
1039 (bindings--define-key map [vc-next-action]
1040 '(menu-item "Check In/Out" vc-next-action
1041 :help "Do the next logical version control operation on the current fileset"))
1042 (bindings--define-key map [vc-register]
1043 '(menu-item "Register" vc-register
1044 :help "Register file set into a version control system"))
1045 (bindings--define-key map [vc-ignore]
1046 '(menu-item "Ignore File..." vc-ignore
1047 :help "Ignore a file under current version control system"))
1048 (bindings--define-key map [vc-dir]
1049 '(menu-item "VC Dir" vc-dir
1050 :help "Show the VC status of files in a directory"))
1051 map))
1052
1053 (defalias 'vc-menu-map vc-menu-map)
1054
1055 (declare-function vc-responsible-backend "vc" (file))
1056
1057 (defun vc-menu-map-filter (orig-binding)
1058 (if (and (symbolp orig-binding) (fboundp orig-binding))
1059 (setq orig-binding (indirect-function orig-binding)))
1060 (let ((ext-binding
1061 (when vc-mode
1062 (vc-call-backend
1063 (if buffer-file-name
1064 (vc-backend buffer-file-name)
1065 (vc-responsible-backend default-directory))
1066 'extra-menu))))
1067 ;; Give the VC backend a chance to add menu entries
1068 ;; specific for that backend.
1069 (if (null ext-binding)
1070 orig-binding
1071 (append orig-binding
1072 '((ext-menu-separator "--"))
1073 ext-binding))))
1074
1075 (defun vc-default-extra-menu (_backend)
1076 nil)
1077
1078 (provide 'vc-hooks)
1079
1080 ;;; vc-hooks.el ends here