##// END OF EJS Templates
mercurial.el: fix misleading indent of code....
Vadim Gelfer -
r2518:d78ae783 default
parent child Browse files
Show More
@@ -1,1179 +1,1179 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 (buffer-file-name) default-directory)))
383 (let ((path (or default (buffer-file-name) default-directory)))
384 (if (or (not path) current-prefix-arg)
384 (if (or (not path) current-prefix-arg)
385 (expand-file-name
385 (expand-file-name
386 (eval (list* 'read-file-name
386 (eval (list* 'read-file-name
387 (format "File, directory or pattern%s: "
387 (format "File, directory or pattern%s: "
388 (or prompt ""))
388 (or prompt ""))
389 (and path (file-name-directory path))
389 (and path (file-name-directory path))
390 nil nil
390 nil nil
391 (and path (file-name-nondirectory path))
391 (and path (file-name-nondirectory path))
392 (if hg-running-xemacs
392 (if hg-running-xemacs
393 (cons (quote 'hg-file-history) nil)
393 (cons (quote 'hg-file-history) nil)
394 nil))))
394 nil))))
395 path))))
395 path))))
396
396
397 (defun hg-read-number (&optional prompt default)
397 (defun hg-read-number (&optional prompt default)
398 "Read a integer value."
398 "Read a integer value."
399 (save-excursion
399 (save-excursion
400 (if (or (not default) current-prefix-arg)
400 (if (or (not default) current-prefix-arg)
401 (string-to-number
401 (string-to-number
402 (eval (list* 'read-string
402 (eval (list* 'read-string
403 (or prompt "")
403 (or prompt "")
404 (if default (cons (format "%d" default) nil) nil))))
404 (if default (cons (format "%d" default) nil) nil))))
405 default)))
405 default)))
406
406
407 (defun hg-read-config ()
407 (defun hg-read-config ()
408 "Return an alist of (key . value) pairs of Mercurial config data.
408 "Return an alist of (key . value) pairs of Mercurial config data.
409 Each key is of the form (section . name)."
409 Each key is of the form (section . name)."
410 (let (items)
410 (let (items)
411 (dolist (line (split-string (hg-chomp (hg-run0 "debugconfig")) "\n") items)
411 (dolist (line (split-string (hg-chomp (hg-run0 "debugconfig")) "\n") items)
412 (string-match "^\\([^=]*\\)=\\(.*\\)" line)
412 (string-match "^\\([^=]*\\)=\\(.*\\)" line)
413 (let* ((left (substring line (match-beginning 1) (match-end 1)))
413 (let* ((left (substring line (match-beginning 1) (match-end 1)))
414 (right (substring line (match-beginning 2) (match-end 2)))
414 (right (substring line (match-beginning 2) (match-end 2)))
415 (key (split-string left "\\."))
415 (key (split-string left "\\."))
416 (value (hg-replace-in-string right "\\\\n" "\n" t)))
416 (value (hg-replace-in-string right "\\\\n" "\n" t)))
417 (setq items (cons (cons (cons (car key) (cadr key)) value) items))))))
417 (setq items (cons (cons (cons (car key) (cadr key)) value) items))))))
418
418
419 (defun hg-config-section (section config)
419 (defun hg-config-section (section config)
420 "Return an alist of (name . value) pairs for SECTION of CONFIG."
420 "Return an alist of (name . value) pairs for SECTION of CONFIG."
421 (let (items)
421 (let (items)
422 (dolist (item config items)
422 (dolist (item config items)
423 (when (equal (caar item) section)
423 (when (equal (caar item) section)
424 (setq items (cons (cons (cdar item) (cdr item)) items))))))
424 (setq items (cons (cons (cdar item) (cdr item)) items))))))
425
425
426 (defun hg-string-starts-with (sub str)
426 (defun hg-string-starts-with (sub str)
427 "Indicate whether string STR starts with the substring or character SUB."
427 "Indicate whether string STR starts with the substring or character SUB."
428 (if (not (stringp sub))
428 (if (not (stringp sub))
429 (and (> (length str) 0) (equal (elt str 0) sub))
429 (and (> (length str) 0) (equal (elt str 0) sub))
430 (let ((sub-len (length sub)))
430 (let ((sub-len (length sub)))
431 (and (<= sub-len (length str))
431 (and (<= sub-len (length str))
432 (string= sub (substring str 0 sub-len))))))
432 (string= sub (substring str 0 sub-len))))))
433
433
434 (defun hg-complete-repo (string predicate all)
434 (defun hg-complete-repo (string predicate all)
435 "Attempt to complete a repository name.
435 "Attempt to complete a repository name.
436 We complete on either symbolic names from Mercurial's config or real
436 We complete on either symbolic names from Mercurial's config or real
437 directory names from the file system. We do not penalise URLs."
437 directory names from the file system. We do not penalise URLs."
438 (or (if all
438 (or (if all
439 (all-completions string hg-repo-completion-table predicate)
439 (all-completions string hg-repo-completion-table predicate)
440 (try-completion string hg-repo-completion-table predicate))
440 (try-completion string hg-repo-completion-table predicate))
441 (let* ((str (expand-file-name string))
441 (let* ((str (expand-file-name string))
442 (dir (file-name-directory str))
442 (dir (file-name-directory str))
443 (file (file-name-nondirectory str)))
443 (file (file-name-nondirectory str)))
444 (if all
444 (if all
445 (let (completions)
445 (let (completions)
446 (dolist (name (delete "./" (file-name-all-completions file dir))
446 (dolist (name (delete "./" (file-name-all-completions file dir))
447 completions)
447 completions)
448 (let ((path (concat dir name)))
448 (let ((path (concat dir name)))
449 (when (file-directory-p path)
449 (when (file-directory-p path)
450 (setq completions (cons name completions))))))
450 (setq completions (cons name completions))))))
451 (let ((comp (file-name-completion file dir)))
451 (let ((comp (file-name-completion file dir)))
452 (if comp
452 (if comp
453 (hg-abbrev-file-name (concat dir comp))))))))
453 (hg-abbrev-file-name (concat dir comp))))))))
454
454
455 (defun hg-read-repo-name (&optional prompt initial-contents default)
455 (defun hg-read-repo-name (&optional prompt initial-contents default)
456 "Read the location of a repository."
456 "Read the location of a repository."
457 (save-excursion
457 (save-excursion
458 (while hg-prev-buffer
458 (while hg-prev-buffer
459 (set-buffer hg-prev-buffer))
459 (set-buffer hg-prev-buffer))
460 (let (hg-repo-completion-table)
460 (let (hg-repo-completion-table)
461 (if current-prefix-arg
461 (if current-prefix-arg
462 (progn
462 (progn
463 (dolist (path (hg-config-section "paths" (hg-read-config)))
463 (dolist (path (hg-config-section "paths" (hg-read-config)))
464 (setq hg-repo-completion-table
464 (setq hg-repo-completion-table
465 (cons (cons (car path) t) hg-repo-completion-table))
465 (cons (cons (car path) t) hg-repo-completion-table))
466 (unless (hg-string-starts-with directory-sep-char (cdr path))
466 (unless (hg-string-starts-with directory-sep-char (cdr path))
467 (setq hg-repo-completion-table
467 (setq hg-repo-completion-table
468 (cons (cons (cdr path) t) hg-repo-completion-table))))
468 (cons (cons (cdr path) t) hg-repo-completion-table))))
469 (completing-read (format "Repository%s: " (or prompt ""))
469 (completing-read (format "Repository%s: " (or prompt ""))
470 'hg-complete-repo
470 'hg-complete-repo
471 nil
471 nil
472 nil
472 nil
473 initial-contents
473 initial-contents
474 'hg-repo-history
474 'hg-repo-history
475 default))
475 default))
476 default))))
476 default))))
477
477
478 (defun hg-read-rev (&optional prompt default)
478 (defun hg-read-rev (&optional prompt default)
479 "Read a revision or tag, offering completions."
479 "Read a revision or tag, offering completions."
480 (save-excursion
480 (save-excursion
481 (while hg-prev-buffer
481 (while hg-prev-buffer
482 (set-buffer hg-prev-buffer))
482 (set-buffer hg-prev-buffer))
483 (let ((rev (or default "tip")))
483 (let ((rev (or default "tip")))
484 (if current-prefix-arg
484 (if current-prefix-arg
485 (let ((revs (split-string
485 (let ((revs (split-string
486 (hg-chomp
486 (hg-chomp
487 (hg-run0 "-q" "log" "-r"
487 (hg-run0 "-q" "log" "-r"
488 (format "-%d:tip" hg-rev-completion-limit)))
488 (format "-%d:tip" hg-rev-completion-limit)))
489 "[\n:]")))
489 "[\n:]")))
490 (dolist (line (split-string (hg-chomp (hg-run0 "tags")) "\n"))
490 (dolist (line (split-string (hg-chomp (hg-run0 "tags")) "\n"))
491 (setq revs (cons (car (split-string line "\\s-")) revs)))
491 (setq revs (cons (car (split-string line "\\s-")) revs)))
492 (completing-read (format "Revision%s (%s): "
492 (completing-read (format "Revision%s (%s): "
493 (or prompt "")
493 (or prompt "")
494 (or default "tip"))
494 (or default "tip"))
495 (map 'list 'cons revs revs)
495 (map 'list 'cons revs revs)
496 nil
496 nil
497 nil
497 nil
498 nil
498 nil
499 'hg-rev-history
499 'hg-rev-history
500 (or default "tip")))
500 (or default "tip")))
501 rev))))
501 rev))))
502
502
503 (defmacro hg-do-across-repo (path &rest body)
503 (defmacro hg-do-across-repo (path &rest body)
504 (let ((root-name (gensym "root-"))
504 (let ((root-name (gensym "root-"))
505 (buf-name (gensym "buf-")))
505 (buf-name (gensym "buf-")))
506 `(let ((,root-name (hg-root ,path)))
506 `(let ((,root-name (hg-root ,path)))
507 (save-excursion
507 (save-excursion
508 (dolist (,buf-name (buffer-list))
508 (dolist (,buf-name (buffer-list))
509 (set-buffer ,buf-name)
509 (set-buffer ,buf-name)
510 (when (and hg-status (equal (hg-root buffer-file-name) ,root-name))
510 (when (and hg-status (equal (hg-root buffer-file-name) ,root-name))
511 ,@body))))))
511 ,@body))))))
512
512
513 (put 'hg-do-across-repo 'lisp-indent-function 1)
513 (put 'hg-do-across-repo 'lisp-indent-function 1)
514
514
515
515
516 ;;; View mode bits.
516 ;;; View mode bits.
517
517
518 (defun hg-exit-view-mode (buf)
518 (defun hg-exit-view-mode (buf)
519 "Exit from hg-view-mode.
519 "Exit from hg-view-mode.
520 We delete the current window if entering hg-view-mode split the
520 We delete the current window if entering hg-view-mode split the
521 current frame."
521 current frame."
522 (when (and (eq buf (current-buffer))
522 (when (and (eq buf (current-buffer))
523 (> (length (window-list)) 1))
523 (> (length (window-list)) 1))
524 (delete-window))
524 (delete-window))
525 (when (buffer-live-p buf)
525 (when (buffer-live-p buf)
526 (kill-buffer buf)))
526 (kill-buffer buf)))
527
527
528 (defun hg-view-mode (prev-buffer &optional file-name)
528 (defun hg-view-mode (prev-buffer &optional file-name)
529 (goto-char (point-min))
529 (goto-char (point-min))
530 (set-buffer-modified-p nil)
530 (set-buffer-modified-p nil)
531 (toggle-read-only t)
531 (toggle-read-only t)
532 (view-minor-mode prev-buffer 'hg-exit-view-mode)
532 (view-minor-mode prev-buffer 'hg-exit-view-mode)
533 (use-local-map hg-view-mode-map)
533 (use-local-map hg-view-mode-map)
534 (setq truncate-lines t)
534 (setq truncate-lines t)
535 (when file-name
535 (when file-name
536 (set (make-local-variable 'hg-view-file-name)
536 (set (make-local-variable 'hg-view-file-name)
537 (hg-abbrev-file-name file-name))))
537 (hg-abbrev-file-name file-name))))
538
538
539 (defun hg-file-status (file)
539 (defun hg-file-status (file)
540 "Return status of FILE, or nil if FILE does not exist or is unmanaged."
540 "Return status of FILE, or nil if FILE does not exist or is unmanaged."
541 (let* ((s (hg-run "status" file))
541 (let* ((s (hg-run "status" file))
542 (exit (car s))
542 (exit (car s))
543 (output (cdr s)))
543 (output (cdr s)))
544 (if (= exit 0)
544 (if (= exit 0)
545 (let ((state (assoc (substring output 0 (min (length output) 2))
545 (let ((state (assoc (substring output 0 (min (length output) 2))
546 '(("M " . modified)
546 '(("M " . modified)
547 ("A " . added)
547 ("A " . added)
548 ("R " . removed)
548 ("R " . removed)
549 ("? " . nil)))))
549 ("? " . nil)))))
550 (if state
550 (if state
551 (cdr state)
551 (cdr state)
552 'normal)))))
552 'normal)))))
553
553
554 (defun hg-tip ()
554 (defun hg-tip ()
555 (split-string (hg-chomp (hg-run0 "-q" "tip")) ":"))
555 (split-string (hg-chomp (hg-run0 "-q" "tip")) ":"))
556
556
557 (defmacro hg-view-output (args &rest body)
557 (defmacro hg-view-output (args &rest body)
558 "Execute BODY in a clean buffer, then quickly display that buffer.
558 "Execute BODY in a clean buffer, then quickly display that buffer.
559 If the buffer contains one line, its contents are displayed in the
559 If the buffer contains one line, its contents are displayed in the
560 minibuffer. Otherwise, the buffer is displayed in view-mode.
560 minibuffer. Otherwise, the buffer is displayed in view-mode.
561 ARGS is of the form (BUFFER-NAME &optional FILE), where BUFFER-NAME is
561 ARGS is of the form (BUFFER-NAME &optional FILE), where BUFFER-NAME is
562 the name of the buffer to create, and FILE is the name of the file
562 the name of the buffer to create, and FILE is the name of the file
563 being viewed."
563 being viewed."
564 (let ((prev-buf (gensym "prev-buf-"))
564 (let ((prev-buf (gensym "prev-buf-"))
565 (v-b-name (car args))
565 (v-b-name (car args))
566 (v-m-rest (cdr args)))
566 (v-m-rest (cdr args)))
567 `(let ((view-buf-name ,v-b-name)
567 `(let ((view-buf-name ,v-b-name)
568 (,prev-buf (current-buffer)))
568 (,prev-buf (current-buffer)))
569 (get-buffer-create view-buf-name)
569 (get-buffer-create view-buf-name)
570 (kill-buffer view-buf-name)
570 (kill-buffer view-buf-name)
571 (get-buffer-create view-buf-name)
571 (get-buffer-create view-buf-name)
572 (set-buffer view-buf-name)
572 (set-buffer view-buf-name)
573 (save-excursion
573 (save-excursion
574 ,@body)
574 ,@body)
575 (case (count-lines (point-min) (point-max))
575 (case (count-lines (point-min) (point-max))
576 ((0)
576 ((0)
577 (kill-buffer view-buf-name)
577 (kill-buffer view-buf-name)
578 (message "(No output)"))
578 (message "(No output)"))
579 ((1)
579 ((1)
580 (let ((msg (hg-chomp (buffer-substring (point-min) (point-max)))))
580 (let ((msg (hg-chomp (buffer-substring (point-min) (point-max)))))
581 (kill-buffer view-buf-name)
581 (kill-buffer view-buf-name)
582 (message "%s" msg)))
582 (message "%s" msg)))
583 (t
583 (t
584 (pop-to-buffer view-buf-name)
584 (pop-to-buffer view-buf-name)
585 (setq hg-prev-buffer ,prev-buf)
585 (setq hg-prev-buffer ,prev-buf)
586 (hg-view-mode ,prev-buf ,@v-m-rest))))))
586 (hg-view-mode ,prev-buf ,@v-m-rest))))))
587
587
588 (put 'hg-view-output 'lisp-indent-function 1)
588 (put 'hg-view-output 'lisp-indent-function 1)
589
589
590 ;;; Context save and restore across revert.
590 ;;; Context save and restore across revert.
591
591
592 (defun hg-position-context (pos)
592 (defun hg-position-context (pos)
593 "Return information to help find the given position again."
593 "Return information to help find the given position again."
594 (let* ((end (min (point-max) (+ pos 98))))
594 (let* ((end (min (point-max) (+ pos 98))))
595 (list pos
595 (list pos
596 (buffer-substring (max (point-min) (- pos 2)) end)
596 (buffer-substring (max (point-min) (- pos 2)) end)
597 (- end pos))))
597 (- end pos))))
598
598
599 (defun hg-buffer-context ()
599 (defun hg-buffer-context ()
600 "Return information to help restore a user's editing context.
600 "Return information to help restore a user's editing context.
601 This is useful across reverts and merges, where a context is likely
601 This is useful across reverts and merges, where a context is likely
602 to have moved a little, but not really changed."
602 to have moved a little, but not really changed."
603 (let ((point-context (hg-position-context (point)))
603 (let ((point-context (hg-position-context (point)))
604 (mark-context (let ((mark (mark-marker)))
604 (mark-context (let ((mark (mark-marker)))
605 (and mark (hg-position-context mark)))))
605 (and mark (hg-position-context mark)))))
606 (list point-context mark-context)))
606 (list point-context mark-context)))
607
607
608 (defun hg-find-context (ctx)
608 (defun hg-find-context (ctx)
609 "Attempt to find a context in the given buffer.
609 "Attempt to find a context in the given buffer.
610 Always returns a valid, hopefully sane, position."
610 Always returns a valid, hopefully sane, position."
611 (let ((pos (nth 0 ctx))
611 (let ((pos (nth 0 ctx))
612 (str (nth 1 ctx))
612 (str (nth 1 ctx))
613 (fixup (nth 2 ctx)))
613 (fixup (nth 2 ctx)))
614 (save-excursion
614 (save-excursion
615 (goto-char (max (point-min) (- pos 15000)))
615 (goto-char (max (point-min) (- pos 15000)))
616 (if (and (not (equal str ""))
616 (if (and (not (equal str ""))
617 (search-forward str nil t))
617 (search-forward str nil t))
618 (- (point) fixup)
618 (- (point) fixup)
619 (max pos (point-min))))))
619 (max pos (point-min))))))
620
620
621 (defun hg-restore-context (ctx)
621 (defun hg-restore-context (ctx)
622 "Attempt to restore the user's editing context."
622 "Attempt to restore the user's editing context."
623 (let ((point-context (nth 0 ctx))
623 (let ((point-context (nth 0 ctx))
624 (mark-context (nth 1 ctx)))
624 (mark-context (nth 1 ctx)))
625 (goto-char (hg-find-context point-context))
625 (goto-char (hg-find-context point-context))
626 (when mark-context
626 (when mark-context
627 (set-mark (hg-find-context mark-context)))))
627 (set-mark (hg-find-context mark-context)))))
628
628
629
629
630 ;;; Hooks.
630 ;;; Hooks.
631
631
632 (defun hg-mode-line (&optional force)
632 (defun hg-mode-line (&optional force)
633 "Update the modeline with the current status of a file.
633 "Update the modeline with the current status of a file.
634 An update occurs if optional argument FORCE is non-nil,
634 An update occurs if optional argument FORCE is non-nil,
635 hg-update-modeline is non-nil, or we have not yet checked the state of
635 hg-update-modeline is non-nil, or we have not yet checked the state of
636 the file."
636 the file."
637 (when (and (hg-root) (or force hg-update-modeline (not hg-mode)))
637 (when (and (hg-root) (or force hg-update-modeline (not hg-mode)))
638 (let ((status (hg-file-status buffer-file-name)))
638 (let ((status (hg-file-status buffer-file-name)))
639 (setq hg-status status
639 (setq hg-status status
640 hg-mode (and status (concat " Hg:"
640 hg-mode (and status (concat " Hg:"
641 (car (hg-tip))
641 (car (hg-tip))
642 (cdr (assq status
642 (cdr (assq status
643 '((normal . "")
643 '((normal . "")
644 (removed . "r")
644 (removed . "r")
645 (added . "a")
645 (added . "a")
646 (modified . "m")))))))
646 (modified . "m")))))))
647 status)))
647 status)))
648
648
649 (defun hg-mode (&optional toggle)
649 (defun hg-mode (&optional toggle)
650 "Minor mode for Mercurial distributed SCM integration.
650 "Minor mode for Mercurial distributed SCM integration.
651
651
652 The Mercurial mode user interface is based on that of VC mode, so if
652 The Mercurial mode user interface is based on that of VC mode, so if
653 you're already familiar with VC, the same keybindings and functions
653 you're already familiar with VC, the same keybindings and functions
654 will generally work.
654 will generally work.
655
655
656 Below is a list of many common SCM tasks. In the list, `G/L\'
656 Below is a list of many common SCM tasks. In the list, `G/L\'
657 indicates whether a key binding is global (G) to a repository or local
657 indicates whether a key binding is global (G) to a repository or local
658 (L) to a file. Many commands take a prefix argument.
658 (L) to a file. Many commands take a prefix argument.
659
659
660 SCM Task G/L Key Binding Command Name
660 SCM Task G/L Key Binding Command Name
661 -------- --- ----------- ------------
661 -------- --- ----------- ------------
662 Help overview (what you are reading) G C-c h h hg-help-overview
662 Help overview (what you are reading) G C-c h h hg-help-overview
663
663
664 Tell Mercurial to manage a file G C-c h a hg-add
664 Tell Mercurial to manage a file G C-c h a hg-add
665 Commit changes to current file only L C-x v n hg-commit-start
665 Commit changes to current file only L C-x v n hg-commit-start
666 Undo changes to file since commit L C-x v u hg-revert-buffer
666 Undo changes to file since commit L C-x v u hg-revert-buffer
667
667
668 Diff file vs last checkin L C-x v = hg-diff
668 Diff file vs last checkin L C-x v = hg-diff
669
669
670 View file change history L C-x v l hg-log
670 View file change history L C-x v l hg-log
671 View annotated file L C-x v a hg-annotate
671 View annotated file L C-x v a hg-annotate
672
672
673 Diff repo vs last checkin G C-c h = hg-diff-repo
673 Diff repo vs last checkin G C-c h = hg-diff-repo
674 View status of files in repo G C-c h s hg-status
674 View status of files in repo G C-c h s hg-status
675 Commit all changes G C-c h c hg-commit-start
675 Commit all changes G C-c h c hg-commit-start
676
676
677 Undo all changes since last commit G C-c h U hg-revert
677 Undo all changes since last commit G C-c h U hg-revert
678 View repo change history G C-c h l hg-log-repo
678 View repo change history G C-c h l hg-log-repo
679
679
680 See changes that can be pulled G C-c h , hg-incoming
680 See changes that can be pulled G C-c h , hg-incoming
681 Pull changes G C-c h < hg-pull
681 Pull changes G C-c h < hg-pull
682 Update working directory after pull G C-c h u hg-update
682 Update working directory after pull G C-c h u hg-update
683 See changes that can be pushed G C-c h . hg-outgoing
683 See changes that can be pushed G C-c h . hg-outgoing
684 Push changes G C-c h > hg-push"
684 Push changes G C-c h > hg-push"
685 (unless vc-make-backup-files
685 (unless vc-make-backup-files
686 (set (make-local-variable 'backup-inhibited) t))
686 (set (make-local-variable 'backup-inhibited) t))
687 (run-hooks 'hg-mode-hook))
687 (run-hooks 'hg-mode-hook))
688
688
689 (defun hg-find-file-hook ()
689 (defun hg-find-file-hook ()
690 (when (hg-mode-line)
690 (when (hg-mode-line)
691 (hg-mode)))
691 (hg-mode)))
692
692
693 (add-hook 'find-file-hooks 'hg-find-file-hook)
693 (add-hook 'find-file-hooks 'hg-find-file-hook)
694
694
695 (defun hg-after-save-hook ()
695 (defun hg-after-save-hook ()
696 (let ((old-status hg-status))
696 (let ((old-status hg-status))
697 (hg-mode-line)
697 (hg-mode-line)
698 (if (and (not old-status) hg-status)
698 (if (and (not old-status) hg-status)
699 (hg-mode))))
699 (hg-mode))))
700
700
701 (add-hook 'after-save-hook 'hg-after-save-hook)
701 (add-hook 'after-save-hook 'hg-after-save-hook)
702
702
703
703
704 ;;; User interface functions.
704 ;;; User interface functions.
705
705
706 (defun hg-help-overview ()
706 (defun hg-help-overview ()
707 "This is an overview of the Mercurial SCM mode for Emacs.
707 "This is an overview of the Mercurial SCM mode for Emacs.
708
708
709 You can find the source code, license (GPL v2), and credits for this
709 You can find the source code, license (GPL v2), and credits for this
710 code by typing `M-x find-library mercurial RET'."
710 code by typing `M-x find-library mercurial RET'."
711 (interactive)
711 (interactive)
712 (hg-view-output ("Mercurial Help Overview")
712 (hg-view-output ("Mercurial Help Overview")
713 (insert (documentation 'hg-help-overview))
713 (insert (documentation 'hg-help-overview))
714 (let ((pos (point)))
714 (let ((pos (point)))
715 (insert (documentation 'hg-mode))
715 (insert (documentation 'hg-mode))
716 (goto-char pos)
716 (goto-char pos)
717 (end-of-line 1)
717 (end-of-line 1)
718 (delete-region pos (point)))
718 (delete-region pos (point)))
719 (cd (hg-root))))
719 (cd (hg-root))))
720
720
721 (defun hg-add (path)
721 (defun hg-add (path)
722 "Add PATH to the Mercurial repository on the next commit.
722 "Add PATH to the Mercurial repository on the next commit.
723 With a prefix argument, prompt for the path to add."
723 With a prefix argument, prompt for the path to add."
724 (interactive (list (hg-read-file-name " to add")))
724 (interactive (list (hg-read-file-name " to add")))
725 (let ((buf (current-buffer))
725 (let ((buf (current-buffer))
726 (update (equal buffer-file-name path)))
726 (update (equal buffer-file-name path)))
727 (hg-view-output (hg-output-buffer-name)
727 (hg-view-output (hg-output-buffer-name)
728 (apply 'call-process (hg-binary) nil t nil (list "add" path))
728 (apply 'call-process (hg-binary) nil t nil (list "add" path))
729 ;; "hg add" shows pathes relative NOT TO ROOT BUT TO REPOSITORY
729 ;; "hg add" shows pathes relative NOT TO ROOT BUT TO REPOSITORY
730 (replace-regexp " \\.\\.." " " nil 0 (buffer-size))
730 (replace-regexp " \\.\\.." " " nil 0 (buffer-size))
731 (goto-char 0)
731 (goto-char 0)
732 (cd (hg-root path)))
732 (cd (hg-root path)))
733 (when update
733 (when update
734 (unless vc-make-backup-files
734 (unless vc-make-backup-files
735 (set (make-local-variable 'backup-inhibited) t))
735 (set (make-local-variable 'backup-inhibited) t))
736 (with-current-buffer buf
736 (with-current-buffer buf
737 (hg-mode-line)))))
737 (hg-mode-line)))))
738
738
739 (defun hg-addremove ()
739 (defun hg-addremove ()
740 (interactive)
740 (interactive)
741 (error "not implemented"))
741 (error "not implemented"))
742
742
743 (defun hg-annotate ()
743 (defun hg-annotate ()
744 (interactive)
744 (interactive)
745 (error "not implemented"))
745 (error "not implemented"))
746
746
747 (defun hg-commit-toggle-file (pos)
747 (defun hg-commit-toggle-file (pos)
748 "Toggle whether or not the file at POS will be committed."
748 "Toggle whether or not the file at POS will be committed."
749 (interactive "d")
749 (interactive "d")
750 (save-excursion
750 (save-excursion
751 (goto-char pos)
751 (goto-char pos)
752 (let ((face (get-text-property pos 'face))
752 (let ((face (get-text-property pos 'face))
753 (inhibit-read-only t)
753 (inhibit-read-only t)
754 bol)
754 bol)
755 (beginning-of-line)
755 (beginning-of-line)
756 (setq bol (+ (point) 4))
756 (setq bol (+ (point) 4))
757 (end-of-line)
757 (end-of-line)
758 (if (eq face 'bold)
758 (if (eq face 'bold)
759 (progn
759 (progn
760 (remove-text-properties bol (point) '(face nil))
760 (remove-text-properties bol (point) '(face nil))
761 (message "%s will not be committed"
761 (message "%s will not be committed"
762 (buffer-substring bol (point))))
762 (buffer-substring bol (point))))
763 (add-text-properties bol (point) '(face bold))
763 (add-text-properties bol (point) '(face bold))
764 (message "%s will be committed"
764 (message "%s will be committed"
765 (buffer-substring bol (point)))))))
765 (buffer-substring bol (point)))))))
766
766
767 (defun hg-commit-mouse-clicked (event)
767 (defun hg-commit-mouse-clicked (event)
768 "Toggle whether or not the file at POS will be committed."
768 "Toggle whether or not the file at POS will be committed."
769 (interactive "@e")
769 (interactive "@e")
770 (hg-commit-toggle-file (hg-event-point event)))
770 (hg-commit-toggle-file (hg-event-point event)))
771
771
772 (defun hg-commit-kill ()
772 (defun hg-commit-kill ()
773 "Kill the commit currently being prepared."
773 "Kill the commit currently being prepared."
774 (interactive)
774 (interactive)
775 (when (or (not (buffer-modified-p)) (y-or-n-p "Really kill this commit? "))
775 (when (or (not (buffer-modified-p)) (y-or-n-p "Really kill this commit? "))
776 (let ((buf hg-prev-buffer))
776 (let ((buf hg-prev-buffer))
777 (kill-buffer nil)
777 (kill-buffer nil)
778 (switch-to-buffer buf))))
778 (switch-to-buffer buf))))
779
779
780 (defun hg-commit-finish ()
780 (defun hg-commit-finish ()
781 "Finish preparing a commit, and perform the actual commit.
781 "Finish preparing a commit, and perform the actual commit.
782 The hook hg-pre-commit-hook is run before anything else is done. If
782 The hook hg-pre-commit-hook is run before anything else is done. If
783 the commit message is empty and hg-commit-allow-empty-message is nil,
783 the commit message is empty and hg-commit-allow-empty-message is nil,
784 an error is raised. If the list of files to commit is empty and
784 an error is raised. If the list of files to commit is empty and
785 hg-commit-allow-empty-file-list is nil, an error is raised."
785 hg-commit-allow-empty-file-list is nil, an error is raised."
786 (interactive)
786 (interactive)
787 (let ((root hg-root))
787 (let ((root hg-root))
788 (save-excursion
788 (save-excursion
789 (run-hooks 'hg-pre-commit-hook)
789 (run-hooks 'hg-pre-commit-hook)
790 (goto-char (point-min))
790 (goto-char (point-min))
791 (search-forward hg-commit-message-start)
791 (search-forward hg-commit-message-start)
792 (let (message files)
792 (let (message files)
793 (let ((start (point)))
793 (let ((start (point)))
794 (goto-char (point-max))
794 (goto-char (point-max))
795 (search-backward hg-commit-message-end)
795 (search-backward hg-commit-message-end)
796 (setq message (hg-strip (buffer-substring start (point)))))
796 (setq message (hg-strip (buffer-substring start (point)))))
797 (when (and (= (length message) 0)
797 (when (and (= (length message) 0)
798 (not hg-commit-allow-empty-message))
798 (not hg-commit-allow-empty-message))
799 (error "Cannot proceed - commit message is empty"))
799 (error "Cannot proceed - commit message is empty"))
800 (forward-line 1)
800 (forward-line 1)
801 (beginning-of-line)
801 (beginning-of-line)
802 (while (< (point) (point-max))
802 (while (< (point) (point-max))
803 (let ((pos (+ (point) 4)))
803 (let ((pos (+ (point) 4)))
804 (end-of-line)
804 (end-of-line)
805 (when (eq (get-text-property pos 'face) 'bold)
805 (when (eq (get-text-property pos 'face) 'bold)
806 (end-of-line)
806 (end-of-line)
807 (setq files (cons (buffer-substring pos (point)) files))))
807 (setq files (cons (buffer-substring pos (point)) files))))
808 (forward-line 1))
808 (forward-line 1))
809 (when (and (= (length files) 0)
809 (when (and (= (length files) 0)
810 (not hg-commit-allow-empty-file-list))
810 (not hg-commit-allow-empty-file-list))
811 (error "Cannot proceed - no files to commit"))
811 (error "Cannot proceed - no files to commit"))
812 (setq message (concat message "\n"))
812 (setq message (concat message "\n"))
813 (apply 'hg-run0 "--cwd" hg-root "commit" "-m" message files))
813 (apply 'hg-run0 "--cwd" hg-root "commit" "-m" message files))
814 (let ((buf hg-prev-buffer))
814 (let ((buf hg-prev-buffer))
815 (kill-buffer nil)
815 (kill-buffer nil)
816 (switch-to-buffer buf))
816 (switch-to-buffer buf))
817 (hg-do-across-repo root
817 (hg-do-across-repo root
818 (hg-mode-line)))))
818 (hg-mode-line)))))
819
819
820 (defun hg-commit-mode ()
820 (defun hg-commit-mode ()
821 "Mode for describing a commit of changes to a Mercurial repository.
821 "Mode for describing a commit of changes to a Mercurial repository.
822 This involves two actions: describing the changes with a commit
822 This involves two actions: describing the changes with a commit
823 message, and choosing the files to commit.
823 message, and choosing the files to commit.
824
824
825 To describe the commit, simply type some text in the designated area.
825 To describe the commit, simply type some text in the designated area.
826
826
827 By default, all modified, added and removed files are selected for
827 By default, all modified, added and removed files are selected for
828 committing. Files that will be committed are displayed in bold face\;
828 committing. Files that will be committed are displayed in bold face\;
829 those that will not are displayed in normal face.
829 those that will not are displayed in normal face.
830
830
831 To toggle whether a file will be committed, move the cursor over a
831 To toggle whether a file will be committed, move the cursor over a
832 particular file and hit space or return. Alternatively, middle click
832 particular file and hit space or return. Alternatively, middle click
833 on the file.
833 on the file.
834
834
835 Key bindings
835 Key bindings
836 ------------
836 ------------
837 \\[hg-commit-finish] proceed with commit
837 \\[hg-commit-finish] proceed with commit
838 \\[hg-commit-kill] kill commit
838 \\[hg-commit-kill] kill commit
839
839
840 \\[hg-diff-repo] view diff of pending changes"
840 \\[hg-diff-repo] view diff of pending changes"
841 (interactive)
841 (interactive)
842 (use-local-map hg-commit-mode-map)
842 (use-local-map hg-commit-mode-map)
843 (set-syntax-table text-mode-syntax-table)
843 (set-syntax-table text-mode-syntax-table)
844 (setq local-abbrev-table text-mode-abbrev-table
844 (setq local-abbrev-table text-mode-abbrev-table
845 major-mode 'hg-commit-mode
845 major-mode 'hg-commit-mode
846 mode-name "Hg-Commit")
846 mode-name "Hg-Commit")
847 (set-buffer-modified-p nil)
847 (set-buffer-modified-p nil)
848 (setq buffer-undo-list nil)
848 (setq buffer-undo-list nil)
849 (run-hooks 'text-mode-hook 'hg-commit-mode-hook))
849 (run-hooks 'text-mode-hook 'hg-commit-mode-hook))
850
850
851 (defun hg-commit-start ()
851 (defun hg-commit-start ()
852 "Prepare a commit of changes to the repository containing the current file."
852 "Prepare a commit of changes to the repository containing the current file."
853 (interactive)
853 (interactive)
854 (while hg-prev-buffer
854 (while hg-prev-buffer
855 (set-buffer hg-prev-buffer))
855 (set-buffer hg-prev-buffer))
856 (let ((root (hg-root))
856 (let ((root (hg-root))
857 (prev-buffer (current-buffer))
857 (prev-buffer (current-buffer))
858 modified-files)
858 modified-files)
859 (unless root
859 (unless root
860 (error "Cannot commit outside a repository!"))
860 (error "Cannot commit outside a repository!"))
861 (hg-sync-buffers root)
861 (hg-sync-buffers root)
862 (setq modified-files (hg-chomp (hg-run0 "--cwd" root "status" "-arm")))
862 (setq modified-files (hg-chomp (hg-run0 "--cwd" root "status" "-arm")))
863 (when (and (= (length modified-files) 0)
863 (when (and (= (length modified-files) 0)
864 (not hg-commit-allow-empty-file-list))
864 (not hg-commit-allow-empty-file-list))
865 (error "No pending changes to commit"))
865 (error "No pending changes to commit"))
866 (let* ((buf-name (format "*Mercurial: Commit %s*" root)))
866 (let* ((buf-name (format "*Mercurial: Commit %s*" root)))
867 (pop-to-buffer (get-buffer-create buf-name))
867 (pop-to-buffer (get-buffer-create buf-name))
868 (when (= (point-min) (point-max))
868 (when (= (point-min) (point-max))
869 (set (make-local-variable 'hg-root) root)
869 (set (make-local-variable 'hg-root) root)
870 (setq hg-prev-buffer prev-buffer)
870 (setq hg-prev-buffer prev-buffer)
871 (insert "\n")
871 (insert "\n")
872 (let ((bol (point)))
872 (let ((bol (point)))
873 (insert hg-commit-message-end)
873 (insert hg-commit-message-end)
874 (add-text-properties bol (point) '(face bold-italic)))
874 (add-text-properties bol (point) '(face bold-italic)))
875 (let ((file-area (point)))
875 (let ((file-area (point)))
876 (insert modified-files)
876 (insert modified-files)
877 (goto-char file-area)
877 (goto-char file-area)
878 (while (< (point) (point-max))
878 (while (< (point) (point-max))
879 (let ((bol (point)))
879 (let ((bol (point)))
880 (forward-char 1)
880 (forward-char 1)
881 (insert " ")
881 (insert " ")
882 (end-of-line)
882 (end-of-line)
883 (add-text-properties (+ bol 4) (point)
883 (add-text-properties (+ bol 4) (point)
884 '(face bold mouse-face highlight)))
884 '(face bold mouse-face highlight)))
885 (forward-line 1))
885 (forward-line 1))
886 (goto-char file-area)
886 (goto-char file-area)
887 (add-text-properties (point) (point-max)
887 (add-text-properties (point) (point-max)
888 `(keymap ,hg-commit-mode-file-map))
888 `(keymap ,hg-commit-mode-file-map))
889 (goto-char (point-min))
889 (goto-char (point-min))
890 (insert hg-commit-message-start)
890 (insert hg-commit-message-start)
891 (add-text-properties (point-min) (point) '(face bold-italic))
891 (add-text-properties (point-min) (point) '(face bold-italic))
892 (insert "\n\n")
892 (insert "\n\n")
893 (forward-line -1)
893 (forward-line -1)
894 (save-excursion
894 (save-excursion
895 (goto-char (point-max))
895 (goto-char (point-max))
896 (search-backward hg-commit-message-end)
896 (search-backward hg-commit-message-end)
897 (add-text-properties (match-beginning 0) (point-max)
897 (add-text-properties (match-beginning 0) (point-max)
898 '(read-only t))
898 '(read-only t))
899 (goto-char (point-min))
899 (goto-char (point-min))
900 (search-forward hg-commit-message-start)
900 (search-forward hg-commit-message-start)
901 (add-text-properties (match-beginning 0) (match-end 0)
901 (add-text-properties (match-beginning 0) (match-end 0)
902 '(read-only t)))
902 '(read-only t)))
903 (hg-commit-mode)
903 (hg-commit-mode)
904 (cd root))))))
904 (cd root))))))
905
905
906 (defun hg-diff (path &optional rev1 rev2)
906 (defun hg-diff (path &optional rev1 rev2)
907 "Show the differences between REV1 and REV2 of PATH.
907 "Show the differences between REV1 and REV2 of PATH.
908 When called interactively, the default behaviour is to treat REV1 as
908 When called interactively, the default behaviour is to treat REV1 as
909 the \"parent\" revision, REV2 as the current edited version of the file, and
909 the \"parent\" revision, REV2 as the current edited version of the file, and
910 PATH as the file edited in the current buffer.
910 PATH as the file edited in the current buffer.
911 With a prefix argument, prompt for all of these."
911 With a prefix argument, prompt for all of these."
912 (interactive (list (hg-read-file-name " to diff")
912 (interactive (list (hg-read-file-name " to diff")
913 (let ((rev1 (hg-read-rev " to start with" 'parent)))
913 (let ((rev1 (hg-read-rev " to start with" 'parent)))
914 (and (not (eq rev1 'parent)) rev1))
914 (and (not (eq rev1 'parent)) rev1))
915 (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
915 (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
916 (and (not (eq rev2 'working-dir)) rev2))))
916 (and (not (eq rev2 'working-dir)) rev2))))
917 (hg-sync-buffers path)
917 (hg-sync-buffers path)
918 (let ((a-path (hg-abbrev-file-name path))
918 (let ((a-path (hg-abbrev-file-name path))
919 ;; none revision is specified explicitly
919 ;; none revision is specified explicitly
920 (none (and (not rev1) (not rev2)))
920 (none (and (not rev1) (not rev2)))
921 ;; only one revision is specified explicitly
921 ;; only one revision is specified explicitly
922 (one (or (and (or (equal rev1 rev2) (not rev2)) rev1)
922 (one (or (and (or (equal rev1 rev2) (not rev2)) rev1)
923 (and (not rev1) rev2)))
923 (and (not rev1) rev2)))
924 diff)
924 diff)
925 (hg-view-output ((cond
925 (hg-view-output ((cond
926 (none
926 (none
927 (format "Mercurial: Diff against parent of %s" a-path))
927 (format "Mercurial: Diff against parent of %s" a-path))
928 (one
928 (one
929 (format "Mercurial: Diff of rev %s of %s" one a-path))
929 (format "Mercurial: Diff of rev %s of %s" one a-path))
930 (t
930 (t
931 (format "Mercurial: Diff from rev %s to %s of %s"
931 (format "Mercurial: Diff from rev %s to %s of %s"
932 rev1 rev2 a-path))))
932 rev1 rev2 a-path))))
933 (cond
933 (cond
934 (none
934 (none
935 (call-process (hg-binary) nil t nil "diff" path))
935 (call-process (hg-binary) nil t nil "diff" path))
936 (one
936 (one
937 (call-process (hg-binary) nil t nil "diff" "-r" one path))
937 (call-process (hg-binary) nil t nil "diff" "-r" one path))
938 (t
938 (t
939 (call-process (hg-binary) nil t nil "diff" "-r" rev1 "-r" rev2 path)))
939 (call-process (hg-binary) nil t nil "diff" "-r" rev1 "-r" rev2 path)))
940 (diff-mode)
940 (diff-mode)
941 (setq diff (not (= (point-min) (point-max))))
941 (setq diff (not (= (point-min) (point-max))))
942 (font-lock-fontify-buffer)
942 (font-lock-fontify-buffer)
943 (cd (hg-root path)))
943 (cd (hg-root path)))
944 diff))
944 diff))
945
945
946 (defun hg-diff-repo (path &optional rev1 rev2)
946 (defun hg-diff-repo (path &optional rev1 rev2)
947 "Show the differences between REV1 and REV2 of repository containing PATH.
947 "Show the differences between REV1 and REV2 of repository containing PATH.
948 When called interactively, the default behaviour is to treat REV1 as
948 When called interactively, the default behaviour is to treat REV1 as
949 the \"parent\" revision, REV2 as the current edited version of the file, and
949 the \"parent\" revision, REV2 as the current edited version of the file, and
950 PATH as the `hg-root' of the current buffer.
950 PATH as the `hg-root' of the current buffer.
951 With a prefix argument, prompt for all of these."
951 With a prefix argument, prompt for all of these."
952 (interactive (list (hg-read-file-name " to diff")
952 (interactive (list (hg-read-file-name " to diff")
953 (let ((rev1 (hg-read-rev " to start with" 'parent)))
953 (let ((rev1 (hg-read-rev " to start with" 'parent)))
954 (and (not (eq rev1 'parent)) rev1))
954 (and (not (eq rev1 'parent)) rev1))
955 (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
955 (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
956 (and (not (eq rev2 'working-dir)) rev2))))
956 (and (not (eq rev2 'working-dir)) rev2))))
957 (hg-diff (hg-root path) rev1 rev2))
957 (hg-diff (hg-root path) rev1 rev2))
958
958
959 (defun hg-forget (path)
959 (defun hg-forget (path)
960 "Lose track of PATH, which has been added, but not yet committed.
960 "Lose track of PATH, which has been added, but not yet committed.
961 This will prevent the file from being incorporated into the Mercurial
961 This will prevent the file from being incorporated into the Mercurial
962 repository on the next commit.
962 repository on the next commit.
963 With a prefix argument, prompt for the path to forget."
963 With a prefix argument, prompt for the path to forget."
964 (interactive (list (hg-read-file-name " to forget")))
964 (interactive (list (hg-read-file-name " to forget")))
965 (let ((buf (current-buffer))
965 (let ((buf (current-buffer))
966 (update (equal buffer-file-name path)))
966 (update (equal buffer-file-name path)))
967 (hg-view-output (hg-output-buffer-name)
967 (hg-view-output (hg-output-buffer-name)
968 (apply 'call-process (hg-binary) nil t nil (list "forget" path))
968 (apply 'call-process (hg-binary) nil t nil (list "forget" path))
969 ;; "hg forget" shows pathes relative NOT TO ROOT BUT TO REPOSITORY
969 ;; "hg forget" shows pathes relative NOT TO ROOT BUT TO REPOSITORY
970 (replace-regexp " \\.\\.." " " nil 0 (buffer-size))
970 (replace-regexp " \\.\\.." " " nil 0 (buffer-size))
971 (goto-char 0)
971 (goto-char 0)
972 (cd (hg-root path)))
972 (cd (hg-root path)))
973 (when update
973 (when update
974 (with-current-buffer buf
974 (with-current-buffer buf
975 (set (make-local-variable 'backup-inhibited) nil)
975 (set (make-local-variable 'backup-inhibited) nil)
976 (hg-mode-line)))))
976 (hg-mode-line)))))
977
977
978 (defun hg-incoming (&optional repo)
978 (defun hg-incoming (&optional repo)
979 "Display changesets present in REPO that are not present locally."
979 "Display changesets present in REPO that are not present locally."
980 (interactive (list (hg-read-repo-name " where changes would come from")))
980 (interactive (list (hg-read-repo-name " where changes would come from")))
981 (hg-view-output ((format "Mercurial: Incoming from %s to %s"
981 (hg-view-output ((format "Mercurial: Incoming from %s to %s"
982 (hg-abbrev-file-name (hg-root))
982 (hg-abbrev-file-name (hg-root))
983 (hg-abbrev-file-name
983 (hg-abbrev-file-name
984 (or repo hg-incoming-repository))))
984 (or repo hg-incoming-repository))))
985 (call-process (hg-binary) nil t nil "incoming"
985 (call-process (hg-binary) nil t nil "incoming"
986 (or repo hg-incoming-repository))
986 (or repo hg-incoming-repository))
987 (hg-log-mode)
987 (hg-log-mode)
988 (cd (hg-root))))
988 (cd (hg-root))))
989
989
990 (defun hg-init ()
990 (defun hg-init ()
991 (interactive)
991 (interactive)
992 (error "not implemented"))
992 (error "not implemented"))
993
993
994 (defun hg-log-mode ()
994 (defun hg-log-mode ()
995 "Mode for viewing a Mercurial change log."
995 "Mode for viewing a Mercurial change log."
996 (goto-char (point-min))
996 (goto-char (point-min))
997 (when (looking-at "^searching for changes.*$")
997 (when (looking-at "^searching for changes.*$")
998 (delete-region (match-beginning 0) (match-end 0)))
998 (delete-region (match-beginning 0) (match-end 0)))
999 (run-hooks 'hg-log-mode-hook))
999 (run-hooks 'hg-log-mode-hook))
1000
1000
1001 (defun hg-log (path &optional rev1 rev2 log-limit)
1001 (defun hg-log (path &optional rev1 rev2 log-limit)
1002 "Display the revision history of PATH.
1002 "Display the revision history of PATH.
1003 History is displayed between REV1 and REV2.
1003 History is displayed between REV1 and REV2.
1004 Number of displayed changesets is limited to LOG-LIMIT.
1004 Number of displayed changesets is limited to LOG-LIMIT.
1005 REV1 defaults to the tip, while
1005 REV1 defaults to the tip, while
1006 REV2 defaults to `hg-rev-completion-limit' changes from the tip revision.
1006 REV2 defaults to `hg-rev-completion-limit' changes from the tip revision.
1007 LOG-LIMIT defaults to `hg-log-limit'.
1007 LOG-LIMIT defaults to `hg-log-limit'.
1008 With a prefix argument, prompt for each parameter."
1008 With a prefix argument, prompt for each parameter."
1009 (interactive (list (hg-read-file-name " to log")
1009 (interactive (list (hg-read-file-name " to log")
1010 (hg-read-rev " to start with"
1010 (hg-read-rev " to start with"
1011 "tip")
1011 "tip")
1012 (hg-read-rev " to end with"
1012 (hg-read-rev " to end with"
1013 (format "%d" (- hg-rev-completion-limit)))
1013 (format "%d" (- hg-rev-completion-limit)))
1014 (hg-read-number "Output limited to: "
1014 (hg-read-number "Output limited to: "
1015 hg-log-limit)))
1015 hg-log-limit)))
1016 (let ((a-path (hg-abbrev-file-name path))
1016 (let ((a-path (hg-abbrev-file-name path))
1017 (r1 (or rev1 (format "-%d" hg-rev-completion-limit)))
1017 (r1 (or rev1 (format "-%d" hg-rev-completion-limit)))
1018 (r2 (or rev2 rev1 "tip"))
1018 (r2 (or rev2 rev1 "tip"))
1019 (limit (format "%d" (or log-limit hg-log-limit))))
1019 (limit (format "%d" (or log-limit hg-log-limit))))
1020 (hg-view-output ((if (equal r1 r2)
1020 (hg-view-output ((if (equal r1 r2)
1021 (format "Mercurial: Log of rev %s of %s" rev1 a-path)
1021 (format "Mercurial: Log of rev %s of %s" rev1 a-path)
1022 (format
1022 (format
1023 "Mercurial: at most %s log(s) from rev %s to %s of %s"
1023 "Mercurial: at most %s log(s) from rev %s to %s of %s"
1024 limit r1 r2 a-path)))
1024 limit r1 r2 a-path)))
1025 (eval (list* 'call-process (hg-binary) nil t nil
1025 (eval (list* 'call-process (hg-binary) nil t nil
1026 "log"
1026 "log"
1027 "-r" (format "%s:%s" r1 r2)
1027 "-r" (format "%s:%s" r1 r2)
1028 "-l" limit
1028 "-l" limit
1029 (if (> (length path) (length (hg-root path)))
1029 (if (> (length path) (length (hg-root path)))
1030 (cons path nil)
1030 (cons path nil)
1031 nil)))
1031 nil)))
1032 (hg-log-mode)
1032 (hg-log-mode)
1033 (cd (hg-root path)))))
1033 (cd (hg-root path)))))
1034
1034
1035 (defun hg-log-repo (path &optional rev1 rev2 log-limit)
1035 (defun hg-log-repo (path &optional rev1 rev2 log-limit)
1036 "Display the revision history of the repository containing PATH.
1036 "Display the revision history of the repository containing PATH.
1037 History is displayed between REV1 and REV2.
1037 History is displayed between REV1 and REV2.
1038 Number of displayed changesets is limited to LOG-LIMIT,
1038 Number of displayed changesets is limited to LOG-LIMIT,
1039 REV1 defaults to the tip, while
1039 REV1 defaults to the tip, while
1040 REV2 defaults to `hg-rev-completion-limit' changes from the tip revision.
1040 REV2 defaults to `hg-rev-completion-limit' changes from the tip revision.
1041 LOG-LIMIT defaults to `hg-log-limit'.
1041 LOG-LIMIT defaults to `hg-log-limit'.
1042 With a prefix argument, prompt for each parameter."
1042 With a prefix argument, prompt for each parameter."
1043 (interactive (list (hg-read-file-name " to log")
1043 (interactive (list (hg-read-file-name " to log")
1044 (hg-read-rev " to start with"
1044 (hg-read-rev " to start with"
1045 "tip")
1045 "tip")
1046 (hg-read-rev " to end with"
1046 (hg-read-rev " to end with"
1047 (format "%d" (- hg-rev-completion-limit)))
1047 (format "%d" (- hg-rev-completion-limit)))
1048 (hg-read-number "Output limited to: "
1048 (hg-read-number "Output limited to: "
1049 hg-log-limit)))
1049 hg-log-limit)))
1050 (hg-log (hg-root path) rev1 rev2 log-limit))
1050 (hg-log (hg-root path) rev1 rev2 log-limit))
1051
1051
1052 (defun hg-outgoing (&optional repo)
1052 (defun hg-outgoing (&optional repo)
1053 "Display changesets present locally that are not present in REPO."
1053 "Display changesets present locally that are not present in REPO."
1054 (interactive (list (hg-read-repo-name " where changes would go to" nil
1054 (interactive (list (hg-read-repo-name " where changes would go to" nil
1055 hg-outgoing-repository)))
1055 hg-outgoing-repository)))
1056 (hg-view-output ((format "Mercurial: Outgoing from %s to %s"
1056 (hg-view-output ((format "Mercurial: Outgoing from %s to %s"
1057 (hg-abbrev-file-name (hg-root))
1057 (hg-abbrev-file-name (hg-root))
1058 (hg-abbrev-file-name
1058 (hg-abbrev-file-name
1059 (or repo hg-outgoing-repository))))
1059 (or repo hg-outgoing-repository))))
1060 (call-process (hg-binary) nil t nil "outgoing"
1060 (call-process (hg-binary) nil t nil "outgoing"
1061 (or repo hg-outgoing-repository))
1061 (or repo hg-outgoing-repository))
1062 (hg-log-mode)
1062 (hg-log-mode)
1063 (cd (hg-root))))
1063 (cd (hg-root))))
1064
1064
1065 (defun hg-pull (&optional repo)
1065 (defun hg-pull (&optional repo)
1066 "Pull changes from repository REPO.
1066 "Pull changes from repository REPO.
1067 This does not update the working directory."
1067 This does not update the working directory."
1068 (interactive (list (hg-read-repo-name " to pull from")))
1068 (interactive (list (hg-read-repo-name " to pull from")))
1069 (hg-view-output ((format "Mercurial: Pull to %s from %s"
1069 (hg-view-output ((format "Mercurial: Pull to %s from %s"
1070 (hg-abbrev-file-name (hg-root))
1070 (hg-abbrev-file-name (hg-root))
1071 (hg-abbrev-file-name
1071 (hg-abbrev-file-name
1072 (or repo hg-incoming-repository))))
1072 (or repo hg-incoming-repository))))
1073 (call-process (hg-binary) nil t nil "pull"
1073 (call-process (hg-binary) nil t nil "pull"
1074 (or repo hg-incoming-repository))
1074 (or repo hg-incoming-repository))
1075 (cd (hg-root))))
1075 (cd (hg-root))))
1076
1076
1077 (defun hg-push (&optional repo)
1077 (defun hg-push (&optional repo)
1078 "Push changes to repository REPO."
1078 "Push changes to repository REPO."
1079 (interactive (list (hg-read-repo-name " to push to")))
1079 (interactive (list (hg-read-repo-name " to push to")))
1080 (hg-view-output ((format "Mercurial: Push from %s to %s"
1080 (hg-view-output ((format "Mercurial: Push from %s to %s"
1081 (hg-abbrev-file-name (hg-root))
1081 (hg-abbrev-file-name (hg-root))
1082 (hg-abbrev-file-name
1082 (hg-abbrev-file-name
1083 (or repo hg-outgoing-repository))))
1083 (or repo hg-outgoing-repository))))
1084 (call-process (hg-binary) nil t nil "push"
1084 (call-process (hg-binary) nil t nil "push"
1085 (or repo hg-outgoing-repository))
1085 (or repo hg-outgoing-repository))
1086 (cd (hg-root))))
1086 (cd (hg-root))))
1087
1087
1088 (defun hg-revert-buffer-internal ()
1088 (defun hg-revert-buffer-internal ()
1089 (let ((ctx (hg-buffer-context)))
1089 (let ((ctx (hg-buffer-context)))
1090 (message "Reverting %s..." buffer-file-name)
1090 (message "Reverting %s..." buffer-file-name)
1091 (hg-run0 "revert" buffer-file-name)
1091 (hg-run0 "revert" buffer-file-name)
1092 (revert-buffer t t t)
1092 (revert-buffer t t t)
1093 (hg-restore-context ctx)
1093 (hg-restore-context ctx)
1094 (hg-mode-line)
1094 (hg-mode-line)
1095 (message "Reverting %s...done" buffer-file-name)))
1095 (message "Reverting %s...done" buffer-file-name)))
1096
1096
1097 (defun hg-revert-buffer ()
1097 (defun hg-revert-buffer ()
1098 "Revert current buffer's file back to the latest committed version.
1098 "Revert current buffer's file back to the latest committed version.
1099 If the file has not changed, nothing happens. Otherwise, this
1099 If the file has not changed, nothing happens. Otherwise, this
1100 displays a diff and asks for confirmation before reverting."
1100 displays a diff and asks for confirmation before reverting."
1101 (interactive)
1101 (interactive)
1102 (let ((vc-suppress-confirm nil)
1102 (let ((vc-suppress-confirm nil)
1103 (obuf (current-buffer))
1103 (obuf (current-buffer))
1104 diff)
1104 diff)
1105 (vc-buffer-sync)
1105 (vc-buffer-sync)
1106 (unwind-protect
1106 (unwind-protect
1107 (setq diff (hg-diff buffer-file-name))
1107 (setq diff (hg-diff buffer-file-name))
1108 (when diff
1108 (when diff
1109 (unless (yes-or-no-p "Discard changes? ")
1109 (unless (yes-or-no-p "Discard changes? ")
1110 (error "Revert cancelled")))
1110 (error "Revert cancelled")))
1111 (when diff
1111 (when diff
1112 (let ((buf (current-buffer)))
1112 (let ((buf (current-buffer)))
1113 (delete-window (selected-window))
1113 (delete-window (selected-window))
1114 (kill-buffer buf))))
1114 (kill-buffer buf))))
1115 (set-buffer obuf)
1115 (set-buffer obuf)
1116 (when diff
1116 (when diff
1117 (hg-revert-buffer-internal))))
1117 (hg-revert-buffer-internal))))
1118
1118
1119 (defun hg-root (&optional path)
1119 (defun hg-root (&optional path)
1120 "Return the root of the repository that contains the given path.
1120 "Return the root of the repository that contains the given path.
1121 If the path is outside a repository, return nil.
1121 If the path is outside a repository, return nil.
1122 When called interactively, the root is printed. A prefix argument
1122 When called interactively, the root is printed. A prefix argument
1123 prompts for a path to check."
1123 prompts for a path to check."
1124 (interactive (list (hg-read-file-name)))
1124 (interactive (list (hg-read-file-name)))
1125 (if (or path (not hg-root))
1125 (if (or path (not hg-root))
1126 (let ((root (do ((prev nil dir)
1126 (let ((root (do ((prev nil dir)
1127 (dir (file-name-directory
1127 (dir (file-name-directory
1128 (or
1128 (or
1129 path
1129 path
1130 buffer-file-name
1130 buffer-file-name
1131 (expand-file-name default-directory)))
1131 (expand-file-name default-directory)))
1132 (file-name-directory (directory-file-name dir))))
1132 (file-name-directory (directory-file-name dir))))
1133 ((equal prev dir))
1133 ((equal prev dir))
1134 (when (file-directory-p (concat dir ".hg"))
1134 (when (file-directory-p (concat dir ".hg"))
1135 (return dir)))))
1135 (return dir)))))
1136 (when (interactive-p)
1136 (when (interactive-p)
1137 (if root
1137 (if root
1138 (message "The root of this repository is `%s'." root)
1138 (message "The root of this repository is `%s'." root)
1139 (message "The path `%s' is not in a Mercurial repository."
1139 (message "The path `%s' is not in a Mercurial repository."
1140 (hg-abbrev-file-name path))))
1140 (hg-abbrev-file-name path))))
1141 root)
1141 root)
1142 hg-root))
1142 hg-root))
1143
1143
1144 (defun hg-status (path)
1144 (defun hg-status (path)
1145 "Print revision control status of a file or directory.
1145 "Print revision control status of a file or directory.
1146 With prefix argument, prompt for the path to give status for.
1146 With prefix argument, prompt for the path to give status for.
1147 Names are displayed relative to the repository root."
1147 Names are displayed relative to the repository root."
1148 (interactive (list (hg-read-file-name " for status" (hg-root))))
1148 (interactive (list (hg-read-file-name " for status" (hg-root))))
1149 (let ((root (hg-root)))
1149 (let ((root (hg-root)))
1150 (hg-view-output ((format "Mercurial: Status of %s in %s"
1150 (hg-view-output ((format "Mercurial: Status of %s in %s"
1151 (let ((name (substring (expand-file-name path)
1151 (let ((name (substring (expand-file-name path)
1152 (length root))))
1152 (length root))))
1153 (if (> (length name) 0)
1153 (if (> (length name) 0)
1154 name
1154 name
1155 "*"))
1155 "*"))
1156 (hg-abbrev-file-name root)))
1156 (hg-abbrev-file-name root)))
1157 (apply 'call-process (hg-binary) nil t nil
1157 (apply 'call-process (hg-binary) nil t nil
1158 (list "--cwd" root "status" path))
1158 (list "--cwd" root "status" path))
1159 (cd (hg-root path)))))
1159 (cd (hg-root path)))))
1160
1160
1161 (defun hg-undo ()
1161 (defun hg-undo ()
1162 (interactive)
1162 (interactive)
1163 (error "not implemented"))
1163 (error "not implemented"))
1164
1164
1165 (defun hg-update ()
1165 (defun hg-update ()
1166 (interactive)
1166 (interactive)
1167 (error "not implemented"))
1167 (error "not implemented"))
1168
1168
1169 (defun hg-version-other-window ()
1169 (defun hg-version-other-window ()
1170 (interactive)
1170 (interactive)
1171 (error "not implemented"))
1171 (error "not implemented"))
1172
1172
1173
1173
1174 (provide 'mercurial)
1174 (provide 'mercurial)
1175
1175
1176
1176
1177 ;;; Local Variables:
1177 ;;; Local Variables:
1178 ;;; prompt-to-byte-compile: nil
1178 ;;; prompt-to-byte-compile: nil
1179 ;;; end:
1179 ;;; end:
General Comments 0
You need to be logged in to leave comments. Login now