Use XDG_CONFIG_HOME for configuration directory

Fixes #18
This commit is contained in:
Zachary Yedidia
2016-04-18 10:31:19 -04:00
parent 0ca7ac0c27
commit 8dee0ad2d5
8 changed files with 57 additions and 43 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/gdamore/tcell"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"regexp"
"strconv"
@@ -23,14 +22,9 @@ func InitColorscheme() {
LoadDefaultColorscheme()
}
// LoadDefaultColorscheme loads the default colorscheme from ~/.micro/colorschemes
// LoadDefaultColorscheme loads the default colorscheme from $(configDir)/colorschemes
func LoadDefaultColorscheme() {
dir, err := homedir.Dir()
if err != nil {
TermMessage("Error finding your home directory\nCan't load runtime files")
return
}
LoadColorscheme(settings.Colorscheme, dir+"/.micro/colorschemes")
LoadColorscheme(settings.Colorscheme, configDir+"/colorschemes")
}
// LoadColorscheme loads the given colorscheme from a directory

View File

@@ -48,8 +48,15 @@ does not have any spaces in it, you may omit the quotes.
Micro options:
colorscheme: loads the colorscheme stored in ~/.micro/colorschemes/'option'.micro
Configuration directory:
Micro uses the $XDG_CONFIG_HOME/micro as the configuration directory. As per the XDG spec,
if $XDG_CONFIG_HOME is not set, ~/.config/micro is used as the config directory.
colorscheme: loads the colorscheme stored in $(configDir)/colorschemes/'option'.micro
default value: 'default'
Note that the default colorschemes (default, solarized, and solarized-tc) are not located in configDir,
because they are embedded in the micro binary
tabsize: sets the tab size to 'option'
default value: '4'

View File

@@ -2,7 +2,6 @@ package main
import (
"github.com/gdamore/tcell"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"path/filepath"
"regexp"
@@ -109,14 +108,9 @@ var preInstalledSynFiles = []string{
"zsh",
}
// LoadSyntaxFiles loads the syntax files from the default directory ~/.micro
// LoadSyntaxFiles loads the syntax files from the default directory (configDir)
func LoadSyntaxFiles() {
home, err := homedir.Dir()
if err != nil {
TermMessage("Error finding your home directory\nCan't load syntax files")
return
}
LoadSyntaxFilesFromDir(home + "/.micro/syntax")
LoadSyntaxFilesFromDir(configDir + "/syntax")
for _, filetype := range preInstalledSynFiles {
data, err := Asset("runtime/syntax/" + filetype + ".micro")

View File

@@ -5,6 +5,7 @@ import (
"github.com/gdamore/tcell"
"github.com/go-errors/errors"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"os"
)
@@ -25,6 +26,11 @@ var (
// The default style
defStyle tcell.Style
// Where the user's configuration is
// This should be $XDG_CONFIG_HOME/micro
// If $XDG_CONFIG_HOME is not set, it is ~/.config/micro
configDir string
)
// LoadInput loads the file input for the editor
@@ -63,6 +69,30 @@ func LoadInput() (string, []byte, error) {
return filename, input, err
}
// InitConfigDir finds the configuration directory for micro according to the
// XDG spec.
// If no directory is found, it creates one.
func InitConfigDir() {
xdgHome := os.Getenv("XDG_CONFIG_HOME")
if xdgHome == "" {
home, err := homedir.Dir()
if err != nil {
TermMessage("Error finding your home directory\nCan't load syntax files")
return
}
configDir = home + "/.config/micro"
} else {
configDir = xdgHome + "/micro"
}
if _, err := os.Stat(configDir); os.IsNotExist(err) {
err = os.Mkdir(configDir, os.ModePerm)
if err != nil {
TermMessage("Error creating configuration directory: " + err.Error())
}
}
}
func main() {
filename, input, err := LoadInput()
if err != nil {
@@ -70,6 +100,8 @@ func main() {
os.Exit(1)
}
InitConfigDir()
InitSettings()
// Load the syntax files, including the colorscheme

View File

@@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"os"
"strconv"
@@ -25,13 +24,7 @@ type Settings struct {
// InitSettings initializes the options map and sets all options to their default values
func InitSettings() {
home, err := homedir.Dir()
if err != nil {
TermMessage("Error finding your home directory\nCan't load settings file")
return
}
filename := home + "/.micro/settings.json"
filename := configDir + "/settings.json"
if _, e := os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
if err != nil {
@@ -46,19 +39,13 @@ func InitSettings() {
if err != nil {
TermMessage("Error writing settings.json file: " + err.Error())
}
os.Mkdir(home+"/.micro", 755)
}
}
// WriteSettings writes the settings to the specified filename as JSON
func WriteSettings(filename string) error {
var err error
home, err := homedir.Dir()
if err != nil {
return err
}
if _, e := os.Stat(home + "/.micro"); e == nil {
if _, e := os.Stat(configDir); e == nil {
txt, _ := json.MarshalIndent(settings, "", " ")
err = ioutil.WriteFile(filename, txt, 0644)
}
@@ -77,12 +64,7 @@ func DefaultSettings() Settings {
// SetOption prompts the user to set an option and checks that the response is valid
func SetOption(view *View, args []string) {
home, err := homedir.Dir()
if err != nil {
messenger.Error("Error finding your home directory\nCan't load settings file")
}
filename := home + "/.micro/settings.json"
filename := configDir + "/settings.json"
if len(args) == 2 {
option := strings.TrimSpace(args[0])
value := strings.TrimSpace(args[1])