Re: [code] Confused about Events

From: DrTebi <>
Date: Fri, 2 Jan 2015 13:46:12 -0800

Thanks for the quick reply.

I figured it out now, it works great! This is what I have in my
~/.textadept/modules/rest/post_init.lua file:

-- run rst2html after saving an .rst file
events.connect(events.FILE_AFTER_SAVE, function()
  if buffer:get_lexer() ~= 'rest' then return end
    local sh_cmd = '/usr/bin/rst2html --link-stylesheet
    local rst_path = buffer.filename
    local html_path = string.match(buffer.filename,
    local exit_status = os.execute(string.format('%s %q %q', sh_cmd,
rst_path, html_path))

I combine this with the Auto Reload Add-on for Firefox

This is what I do:
- I open the .rst file I want to edit in Textadept and "tile"
Textadept over the right half of my screen.
- I need to engage "save" at least once in Textadept...
- I tile Firefox on the left half of my screen and open the .html file
that rst2html rendered.

Now I can keep editing, and every time I press Ctrl+S, the Auto Reload
plugin detects that the .html file changed and reloads it. Great,
almost as good as a live-preview for writing ReStructuredText

One last thing I would like to implement is the `exit_status` that
os.execute returns--I cannot figure out how to act upon it. It
*should* return 0 when everything went fine, but I always get a
boolean value...
I think the better solution would be to use io.popen, but that will
get a bit more complicated.


On Fri, Jan 2, 2015 at 3:10 AM, Robert Gieseke <> wrote:
> Hi,
> the handling of language module functionality can indeed be confusing,
> so I try some explanations:
> The lexer_loaded event is fired every time a buffer's lexer is set, to
> confirm put
> events.connect(events.LEXER_LOADED, function(lang)
> ui.statusbar_text = lang
> end)
> in your `.textadept/init.lua`. This is used to adjust buffer-specific
> properties like indentation size for a language.
> A `post_init.lua` file is loaded only once by loading the file as a
> module using Lua's require mechanism. So rest/init.lua is loaded first,
> then rest/post_init.lua if it exists. The first file could be located in
> Textadept's module directory (the offical rest module available in the
> modules package) and the second in .textadept/modules/rest.
> For your autocompile command you could start with the following in
> .textadept/modules/rest/post_init.lua
> events.connect(events.FILE_AFTER_SAVE, function()
> if buffer:get_lexer() ~= 'rest' then return end
> ui.statusbar_text = 'after save from post_init'
> end)
> See also
> as an example how the rest syntax checking is done.
> As for the build command the %(filename) etc. syntax is available in
> Textadept's run module. You could define a command like
> = 'rst %(filepath) …'
> If you prefer to compile on every save using the FILE_AFTER_SAVE event,
> you could follow the approach in the linked example above to get the
> filepath.
> Hope this helps!
> Cheers,
> Robert
> Am 02/01/15 um 10:38 schrieb DrTebi:
>> Hello,
>> I am a new Textadept user and have started to write some custom functions.
>> What I am trying to accomplish is the following when I am in 'rst'
>> mode (ReStructuredText):
>> - pressing Ctrl+S saves the file
>> - after saving the file Textadept runs an external command (rst2html)
>> From the manual I have gathered so far that I should create the file
>> ~/.textadept/modules/rest/post_init.lua
>> And that I can then add this functionality somehow like this:
>> events.connect(events.LEXER_LOADED, function(lang)
>> if lang == 'rest' then
>> events.connect(events.FILE_AFTER_SAVE, function(f)
>> os.execute('rst2html "%(filepath)" "%d%e.html"')
>> end)
>> end
>> end)
>> Before I actually tried the rst2html command, I tested this by
>> displaying a message box (ui.dialogs.ok_msgbox{title=f}) and noticed
>> that my function runs in every mode, not just rst.
>> Well, that's where the main problem is... I assume I have to put these
>> custom events somewhere else?
>> I would also appreciate help with constructing the rst2html command,
>> as I am not certain whether the %-variables can be used.
>> Thanks,
>> DrTebi
> --
> You are subscribed to
> To change subscription settings, send an e-mail to
> To unsubscribe, send an e-mail to

"The bitterness of poor quality remains long after the sweetness of
low price is forgotten."
You are subscribed to
To change subscription settings, send an e-mail to
To unsubscribe, send an e-mail to
Received on Fri 02 Jan 2015 - 16:46:12 EST

This archive was generated by hypermail 2.2.0 : Sat 03 Jan 2015 - 06:52:10 EST