Source

xwem / lisp / xwem-launcher.el

  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
;;; xwem-launcher.el --- Program launcher for XWEM.

;; Copyright (C) 2003-2005 by XWEM Org.

;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; Created: Thu Dec  4 16:32:11 MSK 2003
;; Keywords: xwem, xlib
;; X-CVS: $Id$

;; This file is part of XWEM.

;; XWEM 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 2, or (at your option)
;; any later version.

;; XWEM 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 XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: Not in FSF

;;; Commentary:

;; External programs launcher for XWEM.

;;; TODO:
;;    * Assume argument in quotas as single argument, i.e. do not make
;;      splitting inside quotas.

;;; Code:

(eval-and-compile
  (require 'completer)
  (require 'comint)
  (autoload 'executable-find "executable")
  )

(require 'xlib-xlib)
(require 'xlib-tray)
(require 'xlib-xpm)

(require 'xwem-load)

(defgroup xwem-launcher nil
  "Group to customize XWEM's programs launcher."
  :prefix "xwem-"
  :group 'xwem)

(defcustom xwem-launcher-beep-done nil
  "*Non-nil mean beep when execution of program done.
Beep performed using \(xwem-play-sound 'ready\)."
  :type 'boolean
  :group 'xwem-launcher)

(defcustom xwem-launcher-function 'xwem-execute-program
  "Function to be used to execute external program."
  :type 'function
  :group 'xwem-launcher)

(defcustom xwem-launcher-abbrev-table nil
  "Abbrev table used by `xwem-launcher-query'."
  :type 'list
  :group 'xwem-launcher)

(defcustom xwem-launcher-history nil
  "History of `xwem-launcher-query'ies."
  :type 'list
  :group 'xwem-launcher)

(defcustom xwem-launcher-split-type 'xwem-execute-program-other-win-vertical
  "Window split type, when launching program in other window."
  :type '(choice
          (const :tag "Horizontal" xwem-execute-program-other-win-horizontal)
          (const :tag "Vertical" xwem-execute-program-other-win-vertical))
  :group 'xwem-launcher)

(defcustom xwem-launcher-frame-type 'xwem-execute-program-other-frame
  "Type of frame, when launching program in other frame."
  :type '(choice
          (const :tag "Normal frame" xwem-execute-program-other-frame)
          (const :tag "Embedded frame" xwem-execute-program-embedded-frame))
  :group 'xwem-launcher)

(defcustom xwem-xterm-program "xterm"
  "Name of terminal emulator program."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xterm-font-argument "-fn"
  "Program key to specify in order to change font."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xterm-font1 ""
  "Default xterm font."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xterm-font2 "10x20"
  "Second xterm font."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xterm-font3 "9x15"
  "Third xterm font."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xterm-font4 "fixed"
  "Forth xterm font."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-lupe-program "lupe"
  "Xmag like mignifier program."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-lupe-arguments "-noshape -nohud"
  "Argument to pass to `xwem-lupe-program'."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xlock-program "xlock"
  "Program to run in order to lock X display."
  :type 'string
  :group 'xwem-launcher)

(defcustom xwem-xlock-arguments "-mode image"
  "Arguments to supply `xwem-xlock-program'."
  :type 'string
  :group 'xwem-launcher)

;;; Launcher dockapp button
(defcustom xwem-launch-dock-width 24
  "*Launcher dockapp width."
  :type 'number
  :group 'xwem-launcher)

(defcustom xwem-launch-dock-height 24
  "*Launcher dockapp height."
  :type 'number
  :group 'xwem-launcher)

(defcustom xwem-launch-dock-thick 2
  "*Launcher dockapp border thickness."
  :type 'number
  :group 'xwem-launcher)

;;; Internal variables

(defvar xwem-launcher-shell-completion-table nil
  "*Completion table in used by `xwem-completer'.")

;; Executing program
(defun xwem-launcher-build-shell-completion-table (&optional rehash)
  "Scan `exec-path' to build completion table of executables."
  (if (and xwem-launcher-shell-completion-table (not rehash))
      xwem-launcher-shell-completion-table

    (setq xwem-launcher-shell-completion-table
          (apply 'nconc
                 (mapcar #'(lambda (dir)
                             (mapcar 'list (directory-files dir nil nil t t)))
                         exec-path)))))

(defun xwem-launcher-shell-complete (&optional undo mode)
  "Complete the previous command or display possibilities if done
twice in a row.  If called with a prefix, undo the last completion."
  (interactive "P")
  (if undo
      (completer-undo)

    (setq completer-string nil)
    (if (string-match ".*[ \t].*" (buffer-substring (point-min) (point-max)))
        (comint-dynamic-complete-filename)

      ;; Complete application
      (completer-complete-goto "^ \t\n\"" completer-words
                               (xwem-launcher-build-shell-completion-table)
                               #'(lambda (w)
                                   (executable-find (car w)))))))

(defun xwem-launcher-read-command (prompt &optional initial-input)
  "Read command name prompting PROMPT.
Just linke `read-shell-command', but installs
`xwem-launcher-abbrev-table' as abbrev table, `xwem-launcher-history'
as history of commands and `xwem-launcher-shell-completion-table' as table
to performe completions.
INITIAL-INPUT is self-described."
  (let ((minibuffer-completion-table nil)
        (keymap (copy-keymap read-shell-command-map)))
    (xwem-unwind-protect
        (progn
          (define-key read-shell-command-map
            (kbd "TAB") 'xwem-launcher-shell-complete)
          (xwem-read-from-minibuffer
           prompt initial-input read-shell-command-map
           nil 'xwem-launcher-history xwem-launcher-abbrev-table))
      (setq read-shell-command-map keymap))))

(defun xwem-launcher-parse-arguments (cmd-str &optional keep-quotes-p)
  "Parse CMD-STR by spliting it to arguments list.
If optional KEEP-QUOTES-P is non-nil, quotes in quoted arguments are kept."
  (let ((scmd (split-string cmd-str " "))
        qarg args cmd)
    (while scmd
      (if (string-match "^['\"]" (car scmd))
          (setq qarg (car scmd))
        (when (stringp qarg)
          (setq qarg (concat qarg " " (car scmd)))))
      (if (and (stringp qarg)
               (string-match "['\"]$" (car scmd)))
          (setq args (cons (if keep-quotes-p
                               qarg
                             (substring qarg 1 (1- (length qarg))))
                           args)
                qarg nil)
        (setq args (cons (car scmd) args)))
      (setq scmd (cdr scmd)))
    ;; Pre-normalise command and its arguments
    (setq args (remove "" (nreverse args))
          cmd (executable-find (car args)))
    (when cmd
      (cons cmd (cdr args)))))

(defun xwem-launcher-normalize-cmd (cmd)
  "Normalize command CMD string.
Return normalized command string, or signal error if CMD can't be
normalized."
  (let ((pargs (xwem-launcher-parse-arguments cmd t)))
    (unless pargs
      (error 'xwem-error (format "Can't normalize command: %S" cmd)))
    (mapconcat #'identity pargs " ")))

;;;###xwem-autoload
(defun xwem-launcher-query (&optional prompt)
  "Query for command to launch using PROMPT."
  (let ((cmd (xwem-launcher-read-command (or prompt "XWEM-Run: "))))
    (xwem-launcher-normalize-cmd cmd)))

;;;###autoload(autoload 'xwem-run-program "xwem-launcher" "" t)
(define-xwem-command xwem-run-program (command)
  "Run COMMAND."
  (xwem-interactive "eRun command: ")
  (xwem-execute-program command))

(defun xwem-next-job-number ()
  "Return next job number for use by xwem."
  (let ((job-number 1))
    (while (get-process (format "xwem-run-%d" job-number))
      (setq job-number (1+ job-number)))
    job-number))
  
;;;###autoload
(defun xwem-execute-program (command &optional buffer-name)
  "Execute COMMAND in buffer with BUFFER-NAME.
Unlike `background' do not use shell."
  (let* ((cmdargs
          ;; Do it under `condition-case', due to split-string
          ;; args-out-of-range bug.
          (condition-case nil
              (xwem-launcher-parse-arguments command)
            (t (list command))))
         (prg (car cmdargs))
         (args (cdr cmdargs))
         (emacs-env (getenv "EMACS"))
         (job-number (xwem-next-job-number))
         (job-name (format "xwem-run-%d" job-number))
         proc)

    (unless buffer-name
      (setq buffer-name (format " *%s*" job-name)))

    (with-current-buffer (get-buffer-create buffer-name)
      ;; Sometimes `default-directory' became nil for some reason, I
      ;; don't know why.  Maybe XEmacs bug? --lg
      (unless default-directory
        (setq default-directory (expand-file-name "~/")))

      (erase-buffer)
      (insert (format "--- Working directory: %S\n%% %S\n"
                      default-directory command))

      ;; Set our EMACS environment variable so comint-exec doesn't do it
      ;; for us.  Note that if the environment is already set, we may
      ;; not want to do it again.
      (unless emacs-env
        (setenv "EMACS" "xwem"))

      (setq proc (get-buffer-process
                  (comint-exec buffer-name job-name prg nil args)))
      (comint-mode)
      ;; COND because the proc may have died before the G-B-P is called.
      (cond (proc (set-process-sentinel proc 'xwem-program-sentinel)
                  (xwem-message 'note "Job [%d] '%s' PID=%d"
                                job-number command (process-id proc))))
      (setq mode-name "XWEM-Run")

      ;; Restore our Emacs environment variable to its previous state.
      (setenv "EMACS" emacs-env)

      proc)))

(defun xwem-program-sentinel (process msg)
  "Called when PROCESS changed state to MSG."
  (let ((ms (match-data))) ; barf
    (unwind-protect
         (let ((msg (cond ((string= msg "finished\n") "Done")
                          ((string-match "^exited" msg)
                           (concat "Exit " (substring msg 28 -1)))
                          ((zerop (length msg)) "Continuing")
                          (t (concat (upcase (substring msg 0 1))
                                     (substring msg 1 -1))))))
           (when xwem-launcher-beep-done
             (xwem-play-sound 'ready))
           (xwem-message 'note "Job [%s] %s '%s'" (process-name process)
                         msg (mapconcat #'identity
                                        (process-command process) " "))
           (if (null (buffer-name (process-buffer process)))
               (set-process-buffer process nil) ; WHY? Olin.

             (if (memq (process-status process) '(signal exit))
                 (with-current-buffer (process-buffer process)
                   (let ((at-end (eobp)))
                     (save-excursion
                       (goto-char (point-max))
                       (insert ?\n msg ?\x20
                               (substring (current-time-string) 11 19) ?\n))
                     (if at-end (goto-char (point-max))))
                   (set-buffer-modified-p nil)))))
      (store-match-data ms))))

;;;###xwem-autoload
(defun xwem-execute-program-expecting (cmd &optional manage-type cl-plist non-block)
  "Execute CMD expecting till client will be managed.
MANAGE-TYPE specifies which manage type to use in expectance.
CL-PLIST is properties for expected client.
Return managed client unless NON-BLOCK is non-nil."
  (setq cmd (xwem-launcher-normalize-cmd cmd))

  (let* ((job-num (xwem-next-job-number))
         (expt (list manage-type
                     (nconc (list 'job-num job-num) cl-plist)
                     '(eval t)))
         ccls)
    (xwem-manda-add-expectance expt)
    (xwem-execute-program cmd)
    (unless non-block
      (while (memq expt xwem-manage-expectances)
        (dispatch-event (next-event)))
    
      ;; Find a client
      (setq ccls xwem-clients)
      (while (and ccls (not (= job-num
                               (or (xwem-client-property (car ccls) 'job-num) -1))))
        (setq ccls (cdr ccls)))
      (when (car ccls)
        (xwem-client-set-property (car ccls) 'executed-command cmd))
      (car ccls))))

(defun xwem-execute-program-other-win (cmd type &optional select-p)
  "Execute CMD in other XWEM window, making TYPE split if needed.
TYPE is one of 'horizontal of 'vertical.
If SELECT-P is non-nil - select newly created client.
Return newly created client."
  (let ((own (xwem-window-other 1))
        cl)
    ;; Check is there split needed
    (when (eq own (xwem-win-selected))
      (if (eq type 'horizontal)
          (xwem-window-split-horizontally 0)
        (xwem-window-split-vertically 0))
      (setq own (xwem-win-next (xwem-win-selected))))

    (setq cl (xwem-execute-program-expecting
              cmd nil `(expect-win ,(xwem-win-id own))))
    (when select-p
      (xwem-select-client cl))
    cl))

(defun xwem-execute-program-other-win-horizontal (cmd &optional select-p)
  "Execute CMD in other XWEM window, making horizontal split if needed."
  (xwem-execute-program-other-win cmd 'horizontal select-p))

(defun xwem-execute-program-other-win-vertical (cmd &optional select-p)
  "Execute CMD in other XWEM window, making vertical split if needed."
  (xwem-execute-program-other-win cmd 'vertical select-p))

(defun xwem-execute-program-other-frame (cmd &optional select-p)
  "Execute CMD in other XWEM frame.
If SELECT-P is non-nil select newly managed client."
  (let* ((oframe (or (xwem-frame-other (xwem-frame-selected))
                     (xwem-make-frame-1 'desktop :noselect t)))
         (cl (xwem-execute-program-expecting
              cmd nil `(expect-win ,(xwem-win-id (xwem-frame-selwin oframe))))))
    (when select-p
      (xwem-select-client cl))
    cl))

(defun xwem-execute-program-embedded-frame (cmd &optional select-p)
  "Execute CMD in linkaged frame or in embedded XWEM frame."
  (let* ((oframe (or (xwem-frame-other (xwem-frame-selected) 'linkage)
                     (xwem-make-frame-1 'embedded :noselect t)))
         (cl (xwem-execute-program-expecting
              cmd nil `(expect-win ,(xwem-win-id (xwem-frame-selwin oframe))))))
    (when select-p
      (xwem-select-client cl))
    cl))

;;;; XWEM commands.

;;;###autoload(autoload 'xwem-launcher-turn-on-horizontal-split-type "xwem-launcher" "" t)
(define-xwem-command xwem-launcher-turn-on-horizontal-split-type ()
  "Set `xwem-launcher-split-type' to `xwem-execute-program-other-win-horizontal'."
  (xwem-interactive)
  
  (setq xwem-launcher-split-type 'xwem-execute-program-other-win-horizontal)
  (xwem-message 'info "Launcher split type HORIZONTAL on."))

;;;###autoload(autoload 'xwem-launcher-turn-on-vertical-split-type "xwem-launcher" "" t)
(define-xwem-command xwem-launcher-turn-on-vertical-split-type ()
  "Set `xwem-launcher-split-type' to `xwem-execute-program-other-win-vertical'."
  (xwem-interactive)

  (setq xwem-launcher-split-type 'xwem-execute-program-other-win-vertical)
  (xwem-message 'info "Launcher split type VERTICAL on."))

;;;###autoload(autoload 'xwem-launcher-toggle-split-type "xwem-launcher" "" t)
(define-xwem-command xwem-launcher-toggle-split-type (arg)
  "Toggle `xwem-launcher-split-type'.
Without prefix ARG set it to vertical, with prefix ARG set it to horizontal."
  (xwem-interactive "P")
  
  (if arg
      (xwem-launcher-turn-on-horizontal-split-type)
    (xwem-launcher-turn-on-vertical-split-type)))

;;;###autoload(autoload 'xwem-launcher-turn-on-normal-frame-type "xwem-launcher" "" t)
(define-xwem-command xwem-launcher-turn-on-normal-frame-type ()
  "Set `xwem-launcher-frame-type' to `xwem-execute-program-other-frame', aka normal frame type."
  (xwem-interactive)
  
  (setq xwem-launcher-frame-type 'xwem-execute-program-other-frame)
  (xwem-message 'info "Other frame NORMAL type on."))

;;;###autoload(autoload 'xwem-launcher-turn-on-embedded-frame-type "xwem-launcher" "" t)
(define-xwem-command xwem-launcher-turn-on-embedded-frame-type ()
  "Set `xwem-launcher-frame-type' to `xwem-execute-program-embedded-frame' aka embedded frame type."
  (xwem-interactive)

  (setq xwem-launcher-frame-type 'xwem-execute-program-embedded-frame)
  (xwem-message 'info "Other frame EMBEDDED type on."))

;;;###autoload(autoload 'xwem-launcher-toggle-frame-type "xwem-launcher" "" t)
(define-xwem-command xwem-launcher-toggle-frame-type (arg)
  "Toggle `xwem-launcher-frame-type'.
Without prefix ARG set it to normal, with prefix ARG set it to embedded."
  (xwem-interactive "P")
  
  (if arg
      (xwem-launcher-turn-on-normal-frame-type)
    (xwem-launcher-turn-on-embedded-frame-type)))

;;;###autoload(autoload 'xwem-launch-program "xwem-launcher" "" t)
(define-xwem-command xwem-launch-program (cmd &optional arg)
  "Run CMD program in background.
If used with prefix ARG (\\<xwem-global-map>\\[xwem-universal-argument]), installs
expectance on selected window (i.e. when client window performe
MapWindow, it will be managed to window where expectance setuped
regardless selected window at map moment.  It is usefull to do so when
you start application with large start time, such as Mozilla or
AcrobatReader, and you want continue doing things not wainting untill
application window maps.
If used with numeric prefix ARG, then that number specifies how many
programs to run."
  (xwem-interactive "_eLaunch program: \nP")

  (cond ((listp arg)
         (xwem-execute-program-expecting
          cmd nil `(expect-win ,(xwem-win-id (xwem-win-selected)))))

        ((numberp arg)
         (while (> arg 0)
           (xwem-execute-program cmd)
           (setq arg (1- arg))))
        (t (error 'xwem-error "Invalid arg: %s" arg))))

;;;###autoload(autoload 'xwem-launch-program-other-win "xwem-launcher" "" t)
(define-xwem-command xwem-launch-program-other-win (cmd &optional arg)
  "Run program in other window.
If prefix ARG is specified - select newly created client.
Window spliting (if needed) is controled by `xwem-launcher-split-type'."
  (xwem-interactive "_eLaunch Other Win: \nP")

  (funcall xwem-launcher-split-type cmd arg))

;;;###autoload(autoload 'xwem-launch-program-other-frame "xwem-launcher" "" t)
(define-xwem-command xwem-launch-program-other-frame (cmd &optional arg)
  "Run programm in other frame.
If prefix ARG is specified - select newly created client."
  (xwem-interactive "_eLaunch Other Frame: \nP")
  
  (funcall xwem-launcher-frame-type cmd arg))

(defsubst xwem-launch (cmd)
  "Execute CMD using `xwem-launcher'."
  (funcall xwem-launcher-function cmd))

;;;###autoload
(defun xwem-launch-generic-program (cmd sarg)
  "Run generic program CMD with arguments SARG."
  (xwem-launch (mapconcat #'identity (list cmd sarg) " ")))

;; Executing xterm
(defun xwem-xterm-construct-cmd (arg)
  "Construct xterm command according to ARG."
  (let ((fn (symbol-value
             (intern-soft (concat "xwem-xterm-font" (int-to-string arg)))))
        (cmd xwem-xterm-program))

    (when (and (stringp fn) (> (length fn) 0))
      (setq cmd (concat cmd " " xwem-xterm-font-argument " " fn)))
    cmd))

;;;###autoload(autoload 'xwem-launch-xterm "xwem-launcher" "" t)
(define-xwem-command xwem-launch-xterm (arg)
  "Run xterm program `xwem-xterm-program' with ARG as font argument."
  (xwem-interactive "p")

  (let ((cmd (xwem-xterm-construct-cmd arg)))
    (xwem-launch cmd)))

;;;###autoload(autoload 'xwem-launch-xterm-other-win "xwem-launcher" "" t)
(define-xwem-command xwem-launch-xterm-other-win (arg)
  "Execute xterm in other window, ARG have same meaning as in `xwem-launch-xterm'."
  (xwem-interactive "p")

  (let ((xwem-launcher-function xwem-launcher-split-type))
    (xwem-launch-xterm arg)))

;;;###autoload(autoload 'xwem-launch-xterm-other-frame "xwem-launcher" "" t)
(define-xwem-command xwem-launch-xterm-other-frame (arg)
  "Execute xterm in other frame, ARG have same meaning as in `xwem-launch-xterm'."
  (xwem-interactive "p")

  (let ((xwem-launcher-function xwem-launcher-frame-type))
    (xwem-launch-xterm arg)))

;; Executing lupe
;;;###autoload(autoload 'xwem-launch-lupe "xwem-launcher" "" t)
(define-xwem-command xwem-launch-lupe (arg)
  "Run lupe `xwem-lupe-program' with `xwem-lupe-arguments'.
Prefix ARG is actually unused."
  (xwem-interactive "P")

  (xwem-launch-generic-program xwem-lupe-program xwem-lupe-arguments))

;;;###autoload(autoload 'xwem-launch-lupe-other-win "xwem-launcher" "" t)
(define-xwem-command xwem-launch-lupe-other-win (arg)
  "Run lupe in other window.
Prefix ARG is actually unused."
  (xwem-interactive "P")
  
  (let ((xwem-launcher-function xwem-launcher-split-type))
    (xwem-launch-lupe arg)))

;;;###autoload(autoload 'xwem-launch-lupe-other-frame "xwem-launcher" "" t)
(define-xwem-command xwem-launch-lupe-other-frame (arg)
  "Run lupe in other frame.
Prefix ARG is actually unused."
  (xwem-interactive "P")

  (let ((xwem-launcher-function xwem-launcher-frame-type))
    (xwem-launch-lupe arg)))

;;;###autoload(autoload 'xwem-launch-xlock "xwem-launcher" "" t)
(define-xwem-command xwem-launch-xlock ()
  "Launch `xwem-xlock-program' with `xwem-xlock-arguments'."
  (xwem-interactive)

  (xwem-launch-generic-program xwem-xlock-program xwem-xlock-arguments))

;;;; Launcher dockapp
(define-xwem-face xwem-launch-dock-face
  `(((medium) (:foreground "gray70"))
    ((light) (:foreground "white"))
    ((dark) (:foreground "black"))
    (t (:foreground "gray70" :background "black")))
  "Default background face for launcher docks."
  :group 'xwem-launcher
  :group 'xwem-faces)

(defvar xwem-launch-dock-keymap
  (let ((map (make-sparse-keymap)))
    (define-key map [button1] 'xwem-launch-dock-down)
    (define-key map [button1up] 'xwem-launch-dock-launch)
    (define-key map [button3] 'xwem-launch-dock-menu)
    map)
  "Keymap for launch docks.")

;; Macroses to access ladock internals
(defmacro xwem-ladock-state (win)
  `(X-Win-get-prop ,win 'ladock-state))
(defsetf xwem-ladock-state (win) (state)
  `(X-Win-put-prop ,win 'ladock-state ,state))
(defmacro xwem-ladock-action (win)
  `(X-Win-get-prop ,win 'ladock-action))
(defsetf xwem-ladock-action (win) (action)
  `(X-Win-put-prop ,win 'ladock-action ,action))
(defmacro xwem-ladock-pixmap (win)
  `(X-Win-get-prop ,win 'ladock-pixmap))
(defsetf xwem-ladock-pixmap (win) (action)
  `(X-Win-put-prop ,win 'ladock-pixmap ,action))

;;;###autoload(autoload 'xwem-launch-dock-down "xwem-launcher" "" t)
(define-xwem-command xwem-launch-dock-down (ev)
  "Default command when button is down."
  (xwem-interactive (list xwem-last-event))
  (unless (button-event-p ev)
    (error 'xwem-error "`xwem-launch-dock-down' must be bound to mouse event"))

  ;; Push the button
  (setf (xwem-ladock-state (X-Event-win xwem-last-xevent)) 'down)
  (xwem-ladock-redraw (X-Event-win xwem-last-xevent)))

;;;###autoload(autoload 'xwem-launch-dock-launch "xwem-launcher" "" t)
(define-xwem-command xwem-launch-dock-launch (ev)
  "Launch command for launch dock."
  (xwem-interactive (list xwem-last-event))
  (unless (button-event-p ev)
    (error 'xwem-error "`xwem-launch-dock-up' must be bound to mouse event"))

  ;; Pop the button
  (setf (xwem-ladock-state (X-Event-win xwem-last-xevent)) 'up)
  (xwem-ladock-redraw (X-Event-win xwem-last-xevent))

  (when (and (< (X-Event-xbutton-event-x xwem-last-xevent)
                xwem-launch-dock-width)
             (< (X-Event-xbutton-event-y xwem-last-xevent)
                xwem-launch-dock-height))
    ;; React on ButtonRelease only if it released within ladock
    ;; window.
    (let* ((action (xwem-ladock-action (X-Event-win xwem-last-xevent)))
           (atype (car action))
           (acmd (cdr action)))
      (cond ((eq atype 'elisp) (eval (read acmd)))
            ((eq atype 'cmd) (xwem-execute-program acmd))
            (t (xwem-message 'warning "Unknown action type=`%s'" atype))))))

;;;###autoload(autoload 'xwem-launch-dock-menu "xwem-launcher" "" t)
(define-xwem-command xwem-launch-dock-menu (action)
  "Popup menu."
  (xwem-interactive (list (xwem-ladock-action (X-Event-win xwem-last-xevent))))

  (let ((cls (delq nil (mapcar #'(lambda (cl)
                                   (and (string-match (cdr action)
                                                      (xwem-cl-wm-command cl))
                                        cl))
                               xwem-clients)))
        menu)

    ;; Sort clients
    (setq cls (sort cls #'(lambda (cl1 cl2)
                            (and (xwem-frame-p (xwem-cl-frame cl1))
                                 (xwem-frame-p (xwem-cl-frame cl2))
                                 (> (xwem-frame-num (xwem-cl-frame cl1))
                                    (xwem-frame-num (xwem-cl-frame cl2)))))))
    
    (setq menu
          (list "Clients" :filter
                #'(lambda (not-used)
                    (nconc
                     (mapcar #'(lambda (cl)
                                 (let ((frame (xwem-cl-frame cl))
                                       (name (xwem-cl-wm-name cl)))
                                   (vector
                                    (if (xwem-frame-p frame)
                                        (format "[%d](%s): %s"
                                                (xwem-frame-num (xwem-cl-frame cl))
                                                (xwem-frame-name (xwem-cl-frame cl))
                                                name)
                                      name)
                                    `(xwem-cl-pop-to-client ,cl)
                                    :active (xwem-non-dummy-client-p cl))))
                             cls)
                     (list
                      "---"
                      (vector "Destroy"
                              `(xwem-launch-button-stop
                                ,(X-Event-win xwem-last-xevent) t)))))))
    (xwem-popup-menu menu)))

(define-xwem-deffered xwem-ladock-redraw (xwin)
  "Redraw launcher button dock XWIN."
  (xwem-misc-draw-shadow
   (X-Win-dpy xwin) (xwem-ladock-pixmap xwin)
   (xwem-face-get-gc 'xwem-launch-dock-face
     (if (eq (xwem-ladock-state xwin) 'down) '(dark) '(light)))
   (xwem-face-get-gc 'xwem-launch-dock-face
     (if (eq (xwem-ladock-state xwin) 'down) '(light) '(dark)))
   0 0 xwem-launch-dock-width xwem-launch-dock-height xwem-launch-dock-thick)

  (XCopyArea (xwem-dpy) (xwem-ladock-pixmap xwin) xwin
             (XDefaultGC (xwem-dpy)) 0 0
             xwem-launch-dock-width xwem-launch-dock-height 0 0))

(defun xwem-ladock-evhandler (xdpy xwin xev)
  "On XDPY and launcher dock XWIN handle event XEV."
  (X-Event-CASE xev
    ((:X-Expose :X-MapNotify)
     (xwem-ladock-redraw xwin))

    ((:X-ButtonPress :X-ButtonRelease)
     (xwem-overriding-local-map xwem-launch-dock-keymap
       (xwem-dispatch-command-xevent xev)))

    (:X-DestroyNotify
     (xwem-launch-button-stop
      (X-Event-xdestroywindow-window xev)))))

(defun xwem-launch-button-stop (xwin &optional force)
  "Destoy launch dockapp button XWIN.
If FORCE is non-nil also destroy XWIN."
  (XFreePixmap (X-Win-dpy xwin) (xwem-ladock-pixmap xwin))
  
  (setf (xwem-ladock-pixmap xwin) nil
        (xwem-ladock-state xwin)nil
        (xwem-ladock-action xwin)nil)

  ;; Remove events handler
  (X-Win-EventHandler-rem xwin 'xwem-ladock-evhandler)

  (when force
    (XDestroyWindow (X-Win-dpy xwin) xwin)))

;;;###autoload
(defun xwem-launch-button-start (xpm-file action &optional dockid
                                          dockgroup dockalign)
  "Create new dockapp button with XPM-FILE image and doing ACTION on click.
ACTION is cons cell wher car is one of 'elisp or 'cmd and cdr is string.
For 'elisp car, cdr is real elisp expression, to evaluate on click.
For 'cmd  car, cdr is cmd to run on click."
  (let ((xwin (XCreateWindow (xwem-dpy) nil 0 0
                             xwem-launch-dock-width
                             xwem-launch-dock-height
                             0 nil nil nil
                             (make-X-Attr
                              :event-mask (Xmask-or XM-Exposure XM-StructureNotify
                                                    XM-ButtonPress XM-ButtonRelease)
                              :override-redirect t))))

    ;; Create pixmap
    (setf (xwem-ladock-pixmap xwin)
          (XCreatePixmap
           (xwem-dpy) (make-X-Pixmap :dpy (xwem-dpy)
                                     :id (X-Dpy-get-id (xwem-dpy)))
           xwin (XDefaultDepth (xwem-dpy))
           xwem-launch-dock-width xwem-launch-dock-height)
          (xwem-ladock-state xwin) 'up
          (xwem-ladock-action xwin) action)

    ;; Initial pixmaps setup
    (XFillRectangle (xwem-dpy) (xwem-ladock-pixmap xwin)
                    (xwem-face-get-gc 'xwem-launch-dock-face '(medium)) 0 0
                    (X-Pixmap-width (xwem-ladock-pixmap xwin))
                    (X-Pixmap-height (xwem-ladock-pixmap xwin)))
    (let* ((ipix (X:xpm-pixmap-from-file
                  (xwem-dpy) xwin (expand-file-name xpm-file xwem-icons-dir)))
           (imask (X:xpm-pixmap-from-file
                   (xwem-dpy) xwin (expand-file-name xpm-file xwem-icons-dir) t))
           (x-orig (/ (- xwem-launch-dock-width (X-Pixmap-width ipix)) 2))
           (y-orig (/ (- xwem-launch-dock-height (X-Pixmap-height ipix)) 2))
           (clgc (XCreateGC
                  (xwem-dpy) ipix
                  (make-X-Gc :dpy (xwem-dpy)
                             :id (X-Dpy-get-id (xwem-dpy))
                             :clip-x-origin x-orig
                             :clip-y-origin y-orig
                             :clip-mask imask))))
      ;; Copy to our pixmap
      (XCopyArea (xwem-dpy) ipix (xwem-ladock-pixmap xwin) clgc
                 0 0 (X-Pixmap-width ipix) (X-Pixmap-height ipix)
                 x-orig y-orig)
      ;; Release resources
      (XFreeGC (xwem-dpy) clgc)
      (XFreePixmap (xwem-dpy) imask)
      (XFreePixmap (xwem-dpy) ipix))

    (when xwem-misc-turbo-mode
      (XSetWindowBackgroundPixmap (xwem-dpy) xwin (xwem-ladock-pixmap xwin)))

    ;; Install events handler
    (X-Win-EventHandler-add xwin 'xwem-ladock-evhandler nil
                            (list X-Expose X-MapNotify X-ButtonPress
                                  X-ButtonRelease X-DestroyNotify))

    (xwem-XTrayInit (xwem-dpy) xwin dockid dockgroup dockalign)
    xwin))


;;;; Open file
;;;###autoload
(defcustom xwem-open-file-commands-alist
  '(("\\.\\(ps\\|ps_pages\\|eps\\)\\'" . "gv")
    ("\\.pdf\\'" . "xpdf")
    ("\\.\\(jpe?g\\|gif\\|png\\)\\'" . "display")
    ("\\.dvi\\'" . "xdvi")
    ("\\.chm\\'" . "xchm")
    ("\\.djvu\\'" . "djview")
    ("\\.txt\\'" . "xterm -e less"))
  "*Alist specifying how to view special types of files.
`xwem-open-file-commands-alist' can be dynamically changed by
`xwemw-open-file' when registering new file extensions, so we
recommend to use `xwem-desktop' package to save/restore
`xwem-open-file-commands-alist' value between sessions."
  :group 'xwem-launcher
  :type '(repeat (cons (string :tag "Regexp")
                       (string :tag "Command"))))

(defcustom xwem-open-file-registration 'query
  "*Defines the behaviour of `xwem-open-file' when file extension is
not defined in `xwem-open-file-commands-alist'.

 nil - Do nothing.
 t - Register new extensions in `xwem-open-file-commands-alist'.
 query - Query about registration."
  :group 'xwem-launcher
  :type '(choice (const :tag "No registration" nil)
                 (const :tag "Auto registration" t)
                 (const :tag "Query for registration" query)))

;;;###xwem-autoload
(defun xwem-file-find-command (filename)
  "Find appropriate command to open FILENAME file."
  (let ((cmds xwem-open-file-commands-alist))
    (while (and cmds (not (string-match (caar cmds) filename)))
      (setq cmds (cdr cmds)))
    (cdr (car cmds))))

;;;###autoload(autoload 'xwem-open-file "xwem-launcher" "Open file with appopriate command" t)
(define-xwem-command xwem-open-file (file &optional command)
  "Open FILE with command specified by COMMAND.
If prefix arg is specified, expicitely query for the COMMAND."
  (xwem-interactive
   (let* ((file (expand-file-name (xwem-read-filename "Find File: ")))
          (cmd (xwem-file-find-command file)))
     (when (or xwem-prefix-arg
               (not cmd))
       (setq cmd (xwem-read-external-command "Command: "))
       ;; Try to register FILE extension in
       ;; `xwem-open-file-commands-alist'.
       (when (and (not xwem-prefix-arg)
                  xwem-open-file-registration)
         (let ((fext (and (string-match "\\.\\([^.]+\\)\\'" file)
                          (match-string 1 file))))
           (when (and fext
                      (or (eq xwem-open-file-registration t)
                          (xwem-under-minibuffer 
                           (y-or-n-p (format "Register '%s' extension?: "
                                             fext)))))
             (push (cons (concat "\\." fext "\\'") cmd)
                   xwem-open-file-commands-alist)))))
       (list file cmd)))

  ;; Fixate FILE in case `xwem-open-file' called non-interactively
  (setq file (expand-file-name file))
  (unless command
    (setq command (xwem-file-find-command file)))

  (xwem-launch (format "%s '%s'" command file)))


(provide 'xwem-launcher)

;;; xwem-launcher.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.