-
Notifications
You must be signed in to change notification settings - Fork 2
/
pi-php-mode.el
1613 lines (1448 loc) · 70.6 KB
/
pi-php-mode.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
;;; pi-php-mode.el --- major mode for editing PHP code
;; Copyright (C) 1999, 2000, 2001, 2003, 2004 Turadg Aleahmad
;; 2008 Aaron S. Hawley
;; 20011 Philippe Ivaldi
;; Maintainer: Philippe Ivaldi http://www.piprime.fr/
;; Author: Turadg Aleahmad, 1999-2004
;; Keywords: php languages oop
;; Created: 1999-05-17
;; $Last Modified on 2011/06/18
;; X-URL Original version http://php-mode.sourceforge.net/
;; X-URL This fork http://git.piprime.fr/?p=emacs/php-mode.git;a=summary
(defconst php-mode-version-number "pi-php-mode 2.0"
"PHP Mode version number.")
;;; License
;; This file 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 3
;; of the License, or (at your option) any later version.
;; This file 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 file; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Usage
;; Put this file in your Emacs lisp path (eg. site-lisp) and add to
;; your .emacs file:
;;
;; (require 'php-mode)
;; To use abbrev-mode, add lines like this:
;; (add-hook 'php-mode-hook
;; '(lambda () (define-abbrev php-mode-abbrev-table "ex" "extends")))
;; To make php-mode compatible with html-mode, see http://php-mode.sf.net
;; Many options available under Help:Customize
;; Options specific to php-mode are in
;; Programming/Languages/Php
;; Since it inherits much functionality from c-mode, look there too
;; Programming/Languages/C
;;; Commentary:
;; PHP mode is a major mode for editing PHP source code. It's
;; an extension of C mode; thus it inherits all C mode's navigation
;; functionality. But it colors according to the PHP grammar and indents
;; according to the PEAR coding guidelines. It also includes a couple
;; handy IDE-type features such as documentation search and a source
;; and class browser.
;;; Contributors: (in chronological order)
;; Juanjo, Torsten Martinsen, Vinai Kopp, Sean Champ, Doug Marcey,
;; Kevin Blake, Rex McMaster, Mathias Meyer, Boris Folgmann, Roland
;; Rosenfeld, Fred Yankowski, Craig Andrews, John Keller, Ryan
;; Sammartino, ppercot, Valentin Funk, Stig Bakken, Gregory Stark,
;; Chris Morris, Nils Rennebarth, Gerrit Riessen, Eric Mc Sween,
;; Ville Skytta, Giacomo Tesio, Lennart Borgman, Stefan Monnier,
;; Aaron S. Hawley, Ian Eure, Bill Lovett, Dias Badekas, David House
;; Philippe Ivaldi
;;; Changelog:
;; 2.1
;; Added from http://www.emacswiki.org/emacs/php-mode-improved.el
;; New customisation options for some of the syntax highlighting
;; features. I personally use the 'Gauchy' level of syntax
;; highlighting -- I want variables and function calls fontified --
;; but there were several very annoying "features" in this level of
;; syntax highlighting, particularly the ones that warn you about
;; perfectly valid code. I've added:
;;
;; * `php-mode-dollar-property-warning', which, if non-nil, warns on
;; $foo->$bar. (Default is nil.)
;; * `php-mode-dot-property-warning', which, if non-nil, warns on
;; $foo.bar. (Default is nil.)
;; * `php-mode-warn-on-unmatched', which, if non-nil, warns on
;; "everything else". (Default is t.)
;; * `php-mode-warn-if-mumamo-off', which, if nil, suppresses the
;; once-per-file warning about indenting with mumamo-mode turned
;; off. (Default is t)
;; 2.0
;; Philippe Ivaldi (starting fork)
;; Because of development of php-mode is dead, I forked it and rename it php-mode.el
;; Add Handling name spaces highlighting and # as comment char
;; Add support for heredoc indentation and highlighting
;; Add font-coloring for all the official php functions and new keywords in PHP 5.3
;; Optimize some piece of code
;; Fix indentation and set PEAR coding style the default
;; 1.5
;; Support function keywords like public, private and the ampersand
;; character for function-based commands. Support abstract, final,
;; static, public, private and protected keywords in Imenu. Fix
;; reversed order of Imenu entries. Use font-lock-preprocessor-face
;; for PHP and ASP tags. Make php-mode-modified a literal value
;; rather than a computed string. Add date and time constants of
;; PHP. (Dias Badekas) Fix false syntax highlighting of keywords
;; because of underscore character. Change HTML indentation warning
;; to match only HTML at the beginning of the line. Fix
;; byte-compiler warnings. Clean-up whitespace and audited style
;; consistency of code. Remove conditional bindings and XEmacs code
;; that likely does nothing.
;;
;; 1.4
;; Updated GNU GPL to version 3. Ported to Emacs 22 (CC mode
;; 5.31). M-x php-mode-version shows version. Provide end-of-defun
;; beginning-of-defun functionality. Support add-log library.
;; Fix __CLASS__ constant (Ian Eure). Allow imenu to see visibility
;; declarations -- "private", "public", "protected". (Bill Lovett)
;;
;; 1.3
;; Changed the definition of # using a tip from Stefan
;; Monnier to correct highlighting and indentation. (Lennart Borgman)
;; Changed the highlighting of the HTML part. (Lennart Borgman)
;;
;; See the ChangeLog file included with the source package.
;;; Code:
(eval-when-compile
(require 'cl)
(require 'regexp-opt))
(require 'speedbar)
(require 'font-lock)
(require 'cc-mode)
(require 'cc-langs)
(require 'custom)
(require 'etags)
(require 'imenu)
(require 'thingatpt)
;; Local variables
(defgroup php nil
"Major mode `php-mode' for editing PHP code."
:prefix "php-"
:group 'languages)
(defcustom php-default-face 'default
"Default face in `php-mode' buffers."
:type 'face
:group 'php)
(defcustom php-speedbar-config t
"When set to true automatically configures Speedbar to observe PHP files.
Ignores php-file patterns option; fixed to expression \"\\.\\(inc\\|php[s34]?\\)\""
:type 'boolean
:set (lambda (sym val)
(set-default sym val)
(if (and val (boundp 'speedbar))
(speedbar-add-supported-extension
"\\.\\(inc\\|php[s34]?\\|phtml\\|ctp\\)")))
:group 'php)
(defcustom php-highlight-function-call t
"When set to true highlight official PHP functions.
This can slow buffer loading."
:type 'boolean
:group 'php)
(defcustom php-mode-speedbar-open nil
"Normally `php-mode' starts with the speedbar closed.
Turning this on will open it whenever `php-mode' is loaded."
:type 'boolean
:set (lambda (sym val)
(set-default sym val)
(when val
(speedbar 1)))
:group 'php)
(defcustom php-manual-url "http://www.php.net/manual/en/"
"URL at which to find PHP manual.
You can replace \"en\" with your ISO language code."
:type 'string
:group 'php)
(defcustom php-search-url "http://www.php.net/"
"URL at which to search for documentation on a word."
:type 'string
:group 'php)
(defcustom php-completion-file
(concat
(file-name-directory (or load-file-name buffer-file-name))
"php-completion-file.txt")
"Path to the file which contains the function names known to PHP."
:type 'string
:group 'php)
(defcustom php-manual-path "/usr/share/doc/php-doc/html/"
"Path to the directory which contains the PHP manual."
:type 'string
:group 'php)
;;;###autoload
(defcustom php-file-patterns '("\\.php[s0-9]?\\'" "\\.phtml\\'" "\\.inc\\'" "\\.ctp\\'")
"List of file patterns for which to automatically invoke `php-mode'."
:type '(repeat (regexp :tag "Pattern"))
:set (lambda (sym val)
(set-default sym val)
(let ((php-file-patterns-temp val))
(while php-file-patterns-temp
(add-to-list 'auto-mode-alist
(cons (car php-file-patterns-temp) 'php-mode))
(setq php-file-patterns-temp (cdr php-file-patterns-temp)))))
:group 'php)
(defcustom php-mode-hook nil
"List of functions to be executed on entry to `php-mode'."
:type 'hook
:group 'php)
(defcustom php-mode-pear-hook nil
"Hook called when a PHP PEAR file is opened with `php-mode'."
:type 'hook
:group 'php)
(defcustom php-mode-force-pear t
"PEAR coding rules are enforced when the filename contains \"PEAR.\"
Turning this on (the default) will force PEAR rules on all PHP files."
:type 'boolean
:group 'php)
(defcustom php-mode-dollar-property-warning nil
"If non-`nil', warn about expressions like $foo->$bar where you
might have meant $foo->bar. Defaults to `nil' since this is valid
code."
:type 'boolean
:group 'php)
(defcustom php-mode-dot-property-warning nil
"If non-`nil', wan about expressions like $foo.bar, which could
be a valid concatenation (if bar were a constant, or interpreted
as an unquoted string), but it's more likely you meant $foo->bar."
:type 'boolean
:group 'php)
(defcustom php-mode-warn-on-unmatched t
"If non-`nil', use `font-lock-warning-face' on any expression
that isn't matched by the other font lock regular expressions."
:type 'boolean
:group 'php)
(defcustom php-warn-if-mumamo-off t
"Warn once per buffer if you try to indent a buffer without
mumamo-mode turned on. Detects if there are any HTML tags in the
buffer before warning, but this is not very smart; e.g. if you
have any tags inside a PHP string, it will be fooled."
:type '(choice (const :tag "Warn" t) (const "Don't warn" nil))
:group 'php)
(defun php-mode-version ()
"Display string describing the version of PHP mode."
(interactive)
(message "PHP mode version is %s"
php-mode-version-number))
(defconst php-beginning-of-defun-regexp
"^\\s-*\\(?:\\(?:abstract\\|final\\|private\\|protected\\|public\\|static\\)\\s-+\\)*function\\s-+&?\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*("
"Regular expression for a PHP function.")
(defun php-beginning-of-defun (&optional arg)
"Move to the beginning of the ARGth PHP function from point.
Implements PHP version of `beginning-of-defun-function'."
(interactive "p")
(let ((arg (or arg 1)))
(while (> arg 0)
(re-search-backward php-beginning-of-defun-regexp
nil 'noerror)
(setq arg (1- arg)))
(while (< arg 0)
(end-of-line 1)
(let ((opoint (point)))
(beginning-of-defun 1)
(forward-list 2)
(forward-line 1)
(if (eq opoint (point))
(re-search-forward php-beginning-of-defun-regexp
nil 'noerror))
(setq arg (1+ arg))))))
(defun php-end-of-defun (&optional arg)
"Move the end of the ARGth PHP function from point.
Implements PHP befsion of `end-of-defun-function'
See `php-beginning-of-defun'."
(interactive "p")
(php-beginning-of-defun (- (or arg 1))))
(defvar php-warned-bad-indent nil)
(defun php-check-html-for-indentation ()
(let ((html-tag-re "^\\s-*</?\\sw+.*?>")
(here (point)))
(goto-char (line-beginning-position))
(if (or (when (boundp 'mumamo-multi-major-mode) mumamo-multi-major-mode)
;; Fix-me: no idea how to check for mmm or multi-mode
(save-match-data
(not (or (re-search-forward html-tag-re (line-end-position) t)
(re-search-backward html-tag-re (line-beginning-position) t)))))
(progn
(goto-char here)
t)
(goto-char here)
(setq php-warned-bad-indent t)
(let* ((known-multi-libs '(("mumamo" mumamo (lambda () (nxhtml-mumamo)))
("mmm-mode" mmm-mode (lambda () (mmm-mode 1)))
("multi-mode" multi-mode (lambda () (multi-mode 1)))))
(known-names (mapcar (lambda (lib) (car lib)) known-multi-libs))
(available-multi-libs (delq nil
(mapcar
(lambda (lib)
(when (locate-library (car lib)) lib))
known-multi-libs)))
(available-names (mapcar (lambda (lib) (car lib)) available-multi-libs))
(base-msg
(concat
"Indentation fails badly with mixed HTML/PHP in the HTML part in
plaín `php-mode'. To get indentation to work you must use an
Emacs library that supports 'multiple major modes' in a buffer.
Parts of the buffer will then be in `php-mode' and parts in for
example `html-mode'. Known such libraries are:\n\t"
(mapconcat 'identity known-names ", ")
"\n"
(if available-multi-libs
(concat
"You have these available in your `load-path':\n\t"
(mapconcat 'identity available-names ", ")
"\n\n"
"Do you want to turn any of those on? ")
"You do not have any of those in your `load-path'.")))
(is-using-multi
(catch 'is-using
(dolist (lib available-multi-libs)
(when (and (boundp (cadr lib))
(symbol-value (cadr lib)))
(throw 'is-using t))))))
(unless is-using-multi
(if available-multi-libs
(if (not (y-or-n-p base-msg))
(message "Did not do indentation, but you can try again now if you want")
(let* ((name
(if (= 1 (length available-multi-libs))
(car available-names)
;; Minibuffer window is more than one line, fix that first:
(message "")
(completing-read "Choose multiple major mode support library: "
available-names nil t
(car available-names)
'(available-names . 1)
)))
(mode (when name
(caddr (assoc name available-multi-libs)))))
(when mode
;; Minibuffer window is more than one line, fix that first:
(message "")
(load name)
(funcall mode))))
(lwarn 'php-indent :warning base-msg)))
nil))))
(defun php-cautious-indent-region (start end &optional quiet)
(if (or (not php-warn-if-mumamo-off)
php-warned-bad-indent
(php-check-html-for-indentation))
(funcall 'c-indent-region start end quiet)))
(defun php-cautious-indent-line ()
(if (or (not php-warn-if-mumamo-off)
php-warned-bad-indent
(php-check-html-for-indentation))
(funcall 'c-indent-line)))
(defconst php-tags '("<?php" "?>" "<?" "<?="))
(defconst php-tags-key (regexp-opt php-tags))
(defconst php-block-stmt-1-kwds '("do" "else" "finally" "try"))
(defconst php-block-stmt-2-kwds
'("for" "if" "while" "switch" "foreach" "elseif" "catch all"))
(defconst php-block-stmt-1-key
(regexp-opt php-block-stmt-1-kwds))
(defconst php-block-stmt-2-key
(regexp-opt php-block-stmt-2-kwds))
(defconst php-class-decl-kwds '("class" "interface"))
(defconst php-class-key
(concat
"\\(" (regexp-opt php-class-decl-kwds) "\\)\\s-+"
(c-lang-const c-symbol-key c) ;; Class name.
"\\(\\s-+extends\\s-+" (c-lang-const c-symbol-key c) "\\)?" ;; Name of superclass.
"\\(\\s-+implements\\s-+[^{]+{\\)?")) ;; List of any adopted protocols.
(defun php-c-at-vsemi-p (&optional pos)
"Return t on html lines (including php region border), otherwise nil.
POS is a position on the line in question.
This is was done due to the problem reported here:
URL `https://answers.launchpad.net/nxhtml/+question/43320'"
(setq pos (or pos (point)))
(let ((here (point))
ret)
(save-match-data
(goto-char pos)
(beginning-of-line)
(setq ret (looking-at
(rx
(or (seq
bol
(0+ space)
"<"
(in "a-z\\?"))
(seq
;;(0+ anything)
(0+ not-newline)
(in "a-z\\?")
">"
(0+ space)
eol))))))
(goto-char here)
ret))
(defun php-c-vsemi-status-unknown-p ()
"See `php-c-at-vsemi-p'.")
;;;###autoload
(define-derived-mode php-mode c-mode "PHP"
"Major mode for editing PHP code.\n\n\\{php-mode-map}"
(c-add-language 'php-mode 'c-mode)
;; PHP doesn't have C-style macros.
;; HACK: Overwrite this syntax with rules to match <?php and others.
;; (c-lang-defconst c-opt-cpp-start php php-tags-key)
;; (c-lang-defvar c-opt-cpp-start (c-lang-const c-opt-cpp-start))
(set (make-local-variable 'c-opt-cpp-start) php-tags-key)
;; (c-lang-defconst c-opt-cpp-start php php-tags-key)
;; (c-lang-defvar c-opt-cpp-start (c-lang-const c-opt-cpp-start))
(set (make-local-variable 'c-opt-cpp-prefix) php-tags-key)
(c-set-offset 'cpp-macro 0)
;; (c-lang-defconst c-block-stmt-1-kwds php php-block-stmt-1-kwds)
;; (c-lang-defvar c-block-stmt-1-kwds (c-lang-const c-block-stmt-1-kwds))
(set (make-local-variable 'c-block-stmt-1-key) php-block-stmt-1-key)
;; (c-lang-defconst c-block-stmt-2-kwds php php-block-stmt-2-kwds)
;; (c-lang-defvar c-block-stmt-2-kwds (c-lang-const c-block-stmt-2-kwds))
(set (make-local-variable 'c-block-stmt-2-key) php-block-stmt-2-key)
;; Specify that cc-mode recognize Javadoc comment style
(set (make-local-variable 'c-doc-comment-style)
'((php-mode . javadoc)))
;; (c-lang-defconst c-class-decl-kwds
;; php php-class-decl-kwds)
(set (make-local-variable 'c-class-key) php-class-key)
;; [**************************************************************************************
;; Adapted FROM sh-script.el --- shell-script editing commands for Emacs (Daniel Pfeiffer)
(defface php-heredoc
'((((min-colors 88) (class color)
(background dark))
(:foreground "LightSalmon" :weight bold))
(((class color)
(background dark))
(:foreground "LightSalmon" :weight bold))
(((class color)
(background light))
(:foreground "tan1" ))
(t
(:weight bold)))
"Face to show a php here-document"
:group 'php)
(defvar php-heredoc-face 'php-heredoc)
(defconst php-escaped-line-re
;; Should match until the real end-of-continued-line, but if that is not
;; possible (because we bump into EOB or the search bound), then we should
;; match until the search bound.
"\\(?:\\(?:.*[^\\\n]\\)?\\(?:\\\\\\\\\\)*\\\\\n\\)*.*")
(defconst php-here-doc-open-re
(concat "<<<\\s-*\\\\?\\(\\(?:['\"][^'\"]+['\"]\\|\\sw\\)+\\)"
php-escaped-line-re "\\(\n\\)"))
(defvar php-here-doc-markers nil)
(make-variable-buffer-local 'php-here-doc-markers)
(defvar php-here-doc-re php-here-doc-open-re)
(make-variable-buffer-local 'php-here-doc-re)
(defconst php-here-doc-syntax (string-to-syntax "|")) ;; generic string
(defun php-font-lock-here-doc (limit)
"Search for a heredoc marker."
;; This looks silly, but it's because `php-here-doc-re' keeps changing.
(re-search-forward php-here-doc-re limit t))
(defun php-in-comment-or-string (start)
"Return non-nil if START is in a comment or string."
(save-excursion
(let ((state (syntax-ppss start)))
(or (nth 3 state) (nth 4 state)))))
(defun php-font-lock-open-heredoc (start string)
"Determine the syntax of the \\n after a <<<EOF.
START is the position of <<<.
STRING is the actual word used as delimiter (e.g. \"EOF\").
Point is at the beginning of the next line."
(unless (or (memq (char-before start) '(?< ?>))
(php-in-comment-or-string start))
;; We're looking at <<STRING, so we add "^STRING$" to the syntactic
;; font-lock keywords to detect the end of this here document.
(let ((str (replace-regexp-in-string "['\"]" "" string)))
(unless (member str php-here-doc-markers)
(push str php-here-doc-markers)
(setq php-here-doc-re
(concat php-here-doc-open-re "\\|^\\([ \t]*\\)"
(regexp-opt php-here-doc-markers t) "\\([\n; \t]\\)"))))
(let ((ppss (save-excursion (syntax-ppss (1- (point))))))
(if (nth 4 ppss)
;; The \n not only starts the heredoc but also closes a comment.
;; Let's close the comment just before the \n.
(put-text-property (1- (point)) (point) 'syntax-table '(12))) ;">"
(if (or (nth 5 ppss) (> (count-lines start (point)) 1))
;; If the php-escaped-line-re part of php-here-doc-re has matched
;; several lines, make sure we refontify them together.
;; Furthermore, if (nth 5 ppss) is non-nil (i.e. the \n is
;; escaped), it means the right \n is actually further down.
;; Don't bother fixing it now, but place a multiline property so
;; that when jit-lock-context-* refontifies the rest of the
;; buffer, it also refontifies the current line with it.
(put-text-property start (point) 'font-lock-multiline t)))
php-here-doc-syntax))
(defun php-font-lock-syntactic-face-function (state)
(let ((q (nth 3 state)))
(if q
(if (characterp q)
(if (eq q ?\`) 'sh-quoted-exec font-lock-string-face)
php-heredoc-face)
font-lock-comment-face)))
(defun php-font-lock-close-heredoc (bol eof indented)
"Determine the syntax of the \\n after an EOF.
If non-nil INDENTED indicates that the EOF was indented."
(let* ((eof-re (if eof (regexp-quote eof) ""))
;; A rough regexp that should find the opening <<<EOF back.
(sre (concat "<<<\\(-?\\)\\s-*['\"\\]?"
;; Use \s| to cheaply check it's an open-heredoc.
eof-re "['\"]?\\([ \t|;&)<>]"
php-escaped-line-re
"\\)?\\s|"))
;; A regexp that will find other EOFs.
(ere (concat "^" (if indented "[ \t;]*") eof-re "\n"))
(start (save-excursion
(goto-char bol)
(re-search-backward (concat sre "\\|" ere) nil t))))
;; If subgroup 1 matched, we found an open-heredoc, otherwise we first
;; found a close-heredoc which makes the current close-heredoc inoperant.
(cond
((and start (match-end 1)
(not (and indented (= (match-beginning 1) (match-end 1))))
(not (php-in-comment-or-string (match-beginning 0))))
php-here-doc-syntax)
((not (or start (save-excursion (re-search-forward sre nil t))))
;; There's no <<<EOF either before or after us,
;; so we should remove ourselves from font-lock's keywords.
(setq php-here-doc-markers (delete eof php-here-doc-markers))
(setq php-here-doc-re
(concat php-here-doc-open-re "\\|^\\([ \t]*\\)"
(regexp-opt php-here-doc-markers t) "\\(\n\\)"))
nil))))
(defconst php-font-lock-syntactic-keywords
'((php-font-lock-here-doc
(2 (php-font-lock-open-heredoc
(match-beginning 0) (match-string 1)) nil t)
(5 (php-font-lock-close-heredoc
(match-beginning 0) (match-string 4)
(and (match-beginning 3) (/= (match-beginning 3) (match-end 3))))
nil t))))
(defcustom php-here-document-word "EOF"
"Word to delimit here documents.
Any quote characters or leading whitespace in the word are
removed when closing the here document."
:type 'string
:group 'php)
(defun php-maybe-here-document (arg)
"Insert self. Without prefix, following `<<' inserts here document.
The document is bounded by `php-here-document-word'."
(interactive "*P")
(self-insert-command (prefix-numeric-value arg))
(or arg
(not (looking-back "[^<]<<<"))
(let ((delim (replace-regexp-in-string
"['\"]" ""
php-here-document-word)))
(insert php-here-document-word)
(insert ?\n)
(end-of-line 1)
(save-excursion
(insert ?\n (replace-regexp-in-string
"\\`$-?[ \t]*" "" delim) ";")))))
;; **************************************************************************************]
(make-local-variable 'font-lock-defaults)
(setq font-lock-defaults
`((php-font-lock-keywords-1
php-font-lock-keywords-2
php-font-lock-keywords-3)
nil ; KEYWORDS-ONLY
t ; CASE-FOLD
(("_" . "w")) ; SYNTAX-ALIST
nil ; SYNTAX-BEGIN
(font-lock-syntactic-keywords . php-font-lock-syntactic-keywords)
(font-lock-syntactic-face-function . php-font-lock-syntactic-face-function)
))
;; Electric behaviour must be turned off, they do not work since
;; they can not find the correct syntax in embedded PHP.
;;
;; Seems to work with narrowing so let it be on if the user prefers it.
;;(setq c-electric-flag nil)
(setq font-lock-maximum-decoration t
case-fold-search t)
;; Do not force newline at end of file. Such newlines can cause
;; trouble if the PHP file is included in another file before calls
;; to header() or cookie().
(set (make-local-variable 'require-final-newline) nil)
(set (make-local-variable 'next-line-add-newlines) nil)
;; PEAR coding standards
;; (add-hook 'php-mode-pear-hook
;; (lambda ()
;; (set (make-local-variable 'tab-width) 4)
;; (set (make-local-variable 'c-basic-offset) 4)
;; (set (make-local-variable 'indent-tabs-mode) nil)
;; (c-set-offset 'block-open' - )
;; (c-set-offset 'block-close' 0 )
;; (c-set-offset 'substatement-open '0)
;; (c-set-offset 'brace-list-open '0)
;; (c-set-offset 'arglist-close '0)
;; (c-set-offset 'statement-case-open '0)
;; (c-set-offset 'arglist-cont-nonempty '4)
;; (c-set-offset 'arglist-intro 'c-basic-offset)) nil t)
(add-hook 'php-mode-pear-hook
(lambda ()
(set (make-local-variable 'tab-width) 4)
(set (make-local-variable 'c-basic-offset) 4)
(set (make-local-variable 'indent-tabs-mode) nil)
(c-set-offset 'block-open' - )
(c-set-offset 'block-close' 0 )) nil t)
(if (or php-mode-force-pear
(and (stringp buffer-file-name)
(string-match "PEAR\\|pear"
(buffer-file-name))
(string-match "\\.php$" (buffer-file-name))))
(run-hooks 'php-mode-pear-hook))
(setq indent-line-function 'php-cautious-indent-line)
(setq indent-region-function 'php-cautious-indent-region)
(setq c-special-indent-hook nil)
(setq c-at-vsemi-p-fn 'php-c-at-vsemi-p)
(setq c-vsemi-status-unknown-p 'php-c-vsemi-status-unknown-p)
(set (make-local-variable 'beginning-of-defun-function)
'php-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function)
'php-end-of-defun)
(set (make-local-variable 'open-paren-in-column-0-is-defun-start)
nil)
(set (make-local-variable 'defun-prompt-regexp)
"^\\s-*function\\s-+&?\\s-*\\(\\(\\sw\\|\\s_\\)+\\)\\s-*")
(set (make-local-variable 'add-log-current-defun-header-regexp)
php-beginning-of-defun-regexp)
(run-hooks 'php-mode-hook))
(modify-syntax-entry ?# "< b" php-mode-syntax-table)
;; Make a menu keymap (with a prompt string)
;; and make it the menu bar item's definition.
(define-key php-mode-map [menu-bar] (make-sparse-keymap))
(define-key php-mode-map [menu-bar php]
(cons "PHP" (make-sparse-keymap "PHP")))
;; Define specific subcommands in this menu.
(define-key php-mode-map [menu-bar php complete-function]
'("Complete function name" . php-complete-function))
(define-key php-mode-map
[menu-bar php browse-manual]
'("Browse manual" . php-browse-manual))
(define-key php-mode-map
[menu-bar php search-documentation]
'("Search documentation" . php-search-documentation))
;; Define function name completion function
(defvar php-completion-table nil
"Obarray of tag names defined in current tags table and functions known to PHP.")
(defun php-complete-function ()
"Perform function completion on the text around point.
Completes to the set of names listed in the current tags table
and the standard php functions.
The string to complete is chosen in the same way as the default
for \\[find-tag] (which see)."
(interactive)
(let ((pattern (php-get-pattern))
beg
completion
(php-functions (php-completion-table)))
(if (not pattern) (message "Nothing to complete")
(if (not (search-backward pattern nil t))
(message "Can't complete here")
(setq beg (point))
(forward-char (length pattern))
(setq completion (try-completion pattern php-functions nil))
(cond ((eq completion t))
((null completion)
(message "Can't find completion for \"%s\"" pattern)
(ding))
((not (string= pattern completion))
(delete-region beg (point))
(insert completion))
(t
(message "Making completion list...")
(with-output-to-temp-buffer "*Completions*"
(display-completion-list
(all-completions pattern php-functions)))
(message "Making completion list...%s" "done")))))))
(defun php-completion-table ()
"Build variable `php-completion-table' on demand.
The table includes the PHP functions and the tags from the
current `tags-file-name'."
(or (and tags-file-name
(save-excursion (tags-verify-table tags-file-name))
php-completion-table)
(let ((tags-table
(if (and tags-file-name
(functionp 'etags-tags-completion-table))
(with-current-buffer (get-file-buffer tags-file-name)
(etags-tags-completion-table))
nil))
(php-table
(cond ((and (not (string= "" php-completion-file))
(file-readable-p php-completion-file))
(php-build-table-from-file php-completion-file))
((file-directory-p php-manual-path)
(php-build-table-from-path php-manual-path))
(t nil))))
(unless (or php-table tags-table)
(error
(concat "No TAGS file active nor are "
"`php-completion-file' or `php-manual-path' set")))
(when tags-table
;; Combine the tables.
(mapatoms (lambda (sym) (intern (symbol-name sym) php-table))
tags-table))
(setq php-completion-table php-table))))
(defun php-build-table-from-file (filename)
(let ((table (make-vector 1022 0))
;; (buf (find-file-noselect filename))
)
(with-temp-buffer
(insert-file-contents filename)
(goto-char (point-min))
(while (re-search-forward
"^\\([-a-zA-Z0-9_.]+\\)$"
nil t)
(intern (buffer-substring (match-beginning 1) (match-end 1))
table)))
table))
(defun php-build-table-from-path (path)
(let ((table (make-vector 1022 0))
(files (directory-files
path
nil
"^function\\..+\\.html$")))
(mapc (lambda (file)
(string-match "\\.\\([-a-zA-Z_0-9]+\\)\\.html$" file)
(intern
(replace-regexp-in-string
"-" "_" (substring file (match-beginning 1) (match-end 1)) t)
table))
files)
table))
;; Find the pattern we want to complete
;; find-tag-default from GNU Emacs etags.el
(defun php-get-pattern ()
(save-excursion
(while (looking-at "\\sw\\|\\s_")
(forward-char 1))
(if (or (re-search-backward "\\sw\\|\\s_"
(save-excursion (beginning-of-line) (point))
t)
(re-search-forward "\\(\\sw\\|\\s_\\)+"
(save-excursion (end-of-line) (point))
t))
(progn (goto-char (match-end 0))
(buffer-substring-no-properties
(point)
(progn (forward-sexp -1)
(while (looking-at "\\s'")
(forward-char 1))
(point))))
nil)))
(defun php-show-arglist ()
(interactive)
(let* ((tagname (php-get-pattern))
(buf (find-tag-noselect tagname nil nil))
arglist)
(save-excursion
(set-buffer buf)
(goto-char (point-min))
(when (re-search-forward
(format "function\\s-+%s\\s-*(\\([^{]*\\))" tagname)
nil t)
(setq arglist (buffer-substring-no-properties
(match-beginning 1) (match-end 1)))))
(if arglist
(message "Arglist for %s: %s" tagname arglist)
(message "Unknown function: %s" tagname))))
;; Define function documentation function
(defun php-search-documentation ()
"Search PHP documentation for the word at point."
(interactive)
(browse-url (concat php-search-url (current-word t))))
;; Define function for browsing manual
(defun php-browse-manual ()
"Bring up manual for PHP."
(interactive)
(browse-url php-manual-url))
;; Define shortcut
(define-key php-mode-map
"\C-c\C-f"
'php-search-documentation)
;; Define shortcut
(define-key php-mode-map
[(meta tab)]
'php-complete-function)
;; Define shortcut
(define-key php-mode-map
"\C-c\C-m"
'php-browse-manual)
;; Define shortcut
(define-key php-mode-map
'[(control .)]
'php-show-arglist)
(define-key php-mode-map "<" 'php-maybe-here-document)
(defconst php-constants
(eval-when-compile
(regexp-opt
'(;; core constants
"__LINE__" "__FILE__" "__DIR__"
"__FUNCTION__" "__CLASS__" "__METHOD__"
"PHP_OS" "PHP_VERSION"
"TRUE" "FALSE" "NULL"
"E_ERROR" "E_NOTICE" "E_PARSE" "E_WARNING" "E_ALL" "E_STRICT"
"E_USER_ERROR" "E_USER_WARNING" "E_USER_NOTICE"
"DEFAULT_INCLUDE_PATH" "PEAR_INSTALL_DIR" "PEAR_EXTENSION_DIR"
"PHP_BINDIR" "PHP_LIBDIR" "PHP_DATADIR" "PHP_SYSCONFDIR"
"PHP_LOCALSTATEDIR" "PHP_CONFIG_FILE_PATH"
"PHP_EOL"
;; date and time constants
"DATE_ATOM" "DATE_COOKIE" "DATE_ISO8601"
"DATE_RFC822" "DATE_RFC850" "DATE_RFC1036" "DATE_RFC1123"
"DATE_RFC2822" "DATE_RFC3339"
"DATE_RSS" "DATE_W3C"
;; from ext/standard:
"EXTR_OVERWRITE" "EXTR_SKIP" "EXTR_PREFIX_SAME"
"EXTR_PREFIX_ALL" "EXTR_PREFIX_INVALID" "SORT_ASC" "SORT_DESC"
"SORT_REGULAR" "SORT_NUMERIC" "SORT_STRING" "ASSERT_ACTIVE"
"ASSERT_CALLBACK" "ASSERT_BAIL" "ASSERT_WARNING"
"ASSERT_QUIET_EVAL" "CONNECTION_ABORTED" "CONNECTION_NORMAL"
"CONNECTION_TIMEOUT" "M_E" "M_LOG2E" "M_LOG10E" "M_LN2"
"M_LN10" "M_PI" "M_PI_2" "M_PI_4" "M_1_PI" "M_2_PI"
"M_2_SQRTPI" "M_SQRT2" "M_SQRT1_2" "CRYPT_SALT_LENGTH"
"CRYPT_STD_DES" "CRYPT_EXT_DES" "CRYPT_MD5" "CRYPT_BLOWFISH"
"DIRECTORY_SEPARATOR" "SEEK_SET" "SEEK_CUR" "SEEK_END"
"LOCK_SH" "LOCK_EX" "LOCK_UN" "LOCK_NB" "HTML_SPECIALCHARS"
"HTML_ENTITIES" "ENT_COMPAT" "ENT_QUOTES" "ENT_NOQUOTES"
"INFO_GENERAL" "INFO_CREDITS" "INFO_CONFIGURATION"
"INFO_ENVIRONMENT" "INFO_VARIABLES" "INFO_LICENSE" "INFO_ALL"
"CREDITS_GROUP" "CREDITS_GENERAL" "CREDITS_SAPI"
"CREDITS_MODULES" "CREDITS_DOCS" "CREDITS_FULLPAGE"
"CREDITS_QA" "CREDITS_ALL" "PHP_OUTPUT_HANDLER_START"
"PHP_OUTPUT_HANDLER_CONT" "PHP_OUTPUT_HANDLER_END"
"STR_PAD_LEFT" "STR_PAD_RIGHT" "STR_PAD_BOTH"
"PATHINFO_DIRNAME" "PATHINFO_BASENAME" "PATHINFO_EXTENSION"
"CHAR_MAX" "LC_CTYPE" "LC_NUMERIC" "LC_TIME" "LC_COLLATE"
"LC_MONETARY" "LC_ALL" "LC_MESSAGES" "LOG_EMERG" "LOG_ALERT"
"LOG_CRIT" "LOG_ERR" "LOG_WARNING" "LOG_NOTICE" "LOG_INFO"
"LOG_DEBUG" "LOG_KERN" "LOG_USER" "LOG_MAIL" "LOG_DAEMON"
"LOG_AUTH" "LOG_SYSLOG" "LOG_LPR" "LOG_NEWS" "LOG_UUCP"
"LOG_CRON" "LOG_AUTHPRIV" "LOG_LOCAL0" "LOG_LOCAL1"
"LOG_LOCAL2" "LOG_LOCAL3" "LOG_LOCAL4" "LOG_LOCAL5"
"LOG_LOCAL6" "LOG_LOCAL7" "LOG_PID" "LOG_CONS" "LOG_ODELAY"
"LOG_NDELAY" "LOG_NOWAIT" "LOG_PERROR"
;; Disabled by default because they slow buffer loading
;; If you have use for them, uncomment the strings
;; that you want colored.
;; To compile, you may have to increase 'max-specpdl-size'
;; from other bundled extensions:
;; "CAL_EASTER_TO_xxx" "VT_NULL" "VT_EMPTY" "VT_UI1" "VT_I2"
;; "VT_I4" "VT_R4" "VT_R8" "VT_BOOL" "VT_ERROR" "VT_CY" "VT_DATE"
;; "VT_BSTR" "VT_DECIMAL" "VT_UNKNOWN" "VT_DISPATCH" "VT_VARIANT"
;; "VT_I1" "VT_UI2" "VT_UI4" "VT_INT" "VT_UINT" "VT_ARRAY"
;; "VT_BYREF" "CP_ACP" "CP_MACCP" "CP_OEMCP" "CP_SYMBOL"
;; "CP_THREAD_ACP" "CP_UTF7" "CP_UTF8" "CPDF_PM_NONE"
;; "CPDF_PM_OUTLINES" "CPDF_PM_THUMBS" "CPDF_PM_FULLSCREEN"
;; "CPDF_PL_SINGLE" "CPDF_PL_1COLUMN" "CPDF_PL_2LCOLUMN"
;; "CPDF_PL_2RCOLUMN" "CURLOPT_PORT" "CURLOPT_FILE"
;; "CURLOPT_INFILE" "CURLOPT_INFILESIZE" "CURLOPT_URL"
;; "CURLOPT_PROXY" "CURLOPT_VERBOSE" "CURLOPT_HEADER"
;; "CURLOPT_HTTPHEADER" "CURLOPT_NOPROGRESS" "CURLOPT_NOBODY"
;; "CURLOPT_FAILONERROR" "CURLOPT_UPLOAD" "CURLOPT_POST"
;; "CURLOPT_FTPLISTONLY" "CURLOPT_FTPAPPEND" "CURLOPT_NETRC"
;; "CURLOPT_FOLLOWLOCATION" "CURLOPT_FTPASCII" "CURLOPT_PUT"
;; "CURLOPT_MUTE" "CURLOPT_USERPWD" "CURLOPT_PROXYUSERPWD"
;; "CURLOPT_RANGE" "CURLOPT_TIMEOUT" "CURLOPT_POSTFIELDS"
;; "CURLOPT_REFERER" "CURLOPT_USERAGENT" "CURLOPT_FTPPORT"
;; "CURLOPT_LOW_SPEED_LIMIT" "CURLOPT_LOW_SPEED_TIME"
;; "CURLOPT_RESUME_FROM" "CURLOPT_COOKIE" "CURLOPT_SSLCERT"
;; "CURLOPT_SSLCERTPASSWD" "CURLOPT_WRITEHEADER"
;; "CURLOPT_COOKIEFILE" "CURLOPT_SSLVERSION"
;; "CURLOPT_TIMECONDITION" "CURLOPT_TIMEVALUE"
;; "CURLOPT_CUSTOMREQUEST" "CURLOPT_STDERR" "CURLOPT_TRANSFERTEXT"
;; "CURLOPT_RETURNTRANSFER" "CURLOPT_QUOTE" "CURLOPT_POSTQUOTE"
;; "CURLOPT_INTERFACE" "CURLOPT_KRB4LEVEL"
;; "CURLOPT_HTTPPROXYTUNNEL" "CURLOPT_FILETIME"
;; "CURLOPT_WRITEFUNCTION" "CURLOPT_READFUNCTION"
;; "CURLOPT_PASSWDFUNCTION" "CURLOPT_HEADERFUNCTION"
;; "CURLOPT_MAXREDIRS" "CURLOPT_MAXCONNECTS" "CURLOPT_CLOSEPOLICY"
;; "CURLOPT_FRESH_CONNECT" "CURLOPT_FORBID_REUSE"
;; "CURLOPT_RANDOM_FILE" "CURLOPT_EGDSOCKET"
;; "CURLOPT_CONNECTTIMEOUT" "CURLOPT_SSL_VERIFYPEER"
;; "CURLOPT_CAINFO" "CURLOPT_BINARYTRANSER"
;; "CURLCLOSEPOLICY_LEAST_RECENTLY_USED" "CURLCLOSEPOLICY_OLDEST"
;; "CURLINFO_EFFECTIVE_URL" "CURLINFO_HTTP_CODE"
;; "CURLINFO_HEADER_SIZE" "CURLINFO_REQUEST_SIZE"
;; "CURLINFO_TOTAL_TIME" "CURLINFO_NAMELOOKUP_TIME"
;; "CURLINFO_CONNECT_TIME" "CURLINFO_PRETRANSFER_TIME"
;; "CURLINFO_SIZE_UPLOAD" "CURLINFO_SIZE_DOWNLOAD"
;; "CURLINFO_SPEED_DOWNLOAD" "CURLINFO_SPEED_UPLOAD"
;; "CURLINFO_FILETIME" "CURLE_OK" "CURLE_UNSUPPORTED_PROTOCOL"
;; "CURLE_FAILED_INIT" "CURLE_URL_MALFORMAT"
;; "CURLE_URL_MALFORMAT_USER" "CURLE_COULDNT_RESOLVE_PROXY"
;; "CURLE_COULDNT_RESOLVE_HOST" "CURLE_COULDNT_CONNECT"
;; "CURLE_FTP_WEIRD_SERVER_REPLY" "CURLE_FTP_ACCESS_DENIED"
;; "CURLE_FTP_USER_PASSWORD_INCORRECT"
;; "CURLE_FTP_WEIRD_PASS_REPLY" "CURLE_FTP_WEIRD_USER_REPLY"
;; "CURLE_FTP_WEIRD_PASV_REPLY" "CURLE_FTP_WEIRD_227_FORMAT"
;; "CURLE_FTP_CANT_GET_HOST" "CURLE_FTP_CANT_RECONNECT"