Source

xwem / lisp / xwem-minibuffer.el

Full commit
  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
;;; xwem-minibuffer.el --- XWEM minibuffer support.

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

;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; Created: Thu Dec  4 15:13:12 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:

;; XWEM minibuffer stuff.  XWEM minibuffer is Emacs frame with
;; 'minibuffer property set to 'only.  It is used for various thigs,
;; such as messages displaying, system tray, etc.

;;; Code:

(require 'xwem-load)
(require 'xwem-focus)
(require 'xwem-manage)

(eval-when-compile
  (defvar x-emacs-application-class nil))

;; Customization
(defgroup xwem-minibuffer nil
  "Group to customize XWEM minibuffer."
  :prefix "xwem-minibuffer-"
  :group 'xwem-modes)

(defcustom xwem-minibuffer-name "xwem-minibuffer"
  "*Minibuffer name to be used by XWEM."
  :type 'string
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-bgcol "gray80"
  "*Background color to be used in `xwem-minib-frame'."
  :type 'color
  :set (lambda (sym val)
         (set sym val)
         (when (and xwem-minibuffer
                    (X-Win-p (xwem-minib-xwin xwem-minibuffer)))
           (XSetWindowBackground
            (xwem-dpy) (xwem-minib-xwin xwem-minibuffer)
            (XAllocColor (xwem-dpy) (XDefaultColormap (xwem-dpy))
                         (xwem-make-color xwem-minibuffer-bgcol)))
           (XClearArea (xwem-dpy) (xwem-minib-xwin xwem-minibuffer) 0 0
                       (X-Geom-width (xwem-minib-xgeom xwem-minibuffer))
                       (X-Geom-height (xwem-minib-xgeom xwem-minibuffer)) nil)))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-font (face-font-name 'default)
  "*Font to be used in `xwem-minib-frame'.  May be nil or string."
  :type '(restricted-sexp :match-alternatives ('nil try-font-name))
  :set (lambda (sym val)
         (set sym val)
         (when (and xwem-minibuffer
                    (xwem-minib-frame xwem-minibuffer))
           (set-face-font 'default xwem-minibuffer-font
                          (xwem-minib-frame xwem-minibuffer))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-height 1
  "Height of `xwem-minibuffer'."
  :type 'number
  :set (lambda (sym val)
         (set sym val)
         ;; DO NOT RELY on `set-frame-height'
         (let ((frame (and xwem-minibuffer (xwem-minib-frame xwem-minibuffer)))
               (mcl (and xwem-minibuffer (xwem-minib-cl xwem-minibuffer))))
           (when (and frame mcl)
             (setq st (/ (frame-pixel-height frame) (frame-height frame))
                   nsz (* st xwem-minibuffer-height))
             (xwem-client-resize mcl nil nsz))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-width 80
  "*Usable width of `xwem-minibuffer' frame."
  :type 'number
  :set (lambda (sym val)
         (set sym val)
         ;; DO NOT RELY on `set-frame-width'
         (let ((frame (and xwem-minibuffer (xwem-minib-frame xwem-minibuffer)))
               (mcl (and xwem-minibuffer (xwem-minib-cl xwem-minibuffer))))
           (when (and frame mcl)
             (setq st (/ (frame-pixel-width frame) (frame-width frame))
                   nsz (* st xwem-minibuffer-width))
             (xwem-client-resize mcl nsz nil))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

;;;###xwem-autoload
(defcustom xwem-minibuffer-border-width 2
  "Border width for `xwem-minibuffer'."
  :type 'number
  :set (lambda (sym val)
         (set sym val)
         (let ((cl (and xwem-minibuffer (xwem-minib-cl xwem-minibuffer))))
           (when (xwem-cl-p cl)
             (xwem-client-set-property
              cl 'x-border-width xwem-minibuffer-border-width))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-passive-border-color "blue3"
  "Border color for `xwem-minibuffer'."
  :type 'color
  :set (lambda (sym val)
         (set sym val)
         (let ((cl (and xwem-minibuffer (xwem-minib-cl xwem-minibuffer))))
           (when (xwem-cl-p cl)
             (xwem-set-face-foreground
              'x-border-face xwem-minibuffer-passive-border-color nil cl)
             (xwem-client-set-property
              cl 'x-border-color
              (xwem-face-foreground
               'x-border-face (and (xwem-cl-selected-p cl) '(selected)) cl)))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-active-border-color "blue"
  "Border color for `xwem-minibuffer' when it focused."
  :type 'color
  :set (lambda (sym val)
         (set sym val)
         (let ((cl (and xwem-minibuffer (xwem-minib-cl xwem-minibuffer))))
           (when (xwem-cl-p cl)
             (xwem-set-face-foreground
              'x-border-face xwem-minibuffer-active-border-color
              '(selected) cl)
             (xwem-client-set-property
              cl 'x-border-color
              (xwem-face-foreground
               'x-border-face (and (xwem-cl-selected-p cl)
                                   '(selected)) cl)))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-hide-cursor-mode t
  "*Non-nil mean that Emacs cursor will be invisible in `xwem-minibuffer'.
When `xwem-minibuffer' loses focus Emacs cursor hides, and unhides
when it focused."
  :type 'boolean
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-hide-show-parameter 0
  "*Animation delay parameter, when hiding/showing xwem minibuffer."
  :type 'number
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-autohide-timeout nil
  "*Non-nil mean xwem minibuffer will be autohided, after that many seconds."
  :type '(choice (const :tag "Disabled" nil)
                 (number :tag "Seconds"))
  :set (lambda (sym value)
         (set sym value)
         (let ((mcl (xwem-minib-cl xwem-minibuffer)))
           (when mcl
             (if value
                 (xwem-minibuffer-enable-autohide-timer mcl)
               (xwem-activate mcl)
               (xwem-minibuffer-disable-autohide-timer mcl)))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibiffer-always-on-top-rank 20
  "*Always on top rank or nil."
  :type '(choice (const :tag "No rank" nil)
                 (number :tag "Rank"))
  :set (lambda (sym value)
         (set sym value)
         (let ((xwin (xwem-minib-xwin xwem-minibiffer)))
           (when xwin
             (if value
                 (xwem-misc-set-xwin-always-on-top xwin value)
               (xwem-misc-unset-always-on-top xwin)))))
  :initialize 'custom-initialize-default
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-raise-when-active t
  "*Non-nil mean xwem minibuffer is raised when activated."
  :type 'boolean
  :group 'xwem-minibuffer)
  
(defcustom xwem-minibuffer-emacs-frames-has-minibuffer t
  "*Non-nil mean Emacs frames will have their own minibuffers."
  :type 'boolean
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-set-default-minibuffer-frame t
  "*Non-nil mean that xwem minibuffer frame will be set as `default-minibuffer-frame'.
Modify this variable only if you know what you are doing."
  :type 'boolean
  :group 'xwem-minibuffer)

;;;###xwem-autoload
(defcustom xwem-minibuffer-outer-border-width 1
  "*Outer border width for xwem minibuffer."
  :type 'number
  :group 'xwem-minibuffer)

(defcustom xwem-minibuffer-outer-border-color "black"
  "*Outer border color for xwem minibuffer."
  :type 'color
  :group 'xwem-minibuffer)

;;;###autoload
(defcustom xwem-minibuffer-focusin-hook nil
  "*Hooks called when xwem minibuffer got focus."
  :type 'hook
  :group 'xwem-minibuffer
  :group 'xwem-hooks)

;;;###autoload
(defcustom xwem-minibuffer-focusout-hook nil
  "*Hooks called when xwem minibuffer lose focus."
  :type 'hook
  :group 'xwem-minibuffer
  :group 'xwem-hooks)

(defcustom xwem-minib-resize-exact t
  "*If non-`nil', make minibuffer frame exactly the size needed to display all its contents.
Otherwise, the minibuffer frame can temporarily increase in size but
never get smaller while it is active."
  :type 'boolean
  :group 'xwem-minibuffer)

(defcustom xwem-minib-specifiers
  '((default-toolbar-visible-p . nil)

    ;; Gutters
    (default-gutter-visible-p . t)
    (top-gutter . nil)
    (top-gutter-border-width . 1)

    (menubar-visible-p . nil)
    (horizontal-scrollbar-visible-p . nil)
    ((face-font 'default) . xwem-minibuffer-font))
  "*Alist of specifiers to be set for xwem minibuffer."
  :type '(repeat (cons (sexp :tag "Specifier sexp")
                       (sexp :tag "Specifier value sexp")))
  :set (lambda (sym value)
         (set sym value)
         (when (and xwem-minibuffer
                    (frame-live-p (xwem-minib-frame xwem-minibuffer)))
           (xwem-minib-apply-specifiers (xwem-minib-frame xwem-minibuffer))))
  :group 'xwem-minibuffer)

;;; Internal variables

;; Variables
;;;###xwem-autoload
(defvar xwem-minibuffer nil
  "Default xwem minibuffer.")

(defvar xwem-minibuffer-map 
  (let ((map (make-sparse-keymap)))
    (define-key map (xwem-kbd "H-g") 'minibuffer-keyboard-quit)
    (define-key map (xwem-kbd "C-<button1>") 'xwem-client-imove)
    map)
  "Keymap used while in xwem.")


(defun xwem-minib-apply-specifiers (frame)
  "Apply `xwem-minib-specifiers' to FRAME."
  (mapc #'(lambda (spc)
            (set-specifier (eval (car spc)) (eval (cdr spc))
                           frame nil 'remove-locale))
        xwem-minib-specifiers))

(defun xwem-minib-create ()
  "Create minibuffer that will be used by xwem, or use existen."
  (let ((mframe (or (xwem-misc-find-frame xwem-minibuffer-name
                                          (minibuffer-frame-list))
                    (make-frame minibuffer-frame-plist
                                (default-x-device))))
        (dd default-directory))

    (setf (xwem-minib-frame xwem-minibuffer) mframe)
    ;; Set specifiers values for MFRAME
    (xwem-minib-apply-specifiers mframe)
    (redraw-frame mframe t)             ; KEEP THIS!

    ;; Hack over default-directory for minibuffer buffer
    (with-current-buffer
        (window-buffer (frame-root-window mframe))
      (setq default-directory dd))

    mframe))

(defmacro xwem-cl-minibuffer (cl)
  `(xwem-cl-get-sys-prop ,cl 'xwem-minibuffer))
(defsetf xwem-cl-minibuffer (cl) (minib)
  `(xwem-cl-put-sys-prop ,cl 'xwem-minibuffer ,minib))

;;; Minibuffer focus model
(define-xwem-focus-mode minibuffer (cl action &optional xev)
  "Focus mode for xwem minibuffer"
  (let ((mb (xwem-cl-minibuffer cl)))
    (when (and (xwem-minib-p mb)
               (X-Event-p xev)
               (not (member (X-Event-xfocus-mode xev)
                            (list X-NotifyVirtual X-NotifyNonlinearVirtual))))
      (cond ((eq action 'focus-in)
             ;; XWEM Minibuffer activates
             (run-hook-with-args 'xwem-minibuffer-focusin-hook mb))
            ((eq action 'focus-out)
             ;; XWEM Minibuffer deactivates
             (run-hook-with-args 'xwem-minibuffer-focusout-hook mb))))))
  
;;;; ---- XWEM Minibuffer manage type ----
(defun xwem-minibuffer-client-p (cl)
  "Return non-nil if CL is minibuffer client."
  (xwem-cl-minibuffer cl))

(defun xwem-manage-minibuffer (cl)
  "Manage method for xwem minibuffers."
  (let* ((xgeom (make-X-Geom :x 0       ; XXX
                             :y (- (X-Geom-height (xwem-rootgeom))
                                   xwem-minibuffer-outer-border-width
                                   xwem-minibuffer-outer-border-width)
                             :width (- (X-Geom-width (xwem-rootgeom))
                                       xwem-minibuffer-outer-border-width
                                       xwem-minibuffer-outer-border-width)
                             :height (X-Geom-height-with-borders
                                      (xwem-cl-xgeom cl))
                             :border-width xwem-minibuffer-outer-border-width))
         (minib (make-xwem-minib
                 :frame (xwem-misc-find-emacs-frame cl)
                 :cl cl
                 :xgeom xgeom)))

    (setf (xwem-minib-xwin minib)
          (XCreateWindow (xwem-dpy) nil
                         (X-Geom-x xgeom)
                         (X-Geom-y xgeom)
                         (X-Geom-width xgeom)
                         (X-Geom-height xgeom)
                         (X-Geom-border-width xgeom)
                         nil nil nil
                         (make-X-Attr :override-redirect t
                                      :background-pixel
                                      (XAllocNamedColor
                                       (xwem-dpy) (XDefaultColormap (xwem-dpy))
                                       xwem-minibuffer-bgcol))))
    ;; Setup window a little
    (when xwem-minibuffer-outer-border-color
      (XSetWindowBorder (xwem-dpy) (xwem-minib-xwin minib)
                        (XAllocNamedColor
                         (xwem-dpy) (XDefaultColormap (xwem-dpy))
                         xwem-minibuffer-outer-border-color)))
                                          
    ;; Save CL's minibuffer
    (setf (xwem-cl-minibuffer cl) minib)

    ;; Setup x-border-face for minibuffer
    (xwem-set-face-foreground 'x-border-face xwem-minibuffer-active-border-color
                              '(selected) cl)
    (xwem-set-face-foreground 'x-border-face xwem-minibuffer-passive-border-color
                              nil cl)
    (xwem-client-set-property cl 'x-border-width xwem-minibuffer-border-width)
    (xwem-client-set-property cl 'x-border-color
                              (xwem-face-foreground 'x-border-face
                                                    (and (xwem-cl-selected-p cl)
                                                         '(selected)) cl))

    ;; Reparent xwem minib client to parent
    ;; XXX XXX
    (setf (X-Geom-x (xwem-cl-xgeom cl)) 0)
    (setf (X-Geom-y (xwem-cl-xgeom cl)) 0)
    (XReparentWindow (xwem-dpy) (xwem-minib-cl-xwin minib)
                     (xwem-minib-xwin minib) 0 0)

    ;; Set minibuffer focus model
    (xwem-focus-mode-set cl 'minibuffer)

    ;; Install minibuffer local keymap
    (xwem-use-local-map xwem-minibuffer-map cl)

    ;; Finnally refit cl and map parent
    (xwem-refit cl)
    (XMapWindow (xwem-dpy) (xwem-minib-xwin minib))

    ;; Set always on top rank (if any)
    (when xwem-minibiffer-always-on-top-rank
      (xwem-misc-set-xwin-always-on-top
       (xwem-minib-xwin minib) xwem-minibiffer-always-on-top-rank))

    ;; Set default minibuffer, if not already set
    (unless (xwem-cl-p (xwem-minib-cl xwem-minibuffer))
      (setq xwem-minibuffer minib)
      (when xwem-minibuffer-set-default-minibuffer-frame
        (setq default-minibuffer-frame (xwem-minib-frame xwem-minibuffer))))

    ;; Now activate minibuffer
    (xwem-activate cl)
    (xwem-minib-apply-state-1 minib )))

(define-xwem-deffered xwem-minib-apply-pxgeom (minib)
  "Apply MINIB's parent geometry to life."
  (let ((pxgeom (xwem-minib-xgeom minib)))
    (XMoveResizeWindow (xwem-dpy)
                       (xwem-minib-xwin minib)
                       (X-Geom-x pxgeom)
                       (X-Geom-y pxgeom)
                       (X-Geom-width pxgeom)
                       (X-Geom-height pxgeom))))

;;
;; Some bug here:

;;  Do `(xwem-refit (xwem-minib-cl xwem-minibuffer))' - xwem minib
;;  will change its width. (ONLY when xwem-minib-resize-mode is on)

(defun xwem-refit-minibuffer (cl)
  "Refit xwem minibuffer client CL."
  (xwem-debug 'xwem-misc "Minib: Refiting .. to %S" '(xwem-cl-new-xgeom cl))

  (let ((cl-xgeom (xwem-cl-xgeom cl))
        (cl-nx (and (xwem-cl-new-xgeom cl)
                    (X-Geom-x (xwem-cl-new-xgeom cl))))
        (pxgeom (xwem-minib-xgeom (xwem-cl-minibuffer cl))))
    ;; Adjust geometry a little to fill into xwem-minib-xwin and apply
    ;; changes to life
    (xwem-cl-apply-new-xgeom cl)
    (when cl-nx
      ;; CL has new x location - handle it
      (setf (X-Geom-x pxgeom) (X-Geom-x cl-xgeom))
      (setf (X-Geom-width pxgeom)
            (- (X-Geom-width (xwem-rootgeom))
               (X-Geom-x pxgeom)
               xwem-minibuffer-outer-border-width
               xwem-minibuffer-outer-border-width))
      (xwem-minib-apply-pxgeom (xwem-cl-minibuffer cl)))
    (xwem-cl-correct-size-for-size
     cl
     (make-X-Geom :x 0 :y 0
                  :width (X-Geom-width-with-borders cl-xgeom)
                  :height (X-Geom-height-with-borders cl-xgeom)
                  :border-width (X-Geom-border-width cl-xgeom))
     'left 'top)
    (xwem-cl-apply-xgeom cl)

    ;; Check maybe parent need to be resized/moved?
    (unless (= (X-Geom-height-with-borders cl-xgeom)
               (X-Geom-height pxgeom))
      (when (eq (xwem-cl-state cl) 'active)
        (decf (X-Geom-y pxgeom)
              (- (X-Geom-height-with-borders cl-xgeom)
                 (X-Geom-height pxgeom))))
      (setf (X-Geom-height pxgeom)
            (X-Geom-height-with-borders cl-xgeom))
      (xwem-minib-apply-pxgeom (xwem-cl-minibuffer cl)))))

(defun xwem-minibuffer-autohide-timer (cl)
  (when (and (numberp xwem-minibuffer-autohide-timeout)
             (not (xwem-cl-selected-p cl)))
    (xwem-deactivate cl))
  (xwem-cl-rem-sys-prop cl 'auto-hide-timer))

(defun xwem-minibuffer-disable-autohide-timer (cl)
  (let ((tmr (xwem-cl-get-sys-prop cl 'auto-hide-timer)))
    (when tmr
      (disable-timeout tmr)
      (xwem-cl-rem-sys-prop cl 'auto-hide-timer))))
  
(defun xwem-minibuffer-enable-autohide-timer (cl)
  (when (numberp xwem-minibuffer-autohide-timeout)
    (xwem-minibuffer-disable-autohide-timer cl)
    (xwem-cl-put-sys-prop cl 'auto-hide-timer
      (add-timeout xwem-minibuffer-autohide-timeout
                   'xwem-minibuffer-autohide-timer cl))))
  
(define-xwem-deffered xwem-minib-apply-state (minib)
  "Apply xwem minibuffer MINIB's state to life."
  (let* ((pgeom (xwem-minib-xgeom minib))
         (cyo (X-Geom-height pgeom))
         (state (xwem-cl-state (xwem-minib-cl minib)))
         (param (and (numberp xwem-minibuffer-hide-show-parameter)
                     (not (zerop xwem-minibuffer-hide-show-parameter))
                     xwem-minibuffer-hide-show-parameter))
         (i 0))
    (when (not (eq state (xwem-minib-get-prop minib 'saved-state)))
      (xwem-debug 'xwem-misc "Minib: new state .. %S" 'state)

      (while (< i cyo)
        (setf (X-Geom-y (xwem-minib-xgeom minib))
              (funcall (if (eq state 'active) '- '+)
                       (X-Geom-y (xwem-minib-xgeom minib)) 1))
        (XMoveWindow (xwem-dpy) (xwem-minib-xwin minib)
                     (X-Geom-x (xwem-minib-xgeom minib))
                     (X-Geom-y (xwem-minib-xgeom minib)))
        (XFlush (xwem-dpy))
        (when param
          (sit-for param t))
        (setq i (1+ i)))
      (xwem-minib-put-prop minib 'saved-state state)

      (when (and xwem-minibuffer-raise-when-active
                 (eq state 'active))
        (xwem-misc-raise-xwin (xwem-minib-xwin minib))))

    (when (eq state 'active)
      (xwem-minibuffer-enable-autohide-timer (xwem-minib-cl minib)))))

(defun xwem-activate-minibuffer (cl &optional type)
  "Activate xwem minibuffer CL."
  (cond ((eq type 'activate)
         (xwem-minib-apply-state (xwem-cl-minibuffer cl)))

        ((eq type 'select)
         (when xwem-minibuffer-hide-cursor-mode
           (set-frame-property (xwem-minib-frame (xwem-cl-minibuffer cl))
                               'text-cursor-visible-p t))

         (xwem-minibuffer-disable-autohide-timer cl))))
  
(defun xwem-deactivate-minibuffer (cl &optional type)
  "Deactivate xwem minibuffer client CL."
  (cond ((eq type 'deactivate)
         (xwem-minib-apply-state (xwem-cl-minibuffer cl)))

        ((eq type 'deselect)
         (when xwem-minibuffer-hide-cursor-mode
           (set-frame-property (xwem-minib-frame (xwem-cl-minibuffer cl))
                               'text-cursor-visible-p nil))
         (when xwem-minibuffer-autohide-timeout
           (xwem-deactivate cl)))))

(defun xwem-iconify-minibuffer (cl)
  "Iconify xwem minibuffer client CL."
  (xwem-deactivate cl))

;; Events handler
(define-xwem-deffered xwem-minib-focusin-autoraise (minib)
  "Mainly for use in `xwem-minibuffer-focusout-hook'."
  (xwem-misc-raise-xwin (xwem-minib-xwin minib)))

(define-xwem-deffered xwem-minib-focusout-autolower (minib)
  "Mainly for use in `xwem-minibuffer-focusout-hook'."
  (xwem-misc-lower-xwin (xwem-minib-xwin minib)))

;;;###autoload(autoload 'xwem-minibuffer-activate "xwem-minibuffer" "" t)
(define-xwem-command xwem-minibuffer-activate ()
  "Switch to xwem minibuffer if it is in active state."
  (xwem-interactive)
  (if (eq (active-minibuffer-window)
          (frame-selected-window (xwem-minib-frame xwem-minibuffer)))
      (xwem-select-client (xwem-minib-cl xwem-minibuffer))
    (xwem-activate (xwem-minib-cl xwem-minibuffer))))

;;;###autoload
(defun xwem-minibuffer-init ()
  "Initialize xwem minibuffer."
  (xwem-message 'init "Initializing minibuffer ...")

  (setq minibuffer-frame-plist
        (xwem-misc-merge-plists
         minibuffer-frame-plist
         `(name ,xwem-minibuffer-name
                wait-for-wm nil
                height ,xwem-minibuffer-height
                text-cursor-visible-p ,(not xwem-minibuffer-hide-cursor-mode)
                width ,xwem-minibuffer-width
                minibuffer only)))

  ;; Adjust initial frame params
  (unless xwem-minibuffer-emacs-frames-has-minibuffer
    (setq initial-frame-plist
          (plist-put initial-frame-plist 'minibuffer nil)))
  (setq initial-frame-plist
        (plist-put initial-frame-plist 'wait-for-wm nil))

  ;; Adjust default frame params
  (unless xwem-minibuffer-emacs-frames-has-minibuffer
    (setq default-x-frame-plist
          (plist-put default-x-frame-plist 'minibuffer nil)))
  (setq default-x-frame-plist
        (plist-put default-x-frame-plist 'wait-for-wm nil))

  ;; Create XEmacs minibuffer only frame for xwem minibuffer
  (setq xwem-minibuffer (make-xwem-minib))
  (setf (xwem-minib-frame xwem-minibuffer)
        (make-initial-minibuffer-frame nil))
  (xwem-message 'init "Initializing minibuffer ... done"))

;;; Resize-minibuffer mode
(defvar xwem-minib-rsz-saved-height nil)

(defun xwem-minib-rsz-count-window-lines (&optional start end)
  "Return number of window lines occupied by text in region.
The number of window lines may be greater than the number of actual lines
in the buffer if any wrap on the display due to their length.

Optional arguments START and END default to point-min and point-max,
respectively."
  (or start (setq start (point-min)))
  (or end   (setq end   (point-max)))
  (if (= start end)
      0
    (save-excursion
      (save-restriction
        (widen)
        (narrow-to-region start end)
        (goto-char start)
        (vertical-motion (buffer-size))))))

(defun xwem-minib-rsz-restore ()
  "Restore xwem minibuffer size."
  (xwem-minib-rsz-resize xwem-minib-rsz-saved-height)
  (xwem-misc-lower-xwin (xwem-minib-xwin xwem-minibuffer)))

(defun xwem-minib-rsz-check ()
  "Called in `pre-command-hook'.
Check is next command is actually keyboard quit.
If yes, then restore xwem minibuffer size."
  (when (member this-command '(minibuffer-keyboard-quit keyboard-quit))
    (xwem-minib-rsz-restore)))

(defun xwem-minib-rsz-resize (&optional new-height)
  "Resize xwem minibuffer to fit either NEW-HEIGHT.
If NEW-HEIGHT is ommited, current window height considered."
  (let* ((frame (xwem-minib-frame xwem-minibuffer))
         (height (frame-height frame))
         (lines (or new-height (1+ (xwem-minib-rsz-count-window-lines))))
         w n)
    (cond ((or new-height xwem-minib-resize-exact (> lines height))
           (setq w (frame-pixel-height frame))
           (set-frame-size frame (frame-width frame) lines)
           (setq n (frame-pixel-height frame))

           (xwem-misc-raise-xwin (xwem-minib-xwin xwem-minibuffer))

           ;; Adjust xwem minibuffer cl height
           (xwem-client-resize (xwem-minib-cl xwem-minibuffer) nil
                               (+ (X-Geom-height
                                   (xwem-minib-cl-xgeom xwem-minibuffer))
                                  (- n w)))
           ))))

(defun xwem-minib-rsz-setup ()
  "Setup xwem resize minibuffer mode for xwem minibuffer."
  (when (eq (window-frame (minibuffer-window))
            (xwem-minib-frame xwem-minibuffer))
    (setq xwem-minib-rsz-saved-height
          (frame-property (selected-frame) 'height))

    (make-local-hook 'minibuffer-exit-hook)
    (add-hook 'minibuffer-exit-hook 'xwem-minib-rsz-restore nil t)
    (make-local-hook 'pre-command-hook)
    (add-hook 'pre-command-hook 'xwem-minib-rsz-check nil t)
    (make-local-hook 'post-command-hook)
    (add-hook 'post-command-hook 'xwem-minib-rsz-resize nil t)

    (unless (and (boundp 'icomplete-mode)
                 (eval 'icomplete-mode)) ; shutup compiler
      (xwem-minib-rsz-resize))
    ))

;;;###autoload(autoload 'xwem-minib-resize-mode "xwem-minibuffer" "" t)
(define-xwem-command xwem-minib-resize-mode (arg)
  "Start/stop xwem minibuffer auto-resize mode.
With prefix ARG start, without - stop."
  (xwem-interactive "P")
  (if arg
      (add-hook 'minibuffer-setup-hook 'xwem-minib-rsz-setup)
    (remove-hook 'minibuffer-setup-hook 'xwem-minib-rsz-setup)))

;;; xwem minibuffer modeline using top gutter
(defvar xwem-modeline-format
  '("--"
    (symbol-name (xwem-cl-manage-type cl))
    ": "
    "["
    (or (car (xwem-client-application cl))
        "unknown")
    "]"

    ;; Some additional info
    " "
    (if (xwem-cl-marked-p cl) "*" "-")
    (let ((reg (xwem-client-property cl 'register)))
      (if reg (list (format "%c" reg) 'bold) "-"))
    " "

    (list (xwem-client-name cl) 'modeline-buffer-id)
    "   "
    ;; Minor modes
    "("
    (mapconcat 'identity
               (delq nil (mapcar #'(lambda (mm)
                                     (and (symbol-value (car mm))
                                          (cadr mm)))
                                 xwem-minor-mode-alist)) " ")
    ")"
    "----"
    (let ((usz (xwem-cl-get-usize cl)))
      (format "%dx%d" (car usz) (cdr usz)))
    "--")
  "Modeline format.")
(xwem-make-variable-client-local 'xwem-modeline-format)

(defun xwem-modeline-regenerate ()
  "Regenerate modeline string."
  (mapconcat #'(lambda (me)
                 (let ((cl (xwem-cl-selected)))
                   (setq cl cl)         ; shutup compiler
                   (condition-case nil
                       (let ((str "") (faces nil))
                         (setq me (eval me))
                         (if (listp me)
                             (setq str (copy-sequence (car me))
                                   faces (cdr me))
                           (setq str (copy-sequence me)
                                 faces nil))
                         (xwem-str-with-faces str (append '(modeline) faces)))
                     (t "<error>"))))
             xwem-modeline-format ""))

(define-xwem-deffered xwem-modeline-redraw (&optional cl)
  "Redraw xwem modeline."
  (if (xwem-cl-alive-p cl)
      (when (xwem-cl-selected-p cl)
        (xwem-modeline-redraw))

    (let* ((str (xwem-modeline-regenerate))
           (mw (frame-width (xwem-minib-frame xwem-minibuffer))))
      (set-specifier top-gutter (substring str 0 (and (> (length str) mw) mw))
                     (xwem-minib-frame xwem-minibuffer))
      )))

;;;###autoload(autoload 'xwem-modeline-enable "xwem-minibuffer" nil t)
(define-xwem-command xwem-modeline-enable ()
  "Enable modeline."
  (xwem-interactive)

  (add-hook 'xwem-cl-change-hook 'xwem-modeline-redraw)
  (add-hook 'xwem-client-select-hook 'xwem-modeline-redraw)

  (set-specifier top-gutter-visible-p t
                 (xwem-minib-frame xwem-minibuffer))
  ;; Start showing gutter
  (xwem-modeline-redraw-1)
  ;; Fix xwem minibuffer height size
  (xwem-client-resize
   (xwem-minib-cl xwem-minibuffer) nil
   (+ (X-Geom-height-with-borders (xwem-minib-cl-xgeom xwem-minibuffer))
      (gutter-pixel-height 'top (xwem-minib-frame xwem-minibuffer))
      (specifier-instance top-gutter-border-width
                          (xwem-minib-frame xwem-minibuffer))
      (specifier-instance top-gutter-border-width
                          (xwem-minib-frame xwem-minibuffer)))))

;;;###autoload(autoload 'xwem-modeline-disable "xwem-minibuffer" nil t)
(define-xwem-command xwem-modeline-disable ()
  "Disable modeline."
  (xwem-interactive)

  (remove-hook 'xwem-cl-change-hook 'xwem-modeline-redraw)
  (remove-hook 'xwem-client-select-hook 'xwem-modeline-redraw)

  ;; Fix xwem minibuffer height size
  (xwem-client-resize
   (xwem-minib-cl xwem-minibuffer) nil
   (- (X-Geom-height-with-borders (xwem-minib-cl-xgeom xwem-minibuffer))
      (gutter-pixel-height 'top (xwem-minib-frame xwem-minibuffer))
      (specifier-instance top-gutter-border-width
                          (xwem-minib-frame xwem-minibuffer))
      (specifier-instance top-gutter-border-width
                          (xwem-minib-frame xwem-minibuffer))))
  ;; Stop showing gutter
  (set-specifier top-gutter nil
                 (xwem-minib-frame xwem-minibuffer)))


(provide 'xwem-minibuffer)

;;;; On-load actions:
;; Define application
(add-to-list 'xwem-applications-alist
             `("xemacs-xwem-minibuffer"
               (and (class-name ,(concat "^" x-emacs-application-class "$"))
                    (class-inst ,(concat "^" xwem-minibuffer-name "$")))))

;; Add manage type
(define-xwem-manage-model minibuffer
  "Managing model for xwem minibuffer."
  :cl-properties '(dummy-client-p t
                                  skip-deselect t
                                  override-skip-deselect t)
  :match-spec '(application "xemacs-xwem-minibuffer")

  :manage-method 'xwem-manage-minibuffer
  :activate-method 'xwem-activate-minibuffer
  :deactivate-method 'xwem-deactivate-minibuffer
  :refit-method 'xwem-refit-minibuffer
  :iconify-method 'xwem-iconify-minibuffer)

;; - Before init hook
(if xwem-started
    (xwem-minib-create)
  (add-hook 'xwem-config-read-hook 'xwem-minibuffer-init)
  (add-hook 'xwem-before-init-hook 'xwem-minib-create))

;;; xwem-minibuffer.el ends here