Remove old and crusty tooltalk pkg
[packages] / xemacs-packages / vc-cc / vc-cc-hooks.el
1 ;;; vc-cc-hooks.el --- support for vc-cc.el, formerly resident
2
3 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: ttn@netcom.com
8 ;; Version: 5.3 + CVS hacks by ceder@lysator.liu.se made in Jan-Feb 1994.
9 ;;
10 ;; XEmacs fixes, CVS fixes, and general improvements
11 ;; by Jonathan Stigelman <Stig@hackvan.com>
12
13 ;; This file is part of XEmacs.
14
15 ;; XEmacs is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; XEmacs is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 ;; General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with XEmacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Synched up with: FSF 19.28.
31
32 ;;; Commentary:
33
34 ;; This is like vc-hooks.el, but for vc-cc.  See the commentary of
35 ;; vc-hooks.el and vc-cc.el.
36
37 ;;; Code:
38
39 (eval-and-compile
40   (require 'advice))
41
42 ;; Using defconst only because we may have already loaded another version of
43 ;; this library!
44 (defconst vc-master-templates
45   '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
46     ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
47     vc-cc-registered
48     vc-find-cvs-master)
49   "*Where to look for version-control master files.
50 The first pair corresponding to a given back end is used as a template
51 when creating new masters.")
52
53 (defconst ClearCase "@@")
54
55 (defvar vc-path
56   (if (file-exists-p "/usr/sccs")
57       '("/usr/sccs") nil)
58   "*List of extra directories to search for version control commands.")
59
60 (defvar vc-make-backup-files nil
61   "*If non-nil, backups of registered files are made as with other files.
62 If nil (the default), files covered by version control don't get backups.")
63
64 (defvar vc-follow-symlinks 'ask
65   "*Indicates what to do if you visit a symbolic link to a file
66 that is under version control.  Editing such a file through the
67 link bypasses the version control system, which is dangerous and
68 probably not what you want.
69   If this variable is t, VC follows the link and visits the real file,
70 telling you about it in the echo area.  If it is `ask', VC asks for
71 confirmation whether it should follow the link.  If nil, the link is
72 visited and a warning displayed.")
73
74 (defvar vc-display-status t
75   "*If non-nil, display revision number and lock status in modeline.
76 Otherwise, not displayed.")
77
78 (defvar vc-cc-display-branch t
79   "*If non-nil, full branch name of ClearCase working file displayed in modeline.
80 Otherwise, just the version number or label is displayed.")
81
82 (defvar vc-auto-dired-mode t
83   "*If non-nil, automatically enter `vc-dired-mode' in dired-mode buffers where
84 version control is set-up.")
85
86 (defvar vc-cc-pwv nil ;; (getenv "CLEARCASE_ROOT")
87   "The ClearCase present working view for the current buffer.")
88 (make-variable-buffer-local 'vc-cc-pwv)
89
90 (defvar vc-consult-headers t
91   "*If non-nil, identify work files by searching for version headers.")
92
93 (defconst vc-elucidated (string-match "Lucid" emacs-version))
94
95 ;; Tell Emacs about this new kind of minor mode
96 (if (not (assoc 'vc-mode minor-mode-alist))
97     (setq minor-mode-alist (cons '(vc-mode vc-mode)
98                                  minor-mode-alist)))
99 ;; We don't really need to have the toggling feature provided by this command,
100 ;; so in deference to FSF Emacs, I won't use it.
101 ;;(add-minor-mode 'vc-mode 'vc-mode)
102
103 (defvar vc-mode nil)                    ; used for modeline flag
104 (make-variable-buffer-local 'vc-mode)
105 (set-default 'vc-mode nil)
106 (put 'vc-mode 'permanent-local t)
107
108 (defvar vc-dired-mode nil)
109 (make-variable-buffer-local 'vc-dired-mode)
110
111 ;; We need a notion of per-file properties because the version
112 ;; control state of a file is expensive to derive --- we don't
113 ;; want to recompute it even on every find.
114
115 (defmacro vc-error-occurred (&rest body)
116   (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
117
118 (defvar vc-file-prop-obarray (make-vector 17 0)
119   "Obarray for per-file properties.")
120
121 (defun vc-file-setprop (file property value)
122   ;; set per-file property
123   (put (intern file vc-file-prop-obarray) property value))
124
125 (defun vc-file-getprop (file property)
126   ;; get per-file property
127   (get (intern file vc-file-prop-obarray) property))
128
129 (defun vc-file-clearprops (file)
130   ;; clear all properties of a given file
131   (setplist (intern file vc-file-prop-obarray) nil))
132
133 ;;; actual version-control code starts here
134
135 (defun vc-registered (file)
136   (let (handler)
137     (if (boundp 'file-name-handler-alist)
138         (setq handler (find-file-name-handler file 'vc-registered)))
139     (if handler
140         (funcall handler 'vc-registered file)
141       ;; Search for a master corresponding to the given file
142       (let ((dirname (or (file-name-directory file) ""))
143             (basename (file-name-nondirectory file)))
144         (catch 'found
145           (mapcar
146            (function
147             (lambda (s)
148               (if (atom s)
149                   (funcall s dirname basename)
150                 (let ((trial (format (car s) dirname basename)))
151                   (if (and (file-exists-p trial)
152                            ;; This ensures that directories are not considered
153                            ;; to be registered files (this happens with the
154                            ;; third RCS pattern in vc-master-templates).
155                            (not (equal basename ""))
156                            ;; Make sure the file we found with name
157                            ;; TRIAL is not the source file itself.
158                            ;; That can happen with RCS-style names
159                            ;; if the file name is truncated
160                            ;; (e.g. to 14 chars).  See if either
161                            ;; directory or attributes differ.
162                            (or (not (string= dirname
163                                              (file-name-directory trial)))
164                                (not (equal
165                                      (file-attributes file)
166                                      (file-attributes trial)))))
167                       (throw 'found (cons trial (cdr s))))))))
168            vc-master-templates)
169           nil)))))
170
171 (defun vc-cc-registered (dirname basename)
172   ;; Check if DIRNAME/BASENAME is a ClearCase element
173   ;; If it is, do a (throw 'found (cons MASTER '@@)).
174   ;; Use general purpose function for real check
175   ;; This should only be used in vc-master-template.
176   (let ((fullname (concat dirname basename)))
177     ;; If this is a symlink to a ClearCase file, it will think that it is
178     ;; under control, but won't be able to get all information with
179     ;; vc-fetch-properties.  We should leave it up to the user to chase the
180     ;; link, or simply not edit the file through the link.
181     (if (and (not (file-symlink-p fullname))
182              (clearcase-element-p fullname))
183         (throw 'found (cons fullname '@@))))
184   )
185
186 (defun vc-find-cvs-master (dirname basename)
187   ;; Check if DIRNAME/BASENAME is handled by CVS.
188   ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
189   ;; Note: If the file is ``cvs add''ed but not yet ``cvs commit''ed
190   ;; the MASTER will not actually exist yet.  The other parts of VC
191   ;; checks for this condition.  This function returns something random if
192   ;; DIRNAME/BASENAME is not handled by CVS.
193   ;; This should only be used in vc-master-template.
194   (and (string= "" dirname) (setq dirname default-directory))
195   (if (and (file-directory-p (concat dirname "CVS/"))
196            (file-readable-p (concat dirname "CVS/Entries")))
197       (let ((fname (concat dirname basename))
198             sbuf rev)
199         (unwind-protect
200             (save-excursion
201               (set-buffer (generate-new-buffer " vc-scratch"))
202               (setq sbuf (current-buffer))
203               (insert-file-contents (concat dirname "CVS/Entries"))
204               (cond
205                ((re-search-forward
206                  (concat "^/" (regexp-quote basename) "/\\([0-9.]*\\)/.*/\\(T\\([^/\n]+\\)\\)?$")
207                  nil t)
208                 ;; We found it.  Store version number, and branch tag
209                 (setq rev (buffer-substring (match-beginning 1)
210                                             (match-end 1)))
211                 (vc-file-setprop fname 'vc-your-latest-version rev)
212                 ;; XEmacs - we put something useful in the modeline
213                 (vc-file-setprop fname 'sticky-tag
214                                  (cond ((string= "0" rev) "newfile")
215                                        ((match-beginning 3)
216                                         (buffer-substring (match-beginning 3)
217                                                           (match-end 3)))
218                                        (t "main")))
219                 (erase-buffer)
220                 (insert-file-contents (concat dirname "CVS/Repository"))
221                 (let ((master
222                        (concat (file-name-as-directory
223                                 (buffer-substring (point-min)
224                                                   (1- (point-max))))
225                                basename
226                                ",v")))
227                   (throw 'found (cons master 'CVS))))))
228           (kill-buffer sbuf)))))
229
230 (defun vc-name (file)
231   "Return the master name of a file, nil if it is not registered."
232   (or (vc-file-getprop file 'vc-name)
233       (let ((name-and-type (vc-registered file)))
234         (if name-and-type
235             (progn
236               (vc-file-setprop file 'vc-backend (cdr name-and-type))
237               (vc-file-setprop file 'vc-name (car name-and-type)))))))
238
239 (defun vc-backend-deduce (file)
240   "Return the version-control type of a file, nil if it is not registered."
241   (and file
242        (or (vc-file-getprop file 'vc-backend)
243            (let ((name-and-type (vc-registered file)))
244              (if name-and-type
245                  (progn
246                    (vc-file-setprop file 'vc-name (car name-and-type))
247                    (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
248
249 (defun vc-toggle-read-only (&optional verbose)
250   "Change read-only status of current buffer, perhaps via version control.
251 If the buffer is visiting a file registered with a form of version control
252 that locks files by making them read-only (i.e.: not CVS), then check the
253 file in or out.  Otherwise, just change the read-only flag of the buffer.
254
255 If you provide a prefix argument, we pass it on to `vc-next-action'."
256   (interactive "P")
257   (let ((vc-type (vc-backend-deduce (buffer-file-name))))
258     (cond ((and vc-type
259                 buffer-read-only
260                 (file-writable-p buffer-file-name)
261                 (/= 0 (user-uid)))
262            ;; XEmacs - The buffer isn't read-only because it's locked, so
263            ;; keep vc out of this...
264            (toggle-read-only))
265           ((and vc-type (not (eq 'CVS  vc-type)))
266            (vc-next-action verbose))
267           (t
268            (toggle-read-only)))
269     ))
270
271 ;; Map the vc-toggle-read-only key whereever toggle-read-only was
272 (let ((where (where-is-internal 'toggle-read-only global-map)))
273   (if where
274       (mapcar (lambda (key)
275                 (define-key global-map
276                   key 'vc-toggle-read-only))
277               where))
278   )
279 ;;(define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
280
281 ;; For other cases, try advising...
282 (defadvice toggle-read-only (around vc activate)
283   "If file is under version control, perform `vc-next-action'."
284   (if vc-mode
285       (let ((vc-type (vc-backend-deduce (buffer-file-name))))
286         (cond ((and vc-type
287                     buffer-read-only
288                     (file-writable-p buffer-file-name)
289                     (/= 0 (user-uid)))
290                ;; XEmacs - The buffer isn't read-only because it's locked, so
291                ;; keep vc out of this...
292                ad-do-it)
293               ((and vc-type (not (eq 'CVS  vc-type)))
294                (vc-next-action (ad-get-arg 0)))
295               (t ad-do-it))
296         )
297     ad-do-it
298     ))
299
300 (defun vc-file-owner (file)
301   ;; XEmacs - vc-locking-user is just WAY too slow.
302   (let* ((fa (file-attributes file)))
303     (cond ((eq ?w (aref (nth 8 fa) 2))  ; -rw-r--r--
304            ;; #### - if it's writable, we trust unix...dumb move?
305            (user-login-name (nth 2 fa)))
306           (t
307            ;; big slowness here...
308            (require 'vc)
309            (vc-locking-user file)
310            ))))
311
312 (defun vc-mode-line (file &optional label)
313   "Set `vc-mode' to display type of version control for FILE.
314 The value is set in the current buffer, which should be the buffer
315 visiting FILE.  Second optional arg LABEL is put in place of version
316 control system name."
317   (interactive (list buffer-file-name nil))
318   (if file
319       (let ((vc-type (vc-backend-deduce file)))
320         (setq vc-mode
321               (if vc-type
322                   (concat " " (or label (symbol-name vc-type))
323                           (if vc-display-status
324                               (vc-status file vc-type)))))
325         ;; Even root shouldn't modify a registered file without
326         ;; locking it first.
327         (and vc-type
328              (not (string= (user-login-name) (vc-file-owner file)))
329              (setq buffer-read-only t))
330         (and (null vc-type)
331              (file-symlink-p file)
332              (let ((link-type (vc-backend-deduce (file-symlink-p file))))
333                (if link-type
334                    (message
335                     "Warning: symbolic link to %s-controlled source file"
336                     link-type))))
337         (redraw-modeline)
338         ;;(set-buffer-modified-p (buffer-modified-p))  ;;use this if Emacs 18
339         vc-type)))
340
341 (defun vc-status (file vc-type)
342   ;; Return string for placement in modeline by `vc-mode-line'.
343   ;; If FILE is not registered, return nil.
344   ;; If FILE is registered but not locked, return " REV" if there is a head
345   ;; revision and " @@" otherwise.
346   ;; If FILE is locked then return all locks in a string of the
347   ;; form " LOCKER1:REV1 LOCKER2:REV2 ...", where "LOCKERi:" is empty if you
348   ;; are the locker, and otherwise is the name of the locker followed by ":".
349
350   ;; Algorithm:
351
352   ;; Check for master file corresponding to FILE being visited.
353   ;;
354   ;; RCS: Insert the first few characters of the master file into a
355   ;; work buffer.  Search work buffer for "locks...;" phrase; if not
356   ;; found, then keep inserting more characters until the phrase is
357   ;; found.  Extract the locks, and remove control characters
358   ;; separating them, like newlines; the string " user1:revision1
359   ;; user2:revision2 ..." is returned.
360   ;;
361   ;; SCCS: Check if the p-file exists.  If it does, read it and
362   ;; extract the locks, giving them the right format.  Else use prs to
363   ;; find the revision number.
364   ;;
365   ;; CVS: vc-find-cvs-master has already stored the current revision
366   ;; number and sticky-tag for the file.  XEmacs displays the sticky-tag.
367
368   ;; Limitations:
369
370   ;; The output doesn't show which version you are actually looking at.
371   ;; The modeline can get quite cluttered when there are multiple locks.
372   ;; The head revision is probably not what you want if you've used `rcs -b'.
373
374   (let ((master (vc-name file))
375         found
376         status)
377
378     ;; If master file exists, then parse its contents, otherwise we
379     ;; return the nil value of this if form.
380     (if (and master vc-type)
381         (save-excursion
382
383           ;; Create work buffer.
384           (set-buffer (get-buffer-create " *vc-status*"))
385           (setq buffer-read-only nil
386                 default-directory (file-name-directory master))
387           (erase-buffer)
388
389           ;; Set the `status' var to the return value.
390           (cond
391
392            ;; RCS code.
393            ((eq vc-type 'RCS)
394             ;; Check if we have enough of the header.
395             ;; If not, then keep including more.
396             (while
397                 (not (or found
398                          (let ((s (buffer-size)))
399                            (goto-char (1+ s))
400                            (zerop (car (cdr (insert-file-contents
401                                              master nil s (+ s 8192))))))))
402               (beginning-of-line)
403               (setq found (re-search-forward "^locks\\([^;]*\\);" nil t)))
404
405             (if found
406                 ;; Clean control characters and self-locks from text.
407                 (let* ((lock-pattern
408                         (concat "[ \b\t\n\v\f\r]+\\("
409                                 (regexp-quote (user-login-name))
410                                 ":\\)?"))
411                        (locks
412                         (save-restriction
413                           (narrow-to-region (match-beginning 1) (match-end 1))
414                           (goto-char (point-min))
415                           (while (re-search-forward lock-pattern nil t)
416                             (replace-match (if (eobp) "" ":") t t))
417                           (buffer-string))))
418                   (setq status
419                         (if (not (string-equal locks ""))
420                             locks
421                           (goto-char (point-min))
422                           (if (looking-at "head[ \b\t\n\v\f\r]+\\([.0-9]+\\)")
423                               (concat "-"
424                                       (buffer-substring (match-beginning 1)
425                                                         (match-end 1)))
426                             " @@"))))))
427
428            ;; SCCS code.
429            ((eq vc-type 'SCCS)
430             ;; Build the name of the p-file and put it in the work buffer.
431             (insert master)
432             (search-backward "/s.")
433             (delete-char 2)
434             (insert "/p")
435             (if (not (file-exists-p (buffer-string)))
436                 ;; No lock.
437                 (let ((exec-path (if (boundp 'vc-path) (append exec-path vc-path)
438                                    exec-path)))
439                   (erase-buffer)
440                   (insert "-")
441                   (if (zerop (call-process "prs" nil t nil "-d:I:" master))
442                       (setq status (buffer-substring 1 (1- (point-max))))))
443               ;; Locks exist.
444               (insert-file-contents (buffer-string) nil nil nil t)
445               (while (looking-at "[^ ]+ \\([^ ]+\\) \\([^ ]+\\).*\n")
446                 (replace-match " \\2:\\1"))
447               (setq status (buffer-string))
448               (aset status 0 ?:)))
449            ;; CVS code.
450            ((eq vc-type 'CVS)
451             ;; sticky-tag is initialized by vc-backend-deduce
452             (setq status (concat ":" (vc-file-getprop file 'sticky-tag) "-"
453                                  (vc-file-getprop file 'vc-your-latest-version)
454                                  ))
455             )
456            ;; ClearCase code.
457            ((eq vc-type '@@)
458             (require 'vc)
459             ;; Display the explicitly specified version or the latest version
460             (let ((version (or (vc-cc-version-name file)
461                                (vc-latest-version file)
462                                ;; Make sure version is a string in case the
463                                ;; file is not really a versioned object
464                                "")))
465               ;; Check if the user wants to see the branch
466               (if vc-cc-display-branch
467                   (setq status version)
468                 (setq status (concat ":" (file-name-nondirectory version))))
469               ))
470            )
471
472           ;; Clean work buffer.
473           (erase-buffer)
474           (set-buffer-modified-p nil)
475           status))))
476
477 ;;;;; install a call to the above as a find-file hook
478
479 (defun vc-follow-link ()
480   ;; If the current buffer visits a symbolic link, this function makes it
481   ;; visit the real file instead.  If the real file is already visited in
482   ;; another buffer, make that buffer current, and kill the buffer
483   ;; that visits the link.
484   (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
485          (true-buffer (find-buffer-visiting truename))
486          (this-buffer (current-buffer)))
487     (if (eq true-buffer this-buffer)
488         (progn
489           (kill-buffer this-buffer)
490           ;; In principle, we could do something like set-visited-file-name.
491           ;; However, it can't be exactly the same as set-visited-file-name.
492           ;; I'm not going to work out the details right now. -- rms.
493           (set-buffer (find-file-noselect truename)))
494       (set-buffer true-buffer)
495       (kill-buffer this-buffer))))
496
497 (defun vc-set-auto-mode ()
498   "Check again for the mode of the current buffer when using ClearCase version extended paths."
499
500   (if (eq (vc-file-getprop buffer-file-name 'vc-backend) '@@)
501       (let* ((version (vc-cc-version-name buffer-file-name))
502              (buffer-file-name (vc-cc-element-name buffer-file-name)))
503         ;; Need to recheck the major mode only if a version was appended
504         (if version (set-auto-mode))
505         ;; Set a buffer-local variable for the working view
506         (setq vc-cc-pwv (vc-cc-pwv buffer-file-name))
507         ))
508   )
509
510 (defun vc-find-file-hook ()
511   ;; Recompute whether file is version controlled,
512   ;; if user has killed the buffer and revisited.
513   (cond
514    (buffer-file-name
515     (vc-file-clearprops buffer-file-name)
516     (cond
517      ((vc-backend-deduce buffer-file-name)
518       (vc-mode-line buffer-file-name)
519       (cond ((not vc-make-backup-files)
520              ;; Use this variable, not make-backup-files,
521              ;; because this is for things that depend on the file name.
522              (make-local-variable 'backup-inhibited)
523              (setq backup-inhibited t)))
524       (vc-set-auto-mode))
525      ((let* ((link (file-symlink-p buffer-file-name))
526              (link-type (and link (vc-backend-deduce (file-chase-links link)))))
527         (if link-type
528             (cond ((null vc-follow-symlinks)
529                    (message
530                     "Warning: symbolic link to %s-controlled source file" link-type))
531                   ((or (not (eq vc-follow-symlinks 'ask))
532                        ;; If we already visited this file by following
533                        ;; the link, don't ask again if we try to visit
534                        ;; it again.  GUD does that, and repeated questions
535                        ;; are painful.
536                        (let ((find-file-compare-truenames nil))
537                          ;; If compare-truenames is t, this will always be t
538                          (get-file-buffer
539                           (abbreviate-file-name (file-chase-links buffer-file-name)))))
540
541                    (vc-follow-link)
542                    (message "Followed link to %s" buffer-file-name)
543                    (vc-find-file-hook))
544                   (t
545                    (if (yes-or-no-p
546                         (format
547                          "Symbolic link to %s-controlled source file; follow link? "
548                          link-type))
549                        (progn (vc-follow-link)
550                               (message "Followed link to %s" buffer-file-name)
551                               (vc-find-file-hook))
552                      (message
553                       "Warning: editing through the link bypasses version control")
554                      ))))
555         (vc-set-auto-mode)))
556       ))))
557
558 ;;; install a call to the above as a find-file hook
559 (add-hook 'find-file-hooks 'vc-find-file-hook)
560
561 ;; Handle ClearCase version files correctly.
562 ;;
563 ;; This little bit of magic causes the buffer name to be set to
564 ;; <filename>@@/<branch path>/<version>, if you find a specific version of
565 ;; a file.  Without this the name of the buffer will just be the version
566 ;; number.
567
568 (defun vc-check-cc-name ()
569   (let ((match (string-match "@@" default-directory)))
570     (if match
571         (progn
572           (while (and (> match 0)
573                       (not (equal (elt default-directory match)
574                                   (string-to-char "/"))))
575             (setq match (1- match)))
576
577
578           (let ((new-buffer-name
579                  (concat (substring default-directory (1+ match))
580                          (buffer-name)))
581                 (new-dir
582                  (substring default-directory 0 (1+ match))))
583             (or (string= new-buffer-name (buffer-name))
584                 ;; Uniquify the name, if necessary.
585                 ;;
586                 (let ((n 2)
587                       (uniquifier-string ""))
588                   (while (get-buffer (concat new-buffer-name uniquifier-string))
589                     (setq uniquifier-string (format "<%d>" n))
590                     (setq n (1+ n)))
591                   (rename-buffer
592                    (concat new-buffer-name uniquifier-string))))
593             (setq default-directory new-dir)))
594         nil)))
595
596 (add-hook 'find-file-hooks 'vc-check-cc-name)
597
598 (defun vc-find-dir-hook ()
599   ;; Recompute whether file is version controlled,
600   ;; if user has killed the buffer and revisited.
601   (vc-file-clearprops default-directory)
602   (if default-directory
603       (vc-file-setprop default-directory 'vc-backend nil))
604
605   ;; Let dired decide whether the file should be read-only
606   (let (buffer-read-only)
607     (vc-mode-line default-directory))
608
609   ;; If in vc-dired-mode, reformat the buffer
610   (if vc-dired-mode
611       (vc-reformat-dired-buffer)
612     ;; Otherwise, check if we should automatically enter vc-dired-mode
613     (let ((default-directory (dired-current-directory)))
614       (if (and vc-auto-dired-mode
615                (or vc-mode
616                    (file-directory-p "SCCS")
617                    (file-directory-p "RCS")
618                    (file-directory-p "CVS")))
619         (vc-dired-mode 1))))
620   )
621
622 (add-hook 'dired-after-readin-hook 'vc-find-dir-hook)
623
624 ;;; more hooks, this time for file-not-found
625 (defun vc-file-not-found-hook ()
626   "When file is not found, try to check it out from RCS or SCCS.
627 Returns t if checkout was successful, nil otherwise."
628   (if (vc-backend-deduce buffer-file-name)
629       (save-excursion
630         (require 'vc)
631         (not (vc-error-occurred (vc-checkout buffer-file-name))))))
632
633 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
634
635 ;;; Now arrange for bindings and autoloading of the main package.
636 ;;; Bindings for this have to go in the global map, as we'll often
637 ;;; want to call them from random buffers.
638
639 (add-to-list 'file-name-handler-alist '("^/view/[^/]+/" . vc-cc-file-handler))
640 (add-to-list 'file-name-handler-alist '("^/view[/]*$" . vc-cc-view-handler))
641
642 ; XEmacs - this is preloaded.  let's not be obtuse!
643 (defconst vc-prefix-map
644   (let ((map (make-sparse-keymap)))
645     (set-keymap-name map 'vc-prefix-map)
646     (define-key map "a" 'vc-update-change-log)
647     (define-key map "c" 'vc-cancel-version)
648     (define-key map "d" 'vc-directory)
649     (define-key map "h" 'vc-insert-headers)
650     (define-key map "i" 'vc-register)
651     (define-key map "l" 'vc-print-log)
652     (define-key map "n" 'vc-assign-name)
653     (define-key map "r" 'vc-retrieve-snapshot)
654     (define-key map "s" 'vc-create-snapshot)
655     (define-key map "u" 'vc-revert-buffer)
656     (define-key map "v" 'vc-next-action)
657     (define-key map "=" 'vc-diff)
658     (define-key map "?" 'vc-file-status) ; XEmacs - this doesn't fit elsewhere
659     (define-key map "~" 'vc-version-other-window)
660     ;; ClearCase only stuff
661     (define-key map "e" 'vc-edit-config)
662     (define-key map "b" 'vc-cc-browse-versions)
663     (define-key map "m" 'vc-mkbrtype)
664     (define-key map "t" 'vc-graph-history)
665     (define-key map "w" 'vc-cc-what-rule)
666     (global-set-key "\C-xv" map)
667     map
668     ))
669
670 ;;; Emacs 19 menus.
671 (if (and (not vc-elucidated) (boundp 'menu-bar-final-items))
672     (progn
673       (defvar menu-bar-vc-menu
674         (make-sparse-keymap "VC"))
675       (or (memq
676            'vc menu-bar-final-items)
677           (setq menu-bar-final-items
678                 (cons
679                  'vc menu-bar-final-items)))
680       (define-key menu-bar-vc-menu [vc-report-bug]
681         '("Report Bug in Clearcase VC" . cc-vc-submit-bug-report))
682       (define-key menu-bar-vc-menu [vc-directory-registered]
683         '("List Registered Files" . vc-directory-registered))
684       (define-key menu-bar-vc-menu [vc-directory]
685         '("List Locked Files Any User" . vc-directory))
686       (define-key menu-bar-vc-menu [vc-directory-locked]
687         '("List Locked Files" . vc-directory-locked))
688       (put 'vc-print-log 'menu-enable 'vc-mode)
689       (define-key menu-bar-vc-menu [vc-print-log]
690         '("Show Edit History" . vc-print-log))
691       (put 'vc-version-other-window 'menu-enable 'vc-mode)
692       (define-key menu-bar-vc-menu [vc-version-other-window]
693         '("Visit Previous Revision..." . vc-version-other-window))
694       (put 'vc-diff-since-revision 'menu-enable 'vc-mode)
695       (define-key menu-bar-vc-menu [vc-diff-since-revision]
696         '("Show Changes Since Revision..." . vc-diff-since-revision))
697       (put 'vc-diff 'menu-enable 'vc-mode)
698       (define-key menu-bar-vc-menu [vc-diff]
699         '("Show Changes" . vc-diff))
700       (put 'vc-rename-this-file 'menu-enable 'vc-mode)
701       (define-key menu-bar-vc-menu [vc-rename-this-file]
702         '("Rename File" . vc-rename-this-file))
703       (put 'vc-revert-buffer 'menu-enable 'vc-mode)
704       (define-key menu-bar-vc-menu [vc-revert-buffer]
705         '("Revert File" . vc-revert-buffer))
706       (define-key menu-bar-vc-menu [vc-update-directory]
707         '("Update Current Directory" . vc-update-directory))
708       (define-key menu-bar-vc-menu [next-action]
709         '("Next Action" . vc-next-action))
710       (define-key global-map [menu-bar vc]
711         (cons "VC" menu-bar-vc-menu))))
712
713 ;;; Lucid Emacs menus..
714 (defconst vc-default-menu
715   '(:filter vc-menu-filter
716     ["NEXT-OPERATION"                   vc-next-action                  t nil]
717     ["Update Current Directory"         vc-update-directory             t]
718     "----"
719     ["Revert to Last Revision"          vc-revert-buffer                t nil]
720     ["Cancel Last Checkin"              vc-cancel-version               t nil]
721     ["Rename File"                      vc-rename-this-file             t nil]
722     "----"
723     ["Diff Against Last Version"        vc-diff                         t]
724     ["Diff Between Revisions..."        vc-diff-since-revision          t]
725     ["Visit Other Version..."           vc-version-other-window         t]
726     ["Show Edit History"                vc-print-log                    t]
727     ["Assign Label..."                  vc-assign-name                  t]
728     "----"
729     ["List Locked Files"                (progn
730                                           (setq current-prefix-arg '(16))
731                                           (call-interactively 'vc-directory)) t]
732     ["List Locked Files Any User"       vc-directory                    t]
733     ["List Registered Files"            (progn
734                                           (setq current-prefix-arg '(4))
735                                           (call-interactively 'vc-directory)) t])
736   "Menubar entry for using the revision control system.")
737
738 (defconst vc-cvs-menu
739   '(:filter vc-menu-filter
740     ["Update Current Directory"         vc-cvs-update-directory         t]
741     ["Revert File"                      vc-revert-file                  t nil]
742     "----"
743     ["Show Changes"                     vc-show-changes                 t]
744     ["Show Changes Since Revision..."   vc-show-revision-changes        t]
745     ["Visit Previous Revision..."       vc-version-other-window         t]
746     ["Show File Status"                 vc-cvs-file-status              t]
747     ["Show Edit History"                vc-show-history                 t])
748   "Menubar entry for using the revision control system with CVS.")
749
750 (defconst vc-cc-menu
751   '(["Edit Config Spec..." vc-edit-config t]
752     ["Browse Versions"     vc-cc-browse-versions t]
753     ["Make Branch Type..." vc-mkbrtype t]
754     ["View Graph History"  vc-graph-history t]
755     ["Show Rule"           vc-cc-what-rule t])
756   "Menubar entries to add to the VC menu when using ClearCase.")
757
758 ;; This function may be called as a menubar hook, or as a menu filter
759 ;; The filter is much more efficient, and doesn't slow down menubar selection
760 ;; for every single menu action, as does the hook method.
761 (defun vc-menu-filter (&optional menu)
762   (if (null menu)
763       (progn
764         (setq menu (find-menu-item current-menubar '("Tools" "Version Control")))
765         ;; Get just the menu portion
766         (if menu (setq menu (cdr (car menu))))
767         ))
768   (if (null menu)
769       nil
770     (let* ((rest menu)
771            (case-fold-search t)
772            (filepath (cond ((and vc-dired-mode
773                                  (dired-get-filename nil 'no-error)))
774                            (buffer-file-name)
775                            (t (buffer-name))))
776            (file (and filepath (file-name-nondirectory filepath)))
777            (vc-file (and filepath (vc-name filepath)))
778            owner
779            command
780            item)
781       (while rest
782         (setq item (car rest))
783
784         (if (not (vectorp item))
785             nil
786           (setq command (aref item 1))
787           (cond
788            ;; Display the correct action for vc-next-action command
789            ((eq 'vc-next-action command)
790             (aset item 0
791                   (cond ((not vc-file)
792                          "Register File")
793                         ((not (setq owner
794                                     ;; Just check properties, it's too
795                                     ;; slow (and dangerous) to fetch
796                                     ;; properties
797                                     (vc-file-getprop filepath 'vc-locking-user)))
798                          ;;(vc-locking-user filepath)))
799                          "Check out File")
800                         ((not (string-equal owner (user-login-name)))
801                          "Steal File Lock")
802                         (t "Check in File")))
803             (aset item 2 (or buffer-file-name
804                              (and vc-dired-mode "Marked")))
805             )
806
807            ;; Check for commands to disable
808            ((memq command
809                   '(vc-revert-buffer
810                     vc-cancel-version
811                     vc-rename-this-file
812                     vc-diff
813                     vc-diff-since-revision
814                     vc-version-other-window
815                     vc-visit-previous-revision
816                     vc-print-log
817                     vc-assign-name))
818             (aset item 2 vc-file))
819
820            (t nil))
821
822           ;; Add the file to the menu suffix if not disabled
823           (if (and (> (length item) 3) (aref item 2))
824               (aset item 3 
825                     (if vc-dired-mode "Marked" file)))
826           )
827
828         (setq rest (cdr rest)))
829
830       ;; Return menu plus the ClearCase menu if needed
831       (if (and vc-file (clearcase-element-p filepath))
832           ;; Must use append here - nconc will create looped list
833           (append menu '("----") vc-cc-menu)
834           menu)
835       )))
836
837 ;; vc-menu-filter was once called vc-sensitize-menu, so just in case another
838 ;; version of vc was loaded:
839 (defalias 'vc-sensitize-menu 'vc-menu-filter)
840
841 (if (and (fboundp 'add-submenu) (not (featurep 'infodock)) vc-elucidated)
842     (progn
843       (add-submenu '("Tools") (append (list "Version Control") vc-default-menu))
844 ;; Only add the hook if the :filter method is unavailable.  I don't know which
845 ;; version of XEmacs introduced it, but it's been available at least since 19.13
846 ;;      (add-hook 'activate-menubar-hook 'vc-sensitize-menu)))
847       ))
848
849 ;; #### called by files.el.  Define it like this until we're merged.
850 (defun vc-after-save ())
851
852 ;;---------------------------------------------------------------------------
853 ;; Utility functions for ClearCase
854 ;;---------------------------------------------------------------------------
855
856 (defun clearcase-element-p (path)
857   "Determine if PATH refers to a Clearcase element."
858
859   (let (extended-path versioned-path)
860
861     (if (string-match "@@" path)
862         (setq extended-path (substring path 0 (match-end 0))
863               versioned-path t)
864       (setq extended-path (concat path "@@")))
865
866     (and (file-exists-p path)
867          (file-directory-p extended-path)
868
869          ;; Non-checked-out elements have the same inode-number
870          ;; as the extended name ("foo@@").
871          ;; Not so for checked out, and therefore writeable elements.
872          ;;
873          (or (file-writable-p path)
874              versioned-path
875              (eq (file-inode path)
876                  (file-inode extended-path)))
877          )))
878
879 (if (not (fboundp 'file-inode))
880     (defun file-inode (file)
881       (nth 10 (file-attributes file))))
882
883 (defun vc-cc-element-name (path)
884   (if (string-match "@@" path)
885       (substring path 0 (match-beginning 0))
886     path))
887
888 (defun vc-cc-version-name (path)
889   (if (string-match "@@" path)
890       (substring path (match-end 0))
891     nil))
892
893 (defsubst vc-cc-relpath (str)
894   (and str
895        (stringp str)
896        (string-match "^/view/\\([^/]+\\)" str)
897        (substring str
898                   (match-end 1))))
899
900 (defun vc-cc-build-version (file version &optional view-tag)
901   "Build a ClearCase version-extended pathname for ELEMENT's version VERSION.
902 If ELEMENT is actually a version-extended pathname, substitute VERSION for
903 the version included in ELEMENT.  If VERSION is nil, remove the version-extended
904 pathname.
905
906 If optional VIEW-TAG is specified, make a view-relative pathname, possibly
907 replacing the existing view prefix."
908   (let* ((element (vc-cc-element-name file))
909          (glue-fmt (if (and (> (length version) 0)
910                             (= (aref version 0) ?/))
911                        "%s@@%s"
912                      "%s@@/%s"))
913          (relpath (vc-cc-relpath element)))
914     (if view-tag
915         (setq element (concat "/view/" view-tag (or relpath element))))
916     (if version
917         (format glue-fmt element version)
918       element)
919     ))
920
921 ;; These stolen from vc.  pcl-cvs wants to call these in
922 ;; cvs-mark-buffer-changed.  (Basically only changed vc-backend to
923 ;; vc-backend-deduce.)
924
925 (defun vc-consult-rcs-headers (file)
926   ;; Search for RCS headers in FILE, and set properties
927   ;; accordingly.  This function can be disabled by setting
928   ;; vc-consult-headers to nil.  
929   ;; Returns: nil            if no headers were found 
930   ;;                         (or if the feature is disabled,
931   ;;                         or if there is currently no buffer 
932   ;;                         visiting FILE)
933   ;;          'rev           if a workfile revision was found
934   ;;          'rev-and-lock  if revision and lock info was found 
935   (cond
936    ((or (not vc-consult-headers) 
937         (not (get-file-buffer file))) nil)
938    ((let (status version locking-user)
939      (save-excursion
940       (set-buffer (get-file-buffer file))
941       (goto-char (point-min))
942       (cond  
943        ;; search for $Id or $Header
944        ;; -------------------------
945        ((or (and (search-forward "$Id: " nil t)
946                  (looking-at "[^ ]+ \\([0-9.]+\\) "))
947             (and (progn (goto-char (point-min))
948                         (search-forward "$Header: " nil t))
949                  (looking-at "[^ ]+ \\([0-9.]+\\) ")))
950         (goto-char (match-end 0))
951         ;; if found, store the revision number ...
952         (setq version (buffer-substring-no-properties (match-beginning 1)
953                                                       (match-end 1)))
954         ;; ... and check for the locking state
955         (cond 
956          ((looking-at
957            (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] "             ; date
958             "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
959                    "[^ ]+ [^ ]+ "))                       ; author & state
960           (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
961           (cond 
962            ;; unlocked revision
963            ((looking-at "\\$")
964             (setq locking-user 'none)
965             (setq status 'rev-and-lock))
966            ;; revision is locked by some user
967            ((looking-at "\\([^ ]+\\) \\$")
968             (setq locking-user
969                   (buffer-substring-no-properties (match-beginning 1)
970                                                   (match-end 1)))
971             (setq status 'rev-and-lock))
972            ;; everything else: false
973            (nil)))
974          ;; unexpected information in
975          ;; keyword string --> quit
976          (nil)))
977        ;; search for $Revision
978        ;; --------------------
979        ((re-search-forward (concat "\\$" 
980                                    "Revision: \\([0-9.]+\\) \\$")
981                            nil t)
982         ;; if found, store the revision number ...
983         (setq version (buffer-substring-no-properties (match-beginning 1)
984                                                       (match-end 1)))
985         ;; and see if there's any lock information
986         (goto-char (point-min))
987         (if (re-search-forward (concat "\\$" "Locker:") nil t)
988             (cond ((looking-at " \\([^ ]+\\) \\$")
989                    (setq locking-user (buffer-substring-no-properties
990                                        (match-beginning 1)
991                                        (match-end 1)))
992                    (setq status 'rev-and-lock))
993                   ((looking-at " *\\$") 
994                    (setq locking-user 'none)
995                    (setq status 'rev-and-lock))
996                   (t 
997                    (setq locking-user 'none)
998                    (setq status 'rev-and-lock)))
999           (setq status 'rev)))
1000        ;; else: nothing found
1001        ;; -------------------
1002        (t nil)))
1003      (if status (vc-file-setprop file 'vc-workfile-version version))
1004      (and (eq status 'rev-and-lock)
1005           (eq (vc-backend-deduce file) 'RCS)
1006           (vc-file-setprop file 'vc-locking-user locking-user)
1007           ;; If the file has headers, we don't want to query the master file,
1008           ;; because that would eliminate all the performance gain the headers
1009           ;; brought us.  We therefore use a heuristic for the checkout model 
1010           ;; now:  If we trust the file permissions, and the file is not 
1011           ;; locked, then if the file is read-only the checkout model is 
1012           ;; `manual', otherwise `implicit'.
1013           (not (vc-mistrust-permissions file))
1014           (not (vc-locking-user file))
1015           (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
1016               (vc-file-setprop file 'vc-checkout-model 'manual)
1017             (vc-file-setprop file 'vc-checkout-model 'implicit)))
1018      status))))
1019
1020 (defun vc-workfile-version (file)
1021   ;; Return version level of the current workfile FILE
1022   ;; This is attempted by first looking at the RCS keywords.
1023   ;; If there are no keywords in the working file, 
1024   ;; vc-master-workfile-version is taken.
1025   ;; Note that this property is cached, that is, it is only 
1026   ;; looked up if it is nil.
1027   ;; For SCCS, this property is equivalent to vc-latest-version.
1028   (cond ((vc-file-getprop file 'vc-workfile-version))
1029         ((eq (vc-backend-deduce file) 'SCCS) (vc-latest-version file))
1030         ((eq (vc-backend-deduce file) 'RCS)
1031          (if (vc-consult-rcs-headers file)
1032              (vc-file-getprop file 'vc-workfile-version)
1033            (let ((rev (cond ((vc-master-workfile-version file))
1034                             ((vc-latest-version file)))))
1035              (vc-file-setprop file 'vc-workfile-version rev)
1036              rev)))
1037         ((eq (vc-backend-deduce file) 'CVS)
1038          (if (vc-consult-rcs-headers file)   ;; CVS
1039              (vc-file-getprop file 'vc-workfile-version)
1040            (catch 'found
1041              (vc-find-cvs-master (file-name-directory file)
1042                                  (file-name-nondirectory file)))
1043            (vc-file-getprop file 'vc-workfile-version)))))
1044
1045 (provide 'vc-cc-hooks)
1046
1047 ;;; vc-cc-hooks.el ends here