@ -32,6 +32,8 @@ function(
this . base _url = options . base _url ;
this . file _path = options . file _path ;
this . config = options . config ;
this . file _extension _modes = options . file _extension _modes || { } ;
this . codemirror = new CodeMirror ( $ ( this . selector ) [ 0 ] ) ;
this . codemirror . on ( 'changes' , function ( cm , changes ) {
that . _clean _state ( ) ;
@ -54,6 +56,14 @@ function(
) ;
that . _set _codemirror _options ( cmopts ) ;
that . events . trigger ( 'config_changed.Editor' , { config : that . config } ) ;
if ( cfg . file _extension _modes ) {
// check for file extension in user preferences
var modename = cfg . file _extension _modes [ that . _get _file _extension ( ) ] ;
if ( modename ) {
var modeinfo = CodeMirror . findModeByName ( modename ) ;
that . set _codemirror _mode ( modeinfo ) ;
}
}
that . _clean _state ( ) ;
} ) ;
this . clean _sel = $ ( '<div/>' ) ;
@ -99,7 +109,7 @@ function(
}
) ;
} ;
Editor . prototype . _set _mode _for _model = function ( model ) {
/** Set the CodeMirror mode based on the file model */
@ -107,20 +117,28 @@ function(
// first by mime-type, then by file extension
var modeinfo ;
// mimetype is unset on file rename
if ( model . mimetype ) {
modeinfo = CodeMirror . findModeByMIME ( model . mimetype ) ;
var ext = this . _get _file _extension ( ) ;
if ( ext ) {
// check if a mode has been remembered for this extension
var modename = this . file _extension _modes [ ext ] ;
if ( modename ) {
modeinfo = CodeMirror . findModeByName ( modename ) ;
}
}
// prioritize CodeMirror's filename identification
if ( ! modeinfo || modeinfo . mode === "null" ) {
// find by mime failed, use find by ext
var ext _idx = model . name . lastIndexOf ( '.' ) ;
if ( ext _idx > 0 ) {
// CodeMirror.findModeByExtension wants extension without '.'
modeinfo = CodeMirror . findModeByExtension (
model . name . slice ( ext _idx + 1 ) . toLowerCase ( ) ) ;
modeinfo = CodeMirror . findModeByFileName ( model . name ) ;
// codemirror's filename identification is case-sensitive.
// try once more with lowercase extension
if ( ! modeinfo && ext ) {
// CodeMirror wants lowercase ext without leading '.'
modeinfo = CodeMirror . findModeByExtension ( ext . slice ( 1 ) . toLowerCase ( ) ) ;
}
}
if ( model . mimetype && ( ! modeinfo || modeinfo . mode === "null" ) ) {
// mimetype is not set on file rename
modeinfo = CodeMirror . findModeByMIME ( model . mimetype ) ;
}
if ( modeinfo ) {
this . set _codemirror _mode ( modeinfo ) ;
}
@ -134,11 +152,41 @@ function(
that . events . trigger ( "mode_changed.Editor" , modeinfo ) ;
} ) ;
} ;
Editor . prototype . save _codemirror _mode = function ( modeinfo ) {
/** save the selected codemirror mode for the current extension in config */
var update _mode _map = { } ;
var ext = this . _get _file _extension ( ) ;
// no extension, nothing to save
// TODO: allow remembering no-extension things like Makefile?
if ( ! ext ) return ;
update _mode _map [ ext ] = modeinfo . name ;
return this . config . update ( {
Editor : {
file _extension _modes : update _mode _map ,
}
} )
} ;
Editor . prototype . get _filename = function ( ) {
return utils . url _path _split ( this . file _path ) [ 1 ] ;
} ;
Editor . prototype . _get _file _extension = function ( ) {
/ * * r e t u r n f i l e e x t e n s i o n * i n c l u d i n g * .
Returns undefined if no extension is found .
* /
var filename = this . get _filename ( ) ;
var ext _idx = filename . lastIndexOf ( '.' ) ;
if ( ext _idx < 0 ) {
return ;
} else {
return filename . slice ( ext _idx ) ;
}
} ;
Editor . prototype . rename = function ( new _name ) {
/** rename the file */
var that = this ;
@ -203,7 +251,7 @@ function(
} ) ;
var that = this ;
} ;
Editor . prototype . update _codemirror _options = function ( options ) {
/** update codemirror options locally and save changes in config */
var that = this ;