jordansissel / xdotool

fake keyboard/mouse input, window management, and more
Other
3.18k stars 316 forks source link

'Regular' expressions aren't very regular or at least aren't documented #172

Closed ghost closed 7 years ago

ghost commented 7 years ago

xdotool is wonderful, but it took me a lot of guesswork to determine how to match a window title exactly. Indeed, I fear I am not fully there yet. For what I have, which is as follows, probably picks out the first window that ends with the string in question (namely, 'KeePass'), whereas what I want is to pick out the first window that both ends and starts with that string.

timeout 30s xdotool search --onlyvisible --limit 1 --name KeePass$ windowminimize

bitozoid commented 7 years ago
$ xdotool search --all --screen 1 --name "^a" | tee /dev/stderr | xargs xdotool getwindowname
14680147
a:2:vim - "vim alfresco.md" 
$ xdotool search --all --screen 1 --name "^a:" | tee /dev/stderr | xargs xdotool getwindowname
There are no windows in the stack
Invalid window '%1'
Usage: getwindowname [window=%1]
If no window is given, %1 is used. See WINDOW STACK in xdotool(1)
$ xdotool search --all --screen 1 --name "^b" | tee /dev/stderr | xargs xdotool getwindowname
14680071
b:1:nvim - "vi alfresco.log.2017-06-28" 
$ xdotool search --all --screen 1 --name "^b:" | tee /dev/stderr | xargs xdotool getwindowname
14680071
b:1:nvim - "vi alfresco.log.2017-06-28" 
lucaswerkmeister commented 7 years ago

For anyone else finding this via Google: xdotool uses case-insensitive POSIX Extended Regular Expressions. The matching happens in xdo_search.c, where check_window_match uses compile_re, which delegates to regcomp with the REG_EXTENDED and REG_ICASE flags.

To resolve this issue, I would suggest explicitly stating in the manpage that extended regular expressions are used, with a pointer to the regex(7) manpage. An option to disable regular expressions and require an exact match would also be convenient, since it’s cumbersome to escape a string to be used in a regular expression in a script.

jordansissel commented 7 years ago

Thanks for this research, @lucaswerkmeister!

I agree it should be documented. I'll merge a patch if someone provides it before I get a chance to work on it.

An option to disable regular expressions and require an exact match would also be convenient

I agree.

lucaswerkmeister commented 7 years ago

Okay, I created PR #182 for the manpage and separate issue #183 for the proposed feature.