vc-dispatcher.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. ;;; vc-dispatcher.el -- generic command-dispatcher facility.
  2. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: FSF (see below for full credits)
  4. ;; Maintainer: Eric S. Raymond <esr@thyrsus.com>
  5. ;; Keywords: vc tools
  6. ;; Package: vc
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Credits:
  19. ;; Designed and implemented by Eric S. Raymond, originally as part of VC mode.
  20. ;; Stefan Monnier and Dan Nicolaescu contributed substantial work on the
  21. ;; vc-dir front end.
  22. ;;; Commentary:
  23. ;; Goals:
  24. ;;
  25. ;; There is a class of front-ending problems that Emacs might be used
  26. ;; to address that involves selecting sets of files, or possibly
  27. ;; directories, and passing the selection set to slave commands. The
  28. ;; prototypical example, from which this code is derived, is talking
  29. ;; to version-control systems.
  30. ;;
  31. ;; vc-dispatcher.el is written to decouple the UI issues in such front
  32. ;; ends from their application-specific logic. It also provides a
  33. ;; service layer for running the slave commands either synchronously
  34. ;; or asynchronously and managing the message/error logs from the
  35. ;; command runs.
  36. ;;
  37. ;; Similar UI problems can be expected to come up in applications
  38. ;; areas other than VCSes; IDEs and document search are two obvious ones.
  39. ;; This mode is intended to ensure that the Emacs interfaces for all such
  40. ;; beasts are consistent and carefully designed. But even if nothing
  41. ;; but VC ever uses it, getting the layer separation right will be
  42. ;; a valuable thing.
  43. ;; Dispatcher's universe:
  44. ;;
  45. ;; The universe consists of the file tree rooted at the current
  46. ;; directory. The dispatcher's upper layer deduces some subset
  47. ;; of the file tree from the state of the currently visited buffer
  48. ;; and returns that subset, presumably to a client mode.
  49. ;;
  50. ;; The user may be looking at either of two different views; a buffer
  51. ;; visiting a file, or a directory buffer generated by vc-dispatcher.
  52. ;;
  53. ;; The lower layer of this mode runs commands in subprocesses, either
  54. ;; synchronously or asynchronously. Commands may be launched in one
  55. ;; of two ways: they may be run immediately, or the calling mode can
  56. ;; create a closure associated with a text-entry buffer, to be
  57. ;; executed when the user types C-c to ship the buffer contents. In
  58. ;; either case the command messages and error (if any) will remain
  59. ;; available in a status buffer.
  60. ;; Special behavior of dispatcher directory buffers:
  61. ;;
  62. ;; In dispatcher directory buffers, facilities to perform basic
  63. ;; navigation and selection operations are provided by keymap and menu
  64. ;; entries that dispatcher sets up itself, so they'll be uniform
  65. ;; across all dispatcher-using client modes. Client modes are
  66. ;; expected to append to these to provide mode-specific bindings.
  67. ;;
  68. ;; The standard map associates a 'state' slot (that the client mode
  69. ;; may set) with each directory entry. The dispatcher knows nothing
  70. ;; about the semantics of individual states, but mark and unmark commands
  71. ;; treat all entries with the same state as the currently selected one as
  72. ;; a unit.
  73. ;; The interface:
  74. ;;
  75. ;; The main interface to the lower level is vc-do-command. This launches a
  76. ;; command, synchronously or asynchronously, making the output available
  77. ;; in a command log buffer. Two other functions, (vc-start-logentry) and
  78. ;; (vc-finish-logentry), allow you to associate a command closure with an
  79. ;; annotation buffer so that when the user confirms the comment the closure
  80. ;; is run (with the comment as part of its context).
  81. ;;
  82. ;; The interface to the upper level has the two main entry points (vc-dir)
  83. ;; and (vc-dispatcher-selection-set) and a couple of convenience functions.
  84. ;; (vc-dir) sets up a dispatcher browsing buffer; (vc-dispatcher-selection-set)
  85. ;; returns a selection set of files, either the marked files in a browsing
  86. ;; buffer or the singleton set consisting of the file visited by the current
  87. ;; buffer (when that is appropriate). It also does what is needed to ensure
  88. ;; that on-disk files and the contents of their visiting Emacs buffers
  89. ;; coincide.
  90. ;;
  91. ;; When the client mode adds a local vc-mode-line-hook to a buffer, it
  92. ;; will be called with the buffer file name as argument whenever the
  93. ;; dispatcher resyncs the buffer.
  94. ;; To do:
  95. ;;
  96. ;; - log buffers need font-locking.
  97. ;;
  98. ;; General customization
  99. (defcustom vc-logentry-check-hook nil
  100. "Normal hook run by `vc-finish-logentry'.
  101. Use this to impose your own rules on the entry in addition to any the
  102. dispatcher client mode imposes itself."
  103. :type 'hook
  104. :group 'vc)
  105. (defcustom vc-delete-logbuf-window t
  106. "If non-nil, delete the log buffer and window after each logical action.
  107. If nil, bury that buffer instead.
  108. This is most useful if you have multiple windows on a frame and would like to
  109. preserve the setting."
  110. :type 'boolean
  111. :group 'vc)
  112. (defcustom vc-command-messages nil
  113. "If non-nil, display run messages from back-end commands."
  114. :type 'boolean
  115. :group 'vc)
  116. (defcustom vc-suppress-confirm nil
  117. "If non-nil, treat user as expert; suppress yes-no prompts on some things."
  118. :type 'boolean
  119. :group 'vc)
  120. ;; Variables the user doesn't need to know about.
  121. (defvar vc-log-operation nil)
  122. (defvar vc-log-after-operation-hook nil)
  123. (defvar vc-log-fileset)
  124. ;; In a log entry buffer, this is a local variable
  125. ;; that points to the buffer for which it was made
  126. ;; (either a file, or a directory buffer).
  127. (defvar vc-parent-buffer nil)
  128. (put 'vc-parent-buffer 'permanent-local t)
  129. (defvar vc-parent-buffer-name nil)
  130. (put 'vc-parent-buffer-name 'permanent-local t)
  131. ;; Common command execution logic
  132. (defun vc-process-filter (p s)
  133. "An alternative output filter for async process P.
  134. One difference with the default filter is that this inserts S after markers.
  135. Another is that undo information is not kept."
  136. (let ((buffer (process-buffer p)))
  137. (when (buffer-live-p buffer)
  138. (with-current-buffer buffer
  139. (save-excursion
  140. (let ((buffer-undo-list t)
  141. (inhibit-read-only t))
  142. (goto-char (process-mark p))
  143. (insert s)
  144. (set-marker (process-mark p) (point))))))))
  145. (defun vc-setup-buffer (buf)
  146. "Prepare BUF for executing a slave command and make it current."
  147. (let ((camefrom (current-buffer))
  148. (olddir default-directory))
  149. (set-buffer (get-buffer-create buf))
  150. (kill-all-local-variables)
  151. (set (make-local-variable 'vc-parent-buffer) camefrom)
  152. (set (make-local-variable 'vc-parent-buffer-name)
  153. (concat " from " (buffer-name camefrom)))
  154. (setq default-directory olddir)
  155. (let ((buffer-undo-list t)
  156. (inhibit-read-only t))
  157. (erase-buffer))))
  158. (defvar vc-sentinel-movepoint) ;Dynamically scoped.
  159. (defun vc-process-sentinel (p s)
  160. (let ((previous (process-get p 'vc-previous-sentinel))
  161. (buf (process-buffer p)))
  162. ;; Impatient users sometime kill "slow" buffers; check liveness
  163. ;; to avoid "error in process sentinel: Selecting deleted buffer".
  164. (when (buffer-live-p buf)
  165. (when previous (funcall previous p s))
  166. (with-current-buffer buf
  167. (setq mode-line-process
  168. (let ((status (process-status p)))
  169. ;; Leave mode-line uncluttered, normally.
  170. (unless (eq 'exit status)
  171. (format " (%s)" status))))
  172. (let (vc-sentinel-movepoint)
  173. ;; Normally, we want async code such as sentinels to not move point.
  174. (save-excursion
  175. (goto-char (process-mark p))
  176. (let ((cmds (process-get p 'vc-sentinel-commands)))
  177. (process-put p 'vc-sentinel-commands nil)
  178. (dolist (cmd cmds)
  179. ;; Each sentinel may move point and the next one should be run
  180. ;; at that new point. We could get the same result by having
  181. ;; each sentinel read&set process-mark, but since `cmd' needs
  182. ;; to work both for async and sync processes, this would be
  183. ;; difficult to achieve.
  184. (vc-exec-after cmd))))
  185. ;; But sometimes the sentinels really want to move point.
  186. (when vc-sentinel-movepoint
  187. (let ((win (get-buffer-window (current-buffer) 0)))
  188. (if (not win)
  189. (goto-char vc-sentinel-movepoint)
  190. (with-selected-window win
  191. (goto-char vc-sentinel-movepoint))))))))))
  192. (defun vc-set-mode-line-busy-indicator ()
  193. (setq mode-line-process
  194. (concat " " (propertize "[waiting...]"
  195. 'face 'mode-line-emphasis
  196. 'help-echo
  197. "A command is in progress in this buffer"))))
  198. (defun vc-exec-after (code)
  199. "Eval CODE when the current buffer's process is done.
  200. If the current buffer has no process, just evaluate CODE.
  201. Else, add CODE to the process' sentinel."
  202. (let ((proc (get-buffer-process (current-buffer))))
  203. (cond
  204. ;; If there's no background process, just execute the code.
  205. ;; We used to explicitly call delete-process on exited processes,
  206. ;; but this led to timing problems causing process output to be
  207. ;; lost. Terminated processes get deleted automatically
  208. ;; anyway. -- cyd
  209. ((or (null proc) (eq (process-status proc) 'exit))
  210. ;; Make sure we've read the process's output before going further.
  211. (when proc (accept-process-output proc))
  212. (eval code))
  213. ;; If a process is running, add CODE to the sentinel
  214. ((eq (process-status proc) 'run)
  215. (vc-set-mode-line-busy-indicator)
  216. (let ((previous (process-sentinel proc)))
  217. (unless (eq previous 'vc-process-sentinel)
  218. (process-put proc 'vc-previous-sentinel previous))
  219. (set-process-sentinel proc 'vc-process-sentinel))
  220. (process-put proc 'vc-sentinel-commands
  221. ;; We keep the code fragments in the order given
  222. ;; so that vc-diff-finish's message shows up in
  223. ;; the presence of non-nil vc-command-messages.
  224. (append (process-get proc 'vc-sentinel-commands)
  225. (list code))))
  226. (t (error "Unexpected process state"))))
  227. nil)
  228. (defvar vc-post-command-functions nil
  229. "Hook run at the end of `vc-do-command'.
  230. Each function is called inside the buffer in which the command was run
  231. and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
  232. (defvar w32-quote-process-args)
  233. (defun vc-delistify (filelist)
  234. "Smash a FILELIST into a file list string suitable for info messages."
  235. ;; FIXME what about file names with spaces?
  236. (if (not filelist) "." (mapconcat 'identity filelist " ")))
  237. ;;;###autoload
  238. (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
  239. "Execute a slave command, notifying user and checking for errors.
  240. Output from COMMAND goes to BUFFER, or the current buffer if
  241. BUFFER is t. If the destination buffer is not already current,
  242. set it up properly and erase it. The command is considered
  243. successful if its exit status does not exceed OKSTATUS (if
  244. OKSTATUS is nil, that means to ignore error status, if it is
  245. `async', that means not to wait for termination of the
  246. subprocess; if it is t it means to ignore all execution errors).
  247. FILE-OR-LIST is the name of a working file; it may be a list of
  248. files or be nil (to execute commands that don't expect a file
  249. name or set of files). If an optional list of FLAGS is present,
  250. that is inserted into the command line before the filename.
  251. Return the return value of the slave command in the synchronous
  252. case, and the process object in the asynchronous case."
  253. ;; FIXME: file-relative-name can return a bogus result because
  254. ;; it doesn't look at the actual file-system to see if symlinks
  255. ;; come into play.
  256. (let* ((files
  257. (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
  258. (if (listp file-or-list) file-or-list (list file-or-list))))
  259. (full-command
  260. ;; What we're doing here is preparing a version of the command
  261. ;; for display in a debug-progress message. If it's fewer than
  262. ;; 20 characters display the entire command (without trailing
  263. ;; newline). Otherwise display the first 20 followed by an ellipsis.
  264. (concat (if (string= (substring command -1) "\n")
  265. (substring command 0 -1)
  266. command)
  267. " "
  268. (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
  269. " " (vc-delistify files))))
  270. (save-current-buffer
  271. (unless (or (eq buffer t)
  272. (and (stringp buffer)
  273. (string= (buffer-name) buffer))
  274. (eq buffer (current-buffer)))
  275. (vc-setup-buffer buffer))
  276. ;; If there's some previous async process still running, just kill it.
  277. (let ((oldproc (get-buffer-process (current-buffer))))
  278. ;; If we wanted to wait for oldproc to finish before doing
  279. ;; something, we'd have used vc-eval-after.
  280. ;; Use `delete-process' rather than `kill-process' because we don't
  281. ;; want any of its output to appear from now on.
  282. (when oldproc (delete-process oldproc)))
  283. (let ((squeezed (remq nil flags))
  284. (inhibit-read-only t)
  285. (status 0))
  286. (when files
  287. (setq squeezed (nconc squeezed files)))
  288. (let (;; Since some functions need to parse the output
  289. ;; from external commands, set LC_MESSAGES to C.
  290. (process-environment (cons "LC_MESSAGES=C" process-environment))
  291. (w32-quote-process-args t))
  292. (if (eq okstatus 'async)
  293. ;; Run asynchronously.
  294. (let ((proc
  295. (let ((process-connection-type nil))
  296. (apply 'start-file-process command (current-buffer)
  297. command squeezed))))
  298. (when vc-command-messages
  299. (message "Running %s in background..." full-command))
  300. ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
  301. (set-process-filter proc 'vc-process-filter)
  302. (setq status proc)
  303. (when vc-command-messages
  304. (vc-exec-after
  305. `(message "Running %s in background... done" ',full-command))))
  306. ;; Run synchronously
  307. (when vc-command-messages
  308. (message "Running %s in foreground..." full-command))
  309. (let ((buffer-undo-list t))
  310. (setq status (apply 'process-file command nil t nil squeezed)))
  311. (when (and (not (eq t okstatus))
  312. (or (not (integerp status))
  313. (and okstatus (< okstatus status))))
  314. (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
  315. (pop-to-buffer (current-buffer))
  316. (goto-char (point-min))
  317. (shrink-window-if-larger-than-buffer))
  318. (error "Running %s...FAILED (%s)" full-command
  319. (if (integerp status) (format "status %d" status) status)))
  320. (when vc-command-messages
  321. (message "Running %s...OK = %d" full-command status))))
  322. (vc-exec-after
  323. `(run-hook-with-args 'vc-post-command-functions
  324. ',command ',file-or-list ',flags))
  325. status))))
  326. (defun vc-do-async-command (buffer root command &rest args)
  327. "Run COMMAND asynchronously with ARGS, displaying the result.
  328. Send the output to BUFFER, which should be a buffer or the name
  329. of a buffer, which is created.
  330. ROOT should be the directory in which the command should be run.
  331. Display the buffer in some window, but don't select it."
  332. (let* ((dir default-directory)
  333. (inhibit-read-only t)
  334. window new-window-start)
  335. (setq buffer (get-buffer-create buffer))
  336. (if (get-buffer-process buffer)
  337. (error "Another VC action on %s is running" root))
  338. (with-current-buffer buffer
  339. (setq default-directory root)
  340. (goto-char (point-max))
  341. (unless (eq (point) (point-min))
  342. (insert " \n"))
  343. (setq new-window-start (point))
  344. (insert "Running \"" command)
  345. (dolist (arg args)
  346. (insert " " arg))
  347. (insert "\"...\n")
  348. ;; Run in the original working directory.
  349. (let ((default-directory dir))
  350. (apply 'vc-do-command t 'async command nil args)))
  351. (setq window (display-buffer buffer))
  352. (if window
  353. (set-window-start window new-window-start))
  354. buffer))
  355. (defun vc-set-async-update (process-buffer)
  356. "Set a `vc-exec-after' action appropriate to the current buffer.
  357. This action will update the current buffer after the current
  358. asynchronous VC command has completed. PROCESS-BUFFER is the
  359. buffer for the asynchronous VC process.
  360. If the current buffer is a VC Dir buffer, call `vc-dir-refresh'.
  361. If the current buffer is a Dired buffer, revert it."
  362. (let* ((buf (current-buffer))
  363. (tick (buffer-modified-tick buf)))
  364. (cond
  365. ((derived-mode-p 'vc-dir-mode)
  366. (with-current-buffer process-buffer
  367. (vc-exec-after
  368. `(if (buffer-live-p ,buf)
  369. (with-current-buffer ,buf
  370. (vc-dir-refresh))))))
  371. ((derived-mode-p 'dired-mode)
  372. (with-current-buffer process-buffer
  373. (vc-exec-after
  374. `(and (buffer-live-p ,buf)
  375. (= (buffer-modified-tick ,buf) ,tick)
  376. (with-current-buffer ,buf
  377. (revert-buffer)))))))))
  378. ;; These functions are used to ensure that the view the user sees is up to date
  379. ;; even if the dispatcher client mode has messed with file contents (as in,
  380. ;; for example, VCS keyword expansion).
  381. (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
  382. (defun vc-position-context (posn)
  383. "Save a bit of the text around POSN in the current buffer.
  384. Used to help us find the corresponding position again later
  385. if markers are destroyed or corrupted."
  386. ;; A lot of this was shamelessly lifted from Sebastian Kremer's
  387. ;; rcs.el mode.
  388. (list posn
  389. (buffer-size)
  390. (buffer-substring posn
  391. (min (point-max) (+ posn 100)))))
  392. (defun vc-find-position-by-context (context)
  393. "Return the position of CONTEXT in the current buffer.
  394. If CONTEXT cannot be found, return nil."
  395. (let ((context-string (nth 2 context)))
  396. (if (equal "" context-string)
  397. (point-max)
  398. (save-excursion
  399. (let ((diff (- (nth 1 context) (buffer-size))))
  400. (when (< diff 0) (setq diff (- diff)))
  401. (goto-char (nth 0 context))
  402. (if (or (search-forward context-string nil t)
  403. ;; Can't use search-backward since the match may continue
  404. ;; after point.
  405. (progn (goto-char (- (point) diff (length context-string)))
  406. ;; goto-char doesn't signal an error at
  407. ;; beginning of buffer like backward-char would
  408. (search-forward context-string nil t)))
  409. ;; to beginning of OSTRING
  410. (- (point) (length context-string))))))))
  411. (defun vc-context-matches-p (posn context)
  412. "Return t if POSN matches CONTEXT, nil otherwise."
  413. (let* ((context-string (nth 2 context))
  414. (len (length context-string))
  415. (end (+ posn len)))
  416. (if (> end (1+ (buffer-size)))
  417. nil
  418. (string= context-string (buffer-substring posn end)))))
  419. (defun vc-buffer-context ()
  420. "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
  421. Used by `vc-restore-buffer-context' to later restore the context."
  422. (let ((point-context (vc-position-context (point)))
  423. ;; Use mark-marker to avoid confusion in transient-mark-mode.
  424. (mark-context (when (eq (marker-buffer (mark-marker)) (current-buffer))
  425. (vc-position-context (mark-marker))))
  426. ;; Make the right thing happen in transient-mark-mode.
  427. (mark-active nil))
  428. (list point-context mark-context nil)))
  429. (defun vc-restore-buffer-context (context)
  430. "Restore point/mark, and reparse any affected compilation buffers.
  431. CONTEXT is that which `vc-buffer-context' returns."
  432. (let ((point-context (nth 0 context))
  433. (mark-context (nth 1 context)))
  434. ;; if necessary, restore point and mark
  435. (if (not (vc-context-matches-p (point) point-context))
  436. (let ((new-point (vc-find-position-by-context point-context)))
  437. (when new-point (goto-char new-point))))
  438. (and mark-active
  439. mark-context
  440. (not (vc-context-matches-p (mark) mark-context))
  441. (let ((new-mark (vc-find-position-by-context mark-context)))
  442. (when new-mark (set-mark new-mark))))))
  443. (defun vc-revert-buffer-internal (&optional arg no-confirm)
  444. "Revert buffer, keeping point and mark where user expects them.
  445. Try to be clever in the face of changes due to expanded version-control
  446. key words. This is important for typeahead to work as expected.
  447. ARG and NO-CONFIRM are passed on to `revert-buffer'."
  448. (interactive "P")
  449. (widen)
  450. (let ((context (vc-buffer-context)))
  451. ;; Use save-excursion here, because it may be able to restore point
  452. ;; and mark properly even in cases where vc-restore-buffer-context
  453. ;; would fail. However, save-excursion might also get it wrong --
  454. ;; in this case, vc-restore-buffer-context gives it a second try.
  455. (save-excursion
  456. ;; t means don't call normal-mode;
  457. ;; that's to preserve various minor modes.
  458. (revert-buffer arg no-confirm t))
  459. (vc-restore-buffer-context context)))
  460. (defvar vc-mode-line-hook nil)
  461. (make-variable-buffer-local 'vc-mode-line-hook)
  462. (put 'vc-mode-line-hook 'permanent-local t)
  463. (defun vc-resynch-window (file &optional keep noquery reset-vc-info)
  464. "If FILE is in the current buffer, either revert or unvisit it.
  465. The choice between revert (to see expanded keywords) and unvisit
  466. depends on KEEP. NOQUERY if non-nil inhibits confirmation for
  467. reverting. NOQUERY should be t *only* if it is known the only
  468. difference between the buffer and the file is due to
  469. modifications by the dispatcher client code, rather than user
  470. editing!"
  471. (and (string= buffer-file-name file)
  472. (if keep
  473. (when (file-exists-p file)
  474. (when reset-vc-info
  475. (vc-file-clearprops file))
  476. (vc-revert-buffer-internal t noquery)
  477. ;; VC operations might toggle the read-only state. In
  478. ;; that case we need to adjust the `view-mode' status
  479. ;; when `view-read-only' is non-nil.
  480. (and view-read-only
  481. (if (file-writable-p file)
  482. (and view-mode
  483. (let ((view-old-buffer-read-only nil))
  484. (view-mode-exit)))
  485. (and (not view-mode)
  486. (not (eq (get major-mode 'mode-class) 'special))
  487. (view-mode-enter))))
  488. ;; FIXME: Why use a hook? Why pass it buffer-file-name?
  489. (run-hook-with-args 'vc-mode-line-hook buffer-file-name))
  490. (kill-buffer (current-buffer)))))
  491. (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
  492. (defun vc-resynch-buffers-in-directory (directory &optional keep noquery reset-vc-info)
  493. "Resync all buffers that visit files in DIRECTORY."
  494. (dolist (buffer (buffer-list))
  495. (let ((fname (buffer-file-name buffer)))
  496. (when (and fname (string-prefix-p directory fname))
  497. (with-current-buffer buffer
  498. (vc-resynch-buffer fname keep noquery reset-vc-info))))))
  499. (defun vc-resynch-buffer (file &optional keep noquery reset-vc-info)
  500. "If FILE is currently visited, resynch its buffer."
  501. (if (string= buffer-file-name file)
  502. (vc-resynch-window file keep noquery reset-vc-info)
  503. (if (file-directory-p file)
  504. (vc-resynch-buffers-in-directory file keep noquery reset-vc-info)
  505. (let ((buffer (get-file-buffer file)))
  506. (when buffer
  507. (with-current-buffer buffer
  508. (vc-resynch-window file keep noquery reset-vc-info))))))
  509. ;; Try to avoid unnecessary work, a *vc-dir* buffer is only present
  510. ;; if this is true.
  511. (when vc-dir-buffers
  512. (vc-dir-resynch-file file)))
  513. (defun vc-buffer-sync (&optional not-urgent)
  514. "Make sure the current buffer and its working file are in sync.
  515. NOT-URGENT means it is ok to continue if the user says not to save."
  516. (when (buffer-modified-p)
  517. (if (or vc-suppress-confirm
  518. (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
  519. (save-buffer)
  520. (unless not-urgent
  521. (error "Aborted")))))
  522. ;; Command closures
  523. ;; Set up key bindings for use while editing log messages
  524. (defun vc-log-edit (fileset mode)
  525. "Set up `log-edit' for use on FILE."
  526. (setq default-directory
  527. (with-current-buffer vc-parent-buffer default-directory))
  528. (log-edit 'vc-finish-logentry
  529. nil
  530. `((log-edit-listfun . (lambda ()
  531. ;; FIXME: Should expand the list
  532. ;; for directories.
  533. (mapcar 'file-relative-name
  534. ',fileset)))
  535. (log-edit-diff-function . (lambda () (vc-diff nil))))
  536. nil
  537. mode)
  538. (set (make-local-variable 'vc-log-fileset) fileset)
  539. (set-buffer-modified-p nil)
  540. (setq buffer-file-name nil))
  541. (defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook)
  542. "Accept a comment for an operation on FILES.
  543. If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
  544. action on close to ACTION. If COMMENT is a string and
  545. INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
  546. contents of the log entry buffer. If COMMENT is a string and
  547. INITIAL-CONTENTS is nil, do action immediately as if the user had
  548. entered COMMENT. If COMMENT is t, also do action immediately with an
  549. empty comment. Remember the file's buffer in `vc-parent-buffer'
  550. \(current one if no file). Puts the log-entry buffer in major-mode
  551. MODE, defaulting to `log-edit-mode' if MODE is nil.
  552. AFTER-HOOK specifies the local value for `vc-log-after-operation-hook'."
  553. (let ((parent
  554. (if (vc-dispatcher-browsing)
  555. ;; If we are called from a directory browser, the parent buffer is
  556. ;; the current buffer.
  557. (current-buffer)
  558. (if (and files (equal (length files) 1))
  559. (get-file-buffer (car files))
  560. (current-buffer)))))
  561. (if (and comment (not initial-contents))
  562. (set-buffer (get-buffer-create logbuf))
  563. (pop-to-buffer (get-buffer-create logbuf)))
  564. (set (make-local-variable 'vc-parent-buffer) parent)
  565. (set (make-local-variable 'vc-parent-buffer-name)
  566. (concat " from " (buffer-name vc-parent-buffer)))
  567. (vc-log-edit files mode)
  568. (make-local-variable 'vc-log-after-operation-hook)
  569. (when after-hook
  570. (setq vc-log-after-operation-hook after-hook))
  571. (setq vc-log-operation action)
  572. (when comment
  573. (erase-buffer)
  574. (when (stringp comment) (insert comment)))
  575. (if (or (not comment) initial-contents)
  576. (message "%s Type C-c C-c when done" msg)
  577. (vc-finish-logentry (eq comment t)))))
  578. (declare-function vc-dir-move-to-goal-column "vc-dir" ())
  579. ;; vc-finish-logentry is typically called from a log-edit buffer (see
  580. ;; vc-start-logentry).
  581. (defun vc-finish-logentry (&optional nocomment)
  582. "Complete the operation implied by the current log entry.
  583. Use the contents of the current buffer as a check-in or registration
  584. comment. If the optional arg NOCOMMENT is non-nil, then don't check
  585. the buffer contents as a comment."
  586. (interactive)
  587. ;; Check and record the comment, if any.
  588. (unless nocomment
  589. (run-hooks 'vc-logentry-check-hook))
  590. ;; Sync parent buffer in case the user modified it while editing the comment.
  591. ;; But not if it is a vc-dir buffer.
  592. (with-current-buffer vc-parent-buffer
  593. (or (vc-dispatcher-browsing) (vc-buffer-sync)))
  594. (unless vc-log-operation
  595. (error "No log operation is pending"))
  596. ;; save the parameters held in buffer-local variables
  597. (let ((logbuf (current-buffer))
  598. (log-operation vc-log-operation)
  599. ;; FIXME: When coming from VC-Dir, we should check that the
  600. ;; set of selected files is still equal to vc-log-fileset,
  601. ;; to avoid surprises.
  602. (log-fileset vc-log-fileset)
  603. (log-entry (buffer-string))
  604. (after-hook vc-log-after-operation-hook))
  605. (pop-to-buffer vc-parent-buffer)
  606. ;; OK, do it to it
  607. (save-excursion
  608. (funcall log-operation
  609. log-fileset
  610. log-entry))
  611. ;; Quit windows on logbuf.
  612. (cond
  613. ((not logbuf))
  614. (vc-delete-logbuf-window
  615. (quit-windows-on logbuf t (selected-frame)))
  616. (t
  617. (quit-windows-on logbuf nil 0)))
  618. ;; Now make sure we see the expanded headers
  619. (when log-fileset
  620. (mapc
  621. (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
  622. log-fileset))
  623. (when (vc-dispatcher-browsing)
  624. (vc-dir-move-to-goal-column))
  625. (run-hooks after-hook 'vc-finish-logentry-hook)))
  626. (defun vc-dispatcher-browsing ()
  627. "Are we in a directory browser buffer?"
  628. (derived-mode-p 'vc-dir-mode))
  629. ;; These are unused.
  630. ;; (defun vc-dispatcher-in-fileset-p (fileset)
  631. ;; (let ((member nil))
  632. ;; (while (and (not member) fileset)
  633. ;; (let ((elem (pop fileset)))
  634. ;; (if (if (file-directory-p elem)
  635. ;; (eq t (compare-strings buffer-file-name nil (length elem)
  636. ;; elem nil nil))
  637. ;; (eq (current-buffer) (get-file-buffer elem)))
  638. ;; (setq member t))))
  639. ;; member))
  640. ;; (defun vc-dispatcher-selection-set (&optional observer)
  641. ;; "Deduce a set of files to which to apply an operation. Return a cons
  642. ;; cell (SELECTION . FILESET), where SELECTION is what the user chose
  643. ;; and FILES is the flist with any directories replaced by the listed files
  644. ;; within them.
  645. ;; If we're in a directory display, the fileset is the list of marked files (if
  646. ;; there is one) else the file on the current line. If not in a directory
  647. ;; display, but the current buffer visits a file, the fileset is a singleton
  648. ;; containing that file. Otherwise, throw an error."
  649. ;; (let ((selection
  650. ;; (cond
  651. ;; ;; Browsing with vc-dir
  652. ;; ((vc-dispatcher-browsing)
  653. ;; ;; If no files are marked, temporarily mark current file
  654. ;; ;; and choose on that basis (so we get subordinate files)
  655. ;; (if (not (vc-dir-marked-files))
  656. ;; (prog2
  657. ;; (vc-dir-mark-file)
  658. ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))
  659. ;; (vc-dir-unmark-all-files t))
  660. ;; (cons (vc-dir-marked-files) (vc-dir-marked-only-files))))
  661. ;; ;; Visiting an eligible file
  662. ;; ((buffer-file-name)
  663. ;; (cons (list buffer-file-name) (list buffer-file-name)))
  664. ;; ;; No eligible file -- if there's a parent buffer, deduce from there
  665. ;; ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
  666. ;; (with-current-buffer vc-parent-buffer
  667. ;; (vc-dispatcher-browsing))))
  668. ;; (with-current-buffer vc-parent-buffer
  669. ;; (vc-dispatcher-selection-set)))
  670. ;; ;; No good set here, throw error
  671. ;; (t (error "No fileset is available here")))))
  672. ;; ;; We assume, in order to avoid unpleasant surprises to the user,
  673. ;; ;; that a fileset is not in good shape to be handed to the user if the
  674. ;; ;; buffers visiting the fileset don't match the on-disk contents.
  675. ;; (unless observer
  676. ;; (save-some-buffers
  677. ;; nil (lambda () (vc-dispatcher-in-fileset-p (cdr selection)))))
  678. ;; selection))
  679. (provide 'vc-dispatcher)
  680. ;;; vc-dispatcher.el ends here