Re: [code] How would this work?

From: Mitchell <m.att.foicica.com>
Date: Thu, 21 Feb 2013 23:26:20 -0500 (EST)

Hi,

On Thu, 21 Feb 2013, Robert wrote:

> On Thu, Feb 21, 2013 at 8:58 AM, Michael Richter <ttmrichter.att.gmail.com> wrote:
>> I'd like to support vim modelines (or something similar) in textadept. A
>> modeline, for those not in the know, looks something like this, stuck in a
>> comment somewhere:
>>
>> vim: ft=prolog sw=3 ts=6
>>
>> This modeline, for example, tells vim that the file has a file type of
>> "prolog", a shift width of 3 and a tab stop of 6.
>>
>> If I wanted to make a commensurate feature for textadept, how would I go
>> about doing this? Let's say I wanted to look for and parse something like
>> this:
>>
>> textadept: set_lexer=prolog tab_width=4 indent=4 use_tabs=false
>>
>> It could be anywhere in the file (I'd rather not force it to be the first
>> line because then shebang settings could interfere) so ... where would I
>> hook things for this kind of functionality? (I can write the actual code
>> that parses and sets the exposed values easily.)
>>
>
> You'd hook that into the "file_opened" event:
>
> events.connect(events.FILE_OPENED, function()
> -- ...
> end)
>
> It doesn't show up in the "events" documentation, but there are a few
> events triggered by fiel opening/saving, that are quite useful.
> See core/file_io.lua (http://foicica.com/textadept/api/io.html) for
> some examples.

You could also piggy-back off of _M.textadept.editing.comment_string[1] if
you populate it[2].

events.connect(events.FILE_OPENED, function(utf8_filename)
   local comment = _M.textadept.editing.comment_string[buffer:get_lexer()]
   if comment then
     comment = comment:gsub('([-*])', '%%%1') -- sanitize for Lua patterns
     for i = 0, buffer.line_count - 1 do
       if buffer:get_line(i):find('^'..comment..'%s*textadept:') then
          -- parse ta modeline
          return
       end
     end
   end
end)

Cheers,
Mitchell

[1]: http://foicica.com/textadept/api/_M.textadept.editing.html#comment_string
[2]: http://foicica.com/wiki/comment-supplemental

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Thu 21 Feb 2013 - 23:26:20 EST

This archive was generated by hypermail 2.2.0 : Fri 22 Feb 2013 - 06:37:10 EST