Mercurial > emacs
annotate lisp/emerge.el @ 866:ae5c412a32ec
entered into RCS
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 24 Jul 1992 20:13:03 +0000 |
parents | 0b4f3a91f207 |
children | 7e4999005da1 |
rev | line source |
---|---|
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
1 ;;; emerge.el --- merge diffs under Emacs control |
583 | 2 |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
3 ;; Author: Dale R. Worley <drw@math.mit.edu> |
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
4 ;; Version: 4 |
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
5 ;; Keywords: unix, tools |
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
6 |
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
7 ;;; Commentary: |
583 | 8 |
9 ; - Changes from version 3 to version 4 | |
10 ; | |
11 ; More configuration variables are marked as user options. | |
12 ; | |
13 ; Code is included for an improved version of make-auto-save-file-name | |
14 ; which eliminates many problems with the default version. See the | |
15 ; documentation of emerge-make-auto-save-file-name to see how to | |
16 ; activate it. | |
17 ; | |
18 ; Emerge now works with Gnu diff3, which can produce the groups of lines | |
19 ; from the various files in the order 1, 2, 3 or 1, 3, 2. | |
20 ; | |
21 ; Added x f command to show what files or buffers are being operated on. | |
22 ; | |
23 ; The merge buffer now starts read-only, which being in fast mode it | |
24 ; should be. | |
25 ; | |
26 ; When merging buffers, Emerge writes their contents into temporary | |
27 ; files in the directory $TMPDIR (if it is defined), or /tmp by default. | |
28 ; | |
29 ; Added x j command to join two differences. | |
30 ; | |
31 ; Added x s command to split a difference into two differences. | |
32 ; | |
33 ; Added emerge-version variable and function to report the version of Emerge | |
34 ; being run. | |
35 ; | |
36 ; Added x t command to trim unchanged lines off top and bottom of | |
37 ; difference region. | |
38 ; | |
39 ; Added x d, x a, and x b commands to locate the differences at or near | |
40 ; a given location in one of the buffers. | |
41 ; | |
42 ; Emerge no longer tries to copy the minor modes from the A buffer to | |
43 ; the merge buffer, only the major mode. | |
44 ; | |
45 ; The programs executed to find the differences between versions of the file | |
46 ; are no longer controlled by emerge-diff/diff3-command, but rather by: | |
47 ; emerge-diff-program | |
48 ; Variable: *Name of the program which compares two files. | |
49 ; emerge-diff3-program | |
50 ; Variable: *Name of the program which compares an ancestor file | |
51 ; (first argument) and two variant files (second and third arguments). | |
52 ; emerge-diff-options | |
53 ; Variable: *Options to be passed to emerge-diff/diff3-program. | |
54 ; | |
55 ; The names of the files are expanded (see expand-file-name) before being | |
56 ; passed to emerge-diff/diff3-program, so diff need not invoked under a shell | |
57 ; that understands '~', for instance. | |
58 ; | |
59 ; If the diff/diff3 program reports errors, the user is notified and the | |
60 ; errors are displayed. | |
61 ; | |
62 ; The command "0j" can be used to suppress the flags from showing in the buffers. | |
63 ; | |
64 ; A discussion of the effect of the merge flags on indentation of code | |
65 ; has been added to the documentation. | |
66 ; | |
67 ; If kill-fix.el is loaded, Emerge control variables new have their | |
68 ; 'preserved' property set, so setting the major mode in the merge | |
69 ; buffer doesn't destroy Emerge's state. | |
70 ; | |
71 ; Added x c, x C, and x x commands to allow the A and B versions to be | |
72 ; combined into #ifdef - #endif forms. | |
73 ; | |
74 ; Replaced calls of "ding" to calls of "error" where appropriate. | |
75 ; | |
76 ; Added x m command to allow major mode of merge buffer to be changed. | |
77 ; | |
78 ; Added x 1 command to shrink the merge window to one line. | |
79 ; | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
80 ; Added emerge-startup-hook to allow customization. |
583 | 81 ; |
82 ; Fixed a bug that is activated when a remote merge request is made when | |
83 ; the minibuffer window is selected. | |
84 ; | |
85 ; - Changes from version 2 to version 3 | |
86 ; | |
87 ; The directory into which temporary files are written is now controlled | |
88 ; by a user option (emerge-temp-file-prefix). | |
89 ; | |
90 ; The A and B versions of the difference can be loaded into the kill | |
91 ; ring with the "c a" and "c b" commands. | |
92 ; | |
93 ; The A and B versions of the difference can be inserted into the merge | |
94 ; buffer with the "i a" and "i b" commands. | |
95 ; | |
96 ; The difference region of the merge buffer can be surrounded by the | |
97 ; point and mark with the "m" command. | |
98 ; | |
99 ; The three windows can be scrolled together with the "^", "v", "<", | |
100 ; ">", and "|" commands. | |
101 ; | |
102 ; The "s s" and "s a" commands report the state of the option in the | |
103 ; echo area. Similarly, the "f" and "e" commands report what they do in | |
104 ; the echo area. | |
105 ; | |
106 ; The "q" command has been revamped, and its behavior is now controlled | |
107 ; by the manner in which Emerge is started. In particular, if you wish | |
108 ; to write the merge buffer into a file upon exiting, invoke | |
109 ; emerge-files[-with-ancestor] with a prefix argument, and it will | |
110 ; prompt you for the file name. Then exiting will write the merge | |
111 ; buffer to the file, unless "q" is given a prefix argument. | |
112 ; | |
113 ; The "i a" and "i b" commands now work in fast mode. | |
114 ; | |
115 ; The modifications that Emerge makes to save-buffer and write-file are | |
116 ; described. | |
117 ; | |
118 ; Emerge now handles merging narrowed buffers correctly. | |
119 ; | |
120 ; Emerge now isn't fooled when the buffer visiting a file is not the | |
121 ; same as the file on disk. | |
122 ; | |
123 ; - Starting | |
124 ; | |
125 ; To start Emerge, you must run one of four commands: | |
126 ; | |
127 ; emerge-files | |
128 ; emerge-files-with-ancestor | |
129 ; emerge-buffers | |
130 ; emerge-buffers-with-ancestor | |
131 ; | |
132 ; The "files" versions prompt you for two file names (the "A" and "B" | |
133 ; files), the "buffers" versions prompt you for two buffer names (the | |
134 ; "A" and "B" buffers). Emerge then runs a "diff" of the two entities | |
135 ; (emerge-buffers writes the buffers into temporary files for input to | |
136 ; diff) and digests the output to form a list of the differences between | |
137 ; the two files. Then three buffers are set up: two containing the | |
138 ; entities (emerge-files does a find-file (C-x C-f) on the files to get | |
139 ; them into buffers), and one, the "merge buffer", which contains the | |
140 ; working copy of the merged file that you are constructing. The three | |
141 ; buffers are put up in a nice three-window display, showing the A and B | |
142 ; buffers in the upper half and the merge buffer in the lower half. | |
143 ; | |
144 ; The versions of the command that say "with-ancestor" ask for a third | |
145 ; name, that of an entity which is a common ancestor from which the | |
146 ; versions being merged were derived. These commands use "diff3" to | |
147 ; compare all three versions. If one version of a difference agrees | |
148 ; with the ancestor, then it is presumed that the other version is the | |
149 ; "correct" version, and is said to be "preferred". | |
150 ; | |
151 ; (Note that if you use emerge-files, Emerge attempts to make sure that | |
152 ; file on disk and the file in the buffer are the same. If the file on | |
153 ; disk has been changed, Emerge offers to revert the buffer. If the | |
154 ; buffer has been modified, Emerge offers to save the buffer. If the | |
155 ; user declines the offer, or if the file on disk and the buffer have | |
156 ; both been modified, Emerge aborts with an error message. Emerge is | |
157 ; careful to widen the buffers containing the files if they have been | |
158 ; narrowed. If you use emerge-buffers, the buffers are not widened -- | |
159 ; only the visible portion is used.) | |
160 ; | |
161 ; During the merge, the A and B buffers are read-only, so you don't | |
162 ; damage them. (This is because the A and B versions of the differences | |
163 ; are extracted from these buffers.) When you quit the merge, the | |
164 ; read-only/read-write status and modified flag on the A and B buffers | |
165 ; are restored. In addition, auto-saving of the A and B buffers is | |
166 ; suppressed during the merge. This is because Emerge modifies the A | |
167 ; and B buffers to point out the text of the differences, and it would | |
168 ; be useless to save these changes. (Just before suppressing | |
169 ; auto-saving, Emerge forces an auto-save.) | |
170 ; | |
171 ; If you give a prefix argument to emerge-files or | |
172 ; emerge-files-with-ancestor, it prompts you for another file name, | |
173 ; which is the file into which the merged file is to be written when you | |
174 ; exit Emerge. The output file name defaults to the A file name. If | |
175 ; you successfully quit Emerge, the merge buffer will be written to the | |
176 ; output file, and the buffers for the A, B, and ancestor buffers will | |
177 ; be deleted (if they exist and are not modified). If you abort Emerge, | |
178 ; the merge buffer will not be written and the buffers will not be | |
179 ; deleted. | |
180 ; | |
181 ; You can have any number of merges going at once -- just don't use any | |
182 ; one buffer as input to more than one merge at once, since that will | |
183 ; cause the read-only/modified/auto-save status save-and-restore to | |
184 ; screw up. | |
185 ; | |
186 ; Beware that when Emerge starts up, it does a diff or diff3 of the | |
187 ; files, which can take many minutes for long files with many | |
188 ; differences. Emacs can't do anything else until diff finishes. | |
189 ; | |
190 ; If diff or diff3 produces error messages, Emerge will beep and display | |
191 ; the error messages instead of the merge buffer. There will be a | |
192 ; message in the echo area giving the name of the merge buffer. Note | |
193 ; that this is really just an informational message -- you still have | |
194 ; switch to the merge buffer and abort the merge to restore the | |
195 ; conditions before you ran Emerge. (Emerge considers any output line | |
196 ; that does not match the regexp emerge-diff/diff3-ok-lines to be an | |
197 ; error message.) | |
198 ; | |
199 ; After the merge has been set up, Emerge runs the hooks in | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
200 ; emerge-startup-hook. |
583 | 201 ; |
202 ; - Merging | |
203 ; | |
204 ; Once you have started the merge, you manipulate the merge buffer with | |
205 ; special commands issued in the merge buffer. You may also edit the | |
206 ; buffer with ordinary Emacs commands. Emerge keeps track of each | |
207 ; difference between the A and B buffers and the corresponding section | |
208 ; of the merge buffer. Initially, all differences show the A version, | |
209 ; except those for which B is preferred (because A agrees with the | |
210 ; ancestor), which show the B version. Emerge always has its attention | |
211 ; focused on one particular difference, which is marked off in the three | |
212 ; buffers by "vvvvvvvvvvvvvvvvvvvv" above and "^^^^^^^^^^^^^^^^^^^^" | |
213 ; below. The number of the difference is shown in the mode line. | |
214 ; | |
215 ; A merge buffer can be in two modes: "fast" mode and "edit" mode. In | |
216 ; fast mode, emerge commands are single characters, and ordinary Emacs | |
217 ; commands are disabled. This makes Emerge operations fast, but | |
218 ; prevents you from doing more than selecing the A or the B version of | |
219 ; differences. In edit mode, all emerge commands must be prefixed with | |
220 ; C-c, and all (non-conflicting) Emacs commands are available. This | |
221 ; allows editing the merge buffer, but slows down Emerge operations. | |
222 ; Edit and fast modes are indicated by "F" and "E" in the minor modes in | |
223 ; the mode line. | |
224 ; | |
225 ; The Emerge commands are: | |
226 ; | |
227 ; p go to the previous difference | |
228 ; n go to the next difference | |
229 ; a select the A version of this difference | |
230 ; b select the B version of this difference | |
231 ; j go to a particular difference (prefix argument | |
232 ; specifies which difference) (0j suppresses display of | |
233 ; the flags) | |
234 ; q quit - finish the merge* | |
235 ; f go into fast mode | |
236 ; e go into edit mode | |
237 ; s a set/clear auto-advance mode* | |
238 ; s s set/clear skip-prefers mode* | |
239 ; l recenter (C-l) all three windows* | |
240 ; - and 0 through 9 | |
241 ; prefix numeric arguments | |
242 ; d a select the A version as the default from here down in | |
243 ; the merge buffer* | |
244 ; d b select the B version as the default from here down in | |
245 ; the merge buffer* | |
246 ; c a copy the A version of the difference into the kill | |
247 ; ring | |
248 ; c b copy the B version of the difference into the kill | |
249 ; ring | |
250 ; i a insert the A version of the difference at the point | |
251 ; i b insert the B version of the difference at the point | |
252 ; m put the point and mark around the difference region | |
253 ; ^ scroll-down (like M-v) the three windows* | |
254 ; v scroll-up (like C-v) the three windows* | |
255 ; < scroll-left (like C-x <) the three windows* | |
256 ; > scroll-right (like C-x >) the three windows* | |
257 ; | reset horizontal scroll on the three windows* | |
258 ; x 1 shrink the merge window to one line (use C-u l to restore it | |
259 ; to full size) | |
260 ; x a find the difference containing a location in the A buffer* | |
261 ; x b find the difference containing a location in the B buffer* | |
262 ; x c combine the two versions of this difference* | |
263 ; x C combine the two versions of this difference, using a | |
264 ; register's value as the template* | |
265 ; x d find the difference containing a location in the merge buffer* | |
266 ; x f show the files/buffers Emerge is operating on in Help window | |
267 ; (use C-u l to restore windows) | |
268 ; x j join this difference with the following one | |
269 ; (C-u x j joins this difference with the previous one) | |
270 ; x l show line numbers of points in A, B, and merge buffers | |
271 ; x m change major mode of merge buffer* | |
272 ; x s split this difference into two differences | |
273 ; (first position the point in all three buffers to the places | |
274 ; to split the difference) | |
275 ; x t trim identical lines off top and bottom of difference | |
276 ; (such lines occur when the A and B versions are | |
277 ; identical but differ from the ancestor version) | |
278 ; x x set the template for the x c command* | |
279 ; | |
280 ; * - more details on these commands are given below | |
281 ; | |
282 ; emerge-version is a variable giving the version number of Emerge. It | |
283 ; is also a function which displays emerge-version (when called | |
284 ; interactively) or returns it (when called from a program). | |
285 ; | |
286 ; - Differences and their states | |
287 ; | |
288 ; A difference can have one of seven states: | |
289 ; | |
290 ; A: the difference is showing the A version. | |
291 ; | |
292 ; B: the difference is showing the B version. | |
293 ; | |
294 ; default-A and default-B: the difference is showing the A or B state, | |
295 ; but has never been selected by the user. All differences start in the | |
296 ; default-A state (and thus the merge buffer is a copy of the A buffer), | |
297 ; except those for which one buffer or another is preferred. When the | |
298 ; user selects the difference, it changes to the A or B state. | |
299 ; | |
300 ; prefer-A and prefer-B: the difference is showing the A or B state. In | |
301 ; addition, the other buffer (that is, for prefer-A, the B buffer; for | |
302 ; prefer-B, the A buffer) agrees with the ancestor buffer. Thus, | |
303 ; presumably, the displayed version is the correct one. The "a" and "b" | |
304 ; commands override these states, and turn them into the A and B states. | |
305 ; | |
306 ; combined: the difference is showing a combination of the A and B | |
307 ; states that was constructed by the "x c" or "x C" commands. Since | |
308 ; this state is neither the A or B states, the "a" and "b" commands | |
309 ; won't alter the difference unless they are given a prefix argument. | |
310 ; | |
311 ; The state of the currently selected difference is shown in the mode | |
312 ; line of the merge window: | |
313 ; | |
314 ; state display | |
315 ; | |
316 ; A A | |
317 ; B B | |
318 ; prefer-A A* | |
319 ; prefer-B B* | |
320 ; combined comb | |
321 ; | |
322 ; - Select default commands (d a and d b) | |
323 ; | |
324 ; The d a and d b commands change all default-A's to default-B's (or | |
325 ; vice-versa) from the selected difference on down to the end of the | |
326 ; file to default-A or default-B, respectively. (Since a difference | |
327 ; that has been selected can not have state default-A or default-B, it | |
328 ; will never be affected by d a or d b. This leads to the unexpected | |
329 ; result that d a or d b never affects the difference selected at the | |
330 ; moment, but prevents differences that you have already looked at from | |
331 ; changing unexpectedly.) | |
332 ; | |
333 ; If you work your way down from the top of the file, using d a and d b | |
334 ; at judicious points, you can effectivly make the A version the default | |
335 ; for some sections of the merge buffer and the B version the default | |
336 ; for others. | |
337 ; | |
338 ; - Exiting (q) | |
339 ; | |
340 ; The quit command finishes the merge session by restoring the state of | |
341 ; the A and B buffers and removing the markers around the currently | |
342 ; selected difference. It also disables the Emerge commands in the | |
343 ; merge buffer, since executing them later could damage the contents of | |
344 ; the various buffers. | |
345 ; | |
346 ; The action of "q" depends on how Emerge was started and whether "q" | |
347 ; was given a prefix argument. If there was no prefix argument, it is | |
348 ; considered a "successful" finish. If there was a prefix argument, it | |
349 ; is considered an "unsuccessful" finish. In either case, you are asked | |
350 ; to cofirm the exit, and the confirmation message tells which sort of | |
351 ; exit you are confirming. | |
352 ; | |
353 ; If Emerge was started by some other process, success/failure is | |
354 ; reported to the caller. | |
355 ; | |
356 ; If Emerge was started with emerge-files or emerge-files-with-ancestor, | |
357 ; if a prefix argument was given to that command, then you specified a | |
358 ; file into which the merge is to be written. A successful exit writes | |
359 ; the merge into the output file and then kills the A, B, and ancestor | |
360 ; buffers (so they aren't lying around to confuse you, since they | |
361 ; probably all have similar names). | |
362 ; | |
363 ; - Auto-advance mode (s a) | |
364 ; | |
365 ; If auto-advance mode is set, the "a" and "b" commands perform an "n" | |
366 ; (select next difference) afterward. When auto-advance mode is set, | |
367 ; it is indicated by "A" in the minor modes in the mode line. | |
368 ; "s a" with a positive argument sets auto-advance, with a non-positive | |
369 ; argument clears it, and with no argument toggles it. | |
370 ; | |
371 ; - Skip-prefers mode (s s) | |
372 ; | |
373 ; If skip-prefers mode is set, the "n" and "p" commands skip over | |
374 ; differences with states prefer-A and prefer-B. Thus you will only see | |
375 ; differences for which one version isn't presumed "correct". When | |
376 ; skip-prefers mode is set, it is indicated by "S" in the minor modes in | |
377 ; the mode line. "s s" with a positive argument sets auto-advance, with | |
378 ; a non-positive argument clears it, and with no argument toggles it. | |
379 ; | |
380 ; - Recenter (l) | |
381 ; | |
382 ; The Emerge "l" command causes the selected difference to be brought | |
383 ; into view in the three windows, or at least, whichever of the three | |
384 ; merge buffers are visible at the moment. If a prefix argument is | |
385 ; given, then the original three-window display is set up before the | |
386 ; difference texts are shown. | |
387 ; | |
388 ; - Scrolling the text (^, v, <, >, and |) | |
389 ; | |
390 ; Emerge has several commands which scroll all three windows by the same | |
391 ; amount, thus allowing you to easily compare the versions of the text. | |
392 ; The commands are "^" (scroll-up), "v" (scroll-down), "<" | |
393 ; (scroll-left), ">" (scroll-right), and "|" (reset horizontal | |
394 ; scrolling). (Remember that Emacs names scrolling commands by the | |
395 ; motion of the text with respect to the window, so C-v is called | |
396 ; "scroll-up".) | |
397 ; | |
398 ; If these commands (except "|") are given an argument, that is the | |
399 ; number of lines or characters by which the windows are scrolled. | |
400 ; Otherwise, the amount of motion is computed based on the dimensions of | |
401 ; the merge buffer window -- the height of the merge buffer window | |
778 | 402 ; (minus next-frame-context-lines), or half the width of the merge |
583 | 403 ; buffer window. (The A and B version windows are assumed to be as high |
404 ; as the merge window, but half as wide.) If the argument is just `C-u | |
405 ; -', then the scrolling is half the default amount. | |
406 ; | |
407 ; - Finding the difference at or near a location (x d, x a, and x b) | |
408 ; | |
409 ; The "x d" command selects the difference containing the current point | |
410 ; in the merge buffer. If there is no difference containing the point, | |
411 ; an error is given. An argument can be given to the command to change | |
412 ; this behavior: if the argument is positive (e.g., C-u), the next | |
413 ; following difference is selected; if the argument is negative (e.g., | |
414 ; C-u -), the previous difference is selected. | |
415 ; | |
416 ; The "x a" and "x b" commands select the difference containing the | |
417 ; current point in the A and B buffers, respectively. Otherwise, they | |
418 ; act like the "x d" command. Note that although the point used in the | |
419 ; commands is not the merge buffer point, the commands can only be | |
420 ; issued in the merge buffer, because it is the only buffer with the | |
421 ; Emerge keymap. | |
422 ; | |
423 ; - Combining the two versions (x c, x C, and x x) | |
424 ; | |
425 ; Sometimes one wants to combine the two versions of a difference. For | |
426 ; instance, when merging two versions of a program, one wants to make | |
427 ; something like this: | |
428 ; | |
429 ; #ifdef NEW | |
430 ; ...new version of code... | |
431 ; #else /* NEW */ | |
432 ; ...old version of code... | |
433 ; #endif /* NEW */ | |
434 ; | |
435 ; The "x c" command will make such a combined version. (Note that any | |
436 ; combined version is not the same as either the A or B versions, and so | |
437 ; the "a" and "b" commands will refuse to alter it unless they are given | |
438 ; a prefix argument.) The combination is made under control of a | |
439 ; template, which is a character string with the following | |
440 ; interpolations: | |
441 ; | |
442 ; %a the A version of the difference | |
443 ; %b the B version of the difference | |
444 ; %% the character '%' | |
445 ; | |
446 ; Thus, the template used above is | |
447 ; | |
448 ; #ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n | |
449 ; | |
450 ; (using \n here to represent newlines). The template is stored in the | |
451 ; variable emerge-combine-versions-template, and its initial value is | |
452 ; the one given above. The template can be set (from the current | |
453 ; region) by the "x x" command. (Be careful to get the newlines in the | |
454 ; template in the right places!) ("x x" was chosen by analogy with "C-x | |
455 ; x".) ("x x" is only available in the merge buffer, of course. | |
456 ; Elsewhere, M-x emerge-set-combine-versions-template can be used.) If | |
457 ; "x x" is given a prefix argument, emerge-combine-versions-template is | |
458 ; localized in the merge buffer before its value is set, so the "x x" | |
459 ; command's effect (and the effect of any later "x x" command in the | |
460 ; merge buffer) is only on the merge buffer. | |
461 ; | |
462 ; The "x C" command is like "x c", but it prompts for a character | |
463 ; which is the register whose value is to be used as the template. | |
464 ; This allows one to use multiple templates conveniently. | |
465 ; | |
466 ; - Changing the major mode of the edit buffer (x m) | |
467 ; | |
468 ; The "x m" command prompts for the name of a major-mode-setting command | |
469 ; and executes it. Ordinarily, major-mode-setting commands change the | |
470 ; mode line and local keymap, so the "x m" command then resets the | |
471 ; Emerge mode line and the fast or edit mode local keymap, as | |
472 ; appropriate. | |
473 ; | |
474 ; If you have already changed the major mode of the merge buffer and | |
475 ; lost the Emerge keymap, you can use M-x emerge-set-merge-mode to | |
476 ; execute this command. | |
477 ; | |
478 ; Beware that "x m" accepts any command name, not just | |
479 ; major-mode-setting commands. | |
480 ; | |
481 ; - Writing the merge buffer manually | |
482 ; | |
483 ; Emerge places a wrapper (emerge-query-and-call) on the key bindings of | |
484 ; save-buffer (usually "C-x C-s") and write-file (usually "C-x C-w"), in | |
485 ; order to protect the user from writing out the merge before it is | |
486 ; finished. Emerge-query-and-call asks the user if he is sure he wants | |
487 ; to write out the incomplete merge. If he answers yes, the buffer is | |
488 ; written out. The flags are suppressed while the write is being done. | |
489 ; As a result of this, the displayed portions of the buffers are | |
490 ; recentered (equivalent to "l"). | |
491 ; | |
492 ; - Running Emerge standalone | |
493 ; | |
494 ; If you invoke emacs with the following arguments, you can execute | |
495 ; Emerge as a standalone program: | |
496 ; | |
497 ; emacs -l emerge -f emerge-files-command file-a file-b file-out | |
498 ; | |
499 ; emacs -l emerge -f emerge-files-with-ancestor-command | |
500 ; file-a file-b file-ancestor file-out | |
501 ; | |
502 ; When the user gives the "q" (quit) command, Emerge will write out the | |
503 ; merge buffer in file-out and terminate Emacs. If a prefix argument is | |
504 ; given, Emacs will terminate with an unsuccessful return code (1), if | |
505 ; not, it will terminate with a successful return code (0). | |
506 ; | |
507 ; - Invoking Emerge remotely | |
508 ; | |
509 ; If you use the Emacs client/server code that supports remote | |
510 ; execution, then you can invoke Emerge remotely by executing one of the | |
511 ; Lisp calls: | |
512 ; | |
513 ; (emerge-files-remote "file A" "file B" "output file") | |
514 ; | |
515 ; (emerge-files-with-ancestor-remote "file A" "file B" | |
516 ; "ancestor file" "output file") | |
517 ; | |
518 ; Returning a successful/unsuccessful return code is not yet supported | |
519 ; by the Emacs client/server code. | |
520 ; | |
521 ; Beware that in systems of networked workstations, even though all user | |
522 ; directories are shared between all the workstations, the /tmp | |
523 ; directory on each workstation is not shared, so writing files into | |
524 ; /tmp and then remotely invoking Emerge is not likely to work. | |
525 ; | |
526 ; - Effect of merge flags on indenting code | |
527 ; | |
528 ; The presence of the flags confuses the indentation code of C and | |
529 ; Emacs-Lisp modes. Starting the flag strings | |
530 ; (emerge-{before,after}-flag) with '#' (for C) or ';' (for Lisp) | |
531 ; prevents the indentation code from noticing the flags. Remember to | |
532 ; change the flag strings before loading Emerge, or to execute | |
533 ; emerge-new-flags after changing them. But never change the flag | |
534 ; strings while a merge is being performed. | |
535 ; | |
536 ; - Autoloading | |
537 ; | |
538 ; The following autoloads will make all top-level Emerge files | |
539 ; autoloading. Make sure that "emerge" is in a directory on load-path. | |
540 ; | |
541 ; (autoload 'emerge-files "emerge" | |
542 ; "Run Emerge on two files." | |
543 ; t) | |
544 ; (autoload 'emerge-files-with-ancestor "emerge" | |
545 ; "Run Emerge on two files, giving another file as the ancestor." | |
546 ; t) | |
547 ; (autoload 'emerge-buffers "emerge" | |
548 ; "Run Emerge on two buffers." | |
549 ; t) | |
550 ; (autoload 'emerge-buffers-with-ancestor "emerge" | |
551 ; "Run Emerge on two buffers, giving another buffer as the ancestor." | |
552 ; t) | |
553 ; (autoload 'emerge-files-command "emerge") | |
554 ; (autoload 'emerge-files-with-ancestor-command "emerge") | |
555 ; (autoload 'emerge-files-remote "emerge") | |
556 ; (autoload 'emerge-files-with-ancestor-remote "emerge") | |
557 ; | |
558 ; ================================================================ | |
559 | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
560 ;;; Code: |
583 | 561 |
562 ;;; Macros | |
563 | |
564 (defmacro emerge-eval-in-buffer (buffer &rest forms) | |
565 "Macro to switch to BUFFER, evaluate FORMS, returns to original buffer. | |
566 Differs from save-excursion in that it doesn't save the point and mark." | |
567 (` (let ((StartBuffer (current-buffer))) | |
568 (unwind-protect | |
569 (progn | |
570 (set-buffer (, buffer)) | |
571 (,@ forms)) | |
572 (set-buffer StartBuffer))))) | |
573 | |
574 (defmacro emerge-defvar-local (var value doc) | |
575 "Defines SYMBOL as an advertised variable. Performs a defvar, then | |
576 executes make-variable-buffer-local on the variable. Also sets the | |
577 'preserved' property, so that kill-all-local-variables (called by major-mode | |
578 setting commands) won't destroy Emerge control variables." | |
579 (` (progn | |
580 (defvar (, var) (, value) (, doc)) | |
581 (make-variable-buffer-local '(, var)) | |
582 (put '(, var) 'preserved t)))) | |
583 | |
584 ;; Add entries to minor-mode-alist so that emerge modes show correctly | |
585 (setq emerge-minor-modes-list '((emerge-mode " Emerge") | |
586 (emerge-fast-mode " F") | |
587 (emerge-edit-mode " E") | |
588 (emerge-auto-advance " A") | |
589 (emerge-skip-prefers " S"))) | |
590 (if (not (assq 'emerge-mode minor-mode-alist)) | |
591 (setq minor-mode-alist (append emerge-minor-modes-list | |
592 minor-mode-alist))) | |
593 | |
594 ;; We need to define this function so describe-mode can describe Emerge mode. | |
595 (defun emerge-mode () | |
596 "Emerge mode is used by the Emerge file-merging package. It is entered only | |
597 through one of the functions: | |
598 emerge-files | |
599 emerge-files-with-ancestor | |
600 emerge-buffers | |
601 emerge-buffers-with-ancestor | |
602 emerge-files-command | |
603 emerge-files-with-ancestor-command | |
604 emerge-files-remote | |
605 emerge-files-with-ancestor-remote | |
606 | |
607 Commands: | |
608 \\{emerge-basic-keymap} | |
609 Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode, but can be invoked directly | |
610 in 'fast' mode.") | |
611 | |
612 (defvar emerge-version "4" | |
613 "The version of Emerge.") | |
614 | |
615 (defun emerge-version () | |
616 "Return string describing the version of Emerge. When called interactively, | |
617 displays the version." | |
618 (interactive) | |
619 (if (interactive-p) | |
620 (message "Emerge version %s" (emerge-version)) | |
621 emerge-version)) | |
622 | |
623 ;;; Emerge configuration variables | |
624 | |
625 ;; Commands that produce difference files | |
626 ;; All that can be configured is the name of the programs to execute | |
627 ;; (emerge-diff-program and emerge-diff3-program) and the options | |
628 ;; to be provided (emerge-diff-options). The order in which the file names | |
629 ;; are given is fixed. | |
630 ;; The file names are always expanded (see expand-file-name) before being | |
631 ;; passed to diff, thus they need not be invoked under a shell that | |
632 ;; understands '~'. | |
633 ;; The code which processes the diff/diff3 output depends on all the | |
634 ;; finicky details of their output, including the somewhat strange | |
635 ;; way they number lines of a file. | |
636 (defvar emerge-diff-program "diff" | |
637 "*Name of the program which compares two files.") | |
638 (defvar emerge-diff3-program "diff3" | |
639 "*Name of the program which compares an ancestor file (first argument) | |
640 and two variant files (second and third arguments).") | |
641 (defvar emerge-diff-options "" | |
642 "*Options to be passed to emerge-diff/diff3-program.") | |
643 (defvar emerge-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)")) | |
644 (concat "^" x "\\([acd]\\)" x "$")) | |
645 "*Pattern to match lines produced by diff that describe differences (as | |
646 opposed to lines from the source files).") | |
647 (defvar emerge-diff-ok-lines | |
648 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)" | |
649 "*Regexp that matches normal output lines from emerge-diff-program . | |
650 Lines that do not match are assumed to be error output.") | |
651 (defvar emerge-diff3-ok-lines | |
652 "^\\([1-3]:\\|====\\| \\)" | |
653 "*Regexp that matches normal output lines from emerge-diff3-program . | |
654 Lines that do not match are assumed to be error output.") | |
655 | |
656 ;; The flags used to mark differences in the buffers. | |
657 | |
658 ;; These function definitions need to be up here, because they are used | |
659 ;; during loading. | |
660 (defun emerge-new-flags () | |
661 "Function to be called after emerge-{before,after}-flag are changed to | |
662 compute values that depend on the flags." | |
663 (setq emerge-before-flag-length (length emerge-before-flag)) | |
664 (setq emerge-before-flag-lines | |
665 (count-matches-string emerge-before-flag "\n")) | |
666 (setq emerge-before-flag-match (regexp-quote emerge-before-flag)) | |
667 (setq emerge-after-flag-length (length emerge-after-flag)) | |
668 (setq emerge-after-flag-lines | |
669 (count-matches-string emerge-after-flag "\n")) | |
670 (setq emerge-after-flag-match (regexp-quote emerge-after-flag))) | |
671 (defun count-matches-string (string regexp) | |
672 "Return the number of matches in STRING for REGEXP." | |
673 (let ((i 0) | |
674 (count 0)) | |
675 (while (string-match regexp string i) | |
676 (setq count (1+ count)) | |
677 (setq i (match-end 0))) | |
678 count)) | |
679 | |
680 (defvar emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n" | |
681 "*Flag placed above the highlighted block of code. Must end with newline. | |
682 Must be set before Emerge is loaded, or emerge-new-flags must be run | |
683 after setting.") | |
684 (defvar emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n" | |
685 "*Flag placed below the highlighted block of code. Must end with newline. | |
686 Must be set before Emerge is loaded, or emerge-new-flags must be run | |
687 after setting.") | |
688 | |
689 ;; Calculate dependent variables | |
690 (emerge-new-flags) | |
691 | |
692 (defvar emerge-min-visible-lines 3 | |
693 "*Number of lines that we want to show above and below the flags when we are | |
694 displaying a difference.") | |
695 | |
696 (defvar emerge-temp-file-prefix | |
697 (let ((env (getenv "TMPDIR")) | |
698 d) | |
699 (setq d (if (and env (> (length env) 0)) | |
700 env | |
701 "/tmp")) | |
702 (if (= (aref d (1- (length d))) ?/) | |
703 (setq d (substring d 0 -1))) | |
704 (concat d "/emerge")) | |
705 "*Prefix to put on Emerge temporary file names. | |
706 Do not start with '~/' or '~user-name/'.") | |
707 | |
708 (defvar emerge-temp-file-mode 384 ; u=rw only | |
709 "*Mode for Emerge temporary files.") | |
710 | |
711 (defvar emerge-combine-versions-template | |
712 "#ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n" | |
713 "*Template for emerge-combine-versions to combine the two versions. | |
714 The template is inserted as a string, with the following interpolations: | |
715 %a the A version of the difference | |
716 %b the B version of the difference | |
717 %% the character '%' | |
718 Don't forget to end the template with a newline. | |
719 Note that this variable can be made local to a particular merge buffer by | |
720 giving a prefix argument to emerge-set-combine-versions-template .") | |
721 | |
722 ;; Build keymaps | |
723 | |
724 (defvar emerge-basic-keymap nil | |
725 "Keymap of Emerge commands. | |
726 Directly available in 'fast' mode; | |
727 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode.") | |
728 | |
729 (defvar emerge-fast-keymap nil | |
730 "Local keymap used in Emerge 'fast' mode. | |
731 Makes Emerge commands directly available.") | |
732 | |
733 (defvar emerge-command-prefix "\C-c" | |
734 "*Command prefix for Emerge commands in 'edit' mode. | |
735 Must be set before Emerge is loaded.") | |
736 | |
737 ;; This function sets up the fixed keymaps. It is executed when the first | |
738 ;; Emerge is done to allow the user maximum time to set up the global keymap. | |
739 (defun emerge-setup-fixed-keymaps () | |
740 ;; Set up the basic keymap | |
741 (setq emerge-basic-keymap (make-keymap)) | |
742 (suppress-keymap emerge-basic-keymap) ; this sets 0..9 to digit-argument and | |
743 ; - to negative-argument | |
744 (define-key emerge-basic-keymap "p" 'emerge-previous-difference) | |
745 (define-key emerge-basic-keymap "n" 'emerge-next-difference) | |
746 (define-key emerge-basic-keymap "a" 'emerge-select-A) | |
747 (define-key emerge-basic-keymap "b" 'emerge-select-B) | |
748 (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference) | |
749 (define-key emerge-basic-keymap "q" 'emerge-quit) | |
750 (define-key emerge-basic-keymap "f" 'emerge-fast-mode) | |
751 (define-key emerge-basic-keymap "e" 'emerge-edit-mode) | |
752 (define-key emerge-basic-keymap "s" nil) | |
753 (define-key emerge-basic-keymap "sa" 'emerge-auto-advance) | |
754 (define-key emerge-basic-keymap "ss" 'emerge-skip-prefers) | |
755 (define-key emerge-basic-keymap "l" 'emerge-recenter) | |
756 (define-key emerge-basic-keymap "d" nil) | |
757 (define-key emerge-basic-keymap "da" 'emerge-default-A) | |
758 (define-key emerge-basic-keymap "db" 'emerge-default-B) | |
759 (define-key emerge-basic-keymap "c" nil) | |
760 (define-key emerge-basic-keymap "ca" 'emerge-copy-as-kill-A) | |
761 (define-key emerge-basic-keymap "cb" 'emerge-copy-as-kill-B) | |
762 (define-key emerge-basic-keymap "i" nil) | |
763 (define-key emerge-basic-keymap "ia" 'emerge-insert-A) | |
764 (define-key emerge-basic-keymap "ib" 'emerge-insert-B) | |
765 (define-key emerge-basic-keymap "m" 'emerge-mark-difference) | |
766 (define-key emerge-basic-keymap "v" 'emerge-scroll-up) | |
767 (define-key emerge-basic-keymap "^" 'emerge-scroll-down) | |
768 (define-key emerge-basic-keymap "<" 'emerge-scroll-left) | |
769 (define-key emerge-basic-keymap ">" 'emerge-scroll-right) | |
770 (define-key emerge-basic-keymap "|" 'emerge-scroll-reset) | |
771 (define-key emerge-basic-keymap "x" nil) | |
772 (define-key emerge-basic-keymap "x1" 'emerge-one-line-window) | |
773 (define-key emerge-basic-keymap "xa" 'emerge-find-difference-A) | |
774 (define-key emerge-basic-keymap "xb" 'emerge-find-difference-B) | |
775 (define-key emerge-basic-keymap "xc" 'emerge-combine-versions) | |
776 (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register) | |
777 (define-key emerge-basic-keymap "xd" 'emerge-find-difference) | |
778 (define-key emerge-basic-keymap "xf" 'emerge-file-names) | |
779 (define-key emerge-basic-keymap "xj" 'emerge-join-differences) | |
780 (define-key emerge-basic-keymap "xl" 'emerge-line-numbers) | |
781 (define-key emerge-basic-keymap "xm" 'emerge-set-merge-mode) | |
782 (define-key emerge-basic-keymap "xs" 'emerge-split-difference) | |
783 (define-key emerge-basic-keymap "xt" 'emerge-trim-difference) | |
784 (define-key emerge-basic-keymap "xx" 'emerge-set-combine-versions-template) | |
785 ;; Allow emerge-basic-keymap to be referenced indirectly | |
786 (fset 'emerge-basic-keymap emerge-basic-keymap) | |
787 ;; Set up the fast mode keymap | |
788 (setq emerge-fast-keymap (copy-keymap emerge-basic-keymap)) | |
789 ;; Allow prefixed commands to work in fast mode | |
790 (define-key emerge-fast-keymap emerge-command-prefix 'emerge-basic-keymap) | |
791 ;; Allow emerge-fast-keymap to be referenced indirectly | |
792 (fset 'emerge-fast-keymap emerge-fast-keymap) | |
793 ;; Suppress write-file and save-buffer | |
794 (emerge-shadow-key-definition 'write-file 'emerge-query-write-file | |
795 (current-global-map) emerge-fast-keymap) | |
796 (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer | |
797 (current-global-map) emerge-fast-keymap)) | |
798 | |
799 ;; Variables which control each merge. They are local to the merge buffer. | |
800 | |
801 ;; Mode variables | |
802 (emerge-defvar-local emerge-mode nil | |
803 "Indicator for emerge-mode.") | |
804 (emerge-defvar-local emerge-fast-mode nil | |
805 "Indicator for emerge-mode fast submode.") | |
806 (emerge-defvar-local emerge-edit-mode nil | |
807 "Indicator for emerge-mode edit submode.") | |
808 (emerge-defvar-local emerge-A-buffer nil | |
809 "The buffer in which the A variant is stored.") | |
810 (emerge-defvar-local emerge-B-buffer nil | |
811 "The buffer in which the B variant is stored.") | |
812 (emerge-defvar-local emerge-merge-buffer nil | |
813 "The buffer in which the merged file is manipulated.") | |
814 (emerge-defvar-local emerge-ancestor-buffer nil | |
815 "The buffer in which the ancestor variant is stored, | |
816 or nil if there is none.") | |
817 | |
818 (defconst emerge-saved-variables | |
819 '((buffer-modified-p set-buffer-modified-p) | |
820 buffer-read-only | |
821 buffer-auto-save-file-name) | |
822 "Variables and properties of a buffer which are saved, modified and restored | |
823 during a merge.") | |
824 (defconst emerge-merging-values '(nil t nil) | |
825 "Values to be assigned to emerge-saved-variables during a merge.") | |
826 | |
827 (emerge-defvar-local emerge-A-buffer-values nil | |
828 "Remembers emerge-saved-variables for emerge-A-buffer.") | |
829 (emerge-defvar-local emerge-B-buffer-values nil | |
830 "Remembers emerge-saved-variables for emerge-B-buffer.") | |
831 | |
832 (emerge-defvar-local emerge-difference-list nil | |
833 "Vector of differences between the variants, and markers in the buffers to | |
834 show where they are. Each difference is represented by a vector of seven | |
835 elements. The first two are markers to the beginning and end of the difference | |
836 section in the A buffer, the second two are markers for the B buffer, the third | |
837 two are markers for the merge buffer, and the last element is the \"state\" of | |
838 that difference in the merge buffer. | |
839 A section of a buffer is described by two markers, one to the beginning of | |
840 the first line of the section, and one to the beginning of the first line | |
841 after the section. (If the section is empty, both markers point to the same | |
842 point.) If the section is part of the selected difference, then the markers | |
843 are moved into the flags, so the user can edit the section without disturbing | |
844 the markers. | |
845 The \"states\" are: | |
846 A the merge buffer currently contains the A variant | |
847 B the merge buffer currently contains the B variant | |
848 default-A the merge buffer contains the A variant by default, | |
849 but this difference hasn't been selected yet, so | |
850 change-default commands can alter it | |
851 default-B the merge buffer contains the B variant by default, | |
852 but this difference hasn't been selected yet, so | |
853 change-default commands can alter it | |
854 prefer-A in a three-file merge, the A variant is the prefered | |
855 choice | |
856 prefer-B in a three-file merge, the B variant is the prefered | |
857 choice") | |
858 (emerge-defvar-local emerge-current-difference -1 | |
859 "The difference that is currently selected.") | |
860 (emerge-defvar-local emerge-number-of-differences nil | |
861 "Number of differences found.") | |
862 (emerge-defvar-local emerge-edit-keymap nil | |
863 "The local keymap for the merge buffer, with the emerge commands defined in | |
864 it. Used to save the local keymap during fast mode, when the local keymap is | |
865 replaced by emerge-fast-keymap.") | |
866 (emerge-defvar-local emerge-old-keymap nil | |
867 "The original local keymap for the merge buffer.") | |
868 (emerge-defvar-local emerge-auto-advance nil | |
869 "*If non-nil, emerge-select-A and emerge-select-B automatically advance to | |
870 the next difference.") | |
871 (emerge-defvar-local emerge-skip-prefers nil | |
872 "*If non-nil, differences for which there is a preference are automatically | |
873 skipped.") | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
874 (emerge-defvar-local emerge-startup-hook nil |
583 | 875 "*Hooks to run in the merge buffer after the merge has been set up.") |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
876 (emerge-defvar-local emerge-quit-hook nil |
583 | 877 "Hooks to run in the merge buffer after the merge has been finished. |
878 emerge-prefix-argument will be bound to the prefix argument of the emerge-quit | |
879 command. | |
880 This is not a user option, since Emerge uses it for its own processing.") | |
881 (emerge-defvar-local emerge-output-description nil | |
882 "Describes output destination of the merge, for the use of | |
883 emerge-file-names.") | |
884 | |
885 ;;; Setup functions for two-file mode. | |
886 | |
887 (defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks | |
888 output-file) | |
889 (let ((buffer-A (find-file-noselect file-A)) | |
890 (buffer-B (find-file-noselect file-B))) | |
891 ;; Make sure the entire files are seen, and they reflect what is on disk | |
892 (emerge-eval-in-buffer buffer-A | |
893 (widen) | |
894 (emerge-verify-file-buffer)) | |
895 (emerge-eval-in-buffer buffer-B | |
896 (widen) | |
897 (emerge-verify-file-buffer)) | |
898 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks | |
899 output-file))) | |
900 | |
901 ;; Start up Emerge on two files | |
902 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks | |
903 output-file) | |
904 (setq file-A (expand-file-name file-A)) | |
905 (setq file-B (expand-file-name file-B)) | |
906 (setq output-file (and output-file (expand-file-name output-file))) | |
907 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*")) | |
908 ;; create the merge buffer from buffer A, so it inherits buffer A's | |
909 ;; default directory, etc. | |
910 (merge-buffer (emerge-eval-in-buffer | |
911 buffer-A | |
912 (get-buffer-create merge-buffer-name)))) | |
913 (emerge-eval-in-buffer | |
914 merge-buffer | |
915 (emerge-copy-modes buffer-A) | |
916 (setq buffer-read-only nil) | |
917 (auto-save-mode 1) | |
918 (setq emerge-mode t) | |
919 (setq emerge-A-buffer buffer-A) | |
920 (setq emerge-B-buffer buffer-B) | |
921 (setq emerge-ancestor-buffer nil) | |
922 (setq emerge-merge-buffer merge-buffer) | |
923 (setq emerge-output-description | |
924 (if output-file | |
925 (concat "Output to file: " output-file) | |
926 (concat "Output to buffer: " (buffer-name merge-buffer)))) | |
927 (insert-buffer emerge-A-buffer) | |
928 (emerge-set-keys) | |
929 (setq emerge-difference-list (emerge-make-diff-list file-A file-B)) | |
930 (setq emerge-number-of-differences (length emerge-difference-list)) | |
931 (setq emerge-current-difference -1) | |
932 (setq emerge-quit-hooks quit-hooks) | |
933 (emerge-remember-buffer-characteristics)) | |
934 (emerge-setup-windows buffer-A buffer-B merge-buffer t) | |
935 (emerge-eval-in-buffer merge-buffer | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
936 (run-hooks 'startup-hooks 'emerge-startup-hook) |
583 | 937 (setq buffer-read-only t)))) |
938 | |
939 ;; Generate the Emerge difference list between two files | |
940 (defun emerge-make-diff-list (file-A file-B) | |
941 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*")) | |
942 (emerge-eval-in-buffer | |
943 emerge-diff-buffer | |
944 (erase-buffer) | |
945 (shell-command | |
946 (format "%s %s %s %s" | |
947 emerge-diff-program emerge-diff-options file-A file-B) | |
948 t)) | |
949 (emerge-prepare-error-list emerge-diff-ok-lines) | |
950 (emerge-convert-diffs-to-markers | |
951 emerge-A-buffer emerge-B-buffer emerge-merge-buffer | |
952 (emerge-extract-diffs emerge-diff-buffer))) | |
953 | |
954 (defun emerge-extract-diffs (diff-buffer) | |
955 (let (list) | |
956 (emerge-eval-in-buffer | |
957 diff-buffer | |
958 (goto-char (point-min)) | |
959 (while (re-search-forward emerge-match-diff-line nil t) | |
960 (let* ((a-begin (string-to-int (buffer-substring (match-beginning 1) | |
961 (match-end 1)))) | |
962 (a-end (let ((b (match-beginning 3)) | |
963 (e (match-end 3))) | |
964 (if b | |
965 (string-to-int (buffer-substring b e)) | |
966 a-begin))) | |
967 (diff-type (buffer-substring (match-beginning 4) (match-end 4))) | |
968 (b-begin (string-to-int (buffer-substring (match-beginning 5) | |
969 (match-end 5)))) | |
970 (b-end (let ((b (match-beginning 7)) | |
971 (e (match-end 7))) | |
972 (if b | |
973 (string-to-int (buffer-substring b e)) | |
974 b-begin)))) | |
975 ;; fix the beginning and end numbers, because diff is somewhat | |
976 ;; strange about how it numbers lines | |
977 (if (string-equal diff-type "a") | |
978 (progn | |
979 (setq b-end (1+ b-end)) | |
980 (setq a-begin (1+ a-begin)) | |
981 (setq a-end a-begin)) | |
982 (if (string-equal diff-type "d") | |
983 (progn | |
984 (setq a-end (1+ a-end)) | |
985 (setq b-begin (1+ b-begin)) | |
986 (setq b-end b-begin)) | |
987 ;; (string-equal diff-type "c") | |
988 (progn | |
989 (setq a-end (1+ a-end)) | |
990 (setq b-end (1+ b-end))))) | |
991 (setq list (cons (vector a-begin a-end | |
992 b-begin b-end | |
993 'default-A) | |
994 list))))) | |
995 (nreverse list))) | |
996 | |
997 ;; Set up buffer of diff/diff3 error messages. | |
998 (defun emerge-prepare-error-list (ok-regexp) | |
999 (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*")) | |
1000 (emerge-eval-in-buffer | |
1001 emerge-diff-error-buffer | |
1002 (erase-buffer) | |
1003 (insert-buffer emerge-diff-buffer) | |
1004 (delete-matching-lines ok-regexp))) | |
1005 | |
1006 ;;; Top-level and setup functions for three-file mode. | |
1007 | |
1008 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor | |
1009 &optional startup-hooks quit-hooks | |
1010 output-file) | |
1011 (let ((buffer-A (find-file-noselect file-A)) | |
1012 (buffer-B (find-file-noselect file-B)) | |
1013 (buffer-ancestor (find-file-noselect file-ancestor))) | |
1014 ;; Make sure the entire files are seen, and they reflect what is on disk | |
1015 (emerge-eval-in-buffer buffer-A | |
1016 (widen) | |
1017 (emerge-verify-file-buffer)) | |
1018 (emerge-eval-in-buffer buffer-B | |
1019 (widen) | |
1020 (emerge-verify-file-buffer)) | |
1021 (emerge-eval-in-buffer buffer-ancestor | |
1022 (widen) | |
1023 (emerge-verify-file-buffer)) | |
1024 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B | |
1025 buffer-ancestor file-ancestor | |
1026 startup-hooks quit-hooks output-file))) | |
1027 | |
1028 ;; Start up Emerge on two files with an ancestor | |
1029 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B | |
1030 buffer-ancestor file-ancestor | |
1031 &optional startup-hooks quit-hooks | |
1032 output-file) | |
1033 (setq file-A (expand-file-name file-A)) | |
1034 (setq file-B (expand-file-name file-B)) | |
1035 (setq file-ancestor (expand-file-name file-ancestor)) | |
1036 (setq output-file (and output-file (expand-file-name output-file))) | |
1037 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*")) | |
1038 ;; create the merge buffer from buffer A, so it inherits buffer A's | |
1039 ;; default directory, etc. | |
1040 (merge-buffer (emerge-eval-in-buffer | |
1041 buffer-A | |
1042 (get-buffer-create merge-buffer-name)))) | |
1043 (emerge-eval-in-buffer | |
1044 merge-buffer | |
1045 (emerge-copy-modes buffer-A) | |
1046 (setq buffer-read-only nil) | |
1047 (auto-save-mode 1) | |
1048 (setq emerge-mode t) | |
1049 (setq emerge-A-buffer buffer-A) | |
1050 (setq emerge-B-buffer buffer-B) | |
1051 (setq emerge-ancestor-buffer buffer-ancestor) | |
1052 (setq emerge-merge-buffer merge-buffer) | |
1053 (setq emerge-output-description | |
1054 (if output-file | |
1055 (concat "Output to file: " output-file) | |
1056 (concat "Output to buffer: " (buffer-name merge-buffer)))) | |
1057 (insert-buffer emerge-A-buffer) | |
1058 (emerge-set-keys) | |
1059 (setq emerge-difference-list | |
1060 (emerge-make-diff3-list file-A file-B file-ancestor)) | |
1061 (setq emerge-number-of-differences (length emerge-difference-list)) | |
1062 (setq emerge-current-difference -1) | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
1063 (setq emerge-quit-hook quit-hooks) |
583 | 1064 (emerge-remember-buffer-characteristics) |
1065 (emerge-select-prefer-Bs)) | |
1066 (emerge-setup-windows buffer-A buffer-B merge-buffer t) | |
1067 (emerge-eval-in-buffer merge-buffer | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
1068 (run-hooks 'startup-hooks 'emerge-startup-hook) |
583 | 1069 (setq buffer-read-only t)))) |
1070 | |
1071 ;; Generate the Emerge difference list between two files with an ancestor | |
1072 (defun emerge-make-diff3-list (file-A file-B file-ancestor) | |
1073 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*")) | |
1074 (emerge-eval-in-buffer | |
1075 emerge-diff-buffer | |
1076 (erase-buffer) | |
1077 (shell-command | |
1078 (format "%s %s %s %s %s" | |
1079 emerge-diff3-program emerge-diff-options | |
1080 file-ancestor file-A file-B) | |
1081 t)) | |
1082 (emerge-prepare-error-list emerge-diff3-ok-lines) | |
1083 (emerge-convert-diffs-to-markers | |
1084 emerge-A-buffer emerge-B-buffer emerge-merge-buffer | |
1085 (emerge-extract-diffs3 emerge-diff-buffer))) | |
1086 | |
1087 (defun emerge-extract-diffs3 (diff-buffer) | |
1088 (let (list) | |
1089 (emerge-eval-in-buffer | |
1090 diff-buffer | |
1091 (while (re-search-forward "^====\\(.?\\)$" nil t) | |
1092 ;; leave point after matched line | |
1093 (beginning-of-line 2) | |
1094 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1)))) | |
1095 ;; if the A and B files are the same, ignore the difference | |
1096 (if (not (string-equal agreement "1")) | |
1097 (setq list | |
1098 (cons | |
1099 (let ((group-2 (emerge-get-diff3-group "2")) | |
1100 (group-3 (emerge-get-diff3-group "3"))) | |
1101 (vector (car group-2) (car (cdr group-2)) | |
1102 (car group-3) (car (cdr group-3)) | |
1103 (cond ((string-equal agreement "2") 'prefer-A) | |
1104 ((string-equal agreement "3") 'prefer-B) | |
1105 (t 'default-A)))) | |
1106 list)))))) | |
1107 (nreverse list))) | |
1108 | |
1109 (defun emerge-get-diff3-group (file) | |
1110 ;; This save-excursion allows emerge-get-diff3-group to be called for the | |
1111 ;; various groups of lines (1, 2, 3) in any order, and for the lines to | |
1112 ;; appear in any order. The reason this is necessary is that Gnu diff3 | |
1113 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2. | |
1114 (save-excursion | |
1115 (re-search-forward | |
1116 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$")) | |
1117 (beginning-of-line 2) | |
1118 ;; treatment depends on whether it is an "a" group or a "c" group | |
1119 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c") | |
1120 ;; it is a "c" group | |
1121 (if (match-beginning 2) | |
1122 ;; it has two numbers | |
1123 (list (string-to-int | |
1124 (buffer-substring (match-beginning 1) (match-end 1))) | |
1125 (1+ (string-to-int | |
1126 (buffer-substring (match-beginning 3) (match-end 3))))) | |
1127 ;; it has one number | |
1128 (let ((x (string-to-int | |
1129 (buffer-substring (match-beginning 1) (match-end 1))))) | |
1130 (list x (1+ x)))) | |
1131 ;; it is an "a" group | |
1132 (let ((x (1+ (string-to-int | |
1133 (buffer-substring (match-beginning 1) (match-end 1)))))) | |
1134 (list x x))))) | |
1135 | |
1136 ;;; Functions to start Emerge on files | |
1137 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1138 ;;;###autoload |
583 | 1139 (defun emerge-files (arg file-A file-B file-out &optional startup-hooks |
1140 quit-hooks) | |
1141 "Run Emerge on two files." | |
1142 (interactive | |
1143 (let (f) | |
1144 (list current-prefix-arg | |
1145 (setq f (read-file-name "File A to merge: " nil nil 'confirm)) | |
1146 (read-file-name "File B to merge: " nil nil 'confirm) | |
1147 (and current-prefix-arg | |
1148 (read-file-name | |
1149 (format "Output file: (default %s) " f) | |
1150 nil f nil))))) | |
1151 (emerge-files-internal | |
1152 file-A file-B startup-hooks | |
1153 (if arg | |
1154 (cons (` (lambda () (emerge-files-exit (, file-out)))) | |
1155 quit-hooks) | |
1156 quit-hooks) | |
1157 file-out)) | |
1158 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1159 ;;;###autoload |
583 | 1160 (defun emerge-files-with-ancestor (arg file-A file-B file-ancestor file-out |
1161 &optional startup-hooks quit-hooks) | |
1162 "Run Emerge on two files, giving another file as the ancestor." | |
1163 (interactive | |
1164 (let (f) | |
1165 (list current-prefix-arg | |
1166 (setq f (read-file-name "File A to merge: " nil nil 'confirm)) | |
1167 (read-file-name "File B to merge: " nil nil 'confirm) | |
1168 (read-file-name "Ancestor file: " nil nil 'confirm) | |
1169 (and current-prefix-arg | |
1170 (read-file-name | |
1171 (format "Output file: (default %s) " f) | |
1172 nil f nil))))) | |
1173 (emerge-files-with-ancestor-internal | |
1174 file-A file-B file-ancestor startup-hooks | |
1175 (if arg | |
1176 (cons (` (lambda () (emerge-files-exit (, file-out)))) | |
1177 quit-hooks) | |
1178 quit-hooks) | |
1179 file-out)) | |
1180 | |
1181 ;; Write the merge buffer out in place of the file the A buffer is visiting. | |
1182 (defun emerge-files-exit (file-out) | |
1183 ;; if merge was successful was given, save to disk | |
1184 (if (not emerge-prefix-argument) | |
1185 (emerge-write-and-delete file-out))) | |
1186 | |
1187 ;;; Functions to start Emerge on buffers | |
1188 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1189 ;;;###autoload |
583 | 1190 (defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks) |
1191 "Run Emerge on two buffers." | |
1192 (interactive "bBuffer A to merge: \nbBuffer B to merge: ") | |
1193 (let ((emerge-file-A (emerge-make-temp-file "A")) | |
1194 (emerge-file-B (emerge-make-temp-file "B"))) | |
1195 (emerge-eval-in-buffer | |
1196 buffer-A | |
1197 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)) | |
1198 (emerge-eval-in-buffer | |
1199 buffer-B | |
1200 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)) | |
1201 (emerge-setup (get-buffer buffer-A) emerge-file-A | |
1202 (get-buffer buffer-B) emerge-file-B | |
1203 (cons (function (lambda () | |
1204 (delete-file emerge-file-A) | |
1205 (delete-file emerge-file-B))) | |
1206 startup-hooks) | |
1207 quit-hooks | |
1208 nil))) | |
1209 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1210 ;;;###autoload |
583 | 1211 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor |
1212 &optional startup-hooks | |
1213 quit-hooks) | |
1214 "Run Emerge on two buffers, giving another buffer as the ancestor." | |
1215 (interactive | |
1216 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ") | |
1217 (let ((emerge-file-A (emerge-make-temp-file "A")) | |
1218 (emerge-file-B (emerge-make-temp-file "B")) | |
1219 (emerge-file-ancestor (emerge-make-temp-file "anc"))) | |
1220 (emerge-eval-in-buffer | |
1221 buffer-A | |
1222 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)) | |
1223 (emerge-eval-in-buffer | |
1224 buffer-B | |
1225 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)) | |
1226 (emerge-eval-in-buffer | |
1227 buffer-ancestor | |
1228 (write-region (point-min) (point-max) emerge-file-ancestor nil | |
1229 'no-message)) | |
1230 (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A | |
1231 (get-buffer buffer-B) emerge-file-B | |
1232 (get-buffer buffer-ancestor) | |
1233 emerge-file-ancestor | |
1234 (cons (function (lambda () | |
1235 (delete-file emerge-file-A) | |
1236 (delete-file emerge-file-B) | |
1237 (delete-file | |
1238 emerge-file-ancestor))) | |
1239 startup-hooks) | |
1240 quit-hooks | |
1241 nil))) | |
1242 | |
1243 ;;; Functions to start Emerge from the command line | |
1244 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1245 ;;;###autoload |
583 | 1246 (defun emerge-files-command () |
1247 (let ((file-a (nth 0 command-line-args-left)) | |
1248 (file-b (nth 1 command-line-args-left)) | |
1249 (file-out (nth 2 command-line-args-left))) | |
1250 (setq command-line-args-left (nthcdr 3 command-line-args-left)) | |
1251 (emerge-files-internal | |
1252 file-a file-b nil | |
1253 (list (` (lambda () (emerge-command-exit (, file-out)))))))) | |
1254 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1255 ;;;###autoload |
583 | 1256 (defun emerge-files-with-ancestor-command () |
1257 (let (file-a file-b file-anc file-out) | |
1258 ;; check for a -a flag, for filemerge compatibility | |
1259 (if (string= (car command-line-args-left) "-a") | |
1260 ;; arguments are "-a ancestor file-a file-b file-out" | |
1261 (progn | |
1262 (setq file-a (nth 2 command-line-args-left)) | |
1263 (setq file-b (nth 3 command-line-args-left)) | |
1264 (setq file-anc (nth 1 command-line-args-left)) | |
1265 (setq file-out (nth 4 command-line-args-left)) | |
1266 (setq command-line-args-left (nthcdr 5 command-line-args-left))) | |
1267 ;; arguments are "file-a file-b ancestor file-out" | |
1268 (setq file-a (nth 0 command-line-args-left)) | |
1269 (setq file-b (nth 1 command-line-args-left)) | |
1270 (setq file-anc (nth 2 command-line-args-left)) | |
1271 (setq file-out (nth 3 command-line-args-left)) | |
1272 (setq command-line-args-left (nthcdr 4 command-line-args-left))) | |
1273 (emerge-files-with-ancestor-internal | |
1274 file-a file-b file-anc nil | |
1275 (list (` (lambda () (emerge-command-exit (, file-out)))))))) | |
1276 | |
1277 (defun emerge-command-exit (file-out) | |
1278 (emerge-write-and-delete file-out) | |
1279 (kill-emacs (if emerge-prefix-argument 1 0))) | |
1280 | |
1281 ;;; Functions to start Emerge via remote request | |
1282 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1283 ;;;###autoload |
583 | 1284 (defun emerge-files-remote (file-a file-b file-out) |
1285 (setq emerge-file-out file-out) | |
1286 (emerge-files-internal | |
1287 file-a file-b nil | |
1288 (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func))))) | |
1289 file-out) | |
1290 (throw 'client-wait nil)) | |
1291 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
1292 ;;;###autoload |
583 | 1293 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out) |
1294 (setq emerge-file-out file-out) | |
1295 (emerge-files-with-ancestor-internal | |
1296 file-a file-b file-anc nil | |
1297 (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func))))) | |
1298 file-out) | |
1299 (throw 'client-wait nil)) | |
1300 | |
1301 (defun emerge-remote-exit (file-out exit-func) | |
1302 (emerge-write-and-delete file-out) | |
1303 (kill-buffer emerge-merge-buffer) | |
1304 (funcall exit-func (if emerge-prefix-argument 1 0))) | |
1305 | |
1306 ;;; Common setup routines | |
1307 | |
1308 ;; Set up the window configuration. If POS is given, set the points to | |
1309 ;; the beginnings of the buffers. | |
1310 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer &optional pos) | |
1311 ;; Make sure we are not in the minibuffer window when we try to delete | |
1312 ;; all other windows. | |
1313 (if (eq (selected-window) (minibuffer-window)) | |
1314 (other-window 1)) | |
1315 (delete-other-windows) | |
1316 (switch-to-buffer merge-buffer) | |
1317 (emerge-refresh-mode-line) | |
1318 (split-window-vertically) | |
1319 (split-window-horizontally) | |
1320 (switch-to-buffer buffer-A) | |
1321 (if pos | |
1322 (goto-char (point-min))) | |
1323 (other-window 1) | |
1324 (switch-to-buffer buffer-B) | |
1325 (if pos | |
1326 (goto-char (point-min))) | |
1327 (other-window 1) | |
1328 (if pos | |
1329 (goto-char (point-min))) | |
1330 ;; If diff/diff3 reports errors, display them rather than the merge buffer. | |
1331 (if (/= 0 (emerge-eval-in-buffer emerge-diff-error-buffer (buffer-size))) | |
1332 (progn | |
1333 (ding) | |
1334 (message "Errors found in diff/diff3 output. Merge buffer is %s." | |
1335 (buffer-name emerge-merge-buffer)) | |
1336 (switch-to-buffer emerge-diff-error-buffer)))) | |
1337 | |
1338 ;; Set up the keymap in the merge buffer | |
1339 (defun emerge-set-keys () | |
1340 ;; Set up fixed keymaps if necessary | |
1341 (if (not emerge-basic-keymap) | |
1342 (emerge-setup-fixed-keymaps)) | |
1343 ;; Save the old local map | |
1344 (setq emerge-old-keymap (current-local-map)) | |
1345 ;; Construct the edit keymap | |
1346 (setq emerge-edit-keymap (if emerge-old-keymap | |
1347 (copy-keymap emerge-old-keymap) | |
1348 (make-sparse-keymap))) | |
1349 ;; Install the Emerge commands | |
1350 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix | |
1351 'emerge-basic-keymap) | |
1352 ;; Suppress write-file and save-buffer | |
1353 (emerge-recursively-substitute-key-definition 'write-file | |
1354 'emerge-query-write-file | |
1355 emerge-edit-keymap) | |
1356 (emerge-recursively-substitute-key-definition 'save-buffer | |
1357 'emerge-query-save-buffer | |
1358 emerge-edit-keymap) | |
1359 (emerge-shadow-key-definition 'write-file 'emerge-query-write-file | |
1360 (current-global-map) emerge-edit-keymap) | |
1361 (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer | |
1362 (current-global-map) emerge-edit-keymap) | |
1363 (use-local-map emerge-fast-keymap) | |
1364 (setq emerge-edit-mode nil) | |
1365 (setq emerge-fast-mode t)) | |
1366 | |
1367 (defun emerge-remember-buffer-characteristics () | |
1368 "Must be called in the merge buffer. Remembers certain properties of the | |
1369 buffers being merged (read-only, modified, auto-save), and saves them in | |
1370 buffer local variables. Sets the buffers read-only and turns off auto-save. | |
1371 These characteristics are restored by emerge-restore-buffer-characteristics." | |
1372 ;; force auto-save, because we will turn off auto-saving in buffers for the | |
1373 ;; duration | |
1374 (do-auto-save) | |
1375 ;; remember and alter buffer characteristics | |
1376 (setq emerge-A-buffer-values | |
1377 (emerge-eval-in-buffer | |
1378 emerge-A-buffer | |
1379 (prog1 | |
1380 (emerge-save-variables emerge-saved-variables) | |
1381 (emerge-restore-variables emerge-saved-variables | |
1382 emerge-merging-values)))) | |
1383 (setq emerge-B-buffer-values | |
1384 (emerge-eval-in-buffer | |
1385 emerge-B-buffer | |
1386 (prog1 | |
1387 (emerge-save-variables emerge-saved-variables) | |
1388 (emerge-restore-variables emerge-saved-variables | |
1389 emerge-merging-values))))) | |
1390 | |
1391 (defun emerge-restore-buffer-characteristics () | |
1392 "Restores the characteristics remembered by | |
1393 emerge-remember-buffer-characteristics." | |
1394 (let ((A-values emerge-A-buffer-values) | |
1395 (B-values emerge-B-buffer-values)) | |
1396 (emerge-eval-in-buffer emerge-A-buffer | |
1397 (emerge-restore-variables emerge-saved-variables | |
1398 A-values)) | |
1399 (emerge-eval-in-buffer emerge-B-buffer | |
1400 (emerge-restore-variables emerge-saved-variables | |
1401 B-values)))) | |
1402 | |
1403 (defun emerge-convert-diffs-to-markers (A-buffer | |
1404 B-buffer | |
1405 merge-buffer | |
1406 lineno-list) | |
1407 (let* (marker-list | |
1408 (A-point-min (emerge-eval-in-buffer A-buffer (point-min))) | |
1409 (offset (1- A-point-min)) | |
1410 (A-hidden-lines (emerge-eval-in-buffer | |
1411 A-buffer | |
1412 (save-restriction | |
1413 (widen) | |
1414 (count-lines 1 A-point-min)))) | |
1415 (B-point-min (emerge-eval-in-buffer B-buffer (point-min))) | |
1416 (B-hidden-lines (emerge-eval-in-buffer | |
1417 B-buffer | |
1418 (save-restriction | |
1419 (widen) | |
1420 (count-lines 1 B-point-min))))) | |
1421 (while lineno-list | |
1422 (let* ((list-element (car lineno-list)) | |
1423 a-begin-marker | |
1424 a-end-marker | |
1425 b-begin-marker | |
1426 b-end-marker | |
1427 (a-begin (aref list-element 0)) | |
1428 (a-end (aref list-element 1)) | |
1429 (b-begin (aref list-element 2)) | |
1430 (b-end (aref list-element 3)) | |
1431 (state (aref list-element 4))) | |
1432 ;; place markers at the appropriate places in the buffers | |
1433 (emerge-eval-in-buffer | |
1434 A-buffer | |
1435 (goto-line (+ a-begin A-hidden-lines)) | |
1436 (setq a-begin-marker (point-marker)) | |
1437 (goto-line (+ a-end A-hidden-lines)) | |
1438 (setq a-end-marker (point-marker))) | |
1439 (emerge-eval-in-buffer | |
1440 B-buffer | |
1441 (goto-line (+ b-begin B-hidden-lines)) | |
1442 (setq b-begin-marker (point-marker)) | |
1443 (goto-line (+ b-end B-hidden-lines)) | |
1444 (setq b-end-marker (point-marker))) | |
1445 (setq merge-begin-marker (set-marker | |
1446 (make-marker) | |
1447 (- (marker-position a-begin-marker) | |
1448 offset) | |
1449 merge-buffer)) | |
1450 (setq merge-end-marker (set-marker | |
1451 (make-marker) | |
1452 (- (marker-position a-end-marker) | |
1453 offset) | |
1454 merge-buffer)) | |
1455 ;; record all the markers for this difference | |
1456 (setq marker-list (cons (vector a-begin-marker a-end-marker | |
1457 b-begin-marker b-end-marker | |
1458 merge-begin-marker merge-end-marker | |
1459 state) | |
1460 marker-list))) | |
1461 (setq lineno-list (cdr lineno-list))) | |
1462 ;; convert the list of difference information into a vector for | |
1463 ;; fast access | |
1464 (setq emerge-difference-list (apply 'vector (nreverse marker-list))))) | |
1465 | |
1466 ;; If we have an ancestor, select all B variants that we prefer | |
1467 (defun emerge-select-prefer-Bs () | |
1468 (let ((n 0)) | |
1469 (while (< n emerge-number-of-differences) | |
1470 (if (eq (aref (aref emerge-difference-list n) 6) 'prefer-B) | |
1471 (progn | |
1472 (emerge-unselect-and-select-difference n t) | |
1473 (emerge-select-B) | |
1474 (aset (aref emerge-difference-list n) 6 'prefer-B))) | |
1475 (setq n (1+ n)))) | |
1476 (emerge-unselect-and-select-difference -1)) | |
1477 | |
1478 ;;; Common exit routines | |
1479 | |
1480 (defun emerge-write-and-delete (file-out) | |
1481 ;; clear screen format | |
1482 (delete-other-windows) | |
1483 ;; delete A, B, and ancestor buffers, if they haven't been changed | |
1484 (if (not (buffer-modified-p emerge-A-buffer)) | |
1485 (kill-buffer emerge-A-buffer)) | |
1486 (if (not (buffer-modified-p emerge-B-buffer)) | |
1487 (kill-buffer emerge-B-buffer)) | |
1488 (if (and emerge-ancestor-buffer | |
1489 (not (buffer-modified-p emerge-ancestor-buffer))) | |
1490 (kill-buffer emerge-ancestor-buffer)) | |
1491 ;; Write merge buffer to file | |
1492 (write-file file-out)) | |
1493 | |
1494 ;;; Commands | |
1495 | |
1496 (defun emerge-recenter (&optional arg) | |
1497 "Bring the highlighted region of all three merge buffers into view, | |
1498 if they are in windows. If an ARGUMENT is given, the default three-window | |
1499 display is reestablished." | |
1500 (interactive "P") | |
1501 ;; If there is an argument, rebuild the window structure | |
1502 (if arg | |
1503 (emerge-setup-windows emerge-A-buffer emerge-B-buffer | |
1504 emerge-merge-buffer)) | |
1505 ;; Redisplay whatever buffers are showing, if there is a selected difference | |
1506 (if (and (>= emerge-current-difference 0) | |
1507 (< emerge-current-difference emerge-number-of-differences)) | |
1508 (let* ((merge-buffer emerge-merge-buffer) | |
1509 (buffer-A emerge-A-buffer) | |
1510 (buffer-B emerge-B-buffer) | |
1511 (window-A (get-buffer-window buffer-A)) | |
1512 (window-B (get-buffer-window buffer-B)) | |
1513 (merge-window (get-buffer-window merge-buffer)) | |
1514 (diff-vector | |
1515 (aref emerge-difference-list emerge-current-difference))) | |
1516 (if window-A (progn | |
1517 (select-window window-A) | |
1518 (emerge-position-region | |
1519 (- (aref diff-vector 0) | |
1520 (1- emerge-before-flag-length)) | |
1521 (+ (aref diff-vector 1) | |
1522 (1- emerge-after-flag-length)) | |
1523 (1+ (aref diff-vector 0))))) | |
1524 (if window-B (progn | |
1525 (select-window window-B) | |
1526 (emerge-position-region | |
1527 (- (aref diff-vector 2) | |
1528 (1- emerge-before-flag-length)) | |
1529 (+ (aref diff-vector 3) | |
1530 (1- emerge-after-flag-length)) | |
1531 (1+ (aref diff-vector 2))))) | |
1532 (if merge-window (progn | |
1533 (select-window merge-window) | |
1534 (emerge-position-region | |
1535 (- (aref diff-vector 4) | |
1536 (1- emerge-before-flag-length)) | |
1537 (+ (aref diff-vector 5) | |
1538 (1- emerge-after-flag-length)) | |
1539 (1+ (aref diff-vector 4)))))))) | |
1540 | |
1541 ;;; Window scrolling operations | |
1542 ;; These operations are designed to scroll all three windows the same amount, | |
1543 ;; so as to keep the text in them aligned. | |
1544 | |
1545 ;; Perform some operation on all three windows (if they are showing). | |
1546 ;; Catches all errors on the operation in the A and B windows, but not | |
1547 ;; in the merge window. Usually, errors come from scrolling off the | |
1548 ;; beginning or end of the buffer, and this gives a nice error message: | |
1549 ;; End of buffer is reported in the merge buffer, but if the scroll was | |
1550 ;; possible in the A or B windows, it is performed there before the error | |
1551 ;; is reported. | |
1552 (defun emerge-operate-on-windows (operation arg) | |
1553 (let* ((merge-buffer emerge-merge-buffer) | |
1554 (buffer-A emerge-A-buffer) | |
1555 (buffer-B emerge-B-buffer) | |
1556 (window-A (get-buffer-window buffer-A)) | |
1557 (window-B (get-buffer-window buffer-B)) | |
1558 (merge-window (get-buffer-window merge-buffer))) | |
1559 (if window-A (progn | |
1560 (select-window window-A) | |
1561 (condition-case nil | |
1562 (funcall operation arg) | |
1563 (error)))) | |
1564 (if window-B (progn | |
1565 (select-window window-B) | |
1566 (condition-case nil | |
1567 (funcall operation arg) | |
1568 (error)))) | |
1569 (if merge-window (progn | |
1570 (select-window merge-window) | |
1571 (funcall operation arg))))) | |
1572 | |
1573 (defun emerge-scroll-up (&optional arg) | |
1574 "Scroll up all three merge buffers, if they are in windows. | |
1575 If an ARGUMENT is given, that is how many lines are scrolled, else nearly | |
1576 the size of the merge window. `C-u -' alone as argument scrolls half the | |
1577 size of the merge window." | |
1578 (interactive "P") | |
1579 (emerge-operate-on-windows | |
1580 'scroll-up | |
1581 ;; calculate argument to scroll-up | |
1582 ;; if there is an explicit argument | |
1583 (if (and arg (not (equal arg '-))) | |
1584 ;; use it | |
1585 (prefix-numeric-value arg) | |
1586 ;; if not, see if we can determine a default amount (the window height) | |
1587 (let ((merge-window (get-buffer-window emerge-merge-buffer))) | |
1588 (if (null merge-window) | |
1589 ;; no window, use nil | |
1590 nil | |
1591 (let ((default-amount | |
1592 (- (window-height merge-window) 1 next-screen-context-lines))) | |
1593 ;; the window was found | |
1594 (if arg | |
1595 ;; C-u as argument means half of default amount | |
1596 (/ default-amount 2) | |
1597 ;; no argument means default amount | |
1598 default-amount))))))) | |
1599 | |
1600 (defun emerge-scroll-down (&optional arg) | |
1601 "Scroll down all three merge buffers, if they are in windows. | |
1602 If an ARGUMENT is given, that is how many lines are scrolled, else nearly | |
1603 the size of the merge window. `C-u -' alone as argument scrolls half the | |
1604 size of the merge window." | |
1605 (interactive "P") | |
1606 (emerge-operate-on-windows | |
1607 'scroll-down | |
1608 ;; calculate argument to scroll-down | |
1609 ;; if there is an explicit argument | |
1610 (if (and arg (not (equal arg '-))) | |
1611 ;; use it | |
1612 (prefix-numeric-value arg) | |
1613 ;; if not, see if we can determine a default amount (the window height) | |
1614 (let ((merge-window (get-buffer-window emerge-merge-buffer))) | |
1615 (if (null merge-window) | |
1616 ;; no window, use nil | |
1617 nil | |
1618 (let ((default-amount | |
1619 (- (window-height merge-window) 1 next-screen-context-lines))) | |
1620 ;; the window was found | |
1621 (if arg | |
1622 ;; C-u as argument means half of default amount | |
1623 (/ default-amount 2) | |
1624 ;; no argument means default amount | |
1625 default-amount))))))) | |
1626 | |
1627 (defun emerge-scroll-left (&optional arg) | |
1628 "Scroll left all three merge buffers, if they are in windows. | |
1629 If an ARGUMENT is given, that is how many columns are scrolled, else nearly | |
1630 the width of the A and B windows. `C-u -' alone as argument scrolls half the | |
1631 width of the A and B windows." | |
1632 (interactive "P") | |
1633 (emerge-operate-on-windows | |
1634 'scroll-left | |
1635 ;; calculate argument to scroll-left | |
1636 ;; if there is an explicit argument | |
1637 (if (and arg (not (equal arg '-))) | |
1638 ;; use it | |
1639 (prefix-numeric-value arg) | |
1640 ;; if not, see if we can determine a default amount | |
1641 ;; (half the window width) | |
1642 (let ((merge-window (get-buffer-window emerge-merge-buffer))) | |
1643 (if (null merge-window) | |
1644 ;; no window, use nil | |
1645 nil | |
1646 (let ((default-amount | |
1647 (- (/ (window-width merge-window) 2) 3))) | |
1648 ;; the window was found | |
1649 (if arg | |
1650 ;; C-u as argument means half of default amount | |
1651 (/ default-amount 2) | |
1652 ;; no argument means default amount | |
1653 default-amount))))))) | |
1654 | |
1655 (defun emerge-scroll-right (&optional arg) | |
1656 "Scroll right all three merge buffers, if they are in windows. | |
1657 If an ARGUMENT is given, that is how many columns are scrolled, else nearly | |
1658 the width of the A and B windows. `C-u -' alone as argument scrolls half the | |
1659 width of the A and B windows." | |
1660 (interactive "P") | |
1661 (emerge-operate-on-windows | |
1662 'scroll-right | |
1663 ;; calculate argument to scroll-right | |
1664 ;; if there is an explicit argument | |
1665 (if (and arg (not (equal arg '-))) | |
1666 ;; use it | |
1667 (prefix-numeric-value arg) | |
1668 ;; if not, see if we can determine a default amount | |
1669 ;; (half the window width) | |
1670 (let ((merge-window (get-buffer-window emerge-merge-buffer))) | |
1671 (if (null merge-window) | |
1672 ;; no window, use nil | |
1673 nil | |
1674 (let ((default-amount | |
1675 (- (/ (window-width merge-window) 2) 3))) | |
1676 ;; the window was found | |
1677 (if arg | |
1678 ;; C-u as argument means half of default amount | |
1679 (/ default-amount 2) | |
1680 ;; no argument means default amount | |
1681 default-amount))))))) | |
1682 | |
1683 (defun emerge-scroll-reset () | |
1684 "Reset horizontal scrolling of all three merge buffers to the left margin, | |
1685 if they are in windows." | |
1686 (interactive) | |
1687 (emerge-operate-on-windows | |
1688 (function (lambda (x) (set-window-hscroll (selected-window) 0))) | |
1689 nil)) | |
1690 | |
1691 ;; Attempt to show the region nicely. | |
1692 ;; If there are min-lines lines above and below the region, then don't do | |
1693 ;; anything. | |
1694 ;; If not, recenter the region to make it so. | |
1695 ;; If that isn't possible, remove context lines balancedly from top and botton | |
1696 ;; so the entire region shows. | |
1697 ;; If that isn't possible, show the top of the region. | |
1698 ;; BEG must be at the beginning of a line. | |
1699 (defun emerge-position-region (beg end pos) | |
1700 ;; First test whether the entire region is visible with | |
1701 ;; emerge-min-visible-lines above and below it | |
1702 (if (not (and (<= (progn | |
1703 (move-to-window-line emerge-min-visible-lines) | |
1704 (point)) | |
1705 beg) | |
1706 (<= end (progn | |
1707 (move-to-window-line | |
1708 (- (1+ emerge-min-visible-lines))) | |
1709 (point))))) | |
1710 ;; We failed that test, see if it fits at all | |
1711 ;; Meanwhile positioning it correctly in case it doesn't fit | |
1712 (progn | |
1713 (set-window-start (selected-window) beg) | |
1714 (setq fits (pos-visible-in-window-p end)) | |
1715 (if fits | |
1716 ;; Determine the number of lines that the region occupies | |
1717 (let ((lines 0)) | |
1718 (while (> end (progn | |
1719 (move-to-window-line lines) | |
1720 (point))) | |
1721 (setq lines (1+ lines))) | |
1722 ;; And position the beginning on the right line | |
1723 (goto-char beg) | |
1724 (recenter (/ (1+ (- (1- (window-height (selected-window))) | |
1725 lines)) | |
1726 2)))))) | |
1727 (goto-char pos)) | |
1728 | |
1729 (defun emerge-next-difference () | |
1730 "Advance to the next difference." | |
1731 (interactive) | |
1732 (if (< emerge-current-difference emerge-number-of-differences) | |
1733 (let ((n (1+ emerge-current-difference))) | |
1734 (while (and emerge-skip-prefers | |
1735 (< n emerge-number-of-differences) | |
1736 (memq (aref (aref emerge-difference-list n) 6) | |
1737 '(prefer-A prefer-B))) | |
1738 (setq n (1+ n))) | |
1739 (let ((buffer-read-only nil)) | |
1740 (emerge-unselect-and-select-difference n))) | |
1741 (error "At end"))) | |
1742 | |
1743 (defun emerge-previous-difference () | |
1744 "Go to the previous difference." | |
1745 (interactive) | |
1746 (if (> emerge-current-difference -1) | |
1747 (let ((n (1- emerge-current-difference))) | |
1748 (while (and emerge-skip-prefers | |
1749 (> n -1) | |
1750 (memq (aref (aref emerge-difference-list n) 6) | |
1751 '(prefer-A prefer-B))) | |
1752 (setq n (1- n))) | |
1753 (let ((buffer-read-only nil)) | |
1754 (emerge-unselect-and-select-difference n))) | |
1755 (error "At beginning"))) | |
1756 | |
1757 (defun emerge-jump-to-difference (difference-number) | |
1758 "Go to the N-th difference." | |
1759 (interactive "p") | |
1760 (let ((buffer-read-only nil)) | |
1761 (setq difference-number (1- difference-number)) | |
1762 (if (and (>= difference-number -1) | |
1763 (< difference-number (1+ emerge-number-of-differences))) | |
1764 (emerge-unselect-and-select-difference difference-number) | |
1765 (error "Bad difference number")))) | |
1766 | |
1767 (defun emerge-quit (arg) | |
1768 "Finish an Emerge session. Prefix ARGUMENT means to abort rather than | |
1769 successfully finish. The difference depends on how the merge was started, | |
1770 but usually means to not write over one of the original files, or to signal | |
1771 to some process which invoked Emerge a failure code. | |
1772 | |
1773 Unselects the selected difference, if any, restores the read-only and modified | |
1774 flags of the merged file buffers, restores the local keymap of the merge | |
1775 buffer, and sets off various emerge flags. Using Emerge commands in this | |
1776 buffer after this will cause serious problems." | |
1777 (interactive "P") | |
1778 (if (prog1 | |
1779 (y-or-n-p | |
1780 (if (not arg) | |
1781 "Do you really want to successfully finish this merge? " | |
1782 "Do you really want to abort this merge? ")) | |
1783 (message "")) | |
1784 (emerge-really-quit arg))) | |
1785 | |
1786 ;; Perform the quit operations. | |
1787 (defun emerge-really-quit (arg) | |
1788 (setq buffer-read-only nil) | |
1789 (emerge-unselect-and-select-difference -1) | |
1790 (emerge-restore-buffer-characteristics) | |
1791 ;; null out the difference markers so they don't slow down future editing | |
1792 ;; operations | |
1793 (mapcar (function (lambda (d) | |
1794 (set-marker (aref d 0) nil) | |
1795 (set-marker (aref d 1) nil) | |
1796 (set-marker (aref d 2) nil) | |
1797 (set-marker (aref d 3) nil) | |
1798 (set-marker (aref d 4) nil) | |
1799 (set-marker (aref d 5) nil))) | |
1800 emerge-difference-list) | |
1801 ;; allow them to be garbage collected | |
1802 (setq emerge-difference-list nil) | |
1803 ;; restore the local map | |
1804 (use-local-map emerge-old-keymap) | |
1805 ;; turn off all the emerge modes | |
1806 (setq emerge-mode nil) | |
1807 (setq emerge-fast-mode nil) | |
1808 (setq emerge-edit-mode nil) | |
1809 (setq emerge-auto-advance nil) | |
1810 (setq emerge-skip-prefers nil) | |
1811 ;; restore mode line | |
1812 (kill-local-variable 'mode-line-buffer-identification) | |
1813 (let ((emerge-prefix-argument arg)) | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
778
diff
changeset
|
1814 (run-hooks 'emerge-quit-hook))) |
583 | 1815 |
1816 (defun emerge-select-A (&optional force) | |
1817 "Select the A variant of this difference. Refuses to function if this | |
1818 difference has been edited, i.e., if it is neither the A nor the B variant. | |
1819 An ARGUMENT forces the variant to be selected even if the difference has | |
1820 been edited." | |
1821 (interactive "P") | |
1822 (let ((operate | |
1823 (function (lambda () | |
1824 (emerge-select-A-edit merge-begin merge-end A-begin A-end) | |
1825 (if emerge-auto-advance | |
1826 (emerge-next-difference))))) | |
1827 (operate-no-change | |
1828 (function (lambda () | |
1829 (if emerge-auto-advance | |
1830 (emerge-next-difference)))))) | |
1831 (emerge-select-version force operate-no-change operate operate))) | |
1832 | |
1833 ;; Actually select the A variant | |
1834 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end) | |
1835 (emerge-eval-in-buffer | |
1836 emerge-merge-buffer | |
1837 (delete-region merge-begin merge-end) | |
1838 (goto-char merge-begin) | |
1839 (insert-buffer-substring emerge-A-buffer A-begin A-end) | |
1840 (goto-char merge-begin) | |
1841 (aset diff-vector 6 'A) | |
1842 (emerge-refresh-mode-line))) | |
1843 | |
1844 (defun emerge-select-B (&optional force) | |
1845 "Select the B variant of this difference. Refuses to function if this | |
1846 difference has been edited, i.e., if it is neither the A nor the B variant. | |
1847 An ARGUMENT forces the variant to be selected even if the difference has | |
1848 been edited." | |
1849 (interactive "P") | |
1850 (let ((operate | |
1851 (function (lambda () | |
1852 (emerge-select-B-edit merge-begin merge-end B-begin B-end) | |
1853 (if emerge-auto-advance | |
1854 (emerge-next-difference))))) | |
1855 (operate-no-change | |
1856 (function (lambda () | |
1857 (if emerge-auto-advance | |
1858 (emerge-next-difference)))))) | |
1859 (emerge-select-version force operate operate-no-change operate))) | |
1860 | |
1861 ;; Actually select the B variant | |
1862 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end) | |
1863 (emerge-eval-in-buffer | |
1864 emerge-merge-buffer | |
1865 (delete-region merge-begin merge-end) | |
1866 (goto-char merge-begin) | |
1867 (insert-buffer-substring emerge-B-buffer B-begin B-end) | |
1868 (goto-char merge-begin) | |
1869 (aset diff-vector 6 'B) | |
1870 (emerge-refresh-mode-line))) | |
1871 | |
1872 (defun emerge-default-A () | |
1873 "Selects the A variant for all differences from here down in the buffer | |
1874 which are still defaulted, i.e., which the user has not selected and for | |
1875 which there is no preference." | |
1876 (interactive) | |
1877 (let ((buffer-read-only nil)) | |
1878 (let ((selected-difference emerge-current-difference) | |
1879 (n (max emerge-current-difference 0))) | |
1880 (while (< n emerge-number-of-differences) | |
1881 (let ((diff-vector (aref emerge-difference-list n))) | |
1882 (if (eq (aref diff-vector 6) 'default-B) | |
1883 (progn | |
1884 (emerge-unselect-and-select-difference n t) | |
1885 (emerge-select-A) | |
1886 (aset diff-vector 6 'default-A)))) | |
1887 (setq n (1+ n)) | |
1888 (if (= (* (/ n 10) 10) n) | |
1889 (message "Setting default to A...%d" n))) | |
1890 (emerge-unselect-and-select-difference selected-difference))) | |
1891 (message "Default A set")) | |
1892 | |
1893 (defun emerge-default-B () | |
1894 "Selects the B variant for all differences from here down in the buffer | |
1895 which are still defaulted, i.e., which the user has not selected and for | |
1896 which there is no preference." | |
1897 (interactive) | |
1898 (let ((buffer-read-only nil)) | |
1899 (let ((selected-difference emerge-current-difference) | |
1900 (n (max emerge-current-difference 0))) | |
1901 (while (< n emerge-number-of-differences) | |
1902 (let ((diff-vector (aref emerge-difference-list n))) | |
1903 (if (eq (aref diff-vector 6) 'default-A) | |
1904 (progn | |
1905 (emerge-unselect-and-select-difference n t) | |
1906 (emerge-select-B) | |
1907 (aset diff-vector 6 'default-B)))) | |
1908 (setq n (1+ n)) | |
1909 (if (= (* (/ n 10) 10) n) | |
1910 (message "Setting default to B...%d" n))) | |
1911 (emerge-unselect-and-select-difference selected-difference))) | |
1912 (message "Default B set")) | |
1913 | |
1914 (defun emerge-fast-mode () | |
1915 "Set fast mode, in which ordinary Emacs commands are disabled, and Emerge | |
1916 commands are need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]." | |
1917 (interactive) | |
1918 (setq buffer-read-only t) | |
1919 (use-local-map emerge-fast-keymap) | |
1920 (setq emerge-mode t) | |
1921 (setq emerge-fast-mode t) | |
1922 (setq emerge-edit-mode nil) | |
1923 (message "Fast mode set") | |
1924 ;; force mode line redisplay | |
1925 (set-buffer-modified-p (buffer-modified-p))) | |
1926 | |
1927 (defun emerge-edit-mode () | |
1928 "Set edit mode, in which ordinary Emacs commands are available, and Emerge | |
1929 commands must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]." | |
1930 (interactive) | |
1931 (setq buffer-read-only nil) | |
1932 (use-local-map emerge-edit-keymap) | |
1933 (setq emerge-mode t) | |
1934 (setq emerge-fast-mode nil) | |
1935 (setq emerge-edit-mode t) | |
1936 (message "Edit mode set") | |
1937 ;; force mode line redisplay | |
1938 (set-buffer-modified-p (buffer-modified-p))) | |
1939 | |
1940 (defun emerge-auto-advance (arg) | |
1941 "Toggle auto-advance mode, which causes emerge-select-A and | |
1942 emerge-select-B to automatically advance to the next difference. (See | |
1943 emerge-auto-advance.) | |
1944 If a positive ARGUMENT is given, it turns on auto-advance mode. | |
1945 If a negative ARGUMENT is given, it turns off auto-advance mode." | |
1946 (interactive "P") | |
1947 (setq emerge-auto-advance (if (null arg) | |
1948 (not emerge-auto-advance) | |
1949 (> (prefix-numeric-value arg) 0))) | |
1950 (message (if emerge-skip-prefers | |
1951 "Auto-advance set" | |
1952 "Auto-advance cleared")) | |
1953 ;; force mode line redisplay | |
1954 (set-buffer-modified-p (buffer-modified-p))) | |
1955 | |
1956 (defun emerge-skip-prefers (arg) | |
1957 "Toggle skip-prefers mode, which causes emerge-next-difference and | |
1958 emerge-previous-difference to automatically skip over differences for which | |
1959 there is a preference. (See emerge-skip-prefers.) | |
1960 If a positive ARGUMENT is given, it turns on skip-prefers mode. | |
1961 If a negative ARGUMENT is given, it turns off skip-prefers mode." | |
1962 (interactive "P") | |
1963 (setq emerge-skip-prefers (if (null arg) | |
1964 (not emerge-skip-prefers) | |
1965 (> (prefix-numeric-value arg) 0))) | |
1966 (message (if emerge-skip-prefers | |
1967 "Skip-prefers set" | |
1968 "Skip-prefers cleared")) | |
1969 ;; force mode line redisplay | |
1970 (set-buffer-modified-p (buffer-modified-p))) | |
1971 | |
1972 (defun emerge-copy-as-kill-A () | |
1973 "Put the A variant of this difference in the kill ring." | |
1974 (interactive) | |
1975 (emerge-validate-difference) | |
1976 (let* ((diff-vector | |
1977 (aref emerge-difference-list emerge-current-difference)) | |
1978 (A-begin (1+ (aref diff-vector 0))) | |
1979 (A-end (1- (aref diff-vector 1))) | |
1980 ;; so further kills don't append | |
1981 this-command) | |
1982 (save-excursion | |
1983 (set-buffer emerge-A-buffer) | |
1984 (copy-region-as-kill A-begin A-end)))) | |
1985 | |
1986 (defun emerge-copy-as-kill-B () | |
1987 "Put the B variant of this difference in the kill ring." | |
1988 (interactive) | |
1989 (emerge-validate-difference) | |
1990 (let* ((diff-vector | |
1991 (aref emerge-difference-list emerge-current-difference)) | |
1992 (B-begin (1+ (aref diff-vector 2))) | |
1993 (B-end (1- (aref diff-vector 3))) | |
1994 ;; so further kills don't append | |
1995 this-command) | |
1996 (save-excursion | |
1997 (set-buffer emerge-B-buffer) | |
1998 (copy-region-as-kill B-begin B-end)))) | |
1999 | |
2000 (defun emerge-insert-A (arg) | |
2001 "Insert the A variant of this difference at the point. | |
2002 Leaves point after text, mark before. | |
2003 With prefix argument, puts point before, mark after." | |
2004 (interactive "P") | |
2005 (emerge-validate-difference) | |
2006 (let* ((diff-vector | |
2007 (aref emerge-difference-list emerge-current-difference)) | |
2008 (A-begin (1+ (aref diff-vector 0))) | |
2009 (A-end (1- (aref diff-vector 1))) | |
2010 (opoint (point)) | |
2011 (buffer-read-only nil)) | |
2012 (insert-buffer-substring emerge-A-buffer A-begin A-end) | |
2013 (if (not arg) | |
2014 (set-mark opoint) | |
2015 (set-mark (point)) | |
2016 (goto-char opoint)))) | |
2017 | |
2018 (defun emerge-insert-B (arg) | |
2019 "Insert the B variant of this difference at the point. | |
2020 Leaves point after text, mark before. | |
2021 With prefix argument, puts point before, mark after." | |
2022 (interactive "P") | |
2023 (emerge-validate-difference) | |
2024 (let* ((diff-vector | |
2025 (aref emerge-difference-list emerge-current-difference)) | |
2026 (B-begin (1+ (aref diff-vector 2))) | |
2027 (B-end (1- (aref diff-vector 3))) | |
2028 (opoint (point)) | |
2029 (buffer-read-only nil)) | |
2030 (insert-buffer-substring emerge-B-buffer B-begin B-end) | |
2031 (if (not arg) | |
2032 (set-mark opoint) | |
2033 (set-mark (point)) | |
2034 (goto-char opoint)))) | |
2035 | |
2036 (defun emerge-mark-difference (arg) | |
2037 "Leaves the point before this difference and the mark after it. | |
2038 With prefix argument, puts mark before, point after." | |
2039 (interactive "P") | |
2040 (emerge-validate-difference) | |
2041 (let* ((diff-vector | |
2042 (aref emerge-difference-list emerge-current-difference)) | |
2043 (merge-begin (1+ (aref diff-vector 4))) | |
2044 (merge-end (1- (aref diff-vector 5)))) | |
2045 (if (not arg) | |
2046 (progn | |
2047 (goto-char merge-begin) | |
2048 (set-mark merge-end)) | |
2049 (goto-char merge-end) | |
2050 (set-mark merge-begin)))) | |
2051 | |
2052 (defun emerge-file-names () | |
2053 "Show the names of the buffers or files being operated on by Emerge. | |
2054 Use ^U L to reset the windows afterward." | |
2055 (interactive) | |
2056 (delete-other-windows) | |
2057 (let ((temp-buffer-show-hook | |
2058 (function (lambda (buf) | |
2059 (split-window-vertically) | |
2060 (switch-to-buffer buf) | |
2061 (other-window 1))))) | |
2062 (with-output-to-temp-buffer "*Help*" | |
2063 (emerge-eval-in-buffer emerge-A-buffer | |
2064 (if buffer-file-name | |
2065 (progn | |
2066 (princ "File A is: ") | |
2067 (princ buffer-file-name)) | |
2068 (progn | |
2069 (princ "Buffer A is: ") | |
2070 (princ (buffer-name)))) | |
2071 (princ "\n")) | |
2072 (emerge-eval-in-buffer emerge-B-buffer | |
2073 (if buffer-file-name | |
2074 (progn | |
2075 (princ "File B is: ") | |
2076 (princ buffer-file-name)) | |
2077 (progn | |
2078 (princ "Buffer B is: ") | |
2079 (princ (buffer-name)))) | |
2080 (princ "\n")) | |
2081 (if emerge-ancestor-buffer | |
2082 (emerge-eval-in-buffer emerge-ancestor-buffer | |
2083 (if buffer-file-name | |
2084 (progn | |
2085 (princ "Ancestor file is: ") | |
2086 (princ buffer-file-name)) | |
2087 (progn | |
2088 (princ "Ancestor buffer is: ") | |
2089 (princ (buffer-name)))) | |
2090 (princ "\n"))) | |
2091 (princ emerge-output-description)))) | |
2092 | |
2093 (defun emerge-join-differences (arg) | |
2094 "Join the selected difference with the following one. With a prefix | |
2095 argument, join with the preceeding one." | |
2096 (interactive "P") | |
2097 (let ((n emerge-current-difference)) | |
2098 ;; adjust n to be first difference to join | |
2099 (if arg | |
2100 (setq n (1- n))) | |
2101 ;; n and n+1 are the differences to join | |
2102 ;; check that they are both differences | |
2103 (if (or (< n 0) (>= n (1- emerge-number-of-differences))) | |
2104 (error "Incorrect differences to join")) | |
2105 ;; remove the flags | |
2106 (emerge-unselect-difference emerge-current-difference) | |
2107 ;; decrement total number of differences | |
2108 (setq emerge-number-of-differences (1- emerge-number-of-differences)) | |
2109 ;; build new differences vector | |
2110 (let ((i 0) | |
2111 (new-differences (make-vector emerge-number-of-differences nil))) | |
2112 (while (< i emerge-number-of-differences) | |
2113 (aset new-differences i | |
2114 (cond | |
2115 ((< i n) (aref emerge-difference-list i)) | |
2116 ((> i n) (aref emerge-difference-list (1+ i))) | |
2117 (t (let ((prev (aref emerge-difference-list i)) | |
2118 (next (aref emerge-difference-list (1+ i)))) | |
2119 (vector (aref prev 0) | |
2120 (aref next 1) | |
2121 (aref prev 2) | |
2122 (aref next 3) | |
2123 (aref prev 4) | |
2124 (aref next 5) | |
2125 (let ((ps (aref prev 6)) | |
2126 (ns (aref next 6))) | |
2127 (cond | |
2128 ((eq ps ns) | |
2129 ps) | |
2130 ((and (or (eq ps 'B) (eq ps 'prefer-B)) | |
2131 (or (eq ns 'B) (eq ns 'prefer-B))) | |
2132 'B) | |
2133 (t 'A)))))))) | |
2134 (setq i (1+ i))) | |
2135 (setq emerge-difference-list new-differences)) | |
2136 ;; set the current difference correctly | |
2137 (setq emerge-current-difference n) | |
2138 ;; fix the mode line | |
2139 (emerge-refresh-mode-line) | |
2140 ;; reinsert the flags | |
2141 (emerge-select-difference emerge-current-difference) | |
2142 (emerge-recenter))) | |
2143 | |
2144 (defun emerge-split-difference () | |
2145 "Split the current difference where the points are in the three windows." | |
2146 (interactive) | |
2147 (let ((n emerge-current-difference)) | |
2148 ;; check that this is a valid difference | |
2149 (emerge-validate-difference) | |
2150 ;; get the point values and old difference | |
2151 (let ((A-point (emerge-eval-in-buffer emerge-A-buffer | |
2152 (point-marker))) | |
2153 (B-point (emerge-eval-in-buffer emerge-B-buffer | |
2154 (point-marker))) | |
2155 (merge-point (point-marker)) | |
2156 (old-diff (aref emerge-difference-list n))) | |
2157 ;; check location of the points, give error if they aren't in the | |
2158 ;; differences | |
2159 (if (or (< A-point (aref old-diff 0)) | |
2160 (> A-point (aref old-diff 1))) | |
2161 (error "Point outside of difference in A buffer")) | |
2162 (if (or (< B-point (aref old-diff 2)) | |
2163 (> B-point (aref old-diff 3))) | |
2164 (error "Point outside of difference in B buffer")) | |
2165 (if (or (< merge-point (aref old-diff 4)) | |
2166 (> merge-point (aref old-diff 5))) | |
2167 (error "Point outside of difference in merge buffer")) | |
2168 ;; remove the flags | |
2169 (emerge-unselect-difference emerge-current-difference) | |
2170 ;; increment total number of differences | |
2171 (setq emerge-number-of-differences (1+ emerge-number-of-differences)) | |
2172 ;; build new differences vector | |
2173 (let ((i 0) | |
2174 (new-differences (make-vector emerge-number-of-differences nil))) | |
2175 (while (< i emerge-number-of-differences) | |
2176 (aset new-differences i | |
2177 (cond | |
2178 ((< i n) | |
2179 (aref emerge-difference-list i)) | |
2180 ((> i (1+ n)) | |
2181 (aref emerge-difference-list (1- i))) | |
2182 ((= i n) | |
2183 (vector (aref old-diff 0) | |
2184 A-point | |
2185 (aref old-diff 2) | |
2186 B-point | |
2187 (aref old-diff 4) | |
2188 merge-point | |
2189 (aref old-diff 6))) | |
2190 (t | |
2191 (vector (copy-marker A-point) | |
2192 (aref old-diff 1) | |
2193 (copy-marker B-point) | |
2194 (aref old-diff 3) | |
2195 (copy-marker merge-point) | |
2196 (aref old-diff 5) | |
2197 (aref old-diff 6))))) | |
2198 (setq i (1+ i))) | |
2199 (setq emerge-difference-list new-differences)) | |
2200 ;; set the current difference correctly | |
2201 (setq emerge-current-difference n) | |
2202 ;; fix the mode line | |
2203 (emerge-refresh-mode-line) | |
2204 ;; reinsert the flags | |
2205 (emerge-select-difference emerge-current-difference) | |
2206 (emerge-recenter)))) | |
2207 | |
2208 (defun emerge-trim-difference () | |
2209 "Trim lines off the top and bottom of a difference that are the same in | |
2210 both the A and B versions. (This can happen when the A and B versions | |
2211 have common lines that the ancestor version does not share.)" | |
2212 (interactive) | |
2213 ;; make sure we are in a real difference | |
2214 (emerge-validate-difference) | |
2215 ;; remove the flags | |
2216 (emerge-unselect-difference emerge-current-difference) | |
2217 (let* ((diff (aref emerge-difference-list emerge-current-difference)) | |
2218 (top-a (marker-position (aref diff 0))) | |
2219 (bottom-a (marker-position (aref diff 1))) | |
2220 (top-b (marker-position (aref diff 2))) | |
2221 (bottom-b (marker-position (aref diff 3))) | |
2222 (top-m (marker-position (aref diff 4))) | |
2223 (bottom-m (marker-position (aref diff 5))) | |
2224 size success sa sb sm) | |
2225 ;; move down the tops of the difference regions as much as possible | |
2226 ;; Try advancing comparing 1000 chars at a time. | |
2227 ;; When that fails, go 500 chars at a time, and so on. | |
2228 (setq size 1000) | |
2229 (while (> size 0) | |
2230 (setq success t) | |
2231 (while success | |
2232 (setq size (min size (- bottom-a top-a) (- bottom-b top-b) | |
2233 (- bottom-m top-m))) | |
2234 (setq sa (emerge-eval-in-buffer emerge-A-buffer | |
2235 (buffer-substring top-a | |
2236 (+ size top-a)))) | |
2237 (setq sb (emerge-eval-in-buffer emerge-B-buffer | |
2238 (buffer-substring top-b | |
2239 (+ size top-b)))) | |
2240 (setq sm (buffer-substring top-m (+ size top-m))) | |
2241 (setq success (and (> size 0) (equal sa sb) (equal sb sm))) | |
2242 (if success | |
2243 (setq top-a (+ top-a size) | |
2244 top-b (+ top-b size) | |
2245 top-m (+ top-m size)))) | |
2246 (setq size (/ size 2))) | |
2247 ;; move up the bottoms of the difference regions as much as possible | |
2248 ;; Try advancing comparing 1000 chars at a time. | |
2249 ;; When that fails, go 500 chars at a time, and so on. | |
2250 (setq size 1000) | |
2251 (while (> size 0) | |
2252 (setq success t) | |
2253 (while success | |
2254 (setq size (min size (- bottom-a top-a) (- bottom-b top-b) | |
2255 (- bottom-m top-m))) | |
2256 (setq sa (emerge-eval-in-buffer emerge-A-buffer | |
2257 (buffer-substring (- bottom-a size) | |
2258 bottom-a))) | |
2259 (setq sb (emerge-eval-in-buffer emerge-B-buffer | |
2260 (buffer-substring (- bottom-b size) | |
2261 bottom-b))) | |
2262 (setq sm (buffer-substring (- bottom-m size) bottom-m)) | |
2263 (setq success (and (> size 0) (equal sa sb) (equal sb sm))) | |
2264 (if success | |
2265 (setq bottom-a (- bottom-a size) | |
2266 bottom-b (- bottom-b size) | |
2267 bottom-m (- bottom-m size)))) | |
2268 (setq size (/ size 2))) | |
2269 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends | |
2270 ;; of the difference regions. Move them to the beginning of lines, as | |
2271 ;; appropriate. | |
2272 (emerge-eval-in-buffer emerge-A-buffer | |
2273 (goto-char top-a) | |
2274 (beginning-of-line) | |
2275 (aset diff 0 (point-marker)) | |
2276 (goto-char bottom-a) | |
2277 (beginning-of-line 2) | |
2278 (aset diff 1 (point-marker))) | |
2279 (emerge-eval-in-buffer emerge-B-buffer | |
2280 (goto-char top-b) | |
2281 (beginning-of-line) | |
2282 (aset diff 2 (point-marker)) | |
2283 (goto-char bottom-b) | |
2284 (beginning-of-line 2) | |
2285 (aset diff 3 (point-marker))) | |
2286 (goto-char top-m) | |
2287 (beginning-of-line) | |
2288 (aset diff 4 (point-marker)) | |
2289 (goto-char bottom-m) | |
2290 (beginning-of-line 2) | |
2291 (aset diff 5 (point-marker)) | |
2292 ;; put the flags back in, recenter the display | |
2293 (emerge-select-difference emerge-current-difference) | |
2294 (emerge-recenter))) | |
2295 | |
2296 (defun emerge-find-difference (arg) | |
2297 "Find the difference containing the current position of the point. | |
2298 If there is no containing difference and the prefix argument is positive, | |
2299 it finds the nearest following difference. A negative prefix argument finds | |
2300 the nearest previous difference." | |
2301 (interactive "P") | |
2302 ;; search for the point in the merge buffer, using the markers | |
2303 ;; for the beginning and end of the differences in the merge buffer | |
2304 (emerge-find-difference1 arg (point) 4 5)) | |
2305 | |
2306 (defun emerge-find-difference-A (arg) | |
2307 "Find the difference containing the current position of the point in the | |
2308 A buffer. (Nonetheless, this command must be executed in the merge buffer.) | |
2309 If there is no containing difference and the prefix argument is positive, | |
2310 it finds the nearest following difference. A negative prefix argument finds | |
2311 the nearest previous difference." | |
2312 (interactive "P") | |
2313 ;; search for the point in the A buffer, using the markers | |
2314 ;; for the beginning and end of the differences in the A buffer | |
2315 (emerge-find-difference1 arg | |
2316 (emerge-eval-in-buffer emerge-A-buffer (point)) | |
2317 0 1)) | |
2318 | |
2319 (defun emerge-find-difference-B (arg) | |
2320 "Find the difference containing the current position of the point in the | |
2321 B buffer. (Nonetheless, this command must be executed in the merge buffer.) | |
2322 If there is no containing difference and the prefix argument is positive, | |
2323 it finds the nearest following difference. A negative prefix argument finds | |
2324 the nearest previous difference." | |
2325 (interactive "P") | |
2326 ;; search for the point in the B buffer, using the markers | |
2327 ;; for the beginning and end of the differences in the B buffer | |
2328 (emerge-find-difference1 arg | |
2329 (emerge-eval-in-buffer emerge-B-buffer (point)) | |
2330 2 3)) | |
2331 | |
2332 (defun emerge-find-difference1 (arg location begin end) | |
2333 (let* ((index | |
2334 ;; find first difference containing or after the current position | |
2335 (catch 'search | |
2336 (let ((n 0)) | |
2337 (while (< n emerge-number-of-differences) | |
2338 (let ((diff-vector (aref emerge-difference-list n))) | |
2339 (if (<= location (marker-position (aref diff-vector end))) | |
2340 (throw 'search n))) | |
2341 (setq n (1+ n)))) | |
2342 emerge-number-of-differences)) | |
2343 (contains | |
2344 ;; whether the found difference contains the current position | |
2345 (and (< index emerge-number-of-differences) | |
2346 (<= (marker-position (aref (aref emerge-difference-list index) | |
2347 begin)) | |
2348 location))) | |
2349 (arg-value | |
2350 ;; numeric value of prefix argument | |
2351 (prefix-numeric-value arg))) | |
2352 (emerge-unselect-and-select-difference | |
2353 (cond | |
2354 ;; if the point is in a difference, select it | |
2355 (contains index) | |
2356 ;; if the arg is nil and the point is not in a difference, error | |
2357 ((null arg) (error "No difference contains point")) | |
2358 ;; if the arg is positive, select the following difference | |
2359 ((> arg-value 0) | |
2360 (if (< index emerge-number-of-differences) | |
2361 index | |
2362 (error "No difference contains or follows point"))) | |
2363 ;; if the arg is negative, select the preceeding difference | |
2364 (t | |
2365 (if (> index 0) | |
2366 (1- index) | |
2367 (error "No difference contains or preceeds point"))))))) | |
2368 | |
2369 (defun emerge-line-numbers () | |
2370 "Display the current line numbers of the points in the A, B, and | |
2371 merge buffers." | |
2372 (interactive) | |
2373 (let* ((valid-diff | |
2374 (and (>= emerge-current-difference 0) | |
2375 (< emerge-current-difference emerge-number-of-differences))) | |
2376 (diff (and valid-diff | |
2377 (aref emerge-difference-list emerge-current-difference))) | |
2378 (merge-line (emerge-line-number-in-buf 4 5)) | |
2379 (A-line (emerge-eval-in-buffer emerge-A-buffer | |
2380 (emerge-line-number-in-buf 0 1))) | |
2381 (B-line (emerge-eval-in-buffer emerge-B-buffer | |
2382 (emerge-line-number-in-buf 2 3)))) | |
2383 (message "At lines: merge = %d, A = %d, B = %d" | |
2384 merge-line A-line B-line))) | |
2385 | |
2386 (defun emerge-line-number-in-buf (begin-marker end-marker) | |
2387 (let (temp) | |
2388 (setq temp (save-excursion | |
2389 (beginning-of-line) | |
2390 (1+ (count-lines 1 (point))))) | |
2391 (if valid-diff | |
2392 (progn | |
2393 (if (> (point) (aref diff begin-marker)) | |
2394 (setq temp (- temp emerge-before-flag-lines))) | |
2395 (if (> (point) (aref diff end-marker)) | |
2396 (setq temp (- temp emerge-after-flag-lines))))) | |
2397 temp)) | |
2398 | |
2399 (defun emerge-set-combine-versions-template (start end &optional localize) | |
2400 "Copy region into emerge-combine-versions-template which controls how | |
2401 emerge-combine-versions will combine the two versions. | |
2402 With prefix argument, emerge-combine-versions is made local to this | |
2403 merge buffer. Localization is permanent for any particular merge buffer." | |
2404 (interactive "r\nP") | |
2405 (if localize | |
2406 (make-local-variable 'emerge-combine-versions-template)) | |
2407 (setq emerge-combine-versions-template (buffer-substring start end)) | |
2408 (message | |
2409 (if (assq 'emerge-combine-versions-template (buffer-local-variables)) | |
2410 "emerge-set-combine-versions-template set locally." | |
2411 "emerge-set-combine-versions-template set."))) | |
2412 | |
2413 (defun emerge-combine-versions (&optional force) | |
2414 "Combine the two versions using the template in | |
2415 emerge-combine-versions-template. | |
2416 Refuses to function if this difference has been edited, i.e., if it is | |
2417 neither the A nor the B variant. | |
2418 An ARGUMENT forces the variant to be selected even if the difference has | |
2419 been edited." | |
2420 (interactive "P") | |
2421 (emerge-combine-versions-internal emerge-combine-versions-template force)) | |
2422 | |
2423 (defun emerge-combine-versions-register (char &optional force) | |
2424 "Combine the two versions using the template in register REG. | |
2425 See documentation of the variable emerge-combine-versions-template | |
2426 for how the template is interpreted. | |
2427 Refuses to function if this difference has been edited, i.e., if it is | |
2428 neither the A nor the B variant. | |
2429 An ARGUMENT forces the variant to be selected even if the difference has | |
2430 been edited." | |
2431 (interactive "cRegister containing template: \nP") | |
2432 (let ((template (get-register char))) | |
2433 (if (not (stringp template)) | |
2434 (error "Register does not contain text")) | |
2435 (emerge-combine-versions-internal template force))) | |
2436 | |
2437 (defun emerge-combine-versions-internal (template force) | |
2438 (let ((operate | |
2439 (function (lambda () | |
2440 (emerge-combine-versions-edit merge-begin merge-end | |
2441 A-begin A-end B-begin B-end) | |
2442 (if emerge-auto-advance | |
2443 (emerge-next-difference)))))) | |
2444 (emerge-select-version force operate operate operate))) | |
2445 | |
2446 (defun emerge-combine-versions-edit (merge-begin merge-end | |
2447 A-begin A-end B-begin B-end) | |
2448 (emerge-eval-in-buffer | |
2449 emerge-merge-buffer | |
2450 (delete-region merge-begin merge-end) | |
2451 (goto-char merge-begin) | |
2452 (let ((i 0)) | |
2453 (while (< i (length template)) | |
2454 (let ((c (aref template i))) | |
2455 (if (= c ?%) | |
2456 (progn | |
2457 (setq i (1+ i)) | |
2458 (setq c | |
2459 (condition-case nil | |
2460 (aref template i) | |
2461 (error ?%))) | |
2462 (cond ((= c ?a) | |
2463 (insert-buffer-substring emerge-A-buffer A-begin A-end)) | |
2464 ((= c ?b) | |
2465 (insert-buffer-substring emerge-B-buffer B-begin B-end)) | |
2466 ((= c ?%) | |
2467 (insert ?%) | |
2468 (t | |
2469 (insert c))))) | |
2470 (insert c))) | |
2471 (setq i (1+ i)))) | |
2472 (goto-char merge-begin) | |
2473 (aset diff-vector 6 'combined) | |
2474 (emerge-refresh-mode-line))) | |
2475 | |
2476 (defun emerge-set-merge-mode (mode) | |
2477 "Set the major mode in a merge buffer. Overrides any change that the mode | |
2478 might make to the mode line or local keymap. Leaves merge in fast mode." | |
2479 (interactive | |
2480 (list (intern (completing-read "New major mode for merge buffer: " | |
2481 obarray 'commandp t nil)))) | |
2482 (funcall mode) | |
2483 (emerge-refresh-mode-line) | |
2484 (if emerge-fast-mode | |
2485 (emerge-fast-mode) | |
2486 (emerge-edit-mode))) | |
2487 | |
2488 (defun emerge-one-line-window () | |
2489 (interactive) | |
2490 (let ((window-min-height 1)) | |
2491 (shrink-window (- (window-height) 2)))) | |
2492 | |
2493 ;;; Support routines | |
2494 | |
2495 ;; Select a difference by placing the visual flags around the appropriate | |
2496 ;; group of lines in the A, B, and merge buffers | |
2497 (defun emerge-select-difference (n) | |
2498 (let ((diff-vector (aref emerge-difference-list n))) | |
2499 (emerge-place-flags-in-buffer emerge-A-buffer | |
2500 (aref diff-vector 0) (aref diff-vector 1)) | |
2501 (emerge-place-flags-in-buffer emerge-B-buffer | |
2502 (aref diff-vector 2) (aref diff-vector 3)) | |
2503 (emerge-place-flags-in-buffer emerge-merge-buffer | |
2504 (aref diff-vector 4) (aref diff-vector 5)))) | |
2505 | |
2506 (defun emerge-place-flags-in-buffer (buffer before after) | |
2507 (if (eq buffer emerge-merge-buffer) | |
2508 (emerge-place-flags-in-buffer1 buffer before after) | |
2509 (emerge-eval-in-buffer | |
2510 buffer | |
2511 (emerge-place-flags-in-buffer1 buffer before after)))) | |
2512 | |
2513 (defun emerge-place-flags-in-buffer1 (buffer before after) | |
2514 (let ((buffer-read-only nil)) | |
2515 ;; insert the flags | |
2516 (goto-char before) | |
2517 (insert-before-markers emerge-before-flag) | |
2518 (goto-char after) | |
2519 (insert emerge-after-flag) | |
2520 ;; put the markers into the flags, so alterations above or below won't move | |
2521 ;; them | |
2522 ;; before marker is one char before the end of the before flag | |
2523 ;; after marker is one char after the beginning of the after flag | |
2524 (set-marker before (1- before)) | |
2525 (set-marker after (1+ after)))) | |
2526 | |
2527 ;; Unselect a difference by removing the visual flags in the buffers. | |
2528 (defun emerge-unselect-difference (n) | |
2529 (let ((diff-vector (aref emerge-difference-list n))) | |
2530 (emerge-remove-flags-in-buffer emerge-A-buffer | |
2531 (aref diff-vector 0) (aref diff-vector 1)) | |
2532 (emerge-remove-flags-in-buffer emerge-B-buffer | |
2533 (aref diff-vector 2) (aref diff-vector 3)) | |
2534 (emerge-remove-flags-in-buffer emerge-merge-buffer | |
2535 (aref diff-vector 4) (aref diff-vector 5)))) | |
2536 | |
2537 (defun emerge-remove-flags-in-buffer (buffer before after) | |
2538 (emerge-eval-in-buffer | |
2539 buffer | |
2540 (let ((buffer-read-only nil)) | |
2541 ;; put the markers at the beginning of the flags | |
2542 (set-marker before (- before (1- emerge-before-flag-length))) | |
2543 (set-marker after (1- after)) | |
2544 ;; remove the flags | |
2545 (goto-char before) | |
2546 (if (looking-at emerge-before-flag-match) | |
2547 (delete-char emerge-before-flag-length) | |
2548 ;; the flag isn't there | |
2549 (ding) | |
2550 (message "Trouble removing flag.")) | |
2551 (goto-char after) | |
2552 (if (looking-at emerge-after-flag-match) | |
2553 (delete-char emerge-after-flag-length) | |
2554 ;; the flag isn't there | |
2555 (ding) | |
2556 (message "Trouble removing flag."))))) | |
2557 | |
2558 ;; Select a difference, removing an flags that exist now. | |
2559 (defun emerge-unselect-and-select-difference (n &optional suppress-display) | |
2560 (if (and (>= emerge-current-difference 0) | |
2561 (< emerge-current-difference emerge-number-of-differences)) | |
2562 (emerge-unselect-difference emerge-current-difference)) | |
2563 (if (and (>= n 0) (< n emerge-number-of-differences)) | |
2564 (progn | |
2565 (emerge-select-difference n) | |
2566 (let* ((diff-vector (aref emerge-difference-list n)) | |
2567 (selection-type (aref diff-vector 6))) | |
2568 (if (eq selection-type 'default-A) | |
2569 (aset diff-vector 6 'A) | |
2570 (if (eq selection-type 'default-B) | |
2571 (aset diff-vector 6 'B)))))) | |
2572 (setq emerge-current-difference n) | |
2573 (if (not suppress-display) | |
2574 (progn | |
2575 (emerge-recenter) | |
2576 (emerge-refresh-mode-line)))) | |
2577 | |
2578 ;; Perform tests to see whether user should be allowed to select a version | |
2579 ;; of this difference: | |
2580 ;; a valid difference has been selected; and | |
2581 ;; the difference text in the merge buffer is: | |
2582 ;; the A version (execute a-version), or | |
2583 ;; the B version (execute b-version), or | |
2584 ;; empty (execute neither-version), or | |
2585 ;; argument FORCE is true (execute neither-version) | |
2586 ;; Otherwise, signal an error. | |
2587 (defun emerge-select-version (force a-version b-version neither-version) | |
2588 (emerge-validate-difference) | |
2589 (let ((buffer-read-only nil)) | |
2590 (let* ((diff-vector | |
2591 (aref emerge-difference-list emerge-current-difference)) | |
2592 (A-begin (1+ (aref diff-vector 0))) | |
2593 (A-end (1- (aref diff-vector 1))) | |
2594 (B-begin (1+ (aref diff-vector 2))) | |
2595 (B-end (1- (aref diff-vector 3))) | |
2596 (merge-begin (1+ (aref diff-vector 4))) | |
2597 (merge-end (1- (aref diff-vector 5)))) | |
2598 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end | |
2599 emerge-merge-buffer merge-begin | |
2600 merge-end) | |
2601 (funcall a-version) | |
2602 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end | |
2603 emerge-merge-buffer merge-begin | |
2604 merge-end) | |
2605 (funcall b-version) | |
2606 (if (or force (= merge-begin merge-end)) | |
2607 (funcall neither-version) | |
2608 (error "This difference region has been edited."))))))) | |
2609 | |
2610 ;; Revise the mode line to display which difference we have selected | |
2611 | |
2612 (defun emerge-refresh-mode-line () | |
2613 (setq mode-line-buffer-identification | |
2614 (list (format "Emerge: %%b diff %d of %d%s" | |
2615 (1+ emerge-current-difference) | |
2616 emerge-number-of-differences | |
2617 (if (and (>= emerge-current-difference 0) | |
2618 (< emerge-current-difference | |
2619 emerge-number-of-differences)) | |
2620 (cdr (assq (aref (aref emerge-difference-list | |
2621 emerge-current-difference) | |
2622 6) | |
2623 '((A . " - A") | |
2624 (B . " - B") | |
2625 (prefer-A . " - A*") | |
2626 (prefer-B . " - B*") | |
2627 (combined . " - comb")))) | |
2628 "")))) | |
2629 ;; Force mode-line redisplay | |
2630 (set-buffer-modified-p (buffer-modified-p))) | |
2631 | |
2632 ;; compare two regions in two buffers for containing the same text | |
2633 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end) | |
2634 ;; first check that the two regions are the same length | |
2635 (if (not (and (= (- x-end x-begin) (- y-end y-begin)))) | |
2636 nil | |
2637 (catch 'exit | |
2638 (while (< x-begin x-end) | |
2639 ;; bite off and compare no more than 1000 characters at a time | |
2640 (let* ((compare-length (min (- x-end x-begin) 1000)) | |
2641 (x-string (emerge-eval-in-buffer | |
2642 buffer-x | |
2643 (buffer-substring x-begin | |
2644 (+ x-begin compare-length)))) | |
2645 (y-string (emerge-eval-in-buffer | |
2646 buffer-y | |
2647 (buffer-substring y-begin | |
2648 (+ y-begin compare-length))))) | |
2649 (if (not (string-equal x-string y-string)) | |
2650 (throw 'exit nil) | |
2651 (setq x-begin (+ x-begin compare-length)) | |
2652 (setq y-begin (+ y-begin compare-length))))) | |
2653 t))) | |
2654 | |
2655 ;; Construct a unique buffer name. | |
2656 ;; The first one tried is prefixsuffix, then prefix<2>suffix, | |
2657 ;; prefix<3>suffix, etc. | |
2658 (defun emerge-unique-buffer-name (prefix suffix) | |
2659 (if (null (get-buffer (concat prefix suffix))) | |
2660 (concat prefix suffix) | |
2661 (let ((n 2)) | |
2662 (while (get-buffer (format "%s<%d>%s" prefix n suffix)) | |
2663 (setq n (1+ n))) | |
2664 (format "%s<%d>%s" prefix n suffix)))) | |
2665 | |
2666 ;; Verify that we have a difference selected. | |
2667 (defun emerge-validate-difference () | |
2668 (if (not (and (>= emerge-current-difference 0) | |
2669 (< emerge-current-difference emerge-number-of-differences))) | |
2670 (error "No difference selected"))) | |
2671 | |
2672 ;;; Functions for saving and restoring a batch of variables | |
2673 | |
2674 ;; These functions save (get the values of) and restore (set the values of) | |
2675 ;; a list of variables. The argument is a list of symbols (the names of | |
2676 ;; the variables). A list element can also be a list of two functions, | |
2677 ;; the first of which (when called with no arguments) gets the value, and | |
2678 ;; the second (when called with a value as an argment) sets the value. | |
2679 ;; A "function" is anything that funcall can handle as an argument. | |
2680 | |
2681 (defun emerge-save-variables (vars) | |
2682 (mapcar (function (lambda (v) (if (symbolp v) | |
2683 (symbol-value v) | |
2684 (funcall (car v))))) | |
2685 vars)) | |
2686 | |
2687 (defun emerge-restore-variables (vars values) | |
2688 (while vars | |
2689 (let ((var (car vars)) | |
2690 (value (car values))) | |
2691 (if (symbolp var) | |
2692 (set var value) | |
2693 (funcall (car (cdr var)) value))) | |
2694 (setq vars (cdr vars)) | |
2695 (setq values (cdr values)))) | |
2696 | |
2697 ;; Make a temporary file that only we have access to. | |
2698 ;; PREFIX is appended to emerge-temp-file-prefix to make the filename prefix. | |
2699 (defun emerge-make-temp-file (prefix) | |
2700 (let ((f (make-temp-name (concat emerge-temp-file-prefix prefix)))) | |
2701 ;; create the file | |
2702 (write-region (point-min) (point-min) f nil 'no-message) | |
2703 (set-file-modes f emerge-temp-file-mode) | |
2704 f)) | |
2705 | |
2706 ;;; Functions that query the user before he can write out the current buffer. | |
2707 | |
2708 (defun emerge-query-write-file () | |
2709 "Query the user if he really wants to write out the incomplete merge. | |
2710 If he says yes, call write-file to do so. See emerge-query-and-call | |
2711 for details of the querying process." | |
2712 (interactive) | |
2713 (emerge-query-and-call 'write-file)) | |
2714 | |
2715 (defun emerge-query-save-buffer () | |
2716 "Query the user if he really wants to write out the incomplete merge. | |
2717 If he says yes, call save-buffer to do so. See emerge-query-and-call | |
2718 for details of the querying process." | |
2719 (interactive) | |
2720 (emerge-query-and-call 'save-buffer)) | |
2721 | |
2722 (defun emerge-query-and-call (command) | |
2723 "Query the user if he really wants to write out the incomplete merge. | |
2724 If he says yes, call COMMAND interactively. During the call, the flags | |
2725 around the current difference are removed." | |
2726 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ") | |
2727 ;; He really wants to do it -- unselect the difference for the duration | |
2728 (progn | |
2729 (if (and (>= emerge-current-difference 0) | |
2730 (< emerge-current-difference emerge-number-of-differences)) | |
2731 (emerge-unselect-difference emerge-current-difference)) | |
2732 ;; call-interactively takes the value of current-prefix-arg as the | |
2733 ;; prefix argument value to be passed to the command. Thus, we have | |
2734 ;; to do nothing special to make sure the prefix argument is | |
2735 ;; transmitted to the command. | |
2736 (call-interactively command) | |
2737 (if (and (>= emerge-current-difference 0) | |
2738 (< emerge-current-difference emerge-number-of-differences)) | |
2739 (progn | |
2740 (emerge-select-difference emerge-current-difference) | |
2741 (emerge-recenter)))) | |
2742 ;; He's being smart and not doing it | |
2743 (message "Not written"))) | |
2744 | |
2745 ;; Make sure the current buffer (for a file) has the same contents as the | |
2746 ;; file on disk, and attempt to remedy the situation if not. | |
2747 ;; Signal an error if we can't make them the same, or the user doesn't want | |
2748 ;; to do what is necessary to make them the same. | |
2749 (defun emerge-verify-file-buffer () | |
2750 ;; First check if the file has been modified since the buffer visited it. | |
2751 (if (verify-visited-file-modtime (current-buffer)) | |
2752 (if (buffer-modified-p) | |
2753 ;; If buffer is not obsolete and is modified, offer to save | |
2754 (if (yes-or-no-p (format "Save file %s? " buffer-file-name)) | |
2755 (save-buffer) | |
2756 (error "Buffer out of sync for file %s" buffer-file-name)) | |
2757 ;; If buffer is not obsolete and is not modified, do nothing | |
2758 nil) | |
2759 (if (buffer-modified-p) | |
2760 ;; If buffer is obsolete and is modified, give error | |
2761 (error "Buffer out of sync for file %s" buffer-file-name) | |
2762 ;; If buffer is obsolete and is not modified, offer to revert | |
2763 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name)) | |
2764 (revert-buffer t t) | |
2765 (error "Buffer out of sync for file %s" buffer-file-name))))) | |
2766 | |
2767 ;; Utilities that might have value outside of Emerge. | |
2768 | |
2769 ;; Set up the mode in the current buffer to duplicate the mode in another | |
2770 ;; buffer. | |
2771 (defun emerge-copy-modes (buffer) | |
2772 ;; Set the major mode | |
2773 (funcall (emerge-eval-in-buffer buffer major-mode))) | |
2774 | |
2775 ;; Define a key, even if a prefix of it is defined | |
2776 (defun emerge-force-define-key (keymap key definition) | |
2777 "Like define-key, but is not stopped if a prefix of KEY is a defined | |
2778 command." | |
2779 ;; Find out if a prefix of key is defined | |
2780 (let ((v (lookup-key keymap key))) | |
2781 ;; If so, undefine it | |
2782 (if (integerp v) | |
2783 (define-key keymap (substring key 0 v) nil))) | |
2784 ;; Now define the key | |
2785 (define-key keymap key definition)) | |
2786 | |
2787 ;;; Improvements to describe-mode, so that it describes minor modes as well | |
2788 ;;; as the major mode | |
2789 (defun describe-mode (&optional minor) | |
2790 "Display documentation of current major mode. | |
2791 If optional MINOR is non-nil (or prefix argument is given if interactive), | |
2792 display documentation of acive minor modes as well. | |
2793 For this to work correctly for a minor mode, the mode's indicator variable | |
2794 (listed in minor-mode-alist) must also be a function whose documentation | |
2795 describes the minor mode." | |
2796 (interactive) | |
2797 (with-output-to-temp-buffer "*Help*" | |
2798 (princ mode-name) | |
2799 (princ " Mode:\n") | |
2800 (princ (documentation major-mode)) | |
2801 (let ((minor-modes minor-mode-alist) | |
2802 (locals (buffer-local-variables))) | |
2803 (while minor-modes | |
2804 (let* ((minor-mode (car (car minor-modes))) | |
2805 (indicator (car (cdr (car minor-modes)))) | |
2806 (local-binding (assq minor-mode locals))) | |
2807 ;; Document a minor mode if it is listed in minor-mode-alist, | |
2808 ;; bound locally in this buffer, non-nil, and has a function | |
2809 ;; definition. | |
2810 (if (and local-binding | |
2811 (cdr local-binding) | |
2812 (fboundp minor-mode)) | |
2813 (progn | |
2814 (princ (format "\n\n\n%s minor mode (indicator%s):\n" | |
2815 minor-mode indicator)) | |
2816 (princ (documentation minor-mode))))) | |
2817 (setq minor-modes (cdr minor-modes)))) | |
2818 (print-help-return-message))) | |
2819 | |
2820 ;; Adjust things so that keyboard macro definitions are documented correctly. | |
2821 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) | |
2822 | |
2823 ;; Function to shadow a definition in a keymap with definitions in another. | |
2824 (defun emerge-shadow-key-definition (olddef newdef keymap shadowmap) | |
2825 "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP. | |
2826 In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP | |
2827 with NEWDEF. Does not affect keys that are already defined in SHADOWMAP, | |
2828 including those whose definition is OLDDEF." | |
2829 ;; loop through all keymaps accessible from keymap | |
2830 (let ((maps (accessible-keymaps keymap))) | |
2831 (while maps | |
2832 (let ((prefix (car (car maps))) | |
2833 (map (cdr (car maps)))) | |
2834 ;; examine a keymap | |
2835 (if (arrayp map) | |
2836 ;; array keymap | |
2837 (let ((len (length map)) | |
2838 (i 0)) | |
2839 (while (< i len) | |
2840 (if (eq (aref map i) olddef) | |
2841 ;; set the shadowing definition | |
2842 (let ((key (concat prefix (char-to-string i)))) | |
2843 (emerge-define-key-if-possible shadowmap key newdef))) | |
2844 (setq i (1+ i)))) | |
2845 ;; sparse keymap | |
2846 (while map | |
2847 (if (eq (cdr-safe (car-safe map)) olddef) | |
2848 ;; set the shadowing definition | |
2849 (let ((key | |
2850 (concat prefix (char-to-string (car (car map)))))) | |
2851 (emerge-define-key-if-possible shadowmap key newdef))) | |
2852 (setq map (cdr map))))) | |
2853 (setq maps (cdr maps))))) | |
2854 | |
2855 ;; Define a key if it (or a prefix) is not already defined in the map. | |
2856 (defun emerge-define-key-if-possible (keymap key definition) | |
2857 ;; look up the present definition of the key | |
2858 (let ((present (lookup-key keymap key))) | |
2859 (if (integerp present) | |
2860 ;; if it is "too long", look up the valid prefix | |
2861 (if (not (lookup-key keymap (substring key 0 present))) | |
2862 ;; if the prefix isn't defined, define it | |
2863 (define-key keymap key definition)) | |
2864 ;; if there is no present definition, define it | |
2865 (if (not present) | |
2866 (define-key keymap key definition))))) | |
2867 | |
2868 (defun emerge-recursively-substitute-key-definition (olddef newdef keymap) | |
2869 "Like substitute-key-definition, but examines and substitutes in all | |
2870 keymaps accessible from KEYMAP. Make sure that subordinate keymaps aren't | |
2871 shared with other keymaps! (copy-keymap will suffice.)" | |
2872 ;; Loop through all keymaps accessible from keymap | |
2873 (let ((maps (accessible-keymaps keymap))) | |
2874 (while maps | |
2875 ;; Substitute in this keymap | |
2876 (substitute-key-definition olddef newdef (cdr (car maps))) | |
2877 (setq maps (cdr maps))))) | |
2878 | |
2879 ;; Show the name of the file in the buffer. | |
2880 (defun emerge-show-file-name () | |
2881 "Displays the name of the file loaded into the current buffer. | |
2882 If the name won't fit on one line, the minibuffer is expanded to hold it, | |
2883 and the command waits for a keystroke from the user. If the keystroke is | |
2884 SPC, it is ignored; if it is anything else, it is processed as a command." | |
2885 (interactive) | |
2886 (let ((name (buffer-file-name))) | |
2887 (or name | |
2888 (setq name "Buffer has no file name.")) | |
2889 (save-window-excursion | |
2890 (select-window (minibuffer-window)) | |
2891 (erase-buffer) | |
2892 (insert name) | |
2893 (if (not (pos-visible-in-window-p)) | |
2894 (let ((echo-keystrokes 0)) | |
2895 (while (and (not (pos-visible-in-window-p)) | |
778 | 2896 (> (1- (frame-height)) (window-height))) |
583 | 2897 (enlarge-window 1)) |
2898 (let ((c (read-char))) | |
2899 (if (/= c 32) | |
2900 (setq unread-command-char c)))))))) | |
2901 | |
2902 ;; Improved auto-save file names. | |
2903 ;; This function fixes many problems with the standard auto-save file names: | |
2904 ;; Auto-save files for non-file buffers get put in the default directory | |
2905 ;; for the buffer, whether that makes sense or not. | |
2906 ;; Auto-save files for file buffers get put in the directory of the file, | |
2907 ;; regardless of whether we can write into it or not. | |
2908 ;; Auto-save files for non-file buffers don't use the process id, so if a | |
2909 ;; user runs more than on Emacs, they can make auto-save files that overwrite | |
2910 ;; each other. | |
2911 ;; To use this function, do: | |
2912 ;; (fset 'make-auto-save-file-name | |
2913 ;; (symbol-function 'emerge-make-auto-save-file-name)) | |
2914 (defun emerge-make-auto-save-file-name () | |
2915 "Return file name to use for auto-saves of current buffer. | |
2916 Does not consider auto-save-visited-file-name; that is checked | |
2917 before calling this function. | |
2918 You can redefine this for customization. | |
2919 See also auto-save-file-name-p." | |
2920 (if buffer-file-name | |
2921 ;; if buffer has a file, try the format <file directory>/#<file name># | |
2922 (let ((f (concat (file-name-directory buffer-file-name) | |
2923 "#" | |
2924 (file-name-nondirectory buffer-file-name) | |
2925 "#"))) | |
2926 (if (file-writable-p f) | |
2927 ;; the file is writable, so use it | |
2928 f | |
2929 ;; the file isn't writable, so use the format | |
2930 ;; ~/#&<file name>&<hash of directory># | |
2931 (concat (getenv "HOME") | |
2932 "/#&" | |
2933 (file-name-nondirectory buffer-file-name) | |
2934 "&" | |
2935 (hash-string-into-string | |
2936 (file-name-directory buffer-file-name)) | |
2937 "#"))) | |
2938 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id># | |
2939 (expand-file-name (concat (getenv "HOME") | |
2940 "/#%" | |
2941 ;; quote / into \! and \ into \\ | |
2942 (unslashify-name (buffer-name)) | |
2943 "%" | |
2944 (make-temp-name "") | |
2945 "#")))) | |
2946 | |
2947 ;; Hash a string into five characters more-or-less suitable for use in a file | |
2948 ;; name. (Allowed characters are ! through ~, except /.) | |
2949 (defun hash-string-into-string (s) | |
2950 (let ((bins (vector 0 0 0 0 0)) | |
2951 (i 0)) | |
2952 (while (< i (length s)) | |
2953 (aset bins (% i 5) (% (+ (* (aref bins (% i 5)) 35) | |
2954 (aref s i)) | |
2955 65536)) | |
2956 (setq i (1+ i))) | |
2957 (mapconcat (function (lambda (b) | |
2958 (setq b (+ (% b 93) ?!)) | |
2959 (if (>= b ?/) | |
2960 (setq b (1+ b))) | |
2961 (char-to-string b))) | |
2962 bins ""))) | |
2963 | |
2964 ;; Quote any /s in a string by replacing them with \!. | |
2965 ;; Also, replace any \s by \\, to make it one-to-one. | |
2966 (defun unslashify-name (s) | |
2967 (let ((limit 0)) | |
2968 (while (string-match "[/\\]" s limit) | |
2969 (setq s (concat (substring s 0 (match-beginning 0)) | |
2970 (if (string= (substring s (match-beginning 0) | |
2971 (match-end 0)) | |
2972 "/") | |
2973 "\\!" | |
2974 "\\\\") | |
2975 (substring s (match-end 0)))) | |
2976 (setq limit (1+ (match-end 0))))) | |
2977 s) | |
2978 | |
2979 (provide 'emerge) | |
2980 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
583
diff
changeset
|
2981 ;;; emerge.el ends here |