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 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.
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 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.
* 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)
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.
# Contributing

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])

View File

@@ -1,4 +1,4 @@
# 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
and syntax files, you can put in in `~/.micro/colorschemes` and `~/.micro/syntax` respectively.
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 `ConfigDir/colorschemes` and `ConfigDir/syntax` respectively.

View File

@@ -1,7 +1,7 @@
# Micro syntax highlighting files
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).
Micro syntax files are almost identical to Nano's, except for some key differences: