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");
41 Open a document from a path.
43 The file system path to the document. An instance of link::Classes/String::.
44 argument:: selectionStart
45 The beginning of the cursor selection of the file content.
46 argument:: selectionLength
47 The length of the cursor selection of the file content.
49 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::.
52 Document.open("README", 292,253); // notice the selected text in the open document
55 method:: openDocuments
56 Returns an Array of all open documents.
58 d = Document.openDocuments.do{ |doc| doc.name.postln };
61 method:: hasEditedDocuments
62 Returns true if there are edited Documents.
66 Closes all open Documents, whether edited or not.
68 argument:: leavePostWindowOpen
69 An instance of link::Classes/Boolean::.
71 method:: closeAllUnedited
72 Closes all unedited Documents.
73 argument:: leavePostWindowOpen
74 An instance of link::Classes/Boolean::.
77 Gets/sets the current Document.
82 Document.current.name.postln; // Prints "Document.html"
87 Returns the current Document which is the listener, i.e. the Document where interpreter messages are posted.
90 Returns all documents.
92 method:: globalKeyDownAction
93 Get/set A global action to be performed when a key is pressed.
95 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
97 method:: globalKeyUpAction
98 Get/set A global action to be performed when a key is released.
100 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
103 Get/set A an action to be performed up opening or creating a Document.
105 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
108 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.
110 An instance of link::Classes/Boolean::. Default value is code::true::.
113 If set to code::true::, underlining text will create a wiki link.
115 An instance of link::Classes/Boolean::. Default value is code::true::.
117 method:: implementationClass
118 The editor implementation specific class which will handle Documents.
120 A class for implementing Document, e.g. link::Classes/CocoaDocument::.
123 Get / set the listeners pen color.
125 An instance of link::Classes/Color::.
128 Document.postColor; // returns current post color
129 Document.postColor_(Color.red);
130 Document.postColor_(Color.green);
131 Document.postColor_(Color.blue);
132 Document.postColor_(Color.black);
136 Document.postColor_(Color.rand);
137 "There is no blue without yellow and without orange.".postln;
140 Document.postColor_(Color.black);
150 Sets the theme for syntax colorization.
152 A link::Classes/Symbol::, defining the name of the theme that you've put into code::Document.themes::.
154 The Document class has a preset theme called code::'default'::, which is set as follows (default SC colors):
158 classColor: Color(0, 0, 0.75, 1),
159 textColor: Color(0, 0, 0, 1),
160 stringColor: Color(0.375, 0.375, 0.375, 1),
161 commentColor: Color(0.75, 0, 0, 1),
162 symbolColor: Color(0, 0.45, 0, 1),
163 numberColor: Color(0, 0, 0, 1)
167 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:
169 //putting a custom color theme into Document.themes
173 classColor: Color.new255(53, 74, 187),
174 textColor: Color.new255(0, 0, 0),
175 stringColor: Color.new255(96, 129, 158),
176 commentColor: Color.new255(206, 27, 28),
177 symbolColor: Color.new255(57, 154, 20),
178 numberColor: Color.new255(157, 80, 65)
182 //and then calling setTheme with the name:
183 Document.setTheme('myTheme');
184 //to see the current theme:
187 You can switch to the default theme anytime by calling:
189 Document.setTheme('default');
191 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.
195 subsection:: Path Utilities
197 Utilities and settings for dealing with documents such as super collider code files. By default the document directory is SuperCollider's application directory.
200 Get/set the default document directory. The default is dependent on link::Classes/Document#implementationClass::.
202 The file system path to the directory. An instance of link::Classes/String::.
204 In Main-startUp you can set this to a more practical directory:
206 Document.dir = "~/Documents/SuperCollider";
211 Get/set the default wiki directory. The default is dependent on link::Classes/Document#implementationClass::.
213 The file system path to the directory. An instance of link::Classes/String::.
215 method:: standardizePath
217 The file system path to the directory. An instance of link::Classes/String::.
219 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.
221 Document.standardizePath("~/"); // This will print your home directory
223 Document.standardizePath(":Patches:newfoots:fastRuckAndTuck");
224 // Returns: /Volumes/Macintosh HD/Users/cruxxial/Documents/SC3docs/Patches/newfoots/fastRuckAndTuck
226 Document.standardizePath("~/Documents/SC3docs/Patches/newfoots/fastRuckAndTuck");
227 // Returns: Patches/newfoots/fastRuckAndTuck
229 Document.standardizePath("Patches/newfoots/fastRuckAndTuck")
230 // Returns: Patches/newfoots/fastRuckAndTuck
234 Returns a path relative to Document.dir, if the path is inside Document.dir.
236 The file system path to the directory. An instance of link::Classes/String::.
243 private:: prGetBackgroundColor, prGetBounds, prIsEditable, propen, prGetTitle, prinitByString, prGetLastIndex, prSetBackgroundColor, prSetFileName, prUsesAutoInOutdent, prclose, prGetSelectedBackgroundColor, prGetFileName, prSetSelectedBackgroundColor, prSelectLine, prinitByIndex, prAdd, prSetBounds, prinsertText, prSetTitle, initFromPath, initByString
245 subsection:: General Document Properties
248 Get/set the bounds of the document.
250 An instance of link::Classes/Rect::.
253 Get / set the the Document's path.
255 An instance of link::Classes/String::. A files system path.
258 Document.current.path.postln;
262 Returns the directory of a Document.
265 Document.current.dir.postln;
271 An instance of Document.
274 Document.current == Document.listener; // presumaably returns false
278 Get / set the the document is editable.
280 An instance of link::Classes/Boolean::.
283 Get / set the title (same as link::Classes/Document#title::).
285 An instance of link::Classes/String::.
288 Document.current.name.postln;
293 Get / set the title (same as link::Classes/Document#name::).
295 An instance of link::Classes/String::.
298 Get / set the the Document's background color.
300 An instance of link::Classes/Color::.
304 a = Document("background", "'hardly see anything");
305 a.background_(Color.blue(alpha:0.8)); // notice that alpha controls the window transparency
311 Get/set whether a document is always on top.
313 An instance of link::Classes/Boolean::.
315 method:: promptToSave
316 Get/set whether a document is prompts to save if it has been changed. Use this with caution.
318 An instance of link::Classes/Boolean::.
321 Returns code::true:: if the document has been closed.
324 Returns code::true:: if the document has been edited.
326 Document.current.isEdited.postln;
330 Returns code::true:: if the document is in front.
333 Returns if the document is the listener.
335 method:: didBecomeKey
336 Saves the current link::Classes/Environment::, makes the document current, and performs its link::Classes/Document#toFrontAction::.
338 method:: didResignKey
339 Performs the Document's link::Classes/Document#endFrontAction:: and restores the current link::Classes/Environment::.
343 subsection:: Controlling Document
351 doc = Document("background", "closing in 2 seconds");
352 doc.stringColor_(Color.blue);
354 doc.background_(Color.blue(alpha:0.8));
362 Bring a document to the front.
364 Document.listener.front;
367 method:: unfocusedFront
368 Bring a document to the front without focusing it.
370 Document.listener.unfocusedFront;
374 Get/set the action to be performed on closing the document.
376 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
378 method:: endFrontAction
379 Get/set the action to be performed when the document becomes no longer the front document.
381 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
383 method:: toFrontAction
384 Get / set the action to be performed when the document become the front document.
386 An instance of link::Classes/Function:: or link::Classes/FunctionList::.
388 method:: mouseDownAction
389 Get/set the action to be performed on link::Classes/Document#mouseDown::.
391 The Mac OSX built-in editor does not supports this feature. A mouseDownAction that you supply will be ignored.
394 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 method:: mouseUpAction
398 Get/set the action to be performed on link::Classes/Document#mouseUp::.
400 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::.
405 //add a mouse action to this document:
406 //example: easy button:
407 //when you click in front of a 17 a SinOsc will start up;
409 Document.current.mouseUpAction_({arg doc;
411 char = doc.rangeText(doc.selectionStart, 2);
413 {EnvGen.kr(Env.perc, doneAction:2) * SinOsc.ar([600,720,300].choose, 0, 0.5)}.play;
416 {EnvGen.kr(Env.perc, doneAction:2) * PinkNoise.ar(0.2)}.play;
422 Test here and click in front of the numbers: 17 and 23.
424 Document.current.mouseUpAction = nil; // clear mouseUpActiont
429 method:: keyDownAction
430 Get/set the action to be performed on link::Classes/Document#keyDown::.
432 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::.
435 Document.current.keyDownAction = { arg ...args; args.postln };
436 // now type some text
437 Document.current.keyDownAction = nil;
442 Get/set the action to be performed on link::Classes/Document#keyUp::.
444 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::.
446 Document.current.keyUpAction = { arg ...args; args.postln };
447 // now type some text
448 Document.current.keyUpAction = nil;
452 method:: makeWikiPage
455 An instance of link::Classes/String::. The name of the document.
457 An instance of link::Classes/String::. The file extension.
459 An instance of link::Classes/String::. The directory in which to save the page.
462 Document.current.makeWikiPage("test1");
466 method:: openWikiPage
467 Opens/creates a wiki page out of the currently selected text.
471 subsection:: Editing Content
474 Select a line of the document by number.
476 An link::Classes/Integer::.
479 Document.current.selectLine(390);
484 Select a text range in the string of the document.
488 The length of the selection.
492 Document.current.selectRange(Document.current.selectedRangeLocation + 3, 150);
497 method:: balanceParens
498 Starting from the current selection, increase the selection until matching parentheses are selected.
500 Do this as many times to find ever wider brackets. Set to code::inf:: for outmost.
504 Document.current.balanceParens(1);
505 Document.current.balanceParens(3);
506 Document.current.balanceParens(inf);
510 method:: selectionStart
511 Returns the start of a current selection.
513 Document.current.selectionStart.postln;
516 method:: selectionSize
517 Returns the size of a current selection.
521 doc = Document.current;
522 doc.selectRange(doc.selectionStart - 40, 10);
523 doc.selectionSize.postln;
528 method:: selectedString
529 Gets/sets the selected string.
531 An instance of link::Classes/String::.
536 doc = Document.current;
537 doc.selectRange(doc.selectionStart - 40, 10);
538 doc.selectedString.postln;
544 Returns the current line as a link::Classes/String::.
548 doc = Document.current;
549 doc.selectRange(doc.selectionStart - 40, 10);
550 doc.currentLine.postln;
554 method:: getSelectedLines
555 Returns all full lines from before code::rangestart:: to after code::rangestart + rangesize:: as a link::Classes/String::.
556 argument:: rangestart
562 doc = Document.current;
563 doc.selectRange(doc.selectionStart - 40, 10);
564 doc.getSelectedLines(doc.selectionStart - 40, 130).postln;
571 Gets or sets the string within a certain range.
572 argument:: rangestart
575 Sets the string within a certain range.
577 argument:: rangestart
578 An link::Classes/Integer::. Default value is code::nil::.
580 An link::Classes/Integer::. Default value is 1.
583 // Select the following code in parentheses and execute it
585 Document.current.string_(": test test test test test ",
586 Document.current.selectedRangeLocation + 12,
589 // Watch me change content
594 Gets or sets the font within a certain range.
596 An instance of link::Classes/Font::.
597 argument:: rangestart
598 An link::Classes/Integer::. Default value is -1. If code::rangestart = -1::, the whole document is selected.
600 An link::Classes/nteger::. Default value is 0.
603 // Select the following code in parentheses and execute it
605 Document.current.font_(Font("Impact", 14),
606 Document.current.selectedRangeLocation + 12,
609 // Watch me change font
614 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::.
616 An instance of link::Classes/Color::.
617 argument:: rangeStart
618 An link::Classes/Integer::. Default is -1.
620 An link::Classes/Integer::. Default value is 0
623 // Select the following code in parentheses and execute it
625 Document.current.stringColor_(Color.rand(0.2, 0.8),
626 Document.current.selectedRangeLocation + 13,
629 // Watch me change color
633 method:: selectedBackground
634 Gets or sets the document's background color for selected text. Applies to the whole document instance.
636 An instance of link::Classes/Color::.
639 Document.current.selectedBackground; // returns default color
641 w = Document.new("Test", "Here is a selected text...");
642 w.selectedBackground_(Color.new255(120, 180, 110));
643 w.selectRange(10, 13);
648 method:: syntaxColorize
649 Syntax colorize a document.
651 method:: underlineSelection
652 Underlines the current selection of a Document.
668 subsection:: Auto-Completion
671 OSX version only, currently. See link::Reference/DocumentAutoCompletion::.
677 *autoCompleteKeyAction
678 *openFileAutoComplete (path)
685 subsection:: Subclassing and Internal Methods
687 The following methods are usually not used directly or are called by a primitive. Programmers can still call or override these as needed.
691 mouseUp (x, y, modifiers, buttonNumber, clickCount, clickPos)
692 keyDown (character, modifiers, unicode, keycode)
693 keyUp (character, modifiers, unicode, keycode)
694 getIdentifierCoordFromEnd (endPos)
697 Private. Used only internally:
701 *prGetIndexOfListener
705 setFont (font, rangeStart, rangeSize)
706 setTextColor (color, rangeStart, rangeSize)
707 propen (path, selectionStart, selectionLength)
708 rangeText (rangestart, rangesize)
709 insertTextRange (string, rangestart, rangesize)
710 prinitByString (title, str, makeListener)
711 prSetBackgroundColor (color)
712 prGetBackgroundColor (color)
714 prIsEditable_ (editable)
718 prSetFileName (apath)
719 prGetBounds (argBounds)
720 prSetBounds (argBounds)
722 prinsertText (dataPtr, txt)
729 selectUnderlinedText (clickPos)
730 linkAtClickPos (clickPos)
731 selectedRangeLocation
733 restoreCurrentEnvironment
734 saveCurrentEnvironment
737 initFromPath (path, selectionStart, selectionLength)
738 initByString (argTitle, str, makeListener)
750 Document.allDocuments.at(0).unfocusedFront
756 doc = Document("", "||");
757 doc.background_(Color.blue(alpha: 1.0.rand));
761 doc.setFont(rangeSize: [7, 8, 9, 24].choose);
769 doc.stringColor_([Color.red(alpha: 1.0.rand), Color.green(alpha: 1.0.rand)].choose);
776 doc.selectedString_(["\"\n#", "||", "-", "--"].choose);
787 if(co > 0.99, { co = 0.1 });
788 doc.background_(Color.blue(alpha: co));
795 A simple implementation of TBT (time based text) http://tbt.dyne.org/?info=download
797 // record: type some text
799 var time = Main.elapsedTime;
803 a = a.add([char, Main.elapsedTime - time]);
808 Document.new("type some text")
809 .bounds_(Rect(100,SCWindow.screenBounds.height - 250, 400, 200))
810 .keyDownAction = { |doc, key| r.value(key) ; time = Main.elapsedTime};
813 // play back text in time
815 d = Document.new("type some text")
816 .bounds_(Rect(550,SCWindow.screenBounds.height-250,400,200));
819 d.string = d.string ++ pair[0];
826 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):: ).
829 Document.listener.background = Color.red; //a special color for post document
830 Document.listener.bounds = Rect(1, 461, 620, 567); //move and resize post document
831 Document.initAction = {|doc| //function to run for every new document
832 doc.background = Color.grey(0.1, 0.9);
833 doc.bounds = Rect(0, 119, 1280, 659);
834 doc.selectedBackground = Color(0.4, 0.05, 0.18);
835 doc.stringColor = Color.grey(0.9);