hardcore require(). _ and $ are no longer global. ultimate victory.

CoffeeScript is still a global because it's used by require() to
open .coffee files. However, you can also load your own:

cs4 = require('my-coffee-script').CoffeeScript

🚬
This commit is contained in:
Chris Wanstrath
2011-08-28 12:27:17 -07:00
parent cbfd4f9d80
commit 868c0f08c2
8 changed files with 286 additions and 265 deletions

View File

@@ -1,125 +1,128 @@
# nice!
define (require, exports, module) ->
{Chrome, File, Process, Dir} = require 'osx'
$ = require 'jquery'
_ = require 'underscore'
ace = require 'ace/ace'
canon = require 'pilot/canon'
{Chrome, File, Process, Dir} = require 'osx'
Chrome.addPane 'main', '<div id="editor"></div>'
ace = require 'ace/ace'
canon = require 'pilot/canon'
editor = ace.edit "editor"
editor.setTheme require "ace/theme/twilight"
JavaScriptMode = require("ace/mode/javascript").Mode
CoffeeMode = require("ace/mode/coffee").Mode
HTMLMode = require("ace/mode/html").Mode
editor.getSession().setMode new JavaScriptMode
editor.getSession().setUseSoftTabs true
editor.getSession().setTabSize 2
Chrome.addPane 'main', '<div id="editor"></div>'
# fuuuuu, ui bug
setTimeout ->
editor.focus()
editor.resize()
, 50
editor = ace.edit "editor"
editor.setTheme require "ace/theme/twilight"
JavaScriptMode = require("ace/mode/javascript").Mode
CoffeeMode = require("ace/mode/coffee").Mode
HTMLMode = require("ace/mode/html").Mode
editor.getSession().setMode new JavaScriptMode
editor.getSession().setUseSoftTabs true
editor.getSession().setTabSize 2
filename = null
editor.getSession().on 'change', ->
Chrome.setDirty true
save = ->
File.write filename, editor.getSession().getValue()
# fuuuuu, ui bug
setTimeout ->
editor.focus()
editor.resize()
, 50
filename = null
editor.getSession().on 'change', ->
Chrome.setDirty true
save = ->
File.write filename, editor.getSession().getValue()
setMode()
Chrome.setDirty false
open = ->
if /png|jpe?g|gif/i.test filename
Chrome.openURL filename
else
Chrome.title _.last filename.split('/')
editor.getSession().setValue File.read filename
setMode()
Chrome.setDirty false
open = ->
if /png|jpe?g|gif/i.test filename
Chrome.openURL filename
else
Chrome.title _.last filename.split('/')
editor.getSession().setValue File.read filename
setMode()
Chrome.setDirty false
setMode = ->
if /\.js$/.test filename
editor.getSession().setMode new JavaScriptMode
else if /\.coffee$/.test filename
editor.getSession().setMode new CoffeeMode
else if /\.html/.test filename
editor.getSession().setMode new HTMLMode
saveAs = ->
if file = Chrome.savePanel()
filename = file
Chrome.title _.last filename.split('/')
save()
exports.bindKey = bindKey = (name, shortcut, callback) ->
canon.addCommand
name: name
exec: callback
bindKey:
win: null
mac: shortcut
sender: 'editor'
setMode = ->
if /\.js$/.test filename
editor.getSession().setMode new JavaScriptMode
else if /\.coffee$/.test filename
editor.getSession().setMode new CoffeeMode
else if /\.html/.test filename
editor.getSession().setMode new HTMLMode
saveAs = ->
if file = Chrome.savePanel()
filename = file
Chrome.title _.last filename.split('/')
save()
exports.bindKey = bindKey = (name, shortcut, callback) ->
canon.addCommand
name: name
exec: callback
bindKey:
win: null
mac: shortcut
sender: 'editor'
bindKey 'open', 'Command-O', (env, args, request) ->
if file = Chrome.openPanel()
filename = file
open()
bindKey 'open', 'Command-O', (env, args, request) ->
if file = Chrome.openPanel()
filename = file
open()
bindKey 'openURL', 'Command-Shift-O', (env, args, request) ->
if url = prompt "Enter URL:"
Chrome.openURL url
bindKey 'openURL', 'Command-Shift-O', (env, args, request) ->
if url = prompt "Enter URL:"
Chrome.openURL url
bindKey 'saveAs', 'Command-Shift-S', (env, args, request) ->
saveAs()
bindKey 'saveAs', 'Command-Shift-S', (env, args, request) ->
saveAs()
bindKey 'save', 'Command-S', (env, args, request) ->
if filename then save() else saveAs()
bindKey 'save', 'Command-S', (env, args, request) ->
if filename then save() else saveAs()
bindKey 'new', 'Command-N', (env, args, request) ->
Chrome.createWindow()
bindKey 'new', 'Command-N', (env, args, request) ->
console.log 'hi mom'
Chrome.createWindow()
bindKey 'copy', 'Command-C', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
bindKey 'copy', 'Command-C', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
bindKey 'cut', 'Command-X', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
editor.session.remove editor.getSelectionRange()
bindKey 'cut', 'Command-X', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
editor.session.remove editor.getSelectionRange()
bindKey 'eval', 'Command-R', (env, args, request) ->
eval env.editor.getSession().getValue()
bindKey 'eval', 'Command-R', (env, args, request) ->
eval env.editor.getSession().getValue()
# textmate
# textmate
bindKey 'togglecomment', 'Command-/', (env) ->
env.editor.toggleCommentLines()
bindKey 'togglecomment', 'Command-/', (env) ->
env.editor.toggleCommentLines()
bindKey 'tmoutdent', 'Command-[', (env) ->
env.editor.blockOutdent()
bindKey 'tmoutdent', 'Command-[', (env) ->
env.editor.blockOutdent()
bindKey 'tmindent', 'Command-]', (env) ->
env.editor.indent()
bindKey 'tmindent', 'Command-]', (env) ->
env.editor.indent()
# emacs > you
# emacs > you
bindKey 'moveforward', 'Alt-F', (env) ->
env.editor.navigateWordRight()
bindKey 'moveforward', 'Alt-F', (env) ->
env.editor.navigateWordRight()
bindKey 'moveback', 'Alt-B', (env) ->
env.editor.navigateWordLeft()
bindKey 'moveback', 'Alt-B', (env) ->
env.editor.navigateWordLeft()
bindKey 'deleteword', 'Alt-D', (env) ->
env.editor.removeWordRight()
bindKey 'deleteword', 'Alt-D', (env) ->
env.editor.removeWordRight()
bindKey 'selectwordright', 'Alt-B', (env) ->
env.editor.navigateWordLeft()
bindKey 'selectwordright', 'Alt-B', (env) ->
env.editor.navigateWordLeft()
bindKey 'home', 'Alt-Shift-,', (env) ->
env.editor.navigateFileStart()
bindKey 'home', 'Alt-Shift-,', (env) ->
env.editor.navigateFileStart()
bindKey 'end', 'Alt-Shift-.', (env) ->
env.editor.navigateFileEnd()
bindKey 'end', 'Alt-Shift-.', (env) ->
env.editor.navigateFileEnd()
bindKey 'fullscreen', 'Command-Shift-Return', (env) ->
Chrome.toggleFullscreen()
bindKey 'fullscreen', 'Command-Shift-Return', (env) ->
Chrome.toggleFullscreen()

View File

@@ -1,118 +1,121 @@
# This is the CoffeeScript API that wraps all of Cocoa.
define (require, exports, module) ->
# Handles the UI chrome
Chrome =
addPane: (position, html) ->
verticalDiv = $('#app-vertical')
horizontalDiv = $('#app-horizontal')
el = document.createElement("div")
el.setAttribute('class', "pane " + position)
el.innerHTML = html
$ = require 'jquery'
_ = require 'underscore'
switch position
when 'top', 'main'
verticalDiv.prepend(el)
when 'left'
horizontalDiv.prepend(el)
when 'bottom'
verticalDiv.append(el)
when 'right'
horizontalDiv.append(el)
else
throw "I DON'T KNOW HOW TO DEAL WITH #{position}"
# Handles the UI chrome
Chrome =
addPane: (position, html) ->
verticalDiv = $('#app-vertical')
horizontalDiv = $('#app-horizontal')
# path - Optional. The String path to the file to base it on.
createWindow: (path) ->
c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow"
c.window
c.window.makeKeyAndOrderFront null
el = document.createElement("div")
el.setAttribute('class', "pane " + position)
el.innerHTML = html
# Set the active window's dirty status.
setDirty: (bool) ->
Chrome.activeWindow().setDocumentEdited bool
# Returns a boolean
dirty: ->
Chrome.activeWindow().isDocumentEdited()
# Returns the active NSWindow object
activeWindow: ->
OSX.NSApplication.sharedApplication.keyWindow
# Returns null or a file path.
openPanel: ->
panel = OSX.NSOpenPanel.openPanel
panel.setCanChooseDirectories(true)
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
# Returns null or a file path.
savePanel: ->
panel = OSX.NSSavePanel.savePanel
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
writeToPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType
openURL: (url) ->
window.location = url
Chrome.title _.last url.replace(/\/$/,'').split '/'
title: (text) ->
WindowController.window.title = text
appRoot: ->
OSX.NSBundle.mainBundle.resourcePath
# Handles the file system
File =
read: (path) ->
OSX.NSString.stringWithContentsOfFile File.expand path
write: (path, contents) ->
str = OSX.NSString.stringWithString contents
str.writeToFile_atomically File.expand(path), true
expand: (path) ->
if /~/.test path
OSX.NSString.stringWithString(path).stringByExpandingTildeInPath
else if path.indexOf('./') is 0
"#{Chrome.appRoot}/#{path}"
switch position
when 'top', 'main'
verticalDiv.prepend(el)
when 'left'
horizontalDiv.prepend(el)
when 'bottom'
verticalDiv.append(el)
when 'right'
horizontalDiv.append(el)
else
path
isFile: (path) ->
isDir = new outArgument
exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir)
exists and not isDir.valueOf()
throw "I DON'T KNOW HOW TO DEAL WITH #{position}"
Dir =
list: (path, recursive) ->
path = File.expand path
fm = OSX.NSFileManager.defaultManager
if recursive
paths = fm.subpathsAtPath path
else
paths = fm.contentsOfDirectoryAtPath_error path, null
_.map paths, (entry) -> "#{path}/#{entry}"
isDir: (path) ->
isDir = new outArgument
exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir)
exists and isDir.valueOf()
# path - Optional. The String path to the file to base it on.
createWindow: (path) ->
c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow"
c.window
c.window.makeKeyAndOrderFront null
Process =
cwd: (path) ->
if dir?
OSX.NSFileManager.defaultManager.changeCurrentDirectoryPath(path)
else
OSX.NSFileManager.defaultManager.currentDirectoryPath()
# Set the active window's dirty status.
setDirty: (bool) ->
Chrome.activeWindow().setDocumentEdited bool
env: ->
OSX.NSProcess.processInfo.environment()
# Returns a boolean
dirty: ->
Chrome.activeWindow().isDocumentEdited()
exports.Chrome = Chrome
exports.File = File
exports.Dir = Dir
# Returns the active NSWindow object
activeWindow: ->
OSX.NSApplication.sharedApplication.keyWindow
# Returns null or a file path.
openPanel: ->
panel = OSX.NSOpenPanel.openPanel
panel.setCanChooseDirectories(true)
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
# Returns null or a file path.
savePanel: ->
panel = OSX.NSSavePanel.savePanel
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
writeToPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType
openURL: (url) ->
window.location = url
Chrome.title _.last url.replace(/\/$/,'').split '/'
title: (text) ->
WindowController.window.title = text
appRoot: ->
OSX.NSBundle.mainBundle.resourcePath
# Handles the file system
File =
read: (path) ->
OSX.NSString.stringWithContentsOfFile(File.expand path).toString()
write: (path, contents) ->
str = OSX.NSString.stringWithString contents
str.writeToFile_atomically File.expand(path), true
expand: (path) ->
if /~/.test path
OSX.NSString.stringWithString(path).stringByExpandingTildeInPath
else if path.indexOf('./') is 0
"#{Chrome.appRoot}/#{path}"
else
path
isFile: (path) ->
isDir = new outArgument
exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir)
exists and not isDir.valueOf()
Dir =
list: (path, recursive) ->
path = File.expand path
fm = OSX.NSFileManager.defaultManager
if recursive
paths = fm.subpathsAtPath path
else
paths = fm.contentsOfDirectoryAtPath_error path, null
_.map paths, (entry) -> "#{path}/#{entry}"
isDir: (path) ->
isDir = new outArgument
exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir)
exists and isDir.valueOf()
Process =
cwd: (path) ->
if dir?
OSX.NSFileManager.defaultManager.changeCurrentDirectoryPath(path)
else
OSX.NSFileManager.defaultManager.currentDirectoryPath()
env: ->
OSX.NSProcess.processInfo.environment()
exports.Chrome = Chrome
exports.File = File
exports.Dir = Dir

View File

@@ -1,3 +1,6 @@
$ = require 'jquery'
_ = require 'underscore'
{Chrome, Dir, File} = require 'osx'
_.map Dir.list(Chrome.appRoot() + "/plugins"), (plugin) ->

View File

@@ -25,12 +25,19 @@ define = (cb) ->
defines.push ->
exports = {}
module = exports: exports
cb.call exports, require, exports, module
exports
cb.call exports, require, exports, module, window
module.exports or exports
exts =
js: (file, code) ->
code or= __read file
if not /define\(/.test code
code = """
define(function(require, exports, module, window) {
#{code};
});
"""
__jsc__.evalJSString_withScriptPath code, file
defines.pop()?.call()
coffee: (file) ->