i3 fix, other improvements
[distro-setup] / g
diff --git a/g b/g
index 199b8ade6d4fe0d29eb9ca4ab28492b344d15561..d559b8a80fa4e1104a3a45ad652bd0d7bc57abf7 100755 (executable)
--- a/g
+++ b/g
 # -a = alternate editor, empty string makes it start emacs daemon.
 # This is also set via env variable, but that doesn't propagate everywhere.
 
-# emacs will hold open the directory it's started in, which
-# will prevent unmounting it.
-if [[ ! $1 ]]; then
-    cd /
-fi
-
-if pgrep -u $EUID emacsclient && (( $# )); then
-    emacsclient -a "" -n "$@"
-else
-    emacsclient -a "" -nc "$@"
-fi
+
+g() {
+
+  local args gdb=false
+
+  if [[ $DISPLAY ]]; then
+    args=-n
+  fi
+
+  if (( $# == 0 )); then
+    args+=" -c"
+  fi
+  # duplicate -c, but oh well
+  if ! pgrep -u $EUID emacsclient &>/dev/null; then
+    if (( $# == 0 )) && type -p gdb &>/dev/null; then
+      gdb=true
+    else
+      args+=" -c"
+    fi
+  fi
+  if $gdb; then
+    # due to a bug, we cant debug from the start unless we get a new gdb
+    # https://sourceware.org/bugzilla/show_bug.cgi?id=24454
+    # m gdb -ex="set follow-fork-mode child" -ex=r -ex=quit --args emacs --daemon
+    emacsclient -a "" $args "$@"
+    sleep 1
+    cd "/a/opt/emacs-$(distro-name)$(distro-num)"
+    sudo gdb -p "$(pgrep -f 'emacs --daemon')" -ex c
+    cd -
+  else
+    emacsclient -a "" $args "$@"
+  fi
+}
+
+g "$@"