EdMonad.icl 16 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
implementation module EdMonad

3
4
// the monad that keeps track of the local EditState and another state (mostly the program state)

Diederik van Arkel's avatar
Diederik van Arkel committed
5
6
7
import StdArray, StdBool, StdChar, StdClass, StdFunc, StdInt, StdMisc
import StdMenuElement, StdPSt, StdWindow, StdSystem
import UtilNewlinesFile, StateMonad
8
import StdId
Diederik van Arkel's avatar
Diederik van Arkel committed
9
10
import EdVisualText, EdSelection, EdLook
import EdAction
Diederik van Arkel's avatar
Diederik van Arkel committed
11
12
13
import Platform
//import dodebug
trace_n` _ s :== s
Diederik van Arkel's avatar
Diederik van Arkel committed
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

:: 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	 
45
	 , cursorInfo	:: !CursorInfo`
Diederik van Arkel's avatar
Diederik van Arkel committed
46
47
48
49
50
51
52
53
54
55
	 , selectInfo	:: !SelectInfo
	 , undoInfo		:: !UndoInfo
	 , needSave		:: !Bool
	 , undoeId		:: !Id
	 , lineNumbers	:: !Bool
	 , newlineConv	:: !NewlineConvention
	 , menusel		:: !Maybe String
	 , readOnly		:: !Bool
	 }

56
:: CursorInfo`	// rename to avoid conflict with Object I/O
Diederik van Arkel's avatar
Diederik van Arkel committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
   = { 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
79
	, marginWidth		:: !Int				// logical in #chars
Diederik van Arkel's avatar
Diederik van Arkel committed
80
81
82
83
84
85
86
87
88
	, autoTab			:: !Bool
	, showTabs			:: !Bool
	, showSyntax		:: !Bool
	, syntaxColours		:: !SyntaxColours
	}

:: SyntaxColours =
	{ textColour		:: !Colour
	, backgroundColour	:: !Colour
89
	, marginColour		:: !Colour
Diederik van Arkel's avatar
Diederik van Arkel committed
90
91
92
93
94
	, tabColour			:: !Colour
	, commentColour		:: !Colour
	, stringColour		:: !Colour
	, charColour		:: !Colour
	, keywordColour		:: !Colour
Diederik van Arkel's avatar
Diederik van Arkel committed
95
	, typedefColour		:: !Colour
96
	, typedeclColour	:: !Colour
Diederik van Arkel's avatar
Diederik van Arkel committed
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
	}
	
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
144
setLineNumbers :: !Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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 */

161
162
initEditState :: !Id !Id !String !Font !(Int,Bool,Bool,Bool,Bool) !Int !SyntaxColours !*(PSt .l) -> (EditState, *PSt .l)
initEditState windowId eUndoId pathName font tabs=:(_,_,_,linenos,showSynCol) margin syncols pstate
Diederik van Arkel's avatar
Diederik van Arkel committed
163
  # (tId,pstate) = openId pstate			// P4
164
  # (fontInfo, pstate) = computeFontInfo font tabs margin syncols pstate
Diederik van Arkel's avatar
Diederik van Arkel committed
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
  = (	{ 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
230
setMenuSelection :: (Maybe String) -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
231
232
233
234
235
236
237
238
239
240
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
241
setNewlineConvention :: NewlineConvention -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
242
243
244
245
246
247
248
249
250
251
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
252
setReadOnly :: Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
253
254
255
256
257
258
259
260
261
262
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
263
setText :: !Text -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
264
265
266
267
268
269
270
271
272
273
274
275
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
276
setFontInfo	:: FontInfo -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
277
278
279
280
281
282
setFontInfo fontInfo =
	updateEditState update
where
    update editState
      = {editState & fontInfo=fontInfo}

Diederik van Arkel's avatar
Diederik van Arkel committed
283
appFontInfo :: (FontInfo -> FontInfo) -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
284
285
286
287
288
289
290
291
292
293
294
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
295
setCursorVisibility :: Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
296
297
298
299
300
301
302
303
304
305
306
307
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
308
setSelection :: Selection -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
309
310
311
312
313
314
315
316
317
318
319
320
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
321
setVirtualX :: Int -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
322
323
324
325
326
327
328
329
330
331
332
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
333
setSelectMode :: SelectMode	-> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
350
setFont :: Font -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
351
352
353
setFont font = monad
where
	monad (editState,pState)
354
		# ({tabSize,autoTab,showTabs,showSyntax,marginWidth,syntaxColours},(editState,pState))
Diederik van Arkel's avatar
Diederik van Arkel committed
355
356
357
358
359
360
			= getFontInfo (editState,pState)
		# (linenos,(editState,pState))
			= getLineNumbers (editState,pState)
    	# tabs
    		= (tabSize,autoTab,showTabs,linenos,showSyntax)
		# (fontInfo,(editState,pState))
361
			= accEnv (computeFontInfo font tabs marginWidth syntaxColours) (editState,pState)
Diederik van Arkel's avatar
Diederik van Arkel committed
362
363
364
365
366
367
368
369
370
371
372
		# (_,(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
373
updateEditState :: (EditState -> EditState) -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
374
375
376
377
378
379
380
381
382
383
384
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
385
updateLook :: EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
386
387
388
updateLook 
  = getWindowId		>>>= \windowId ->
	getEditState	>>>= \editState ->
Diederik van Arkel's avatar
Diederik van Arkel committed
389
	appEnv (appPIO (setWindowLook windowId False (True,editWindowLook editState)))
Diederik van Arkel's avatar
Diederik van Arkel committed
390
391
392

// compute some properties of a font

393
394
computeFontInfo :: !Font !(Int,Bool,Bool,Bool,Bool) !Int !SyntaxColours !(PSt .l) -> (FontInfo, PSt .l)
computeFontInfo font (tabSize,autoTab,showTabs,_,showSynCol) marginWidth syncols pstate
Diederik van Arkel's avatar
Diederik van Arkel committed
395
396
397
398
399
400
401
402
403
  # (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
404
	  , marginWidth			= marginWidth
Diederik van Arkel's avatar
Diederik van Arkel committed
405
406
407
408
409
410
411
	  , showTabs			= showTabs
	  , showSyntax			= showSynCol
	  , syntaxColours		= syncols
	  }
	, pstate
	) 

412
DefaultSyntaxColours :: SyntaxColours
Diederik van Arkel's avatar
Diederik van Arkel committed
413
414
415
416
DefaultSyntaxColours =
	{ textColour		= Black
	, tabColour			= Red
	, backgroundColour	= White
417
	, marginColour		= White
Diederik van Arkel's avatar
Diederik van Arkel committed
418
419
420
421
	, commentColour		= Blue
	, stringColour		= Green
	, charColour		= Magenta
	, keywordColour		= Grey
Diederik van Arkel's avatar
Diederik van Arkel committed
422
	, typedefColour		= Black
423
	, typedeclColour	= Black
Diederik van Arkel's avatar
Diederik van Arkel committed
424
425
426
427
428
429
430
431
432
	}

//--

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

Diederik van Arkel's avatar
Diederik van Arkel committed
433
setPathName :: String -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
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
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
480
setNeedSave :: Bool -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
481
482
483
484
485
setNeedSave need =
	getReadOnly												>>>= \readOnly ->
	getEditState											>>>= \{windowId,pathName} ->
	accEnv (accPIO (getWindowTitle windowId))				>>>= \oldTitle ->
	let
Diederik van Arkel's avatar
Diederik van Arkel committed
486
487
488
489
		windowName	= (if readOnly pathNameToWindowTitle` pathNameToWindowTitle) pathName
		windowTitle	= PlatformDependant
						/*Win*/	(if need ("*"+++windowName) windowName)
						/*Mac*/ windowName
Diederik van Arkel's avatar
Diederik van Arkel committed
490
	in
Diederik van Arkel's avatar
Diederik van Arkel committed
491

Diederik van Arkel's avatar
Diederik van Arkel committed
492
493
494
	IF (needsetwin oldTitle windowTitle)
	THEN
	(
Diederik van Arkel's avatar
Diederik van Arkel committed
495
496
497
498
499
500
501
502
503
504
505
506
507
		appEnv (appPIO (setWindowTitle windowId windowTitle))
	)
	ELSE
	(
		skip
	)	>>>

	accEnv (accPIO (getWindowModified windowId))			>>>= \wasModified ->
	IF (isJust wasModified && fromJust wasModified <> need)

	THEN
	(
	appEnv (appPIO (setWindowModified windowId windowName need))		>>>
Diederik van Arkel's avatar
Diederik van Arkel committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
	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
545
setUndoInfo :: UndoInfo -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
546
setUndoInfo undoInfo
547
	=	getEditState				>>>= \{undoeId} ->
Diederik van Arkel's avatar
Diederik van Arkel committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
		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
570
setToolPt :: Point2 -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
571
572
573
574
575
576
577
setToolPt toolPt =
	updateEditState update
where
	update editState	= {editState & toolPt = toolPt}

//--

Diederik van Arkel's avatar
Diederik van Arkel committed
578
getState :: EditMonad (PSt .l) IRState
Diederik van Arkel's avatar
Diederik van Arkel committed
579
580
581
582
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
583
setState :: IRState -> EditMonad (PSt .l) nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
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
Diederik van Arkel's avatar
Diederik van Arkel committed
600