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