-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsenator.el
2966 lines (2717 loc) · 105 KB
/
senator.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
;;; senator.el --- SEmantic NAvigaTOR
;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by David Ponce
;; Author: David Ponce <[email protected]>
;; Maintainer: David Ponce <[email protected]>
;; Created: 10 Nov 2000
;; Keywords: syntax
;; X-RCS: $Id: senator.el,v 1.138 2009/02/07 02:28:19 zappo Exp $
;; This file is not part of 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 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.
;;; Commentary:
;;
;; This library defines commands and a minor mode to navigate between
;; semantic language tags in the current buffer.
;;
;; The commands `senator-next-tag' and `senator-previous-tag'
;; navigate respectively to the tag after or before the point. The
;; command `senator-jump' directly jumps to a particular semantic
;; symbol.
;;
;; Also, for each built-in search command `search-forward',
;; `search-backward', `re-search-forward', `re-search-backward',
;; `word-search-forward' and `word-search-backward', there is an
;; equivalent `senator-<search-command>' defined which searches only
;; in semantic tag names.
;;
;; The command `senator-isearch-toggle-semantic-mode' toggles semantic
;; search in isearch mode. When semantic search is enabled, isearch
;; is restricted to tag names.
;;
;; Finally, the library provides a `senator-minor-mode' to easily
;; enable or disable the SEmantic NAvigaTOR stuff for the current
;; buffer.
;;
;; The best way to use navigation commands is to bind them to keyboard
;; shortcuts. Senator minor mode uses the common prefix key "C-c ,".
;; The following default key bindings are provided when semantic minor
;; mode is enabled:
;;
;; key binding
;; --- -------
;; C-c , n `senator-next-tag'
;; C-c , p `senator-previous-tag'
;; C-c , j `senator-jump'
;; C-c , i `senator-isearch-toggle-semantic-mode'
;; C-c , TAB `senator-complete-symbol'
;; C-c , SPC `senator-completion-menu-popup'
;; S-mouse-3 `senator-completion-menu-popup'
;; C-c , C-y `senator-yank-tag'
;; C-c , C-w `senator-kill-tag'
;; C-c , M-w `senator-copy-tag'
;; C-c , r `senator-copy-tag-to-register'
;; C-c , t `senator-transpose-tags-up'
;;
;; You can customize the `senator-step-at-tag-classes' to navigate (and
;; search) only between tags of a particular class. (Such as
;; functions and variables.)
;;
;; Customize `senator-step-at-start-end-tag-classes' to stop at the
;; start and end of the specified tag classes.
;;
;; To have a mode specific customization, do something like this in a
;; hook:
;;
;; (add-hook 'mode-hook
;; (lambda ()
;; (setq senator-step-at-tag-classes '(function variable))
;; (setq senator-step-at-start-end-tag-classes '(function))
;; ))
;;
;; The above example specifies to navigate (and search) only between
;; functions and variables, and to step at start and end of functions
;; only.
;;; History:
;;
;;; Code:
(require 'semantic)
(require 'semantic-ctxt)
(require 'semantic-imenu)
(eval-when-compile
(require 'semanticdb)
(require 'semanticdb-find)
(require 'cl)
)
;;; Customization
(defgroup senator nil
"SEmantic NAvigaTOR."
:group 'semantic)
;;;###autoload
(defcustom global-senator-minor-mode nil
"*If non-nil enable global use of senator minor mode."
:group 'senator
:type 'boolean
:require 'senator
:initialize 'custom-initialize-default
:set (lambda (sym val)
(global-senator-minor-mode (if val 1 -1))))
(defcustom senator-minor-mode-hook nil
"Hook run at the end of function `senator-minor-mode'."
:group 'senator
:type 'hook)
;;;###autoload
(defcustom senator-step-at-tag-classes nil
"*List of tag classes where to step.
A tag class is a symbol like 'variable, 'function, 'type, or other.
If nil navigation steps at any tag found. This is a buffer local
variable. It can be set in a mode hook to get a specific langage
navigation."
:group 'senator
:type '(repeat (symbol)))
(make-variable-buffer-local 'senator-step-at-tag-classes)
(semantic-varalias-obsolete 'semantic-step-at-token-ids
'semantic-step-at-tag-classes)
;;;###autoload
(defcustom senator-step-at-start-end-tag-classes '(function)
"*List of tag classes where to step at start and end.
A tag class is a symbol like 'variable, 'function, 'type, or other.
If nil, navigation only step at beginning of tags. If t, step at
start and end of any tag where it is allowed to step. Also, stepping
at start and end of a tag prevent stepping inside its components.
This is a buffer local variable. It can be set in a mode hook to get
a specific langage navigation."
:group 'senator
:type '(choice :tag "Identifiers"
(repeat :menu-tag "Symbols" (symbol))
(const :tag "All" t)))
(make-variable-buffer-local 'senator-step-at-start-end-tag-classes)
(semantic-varalias-obsolete 'senator-step-at-start-end-token-ids
'senator-step-at-start-end-tag-classes)
(defcustom senator-highlight-found t
"*If non-nil highlight tags found.
This option requires semantic 1.3 and above. This is a buffer
local variable. It can be set in a mode hook to get a specific
langage behaviour."
:group 'senator
:type 'boolean)
(make-variable-buffer-local 'senator-highlight-found)
;;; Faces
(defface senator-momentary-highlight-face
'((((class color) (background dark))
(:background "gray30"))
(((class color) (background light))
(:background "gray70")))
"Face used to momentarily highlight tags."
:group 'semantic-faces)
(defface senator-intangible-face
'((((class color) (background light))
(:foreground "gray25"))
(((class color) (background dark))
(:foreground "gray75")))
"Face placed on intangible text."
:group 'semantic-faces)
(defface senator-read-only-face
'((((class color) (background dark))
(:background "#664444"))
(((class color) (background light))
(:background "#CCBBBB")))
"Face placed on read-only text."
:group 'semantic-faces)
;;;;
;;;; Common functions
;;;;
(defsubst senator-parse ()
"Parse the current buffer and return the tags where to navigate."
(semantic-fetch-tags))
(defun senator-force-refresh ()
"Force a full refresh of the current buffer's tags.
Throws away all the old tags, and recreates the tag database for
this buffer."
(interactive)
(semantic-clear-toplevel-cache)
(senator-parse))
(defsubst senator-current-tag ()
"Return the current tag in the current buffer.
Raise an error is there is no tag here."
(or (semantic-current-tag)
(error "No semantic tag here")))
(semantic-alias-obsolete 'senator-current-token 'senator-current-tag)
(defun senator-momentary-highlight-tag (tag)
"Momentary highlight TAG.
Does nothing if `senator-highlight-found' is nil."
(and senator-highlight-found
(semantic-momentary-highlight-tag
tag 'senator-momentary-highlight-face)))
(defun senator-step-at-start-end-p (tag)
"Return non-nil if must step at start and end of TAG."
(and tag
(or (eq senator-step-at-start-end-tag-classes t)
(memq (semantic-tag-class tag)
senator-step-at-start-end-tag-classes))))
(defun senator-skip-p (tag)
"Return non-nil if must skip TAG."
(and tag
senator-step-at-tag-classes
(not (memq (semantic-tag-class tag)
senator-step-at-tag-classes))))
(defun senator-middle-of-tag-p (pos tag)
"Return non-nil if POS is between start and end of TAG."
(and (> pos (semantic-tag-start tag))
(< pos (semantic-tag-end tag))))
(defun senator-step-at-parent (tag)
"Return TAG's outermost parent if must step at start/end of it.
Return nil otherwise."
(if tag
(let (parent parents)
(setq parents (semantic-find-tag-by-overlay
(semantic-tag-start tag)))
(while (and parents (not parent))
(setq parent (car parents)
parents (cdr parents))
(if (or (eq tag parent)
(senator-skip-p parent)
(not (senator-step-at-start-end-p parent)))
(setq parent nil)))
parent)))
(defun senator-previous-tag-or-parent (pos)
"Return the tag before POS or one of its parent where to step."
(let (ol tag)
(while (and pos (> pos (point-min)) (not tag))
(setq pos (semantic-overlay-previous-change pos))
(when pos
;; Get overlays at position
(setq ol (semantic-overlays-at pos))
;; find the overlay that belongs to semantic
;; and STARTS or ENDS at the found position.
(while (and ol (not tag))
(setq tag (semantic-overlay-get (car ol) 'semantic))
(unless (and tag (semantic-tag-p tag)
(or (= (semantic-tag-start tag) pos)
(= (semantic-tag-end tag) pos)))
(setq tag nil
ol (cdr ol))))))
(or (senator-step-at-parent tag) tag)))
(defun senator-full-tag-name (tag parent)
"Compose a full name from TAG name and PARENT names.
That is append to TAG name PARENT names each one separated by
`semantic-type-relation-separator-character'. The PARENT list is in
reverse order."
(let ((sep (car semantic-type-relation-separator-character))
(name ""))
(while parent
(setq name (concat name sep
(semantic-tag-name (car parent)))
parent (cdr parent)))
(concat (semantic-tag-name tag) name)))
(semantic-alias-obsolete 'senator-full-token-name
'senator-full-tag-name)
(defvar senator-completion-cache nil
"The latest full completion list is cached here.")
(make-variable-buffer-local 'senator-completion-cache)
(defun senator-completion-cache-flush-fcn (&optional ignore)
"Hook run to clear the completion list cache.
It is called each time the semantic cache is changed.
IGNORE arguments."
(setq senator-completion-cache nil))
(defun senator-completion-flatten-stream (stream parents &optional top-level)
"Return a flat list of all tags available in STREAM.
PARENTS is the list of parent tags. Each element of the list is a
pair (TAG . PARENTS) where PARENTS is the list of TAG parent
tags or nil. If TOP-LEVEL is non-nil the completion list will
contain only tags at top level. Otherwise all component tags are
included too."
(let (fs e tag components)
(while stream
(setq tag (car stream)
stream (cdr stream)
e (cons tag parents)
fs (cons e fs))
(and (not top-level)
;; Not include function arguments
(not (semantic-tag-of-class-p tag 'function))
(setq components (semantic-tag-components tag))
(setq fs (append fs (senator-completion-flatten-stream
components e)))))
fs))
(defun senator-completion-function-args (tag)
"Return a string of argument names from function TAG."
(mapconcat #'(lambda (arg)
(if (semantic-tag-p arg)
(semantic-tag-name arg)
(format "%s" arg)))
(semantic-tag-function-arguments tag)
semantic-function-argument-separation-character))
(defun senator-completion-refine-name (elt)
"Refine the name part of ELT.
ELT has the form (NAME . (TAG . PARENTS)). The NAME refinement is
done in the following incremental way:
- If TAG is a function, append the list of argument names to NAME.
- If TAG is a type, append \"{}\" to NAME.
- If TAG is an include, append \"#\" to NAME.
- If TAG is a package, append \"=\" to NAME.
- If TAG has PARENTS append to NAME, the first separator in
`semantic-type-relation-separator-character', followed by the next
parent name.
- Otherwise NAME is set to \"tag-name@tag-start-position\"."
(let* ((sep (car semantic-type-relation-separator-character))
(name (car elt))
(tag (car (cdr elt)))
(parents (cdr (cdr elt)))
(oname (semantic-tag-name tag))
(class (semantic-tag-class tag)))
(cond
((and (eq class 'function) (string-equal name oname))
(setq name (format "%s(%s)" name
(senator-completion-function-args tag))))
((and (eq class 'type) (string-equal name oname))
(setq name (format "%s{}" name)))
((and (eq class 'include) (string-equal name oname))
(setq name (format "%s#" name)))
((and (eq class 'package) (string-equal name oname))
(setq name (format "%s=" name)))
(parents
(setq name (format "%s%s%s" name
(if (semantic-tag-of-class-p
(car parents) 'function)
")" sep)
(semantic-tag-name (car parents)))
parents (cdr parents)))
(t
(setq name (format "%s@%d" oname
(semantic-tag-start tag)))))
(setcar elt name)
(setcdr elt (cons tag parents))))
(defun senator-completion-uniquify-names (completion-stream)
"Uniquify names in COMPLETION-STREAM.
That is refine the name part of each COMPLETION-STREAM element until
there is no duplicated names. Each element of COMPLETION-STREAM has
the form (NAME . (TAG . PARENTS)). See also the function
`senator-completion-refine-name'."
(let ((completion-stream (sort completion-stream
#'(lambda (e1 e2)
(string-lessp (car e1)
(car e2)))))
(dupp t)
clst elt dup name)
(while dupp
(setq dupp nil
clst completion-stream)
(while clst
(setq elt (car clst)
name (car elt)
clst (cdr clst)
dup (and clst
(string-equal name (car (car clst)))
elt)
dupp (or dupp dup))
(while dup
(senator-completion-refine-name dup)
(setq elt (car clst)
dup (and elt (string-equal name (car elt)) elt))
(and dup (setq clst (cdr clst))))))
;; Return a usable completion alist where each element has the
;; form (NAME . TAG).
(setq clst completion-stream)
(while clst
(setq elt (car clst)
clst (cdr clst))
(setcdr elt (car (cdr elt))))
completion-stream))
(defun senator-completion-stream (stream &optional top-level)
"Return a useful completion list from tags in STREAM.
That is an alist of all (COMPLETION-NAME . TAG) available.
COMPLETION-NAME is an unique tag name (see also the function
`senator-completion-uniquify-names'). If TOP-LEVEL is non-nil the
completion list will contain only tags at top level. Otherwise all
sub tags are included too."
(let* ((fs (senator-completion-flatten-stream stream nil top-level))
cs elt tag)
;; Transform each FS element from (TAG . PARENTS)
;; to (NAME . (TAG . PARENT)).
(while fs
(setq elt (car fs)
tag (car elt)
fs (cdr fs)
cs (cons (cons (semantic-tag-name tag) elt) cs)))
;; Return a completion list with unique COMPLETION-NAMEs.
(senator-completion-uniquify-names cs)))
(defun senator-current-type-context ()
"Return tags in the type context at point or nil if not found."
(let ((context (semantic-find-tags-by-class
'type (semantic-find-tag-by-overlay))))
(if context
(semantic-tag-type-members
(nth (1- (length context)) context)))))
(defun senator-completion-list (&optional in-context)
"Return a useful completion list from tags in current buffer.
If IN-CONTEXT is non-nil return only the top level tags in the type
context at point or the top level tags in the current buffer if no
type context exists at point."
(let (stream)
(if in-context
(setq stream (senator-current-type-context)))
(or stream (setq stream (senator-parse)))
;; IN-CONTEXT completion doesn't use nor set the cache.
(or (and (not in-context) senator-completion-cache)
(let ((clst (senator-completion-stream stream in-context)))
(or in-context
(setq senator-completion-cache clst))
clst))))
(defun senator-find-tag-for-completion (prefix)
"Find all tags with a name starting with PREFIX.
Uses `semanticdb' when available."
(let ((tagsa nil)
(tagsb nil))
(when (and (featurep 'semantic-analyze))
(let ((ctxt (semantic-analyze-current-context)))
(when ctxt
(condition-case nil
(setq tagsa (semantic-analyze-possible-completions
ctxt))
(error nil)))))
(if tagsa
tagsa
;; If the analyzer fails, then go into boring completion
(setq tagsb
(if (and (featurep 'semanticdb) (semanticdb-minor-mode-p))
;; semanticdb version returns a list of (DB-TABLE . TAG-LIST)
(semanticdb-deep-find-tags-for-completion prefix)
;; semantic version returns a TAG-LIST
(semantic-deep-find-tags-for-completion prefix (current-buffer))))
(semanticdb-fast-strip-find-results tagsb))))
;;; Senator stream searching functions: no more supported.
;;
(defun senator-find-nonterminal-by-name (&rest ignore)
(error "Use the semantic and semanticdb find API instead"))
(defun senator-find-nonterminal-by-name-regexp (&rest ignore)
(error "Use the semantic and semanticdb find API instead"))
;;;;
;;;; Search functions
;;;;
(defun senator-search-tag-name (tag)
"Search for TAG name in current buffer.
Limit the search to TAG bounds.
If found, set point to the end of the name, and return point. The
beginning of the name is at (match-beginning 0).
Return nil if not found, that is if TAG name doesn't come from the
source."
(let ((name (semantic-tag-name tag)))
(setq name (if (string-match "\\`\\([^[]+\\)[[]" name)
(match-string 1 name)
name))
(goto-char (semantic-tag-start tag))
(when (re-search-forward (concat
;; The tag name is expected to be
;; between word delimiters, whitespaces,
;; or punctuations.
"\\(\\<\\|\\s-+\\|\\s.\\)"
(regexp-quote name)
"\\(\\>\\|\\s-+\\|\\s.\\)")
(semantic-tag-end tag)
t)
(goto-char (match-beginning 0))
(search-forward name))))
(defcustom senator-search-ignore-tag-classes
'(code block)
"*List of ignored tag classes.
Tags of those classes are excluded from search."
:group 'senator
:type '(repeat (symbol :tag "class")))
(defun senator-search-default-tag-filter (tag)
"Default function that filters searched tags.
Ignore tags of classes in `senator-search-ignore-tag-classes'"
(not (memq (semantic-tag-class tag)
senator-search-ignore-tag-classes)))
(defvar senator-search-tag-filter-functions
'(senator-search-default-tag-filter)
"List of functions to be called to filter searched tags.
Each function is passed a tag. If one of them returns nil, the tag is
excluded from the search.")
(defun senator-search (searcher text &optional bound noerror count)
"Use the SEARCHER function to search from point for TEXT in a tag name.
SEARCHER is typically the function `search-forward', `search-backward',
`word-search-forward', `word-search-backward', `re-search-forward', or
`re-search-backward'. See one of the above function to see how the
TEXT, BOUND, NOERROR, and COUNT arguments are interpreted."
(let* ((origin (point))
(count (or count 1))
(step (cond ((> count 0) 1)
((< count 0) (setq count (- count)) -1)
(0)))
found next sstart send tag tstart tend)
(or (zerop step)
(while (and (not found)
(setq next (funcall searcher text bound t step)))
(setq sstart (match-beginning 0)
send (match-end 0))
(if (= sstart send)
(setq found t)
(and (setq tag (semantic-current-tag))
(run-hook-with-args-until-failure
'senator-search-tag-filter-functions tag)
(setq tend (senator-search-tag-name tag))
(setq tstart (match-beginning 0)
found (and (>= sstart tstart)
(<= send tend)
(zerop (setq count (1- count))))))
(goto-char next))))
(cond ((null found)
(setq next origin
send origin))
((= next sstart)
(setq next send
send sstart))
(t
(setq next sstart)))
(goto-char next)
;; Setup the returned value and the `match-data' or maybe fail!
(funcall searcher text send noerror step)))
;;;;
;;;; Navigation commands
;;;;
;;;###autoload
(defun senator-next-tag ()
"Navigate to the next Semantic tag.
Return the tag or nil if at end of buffer."
(interactive)
(let ((pos (point))
(tag (semantic-current-tag))
where)
(if (and tag
(not (senator-skip-p tag))
(senator-step-at-start-end-p tag)
(or (= pos (semantic-tag-start tag))
(senator-middle-of-tag-p pos tag)))
nil
(if (setq tag (senator-step-at-parent tag))
nil
(setq tag (semantic-find-tag-by-overlay-next pos))
(while (and tag (senator-skip-p tag))
(setq tag (semantic-find-tag-by-overlay-next
(semantic-tag-start tag))))))
(if (not tag)
(progn
(goto-char (point-max))
(working-message "End of buffer"))
(cond ((and (senator-step-at-start-end-p tag)
(or (= pos (semantic-tag-start tag))
(senator-middle-of-tag-p pos tag)))
(setq where "end")
(goto-char (semantic-tag-end tag)))
(t
(setq where "start")
(goto-char (semantic-tag-start tag))))
(senator-momentary-highlight-tag tag)
(working-message "%S: %s (%s)"
(semantic-tag-class tag)
(semantic-tag-name tag)
where))
tag))
(semantic-alias-obsolete 'senator-next-token 'senator-next-tag)
;;;###autoload
(defun senator-previous-tag ()
"Navigate to the previous Semantic tag.
Return the tag or nil if at beginning of buffer."
(interactive)
(let ((pos (point))
(tag (semantic-current-tag))
where)
(if (and tag
(not (senator-skip-p tag))
(senator-step-at-start-end-p tag)
(or (= pos (semantic-tag-end tag))
(senator-middle-of-tag-p pos tag)))
nil
(if (setq tag (senator-step-at-parent tag))
nil
(setq tag (senator-previous-tag-or-parent pos))
(while (and tag (senator-skip-p tag))
(setq tag (senator-previous-tag-or-parent
(semantic-tag-start tag))))))
(if (not tag)
(progn
(goto-char (point-min))
(working-message "Beginning of buffer"))
(cond ((or (not (senator-step-at-start-end-p tag))
(= pos (semantic-tag-end tag))
(senator-middle-of-tag-p pos tag))
(setq where "start")
(goto-char (semantic-tag-start tag)))
(t
(setq where "end")
(goto-char (semantic-tag-end tag))))
(senator-momentary-highlight-tag tag)
(working-message "%S: %s (%s)"
(semantic-tag-class tag)
(semantic-tag-name tag)
where))
tag))
(semantic-alias-obsolete 'senator-previous-token 'senator-previous-tag)
(defvar senator-jump-completion-list nil
"`senator-jump' stores here its current completion list.
Then use `assoc' to retrieve the tag associated to a symbol.")
(defun senator-jump-interactive (prompt &optional in-context no-default require-match)
"Called interactively to provide completion on some tag name.
Use PROMPT. If optional IN-CONTEXT is non-nil jump in the local
type's context \(see function `senator-current-type-context'). If
optional NO-DEFAULT is non-nil do not provide a default value. If
optional REQUIRE-MATCH is non-nil an explicit match must be made.
The IN-CONTEXT and NO-DEFAULT switches are combined using the
following prefix arguments:
- \\[universal-argument] IN-CONTEXT.
- \\[universal-argument] - NO-DEFAULT.
- \\[universal-argument] \\[universal-argument] IN-CONTEXT + NO-DEFAULT."
(let* ((arg (prefix-numeric-value current-prefix-arg))
(no-default
(or no-default
;; The `completing-read' function provided by XEmacs
;; (21.1) don't allow a default value argument :-(
(featurep 'xemacs)
(= arg -1) ; C-u -
(= arg 16))) ; C-u C-u
(in-context
(or in-context
(= arg 4) ; C-u
(= arg 16))) ; C-u C-u
(context
(and (not no-default)
(or (semantic-ctxt-current-symbol)
(semantic-ctxt-current-function))))
(completing-read-args
(list (if (and context (car context))
(format "%s(default: %s) " prompt (car context))
prompt)
(setq senator-jump-completion-list
(senator-completion-list in-context))
nil
require-match
""
'semantic-read-symbol-history)))
(list
(apply #'completing-read
(if (and context (car context))
(append completing-read-args context)
completing-read-args))
in-context no-default)))
(defun senator-jump-noselect (sym &optional next-p regexp-p)
"Jump to the semantic symbol SYM.
If NEXT-P is non-nil, then move the the next tag in the search
assuming there was already one jump for the given symbol.
If REGEXP-P is non nil, then treat SYM as a regular expression.
Return the tag jumped to.
Note: REGEXP-P doesn't work yet. This needs to be added to get
the etags override to be fully functional."
(let ((tag (cdr (assoc sym senator-jump-completion-list))))
(when tag
(set-buffer (semantic-tag-buffer tag))
(goto-char (semantic-tag-start tag))
tag)))
;;;###autoload
(defun senator-jump (sym &optional in-context no-default)
"Jump to the semantic symbol SYM.
If optional IN-CONTEXT is non-nil jump in the local type's context
\(see function `senator-current-type-context'). If optional
NO-DEFAULT is non-nil do not provide a default value.
When called interactively you can combine the IN-CONTEXT and
NO-DEFAULT switches like this:
- \\[universal-argument] IN-CONTEXT.
- \\[universal-argument] - NO-DEFAULT.
- \\[universal-argument] \\[universal-argument] IN-CONTEXT + NO-DEFAULT."
(interactive (senator-jump-interactive "Jump to: " nil nil t))
(push-mark)
(let ((tag (senator-jump-noselect sym no-default)))
(when tag
(switch-to-buffer (semantic-tag-buffer tag))
(senator-momentary-highlight-tag tag)
(working-message "%S: %s "
(semantic-tag-class tag)
(semantic-tag-name tag)))))
;;;###autoload
(defun senator-jump-regexp (symregex &optional in-context no-default)
"Jump to the semantic symbol SYMREGEX.
SYMREGEX is treated as a regular expression.
If optional IN-CONTEXT is non-nil jump in the local type's context
\(see function `senator-current-type-context'). If optional
NO-DEFAULT is non-nil do not provide a default value and move to the
next match of SYMREGEX. NOTE: Doesn't actually work yet.
When called interactively you can combine the IN-CONTEXT and
NO-DEFAULT switches like this:
- \\[universal-argument] IN-CONTEXT.
- \\[universal-argument] - NO-DEFAULT.
- \\[universal-argument] \\[universal-argument] IN-CONTEXT + NO-DEFAULT."
(interactive (senator-jump-interactive "Jump to: "))
(let ((tag (senator-jump-noselect symregex no-default)))
(when tag
(switch-to-buffer (semantic-tag-buffer tag))
(senator-momentary-highlight-tag tag)
(working-message "%S: %s "
(semantic-tag-class tag)
(semantic-tag-name tag)))))
(defvar senator-last-completion-stats nil
"The last senator completion was here.
Of the form (BUFFER STARTPOS INDEX REGEX COMPLIST...)")
(defsubst senator-current-symbol-start ()
"Return position of start of the current symbol under point or nil."
(let* ((sb (semantic-ctxt-current-symbol-and-bounds (point)))
(bounds (nth 2 sb)))
(car bounds)))
;; (condition-case nil
;; (save-excursion (forward-sexp -1) (point))
;; (error nil)))
;;;###autoload
(defun senator-complete-symbol (&optional cycle-once)
"Complete the current symbol under point.
If optional argument CYCLE-ONCE is non-nil, only cycle through the list
of completions once, doing nothing where there are no more matches."
(interactive)
(let ((symstart (senator-current-symbol-start))
regex complst)
(if symstart
;; Get old stats if apropriate.
(if (and senator-last-completion-stats
;; Check if completing in the same buffer
(eq (car senator-last-completion-stats) (current-buffer))
;; Check if completing from the same point
(= (nth 1 senator-last-completion-stats) symstart)
;; Check if completing the same symbol
(save-excursion
(goto-char symstart)
(looking-at (nth 3 senator-last-completion-stats))))
(setq complst (nthcdr 4 senator-last-completion-stats))
(setq regex (regexp-quote (buffer-substring symstart (point)))
complst (senator-find-tag-for-completion regex)
senator-last-completion-stats (append (list (current-buffer)
symstart
0
regex)
complst))))
;; Do the completion if appropriate.
(if complst
(let ((ret t)
(index (nth 2 senator-last-completion-stats))
newtok)
(if (= index (length complst))
;; Cycle to the first completion tag.
(setq index 0
;; Stop completion if CYCLE-ONCE is non-nil.
ret (not cycle-once)))
;; Get the new completion tag.
(setq newtok (nth index complst))
(when ret
;; Move index to the next completion tag.
(setq index (1+ index)
;; Return the completion string (useful to hippie
;; expand for example)
ret (semantic-tag-name newtok))
;; Replace the string.
(delete-region symstart (point))
(insert ret))
;; Update the completion index.
(setcar (nthcdr 2 senator-last-completion-stats) index)
ret))))
;;;;
;;;; Completion menu
;;;;
(defcustom senator-completion-menu-summary-function
'semantic-format-tag-concise-prototype
"*Function to use when creating items in completion menu.
Some useful functions are in `semantic-format-tag-functions'."
:group 'senator
:type semantic-format-tag-custom-list)
(make-variable-buffer-local 'senator-completion-menu-summary-function)
(defcustom senator-completion-menu-insert-function
'senator-completion-menu-insert-default
"*Function to use to insert an item from completion menu.
It will receive a Semantic tag as argument."
:group 'senator
:type '(radio (const senator-completion-menu-insert-default)
(function)))
(make-variable-buffer-local 'senator-completion-menu-insert-function)
(defun senator-completion-menu-insert-default (tag)
"Insert a text representation of TAG at point."
(insert (semantic-tag-name tag)))
(defun senator-completion-menu-do-complete (tag-array)
"Replace the current syntactic expression with a chosen completion.
Argument TAG-ARRAY is an array of one element containting the tag
choosen from the completion menu."
(let ((tag (aref tag-array 0))
(symstart (senator-current-symbol-start))
(finsert (if (fboundp senator-completion-menu-insert-function)
senator-completion-menu-insert-function
#'senator-completion-menu-insert-default)))
(if symstart
(progn
(delete-region symstart (point))
(funcall finsert tag)))))
(defun senator-completion-menu-item (tag)
"Return a completion menu item from TAG.
That is a pair (MENU-ITEM-TEXT . TAG-ARRAY). TAG-ARRAY is an
array of one element containing TAG. Can return nil to discard a
menu item."
(cons (funcall (if (fboundp senator-completion-menu-summary-function)
senator-completion-menu-summary-function
#'semantic-format-tag-prototype) tag)
(vector tag)))
(defun senator-completion-menu-window-offsets (&optional window)
"Return offsets of WINDOW relative to WINDOW's frame.
Return a cons cell (XOFFSET . YOFFSET) so the position (X . Y) in
WINDOW is equal to the position ((+ X XOFFSET) . (+ Y YOFFSET)) in
WINDOW'S frame."
(let* ((window (or window (selected-window)))
(e (window-edges window))
(left (nth 0 e))
(top (nth 1 e))
(right (nth 2 e))
(bottom (nth 3 e))
(x (+ left (/ (- right left) 2)))
(y (+ top (/ (- bottom top) 2)))
(wpos (coordinates-in-window-p (cons x y) window))
(xoffset 0)
(yoffset 0))
(if (consp wpos)
(let* ((f (window-frame window))
(cy (/ 1.0 (float (frame-char-height f)))))
(setq xoffset (- x (car wpos))
yoffset (float (- y (cdr wpos))))
;; If Emacs 21 add to:
;; - XOFFSET the WINDOW left margin width.
;; - YOFFSET the height of header lines above WINDOW.
(if (> emacs-major-version 20)
(progn
(setq wpos (cons (+ left xoffset) 0.0)
bottom (float bottom))
(while (< (cdr wpos) bottom)
(if (eq (coordinates-in-window-p wpos window)
'header-line)
(setq yoffset (+ yoffset cy)))
(setcdr wpos (+ (cdr wpos) cy)))
(setq xoffset (floor (+ xoffset
(or (car (window-margins window))
0))))))
(setq yoffset (floor yoffset))))
(cons xoffset yoffset)))
(defun senator-completion-menu-point-as-event()
"Returns the text cursor position as an event.
Also move the mouse pointer to the cursor position."
(let* ((w (get-buffer-window (current-buffer)))
(x (mod (- (current-column) (window-hscroll))
(window-width)))
(y (save-excursion
(save-restriction
(widen)
(narrow-to-region (window-start) (point))
(goto-char (point-min))
(1+ (vertical-motion (buffer-size))))))
)
(if (featurep 'xemacs)
(let* ((at (progn (set-mouse-position w x (1- y))
(cdr (mouse-pixel-position))))
(x (car at))
(y (cdr at)))
(make-event 'button-press
(list 'button 3
'modifiers nil
'x x
'y y)))
;; Emacs
(let ((offsets (senator-completion-menu-window-offsets w)))
;; Convert window position (x,y) to the equivalent frame
;; position and move the mouse pointer to it.
(set-mouse-position (window-frame w)
(+ x (car offsets))
(+ y (cdr offsets)))
t))))
;;;###autoload
(defun senator-completion-menu-popup ()
"Popup a completion menu for the symbol at point.
The popup menu displays all of the possible completions for the symbol
it was invoked on. To automatically split large menus this function
use `imenu--mouse-menu' to handle the popup menu."
(interactive)
(let ((symstart (senator-current-symbol-start))
symbol regexp complst
;; Turn off tag jumping for this menu.
(imenu-default-goto-function (lambda (name pos &optional rest) pos)))
(if symstart
(setq symbol (buffer-substring-no-properties symstart (point))
regexp (regexp-quote symbol)
complst (senator-find-tag-for-completion regexp)))
(if (not complst)
(error "No completions available"))
;; We have a completion list, build a menu
(let ((index (delq nil
(mapcar #'senator-completion-menu-item
complst)))
title item)
(cond ;; Here index is a menu structure like:
;; -1- (("menu-item1" . [tag1]) ...)
((vectorp (cdr (car index)))
;; There are more than one item, setup the popup title.
(if (cdr index)
(setq title (format "%S completion" symbol))
;; Only one item , no need to popup the menu.
(setq item (car index))))
;; -2- (("menu-title1" ("menu-item1" . [tag1]) ...) ...)
(t
;; There are sub-menus.
(if (cdr index)
;; Several sub-menus, setup the popup title.
(setq title (format "%S completion" symbol))
;; Only one sub-menu, convert it to a main menu and add the
;; sub-menu title (filename) to the popup title.
(setq title (format "%S completion (%s)"