vc-hooks.el 10.3 KB
Newer Older
1
;;; vc-hooks.el --- resident support for version-control
Eric S. Raymond's avatar
Eric S. Raymond committed
2

Karl Heuer's avatar
Karl Heuer committed
3
;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4 5

;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Version: 5.3
Eric S. Raymond's avatar
Eric S. Raymond committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; See the commentary of vc.el.

;;; Code:

(defvar vc-master-templates
  '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
    ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS))
  "*Where to look for version-control master files.
The first pair corresponding to a given back end is used as a template
when creating new masters.")

(defvar vc-make-backup-files nil
38
  "*If non-nil, backups of registered files are made as with other files.
39
If nil (the default), files covered by version control don't get backups.")
Eric S. Raymond's avatar
Eric S. Raymond committed
40

41 42 43 44
(defvar vc-rcs-status t
  "*If non-nil, revision and locks on RCS working file displayed in modeline.
Otherwise, not displayed.")

Eric S. Raymond's avatar
Eric S. Raymond committed
45
;; Tell Emacs about this new kind of minor mode
Eric S. Raymond's avatar
Eric S. Raymond committed
46 47
(if (not (assoc 'vc-mode minor-mode-alist))
    (setq minor-mode-alist (cons '(vc-mode vc-mode)
Eric S. Raymond's avatar
Eric S. Raymond committed
48 49
				 minor-mode-alist)))

Eric S. Raymond's avatar
Eric S. Raymond committed
50
(make-variable-buffer-local 'vc-mode)
51
(put 'vc-mode 'permanent-local t)
Eric S. Raymond's avatar
Eric S. Raymond committed
52 53 54 55 56

;; We need a notion of per-file properties because the version
;; control state of a file is expensive to derive --- we don't
;; want to recompute it even on every find.

57 58 59
(defmacro vc-error-occurred (&rest body)
  (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))

Eric S. Raymond's avatar
Eric S. Raymond committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
(defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  "Obarray for per-file properties.")

(defun vc-file-setprop (file property value)
  ;; set per-file property
  (put (intern file vc-file-prop-obarray) property value))

(defun vc-file-getprop (file property)
  ;; get per-file property
  (get (intern file vc-file-prop-obarray) property))

;;; actual version-control code starts here

(defun vc-registered (file)
74 75
  (let (handler handlers)
    (if (boundp 'file-name-handler-alist)
76
	(setq handler (find-file-name-handler file 'vc-registered)))
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    (if handler
	(funcall handler 'vc-registered file)
      ;; Search for a master corresponding to the given file
      (let ((dirname (or (file-name-directory file) ""))
	    (basename (file-name-nondirectory file)))
	(catch 'found
	  (mapcar
	   (function (lambda (s)
	      (let ((trial (format (car s) dirname basename)))
		(if (and (file-exists-p trial)
			 ;; Make sure the file we found with name
			 ;; TRIAL is not the source file itself.
			 ;; That can happen with RCS-style names
			 ;; if the file name is truncated
			 ;; (e.g. to 14 chars).  See if either
			 ;; directory or attributes differ.
			 (or (not (string= dirname
					   (file-name-directory trial)))
			     (not (equal
				   (file-attributes file)
				   (file-attributes trial)))))
		    (throw 'found (cons trial (cdr s)))))))
	   vc-master-templates)
	  nil)))))
Eric S. Raymond's avatar
Eric S. Raymond committed
101

102 103 104 105 106 107 108 109 110
(defun vc-name (file)
  "Return the master name of a file, nil if it is not registered."
  (or (vc-file-getprop file 'vc-name)
      (let ((name-and-type (vc-registered file)))
	(if name-and-type
	    (progn
	      (vc-file-setprop file 'vc-backend (cdr name-and-type))
	      (vc-file-setprop file 'vc-name (car name-and-type)))))))

Eric S. Raymond's avatar
Eric S. Raymond committed
111
(defun vc-backend-deduce (file)
112
  "Return the version-control type of a file, nil if it is not registered."
Eric S. Raymond's avatar
Eric S. Raymond committed
113 114
  (and file
       (or (vc-file-getprop file 'vc-backend)
115 116 117 118 119
	   (let ((name-and-type (vc-registered file)))
	     (if name-and-type
		 (progn
		   (vc-file-setprop file 'vc-name (car name-and-type))
		   (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
120 121

(defun vc-toggle-read-only ()
122 123 124 125
  "Change read-only status of current buffer, perhaps via version control.
If the buffer is visiting a file registered with version control,
then check the file in or out.  Otherwise, just change the read-only flag
of the buffer."
Eric S. Raymond's avatar
Eric S. Raymond committed
126 127 128 129
  (interactive)
  (if (vc-backend-deduce (buffer-file-name))
      (vc-next-action nil)
    (toggle-read-only)))
130
(define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
Eric S. Raymond's avatar
Eric S. Raymond committed
131 132

(defun vc-mode-line (file &optional label)
Eric S. Raymond's avatar
Eric S. Raymond committed
133
  "Set `vc-mode' to display type of version control for FILE.
Eric S. Raymond's avatar
Eric S. Raymond committed
134 135
The value is set in the current buffer, which should be the buffer
visiting FILE."
136
  (interactive (list buffer-file-name nil))
Eric S. Raymond's avatar
Eric S. Raymond committed
137
  (let ((vc-type (vc-backend-deduce file)))
138 139 140 141 142
    (setq vc-mode
	  (and vc-type
	       (concat " " (or label (symbol-name vc-type))
		       (if (and vc-rcs-status (eq vc-type 'RCS))
			   (vc-rcs-status file)))))
143 144 145 146 147 148 149
    ;; Even root shouldn't modify a registered file without locking it first.
    (and vc-type
	 (not buffer-read-only)
	 (zerop (user-uid))
	 (require 'vc)
	 (not (string-equal (user-login-name) (vc-locking-user file)))
	 (setq buffer-read-only t))
150
    (and (null vc-type)
151 152
	 (file-symlink-p file)
	 (let ((link-type (vc-backend-deduce (file-symlink-p file))))
153 154 155
	   (if link-type
	       (message "Warning: symbolic link to %s-controlled source file"
			link-type))))
156 157
    (force-mode-line-update)
    ;;(set-buffer-modified-p (buffer-modified-p))  ;;use this if Emacs 18
Eric S. Raymond's avatar
Eric S. Raymond committed
158 159
    vc-type))

160
(defun vc-rcs-status (file)
161 162 163 164 165
  ;; Return string for placement in modeline by `vc-mode-line'.
  ;; If FILE is not registered under RCS, return nil.
  ;; If FILE is registered but not locked, return " REV" if there is a head
  ;; revision and " @@" otherwise.
  ;; If FILE is locked then return all locks in a string of the
166 167
  ;; form " LOCKER1:REV1 LOCKER2:REV2 ...", where "LOCKERi:" is empty if you
  ;; are the locker, and otherwise is the name of the locker followed by ":".
168 169 170

  ;; Algorithm: 

171
  ;; 1. Check for master file corresponding to FILE being visited.
172
  ;; 
173
  ;; 2. Insert the first few characters of the master file into a work
174 175
  ;; buffer.
  ;;  
176 177
  ;; 3. Search work buffer for "locks...;" phrase; if not found, then
  ;; keep inserting more characters until the phrase is found.
178
  ;; 
179
  ;; 4. Extract the locks, and remove control characters
180 181 182 183 184 185 186
  ;; separating them, like newlines; the string " user1:revision1
  ;; user2:revision2 ..." is returned.

  ;; Limitations:

  ;; The output doesn't show which version you are actually looking at.
  ;; The modeline can get quite cluttered when there are multiple locks.
187
  ;; The head revision is probably not what you want if you've used `rcs -b'.
188

189
  (let ((master (vc-name file))
190
	found)
191 192 193

    ;; If master file exists, then parse its contents, otherwise we return the 
    ;; nil value of this if form.
194
    (if master
195 196 197
        (save-excursion

          ;; Create work buffer.
198
          (set-buffer (get-buffer-create " *vc-rcs-status*"))
199 200 201 202
          (setq buffer-read-only nil
                default-directory (file-name-directory master))
          (erase-buffer)

203 204 205 206 207 208 209 210 211 212
          ;; Check if we have enough of the header.
	  ;; If not, then keep including more.
          (while
	      (not (or found
		       (let ((s (buffer-size)))
			 (goto-char (1+ s))
			 (zerop (car (cdr (insert-file-contents
					   master nil s (+ s 8192))))))))
	    (beginning-of-line)
	    (setq found (re-search-forward "^locks\\([^;]*\\);" nil t)))
213 214

          (if found
215 216 217 218
	      ;; Clean control characters and self-locks from text.
	      (let* ((lock-pattern
		      (concat "[ \b\t\n\v\f\r]+\\("
			      (regexp-quote (user-login-name))
219
			      ":\\)?"))
220
		     (locks
221 222 223
		      (save-restriction
			(narrow-to-region (match-beginning 1) (match-end 1))
			(goto-char (point-min))
224
			(while (re-search-forward lock-pattern nil t)
225
			  (replace-match (if (eobp) "" ":") t t))
226 227 228 229 230 231
			(buffer-string)))
		     (status
		      (if (not (string-equal locks ""))
			  locks
			(goto-char (point-min))
			(if (looking-at "head[ \b\t\n\v\f\r]+\\([.0-9]+\\)")
232
			    (concat "-" (buffer-substring (match-beginning 1)
233 234
							  (match-end 1)))
			  " @@"))))
235 236 237 238
		;; Clean work buffer.
		(erase-buffer)
		(set-buffer-modified-p nil)
		status))))))
239

Eric S. Raymond's avatar
Eric S. Raymond committed
240 241
;;; install a call to the above as a find-file hook
(defun vc-find-file-hook ()
242 243
  ;; Recompute whether file is version controlled,
  ;; if user has killed the buffer and revisited.
244 245
  (if buffer-file-name
      (vc-file-setprop buffer-file-name 'vc-backend nil))
Eric S. Raymond's avatar
Eric S. Raymond committed
246 247
  (if (and (vc-mode-line buffer-file-name) (not vc-make-backup-files))
      (progn
248 249 250 251
	;; Use this variable, not make-backup-files,
	;; because this is for things that depend on the file name.
	(make-local-variable 'backup-inhibited)
	(setq backup-inhibited t))))
Eric S. Raymond's avatar
Eric S. Raymond committed
252

253
(add-hook 'find-file-hooks 'vc-find-file-hook)
Eric S. Raymond's avatar
Eric S. Raymond committed
254 255 256 257 258 259 260 261 262 263

;;; more hooks, this time for file-not-found
(defun vc-file-not-found-hook ()
  "When file is not found, try to check it out from RCS or SCCS.
Returns t if checkout was successful, nil otherwise."
  (if (vc-backend-deduce buffer-file-name)
      (progn
	(require 'vc)
	(not (vc-error-occurred (vc-checkout buffer-file-name))))))

264
(add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
Eric S. Raymond's avatar
Eric S. Raymond committed
265 266

;;; Now arrange for bindings and autoloading of the main package.
Eric S. Raymond's avatar
Eric S. Raymond committed
267 268
;;; Bindings for this have to go in the global map, as we'll often
;;; want to call them from random buffers.
Eric S. Raymond's avatar
Eric S. Raymond committed
269 270 271 272 273 274 275 276

(setq vc-prefix-map (lookup-key global-map "\C-xv"))
(if (not (keymapp vc-prefix-map))
    (progn
      (setq vc-prefix-map (make-sparse-keymap))
      (define-key global-map "\C-xv" vc-prefix-map)
      (define-key vc-prefix-map "a" 'vc-update-change-log)
      (define-key vc-prefix-map "c" 'vc-cancel-version)
277
      (define-key vc-prefix-map "d" 'vc-directory)
Eric S. Raymond's avatar
Eric S. Raymond committed
278 279 280 281 282 283 284
      (define-key vc-prefix-map "h" 'vc-insert-headers)
      (define-key vc-prefix-map "i" 'vc-register)
      (define-key vc-prefix-map "l" 'vc-print-log)
      (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
      (define-key vc-prefix-map "s" 'vc-create-snapshot)
      (define-key vc-prefix-map "u" 'vc-revert-buffer)
      (define-key vc-prefix-map "v" 'vc-next-action)
285
      (define-key vc-prefix-map "=" 'vc-diff)
286
      (define-key vc-prefix-map "~" 'vc-version-other-window)
Eric S. Raymond's avatar
Eric S. Raymond committed
287 288 289 290 291
      ))

(provide 'vc-hooks)

;;; vc-hooks.el ends here