views:

901

answers:

3

Using TextMate:

Is it possible to assign a shortcut to preview/refresh the currently edited HTML document in, say, Firefox, without having to first hit Save?

I'm looking for the same functionality as TextMate's built-in Web Preview window, but I'd prefer an external browser instead of TextMate's. (Mainly in order to use a JavaScript console such as Firebug for instance).

Would it be possible to pipe the currently unsaved document through the shell and then preview in Firefox. And if so, is there anyone having a TextMate command for this, willing to share it?

A: 

I don't think this is possible. You can however enable the 'atomic saves' option so every time you alt tab to Firefox your project is saved.

If you ever find a solution to have a proper Firefox live preview, let us know.

Wolfr
+1  A: 

Not trivially. The easiest way would be to write the current file to the temp dir, then launch that file.. but, this would break any relative links (images, scripts, CSS files)

Add a bundle:

Input: Entire Document
Output: Discard
Scope Selector: source.html

And the script:

#!/usr/bin/env python2.5

import os
import sys
import random
import tempfile
import subprocess

fname = os.environ.get("TM_FILEPATH", "Untitled %s.html" % random.randint(100, 1000))
fcontent = sys.stdin.read()

fd, name = tempfile.mkstemp()
print name

open(name, "w+").write(fcontent)

print subprocess.Popen(["open", "-a", "Firefox", name]).communicate()

As I said, that wont work with relative resource links, which is probably a big problem.. Another option is to modify the following line of code, from the exiting "Refresh Browsers" command:

osascript <<'APPLESCRIPT'
    tell app "Firefox" to Get URL "JavaScript:window.location.reload();" inside window 1
APPLESCRIPT

Instead of having the javascript reload the page, it could clear it, and write the current document using a series of document.write() calls. The problem with this is you can't guarantee the current document is the one you want to replace.. Windows 1 could have changed to another site etc, especially with tabbed browsing..

Finally, an option that doesn't have a huge drawback: Use version control, particularly one of the "distributed" ones, where you don't have to send your changes to a remote server - git, mercurial, darcs, bazaar etc (all have TextMate integration also)

If your code is in version control, it doesn't matter if you save before previewing, you can also always go back to your last-commited version if you break something and lose the undo buffer.

dbr
A: 

Here's something that you can use and just replace "Safari" with "Firefox": http://wiki.macromates.com/Main/Howtos#SafariPreview

Billy