-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcc-engine.el
2796 lines (2708 loc) · 98.6 KB
/
cc-engine.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
;;; cc-engine.el --- core syntax guessing engine for CC mode
;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
;; Authors: 2000- Martin Stjernholm
;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
;; 1992-1997 Barry A. Warsaw
;; 1987 Dave Detlefs and Stewart Clamen
;; 1985 Richard M. Stallman
;; Maintainer: [email protected]
;; Created: 22-Apr-1997 (split from cc-mode.el)
;; Version: See cc-mode.el
;; Keywords: c languages oop
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;; Code:
(eval-when-compile
(let ((load-path
(if (and (boundp 'byte-compile-dest-file)
(stringp byte-compile-dest-file))
(cons (file-name-directory byte-compile-dest-file) load-path)
load-path)))
(require 'cc-bytecomp)))
(cc-require 'cc-defs)
(cc-require 'cc-vars)
(cc-require 'cc-langs)
;; Silence the compiler.
(cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
(defvar c-state-cache nil)
(defvar c-in-literal-cache t)
;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
;; better way should be implemented, but this will at least shut up
;; the byte compiler.
(defvar c-maybe-labelp nil)
;; WARNING WARNING WARNING
;;
;; Be *exceptionally* careful about modifications to this function!
;; Much of CC Mode depends on this Doing The Right Thing. If you
;; break it you will be sorry. If you think you know how this works,
;; you probably don't. No human on Earth does! :-)
;;
;; WARNING WARNING WARNING
(defun c-beginning-of-statement-1 (&optional lim)
;; move to the start of the current statement, or the previous
;; statement if already at the beginning of one.
(let ((firstp t)
(substmt-p t)
donep c-in-literal-cache saved
(last-begin (point)))
;; first check for bare semicolon
(if (and (progn (c-backward-syntactic-ws lim)
(eq (char-before) ?\;))
(c-safe (progn (forward-char -1)
(setq saved (point))
t))
(progn (c-backward-syntactic-ws lim)
(memq (char-before) '(?\; ?{ ?:)))
)
(setq last-begin saved)
(goto-char last-begin)
(while (not donep)
;; stop at beginning of buffer
(if (bobp) (setq donep t)
;; go backwards one balanced expression, but be careful of
;; unbalanced paren being reached
(if (not (c-safe (progn (c-backward-sexp 1) t)))
(progn
(if firstp
(backward-up-list 1)
(goto-char last-begin))
;; skip over any unary operators, or other special
;; characters appearing at front of identifier
(save-excursion
(c-backward-syntactic-ws lim)
(skip-chars-backward "-+!*&:.~@ \t\n")
(if (eq (char-before) ?\()
(setq last-begin (point))))
(goto-char last-begin)
(setq donep t)))
(setq c-maybe-labelp nil)
;; see if we're in a literal. if not, then this bufpos may be
;; a candidate for stopping
(cond
;; CASE 0: did we hit the error condition above?
(donep)
;; CASE 1: are we in a literal?
((eq (c-in-literal lim) 'pound)
(beginning-of-line))
;; CASE 2: some other kind of literal?
((c-in-literal lim))
;; CASE 3: are we looking at a conditional keyword?
((or (and c-conditional-key (looking-at c-conditional-key))
(and (eq (char-after) ?\()
(save-excursion
(c-forward-sexp 1)
(c-forward-syntactic-ws)
(not (eq (char-after) ?\;)))
(let ((here (point))
(foundp (progn
(c-backward-syntactic-ws lim)
(forward-word -1)
(and lim
(<= lim (point))
(not (c-in-literal lim))
(not (eq (char-before) ?_))
c-conditional-key
(looking-at c-conditional-key)
))))
;; did we find a conditional?
(if (not foundp)
(goto-char here))
foundp)))
;; are we in the middle of an else-if clause?
(if (save-excursion
(and (not substmt-p)
(c-safe (progn (c-forward-sexp -1) t))
(looking-at "\\<else\\>[ \t\n]+\\<if\\>")
(not (c-in-literal lim))))
(progn
(c-forward-sexp -1)
(c-backward-to-start-of-if lim)))
;; are we sitting at an else clause, that we are not a
;; substatement of?
(if (and (not substmt-p)
(looking-at "\\<else\\>[^_]"))
(c-backward-to-start-of-if lim))
;; a finally or a series of catches?
(if (not substmt-p)
(while (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")
(c-safe (c-backward-sexp 2))
(if (eq (char-after) ?\()
(c-safe (c-backward-sexp)))))
;; are we sitting at the while of a do-while?
(if (and (looking-at "\\<while\\>[^_]")
(c-backward-to-start-of-do lim))
(setq substmt-p nil))
(setq last-begin (point)
donep substmt-p))
;; CASE 4: are we looking at a label? (But we handle
;; switch labels later.)
((and (looking-at c-label-key)
(not (looking-at "default\\>"))
(not (and (c-major-mode-is 'pike-mode)
(save-excursion
;; Not inside a Pike type declaration?
(and (c-safe (backward-up-list 1) t)
(eq (char-after) ?\()))))))
;; CASE 5: is this the first time we're checking?
(firstp (setq firstp nil
substmt-p (not (c-crosses-statement-barrier-p
(point) last-begin))
last-begin (point)))
;; CASE 6: have we crossed a statement barrier?
((save-excursion
;; Move over in-expression blocks before checking the
;; barrier
(if (or (memq (char-after) '(?\( ?\[))
(and (eq (char-after) ?{)
(c-looking-at-inexpr-block lim)))
(c-forward-sexp 1))
(c-crosses-statement-barrier-p (point) last-begin))
(setq donep t))
;; CASE 7: ignore labels
((and c-maybe-labelp
(or (and c-access-key (looking-at c-access-key))
;; with switch labels, we have to go back further
;; to try to pick up the case or default
;; keyword. Potential bogosity alert: we assume
;; `case' or `default' is first thing on line
(let ((here (point)))
(beginning-of-line)
(c-forward-syntactic-ws here)
(if (looking-at c-switch-label-key)
t
(goto-char here)
nil)))))
;; CASE 8: ObjC or Java method def
((and c-method-key
(setq last-begin (c-in-method-def-p)))
(setq donep t))
;; CASE 9: Normal token. At bob, we can end up at ws or a
;; comment, and last-begin shouldn't be updated then.
((not (looking-at "\\s \\|/[/*]"))
(setq last-begin (point)))
))))
(goto-char last-begin)
;; We always want to skip over the non-whitespace modifier
;; characters that can start a statement.
(let ((lim (point)))
(skip-chars-backward "-+!*&~@`# \t\n" (c-point 'boi))
(skip-chars-forward " \t\n" lim))))
(defun c-end-of-statement-1 ()
(condition-case nil
(let (beg end found)
(while (and (not (eobp))
(progn
(setq beg (point))
(c-forward-sexp 1)
(setq end (point))
(goto-char beg)
(setq found nil)
(while (and (not found)
(re-search-forward "[;{}]" end t))
(if (not (c-in-literal beg))
(setq found t)))
(not found)))
(goto-char end))
(re-search-backward "[;{}]")
(forward-char 1))
(error
(let ((beg (point)))
(c-safe (backward-up-list -1))
(let ((end (point)))
(goto-char beg)
(search-forward ";" end 'move)))
)))
(defun c-crosses-statement-barrier-p (from to)
;; Does buffer positions FROM to TO cross a C statement boundary?
(let ((here (point))
(lim from)
crossedp)
(condition-case ()
(progn
(goto-char from)
(while (and (not crossedp)
(< (point) to))
(skip-chars-forward "^;{}:" (1- to))
(if (not (c-in-literal lim))
(progn
(if (memq (char-after) '(?\; ?{ ?}))
(setq crossedp t)
(if (eq (char-after) ?:)
(setq c-maybe-labelp t))
(forward-char 1))
(setq lim (point)))
(forward-char 1))))
(error (setq crossedp nil)))
(goto-char here)
crossedp))
(defun c-beginning-of-macro (&optional lim)
;; Go to the beginning of a cpp macro definition. Leaves point at
;; the beginning of the macro and returns t if in a cpp macro
;; definition, otherwise returns nil and leaves point unchanged.
;; `lim' is currently ignored, but the interface requires it.
(let ((here (point)))
(beginning-of-line)
(while (eq (char-before (1- (point))) ?\\)
(forward-line -1))
(back-to-indentation)
(if (and (<= (point) here)
(eq (char-after) ?#))
t
(goto-char here)
nil)))
;; Skipping of "syntactic whitespace", defined as lexical whitespace,
;; C and C++ style comments, and preprocessor directives. Search no
;; farther back or forward than optional LIM. If LIM is omitted,
;; `beginning-of-defun' is used for backward skipping, point-max is
;; used for forward skipping.
(defun c-forward-syntactic-ws (&optional lim)
;; Forward skip of syntactic whitespace for Emacs 19.
(let* ((here (point-max))
(hugenum (point-max)))
(while (/= here (point))
(setq here (point))
(c-forward-comment hugenum)
;; skip preprocessor directives
(when (and (eq (char-after) ?#)
(= (c-point 'boi) (point)))
(while (and (eq (char-before (c-point 'eol)) ?\\)
(= (forward-line 1) 0)))
(end-of-line))
)
(if lim (goto-char (min (point) lim)))))
(defun c-backward-syntactic-ws (&optional lim)
;; Backward skip over syntactic whitespace for Emacs 19.
(let* ((here (point-min))
(hugenum (- (point-max))))
(while (/= here (point))
(setq here (point))
(c-forward-comment hugenum)
(c-beginning-of-macro))
(if lim (goto-char (max (point) lim)))))
;; Moving by tokens, where a token is defined as all symbols and
;; identifiers which aren't syntactic whitespace (note that "->" is
;; considered to be two tokens). Point is always either left at the
;; beginning of a token or not moved at all. COUNT specifies the
;; number of tokens to move; a negative COUNT moves in the opposite
;; direction. A COUNT of 0 moves to the next token beginning only if
;; not already at one. If BALANCED is true, move over balanced
;; parens, otherwise move into them. Also, if BALANCED is true, never
;; move out of an enclosing paren. LIM sets the limit for the
;; movement and defaults to the point limit. Returns the number of
;; tokens left to move (positive or negative). If BALANCED is true, a
;; move over a balanced paren counts as one. Note that if COUNT is 0
;; and no appropriate token beginning is found, 1 will be returned.
;; Thus, a return value of 0 guarantees that point is at the requested
;; position and a return value less (without signs) than COUNT
;; guarantees that point is at the beginning of some token.
(defun c-forward-token-1 (&optional count balanced lim)
(or count (setq count 1))
(if (< count 0)
(- (c-backward-token-1 (- count) balanced lim))
(let ((jump-syntax (if balanced
'(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
'(?w ?_ ?\" ?\\ ?/ ?')))
(last (point))
(prev (point)))
(save-restriction
(if lim (narrow-to-region (point-min) lim))
(if (/= (point)
(progn (c-forward-syntactic-ws) (point)))
;; Skip whitespace. Count this as a move if we did in fact
;; move and aren't out of bounds.
(or (eobp)
(setq count (max (1- count) 0))))
(if (and (= count 0)
(or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
(memq (char-syntax (or (char-before) ? )) '(?w ?_)))
(eobp)))
;; If count is zero we should jump if in the middle of a
;; token or if there is whitespace between point and the
;; following token beginning.
(setq count 1))
(if (eobp)
(goto-char last)
;; Avoid having the limit tests inside the loop.
(condition-case nil
(while (> count 0)
(setq prev last
last (point))
(if (memq (char-syntax (char-after)) jump-syntax)
(goto-char (scan-sexps (point) 1))
(forward-char))
(c-forward-syntactic-ws lim)
(setq count (1- count)))
(error (goto-char last)))
(when (eobp)
(goto-char prev)
(setq count (1+ count)))))
count)))
(defun c-backward-token-1 (&optional count balanced lim)
(or count (setq count 1))
(if (< count 0)
(- (c-forward-token-1 (- count) balanced lim))
(let ((jump-syntax (if balanced
'(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
'(?w ?_ ?\" ?\\ ?/ ?')))
last)
(if (and (= count 0)
(or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
(memq (char-syntax (or (char-before) ? )) '(?w ?_)))
(/= (point)
(save-excursion (c-forward-syntactic-ws) (point)))
(eobp)))
;; If count is zero we should jump if in the middle of a
;; token or if there is whitespace between point and the
;; following token beginning.
(setq count 1))
(save-restriction
(if lim (narrow-to-region lim (point-max)))
(or (bobp)
(progn
;; Avoid having the limit tests inside the loop.
(condition-case nil
(while (progn
(setq last (point))
(> count 0))
(c-backward-syntactic-ws lim)
(if (memq (char-syntax (char-before)) jump-syntax)
(goto-char (scan-sexps (point) -1))
(backward-char))
(setq count (1- count)))
(error (goto-char last)))
(if (bobp) (goto-char last)))))
count)))
;; Return `c' if in a C-style comment, `c++' if in a C++ style
;; comment, `string' if in a string literal, `pound' if on a
;; preprocessor line, or nil if not in a comment at all. Optional LIM
;; is used as the backward limit of the search. If omitted, or nil,
;; `beginning-of-defun' is used."
(defun c-in-literal (&optional lim)
;; Determine if point is in a C++ literal. we cache the last point
;; calculated if the cache is enabled
(if (and (vectorp c-in-literal-cache)
(= (point) (aref c-in-literal-cache 0)))
(aref c-in-literal-cache 1)
(let ((rtn (save-excursion
(let* ((lim (or lim (c-point 'bod)))
(state (parse-partial-sexp lim (point))))
(cond
((nth 3 state) 'string)
((nth 4 state) (if (nth 7 state) 'c++ 'c))
((c-beginning-of-macro lim) 'pound)
(t nil))))))
;; cache this result if the cache is enabled
(if (not c-in-literal-cache)
(setq c-in-literal-cache (vector (point) rtn)))
rtn)))
;; XEmacs has a built-in function that should make this much quicker.
;; I don't think we even need the cache, which makes our lives more
;; complicated anyway. In this case, lim is ignored.
(defun c-fast-in-literal (&optional lim)
(let ((context (buffer-syntactic-context)))
(cond
((eq context 'string) 'string)
((eq context 'comment) 'c++)
((eq context 'block-comment) 'c)
((save-excursion (c-beginning-of-macro lim)) 'pound))))
(if (fboundp 'buffer-syntactic-context)
(defalias 'c-in-literal 'c-fast-in-literal))
(defun c-literal-limits (&optional lim near not-in-delimiter)
;; Returns a cons of the beginning and end positions of the comment
;; or string surrounding point (including both delimiters), or nil
;; if point isn't in one. If LIM is non-nil, it's used as the
;; "safe" position to start parsing from. If NEAR is non-nil, then
;; the limits of any literal next to point is returned. "Next to"
;; means there's only [ \t] between point and the literal. The
;; search for such a literal is done first in forward direction. If
;; NOT-IN-DELIMITER is non-nil, the case when point is inside a
;; starting delimiter won't be recognized. This only has effect for
;; comments, which have starting delimiters with more than one
;; character.
(save-excursion
(let* ((pos (point))
(lim (or lim (c-point 'bod)))
(state (parse-partial-sexp lim (point))))
(cond ((nth 3 state)
;; String. Search backward for the start.
(while (nth 3 state)
(search-backward (make-string 1 (nth 3 state)))
(setq state (parse-partial-sexp lim (point))))
(cons (point) (or (c-safe (c-forward-sexp 1) (point))
(point-max))))
((nth 7 state)
;; Line comment. Search from bol for the comment starter.
(beginning-of-line)
(setq state (parse-partial-sexp lim (point))
lim (point))
(while (not (nth 7 state))
(search-forward "//") ; Should never fail.
(setq state (parse-partial-sexp
lim (point) nil nil state)
lim (point)))
(backward-char 2)
(cons (point) (progn (c-forward-comment 1) (point))))
((nth 4 state)
;; Block comment. Search backward for the comment starter.
(while (nth 4 state)
(search-backward "/*") ; Should never fail.
(setq state (parse-partial-sexp lim (point))))
(cons (point) (progn (c-forward-comment 1) (point))))
((and (not not-in-delimiter)
(not (nth 5 state))
(eq (char-before) ?/)
(looking-at "[/*]"))
;; We're standing in a comment starter.
(backward-char 1)
(cons (point) (progn (c-forward-comment 1) (point))))
(near
(goto-char pos)
;; Search forward for a literal.
(skip-chars-forward " \t")
(cond
((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
(cons (point) (or (c-safe (c-forward-sexp 1) (point))
(point-max))))
((looking-at "/[/*]") ; Line or block comment.
(cons (point) (progn (c-forward-comment 1) (point))))
(t
;; Search backward.
(skip-chars-backward " \t")
(let ((end (point)) beg)
(cond
((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
(setq beg (c-safe (c-backward-sexp 1) (point))))
((and (c-safe (forward-char -2) t)
(looking-at "*/"))
;; Block comment. Due to the nature of line
;; comments, they will always be covered by the
;; normal case above.
(goto-char end)
(c-forward-comment -1)
;; If LIM is bogus, beg will be bogus.
(setq beg (point))))
(if beg (cons beg end))))))
))))
(defun c-literal-limits-fast (&optional lim near not-in-delimiter)
;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
;; returns the pos of the comment start.
(save-excursion
(let* ((pos (point))
(lim (or lim (c-point 'bod)))
(state (parse-partial-sexp lim (point))))
(cond ((nth 3 state) ; String.
(goto-char (nth 8 state))
(cons (point) (or (c-safe (c-forward-sexp 1) (point))
(point-max))))
((nth 4 state) ; Comment.
(goto-char (nth 8 state))
(cons (point) (progn (c-forward-comment 1) (point))))
((and (not not-in-delimiter)
(not (nth 5 state))
(eq (char-before) ?/)
(looking-at "[/*]"))
;; We're standing in a comment starter.
(backward-char 1)
(cons (point) (progn (c-forward-comment 1) (point))))
(near
(goto-char pos)
;; Search forward for a literal.
(skip-chars-forward " \t")
(cond
((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
(cons (point) (or (c-safe (c-forward-sexp 1) (point))
(point-max))))
((looking-at "/[/*]") ; Line or block comment.
(cons (point) (progn (c-forward-comment 1) (point))))
(t
;; Search backward.
(skip-chars-backward " \t")
(let ((end (point)) beg)
(cond
((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
(setq beg (c-safe (c-backward-sexp 1) (point))))
((and (c-safe (forward-char -2) t)
(looking-at "*/"))
;; Block comment. Due to the nature of line
;; comments, they will always be covered by the
;; normal case above.
(goto-char end)
(c-forward-comment -1)
;; If LIM is bogus, beg will be bogus.
(setq beg (point))))
(if beg (cons beg end))))))
))))
(if (c-safe (> (length (save-excursion (parse-partial-sexp 1 1))) 8))
(defalias 'c-literal-limits 'c-literal-limits-fast))
(defun c-collect-line-comments (range)
;; If the argument is a cons of two buffer positions (such as
;; returned by c-literal-limits), and that range contains a C++
;; style line comment, then an extended range is returned that
;; contains all adjacent line comments (i.e. all comments that
;; starts in the same column with no empty lines or non-whitespace
;; characters between them). Otherwise the argument is returned.
(save-excursion
(condition-case nil
(if (and (consp range) (progn
(goto-char (car range))
(looking-at "//")))
(let ((col (current-column))
(beg (point))
(bopl (c-point 'bopl))
(end (cdr range)))
;; Got to take care in the backward direction to handle
;; comments which are preceded by code.
(while (and (c-forward-comment -1)
(>= (point) bopl)
(looking-at "//")
(= col (current-column)))
(setq beg (point)
bopl (c-point 'bopl)))
(goto-char end)
(while (and (progn (skip-chars-forward " \t")
(looking-at "//"))
(= col (current-column))
(prog1 (zerop (forward-line 1))
(setq end (point)))))
(cons beg end))
range)
(error range))))
(defun c-literal-type (range)
;; Convenience function that given the result of c-literal-limits,
;; returns nil or the type of literal that the range surrounds.
;; It's much faster than using c-in-literal and is intended to be
;; used when you need both the type of a literal and its limits.
(if (consp range)
(save-excursion
(goto-char (car range))
(cond ((eq (char-syntax (or (char-after) ?\ )) ?\") 'string)
((looking-at "//") 'c++)
(t 'c))) ; Assuming the range is valid.
range))
;; utilities for moving and querying around syntactic elements
(defvar c-parsing-error nil)
(defun c-parse-state ()
;; Finds and records all open parens between some important point
;; earlier in the file and point.
;;
;; if there's a state cache, return it
(if c-state-cache c-state-cache
(let* (at-bob
(pos (save-excursion
;; go back 2 bods, but ignore any bogus positions
;; returned by beginning-of-defun (i.e. open paren
;; in column zero)
(let ((cnt 2))
(while (not (or at-bob (zerop cnt)))
(goto-char (c-point 'bod))
(if (and
(eq (char-after) ?\{)
;; The following catches an obscure special
;; case where the brace is preceded by an
;; open paren. That can only legally occur
;; with blocks inside expressions and in
;; Pike special brace lists. Even so, this
;; test is still bogus then, but hopefully
;; good enough. (We don't want to use
;; up-list here since it might be slow.)
(save-excursion
(c-backward-syntactic-ws)
(not (eq (char-before) ?\())))
(setq cnt (1- cnt)))
(if (bobp)
(setq at-bob t))))
(point)))
(here (save-excursion
;;(skip-chars-forward " \t}")
(point)))
(last-bod here) (last-pos pos)
placeholder state sexp-end)
;; cache last bod position
(while (catch 'backup-bod
(setq state nil)
(while (and pos (< pos here))
(setq last-pos pos)
(if (and (setq pos (c-safe (scan-lists pos 1 -1)))
(<= pos here))
(progn
(setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
(if (and sexp-end
(<= sexp-end here))
;; we want to record both the start and end
;; of this sexp, but we only want to record
;; the last-most of any of them before here
(progn
(if (eq (char-after (1- pos)) ?\{)
(setq state (cons (cons (1- pos) sexp-end)
(if (consp (car state))
(cdr state)
state))))
(setq pos sexp-end))
;; we're contained in this sexp so put pos on
;; front of list
(setq state (cons (1- pos) state))))
;; something bad happened. check to see if we
;; crossed an unbalanced close brace. if so, we
;; didn't really find the right `important bufpos'
;; so lets back up and try again
(if (and (not pos) (not at-bob)
(setq placeholder
(c-safe (scan-lists last-pos 1 1)))
;;(char-after (1- placeholder))
(<= placeholder here)
(eq (char-after (1- placeholder)) ?\}))
(while t
(setq last-bod (c-safe (scan-lists last-pos -1 1)))
(if (not last-bod)
(save-excursion
;; bogus, but what can we do here?
(goto-char placeholder)
(beginning-of-line)
(setq c-parsing-error
(format "\
Unbalanced close brace at line %d" (1+ (count-lines 1 (point)))))
(throw 'backup-bod nil))
(setq at-bob (= last-bod (point-min))
pos last-bod)
(if (= (char-after last-bod) ?\{)
(throw 'backup-bod t)))
)) ;end-if
)) ;end-while
nil))
state)))
(defun c-whack-state (bufpos state)
;; whack off any state information that appears on STATE which lies
;; after the bounds of BUFPOS.
(let (newstate car)
(while state
(setq car (car state)
state (cdr state))
(if (consp car)
;; just check the car, because in a balanced brace
;; expression, it must be impossible for the corresponding
;; close brace to be before point, but the open brace to be
;; after.
(if (<= bufpos (car car))
nil ; whack it off
;; its possible that the open brace is before bufpos, but
;; the close brace is after. In that case, convert this
;; to a non-cons element.
(if (<= bufpos (cdr car))
(setq newstate (append newstate (list (car car))))
;; we know that both the open and close braces are
;; before bufpos, so we also know that everything else
;; on state is before bufpos, so we can glom up the
;; whole thing and exit.
(setq newstate (append newstate (list car) state)
state nil)))
(if (<= bufpos car)
nil ; whack it off
;; it's before bufpos, so everything else should too
(setq newstate (append newstate (list car) state)
state nil))))
newstate))
(defun c-hack-state (bufpos which state)
;; Using BUFPOS buffer position, and WHICH (must be 'open or
;; 'close), hack the c-parse-state STATE and return the results.
(if (eq which 'open)
(let ((car (car state)))
(if (or (null car)
(consp car)
(/= bufpos car))
(cons bufpos state)
state))
(if (not (eq which 'close))
(error "c-hack-state, bad argument: %s" which))
;; 'close brace
(let ((car (car state))
(cdr (cdr state)))
(if (consp car)
(setq car (car cdr)
cdr (cdr cdr)))
;; TBD: is this test relevant???
(if (consp car)
state ;on error, don't change
;; watch out for balanced expr already on cdr of list
(cons (cons car bufpos)
(if (consp (car cdr))
(cdr cdr) cdr))
))))
(defun c-adjust-state (from to shift state)
;; Adjust all points in state that lie in the region FROM..TO by
;; SHIFT amount.
(mapcar
(function
(lambda (e)
(if (consp e)
(let ((car (car e))
(cdr (cdr e)))
(if (and (<= from car) (< car to))
(setcar e (+ shift car)))
(if (and (<= from cdr) (< cdr to))
(setcdr e (+ shift cdr))))
(if (and (<= from e) (< e to))
(setq e (+ shift e))))
e))
state))
(defun c-beginning-of-inheritance-list (&optional lim)
;; Go to the first non-whitespace after the colon that starts a
;; multiple inheritance introduction. Optional LIM is the farthest
;; back we should search.
(let* ((lim (or lim (c-point 'bod)))
(placeholder (progn
(back-to-indentation)
(point)))
(chr (char-after)))
(c-backward-syntactic-ws lim)
(while (and (> (point) lim)
(or (eq chr ?,)
(memq (char-before) '(?, ?:)))
(progn
(beginning-of-line)
(setq placeholder (point))
(skip-chars-forward " \t")
(setq chr (char-after))
(not (looking-at c-class-key))
))
(c-backward-syntactic-ws lim))
(goto-char placeholder)
(skip-chars-forward "^:" (c-point 'eol))))
(defun c-in-method-def-p ()
;; Return nil if we aren't in a method definition, otherwise the
;; position of the initial [+-].
(save-excursion
(beginning-of-line)
(and c-method-key
(looking-at c-method-key)
(point))
))
(defun c-at-toplevel-p ()
"Return a determination as to whether point is at the `top-level'.
Being at the top-level means that point is either outside any
enclosing block (such function definition), or inside a class
definition, but outside any method blocks.
If point is not at the top-level (e.g. it is inside a method
definition), then nil is returned. Otherwise, if point is at a
top-level not enclosed within a class definition, t is returned.
Otherwise, a 2-vector is returned where the zeroth element is the
buffer position of the start of the class declaration, and the first
element is the buffer position of the enclosing class's opening
brace."
(let ((state (c-parse-state)))
(or (not (c-most-enclosing-brace state))
(c-search-uplist-for-classkey state))))
(defun c-just-after-func-arglist-p (&optional containing)
;; Return t if we are between a function's argument list closing
;; paren and its opening brace. Note that the list close brace
;; could be followed by a "const" specifier or a member init hanging
;; colon. Optional CONTAINING is position of containing s-exp open
;; brace. If not supplied, point is used as search start.
(save-excursion
(c-backward-syntactic-ws)
(let ((checkpoint (or containing (point))))
(goto-char checkpoint)
;; could be looking at const specifier
(if (and (eq (char-before) ?t)
(forward-word -1)
(looking-at "\\<const\\|in\\|body>"))
(c-backward-syntactic-ws)
;; otherwise, we could be looking at a hanging member init
;; colon
(goto-char checkpoint)
(while (eq (char-before) ?,)
;; this will catch member inits with multiple
;; line arglists
(forward-char -1)
(c-backward-syntactic-ws (c-point 'bol))
(if (eq (char-before) ?\))
(c-backward-sexp 2)
(c-backward-sexp 1))
(c-backward-syntactic-ws))
(if (and (eq (char-before) ?:)
(progn
(forward-char -1)
(c-backward-syntactic-ws)
(looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
nil
(goto-char checkpoint))
)
(and (eq (char-before) ?\))
;; check if we are looking at a method def
(or (not c-method-key)
(progn
(c-forward-sexp -1)
(forward-char -1)
(c-backward-syntactic-ws)
(not (or (memq (char-before) '(?- ?+))
;; or a class category
(progn
(c-forward-sexp -2)
(looking-at c-class-key))
)))))
)))
;; defuns to look backwards for things
(defun c-backward-to-start-of-do (&optional lim)
;; Move to the start of the last "unbalanced" do expression.
;; Optional LIM is the farthest back to search. If none is found,
;; nil is returned and point is left unchanged, otherwise t is returned.
(let ((do-level 1)
(case-fold-search nil)
(lim (or lim (c-point 'bod)))
(here (point))
foundp)
(while (not (zerop do-level))
;; we protect this call because trying to execute this when the
;; while is not associated with a do will throw an error
(condition-case nil
(progn
(c-backward-sexp 1)
(cond
;; break infloop for illegal C code
((bobp) (setq do-level 0))
((memq (c-in-literal lim) '(c c++)))
((looking-at "while\\b[^_]")
(setq do-level (1+ do-level)))
((looking-at "do\\b[^_]")
(if (zerop (setq do-level (1- do-level)))
(setq foundp t)))
((<= (point) lim)
(setq do-level 0)
(goto-char lim))))
(error
(goto-char lim)
(setq do-level 0))))
(if (not foundp)
(goto-char here))
foundp))
(defun c-backward-to-start-of-if (&optional lim)
;; Move to the start of the last "unbalanced" if and return t. If
;; none is found, and we are looking at an if clause, nil is
;; returned.
(let ((if-level 1)
(here (c-point 'bol))
(case-fold-search nil)
(lim (or (and lim (>= (point) lim) lim)
(c-point 'bod)))
(at-if (looking-at "if\\b[^_]")))
(catch 'orphan-if
(while (and (not (bobp))
(not (zerop if-level)))
(c-backward-syntactic-ws)
(condition-case nil
(c-backward-sexp 1)
(error
(unless at-if
(goto-char here)
(c-beginning-of-statement-1)
(setq c-parsing-error
(format "No matching `if' found for `else' on line %d"
(1+ (count-lines 1 here))))
(throw 'orphan-if nil))))
(cond
((looking-at "else\\b[^_]")
(setq if-level (1+ if-level)))
((looking-at "if\\b[^_]")
;; check for else if... skip over
(let ((here (point)))
(c-safe (c-forward-sexp -1))
(if (looking-at "\\<else\\>[ \t]+\\<if\\>[^_]")
nil
(setq if-level (1- if-level))
(goto-char here))))
((< (point) lim)
(setq if-level 0)
(goto-char lim))
))
t)))
(defun c-skip-conditional ()
;; skip forward over conditional at point, including any predicate
;; statements in parentheses. No error checking is performed.
(c-forward-sexp (cond
;; else if()
((looking-at "\\<else\\>[ \t]+\\<if\\>\\([^_]\\|$\\)") 3)
;; do, else, try, finally
((looking-at
"\\<\\(do\\|else\\|try\\|finally\\)\\>\\([^_]\\|$\\)")
1)
;; for, if, while, switch, catch, synchronized, foreach
(t 2))))
(defun c-beginning-of-closest-statement (&optional lim)
;; Go back to the closest preceding statement start.
(let ((start (point))
(label-re (concat c-label-key "\\|"