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