declare smobs in alloc.c
[bpt/emacs.git] / lisp / progmodes / cperl-mode.el
CommitLineData
f83d2997
KH
1;;; cperl-mode.el --- Perl code editing commands for Emacs
2
ba318903 3;; Copyright (C) 1985-1987, 1991-2014 Free Software Foundation, Inc.
f83d2997 4
5858f68c
GM
5;; Author: Ilya Zakharevich
6;; Bob Olson
4ab89e7b 7;; Maintainer: Ilya Zakharevich <ilyaz@cpan.org>
f83d2997
KH
8;; Keywords: languages, Perl
9
10;; This file is part of GNU Emacs.
11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
f83d2997 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
f83d2997
KH
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
f83d2997 24
4ab89e7b 25;;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
f83d2997
KH
26
27;;; Commentary:
28
83261a2f
SM
29;; You can either fine-tune the bells and whistles of this mode or
30;; bulk enable them by putting
f83d2997
KH
31
32;; (setq cperl-hairy t)
33
83261a2f
SM
34;; in your .emacs file. (Emacs rulers do not consider it politically
35;; correct to make whistles enabled by default.)
f83d2997 36
83261a2f
SM
37;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<<
38;; or as help on variables `cperl-tips', `cperl-problems', <<<<<<
15ca5699 39;; `cperl-praise', `cperl-speed'. <<<<<<
f83d2997 40
83261a2f
SM
41;; The mode information (on C-h m) provides some customization help.
42;; If you use font-lock feature of this mode, it is advisable to use
43;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock.
f83d2997 44
83261a2f
SM
45;; Faces used now: three faces for first-class and second-class keywords
46;; and control flow words, one for each: comments, string, labels,
47;; functions definitions and packages, arrays, hashes, and variable
48;; definitions. If you do not see all these faces, your font-lock does
49;; not define them, so you need to define them manually.
f83d2997 50
83261a2f
SM
51;; This mode supports font-lock, imenu and mode-compile. In the
52;; hairy version font-lock is on, but you should activate imenu
53;; yourself (note that mode-compile is not standard yet). Well, you
54;; can use imenu from keyboard anyway (M-x imenu), but it is better
55;; to bind it like that:
f83d2997
KH
56
57;; (define-key global-map [M-S-down-mouse-3] 'imenu)
58
83261a2f
SM
59;;; Font lock bugs as of v4.32:
60
61;; The following kinds of Perl code erroneously start strings:
62;; \$` \$' \$"
63;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
64;; likewise with m, tr, y, q, qX instead of s
65
f83d2997 66;;; Code:
4ab89e7b 67\f
b5b0cb34
JB
68(defvar vc-rcs-header)
69(defvar vc-sccs-header)
70
80585273 71(eval-when-compile
4ab89e7b
SM
72 (condition-case nil
73 (require 'custom)
74 (error nil))
75 (condition-case nil
76 (require 'man)
77 (error nil))
4ab89e7b 78 (defvar cperl-can-font-lock
6546555e 79 (or (featurep 'xemacs)
4ab89e7b
SM
80 (and (boundp 'emacs-major-version)
81 (or window-system
82 (> emacs-major-version 20)))))
83 (if cperl-can-font-lock
84 (require 'font-lock))
85 (defvar msb-menu-cond)
86 (defvar gud-perldb-history)
87 (defvar font-lock-background-mode) ; not in Emacs
88 (defvar font-lock-display-type) ; ditto
89 (defvar paren-backwards-message) ; Not in newer XEmacs?
90 (or (fboundp 'defgroup)
91 (defmacro defgroup (name val doc &rest arr)
92 nil))
93 (or (fboundp 'custom-declare-variable)
94 (defmacro defcustom (name val doc &rest arr)
9edd6ee6 95 `(defvar ,name ,val ,doc)))
4ab89e7b
SM
96 (or (and (fboundp 'custom-declare-variable)
97 (string< "19.31" emacs-version)) ; Checked with 19.30: defface does not work
98 (defmacro defface (&rest arr)
99 nil))
100 ;; Avoid warning (tmp definitions)
101 (or (fboundp 'x-color-defined-p)
102 (defmacro x-color-defined-p (col)
9edd6ee6 103 (cond ((fboundp 'color-defined-p) `(color-defined-p ,col))
4ab89e7b 104 ;; XEmacs >= 19.12
9edd6ee6 105 ((fboundp 'valid-color-name-p) `(valid-color-name-p ,col))
4ab89e7b 106 ;; XEmacs 19.11
9edd6ee6 107 ((fboundp 'x-valid-color-name-p) `(x-valid-color-name-p ,col))
4ab89e7b
SM
108 (t '(error "Cannot implement color-defined-p")))))
109 (defmacro cperl-is-face (arg) ; Takes quoted arg
110 (cond ((fboundp 'find-face)
9edd6ee6 111 `(find-face ,arg))
4ab89e7b
SM
112 (;;(and (fboundp 'face-list)
113 ;; (face-list))
114 (fboundp 'face-list)
9edd6ee6
SM
115 `(member ,arg (and (fboundp 'face-list)
116 (face-list))))
4ab89e7b 117 (t
9edd6ee6 118 `(boundp ,arg))))
4ab89e7b
SM
119 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
120 (cond ((fboundp 'make-face)
9edd6ee6 121 `(make-face (quote ,arg)))
4ab89e7b 122 (t
9edd6ee6 123 `(defvar ,arg (quote ,arg) ,descr))))
4ab89e7b 124 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
9edd6ee6
SM
125 `(progn
126 (or (cperl-is-face (quote ,arg))
127 (cperl-make-face ,arg ,descr))
128 (or (boundp (quote ,arg)) ; We use unquoted variants too
129 (defvar ,arg (quote ,arg) ,descr))))
6546555e 130 (if (featurep 'xemacs)
4ab89e7b 131 (defmacro cperl-etags-snarf-tag (file line)
9edd6ee6
SM
132 `(progn
133 (beginning-of-line 2)
134 (list ,file ,line)))
4ab89e7b 135 (defmacro cperl-etags-snarf-tag (file line)
9edd6ee6 136 `(etags-snarf-tag)))
6546555e 137 (if (featurep 'xemacs)
4ab89e7b 138 (defmacro cperl-etags-goto-tag-location (elt)
9edd6ee6
SM
139 ;;(progn
140 ;; (switch-to-buffer (get-file-buffer (elt ,elt 0)))
141 ;; (set-buffer (get-file-buffer (elt ,elt 0)))
142 ;; Probably will not work due to some save-excursion???
143 ;; Or save-file-position?
144 ;; (message "Did I get to line %s?" (elt ,elt 1))
145 `(goto-line (string-to-int (elt ,elt 1))))
4ab89e7b
SM
146 ;;)
147 (defmacro cperl-etags-goto-tag-location (elt)
9edd6ee6 148 `(etags-goto-tag-location ,elt))))
5bd52f0e 149
83261a2f 150(defvar cperl-can-font-lock
6546555e 151 (or (featurep 'xemacs)
83261a2f
SM
152 (and (boundp 'emacs-major-version)
153 (or window-system
154 (> emacs-major-version 20)))))
155
5bd52f0e
RS
156(defun cperl-choose-color (&rest list)
157 (let (answer)
158 (while list
159 (or answer
160 (if (or (x-color-defined-p (car list))
161 (null (cdr list)))
162 (setq answer (car list))))
163 (setq list (cdr list)))
164 answer))
165
ccc3ce39
SE
166(defgroup cperl nil
167 "Major mode for editing Perl code."
168 :prefix "cperl-"
db133cb6
RS
169 :group 'languages
170 :version "20.3")
171
172(defgroup cperl-indentation-details nil
173 "Indentation."
174 :prefix "cperl-"
175 :group 'cperl)
176
177(defgroup cperl-affected-by-hairy nil
178 "Variables affected by `cperl-hairy'."
179 :prefix "cperl-"
180 :group 'cperl)
181
182(defgroup cperl-autoinsert-details nil
183 "Auto-insert tuneup."
184 :prefix "cperl-"
185 :group 'cperl)
186
187(defgroup cperl-faces nil
188 "Fontification colors."
8ec3bce0 189 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
db133cb6
RS
190 :prefix "cperl-"
191 :group 'cperl)
192
193(defgroup cperl-speed nil
194 "Speed vs. validity tuneup."
195 :prefix "cperl-"
196 :group 'cperl)
197
198(defgroup cperl-help-system nil
199 "Help system tuneup."
200 :prefix "cperl-"
201 :group 'cperl)
ccc3ce39 202
f83d2997 203\f
ccc3ce39 204(defcustom cperl-extra-newline-before-brace nil
f83d2997
KH
205 "*Non-nil means that if, elsif, while, until, else, for, foreach
206and do constructs look like:
207
208 if ()
209 {
210 }
211
212instead of:
213
214 if () {
83261a2f 215 }"
ccc3ce39 216 :type 'boolean
db133cb6
RS
217 :group 'cperl-autoinsert-details)
218
5c8b7eaf 219(defcustom cperl-extra-newline-before-brace-multiline
db133cb6
RS
220 cperl-extra-newline-before-brace
221 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
222for constructs with multiline if/unless/while/until/for/foreach condition."
223 :type 'boolean
224 :group 'cperl-autoinsert-details)
ccc3ce39
SE
225
226(defcustom cperl-indent-level 2
227 "*Indentation of CPerl statements with respect to containing block."
228 :type 'integer
db133cb6 229 :group 'cperl-indentation-details)
f152a898 230
2d5590e0 231;; Is is not unusual to put both things like perl-indent-level and
f152a898
DN
232;; cperl-indent-level in the local variable section of a file. If only
233;; one of perl-mode and cperl-mode is in use, a warning will be issued
2d5590e0 234;; about the variable. Autoload these here, so that no warning is
f152a898
DN
235;; issued when using either perl-mode or cperl-mode.
236;;;###autoload(put 'cperl-indent-level 'safe-local-variable 'integerp)
2d5590e0
DN
237;;;###autoload(put 'cperl-brace-offset 'safe-local-variable 'integerp)
238;;;###autoload(put 'cperl-continued-brace-offset 'safe-local-variable 'integerp)
239;;;###autoload(put 'cperl-label-offset 'safe-local-variable 'integerp)
240;;;###autoload(put 'cperl-continued-statement-offset 'safe-local-variable 'integerp)
241;;;###autoload(put 'cperl-extra-newline-before-brace 'safe-local-variable 'booleanp)
242;;;###autoload(put 'cperl-merge-trailing-else 'safe-local-variable 'booleanp)
f83d2997 243
ccc3ce39 244(defcustom cperl-lineup-step nil
f83d2997 245 "*`cperl-lineup' will always lineup at multiple of this number.
029cb4d5 246If nil, the value of `cperl-indent-level' will be used."
ccc3ce39 247 :type '(choice (const nil) integer)
db133cb6
RS
248 :group 'cperl-indentation-details)
249
ccc3ce39 250(defcustom cperl-brace-imaginary-offset 0
f83d2997
KH
251 "*Imagined indentation of a Perl open brace that actually follows a statement.
252An open brace following other text is treated as if it were this far
ccc3ce39
SE
253to the right of the start of its line."
254 :type 'integer
db133cb6 255 :group 'cperl-indentation-details)
ccc3ce39
SE
256
257(defcustom cperl-brace-offset 0
258 "*Extra indentation for braces, compared with other text in same context."
259 :type 'integer
db133cb6 260 :group 'cperl-indentation-details)
ccc3ce39
SE
261(defcustom cperl-label-offset -2
262 "*Offset of CPerl label lines relative to usual indentation."
263 :type 'integer
db133cb6 264 :group 'cperl-indentation-details)
ccc3ce39
SE
265(defcustom cperl-min-label-indent 1
266 "*Minimal offset of CPerl label lines."
267 :type 'integer
db133cb6 268 :group 'cperl-indentation-details)
ccc3ce39
SE
269(defcustom cperl-continued-statement-offset 2
270 "*Extra indent for lines not starting new statements."
271 :type 'integer
db133cb6 272 :group 'cperl-indentation-details)
ccc3ce39 273(defcustom cperl-continued-brace-offset 0
f83d2997 274 "*Extra indent for substatements that start with open-braces.
ccc3ce39
SE
275This is in addition to cperl-continued-statement-offset."
276 :type 'integer
db133cb6 277 :group 'cperl-indentation-details)
ccc3ce39
SE
278(defcustom cperl-close-paren-offset -1
279 "*Extra indent for substatements that start with close-parenthesis."
280 :type 'integer
db133cb6 281 :group 'cperl-indentation-details)
ccc3ce39 282
4ab89e7b
SM
283(defcustom cperl-indent-wrt-brace t
284 "*Non-nil means indent statements in if/etc block relative brace, not if/etc.
285Versions 5.2 ... 5.20 behaved as if this were `nil'."
286 :type 'boolean
287 :group 'cperl-indentation-details)
288
ccc3ce39 289(defcustom cperl-auto-newline nil
f83d2997
KH
290 "*Non-nil means automatically newline before and after braces,
291and after colons and semicolons, inserted in CPerl code. The following
292\\[cperl-electric-backspace] will remove the inserted whitespace.
5c8b7eaf 293Insertion after colons requires both this variable and
ccc3ce39
SE
294`cperl-auto-newline-after-colon' set."
295 :type 'boolean
db133cb6 296 :group 'cperl-autoinsert-details)
f83d2997 297
6c389151
SM
298(defcustom cperl-autoindent-on-semi nil
299 "*Non-nil means automatically indent after insertion of (semi)colon.
300Active if `cperl-auto-newline' is false."
301 :type 'boolean
302 :group 'cperl-autoinsert-details)
303
ccc3ce39 304(defcustom cperl-auto-newline-after-colon nil
f83d2997 305 "*Non-nil means automatically newline even after colons.
ccc3ce39
SE
306Subject to `cperl-auto-newline' setting."
307 :type 'boolean
db133cb6 308 :group 'cperl-autoinsert-details)
f83d2997 309
ccc3ce39 310(defcustom cperl-tab-always-indent t
f83d2997 311 "*Non-nil means TAB in CPerl mode should always reindent the current line,
ccc3ce39
SE
312regardless of where in the line point is when the TAB command is used."
313 :type 'boolean
db133cb6 314 :group 'cperl-indentation-details)
f83d2997 315
ccc3ce39 316(defcustom cperl-font-lock nil
029cb4d5 317 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
ccc3ce39 318Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
319 :type '(choice (const null) boolean)
320 :group 'cperl-affected-by-hairy)
f83d2997 321
ccc3ce39 322(defcustom cperl-electric-lbrace-space nil
029cb4d5 323 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
ccc3ce39 324Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
325 :type '(choice (const null) boolean)
326 :group 'cperl-affected-by-hairy)
f83d2997 327
ccc3ce39 328(defcustom cperl-electric-parens-string "({[]})<"
f83d2997 329 "*String of parentheses that should be electric in CPerl.
ccc3ce39
SE
330Closing ones are electric only if the region is highlighted."
331 :type 'string
db133cb6 332 :group 'cperl-affected-by-hairy)
f83d2997 333
ccc3ce39 334(defcustom cperl-electric-parens nil
f83d2997 335 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
ccc3ce39 336Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
337 :type '(choice (const null) boolean)
338 :group 'cperl-affected-by-hairy)
339
340(defvar zmacs-regions) ; Avoid warning
341
5c8b7eaf 342(defcustom cperl-electric-parens-mark
f83d2997
KH
343 (and window-system
344 (or (and (boundp 'transient-mark-mode) ; For Emacs
345 transient-mark-mode)
346 (and (boundp 'zmacs-regions) ; For XEmacs
347 zmacs-regions)))
348 "*Not-nil means that electric parens look for active mark.
ccc3ce39
SE
349Default is yes if there is visual feedback on mark."
350 :type 'boolean
db133cb6 351 :group 'cperl-autoinsert-details)
f83d2997 352
ccc3ce39 353(defcustom cperl-electric-linefeed nil
f83d2997
KH
354 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
355In any case these two mean plain and hairy linefeeds together.
ccc3ce39 356Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
357 :type '(choice (const null) boolean)
358 :group 'cperl-affected-by-hairy)
f83d2997 359
ccc3ce39 360(defcustom cperl-electric-keywords nil
f83d2997 361 "*Not-nil (and non-null) means keywords are electric in CPerl.
350b4cb9
EZ
362Can be overwritten by `cperl-hairy' if nil.
363
364Uses `abbrev-mode' to do the expansion. If you want to use your
365own abbrevs in cperl-mode, but do not want keywords to be
366electric, you must redefine `cperl-mode-abbrev-table': do
367\\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
368that paragraph, delete the words that appear at the ends of lines and
369that begin with \"cperl-electric\".
370"
db133cb6
RS
371 :type '(choice (const null) boolean)
372 :group 'cperl-affected-by-hairy)
ccc3ce39 373
f739b53b
SM
374(defcustom cperl-electric-backspace-untabify t
375 "*Not-nil means electric-backspace will untabify in CPerl."
376 :type 'boolean
377 :group 'cperl-autoinsert-details)
378
ccc3ce39 379(defcustom cperl-hairy nil
db133cb6 380 "*Not-nil means most of the bells and whistles are enabled in CPerl.
5c8b7eaf 381Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
db133cb6
RS
382`cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
383`cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
384`cperl-lazy-help-time'."
ccc3ce39 385 :type 'boolean
db133cb6 386 :group 'cperl-affected-by-hairy)
ccc3ce39
SE
387
388(defcustom cperl-comment-column 32
389 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
390 :type 'integer
db133cb6 391 :group 'cperl-indentation-details)
ccc3ce39 392
4ab89e7b
SM
393(defcustom cperl-indent-comment-at-column-0 nil
394 "*Non-nil means that comment started at column 0 should be indentable."
395 :type 'boolean
396 :group 'cperl-indentation-details)
e1a5828f
AS
397
398(defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
399 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
400 :type '(repeat string)
401 :group 'cperl)
402
4ab89e7b 403(defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
e1a5828f
AS
404 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
405 :type '(repeat string)
4ab89e7b
SM
406 :group 'cperl)
407
408;; This became obsolete...
409(defvar cperl-vc-header-alist nil)
410(make-obsolete-variable
411 'cperl-vc-header-alist
cb5b40ee
SM
412 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead."
413 "22.1")
ccc3ce39 414
cb559258
GM
415;; (defcustom cperl-clobber-mode-lists
416;; (not
417;; (and
418;; (boundp 'interpreter-mode-alist)
419;; (assoc "miniperl" interpreter-mode-alist)
420;; (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
421;; "*Whether to install us into `interpreter-' and `extension' mode lists."
422;; :type 'boolean
423;; :group 'cperl)
5bd52f0e 424
ccc3ce39 425(defcustom cperl-info-on-command-no-prompt nil
f83d2997 426 "*Not-nil (and non-null) means not to prompt on C-h f.
6292d528 427The opposite behavior is always available if prefixed with C-c.
ccc3ce39 428Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
429 :type '(choice (const null) boolean)
430 :group 'cperl-affected-by-hairy)
431
432(defcustom cperl-clobber-lisp-bindings nil
433 "*Not-nil (and non-null) means not overwrite C-h f.
434The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
435Can be overwritten by `cperl-hairy' if nil."
436 :type '(choice (const null) boolean)
437 :group 'cperl-affected-by-hairy)
f83d2997 438
ccc3ce39 439(defcustom cperl-lazy-help-time nil
db133cb6
RS
440 "*Not-nil (and non-null) means to show lazy help after given idle time.
441Can be overwritten by `cperl-hairy' to be 5 sec if nil."
300f7bb3 442 :type '(choice (const null) (const nil) integer)
db133cb6 443 :group 'cperl-affected-by-hairy)
f83d2997 444
ccc3ce39 445(defcustom cperl-pod-face 'font-lock-comment-face
83261a2f 446 "*Face for POD highlighting."
ccc3ce39 447 :type 'face
db133cb6 448 :group 'cperl-faces)
f83d2997 449
ccc3ce39 450(defcustom cperl-pod-head-face 'font-lock-variable-name-face
83261a2f 451 "*Face for POD highlighting.
ccc3ce39
SE
452Font for POD headers."
453 :type 'face
db133cb6 454 :group 'cperl-faces)
f83d2997 455
ccc3ce39 456(defcustom cperl-here-face 'font-lock-string-face
80585273 457 "*Face for here-docs highlighting."
ccc3ce39 458 :type 'face
db133cb6 459 :group 'cperl-faces)
f83d2997 460
4ab89e7b 461;;; Some double-evaluation happened with font-locks... Needed with 21.2...
6546555e 462(defvar cperl-singly-quote-face (featurep 'xemacs))
4ab89e7b 463
224ca9c9
CY
464(defcustom cperl-invalid-face 'underline
465 "*Face for highlighting trailing whitespace."
80585273 466 :type 'face
ac6857fb 467 :version "21.1"
5bd52f0e
RS
468 :group 'cperl-faces)
469
ccc3ce39 470(defcustom cperl-pod-here-fontify '(featurep 'font-lock)
83261a2f 471 "*Not-nil after evaluation means to highlight POD and here-docs sections."
ccc3ce39 472 :type 'boolean
db133cb6 473 :group 'cperl-faces)
f83d2997 474
5bd52f0e
RS
475(defcustom cperl-fontify-m-as-s t
476 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
477 :type 'boolean
478 :group 'cperl-faces)
479
6c389151
SM
480(defcustom cperl-highlight-variables-indiscriminately nil
481 "*Non-nil means perform additional highlighting on variables.
482Currently only changes how scalar variables are highlighted.
483Note that that variable is only read at initialization time for
484the variable `cperl-font-lock-keywords-2', so changing it after you've
f94a632a 485entered CPerl mode the first time will have no effect."
6c389151
SM
486 :type 'boolean
487 :group 'cperl)
488
ccc3ce39 489(defcustom cperl-pod-here-scan t
83261a2f 490 "*Not-nil means look for POD and here-docs sections during startup.
ccc3ce39
SE
491You can always make lookup from menu or using \\[cperl-find-pods-heres]."
492 :type 'boolean
db133cb6 493 :group 'cperl-speed)
f83d2997 494
6c389151
SM
495(defcustom cperl-regexp-scan t
496 "*Not-nil means make marking of regular expression more thorough.
4ab89e7b
SM
497Effective only with `cperl-pod-here-scan'."
498 :type 'boolean
499 :group 'cperl-speed)
500
501(defcustom cperl-hook-after-change t
502 "*Not-nil means install hook to know which regions of buffer are changed.
503May significantly speed up delayed fontification. Changes take effect
504after reload."
6c389151
SM
505 :type 'boolean
506 :group 'cperl-speed)
507
ccc3ce39 508(defcustom cperl-imenu-addback nil
f83d2997 509 "*Not-nil means add backreferences to generated `imenu's.
db133cb6 510May require patched `imenu' and `imenu-go'. Obsolete."
ccc3ce39 511 :type 'boolean
db133cb6 512 :group 'cperl-help-system)
f83d2997 513
ccc3ce39
SE
514(defcustom cperl-max-help-size 66
515 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
516 :type '(choice integer (const nil))
db133cb6 517 :group 'cperl-help-system)
f83d2997 518
ccc3ce39
SE
519(defcustom cperl-shrink-wrap-info-frame t
520 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
521 :type 'boolean
db133cb6 522 :group 'cperl-help-system)
f83d2997 523
ccc3ce39 524(defcustom cperl-info-page "perl"
f83d2997 525 "*Name of the info page containing perl docs.
ccc3ce39
SE
526Older version of this page was called `perl5', newer `perl'."
527 :type 'string
db133cb6 528 :group 'cperl-help-system)
f83d2997 529
5c8b7eaf 530(defcustom cperl-use-syntax-table-text-property
f83d2997 531 (boundp 'parse-sexp-lookup-properties)
ccc3ce39
SE
532 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
533 :type 'boolean
db133cb6 534 :group 'cperl-speed)
f83d2997 535
5c8b7eaf 536(defcustom cperl-use-syntax-table-text-property-for-tags
f83d2997 537 cperl-use-syntax-table-text-property
ccc3ce39
SE
538 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
539 :type 'boolean
db133cb6 540 :group 'cperl-speed)
ccc3ce39
SE
541
542(defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
543 "*Regexp to match files to scan when generating TAGS."
544 :type 'regexp
545 :group 'cperl)
546
8937f01b
RS
547(defcustom cperl-noscan-files-regexp
548 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
ccc3ce39
SE
549 "*Regexp to match files/dirs to skip when generating TAGS."
550 :type 'regexp
551 :group 'cperl)
552
553(defcustom cperl-regexp-indent-step nil
554 "*Indentation used when beautifying regexps.
029cb4d5 555If nil, the value of `cperl-indent-level' will be used."
ccc3ce39 556 :type '(choice integer (const nil))
db133cb6 557 :group 'cperl-indentation-details)
ccc3ce39
SE
558
559(defcustom cperl-indent-left-aligned-comments t
560 "*Non-nil means that the comment starting in leftmost column should indent."
561 :type 'boolean
db133cb6 562 :group 'cperl-indentation-details)
ccc3ce39 563
8f222248 564(defcustom cperl-under-as-char nil
ccc3ce39
SE
565 "*Non-nil means that the _ (underline) should be treated as word char."
566 :type 'boolean
567 :group 'cperl)
1d5963cc 568(make-obsolete-variable 'cperl-under-as-char 'superword-mode "24.4")
f83d2997 569
db133cb6
RS
570(defcustom cperl-extra-perl-args ""
571 "*Extra arguments to use when starting Perl.
572Currently used with `cperl-check-syntax' only."
573 :type 'string
574 :group 'cperl)
575
576(defcustom cperl-message-electric-keyword t
577 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
578 :type 'boolean
579 :group 'cperl-help-system)
580
581(defcustom cperl-indent-region-fix-constructs 1
582 "*Amount of space to insert between `}' and `else' or `elsif'
583in `cperl-indent-region'. Set to nil to leave as is. Values other
584than 1 and nil will probably not work."
585 :type '(choice (const nil) (const 1))
586 :group 'cperl-indentation-details)
587
588(defcustom cperl-break-one-line-blocks-when-indent t
589 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
2022c546 590need to be reformatted into multiline ones when indenting a region."
db133cb6
RS
591 :type 'boolean
592 :group 'cperl-indentation-details)
593
594(defcustom cperl-fix-hanging-brace-when-indent t
595 "*Non-nil means that BLOCK-end `}' may be put on a separate line
5c8b7eaf 596when indenting a region.
db133cb6
RS
597Braces followed by else/elsif/while/until are excepted."
598 :type 'boolean
599 :group 'cperl-indentation-details)
600
601(defcustom cperl-merge-trailing-else t
5c8b7eaf 602 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
db133cb6
RS
603may be merged to be on the same line when indenting a region."
604 :type 'boolean
605 :group 'cperl-indentation-details)
606
6c389151
SM
607(defcustom cperl-indent-parens-as-block nil
608 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
609but for trailing \",\" inside the group, which won't increase indentation.
610One should tune up `cperl-close-paren-offset' as well."
611 :type 'boolean
612 :group 'cperl-indentation-details)
613
a1506d29 614(defcustom cperl-syntaxify-by-font-lock
83261a2f 615 (and cperl-can-font-lock
5bd52f0e 616 (boundp 'parse-sexp-lookup-properties))
3af98a7b 617 "*Non-nil means that CPerl uses the `font-lock' routines for syntaxification."
5bd52f0e
RS
618 :type '(choice (const message) boolean)
619 :group 'cperl-speed)
620
621(defcustom cperl-syntaxify-unwind
622 t
f94a632a 623 "*Non-nil means that CPerl unwinds to a start of a long construction
5bd52f0e 624when syntaxifying a chunk of buffer."
db133cb6
RS
625 :type 'boolean
626 :group 'cperl-speed)
627
4ab89e7b
SM
628(defcustom cperl-syntaxify-for-menu
629 t
630 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
631This way enabling/disabling of menu items is more correct."
632 :type 'boolean
633 :group 'cperl-speed)
634
5bd52f0e
RS
635(defcustom cperl-ps-print-face-properties
636 '((font-lock-keyword-face nil nil bold shadow)
637 (font-lock-variable-name-face nil nil bold)
638 (font-lock-function-name-face nil nil bold italic box)
639 (font-lock-constant-face nil "LightGray" bold)
4ab89e7b 640 (cperl-array-face nil "LightGray" bold underline)
8c777c8d 641 (cperl-hash-face nil "LightGray" bold italic underline)
5bd52f0e
RS
642 (font-lock-comment-face nil "LightGray" italic)
643 (font-lock-string-face nil nil italic underline)
4ab89e7b 644 (cperl-nonoverridable-face nil nil italic underline)
5bd52f0e 645 (font-lock-type-face nil nil underline)
4ab89e7b 646 (font-lock-warning-face nil "LightGray" bold italic box)
5bd52f0e
RS
647 (underline nil "LightGray" strikeout))
648 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
5c8b7eaf 649 :type '(repeat (cons symbol
5bd52f0e
RS
650 (cons (choice (const nil) string)
651 (cons (choice (const nil) string)
652 (repeat symbol)))))
653 :group 'cperl-faces)
654
5cc679ab
JB
655(defvar cperl-dark-background
656 (cperl-choose-color "navy" "os2blue" "darkgreen"))
657(defvar cperl-dark-foreground
658 (cperl-choose-color "orchid1" "orange"))
659
4ab89e7b 660(defface cperl-nonoverridable-face
5cc679ab
JB
661 `((((class grayscale) (background light))
662 (:background "Gray90" :slant italic :underline t))
663 (((class grayscale) (background dark))
664 (:foreground "Gray80" :slant italic :underline t :weight bold))
665 (((class color) (background light))
666 (:foreground "chartreuse3"))
667 (((class color) (background dark))
668 (:foreground ,cperl-dark-foreground))
669 (t (:weight bold :underline t)))
c73fce9a 670 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
5cc679ab
JB
671 :group 'cperl-faces)
672
4ab89e7b 673(defface cperl-array-face
5cc679ab
JB
674 `((((class grayscale) (background light))
675 (:background "Gray90" :weight bold))
676 (((class grayscale) (background dark))
677 (:foreground "Gray80" :weight bold))
678 (((class color) (background light))
679 (:foreground "Blue" :background "lightyellow2" :weight bold))
680 (((class color) (background dark))
681 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
682 (t (:weight bold)))
683 "Font Lock mode face used to highlight array names."
684 :group 'cperl-faces)
685
4ab89e7b 686(defface cperl-hash-face
5cc679ab
JB
687 `((((class grayscale) (background light))
688 (:background "Gray90" :weight bold :slant italic))
689 (((class grayscale) (background dark))
690 (:foreground "Gray80" :weight bold :slant italic))
691 (((class color) (background light))
692 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
693 (((class color) (background dark))
694 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
695 (t (:weight bold :slant italic)))
696 "Font Lock mode face used to highlight hash names."
697 :group 'cperl-faces)
5bd52f0e 698
f83d2997
KH
699\f
700
701;;; Short extra-docs.
702
703(defvar cperl-tips 'please-ignore-this-line
83261a2f 704 "Get maybe newer version of this package from
4ab89e7b 705 http://ilyaz.org/software/emacs
db133cb6
RS
706Subdirectory `cperl-mode' may contain yet newer development releases and/or
707patches to related files.
f83d2997 708
5bd52f0e
RS
709For best results apply to an older Emacs the patches from
710 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
83261a2f 711\(this upgrades syntax-parsing abilities of Emacsen v19.34 and
8e3acc66 712v20.2 up to the level of Emacs v20.3 - a must for a good Perl
83261a2f 713mode.) As of beginning of 2003, XEmacs may provide a similar ability.
5bd52f0e 714
f83d2997
KH
715Get support packages choose-color.el (or font-lock-extra.el before
71619.30), imenu-go.el from the same place. \(Look for other files there
717too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
5c8b7eaf 718later you should use choose-color.el *instead* of font-lock-extra.el
f83d2997
KH
719\(and you will not get smart highlighting in C :-().
720
721Note that to enable Compile choices in the menu you need to install
722mode-compile.el.
723
5efe6a56
SM
724If your Emacs does not default to `cperl-mode' on Perl files, and you
725want it to: put the following into your .emacs file:
726
727 (defalias 'perl-mode 'cperl-mode)
728
a1506d29 729Get perl5-info from
4ab89e7b
SM
730 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
731Also, one can generate a newer documentation running `pod2texi' converter
732 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
f83d2997
KH
733
734If you use imenu-go, run imenu on perl5-info buffer (you can do it
5bd52f0e
RS
735from Perl menu). If many files are related, generate TAGS files from
736Tools/Tags submenu in Perl menu.
f83d2997
KH
737
738If some class structure is too complicated, use Tools/Hierarchy-view
029cb4d5 739from Perl menu, or hierarchic view of imenu. The second one uses the
f83d2997 740current buffer only, the first one requires generation of TAGS from
5bd52f0e
RS
741Perl/Tools/Tags menu beforehand.
742
743Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
744
745Switch auto-help on/off with Perl/Tools/Auto-help.
746
747Though with contemporary Emaxen CPerl mode should maintain the correct
748parsing of Perl even when editing, sometimes it may be lost. Fix this by
749
029cb4d5 750 \\[normal-mode]
f83d2997 751
5bd52f0e 752In cases of more severe confusion sometimes it is helpful to do
f83d2997 753
029cb4d5
SM
754 \\[load-library] cperl-mode RET
755 \\[normal-mode]
f83d2997 756
5bd52f0e
RS
757Before reporting (non-)problems look in the problem section of online
758micro-docs on what I know about CPerl problems.")
f83d2997
KH
759
760(defvar cperl-problems 'please-ignore-this-line
f94a632a
RS
761 "Description of problems in CPerl mode.
762Some faces will not be shown on some versions of Emacs unless you
bab27c0c 763install choose-color.el, available from
4ab89e7b 764 http://ilyaz.org/software/emacs
bab27c0c 765
6c389151 766`fill-paragraph' on a comment may leave the point behind the
4ab89e7b
SM
767paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
768to detect it and bulk out).
769
770See documentation of a variable `cperl-problems-old-emaxen' for the
771problems which disappear if you upgrade Emacs to a reasonably new
772version (20.3 for Emacs, and those of 2004 for XEmacs).")
773
774(defvar cperl-problems-old-emaxen 'please-ignore-this-line
775 "Description of problems in CPerl mode specific for older Emacs versions.
6c389151 776
8e3acc66 777Emacs had a _very_ restricted syntax parsing engine until version
5bd52f0e 77820.1. Most problems below are corrected starting from this version of
8e3acc66 779Emacs, and all of them should be fixed in version 20.3. (Or apply
83261a2f
SM
780patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
781this respect (until 2003).
5bd52f0e 782
6c389151
SM
783Note that even with newer Emacsen in some very rare cases the details
784of interaction of `font-lock' and syntaxification may be not cleaned
785up yet. You may get slightly different colors basing on the order of
786fontification and syntaxification. Say, the initial faces is correct,
787but editing the buffer breaks this.
f83d2997 788
db133cb6
RS
789Even with older Emacsen CPerl mode tries to corrects some Emacs
790misunderstandings, however, for efficiency reasons the degree of
791correction is different for different operations. The partially
792corrected problems are: POD sections, here-documents, regexps. The
793operations are: highlighting, indentation, electric keywords, electric
794braces.
f83d2997
KH
795
796This may be confusing, since the regexp s#//#/#\; may be highlighted
797as a comment, but it will be recognized as a regexp by the indentation
83261a2f 798code. Or the opposite case, when a POD section is highlighted, but
f83d2997
KH
799may break the indentation of the following code (though indentation
800should work if the balance of delimiters is not broken by POD).
801
802The main trick (to make $ a \"backslash\") makes constructions like
803${aaa} look like unbalanced braces. The only trick I can think of is
2e8b9c7d 804to insert it as $ {aaa} (valid in perl5, not in perl4).
f83d2997
KH
805
806Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
db133cb6
RS
807as /($|\\s)/. Note that such a transposition is not always possible.
808
5bd52f0e 809The solution is to upgrade your Emacs or patch an older one. Note
8e3acc66 810that Emacs 20.2 has some bugs related to `syntax-table' text
5bd52f0e
RS
811properties. Patches are available on the main CPerl download site,
812and on CPAN.
db133cb6
RS
813
814If these bugs cannot be fixed on your machine (say, you have an inferior
815environment and cannot recompile), you may still disable all the fancy stuff
83261a2f 816via `cperl-use-syntax-table-text-property'.")
f83d2997 817
f83d2997 818(defvar cperl-praise 'please-ignore-this-line
8e3acc66 819 "Advantages of CPerl mode.
f83d2997
KH
820
8210) It uses the newest `syntax-table' property ;-);
822
8231) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
5c8b7eaf 824mode - but the latter number may have improved too in last years) even
5bd52f0e
RS
825with old Emaxen which do not support `syntax-table' property.
826
827When using `syntax-table' property for syntax assist hints, it should
828handle 99.995% of lines correct - or somesuch. It automatically
829updates syntax assist hints when you edit your script.
f83d2997 830
bab27c0c 8312) It is generally believed to be \"the most user-friendly Emacs
f83d2997
KH
832package\" whatever it may mean (I doubt that the people who say similar
833things tried _all_ the rest of Emacs ;-), but this was not a lonely
834voice);
835
8363) Everything is customizable, one-by-one or in a big sweep;
837
549c0a96 8384) It has many easily-accessible \"tools\":
f83d2997
KH
839 a) Can run program, check syntax, start debugger;
840 b) Can lineup vertically \"middles\" of rows, like `=' in
841 a = b;
842 cc = d;
53964682 843 c) Can insert spaces where this improves readability (in one
f83d2997
KH
844 interactive sweep over the buffer);
845 d) Has support for imenu, including:
846 1) Separate unordered list of \"interesting places\";
847 2) Separate TOC of POD sections;
848 3) Separate list of packages;
849 4) Hierarchical view of methods in (sub)packages;
850 5) and functions (by the full name - with package);
851 e) Has an interface to INFO docs for Perl; The interface is
852 very flexible, including shrink-wrapping of
853 documentation buffer/frame;
854 f) Has a builtin list of one-line explanations for perl constructs.
855 g) Can show these explanations if you stay long enough at the
856 corresponding place (or on demand);
857 h) Has an enhanced fontification (using 3 or 4 additional faces
858 comparing to font-lock - basically, different
859 namespaces in Perl have different colors);
860 i) Can construct TAGS basing on its knowledge of Perl syntax,
861 the standard menu has 6 different way to generate
db133cb6 862 TAGS (if \"by directory\", .xs files - with C-language
f83d2997
KH
863 bindings - are included in the scan);
864 j) Can build a hierarchical view of classes (via imenu) basing
865 on generated TAGS file;
866 k) Has electric parentheses, electric newlines, uses Abbrev
867 for electric logical constructs
868 while () {}
869 with different styles of expansion (context sensitive
870 to be not so bothering). Electric parentheses behave
871 \"as they should\" in a presence of a visible region.
872 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
db133cb6
RS
873 m) Can convert from
874 if (A) { B }
875 to
876 B if A;
f83d2997 877
5bd52f0e 878 n) Highlights (by user-choice) either 3-delimiters constructs
6c389151
SM
879 (such as tr/a/b/), or regular expressions and `y/tr';
880 o) Highlights trailing whitespace;
881 p) Is able to manipulate Perl Regular Expressions to ease
882 conversion to a more readable form.
4ab89e7b
SM
883 q) Can ispell POD sections and HERE-DOCs.
884 r) Understands comments and character classes inside regular
885 expressions; can find matching () and [] in a regular expression.
886 s) Allows indentation of //x-style regular expressions;
887 t) Highlights different symbols in regular expressions according
888 to their function; much less problems with backslashitis;
889 u) Allows to find regular expressions which contain interpolated parts.
5bd52f0e 890
f83d2997
KH
8915) The indentation engine was very smart, but most of tricks may be
892not needed anymore with the support for `syntax-table' property. Has
893progress indicator for indentation (with `imenu' loaded).
894
5c8b7eaf 8956) Indent-region improves inline-comments as well; also corrects
db133cb6 896whitespace *inside* the conditional/loop constructs.
f83d2997
KH
897
8987) Fill-paragraph correctly handles multi-line comments;
db133cb6
RS
899
9008) Can switch to different indentation styles by one command, and restore
901the settings present before the switch.
902
5c8b7eaf 9039) When doing indentation of control constructs, may correct
db133cb6 904line-breaks/spacing between elements of the construct.
029cb4d5 905
91af3942 90610) Uses a linear-time algorithm for indentation of regions (on Emaxen with
4ab89e7b
SM
907capable syntax engines).
908
90911) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
910")
db133cb6
RS
911
912(defvar cperl-speed 'please-ignore-this-line
913 "This is an incomplete compendium of what is available in other parts
914of CPerl documentation. (Please inform me if I skept anything.)
915
916There is a perception that CPerl is slower than alternatives. This part
917of documentation is designed to overcome this misconception.
918
919*By default* CPerl tries to enable the most comfortable settings.
920From most points of view, correctly working package is infinitely more
921comfortable than a non-correctly working one, thus by default CPerl
922prefers correctness over speed. Below is the guide how to change
923settings if your preferences are different.
924
925A) Speed of loading the file. When loading file, CPerl may perform a
926scan which indicates places which cannot be parsed by primitive Emacs
927syntax-parsing routines, and marks them up so that either
928
929 A1) CPerl may work around these deficiencies (for big chunks, mostly
930 PODs and HERE-documents), or
3ed8598c 931 A2) On capable Emaxen CPerl will use improved syntax-handling
db133cb6
RS
932 which reads mark-up hints directly.
933
934 The scan in case A2 is much more comprehensive, thus may be slower.
935
936 User can disable syntax-engine-helping scan of A2 by setting
937 `cperl-use-syntax-table-text-property'
938 variable to nil (if it is set to t).
939
940 One can disable the scan altogether (both A1 and A2) by setting
941 `cperl-pod-here-scan'
942 to nil.
943
5c8b7eaf 944B) Speed of editing operations.
db133cb6
RS
945
946 One can add a (minor) speedup to editing operations by setting
947 `cperl-use-syntax-table-text-property'
948 variable to nil (if it is set to t). This will disable
949 syntax-engine-helping scan, thus will make many more Perl
950 constructs be wrongly recognized by CPerl, thus may lead to
951 wrongly matched parentheses, wrong indentation, etc.
5bd52f0e
RS
952
953 One can unset `cperl-syntaxify-unwind'. This might speed up editing
83261a2f 954 of, say, long POD sections.")
f83d2997 955
5bd52f0e
RS
956(defvar cperl-tips-faces 'please-ignore-this-line
957 "CPerl mode uses following faces for highlighting:
958
4ab89e7b
SM
959 `cperl-array-face' Array names
960 `cperl-hash-face' Hash names
8661c643 961 `font-lock-comment-face' Comments, PODs and whatever is considered
bbd240ce 962 syntactically to be not code
8661c643 963 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
5bd52f0e 964 2-arg operators s/y/tr/ or of RExen,
4ab89e7b
SM
965 `font-lock-warning-face' Special-cased m// and s//foo/,
966 `font-lock-function-name-face' _ as a target of a file tests, file tests,
5bd52f0e
RS
967 subroutine names at the moment of definition
968 (except those conflicting with Perl operators),
969 package names (when recognized), format names
8661c643 970 `font-lock-keyword-face' Control flow switch constructs, declarators
4ab89e7b 971 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
8661c643 972 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
5bd52f0e 973 literal parts and the terminator of formats
bbd240ce 974 and whatever is syntactically considered
5bd52f0e 975 as string literals
8661c643
DL
976 `font-lock-type-face' Overridable keywords
977 `font-lock-variable-name-face' Variable declarations, indirect array and
5bd52f0e 978 hash names, POD headers/item names
8c777c8d 979 `cperl-invalid-face' Trailing whitespace
5bd52f0e
RS
980
981Note that in several situations the highlighting tries to inform about
982possible confusion, such as different colors for function names in
983declarations depending on what they (do not) override, or special cases
984m// and s/// which do not do what one would expect them to do.
985
5c8b7eaf 986Help with best setup of these faces for printout requested (for each of
5bd52f0e
RS
987the faces: please specify bold, italic, underline, shadow and box.)
988
8c777c8d 989In regular expressions (including character classes):
4ab89e7b
SM
990 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
991 `font-lock-constant-face': Delimiters
992 `font-lock-warning-face' Special-cased m// and s//foo/,
993 Mismatched closing delimiters, parens
994 we couldn't match, misplaced quantifiers,
995 unrecognized escape sequences
996 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
8c777c8d 997 `font-lock-type-face' escape sequences with arguments (\\x \\23 \\p \\N)
4ab89e7b
SM
998 and others match-a-char escape sequences
999 `font-lock-keyword-face' Capturing parens, and |
1000 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
8c777c8d
CY
1001 \"Range -\" in character classes
1002 `font-lock-builtin-face' \"Remaining\" 0-length constructs, multipliers
1003 ?+*{}, not-capturing parens, leading
1004 backslashes of escape sequences
1005 `font-lock-variable-name-face' Interpolated constructs, embedded code,
1006 POSIX classes (inside charclasses)
4ab89e7b
SM
1007 `font-lock-comment-face' Embedded comments
1008
1009")
5bd52f0e 1010
f83d2997
KH
1011\f
1012
1013;;; Portability stuff:
1014
1015(defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
b787fc05
GM
1016 `(define-key cperl-mode-map
1017 ,(if xemacs-key
6546555e 1018 `(if (featurep 'xemacs) ,xemacs-key ,emacs-key)
b787fc05
GM
1019 emacs-key)
1020 ,definition))
f83d2997
KH
1021
1022(defvar cperl-del-back-ch
1023 (car (append (where-is-internal 'delete-backward-char)
1024 (where-is-internal 'backward-delete-char-untabify)))
029cb4d5 1025 "Character generated by key bound to `delete-backward-char'.")
f83d2997 1026
5c8b7eaf 1027(and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
f83d2997
KH
1028 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1029
db133cb6 1030(defun cperl-mark-active () (mark)) ; Avoid undefined warning
6546555e 1031(if (featurep 'xemacs)
f83d2997
KH
1032 (progn
1033 ;; "Active regions" are on: use region only if active
1034 ;; "Active regions" are off: use region unconditionally
1035 (defun cperl-use-region-p ()
db133cb6 1036 (if zmacs-regions (mark) t)))
f83d2997
KH
1037 (defun cperl-use-region-p ()
1038 (if transient-mark-mode mark-active t))
1039 (defun cperl-mark-active () mark-active))
1040
1041(defsubst cperl-enable-font-lock ()
83261a2f 1042 cperl-can-font-lock)
f83d2997 1043
83261a2f 1044(defun cperl-putback-char (c) ; Emacs 19
db133cb6
RS
1045 (set 'unread-command-events (list c))) ; Avoid undefined warning
1046
6546555e 1047(if (featurep 'xemacs)
0ce7de92
RS
1048 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1049 (setq unread-command-events (list (eval '(character-to-event c))))))
f83d2997
KH
1050
1051(or (fboundp 'uncomment-region)
1052 (defun uncomment-region (beg end)
1053 (interactive "r")
1054 (comment-region beg end -1)))
1055
1056(defvar cperl-do-not-fontify
1057 (if (string< emacs-version "19.30")
1058 'fontified
1059 'lazy-lock)
1060 "Text property which inhibits refontification.")
1061
5bd52f0e
RS
1062(defsubst cperl-put-do-not-fontify (from to &optional post)
1063 ;; If POST, do not do it with postponed fontification
1064 (if (and post cperl-syntaxify-by-font-lock)
1065 nil
8c777c8d 1066 (put-text-property (max (point-min) (1- from))
5bd52f0e 1067 to cperl-do-not-fontify t)))
f83d2997 1068
ccc3ce39 1069(defcustom cperl-mode-hook nil
f94a632a 1070 "Hook run by CPerl mode."
ccc3ce39
SE
1071 :type 'hook
1072 :group 'cperl)
f83d2997 1073
db133cb6
RS
1074(defvar cperl-syntax-state nil)
1075(defvar cperl-syntax-done-to nil)
5bd52f0e 1076(defvar cperl-emacs-can-parse (> (length (save-excursion
ce22dd53 1077 (parse-partial-sexp (point) (point)))) 9))
db133cb6
RS
1078\f
1079;; Make customization possible "in reverse"
1080(defsubst cperl-val (symbol &optional default hairy)
1081 (cond
1082 ((eq (symbol-value symbol) 'null) default)
1083 (cperl-hairy (or hairy t))
1084 (t (symbol-value symbol))))
f83d2997 1085\f
4ab89e7b
SM
1086
1087(defun cperl-make-indent (column &optional minimum keep)
1088 "Makes indent of the current line the requested amount.
1089Unless KEEP, removes the old indentation. Works around a bug in ancient
1090versions of Emacs."
1091 (let ((prop (get-text-property (point) 'syntax-type)))
1092 (or keep
1093 (delete-horizontal-space))
1094 (indent-to column minimum)
1095 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1096 (and prop
1097 (> (current-column) 0)
1098 (save-excursion
1099 (beginning-of-line)
1100 (or (get-text-property (point) 'syntax-type)
1101 (and (looking-at "\\=[ \t]")
1102 (put-text-property (point) (match-end 0)
1103 'syntax-type prop)))))))
1104
f83d2997
KH
1105;;; Probably it is too late to set these guys already, but it can help later:
1106
5bd52f0e 1107;;;(and cperl-clobber-mode-lists
f83d2997
KH
1108;;;(setq auto-mode-alist
1109;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1110;;;(and (boundp 'interpreter-mode-alist)
1111;;; (setq interpreter-mode-alist (append interpreter-mode-alist
5bd52f0e 1112;;; '(("miniperl" . perl-mode))))))
80585273 1113(eval-when-compile
dba01120
GM
1114 (mapc (lambda (p)
1115 (condition-case nil
1116 (require p)
1117 (error nil)))
1118 '(imenu easymenu etags timer man info))
80585273
DL
1119 (if (fboundp 'ps-extend-face-list)
1120 (defmacro cperl-ps-extend-face-list (arg)
1121 `(ps-extend-face-list ,arg))
1122 (defmacro cperl-ps-extend-face-list (arg)
e8af40ee 1123 `(error "This version of Emacs has no `ps-extend-face-list'")))
80585273
DL
1124 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1125 ;; macros instead of defsubsts don't work on Emacs, so we do the
1126 ;; expansion manually. Any other suggestions?
1127 (require 'cl))
f83d2997
KH
1128
1129(defvar cperl-mode-abbrev-table nil
f94a632a 1130 "Abbrev table in use in CPerl mode buffers.")
f83d2997
KH
1131
1132(add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1133
1134(defvar cperl-mode-map () "Keymap used in CPerl mode.")
1135
1136(if cperl-mode-map nil
1137 (setq cperl-mode-map (make-sparse-keymap))
1138 (cperl-define-key "{" 'cperl-electric-lbrace)
1139 (cperl-define-key "[" 'cperl-electric-paren)
1140 (cperl-define-key "(" 'cperl-electric-paren)
1141 (cperl-define-key "<" 'cperl-electric-paren)
1142 (cperl-define-key "}" 'cperl-electric-brace)
1143 (cperl-define-key "]" 'cperl-electric-rparen)
1144 (cperl-define-key ")" 'cperl-electric-rparen)
1145 (cperl-define-key ";" 'cperl-electric-semi)
1146 (cperl-define-key ":" 'cperl-electric-terminator)
1147 (cperl-define-key "\C-j" 'newline-and-indent)
1148 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
db133cb6 1149 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
f83d2997
KH
1150 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1151 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
db133cb6
RS
1152 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1153 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
f83d2997 1154 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
4ab89e7b
SM
1155 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1156 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1157 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1158 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1159 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1160 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1161 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
db133cb6 1162 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
4ab89e7b
SM
1163 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1164 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
f83d2997
KH
1165 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1166 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1167 [(control meta |)])
1168 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1169 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1170 (cperl-define-key "\177" 'cperl-electric-backspace)
1171 (cperl-define-key "\t" 'cperl-indent-command)
1172 ;; don't clobber the backspace binding:
db133cb6
RS
1173 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1174 [(control c) (control h) F])
db133cb6
RS
1175 (if (cperl-val 'cperl-clobber-lisp-bindings)
1176 (progn
1177 (cperl-define-key "\C-hf"
1178 ;;(concat (char-to-string help-char) "f") ; does not work
1179 'cperl-info-on-command
1180 [(control h) f])
1181 (cperl-define-key "\C-hv"
1182 ;;(concat (char-to-string help-char) "v") ; does not work
1183 'cperl-get-help
5bd52f0e
RS
1184 [(control h) v])
1185 (cperl-define-key "\C-c\C-hf"
1186 ;;(concat (char-to-string help-char) "f") ; does not work
1187 (key-binding "\C-hf")
1188 [(control c) (control h) f])
1189 (cperl-define-key "\C-c\C-hv"
1190 ;;(concat (char-to-string help-char) "v") ; does not work
1191 (key-binding "\C-hv")
1192 [(control c) (control h) v]))
1193 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1194 [(control c) (control h) f])
1195 (cperl-define-key "\C-c\C-hv"
1196 ;;(concat (char-to-string help-char) "v") ; does not work
1197 'cperl-get-help
1198 [(control c) (control h) v]))
6546555e 1199 (if (and (featurep 'xemacs)
f83d2997
KH
1200 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1201 (progn
1202 ;; substitute-key-definition is usefulness-deenhanced...
4ab89e7b 1203 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
f83d2997
KH
1204 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1205 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
4ab89e7b
SM
1206 (or (boundp 'fill-paragraph-function)
1207 (substitute-key-definition
1208 'fill-paragraph 'cperl-fill-paragraph
1209 cperl-mode-map global-map))
f83d2997
KH
1210 (substitute-key-definition
1211 'indent-sexp 'cperl-indent-exp
1212 cperl-mode-map global-map)
f83d2997
KH
1213 (substitute-key-definition
1214 'indent-region 'cperl-indent-region
1215 cperl-mode-map global-map)
1216 (substitute-key-definition
1217 'indent-for-comment 'cperl-indent-for-comment
1218 cperl-mode-map global-map)))
1219
1220(defvar cperl-menu)
db133cb6
RS
1221(defvar cperl-lazy-installed)
1222(defvar cperl-old-style nil)
f83d2997
KH
1223(condition-case nil
1224 (progn
1225 (require 'easymenu)
83261a2f 1226 (easy-menu-define
4ab89e7b
SM
1227 cperl-menu cperl-mode-map "Menu for CPerl mode"
1228 '("Perl"
1229 ["Beginning of function" beginning-of-defun t]
1230 ["End of function" end-of-defun t]
1231 ["Mark function" mark-defun t]
1232 ["Indent expression" cperl-indent-exp t]
82eb0dae 1233 ["Fill paragraph/comment" fill-paragraph t]
4ab89e7b
SM
1234 "----"
1235 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1236 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1237 ("Regexp"
1238 ["Beautify" cperl-beautify-regexp
1239 cperl-use-syntax-table-text-property]
1240 ["Beautify one level deep" (cperl-beautify-regexp 1)
1241 cperl-use-syntax-table-text-property]
1242 ["Beautify a group" cperl-beautify-level
1243 cperl-use-syntax-table-text-property]
1244 ["Beautify a group one level deep" (cperl-beautify-level 1)
1245 cperl-use-syntax-table-text-property]
1246 ["Contract a group" cperl-contract-level
1247 cperl-use-syntax-table-text-property]
1248 ["Contract groups" cperl-contract-levels
1249 cperl-use-syntax-table-text-property]
83261a2f 1250 "----"
cb5bf6ba 1251 ["Find next interpolated" cperl-next-interpolated-REx
4ab89e7b
SM
1252 (next-single-property-change (point-min) 'REx-interpolated)]
1253 ["Find next interpolated (no //o)"
1254 cperl-next-interpolated-REx-0
1255 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1256 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1257 ["Find next interpolated (neither //o nor whole-REx)"
1258 cperl-next-interpolated-REx-1
1259 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1260 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1261 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1262 "----"
1263 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1264 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1265 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1266 "----"
1267 ["Run" mode-compile (fboundp 'mode-compile)]
1268 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1269 (get-buffer "*compilation*"))]
1270 ["Next error" next-error (get-buffer "*compilation*")]
1271 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1272 "----"
1273 ["Debugger" cperl-db t]
1274 "----"
1275 ("Tools"
1276 ["Imenu" imenu (fboundp 'imenu)]
1277 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
83261a2f 1278 "----"
4ab89e7b
SM
1279 ["Ispell PODs" cperl-pod-spell
1280 ;; Better not to update syntaxification here:
40ba43b4 1281 ;; debugging syntaxification can be broken by this???
4ab89e7b
SM
1282 (or
1283 (get-text-property (point-min) 'in-pod)
1284 (< (progn
1285 (and cperl-syntaxify-for-menu
1286 (cperl-update-syntaxification (point-max) (point-max)))
1287 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1288 (point-max)))]
1289 ["Ispell HERE-DOCs" cperl-here-doc-spell
1290 (< (progn
1291 (and cperl-syntaxify-for-menu
1292 (cperl-update-syntaxification (point-max) (point-max)))
1293 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1294 (point-max))]
1295 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1296 (eq 'here-doc (progn
1297 (and cperl-syntaxify-for-menu
1298 (cperl-update-syntaxification (point) (point)))
1299 (get-text-property (point) 'syntax-type)))]
1300 ["Select this HERE-DOC or POD section"
1301 cperl-select-this-pod-or-here-doc
1302 (memq (progn
1303 (and cperl-syntaxify-for-menu
1304 (cperl-update-syntaxification (point) (point)))
1305 (get-text-property (point) 'syntax-type))
1306 '(here-doc pod))]
83261a2f 1307 "----"
4c36be58 1308 ["CPerl pretty print (experimental)" cperl-ps-print
4ab89e7b 1309 (fboundp 'ps-extend-face-list)]
83261a2f 1310 "----"
4ab89e7b
SM
1311 ["Syntaxify region" cperl-find-pods-heres-region
1312 (cperl-use-region-p)]
1313 ["Profile syntaxification" cperl-time-fontification t]
1314 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1315 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1316 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1317 (cperl-toggle-set-debug-unwind nil t) t]
83261a2f 1318 "----"
4ab89e7b
SM
1319 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1320 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1321 ("Tags"
f83d2997
KH
1322;;; ["Create tags for current file" cperl-etags t]
1323;;; ["Add tags for current file" (cperl-etags t) t]
1324;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1325;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
5c8b7eaf 1326;;; ["Create tags for Perl files in (sub)directories"
f83d2997
KH
1327;;; (cperl-etags nil 'recursive) t]
1328;;; ["Add tags for Perl files in (sub)directories"
5c8b7eaf 1329;;; (cperl-etags t 'recursive) t])
f83d2997 1330;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
f739b53b
SM
1331 ["Create tags for current file" (cperl-write-tags nil t) t]
1332 ["Add tags for current file" (cperl-write-tags) t]
1333 ["Create tags for Perl files in directory"
1334 (cperl-write-tags nil t nil t) t]
1335 ["Add tags for Perl files in directory"
1336 (cperl-write-tags nil nil nil t) t]
1337 ["Create tags for Perl files in (sub)directories"
1338 (cperl-write-tags nil t t t) t]
1339 ["Add tags for Perl files in (sub)directories"
1340 (cperl-write-tags nil nil t t) t]))
1341 ("Perl docs"
15ca5699 1342 ["Define word at point" imenu-go-find-at-position
f739b53b
SM
1343 (fboundp 'imenu-go-find-at-position)]
1344 ["Help on function" cperl-info-on-command t]
1345 ["Help on function at point" cperl-info-on-current-command t]
1346 ["Help on symbol at point" cperl-get-help t]
1347 ["Perldoc" cperl-perldoc t]
1348 ["Perldoc on word at point" cperl-perldoc-at-point t]
1349 ["View manpage of POD in this file" cperl-build-manpage t]
15ca5699 1350 ["Auto-help on" cperl-lazy-install
f739b53b
SM
1351 (and (fboundp 'run-with-idle-timer)
1352 (not cperl-lazy-installed))]
1353 ["Auto-help off" cperl-lazy-unstall
1354 (and (fboundp 'run-with-idle-timer)
1355 cperl-lazy-installed)])
1356 ("Toggle..."
1357 ["Auto newline" cperl-toggle-auto-newline t]
1358 ["Electric parens" cperl-toggle-electric t]
1359 ["Electric keywords" cperl-toggle-abbrev t]
1360 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1361 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
15ca5699 1362 ["Auto fill" auto-fill-mode t])
f739b53b
SM
1363 ("Indent styles..."
1364 ["CPerl" (cperl-set-style "CPerl") t]
1365 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1366 ["GNU" (cperl-set-style "GNU") t]
1367 ["C++" (cperl-set-style "C++") t]
4ab89e7b 1368 ["K&R" (cperl-set-style "K&R") t]
f739b53b
SM
1369 ["BSD" (cperl-set-style "BSD") t]
1370 ["Whitesmith" (cperl-set-style "Whitesmith") t]
4ab89e7b 1371 ["Memorize Current" (cperl-set-style "Current") t]
f739b53b
SM
1372 ["Memorized" (cperl-set-style-back) cperl-old-style])
1373 ("Micro-docs"
1374 ["Tips" (describe-variable 'cperl-tips) t]
1375 ["Problems" (describe-variable 'cperl-problems) t]
1376 ["Speed" (describe-variable 'cperl-speed) t]
1377 ["Praise" (describe-variable 'cperl-praise) t]
1378 ["Faces" (describe-variable 'cperl-tips-faces) t]
1379 ["CPerl mode" (describe-function 'cperl-mode) t]
1380 ["CPerl version"
1381 (message "The version of master-file for this CPerl is %s-Emacs"
1382 cperl-version) t]))))
f83d2997
KH
1383 (error nil))
1384
1385(autoload 'c-macro-expand "cmacexp"
1386 "Display the result of expanding all C macros occurring in the region.
1387The expansion is entirely correct because it uses the C preprocessor."
1388 t)
1389
4ab89e7b
SM
1390;;; These two must be unwound, otherwise take exponential time
1391(defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
e4769531 1392"Regular expression to match optional whitespace with interspersed comments.
4ab89e7b
SM
1393Should contain exactly one group.")
1394
1395;;; This one is tricky to unwind; still very inefficient...
1396(defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
e4769531 1397"Regular expression to match whitespace with interspersed comments.
4ab89e7b
SM
1398Should contain exactly one group.")
1399
1400
1401;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1402;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1403;;; Details of groups in this may be used in several functions; see comments
1404;;; near mentioned above variable(s)...
1405;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1406(defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1407 "Match the text after `sub' in a subroutine declaration.
1408If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1409of attributes (if present), or end of the name or prototype (whatever is
1410the last)."
1411 (concat ; Assume n groups before this...
1412 "\\(" ; n+1=name-group
1413 cperl-white-and-comment-rex ; n+2=pre-name
1414 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1415 "\\)" ; END n+1=name-group
1416 (if named "" "?")
1417 "\\(" ; n+4=proto-group
1418 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1419 "\\(([^()]*)\\)" ; n+6=prototype
1420 "\\)?" ; END n+4=proto-group
1421 "\\(" ; n+7=attr-group
1422 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1423 "\\(" ; n+9=start-attr
1424 ":"
1425 (if attr (concat
1426 "\\("
1427 cperl-maybe-white-and-comment-rex ; whitespace-comments
1428 "\\(\\sw\\|_\\)+" ; attr-name
1429 ;; attr-arg (1 level of internal parens allowed!)
1430 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1431 "\\(" ; optional : (XXX allows trailing???)
1432 cperl-maybe-white-and-comment-rex ; whitespace-comments
1433 ":\\)?"
1434 "\\)+")
1435 "[^:]")
1436 "\\)"
1437 "\\)?" ; END n+6=proto-group
1438 ))
1439
1440;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1441;;; and `cperl-outline-level'.
1442;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
95f433f4
RS
1443(defvar cperl-imenu--function-name-regexp-perl
1444 (concat
4ab89e7b
SM
1445 "^\\(" ; 1 = all
1446 "\\([ \t]*package" ; 2 = package-group
1447 "\\(" ; 3 = package-name-group
1448 cperl-white-and-comment-rex ; 4 = pre-package-name
1449 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1450 "\\|"
1451 "[ \t]*sub"
1452 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1453 cperl-maybe-white-and-comment-rex ; 15=pre-block
1454 "\\|"
1455 "=head\\([1-4]\\)[ \t]+" ; 16=level
1456 "\\([^\n]+\\)$" ; 17=text
95f433f4
RS
1457 "\\)"))
1458
8dd511f6
RS
1459(defvar cperl-outline-regexp
1460 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1461
f83d2997 1462(defvar cperl-mode-syntax-table nil
f94a632a 1463 "Syntax table in use in CPerl mode buffers.")
f83d2997
KH
1464
1465(defvar cperl-string-syntax-table nil
f94a632a 1466 "Syntax table in use in CPerl mode string-like chunks.")
f83d2997 1467
4ab89e7b
SM
1468(defsubst cperl-1- (p)
1469 (max (point-min) (1- p)))
1470
1471(defsubst cperl-1+ (p)
1472 (min (point-max) (1+ p)))
1473
f83d2997
KH
1474(if cperl-mode-syntax-table
1475 ()
1476 (setq cperl-mode-syntax-table (make-syntax-table))
1477 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1478 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1479 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1480 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1481 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1482 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1483 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1484 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1485 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1486 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1487 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1488 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1489 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1490 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1491 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1492 (if cperl-under-as-char
1493 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1494 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1495 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1496 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1497 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
4ab89e7b
SM
1498 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1499 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
8c777c8d
CY
1500 (modify-syntax-entry ?\" "." cperl-string-syntax-table)
1501 (modify-syntax-entry ?' "." cperl-string-syntax-table)
1502 (modify-syntax-entry ?` "." cperl-string-syntax-table)
83261a2f 1503 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
f83d2997
KH
1504
1505
1506\f
db133cb6 1507(defvar cperl-faces-init nil)
f83d2997
KH
1508;; Fix for msb.el
1509(defvar cperl-msb-fixed nil)
83261a2f 1510(defvar cperl-use-major-mode 'cperl-mode)
4ab89e7b
SM
1511(defvar cperl-font-lock-multiline-start nil)
1512(defvar cperl-font-lock-multiline nil)
4ab89e7b 1513(defvar cperl-font-locking nil)
83261a2f 1514
e9bfd3a3 1515;; NB as it stands the code in cperl-mode assumes this only has one
0d26e0b6 1516;; element. If XEmacs 19 support were dropped, this could all be simplified.
e9bfd3a3
GM
1517(defvar cperl-compilation-error-regexp-alist
1518 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
1519 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
1520 2 3))
1521 "Alist that specifies how to match errors in perl output.")
1522
73e72da4
DN
1523(defvar compilation-error-regexp-alist)
1524
f83d2997 1525;;;###autoload
5fdd4046 1526(define-derived-mode cperl-mode prog-mode "CPerl"
f83d2997
KH
1527 "Major mode for editing Perl code.
1528Expression and list commands understand all C brackets.
1529Tab indents for Perl code.
1530Paragraphs are separated by blank lines only.
1531Delete converts tabs to spaces as it moves back.
1532
1533Various characters in Perl almost always come in pairs: {}, (), [],
1534sometimes <>. When the user types the first, she gets the second as
1535well, with optional special formatting done on {}. (Disabled by
1536default.) You can always quote (with \\[quoted-insert]) the left
1537\"paren\" to avoid the expansion. The processing of < is special,
f94a632a 1538since most the time you mean \"less\". CPerl mode tries to guess
f83d2997
KH
1539whether you want to type pair <>, and inserts is if it
1540appropriate. You can set `cperl-electric-parens-string' to the string that
bbd240ce
PE
1541contains the parens from the above list you want to be electrical.
1542Electricity of parens is controlled by `cperl-electric-parens'.
f83d2997
KH
1543You may also set `cperl-electric-parens-mark' to have electric parens
1544look for active mark and \"embrace\" a region if possible.'
1545
1546CPerl mode provides expansion of the Perl control constructs:
db133cb6 1547
5c8b7eaf 1548 if, else, elsif, unless, while, until, continue, do,
db133cb6
RS
1549 for, foreach, formy and foreachmy.
1550
1551and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1552
1553The user types the keyword immediately followed by a space, which
1554causes the construct to be expanded, and the point is positioned where
65e7ca35 1555she is most likely to want to be. E.g., when the user types a space
db133cb6
RS
1556following \"if\" the following appears in the buffer: if () { or if ()
1557} { } and the cursor is between the parentheses. The user can then
1558type some boolean expression within the parens. Having done that,
1559typing \\[cperl-linefeed] places you - appropriately indented - on a
1560new line between the braces (if you typed \\[cperl-linefeed] in a POD
5c8b7eaf 1561directive line, then appropriate number of new lines is inserted).
db133cb6
RS
1562
1563If CPerl decides that you want to insert \"English\" style construct like
1564
f83d2997 1565 bite if angry;
db133cb6
RS
1566
1567it will not do any expansion. See also help on variable
1568`cperl-extra-newline-before-brace'. (Note that one can switch the
1569help message on expansion by setting `cperl-message-electric-keyword'
1570to nil.)
f83d2997
KH
1571
1572\\[cperl-linefeed] is a convenience replacement for typing carriage
1573return. It places you in the next line with proper indentation, or if
1574you type it inside the inline block of control construct, like
db133cb6 1575
f83d2997 1576 foreach (@lines) {print; print}
db133cb6 1577
f83d2997
KH
1578and you are on a boundary of a statement inside braces, it will
1579transform the construct into a multiline and will place you into an
5c8b7eaf 1580appropriately indented blank line. If you need a usual
6292d528 1581`newline-and-indent' behavior, it is on \\[newline-and-indent],
f83d2997
KH
1582see documentation on `cperl-electric-linefeed'.
1583
db133cb6
RS
1584Use \\[cperl-invert-if-unless] to change a construction of the form
1585
1586 if (A) { B }
1587
1588into
1589
1590 B if A;
1591
f83d2997
KH
1592\\{cperl-mode-map}
1593
db133cb6
RS
1594Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1595\(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1596on electric space between $ and {, `cperl-electric-parens-string' is
1597the string that contains parentheses that should be electric in CPerl
1598\(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
f83d2997
KH
1599setting `cperl-electric-keywords' enables electric expansion of
1600control structures in CPerl. `cperl-electric-linefeed' governs which
1601one of two linefeed behavior is preferable. You can enable all these
1602options simultaneously (recommended mode of use) by setting
1603`cperl-hairy' to t. In this case you can switch separate options off
db133cb6
RS
1604by setting them to `null'. Note that one may undo the extra
1605whitespace inserted by semis and braces in `auto-newline'-mode by
1606consequent \\[cperl-electric-backspace].
f83d2997
KH
1607
1608If your site has perl5 documentation in info format, you can use commands
1609\\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1610These keys run commands `cperl-info-on-current-command' and
1611`cperl-info-on-command', which one is which is controlled by variable
5c8b7eaf 1612`cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
db133cb6 1613\(in turn affected by `cperl-hairy').
f83d2997
KH
1614
1615Even if you have no info-format documentation, short one-liner-style
db133cb6
RS
1616help is available on \\[cperl-get-help], and one can run perldoc or
1617man via menu.
f83d2997 1618
db133cb6
RS
1619It is possible to show this help automatically after some idle time.
1620This is regulated by variable `cperl-lazy-help-time'. Default with
1621`cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1622secs idle time . It is also possible to switch this on/off from the
1623menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
f83d2997
KH
1624
1625Use \\[cperl-lineup] to vertically lineup some construction - put the
1626beginning of the region at the start of construction, and make region
1627span the needed amount of lines.
1628
1629Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
83261a2f 1630`cperl-pod-face', `cperl-pod-head-face' control processing of POD and
db133cb6
RS
1631here-docs sections. With capable Emaxen results of scan are used
1632for indentation too, otherwise they are used for highlighting only.
f83d2997
KH
1633
1634Variables controlling indentation style:
1635 `cperl-tab-always-indent'
1636 Non-nil means TAB in CPerl mode should always reindent the current line,
1637 regardless of where in the line point is when the TAB command is used.
db133cb6
RS
1638 `cperl-indent-left-aligned-comments'
1639 Non-nil means that the comment starting in leftmost column should indent.
f83d2997
KH
1640 `cperl-auto-newline'
1641 Non-nil means automatically newline before and after braces,
1642 and after colons and semicolons, inserted in Perl code. The following
1643 \\[cperl-electric-backspace] will remove the inserted whitespace.
5c8b7eaf
SS
1644 Insertion after colons requires both this variable and
1645 `cperl-auto-newline-after-colon' set.
f83d2997
KH
1646 `cperl-auto-newline-after-colon'
1647 Non-nil means automatically newline even after colons.
1648 Subject to `cperl-auto-newline' setting.
1649 `cperl-indent-level'
1650 Indentation of Perl statements within surrounding block.
1651 The surrounding block's indentation is the indentation
1652 of the line on which the open-brace appears.
1653 `cperl-continued-statement-offset'
1654 Extra indentation given to a substatement, such as the
1655 then-clause of an if, or body of a while, or just a statement continuation.
1656 `cperl-continued-brace-offset'
1657 Extra indentation given to a brace that starts a substatement.
1658 This is in addition to `cperl-continued-statement-offset'.
1659 `cperl-brace-offset'
1660 Extra indentation for line if it starts with an open brace.
1661 `cperl-brace-imaginary-offset'
1662 An open brace following other text is treated as if it the line started
1663 this far to the right of the actual line indentation.
1664 `cperl-label-offset'
1665 Extra indentation for line that is a label.
1666 `cperl-min-label-indent'
1667 Minimal indentation for line that is a label.
1668
4ab89e7b
SM
1669Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1670 `cperl-indent-level' 5 4 2 4
1671 `cperl-brace-offset' 0 0 0 0
1672 `cperl-continued-brace-offset' -5 -4 0 0
1673 `cperl-label-offset' -5 -4 -2 -4
1674 `cperl-continued-statement-offset' 5 4 2 4
f83d2997 1675
db133cb6
RS
1676CPerl knows several indentation styles, and may bulk set the
1677corresponding variables. Use \\[cperl-set-style] to do this. Use
1678\\[cperl-set-style-back] to restore the memorized preexisting values
4ab89e7b
SM
1679\(both available from menu). See examples in `cperl-style-examples'.
1680
1681Part of the indentation style is how different parts of if/elsif/else
1682statements are broken into lines; in CPerl, this is reflected on how
1683templates for these constructs are created (controlled by
8c777c8d
CY
1684`cperl-extra-newline-before-brace'), and how reflow-logic should treat
1685\"continuation\" blocks of else/elsif/continue, controlled by the same
1686variable, and by `cperl-extra-newline-before-brace-multiline',
4ab89e7b 1687`cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
db133cb6
RS
1688
1689If `cperl-indent-level' is 0, the statement after opening brace in
5c8b7eaf 1690column 0 is indented on
db133cb6 1691`cperl-brace-offset'+`cperl-continued-statement-offset'.
f83d2997
KH
1692
1693Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
db133cb6
RS
1694with no args.
1695
1696DO NOT FORGET to read micro-docs (available from `Perl' menu)
1697or as help on variables `cperl-tips', `cperl-problems',
f94a632a 1698`cperl-praise', `cperl-speed'."
f83d2997
KH
1699 (if (cperl-val 'cperl-electric-linefeed)
1700 (progn
1701 (local-set-key "\C-J" 'cperl-linefeed)
1702 (local-set-key "\C-C\C-J" 'newline-and-indent)))
db133cb6
RS
1703 (if (and
1704 (cperl-val 'cperl-clobber-lisp-bindings)
1705 (cperl-val 'cperl-info-on-command-no-prompt))
f83d2997
KH
1706 (progn
1707 ;; don't clobber the backspace binding:
1708 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1709 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1710 [(control c) (control h) f])))
449657e8
GM
1711 (let ((prev-a-c abbrevs-changed))
1712 (define-abbrev-table 'cperl-mode-abbrev-table '(
f83d2997
KH
1713 ("if" "if" cperl-electric-keyword 0)
1714 ("elsif" "elsif" cperl-electric-keyword 0)
1715 ("while" "while" cperl-electric-keyword 0)
1716 ("until" "until" cperl-electric-keyword 0)
1717 ("unless" "unless" cperl-electric-keyword 0)
1718 ("else" "else" cperl-electric-else 0)
db133cb6 1719 ("continue" "continue" cperl-electric-else 0)
f83d2997
KH
1720 ("for" "for" cperl-electric-keyword 0)
1721 ("foreach" "foreach" cperl-electric-keyword 0)
db133cb6
RS
1722 ("formy" "formy" cperl-electric-keyword 0)
1723 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1724 ("do" "do" cperl-electric-keyword 0)
6c389151
SM
1725 ("=pod" "=pod" cperl-electric-pod 0)
1726 ("=over" "=over" cperl-electric-pod 0)
1727 ("=head1" "=head1" cperl-electric-pod 0)
1728 ("=head2" "=head2" cperl-electric-pod 0)
db133cb6
RS
1729 ("pod" "pod" cperl-electric-pod 0)
1730 ("over" "over" cperl-electric-pod 0)
1731 ("head1" "head1" cperl-electric-pod 0)
1732 ("head2" "head2" cperl-electric-pod 0)))
449657e8 1733 (setq abbrevs-changed prev-a-c))
f83d2997 1734 (setq local-abbrev-table cperl-mode-abbrev-table)
4ab89e7b
SM
1735 (if (cperl-val 'cperl-electric-keywords)
1736 (abbrev-mode 1))
f83d2997 1737 (set-syntax-table cperl-mode-syntax-table)
4ab89e7b
SM
1738 ;; Until Emacs is multi-threaded, we do not actually need it local:
1739 (make-local-variable 'cperl-font-lock-multiline-start)
1740 (make-local-variable 'cperl-font-locking)
6c389151
SM
1741 (make-local-variable 'outline-regexp)
1742 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1743 (setq outline-regexp cperl-outline-regexp)
1744 (make-local-variable 'outline-level)
1745 (setq outline-level 'cperl-outline-level)
ba03d0d9
CY
1746 (make-local-variable 'add-log-current-defun-function)
1747 (setq add-log-current-defun-function
1748 (lambda ()
9dffb5b6
CY
1749 (save-excursion
1750 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
1751 (match-string-no-properties 1)))))
ba03d0d9 1752
f83d2997
KH
1753 (make-local-variable 'paragraph-start)
1754 (setq paragraph-start (concat "^$\\|" page-delimiter))
1755 (make-local-variable 'paragraph-separate)
1756 (setq paragraph-separate paragraph-start)
1757 (make-local-variable 'paragraph-ignore-fill-prefix)
1758 (setq paragraph-ignore-fill-prefix t)
6546555e 1759 (if (featurep 'xemacs)
4ab89e7b
SM
1760 (progn
1761 (make-local-variable 'paren-backwards-message)
1762 (set 'paren-backwards-message t)))
f83d2997
KH
1763 (make-local-variable 'indent-line-function)
1764 (setq indent-line-function 'cperl-indent-line)
1765 (make-local-variable 'require-final-newline)
7d441781 1766 (setq require-final-newline mode-require-final-newline)
f83d2997
KH
1767 (make-local-variable 'comment-start)
1768 (setq comment-start "# ")
1769 (make-local-variable 'comment-end)
1770 (setq comment-end "")
1771 (make-local-variable 'comment-column)
1772 (setq comment-column cperl-comment-column)
1773 (make-local-variable 'comment-start-skip)
1774 (setq comment-start-skip "#+ *")
1775 (make-local-variable 'defun-prompt-regexp)
4ab89e7b
SM
1776;;; "[ \t]*sub"
1777;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1778;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1779 (setq defun-prompt-regexp
1780 (concat "^[ \t]*\\(sub"
1781 (cperl-after-sub-regexp 'named 'attr-groups)
1782 "\\|" ; per toke.c
1783 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1784 "\\)"
1785 cperl-maybe-white-and-comment-rex))
f83d2997
KH
1786 (make-local-variable 'comment-indent-function)
1787 (setq comment-indent-function 'cperl-comment-indent)
4ab89e7b
SM
1788 (and (boundp 'fill-paragraph-function)
1789 (progn
1790 (make-local-variable 'fill-paragraph-function)
1791 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
f83d2997
KH
1792 (make-local-variable 'parse-sexp-ignore-comments)
1793 (setq parse-sexp-ignore-comments t)
1794 (make-local-variable 'indent-region-function)
1795 (setq indent-region-function 'cperl-indent-region)
1796 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1797 (make-local-variable 'imenu-create-index-function)
1798 (setq imenu-create-index-function
80585273 1799 (function cperl-imenu--create-perl-index))
f83d2997
KH
1800 (make-local-variable 'imenu-sort-function)
1801 (setq imenu-sort-function nil)
e1a5828f
AS
1802 (make-local-variable 'vc-rcs-header)
1803 (set 'vc-rcs-header cperl-vc-rcs-header)
1804 (make-local-variable 'vc-sccs-header)
1805 (set 'vc-sccs-header cperl-vc-sccs-header)
67141a37
GM
1806 (when (featurep 'xemacs)
1807 ;; This one is obsolete...
1808 (make-local-variable 'vc-header-alist)
1809 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1810 `((SCCS ,(car cperl-vc-sccs-header))
1811 (RCS ,(car cperl-vc-rcs-header))))))
4ab89e7b
SM
1812 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1813 (make-local-variable 'compilation-error-regexp-alist-alist)
1814 (set 'compilation-error-regexp-alist-alist
e9bfd3a3 1815 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
4ab89e7b 1816 (symbol-value 'compilation-error-regexp-alist-alist)))
8c777c8d
CY
1817 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1818 (let ((f 'compilation-build-compilation-error-regexp-alist))
1819 (funcall f))
1820 (make-local-variable 'compilation-error-regexp-alist)
1821 (push 'cperl compilation-error-regexp-alist)))
ee7683eb 1822 ((boundp 'compilation-error-regexp-alist);; xemacs 19.x
4ab89e7b
SM
1823 (make-local-variable 'compilation-error-regexp-alist)
1824 (set 'compilation-error-regexp-alist
10715960
RS
1825 (append cperl-compilation-error-regexp-alist
1826 (symbol-value 'compilation-error-regexp-alist)))))
f83d2997
KH
1827 (make-local-variable 'font-lock-defaults)
1828 (setq font-lock-defaults
db133cb6
RS
1829 (cond
1830 ((string< emacs-version "19.30")
4ab89e7b 1831 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
db133cb6 1832 ((string< emacs-version "19.33") ; Which one to use?
5efe6a56
SM
1833 '((cperl-font-lock-keywords
1834 cperl-font-lock-keywords-1
4ab89e7b 1835 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
db133cb6
RS
1836 (t
1837 '((cperl-load-font-lock-keywords
1838 cperl-load-font-lock-keywords-1
4ab89e7b 1839 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
db133cb6 1840 (make-local-variable 'cperl-syntax-state)
4ab89e7b 1841 (setq cperl-syntax-state nil) ; reset syntaxification cache
f83d2997 1842 (if cperl-use-syntax-table-text-property
4813c453 1843 (if (eval-when-compile (fboundp 'syntax-propertize-rules))
cf38dd42
SM
1844 (progn
1845 ;; Reset syntaxification cache.
1846 (set (make-local-variable 'cperl-syntax-done-to) nil)
1847 (set (make-local-variable 'syntax-propertize-function)
1848 (lambda (start end)
638eaeb9
SM
1849 (goto-char start)
1850 ;; Even if cperl-fontify-syntaxically has already gone
1851 ;; beyond `start', syntax-propertize has just removed
1852 ;; syntax-table properties between start and end, so we have
1853 ;; to re-apply them.
1854 (setq cperl-syntax-done-to start)
1855 (cperl-fontify-syntaxically end))))
029cb4d5 1856 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997 1857 ;; Do not introduce variable if not needed, we check it!
db133cb6
RS
1858 (set 'parse-sexp-lookup-properties t)
1859 ;; Fix broken font-lock:
1860 (or (boundp 'font-lock-unfontify-region-function)
1861 (set 'font-lock-unfontify-region-function
83261a2f 1862 'font-lock-default-unfontify-region))
6546555e 1863 (unless (featurep 'xemacs) ; Our: just a plug for wrong font-lock
4ab89e7b
SM
1864 (make-local-variable 'font-lock-unfontify-region-function)
1865 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1866 'cperl-font-lock-unfontify-region-function))
029cb4d5 1867 (make-local-variable 'cperl-syntax-done-to)
4ab89e7b 1868 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
029cb4d5 1869 (make-local-variable 'font-lock-syntactic-keywords)
5c8b7eaf 1870 (setq font-lock-syntactic-keywords
db133cb6 1871 (if cperl-syntaxify-by-font-lock
11b41e6f
SM
1872 '((cperl-fontify-syntaxically))
1873 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1874 ;; used to ignore syntax-table text-properties. (t) is a hack
1875 ;; to make font-lock think that font-lock-syntactic-keywords
1876 ;; are defined.
db133cb6 1877 '(t)))))
4ab89e7b
SM
1878 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1879 (progn
1880 (setq cperl-font-lock-multiline t) ; Not localized...
f453f5a8 1881 (set (make-local-variable 'font-lock-multiline) t))
4ab89e7b
SM
1882 (make-local-variable 'font-lock-fontify-region-function)
1883 (set 'font-lock-fontify-region-function ; not present with old Emacs
1884 'cperl-font-lock-fontify-region-function))
1885 (make-local-variable 'font-lock-fontify-region-function)
1886 (set 'font-lock-fontify-region-function ; not present with old Emacs
1887 'cperl-font-lock-fontify-region-function)
db133cb6 1888 (make-local-variable 'cperl-old-style)
83261a2f
SM
1889 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1890 (set (make-local-variable 'normal-auto-fill-function)
4ab89e7b 1891 'cperl-do-auto-fill)
83261a2f
SM
1892 (or (fboundp 'cperl-old-auto-fill-mode)
1893 (progn
1894 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1895 (defun auto-fill-mode (&optional arg)
1896 (interactive "P")
1897 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1898 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1899 (setq auto-fill-function 'cperl-do-auto-fill))))))
f83d2997 1900 (if (cperl-enable-font-lock)
5c8b7eaf 1901 (if (cperl-val 'cperl-font-lock)
f83d2997
KH
1902 (progn (or cperl-faces-init (cperl-init-faces))
1903 (font-lock-mode 1))))
4ab89e7b
SM
1904 (set (make-local-variable 'facemenu-add-face-function)
1905 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
f83d2997
KH
1906 (and (boundp 'msb-menu-cond)
1907 (not cperl-msb-fixed)
1908 (cperl-msb-fix))
ded62b08 1909 (if (fboundp 'easy-menu-add)
46c72468 1910 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
a3c328ee 1911 (run-mode-hooks 'cperl-mode-hook)
4ab89e7b 1912 (if cperl-hook-after-change
39234e39 1913 (add-hook 'after-change-functions 'cperl-after-change-function nil t))
f83d2997 1914 ;; After hooks since fontification will break this
5c8b7eaf 1915 (if cperl-pod-here-scan
83261a2f 1916 (or cperl-syntaxify-by-font-lock
5bd52f0e
RS
1917 (progn (or cperl-faces-init (cperl-init-faces-weak))
1918 (cperl-find-pods-heres)))))
f83d2997
KH
1919\f
1920;; Fix for perldb - make default reasonable
1921(defun cperl-db ()
1922 (interactive)
1923 (require 'gud)
1924 (perldb (read-from-minibuffer "Run perldb (like this): "
1925 (if (consp gud-perldb-history)
1926 (car gud-perldb-history)
1927 (concat "perl " ;;(file-name-nondirectory
83261a2f
SM
1928 ;; I have problems
1929 ;; in OS/2
1930 ;; otherwise
1931 (buffer-file-name)))
f83d2997
KH
1932 nil nil
1933 '(gud-perldb-history . 1))))
1934\f
f83d2997
KH
1935(defun cperl-msb-fix ()
1936 ;; Adds perl files to msb menu, supposes that msb is already loaded
1937 (setq cperl-msb-fixed t)
1938 (let* ((l (length msb-menu-cond))
1939 (last (nth (1- l) msb-menu-cond))
1940 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1941 (handle (1- (nth 1 last))))
1942 (setcdr precdr (list
1943 (list
996e2616 1944 '(memq major-mode '(cperl-mode perl-mode))
f83d2997
KH
1945 handle
1946 "Perl Files (%d)")
1947 last))))
1948\f
1949;; This is used by indent-for-comment
1950;; to decide how much to indent a comment in CPerl code
1951;; based on its context. Do fallback if comment is found wrong.
1952
1953(defvar cperl-wrong-comment)
5bd52f0e
RS
1954(defvar cperl-st-cfence '(14)) ; Comment-fence
1955(defvar cperl-st-sfence '(15)) ; String-fence
1956(defvar cperl-st-punct '(1))
1957(defvar cperl-st-word '(2))
1958(defvar cperl-st-bra '(4 . ?\>))
1959(defvar cperl-st-ket '(5 . ?\<))
1960
f83d2997 1961
4ab89e7b 1962(defun cperl-comment-indent () ; called at point at supposed comment
5bd52f0e 1963 (let ((p (point)) (c (current-column)) was phony)
4ab89e7b
SM
1964 (if (and (not cperl-indent-comment-at-column-0)
1965 (looking-at "^#"))
1966 0 ; Existing comment at bol stays there.
f83d2997
KH
1967 ;; Wrong comment found
1968 (save-excursion
5bd52f0e
RS
1969 (setq was (cperl-to-comment-or-eol)
1970 phony (eq (get-text-property (point) 'syntax-table)
1971 cperl-st-cfence))
1972 (if phony
4ab89e7b 1973 (progn ; Too naive???
5bd52f0e
RS
1974 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1975 (if (eq (preceding-char) ?\#)
1976 (forward-char -1))
1977 (setq was nil)))
4ab89e7b 1978 (if (= (point) p) ; Our caller found a correct place
f83d2997
KH
1979 (progn
1980 (skip-chars-backward " \t")
4ab89e7b
SM
1981 (setq was (current-column))
1982 (if (eq was 0)
1983 comment-column
1984 (max (1+ was) ; Else indent at comment column
1985 comment-column)))
1986 ;; No, the caller found a random place; we need to edit ourselves
f83d2997
KH
1987 (if was nil
1988 (insert comment-start)
1989 (backward-char (length comment-start)))
1990 (setq cperl-wrong-comment t)
4ab89e7b
SM
1991 (cperl-make-indent comment-column 1) ; Indent min 1
1992 c)))))
f83d2997
KH
1993
1994;;;(defun cperl-comment-indent-fallback ()
1995;;; "Is called if the standard comment-search procedure fails.
1996;;;Point is at start of real comment."
1997;;; (let ((c (current-column)) target cnt prevc)
1998;;; (if (= c comment-column) nil
1999;;; (setq cnt (skip-chars-backward "[ \t]"))
5c8b7eaf 2000;;; (setq target (max (1+ (setq prevc
f83d2997
KH
2001;;; (current-column))) ; Else indent at comment column
2002;;; comment-column))
2003;;; (if (= c comment-column) nil
2004;;; (delete-backward-char cnt)
2005;;; (while (< prevc target)
2006;;; (insert "\t")
2007;;; (setq prevc (current-column)))
2008;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
2009;;; (while (< prevc target)
2010;;; (insert " ")
2011;;; (setq prevc (current-column)))))))
2012
2013(defun cperl-indent-for-comment ()
2014 "Substitute for `indent-for-comment' in CPerl."
2015 (interactive)
2016 (let (cperl-wrong-comment)
2017 (indent-for-comment)
4ab89e7b 2018 (if cperl-wrong-comment ; set by `cperl-comment-indent'
f83d2997
KH
2019 (progn (cperl-to-comment-or-eol)
2020 (forward-char (length comment-start))))))
2021
2022(defun cperl-comment-region (b e arg)
2023 "Comment or uncomment each line in the region in CPerl mode.
2024See `comment-region'."
2025 (interactive "r\np")
2026 (let ((comment-start "#"))
2027 (comment-region b e arg)))
2028
2029(defun cperl-uncomment-region (b e arg)
2030 "Uncomment or comment each line in the region in CPerl mode.
2031See `comment-region'."
2032 (interactive "r\np")
2033 (let ((comment-start "#"))
2034 (comment-region b e (- arg))))
2035
2036(defvar cperl-brace-recursing nil)
2037
2038(defun cperl-electric-brace (arg &optional only-before)
2039 "Insert character and correct line's indentation.
2040If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2041place (even in empty line), but not after. If after \")\" and the inserted
5c8b7eaf 2042char is \"{\", insert extra newline before only if
f83d2997
KH
2043`cperl-extra-newline-before-brace'."
2044 (interactive "P")
2045 (let (insertpos
2046 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 2047 (cperl-mark-active)
f83d2997 2048 (< (mark) (point)))
5c8b7eaf 2049 (mark)
f83d2997
KH
2050 nil)))
2051 (if (and other-end
2052 (not cperl-brace-recursing)
2053 (cperl-val 'cperl-electric-parens)
2054 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2055 ;; Need to insert a matching pair
2056 (progn
2057 (save-excursion
2058 (setq insertpos (point-marker))
2059 (goto-char other-end)
1ba983e8 2060 (setq last-command-event ?\{)
f83d2997
KH
2061 (cperl-electric-lbrace arg insertpos))
2062 (forward-char 1))
83261a2f 2063 ;; Check whether we close something "usual" with `}'
1ba983e8 2064 (if (and (eq last-command-event ?\})
5c8b7eaf 2065 (not
db133cb6
RS
2066 (condition-case nil
2067 (save-excursion
2068 (up-list (- (prefix-numeric-value arg)))
2069 ;;(cperl-after-block-p (point-min))
f739b53b
SM
2070 (or (cperl-after-expr-p nil "{;)")
2071 ;; after sub, else, continue
2072 (cperl-after-block-p nil 'pre)))
db133cb6
RS
2073 (error nil))))
2074 ;; Just insert the guy
2075 (self-insert-command (prefix-numeric-value arg))
2076 (if (and (not arg) ; No args, end (of empty line or auto)
2077 (eolp)
2078 (or (and (null only-before)
2079 (save-excursion
2080 (skip-chars-backward " \t")
2081 (bolp)))
1ba983e8 2082 (and (eq last-command-event ?\{) ; Do not insert newline
db133cb6
RS
2083 ;; if after ")" and `cperl-extra-newline-before-brace'
2084 ;; is nil, do not insert extra newline.
2085 (not cperl-extra-newline-before-brace)
2086 (save-excursion
2087 (skip-chars-backward " \t")
2088 (eq (preceding-char) ?\))))
5c8b7eaf 2089 (if cperl-auto-newline
db133cb6
RS
2090 (progn (cperl-indent-line) (newline) t) nil)))
2091 (progn
2092 (self-insert-command (prefix-numeric-value arg))
2093 (cperl-indent-line)
2094 (if cperl-auto-newline
2095 (setq insertpos (1- (point))))
2096 (if (and cperl-auto-newline (null only-before))
2097 (progn
2098 (newline)
2099 (cperl-indent-line)))
2100 (save-excursion
2101 (if insertpos (progn (goto-char insertpos)
5c8b7eaf 2102 (search-forward (make-string
1ba983e8 2103 1 last-command-event))
db133cb6
RS
2104 (setq insertpos (1- (point)))))
2105 (delete-char -1))))
2106 (if insertpos
f83d2997 2107 (save-excursion
db133cb6
RS
2108 (goto-char insertpos)
2109 (self-insert-command (prefix-numeric-value arg)))
2110 (self-insert-command (prefix-numeric-value arg)))))))
f83d2997
KH
2111
2112(defun cperl-electric-lbrace (arg &optional end)
2113 "Insert character, correct line's indentation, correct quoting by space."
2114 (interactive "P")
83261a2f
SM
2115 (let ((cperl-brace-recursing t)
2116 (cperl-auto-newline cperl-auto-newline)
2117 (other-end (or end
2118 (if (and cperl-electric-parens-mark
2119 (cperl-mark-active)
2120 (> (mark) (point)))
2121 (save-excursion
2122 (goto-char (mark))
2123 (point-marker))
2124 nil)))
2125 pos after)
f83d2997
KH
2126 (and (cperl-val 'cperl-electric-lbrace-space)
2127 (eq (preceding-char) ?$)
2128 (save-excursion
2129 (skip-chars-backward "$")
2130 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
b5b0cb34 2131 (insert ?\s))
bab27c0c 2132 ;; Check whether we are in comment
5c8b7eaf 2133 (if (and
bab27c0c
RS
2134 (save-excursion
2135 (beginning-of-line)
2136 (not (looking-at "[ \t]*#")))
2137 (cperl-after-expr-p nil "{;)"))
2138 nil
2139 (setq cperl-auto-newline nil))
f83d2997
KH
2140 (cperl-electric-brace arg)
2141 (and (cperl-val 'cperl-electric-parens)
1ba983e8
GM
2142 (eq last-command-event ?{)
2143 (memq last-command-event
f83d2997
KH
2144 (append cperl-electric-parens-string nil))
2145 (or (if other-end (goto-char (marker-position other-end)))
2146 t)
1ba983e8 2147 (setq last-command-event ?} pos (point))
f83d2997
KH
2148 (progn (cperl-electric-brace arg t)
2149 (goto-char pos)))))
2150
2151(defun cperl-electric-paren (arg)
f739b53b
SM
2152 "Insert an opening parenthesis or a matching pair of parentheses.
2153See `cperl-electric-parens'."
f83d2997 2154 (interactive "P")
e180ab9f 2155 (let ((beg (point-at-bol))
f83d2997 2156 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 2157 (cperl-mark-active)
f83d2997 2158 (> (mark) (point)))
83261a2f
SM
2159 (save-excursion
2160 (goto-char (mark))
2161 (point-marker))
f83d2997
KH
2162 nil)))
2163 (if (and (cperl-val 'cperl-electric-parens)
1ba983e8 2164 (memq last-command-event
f83d2997
KH
2165 (append cperl-electric-parens-string nil))
2166 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2167 ;;(not (save-excursion (search-backward "#" beg t)))
1ba983e8 2168 (if (eq last-command-event ?<)
f83d2997 2169 (progn
7a55c78b
CY
2170 ;; This code is too electric, see Bug#3943.
2171 ;; (and abbrev-mode ; later it is too late, may be after `for'
2172 ;; (expand-abbrev))
f83d2997
KH
2173 (cperl-after-expr-p nil "{;(,:="))
2174 1))
2175 (progn
2176 (self-insert-command (prefix-numeric-value arg))
2177 (if other-end (goto-char (marker-position other-end)))
5c8b7eaf 2178 (insert (make-string
f83d2997 2179 (prefix-numeric-value arg)
1ba983e8 2180 (cdr (assoc last-command-event '((?{ .?})
f83d2997
KH
2181 (?[ . ?])
2182 (?( . ?))
2183 (?< . ?>))))))
2184 (forward-char (- (prefix-numeric-value arg))))
2185 (self-insert-command (prefix-numeric-value arg)))))
2186
2187(defun cperl-electric-rparen (arg)
2188 "Insert a matching pair of parentheses if marking is active.
f739b53b
SM
2189If not, or if we are not at the end of marking range, would self-insert.
2190Affected by `cperl-electric-parens'."
f83d2997 2191 (interactive "P")
e180ab9f 2192 (let ((beg (point-at-bol))
f83d2997
KH
2193 (other-end (if (and cperl-electric-parens-mark
2194 (cperl-val 'cperl-electric-parens)
1ba983e8 2195 (memq last-command-event
f83d2997 2196 (append cperl-electric-parens-string nil))
5c8b7eaf 2197 (cperl-mark-active)
f83d2997 2198 (< (mark) (point)))
5c8b7eaf 2199 (mark)
f83d2997
KH
2200 nil))
2201 p)
2202 (if (and other-end
2203 (cperl-val 'cperl-electric-parens)
1ba983e8 2204 (memq last-command-event '( ?\) ?\] ?\} ?\> ))
f83d2997
KH
2205 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2206 ;;(not (save-excursion (search-backward "#" beg t)))
2207 )
2208 (progn
2209 (self-insert-command (prefix-numeric-value arg))
2210 (setq p (point))
2211 (if other-end (goto-char other-end))
2212 (insert (make-string
2213 (prefix-numeric-value arg)
1ba983e8 2214 (cdr (assoc last-command-event '((?\} . ?\{)
83261a2f
SM
2215 (?\] . ?\[)
2216 (?\) . ?\()
2217 (?\> . ?\<))))))
f83d2997
KH
2218 (goto-char (1+ p)))
2219 (self-insert-command (prefix-numeric-value arg)))))
2220
2221(defun cperl-electric-keyword ()
db133cb6
RS
2222 "Insert a construction appropriate after a keyword.
2223Help message may be switched off by setting `cperl-message-electric-keyword'
2224to nil."
e180ab9f 2225 (let ((beg (point-at-bol))
1ba983e8 2226 (dollar (and (eq last-command-event ?$)
f83d2997 2227 (eq this-command 'self-insert-command)))
1ba983e8 2228 (delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
db133cb6
RS
2229 (memq this-command '(self-insert-command newline))))
2230 my do)
f83d2997 2231 (and (save-excursion
db133cb6
RS
2232 (condition-case nil
2233 (progn
2234 (backward-sexp 1)
2235 (setq do (looking-at "do\\>")))
2236 (error nil))
f83d2997 2237 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
2238 (save-excursion
2239 (not
f83d2997 2240 (re-search-backward
5bd52f0e 2241 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
2242 beg t)))
2243 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
2244 (or
2245 (looking-at "=cut")
2246 (and cperl-use-syntax-table-text-property
2247 (not (eq (get-text-property (point)
2248 'syntax-type)
2249 'pod))))))
f739b53b
SM
2250 (save-excursion (forward-sexp -1)
2251 (not (memq (following-char) (append "$@%&*" nil))))
f83d2997 2252 (progn
db133cb6
RS
2253 (and (eq (preceding-char) ?y)
2254 (progn ; "foreachmy"
2255 (forward-char -2)
2256 (insert " ")
2257 (forward-char 2)
5c8b7eaf
SS
2258 (setq my t dollar t
2259 delete
db133cb6 2260 (memq this-command '(self-insert-command newline)))))
f83d2997
KH
2261 (and dollar (insert " $"))
2262 (cperl-indent-line)
2263 ;;(insert " () {\n}")
2264 (cond
2265 (cperl-extra-newline-before-brace
db133cb6 2266 (insert (if do "\n" " ()\n"))
f83d2997
KH
2267 (insert "{")
2268 (cperl-indent-line)
2269 (insert "\n")
2270 (cperl-indent-line)
db133cb6
RS
2271 (insert "\n}")
2272 (and do (insert " while ();")))
f83d2997 2273 (t
83261a2f 2274 (insert (if do " {\n} while ();" " () {\n}"))))
f83d2997
KH
2275 (or (looking-at "[ \t]\\|$") (insert " "))
2276 (cperl-indent-line)
2277 (if dollar (progn (search-backward "$")
5c8b7eaf 2278 (if my
db133cb6
RS
2279 (forward-char 1)
2280 (delete-char 1)))
f739b53b 2281 (search-backward ")")
1ba983e8 2282 (if (eq last-command-event ?\()
f739b53b
SM
2283 (progn ; Avoid "if (())"
2284 (delete-backward-char 1)
2285 (delete-backward-char -1))))
f83d2997 2286 (if delete
db133cb6
RS
2287 (cperl-putback-char cperl-del-back-ch))
2288 (if cperl-message-electric-keyword
2289 (message "Precede char by C-q to avoid expansion"))))))
2290
2291(defun cperl-ensure-newlines (n &optional pos)
2292 "Make sure there are N newlines after the point."
2293 (or pos (setq pos (point)))
2294 (if (looking-at "\n")
2295 (forward-char 1)
2296 (insert "\n"))
2297 (if (> n 1)
2298 (cperl-ensure-newlines (1- n) pos)
2299 (goto-char pos)))
2300
2301(defun cperl-electric-pod ()
2302 "Insert a POD chunk appropriate after a =POD directive."
1ba983e8 2303 (let ((delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
db133cb6
RS
2304 (memq this-command '(self-insert-command newline))))
2305 head1 notlast name p really-delete over)
2306 (and (save-excursion
6c389151 2307 (forward-word -1)
a1506d29 2308 (and
db133cb6
RS
2309 (eq (preceding-char) ?=)
2310 (progn
6c389151
SM
2311 (setq head1 (looking-at "head1\\>[ \t]*$"))
2312 (setq over (and (looking-at "over\\>[ \t]*$")
2313 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
db133cb6
RS
2314 (forward-char -1)
2315 (bolp))
5c8b7eaf 2316 (or
5bd52f0e 2317 (get-text-property (point) 'in-pod)
db133cb6 2318 (cperl-after-expr-p nil "{;:")
4ab89e7b
SM
2319 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2320 (not (looking-at "\n*=cut"))
2321 (or (not cperl-use-syntax-table-text-property)
2322 (eq (get-text-property (point) 'syntax-type) 'pod))))))
db133cb6
RS
2323 (progn
2324 (save-excursion
6c389151 2325 (setq notlast (re-search-forward "^\n=" nil t)))
db133cb6
RS
2326 (or notlast
2327 (progn
2328 (insert "\n\n=cut")
2329 (cperl-ensure-newlines 2)
6c389151 2330 (forward-word -2)
a1506d29
JB
2331 (if (and head1
2332 (not
db133cb6
RS
2333 (save-excursion
2334 (forward-char -1)
2335 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
83261a2f 2336 nil t)))) ; Only one
a1506d29 2337 (progn
6c389151 2338 (forward-word 1)
d2c32364 2339 (setq name (file-name-base)
db133cb6 2340 p (point))
5c8b7eaf 2341 (insert " NAME\n\n" name
029cb4d5 2342 " - \n\n=head1 SYNOPSIS\n\n\n\n"
db133cb6
RS
2343 "=head1 DESCRIPTION")
2344 (cperl-ensure-newlines 4)
2345 (goto-char p)
6c389151 2346 (forward-word 2)
db133cb6
RS
2347 (end-of-line)
2348 (setq really-delete t))
6c389151 2349 (forward-word 1))))
db133cb6
RS
2350 (if over
2351 (progn
2352 (setq p (point))
2353 (insert "\n\n=item \n\n\n\n"
2354 "=back")
2355 (cperl-ensure-newlines 2)
2356 (goto-char p)
6c389151 2357 (forward-word 1)
db133cb6
RS
2358 (end-of-line)
2359 (setq really-delete t)))
2360 (if (and delete really-delete)
f83d2997
KH
2361 (cperl-putback-char cperl-del-back-ch))))))
2362
2363(defun cperl-electric-else ()
db133cb6
RS
2364 "Insert a construction appropriate after a keyword.
2365Help message may be switched off by setting `cperl-message-electric-keyword'
2366to nil."
e180ab9f 2367 (let ((beg (point-at-bol)))
f83d2997
KH
2368 (and (save-excursion
2369 (backward-sexp 1)
2370 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
2371 (save-excursion
2372 (not
f83d2997 2373 (re-search-backward
5bd52f0e 2374 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
2375 beg t)))
2376 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
2377 (looking-at "=cut")
2378 (and cperl-use-syntax-table-text-property
2379 (not (eq (get-text-property (point)
2380 'syntax-type)
2381 'pod)))))
f83d2997
KH
2382 (progn
2383 (cperl-indent-line)
2384 ;;(insert " {\n\n}")
2385 (cond
2386 (cperl-extra-newline-before-brace
2387 (insert "\n")
2388 (insert "{")
2389 (cperl-indent-line)
2390 (insert "\n\n}"))
2391 (t
83261a2f 2392 (insert " {\n\n}")))
f83d2997
KH
2393 (or (looking-at "[ \t]\\|$") (insert " "))
2394 (cperl-indent-line)
2395 (forward-line -1)
2396 (cperl-indent-line)
db133cb6
RS
2397 (cperl-putback-char cperl-del-back-ch)
2398 (setq this-command 'cperl-electric-else)
2399 (if cperl-message-electric-keyword
2400 (message "Precede char by C-q to avoid expansion"))))))
f83d2997
KH
2401
2402(defun cperl-linefeed ()
db133cb6
RS
2403 "Go to end of line, open a new line and indent appropriately.
2404If in POD, insert appropriate lines."
f83d2997 2405 (interactive)
e180ab9f
GM
2406 (let ((beg (point-at-bol))
2407 (end (point-at-eol))
db133cb6 2408 (pos (point)) start over cut res)
f83d2997 2409 (if (and ; Check if we need to split:
5c8b7eaf 2410 ; i.e., on a boundary and inside "{...}"
f83d2997 2411 (save-excursion (cperl-to-comment-or-eol)
83261a2f 2412 (>= (point) pos)) ; Not in a comment
f83d2997
KH
2413 (or (save-excursion
2414 (skip-chars-backward " \t" beg)
2415 (forward-char -1)
2416 (looking-at "[;{]")) ; After { or ; + spaces
2417 (looking-at "[ \t]*}") ; Before }
2418 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2419 (save-excursion
2420 (and
5c8b7eaf 2421 (eq (car (parse-partial-sexp pos end -1)) -1)
f83d2997
KH
2422 ; Leave the level of parens
2423 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2424 ; Are at end
6c389151 2425 (cperl-after-block-p (point-min))
f83d2997
KH
2426 (progn
2427 (backward-sexp 1)
2428 (setq start (point-marker))
db133cb6 2429 (<= start pos))))) ; Redundant? Are after the
f83d2997
KH
2430 ; start of parens group.
2431 (progn
2432 (skip-chars-backward " \t")
2433 (or (memq (preceding-char) (append ";{" nil))
2434 (insert ";"))
2435 (insert "\n")
2436 (forward-line -1)
2437 (cperl-indent-line)
2438 (goto-char start)
2439 (or (looking-at "{[ \t]*$") ; If there is a statement
2440 ; before, move it to separate line
2441 (progn
2442 (forward-char 1)
2443 (insert "\n")
2444 (cperl-indent-line)))
2445 (forward-line 1) ; We are on the target line
2446 (cperl-indent-line)
2447 (beginning-of-line)
2448 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
83261a2f 2449 ; after, move it to separate line
f83d2997
KH
2450 (progn
2451 (end-of-line)
2452 (search-backward "}" beg)
2453 (skip-chars-backward " \t")
2454 (or (memq (preceding-char) (append ";{" nil))
2455 (insert ";"))
2456 (insert "\n")
2457 (cperl-indent-line)
2458 (forward-line -1)))
5c8b7eaf 2459 (forward-line -1) ; We are on the line before target
f83d2997
KH
2460 (end-of-line)
2461 (newline-and-indent))
db133cb6 2462 (end-of-line) ; else - no splitting
f83d2997
KH
2463 (cond
2464 ((and (looking-at "\n[ \t]*{$")
2465 (save-excursion
2466 (skip-chars-backward " \t")
2467 (eq (preceding-char) ?\)))) ; Probably if () {} group
83261a2f 2468 ; with an extra newline.
f83d2997
KH
2469 (forward-line 2)
2470 (cperl-indent-line))
db133cb6
RS
2471 ((save-excursion ; In POD header
2472 (forward-paragraph -1)
2473 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2474 ;; We are after \n now, so look for the rest
2475 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
5c8b7eaf 2476 (progn
db133cb6
RS
2477 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2478 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2479 t)))
2480 (if (and over
2481 (progn
2482 (forward-paragraph -1)
2483 (forward-word 1)
2484 (setq pos (point))
e180ab9f
GM
2485 (setq cut (buffer-substring (point) (point-at-eol)))
2486 (delete-char (- (point-at-eol) (point)))
db133cb6
RS
2487 (setq res (expand-abbrev))
2488 (save-excursion
2489 (goto-char pos)
2490 (insert cut))
2491 res))
2492 nil
2493 (cperl-ensure-newlines (if cut 2 4))
2494 (forward-line 2)))
2495 ((get-text-property (point) 'in-pod) ; In POD section
2496 (cperl-ensure-newlines 4)
2497 (forward-line 2))
f83d2997
KH
2498 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2499 (forward-line 1)
2500 (cperl-indent-line))
2501 (t
2502 (newline-and-indent))))))
2503
2504(defun cperl-electric-semi (arg)
2505 "Insert character and correct line's indentation."
2506 (interactive "P")
2507 (if cperl-auto-newline
2508 (cperl-electric-terminator arg)
6c389151
SM
2509 (self-insert-command (prefix-numeric-value arg))
2510 (if cperl-autoindent-on-semi
2511 (cperl-indent-line))))
f83d2997
KH
2512
2513(defun cperl-electric-terminator (arg)
2514 "Insert character and correct line's indentation."
2515 (interactive "P")
83261a2f
SM
2516 (let ((end (point))
2517 (auto (and cperl-auto-newline
1ba983e8 2518 (or (not (eq last-command-event ?:))
83261a2f
SM
2519 cperl-auto-newline-after-colon)))
2520 insertpos)
5c8b7eaf 2521 (if (and ;;(not arg)
f83d2997
KH
2522 (eolp)
2523 (not (save-excursion
2524 (beginning-of-line)
2525 (skip-chars-forward " \t")
2526 (or
2527 ;; Ignore in comment lines
2528 (= (following-char) ?#)
2529 ;; Colon is special only after a label
2530 ;; So quickly rule out most other uses of colon
2531 ;; and do no indentation for them.
1ba983e8 2532 (and (eq last-command-event ?:)
f83d2997
KH
2533 (save-excursion
2534 (forward-word 1)
2535 (skip-chars-forward " \t")
2536 (and (< (point) end)
2537 (progn (goto-char (- end 1))
2538 (not (looking-at ":"))))))
2539 (progn
2540 (beginning-of-defun)
2541 (let ((pps (parse-partial-sexp (point) end)))
2542 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2543 (progn
2544 (self-insert-command (prefix-numeric-value arg))
2545 ;;(forward-char -1)
2546 (if auto (setq insertpos (point-marker)))
2547 ;;(forward-char 1)
2548 (cperl-indent-line)
2549 (if auto
2550 (progn
2551 (newline)
2552 (cperl-indent-line)))
f83d2997
KH
2553 (save-excursion
2554 (if insertpos (goto-char (1- (marker-position insertpos)))
2555 (forward-char -1))
2556 (delete-char 1))))
2557 (if insertpos
2558 (save-excursion
2559 (goto-char insertpos)
2560 (self-insert-command (prefix-numeric-value arg)))
2561 (self-insert-command (prefix-numeric-value arg)))))
2562
2563(defun cperl-electric-backspace (arg)
8c777c8d
CY
2564 "Backspace, or remove whitespace around the point inserted by an electric key.
2565Will untabify if `cperl-electric-backspace-untabify' is non-nil."
f83d2997 2566 (interactive "p")
5c8b7eaf
SS
2567 (if (and cperl-auto-newline
2568 (memq last-command '(cperl-electric-semi
f83d2997
KH
2569 cperl-electric-terminator
2570 cperl-electric-lbrace))
b5b0cb34 2571 (memq (preceding-char) '(?\s ?\t ?\n)))
f83d2997 2572 (let (p)
5c8b7eaf 2573 (if (eq last-command 'cperl-electric-lbrace)
f83d2997
KH
2574 (skip-chars-forward " \t\n"))
2575 (setq p (point))
2576 (skip-chars-backward " \t\n")
2577 (delete-region (point) p))
db133cb6
RS
2578 (and (eq last-command 'cperl-electric-else)
2579 ;; We are removing the whitespace *inside* cperl-electric-else
2580 (setq this-command 'cperl-electric-else-really))
5c8b7eaf 2581 (if (and cperl-auto-newline
db133cb6 2582 (eq last-command 'cperl-electric-else-really)
b5b0cb34 2583 (memq (preceding-char) '(?\s ?\t ?\n)))
db133cb6
RS
2584 (let (p)
2585 (skip-chars-forward " \t\n")
2586 (setq p (point))
2587 (skip-chars-backward " \t\n")
2588 (delete-region (point) p))
f739b53b
SM
2589 (if cperl-electric-backspace-untabify
2590 (backward-delete-char-untabify arg)
2591 (delete-backward-char arg)))))
f83d2997 2592
d6156ce8
KS
2593(put 'cperl-electric-backspace 'delete-selection 'supersede)
2594
4ab89e7b 2595(defun cperl-inside-parens-p () ;; NOT USED????
f83d2997
KH
2596 (condition-case ()
2597 (save-excursion
2598 (save-restriction
2599 (narrow-to-region (point)
2600 (progn (beginning-of-defun) (point)))
2601 (goto-char (point-max))
2602 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2603 (error nil)))
2604\f
2605(defun cperl-indent-command (&optional whole-exp)
2606 "Indent current line as Perl code, or in some cases insert a tab character.
5c8b7eaf 2607If `cperl-tab-always-indent' is non-nil (the default), always indent current
db133cb6 2608line. Otherwise, indent the current line only if point is at the left margin
f83d2997
KH
2609or in the line's indentation; otherwise insert a tab.
2610
2611A numeric argument, regardless of its value,
2612means indent rigidly all the lines of the expression starting after point
2613so that this line becomes properly indented.
2614The relative indentation among the lines of the expression are preserved."
2615 (interactive "P")
5bd52f0e 2616 (cperl-update-syntaxification (point) (point))
f83d2997
KH
2617 (if whole-exp
2618 ;; If arg, always indent this line as Perl
2619 ;; and shift remaining lines of expression the same amount.
2620 (let ((shift-amt (cperl-indent-line))
2621 beg end)
2622 (save-excursion
2623 (if cperl-tab-always-indent
2624 (beginning-of-line))
2625 (setq beg (point))
2626 (forward-sexp 1)
2627 (setq end (point))
2628 (goto-char beg)
2629 (forward-line 1)
2630 (setq beg (point)))
db133cb6 2631 (if (and shift-amt (> end beg))
f83d2997
KH
2632 (indent-code-rigidly beg end shift-amt "#")))
2633 (if (and (not cperl-tab-always-indent)
2634 (save-excursion
2635 (skip-chars-backward " \t")
2636 (not (bolp))))
2637 (insert-tab)
2638 (cperl-indent-line))))
2639
5bd52f0e 2640(defun cperl-indent-line (&optional parse-data)
f83d2997
KH
2641 "Indent current line as Perl code.
2642Return the amount the indentation changed by."
83261a2f
SM
2643 (let ((case-fold-search nil)
2644 (pos (- (point-max) (point)))
2645 indent i beg shift-amt)
5bd52f0e 2646 (setq indent (cperl-calculate-indent parse-data)
db133cb6 2647 i indent)
f83d2997
KH
2648 (beginning-of-line)
2649 (setq beg (point))
2650 (cond ((or (eq indent nil) (eq indent t))
db133cb6 2651 (setq indent (current-indentation) i nil))
f83d2997
KH
2652 ;;((eq indent t) ; Never?
2653 ;; (setq indent (cperl-calculate-indent-within-comment)))
2654 ;;((looking-at "[ \t]*#")
2655 ;; (setq indent 0))
2656 (t
2657 (skip-chars-forward " \t")
2658 (if (listp indent) (setq indent (car indent)))
82d9a08d
SM
2659 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2660 (not (looking-at "[smy]:\\|tr:")))
f83d2997
KH
2661 (and (> indent 0)
2662 (setq indent (max cperl-min-label-indent
2663 (+ indent cperl-label-offset)))))
2664 ((= (following-char) ?})
2665 (setq indent (- indent cperl-indent-level)))
2666 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2667 (setq indent (+ indent cperl-close-paren-offset)))
2668 ((= (following-char) ?{)
2669 (setq indent (+ indent cperl-brace-offset))))))
2670 (skip-chars-forward " \t")
db133cb6
RS
2671 (setq shift-amt (and i (- indent (current-column))))
2672 (if (or (not shift-amt)
2673 (zerop shift-amt))
f83d2997
KH
2674 (if (> (- (point-max) pos) (point))
2675 (goto-char (- (point-max) pos)))
4ab89e7b
SM
2676 ;;;(delete-region beg (point))
2677 ;;;(indent-to indent)
2678 (cperl-make-indent indent)
f83d2997
KH
2679 ;; If initial point was within line's indentation,
2680 ;; position after the indentation. Else stay at same point in text.
2681 (if (> (- (point-max) pos) (point))
2682 (goto-char (- (point-max) pos))))
2683 shift-amt))
2684
2685(defun cperl-after-label ()
2686 ;; Returns true if the point is after label. Does not do save-excursion.
2687 (and (eq (preceding-char) ?:)
2688 (memq (char-syntax (char-after (- (point) 2)))
2689 '(?w ?_))
2690 (progn
2691 (backward-sexp)
2692 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2693
2694(defun cperl-get-state (&optional parse-start start-state)
5bd52f0e
RS
2695 ;; returns list (START STATE DEPTH PRESTART),
2696 ;; START is a good place to start parsing, or equal to
5c8b7eaf 2697 ;; PARSE-START if preset,
5bd52f0e
RS
2698 ;; STATE is what is returned by `parse-partial-sexp'.
2699 ;; DEPTH is true is we are immediately after end of block
2700 ;; which contains START.
2701 ;; PRESTART is the position basing on which START was found.
f83d2997
KH
2702 (save-excursion
2703 (let ((start-point (point)) depth state start prestart)
5bd52f0e
RS
2704 (if (and parse-start
2705 (<= parse-start start-point))
f83d2997 2706 (goto-char parse-start)
5bd52f0e
RS
2707 (beginning-of-defun)
2708 (setq start-state nil))
f83d2997
KH
2709 (setq prestart (point))
2710 (if start-state nil
2711 ;; Try to go out, if sub is not on the outermost level
2712 (while (< (point) start-point)
2713 (setq start (point) parse-start start depth nil
2714 state (parse-partial-sexp start start-point -1))
2715 (if (> (car state) -1) nil
2716 ;; The current line could start like }}}, so the indentation
2717 ;; corresponds to a different level than what we reached
2718 (setq depth t)
2719 (beginning-of-line 2))) ; Go to the next line.
2720 (if start (goto-char start))) ; Not at the start of file
2721 (setq start (point))
f83d2997
KH
2722 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2723 (list start state depth prestart))))
2724
f83d2997
KH
2725(defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2726
4ab89e7b
SM
2727(defun cperl-beginning-of-property (p prop &optional lim)
2728 "Given that P has a property PROP, find where the property starts.
2729Will not look before LIM."
2730 ;;; XXXX What to do at point-max???
2731 (or (previous-single-property-change (cperl-1+ p) prop lim)
2732 (point-min))
2733;;; (cond ((eq p (point-min))
2734;;; p)
2735;;; ((and lim (<= p lim))
2736;;; p)
2737;;; ((not (get-text-property (1- p) prop))
2738;;; p)
2739;;; (t (or (previous-single-property-change p look-prop lim)
2740;;; (point-min))))
2741 )
2742
2743(defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
8c777c8d 2744 ;; the sniffer logic to understand what the current line MEANS.
f739b53b 2745 (cperl-update-syntaxification (point) (point))
4ab89e7b
SM
2746 (let ((res (get-text-property (point) 'syntax-type)))
2747 (save-excursion
2748 (cond
2749 ((and (memq res '(pod here-doc here-doc-delim format))
2750 (not (get-text-property (point) 'indentable)))
2751 (vector res))
2752 ;; before start of POD - whitespace found since do not have 'pod!
2753 ((looking-at "[ \t]*\n=")
2754 (error "Spaces before POD section!"))
2755 ((and (not cperl-indent-left-aligned-comments)
2756 (looking-at "^#"))
2757 [comment-special:at-beginning-of-line])
2758 ((get-text-property (point) 'in-pod)
2759 [in-pod])
2760 (t
2761 (beginning-of-line)
2762 (let* ((indent-point (point))
2763 (char-after-pos (save-excursion
2764 (skip-chars-forward " \t")
2765 (point)))
2766 (char-after (char-after char-after-pos))
2767 (pre-indent-point (point))
2768 p prop look-prop is-block delim)
2769 (save-excursion ; Know we are not in POD, find appropriate pos before
83261a2f
SM
2770 (cperl-backward-to-noncomment nil)
2771 (setq p (max (point-min) (1- (point)))
2772 prop (get-text-property p 'syntax-type)
2773 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2774 'syntax-type))
2775 (if (memq prop '(pod here-doc format here-doc-delim))
2776 (progn
4ab89e7b 2777 (goto-char (cperl-beginning-of-property p look-prop))
83261a2f 2778 (beginning-of-line)
4ab89e7b 2779 (setq pre-indent-point (point)))))
97610156 2780 (goto-char pre-indent-point) ; Orig line skipping preceding pod/etc
4ab89e7b
SM
2781 (let* ((case-fold-search nil)
2782 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2783 (start (or (nth 2 parse-data) ; last complete sexp terminated
2784 (nth 0 s-s))) ; Good place to start parsing
2785 (state (nth 1 s-s))
2786 (containing-sexp (car (cdr state)))
2787 old-indent)
2788 (if (and
2789 ;;containing-sexp ;; We are buggy at toplevel :-(
2790 parse-data)
2791 (progn
2792 (setcar parse-data pre-indent-point)
2793 (setcar (cdr parse-data) state)
2794 (or (nth 2 parse-data)
2795 (setcar (cddr parse-data) start))
2796 ;; Before this point: end of statement
2797 (setq old-indent (nth 3 parse-data))))
2798 (cond ((get-text-property (point) 'indentable)
2799 ;; indent to "after" the surrounding open
2800 ;; (same offset as `cperl-beautify-regexp-piece'),
2801 ;; skip blanks if we do not close the expression.
2802 (setq delim ; We do not close the expression
2803 (get-text-property
2804 (cperl-1+ char-after-pos) 'indentable)
2805 p (1+ (cperl-beginning-of-property
2806 (point) 'indentable))
97610156
GM
2807 is-block ; misused for: preceding line in REx
2808 (save-excursion ; Find preceding line
4ab89e7b
SM
2809 (cperl-backward-to-noncomment p)
2810 (beginning-of-line)
2811 (if (<= (point) p)
2812 (progn ; get indent from the first line
2813 (goto-char p)
2814 (skip-chars-forward " \t")
2815 (if (memq (char-after (point))
2816 (append "#\n" nil))
53964682 2817 nil ; Can't use indentation of this line...
4ab89e7b
SM
2818 (point)))
2819 (skip-chars-forward " \t")
2820 (point)))
2821 prop (parse-partial-sexp p char-after-pos))
2822 (cond ((not delim) ; End the REx, ignore is-block
2823 (vector 'indentable 'terminator p is-block))
97610156 2824 (is-block ; Indent w.r.t. preceding line
4ab89e7b
SM
2825 (vector 'indentable 'cont-line char-after-pos
2826 is-block char-after p))
97610156 2827 (t ; No preceding line...
4ab89e7b
SM
2828 (vector 'indentable 'first-line p))))
2829 ((get-text-property char-after-pos 'REx-part2)
2830 (vector 'REx-part2 (point)))
4ab89e7b 2831 ((nth 4 state)
82d9a08d
SM
2832 [comment])
2833 ((nth 3 state)
4ab89e7b
SM
2834 [string])
2835 ;; XXXX Do we need to special-case this?
2836 ((null containing-sexp)
2837 ;; Line is at top level. May be data or function definition,
2838 ;; or may be function argument declaration.
2839 ;; Indent like the previous top level line
2840 ;; unless that ends in a closeparen without semicolon,
2841 ;; in which case this line is the first argument decl.
2842 (skip-chars-forward " \t")
2843 (cperl-backward-to-noncomment (or old-indent (point-min)))
2844 (setq state
2845 (or (bobp)
2846 (eq (point) old-indent) ; old-indent was at comment
2847 (eq (preceding-char) ?\;)
2848 ;; Had ?\) too
2849 (and (eq (preceding-char) ?\})
2850 (cperl-after-block-and-statement-beg
2851 (point-min))) ; Was start - too close
2852 (memq char-after (append ")]}" nil))
2853 (and (eq (preceding-char) ?\:) ; label
83261a2f
SM
2854 (progn
2855 (forward-sexp -1)
4ab89e7b
SM
2856 (skip-chars-backward " \t")
2857 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2858 (get-text-property (point) 'first-format-line)))
cb5bf6ba 2859
4ab89e7b
SM
2860 ;; Look at previous line that's at column 0
2861 ;; to determine whether we are in top-level decls
2862 ;; or function's arg decls. Set basic-indent accordingly.
2863 ;; Now add a little if this is a continuation line.
2864 (and state
2865 parse-data
2866 (not (eq char-after ?\C-j))
2867 (setcdr (cddr parse-data)
2868 (list pre-indent-point)))
2869 (vector 'toplevel start char-after state (nth 2 s-s)))
2870 ((not
2871 (or (setq is-block
2872 (and (setq delim (= (char-after containing-sexp) ?{))
2873 (save-excursion ; Is it a hash?
2874 (goto-char containing-sexp)
2875 (cperl-block-p))))
2876 cperl-indent-parens-as-block))
2877 ;; group is an expression, not a block:
2878 ;; indent to just after the surrounding open parens,
2879 ;; skip blanks if we do not close the expression.
2880 (goto-char (1+ containing-sexp))
2881 (or (memq char-after
2882 (append (if delim "}" ")]}") nil))
2883 (looking-at "[ \t]*\\(#\\|$\\)")
2884 (skip-chars-forward " \t"))
2885 (setq old-indent (point)) ; delim=is-brace
2886 (vector 'in-parens char-after (point) delim containing-sexp))
2887 (t
2888 ;; Statement level. Is it a continuation or a new statement?
2889 ;; Find previous non-comment character.
2890 (goto-char pre-indent-point) ; Skip one level of POD/etc
2891 (cperl-backward-to-noncomment containing-sexp)
2892 ;; Back up over label lines, since they don't
2893 ;; affect whether our line is a continuation.
2894 ;; (Had \, too)
2895 (while;;(or (eq (preceding-char) ?\,)
2896 (and (eq (preceding-char) ?:)
2897 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2898 (memq (char-syntax (char-after (- (point) 2)))
2899 '(?w ?_))))
2900 ;;)
2901 ;; This is always FALSE?
2902 (if (eq (preceding-char) ?\,)
2903 ;; Will go to beginning of line, essentially.
2904 ;; Will ignore embedded sexpr XXXX.
2905 (cperl-backward-to-start-of-continued-exp containing-sexp))
2906 (beginning-of-line)
2907 (cperl-backward-to-noncomment containing-sexp))
97610156 2908 ;; Now we get non-label preceding the indent point
4ab89e7b
SM
2909 (if (not (or (eq (1- (point)) containing-sexp)
2910 (memq (preceding-char)
2911 (append (if is-block " ;{" " ,;{") '(nil)))
2912 (and (eq (preceding-char) ?\})
2913 (cperl-after-block-and-statement-beg
2914 containing-sexp))
2915 (get-text-property (point) 'first-format-line)))
2916 ;; This line is continuation of preceding line's statement;
2917 ;; indent `cperl-continued-statement-offset' more than the
2918 ;; previous line of the statement.
2919 ;;
2920 ;; There might be a label on this line, just
2921 ;; consider it bad style and ignore it.
2922 (progn
2923 (cperl-backward-to-start-of-continued-exp containing-sexp)
2924 (vector 'continuation (point) char-after is-block delim))
2925 ;; This line starts a new statement.
2926 ;; Position following last unclosed open brace
2927 (goto-char containing-sexp)
2928 ;; Is line first statement after an open-brace?
2929 (or
2930 ;; If no, find that first statement and indent like
2931 ;; it. If the first statement begins with label, do
2932 ;; not believe when the indentation of the label is too
2933 ;; small.
2934 (save-excursion
2935 (forward-char 1)
2936 (let ((colon-line-end 0))
2937 (while
2938 (progn (skip-chars-forward " \t\n")
82d9a08d
SM
2939 ;; s: foo : bar :x is NOT label
2940 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2941 (not (looking-at "[sym]:\\|tr:"))))
4ab89e7b
SM
2942 ;; Skip over comments and labels following openbrace.
2943 (cond ((= (following-char) ?\#)
2944 (forward-line 1))
2945 ((= (following-char) ?\=)
2946 (goto-char
2947 (or (next-single-property-change (point) 'in-pod)
2948 (point-max)))) ; do not loop if no syntaxification
2949 ;; label:
2950 (t
e180ab9f 2951 (setq colon-line-end (point-at-eol))
4ab89e7b
SM
2952 (search-forward ":"))))
2953 ;; We are at beginning of code (NOT label or comment)
2954 ;; First, the following code counts
2955 ;; if it is before the line we want to indent.
2956 (and (< (point) indent-point)
2957 (vector 'have-prev-sibling (point) colon-line-end
2958 containing-sexp))))
2959 (progn
2960 ;; If no previous statement,
2961 ;; indent it relative to line brace is on.
2962
2963 ;; For open-braces not the first thing in a line,
2964 ;; add in cperl-brace-imaginary-offset.
2965
2966 ;; If first thing on a line: ?????
2967 ;; Move back over whitespace before the openbrace.
2968 (setq ; brace first thing on a line
2969 old-indent (progn (skip-chars-backward " \t") (bolp)))
2970 ;; Should we indent w.r.t. earlier than start?
2971 ;; Move to start of control group, possibly on a different line
2972 (or cperl-indent-wrt-brace
2973 (cperl-backward-to-noncomment (point-min)))
2974 ;; If the openbrace is preceded by a parenthesized exp,
2975 ;; move to the beginning of that;
2976 (if (eq (preceding-char) ?\))
2977 (progn
2978 (forward-sexp -1)
2979 (cperl-backward-to-noncomment (point-min))))
2980 ;; In the case it starts a subroutine, indent with
2981 ;; respect to `sub', not with respect to the
2982 ;; first thing on the line, say in the case of
2983 ;; anonymous sub in a hash.
2984 (if (and;; Is it a sub in group starting on this line?
2985 (cond ((get-text-property (point) 'attrib-group)
2986 (goto-char (cperl-beginning-of-property
2987 (point) 'attrib-group)))
2988 ((eq (preceding-char) ?b)
2989 (forward-sexp -1)
2990 (looking-at "sub\\>")))
2991 (setq p (nth 1 ; start of innermost containing list
2992 (parse-partial-sexp
9b026d9f 2993 (point-at-bol)
4ab89e7b
SM
2994 (point)))))
2995 (progn
2996 (goto-char (1+ p)) ; enclosing block on the same line
2997 (skip-chars-forward " \t")
2998 (vector 'code-start-in-block containing-sexp char-after
2999 (and delim (not is-block)) ; is a HASH
3000 old-indent ; brace first thing on a line
3001 t (point) ; have something before...
3002 )
3003 ;;(current-column)
3004 )
3005 ;; Get initial indentation of the line we are on.
3006 ;; If line starts with label, calculate label indentation
3007 (vector 'code-start-in-block containing-sexp char-after
3008 (and delim (not is-block)) ; is a HASH
3009 old-indent ; brace first thing on a line
82d9a08d 3010 nil (point))))))))))))))) ; nothing interesting before
4ab89e7b
SM
3011
3012(defvar cperl-indent-rules-alist
3013 '((pod nil) ; via `syntax-type' property
3014 (here-doc nil) ; via `syntax-type' property
3015 (here-doc-delim nil) ; via `syntax-type' property
3016 (format nil) ; via `syntax-type' property
3017 (in-pod nil) ; via `in-pod' property
3018 (comment-special:at-beginning-of-line nil)
3019 (string t)
3020 (comment nil))
3021 "Alist of indentation rules for CPerl mode.
3022The values mean:
3023 nil: do not indent;
82d9a08d 3024 number: add this amount of indentation.")
4ab89e7b
SM
3025
3026(defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3027 "Return appropriate indentation for current line as Perl code.
3028In usual case returns an integer: the column to indent to.
3029Returns nil if line starts inside a string, t if in a comment.
3030
3031Will not correct the indentation for labels, but will correct it for braces
3032and closing parentheses and brackets."
3033 ;; This code is still a broken architecture: in some cases we need to
3034 ;; compensate for some modifications which `cperl-indent-line' will add later
3035 (save-excursion
3036 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3037 (cond
3038 ;;((or (null i) (eq i t) (numberp i))
3039 ;; i)
3040 ((vectorp i)
3041 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3042 (cond
3043 (what (cadr what)) ; Load from table
3044 ;;
3045 ;; Indenters for regular expressions with //x and qw()
3046 ;;
3047 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3048 (goto-char (elt i 1))
3049 (condition-case nil ; Use indentation of the 1st part
3050 (forward-sexp -1))
3051 (current-column))
3052 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3053 (cond ;;; [indentable terminator start-pos is-block]
3054 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3055 (goto-char (elt i 2)) ; After opening parens
3056 (1- (current-column)))
3057 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3058 (goto-char (elt i 2))
3059 (+ (or cperl-regexp-indent-step cperl-indent-level)
3060 -1
3061 (current-column)))
3062 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3063 ;; Indent as the level after closing parens
3064 (goto-char (elt i 2)) ; indent line
3065 (skip-chars-forward " \t)") ; Skip closing parens
3066 (setq p (point))
3067 (goto-char (elt i 3)) ; previous line
3068 (skip-chars-forward " \t)") ; Skip closing parens
3069 ;; Number of parens in between:
3070 (setq p (nth 0 (parse-partial-sexp (point) p))
3071 what (elt i 4)) ; First char on current line
3072 (goto-char (elt i 3)) ; previous line
3073 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3074 (cond ((eq what ?\) )
3075 (- cperl-close-paren-offset)) ; compensate
3076 ((eq what ?\| )
3077 (- (or cperl-regexp-indent-step cperl-indent-level)))
3078 (t 0))
3079 (if (eq (following-char) ?\| )
3080 (or cperl-regexp-indent-step cperl-indent-level)
3081 0)
3082 (current-column)))
3083 (t
3084 (error "Unrecognized value of indent: %s" i))))
3085 ;;
3086 ;; Indenter for stuff at toplevel
3087 ;;
3088 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3089 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3090 (goto-char (elt i 1)) ; start = Good place to start parsing
cb5bf6ba 3091 (- (current-indentation) ;
4ab89e7b
SM
3092 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3093 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3094 ;; Look at previous line that's at column 0
3095 ;; to determine whether we are in top-level decls
3096 ;; or function's arg decls. Set basic-indent accordingly.
3097 ;; Now add a little if this is a continuation line.
3098 (if (elt i 3) ; state (XXX What is the semantic???)
3099 0
3100 cperl-continued-statement-offset)))
3101 ;;
3102 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3103 ;;
3104 ((eq 'in-parens (elt i 0))
3105 ;; in-parens char-after old-indent-point is-brace containing-sexp
3106
3107 ;; group is an expression, not a block:
3108 ;; indent to just after the surrounding open parens,
3109 ;; skip blanks if we do not close the expression.
3110 (+ (progn
3111 (goto-char (elt i 2)) ; old-indent-point
3112 (current-column))
3113 (if (and (elt i 3) ; is-brace
3114 (eq (elt i 1) ?\})) ; char-after
3115 ;; Correct indentation of trailing ?\}
3116 (+ cperl-indent-level cperl-close-paren-offset)
3117 0)))
3118 ;;
3119 ;; Indenter for continuation lines
3120 ;;
3121 ((eq 'continuation (elt i 0))
3122 ;; [continuation statement-start char-after is-block is-brace]
3123 (goto-char (elt i 1)) ; statement-start
fd146719
SS
3124 (+ (if (or (memq (elt i 2) (append "}])" nil)) ; char-after
3125 (eq 'continuation ; do not stagger continuations
3126 (elt (cperl-sniff-for-indent parse-data) 0)))
c91c771d 3127 0 ; Closing parenthesis or continuation of a continuation
4ab89e7b
SM
3128 cperl-continued-statement-offset)
3129 (if (or (elt i 3) ; is-block
3130 (not (elt i 4)) ; is-brace
3131 (not (eq (elt i 2) ?\}))) ; char-after
3132 0
3133 ;; Now it is a hash reference
3134 (+ cperl-indent-level cperl-close-paren-offset))
3135 ;; Labels do not take :: ...
3136 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3137 (if (> (current-indentation) cperl-min-label-indent)
3138 (- (current-indentation) cperl-label-offset)
3139 ;; Do not move `parse-data', this should
3140 ;; be quick anyway (this comment comes
3141 ;; from different location):
3142 (cperl-calculate-indent))
3143 (current-column))
3144 (if (eq (elt i 2) ?\{) ; char-after
3145 cperl-continued-brace-offset 0)))
3146 ;;
3147 ;; Indenter for lines in a block which are not leading lines
3148 ;;
3149 ((eq 'have-prev-sibling (elt i 0))
3150 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
82d9a08d
SM
3151 (goto-char (elt i 1)) ; sibling-beg
3152 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
4ab89e7b
SM
3153 (if (> (current-indentation)
3154 cperl-min-label-indent)
3155 (- (current-indentation) cperl-label-offset)
3156 ;; Do not believe: `max' was involved in calculation of indent
3157 (+ cperl-indent-level
3158 (save-excursion
3159 (goto-char (elt i 3)) ; block-start
3160 (current-indentation))))
3161 (current-column)))
3162 ;;
3163 ;; Indenter for the first line in a block
3164 ;;
3165 ((eq 'code-start-in-block (elt i 0))
3166 ;;[code-start-in-block before-brace char-after
3167 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3168 ;; group-starts-before-start-of-sub start-of-control-group]
3169 (goto-char (elt i 1))
3170 ;; For open brace in column zero, don't let statement
3171 ;; start there too. If cperl-indent-level=0,
3172 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3173 (+ (if (and (bolp) (zerop cperl-indent-level))
3174 (+ cperl-brace-offset cperl-continued-statement-offset)
3175 cperl-indent-level)
3176 (if (and (elt i 3) ; is-a-HASH-ref
3177 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3178 (+ cperl-indent-level cperl-close-paren-offset)
3179 0)
3180 ;; Unless openbrace is the first nonwhite thing on the line,
3181 ;; add the cperl-brace-imaginary-offset.
3182 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3183 cperl-brace-imaginary-offset)
3184 (progn
3185 (goto-char (elt i 6)) ; start-of-control-group
3186 (if (elt i 5) ; group-starts-before-start-of-sub
3187 (current-column)
3188 ;; Get initial indentation of the line we are on.
3189 ;; If line starts with label, calculate label indentation
3190 (if (save-excursion
3191 (beginning-of-line)
3192 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3193 (if (> (current-indentation) cperl-min-label-indent)
3194 (- (current-indentation) cperl-label-offset)
3195 ;; Do not move `parse-data', this should
3196 ;; be quick anyway:
3197 (cperl-calculate-indent))
3198 (current-indentation))))))
3199 (t
3200 (error "Unrecognized value of indent: %s" i))))
3201 (t
3202 (error "Got strange value of indent: %s" i))))))
3203
f83d2997
KH
3204(defun cperl-calculate-indent-within-comment ()
3205 "Return the indentation amount for line, assuming that
3206the current line is to be regarded as part of a block comment."
3207 (let (end star-start)
3208 (save-excursion
3209 (beginning-of-line)
3210 (skip-chars-forward " \t")
3211 (setq end (point))
3212 (and (= (following-char) ?#)
3213 (forward-line -1)
3214 (cperl-to-comment-or-eol)
3215 (setq end (point)))
3216 (goto-char end)
3217 (current-column))))
3218
3219
3220(defun cperl-to-comment-or-eol ()
029cb4d5 3221 "Go to position before comment on the current line, or to end of line.
4ab89e7b
SM
3222Returns true if comment is found. In POD will not move the point."
3223 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3224 ;; then looks for literal # or end-of-line.
e180ab9f 3225 (let (state stop-in cpoint (lim (point-at-eol)) pr e)
4ab89e7b
SM
3226 (or cperl-font-locking
3227 (cperl-update-syntaxification lim lim))
83261a2f 3228 (beginning-of-line)
4ab89e7b
SM
3229 (if (setq pr (get-text-property (point) 'syntax-type))
3230 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3231 (if (or (eq pr 'pod)
3232 (if (or (not e) (> e lim)) ; deep inside a group
3233 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
83261a2f 3234 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
4ab89e7b
SM
3235 ;; Else - need to do it the hard way
3236 (and (and e (<= e lim))
3237 (goto-char e))
83261a2f
SM
3238 (while (not stop-in)
3239 (setq state (parse-partial-sexp (point) lim nil nil nil t))
f83d2997 3240 ; stop at comment
83261a2f
SM
3241 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3242 (if (nth 4 state) ; After `#';
f83d2997
KH
3243 ; (nth 2 state) can be
3244 ; beginning of m,s,qq and so
3245 ; on
83261a2f
SM
3246 (if (nth 2 state)
3247 (progn
3248 (setq cpoint (point))
3249 (goto-char (nth 2 state))
3250 (cond
3251 ((looking-at "\\(s\\|tr\\)\\>")
3252 (or (re-search-forward
3253 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3254 lim 'move)
3255 (setq stop-in t)))
3256 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3257 (or (re-search-forward
3258 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3259 lim 'move)
3260 (setq stop-in t)))
3261 (t ; It was fair comment
3262 (setq stop-in t) ; Finish
3263 (goto-char (1- cpoint)))))
3264 (setq stop-in t) ; Finish
3265 (forward-char -1))
15ca5699 3266 (setq stop-in t))) ; Finish
83261a2f 3267 (nth 4 state))))
f83d2997 3268
f83d2997
KH
3269(defsubst cperl-modify-syntax-type (at how)
3270 (if (< at (point-max))
3271 (progn
3272 (put-text-property at (1+ at) 'syntax-table how)
4ab89e7b 3273 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
f83d2997
KH
3274
3275(defun cperl-protect-defun-start (s e)
3276 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3277 (save-excursion
3278 (goto-char s)
3279 (while (re-search-forward "^\\s(" e 'to-end)
3280 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3281
5bd52f0e 3282(defun cperl-commentify (bb e string &optional noface)
5c8b7eaf 3283 (if cperl-use-syntax-table-text-property
5bd52f0e
RS
3284 (if (eq noface 'n) ; Only immediate
3285 nil
f83d2997
KH
3286 ;; We suppose that e is _after_ the end of construction, as after eol.
3287 (setq string (if string cperl-st-sfence cperl-st-cfence))
6c389151
SM
3288 (if (> bb (- e 2))
3289 ;; one-char string/comment?!
3290 (cperl-modify-syntax-type bb cperl-st-punct)
3291 (cperl-modify-syntax-type bb string)
3292 (cperl-modify-syntax-type (1- e) string))
f83d2997 3293 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
5c8b7eaf 3294 (put-text-property (1+ bb) (1- e)
f83d2997 3295 'syntax-table cperl-string-syntax-table))
5bd52f0e
RS
3296 (cperl-protect-defun-start bb e))
3297 ;; Fontify
3298 (or noface
3299 (not cperl-pod-here-fontify)
3300 (put-text-property bb e 'face (if string 'font-lock-string-face
3301 'font-lock-comment-face)))))
6c389151 3302
5bd52f0e
RS
3303(defvar cperl-starters '(( ?\( . ?\) )
3304 ( ?\[ . ?\] )
3305 ( ?\{ . ?\} )
3306 ( ?\< . ?\> )))
f83d2997 3307
4ab89e7b
SM
3308(defun cperl-cached-syntax-table (st)
3309 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3310All the entries of the syntax table are \".\", except for a backslash, which
3311is quoting."
3312 (if (car-safe st)
3313 (car st)
3314 (setcar st (make-syntax-table))
3315 (setq st (car st))
3316 (let ((i 0))
3317 (while (< i 256)
3318 (modify-syntax-entry i "." st)
3319 (setq i (1+ i))))
3320 (modify-syntax-entry ?\\ "\\" st)
3321 st))
3322
3323(defun cperl-forward-re (lim end is-2arg st-l err-l argument
f83d2997 3324 &optional ostart oend)
4ab89e7b
SM
3325"Find the end of a regular expression or a stringish construct (q[] etc).
3326The point should be before the starting delimiter.
3327
3328Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3329is s/// or tr/// like expression. If END is nil, generates an error
3330message if needed. If SET-ST is non-nil, will use (or generate) a
3331cached syntax table in ST-L. If ERR-L is non-nil, will store the
3332error message in its CAR (unless it already contains some error
3333message). ARGUMENT should be the name of the construct (used in error
3334messages). OSTART, OEND may be set in recursive calls when processing
3335the second argument of 2ARG construct.
3336
3337Works *before* syntax recognition is done. In IS-2ARG situation may
3338modify syntax-type text property if the situation is too hard."
3339 (let (b starter ender st i i2 go-forward reset-st set-st)
f83d2997
KH
3340 (skip-chars-forward " \t")
3341 ;; ender means matching-char matcher.
5c8b7eaf 3342 (setq b (point)
5bd52f0e
RS
3343 starter (if (eobp) 0 (char-after b))
3344 ender (cdr (assoc starter cperl-starters)))
f83d2997 3345 ;; What if starter == ?\\ ????
4ab89e7b 3346 (setq st (cperl-cached-syntax-table st-l))
f83d2997
KH
3347 (setq set-st t)
3348 ;; Whether we have an intermediate point
3349 (setq i nil)
3350 ;; Prepare the syntax table:
4ab89e7b
SM
3351 (if (not ender) ; m/blah/, s/x//, s/x/y/
3352 (modify-syntax-entry starter "$" st)
3353 (modify-syntax-entry starter (concat "(" (list ender)) st)
3354 (modify-syntax-entry ender (concat ")" (list starter)) st))
f83d2997
KH
3355 (condition-case bb
3356 (progn
5bd52f0e
RS
3357 ;; We use `$' syntax class to find matching stuff, but $$
3358 ;; is recognized the same as $, so we need to check this manually.
f83d2997
KH
3359 (if (and (eq starter (char-after (cperl-1+ b)))
3360 (not ender))
3361 ;; $ has TeXish matching rules, so $$ equiv $...
3362 (forward-char 2)
6c389151 3363 (setq reset-st (syntax-table))
f83d2997
KH
3364 (set-syntax-table st)
3365 (forward-sexp 1)
6c389151
SM
3366 (if (<= (point) (1+ b))
3367 (error "Unfinished regular expression"))
3368 (set-syntax-table reset-st)
3369 (setq reset-st nil)
f83d2997
KH
3370 ;; Now the problem is with m;blah;;
3371 (and (not ender)
3372 (eq (preceding-char)
3373 (char-after (- (point) 2)))
3374 (save-excursion
3375 (forward-char -2)
3376 (= 0 (% (skip-chars-backward "\\\\") 2)))
3377 (forward-char -1)))
5bd52f0e 3378 ;; Now we are after the first part.
f83d2997
KH
3379 (and is-2arg ; Have trailing part
3380 (not ender)
3381 (eq (following-char) starter) ; Empty trailing part
3382 (progn
3383 (or (eq (char-syntax (following-char)) ?.)
3384 ;; Make trailing letter into punctuation
3385 (cperl-modify-syntax-type (point) cperl-st-punct))
3386 (setq is-2arg nil go-forward t))) ; Ignore the tail
3387 (if is-2arg ; Not number => have second part
3388 (progn
3389 (setq i (point) i2 i)
3390 (if ender
b5b0cb34 3391 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
f83d2997
KH
3392 (progn
3393 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3394 (goto-char (match-end 0))
3395 (skip-chars-forward " \t\n\f"))
3396 (setq i2 (point))))
3397 (forward-char -1))
3398 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
5c8b7eaf 3399 (if ender (modify-syntax-entry ender "." st))
f83d2997 3400 (setq set-st nil)
4ab89e7b 3401 (setq ender (cperl-forward-re lim end nil st-l err-l
5bd52f0e 3402 argument starter ender)
8c777c8d 3403 ender (nth 2 ender)))))
f83d2997
KH
3404 (error (goto-char lim)
3405 (setq set-st nil)
6c389151
SM
3406 (if reset-st
3407 (set-syntax-table reset-st))
f83d2997 3408 (or end
8c777c8d
CY
3409 (and cperl-brace-recursing
3410 (or (eq ostart ?\{)
3411 (eq starter ?\{)))
f83d2997 3412 (message
5bd52f0e 3413 "End of `%s%s%c ... %c' string/RE not found: %s"
f83d2997
KH
3414 argument
3415 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3416 starter (or ender starter) bb)
3417 (or (car err-l) (setcar err-l b)))))
3418 (if set-st
3419 (progn
3420 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3421 (if ender (modify-syntax-entry ender "." st))))
5bd52f0e 3422 ;; i: have 2 args, after end of the first arg
e7f767c2 3423 ;; i2: start of the second arg, if any (before delim if `ender').
5bd52f0e
RS
3424 ;; ender: the last arg bounded by parens-like chars, the second one of them
3425 ;; starter: the starting delimiter of the first arg
5efe6a56 3426 ;; go-forward: has 2 args, and the second part is empty
f83d2997
KH
3427 (list i i2 ender starter go-forward)))
3428
4ab89e7b
SM
3429(defun cperl-forward-group-in-re (&optional st-l)
3430 "Find the end of a group in a REx.
3431Return the error message (if any). Does not work if delimiter is `)'.
3432Works before syntax recognition is done."
3433 ;; Works *before* syntax recognition is done
3434 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3435 (let (st b reset-st)
3436 (condition-case b
3437 (progn
3438 (setq st (cperl-cached-syntax-table st-l))
3439 (modify-syntax-entry ?\( "()" st)
3440 (modify-syntax-entry ?\) ")(" st)
3441 (setq reset-st (syntax-table))
3442 (set-syntax-table st)
3443 (forward-sexp 1))
3444 (error (message
3445 "cperl-forward-group-in-re: error %s" b)))
3446 ;; now restore the initial state
3447 (if st
3448 (progn
3449 (modify-syntax-entry ?\( "." st)
3450 (modify-syntax-entry ?\) "." st)))
3451 (if reset-st
3452 (set-syntax-table reset-st))
3453 b))
3454
3455
83261a2f
SM
3456(defvar font-lock-string-face)
3457;;(defvar font-lock-reference-face)
3458(defvar font-lock-constant-face)
5c8b7eaf 3459(defsubst cperl-postpone-fontification (b e type val &optional now)
5bd52f0e
RS
3460 ;; Do after syntactic fontification?
3461 (if cperl-syntaxify-by-font-lock
3462 (or now (put-text-property b e 'cperl-postpone (cons type val)))
83261a2f 3463 (put-text-property b e type val)))
5bd52f0e
RS
3464
3465;;; Here is how the global structures (those which cannot be
3466;;; recognized locally) are marked:
5c8b7eaf 3467;; a) PODs:
5bd52f0e
RS
3468;; Start-to-end is marked `in-pod' ==> t
3469;; Each non-literal part is marked `syntax-type' ==> `pod'
3470;; Each literal part is marked `syntax-type' ==> `in-pod'
5c8b7eaf 3471;; b) HEREs:
5bd52f0e
RS
3472;; Start-to-end is marked `here-doc-group' ==> t
3473;; The body is marked `syntax-type' ==> `here-doc'
3474;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
5c8b7eaf 3475;; c) FORMATs:
f739b53b
SM
3476;; First line (to =) marked `first-format-line' ==> t
3477;; After-this--to-end is marked `syntax-type' ==> `format'
5c8b7eaf 3478;; d) 'Q'uoted string:
5bd52f0e 3479;; part between markers inclusive is marked `syntax-type' ==> `string'
6c389151 3480;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
4ab89e7b
SM
3481;; second part of s///e is marked `syntax-type' ==> `multiline'
3482;; e) Attributes of subroutines: `attrib-group' ==> t
3483;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3484;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3485
3486;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3487;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
5bd52f0e
RS
3488
3489(defun cperl-unwind-to-safe (before &optional end)
3490 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3491 (let ((pos (point)) opos)
4ab89e7b
SM
3492 (while (and pos (progn
3493 (beginning-of-line)
3494 (get-text-property (setq pos (point)) 'syntax-type)))
3495 (setq opos pos
3496 pos (cperl-beginning-of-property pos 'syntax-type))
3497 (if (eq pos (point-min))
3498 (setq pos nil))
5bd52f0e
RS
3499 (if pos
3500 (if before
3501 (progn
3502 (goto-char (cperl-1- pos))
3503 (beginning-of-line)
3504 (setq pos (point)))
3505 (goto-char (setq pos (cperl-1- pos))))
3506 ;; Up to the start
3507 (goto-char (point-min))))
6c389151
SM
3508 ;; Skip empty lines
3509 (and (looking-at "\n*=")
3510 (/= 0 (skip-chars-backward "\n"))
3511 (forward-char))
3512 (setq pos (point))
5bd52f0e
RS
3513 (if end
3514 ;; Do the same for end, going small steps
4ab89e7b 3515 (save-excursion
95bdccb7
SM
3516 (while (and end (< end (point-max))
3517 (get-text-property end 'syntax-type))
5bd52f0e 3518 (setq pos end
4ab89e7b
SM
3519 end (next-single-property-change end 'syntax-type nil (point-max)))
3520 (if end (progn (goto-char end)
3521 (or (bolp) (forward-line 1))
3522 (setq end (point)))))
5bd52f0e
RS
3523 (or end pos)))))
3524
4ab89e7b 3525;;; These are needed for byte-compile (at least with v19)
6c389151 3526(defvar cperl-nonoverridable-face)
4ab89e7b 3527(defvar font-lock-variable-name-face)
6c389151 3528(defvar font-lock-function-name-face)
4ab89e7b
SM
3529(defvar font-lock-keyword-face)
3530(defvar font-lock-builtin-face)
3531(defvar font-lock-type-face)
6c389151 3532(defvar font-lock-comment-face)
4ab89e7b 3533(defvar font-lock-warning-face)
6c389151 3534
4ab89e7b 3535(defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
bbd240ce 3536 "Syntactically mark (and fontify) attributes of a subroutine.
4ab89e7b
SM
3537Should be called with the point before leading colon of an attribute."
3538 ;; Works *before* syntax recognition is done
3539 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3540 (let (st b p reset-st after-first (start (point)) start1 end1)
3541 (condition-case b
3542 (while (looking-at
3543 (concat
3544 "\\(" ; 1=optional? colon
3545 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3546 "\\)"
3547 (if after-first "?" "")
3548 ;; No space between name and paren allowed...
3549 "\\(\\sw+\\)" ; 3=name
3550 "\\((\\)?")) ; 4=optional paren
3551 (and (match-beginning 1)
3552 (cperl-postpone-fontification
3553 (match-beginning 0) (cperl-1+ (match-beginning 0))
3554 'face font-lock-constant-face))
3555 (setq start1 (match-beginning 3) end1 (match-end 3))
3556 (cperl-postpone-fontification start1 end1
3557 'face font-lock-constant-face)
3558 (goto-char end1) ; end or before `('
3559 (if (match-end 4) ; Have attribute arguments...
3560 (progn
3561 (if st nil
3562 (setq st (cperl-cached-syntax-table st-l))
3563 (modify-syntax-entry ?\( "()" st)
3564 (modify-syntax-entry ?\) ")(" st))
3565 (setq reset-st (syntax-table) p (point))
3566 (set-syntax-table st)
3567 (forward-sexp 1)
3568 (set-syntax-table reset-st)
3569 (setq reset-st nil)
3570 (cperl-commentify p (point) t))) ; mark as string
3571 (forward-comment (buffer-size))
3572 (setq after-first t))
3573 (error (message
3574 "L%d: attribute `%s': %s"
3575 (count-lines (point-min) (point))
3576 (and start1 end1 (buffer-substring start1 end1)) b)
3577 (setq start nil)))
3578 (and start
3579 (progn
3580 (put-text-property start (point)
3581 'attrib-group (if (looking-at "{") t 0))
3582 (and pos
3583 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3584 ;; Apparently, we do not need `multiline': faces added now
3585 (put-text-property (+ 3 pos) (cperl-1+ (point))
3586 'syntax-type 'sub-decl))
3587 (and b-fname ; Fontify here: the following condition
3588 (cperl-postpone-fontification ; is too hard to determine by
3589 b-fname e-fname 'face ; a REx, so do it here
3590 (if (looking-at "{")
3591 font-lock-function-name-face
3592 font-lock-variable-name-face)))))
3593 ;; now restore the initial state
3594 (if st
3595 (progn
3596 (modify-syntax-entry ?\( "." st)
3597 (modify-syntax-entry ?\) "." st)))
3598 (if reset-st
3599 (set-syntax-table reset-st))))
3600
3601(defsubst cperl-look-at-leading-count (is-x-REx e)
82d9a08d
SM
3602 (if (and
3603 (< (point) e)
3604 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3605 (1- e) t)) ; return nil on failure, no moving
4ab89e7b
SM
3606 (if (eq ?\{ (preceding-char)) nil
3607 (cperl-postpone-fontification
3608 (1- (point)) (point)
3609 'face font-lock-warning-face))))
3610
8c777c8d
CY
3611;; Do some smarter-highlighting
3612;; XXXX Currently ignores alphanum/dash delims,
3613(defsubst cperl-highlight-charclass (endbracket dashface bsface onec-space)
3614 (let ((l '(1 5 7)) ll lle lll
3615 ;; 2 groups, the first takes the whole match (include \[trnfabe])
3616 (singleChar (concat "\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)")))
3617 (while ; look for unescaped - between non-classes
3618 (re-search-forward
3619 ;; On 19.33, certain simplifications lead
3620 ;; to bugs (as in [^a-z] \\| [trnfabe] )
3621 (concat ; 1: SingleChar (include \[trnfabe])
3622 singleChar
3623 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3624 "\\(" ; 3: DASH SingleChar (match optionally)
3625 "\\(-\\)" ; 4: DASH
3626 singleChar ; 5: SingleChar
3627 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3628 "\\)?"
3629 "\\|"
3630 "\\(" ; 7: other escapes
3631 "\\\\[pP]" "\\([^{]\\|{[^{}]*}\\)"
3632 "\\|" "\\\\[^pP]" "\\)"
3633 )
3634 endbracket 'toend)
3635 (if (match-beginning 4)
3636 (cperl-postpone-fontification
3637 (match-beginning 4) (match-end 4)
3638 'face dashface))
3639 ;; save match data (for looking-at)
3640 (setq lll (mapcar (function (lambda (elt) (cons (match-beginning elt)
3641 (match-end elt)))) l))
3642 (while lll
3643 (setq ll (car lll))
3644 (setq lle (cdr ll)
3645 ll (car ll))
3646 ;; (message "Got %s of %s" ll l)
3647 (if (and ll (eq (char-after ll) ?\\ ))
3648 (save-excursion
3649 (goto-char ll)
3650 (cperl-postpone-fontification ll (1+ ll)
3651 'face bsface)
3652 (if (looking-at "\\\\[a-zA-Z0-9]")
3653 (cperl-postpone-fontification (1+ ll) lle
3654 'face onec-space))))
3655 (setq lll (cdr lll))))
3656 (goto-char endbracket) ; just in case something misbehaves???
3657 t))
3658
4ab89e7b
SM
3659;;; Debugging this may require (setq max-specpdl-size 2000)...
3660(defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
f83d2997 3661 "Scans the buffer for hard-to-parse Perl constructions.
5c8b7eaf
SS
3662If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3663the sections using `cperl-pod-head-face', `cperl-pod-face',
f83d2997
KH
3664`cperl-here-face'."
3665 (interactive)
05927f8c 3666 (or min (setq min (point-min)
db133cb6
RS
3667 cperl-syntax-state nil
3668 cperl-syntax-done-to min))
f83d2997 3669 (or max (setq max (point-max)))
83261a2f
SM
3670 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3671 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
4ab89e7b 3672 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
83261a2f 3673 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
af1d43f9 3674 (modified (buffer-modified-p)) overshoot is-o-REx name
83261a2f 3675 (after-change-functions nil)
4ab89e7b 3676 (cperl-font-locking t)
83261a2f
SM
3677 (use-syntax-state (and cperl-syntax-state
3678 (>= min (car cperl-syntax-state))))
3679 (state-point (if use-syntax-state
3680 (car cperl-syntax-state)
3681 (point-min)))
3682 (state (if use-syntax-state
3683 (cdr cperl-syntax-state)))
3684 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3685 (st-l (list nil)) (err-l (list nil))
3686 ;; Somehow font-lock may be not loaded yet...
4ab89e7b 3687 ;; (e.g., when building TAGS via command-line call)
83261a2f
SM
3688 (font-lock-string-face (if (boundp 'font-lock-string-face)
3689 font-lock-string-face
3690 'font-lock-string-face))
4ab89e7b 3691 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
83261a2f
SM
3692 font-lock-constant-face
3693 'font-lock-constant-face))
4ab89e7b 3694 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
83261a2f
SM
3695 (if (boundp 'font-lock-function-name-face)
3696 font-lock-function-name-face
3697 'font-lock-function-name-face))
4ab89e7b
SM
3698 (font-lock-variable-name-face ; interpolated vars and ({})-code
3699 (if (boundp 'font-lock-variable-name-face)
3700 font-lock-variable-name-face
3701 'font-lock-variable-name-face))
3702 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3703 (if (boundp 'font-lock-function-name-face)
3704 font-lock-function-name-face
3705 'font-lock-function-name-face))
3706 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3707 (if (boundp 'font-lock-constant-face)
3708 font-lock-constant-face
3709 'font-lock-constant-face))
3710 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3711 (if (boundp 'font-lock-builtin-face)
3712 font-lock-builtin-face
3713 'font-lock-builtin-face))
83261a2f
SM
3714 (font-lock-comment-face
3715 (if (boundp 'font-lock-comment-face)
3716 font-lock-comment-face
3717 'font-lock-comment-face))
4ab89e7b
SM
3718 (font-lock-warning-face
3719 (if (boundp 'font-lock-warning-face)
3720 font-lock-warning-face
3721 'font-lock-warning-face))
3722 (my-cperl-REx-ctl-face ; (|)
3723 (if (boundp 'font-lock-keyword-face)
3724 font-lock-keyword-face
3725 'font-lock-keyword-face))
3726 (my-cperl-REx-modifiers-face ; //gims
83261a2f
SM
3727 (if (boundp 'cperl-nonoverridable-face)
3728 cperl-nonoverridable-face
4ab89e7b
SM
3729 'cperl-nonoverridable-face))
3730 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3731 (if (boundp 'font-lock-type-face)
3732 font-lock-type-face
3733 'font-lock-type-face))
83261a2f
SM
3734 (stop-point (if ignore-max
3735 (point-max)
3736 max))
3737 (search
3738 (concat
4ab89e7b 3739 "\\(\\`\n?\\|^\n\\)=" ; POD
83261a2f
SM
3740 "\\|"
3741 ;; One extra () before this:
4ab89e7b 3742 "<<" ; HERE-DOC
83261a2f
SM
3743 "\\(" ; 1 + 1
3744 ;; First variant "BLAH" or just ``.
3745 "[ \t]*" ; Yes, whitespace is allowed!
3746 "\\([\"'`]\\)" ; 2 + 1 = 3
3747 "\\([^\"'`\n]*\\)" ; 3 + 1
3748 "\\3"
3749 "\\|"
f739b53b 3750 ;; Second variant: Identifier or \ID (same as 'ID') or empty
83261a2f
SM
3751 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3752 ;; Do not have <<= or << 30 or <<30 or << $blah.
3753 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3754 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3755 "\\)"
3756 "\\|"
3757 ;; 1+6 extra () before this:
4ab89e7b 3758 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
83261a2f 3759 (if cperl-use-syntax-table-text-property
db133cb6 3760 (concat
db133cb6 3761 "\\|"
83261a2f 3762 ;; 1+6+2=9 extra () before this:
4ab89e7b 3763 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
83261a2f
SM
3764 "\\|"
3765 ;; 1+6+2+1=10 extra () before this:
3766 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3767 "\\|"
4ab89e7b
SM
3768 ;; 1+6+2+1+1=11 extra () before this
3769 "\\<sub\\>" ; sub with proto/attr
3770 "\\("
3771 cperl-white-and-comment-rex
3772 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3773 "\\("
3774 cperl-maybe-white-and-comment-rex
3775 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
83261a2f 3776 "\\|"
4ab89e7b
SM
3777 ;; 1+6+2+1+1+6=17 extra () before this:
3778 "\\$\\(['{]\\)" ; $' or ${foo}
83261a2f 3779 "\\|"
4ab89e7b
SM
3780 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3781 ;; we do not support intervening comments...):
83261a2f 3782 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
4ab89e7b 3783 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
83261a2f 3784 "\\|"
4ab89e7b
SM
3785 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3786 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
db133cb6 3787 "\\|"
4ab89e7b 3788 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
83261a2f 3789 ""))))
f83d2997
KH
3790 (unwind-protect
3791 (progn
3792 (save-excursion
3793 (or non-inter
3794 (message "Scanning for \"hard\" Perl constructions..."))
4ab89e7b 3795 ;;(message "find: %s --> %s" min max)
db133cb6 3796 (and cperl-pod-here-fontify
83261a2f
SM
3797 ;; We had evals here, do not know why...
3798 (setq face cperl-pod-face
3799 head-face cperl-pod-head-face
3800 here-face cperl-here-face))
5c8b7eaf 3801 (remove-text-properties min max
5bd52f0e 3802 '(syntax-type t in-pod t syntax-table t
4ab89e7b
SM
3803 attrib-group t
3804 REx-interpolated t
6c389151
SM
3805 cperl-postpone t
3806 syntax-subtype t
3807 rear-nonsticky t
4ab89e7b 3808 front-sticky t
f739b53b
SM
3809 here-doc-group t
3810 first-format-line t
4ab89e7b 3811 REx-part2 t
6c389151 3812 indentable t))
f83d2997
KH
3813 ;; Need to remove face as well...
3814 (goto-char min)
72bc50c0
GM
3815 ;; 'emx not supported by Emacs since at least 21.1.
3816 (and (featurep 'xemacs) (eq system-type 'emx)
4ab89e7b
SM
3817 (eq (point) 1)
3818 (let ((case-fold-search t))
3819 (looking-at "extproc[ \t]")) ; Analogue of #!
5c8b7eaf 3820 (cperl-commentify min
e180ab9f 3821 (point-at-eol)
db133cb6
RS
3822 nil))
3823 (while (and
3824 (< (point) max)
3825 (re-search-forward search max t))
5bd52f0e 3826 (setq tmpend nil) ; Valid for most cases
4ab89e7b
SM
3827 (setq b (match-beginning 0)
3828 state (save-excursion (parse-partial-sexp
3829 state-point b nil nil state))
3830 state-point b)
5c8b7eaf 3831 (cond
4ab89e7b
SM
3832 ;; 1+6+2+1+1+6=17 extra () before this:
3833 ;; "\\$\\(['{]\\)"
3834 ((match-beginning 18) ; $' or ${foo}
3835 (if (eq (preceding-char) ?\') ; $'
3836 (progn
3837 (setq b (1- (point))
3838 state (parse-partial-sexp
3839 state-point (1- b) nil nil state)
3840 state-point (1- b))
3841 (if (nth 3 state) ; in string
3842 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3843 (goto-char (1+ b)))
3844 ;; else: ${
3845 (setq bb (match-beginning 0))
3846 (cperl-modify-syntax-type bb cperl-st-punct)))
3847 ;; No processing in strings/comments beyond this point:
3848 ((or (nth 3 state) (nth 4 state))
3849 t) ; Do nothing in comment/string
f83d2997 3850 ((match-beginning 1) ; POD section
a1506d29 3851 ;; "\\(\\`\n?\\|^\n\\)="
4ab89e7b
SM
3852 (setq b (match-beginning 0)
3853 state (parse-partial-sexp
3854 state-point b nil nil state)
3855 state-point b)
3856 (if (or (nth 3 state) (nth 4 state)
3857 (looking-at "cut\\>"))
3858 (if (or (nth 3 state) (nth 4 state) ignore-max)
5bd52f0e 3859 nil ; Doing a chunk only
f83d2997
KH
3860 (message "=cut is not preceded by a POD section")
3861 (or (car err-l) (setcar err-l (point))))
3862 (beginning-of-line)
5c8b7eaf
SS
3863
3864 (setq b (point)
5bd52f0e
RS
3865 bb b
3866 tb (match-beginning 0)
3867 b1 nil) ; error condition
db133cb6
RS
3868 ;; We do not search to max, since we may be called from
3869 ;; some hook of fontification, and max is random
6c389151 3870 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
f83d2997 3871 (progn
6c389151
SM
3872 (goto-char b)
3873 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3874 (progn
3875 (message "=cut is not preceded by an empty line")
3876 (setq b1 t)
3877 (or (car err-l) (setcar err-l b))))))
f83d2997
KH
3878 (beginning-of-line 2) ; An empty line after =cut is not POD!
3879 (setq e (point))
db133cb6 3880 (and (> e max)
6c389151 3881 (progn
a1506d29 3882 (remove-text-properties
6c389151 3883 max e '(syntax-type t in-pod t syntax-table t
4ab89e7b
SM
3884 attrib-group t
3885 REx-interpolated t
6c389151
SM
3886 cperl-postpone t
3887 syntax-subtype t
f739b53b 3888 here-doc-group t
6c389151 3889 rear-nonsticky t
4ab89e7b 3890 front-sticky t
f739b53b 3891 first-format-line t
4ab89e7b 3892 REx-part2 t
6c389151
SM
3893 indentable t))
3894 (setq tmpend tb)))
f83d2997 3895 (put-text-property b e 'in-pod t)
6c389151 3896 (put-text-property b e 'syntax-type 'in-pod)
f83d2997
KH
3897 (goto-char b)
3898 (while (re-search-forward "\n\n[ \t]" e t)
3899 ;; We start 'pod 1 char earlier to include the preceding line
3900 (beginning-of-line)
3901 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
5efe6a56
SM
3902 (cperl-put-do-not-fontify b (point) t)
3903 ;; mark the non-literal parts as PODs
a1506d29 3904 (if cperl-pod-here-fontify
5efe6a56 3905 (cperl-postpone-fontification b (point) 'face face t))
f83d2997
KH
3906 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3907 (beginning-of-line)
3908 (setq b (point)))
3909 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
5efe6a56 3910 (cperl-put-do-not-fontify (point) e t)
a1506d29
JB
3911 (if cperl-pod-here-fontify
3912 (progn
5efe6a56
SM
3913 ;; mark the non-literal parts as PODs
3914 (cperl-postpone-fontification (point) e 'face face t)
3915 (goto-char bb)
a1506d29 3916 (if (looking-at
5efe6a56
SM
3917 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3918 ;; mark the headers
a1506d29 3919 (cperl-postpone-fontification
5efe6a56 3920 (match-beginning 1) (match-end 1)
6c389151
SM
3921 'face head-face))
3922 (while (re-search-forward
3923 ;; One paragraph
3924 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3925 e 'toend)
3926 ;; mark the headers
a1506d29 3927 (cperl-postpone-fontification
5efe6a56
SM
3928 (match-beginning 1) (match-end 1)
3929 'face head-face))))
f83d2997
KH
3930 (cperl-commentify bb e nil)
3931 (goto-char e)
3932 (or (eq e (point-max))
83261a2f 3933 (forward-char -1)))) ; Prepare for immediate POD start.
f83d2997 3934 ;; Here document
4ab89e7b
SM
3935 ;; We can do many here-per-line;
3936 ;; but multiline quote on the same line as <<HERE confuses us...
5bd52f0e 3937 ;; ;; One extra () before this:
5c8b7eaf 3938 ;;"<<"
5bd52f0e
RS
3939 ;; "\\(" ; 1 + 1
3940 ;; ;; First variant "BLAH" or just ``.
f739b53b 3941 ;; "[ \t]*" ; Yes, whitespace is allowed!
5bd52f0e
RS
3942 ;; "\\([\"'`]\\)" ; 2 + 1
3943 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3944 ;; "\\3"
3945 ;; "\\|"
3946 ;; ;; Second variant: Identifier or \ID or empty
3947 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3948 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3949 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3950 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3951 ;; "\\)"
f83d2997 3952 ((match-beginning 2) ; 1 + 1
4ab89e7b 3953 (setq b (point)
5bd52f0e 3954 tb (match-beginning 0)
4ab89e7b
SM
3955 c (and ; not HERE-DOC
3956 (match-beginning 5)
3957 (save-match-data
3958 (or (looking-at "[ \t]*(") ; << function_call()
3959 (save-excursion ; 1 << func_name, or $foo << 10
3960 (condition-case nil
3961 (progn
3962 (goto-char tb)
3963 ;;; XXX What to do: foo <<bar ???
3964 ;;; XXX Need to support print {a} <<B ???
3965 (forward-sexp -1)
cb5bf6ba 3966 (save-match-data
4ab89e7b
SM
3967 ; $foo << b; $f .= <<B;
3968 ; ($f+1) << b; a($f) . <<B;
3969 ; foo 1, <<B; $x{a} <<b;
3970 (cond
3971 ((looking-at "[0-9$({]")
3972 (forward-sexp 1)
3973 (and
3974 (looking-at "[ \t]*<<")
3975 (condition-case nil
3976 ;; print $foo <<EOF
3977 (progn
3978 (forward-sexp -2)
3979 (not
3980 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
3981 (error t)))))))
3982 (error nil))) ; func(<<EOF)
3983 (and (not (match-beginning 6)) ; Empty
3984 (looking-at
3985 "[ \t]*[=0-9$@%&(]"))))))
5bd52f0e
RS
3986 (if c ; Not here-doc
3987 nil ; Skip it.
4ab89e7b 3988 (setq c (match-end 2)) ; 1 + 1
f83d2997
KH
3989 (if (match-beginning 5) ;4 + 1
3990 (setq b1 (match-beginning 5) ; 4 + 1
3991 e1 (match-end 5)) ; 4 + 1
3992 (setq b1 (match-beginning 4) ; 3 + 1
3993 e1 (match-end 4))) ; 3 + 1
3994 (setq tag (buffer-substring b1 e1)
3995 qtag (regexp-quote tag))
5c8b7eaf 3996 (cond (cperl-pod-here-fontify
5bd52f0e 3997 ;; Highlight the starting delimiter
cb5bf6ba 3998 (cperl-postpone-fontification
4ab89e7b 3999 b1 e1 'face my-cperl-delimiters-face)
5bd52f0e 4000 (cperl-put-do-not-fontify b1 e1 t)))
f83d2997 4001 (forward-line)
4ab89e7b
SM
4002 (setq i (point))
4003 (if end-of-here-doc
4004 (goto-char end-of-here-doc))
f83d2997 4005 (setq b (point))
db133cb6
RS
4006 ;; We do not search to max, since we may be called from
4007 ;; some hook of fontification, and max is random
f739b53b
SM
4008 (or (and (re-search-forward (concat "^" qtag "$")
4009 stop-point 'toend)
4ab89e7b
SM
4010 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4011 )
f739b53b
SM
4012 (progn ; Pretend we matched at the end
4013 (goto-char (point-max))
4014 (re-search-forward "\\'")
4015 (message "End of here-document `%s' not found." tag)
4016 (or (car err-l) (setcar err-l b))))
4017 (if cperl-pod-here-fontify
4018 (progn
4019 ;; Highlight the ending delimiter
4ab89e7b
SM
4020 (cperl-postpone-fontification
4021 (match-beginning 0) (match-end 0)
4022 'face my-cperl-delimiters-face)
f739b53b
SM
4023 (cperl-put-do-not-fontify b (match-end 0) t)
4024 ;; Highlight the HERE-DOC
4025 (cperl-postpone-fontification b (match-beginning 0)
4026 'face here-face)))
4027 (setq e1 (cperl-1+ (match-end 0)))
4028 (put-text-property b (match-beginning 0)
4029 'syntax-type 'here-doc)
4030 (put-text-property (match-beginning 0) e1
4031 'syntax-type 'here-doc-delim)
4ab89e7b
SM
4032 (put-text-property b e1 'here-doc-group t)
4033 ;; This makes insertion at the start of HERE-DOC update
4034 ;; the whole construct:
4035 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
f739b53b
SM
4036 (cperl-commentify b e1 nil)
4037 (cperl-put-do-not-fontify b (match-end 0) t)
4ab89e7b
SM
4038 ;; Cache the syntax info...
4039 (setq cperl-syntax-state (cons state-point state))
4040 ;; ... and process the rest of the line...
4041 (setq overshoot
4042 (elt ; non-inter ignore-max
4043 (cperl-find-pods-heres c i t end t e1) 1))
4044 (if (and overshoot (> overshoot (point)))
4045 (goto-char overshoot)
4046 (setq overshoot e1))
f739b53b
SM
4047 (if (> e1 max)
4048 (setq tmpend tb))))
f83d2997
KH
4049 ;; format
4050 ((match-beginning 8)
4051 ;; 1+6=7 extra () before this:
4052 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4053 (setq b (point)
4054 name (if (match-beginning 8) ; 7 + 1
4055 (buffer-substring (match-beginning 8) ; 7 + 1
4056 (match-end 8)) ; 7 + 1
5bd52f0e
RS
4057 "")
4058 tb (match-beginning 0))
f83d2997 4059 (setq argument nil)
9b026d9f 4060 (put-text-property (point-at-bol) b 'first-format-line 't)
5c8b7eaf 4061 (if cperl-pod-here-fontify
f83d2997
KH
4062 (while (and (eq (forward-line) 0)
4063 (not (looking-at "^[.;]$")))
4064 (cond
4065 ((looking-at "^#")) ; Skip comments
4066 ((and argument ; Skip argument multi-lines
5c8b7eaf 4067 (looking-at "^[ \t]*{"))
f83d2997
KH
4068 (forward-sexp 1)
4069 (setq argument nil))
4070 (argument ; Skip argument lines
4071 (setq argument nil))
4072 (t ; Format line
4073 (setq b1 (point))
4074 (setq argument (looking-at "^[^\n]*[@^]"))
4075 (end-of-line)
5bd52f0e 4076 ;; Highlight the format line
5c8b7eaf 4077 (cperl-postpone-fontification b1 (point)
83261a2f 4078 'face font-lock-string-face)
f83d2997 4079 (cperl-commentify b1 (point) nil)
5bd52f0e 4080 (cperl-put-do-not-fontify b1 (point) t))))
db133cb6
RS
4081 ;; We do not search to max, since we may be called from
4082 ;; some hook of fontification, and max is random
4083 (re-search-forward "^[.;]$" stop-point 'toend))
f83d2997 4084 (beginning-of-line)
83261a2f 4085 (if (looking-at "^\\.$") ; ";" is not supported yet
f83d2997 4086 (progn
5bd52f0e
RS
4087 ;; Highlight the ending delimiter
4088 (cperl-postpone-fontification (point) (+ (point) 2)
83261a2f 4089 'face font-lock-string-face)
f83d2997 4090 (cperl-commentify (point) (+ (point) 2) nil)
5bd52f0e 4091 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
f83d2997
KH
4092 (message "End of format `%s' not found." name)
4093 (or (car err-l) (setcar err-l b)))
4094 (forward-line)
5bd52f0e
RS
4095 (if (> (point) max)
4096 (setq tmpend tb))
db133cb6 4097 (put-text-property b (point) 'syntax-type 'format))
4ab89e7b 4098 ;; qq-like String or Regexp:
f83d2997
KH
4099 ((or (match-beginning 10) (match-beginning 11))
4100 ;; 1+6+2=9 extra () before this:
5bd52f0e 4101 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
f83d2997 4102 ;; "\\|"
5bd52f0e 4103 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
f83d2997
KH
4104 (setq b1 (if (match-beginning 10) 10 11)
4105 argument (buffer-substring
4106 (match-beginning b1) (match-end b1))
4ab89e7b 4107 b (point) ; end of qq etc
f83d2997
KH
4108 i b
4109 c (char-after (match-beginning b1))
4ab89e7b 4110 bb (char-after (1- (match-beginning b1))) ; tmp holder
5bd52f0e
RS
4111 ;; bb == "Not a stringy"
4112 bb (if (eq b1 10) ; user variables/whatever
f739b53b
SM
4113 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4114 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4115 ((eq bb ?\:) ; $opt::s
4116 (eq (char-after
4117 (- (match-beginning b1) 2))
4118 ?\:))
4119 ((eq bb ?\>) ; $foo->s
4120 (eq (char-after
4121 (- (match-beginning b1) 2))
4122 ?\-))
4123 ((eq bb ?\&)
4ab89e7b 4124 (not (eq (char-after ; &&m/blah/
f739b53b
SM
4125 (- (match-beginning b1) 2))
4126 ?\&)))
4127 (t t)))
5bd52f0e
RS
4128 ;; <file> or <$file>
4129 (and (eq c ?\<)
6c389151 4130 ;; Do not stringify <FH>, <$fh> :
5bd52f0e 4131 (save-match-data
5c8b7eaf 4132 (looking-at
6c389151 4133 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
5bd52f0e 4134 tb (match-beginning 0))
db133cb6
RS
4135 (goto-char (match-beginning b1))
4136 (cperl-backward-to-noncomment (point-min))
f83d2997 4137 (or bb
5bd52f0e 4138 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
f83d2997 4139 (setq argument ""
f739b53b 4140 b1 nil
db133cb6 4141 bb ; Not a regexp?
4ab89e7b
SM
4142 (not
4143 ;; What is below: regexp-p?
4144 (and
4145 (or (memq (preceding-char)
4146 (append (if (memq c '(?\? ?\<))
4147 ;; $a++ ? 1 : 2
4148 "~{(=|&*!,;:["
4149 "~{(=|&+-*!,;:[") nil))
4150 (and (eq (preceding-char) ?\})
4151 (cperl-after-block-p (point-min)))
4152 (and (eq (char-syntax (preceding-char)) ?w)
4153 (progn
4154 (forward-sexp -1)
6c389151
SM
4155;; After these keywords `/' starts a RE. One should add all the
4156;; functions/builtins which expect an argument, but ...
4ab89e7b
SM
4157 (if (eq (preceding-char) ?-)
4158 ;; -d ?foo? is a RE
4159 (looking-at "[a-zA-Z]\\>")
4160 (and
4161 (not (memq (preceding-char)
4162 '(?$ ?@ ?& ?%)))
4163 (looking-at
4164 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4165 (and (eq (preceding-char) ?.)
4166 (eq (char-after (- (point) 2)) ?.))
4167 (bobp))
4168 ;; m|blah| ? foo : bar;
4169 (not
4170 (and (eq c ?\?)
4171 cperl-use-syntax-table-text-property
4172 (not (bobp))
4173 (progn
4174 (forward-char -1)
4175 (looking-at "\\s|"))))))
db133cb6
RS
4176 b (1- b))
4177 ;; s y tr m
f739b53b
SM
4178 ;; Check for $a -> y
4179 (setq b1 (preceding-char)
4180 go (point))
4181 (if (and (eq b1 ?>)
4182 (eq (char-after (- go 2)) ?-))
db133cb6
RS
4183 ;; Not a regexp
4184 (setq bb t))))
f739b53b
SM
4185 (or bb
4186 (progn
4ab89e7b 4187 (goto-char b)
f739b53b
SM
4188 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4189 (goto-char (match-end 0))
4190 (skip-chars-forward " \t\n\f"))
4191 (cond ((and (eq (following-char) ?\})
4192 (eq b1 ?\{))
4193 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4194 (goto-char (1- go))
4195 (skip-chars-backward " \t\n\f")
4196 (if (memq (preceding-char) (append "$@%&*" nil))
4197 (setq bb t) ; @{y}
4198 (condition-case nil
4199 (forward-sexp -1)
4200 (error nil)))
4201 (if (or bb
4202 (looking-at ; $foo -> {s}
4203 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4204 (and ; $foo[12] -> {s}
4205 (memq (following-char) '(?\{ ?\[))
4206 (progn
4207 (forward-sexp 1)
4208 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4209 (setq bb t)
4210 (goto-char b)))
4211 ((and (eq (following-char) ?=)
4212 (eq (char-after (1+ (point))) ?\>))
4213 ;; Check for { foo => 1, s => 2 }
4214 ;; Apparently s=> is never a substitution...
4215 (setq bb t))
4216 ((and (eq (following-char) ?:)
4217 (eq b1 ?\{) ; Check for $ { s::bar }
4218 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
15ca5699 4219 (progn
f739b53b
SM
4220 (goto-char (1- go))
4221 (skip-chars-backward " \t\n\f")
4222 (memq (preceding-char)
4223 (append "$@%&*" nil))))
4ab89e7b
SM
4224 (setq bb t))
4225 ((eobp)
f739b53b
SM
4226 (setq bb t)))))
4227 (if bb
f83d2997 4228 (goto-char i)
6c389151 4229 ;; Skip whitespace and comments...
f83d2997
KH
4230 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4231 (goto-char (match-end 0))
4232 (skip-chars-forward " \t\n\f"))
6c389151
SM
4233 (if (> (point) b)
4234 (put-text-property b (point) 'syntax-type 'prestring))
f83d2997
KH
4235 ;; qtag means two-arg matcher, may be reset to
4236 ;; 2 or 3 later if some special quoting is needed.
4237 ;; e1 means matching-char matcher.
4ab89e7b 4238 (setq b (point) ; before the first delimiter
5bd52f0e
RS
4239 ;; has 2 args
4240 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
db133cb6
RS
4241 ;; We do not search to max, since we may be called from
4242 ;; some hook of fontification, and max is random
4243 i (cperl-forward-re stop-point end
5bd52f0e 4244 i2
4ab89e7b
SM
4245 st-l err-l argument)
4246 ;; If `go', then it is considered as 1-arg, `b1' is nil
4247 ;; as in s/foo//x; the point is before final "slash"
5bd52f0e 4248 b1 (nth 1 i) ; start of the second part
5c8b7eaf 4249 tag (nth 2 i) ; ender-char, true if second part
5bd52f0e 4250 ; is with matching chars []
f83d2997
KH
4251 go (nth 4 i) ; There is a 1-char part after the end
4252 i (car i) ; intermediate point
5c8b7eaf 4253 e1 (point) ; end
5bd52f0e 4254 ;; Before end of the second part if non-matching: ///
5c8b7eaf 4255 tail (if (and i (not tag))
5bd52f0e
RS
4256 (1- e1))
4257 e (if i i e1) ; end of the first part
6c389151 4258 qtag nil ; need to preserve backslashitis
4ab89e7b
SM
4259 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4260 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
f83d2997
KH
4261 ;; Commenting \\ is dangerous, what about ( ?
4262 (and i tail
4263 (eq (char-after i) ?\\)
5bd52f0e 4264 (setq qtag t))
4ab89e7b
SM
4265 (and (if go (looking-at ".\\sw*x")
4266 (looking-at "\\sw*x")) ; qr//x
4267 (setq is-x-REx t))
4268 (and (if go (looking-at ".\\sw*o")
4269 (looking-at "\\sw*o")) ; //o
4270 (setq is-o-REx t))
f83d2997 4271 (if (null i)
5bd52f0e 4272 ;; Considered as 1arg form
f83d2997
KH
4273 (progn
4274 (cperl-commentify b (point) t)
5bd52f0e 4275 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
4276 (if (or is-x-REx
4277 ;; ignore other text properties:
4278 (string-match "^qw$" argument))
4279 (put-text-property b (point) 'indentable t))
5bd52f0e
RS
4280 (and go
4281 (setq e1 (cperl-1+ e1))
4282 (or (eobp)
4283 (forward-char 1))))
f83d2997
KH
4284 (cperl-commentify b i t)
4285 (if (looking-at "\\sw*e") ; s///e
4286 (progn
4ab89e7b
SM
4287 ;; Cache the syntax info...
4288 (setq cperl-syntax-state (cons state-point state))
f83d2997
KH
4289 (and
4290 ;; silent:
4ab89e7b 4291 (car (cperl-find-pods-heres b1 (1- (point)) t end))
f83d2997
KH
4292 ;; Error
4293 (goto-char (1+ max)))
5bd52f0e 4294 (if (and tag (eq (preceding-char) ?\>))
f83d2997
KH
4295 (progn
4296 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
5bd52f0e 4297 (cperl-modify-syntax-type i cperl-st-bra)))
6c389151 4298 (put-text-property b i 'syntax-type 'string)
4ab89e7b 4299 (put-text-property i (point) 'syntax-type 'multiline)
6c389151
SM
4300 (if is-x-REx
4301 (put-text-property b i 'indentable t)))
5bd52f0e
RS
4302 (cperl-commentify b1 (point) t)
4303 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
4304 (if is-x-REx
4305 (put-text-property b i 'indentable t))
5bd52f0e 4306 (if qtag
db133cb6 4307 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
f83d2997 4308 (setq tail nil)))
5bd52f0e 4309 ;; Now: tail: if the second part is non-matching without ///e
f83d2997
KH
4310 (if (eq (char-syntax (following-char)) ?w)
4311 (progn
4312 (forward-word 1) ; skip modifiers s///s
5bd52f0e 4313 (if tail (cperl-commentify tail (point) t))
a1506d29 4314 (cperl-postpone-fontification
4ab89e7b 4315 e1 (point) 'face my-cperl-REx-modifiers-face)))
5bd52f0e
RS
4316 ;; Check whether it is m// which means "previous match"
4317 ;; and highlight differently
a1506d29 4318 (setq is-REx
6c389151
SM
4319 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4320 (or (not (= (length argument) 0))
4321 (not (eq c ?\<)))))
a1506d29 4322 (if (and is-REx
6c389151 4323 (eq e (+ 2 b))
5bd52f0e
RS
4324 ;; split // *is* using zero-pattern
4325 (save-excursion
4326 (condition-case nil
4327 (progn
4328 (goto-char tb)
4329 (forward-sexp -1)
4330 (not (looking-at "split\\>")))
4331 (error t))))
5c8b7eaf 4332 (cperl-postpone-fontification
4ab89e7b 4333 b e 'face font-lock-warning-face)
5bd52f0e
RS
4334 (if (or i2 ; Has 2 args
4335 (and cperl-fontify-m-as-s
4336 (or
4337 (string-match "^\\(m\\|qr\\)$" argument)
4338 (and (eq 0 (length argument))
4339 (not (eq ?\< (char-after b)))))))
4340 (progn
5c8b7eaf 4341 (cperl-postpone-fontification
4ab89e7b 4342 b (cperl-1+ b) 'face my-cperl-delimiters-face)
5c8b7eaf 4343 (cperl-postpone-fontification
4ab89e7b 4344 (1- e) e 'face my-cperl-delimiters-face)))
6c389151 4345 (if (and is-REx cperl-regexp-scan)
4ab89e7b
SM
4346 ;; Process RExen: embedded comments, charclasses and ]
4347;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4348;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4349;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4350;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4351;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4352;;;m^a[\^b]c^ + m.a[^b]\.c.;
6c389151
SM
4353 (save-excursion
4354 (goto-char (1+ b))
cb5bf6ba 4355 ;; First
4ab89e7b
SM
4356 (cperl-look-at-leading-count is-x-REx e)
4357 (setq hairy-RE
4358 (concat
4359 (if is-x-REx
4360 (if (eq (char-after b) ?\#)
4361 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4362 "\\((\\?#\\)\\|\\(#\\)")
4363 ;; keep the same count: add a fake group
4364 (if (eq (char-after b) ?\#)
4365 "\\((\\?\\\\#\\)\\(\\)"
4366 "\\((\\?#\\)\\(\\)"))
4367 "\\|"
4368 "\\(\\[\\)" ; 3=[
4369 "\\|"
4370 "\\(]\\)" ; 4=]
4371 "\\|"
4372 ;; XXXX Will not be able to use it in s)))
4373 (if (eq (char-after b) ?\) )
4374 "\\())))\\)" ; Will never match
4375 (if (eq (char-after b) ?? )
4376 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4377 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4378 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4379 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4380 "\\(" ;; XXXX 1-char variables, exc. |()\s
4381 "[$@]"
4382 "\\("
4383 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4384 "\\|"
4385 "{[^{}]*}" ; only one-level allowed
4386 "\\|"
4387 "[^{(|) \t\r\n\f]"
4388 "\\)"
4389 "\\(" ;;8,9:code part of array/hash elt
4390 "\\(" "->" "\\)?"
4391 "\\[[^][]*\\]"
4392 "\\|"
4393 "{[^{}]*}"
4394 "\\)*"
4395 ;; XXXX: what if u is delim?
4396 "\\|"
4397 "[)^|$.*?+]"
4398 "\\|"
4399 "{[0-9]+}"
4400 "\\|"
4401 "{[0-9]+,[0-9]*}"
4402 "\\|"
4403 "\\\\[luLUEQbBAzZG]"
4404 "\\|"
4405 "(" ; Group opener
4406 "\\(" ; 10 group opener follower
4407 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4408 "\\|"
4409 "\\?[:=!>?{]" ; "?" something
4410 "\\|"
4411 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4412 "\\|"
4413 "\\?([0-9]+)" ; (?(1)foo|bar)
4414 "\\|"
4415 "\\?<[=!]"
4416 ;;;"\\|"
4417 ;;; "\\?"
4418 "\\)?"
4419 "\\)"
4420 "\\|"
4421 "\\\\\\(.\\)" ; 12=\SYMBOL
4422 ))
6c389151 4423 (while
4ab89e7b
SM
4424 (and (< (point) (1- e))
4425 (re-search-forward hairy-RE (1- e) 'to-end))
6c389151 4426 (goto-char (match-beginning 0))
4ab89e7b
SM
4427 (setq REx-subgr-start (point)
4428 was-subgr (following-char))
4429 (cond
4430 ((match-beginning 6) ; 0-length builtins, groups
4431 (goto-char (match-end 0))
4432 (if (match-beginning 11)
4433 (goto-char (match-beginning 11)))
4434 (if (>= (point) e)
4435 (goto-char (1- e)))
4436 (cperl-postpone-fontification
4437 (match-beginning 0) (point)
4438 'face
4439 (cond
4440 ((eq was-subgr ?\) )
4441 (condition-case nil
4442 (save-excursion
4443 (forward-sexp -1)
4444 (if (> (point) b)
4445 (if (if (eq (char-after b) ?? )
4446 (looking-at "(\\\\\\?")
4447 (eq (char-after (1+ (point))) ?\?))
4448 my-cperl-REx-0length-face
4449 my-cperl-REx-ctl-face)
4450 font-lock-warning-face))
4451 (error font-lock-warning-face)))
4452 ((eq was-subgr ?\| )
4453 my-cperl-REx-ctl-face)
4454 ((eq was-subgr ?\$ )
4455 (if (> (point) (1+ REx-subgr-start))
4456 (progn
4457 (put-text-property
4458 (match-beginning 0) (point)
4459 'REx-interpolated
4460 (if is-o-REx 0
4461 (if (and (eq (match-beginning 0)
4462 (1+ b))
4463 (eq (point)
4464 (1- e))) 1 t)))
4465 font-lock-variable-name-face)
4466 my-cperl-REx-spec-char-face))
4467 ((memq was-subgr (append "^." nil) )
4468 my-cperl-REx-spec-char-face)
4469 ((eq was-subgr ?\( )
4470 (if (not (match-beginning 10))
4471 my-cperl-REx-ctl-face
4472 my-cperl-REx-0length-face))
4473 (t my-cperl-REx-0length-face)))
4474 (if (and (memq was-subgr (append "(|" nil))
4475 (not (string-match "(\\?[-imsx]+)"
4476 (match-string 0))))
4477 (cperl-look-at-leading-count is-x-REx e))
4478 (setq was-subgr nil)) ; We do stuff here
4479 ((match-beginning 12) ; \SYMBOL
4480 (forward-char 2)
4481 (if (>= (point) e)
4482 (goto-char (1- e))
4483 ;; How many chars to not highlight:
4484 ;; 0-len special-alnums in other branch =>
4485 ;; Generic: \non-alnum (1), \alnum (1+face)
4486 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4487 (setq REx-subgr-start (point)
4488 qtag (preceding-char))
4489 (cperl-postpone-fontification
4490 (- (point) 2) (- (point) 1) 'face
4491 (if (memq qtag
4492 (append "ghijkmoqvFHIJKMORTVY" nil))
4493 font-lock-warning-face
4494 my-cperl-REx-0length-face))
4495 (if (and (eq (char-after b) qtag)
4496 (memq qtag (append ".])^$|*?+" nil)))
4497 (progn
4498 (if (and cperl-use-syntax-table-text-property
4499 (eq qtag ?\) ))
4500 (put-text-property
4501 REx-subgr-start (1- (point))
4502 'syntax-table cperl-st-punct))
4503 (cperl-postpone-fontification
4504 (1- (point)) (point) 'face
4505 ; \] can't appear below
4506 (if (memq qtag (append ".]^$" nil))
4507 'my-cperl-REx-spec-char-face
4508 (if (memq qtag (append "*?+" nil))
4509 'my-cperl-REx-0length-face
4510 'my-cperl-REx-ctl-face))))) ; )|
4511 ;; Test for arguments:
4512 (cond
4513 ;; This is not pretty: the 5.8.7 logic:
4514 ;; \0numx -> octal (up to total 3 dig)
4515 ;; \DIGIT -> backref unless \0
cb5bf6ba 4516 ;; \DIGITs -> backref if valid
4ab89e7b
SM
4517 ;; otherwise up to 3 -> octal
4518 ;; Do not try to distinguish, we guess
4519 ((or (and (memq qtag (append "01234567" nil))
4520 (re-search-forward
4521 "\\=[01234567]?[01234567]?"
4522 (1- e) 'to-end))
4523 (and (memq qtag (append "89" nil))
cb5bf6ba 4524 (re-search-forward
4ab89e7b
SM
4525 "\\=[0123456789]*" (1- e) 'to-end))
4526 (and (eq qtag ?x)
4527 (re-search-forward
4528 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4529 (1- e) 'to-end))
4530 (and (memq qtag (append "pPN" nil))
4531 (re-search-forward "\\={[^{}]+}\\|."
4532 (1- e) 'to-end))
4533 (eq (char-syntax qtag) ?w))
4534 (cperl-postpone-fontification
4535 (1- REx-subgr-start) (point)
4536 'face my-cperl-REx-length1-face))))
4537 (setq was-subgr nil)) ; We do stuff here
4538 ((match-beginning 3) ; [charclass]
8c777c8d 4539 ;; Highlight leader, trailer, POSIX classes
4ab89e7b
SM
4540 (forward-char 1)
4541 (if (eq (char-after b) ?^ )
4542 (and (eq (following-char) ?\\ )
4543 (eq (char-after (cperl-1+ (point)))
4544 ?^ )
4545 (forward-char 2))
4546 (and (eq (following-char) ?^ )
4547 (forward-char 1)))
8c777c8d 4548 (setq argument b ; continue? & end of last POSIX
4ab89e7b 4549 tag nil ; list of POSIX classes
8c777c8d 4550 qtag (point)) ; after leading ^ if present
4ab89e7b
SM
4551 (if (eq (char-after b) ?\] )
4552 (and (eq (following-char) ?\\ )
4553 (eq (char-after (cperl-1+ (point)))
4554 ?\] )
4555 (setq qtag (1+ qtag))
4556 (forward-char 2))
4557 (and (eq (following-char) ?\] )
4558 (forward-char 1)))
3ed8598c 4559 (setq REx-subgr-end qtag) ;End smart-highlighted
4ab89e7b
SM
4560 ;; Apparently, I can't put \] into a charclass
4561 ;; in m]]: m][\\\]\]] produces [\\]]
4562;;; POSIX? [:word:] [:^word:] only inside []
8c777c8d
CY
4563;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4564 (while ; look for unescaped ]
4ab89e7b
SM
4565 (and argument
4566 (re-search-forward
4567 (if (eq (char-after b) ?\] )
4568 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4569 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4570 (1- e) 'toend))
4571 ;; Is this ] an end of POSIX class?
4572 (if (save-excursion
4573 (and
4574 (search-backward "[" argument t)
4575 (< REx-subgr-start (point))
8c777c8d
CY
4576 (setq argument (point)) ; POSIX-start
4577 (or ; Should work with delim = \
4578 (not (eq (preceding-char) ?\\ ))
4579 ;; XXXX Double \\ is needed with 19.33
4580 (= (% (skip-chars-backward "\\\\") 2) 0))
4ab89e7b
SM
4581 (looking-at
4582 (cond
4583 ((eq (char-after b) ?\] )
4584 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4585 ((eq (char-after b) ?\: )
4586 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4587 ((eq (char-after b) ?^ )
4588 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4589 ((eq (char-syntax (char-after b))
4590 ?w)
4591 (concat
4592 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4593 (char-to-string (char-after b))
4594 "\\|\\sw\\)+:\]"))
4595 (t "\\\\*\\[:\\^?\\sw*:]")))
8c777c8d
CY
4596 (goto-char REx-subgr-end)
4597 (cperl-highlight-charclass
4598 argument my-cperl-REx-spec-char-face
4599 my-cperl-REx-0length-face my-cperl-REx-length1-face)))
4ab89e7b
SM
4600 (setq tag (cons (cons argument (point))
4601 tag)
8c777c8d
CY
4602 argument (point)
4603 REx-subgr-end argument) ; continue
4ab89e7b
SM
4604 (setq argument nil)))
4605 (and argument
4606 (message "Couldn't find end of charclass in a REx, pos=%s"
4607 REx-subgr-start))
8c777c8d
CY
4608 (setq argument (1- (point)))
4609 (goto-char REx-subgr-end)
4610 (cperl-highlight-charclass
4611 argument my-cperl-REx-spec-char-face
4612 my-cperl-REx-0length-face my-cperl-REx-length1-face)
4613 (forward-char 1)
4614 ;; Highlight starter, trailer, POSIX
4ab89e7b
SM
4615 (if (and cperl-use-syntax-table-text-property
4616 (> (- (point) 2) REx-subgr-start))
4617 (put-text-property
4618 (1+ REx-subgr-start) (1- (point))
4619 'syntax-table cperl-st-punct))
4620 (cperl-postpone-fontification
4621 REx-subgr-start qtag
4622 'face my-cperl-REx-spec-char-face)
4623 (cperl-postpone-fontification
4624 (1- (point)) (point) 'face
4625 my-cperl-REx-spec-char-face)
4626 (if (eq (char-after b) ?\] )
4627 (cperl-postpone-fontification
4628 (- (point) 2) (1- (point))
4629 'face my-cperl-REx-0length-face))
4630 (while tag
4631 (cperl-postpone-fontification
4632 (car (car tag)) (cdr (car tag))
8c777c8d 4633 'face font-lock-variable-name-face) ;my-cperl-REx-length1-face
4ab89e7b
SM
4634 (setq tag (cdr tag)))
4635 (setq was-subgr nil)) ; did facing already
4636 ;; Now rare stuff:
4637 ((and (match-beginning 2) ; #-comment
4638 (/= (match-beginning 2) (match-end 2)))
4639 (beginning-of-line 2)
4640 (if (> (point) e)
4641 (goto-char (1- e))))
4642 ((match-beginning 4) ; character "]"
4643 (setq was-subgr nil) ; We do stuff here
4644 (goto-char (match-end 0))
4645 (if cperl-use-syntax-table-text-property
4646 (put-text-property
4647 (1- (point)) (point)
4648 'syntax-table cperl-st-punct))
4649 (cperl-postpone-fontification
4650 (1- (point)) (point)
4651 'face font-lock-warning-face))
4652 ((match-beginning 5) ; before (?{}) (??{})
4653 (setq tag (match-end 0))
4654 (if (or (setq qtag
4655 (cperl-forward-group-in-re st-l))
4656 (and (>= (point) e)
4657 (setq qtag "no matching `)' found"))
4658 (and (not (eq (char-after (- (point) 2))
4659 ?\} ))
4660 (setq qtag "Can't find })")))
a1506d29 4661 (progn
4ab89e7b 4662 (goto-char (1- e))
274f1353 4663 (message "%s" qtag))
4ab89e7b
SM
4664 (cperl-postpone-fontification
4665 (1- tag) (1- (point))
4666 'face font-lock-variable-name-face)
4667 (cperl-postpone-fontification
4668 REx-subgr-start (1- tag)
4669 'face my-cperl-REx-spec-char-face)
4670 (cperl-postpone-fontification
4671 (1- (point)) (point)
4672 'face my-cperl-REx-spec-char-face)
4673 (if cperl-use-syntax-table-text-property
4674 (progn
4675 (put-text-property
4676 (- (point) 2) (1- (point))
4677 'syntax-table cperl-st-cfence)
4678 (put-text-property
4679 (+ REx-subgr-start 2)
4680 (+ REx-subgr-start 3)
4681 'syntax-table cperl-st-cfence))))
4682 (setq was-subgr nil))
4683 (t ; (?#)-comment
4684 ;; Inside "(" and "\" arn't special in any way
4685 ;; Works also if the outside delimiters are ().
4686 (or;;(if (eq (char-after b) ?\) )
4687 ;;(re-search-forward
4688 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4689 ;; (1- e) 'toend)
4690 (search-forward ")" (1- e) 'toend)
4691 ;;)
4692 (message
4693 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4694 REx-subgr-start))))
6c389151
SM
4695 (if (>= (point) e)
4696 (goto-char (1- e)))
4ab89e7b
SM
4697 (cond
4698 (was-subgr
4699 (setq REx-subgr-end (point))
4700 (cperl-commentify
4701 REx-subgr-start REx-subgr-end nil)
4702 (cperl-postpone-fontification
4703 REx-subgr-start REx-subgr-end
4704 'face font-lock-comment-face))))))
6c389151 4705 (if (and is-REx is-x-REx)
a1506d29 4706 (put-text-property (1+ b) (1- e)
6c389151 4707 'syntax-subtype 'x-REx)))
8c777c8d 4708 (if (and i2 e1 (or (not b1) (> e1 b1)))
82d9a08d 4709 (progn ; No errors finding the second part...
5c8b7eaf 4710 (cperl-postpone-fontification
4ab89e7b 4711 (1- e1) e1 'face my-cperl-delimiters-face)
05927f8c
VJL
4712 (if (and (not (eobp))
4713 (assoc (char-after b) cperl-starters))
4ab89e7b
SM
4714 (progn
4715 (cperl-postpone-fontification
4716 b1 (1+ b1) 'face my-cperl-delimiters-face)
4717 (put-text-property b1 (1+ b1)
4718 'REx-part2 t)))))
5bd52f0e
RS
4719 (if (> (point) max)
4720 (setq tmpend tb))))
4ab89e7b
SM
4721 ((match-beginning 17) ; sub with prototype or attribute
4722 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4723 ;;"\\<sub\\>\\(" ;12
4724 ;; cperl-white-and-comment-rex ;13
4725 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4726 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4727 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4728 (setq b1 (match-beginning 14) e1 (match-end 14))
f83d2997
KH
4729 (if (memq (char-after (1- b))
4730 '(?\$ ?\@ ?\% ?\& ?\*))
4731 nil
4ab89e7b
SM
4732 (goto-char b)
4733 (if (eq (char-after (match-beginning 17)) ?\( )
4734 (progn
4735 (cperl-commentify ; Prototypes; mark as string
4736 (match-beginning 17) (match-end 17) t)
4737 (goto-char (match-end 0))
4738 ;; Now look for attributes after prototype:
4739 (forward-comment (buffer-size))
4740 (and (looking-at ":[^:]")
4741 (cperl-find-sub-attrs st-l b1 e1 b)))
4742 ;; treat attributes without prototype
4743 (goto-char (match-beginning 17))
4744 (cperl-find-sub-attrs st-l b1 e1 b))))
4745 ;; 1+6+2+1+1+6+1=18 extra () before this:
f83d2997 4746 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4ab89e7b
SM
4747 ((match-beginning 19) ; old $abc'efg syntax
4748 (setq bb (match-end 0))
4749 ;;;(if (nth 3 state) nil ; in string
4750 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
f83d2997 4751 (goto-char bb))
4ab89e7b 4752 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
f83d2997 4753 ;; "__\\(END\\|DATA\\)__"
4ab89e7b
SM
4754 ((match-beginning 20) ; __END__, __DATA__
4755 (setq bb (match-end 0))
4756 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4757 (cperl-commentify b bb nil)
4758 (setq end t))
4759 ;; "\\\\\\(['`\"($]\\)"
4760 ((match-beginning 21)
4761 ;; Trailing backslash; make non-quoting outside string/comment
4762 (setq bb (match-end 0))
6c389151
SM
4763 (goto-char b)
4764 (skip-chars-backward "\\\\")
4765 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4ab89e7b 4766 (cperl-modify-syntax-type b cperl-st-punct)
6c389151
SM
4767 (goto-char bb))
4768 (t (error "Error in regexp of the sniffer")))
db133cb6 4769 (if (> (point) stop-point)
f83d2997 4770 (progn
5c8b7eaf 4771 (if end
f83d2997
KH
4772 (message "Garbage after __END__/__DATA__ ignored")
4773 (message "Unbalanced syntax found while scanning")
4774 (or (car err-l) (setcar err-l b)))
db133cb6
RS
4775 (goto-char stop-point))))
4776 (setq cperl-syntax-state (cons state-point state)
4ab89e7b
SM
4777 ;; Do not mark syntax as done past tmpend???
4778 cperl-syntax-done-to (or tmpend (max (point) max)))
4779 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4780 )
f83d2997 4781 (if (car err-l) (goto-char (car err-l))
db133cb6
RS
4782 (or non-inter
4783 (message "Scanning for \"hard\" Perl constructions... done"))))
f83d2997
KH
4784 (and (buffer-modified-p)
4785 (not modified)
4786 (set-buffer-modified-p nil))
00424a9e
SM
4787 ;; I do not understand what this is doing here. It breaks font-locking
4788 ;; because it resets the syntax-table from font-lock-syntax-table to
4789 ;; cperl-mode-syntax-table.
4790 ;; (set-syntax-table cperl-mode-syntax-table)
4791 )
4ab89e7b
SM
4792 (list (car err-l) overshoot)))
4793
4794(defun cperl-find-pods-heres-region (min max)
4795 (interactive "r")
4796 (cperl-find-pods-heres min max))
f83d2997
KH
4797
4798(defun cperl-backward-to-noncomment (lim)
4799 ;; Stops at lim or after non-whitespace that is not in comment
4ab89e7b 4800 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
5bd52f0e 4801 (let (stop p pr)
4ab89e7b 4802 (while (and (not stop) (> (point) (or lim (point-min))))
f83d2997
KH
4803 (skip-chars-backward " \t\n\f" lim)
4804 (setq p (point))
4805 (beginning-of-line)
5bd52f0e
RS
4806 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4807 '(pod here-doc here-doc-delim))
82d9a08d
SM
4808 (progn
4809 (cperl-unwind-to-safe nil)
4810 (setq pr (get-text-property (point) 'syntax-type))))
4811 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4812 (not (memq pr '(string prestring))))
4813 (progn (cperl-to-comment-or-eol) (bolp))
4814 (progn
4815 (skip-chars-backward " \t")
4816 (if (< p (point)) (goto-char p))
4817 (setq stop t))))))
f83d2997 4818
4ab89e7b
SM
4819;; Used only in `cperl-calculate-indent'...
4820(defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4821 ;; Positions is before ?\{. Checks whether it starts a block.
4822 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4823 (cperl-backward-to-noncomment (point-min))
4824 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4825 ; Label may be mixed up with `$blah :'
4826 (save-excursion (cperl-after-label))
4827 (get-text-property (cperl-1- (point)) 'attrib-group)
4828 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4829 (progn
4830 (backward-sexp)
4831 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4832 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4833 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4834 ;; sub bless::foo {}
4835 (progn
4836 (cperl-backward-to-noncomment (point-min))
4837 (and (eq (preceding-char) ?b)
4838 (progn
4839 (forward-sexp -1)
4840 (looking-at "sub[ \t\n\f#]")))))))))
4841
4842;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4843;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4844;;; may be a part of an in-statement construct, such as
4845;;; ${something()}, print {FH} $data.
4846;;; Moreover, one takes positive approach (looks for else,grep etc)
4847;;; another negative (looks for bless,tr etc)
f739b53b 4848(defun cperl-after-block-p (lim &optional pre-block)
97610156 4849 "Return true if the preceding } (if PRE-BLOCK, following {) delimits a block.
4ab89e7b
SM
4850Would not look before LIM. Assumes that LIM is a good place to begin a
4851statement. The kind of block we treat here is one after which a new
4852statement would start; thus the block in ${func()} does not count."
f83d2997
KH
4853 (save-excursion
4854 (condition-case nil
4855 (progn
f739b53b 4856 (or pre-block (forward-sexp -1))
f83d2997 4857 (cperl-backward-to-noncomment lim)
bab27c0c 4858 (or (eq (point) lim)
4ab89e7b
SM
4859 ;; if () {} // sub f () {} // sub f :a(') {}
4860 (eq (preceding-char) ?\) )
4861 ;; label: {}
4862 (save-excursion (cperl-after-label))
4863 ;; sub :attr {}
4864 (get-text-property (cperl-1- (point)) 'attrib-group)
4865 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
db133cb6
RS
4866 (save-excursion
4867 (forward-sexp -1)
4ab89e7b
SM
4868 ;; else {} but not else::func {}
4869 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4870 (not (looking-at "\\(\\sw\\|_\\)+::")))
db133cb6
RS
4871 ;; sub f {}
4872 (progn
4873 (cperl-backward-to-noncomment lim)
4ab89e7b 4874 (and (eq (preceding-char) ?b)
db133cb6
RS
4875 (progn
4876 (forward-sexp -1)
4ab89e7b 4877 (looking-at "sub[ \t\n\f#]"))))))
97610156 4878 ;; What precedes is not word... XXXX Last statement in sub???
db133cb6 4879 (cperl-after-expr-p lim))))
f83d2997
KH
4880 (error nil))))
4881
4882(defun cperl-after-expr-p (&optional lim chars test)
029cb4d5 4883 "Return true if the position is good for start of expression.
f83d2997
KH
4884TEST is the expression to evaluate at the found position. If absent,
4885CHARS is a string that contains good characters to have before us (however,
4886`}' is treated \"smartly\" if it is not in the list)."
83261a2f 4887 (let ((lim (or lim (point-min)))
f739b53b
SM
4888 stop p pr)
4889 (cperl-update-syntaxification (point) (point))
f83d2997
KH
4890 (save-excursion
4891 (while (and (not stop) (> (point) lim))
4892 (skip-chars-backward " \t\n\f" lim)
4893 (setq p (point))
4894 (beginning-of-line)
f739b53b
SM
4895 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4896 ;; '(pod here-doc here-doc-delim))
4897 (if (get-text-property (point) 'here-doc-group)
4898 (progn
4899 (goto-char
4ab89e7b 4900 (cperl-beginning-of-property (point) 'here-doc-group))
f739b53b
SM
4901 (beginning-of-line 0)))
4902 (if (get-text-property (point) 'in-pod)
4903 (progn
4904 (goto-char
4ab89e7b 4905 (cperl-beginning-of-property (point) 'in-pod))
f739b53b 4906 (beginning-of-line 0)))
f83d2997 4907 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
5bd52f0e 4908 ;; Else: last iteration, or a label
f739b53b 4909 (cperl-to-comment-or-eol) ; Will not move past "." after a format
f83d2997
KH
4910 (skip-chars-backward " \t")
4911 (if (< p (point)) (goto-char p))
5bd52f0e
RS
4912 (setq p (point))
4913 (if (and (eq (preceding-char) ?:)
4914 (progn
4915 (forward-char -1)
4916 (skip-chars-backward " \t\n\f" lim)
4ab89e7b 4917 (memq (char-syntax (preceding-char)) '(?w ?_))))
5bd52f0e
RS
4918 (forward-sexp -1) ; Possibly label. Skip it
4919 (goto-char p)
4920 (setq stop t))))
bab27c0c
RS
4921 (or (bobp) ; ???? Needed
4922 (eq (point) lim)
029cb4d5 4923 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
f83d2997
KH
4924 (progn
4925 (if test (eval test)
4926 (or (memq (preceding-char) (append (or chars "{;") nil))
4927 (and (eq (preceding-char) ?\})
f739b53b
SM
4928 (cperl-after-block-p lim))
4929 (and (eq (following-char) ?.) ; in format: see comment above
4930 (eq (get-text-property (point) 'syntax-type)
4931 'format)))))))))
f83d2997 4932
4ab89e7b
SM
4933(defun cperl-backward-to-start-of-expr (&optional lim)
4934 (condition-case nil
4935 (progn
4936 (while (and (or (not lim)
4937 (> (point) lim))
4938 (not (cperl-after-expr-p lim)))
4939 (forward-sexp -1)
4940 ;; May be after $, @, $# etc of a variable
4941 (skip-chars-backward "$@%#")))
4942 (error nil)))
4943
4944(defun cperl-at-end-of-expr (&optional lim)
4945 ;; Since the SEXP approach below is very fragile, do some overengineering
4946 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4947 (condition-case nil
4948 (save-excursion
4949 ;; If nothing interesting after, does as (forward-sexp -1);
4950 ;; otherwise fails, or ends at a start of following sexp.
4951 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4952 ;; may be stuck after @ or $; just put some stupid workaround now:
4953 (let ((p (point)))
4954 (forward-sexp 1)
4955 (forward-sexp -1)
4956 (while (memq (preceding-char) (append "%&@$*" nil))
4957 (forward-char -1))
4958 (or (< (point) p)
4959 (cperl-after-expr-p lim))))
4960 (error t))))
4961
4962(defun cperl-forward-to-end-of-expr (&optional lim)
4963 (let ((p (point))))
4964 (condition-case nil
4965 (progn
4966 (while (and (< (point) (or lim (point-max)))
4967 (not (cperl-at-end-of-expr)))
4968 (forward-sexp 1)))
4969 (error nil)))
4970
f83d2997
KH
4971(defun cperl-backward-to-start-of-continued-exp (lim)
4972 (if (memq (preceding-char) (append ")]}\"'`" nil))
4973 (forward-sexp -1))
4974 (beginning-of-line)
4975 (if (<= (point) lim)
4976 (goto-char (1+ lim)))
4977 (skip-chars-forward " \t"))
4978
db133cb6
RS
4979(defun cperl-after-block-and-statement-beg (lim)
4980 ;; We assume that we are after ?\}
5c8b7eaf 4981 (and
db133cb6
RS
4982 (cperl-after-block-p lim)
4983 (save-excursion
4984 (forward-sexp -1)
4985 (cperl-backward-to-noncomment (point-min))
4986 (or (bobp)
bab27c0c 4987 (eq (point) lim)
db133cb6
RS
4988 (not (= (char-syntax (preceding-char)) ?w))
4989 (progn
4990 (forward-sexp -1)
5c8b7eaf 4991 (not
db133cb6
RS
4992 (looking-at
4993 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4994
f83d2997 4995\f
f83d2997
KH
4996(defun cperl-indent-exp ()
4997 "Simple variant of indentation of continued-sexp.
5bd52f0e
RS
4998
4999Will not indent comment if it starts at `comment-indent' or looks like
5000continuation of the comment on the previous line.
db133cb6 5001
5c8b7eaf 5002If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 5003conditional/loop constructs."
f83d2997
KH
5004 (interactive)
5005 (save-excursion
e180ab9f 5006 (let ((tmp-end (point-at-eol)) top done)
f83d2997
KH
5007 (save-excursion
5008 (beginning-of-line)
5009 (while (null done)
5010 (setq top (point))
4ab89e7b
SM
5011 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5012 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
bbd240ce 5013 (setq top (point))) ; Get the outermost parens in line
f83d2997
KH
5014 (goto-char top)
5015 (while (< (point) tmp-end)
5016 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5017 (or (eolp) (forward-sexp 1)))
4ab89e7b
SM
5018 (if (> (point) tmp-end) ; Yes, there an unfinished block
5019 nil
5020 (if (eq ?\) (preceding-char))
5021 (progn ;; Plan B: find by REGEXP block followup this line
5022 (setq top (point))
5023 (condition-case nil
5024 (progn
5025 (forward-sexp -2)
5026 (if (eq (following-char) ?$ ) ; for my $var (list)
5027 (progn
5028 (forward-sexp -1)
5029 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5030 (forward-sexp -1))))
5031 (if (looking-at
5032 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5033 "\\|for\\(each\\)?\\>\\(\\("
5034 cperl-maybe-white-and-comment-rex
5035 "\\(my\\|local\\|our\\)\\)?"
5036 cperl-maybe-white-and-comment-rex
5037 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5038 (progn
5039 (goto-char top)
5040 (forward-sexp 1)
5041 (setq top (point)))))
5042 (error (setq done t)))
5043 (goto-char top))
5044 (if (looking-at ; Try Plan C: continuation block
5045 (concat cperl-maybe-white-and-comment-rex
5046 "\\<\\(else\\|elsif\|continue\\)\\>"))
5047 (progn
5048 (goto-char (match-end 0))
e180ab9f 5049 (setq tmp-end (point-at-eol)))
4ab89e7b 5050 (setq done t))))
e180ab9f 5051 (setq tmp-end (point-at-eol)))
f83d2997
KH
5052 (goto-char tmp-end)
5053 (setq tmp-end (point-marker)))
db133cb6
RS
5054 (if cperl-indent-region-fix-constructs
5055 (cperl-fix-line-spacing tmp-end))
f83d2997
KH
5056 (cperl-indent-region (point) tmp-end))))
5057
5bd52f0e
RS
5058(defun cperl-fix-line-spacing (&optional end parse-data)
5059 "Improve whitespace in a conditional/loop construct.
5060Returns some position at the last line."
db133cb6
RS
5061 (interactive)
5062 (or end
5063 (setq end (point-max)))
e180ab9f 5064 (let ((ee (point-at-eol))
83261a2f
SM
5065 (cperl-indent-region-fix-constructs
5066 (or cperl-indent-region-fix-constructs 1))
5067 p pp ml have-brace ret)
db133cb6
RS
5068 (save-excursion
5069 (beginning-of-line)
5bd52f0e 5070 (setq ret (point))
5c8b7eaf 5071 ;; }? continue
5bd52f0e 5072 ;; blah; }
5c8b7eaf 5073 (if (not
5bd52f0e
RS
5074 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5075 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5076 nil ; Do not need to do anything
83261a2f
SM
5077 ;; Looking at:
5078 ;; }
5079 ;; else
4ab89e7b
SM
5080 (if cperl-merge-trailing-else
5081 (if (looking-at
5082 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5083 (progn
5084 (search-forward "}")
5085 (setq p (point))
5086 (skip-chars-forward " \t\n")
5087 (delete-region p (point))
b5b0cb34 5088 (insert (make-string cperl-indent-region-fix-constructs ?\s))
4ab89e7b
SM
5089 (beginning-of-line)))
5090 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5091 (save-excursion
5092 (search-forward "}")
5093 (delete-horizontal-space)
5094 (insert "\n")
5095 (setq ret (point))
5096 (if (cperl-indent-line parse-data)
5097 (progn
5098 (cperl-fix-line-spacing end parse-data)
5099 (setq ret (point)))))))
83261a2f
SM
5100 ;; Looking at:
5101 ;; } else
5102 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5103 (progn
5104 (search-forward "}")
5105 (delete-horizontal-space)
b5b0cb34 5106 (insert (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f
SM
5107 (beginning-of-line)))
5108 ;; Looking at:
5109 ;; else {
5110 (if (looking-at
5111 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5112 (progn
5113 (forward-word 1)
5114 (delete-horizontal-space)
b5b0cb34 5115 (insert (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f
SM
5116 (beginning-of-line)))
5117 ;; Looking at:
5118 ;; foreach my $var
5119 (if (looking-at
5120 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5121 (progn
5122 (forward-word 2)
5123 (delete-horizontal-space)
b5b0cb34 5124 (insert (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f
SM
5125 (beginning-of-line)))
5126 ;; Looking at:
5127 ;; foreach my $var (
5128 (if (looking-at
6c389151 5129 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
83261a2f 5130 (progn
f739b53b 5131 (forward-sexp 3)
83261a2f
SM
5132 (delete-horizontal-space)
5133 (insert
b5b0cb34 5134 (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f 5135 (beginning-of-line)))
4ab89e7b
SM
5136 ;; Looking at (with or without "}" at start, ending after "({"):
5137 ;; } foreach my $var () OR {
83261a2f 5138 (if (looking-at
ce22dd53 5139 "[ \t]*\\(}[ \t]*\\)?\\<\\(\\els\\(e\\|if\\)\\|continue\\|if\\|unless\\|while\\|for\\(each\\)?\\(\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\$[_a-zA-Z0-9]+\\)?\\|until\\)\\>\\([ \t]*(\\|[ \t\n]*{\\)\\|[ \t]*{")
83261a2f 5140 (progn
4ab89e7b 5141 (setq ml (match-beginning 8)) ; "(" or "{" after control word
83261a2f
SM
5142 (re-search-forward "[({]")
5143 (forward-char -1)
5144 (setq p (point))
5145 (if (eq (following-char) ?\( )
5146 (progn
5147 (forward-sexp 1)
c91c771d 5148 (setq pp (point))) ; past parenthesis-group
83261a2f
SM
5149 ;; after `else' or nothing
5150 (if ml ; after `else'
5151 (skip-chars-backward " \t\n")
5152 (beginning-of-line))
5153 (setq pp nil))
5154 ;; Now after the sexp before the brace
5155 ;; Multiline expr should be special
5156 (setq ml (and pp (save-excursion (goto-char p)
5157 (search-forward "\n" pp t))))
4ab89e7b 5158 (if (and (or (not pp) (< pp end)) ; Do not go too far...
83261a2f
SM
5159 (looking-at "[ \t\n]*{"))
5160 (progn
5161 (cond
5162 ((bolp) ; Were before `{', no if/else/etc
5163 nil)
4ab89e7b 5164 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
83261a2f
SM
5165 (delete-horizontal-space)
5166 (if (if ml
5167 cperl-extra-newline-before-brace-multiline
5168 cperl-extra-newline-before-brace)
5169 (progn
5170 (delete-horizontal-space)
5171 (insert "\n")
5172 (setq ret (point))
5173 (if (cperl-indent-line parse-data)
5174 (progn
5175 (cperl-fix-line-spacing end parse-data)
5176 (setq ret (point)))))
5177 (insert
b5b0cb34 5178 (make-string cperl-indent-region-fix-constructs ?\s))))
83261a2f
SM
5179 ((and (looking-at "[ \t]*\n")
5180 (not (if ml
5181 cperl-extra-newline-before-brace-multiline
5182 cperl-extra-newline-before-brace)))
5183 (setq pp (point))
5184 (skip-chars-forward " \t\n")
5185 (delete-region pp (point))
db133cb6 5186 (insert
4ab89e7b
SM
5187 (make-string cperl-indent-region-fix-constructs ?\ )))
5188 ((and (looking-at "[\t ]*{")
5189 (if ml cperl-extra-newline-before-brace-multiline
5190 cperl-extra-newline-before-brace))
5191 (delete-horizontal-space)
5192 (insert "\n")
5193 (setq ret (point))
5194 (if (cperl-indent-line parse-data)
5195 (progn
5196 (cperl-fix-line-spacing end parse-data)
5197 (setq ret (point))))))
83261a2f
SM
5198 ;; Now we are before `{'
5199 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5200 (progn
5201 (skip-chars-forward " \t\n")
5202 (setq pp (point))
5203 (forward-sexp 1)
5204 (setq p (point))
5205 (goto-char pp)
5206 (setq ml (search-forward "\n" p t))
5207 (if (or cperl-break-one-line-blocks-when-indent ml)
5208 ;; not good: multi-line BLOCK
5209 (progn
5210 (goto-char (1+ pp))
5211 (delete-horizontal-space)
5212 (insert "\n")
5213 (setq ret (point))
5214 (if (cperl-indent-line parse-data)
5215 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5216 (beginning-of-line)
e180ab9f 5217 (setq p (point) pp (point-at-eol)) ; May be different from ee.
83261a2f
SM
5218 ;; Now check whether there is a hanging `}'
5219 ;; Looking at:
5220 ;; } blah
5221 (if (and
5222 cperl-fix-hanging-brace-when-indent
5223 have-brace
5224 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5225 (condition-case nil
5226 (progn
5227 (up-list 1)
5228 (if (and (<= (point) pp)
5229 (eq (preceding-char) ?\} )
5230 (cperl-after-block-and-statement-beg (point-min)))
5231 t
5232 (goto-char p)
5233 nil))
5234 (error nil)))
5235 (progn
5236 (forward-char -1)
5237 (skip-chars-backward " \t")
5238 (if (bolp)
5239 ;; `}' was the first thing on the line, insert NL *after* it.
5240 (progn
5241 (cperl-indent-line parse-data)
5242 (search-forward "}")
5243 (delete-horizontal-space)
5244 (insert "\n"))
5245 (delete-horizontal-space)
5246 (or (eq (preceding-char) ?\;)
5247 (bolp)
5248 (and (eq (preceding-char) ?\} )
5249 (cperl-after-block-p (point-min)))
5250 (insert ";"))
5251 (insert "\n")
5252 (setq ret (point)))
5253 (if (cperl-indent-line parse-data)
5254 (setq ret (cperl-fix-line-spacing end parse-data)))
5255 (beginning-of-line)))))
5bd52f0e
RS
5256 ret))
5257
5258(defvar cperl-update-start) ; Do not need to make them local
5259(defvar cperl-update-end)
5260(defun cperl-delay-update-hook (beg end old-len)
5261 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5262 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
db133cb6 5263
f83d2997
KH
5264(defun cperl-indent-region (start end)
5265 "Simple variant of indentation of region in CPerl mode.
5c8b7eaf 5266Should be slow. Will not indent comment if it starts at `comment-indent'
f83d2997 5267or looks like continuation of the comment on the previous line.
5c8b7eaf
SS
5268Indents all the lines whose first character is between START and END
5269inclusive.
db133cb6 5270
5c8b7eaf 5271If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 5272conditional/loop constructs."
f83d2997 5273 (interactive "r")
5bd52f0e 5274 (cperl-update-syntaxification end end)
f83d2997 5275 (save-excursion
5bd52f0e 5276 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
83261a2f
SM
5277 (let ((indent-info (if cperl-emacs-can-parse
5278 (list nil nil nil) ; Cannot use '(), since will modify
5279 nil))
c326ddd1 5280 (pm 0)
83261a2f
SM
5281 after-change-functions ; Speed it up!
5282 st comm old-comm-indent new-comm-indent p pp i empty)
5bd52f0e 5283 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
83261a2f
SM
5284 (goto-char start)
5285 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5286 (current-column))
5287 new-comm-indent old-comm-indent)
5288 (goto-char start)
5289 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5290 (or (bolp) (beginning-of-line 2))
83261a2f 5291 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5bd52f0e
RS
5292 (setq st (point))
5293 (if (or
5294 (setq empty (looking-at "[ \t]*\n"))
5295 (and (setq comm (looking-at "[ \t]*#"))
83261a2f
SM
5296 (or (eq (current-indentation) (or old-comm-indent
5297 comment-column))
5bd52f0e 5298 (setq old-comm-indent nil))))
8c777c8d 5299 (if (and old-comm-indent
5bd52f0e 5300 (not empty)
8c777c8d 5301 (= (current-indentation) old-comm-indent)
5bd52f0e
RS
5302 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5303 (not (eq (get-text-property (point) 'syntax-table)
5304 cperl-st-cfence)))
83261a2f
SM
5305 (let ((comment-column new-comm-indent))
5306 (indent-for-comment)))
5307 (progn
5bd52f0e 5308 (setq i (cperl-indent-line indent-info))
8c777c8d
CY
5309 (or comm
5310 (not i)
5311 (progn
5312 (if cperl-indent-region-fix-constructs
5bd52f0e 5313 (goto-char (cperl-fix-line-spacing end indent-info)))
83261a2f
SM
5314 (if (setq old-comm-indent
5315 (and (cperl-to-comment-or-eol)
5316 (not (memq (get-text-property (point)
5317 'syntax-type)
5318 '(pod here-doc)))
5c8b7eaf 5319 (not (eq (get-text-property (point)
5bd52f0e
RS
5320 'syntax-table)
5321 cperl-st-cfence))
8c777c8d
CY
5322 (current-column)))
5323 (progn (indent-for-comment)
5324 (skip-chars-backward " \t")
5325 (skip-chars-backward "#")
5326 (setq new-comm-indent (current-column))))))))
5327 (beginning-of-line 2)))
5bd52f0e 5328 ;; Now run the update hooks
83261a2f
SM
5329 (and after-change-functions
5330 cperl-update-end
5331 (save-excursion
5332 (goto-char cperl-update-end)
5333 (insert " ")
5334 (delete-char -1)
5335 (goto-char cperl-update-start)
5336 (insert " ")
5337 (delete-char -1))))))
f83d2997 5338
f83d2997
KH
5339;; Stolen from lisp-mode with a lot of improvements
5340
5341(defun cperl-fill-paragraph (&optional justify iteration)
82eb0dae 5342 "Like `fill-paragraph', but handle CPerl comments.
f83d2997
KH
5343If any of the current line is a comment, fill the comment or the
5344block of it that point is in, preserving the comment's initial
5345indentation and initial hashes. Behaves usually outside of comment."
82eb0dae 5346 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
83261a2f 5347 (let (;; Non-nil if the current line contains a comment.
f83d2997 5348 has-comment
4ab89e7b 5349 fill-paragraph-function ; do not recurse
f83d2997
KH
5350 ;; If has-comment, the appropriate fill-prefix for the comment.
5351 comment-fill-prefix
5352 ;; Line that contains code and comment (or nil)
5353 start
5354 c spaces len dc (comment-column comment-column))
5355 ;; Figure out what kind of comment we are looking at.
5356 (save-excursion
5357 (beginning-of-line)
5358 (cond
5359
5360 ;; A line with nothing but a comment on it?
5361 ((looking-at "[ \t]*#[# \t]*")
5362 (setq has-comment t
5363 comment-fill-prefix (buffer-substring (match-beginning 0)
5364 (match-end 0))))
5365
5366 ;; A line with some code, followed by a comment? Remember that the
5367 ;; semi which starts the comment shouldn't be part of a string or
5368 ;; character.
5369 ((cperl-to-comment-or-eol)
5370 (setq has-comment t)
5371 (looking-at "#+[ \t]*")
5c8b7eaf 5372 (setq start (point) c (current-column)
f83d2997 5373 comment-fill-prefix
b5b0cb34 5374 (concat (make-string (current-column) ?\s)
f83d2997 5375 (buffer-substring (match-beginning 0) (match-end 0)))
5c8b7eaf 5376 spaces (progn (skip-chars-backward " \t")
f83d2997 5377 (buffer-substring (point) start))
5c8b7eaf 5378 dc (- c (current-column)) len (- start (point))
f83d2997
KH
5379 start (point-marker))
5380 (delete-char len)
4ab89e7b 5381 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
f83d2997 5382 (if (not has-comment)
83261a2f 5383 (fill-paragraph justify) ; Do the usual thing outside of comment
f83d2997
KH
5384 ;; Narrow to include only the comment, and then fill the region.
5385 (save-restriction
5386 (narrow-to-region
5387 ;; Find the first line we should include in the region to fill.
5388 (if start (progn (beginning-of-line) (point))
5389 (save-excursion
5390 (while (and (zerop (forward-line -1))
5391 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5392 ;; We may have gone to far. Go forward again.
5393 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5394 (forward-line 1))
5395 (point)))
5396 ;; Find the beginning of the first line past the region to fill.
5397 (save-excursion
5398 (while (progn (forward-line 1)
5399 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5400 (point)))
5401 ;; Remove existing hashes
4ab89e7b 5402 (goto-char (point-min))
8c777c8d
CY
5403 (save-excursion
5404 (while (progn (forward-line 1) (< (point) (point-max)))
5405 (skip-chars-forward " \t")
5406 (if (looking-at "#+")
5407 (progn
5408 (if (and (eq (point) (match-beginning 0))
5409 (not (eq (point) (match-end 0)))) nil
4ab89e7b
SM
5410 (error
5411 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5412 (delete-char (- (match-end 0) (match-beginning 0)))))))
f83d2997
KH
5413
5414 ;; Lines with only hashes on them can be paragraph boundaries.
5415 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5416 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5417 (fill-prefix comment-fill-prefix))
5418 (fill-paragraph justify)))
5419 (if (and start)
5c8b7eaf 5420 (progn
f83d2997
KH
5421 (goto-char start)
5422 (if (> dc 0)
83261a2f 5423 (progn (delete-char dc) (insert spaces)))
f83d2997
KH
5424 (if (or (= (current-column) c) iteration) nil
5425 (setq comment-column c)
5426 (indent-for-comment)
5427 ;; Repeat once more, flagging as iteration
4ab89e7b
SM
5428 (cperl-fill-paragraph justify t))))))
5429 t)
f83d2997
KH
5430
5431(defun cperl-do-auto-fill ()
5432 ;; Break out if the line is short enough
5433 (if (> (save-excursion
5434 (end-of-line)
5435 (current-column))
5436 fill-column)
83261a2f
SM
5437 (let ((c (save-excursion (beginning-of-line)
5438 (cperl-to-comment-or-eol) (point)))
8038e2cf 5439 (s (memq (following-char) '(?\s ?\t))) marker)
82eb0dae
SM
5440 (if (>= c (point))
5441 ;; Don't break line inside code: only inside comment.
5442 nil
83261a2f 5443 (setq marker (point-marker))
82eb0dae 5444 (fill-paragraph nil)
83261a2f
SM
5445 (goto-char marker)
5446 ;; Is not enough, sometimes marker is a start of line
5447 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5448 (goto-char (match-end 0))))
5449 ;; Following space could have gone:
8038e2cf 5450 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
83261a2f
SM
5451 (insert " ")
5452 (backward-char 1))
5453 ;; Previous space could have gone:
8038e2cf 5454 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
f83d2997 5455
f83d2997
KH
5456(defun cperl-imenu-addback (lst &optional isback name)
5457 ;; We suppose that the lst is a DAG, unless the first element only
5458 ;; loops back, and ISBACK is set. Thus this function cannot be
5459 ;; applied twice without ISBACK set.
5460 (cond ((not cperl-imenu-addback) lst)
5461 (t
5c8b7eaf 5462 (or name
f83d2997 5463 (setq name "+++BACK+++"))
dba01120
GM
5464 (mapc (lambda (elt)
5465 (if (and (listp elt) (listp (cdr elt)))
5466 (progn
5467 ;; In the other order it goes up
5468 ;; one level only ;-(
5469 (setcdr elt (cons (cons name lst)
5470 (cdr elt)))
5471 (cperl-imenu-addback (cdr elt) t name))))
5472 (if isback (cdr lst) lst))
f83d2997
KH
5473 lst)))
5474
80585273 5475(defun cperl-imenu--create-perl-index (&optional regexp)
f83d2997 5476 (require 'imenu) ; May be called from TAGS creator
5c8b7eaf 5477 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
f83d2997
KH
5478 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5479 (index-meth-alist '()) meth
4ab89e7b
SM
5480 packages ends-ranges p marker is-proto
5481 (prev-pos 0) is-pack index index1 name (end-range 0) package)
f83d2997 5482 (goto-char (point-min))
6c389151 5483 (cperl-update-syntaxification (point-max) (point-max))
f83d2997
KH
5484 ;; Search for the function
5485 (progn ;;save-match-data
5486 (while (re-search-forward
80585273 5487 (or regexp cperl-imenu--function-name-regexp-perl)
f83d2997 5488 nil t)
4ab89e7b 5489 ;; 2=package-group, 5=package-name 8=sub-name
f83d2997
KH
5490 (cond
5491 ((and ; Skip some noise if building tags
4ab89e7b
SM
5492 (match-beginning 5) ; package name
5493 ;;(eq (char-after (match-beginning 2)) ?p) ; package
f83d2997 5494 (not (save-match-data
83261a2f 5495 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
f83d2997
KH
5496 nil)
5497 ((and
4ab89e7b
SM
5498 (or (match-beginning 2)
5499 (match-beginning 8)) ; package or sub
6c389151 5500 ;; Skip if quoted (will not skip multi-line ''-strings :-():
f83d2997
KH
5501 (null (get-text-property (match-beginning 1) 'syntax-table))
5502 (null (get-text-property (match-beginning 1) 'syntax-type))
5503 (null (get-text-property (match-beginning 1) 'in-pod)))
4ab89e7b 5504 (setq is-pack (match-beginning 2))
f83d2997
KH
5505 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5506 ;; (goto-char (match-end 0))) ; Messes what follows
4ab89e7b 5507 (setq meth nil
f83d2997
KH
5508 p (point))
5509 (while (and ends-ranges (>= p (car ends-ranges)))
5510 ;; delete obsolete entries
5511 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5512 (setq package (or (car packages) "")
5513 end-range (or (car ends-ranges) 0))
4ab89e7b
SM
5514 (if is-pack ; doing "package"
5515 (progn
5516 (if (match-beginning 5) ; named package
5517 (setq name (buffer-substring (match-beginning 5)
5518 (match-end 5))
5519 name (progn
5520 (set-text-properties 0 (length name) nil name)
5521 name)
5522 package (concat name "::")
5523 name (concat "package " name))
5524 ;; Support nameless packages
5525 (setq name "package;" package ""))
5526 (setq end-range
5527 (save-excursion
5528 (parse-partial-sexp (point) (point-max) -1) (point))
5529 ends-ranges (cons end-range ends-ranges)
5530 packages (cons package packages)))
5531 (setq is-proto
5532 (or (eq (following-char) ?\;)
5533 (eq 0 (get-text-property (point) 'attrib-group)))))
f83d2997 5534 ;; Skip this function name if it is a prototype declaration.
4ab89e7b
SM
5535 (if (and is-proto (not is-pack)) nil
5536 (or is-pack
5537 (setq name
5538 (buffer-substring (match-beginning 8) (match-end 8)))
5539 (set-text-properties 0 (length name) nil name))
5540 (setq marker (make-marker))
5541 (set-marker marker (match-end (if is-pack 2 8)))
5542 (cond (is-pack nil)
5543 ((string-match "[:']" name)
5544 (setq meth t))
5545 ((> p end-range) nil)
5546 (t
5547 (setq name (concat package name) meth t)))
6c389151 5548 (setq index (cons name marker))
4ab89e7b 5549 (if is-pack
f83d2997
KH
5550 (push index index-pack-alist)
5551 (push index index-alist))
5552 (if meth (push index index-meth-alist))
5553 (push index index-unsorted-alist)))
4ab89e7b
SM
5554 ((match-beginning 16) ; POD section
5555 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5556 marker (make-marker))
5557 (set-marker marker (match-beginning 17))
f83d2997 5558 (set-text-properties 0 (length name) nil name)
4ab89e7b
SM
5559 (setq name (concat (make-string
5560 (* 3 (- (char-after (match-beginning 16)) ?1))
5561 ?\ )
5562 name)
5563 index (cons name marker))
f83d2997
KH
5564 (setq index1 (cons (concat "=" name) (cdr index)))
5565 (push index index-pod-alist)
5566 (push index1 index-unsorted-alist)))))
5c8b7eaf 5567 (setq index-alist
f83d2997
KH
5568 (if (default-value 'imenu-sort-function)
5569 (sort index-alist (default-value 'imenu-sort-function))
83261a2f 5570 (nreverse index-alist)))
f83d2997
KH
5571 (and index-pod-alist
5572 (push (cons "+POD headers+..."
5573 (nreverse index-pod-alist))
5574 index-alist))
5575 (and (or index-pack-alist index-meth-alist)
5576 (let ((lst index-pack-alist) hier-list pack elt group name)
5577 ;; Remove "package ", reverse and uniquify.
5578 (while lst
5579 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5580 (if (assoc name hier-list) nil
5581 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5582 (setq lst index-meth-alist)
5583 (while lst
5584 (setq elt (car lst) lst (cdr lst))
5585 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5586 (setq pack (substring (car elt) 0 (match-beginning 0)))
5c8b7eaf 5587 (if (setq group (assoc pack hier-list))
f83d2997
KH
5588 (if (listp (cdr group))
5589 ;; Have some functions already
5c8b7eaf
SS
5590 (setcdr group
5591 (cons (cons (substring
f83d2997
KH
5592 (car elt)
5593 (+ 2 (match-beginning 0)))
5594 (cdr elt))
5595 (cdr group)))
5c8b7eaf 5596 (setcdr group (list (cons (substring
f83d2997
KH
5597 (car elt)
5598 (+ 2 (match-beginning 0)))
5599 (cdr elt)))))
5c8b7eaf
SS
5600 (setq hier-list
5601 (cons (cons pack
5602 (list (cons (substring
f83d2997
KH
5603 (car elt)
5604 (+ 2 (match-beginning 0)))
5605 (cdr elt))))
5606 hier-list))))))
5607 (push (cons "+Hierarchy+..."
5608 hier-list)
5609 index-alist)))
5610 (and index-pack-alist
5611 (push (cons "+Packages+..."
5612 (nreverse index-pack-alist))
5613 index-alist))
5c8b7eaf 5614 (and (or index-pack-alist index-pod-alist
f83d2997
KH
5615 (default-value 'imenu-sort-function))
5616 index-unsorted-alist
5617 (push (cons "+Unsorted List+..."
5618 (nreverse index-unsorted-alist))
5619 index-alist))
5620 (cperl-imenu-addback index-alist)))
5621
6c389151 5622\f
6c389151
SM
5623;; Suggested by Mark A. Hershberger
5624(defun cperl-outline-level ()
5625 (looking-at outline-regexp)
5626 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
4ab89e7b
SM
5627;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5628 ((match-beginning 2) 0) ; package
5629 ((match-beginning 8) 1) ; sub
5630 ((match-beginning 16)
5631 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5632 (t 5))) ; should not happen
6c389151
SM
5633
5634\f
f83d2997
KH
5635(defun cperl-windowed-init ()
5636 "Initialization under windowed version."
f453f5a8 5637 (cond ((featurep 'ps-print)
82d9a08d
SM
5638 (or cperl-faces-init
5639 (progn
5640 (and (boundp 'font-lock-multiline)
5641 (setq cperl-font-lock-multiline t))
5642 (cperl-init-faces))))
f453f5a8
CY
5643 ((not cperl-faces-init)
5644 (add-hook 'font-lock-mode-hook
5645 (function
5646 (lambda ()
5647 (if (memq major-mode '(perl-mode cperl-mode))
5648 (progn
5649 (or cperl-faces-init (cperl-init-faces)))))))
5650 (if (fboundp 'eval-after-load)
5651 (eval-after-load
5652 "ps-print"
5653 '(or cperl-faces-init (cperl-init-faces)))))))
db133cb6 5654
5efe6a56 5655(defvar cperl-font-lock-keywords-1 nil
80585273 5656 "Additional expressions to highlight in Perl mode. Minimal set.")
5efe6a56 5657(defvar cperl-font-lock-keywords nil
80585273 5658 "Additional expressions to highlight in Perl mode. Default set.")
5efe6a56 5659(defvar cperl-font-lock-keywords-2 nil
80585273
DL
5660 "Additional expressions to highlight in Perl mode. Maximal set")
5661
db133cb6
RS
5662(defun cperl-load-font-lock-keywords ()
5663 (or cperl-faces-init (cperl-init-faces))
5efe6a56 5664 cperl-font-lock-keywords)
db133cb6
RS
5665
5666(defun cperl-load-font-lock-keywords-1 ()
5667 (or cperl-faces-init (cperl-init-faces))
5efe6a56 5668 cperl-font-lock-keywords-1)
db133cb6
RS
5669
5670(defun cperl-load-font-lock-keywords-2 ()
5671 (or cperl-faces-init (cperl-init-faces))
5efe6a56 5672 cperl-font-lock-keywords-2)
f83d2997 5673
5bd52f0e
RS
5674(defun cperl-init-faces-weak ()
5675 ;; Allow `cperl-find-pods-heres' to run.
5676 (or (boundp 'font-lock-constant-face)
5677 (cperl-force-face font-lock-constant-face
4ab89e7b
SM
5678 "Face for constant and label names"))
5679 (or (boundp 'font-lock-warning-face)
5680 (cperl-force-face font-lock-warning-face
5681 "Face for things which should stand out"))
5682 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5683 )
5bd52f0e 5684
f83d2997 5685(defun cperl-init-faces ()
5bd52f0e 5686 (condition-case errs
f83d2997
KH
5687 (progn
5688 (require 'font-lock)
5689 (and (fboundp 'font-lock-fontify-anchored-keywords)
5690 (featurep 'font-lock-extra)
5691 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5692 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
f83d2997
KH
5693 (if (fboundp 'font-lock-fontify-anchored-keywords)
5694 (setq font-lock-anchored t))
5c8b7eaf 5695 (setq
f83d2997
KH
5696 t-font-lock-keywords
5697 (list
1f5c1626 5698 `("[ \t]+$" 0 ',cperl-invalid-face t)
f83d2997
KH
5699 (cons
5700 (concat
5701 "\\(^\\|[^$@%&\\]\\)\\<\\("
5702 (mapconcat
5703 'identity
5704 '("if" "until" "while" "elsif" "else" "unless" "for"
5705 "foreach" "continue" "exit" "die" "last" "goto" "next"
4ab89e7b 5706 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
6c389151 5707 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
f83d2997
KH
5708 "\\|") ; Flow control
5709 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5710 ; In what follows we use `type' style
5711 ; for overwritable builtins
5712 (list
5713 (concat
5714 "\\(^\\|[^$@%&\\]\\)\\<\\("
5715 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5716 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5717 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5718 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5719 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5720 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5721 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5722 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5723 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5724 ;; "gethostbyname" "gethostent" "getlogin"
5725 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5726 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5727 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5728 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5729 ;; "getservbyport" "getservent" "getsockname"
5730 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5731 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5bd52f0e 5732 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
f83d2997
KH
5733 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5734 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5735 ;; "quotemeta" "rand" "read" "readdir" "readline"
5736 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5737 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5738 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5739 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5740 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5741 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5742 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5743 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
6c389151 5744 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
f83d2997
KH
5745 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5746 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5747 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5c8b7eaf 5748 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
f83d2997
KH
5749 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5750 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5751 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5752 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5753 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5754 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5755 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5756 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5757 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5758 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5759 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5760 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5761 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5762 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5763 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5bd52f0e 5764 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
f83d2997
KH
5765 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5766 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5767 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5768 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5769 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5770 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5771 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5772 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
6c389151 5773 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
f83d2997
KH
5774 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5775 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5776 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5777 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5778 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5779 "\\)\\>") 2 'font-lock-type-face)
5780 ;; In what follows we use `other' style
5781 ;; for nonoverwritable builtins
5782 ;; Somehow 's', 'm' are not auto-generated???
5783 (list
5784 (concat
5785 "\\(^\\|[^$@%&\\]\\)\\<\\("
6c389151 5786 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
f83d2997
KH
5787 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5788 ;; "eval" "exists" "for" "foreach" "format" "goto"
5789 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
4ab89e7b 5790 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
f83d2997
KH
5791 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5792 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5793 ;; "undef" "unless" "unshift" "untie" "until" "use"
5794 ;; "while" "y"
6c389151 5795 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
f83d2997 5796 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
6c389151
SM
5797 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5798 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
f83d2997 5799 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5bd52f0e 5800 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
f83d2997
KH
5801 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5802 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5803 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5804 "\\|[sm]" ; Added manually
4ab89e7b 5805 "\\)\\>") 2 'cperl-nonoverridable-face)
f83d2997
KH
5806 ;; (mapconcat 'identity
5807 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5808 ;; "#include" "#define" "#undef")
5809 ;; "\\|")
5810 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5811 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
4c36be58 5812 ;; This highlights declarations and definitions differently.
4ab89e7b
SM
5813 ;; We do not try to highlight in the case of attributes:
5814 ;; it is already done by `cperl-find-pods-heres'
5815 (list (concat "\\<sub"
5816 cperl-white-and-comment-rex ; whitespace/comments
5817 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5818 "\\("
5819 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5820 "([^()]*)\\)?" ; prototype
5821 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5822 "[{;]")
5823 2 (if cperl-font-lock-multiline
5824 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5825 'font-lock-function-name-face
5826 'font-lock-variable-name-face)
5827 ;; need to manually set 'multiline' for older font-locks
5828 '(progn
5829 (if (< 1 (count-lines (match-beginning 0)
5830 (match-end 0)))
5831 (put-text-property
5832 (+ 3 (match-beginning 0)) (match-end 0)
5833 'syntax-type 'multiline))
5834 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5835 'font-lock-function-name-face
5836 'font-lock-variable-name-face))))
f83d2997
KH
5837 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5838 2 font-lock-function-name-face)
5839 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5840 1 font-lock-function-name-face)
5841 (cond ((featurep 'font-lock-extra)
5c8b7eaf 5842 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
f83d2997
KH
5843 (2 font-lock-string-face t)
5844 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5845 (font-lock-anchored
5846 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5847 (2 font-lock-string-face t)
5848 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5849 nil nil
5850 (1 font-lock-string-face t))))
5851 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5852 2 font-lock-string-face t)))
db133cb6 5853 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
f83d2997 5854 font-lock-string-face t)
5c8b7eaf 5855 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
83261a2f 5856 font-lock-constant-face) ; labels
f83d2997 5857 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
883212ce 5858 2 font-lock-constant-face)
6c389151
SM
5859 ;; Uncomment to get perl-mode-like vars
5860 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5861 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5862 ;;; (2 (cons font-lock-variable-name-face '(underline))))
f83d2997 5863 (cond ((featurep 'font-lock-extra)
6c389151 5864 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
f83d2997
KH
5865 (3 font-lock-variable-name-face)
5866 (4 '(another 4 nil
5867 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5868 (1 font-lock-variable-name-face)
5c8b7eaf 5869 (2 '(restart 2 nil) nil t)))
f83d2997
KH
5870 nil t))) ; local variables, multiple
5871 (font-lock-anchored
4ab89e7b 5872 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
9edd6ee6 5873 `(,(concat "\\<\\(my\\|local\\|our\\)"
4ab89e7b
SM
5874 cperl-maybe-white-and-comment-rex
5875 "\\(("
5876 cperl-maybe-white-and-comment-rex
9edd6ee6
SM
5877 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5878 (5 ,(if cperl-font-lock-multiline
4ab89e7b
SM
5879 'font-lock-variable-name-face
5880 '(progn (setq cperl-font-lock-multiline-start
5881 (match-beginning 0))
9edd6ee6
SM
5882 'font-lock-variable-name-face)))
5883 (,(concat "\\="
4ab89e7b
SM
5884 cperl-maybe-white-and-comment-rex
5885 ","
5886 cperl-maybe-white-and-comment-rex
9edd6ee6 5887 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
cb5bf6ba 5888 ;; Bug in font-lock: limit is used not only to limit
4ab89e7b
SM
5889 ;; searches, but to set the "extend window for
5890 ;; facification" property. Thus we need to minimize.
9edd6ee6 5891 ,(if cperl-font-lock-multiline
4ab89e7b
SM
5892 '(if (match-beginning 3)
5893 (save-excursion
5894 (goto-char (match-beginning 3))
5895 (condition-case nil
5896 (forward-sexp 1)
5897 (error
5898 (condition-case nil
5899 (forward-char 200)
5900 (error nil)))) ; typeahead
5901 (1- (point))) ; report limit
5902 (forward-char -2)) ; disable continued expr
5903 '(if (match-beginning 3)
5904 (point-max) ; No limit for continuation
9edd6ee6
SM
5905 (forward-char -2))) ; disable continued expr
5906 ,(if cperl-font-lock-multiline
4ab89e7b
SM
5907 nil
5908 '(progn ; Do at end
5909 ;; "my" may be already fontified (POD),
5910 ;; so cperl-font-lock-multiline-start is nil
5911 (if (or (not cperl-font-lock-multiline-start)
5912 (> 2 (count-lines
5913 cperl-font-lock-multiline-start
5914 (point))))
5915 nil
5916 (put-text-property
5917 (1+ cperl-font-lock-multiline-start) (point)
5918 'syntax-type 'multiline))
9edd6ee6
SM
5919 (setq cperl-font-lock-multiline-start nil)))
5920 (3 font-lock-variable-name-face))))
4ab89e7b 5921 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
f83d2997 5922 3 font-lock-variable-name-face)))
6c389151 5923 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
eb6121fc 5924 4 font-lock-variable-name-face)
bbd240ce 5925 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntactically
9ed9fd35 5926 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
eb6121fc 5927 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
a1506d29 5928 (setq
f83d2997
KH
5929 t-font-lock-keywords-1
5930 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
4ab89e7b
SM
5931 ;; not yet as of XEmacs 19.12, works with 21.1.11
5932 (or
6546555e 5933 (not (featurep 'xemacs))
4ab89e7b
SM
5934 (string< "21.1.9" emacs-version)
5935 (and (string< "21.1.10" emacs-version)
5936 (string< emacs-version "21.1.2")))
f83d2997
KH
5937 '(
5938 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5939 (if (eq (char-after (match-beginning 2)) ?%)
4ab89e7b
SM
5940 'cperl-hash-face
5941 'cperl-array-face)
f83d2997
KH
5942 t) ; arrays and hashes
5943 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5944 1
5c8b7eaf 5945 (if (= (- (match-end 2) (match-beginning 2)) 1)
f83d2997 5946 (if (eq (char-after (match-beginning 3)) ?{)
4ab89e7b
SM
5947 'cperl-hash-face
5948 'cperl-array-face) ; arrays and hashes
f83d2997
KH
5949 font-lock-variable-name-face) ; Just to put something
5950 t)
4ab89e7b
SM
5951 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5952 (1 cperl-array-face)
5953 (2 font-lock-variable-name-face))
5954 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5955 (1 cperl-hash-face)
5956 (2 font-lock-variable-name-face))
f83d2997
KH
5957 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5958 ;;; Too much noise from \s* @s[ and friends
5c8b7eaf 5959 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
f83d2997
KH
5960 ;;(3 font-lock-function-name-face t t)
5961 ;;(4
5962 ;; (if (cperl-slash-is-regexp)
5963 ;; font-lock-function-name-face 'default) nil t))
5964 )))
6c389151
SM
5965 (if cperl-highlight-variables-indiscriminately
5966 (setq t-font-lock-keywords-1
5967 (append t-font-lock-keywords-1
4ab89e7b 5968 (list '("\\([$*]{?\\sw+\\)" 1
6c389151 5969 font-lock-variable-name-face)))))
a1506d29 5970 (setq cperl-font-lock-keywords-1
5bd52f0e
RS
5971 (if cperl-syntaxify-by-font-lock
5972 (cons 'cperl-fontify-update
5973 t-font-lock-keywords)
5974 t-font-lock-keywords)
5efe6a56
SM
5975 cperl-font-lock-keywords cperl-font-lock-keywords-1
5976 cperl-font-lock-keywords-2 (append
6c389151
SM
5977 cperl-font-lock-keywords-1
5978 t-font-lock-keywords-1)))
f83d2997
KH
5979 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5980 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
db133cb6 5981 (eval ; Avoid a warning
83261a2f
SM
5982 '(font-lock-require-faces
5983 (list
5984 ;; Color-light Color-dark Gray-light Gray-dark Mono
5985 (list 'font-lock-comment-face
5986 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5987 nil
5988 [nil nil t t t]
5989 [nil nil t t t]
5990 nil)
5991 (list 'font-lock-string-face
5992 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5993 nil
5994 nil
5995 [nil nil t t t]
5996 nil)
5997 (list 'font-lock-function-name-face
5998 (vector
5999 "Blue" "LightSkyBlue" "Gray50" "LightGray"
6000 (cdr (assq 'background-color ; if mono
6001 (frame-parameters))))
6002 (vector
6003 nil nil nil nil
6004 (cdr (assq 'foreground-color ; if mono
6005 (frame-parameters))))
6006 [nil nil t t t]
6007 nil
6008 nil)
6009 (list 'font-lock-variable-name-face
6010 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6011 nil
6012 [nil nil t t t]
6013 [nil nil t t t]
6014 nil)
6015 (list 'font-lock-type-face
6016 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6017 nil
6018 [nil nil t t t]
6019 nil
6020 [nil nil t t t])
4ab89e7b
SM
6021 (list 'font-lock-warning-face
6022 ["Pink" "Red" "Gray50" "LightGray"]
6023 ["gray20" "gray90"
6024 "gray80" "gray20"]
6025 [nil nil t t t]
6026 nil
6027 [nil nil t t t]
6028 )
83261a2f
SM
6029 (list 'font-lock-constant-face
6030 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6031 nil
6032 [nil nil t t t]
6033 nil
6034 [nil nil t t t])
4ab89e7b 6035 (list 'cperl-nonoverridable-face
83261a2f
SM
6036 ["chartreuse3" ("orchid1" "orange")
6037 nil "Gray80"]
6038 [nil nil "gray90"]
6039 [nil nil nil t t]
6040 [nil nil t t]
6041 [nil nil t t t])
4ab89e7b 6042 (list 'cperl-array-face
83261a2f
SM
6043 ["blue" "yellow" nil "Gray80"]
6044 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6045 "gray90"]
6046 t
6047 nil
6048 nil)
4ab89e7b 6049 (list 'cperl-hash-face
83261a2f
SM
6050 ["red" "red" nil "Gray80"]
6051 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6052 "gray90"]
6053 t
6054 t
6055 nil))))
5bd52f0e 6056 ;; Do it the dull way, without choose-color
f83d2997
KH
6057 (defvar cperl-guessed-background nil
6058 "Display characteristics as guessed by cperl.")
83261a2f 6059 ;; (or (fboundp 'x-color-defined-p)
15ca5699 6060 ;; (defalias 'x-color-defined-p
83261a2f
SM
6061 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6062 ;; ;; XEmacs >= 19.12
6063 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6064 ;; ;; XEmacs 19.11
6065 ;; (t 'x-valid-color-name-p))))
5c8b7eaf 6066 (cperl-force-face font-lock-constant-face
5bd52f0e
RS
6067 "Face for constant and label names")
6068 (cperl-force-face font-lock-variable-name-face
6069 "Face for variable names")
6070 (cperl-force-face font-lock-type-face
6071 "Face for data types")
4ab89e7b 6072 (cperl-force-face cperl-nonoverridable-face
5bd52f0e 6073 "Face for data types from another group")
4ab89e7b
SM
6074 (cperl-force-face font-lock-warning-face
6075 "Face for things which should stand out")
5bd52f0e
RS
6076 (cperl-force-face font-lock-comment-face
6077 "Face for comments")
6078 (cperl-force-face font-lock-function-name-face
6079 "Face for function names")
4ab89e7b 6080 (cperl-force-face cperl-hash-face
5bd52f0e 6081 "Face for hashes")
4ab89e7b 6082 (cperl-force-face cperl-array-face
5bd52f0e
RS
6083 "Face for arrays")
6084 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6085 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6086 ;;(or (boundp 'font-lock-type-face)
6087 ;; (defconst font-lock-type-face
6088 ;; 'font-lock-type-face
6089 ;; "Face to use for data types."))
6090 ;;(or (boundp 'cperl-nonoverridable-face)
6091 ;; (defconst cperl-nonoverridable-face
4ab89e7b 6092 ;; 'cperl-nonoverridable-face
5bd52f0e 6093 ;; "Face to use for data types from another group."))
6546555e 6094 ;;(if (not (featurep 'xemacs)) nil
5bd52f0e
RS
6095 ;; (or (boundp 'font-lock-comment-face)
6096 ;; (defconst font-lock-comment-face
6097 ;; 'font-lock-comment-face
6098 ;; "Face to use for comments."))
6099 ;; (or (boundp 'font-lock-keyword-face)
6100 ;; (defconst font-lock-keyword-face
6101 ;; 'font-lock-keyword-face
6102 ;; "Face to use for keywords."))
6103 ;; (or (boundp 'font-lock-function-name-face)
6104 ;; (defconst font-lock-function-name-face
6105 ;; 'font-lock-function-name-face
6106 ;; "Face to use for function names.")))
6107 (if (and
4ab89e7b 6108 (not (cperl-is-face 'cperl-array-face))
5c8b7eaf 6109 (cperl-is-face 'font-lock-emphasized-face))
4ab89e7b 6110 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
5bd52f0e 6111 (if (and
4ab89e7b 6112 (not (cperl-is-face 'cperl-hash-face))
5c8b7eaf 6113 (cperl-is-face 'font-lock-other-emphasized-face))
4ab89e7b 6114 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
5bd52f0e 6115 (if (and
4ab89e7b 6116 (not (cperl-is-face 'cperl-nonoverridable-face))
5c8b7eaf 6117 (cperl-is-face 'font-lock-other-type-face))
4ab89e7b 6118 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
5bd52f0e
RS
6119 ;;(or (boundp 'cperl-hash-face)
6120 ;; (defconst cperl-hash-face
4ab89e7b 6121 ;; 'cperl-hash-face
5bd52f0e
RS
6122 ;; "Face to use for hashes."))
6123 ;;(or (boundp 'cperl-array-face)
6124 ;; (defconst cperl-array-face
4ab89e7b 6125 ;; 'cperl-array-face
5bd52f0e 6126 ;; "Face to use for arrays."))
f83d2997
KH
6127 ;; Here we try to guess background
6128 (let ((background
6129 (if (boundp 'font-lock-background-mode)
6130 font-lock-background-mode
5c8b7eaf 6131 'light))
83261a2f 6132 (face-list (and (fboundp 'face-list) (face-list))))
5bd52f0e
RS
6133;;;; (fset 'cperl-is-face
6134;;;; (cond ((fboundp 'find-face)
6135;;;; (symbol-function 'find-face))
6136;;;; (face-list
6137;;;; (function (lambda (face) (member face face-list))))
6138;;;; (t
6139;;;; (function (lambda (face) (boundp face))))))
f83d2997
KH
6140 (defvar cperl-guessed-background
6141 (if (and (boundp 'font-lock-display-type)
6142 (eq font-lock-display-type 'grayscale))
6143 'gray
6144 background)
6145 "Background as guessed by CPerl mode")
83261a2f
SM
6146 (and (not (cperl-is-face 'font-lock-constant-face))
6147 (cperl-is-face 'font-lock-reference-face)
6148 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
db133cb6 6149 (if (cperl-is-face 'font-lock-type-face) nil
f83d2997
KH
6150 (copy-face 'default 'font-lock-type-face)
6151 (cond
6152 ((eq background 'light)
6153 (set-face-foreground 'font-lock-type-face
6154 (if (x-color-defined-p "seagreen")
6155 "seagreen"
6156 "sea green")))
6157 ((eq background 'dark)
6158 (set-face-foreground 'font-lock-type-face
6159 (if (x-color-defined-p "os2pink")
6160 "os2pink"
6161 "pink")))
6162 (t
6163 (set-face-background 'font-lock-type-face "gray90"))))
4ab89e7b 6164 (if (cperl-is-face 'cperl-nonoverridable-face)
f83d2997 6165 nil
4ab89e7b 6166 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
f83d2997
KH
6167 (cond
6168 ((eq background 'light)
4ab89e7b 6169 (set-face-foreground 'cperl-nonoverridable-face
f83d2997
KH
6170 (if (x-color-defined-p "chartreuse3")
6171 "chartreuse3"
6172 "chartreuse")))
6173 ((eq background 'dark)
4ab89e7b 6174 (set-face-foreground 'cperl-nonoverridable-face
f83d2997
KH
6175 (if (x-color-defined-p "orchid1")
6176 "orchid1"
6177 "orange")))))
5bd52f0e
RS
6178;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6179;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6180;;; (cond
6181;;; ((eq background 'light)
6182;;; (set-face-background 'font-lock-other-emphasized-face
6183;;; (if (x-color-defined-p "lightyellow2")
6184;;; "lightyellow2"
6185;;; (if (x-color-defined-p "lightyellow")
6186;;; "lightyellow"
6187;;; "light yellow"))))
6188;;; ((eq background 'dark)
6189;;; (set-face-background 'font-lock-other-emphasized-face
6190;;; (if (x-color-defined-p "navy")
6191;;; "navy"
6192;;; (if (x-color-defined-p "darkgreen")
6193;;; "darkgreen"
6194;;; "dark green"))))
6195;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6196;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6197;;; (copy-face 'bold 'font-lock-emphasized-face)
6198;;; (cond
6199;;; ((eq background 'light)
6200;;; (set-face-background 'font-lock-emphasized-face
6201;;; (if (x-color-defined-p "lightyellow2")
6202;;; "lightyellow2"
6203;;; "lightyellow")))
6204;;; ((eq background 'dark)
6205;;; (set-face-background 'font-lock-emphasized-face
6206;;; (if (x-color-defined-p "navy")
6207;;; "navy"
6208;;; (if (x-color-defined-p "darkgreen")
6209;;; "darkgreen"
6210;;; "dark green"))))
6211;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
db133cb6 6212 (if (cperl-is-face 'font-lock-variable-name-face) nil
f83d2997 6213 (copy-face 'italic 'font-lock-variable-name-face))
db133cb6 6214 (if (cperl-is-face 'font-lock-constant-face) nil
883212ce 6215 (copy-face 'italic 'font-lock-constant-face))))
f83d2997 6216 (setq cperl-faces-init t))
5bd52f0e 6217 (error (message "cperl-init-faces (ignored): %s" errs))))
f83d2997
KH
6218
6219
1d653303
GM
6220(defvar ps-bold-faces)
6221(defvar ps-italic-faces)
6222(defvar ps-underlined-faces)
6223
f83d2997
KH
6224(defun cperl-ps-print-init ()
6225 "Initialization of `ps-print' components for faces used in CPerl."
5bd52f0e
RS
6226 (eval-after-load "ps-print"
6227 '(setq ps-bold-faces
5c8b7eaf 6228 ;; font-lock-variable-name-face
5bd52f0e 6229 ;; font-lock-constant-face
4ab89e7b 6230 (append '(cperl-array-face cperl-hash-face)
5bd52f0e
RS
6231 ps-bold-faces)
6232 ps-italic-faces
6233 ;; font-lock-constant-face
4ab89e7b 6234 (append '(cperl-nonoverridable-face cperl-hash-face)
5bd52f0e
RS
6235 ps-italic-faces)
6236 ps-underlined-faces
6237 ;; font-lock-type-face
4ab89e7b 6238 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
5bd52f0e
RS
6239 ps-underlined-faces))))
6240
6241(defvar ps-print-face-extension-alist)
6242
6243(defun cperl-ps-print (&optional file)
6244 "Pretty-print in CPerl style.
6245If optional argument FILE is an empty string, prints to printer, otherwise
6246to the file FILE. If FILE is nil, prompts for a file name.
6247
6248Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6249 (interactive)
5c8b7eaf
SS
6250 (or file
6251 (setq file (read-from-minibuffer
5bd52f0e
RS
6252 "Print to file (if empty - to printer): "
6253 (concat (buffer-file-name) ".ps")
6254 nil nil 'file-name-history)))
6255 (or (> (length file) 0)
6256 (setq file nil))
6257 (require 'ps-print) ; To get ps-print-face-extension-alist
6258 (let ((ps-print-color-p t)
6259 (ps-print-face-extension-alist ps-print-face-extension-alist))
6260 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6261 (ps-print-buffer-with-faces file)))
6262
6263;;; (defun cperl-ps-print-init ()
6264;;; "Initialization of `ps-print' components for faces used in CPerl."
6265;;; ;; Guard against old versions
6266;;; (defvar ps-underlined-faces nil)
6267;;; (defvar ps-bold-faces nil)
6268;;; (defvar ps-italic-faces nil)
6269;;; (setq ps-bold-faces
6270;;; (append '(font-lock-emphasized-face
4ab89e7b 6271;;; cperl-array-face
5c8b7eaf
SS
6272;;; font-lock-keyword-face
6273;;; font-lock-variable-name-face
6274;;; font-lock-constant-face
6275;;; font-lock-reference-face
5bd52f0e 6276;;; font-lock-other-emphasized-face
4ab89e7b 6277;;; cperl-hash-face)
5bd52f0e
RS
6278;;; ps-bold-faces))
6279;;; (setq ps-italic-faces
4ab89e7b 6280;;; (append '(cperl-nonoverridable-face
5c8b7eaf
SS
6281;;; font-lock-constant-face
6282;;; font-lock-reference-face
5bd52f0e 6283;;; font-lock-other-emphasized-face
4ab89e7b 6284;;; cperl-hash-face)
5bd52f0e
RS
6285;;; ps-italic-faces))
6286;;; (setq ps-underlined-faces
6287;;; (append '(font-lock-emphasized-face
4ab89e7b 6288;;; cperl-array-face
5bd52f0e 6289;;; font-lock-other-emphasized-face
4ab89e7b
SM
6290;;; cperl-hash-face
6291;;; cperl-nonoverridable-face font-lock-type-face)
5bd52f0e
RS
6292;;; ps-underlined-faces))
6293;;; (cons 'font-lock-type-face ps-underlined-faces))
f83d2997
KH
6294
6295
6296(if (cperl-enable-font-lock) (cperl-windowed-init))
6297
db133cb6 6298(defconst cperl-styles-entries
5c8b7eaf
SS
6299 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6300 cperl-label-offset cperl-extra-newline-before-brace
4ab89e7b 6301 cperl-extra-newline-before-brace-multiline
bab27c0c 6302 cperl-merge-trailing-else
db133cb6
RS
6303 cperl-continued-statement-offset))
6304
4ab89e7b
SM
6305(defconst cperl-style-examples
6306"##### Numbers etc are: cperl-indent-level cperl-brace-offset
6307##### cperl-continued-brace-offset cperl-label-offset
6308##### cperl-continued-statement-offset
6309##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6310
6311########### (Do not forget cperl-extra-newline-before-brace-multiline)
6312
6313### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6314if (foo) {
6315 bar
6316 baz;
6317 label:
6318 {
6319 boon;
6320 }
6321} else {
6322 stop;
6323}
6324
6325### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6326if (foo) {
6327 bar
6328 baz;
6329 label:
6330 {
6331 boon;
6332 }
6333} else {
6334 stop;
6335}
6336
6337### GNU 2/0/0/-2/2/nil/t
6338if (foo)
6339 {
6340 bar
6341 baz;
6342 label:
6343 {
6344 boon;
6345 }
6346 }
6347else
6348 {
6349 stop;
6350 }
6351
6352### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6353if (foo)
6354{
6355 bar
6356 baz;
6357 label:
6358 {
6359 boon;
6360 }
6361}
6362else
6363{
6364 stop;
6365}
6366
6367### BSD (=C++, but will not change preexisting merge-trailing-else
6368### and extra-newline-before-brace ) 4/0/-4/-4/4
6369if (foo)
6370{
6371 bar
6372 baz;
6373 label:
6374 {
6375 boon;
6376 }
6377}
6378else
6379{
6380 stop;
6381}
6382
6383### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6384### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6385if (foo)
6386{
6387 bar
6388 baz;
6389 label:
6390 {
6391 boon;
6392 }
6393}
6394else
6395{
6396 stop;
6397}
6398
6399### Whitesmith (=PerlStyle, but will not change preexisting
6400### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6401if (foo)
6402 {
6403 bar
6404 baz;
6405 label:
6406 {
6407 boon;
6408 }
6409 }
6410else
6411 {
6412 stop;
6413 }
6414"
6415"Examples of if/else with different indent styles (with v4.23).")
6416
db133cb6 6417(defconst cperl-style-alist
4ab89e7b 6418 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
db133cb6
RS
6419 (cperl-indent-level . 2)
6420 (cperl-brace-offset . 0)
6421 (cperl-continued-brace-offset . 0)
6422 (cperl-label-offset . -2)
4ab89e7b 6423 (cperl-continued-statement-offset . 2)
db133cb6 6424 (cperl-extra-newline-before-brace . nil)
4ab89e7b
SM
6425 (cperl-extra-newline-before-brace-multiline . nil)
6426 (cperl-merge-trailing-else . t))
6427
83261a2f 6428 ("PerlStyle" ; CPerl with 4 as indent
db133cb6
RS
6429 (cperl-indent-level . 4)
6430 (cperl-brace-offset . 0)
6431 (cperl-continued-brace-offset . 0)
6432 (cperl-label-offset . -4)
4ab89e7b 6433 (cperl-continued-statement-offset . 4)
db133cb6 6434 (cperl-extra-newline-before-brace . nil)
4ab89e7b
SM
6435 (cperl-extra-newline-before-brace-multiline . nil)
6436 (cperl-merge-trailing-else . t))
6437
db133cb6
RS
6438 ("GNU"
6439 (cperl-indent-level . 2)
6440 (cperl-brace-offset . 0)
6441 (cperl-continued-brace-offset . 0)
6442 (cperl-label-offset . -2)
4ab89e7b 6443 (cperl-continued-statement-offset . 2)
db133cb6 6444 (cperl-extra-newline-before-brace . t)
4ab89e7b
SM
6445 (cperl-extra-newline-before-brace-multiline . t)
6446 (cperl-merge-trailing-else . nil))
6447
db133cb6
RS
6448 ("K&R"
6449 (cperl-indent-level . 5)
6450 (cperl-brace-offset . 0)
6451 (cperl-continued-brace-offset . -5)
6452 (cperl-label-offset . -5)
4ab89e7b 6453 (cperl-continued-statement-offset . 5)
db133cb6 6454 ;;(cperl-extra-newline-before-brace . nil) ; ???
4ab89e7b
SM
6455 ;;(cperl-extra-newline-before-brace-multiline . nil)
6456 (cperl-merge-trailing-else . nil))
6457
db133cb6
RS
6458 ("BSD"
6459 (cperl-indent-level . 4)
6460 (cperl-brace-offset . 0)
6461 (cperl-continued-brace-offset . -4)
6462 (cperl-label-offset . -4)
4ab89e7b 6463 (cperl-continued-statement-offset . 4)
db133cb6 6464 ;;(cperl-extra-newline-before-brace . nil) ; ???
4ab89e7b
SM
6465 ;;(cperl-extra-newline-before-brace-multiline . nil)
6466 ;;(cperl-merge-trailing-else . nil) ; ???
6467 )
6468
db133cb6
RS
6469 ("C++"
6470 (cperl-indent-level . 4)
6471 (cperl-brace-offset . 0)
6472 (cperl-continued-brace-offset . -4)
6473 (cperl-label-offset . -4)
6474 (cperl-continued-statement-offset . 4)
4ab89e7b
SM
6475 (cperl-extra-newline-before-brace . t)
6476 (cperl-extra-newline-before-brace-multiline . t)
6477 (cperl-merge-trailing-else . nil))
6478
db133cb6
RS
6479 ("Whitesmith"
6480 (cperl-indent-level . 4)
6481 (cperl-brace-offset . 0)
6482 (cperl-continued-brace-offset . 0)
6483 (cperl-label-offset . -4)
4ab89e7b 6484 (cperl-continued-statement-offset . 4)
db133cb6 6485 ;;(cperl-extra-newline-before-brace . nil) ; ???
4ab89e7b
SM
6486 ;;(cperl-extra-newline-before-brace-multiline . nil)
6487 ;;(cperl-merge-trailing-else . nil) ; ???
6488 )
6489 ("Current"))
6490 "List of variables to set to get a particular indentation style.
6491Should be used via `cperl-set-style' or via Perl menu.
6492
6493See examples in `cperl-style-examples'.")
db133cb6 6494
f83d2997 6495(defun cperl-set-style (style)
f94a632a 6496 "Set CPerl mode variables to use one of several different indentation styles.
f83d2997 6497The arguments are a string representing the desired style.
5c8b7eaf 6498The list of styles is in `cperl-style-alist', available styles
4ab89e7b 6499are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
db133cb6
RS
6500
6501The current value of style is memorized (unless there is a memorized
6502data already), may be restored by `cperl-set-style-back'.
6503
fe3c5669 6504Choosing \"Current\" style will not change style, so this may be used for
4ab89e7b 6505side-effect of memorizing only. Examples in `cperl-style-examples'."
5c8b7eaf 6506 (interactive
15ca5699 6507 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
db133cb6 6508 cperl-style-alist)))
f83d2997 6509 (list (completing-read "Enter style: " list nil 'insist))))
db133cb6
RS
6510 (or cperl-old-style
6511 (setq cperl-old-style
6512 (mapcar (function
6513 (lambda (name)
6514 (cons name (eval name))))
6515 cperl-styles-entries)))
6516 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
f83d2997
KH
6517 (while style
6518 (setq setting (car style) style (cdr style))
db133cb6
RS
6519 (set (car setting) (cdr setting)))))
6520
6521(defun cperl-set-style-back ()
810fb442 6522 "Restore a style memorized by `cperl-set-style'."
db133cb6
RS
6523 (interactive)
6524 (or cperl-old-style (error "The style was not changed"))
6525 (let (setting)
6526 (while cperl-old-style
5c8b7eaf 6527 (setq setting (car cperl-old-style)
db133cb6
RS
6528 cperl-old-style (cdr cperl-old-style))
6529 (set (car setting) (cdr setting)))))
f83d2997
KH
6530
6531(defun cperl-check-syntax ()
6532 (interactive)
6533 (require 'mode-compile)
db133cb6
RS
6534 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6535 (eval '(mode-compile)))) ; Avoid a warning
f83d2997 6536
ded62b08 6537(declare-function Info-find-node "info"
16d9f896 6538 (filename nodename &optional no-going-back strict-case))
ded62b08 6539
f83d2997
KH
6540(defun cperl-info-buffer (type)
6541 ;; Returns buffer with documentation. Creates if missing.
6542 ;; If TYPE, this vars buffer.
6543 ;; Special care is taken to not stomp over an existing info buffer
6544 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6545 (info (get-buffer bname))
6546 (oldbuf (get-buffer "*info*")))
6547 (if info info
6548 (save-window-excursion
6549 ;; Get Info running
6550 (require 'info)
6551 (cond (oldbuf
6552 (set-buffer oldbuf)
6553 (rename-buffer "*info-perl-tmp*")))
6554 (save-window-excursion
6555 (info))
6556 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6557 (set-buffer "*info*")
6558 (rename-buffer bname)
6559 (cond (oldbuf
6560 (set-buffer "*info-perl-tmp*")
6561 (rename-buffer "*info*")
6562 (set-buffer bname)))
029cb4d5 6563 (make-local-variable 'window-min-height)
f83d2997
KH
6564 (setq window-min-height 2)
6565 (current-buffer)))))
6566
6567(defun cperl-word-at-point (&optional p)
f94a632a 6568 "Return the word at point or at P."
f83d2997
KH
6569 (save-excursion
6570 (if p (goto-char p))
6571 (or (cperl-word-at-point-hard)
6572 (progn
6573 (require 'etags)
6574 (funcall (or (and (boundp 'find-tag-default-function)
6575 find-tag-default-function)
6576 (get major-mode 'find-tag-default-function)
6577 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6578 ;; automatically used within `find-tag-default':
6579 'find-tag-default))))))
6580
6581(defun cperl-info-on-command (command)
f94a632a 6582 "Show documentation for Perl command COMMAND in other window.
f83d2997
KH
6583If perl-info buffer is shown in some frame, uses this frame.
6584Customized by setting variables `cperl-shrink-wrap-info-frame',
6585`cperl-max-help-size'."
5c8b7eaf 6586 (interactive
f83d2997 6587 (let* ((default (cperl-word-at-point))
5c8b7eaf 6588 (read (read-string
83261a2f
SM
6589 (format "Find doc for Perl function (default %s): "
6590 default))))
5c8b7eaf 6591 (list (if (equal read "")
83261a2f
SM
6592 default
6593 read))))
f83d2997
KH
6594
6595 (let ((buffer (current-buffer))
6596 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6597 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6598 max-height char-height buf-list)
6599 (if (string-match "^-[a-zA-Z]$" command)
6600 (setq cmd-desc "^-X[ \t\n]"))
6601 (setq isvar (string-match "^[$@%]" command)
6602 buf (cperl-info-buffer isvar)
6603 iniwin (selected-window)
6604 fr1 (window-frame iniwin))
6605 (set-buffer buf)
fc49c9c6 6606 (goto-char (point-min))
5c8b7eaf 6607 (or isvar
f83d2997
KH
6608 (progn (re-search-forward "^-X[ \t\n]")
6609 (forward-line -1)))
6610 (if (re-search-forward cmd-desc nil t)
6611 (progn
6612 ;; Go back to beginning of the group (ex, for qq)
6613 (if (re-search-backward "^[ \t\n\f]")
6614 (forward-line 1))
6615 (beginning-of-line)
5c8b7eaf 6616 ;; Get some of
f83d2997
KH
6617 (setq pos (point)
6618 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6619 (while (and (not win) buf-list)
6620 (setq win (get-buffer-window (car buf-list) t))
6621 (setq buf-list (cdr buf-list)))
6622 (or (not win)
6623 (eq (window-buffer win) buf)
6624 (set-window-buffer win buf))
6625 (and win (setq fr2 (window-frame win)))
6626 (if (or (not fr2) (eq fr1 fr2))
6627 (pop-to-buffer buf)
6628 (special-display-popup-frame buf) ; Make it visible
6629 (select-window win))
6630 (goto-char pos) ; Needed (?!).
6631 ;; Resize
6632 (setq iniheight (window-height)
6633 frheight (frame-height)
6634 not-loner (< iniheight (1- frheight))) ; Are not alone
5c8b7eaf 6635 (cond ((if not-loner cperl-max-help-size
f83d2997 6636 cperl-shrink-wrap-info-frame)
5c8b7eaf
SS
6637 (setq height
6638 (+ 2
6639 (count-lines
6640 pos
f83d2997
KH
6641 (save-excursion
6642 (if (re-search-forward
6643 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6644 (match-beginning 0) (point-max)))))
5c8b7eaf 6645 max-height
f83d2997
KH
6646 (if not-loner
6647 (/ (* (- frheight 3) cperl-max-help-size) 100)
6648 (setq char-height (frame-char-height))
6649 ;; Non-functioning under OS/2:
6650 (if (eq char-height 1) (setq char-height 18))
6651 ;; Title, menubar, + 2 for slack
d431decb 6652 (- (/ (display-pixel-height) char-height) 4)))
f83d2997
KH
6653 (if (> height max-height) (setq height max-height))
6654 ;;(message "was %s doing %s" iniheight height)
6655 (if not-loner
6656 (enlarge-window (- height iniheight))
6657 (set-frame-height (window-frame win) (1+ height)))))
6658 (set-window-start (selected-window) pos))
6659 (message "No entry for %s found." command))
6660 ;;(pop-to-buffer buffer)
6661 (select-window iniwin)))
6662
6663(defun cperl-info-on-current-command ()
029cb4d5 6664 "Show documentation for Perl command at point in other window."
f83d2997
KH
6665 (interactive)
6666 (cperl-info-on-command (cperl-word-at-point)))
6667
6668(defun cperl-imenu-info-imenu-search ()
6669 (if (looking-at "^-X[ \t\n]") nil
6670 (re-search-backward
6671 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6672 (forward-line 1)))
6673
5c8b7eaf 6674(defun cperl-imenu-info-imenu-name ()
f83d2997
KH
6675 (buffer-substring
6676 (match-beginning 1) (match-end 1)))
6677
3694d13f
GM
6678(declare-function imenu-choose-buffer-index "imenu" (&optional prompt alist))
6679
f83d2997 6680(defun cperl-imenu-on-info ()
4ab89e7b
SM
6681 "Shows imenu for Perl Info Buffer.
6682Opens Perl Info buffer if needed."
f83d2997 6683 (interactive)
3694d13f 6684 (require 'imenu)
f83d2997
KH
6685 (let* ((buffer (current-buffer))
6686 imenu-create-index-function
5c8b7eaf
SS
6687 imenu-prev-index-position-function
6688 imenu-extract-index-name-function
f83d2997
KH
6689 (index-item (save-restriction
6690 (save-window-excursion
6691 (set-buffer (cperl-info-buffer nil))
5c8b7eaf 6692 (setq imenu-create-index-function
f83d2997
KH
6693 'imenu-default-create-index-function
6694 imenu-prev-index-position-function
6695 'cperl-imenu-info-imenu-search
6696 imenu-extract-index-name-function
6697 'cperl-imenu-info-imenu-name)
6698 (imenu-choose-buffer-index)))))
6699 (and index-item
6700 (progn
6701 (push-mark)
6702 (pop-to-buffer "*info-perl*")
6703 (cond
6704 ((markerp (cdr index-item))
6705 (goto-char (marker-position (cdr index-item))))
6706 (t
6707 (goto-char (cdr index-item))))
6708 (set-window-start (selected-window) (point))
6709 (pop-to-buffer buffer)))))
6710
6711(defun cperl-lineup (beg end &optional step minshift)
6712 "Lineup construction in a region.
6713Beginning of region should be at the start of a construction.
6714All first occurrences of this construction in the lines that are
6715partially contained in the region are lined up at the same column.
6716
6717MINSHIFT is the minimal amount of space to insert before the construction.
6718STEP is the tabwidth to position constructions.
029cb4d5 6719If STEP is nil, `cperl-lineup-step' will be used
15ca5699 6720\(or `cperl-indent-level', if `cperl-lineup-step' is nil).
f83d2997
KH
6721Will not move the position at the start to the left."
6722 (interactive "r")
4ab89e7b 6723 (let (search col tcol seen b)
f83d2997
KH
6724 (save-excursion
6725 (goto-char end)
6726 (end-of-line)
6727 (setq end (point-marker))
6728 (goto-char beg)
6729 (skip-chars-forward " \t\f")
6730 (setq beg (point-marker))
6731 (indent-region beg end nil)
6732 (goto-char beg)
6733 (setq col (current-column))
6734 (if (looking-at "[a-zA-Z0-9_]")
6735 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6736 (setq search
5c8b7eaf
SS
6737 (concat "\\<"
6738 (regexp-quote
f83d2997
KH
6739 (buffer-substring (match-beginning 0)
6740 (match-end 0))) "\\>"))
6741 (error "Cannot line up in a middle of the word"))
6742 (if (looking-at "$")
6743 (error "Cannot line up end of line"))
6744 (setq search (regexp-quote (char-to-string (following-char)))))
6745 (setq step (or step cperl-lineup-step cperl-indent-level))
6746 (or minshift (setq minshift 1))
6747 (while (progn
6748 (beginning-of-line 2)
5c8b7eaf 6749 (and (< (point) end)
f83d2997
KH
6750 (re-search-forward search end t)
6751 (goto-char (match-beginning 0))))
6752 (setq tcol (current-column) seen t)
6753 (if (> tcol col) (setq col tcol)))
6754 (or seen
6755 (error "The construction to line up occurred only once"))
6756 (goto-char beg)
6757 (setq col (+ col minshift))
6758 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
5c8b7eaf 6759 (while
f83d2997 6760 (progn
4ab89e7b 6761 (cperl-make-indent col)
5c8b7eaf
SS
6762 (beginning-of-line 2)
6763 (and (< (point) end)
f83d2997
KH
6764 (re-search-forward search end t)
6765 (goto-char (match-beginning 0)))))))) ; No body
6766
4ab89e7b 6767(defun cperl-etags (&optional add all files) ;; NOT USED???
f83d2997
KH
6768 "Run etags with appropriate options for Perl files.
6769If optional argument ALL is `recursive', will process Perl files
6770in subdirectories too."
6771 (interactive)
6772 (let ((cmd "etags")
4ab89e7b
SM
6773 (args '("-l" "none" "-r"
6774 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6775 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6776 "-r"
6777 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6778 "-r"
6779 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
f83d2997
KH
6780 res)
6781 (if add (setq args (cons "-a" args)))
6782 (or files (setq files (list buffer-file-name)))
6783 (cond
6784 ((eq all 'recursive)
6785 ;;(error "Not implemented: recursive")
5c8b7eaf 6786 (setq args (append (list "-e"
f83d2997
KH
6787 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6788 use File::Find;
6789 find(\\&wanted, '.');
5c8b7eaf 6790 exec @ARGV;"
f83d2997
KH
6791 cmd) args)
6792 cmd "perl"))
5c8b7eaf 6793 (all
f83d2997 6794 ;;(error "Not implemented: all")
5c8b7eaf 6795 (setq args (append (list "-e"
f83d2997 6796 "push @ARGV, <*.PL *.pl *.pm>;
5c8b7eaf 6797 exec @ARGV;"
f83d2997
KH
6798 cmd) args)
6799 cmd "perl"))
6800 (t
6801 (setq args (append args files))))
6802 (setq res (apply 'call-process cmd nil nil nil args))
6803 (or (eq res 0)
6804 (message "etags returned \"%s\"" res))))
6805
6806(defun cperl-toggle-auto-newline ()
6807 "Toggle the state of `cperl-auto-newline'."
6808 (interactive)
6809 (setq cperl-auto-newline (not cperl-auto-newline))
5c8b7eaf 6810 (message "Newlines will %sbe auto-inserted now."
f83d2997
KH
6811 (if cperl-auto-newline "" "not ")))
6812
6813(defun cperl-toggle-abbrev ()
6814 "Toggle the state of automatic keyword expansion in CPerl mode."
6815 (interactive)
6816 (abbrev-mode (if abbrev-mode 0 1))
5c8b7eaf 6817 (message "Perl control structure will %sbe auto-inserted now."
f83d2997
KH
6818 (if abbrev-mode "" "not ")))
6819
6820
6821(defun cperl-toggle-electric ()
6822 "Toggle the state of parentheses doubling in CPerl mode."
6823 (interactive)
6824 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
5c8b7eaf 6825 (message "Parentheses will %sbe auto-doubled now."
f83d2997
KH
6826 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6827
db133cb6 6828(defun cperl-toggle-autohelp ()
f739b53b
SM
6829 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6830Delay of auto-help controlled by `cperl-lazy-help-time'."
db133cb6
RS
6831 (interactive)
6832 (if (fboundp 'run-with-idle-timer)
6833 (progn
6834 (if cperl-lazy-installed
f739b53b 6835 (cperl-lazy-unstall)
db133cb6 6836 (cperl-lazy-install))
5c8b7eaf 6837 (message "Perl help messages will %sbe automatically shown now."
db133cb6
RS
6838 (if cperl-lazy-installed "" "not ")))
6839 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6840
6841(defun cperl-toggle-construct-fix ()
6842 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6843 (interactive)
5c8b7eaf 6844 (setq cperl-indent-region-fix-constructs
5bd52f0e
RS
6845 (if cperl-indent-region-fix-constructs
6846 nil
6847 1))
5c8b7eaf 6848 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
db133cb6
RS
6849 (if cperl-indent-region-fix-constructs "" "not ")))
6850
4ab89e7b
SM
6851(defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6852 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6853Nonpositive numeric argument disables debugging messages. The message
6854summarizes which regions it was decided to rescan for syntactic constructs.
6855
6856The message looks like this:
6857
6858 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6859
6860Numbers are character positions in the buffer. REQ provides the range to
6861rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6862for correct operation it should start and end outside any special syntactic
6863construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6864by CPerl."
6865 (interactive "P")
6866 (or arg
cb5bf6ba 6867 (setq arg (if (eq cperl-syntaxify-by-font-lock
4ab89e7b
SM
6868 (if backtrace 'backtrace 'message)) 0 1)))
6869 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6870 (setq cperl-syntaxify-by-font-lock arg)
6871 (message "Debugging messages of syntax unwind %sabled."
6872 (if (eq arg t) "dis" "en")))
6873
f83d2997
KH
6874;;;; Tags file creation.
6875
6876(defvar cperl-tmp-buffer " *cperl-tmp*")
6877
6878(defun cperl-setup-tmp-buf ()
6879 (set-buffer (get-buffer-create cperl-tmp-buffer))
6880 (set-syntax-table cperl-mode-syntax-table)
6881 (buffer-disable-undo)
6882 (auto-fill-mode 0)
6883 (if cperl-use-syntax-table-text-property-for-tags
6884 (progn
029cb4d5 6885 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997
KH
6886 ;; Do not introduce variable if not needed, we check it!
6887 (set 'parse-sexp-lookup-properties t))))
6888
47e83968
GM
6889;; Copied from imenu-example--name-and-position.
6890(defvar imenu-use-markers)
6891
6892(defun cperl-imenu-name-and-position ()
6893 "Return the current/previous sexp and its (beginning) location.
6894Does not move point."
6895 (save-excursion
6896 (forward-sexp -1)
6897 (let ((beg (if imenu-use-markers (point-marker) (point)))
6898 (end (progn (forward-sexp) (point))))
6899 (cons (buffer-substring beg end)
6900 beg))))
6901
f83d2997 6902(defun cperl-xsub-scan ()
f83d2997 6903 (require 'imenu)
5c8b7eaf 6904 (let ((index-alist '())
f83d2997
KH
6905 (prev-pos 0) index index1 name package prefix)
6906 (goto-char (point-min))
f83d2997
KH
6907 ;; Search for the function
6908 (progn ;;save-match-data
6909 (while (re-search-forward
6910 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6911 nil t)
f83d2997 6912 (cond
83261a2f 6913 ((match-beginning 2) ; SECTION
f83d2997
KH
6914 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6915 (goto-char (match-beginning 0))
6916 (skip-chars-forward " \t")
6917 (forward-char 1)
6918 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6919 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6920 (setq prefix nil)))
6921 ((not package) nil) ; C language section
6922 ((match-beginning 3) ; XSUB
6923 (goto-char (1+ (match-beginning 3)))
47e83968 6924 (setq index (cperl-imenu-name-and-position))
f83d2997
KH
6925 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6926 (if (and prefix (string-match (concat "^" prefix) name))
6927 (setq name (substring name (length prefix))))
6928 (cond ((string-match "::" name) nil)
6929 (t
6930 (setq index1 (cons (concat package "::" name) (cdr index)))
6931 (push index1 index-alist)))
6932 (setcar index name)
6933 (push index index-alist))
6934 (t ; BOOT: section
6935 ;; (beginning-of-line)
47e83968 6936 (setq index (cperl-imenu-name-and-position))
f83d2997
KH
6937 (setcar index (concat package "::BOOT:"))
6938 (push index index-alist)))))
f83d2997
KH
6939 index-alist))
6940
6c389151
SM
6941(defvar cperl-unreadable-ok nil)
6942
6943(defun cperl-find-tags (ifile xs topdir)
83261a2f
SM
6944 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6945 (cperl-pod-here-fontify nil) f file)
f83d2997
KH
6946 (save-excursion
6947 (if b (set-buffer b)
83261a2f 6948 (cperl-setup-tmp-buf))
f83d2997 6949 (erase-buffer)
6c389151
SM
6950 (condition-case err
6951 (setq file (car (insert-file-contents ifile)))
6952 (error (if cperl-unreadable-ok nil
6953 (if (y-or-n-p
6954 (format "File %s unreadable. Continue? " ifile))
6955 (setq cperl-unreadable-ok t)
6956 (error "Aborting: unreadable file %s" ifile)))))
a1506d29 6957 (if (not file)
6c389151 6958 (message "Unreadable file %s" ifile)
83261a2f
SM
6959 (message "Scanning file %s ..." file)
6960 (if (and cperl-use-syntax-table-text-property-for-tags
6961 (not xs))
6962 (condition-case err ; after __END__ may have garbage
6963 (cperl-find-pods-heres nil nil noninteractive)
6964 (error (message "While scanning for syntax: %s" err))))
6965 (if xs
6966 (setq lst (cperl-xsub-scan))
6967 (setq ind (cperl-imenu--create-perl-index))
6968 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6969 (setq lst
6970 (mapcar
6971 (function
6972 (lambda (elt)
6973 (cond ((string-match "^[_a-zA-Z]" (car elt))
6974 (goto-char (cdr elt))
6975 (beginning-of-line) ; pos should be of the start of the line
6976 (list (car elt)
6977 (point)
6978 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6979 (buffer-substring (progn
6980 (goto-char (cdr elt))
6981 ;; After name now...
6982 (or (eolp) (forward-char 1))
6983 (point))
6984 (progn
6985 (beginning-of-line)
6986 (point))))))))
6987 lst))
6988 (erase-buffer)
6989 (while lst
6990 (setq elt (car lst) lst (cdr lst))
6991 (if elt
6992 (progn
6993 (insert (elt elt 3)
6994 127
6995 (if (string-match "^package " (car elt))
6996 (substring (car elt) 8)
6997 (car elt) )
6998 1
6999 (number-to-string (elt elt 2)) ; Line
7000 ","
7001 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
7002 "\n")
7003 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
7004 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
7005 (elt elt 3)))
7006 ;; Need to insert the name without package as well
15ca5699 7007 (setq lst (cons (cons (substring (elt elt 3)
83261a2f
SM
7008 (match-beginning 1)
7009 (match-end 1))
7010 (cdr elt))
7011 lst))))))
7012 (setq pos (point))
7013 (goto-char 1)
7014 (setq rel file)
7015 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7016 (set-text-properties 0 (length rel) nil rel)
7017 (and (equal topdir (substring rel 0 (length topdir)))
7018 (setq rel (substring file (length topdir))))
7019 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
7020 (setq ret (buffer-substring 1 (point-max)))
7021 (erase-buffer)
7022 (or noninteractive
7023 (message "Scanning file %s finished" file))
7024 ret))))
f83d2997
KH
7025
7026(defun cperl-add-tags-recurse-noxs ()
4ab89e7b 7027 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
f83d2997
KH
7028Use as
7029 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
4ab89e7b 7030 -f cperl-add-tags-recurse-noxs
f83d2997
KH
7031"
7032 (cperl-write-tags nil nil t t nil t))
7033
4ab89e7b
SM
7034(defun cperl-add-tags-recurse-noxs-fullpath ()
7035 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7036Writes down fullpath, so TAGS is relocatable (but if the build directory
7037is relocated, the file TAGS inside it breaks). Use as
7038 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7039 -f cperl-add-tags-recurse-noxs-fullpath
7040"
7041 (cperl-write-tags nil nil t t nil t ""))
7042
f83d2997
KH
7043(defun cperl-add-tags-recurse ()
7044 "Add to TAGS file data for Perl files in the current directory and kids.
7045Use as
7046 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
5c8b7eaf 7047 -f cperl-add-tags-recurse
f83d2997
KH
7048"
7049 (cperl-write-tags nil nil t t))
7050
7051(defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7052 ;; If INBUFFER, do not select buffer, and do not save
7053 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7054 (require 'etags)
7055 (if file nil
7056 (setq file (if dir default-directory (buffer-file-name)))
7057 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7058 (or topdir
7059 (setq topdir default-directory))
7060 (let ((tags-file-name "TAGS")
72bc50c0 7061 (case-fold-search (and (featurep 'xemacs) (eq system-type 'emx)))
6c389151 7062 xs rel tm)
f83d2997
KH
7063 (save-excursion
7064 (cond (inbuffer nil) ; Already there
7065 ((file-exists-p tags-file-name)
6546555e 7066 (if (featurep 'xemacs)
5bd52f0e 7067 (visit-tags-table-buffer)
83261a2f 7068 (visit-tags-table-buffer tags-file-name)))
f83d2997
KH
7069 (t (set-buffer (find-file-noselect tags-file-name))))
7070 (cond
7071 (dir
7072 (cond ((eq erase 'ignore))
7073 (erase
7074 (erase-buffer)
7075 (setq erase 'ignore)))
a1506d29 7076 (let ((files
6c389151 7077 (condition-case err
a1506d29 7078 (directory-files file t
6c389151
SM
7079 (if recurse nil cperl-scan-files-regexp)
7080 t)
7081 (error
7082 (if cperl-unreadable-ok nil
7083 (if (y-or-n-p
7084 (format "Directory %s unreadable. Continue? " file))
a1506d29 7085 (setq cperl-unreadable-ok t
83261a2f 7086 tm nil) ; Return empty list
6c389151 7087 (error "Aborting: unreadable directory %s" file)))))))
dba01120
GM
7088 (mapc (function
7089 (lambda (file)
7090 (cond
7091 ((string-match cperl-noscan-files-regexp file)
7092 nil)
7093 ((not (file-directory-p file))
7094 (if (string-match cperl-scan-files-regexp file)
7095 (cperl-write-tags file erase recurse nil t noxs topdir)))
7096 ((not recurse) nil)
7097 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7098 files)))
f83d2997
KH
7099 (t
7100 (setq xs (string-match "\\.xs$" file))
7101 (if (not (and xs noxs))
7102 (progn
7103 (cond ((eq erase 'ignore) (goto-char (point-max)))
83261a2f
SM
7104 (erase (erase-buffer))
7105 (t
7106 (goto-char 1)
7107 (setq rel file)
7108 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7109 (set-text-properties 0 (length rel) nil rel)
7110 (and (equal topdir (substring rel 0 (length topdir)))
7111 (setq rel (substring file (length topdir))))
7112 (if (search-forward (concat "\f\n" rel ",") nil t)
7113 (progn
7114 (search-backward "\f\n")
7115 (delete-region (point)
7116 (save-excursion
7117 (forward-char 1)
7118 (if (search-forward "\f\n"
7119 nil 'toend)
7120 (- (point) 2)
7121 (point-max)))))
7122 (goto-char (point-max)))))
f83d2997 7123 (insert (cperl-find-tags file xs topdir))))))
83261a2f
SM
7124 (if inbuffer nil ; Delegate to the caller
7125 (save-buffer 0) ; No backup
f83d2997
KH
7126 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7127 (initialize-new-tags-table))))))
7128
7129(defvar cperl-tags-hier-regexp-list
5c8b7eaf 7130 (concat
f83d2997
KH
7131 "^\\("
7132 "\\(package\\)\\>"
7133 "\\|"
7134 "sub\\>[^\n]+::"
7135 "\\|"
7136 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7137 "\\|"
7138 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7139 "\\)"))
7140
7141(defvar cperl-hierarchy '(() ())
f94a632a 7142 "Global hierarchy of classes.")
f83d2997 7143
3694d13f
GM
7144;; Follows call to (autoloaded) visit-tags-table.
7145(declare-function file-of-tag "etags" (&optional relative))
7146(declare-function etags-snarf-tag "etags" (&optional use-explicit))
7147
f83d2997
KH
7148(defun cperl-tags-hier-fill ()
7149 ;; Suppose we are in a tag table cooked by cperl.
7150 (goto-char 1)
7151 (let (type pack name pos line chunk ord cons1 file str info fileind)
7152 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
5c8b7eaf 7153 (setq pos (match-beginning 0)
f83d2997
KH
7154 pack (match-beginning 2))
7155 (beginning-of-line)
7156 (if (looking-at (concat
7157 "\\([^\n]+\\)"
7158 "\C-?"
7159 "\\([^\n]+\\)"
7160 "\C-a"
7161 "\\([0-9]+\\)"
7162 ","
7163 "\\([0-9]+\\)"))
7164 (progn
7165 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7166 name (buffer-substring (match-beginning 2) (match-end 2))
7167 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
5bd52f0e 7168 line (buffer-substring (match-beginning 3) (match-end 3))
f83d2997 7169 ord (if pack 1 0)
f83d2997 7170 file (file-of-tag)
5bd52f0e
RS
7171 fileind (format "%s:%s" file line)
7172 ;; Moves to beginning of the next line:
7173 info (cperl-etags-snarf-tag file line))
f83d2997
KH
7174 ;; Move back
7175 (forward-char -1)
7176 ;; Make new member of hierarchy name ==> file ==> pos if needed
7177 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7178 ;; Name known
7179 (setcdr cons1 (cons (cons fileind (vector file info))
7180 (cdr cons1)))
7181 ;; First occurrence of the name, start alist
7182 (setq cons1 (cons name (list (cons fileind (vector file info)))))
5c8b7eaf 7183 (if pack
f83d2997
KH
7184 (setcar (cdr cperl-hierarchy)
7185 (cons cons1 (nth 1 cperl-hierarchy)))
7186 (setcar cperl-hierarchy
7187 (cons cons1 (car cperl-hierarchy)))))))
7188 (end-of-line))))
7189
e8a11b22 7190(declare-function x-popup-menu "menu.c" (position menu))
3694d13f 7191(declare-function etags-goto-tag-location "etags" (tag-info))
f2d9c15f 7192
f83d2997
KH
7193(defun cperl-tags-hier-init (&optional update)
7194 "Show hierarchical menu of classes and methods.
7195Finds info about classes by a scan of loaded TAGS files.
7196Supposes that the TAGS files contain fully qualified function names.
7197One may build such TAGS files from CPerl mode menu."
7198 (interactive)
7199 (require 'etags)
7200 (require 'imenu)
7201 (if (or update (null (nth 2 cperl-hierarchy)))
83261a2f
SM
7202 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7203 (or (nthcdr 2 elt)
7204 ;; Only in one file
7205 (setcdr elt (cdr (nth 1 elt)))))))
7206 pack name cons1 to l1 l2 l3 l4 b)
f83d2997
KH
7207 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7208 (setq cperl-hierarchy (list l1 l2 l3))
6546555e 7209 (if (featurep 'xemacs) ; Not checked
5bd52f0e
RS
7210 (progn
7211 (or tags-file-name
7212 ;; Does this work in XEmacs?
8c777c8d
CY
7213 (call-interactively 'visit-tags-table))
7214 (message "Updating list of classes...")
5bd52f0e
RS
7215 (set-buffer (get-file-buffer tags-file-name))
7216 (cperl-tags-hier-fill))
7217 (or tags-table-list
7218 (call-interactively 'visit-tags-table))
dba01120 7219 (mapc
4ab89e7b
SM
7220 (function
7221 (lambda (tagsfile)
5bd52f0e 7222 (message "Updating list of classes... %s" tagsfile)
8c777c8d
CY
7223 (set-buffer (get-file-buffer tagsfile))
7224 (cperl-tags-hier-fill)))
dba01120 7225 tags-table-list)
5bd52f0e 7226 (message "Updating list of classes... postprocessing..."))
dba01120
GM
7227 (mapc remover (car cperl-hierarchy))
7228 (mapc remover (nth 1 cperl-hierarchy))
f83d2997
KH
7229 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7230 (cons "Methods: " (car cperl-hierarchy))))
7231 (cperl-tags-treeify to 1)
7232 (setcar (nthcdr 2 cperl-hierarchy)
7233 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7234 (message "Updating list of classes: done, requesting display...")
7235 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7236 ))
7237 (or (nth 2 cperl-hierarchy)
7238 (error "No items found"))
7239 (setq update
7240;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
83261a2f
SM
7241 (if (if (fboundp 'display-popup-menus-p)
7242 (let ((f 'display-popup-menus-p))
7243 (funcall f))
7244 window-system)
f83d2997
KH
7245 (x-popup-menu t (nth 2 cperl-hierarchy))
7246 (require 'tmm)
7247 (tmm-prompt (nth 2 cperl-hierarchy))))
7248 (if (and update (listp update))
7249 (progn (while (cdr update) (setq update (cdr update)))
7250 (setq update (car update)))) ; Get the last from the list
5c8b7eaf 7251 (if (vectorp update)
f83d2997
KH
7252 (progn
7253 (find-file (elt update 0))
5bd52f0e 7254 (cperl-etags-goto-tag-location (elt update 1))))
f83d2997
KH
7255 (if (eq update -999) (cperl-tags-hier-init t)))
7256
7257(defun cperl-tags-treeify (to level)
7258 ;; cadr of `to' is read-write. On start it is a cons
5c8b7eaf 7259 (let* ((regexp (concat "^\\(" (mapconcat
f83d2997
KH
7260 'identity
7261 (make-list level "[_a-zA-Z0-9]+")
7262 "::")
7263 "\\)\\(::\\)?"))
7264 (packages (cdr (nth 1 to)))
7265 (methods (cdr (nth 2 to)))
7266 l1 head tail cons1 cons2 ord writeto packs recurse
7267 root-packages root-functions ms many_ms same_name ps
7268 (move-deeper
5c8b7eaf 7269 (function
f83d2997
KH
7270 (lambda (elt)
7271 (cond ((and (string-match regexp (car elt))
7272 (or (eq ord 1) (match-end 2)))
7273 (setq head (substring (car elt) 0 (match-end 1))
5c8b7eaf 7274 tail (if (match-end 2) (substring (car elt)
f83d2997
KH
7275 (match-end 2)))
7276 recurse t)
7277 (if (setq cons1 (assoc head writeto)) nil
7278 ;; Need to init new head
7279 (setcdr writeto (cons (list head (list "Packages: ")
7280 (list "Methods: "))
7281 (cdr writeto)))
7282 (setq cons1 (nth 1 writeto)))
7283 (setq cons2 (nth ord cons1)) ; Either packs or meths
7284 (setcdr cons2 (cons elt (cdr cons2))))
7285 ((eq ord 2)
7286 (setq root-functions (cons elt root-functions)))
7287 (t
7288 (setq root-packages (cons elt root-packages))))))))
7289 (setcdr to l1) ; Init to dynamic space
7290 (setq writeto to)
7291 (setq ord 1)
dba01120 7292 (mapc move-deeper packages)
f83d2997 7293 (setq ord 2)
dba01120 7294 (mapc move-deeper methods)
f83d2997 7295 (if recurse
dba01120 7296 (mapc (function (lambda (elt)
f83d2997 7297 (cperl-tags-treeify elt (1+ level))))
dba01120 7298 (cdr to)))
f83d2997 7299 ;;Now clean up leaders with one child only
dba01120
GM
7300 (mapc (function (lambda (elt)
7301 (if (not (and (listp (cdr elt))
7302 (eq (length elt) 2))) nil
7303 (setcar elt (car (nth 1 elt)))
7304 (setcdr elt (cdr (nth 1 elt))))))
7305 (cdr to))
f83d2997
KH
7306 ;; Sort the roots of subtrees
7307 (if (default-value 'imenu-sort-function)
7308 (setcdr to
7309 (sort (cdr to) (default-value 'imenu-sort-function))))
7310 ;; Now add back functions removed from display
dba01120
GM
7311 (mapc (function (lambda (elt)
7312 (setcdr to (cons elt (cdr to)))))
7313 (if (default-value 'imenu-sort-function)
7314 (nreverse
7315 (sort root-functions (default-value 'imenu-sort-function)))
7316 root-functions))
f83d2997 7317 ;; Now add back packages removed from display
dba01120
GM
7318 (mapc (function (lambda (elt)
7319 (setcdr to (cons (cons (concat "package " (car elt))
7320 (cdr elt))
7321 (cdr to)))))
7322 (if (default-value 'imenu-sort-function)
7323 (nreverse
7324 (sort root-packages (default-value 'imenu-sort-function)))
7325 root-packages))))
f83d2997
KH
7326
7327;;;(x-popup-menu t
5c8b7eaf 7328;;; '(keymap "Name1"
f83d2997 7329;;; ("Ret1" "aa")
5c8b7eaf
SS
7330;;; ("Head1" "ab"
7331;;; keymap "Name2"
f83d2997
KH
7332;;; ("Tail1" "x") ("Tail2" "y"))))
7333
7334(defun cperl-list-fold (list name limit)
7335 (let (list1 list2 elt1 (num 0))
7336 (if (<= (length list) limit) list
7337 (setq list1 nil list2 nil)
7338 (while list
5c8b7eaf 7339 (setq num (1+ num)
f83d2997
KH
7340 elt1 (car list)
7341 list (cdr list))
7342 (if (<= num imenu-max-items)
7343 (setq list2 (cons elt1 list2))
7344 (setq list1 (cons (cons name
7345 (nreverse list2))
7346 list1)
7347 list2 (list elt1)
7348 num 1)))
7349 (nreverse (cons (cons name
7350 (nreverse list2))
7351 list1)))))
7352
7353(defun cperl-menu-to-keymap (menu &optional name)
7354 (let (list)
5c8b7eaf
SS
7355 (cons 'keymap
7356 (mapcar
7357 (function
f83d2997
KH
7358 (lambda (elt)
7359 (cond ((listp (cdr elt))
7360 (setq list (cperl-list-fold
7361 (cdr elt) (car elt) imenu-max-items))
7362 (cons nil
7363 (cons (car elt)
7364 (cperl-menu-to-keymap list))))
7365 (t
7366 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7367 (cperl-list-fold menu "Root" imenu-max-items)))))
7368
7369\f
7370(defvar cperl-bad-style-regexp
7371 (mapconcat 'identity
83261a2f 7372 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
15ca5699 7373 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
83261a2f 7374 "\\|")
f83d2997
KH
7375 "Finds places such that insertion of a whitespace may help a lot.")
7376
5c8b7eaf 7377(defvar cperl-not-bad-style-regexp
15ca5699 7378 (mapconcat
83261a2f 7379 'identity
f83d2997
KH
7380 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7381 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7382 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
4ab89e7b 7383 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
5bd52f0e 7384 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
f83d2997
KH
7385 "-[0-9]" ; -5
7386 "\\+\\+" ; ++var
7387 "--" ; --var
7388 ".->" ; a->b
7389 "->" ; a SPACE ->b
7390 "\\[-" ; a[-1]
5bd52f0e 7391 "\\\\[&$@*\\\\]" ; \&func
f83d2997 7392 "^=" ; =head
5bd52f0e
RS
7393 "\\$." ; $|
7394 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
f83d2997
KH
7395 "||"
7396 "&&"
7397 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
83261a2f 7398 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
f83d2997
KH
7399 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7400 ;;"[*/+-|&<.]+="
7401 )
7402 "\\|")
7403 "If matches at the start of match found by `my-bad-c-style-regexp',
7404insertion of a whitespace will not help.")
7405
7406(defvar found-bad)
7407
7408(defun cperl-find-bad-style ()
7409 "Find places in the buffer where insertion of a whitespace may help.
7410Prompts user for insertion of spaces.
7411Currently it is tuned to C and Perl syntax."
7412 (interactive)
7413 (let (found-bad (p (point)))
7414 (setq last-nonmenu-event 13) ; To disable popup
4ab89e7b 7415 (goto-char (point-min))
f83d2997 7416 (map-y-or-n-p "Insert space here? "
83261a2f 7417 (lambda (arg) (insert " "))
f83d2997 7418 'cperl-next-bad-style
5c8b7eaf 7419 '("location" "locations" "insert a space into")
f83d2997
KH
7420 '((?\C-r (lambda (arg)
7421 (let ((buffer-quit-function
7422 'exit-recursive-edit))
7423 (message "Exit with Esc Esc")
7424 (recursive-edit)
7425 t)) ; Consider acted upon
5c8b7eaf 7426 "edit, exit with Esc Esc")
f83d2997
KH
7427 (?e (lambda (arg)
7428 (let ((buffer-quit-function
7429 'exit-recursive-edit))
7430 (message "Exit with Esc Esc")
7431 (recursive-edit)
7432 t)) ; Consider acted upon
7433 "edit, exit with Esc Esc"))
7434 t)
7435 (if found-bad (goto-char found-bad)
7436 (goto-char p)
7437 (message "No appropriate place found"))))
7438
7439(defun cperl-next-bad-style ()
7440 (let (p (not-found t) (point (point)) found)
7441 (while (and not-found
7442 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7443 (setq p (point))
7444 (goto-char (match-beginning 0))
7445 (if (or
7446 (looking-at cperl-not-bad-style-regexp)
7447 ;; Check for a < -b and friends
7448 (and (eq (following-char) ?\-)
7449 (save-excursion
7450 (skip-chars-backward " \t\n")
07cb2aa3 7451 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
f83d2997
KH
7452 ;; Now check for syntax type
7453 (save-match-data
7454 (setq found (point))
7455 (beginning-of-defun)
7456 (let ((pps (parse-partial-sexp (point) found)))
7457 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7458 (goto-char (match-end 0))
7459 (goto-char (1- p))
7460 (setq not-found nil
7461 found-bad found)))
7462 (not not-found)))
7463
f1d851ae 7464\f
f83d2997 7465;;; Getting help
5c8b7eaf 7466(defvar cperl-have-help-regexp
f83d2997
KH
7467 ;;(concat "\\("
7468 (mapconcat
7469 'identity
83261a2f 7470 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
f83d2997
KH
7471 "[$@]\\^[a-zA-Z]" ; Special variable
7472 "[$@][^ \n\t]" ; Special variable
7473 "-[a-zA-Z]" ; File test
7474 "\\\\[a-zA-Z0]" ; Special chars
83261a2f 7475 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
f83d2997
KH
7476 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7477 "[a-zA-Z_0-9:]+" ; symbol or number
7478 "x="
83261a2f 7479 "#!")
f83d2997 7480 ;;"\\)\\|\\("
83261a2f
SM
7481 "\\|")
7482 ;;"\\)"
7483 ;;)
f83d2997
KH
7484 "Matches places in the buffer we can find help for.")
7485
7486(defvar cperl-message-on-help-error t)
7487(defvar cperl-help-from-timer nil)
7488
7489(defun cperl-word-at-point-hard ()
7490 ;; Does not save-excursion
7491 ;; Get to the something meaningful
7492 (or (eobp) (eolp) (forward-char 1))
5c8b7eaf 7493 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
e180ab9f 7494 (point-at-bol)
f83d2997
KH
7495 'to-beg)
7496 ;; (cond
7497 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7498 ;; (skip-chars-backward " \n\t\r({[]});,")
7499 ;; (or (bobp) (backward-char 1))))
7500 ;; Try to backtrace
7501 (cond
7502 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7503 (skip-chars-backward "a-zA-Z0-9_:")
5c8b7eaf 7504 (cond
f83d2997
KH
7505 ((and (eq (preceding-char) ?^) ; $^I
7506 (eq (char-after (- (point) 2)) ?\$))
7507 (forward-char -2))
7508 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7509 (forward-char -1))
7510 ((and (eq (preceding-char) ?\=)
7511 (eq (current-column) 1))
7512 (forward-char -1))) ; =head1
7513 (if (and (eq (preceding-char) ?\<)
7514 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7515 (forward-char -1)))
7516 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7517 (forward-char -1))
7518 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7519 (forward-char -1))
7520 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7521 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7522 (cond
7523 ((and (eq (preceding-char) ?\$)
7524 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7525 (forward-char -1))
7526 ((and (eq (following-char) ?\>)
7527 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7528 (save-excursion
7529 (forward-sexp -1)
7530 (and (eq (preceding-char) ?\<)
7531 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7532 (search-backward "<"))))
7533 ((and (eq (following-char) ?\$)
7534 (eq (preceding-char) ?\<)
7535 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7536 (forward-char -1)))
7537 (if (looking-at cperl-have-help-regexp)
7538 (buffer-substring (match-beginning 0) (match-end 0))))
7539
7540(defun cperl-get-help ()
7541 "Get one-line docs on the symbol at the point.
7542The data for these docs is a little bit obsolete and may be in fact longer
7543than a line. Your contribution to update/shorten it is appreciated."
7544 (interactive)
7545 (save-match-data ; May be called "inside" query-replace
7546 (save-excursion
7547 (let ((word (cperl-word-at-point-hard)))
7548 (if word
7549 (if (and cperl-help-from-timer ; Bail out if not in mainland
7550 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7551 (or (memq (get-text-property (point) 'face)
7552 '(font-lock-comment-face font-lock-string-face))
7553 (memq (get-text-property (point) 'syntax-type)
7554 '(pod here-doc format))))
7555 nil
7556 (cperl-describe-perl-symbol word))
7557 (if cperl-message-on-help-error
5c8b7eaf 7558 (message "Nothing found for %s..."
f83d2997
KH
7559 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7560
7561;;; Stolen from perl-descr.el by Johan Vromans:
7562
7563(defvar cperl-doc-buffer " *perl-doc*"
7564 "Where the documentation can be found.")
7565
7566(defun cperl-describe-perl-symbol (val)
7567 "Display the documentation of symbol at point, a Perl operator."
7568 (let ((enable-recursive-minibuffers t)
7569 args-file regexp)
7570 (cond
83261a2f
SM
7571 ((string-match "^[&*][a-zA-Z_]" val)
7572 (setq val (concat (substring val 0 1) "NAME")))
7573 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7574 (setq val (concat "@" (substring val 1 (match-end 1)))))
7575 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7576 (setq val (concat "%" (substring val 1 (match-end 1)))))
7577 ((and (string= val "x") (string-match "^x=" val))
7578 (setq val "x="))
7579 ((string-match "^\\$[\C-a-\C-z]" val)
7580 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7581 ((string-match "^CORE::" val)
7582 (setq val "CORE::"))
7583 ((string-match "^SUPER::" val)
7584 (setq val "SUPER::"))
7585 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7586 (setq val "<NAME>")))
5c8b7eaf 7587 (setq regexp (concat "^"
f83d2997 7588 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
5c8b7eaf 7589 (regexp-quote val)
f83d2997
KH
7590 "\\([ \t([/]\\|$\\)"))
7591
7592 ;; get the buffer with the documentation text
7593 (cperl-switch-to-doc-buffer)
7594
7595 ;; lookup in the doc
7596 (goto-char (point-min))
7597 (let ((case-fold-search nil))
5c8b7eaf 7598 (list
f83d2997
KH
7599 (if (re-search-forward regexp (point-max) t)
7600 (save-excursion
7601 (beginning-of-line 1)
7602 (let ((lnstart (point)))
7603 (end-of-line)
7604 (message "%s" (buffer-substring lnstart (point)))))
7605 (if cperl-message-on-help-error
7606 (message "No definition for %s" val)))))))
7607
83261a2f 7608(defvar cperl-short-docs 'please-ignore-this-line
f83d2997
KH
7609 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7610 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
f739b53b 7611... Range (list context); flip/flop [no flop when flip] (scalar context).
5c8b7eaf 7612! ... Logical negation.
f83d2997
KH
7613... != ... Numeric inequality.
7614... !~ ... Search pattern, substitution, or translation (negated).
7615$! In numeric context: errno. In a string context: error string.
7616$\" The separator which joins elements of arrays interpolated in strings.
f739b53b 7617$# The output format for printed numbers. Default is %.15g or close.
f83d2997
KH
7618$$ Process number of this script. Changes in the fork()ed child process.
7619$% The current page number of the currently selected output channel.
7620
7621 The following variables are always local to the current block:
7622
7623$1 Match of the 1st set of parentheses in the last match (auto-local).
7624$2 Match of the 2nd set of parentheses in the last match (auto-local).
7625$3 Match of the 3rd set of parentheses in the last match (auto-local).
7626$4 Match of the 4th set of parentheses in the last match (auto-local).
7627$5 Match of the 5th set of parentheses in the last match (auto-local).
7628$6 Match of the 6th set of parentheses in the last match (auto-local).
7629$7 Match of the 7th set of parentheses in the last match (auto-local).
7630$8 Match of the 8th set of parentheses in the last match (auto-local).
7631$9 Match of the 9th set of parentheses in the last match (auto-local).
7632$& The string matched by the last pattern match (auto-local).
7633$' The string after what was matched by the last match (auto-local).
7634$` The string before what was matched by the last match (auto-local).
7635
7636$( The real gid of this process.
7637$) The effective gid of this process.
7638$* Deprecated: Set to 1 to do multiline matching within a string.
7639$+ The last bracket matched by the last search pattern.
7640$, The output field separator for the print operator.
7641$- The number of lines left on the page.
7642$. The current input line number of the last filehandle that was read.
7643$/ The input record separator, newline by default.
f739b53b 7644$0 Name of the file containing the current perl script (read/write).
f83d2997
KH
7645$: String may be broken after these characters to fill ^-lines in a format.
7646$; Subscript separator for multi-dim array emulation. Default \"\\034\".
7647$< The real uid of this process.
7648$= The page length of the current output channel. Default is 60 lines.
7649$> The effective uid of this process.
7650$? The status returned by the last ``, pipe close or `system'.
7651$@ The perl error message from the last eval or do @var{EXPR} command.
7652$ARGV The name of the current file used with <> .
7653$[ Deprecated: The index of the first element/char in an array/string.
7654$\\ The output record separator for the print operator.
7655$] The perl version string as displayed with perl -v.
7656$^ The name of the current top-of-page format.
7657$^A The current value of the write() accumulator for format() lines.
7658$^D The value of the perl debug (-D) flags.
7659$^E Information about the last system error other than that provided by $!.
7660$^F The highest system file descriptor, ordinarily 2.
7661$^H The current set of syntax checks enabled by `use strict'.
7662$^I The value of the in-place edit extension (perl -i option).
d7584f0f 7663$^L What formats output to perform a formfeed. Default is \\f.
5bd52f0e 7664$^M A buffer for emergency memory allocation when running out of memory.
f83d2997
KH
7665$^O The operating system name under which this copy of Perl was built.
7666$^P Internal debugging flag.
7667$^T The time the script was started. Used by -A/-M/-C file tests.
7668$^W True if warnings are requested (perl -w flag).
7669$^X The name under which perl was invoked (argv[0] in C-speech).
7670$_ The default input and pattern-searching space.
5c8b7eaf 7671$| Auto-flush after write/print on current output channel? Default 0.
f83d2997
KH
7672$~ The name of the current report format.
7673... % ... Modulo division.
7674... %= ... Modulo division assignment.
7675%ENV Contains the current environment.
7676%INC List of files that have been require-d or do-ne.
7677%SIG Used to set signal handlers for various signals.
7678... & ... Bitwise and.
7679... && ... Logical and.
7680... &&= ... Logical and assignment.
7681... &= ... Bitwise and assignment.
7682... * ... Multiplication.
7683... ** ... Exponentiation.
e4920bc9 7684*NAME Glob: all objects referred by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
f83d2997
KH
7685&NAME(arg0, ...) Subroutine call. Arguments go to @_.
7686... + ... Addition. +EXPR Makes EXPR into scalar context.
7687++ Auto-increment (magical on strings). ++EXPR EXPR++
7688... += ... Addition assignment.
7689, Comma operator.
7690... - ... Subtraction.
7691-- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7692... -= ... Subtraction assignment.
7693-A Access time in days since script started.
7694-B File is a non-text (binary) file.
7695-C Inode change time in days since script started.
7696-M Age in days since script started.
7697-O File is owned by real uid.
7698-R File is readable by real uid.
7699-S File is a socket .
7700-T File is a text file.
7701-W File is writable by real uid.
7702-X File is executable by real uid.
7703-b File is a block special file.
7704-c File is a character special file.
7705-d File is a directory.
7706-e File exists .
7707-f File is a plain file.
7708-g File has setgid bit set.
7709-k File has sticky bit set.
7710-l File is a symbolic link.
7711-o File is owned by effective uid.
7712-p File is a named pipe (FIFO).
7713-r File is readable by effective uid.
7714-s File has non-zero size.
7715-t Tests if filehandle (STDIN by default) is opened to a tty.
7716-u File has setuid bit set.
7717-w File is writable by effective uid.
7718-x File is executable by effective uid.
7719-z File has zero size.
7720. Concatenate strings.
f739b53b 7721.. Range (list context); flip/flop (scalar context) operator.
f83d2997
KH
7722.= Concatenate assignment strings
7723... / ... Division. /PATTERN/ioxsmg Pattern match
7724... /= ... Division assignment.
7725/PATTERN/ioxsmg Pattern match.
f739b53b 7726... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
f83d2997
KH
7727<NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7728<pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7729<> Reads line from union of files in @ARGV (= command line) and STDIN.
7730... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7731... <= ... Numeric less than or equal to.
7732... <=> ... Numeric compare.
7733... = ... Assignment.
7734... == ... Numeric equality.
7735... =~ ... Search pattern, substitution, or translation
7736... > ... Numeric greater than.
7737... >= ... Numeric greater than or equal to.
7738... >> ... Bitwise shift right.
7739... >>= ... Bitwise shift right assignment.
7740... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7741?PATTERN? One-time pattern match.
7742@ARGV Command line arguments (not including the command name - see $0).
7743@INC List of places to look for perl scripts during do/include/use.
f739b53b 7744@_ Parameter array for subroutines; result of split() unless in list context.
d7584f0f 7745\\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
f83d2997
KH
7746\\0 Octal char, e.g. \\033.
7747\\E Case modification terminator. See \\Q, \\L, and \\U.
d7584f0f
AS
7748\\L Lowercase until \\E . See also \\l, lc.
7749\\U Upcase until \\E . See also \\u, uc.
f83d2997
KH
7750\\Q Quote metacharacters until \\E . See also quotemeta.
7751\\a Alarm character (octal 007).
7752\\b Backspace character (octal 010).
7753\\c Control character, e.g. \\c[ .
7754\\e Escape character (octal 033).
7755\\f Formfeed character (octal 014).
7756\\l Lowercase the next character. See also \\L and \\u, lcfirst.
7757\\n Newline character (octal 012 on most systems).
7758\\r Return character (octal 015 on most systems).
7759\\t Tab character (octal 011).
7760\\u Upcase the next character. See also \\U and \\l, ucfirst.
7761\\x Hex character, e.g. \\x1b.
7762... ^ ... Bitwise exclusive or.
7763__END__ Ends program source.
7764__DATA__ Ends program source.
7765__FILE__ Current (source) filename.
7766__LINE__ Current line in current source.
7767__PACKAGE__ Current package.
7768ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7769ARGVOUT Output filehandle with -i flag.
7770BEGIN { ... } Immediately executed (during compilation) piece of code.
7771END { ... } Pseudo-subroutine executed after the script finishes.
6c389151
SM
7772CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7773INIT { ... } Pseudo-subroutine executed before the script starts running.
f83d2997
KH
7774DATA Input filehandle for what follows after __END__ or __DATA__.
7775accept(NEWSOCKET,GENERICSOCKET)
7776alarm(SECONDS)
7777atan2(X,Y)
7778bind(SOCKET,NAME)
7779binmode(FILEHANDLE)
7780caller[(LEVEL)]
7781chdir(EXPR)
7782chmod(LIST)
7783chop[(LIST|VAR)]
7784chown(LIST)
7785chroot(FILENAME)
7786close(FILEHANDLE)
7787closedir(DIRHANDLE)
7788... cmp ... String compare.
7789connect(SOCKET,NAME)
7790continue of { block } continue { block }. Is executed after `next' or at end.
7791cos(EXPR)
7792crypt(PLAINTEXT,SALT)
7793dbmclose(%HASH)
7794dbmopen(%HASH,DBNAME,MODE)
7795defined(EXPR)
7796delete($HASH{KEY})
7797die(LIST)
7798do { ... }|SUBR while|until EXPR executes at least once
7799do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7800dump LABEL
7801each(%HASH)
7802endgrent
7803endhostent
7804endnetent
7805endprotoent
7806endpwent
7807endservent
7808eof[([FILEHANDLE])]
7809... eq ... String equality.
7810eval(EXPR) or eval { BLOCK }
4ab89e7b 7811exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
f83d2997
KH
7812exit(EXPR)
7813exp(EXPR)
7814fcntl(FILEHANDLE,FUNCTION,SCALAR)
7815fileno(FILEHANDLE)
7816flock(FILEHANDLE,OPERATION)
7817for (EXPR;EXPR;EXPR) { ... }
7818foreach [VAR] (@ARRAY) { ... }
7819fork
7820... ge ... String greater than or equal.
7821getc[(FILEHANDLE)]
7822getgrent
7823getgrgid(GID)
7824getgrnam(NAME)
7825gethostbyaddr(ADDR,ADDRTYPE)
7826gethostbyname(NAME)
7827gethostent
7828getlogin
7829getnetbyaddr(ADDR,ADDRTYPE)
7830getnetbyname(NAME)
7831getnetent
7832getpeername(SOCKET)
7833getpgrp(PID)
7834getppid
7835getpriority(WHICH,WHO)
7836getprotobyname(NAME)
7837getprotobynumber(NUMBER)
7838getprotoent
7839getpwent
7840getpwnam(NAME)
7841getpwuid(UID)
7842getservbyname(NAME,PROTO)
7843getservbyport(PORT,PROTO)
7844getservent
7845getsockname(SOCKET)
7846getsockopt(SOCKET,LEVEL,OPTNAME)
7847gmtime(EXPR)
7848goto LABEL
f83d2997
KH
7849... gt ... String greater than.
7850hex(EXPR)
7851if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7852index(STR,SUBSTR[,OFFSET])
7853int(EXPR)
7854ioctl(FILEHANDLE,FUNCTION,SCALAR)
7855join(EXPR,LIST)
7856keys(%HASH)
7857kill(LIST)
7858last [LABEL]
7859... le ... String less than or equal.
7860length(EXPR)
7861link(OLDFILE,NEWFILE)
7862listen(SOCKET,QUEUESIZE)
7863local(LIST)
7864localtime(EXPR)
7865log(EXPR)
7866lstat(EXPR|FILEHANDLE|VAR)
7867... lt ... String less than.
7868m/PATTERN/iogsmx
7869mkdir(FILENAME,MODE)
7870msgctl(ID,CMD,ARG)
7871msgget(KEY,FLAGS)
7872msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7873msgsnd(ID,MSG,FLAGS)
7874my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
6c389151 7875our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
f83d2997
KH
7876... ne ... String inequality.
7877next [LABEL]
7878oct(EXPR)
7879open(FILEHANDLE[,EXPR])
7880opendir(DIRHANDLE,EXPR)
7881ord(EXPR) ASCII value of the first char of the string.
7882pack(TEMPLATE,LIST)
7883package NAME Introduces package context.
7884pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7885pop(ARRAY)
7886print [FILEHANDLE] [(LIST)]
7887printf [FILEHANDLE] (FORMAT,LIST)
7888push(ARRAY,LIST)
7889q/STRING/ Synonym for 'STRING'
7890qq/STRING/ Synonym for \"STRING\"
7891qx/STRING/ Synonym for `STRING`
7892rand[(EXPR)]
7893read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7894readdir(DIRHANDLE)
7895readlink(EXPR)
7896recv(SOCKET,SCALAR,LEN,FLAGS)
7897redo [LABEL]
7898rename(OLDNAME,NEWNAME)
7899require [FILENAME | PERL_VERSION]
7900reset[(EXPR)]
7901return(LIST)
7902reverse(LIST)
7903rewinddir(DIRHANDLE)
7904rindex(STR,SUBSTR[,OFFSET])
7905rmdir(FILENAME)
7906s/PATTERN/REPLACEMENT/gieoxsm
7907scalar(EXPR)
7908seek(FILEHANDLE,POSITION,WHENCE)
7909seekdir(DIRHANDLE,POS)
7910select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7911semctl(ID,SEMNUM,CMD,ARG)
7912semget(KEY,NSEMS,SIZE,FLAGS)
7913semop(KEY,...)
7914send(SOCKET,MSG,FLAGS[,TO])
7915setgrent
7916sethostent(STAYOPEN)
7917setnetent(STAYOPEN)
7918setpgrp(PID,PGRP)
7919setpriority(WHICH,WHO,PRIORITY)
7920setprotoent(STAYOPEN)
7921setpwent
7922setservent(STAYOPEN)
7923setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7924shift[(ARRAY)]
7925shmctl(ID,CMD,ARG)
7926shmget(KEY,SIZE,FLAGS)
7927shmread(ID,VAR,POS,SIZE)
7928shmwrite(ID,STRING,POS,SIZE)
7929shutdown(SOCKET,HOW)
7930sin(EXPR)
7931sleep[(EXPR)]
7932socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7933socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7934sort [SUBROUTINE] (LIST)
7935splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7936split[(/PATTERN/[,EXPR[,LIMIT]])]
7937sprintf(FORMAT,LIST)
7938sqrt(EXPR)
7939srand(EXPR)
7940stat(EXPR|FILEHANDLE|VAR)
7941study[(SCALAR)]
7942sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7943substr(EXPR,OFFSET[,LEN])
7944symlink(OLDFILE,NEWFILE)
7945syscall(LIST)
7946sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
4ab89e7b 7947system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
f83d2997
KH
7948syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7949tell[(FILEHANDLE)]
7950telldir(DIRHANDLE)
7951time
7952times
7953tr/SEARCHLIST/REPLACEMENTLIST/cds
7954truncate(FILE|EXPR,LENGTH)
7955umask[(EXPR)]
7956undef[(EXPR)]
7957unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7958unlink(LIST)
7959unpack(TEMPLATE,EXPR)
7960unshift(ARRAY,LIST)
7961until (EXPR) { ... } EXPR until EXPR
7962utime(LIST)
7963values(%HASH)
7964vec(EXPR,OFFSET,BITS)
7965wait
7966waitpid(PID,FLAGS)
7967wantarray Returns true if the sub/eval is called in list context.
7968warn(LIST)
7969while (EXPR) { ... } EXPR while EXPR
7970write[(EXPR|FILEHANDLE)]
7971... x ... Repeat string or array.
7972x= ... Repetition assignment.
7973y/SEARCHLIST/REPLACEMENTLIST/
7974... | ... Bitwise or.
7975... || ... Logical or.
7976~ ... Unary bitwise complement.
db133cb6 7977#! OS interpreter indicator. If contains `perl', used for options, and -x.
f83d2997
KH
7978AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7979CORE:: Prefix to access builtin function if imported sub obscures it.
7980SUPER:: Prefix to lookup for a method in @ISA classes.
7981DESTROY Shorthand for `sub DESTROY {...}'.
7982... EQ ... Obsolete synonym of `eq'.
7983... GE ... Obsolete synonym of `ge'.
7984... GT ... Obsolete synonym of `gt'.
7985... LE ... Obsolete synonym of `le'.
7986... LT ... Obsolete synonym of `lt'.
7987... NE ... Obsolete synonym of `ne'.
7988abs [ EXPR ] absolute value
7989... and ... Low-precedence synonym for &&.
7990bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7991chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
7992chr Converts a number to char with the same ordinal.
7993else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7994elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
83261a2f 7995exists $HASH{KEY} True if the key exists.
f83d2997
KH
7996format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7997formline PICTURE, LIST Backdoor into \"format\" processing.
7998glob EXPR Synonym of <EXPR>.
7999lc [ EXPR ] Returns lowercased EXPR.
8000lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
db133cb6 8001grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
f83d2997
KH
8002map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
8003no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
8004not ... Low-precedence synonym for ! - negation.
8005... or ... Low-precedence synonym for ||.
8006pos STRING Set/Get end-position of the last match over this string, see \\G.
8007quotemeta [ EXPR ] Quote regexp metacharacters.
8008qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
8009readline FH Synonym of <FH>.
8010readpipe CMD Synonym of `CMD`.
8011ref [ EXPR ] Type of EXPR when dereferenced.
8012sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
8013tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
8014tied Returns internal object for a tied data.
8015uc [ EXPR ] Returns upcased EXPR.
8016ucfirst [ EXPR ] Returns EXPR with upcased first letter.
8017untie VAR Unlink an object from a simple Perl variable.
8018use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
8019... xor ... Low-precedence synonym for exclusive or.
d7584f0f 8020prototype \\&SUB Returns prototype of the function given a reference.
f83d2997
KH
8021=head1 Top-level heading.
8022=head2 Second-level heading.
8023=head3 Third-level heading (is there such?).
8024=over [ NUMBER ] Start list.
8025=item [ TITLE ] Start new item in the list.
8026=back End list.
8027=cut Switch from POD to Perl.
8028=pod Switch from Perl to POD.
8029")
8030
21df56d5 8031(defun cperl-switch-to-doc-buffer (&optional interactive)
f83d2997 8032 "Go to the perl documentation buffer and insert the documentation."
21df56d5 8033 (interactive "p")
f83d2997 8034 (let ((buf (get-buffer-create cperl-doc-buffer)))
21df56d5 8035 (if interactive
f83d2997
KH
8036 (switch-to-buffer-other-window buf)
8037 (set-buffer buf))
8038 (if (= (buffer-size) 0)
8039 (progn
8040 (insert (documentation-property 'cperl-short-docs
8041 'variable-documentation))
8042 (setq buffer-read-only t)))))
8043
6c389151 8044(defun cperl-beautify-regexp-piece (b e embed level)
f83d2997
KH
8045 ;; b is before the starting delimiter, e before the ending
8046 ;; e should be a marker, may be changed, but remains "correct".
e7f767c2 8047 ;; EMBED is nil if we process the whole REx.
4ab89e7b 8048 ;; The REx is guaranteed to have //x
6c389151
SM
8049 ;; LEVEL shows how many levels deep to go
8050 ;; position at enter and at leave is not defined
8051 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
8c777c8d
CY
8052 (if embed
8053 (progn
8054 (goto-char b)
8055 (setq c (if (eq embed t) (current-indentation) (current-column)))
8056 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
8057 (forward-char 2)
8058 (delete-char 1)
8059 (forward-char 1))
8060 ((looking-at "(\\?[^a-zA-Z]")
8061 (forward-char 3))
8062 ((looking-at "(\\?") ; (?i)
8063 (forward-char 2))
8064 (t
8065 (forward-char 1))))
8066 (goto-char (1+ b))
8067 (setq c (1- (current-column))))
8068 (setq c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
f83d2997
KH
8069 (or (looking-at "[ \t]*[\n#]")
8070 (progn
8071 (insert "\n")))
8072 (goto-char e)
8073 (beginning-of-line)
8074 (if (re-search-forward "[^ \t]" e t)
83261a2f 8075 (progn ; Something before the ending delimiter
f83d2997 8076 (goto-char e)
6c389151 8077 (delete-horizontal-space)
f83d2997 8078 (insert "\n")
4ab89e7b 8079 (cperl-make-indent c)
f83d2997
KH
8080 (set-marker e (point))))
8081 (goto-char b)
8082 (end-of-line 2)
8083 (while (< (point) (marker-position e))
8084 (beginning-of-line)
8085 (setq s (point)
8086 inline t)
8087 (skip-chars-forward " \t")
8088 (delete-region s (point))
4ab89e7b 8089 (cperl-make-indent c1)
f83d2997
KH
8090 (while (and
8091 inline
5c8b7eaf 8092 (looking-at
f83d2997
KH
8093 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8094 "\\|" ; Embedded variable
8095 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8096 "\\|" ; $ ^
8097 "[$^]"
8098 "\\|" ; simple-code simple-code*?
8099 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8100 "\\|" ; Class
8101 "\\(\\[\\)" ; 6
8102 "\\|" ; Grouping
8103 "\\((\\(\\?\\)?\\)" ; 7 8
8104 "\\|" ; |
83261a2f 8105 "\\(|\\)"))) ; 9
f83d2997
KH
8106 (goto-char (match-end 0))
8107 (setq spaces t)
8108 (cond ((match-beginning 1) ; Alphanum word + junk
8109 (forward-char -1))
8110 ((or (match-beginning 3) ; $ab[12]
8111 (and (match-beginning 5) ; X* X+ X{2,3}
8112 (eq (preceding-char) ?\{)))
8113 (forward-char -1)
8114 (forward-sexp 1))
4ab89e7b
SM
8115 ((and ; [], already syntaxified
8116 (match-beginning 6)
8117 cperl-regexp-scan
8118 cperl-use-syntax-table-text-property)
8119 (forward-char -1)
8120 (forward-sexp 1)
8121 (or (eq (preceding-char) ?\])
8122 (error "[]-group not terminated"))
8123 (re-search-forward
8124 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
f83d2997
KH
8125 ((match-beginning 6) ; []
8126 (setq tmp (point))
8127 (if (looking-at "\\^?\\]")
8128 (goto-char (match-end 0)))
6c389151
SM
8129 ;; XXXX POSIX classes?!
8130 (while (and (not pos)
8131 (re-search-forward "\\[:\\|\\]" e t))
8132 (if (eq (preceding-char) ?:)
8133 (or (re-search-forward ":\\]" e t)
8134 (error "[:POSIX:]-group in []-group not terminated"))
8135 (setq pos t)))
8136 (or (eq (preceding-char) ?\])
8137 (error "[]-group not terminated"))
4ab89e7b
SM
8138 (re-search-forward
8139 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
f83d2997
KH
8140 ((match-beginning 7) ; ()
8141 (goto-char (match-beginning 0))
6c389151
SM
8142 (setq pos (current-column))
8143 (or (eq pos c1)
f83d2997 8144 (progn
6c389151 8145 (delete-horizontal-space)
f83d2997 8146 (insert "\n")
4ab89e7b 8147 (cperl-make-indent c1)))
f83d2997
KH
8148 (setq tmp (point))
8149 (forward-sexp 1)
8150 ;; (or (forward-sexp 1)
8151 ;; (progn
8152 ;; (goto-char tmp)
8153 ;; (error "()-group not terminated")))
8154 (set-marker m (1- (point)))
8155 (set-marker m1 (point))
6c389151
SM
8156 (if (= level 1)
8157 (if (progn ; indent rigidly if multiline
a1506d29 8158 ;; In fact does not make a lot of sense, since
6c389151
SM
8159 ;; the starting position can be already lost due
8160 ;; to insertion of "\n" and " "
8161 (goto-char tmp)
8162 (search-forward "\n" m1 t))
8163 (indent-rigidly (point) m1 (- c1 pos)))
8164 (setq level (1- level))
8165 (cond
8166 ((not (match-beginning 8))
8167 (cperl-beautify-regexp-piece tmp m t level))
8168 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8169 t)
8170 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8171 (goto-char (+ 2 tmp))
8172 (forward-sexp 1)
8173 (cperl-beautify-regexp-piece (point) m t level))
8174 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8175 (goto-char (+ 3 tmp))
8176 (cperl-beautify-regexp-piece (point) m t level))
8177 (t
8178 (cperl-beautify-regexp-piece tmp m t level))))
f83d2997
KH
8179 (goto-char m1)
8180 (cond ((looking-at "[*+?]\\??")
8181 (goto-char (match-end 0)))
8182 ((eq (following-char) ?\{)
8183 (forward-sexp 1)
8184 (if (eq (following-char) ?\?)
8185 (forward-char))))
8186 (skip-chars-forward " \t")
8187 (setq spaces nil)
8188 (if (looking-at "[#\n]")
8189 (progn
8190 (or (eolp) (indent-for-comment))
8191 (beginning-of-line 2))
6c389151 8192 (delete-horizontal-space)
f83d2997
KH
8193 (insert "\n"))
8194 (end-of-line)
8195 (setq inline nil))
8196 ((match-beginning 9) ; |
8197 (forward-char -1)
8198 (setq tmp (point))
8199 (beginning-of-line)
8200 (if (re-search-forward "[^ \t]" tmp t)
8201 (progn
8202 (goto-char tmp)
6c389151 8203 (delete-horizontal-space)
f83d2997
KH
8204 (insert "\n"))
8205 ;; first at line
8206 (delete-region (point) tmp))
4ab89e7b 8207 (cperl-make-indent c)
f83d2997
KH
8208 (forward-char 1)
8209 (skip-chars-forward " \t")
8210 (setq spaces nil)
8211 (if (looking-at "[#\n]")
8212 (beginning-of-line 2)
6c389151 8213 (delete-horizontal-space)
f83d2997
KH
8214 (insert "\n"))
8215 (end-of-line)
8216 (setq inline nil)))
8217 (or (looking-at "[ \t\n]")
8218 (not spaces)
8219 (insert " "))
8220 (skip-chars-forward " \t"))
83261a2f
SM
8221 (or (looking-at "[#\n]")
8222 (error "Unknown code `%s' in a regexp"
8223 (buffer-substring (point) (1+ (point)))))
8224 (and inline (end-of-line 2)))
f83d2997
KH
8225 ;; Special-case the last line of group
8226 (if (and (>= (point) (marker-position e))
8227 (/= (current-indentation) c))
8228 (progn
83261a2f 8229 (beginning-of-line)
4ab89e7b 8230 (cperl-make-indent c)))))
f83d2997
KH
8231
8232(defun cperl-make-regexp-x ()
db133cb6 8233 ;; Returns position of the start
6c389151 8234 ;; XXX this is called too often! Need to cache the result!
f83d2997
KH
8235 (save-excursion
8236 (or cperl-use-syntax-table-text-property
5bd52f0e 8237 (error "I need to have a regexp marked!"))
f83d2997 8238 ;; Find the start
db133cb6
RS
8239 (if (looking-at "\\s|")
8240 nil ; good already
8c777c8d
CY
8241 (if (or (looking-at "\\([smy]\\|qr\\)\\s|")
8242 (and (eq (preceding-char) ?q)
8243 (looking-at "\\(r\\)\\s|")))
8244 (goto-char (match-end 1))
83261a2f 8245 (re-search-backward "\\s|"))) ; Assume it is scanned already.
f83d2997
KH
8246 ;;(forward-char 1)
8247 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8248 (sub-p (eq (preceding-char) ?s)) s)
8249 (forward-sexp 1)
8250 (set-marker e (1- (point)))
8251 (setq delim (preceding-char))
8252 (if (and sub-p (eq delim (char-after (- (point) 2))))
8253 (error "Possible s/blah// - do not know how to deal with"))
8254 (if sub-p (forward-sexp 1))
5c8b7eaf 8255 (if (looking-at "\\sw*x")
f83d2997
KH
8256 (setq have-x t)
8257 (insert "x"))
8258 ;; Protect fragile " ", "#"
8259 (if have-x nil
8260 (goto-char (1+ b))
8261 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8262 (forward-char -1)
8263 (insert "\\")
8264 (forward-char 1)))
8265 b)))
8266
6c389151 8267(defun cperl-beautify-regexp (&optional deep)
f94a632a 8268 "Do it. (Experimental, may change semantics, recheck the result.)
f83d2997 8269We suppose that the regexp is scanned already."
6c389151 8270 (interactive "P")
0c602a0f 8271 (setq deep (if deep (prefix-numeric-value deep) -1))
6c389151
SM
8272 (save-excursion
8273 (goto-char (cperl-make-regexp-x))
8274 (let ((b (point)) (e (make-marker)))
8275 (forward-sexp 1)
8276 (set-marker e (1- (point)))
8277 (cperl-beautify-regexp-piece b e nil deep))))
f83d2997 8278
db133cb6
RS
8279(defun cperl-regext-to-level-start ()
8280 "Goto start of an enclosing group in regexp.
f83d2997
KH
8281We suppose that the regexp is scanned already."
8282 (interactive)
db133cb6 8283 (let ((limit (cperl-make-regexp-x)) done)
f83d2997
KH
8284 (while (not done)
8285 (or (eq (following-char) ?\()
db133cb6 8286 (search-backward "(" (1+ limit) t)
f83d2997
KH
8287 (error "Cannot find `(' which starts a group"))
8288 (setq done
8289 (save-excursion
8290 (skip-chars-backward "\\")
8291 (looking-at "\\(\\\\\\\\\\)*(")))
db133cb6
RS
8292 (or done (forward-char -1)))))
8293
8294(defun cperl-contract-level ()
5bd52f0e 8295 "Find an enclosing group in regexp and contract it.
db133cb6
RS
8296\(Experimental, may change semantics, recheck the result.)
8297We suppose that the regexp is scanned already."
8298 (interactive)
6c389151 8299 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
83261a2f 8300 (cperl-regext-to-level-start)
4ab89e7b 8301 (let ((b (point)) (e (make-marker)) c)
83261a2f
SM
8302 (forward-sexp 1)
8303 (set-marker e (1- (point)))
8304 (goto-char b)
8305 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8306 (cond
8307 ((match-beginning 1) ; #-comment
8308 (or c (setq c (current-indentation)))
8309 (beginning-of-line 2) ; Skip
4ab89e7b 8310 (cperl-make-indent c))
83261a2f
SM
8311 (t
8312 (delete-char -1)
8313 (just-one-space))))))
db133cb6
RS
8314
8315(defun cperl-contract-levels ()
5bd52f0e 8316 "Find an enclosing group in regexp and contract all the kids.
db133cb6
RS
8317\(Experimental, may change semantics, recheck the result.)
8318We suppose that the regexp is scanned already."
8319 (interactive)
6c389151
SM
8320 (save-excursion
8321 (condition-case nil
8322 (cperl-regext-to-level-start)
8323 (error ; We are outside outermost group
5efe6a56
SM
8324 (goto-char (cperl-make-regexp-x))))
8325 (let ((b (point)) (e (make-marker)) s c)
8326 (forward-sexp 1)
8327 (set-marker e (1- (point)))
8328 (goto-char (1+ b))
8329 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
a1506d29 8330 (cond
6c389151
SM
8331 ((match-beginning 1) ; Skip
8332 nil)
8333 (t ; Group
8334 (cperl-contract-level)))))))
f83d2997 8335
6c389151 8336(defun cperl-beautify-level (&optional deep)
f83d2997
KH
8337 "Find an enclosing group in regexp and beautify it.
8338\(Experimental, may change semantics, recheck the result.)
8339We suppose that the regexp is scanned already."
6c389151 8340 (interactive "P")
0c602a0f 8341 (setq deep (if deep (prefix-numeric-value deep) -1))
6c389151
SM
8342 (save-excursion
8343 (cperl-regext-to-level-start)
8344 (let ((b (point)) (e (make-marker)))
8345 (forward-sexp 1)
8346 (set-marker e (1- (point)))
8c777c8d 8347 (cperl-beautify-regexp-piece b e 'level deep))))
db133cb6 8348
4ab89e7b
SM
8349(defun cperl-invert-if-unless-modifiers ()
8350 "Change `B if A;' into `if (A) {B}' etc if possible.
8351\(Unfinished.)"
8c777c8d 8352 (interactive)
4ab89e7b
SM
8353 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8354 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8355 (and (= (char-syntax (preceding-char)) ?w)
8356 (forward-sexp -1))
8357 (setq pre-if (point))
8358 (cperl-backward-to-start-of-expr)
8359 (setq pre-B (point))
8360 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8361 (cperl-forward-to-end-of-expr)
8362 (setq post-A (point))
8363 (goto-char pre-if)
8364 (or (looking-at w-rex)
8365 ;; Find the position
8366 (progn (goto-char post-A)
8367 (while (and
8368 (not (looking-at w-rex))
8369 (> (point) pre-B))
8370 (forward-sexp -1))
8371 (setq pre-if (point))))
8372 (or (looking-at w-rex)
8373 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8374 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8375 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8376 ;; First, simple part: find code boundaries
8377 (forward-sexp 1)
8378 (setq post-if (point))
8379 (forward-sexp -2)
8380 (forward-sexp 1)
8381 (setq post-B (point))
8382 (cperl-backward-to-start-of-expr)
8383 (setq pre-B (point))
8384 (setq B (buffer-substring pre-B post-B))
8385 (goto-char pre-if)
8386 (forward-sexp 2)
8387 (forward-sexp -1)
8388 ;; May be after $, @, $# etc of a variable
8389 (skip-chars-backward "$@%#")
8390 (setq pre-A (point))
8391 (cperl-forward-to-end-of-expr)
8392 (setq post-A (point))
8393 (setq A (buffer-substring pre-A post-A))
8394 ;; Now modify (from end, to not break the stuff)
8395 (skip-chars-forward " \t;")
8396 (delete-region pre-A (point)) ; we move to pre-A
8397 (insert "\n" B ";\n}")
8398 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8399 (delete-region pre-if post-if)
8400 (delete-region pre-B post-B)
8401 (goto-char pre-B)
8402 (insert if-string " (" A ") {")
8403 (setq post-B (point))
8404 (if (looking-at "[ \t]+$")
8405 (delete-horizontal-space)
8406 (if (looking-at "[ \t]*#")
8407 (cperl-indent-for-comment)
8408 (just-one-space)))
8409 (forward-line 1)
8410 (if (looking-at "[ \t]*$")
8411 (progn ; delete line
8412 (delete-horizontal-space)
8413 (delete-region (point) (1+ (point)))))
8414 (cperl-indent-line)
8415 (goto-char (1- post-B))
8416 (forward-sexp 1)
8417 (cperl-indent-line)
8418 (goto-char pre-B)))
8419
db133cb6 8420(defun cperl-invert-if-unless ()
4ab89e7b
SM
8421 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8422If the cursor is not on the leading keyword of the BLOCK flavor of
8423construct, will assume it is the STATEMENT flavor, so will try to find
8424the appropriate statement modifier."
db133cb6 8425 (interactive)
4ab89e7b
SM
8426 (and (= (char-syntax (preceding-char)) ?w)
8427 (forward-sexp -1))
6c389151 8428 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
4ab89e7b
SM
8429 (let ((pre-if (point))
8430 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8431 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
db133cb6 8432 (forward-sexp 2)
4ab89e7b 8433 (setq post-A (point))
db133cb6 8434 (forward-sexp -1)
4ab89e7b
SM
8435 (setq pre-A (point))
8436 (setq is-block (and (eq (following-char) ?\( )
8437 (save-excursion
8438 (condition-case nil
8439 (progn
8440 (forward-sexp 2)
8441 (forward-sexp -1)
8442 (eq (following-char) ?\{ ))
8443 (error nil)))))
8444 (if is-block
db133cb6 8445 (progn
4ab89e7b 8446 (goto-char post-A)
db133cb6 8447 (forward-sexp 1)
4ab89e7b 8448 (setq post-B (point))
db133cb6 8449 (forward-sexp -1)
4ab89e7b 8450 (setq pre-B (point))
db133cb6
RS
8451 (if (and (eq (following-char) ?\{ )
8452 (progn
4ab89e7b 8453 (cperl-backward-to-noncomment post-A)
db133cb6
RS
8454 (eq (preceding-char) ?\) )))
8455 (if (condition-case nil
8456 (progn
4ab89e7b 8457 (goto-char post-B)
db133cb6
RS
8458 (forward-sexp 1)
8459 (forward-sexp -1)
8460 (looking-at "\\<els\\(e\\|if\\)\\>"))
8461 (error nil))
8462 (error
4ab89e7b
SM
8463 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8464 (goto-char (1- post-B))
8465 (cperl-backward-to-noncomment pre-B)
db133cb6
RS
8466 (if (eq (preceding-char) ?\;)
8467 (forward-char -1))
4ab89e7b
SM
8468 (setq end-B-code (point))
8469 (goto-char pre-B)
8470 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
db133cb6 8471 (setq p (match-beginning 0)
4ab89e7b
SM
8472 A (buffer-substring p (match-end 0))
8473 state (parse-partial-sexp pre-B p))
5c8b7eaf 8474 (or (nth 3 state)
db133cb6
RS
8475 (nth 4 state)
8476 (nth 5 state)
4ab89e7b 8477 (error "`%s' inside `%s' BLOCK" A if-string))
db133cb6
RS
8478 (goto-char (match-end 0)))
8479 ;; Finally got it
4ab89e7b 8480 (goto-char (1+ pre-B))
db133cb6 8481 (skip-chars-forward " \t\n")
4ab89e7b
SM
8482 (setq B (buffer-substring (point) end-B-code))
8483 (goto-char end-B-code)
db133cb6
RS
8484 (or (looking-at ";?[ \t\n]*}")
8485 (progn
8486 (skip-chars-forward "; \t\n")
4ab89e7b
SM
8487 (setq B-comment
8488 (buffer-substring (point) (1- post-B)))))
8489 (and (equal B "")
8490 (setq B "1"))
8491 (goto-char (1- post-A))
8492 (cperl-backward-to-noncomment pre-A)
db133cb6 8493 (or (looking-at "[ \t\n]*)")
4ab89e7b 8494 (goto-char (1- post-A)))
db133cb6 8495 (setq p (point))
4ab89e7b 8496 (goto-char (1+ pre-A))
db133cb6 8497 (skip-chars-forward " \t\n")
4ab89e7b
SM
8498 (setq A (buffer-substring (point) p))
8499 (delete-region pre-B post-B)
8500 (delete-region pre-A post-A)
8501 (goto-char pre-if)
8502 (insert B " ")
8503 (and B-comment (insert B-comment " "))
db133cb6
RS
8504 (just-one-space)
8505 (forward-word 1)
4ab89e7b
SM
8506 (setq pre-A (point))
8507 (insert " " A ";")
6c389151 8508 (delete-horizontal-space)
4ab89e7b
SM
8509 (setq post-B (point))
8510 (if (looking-at "#")
8511 (indent-for-comment))
8512 (goto-char post-B)
db133cb6
RS
8513 (forward-char -1)
8514 (delete-horizontal-space)
4ab89e7b 8515 (goto-char pre-A)
db133cb6 8516 (just-one-space)
4ab89e7b
SM
8517 (goto-char pre-if)
8518 (setq pre-A (set-marker (make-marker) pre-A))
8519 (while (<= (point) (marker-position pre-A))
8520 (cperl-indent-line)
8521 (forward-line 1))
8522 (goto-char (marker-position pre-A))
8523 (if B-comment
8524 (progn
8525 (forward-line -1)
8526 (indent-for-comment)
8527 (goto-char (marker-position pre-A)))))
8528 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8529 ;; (error "`%s' not with an (EXPR)" if-string)
8530 (forward-sexp -1)
8531 (cperl-invert-if-unless-modifiers)))
8532 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8533 (cperl-invert-if-unless-modifiers)))
db133cb6 8534
ded62b08
GM
8535(declare-function Man-getpage-in-background "man" (topic))
8536
5bd52f0e 8537;;; By Anthony Foiani <afoiani@uswest.com>
b7ec9e59
RS
8538;;; Getting help on modules in C-h f ?
8539;;; This is a modified version of `man'.
8540;;; Need to teach it how to lookup functions
4ab89e7b 8541;;;###autoload
b7ec9e59
RS
8542(defun cperl-perldoc (word)
8543 "Run `perldoc' on WORD."
8544 (interactive
8545 (list (let* ((default-entry (cperl-word-at-point))
8546 (input (read-string
8547 (format "perldoc entry%s: "
8548 (if (string= default-entry "")
8549 ""
8550 (format " (default %s)" default-entry))))))
8551 (if (string= input "")
8552 (if (string= default-entry "")
8553 (error "No perldoc args given")
8554 default-entry)
8555 input))))
a8e1e57f 8556 (require 'man)
f739b53b
SM
8557 (let* ((case-fold-search nil)
8558 (is-func (and
b7ec9e59
RS
8559 (string-match "^[a-z]+$" word)
8560 (string-match (concat "^" word "\\>")
8561 (documentation-property
8562 'cperl-short-docs
8563 'variable-documentation))))
8c777c8d 8564 (Man-switches "")
b7ec9e59 8565 (manual-program (if is-func "perldoc -f" "perldoc")))
f739b53b 8566 (cond
6546555e 8567 ((featurep 'xemacs)
f739b53b
SM
8568 (let ((Manual-program "perldoc")
8569 (Manual-switches (if is-func (list "-f"))))
8570 (manual-entry word)))
8571 (t
8572 (Man-getpage-in-background word)))))
b7ec9e59 8573
4ab89e7b 8574;;;###autoload
b7ec9e59
RS
8575(defun cperl-perldoc-at-point ()
8576 "Run a `perldoc' on the word around point."
8577 (interactive)
8578 (cperl-perldoc (cperl-word-at-point)))
8579
8580(defcustom pod2man-program "pod2man"
8581 "*File name for `pod2man'."
8582 :type 'file
8583 :group 'cperl)
8584
5bd52f0e 8585;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
b7ec9e59
RS
8586(defun cperl-pod-to-manpage ()
8587 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8588 (interactive)
8589 (require 'man)
8590 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8591 (bufname (concat "Man " buffer-file-name))
8592 (buffer (generate-new-buffer bufname)))
9a529312 8593 (with-current-buffer buffer
b7ec9e59
RS
8594 (let ((process-environment (copy-sequence process-environment)))
8595 ;; Prevent any attempt to use display terminal fanciness.
8596 (setenv "TERM" "dumb")
8597 (set-process-sentinel
8598 (start-process pod2man-program buffer "sh" "-c"
8599 (format (cperl-pod2man-build-command) pod2man-args))
8600 'Man-bgproc-sentinel)))))
8601
f739b53b
SM
8602;;; Updated version by him too
8603(defun cperl-build-manpage ()
8604 "Create a virtual manpage in Emacs from the POD in the file."
8605 (interactive)
8606 (require 'man)
8607 (cond
6546555e 8608 ((featurep 'xemacs)
f739b53b
SM
8609 (let ((Manual-program "perldoc"))
8610 (manual-entry buffer-file-name)))
8611 (t
8c777c8d
CY
8612 (let* ((manual-program "perldoc")
8613 (Man-switches ""))
f739b53b
SM
8614 (Man-getpage-in-background buffer-file-name)))))
8615
b7ec9e59
RS
8616(defun cperl-pod2man-build-command ()
8617 "Builds the entire background manpage and cleaning command."
8618 (let ((command (concat pod2man-program " %s 2>/dev/null"))
4ab89e7b 8619 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
b7ec9e59
RS
8620 (while (and flist (car flist))
8621 (let ((pcom (car (car flist)))
8622 (pargs (cdr (car flist))))
8623 (setq command
8624 (concat command " | " pcom " "
4f91a816
SM
8625 (mapconcat (lambda (phrase)
8626 (if (not (stringp phrase))
8627 (error "Malformed Man-filter-list"))
8628 phrase)
b7ec9e59
RS
8629 pargs " ")))
8630 (setq flist (cdr flist))))
8631 command))
db133cb6 8632
4ab89e7b
SM
8633
8634(defun cperl-next-interpolated-REx-1 ()
8635 "Move point to next REx which has interpolated parts without //o.
8636Skips RExes consisting of one interpolated variable.
8637
8638Note that skipped RExen are not performance hits."
8639 (interactive "")
8640 (cperl-next-interpolated-REx 1))
8641
8642(defun cperl-next-interpolated-REx-0 ()
8643 "Move point to next REx which has interpolated parts without //o."
8644 (interactive "")
8645 (cperl-next-interpolated-REx 0))
8646
8647(defun cperl-next-interpolated-REx (&optional skip beg limit)
8648 "Move point to next REx which has interpolated parts.
8649SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8650point and the limit of search (default to point and end of buffer).
8651
8652SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8653semantic may be used as a numeric argument.
8654
8655Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8656a result of qr//, this is not a performance hit), t for the rest."
8657 (interactive "P")
8658 (if (numberp skip) (setq skip (list 0 skip)))
8659 (or beg (setq beg (point)))
8660 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8661 (let (pp)
8662 (and (eq (get-text-property beg 'syntax-type) 'string)
8663 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8664 (cperl-map-pods-heres
8665 (function (lambda (s e p)
8666 (if (memq (get-text-property s 'REx-interpolated) skip)
8667 t
8668 (setq pp s)
8669 nil))) ; nil stops
8670 'REx-interpolated beg limit)
8671 (if pp (goto-char pp)
8672 (message "No more interpolated REx"))))
8673
8674;;; Initial version contributed by Trey Belew
8675(defun cperl-here-doc-spell (&optional beg end)
8676 "Spell-check HERE-documents in the Perl buffer.
8677If a region is highlighted, restricts to the region."
8678 (interactive "")
8679 (cperl-pod-spell t beg end))
8680
8681(defun cperl-pod-spell (&optional do-heres beg end)
8682 "Spell-check POD documentation.
8683If invoked with prefix argument, will do HERE-DOCs instead.
8684If a region is highlighted, restricts to the region."
8685 (interactive "P")
8686 (save-excursion
8687 (let (beg end)
8688 (if (cperl-mark-active)
8689 (setq beg (min (mark) (point))
8690 end (max (mark) (point)))
8691 (setq beg (point-min)
8692 end (point-max)))
8693 (cperl-map-pods-heres (function
8694 (lambda (s e p)
8695 (if do-heres
8696 (setq e (save-excursion
8697 (goto-char e)
8698 (forward-line -1)
8699 (point))))
8700 (ispell-region s e)
8701 t))
8702 (if do-heres 'here-doc-group 'in-pod)
8703 beg end))))
8704
8705(defun cperl-map-pods-heres (func &optional prop s end)
8706 "Executes a function over regions of pods or here-documents.
8707PROP is the text-property to search for; default to `in-pod'. Stop when
8708function returns nil."
8709 (let (pos posend has-prop (cont t))
8710 (or prop (setq prop 'in-pod))
8711 (or s (setq s (point-min)))
8712 (or end (setq end (point-max)))
8713 (cperl-update-syntaxification end end)
8714 (save-excursion
8715 (goto-char (setq pos s))
8716 (while (and cont (< pos end))
8717 (setq has-prop (get-text-property pos prop))
8718 (setq posend (next-single-property-change pos prop nil end))
8719 (and has-prop
8720 (setq cont (funcall func pos posend prop)))
8721 (setq pos posend)))))
8722
8723;;; Based on code by Masatake YAMATO:
8724(defun cperl-get-here-doc-region (&optional pos pod)
8725 "Return HERE document region around the point.
8726Return nil if the point is not in a HERE document region. If POD is non-nil,
8727will return a POD section if point is in a POD section."
8728 (or pos (setq pos (point)))
8729 (cperl-update-syntaxification pos pos)
8730 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8731 (and pod
8732 (eq 'pod (get-text-property pos 'syntax-type))))
8733 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8734 (e (next-single-property-change pos 'syntax-type)))
8735 (cons b (or e (point-max))))))
8736
8737(defun cperl-narrow-to-here-doc (&optional pos)
8738 "Narrows editing region to the HERE-DOC at POS.
8739POS defaults to the point."
8740 (interactive "d")
8741 (or pos (setq pos (point)))
8742 (let ((p (cperl-get-here-doc-region pos)))
8743 (or p (error "Not inside a HERE document"))
8744 (narrow-to-region (car p) (cdr p))
8745 (message
8746 "When you are finished with narrow editing, type C-x n w")))
8747
8748(defun cperl-select-this-pod-or-here-doc (&optional pos)
8749 "Select the HERE-DOC (or POD section) at POS.
8750POS defaults to the point."
8751 (interactive "d")
8752 (let ((p (cperl-get-here-doc-region pos t)))
8753 (if p
8754 (progn
8755 (goto-char (car p))
8756 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8757 (message "I do not think POS is in POD or a HERE-doc..."))))
8758
8759(defun cperl-facemenu-add-face-function (face end)
8760 "A callback to process user-initiated font-change requests.
8761Translates `bold', `italic', and `bold-italic' requests to insertion of
8762corresponding POD directives, and `underline' to C<> POD directive.
8763
8764Such requests are usually bound to M-o LETTER."
8765 (or (get-text-property (point) 'in-pod)
8766 (error "Faces can only be set within POD"))
8767 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8768 (cdr (or (assq face '((bold . "B<")
8769 (italic . "I<")
8770 (bold-italic . "B<I<")
8771 (underline . "C<")))
8772 (error "Face %s not configured for cperl-mode"
8773 face))))
8774\f
8775(defun cperl-time-fontification (&optional l step lim)
8776 "Times how long it takes to do incremental fontification in a region.
8777L is the line to start at, STEP is the number of lines to skip when
8778doing next incremental fontification, LIM is the maximal number of
8779incremental fontification to perform. Messages are accumulated in
8780*Messages* buffer.
8781
8782May be used for pinpointing which construct slows down buffer fontification:
8783start with default arguments, then refine the slowdown regions."
8784 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8785 (or l (setq l 1))
8786 (or step (setq step 500))
8787 (or lim (setq lim 40))
8788 (let* ((timems (function (lambda ()
8789 (let ((tt (current-time)))
8790 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8791 (tt (funcall timems)) (c 0) delta tot)
47e83968
GM
8792 (goto-char (point-min))
8793 (forward-line (1- l))
4ab89e7b
SM
8794 (cperl-mode)
8795 (setq tot (- (- tt (setq tt (funcall timems)))))
8796 (message "cperl-mode at %s: %s" l tot)
8797 (while (and (< c lim) (not (eobp)))
8798 (forward-line step)
8799 (setq l (+ l step))
8800 (setq c (1+ c))
8801 (cperl-update-syntaxification (point) (point))
8802 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8803 (message "to %s:%6s,%7s" l delta tot))
8804 tot))
8805
6546555e
DN
8806(defvar font-lock-cache-position)
8807
4ab89e7b
SM
8808(defun cperl-emulate-lazy-lock (&optional window-size)
8809 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8810Start fontifying the buffer from the start (or end) using the given
8811WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8812goes backwards; default is -50. This function is not CPerl-specific; it
8813may be used to debug problems with delayed incremental fontification."
8814 (interactive
8815 "nSize of window for incremental fontification, negative goes backwards: ")
8816 (or window-size (setq window-size -50))
8817 (let ((pos (if (> window-size 0)
8818 (point-min)
8819 (point-max)))
8820 p)
8821 (goto-char pos)
8822 (normal-mode)
8823 ;; Why needed??? With older font-locks???
8824 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8825 (while (if (> window-size 0)
8826 (< pos (point-max))
8827 (> pos (point-min)))
8828 (setq p (progn
8829 (forward-line window-size)
8830 (point)))
8831 (font-lock-fontify-region (min p pos) (max p pos))
8832 (setq pos p))))
8833
8834\f
db133cb6 8835(defun cperl-lazy-install ()) ; Avoid a warning
f739b53b 8836(defun cperl-lazy-unstall ()) ; Avoid a warning
f83d2997
KH
8837
8838(if (fboundp 'run-with-idle-timer)
8839 (progn
8840 (defvar cperl-help-shown nil
8841 "Non-nil means that the help was already shown now.")
8842
8843 (defvar cperl-lazy-installed nil
8844 "Non-nil means that the lazy-help handlers are installed now.")
8845
8846 (defun cperl-lazy-install ()
f739b53b
SM
8847 "Switches on Auto-Help on Perl constructs (put in the message area).
8848Delay of auto-help controlled by `cperl-lazy-help-time'."
f83d2997 8849 (interactive)
4ab89e7b 8850 (make-local-variable 'cperl-help-shown)
f83d2997
KH
8851 (if (and (cperl-val 'cperl-lazy-help-time)
8852 (not cperl-lazy-installed))
8853 (progn
8854 (add-hook 'post-command-hook 'cperl-lazy-hook)
5c8b7eaf
SS
8855 (run-with-idle-timer
8856 (cperl-val 'cperl-lazy-help-time 1000000 5)
8857 t
f83d2997
KH
8858 'cperl-get-help-defer)
8859 (setq cperl-lazy-installed t))))
8860
8861 (defun cperl-lazy-unstall ()
f739b53b
SM
8862 "Switches off Auto-Help on Perl constructs (put in the message area).
8863Delay of auto-help controlled by `cperl-lazy-help-time'."
f83d2997
KH
8864 (interactive)
8865 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8866 (cancel-function-timers 'cperl-get-help-defer)
8867 (setq cperl-lazy-installed nil))
8868
8869 (defun cperl-lazy-hook ()
8870 (setq cperl-help-shown nil))
8871
8872 (defun cperl-get-help-defer ()
83261a2f 8873 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
f83d2997
KH
8874 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8875 (cperl-get-help)
8876 (setq cperl-help-shown t))))
8877 (cperl-lazy-install)))
8878
db133cb6
RS
8879
8880;;; Plug for wrong font-lock:
8881
8882(defun cperl-font-lock-unfontify-region-function (beg end)
4ab89e7b
SM
8883 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8884 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8885 before-change-functions after-change-functions
8886 deactivate-mark buffer-file-name buffer-file-truename)
8887 (remove-text-properties beg end '(face nil))
8888 (if (and (not modified) (buffer-modified-p))
8889 (set-buffer-modified-p nil))))
8890
8891(defun cperl-font-lock-fontify-region-function (beg end loudly)
8892 "Extends the region to safe positions, then calls the default function.
8893Newer `font-lock's can do it themselves.
8894We unwind only as far as needed for fontification. Syntaxification may
8895do extra unwind via `cperl-unwind-to-safe'."
8896 (save-excursion
8897 (goto-char beg)
8898 (while (and beg
8899 (progn
8900 (beginning-of-line)
8901 (eq (get-text-property (setq beg (point)) 'syntax-type)
8902 'multiline)))
ec79b92b
NT
8903 (let ((new-beg (cperl-beginning-of-property beg 'syntax-type)))
8904 (setq beg (if (= new-beg beg) nil new-beg))
8905 (goto-char new-beg)))
4ab89e7b
SM
8906 (setq beg (point))
8907 (goto-char end)
8908 (while (and end
8909 (progn
8910 (or (bolp) (condition-case nil
8911 (forward-line 1)
8912 (error nil)))
8913 (eq (get-text-property (setq end (point)) 'syntax-type)
8914 'multiline)))
8915 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8916 (goto-char end))
8917 (setq end (point)))
8918 (font-lock-default-fontify-region beg end loudly))
db133cb6
RS
8919
8920(defvar cperl-d-l nil)
8921(defun cperl-fontify-syntaxically (end)
5bd52f0e 8922 ;; Some vars for debugging only
6c389151 8923 ;; (message "Syntaxifying...")
4ab89e7b 8924 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
83261a2f 8925 (istate (car cperl-syntax-state))
4ab89e7b
SM
8926 start from-start edebug-backtrace-buffer)
8927 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8928 (progn
8929 (require 'edebug)
8930 (let ((f 'edebug-backtrace))
8931 (funcall f)))) ; Avoid compile-time warning
db133cb6 8932 (or cperl-syntax-done-to
4ab89e7b
SM
8933 (setq cperl-syntax-done-to (point-min)
8934 from-start t))
8935 (setq start (if (and cperl-hook-after-change
8936 (not from-start))
8937 cperl-syntax-done-to ; Fontify without change; ignore start
8938 ;; Need to forget what is after `start'
8939 (min cperl-syntax-done-to (point))))
8940 (goto-char start)
8941 (beginning-of-line)
8942 (setq start (point))
8943 (and cperl-syntaxify-unwind
8944 (setq end (cperl-unwind-to-safe t end)
8945 start (point)))
db133cb6
RS
8946 (and (> end start)
8947 (setq cperl-syntax-done-to start) ; In case what follows fails
8948 (cperl-find-pods-heres start end t nil t))
4ab89e7b
SM
8949 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8950 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8951 dbg iend start end idone cperl-syntax-done-to
5c8b7eaf 8952 istate (car cperl-syntax-state))) ; For debugging
83261a2f 8953 nil)) ; Do not iterate
db133cb6 8954
5bd52f0e 8955(defun cperl-fontify-update (end)
4ab89e7b
SM
8956 (let ((pos (point-min)) prop posend)
8957 (setq end (point-max))
5bd52f0e 8958 (while (< pos end)
4ab89e7b
SM
8959 (setq prop (get-text-property pos 'cperl-postpone)
8960 posend (next-single-property-change pos 'cperl-postpone nil end))
5bd52f0e
RS
8961 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8962 (setq pos posend)))
83261a2f 8963 nil) ; Do not iterate
5bd52f0e 8964
4ab89e7b
SM
8965(defun cperl-fontify-update-bad (end)
8966 ;; Since fontification happens with different region than syntaxification,
8967 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8968 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8969 (if prop
8970 (setq pos (or (cperl-beginning-of-property
8971 (cperl-1+ pos) 'cperl-postpone)
8972 (point-min))))
8973 (while (< pos end)
8974 (setq posend (next-single-property-change pos 'cperl-postpone))
8975 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8976 (setq pos posend)
8977 (setq prop (get-text-property pos 'cperl-postpone))))
8978 nil) ; Do not iterate
8979
8980;; Called when any modification is made to buffer text.
8981(defun cperl-after-change-function (beg end old-len)
8982 ;; We should have been informed about changes by `font-lock'. Since it
4c36be58 8983 ;; does not inform as which calls are deferred, do it ourselves
4ab89e7b
SM
8984 (if cperl-syntax-done-to
8985 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8986
5bd52f0e 8987(defun cperl-update-syntaxification (from to)
bde2ab6f
SM
8988 (cond
8989 ((not cperl-use-syntax-table-text-property) nil)
8990 ((fboundp 'syntax-propertize) (syntax-propertize to))
8991 ((and cperl-syntaxify-by-font-lock
8992 (or (null cperl-syntax-done-to)
8993 (< cperl-syntax-done-to to)))
8994 (save-excursion
8995 (goto-char from)
8996 (cperl-fontify-syntaxically to)))))
5bd52f0e 8997
5c8b7eaf 8998(defvar cperl-version
8c777c8d 8999 (let ((v "Revision: 6.2"))
5bd52f0e
RS
9000 (string-match ":\\s *\\([0-9.]+\\)" v)
9001 (substring v (match-beginning 1) (match-end 1)))
9002 "Version of IZ-supported CPerl package this file is based on.")
9003
f83d2997
KH
9004(provide 'cperl-mode)
9005
9006;;; cperl-mode.el ends here