EdClient.icl 25.6 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
implementation module EdClient

//*********************************************************************************
// Original Clean Library Software Module
// Written for Clean version  : 1.3
// Written for I/O version    : 1.2
// Author                     : Diederik van Arkel
// Date                       :
// Last Modified by           :
// Date                       :
// Copyright                  : 1999 Hilt - High Level Software Tools B.V.
//                            : University of Nijmegen
// e-mail                     : clean@cs.kun.nl or rinus@hilt.nl
//*********************************************************************************
// It is allowed to modify this module for your own purposes but it is NOT allowed
// to (re)distribute the code or the modified code in ANY form without written
// permission.
//*********************************************************************************

/*
 * EdClient.icl: only this module should be imported by users of the editor
 */

import StdTuple, StdList, StdArray, StdMisc, StdEnum
import StdWindow, StdClipboard, StdPSt, StdPrintText, StdId, StdPStClass
import EdVisualText, EdVisualCursor, EdAction
import EdFind, EdState, EdMessage, EdMonad, EdFile
import PmPath
import StrictList
import balance, textcharstream
import EdCommon
import EdText
import IdeState, UtilNewlinesFile


Diederik van Arkel's avatar
Diederik van Arkel committed
36
isEditWin :: Id *(PSt *l) -> *(Bool,*PSt *l) | Editor l
Diederik van Arkel's avatar
Diederik van Arkel committed
37 38
isEditWin windowId pState
  # (editorState, pState)		= getEditorState pState
39
  # (maybeEditId, editorState)	= findReceiver windowId editorState
Diederik van Arkel's avatar
Diederik van Arkel committed
40 41 42 43
  # iseditwin					= not (isNothing maybeEditId)
  # pState						= setEditorState editorState pState
  = (iseditwin, pState)

Diederik van Arkel's avatar
Diederik van Arkel committed
44
message :: !Id !.(EditAction *l .r) !*(PSt *l) -> *(Maybe .r,*PSt *l) | Editor l
Diederik van Arkel's avatar
Diederik van Arkel committed
45 46
message windowId monad pState
  # (editorState, pState)		= getEditorState pState
47
  # (maybeEditId, editorState)	= findReceiver windowId editorState
Diederik van Arkel's avatar
Diederik van Arkel committed
48 49 50
  | isNothing maybeEditId
  	= (Nothing, setEditorState editorState pState)
  # editId						= fromJust maybeEditId
51
  # (x, pState)					= appEditState editId monad pState
Diederik van Arkel's avatar
Diederik van Arkel committed
52 53 54
  # pState						= setEditorState editorState pState
  = (Just x, pState)

Diederik van Arkel's avatar
Diederik van Arkel committed
55
sendToActiveWindow :: .(EditAction *l .r) !*(PSt *l) -> *(Maybe .r,*PSt *l) | Editor l
Diederik van Arkel's avatar
Diederik van Arkel committed
56
sendToActiveWindow editAction pState
57
  # (maybeId, pState)			= accPIO getActiveWindow pState
Diederik van Arkel's avatar
Diederik van Arkel committed
58 59
  | isNothing maybeId 
	= (Nothing,pState)	// fail silently
60 61 62 63
  #	windowId					= fromJust maybeId
    (editorState,pState) 		= getEditorState pState
  # (maybeEditId, editorState)	= findReceiver windowId editorState
  | isNothing maybeEditId
Diederik van Arkel's avatar
Diederik van Arkel committed
64 65 66 67 68
  	= (Nothing,pState)
  = message windowId editAction pState
	
//--

Diederik van Arkel's avatar
Diederik van Arkel committed
69
msgSetFont :: Font -> EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
70 71 72
msgSetFont font
  = setFont font

Diederik van Arkel's avatar
Diederik van Arkel committed
73
msgGetFont :: EditAction .l Font
Diederik van Arkel's avatar
Diederik van Arkel committed
74 75 76 77
msgGetFont 
  = getFontInfo						>>>= \{ thefont } ->
	result thefont

Diederik van Arkel's avatar
Diederik van Arkel committed
78
msgSetTabs :: !(Int,Bool,Bool) -> EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
79 80 81 82 83 84
msgSetTabs (t,a,s) =
	getFontInfo														>>>= \fontinfo ->
  	setFontInfo {fontinfo & tabSize = t, autoTab = a, showTabs = s} >>>
  	setFont fontinfo.thefont >>>
  	skip

Diederik van Arkel's avatar
Diederik van Arkel committed
85
msgGetTabs :: EditAction .l (Int,Bool,Bool)
Diederik van Arkel's avatar
Diederik van Arkel committed
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
msgGetTabs 
  = getFontInfo						>>>= \{ tabSize,autoTab,showTabs } ->
	result (tabSize,autoTab,showTabs)

//--

msgUndo :: EditAction General nothing
msgUndo
	=	undoAction											>>>
		getUndoInfo											>>>= \undoinfo=:{state,uninfo} ->
		IF (state == Undo)
		THEN
			(case uninfo of
			(CopyInfo newclip oldclip) ->
				appEnv (setClipboard oldclip)					>>>
				setUndoInfo {undoinfo & state = Redo}
			(CutInfo newclip oldclip fragment position osel needsave) ->
				appEnv (setClipboard oldclip)					>>>
				vDoCursorSafe (
					vInsertText osel.start fragment		>>>
					vChangeSelectionTo osel				>>>
					mChangeSelectionTo osel				>>>
					vCenterCursor
					)											>>>
				setNeedSave needsave							>>>
				setUndoInfo {undoinfo & state = Redo}
			(ClearInfo fragment position osel needsave) ->
			    vDoCursorSafe (
					vInsertText osel.start fragment					>>>
					vChangeSelectionTo osel							>>>
					mChangeSelectionTo osel							>>>
					vCenterCursor
					)											>>>
				setNeedSave needsave							>>>
				setUndoInfo {undoinfo & state = Redo}
			(PasteInfo fragment ofragment selection position needsave) ->
			    vDoCursorSafe (
					vRemoveText {start=position,end=newCursorPos position fragment}	>>>
					vInsertText position ofragment									>>>
					vChangeSelectionTo selection									>>>
					mChangeSelectionTo selection									>>>
					vCenterCursor
					)															>>>
				setNeedSave needsave											>>>
				setUndoInfo {undoinfo & state = Redo}
			(ReplaceAllInfo nfrag osel nsel ulist needsave) ->
				vChangeSelectionTo emptySelection >>>
				getText														>>>= \text ->
				let
					txt` = undoReplaceAll (reverse ulist) text
				in
				vDoCursorSafe (
					// loop ulist and replace all nfrag's by ofrag
					setText txt` >>>
					vResetViewDomain										>>>
					vChangeSelectionTo osel									>>>
					mChangeSelectionTo osel									>>>
					vCenterCursor
					)					>>>
				setNeedSave needsave	>>>
				setUndoInfo {undoinfo & state = Redo}
			(UndoneInfo ini fin) ->
				setState ini									>>>
		//		getWindowId										>>>= \win ->
		//		appEnv (appPIO (updateWindow win Nothing))		>>>
				setUndoInfo {undoinfo & state = Redo}			>>>
				vResetViewDomain
			_ -> abort "EdClient: [msgUndo] unimplemented undo type"
			)
		ELSE	// assume state == Redo
			(case uninfo of
			(CopyInfo newclip oldclip) ->
				appEnv (setClipboard newclip)								>>>
				setUndoInfo {undoinfo & state = Undo}
			(CutInfo newclip oldclip fragment position osel needsave) ->
				appEnv (setClipboard newclip)								>>>
				vDoCursorSafe (
					vRemoveSelection										>>>
					mRemoveSelection										>>>
					vCenterCursor
					)														>>>
				/* should restore to pre-Undo */
				setNeedSave	True											>>>
				setUndoInfo {undoinfo & state = Undo}
			(ClearInfo fragment position osel needsave) ->
				vDoCursorSafe (
					vRemoveSelection											>>>
					mRemoveSelection										>>>
					vCenterCursor
					)														>>>
				/* should restore to pre-Undo */
				setNeedSave	True											>>>
				setUndoInfo {undoinfo & state = Undo}
			(PasteInfo fragment ofragment selection position needsave) ->
				let newPosition = newCursorPos position fragment
				in
			    vDoCursorSafe (
					vChangeSelectionTo selection								>>>
					vRemoveSelection											>>>
					mRemoveSelection										>>>
					vInsertText position fragment								>>>
					vChangeSelectionTo {start=newPosition,end=newPosition}		>>>
					mChangeSelectionTo {start=newPosition,end=newPosition}		>>>
					vCenterCursor
					)														>>>
				/* should restore to pre-Undo */
				setNeedSave	True											>>>
				setUndoInfo {undoinfo & state = Undo}
			(ReplaceAllInfo nfrag osel nsel ulist needsave) ->
				vChangeSelectionTo emptySelection >>>
				getText														>>>= \text ->
				let
					txt` = redoReplaceAll ulist text
				in
				vDoCursorSafe (
					// loop ulist and replace all nfrag's by ofrag
					setText txt` >>>
					vResetViewDomain										>>>
					vChangeSelectionTo nsel									>>>
					mChangeSelectionTo nsel									>>>
					vCenterCursor
					)					>>>
				setNeedSave True	>>>
				setUndoInfo {undoinfo & state = Undo}
			(UndoneInfo ini fin) ->
				setState fin												>>>
	//			getWindowId													>>>= \win ->
	//			appEnv (appPIO (updateWindow win Nothing))					>>>
				setUndoInfo {undoinfo & state = Undo}						>>>
				vResetViewDomain
			_ -> abort "EdClient: [msgUndo] unimplemented redo type"
			)
where
  	newCursorPos pos=:{col, row} SNil
  	  = pos
  	newCursorPos {col, row} (SCons string SNil)
  	  = {col = col + size string, row = row }
  	newCursorPos {col, row} strings
  	  = { col = size (slLast strings)
  	    , row = row + slLength strings - 1
  	    }

//--

msgSelectAll :: EditAction General nothing
msgSelectAll =
	getText																		>>>= \text ->
	let	linenr		= lastLineNr text
		(line,_)	= getLine linenr text
	in
	vDoCursorSafe (
		vChangeSelectionTo {start={row=0,col=0},end={row=linenr,col = size line}}
		)																		>>>
	mChangeSelectionTo {start={row=0,col=0},end={row=linenr,col = size line}}
	
Diederik van Arkel's avatar
Diederik van Arkel committed
241
msgCopy :: EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
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
msgCopy =
	getText									>>>= \text ->
	getSelection							>>>= \selection ->
	accEnv getClipboard						>>>= \oldclip ->
	IF (isEmptySelection selection)
	THEN
	  ( skip )
	ELSE
	  (
		let orderedSelection	= orderSelection selection
			(fragment, _)		= getTextFragment orderedSelection text
		    string				= stringsToString fragment
		    newclip				= [toClipboard string]
		in
		appEnv (setClipboard newclip)			>>>
		setUndoInfo {state = Undo, action = " Copy", uninfo = CopyInfo newclip oldclip}
	  )

msgClear :: EditAction General nothing
msgClear =
	getSelection							>>>= \selection ->
	IF (selection.start==selection.end)
	THEN
		( skip )
	ELSE 
		(
	  	getText																>>>= \text ->
		getNeedSave															>>>= \needsave ->
		let orderedSelection	= orderSelection selection
			(fragment, _)		= getTextFragment orderedSelection text
			position = selection.end
		in
		setUndoInfo
			{ state = Undo
			, action = " Clear"
			, uninfo = ClearInfo fragment position orderedSelection needsave
			}							>>>
		setNeedSave True				>>>
		vDoCursorSafe (
			vRemoveSelection			>>>
			mRemoveSelection			>>>
			vCenterCursor
			)
		)

msgCut :: EditAction General nothing
msgCut =
	getReadOnly																>>>= \readOnly ->
	IF readOnly THEN msgCopy ELSE (
	getSelection															>>>= \selection ->
	IF (selection.start==selection.end)
	THEN
	  ( skip )
	ELSE 
	  (
	  	getText																>>>= \text ->
		accEnv getClipboard													>>>= \oldclip ->
		getNeedSave															>>>= \needsave ->
		let orderedSelection	= orderSelection selection
			(fragment, _)		= getTextFragment orderedSelection text
		    string				= stringsToString fragment
		    newclip				= [toClipboard string]
		    position = selection.end
		in
		appEnv (setClipboard newclip)										>>>
		setUndoInfo
			{ state = Undo
			, action = " Cut"
			, uninfo = CutInfo newclip oldclip fragment position orderedSelection needsave
			}	>>>
		setNeedSave True													>>>
		vDoCursorSafe (
			vRemoveSelection			>>>
			mRemoveSelection										>>>
			vCenterCursor
			)
	  ))

msgReplaceSelection :: String -> EditAction General nothing
msgReplaceSelection string
  =	getText								>>>= \text ->
  	getSelection						>>>= \selection ->
	getNeedSave							>>>= \needsave ->

	let orderedSelection	= orderSelection selection
		(ofragment, _)		= getTextFragment orderedSelection text
		fragment			= stringToStrings string
		position			= orderedSelection.start
		newPosition			= newCursorPos position fragment
	in
	vDoCursorSafe (
		vRemoveSelection					>>>
		mRemoveSelection										>>>
		vInsertText position fragment		>>>
		vChangeSelectionTo {start=newPosition,end=newPosition}	>>>
		mChangeSelectionTo {start=newPosition,end=newPosition}	>>>
		vCenterCursor
		)								>>>
	setNeedSave True					>>>
	setUndoInfo
		{ state = Undo
		, action = " Replace"
		, uninfo = PasteInfo fragment ofragment selection position needsave
		}
where
	newCursorPos :: !Position !TextFragment -> Position
  	newCursorPos pos=:{col, row} SNil
  	  = pos
  	newCursorPos {col, row} (SCons string SNil)
  	  = {col = col + size string, row = row }
  	newCursorPos {col, row} strings
  	  = { col = size (slLast strings)
  	    , row = row + slLength strings - 1
  	    }

msgPaste :: EditAction General nothing
msgPaste =
	getReadOnly								>>>= \readOnly ->
	IF readOnly THEN skip ELSE (
	accEnv getClipboard						>>>= \clipItems ->
	let maybeString = clipItemsToString	clipItems in
	IF (isNothing maybeString)
	THEN
	  ( skip )
	ELSE
	  (
	  	getText								>>>= \text ->
  		getSelection						>>>= \selection ->
		getNeedSave							>>>= \needsave ->
		let orderedSelection	= orderSelection selection
			fragment			= stringToStrings (fromJust maybeString)
			(ofragment, _)		= getTextFragment orderedSelection text
			position			= orderedSelection.start
			newPosition			= newCursorPos position fragment
		in
		vDoCursorSafe (
			vRemoveSelection					>>>
			mRemoveSelection										>>>
			vInsertText orderedSelection.start fragment		>>>
			vChangeSelectionTo {start=newPosition,end=newPosition}	>>>
			mChangeSelectionTo {start=newPosition,end=newPosition}	>>>
			vCenterCursor
			)										>>>
		setNeedSave True							>>>
		setUndoInfo
			{ state = Undo
			, action = " Paste"
			, uninfo = PasteInfo fragment ofragment selection position needsave
			}
	  ))
		
where
	newCursorPos :: !Position !TextFragment -> Position
  	newCursorPos pos=:{col, row} SNil			= pos
  	newCursorPos {col, row} (SCons string SNil)	= {col = col + size string	, row = row }
  	newCursorPos {col, row} strings				= {col = size (slLast strings), row = row + slLength strings - 1}
  	  
	clipItemsToString :: [ClipboardItem] -> Maybe String
	clipItemsToString clipItems	  = findString (map clipToString clipItems)
	
	clipToString :: ClipboardItem -> Maybe String
	clipToString clipItem = fromClipboard clipItem
	
	findString :: [Maybe String] -> Maybe String
	findString []				= Nothing
	findString [Just string:_]	= Just string
	findString [_:rest]			= findString rest

//--

msgSave :: EditAction General (Maybe String)
msgSave = 
	getReadOnly							>>>= \readOnly ->
	IF readOnly
	THEN (
		result (Just "File is read-only")
		)
	ELSE (
		getText											>>>= \text ->
		getPathName										>>>= \pathName ->
		getNewlineConvention							>>>= \newlineConv ->
		accEnv (getPrefs)								>>>= \{newline_handling} ->
		let
			nc = case newline_handling of
					(LeaveAlone _)						-> newlineConv
					(AlwaysUse NewlineConventionNone)	-> HostNativeNewlineConvention
					(AlwaysUse conv)					-> conv
		in
		accEnv (writeText pathName nc text)	>>>= \error ->
		IF (isNothing error)
		THEN
			( setNeedSave False					>>>
			  result Nothing
			)
		ELSE
			( result error
			)
		)

msgSaveTo :: !String -> EditAction General (Maybe String)
msgSaveTo pathName =
	getText											>>>= \text ->
	accEnv (getPrefs)								>>>= \{newline_handling} ->
	let
		nc = case newline_handling of
				(LeaveAlone conv)					-> conv
				(AlwaysUse NewlineConventionNone)	-> HostNativeNewlineConvention
				(AlwaysUse conv)					-> conv
	in
	accEnv (writeText pathName nc text)	>>>= \error ->
	IF (isNothing error)
	THEN
		( setNeedSave False							>>>
		  result Nothing
		)
	ELSE
		( result error
		)
	
msgBalance :: EditAction General nothing
msgBalance =
	getText							>>>= \text ->
	getSelection					>>>= \sel ->
	IF (sel.start == sel.end)
	THEN
		(
		let pos = sel.start
			(ok,nsel) = Text_Balance pos.row pos.col pos.row pos.col text in
		IF ok
		THEN (
			vDoCursorSafe (
				vChangeSelectionTo nsel		>>>
				mChangeSelectionTo nsel
				)
			)
		ELSE
			(appEnv (appPIO beep))
		)
	ELSE
		(
		let
			(ok,nsel) = Text_Balance sel.start.row sel.start.col sel.end.row sel.end.col text
		in
		IF ok
		THEN (
			vDoCursorSafe (
				vChangeSelectionTo nsel		>>>
				mChangeSelectionTo nsel
				)
			)
		ELSE
			(appEnv (appPIO beep))
		)

Diederik van Arkel's avatar
Diederik van Arkel committed
496
msgGetUndoState :: EditAction .l (UndoState,String)
Diederik van Arkel's avatar
Diederik van Arkel committed
497 498 499 500
msgGetUndoState
	=	getUndoInfo				>>>= \undoinfo ->
		result (undoinfo.state,undoinfo.action)

Diederik van Arkel's avatar
Diederik van Arkel committed
501
msgGetPathName :: EditAction .l String
Diederik van Arkel's avatar
Diederik van Arkel committed
502 503 504
msgGetPathName
	= getPathName

Diederik van Arkel's avatar
Diederik van Arkel committed
505
msgSetPathName :: String -> EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
506 507 508
msgSetPathName path =
	setPathName path

Diederik van Arkel's avatar
Diederik van Arkel committed
509
msgGetNeedSave :: EditAction .l Bool
Diederik van Arkel's avatar
Diederik van Arkel committed
510 511 512
msgGetNeedSave
	= getNeedSave

Diederik van Arkel's avatar
Diederik van Arkel committed
513
msgSetNeedSave :: Bool -> EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
514 515 516
msgSetNeedSave ns
	= setNeedSave ns

Diederik van Arkel's avatar
Diederik van Arkel committed
517
msgGetText :: EditAction .l Text
Diederik van Arkel's avatar
Diederik van Arkel committed
518 519 520
msgGetText
	= getText

Diederik van Arkel's avatar
Diederik van Arkel committed
521
msgSetText :: !Text -> EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
msgSetText text =
	setText	text							>>>
  	setSelection emptySelection				>>>
  	vCenterCursor							>>>
  	vResetViewDomain						>>>
	setNeedSave True						>>>
	setUndoInfo {state = None, action = "", uninfo = NoInfo}
	
msgRevertText :: !Text -> EditAction General nothing
msgRevertText text =
	getSelection							>>>= \selection ->
	let realsel = validateSelection selection text in
	setText	text							>>>
	vChangeSelectionTo realsel					>>>
	mChangeSelectionTo realsel					>>>
	vResetViewDomain						>>>
	setNeedSave False						>>>							// DvA: only called by revert!
	setUndoInfo {state = None, action = "", uninfo = NoInfo}			// DvA: only called by revert!
	
Diederik van Arkel's avatar
Diederik van Arkel committed
541
msgGetSelection :: EditAction .l (String,Selection)
Diederik van Arkel's avatar
Diederik van Arkel committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
msgGetSelection 
  = getText							>>>= \text ->
	getSelection					>>>= \selection ->
	IF (selection.start == selection.end)
	THEN
	  (
	    result ("",selection)
	  )
	ELSE
	  (
		let orderedSelection	= orderSelection selection
			(fragment, _)		= getTextFragment orderedSelection text
		    string				= stringsToString fragment in
		result (string,orderedSelection)
	  )

msgChangeSelection :: Selection -> EditAction General nothing
msgChangeSelection selection =
	vDoCursorSafe (
		getText										>>>= \text ->
		let realsel = validateSelection selection text in
		vChangeSelectionTo realsel					>>>
		mChangeSelectionTo realsel					>>>
	  	vCenterCursor
	  	)
 	
Diederik van Arkel's avatar
Diederik van Arkel committed
568
msgScrollToCursor :: EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
msgScrollToCursor
	= vCenterCursor

msgScrollToLine :: LineNr -> EditAction General nothing
msgScrollToLine line =
	vDoCursorSafe (
		getText									>>>= \text ->
		let realline = validateLineNr line text in
		vChangeSelectionTo {start={col=0,row=realline},end={col=0,row=realline}} >>>
		mChangeSelectionTo {start={col=0,row=realline},end={col=0,row=realline}} >>>
		vCenterCursor
		)

//-- Find & Replace support

/*
instance toString Selection where
  toString {start,end}
    = "{ start = " +++ toString start +++ ", end = " +++ toString end +++ "}"

instance toString Position where
  toString {col,row}
    = "{ col = " +++ toString col +++ ", row = " +++ toString row +++ "}"
*/

:: FRInfo =
	{ fr_pos			:: !Selection
	, fr_search			:: !String
	, fr_replace		:: !String
	, fr_ignore_case	:: !Bool
	, fr_match_words	:: !Bool
	, fr_backwards		:: !Bool
	, fr_wraparound		:: !Bool
	, fr_regexp			:: !Bool
	}

msgFind :: !FRInfo -> EditAction General Bool
msgFind fr
	=	getText							>>>= \text ->
		let
			maybesel`
				| fr.fr_regexp
					= regexpLineSearch fr.fr_pos fr.fr_backwards fr.fr_wraparound fr.fr_ignore_case fr.fr_match_words fr.fr_search text
					= simpleLineSearch fr.fr_pos fr.fr_backwards fr.fr_wraparound fr.fr_ignore_case fr.fr_match_words fr.fr_search text
			maybesel = maybesel`
		in
		IF (isNothing maybesel)
		THEN
			(result False)
		ELSE
			(
			let
				realsel = validateSelection (fromJust maybesel) text
			in
		    vDoCursorSafe (
				vChangeSelectionTo realsel					>>>
				mChangeSelectionTo realsel					>>>
			  	vCenterCursor
			  	)	>>>
		  	result True
			)

msgReplace :: !FRInfo -> EditAction General Bool
msgReplace fr =
	getText							>>>= \text ->
	let
		maybesel`
			| fr.fr_regexp
				= regexpLineSearch fr.fr_pos fr.fr_backwards fr.fr_wraparound fr.fr_ignore_case fr.fr_match_words fr.fr_search text
				= simpleLineSearch fr.fr_pos fr.fr_backwards fr.fr_wraparound fr.fr_ignore_case fr.fr_match_words fr.fr_search text
		maybesel = maybesel`
	in
	IF (isNothing maybesel)
	THEN
		(result False)
	ELSE
		(
		getNeedSave				>>>= \needsave ->
		let
			realsel				= validateSelection (fromJust maybesel) text
			orderedSelection	= orderSelection realsel
			(ofragment, _)		= getTextFragment orderedSelection text
			fragment`
				| fr.fr_regexp		
								# (line,_) = getLine orderedSelection.start.row text
								= regexpLineReplace orderedSelection fr.fr_backwards fr.fr_wraparound fr.fr_ignore_case fr.fr_match_words fr.fr_search fr.fr_replace line
								= Just fr.fr_replace
			mfragment			= fragment`
			jfragment`			= stringToStrings (fromJust mfragment)
			jfragment			= jfragment`
			position			= orderedSelection.start
			newPosition			= newCursorPos position jfragment
		in
		IF (isNothing mfragment)
		THEN
			(result False)
		ELSE (
			vDoCursorSafe (
				vChangeSelectionTo realsel							>>>
				vRemoveSelection									>>>
				mRemoveSelection										>>>
				vInsertText position jfragment						>>>
				vChangeSelectionTo {start=position,end=newPosition}	>>>
				mChangeSelectionTo {start=position,end=newPosition}	>>>
				vCenterCursor
				)					>>>
			setNeedSave True		>>>
			setUndoInfo
				{ state		= Undo
				, action	= " Replace"
				, uninfo	= PasteInfo jfragment ofragment realsel position needsave
				}					>>>
			result True
			)
	)
where
	newCursorPos :: !Position !TextFragment -> Position
  	newCursorPos pos=:{col, row} SNil
  	  = pos
  	newCursorPos {col, row} (SCons string SNil)
  	  = {col = col + size string, row = row }
  	newCursorPos {col, row} strings
  	  = { col = size (slLast strings)
  	    , row = row + slLength strings - 1
  	    }

msgReplaceAll :: !FRInfo -> EditAction General Int
msgReplaceAll fr =
// vlaggetjes afhandelen...

// ignore_case
// match_words
// backwards <- zinloze vlag...
// wraparound <- ook zinloos...
// regexp

	getText							>>>= \text ->
	getNeedSave						>>>= \needsave ->
	let
		lines			= textToStrings text
		(ulist,lines`)	= allFun emptySelection [] ({e \\ e <- slToList lines})
		text`			= stringsToText (slFromList [e \\ e <-: lines`])
	in
	vChangeSelectionTo emptySelection	>>>
	mChangeSelectionTo emptySelection	>>>
	setText text`					>>>
	setNeedSave True				>>>
	setUndoInfo
		{ state		= Undo
		, action	= " ReplaceAll"
		, uninfo	= ReplaceAllInfo (stringToStrings fr.fr_replace) fr.fr_pos emptySelection ulist needsave
		}							>>>
	vResetViewDomain				>>>
	result (length ulist)
where
	searchFun :: !Selection !*{String} -> !(!Maybe Selection,!*{String})
	searchFun pos txt
		| fr.fr_regexp
			= regexpLineSearch` pos False False fr.fr_ignore_case fr.fr_match_words fr.fr_search txt
			= simpleLineSearch` pos False False fr.fr_ignore_case fr.fr_match_words fr.fr_search txt

	replaceFun :: !Selection !*{String} -> !(!String,!*{String})
	replaceFun sel txt
		| fr.fr_regexp
			# (line,txt) = uselect txt sel.start.row
			# mfrg = regexpLineReplace sel False False fr.fr_ignore_case fr.fr_match_words fr.fr_search fr.fr_replace line
			# nfrg = fromJust mfrg
			= (nfrg,replaceText` sel nfrg txt)
		= (fr.fr_replace,replaceText` sel fr.fr_replace txt)
		
	allFun :: !Selection ![(Selection,Selection,TextFragment,TextFragment)] !*{String} -> !(![(Selection,Selection,TextFragment,TextFragment)],!*{String})
	allFun pos acc txt
		# (msel,txt)			= searchFun pos txt
		| isNothing msel
			= (reverse acc,txt)
		# sel			= fromJust msel
		# osel			= orderSelection sel
		# (frag,txt)	= getTextFragment` osel txt
		# (nfrg,txt)	= replaceFun osel txt
		# endpos		= newCursorPos osel.start nfrg
		= allFun {start=endpos,end=endpos} [(osel,{osel & end = endpos},frag,stringToStrings nfrg):acc] txt

  	newCursorPos :: !Position !String -> !Position
  	newCursorPos {col, row} string
  	  = {col = col + size string, row = row }
  	
 	getTextFragment` :: !Selection !*{String} -> (!TextFragment,!*{String})
 	getTextFragment` {start={col=col1,row=row1},end={col=col2,row=row2}} text
	  | row1 == row2 
		# (line,text) = uselect text row1
		= ( SCons (line % (col1, col2 - 1)) SNil
		  , text
		  )
	  = abort "EdClient: ReplaceAll: fragment must be single line..."
  	
	replaceText` :: !Selection !String !*{String} -> !*{String}
	replaceText` sel=:{start={col=col1,row=row1},end={col=col2,row=row2}} string text
		| row1 <> row2
		  = abort "EdClient: ReplaceAll: fragment must be single line..."
		# (firstLine,text)	= uselect text row1
		# left = firstLine%(0, col1 - 1)
		# right = firstLine%(col2, size firstLine - 1)
	
		# newLine			= left +++ string +++ right
		= update text row1 newLine
		

undoReplaceAll [] text
	= text
undoReplaceAll [(osel,nsel,ofrg,nfrg):rest] text
	# text = replaceText nsel ofrg text
	= undoReplaceAll rest text

redoReplaceAll [] text
	= text
redoReplaceAll [(osel,nsel,ofrg,nfrg):rest] text
	# text = replaceText osel nfrg text
	= redoReplaceAll rest text

//-- Ed Text Printing

Diederik van Arkel's avatar
Diederik van Arkel committed
790
msgPrint :: !PrintSetup -> EditAction .l PrintSetup
Diederik van Arkel's avatar
Diederik van Arkel committed
791 792 793 794 795 796 797 798 799 800
msgPrint printSetup
  = getText								>>>= \text ->
	getPathName							>>>= \pathName ->
	getFontInfo							>>>= \fontInfo ->
	getLineNumbers						>>>= \linenos ->
	accEnv (myPrintText printSetup pathName text fontInfo linenos)

myPrintText	:: !PrintSetup !String !Text !FontInfo !Bool !*env  -> (PrintSetup,*env)	| PrintEnvironments env
myPrintText printsetup path text info linenos env
	#	fdef	= getFontDef info.thefont
Diederik van Arkel's avatar
Diederik van Arkel committed
801
		((_,printsetup),env)	= printText2 path "page " True LeftJustify//RightJustify 
Diederik van Arkel's avatar
Diederik van Arkel committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
					fdef
					info.tabSize
					textstream
					printsetup
					env
	= (printsetup,env)
where
	textstream
		| linenos
			= Text2TextCharStreamWithLinenrs info.tabSize text
		= Text2TextCharStream text

Text2TextCharStream txt
	# txt		= textToStrings txt
	# txt		= slMap (\str -> str +++. "\n") txt
	= {tcs_txt	= txt, tcs_col = 0, tcs_sav = (SNil,0)}

Text2TextCharStreamWithLinenrs tabWidth txt
	# txt		= textToStrings txt
	# txt		= tabs2spaces tabWidth txt
	# txt		= func 1 txt
	= {tcs_txt	= txt, tcs_col = 0, tcs_sav = (SNil,0)}
where
	width = 5
	
	func i SNil
		= SNil
	func i (SCons h t)
		= SCons ((pads width i) +++. h +++. "\n") (func (inc i) t)
	
	pads w i = blanks%(0,w-intlength) +++. intstring +++. " "
	where
		intstring = toString i
		intlength = size intstring
		blanks = "         "

Diederik van Arkel's avatar
Diederik van Arkel committed
838
msgDetab :: EditAction .l nothing
Diederik van Arkel's avatar
Diederik van Arkel committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
msgDetab =
	getText								>>>= \text ->
	let
		text1				= textToStrings text
		text2				= tabs2spaces 4 text1
		text`				= stringsToText text2
	in
	setText	text`							>>>
	vResetViewDomain						>>>
	setNeedSave True						>>>
	setUndoInfo {state = None, action = "", uninfo = NoInfo}
	

tabs2spaces tabwidth text
	# text = slMap fun` text
	= text
where
	fun` line
		# line = a2l line
		# s = length line
		# line = doit 0 s line [] tabwidth
		# line = l2a line
		= line
	where
		doit i s l m n
			| i >= s = reverse m
			# c = l!!i
			| c == '\t'
				# m = repeatn n ' ' ++ m
				= doit (inc i) s l m tabwidth
			= doit (inc i) s l [c:m] (safemod (dec n) tabwidth)
	
		safemod x m
			# s = x mod m
			| s <= 0 = s + m
			= s
		
		a2l a = [c \\ c <-: a]
		l2a l = {c \\ c <- l}