19 "github.com/zyedidia/micro/cmd/micro/config"
20 "github.com/zyedidia/micro/cmd/micro/highlight"
22 . "github.com/zyedidia/micro/cmd/micro/util"
25 // LargeFileThreshold is the number of bytes when fastdirty is forced
26 // because hashing is too slow
27 const LargeFileThreshold = 50000
29 // overwriteFile opens the given file for writing, truncating if one exists, and then calls
30 // the supplied function with the file as io.Writer object, also making sure the file is
32 func overwriteFile(name string, fn func(io.Writer) error) (err error) {
35 if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
40 if e := file.Close(); e != nil && err == nil {
45 w := bufio.NewWriter(file)
47 if err = fn(w); err != nil {
55 // The BufType defines what kind of buffer this is
58 Readonly bool // The file cannot be edited
59 Scratch bool // The file cannot be saved
63 BTDefault = BufType{0, false, false}
64 BTHelp = BufType{1, true, true}
65 BTLog = BufType{2, true, true}
66 BTScratch = BufType{3, false, true}
67 BTRaw = BufType{4, true, true}
77 // Path to the file on disk
79 // Absolute path to the file on disk
81 // Name of the buffer on the status line
84 // Whether or not the buffer has been modified since it was opened
87 // Stores the last modification time of the file the buffer is pointing to
90 SyntaxDef *highlight.Def
91 Highlighter *highlight.Highlighter
93 // Hash of the original buffer -- empty if fastdirty is on
94 origHash [md5.Size]byte
96 // Settings customized by the user
97 Settings map[string]interface{}
99 // Type of the buffer (e.g. help, raw, scratch etc..)
103 // The SerializedBuffer holds the types that get serialized when a buffer is saved
104 // These are used for the savecursor and saveundo options
105 type SerializedBuffer struct {
106 EventHandler *EventHandler
111 // NewBufferFromFile opens a new buffer using the given path
112 // It will also automatically handle `~`, and line/column with filename:l:c
113 // It will return an empty buffer if the path does not exist
114 // and an error if the file is a directory
115 func NewBufferFromFile(path string) (*Buffer, error) {
117 filename, cursorPosition := GetPathAndCursorPosition(path)
118 filename, err = ReplaceHome(filename)
123 file, err := os.Open(filename)
124 fileInfo, _ := os.Stat(filename)
126 if err == nil && fileInfo.IsDir() {
127 return nil, errors.New(filename + " is a directory")
134 // File does not exist -- create an empty buffer with that name
135 buf = NewBufferFromString("", filename)
137 buf = NewBuffer(file, FSize(file), filename, cursorPosition)
143 // NewBufferFromString creates a new buffer containing the given string
144 func NewBufferFromString(text, path string) *Buffer {
145 return NewBuffer(strings.NewReader(text), int64(len(text)), path, nil)
148 // NewBuffer creates a new buffer from a given reader with a given path
149 // Ensure that ReadSettings and InitGlobalSettings have been called before creating
151 func NewBuffer(reader io.Reader, size int64, path string, cursorPosition []string) *Buffer {
154 b.Settings = config.DefaultLocalSettings()
155 for k, v := range config.GlobalSettings {
156 if _, ok := b.Settings[k]; ok {
160 config.InitLocalSettings(b.Settings, b.Path)
162 b.LineArray = NewLineArray(uint64(size), FFAuto, reader)
164 absPath, _ := filepath.Abs(path)
169 // The last time this file was modified
170 b.ModTime, _ = GetModTime(b.Path)
172 b.EventHandler = NewEventHandler(b)
176 if _, err := os.Stat(config.ConfigDir + "/buffers/"); os.IsNotExist(err) {
177 os.Mkdir(config.ConfigDir+"/buffers/", os.ModePerm)
180 // cursorLocation, err := GetBufferCursorLocation(cursorPosition, b)
181 // b.startcursor = Cursor{
182 // Loc: cursorLocation,
186 if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
187 err := b.Unserialize()
193 if !b.Settings["fastdirty"].(bool) {
194 if size > LargeFileThreshold {
195 // If the file is larger than LargeFileThreshold fastdirty needs to be on
196 b.Settings["fastdirty"] = true
198 calcHash(b, &b.origHash)
205 // GetName returns the name that should be displayed in the statusline
207 func (b *Buffer) GetName() string {
217 // FileType returns the buffer's filetype
218 func (b *Buffer) FileType() string {
219 return b.Settings["filetype"].(string)
222 // ReOpen reloads the current buffer from disk
223 func (b *Buffer) ReOpen() error {
224 data, err := ioutil.ReadFile(b.Path)
230 b.EventHandler.ApplyDiff(txt)
232 b.ModTime, err = GetModTime(b.Path)
235 // TODO: buffer cursor
236 // b.Cursor.Relocate()
241 // Save saves the buffer to its default path
242 func (b *Buffer) Save() error {
243 return b.SaveAs(b.Path)
246 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
247 func (b *Buffer) SaveAs(filename string) error {
248 // TODO: rmtrailingws and updaterules
250 // if b.Settings["rmtrailingws"].(bool) {
251 // for i, l := range b.lines {
252 // pos := len(bytes.TrimRightFunc(l.data, unicode.IsSpace))
254 // if pos < len(l.data) {
255 // b.deleteToEnd(Loc{pos, i})
259 // b.Cursor.Relocate()
262 if b.Settings["eofnewline"].(bool) {
264 if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
269 // Update the last time this file was updated after saving
271 b.ModTime, _ = GetModTime(filename)
274 // Removes any tilde and replaces with the absolute path to home
275 absFilename, _ := ReplaceHome(filename)
277 // TODO: save creates parent dirs
278 // // Get the leading path to the file | "." is returned if there's no leading path provided
279 // if dirname := filepath.Dir(absFilename); dirname != "." {
280 // // Check if the parent dirs don't exist
281 // if _, statErr := os.Stat(dirname); os.IsNotExist(statErr) {
282 // // Prompt to make sure they want to create the dirs that are missing
283 // if yes, canceled := messenger.YesNoPrompt("Parent folders \"" + dirname + "\" do not exist. Create them? (y,n)"); yes && !canceled {
284 // // Create all leading dir(s) since they don't exist
285 // if mkdirallErr := os.MkdirAll(dirname, os.ModePerm); mkdirallErr != nil {
286 // // If there was an error creating the dirs
287 // return mkdirallErr
290 // // If they canceled the creation of leading dirs
291 // return errors.New("Save aborted")
298 err := overwriteFile(absFilename, func(file io.Writer) (e error) {
299 if len(b.lines) == 0 {
305 if b.Settings["fileformat"] == "dos" {
306 eol = []byte{'\r', '\n'}
312 if fileSize, e = file.Write(b.lines[0].data); e != nil {
316 for _, l := range b.lines[1:] {
317 if _, e = file.Write(eol); e != nil {
320 if _, e = file.Write(l.data); e != nil {
323 fileSize += len(eol) + len(l.data)
332 if !b.Settings["fastdirty"].(bool) {
333 if fileSize > LargeFileThreshold {
334 // For large files 'fastdirty' needs to be on
335 b.Settings["fastdirty"] = true
337 calcHash(b, &b.origHash)
342 absPath, _ := filepath.Abs(filename)
348 // SaveWithSudo saves the buffer to the default path with sudo
349 func (b *Buffer) SaveWithSudo() error {
350 return b.SaveAsWithSudo(b.Path)
353 // SaveAsWithSudo is the same as SaveAs except it uses a neat trick
354 // with tee to use sudo so the user doesn't have to reopen micro with sudo
355 func (b *Buffer) SaveAsWithSudo(filename string) error {
358 absPath, _ := filepath.Abs(filename)
361 // Set up everything for the command
362 cmd := exec.Command(config.GlobalSettings["sucmd"].(string), "tee", filename)
363 cmd.Stdin = bytes.NewBuffer(b.Bytes())
365 // This is a trap for Ctrl-C so that it doesn't kill micro
366 // Instead we trap Ctrl-C to kill the program we're running
367 c := make(chan os.Signal, 1)
368 signal.Notify(c, os.Interrupt)
381 b.ModTime, _ = GetModTime(filename)
387 func (b *Buffer) SetCursors(c []*Cursor) {
391 func (b *Buffer) GetActiveCursor() *Cursor {
395 func (b *Buffer) GetCursor(n int) *Cursor {
399 func (b *Buffer) GetCursors() []*Cursor {
403 func (b *Buffer) NumCursors() int {
404 return len(b.cursors)
407 func (b *Buffer) LineBytes(n int) []byte {
408 if n >= len(b.lines) || n < 0 {
411 return b.lines[n].data
414 func (b *Buffer) LinesNum() int {
418 func (b *Buffer) Start() Loc {
422 // End returns the location of the last character in the buffer
423 func (b *Buffer) End() Loc {
424 numlines := len(b.lines)
425 return Loc{utf8.RuneCount(b.lines[numlines-1].data), numlines - 1}
428 // RuneAt returns the rune at a given location in the buffer
429 func (b *Buffer) RuneAt(loc Loc) rune {
430 line := b.LineBytes(loc.Y)
434 r, size := utf8.DecodeRune(line)
446 // Modified returns if this buffer has been modified since
448 func (b *Buffer) Modified() bool {
449 if b.Settings["fastdirty"].(bool) {
453 var buff [md5.Size]byte
456 return buff != b.origHash
459 // calcHash calculates md5 hash of all lines in the buffer
460 func calcHash(b *Buffer, out *[md5.Size]byte) {
463 if len(b.lines) > 0 {
464 h.Write(b.lines[0].data)
466 for _, l := range b.lines[1:] {
467 h.Write([]byte{'\n'})
476 gob.Register(TextEvent{})
477 gob.Register(SerializedBuffer{})
480 // Serialize serializes the buffer to config.ConfigDir/buffers
481 func (b *Buffer) Serialize() error {
482 if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
486 name := config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath)
488 return overwriteFile(name, func(file io.Writer) error {
489 return gob.NewEncoder(file).Encode(SerializedBuffer{
491 b.GetActiveCursor().Loc,
497 func (b *Buffer) Unserialize() error {
498 // If either savecursor or saveundo is turned on, we need to load the serialized information
499 // from ~/.config/micro/buffers
500 file, err := os.Open(config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath))
503 var buffer SerializedBuffer
504 decoder := gob.NewDecoder(file)
505 gob.Register(TextEvent{})
506 err = decoder.Decode(&buffer)
508 return errors.New(err.Error() + "\nYou may want to remove the files in ~/.config/micro/buffers (these files store the information for the 'saveundo' and 'savecursor' options) if this problem persists.")
510 if b.Settings["savecursor"].(bool) {
511 b.StartCursor = buffer.Cursor
514 if b.Settings["saveundo"].(bool) {
515 // We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime
516 if b.ModTime == buffer.ModTime {
517 b.EventHandler = buffer.EventHandler
518 b.EventHandler.buf = b
525 // UpdateRules updates the syntax rules and filetype for this buffer
526 // This is called when the colorscheme changes
527 func (b *Buffer) UpdateRules() {
529 var files []*highlight.File
530 for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
531 data, err := f.Data()
533 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
535 file, err := highlight.ParseFile(data)
537 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
540 ftdetect, err := highlight.ParseFtDetect(file)
542 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
546 ft := b.Settings["filetype"].(string)
547 if (ft == "Unknown" || ft == "") && !rehighlight {
548 if highlight.MatchFiletype(ftdetect, b.Path, b.lines[0].data) {
549 header := new(highlight.Header)
550 header.FileType = file.FileType
551 header.FtDetect = ftdetect
552 b.SyntaxDef, err = highlight.ParseDef(file, header)
554 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
560 if file.FileType == ft && !rehighlight {
561 header := new(highlight.Header)
562 header.FileType = file.FileType
563 header.FtDetect = ftdetect
564 b.SyntaxDef, err = highlight.ParseDef(file, header)
566 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
572 files = append(files, file)
576 if b.SyntaxDef != nil {
577 highlight.ResolveIncludes(b.SyntaxDef, files)
580 if b.Highlighter == nil || rehighlight {
581 if b.SyntaxDef != nil {
582 b.Settings["filetype"] = b.SyntaxDef.FileType
583 b.Highlighter = highlight.NewHighlighter(b.SyntaxDef)
584 if b.Settings["syntax"].(bool) {
585 b.Highlighter.HighlightStates(b)