PageRenderTime 95ms CodeModel.GetById 57ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/mercurial.el

https://bitbucket.org/mirror/mercurial/
Emacs Lisp | 1293 lines | 1089 code | 152 blank | 52 comment | 31 complexity | f75f6a8372b995f828c40aa164b7f8c5 MD5 | raw file
Possible License(s): GPL-2.0
  1. ;;; mercurial.el --- Emacs support for the Mercurial distributed SCM
  2. ;; Copyright (C) 2005, 2006 Bryan O'Sullivan
  3. ;; Author: Bryan O'Sullivan <bos@serpentine.com>
  4. ;; mercurial.el is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU General Public License version
  6. ;; 2 or any later version.
  7. ;; mercurial.el is distributed in the hope that it will be useful, but
  8. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. ;; General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with mercurial.el, GNU Emacs, or XEmacs; see the file COPYING
  13. ;; (`C-h C-l'). If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; mercurial.el builds upon Emacs's VC mode to provide flexible
  16. ;; integration with the Mercurial distributed SCM tool.
  17. ;; To get going as quickly as possible, load mercurial.el into Emacs and
  18. ;; type `C-c h h'; this runs hg-help-overview, which prints a helpful
  19. ;; usage overview.
  20. ;; Much of the inspiration for mercurial.el comes from Rajesh
  21. ;; Vaidheeswarran's excellent p4.el, which does an admirably thorough
  22. ;; job for the commercial Perforce SCM product. In fact, substantial
  23. ;; chunks of code are adapted from p4.el.
  24. ;; This code has been developed under XEmacs 21.5, and may not work as
  25. ;; well under GNU Emacs (albeit tested under 21.4). Patches to
  26. ;; enhance the portability of this code, fix bugs, and add features
  27. ;; are most welcome.
  28. ;; As of version 22.3, GNU Emacs's VC mode has direct support for
  29. ;; Mercurial, so this package may not prove as useful there.
  30. ;; Please send problem reports and suggestions to bos@serpentine.com.
  31. ;;; Code:
  32. (eval-when-compile (require 'cl))
  33. (require 'diff-mode)
  34. (require 'easymenu)
  35. (require 'executable)
  36. (require 'vc)
  37. (defmacro hg-feature-cond (&rest clauses)
  38. "Test CLAUSES for feature at compile time.
  39. Each clause is (FEATURE BODY...)."
  40. (dolist (x clauses)
  41. (let ((feature (car x))
  42. (body (cdr x)))
  43. (when (or (eq feature t)
  44. (featurep feature))
  45. (return (cons 'progn body))))))
  46. ;;; XEmacs has view-less, while GNU Emacs has view. Joy.
  47. (hg-feature-cond
  48. (xemacs (require 'view-less))
  49. (t (require 'view)))
  50. ;;; Variables accessible through the custom system.
  51. (defgroup mercurial nil
  52. "Mercurial distributed SCM."
  53. :group 'tools)
  54. (defcustom hg-binary
  55. (or (executable-find "hg")
  56. (dolist (path '("~/bin/hg" "/usr/bin/hg" "/usr/local/bin/hg"))
  57. (when (file-executable-p path)
  58. (return path))))
  59. "The path to Mercurial's hg executable."
  60. :type '(file :must-match t)
  61. :group 'mercurial)
  62. (defcustom hg-mode-hook nil
  63. "Hook run when a buffer enters hg-mode."
  64. :type 'sexp
  65. :group 'mercurial)
  66. (defcustom hg-commit-mode-hook nil
  67. "Hook run when a buffer is created to prepare a commit."
  68. :type 'sexp
  69. :group 'mercurial)
  70. (defcustom hg-pre-commit-hook nil
  71. "Hook run before a commit is performed.
  72. If you want to prevent the commit from proceeding, raise an error."
  73. :type 'sexp
  74. :group 'mercurial)
  75. (defcustom hg-log-mode-hook nil
  76. "Hook run after a buffer is filled with log information."
  77. :type 'sexp
  78. :group 'mercurial)
  79. (defcustom hg-global-prefix "\C-ch"
  80. "The global prefix for Mercurial keymap bindings."
  81. :type 'sexp
  82. :group 'mercurial)
  83. (defcustom hg-commit-allow-empty-message nil
  84. "Whether to allow changes to be committed with empty descriptions."
  85. :type 'boolean
  86. :group 'mercurial)
  87. (defcustom hg-commit-allow-empty-file-list nil
  88. "Whether to allow changes to be committed without any modified files."
  89. :type 'boolean
  90. :group 'mercurial)
  91. (defcustom hg-rev-completion-limit 100
  92. "The maximum number of revisions that hg-read-rev will offer to complete.
  93. This affects memory usage and performance when prompting for revisions
  94. in a repository with a lot of history."
  95. :type 'integer
  96. :group 'mercurial)
  97. (defcustom hg-log-limit 50
  98. "The maximum number of revisions that hg-log will display."
  99. :type 'integer
  100. :group 'mercurial)
  101. (defcustom hg-update-modeline t
  102. "Whether to update the modeline with the status of a file after every save.
  103. Set this to nil on platforms with poor process management, such as Windows."
  104. :type 'boolean
  105. :group 'mercurial)
  106. (defcustom hg-incoming-repository "default"
  107. "The repository from which changes are pulled from by default.
  108. This should be a symbolic repository name, since it is used for all
  109. repository-related commands."
  110. :type 'string
  111. :group 'mercurial)
  112. (defcustom hg-outgoing-repository ""
  113. "The repository to which changes are pushed to by default.
  114. This should be a symbolic repository name, since it is used for all
  115. repository-related commands."
  116. :type 'string
  117. :group 'mercurial)
  118. ;;; Other variables.
  119. (defvar hg-mode nil
  120. "Is this file managed by Mercurial?")
  121. (make-variable-buffer-local 'hg-mode)
  122. (put 'hg-mode 'permanent-local t)
  123. (defvar hg-status nil)
  124. (make-variable-buffer-local 'hg-status)
  125. (put 'hg-status 'permanent-local t)
  126. (defvar hg-prev-buffer nil)
  127. (make-variable-buffer-local 'hg-prev-buffer)
  128. (put 'hg-prev-buffer 'permanent-local t)
  129. (defvar hg-root nil)
  130. (make-variable-buffer-local 'hg-root)
  131. (put 'hg-root 'permanent-local t)
  132. (defvar hg-view-mode nil)
  133. (make-variable-buffer-local 'hg-view-mode)
  134. (put 'hg-view-mode 'permanent-local t)
  135. (defvar hg-view-file-name nil)
  136. (make-variable-buffer-local 'hg-view-file-name)
  137. (put 'hg-view-file-name 'permanent-local t)
  138. (defvar hg-output-buffer-name "*Hg*"
  139. "The name to use for Mercurial output buffers.")
  140. (defvar hg-file-history nil)
  141. (defvar hg-repo-history nil)
  142. (defvar hg-rev-history nil)
  143. (defvar hg-repo-completion-table nil) ; shut up warnings
  144. ;;; Random constants.
  145. (defconst hg-commit-message-start
  146. "--- Enter your commit message. Type `C-c C-c' to commit. ---\n")
  147. (defconst hg-commit-message-end
  148. "--- Files in bold will be committed. Click to toggle selection. ---\n")
  149. (defconst hg-state-alist
  150. '((?M . modified)
  151. (?A . added)
  152. (?R . removed)
  153. (?! . deleted)
  154. (?C . normal)
  155. (?I . ignored)
  156. (?? . nil)))
  157. ;;; hg-mode keymap.
  158. (defvar hg-prefix-map
  159. (let ((map (make-sparse-keymap)))
  160. (hg-feature-cond (xemacs (set-keymap-name map 'hg-prefix-map))) ; XEmacs
  161. (set-keymap-parent map vc-prefix-map)
  162. (define-key map "=" 'hg-diff)
  163. (define-key map "c" 'hg-undo)
  164. (define-key map "g" 'hg-annotate)
  165. (define-key map "i" 'hg-add)
  166. (define-key map "l" 'hg-log)
  167. (define-key map "n" 'hg-commit-start)
  168. ;; (define-key map "r" 'hg-update)
  169. (define-key map "u" 'hg-revert-buffer)
  170. (define-key map "~" 'hg-version-other-window)
  171. map)
  172. "This keymap overrides some default vc-mode bindings.")
  173. (defvar hg-mode-map
  174. (let ((map (make-sparse-keymap)))
  175. (define-key map "\C-xv" hg-prefix-map)
  176. map))
  177. (add-minor-mode 'hg-mode 'hg-mode hg-mode-map)
  178. ;;; Global keymap.
  179. (defvar hg-global-map
  180. (let ((map (make-sparse-keymap)))
  181. (define-key map "," 'hg-incoming)
  182. (define-key map "." 'hg-outgoing)
  183. (define-key map "<" 'hg-pull)
  184. (define-key map "=" 'hg-diff-repo)
  185. (define-key map ">" 'hg-push)
  186. (define-key map "?" 'hg-help-overview)
  187. (define-key map "A" 'hg-addremove)
  188. (define-key map "U" 'hg-revert)
  189. (define-key map "a" 'hg-add)
  190. (define-key map "c" 'hg-commit-start)
  191. (define-key map "f" 'hg-forget)
  192. (define-key map "h" 'hg-help-overview)
  193. (define-key map "i" 'hg-init)
  194. (define-key map "l" 'hg-log-repo)
  195. (define-key map "r" 'hg-root)
  196. (define-key map "s" 'hg-status)
  197. (define-key map "u" 'hg-update)
  198. map))
  199. (global-set-key hg-global-prefix hg-global-map)
  200. ;;; View mode keymap.
  201. (defvar hg-view-mode-map
  202. (let ((map (make-sparse-keymap)))
  203. (hg-feature-cond (xemacs (set-keymap-name map 'hg-view-mode-map))) ; XEmacs
  204. (define-key map (hg-feature-cond (xemacs [button2])
  205. (t [mouse-2]))
  206. 'hg-buffer-mouse-clicked)
  207. map))
  208. (add-minor-mode 'hg-view-mode "" hg-view-mode-map)
  209. ;;; Commit mode keymaps.
  210. (defvar hg-commit-mode-map
  211. (let ((map (make-sparse-keymap)))
  212. (define-key map "\C-c\C-c" 'hg-commit-finish)
  213. (define-key map "\C-c\C-k" 'hg-commit-kill)
  214. (define-key map "\C-xv=" 'hg-diff-repo)
  215. map))
  216. (defvar hg-commit-mode-file-map
  217. (let ((map (make-sparse-keymap)))
  218. (define-key map (hg-feature-cond (xemacs [button2])
  219. (t [mouse-2]))
  220. 'hg-commit-mouse-clicked)
  221. (define-key map " " 'hg-commit-toggle-file)
  222. (define-key map "\r" 'hg-commit-toggle-file)
  223. map))
  224. ;;; Convenience functions.
  225. (defsubst hg-binary ()
  226. (if hg-binary
  227. hg-binary
  228. (error "No `hg' executable found!")))
  229. (defsubst hg-replace-in-string (str regexp newtext &optional literal)
  230. "Replace all matches in STR for REGEXP with NEWTEXT string.
  231. Return the new string. Optional LITERAL non-nil means do a literal
  232. replacement.
  233. This function bridges yet another pointless impedance gap between
  234. XEmacs and GNU Emacs."
  235. (hg-feature-cond
  236. (xemacs (replace-in-string str regexp newtext literal))
  237. (t (replace-regexp-in-string regexp newtext str nil literal))))
  238. (defsubst hg-strip (str)
  239. "Strip leading and trailing blank lines from a string."
  240. (hg-replace-in-string (hg-replace-in-string str "[\r\n][ \t\r\n]*\\'" "")
  241. "\\`[ \t\r\n]*[\r\n]" ""))
  242. (defsubst hg-chomp (str)
  243. "Strip trailing newlines from a string."
  244. (hg-replace-in-string str "[\r\n]+\\'" ""))
  245. (defun hg-run-command (command &rest args)
  246. "Run the shell command COMMAND, returning (EXIT-CODE . COMMAND-OUTPUT).
  247. The list ARGS contains a list of arguments to pass to the command."
  248. (let* (exit-code
  249. (output
  250. (with-output-to-string
  251. (with-current-buffer
  252. standard-output
  253. (setq exit-code
  254. (apply 'call-process command nil t nil args))))))
  255. (cons exit-code output)))
  256. (defun hg-run (command &rest args)
  257. "Run the Mercurial command COMMAND, returning (EXIT-CODE . COMMAND-OUTPUT)."
  258. (apply 'hg-run-command (hg-binary) command args))
  259. (defun hg-run0 (command &rest args)
  260. "Run the Mercurial command COMMAND, returning its output.
  261. If the command does not exit with a zero status code, raise an error."
  262. (let ((res (apply 'hg-run-command (hg-binary) command args)))
  263. (if (not (eq (car res) 0))
  264. (error "Mercurial command failed %s - exit code %s"
  265. (cons command args)
  266. (car res))
  267. (cdr res))))
  268. (defmacro hg-do-across-repo (path &rest body)
  269. (let ((root-name (make-symbol "root-"))
  270. (buf-name (make-symbol "buf-")))
  271. `(let ((,root-name (hg-root ,path)))
  272. (save-excursion
  273. (dolist (,buf-name (buffer-list))
  274. (set-buffer ,buf-name)
  275. (when (and hg-status (equal (hg-root buffer-file-name) ,root-name))
  276. ,@body))))))
  277. (put 'hg-do-across-repo 'lisp-indent-function 1)
  278. (defun hg-sync-buffers (path)
  279. "Sync buffers visiting PATH with their on-disk copies.
  280. If PATH is not being visited, but is under the repository root, sync
  281. all buffers visiting files in the repository."
  282. (let ((buf (find-buffer-visiting path)))
  283. (if buf
  284. (with-current-buffer buf
  285. (vc-buffer-sync))
  286. (hg-do-across-repo path
  287. (vc-buffer-sync)))))
  288. (defun hg-buffer-commands (pnt)
  289. "Use the properties of a character to do something sensible."
  290. (interactive "d")
  291. (let ((rev (get-char-property pnt 'rev))
  292. (file (get-char-property pnt 'file)))
  293. (cond
  294. (file
  295. (find-file-other-window file))
  296. (rev
  297. (hg-diff hg-view-file-name rev rev))
  298. ((message "I don't know how to do that yet")))))
  299. (defsubst hg-event-point (event)
  300. "Return the character position of the mouse event EVENT."
  301. (hg-feature-cond (xemacs (event-point event))
  302. (t (posn-point (event-start event)))))
  303. (defsubst hg-event-window (event)
  304. "Return the window over which mouse event EVENT occurred."
  305. (hg-feature-cond (xemacs (event-window event))
  306. (t (posn-window (event-start event)))))
  307. (defun hg-buffer-mouse-clicked (event)
  308. "Translate the mouse clicks in a HG log buffer to character events.
  309. These are then handed off to `hg-buffer-commands'.
  310. Handle frickin' frackin' gratuitous event-related incompatibilities."
  311. (interactive "e")
  312. (select-window (hg-event-window event))
  313. (hg-buffer-commands (hg-event-point event)))
  314. (defsubst hg-abbrev-file-name (file)
  315. "Portable wrapper around abbreviate-file-name."
  316. (hg-feature-cond (xemacs (abbreviate-file-name file t))
  317. (t (abbreviate-file-name file))))
  318. (defun hg-read-file-name (&optional prompt default)
  319. "Read a file or directory name, or a pattern, to use with a command."
  320. (save-excursion
  321. (while hg-prev-buffer
  322. (set-buffer hg-prev-buffer))
  323. (let ((path (or default
  324. (buffer-file-name)
  325. (expand-file-name default-directory))))
  326. (if (or (not path) current-prefix-arg)
  327. (expand-file-name
  328. (eval (list* 'read-file-name
  329. (format "File, directory or pattern%s: "
  330. (or prompt ""))
  331. (and path (file-name-directory path))
  332. nil nil
  333. (and path (file-name-nondirectory path))
  334. (hg-feature-cond
  335. (xemacs (cons (quote 'hg-file-history) nil))
  336. (t nil)))))
  337. path))))
  338. (defun hg-read-number (&optional prompt default)
  339. "Read a integer value."
  340. (save-excursion
  341. (if (or (not default) current-prefix-arg)
  342. (string-to-number
  343. (eval (list* 'read-string
  344. (or prompt "")
  345. (if default (cons (format "%d" default) nil) nil))))
  346. default)))
  347. (defun hg-read-config ()
  348. "Return an alist of (key . value) pairs of Mercurial config data.
  349. Each key is of the form (section . name)."
  350. (let (items)
  351. (dolist (line (split-string (hg-chomp (hg-run0 "debugconfig")) "\n") items)
  352. (string-match "^\\([^=]*\\)=\\(.*\\)" line)
  353. (let* ((left (substring line (match-beginning 1) (match-end 1)))
  354. (right (substring line (match-beginning 2) (match-end 2)))
  355. (key (split-string left "\\."))
  356. (value (hg-replace-in-string right "\\\\n" "\n" t)))
  357. (setq items (cons (cons (cons (car key) (cadr key)) value) items))))))
  358. (defun hg-config-section (section config)
  359. "Return an alist of (name . value) pairs for SECTION of CONFIG."
  360. (let (items)
  361. (dolist (item config items)
  362. (when (equal (caar item) section)
  363. (setq items (cons (cons (cdar item) (cdr item)) items))))))
  364. (defun hg-string-starts-with (sub str)
  365. "Indicate whether string STR starts with the substring or character SUB."
  366. (if (not (stringp sub))
  367. (and (> (length str) 0) (equal (elt str 0) sub))
  368. (let ((sub-len (length sub)))
  369. (and (<= sub-len (length str))
  370. (string= sub (substring str 0 sub-len))))))
  371. (defun hg-complete-repo (string predicate all)
  372. "Attempt to complete a repository name.
  373. We complete on either symbolic names from Mercurial's config or real
  374. directory names from the file system. We do not penalize URLs."
  375. (or (if all
  376. (all-completions string hg-repo-completion-table predicate)
  377. (try-completion string hg-repo-completion-table predicate))
  378. (let* ((str (expand-file-name string))
  379. (dir (file-name-directory str))
  380. (file (file-name-nondirectory str)))
  381. (if all
  382. (let (completions)
  383. (dolist (name (delete "./" (file-name-all-completions file dir))
  384. completions)
  385. (let ((path (concat dir name)))
  386. (when (file-directory-p path)
  387. (setq completions (cons name completions))))))
  388. (let ((comp (file-name-completion file dir)))
  389. (if comp
  390. (hg-abbrev-file-name (concat dir comp))))))))
  391. (defun hg-read-repo-name (&optional prompt initial-contents default)
  392. "Read the location of a repository."
  393. (save-excursion
  394. (while hg-prev-buffer
  395. (set-buffer hg-prev-buffer))
  396. (let (hg-repo-completion-table)
  397. (if current-prefix-arg
  398. (progn
  399. (dolist (path (hg-config-section "paths" (hg-read-config)))
  400. (setq hg-repo-completion-table
  401. (cons (cons (car path) t) hg-repo-completion-table))
  402. (unless (hg-string-starts-with (hg-feature-cond
  403. (xemacs directory-sep-char)
  404. (t ?/))
  405. (cdr path))
  406. (setq hg-repo-completion-table
  407. (cons (cons (cdr path) t) hg-repo-completion-table))))
  408. (completing-read (format "Repository%s: " (or prompt ""))
  409. 'hg-complete-repo
  410. nil
  411. nil
  412. initial-contents
  413. 'hg-repo-history
  414. default))
  415. default))))
  416. (defun hg-read-rev (&optional prompt default)
  417. "Read a revision or tag, offering completions."
  418. (save-excursion
  419. (while hg-prev-buffer
  420. (set-buffer hg-prev-buffer))
  421. (let ((rev (or default "tip")))
  422. (if current-prefix-arg
  423. (let ((revs (split-string
  424. (hg-chomp
  425. (hg-run0 "-q" "log" "-l"
  426. (format "%d" hg-rev-completion-limit)))
  427. "[\n:]")))
  428. (dolist (line (split-string (hg-chomp (hg-run0 "tags")) "\n"))
  429. (setq revs (cons (car (split-string line "\\s-")) revs)))
  430. (completing-read (format "Revision%s (%s): "
  431. (or prompt "")
  432. (or default "tip"))
  433. (mapcar (lambda (x) (cons x x)) revs)
  434. nil
  435. nil
  436. nil
  437. 'hg-rev-history
  438. (or default "tip")))
  439. rev))))
  440. (defun hg-parents-for-mode-line (root)
  441. "Format the parents of the working directory for the mode line."
  442. (let ((parents (split-string (hg-chomp
  443. (hg-run0 "--cwd" root "parents" "--template"
  444. "{rev}\n")) "\n")))
  445. (mapconcat 'identity parents "+")))
  446. (defun hg-buffers-visiting-repo (&optional path)
  447. "Return a list of buffers visiting the repository containing PATH."
  448. (let ((root-name (hg-root (or path (buffer-file-name))))
  449. bufs)
  450. (save-excursion
  451. (dolist (buf (buffer-list) bufs)
  452. (set-buffer buf)
  453. (let ((name (buffer-file-name)))
  454. (when (and hg-status name (equal (hg-root name) root-name))
  455. (setq bufs (cons buf bufs))))))))
  456. (defun hg-update-mode-lines (path)
  457. "Update the mode lines of all buffers visiting the same repository as PATH."
  458. (let* ((root (hg-root path))
  459. (parents (hg-parents-for-mode-line root)))
  460. (save-excursion
  461. (dolist (info (hg-path-status
  462. root
  463. (mapcar
  464. (function
  465. (lambda (buf)
  466. (substring (buffer-file-name buf) (length root))))
  467. (hg-buffers-visiting-repo root))))
  468. (let* ((name (car info))
  469. (status (cdr info))
  470. (buf (find-buffer-visiting (concat root name))))
  471. (when buf
  472. (set-buffer buf)
  473. (hg-mode-line-internal status parents)))))))
  474. ;;; View mode bits.
  475. (defun hg-exit-view-mode (buf)
  476. "Exit from hg-view-mode.
  477. We delete the current window if entering hg-view-mode split the
  478. current frame."
  479. (when (and (eq buf (current-buffer))
  480. (> (length (window-list)) 1))
  481. (delete-window))
  482. (when (buffer-live-p buf)
  483. (kill-buffer buf)))
  484. (defun hg-view-mode (prev-buffer &optional file-name)
  485. (goto-char (point-min))
  486. (set-buffer-modified-p nil)
  487. (toggle-read-only t)
  488. (hg-feature-cond (xemacs (view-minor-mode prev-buffer 'hg-exit-view-mode))
  489. (t (view-mode-enter nil 'hg-exit-view-mode)))
  490. (setq hg-view-mode t)
  491. (setq truncate-lines t)
  492. (when file-name
  493. (setq hg-view-file-name
  494. (hg-abbrev-file-name file-name))))
  495. (defun hg-file-status (file)
  496. "Return status of FILE, or nil if FILE does not exist or is unmanaged."
  497. (let* ((s (hg-run "status" file))
  498. (exit (car s))
  499. (output (cdr s)))
  500. (if (= exit 0)
  501. (let ((state (and (>= (length output) 2)
  502. (= (aref output 1) ? )
  503. (assq (aref output 0) hg-state-alist))))
  504. (if state
  505. (cdr state)
  506. 'normal)))))
  507. (defun hg-path-status (root paths)
  508. "Return status of PATHS in repo ROOT as an alist.
  509. Each entry is a pair (FILE-NAME . STATUS)."
  510. (let ((s (apply 'hg-run "--cwd" root "status" "-marduc" paths))
  511. result)
  512. (dolist (entry (split-string (hg-chomp (cdr s)) "\n") (nreverse result))
  513. (let (state name)
  514. (cond ((= (aref entry 1) ? )
  515. (setq state (assq (aref entry 0) hg-state-alist)
  516. name (substring entry 2)))
  517. ((string-match "\\(.*\\): " entry)
  518. (setq name (match-string 1 entry))))
  519. (setq result (cons (cons name state) result))))))
  520. (defmacro hg-view-output (args &rest body)
  521. "Execute BODY in a clean buffer, then quickly display that buffer.
  522. If the buffer contains one line, its contents are displayed in the
  523. minibuffer. Otherwise, the buffer is displayed in view-mode.
  524. ARGS is of the form (BUFFER-NAME &optional FILE), where BUFFER-NAME is
  525. the name of the buffer to create, and FILE is the name of the file
  526. being viewed."
  527. (let ((prev-buf (make-symbol "prev-buf-"))
  528. (v-b-name (car args))
  529. (v-m-rest (cdr args)))
  530. `(let ((view-buf-name ,v-b-name)
  531. (,prev-buf (current-buffer)))
  532. (get-buffer-create view-buf-name)
  533. (kill-buffer view-buf-name)
  534. (get-buffer-create view-buf-name)
  535. (set-buffer view-buf-name)
  536. (save-excursion
  537. ,@body)
  538. (case (count-lines (point-min) (point-max))
  539. ((0)
  540. (kill-buffer view-buf-name)
  541. (message "(No output)"))
  542. ((1)
  543. (let ((msg (hg-chomp (buffer-substring (point-min) (point-max)))))
  544. (kill-buffer view-buf-name)
  545. (message "%s" msg)))
  546. (t
  547. (pop-to-buffer view-buf-name)
  548. (setq hg-prev-buffer ,prev-buf)
  549. (hg-view-mode ,prev-buf ,@v-m-rest))))))
  550. (put 'hg-view-output 'lisp-indent-function 1)
  551. ;;; Context save and restore across revert and other operations.
  552. (defun hg-position-context (pos)
  553. "Return information to help find the given position again."
  554. (let* ((end (min (point-max) (+ pos 98))))
  555. (list pos
  556. (buffer-substring (max (point-min) (- pos 2)) end)
  557. (- end pos))))
  558. (defun hg-buffer-context ()
  559. "Return information to help restore a user's editing context.
  560. This is useful across reverts and merges, where a context is likely
  561. to have moved a little, but not really changed."
  562. (let ((point-context (hg-position-context (point)))
  563. (mark-context (let ((mark (mark-marker)))
  564. (and mark
  565. ;; make sure active mark
  566. (marker-buffer mark)
  567. (marker-position mark)
  568. (hg-position-context mark)))))
  569. (list point-context mark-context)))
  570. (defun hg-find-context (ctx)
  571. "Attempt to find a context in the given buffer.
  572. Always returns a valid, hopefully sane, position."
  573. (let ((pos (nth 0 ctx))
  574. (str (nth 1 ctx))
  575. (fixup (nth 2 ctx)))
  576. (save-excursion
  577. (goto-char (max (point-min) (- pos 15000)))
  578. (if (and (not (equal str ""))
  579. (search-forward str nil t))
  580. (- (point) fixup)
  581. (max pos (point-min))))))
  582. (defun hg-restore-context (ctx)
  583. "Attempt to restore the user's editing context."
  584. (let ((point-context (nth 0 ctx))
  585. (mark-context (nth 1 ctx)))
  586. (goto-char (hg-find-context point-context))
  587. (when mark-context
  588. (set-mark (hg-find-context mark-context)))))
  589. ;;; Hooks.
  590. (defun hg-mode-line-internal (status parents)
  591. (setq hg-status status
  592. hg-mode (and status (concat " Hg:"
  593. parents
  594. (cdr (assq status
  595. '((normal . "")
  596. (removed . "r")
  597. (added . "a")
  598. (deleted . "!")
  599. (modified . "m"))))))))
  600. (defun hg-mode-line (&optional force)
  601. "Update the modeline with the current status of a file.
  602. An update occurs if optional argument FORCE is non-nil,
  603. hg-update-modeline is non-nil, or we have not yet checked the state of
  604. the file."
  605. (let ((root (hg-root)))
  606. (when (and root (or force hg-update-modeline (not hg-mode)))
  607. (let ((status (hg-file-status buffer-file-name))
  608. (parents (hg-parents-for-mode-line root)))
  609. (hg-mode-line-internal status parents)
  610. status))))
  611. (defun hg-mode (&optional toggle)
  612. "Minor mode for Mercurial distributed SCM integration.
  613. The Mercurial mode user interface is based on that of VC mode, so if
  614. you're already familiar with VC, the same keybindings and functions
  615. will generally work.
  616. Below is a list of many common SCM tasks. In the list, `G/L\'
  617. indicates whether a key binding is global (G) to a repository or
  618. local (L) to a file. Many commands take a prefix argument.
  619. SCM Task G/L Key Binding Command Name
  620. -------- --- ----------- ------------
  621. Help overview (what you are reading) G C-c h h hg-help-overview
  622. Tell Mercurial to manage a file G C-c h a hg-add
  623. Commit changes to current file only L C-x v n hg-commit-start
  624. Undo changes to file since commit L C-x v u hg-revert-buffer
  625. Diff file vs last checkin L C-x v = hg-diff
  626. View file change history L C-x v l hg-log
  627. View annotated file L C-x v a hg-annotate
  628. Diff repo vs last checkin G C-c h = hg-diff-repo
  629. View status of files in repo G C-c h s hg-status
  630. Commit all changes G C-c h c hg-commit-start
  631. Undo all changes since last commit G C-c h U hg-revert
  632. View repo change history G C-c h l hg-log-repo
  633. See changes that can be pulled G C-c h , hg-incoming
  634. Pull changes G C-c h < hg-pull
  635. Update working directory after pull G C-c h u hg-update
  636. See changes that can be pushed G C-c h . hg-outgoing
  637. Push changes G C-c h > hg-push"
  638. (unless vc-make-backup-files
  639. (set (make-local-variable 'backup-inhibited) t))
  640. (run-hooks 'hg-mode-hook))
  641. (defun hg-find-file-hook ()
  642. (ignore-errors
  643. (when (hg-mode-line)
  644. (hg-mode))))
  645. (add-hook 'find-file-hooks 'hg-find-file-hook)
  646. (defun hg-after-save-hook ()
  647. (ignore-errors
  648. (let ((old-status hg-status))
  649. (hg-mode-line)
  650. (if (and (not old-status) hg-status)
  651. (hg-mode)))))
  652. (add-hook 'after-save-hook 'hg-after-save-hook)
  653. ;;; User interface functions.
  654. (defun hg-help-overview ()
  655. "This is an overview of the Mercurial SCM mode for Emacs.
  656. You can find the source code, license (GPLv2+), and credits for this
  657. code by typing `M-x find-library mercurial RET'."
  658. (interactive)
  659. (hg-view-output ("Mercurial Help Overview")
  660. (insert (documentation 'hg-help-overview))
  661. (let ((pos (point)))
  662. (insert (documentation 'hg-mode))
  663. (goto-char pos)
  664. (end-of-line 1)
  665. (delete-region pos (point)))
  666. (let ((hg-root-dir (hg-root)))
  667. (if (not hg-root-dir)
  668. (error "error: %s: directory is not part of a Mercurial repository."
  669. default-directory)
  670. (cd hg-root-dir)))))
  671. (defun hg-fix-paths ()
  672. "Fix paths reported by some Mercurial commands."
  673. (save-excursion
  674. (goto-char (point-min))
  675. (while (re-search-forward " \\.\\.." nil t)
  676. (replace-match " " nil nil))))
  677. (defun hg-add (path)
  678. "Add PATH to the Mercurial repository on the next commit.
  679. With a prefix argument, prompt for the path to add."
  680. (interactive (list (hg-read-file-name " to add")))
  681. (let ((buf (current-buffer))
  682. (update (equal buffer-file-name path)))
  683. (hg-view-output (hg-output-buffer-name)
  684. (apply 'call-process (hg-binary) nil t nil (list "add" path))
  685. (hg-fix-paths)
  686. (goto-char (point-min))
  687. (cd (hg-root path)))
  688. (when update
  689. (unless vc-make-backup-files
  690. (set (make-local-variable 'backup-inhibited) t))
  691. (with-current-buffer buf
  692. (hg-mode-line)))))
  693. (defun hg-addremove ()
  694. (interactive)
  695. (error "not implemented"))
  696. (defun hg-annotate ()
  697. (interactive)
  698. (error "not implemented"))
  699. (defun hg-commit-toggle-file (pos)
  700. "Toggle whether or not the file at POS will be committed."
  701. (interactive "d")
  702. (save-excursion
  703. (goto-char pos)
  704. (let (face
  705. (inhibit-read-only t)
  706. bol)
  707. (beginning-of-line)
  708. (setq bol (+ (point) 4))
  709. (setq face (get-text-property bol 'face))
  710. (end-of-line)
  711. (if (eq face 'bold)
  712. (progn
  713. (remove-text-properties bol (point) '(face nil))
  714. (message "%s will not be committed"
  715. (buffer-substring bol (point))))
  716. (add-text-properties bol (point) '(face bold))
  717. (message "%s will be committed"
  718. (buffer-substring bol (point)))))))
  719. (defun hg-commit-mouse-clicked (event)
  720. "Toggle whether or not the file at POS will be committed."
  721. (interactive "@e")
  722. (hg-commit-toggle-file (hg-event-point event)))
  723. (defun hg-commit-kill ()
  724. "Kill the commit currently being prepared."
  725. (interactive)
  726. (when (or (not (buffer-modified-p)) (y-or-n-p "Really kill this commit? "))
  727. (let ((buf hg-prev-buffer))
  728. (kill-buffer nil)
  729. (switch-to-buffer buf))))
  730. (defun hg-commit-finish ()
  731. "Finish preparing a commit, and perform the actual commit.
  732. The hook hg-pre-commit-hook is run before anything else is done. If
  733. the commit message is empty and hg-commit-allow-empty-message is nil,
  734. an error is raised. If the list of files to commit is empty and
  735. hg-commit-allow-empty-file-list is nil, an error is raised."
  736. (interactive)
  737. (let ((root hg-root))
  738. (save-excursion
  739. (run-hooks 'hg-pre-commit-hook)
  740. (goto-char (point-min))
  741. (search-forward hg-commit-message-start)
  742. (let (message files)
  743. (let ((start (point)))
  744. (goto-char (point-max))
  745. (search-backward hg-commit-message-end)
  746. (setq message (hg-strip (buffer-substring start (point)))))
  747. (when (and (= (length message) 0)
  748. (not hg-commit-allow-empty-message))
  749. (error "Cannot proceed - commit message is empty"))
  750. (forward-line 1)
  751. (beginning-of-line)
  752. (while (< (point) (point-max))
  753. (let ((pos (+ (point) 4)))
  754. (end-of-line)
  755. (when (eq (get-text-property pos 'face) 'bold)
  756. (end-of-line)
  757. (setq files (cons (buffer-substring pos (point)) files))))
  758. (forward-line 1))
  759. (when (and (= (length files) 0)
  760. (not hg-commit-allow-empty-file-list))
  761. (error "Cannot proceed - no files to commit"))
  762. (setq message (concat message "\n"))
  763. (apply 'hg-run0 "--cwd" hg-root "commit" "-m" message files))
  764. (let ((buf hg-prev-buffer))
  765. (kill-buffer nil)
  766. (switch-to-buffer buf))
  767. (hg-update-mode-lines root))))
  768. (defun hg-commit-mode ()
  769. "Mode for describing a commit of changes to a Mercurial repository.
  770. This involves two actions: describing the changes with a commit
  771. message, and choosing the files to commit.
  772. To describe the commit, simply type some text in the designated area.
  773. By default, all modified, added and removed files are selected for
  774. committing. Files that will be committed are displayed in bold face\;
  775. those that will not are displayed in normal face.
  776. To toggle whether a file will be committed, move the cursor over a
  777. particular file and hit space or return. Alternatively, middle click
  778. on the file.
  779. Key bindings
  780. ------------
  781. \\[hg-commit-finish] proceed with commit
  782. \\[hg-commit-kill] kill commit
  783. \\[hg-diff-repo] view diff of pending changes"
  784. (interactive)
  785. (use-local-map hg-commit-mode-map)
  786. (set-syntax-table text-mode-syntax-table)
  787. (setq local-abbrev-table text-mode-abbrev-table
  788. major-mode 'hg-commit-mode
  789. mode-name "Hg-Commit")
  790. (set-buffer-modified-p nil)
  791. (setq buffer-undo-list nil)
  792. (run-hooks 'text-mode-hook 'hg-commit-mode-hook))
  793. (defun hg-commit-start ()
  794. "Prepare a commit of changes to the repository containing the current file."
  795. (interactive)
  796. (while hg-prev-buffer
  797. (set-buffer hg-prev-buffer))
  798. (let ((root (hg-root))
  799. (prev-buffer (current-buffer))
  800. modified-files)
  801. (unless root
  802. (error "Cannot commit outside a repository!"))
  803. (hg-sync-buffers root)
  804. (setq modified-files (hg-chomp (hg-run0 "--cwd" root "status" "-arm")))
  805. (when (and (= (length modified-files) 0)
  806. (not hg-commit-allow-empty-file-list))
  807. (error "No pending changes to commit"))
  808. (let* ((buf-name (format "*Mercurial: Commit %s*" root)))
  809. (pop-to-buffer (get-buffer-create buf-name))
  810. (when (= (point-min) (point-max))
  811. (set (make-local-variable 'hg-root) root)
  812. (setq hg-prev-buffer prev-buffer)
  813. (insert "\n")
  814. (let ((bol (point)))
  815. (insert hg-commit-message-end)
  816. (add-text-properties bol (point) '(face bold-italic)))
  817. (let ((file-area (point)))
  818. (insert modified-files)
  819. (goto-char file-area)
  820. (while (< (point) (point-max))
  821. (let ((bol (point)))
  822. (forward-char 1)
  823. (insert " ")
  824. (end-of-line)
  825. (add-text-properties (+ bol 4) (point)
  826. '(face bold mouse-face highlight)))
  827. (forward-line 1))
  828. (goto-char file-area)
  829. (add-text-properties (point) (point-max)
  830. `(keymap ,hg-commit-mode-file-map))
  831. (goto-char (point-min))
  832. (insert hg-commit-message-start)
  833. (add-text-properties (point-min) (point) '(face bold-italic))
  834. (insert "\n\n")
  835. (forward-line -1)
  836. (save-excursion
  837. (goto-char (point-max))
  838. (search-backward hg-commit-message-end)
  839. (add-text-properties (match-beginning 0) (point-max)
  840. '(read-only t))
  841. (goto-char (point-min))
  842. (search-forward hg-commit-message-start)
  843. (add-text-properties (match-beginning 0) (match-end 0)
  844. '(read-only t)))
  845. (hg-commit-mode)
  846. (cd root))))))
  847. (defun hg-diff (path &optional rev1 rev2)
  848. "Show the differences between REV1 and REV2 of PATH.
  849. When called interactively, the default behaviour is to treat REV1 as
  850. the \"parent\" revision, REV2 as the current edited version of the file, and
  851. PATH as the file edited in the current buffer.
  852. With a prefix argument, prompt for all of these."
  853. (interactive (list (hg-read-file-name " to diff")
  854. (let ((rev1 (hg-read-rev " to start with" 'parent)))
  855. (and (not (eq rev1 'parent)) rev1))
  856. (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
  857. (and (not (eq rev2 'working-dir)) rev2))))
  858. (hg-sync-buffers path)
  859. (let ((a-path (hg-abbrev-file-name path))
  860. ;; none revision is specified explicitly
  861. (none (and (not rev1) (not rev2)))
  862. ;; only one revision is specified explicitly
  863. (one (or (and (or (equal rev1 rev2) (not rev2)) rev1)
  864. (and (not rev1) rev2)))
  865. diff)
  866. (hg-view-output ((cond
  867. (none
  868. (format "Mercurial: Diff against parent of %s" a-path))
  869. (one
  870. (format "Mercurial: Diff of rev %s of %s" one a-path))
  871. (t
  872. (format "Mercurial: Diff from rev %s to %s of %s"
  873. rev1 rev2 a-path))))
  874. (cond
  875. (none
  876. (call-process (hg-binary) nil t nil "diff" path))
  877. (one
  878. (call-process (hg-binary) nil t nil "diff" "-r" one path))
  879. (t
  880. (call-process (hg-binary) nil t nil "diff" "-r" rev1 "-r" rev2 path)))
  881. (diff-mode)
  882. (setq diff (not (= (point-min) (point-max))))
  883. (font-lock-fontify-buffer)
  884. (cd (hg-root path)))
  885. diff))
  886. (defun hg-diff-repo (path &optional rev1 rev2)
  887. "Show the differences between REV1 and REV2 of repository containing PATH.
  888. When called interactively, the default behaviour is to treat REV1 as
  889. the \"parent\" revision, REV2 as the current edited version of the file, and
  890. PATH as the `hg-root' of the current buffer.
  891. With a prefix argument, prompt for all of these."
  892. (interactive (list (hg-read-file-name " to diff")
  893. (let ((rev1 (hg-read-rev " to start with" 'parent)))
  894. (and (not (eq rev1 'parent)) rev1))
  895. (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
  896. (and (not (eq rev2 'working-dir)) rev2))))
  897. (hg-diff (hg-root path) rev1 rev2))
  898. (defun hg-forget (path)
  899. "Lose track of PATH, which has been added, but not yet committed.
  900. This will prevent the file from being incorporated into the Mercurial
  901. repository on the next commit.
  902. With a prefix argument, prompt for the path to forget."
  903. (interactive (list (hg-read-file-name " to forget")))
  904. (let ((buf (current-buffer))
  905. (update (equal buffer-file-name path)))
  906. (hg-view-output (hg-output-buffer-name)
  907. (apply 'call-process (hg-binary) nil t nil (list "forget" path))
  908. ;; "hg forget" shows pathes relative NOT TO ROOT BUT TO REPOSITORY
  909. (hg-fix-paths)
  910. (goto-char (point-min))
  911. (cd (hg-root path)))
  912. (when update
  913. (with-current-buffer buf
  914. (when (local-variable-p 'backup-inhibited)
  915. (kill-local-variable 'backup-inhibited))
  916. (hg-mode-line)))))
  917. (defun hg-incoming (&optional repo)
  918. "Display changesets present in REPO that are not present locally."
  919. (interactive (list (hg-read-repo-name " where changes would come from")))
  920. (hg-view-output ((format "Mercurial: Incoming from %s to %s"
  921. (hg-abbrev-file-name (hg-root))
  922. (hg-abbrev-file-name
  923. (or repo hg-incoming-repository))))
  924. (call-process (hg-binary) nil t nil "incoming"
  925. (or repo hg-incoming-repository))
  926. (hg-log-mode)
  927. (cd (hg-root))))
  928. (defun hg-init ()
  929. (interactive)
  930. (error "not implemented"))
  931. (defun hg-log-mode ()
  932. "Mode for viewing a Mercurial change log."
  933. (goto-char (point-min))
  934. (when (looking-at "^searching for changes.*$")
  935. (delete-region (match-beginning 0) (match-end 0)))
  936. (run-hooks 'hg-log-mode-hook))
  937. (defun hg-log (path &optional rev1 rev2 log-limit)
  938. "Display the revision history of PATH.
  939. History is displayed between REV1 and REV2.
  940. Number of displayed changesets is limited to LOG-LIMIT.
  941. REV1 defaults to the tip, while REV2 defaults to 0.
  942. LOG-LIMIT defaults to `hg-log-limit'.
  943. With a prefix argument, prompt for each parameter."
  944. (interactive (list (hg-read-file-name " to log")
  945. (hg-read-rev " to start with"
  946. "tip")
  947. (hg-read-rev " to end with"
  948. "0")
  949. (hg-read-number "Output limited to: "
  950. hg-log-limit)))
  951. (let ((a-path (hg-abbrev-file-name path))
  952. (r1 (or rev1 "tip"))
  953. (r2 (or rev2 "0"))
  954. (limit (format "%d" (or log-limit hg-log-limit))))
  955. (hg-view-output ((if (equal r1 r2)
  956. (format "Mercurial: Log of rev %s of %s" rev1 a-path)
  957. (format
  958. "Mercurial: at most %s log(s) from rev %s to %s of %s"
  959. limit r1 r2 a-path)))
  960. (eval (list* 'call-process (hg-binary) nil t nil
  961. "log"
  962. "-r" (format "%s:%s" r1 r2)
  963. "-l" limit
  964. (if (> (length path) (length (hg-root path)))
  965. (cons path nil)
  966. nil)))
  967. (hg-log-mode)
  968. (cd (hg-root path)))))
  969. (defun hg-log-repo (path &optional rev1 rev2 log-limit)
  970. "Display the revision history of the repository containing PATH.
  971. History is displayed between REV1 and REV2.
  972. Number of displayed changesets is limited to LOG-LIMIT,
  973. REV1 defaults to the tip, while REV2 defaults to 0.
  974. LOG-LIMIT defaults to `hg-log-limit'.
  975. With a prefix argument, prompt for each parameter."
  976. (interactive (list (hg-read-file-name " to log")
  977. (hg-read-rev " to start with"
  978. "tip")
  979. (hg-read-rev " to end with"
  980. "0")
  981. (hg-read-number "Output limited to: "
  982. hg-log-limit)))
  983. (hg-log (hg-root path) rev1 rev2 log-limit))
  984. (defun hg-outgoing (&optional repo)
  985. "Display changesets present locally that are not present in REPO."
  986. (interactive (list (hg-read-repo-name " where changes would go to" nil
  987. hg-outgoing-repository)))
  988. (hg-view-output ((format "Mercurial: Outgoing from %s to %s"
  989. (hg-abbrev-file-name (hg-root))
  990. (hg-abbrev-file-name
  991. (or repo hg-outgoing-repository))))
  992. (call-process (hg-binary) nil t nil "outgoing"
  993. (or repo hg-outgoing-repository))
  994. (hg-log-mode)
  995. (cd (hg-root))))
  996. (defun hg-pull (&optional repo)
  997. "Pull changes from repository REPO.
  998. This does not update the working directory."
  999. (interactive (list (hg-read-repo-name " to pull from")))
  1000. (hg-view-output ((format "Mercurial: Pull to %s from %s"
  1001. (hg-abbrev-file-name (hg-root))
  1002. (hg-abbrev-file-name
  1003. (or repo hg-incoming-repository))))
  1004. (call-process (hg-binary) nil t nil "pull"
  1005. (or repo hg-incoming-repository))
  1006. (cd (hg-root))))
  1007. (defun hg-push (&optional repo)
  1008. "Push changes to repository REPO."
  1009. (interactive (list (hg-read-repo-name " to push to")))
  1010. (hg-view-output ((format "Mercurial: Push from %s to %s"
  1011. (hg-abbrev-file-name (hg-root))
  1012. (hg-abbrev-file-name
  1013. (or repo hg-outgoing-repository))))
  1014. (call-process (hg-binary) nil t nil "push"
  1015. (or repo hg-outgoing-repository))
  1016. (cd (hg-root))))
  1017. (defun hg-revert-buffer-internal ()
  1018. (let ((ctx (hg-buffer-context)))
  1019. (message "Reverting %s..." buffer-file-name)
  1020. (hg-run0 "revert" buffer-file-name)
  1021. (revert-buffer t t t)
  1022. (hg-restore-context ctx)
  1023. (hg-mode-line)
  1024. (message "Reverting %s...done" buffer-file-name)))
  1025. (defun hg-revert-buffer ()
  1026. "Revert current buffer's file back to the latest committed version.
  1027. If the file has not changed, nothing happens. Otherwise, this
  1028. displays a diff and asks for confirmation before reverting."
  1029. (interactive)
  1030. (let ((vc-suppress-confirm nil)
  1031. (obuf (current-buffer))
  1032. diff)
  1033. (vc-buffer-sync)
  1034. (unwind-protect
  1035. (setq diff (hg-diff buffer-file-name))
  1036. (when diff
  1037. (unless (yes-or-no-p "Discard changes? ")
  1038. (error "Revert cancelled")))
  1039. (when diff
  1040. (let ((buf (current-buffer)))
  1041. (delete-window (selected-window))
  1042. (kill-buffer buf))))
  1043. (set-buffer obuf)
  1044. (when diff
  1045. (hg-revert-buffer-internal))))
  1046. (defun hg-root (&optional path)
  1047. "Return the root of the repository that contains the given path.
  1048. If the path is outside a repository, return nil.
  1049. When called interactively, the root is printed. A prefix argument
  1050. prompts for a path to check."
  1051. (interactive (list (hg-read-file-name)))
  1052. (if (or path (not hg-root))
  1053. (let ((root (do ((prev nil dir)
  1054. (dir (file-name-directory
  1055. (or
  1056. path
  1057. buffer-file-name
  1058. (expand-file-name default-directory)))
  1059. (file-name-directory (directory-file-name dir))))
  1060. ((equal prev dir))
  1061. (when (file-directory-p (concat dir ".hg"))
  1062. (return dir)))))
  1063. (when (interactive-p)
  1064. (if root
  1065. (message "The root of this repository is `%s'." root)
  1066. (message "The path `%s' is not in a Mercurial repository."
  1067. (hg-abbrev-file-name path))))
  1068. root)
  1069. hg-root))
  1070. (defun hg-cwd (&optional path)
  1071. "Return the current directory of PATH within the repository."
  1072. (do ((stack nil (cons (file-name-nondirectory
  1073. (directory-file-name dir))
  1074. stack))
  1075. (prev nil dir)
  1076. (dir (file-name-directory (or path buffer-file-name
  1077. (expand-file-name default-directory)))
  1078. (file-name-directory (directory-file-name dir))))
  1079. ((equal prev dir))
  1080. (when (file-directory-p (concat dir ".hg"))
  1081. (let ((cwd (mapconcat 'identity stack "/")))
  1082. (unless (equal cwd "")
  1083. (return (file-name-as-directory cwd)))))))
  1084. (defun hg-status (path)
  1085. "Print revision control status of a file or directory.
  1086. With prefix argument, prompt for the path to give status for.
  1087. Names are displayed relative to the repository root."
  1088. (interactive (list (hg-read-file-name " for status" (hg-root))))
  1089. (let ((root (hg-root)))
  1090. (hg-view-output ((format "Mercurial: Status of %s in %s"
  1091. (let ((name (substring (expand-file-name path)
  1092. (length root))))
  1093. (if (> (length name) 0)
  1094. name
  1095. "*"))
  1096. (hg-abbrev-file-name root)))
  1097. (apply 'call-process (hg-binary) nil t nil
  1098. (list "--cwd" root "status" path))
  1099. (cd (hg-root path)))))
  1100. (defun hg-undo ()
  1101. (interactive)
  1102. (error "not implemented"))
  1103. (defun hg-update ()
  1104. (interactive)
  1105. (error "not implemented"))
  1106. (defun hg-version-other-window (rev)
  1107. "Visit version REV of the current file in another window.
  1108. If the current file is named `F', the version is named `F.~REV~'.
  1109. If `F.~REV~' already exists, use it instead of checking it out again."
  1110. (interactive "sVersion to visit (default is workfile version): ")
  1111. (let* ((file buffer-file-name)
  1112. (version (if (string-equal rev "")
  1113. "tip"
  1114. rev))
  1115. (automatic-backup (vc-version-backup-file-name file version))
  1116. (manual-backup (vc-version-backup-file-name file version 'manual)))
  1117. (unless (file-exists-p manual-backup)
  1118. (if (file-exists-p automatic-backup)
  1119. (rename-file automatic-backup manual-backup nil)
  1120. (hg-run0 "-q" "cat" "-r" version "-o" manual-backup file)))
  1121. (find-file-other-window manual-backup)))
  1122. (provide 'mercurial)
  1123. ;;; Local Variables:
  1124. ;;; prompt-to-byte-compile: nil
  1125. ;;; end: