vc-dir.el 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. ;;; vc-dir.el --- Directory status display under VC -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. ;; Author: Dan Nicolaescu <dann@ics.uci.edu>
  4. ;; Keywords: vc tools
  5. ;; Package: vc
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Credits:
  18. ;; The original VC directory status implementation was based on dired.
  19. ;; This implementation was inspired by PCL-CVS.
  20. ;; Many people contributed comments, ideas and code to this
  21. ;; implementation. These include:
  22. ;;
  23. ;; Alexandre Julliard <julliard@winehq.org>
  24. ;; Stefan Monnier <monnier@iro.umontreal.ca>
  25. ;; Tom Tromey <tromey@redhat.com>
  26. ;;; Commentary:
  27. ;;
  28. ;;; Todo: see vc.el.
  29. (require 'vc-hooks)
  30. (require 'vc)
  31. (require 'tool-bar)
  32. (require 'ewoc)
  33. ;;; Code:
  34. (eval-when-compile (require 'cl-lib))
  35. (defcustom vc-dir-mode-hook nil
  36. "Normal hook run by `vc-dir-mode'.
  37. See `run-hooks'."
  38. :type 'hook
  39. :group 'vc)
  40. ;; Used to store information for the files displayed in the directory buffer.
  41. ;; Each item displayed corresponds to one of these defstructs.
  42. (cl-defstruct (vc-dir-fileinfo
  43. (:copier nil)
  44. (:type list) ;So we can use `member' on lists of FIs.
  45. (:constructor
  46. ;; We could define it as an alias for `list'.
  47. vc-dir-create-fileinfo (name state &optional extra marked directory))
  48. (:conc-name vc-dir-fileinfo->))
  49. name ;Keep it as first, for `member'.
  50. state
  51. ;; For storing backend specific information.
  52. extra
  53. marked
  54. ;; To keep track of not updated files during a global refresh
  55. needs-update
  56. ;; To distinguish files and directories.
  57. directory)
  58. (defvar vc-ewoc nil)
  59. (defvar vc-dir-process-buffer nil
  60. "The buffer used for the asynchronous call that computes status.")
  61. (defvar vc-dir-backend nil
  62. "The backend used by the current *vc-dir* buffer.")
  63. (defun vc-dir-move-to-goal-column ()
  64. ;; Used to keep the cursor on the file name column.
  65. (beginning-of-line)
  66. (unless (eolp)
  67. ;; Must be in sync with vc-default-dir-printer.
  68. (forward-char 25)))
  69. (defun vc-dir-prepare-status-buffer (bname dir backend &optional create-new)
  70. "Find a buffer named BNAME showing DIR, or create a new one."
  71. (setq dir (file-name-as-directory (expand-file-name dir)))
  72. (let* ;; Look for another buffer name BNAME visiting the same directory.
  73. ((buf (save-excursion
  74. (unless create-new
  75. (cl-dolist (buffer vc-dir-buffers)
  76. (when (buffer-live-p buffer)
  77. (set-buffer buffer)
  78. (when (and (derived-mode-p 'vc-dir-mode)
  79. (eq vc-dir-backend backend)
  80. (string= default-directory dir))
  81. (cl-return buffer))))))))
  82. (or buf
  83. ;; Create a new buffer named BNAME.
  84. ;; We pass a filename to create-file-buffer because it is what
  85. ;; the function expects, and also what uniquify needs (if active)
  86. (with-current-buffer (create-file-buffer (expand-file-name bname dir))
  87. (setq default-directory dir)
  88. (vc-setup-buffer (current-buffer))
  89. ;; Reset the vc-parent-buffer-name so that it does not appear
  90. ;; in the mode-line.
  91. (setq vc-parent-buffer-name nil)
  92. (current-buffer)))))
  93. (defvar vc-dir-menu-map
  94. (let ((map (make-sparse-keymap "VC-Dir")))
  95. (define-key map [quit]
  96. '(menu-item "Quit" quit-window
  97. :help "Quit"))
  98. (define-key map [kill]
  99. '(menu-item "Kill Update Command" vc-dir-kill-dir-status-process
  100. :enable (vc-dir-busy)
  101. :help "Kill the command that updates the directory buffer"))
  102. (define-key map [refresh]
  103. '(menu-item "Refresh" revert-buffer
  104. :enable (not (vc-dir-busy))
  105. :help "Refresh the contents of the directory buffer"))
  106. (define-key map [remup]
  107. '(menu-item "Hide Up-to-date" vc-dir-hide-up-to-date
  108. :help "Hide up-to-date items from display"))
  109. ;; Movement.
  110. (define-key map [sepmv] '("--"))
  111. (define-key map [next-line]
  112. '(menu-item "Next Line" vc-dir-next-line
  113. :help "Go to the next line" :keys "n"))
  114. (define-key map [previous-line]
  115. '(menu-item "Previous Line" vc-dir-previous-line
  116. :help "Go to the previous line"))
  117. ;; Marking.
  118. (define-key map [sepmrk] '("--"))
  119. (define-key map [unmark-all]
  120. '(menu-item "Unmark All" vc-dir-unmark-all-files
  121. :help "Unmark all files that are in the same state as the current file\
  122. \nWith prefix argument unmark all files"))
  123. (define-key map [unmark-previous]
  124. '(menu-item "Unmark Previous " vc-dir-unmark-file-up
  125. :help "Move to the previous line and unmark the file"))
  126. (define-key map [mark-all]
  127. '(menu-item "Mark All" vc-dir-mark-all-files
  128. :help "Mark all files that are in the same state as the current file\
  129. \nWith prefix argument mark all files"))
  130. (define-key map [unmark]
  131. '(menu-item "Unmark" vc-dir-unmark
  132. :help "Unmark the current file or all files in the region"))
  133. (define-key map [mark]
  134. '(menu-item "Mark" vc-dir-mark
  135. :help "Mark the current file or all files in the region"))
  136. (define-key map [sepopn] '("--"))
  137. (define-key map [qr]
  138. '(menu-item "Query Replace in Files..." vc-dir-query-replace-regexp
  139. :help "Replace a string in the marked files"))
  140. (define-key map [se]
  141. '(menu-item "Search Files..." vc-dir-search
  142. :help "Search a regexp in the marked files"))
  143. (define-key map [ires]
  144. '(menu-item "Isearch Regexp Files..." vc-dir-isearch-regexp
  145. :help "Incremental search a regexp in the marked files"))
  146. (define-key map [ise]
  147. '(menu-item "Isearch Files..." vc-dir-isearch
  148. :help "Incremental search a string in the marked files"))
  149. (define-key map [display]
  150. '(menu-item "Display in Other Window" vc-dir-display-file
  151. :help "Display the file on the current line, in another window"))
  152. (define-key map [open-other]
  153. '(menu-item "Open in Other Window" vc-dir-find-file-other-window
  154. :help "Find the file on the current line, in another window"))
  155. (define-key map [open]
  156. '(menu-item "Open File" vc-dir-find-file
  157. :help "Find the file on the current line"))
  158. (define-key map [sepvcdet] '("--"))
  159. ;; FIXME: This needs a key binding. And maybe a better name
  160. ;; ("Insert" like PCL-CVS uses does not sound that great either)...
  161. (define-key map [ins]
  162. '(menu-item "Show File" vc-dir-show-fileentry
  163. :help "Show a file in the VC status listing even though it might be up to date"))
  164. (define-key map [annotate]
  165. '(menu-item "Annotate" vc-annotate
  166. :help "Display the edit history of the current file using colors"))
  167. (define-key map [diff]
  168. '(menu-item "Compare with Base Version" vc-diff
  169. :help "Compare file set with the base version"))
  170. (define-key map [logo]
  171. '(menu-item "Show Outgoing Log" vc-log-outgoing
  172. :help "Show a log of changes that will be sent with a push operation"))
  173. (define-key map [logi]
  174. '(menu-item "Show Incoming Log" vc-log-incoming
  175. :help "Show a log of changes that will be received with a pull operation"))
  176. (define-key map [log]
  177. '(menu-item "Show History" vc-print-log
  178. :help "List the change log of the current file set in a window"))
  179. (define-key map [rlog]
  180. '(menu-item "Show Top of the Tree History " vc-print-root-log
  181. :help "List the change log for the current tree in a window"))
  182. ;; VC commands.
  183. (define-key map [sepvccmd] '("--"))
  184. (define-key map [push]
  185. '(menu-item "Push Changes" vc-push
  186. :enable (vc-find-backend-function vc-dir-backend 'push)
  187. :help "Push the current branch's changes"))
  188. (define-key map [update]
  189. '(menu-item "Update to Latest Version" vc-update
  190. :help "Update the current fileset's files to their tip revisions"))
  191. (define-key map [revert]
  192. '(menu-item "Revert to Base Version" vc-revert
  193. :help "Revert working copies of the selected fileset to their repository contents."))
  194. (define-key map [next-action]
  195. ;; FIXME: This really really really needs a better name!
  196. ;; And a key binding too.
  197. '(menu-item "Check In/Out" vc-next-action
  198. :help "Do the next logical version control operation on the current fileset"))
  199. (define-key map [register]
  200. '(menu-item "Register" vc-register
  201. :help "Register file set into the version control system"))
  202. (define-key map [ignore]
  203. '(menu-item "Ignore Current File" vc-dir-ignore
  204. :help "Ignore the current file under current version control system"))
  205. map)
  206. "Menu for VC dir.")
  207. ;; VC backends can use this to add mode-specific menu items to
  208. ;; vc-dir-menu-map.
  209. (defun vc-dir-menu-map-filter (orig-binding)
  210. (when (and (symbolp orig-binding) (fboundp orig-binding))
  211. (setq orig-binding (indirect-function orig-binding)))
  212. (let ((ext-binding
  213. (when (derived-mode-p 'vc-dir-mode)
  214. (vc-call-backend vc-dir-backend 'extra-status-menu))))
  215. (if (null ext-binding)
  216. orig-binding
  217. (append orig-binding
  218. '("----")
  219. ext-binding))))
  220. (defvar vc-dir-mode-map
  221. (let ((map (make-sparse-keymap)))
  222. ;; VC commands
  223. (define-key map "v" 'vc-next-action) ;; C-x v v
  224. (define-key map "=" 'vc-diff) ;; C-x v =
  225. (define-key map "D" 'vc-root-diff) ;; C-x v D
  226. (define-key map "i" 'vc-register) ;; C-x v i
  227. (define-key map "+" 'vc-update) ;; C-x v +
  228. ;; I'd prefer some kind of symmetry with vc-update:
  229. (define-key map "P" 'vc-push) ;; C-x v P
  230. (define-key map "l" 'vc-print-log) ;; C-x v l
  231. (define-key map "L" 'vc-print-root-log) ;; C-x v L
  232. (define-key map "I" 'vc-log-incoming) ;; C-x v I
  233. ;; More confusing than helpful, probably
  234. ;;(define-key map "R" 'vc-revert) ;; u is taken by vc-dir-unmark.
  235. ;;(define-key map "A" 'vc-annotate) ;; g is taken by revert-buffer
  236. ;; bound by `special-mode'.
  237. ;; Marking.
  238. (define-key map "m" 'vc-dir-mark)
  239. (define-key map "M" 'vc-dir-mark-all-files)
  240. (define-key map "u" 'vc-dir-unmark)
  241. (define-key map "U" 'vc-dir-unmark-all-files)
  242. (define-key map "\C-?" 'vc-dir-unmark-file-up)
  243. (define-key map "\M-\C-?" 'vc-dir-unmark-all-files)
  244. ;; Movement.
  245. (define-key map "n" 'vc-dir-next-line)
  246. (define-key map " " 'vc-dir-next-line)
  247. (define-key map "\t" 'vc-dir-next-directory)
  248. (define-key map "p" 'vc-dir-previous-line)
  249. (define-key map [?\S-\ ] 'vc-dir-previous-line)
  250. (define-key map [backtab] 'vc-dir-previous-directory)
  251. ;;; Rebind paragraph-movement commands.
  252. (define-key map "\M-}" 'vc-dir-next-directory)
  253. (define-key map "\M-{" 'vc-dir-previous-directory)
  254. (define-key map [C-down] 'vc-dir-next-directory)
  255. (define-key map [C-up] 'vc-dir-previous-directory)
  256. ;; The remainder.
  257. (define-key map "f" 'vc-dir-find-file)
  258. (define-key map "e" 'vc-dir-find-file) ; dired-mode compatibility
  259. (define-key map "\C-m" 'vc-dir-find-file)
  260. (define-key map "o" 'vc-dir-find-file-other-window)
  261. (define-key map "\C-o" 'vc-dir-display-file)
  262. (define-key map "\C-c\C-c" 'vc-dir-kill-dir-status-process)
  263. (define-key map [down-mouse-3] 'vc-dir-menu)
  264. (define-key map [mouse-2] 'vc-dir-toggle-mark)
  265. (define-key map [follow-link] 'mouse-face)
  266. (define-key map "x" 'vc-dir-hide-up-to-date)
  267. (define-key map [?\C-k] 'vc-dir-kill-line)
  268. (define-key map "S" 'vc-dir-search) ;; FIXME: Maybe use A like dired?
  269. (define-key map "Q" 'vc-dir-query-replace-regexp)
  270. (define-key map (kbd "M-s a C-s") 'vc-dir-isearch)
  271. (define-key map (kbd "M-s a M-C-s") 'vc-dir-isearch-regexp)
  272. (define-key map "G" 'vc-dir-ignore)
  273. ;; Hook up the menu.
  274. (define-key map [menu-bar vc-dir-mode]
  275. `(menu-item
  276. ;; VC backends can use this to add mode-specific menu items to
  277. ;; vc-dir-menu-map.
  278. "VC-Dir" ,vc-dir-menu-map :filter vc-dir-menu-map-filter))
  279. map)
  280. "Keymap for directory buffer.")
  281. (defmacro vc-dir-at-event (event &rest body)
  282. "Evaluate BODY with point located at event-start of EVENT.
  283. If BODY uses EVENT, it should be a variable,
  284. otherwise it will be evaluated twice."
  285. (let ((posn (make-symbol "vc-dir-at-event-posn")))
  286. `(save-excursion
  287. (unless (equal ,event '(tool-bar))
  288. (let ((,posn (event-start ,event)))
  289. (set-buffer (window-buffer (posn-window ,posn)))
  290. (goto-char (posn-point ,posn))))
  291. ,@body)))
  292. (defun vc-dir-menu (e)
  293. "Popup the VC dir menu."
  294. (interactive "e")
  295. (vc-dir-at-event e (popup-menu vc-dir-menu-map e)))
  296. (defvar vc-dir-tool-bar-map
  297. (let ((map (make-sparse-keymap)))
  298. (tool-bar-local-item-from-menu 'find-file "new" map nil
  299. :label "New File" :vert-only t)
  300. (tool-bar-local-item-from-menu 'menu-find-file-existing "open" map nil
  301. :label "Open" :vert-only t)
  302. (tool-bar-local-item-from-menu 'dired "diropen" map nil
  303. :vert-only t)
  304. (tool-bar-local-item-from-menu 'quit-window "close" map vc-dir-mode-map
  305. :vert-only t)
  306. (tool-bar-local-item-from-menu 'vc-next-action "saveas" map
  307. vc-dir-mode-map :label "Commit")
  308. (tool-bar-local-item-from-menu 'vc-print-log "info"
  309. map vc-dir-mode-map
  310. :label "Log")
  311. (define-key-after map [separator-1] menu-bar-separator)
  312. (tool-bar-local-item-from-menu 'vc-dir-kill-dir-status-process "cancel"
  313. map vc-dir-mode-map
  314. :label "Stop" :vert-only t)
  315. (tool-bar-local-item-from-menu 'revert-buffer "refresh"
  316. map vc-dir-mode-map :vert-only t)
  317. (define-key-after map [separator-2] menu-bar-separator)
  318. (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [cut])
  319. "cut" map nil :vert-only t)
  320. (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [copy])
  321. "copy" map nil :vert-only t)
  322. (tool-bar-local-item-from-menu (lookup-key menu-bar-edit-menu [paste])
  323. "paste" map nil :vert-only t)
  324. (define-key-after map [separator-3] menu-bar-separator)
  325. (tool-bar-local-item-from-menu 'isearch-forward
  326. "search" map nil
  327. :label "Search" :vert-only t)
  328. map))
  329. (defun vc-dir-node-directory (node)
  330. ;; Compute the directory for NODE.
  331. ;; If it's a directory node, get it from the node.
  332. (let ((data (ewoc-data node)))
  333. (or (vc-dir-fileinfo->directory data)
  334. ;; Otherwise compute it from the file name.
  335. (file-name-directory
  336. (directory-file-name
  337. (expand-file-name
  338. (vc-dir-fileinfo->name data)))))))
  339. (defun vc-dir-update (entries buffer &optional noinsert)
  340. "Update BUFFER's ewoc from the list of ENTRIES.
  341. If NOINSERT, ignore elements on ENTRIES which are not in the ewoc."
  342. ;; Add ENTRIES to the vc-dir buffer BUFFER.
  343. (with-current-buffer buffer
  344. ;; Insert the entries sorted by name into the ewoc.
  345. ;; We assume the ewoc is sorted too, which should be the
  346. ;; case if we always add entries with vc-dir-update.
  347. (setq entries
  348. ;; Sort: first files and then subdirectories.
  349. ;; XXX: this is VERY inefficient, it computes the directory
  350. ;; names too many times
  351. (sort entries
  352. (lambda (entry1 entry2)
  353. (let ((dir1 (file-name-directory
  354. (directory-file-name (expand-file-name (car entry1)))))
  355. (dir2 (file-name-directory
  356. (directory-file-name (expand-file-name (car entry2))))))
  357. (cond
  358. ((string< dir1 dir2) t)
  359. ((not (string= dir1 dir2)) nil)
  360. ((string< (car entry1) (car entry2))))))))
  361. ;; Insert directory entries in the right places.
  362. (let ((entry (car entries))
  363. (node (ewoc-nth vc-ewoc 0))
  364. (to-remove nil)
  365. (dotname (file-relative-name default-directory)))
  366. ;; Insert . if it is not present.
  367. (unless node
  368. (ewoc-enter-last
  369. vc-ewoc (vc-dir-create-fileinfo
  370. dotname nil nil nil default-directory))
  371. (setq node (ewoc-nth vc-ewoc 0)))
  372. (while (and entry node)
  373. (let* ((entryfile (car entry))
  374. (entrydir (file-name-directory (directory-file-name
  375. (expand-file-name entryfile))))
  376. (nodedir (vc-dir-node-directory node)))
  377. (cond
  378. ;; First try to find the directory.
  379. ((string-lessp nodedir entrydir)
  380. (setq node (ewoc-next vc-ewoc node)))
  381. ((string-equal nodedir entrydir)
  382. ;; Found the directory, find the place for the file name.
  383. (let ((nodefile (vc-dir-fileinfo->name (ewoc-data node))))
  384. (cond
  385. ((string= nodefile dotname)
  386. (setq node (ewoc-next vc-ewoc node)))
  387. ((string-lessp nodefile entryfile)
  388. (setq node (ewoc-next vc-ewoc node)))
  389. ((string-equal nodefile entryfile)
  390. (if (nth 1 entry)
  391. (progn
  392. (setf (vc-dir-fileinfo->state (ewoc-data node)) (nth 1 entry))
  393. (setf (vc-dir-fileinfo->extra (ewoc-data node)) (nth 2 entry))
  394. (setf (vc-dir-fileinfo->needs-update (ewoc-data node)) nil)
  395. (ewoc-invalidate vc-ewoc node))
  396. ;; If the state is nil, the file does not exist
  397. ;; anymore, so remember the entry so we can remove
  398. ;; it after we are done inserting all ENTRIES.
  399. (push node to-remove))
  400. (setq entries (cdr entries))
  401. (setq entry (car entries))
  402. (setq node (ewoc-next vc-ewoc node)))
  403. (t
  404. (unless noinsert
  405. (ewoc-enter-before vc-ewoc node
  406. (apply 'vc-dir-create-fileinfo entry)))
  407. (setq entries (cdr entries))
  408. (setq entry (car entries))))))
  409. (t
  410. (unless noinsert
  411. ;; We might need to insert a directory node if the
  412. ;; previous node was in a different directory.
  413. (let* ((rd (file-relative-name entrydir))
  414. (prev-node (ewoc-prev vc-ewoc node))
  415. (prev-dir (if prev-node
  416. (vc-dir-node-directory prev-node))))
  417. (unless (string-equal entrydir prev-dir)
  418. (ewoc-enter-before
  419. vc-ewoc node (vc-dir-create-fileinfo rd nil nil nil entrydir))))
  420. ;; Now insert the node itself.
  421. (ewoc-enter-before vc-ewoc node
  422. (apply 'vc-dir-create-fileinfo entry)))
  423. (setq entries (cdr entries) entry (car entries))))))
  424. ;; We're past the last node, all remaining entries go to the end.
  425. (unless (or node noinsert)
  426. (let ((lastdir (vc-dir-node-directory (ewoc-nth vc-ewoc -1))))
  427. (dolist (entry entries)
  428. (let ((entrydir (file-name-directory
  429. (directory-file-name (expand-file-name (car entry))))))
  430. ;; Insert a directory node if needed.
  431. (unless (string-equal lastdir entrydir)
  432. (setq lastdir entrydir)
  433. (let ((rd (file-relative-name entrydir)))
  434. (ewoc-enter-last
  435. vc-ewoc (vc-dir-create-fileinfo rd nil nil nil entrydir))))
  436. ;; Now insert the node itself.
  437. (ewoc-enter-last vc-ewoc
  438. (apply 'vc-dir-create-fileinfo entry))))))
  439. (when to-remove
  440. (let ((inhibit-read-only t))
  441. (apply 'ewoc-delete vc-ewoc (nreverse to-remove)))))))
  442. (defun vc-dir-busy ()
  443. (and (buffer-live-p vc-dir-process-buffer)
  444. (get-buffer-process vc-dir-process-buffer)))
  445. (defun vc-dir-kill-dir-status-process ()
  446. "Kill the temporary buffer and associated process."
  447. (interactive)
  448. (when (buffer-live-p vc-dir-process-buffer)
  449. (let ((proc (get-buffer-process vc-dir-process-buffer)))
  450. (when proc (delete-process proc))
  451. (setq vc-dir-process-buffer nil)
  452. (setq mode-line-process nil))))
  453. (defun vc-dir-kill-query ()
  454. ;; Make sure that when the status buffer is killed the update
  455. ;; process running in background is also killed.
  456. (if (vc-dir-busy)
  457. (when (y-or-n-p "Status update process running, really kill status buffer? ")
  458. (vc-dir-kill-dir-status-process)
  459. t)
  460. t))
  461. (defun vc-dir-next-line (arg)
  462. "Go to the next line.
  463. If a prefix argument is given, move by that many lines."
  464. (interactive "p")
  465. (with-no-warnings
  466. (ewoc-goto-next vc-ewoc arg)
  467. (vc-dir-move-to-goal-column)))
  468. (defun vc-dir-previous-line (arg)
  469. "Go to the previous line.
  470. If a prefix argument is given, move by that many lines."
  471. (interactive "p")
  472. (ewoc-goto-prev vc-ewoc arg)
  473. (vc-dir-move-to-goal-column))
  474. (defun vc-dir-next-directory ()
  475. "Go to the next directory."
  476. (interactive)
  477. (let ((orig (point)))
  478. (if
  479. (catch 'foundit
  480. (while t
  481. (let* ((next (ewoc-next vc-ewoc (ewoc-locate vc-ewoc))))
  482. (cond ((not next)
  483. (throw 'foundit t))
  484. (t
  485. (progn
  486. (ewoc-goto-node vc-ewoc next)
  487. (vc-dir-move-to-goal-column)
  488. (if (vc-dir-fileinfo->directory (ewoc-data next))
  489. (throw 'foundit nil))))))))
  490. (goto-char orig))))
  491. (defun vc-dir-previous-directory ()
  492. "Go to the previous directory."
  493. (interactive)
  494. (let ((orig (point)))
  495. (if
  496. (catch 'foundit
  497. (while t
  498. (let* ((prev (ewoc-prev vc-ewoc (ewoc-locate vc-ewoc))))
  499. (cond ((not prev)
  500. (throw 'foundit t))
  501. (t
  502. (progn
  503. (ewoc-goto-node vc-ewoc prev)
  504. (vc-dir-move-to-goal-column)
  505. (if (vc-dir-fileinfo->directory (ewoc-data prev))
  506. (throw 'foundit nil))))))))
  507. (goto-char orig))))
  508. (defun vc-dir-mark-unmark (mark-unmark-function)
  509. (if (use-region-p)
  510. (let (;; (firstl (line-number-at-pos (region-beginning)))
  511. (lastl (line-number-at-pos (region-end))))
  512. (save-excursion
  513. (goto-char (region-beginning))
  514. (while (<= (line-number-at-pos) lastl)
  515. (condition-case nil
  516. (funcall mark-unmark-function)
  517. ;; `vc-dir-mark-file' signals an error if we try marking
  518. ;; a directory containing marked files in its tree, or a
  519. ;; file in a marked directory tree. Just continue.
  520. (error (vc-dir-next-line 1))))))
  521. (funcall mark-unmark-function)))
  522. (defun vc-dir-parent-marked-p (arg)
  523. ;; Non-nil iff a parent directory of arg is marked.
  524. ;; Return value, if non-nil is the `ewoc-data' for the marked parent.
  525. (let* ((argdir (vc-dir-node-directory arg))
  526. ;; (arglen (length argdir))
  527. (crt arg)
  528. (found nil))
  529. ;; Go through the predecessors, checking if any directory that is
  530. ;; a parent is marked.
  531. (while (and (null found)
  532. (setq crt (ewoc-prev vc-ewoc crt)))
  533. (let ((data (ewoc-data crt))
  534. (dir (vc-dir-node-directory crt)))
  535. (and (vc-dir-fileinfo->directory data)
  536. (string-prefix-p dir argdir)
  537. (vc-dir-fileinfo->marked data)
  538. (setq found data))))
  539. found))
  540. (defun vc-dir-children-marked-p (arg)
  541. ;; Non-nil iff a child of ARG is marked.
  542. ;; Return value, if non-nil, is the `ewoc-data' for the marked child.
  543. (let* ((argdir-re (concat "\\`" (regexp-quote (vc-dir-node-directory arg))))
  544. (is-child t)
  545. (crt arg)
  546. (found nil))
  547. (while (and is-child
  548. (null found)
  549. (setq crt (ewoc-next vc-ewoc crt)))
  550. (let ((data (ewoc-data crt))
  551. (dir (vc-dir-node-directory crt)))
  552. (if (string-match argdir-re dir)
  553. (if (vc-dir-fileinfo->marked data)
  554. (setq found data))
  555. ;; We are done, we got to an entry that is not a child of `arg'.
  556. (setq is-child nil))))
  557. found))
  558. (defun vc-dir-mark-file (&optional arg)
  559. ;; Mark ARG or the current file and move to the next line.
  560. (let* ((crt (or arg (ewoc-locate vc-ewoc)))
  561. (file (ewoc-data crt))
  562. (isdir (vc-dir-fileinfo->directory file))
  563. ;; Forbid marking a directory containing marked files in its
  564. ;; tree, or a file in a marked directory tree.
  565. (conflict (if isdir
  566. (vc-dir-children-marked-p crt)
  567. (vc-dir-parent-marked-p crt))))
  568. (when conflict
  569. (error (if isdir
  570. "File `%s' in this directory is already marked"
  571. "Parent directory `%s' is already marked")
  572. (vc-dir-fileinfo->name conflict)))
  573. (setf (vc-dir-fileinfo->marked file) t)
  574. (ewoc-invalidate vc-ewoc crt)
  575. (unless (or arg (mouse-event-p last-command-event))
  576. (vc-dir-next-line 1))))
  577. (defun vc-dir-mark ()
  578. "Mark the current file or all files in the region.
  579. If the region is active, mark all the files in the region.
  580. Otherwise mark the file on the current line and move to the next
  581. line."
  582. (interactive)
  583. (vc-dir-mark-unmark 'vc-dir-mark-file))
  584. (defun vc-dir-mark-all-files (arg)
  585. "Mark all files with the same state as the current one.
  586. With a prefix argument mark all files.
  587. If the current entry is a directory, mark all child files.
  588. The commands operate on files that are on the same state.
  589. This command is intended to make it easy to select all files that
  590. share the same state."
  591. (interactive "P")
  592. (if arg
  593. ;; Mark all files.
  594. (progn
  595. ;; First check that no directory is marked, we can't mark
  596. ;; files in that case.
  597. (ewoc-map
  598. (lambda (filearg)
  599. (when (and (vc-dir-fileinfo->directory filearg)
  600. (vc-dir-fileinfo->marked filearg))
  601. (error "Cannot mark all files, directory `%s' marked"
  602. (vc-dir-fileinfo->name filearg))))
  603. vc-ewoc)
  604. (ewoc-map
  605. (lambda (filearg)
  606. (unless (vc-dir-fileinfo->marked filearg)
  607. (setf (vc-dir-fileinfo->marked filearg) t)
  608. t))
  609. vc-ewoc))
  610. (let* ((crt (ewoc-locate vc-ewoc))
  611. (data (ewoc-data crt)))
  612. (if (vc-dir-fileinfo->directory data)
  613. ;; It's a directory, mark child files.
  614. (let (crt-data)
  615. (while (and (setq crt (ewoc-next vc-ewoc crt))
  616. (setq crt-data (ewoc-data crt))
  617. (not (vc-dir-fileinfo->directory crt-data)))
  618. (setf (vc-dir-fileinfo->marked crt-data) t)
  619. (ewoc-invalidate vc-ewoc crt)))
  620. ;; It's a file
  621. (let ((state (vc-dir-fileinfo->state data)))
  622. (setq crt (ewoc-nth vc-ewoc 0))
  623. (while crt
  624. (let ((crt-data (ewoc-data crt)))
  625. (when (and (not (vc-dir-fileinfo->marked crt-data))
  626. (eq (vc-dir-fileinfo->state crt-data) state)
  627. (not (vc-dir-fileinfo->directory crt-data)))
  628. (vc-dir-mark-file crt)))
  629. (setq crt (ewoc-next vc-ewoc crt))))))))
  630. (defun vc-dir-unmark-file ()
  631. ;; Unmark the current file and move to the next line.
  632. (let* ((crt (ewoc-locate vc-ewoc))
  633. (file (ewoc-data crt)))
  634. (setf (vc-dir-fileinfo->marked file) nil)
  635. (ewoc-invalidate vc-ewoc crt)
  636. (unless (mouse-event-p last-command-event)
  637. (vc-dir-next-line 1))))
  638. (defun vc-dir-unmark ()
  639. "Unmark the current file or all files in the region.
  640. If the region is active, unmark all the files in the region.
  641. Otherwise mark the file on the current line and move to the next
  642. line."
  643. (interactive)
  644. (vc-dir-mark-unmark 'vc-dir-unmark-file))
  645. (defun vc-dir-unmark-file-up ()
  646. "Move to the previous line and unmark the file."
  647. (interactive)
  648. ;; If we're on the first line, we won't move up, but we will still
  649. ;; remove the mark. This seems a bit odd but it is what buffer-menu
  650. ;; does.
  651. (let* ((prev (ewoc-goto-prev vc-ewoc 1))
  652. (file (ewoc-data prev)))
  653. (setf (vc-dir-fileinfo->marked file) nil)
  654. (ewoc-invalidate vc-ewoc prev)
  655. (vc-dir-move-to-goal-column)))
  656. (defun vc-dir-unmark-all-files (arg)
  657. "Unmark all files with the same state as the current one.
  658. With a prefix argument unmark all files.
  659. If the current entry is a directory, unmark all the child files.
  660. The commands operate on files that are on the same state.
  661. This command is intended to make it easy to deselect all files
  662. that share the same state."
  663. (interactive "P")
  664. (if arg
  665. (ewoc-map
  666. (lambda (filearg)
  667. (when (vc-dir-fileinfo->marked filearg)
  668. (setf (vc-dir-fileinfo->marked filearg) nil)
  669. t))
  670. vc-ewoc)
  671. (let* ((crt (ewoc-locate vc-ewoc))
  672. (data (ewoc-data crt)))
  673. (if (vc-dir-fileinfo->directory data)
  674. ;; It's a directory, unmark child files.
  675. (while (setq crt (ewoc-next vc-ewoc crt))
  676. (let ((crt-data (ewoc-data crt)))
  677. (unless (vc-dir-fileinfo->directory crt-data)
  678. (setf (vc-dir-fileinfo->marked crt-data) nil)
  679. (ewoc-invalidate vc-ewoc crt))))
  680. ;; It's a file
  681. (let ((crt-state (vc-dir-fileinfo->state (ewoc-data crt))))
  682. (ewoc-map
  683. (lambda (filearg)
  684. (when (and (vc-dir-fileinfo->marked filearg)
  685. (eq (vc-dir-fileinfo->state filearg) crt-state))
  686. (setf (vc-dir-fileinfo->marked filearg) nil)
  687. t))
  688. vc-ewoc))))))
  689. (defun vc-dir-toggle-mark-file ()
  690. (let* ((crt (ewoc-locate vc-ewoc))
  691. (file (ewoc-data crt)))
  692. (if (vc-dir-fileinfo->marked file)
  693. (vc-dir-unmark-file)
  694. (vc-dir-mark-file))))
  695. (defun vc-dir-toggle-mark (e)
  696. (interactive "e")
  697. (vc-dir-at-event e (vc-dir-mark-unmark 'vc-dir-toggle-mark-file)))
  698. (defun vc-dir-delete-file ()
  699. "Delete the marked files, or the current file if no marks."
  700. (interactive)
  701. (mapc 'vc-delete-file (or (vc-dir-marked-files)
  702. (list (vc-dir-current-file)))))
  703. (defun vc-dir-find-file ()
  704. "Find the file on the current line."
  705. (interactive)
  706. (find-file (vc-dir-current-file)))
  707. (defun vc-dir-find-file-other-window (&optional event)
  708. "Find the file on the current line, in another window."
  709. (interactive (list last-nonmenu-event))
  710. (if event (posn-set-point (event-end event)))
  711. (find-file-other-window (vc-dir-current-file)))
  712. (defun vc-dir-display-file (&optional event)
  713. "Display the file on the current line, in another window."
  714. (interactive (list last-nonmenu-event))
  715. (if event (posn-set-point (event-end event)))
  716. (display-buffer (find-file-noselect (vc-dir-current-file))
  717. t))
  718. (defun vc-dir-isearch ()
  719. "Search for a string through all marked buffers using Isearch."
  720. (interactive)
  721. (multi-isearch-files
  722. (mapcar 'car (vc-dir-marked-only-files-and-states))))
  723. (defun vc-dir-isearch-regexp ()
  724. "Search for a regexp through all marked buffers using Isearch."
  725. (interactive)
  726. (multi-isearch-files-regexp
  727. (mapcar 'car (vc-dir-marked-only-files-and-states))))
  728. (defun vc-dir-search (regexp)
  729. "Search through all marked files for a match for REGEXP.
  730. For marked directories, use the files displayed from those directories.
  731. Stops when a match is found.
  732. To continue searching for next match, use command \\[tags-loop-continue]."
  733. (interactive "sSearch marked files (regexp): ")
  734. (tags-search regexp '(mapcar 'car (vc-dir-marked-only-files-and-states))))
  735. (defun vc-dir-query-replace-regexp (from to &optional delimited)
  736. "Do `query-replace-regexp' of FROM with TO, on all marked files.
  737. If a directory is marked, then use the files displayed for that directory.
  738. Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
  739. If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
  740. with the command \\[tags-loop-continue]."
  741. ;; FIXME: this is almost a copy of `dired-do-query-replace-regexp'. This
  742. ;; should probably be made generic and used in both places instead of
  743. ;; duplicating it here.
  744. (interactive
  745. (let ((common
  746. (query-replace-read-args
  747. "Query replace regexp in marked files" t t)))
  748. (list (nth 0 common) (nth 1 common) (nth 2 common))))
  749. (dolist (file (mapcar 'car (vc-dir-marked-only-files-and-states)))
  750. (let ((buffer (get-file-buffer file)))
  751. (if (and buffer (with-current-buffer buffer
  752. buffer-read-only))
  753. (error "File `%s' is visited read-only" file))))
  754. (tags-query-replace from to delimited
  755. '(mapcar 'car (vc-dir-marked-only-files-and-states))))
  756. (defun vc-dir-ignore ()
  757. "Ignore the current file."
  758. (interactive)
  759. (vc-ignore (vc-dir-current-file)))
  760. (defun vc-dir-current-file ()
  761. (let ((node (ewoc-locate vc-ewoc)))
  762. (unless node
  763. (error "No file available"))
  764. (expand-file-name (vc-dir-fileinfo->name (ewoc-data node)))))
  765. (defun vc-dir-marked-files ()
  766. "Return the list of marked files."
  767. (mapcar
  768. (lambda (elem) (expand-file-name (vc-dir-fileinfo->name elem)))
  769. (ewoc-collect vc-ewoc 'vc-dir-fileinfo->marked)))
  770. (defun vc-dir-marked-only-files-and-states ()
  771. "Return the list of conses (FILE . STATE) for the marked files.
  772. For marked directories return the corresponding conses for the
  773. child files."
  774. (let ((crt (ewoc-nth vc-ewoc 0))
  775. result)
  776. (while crt
  777. (let ((crt-data (ewoc-data crt)))
  778. (if (vc-dir-fileinfo->marked crt-data)
  779. ;; FIXME: use vc-dir-child-files-and-states here instead of duplicating it.
  780. (if (vc-dir-fileinfo->directory crt-data)
  781. (let* ((dir (vc-dir-fileinfo->directory crt-data))
  782. ;; (dirlen (length dir))
  783. data)
  784. (while
  785. (and (setq crt (ewoc-next vc-ewoc crt))
  786. (string-prefix-p dir
  787. (progn
  788. (setq data (ewoc-data crt))
  789. (vc-dir-node-directory crt))))
  790. (unless (vc-dir-fileinfo->directory data)
  791. (push
  792. (cons (expand-file-name (vc-dir-fileinfo->name data))
  793. (vc-dir-fileinfo->state data))
  794. result))))
  795. (push (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
  796. (vc-dir-fileinfo->state crt-data))
  797. result)
  798. (setq crt (ewoc-next vc-ewoc crt)))
  799. (setq crt (ewoc-next vc-ewoc crt)))))
  800. (nreverse result)))
  801. (defun vc-dir-child-files-and-states ()
  802. "Return the list of conses (FILE . STATE) for child files of the current entry if it's a directory.
  803. If it is a file, return the corresponding cons for the file itself."
  804. (let* ((crt (ewoc-locate vc-ewoc))
  805. (crt-data (ewoc-data crt))
  806. result)
  807. (if (vc-dir-fileinfo->directory crt-data)
  808. (let* ((dir (vc-dir-fileinfo->directory crt-data))
  809. ;; (dirlen (length dir))
  810. data)
  811. (while
  812. (and (setq crt (ewoc-next vc-ewoc crt))
  813. (string-prefix-p dir (progn
  814. (setq data (ewoc-data crt))
  815. (vc-dir-node-directory crt))))
  816. (unless (vc-dir-fileinfo->directory data)
  817. (push
  818. (cons (expand-file-name (vc-dir-fileinfo->name data))
  819. (vc-dir-fileinfo->state data))
  820. result))))
  821. (push
  822. (cons (expand-file-name (vc-dir-fileinfo->name crt-data))
  823. (vc-dir-fileinfo->state crt-data)) result))
  824. (nreverse result)))
  825. (defun vc-dir-recompute-file-state (fname def-dir)
  826. (let* ((file-short (file-relative-name fname def-dir))
  827. (_remove-me-when-CVS-works
  828. (when (eq vc-dir-backend 'CVS)
  829. ;; FIXME: Warning: UGLY HACK. The CVS backend caches the state
  830. ;; info, this forces the backend to update it.
  831. (vc-call-backend vc-dir-backend 'registered fname)))
  832. (state (vc-call-backend vc-dir-backend 'state fname))
  833. (extra (vc-call-backend vc-dir-backend
  834. 'status-fileinfo-extra fname)))
  835. (list file-short state extra)))
  836. (defun vc-dir-find-child-files (dirname)
  837. ;; Give a DIRNAME string return the list of all child files shown in
  838. ;; the current *vc-dir* buffer.
  839. (let ((crt (ewoc-nth vc-ewoc 0))
  840. children)
  841. ;; Find DIR
  842. (while (and crt (not (string-prefix-p
  843. dirname (vc-dir-node-directory crt))))
  844. (setq crt (ewoc-next vc-ewoc crt)))
  845. (while (and crt (string-prefix-p
  846. dirname
  847. (vc-dir-node-directory crt)))
  848. (let ((data (ewoc-data crt)))
  849. (unless (vc-dir-fileinfo->directory data)
  850. (push (expand-file-name (vc-dir-fileinfo->name data)) children)))
  851. (setq crt (ewoc-next vc-ewoc crt)))
  852. children))
  853. (defun vc-dir-resync-directory-files (dirname)
  854. ;; Update the entries for all the child files of DIRNAME shown in
  855. ;; the current *vc-dir* buffer.
  856. (let ((files (vc-dir-find-child-files dirname))
  857. (ddir default-directory)
  858. fileentries)
  859. (when files
  860. (dolist (crt files)
  861. (push (vc-dir-recompute-file-state crt ddir)
  862. fileentries))
  863. (vc-dir-update fileentries (current-buffer)))))
  864. (defun vc-dir-resynch-file (&optional fname)
  865. "Update the entries for FNAME in any directory buffers that list it."
  866. (let ((file (expand-file-name (or fname buffer-file-name)))
  867. (drop '()))
  868. (save-current-buffer
  869. ;; look for a vc-dir buffer that might show this file.
  870. (dolist (status-buf vc-dir-buffers)
  871. (if (not (buffer-live-p status-buf))
  872. (push status-buf drop)
  873. (set-buffer status-buf)
  874. (if (not (derived-mode-p 'vc-dir-mode))
  875. (push status-buf drop)
  876. (let ((ddir default-directory))
  877. (when (string-prefix-p ddir file)
  878. (if (file-directory-p file)
  879. (progn
  880. (vc-dir-resync-directory-files file)
  881. (ewoc-set-hf vc-ewoc
  882. (vc-dir-headers vc-dir-backend default-directory) ""))
  883. (let* ((complete-state (vc-dir-recompute-file-state file ddir))
  884. (state (cadr complete-state)))
  885. (vc-dir-update
  886. (list complete-state)
  887. status-buf (or (not state)
  888. (eq state 'up-to-date)))))))))))
  889. ;; Remove out-of-date entries from vc-dir-buffers.
  890. (dolist (b drop) (setq vc-dir-buffers (delq b vc-dir-buffers)))))
  891. (defvar use-vc-backend) ;; dynamically bound
  892. (define-derived-mode vc-dir-mode special-mode "VC dir"
  893. "Major mode for VC directory buffers.
  894. Marking/Unmarking key bindings and actions:
  895. m - mark a file/directory
  896. - if the region is active, mark all the files in region.
  897. Restrictions: - a file cannot be marked if any parent directory is marked
  898. - a directory cannot be marked if any child file or
  899. directory is marked
  900. u - unmark a file/directory
  901. - if the region is active, unmark all the files in region.
  902. M - if the cursor is on a file: mark all the files with the same state as
  903. the current file
  904. - if the cursor is on a directory: mark all child files
  905. - with a prefix argument: mark all files
  906. U - if the cursor is on a file: unmark all the files with the same state
  907. as the current file
  908. - if the cursor is on a directory: unmark all child files
  909. - with a prefix argument: unmark all files
  910. mouse-2 - toggles the mark state
  911. VC commands
  912. VC commands in the `C-x v' prefix can be used.
  913. VC commands act on the marked entries. If nothing is marked, VC
  914. commands act on the current entry.
  915. Search & Replace
  916. S - searches the marked files
  917. Q - does a query replace on the marked files
  918. M-s a C-s - does an isearch on the marked files
  919. M-s a C-M-s - does a regexp isearch on the marked files
  920. If nothing is marked, these commands act on the current entry.
  921. When a directory is current or marked, the Search & Replace
  922. commands act on the child files of that directory that are displayed in
  923. the *vc-dir* buffer.
  924. \\{vc-dir-mode-map}"
  925. (set (make-local-variable 'vc-dir-backend) use-vc-backend)
  926. (set (make-local-variable 'desktop-save-buffer)
  927. 'vc-dir-desktop-buffer-misc-data)
  928. (setq buffer-read-only t)
  929. (when (boundp 'tool-bar-map)
  930. (set (make-local-variable 'tool-bar-map) vc-dir-tool-bar-map))
  931. (let ((buffer-read-only nil))
  932. (erase-buffer)
  933. (set (make-local-variable 'vc-dir-process-buffer) nil)
  934. (set (make-local-variable 'vc-ewoc) (ewoc-create #'vc-dir-printer))
  935. (set (make-local-variable 'revert-buffer-function)
  936. 'vc-dir-revert-buffer-function)
  937. (setq list-buffers-directory (expand-file-name "*vc-dir*" default-directory))
  938. (add-to-list 'vc-dir-buffers (current-buffer))
  939. ;; Make sure that if the directory buffer is killed, the update
  940. ;; process running in the background is also killed.
  941. (add-hook 'kill-buffer-query-functions 'vc-dir-kill-query nil t)
  942. (hack-dir-local-variables-non-file-buffer)
  943. (vc-dir-refresh)))
  944. (defun vc-dir-headers (backend dir)
  945. "Display the headers in the *VC dir* buffer.
  946. It calls the `dir-extra-headers' backend method to display backend
  947. specific headers."
  948. (concat
  949. ;; First layout the common headers.
  950. (propertize "VC backend : " 'face 'font-lock-type-face)
  951. (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
  952. (propertize "Working dir: " 'face 'font-lock-type-face)
  953. (propertize (format "%s\n" (abbreviate-file-name dir))
  954. 'face 'font-lock-variable-name-face)
  955. ;; Then the backend specific ones.
  956. (vc-call-backend backend 'dir-extra-headers dir)
  957. "\n"))
  958. (defun vc-dir-refresh-files (files)
  959. "Refresh some files in the *VC-dir* buffer."
  960. (let ((def-dir default-directory)
  961. (backend vc-dir-backend))
  962. (vc-set-mode-line-busy-indicator)
  963. ;; Call the `dir-status-files' backend function.
  964. ;; `dir-status-files' is supposed to be asynchronous.
  965. ;; It should compute the results, and then call the function
  966. ;; passed as an argument in order to update the vc-dir buffer
  967. ;; with the results.
  968. (unless (buffer-live-p vc-dir-process-buffer)
  969. (setq vc-dir-process-buffer
  970. (generate-new-buffer (format " *VC-%s* tmp status" backend))))
  971. (let ((buffer (current-buffer)))
  972. (with-current-buffer vc-dir-process-buffer
  973. (setq default-directory def-dir)
  974. (erase-buffer)
  975. (vc-call-backend
  976. backend 'dir-status-files def-dir files
  977. (lambda (entries &optional more-to-come)
  978. ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
  979. ;; If MORE-TO-COME is true, then more updates will come from
  980. ;; the asynchronous process.
  981. (with-current-buffer buffer
  982. (vc-dir-update entries buffer)
  983. (unless more-to-come
  984. (setq mode-line-process nil)
  985. ;; Remove the ones that haven't been updated at all.
  986. ;; Those not-updated are those whose state is nil because the
  987. ;; file/dir doesn't exist and isn't versioned.
  988. (ewoc-filter vc-ewoc
  989. (lambda (info)
  990. ;; The state for directory entries might
  991. ;; have been changed to 'up-to-date,
  992. ;; reset it, otherwise it will be removed when doing 'x'
  993. ;; next time.
  994. ;; FIXME: There should be a more elegant way to do this.
  995. (when (and (vc-dir-fileinfo->directory info)
  996. (eq (vc-dir-fileinfo->state info)
  997. 'up-to-date))
  998. (setf (vc-dir-fileinfo->state info) nil))
  999. (not (vc-dir-fileinfo->needs-update info))))))))))))
  1000. (defun vc-dir-revert-buffer-function (&optional _ignore-auto _noconfirm)
  1001. (vc-dir-refresh))
  1002. (defun vc-dir-refresh ()
  1003. "Refresh the contents of the *VC-dir* buffer.
  1004. Throw an error if another update process is in progress."
  1005. (interactive)
  1006. (if (vc-dir-busy)
  1007. (error "Another update process is in progress, cannot run two at a time")
  1008. (let ((def-dir default-directory)
  1009. (backend vc-dir-backend))
  1010. (vc-set-mode-line-busy-indicator)
  1011. ;; Call the `dir-status' backend function.
  1012. ;; `dir-status' is supposed to be asynchronous.
  1013. ;; It should compute the results, and then call the function
  1014. ;; passed as an argument in order to update the vc-dir buffer
  1015. ;; with the results.
  1016. ;; Create a buffer that can be used by `dir-status' and call
  1017. ;; `dir-status' with this buffer as the current buffer. Use
  1018. ;; `vc-dir-process-buffer' to remember this buffer, so that
  1019. ;; it can be used later to kill the update process in case it
  1020. ;; takes too long.
  1021. (unless (buffer-live-p vc-dir-process-buffer)
  1022. (setq vc-dir-process-buffer
  1023. (generate-new-buffer (format " *VC-%s* tmp status" backend))))
  1024. ;; set the needs-update flag on all non-directory entries
  1025. (ewoc-map (lambda (info)
  1026. (unless (vc-dir-fileinfo->directory info)
  1027. (setf (vc-dir-fileinfo->needs-update info) t) nil))
  1028. vc-ewoc)
  1029. ;; Bzr has serious locking problems, so setup the headers first (this is
  1030. ;; synchronous) rather than doing it while dir-status is running.
  1031. (ewoc-set-hf vc-ewoc (vc-dir-headers backend def-dir) "")
  1032. (let ((buffer (current-buffer)))
  1033. (with-current-buffer vc-dir-process-buffer
  1034. (setq default-directory def-dir)
  1035. (erase-buffer)
  1036. (vc-call-backend
  1037. backend 'dir-status-files def-dir nil
  1038. (lambda (entries &optional more-to-come)
  1039. ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
  1040. ;; If MORE-TO-COME is true, then more updates will come from
  1041. ;; the asynchronous process.
  1042. (with-current-buffer buffer
  1043. (vc-dir-update entries buffer)
  1044. (unless more-to-come
  1045. (let ((remaining
  1046. (ewoc-collect
  1047. vc-ewoc 'vc-dir-fileinfo->needs-update)))
  1048. (if remaining
  1049. (vc-dir-refresh-files
  1050. (mapcar 'vc-dir-fileinfo->name remaining))
  1051. (setq mode-line-process nil))))))))))))
  1052. (defun vc-dir-show-fileentry (file)
  1053. "Insert an entry for a specific file into the current *VC-dir* listing.
  1054. This is typically used if the file is up-to-date (or has been added
  1055. outside of VC) and one wants to do some operation on it."
  1056. (interactive "fShow file: ")
  1057. (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
  1058. (defun vc-dir-hide-state (&optional state)
  1059. "Hide items that are in STATE from display.
  1060. See `vc-state' for valid values of STATE.
  1061. If STATE is nil, hide both `up-to-date' and `ignored' items.
  1062. Interactively, if `current-prefix-arg' is non-nil, set STATE to
  1063. state of item at point, if any."
  1064. (interactive (list
  1065. (and current-prefix-arg
  1066. ;; Command is prefixed. Infer STATE from point.
  1067. (let ((node (ewoc-locate vc-ewoc)))
  1068. (and node (vc-dir-fileinfo->state (ewoc-data node)))))))
  1069. (if state
  1070. (message "Hiding items in state \"%s\"" state)
  1071. (message "Hiding up-to-date and ignored items"))
  1072. (let ((crt (ewoc-nth vc-ewoc -1))
  1073. (first (ewoc-nth vc-ewoc 0)))
  1074. ;; Go over from the last item to the first and remove the
  1075. ;; up-to-date files and directories with no child files.
  1076. (while (not (eq crt first))
  1077. (let* ((data (ewoc-data crt))
  1078. (dir (vc-dir-fileinfo->directory data))
  1079. (next (ewoc-next vc-ewoc crt))
  1080. (prev (ewoc-prev vc-ewoc crt))
  1081. ;; ewoc-delete does not work without this...
  1082. (inhibit-read-only t))
  1083. (when (or
  1084. ;; Remove directories with no child files.
  1085. (and dir
  1086. (or
  1087. ;; Nothing follows this directory.
  1088. (not next)
  1089. ;; Next item is a directory.
  1090. (vc-dir-fileinfo->directory (ewoc-data next))))
  1091. ;; Remove files in specified STATE. STATE can be a
  1092. ;; symbol, a user-name, or nil.
  1093. (if state
  1094. (equal (vc-dir-fileinfo->state data) state)
  1095. (memq (vc-dir-fileinfo->state data) '(up-to-date ignored))))
  1096. (ewoc-delete vc-ewoc crt))
  1097. (setq crt prev)))))
  1098. (defalias 'vc-dir-hide-up-to-date 'vc-dir-hide-state)
  1099. (defun vc-dir-kill-line ()
  1100. "Remove the current line from display."
  1101. (interactive)
  1102. (let ((crt (ewoc-locate vc-ewoc))
  1103. (inhibit-read-only t))
  1104. (ewoc-delete vc-ewoc crt)))
  1105. (defun vc-dir-printer (fileentry)
  1106. (vc-call-backend vc-dir-backend 'dir-printer fileentry))
  1107. (defun vc-dir-deduce-fileset (&optional state-model-only-files)
  1108. (let ((marked (vc-dir-marked-files))
  1109. files
  1110. only-files-list
  1111. state
  1112. model)
  1113. (if marked
  1114. (progn
  1115. (setq files marked)
  1116. (when state-model-only-files
  1117. (setq only-files-list (vc-dir-marked-only-files-and-states))))
  1118. (let ((crt (vc-dir-current-file)))
  1119. (setq files (list crt))
  1120. (when state-model-only-files
  1121. (setq only-files-list (vc-dir-child-files-and-states)))))
  1122. (when state-model-only-files
  1123. (setq state (cdar only-files-list))
  1124. ;; Check that all files are in a consistent state, since we use that
  1125. ;; state to decide which operation to perform.
  1126. (dolist (crt (cdr only-files-list))
  1127. (unless (vc-compatible-state (cdr crt) state)
  1128. (error "When applying VC operations to multiple files, the files are required\nto be in similar VC states.\n%s in state %s clashes with %s in state %s"
  1129. (car crt) (cdr crt) (caar only-files-list) state)))
  1130. (setq only-files-list (mapcar 'car only-files-list))
  1131. (when (and state (not (eq state 'unregistered)))
  1132. (setq model (vc-checkout-model vc-dir-backend only-files-list))))
  1133. (list vc-dir-backend files only-files-list state model)))
  1134. ;;;###autoload
  1135. (defun vc-dir (dir &optional backend)
  1136. "Show the VC status for \"interesting\" files in and below DIR.
  1137. This allows you to mark files and perform VC operations on them.
  1138. The list omits files which are up to date, with no changes in your copy
  1139. or the repository, if there is nothing in particular to say about them.
  1140. Preparing the list of file status takes time; when the buffer
  1141. first appears, it has only the first few lines of summary information.
  1142. The file lines appear later.
  1143. Optional second argument BACKEND specifies the VC backend to use.
  1144. Interactively, a prefix argument means to ask for the backend.
  1145. These are the commands available for use in the file status buffer:
  1146. \\{vc-dir-mode-map}"
  1147. (interactive
  1148. (list
  1149. ;; When you hit C-x v d in a visited VC file,
  1150. ;; the *vc-dir* buffer visits the directory under its truename;
  1151. ;; therefore it makes sense to always do that.
  1152. ;; Otherwise if you do C-x v d -> C-x C-f -> C-c v d
  1153. ;; you may get a new *vc-dir* buffer, different from the original
  1154. (file-truename (read-directory-name "VC status for directory: "
  1155. (vc-root-dir) nil t
  1156. nil))
  1157. (if current-prefix-arg
  1158. (intern
  1159. (completing-read
  1160. "Use VC backend: "
  1161. (mapcar (lambda (b) (list (symbol-name b)))
  1162. vc-handled-backends)
  1163. nil t nil nil)))))
  1164. (unless backend
  1165. (setq backend (vc-responsible-backend dir)))
  1166. (let (pop-up-windows) ; based on cvs-examine; bug#6204
  1167. (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir backend)))
  1168. (if (derived-mode-p 'vc-dir-mode)
  1169. (vc-dir-refresh)
  1170. ;; FIXME: find a better way to pass the backend to `vc-dir-mode'.
  1171. (let ((use-vc-backend backend))
  1172. (vc-dir-mode))))
  1173. (defun vc-default-dir-extra-headers (_backend _dir)
  1174. ;; Be loud by default to remind people to add code to display
  1175. ;; backend specific headers.
  1176. ;; XXX: change this to return nil before the release.
  1177. (concat
  1178. (propertize "Extra : " 'face 'font-lock-type-face)
  1179. (propertize "Please add backend specific headers here. It's easy!"
  1180. 'face 'font-lock-warning-face)))
  1181. (defvar vc-dir-filename-mouse-map
  1182. (let ((map (make-sparse-keymap)))
  1183. (define-key map [mouse-2] 'vc-dir-find-file-other-window)
  1184. map)
  1185. "Local keymap for visiting a file.")
  1186. (defun vc-default-dir-printer (_backend fileentry)
  1187. "Pretty print FILEENTRY."
  1188. ;; If you change the layout here, change vc-dir-move-to-goal-column.
  1189. ;; VC backends can implement backend specific versions of this
  1190. ;; function. Changes here might need to be reflected in the
  1191. ;; vc-BACKEND-dir-printer functions.
  1192. (let* ((isdir (vc-dir-fileinfo->directory fileentry))
  1193. (state (if isdir "" (vc-dir-fileinfo->state fileentry)))
  1194. (filename (vc-dir-fileinfo->name fileentry)))
  1195. (insert
  1196. (propertize
  1197. (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
  1198. 'face 'font-lock-type-face)
  1199. " "
  1200. (propertize
  1201. (format "%-20s" state)
  1202. 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
  1203. ((memq state '(missing conflict)) 'font-lock-warning-face)
  1204. ((eq state 'edited) 'font-lock-constant-face)
  1205. (t 'font-lock-variable-name-face))
  1206. 'mouse-face 'highlight)
  1207. " "
  1208. (propertize
  1209. (format "%s" filename)
  1210. 'face
  1211. (if isdir 'font-lock-comment-delimiter-face 'font-lock-function-name-face)
  1212. 'help-echo
  1213. (if isdir
  1214. "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
  1215. "File\nmouse-3: Pop-up menu")
  1216. 'mouse-face 'highlight
  1217. 'keymap vc-dir-filename-mouse-map))))
  1218. (defun vc-default-extra-status-menu (_backend)
  1219. nil)
  1220. (defun vc-default-status-fileinfo-extra (_backend _file)
  1221. "Default absence of extra information returned for a file."
  1222. nil)
  1223. ;;; Support for desktop.el (adapted from what dired.el does).
  1224. (declare-function desktop-file-name "desktop" (filename dirname))
  1225. (defun vc-dir-desktop-buffer-misc-data (dirname)
  1226. "Auxiliary information to be saved in desktop file."
  1227. (cons (desktop-file-name default-directory dirname) vc-dir-backend))
  1228. (defvar desktop-missing-file-warning)
  1229. (defun vc-dir-restore-desktop-buffer (_filename _buffername misc-data)
  1230. "Restore a `vc-dir' buffer specified in a desktop file."
  1231. (let ((dir (car misc-data))
  1232. (backend (cdr misc-data)))
  1233. (if (file-directory-p dir)
  1234. (progn
  1235. (vc-dir dir backend)
  1236. (current-buffer))
  1237. (message "Desktop: Directory %s no longer exists." dir)
  1238. (when desktop-missing-file-warning (sit-for 1))
  1239. nil)))
  1240. (add-to-list 'desktop-buffer-mode-handlers
  1241. '(vc-dir-mode . vc-dir-restore-desktop-buffer))
  1242. (provide 'vc-dir)
  1243. ;;; vc-dir.el ends here