Skip to content

Commit

Permalink
added a cursor placement option. just add a % to your macro
Browse files Browse the repository at this point in the history
  • Loading branch information
cappelnord committed May 25, 2010
1 parent caa153d commit fc3420e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
15 changes: 10 additions & 5 deletions MacroExpander.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
p.p7 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 12.0px Helvetica}
p.p8 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #9d1c12}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #c50000}
p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0026b4}
p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060; min-height: 12.0px}
span.s1 {color: #000000}
span.s2 {color: #0026b4}
span.s2 {color: #0000bf}
span.s3 {color: #606060}
span.Apple-tab-span {white-space:pre}
</style>
Expand Down Expand Up @@ -93,6 +92,12 @@
<p class="p10"><br></p>
<p class="p9">// this will print the compile string</p>
<p class="p4">"test4"<span class="s1">.xx</span></p>
<p class="p13"><br></p>
<p class="p10"><br></p>
<p class="p9">// if you add a % character somewhere the expanded code won't be selected entirely</p>
<p class="p9">// but instead the cursor will be placed where the % character was</p>
<p class="p4"><span class="s2">MacroExpander</span><span class="s1">.put(</span>"test5"<span class="s1">, </span>"Hello, my Name is %. How are you?"<span class="s1">);</span></p>
<p class="p10"><br></p>
<p class="p9">// you can start typing your name right away!</p>
<p class="p4">"test5".<span class="s1">xx</span></p>
</body>
</html>
20 changes: 16 additions & 4 deletions MacroExpander.sc
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ MacroExpander
expand {|cmd|

var replacement = this.parseAndProcess(cmd);

var cursorPos;

(replacement.isNil.not).if {
this.pr_replace("\"" ++ cmd ++ "\".xx", replacement.asString);
cursorPos = replacement.find("%");

cursorPos.isNil.not.if {
replacement = replacement.replace("%", "");
};

this.pr_replace("\"" ++ cmd ++ "\".xx", replacement.asString, cursorPos);
};
}

// private
pr_replace {|cmd, replacement|
pr_replace {|cmd, replacement, cursorPos|

// this would be more sensible, if it wouldn't try to replace all occurences of the expand cmd.

Expand All @@ -58,7 +65,12 @@ MacroExpander

Document.current.string_(string);
Document.current.syntaxColorize;
Document.current.selectRange(pos, replacement.size);

cursorPos.isNil.if({
Document.current.selectRange(pos, replacement.size);
},{
Document.current.selectRange(pos + cursorPos,0);
});
}

parseAndProcess {|cmd|
Expand Down

0 comments on commit fc3420e

Please sign in to comment.