-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphp-structure.el
1305 lines (1238 loc) · 60.8 KB
/
php-structure.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
;;; php-structure.el --- Functions that deal with the structure of a PHP script
;; Version: 1.0
;; Created: 10-03-2011
;; Copyright © 2011 Michael Dwyer
;; Author(s):
;; Michael Dwyer <mdwyer@ehtech.in>
;;; *****
;;; About
;;; *****
;; php-structure.el is a part of the php+-mode suite and contains
;; convenience functions for dealing with PHP structure, such as
;; determining whether point is within a string and where the
;; beginning of the current statment resides.
;; ********************************************************************
;; ************
;; REQUIREMENTS
;; ************
(require 'cl)
(require 'php-parse)
(require 'php-utils)
;; *********
;; VARIABLES
;; *********
(defvar php-text-struct-cache nil
"Cache used by ``php-in-text-structp''.")
;; *********
;; FUNCTIONS
;; *********
(defun syntax-inside-text-structp (&optional pos)
"Uses ``syntax-ppss'' to determine whether point (or optionally
POS) is within a string or comment. Returns the value of (point)
at the beginning of the text structure."
(save-excursion
(elt (syntax-ppss pos) 8)))
(defun php-locate-non-enclosed (reg &optional end skip-sexps)
"This function returns a list of locations and lengths of REG
that are not inside of a PHP string or enclosure. REG may be a
list. Stops at the end of the line or END. If END is an integer,
it will stop at that location. If END is a regexp it will stop
at the first non-string occurrence of END. It can be told to
SKIP-SEXP."
(unless reg (error "Invalid character(s)."))
(save-excursion
(save-match-data
(let ((string-begin (php-in-stringp)))
(when string-begin
(goto-char string-begin)))
(let* ((end (or end (save-excursion (php-skip-this-statement) (point))))
(reg (if (listp reg) reg `(,reg)))
(enclosure-start-re (concat "[" (when skip-sexps "[(") "\"'{]"))
(reg-locs '())
(regexp (concat "\\(" (mapconcat 'identity reg "\\|") "\\)")))
(catch 'done
(while (< (point) (point-max))
(when (looking-at-p enclosure-start-re)
(condition-case nil
(forward-sexp)
(error (throw 'done (reverse reg-locs))))
(backward-char))
(when (if (integerp end) (>= (point) end) (looking-at-p end))
(throw 'done (reverse reg-locs)))
(if (not (looking-at regexp))
(forward-char)
(setf reg-locs (cons `(,(point)
,(length (match-string-no-properties 0)))
reg-locs))
(goto-char (match-end 0)))))))))
(defun php-find-struct-position (type name &optional visibility staticp
abstractp finalp)
"Return the best position to insert a new struct with the
provided description. It first looks for a class at point, then
an interface at point, and then finally for the first class found
in the file, then the first interface"
(save-excursion
(save-match-data
(let* ((parse (php-parse-current '(class interface script file)))
(parse-type (rest (assoc 'type parse))))
(when (eq 'file parse-type)
(setq parse (first (rest (assoc 'scripts parse)))))
(when (eq 'script parse-type)
(setq parse (first (or (rest (assoc 'classes parse))
(rest (assoc 'interfaces parse))))))
(when parse
(let* ((structures (sort (append (rest (assoc 'constants parse))
(rest (assoc 'properties parse))
(rest (assoc 'methods parse)))
'php-parse<))
(new-struct `((begin . 0)
(end . 0)
(text . "")
(name . ,name)
(type . ,type)
(staticp . ,staticp)
(finalp . ,finalp)
(abstractp . ,abstractp)
(visibility . ,visibility)))
(begin (or (rest (assoc 'begin
(catch 'found
(dolist (s structures)
(when (php-parse< new-struct s)
(throw 'found s))))))
(1- (rest (assoc 'end parse))))))
(goto-char begin)
(when (looking-at-p ws-re)
(re-search-forward non-ws-re nil t)
(backward-char))
(point)))))))
(defun php-find-current-sexp-begin/end-get-args (&optional delimiter
matching-delimiter)
"This function gathers arguments for
php-find-current-sexp-{begin,end}."
(let* ((delimiter (if (consp current-prefix-arg)
(read-string "Delimiter ('('): " nil nil
"(") "("))
(default-matching
(condition-case nil (matching-delimiter delimiter)
(error nil)))
(matching-delimiter
(if (consp current-prefix-arg)
(read-string (concat "Matching ('" default-matching
"'): ") nil nil
default-matching)
")")))
`(,delimiter ,matching-delimiter t)))
(defun php-find-current-sexp-begin/end (which &optional delimiter
matching-delimiter move-point)
"This function locates the beginning/ending of the current sexp
starting with DELIMITER. It defaults to parenthesis. A
MATCHING-DELIMITER may also be specified if there is no
reasonable default. If point happens to be on DELIMITER, it is
considered part of its parent sexp. This function can be told to
MOVE-POINT, unless it is not part of an sexp. It will do so if
called interactively."
(let* ((delimiter (or delimiter "("))
(matching-delimiter (or matching-delimiter
(matching-delimiter delimiter)))
(delimiter (if (string= delimiter "[") "\\[" delimiter))
(delimiter-count 0)
(retval
(if (and (member delimiter '("(" "{" "["))
(string= matching-delimiter (matching-delimiter delimiter)))
(let (sexp-begin)
(save-excursion
(catch 'found
(while (setf sexp-begin (elt (syntax-ppss) 1))
(goto-char sexp-begin)
(when (looking-at-p delimiter)
(if (eq which 'begin)
(throw 'found (point))
(forward-sexp)
(throw 'found (point))))))))
(save-excursion
(catch 'found
(while (or (and (eq which 'begin)
(> (point) (point-min)))
(and (eq which 'end)
(< (point) (point-max))))
(cond ((eq which 'begin) (backward-char))
((eq which 'end) (forward-char)))
(if (and (looking-at-p delimiter)
(not (php-in-text-structp)))
(setq delimiter-count (1+ delimiter-count))
(if (and (looking-at-p matching-delimiter)
(not (php-in-text-structp)))
(setq delimiter-count (1- delimiter-count))))
(when (or (and (eq which 'begin) (= delimiter-count 1))
(and (eq which 'end) (= delimiter-count -1)))
(throw 'found (point)))))))))
(when (and move-point (integerp retval)) (goto-char retval)) retval))
(defun php-find-current-sexp-begin (&optional delimiter matching-delimiter
move-point)
(interactive (php-find-current-sexp-begin/end-get-args))
(php-find-current-sexp-begin/end 'begin delimiter matching-delimiter
move-point))
(defun php-find-current-sexp-end (&optional delimiter matching-delimiter
move-point)
(interactive (php-find-current-sexp-begin/end-get-args))
(php-find-current-sexp-begin/end 'end delimiter matching-delimiter
move-point))
(defun php-sexp-end-position (&optional pos)
"Return the position of the end of sexp started by the
character at point or POS."
(save-excursion
(when pos (goto-char pos))
(when parenthesis-stack
(goto-char (first parenthesis-stack))
(forward-sexp)
(1- (point)))))
(defun php-get-current-sexp-level (&optional delimiter matching-delimiter)
"Return how many sexps deep point is."
(save-excursion
(let ((count 0))
(catch 'done
(while (> (point) (point-min))
(if (php-find-current-sexp-begin delimiter matching-delimiter t)
(setf count (1+ count))
(throw 'done count))))
count)))
(defun php-goto-start-of-script/html ()
"Move point to the beginning of the current script or HTML
block."
(interactive)
(let ((begin (php-in-scriptp)))
(if (integerp begin)
(goto-char begin)
(let ((begin (php-in-bare-htmlp)))
(when (integerp begin)
(goto-char begin))))))
(defun php-goto-end-of-script/html ()
"Move point to the beginning of the current script or HTML
block."
(interactive)
(or (php-skip-this-script)
(php-skip-this-bare-html)))
(defun php-in-scriptp ()
"If point is within a PHP script block, return (point) at the
beginning of the block."
(let ((parse (php-parse-current 'script)))
(when (php-parse-p parse)
(rest (assoc 'begin parse)))))
(defun php-skip-this-script ()
"If in a PHP script block, send point to the end of it."
(let ((script (php-parse-current 'script)))
(when script (goto-char (1+ (rest (assoc 'end script)))))))
(defun php-in-bare-htmlp ()
"If point is not within a PHP script block, return (point) at
the beginning of the non-script section."
(unless (php-in-scriptp)
(let ((scripts (rest (assoc 'scripts (php-parse-current 'file))))
(script-end (point-min)))
(catch 'too-far
(dolist (script scripts)
(let ((cur-end (rest (assoc 'end script))))
(if (< cur-end (point))
(setf script-end cur-end)
(throw 'too-far t)))))
(1+ script-end))))
(defun php-skip-this-bare-html ()
"If in bare HTML, send point to the end of it."
(unless (php-in-scriptp)
(let ((scripts (rest (assoc 'scripts (php-parse-current 'file))))
(script-begin (point-max)))
(catch 'found-it
(dolist (script scripts)
(let ((cur-begin (rest (assoc 'begin script))))
(when (>= cur-begin (point))
(setf script-begin cur-begin)
(throw 'found-it t)))))
(goto-char script-begin))))
(defun php-get-text-type (&optional pos)
"Return the type of the current text struct (optionally at POS)
or nil if not in one."
(save-excursion
(when (and (wholenump pos)
(>= pos (point-min))
(<= pos (point-max)))
(goto-char pos))
(third (first (member-if (lambda (x)
(and (>= (point) (first x))
(<= (point) (second x))))
php-text-struct-cache)))))
(defun php-next-text-struct (&optional bound not-in-string)
"Goto the next PHP text struct. Optionally stop at BOUND. If
``php-text-struct-cache'' is invalid and we are NOT-IN-STRING
tell us so that we may avoid infinite recursion. Returns the
type."
(save-match-data
(let* ((bound (if (integerp bound) bound (point-max)))
(start-point (point))
type
(type (catch 'done
(unless not-in-string
(when (php-in-text-structp)
(php-skip-this-text-struct)))
(while (<= (point) bound)
(if (not (re-search-forward
"\\?>\\|<<<\\|//\\|/\\*\\|[#'\"]" nil t))
(throw 'done nil)
(goto-char (match-beginning 0))
(if (> (point) bound)
(throw 'done nil)
(let ((match (match-string-no-properties 0)))
(setf type (cond ((string= match "?>") 'bare-html)
((or (string= match "<<<")
(string= match "'")
(string= match "\""))
'string)
((or (string= match "//")
(string= match "/*")
(string= match "#"))
'comment)))
(if type
(throw 'done type)
(goto-char (match-end 0))))))))))
(unless type
(goto-char start-point))
type)))
(defun php-skip-this-text-struct (&optional bound at-beginning)
"If at the beginning of a text structure (string/comment), skip
to the end and return (point). Optionally stop at BOUND. If you
know that ``php-text-struct-cache'' is invalid, make sure you are
AT-BEGINNING and tell us so that we avoid infinite recursion."
(save-match-data
(let* ((text-begin (if at-beginning (point) (php-in-text-structp)))
(text-type (if at-beginning
(cond ((looking-at-p "[\"']\\|<<<") 'string)
((looking-at-p "\\?>") 'bare-html)
(t 'comment))
(php-get-text-type))))
(when (integerp text-begin) (goto-char text-begin))
(let ((bound (if (integerp bound) bound (point-max))))
(when (cond ((or (= (point) (point-min))
(looking-at-p "\\?>")
(and (looking-at-p ">")
(looking-back-p "\\?"))
(looking-back-p "\\?>"))
(or (when (re-search-forward "<\\?\\(php\\|=\\)?" nil t)
(goto-char (match-beginning 0)))
(goto-char (point-max))))
((looking-at-p "/\\*")
(or (re-search-forward "\\*/" nil t) (goto-char bound)))
((or (looking-at-p "//") (looking-at-p "#"))
(forward-char)
(or (when (re-search-forward "\\?>\\|^" nil t)
(goto-char (match-beginning 0)))
(goto-char bound)))
((looking-at "[\"']")
(let* ((quote-string (match-string-no-properties 0))
(search-re (concat "[" quote-string "{]")))
(forward-char)
(or (catch 'done
(while (re-search-forward search-re bound t)
(if (string= (match-string-no-properties 0) "{")
(when (looking-at-p
(concat "$"
(substring
(php-type-regexp
'identifier)
2) "}"))
(re-search-forward "}" bound t))
(unless (and (char-equal
(char-before (1- (point))) ?\\)
(not (char-equal
(char-before (- (point) 2))
?\\)))
(throw 'done (point))))))
(goto-char bound))))
((looking-at-p "<<<")
(forward-char 3)
(or (when (looking-at-p non-ws-re)
(let ((this-quote nil))
(when (looking-at-p "['\"]")
(setq this-quote (char-after))
(forward-char))
(let ((doc-id-start (point)))
(re-search-forward "$" nil t)
(when (and (characterp this-quote)
(= this-quote (char-before)))
(backward-char))
(when (re-search-forward
(concat (buffer-substring-no-properties
doc-id-start (point)) "[\n;]")
nil t)
(when (char-equal (char-before) ?\;)
(backward-char))
t))))
(goto-char bound))))
(goto-char (min bound (point))))))))
(defun php-text-struct-cache-change-function (begin end old-length)
"This hook updates ``php-text-struct-cache'' whenever the
buffer is changed."
(put 'php-text-struct-cache 'valid nil)
(let* ((change (- (- end begin) old-length))
found-begin
(affected (member-if (lambda (x)
(let* ((x-type (third x))
(x-beg
(- (first x)
(if (eq x-type 'bare-html) 2 0)))
(x-end
(+ (second x)
(if (eq x-type 'bare-html) 2 0))))
(or (> x-beg end)
(integer-range-intersectp
`(,x-beg ,x-end) `(,begin ,end)))))
php-text-struct-cache))
(unaffected (butlast php-text-struct-cache (length affected))))
(if affected
(let ((affected-start (if affected
(save-excursion
(save-match-data
(let ((text-struct-begin (caar affected)))
(goto-char (min begin text-struct-begin))
(if (or (and (looking-back-p "\\?")
(looking-at-p ">"))
(and (eq 'bare-html
(third (car affected)))
(= (point)
text-struct-begin)))
(progn
(backward-char (min (- (point)
(point-min))
2))
(point))
(-compensate-for-new-quotes)))))
begin)))
(when (integerp affected-start)
(let* ((first-affected (first affected))
(affected (if (equal (mapcar (lambda (x) (inc-ints x change))
first-affected)
(first (php-scan-for-text-structs
(+ (second first-affected) change)
affected-start)))
(mapcar (lambda (x)
(mapcar (lambda (y)
(inc-ints y change)) x))
affected)
(php-scan-for-text-structs nil affected-start))))
(setf php-text-struct-cache (nconc unaffected affected)))))
(setf php-text-struct-cache
(nconc unaffected (php-scan-for-text-structs end begin)))))
(put 'php-text-struct-cache 'valid t))
(defun -compensate-for-new-quotes ()
(save-match-data
(if (or (and (looking-at "/")
(or (looking-back "/")
(looking-at "//")
(looking-at "/\\*")))
(and (looking-at "\\*")
(looking-back "/"))
(and (looking-at "<")
(or (looking-at "<<<")
(and (looking-back "<")
(looking-at "<<"))
(looking-back "<<"))))
(match-beginning 0)
(point))))
(defun php-text-struct-cache-initialize ()
"Initializes ``php-text-struct-cache''."
(set (make-local-variable 'php-text-struct-cache) (php-scan-for-text-structs))
(put 'php-text-struct-cache 'valid t)
(add-hook 'after-change-functions
'php-text-struct-cache-change-function nil t))
(defun php-text-struct-cache-deactivate ()
(remove-hook 'after-change-functions 'php-text-struct-cache-change-function
t))
(defun php-text-struct-cache-clear ()
"Clears ``php-text-struct-cache''."
(setf php-text-struct-cache nil))
(defun php-text-struct-cache-insert (string-stats &optional at-end)
"Inserts a STRING-STATS '(start end type) into the
``php-text-struct-cache'' in the appropriate place. Returns the
position that STRING-STATS was inserted at. If you know you will
be adding to the end of the cache, apecifying AT-END will improve
performance."
(let* ((cache-len (length php-text-struct-cache)))
(if (not at-end)
(let* ((pos (catch 'new-pos
(dotimes (i cache-len)
(let* ((cur-string-stats (elt php-text-struct-cache i))
(start (first cur-string-stats))
(end (second cur-string-stats)))
(when (or (and (>= (first string-stats) start)
(<= (first string-stats) end))
(and (>= (second string-stats) start)
(<= (second string-stats) end)))
(error (concat "String-Stats %s intersects already "
"existing string-stats %s")
string-stats cur-string-stats)
(when (> start (first string-stats))
(throw 'new-pos i)))))))
(pos (or pos cache-len)))
(setf php-text-struct-cache (append (butlast php-text-struct-cache
(- cache-len pos))
`(,string-stats)
(nthcdr pos
php-text-struct-cache)))
pos)
(setf php-text-struct-cache
(append php-text-struct-cache `(,string-stats)))
cache-len)))
(defun php-text-struct-cachep (pos)
"Check to see whether POS is in a string in
``php-text-struct-map''. Returns the bounds of the string."
(first (member-if (lambda (string-stats)
(and (>= pos (first string-stats))
(<= pos (second string-stats))))
php-text-struct-cache)))
(defun php-in-text-structp (&optional type pos end)
"TYPE may be a list of types or nil. If nil, all types
'(string comments) will be tried. The beginning and any end
delimiters will be considered part of the struct. Returns
point at the beginning of the struct or nil. If POS is an
integer, start there. If END is provided, return true if any
character from POS to END qualifies. This function relies on
``php-text-struct-cache''!"
(unless (get 'php-text-struct-cache 'valid)
(php-text-struct-cache-initialize))
(let* ((pos (if (integerp pos) pos (point)))
(end (if (and (integerp end) (>= end pos)) end pos))
(type (cond ((consp type) type)
((not type) '(string comment bare-html))
(t `(,type)))))
(catch 'done
(dotimes (i (- (1+ end) pos))
(let ((match (php-text-struct-cachep (+ pos i))))
(when (and match (member (third match) type))
(throw 'done (first match))))))))
(defun php-scan-for-text-structs (&optional bound start)
"Scan the buffer for text structs, optionally up to BOUND,
returning a structure like ``php-text-struct-cache''. If you
want parsing to begin at START rather than (point-min), you may
supply it."
(save-excursion
(save-restriction
(widen)
(let (php-text-struct-cache
(bound (if (wholenump bound) bound (point-max)))
(start (if (wholenump start) start (point-min))))
(goto-char start)
(let* ((in-script (save-excursion
(save-match-data
(when (looking-at (concat non-ws-re "+"))
(goto-char (match-end 0)))
(when (re-search-backward "<\\?" nil t)
(match-beginning 0)))))
(next-script (unless in-script
(re-search-forward "<\\?" nil t))))
(unless in-script
(let ((struct-end (min bound
(if next-script
(1- next-script) (point-max)))))
(setf php-text-struct-cache `((,start ,struct-end bare-html)))
(goto-char struct-end)))
(catch 'done
(let ((last-point -1)
in-struct)
(while (<= (point) bound)
(when (<= (point) last-point)
(error (concat "Infinite loop detected in "
"php-scan-for-text-structs.")))
(setf last-point (point))
(setq in-struct nil)
(let ((this-type
(cond ((looking-at-p "//\\|/\\*\\|#") 'comment)
((looking-at-p (concat "['\"]\\|"
doc-begin-tag-re))
'string)
((looking-at-p "\\?>") 'bare-html)
(t (php-next-text-struct bound t)))))
(if (not this-type)
(throw 'done nil)
(setq in-struct
(when (<= (point) bound)
(if (eq this-type 'bare-html)
(+ (point) 2)
(point))))
(let* ((string-end (php-skip-this-text-struct nil t))
(string-end (if (= (point-max) string-end)
string-end
(1- string-end))))
(setf php-text-struct-cache
(nconc php-text-struct-cache
`((,in-struct ,string-end ,this-type)))))
(when (or (= (point) (point-max))
(> (point) bound))
(throw 'done in-struct))))))))
php-text-struct-cache))))
(defun php-inside-text-structp (&optional type pos end)
"Return t if point is inside a text struct (optionally of TYPE
at POS), not on the delimiters (quotes, etc). If END is
provided, return true if any character from POS to END
qualifies."
(let* ((pos (if (integerp pos) pos (point)))
(begin (php-in-text-structp type pos end)))
(when begin
(let* ((inside-begin (+ begin
(save-excursion
(goto-char begin)
(save-match-data
(cond ((looking-at doc-begin-tag-re)
(- (match-end 0) (match-beginning 0)))
((looking-at-p "\\?>") 2)
(t 1))))))
(inside-end (save-excursion
(- (php-skip-this-text-struct)
(save-match-data
(cond ((or (looking-back
(substring doc-end-tag-re 0 -8))
(looking-back "<\\?\\(php\\|=\\)?"))
(1+ (- (match-end 0)
(match-beginning 0))))
(t 2)))))))
(and (>= pos inside-begin)
(<= pos inside-end))))))
(defun php-in-stringp (&optional pos end)
"The beginning and end quotes and {here,now}doc tags will be
considered part of the string. Returns point at the beginning
of the string or nil. If END is provided, return true if any
character from POS to END qualifies."
(php-in-text-structp 'string pos end))
(defun php-in-commentp (&optional pos end)
"The beginning and end delimiters will be considered part of
the comment. Returns point at the beginning of the comment or
nil. If END is provided, return true if any character from POS
to END qualifies."
(php-in-text-structp 'comment pos end))
(defconst php-possible-num-chars "-+.0-9xa-fA-F")
(defun php-in-numberp (&optional pos)
"Returns point at the beginning of the number or nil."
(save-excursion
(save-match-data
(when (integerp pos)
(goto-char pos))
(when (not (php-in-text-structp))
(when (looking-at-p (concat "[" php-possible-num-chars "]"))
(re-search-backward (concat "[^" php-possible-num-chars "]") nil t))
(when (looking-at-p ws-re)
(forward-char)
(when (looking-at-p (php-type-regexp 'number))
(point)))))))
(defconst php-definite-identifier-chars "][_a-zA-Z0-9-ÿ")
(defconst php-possible-identifier-chars "$<?:->!\\+[:space:]\n()\\.")
(defun php-in-identifierp (&optional pos)
"Returns point at the beginning of the identifier or nil."
(save-excursion
(when (integerp pos)
(goto-char pos))
(let* ((start (point))
(accessor-re (php-type-regexp 'accessor))
(begin (catch 'done
(when (looking-at-p "<\\?\\(php\\|=\\)?")
(throw 'done (point)))
(while (> (point) (point-min))
(let ((in-string (php-in-stringp)))
(when in-string (goto-char (1- in-string)))
(if (looking-at-p (concat "["
php-definite-identifier-chars
"]"))
(backward-char)
(if (looking-at-p
(concat "[^" php-possible-identifier-chars "]"))
(throw 'done (point))
(cond ((looking-at-p "!")
(throw 'done (point)))
((looking-at-p "\\$")
(if (looking-back-p (concat "\\(->\\|::\\)"
ws-re "*"))
(backward-char)
(throw 'done (point))))
((looking-at "\\.")
(if (php-in-numberp)
(backward-char)
(throw 'done (1+ (point)))))
((looking-at-p "\\+")
(if (looking-back-p "\\+")
(throw 'done (1- (point)))
(if (looking-back-p
(concat "["
php-definite-identifier-chars
"]"))
(throw 'done (1+ (point)))
(throw 'done (point)))))
((looking-at-p ":")
(if (looking-at-p "::")
(backward-char)
(if (looking-back-p ":")
(backward-char 2)
(throw 'done (1+ (point))))))
((looking-at-p "-")
(if (looking-at-p "->")
(backward-char)
(if (looking-back-p "-")
(throw 'done (1- (point)))
(if (looking-back-p
php-definite-identifier-chars)
(throw 'done (1+ (point)))
(throw 'done (point))))))
((looking-at-p "<\\?\\(php\\|=\\)?")
(throw 'done (point)))
((looking-at-p "\\?")
(if (looking-back-p "<")
(throw 'done (1- (point)))
(backward-char)))
((looking-at-p ">")
(if (or (looking-back-p "-")
(looking-back-p "?"))
(backward-char 2)
(throw 'done (1+ (point)))))
((looking-at-p ws-re)
(let ((pos (point)))
(save-match-data
(re-search-backward non-ws-re nil t))
(forward-char)
(if (and
(or
(not
(looking-at-p
(concat ws-re "*" accessor-re)))
(save-excursion
(backward-char)
(looking-at-p
(concat
"[^"
php-definite-identifier-chars
(substring
php-possible-identifier-chars 9)
"]"))))
(not (or (looking-back-p "->")
(looking-back-p "::")
(looking-back-p "new"))))
(throw 'done (1+ pos))
(backward-char))))
((looking-at-p "(")
(backward-char))
((looking-at-p ")")
(forward-char)
(backward-sexp)
(backward-char))
(t (throw 'done (1+ (point))))))))))))
(when (and (integerp begin) (<= begin start))
begin))))
(defun php-skip-this-identifier (&optional bound ignore-in-string)
"If at the beginning of a identifier, skip to the end.
Optionally stop at BOUND or IGNORE-IN-STRING. Returns (point) at
the end."
(save-match-data
(let ((start (point)))
(if (and (not ignore-in-string) (php-in-text-structp))
(php-skip-this-text-struct)
(when (looking-at
(concat "\\(\\(\\+\\+?\\|--?\\|!\\)[(a-zA-Z_-ÿ$]\\)\\|\\$\\|)"
"\\|<?\\?>?"))
(goto-char (match-end 0)))
(let ((re (concat "\\(?99:[^]a-zA-Z0-9_-ÿ]\\)\\|"
"\\(?99:" doc-begin-tag-re "\\)\\|"
"\\(?99:-[^>]\\)\\|\\([^:]\\(?99::\\)[^:]\\)\\|"
"\\(?:[^>:[:space:]\n]\\(?99:" ws-re
"+[^-[:space:]\n:]\\)\\)\\|"
"\\(?:[^>:[:space:]\n]\\(?99:" ws-re
"+-[^>]\\)\\)\\|"
"\\(?:[^>:[:space:]\n]\\(?99:" ws-re
"+:[^:]\\)\\)\\|\\(?99:[^-]>" ws-re "+\\)\\|"
"\\(?99:[^:]:" ws-re "+\\)\\|"
"\\(?99:" ws-re "+[^-[:space:]\n:]\\)\\|"
"\\(?99:" ws-re "+-[^>]\\)\\|"
"\\(?99:" ws-re "+:[^:]\\)"))
(current-sexp (php-get-current-sexp-level)))
(catch 'done
(while (re-search-forward re nil t)
(goto-char (match-beginning 99))
(when (looking-at-p ws-re)
(save-match-data
(re-search-backward non-ws-re nil t)
(forward-char)))
(save-match-data
(when (and (looking-back-p "function")
(looking-at (concat ws-re "*\\("
(php-type-regexp 'identifier)
"\\)?([^)]*)" ws-re "*{")))
(goto-char (1- (match-end 0)))
(setf start (point))))
(if (and (looking-at-p ")")
(> (php-get-current-sexp-level) current-sexp))
(forward-char)
(catch 'parenthesis
(while (looking-at-p "[[({]")
(when (and (looking-at-p "{")
(not (= (point) start)))
(throw 'parenthesis t))
(condition-case nil
(forward-sexp)
(error (throw 'done (1+ (point)))))
(when (looking-back-p ")")
(throw 'parenthesis t)))))
(if (looking-at (concat ws-re "*" "\\(->\\|::\\)"))
(goto-char (match-end 0))
(throw 'done (when (> (point) start) (point)))))))))))
(defun php-in-variablep (&optional stand-alone)
"When inside a variable, return point at the beginning of the
variable. Optionally, the variable must STAND-ALONE, and not be
part of any function calls."
(save-excursion
(let ((begin (php-in-identifierp)))
(when (integerp begin)
(goto-char begin)
(let ((end (save-excursion (php-skip-this-identifier))))
(when (looking-at-p "\\$")
(if (not stand-alone)
begin
(catch 'found-end
(while (<= (point) end)
(forward-char)
(when (looking-at-p ws-re)
(throw 'found-end begin))
(unless (looking-at-p (php-type-regexp 'identifier))
(throw 'found-end nil)))))))))))
(defun php-in-method-call ()
"When in a method call, return t."
(and (> (php-get-current-sexp-level) 0)
(not (php-function-definitionp))
(not (php-in-control-statementp))))
(defun php-anonymous-function-definitionp ()
"When in an anonymous function definition return point at the
beginning of the definition."
(php-function-definitionp t))
(defun php-named-function-definitionp ()
"When in a named function definition return point at the
beginning of the definition."
(and (not (php-anonymous-function-definitionp))
(php-function-definitionp)))
(defun php-class/interface-definitionp ()
"When inside a class/interface definition return point at the
beginning of the definition."
(let ((statement-begin (php-in-statementp)))
(when (integerp statement-begin)
(save-excursion
(goto-char statement-begin)
(looking-at-p (concat "\\(\\(final\\|abstract\\)" ws-re
"\\)*class\\|interface"))))))
(defun php-function-definitionp (&optional anonymousp)
"When inside a function definition return point at the
beginning of the definition. Optionally, return nil unless the
definition is ANONYMOUSP."
(save-excursion
(save-match-data
(let ((start (point)))
(end-of-line)
(when (looking-back "{[[:space:]]*")
(goto-char (1- (match-beginning 0))))
(let ((too-far (or (save-excursion
(catch 'found
(beginning-of-line)
(while (re-search-backward "[{;]\\|\\*/" nil t)
(when (not (php-in-text-structp))
(throw 'found (point))))))
(point-min))))
(when (wholenump too-far)
(let ((def-begin
(catch 'found
(while (re-search-backward-greedy
(concat "\\(\\(public\\|private\\|protected"
"\\|static\\|abstract\\)" ws-re
"*\\)*function\\(" ws-re "*("
(unless anonymousp
(concat "\\|" ws-re "+"
(substring
(php-type-regexp 'identifier)
2) ws-re "*("))
"\\)")
too-far t)
(unless (php-in-text-structp)
(throw 'found (point)))))))
(when (and (wholenump def-begin)
(<= def-begin start))
def-begin))))))))
(defun php-in-statementp (&optional pos)
"Returns point at the beginning of the statement or nil."
(save-excursion
(save-match-data
(when (integerp pos)
(goto-char pos))
(let* ((starting-point (point))
(starting-block-level (php-get-current-sexp-level "{"))
(in-comment (php-in-commentp))
(in-start-tag (save-excursion
(cond ((looking-at-p non-ws-re)
(re-search-forward ws-re nil t)
(backward-char))
(t (re-search-backward non-ws-re nil t)
(forward-char)))
(when (looking-back "<\\?\\(php\\|=\\)?")
(match-beginning 0))))
(in-bare-html (unless (or in-comment in-start-tag)
(let ((start (php-in-bare-htmlp)))
(cond ((integerp start)
(- start 2))
((and (> (point) (point-min))
(char-equal (char-before) ??)
(char-equal (char-after) ?>))
(1- (point)))
((looking-at-p "\\?>") (point)))))))
(cond ((integerp in-start-tag) in-start-tag)
((integerp in-comment) in-comment)
((integerp in-bare-html) in-bare-html)
(t (catch 'found
(unless (or (= (point) (point-max))
(looking-at-p "}"))
(while (re-search-backward
"[:;{}#]\\|\\*/\\|//\\|<\\?\\(php\\|=\\)?" nil t)
(unless (php-inside-text-structp)
(let ((match (match-string-no-properties 0)))
(when (and (string= match "<?")
(looking-at "<\\?\\(php\\|=\\)?"))
(setf match (match-string-no-properties 0)))
(when (cond ((string= match "*/")
(goto-char (+ 2 (point))))
((or (string= match "//")
(string= match "#"))
(forward-line))
((or (string= match "<?php")
(string= match "<?=")
(string= match "<?"))
(goto-char (+ (length match) (point))))
((not (php-in-text-structp))
(goto-char (1+ (point)))))
(let
((skip
(or
(when (looking-back-p ";")
(php-in-control-statementp "for" t))
(when (or (looking-back-p "}")
(looking-back-p "{"))
(save-excursion
(cond ((looking-back-p "}")
(backward-sexp))
((looking-back-p "{")
(backward-char)))
(when
(> (save-excursion
(forward-char)
(php-get-current-sexp-level "{"))
starting-block-level)
(php-anonymous-function-definitionp))))
(when (looking-back-p ":")
(cond ((looking-back-p "::")
(- (point) 2))
((or (and (looking-back-p ":")
(looking-at-p ":"))
(save-excursion
(beginning-of-line-non-whitespace)
(not
(or (looking-at-p
"case ")
(looking-at-p
"default:")))))
(1- (point))))))))
(if skip
(goto-char skip)
(if (not (re-search-forward non-ws-re nil t))
(throw 'found nil)
(backward-char)
(while (php-in-commentp)
(php-skip-this-text-struct)
(re-search-forward non-ws-re nil t)
(backward-char))
(if (< starting-point (point))
(throw 'found nil)
(throw 'found (point))))))))))))))))))