File: edit.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (449 lines) | stat: -rw-r--r-- 11,855 bytes parent folder | download | duplicates (4)
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
/* edit - Macintosh editing functions                                  */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */
 
#include "xlisp.h"
#include "xlstat.h"
#include "xlgraph.h"
#include "TransEdit1.h"

/* external variables */
extern LVAL sk_show, sk_allocate, s_istream, s_ostream, s_input_stream,
  s_input_enabled, s_hardware_address, sk_go_away, s_go_away, sk_dispose,
  s_title, s_bind_to_file;
extern WindowPtr ttyWind;
extern int hasAppleEvents;

#define dfltLeft 10
#define dfltTop 40
#define dfltWidth 490
#define dfltHeight 280

# define check_edit_window_address(d) valid_edit_window_address(slot_value(d, s_hardware_address))

/* forward declarations */
LOCAL int match(char *, char *, long);

/***********************************************************************/
/**                                                                   **/
/**                          Utilitiy Functions                       **/
/**                                                                   **/
/***********************************************************************/

LVAL xssetvolume(void)
{
  DirSpec newdir, olddir;
  LVAL val;
  
  GetDir(&olddir);
  if (moreargs()) {
    LVAL arg = xlgalist();
    if (! fixp(car(arg)) || ! fixp(cdr(arg)))
      xlbadtype(arg);
    newdir.vRefNum = getfixnum(car(arg));
    newdir.parID = getfixnum(cdr(arg));
  }
  else {
    newdir = default_dir;
  }
  xllastarg();
  
  SetDir(&newdir);
  xlsave1(val);
  val = consd(cvfixnum((FIXTYPE) olddir.parID));
  rplaca(val, cvfixnum((FIXTYPE) olddir.vRefNum));
  xlpop();
  return val;
}

LVAL xsopenfiledialog(void)
{
  SFTypeList myTypes;
  Point p;
  short ntypes;
  LVAL result;
  Boolean setvol = TRUE;
  LVAL arg, next;;
  
  myTypes[0]='TEXT';
  ntypes = 1;
  
  if (moreargs()) {
    setvol = (xlgetarg() != NIL) ? TRUE : FALSE;
    if (moreargs()) {
      arg = xlgetarg();
      if (null(arg))
        ntypes = -1;
      else if (stringp(arg))
        myTypes[0] = string_to_type(getstring(arg));
      else if (consp(arg))
        for (ntypes = 0, next = arg; consp(next) && ntypes < 4; next = cdr(next)) {
          if (! stringp(car(next)))
            xlbadtype(arg);
          myTypes[ntypes] = string_to_type(getstring(car(next)));
          ntypes++;
        }
      else
        xlbadtype(arg);
    }       
  }
  xllastarg();
	
  NotifyIfInBackground();
  if (hasAppleEvents) {
    StandardFileReply reply;
    
    StandardGetFile(nil, ntypes, myTypes, &reply);
    if (reply.sfGood) {
      PtoCstr((StringPtr) reply.sfFile.name);
      result = cvstring((char *) &reply.sfFile.name);
      if (setvol) {
        DirSpec dir;
        dir.vRefNum = reply.sfFile.vRefNum;
        dir.parID = reply.sfFile.parID;
        SetDir(&dir);
      }
    }
    else result = NIL;
  }
  else {
    SFReply reply;

    SetPt(&p, 82, 90);
    SFGetFile( p, "\p", 0L, ntypes, myTypes, 0L, &reply );
    if (reply.good) {
      PtoCstr((StringPtr) &reply.fName);
      result = cvstring((char *) &reply.fName);
      if (setvol) SetVol(nil, reply.vRefNum);
    }
    else result = NIL;
  }
  return(result);
}

LVAL xssetfiledialog(void)
{
  LVAL result;
  char *prompt, *dflt = "";
  Str255 pprompt, pdflt;
  Boolean setvol = TRUE;
  
  prompt = (char *) getstring(xlgastring());
  if (moreargs())
    dflt = (char *) getstring(xlgastring());
  if (moreargs())
    setvol = (xlgetarg() != NIL) ? TRUE : FALSE;
  xllastarg();
  
  NotifyIfInBackground();
  CintoPstring(prompt, pprompt, sizeof pprompt, FALSE);
  CintoPstring(dflt, pdflt, sizeof pdflt, FALSE);
  if (hasAppleEvents) {
    StandardFileReply reply;
    
    StandardPutFile(pprompt, pdflt, &reply);
    if (reply.sfGood) {
      PtoCstr((StringPtr) reply.sfFile.name);
      result = cvstring((char *) &reply.sfFile.name);
      if (setvol) {
        DirSpec dir;
        dir.vRefNum = reply.sfFile.vRefNum;
        dir.parID = reply.sfFile.parID;
        SetDir(&dir);
      }
    }
    else result = NIL;
  }
  else {
    SFReply reply;
    Point p;

    SetPt(&p, 82, 90);
    SFPutFile(p, pprompt, pdflt, nil, &reply);
    if (reply.good) {
      PtoCstr((StringPtr) &reply.fName);
      result = cvstring((char *) &reply.fName);
      if (setvol) SetVol(nil, reply.vRefNum);
    }
    else result = NIL;
  }
	
  return(result);
}

LVAL xsfrontwindow(void) { return(get_hardware_object_by_address((CPTR) FrontWindow())); }

LVAL xshidefrontwindow(void)
{
	WindowPeek wPeek;
	
	xllastarg();
	
	if ((wPeek = (WindowPeek) FrontWindow()) != nil) {
		if (wPeek->windowKind < 0)
	    	CloseDeskAcc(wPeek->windowKind);
	    else
	      	HideWindow(FrontWindow());
	}
	return(NIL);
}

LVAL xssystem_edit(void)
{
  int item;
  
  item = getfixnum(xlgafixnum());
  xllastarg();
  
  return((SystemEdit(item)) ? s_true : NIL);
}

/***********************************************************************/
/**                                                                   **/
/**                          Listener Methods                         **/
/**                                                                   **/
/***********************************************************************/

LVAL xslistener_isnew(void)
{
  LVAL object = xlgaobject();
  
  if (xsboolkey(sk_show, TRUE)) send_message(object, sk_allocate);
  if (slot_value(object, s_istream) == NIL)
    set_slot_value(object, s_istream, getvalue(s_input_stream));
  if (slot_value(object, s_ostream) == NIL)
    set_slot_value(object, s_ostream, newustream());
  set_slot_value(object, s_input_enabled, s_true);
  return(object);
}

LVAL xslistener_allocate(void)
{
  LVAL object = xlgaobject();
  WindowData data;
  
  if (get_window_data(ttyWind) == nil) {
    data = (WindowData) StCalloc(sizeof(struct window_data), 1);
    set_window_data(ttyWind, data);
  }
  if (slot_value(object, s_hardware_address) != NIL)
    standard_hardware_clobber(object);
  set_edit_window_address((CPTR) ttyWind, object);
  set_window_object(ttyWind, object);
  return(object);
}

static LVAL clip_stream(void)
{
  LVAL stream;
  int numChar, count;
  char **scrapH, ch;

  TEFromScrap ();
  numChar = TEGetScrapLength();
  scrapH = TEScrapHandle();

  xlsave1(stream);

  stream = newustream();

  for (count = 0; count < numChar; count++) {
    ch = (*scrapH)[count];
    if (ch == '\r') ch = '\n';
    xlputc(stream, ch);
  }
  
  xlpop();	
  return(stream);
}

/***********************************************************************/
/**                                                                   **/
/**                         Edit Window Methods                       **/
/**                                                                   **/
/***********************************************************************/

LVAL xsedit_window_isnew(void)
{
  LVAL object, value;
  
  object = xlgaobject();
  object_isnew(object);
    
  if (slot_value(object, s_ostream) == NIL)
    set_slot_value(object, s_ostream, newustream());
  set_slot_value(object, s_input_enabled, s_true);
  if (! xlgetkeyarg(sk_go_away, &value)) value = s_true;
  set_slot_value(object, s_go_away, value);
  
  if (xsboolkey(sk_show, TRUE)) send_message(object, sk_allocate);
  return(object);
}

LVAL xsedit_window_allocate(void)
{
  LVAL object, value;
  Rect bounds;
  char *title;
  int visible, goAway, bindToFile;
  WindowPtr behind;
  long refNum;
  WindowPtr w;
  WindowData data;
  int left, top, width, height;
  Str255 ptitle;
  object = xlgaobject();
  if (check_edit_window_address(object))
    send_message(object, sk_dispose);

  left = dfltLeft;
  top = dfltTop;
  width = dfltWidth;
  height = dfltHeight;
  get_window_bounds(object, &left, &top, &width, &height);
  SetRect(&bounds, left, top, left + width, top + height);

  value = slot_value(object, s_title);
  title = stringp(value) ? (char *) getstring(value) : "";
  if (strlen(title) == 0) title = nil;
  goAway = slot_value(object, s_go_away) != NIL;
  visible = TRUE;
  behind = (WindowPtr) -1;
  refNum = (long) nil;
  bindToFile = (slot_value(object, s_bind_to_file) != NIL) ? TRUE : FALSE;
  
  if (title != nil)
    CintoPstring(title, ptitle, sizeof ptitle, FALSE);

  w = NewEWindow (&bounds, (StringPtr) (title == nil ? nil : ptitle), visible, behind, 
                  goAway, refNum, bindToFile);
  if (w == NULL) return(NIL);
  else {
    SetEWindowStyle (w, editFontNum, editFontSize, editFontStyle, 0, teJustLeft);
    set_edit_window_procs(w);
    set_edit_window_address((CPTR) w, object);
    data = (WindowData) StCalloc(sizeof(struct window_data), 1);
    set_window_data(w, data);
    set_window_object(w, object);
    return(object);
  }
}

static LVAL edit_window_edit(int which)
{
  WindowPtr w;

  w = (WindowPtr) get_edit_window_address(xlgetarg());
  xllastarg();
   
  if (IsEWindow(w) && w == FrontWindow()) {
    if (which == 3 || which == 5) adjust_insert(w);
    EWindowEditOp(which);
    if (w == ttyWind && which == 5) return_action(GetEWindowTE(w));
    if (w == ttyWind) SetEWindowDirty(w, FALSE);
  }
  return(NIL);
}

LVAL xsedit_window_cut(void)   { return(edit_window_edit(3)); }
LVAL xsedit_window_copy(void)  { return(edit_window_edit(4)); }
LVAL xsedit_window_paste(void) { return(edit_window_edit(5)); }

static LVAL ewindow_action(int action)
{
  WindowPtr w;
  int result;

  w = (WindowPtr) get_edit_window_address(xlgetarg());
  xllastarg();
  if (w == ttyWind) return(NIL);
  
  switch (action) {
  case 'R': result = EWindowRevert(w); break;
  case 'S': result = EWindowSave(w); break;
  case 'A': result = EWindowSaveAs(w); break;
  case 'C': result = EWindowSaveCopy(w); break;
  }
  return((result) ? s_true : NIL);
}

LVAL xsedit_window_revert(void)    { return(ewindow_action('R')); }
LVAL xsedit_window_save(void)      { return(ewindow_action('S')); }
LVAL xsedit_window_save_as(void)   { return(ewindow_action('A')); }
LVAL xsedit_window_save_copy(void) { return(ewindow_action('C')); }

LVAL xsedit_window_selection_stream(void)
{
  WindowPtr w;
  TEHandle te;
	
  w = (moreargs()) ? (WindowPtr) get_edit_window_address(xlgetarg()) : nil;
  xllastarg();

  if (w != nil) {
    te = GetEWindowTE(w);
    if (te == nil) xlfail("not an edit window");
    TECopy (te);
    ZeroScrap ();
    TEToScrap ();
  }
  return(clip_stream());
}

LVAL xsedit_window_remove(void)
{
  WindowPtr w;
  LVAL object;
  int result;
  WindowData data;
  
  object = xlgaobject();
  if (check_edit_window_address(object)) {
    w = (WindowPtr) get_edit_window_address(object);
    data = (WindowData) get_window_data(w);
    if (w == ttyWind) result = FALSE;
    else result = (! IsEWindow(w) || EWindowClose(w));
    if (result) StFree(data);
  }
  else result = TRUE;
  if (result) standard_hardware_clobber(object);
  return((result) ? s_true : NIL);
}

LVAL xsedit_window_find(void)
{
  WindowPtr w;
  int result = FALSE;
  char **ptext, *text, *s;
  long i, n, len, start;
  TEHandle te;
  
  w = (WindowPtr) get_edit_window_address(xlgaobject());
  s = (char *) getstring(xlgastring());

  if (IsEWindow(w) && (te = GetEWindowTE(w)) != nil) {
    n = (*te)->teLength;
    start = (*te)->selEnd;
    ptext = (char **) TEGetText(te);
    len = strlen(s);
    for (i = start; i < n - len; i++) {
      text = *ptext + i;
      if (match(s, text, len)) {
        TESetSelect (i, i + len, te);
        result = TRUE;
        break;
      }
    }
  }
  if (result) EWindowOverhaul(w, TRUE, FALSE, FALSE);
  return(result ? s_true : NIL);
}

LOCAL int match(char *s1, char *s2, long n)
{
  while (n-- > 0)
    if (toupper(*s1++) != toupper(*s2++)) return(FALSE);
  return(TRUE);
}