[ENH] ParenBlinking ([sm])

Avi Bryant avi at beta4.com
Sun Jul 6 08:28:57 UTC 2003


This has been on SqueakMap forever, but I figured with the new BFAV and so
on it might be worth posting it as an ENH, in the hopes that it'll
actually make it into the image.

This makes two changes to the ParagraphEditor:
 1. upon typing ), }, or ], the matching (, {, or [ will be made bold
until the next keystroke
 2. the meanings of return and control-return are switched (ie,
auto-indent is the default)

I have had this changeset loaded in every image I've used for the last
year and a half, with no ill effects.  I think it makes the
ParagraphEditor significantly more useful for writing code (which is,
after all, what most of us use ParapraphEditor for most of the time).

If people want change 1 without change 2, I'm happy to introduce a
preference that controls that (although I personally think of auto-indent
as something any good text editor does, and can think why you wouldn't
want it).

Separating them into two changesets is more difficult, however, since they
both need to modify #dispatchOnCharacter:with:.

Cheers,
Avi
-------------- next part --------------
'From Squeak3.2gamma of 15 January 2002 [latest update: #4653] on 29 March 2002 at 12:38:18 am'!
ParagraphEditor addInstVarName: 'lastParenLocation'!

!ParagraphEditor methodsFor: 'parenblinking' stamp: 'AB 1/7/2002 03:51'!
blinkParenAt: parenLocation 
	self text
		addAttribute: TextEmphasis bold
		from: parenLocation
		to: parenLocation.
	lastParenLocation _ parenLocation.! !

!ParagraphEditor methodsFor: 'parenblinking' stamp: 'AB 1/7/2002 04:03'!
blinkPrevParen
	| openDelimiter closeDelimiter level string here hereChar |
	string _ paragraph text string.
	here _ startBlock stringIndex.
	openDelimiter _ sensor keyboardPeek.
	closeDelimiter _ '([{' at: (')]}' indexOf: openDelimiter).
	level _ 1.
	[level > 0 and: [here > 2]]
		whileTrue:
			[hereChar _ string at: (here _ here - 1).
			hereChar = closeDelimiter
				ifTrue:
					[level _ level - 1.
					level = 0
						ifTrue: [^ self blinkParenAt: here]]
				ifFalse:
					[hereChar = openDelimiter
						ifTrue: [level _ level + 1]]].! !

!ParagraphEditor methodsFor: 'parenblinking' stamp: 'AB 1/10/2002 00:30'!
clearParens
	lastParenLocation ifNotNil:
		[self text string size >= lastParenLocation ifTrue: [
			self text
				removeAttribute: TextEmphasis bold
				from: lastParenLocation
				to: lastParenLocation]]
! !

!ParagraphEditor methodsFor: 'parenblinking' stamp: 'AB 1/8/2002 03:30'!
dispatchOnCharacter: char with: typeAheadStream
	"Carry out the action associated with this character, if any.
	Type-ahead is passed so some routines can flush or use it."

	| honorCommandKeys |
	self clearParens.
  
	char asciiValue = 13 ifTrue: [
		^ sensor controlKeyPressed
			ifTrue: [self normalCharacter: typeAheadStream]
			ifFalse: [self crWithIndent: typeAheadStream]].

	((honorCommandKeys _ Preferences cmdKeysInText) and: [char = Character enter])
		ifTrue: [^ self dispatchOnEnterWith: typeAheadStream].

	"Special keys overwrite crtl+key combinations - at least on Windows. To resolve this
	conflict, assume that keys other than cursor keys aren't used together with Crtl." 
	((self class specialShiftCmdKeys includes: char asciiValue) and: [char asciiValue < 27])
		ifTrue: [^ sensor controlKeyPressed
			ifTrue: [self perform: (ShiftCmdActions at: char asciiValue + 1) with: typeAheadStream]
			ifFalse: [self perform: (CmdActions at: char asciiValue + 1) with: typeAheadStream]].

	"backspace, and escape keys (ascii 8 and 27) are command keys"
	((honorCommandKeys and: [sensor commandKeyPressed]) or: [self class specialShiftCmdKeys includes: char asciiValue]) ifTrue:
		[^ sensor leftShiftDown
			ifTrue:
				[self perform: (ShiftCmdActions at: char asciiValue + 1) with: typeAheadStream]
			ifFalse:
				[self perform: (CmdActions at: char asciiValue + 1) with: typeAheadStream]].

	"the control key can be used to invoke shift-cmd shortcuts"
	(honorCommandKeys and: [sensor controlKeyPressed])
		ifTrue:
			[^ self perform: (ShiftCmdActions at: char asciiValue + 1) with: typeAheadStream].

	(')]}' includes: char)
		ifTrue: [self blinkPrevParen].

	^ self perform: #normalCharacter: with: typeAheadStream! !



More information about the Squeak-dev mailing list