diff --git a/emacs/emacs.desktop b/emacs/emacs.desktop new file mode 100644 index 0000000..9a8d4cc --- /dev/null +++ b/emacs/emacs.desktop @@ -0,0 +1,29 @@ +[Desktop Entry] +Type=Application +Name=Emacs +GenericName=Emacs Editor +Icon=emacs +Comment=Open an emacs frame through the running daemon + +Exec={{.workspace_bin}} run emacsclient -c %F +StartupWMClass=Emacs +Version=1.0 +Terminal=false + +Categories=Development;TextEditor;IDE;WebDevelopment;GTK; +Keywords=Text;Editor;IDE;scratch; +MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;inode/directory; + +Actions=NewWindow;NewFile;NewInstance + +[Desktop Action NewWindow] +Name=New Window +Exec={{.workspace_bin}} run emacsclient -c %F + +[Desktop Action NewInstance] +Name=New Instance +Exec={{.workspace_bin}} run -privileged emacs %F + +[Desktop Action NewFile] +Name=New File +Exec={{.workspace_bin}} run emacsclient -c -e '(switch-to-buffer (generate-new-buffer "new"))' %F diff --git a/emacs/emacs.png b/emacs/emacs.png new file mode 100644 index 0000000..9ab43d7 Binary files /dev/null and b/emacs/emacs.png differ diff --git a/main.go b/main.go index 7a41d56..5c10e5b 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "bytes" _ "embed" "errors" "flag" @@ -10,10 +11,12 @@ import ( "os" "os/exec" "os/user" + "path" "runtime" "strconv" "strings" "syscall" + "text/template" ) const usage = `workspace - Use a containerized workspace @@ -35,6 +38,12 @@ var customLisp string //go:embed emacs/epaper-theme.el var elispEpaperTheme string +//go:embed emacs/emacs.desktop +var emacsDesktop string + +//go:embed emacs/emacs.png +var emacsIcon []byte + //go:embed bash/bash_aliases var bashAliases string @@ -317,16 +326,24 @@ func SetEnvVars() { os.Setenv("PATH", "/usr/local/go/bin:"+os.Getenv("PATH")) os.Setenv("PATH", "/usr/local/gopkg/bin:"+os.Getenv("PATH")) os.Setenv("GOPRIVATE", "git.snorba.art") + os.Setenv("EMACS_SOCKET_NAME", home+"/.cache/emacs/server") } -func DumpStringToFile(data string, path string) error { - file, err := os.Create(path) +func DumpBytesToFile(data []byte, filePath string) error { + fmt.Fprintln(os.Stderr, "Dumping to "+filePath) + + err := EnsureDirectory(path.Dir(filePath)) + if err != nil { + return err + } + + file, err := os.Create(filePath) if err != nil { return err } writer := bufio.NewWriter(file) - _, err = writer.WriteString(data) + _, err = writer.Write(data) if err != nil { return err } @@ -334,6 +351,10 @@ func DumpStringToFile(data string, path string) error { return writer.Flush() } +func DumpStringToFile(data string, path string) error { + return DumpBytesToFile([]byte(data), path) +} + func ToggleNightLight() error { getState := exec.Command( "gsettings", @@ -368,22 +389,26 @@ func ToggleNightLight() error { return nil } -func DumpInitFiles() error { - home := os.Getenv("HOME") - elispThemeDir := home + "/.workspace/elisp-themes" - - if _, err := os.Stat(elispThemeDir); os.IsNotExist(err) { - err = os.MkdirAll(elispThemeDir, 0755) +func EnsureDirectory(dir string) error { + if _, err := os.Stat(dir); os.IsNotExist(err) { + err = os.MkdirAll(dir, 0755) if err != nil { return fmt.Errorf( - "Failed to create elisp theme dir in %v: %w", - elispThemeDir, + "Failed to create dir %v: %w", + dir, err, ) } - } + return nil +} + +func DumpInitFiles() error { + home := os.Getenv("HOME") + elispThemeDir := home + "/.workspace/elisp-themes" + err := EnsureDirectory(elispThemeDir) + files := map[string]string{ home + "/.emacs": initLisp, home + "/.custom.el": customLisp, @@ -394,14 +419,39 @@ func DumpInitFiles() error { } for file, contents := range files { - fmt.Fprintln(os.Stderr, "Dumping to "+file) err := DumpStringToFile(contents, file) if err != nil { return err } } - return nil + tmp, err := template.New("emacs.desktop").Parse(emacsDesktop) + if err != nil { + return err + } + + executable, err := os.Executable() + if err != nil { + return err + } + + var out bytes.Buffer + + tmp.Execute(&out, map[string]string{ + "workspace_bin": executable, + }) + + err = DumpBytesToFile( + out.Bytes(), home+"/.local/share/applications/emacs.desktop", + ) + if err != nil { + return err + } + + return DumpBytesToFile( + emacsIcon, + home+"/.local/share/icons/hicolor/128x128/apps/emacs.png", + ) } func main() {