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