-
Notifications
You must be signed in to change notification settings - Fork 9
/
auto-highlight-symbol.el
1686 lines (1463 loc) · 53.9 KB
/
auto-highlight-symbol.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
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
;;; auto-highlight-symbol.el --- Automatic highlighting current symbol minor mode
;; Copyright (C) 2009 2010 Mitsuo Saito
;; Created date 2009-03-03 21:44 +0900
;; Author: Mitsuo Saito <[email protected]>
;; Version: 1.54 beta
;; Keywords: highlight face match convenience
;; URL: http://github.com/mitsuo-saito/auto-highlight-symbol-mode/raw/master/auto-highlight-symbol.el
;; Compatibility: GNU Emacs 22.3 23.x 24.x later
;;
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; If you have `linkd.el' turn on `linkd-mode'
;; and (setq linkd-use-icons t ) more easily navigation.
;; You can get `linkd.el' here:
;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
;; http://www.emacswiki.org/emacs/linkd.tar.gz -- with cool icon
;;
;;; (@* "Index" )
;;
;; (@> "What's this") I am ...
;; (@> "Setup") Basic setup
;; (@> "Screencast") Screencast
;; (@> "Mode map") Key binding
;;
;; (@> "Note") Performance note
;;
;; (@> "Custom variable") Customizable varible
;; (@> "Face") Face used in auto-highlight-symbol-mode
;; (@> "Highlight Rules") Whether to highlight the symbol.
;;
;; (@> "Internal variable") Internal variables
;; (@> "Logging") Log data
;; (@> "Range plugin") Range plugin functions
;; (@> "Built-in plugin") Built-in plugin section
;; (@> "Timer") Timer functions
;; (@> "Idle") Idle functions
;; (@> "Highlight") Highlight functions
;; (@> "Edit mode") Edit mode functions
;; (@> "Select") Selective functions
;; (@> "Misc") Miscellaneous
;; (@> "Interactive") Interactive functions
;; (@> "Define mode") Mode definition
;; (@> "Revert") Protect from revert-buffer
;;
;;; (@* "What's this" )
;;
;; A minor mode for emacs.
;;
;; * automatic highlighting current symbol like eclipse IDE.
;; * cycle through highlighted locations.
;; * can specify the range to highlight.
;; * can edit the highlighted symbols at a time.
;;
;; Tested on GNU Emacs 22.3/23.2/24.0.50
;;
;;; (@* "Setup" )
;;
;; Basic steps to setup:
;; 1. Place `auto-highlight-symbol.el' in your `load-path'.
;;
;; 2. In your `.emacs.el' file
;; (require 'auto-highlight-symbol)
;; (global-auto-highlight-symbol-mode t)
;;
;;; (@* "Screencast" )
;;
;; Screencast on YouTube and ScreenToaster
;; YouTube -- http://www.youtube.com/watch?v=xzJ2r4-s7fo
;; ScreenToaster -- http://www.screentoaster.com/watch/stUE9VQ0dMRFtXRlVeU19cX1Bd/auto_highlight_symbol_mode_screencast
;;
;;
;;; More Information (currently underconstruction)
;; See also http://github.com/mitsuo-saito/auto-highlight-symbol-mode/wiki/
;;
;;; Commands:
;;
;; Below are complete command list:
;;
;; `ahs-forward'
;; Select highlighted symbols forwardly.
;; `ahs-backward'
;; Select highlighted symbols backwardly.
;; `ahs-forward-definition'
;; Select highlighted symbols forwardly. only symbol definition.
;; `ahs-backward-definition'
;; Select highlighted symbols backwardly. only symbol definition.
;; `ahs-back-to-start'
;; Go back to the starting point.
;; `ahs-change-range'
;; Current plugin change to `RANGE' plugin. `RANGE' defaults to next runnable plugin.
;; `ahs-set-idle-interval'
;; Set wait until highlighting symbol when emacs is idle.
;; `ahs-display-stat'
;; Display current status.
;; `ahs-highlight-now'
;; Highlight NOW!!
;; `ahs-goto-web'
;; Go to official? web site.
;; `ahs-edit-mode'
;; Turn on edit mode. With a prefix argument, current plugin change to `whole buffer' temporary.
;; `auto-highlight-symbol-mode'
;; Toggle Auto Highlight Symbol Mode
;;
;;; Customizable Options:
;;
;; Below are customizable option list:
;;
;; `ahs-modes'
;; Major modes `auto-highlight-symbol-mode' can run on.
;; `ahs-suppress-log'
;; *Non-nil means suppress log message.
;; `ahs-log-echo-area-only'
;; *Non-nil means log doesn't display the `*Messages*' buffer.
;; `ahs-decorate-log'
;; *Non-nil means decorate logs.
;; `ahs-default-range'
;; Default Plugin.
;; `ahs-edit-mode-lighter-pair'
;; Decorate mode line lighter in edit mode.
;; `ahs-select-invisible'
;; Behavior when selected symbol in hidden text.
;; `auto-highlight-symbol-mode-hook'
;; Hook for `auto-highlight-symbol-mode'.
;; `ahs-edit-mode-on-hook'
;; Normal hook for run when entering edit mode.
;; `ahs-edit-mode-off-hook'
;; Normal hook for run when go out edit mode.
;; `ahs-idle-interval'
;; Number of seconds to wait before highlighting symbol.
;; `ahs-case-fold-search'
;; *Non-nil means symbol search ignores case.
;; `ahs-include'
;; Variable for start highlighting.
;; `ahs-exclude'
;; Variable for inhibit highlighting.
;; `ahs-face-check-include-overlay'
;; *Non-nil means face checks include overlay face.
;; `ahs-inhibit-face-list'
;; Face list for inhibit highlighting.
;; `ahs-definition-face-list'
;; Face list for higilight definition.
;; `ahs-plugin-bod-modes'
;; Major modes `beginning of defun' plugin can run on.
;; `ahs-plugin-bod-function'
;; Function used in `beginning of defun' plugin.
;;
;; Happy Coding !!
;;
;;; SCM Log
;;
;; $Revision: 243:6aa59061b1df tip $
;; $Commiter: Mitso Saito <[email protected]> $
;; $LastModified: Sun, 21 Nov 2010 14:42:11 +0900 $
;;
;; $Lastlog: font lock again $
;;
;;; (@* "Changelog" )
;;
;; v1.54 beta
;; Bug fix release
;; ** fix overlay violation problem in edit mode(backward) - !incomplete!
;; fix font-lock problem
;; fix built-in plugin
;; add onekey edit
;; remove ahs-invisible-face-list
;; remove obsoleted alias
;; minor bug fix
;;
;; v1.53 2010-11-03 22:17 +0900
;; improve invisible overlay's handling
;; new plugin property `face' available
;; add ahs-back-to-start
;; minor bug fix
;;
;; v1.52 2010-10-31 14:46 +0900
;; skip folding(select function only)
;;
;; v1.51 2010-10-30 09:17 +0900
;; plugin minor change
;;
;; v1.5 2010-10-30 02:31 +0900
;; add range plugin
;; ahs-whole-of-buffer is not working.
;; use ahs-default-range instead.
;; ahs-mode-lighter , ahs-wmode-lighter is not be used
;;
;; v1.03 2010-10-28 07:00 +0900
;; bug fix
;;
;; v1.02 2010-10-26 23:39 +0900
;; minor fix
;;
;; v1.01 2010-10-26 20:50 +0900
;; add edit mode hook for protect overlay
;;
;; v1.0 2010-10-26 16:33 +0900
;; first release
;;
;;; (@* "TODO" )
;;
;; fix overlay violation problem
;; fix poor doc
;; face check tweak
;; add onekey-***
;; refactor
;; cleanup
;; add comment
;;
;;; Code:
(eval-when-compile
;; Suppress bytecompiler error warning
(require 'easy-mmode)
(require 'cl)
(defvar dropdown-list-overlays nil))
(eval-and-compile
(defconst ahs-web "http://github.com/mitsuo-saito/auto-highlight-symbol-mode/")
;; Compatibility
(if (or (>= emacs-major-version 24)
(and (= emacs-major-version 23)
(>= emacs-minor-version 2)))
(defalias 'ahs-called-interactively-p 'called-interactively-p)
(defmacro ahs-called-interactively-p (&optional arg)
'(called-interactively-p))))
(defconst ahs-mode-vers
"$Id: auto-highlight-symbol.el,v 243:6aa59061b1df 2010-11-21 14:42 +0900 arch320 $"
"auto-highlight-symbol-mode version.")
;;
;; (@* "Custom variable" )
;;
(defgroup auto-highlight-symbol nil
"Automatic highlighting current symbol minor mode"
:group 'convenience
:link `(url-link :tag "Download latest version"
,(eval-when-compile (concat ahs-web "raw/master/auto-highlight-symbol.el")))
:link `(url-link :tag "Wiki" ,(eval-when-compile (concat ahs-web "wiki/")))
:link `(url-link :tag "Information" ,(eval-when-compile ahs-web)))
(defcustom ahs-modes
'( actionscript-mode
apache-mode
bat-generic-mode
c++-mode
c-mode
csharp-mode
css-mode
dos-mode
emacs-lisp-mode
html-mode
ini-generic-mode
java-mode
javascript-mode
js-mode
lisp-interaction-mode
lua-mode
latex-mode
makefile-mode
makefile-gmake-mode
markdown-mode
moccur-edit-mode
nxml-mode
nxhtml-mode
outline-mode
perl-mode cperl-mode
php-mode
python-mode
rc-generic-mode
reg-generic-mode
ruby-mode
sgml-mode
sh-mode
squirrel-mode
text-mode
tcl-mode
visual-basic-mode )
"Major modes `auto-highlight-symbol-mode' can run on."
:group 'auto-highlight-symbol
:type '(repeat symbol))
(defcustom ahs-suppress-log t
"*Non-nil means suppress log message."
:group 'auto-highlight-symbol
:type 'boolean)
(defcustom ahs-log-echo-area-only t
"*Non-nil means log doesn't display the `*Messages*' buffer."
:group 'auto-highlight-symbol
:type 'boolean)
(defcustom ahs-decorate-log t
"*Non-nil means decorate logs."
:group 'auto-highlight-symbol
:type 'boolean)
(defcustom ahs-default-range 'ahs-range-display
"Default Plugin."
:group 'auto-highlight-symbol
:type '(choice (symbol :tag "Display area" ahs-range-display)
(symbol :tag "Whole buffer" ahs-range-whole-buffer)))
(defcustom ahs-edit-mode-lighter-pair '( "*" . "*" )
"Decorate mode line lighter in edit mode."
:group 'auto-highlight-symbol
:type '(choice (cons :tag "Asterisk" (string "*") (string "*"))
(cons :tag "Exclamation" (string "!") (string "!"))
(cons :tag "DANGEROUS" (string "DANGER->") (string "<-DANGER"))
(cons :tag "Silence!!" (string "") (string ""))))
(defcustom ahs-select-invisible 'immediate
"Behavior when selected symbol in hidden text.
When the value is
`immediate' Open hidden text. When leaving opened text, close it immediately.
`temporary' Open hidden text. When unhighlight or change plugin, close the opened texts except selected.
`open' Open hidden text permanently.
`skip' Select next visible symbol.
Affects only overlay(hidden text) has a property `isearch-open-invisible'."
:group 'auto-highlight-symbol
:type '(choice (const :tag "Open hidden text only when necessary" immediate)
(const :tag "Open hidden text temporary" temporary)
(const :tag "Open hidden text permanently" open)
(const :tag "Skip over all symbols in hidden text" skip)))
(defcustom auto-highlight-symbol-mode-hook nil
"Hook for `auto-highlight-symbol-mode'."
:group 'auto-highlight-symbol
:type 'hook)
(defcustom ahs-edit-mode-on-hook nil
"Normal hook for run when entering edit mode."
:group 'auto-highlight-symbol
:type 'hook)
(defcustom ahs-edit-mode-off-hook nil
"Normal hook for run when go out edit mode."
:group 'auto-highlight-symbol
:type 'hook)
(defvar ahs-idle-timer nil
"Timer used to highlighting symbol whenever emacs is idle.")
(defcustom ahs-idle-interval 1.0
"Number of seconds to wait before highlighting symbol."
:group 'auto-highlight-symbol
:type 'float
:set (lambda (symbol value)
(set-default symbol value)
(when (timerp ahs-idle-timer)
(cancel-timer ahs-idle-timer)
(setq ahs-idle-timer nil)
(ahs-start-timer))))
;;
;; (@* "Face" )
;;
(defface ahs-face
'((t (:foreground "GhostWhite" :background "LightYellow4")))
"Highlight the symbol using this face."
:group 'auto-highlight-symbol)
(defvar ahs-face 'ahs-face)
(defface ahs-definition-face
'((t (:foreground "moccasin" :background "CadetBlue" :underline t)))
"Highlight the symbol definition using this face."
:group 'auto-highlight-symbol)
(defvar ahs-definition-face 'ahs-definition-face)
(defface ahs-warning-face
'((t (:foreground "Red" :bold t)))
"Face for warning message."
:group 'auto-highlight-symbol)
(defvar ahs-warning-face 'ahs-warning-face)
(defface ahs-plugin-defalt-face
'((t (:foreground "Black" :background "Orange1")))
"Face used in `display' plugin."
:group 'auto-highlight-symbol)
(defvar ahs-plugin-defalt-face 'ahs-plugin-defalt-face)
(defface ahs-plugin-whole-buffer-face
'((t (:foreground "Black" :background "GreenYellow")))
"Face used in `whole buffer' plugin."
:group 'auto-highlight-symbol)
(defvar ahs-plugin-whole-buffer-face 'ahs-plugin-whole-buffer-face)
(defface ahs-plugin-bod-face
'((t (:foreground "Black" :background "DodgerBlue")))
"Face used in `beginning of defun' plugin."
:group 'auto-highlight-symbol)
(defvar ahs-plugin-bod-face 'ahs-plugin-bod-face)
(defface ahs-edit-mode-face
'((t (:foreground "White" :background "Coral3")))
"Face used in edit mode."
:group 'auto-highlight-symbol)
(defvar ahs-edit-mode-face 'ahs-edit-mode-face)
;;
;; (@* "Highlight Rules" )
;;
(defconst ahs-default-symbol-regexp "\\(?:\\s_\\|\\sw\\)+$"
"Default symbol regular expression.")
(defcustom ahs-include ahs-default-symbol-regexp
"Variable for start highlighting.
This variable can be set in three different types.
1. `REGEXP' Regular expression.
If symbol matches regular expression `REGEXP' then start highlighting.
2. `my-include-function' Function predicate.
If return value is Non-nil then start highlighting.
Function is called with one argument, the symbol.
3. `alist'
'(
( emacs-lisp-mode . \"REGEXP\") ;; Regular expression in emacs-lisp-mode
( php-mode . my-include-function) ;; Function predicate in php-mode
)
If major mode not in list `ahs-default-symbol-regexp' will be used instead."
:group 'auto-highlight-symbol
:type '(choice (regexp :tag "Regexp" ahs-default-symbol-regexp)
(symbol :tag "Function" function)
(alist :tag "alist")))
(defcustom ahs-exclude nil
"Variable for inhibit highlighting.
This variable can be set in three different types.
1. `REGEXP' Regular expression.
If symbol matches regular expression `REGEXP' then inhibit highlighting.
2. `my-exclude-function' Function predicate.
If return value is Non-nil then inhibit highlighting.
Function is called with one argument, the symbol.
3. `alist'
'(
( ruby-mode . \"\\_<\\(end\\|def\\|class\\)\\_>\") ;; Regular expression in ruby-mode
( dos-mode . i-hate-wxxxxxs) ;; Function predicate in dos-mode
)
If major mode not in list all symbols can be highlighted."
:group 'auto-highlight-symbol
:type '(choice (const :tag "All symbols can be highlighted" nil)
(regexp :tag "Regexp" "")
(symbol :tag "Function" function)
(alist :tag "alist")))
(defcustom ahs-face-check-include-overlay nil
"*Non-nil means face checks include overlay face."
:group 'auto-highlight-symbol
:type 'boolean)
(defcustom ahs-inhibit-face-list
'( font-lock-comment-delimiter-face
font-lock-comment-face
font-lock-doc-face
font-lock-doc-string-face
font-lock-string-face )
"Face list for inhibit highlighting."
:group 'auto-highlight-symbol
:type '(repeat symbol))
(defcustom ahs-definition-face-list
'( font-lock-function-name-face
font-lock-variable-name-face )
"Face list for higilight definition."
:group 'auto-highlight-symbol
:type '(repeat symbol))
;;
;; (@* "Mode map" )
;;
(defvar auto-highlight-symbol-mode-map nil
"Keymap used in `auto-highlight-symbol-mode'.")
(if auto-highlight-symbol-mode-map
nil
(setq auto-highlight-symbol-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-<left>" ) 'ahs-backward )
(define-key map (kbd "M-<right>" ) 'ahs-forward )
(define-key map (kbd "M-S-<left>" ) 'ahs-backward-definition )
(define-key map (kbd "M-S-<right>" ) 'ahs-forward-definition )
;; (define-key map (kbd "M--" ) 'ahs-back-to-start )
;; (define-key map (kbd "C-x C-'" ) 'ahs-change-range )
;; (define-key map (kbd "C-x C-a" ) 'ahs-edit-mode )
map)))
(defmacro ahs-onekey-edit (keys plugin-name &optional keep keymap)
"Macro of One Key Edit.
ahs-change-range -> ahs-edit-mode -> editing... ->
ahs-edit-mode(off) -> ahs-change-range... sigh...
You can do these operations at One Key!
`KEYS' Keyboard macro
`PLUGIN-NAME' Plugin name
`KEEP' Keep plugin after exiting edit mode.
`KEYMAP' Keymap If value is `nil' or not keymap
`auto-highlight-symbol-mode-map' will be used instead.
ex.(ahs-onekey-edit \"C-x C-y\" beginning-of-defun)"
`(define-key (if (keymapp ,keymap)
,keymap
auto-highlight-symbol-mode-map)
(read-kbd-macro ,keys)
(lambda()
(interactive)
(ahs-onekey-edit-function ',plugin-name ,keep))))
(defmacro ahs-onekey-change (keys plugin-name &optional keymap)
"Macro of change plugin.
`KEYS' Keyboard macro
`PLUGIN-NAME' Plugin name
`KEYMAP' Keymap If value is `nil' or not keymap
`auto-highlight-symbol-mode-map' will be used instead.
ex.(ahs-onekey-change \"C-x C-y\" display)"
`(define-key (if (keymapp ,keymap)
,keymap
auto-highlight-symbol-mode-map)
(read-kbd-macro ,keys)
',(intern (format "ahs-chrange-%s" plugin-name))))
;;
;; (@* "Internal variable" )
;;
(defvar auto-highlight-symbol-mode nil
"Dummy for suppress bytecompiler warning.")
(defvar ahs-inhibit-modification-commands
'( undo
redo ))
(defvar ahs-unhighlight-allowed-commands
'( universal-argument
universal-argument-other-key
ahs-back-to-start
ahs-backward
ahs-backward-definition
ahs-display-stat
ahs-edit-mode
ahs-forward
ahs-forward-definition ))
(defvar ahs-range-plugin-list nil
"List of installed plugin.")
(defvar ahs-search-work nil)
(defvar ahs-need-fontify nil)
;; Buffer local variable
(defvar ahs-current-overlay nil)
(defvar ahs-current-range nil)
(defvar ahs-edit-mode-enable nil)
(defvar ahs-highlighted nil)
(defvar ahs-inhibit-modification nil)
(defvar ahs-mode-line nil)
(defvar ahs-onekey-range-store nil)
(defvar ahs-opened-overlay-list nil)
(defvar ahs-overlay-list nil)
(defvar ahs-start-modification nil)
(defvar ahs-start-point nil)
(defvar ahs-case-fold-search t
"*Non-nil means symbol search ignores case.")
(make-variable-buffer-local 'ahs-current-overlay )
(make-variable-buffer-local 'ahs-current-range )
(make-variable-buffer-local 'ahs-edit-mode-enable )
(make-variable-buffer-local 'ahs-highlighted )
(make-variable-buffer-local 'ahs-inhibit-modification )
(make-variable-buffer-local 'ahs-mode-line )
(make-variable-buffer-local 'ahs-onekey-range-store )
(make-variable-buffer-local 'ahs-opened-overlay-list )
(make-variable-buffer-local 'ahs-overlay-list )
(make-variable-buffer-local 'ahs-start-modification )
(make-variable-buffer-local 'ahs-start-point )
(make-variable-buffer-local 'ahs-case-fold-search )
;;
;; (@* "Logging" )
;;
(defconst ahs-log-data
'(;; plugin
( plugin-badcondition . "Plugin `%s' incorrect major-mode or condition property is `nil'.")
( plugin-changed . "Current plugin has been changed to `%s'.")
( plugin-notfound . "Plugin `%s' doesn't exist.")
( plugin-notplugin . "Plugin `%s' wrong type plugin.")
( plugin-error-log1 . "---- auto-highlight-symbol-mode plugin error log ----")
( plugin-error-log2 . "%s in `%s' plugin `%s' property")
( plugin-error-log3 . "---- end")
( plugin-error-log4 . "Plugin error occurred. see *Messages*. Current plugin has been changed to `%s'.")
;; error
( error-ahs-disable . "`auto-highlight-symbol-mode' is not working at current buffer.")
( error-read-only . "Buffer is read-only: `%s'")
( error-scan-sexp . "%s: \"%s\" %s %s")
;; edit-mode
( turn-on-edit-mode . "Entering edit mode. %s")
( turn-off-edit-mode . "Exited edit mode.")
( onekey-turn-on-edit-mode . "Entering edit mode. Current plugin has been changed to `%s'. %s")
( onekey-turn-off-edit-mode . "Exited edit mode. Current plugin has been changed to `%s'.")
( onekey-no-symbol-at-point . "No symbol to highlight at point. Current plugin is `%s' now.")
;; misc
( no-symbol-at-point . "No symbol to highlight at point.")
( exist-elsewhere . "%s symbols exist elsewhere.")
( stat . "Current plugin `%s' matched %s displayed %s hidden %s before %s after %s.")
( self . "%s")
)
"Log data")
(defmacro ahs-decorate-if (body face)
`(if ahs-decorate-log
(propertize ,body 'face ,face)
,body))
(defmacro ahs-log-format (key)
`(cdr (assoc ,key ahs-log-data)))
(defun ahs-log (key &rest args)
"Display log."
(unless ahs-suppress-log
(let* ((data (ahs-log-format key))
(msg (apply 'format data args))
(message-log-max
(not ahs-log-echo-area-only)))
(message "%s" msg))) nil)
;;
;; (@* "Range plugin" )
;;
(defmacro ahs-regist-range-plugin (plugin-name body &optional docstring)
"Macro of regist range plugin.
\(fn PLUGIN-NAME BODY [DOCSTRING])"
(declare (indent 1))
`(progn
(defvar ,(intern (format "ahs-range-%s" plugin-name))
nil ,docstring)
(setq ,(intern (format "ahs-range-%s" plugin-name)) ,body)
(add-to-list 'ahs-range-plugin-list ',(intern (format "ahs-range-%s" plugin-name)))
(defun ,(intern (format "ahs-chrange-%s" plugin-name)) ()
(interactive)
(ahs-change-range ',(intern (format "ahs-range-%s" plugin-name)))
(when (ahs-called-interactively-p 'interactive)
(ahs-idle-function)))))
(defun ahs-decorated-current-plugin-name ()
"Return decorated current plugin's name."
(let ((name (ahs-current-plugin-prop 'name)))
(if ahs-decorate-log
(propertize name 'face (ahs-current-plugin-prop 'face))
name)))
(defun ahs-plugin-error-message (err prop range)
"Display plugin error message."
(let ((ahs-suppress-log)
(ahs-log-echo-area-only))
(ahs-log 'plugin-error-log1)
(ahs-log 'plugin-error-log2
err (ahs-get-plugin-prop 'name range) prop) ;; infinite loop? if 'name is badly function
(ahs-log 'plugin-error-log3)
(ahs-change-range-internal ahs-default-range)
(ahs-log 'plugin-error-log4
(ahs-decorated-current-plugin-name))))
(defun ahs-get-plugin-prop (prop range &optional arg)
"Return value of the `PROP' property of the `RANGE' plugin."
(let ((value (cdr (assoc prop (symbol-value range)))))
(cond
((equal value 'abort) 'abort) ;; abort
((equal prop 'face) ;; face
(cond
((facep value)
value)
((functionp value)
(condition-case err
(funcall value)
(error err
(ahs-plugin-error-message err prop range)
'abort)))
(t
ahs-plugin-defalt-face)))
((and (functionp value)
(equal prop 'major-mode)) value) ;; major-mode
((functionp value) ;; function
(condition-case err
(if arg
(funcall value arg)
(funcall value))
(error err
(ahs-plugin-error-message err prop range)
'abort)))
((null value) 'none) ;; property not found
((symbolp value) ;; symbol
(ignore-errors
(symbol-value value)))
(t value)))) ;; others
(defun ahs-current-plugin-prop (prop &optional arg)
"Return value of the `PROP' property of the current plugin."
(ahs-get-plugin-prop prop 'ahs-current-range arg))
(defun ahs-valid-plugin-p (range &optional plugin-name)
"Return Non-nil if `RANGE' plugin can run."
(setq plugin-name
(or plugin-name
(let ((name (format "%s" range)))
(if (string-match "ahs-range-" name)
(substring name (match-end 0) (length name))
name))))
(cond
((not (boundp range))
(ahs-log 'plugin-notfound plugin-name))
((not (memq range ahs-range-plugin-list))
(ahs-log 'plugin-notplugin plugin-name))
((not (memq range (ahs-runnable-plugins)))
(ahs-log 'plugin-badcondition
(ahs-get-plugin-prop 'name range)))
(t t)))
(defun ahs-runnable-plugins (&optional getnext)
"Return list of runnable plugins."
(loop with current = nil
with available = nil
for range in ahs-range-plugin-list
for plugin = (symbol-value range)
for mode = (ahs-get-plugin-prop 'major-mode range)
when (equal plugin ahs-current-range) do (setq current range)
when (or (equal 'none mode)
(and (listp mode)
(memq major-mode mode))
(eq major-mode mode))
when (ahs-get-plugin-prop 'condition range)
collect range into available
finally
return (if getnext
(or (cadr (memq current available))
(car available))
available)))
(defun ahs-change-range-internal (range)
"Current plugin change to `RANGE' plugin."
(setq ahs-current-range (symbol-value range))
(ahs-current-plugin-prop 'init))
;;
;; (@* "Built-in plugin" )
;;
(ahs-regist-range-plugin
display
'((name . "display area")
(lighter . "HS")
(face . ahs-plugin-defalt-face)
(start . window-start)
(end . window-end))
"Display area")
(ahs-regist-range-plugin
whole-buffer
'((name . "whole buffer")
(lighter . "HSA")
(face . ahs-plugin-whole-buffer-face)
(start . point-min)
(end . point-max))
"Whole buffer")
;; beginning-of-defun
(defvar ahs-plugin-bod-start nil)
(defvar ahs-plugin-bod-end nil)
(defcustom ahs-plugin-bod-modes
'( emacs-lisp-mode lisp-interaction-mode c++-mode c-mode )
"Major modes `beginning of defun' plugin can run on."
:group 'auto-highlight-symbol
:type '(repeat symbol))
(defcustom ahs-plugin-bod-function 'ahs-plugin-ahs-bod
"Function used in `beginning of defun' plugin."
:group 'auto-highlight-symbol
:type '(choice
(symbol :tag "Use built-in function" ahs-plugin-ahs-bod)
(symbol :tag "Use original narrow-to-defun" ahs-plugin-orignal-n2d)))
(defmacro ahs-plugin-bod-error (err)
`(if (= 4 (length ,err))
(apply 'ahs-log 'error-scan-sexp ,err)
(ahs-log 'self ,err)))
(defun ahs-plugin-orignal-n2d ()
"Original narrow-to-defun."
(save-restriction
(condition-case err
(progn
(narrow-to-defun)
(cons (point-min) (point-max)))
(error err (ahs-plugin-bod-error err)))))
(defun ahs-plugin-ahs-bod ()
"Another narrow-to-defun."
(condition-case err
(let ((opoint (point))
beg end)
;; Point in function
(beginning-of-defun)
(setq beg (point))
(end-of-defun)
(setq end (point))
(cond
;; Between point-min and function
((equal beg (point-min))
(goto-char opoint)
(beginning-of-defun -1)
(if (and (>= opoint beg)
(< opoint end))
;; From point-min to first function
(when (> end (point))
(setq end (point)))
;; Outside function
(setq beg end
end (point))))
;; Between function and function
((>= opoint end)
(setq beg end)
(beginning-of-defun -1)
(setq end (point))))
(cons beg end))
(error err (ahs-plugin-bod-error err))))
(ahs-regist-range-plugin
beginning-of-defun
'((name . "beginning of defun")
(lighter . "HSD")
(face . ahs-plugin-bod-face)
(major-mode . ahs-plugin-bod-modes)
(before-search . ahs-plugin-set-bod-region)
(start . ahs-plugin-bod-start)
(end . ahs-plugin-bod-end))
"beginning-of-defun to end-of-defun.")
(defvar ahs-plugin-hybrid-defined nil)
(defun ahs-plugin-hybrid-face-function ()
(let* ((hl (ahs-highlight-p))
(symbol (and hl (nth 0 hl))))
(if (and symbol ahs-plugin-hybrid-defined)
ahs-plugin-whole-buffer-face
ahs-plugin-bod-face)))
(defun ahs-plugin-hybrid-set-bod-region (symbol)
(setq ahs-plugin-hybrid-defined
(ahs-symbol-defined-in-this-buffer symbol))
(if ahs-plugin-hybrid-defined
(progn
(setq ahs-plugin-bod-start (point-min))
(setq ahs-plugin-bod-end (point-max)))
(ahs-plugin-set-bod-region symbol)))
(defun ahs-plugin-set-bod-region (symbol)
(save-excursion
(let ((pos (funcall ahs-plugin-bod-function)))
(if (not (consp pos))
'abort
(setq ahs-plugin-bod-start (car pos))
(setq ahs-plugin-bod-end (cdr pos))))))
;;FIXME more powerful hybrid plugin
(ahs-regist-range-plugin
hybrid
'((name . "hybrid buffer")
(lighter . "HSH")
(face . ahs-plugin-hybrid-face-function)
(major-mode . ahs-plugin-bod-modes)
(before-search . ahs-plugin-hybrid-set-bod-region)
(start . ahs-plugin-bod-start)
(end . ahs-plugin-bod-end))
"Highlight whole buffer if symbol defined in this buffer or
highlight current defun.")
;;
;; (@* "Timer" )
;;
(defun ahs-start-timer ()
"Start idle timer."
(unless ahs-idle-timer
(setq ahs-idle-timer
(run-with-idle-timer ahs-idle-interval t 'ahs-idle-function))))
(defun ahs-restart-timer ()
"Restart idle timer."
(when (timerp ahs-idle-timer)
(cancel-timer ahs-idle-timer)
(setq ahs-idle-timer nil)
(ahs-start-timer)))
;;
;; (@* "Idle" )
;;
(defun ahs-idle-function ()
"Idle function. Called by `ahs-idle-timer'."
(when (and auto-highlight-symbol-mode
(not ahs-highlighted))
(save-match-data
(with-local-quit
(let ((hl (ahs-highlight-p)))
(when hl
(ahs-highlight (nth 0 hl)
(nth 1 hl)
(nth 2 hl))))))))
(defmacro ahs-add-overlay-face (pos face)
`(if ahs-face-check-include-overlay
(append (ahs-get-overlay-face ,pos)
(if (listp ,face)
,face
(list ,face))) ,face))
(defun ahs-highlight-p ()
"Ruturn Non-nil if symbols can be highlighted."
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(beg (car bounds))
(end (cdr bounds))
(face (when bounds
(get-text-property beg 'face)))
(symbol (when bounds
(buffer-substring beg end))))
(when (and symbol
(not (ahs-dropdown-list-p))
(not (ahs-face-p (ahs-add-overlay-face beg face) 'ahs-inhibit-face-list))
(not (ahs-symbol-p ahs-exclude symbol t))
(ahs-symbol-p ahs-include symbol))
(list symbol beg end))))
(defun ahs-symbol-p (pred symbol &optional nodefs)
"Return Non-nil if `SYMBOL' matches `PRED'."
(cond
;; Default include/no exclude
((null pred)
(unless nodefs
(let ((case-fold-search ahs-case-fold-search))
(string-match ahs-default-symbol-regexp symbol))))
;; REGEXP
((stringp pred)
(let ((case-fold-search ahs-case-fold-search))
(string-match pred symbol)))