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