-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmswemacs.c
501 lines (441 loc) · 15.2 KB
/
mswemacs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* The routines in this file provide extra emacs functions available
under the Microsoft Windows environment on an IBM-PC or compatible
computer. The following functions are supplied: cutregion,
clipregion, insertclip and helpengine.
Also implemented here is the support for scroll bars
Must be compiled with Borland C++ 2.0 or MSC 6.0 or later versions
It should not be compiled if the WINDOW_MSWIN symbol is not set */
#include "estruct.h"
#include <stdio.h>
#include "eproto.h"
#include "edef.h"
#include "elang.h"
#include "mswin.h"
#include "mswmenu.h"
/* to invoke the commands corresponding to scroll bar actions */
#define MAXSCROLL 32767 /* maximum position for scrollbars */
static HANDLE hClipData = NULL; /* used by insertclip and
ClipboardCleanup */
/* CopyToClipboard: internal function to copy region to clipboard */
/* =============== */
static BOOL PASCAL CopyToClipboard (REGION *Region)
{
long Size = 0L;
HANDLE hData;
#if WINDOW_MSWIN32
char *Data;
#else
char huge *Data;
#endif
BOOL Result = TRUE;
register LINE *lp;
register int Offset;
register int lcnt; /* used to reduce longop() overhead */
/*-figure out the size of the clipboard data (end of lines have to
be turned into CR-LF) */
Size = Region->r_size;
if (curwp->w_dotp != curwp->w_markp[0]) { /* multiple lines */
lp = Region->r_linep;
do {
++Size;
lp = lforw(lp);
} while ((lp != curwp->w_dotp) && (lp != curwp->w_markp[0]));
}
if (Size == 0L) return TRUE;
/*-copy the buffer data into a block of global memory */
if (hData = GlobalAlloc (GMEM_MOVEABLE, Size + 1)) {
if (!(Data = GlobalLock (hData))) goto NoClipboardMemory;
lp = Region->r_linep;
Offset = Region->r_offset;
lcnt = 0;
while (Size-- > 0) {
if (Offset != lused(lp)) { /* middle of line */
*Data++ = lgetc(lp, Offset);
++Offset;
}
else { /* end of line */
*Data++ = '\r';
*Data++ = '\n';
Size--;
lp = lforw(lp);
Offset = 0;
if (--lcnt < 0) {
longop (TRUE);
lcnt = 10; /* reduce longop calls overhead */
}
}
}
*Data = '\0';
/*-pass the text to the clipboard */
GlobalUnlock (hData);
if (OpenClipboard (hFrameWnd)) {
if (EmptyClipboard ()) {
SetClipboardData (CF_TEXT, hData);
}
else Result = FALSE;
CloseClipboard ();
}
else Result = FALSE;
if (Result == FALSE) GlobalFree (hData);
}
else {
NoClipboardMemory:
mlabort (TEXT94); /* out of memory */
Result = FALSE;
}
return Result;
} /* CopyToClipboard */
/* cutregion: move the current region to the clipboard */
/* ========= */
int PASCAL cutregion (int f, int n)
{
REGION Region;
int Result;
/*-don't allow command if read-only mode */
if (curbp->b_mode & MDVIEW) return rdonly();
if ((Result = getregion (&Region)) != TRUE) return Result;
if ((Result = CopyToClipboard (&Region)) != TRUE) return Result;
curwp->w_dotp = Region.r_linep;
curwp->w_doto = Region.r_offset;
return ldelete (Region.r_size, FALSE);
} /* cutregion */
/* clipregion: copy the current region into the clipboard */
/* ========== */
int PASCAL clipregion (int f, int n)
{
REGION Region;
int Result;
if ((Result = getregion (&Region)) != TRUE) return Result;
return CopyToClipboard (&Region);
} /* clipregion */
/* insertclip: insert the clipboard contents at dot */
/* ========== */
int PASCAL insertclip (int f, int n)
{
BOOL Result = TRUE;
char *Text, *TextHead;
short int curoff;
LINE *curline;
/*-don't allow command if read-only mode */
if (curbp->b_mode & MDVIEW) return rdonly();
if (OpenClipboard (hFrameWnd)) {
if ((hClipData = GetClipboardData (CF_TEXT)) != NULL) {
/* Save the local pointers to hold global "." */
if (yankflag) {
/* Find the *previous* line, since the line we are on
may disappear due to re-allocation. This works even
if we are on the first line of the file. */
curline = lback(curwp->w_dotp);
curoff = curwp->w_doto;
}
if ((TextHead = GlobalLock (hClipData)) != NULL) {
while (n--) {
Text = TextHead;
while (*Text != '\0') {
if (*Text == '\n') {
if (lnewline () == FALSE) {
Result = FALSE;
goto bail_out;
}
}
else {
if (*Text != '\r') if (linsert (1, *Text) == FALSE) {
Result = FALSE;
goto bail_out;
}
}
++Text;
}
}
bail_out:
GlobalUnlock (hClipData);
hClipData = NULL; /* for ClipboardCleanup */
/* If requested, set global "." back to the beginning of
the yanked text. */
if (yankflag) {
curwp->w_dotp = lforw(curline);
curwp->w_doto = curoff;
}
}
}
else Result = FALSE;
CloseClipboard ();
}
else Result = FALSE;
return Result;
} /* insertclip */
/* ClipboardCleanup: to be called if the user aborts during a longop */
/* ================ */
void FAR PASCAL ClipboardCleanup (void)
{
if (hClipData) {
GlobalUnlock (hClipData);
CloseClipboard ();
}
} /* ClipboardCleanup */
/* helpengine: invoke the MS-Windows help engine */
/* ========== */
int PASCAL helpengine (int f, int n)
{
char OldHelpFile [NFILEN];
char HelpKey [NLINE];
BOOL Result;
strcpy (OldHelpFile, HelpEngineFile);
SetWorkingDir ();
if ((Result = FILENAMEREPLY (TEXT307, HelpEngineFile, NFILEN)) != TRUE) return Result;
/* "Help file: " */
if (HelpEngineFile[0] == '\0') {
strcpy (HelpEngineFile, OldHelpFile);
return FALSE;
}
else {
Result = mlreply (TEXT308, HelpKey, NLINE);
if ((Result != TRUE) && (Result != FALSE)) return Result;
/* "Help key: " */
if (HelpKey[0] == '\0') {
WinHelp (hFrameWnd, HelpEngineFile, HELP_INDEX, NULL);
}
else {
WinHelp (hFrameWnd, HelpEngineFile, HELP_KEY,
(DWORD)(LPSTR)&HelpKey[0]);
}
}
return TRUE;
} /* helpengine */
/* minimizescreen: turn the current screen into an icon */
/* ============== */
PASCAL minimizescreen (int f, int n)
{
BOOL nq;
nq = notquiescent;
notquiescent = 0;
ShowWindow (first_screen->s_drvhandle, SW_MINIMIZE);
notquiescent = nq;
return TRUE;
} /* minimizescreen */
/* ForceMessage: do a SendMessage, forcing quiescent mode */
/* ============ */
static PASCAL ForceMessage (HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
{
BOOL nq;
nq = notquiescent;
notquiescent = 0;
SendMessage (hWnd, wMsg, wParam, lParam);
notquiescent = nq;
} /* ForceMessage */
/* maximizescreen: maximize the current screen */
/* ============== */
PASCAL maximizescreen (int f, int n)
{
ForceMessage (hMDIClientWnd, WM_MDIMAXIMIZE,
(UINT)first_screen->s_drvhandle, 0L);
return TRUE;
} /* maximizescreen */
/* restorescreen: restore the current screen from maximized/minimized state */
/* ============= */
PASCAL restorescreen (int f, int n)
{
ForceMessage (hMDIClientWnd, WM_MDIRESTORE,
(UINT)first_screen->s_drvhandle, 0L);
return TRUE;
} /* restorescreen */
/* tilescreens: tile the non-iconized screens */
/* =========== */
PASCAL tilescreens (int f, int n)
/* without a numeric argument, tile horizontally. With a numeric argument
of 1, tile vertically */
{
if (f && (n == 1)) {
ForceMessage (hMDIClientWnd, WM_MDITILE, MDITILE_HORIZONTAL, 0L);
}
else ForceMessage (hMDIClientWnd, WM_MDITILE, MDITILE_VERTICAL, 0L);
return TRUE;
} /* tilescreens */
/* cascadescreens: position the non-iconized screens in cascade */
/* ============== */
PASCAL cascadescreens (int f, int n)
{
ForceMessage (hMDIClientWnd, WM_MDICASCADE, 0, 0L);
return TRUE;
} /* cascadescreens */
/* ScrollMessage: handle WM_HSCROLL and WM_VSCROLL */
/* ============= */
void FAR PASCAL ScrollMessage (HWND hWnd, UINT wMsg, WORD ScrlCode, int Pos)
{
int Delta;
if (notquiescent) return;
if (wMsg == WM_VSCROLL) {
switch (ScrlCode) {
case SB_LINEUP:
mvupwind (FALSE, 1);
break;
case SB_LINEDOWN:
mvdnwind (FALSE, 1);
break;
case SB_PAGEUP:
backpage (FALSE, 1);
break;
case SB_PAGEDOWN:
forwpage (FALSE, 1);
break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION:
if (Win31API) {
if (ScrlCode == SB_THUMBPOSITION) return;
}
else {
if (ScrlCode == SB_THUMBTRACK) return;
/* there is something wrong with thumb tracking in
Windows 3.0 */
}
Delta = Pos - GetScrollPos (hWnd, SB_VERT);
if (Delta) mvdnwind (TRUE, Delta);
break;
default:
return;
}
curwp->w_flag |= WFMODE;
}
else {
switch (ScrlCode) {
case SB_LINEUP:
Delta = -1;
break;
case SB_LINEDOWN:
Delta = 1;
break;
case SB_PAGEUP:
Delta = -term.t_ncol;
break;
case SB_PAGEDOWN:
Delta = term.t_ncol;
break;
case SB_THUMBTRACK:
Delta = Pos - GetScrollPos (hWnd, SB_HORZ);
break;
default:
return;
}
curwp->w_fcol += Delta;
if (curwp->w_fcol < 0) curwp->w_fcol = 0;
if (curwp->w_doto < curwp->w_fcol) {
/* reframe dot if it was left past the left of the screen */
curwp->w_doto = min(curwp->w_fcol,lused(curwp->w_dotp));
}
if (curwp->w_doto > (curwp->w_fcol + term.t_ncol - 2)) {
/* reframe dot if it was left past the right of the screen */
curwp->w_doto = curwp->w_fcol + term.t_ncol - 2;
}
curwp->w_flag |= WFMODE | WFHARD;
}
if (in_check()) GenerateMenuSeq (IDM_NULLPROC);
/* this ensures we go through the editloop(), updating the
modeline display and running the cmdhook, among other things */
ShowEmacsCaret (FALSE);
update (TRUE);
ShowEmacsCaret (TRUE);
} /* ScrollMessage */
/* ScrollBars: shows/hides, enables/disables scroll bars for all screens */
/* ========== */
void FAR PASCAL ScrollBars (void)
{
static int VScroll = TRUE;
static int HScroll = TRUE;
static int Enabled = TRUE;
int Quiescence;
SCREEN *sp;
if (vscrollbar) vscrollbar = TRUE; /* normalize... */
if (hscrollbar) hscrollbar = TRUE;
Quiescence = (notquiescent == 0);
for (sp = first_screen; sp != (SCREEN*)NULL; sp = sp->s_next_screen) {
if (vscrollbar != VScroll) {
ShowScrollBar ((HWND)sp->s_drvhandle, SB_VERT, vscrollbar);
}
if (hscrollbar != HScroll) {
ShowScrollBar ((HWND)sp->s_drvhandle, SB_HORZ, hscrollbar);
}
if ((Enabled != Quiescence) && Win31API && !TakingANap) {
/* note: no disabling of scroll bars during naps (i.e. fence
matching), to avoid annoying blinking */
EnableScrollBar ((HWND)sp->s_drvhandle, SB_BOTH,
Quiescence ? ESB_ENABLE_BOTH : ESB_DISABLE_BOTH);
}
}
if ((Enabled != Quiescence) && Win31API && !TakingANap) {
Enabled = Quiescence;
}
VScroll = vscrollbar;
HScroll = hscrollbar;
} /* ScrollBars */
/* updscrollbars: updates the scroll bars for a screen */
/* ============= */
PASCAL updscrollbars (SCREEN *sp, char w_flag)
/* the w_flag is used to determine what needs updating: if the WFHARD
bit is set, both scroll bars need an update. If the WFMOVE bit
is set, the horizontal scroll bar needs an update */
/* this function assumes s_cur_window matches curwp for the first_screen */
{
int ScrollMax, ScrollMin, ScrollPos;
if (vscrollbar && (w_flag & WFHARD)) {
int lastline;
int topline;
{ /*-figure-out where we are at vertically */
register LINE *lp;
register LINE *linep; /* header (= last) line of buffer */
LINE *toplp; /* top line of window */
linep = sp->s_cur_window->w_bufp->b_linep;
toplp = sp->s_cur_window->w_linep;
topline = lastline = 0;
lp = linep;
do {
lp = lforw(lp);
lastline++;
if (lp == toplp) topline = lastline;
} while (lp != linep);
}
lastline += 1 - sp->s_cur_window->w_ntrows;
if (lastline <= 1) lastline = 2; /* to avoid scrollbar hiding */
lastline = min(lastline, MAXSCROLL);
topline = min(topline, MAXSCROLL);
GetScrollRange ((HWND)sp->s_drvhandle, SB_VERT,
&ScrollMin, &ScrollMax);
if ((ScrollMax != lastline) || (ScrollMin != 1)) {
SetScrollRange ((HWND)sp->s_drvhandle, SB_VERT, 1,
lastline, FALSE);
ScrollPos = -1; /* makes sure the scroll display is updated */
}
else ScrollPos = GetScrollPos ((HWND)sp->s_drvhandle, SB_VERT);
if (topline != ScrollPos) {
SetScrollPos ((HWND)sp->s_drvhandle, SB_VERT,
topline, TRUE);
}
}
if (hscrollbar && (w_flag & (WFMOVE | WFHARD))) {
/*-figure-out where we stand horizontally */
int row;
LINE *lp;
EWINDOW *wp = sp->s_cur_window;
int maxlength = 0;
lp = wp->w_linep;
for (row = 0; (row < wp->w_ntrows) && (lp != wp->w_bufp->b_linep);
row++) {
maxlength = max(maxlength, lused(lp));
lp = lforw(lp);
}
if (maxlength <= 0) maxlength = 1;
maxlength = min(maxlength, MAXSCROLL);
row = min(wp->w_fcol, MAXSCROLL);
GetScrollRange ((HWND)sp->s_drvhandle, SB_HORZ,
&ScrollMin, &ScrollMax);
if ((ScrollMax != maxlength) || (ScrollMin != 0)) {
SetScrollRange ((HWND)sp->s_drvhandle, SB_HORZ, 0,
maxlength, FALSE);
ScrollPos = -1; /* makes sure the scroll display is updated */
}
else ScrollPos = GetScrollPos ((HWND)sp->s_drvhandle, SB_HORZ);
if (row != ScrollPos) {
SetScrollPos ((HWND)sp->s_drvhandle, SB_HORZ,
row, TRUE);
}
}
} /* updscrollbars */