2

I use emacs and have fill commands set to Alt-Tab in certain modes and, as a creature of habit, would like to keep it that way.

Is there an easy way to disable application switching via Alt-Tab when emacs is open?

Zanna
  • 70,465
Dan
  • 123
  • This could be put in a script quite easily for detecting window with particular title / class. Just need to figure out specific gconf/dconf command to set/unset Alt-Tab shortcut – Sergiy Kolodyazhnyy Jun 19 '15 at 20:25

2 Answers2

4

I have a solution. All what you need is, to start this watcher script:

#!/bin/bash

keySwitchApplication="switch-applications"
keySwitchApplicationBackward="switch-applications-backward"

backupSwitchApplications="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplication")"
disableSwitchApplications="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplication" | sed "s/\,*\s*'<Alt>Tab'//")"

backupSwitchApplicationsBackward="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward")"
disableSwitchApplicationsBackward="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward" | sed "s/\,*\s*'<Shift><Alt>Tab'//")"

disabled="0"

while true; do
  isActive=$(wmctrl -lx | awk -v search=$(printf 0x0%x $(xdotool getactivewindow)) -v wm_class="$wm_class" '{ if($1 ~ search && $3 ~ /emacs/) print $3 }')

  if [[ "$isActive" != "" ]]; then
    # echo "active"
    if [[ "$disabled" == "0" ]]; then
      # echo "disable shortcut"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplication" "$disableSwitchApplications"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward" "$disableSwitchApplicationsBackward"
      disabled="1";
    fi
  else
    # echo "not active"
    if [[ "$disabled" == "1" ]]; then
      # echo "enable shortcut"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplication" "$backupSwitchApplications"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward" "$backupSwitchApplicationsBackward"
      disabled="0"
    fi;
  fi;
  sleep 1
done

The script checks in a endless loop the window class emacs and disables/enables Alt+Tab and Shift+Alt+Tab


If anything goes wrong, then you can reset the entry to the default settings:

gsettings reset org.gnome.desktop.wm.keybindings switch-applications
gsettings reset org.gnome.desktop.wm.keybindings switch-applications-backward

In my case:

% gsettings reset org.gnome.desktop.wm.keybindings switch-applications  
% gsettings get org.gnome.desktop.wm.keybindings switch-applications   
['<Super>Tab', '<Alt>Tab']

% gsettings reset org.gnome.desktop.wm.keybindings switch-applications-backward
% gsettings get org.gnome.desktop.wm.keybindings switch-applications-backward  
['<Shift><Super>Tab', '<Shift><Alt>Tab']

Credits

@Serg and his answer How to disable input language switching in terminal
@JacobVlijm for his comments

A.B.
  • 90,397
  • This will repeatedly set the values, which is an unnecessary addition to processor occupation. It only should act on changes in the situation. – Jacob Vlijm Jun 20 '15 at 10:06
  • Absolutely, but it can even be "cheaper" (more efficient), so you won't have to check gsettings on every cycle, by checking the front-window state before the loop, comparing it to "state_2" inside the loop, at the end of the cycle making state_1 = state_2, like here: http://askubuntu.com/a/637334/72216 and here: http://askubuntu.com/a/637325/72216. Subsequently, you won't even need to check gsettings, since if the "quality" of the front window changes, you know you'll have to set gsettings for sure. – Jacob Vlijm Jun 20 '15 at 10:28
  • Forgive me for annoying you once more, and for the "pythonish" way of making an overview, but this https://dl.dropboxusercontent.com/u/1155139/concept is the concept I mean. This way, you can completely leave out the gsettings get section, since you can be pretty sure that if there is a change in the identity of the active window (emacs/no emacs) you will have to act. Leaving out the get section will make the script do practically nothing if there is no change, which will make it nicely "low on juice". The same for the sleep 1 which will dramatically reduce the "fuel consumption". – Jacob Vlijm Jun 20 '15 at 11:24
  • @JacobVlijm Better? =) – A.B. Jun 20 '15 at 12:36
  • 1
    +1 for functionality, but the trick would be better to make the connection not "indirect" (starting with disabled="0" and hope no error will ever occur, making disabled run out of phase). Why not simply start the script before the loop with the check isActive= (as in the link above), then after every cycle make the current state the initial one, to be compared with the second isActive= check. Thanks for all your effort by the way :) – Jacob Vlijm Jun 20 '15 at 13:31
  • @A.B. Thanks so much for your response! I've been using Linux/Ubuntu off and on for years but still feel my big issue when customizing things like this is not knowing where to change settings in current programs. Your response is perfect in that not only does it seem to be well on its way towards solving the problem it also has lead me to reading up on gsettings! :D – Dan Jun 21 '15 at 15:42
  • @A.B. Having said that, is there a reason this wouldn't work on Linux Mint Cinnamon? I realize that this is a bit far afield on an Ubuntu Q&A site, but couldn't resist asking. I installed xdotool and wmctrl packages, since the script was throwing errors, but am still not seeing Alt-Tab disabled when emacs is active. – Dan Jun 21 '15 at 15:45
  • Linux Mint Cinnamon could be a problem. Sorry. :\ – A.B. Jun 21 '15 at 15:46
  • @A.B. No problem at all! I'll figure out what to do there, or ask on the forums. Thanks again! – Dan Jun 21 '15 at 15:47
1

One ugly hackish way comes to mind...

Keyboard setting should be stored in: ~/.config/dconf/user So if you have two files, one where Alt+Tab is system wide enabled, and the other where Alt+Tab is globally disabled, you could theoretically swap them.

Simple script which would:

  1. Backup Alt+Tab Enabled
  2. Copy Disabled Alt+Tab over Enabled Alt+Tab
  3. Run Emacs
  4. After Emacs is closed copy files back as they are supposed to

That script would be used to open/run Emacs

I have no idea if it would work, for safety I would try it in VB first and now I don't have time to try unfortunatelly...

RiddleMeThis
  • 1,076
  • 8
  • 17