Skip to content

Instantly share code, notes, and snippets.

@shimakyohsuke
Created August 3, 2015 06:10
Show Gist options
  • Save shimakyohsuke/30ad5ae5be9db79f7165 to your computer and use it in GitHub Desktop.
Save shimakyohsuke/30ad5ae5be9db79f7165 to your computer and use it in GitHub Desktop.
atom editor init.coffee
fs = require 'fs'
atom.workspace.onDidOpen ->
editor = atom.workspace.getActiveTextEditor()
try
filePath = editor.getPath()
console.log filePath
catch error
console.log "error"
return
return unless fs.existsSync(filePath)
jschardet = require 'jschardet'
iconv = require 'iconv-lite'
fs.readFile filePath, (error, buffer) =>
return if error?
{encoding} = jschardet.detect(buffer) ? {}
encoding = 'utf8' if encoding is 'ascii'
return unless iconv.encodingExists(encoding)
encoding = encoding.toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, '')
editor.setEncoding(encoding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment