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