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