15 "github.com/zyedidia/micro/cmd/micro/config"
16 "github.com/zyedidia/micro/cmd/micro/highlight"
18 . "github.com/zyedidia/micro/cmd/micro/util"
21 // LargeFileThreshold is the number of bytes when fastdirty is forced
22 // because hashing is too slow
23 const LargeFileThreshold = 50000
25 // overwriteFile opens the given file for writing, truncating if one exists, and then calls
26 // the supplied function with the file as io.Writer object, also making sure the file is
28 func overwriteFile(name string, fn func(io.Writer) error) (err error) {
31 if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
36 if e := file.Close(); e != nil && err == nil {
41 w := bufio.NewWriter(file)
43 if err = fn(w); err != nil {
51 // The BufType defines what kind of buffer this is
54 Readonly bool // The file cannot be edited
55 Scratch bool // The file cannot be saved
59 BTDefault = BufType{0, false, false}
60 BTHelp = BufType{1, true, true}
61 BTLog = BufType{2, true, true}
62 BTScratch = BufType{3, false, true}
63 BTRaw = BufType{4, true, true}
66 // Buffer stores the main information about a currently open file including
67 // the actual text (in a LineArray), the undo/redo stack (in an EventHandler)
68 // all the cursors, the syntax highlighting info, the settings for the buffer
69 // and some misc info about modification time and path location.
70 // The syntax highlighting info must be stored with the buffer because the syntax
71 // highlighter attaches information to each line of the buffer for optimization
72 // purposes so it doesn't have to rehighlight everything on every update.
80 // Path to the file on disk
82 // Absolute path to the file on disk
84 // Name of the buffer on the status line
87 // Whether or not the buffer has been modified since it was opened
90 // Stores the last modification time of the file the buffer is pointing to
93 SyntaxDef *highlight.Def
94 Highlighter *highlight.Highlighter
96 // Hash of the original buffer -- empty if fastdirty is on
97 origHash [md5.Size]byte
99 // Settings customized by the user
100 Settings map[string]interface{}
102 // Type of the buffer (e.g. help, raw, scratch etc..)
106 // NewBufferFromFile opens a new buffer using the given path
107 // It will also automatically handle `~`, and line/column with filename:l:c
108 // It will return an empty buffer if the path does not exist
109 // and an error if the file is a directory
110 func NewBufferFromFile(path string) (*Buffer, error) {
112 filename, cursorPosition := GetPathAndCursorPosition(path)
113 filename, err = ReplaceHome(filename)
118 file, err := os.Open(filename)
119 fileInfo, _ := os.Stat(filename)
121 if err == nil && fileInfo.IsDir() {
122 return nil, errors.New(filename + " is a directory")
129 // File does not exist -- create an empty buffer with that name
130 buf = NewBufferFromString("", filename)
132 buf = NewBuffer(file, FSize(file), filename, cursorPosition)
138 // NewBufferFromString creates a new buffer containing the given string
139 func NewBufferFromString(text, path string) *Buffer {
140 return NewBuffer(strings.NewReader(text), int64(len(text)), path, nil)
143 // NewBuffer creates a new buffer from a given reader with a given path
144 // Ensure that ReadSettings and InitGlobalSettings have been called before creating
146 func NewBuffer(reader io.Reader, size int64, path string, cursorPosition []string) *Buffer {
149 b.Settings = config.DefaultLocalSettings()
150 for k, v := range config.GlobalSettings {
151 if _, ok := b.Settings[k]; ok {
155 config.InitLocalSettings(b.Settings, b.Path)
157 b.LineArray = NewLineArray(uint64(size), FFAuto, reader)
159 absPath, _ := filepath.Abs(path)
164 // The last time this file was modified
165 b.ModTime, _ = GetModTime(b.Path)
167 b.EventHandler = NewEventHandler(b)
171 if _, err := os.Stat(config.ConfigDir + "/buffers/"); os.IsNotExist(err) {
172 os.Mkdir(config.ConfigDir+"/buffers/", os.ModePerm)
175 // cursorLocation, err := GetBufferCursorLocation(cursorPosition, b)
176 // b.startcursor = Cursor{
177 // Loc: cursorLocation,
181 if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
182 err := b.Unserialize()
188 if !b.Settings["fastdirty"].(bool) {
189 if size > LargeFileThreshold {
190 // If the file is larger than LargeFileThreshold fastdirty needs to be on
191 b.Settings["fastdirty"] = true
193 calcHash(b, &b.origHash)
200 // GetName returns the name that should be displayed in the statusline
202 func (b *Buffer) GetName() string {
212 // FileType returns the buffer's filetype
213 func (b *Buffer) FileType() string {
214 return b.Settings["filetype"].(string)
217 // ReOpen reloads the current buffer from disk
218 func (b *Buffer) ReOpen() error {
219 data, err := ioutil.ReadFile(b.Path)
225 b.EventHandler.ApplyDiff(txt)
227 b.ModTime, err = GetModTime(b.Path)
230 // TODO: buffer cursor
231 // b.Cursor.Relocate()
234 // SetCursors resets this buffer's cursors to a new list
235 func (b *Buffer) SetCursors(c []*Cursor) {
239 // GetActiveCursor returns the main cursor in this buffer
240 func (b *Buffer) GetActiveCursor() *Cursor {
244 // GetCursor returns the nth cursor
245 func (b *Buffer) GetCursor(n int) *Cursor {
249 // GetCursors returns the list of cursors in this buffer
250 func (b *Buffer) GetCursors() []*Cursor {
254 // NumCursors returns the number of cursors
255 func (b *Buffer) NumCursors() int {
256 return len(b.cursors)
259 // LineBytes returns line n as an array of bytes
260 func (b *Buffer) LineBytes(n int) []byte {
261 if n >= len(b.lines) || n < 0 {
264 return b.lines[n].data
267 // LinesNum returns the number of lines in the buffer
268 func (b *Buffer) LinesNum() int {
272 // Start returns the start of the buffer
273 func (b *Buffer) Start() Loc {
277 // End returns the location of the last character in the buffer
278 func (b *Buffer) End() Loc {
279 numlines := len(b.lines)
280 return Loc{utf8.RuneCount(b.lines[numlines-1].data), numlines - 1}
283 // RuneAt returns the rune at a given location in the buffer
284 func (b *Buffer) RuneAt(loc Loc) rune {
285 line := b.LineBytes(loc.Y)
289 r, size := utf8.DecodeRune(line)
301 // Modified returns if this buffer has been modified since
303 func (b *Buffer) Modified() bool {
304 if b.Settings["fastdirty"].(bool) {
308 var buff [md5.Size]byte
311 return buff != b.origHash
314 // calcHash calculates md5 hash of all lines in the buffer
315 func calcHash(b *Buffer, out *[md5.Size]byte) {
318 if len(b.lines) > 0 {
319 h.Write(b.lines[0].data)
321 for _, l := range b.lines[1:] {
322 h.Write([]byte{'\n'})
330 func (b *Buffer) insert(pos Loc, value []byte) {
332 b.LineArray.insert(pos, value)
334 func (b *Buffer) remove(start, end Loc) []byte {
336 sub := b.LineArray.remove(start, end)
339 func (b *Buffer) deleteToEnd(start Loc) {
341 b.LineArray.deleteToEnd(start)
344 // UpdateRules updates the syntax rules and filetype for this buffer
345 // This is called when the colorscheme changes
346 func (b *Buffer) UpdateRules() {
348 var files []*highlight.File
349 for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
350 data, err := f.Data()
352 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
354 file, err := highlight.ParseFile(data)
356 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
359 ftdetect, err := highlight.ParseFtDetect(file)
361 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
365 ft := b.Settings["filetype"].(string)
366 if (ft == "Unknown" || ft == "") && !rehighlight {
367 if highlight.MatchFiletype(ftdetect, b.Path, b.lines[0].data) {
368 header := new(highlight.Header)
369 header.FileType = file.FileType
370 header.FtDetect = ftdetect
371 b.SyntaxDef, err = highlight.ParseDef(file, header)
373 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
379 if file.FileType == ft && !rehighlight {
380 header := new(highlight.Header)
381 header.FileType = file.FileType
382 header.FtDetect = ftdetect
383 b.SyntaxDef, err = highlight.ParseDef(file, header)
385 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
391 files = append(files, file)
395 if b.SyntaxDef != nil {
396 highlight.ResolveIncludes(b.SyntaxDef, files)
399 if b.Highlighter == nil || rehighlight {
400 if b.SyntaxDef != nil {
401 b.Settings["filetype"] = b.SyntaxDef.FileType
402 b.Highlighter = highlight.NewHighlighter(b.SyntaxDef)
403 if b.Settings["syntax"].(bool) {
404 b.Highlighter.HighlightStates(b)
410 // IndentString returns this buffer's indent method (a tabstop or n spaces
411 // depending on the settings)
412 func (b *Buffer) IndentString(tabsize int) string {
413 if b.Settings["tabstospaces"].(bool) {
414 return Spaces(tabsize)