diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index 29dd823a3d..bd172d5825 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -766,9 +766,15 @@ func (b *Buffer) UpdateRules() { if length > 0 { signatureMatch := false if length > 1 { + detectlimit := util.IntOpt(b.Settings["detectlimit"]) + lineCount := len(b.lines) + limit := lineCount + if detectlimit > 0 && lineCount > detectlimit { + limit = detectlimit + } for i := 0; i < length && !signatureMatch; i++ { if syntaxFiles[i].header.HasFileSignature() { - for j := 0; j < 100 && !signatureMatch; j++ { + for j := 0; j < limit && !signatureMatch; j++ { if syntaxFiles[i].header.MatchFileSignature(b.lines[j].data) { syntaxFile = syntaxFiles[i].fileName b.SyntaxDef = syntaxFiles[i].syntaxDef diff --git a/internal/config/settings.go b/internal/config/settings.go index 72e998f147..eca52074ac 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -44,6 +44,7 @@ func init() { var optionValidators = map[string]optionValidator{ "autosave": validateNonNegativeValue, "clipboard": validateClipboard, + "detectlimit": validateNonNegativeValue, "tabsize": validatePositiveValue, "scrollmargin": validateNonNegativeValue, "scrollspeed": validateNonNegativeValue, @@ -280,6 +281,7 @@ var defaultCommonSettings = map[string]interface{}{ "basename": false, "colorcolumn": float64(0), "cursorline": true, + "detectlimit": float64(100), "diffgutter": false, "encoding": "utf-8", "eofnewline": true, diff --git a/runtime/help/options.md b/runtime/help/options.md index 3170dc4c21..376efdb527 100644 --- a/runtime/help/options.md +++ b/runtime/help/options.md @@ -100,6 +100,13 @@ Here are the available options: default value: `true` +* `detectlimit`: if this is not set to 0, it will limit the amount of first + lines in a file that are matched to determine the filetype. + A higher limit means better accuracy of guessing the filetype, but also + taking more time. + + default value: `100` + * `diffgutter`: display diff indicators before lines. default value: `false` diff --git a/runtime/syntax/README.md b/runtime/syntax/README.md index e97a4f77ed..5bcbf1380e 100644 --- a/runtime/syntax/README.md +++ b/runtime/syntax/README.md @@ -2,7 +2,7 @@ Here are micro's syntax files. -Each yaml file specifies how to detect the filetype based on file extension or given signature. The signature can be matched to a maximum of 100 lines (to limit parse times) for a best "guess". +Each yaml file specifies how to detect the filetype based on file extension or given signature. The signature can be matched to all available lines of the file or to the value defined with the option `detectlimit` (to limit parse times) for a best "guess". Then there are patterns and regions linked to highlight groups which tell micro how to highlight that filetype. Making your own syntax files is very simple. I recommend you check the file after you are finished with the