MCPcopy
hub / github.com/micro-editor/micro / NewBuffer

Function NewBuffer

internal/buffer/buffer.go:357–500  ·  view source on GitHub ↗

NewBuffer creates a new buffer from a given reader with a given path Ensure that ReadSettings and InitGlobalSettings have been called before creating a new buffer Places the cursor at startcursor. If startcursor is -1, -1 places the cursor at an autodetected location (based on savecursor or :LINE:CO

(r io.Reader, size int64, path string, btype BufType, cmd Command)

Source from the content-addressed store, hash-verified

355// Places the cursor at startcursor. If startcursor is -1, -1 places the
356// cursor at an autodetected location (based on savecursor or :LINE:COL)
357func NewBuffer(r io.Reader, size int64, path string, btype BufType, cmd Command) *Buffer {
358 absPath, err := filepath.Abs(path)
359 if err != nil {
360 absPath = path
361 }
362
363 b := new(Buffer)
364
365 found := false
366 if len(path) > 0 {
367 for _, buf := range OpenBuffers {
368 if buf.AbsPath == absPath && buf.Type != BTInfo {
369 found = true
370 b.SharedBuffer = buf.SharedBuffer
371 b.EventHandler = buf.EventHandler
372 }
373 }
374 }
375
376 hasBackup := false
377 if !found {
378 b.SharedBuffer = new(SharedBuffer)
379 b.Type = btype
380
381 b.AbsPath = absPath
382 b.Path = path
383
384 b.Settings = config.DefaultCommonSettings()
385 b.LocalSettings = make(map[string]bool)
386 for k, v := range config.GlobalSettings {
387 if _, ok := config.DefaultGlobalOnlySettings[k]; !ok {
388 // make sure setting is not global-only
389 b.Settings[k] = v
390 }
391 }
392 config.UpdatePathGlobLocals(b.Settings, absPath)
393
394 b.encoding, err = htmlindex.Get(b.Settings["encoding"].(string))
395 if err != nil {
396 b.encoding = unicode.UTF8
397 b.Settings["encoding"] = "utf-8"
398 }
399
400 var ok bool
401 hasBackup, ok = b.ApplyBackup(size)
402
403 if !ok {
404 return NewBufferFromString("", "", btype)
405 }
406 if !hasBackup {
407 reader := bufio.NewReader(transform.NewReader(r, b.encoding.NewDecoder()))
408
409 var ff FileFormat = FFAuto
410
411 if size == 0 {
412 // for empty files, use the fileformat setting instead of
413 // autodetection
414 switch b.Settings["fileformat"] {

Callers 3

NewBufferFromStringFunction · 0.70

Calls 15

DefaultCommonSettingsFunction · 0.92
UpdatePathGlobLocalsFunction · 0.92
UpdateFileTypeLocalsFunction · 0.92
TermMessageFunction · 0.92
RunPluginFnFunction · 0.92
NewBufferFromStringFunction · 0.85
NewLineArrayFunction · 0.85
NewEventHandlerFunction · 0.85
NewCursorFunction · 0.85
GetMethod · 0.80
ApplyBackupMethod · 0.80
UpdateModTimeMethod · 0.80

Tested by

no test coverage detected