-
Notifications
You must be signed in to change notification settings - Fork 0
/
abscope.el
979 lines (863 loc) · 29.2 KB
/
abscope.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
;;----------------------------------------
;; todos:
;; - function params
;; - struct members (AC)
;;
;; exe todos:
;; - unit test everything (wire up ptrhash tests)
;; - standardize 0 as success. (e.g. in serialize)
;; - add strings.abs : for searching all text strings
;; - context should grab entire line.
;; - editors/MultiEditTable.h/(209):editors/MultiEditTable.h/(14):struct METable; : misparsed?
;; - recursive search/call graph
;;----------------------------------------
;;; abscope.el --- functions for interacting with the abscope tags system
;;
;; Copyright (C) 2009
;; Author: Aaron Brady
;; Keywords: tags, tools
;; Homepage: http://github.com/abrady/abscope
;; Version: 0.0a
;;
;; Installation:
;; - make sure the abscope binary is in the path somewhere, or set `abscope-exe'
;; - load the abscope library
;; - call abscope-scan-src on the directory you'd like to scan (customize abscope-scan-args if necessary. -E dir is the most likely thing to add for excluding certain directories)
;;
;; Running:
;; All tag queries are pcre regular expressions (http://www.pcre.org/), case insensitive.
;;
;; The results of tag queries are printed to a file in the project
;; directory named the value of `abscope-file'. usually
;; abscope-queries.org. as you run abscope-query and abscope-jump
;; functions this file will keep a handy history of everything you've
;; searched for so far in a format 'org-mode' can understand for
;; navigation.
;;
;; Most people will use one of the query or jump helper functions:
;; - abscope-query-function : run a query for a function and print the results in the `abscope-file' file.
;; - abscope-jump-function : query and jump to the matching location (best guess for ambiguous)
;; - abscope-query-struct : same as above, but for a struct and not a function
;; - abscope-jump-struct : "", jump to the best-guess location
;; - ...
;;
;; General queries can be done with this function:
;; - abscope-query : takes a tag and a string of types to match, with
;; "a" being a special character for matching all
;; types. e.g. (abscope-query "a" "main") will look for any tag named
;; 'main'. you could also say (abscope-query "df" "main") to look for
;; all defines or functions named 'main'
;;
;; Navigation and history:
;; - abscope-pop-loc : jump to the last location you used an abscope-jump function or link to get to
;; - abscope-next-match : next match from the last query run
;; - abscope-prev-match : prev match from the last query run
;; - abscope-query-replace
;;
;; Autocomplete (work in progress):
;; - abscope-insert : do a query, and then insert the results
;; - abscope-find-members : autocomplete struct members: if you call this with point after a-> it will try to get the type for 'a' and get you the members.
;; - abscope-find-params : autocomplete for function params:
;;
;;
;; Finally, you may want to set up the following:
;; in your .emacs file so the `abscope-file' buffer is in org-mode automatically:
;; (require 'org)
;; (add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))
;;----------------------------------------
(require 'cl)
;;----------------------------------------
;; my crazy alias section. I prefer mnemonics like
;; M-x abc to C-a C-b c commands, feel free to use or change.
(defcustom abscope-define-aliases t
"variable that controls if the three and four letter aliases for the abscope set of functions are defined.")
(defcustom abscope-default-project-root "c:/src"
"if no abscope files can be found in the current buffer's directory or any parent directories, jump to this directory to find them")
(defcustom abscope-scan-args '("-R" "." "-E" "AutoGen" "-E" "3rdparty")
"parameters to an abscope scan, e.g. -E .svn for ignored directories ")
(if abscope-define-aliases
(progn
(defalias 'abp 'abscope-pop-loc)
;; primary query/jump functions
(defalias 'abq 'abscope-query)
(defalias 'abj 'abscope-jump)
;; query helpers
(defalias 'abqc 'abscope-query-srcfile)
(defalias 'abqf 'abscope-query-function)
(defalias 'abqF 'abscope-query-funcref)
(defalias 'abqs 'abscope-query-struct)
(defalias 'abqp 'abscope-query-cryptic)
(defalias 'abqd 'abscope-query-define)
(defalias 'abqe 'abscope-query-enum)
(defalias 'abqx 'abscope-query-context)
;; jump helpers
(defalias 'abjc 'abscope-jump-srcfile)
(defalias 'abjf 'abscope-jump-function)
(defalias 'abjF 'abscope-jump-funcref)
(defalias 'abjs 'abscope-jump-struct)
(defalias 'abjp 'abscope-jump-cryptic)
(defalias 'abjd 'abscope-jump-define)
(defalias 'abje 'abscope-jump-enum)
;; auto complete
(defalias 'abfm 'abscope-find-members)
))
;;----------------------------------------
;; buffer local variables used
(defvar abscope-file "abscope-queries.org" "the org file where the output of abscope queries will go")
(defvar abscope-exe "abscope.exe" "the application that will run as a subprocess of this library")
(defvar abscope-process nil "the process for each project")
(defvar abscope-tq nil "the command-response queue for each process")
(defvar abscope-loc-history nil "the process for each project")
(defvar abscope-tags nil "a list of tags exported when the abscope process starts by field")
(defvar abscope-tags-all nil "every tag string in a single list")
(make-variable-buffer-local 'abscope-tq)
(make-variable-buffer-local 'abscope-process)
(make-variable-buffer-local 'abscope-loc-history)
(make-variable-buffer-local 'abscope-tags)
(make-variable-buffer-local 'abscope-tags-all)
(defun abscope-scan-src (dir)
"perform a scan on the passed directory using the default options. see `abscope-scan-args'
put #SCAN-OPTS: at the start of your query buffer to specify project specific options"
(interactive "Ddir:")
(save-window-excursion
(find-file dir)
(with-abscope-buffer
(let (addl-args)
(beginning-of-buffer)
(if (re-search-forward "#SCAN-OPTS: \\(.*\\)" nil t)
(setq addl-args (split-string-and-unquote (match-string-no-properties 1))))
(set-process-sentinel (eval `(start-process (format "abscope-scan<%s>" dir) nil ,abscope-exe ,@abscope-scan-args ,@addl-args)) (lambda (p e) (message (format "done: %s `%s'" p e)) ))
))))
(defun abscope-follow-link-hook ()
"what do do after an abscope link is followed by org mode"
)
(defun abscope-default-input ()
"default input for user actions, e.g. the current word"
(if mark-active
(buffer-substring
(region-beginning) (region-end))
(thing-at-point 'symbol)))
(defun abscope-switch-buffer (buffer)
"switch to the buffer"
(switch-to-buffer buffer))
(add-hook 'org-follow-link-hook 'abscope-follow-link-hook)
(defun abscope-buf ()
"get the buffer for abscope given the context"
(let
((dn)
(fn)
(tmp)
(tmp2)
(was-loaded)
(b)
)
;; walk up the directory tree until a project is found
(setq tmp default-directory)
(while tmp
(setq fn (concat tmp "tags.el"))
(if (file-exists-p fn)
(progn
(setq dn tmp)
(setq tmp nil))
(if (equal tmp (setq tmp2 (file-name-as-directory (expand-file-name (concat tmp ".."))))) ;;(<= (length tmp) 3) ;; on win32 c:/.. => c:/
(setq tmp nil)
(setq tmp tmp2))
)
)
(if (not dn)
(setq dn abscope-default-project-root))
(setq fn (concat (file-name-as-directory dn) abscope-file))
(if (not (find-buffer-visiting fn))
(setq was-loaded t))
(setq b (find-file-noselect fn))
;; (if was-loaded
;; (with-current-buffer b (setq buffer-read-only t)))
b
))
(defmacro with-abscope-buffer (&rest body)
"helper to set the context for abscope execution"
`(with-current-buffer (abscope-buf)
(let
(
;; (was-read-only buffer-read-only)
(res))
;; (setq buffer-read-only nil)
(setq res (progn ,@body))
;; (if was-read-only
;; (setq buffer-read-only t))
res
)
))
(defun abscope-push-loc ()
"push the current point on the abscope list"
(interactive)
(let
((m (point-marker)))
(with-abscope-buffer
(setq abscope-loc-history (cons m abscope-loc-history ))
)
)
)
(defun abscope-pop-loc ()
"pop to the last pushed abscope location"
(interactive)
(let
((m))
(with-abscope-buffer
(setq m (pop abscope-loc-history))
)
(if (not m) (message "location stack is empty")
(progn
(switch-to-buffer (marker-buffer m))
(goto-char (marker-position m))
))
)
)
(defun abscope-dir () (with-abscope-buffer default-directory))
(defun abscope-proc ()
"get the abscope process for a given project"
(with-abscope-buffer
abscope-process))
(defun abs-running-p ()
(with-abscope-buffer
(and abscope-process (equal (process-status abscope-process) 'run))))
(defun abscope-re-launch ()
"launch/re-launch the exe"
(with-abscope-buffer
(if (not (abs-running-p))
(save-window-excursion
(setq abscope-process (start-process "abscope" (current-buffer) abscope-exe "-Qa" "-"))
(setq abscope-tq (tq-create abscope-process))
(message "%s not running. launching..." abscope-exe)
(sleep-for 0 750) ;; stall for a bit to let the message show/process launch.
)
)
)
)
(defun abscope-init (project-dir)
"fire up abscope for a given directory. you may want to add this to your startup for commonly used large projects where abscope load time may be a problem"
(interactive "D project dir:")
(save-window-excursion
(with-current-buffer (find-file-noselect project-dir)
(abscope-re-launch))))
(defun abscope-kill ()
"kill abscope process in current project"
(interactive)
(with-abscope-buffer
(if (and abscope-process (not (eq (process-status abscope-process) 'run)))
(kill-process abscope-process)
)))
(defun abscope-clear ()
"erase the abscope buffer"
(interactive)
(with-abscope-buffer
(erase-buffer)))
;; (abs-print-locinfo (cdar foo) "**")
(defvar abscope-last-output nil
"last form returned by abscope")
(defvar abscope-last-output-str nil
"string last returned by abscope")
(make-variable-buffer-local 'abscope-last-output)
(make-variable-buffer-local 'abscope-last-output-str)
(defun abs-tag-from-locinfo (li)
"helper to get the tag field from a locinfo"
(and (eq (car li) 'LocInfo))
(assoc 'Tag (cdr li)))
(defun abs-proc-eval-output (proc str)
"helper for reading and evaluating any exprs in the output"
(setq abscope-last-output (eval (read str)))
)
;;(setq foo '(1 2 3))
;;(pop (reverse foo))
;; (setq foo-str "'(
;; (LocInfo
;; (File . \"a b\")
;; (Lineno . 1)
;; (Line . \"a b c d;\")
;; (Tag . \"asdf\")
;; (RefName . \"refname\")
;; (Ctxt . \"ctxt\")
;; (Ref (LocInfo
;; (File . \"file ref\")
;; (Lineno . 3)
;; (Line . \"ref line str2;\")
;; (Tag . \"ref asdf2\")
;; (RefName . \"sub refname2\")
;; (Ctxt . \"sub ctxt2\")
;; ))
;; ))
(defun abs-print-locinfo (struct-block prefix)
"print out a locinfo block of the form:
LocInfo
File foo
Lineno n
Ref r
Ctxt c"
(let
(
(File "") (Lineno "1")
(Tag "") (RefName "") (Ctxt "") (Line "") (flds)
)
(setq flds '(File Lineno Tag RefName Ctxt Line))
(loop with sym for i in struct-block do
(setq sym (car i))
(if (and (eql 'Ref (car i)) (eql 'LocInfo (caadr i)))
(abs-print-locinfo (cdadr i) (concat prefix "*"))
(loop for j in flds do
(if (eql sym j)
(set j (cdr i))
)
)
)
)
;;
(setq Ctxt
(if (string-match ".*? \\(.*\\)" Ctxt) ;; strip "struct " from "struct Foo"
(match-string 1 Ctxt)
(if (> (length Ctxt) 0) Ctxt ;; if no context, use file instead
(file-name-nondirectory File)
)
)
)
(insert (format "%s [[file:%s::%s][%s]]" prefix File Lineno Ctxt))
(insert (format "\t%s\n" Line))
)
)
(defun abs-reload-tags ()
(let
((b))
(if (file-exists-p "tags.el")
(progn
(if (not (setq b (find-buffer-visiting "tags.el")))
(save-window-excursion
(setq abscope-tags nil) ;; clear this for reload
(find-file "tags.el")
(setq b (current-buffer))))
(if (and b (or (not abscope-tags) (not (verify-visited-file-modtime b))))
(progn
(setq abscope-tags (with-current-buffer b
(revert-buffer t t)
(beginning-of-buffer)
(eval (read (current-buffer)))
))
(setq abscope-tags-all (loop for i in abscope-tags append (cadr i)))
)
)
)
)
)
)
(defun abscope-proc-print-output (proc str)
"abscope process filter: prints output to the abscope buffer"
(with-current-buffer (process-buffer proc)
(abs-reload-tags)
(save-excursion
(goto-char (process-mark proc)) ;; todo: proper mark saving.
(insert ":") ;; subtle notice of when the process finishes
(let
(
(forms)
)
(setq abscope-last-output-str str)
(condition-case err
(progn
(setq forms (abs-proc-eval-output proc str))
(loop for i in forms do
(case (car i)
('LocInfo (abs-print-locinfo (cdr i) "**"))
('QUERY_DONE) ;; nothing to do
(t (message "unknown form %s" (print i))))
)
)
(insert (format "error in output %s" (princ err)))
)
)
(set-marker (process-mark proc) (point))
)
(re-search-forward "\\[\\[" (line-end-position) t)
)
)
(defun abscope-proc-eval (proc str)
"abscope process filter: just evals the output"
(with-current-buffer (process-buffer proc)
(abs-reload-tags)
(setq abscope-last-output-str str)
(abs-proc-eval-output proc str)
))
(defun abs-proc-wait-once ()
"stall for just a short period to try to get the output
*seems* to be more responsive if you do this"
(with-abscope-buffer
;;
(tq-process-buffer abscope-tq)
(accept-process-output abscope-process 0 100 1)))
(defun abs-proc-wait-until-done ()
(loop for i from 1 to 1000 until abscope-last-output
do (abs-proc-wait-once)
)
)
(defun abs-query (type fields tag)
"fundamental function for queueing a request. fields a string with any of:
t.ag default if fields is nil.
r.ef : context-specific referrer to the tag e.g. int foo, ref is int. a->b ref is a.
c.ontext: where the tag was parsed. e.g. function, struct, global
f.ile: file where tag was parsed
l.ine: actual text line where the tag was parsed"
(with-abscope-buffer
(abscope-re-launch)
(if (equal type "") (setq type "a"))
(if (equal tag "") (error "invalid (empty) tag"))
(setq abscope-last-output nil)
(tq-enqueue abscope-tq (concat "Query " type " " (or fields "t") " " tag " End\n") "^(QUERY_DONE))\n\n" abscope-process 'abscope-proc-print-output) ;; search for (t)ags by default
(abs-proc-wait-until-done)
abscope-last-output
)
)
(defun abs-query-print (type fields tag)
"fundamental function for queueing a request. fields a string with any of:
t.ag default if fields is nil.
r.ef : context-specific referrer to the tag e.g. int foo, ref is int. a->b ref is a.
c.ontext: where the tag was parsed. e.g. function, struct, global
f.ile: file where tag was parsed
l.ine: actual text line where the tag was parsed"
(with-abscope-buffer
(abscope-re-launch)
(if (equal type "") (setq type "a"))
(if (equal tag "") (error "invalid (empty) tag"))
(end-of-buffer)
(setq pt-start (point))
(insert "\n\n* " tag)
(set-marker (process-mark abscope-process) (point))
(tq-enqueue abscope-tq (concat "Query " type " " (or fields "t") " " tag " End\n") "^(QUERY_DONE))\n\n" abscope-process 'abscope-proc-print-output) ;; search for (t)ags by default
(abs-proc-wait-once)
(tq-process-buffer abscope-tq)
)
)
(defun abs-querytype-from-char (c)
"get they query type from the passed character"
(case c
(?c 'srcfiles)
(?f 'funcs)
(?F 'funcs)
(?s 'structs)
(?p 'cryptic)
(?d 'defines)
))
(defun abs-query-read (prompt &rest fields)
(completing-read prompt
;; jump to abscope buf to get list of tags
(with-abscope-buffer
(abs-reload-tags)
(if fields
;; big list of all matching fields
(loop for field in fields
append (cadr (assoc field abscope-tags)))
abscope-tags-all))
nil nil
;; get symbol at point
(abscope-default-input)))
(defun abscope-query (type tag &optional fields)
"query a tag by type. uses pcre syntax:
- ? : for shortest match.
1. \\b Match a word boundary
2. \\B Match except at a word boundary
3. \\A Match only at beginning of string
4. \\Z Match only at end of string, or before newline at the end
5. \\z Match only at end of string
6. \\G Match only at pos() (e.g. at the end-of-match position of prior m//g)
"
(interactive (list (read-string "s (s)truct (S)tructref (f)unc (F)uncref:" "a")
(abs-query-read "tag:" nil)))
(abscope-switch-buffer (abscope-buf))
(abs-query-print type fields tag)
)
(defun abscope-query-srcfile (tag)
"find a src file"
(interactive (list (abs-query-read "src file to search for:" 'srcfiles)))
(abscope-query "c" tag)
)
(defun abscope-query-function (tag)
"find a function"
(interactive (list (abs-query-read "func to search for:" 'funcs 'defines)))
(abscope-query "f" tag)
)
(defun abscope-query-funcref (tag)
"find a function ref"
(interactive (list (abs-query-read "func to search for:" 'funcs 'defines)))
(abscope-query "F" tag)
)
(defun abscope-query-struct (tag)
"find a struct def"
(interactive (list (abs-query-read "struct to search for:" 'structs)))
(abscope-query "s" tag)
)
(defun abscope-query-cryptic (tag)
"find a cryptic def"
(interactive (list (abs-query-read "cryptic to search for:" 'cryptic)))
(abscope-query "p" tag)
)
(defun abscope-query-define (tag)
"find define"
(interactive (list (abs-query-read "The tag to search for:" 'defines)))
(abscope-query "ad" tag)
)
(defun abscope-query-enum (tag)
"find an enum"
(interactive (list (abs-query-read "The tag to search for:" 'defines)))
(abscope-query "e" tag)
)
(defun abscope-query-context (tag)
"find everything with 'tag' in its context def"
(interactive (list (read-string "context to search for:" (abscope-default-input))))
(abscope-query "a" tag "x")
)
;; *************************************************************************
;; smart jump directly to query
;; *************************************************************************
(defun abscope-jump(tag flags &optional flds)
(interactive (list (read-string "tag:" (abscope-default-input))
(read-string "flags (s)truct (f)unc:" "a" 'abscope-find-members-history)))
(interactive)
(let
(
(li)
(loc)
(lis)
)
(save-window-excursion
(abscope-push-loc)
(with-abscope-buffer
(setq abscope-last-output nil)
(abs-query-print flags nil tag) ;; this logs the history as well as provides the info
(abs-proc-wait-until-done)
(if (not abscope-last-output)
(error "no info for type %s" vartype))
(setq lis (loop for i in abscope-last-output
if (eq (car i) 'LocInfo) collect (cons (cdr (assoc 'Tag (cdr i))) (cdr i))))
(or (> (length lis) 0) (error "no locations found for %s" tag))
(setq loc (or
(assoc tag lis)
(if (= (length lis) 1)
(car lis)
(progn
(setq li (completing-read "jump to:" lis nil t nil 'abscope-find-members-history nil nil))
(if (not li)
(error "no location chosen"))
(assoc li lis))
)
)
)
)
)
(if (not loc)
(error "no location found"))
(find-file (concat (abscope-dir) (cdr (assoc 'File loc))))
(goto-line (string-to-number (cdr (assoc 'Lineno loc))))
)
)
(defun abscope-jump-srcfile (tag)
"jump srcfile"
(interactive (list (abs-query-read "srcfile:" 'srcfiles)))
(abscope-jump tag "c"))
(defun abscope-jump-function (tag)
"jump to func/define"
(interactive (list (abs-query-read "func:" 'funcs)))
(abscope-jump tag "fd"))
(defun abscope-jump-struct (tag)
"jump struct"
(interactive (list (abs-query-read "struct:" 'structs)))
(abscope-jump tag "s"))
(defun abscope-jump-cryptic (tag)
"jump cryptic"
(interactive (list (abs-query-read "cryptic:" 'cryptic)))
(abscope-jump tag "s"))
(defun abscope-jump-define (tag)
"jump #define"
(interactive (list (abs-query-read "define:" 'defines)))
(abscope-jump tag "d"))
(defun abscope-jump-enum (tag)
"jump enum"
(interactive (list (abs-query-read "enum:" 'defines)))
(abscope-jump tag "e"))
;; *************************************************************************
;; navigating matches
;; *************************************************************************
(defun abscope-next-match ()
"find the next match and go to it"
(interactive)
(let
((buf))
(find-file (buffer-file-name (abscope-buf)))
(setq buf (save-window-excursion
(org-open-at-point)
(current-buffer))
)
(forward-line 1)
(abscope-switch-buffer buf)
)
)
(defun abscope-prev-match ()
"find the next match and go to it"
(interactive)
(let
((buf))
(find-file (abscope-buf))
(forward-line -1)
(setq buf (save-window-excursion
(org-open-at-point)
(current-buffer))
)
(abscope-switch-buffer buf)
)
)
;; *************************************************************************
;; smart inserts/completes (work in progress)
;; *************************************************************************
(defun abscope-query-replace ()
"do a query-replace on each match in the current query from the current point in the query buffer."
(interactive)
(while t
(abscope-next-match)
(call-interactively 'query-replace-regexp)))
(defun abscope-find-var-type (varname)
"int a; ... should return int"
(let
(
(pt-max (point))
(s)
)
(save-excursion
(beginning-of-defun)
(re-search-forward varname pt-max)
(backward-sexp 2)
(setq s (thing-at-point 'symbol))
(if (equal s "NOCONST")
(progn
(forward-char 1)
(thing-at-point 'symbol))
s)
)))
(defun abscope-gather-vars (start end)
"gather variable decls in a region"
(save-excursion
(goto-char start)
(loop while (re-search-forward ".*?\\(\\b[a-z_]+\\b\\);" end t)
collect (cons (match-string-no-properties 1) (match-string-no-properties 0)))))
(defun abscope-gather-struct-vars ()
(save-excursion
(abscope-gather-vars (point) (progn (end-of-defun) (point)))
)
)
(defun abscope-insert(tag flags)
"do a query, and then insert the results"
(interactive (list (read-string "tag:" (abscope-default-input))
(read-string "flags (s)truct (f)unc:" "a" 'abscope-find-members-history)))
(interactive)
(let
(
(li)
(loc)
(lis)
)
(save-window-excursion
(with-abscope-buffer
(setq abscope-last-output nil)
(abscope-query flags tag) ;; this logs the history as well as provides the info
(abs-proc-wait-until-done)
(if (not abscope-last-output)
(error "no info for type %s" vartype))
(setq lis (loop for i in abscope-last-output
if (eq (car i) 'LocInfo) collect (cons (cdr (assoc 'Tag (cdr i))) (cdr i))))
(setq li (completing-read "insert:" lis nil t (caar lis) 'abscope-find-members-history nil nil))
(if (not li)
(error "no location chosen"))
)
)
(insert li)
)
)
(defvar abs-find-members-history)
(defvar abs-find-members-last-type)
(make-variable-buffer-local 'abs-find-members-history)
(make-variable-buffer-local 'abs-find-members-last-type)
(defun abs-nearest-tag ()
"helper for finding a previous tag"
(save-excursion
(backward-word)
(thing-at-point 'symbol)))
(defun abscope-find-members (varname)
"autocomplete struct members: if you call this with point after a-> it will try to get the type for 'a' and get you the members."
(interactive (list (abs-nearest-tag)))
(let
(
(vartype)
(mbrs)
(li)
(mbr)
(choice)
)
(save-excursion
(if (not varname)
(error "no var found"))
(setq vartype (abscope-find-var-type (concat "\\b" varname "\\b")))
(if (not vartype)
(error "couldn't find vartype for var %s" varname)
)
(setq vartype (concat "\\b" vartype "\\b" ))
(setq abscope-last-output nil)
(save-window-excursion
(with-abscope-buffer
(abs-query-print "s" nil vartype)
(abs-proc-wait-until-done)
(if (not abscope-last-output)
(error "no info for type %s" vartype))
(if (equal (car abscope-last-output) '(QUERY_DONE))
(error "no matches found for type %s" vartype))
(setq li (cdar abscope-last-output))
(find-file (cdr (assoc 'File li)))
(goto-line (string-to-number (cdr (assoc 'Lineno li))))
(setq mbrs (abscope-gather-struct-vars)))
)
)
(setq choice (completing-read "mbr:" mbrs nil t nil 'abs-find-members-history nil nil))
(setq mbr (assoc choice mbrs))
(insert choice)
(if (string-match " *\b\\(.*?\\) " (cdr mbr))
(setq abs-find-members-last-type (match-string 1 (cdr mbr))))
)
)
(defun abs-choose-result ()
(let
(
(o)
(c)
)
(setq o (loop for j from 0 for i in abscope-last-output
if (eq (car i) 'LocInfo) collect (cons (format "%s" (cdr (assoc 'Tag (cdr i)))) (cdr i))))
(setq c
(if (> (length o) 1)
(completing-read "tag: " o nil t nil 'abs-find-members-history)
(caar o)))
(cdr (assoc c o))
)
)
(defun abscope-find-params (funcname)
"autocomplete for function params. call this with point after opening parens"
(interactive (list (abs-nearest-tag)))
(let
(
(li)
(parms-str)
(parms)
(p)
(tag)
)
(if (not funcname)
(error "no func found"))
(with-abscope-buffer
(abs-query-print "fd" nil (format "\\b%s\\b" funcname))
(abs-proc-wait-until-done)
(if (not abscope-last-output)
(error "no info for func %s" funcname))
(setq li (abs-choose-result))
(save-window-excursion
(find-file (cdr (assoc 'File li)))
(goto-line (string-to-number (cdr (assoc 'Lineno li))))
(re-search-forward "(\\(.*\\))" nil t)
(setq parms-str (match-string 1))
)
)
(setq tag (cdr (assoc 'Tag li)))
(setq parms (split-string parms-str ","))
(loop with first = nil for i in parms do
(if first
(insert ", "))
(setq first t)
(setq p (read-string (concat tag ": " i " :")))
(insert p))
)
)
;; *************************************************************************
;; autocomplete intergration. very handy
;; *************************************************************************
(defun c-parse-curfunc ()
"try to figure out the name of the function the current point is in"
(save-excursion
;; match () pairs and find the first one missing a close
(let ((closes 0)
func)
(while (and (re-search-backward "\\((\\|)\\)" nil t) (not func))
(if (equal ")" (match-string 0))
(incf closes)
(progn
(decf closes)
(if (< closes 0)
(setq func (thing-at-point 'symbol)))
)
)
)
func)))
(defvar absp-last-match-q nil
"the last matched popup query")
(make-local-variable 'absp-last-match-q)
(defvar absp-last-match-cancelled nil
"was the last match C-g'd")
(make-local-variable 'absp-last-match-cancelled)
(defun abs-popupinfo-cb ()
"helper function that pops abscope info up about the current context"
(interactive)
(let
(f q r)
(if (and (string-match "c" (file-name-extension (buffer-name)))
(setq f (c-parse-curfunc))
(setq q (abs-query "f" nil (concat "\\b" f "\\b")))
)
(if (and (equal q absp-last-match-q) absp-last-match-cancelled)
nil ;; if we've done this query before and it was cancelled
(progn
(setq absp-last-match-cancelled nil)
(setq r (cdr (assoc 'Line (car q))))
(if r
(popupinfo r)
)
)
)
)
)
)
(defun abscope-autocomplete-candidate-words ()
"helper for auto-completing a word"
(cond
;; excludes
((re-search-backward "//" (line-beginning-position) t) nil)
((oddp (count-matches "\\\"" (line-beginning-position) (point))) nil) ;; in a string
((save-excursion
(goto-char ac-point)
(or (looking-back "typedef \\(struct\\)? "))))
;; context specific matches
((string-match "($" ac-prefix) (abscope-autocomplete-candidate-functions))
(t
(if (> (length ac-prefix) 3)
(append
(with-abscope-buffer
(all-completions ac-prefix abscope-tags-all))
(ac-candidate-words-in-buffer))
))
))
(defun abscope-autocomplete-docs (buf)
"If I ever do anything here: staying on a single auto-complete item for a period of time will pop this up. so... put something useful here per function/struct/etc."
"")
(defvar abscope-autocomplete-sources '((candidates . abscope-autocomplete-candidate-words)
(document . abscope-autocomplete-docs)))
(defun abscope-autocomplete-init ()
(interactive)
"set up the auto complete variables"
(setq ac-sources '(
ac-source-words-in-same-mode-buffers
abscope-autocomplete-sources
))
(add-hook 'post-command-hook 'abs-popupinfo-cb nil t)
;; (remove-hook 'post-command-hook 'abs-popupinfo-cb)
)
(provide 'abscope)
;; abscope-find-members : currently the thing to use to autocomplete struct members