2 redirect:: implementationClass
3 summary:: An abstract class for editor-specific text document editing classes
5 related:: Classes/CocoaDocument, Classes/EmacsDocument, Classes/ScelDocument
8 The Document class represents a text document within the context of your text editing environment. You can use the class to programmatically create, modify, and query these documents. While it is an abstract class, you still use it to create a code::new:: Document. It simply passes on code::new:: to the appropriate document implementation, e.g. link::Classes/CocoaDocument::.
10 subsection:: Some Important Issues Regarding Document
12 Different text-editing environments can be used with SuperCollider. Therefore Document is an abstract class, meaning it doesn't provide all the functionality itself, but relies on subclasses to complete the functionality. Calls to code::Document.new:: or code::Document.open:: are actually passed down to the relevant class for the editor you're using, such as link::Classes/CocoaDocument:: (for most Mac users) or link::Classes/ScelDocument:: (containing an link::Classes/EmacsDocument::).
14 subsection:: Setting the Environment
16 By default code::envir:: it is set to the current link::Classes/Environment::. However, you can make it use its own link::Classes/Environment:: also. Thus, e.g., if you were to set the link::Classes/Environment:: variable code::~myVar = 12:: in the current link::Classes/Environment::, you can create a new Document window in which that link::Classes/Environment:: variable is not set.
23 private:: prGetLast, prSetSyntaxColorTheme, prnumberOfOpen, prBasicNew, prGetIndexOfListener, prDefaultUsesAutoInOutdent, initClass
27 An instance of link::Classes/String:: or link::Classes/Symbol::.
29 An instance of link::Classes/String::. The contents of the document.
30 argument:: makeListener
31 Makes this document the listener, i.e. the place where SC-lang posts messages.
33 An instance of link::Classes/Environment::. The link::Classes/Environment:: to be used by the interpreter of the document window. By defualt, it is set to the current link::Classes/Environment::.
36 Document.new("this is the title", "this is the text");
40 Open a document from a path.
42 The file system path to the document. An instance of link::Classes/String::.
43 argument:: selectionStart
44 The beginning of the cursor selection of the file content.
45 argument:: selectionLength
46 The length of the cursor selection of the file content.
48 An instance of link::Classes/Environment::. The Environment to be used by the interpreter of the document window. By defualt, it is set to the current link::Classes/Environment::.
51 Document.open("README", 292,253); // notice the selected text in the open document
54 method:: openDocuments
55 Returns an Array of all open documents.
57 d = Document.openDocuments.do{ |doc| doc.name.postln };
60 method:: hasEditedDocuments
61 Returns true if there are edited Documents.
65 Closes all open Documents, whether edited or not.
67 argument:: leavePostWindowOpen
68 An instance of link::Classes/Boolean::.
70 method:: closeAllUnedited
71 Closes all unedited Documents.
72 argument:: leavePostWindowOpen
73 An instance of link::Classes/Boolean::.
76 Gets/sets the current Document.
81 Document.current.name.postln; // Prints "Document.html"
85 Returns the current Document which is the listener, i.e. the Document where interpreter messages are posted.
88 Returns all documents.
90 method:: globalKeyDownAction
91 Get/set A global action to be performed when a key is pressed.
93 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
95 method:: globalKeyUpAction
96 Get/set A global action to be performed when a key is released.
98 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
101 Get/set A an action to be performed up opening or creating a Document.
103 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
106 If a document begins with the link::Classes/String::, code::"/*RUN*/"::, then the code following it int he file will be exectued on opening the file, if autorun is set to true.
108 An instance of link::Classes/Boolean::. Default value is code::true::.
111 If set to code::true::, underlining text will create a wiki link.
113 An instance of link::Classes/Boolean::. Default value is code::true::.
115 method:: implementationClass
116 The editor implementation specific class which will handle Documents.
118 A class for implementing Document, e.g. link::Classes/CocoaDocument::.
121 Get / set the listeners pen color.
123 An instance of link::Classes/Color::.
126 Document.postColor; // returns current post color
127 Document.postColor_(Color.red);
128 Document.postColor_(Color.green);
129 Document.postColor_(Color.blue);
130 Document.postColor_(Color.black);
134 Document.postColor_(Color.rand);
135 "There is no blue without yellow and without orange.".postln;
138 Document.postColor_(Color.black);
147 Sets the theme for syntax colorization.
149 A link::Classes/Symbol::, defining the name of the theme that you've put into code::Document.themes::.
151 The Document class has a preset theme called code::'default'::, which is set as follows (default SC colors):
155 classColor: Color(0, 0, 0.75, 1),
156 textColor: Color(0, 0, 0, 1),
157 stringColor: Color(0.375, 0.375, 0.375, 1),
158 commentColor: Color(0.75, 0, 0, 1),
159 symbolColor: Color(0, 0.45, 0, 1),
160 numberColor: Color(0, 0, 0, 1)
164 If you want to have your own themes for syntax colorization, you need to put your color set into code::Document.themes:: first (preferably in startup.rtf file) and call code::setTheme:: by giving it the name of the theme you've added to "themes" earlier:
166 //putting a custom color theme into Document.themes
170 classColor: Color.new255(53, 74, 187),
171 textColor: Color.new255(0, 0, 0),
172 stringColor: Color.new255(96, 129, 158),
173 commentColor: Color.new255(206, 27, 28),
174 symbolColor: Color.new255(57, 154, 20),
175 numberColor: Color.new255(157, 80, 65)
179 //and then calling setTheme with the name:
180 Document.setTheme('myTheme');
181 //to see the current theme:
184 You can switch to the default theme anytime by calling:
186 Document.setTheme('default');
188 Next time you invoke syntaxColorize, the color theme set by setTheme will be used for syntax colorization. If you want to change the background color for the document window and selected text, in order to make them fit with your syntax colorization theme, see the help for the link::Classes/Document#background:: and link::Classes/Document#selectedBackground:: methods for Document.
190 subsection:: Path Utilities
192 Utilities and settings for dealing with documents such as super collider code files. By default the document directory is SuperCollider's application directory.
195 Get/set the default document directory. The default is dependent on link::Classes/Document#implementationClass::.
197 The file system path to the directory. An instance of link::Classes/String::.
199 In Main-startUp you can set this to a more practical directory:
201 Document.dir = "~/Documents/SuperCollider";
205 Get/set the default wiki directory. The default is dependent on link::Classes/Document#implementationClass::.
207 The file system path to the directory. An instance of link::Classes/String::.
209 method:: standardizePath
211 The file system path to the directory. An instance of link::Classes/String::.
213 If it is a relative path, expand it to an absolute path relative to your document directory. Expand tildes in path (your home directory), resolve symbolic links (but not aliases). Also converts from OS9 macintosh path format. See PathName for more complex needs.
215 Document.standardizePath("~/"); // This will print your home directory
217 Document.standardizePath(":Patches:newfoots:fastRuckAndTuck");
218 // Returns: /Volumes/Macintosh HD/Users/cruxxial/Documents/SC3docs/Patches/newfoots/fastRuckAndTuck
220 Document.standardizePath("~/Documents/SC3docs/Patches/newfoots/fastRuckAndTuck");
221 // Returns: Patches/newfoots/fastRuckAndTuck
223 Document.standardizePath("Patches/newfoots/fastRuckAndTuck")
224 // Returns: Patches/newfoots/fastRuckAndTuck
228 Returns a path relative to Document.dir, if the path is inside Document.dir.
230 The file system path to the directory. An instance of link::Classes/String::.
237 private:: prGetBackgroundColor, prGetBounds, prIsEditable, propen, prGetTitle, prinitByString, prGetLastIndex, prSetBackgroundColor, prSetFileName, prUsesAutoInOutdent, prclose, prGetSelectedBackgroundColor, prGetFileName, prSetSelectedBackgroundColor, prSelectLine, prinitByIndex, prAdd, prSetBounds, prinsertText, prSetTitle, initFromPath, initByString
239 subsection:: General Document Properties
242 Get/set the bounds of the document.
244 An instance of link::Classes/Rect::.
247 Get / set the the Document's path.
249 An instance of link::Classes/String::. A files system path.
252 Document.current.path.postln;
256 Returns the directory of a Document.
259 Document.current.dir.postln;
265 An instance of Document.
268 Document.current == Document.listener; // presumaably returns false
272 Get / set the the document is editable.
274 An instance of link::Classes/Boolean::.
277 Get / set the title (same as link::Classes/Document#title::).
279 An instance of link::Classes/String::.
282 Document.current.name.postln;
286 Get / set the title (same as link::Classes/Document#name::).
288 An instance of link::Classes/String::.
291 Get / set the the Document's background color.
293 An instance of link::Classes/Color::.
297 a = Document("background", "'hardly see anything");
298 a.background_(Color.blue(alpha:0.8)); // notice that alpha controls the window transparency
303 Get/set whether a document is always on top.
305 An instance of link::Classes/Boolean::.
307 method:: promptToSave
308 Get/set whether a document is prompts to save if it has been changed. Use this with caution.
310 An instance of link::Classes/Boolean::.
313 Returns code::true:: if the document has been closed.
316 Returns code::true:: if the document has been edited.
318 Document.current.isEdited.postln;
322 Returns code::true:: if the document is in front.
325 Returns if the document is the listener.
327 method:: didBecomeKey
328 Saves the current link::Classes/Environment::, makes the document current, and performs its link::Classes/Document#toFrontAction::.
330 method:: didResignKey
331 Performs the Document's link::Classes/Document#endFrontAction:: and restores the current link::Classes/Environment::.
335 subsection:: Controlling Document
343 doc = Document("background", "closing in 2 seconds");
344 doc.stringColor_(Color.blue);
346 doc.background_(Color.blue(alpha:0.8));
354 Bring a document to the front.
356 Document.listener.front;
359 method:: unfocusedFront
360 Bring a document to the front without focusing it.
362 Document.listener.unfocusedFront;
366 Get/set the action to be performed on closing the document.
368 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
370 method:: endFrontAction
371 Get/set the action to be performed when the document becomes no longer the front document.
373 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
375 method:: toFrontAction
376 Get / set the action to be performed when the document become the front document.
378 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
380 method:: mouseDownAction
381 Get/set the action to be performed on link::Classes/Document#mouseDown::.
383 The Mac OSX built-in editor does not supports this feature. A mouseDownAction that you supply will be ignored.
386 An instance of link::Classes/Function:: or link::Classes/FunctionList::. The arguments passed to the function are: code::x::, code::y::, code::modifiers::, code::buttonNumber::, code::clickCount::, code::clickPos::.
389 method:: mouseUpAction
390 Get/set the action to be performed on link::Classes/Document#mouseUp::.
392 An instance of link::Classes/Function:: or link::Classes/FunctionList::. The arguments passed to the function are: code::x::, code::y::, code::modifiers::, code::buttonNumber::, code::clickCount::, code::clickPos::.
397 //add a mouse action to this document:
398 //example: easy button:
399 //when you click in front of a 17 a SinOsc will start up;
401 Document.current.mouseUpAction_({arg doc;
403 char = doc.rangeText(doc.selectionStart, 2);
405 {EnvGen.kr(Env.perc, doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
408 {EnvGen.kr(Env.perc, doneAction:2) * PinkNoise.ar(0.2)}.play;
414 Test here and click in front of the numbers: 17 and 23.
416 Document.current.mouseUpAction = nil; // clear mouseUpActiont
420 method:: keyDownAction
421 Get/set the action to be performed on link::Classes/Document#keyDown::.
423 An instance of link::Classes/Function:: or link::Classes/FunctionList::. The arguments passed to the function are: code::char::, code::modifiers::, code::unicode::, code::keycode::.
426 Document.current.keyDownAction = { arg ...args; args.postln };
427 // now type some text
428 Document.current.keyDownAction = nil;
432 Get/set the action to be performed on link::Classes/Document#keyUp::.
434 An instance of link::Classes/Function:: or link::Classes/FunctionList::. The arguments passed to the function are: code::char::, code::modifiers::, code::unicode::, code::keycode::.
436 Document.current.keyUpAction = { arg ...args; args.postln };
437 // now type some text
438 Document.current.keyUpAction = nil;
441 method:: makeWikiPage
444 An instance of link::Classes/String::. The name of the document.
446 An instance of link::Classes/String::. The file extension.
448 An instance of link::Classes/String::. The directory in which to save the page.
451 Document.current.makeWikiPage("test1");
454 method:: openWikiPage
455 Opens/creates a wiki page out of the currently selected text.
459 subsection:: Editing Content
462 Select a line of the document by number.
464 An link::Classes/Integer::.
467 Document.current.selectLine(390);
471 Select a text range in the string of the document.
475 The length of the selection.
479 Document.current.selectRange(Document.current.selectedRangeLocation + 3, 150);
483 method:: balanceParens
484 Starting from the current selection, increase the selection until matching parentheses are selected.
486 Do this as many times to find ever wider brackets. Set to code::inf:: for outmost.
490 Document.current.balanceParens(1);
491 Document.current.balanceParens(3);
492 Document.current.balanceParens(inf);
496 method:: selectionStart
497 Returns the start of a current selection.
499 Document.current.selectionStart.postln;
502 method:: selectionSize
503 Returns the size of a current selection.
507 doc = Document.current;
508 doc.selectRange(doc.selectionStart - 40, 10);
509 doc.selectionSize.postln;
514 method:: selectedString
515 Gets/sets the selected string.
517 An instance of link::Classes/String::.
522 doc = Document.current;
523 doc.selectRange(doc.selectionStart - 40, 10);
524 doc.selectedString.postln;
529 Returns the current line as a link::Classes/String::.
533 doc = Document.current;
534 doc.selectRange(doc.selectionStart - 40, 10);
535 doc.currentLine.postln;
539 method:: getSelectedLines
540 Returns all full lines from before code::rangestart:: to after code::rangestart + rangesize:: as a link::Classes/String::.
545 doc = Document.current;
546 doc.selectRange(doc.selectionStart - 40, 10);
547 doc.getSelectedLines(doc.selectionStart - 40, 130).postln;
553 Gets or sets the string within a certain range.
555 A link::Classes/String::.
556 argument:: rangestart
557 An link::Classes/Integer::.
559 An link::Classes/Integer::.
562 // Select the following code in parentheses and execute it
564 Document.current.string_(": test test test test test ",
565 Document.current.selectedRangeLocation + 12,
568 // Watch me change content
572 Gets or sets the font within a certain range.
574 An instance of link::Classes/Font::.
575 argument:: rangestart
576 An link::Classes/Integer::. Default value is -1. If code::rangestart = -1::, the whole document is selected.
578 An link::Classes/Integer::. Default value is 0.
581 // Select the following code in parentheses and execute it
583 Document.current.font_(Font("Impact", 14),
584 Document.current.selectedRangeLocation + 12,
587 // Watch me change font
591 Gets or sets the string color of a specific range of already printed text. Default is the whole document. To set the listener text color for posting, see: link::Classes/Document#postColor::.
593 An instance of link::Classes/Color::.
594 argument:: rangeStart
595 An link::Classes/Integer::. Default is -1.
597 An link::Classes/Integer::. Default value is 0
600 // Select the following code in parentheses and execute it
602 Document.current.stringColor_(Color.rand(0.2, 0.8),
603 Document.current.selectedRangeLocation + 13,
606 // Watch me change color
609 method:: selectedBackground
610 Gets or sets the document's background color for selected text. Applies to the whole document instance.
612 An instance of link::Classes/Color::.
615 Document.current.selectedBackground; // returns default color
617 w = Document.new("Test", "Here is a selected text...");
618 w.selectedBackground_(Color.new255(120, 180, 110));
619 w.selectRange(10, 13);
623 method:: syntaxColorize
624 Syntax colorize a document.
626 method:: underlineSelection
627 Underlines the current selection of a Document.
630 subsection:: Auto-Completion
633 OSX version only, currently. See link::Reference/DocumentAutoCompletion::.
639 *autoCompleteKeyAction
640 *openFileAutoComplete (path)
645 subsection:: Subclassing and Internal Methods
647 The following methods are usually not used directly or are called by a primitive. Programmers can still call or override these as needed.
651 mouseUp (x, y, modifiers, buttonNumber, clickCount, clickPos)
652 keyDown (character, modifiers, unicode, keycode)
653 keyUp (character, modifiers, unicode, keycode)
654 getIdentifierCoordFromEnd (endPos)
657 Private. Used only internally:
661 *prGetIndexOfListener
665 setFont (font, rangeStart, rangeSize)
666 setTextColor (color, rangeStart, rangeSize)
667 propen (path, selectionStart, selectionLength)
668 rangeText (rangestart, rangesize)
669 insertTextRange (string, rangestart, rangesize)
670 prinitByString (title, str, makeListener)
671 prSetBackgroundColor (color)
672 prGetBackgroundColor (color)
674 prIsEditable_ (editable)
678 prSetFileName (apath)
679 prGetBounds (argBounds)
680 prSetBounds (argBounds)
682 prinsertText (dataPtr, txt)
689 selectUnderlinedText (clickPos)
690 linkAtClickPos (clickPos)
691 selectedRangeLocation
693 restoreCurrentEnvironment
694 saveCurrentEnvironment
697 initFromPath (path, selectionStart, selectionLength)
698 initByString (argTitle, str, makeListener)
710 Document.allDocuments.at(0).unfocusedFront
716 doc = Document("", "||");
717 doc.background_(Color.blue(alpha: 1.0.rand));
721 doc.setFont(rangeSize: [7, 8, 9, 24].choose);
729 doc.stringColor_([Color.red(alpha: 1.0.rand), Color.green(alpha: 1.0.rand)].choose);
736 doc.selectedString_(["\"\n#", "||", "-", "--"].choose);
747 if(co > 0.99, { co = 0.1 });
748 doc.background_(Color.blue(alpha: co));
755 A simple implementation of TBT (time based text) http://tbt.dyne.org/?info=download
757 // record: type some text
759 var time = Main.elapsedTime;
763 a = a.add([char, Main.elapsedTime - time]);
768 Document.new("type some text")
769 .bounds_(Rect(100,SCWindow.screenBounds.height - 250, 400, 200))
770 .keyDownAction = { |doc, key| r.value(key) ; time = Main.elapsedTime};
773 // play back text in time
775 d = Document.new("type some text")
776 .bounds_(Rect(550,SCWindow.screenBounds.height-250,400,200));
779 d.string = d.string ++ pair[0];
786 Changing the default look of documents can be done with the help of the link::#*initAction:: method. Run the following example once. Afterwards all newly created documents will have a dark grey background. To make this change happen every time you start supercollider, put the code inside your startup.scd file (and optionally wrap it in a code::{}.defer(0.1):: ).
789 Document.listener.background = Color.red; //a special color for post document
790 Document.listener.bounds = Rect(1, 461, 620, 567); //move and resize post document
791 Document.initAction = {|doc| //function to run for every new document
792 doc.background = Color.grey(0.1, 0.9);
793 doc.bounds = Rect(0, 119, 1280, 659);
794 doc.selectedBackground = Color(0.4, 0.05, 0.18);
795 doc.stringColor = Color.grey(0.9);