]> git.lizzy.rs Git - micro.git/blob - internal/buffer/settings.go
Support for highlighting all search matches (hlsearch) (#1762)
[micro.git] / internal / buffer / settings.go
1 package buffer
2
3 import (
4         "github.com/zyedidia/micro/v2/internal/config"
5         "github.com/zyedidia/micro/v2/internal/screen"
6 )
7
8 func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
9         b.Settings[option] = nativeValue
10
11         if option == "fastdirty" {
12                 if !nativeValue.(bool) {
13                         if !b.Modified() {
14                                 e := calcHash(b, &b.origHash)
15                                 if e == ErrFileTooLarge {
16                                         b.Settings["fastdirty"] = false
17                                 }
18                         }
19                 }
20         } else if option == "statusline" {
21                 screen.Redraw()
22         } else if option == "filetype" {
23                 b.UpdateRules()
24         } else if option == "fileformat" {
25                 switch b.Settings["fileformat"].(string) {
26                 case "unix":
27                         b.Endings = FFUnix
28                 case "dos":
29                         b.Endings = FFDos
30                 }
31                 b.isModified = true
32         } else if option == "syntax" {
33                 if !nativeValue.(bool) {
34                         b.ClearMatches()
35                 } else {
36                         b.UpdateRules()
37                 }
38         } else if option == "encoding" {
39                 b.isModified = true
40         } else if option == "readonly" && b.Type.Kind == BTDefault.Kind {
41                 b.Type.Readonly = nativeValue.(bool)
42         } else if option == "hlsearch" {
43                 for _, buf := range OpenBuffers {
44                         if b.SharedBuffer == buf.SharedBuffer {
45                                 buf.HighlightSearch = nativeValue.(bool)
46                         }
47                 }
48         }
49
50         if b.OptionCallback != nil {
51                 b.OptionCallback(option, nativeValue)
52         }
53
54         return nil
55 }
56
57 // SetOption sets a given option to a value just for this buffer
58 func (b *Buffer) SetOption(option, value string) error {
59         if _, ok := b.Settings[option]; !ok {
60                 return config.ErrInvalidOption
61         }
62
63         nativeValue, err := config.GetNativeValue(option, b.Settings[option], value)
64         if err != nil {
65                 return err
66         }
67
68         return b.SetOptionNative(option, nativeValue)
69 }