]> code.delx.au - gnu-emacs/blob - lisp/vc-hooks.el
(shell-command-on-region):
[gnu-emacs] / lisp / vc-hooks.el
1 ;;; vc-hooks.el -- resident support for version-control
2
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Version: 4.0
7
8 ;; $Id: vc-hooks.el,v 1.5 1992/10/20 18:43:33 rms Exp rms $
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
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
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;; See the commentary of vc.el.
29
30 ;;; Code:
31
32 (defvar vc-master-templates
33 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
34 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS))
35 "*Where to look for version-control master files.
36 The first pair corresponding to a given back end is used as a template
37 when creating new masters.")
38
39 (defvar vc-make-backup-files nil
40 "*If non-nil, backups of registered files are made according to
41 the make-backup-files variable. Otherwise, prevents backups being made.")
42
43 ;; Tell Emacs about this new kind of minor mode
44 (if (not (assoc 'vc-mode-string minor-mode-alist))
45 (setq minor-mode-alist (cons '(vc-mode-string vc-mode-string)
46 minor-mode-alist)))
47
48 (make-variable-buffer-local 'vc-mode-string)
49
50 ;; We need a notion of per-file properties because the version
51 ;; control state of a file is expensive to derive --- we don't
52 ;; want to recompute it even on every find.
53
54 (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
55 "Obarray for per-file properties.")
56
57 (defun vc-file-setprop (file property value)
58 ;; set per-file property
59 (put (intern file vc-file-prop-obarray) property value))
60
61 (defun vc-file-getprop (file property)
62 ;; get per-file property
63 (get (intern file vc-file-prop-obarray) property))
64
65 ;;; actual version-control code starts here
66
67 (defun vc-registered (file)
68 (let (handler handlers)
69 (if (boundp 'file-name-handler-alist)
70 (save-match-data
71 (setq handlers file-name-handler-alist)
72 (while (and (consp handlers) (null handler))
73 (if (and (consp (car handlers))
74 (stringp (car (car handlers)))
75 (string-match (car (car handlers)) file))
76 (setq handler (cdr (car handlers))))
77 (setq handlers (cdr handlers)))))
78 (if handler
79 (funcall handler 'vc-registered file)
80 ;; Search for a master corresponding to the given file
81 (let ((dirname (or (file-name-directory file) ""))
82 (basename (file-name-nondirectory file)))
83 (catch 'found
84 (mapcar
85 (function (lambda (s)
86 (let ((trial (format (car s) dirname basename)))
87 (if (and (file-exists-p trial)
88 ;; Make sure the file we found with name
89 ;; TRIAL is not the source file itself.
90 ;; That can happen with RCS-style names
91 ;; if the file name is truncated
92 ;; (e.g. to 14 chars). See if either
93 ;; directory or attributes differ.
94 (or (not (string= dirname
95 (file-name-directory trial)))
96 (not (equal
97 (file-attributes file)
98 (file-attributes trial)))))
99 (throw 'found (cons trial (cdr s)))))))
100 vc-master-templates)
101 nil)))))
102
103 (defun vc-backend-deduce (file)
104 "Return the version-control type of a file, nil if it is not registered"
105 (and file
106 (or (vc-file-getprop file 'vc-backend)
107 (vc-file-setprop file 'vc-backend (cdr (vc-registered file))))))
108
109 (defun vc-toggle-read-only ()
110 "If the file in the current buffer is under version control, perform the
111 logical next version-control action; otherwise, just toggle the buffer's
112 read-only flag."
113 (interactive)
114 (if (vc-backend-deduce (buffer-file-name))
115 (vc-next-action nil)
116 (toggle-read-only)))
117
118 (defun vc-mode-line (file &optional label)
119 "Set `vc-mode-string' to display type of version control for FILE.
120 The value is set in the current buffer, which should be the buffer
121 visiting FILE."
122 (interactive (list buffer-file-name nil))
123 (let ((vc-type (vc-backend-deduce file)))
124 (if vc-type
125 (progn
126 (if (null (current-local-map))
127 (use-local-map (make-sparse-keymap)))
128 (define-key (current-local-map) "\C-x\C-q" 'vc-toggle-read-only)
129 (setq vc-mode-string
130 (concat " " (or label (symbol-name vc-type))))))
131 ;; force update of mode line
132 (set-buffer-modified-p (buffer-modified-p))
133 vc-type))
134
135 ;;; install a call to the above as a find-file hook
136 (defun vc-find-file-hook ()
137 (if (and (vc-mode-line buffer-file-name) (not vc-make-backup-files))
138 (progn
139 (make-local-variable 'make-backup-files)
140 (setq make-backup-files nil))))
141
142 (or (memq 'vc-find-file-hook find-file-hooks)
143 (setq find-file-hooks
144 (cons 'vc-find-file-hook find-file-hooks)))
145
146 ;;; more hooks, this time for file-not-found
147 (defun vc-file-not-found-hook ()
148 "When file is not found, try to check it out from RCS or SCCS.
149 Returns t if checkout was successful, nil otherwise."
150 (if (vc-backend-deduce buffer-file-name)
151 (progn
152 (require 'vc)
153 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
154
155 (or (memq 'vc-file-not-found-hook find-file-not-found-hooks)
156 (setq find-file-not-found-hooks
157 (cons 'vc-file-not-found-hook find-file-not-found-hooks)))
158
159 ;;; Now arrange for bindings and autoloading of the main package.
160 ;;; Bindings for this have to go in the global map, as it may have
161 ;;; to coexist with a lot of different major modes.
162
163 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
164 (if (not (keymapp vc-prefix-map))
165 (progn
166 (setq vc-prefix-map (make-sparse-keymap))
167 (define-key global-map "\C-xv" vc-prefix-map)
168 (define-key vc-prefix-map "a" 'vc-update-change-log)
169 (define-key vc-prefix-map "c" 'vc-cancel-version)
170 (define-key vc-prefix-map "d" 'vc-directory)
171 (define-key vc-prefix-map "h" 'vc-insert-headers)
172 (define-key vc-prefix-map "i" 'vc-register)
173 (define-key vc-prefix-map "l" 'vc-print-log)
174 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
175 (define-key vc-prefix-map "s" 'vc-create-snapshot)
176 (define-key vc-prefix-map "u" 'vc-revert-buffer)
177 (define-key vc-prefix-map "v" 'vc-next-action)
178 (define-key vc-prefix-map "=" 'vc-diff)
179 ))
180
181 (provide 'vc-hooks)
182
183 ;;; vc-hooks.el ends here