-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphp-format.el
1552 lines (1490 loc) · 72.8 KB
/
php-format.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-format.el --- Functions that deal with PHP formatting
;; Version: 2.0
;; Created: 07-29-2011
;; Copyright © 2011 Michael Dwyer
;; Author(s):
;; Michael Dwyer <[email protected]>
;;; *****
;;; About
;;; *****
;; php-format.el is a part of the php+-mode suite and contains
;; convenience functions for PHP formatting, such as re-ordering the
;; constants, properties and methods withing a class and line
;; breaking.
;; ********************************************************************
;; ************
;; REQUIREMENTS
;; ************
(require 'php-parse)
(require 'php-structure)
;; *********
;; CUSTOMIZE
;; *********
(defcustom php-blank-line-at-start-of-class nil
"Whether you like to have a blank line between the opening
brace of a class and the next line of code. Also applies to
interfaces."
:group 'php-format
:type 'boolean)
(defcustom php-blank-line-at-end-of-class nil
"Whether you like to have a blank line between the closing
brace of a class and the previous line of code. Also applies to
interfaces."
:group 'php-format
:type 'boolean)
(defcustom php-auto-fill nil
"Whether to make use of the php-auto-fill functions."
:group 'php-format
:type 'boolean)
(defcustom php-format-align-array-double-arrows nil
"Whether to align arrays at the double-arrows."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-anonymous-function-always t
"Whether you like to have statements in anonymous functions
broken even if the entire definition and implementation fit on
the current line."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-all-method-chain-links nil
"Whether you like to have method chains broken at each link (t) or only to
fit 80 character line length (nil)."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-string-before-white-space nil
"Whether to break a string before or after white-space."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-all-method-call-arguments nil
"Whether to break all method call arguments even if they fit on
one line."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-function-call-like-definition t
"Whether to break function definition parameters one per line
just like function calls. This was added to abide by PSR-2."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-all-for-semicolons nil
"Whether to break on subsequent semicolons of a for statement
always after breaking on the first semicolon, even if the rest of
the expression fits on the same line."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-all-ternary-parts nil
"Whether to break on the colon of a ternary always after
breaking on the question mark, even if the rest of the expression
fits on the same line."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-all-operators nil
"Whether to break at all operators (boolean, logical and
arithmetic) even if they fit on one line."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-all-assignment-operators nil
"Whether to break at all assignment operators even if they fit
on one line."
:group 'php-format
:type 'boolean)
(defcustom php-format-break-start-line-with-concat t
"Whether to start a new line with the concatenate operator when
breaking strings."
:group 'php-format
:type 'boolean)
(defcustom php-force-single-quoted-strings nil
"Whether to auto-convert strings to single-quoted when running
script cleanup functions."
:group 'php-format
:type 'boolean)
;; *********
;; FUNCTIONS
;; *********
; declarations for compiler
(declare-function php-jump-to-first-statement "php-funcs")
(declare-function php-jump-to-first-class/interface "php-funcs")
(declare-function php-kill-current "php-edit")
(declare-function php-yank "php-edit")
(declare-function php-format-spacing "php-format")
(declare-function php-change-string-quotes "php-string")
(declare-function php-implode-concats-in-statement "php-string")
(declare-function php+-mode "php+-mode")
(declare-function hs-already-hidden-p "hideshow")
(declare-function hs-hide-block "hideshow")
(defun php-rearrange-current (&optional type sit-for)
"Kills the current TYPE PHP structure and yanks it into its
proper position. TYPE follows the semantics of
php-parse-current. Optionally, you may tell it how long to
SIT-FOR at each statement. If SIT-FOR is nil, will not sit at
all."
(interactive)
(let* ((type (or type '(constant property method)))
(parse (php-parse-current type))
current-doc-hs-status current-innard-hs-status)
(when (php-parse-p parse)
(save-excursion
(goto-char (rest (assoc 'begin parse)))
(when (and hs-minor-mode (hs-already-hidden-p))
(setf current-doc-hs-status t))
(php-jump-to-first-statement nil t)
(setf current-innard-hs-status
(and hs-minor-mode (hs-already-hidden-p))))
(php-kill-current type)
(let* ((type (rest (assoc 'type parse)))
(name (rest (assoc 'name parse)))
(visibility (rest (assoc 'visibility parse)))
(staticp (rest (assoc 'staticp parse)))
(abstractp (rest (assoc 'abstractp parse)))
(finalp (rest (assoc 'finalp parse)))
(pos (php-find-struct-position type name visibility staticp
abstractp finalp)))
(goto-char pos)
(when (looking-at-p "}")
(re-search-backward non-ws-re nil t)
(forward-char)
(delete-region (point) pos)
(newline 3)
(forward-line -1)
(indent-according-to-mode))
(php-yank)
(when (integerp sit-for)
(sit-for sit-for))
(save-excursion
(goto-char pos)
(when (and hs-minor-mode current-doc-hs-status) (hs-hide-block))
(php-jump-to-first-statement nil t)
(when (and hs-minor-mode current-innard-hs-status)
(hs-hide-block)))))))
(defun php-update-innards-bounds (innards start delta updatep decision-point)
"Auxillary function to update innards bounds."
(dotimes (j (- (length innards) start) innards)
(let* ((j* (+ start j))
(j-innard (elt innards j*))
(j-begin (rest (assoc 'begin j-innard)))
(j-end (rest (assoc 'end j-innard))))
(when (funcall updatep j-innard decision-point)
(setf (rest (assoc 'begin (elt innards j*)))
(+ j-begin delta)
(rest (assoc 'end (elt innards j*)))
(+ j-end delta))))))
(defun php-innard-beforep (innard decision-point)
"Auxillary function to decide whether an innard is before a certain point."
(<= (rest (assoc 'end innard)) decision-point))
(defun php-innard-afterp (innard decision-point)
"Auxillary function to decide whether an innard is after a certain point."
(>= (rest (assoc 'begin innard)) decision-point))
(defun php-rearrange-innards (&optional no-fix-spacing sit-for)
"Rearrange all of the constants. properties and methods in the
current class or interface. If NO-FIX-SPACING is non-nil, don't
run ``php-format-spacing'' afterwards. Optionally, you may tell
it how long to SIT-FOR at each change. If SIT-FOR is nil, will
not sit at all."
(interactive)
(save-excursion
(let ((top-struct (php-parse-current '(class interface))))
(when (php-parse-p top-struct)
(let* ((constants (rest (assoc 'constants top-struct)))
(properties (rest (assoc 'properties top-struct)))
(methods (rest (assoc 'methods top-struct)))
(innards (sort (append constants properties methods)
'php-parse<)))
(php-jump-to-first-statement top-struct)
(let ((next-insert (point)))
(dotimes (i (1- (length innards)))
(let* ((current-innard (elt innards i))
(current-type (rest (assoc 'type current-innard)))
current-doc-hs-status current-innard-hs-status)
(goto-char (rest (assoc 'begin current-innard)))
(when (looking-at-p ws-re)
(re-search-forward non-ws-re nil t))
(when (and hs-minor-mode (hs-already-hidden-p))
(setf current-doc-hs-status t))
(save-excursion
(php-jump-to-first-statement nil t)
(setf current-innard-hs-status
(and hs-minor-mode (hs-already-hidden-p))))
(let* ((old-innard (php-kill-current current-type t))
(old-begin (rest (assoc 'begin old-innard)))
(old-end (rest (assoc 'end old-innard)))
(deleted-length (- old-end old-begin))
(innards (php-update-innards-bounds innards
(1+ i)
(- deleted-length)
'php-innard-afterp
(point))))
(goto-char next-insert)
(setf (rest (assoc 'begin (elt innards i))) (point))
(php-yank nil t t)
(when (integerp sit-for)
(sit-for sit-for))
(save-excursion
(let ((innard (elt innards i)))
(goto-char (rest (assoc 'begin innard)))
(when (and hs-minor-mode current-doc-hs-status)
(hs-hide-block))
(when (and hs-minor-mode current-innard-hs-status)
(php-jump-to-first-statement innard t)
(hs-hide-block))))
(when (not (looking-back-p (concat "^" ws-re "*")))
(newline)
(newline-and-indent))
(let ((inserted-length (- (point) next-insert)))
(setf (rest (assoc 'end (elt innards i))) (point))
(setq innards
(php-update-innards-bounds innards
(1+ i)
inserted-length
'php-innard-afterp
next-insert))
(setq next-insert (point)))))))))))
(unless no-fix-spacing
(php-format-spacing)))
(defun php-format-break-statement (&optional strip-newlines-in-string
max-length begin end verbose)
"This function breaks the current PHP statement. Lines will be
kept below MAX-LENGTH. Optionally, start at BEGIN and end at
END. You may also STRIP-NEWLINES-IN-STRINGs with a prefix arg.
You may also tell it to be VERBOSE by using the parameter or
setting php-verbose to true."
(interactive "P")
(let ((start-pos (point)))
(when (looking-at-p (concat ws-re "*$"))
(re-search-backward non-ws-re nil t))
(when (and (looking-back-p (concat "^" ws-re "*"))
(re-search-forward non-ws-re nil t))
(backward-char))
(let ((in-comment (php-in-commentp)))
(if (integerp in-comment)
(let* ((comment-end (save-excursion (php-skip-this-text-struct)))
(old-end comment-end))
(goto-char in-comment)
(while (< (point) comment-end)
(setf comment-end (+ comment-end
(indent-according-to-mode)))
(forward-line))
(goto-char start-pos)
(- comment-end old-end))
(let ((verbose (or (and (boundp 'php-verbose) php-verbose) verbose))
(begin (if (integerp begin) begin (php-in-statementp))))
(when begin
(goto-char begin)
(unless (looking-back-p (concat "^" ws-re "*"))
(indent-according-to-mode))
(let ((change
(if (or (looking-at-p "<\\?\\(php\\|=\\)?")
(looking-at-p "\\?>"))
0
(let* ((max-length (or max-length 80))
(end (or end (save-excursion
(php-skip-this-statement))))
(old-end end)
(indent-gap (indent-according-to-mode))
(begin (+ begin indent-gap))
(begin-column (current-column))
(end (+ end indent-gap))
(bounds
(php-format-strip-newlines
`((,end 1)) strip-newlines-in-string))
(broke-once nil)
(bound (first (first bounds)))
(bound (- bound (php-implode-concats-in-statement)))
(in-control (php-in-control-statementp))
(in-function-def (php-function-definitionp))
(ternary-regexp
"\\(?1:\\?\\)[^:]\\|[^?:]\\(?1::\\)[^:]")
(accessor-regexp (php-type-regexp 'accessor))
(operator-regexp (php-type-regexp 'operator))
(assignment-operator-regexp
(php-type-regexp 'assignment-operator))
last-point
(statement-text
(buffer-substring-no-properties begin bound)))
(php-format-break-statement-text verbose)
(php-delete-horizontal-space)
(unless (looking-at-p (concat ws-re "*$"))
(newline-and-indent))
(- (point) old-end)))))
(goto-char start-pos)
change)))))))
(defun php-format-break-statement-text (&optional in-buffer)
"Function that takes ``statement-text'' and breaks it in a temp
buffer, returning the broken statement. Can be told to perform
everything IN-BUFFER."
(cond (in-buffer (php-format-break-statement-loops)
(goto-char bound))
(t (goto-char bound)
(insert (chomp (with-temp-buffer
(php+-mode)
(insert "<?php")
(newline)
(dotimes (i (/ begin-column php-basic-offset))
(indent-according-to-mode)
(insert "if ($test) {\n"))
(indent-according-to-mode)
(save-excursion
(insert statement-text))
(let* ((delta (- begin (point)))
(begin (point))
(bound (- bound delta))
(end (- end delta)))
(php-format-break-statement-loops)
(buffer-substring begin (point-max))))))
(save-excursion
(delete-region begin bound)))))
(defun php-format-break-statement-loops ()
"Actual breaking loops for php-format-break-statement."
(dolist (break-regexp `((boolean+ . ,(concat "&&\\|||\\|" ws-re "+as"
ws-re "+\\|=>\\|"
"[^.+-*/%&|^<>!=]\\(?1:=\\)"
"[^=>]"))
(acc1 . ,accessor-regexp)
(concats . "\\(?1:\\.\\)[^=]")
(commas . ",")
(sexp1 . "[{()};]")
(concats+commas . "\\(?1:\\.\\)[^=]\\|,")
(sexp2 . "[(,);]")
(tern1 . ,ternary-regexp)
(quotes . "['\"]")
(ops1 . ,operator-regexp)
(acc2 . ,accessor-regexp)
(ass-ops
. ,assignment-operator-regexp)
(tern2 . ,ternary-regexp)
(ops2 . ,operator-regexp)
(brackets . "[][]")))
(let ((pass-name (first break-regexp))
(break-regexp (rest break-regexp)))
(setf last-point -1)
(goto-char begin)
(while (and (< (point) bound)
(re-search-forward break-regexp bound t))
(catch 'skip
(when verbose (message "While point: %s" (point)))
(when (<= (point) last-point)
(when verbose
(message (concat "Infinite loop %s: %s <= %s detected in "
"php-format-break-statement. Advancing "
"through statement.")
pass-name (point) last-point))
(forward-char)
(throw 'skip t))
(setf last-point (point))
(let* ((sexp-level (php-get-current-sexp-level))
(whole-match (match-string-no-properties 0))
(whole-match-begin (match-beginning 0))
(whole-match-end (match-end 0))
(i (if (match-string-no-properties 1) 1 0))
(best-match (match-string-no-properties i))
(best-match-begin (match-beginning i))
(best-match-end (match-end i))
(best-match-length (- best-match-end best-match-begin)))
(when verbose
(message "Whole-match %s - %s (%s)" whole-match-begin
whole-match-end whole-match)
(message "Best-match %s - %s (%s)" best-match-begin
best-match-end best-match))
(let ((string-start (php-in-text-structp))
(looking-at-quote (or (string= best-match "'")
(string= best-match "\""))))
(when (or (and looking-at-quote
(wholenump string-start)
(< string-start (1- (point))))
(and (or (not looking-at-quote) (not string-start))
(php-in-text-structp nil best-match-begin
(1- best-match-end))))
(goto-char best-match-end)
(throw 'skip t)))
(when (eq pass-name 'tern2)
(when php-format-break-all-ternary-parts
(php-format-break-statement-ternary-aux))
(throw 'skip t))
(when (or (and (eq pass-name 'boolean+)
(or in-control (not (string= best-match "=>"))))
(and (eq pass-name 'tern1)
(string-match-p ternary-regexp whole-match))
(and (or (and (or (eq pass-name 'ops1)
(eq pass-name 'ops2))
(not in-control))
(and (member pass-name '(concats concats+commas))
(or in-control (<= sexp-level 1))))
(string-match-p operator-regexp whole-match))
(and (eq pass-name 'ass-ops)
(string-match-p assignment-operator-regexp
whole-match)))
(when (string= best-match ".")
(save-excursion
(goto-char best-match-begin)
(when (looking-back-p "^[[:space:]]*")
(let ((cur-line (line-number-at-pos)))
(end-of-line 0)
(when (not (= cur-line (line-number-at-pos)))
(let ((gap (php-format-slurp-operands
max-length "\\."
best-match-begin)))
(setf last-point (+ last-point gap)
best-match-begin (+ best-match-begin gap)
best-match-end (+ best-match-end gap)
bound (+ bound gap))))))))
(php-format-break-statement-operator-aux)
(goto-char best-match-end)
(throw 'skip t))
(when (or (and (eq pass-name 'acc1)
(zerop (php-get-current-sexp-level)))
(eq pass-name 'acc2))
(php-format-break-statement-accessor-aux)
(throw 'skip t))
(when (looking-back-p "\\.")
(when (or (not (eq pass-name 'concats+commas))
(<= sexp-level 1))
(php-format-break-statement-concat-aux))
(throw 'skip t))
(when (or (looking-back-p "'")
(looking-back-p "\""))
(php-format-break-statement-quote-aux)
(forward-char)
(when (looking-at-p "[[:space:]]*$")
(let ((gap (php-format-slurp-operands max-length "\\.")))
(setf bound (+ bound gap))))
(throw 'skip t))
(when (looking-back-p "{")
(php-format-break-statement-opening-brace-aux)
(throw 'skip t))
(when (or (and (looking-back-p "(")
(or (eq pass-name 'sexp1) (not in-function-def)))
(looking-back-p "\\["))
(php-format-break-statement-opening-parenthesis-aux)
(throw 'skip t))
(when (looking-back-p ";")
(php-format-break-statement-semicolon-aux)
(throw 'skip t))
(when (and (looking-back-p ",")
(or (zerop (php-get-current-sexp-level))
(not (eq pass-name 'commas))))
(php-format-break-statement-commas-aux)
(throw 'skip t))
(when (or (looking-back-p ")")
(looking-back-p "]")
(looking-back-p "}"))
(php-format-break-statement-closing-parenthesis-aux)
(throw 'skip t))))))))
(defun php-format-slurp-operands (&optional max-length operator-regexp
change-point)
"Run ``php-format-slurp-next-operand'' until it returns 0.
Return the total change in text size. You may tell it that you
only care about the movement of CHANGE-POINT."
(let ((total-change 0))
(catch 'done
(while t
(let ((change (php-format-slurp-next-operand max-length
operator-regexp
change-point)))
(when (zerop change) (throw 'done total-change))
(setf total-change (+ total-change change)
change-point (+ change-point change)))))
total-change))
(defun php-format-slurp-next-operand (&optional max-length operator-regexp
change-point)
"This function looks at the space left between the end of this
line and MAX-LENGTH, and the length of next operand on the line
below. If it is short enough, it will slurp it up. Returns the
change in text size. You may tell it that you only care about
the movement of CHANGE-POINT."
(let ((max-length (if (wholenump max-length) max-length 80))
(operator-regexp (or operator-regexp
(concat (php-type-regexp 'operator) "\\|"
(php-type-regexp 'assignment-operator))))
(cur-line (line-number-at-pos))
(change-point (if (wholenump change-point) change-point (point)))
(change 0))
(save-excursion
(end-of-line)
(let ((gap (php-delete-horizontal-space)))
(when (<= (point) change-point)
(setf change gap change-point (+ change-point gap))))
(when (looking-at-p (concat ws-re "*" operator-regexp))
(let ((end-col-1 (current-column)))
(beginning-of-line-non-whitespace 2)
(when (> (line-number-at-pos) cur-line)
(let ((start-col (current-column)))
(save-match-data
(when (looking-at operator-regexp)
(goto-char (if (match-end 1) (match-end 1) (match-end 0)))
(unless (looking-at-p non-ws-re)
(re-search-forward non-ws-re (line-end-position) t)
(backward-char)))
(let ((bounds (php-atom-bounds)))
(when (and bounds (not (looking-at-p "[]})]")))
(goto-char (second bounds))
(when (looking-at-p "[,;]")
(forward-char))
(let ((operand-length (- (current-column) start-col)))
(when (and (= (line-number-at-pos)
(1+ cur-line))
(< (+ end-col-1 1 operand-length) max-length))
(if (looking-at "\\([[:space:]]\\)*$")
(let ((b (match-beginning 1))
(e (match-end 1)))
(when (and (integerp b) (integerp e))
(let ((gap (- b e)))
(when (<= (point) change-point)
(setf change (+ change gap)
change-point (+ change gap))))
(delete-region b e)))
(save-excursion
(let ((gap (1+ (newline-and-indent))))
(when (<= (point) change-point)
(setf change (+ change gap)
change-point (+ change-point gap))))))
(beginning-of-line-non-whitespace)
(let ((gap (php-delete-horizontal-space t)))
(when (<= (point) change-point)
(setf change (+ change gap)
change-point (+ change-point gap))))
(backward-delete-char 1)
(insert " ")))))))))))
change))
(defun php-format-break-string (&optional strip-newlines max-length begin end)
"This function breaks a PHP string with the proper quotes and
concatenation operator at a maximum length of
MAX-LENGTH. Optionally start at BEGIN and stop at END. Returns
number of characters added. Optionally STRIP-NEWLINES as well."
(interactive "P")
(let* ((max-length (or max-length 80))
(statement-begin-col (save-excursion
(goto-char (php-in-statementp))
(current-column)))
(begin (if (integerp begin) begin (or (php-in-stringp)
(line-beginning-position)))))
(save-excursion
(save-match-data
(goto-char begin)
(let* ((end (if (integerp end)
end
(save-excursion
(forward-sexp)
(if (looking-at-p "[];)]")
(point)
(1- (point))))))
(old-end end))
(catch 'done
(when (>= (current-column) max-length)
(setq end (+ end (newline-and-indent))))
(let* ((this-quote (char-after))
(this-string-begin begin))
(while (< (point) (1- end))
(forward-char)
(while (and strip-newlines (looking-at-p "\n"))
(delete-char 1)
(setf end (1- end)))
(when (or (= (current-column) (1- max-length))
(looking-at-p "\n"))
(if (not (looking-at-p "\n"))
(re-search-backward ws-re this-string-begin t)
(when (= this-quote ?\")
(insert "\\n")
(setf end (+ 2 end))))
(when (not php-format-break-string-before-white-space)
(re-search-forward non-ws-re nil t)
(backward-char))
(when (and (not php-format-break-start-line-with-concat)
(>= (current-column) (- max-length 3)))
(re-search-backward ws-re this-string-begin t))
(when (= (char-after) this-quote)
(throw 'done t))
(insert this-quote)
(insert " .")
(setf end (+ 3 end))
(when php-format-break-start-line-with-concat
(insert " ")
(setf end (1+ end)))
(when (and (looking-at-p "\n")
(= this-quote ?\"))
(delete-char 1)
(setf end (1- end)))
(insert this-quote)
(setf end (1+ end))
(when (and (looking-at-p "\n")
(= this-quote ?\"))
(insert "\\n")
(delete-char 1)
(setf end (1+ end)))
(backward-char (if php-format-break-start-line-with-concat
3 1))
(when (looking-back-p ws-re)
(backward-char)
(setf end (+ end (php-delete-horizontal-space))))
(setq end (+ end (newline-and-indent)))
(re-search-forward (char-to-string this-quote))
(setf this-string-begin (point))))
(forward-char 1)))
(- end old-end))))))
(defun php-format-break-statement-ternary-aux ()
"Auxillary function used by ``php-format-break-statement''."
(let* ((ternary-bounds (php-get-ternary-bounds))
(start-line (save-excursion
(goto-char (first ternary-bounds))
(line-number-at-pos)))
(q-line (save-excursion
(goto-char (third ternary-bounds))
(line-number-at-pos)))
(c-line (save-excursion
(goto-char (fourth ternary-bounds))
(line-number-at-pos))))
(if (or (and (= start-line q-line) (= q-line c-line))
(not (or (= start-line q-line) (= q-line c-line))))
(goto-char (second ternary-bounds))
(let ((break-point (if (= start-line q-line)
(third ternary-bounds)
(fourth ternary-bounds))))
(goto-char break-point)
(let ((gap (newline-and-indent)))
(setf bound (+ bound gap))
(goto-char (+ gap (second ternary-bounds))))))))
(defun php-format-break-statement-operator-aux ()
"Auxillary function used by ``php-format-break-statement''."
(unless (and in-function-def (not (string= best-match "=")))
(goto-char best-match-begin)
(when (looking-back-p "^[[:space:]]*")
(let ((gap (indent-according-to-mode)))
(setf best-match-begin (+ best-match-begin gap)
best-match-end (+ best-match-end gap)
last-point (+ last-point gap)
bound (+ bound gap))))
(if (not (save-excursion
(re-search-backward non-ws-re nil t)
(php-atom-bounds)))
(goto-char whole-match-end)
(let* ((changes (clean-up-whitespace-around-chunk
best-match-begin best-match-end t))
(begin-gap (first changes))
(gap (+ begin-gap (second changes))))
(setf best-match-begin (+ begin-gap best-match-begin)
best-match-end (+ begin-gap best-match-end)
last-point (+ begin-gap last-point)
bound (+ gap bound))
(unless in-function-def
(if (looking-back-p "^[[:space:]]*")
(let ((gap (indent-according-to-mode)))
(setf best-match-begin (+ best-match-begin gap)
best-match-end (+ best-match-end gap)
bound (+ bound gap) last-point (+ last-point gap)))
(goto-char best-match-end)
(let* ((operand-end
(save-excursion
(catch 'done
(while t
(unless (looking-at-p non-ws-re)
(save-match-data
(re-search-forward non-ws-re bound t))
(backward-char))
(let ((current-atom (php-atom-bounds)))
(if current-atom
(let ((atom-end (second current-atom)))
(if (<= atom-end bound)
(goto-char (second current-atom))
(throw 'done bound)))
(or (re-search-forward ws-re bound t)
(throw 'done (1- bound)))))
(cond ((eq pass-name 'boolean+)
(let ((op-end (1- (point))))
(if (not (re-search-forward non-ws-re bound
t))
(throw 'done op-end)
(backward-char)
(when (or (looking-at-p break-regexp)
(looking-at-p "[],)]"))
(throw 'done op-end)))))
((looking-back-p "new") (forward-char))
(t (if (not (looking-at-p "[],);]"))
(throw 'done (1- (point)))
(forward-char)
(unless (member best-match
'("extends" "implements"))
(throw 'done (1- (point)))))))))))
(gap (php-clean-up-whitespace-around-operators
(point) operand-end t
(when php-format-align-array-double-arrows "=>"))))
(setf operand-end (+ gap operand-end) bound (+ gap bound))
(let* ((operand-end-stats (save-excursion
(goto-char operand-end)
`(,(current-column)
,(line-number-at-pos))))
(operand-end-col (first operand-end-stats))
(operand-end-line (second operand-end-stats))
(num-lines (1+ (- operand-end-line (line-number-at-pos)))))
(when (and (or (>= operand-end-col max-length)
(and (> num-lines 1)
(save-excursion
(catch 'needs-breaking
(dotimes (i num-lines)
(end-of-line)
(when (>= (current-column) max-length)
(throw 'needs-breaking t))
(forward-line))))))
(not (and (eq pass-name 'boolean+)
(string= best-match "="))))
(goto-char best-match-begin)
(let ((gap (newline-and-indent)))
(setf operand-end (+ gap operand-end)
bound (+ gap bound)))))
(goto-char (1+ operand-end)))))))))
(defun php-format-break-statement-accessor-aux ()
"Auxillary function used by ``php-format-break-statement''."
(when (looking-back-p "->")
(let* ((bounds (php-atom-bounds))
(end (second bounds))
(need-to-break (and (not (php-in-method-call))
(save-excursion
(goto-char end)
(> (current-column) max-length))))
(gap 0))
(when need-to-break
(setf gap (php-format-break-at-arrows max-length (point) end))
(setq bound (+ bound gap) end (+ end gap)))
(goto-char end))))
(defun php-format-break-statement-concat-aux ()
"Auxillary function used by ``php-format-break-statement''."
(if (looking-at-p "=")
(forward-char)
(save-excursion
(backward-char 2)
(when (looking-at-p non-ws-re)
(forward-char)
(insert " ")))
(let* ((begin (php-beginning-of-concat begin))
(end (php-end-of-concat)))
(setq bound (+ bound (php-format-break-at-concats max-length begin end)))
(php-skip-this-concat))))
(defun php-format-break-statement-quote-aux ()
"Auxillary function used by ``php-format-break-statement''."
(if (not (zerop (php-get-current-sexp-level "[")))
(php-skip-this-text-struct)
(backward-char)
(let ((begin (point))
(begin-col (current-column))
(begin-line (line-number-at-pos)))
(forward-sexp)
(unless (looking-at-p "[];),]")
(backward-char))
(unless (>= begin-col max-length)
(when (or (>= (current-column) max-length)
(and strip-newlines-in-string
(> (line-number-at-pos) begin-line)))
(let* ((end (point))
(gap (php-format-break-string strip-newlines-in-string
max-length begin end)))
(setf bound (+ bound gap))
(goto-char (+ end gap))))))))
(defun php-format-break-statement-opening-brace-aux ()
"Auxillary function used by ``php-format-break-statement''."
(save-excursion
(backward-char)
(if (and (php-class/interface-definitionp)
(not (looking-back "^[[:space:]]*")))
(let ((gap (+ (php-delete-horizontal-space)
(newline-and-indent))))
(setf bound (+ bound gap) last-point (+ bound gap)))
(when (re-search-backward ")" begin t)
(let ((multiline-call
(not (= (line-number-at-pos)
(save-excursion
(forward-char)
(backward-sexp)
(line-number-at-pos))))))
(when multiline-call
(unless (looking-back (concat "^" ws-re "*"))
(setf bound (1+ bound))
(setf bound (+ bound (indent-according-to-mode)))))
(forward-char)
(let ((gap (php-delete-horizontal-space)))
(insert " ")
(setf bound (+ bound gap 1) last-point (+ last-point gap 1)))
(if (or (and (php-anonymous-function-definitionp)
(or php-format-break-anonymous-function-always
(save-excursion
(forward-sexp)
(> (current-column) max-length))))
(and (php-named-function-definitionp) (not multiline-call)))
(progn
(unless (php-named-function-definitionp)
(forward-char))
(let ((gap (1+ (newline-and-indent))))
(setf bound (+ bound gap) last-point (+ last-point gap)))
(when (and (looking-at-p "{")
(not (looking-at-p (concat "{" ws-re "*$"))))
(forward-char)
(setf bound (+ 1 bound (newline-and-indent))))
(backward-char))))))
(when (looking-back-p non-ws-re)
(insert " ")
(setf bound (1+ bound) last-point (1+ last-point)))
(when (looking-at-p (concat ws-re "*$"))
(php-delete-horizontal-space))))
(defun php-format-break-statement-opening-parenthesis-aux ()
"Auxillary function used by ``php-format-break-statement''."
(when (<= (current-column) max-length)
(setf bound (+ bound (php-delete-horizontal-space nil t)))
(backward-char)
(save-match-data
(re-search-backward non-ws-re nil t)
(forward-char)
(when (looking-back-p (php-type-regexp 'identifier))
(let ((gap (php-delete-horizontal-space)))
(setf bound (+ bound gap) last-point (+ last-point gap)))))
(when (or (looking-back-p "return")
(looking-back-p "function")
(and in-control
(looking-at-p "(")
(= sexp-level 1)
(looking-back-p "[^([:space:]\n]")))
(insert " ")
(setf bound (1+ bound) last-point (1+ last-point)))
(let* ((skipped 0)
(sexp-ends (save-excursion
(forward-sexp)
(while (looking-at-p "[])]")
(forward-char)
(incf skipped))
(dotimes (i (min skipped (or broke-once 0)))
(backward-char))
(when (looking-at-p ";")
(forward-char))
(save-match-data
(when (looking-at (concat "[[:space:]]*{"))
(goto-char (match-end 0))))
`(,(1- (point)) ,(1- (current-column))
,(line-number-at-pos))))
(sexp-end (first sexp-ends))
(sexp-end-col (second sexp-ends))
(sexp-end-line (third sexp-ends))
(in-control (php-in-control-statementp nil t)))
(if (and (= sexp-end-col (+ 2 (current-column)))
(= sexp-end-line (line-number-at-pos)))
(forward-char 2)
(if (or (>= sexp-end-col max-length)
(and (> sexp-end-line (line-number-at-pos))
(not (looking-at-p (concat ws-re "*$"))))
(and (wholenump broke-once)
(= (1+ broke-once) (php-get-current-sexp-level))
(not (looking-at-p "\\["))
php-format-break-all-method-call-arguments)
(and php-format-break-anonymous-function-always
(save-excursion
(re-search-forward
(concat "function" ws-re "*(") sexp-end t))))
(if (or (or php-format-break-function-call-like-definition
(not (php-function-definitionp)))
(and (> (php-get-current-sexp-level) 0)
(looking-back-p "array")))
(let* ((function-call
(unless (or in-control
(and (looking-at-p "\\[")
(php-in-control-statementp)))
(looking-back-p (php-type-regexp 'identifier)))))
(forward-char)
(if in-control
(throw 'skip t)
(when function-call
(setq broke-once (php-get-current-sexp-level))
(setf bound (+ 1 bound (newline-and-indent))))))
(setf bound
(+ bound
(php-format-break-at-commas max-length (1+ (point))
sexp-end)))
(forward-char))
(forward-sexp))))))
(defun php-format-break-statement-commas-aux ()
"Auxillary function used by ``php-format-break-statement''."
(save-excursion
(backward-char)
(let ((gap (php-delete-horizontal-space)))
(setf bound (+ gap bound) last-point (+ gap last-point))))
(when (looking-at-p ws-re)
(setf bound (+ bound (php-delete-horizontal-space))))
(unless (looking-at-p "$")
(let* ((current-line (line-number-at-pos))
(atom-end-col (save-excursion (php-skip-this-atom) (current-column)))
(sexp-begin (php-find-current-sexp-begin))
(sexp-begin-line (save-excursion
(cond ((integerp sexp-begin)
(goto-char sexp-begin)
(line-number-at-pos))
(t (beginning-of-line-non-whitespace)
(line-number-at-pos)))))
(multilinep (or (>= (current-column) max-length)
(>= atom-end-col max-length)
(not (= current-line sexp-begin-line)))))
(if (and multilinep (or php-format-break-function-call-like-definition
(not in-function-def)))
(setf bound (+ 1 bound (newline-and-indent)))
(insert " ")
(setf bound (1+ bound))))))
(defun php-format-break-statement-semicolon-aux ()
"Auxillary function used by ``php-format-break-statement''."
(unless (>= (point) bound)
(let ((sexp-begin (php-in-control-statementp "for" t)))
(if sexp-begin
(php-format-break-statement-for-semicolons-aux)
(unless (or (looking-at-p (concat ws-re "*$"))
(= (line-number-at-pos)
(save-excursion
(when (php-find-current-sexp-begin "{" nil t)
(line-number-at-pos)))))
(set 'bound (+ 1 bound (newline-and-indent))))))))
(defun php-format-break-statement-for-semicolons-aux ()
"Auxillary function used by ``php-format-break-statement''."
(when (looking-at-p ws-re)
(setf bound (+ bound (php-delete-horizontal-space nil t))))
(let* ((current-line (line-number-at-pos))
(sexp-begin-line (when (integerp sexp-begin)
(save-excursion
(goto-char sexp-begin)
(line-number-at-pos))))
(sexp-end (when (integerp sexp-begin)
(save-excursion
(goto-char sexp-begin)
(save-match-data
(re-search-forward "(" nil t)
(backward-char)
(forward-sexp)
(point)))))
(sexp-end-col (when (integerp sexp-end)
(save-excursion
(goto-char sexp-end)
(current-column))))
(multilinep (not (= current-line
sexp-begin-line))))
(if (or (and php-format-break-all-for-semicolons