-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabkey2.el
More file actions
1543 lines (1393 loc) · 57.8 KB
/
tabkey2.el
File metadata and controls
1543 lines (1393 loc) · 57.8 KB
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; tabkey2.el --- Use second tab key pressed for what you want
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2008-03-15T14:40:28+0100 Sat
(defconst tabkey2:version "1.33")
;; Last-Updated: 2008-07-21T22:24:55+0200 Mon
;; URL: http://www.emacswiki.org/cgi-bin/wiki/tabkey2.el
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;; `appmenu', `cl'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; The tab key is in Emacs often used for indentation. However if you
;; press the tab key a second time and Emacs tries to do indentation
;; again, then usually nothing exciting will happen. Then why not use
;; second tab key in a row for something else?
;;
;; Commonly used completion functions in Emacs is often bound to
;; something corresponding to Alt-Tab. Unfortunately this is unusable
;; if you have a window manager that have an apetite for it (like that
;; on MS Windows for example, and several on GNU/Linux).
;;
;; Then using the second tab key press for completion might be a good
;; choice and perhaps also easy to remember.
;;
;; This little library tries to make it easy to do use the second tab
;; press for completion. Or you can see this library as a swizz army
;; knife for the tab key ;-)
;;
;; See `tabkey2-mode' for more information.
;;
;;
;; This is a generalized of an idea Sebastien Rocca Serra once
;; presented on Emacs Wiki and called "Smart Tab". (It seems like
;; many others have also been using Tab for completion in one way or
;; another for years.)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;; Version 1.04:
;; - Add overlay to display state after first tab.
;;
;; Version 1.05:
;; - Fix remove overlay problem.
;;
;; Version 1.06:
;; - Add completion function choice.
;; - Add support for popcmp popup completion.
;;
;; Version 1.07:
;; - Add informational message after first tab.
;;
;; Version 1.08:
;; - Give better informational message after first tab.
;;
;; Version 1.09:
;; - Put flyspell first.
;;
;; Version 1.09:
;; - Give the overlay higher priority.
;;
;; Version 1.10:
;; - Correct tabkey2-completion-functions.
;; - Add double-tab for modes where tab can not be typed again.
;; - Use better condition for when completion can be done, so that it
;; can be done later while still on the same line.
;; - Add a better message handling for the "Tab completion state".
;; - Add C-g break out of the "Tab completion state".
;; - Add faces for highlight.
;; - Make it work in custom mode buffers.
;; - Fix documentation for `tabkey2-first'
;;
;; Version 1.11:
;; - Don't call chosen completion function directly. Instead make it
;; default for current buffer.
;;
;; Version 1.12:
;; - Simplify code.
;; - Add help to C-f1 during "Tab completion state".
;; - Fix documentation basics.
;; - Add customization of state message and line marking.
;; - Fix handling of double-Tab modes.
;; - Make user interaction better.
;; - Handle read-only in custom buffers better.
;; - Add more flexible check for if completion function is active.
;; - Support predictive mode.
;; - Reorder and simplify.
;;
;; Version 1.13:
;; - Add org-mode to the double-tab gang.
;; - Make it possible to use double-tab in normal buffers.
;; - Add cycling through completion functions to S-tab.
;;
;; Version 1.14:
;; - Fix bug in handling of read-only.
;; - Show completion binding in help message.
;; - Add binding to make current choice buffer local when cycling.
;;
;; Version 1.15:
;; - Fix problem at buffer end.
;; - Add S-tab to enter completion state without indentation.
;; - Add backtab bindings too for this.
;; - Remove double-tab, S-tab is better.
;; - Add list of modes that uses more tabs.
;; - Add list of modes that uses tab only for completion.
;; - Move first overlay when indentation changes.
;; - Make mark at line beginning 1 char long.
;;
;; Version 1.16:
;; - Don't call tab function when alternate key is pressed.
;;
;; Version 1.17:
;; - Let alternate key cycle completion functions instead of complete.
;; - Bind backtab.
;; - Fix bug when only one completion funciton was available.
;; - Fix bug when alt key and major without fix indent.
;;
;; Version 1.18:
;; - Add popup style messages.
;; - Add delay to first message.
;; - Use different face for indicator on line and message.
;; - Use different face for echo area and popup messages.
;; - Add anything to completion functions.
;; - Put help funciton on f1.
;; - Always bind alternate key to cycle.
;; - Change defcustoms to simplify (excuse me).
;; - Work around end of buffer problems.
;; - Work around start of buffer problems.
;; - Assure popup messages are visible.
;; - Reorder code in more logical order.
;;
;; Version 1.19:
;; - Make overlay keymap end advance.
;; - Remove overlay keymap parent.
;;
;; Version 1.20:
;; - Fix bug on emtpy line.
;; - Fix some text problems.
;; - Make f1 c/k work in tab completion state.
;;
;; Version 1.20:
;; - Fixed bug in overlay removal.
;;
;; Version 1.21:
;; - Fixed bug in minibuffer setup.
;;
;; Version 1.22:
;; - Honour widget-forward, button-forward.
;;
;; Version 1.23:
;; - Remove binding of shift tab.
;; - Check if use-region-p is defined.
;;
;; Version 1.24:
;; - Add option for completion state mode line marker.
;; - Fix bug in tabkey2-show-completion-functions.
;; - Move off completion point cancels completion state.
;; - Fix bugs in help.
;; - Try to fix some problems with invisible text, at least in
;; org-mode.
;; - Restore window config, completions often leaves without.
;;
;; Version 1.25:
;; - Fix bug in tabkey2-completion-state-p.
;;
;; Version 1.26:
;; - Make tabkey2-mode a buffer local mode.
;; - Add tabkey2-global-mode.
;; - Fix some bugs.
;;
;; Version 1.27:
;; - Fix some bugs in customization.
;;
;; Version 1.28:
;; - Use invisible-p.
;;
;; Version 1.29:
;; - Remove tabkey2-global-mode because of problem with minibuffers.
;;
;; Version 1.30:
;; - Add Semantic's smart completion to completion functions.
;; (Thanks Eric.)
;;
;; Version 1.31:
;; - Add yasnippet and pabbrev completion functions. (Thanks Eric.)
;; - Reorder completion functions.
;;
;; Version 1.32:
;; - Add support for pcomplete.
;; - Inform about other key bindings in completion functions list.
;; - Remove no longer used "preferred" from completion functions list.
;;
;; Version 1.33:
;; -- Automatically select next function on completion failure.
;; -- Add completion functions reset functions.
;;
;; Fix-me: maybe add \\_>> option to behave like smart-tab. But this
;; will only works for modes that does not do completion of empty
;; words (like in smart-tab).
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Known bugs
;;
;; - Maybe problems with comint shell.
;; - Does not check visibility very carefully.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 2, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(eval-when-compile (require 'cl))
;;(require 'popcmp nil t)
(require 'appmenu nil t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Custom
(defgroup tabkey2 nil
"Customization of second tab key press."
:group 'nxhtml
:group 'convenience)
(defface tabkey2-highlight-line
'((t :inherit highlight))
"Face for marker on line when default function is active."
:group 'tabkey2)
(defface tabkey2-highlight-line2
'((t :inherit isearch-fail))
"Face for marker on line when non-default function is active."
:group 'tabkey2)
(defface tabkey2-highlight-message
'((t :inherit tabkey2-highlight-line))
"Face for messages in echo area."
:group 'tabkey2)
(defface tabkey2-highlight-popup
'((default :box t :inherit tabkey2-highlight-message)
(((class color) (background light)) :foreground "black")
(((class color) (background dark)) :foreground "yellow"))
"Face for popup messages."
:group 'tabkey2)
(defcustom tabkey2-show-mark-on-active-line t
"Show mark on active line if non-nil.
This mark is shown during 'Tab completion state'."
:type 'boolean
:group 'tabkey2)
(defvar tabkey2-completion-lighter nil)
(defcustom tabkey2-completion-lighter-on nil
"Mode line lighter for function `tabkey2-completion-state-mode'."
:type 'boolean
:set (lambda (symbol value)
(set-default symbol value)
(setq tabkey2-completion-lighter (if value " Tab2" nil))
(setq minor-mode-alist
(assq-delete-all 'tabkey2-completion-state-mode
minor-mode-alist)))
:group 'tabkey2)
(defcustom tabkey2-show-message-on-enter 2.0
"If non-nil show message when entering 'Tab completion state'.
If value is a number then delay message that number of seconds."
:type '(choice (const :tag "Don't show" nil)
(const :tag "Show at once" t)
(float :tag "Show, but delayed (seconds)"))
:group 'tabkey2)
;; (setq tabkey2-message-style 'popup)
;; (setq tabkey2-message-style 'echo-area)
(defcustom tabkey2-message-style 'popup
"How to show messages."
:type '(choice (const :tag "Popup" popup)
(const :tag "Echo area" echo-area))
:group 'tabkey2)
(defcustom tabkey2-in-minibuffer nil
"If non-nil use command `tabkey2-mode' also in minibuffer."
:type 'boolean
:group 'tabkey2)
(defcustom tabkey2-in-appmenu t
"Show a completion menu in command `appmenu-mode' if t."
:type 'boolean
:set (lambda (sym val)
(set-default sym val)
(when (fboundp 'appmenu-add)
(if val
(appmenu-add 'tabkey2 nil t "Completion" 'tabkey2-appmenu)
(appmenu-remove 'tabkey2))))
:group 'tabkey2)
(defcustom tabkey2-completion-functions
'(
;; Temporary things
("Spell check word" flyspell-correct-word-before-point)
;; Main mode related, often used
("Semantic Smart Completion" senator-complete-symbol senator-minor-mode)
("Programmable completion" pcomplete)
("nXML completion" nxml-complete)
("Complete Emacs symbol" lisp-complete-symbol)
("Widget complete" widget-complete)
("Comint Dynamic Complete" comint-dynamic-complete)
("PHP completion" php-complete-function)
("Tags completion" complete-symbol)
;; General word completion
("Predictive word" complete-word-at-point predictive-mode)
("Predictive abbreviations" pabbrev-expand-maybe)
("Dynamic word expansion" dabbrev-expand nil (setq dabbrev--last-abbrev-location nil))
("Ispell complete word" ispell-complete-word)
;; Snippets
("Yasnippet" yas/expand)
;; The catch all
("Anything" anything (commandp 'anything))
)
"List of completion functions.
The first 'active' entry in this list is normally used during the
'Tab completion state' by `tabkey2-complete'. An entry in the
list should have either of this forms
\(DESCRIPTION FUNCTION ACTIVE-FORM RESET-FORM)
The entry is considered active if:
- The symbol FUNCTION is bound to a function
and
- this function has a key binding at point,
or
the elisp expression ACTIVE-FORM evaluates to non-nil. If it
is a single symbol then its variable value is used, otherwise
the elisp form is evaled.
RESET-FORM is used to reset the completion function.
When choosing with `tabkey2-cycle-completion-functions'
only the currently active entry in this list are shown."
:type '(repeat (list string (choice (command :tag "Currently known command")
(symbol :tag "Command not known yet"))
(choice (const :tag "Active only if it has a key binding at point" nil)
(sexp :tag "Elisp, if evals to non-nil then active"))
(sexp :tag "Elisp, reset completion function")))
:group 'tabkey2)
;; Use emulation mode map for first Tab key
(defconst tabkey2-mode-emul-map (make-sparse-keymap)
"This keymap just binds tab and alternate key all the time.
By default this binds Tab to `tabkey2-first'. The actual keys
bound are in `tabkey2-first-key' and `tabkey2-alternate-key'.")
(defvar tabkey2--emul-keymap-alist nil)
;; (setq tabkey2-keymap-overlay nil)
(defconst tabkey2-completion-state-emul-map
(let ((map (make-sparse-keymap)))
(define-key map [(control ?c) tab] 'tabkey2-make-current-default)
;;(define-key map tabkey2-alternate-key 'tabkey2-cycle-completion-functions)
(define-key map [backtab] 'tabkey2-cycle-completion-functions)
(define-key map [(control f1)] 'tabkey2-completion-function-help)
(define-key map [(meta f1)] 'tabkey2-show-completion-functions)
(define-key map [f1] 'tabkey2-completion-state-help)
(define-key map [(control ?g)] 'tabkey2-completion-state-off)
(define-key map [tab] 'tabkey2-complete)
map)
"This keymap is for `tabkey2-keymap-overlay'.")
(defun tabkey2-bind-keys (first-key alternate-key)
(let ((mode-map tabkey2-mode-emul-map)
(comp-map tabkey2-completion-state-emul-map))
;; First key
(when (and (boundp 'tabkey2-first-key)
tabkey2-first-key)
(define-key mode-map tabkey2-first-key nil))
(when first-key
(define-key mode-map first-key 'tabkey2-first))
;; Alternate key
(when (and (boundp 'tabkey2-alternate-key)
tabkey2-alternate-key)
(define-key mode-map tabkey2-alternate-key nil)
(define-key comp-map tabkey2-alternate-key nil))
(when alternate-key
(define-key mode-map alternate-key 'tabkey2-cycle-completion-functions)
(define-key comp-map alternate-key 'tabkey2-cycle-completion-functions))
(when (and (boundp 'tabkey2-completion-state-mode)
tabkey2-completion-state-mode)
(tabkey2-completion-state-mode -1)
(tabkey2-completion-state-mode 1))))
(defcustom tabkey2-first-key [tab]
"First key, first time indents, more invocations completes.
This key is always bound to `tabkey2-first'."
:set (lambda (sym val)
(tabkey2-bind-keys val (when (boundp 'tabkey2-alternate-key) tabkey2-alternate-key))
(set-default sym val))
:type 'key-sequence
:group 'tabkey2)
(defcustom tabkey2-alternate-key [f8]
"Alternate key, bound to cycle and show completion functions.
This key is always bound to `tabkey2-cycle-completion-functions'."
:set (lambda (sym val)
(tabkey2-bind-keys (when (boundp 'tabkey2-first-key) tabkey2-first-key) val)
(set-default sym val))
:type 'key-sequence
:group 'tabkey2)
(tabkey2-bind-keys tabkey2-first-key tabkey2-alternate-key)
(defcustom tabkey2-modes-that-use-more-tabs
'(python-mode
haskell-mode
makefile-mode
org-mode
Custom-mode
;; other
cmd-mode
)
"In those modes use must use S-Tab to start completion state.
In those modes pressing Tab several types may make sense so you
can not go into 'Tab completion state' just because one Tab has
been pressed. Instead you use S-Tab to go into that state.
After that Tab does completion.
You can do use S-Tab in other modes too if you want too."
:type '(repeat (choice (command :tag "Currently known command")
(symbol :tag "Command not known yet")))
:group 'tabkey2)
(defcustom tabkey2-modes-that-just-complete
'(shell-mode)
"Tab is only used for completion in these modes.
Therefore `tabkey2-first' just calls the function on Tab."
:type '(repeat (choice (command :tag "Currently known command")
(symbol :tag "Command not known yet")))
:group 'tabkey2)
;;(setq tabkey2-use-popup-menus nil)
;; (defcustom tabkey2-use-popup-menus (when (featurep 'popcmp) t)
;; "Use pop menus if available."
;; :type 'boolean
;; :group 'tabkey2)
;; (defvar tabkey2-preferred nil
;; "Preferred function for second tab key press.")
;; (make-variable-buffer-local 'tabkey2-preferred)
;; (put 'tabkey2-preferred 'permanent-local t)
(defvar tabkey2-fallback nil
"Fallback function for second tab key press.")
(make-variable-buffer-local 'tabkey2-fallback)
(put 'tabkey2-fallback 'permanent-local t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; State
(defvar tabkey2-overlay nil
"Show when tab key 2 action is to be done.")
(defvar tabkey2-keymap-overlay nil
"Hold the keymap for tab key 2.")
(defun tabkey2-completion-state-p ()
"Return t if Tab completion state should continue.
Otherwise return nil."
(when (and (eq (current-buffer) (overlay-buffer tabkey2-keymap-overlay))
(eq (overlay-get tabkey2-keymap-overlay 'window) (selected-window)))
(let* ((start (overlay-start tabkey2-keymap-overlay))
(end (overlay-end tabkey2-keymap-overlay))
(chars (append (buffer-substring-no-properties start end) nil)))
(and (not (memq ?\n chars))
(<= start (point))
(<= (point) end)))))
(defvar tabkey2-current-tab-info nil
"Saved information message for Tab completion state.")
(defvar tabkey2-current-tab-function nil
"Tab completion state current completion function.")
(make-variable-buffer-local 'tabkey2-current-tab-function)
(defun tabkey2-read-only-p ()
"Return non-nil if buffer seems to be read-only at point."
(or buffer-read-only
(get-char-property (min (+ 0 (point)) (point-max)) 'read-only)
(let ((remap (command-remapping 'self-insert-command (point))))
(memq remap '(Custom-no-edit)))))
;;;; Minor mode active after first tab
(defun tabkey2-get-highlight-face ()
(if (eq tabkey2-current-tab-function
(tabkey2-first-active-from-completion-functions))
'tabkey2-highlight-line
'tabkey2-highlight-line2))
(defun tabkey2-move-overlays ()
"Move overlays that mark the state and carries the state keymap."
(let* ((beg (let ((inhibit-field-text-motion t))
(line-beginning-position)))
(ind (current-indentation))
(end (+ beg 1)) ;(if (> ind 0) ind 1)))
(inhibit-read-only t))
(unless tabkey2-overlay
(setq tabkey2-overlay (make-overlay beg end)))
;; Fix-me: gets some strange errors, try avoid moving:
(unless (and (eq (current-buffer) (overlay-buffer tabkey2-overlay))
(= beg (overlay-start tabkey2-overlay))
(= end (overlay-end tabkey2-overlay)))
(move-overlay tabkey2-overlay beg end (current-buffer)))
;; Give it a high priority, it is very temporary
(overlay-put tabkey2-overlay 'priority 1000)
(if tabkey2-show-mark-on-active-line
(progn
(overlay-put tabkey2-overlay 'face
;;'tabkey2-highlight-line
(tabkey2-get-highlight-face)
)
(overlay-put tabkey2-overlay 'help-echo
"This highlight shows that Tab completion state is on"))
(overlay-put tabkey2-overlay 'face nil)
(overlay-put tabkey2-overlay 'help-echo nil)))
;; The keymap overlay
(let ((beg (line-beginning-position))
(end (line-end-position)))
;;(when (= end (point-max)) (setq end (1+ end)))
(setq beg (point))
(setq end (point))
(unless tabkey2-keymap-overlay
;; Make the rear of the overlay advance so that the keymap works
;; at the end of a line and the end of the buffer.
(setq tabkey2-keymap-overlay (make-overlay 0 0 nil nil t)))
(overlay-put tabkey2-keymap-overlay 'priority 1000)
;;(overlay-put tabkey2-keymap-overlay 'face 'secondary-selection)
(overlay-put tabkey2-keymap-overlay 'keymap
tabkey2-completion-state-emul-map)
(overlay-put tabkey2-keymap-overlay 'window (selected-window))
(move-overlay tabkey2-keymap-overlay beg end (current-buffer))))
(defun tabkey2-is-active (fun chk)
"Return t FUN is active.
Return t if CHK is a symbol with non-nil value or a form that
evals to non-nil.
Otherwise return t if FUN has a key binding at point."
(or (if (symbolp chk)
(when (boundp chk) (symbol-value chk))
(eval chk))
(let* ((emulation-mode-map-alists
;; Remove keymaps from tabkey2 in this copy:
(delq 'tabkey2--emul-keymap-alist
(copy-sequence emulation-mode-map-alists)))
(keys (tabkey2-symbol-keys fun))
kb-bound)
(dolist (key keys)
(unless (memq (car (append key nil))
'(menu-bar))
(setq kb-bound t)))
kb-bound)))
(defun tabkey2-is-active-p (fun)
"Return FUN is active.
Look it up in `tabkey2-completion-functions' to find out what to
check and return the value from `tabkey2-is-active'."
(let ((chk (catch 'chk
(dolist (rec tabkey2-completion-functions)
(when (eq fun (nth 1 rec))
(throw 'chk (nth 2 rec)))))))
(tabkey2-is-active fun chk)))
(defun tabkey2-first-active-from-completion-functions ()
"Return first active completion function.
Look in `tabkey2-completion-functions' for the first function
that has an active key binding."
(catch 'active-fun
(dolist (rec tabkey2-completion-functions)
(let ((fun (nth 1 rec))
(chk (nth 2 rec)))
(when (tabkey2-is-active fun chk)
(throw 'active-fun fun))))))
(defun tabkey2-get-default-completion-fun ()
"Return the default completion function.
See `tabkey2-first' for the list considered."
(or (when (and tabkey2-chosen-completion-function
(tabkey2-is-active-p
tabkey2-chosen-completion-function))
tabkey2-chosen-completion-function)
;;tabkey2-preferred
(tabkey2-first-active-from-completion-functions)
tabkey2-fallback))
(defvar tabkey2-completion-state-mode nil)
;;(make-variable-buffer-local 'tabkey2-completion-state-mode)
(defun tabkey2-completion-state-mode (arg)
"Tab completion state minor mode.
This pseudo-minor mode holds the 'Tab completion state'. When this
minor mode is on completion key bindings are available.
With ARG a positive number turn on, otherwise turn off this minor
mode.
See `tabkey2-first' for more information."
;;(assq-delete-all 'tabkey2-completion-state-mode minor-mode-alist)
(unless (assoc 'tabkey2-completion-state-mode minor-mode-alist)
;;(setq minor-mode-alist (cons '(tabkey2-completion-state-mode " Tab2")
(setq minor-mode-alist (cons (list 'tabkey2-completion-state-mode
tabkey2-completion-lighter)
minor-mode-alist)))
(let ((emul-map (cdr (car tabkey2--emul-keymap-alist)))
(old-wincfg tabkey2-completion-state-mode))
(setq tabkey2-completion-state-mode (when (and (numberp arg)
(> arg 0))
;;t
(current-window-configuration)
))
(if tabkey2-completion-state-mode
(progn
;; Set default completion function
(tabkey2-make-message-and-set-fun
(tabkey2-get-default-completion-fun))
;; Message
;;(setq tabkey2-message-is-shown nil)
(when tabkey2-show-message-on-enter
(tabkey2-show-current-message
(when (numberp tabkey2-show-message-on-enter)
tabkey2-show-message-on-enter)))
;; Move overlays
(tabkey2-move-overlays)
;; Work around eob keymap problem ...
;;(set-keymap-parent emul-map (overlay-get tabkey2-keymap-overlay
;; 'keymap))
;; Set up for pre/post-command-hook
(add-hook 'pre-command-hook 'tabkey2-pre-command)
(add-hook 'post-command-hook 'tabkey2-post-command))
;;(set-keymap-parent emul-map nil)
(setq tabkey2-current-tab-function nil)
(when (and old-wincfg
tabkey2-keymap-overlay
(eq (overlay-get tabkey2-keymap-overlay 'window) (selected-window))
(not (active-minibuffer-window)))
(set-window-configuration old-wincfg))
(let ((inhibit-read-only t))
(when tabkey2-keymap-overlay
(delete-overlay tabkey2-keymap-overlay))
(when tabkey2-overlay
(delete-overlay tabkey2-overlay)))
(remove-hook 'pre-command-hook 'tabkey2-pre-command)
(remove-hook 'post-command-hook 'tabkey2-post-command)
(tabkey2-overlay-message nil)
;;(message "")
)))
(defun tabkey2-completion-state-off ()
"Quit Tab completion state."
(interactive)
(tabkey2-completion-state-mode -1)
(message "Quit"))
(defvar tabkey2-message-is-shown nil)
(defun tabkey2-message-is-shown ()
(case tabkey2-message-style
('popup
(when tabkey2-overlay-message
(overlay-buffer tabkey2-overlay-message)))
('echo-area
(get (current-message) 'tabkey2))))
(defun tabkey2-pre-command ()
"Run this in `pre-command-hook'.
Check if message is shown.
Remove overlay message.
Cancel delayed message."
;;(message "=====> tabkey2-pre-command")
(condition-case err
(progn
(setq tabkey2-message-is-shown (tabkey2-message-is-shown))
;;(message "tabkey2-overlay-message=%s, is-shown=%s" tabkey2-overlay-message tabkey2-message-is-shown)
(tabkey2-overlay-message nil)
(tabkey2-cancel-delayed-message)
;;(message "here buffer=%s, this-command=%s" (current-buffer) this-command)
)
(error (message "tabkey2 pre: %s" (error-message-string err)))))
(defun tabkey2-post-command ()
"Turn off Tab completion state if not feasable any more.
This is run in `post-command-hook' after each command."
(condition-case err
(save-match-data
;; Delayd messages
(if (not (tabkey2-completion-state-p))
(tabkey2-completion-state-mode -1)
;;(message "tabkey2-current-tab-function=%s" tabkey2-current-tab-function)
(tabkey2-move-overlays)))
(error (message "tabkey2 post: %s" (error-message-string err)))))
(defun tabkey2-minibuffer-setup ()
"Activate/deactivate function `tabkey2-mode' in minibuffer."
(set (make-local-variable 'tabkey2-mode)
(and tabkey2-mode
tabkey2-in-minibuffer))
(unless tabkey2-mode
(set (make-local-variable 'emulation-mode-map-alists)
(delq 'tabkey2--emul-keymap-alist
(copy-sequence emulation-mode-map-alists)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Message functions
;; Fix-me: is something like this included in Emacs now? reveal.el
;; gets it wrong.
(unless (fboundp 'invisible-p)
(defun invisible-p (pos)
"Return non-nil if the character after POS is currently invisible."
(let ((prop
(get-char-property pos 'invisible)))
(if (eq buffer-invisibility-spec t)
prop
(if (listp prop)
(catch 'invis
(dolist (p prop)
(when (or (memq p buffer-invisibility-spec)
(assq p buffer-invisibility-spec))
(throw 'invis t))))
(or (memq prop buffer-invisibility-spec)
(assq prop buffer-invisibility-spec)))))))
(defvar tabkey2-overlay-message nil)
;; (defun test-scroll ()
;; (interactive)
;; (setq debug-on-error t)
;; (let* ((buffer-name "test-scroll")
;; (buffer (get-buffer buffer-name)))
;; (when buffer (kill-buffer buffer))
;; (setq buffer (get-buffer-create buffer-name))
;; (switch-to-buffer buffer)
;; (message "here 1") (sit-for 1)
;; (condition-case err
;; (scroll-up 1)
;; (error (message "scroll-up error: %s" err)
;; (sit-for 1)))
;; (message "here 2") (sit-for 1)
;; (scroll-up 1)
;; (message "here 3") (sit-for 1)
;; ))
(defun tabkey2-overlay-message (txt)
"Display TXT below or above current line using an overlay."
;;(setq tabkey2-message-is-shown txt)
(if (not txt)
(when tabkey2-overlay-message
(delete-overlay tabkey2-overlay-message)
(setq tabkey2-overlay-message nil))
(let ((ovl tabkey2-overlay-message)
(column (current-column))
(txt-len (length txt))
(here (point))
beg end
(before "")
(after "")
ovl-str too-much
(is-eob (eobp))
(direction 1))
(unless ovl (setq ovl (make-overlay 0 0)))
(when tabkey2-overlay-message
(delete-overlay tabkey2-overlay-message))
(setq tabkey2-overlay-message ovl)
(when is-eob
(setq direction -1))
(when (and (/= (point-min) (window-start))
(not (pos-visible-in-window-p (min (point-max) (1+ (line-end-position))))))
;; Go back inside window to avoid aggressive scrolling:
(forward-line -1)
(scroll-up 1)
(forward-line 1))
(forward-line direction)
;; Fix-me: Emacs bug workaround
(if (when (< 1 (point))
(invisible-p (1- (line-end-position))))
(progn
(goto-char here)
(tabkey2-echo-area-message txt))
;; Fix-me: Does this really do anything now:
(when (invisible-p (point))
(while (invisible-p (point))
(forward-line direction)))
(setq beg (line-beginning-position))
(setq end (line-end-position))
(if (or (invisible-p beg) (invisible-p end))
;; Give up, do not fight invisibility:
(progn
(tabkey2-overlay-message nil)
(tabkey2-echo-area-message txt))
;; string before
(move-to-column column)
(setq before (buffer-substring beg (point)))
(when (< (current-column) column)
(setq before
(concat before
(make-string (- column (current-column)) ? ))))
(setq too-much (- (+ 1 txt-len (length before))
(window-width)))
(when (> too-much 0)
(setq before (substring before 0 (- too-much))))
(unless (> too-much 0)
(move-to-column (+ txt-len (length before)))
(setq after (buffer-substring (point) end)))
(setq ovl-str (concat before
(propertize txt 'face 'tabkey2-highlight-popup)
after
))
(overlay-put ovl 'after-string ovl-str)
(overlay-put ovl 'display "")
(overlay-put ovl 'window (selected-window))
(move-overlay ovl beg end (current-buffer)))
(goto-char here)
))))
;; Fix-me: This was not usable IMO. Too much flickering.
;; (defun tabkey2-tooltip (txt)
;; (let* ((params tooltip-frame-parameters)
;; (coord (car (point-to-coord (point))))
;; (left (car coord))
;; (top (cadr coord))
;; tooltip-frame-parameters
;; )
;; ;; Fix-me: how do you get char height??
;; (setq top (+ top 50))
;; (setq params (tooltip-set-param params 'left left))
;; (setq params (tooltip-set-param params 'top top))
;; (setq params (tooltip-set-param params 'top top))
;; (setq tooltip-frame-parameters params)
;; (tooltip-hide)
;; (tooltip-show txt nil)))
(defun tabkey2-echo-area-message (txt)
"Show TXT in the echo area with a special face.
Shown with the face `tabkey2-highlight-message'."
(message "%s" (propertize txt
'face 'tabkey2-highlight-message
'tabkey2 t)))
(defun tabkey2-deliver-message (txt)
"Show message TXT to user."
(case tabkey2-message-style
(popup (tabkey2-overlay-message txt))
(t (tabkey2-echo-area-message txt))))
(defun tabkey2-timer-deliver-message (txt where)
"Show message TXT to user.
Protect from errors cause this is run during a timer."
(when (and tabkey2-completion-state-mode
(equal (point-marker) where))
(condition-case err
(tabkey2-deliver-message txt)
(error (message "tabkey2-timer-deliver-message: %s"
(error-message-string err))))))
(defvar tabkey2-delayed-timer nil)
(defun tabkey2-cancel-delayed-message ()
"Cancel delayed message."
(when tabkey2-delayed-timer
(cancel-timer tabkey2-delayed-timer)
(setq tabkey2-delayed-timer)))
(defun tabkey2-maybe-delayed-message (txt delay)
"Show message TXT, delay it if DELAY is non-nil."
(if delay
(setq tabkey2-delayed-timer
(run-with-idle-timer
delay nil
'tabkey2-timer-deliver-message txt (point-marker)))
(tabkey2-deliver-message txt)))
(defun tabkey2-message (delay format-string &rest args)
"Show, if DELAY delayed, otherwise immediately message.
FORMAT-STRING and ARGS are like for `message'."
(let ((txt (apply 'format format-string args)))
(tabkey2-maybe-delayed-message txt delay)))
(defun tabkey2-show-current-message (&optional delay)
"Show current completion message, delayed if DELAY is non-nil."
(tabkey2-cancel-delayed-message)
(tabkey2-message delay "%s" tabkey2-current-tab-info))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Completion function selection etc
(defun tabkey2-symbol-keys (comp-fun)
"Get a list of all key bindings for COMP-FUN."
(let* ((remapped (command-remapping comp-fun)))
(where-is-internal comp-fun
nil ;;overriding-local-map
nil nil remapped)))
(defun tabkey2-get-active-completion-functions ()
"Get a list of active completion functions.
Consider only those in `tabkey2-completion-functions'."
(delq nil
(mapcar (lambda (rec)
(let ((fun (nth 1 rec))
(chk (nth 2 rec)))
(when (tabkey2-is-active fun chk) rec)))
tabkey2-completion-functions)))
(defvar tabkey2-chosen-completion-function nil)
(make-variable-buffer-local 'tabkey2-chosen-completion-function)
(put 'tabkey2-chosen-completion-function 'permanent-local t)
(defun tabkey2-make-current-default ()
"Make current Tab completion function default.
Set the current Tab completion function at point as default for
the current buffer."
(interactive)
(let ((set-it
(y-or-n-p
(format
"Make %s default for Tab completion in current buffer? "
tabkey2-current-tab-function))))
(when set-it
(setq tabkey2-chosen-completion-function
tabkey2-current-tab-function))
(unless set-it
(when (local-variable-p 'tabkey2-chosen-completion-function)
(when (y-or-n-p "Use default Tab completion selection in buffer? ")
(setq set-it t))
(kill-local-variable 'tabkey2-chosen-completion-function)))
(when (tabkey2-completion-state-p)
(tabkey2-message nil "%s%s" tabkey2-current-tab-info
(if set-it " - Done" "")))))
(defun tabkey2-activate-next-completion-function ()
(let* ((active (mapcar (lambda (rec)
(nth 1 rec))
(tabkey2-get-active-completion-functions)))
(first (car active))
next)
;;(message "is-shown=%s current=%s active=%s overlay=%s" tabkey2-message-is-shown tabkey2-current-tab-function active tabkey2-overlay)
(when tabkey2-current-tab-function
(while (and active (not next))
(when (eq (car active) tabkey2-current-tab-function)
(setq next (cadr active)))
(setq active (cdr active))))
(unless next (setq next first))
;;(if (eq first next)
(tabkey2-make-message-and-set-fun next)))
(defun tabkey2-cycle-completion-functions (prefix)
"Cycle through cnd display ompletion functions.
If 'Tab completion state' is not on then turn it on.
If PREFIX is given just show what this command will do."
(interactive "P")
(if (tabkey2-read-only-p)
(message "Buffer is read only at point")