| 1 | ;;; vc-dispatcher.el -- generic command-dispatcher facility. |
| 2 | |
| 3 | ;; Copyright (C) 2008 |
| 4 | ;; Free Software Foundation, Inc. |
| 5 | |
| 6 | ;; Author: FSF (see below for full credits) |
| 7 | ;; Maintainer: Eric S. Raymond <esr@thyrsus.com> |
| 8 | ;; Keywords: tools |
| 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 3 of the License, or |
| 15 | ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | ;;; Credits: |
| 26 | |
| 27 | ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode. |
| 28 | ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the |
| 29 | ;; vc-dir front end. |
| 30 | |
| 31 | ;;; Commentary: |
| 32 | |
| 33 | ;; Goals: |
| 34 | ;; |
| 35 | ;; There is a class of front-ending problems that Emacs might be used |
| 36 | ;; to address that involves selecting sets of files, or possibly |
| 37 | ;; directories, and passing the selection set to slave commands. The |
| 38 | ;; prototypical example, from which this code is derived, is talking |
| 39 | ;; to version-control systems. |
| 40 | ;; |
| 41 | ;; vc-dispatcher.el is written to decouple the UI issues in such front |
| 42 | ;; ends from their application-specific logic. It also provides a |
| 43 | ;; service layer for running the slave commands either synchronously |
| 44 | ;; or asynchronously and managing the message/error logs from the |
| 45 | ;; command runs. |
| 46 | ;; |
| 47 | ;; Similar UI problems can be expected to come up in applications |
| 48 | ;; areas other than VCSes; IDEs and document search are two obvious ones. |
| 49 | ;; This mode is intended to ensure that the Emacs interfaces for all such |
| 50 | ;; beasts are consistent and carefully designed. But even if nothing |
| 51 | ;; but VC ever uses it, getting the layer separation right will be |
| 52 | ;; a valuable thing. |
| 53 | |
| 54 | ;; Dispatcher's universe: |
| 55 | ;; |
| 56 | ;; The universe consists of the file tree rooted at the current |
| 57 | ;; directory. The dispatcher's upper layer deduces some subset |
| 58 | ;; of the file tree from the state of the currently visited buffer |
| 59 | ;; and returns that subset, presumably to a client mode. |
| 60 | ;; |
| 61 | ;; The user may be looking at either of two different views; a buffer |
| 62 | ;; visiting a file, or a directory buffer generated by vc-dispatcher. |
| 63 | ;; |
| 64 | ;; The lower layer of this mode runs commands in subprocesses, either |
| 65 | ;; synchronously or asynchronously. Commands may be launched in one |
| 66 | ;; of two ways: they may be run immediately, or the calling mode can |
| 67 | ;; create a closure associated with a text-entry buffer, to be |
| 68 | ;; executed when the user types C-c to ship the buffer contents. In |
| 69 | ;; either case the command messages and error (if any) will remain |
| 70 | ;; available in a status buffer. |
| 71 | |
| 72 | ;; Special behavior of dispatcher directory buffers: |
| 73 | ;; |
| 74 | ;; In dispatcher directory buffers, facilities to perform basic |
| 75 | ;; navigation and selection operations are provided by keymap and menu |
| 76 | ;; entries that dispatcher sets up itself, so they'll be uniform |
| 77 | ;; across all dispatcher-using client modes. Client modes are |
| 78 | ;; expected to append to these to provide mode-specific bindings. |
| 79 | ;; |
| 80 | ;; The standard map associates a 'state' slot (that the client mode |
| 81 | ;; may set) with each directory entry. The dispatcher knows nothing |
| 82 | ;; about the semantics of individual states, but mark and unmark commands |
| 83 | ;; treat all entries with the same state as the currently selected one as |
| 84 | ;; a unit. |
| 85 | |
| 86 | ;; The interface |
| 87 | ;; |
| 88 | ;; The main interface to the lower level is vc-do-command. This launches a |
| 89 | ;; comand, synchronously or asynchronously, making the output available |
| 90 | ;; in a command log buffer. Two other functions, (vc-start-annotation) and |
| 91 | ;; (vc-finish-logentry), allow you to associate a command closure with an |
| 92 | ;; abbotation buffer so that when the user confirms the comment the closure |
| 93 | ;; is run (with the comment as part of its context). |
| 94 | ;; |
| 95 | ;; The interface to the upper level has the two main entry points (vc-dir) |
| 96 | ;; and (vc-dispatcher-selection-set) and a couple of convenience functions. |
| 97 | ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set) |
| 98 | ;; returns a selection set of files, either the marked files in a browsing |
| 99 | ;; buffer or the singleton set consisting of the file visited by the current |
| 100 | ;; buffer (when that is appropriate). It also does what is needed to ensure |
| 101 | ;; that on-disk files and the contents of their visiting Emacs buffers |
| 102 | ;; coincide. |
| 103 | ;; |
| 104 | ;; When the client mode adds a local mode-line-hook to a buffer, it |
| 105 | ;; will be called with the buffer file name as argument whenever the |
| 106 | ;; dispatcher resynchs the buffer. |
| 107 | |
| 108 | ;; To do: |
| 109 | ;; |
| 110 | ;; - vc-dir-kill-dir-status-process should not be specific to dir-status, |
| 111 | ;; it should work for other async commands done through vc-do-command |
| 112 | ;; as well, |
| 113 | ;; |
| 114 | ;; - the *VC-log* buffer needs font-locking. |
| 115 | ;; |
| 116 | ;; - vc-dir needs mouse bindings. |
| 117 | ;; |
| 118 | ;; - vc-dir toolbar needs more icons. |
| 119 | ;; |
| 120 | ;; - add commands to move to the prev/next directory in vc-dir. |
| 121 | ;; |
| 122 | ;; - vc-dir-menu-map-filter hook call needs to be moved to vc.el. |
| 123 | ;; |
| 124 | |
| 125 | (provide 'vc-dispatcher) |
| 126 | |
| 127 | (eval-when-compile |
| 128 | (require 'cl)) |
| 129 | |
| 130 | ;; General customization |
| 131 | |
| 132 | (defcustom vc-logentry-check-hook nil |
| 133 | "Normal hook run by `vc-finish-logentry'. |
| 134 | Use this to impose your own rules on the entry in addition to any the |
| 135 | dispatcher client mode imposes itself." |
| 136 | :type 'hook |
| 137 | :group 'vc) |
| 138 | |
| 139 | (defcustom vc-delete-logbuf-window t |
| 140 | "If non-nil, delete the *VC-log* buffer and window after each logical action. |
| 141 | If nil, bury that buffer instead. |
| 142 | This is most useful if you have multiple windows on a frame and would like to |
| 143 | preserve the setting." |
| 144 | :type 'boolean |
| 145 | :group 'vc) |
| 146 | |
| 147 | (defcustom vc-command-messages nil |
| 148 | "If non-nil, display run messages from back-end commands." |
| 149 | :type 'boolean |
| 150 | :group 'vc) |
| 151 | |
| 152 | (defcustom vc-suppress-confirm nil |
| 153 | "If non-nil, treat user as expert; suppress yes-no prompts on some things." |
| 154 | :type 'boolean |
| 155 | :group 'vc) |
| 156 | |
| 157 | ;; Variables the user doesn't need to know about. |
| 158 | |
| 159 | (defvar vc-log-operation nil) |
| 160 | (defvar vc-log-after-operation-hook nil) |
| 161 | (defvar vc-log-fileset) |
| 162 | (defvar vc-log-extra) |
| 163 | |
| 164 | ;; In a log entry buffer, this is a local variable |
| 165 | ;; that points to the buffer for which it was made |
| 166 | ;; (either a file, or a VC directory buffer). |
| 167 | (defvar vc-parent-buffer nil) |
| 168 | (put 'vc-parent-buffer 'permanent-local t) |
| 169 | (defvar vc-parent-buffer-name nil) |
| 170 | (put 'vc-parent-buffer-name 'permanent-local t) |
| 171 | |
| 172 | ;; Common command execution logic |
| 173 | |
| 174 | (defun vc-process-filter (p s) |
| 175 | "An alternative output filter for async process P. |
| 176 | One difference with the default filter is that this inserts S after markers. |
| 177 | Another is that undo information is not kept." |
| 178 | (let ((buffer (process-buffer p))) |
| 179 | (when (buffer-live-p buffer) |
| 180 | (with-current-buffer buffer |
| 181 | (save-excursion |
| 182 | (let ((buffer-undo-list t) |
| 183 | (inhibit-read-only t)) |
| 184 | (goto-char (process-mark p)) |
| 185 | (insert s) |
| 186 | (set-marker (process-mark p) (point)))))))) |
| 187 | |
| 188 | (defun vc-setup-buffer (buf) |
| 189 | "Prepare BUF for executing a slave command and make it current." |
| 190 | (let ((camefrom (current-buffer)) |
| 191 | (olddir default-directory)) |
| 192 | (set-buffer (get-buffer-create buf)) |
| 193 | (kill-all-local-variables) |
| 194 | (set (make-local-variable 'vc-parent-buffer) camefrom) |
| 195 | (set (make-local-variable 'vc-parent-buffer-name) |
| 196 | (concat " from " (buffer-name camefrom))) |
| 197 | (setq default-directory olddir) |
| 198 | (let ((buffer-undo-list t) |
| 199 | (inhibit-read-only t)) |
| 200 | (erase-buffer)))) |
| 201 | |
| 202 | (defvar vc-sentinel-movepoint) ;Dynamically scoped. |
| 203 | |
| 204 | (defun vc-process-sentinel (p s) |
| 205 | (let ((previous (process-get p 'vc-previous-sentinel)) |
| 206 | (buf (process-buffer p))) |
| 207 | ;; Impatient users sometime kill "slow" buffers; check liveness |
| 208 | ;; to avoid "error in process sentinel: Selecting deleted buffer". |
| 209 | (when (buffer-live-p buf) |
| 210 | (when previous (funcall previous p s)) |
| 211 | (with-current-buffer buf |
| 212 | (setq mode-line-process |
| 213 | (let ((status (process-status p))) |
| 214 | ;; Leave mode-line uncluttered, normally. |
| 215 | (unless (eq 'exit status) |
| 216 | (format " (%s)" status)))) |
| 217 | (let (vc-sentinel-movepoint) |
| 218 | ;; Normally, we want async code such as sentinels to not move point. |
| 219 | (save-excursion |
| 220 | (goto-char (process-mark p)) |
| 221 | (let ((cmds (process-get p 'vc-sentinel-commands))) |
| 222 | (process-put p 'vc-sentinel-commands nil) |
| 223 | (dolist (cmd cmds) |
| 224 | ;; Each sentinel may move point and the next one should be run |
| 225 | ;; at that new point. We could get the same result by having |
| 226 | ;; each sentinel read&set process-mark, but since `cmd' needs |
| 227 | ;; to work both for async and sync processes, this would be |
| 228 | ;; difficult to achieve. |
| 229 | (vc-exec-after cmd)))) |
| 230 | ;; But sometimes the sentinels really want to move point. |
| 231 | (when vc-sentinel-movepoint |
| 232 | (let ((win (get-buffer-window (current-buffer) 0))) |
| 233 | (if (not win) |
| 234 | (goto-char vc-sentinel-movepoint) |
| 235 | (with-selected-window win |
| 236 | (goto-char vc-sentinel-movepoint)))))))))) |
| 237 | |
| 238 | (defun vc-set-mode-line-busy-indicator () |
| 239 | (setq mode-line-process |
| 240 | (concat " " (propertize "[waiting...]" |
| 241 | 'face 'mode-line-emphasis |
| 242 | 'help-echo |
| 243 | "A VC command is in progress in this buffer")))) |
| 244 | |
| 245 | (defun vc-exec-after (code) |
| 246 | "Eval CODE when the current buffer's process is done. |
| 247 | If the current buffer has no process, just evaluate CODE. |
| 248 | Else, add CODE to the process' sentinel." |
| 249 | (let ((proc (get-buffer-process (current-buffer)))) |
| 250 | (cond |
| 251 | ;; If there's no background process, just execute the code. |
| 252 | ;; We used to explicitly call delete-process on exited processes, |
| 253 | ;; but this led to timing problems causing process output to be |
| 254 | ;; lost. Terminated processes get deleted automatically |
| 255 | ;; anyway. -- cyd |
| 256 | ((or (null proc) (eq (process-status proc) 'exit)) |
| 257 | ;; Make sure we've read the process's output before going further. |
| 258 | (when proc (accept-process-output proc)) |
| 259 | (eval code)) |
| 260 | ;; If a process is running, add CODE to the sentinel |
| 261 | ((eq (process-status proc) 'run) |
| 262 | (vc-set-mode-line-busy-indicator) |
| 263 | (let ((previous (process-sentinel proc))) |
| 264 | (unless (eq previous 'vc-process-sentinel) |
| 265 | (process-put proc 'vc-previous-sentinel previous)) |
| 266 | (set-process-sentinel proc 'vc-process-sentinel)) |
| 267 | (process-put proc 'vc-sentinel-commands |
| 268 | ;; We keep the code fragments in the order given |
| 269 | ;; so that vc-diff-finish's message shows up in |
| 270 | ;; the presence of non-nil vc-command-messages. |
| 271 | (append (process-get proc 'vc-sentinel-commands) |
| 272 | (list code)))) |
| 273 | (t (error "Unexpected process state")))) |
| 274 | nil) |
| 275 | |
| 276 | (defvar vc-post-command-functions nil |
| 277 | "Hook run at the end of `vc-do-command'. |
| 278 | Each function is called inside the buffer in which the command was run |
| 279 | and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.") |
| 280 | |
| 281 | (defvar w32-quote-process-args) |
| 282 | |
| 283 | (defun vc-delistify (filelist) |
| 284 | "Smash a FILELIST into a file list string suitable for info messages." |
| 285 | ;; FIXME what about file names with spaces? |
| 286 | (if (not filelist) "." (mapconcat 'identity filelist " "))) |
| 287 | |
| 288 | ;;;###autoload |
| 289 | (defun vc-do-command (buffer okstatus command file-or-list &rest flags) |
| 290 | "Execute a slave command, notifying user and checking for errors. |
| 291 | Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the |
| 292 | current buffer if BUFFER is t. If the destination buffer is not |
| 293 | already current, set it up properly and erase it. The command is |
| 294 | considered successful if its exit status does not exceed OKSTATUS (if |
| 295 | OKSTATUS is nil, that means to ignore error status, if it is `async', that |
| 296 | means not to wait for termination of the subprocess; if it is t it means to |
| 297 | ignore all execution errors). FILE-OR-LIST is the name of a working file; |
| 298 | it may be a list of files or be nil (to execute commands that don't expect |
| 299 | a file name or set of files). If an optional list of FLAGS is present, |
| 300 | that is inserted into the command line before the filename." |
| 301 | ;; FIXME: file-relative-name can return a bogus result because |
| 302 | ;; it doesn't look at the actual file-system to see if symlinks |
| 303 | ;; come into play. |
| 304 | (let* ((files |
| 305 | (mapcar (lambda (f) (file-relative-name (expand-file-name f))) |
| 306 | (if (listp file-or-list) file-or-list (list file-or-list)))) |
| 307 | (full-command |
| 308 | ;; What we're doing here is preparing a version of the command |
| 309 | ;; for display in a debug-progess message. If it's fewer than |
| 310 | ;; 20 characters display the entire command (without trailing |
| 311 | ;; newline). Otherwise display the first 20 followed by an ellipsis. |
| 312 | (concat (if (string= (substring command -1) "\n") |
| 313 | (substring command 0 -1) |
| 314 | command) |
| 315 | " " |
| 316 | (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags)) |
| 317 | " " (vc-delistify files)))) |
| 318 | (save-current-buffer |
| 319 | (unless (or (eq buffer t) |
| 320 | (and (stringp buffer) |
| 321 | (string= (buffer-name) buffer)) |
| 322 | (eq buffer (current-buffer))) |
| 323 | (vc-setup-buffer (or buffer "*vc*"))) |
| 324 | ;; If there's some previous async process still running, just kill it. |
| 325 | (let ((oldproc (get-buffer-process (current-buffer)))) |
| 326 | ;; If we wanted to wait for oldproc to finish before doing |
| 327 | ;; something, we'd have used vc-eval-after. |
| 328 | ;; Use `delete-process' rather than `kill-process' because we don't |
| 329 | ;; want any of its output to appear from now on. |
| 330 | (if oldproc (delete-process oldproc))) |
| 331 | (let ((squeezed (remq nil flags)) |
| 332 | (inhibit-read-only t) |
| 333 | (status 0)) |
| 334 | (when files |
| 335 | (setq squeezed (nconc squeezed files))) |
| 336 | (let ((exec-path (append vc-path exec-path)) |
| 337 | ;; Add vc-path to PATH for the execution of this command. |
| 338 | (process-environment |
| 339 | (cons (concat "PATH=" (getenv "PATH") |
| 340 | path-separator |
| 341 | (mapconcat 'identity vc-path path-separator)) |
| 342 | process-environment)) |
| 343 | (w32-quote-process-args t)) |
| 344 | (when (and (eq okstatus 'async) (file-remote-p default-directory)) |
| 345 | ;; start-process does not support remote execution |
| 346 | (setq okstatus nil)) |
| 347 | (if (eq okstatus 'async) |
| 348 | ;; Run asynchronously. |
| 349 | (let ((proc |
| 350 | (let ((process-connection-type nil)) |
| 351 | (apply 'start-file-process command (current-buffer) |
| 352 | command squeezed)))) |
| 353 | (if vc-command-messages |
| 354 | (message "Running %s in background..." full-command)) |
| 355 | ;;(set-process-sentinel proc (lambda (p msg) (delete-process p))) |
| 356 | (set-process-filter proc 'vc-process-filter) |
| 357 | (vc-exec-after |
| 358 | `(if vc-command-messages |
| 359 | (message "Running %s in background... done" ',full-command)))) |
| 360 | ;; Run synchrously |
| 361 | (when vc-command-messages |
| 362 | (message "Running %s in foreground..." full-command)) |
| 363 | (let ((buffer-undo-list t)) |
| 364 | (setq status (apply 'process-file command nil t nil squeezed))) |
| 365 | (when (and (not (eq t okstatus)) |
| 366 | (or (not (integerp status)) |
| 367 | (and okstatus (< okstatus status)))) |
| 368 | (unless (eq ?\s (aref (buffer-name (current-buffer)) 0)) |
| 369 | (pop-to-buffer (current-buffer)) |
| 370 | (goto-char (point-min)) |
| 371 | (shrink-window-if-larger-than-buffer)) |
| 372 | (error "Running %s...FAILED (%s)" full-command |
| 373 | (if (integerp status) (format "status %d" status) status)))) |
| 374 | ;; We're done. But don't emit a status message if running |
| 375 | ;; asychronously, it would just mislead. |
| 376 | (if (and vc-command-messages (not (eq okstatus 'async))) |
| 377 | (message "Running %s...OK = %d" full-command status))) |
| 378 | (vc-exec-after |
| 379 | `(run-hook-with-args 'vc-post-command-functions |
| 380 | ',command ',file-or-list ',flags)) |
| 381 | status)))) |
| 382 | |
| 383 | ;; These functions are used to ensure that the view the user sees is up to date |
| 384 | ;; even if the dispatcher client mode has messed with file contents (as in, |
| 385 | ;; for example, VCS keyword expansion). |
| 386 | |
| 387 | (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win)) |
| 388 | |
| 389 | (defun vc-position-context (posn) |
| 390 | "Save a bit of the text around POSN in the current buffer. |
| 391 | Used to help us find the corresponding position again later |
| 392 | if markers are destroyed or corrupted." |
| 393 | ;; A lot of this was shamelessly lifted from Sebastian Kremer's |
| 394 | ;; rcs.el mode. |
| 395 | (list posn |
| 396 | (buffer-size) |
| 397 | (buffer-substring posn |
| 398 | (min (point-max) (+ posn 100))))) |
| 399 | |
| 400 | (defun vc-find-position-by-context (context) |
| 401 | "Return the position of CONTEXT in the current buffer. |
| 402 | If CONTEXT cannot be found, return nil." |
| 403 | (let ((context-string (nth 2 context))) |
| 404 | (if (equal "" context-string) |
| 405 | (point-max) |
| 406 | (save-excursion |
| 407 | (let ((diff (- (nth 1 context) (buffer-size)))) |
| 408 | (when (< diff 0) (setq diff (- diff))) |
| 409 | (goto-char (nth 0 context)) |
| 410 | (if (or (search-forward context-string nil t) |
| 411 | ;; Can't use search-backward since the match may continue |
| 412 | ;; after point. |
| 413 | (progn (goto-char (- (point) diff (length context-string))) |
| 414 | ;; goto-char doesn't signal an error at |
| 415 | ;; beginning of buffer like backward-char would |
| 416 | (search-forward context-string nil t))) |
| 417 | ;; to beginning of OSTRING |
| 418 | (- (point) (length context-string)))))))) |
| 419 | |
| 420 | (defun vc-context-matches-p (posn context) |
| 421 | "Return t if POSN matches CONTEXT, nil otherwise." |
| 422 | (let* ((context-string (nth 2 context)) |
| 423 | (len (length context-string)) |
| 424 | (end (+ posn len))) |
| 425 | (if (> end (1+ (buffer-size))) |
| 426 | nil |
| 427 | (string= context-string (buffer-substring posn end))))) |
| 428 | |
| 429 | (defun vc-buffer-context () |
| 430 | "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE). |
| 431 | Used by `vc-restore-buffer-context' to later restore the context." |
| 432 | (let ((point-context (vc-position-context (point))) |
| 433 | ;; Use mark-marker to avoid confusion in transient-mark-mode. |
| 434 | (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer)) |
| 435 | (vc-position-context (mark-marker)))) |
| 436 | ;; Make the right thing happen in transient-mark-mode. |
| 437 | (mark-active nil)) |
| 438 | (list point-context mark-context nil))) |
| 439 | |
| 440 | (defun vc-restore-buffer-context (context) |
| 441 | "Restore point/mark, and reparse any affected compilation buffers. |
| 442 | CONTEXT is that which `vc-buffer-context' returns." |
| 443 | (let ((point-context (nth 0 context)) |
| 444 | (mark-context (nth 1 context))) |
| 445 | ;; if necessary, restore point and mark |
| 446 | (if (not (vc-context-matches-p (point) point-context)) |
| 447 | (let ((new-point (vc-find-position-by-context point-context))) |
| 448 | (when new-point (goto-char new-point)))) |
| 449 | (and mark-active |
| 450 | mark-context |
| 451 | (not (vc-context-matches-p (mark) mark-context)) |
| 452 | (let ((new-mark (vc-find-position-by-context mark-context))) |
| 453 | (when new-mark (set-mark new-mark)))))) |
| 454 | |
| 455 | (defun vc-revert-buffer-internal (&optional arg no-confirm) |
| 456 | "Revert buffer, keeping point and mark where user expects them. |
| 457 | Try to be clever in the face of changes due to expanded version-control |
| 458 | key words. This is important for typeahead to work as expected. |
| 459 | ARG and NO-CONFIRM are passed on to `revert-buffer'." |
| 460 | (interactive "P") |
| 461 | (widen) |
| 462 | (let ((context (vc-buffer-context))) |
| 463 | ;; Use save-excursion here, because it may be able to restore point |
| 464 | ;; and mark properly even in cases where vc-restore-buffer-context |
| 465 | ;; would fail. However, save-excursion might also get it wrong -- |
| 466 | ;; in this case, vc-restore-buffer-context gives it a second try. |
| 467 | (save-excursion |
| 468 | ;; t means don't call normal-mode; |
| 469 | ;; that's to preserve various minor modes. |
| 470 | (revert-buffer arg no-confirm t)) |
| 471 | (vc-restore-buffer-context context))) |
| 472 | |
| 473 | (defun vc-resynch-window (file &optional keep noquery) |
| 474 | "If FILE is in the current buffer, either revert or unvisit it. |
| 475 | The choice between revert (to see expanded keywords) and unvisit |
| 476 | depends on KEEP. NOQUERY if non-nil inhibits confirmation for |
| 477 | reverting. NOQUERY should be t *only* if it is known the only |
| 478 | difference between the buffer and the file is due to |
| 479 | modifications by the dispatcher client code, rather than user |
| 480 | editing!" |
| 481 | (and (string= buffer-file-name file) |
| 482 | (if keep |
| 483 | (progn |
| 484 | (vc-revert-buffer-internal t noquery) |
| 485 | ;; TODO: Adjusting view mode might no longer be necessary |
| 486 | ;; after RMS change to files.el of 1999-08-08. Investigate |
| 487 | ;; this when we install the new VC. |
| 488 | (and view-read-only |
| 489 | (if (file-writable-p file) |
| 490 | (and view-mode |
| 491 | (let ((view-old-buffer-read-only nil)) |
| 492 | (view-mode-exit))) |
| 493 | (and (not view-mode) |
| 494 | (not (eq (get major-mode 'mode-class) 'special)) |
| 495 | (view-mode-enter)))) |
| 496 | (run-hook-with-args 'modeline-hook buffer-file-name)) |
| 497 | (kill-buffer (current-buffer))))) |
| 498 | |
| 499 | (defun vc-resynch-buffer (file &optional keep noquery) |
| 500 | "If FILE is currently visited, resynch its buffer." |
| 501 | (if (string= buffer-file-name file) |
| 502 | (vc-resynch-window file keep noquery) |
| 503 | (let ((buffer (get-file-buffer file))) |
| 504 | (when buffer |
| 505 | (with-current-buffer buffer |
| 506 | (vc-resynch-window file keep noquery))))) |
| 507 | (vc-directory-resynch-file file)) |
| 508 | |
| 509 | ;; Command closures |
| 510 | |
| 511 | (defun vc-start-logentry (files extra comment initial-contents msg action &optional after-hook) |
| 512 | "Accept a comment for an operation on FILES with extra data EXTRA. |
| 513 | If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the |
| 514 | action on close to ACTION. If COMMENT is a string and |
| 515 | INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial |
| 516 | contents of the log entry buffer. If COMMENT is a string and |
| 517 | INITIAL-CONTENTS is nil, do action immediately as if the user had |
| 518 | entered COMMENT. If COMMENT is t, also do action immediately with an |
| 519 | empty comment. Remember the file's buffer in `vc-parent-buffer' |
| 520 | \(current one if no file). AFTER-HOOK specifies the local value |
| 521 | for `vc-log-after-operation-hook'." |
| 522 | (let ((parent |
| 523 | (if (vc-dispatcher-browsing) |
| 524 | ;; If we are called from a directory browser, the parent buffer is |
| 525 | ;; the current buffer. |
| 526 | (current-buffer) |
| 527 | (if (and files (equal (length files) 1)) |
| 528 | (get-file-buffer (car files)) |
| 529 | (current-buffer))))) |
| 530 | (if (and comment (not initial-contents)) |
| 531 | (set-buffer (get-buffer-create "*VC-log*")) |
| 532 | (pop-to-buffer (get-buffer-create "*VC-log*"))) |
| 533 | (set (make-local-variable 'vc-parent-buffer) parent) |
| 534 | (set (make-local-variable 'vc-parent-buffer-name) |
| 535 | (concat " from " (buffer-name vc-parent-buffer))) |
| 536 | (vc-log-edit files) |
| 537 | (make-local-variable 'vc-log-after-operation-hook) |
| 538 | (when after-hook |
| 539 | (setq vc-log-after-operation-hook after-hook)) |
| 540 | (setq vc-log-operation action) |
| 541 | (setq vc-log-extra extra) |
| 542 | (when comment |
| 543 | (erase-buffer) |
| 544 | (when (stringp comment) (insert comment))) |
| 545 | (if (or (not comment) initial-contents) |
| 546 | (message "%s Type C-c C-c when done" msg) |
| 547 | (vc-finish-logentry (eq comment t))))) |
| 548 | |
| 549 | (defun vc-finish-logentry (&optional nocomment) |
| 550 | "Complete the operation implied by the current log entry. |
| 551 | Use the contents of the current buffer as a check-in or registration |
| 552 | comment. If the optional arg NOCOMMENT is non-nil, then don't check |
| 553 | the buffer contents as a comment." |
| 554 | (interactive) |
| 555 | ;; Check and record the comment, if any. |
| 556 | (unless nocomment |
| 557 | (run-hooks 'vc-logentry-check-hook)) |
| 558 | ;; Sync parent buffer in case the user modified it while editing the comment. |
| 559 | ;; But not if it is a vc-directory buffer. |
| 560 | (with-current-buffer vc-parent-buffer |
| 561 | (or (vc-dispatcher-browsing) (vc-buffer-sync))) |
| 562 | (unless vc-log-operation |
| 563 | (error "No log operation is pending")) |
| 564 | ;; save the parameters held in buffer-local variables |
| 565 | (let ((log-operation vc-log-operation) |
| 566 | (log-fileset vc-log-fileset) |
| 567 | (log-extra vc-log-extra) |
| 568 | (log-entry (buffer-string)) |
| 569 | (after-hook vc-log-after-operation-hook) |
| 570 | (tmp-vc-parent-buffer vc-parent-buffer)) |
| 571 | (pop-to-buffer vc-parent-buffer) |
| 572 | ;; OK, do it to it |
| 573 | (save-excursion |
| 574 | (funcall log-operation |
| 575 | log-fileset |
| 576 | log-extra |
| 577 | log-entry)) |
| 578 | ;; Remove checkin window (after the checkin so that if that fails |
| 579 | ;; we don't zap the *VC-log* buffer and the typing therein). |
| 580 | ;; -- IMO this should be replaced with quit-window |
| 581 | (let ((logbuf (get-buffer "*VC-log*"))) |
| 582 | (cond ((and logbuf vc-delete-logbuf-window) |
| 583 | (delete-windows-on logbuf (selected-frame)) |
| 584 | ;; Kill buffer and delete any other dedicated windows/frames. |
| 585 | (kill-buffer logbuf)) |
| 586 | (logbuf (pop-to-buffer "*VC-log*") |
| 587 | (bury-buffer) |
| 588 | (pop-to-buffer tmp-vc-parent-buffer)))) |
| 589 | ;; Now make sure we see the expanded headers |
| 590 | (when log-fileset |
| 591 | (mapc |
| 592 | (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t)) |
| 593 | log-fileset)) |
| 594 | (when (vc-dispatcher-browsing) |
| 595 | (vc-dir-move-to-goal-column)) |
| 596 | (run-hooks after-hook 'vc-finish-logentry-hook))) |
| 597 | |
| 598 | ;; The ewoc-based vc-directory implementation |
| 599 | |
| 600 | (defcustom vc-dir-mode-hook nil |
| 601 | "Normal hook run by `vc-dir-mode'. |
| 602 | See `run-hooks'." |
| 603 | :type 'hook |
| 604 | :group 'vc) |
| 605 | |
| 606 | ;; Used to store information for the files displayed in the *VC status* buffer. |
| 607 | ;; Each item displayed corresponds to one of these defstructs. |
| 608 | (defstruct (vc-dir-fileinfo |
| 609 | (:copier nil) |
| 610 | (:type list) ;So we can use `member' on lists of FIs. |
| 611 | (:constructor |
| 612 | ;; We could define it as an alias for `list'. |
| 613 | vc-dir-create-fileinfo (name state &optional extra marked directory)) |
| 614 | (:conc-name vc-dir-fileinfo->)) |
| 615 | name ;Keep it as first, for `member'. |
| 616 | state |
| 617 | ;; For storing client-mode specific information. |
| 618 | extra |
| 619 | marked |
| 620 | ;; To keep track of not updated files during a global refresh |
| 621 | needs-update |
| 622 | ;; To distinguish files and directories. |
| 623 | directory) |
| 624 | |
| 625 | ;; Used to describe a dispatcher client mode. |
| 626 | (defstruct (vc-client-object |
| 627 | (:copier nil) |
| 628 | (:constructor |
| 629 | vc-create-client-object (name |
| 630 | headers |
| 631 | file-to-info |
| 632 | file-to-state |
| 633 | file-to-extra |
| 634 | updater)) |
| 635 | (:conc-name vc-client-object->)) |
| 636 | name |
| 637 | headers |
| 638 | file-to-info |
| 639 | file-to-state |
| 640 | file-to-extra |
| 641 | updater) |
| 642 | |
| 643 | (defvar vc-ewoc nil) |
| 644 | (defvar vc-dir-process-buffer nil |
| 645 | "The buffer used for the asynchronous call that computes the VC status.") |
| 646 | |
| 647 | (defun vc-dir-move-to-goal-column () |
| 648 | ;; Used to keep the cursor on the file name column. |
| 649 | (beginning-of-line) |
| 650 | ;; Must be in sync with vc-default-status-printer. |
| 651 | (forward-char 25)) |
| 652 | |
| 653 | (defun vc-dir-prepare-status-buffer (dir &optional create-new) |
| 654 | "Find a *vc-dir* buffer showing DIR, or create a new one." |
| 655 | (setq dir (expand-file-name dir)) |
| 656 | (let* ((bname "*vc-dir*") |
| 657 | ;; Look for another *vc-dir* buffer visiting the same directory. |
| 658 | (buf (save-excursion |
| 659 | (unless create-new |
| 660 | (dolist (buffer (buffer-list)) |
| 661 | (set-buffer buffer) |
| 662 | (when (and (vc-dispatcher-browsing) |
| 663 | (string= (expand-file-name default-directory) dir)) |
| 664 | (return buffer))))))) |
| 665 | (or buf |
| 666 | ;; Create a new *vc-dir* buffer. |
| 667 | (with-current-buffer (create-file-buffer bname) |
| 668 | (cd dir) |
| 669 | (vc-setup-buffer (current-buffer)) |
| 670 | ;; Reset the vc-parent-buffer-name so that it does not appear |
| 671 | ;; in the mode-line. |
| 672 | (setq vc-parent-buffer-name nil) |
| 673 | (current-buffer))))) |
| 674 | |
| 675 | (defvar vc-dir-menu-map |
| 676 | (let ((map (make-sparse-keymap "VC-dir"))) |
| 677 | (define-key map [quit] |
| 678 | '(menu-item "Quit" quit-window |
| 679 | :help "Quit")) |
| 680 | (define-key map [kill] |
| 681 | '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process |
| 682 | :enable (vc-dir-busy) |
| 683 | :help "Kill the command that updates VC status buffer")) |
| 684 | (define-key map [refresh] |
| 685 | '(menu-item "Refresh" vc-dir-refresh |
| 686 | :enable (not (vc-dir-busy)) |
| 687 | :help "Refresh the contents of the VC status buffer")) |
| 688 | ;; Movement. |
| 689 | (define-key map [sepmv] '("--")) |
| 690 | (define-key map [next-line] |
| 691 | '(menu-item "Next line" vc-dir-next-line |
| 692 | :help "Go to the next line" :keys "n")) |
| 693 | (define-key map [previous-line] |
| 694 | '(menu-item "Previous line" vc-dir-previous-line |
| 695 | :help "Go to the previous line")) |
| 696 | ;; Marking. |
| 697 | (define-key map [sepmrk] '("--")) |
| 698 | (define-key map [unmark-all] |
| 699 | '(menu-item "Unmark All" vc-dir-unmark-all-files |
| 700 | :help "Unmark all files that are in the same state as the current file\ |
| 701 | \nWith prefix argument unmark all files")) |
| 702 | (define-key map [unmark-previous] |
| 703 | '(menu-item "Unmark previous " vc-dir-unmark-file-up |
| 704 | :help "Move to the previous line and unmark the file")) |
| 705 | |
| 706 | (define-key map [mark-all] |
| 707 | '(menu-item "Mark All" vc-dir-mark-all-files |
| 708 | :help "Mark all files that are in the same state as the current file\ |
| 709 | \nWith prefix argument mark all files")) |
| 710 | (define-key map [unmark] |
| 711 | '(menu-item "Unmark" vc-dir-unmark |
| 712 | :help "Unmark the current file or all files in the region")) |
| 713 | |
| 714 | (define-key map [mark] |
| 715 | '(menu-item "Mark" vc-dir-mark |
| 716 | :help "Mark the current file or all files in the region")) |
| 717 | |
| 718 | (define-key map [sepopn] '("--")) |
| 719 | (define-key map [open-other] |
| 720 | '(menu-item "Open in other window" vc-dir-find-file-other-window |
| 721 | :help "Find the file on the current line, in another window")) |
| 722 | (define-key map [open] |
| 723 | '(menu-item "Open file" vc-dir-find-file |
| 724 | :help "Find the file on the current line")) |
| 725 | map) |
| 726 | "Menu for dispatcher status") |
| 727 | |
| 728 | (defalias 'vc-dir-menu-map vc-dir-menu-map) |
| 729 | |
| 730 | (defvar vc-dir-mode-map |
| 731 | (let ((map (make-keymap))) |
| 732 | (suppress-keymap map) |
| 733 | ;; Marking. |
| 734 | (define-key map "m" 'vc-dir-mark) |
| 735 | (define-key map "M" 'vc-dir-mark-all-files) |
| 736 | (define-key map "u" 'vc-dir-unmark) |
| 737 | (define-key map "U" 'vc-dir-unmark-all-files) |
| 738 | (define-key map "\C-?" 'vc-dir-unmark-file-up) |
| 739 | (define-key map "\M-\C-?" 'vc-dir-unmark-all-files) |
| 740 | ;; Movement. |
| 741 | (define-key map "n" 'vc-dir-next-line) |
| 742 | (define-key map " " 'vc-dir-next-line) |
| 743 | (define-key map "\t" 'vc-dir-next-line) |
| 744 | (define-key map "p" 'vc-dir-previous-line) |
| 745 | (define-key map [backtab] 'vc-dir-previous-line) |
| 746 | ;; The remainder. |
| 747 | (define-key map "f" 'vc-dir-find-file) |
| 748 | (define-key map "\C-m" 'vc-dir-find-file) |
| 749 | (define-key map "o" 'vc-dir-find-file-other-window) |
| 750 | (define-key map "q" 'quit-window) |
| 751 | (define-key map "g" 'vc-dir-refresh) |
| 752 | (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process) |
| 753 | (define-key map [(down-mouse-3)] 'vc-dir-menu) |
| 754 | (define-key map [(mouse-2)] 'vc-dir-toggle-mark) |
| 755 | |
| 756 | ;; FIXME: Calls back into vc.el |
| 757 | ;; Hook up the menu. |
| 758 | (define-key map [menu-bar vc-dir-mode] |
| 759 | '(menu-item |
| 760 | ;; This is used so that client modes can add mode-specific |
| 761 | ;; menu items to vc-dir-menu-map. |
| 762 | "VC Status" vc-dir-menu-map :filter vc-dir-menu-map-filter)) |
| 763 | map) |
| 764 | "Keymap for VC status") |
| 765 | |
| 766 | (defmacro vc-at-event (event &rest body) |
| 767 | "Evaluate `body' wich point located at event-start of `event'. |
| 768 | If `body' uses `event', it should be a variable, |
| 769 | otherwise it will be evaluated twice." |
| 770 | (let ((posn (gensym "vc-at-event-posn"))) |
| 771 | `(let ((,posn (event-start ,event))) |
| 772 | (save-excursion |
| 773 | (set-buffer (window-buffer (posn-window ,posn))) |
| 774 | (goto-char (posn-point ,posn)) |
| 775 | ,@body)))) |
| 776 | |
| 777 | (defun vc-dir-menu (e) |
| 778 | "Popup the VC status menu." |
| 779 | (interactive "e") |
| 780 | (vc-at-event e (popup-menu vc-dir-menu-map e))) |
| 781 | |
| 782 | (defvar vc-dir-tool-bar-map |
| 783 | (let ((map (make-sparse-keymap))) |
| 784 | (tool-bar-local-item-from-menu 'vc-dir-find-file "open" |
| 785 | map vc-dir-mode-map) |
| 786 | (tool-bar-local-item "bookmark_add" |
| 787 | 'vc-dir-toggle-mark 'vc-dir-toggle-mark map |
| 788 | :help "Toggle mark on current item") |
| 789 | (tool-bar-local-item-from-menu 'vc-dir-previous-line "left-arrow" |
| 790 | map vc-dir-mode-map |
| 791 | :rtl "right-arrow") |
| 792 | (tool-bar-local-item-from-menu 'vc-dir-next-line "right-arrow" |
| 793 | map vc-dir-mode-map |
| 794 | :rtl "left-arrow") |
| 795 | (tool-bar-local-item-from-menu 'vc-print-log "info" |
| 796 | map vc-dir-mode-map) |
| 797 | (tool-bar-local-item-from-menu 'vc-dir-refresh "refresh" |
| 798 | map vc-dir-mode-map) |
| 799 | (tool-bar-local-item-from-menu 'nonincremental-search-forward |
| 800 | "search" map) |
| 801 | (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel" |
| 802 | map vc-dir-mode-map) |
| 803 | (tool-bar-local-item-from-menu 'quit-window "exit" |
| 804 | map vc-dir-mode-map) |
| 805 | map)) |
| 806 | |
| 807 | (defun vc-dir-update (entries buffer &optional noinsert) |
| 808 | "Update BUFFER's ewoc from the list of ENTRIES. |
| 809 | If NOINSERT, ignore elements on ENTRIES which are not in the ewoc." |
| 810 | ;; Add ENTRIES to the vc-dir buffer BUFFER. |
| 811 | (with-current-buffer buffer |
| 812 | ;; Insert the entries sorted by name into the ewoc. |
| 813 | ;; We assume the ewoc is sorted too, which should be the |
| 814 | ;; case if we always add entries with vc-dir-update. |
| 815 | (setq entries |
| 816 | ;; Sort: first files and then subdirectories. |
| 817 | ;; XXX: this is VERY inefficient, it computes the directory |
| 818 | ;; names too many times |
| 819 | (sort entries |
| 820 | (lambda (entry1 entry2) |
| 821 | (let ((dir1 (file-name-directory (expand-file-name (car entry1)))) |
| 822 | (dir2 (file-name-directory (expand-file-name (car entry2))))) |
| 823 | (cond |
| 824 | ((string< dir1 dir2) t) |
| 825 | ((not (string= dir1 dir2)) nil) |
| 826 | ((string< (car entry1) (car entry2)))))))) |
| 827 | ;; Insert directory entries in the right places. |
| 828 | (let ((entry (car entries)) |
| 829 | (node (ewoc-nth vc-ewoc 0))) |
| 830 | ;; Insert . if it is not present. |
| 831 | (unless node |
| 832 | (let ((rd (file-relative-name default-directory))) |
| 833 | (ewoc-enter-last |
| 834 | vc-ewoc (vc-dir-create-fileinfo |
| 835 | rd nil nil nil (expand-file-name default-directory)))) |
| 836 | (setq node (ewoc-nth vc-ewoc 0))) |
| 837 | |
| 838 | (while (and entry node) |
| 839 | (let* ((entryfile (car entry)) |
| 840 | (entrydir (file-name-directory (expand-file-name entryfile))) |
| 841 | (nodedir |
| 842 | (or (vc-dir-fileinfo->directory (ewoc-data node)) |
| 843 | (file-name-directory |
| 844 | (expand-file-name |
| 845 | (vc-dir-fileinfo->name (ewoc-data node))))))) |
| 846 | (cond |
| 847 | ;; First try to find the directory. |
| 848 | ((string-lessp nodedir entrydir) |
| 849 | (setq node (ewoc-next vc-ewoc node))) |
| 850 | ((string-equal nodedir entrydir) |
| 851 | ;; Found the directory, find the place for the file name. |
| 852 | (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node)))) |
| 853 | (cond |
| 854 | ((string-lessp nodefile entryfile) |
| 855 | (setq node (ewoc-next vc-ewoc node))) |
| 856 | ((string-equal nodefile entryfile) |
| 857 | (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry)) |
| 858 | (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry)) |
| 859 | (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil) |
| 860 | (ewoc-invalidate vc-ewoc node) |
| 861 | (setq entries (cdr entries) entry (car entries)) |
| 862 | (setq node (ewoc-next vc-ewoc node))) |
| 863 | (t |
| 864 | (ewoc-enter-before vc-ewoc node |
| 865 | (apply 'vc-dir-create-fileinfo entry)) |
| 866 | (setq entries (cdr entries) entry (car entries)))))) |
| 867 | (t |
| 868 | ;; We need to insert a directory node |
| 869 | (let ((rd (file-relative-name entrydir))) |
| 870 | (ewoc-enter-last |
| 871 | vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))) |
| 872 | ;; Now insert the node itself. |
| 873 | (ewoc-enter-before vc-ewoc node |
| 874 | (apply 'vc-dir-create-fileinfo entry)) |
| 875 | (setq entries (cdr entries) entry (car entries)))))) |
| 876 | ;; We're past the last node, all remaining entries go to the end. |
| 877 | (unless (or node noinsert) |
| 878 | (let* ((lastnode (ewoc-nth vc-ewoc -1)) |
| 879 | (lastdir |
| 880 | (or (vc-dir-fileinfo->directory (ewoc-data lastnode)) |
| 881 | (file-name-directory |
| 882 | (expand-file-name |
| 883 | (vc-dir-fileinfo->name (ewoc-data lastnode))))))) |
| 884 | (dolist (entry entries) |
| 885 | (let ((entrydir (file-name-directory (expand-file-name (car entry))))) |
| 886 | ;; Insert a directory node if needed. |
| 887 | (unless (string-equal lastdir entrydir) |
| 888 | (setq lastdir entrydir) |
| 889 | (let ((rd (file-relative-name entrydir))) |
| 890 | (ewoc-enter-last |
| 891 | vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir)))) |
| 892 | ;; Now insert the node itself. |
| 893 | (ewoc-enter-last vc-ewoc |
| 894 | (apply 'vc-dir-create-fileinfo entry))))))))) |
| 895 | |
| 896 | (defun vc-dir-busy () |
| 897 | (and (buffer-live-p vc-dir-process-buffer) |
| 898 | (get-buffer-process vc-dir-process-buffer))) |
| 899 | |
| 900 | (defun vc-dir-kill-dir-status-process () |
| 901 | "Kill the temporary buffer and associated process." |
| 902 | (interactive) |
| 903 | (when (buffer-live-p vc-dir-process-buffer) |
| 904 | (let ((proc (get-buffer-process vc-dir-process-buffer))) |
| 905 | (when proc (delete-process proc)) |
| 906 | (setq vc-dir-process-buffer nil) |
| 907 | (setq mode-line-process nil)))) |
| 908 | |
| 909 | (defun vc-dir-kill-query () |
| 910 | ;; Make sure that when the VC status buffer is killed the update |
| 911 | ;; process running in background is also killed. |
| 912 | (if (vc-dir-busy) |
| 913 | (when (y-or-n-p "Status update process running, really kill status buffer?") |
| 914 | (vc-dir-kill-dir-status-process) |
| 915 | t) |
| 916 | t)) |
| 917 | |
| 918 | (defun vc-dir-next-line (arg) |
| 919 | "Go to the next line. |
| 920 | If a prefix argument is given, move by that many lines." |
| 921 | (interactive "p") |
| 922 | (with-no-warnings |
| 923 | (ewoc-goto-next vc-ewoc arg) |
| 924 | (vc-dir-move-to-goal-column))) |
| 925 | |
| 926 | (defun vc-dir-previous-line (arg) |
| 927 | "Go to the previous line. |
| 928 | If a prefix argument is given, move by that many lines." |
| 929 | (interactive "p") |
| 930 | (ewoc-goto-prev vc-ewoc arg) |
| 931 | (vc-dir-move-to-goal-column)) |
| 932 | |
| 933 | (defun vc-dir-mark-unmark (mark-unmark-function) |
| 934 | (if (use-region-p) |
| 935 | (let ((firstl (line-number-at-pos (region-beginning))) |
| 936 | (lastl (line-number-at-pos (region-end)))) |
| 937 | (save-excursion |
| 938 | (goto-char (region-beginning)) |
| 939 | (while (<= (line-number-at-pos) lastl) |
| 940 | (funcall mark-unmark-function)))) |
| 941 | (funcall mark-unmark-function))) |
| 942 | |
| 943 | (defun vc-dir-parent-marked-p (arg) |
| 944 | ;; Return nil if none of the parent directories of arg is marked. |
| 945 | (let* ((argdata (ewoc-data arg)) |
| 946 | (argdir |
| 947 | (let ((crtdir (vc-dir-fileinfo->directory argdata))) |
| 948 | (if crtdir |
| 949 | crtdir |
| 950 | (file-name-directory (expand-file-name |
| 951 | (vc-dir-fileinfo->name argdata)))))) |
| 952 | (arglen (length argdir)) |
| 953 | (crt arg) |
| 954 | data dir) |
| 955 | ;; Go through the predecessors, checking if any directory that is |
| 956 | ;; a parent is marked. |
| 957 | (while (setq crt (ewoc-prev vc-ewoc crt)) |
| 958 | (setq data (ewoc-data crt)) |
| 959 | (setq dir |
| 960 | (let ((crtdir (vc-dir-fileinfo->directory data))) |
| 961 | (if crtdir |
| 962 | crtdir |
| 963 | (file-name-directory (expand-file-name |
| 964 | (vc-dir-fileinfo->name data)))))) |
| 965 | |
| 966 | (when (and (vc-dir-fileinfo->directory data) |
| 967 | (string-equal (substring argdir 0 (length dir)) dir)) |
| 968 | (when (vc-dir-fileinfo->marked data) |
| 969 | (error "Cannot mark `%s', parent directory `%s' marked" |
| 970 | (vc-dir-fileinfo->name argdata) |
| 971 | (vc-dir-fileinfo->name data))))) |
| 972 | nil)) |
| 973 | |
| 974 | (defun vc-dir-children-marked-p (arg) |
| 975 | ;; Return nil if none of the children of arg is marked. |
| 976 | (let* ((argdata (ewoc-data arg)) |
| 977 | (argdir (vc-dir-fileinfo->directory argdata)) |
| 978 | (arglen (length argdir)) |
| 979 | (is-child t) |
| 980 | (crt arg) |
| 981 | data dir) |
| 982 | (while (and is-child (setq crt (ewoc-next vc-ewoc crt))) |
| 983 | (setq data (ewoc-data crt)) |
| 984 | (setq dir |
| 985 | (let ((crtdir (vc-dir-fileinfo->directory data))) |
| 986 | (if crtdir |
| 987 | crtdir |
| 988 | (file-name-directory (expand-file-name |
| 989 | (vc-dir-fileinfo->name data)))))) |
| 990 | (if (string-equal argdir (substring dir 0 arglen)) |
| 991 | (when (vc-dir-fileinfo->marked data) |
| 992 | (error "Cannot mark `%s', child `%s' marked" |
| 993 | (vc-dir-fileinfo->name argdata) |
| 994 | (vc-dir-fileinfo->name data))) |
| 995 | ;; We are done, we got to an entry that is not a child of `arg'. |
| 996 | (setq is-child nil))) |
| 997 | nil)) |
| 998 | |
| 999 | (defun vc-dir-mark-file (&optional arg) |
| 1000 | ;; Mark ARG or the current file and move to the next line. |
| 1001 | (let* ((crt (or arg (ewoc-locate vc-ewoc))) |
| 1002 | (file (ewoc-data crt)) |
| 1003 | (isdir (vc-dir-fileinfo->directory file))) |
| 1004 | (when (or (and isdir (not (vc-dir-children-marked-p crt))) |
| 1005 | (and (not isdir) (not (vc-dir-parent-marked-p crt)))) |
| 1006 | (setf (vc-dir-fileinfo->marked file) t) |
| 1007 | (ewoc-invalidate vc-ewoc crt) |
| 1008 | (unless (or arg (mouse-event-p last-command-event)) |
| 1009 | (vc-dir-next-line 1))))) |
| 1010 | |
| 1011 | (defun vc-dir-mark () |
| 1012 | "Mark the current file or all files in the region. |
| 1013 | If the region is active, mark all the files in the region. |
| 1014 | Otherwise mark the file on the current line and move to the next |
| 1015 | line." |
| 1016 | (interactive) |
| 1017 | (vc-dir-mark-unmark 'vc-dir-mark-file)) |
| 1018 | |
| 1019 | (defun vc-dir-mark-all-files (arg) |
| 1020 | "Mark all files with the same state as the current one. |
| 1021 | With a prefix argument mark all files. |
| 1022 | If the current entry is a directory, mark all child files. |
| 1023 | |
| 1024 | The VC commands operate on files that are on the same state. |
| 1025 | This command is intended to make it easy to select all files that |
| 1026 | share the same state." |
| 1027 | (interactive "P") |
| 1028 | (if arg |
| 1029 | ;; Mark all files. |
| 1030 | (progn |
| 1031 | ;; First check that no directory is marked, we can't mark |
| 1032 | ;; files in that case. |
| 1033 | (ewoc-map |
| 1034 | (lambda (filearg) |
| 1035 | (when (and (vc-dir-fileinfo->directory filearg) |
| 1036 | (vc-dir-fileinfo->directory filearg)) |
| 1037 | (error "Cannot mark all files, directory `%s' marked" |
| 1038 | (vc-dir-fileinfo->name filearg)))) |
| 1039 | vc-ewoc) |
| 1040 | (ewoc-map |
| 1041 | (lambda (filearg) |
| 1042 | (unless (vc-dir-fileinfo->marked filearg) |
| 1043 | (setf (vc-dir-fileinfo->marked filearg) t) |
| 1044 | t)) |
| 1045 | vc-ewoc)) |
| 1046 | (let ((data (ewoc-data (ewoc-locate vc-ewoc)))) |
| 1047 | (if (vc-dir-fileinfo->directory data) |
| 1048 | ;; It's a directory, mark child files. |
| 1049 | (let ((crt (ewoc-locate vc-ewoc))) |
| 1050 | (unless (vc-dir-children-marked-p crt) |
| 1051 | (while (setq crt (ewoc-next vc-ewoc crt)) |
| 1052 | (let ((crt-data (ewoc-data crt))) |
| 1053 | (unless (vc-dir-fileinfo->directory crt-data) |
| 1054 | (setf (vc-dir-fileinfo->marked crt-data) t) |
| 1055 | (ewoc-invalidate vc-ewoc crt)))))) |
| 1056 | ;; It's a file |
| 1057 | (let ((state (vc-dir-fileinfo->state data)) |
| 1058 | (crt (ewoc-nth vc-ewoc 0))) |
| 1059 | (while crt |
| 1060 | (let ((crt-data (ewoc-data crt))) |
| 1061 | (when (and (not (vc-dir-fileinfo->marked crt-data)) |
| 1062 | (eq (vc-dir-fileinfo->state crt-data) state) |
| 1063 | (not (vc-dir-fileinfo->directory crt-data))) |
| 1064 | (vc-dir-mark-file crt))) |
| 1065 | (setq crt (ewoc-next vc-ewoc crt)))))))) |
| 1066 | |
| 1067 | (defun vc-dir-unmark-file () |
| 1068 | ;; Unmark the current file and move to the next line. |
| 1069 | (let* ((crt (ewoc-locate vc-ewoc)) |
| 1070 | (file (ewoc-data crt))) |
| 1071 | (setf (vc-dir-fileinfo->marked file) nil) |
| 1072 | (ewoc-invalidate vc-ewoc crt) |
| 1073 | (unless (mouse-event-p last-command-event) |
| 1074 | (vc-dir-next-line 1)))) |
| 1075 | |
| 1076 | (defun vc-dir-unmark () |
| 1077 | "Unmark the current file or all files in the region. |
| 1078 | If the region is active, unmark all the files in the region. |
| 1079 | Otherwise mark the file on the current line and move to the next |
| 1080 | line." |
| 1081 | (interactive) |
| 1082 | (vc-dir-mark-unmark 'vc-dir-unmark-file)) |
| 1083 | |
| 1084 | (defun vc-dir-unmark-file-up () |
| 1085 | "Move to the previous line and unmark the file." |
| 1086 | (interactive) |
| 1087 | ;; If we're on the first line, we won't move up, but we will still |
| 1088 | ;; remove the mark. This seems a bit odd but it is what buffer-menu |
| 1089 | ;; does. |
| 1090 | (let* ((prev (ewoc-goto-prev vc-ewoc 1)) |
| 1091 | (file (ewoc-data prev))) |
| 1092 | (setf (vc-dir-fileinfo->marked file) nil) |
| 1093 | (ewoc-invalidate vc-ewoc prev) |
| 1094 | (vc-dir-move-to-goal-column))) |
| 1095 | |
| 1096 | (defun vc-dir-unmark-all-files (arg) |
| 1097 | "Unmark all files with the same state as the current one. |
| 1098 | With a prefix argument unmark all files. |
| 1099 | If the current entry is a directory, unmark all the child files. |
| 1100 | |
| 1101 | The VC commands operate on files that are on the same state. |
| 1102 | This command is intended to make it easy to deselect all files |
| 1103 | that share the same state." |
| 1104 | (interactive "P") |
| 1105 | (if arg |
| 1106 | (ewoc-map |
| 1107 | (lambda (filearg) |
| 1108 | (when (vc-dir-fileinfo->marked filearg) |
| 1109 | (setf (vc-dir-fileinfo->marked filearg) nil) |
| 1110 | t)) |
| 1111 | vc-ewoc) |
| 1112 | (let* ((crt (ewoc-locate vc-ewoc)) |
| 1113 | (data (ewoc-data crt))) |
| 1114 | (if (vc-dir-fileinfo->directory data) |
| 1115 | ;; It's a directory, unmark child files. |
| 1116 | (while (setq crt (ewoc-next vc-ewoc crt)) |
| 1117 | (let ((crt-data (ewoc-data crt))) |
| 1118 | (unless (vc-dir-fileinfo->directory crt-data) |
| 1119 | (setf (vc-dir-fileinfo->marked crt-data) nil) |
| 1120 | (ewoc-invalidate vc-ewoc crt)))) |
| 1121 | ;; It's a file |
| 1122 | (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt)))) |
| 1123 | (ewoc-map |
| 1124 | (lambda (filearg) |
| 1125 | (when (and (vc-dir-fileinfo->marked filearg) |
| 1126 | (eq (vc-dir-fileinfo->state filearg) crt-state)) |
| 1127 | (setf (vc-dir-fileinfo->marked filearg) nil) |
| 1128 | t)) |
| 1129 | vc-ewoc)))))) |
| 1130 | |
| 1131 | (defun vc-dir-toggle-mark-file () |
| 1132 | (let* ((crt (ewoc-locate vc-ewoc)) |
| 1133 | (file (ewoc-data crt))) |
| 1134 | (if (vc-dir-fileinfo->marked file) |
| 1135 | (vc-dir-unmark-file) |
| 1136 | (vc-dir-mark-file)))) |
| 1137 | |
| 1138 | (defun vc-dir-toggle-mark (e) |
| 1139 | (interactive "e") |
| 1140 | (vc-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file))) |
| 1141 | |
| 1142 | (defun vc-dir-delete-file () |
| 1143 | "Delete the marked files, or the current file if no marks." |
| 1144 | (interactive) |
| 1145 | (mapc 'vc-delete-file (or (vc-dir-marked-files) |
| 1146 | (list (vc-dir-current-file))))) |
| 1147 | |
| 1148 | (defun vc-dir-find-file () |
| 1149 | "Find the file on the current line." |
| 1150 | (interactive) |
| 1151 | (find-file (vc-dir-current-file))) |
| 1152 | |
| 1153 | (defun vc-dir-find-file-other-window () |
| 1154 | "Find the file on the current line, in another window." |
| 1155 | (interactive) |
| 1156 | (find-file-other-window (vc-dir-current-file))) |
| 1157 | |
| 1158 | (defun vc-dir-current-file () |
| 1159 | (let ((node (ewoc-locate vc-ewoc))) |
| 1160 | (unless node |
| 1161 | (error "No file available.")) |
| 1162 | (expand-file-name (vc-dir-fileinfo->name (ewoc-data node))))) |
| 1163 | |
| 1164 | (defun vc-dir-marked-files () |
| 1165 | "Return the list of marked files." |
| 1166 | (mapcar |
| 1167 | (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem))) |
| 1168 | (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked))) |
| 1169 | |
| 1170 | (defun vc-dir-marked-only-files () |
| 1171 | "Return the list of marked files, For marked directories return child files." |
| 1172 | (let ((crt (ewoc-nth vc-ewoc 0)) |
| 1173 | result) |
| 1174 | (while crt |
| 1175 | (let ((crt-data (ewoc-data crt))) |
| 1176 | (if (vc-dir-fileinfo->marked crt-data) |
| 1177 | (if (vc-dir-fileinfo->directory crt-data) |
| 1178 | (let* ((dir (vc-dir-fileinfo->directory crt-data)) |
| 1179 | (dirlen (length dir)) |
| 1180 | data) |
| 1181 | (while |
| 1182 | (and (setq crt (ewoc-next vc-ewoc crt)) |
| 1183 | (string-equal |
| 1184 | (substring |
| 1185 | (progn |
| 1186 | (setq data (ewoc-data crt)) |
| 1187 | (let ((crtdir (vc-dir-fileinfo->directory data))) |
| 1188 | (if crtdir |
| 1189 | crtdir |
| 1190 | (file-name-directory |
| 1191 | (expand-file-name |
| 1192 | (vc-dir-fileinfo->name data)))))) |
| 1193 | 0 dirlen) |
| 1194 | dir)) |
| 1195 | (unless (vc-dir-fileinfo->directory data) |
| 1196 | (push (vc-dir-fileinfo->name data) result)))) |
| 1197 | (push (expand-file-name (vc-dir-fileinfo->name crt-data)) result) |
| 1198 | (setq crt (ewoc-next vc-ewoc crt))) |
| 1199 | (setq crt (ewoc-next vc-ewoc crt))))) |
| 1200 | result)) |
| 1201 | |
| 1202 | (defun vc-directory-resynch-file (&optional fname) |
| 1203 | "Update the entries for FILE in any directory buffers that list it." |
| 1204 | (let ((file (or fname (expand-file-name buffer-file-name)))) |
| 1205 | ;; The vc-dir case |
| 1206 | (let ((found-vc-dir-buf nil)) |
| 1207 | (save-excursion |
| 1208 | (dolist (status-buf (buffer-list)) |
| 1209 | (set-buffer status-buf) |
| 1210 | ;; look for a vc-dir buffer that might show this file. |
| 1211 | (when (eq major-mode 'vc-dir-mode) |
| 1212 | (setq found-vc-dir-buf t) |
| 1213 | (let ((ddir (expand-file-name default-directory))) |
| 1214 | ;; This test is cvs-string-prefix-p |
| 1215 | (when (eq t (compare-strings file nil (length ddir) ddir nil nil)) |
| 1216 | (let* |
| 1217 | ((file-short (substring file (length ddir))) |
| 1218 | (state |
| 1219 | (funcall (vc-client-object->file-to-state vc-client-mode) |
| 1220 | file)) |
| 1221 | (extra |
| 1222 | (funcall (vc-client-object->file-to-extra vc-client-mode) |
| 1223 | file)) |
| 1224 | (entry |
| 1225 | (list file-short state extra))) |
| 1226 | (vc-dir-update (list entry) status-buf)))))) |
| 1227 | ;; We didn't find any vc-dir buffers, remove the hook, it is |
| 1228 | ;; not needed. |
| 1229 | (unless found-vc-dir-buf (remove-hook 'after-save-hook 'vc-directory-resynch-file)))))) |
| 1230 | |
| 1231 | (defun vc-dir-mode (client-object) |
| 1232 | "Major mode for showing the VC status for a directory. |
| 1233 | Marking/Unmarking key bindings and actions: |
| 1234 | m - marks a file/directory or if the region is active, mark all the files |
| 1235 | in region. |
| 1236 | Restrictions: - a file cannot be marked if any parent directory is marked |
| 1237 | - a directory cannot be marked if any child file or |
| 1238 | directory is marked |
| 1239 | u - marks a file/directory or if the region is active, unmark all the files |
| 1240 | in region. |
| 1241 | M - if the cursor is on a file: mark all the files with the same VC state as |
| 1242 | the current file |
| 1243 | - if the cursor is on a directory: mark all child files |
| 1244 | - with a prefix argument: mark all files |
| 1245 | U - if the cursor is on a file: unmark all the files with the same VC state |
| 1246 | as the current file |
| 1247 | - if the cursor is on a directory: unmark all child files |
| 1248 | - with a prefix argument: unmark all files |
| 1249 | |
| 1250 | |
| 1251 | \\{vc-dir-mode-map}" |
| 1252 | (setq mode-name (vc-client-object->name client-object)) |
| 1253 | (setq major-mode 'vc-dir-mode) |
| 1254 | (setq buffer-read-only t) |
| 1255 | (use-local-map vc-dir-mode-map) |
| 1256 | (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map) |
| 1257 | (set (make-local-variable 'vc-client-mode) client-object) |
| 1258 | (let ((buffer-read-only nil)) |
| 1259 | (erase-buffer) |
| 1260 | (set (make-local-variable 'vc-dir-process-buffer) nil) |
| 1261 | (set (make-local-variable 'vc-ewoc) |
| 1262 | (ewoc-create (vc-client-object->file-to-info client-object) |
| 1263 | (vc-client-object->headers client-object))) |
| 1264 | (add-hook 'after-save-hook 'vc-directory-resynch-file) |
| 1265 | ;; Make sure that if the VC status buffer is killed, the update |
| 1266 | ;; process running in the background is also killed. |
| 1267 | (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t) |
| 1268 | (funcall (vc-client-object->updater client-object))) |
| 1269 | (run-hooks 'vc-dir-mode-hook)) |
| 1270 | |
| 1271 | (put 'vc-dir-mode 'mode-class 'special) |
| 1272 | |
| 1273 | (defun vc-buffer-sync (&optional not-urgent) |
| 1274 | "Make sure the current buffer and its working file are in sync. |
| 1275 | NOT-URGENT means it is ok to continue if the user says not to save." |
| 1276 | (when (buffer-modified-p) |
| 1277 | (if (or vc-suppress-confirm |
| 1278 | (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name)))) |
| 1279 | (save-buffer) |
| 1280 | (unless not-urgent |
| 1281 | (error "Aborted"))))) |
| 1282 | |
| 1283 | (defun vc-dispatcher-browsing () |
| 1284 | "Are we in a directory browser buffer?" |
| 1285 | (derived-mode-p 'vc-dir-mode)) |
| 1286 | |
| 1287 | (defun vc-dispatcher-in-fileset-p (fileset) |
| 1288 | (let ((member nil)) |
| 1289 | (while (and (not member) fileset) |
| 1290 | (let ((elem (pop fileset))) |
| 1291 | (if (if (file-directory-p elem) |
| 1292 | (eq t (compare-strings buffer-file-name nil (length elem) |
| 1293 | elem nil nil)) |
| 1294 | (eq (current-buffer) (get-file-buffer elem))) |
| 1295 | (setq member t)))) |
| 1296 | member)) |
| 1297 | |
| 1298 | (defun vc-dispatcher-selection-set () |
| 1299 | "Deduce a set of files to which to apply an operation. Return the fileset. |
| 1300 | If we're in a directory display, the fileset is the list of marked files (if |
| 1301 | there is one) else the file on the curreent line. If not in a directory |
| 1302 | display, but the current buffer visits a file, the fileset is a singleton |
| 1303 | containing that file. Otherwise, throw an error." |
| 1304 | (let ((files |
| 1305 | (cond |
| 1306 | ;; Browsing with vc-dir |
| 1307 | ((vc-dispatcher-browsing) |
| 1308 | (or (vc-dir-marked-files) (list (vc-dir-current-file)))) |
| 1309 | ;; Visiting an eligible file |
| 1310 | ((buffer-file-name) |
| 1311 | (list buffer-file-name)) |
| 1312 | ;; No eligible file -- if there's a parent buffer, deduce from there |
| 1313 | ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer) |
| 1314 | (with-current-buffer vc-parent-buffer |
| 1315 | (vc-dispatcher-browsing)))) |
| 1316 | (with-current-buffer vc-parent-buffer |
| 1317 | (vc-dispatcher-selection-set))) |
| 1318 | ;; No good set here, throw error |
| 1319 | (t (error "No fileset is available here"))))) |
| 1320 | ;; We assume, in order to avoid unpleasant surprises to the user, |
| 1321 | ;; that a fileset is not in good shape to be handed to the user if the |
| 1322 | ;; buffers visiting the fileset don't match the on-disk contents. |
| 1323 | ;; This is actually untrue for operations like `print-log' (or `diff' |
| 1324 | ;; between two revisions), so maybe this should be moved elsewhere. |
| 1325 | (save-some-buffers |
| 1326 | nil (lambda () (vc-dispatcher-in-fileset-p files))) |
| 1327 | files)) |
| 1328 | |
| 1329 | ;; arch-tag: 7d08b17f-5470-4799-914b-bfb9fcf6a246 |
| 1330 | ;;; vc-dispatcher.el ends here |