EdMonad.icl 15.4 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
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
/*
 * EdMonad.icl: the monad that keeps track of the local EditState and another state (mostly the program state)
 */

implementation module EdMonad

import StdArray, StdBool, StdChar, StdClass, StdFunc, StdInt, StdMisc
import StdMenuElement, StdPSt, StdWindow, StdSystem
import UtilNewlinesFile, StateMonad
import StdId	// P4

import EdVisualText, EdSelection, EdLook
import EdAction

:: ActionInfo
	= NoInfo
	| CopyInfo ![ClipboardItem] ![ClipboardItem]
	| CutInfo ![ClipboardItem] ![ClipboardItem] !TextFragment !Position !Selection !Bool
	| ClearInfo !TextFragment !Position !Selection !Bool
	| PasteInfo !TextFragment !TextFragment !Selection !Position !Bool
	| InsertInfo !Bool !IRState
	| RemoveInfo !Bool !IRState
	| ReplaceAllInfo !TextFragment !Selection !Selection ![(Selection,Selection,TextFragment,TextFragment)] !Bool
	| UndoneInfo !IRState !IRState

:: UndoInfo =
	{ state		:: !UndoState		// undo or redo
	, action	:: !String			// string describing action to be undone/redone
	, uninfo	:: !ActionInfo
	}

:: TabSize :== Int

:: NewTabSize :== [TabSize]

:: UndoState = None | Undo | Redo

:: EditState
   = { text			:: !Text
	 , pathName		:: !String
	 , windowId		:: !Id
	 , timerId		:: !Id				// P4
	 , toolPt		:: !Point2			// P4
	 , fontInfo		:: !FontInfo	 
	 , cursorInfo	:: !CursorInfo
	 , selectInfo	:: !SelectInfo
	 , undoInfo		:: !UndoInfo
	 , needSave		:: !Bool
	 , undoeId		:: !Id
	 , lineNumbers	:: !Bool
	 , newlineConv	:: !NewlineConvention
	 , menusel		:: !Maybe String
	 , readOnly		:: !Bool
	 }

:: CursorInfo
   = { position		:: !Position
	 , virtualX		:: !Int
	 , visible		:: !Bool
	 }

:: SelectInfo
	= { selection	:: !Selection
	  , selectMode	:: !SelectMode
	  }
	  
:: SelectMode
	= SelectWords Selection
	| SelectLines LineNr
	| SelectChars

:: FontInfo =
	{ thefont			:: !Font
	, lineHeight		:: !Int
	, metrics			:: !FontMetrics
	, tabSize			:: !TabSize			// logical tabsize
//	, tabWidth			:: !NewTabSize		// physical tabsize
	, charWidth			:: !Int				// physical in #pixels
	, autoTab			:: !Bool
	, showTabs			:: !Bool
	, showSyntax		:: !Bool
	, syntaxColours		:: !SyntaxColours
	}

:: SyntaxColours =
	{ textColour		:: !Colour
	, backgroundColour	:: !Colour
	, tabColour			:: !Colour
	, commentColour		:: !Colour
	, stringColour		:: !Colour
	, charColour		:: !Colour
	, keywordColour		:: !Colour
	}
	
instance toString ActionInfo
where
	toString NoInfo	= "N"
	toString (CopyInfo oclip nclip) = "C "
	toString (CutInfo oclip nclip frag pos sel ns) = "X "
	toString (PasteInfo ofrag frag sel pos ns)
		= "V "+++stringsToString ofrag+++":::"+++stringsToString frag+++":::"+++toSelString sel+++":::"+++toPosString pos+++"]]]"
	toString _ = "? "

toSelString {start,end}
    = "{ start = " +++ toPosString start +++ ", end = " +++ toPosString end +++ "}"

toPosString {col,row}
    = "{ col = " +++ toString col +++ ", row = " +++ toString row +++ "}"

instance == UndoState
where
	(==) None None = True
	(==) Undo Undo = True
	(==) Redo Redo = True
	(==) _ _ = False

instance toString UndoState
where
	toString None	= "None"
	toString Undo	= "Undo"
	toString Redo	= "Redo"

::IRState=
	{ txt	:: !Text
	, sel	:: !Selection
	, mod	:: !SelectMode
	, vix	:: !Int
	, vis	:: !Bool
	, ns	:: !Bool
	}

emptyUndoInfo :: UndoInfo
emptyUndoInfo = {state = None, action = "", uninfo = NoInfo}

getLineNumbers :: EditMonad .env Bool
getLineNumbers =
	getEditState		>>>= \{lineNumbers} ->
	result lineNumbers

Diederik van Arkel's avatar
Diederik van Arkel committed
140
setLineNumbers :: !Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
setLineNumbers linenumbers =
	updateEditState update	>>>
	getEditState			>>>= \{windowId} ->
	computeViewDomain		>>>= \viewDomain=:{corner1=c1=:{x}} ->
	let	maybemove = if linenumbers (moveWindowViewFrame windowId {zero & vx = x}) (id)
	in
	appEnv (appPIO (maybemove o setWindowViewDomain windowId viewDomain))
where
	update editState =
		{ editState & lineNumbers = linenumbers}

:: EditMonad env a
   :== StateM *(!EditState, env) a

/* EXPORTED FUNCTIONS */

Diederik van Arkel's avatar
Diederik van Arkel committed
157
initEditState :: !Id !Id !String !Font !(Int,Bool,Bool,Bool,Bool) !SyntaxColours !*(PSt .l) -> (EditState, *PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
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
initEditState windowId eUndoId pathName font tabs=:(_,_,_,linenos,showSynCol) syncols pstate
  # (tId,pstate) = openId pstate			// P4
  # (fontInfo, pstate) = computeFontInfo font tabs syncols pstate
  = (	{ text			= newText
		, pathName		= pathName
		, windowId		= windowId
		, timerId		= tId	// P4
		, toolPt		= zero	// P4
		, fontInfo		= fontInfo
		, selectInfo	= { selection	= emptySelection
						  , selectMode	= SelectChars 
						  }
		, cursorInfo	= { position	= {col=0, row=0}
						  , virtualX	= 0
						  , visible		= True
						  }
		, undoInfo		= emptyUndoInfo
		, needSave		= False
		, undoeId		= eUndoId
		, lineNumbers	= linenos
		, newlineConv	= NewlineConventionNone
		, menusel		= Nothing
		, readOnly		= False
		}
	, pstate
	)
		     	  
getEditState :: EditMonad .env EditState
getEditState
  = getEditState`
  where
    getEditState` (editState, env)
      = (editState, (editState, env))

appEnv :: (.env -> .env) -> EditMonad .env nothing
appEnv envFun
  = appEnv`
  where
	appEnv` (editState, env)
	  # env = envFun env
	  = (nothing, (editState, env))

accEnv :: (.env -> (.a, .env)) -> EditMonad .env .a
accEnv envFun
  = accEnv`
  where
	accEnv` (editState, env)
	  # (a, env) = envFun env
	  = (a, (editState, env))

noResult :: !(EditMonad .env a) *(EditState, .env) 
				-> (EditState, .env)
noResult ma state
  # (_, state) = ma state
  = state

onlyEnv :: !(EditMonad .env a) *(EditState, .env) -> .env
onlyEnv ma state
  # (_, (_, env)) = ma state
  = env

// SELECTORS & MODIFIERS

getMenuSelection :: EditMonad .env (Maybe String)
getMenuSelection =
	getEditState	>>>= \{menusel} ->
	result menusel

Diederik van Arkel's avatar
Diederik van Arkel committed
226
setMenuSelection :: (Maybe String) -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
227
228
229
230
231
232
233
234
235
236
setMenuSelection menusel =
	updateEditState update
where
	update editState = {editState & menusel = menusel}

getNewlineConvention :: EditMonad .env NewlineConvention
getNewlineConvention =
	getEditState	>>>= \{newlineConv} ->
	result newlineConv

Diederik van Arkel's avatar
Diederik van Arkel committed
237
setNewlineConvention :: NewlineConvention -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
238
239
240
241
242
243
244
245
246
247
setNewlineConvention newlineConv =
	updateEditState update
where
	update editState = {editState & newlineConv = newlineConv}

getReadOnly :: EditMonad .env Bool
getReadOnly =
	getEditState	>>>= \{readOnly} ->
	result readOnly

Diederik van Arkel's avatar
Diederik van Arkel committed
248
setReadOnly :: Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
249
250
251
252
253
254
255
256
257
258
setReadOnly readOnly =
	updateEditState update
where
	update editState = {editState & readOnly = readOnly}

getText :: EditMonad .env Text
getText
  = getEditState	>>>= \{text} ->
	result text

Diederik van Arkel's avatar
Diederik van Arkel committed
259
setText :: !Text -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
260
261
262
263
264
265
266
267
268
269
270
271
setText text =
	updateEditState update	>>>
	updateLook
where
    update editState
      = {editState & text = text}

getFontInfo :: EditMonad .env FontInfo
getFontInfo
  = getEditState	>>>= \{fontInfo} ->
	result fontInfo
	
Diederik van Arkel's avatar
Diederik van Arkel committed
272
setFontInfo	:: FontInfo -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
273
274
275
276
277
278
setFontInfo fontInfo =
	updateEditState update
where
    update editState
      = {editState & fontInfo=fontInfo}

Diederik van Arkel's avatar
Diederik van Arkel committed
279
appFontInfo :: (FontInfo -> FontInfo) -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
280
281
282
283
284
285
286
287
288
289
290
appFontInfo fontFun =
	updateEditState update	>>>
	updateLook
where
	update editState = {editState & fontInfo = fontFun editState.fontInfo}

getCursorVisibility :: EditMonad .env Bool
getCursorVisibility
  = getEditState	>>>= \{cursorInfo} ->
	result cursorInfo.visible
	
Diederik van Arkel's avatar
Diederik van Arkel committed
291
setCursorVisibility :: Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
292
293
294
295
296
297
298
299
300
301
302
303
setCursorVisibility visible =
	updateEditState update	>>>
	updateLook
where
    update editState=:{cursorInfo}
      = {editState & cursorInfo={cursorInfo & visible=visible}}

getSelection :: EditMonad .env Selection
getSelection
  = getEditState	>>>= \{selectInfo={selection}} ->
	result selection
	
Diederik van Arkel's avatar
Diederik van Arkel committed
304
setSelection :: Selection -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
305
306
307
308
309
310
311
312
313
314
315
316
setSelection selection =
	updateEditState update	>>>
	updateLook
where
    update editState=:{selectInfo}
      = {editState & selectInfo={selectInfo & selection=selection}}
      
getVirtualX	:: EditMonad .env Int
getVirtualX 
  = getEditState	>>>= \{cursorInfo={virtualX}} ->
	result virtualX
	
Diederik van Arkel's avatar
Diederik van Arkel committed
317
setVirtualX :: Int -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
318
319
320
321
322
323
324
325
326
327
328
setVirtualX virtualX
  = updateEditState update
where
    update editState=:{cursorInfo}
      = {editState & cursorInfo={cursorInfo & virtualX=virtualX}}
      
getSelectMode :: EditMonad .env SelectMode
getSelectMode
  = getEditState	>>>= \{selectInfo={selectMode}} ->
	result selectMode
	
Diederik van Arkel's avatar
Diederik van Arkel committed
329
setSelectMode :: SelectMode	-> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
setSelectMode selectMode
  = updateEditState update
where
    update editState=:{selectInfo}
      = {editState & selectInfo={selectInfo & selectMode=selectMode}}

getCursorHeight :: EditMonad .env Int
getCursorHeight
  = getEditState	>>>= \{fontInfo={lineHeight}} ->
	result lineHeight

getWindowId :: EditMonad .env Id
getWindowId
  = getEditState	>>>= \{windowId} ->
	result windowId

Diederik van Arkel's avatar
Diederik van Arkel committed
346
setFont :: Font -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
setFont font = monad
where
	monad (editState,pState)
		# ({tabSize,autoTab,showTabs,showSyntax,syntaxColours},(editState,pState))
			= getFontInfo (editState,pState)
		# (linenos,(editState,pState))
			= getLineNumbers (editState,pState)
    	# tabs
    		= (tabSize,autoTab,showTabs,linenos,showSyntax)
		# (fontInfo,(editState,pState))
			= accEnv (computeFontInfo font tabs syntaxColours) (editState,pState)
		# (_,(editState,pState))
			= updateEditState (update fontInfo)	(editState,pState)
		# (_,(editState,pState))
			= updateLook (editState,pState)
		# (_,(editState,pState))
			= vResetViewDomain (editState,pState)
		= (undef,(editState,pState))
	where
	    update fontInfo editState
	      = { editState & fontInfo = fontInfo }

Diederik van Arkel's avatar
Diederik van Arkel committed
369
updateEditState :: (EditState -> EditState) -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
370
371
372
373
374
375
376
377
378
379
380
updateEditState editStateFun
  = updateEditState`
where 
	updateEditState` (editState, pstate)
	  #! editState = editStateFun editState
	  = (nothing, (editState, pstate))
	  
// updateLook updates the look function of the window. This
// look function is not applied, so no visible update is
// caused by updateLook.

Diederik van Arkel's avatar
Diederik van Arkel committed
381
updateLook :: EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
382
383
384
updateLook 
  = getWindowId		>>>= \windowId ->
	getEditState	>>>= \editState ->
Diederik van Arkel's avatar
Diederik van Arkel committed
385
	appEnv (appPIO (setWindowLook windowId False (True,editWindowLook editState)))
Diederik van Arkel's avatar
Diederik van Arkel committed
386
387
388

// compute some properties of a font

Diederik van Arkel's avatar
Diederik van Arkel committed
389
computeFontInfo :: !Font !(Int,Bool,Bool,Bool,Bool) !SyntaxColours !(PSt .l) -> (FontInfo, PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
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
computeFontInfo font (tabSize,autoTab,showTabs,_,showSynCol) syncols pstate
  # (metrics, pstate)	= accPIO (accScreenPicture (getFontMetrics font)) pstate
	lineHeight			= metrics.fAscent + metrics.fDescent + metrics.fLeading
	(charWidth,pstate) = accPIO (accScreenPicture (getFontStringWidth font "M")) pstate
  = ( { thefont				= font
	  , lineHeight			= lineHeight
	  , metrics				= metrics
	  , tabSize				= tabSize
	  , autoTab				= autoTab
	  , charWidth			= charWidth
	  , showTabs			= showTabs
	  , showSyntax			= showSynCol
	  , syntaxColours		= syncols
	  }
	, pstate
	) 

DefaultSyntaxColours :: !SyntaxColours
DefaultSyntaxColours =
	{ textColour		= Black
	, tabColour			= Red
	, backgroundColour	= White
	, commentColour		= Blue
	, stringColour		= Green
	, charColour		= Magenta
	, keywordColour		= Grey
	}

//--

getPathName :: EditMonad .env String
getPathName =
	getEditState		>>>= \{pathName} ->
	result pathName

Diederik van Arkel's avatar
Diederik van Arkel committed
425
setPathName :: String -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
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
setPathName path =
	updateEditState update
where
	update editState	= {editState & pathName = path}

getNeedSave :: EditMonad .env Bool
getNeedSave =
	getEditState		>>>= \{needSave} ->
	result needSave

pathNameToWindowTitle :: !String -> String
pathNameToWindowTitle pathName
	= RemovePath pathName+++" - "+++pathName
where
	RemovePath	:: !String -> String;
	RemovePath path
		| found	= path % (inc position, last);
				= path;
		where 
			(found,position)= FindLastChar dirseparator path last;
			last			= dec (size path);
	
	FindLastChar :: !Char !String !Int -> (!Bool, !Int);
	FindLastChar c s i
		| i <= 0			= (False,0);
		| c ==  s.[i]		= (True, i);
							= FindLastChar c s (dec i);

pathNameToWindowTitle` :: !String -> String
pathNameToWindowTitle` pathName
	= "["+++RemovePath pathName+++"] - "+++pathName
where
	RemovePath	:: !String -> String;
	RemovePath path
		| found	= path % (inc position, last);
				= path;
		where 
			(found,position)= FindLastChar dirseparator path last;
			last			= dec (size path);
	
	FindLastChar :: !Char !String !Int -> (!Bool, !Int);
	FindLastChar c s i
		| i <= 0			= (False,0);
		| c ==  s.[i]		= (True, i);
							= FindLastChar c s (dec i);


Diederik van Arkel's avatar
Diederik van Arkel committed
473
setNeedSave :: Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
setNeedSave need =
	getReadOnly												>>>= \readOnly ->
	getEditState											>>>= \{windowId,pathName} ->
	accEnv (accPIO (getWindowTitle windowId))				>>>= \oldTitle ->
	let
		windowName = (if readOnly pathNameToWindowTitle` pathNameToWindowTitle) pathName
		windowTitle = if need ("*"+++windowName) windowName
	in
	IF (needsetwin oldTitle windowTitle)
	THEN
	(
	appEnv (appPIO (setWindowTitle windowId windowTitle) )	>>>
	updateEditState update
	)
	ELSE
	(
	updateEditState update
	)														>>>
	IF needfixundo
	THEN
		(
		updateEditState undoupdate
		)
	ELSE
		skip
where
	update editState	= {editState & needSave = need}
	needsetwin Nothing _ = True
	needsetwin (Just oldtitle) newtitle = oldtitle <> newtitle
	needfixundo = not need
	undoupdate editState=:{undoInfo} = {editState & undoInfo = fix undoInfo}
	fix ui=:{uninfo,state}
		# info = case uninfo of		// fix to correct undo'd need save state after save
			(NoInfo)				-> NoInfo
			(CopyInfo x y)			-> CopyInfo x y
			(CutInfo a b c d e ns)	-> CutInfo a b c d e True
			(ClearInfo a b c ns)	-> ClearInfo a b c True
			(PasteInfo a b c d ns)	-> PasteInfo a b c d True
			(InsertInfo a ir)		-> InsertInfo a {ir & ns = True}
			(RemoveInfo a ir)		-> RemoveInfo a {ir & ns = True}
			(ReplaceAllInfo nfrag osel nsel ul ns) -> (ReplaceAllInfo nfrag osel nsel ul True)
			(UndoneInfo or nr)		-> UndoneInfo {or & ns = (state==Undo)} {nr & ns = (state==Redo)}
		= {ui & uninfo = info}

getUndoInfo :: EditMonad .env UndoInfo
getUndoInfo
	=	getEditState		>>>= \{undoInfo} ->
		result undoInfo

Diederik van Arkel's avatar
Diederik van Arkel committed
523
setUndoInfo :: UndoInfo -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
setUndoInfo undoInfo
	=	getEditState				>>>= \{/*undomId,*/undoeId} ->
		appEnv (appPIO (mfun undoeId))	>>>
		updateEditState update
where
    update editState
      = {editState & undoInfo = undoInfo}
	mfun r		= case undoInfo.state of
					None	-> disableMenuElements [r] o setMenuElementTitles [(r,"Undo")]
					Undo	-> enableMenuElements [r] o setMenuElementTitles [(r,"Undo"+++undoInfo.action)]
					Redo	-> enableMenuElements [r] o setMenuElementTitles [(r,"Redo"+++undoInfo.action)]

//-- P4

getTimerId :: EditMonad .env Id
getTimerId
  = getEditState	>>>= \{timerId} ->
	result timerId

getToolPt :: EditMonad .env Point2
getToolPt
  = getEditState	>>>= \{toolPt} ->
	result toolPt

Diederik van Arkel's avatar
Diederik van Arkel committed
548
setToolPt :: Point2 -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
549
550
551
552
553
554
555
setToolPt toolPt =
	updateEditState update
where
	update editState	= {editState & toolPt = toolPt}

//--

Diederik van Arkel's avatar
Diederik van Arkel committed
556
getState :: EditMonad (PSt .l) IRState
Diederik van Arkel's avatar
Diederik van Arkel committed
557
558
559
560
getState =
	getEditState	>>>= \{text,selectInfo={selection,selectMode},cursorInfo={virtualX,visible},needSave} ->
	result {txt=text,sel=selection,mod=selectMode,vix=virtualX,vis=visible,ns=needSave}

Diederik van Arkel's avatar
Diederik van Arkel committed
561
setState :: IRState -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
setState state =
	updateEditState update		>>>
	updateLook					>>>
	setNeedSave     state.ns
where
	update editState=:{selectInfo,cursorInfo}
	  =	{ editState
		& text			= text
		, selectInfo	= {selectInfo & selection=selection, selectMode=selectMode}
		, cursorInfo	= {cursorInfo & virtualX = virtualX, visible = visible}
		}
	text		= state.txt
	selection	= state.sel
	selectMode	= state.mod
	virtualX	= state.vix
	visible		= state.vis