title: Templating language, determine current scope
body: <p>I'm working with a tempting language that supports changing scope using special delimiters, like this:</p>
  
  <pre><code>%% scope Foo %%
  Stuff
  %% end %%
  </code></pre>
  
  <p>Blocks can be nested indefinitely like so:</p>
  
  <pre><code>%% scope Foo %%
  Stuff in the Foo namespace
    %% scope Bar %%
       Stuff in the bar namespace
    %% end %%
  %% end %%
  </code></pre>
  
  <p>When editing one of these templates in a textarea, I'd like a Javascript function to run to report out the current scope. The current line number and cursor position are known already and can be passed to the function.</p>
  
  <p>In other words, using the example above, if my cursor is anywhere on the second line, the function should log 'Foo'. Likewise, if I'm on the 4th line, it should log 'Bar'. If I'm at the very beginning of the third line, (before the %%), it should log "Foo".</p>