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

@ -111,6 +111,11 @@ to enable word selection, and triple click to enable line selection.
# Configuration # Configuration
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.
At this point, there isn't much you can configure. At this point, there isn't much you can configure.
Micro has a few options which you can set: Micro has a few options which you can set:
@ -122,7 +127,7 @@ To set an option run Ctrl-e to execute a command, and type `set option value`, s
The syntax option can simply be on or off, so for example to turn syntax highlighting off, run `set syntax off`. The syntax option can simply be on or off, so for example to turn syntax highlighting off, run `set syntax off`.
The colorscheme can be selected from all the files in the `~/.micro/colorschemes/` directory. Micro comes by default with three colorschemes: The colorscheme can be selected from all the files in the `ConfigDir/colorschemes/` directory. Micro comes by default with three colorschemes:
* default: this is the default colorscheme. * default: this is the default colorscheme.
* solarized: this is the solarized colorscheme (used in the screenshot). You should have the solarized color palette in your terminal to use it. * solarized: this is the solarized colorscheme (used in the screenshot). You should have the solarized color palette in your terminal to use it.
@ -130,7 +135,7 @@ The colorscheme can be selected from all the files in the `~/.micro/colorschemes
These are embedded in the Go binary, but to see their source code, look [here](./runtime/colorschemes) These are embedded in the Go binary, but to see their source code, look [here](./runtime/colorschemes)
Any option you set in the editor will be saved to the file `~/.micro/settings.json` so, in effect, your configuration file will be created Any option you set in the editor will be saved to the file `ConfigDir/settings.json` so, in effect, your configuration file will be created
for you. If you'd like to take your configuration with you to another machine, simply copy the `settings.json` to the other machine. for you. If you'd like to take your configuration with you to another machine, simply copy the `settings.json` to the other machine.
# Contributing # Contributing

View file

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

View file

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

View file

@ -5,6 +5,7 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir"
"io/ioutil" "io/ioutil"
"os" "os"
) )
@ -25,6 +26,11 @@ var (
// The default style // The default style
defStyle tcell.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 // LoadInput loads the file input for the editor
@ -63,6 +69,30 @@ func LoadInput() (string, []byte, error) {
return filename, input, err 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() { func main() {
filename, input, err := LoadInput() filename, input, err := LoadInput()
if err != nil { if err != nil {
@ -70,6 +100,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
InitConfigDir()
InitSettings() InitSettings()
// Load the syntax files, including the colorscheme // Load the syntax files, including the colorscheme

View file

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

View file

@ -1,4 +1,4 @@
# Runtime files for Micro # Runtime files for Micro
This directory will be embedded in the Go binary for portability, but it may just as well be put in `~/.micro`. If you would like to make your own colorschemes This directory will be embedded in the Go binary for portability, but it may just as well be put in `ConfigDir`. If you would like to make your own colorschemes
and syntax files, you can put in in `~/.micro/colorschemes` and `~/.micro/syntax` respectively. and syntax files, you can put in in `ConfigDir/colorschemes` and `ConfigDir/syntax` respectively.

View file

@ -1,7 +1,7 @@
# Micro syntax highlighting files # Micro syntax highlighting files
These are the syntax highlighting files for micro. To install them, just These are the syntax highlighting files for micro. To install them, just
put all the syntax files in `~/.micro/syntax`. put all the syntax files in `ConfigDir/syntax`.
They are taken from Nano, specifically from [this repository](https://github.com/scopatz/nanorc). They are taken from Nano, specifically from [this repository](https://github.com/scopatz/nanorc).
Micro syntax files are almost identical to Nano's, except for some key differences: Micro syntax files are almost identical to Nano's, except for some key differences: