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