aboutsummaryrefslogtreecommitdiffstats
path: root/hlwm-run-or-raise
blob: f4a019692153984d35bb24bb75792ef4cd82a2b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env sh
# -*- mode: scheme; -*-
IFS=" "
exec scsh -s "$0" "$@"
!#
;; hlwm-run-or-raise --- Raise a window or start a new process

;; Copyright (C) 2022  Tom Willemse

;; This program is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation, either version 3 of the License, or (at your option) any later
;; version.

;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.

;; You should have received a copy of the GNU General Public License along with
;; this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Tries to find a window ID for a window with a given class value. If
;; no such window can be found treat the rest of the command line as
;; the command to start.

;; This script depends on both herbstluftwm and wmctrl.

(define (get-current-window-id)
  (let* ((output (run/string (wmctrl -v -a ":ACTIVE:") (= 2 1)))
         (re (rx "Using window: "
                 (submatch "0x" (+ hex-digit))
                 "\n"))
         (match (regexp-search re output)))
    (and match (match:substring match 1))))

(define (pull-element-to-front elt collection)
  (if (string= (car collection) elt)
      collection
      (pull-element-to-front elt (rotate collection))))

(define (rotate lst)
  (append (cdr lst) (list (car lst))))

(define current-window-id
  (get-current-window-id))

(define window-ids
  (run/strings
   (pipe (wmctrl -lx)
         (awk "{ print $1, $3 }")
         (grep ,(string-append "\\." (car command-line-arguments)))
         (awk "{ print $1 }"))))

(if (not (null? window-ids))
    (let ((next-window
           (if (and (> (length window-ids) 1)
                    (member current-window-id window-ids))
               (cadr (pull-element-to-front current-window-id window-ids))
               (car window-ids))))
      (run (hlwm-switch-to-window ,next-window)))
    (exec-epf (,@(cdr command-line-arguments))))