2006-10-22 John Wiegley <johnw@newartisans.com>
[bpt/emacs.git] / lisp / progmodes / cperl-mode.el
CommitLineData
f83d2997
KH
1;;; cperl-mode.el --- Perl code editing commands for Emacs
2
034babe1 3;; Copyright (C) 1985, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
d91362c9 4;; 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
f83d2997
KH
5;; Free Software Foundation, Inc.
6
7;; Author: Ilya Zakharevich and Bob Olson
4ab89e7b 8;; Maintainer: Ilya Zakharevich <ilyaz@cpan.org>
f83d2997
KH
9;; Keywords: languages, Perl
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
f83d2997 27
4ab89e7b 28;;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
f83d2997
KH
29
30;;; Commentary:
31
83261a2f
SM
32;; You can either fine-tune the bells and whistles of this mode or
33;; bulk enable them by putting
f83d2997
KH
34
35;; (setq cperl-hairy t)
36
83261a2f
SM
37;; in your .emacs file. (Emacs rulers do not consider it politically
38;; correct to make whistles enabled by default.)
f83d2997 39
83261a2f
SM
40;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<<
41;; or as help on variables `cperl-tips', `cperl-problems', <<<<<<
15ca5699 42;; `cperl-praise', `cperl-speed'. <<<<<<
f83d2997 43
83261a2f
SM
44;; The mode information (on C-h m) provides some customization help.
45;; If you use font-lock feature of this mode, it is advisable to use
46;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock.
f83d2997 47
83261a2f
SM
48;; Faces used now: three faces for first-class and second-class keywords
49;; and control flow words, one for each: comments, string, labels,
50;; functions definitions and packages, arrays, hashes, and variable
51;; definitions. If you do not see all these faces, your font-lock does
52;; not define them, so you need to define them manually.
f83d2997 53
83261a2f
SM
54;; This mode supports font-lock, imenu and mode-compile. In the
55;; hairy version font-lock is on, but you should activate imenu
56;; yourself (note that mode-compile is not standard yet). Well, you
57;; can use imenu from keyboard anyway (M-x imenu), but it is better
58;; to bind it like that:
f83d2997
KH
59
60;; (define-key global-map [M-S-down-mouse-3] 'imenu)
61
83261a2f
SM
62;;; Font lock bugs as of v4.32:
63
64;; The following kinds of Perl code erroneously start strings:
65;; \$` \$' \$"
66;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
67;; likewise with m, tr, y, q, qX instead of s
68
f83d2997 69;;; Code:
4ab89e7b 70\f
b5b0cb34
JB
71(defvar vc-rcs-header)
72(defvar vc-sccs-header)
73
80585273 74(eval-when-compile
4ab89e7b
SM
75 (condition-case nil
76 (require 'custom)
77 (error nil))
78 (condition-case nil
79 (require 'man)
80 (error nil))
81 (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
82 (defvar cperl-can-font-lock
83 (or cperl-xemacs-p
84 (and (boundp 'emacs-major-version)
85 (or window-system
86 (> emacs-major-version 20)))))
87 (if cperl-can-font-lock
88 (require 'font-lock))
89 (defvar msb-menu-cond)
90 (defvar gud-perldb-history)
91 (defvar font-lock-background-mode) ; not in Emacs
92 (defvar font-lock-display-type) ; ditto
93 (defvar paren-backwards-message) ; Not in newer XEmacs?
94 (or (fboundp 'defgroup)
95 (defmacro defgroup (name val doc &rest arr)
96 nil))
97 (or (fboundp 'custom-declare-variable)
98 (defmacro defcustom (name val doc &rest arr)
99 (` (defvar (, name) (, val) (, doc)))))
100 (or (and (fboundp 'custom-declare-variable)
101 (string< "19.31" emacs-version)) ; Checked with 19.30: defface does not work
102 (defmacro defface (&rest arr)
103 nil))
104 ;; Avoid warning (tmp definitions)
105 (or (fboundp 'x-color-defined-p)
106 (defmacro x-color-defined-p (col)
107 (cond ((fboundp 'color-defined-p) (` (color-defined-p (, col))))
108 ;; XEmacs >= 19.12
109 ((fboundp 'valid-color-name-p) (` (valid-color-name-p (, col))))
110 ;; XEmacs 19.11
111 ((fboundp 'x-valid-color-name-p) (` (x-valid-color-name-p (, col))))
112 (t '(error "Cannot implement color-defined-p")))))
113 (defmacro cperl-is-face (arg) ; Takes quoted arg
114 (cond ((fboundp 'find-face)
115 (` (find-face (, arg))))
116 (;;(and (fboundp 'face-list)
117 ;; (face-list))
118 (fboundp 'face-list)
119 (` (member (, arg) (and (fboundp 'face-list)
120 (face-list)))))
121 (t
122 (` (boundp (, arg))))))
123 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
124 (cond ((fboundp 'make-face)
125 (` (make-face (quote (, arg)))))
126 (t
127 (` (defvar (, arg) (quote (, arg)) (, descr))))))
128 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
129 (` (progn
130 (or (cperl-is-face (quote (, arg)))
131 (cperl-make-face (, arg) (, descr)))
132 (or (boundp (quote (, arg))) ; We use unquoted variants too
133 (defvar (, arg) (quote (, arg)) (, descr))))))
134 (if cperl-xemacs-p
135 (defmacro cperl-etags-snarf-tag (file line)
136 (` (progn
137 (beginning-of-line 2)
138 (list (, file) (, line)))))
139 (defmacro cperl-etags-snarf-tag (file line)
140 (` (etags-snarf-tag))))
141 (if cperl-xemacs-p
142 (defmacro cperl-etags-goto-tag-location (elt)
143 (`;;(progn
144 ;; (switch-to-buffer (get-file-buffer (elt (, elt) 0)))
145 ;; (set-buffer (get-file-buffer (elt (, elt) 0)))
146 ;; Probably will not work due to some save-excursion???
147 ;; Or save-file-position?
148 ;; (message "Did I get to line %s?" (elt (, elt) 1))
149 (goto-line (string-to-int (elt (, elt) 1)))))
150 ;;)
151 (defmacro cperl-etags-goto-tag-location (elt)
152 (` (etags-goto-tag-location (, elt))))))
5bd52f0e 153
83261a2f
SM
154(defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
155
156(defvar cperl-can-font-lock
157 (or cperl-xemacs-p
158 (and (boundp 'emacs-major-version)
159 (or window-system
160 (> emacs-major-version 20)))))
161
5bd52f0e
RS
162(defun cperl-choose-color (&rest list)
163 (let (answer)
164 (while list
165 (or answer
166 (if (or (x-color-defined-p (car list))
167 (null (cdr list)))
168 (setq answer (car list))))
169 (setq list (cdr list)))
170 answer))
171
ccc3ce39
SE
172(defgroup cperl nil
173 "Major mode for editing Perl code."
174 :prefix "cperl-"
db133cb6
RS
175 :group 'languages
176 :version "20.3")
177
178(defgroup cperl-indentation-details nil
179 "Indentation."
180 :prefix "cperl-"
181 :group 'cperl)
182
183(defgroup cperl-affected-by-hairy nil
184 "Variables affected by `cperl-hairy'."
185 :prefix "cperl-"
186 :group 'cperl)
187
188(defgroup cperl-autoinsert-details nil
189 "Auto-insert tuneup."
190 :prefix "cperl-"
191 :group 'cperl)
192
193(defgroup cperl-faces nil
194 "Fontification colors."
8ec3bce0 195 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
db133cb6
RS
196 :prefix "cperl-"
197 :group 'cperl)
198
199(defgroup cperl-speed nil
200 "Speed vs. validity tuneup."
201 :prefix "cperl-"
202 :group 'cperl)
203
204(defgroup cperl-help-system nil
205 "Help system tuneup."
206 :prefix "cperl-"
207 :group 'cperl)
ccc3ce39 208
f83d2997 209\f
ccc3ce39 210(defcustom cperl-extra-newline-before-brace nil
f83d2997
KH
211 "*Non-nil means that if, elsif, while, until, else, for, foreach
212and do constructs look like:
213
214 if ()
215 {
216 }
217
218instead of:
219
220 if () {
83261a2f 221 }"
ccc3ce39 222 :type 'boolean
db133cb6
RS
223 :group 'cperl-autoinsert-details)
224
5c8b7eaf 225(defcustom cperl-extra-newline-before-brace-multiline
db133cb6
RS
226 cperl-extra-newline-before-brace
227 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
228for constructs with multiline if/unless/while/until/for/foreach condition."
229 :type 'boolean
230 :group 'cperl-autoinsert-details)
ccc3ce39
SE
231
232(defcustom cperl-indent-level 2
233 "*Indentation of CPerl statements with respect to containing block."
234 :type 'integer
db133cb6 235 :group 'cperl-indentation-details)
f83d2997 236
ccc3ce39 237(defcustom cperl-lineup-step nil
f83d2997 238 "*`cperl-lineup' will always lineup at multiple of this number.
029cb4d5 239If nil, the value of `cperl-indent-level' will be used."
ccc3ce39 240 :type '(choice (const nil) integer)
db133cb6
RS
241 :group 'cperl-indentation-details)
242
ccc3ce39 243(defcustom cperl-brace-imaginary-offset 0
f83d2997
KH
244 "*Imagined indentation of a Perl open brace that actually follows a statement.
245An open brace following other text is treated as if it were this far
ccc3ce39
SE
246to the right of the start of its line."
247 :type 'integer
db133cb6 248 :group 'cperl-indentation-details)
ccc3ce39
SE
249
250(defcustom cperl-brace-offset 0
251 "*Extra indentation for braces, compared with other text in same context."
252 :type 'integer
db133cb6 253 :group 'cperl-indentation-details)
ccc3ce39
SE
254(defcustom cperl-label-offset -2
255 "*Offset of CPerl label lines relative to usual indentation."
256 :type 'integer
db133cb6 257 :group 'cperl-indentation-details)
ccc3ce39
SE
258(defcustom cperl-min-label-indent 1
259 "*Minimal offset of CPerl label lines."
260 :type 'integer
db133cb6 261 :group 'cperl-indentation-details)
ccc3ce39
SE
262(defcustom cperl-continued-statement-offset 2
263 "*Extra indent for lines not starting new statements."
264 :type 'integer
db133cb6 265 :group 'cperl-indentation-details)
ccc3ce39 266(defcustom cperl-continued-brace-offset 0
f83d2997 267 "*Extra indent for substatements that start with open-braces.
ccc3ce39
SE
268This is in addition to cperl-continued-statement-offset."
269 :type 'integer
db133cb6 270 :group 'cperl-indentation-details)
ccc3ce39
SE
271(defcustom cperl-close-paren-offset -1
272 "*Extra indent for substatements that start with close-parenthesis."
273 :type 'integer
db133cb6 274 :group 'cperl-indentation-details)
ccc3ce39 275
4ab89e7b
SM
276(defcustom cperl-indent-wrt-brace t
277 "*Non-nil means indent statements in if/etc block relative brace, not if/etc.
278Versions 5.2 ... 5.20 behaved as if this were `nil'."
279 :type 'boolean
280 :group 'cperl-indentation-details)
281
ccc3ce39 282(defcustom cperl-auto-newline nil
f83d2997
KH
283 "*Non-nil means automatically newline before and after braces,
284and after colons and semicolons, inserted in CPerl code. The following
285\\[cperl-electric-backspace] will remove the inserted whitespace.
5c8b7eaf 286Insertion after colons requires both this variable and
ccc3ce39
SE
287`cperl-auto-newline-after-colon' set."
288 :type 'boolean
db133cb6 289 :group 'cperl-autoinsert-details)
f83d2997 290
6c389151
SM
291(defcustom cperl-autoindent-on-semi nil
292 "*Non-nil means automatically indent after insertion of (semi)colon.
293Active if `cperl-auto-newline' is false."
294 :type 'boolean
295 :group 'cperl-autoinsert-details)
296
ccc3ce39 297(defcustom cperl-auto-newline-after-colon nil
f83d2997 298 "*Non-nil means automatically newline even after colons.
ccc3ce39
SE
299Subject to `cperl-auto-newline' setting."
300 :type 'boolean
db133cb6 301 :group 'cperl-autoinsert-details)
f83d2997 302
ccc3ce39 303(defcustom cperl-tab-always-indent t
f83d2997 304 "*Non-nil means TAB in CPerl mode should always reindent the current line,
ccc3ce39
SE
305regardless of where in the line point is when the TAB command is used."
306 :type 'boolean
db133cb6 307 :group 'cperl-indentation-details)
f83d2997 308
ccc3ce39 309(defcustom cperl-font-lock nil
029cb4d5 310 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
ccc3ce39 311Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
312 :type '(choice (const null) boolean)
313 :group 'cperl-affected-by-hairy)
f83d2997 314
ccc3ce39 315(defcustom cperl-electric-lbrace-space nil
029cb4d5 316 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
ccc3ce39 317Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
318 :type '(choice (const null) boolean)
319 :group 'cperl-affected-by-hairy)
f83d2997 320
ccc3ce39 321(defcustom cperl-electric-parens-string "({[]})<"
f83d2997 322 "*String of parentheses that should be electric in CPerl.
ccc3ce39
SE
323Closing ones are electric only if the region is highlighted."
324 :type 'string
db133cb6 325 :group 'cperl-affected-by-hairy)
f83d2997 326
ccc3ce39 327(defcustom cperl-electric-parens nil
f83d2997 328 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
ccc3ce39 329Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
330 :type '(choice (const null) boolean)
331 :group 'cperl-affected-by-hairy)
332
333(defvar zmacs-regions) ; Avoid warning
334
5c8b7eaf 335(defcustom cperl-electric-parens-mark
f83d2997
KH
336 (and window-system
337 (or (and (boundp 'transient-mark-mode) ; For Emacs
338 transient-mark-mode)
339 (and (boundp 'zmacs-regions) ; For XEmacs
340 zmacs-regions)))
341 "*Not-nil means that electric parens look for active mark.
ccc3ce39
SE
342Default is yes if there is visual feedback on mark."
343 :type 'boolean
db133cb6 344 :group 'cperl-autoinsert-details)
f83d2997 345
ccc3ce39 346(defcustom cperl-electric-linefeed nil
f83d2997
KH
347 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
348In any case these two mean plain and hairy linefeeds together.
ccc3ce39 349Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
350 :type '(choice (const null) boolean)
351 :group 'cperl-affected-by-hairy)
f83d2997 352
ccc3ce39 353(defcustom cperl-electric-keywords nil
f83d2997 354 "*Not-nil (and non-null) means keywords are electric in CPerl.
ccc3ce39 355Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
356 :type '(choice (const null) boolean)
357 :group 'cperl-affected-by-hairy)
ccc3ce39 358
f739b53b
SM
359(defcustom cperl-electric-backspace-untabify t
360 "*Not-nil means electric-backspace will untabify in CPerl."
361 :type 'boolean
362 :group 'cperl-autoinsert-details)
363
ccc3ce39 364(defcustom cperl-hairy nil
db133cb6 365 "*Not-nil means most of the bells and whistles are enabled in CPerl.
5c8b7eaf 366Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
db133cb6
RS
367`cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
368`cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
369`cperl-lazy-help-time'."
ccc3ce39 370 :type 'boolean
db133cb6 371 :group 'cperl-affected-by-hairy)
ccc3ce39
SE
372
373(defcustom cperl-comment-column 32
374 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
375 :type 'integer
db133cb6 376 :group 'cperl-indentation-details)
ccc3ce39 377
4ab89e7b
SM
378(defcustom cperl-indent-comment-at-column-0 nil
379 "*Non-nil means that comment started at column 0 should be indentable."
380 :type 'boolean
381 :group 'cperl-indentation-details)
e1a5828f
AS
382
383(defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
384 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
385 :type '(repeat string)
386 :group 'cperl)
387
4ab89e7b 388(defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
e1a5828f
AS
389 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
390 :type '(repeat string)
4ab89e7b
SM
391 :group 'cperl)
392
393;; This became obsolete...
394(defvar cperl-vc-header-alist nil)
395(make-obsolete-variable
396 'cperl-vc-header-alist
397 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead.")
ccc3ce39 398
5c8b7eaf 399(defcustom cperl-clobber-mode-lists
5bd52f0e
RS
400 (not
401 (and
402 (boundp 'interpreter-mode-alist)
403 (assoc "miniperl" interpreter-mode-alist)
404 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
405 "*Whether to install us into `interpreter-' and `extension' mode lists."
406 :type 'boolean
407 :group 'cperl)
408
ccc3ce39 409(defcustom cperl-info-on-command-no-prompt nil
f83d2997 410 "*Not-nil (and non-null) means not to prompt on C-h f.
6292d528 411The opposite behavior is always available if prefixed with C-c.
ccc3ce39 412Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
413 :type '(choice (const null) boolean)
414 :group 'cperl-affected-by-hairy)
415
416(defcustom cperl-clobber-lisp-bindings nil
417 "*Not-nil (and non-null) means not overwrite C-h f.
418The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
419Can be overwritten by `cperl-hairy' if nil."
420 :type '(choice (const null) boolean)
421 :group 'cperl-affected-by-hairy)
f83d2997 422
ccc3ce39 423(defcustom cperl-lazy-help-time nil
db133cb6
RS
424 "*Not-nil (and non-null) means to show lazy help after given idle time.
425Can be overwritten by `cperl-hairy' to be 5 sec if nil."
300f7bb3 426 :type '(choice (const null) (const nil) integer)
db133cb6 427 :group 'cperl-affected-by-hairy)
f83d2997 428
ccc3ce39 429(defcustom cperl-pod-face 'font-lock-comment-face
83261a2f 430 "*Face for POD highlighting."
ccc3ce39 431 :type 'face
db133cb6 432 :group 'cperl-faces)
f83d2997 433
ccc3ce39 434(defcustom cperl-pod-head-face 'font-lock-variable-name-face
83261a2f 435 "*Face for POD highlighting.
ccc3ce39
SE
436Font for POD headers."
437 :type 'face
db133cb6 438 :group 'cperl-faces)
f83d2997 439
ccc3ce39 440(defcustom cperl-here-face 'font-lock-string-face
80585273 441 "*Face for here-docs highlighting."
ccc3ce39 442 :type 'face
db133cb6 443 :group 'cperl-faces)
f83d2997 444
4ab89e7b
SM
445;;; Some double-evaluation happened with font-locks... Needed with 21.2...
446(defvar cperl-singly-quote-face cperl-xemacs-p)
447
448(defcustom cperl-invalid-face ; Does not customize with '' on XEmacs
449 (if cperl-singly-quote-face
450 'underline ''underline) ; On older Emacsen was evaluated by `font-lock'
451 (if cperl-singly-quote-face
452 "*This face is used for highlighting trailing whitespace."
453 "*Face for highlighting trailing whitespace.")
80585273 454 :type 'face
ac6857fb 455 :version "21.1"
5bd52f0e
RS
456 :group 'cperl-faces)
457
ccc3ce39 458(defcustom cperl-pod-here-fontify '(featurep 'font-lock)
83261a2f 459 "*Not-nil after evaluation means to highlight POD and here-docs sections."
ccc3ce39 460 :type 'boolean
db133cb6 461 :group 'cperl-faces)
f83d2997 462
5bd52f0e
RS
463(defcustom cperl-fontify-m-as-s t
464 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
465 :type 'boolean
466 :group 'cperl-faces)
467
6c389151
SM
468(defcustom cperl-highlight-variables-indiscriminately nil
469 "*Non-nil means perform additional highlighting on variables.
470Currently only changes how scalar variables are highlighted.
471Note that that variable is only read at initialization time for
472the variable `cperl-font-lock-keywords-2', so changing it after you've
f94a632a 473entered CPerl mode the first time will have no effect."
6c389151
SM
474 :type 'boolean
475 :group 'cperl)
476
ccc3ce39 477(defcustom cperl-pod-here-scan t
83261a2f 478 "*Not-nil means look for POD and here-docs sections during startup.
ccc3ce39
SE
479You can always make lookup from menu or using \\[cperl-find-pods-heres]."
480 :type 'boolean
db133cb6 481 :group 'cperl-speed)
f83d2997 482
6c389151
SM
483(defcustom cperl-regexp-scan t
484 "*Not-nil means make marking of regular expression more thorough.
4ab89e7b
SM
485Effective only with `cperl-pod-here-scan'."
486 :type 'boolean
487 :group 'cperl-speed)
488
489(defcustom cperl-hook-after-change t
490 "*Not-nil means install hook to know which regions of buffer are changed.
491May significantly speed up delayed fontification. Changes take effect
492after reload."
6c389151
SM
493 :type 'boolean
494 :group 'cperl-speed)
495
ccc3ce39 496(defcustom cperl-imenu-addback nil
f83d2997 497 "*Not-nil means add backreferences to generated `imenu's.
db133cb6 498May require patched `imenu' and `imenu-go'. Obsolete."
ccc3ce39 499 :type 'boolean
db133cb6 500 :group 'cperl-help-system)
f83d2997 501
ccc3ce39
SE
502(defcustom cperl-max-help-size 66
503 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
504 :type '(choice integer (const nil))
db133cb6 505 :group 'cperl-help-system)
f83d2997 506
ccc3ce39
SE
507(defcustom cperl-shrink-wrap-info-frame t
508 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
509 :type 'boolean
db133cb6 510 :group 'cperl-help-system)
f83d2997 511
ccc3ce39 512(defcustom cperl-info-page "perl"
f83d2997 513 "*Name of the info page containing perl docs.
ccc3ce39
SE
514Older version of this page was called `perl5', newer `perl'."
515 :type 'string
db133cb6 516 :group 'cperl-help-system)
f83d2997 517
5c8b7eaf 518(defcustom cperl-use-syntax-table-text-property
f83d2997 519 (boundp 'parse-sexp-lookup-properties)
ccc3ce39
SE
520 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
521 :type 'boolean
db133cb6 522 :group 'cperl-speed)
f83d2997 523
5c8b7eaf 524(defcustom cperl-use-syntax-table-text-property-for-tags
f83d2997 525 cperl-use-syntax-table-text-property
ccc3ce39
SE
526 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
527 :type 'boolean
db133cb6 528 :group 'cperl-speed)
ccc3ce39
SE
529
530(defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
531 "*Regexp to match files to scan when generating TAGS."
532 :type 'regexp
533 :group 'cperl)
534
8937f01b
RS
535(defcustom cperl-noscan-files-regexp
536 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
ccc3ce39
SE
537 "*Regexp to match files/dirs to skip when generating TAGS."
538 :type 'regexp
539 :group 'cperl)
540
541(defcustom cperl-regexp-indent-step nil
542 "*Indentation used when beautifying regexps.
029cb4d5 543If nil, the value of `cperl-indent-level' will be used."
ccc3ce39 544 :type '(choice integer (const nil))
db133cb6 545 :group 'cperl-indentation-details)
ccc3ce39
SE
546
547(defcustom cperl-indent-left-aligned-comments t
548 "*Non-nil means that the comment starting in leftmost column should indent."
549 :type 'boolean
db133cb6 550 :group 'cperl-indentation-details)
ccc3ce39 551
8f222248 552(defcustom cperl-under-as-char nil
ccc3ce39
SE
553 "*Non-nil means that the _ (underline) should be treated as word char."
554 :type 'boolean
555 :group 'cperl)
f83d2997 556
db133cb6
RS
557(defcustom cperl-extra-perl-args ""
558 "*Extra arguments to use when starting Perl.
559Currently used with `cperl-check-syntax' only."
560 :type 'string
561 :group 'cperl)
562
563(defcustom cperl-message-electric-keyword t
564 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
565 :type 'boolean
566 :group 'cperl-help-system)
567
568(defcustom cperl-indent-region-fix-constructs 1
569 "*Amount of space to insert between `}' and `else' or `elsif'
570in `cperl-indent-region'. Set to nil to leave as is. Values other
571than 1 and nil will probably not work."
572 :type '(choice (const nil) (const 1))
573 :group 'cperl-indentation-details)
574
575(defcustom cperl-break-one-line-blocks-when-indent t
576 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
2022c546 577need to be reformatted into multiline ones when indenting a region."
db133cb6
RS
578 :type 'boolean
579 :group 'cperl-indentation-details)
580
581(defcustom cperl-fix-hanging-brace-when-indent t
582 "*Non-nil means that BLOCK-end `}' may be put on a separate line
5c8b7eaf 583when indenting a region.
db133cb6
RS
584Braces followed by else/elsif/while/until are excepted."
585 :type 'boolean
586 :group 'cperl-indentation-details)
587
588(defcustom cperl-merge-trailing-else t
5c8b7eaf 589 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
db133cb6
RS
590may be merged to be on the same line when indenting a region."
591 :type 'boolean
592 :group 'cperl-indentation-details)
593
6c389151
SM
594(defcustom cperl-indent-parens-as-block nil
595 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
596but for trailing \",\" inside the group, which won't increase indentation.
597One should tune up `cperl-close-paren-offset' as well."
598 :type 'boolean
599 :group 'cperl-indentation-details)
600
a1506d29 601(defcustom cperl-syntaxify-by-font-lock
83261a2f 602 (and cperl-can-font-lock
5bd52f0e 603 (boundp 'parse-sexp-lookup-properties))
6c389151 604 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
5bd52f0e
RS
605 :type '(choice (const message) boolean)
606 :group 'cperl-speed)
607
608(defcustom cperl-syntaxify-unwind
609 t
f94a632a 610 "*Non-nil means that CPerl unwinds to a start of a long construction
5bd52f0e 611when syntaxifying a chunk of buffer."
db133cb6
RS
612 :type 'boolean
613 :group 'cperl-speed)
614
4ab89e7b
SM
615(defcustom cperl-syntaxify-for-menu
616 t
617 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
618This way enabling/disabling of menu items is more correct."
619 :type 'boolean
620 :group 'cperl-speed)
621
5bd52f0e
RS
622(defcustom cperl-ps-print-face-properties
623 '((font-lock-keyword-face nil nil bold shadow)
624 (font-lock-variable-name-face nil nil bold)
625 (font-lock-function-name-face nil nil bold italic box)
626 (font-lock-constant-face nil "LightGray" bold)
4ab89e7b
SM
627 (cperl-array-face nil "LightGray" bold underline)
628 (cperl-hash-face nil "LightGray" bold italic underline)
5bd52f0e
RS
629 (font-lock-comment-face nil "LightGray" italic)
630 (font-lock-string-face nil nil italic underline)
4ab89e7b 631 (cperl-nonoverridable-face nil nil italic underline)
5bd52f0e 632 (font-lock-type-face nil nil underline)
4ab89e7b 633 (font-lock-warning-face nil "LightGray" bold italic box)
5bd52f0e
RS
634 (underline nil "LightGray" strikeout))
635 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
5c8b7eaf 636 :type '(repeat (cons symbol
5bd52f0e
RS
637 (cons (choice (const nil) string)
638 (cons (choice (const nil) string)
639 (repeat symbol)))))
640 :group 'cperl-faces)
641
5cc679ab
JB
642(defvar cperl-dark-background
643 (cperl-choose-color "navy" "os2blue" "darkgreen"))
644(defvar cperl-dark-foreground
645 (cperl-choose-color "orchid1" "orange"))
646
4ab89e7b 647(defface cperl-nonoverridable-face
5cc679ab
JB
648 `((((class grayscale) (background light))
649 (:background "Gray90" :slant italic :underline t))
650 (((class grayscale) (background dark))
651 (:foreground "Gray80" :slant italic :underline t :weight bold))
652 (((class color) (background light))
653 (:foreground "chartreuse3"))
654 (((class color) (background dark))
655 (:foreground ,cperl-dark-foreground))
656 (t (:weight bold :underline t)))
c73fce9a 657 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
5cc679ab
JB
658 :group 'cperl-faces)
659
4ab89e7b 660(defface cperl-array-face
5cc679ab
JB
661 `((((class grayscale) (background light))
662 (:background "Gray90" :weight bold))
663 (((class grayscale) (background dark))
664 (:foreground "Gray80" :weight bold))
665 (((class color) (background light))
666 (:foreground "Blue" :background "lightyellow2" :weight bold))
667 (((class color) (background dark))
668 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
669 (t (:weight bold)))
670 "Font Lock mode face used to highlight array names."
671 :group 'cperl-faces)
672
4ab89e7b 673(defface cperl-hash-face
5cc679ab
JB
674 `((((class grayscale) (background light))
675 (:background "Gray90" :weight bold :slant italic))
676 (((class grayscale) (background dark))
677 (:foreground "Gray80" :weight bold :slant italic))
678 (((class color) (background light))
679 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
680 (((class color) (background dark))
681 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
682 (t (:weight bold :slant italic)))
683 "Font Lock mode face used to highlight hash names."
684 :group 'cperl-faces)
5bd52f0e 685
f83d2997
KH
686\f
687
688;;; Short extra-docs.
689
690(defvar cperl-tips 'please-ignore-this-line
83261a2f 691 "Get maybe newer version of this package from
4ab89e7b 692 http://ilyaz.org/software/emacs
db133cb6
RS
693Subdirectory `cperl-mode' may contain yet newer development releases and/or
694patches to related files.
f83d2997 695
5bd52f0e
RS
696For best results apply to an older Emacs the patches from
697 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
83261a2f 698\(this upgrades syntax-parsing abilities of Emacsen v19.34 and
8e3acc66 699v20.2 up to the level of Emacs v20.3 - a must for a good Perl
83261a2f 700mode.) As of beginning of 2003, XEmacs may provide a similar ability.
5bd52f0e 701
f83d2997
KH
702Get support packages choose-color.el (or font-lock-extra.el before
70319.30), imenu-go.el from the same place. \(Look for other files there
704too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
5c8b7eaf 705later you should use choose-color.el *instead* of font-lock-extra.el
f83d2997
KH
706\(and you will not get smart highlighting in C :-().
707
708Note that to enable Compile choices in the menu you need to install
709mode-compile.el.
710
5efe6a56
SM
711If your Emacs does not default to `cperl-mode' on Perl files, and you
712want it to: put the following into your .emacs file:
713
714 (defalias 'perl-mode 'cperl-mode)
715
a1506d29 716Get perl5-info from
4ab89e7b
SM
717 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
718Also, one can generate a newer documentation running `pod2texi' converter
719 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
f83d2997
KH
720
721If you use imenu-go, run imenu on perl5-info buffer (you can do it
5bd52f0e
RS
722from Perl menu). If many files are related, generate TAGS files from
723Tools/Tags submenu in Perl menu.
f83d2997
KH
724
725If some class structure is too complicated, use Tools/Hierarchy-view
029cb4d5 726from Perl menu, or hierarchic view of imenu. The second one uses the
f83d2997 727current buffer only, the first one requires generation of TAGS from
5bd52f0e
RS
728Perl/Tools/Tags menu beforehand.
729
730Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
731
732Switch auto-help on/off with Perl/Tools/Auto-help.
733
734Though with contemporary Emaxen CPerl mode should maintain the correct
735parsing of Perl even when editing, sometimes it may be lost. Fix this by
736
029cb4d5 737 \\[normal-mode]
f83d2997 738
5bd52f0e 739In cases of more severe confusion sometimes it is helpful to do
f83d2997 740
029cb4d5
SM
741 \\[load-library] cperl-mode RET
742 \\[normal-mode]
f83d2997 743
5bd52f0e
RS
744Before reporting (non-)problems look in the problem section of online
745micro-docs on what I know about CPerl problems.")
f83d2997
KH
746
747(defvar cperl-problems 'please-ignore-this-line
f94a632a
RS
748 "Description of problems in CPerl mode.
749Some faces will not be shown on some versions of Emacs unless you
bab27c0c 750install choose-color.el, available from
4ab89e7b 751 http://ilyaz.org/software/emacs
bab27c0c 752
6c389151 753`fill-paragraph' on a comment may leave the point behind the
4ab89e7b
SM
754paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
755to detect it and bulk out).
756
757See documentation of a variable `cperl-problems-old-emaxen' for the
758problems which disappear if you upgrade Emacs to a reasonably new
759version (20.3 for Emacs, and those of 2004 for XEmacs).")
760
761(defvar cperl-problems-old-emaxen 'please-ignore-this-line
762 "Description of problems in CPerl mode specific for older Emacs versions.
6c389151 763
8e3acc66 764Emacs had a _very_ restricted syntax parsing engine until version
5bd52f0e 76520.1. Most problems below are corrected starting from this version of
8e3acc66 766Emacs, and all of them should be fixed in version 20.3. (Or apply
83261a2f
SM
767patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
768this respect (until 2003).
5bd52f0e 769
6c389151
SM
770Note that even with newer Emacsen in some very rare cases the details
771of interaction of `font-lock' and syntaxification may be not cleaned
772up yet. You may get slightly different colors basing on the order of
773fontification and syntaxification. Say, the initial faces is correct,
774but editing the buffer breaks this.
f83d2997 775
db133cb6
RS
776Even with older Emacsen CPerl mode tries to corrects some Emacs
777misunderstandings, however, for efficiency reasons the degree of
778correction is different for different operations. The partially
779corrected problems are: POD sections, here-documents, regexps. The
780operations are: highlighting, indentation, electric keywords, electric
781braces.
f83d2997
KH
782
783This may be confusing, since the regexp s#//#/#\; may be highlighted
784as a comment, but it will be recognized as a regexp by the indentation
83261a2f 785code. Or the opposite case, when a POD section is highlighted, but
f83d2997
KH
786may break the indentation of the following code (though indentation
787should work if the balance of delimiters is not broken by POD).
788
789The main trick (to make $ a \"backslash\") makes constructions like
790${aaa} look like unbalanced braces. The only trick I can think of is
2e8b9c7d 791to insert it as $ {aaa} (valid in perl5, not in perl4).
f83d2997
KH
792
793Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
db133cb6
RS
794as /($|\\s)/. Note that such a transposition is not always possible.
795
5bd52f0e 796The solution is to upgrade your Emacs or patch an older one. Note
8e3acc66 797that Emacs 20.2 has some bugs related to `syntax-table' text
5bd52f0e
RS
798properties. Patches are available on the main CPerl download site,
799and on CPAN.
db133cb6
RS
800
801If these bugs cannot be fixed on your machine (say, you have an inferior
802environment and cannot recompile), you may still disable all the fancy stuff
83261a2f 803via `cperl-use-syntax-table-text-property'.")
f83d2997 804
f83d2997 805(defvar cperl-praise 'please-ignore-this-line
8e3acc66 806 "Advantages of CPerl mode.
f83d2997
KH
807
8080) It uses the newest `syntax-table' property ;-);
809
8101) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
5c8b7eaf 811mode - but the latter number may have improved too in last years) even
5bd52f0e
RS
812with old Emaxen which do not support `syntax-table' property.
813
814When using `syntax-table' property for syntax assist hints, it should
815handle 99.995% of lines correct - or somesuch. It automatically
816updates syntax assist hints when you edit your script.
f83d2997 817
bab27c0c 8182) It is generally believed to be \"the most user-friendly Emacs
f83d2997
KH
819package\" whatever it may mean (I doubt that the people who say similar
820things tried _all_ the rest of Emacs ;-), but this was not a lonely
821voice);
822
8233) Everything is customizable, one-by-one or in a big sweep;
824
8254) It has many easily-accessable \"tools\":
826 a) Can run program, check syntax, start debugger;
827 b) Can lineup vertically \"middles\" of rows, like `=' in
828 a = b;
829 cc = d;
830 c) Can insert spaces where this impoves readability (in one
831 interactive sweep over the buffer);
832 d) Has support for imenu, including:
833 1) Separate unordered list of \"interesting places\";
834 2) Separate TOC of POD sections;
835 3) Separate list of packages;
836 4) Hierarchical view of methods in (sub)packages;
837 5) and functions (by the full name - with package);
838 e) Has an interface to INFO docs for Perl; The interface is
839 very flexible, including shrink-wrapping of
840 documentation buffer/frame;
841 f) Has a builtin list of one-line explanations for perl constructs.
842 g) Can show these explanations if you stay long enough at the
843 corresponding place (or on demand);
844 h) Has an enhanced fontification (using 3 or 4 additional faces
845 comparing to font-lock - basically, different
846 namespaces in Perl have different colors);
847 i) Can construct TAGS basing on its knowledge of Perl syntax,
848 the standard menu has 6 different way to generate
db133cb6 849 TAGS (if \"by directory\", .xs files - with C-language
f83d2997
KH
850 bindings - are included in the scan);
851 j) Can build a hierarchical view of classes (via imenu) basing
852 on generated TAGS file;
853 k) Has electric parentheses, electric newlines, uses Abbrev
854 for electric logical constructs
855 while () {}
856 with different styles of expansion (context sensitive
857 to be not so bothering). Electric parentheses behave
858 \"as they should\" in a presence of a visible region.
859 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
db133cb6
RS
860 m) Can convert from
861 if (A) { B }
862 to
863 B if A;
f83d2997 864
5bd52f0e 865 n) Highlights (by user-choice) either 3-delimiters constructs
6c389151
SM
866 (such as tr/a/b/), or regular expressions and `y/tr';
867 o) Highlights trailing whitespace;
868 p) Is able to manipulate Perl Regular Expressions to ease
869 conversion to a more readable form.
4ab89e7b
SM
870 q) Can ispell POD sections and HERE-DOCs.
871 r) Understands comments and character classes inside regular
872 expressions; can find matching () and [] in a regular expression.
873 s) Allows indentation of //x-style regular expressions;
874 t) Highlights different symbols in regular expressions according
875 to their function; much less problems with backslashitis;
876 u) Allows to find regular expressions which contain interpolated parts.
5bd52f0e 877
f83d2997
KH
8785) The indentation engine was very smart, but most of tricks may be
879not needed anymore with the support for `syntax-table' property. Has
880progress indicator for indentation (with `imenu' loaded).
881
5c8b7eaf 8826) Indent-region improves inline-comments as well; also corrects
db133cb6 883whitespace *inside* the conditional/loop constructs.
f83d2997
KH
884
8857) Fill-paragraph correctly handles multi-line comments;
db133cb6
RS
886
8878) Can switch to different indentation styles by one command, and restore
888the settings present before the switch.
889
5c8b7eaf 8909) When doing indentation of control constructs, may correct
db133cb6 891line-breaks/spacing between elements of the construct.
029cb4d5
SM
892
89310) Uses a linear-time algorith for indentation of regions (on Emaxen with
4ab89e7b
SM
894capable syntax engines).
895
89611) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
897")
db133cb6
RS
898
899(defvar cperl-speed 'please-ignore-this-line
900 "This is an incomplete compendium of what is available in other parts
901of CPerl documentation. (Please inform me if I skept anything.)
902
903There is a perception that CPerl is slower than alternatives. This part
904of documentation is designed to overcome this misconception.
905
906*By default* CPerl tries to enable the most comfortable settings.
907From most points of view, correctly working package is infinitely more
908comfortable than a non-correctly working one, thus by default CPerl
909prefers correctness over speed. Below is the guide how to change
910settings if your preferences are different.
911
912A) Speed of loading the file. When loading file, CPerl may perform a
913scan which indicates places which cannot be parsed by primitive Emacs
914syntax-parsing routines, and marks them up so that either
915
916 A1) CPerl may work around these deficiencies (for big chunks, mostly
917 PODs and HERE-documents), or
918 A2) On capable Emaxen CPerl will use improved syntax-handlings
919 which reads mark-up hints directly.
920
921 The scan in case A2 is much more comprehensive, thus may be slower.
922
923 User can disable syntax-engine-helping scan of A2 by setting
924 `cperl-use-syntax-table-text-property'
925 variable to nil (if it is set to t).
926
927 One can disable the scan altogether (both A1 and A2) by setting
928 `cperl-pod-here-scan'
929 to nil.
930
5c8b7eaf 931B) Speed of editing operations.
db133cb6
RS
932
933 One can add a (minor) speedup to editing operations by setting
934 `cperl-use-syntax-table-text-property'
935 variable to nil (if it is set to t). This will disable
936 syntax-engine-helping scan, thus will make many more Perl
937 constructs be wrongly recognized by CPerl, thus may lead to
938 wrongly matched parentheses, wrong indentation, etc.
5bd52f0e
RS
939
940 One can unset `cperl-syntaxify-unwind'. This might speed up editing
83261a2f 941 of, say, long POD sections.")
f83d2997 942
5bd52f0e
RS
943(defvar cperl-tips-faces 'please-ignore-this-line
944 "CPerl mode uses following faces for highlighting:
945
4ab89e7b
SM
946 `cperl-array-face' Array names
947 `cperl-hash-face' Hash names
8661c643 948 `font-lock-comment-face' Comments, PODs and whatever is considered
5bd52f0e 949 syntaxically to be not code
8661c643 950 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
5bd52f0e 951 2-arg operators s/y/tr/ or of RExen,
4ab89e7b
SM
952 `font-lock-warning-face' Special-cased m// and s//foo/,
953 `font-lock-function-name-face' _ as a target of a file tests, file tests,
5bd52f0e
RS
954 subroutine names at the moment of definition
955 (except those conflicting with Perl operators),
956 package names (when recognized), format names
8661c643 957 `font-lock-keyword-face' Control flow switch constructs, declarators
4ab89e7b 958 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
8661c643 959 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
5bd52f0e
RS
960 literal parts and the terminator of formats
961 and whatever is syntaxically considered
962 as string literals
8661c643
DL
963 `font-lock-type-face' Overridable keywords
964 `font-lock-variable-name-face' Variable declarations, indirect array and
5bd52f0e 965 hash names, POD headers/item names
e4c067b5 966 `cperl-invalid' Trailing whitespace
5bd52f0e
RS
967
968Note that in several situations the highlighting tries to inform about
969possible confusion, such as different colors for function names in
970declarations depending on what they (do not) override, or special cases
971m// and s/// which do not do what one would expect them to do.
972
5c8b7eaf 973Help with best setup of these faces for printout requested (for each of
5bd52f0e
RS
974the faces: please specify bold, italic, underline, shadow and box.)
975
4ab89e7b
SM
976In regular expressions (except character classes):
977 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
978 `font-lock-constant-face': Delimiters
979 `font-lock-warning-face' Special-cased m// and s//foo/,
980 Mismatched closing delimiters, parens
981 we couldn't match, misplaced quantifiers,
982 unrecognized escape sequences
983 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
984 `font-lock-type-face' POSIX classes inside charclasses,
985 escape sequences with arguments (\x \23 \p \N)
986 and others match-a-char escape sequences
987 `font-lock-keyword-face' Capturing parens, and |
988 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
989 `font-lock-builtin-face' \"Remaining\" 0-length constructs, executable
990 parts of a REx, not-capturing parens
991 `font-lock-variable-name-face' Interpolated constructs, embedded code
992 `font-lock-comment-face' Embedded comments
993
994")
5bd52f0e 995
f83d2997
KH
996\f
997
998;;; Portability stuff:
999
1000(defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
b787fc05
GM
1001 `(define-key cperl-mode-map
1002 ,(if xemacs-key
1003 `(if cperl-xemacs-p ,xemacs-key ,emacs-key)
1004 emacs-key)
1005 ,definition))
f83d2997
KH
1006
1007(defvar cperl-del-back-ch
1008 (car (append (where-is-internal 'delete-backward-char)
1009 (where-is-internal 'backward-delete-char-untabify)))
029cb4d5 1010 "Character generated by key bound to `delete-backward-char'.")
f83d2997 1011
5c8b7eaf 1012(and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
f83d2997
KH
1013 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1014
db133cb6 1015(defun cperl-mark-active () (mark)) ; Avoid undefined warning
f83d2997
KH
1016(if cperl-xemacs-p
1017 (progn
1018 ;; "Active regions" are on: use region only if active
1019 ;; "Active regions" are off: use region unconditionally
1020 (defun cperl-use-region-p ()
db133cb6 1021 (if zmacs-regions (mark) t)))
f83d2997
KH
1022 (defun cperl-use-region-p ()
1023 (if transient-mark-mode mark-active t))
1024 (defun cperl-mark-active () mark-active))
1025
1026(defsubst cperl-enable-font-lock ()
83261a2f 1027 cperl-can-font-lock)
f83d2997 1028
83261a2f 1029(defun cperl-putback-char (c) ; Emacs 19
db133cb6
RS
1030 (set 'unread-command-events (list c))) ; Avoid undefined warning
1031
0ce7de92
RS
1032(if cperl-xemacs-p
1033 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1034 (setq unread-command-events (list (eval '(character-to-event c))))))
f83d2997
KH
1035
1036(or (fboundp 'uncomment-region)
1037 (defun uncomment-region (beg end)
1038 (interactive "r")
1039 (comment-region beg end -1)))
1040
1041(defvar cperl-do-not-fontify
1042 (if (string< emacs-version "19.30")
1043 'fontified
1044 'lazy-lock)
1045 "Text property which inhibits refontification.")
1046
5bd52f0e
RS
1047(defsubst cperl-put-do-not-fontify (from to &optional post)
1048 ;; If POST, do not do it with postponed fontification
1049 (if (and post cperl-syntaxify-by-font-lock)
1050 nil
f83d2997 1051 (put-text-property (max (point-min) (1- from))
5bd52f0e 1052 to cperl-do-not-fontify t)))
f83d2997 1053
ccc3ce39 1054(defcustom cperl-mode-hook nil
f94a632a 1055 "Hook run by CPerl mode."
ccc3ce39
SE
1056 :type 'hook
1057 :group 'cperl)
f83d2997 1058
db133cb6
RS
1059(defvar cperl-syntax-state nil)
1060(defvar cperl-syntax-done-to nil)
5bd52f0e 1061(defvar cperl-emacs-can-parse (> (length (save-excursion
ce22dd53 1062 (parse-partial-sexp (point) (point)))) 9))
db133cb6
RS
1063\f
1064;; Make customization possible "in reverse"
1065(defsubst cperl-val (symbol &optional default hairy)
1066 (cond
1067 ((eq (symbol-value symbol) 'null) default)
1068 (cperl-hairy (or hairy t))
1069 (t (symbol-value symbol))))
f83d2997 1070\f
4ab89e7b
SM
1071
1072(defun cperl-make-indent (column &optional minimum keep)
1073 "Makes indent of the current line the requested amount.
1074Unless KEEP, removes the old indentation. Works around a bug in ancient
1075versions of Emacs."
1076 (let ((prop (get-text-property (point) 'syntax-type)))
1077 (or keep
1078 (delete-horizontal-space))
1079 (indent-to column minimum)
1080 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1081 (and prop
1082 (> (current-column) 0)
1083 (save-excursion
1084 (beginning-of-line)
1085 (or (get-text-property (point) 'syntax-type)
1086 (and (looking-at "\\=[ \t]")
1087 (put-text-property (point) (match-end 0)
1088 'syntax-type prop)))))))
1089
f83d2997
KH
1090;;; Probably it is too late to set these guys already, but it can help later:
1091
5bd52f0e 1092;;;(and cperl-clobber-mode-lists
f83d2997
KH
1093;;;(setq auto-mode-alist
1094;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1095;;;(and (boundp 'interpreter-mode-alist)
1096;;; (setq interpreter-mode-alist (append interpreter-mode-alist
5bd52f0e 1097;;; '(("miniperl" . perl-mode))))))
80585273 1098(eval-when-compile
83261a2f
SM
1099 (mapcar (lambda (p)
1100 (condition-case nil
1101 (require p)
1102 (error nil)))
1103 '(imenu easymenu etags timer man info))
80585273
DL
1104 (if (fboundp 'ps-extend-face-list)
1105 (defmacro cperl-ps-extend-face-list (arg)
1106 `(ps-extend-face-list ,arg))
1107 (defmacro cperl-ps-extend-face-list (arg)
e8af40ee 1108 `(error "This version of Emacs has no `ps-extend-face-list'")))
80585273
DL
1109 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1110 ;; macros instead of defsubsts don't work on Emacs, so we do the
1111 ;; expansion manually. Any other suggestions?
1112 (require 'cl))
f83d2997
KH
1113
1114(defvar cperl-mode-abbrev-table nil
f94a632a 1115 "Abbrev table in use in CPerl mode buffers.")
f83d2997
KH
1116
1117(add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1118
1119(defvar cperl-mode-map () "Keymap used in CPerl mode.")
1120
1121(if cperl-mode-map nil
1122 (setq cperl-mode-map (make-sparse-keymap))
1123 (cperl-define-key "{" 'cperl-electric-lbrace)
1124 (cperl-define-key "[" 'cperl-electric-paren)
1125 (cperl-define-key "(" 'cperl-electric-paren)
1126 (cperl-define-key "<" 'cperl-electric-paren)
1127 (cperl-define-key "}" 'cperl-electric-brace)
1128 (cperl-define-key "]" 'cperl-electric-rparen)
1129 (cperl-define-key ")" 'cperl-electric-rparen)
1130 (cperl-define-key ";" 'cperl-electric-semi)
1131 (cperl-define-key ":" 'cperl-electric-terminator)
1132 (cperl-define-key "\C-j" 'newline-and-indent)
1133 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
db133cb6 1134 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
f83d2997
KH
1135 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1136 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
db133cb6
RS
1137 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1138 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
f83d2997 1139 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
4ab89e7b
SM
1140 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1141 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1142 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1143 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1144 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1145 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1146 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
db133cb6 1147 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
4ab89e7b
SM
1148 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1149 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
f83d2997
KH
1150 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1151 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1152 [(control meta |)])
1153 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1154 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1155 (cperl-define-key "\177" 'cperl-electric-backspace)
1156 (cperl-define-key "\t" 'cperl-indent-command)
1157 ;; don't clobber the backspace binding:
db133cb6
RS
1158 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1159 [(control c) (control h) F])
db133cb6
RS
1160 (if (cperl-val 'cperl-clobber-lisp-bindings)
1161 (progn
1162 (cperl-define-key "\C-hf"
1163 ;;(concat (char-to-string help-char) "f") ; does not work
1164 'cperl-info-on-command
1165 [(control h) f])
1166 (cperl-define-key "\C-hv"
1167 ;;(concat (char-to-string help-char) "v") ; does not work
1168 'cperl-get-help
5bd52f0e
RS
1169 [(control h) v])
1170 (cperl-define-key "\C-c\C-hf"
1171 ;;(concat (char-to-string help-char) "f") ; does not work
1172 (key-binding "\C-hf")
1173 [(control c) (control h) f])
1174 (cperl-define-key "\C-c\C-hv"
1175 ;;(concat (char-to-string help-char) "v") ; does not work
1176 (key-binding "\C-hv")
1177 [(control c) (control h) v]))
1178 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1179 [(control c) (control h) f])
1180 (cperl-define-key "\C-c\C-hv"
1181 ;;(concat (char-to-string help-char) "v") ; does not work
1182 'cperl-get-help
1183 [(control c) (control h) v]))
5c8b7eaf 1184 (if (and cperl-xemacs-p
f83d2997
KH
1185 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1186 (progn
1187 ;; substitute-key-definition is usefulness-deenhanced...
4ab89e7b 1188 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
f83d2997
KH
1189 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1190 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
4ab89e7b
SM
1191 (or (boundp 'fill-paragraph-function)
1192 (substitute-key-definition
1193 'fill-paragraph 'cperl-fill-paragraph
1194 cperl-mode-map global-map))
f83d2997
KH
1195 (substitute-key-definition
1196 'indent-sexp 'cperl-indent-exp
1197 cperl-mode-map global-map)
f83d2997
KH
1198 (substitute-key-definition
1199 'indent-region 'cperl-indent-region
1200 cperl-mode-map global-map)
1201 (substitute-key-definition
1202 'indent-for-comment 'cperl-indent-for-comment
1203 cperl-mode-map global-map)))
1204
1205(defvar cperl-menu)
db133cb6
RS
1206(defvar cperl-lazy-installed)
1207(defvar cperl-old-style nil)
f83d2997
KH
1208(condition-case nil
1209 (progn
1210 (require 'easymenu)
83261a2f 1211 (easy-menu-define
4ab89e7b
SM
1212 cperl-menu cperl-mode-map "Menu for CPerl mode"
1213 '("Perl"
1214 ["Beginning of function" beginning-of-defun t]
1215 ["End of function" end-of-defun t]
1216 ["Mark function" mark-defun t]
1217 ["Indent expression" cperl-indent-exp t]
82eb0dae 1218 ["Fill paragraph/comment" fill-paragraph t]
4ab89e7b
SM
1219 "----"
1220 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1221 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1222 ("Regexp"
1223 ["Beautify" cperl-beautify-regexp
1224 cperl-use-syntax-table-text-property]
1225 ["Beautify one level deep" (cperl-beautify-regexp 1)
1226 cperl-use-syntax-table-text-property]
1227 ["Beautify a group" cperl-beautify-level
1228 cperl-use-syntax-table-text-property]
1229 ["Beautify a group one level deep" (cperl-beautify-level 1)
1230 cperl-use-syntax-table-text-property]
1231 ["Contract a group" cperl-contract-level
1232 cperl-use-syntax-table-text-property]
1233 ["Contract groups" cperl-contract-levels
1234 cperl-use-syntax-table-text-property]
83261a2f 1235 "----"
4ab89e7b
SM
1236 ["Find next interpolated" cperl-next-interpolated-REx
1237 (next-single-property-change (point-min) 'REx-interpolated)]
1238 ["Find next interpolated (no //o)"
1239 cperl-next-interpolated-REx-0
1240 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1241 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1242 ["Find next interpolated (neither //o nor whole-REx)"
1243 cperl-next-interpolated-REx-1
1244 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1245 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1246 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1247 "----"
1248 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1249 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1250 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1251 "----"
1252 ["Run" mode-compile (fboundp 'mode-compile)]
1253 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1254 (get-buffer "*compilation*"))]
1255 ["Next error" next-error (get-buffer "*compilation*")]
1256 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1257 "----"
1258 ["Debugger" cperl-db t]
1259 "----"
1260 ("Tools"
1261 ["Imenu" imenu (fboundp 'imenu)]
1262 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
83261a2f 1263 "----"
4ab89e7b
SM
1264 ["Ispell PODs" cperl-pod-spell
1265 ;; Better not to update syntaxification here:
1266 ;; debugging syntaxificatio can be broken by this???
1267 (or
1268 (get-text-property (point-min) 'in-pod)
1269 (< (progn
1270 (and cperl-syntaxify-for-menu
1271 (cperl-update-syntaxification (point-max) (point-max)))
1272 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1273 (point-max)))]
1274 ["Ispell HERE-DOCs" cperl-here-doc-spell
1275 (< (progn
1276 (and cperl-syntaxify-for-menu
1277 (cperl-update-syntaxification (point-max) (point-max)))
1278 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1279 (point-max))]
1280 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1281 (eq 'here-doc (progn
1282 (and cperl-syntaxify-for-menu
1283 (cperl-update-syntaxification (point) (point)))
1284 (get-text-property (point) 'syntax-type)))]
1285 ["Select this HERE-DOC or POD section"
1286 cperl-select-this-pod-or-here-doc
1287 (memq (progn
1288 (and cperl-syntaxify-for-menu
1289 (cperl-update-syntaxification (point) (point)))
1290 (get-text-property (point) 'syntax-type))
1291 '(here-doc pod))]
83261a2f 1292 "----"
4ab89e7b
SM
1293 ["CPerl pretty print (exprmntl)" cperl-ps-print
1294 (fboundp 'ps-extend-face-list)]
83261a2f 1295 "----"
4ab89e7b
SM
1296 ["Syntaxify region" cperl-find-pods-heres-region
1297 (cperl-use-region-p)]
1298 ["Profile syntaxification" cperl-time-fontification t]
1299 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1300 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1301 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1302 (cperl-toggle-set-debug-unwind nil t) t]
83261a2f 1303 "----"
4ab89e7b
SM
1304 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1305 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1306 ("Tags"
f83d2997
KH
1307;;; ["Create tags for current file" cperl-etags t]
1308;;; ["Add tags for current file" (cperl-etags t) t]
1309;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1310;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
5c8b7eaf 1311;;; ["Create tags for Perl files in (sub)directories"
f83d2997
KH
1312;;; (cperl-etags nil 'recursive) t]
1313;;; ["Add tags for Perl files in (sub)directories"
5c8b7eaf 1314;;; (cperl-etags t 'recursive) t])
f83d2997 1315;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
f739b53b
SM
1316 ["Create tags for current file" (cperl-write-tags nil t) t]
1317 ["Add tags for current file" (cperl-write-tags) t]
1318 ["Create tags for Perl files in directory"
1319 (cperl-write-tags nil t nil t) t]
1320 ["Add tags for Perl files in directory"
1321 (cperl-write-tags nil nil nil t) t]
1322 ["Create tags for Perl files in (sub)directories"
1323 (cperl-write-tags nil t t t) t]
1324 ["Add tags for Perl files in (sub)directories"
1325 (cperl-write-tags nil nil t t) t]))
1326 ("Perl docs"
15ca5699 1327 ["Define word at point" imenu-go-find-at-position
f739b53b
SM
1328 (fboundp 'imenu-go-find-at-position)]
1329 ["Help on function" cperl-info-on-command t]
1330 ["Help on function at point" cperl-info-on-current-command t]
1331 ["Help on symbol at point" cperl-get-help t]
1332 ["Perldoc" cperl-perldoc t]
1333 ["Perldoc on word at point" cperl-perldoc-at-point t]
1334 ["View manpage of POD in this file" cperl-build-manpage t]
15ca5699 1335 ["Auto-help on" cperl-lazy-install
f739b53b
SM
1336 (and (fboundp 'run-with-idle-timer)
1337 (not cperl-lazy-installed))]
1338 ["Auto-help off" cperl-lazy-unstall
1339 (and (fboundp 'run-with-idle-timer)
1340 cperl-lazy-installed)])
1341 ("Toggle..."
1342 ["Auto newline" cperl-toggle-auto-newline t]
1343 ["Electric parens" cperl-toggle-electric t]
1344 ["Electric keywords" cperl-toggle-abbrev t]
1345 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1346 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
15ca5699 1347 ["Auto fill" auto-fill-mode t])
f739b53b
SM
1348 ("Indent styles..."
1349 ["CPerl" (cperl-set-style "CPerl") t]
1350 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1351 ["GNU" (cperl-set-style "GNU") t]
1352 ["C++" (cperl-set-style "C++") t]
4ab89e7b 1353 ["K&R" (cperl-set-style "K&R") t]
f739b53b
SM
1354 ["BSD" (cperl-set-style "BSD") t]
1355 ["Whitesmith" (cperl-set-style "Whitesmith") t]
4ab89e7b 1356 ["Memorize Current" (cperl-set-style "Current") t]
f739b53b
SM
1357 ["Memorized" (cperl-set-style-back) cperl-old-style])
1358 ("Micro-docs"
1359 ["Tips" (describe-variable 'cperl-tips) t]
1360 ["Problems" (describe-variable 'cperl-problems) t]
1361 ["Speed" (describe-variable 'cperl-speed) t]
1362 ["Praise" (describe-variable 'cperl-praise) t]
1363 ["Faces" (describe-variable 'cperl-tips-faces) t]
1364 ["CPerl mode" (describe-function 'cperl-mode) t]
1365 ["CPerl version"
1366 (message "The version of master-file for this CPerl is %s-Emacs"
1367 cperl-version) t]))))
f83d2997
KH
1368 (error nil))
1369
1370(autoload 'c-macro-expand "cmacexp"
1371 "Display the result of expanding all C macros occurring in the region.
1372The expansion is entirely correct because it uses the C preprocessor."
1373 t)
1374
4ab89e7b
SM
1375;;; These two must be unwound, otherwise take exponential time
1376(defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1377"Regular expression to match optional whitespace with interpspersed comments.
1378Should contain exactly one group.")
1379
1380;;; This one is tricky to unwind; still very inefficient...
1381(defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1382"Regular expression to match whitespace with interpspersed comments.
1383Should contain exactly one group.")
1384
1385
1386;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1387;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1388;;; Details of groups in this may be used in several functions; see comments
1389;;; near mentioned above variable(s)...
1390;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1391(defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1392 "Match the text after `sub' in a subroutine declaration.
1393If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1394of attributes (if present), or end of the name or prototype (whatever is
1395the last)."
1396 (concat ; Assume n groups before this...
1397 "\\(" ; n+1=name-group
1398 cperl-white-and-comment-rex ; n+2=pre-name
1399 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1400 "\\)" ; END n+1=name-group
1401 (if named "" "?")
1402 "\\(" ; n+4=proto-group
1403 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1404 "\\(([^()]*)\\)" ; n+6=prototype
1405 "\\)?" ; END n+4=proto-group
1406 "\\(" ; n+7=attr-group
1407 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1408 "\\(" ; n+9=start-attr
1409 ":"
1410 (if attr (concat
1411 "\\("
1412 cperl-maybe-white-and-comment-rex ; whitespace-comments
1413 "\\(\\sw\\|_\\)+" ; attr-name
1414 ;; attr-arg (1 level of internal parens allowed!)
1415 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1416 "\\(" ; optional : (XXX allows trailing???)
1417 cperl-maybe-white-and-comment-rex ; whitespace-comments
1418 ":\\)?"
1419 "\\)+")
1420 "[^:]")
1421 "\\)"
1422 "\\)?" ; END n+6=proto-group
1423 ))
1424
1425;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1426;;; and `cperl-outline-level'.
1427;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
95f433f4
RS
1428(defvar cperl-imenu--function-name-regexp-perl
1429 (concat
4ab89e7b
SM
1430 "^\\(" ; 1 = all
1431 "\\([ \t]*package" ; 2 = package-group
1432 "\\(" ; 3 = package-name-group
1433 cperl-white-and-comment-rex ; 4 = pre-package-name
1434 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1435 "\\|"
1436 "[ \t]*sub"
1437 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1438 cperl-maybe-white-and-comment-rex ; 15=pre-block
1439 "\\|"
1440 "=head\\([1-4]\\)[ \t]+" ; 16=level
1441 "\\([^\n]+\\)$" ; 17=text
95f433f4
RS
1442 "\\)"))
1443
8dd511f6
RS
1444(defvar cperl-outline-regexp
1445 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1446
f83d2997 1447(defvar cperl-mode-syntax-table nil
f94a632a 1448 "Syntax table in use in CPerl mode buffers.")
f83d2997
KH
1449
1450(defvar cperl-string-syntax-table nil
f94a632a 1451 "Syntax table in use in CPerl mode string-like chunks.")
f83d2997 1452
4ab89e7b
SM
1453(defsubst cperl-1- (p)
1454 (max (point-min) (1- p)))
1455
1456(defsubst cperl-1+ (p)
1457 (min (point-max) (1+ p)))
1458
f83d2997
KH
1459(if cperl-mode-syntax-table
1460 ()
1461 (setq cperl-mode-syntax-table (make-syntax-table))
1462 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1463 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1464 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1465 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1466 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1467 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1468 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1469 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1470 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1471 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1472 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1473 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1474 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1475 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1476 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1477 (if cperl-under-as-char
1478 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1479 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1480 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1481 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1482 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
4ab89e7b
SM
1483 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1484 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
83261a2f 1485 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
f83d2997
KH
1486
1487
1488\f
db133cb6 1489(defvar cperl-faces-init nil)
f83d2997
KH
1490;; Fix for msb.el
1491(defvar cperl-msb-fixed nil)
83261a2f 1492(defvar cperl-use-major-mode 'cperl-mode)
4ab89e7b
SM
1493(defvar cperl-font-lock-multiline-start nil)
1494(defvar cperl-font-lock-multiline nil)
1495(defvar cperl-compilation-error-regexp-alist nil)
1496(defvar cperl-font-locking nil)
83261a2f 1497
f83d2997
KH
1498;;;###autoload
1499(defun cperl-mode ()
1500 "Major mode for editing Perl code.
1501Expression and list commands understand all C brackets.
1502Tab indents for Perl code.
1503Paragraphs are separated by blank lines only.
1504Delete converts tabs to spaces as it moves back.
1505
1506Various characters in Perl almost always come in pairs: {}, (), [],
1507sometimes <>. When the user types the first, she gets the second as
1508well, with optional special formatting done on {}. (Disabled by
1509default.) You can always quote (with \\[quoted-insert]) the left
1510\"paren\" to avoid the expansion. The processing of < is special,
f94a632a 1511since most the time you mean \"less\". CPerl mode tries to guess
f83d2997
KH
1512whether you want to type pair <>, and inserts is if it
1513appropriate. You can set `cperl-electric-parens-string' to the string that
1514contains the parenths from the above list you want to be electrical.
1515Electricity of parenths is controlled by `cperl-electric-parens'.
1516You may also set `cperl-electric-parens-mark' to have electric parens
1517look for active mark and \"embrace\" a region if possible.'
1518
1519CPerl mode provides expansion of the Perl control constructs:
db133cb6 1520
5c8b7eaf 1521 if, else, elsif, unless, while, until, continue, do,
db133cb6
RS
1522 for, foreach, formy and foreachmy.
1523
1524and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1525
1526The user types the keyword immediately followed by a space, which
1527causes the construct to be expanded, and the point is positioned where
1528she is most likely to want to be. eg. when the user types a space
1529following \"if\" the following appears in the buffer: if () { or if ()
1530} { } and the cursor is between the parentheses. The user can then
1531type some boolean expression within the parens. Having done that,
1532typing \\[cperl-linefeed] places you - appropriately indented - on a
1533new line between the braces (if you typed \\[cperl-linefeed] in a POD
5c8b7eaf 1534directive line, then appropriate number of new lines is inserted).
db133cb6
RS
1535
1536If CPerl decides that you want to insert \"English\" style construct like
1537
f83d2997 1538 bite if angry;
db133cb6
RS
1539
1540it will not do any expansion. See also help on variable
1541`cperl-extra-newline-before-brace'. (Note that one can switch the
1542help message on expansion by setting `cperl-message-electric-keyword'
1543to nil.)
f83d2997
KH
1544
1545\\[cperl-linefeed] is a convenience replacement for typing carriage
1546return. It places you in the next line with proper indentation, or if
1547you type it inside the inline block of control construct, like
db133cb6 1548
f83d2997 1549 foreach (@lines) {print; print}
db133cb6 1550
f83d2997
KH
1551and you are on a boundary of a statement inside braces, it will
1552transform the construct into a multiline and will place you into an
5c8b7eaf 1553appropriately indented blank line. If you need a usual
6292d528 1554`newline-and-indent' behavior, it is on \\[newline-and-indent],
f83d2997
KH
1555see documentation on `cperl-electric-linefeed'.
1556
db133cb6
RS
1557Use \\[cperl-invert-if-unless] to change a construction of the form
1558
1559 if (A) { B }
1560
1561into
1562
1563 B if A;
1564
f83d2997
KH
1565\\{cperl-mode-map}
1566
db133cb6
RS
1567Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1568\(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1569on electric space between $ and {, `cperl-electric-parens-string' is
1570the string that contains parentheses that should be electric in CPerl
1571\(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
f83d2997
KH
1572setting `cperl-electric-keywords' enables electric expansion of
1573control structures in CPerl. `cperl-electric-linefeed' governs which
1574one of two linefeed behavior is preferable. You can enable all these
1575options simultaneously (recommended mode of use) by setting
1576`cperl-hairy' to t. In this case you can switch separate options off
db133cb6
RS
1577by setting them to `null'. Note that one may undo the extra
1578whitespace inserted by semis and braces in `auto-newline'-mode by
1579consequent \\[cperl-electric-backspace].
f83d2997
KH
1580
1581If your site has perl5 documentation in info format, you can use commands
1582\\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1583These keys run commands `cperl-info-on-current-command' and
1584`cperl-info-on-command', which one is which is controlled by variable
5c8b7eaf 1585`cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
db133cb6 1586\(in turn affected by `cperl-hairy').
f83d2997
KH
1587
1588Even if you have no info-format documentation, short one-liner-style
db133cb6
RS
1589help is available on \\[cperl-get-help], and one can run perldoc or
1590man via menu.
f83d2997 1591
db133cb6
RS
1592It is possible to show this help automatically after some idle time.
1593This is regulated by variable `cperl-lazy-help-time'. Default with
1594`cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1595secs idle time . It is also possible to switch this on/off from the
1596menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
f83d2997
KH
1597
1598Use \\[cperl-lineup] to vertically lineup some construction - put the
1599beginning of the region at the start of construction, and make region
1600span the needed amount of lines.
1601
1602Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
83261a2f 1603`cperl-pod-face', `cperl-pod-head-face' control processing of POD and
db133cb6
RS
1604here-docs sections. With capable Emaxen results of scan are used
1605for indentation too, otherwise they are used for highlighting only.
f83d2997
KH
1606
1607Variables controlling indentation style:
1608 `cperl-tab-always-indent'
1609 Non-nil means TAB in CPerl mode should always reindent the current line,
1610 regardless of where in the line point is when the TAB command is used.
db133cb6
RS
1611 `cperl-indent-left-aligned-comments'
1612 Non-nil means that the comment starting in leftmost column should indent.
f83d2997
KH
1613 `cperl-auto-newline'
1614 Non-nil means automatically newline before and after braces,
1615 and after colons and semicolons, inserted in Perl code. The following
1616 \\[cperl-electric-backspace] will remove the inserted whitespace.
5c8b7eaf
SS
1617 Insertion after colons requires both this variable and
1618 `cperl-auto-newline-after-colon' set.
f83d2997
KH
1619 `cperl-auto-newline-after-colon'
1620 Non-nil means automatically newline even after colons.
1621 Subject to `cperl-auto-newline' setting.
1622 `cperl-indent-level'
1623 Indentation of Perl statements within surrounding block.
1624 The surrounding block's indentation is the indentation
1625 of the line on which the open-brace appears.
1626 `cperl-continued-statement-offset'
1627 Extra indentation given to a substatement, such as the
1628 then-clause of an if, or body of a while, or just a statement continuation.
1629 `cperl-continued-brace-offset'
1630 Extra indentation given to a brace that starts a substatement.
1631 This is in addition to `cperl-continued-statement-offset'.
1632 `cperl-brace-offset'
1633 Extra indentation for line if it starts with an open brace.
1634 `cperl-brace-imaginary-offset'
1635 An open brace following other text is treated as if it the line started
1636 this far to the right of the actual line indentation.
1637 `cperl-label-offset'
1638 Extra indentation for line that is a label.
1639 `cperl-min-label-indent'
1640 Minimal indentation for line that is a label.
1641
4ab89e7b
SM
1642Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1643 `cperl-indent-level' 5 4 2 4
1644 `cperl-brace-offset' 0 0 0 0
1645 `cperl-continued-brace-offset' -5 -4 0 0
1646 `cperl-label-offset' -5 -4 -2 -4
1647 `cperl-continued-statement-offset' 5 4 2 4
f83d2997 1648
db133cb6
RS
1649CPerl knows several indentation styles, and may bulk set the
1650corresponding variables. Use \\[cperl-set-style] to do this. Use
1651\\[cperl-set-style-back] to restore the memorized preexisting values
4ab89e7b
SM
1652\(both available from menu). See examples in `cperl-style-examples'.
1653
1654Part of the indentation style is how different parts of if/elsif/else
1655statements are broken into lines; in CPerl, this is reflected on how
1656templates for these constructs are created (controlled by
1657`cperl-extra-newline-before-brace'), and how reflow-logic should treat \"continuation\" blocks of else/elsif/continue, controlled by the same variable,
1658and by `cperl-extra-newline-before-brace-multiline',
1659`cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
db133cb6
RS
1660
1661If `cperl-indent-level' is 0, the statement after opening brace in
5c8b7eaf 1662column 0 is indented on
db133cb6 1663`cperl-brace-offset'+`cperl-continued-statement-offset'.
f83d2997
KH
1664
1665Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
db133cb6
RS
1666with no args.
1667
1668DO NOT FORGET to read micro-docs (available from `Perl' menu)
1669or as help on variables `cperl-tips', `cperl-problems',
f94a632a 1670`cperl-praise', `cperl-speed'."
f83d2997
KH
1671 (interactive)
1672 (kill-all-local-variables)
f83d2997
KH
1673 (use-local-map cperl-mode-map)
1674 (if (cperl-val 'cperl-electric-linefeed)
1675 (progn
1676 (local-set-key "\C-J" 'cperl-linefeed)
1677 (local-set-key "\C-C\C-J" 'newline-and-indent)))
db133cb6
RS
1678 (if (and
1679 (cperl-val 'cperl-clobber-lisp-bindings)
1680 (cperl-val 'cperl-info-on-command-no-prompt))
f83d2997
KH
1681 (progn
1682 ;; don't clobber the backspace binding:
1683 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1684 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1685 [(control c) (control h) f])))
83261a2f 1686 (setq major-mode cperl-use-major-mode)
f83d2997
KH
1687 (setq mode-name "CPerl")
1688 (if (not cperl-mode-abbrev-table)
1689 (let ((prev-a-c abbrevs-changed))
1690 (define-abbrev-table 'cperl-mode-abbrev-table '(
1691 ("if" "if" cperl-electric-keyword 0)
1692 ("elsif" "elsif" cperl-electric-keyword 0)
1693 ("while" "while" cperl-electric-keyword 0)
1694 ("until" "until" cperl-electric-keyword 0)
1695 ("unless" "unless" cperl-electric-keyword 0)
1696 ("else" "else" cperl-electric-else 0)
db133cb6 1697 ("continue" "continue" cperl-electric-else 0)
f83d2997
KH
1698 ("for" "for" cperl-electric-keyword 0)
1699 ("foreach" "foreach" cperl-electric-keyword 0)
db133cb6
RS
1700 ("formy" "formy" cperl-electric-keyword 0)
1701 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1702 ("do" "do" cperl-electric-keyword 0)
6c389151
SM
1703 ("=pod" "=pod" cperl-electric-pod 0)
1704 ("=over" "=over" cperl-electric-pod 0)
1705 ("=head1" "=head1" cperl-electric-pod 0)
1706 ("=head2" "=head2" cperl-electric-pod 0)
db133cb6
RS
1707 ("pod" "pod" cperl-electric-pod 0)
1708 ("over" "over" cperl-electric-pod 0)
1709 ("head1" "head1" cperl-electric-pod 0)
1710 ("head2" "head2" cperl-electric-pod 0)))
f83d2997
KH
1711 (setq abbrevs-changed prev-a-c)))
1712 (setq local-abbrev-table cperl-mode-abbrev-table)
4ab89e7b
SM
1713 (if (cperl-val 'cperl-electric-keywords)
1714 (abbrev-mode 1))
f83d2997 1715 (set-syntax-table cperl-mode-syntax-table)
4ab89e7b
SM
1716 ;; Until Emacs is multi-threaded, we do not actually need it local:
1717 (make-local-variable 'cperl-font-lock-multiline-start)
1718 (make-local-variable 'cperl-font-locking)
6c389151
SM
1719 (make-local-variable 'outline-regexp)
1720 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1721 (setq outline-regexp cperl-outline-regexp)
1722 (make-local-variable 'outline-level)
1723 (setq outline-level 'cperl-outline-level)
f83d2997
KH
1724 (make-local-variable 'paragraph-start)
1725 (setq paragraph-start (concat "^$\\|" page-delimiter))
1726 (make-local-variable 'paragraph-separate)
1727 (setq paragraph-separate paragraph-start)
1728 (make-local-variable 'paragraph-ignore-fill-prefix)
1729 (setq paragraph-ignore-fill-prefix t)
4ab89e7b
SM
1730 (if cperl-xemacs-p
1731 (progn
1732 (make-local-variable 'paren-backwards-message)
1733 (set 'paren-backwards-message t)))
f83d2997
KH
1734 (make-local-variable 'indent-line-function)
1735 (setq indent-line-function 'cperl-indent-line)
1736 (make-local-variable 'require-final-newline)
7d441781 1737 (setq require-final-newline mode-require-final-newline)
f83d2997
KH
1738 (make-local-variable 'comment-start)
1739 (setq comment-start "# ")
1740 (make-local-variable 'comment-end)
1741 (setq comment-end "")
1742 (make-local-variable 'comment-column)
1743 (setq comment-column cperl-comment-column)
1744 (make-local-variable 'comment-start-skip)
1745 (setq comment-start-skip "#+ *")
1746 (make-local-variable 'defun-prompt-regexp)
4ab89e7b
SM
1747;;; "[ \t]*sub"
1748;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1749;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1750 (setq defun-prompt-regexp
1751 (concat "^[ \t]*\\(sub"
1752 (cperl-after-sub-regexp 'named 'attr-groups)
1753 "\\|" ; per toke.c
1754 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1755 "\\)"
1756 cperl-maybe-white-and-comment-rex))
f83d2997
KH
1757 (make-local-variable 'comment-indent-function)
1758 (setq comment-indent-function 'cperl-comment-indent)
4ab89e7b
SM
1759 (and (boundp 'fill-paragraph-function)
1760 (progn
1761 (make-local-variable 'fill-paragraph-function)
1762 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
f83d2997
KH
1763 (make-local-variable 'parse-sexp-ignore-comments)
1764 (setq parse-sexp-ignore-comments t)
1765 (make-local-variable 'indent-region-function)
1766 (setq indent-region-function 'cperl-indent-region)
1767 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1768 (make-local-variable 'imenu-create-index-function)
1769 (setq imenu-create-index-function
80585273 1770 (function cperl-imenu--create-perl-index))
f83d2997
KH
1771 (make-local-variable 'imenu-sort-function)
1772 (setq imenu-sort-function nil)
e1a5828f
AS
1773 (make-local-variable 'vc-rcs-header)
1774 (set 'vc-rcs-header cperl-vc-rcs-header)
1775 (make-local-variable 'vc-sccs-header)
1776 (set 'vc-sccs-header cperl-vc-sccs-header)
4ab89e7b
SM
1777 ;; This one is obsolete...
1778 (make-local-variable 'vc-header-alist)
1779 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1780 (` ((SCCS (, (car cperl-vc-sccs-header)))
1781 (RCS (, (car cperl-vc-rcs-header)))))))
1782 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1783 (make-local-variable 'compilation-error-regexp-alist-alist)
1784 (set 'compilation-error-regexp-alist-alist
1785 (cons (cons 'cperl cperl-compilation-error-regexp-alist)
1786 (symbol-value 'compilation-error-regexp-alist-alist)))
1787 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1788 (let ((f 'compilation-build-compilation-error-regexp-alist))
1789 (funcall f))
1790 (push 'cperl compilation-error-regexp-alist)))
1791 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1792 (make-local-variable 'compilation-error-regexp-alist)
1793 (set 'compilation-error-regexp-alist
1794 (cons cperl-compilation-error-regexp-alist
1795 (symbol-value 'compilation-error-regexp-alist)))))
f83d2997
KH
1796 (make-local-variable 'font-lock-defaults)
1797 (setq font-lock-defaults
db133cb6
RS
1798 (cond
1799 ((string< emacs-version "19.30")
4ab89e7b 1800 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
db133cb6 1801 ((string< emacs-version "19.33") ; Which one to use?
5efe6a56
SM
1802 '((cperl-font-lock-keywords
1803 cperl-font-lock-keywords-1
4ab89e7b 1804 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
db133cb6
RS
1805 (t
1806 '((cperl-load-font-lock-keywords
1807 cperl-load-font-lock-keywords-1
4ab89e7b 1808 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
db133cb6 1809 (make-local-variable 'cperl-syntax-state)
4ab89e7b 1810 (setq cperl-syntax-state nil) ; reset syntaxification cache
f83d2997
KH
1811 (if cperl-use-syntax-table-text-property
1812 (progn
029cb4d5 1813 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997 1814 ;; Do not introduce variable if not needed, we check it!
db133cb6
RS
1815 (set 'parse-sexp-lookup-properties t)
1816 ;; Fix broken font-lock:
1817 (or (boundp 'font-lock-unfontify-region-function)
1818 (set 'font-lock-unfontify-region-function
83261a2f 1819 'font-lock-default-unfontify-region))
4ab89e7b
SM
1820 (unless cperl-xemacs-p ; Our: just a plug for wrong font-lock
1821 (make-local-variable 'font-lock-unfontify-region-function)
1822 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1823 'cperl-font-lock-unfontify-region-function))
029cb4d5 1824 (make-local-variable 'cperl-syntax-done-to)
4ab89e7b 1825 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
029cb4d5 1826 (make-local-variable 'font-lock-syntactic-keywords)
5c8b7eaf 1827 (setq font-lock-syntactic-keywords
db133cb6 1828 (if cperl-syntaxify-by-font-lock
11b41e6f
SM
1829 '((cperl-fontify-syntaxically))
1830 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1831 ;; used to ignore syntax-table text-properties. (t) is a hack
1832 ;; to make font-lock think that font-lock-syntactic-keywords
1833 ;; are defined.
db133cb6 1834 '(t)))))
4ab89e7b
SM
1835 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1836 (progn
1837 (setq cperl-font-lock-multiline t) ; Not localized...
f453f5a8 1838 (set (make-local-variable 'font-lock-multiline) t))
4ab89e7b
SM
1839 (make-local-variable 'font-lock-fontify-region-function)
1840 (set 'font-lock-fontify-region-function ; not present with old Emacs
1841 'cperl-font-lock-fontify-region-function))
1842 (make-local-variable 'font-lock-fontify-region-function)
1843 (set 'font-lock-fontify-region-function ; not present with old Emacs
1844 'cperl-font-lock-fontify-region-function)
db133cb6 1845 (make-local-variable 'cperl-old-style)
83261a2f
SM
1846 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1847 (set (make-local-variable 'normal-auto-fill-function)
4ab89e7b 1848 'cperl-do-auto-fill)
83261a2f
SM
1849 (or (fboundp 'cperl-old-auto-fill-mode)
1850 (progn
1851 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1852 (defun auto-fill-mode (&optional arg)
1853 (interactive "P")
1854 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1855 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1856 (setq auto-fill-function 'cperl-do-auto-fill))))))
f83d2997 1857 (if (cperl-enable-font-lock)
5c8b7eaf 1858 (if (cperl-val 'cperl-font-lock)
f83d2997
KH
1859 (progn (or cperl-faces-init (cperl-init-faces))
1860 (font-lock-mode 1))))
4ab89e7b
SM
1861 (set (make-local-variable 'facemenu-add-face-function)
1862 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
f83d2997
KH
1863 (and (boundp 'msb-menu-cond)
1864 (not cperl-msb-fixed)
1865 (cperl-msb-fix))
1866 (if (featurep 'easymenu)
46c72468 1867 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
a3c328ee 1868 (run-mode-hooks 'cperl-mode-hook)
4ab89e7b
SM
1869 (if cperl-hook-after-change
1870 (progn
1871 (make-local-hook 'after-change-functions)
1872 (add-hook 'after-change-functions 'cperl-after-change-function nil t)))
f83d2997 1873 ;; After hooks since fontification will break this
5c8b7eaf 1874 (if cperl-pod-here-scan
83261a2f 1875 (or cperl-syntaxify-by-font-lock
5bd52f0e
RS
1876 (progn (or cperl-faces-init (cperl-init-faces-weak))
1877 (cperl-find-pods-heres)))))
f83d2997
KH
1878\f
1879;; Fix for perldb - make default reasonable
1880(defun cperl-db ()
1881 (interactive)
1882 (require 'gud)
1883 (perldb (read-from-minibuffer "Run perldb (like this): "
1884 (if (consp gud-perldb-history)
1885 (car gud-perldb-history)
1886 (concat "perl " ;;(file-name-nondirectory
83261a2f
SM
1887 ;; I have problems
1888 ;; in OS/2
1889 ;; otherwise
1890 (buffer-file-name)))
f83d2997
KH
1891 nil nil
1892 '(gud-perldb-history . 1))))
1893\f
f83d2997
KH
1894(defun cperl-msb-fix ()
1895 ;; Adds perl files to msb menu, supposes that msb is already loaded
1896 (setq cperl-msb-fixed t)
1897 (let* ((l (length msb-menu-cond))
1898 (last (nth (1- l) msb-menu-cond))
1899 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1900 (handle (1- (nth 1 last))))
1901 (setcdr precdr (list
1902 (list
996e2616 1903 '(memq major-mode '(cperl-mode perl-mode))
f83d2997
KH
1904 handle
1905 "Perl Files (%d)")
1906 last))))
1907\f
1908;; This is used by indent-for-comment
1909;; to decide how much to indent a comment in CPerl code
1910;; based on its context. Do fallback if comment is found wrong.
1911
1912(defvar cperl-wrong-comment)
5bd52f0e
RS
1913(defvar cperl-st-cfence '(14)) ; Comment-fence
1914(defvar cperl-st-sfence '(15)) ; String-fence
1915(defvar cperl-st-punct '(1))
1916(defvar cperl-st-word '(2))
1917(defvar cperl-st-bra '(4 . ?\>))
1918(defvar cperl-st-ket '(5 . ?\<))
1919
f83d2997 1920
4ab89e7b 1921(defun cperl-comment-indent () ; called at point at supposed comment
5bd52f0e 1922 (let ((p (point)) (c (current-column)) was phony)
4ab89e7b
SM
1923 (if (and (not cperl-indent-comment-at-column-0)
1924 (looking-at "^#"))
1925 0 ; Existing comment at bol stays there.
f83d2997
KH
1926 ;; Wrong comment found
1927 (save-excursion
5bd52f0e
RS
1928 (setq was (cperl-to-comment-or-eol)
1929 phony (eq (get-text-property (point) 'syntax-table)
1930 cperl-st-cfence))
1931 (if phony
4ab89e7b 1932 (progn ; Too naive???
5bd52f0e
RS
1933 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1934 (if (eq (preceding-char) ?\#)
1935 (forward-char -1))
1936 (setq was nil)))
4ab89e7b 1937 (if (= (point) p) ; Our caller found a correct place
f83d2997
KH
1938 (progn
1939 (skip-chars-backward " \t")
4ab89e7b
SM
1940 (setq was (current-column))
1941 (if (eq was 0)
1942 comment-column
1943 (max (1+ was) ; Else indent at comment column
1944 comment-column)))
1945 ;; No, the caller found a random place; we need to edit ourselves
f83d2997
KH
1946 (if was nil
1947 (insert comment-start)
1948 (backward-char (length comment-start)))
1949 (setq cperl-wrong-comment t)
4ab89e7b
SM
1950 (cperl-make-indent comment-column 1) ; Indent min 1
1951 c)))))
f83d2997
KH
1952
1953;;;(defun cperl-comment-indent-fallback ()
1954;;; "Is called if the standard comment-search procedure fails.
1955;;;Point is at start of real comment."
1956;;; (let ((c (current-column)) target cnt prevc)
1957;;; (if (= c comment-column) nil
1958;;; (setq cnt (skip-chars-backward "[ \t]"))
5c8b7eaf 1959;;; (setq target (max (1+ (setq prevc
f83d2997
KH
1960;;; (current-column))) ; Else indent at comment column
1961;;; comment-column))
1962;;; (if (= c comment-column) nil
1963;;; (delete-backward-char cnt)
1964;;; (while (< prevc target)
1965;;; (insert "\t")
1966;;; (setq prevc (current-column)))
1967;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1968;;; (while (< prevc target)
1969;;; (insert " ")
1970;;; (setq prevc (current-column)))))))
1971
1972(defun cperl-indent-for-comment ()
1973 "Substitute for `indent-for-comment' in CPerl."
1974 (interactive)
1975 (let (cperl-wrong-comment)
1976 (indent-for-comment)
4ab89e7b 1977 (if cperl-wrong-comment ; set by `cperl-comment-indent'
f83d2997
KH
1978 (progn (cperl-to-comment-or-eol)
1979 (forward-char (length comment-start))))))
1980
1981(defun cperl-comment-region (b e arg)
1982 "Comment or uncomment each line in the region in CPerl mode.
1983See `comment-region'."
1984 (interactive "r\np")
1985 (let ((comment-start "#"))
1986 (comment-region b e arg)))
1987
1988(defun cperl-uncomment-region (b e arg)
1989 "Uncomment or comment each line in the region in CPerl mode.
1990See `comment-region'."
1991 (interactive "r\np")
1992 (let ((comment-start "#"))
1993 (comment-region b e (- arg))))
1994
1995(defvar cperl-brace-recursing nil)
1996
1997(defun cperl-electric-brace (arg &optional only-before)
1998 "Insert character and correct line's indentation.
1999If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2000place (even in empty line), but not after. If after \")\" and the inserted
5c8b7eaf 2001char is \"{\", insert extra newline before only if
f83d2997
KH
2002`cperl-extra-newline-before-brace'."
2003 (interactive "P")
2004 (let (insertpos
2005 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 2006 (cperl-mark-active)
f83d2997 2007 (< (mark) (point)))
5c8b7eaf 2008 (mark)
f83d2997
KH
2009 nil)))
2010 (if (and other-end
2011 (not cperl-brace-recursing)
2012 (cperl-val 'cperl-electric-parens)
2013 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2014 ;; Need to insert a matching pair
2015 (progn
2016 (save-excursion
2017 (setq insertpos (point-marker))
2018 (goto-char other-end)
2019 (setq last-command-char ?\{)
2020 (cperl-electric-lbrace arg insertpos))
2021 (forward-char 1))
83261a2f 2022 ;; Check whether we close something "usual" with `}'
db133cb6 2023 (if (and (eq last-command-char ?\})
5c8b7eaf 2024 (not
db133cb6
RS
2025 (condition-case nil
2026 (save-excursion
2027 (up-list (- (prefix-numeric-value arg)))
2028 ;;(cperl-after-block-p (point-min))
f739b53b
SM
2029 (or (cperl-after-expr-p nil "{;)")
2030 ;; after sub, else, continue
2031 (cperl-after-block-p nil 'pre)))
db133cb6
RS
2032 (error nil))))
2033 ;; Just insert the guy
2034 (self-insert-command (prefix-numeric-value arg))
2035 (if (and (not arg) ; No args, end (of empty line or auto)
2036 (eolp)
2037 (or (and (null only-before)
2038 (save-excursion
2039 (skip-chars-backward " \t")
2040 (bolp)))
2041 (and (eq last-command-char ?\{) ; Do not insert newline
2042 ;; if after ")" and `cperl-extra-newline-before-brace'
2043 ;; is nil, do not insert extra newline.
2044 (not cperl-extra-newline-before-brace)
2045 (save-excursion
2046 (skip-chars-backward " \t")
2047 (eq (preceding-char) ?\))))
5c8b7eaf 2048 (if cperl-auto-newline
db133cb6
RS
2049 (progn (cperl-indent-line) (newline) t) nil)))
2050 (progn
2051 (self-insert-command (prefix-numeric-value arg))
2052 (cperl-indent-line)
2053 (if cperl-auto-newline
2054 (setq insertpos (1- (point))))
2055 (if (and cperl-auto-newline (null only-before))
2056 (progn
2057 (newline)
2058 (cperl-indent-line)))
2059 (save-excursion
2060 (if insertpos (progn (goto-char insertpos)
5c8b7eaf 2061 (search-forward (make-string
db133cb6
RS
2062 1 last-command-char))
2063 (setq insertpos (1- (point)))))
2064 (delete-char -1))))
2065 (if insertpos
f83d2997 2066 (save-excursion
db133cb6
RS
2067 (goto-char insertpos)
2068 (self-insert-command (prefix-numeric-value arg)))
2069 (self-insert-command (prefix-numeric-value arg)))))))
f83d2997
KH
2070
2071(defun cperl-electric-lbrace (arg &optional end)
2072 "Insert character, correct line's indentation, correct quoting by space."
2073 (interactive "P")
83261a2f
SM
2074 (let ((cperl-brace-recursing t)
2075 (cperl-auto-newline cperl-auto-newline)
2076 (other-end (or end
2077 (if (and cperl-electric-parens-mark
2078 (cperl-mark-active)
2079 (> (mark) (point)))
2080 (save-excursion
2081 (goto-char (mark))
2082 (point-marker))
2083 nil)))
2084 pos after)
f83d2997
KH
2085 (and (cperl-val 'cperl-electric-lbrace-space)
2086 (eq (preceding-char) ?$)
2087 (save-excursion
2088 (skip-chars-backward "$")
2089 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
b5b0cb34 2090 (insert ?\s))
bab27c0c 2091 ;; Check whether we are in comment
5c8b7eaf 2092 (if (and
bab27c0c
RS
2093 (save-excursion
2094 (beginning-of-line)
2095 (not (looking-at "[ \t]*#")))
2096 (cperl-after-expr-p nil "{;)"))
2097 nil
2098 (setq cperl-auto-newline nil))
f83d2997
KH
2099 (cperl-electric-brace arg)
2100 (and (cperl-val 'cperl-electric-parens)
2101 (eq last-command-char ?{)
5c8b7eaf 2102 (memq last-command-char
f83d2997
KH
2103 (append cperl-electric-parens-string nil))
2104 (or (if other-end (goto-char (marker-position other-end)))
2105 t)
2106 (setq last-command-char ?} pos (point))
2107 (progn (cperl-electric-brace arg t)
2108 (goto-char pos)))))
2109
2110(defun cperl-electric-paren (arg)
f739b53b
SM
2111 "Insert an opening parenthesis or a matching pair of parentheses.
2112See `cperl-electric-parens'."
f83d2997
KH
2113 (interactive "P")
2114 (let ((beg (save-excursion (beginning-of-line) (point)))
2115 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 2116 (cperl-mark-active)
f83d2997 2117 (> (mark) (point)))
83261a2f
SM
2118 (save-excursion
2119 (goto-char (mark))
2120 (point-marker))
f83d2997
KH
2121 nil)))
2122 (if (and (cperl-val 'cperl-electric-parens)
2123 (memq last-command-char
2124 (append cperl-electric-parens-string nil))
2125 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2126 ;;(not (save-excursion (search-backward "#" beg t)))
2127 (if (eq last-command-char ?<)
2128 (progn
2129 (and abbrev-mode ; later it is too late, may be after `for'
2130 (expand-abbrev))
2131 (cperl-after-expr-p nil "{;(,:="))
2132 1))
2133 (progn
2134 (self-insert-command (prefix-numeric-value arg))
2135 (if other-end (goto-char (marker-position other-end)))
5c8b7eaf 2136 (insert (make-string
f83d2997
KH
2137 (prefix-numeric-value arg)
2138 (cdr (assoc last-command-char '((?{ .?})
2139 (?[ . ?])
2140 (?( . ?))
2141 (?< . ?>))))))
2142 (forward-char (- (prefix-numeric-value arg))))
2143 (self-insert-command (prefix-numeric-value arg)))))
2144
2145(defun cperl-electric-rparen (arg)
2146 "Insert a matching pair of parentheses if marking is active.
f739b53b
SM
2147If not, or if we are not at the end of marking range, would self-insert.
2148Affected by `cperl-electric-parens'."
f83d2997
KH
2149 (interactive "P")
2150 (let ((beg (save-excursion (beginning-of-line) (point)))
2151 (other-end (if (and cperl-electric-parens-mark
2152 (cperl-val 'cperl-electric-parens)
2153 (memq last-command-char
2154 (append cperl-electric-parens-string nil))
5c8b7eaf 2155 (cperl-mark-active)
f83d2997 2156 (< (mark) (point)))
5c8b7eaf 2157 (mark)
f83d2997
KH
2158 nil))
2159 p)
2160 (if (and other-end
2161 (cperl-val 'cperl-electric-parens)
2162 (memq last-command-char '( ?\) ?\] ?\} ?\> ))
2163 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2164 ;;(not (save-excursion (search-backward "#" beg t)))
2165 )
2166 (progn
2167 (self-insert-command (prefix-numeric-value arg))
2168 (setq p (point))
2169 (if other-end (goto-char other-end))
2170 (insert (make-string
2171 (prefix-numeric-value arg)
2172 (cdr (assoc last-command-char '((?\} . ?\{)
83261a2f
SM
2173 (?\] . ?\[)
2174 (?\) . ?\()
2175 (?\> . ?\<))))))
f83d2997
KH
2176 (goto-char (1+ p)))
2177 (self-insert-command (prefix-numeric-value arg)))))
2178
2179(defun cperl-electric-keyword ()
db133cb6
RS
2180 "Insert a construction appropriate after a keyword.
2181Help message may be switched off by setting `cperl-message-electric-keyword'
2182to nil."
5c8b7eaf 2183 (let ((beg (save-excursion (beginning-of-line) (point)))
f83d2997
KH
2184 (dollar (and (eq last-command-char ?$)
2185 (eq this-command 'self-insert-command)))
b5b0cb34 2186 (delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
db133cb6
RS
2187 (memq this-command '(self-insert-command newline))))
2188 my do)
f83d2997 2189 (and (save-excursion
db133cb6
RS
2190 (condition-case nil
2191 (progn
2192 (backward-sexp 1)
2193 (setq do (looking-at "do\\>")))
2194 (error nil))
f83d2997 2195 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
2196 (save-excursion
2197 (not
f83d2997 2198 (re-search-backward
5bd52f0e 2199 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
2200 beg t)))
2201 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
2202 (or
2203 (looking-at "=cut")
2204 (and cperl-use-syntax-table-text-property
2205 (not (eq (get-text-property (point)
2206 'syntax-type)
2207 'pod))))))
f739b53b
SM
2208 (save-excursion (forward-sexp -1)
2209 (not (memq (following-char) (append "$@%&*" nil))))
f83d2997 2210 (progn
db133cb6
RS
2211 (and (eq (preceding-char) ?y)
2212 (progn ; "foreachmy"
2213 (forward-char -2)
2214 (insert " ")
2215 (forward-char 2)
5c8b7eaf
SS
2216 (setq my t dollar t
2217 delete
db133cb6 2218 (memq this-command '(self-insert-command newline)))))
f83d2997
KH
2219 (and dollar (insert " $"))
2220 (cperl-indent-line)
2221 ;;(insert " () {\n}")
2222 (cond
2223 (cperl-extra-newline-before-brace
db133cb6 2224 (insert (if do "\n" " ()\n"))
f83d2997
KH
2225 (insert "{")
2226 (cperl-indent-line)
2227 (insert "\n")
2228 (cperl-indent-line)
db133cb6
RS
2229 (insert "\n}")
2230 (and do (insert " while ();")))
f83d2997 2231 (t
83261a2f 2232 (insert (if do " {\n} while ();" " () {\n}"))))
f83d2997
KH
2233 (or (looking-at "[ \t]\\|$") (insert " "))
2234 (cperl-indent-line)
2235 (if dollar (progn (search-backward "$")
5c8b7eaf 2236 (if my
db133cb6
RS
2237 (forward-char 1)
2238 (delete-char 1)))
f739b53b
SM
2239 (search-backward ")")
2240 (if (eq last-command-char ?\()
2241 (progn ; Avoid "if (())"
2242 (delete-backward-char 1)
2243 (delete-backward-char -1))))
f83d2997 2244 (if delete
db133cb6
RS
2245 (cperl-putback-char cperl-del-back-ch))
2246 (if cperl-message-electric-keyword
2247 (message "Precede char by C-q to avoid expansion"))))))
2248
2249(defun cperl-ensure-newlines (n &optional pos)
2250 "Make sure there are N newlines after the point."
2251 (or pos (setq pos (point)))
2252 (if (looking-at "\n")
2253 (forward-char 1)
2254 (insert "\n"))
2255 (if (> n 1)
2256 (cperl-ensure-newlines (1- n) pos)
2257 (goto-char pos)))
2258
2259(defun cperl-electric-pod ()
2260 "Insert a POD chunk appropriate after a =POD directive."
b5b0cb34 2261 (let ((delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
db133cb6
RS
2262 (memq this-command '(self-insert-command newline))))
2263 head1 notlast name p really-delete over)
2264 (and (save-excursion
6c389151 2265 (forward-word -1)
a1506d29 2266 (and
db133cb6
RS
2267 (eq (preceding-char) ?=)
2268 (progn
6c389151
SM
2269 (setq head1 (looking-at "head1\\>[ \t]*$"))
2270 (setq over (and (looking-at "over\\>[ \t]*$")
2271 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
db133cb6
RS
2272 (forward-char -1)
2273 (bolp))
5c8b7eaf 2274 (or
5bd52f0e 2275 (get-text-property (point) 'in-pod)
db133cb6 2276 (cperl-after-expr-p nil "{;:")
4ab89e7b
SM
2277 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2278 (not (looking-at "\n*=cut"))
2279 (or (not cperl-use-syntax-table-text-property)
2280 (eq (get-text-property (point) 'syntax-type) 'pod))))))
db133cb6
RS
2281 (progn
2282 (save-excursion
6c389151 2283 (setq notlast (re-search-forward "^\n=" nil t)))
db133cb6
RS
2284 (or notlast
2285 (progn
2286 (insert "\n\n=cut")
2287 (cperl-ensure-newlines 2)
6c389151 2288 (forward-word -2)
a1506d29
JB
2289 (if (and head1
2290 (not
db133cb6
RS
2291 (save-excursion
2292 (forward-char -1)
2293 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
83261a2f 2294 nil t)))) ; Only one
a1506d29 2295 (progn
6c389151 2296 (forward-word 1)
db133cb6
RS
2297 (setq name (file-name-sans-extension
2298 (file-name-nondirectory (buffer-file-name)))
2299 p (point))
5c8b7eaf 2300 (insert " NAME\n\n" name
029cb4d5 2301 " - \n\n=head1 SYNOPSIS\n\n\n\n"
db133cb6
RS
2302 "=head1 DESCRIPTION")
2303 (cperl-ensure-newlines 4)
2304 (goto-char p)
6c389151 2305 (forward-word 2)
db133cb6
RS
2306 (end-of-line)
2307 (setq really-delete t))
6c389151 2308 (forward-word 1))))
db133cb6
RS
2309 (if over
2310 (progn
2311 (setq p (point))
2312 (insert "\n\n=item \n\n\n\n"
2313 "=back")
2314 (cperl-ensure-newlines 2)
2315 (goto-char p)
6c389151 2316 (forward-word 1)
db133cb6
RS
2317 (end-of-line)
2318 (setq really-delete t)))
2319 (if (and delete really-delete)
f83d2997
KH
2320 (cperl-putback-char cperl-del-back-ch))))))
2321
2322(defun cperl-electric-else ()
db133cb6
RS
2323 "Insert a construction appropriate after a keyword.
2324Help message may be switched off by setting `cperl-message-electric-keyword'
2325to nil."
f83d2997
KH
2326 (let ((beg (save-excursion (beginning-of-line) (point))))
2327 (and (save-excursion
2328 (backward-sexp 1)
2329 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
2330 (save-excursion
2331 (not
f83d2997 2332 (re-search-backward
5bd52f0e 2333 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
2334 beg t)))
2335 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
2336 (looking-at "=cut")
2337 (and cperl-use-syntax-table-text-property
2338 (not (eq (get-text-property (point)
2339 'syntax-type)
2340 'pod)))))
f83d2997
KH
2341 (progn
2342 (cperl-indent-line)
2343 ;;(insert " {\n\n}")
2344 (cond
2345 (cperl-extra-newline-before-brace
2346 (insert "\n")
2347 (insert "{")
2348 (cperl-indent-line)
2349 (insert "\n\n}"))
2350 (t
83261a2f 2351 (insert " {\n\n}")))
f83d2997
KH
2352 (or (looking-at "[ \t]\\|$") (insert " "))
2353 (cperl-indent-line)
2354 (forward-line -1)
2355 (cperl-indent-line)
db133cb6
RS
2356 (cperl-putback-char cperl-del-back-ch)
2357 (setq this-command 'cperl-electric-else)
2358 (if cperl-message-electric-keyword
2359 (message "Precede char by C-q to avoid expansion"))))))
f83d2997
KH
2360
2361(defun cperl-linefeed ()
db133cb6
RS
2362 "Go to end of line, open a new line and indent appropriately.
2363If in POD, insert appropriate lines."
f83d2997
KH
2364 (interactive)
2365 (let ((beg (save-excursion (beginning-of-line) (point)))
2366 (end (save-excursion (end-of-line) (point)))
db133cb6 2367 (pos (point)) start over cut res)
f83d2997 2368 (if (and ; Check if we need to split:
5c8b7eaf 2369 ; i.e., on a boundary and inside "{...}"
f83d2997 2370 (save-excursion (cperl-to-comment-or-eol)
83261a2f 2371 (>= (point) pos)) ; Not in a comment
f83d2997
KH
2372 (or (save-excursion
2373 (skip-chars-backward " \t" beg)
2374 (forward-char -1)
2375 (looking-at "[;{]")) ; After { or ; + spaces
2376 (looking-at "[ \t]*}") ; Before }
2377 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2378 (save-excursion
2379 (and
5c8b7eaf 2380 (eq (car (parse-partial-sexp pos end -1)) -1)
f83d2997
KH
2381 ; Leave the level of parens
2382 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2383 ; Are at end
6c389151 2384 (cperl-after-block-p (point-min))
f83d2997
KH
2385 (progn
2386 (backward-sexp 1)
2387 (setq start (point-marker))
db133cb6 2388 (<= start pos))))) ; Redundant? Are after the
f83d2997
KH
2389 ; start of parens group.
2390 (progn
2391 (skip-chars-backward " \t")
2392 (or (memq (preceding-char) (append ";{" nil))
2393 (insert ";"))
2394 (insert "\n")
2395 (forward-line -1)
2396 (cperl-indent-line)
2397 (goto-char start)
2398 (or (looking-at "{[ \t]*$") ; If there is a statement
2399 ; before, move it to separate line
2400 (progn
2401 (forward-char 1)
2402 (insert "\n")
2403 (cperl-indent-line)))
2404 (forward-line 1) ; We are on the target line
2405 (cperl-indent-line)
2406 (beginning-of-line)
2407 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
83261a2f 2408 ; after, move it to separate line
f83d2997
KH
2409 (progn
2410 (end-of-line)
2411 (search-backward "}" beg)
2412 (skip-chars-backward " \t")
2413 (or (memq (preceding-char) (append ";{" nil))
2414 (insert ";"))
2415 (insert "\n")
2416 (cperl-indent-line)
2417 (forward-line -1)))
5c8b7eaf 2418 (forward-line -1) ; We are on the line before target
f83d2997
KH
2419 (end-of-line)
2420 (newline-and-indent))
db133cb6 2421 (end-of-line) ; else - no splitting
f83d2997
KH
2422 (cond
2423 ((and (looking-at "\n[ \t]*{$")
2424 (save-excursion
2425 (skip-chars-backward " \t")
2426 (eq (preceding-char) ?\)))) ; Probably if () {} group
83261a2f 2427 ; with an extra newline.
f83d2997
KH
2428 (forward-line 2)
2429 (cperl-indent-line))
db133cb6
RS
2430 ((save-excursion ; In POD header
2431 (forward-paragraph -1)
2432 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2433 ;; We are after \n now, so look for the rest
2434 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
5c8b7eaf 2435 (progn
db133cb6
RS
2436 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2437 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2438 t)))
2439 (if (and over
2440 (progn
2441 (forward-paragraph -1)
2442 (forward-word 1)
2443 (setq pos (point))
2444 (setq cut (buffer-substring (point)
2445 (save-excursion
2446 (end-of-line)
2447 (point))))
2448 (delete-char (- (save-excursion (end-of-line) (point))
2449 (point)))
2450 (setq res (expand-abbrev))
2451 (save-excursion
2452 (goto-char pos)
2453 (insert cut))
2454 res))
2455 nil
2456 (cperl-ensure-newlines (if cut 2 4))
2457 (forward-line 2)))
2458 ((get-text-property (point) 'in-pod) ; In POD section
2459 (cperl-ensure-newlines 4)
2460 (forward-line 2))
f83d2997
KH
2461 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2462 (forward-line 1)
2463 (cperl-indent-line))
2464 (t
2465 (newline-and-indent))))))
2466
2467(defun cperl-electric-semi (arg)
2468 "Insert character and correct line's indentation."
2469 (interactive "P")
2470 (if cperl-auto-newline
2471 (cperl-electric-terminator arg)
6c389151
SM
2472 (self-insert-command (prefix-numeric-value arg))
2473 (if cperl-autoindent-on-semi
2474 (cperl-indent-line))))
f83d2997
KH
2475
2476(defun cperl-electric-terminator (arg)
2477 "Insert character and correct line's indentation."
2478 (interactive "P")
83261a2f
SM
2479 (let ((end (point))
2480 (auto (and cperl-auto-newline
2481 (or (not (eq last-command-char ?:))
2482 cperl-auto-newline-after-colon)))
2483 insertpos)
5c8b7eaf 2484 (if (and ;;(not arg)
f83d2997
KH
2485 (eolp)
2486 (not (save-excursion
2487 (beginning-of-line)
2488 (skip-chars-forward " \t")
2489 (or
2490 ;; Ignore in comment lines
2491 (= (following-char) ?#)
2492 ;; Colon is special only after a label
2493 ;; So quickly rule out most other uses of colon
2494 ;; and do no indentation for them.
2495 (and (eq last-command-char ?:)
2496 (save-excursion
2497 (forward-word 1)
2498 (skip-chars-forward " \t")
2499 (and (< (point) end)
2500 (progn (goto-char (- end 1))
2501 (not (looking-at ":"))))))
2502 (progn
2503 (beginning-of-defun)
2504 (let ((pps (parse-partial-sexp (point) end)))
2505 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2506 (progn
2507 (self-insert-command (prefix-numeric-value arg))
2508 ;;(forward-char -1)
2509 (if auto (setq insertpos (point-marker)))
2510 ;;(forward-char 1)
2511 (cperl-indent-line)
2512 (if auto
2513 (progn
2514 (newline)
2515 (cperl-indent-line)))
f83d2997
KH
2516 (save-excursion
2517 (if insertpos (goto-char (1- (marker-position insertpos)))
2518 (forward-char -1))
2519 (delete-char 1))))
2520 (if insertpos
2521 (save-excursion
2522 (goto-char insertpos)
2523 (self-insert-command (prefix-numeric-value arg)))
2524 (self-insert-command (prefix-numeric-value arg)))))
2525
2526(defun cperl-electric-backspace (arg)
f739b53b
SM
2527 "Backspace, or remove the whitespace around the point inserted by an electric
2528key. Will untabify if `cperl-electric-backspace-untabify' is non-nil."
f83d2997 2529 (interactive "p")
5c8b7eaf
SS
2530 (if (and cperl-auto-newline
2531 (memq last-command '(cperl-electric-semi
f83d2997
KH
2532 cperl-electric-terminator
2533 cperl-electric-lbrace))
b5b0cb34 2534 (memq (preceding-char) '(?\s ?\t ?\n)))
f83d2997 2535 (let (p)
5c8b7eaf 2536 (if (eq last-command 'cperl-electric-lbrace)
f83d2997
KH
2537 (skip-chars-forward " \t\n"))
2538 (setq p (point))
2539 (skip-chars-backward " \t\n")
2540 (delete-region (point) p))
db133cb6
RS
2541 (and (eq last-command 'cperl-electric-else)
2542 ;; We are removing the whitespace *inside* cperl-electric-else
2543 (setq this-command 'cperl-electric-else-really))
5c8b7eaf 2544 (if (and cperl-auto-newline
db133cb6 2545 (eq last-command 'cperl-electric-else-really)
b5b0cb34 2546 (memq (preceding-char) '(?\s ?\t ?\n)))
db133cb6
RS
2547 (let (p)
2548 (skip-chars-forward " \t\n")
2549 (setq p (point))
2550 (skip-chars-backward " \t\n")
2551 (delete-region (point) p))
f739b53b
SM
2552 (if cperl-electric-backspace-untabify
2553 (backward-delete-char-untabify arg)
2554 (delete-backward-char arg)))))
f83d2997 2555
d6156ce8
KS
2556(put 'cperl-electric-backspace 'delete-selection 'supersede)
2557
4ab89e7b 2558(defun cperl-inside-parens-p () ;; NOT USED????
f83d2997
KH
2559 (condition-case ()
2560 (save-excursion
2561 (save-restriction
2562 (narrow-to-region (point)
2563 (progn (beginning-of-defun) (point)))
2564 (goto-char (point-max))
2565 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2566 (error nil)))
2567\f
2568(defun cperl-indent-command (&optional whole-exp)
2569 "Indent current line as Perl code, or in some cases insert a tab character.
5c8b7eaf 2570If `cperl-tab-always-indent' is non-nil (the default), always indent current
db133cb6 2571line. Otherwise, indent the current line only if point is at the left margin
f83d2997
KH
2572or in the line's indentation; otherwise insert a tab.
2573
2574A numeric argument, regardless of its value,
2575means indent rigidly all the lines of the expression starting after point
2576so that this line becomes properly indented.
2577The relative indentation among the lines of the expression are preserved."
2578 (interactive "P")
5bd52f0e 2579 (cperl-update-syntaxification (point) (point))
f83d2997
KH
2580 (if whole-exp
2581 ;; If arg, always indent this line as Perl
2582 ;; and shift remaining lines of expression the same amount.
2583 (let ((shift-amt (cperl-indent-line))
2584 beg end)
2585 (save-excursion
2586 (if cperl-tab-always-indent
2587 (beginning-of-line))
2588 (setq beg (point))
2589 (forward-sexp 1)
2590 (setq end (point))
2591 (goto-char beg)
2592 (forward-line 1)
2593 (setq beg (point)))
db133cb6 2594 (if (and shift-amt (> end beg))
f83d2997
KH
2595 (indent-code-rigidly beg end shift-amt "#")))
2596 (if (and (not cperl-tab-always-indent)
2597 (save-excursion
2598 (skip-chars-backward " \t")
2599 (not (bolp))))
2600 (insert-tab)
2601 (cperl-indent-line))))
2602
5bd52f0e 2603(defun cperl-indent-line (&optional parse-data)
f83d2997
KH
2604 "Indent current line as Perl code.
2605Return the amount the indentation changed by."
83261a2f
SM
2606 (let ((case-fold-search nil)
2607 (pos (- (point-max) (point)))
2608 indent i beg shift-amt)
5bd52f0e 2609 (setq indent (cperl-calculate-indent parse-data)
db133cb6 2610 i indent)
f83d2997
KH
2611 (beginning-of-line)
2612 (setq beg (point))
2613 (cond ((or (eq indent nil) (eq indent t))
db133cb6 2614 (setq indent (current-indentation) i nil))
f83d2997
KH
2615 ;;((eq indent t) ; Never?
2616 ;; (setq indent (cperl-calculate-indent-within-comment)))
2617 ;;((looking-at "[ \t]*#")
2618 ;; (setq indent 0))
2619 (t
2620 (skip-chars-forward " \t")
2621 (if (listp indent) (setq indent (car indent)))
2622 (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2623 (and (> indent 0)
2624 (setq indent (max cperl-min-label-indent
2625 (+ indent cperl-label-offset)))))
2626 ((= (following-char) ?})
2627 (setq indent (- indent cperl-indent-level)))
2628 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2629 (setq indent (+ indent cperl-close-paren-offset)))
2630 ((= (following-char) ?{)
2631 (setq indent (+ indent cperl-brace-offset))))))
2632 (skip-chars-forward " \t")
db133cb6
RS
2633 (setq shift-amt (and i (- indent (current-column))))
2634 (if (or (not shift-amt)
2635 (zerop shift-amt))
f83d2997
KH
2636 (if (> (- (point-max) pos) (point))
2637 (goto-char (- (point-max) pos)))
4ab89e7b
SM
2638 ;;;(delete-region beg (point))
2639 ;;;(indent-to indent)
2640 (cperl-make-indent indent)
f83d2997
KH
2641 ;; If initial point was within line's indentation,
2642 ;; position after the indentation. Else stay at same point in text.
2643 (if (> (- (point-max) pos) (point))
2644 (goto-char (- (point-max) pos))))
2645 shift-amt))
2646
2647(defun cperl-after-label ()
2648 ;; Returns true if the point is after label. Does not do save-excursion.
2649 (and (eq (preceding-char) ?:)
2650 (memq (char-syntax (char-after (- (point) 2)))
2651 '(?w ?_))
2652 (progn
2653 (backward-sexp)
2654 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2655
2656(defun cperl-get-state (&optional parse-start start-state)
5bd52f0e
RS
2657 ;; returns list (START STATE DEPTH PRESTART),
2658 ;; START is a good place to start parsing, or equal to
5c8b7eaf 2659 ;; PARSE-START if preset,
5bd52f0e
RS
2660 ;; STATE is what is returned by `parse-partial-sexp'.
2661 ;; DEPTH is true is we are immediately after end of block
2662 ;; which contains START.
2663 ;; PRESTART is the position basing on which START was found.
f83d2997
KH
2664 (save-excursion
2665 (let ((start-point (point)) depth state start prestart)
5bd52f0e
RS
2666 (if (and parse-start
2667 (<= parse-start start-point))
f83d2997 2668 (goto-char parse-start)
5bd52f0e
RS
2669 (beginning-of-defun)
2670 (setq start-state nil))
f83d2997
KH
2671 (setq prestart (point))
2672 (if start-state nil
2673 ;; Try to go out, if sub is not on the outermost level
2674 (while (< (point) start-point)
2675 (setq start (point) parse-start start depth nil
2676 state (parse-partial-sexp start start-point -1))
2677 (if (> (car state) -1) nil
2678 ;; The current line could start like }}}, so the indentation
2679 ;; corresponds to a different level than what we reached
2680 (setq depth t)
2681 (beginning-of-line 2))) ; Go to the next line.
2682 (if start (goto-char start))) ; Not at the start of file
2683 (setq start (point))
f83d2997
KH
2684 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2685 (list start state depth prestart))))
2686
f83d2997
KH
2687(defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2688
4ab89e7b
SM
2689(defun cperl-beginning-of-property (p prop &optional lim)
2690 "Given that P has a property PROP, find where the property starts.
2691Will not look before LIM."
2692 ;;; XXXX What to do at point-max???
2693 (or (previous-single-property-change (cperl-1+ p) prop lim)
2694 (point-min))
2695;;; (cond ((eq p (point-min))
2696;;; p)
2697;;; ((and lim (<= p lim))
2698;;; p)
2699;;; ((not (get-text-property (1- p) prop))
2700;;; p)
2701;;; (t (or (previous-single-property-change p look-prop lim)
2702;;; (point-min))))
2703 )
2704
2705(defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2706 ;; Old workhorse for calculation of indentation; the major problem
2707 ;; is that it mixes the sniffer logic to understand what the current line
2708 ;; MEANS with the logic to actually calculate where to indent it.
2709 ;; The latter part should be eventually moved to `cperl-calculate-indent';
2710 ;; actually, this is mostly done now...
f739b53b 2711 (cperl-update-syntaxification (point) (point))
4ab89e7b
SM
2712 (let ((res (get-text-property (point) 'syntax-type)))
2713 (save-excursion
2714 (cond
2715 ((and (memq res '(pod here-doc here-doc-delim format))
2716 (not (get-text-property (point) 'indentable)))
2717 (vector res))
2718 ;; before start of POD - whitespace found since do not have 'pod!
2719 ((looking-at "[ \t]*\n=")
2720 (error "Spaces before POD section!"))
2721 ((and (not cperl-indent-left-aligned-comments)
2722 (looking-at "^#"))
2723 [comment-special:at-beginning-of-line])
2724 ((get-text-property (point) 'in-pod)
2725 [in-pod])
2726 (t
2727 (beginning-of-line)
2728 (let* ((indent-point (point))
2729 (char-after-pos (save-excursion
2730 (skip-chars-forward " \t")
2731 (point)))
2732 (char-after (char-after char-after-pos))
2733 (pre-indent-point (point))
2734 p prop look-prop is-block delim)
2735 (save-excursion ; Know we are not in POD, find appropriate pos before
83261a2f
SM
2736 (cperl-backward-to-noncomment nil)
2737 (setq p (max (point-min) (1- (point)))
2738 prop (get-text-property p 'syntax-type)
2739 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2740 'syntax-type))
2741 (if (memq prop '(pod here-doc format here-doc-delim))
2742 (progn
4ab89e7b 2743 (goto-char (cperl-beginning-of-property p look-prop))
83261a2f 2744 (beginning-of-line)
4ab89e7b
SM
2745 (setq pre-indent-point (point)))))
2746 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2747 (let* ((case-fold-search nil)
2748 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2749 (start (or (nth 2 parse-data) ; last complete sexp terminated
2750 (nth 0 s-s))) ; Good place to start parsing
2751 (state (nth 1 s-s))
2752 (containing-sexp (car (cdr state)))
2753 old-indent)
2754 (if (and
2755 ;;containing-sexp ;; We are buggy at toplevel :-(
2756 parse-data)
2757 (progn
2758 (setcar parse-data pre-indent-point)
2759 (setcar (cdr parse-data) state)
2760 (or (nth 2 parse-data)
2761 (setcar (cddr parse-data) start))
2762 ;; Before this point: end of statement
2763 (setq old-indent (nth 3 parse-data))))
2764 (cond ((get-text-property (point) 'indentable)
2765 ;; indent to "after" the surrounding open
2766 ;; (same offset as `cperl-beautify-regexp-piece'),
2767 ;; skip blanks if we do not close the expression.
2768 (setq delim ; We do not close the expression
2769 (get-text-property
2770 (cperl-1+ char-after-pos) 'indentable)
2771 p (1+ (cperl-beginning-of-property
2772 (point) 'indentable))
2773 is-block ; misused for: preceeding line in REx
2774 (save-excursion ; Find preceeding line
2775 (cperl-backward-to-noncomment p)
2776 (beginning-of-line)
2777 (if (<= (point) p)
2778 (progn ; get indent from the first line
2779 (goto-char p)
2780 (skip-chars-forward " \t")
2781 (if (memq (char-after (point))
2782 (append "#\n" nil))
2783 nil ; Can't use intentation of this line...
2784 (point)))
2785 (skip-chars-forward " \t")
2786 (point)))
2787 prop (parse-partial-sexp p char-after-pos))
2788 (cond ((not delim) ; End the REx, ignore is-block
2789 (vector 'indentable 'terminator p is-block))
2790 (is-block ; Indent w.r.t. preceeding line
2791 (vector 'indentable 'cont-line char-after-pos
2792 is-block char-after p))
2793 (t ; No preceeding line...
2794 (vector 'indentable 'first-line p))))
2795 ((get-text-property char-after-pos 'REx-part2)
2796 (vector 'REx-part2 (point)))
2797 ((nth 3 state)
2798 [comment])
2799 ((nth 4 state)
2800 [string])
2801 ;; XXXX Do we need to special-case this?
2802 ((null containing-sexp)
2803 ;; Line is at top level. May be data or function definition,
2804 ;; or may be function argument declaration.
2805 ;; Indent like the previous top level line
2806 ;; unless that ends in a closeparen without semicolon,
2807 ;; in which case this line is the first argument decl.
2808 (skip-chars-forward " \t")
2809 (cperl-backward-to-noncomment (or old-indent (point-min)))
2810 (setq state
2811 (or (bobp)
2812 (eq (point) old-indent) ; old-indent was at comment
2813 (eq (preceding-char) ?\;)
2814 ;; Had ?\) too
2815 (and (eq (preceding-char) ?\})
2816 (cperl-after-block-and-statement-beg
2817 (point-min))) ; Was start - too close
2818 (memq char-after (append ")]}" nil))
2819 (and (eq (preceding-char) ?\:) ; label
83261a2f
SM
2820 (progn
2821 (forward-sexp -1)
4ab89e7b
SM
2822 (skip-chars-backward " \t")
2823 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2824 (get-text-property (point) 'first-format-line)))
2825
2826 ;; Look at previous line that's at column 0
2827 ;; to determine whether we are in top-level decls
2828 ;; or function's arg decls. Set basic-indent accordingly.
2829 ;; Now add a little if this is a continuation line.
2830 (and state
2831 parse-data
2832 (not (eq char-after ?\C-j))
2833 (setcdr (cddr parse-data)
2834 (list pre-indent-point)))
2835 (vector 'toplevel start char-after state (nth 2 s-s)))
2836 ((not
2837 (or (setq is-block
2838 (and (setq delim (= (char-after containing-sexp) ?{))
2839 (save-excursion ; Is it a hash?
2840 (goto-char containing-sexp)
2841 (cperl-block-p))))
2842 cperl-indent-parens-as-block))
2843 ;; group is an expression, not a block:
2844 ;; indent to just after the surrounding open parens,
2845 ;; skip blanks if we do not close the expression.
2846 (goto-char (1+ containing-sexp))
2847 (or (memq char-after
2848 (append (if delim "}" ")]}") nil))
2849 (looking-at "[ \t]*\\(#\\|$\\)")
2850 (skip-chars-forward " \t"))
2851 (setq old-indent (point)) ; delim=is-brace
2852 (vector 'in-parens char-after (point) delim containing-sexp))
2853 (t
2854 ;; Statement level. Is it a continuation or a new statement?
2855 ;; Find previous non-comment character.
2856 (goto-char pre-indent-point) ; Skip one level of POD/etc
2857 (cperl-backward-to-noncomment containing-sexp)
2858 ;; Back up over label lines, since they don't
2859 ;; affect whether our line is a continuation.
2860 ;; (Had \, too)
2861 (while;;(or (eq (preceding-char) ?\,)
2862 (and (eq (preceding-char) ?:)
2863 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2864 (memq (char-syntax (char-after (- (point) 2)))
2865 '(?w ?_))))
2866 ;;)
2867 ;; This is always FALSE?
2868 (if (eq (preceding-char) ?\,)
2869 ;; Will go to beginning of line, essentially.
2870 ;; Will ignore embedded sexpr XXXX.
2871 (cperl-backward-to-start-of-continued-exp containing-sexp))
2872 (beginning-of-line)
2873 (cperl-backward-to-noncomment containing-sexp))
2874 ;; Now we get non-label preceeding the indent point
2875 (if (not (or (eq (1- (point)) containing-sexp)
2876 (memq (preceding-char)
2877 (append (if is-block " ;{" " ,;{") '(nil)))
2878 (and (eq (preceding-char) ?\})
2879 (cperl-after-block-and-statement-beg
2880 containing-sexp))
2881 (get-text-property (point) 'first-format-line)))
2882 ;; This line is continuation of preceding line's statement;
2883 ;; indent `cperl-continued-statement-offset' more than the
2884 ;; previous line of the statement.
2885 ;;
2886 ;; There might be a label on this line, just
2887 ;; consider it bad style and ignore it.
2888 (progn
2889 (cperl-backward-to-start-of-continued-exp containing-sexp)
2890 (vector 'continuation (point) char-after is-block delim))
2891 ;; This line starts a new statement.
2892 ;; Position following last unclosed open brace
2893 (goto-char containing-sexp)
2894 ;; Is line first statement after an open-brace?
2895 (or
2896 ;; If no, find that first statement and indent like
2897 ;; it. If the first statement begins with label, do
2898 ;; not believe when the indentation of the label is too
2899 ;; small.
2900 (save-excursion
2901 (forward-char 1)
2902 (let ((colon-line-end 0))
2903 (while
2904 (progn (skip-chars-forward " \t\n")
2905 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]\\|=[a-zA-Z]"))
2906 ;; Skip over comments and labels following openbrace.
2907 (cond ((= (following-char) ?\#)
2908 (forward-line 1))
2909 ((= (following-char) ?\=)
2910 (goto-char
2911 (or (next-single-property-change (point) 'in-pod)
2912 (point-max)))) ; do not loop if no syntaxification
2913 ;; label:
2914 (t
2915 (save-excursion (end-of-line)
2916 (setq colon-line-end (point)))
2917 (search-forward ":"))))
2918 ;; We are at beginning of code (NOT label or comment)
2919 ;; First, the following code counts
2920 ;; if it is before the line we want to indent.
2921 (and (< (point) indent-point)
2922 (vector 'have-prev-sibling (point) colon-line-end
2923 containing-sexp))))
2924 (progn
2925 ;; If no previous statement,
2926 ;; indent it relative to line brace is on.
2927
2928 ;; For open-braces not the first thing in a line,
2929 ;; add in cperl-brace-imaginary-offset.
2930
2931 ;; If first thing on a line: ?????
2932 ;; Move back over whitespace before the openbrace.
2933 (setq ; brace first thing on a line
2934 old-indent (progn (skip-chars-backward " \t") (bolp)))
2935 ;; Should we indent w.r.t. earlier than start?
2936 ;; Move to start of control group, possibly on a different line
2937 (or cperl-indent-wrt-brace
2938 (cperl-backward-to-noncomment (point-min)))
2939 ;; If the openbrace is preceded by a parenthesized exp,
2940 ;; move to the beginning of that;
2941 (if (eq (preceding-char) ?\))
2942 (progn
2943 (forward-sexp -1)
2944 (cperl-backward-to-noncomment (point-min))))
2945 ;; In the case it starts a subroutine, indent with
2946 ;; respect to `sub', not with respect to the
2947 ;; first thing on the line, say in the case of
2948 ;; anonymous sub in a hash.
2949 (if (and;; Is it a sub in group starting on this line?
2950 (cond ((get-text-property (point) 'attrib-group)
2951 (goto-char (cperl-beginning-of-property
2952 (point) 'attrib-group)))
2953 ((eq (preceding-char) ?b)
2954 (forward-sexp -1)
2955 (looking-at "sub\\>")))
2956 (setq p (nth 1 ; start of innermost containing list
2957 (parse-partial-sexp
2958 (save-excursion (beginning-of-line)
2959 (point))
2960 (point)))))
2961 (progn
2962 (goto-char (1+ p)) ; enclosing block on the same line
2963 (skip-chars-forward " \t")
2964 (vector 'code-start-in-block containing-sexp char-after
2965 (and delim (not is-block)) ; is a HASH
2966 old-indent ; brace first thing on a line
2967 t (point) ; have something before...
2968 )
2969 ;;(current-column)
2970 )
2971 ;; Get initial indentation of the line we are on.
2972 ;; If line starts with label, calculate label indentation
2973 (vector 'code-start-in-block containing-sexp char-after
2974 (and delim (not is-block)) ; is a HASH
2975 old-indent ; brace first thing on a line
2976 nil (point) ; nothing interesting before
2977 ))))))))))))))
2978
2979(defvar cperl-indent-rules-alist
2980 '((pod nil) ; via `syntax-type' property
2981 (here-doc nil) ; via `syntax-type' property
2982 (here-doc-delim nil) ; via `syntax-type' property
2983 (format nil) ; via `syntax-type' property
2984 (in-pod nil) ; via `in-pod' property
2985 (comment-special:at-beginning-of-line nil)
2986 (string t)
2987 (comment nil))
2988 "Alist of indentation rules for CPerl mode.
2989The values mean:
2990 nil: do not indent;
2991 number: add this amount of indentation.
2992
2993Not finished.")
2994
2995(defun cperl-calculate-indent (&optional parse-data) ; was parse-start
2996 "Return appropriate indentation for current line as Perl code.
2997In usual case returns an integer: the column to indent to.
2998Returns nil if line starts inside a string, t if in a comment.
2999
3000Will not correct the indentation for labels, but will correct it for braces
3001and closing parentheses and brackets."
3002 ;; This code is still a broken architecture: in some cases we need to
3003 ;; compensate for some modifications which `cperl-indent-line' will add later
3004 (save-excursion
3005 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3006 (cond
3007 ;;((or (null i) (eq i t) (numberp i))
3008 ;; i)
3009 ((vectorp i)
3010 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3011 (cond
3012 (what (cadr what)) ; Load from table
3013 ;;
3014 ;; Indenters for regular expressions with //x and qw()
3015 ;;
3016 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3017 (goto-char (elt i 1))
3018 (condition-case nil ; Use indentation of the 1st part
3019 (forward-sexp -1))
3020 (current-column))
3021 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3022 (cond ;;; [indentable terminator start-pos is-block]
3023 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3024 (goto-char (elt i 2)) ; After opening parens
3025 (1- (current-column)))
3026 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3027 (goto-char (elt i 2))
3028 (+ (or cperl-regexp-indent-step cperl-indent-level)
3029 -1
3030 (current-column)))
3031 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3032 ;; Indent as the level after closing parens
3033 (goto-char (elt i 2)) ; indent line
3034 (skip-chars-forward " \t)") ; Skip closing parens
3035 (setq p (point))
3036 (goto-char (elt i 3)) ; previous line
3037 (skip-chars-forward " \t)") ; Skip closing parens
3038 ;; Number of parens in between:
3039 (setq p (nth 0 (parse-partial-sexp (point) p))
3040 what (elt i 4)) ; First char on current line
3041 (goto-char (elt i 3)) ; previous line
3042 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3043 (cond ((eq what ?\) )
3044 (- cperl-close-paren-offset)) ; compensate
3045 ((eq what ?\| )
3046 (- (or cperl-regexp-indent-step cperl-indent-level)))
3047 (t 0))
3048 (if (eq (following-char) ?\| )
3049 (or cperl-regexp-indent-step cperl-indent-level)
3050 0)
3051 (current-column)))
3052 (t
3053 (error "Unrecognized value of indent: %s" i))))
3054 ;;
3055 ;; Indenter for stuff at toplevel
3056 ;;
3057 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3058 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3059 (goto-char (elt i 1)) ; start = Good place to start parsing
3060 (- (current-indentation) ;
3061 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3062 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3063 ;; Look at previous line that's at column 0
3064 ;; to determine whether we are in top-level decls
3065 ;; or function's arg decls. Set basic-indent accordingly.
3066 ;; Now add a little if this is a continuation line.
3067 (if (elt i 3) ; state (XXX What is the semantic???)
3068 0
3069 cperl-continued-statement-offset)))
3070 ;;
3071 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3072 ;;
3073 ((eq 'in-parens (elt i 0))
3074 ;; in-parens char-after old-indent-point is-brace containing-sexp
3075
3076 ;; group is an expression, not a block:
3077 ;; indent to just after the surrounding open parens,
3078 ;; skip blanks if we do not close the expression.
3079 (+ (progn
3080 (goto-char (elt i 2)) ; old-indent-point
3081 (current-column))
3082 (if (and (elt i 3) ; is-brace
3083 (eq (elt i 1) ?\})) ; char-after
3084 ;; Correct indentation of trailing ?\}
3085 (+ cperl-indent-level cperl-close-paren-offset)
3086 0)))
3087 ;;
3088 ;; Indenter for continuation lines
3089 ;;
3090 ((eq 'continuation (elt i 0))
3091 ;; [continuation statement-start char-after is-block is-brace]
3092 (goto-char (elt i 1)) ; statement-start
3093 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3094 0 ; Closing parenth
3095 cperl-continued-statement-offset)
3096 (if (or (elt i 3) ; is-block
3097 (not (elt i 4)) ; is-brace
3098 (not (eq (elt i 2) ?\}))) ; char-after
3099 0
3100 ;; Now it is a hash reference
3101 (+ cperl-indent-level cperl-close-paren-offset))
3102 ;; Labels do not take :: ...
3103 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3104 (if (> (current-indentation) cperl-min-label-indent)
3105 (- (current-indentation) cperl-label-offset)
3106 ;; Do not move `parse-data', this should
3107 ;; be quick anyway (this comment comes
3108 ;; from different location):
3109 (cperl-calculate-indent))
3110 (current-column))
3111 (if (eq (elt i 2) ?\{) ; char-after
3112 cperl-continued-brace-offset 0)))
3113 ;;
3114 ;; Indenter for lines in a block which are not leading lines
3115 ;;
3116 ((eq 'have-prev-sibling (elt i 0))
3117 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3118 (goto-char (elt i 1))
3119 (if (> (elt i 2) (point)) ; colon-line-end; After-label, same line
3120 (if (> (current-indentation)
3121 cperl-min-label-indent)
3122 (- (current-indentation) cperl-label-offset)
3123 ;; Do not believe: `max' was involved in calculation of indent
3124 (+ cperl-indent-level
3125 (save-excursion
3126 (goto-char (elt i 3)) ; block-start
3127 (current-indentation))))
3128 (current-column)))
3129 ;;
3130 ;; Indenter for the first line in a block
3131 ;;
3132 ((eq 'code-start-in-block (elt i 0))
3133 ;;[code-start-in-block before-brace char-after
3134 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3135 ;; group-starts-before-start-of-sub start-of-control-group]
3136 (goto-char (elt i 1))
3137 ;; For open brace in column zero, don't let statement
3138 ;; start there too. If cperl-indent-level=0,
3139 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3140 (+ (if (and (bolp) (zerop cperl-indent-level))
3141 (+ cperl-brace-offset cperl-continued-statement-offset)
3142 cperl-indent-level)
3143 (if (and (elt i 3) ; is-a-HASH-ref
3144 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3145 (+ cperl-indent-level cperl-close-paren-offset)
3146 0)
3147 ;; Unless openbrace is the first nonwhite thing on the line,
3148 ;; add the cperl-brace-imaginary-offset.
3149 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3150 cperl-brace-imaginary-offset)
3151 (progn
3152 (goto-char (elt i 6)) ; start-of-control-group
3153 (if (elt i 5) ; group-starts-before-start-of-sub
3154 (current-column)
3155 ;; Get initial indentation of the line we are on.
3156 ;; If line starts with label, calculate label indentation
3157 (if (save-excursion
3158 (beginning-of-line)
3159 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3160 (if (> (current-indentation) cperl-min-label-indent)
3161 (- (current-indentation) cperl-label-offset)
3162 ;; Do not move `parse-data', this should
3163 ;; be quick anyway:
3164 (cperl-calculate-indent))
3165 (current-indentation))))))
3166 (t
3167 (error "Unrecognized value of indent: %s" i))))
3168 (t
3169 (error "Got strange value of indent: %s" i))))))
3170
3171(defvar cperl-indent-alist
3172 '((string nil)
3173 (comment nil)
3174 (toplevel 0)
3175 (toplevel-after-parenth 2)
3176 (toplevel-continued 2)
3177 (expression 1))
3178 "Alist of indentation rules for CPerl mode.
3179The values mean:
3180 nil: do not indent;
3181 number: add this amount of indentation.
3182
3183Not finished, not used.")
3184
3185(defun cperl-where-am-i (&optional parse-start start-state)
3186 ;; Unfinished
3187 "Return a list of lists ((TYPE POS)...) of good points before the point.
3188POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'.
3189
3190Not finished, not used."
3191 (save-excursion
3192 (let* ((start-point (point)) unused
3193 (s-s (cperl-get-state))
3194 (start (nth 0 s-s))
3195 (state (nth 1 s-s))
3196 (prestart (nth 3 s-s))
3197 (containing-sexp (car (cdr state)))
3198 (case-fold-search nil)
3199 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
3200 (cond ((nth 3 state) ; In string
3201 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
3202 ((nth 4 state) ; In comment
3203 (setq res (cons '(comment) res)))
3204 ((null containing-sexp)
3205 ;; Line is at top level.
3206 ;; Indent like the previous top level line
3207 ;; unless that ends in a closeparen without semicolon,
3208 ;; in which case this line is the first argument decl.
3209 (cperl-backward-to-noncomment (or parse-start (point-min)))
3210 ;;(skip-chars-backward " \t\f\n")
3211 (cond
3212 ((or (bobp)
3213 (memq (preceding-char) (append ";}" nil)))
3214 (setq res (cons (list 'toplevel start) res)))
3215 ((eq (preceding-char) ?\) )
3216 (setq res (cons (list 'toplevel-after-parenth start) res)))
3217 (t
3218 (setq res (cons (list 'toplevel-continued start) res)))))
3219 ((/= (char-after containing-sexp) ?{)
3220 ;; line is expression, not statement:
3221 ;; indent to just after the surrounding open.
3222 ;; skip blanks if we do not close the expression.
3223 (setq res (cons (list 'expression-blanks
3224 (progn
3225 (goto-char (1+ containing-sexp))
3226 (or (looking-at "[ \t]*\\(#\\|$\\)")
3227 (skip-chars-forward " \t"))
3228 (point)))
3229 (cons (list 'expression containing-sexp) res))))
3230 ((progn
3231 ;; Containing-expr starts with \{. Check whether it is a hash.
3232 (goto-char containing-sexp)
3233 (not (cperl-block-p)))
3234 (setq res (cons (list 'expression-blanks
3235 (progn
3236 (goto-char (1+ containing-sexp))
3237 (or (looking-at "[ \t]*\\(#\\|$\\)")
3238 (skip-chars-forward " \t"))
3239 (point)))
3240 (cons (list 'expression containing-sexp) res))))
3241 (t
3242 ;; Statement level.
3243 (setq res (cons (list 'in-block containing-sexp) res))
3244 ;; Is it a continuation or a new statement?
3245 ;; Find previous non-comment character.
3246 (cperl-backward-to-noncomment containing-sexp)
3247 ;; Back up over label lines, since they don't
3248 ;; affect whether our line is a continuation.
3249 ;; Back up comma-delimited lines too ?????
3250 (while (or (eq (preceding-char) ?\,)
3251 (save-excursion (cperl-after-label)))
3252 (if (eq (preceding-char) ?\,)
3253 ;; Will go to beginning of line, essentially
3254 ;; Will ignore embedded sexpr XXXX.
3255 (cperl-backward-to-start-of-continued-exp containing-sexp))
3256 (beginning-of-line)
3257 (cperl-backward-to-noncomment containing-sexp))
3258 ;; Now we get the answer.
3259 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
3260 ;; This line is continuation of preceding line's statement.
3261 (list (list 'statement-continued containing-sexp))
3262 ;; This line starts a new statement.
3263 ;; Position following last unclosed open.
3264 (goto-char containing-sexp)
3265 ;; Is line first statement after an open-brace?
3266 (or
3267 ;; If no, find that first statement and indent like
3268 ;; it. If the first statement begins with label, do
3269 ;; not believe when the indentation of the label is too
3270 ;; small.
3271 (save-excursion
3272 (forward-char 1)
3273 (let ((colon-line-end 0))
3274 (while (progn (skip-chars-forward " \t\n" start-point)
3275 (and (< (point) start-point)
3276 (looking-at
3277 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
3278 ;; Skip over comments and labels following openbrace.
3279 (cond ((= (following-char) ?\#)
3280 ;;(forward-line 1)
3281 (end-of-line))
3282 ;; label:
3283 (t
3284 (save-excursion (end-of-line)
3285 (setq colon-line-end (point)))
3286 (search-forward ":"))))
3287 ;; Now at the point, after label, or at start
3288 ;; of first statement in the block.
3289 (and (< (point) start-point)
3290 (if (> colon-line-end (point))
3291 ;; Before statement after label
3292 (if (> (current-indentation)
3293 cperl-min-label-indent)
3294 (list (list 'label-in-block (point)))
3295 ;; Do not believe: `max' is involved
3296 (list
3297 (list 'label-in-block-min-indent (point))))
3298 ;; Before statement
3299 (list 'statement-in-block (point))))))
3300 ;; If no previous statement,
3301 ;; indent it relative to line brace is on.
3302 ;; For open brace in column zero, don't let statement
3303 ;; start there too. If cperl-indent-level is zero,
3304 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3305 ;; For open-braces not the first thing in a line,
3306 ;; add in cperl-brace-imaginary-offset.
3307
3308 ;; If first thing on a line: ?????
3309 (setq unused ; This is not finished...
3310 (+ (if (and (bolp) (zerop cperl-indent-level))
3311 (+ cperl-brace-offset cperl-continued-statement-offset)
3312 cperl-indent-level)
3313 ;; Move back over whitespace before the openbrace.
3314 ;; If openbrace is not first nonwhite thing on the line,
3315 ;; add the cperl-brace-imaginary-offset.
3316 (progn (skip-chars-backward " \t")
3317 (if (bolp) 0 cperl-brace-imaginary-offset))
3318 ;; If the openbrace is preceded by a parenthesized exp,
3319 ;; move to the beginning of that;
3320 ;; possibly a different line
3321 (progn
3322 (if (eq (preceding-char) ?\))
3323 (forward-sexp -1))
3324 ;; Get initial indentation of the line we are on.
3325 ;; If line starts with label, calculate label indentation
3326 (if (save-excursion
3327 (beginning-of-line)
3328 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3329 (if (> (current-indentation) cperl-min-label-indent)
3330 (- (current-indentation) cperl-label-offset)
3331 (cperl-calculate-indent))
3332 (current-indentation)))))))))
3333 res)))
f83d2997
KH
3334
3335(defun cperl-calculate-indent-within-comment ()
3336 "Return the indentation amount for line, assuming that
3337the current line is to be regarded as part of a block comment."
3338 (let (end star-start)
3339 (save-excursion
3340 (beginning-of-line)
3341 (skip-chars-forward " \t")
3342 (setq end (point))
3343 (and (= (following-char) ?#)
3344 (forward-line -1)
3345 (cperl-to-comment-or-eol)
3346 (setq end (point)))
3347 (goto-char end)
3348 (current-column))))
3349
3350
3351(defun cperl-to-comment-or-eol ()
029cb4d5 3352 "Go to position before comment on the current line, or to end of line.
4ab89e7b
SM
3353Returns true if comment is found. In POD will not move the point."
3354 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3355 ;; then looks for literal # or end-of-line.
3356 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3357 (or cperl-font-locking
3358 (cperl-update-syntaxification lim lim))
83261a2f 3359 (beginning-of-line)
4ab89e7b
SM
3360 (if (setq pr (get-text-property (point) 'syntax-type))
3361 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3362 (if (or (eq pr 'pod)
3363 (if (or (not e) (> e lim)) ; deep inside a group
3364 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
83261a2f 3365 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
4ab89e7b
SM
3366 ;; Else - need to do it the hard way
3367 (and (and e (<= e lim))
3368 (goto-char e))
83261a2f
SM
3369 (while (not stop-in)
3370 (setq state (parse-partial-sexp (point) lim nil nil nil t))
f83d2997 3371 ; stop at comment
83261a2f
SM
3372 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3373 (if (nth 4 state) ; After `#';
f83d2997
KH
3374 ; (nth 2 state) can be
3375 ; beginning of m,s,qq and so
3376 ; on
83261a2f
SM
3377 (if (nth 2 state)
3378 (progn
3379 (setq cpoint (point))
3380 (goto-char (nth 2 state))
3381 (cond
3382 ((looking-at "\\(s\\|tr\\)\\>")
3383 (or (re-search-forward
3384 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3385 lim 'move)
3386 (setq stop-in t)))
3387 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3388 (or (re-search-forward
3389 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3390 lim 'move)
3391 (setq stop-in t)))
3392 (t ; It was fair comment
3393 (setq stop-in t) ; Finish
3394 (goto-char (1- cpoint)))))
3395 (setq stop-in t) ; Finish
3396 (forward-char -1))
15ca5699 3397 (setq stop-in t))) ; Finish
83261a2f 3398 (nth 4 state))))
f83d2997 3399
f83d2997
KH
3400(defsubst cperl-modify-syntax-type (at how)
3401 (if (< at (point-max))
3402 (progn
3403 (put-text-property at (1+ at) 'syntax-table how)
4ab89e7b 3404 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
f83d2997
KH
3405
3406(defun cperl-protect-defun-start (s e)
3407 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3408 (save-excursion
3409 (goto-char s)
3410 (while (re-search-forward "^\\s(" e 'to-end)
3411 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3412
5bd52f0e 3413(defun cperl-commentify (bb e string &optional noface)
5c8b7eaf 3414 (if cperl-use-syntax-table-text-property
5bd52f0e
RS
3415 (if (eq noface 'n) ; Only immediate
3416 nil
f83d2997
KH
3417 ;; We suppose that e is _after_ the end of construction, as after eol.
3418 (setq string (if string cperl-st-sfence cperl-st-cfence))
6c389151
SM
3419 (if (> bb (- e 2))
3420 ;; one-char string/comment?!
3421 (cperl-modify-syntax-type bb cperl-st-punct)
3422 (cperl-modify-syntax-type bb string)
3423 (cperl-modify-syntax-type (1- e) string))
f83d2997 3424 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
5c8b7eaf 3425 (put-text-property (1+ bb) (1- e)
f83d2997 3426 'syntax-table cperl-string-syntax-table))
5bd52f0e
RS
3427 (cperl-protect-defun-start bb e))
3428 ;; Fontify
3429 (or noface
3430 (not cperl-pod-here-fontify)
3431 (put-text-property bb e 'face (if string 'font-lock-string-face
3432 'font-lock-comment-face)))))
6c389151 3433
5bd52f0e
RS
3434(defvar cperl-starters '(( ?\( . ?\) )
3435 ( ?\[ . ?\] )
3436 ( ?\{ . ?\} )
3437 ( ?\< . ?\> )))
f83d2997 3438
4ab89e7b
SM
3439(defun cperl-cached-syntax-table (st)
3440 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3441All the entries of the syntax table are \".\", except for a backslash, which
3442is quoting."
3443 (if (car-safe st)
3444 (car st)
3445 (setcar st (make-syntax-table))
3446 (setq st (car st))
3447 (let ((i 0))
3448 (while (< i 256)
3449 (modify-syntax-entry i "." st)
3450 (setq i (1+ i))))
3451 (modify-syntax-entry ?\\ "\\" st)
3452 st))
3453
3454(defun cperl-forward-re (lim end is-2arg st-l err-l argument
f83d2997 3455 &optional ostart oend)
4ab89e7b
SM
3456"Find the end of a regular expression or a stringish construct (q[] etc).
3457The point should be before the starting delimiter.
3458
3459Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3460is s/// or tr/// like expression. If END is nil, generates an error
3461message if needed. If SET-ST is non-nil, will use (or generate) a
3462cached syntax table in ST-L. If ERR-L is non-nil, will store the
3463error message in its CAR (unless it already contains some error
3464message). ARGUMENT should be the name of the construct (used in error
3465messages). OSTART, OEND may be set in recursive calls when processing
3466the second argument of 2ARG construct.
3467
3468Works *before* syntax recognition is done. In IS-2ARG situation may
3469modify syntax-type text property if the situation is too hard."
3470 (let (b starter ender st i i2 go-forward reset-st set-st)
f83d2997
KH
3471 (skip-chars-forward " \t")
3472 ;; ender means matching-char matcher.
5c8b7eaf 3473 (setq b (point)
5bd52f0e
RS
3474 starter (if (eobp) 0 (char-after b))
3475 ender (cdr (assoc starter cperl-starters)))
f83d2997 3476 ;; What if starter == ?\\ ????
4ab89e7b 3477 (setq st (cperl-cached-syntax-table st-l))
f83d2997
KH
3478 (setq set-st t)
3479 ;; Whether we have an intermediate point
3480 (setq i nil)
3481 ;; Prepare the syntax table:
4ab89e7b
SM
3482 (if (not ender) ; m/blah/, s/x//, s/x/y/
3483 (modify-syntax-entry starter "$" st)
3484 (modify-syntax-entry starter (concat "(" (list ender)) st)
3485 (modify-syntax-entry ender (concat ")" (list starter)) st))
f83d2997
KH
3486 (condition-case bb
3487 (progn
5bd52f0e
RS
3488 ;; We use `$' syntax class to find matching stuff, but $$
3489 ;; is recognized the same as $, so we need to check this manually.
f83d2997
KH
3490 (if (and (eq starter (char-after (cperl-1+ b)))
3491 (not ender))
3492 ;; $ has TeXish matching rules, so $$ equiv $...
3493 (forward-char 2)
6c389151 3494 (setq reset-st (syntax-table))
f83d2997
KH
3495 (set-syntax-table st)
3496 (forward-sexp 1)
6c389151
SM
3497 (if (<= (point) (1+ b))
3498 (error "Unfinished regular expression"))
3499 (set-syntax-table reset-st)
3500 (setq reset-st nil)
f83d2997
KH
3501 ;; Now the problem is with m;blah;;
3502 (and (not ender)
3503 (eq (preceding-char)
3504 (char-after (- (point) 2)))
3505 (save-excursion
3506 (forward-char -2)
3507 (= 0 (% (skip-chars-backward "\\\\") 2)))
3508 (forward-char -1)))
5bd52f0e 3509 ;; Now we are after the first part.
f83d2997
KH
3510 (and is-2arg ; Have trailing part
3511 (not ender)
3512 (eq (following-char) starter) ; Empty trailing part
3513 (progn
3514 (or (eq (char-syntax (following-char)) ?.)
3515 ;; Make trailing letter into punctuation
3516 (cperl-modify-syntax-type (point) cperl-st-punct))
3517 (setq is-2arg nil go-forward t))) ; Ignore the tail
3518 (if is-2arg ; Not number => have second part
3519 (progn
3520 (setq i (point) i2 i)
3521 (if ender
b5b0cb34 3522 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
f83d2997
KH
3523 (progn
3524 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3525 (goto-char (match-end 0))
3526 (skip-chars-forward " \t\n\f"))
3527 (setq i2 (point))))
3528 (forward-char -1))
3529 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
5c8b7eaf 3530 (if ender (modify-syntax-entry ender "." st))
f83d2997 3531 (setq set-st nil)
4ab89e7b 3532 (setq ender (cperl-forward-re lim end nil st-l err-l
5bd52f0e 3533 argument starter ender)
f83d2997
KH
3534 ender (nth 2 ender)))))
3535 (error (goto-char lim)
3536 (setq set-st nil)
6c389151
SM
3537 (if reset-st
3538 (set-syntax-table reset-st))
f83d2997
KH
3539 (or end
3540 (message
5bd52f0e 3541 "End of `%s%s%c ... %c' string/RE not found: %s"
f83d2997
KH
3542 argument
3543 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3544 starter (or ender starter) bb)
3545 (or (car err-l) (setcar err-l b)))))
3546 (if set-st
3547 (progn
3548 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3549 (if ender (modify-syntax-entry ender "." st))))
5bd52f0e
RS
3550 ;; i: have 2 args, after end of the first arg
3551 ;; i2: start of the second arg, if any (before delim iff `ender').
3552 ;; ender: the last arg bounded by parens-like chars, the second one of them
3553 ;; starter: the starting delimiter of the first arg
5efe6a56 3554 ;; go-forward: has 2 args, and the second part is empty
f83d2997
KH
3555 (list i i2 ender starter go-forward)))
3556
4ab89e7b
SM
3557(defun cperl-forward-group-in-re (&optional st-l)
3558 "Find the end of a group in a REx.
3559Return the error message (if any). Does not work if delimiter is `)'.
3560Works before syntax recognition is done."
3561 ;; Works *before* syntax recognition is done
3562 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3563 (let (st b reset-st)
3564 (condition-case b
3565 (progn
3566 (setq st (cperl-cached-syntax-table st-l))
3567 (modify-syntax-entry ?\( "()" st)
3568 (modify-syntax-entry ?\) ")(" st)
3569 (setq reset-st (syntax-table))
3570 (set-syntax-table st)
3571 (forward-sexp 1))
3572 (error (message
3573 "cperl-forward-group-in-re: error %s" b)))
3574 ;; now restore the initial state
3575 (if st
3576 (progn
3577 (modify-syntax-entry ?\( "." st)
3578 (modify-syntax-entry ?\) "." st)))
3579 (if reset-st
3580 (set-syntax-table reset-st))
3581 b))
3582
3583
83261a2f
SM
3584(defvar font-lock-string-face)
3585;;(defvar font-lock-reference-face)
3586(defvar font-lock-constant-face)
5c8b7eaf 3587(defsubst cperl-postpone-fontification (b e type val &optional now)
5bd52f0e
RS
3588 ;; Do after syntactic fontification?
3589 (if cperl-syntaxify-by-font-lock
3590 (or now (put-text-property b e 'cperl-postpone (cons type val)))
83261a2f 3591 (put-text-property b e type val)))
5bd52f0e
RS
3592
3593;;; Here is how the global structures (those which cannot be
3594;;; recognized locally) are marked:
5c8b7eaf 3595;; a) PODs:
5bd52f0e
RS
3596;; Start-to-end is marked `in-pod' ==> t
3597;; Each non-literal part is marked `syntax-type' ==> `pod'
3598;; Each literal part is marked `syntax-type' ==> `in-pod'
5c8b7eaf 3599;; b) HEREs:
5bd52f0e
RS
3600;; Start-to-end is marked `here-doc-group' ==> t
3601;; The body is marked `syntax-type' ==> `here-doc'
3602;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
5c8b7eaf 3603;; c) FORMATs:
f739b53b
SM
3604;; First line (to =) marked `first-format-line' ==> t
3605;; After-this--to-end is marked `syntax-type' ==> `format'
5c8b7eaf 3606;; d) 'Q'uoted string:
5bd52f0e 3607;; part between markers inclusive is marked `syntax-type' ==> `string'
6c389151 3608;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
4ab89e7b
SM
3609;; second part of s///e is marked `syntax-type' ==> `multiline'
3610;; e) Attributes of subroutines: `attrib-group' ==> t
3611;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3612;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3613
3614;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3615;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
5bd52f0e
RS
3616
3617(defun cperl-unwind-to-safe (before &optional end)
3618 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3619 (let ((pos (point)) opos)
4ab89e7b
SM
3620 (while (and pos (progn
3621 (beginning-of-line)
3622 (get-text-property (setq pos (point)) 'syntax-type)))
3623 (setq opos pos
3624 pos (cperl-beginning-of-property pos 'syntax-type))
3625 (if (eq pos (point-min))
3626 (setq pos nil))
5bd52f0e
RS
3627 (if pos
3628 (if before
3629 (progn
3630 (goto-char (cperl-1- pos))
3631 (beginning-of-line)
3632 (setq pos (point)))
3633 (goto-char (setq pos (cperl-1- pos))))
3634 ;; Up to the start
3635 (goto-char (point-min))))
6c389151
SM
3636 ;; Skip empty lines
3637 (and (looking-at "\n*=")
3638 (/= 0 (skip-chars-backward "\n"))
3639 (forward-char))
3640 (setq pos (point))
5bd52f0e
RS
3641 (if end
3642 ;; Do the same for end, going small steps
4ab89e7b 3643 (save-excursion
5bd52f0e
RS
3644 (while (and end (get-text-property end 'syntax-type))
3645 (setq pos end
4ab89e7b
SM
3646 end (next-single-property-change end 'syntax-type nil (point-max)))
3647 (if end (progn (goto-char end)
3648 (or (bolp) (forward-line 1))
3649 (setq end (point)))))
5bd52f0e
RS
3650 (or end pos)))))
3651
4ab89e7b 3652;;; These are needed for byte-compile (at least with v19)
6c389151 3653(defvar cperl-nonoverridable-face)
4ab89e7b 3654(defvar font-lock-variable-name-face)
6c389151 3655(defvar font-lock-function-name-face)
4ab89e7b
SM
3656(defvar font-lock-keyword-face)
3657(defvar font-lock-builtin-face)
3658(defvar font-lock-type-face)
6c389151 3659(defvar font-lock-comment-face)
4ab89e7b 3660(defvar font-lock-warning-face)
6c389151 3661
4ab89e7b
SM
3662(defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3663 "Syntaxically mark (and fontify) attributes of a subroutine.
3664Should be called with the point before leading colon of an attribute."
3665 ;; Works *before* syntax recognition is done
3666 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3667 (let (st b p reset-st after-first (start (point)) start1 end1)
3668 (condition-case b
3669 (while (looking-at
3670 (concat
3671 "\\(" ; 1=optional? colon
3672 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3673 "\\)"
3674 (if after-first "?" "")
3675 ;; No space between name and paren allowed...
3676 "\\(\\sw+\\)" ; 3=name
3677 "\\((\\)?")) ; 4=optional paren
3678 (and (match-beginning 1)
3679 (cperl-postpone-fontification
3680 (match-beginning 0) (cperl-1+ (match-beginning 0))
3681 'face font-lock-constant-face))
3682 (setq start1 (match-beginning 3) end1 (match-end 3))
3683 (cperl-postpone-fontification start1 end1
3684 'face font-lock-constant-face)
3685 (goto-char end1) ; end or before `('
3686 (if (match-end 4) ; Have attribute arguments...
3687 (progn
3688 (if st nil
3689 (setq st (cperl-cached-syntax-table st-l))
3690 (modify-syntax-entry ?\( "()" st)
3691 (modify-syntax-entry ?\) ")(" st))
3692 (setq reset-st (syntax-table) p (point))
3693 (set-syntax-table st)
3694 (forward-sexp 1)
3695 (set-syntax-table reset-st)
3696 (setq reset-st nil)
3697 (cperl-commentify p (point) t))) ; mark as string
3698 (forward-comment (buffer-size))
3699 (setq after-first t))
3700 (error (message
3701 "L%d: attribute `%s': %s"
3702 (count-lines (point-min) (point))
3703 (and start1 end1 (buffer-substring start1 end1)) b)
3704 (setq start nil)))
3705 (and start
3706 (progn
3707 (put-text-property start (point)
3708 'attrib-group (if (looking-at "{") t 0))
3709 (and pos
3710 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3711 ;; Apparently, we do not need `multiline': faces added now
3712 (put-text-property (+ 3 pos) (cperl-1+ (point))
3713 'syntax-type 'sub-decl))
3714 (and b-fname ; Fontify here: the following condition
3715 (cperl-postpone-fontification ; is too hard to determine by
3716 b-fname e-fname 'face ; a REx, so do it here
3717 (if (looking-at "{")
3718 font-lock-function-name-face
3719 font-lock-variable-name-face)))))
3720 ;; now restore the initial state
3721 (if st
3722 (progn
3723 (modify-syntax-entry ?\( "." st)
3724 (modify-syntax-entry ?\) "." st)))
3725 (if reset-st
3726 (set-syntax-table reset-st))))
3727
3728(defsubst cperl-look-at-leading-count (is-x-REx e)
3729 (if (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3730 (1- e) t) ; return nil on failure, no moving
3731 (if (eq ?\{ (preceding-char)) nil
3732 (cperl-postpone-fontification
3733 (1- (point)) (point)
3734 'face font-lock-warning-face))))
3735
3736;;; Debugging this may require (setq max-specpdl-size 2000)...
3737(defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
f83d2997 3738 "Scans the buffer for hard-to-parse Perl constructions.
5c8b7eaf
SS
3739If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3740the sections using `cperl-pod-head-face', `cperl-pod-face',
f83d2997
KH
3741`cperl-here-face'."
3742 (interactive)
4ab89e7b 3743 (or min (setq min (point-min)
db133cb6
RS
3744 cperl-syntax-state nil
3745 cperl-syntax-done-to min))
f83d2997 3746 (or max (setq max (point-max)))
83261a2f
SM
3747 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3748 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
4ab89e7b 3749 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
83261a2f 3750 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
4ab89e7b 3751 (modified (buffer-modified-p)) overshoot is-o-REx
83261a2f 3752 (after-change-functions nil)
4ab89e7b 3753 (cperl-font-locking t)
83261a2f
SM
3754 (use-syntax-state (and cperl-syntax-state
3755 (>= min (car cperl-syntax-state))))
3756 (state-point (if use-syntax-state
3757 (car cperl-syntax-state)
3758 (point-min)))
3759 (state (if use-syntax-state
3760 (cdr cperl-syntax-state)))
3761 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3762 (st-l (list nil)) (err-l (list nil))
3763 ;; Somehow font-lock may be not loaded yet...
4ab89e7b 3764 ;; (e.g., when building TAGS via command-line call)
83261a2f
SM
3765 (font-lock-string-face (if (boundp 'font-lock-string-face)
3766 font-lock-string-face
3767 'font-lock-string-face))
4ab89e7b 3768 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
83261a2f
SM
3769 font-lock-constant-face
3770 'font-lock-constant-face))
4ab89e7b 3771 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
83261a2f
SM
3772 (if (boundp 'font-lock-function-name-face)
3773 font-lock-function-name-face
3774 'font-lock-function-name-face))
4ab89e7b
SM
3775 (font-lock-variable-name-face ; interpolated vars and ({})-code
3776 (if (boundp 'font-lock-variable-name-face)
3777 font-lock-variable-name-face
3778 'font-lock-variable-name-face))
3779 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3780 (if (boundp 'font-lock-function-name-face)
3781 font-lock-function-name-face
3782 'font-lock-function-name-face))
3783 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3784 (if (boundp 'font-lock-constant-face)
3785 font-lock-constant-face
3786 'font-lock-constant-face))
3787 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3788 (if (boundp 'font-lock-builtin-face)
3789 font-lock-builtin-face
3790 'font-lock-builtin-face))
83261a2f
SM
3791 (font-lock-comment-face
3792 (if (boundp 'font-lock-comment-face)
3793 font-lock-comment-face
3794 'font-lock-comment-face))
4ab89e7b
SM
3795 (font-lock-warning-face
3796 (if (boundp 'font-lock-warning-face)
3797 font-lock-warning-face
3798 'font-lock-warning-face))
3799 (my-cperl-REx-ctl-face ; (|)
3800 (if (boundp 'font-lock-keyword-face)
3801 font-lock-keyword-face
3802 'font-lock-keyword-face))
3803 (my-cperl-REx-modifiers-face ; //gims
83261a2f
SM
3804 (if (boundp 'cperl-nonoverridable-face)
3805 cperl-nonoverridable-face
4ab89e7b
SM
3806 'cperl-nonoverridable-face))
3807 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3808 (if (boundp 'font-lock-type-face)
3809 font-lock-type-face
3810 'font-lock-type-face))
83261a2f
SM
3811 (stop-point (if ignore-max
3812 (point-max)
3813 max))
3814 (search
3815 (concat
4ab89e7b 3816 "\\(\\`\n?\\|^\n\\)=" ; POD
83261a2f
SM
3817 "\\|"
3818 ;; One extra () before this:
4ab89e7b 3819 "<<" ; HERE-DOC
83261a2f
SM
3820 "\\(" ; 1 + 1
3821 ;; First variant "BLAH" or just ``.
3822 "[ \t]*" ; Yes, whitespace is allowed!
3823 "\\([\"'`]\\)" ; 2 + 1 = 3
3824 "\\([^\"'`\n]*\\)" ; 3 + 1
3825 "\\3"
3826 "\\|"
f739b53b 3827 ;; Second variant: Identifier or \ID (same as 'ID') or empty
83261a2f
SM
3828 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3829 ;; Do not have <<= or << 30 or <<30 or << $blah.
3830 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3831 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3832 "\\)"
3833 "\\|"
3834 ;; 1+6 extra () before this:
4ab89e7b 3835 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
83261a2f 3836 (if cperl-use-syntax-table-text-property
db133cb6 3837 (concat
db133cb6 3838 "\\|"
83261a2f 3839 ;; 1+6+2=9 extra () before this:
4ab89e7b 3840 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
83261a2f
SM
3841 "\\|"
3842 ;; 1+6+2+1=10 extra () before this:
3843 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3844 "\\|"
4ab89e7b
SM
3845 ;; 1+6+2+1+1=11 extra () before this
3846 "\\<sub\\>" ; sub with proto/attr
3847 "\\("
3848 cperl-white-and-comment-rex
3849 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3850 "\\("
3851 cperl-maybe-white-and-comment-rex
3852 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
83261a2f 3853 "\\|"
4ab89e7b
SM
3854 ;; 1+6+2+1+1+6=17 extra () before this:
3855 "\\$\\(['{]\\)" ; $' or ${foo}
83261a2f 3856 "\\|"
4ab89e7b
SM
3857 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3858 ;; we do not support intervening comments...):
83261a2f 3859 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
4ab89e7b 3860 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
83261a2f 3861 "\\|"
4ab89e7b
SM
3862 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3863 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
db133cb6 3864 "\\|"
4ab89e7b 3865 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
83261a2f 3866 ""))))
f83d2997
KH
3867 (unwind-protect
3868 (progn
3869 (save-excursion
3870 (or non-inter
3871 (message "Scanning for \"hard\" Perl constructions..."))
4ab89e7b 3872 ;;(message "find: %s --> %s" min max)
db133cb6 3873 (and cperl-pod-here-fontify
83261a2f
SM
3874 ;; We had evals here, do not know why...
3875 (setq face cperl-pod-face
3876 head-face cperl-pod-head-face
3877 here-face cperl-here-face))
5c8b7eaf 3878 (remove-text-properties min max
5bd52f0e 3879 '(syntax-type t in-pod t syntax-table t
4ab89e7b
SM
3880 attrib-group t
3881 REx-interpolated t
6c389151
SM
3882 cperl-postpone t
3883 syntax-subtype t
3884 rear-nonsticky t
4ab89e7b 3885 front-sticky t
f739b53b
SM
3886 here-doc-group t
3887 first-format-line t
4ab89e7b 3888 REx-part2 t
6c389151 3889 indentable t))
f83d2997
KH
3890 ;; Need to remove face as well...
3891 (goto-char min)
db133cb6 3892 (and (eq system-type 'emx)
4ab89e7b
SM
3893 (eq (point) 1)
3894 (let ((case-fold-search t))
3895 (looking-at "extproc[ \t]")) ; Analogue of #!
5c8b7eaf 3896 (cperl-commentify min
db133cb6
RS
3897 (save-excursion (end-of-line) (point))
3898 nil))
3899 (while (and
3900 (< (point) max)
3901 (re-search-forward search max t))
5bd52f0e 3902 (setq tmpend nil) ; Valid for most cases
4ab89e7b
SM
3903 (setq b (match-beginning 0)
3904 state (save-excursion (parse-partial-sexp
3905 state-point b nil nil state))
3906 state-point b)
5c8b7eaf 3907 (cond
4ab89e7b
SM
3908 ;; 1+6+2+1+1+6=17 extra () before this:
3909 ;; "\\$\\(['{]\\)"
3910 ((match-beginning 18) ; $' or ${foo}
3911 (if (eq (preceding-char) ?\') ; $'
3912 (progn
3913 (setq b (1- (point))
3914 state (parse-partial-sexp
3915 state-point (1- b) nil nil state)
3916 state-point (1- b))
3917 (if (nth 3 state) ; in string
3918 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3919 (goto-char (1+ b)))
3920 ;; else: ${
3921 (setq bb (match-beginning 0))
3922 (cperl-modify-syntax-type bb cperl-st-punct)))
3923 ;; No processing in strings/comments beyond this point:
3924 ((or (nth 3 state) (nth 4 state))
3925 t) ; Do nothing in comment/string
f83d2997 3926 ((match-beginning 1) ; POD section
a1506d29 3927 ;; "\\(\\`\n?\\|^\n\\)="
4ab89e7b
SM
3928 (setq b (match-beginning 0)
3929 state (parse-partial-sexp
3930 state-point b nil nil state)
3931 state-point b)
3932 (if (or (nth 3 state) (nth 4 state)
3933 (looking-at "cut\\>"))
3934 (if (or (nth 3 state) (nth 4 state) ignore-max)
5bd52f0e 3935 nil ; Doing a chunk only
f83d2997
KH
3936 (message "=cut is not preceded by a POD section")
3937 (or (car err-l) (setcar err-l (point))))
3938 (beginning-of-line)
5c8b7eaf
SS
3939
3940 (setq b (point)
5bd52f0e
RS
3941 bb b
3942 tb (match-beginning 0)
3943 b1 nil) ; error condition
db133cb6
RS
3944 ;; We do not search to max, since we may be called from
3945 ;; some hook of fontification, and max is random
6c389151 3946 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
f83d2997 3947 (progn
6c389151
SM
3948 (goto-char b)
3949 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3950 (progn
3951 (message "=cut is not preceded by an empty line")
3952 (setq b1 t)
3953 (or (car err-l) (setcar err-l b))))))
f83d2997
KH
3954 (beginning-of-line 2) ; An empty line after =cut is not POD!
3955 (setq e (point))
db133cb6 3956 (and (> e max)
6c389151 3957 (progn
a1506d29 3958 (remove-text-properties
6c389151 3959 max e '(syntax-type t in-pod t syntax-table t
4ab89e7b
SM
3960 attrib-group t
3961 REx-interpolated t
6c389151
SM
3962 cperl-postpone t
3963 syntax-subtype t
f739b53b 3964 here-doc-group t
6c389151 3965 rear-nonsticky t
4ab89e7b 3966 front-sticky t
f739b53b 3967 first-format-line t
4ab89e7b 3968 REx-part2 t
6c389151
SM
3969 indentable t))
3970 (setq tmpend tb)))
f83d2997 3971 (put-text-property b e 'in-pod t)
6c389151 3972 (put-text-property b e 'syntax-type 'in-pod)
f83d2997
KH
3973 (goto-char b)
3974 (while (re-search-forward "\n\n[ \t]" e t)
3975 ;; We start 'pod 1 char earlier to include the preceding line
3976 (beginning-of-line)
3977 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
5efe6a56
SM
3978 (cperl-put-do-not-fontify b (point) t)
3979 ;; mark the non-literal parts as PODs
a1506d29 3980 (if cperl-pod-here-fontify
5efe6a56 3981 (cperl-postpone-fontification b (point) 'face face t))
f83d2997
KH
3982 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3983 (beginning-of-line)
3984 (setq b (point)))
3985 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
5efe6a56 3986 (cperl-put-do-not-fontify (point) e t)
a1506d29
JB
3987 (if cperl-pod-here-fontify
3988 (progn
5efe6a56
SM
3989 ;; mark the non-literal parts as PODs
3990 (cperl-postpone-fontification (point) e 'face face t)
3991 (goto-char bb)
a1506d29 3992 (if (looking-at
5efe6a56
SM
3993 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3994 ;; mark the headers
a1506d29 3995 (cperl-postpone-fontification
5efe6a56 3996 (match-beginning 1) (match-end 1)
6c389151
SM
3997 'face head-face))
3998 (while (re-search-forward
3999 ;; One paragraph
4000 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
4001 e 'toend)
4002 ;; mark the headers
a1506d29 4003 (cperl-postpone-fontification
5efe6a56
SM
4004 (match-beginning 1) (match-end 1)
4005 'face head-face))))
f83d2997
KH
4006 (cperl-commentify bb e nil)
4007 (goto-char e)
4008 (or (eq e (point-max))
83261a2f 4009 (forward-char -1)))) ; Prepare for immediate POD start.
f83d2997 4010 ;; Here document
4ab89e7b
SM
4011 ;; We can do many here-per-line;
4012 ;; but multiline quote on the same line as <<HERE confuses us...
5bd52f0e 4013 ;; ;; One extra () before this:
5c8b7eaf 4014 ;;"<<"
5bd52f0e
RS
4015 ;; "\\(" ; 1 + 1
4016 ;; ;; First variant "BLAH" or just ``.
f739b53b 4017 ;; "[ \t]*" ; Yes, whitespace is allowed!
5bd52f0e
RS
4018 ;; "\\([\"'`]\\)" ; 2 + 1
4019 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
4020 ;; "\\3"
4021 ;; "\\|"
4022 ;; ;; Second variant: Identifier or \ID or empty
4023 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
4024 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
4025 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
4026 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
4027 ;; "\\)"
f83d2997 4028 ((match-beginning 2) ; 1 + 1
4ab89e7b 4029 (setq b (point)
5bd52f0e 4030 tb (match-beginning 0)
4ab89e7b
SM
4031 c (and ; not HERE-DOC
4032 (match-beginning 5)
4033 (save-match-data
4034 (or (looking-at "[ \t]*(") ; << function_call()
4035 (save-excursion ; 1 << func_name, or $foo << 10
4036 (condition-case nil
4037 (progn
4038 (goto-char tb)
4039 ;;; XXX What to do: foo <<bar ???
4040 ;;; XXX Need to support print {a} <<B ???
4041 (forward-sexp -1)
4042 (save-match-data
4043 ; $foo << b; $f .= <<B;
4044 ; ($f+1) << b; a($f) . <<B;
4045 ; foo 1, <<B; $x{a} <<b;
4046 (cond
4047 ((looking-at "[0-9$({]")
4048 (forward-sexp 1)
4049 (and
4050 (looking-at "[ \t]*<<")
4051 (condition-case nil
4052 ;; print $foo <<EOF
4053 (progn
4054 (forward-sexp -2)
4055 (not
4056 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
4057 (error t)))))))
4058 (error nil))) ; func(<<EOF)
4059 (and (not (match-beginning 6)) ; Empty
4060 (looking-at
4061 "[ \t]*[=0-9$@%&(]"))))))
5bd52f0e
RS
4062 (if c ; Not here-doc
4063 nil ; Skip it.
4ab89e7b 4064 (setq c (match-end 2)) ; 1 + 1
f83d2997
KH
4065 (if (match-beginning 5) ;4 + 1
4066 (setq b1 (match-beginning 5) ; 4 + 1
4067 e1 (match-end 5)) ; 4 + 1
4068 (setq b1 (match-beginning 4) ; 3 + 1
4069 e1 (match-end 4))) ; 3 + 1
4070 (setq tag (buffer-substring b1 e1)
4071 qtag (regexp-quote tag))
5c8b7eaf 4072 (cond (cperl-pod-here-fontify
5bd52f0e 4073 ;; Highlight the starting delimiter
4ab89e7b
SM
4074 (cperl-postpone-fontification
4075 b1 e1 'face my-cperl-delimiters-face)
5bd52f0e 4076 (cperl-put-do-not-fontify b1 e1 t)))
f83d2997 4077 (forward-line)
4ab89e7b
SM
4078 (setq i (point))
4079 (if end-of-here-doc
4080 (goto-char end-of-here-doc))
f83d2997 4081 (setq b (point))
db133cb6
RS
4082 ;; We do not search to max, since we may be called from
4083 ;; some hook of fontification, and max is random
f739b53b
SM
4084 (or (and (re-search-forward (concat "^" qtag "$")
4085 stop-point 'toend)
4ab89e7b
SM
4086 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4087 )
f739b53b
SM
4088 (progn ; Pretend we matched at the end
4089 (goto-char (point-max))
4090 (re-search-forward "\\'")
4091 (message "End of here-document `%s' not found." tag)
4092 (or (car err-l) (setcar err-l b))))
4093 (if cperl-pod-here-fontify
4094 (progn
4095 ;; Highlight the ending delimiter
4ab89e7b
SM
4096 (cperl-postpone-fontification
4097 (match-beginning 0) (match-end 0)
4098 'face my-cperl-delimiters-face)
f739b53b
SM
4099 (cperl-put-do-not-fontify b (match-end 0) t)
4100 ;; Highlight the HERE-DOC
4101 (cperl-postpone-fontification b (match-beginning 0)
4102 'face here-face)))
4103 (setq e1 (cperl-1+ (match-end 0)))
4104 (put-text-property b (match-beginning 0)
4105 'syntax-type 'here-doc)
4106 (put-text-property (match-beginning 0) e1
4107 'syntax-type 'here-doc-delim)
4ab89e7b
SM
4108 (put-text-property b e1 'here-doc-group t)
4109 ;; This makes insertion at the start of HERE-DOC update
4110 ;; the whole construct:
4111 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
f739b53b
SM
4112 (cperl-commentify b e1 nil)
4113 (cperl-put-do-not-fontify b (match-end 0) t)
4ab89e7b
SM
4114 ;; Cache the syntax info...
4115 (setq cperl-syntax-state (cons state-point state))
4116 ;; ... and process the rest of the line...
4117 (setq overshoot
4118 (elt ; non-inter ignore-max
4119 (cperl-find-pods-heres c i t end t e1) 1))
4120 (if (and overshoot (> overshoot (point)))
4121 (goto-char overshoot)
4122 (setq overshoot e1))
f739b53b
SM
4123 (if (> e1 max)
4124 (setq tmpend tb))))
f83d2997
KH
4125 ;; format
4126 ((match-beginning 8)
4127 ;; 1+6=7 extra () before this:
4128 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4129 (setq b (point)
4130 name (if (match-beginning 8) ; 7 + 1
4131 (buffer-substring (match-beginning 8) ; 7 + 1
4132 (match-end 8)) ; 7 + 1
5bd52f0e
RS
4133 "")
4134 tb (match-beginning 0))
f83d2997 4135 (setq argument nil)
f739b53b
SM
4136 (put-text-property (save-excursion
4137 (beginning-of-line)
4138 (point))
4139 b 'first-format-line 't)
5c8b7eaf 4140 (if cperl-pod-here-fontify
f83d2997
KH
4141 (while (and (eq (forward-line) 0)
4142 (not (looking-at "^[.;]$")))
4143 (cond
4144 ((looking-at "^#")) ; Skip comments
4145 ((and argument ; Skip argument multi-lines
5c8b7eaf 4146 (looking-at "^[ \t]*{"))
f83d2997
KH
4147 (forward-sexp 1)
4148 (setq argument nil))
4149 (argument ; Skip argument lines
4150 (setq argument nil))
4151 (t ; Format line
4152 (setq b1 (point))
4153 (setq argument (looking-at "^[^\n]*[@^]"))
4154 (end-of-line)
5bd52f0e 4155 ;; Highlight the format line
5c8b7eaf 4156 (cperl-postpone-fontification b1 (point)
83261a2f 4157 'face font-lock-string-face)
f83d2997 4158 (cperl-commentify b1 (point) nil)
5bd52f0e 4159 (cperl-put-do-not-fontify b1 (point) t))))
db133cb6
RS
4160 ;; We do not search to max, since we may be called from
4161 ;; some hook of fontification, and max is random
4162 (re-search-forward "^[.;]$" stop-point 'toend))
f83d2997 4163 (beginning-of-line)
83261a2f 4164 (if (looking-at "^\\.$") ; ";" is not supported yet
f83d2997 4165 (progn
5bd52f0e
RS
4166 ;; Highlight the ending delimiter
4167 (cperl-postpone-fontification (point) (+ (point) 2)
83261a2f 4168 'face font-lock-string-face)
f83d2997 4169 (cperl-commentify (point) (+ (point) 2) nil)
5bd52f0e 4170 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
f83d2997
KH
4171 (message "End of format `%s' not found." name)
4172 (or (car err-l) (setcar err-l b)))
4173 (forward-line)
5bd52f0e
RS
4174 (if (> (point) max)
4175 (setq tmpend tb))
db133cb6 4176 (put-text-property b (point) 'syntax-type 'format))
4ab89e7b 4177 ;; qq-like String or Regexp:
f83d2997
KH
4178 ((or (match-beginning 10) (match-beginning 11))
4179 ;; 1+6+2=9 extra () before this:
5bd52f0e 4180 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
f83d2997 4181 ;; "\\|"
5bd52f0e 4182 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
f83d2997
KH
4183 (setq b1 (if (match-beginning 10) 10 11)
4184 argument (buffer-substring
4185 (match-beginning b1) (match-end b1))
4ab89e7b 4186 b (point) ; end of qq etc
f83d2997
KH
4187 i b
4188 c (char-after (match-beginning b1))
4ab89e7b 4189 bb (char-after (1- (match-beginning b1))) ; tmp holder
5bd52f0e
RS
4190 ;; bb == "Not a stringy"
4191 bb (if (eq b1 10) ; user variables/whatever
f739b53b
SM
4192 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4193 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4194 ((eq bb ?\:) ; $opt::s
4195 (eq (char-after
4196 (- (match-beginning b1) 2))
4197 ?\:))
4198 ((eq bb ?\>) ; $foo->s
4199 (eq (char-after
4200 (- (match-beginning b1) 2))
4201 ?\-))
4202 ((eq bb ?\&)
4ab89e7b 4203 (not (eq (char-after ; &&m/blah/
f739b53b
SM
4204 (- (match-beginning b1) 2))
4205 ?\&)))
4206 (t t)))
5bd52f0e
RS
4207 ;; <file> or <$file>
4208 (and (eq c ?\<)
6c389151 4209 ;; Do not stringify <FH>, <$fh> :
5bd52f0e 4210 (save-match-data
5c8b7eaf 4211 (looking-at
6c389151 4212 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
5bd52f0e 4213 tb (match-beginning 0))
db133cb6
RS
4214 (goto-char (match-beginning b1))
4215 (cperl-backward-to-noncomment (point-min))
f83d2997 4216 (or bb
5bd52f0e 4217 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
f83d2997 4218 (setq argument ""
f739b53b 4219 b1 nil
db133cb6 4220 bb ; Not a regexp?
4ab89e7b
SM
4221 (not
4222 ;; What is below: regexp-p?
4223 (and
4224 (or (memq (preceding-char)
4225 (append (if (memq c '(?\? ?\<))
4226 ;; $a++ ? 1 : 2
4227 "~{(=|&*!,;:["
4228 "~{(=|&+-*!,;:[") nil))
4229 (and (eq (preceding-char) ?\})
4230 (cperl-after-block-p (point-min)))
4231 (and (eq (char-syntax (preceding-char)) ?w)
4232 (progn
4233 (forward-sexp -1)
6c389151
SM
4234;; After these keywords `/' starts a RE. One should add all the
4235;; functions/builtins which expect an argument, but ...
4ab89e7b
SM
4236 (if (eq (preceding-char) ?-)
4237 ;; -d ?foo? is a RE
4238 (looking-at "[a-zA-Z]\\>")
4239 (and
4240 (not (memq (preceding-char)
4241 '(?$ ?@ ?& ?%)))
4242 (looking-at
4243 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4244 (and (eq (preceding-char) ?.)
4245 (eq (char-after (- (point) 2)) ?.))
4246 (bobp))
4247 ;; m|blah| ? foo : bar;
4248 (not
4249 (and (eq c ?\?)
4250 cperl-use-syntax-table-text-property
4251 (not (bobp))
4252 (progn
4253 (forward-char -1)
4254 (looking-at "\\s|"))))))
db133cb6
RS
4255 b (1- b))
4256 ;; s y tr m
f739b53b
SM
4257 ;; Check for $a -> y
4258 (setq b1 (preceding-char)
4259 go (point))
4260 (if (and (eq b1 ?>)
4261 (eq (char-after (- go 2)) ?-))
db133cb6
RS
4262 ;; Not a regexp
4263 (setq bb t))))
f739b53b
SM
4264 (or bb
4265 (progn
4ab89e7b 4266 (goto-char b)
f739b53b
SM
4267 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4268 (goto-char (match-end 0))
4269 (skip-chars-forward " \t\n\f"))
4270 (cond ((and (eq (following-char) ?\})
4271 (eq b1 ?\{))
4272 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4273 (goto-char (1- go))
4274 (skip-chars-backward " \t\n\f")
4275 (if (memq (preceding-char) (append "$@%&*" nil))
4276 (setq bb t) ; @{y}
4277 (condition-case nil
4278 (forward-sexp -1)
4279 (error nil)))
4280 (if (or bb
4281 (looking-at ; $foo -> {s}
4282 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4283 (and ; $foo[12] -> {s}
4284 (memq (following-char) '(?\{ ?\[))
4285 (progn
4286 (forward-sexp 1)
4287 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4288 (setq bb t)
4289 (goto-char b)))
4290 ((and (eq (following-char) ?=)
4291 (eq (char-after (1+ (point))) ?\>))
4292 ;; Check for { foo => 1, s => 2 }
4293 ;; Apparently s=> is never a substitution...
4294 (setq bb t))
4295 ((and (eq (following-char) ?:)
4296 (eq b1 ?\{) ; Check for $ { s::bar }
4297 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
15ca5699 4298 (progn
f739b53b
SM
4299 (goto-char (1- go))
4300 (skip-chars-backward " \t\n\f")
4301 (memq (preceding-char)
4302 (append "$@%&*" nil))))
4ab89e7b
SM
4303 (setq bb t))
4304 ((eobp)
f739b53b
SM
4305 (setq bb t)))))
4306 (if bb
f83d2997 4307 (goto-char i)
6c389151 4308 ;; Skip whitespace and comments...
f83d2997
KH
4309 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4310 (goto-char (match-end 0))
4311 (skip-chars-forward " \t\n\f"))
6c389151
SM
4312 (if (> (point) b)
4313 (put-text-property b (point) 'syntax-type 'prestring))
f83d2997
KH
4314 ;; qtag means two-arg matcher, may be reset to
4315 ;; 2 or 3 later if some special quoting is needed.
4316 ;; e1 means matching-char matcher.
4ab89e7b 4317 (setq b (point) ; before the first delimiter
5bd52f0e
RS
4318 ;; has 2 args
4319 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
db133cb6
RS
4320 ;; We do not search to max, since we may be called from
4321 ;; some hook of fontification, and max is random
4322 i (cperl-forward-re stop-point end
5bd52f0e 4323 i2
4ab89e7b
SM
4324 st-l err-l argument)
4325 ;; If `go', then it is considered as 1-arg, `b1' is nil
4326 ;; as in s/foo//x; the point is before final "slash"
5bd52f0e 4327 b1 (nth 1 i) ; start of the second part
5c8b7eaf 4328 tag (nth 2 i) ; ender-char, true if second part
5bd52f0e 4329 ; is with matching chars []
f83d2997
KH
4330 go (nth 4 i) ; There is a 1-char part after the end
4331 i (car i) ; intermediate point
5c8b7eaf 4332 e1 (point) ; end
5bd52f0e 4333 ;; Before end of the second part if non-matching: ///
5c8b7eaf 4334 tail (if (and i (not tag))
5bd52f0e
RS
4335 (1- e1))
4336 e (if i i e1) ; end of the first part
6c389151 4337 qtag nil ; need to preserve backslashitis
4ab89e7b
SM
4338 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4339 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
f83d2997
KH
4340 ;; Commenting \\ is dangerous, what about ( ?
4341 (and i tail
4342 (eq (char-after i) ?\\)
5bd52f0e 4343 (setq qtag t))
4ab89e7b
SM
4344 (and (if go (looking-at ".\\sw*x")
4345 (looking-at "\\sw*x")) ; qr//x
4346 (setq is-x-REx t))
4347 (and (if go (looking-at ".\\sw*o")
4348 (looking-at "\\sw*o")) ; //o
4349 (setq is-o-REx t))
f83d2997 4350 (if (null i)
5bd52f0e 4351 ;; Considered as 1arg form
f83d2997
KH
4352 (progn
4353 (cperl-commentify b (point) t)
5bd52f0e 4354 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
4355 (if (or is-x-REx
4356 ;; ignore other text properties:
4357 (string-match "^qw$" argument))
4358 (put-text-property b (point) 'indentable t))
5bd52f0e
RS
4359 (and go
4360 (setq e1 (cperl-1+ e1))
4361 (or (eobp)
4362 (forward-char 1))))
f83d2997
KH
4363 (cperl-commentify b i t)
4364 (if (looking-at "\\sw*e") ; s///e
4365 (progn
4ab89e7b
SM
4366 ;; Cache the syntax info...
4367 (setq cperl-syntax-state (cons state-point state))
f83d2997
KH
4368 (and
4369 ;; silent:
4ab89e7b 4370 (car (cperl-find-pods-heres b1 (1- (point)) t end))
f83d2997
KH
4371 ;; Error
4372 (goto-char (1+ max)))
5bd52f0e 4373 (if (and tag (eq (preceding-char) ?\>))
f83d2997
KH
4374 (progn
4375 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
5bd52f0e 4376 (cperl-modify-syntax-type i cperl-st-bra)))
6c389151 4377 (put-text-property b i 'syntax-type 'string)
4ab89e7b 4378 (put-text-property i (point) 'syntax-type 'multiline)
6c389151
SM
4379 (if is-x-REx
4380 (put-text-property b i 'indentable t)))
5bd52f0e
RS
4381 (cperl-commentify b1 (point) t)
4382 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
4383 (if is-x-REx
4384 (put-text-property b i 'indentable t))
5bd52f0e 4385 (if qtag
db133cb6 4386 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
f83d2997 4387 (setq tail nil)))
5bd52f0e 4388 ;; Now: tail: if the second part is non-matching without ///e
f83d2997
KH
4389 (if (eq (char-syntax (following-char)) ?w)
4390 (progn
4391 (forward-word 1) ; skip modifiers s///s
5bd52f0e 4392 (if tail (cperl-commentify tail (point) t))
a1506d29 4393 (cperl-postpone-fontification
4ab89e7b 4394 e1 (point) 'face my-cperl-REx-modifiers-face)))
5bd52f0e
RS
4395 ;; Check whether it is m// which means "previous match"
4396 ;; and highlight differently
a1506d29 4397 (setq is-REx
6c389151
SM
4398 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4399 (or (not (= (length argument) 0))
4400 (not (eq c ?\<)))))
a1506d29 4401 (if (and is-REx
6c389151 4402 (eq e (+ 2 b))
5bd52f0e
RS
4403 ;; split // *is* using zero-pattern
4404 (save-excursion
4405 (condition-case nil
4406 (progn
4407 (goto-char tb)
4408 (forward-sexp -1)
4409 (not (looking-at "split\\>")))
4410 (error t))))
5c8b7eaf 4411 (cperl-postpone-fontification
4ab89e7b 4412 b e 'face font-lock-warning-face)
5bd52f0e
RS
4413 (if (or i2 ; Has 2 args
4414 (and cperl-fontify-m-as-s
4415 (or
4416 (string-match "^\\(m\\|qr\\)$" argument)
4417 (and (eq 0 (length argument))
4418 (not (eq ?\< (char-after b)))))))
4419 (progn
5c8b7eaf 4420 (cperl-postpone-fontification
4ab89e7b 4421 b (cperl-1+ b) 'face my-cperl-delimiters-face)
5c8b7eaf 4422 (cperl-postpone-fontification
4ab89e7b 4423 (1- e) e 'face my-cperl-delimiters-face)))
6c389151 4424 (if (and is-REx cperl-regexp-scan)
4ab89e7b
SM
4425 ;; Process RExen: embedded comments, charclasses and ]
4426;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4427;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4428;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4429;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4430;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4431;;;m^a[\^b]c^ + m.a[^b]\.c.;
6c389151
SM
4432 (save-excursion
4433 (goto-char (1+ b))
4ab89e7b
SM
4434 ;; First
4435 (cperl-look-at-leading-count is-x-REx e)
4436 (setq hairy-RE
4437 (concat
4438 (if is-x-REx
4439 (if (eq (char-after b) ?\#)
4440 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4441 "\\((\\?#\\)\\|\\(#\\)")
4442 ;; keep the same count: add a fake group
4443 (if (eq (char-after b) ?\#)
4444 "\\((\\?\\\\#\\)\\(\\)"
4445 "\\((\\?#\\)\\(\\)"))
4446 "\\|"
4447 "\\(\\[\\)" ; 3=[
4448 "\\|"
4449 "\\(]\\)" ; 4=]
4450 "\\|"
4451 ;; XXXX Will not be able to use it in s)))
4452 (if (eq (char-after b) ?\) )
4453 "\\())))\\)" ; Will never match
4454 (if (eq (char-after b) ?? )
4455 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4456 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4457 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4458 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4459 "\\(" ;; XXXX 1-char variables, exc. |()\s
4460 "[$@]"
4461 "\\("
4462 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4463 "\\|"
4464 "{[^{}]*}" ; only one-level allowed
4465 "\\|"
4466 "[^{(|) \t\r\n\f]"
4467 "\\)"
4468 "\\(" ;;8,9:code part of array/hash elt
4469 "\\(" "->" "\\)?"
4470 "\\[[^][]*\\]"
4471 "\\|"
4472 "{[^{}]*}"
4473 "\\)*"
4474 ;; XXXX: what if u is delim?
4475 "\\|"
4476 "[)^|$.*?+]"
4477 "\\|"
4478 "{[0-9]+}"
4479 "\\|"
4480 "{[0-9]+,[0-9]*}"
4481 "\\|"
4482 "\\\\[luLUEQbBAzZG]"
4483 "\\|"
4484 "(" ; Group opener
4485 "\\(" ; 10 group opener follower
4486 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4487 "\\|"
4488 "\\?[:=!>?{]" ; "?" something
4489 "\\|"
4490 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4491 "\\|"
4492 "\\?([0-9]+)" ; (?(1)foo|bar)
4493 "\\|"
4494 "\\?<[=!]"
4495 ;;;"\\|"
4496 ;;; "\\?"
4497 "\\)?"
4498 "\\)"
4499 "\\|"
4500 "\\\\\\(.\\)" ; 12=\SYMBOL
4501 ))
6c389151 4502 (while
4ab89e7b
SM
4503 (and (< (point) (1- e))
4504 (re-search-forward hairy-RE (1- e) 'to-end))
6c389151 4505 (goto-char (match-beginning 0))
4ab89e7b
SM
4506 (setq REx-subgr-start (point)
4507 was-subgr (following-char))
4508 (cond
4509 ((match-beginning 6) ; 0-length builtins, groups
4510 (goto-char (match-end 0))
4511 (if (match-beginning 11)
4512 (goto-char (match-beginning 11)))
4513 (if (>= (point) e)
4514 (goto-char (1- e)))
4515 (cperl-postpone-fontification
4516 (match-beginning 0) (point)
4517 'face
4518 (cond
4519 ((eq was-subgr ?\) )
4520 (condition-case nil
4521 (save-excursion
4522 (forward-sexp -1)
4523 (if (> (point) b)
4524 (if (if (eq (char-after b) ?? )
4525 (looking-at "(\\\\\\?")
4526 (eq (char-after (1+ (point))) ?\?))
4527 my-cperl-REx-0length-face
4528 my-cperl-REx-ctl-face)
4529 font-lock-warning-face))
4530 (error font-lock-warning-face)))
4531 ((eq was-subgr ?\| )
4532 my-cperl-REx-ctl-face)
4533 ((eq was-subgr ?\$ )
4534 (if (> (point) (1+ REx-subgr-start))
4535 (progn
4536 (put-text-property
4537 (match-beginning 0) (point)
4538 'REx-interpolated
4539 (if is-o-REx 0
4540 (if (and (eq (match-beginning 0)
4541 (1+ b))
4542 (eq (point)
4543 (1- e))) 1 t)))
4544 font-lock-variable-name-face)
4545 my-cperl-REx-spec-char-face))
4546 ((memq was-subgr (append "^." nil) )
4547 my-cperl-REx-spec-char-face)
4548 ((eq was-subgr ?\( )
4549 (if (not (match-beginning 10))
4550 my-cperl-REx-ctl-face
4551 my-cperl-REx-0length-face))
4552 (t my-cperl-REx-0length-face)))
4553 (if (and (memq was-subgr (append "(|" nil))
4554 (not (string-match "(\\?[-imsx]+)"
4555 (match-string 0))))
4556 (cperl-look-at-leading-count is-x-REx e))
4557 (setq was-subgr nil)) ; We do stuff here
4558 ((match-beginning 12) ; \SYMBOL
4559 (forward-char 2)
4560 (if (>= (point) e)
4561 (goto-char (1- e))
4562 ;; How many chars to not highlight:
4563 ;; 0-len special-alnums in other branch =>
4564 ;; Generic: \non-alnum (1), \alnum (1+face)
4565 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4566 (setq REx-subgr-start (point)
4567 qtag (preceding-char))
4568 (cperl-postpone-fontification
4569 (- (point) 2) (- (point) 1) 'face
4570 (if (memq qtag
4571 (append "ghijkmoqvFHIJKMORTVY" nil))
4572 font-lock-warning-face
4573 my-cperl-REx-0length-face))
4574 (if (and (eq (char-after b) qtag)
4575 (memq qtag (append ".])^$|*?+" nil)))
4576 (progn
4577 (if (and cperl-use-syntax-table-text-property
4578 (eq qtag ?\) ))
4579 (put-text-property
4580 REx-subgr-start (1- (point))
4581 'syntax-table cperl-st-punct))
4582 (cperl-postpone-fontification
4583 (1- (point)) (point) 'face
4584 ; \] can't appear below
4585 (if (memq qtag (append ".]^$" nil))
4586 'my-cperl-REx-spec-char-face
4587 (if (memq qtag (append "*?+" nil))
4588 'my-cperl-REx-0length-face
4589 'my-cperl-REx-ctl-face))))) ; )|
4590 ;; Test for arguments:
4591 (cond
4592 ;; This is not pretty: the 5.8.7 logic:
4593 ;; \0numx -> octal (up to total 3 dig)
4594 ;; \DIGIT -> backref unless \0
4595 ;; \DIGITs -> backref if legal
4596 ;; otherwise up to 3 -> octal
4597 ;; Do not try to distinguish, we guess
4598 ((or (and (memq qtag (append "01234567" nil))
4599 (re-search-forward
4600 "\\=[01234567]?[01234567]?"
4601 (1- e) 'to-end))
4602 (and (memq qtag (append "89" nil))
4603 (re-search-forward
4604 "\\=[0123456789]*" (1- e) 'to-end))
4605 (and (eq qtag ?x)
4606 (re-search-forward
4607 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4608 (1- e) 'to-end))
4609 (and (memq qtag (append "pPN" nil))
4610 (re-search-forward "\\={[^{}]+}\\|."
4611 (1- e) 'to-end))
4612 (eq (char-syntax qtag) ?w))
4613 (cperl-postpone-fontification
4614 (1- REx-subgr-start) (point)
4615 'face my-cperl-REx-length1-face))))
4616 (setq was-subgr nil)) ; We do stuff here
4617 ((match-beginning 3) ; [charclass]
4618 (forward-char 1)
4619 (if (eq (char-after b) ?^ )
4620 (and (eq (following-char) ?\\ )
4621 (eq (char-after (cperl-1+ (point)))
4622 ?^ )
4623 (forward-char 2))
4624 (and (eq (following-char) ?^ )
4625 (forward-char 1)))
4626 (setq argument b ; continue?
4627 tag nil ; list of POSIX classes
4628 qtag (point))
4629 (if (eq (char-after b) ?\] )
4630 (and (eq (following-char) ?\\ )
4631 (eq (char-after (cperl-1+ (point)))
4632 ?\] )
4633 (setq qtag (1+ qtag))
4634 (forward-char 2))
4635 (and (eq (following-char) ?\] )
4636 (forward-char 1)))
4637 ;; Apparently, I can't put \] into a charclass
4638 ;; in m]]: m][\\\]\]] produces [\\]]
4639;;; POSIX? [:word:] [:^word:] only inside []
4640;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4641 (while
4642 (and argument
4643 (re-search-forward
4644 (if (eq (char-after b) ?\] )
4645 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4646 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4647 (1- e) 'toend))
4648 ;; Is this ] an end of POSIX class?
4649 (if (save-excursion
4650 (and
4651 (search-backward "[" argument t)
4652 (< REx-subgr-start (point))
4653 (not
4654 (and ; Should work with delim = \
4655 (eq (preceding-char) ?\\ )
4656 (= (% (skip-chars-backward
4657 "\\\\") 2) 0)))
4658 (looking-at
4659 (cond
4660 ((eq (char-after b) ?\] )
4661 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4662 ((eq (char-after b) ?\: )
4663 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4664 ((eq (char-after b) ?^ )
4665 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4666 ((eq (char-syntax (char-after b))
4667 ?w)
4668 (concat
4669 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4670 (char-to-string (char-after b))
4671 "\\|\\sw\\)+:\]"))
4672 (t "\\\\*\\[:\\^?\\sw*:]")))
4673 (setq argument (point))))
4674 (setq tag (cons (cons argument (point))
4675 tag)
4676 argument (point)) ; continue
4677 (setq argument nil)))
4678 (and argument
4679 (message "Couldn't find end of charclass in a REx, pos=%s"
4680 REx-subgr-start))
4681 (if (and cperl-use-syntax-table-text-property
4682 (> (- (point) 2) REx-subgr-start))
4683 (put-text-property
4684 (1+ REx-subgr-start) (1- (point))
4685 'syntax-table cperl-st-punct))
4686 (cperl-postpone-fontification
4687 REx-subgr-start qtag
4688 'face my-cperl-REx-spec-char-face)
4689 (cperl-postpone-fontification
4690 (1- (point)) (point) 'face
4691 my-cperl-REx-spec-char-face)
4692 (if (eq (char-after b) ?\] )
4693 (cperl-postpone-fontification
4694 (- (point) 2) (1- (point))
4695 'face my-cperl-REx-0length-face))
4696 (while tag
4697 (cperl-postpone-fontification
4698 (car (car tag)) (cdr (car tag))
4699 'face my-cperl-REx-length1-face)
4700 (setq tag (cdr tag)))
4701 (setq was-subgr nil)) ; did facing already
4702 ;; Now rare stuff:
4703 ((and (match-beginning 2) ; #-comment
4704 (/= (match-beginning 2) (match-end 2)))
4705 (beginning-of-line 2)
4706 (if (> (point) e)
4707 (goto-char (1- e))))
4708 ((match-beginning 4) ; character "]"
4709 (setq was-subgr nil) ; We do stuff here
4710 (goto-char (match-end 0))
4711 (if cperl-use-syntax-table-text-property
4712 (put-text-property
4713 (1- (point)) (point)
4714 'syntax-table cperl-st-punct))
4715 (cperl-postpone-fontification
4716 (1- (point)) (point)
4717 'face font-lock-warning-face))
4718 ((match-beginning 5) ; before (?{}) (??{})
4719 (setq tag (match-end 0))
4720 (if (or (setq qtag
4721 (cperl-forward-group-in-re st-l))
4722 (and (>= (point) e)
4723 (setq qtag "no matching `)' found"))
4724 (and (not (eq (char-after (- (point) 2))
4725 ?\} ))
4726 (setq qtag "Can't find })")))
a1506d29 4727 (progn
4ab89e7b
SM
4728 (goto-char (1- e))
4729 (message qtag))
4730 (cperl-postpone-fontification
4731 (1- tag) (1- (point))
4732 'face font-lock-variable-name-face)
4733 (cperl-postpone-fontification
4734 REx-subgr-start (1- tag)
4735 'face my-cperl-REx-spec-char-face)
4736 (cperl-postpone-fontification
4737 (1- (point)) (point)
4738 'face my-cperl-REx-spec-char-face)
4739 (if cperl-use-syntax-table-text-property
4740 (progn
4741 (put-text-property
4742 (- (point) 2) (1- (point))
4743 'syntax-table cperl-st-cfence)
4744 (put-text-property
4745 (+ REx-subgr-start 2)
4746 (+ REx-subgr-start 3)
4747 'syntax-table cperl-st-cfence))))
4748 (setq was-subgr nil))
4749 (t ; (?#)-comment
4750 ;; Inside "(" and "\" arn't special in any way
4751 ;; Works also if the outside delimiters are ().
4752 (or;;(if (eq (char-after b) ?\) )
4753 ;;(re-search-forward
4754 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4755 ;; (1- e) 'toend)
4756 (search-forward ")" (1- e) 'toend)
4757 ;;)
4758 (message
4759 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4760 REx-subgr-start))))
6c389151
SM
4761 (if (>= (point) e)
4762 (goto-char (1- e)))
4ab89e7b
SM
4763 (cond
4764 (was-subgr
4765 (setq REx-subgr-end (point))
4766 (cperl-commentify
4767 REx-subgr-start REx-subgr-end nil)
4768 (cperl-postpone-fontification
4769 REx-subgr-start REx-subgr-end
4770 'face font-lock-comment-face))))))
6c389151 4771 (if (and is-REx is-x-REx)
a1506d29 4772 (put-text-property (1+ b) (1- e)
6c389151 4773 'syntax-subtype 'x-REx)))
5bd52f0e
RS
4774 (if i2
4775 (progn
5c8b7eaf 4776 (cperl-postpone-fontification
4ab89e7b 4777 (1- e1) e1 'face my-cperl-delimiters-face)
5bd52f0e 4778 (if (assoc (char-after b) cperl-starters)
4ab89e7b
SM
4779 (progn
4780 (cperl-postpone-fontification
4781 b1 (1+ b1) 'face my-cperl-delimiters-face)
4782 (put-text-property b1 (1+ b1)
4783 'REx-part2 t)))))
5bd52f0e
RS
4784 (if (> (point) max)
4785 (setq tmpend tb))))
4ab89e7b
SM
4786 ((match-beginning 17) ; sub with prototype or attribute
4787 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4788 ;;"\\<sub\\>\\(" ;12
4789 ;; cperl-white-and-comment-rex ;13
4790 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4791 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4792 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4793 (setq b1 (match-beginning 14) e1 (match-end 14))
f83d2997
KH
4794 (if (memq (char-after (1- b))
4795 '(?\$ ?\@ ?\% ?\& ?\*))
4796 nil
4ab89e7b
SM
4797 (goto-char b)
4798 (if (eq (char-after (match-beginning 17)) ?\( )
4799 (progn
4800 (cperl-commentify ; Prototypes; mark as string
4801 (match-beginning 17) (match-end 17) t)
4802 (goto-char (match-end 0))
4803 ;; Now look for attributes after prototype:
4804 (forward-comment (buffer-size))
4805 (and (looking-at ":[^:]")
4806 (cperl-find-sub-attrs st-l b1 e1 b)))
4807 ;; treat attributes without prototype
4808 (goto-char (match-beginning 17))
4809 (cperl-find-sub-attrs st-l b1 e1 b))))
4810 ;; 1+6+2+1+1+6+1=18 extra () before this:
f83d2997 4811 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4ab89e7b
SM
4812 ((match-beginning 19) ; old $abc'efg syntax
4813 (setq bb (match-end 0))
4814 ;;;(if (nth 3 state) nil ; in string
4815 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
f83d2997 4816 (goto-char bb))
4ab89e7b 4817 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
f83d2997 4818 ;; "__\\(END\\|DATA\\)__"
4ab89e7b
SM
4819 ((match-beginning 20) ; __END__, __DATA__
4820 (setq bb (match-end 0))
4821 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4822 (cperl-commentify b bb nil)
4823 (setq end t))
4824 ;; "\\\\\\(['`\"($]\\)"
4825 ((match-beginning 21)
4826 ;; Trailing backslash; make non-quoting outside string/comment
4827 (setq bb (match-end 0))
6c389151
SM
4828 (goto-char b)
4829 (skip-chars-backward "\\\\")
4830 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4ab89e7b 4831 (cperl-modify-syntax-type b cperl-st-punct)
6c389151
SM
4832 (goto-char bb))
4833 (t (error "Error in regexp of the sniffer")))
db133cb6 4834 (if (> (point) stop-point)
f83d2997 4835 (progn
5c8b7eaf 4836 (if end
f83d2997
KH
4837 (message "Garbage after __END__/__DATA__ ignored")
4838 (message "Unbalanced syntax found while scanning")
4839 (or (car err-l) (setcar err-l b)))
db133cb6
RS
4840 (goto-char stop-point))))
4841 (setq cperl-syntax-state (cons state-point state)
4ab89e7b
SM
4842 ;; Do not mark syntax as done past tmpend???
4843 cperl-syntax-done-to (or tmpend (max (point) max)))
4844 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4845 )
f83d2997 4846 (if (car err-l) (goto-char (car err-l))
db133cb6
RS
4847 (or non-inter
4848 (message "Scanning for \"hard\" Perl constructions... done"))))
f83d2997
KH
4849 (and (buffer-modified-p)
4850 (not modified)
4851 (set-buffer-modified-p nil))
00424a9e
SM
4852 ;; I do not understand what this is doing here. It breaks font-locking
4853 ;; because it resets the syntax-table from font-lock-syntax-table to
4854 ;; cperl-mode-syntax-table.
4855 ;; (set-syntax-table cperl-mode-syntax-table)
4856 )
4ab89e7b
SM
4857 (list (car err-l) overshoot)))
4858
4859(defun cperl-find-pods-heres-region (min max)
4860 (interactive "r")
4861 (cperl-find-pods-heres min max))
f83d2997
KH
4862
4863(defun cperl-backward-to-noncomment (lim)
4864 ;; Stops at lim or after non-whitespace that is not in comment
4ab89e7b 4865 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
5bd52f0e 4866 (let (stop p pr)
4ab89e7b 4867 (while (and (not stop) (> (point) (or lim (point-min))))
f83d2997
KH
4868 (skip-chars-backward " \t\n\f" lim)
4869 (setq p (point))
4870 (beginning-of-line)
5bd52f0e
RS
4871 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4872 '(pod here-doc here-doc-delim))
4873 (cperl-unwind-to-safe nil)
4ab89e7b
SM
4874 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4875 (not (memq pr '(string prestring))))
4876 (progn (cperl-to-comment-or-eol) (bolp))
4877 (progn
4878 (skip-chars-backward " \t")
4879 (if (< p (point)) (goto-char p))
4880 (setq stop t)))))))
f83d2997 4881
4ab89e7b
SM
4882;; Used only in `cperl-calculate-indent'...
4883(defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4884 ;; Positions is before ?\{. Checks whether it starts a block.
4885 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4886 (cperl-backward-to-noncomment (point-min))
4887 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4888 ; Label may be mixed up with `$blah :'
4889 (save-excursion (cperl-after-label))
4890 (get-text-property (cperl-1- (point)) 'attrib-group)
4891 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4892 (progn
4893 (backward-sexp)
4894 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4895 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4896 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4897 ;; sub bless::foo {}
4898 (progn
4899 (cperl-backward-to-noncomment (point-min))
4900 (and (eq (preceding-char) ?b)
4901 (progn
4902 (forward-sexp -1)
4903 (looking-at "sub[ \t\n\f#]")))))))))
4904
4905;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4906;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4907;;; may be a part of an in-statement construct, such as
4908;;; ${something()}, print {FH} $data.
4909;;; Moreover, one takes positive approach (looks for else,grep etc)
4910;;; another negative (looks for bless,tr etc)
f739b53b 4911(defun cperl-after-block-p (lim &optional pre-block)
4ab89e7b
SM
4912 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4913Would not look before LIM. Assumes that LIM is a good place to begin a
4914statement. The kind of block we treat here is one after which a new
4915statement would start; thus the block in ${func()} does not count."
f83d2997
KH
4916 (save-excursion
4917 (condition-case nil
4918 (progn
f739b53b 4919 (or pre-block (forward-sexp -1))
f83d2997 4920 (cperl-backward-to-noncomment lim)
bab27c0c 4921 (or (eq (point) lim)
4ab89e7b
SM
4922 ;; if () {} // sub f () {} // sub f :a(') {}
4923 (eq (preceding-char) ?\) )
4924 ;; label: {}
4925 (save-excursion (cperl-after-label))
4926 ;; sub :attr {}
4927 (get-text-property (cperl-1- (point)) 'attrib-group)
4928 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
db133cb6
RS
4929 (save-excursion
4930 (forward-sexp -1)
4ab89e7b
SM
4931 ;; else {} but not else::func {}
4932 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4933 (not (looking-at "\\(\\sw\\|_\\)+::")))
db133cb6
RS
4934 ;; sub f {}
4935 (progn
4936 (cperl-backward-to-noncomment lim)
4ab89e7b 4937 (and (eq (preceding-char) ?b)
db133cb6
RS
4938 (progn
4939 (forward-sexp -1)
4ab89e7b
SM
4940 (looking-at "sub[ \t\n\f#]"))))))
4941 ;; What preceeds is not word... XXXX Last statement in sub???
db133cb6 4942 (cperl-after-expr-p lim))))
f83d2997
KH
4943 (error nil))))
4944
4945(defun cperl-after-expr-p (&optional lim chars test)
029cb4d5 4946 "Return true if the position is good for start of expression.
f83d2997
KH
4947TEST is the expression to evaluate at the found position. If absent,
4948CHARS is a string that contains good characters to have before us (however,
4949`}' is treated \"smartly\" if it is not in the list)."
83261a2f 4950 (let ((lim (or lim (point-min)))
f739b53b
SM
4951 stop p pr)
4952 (cperl-update-syntaxification (point) (point))
f83d2997
KH
4953 (save-excursion
4954 (while (and (not stop) (> (point) lim))
4955 (skip-chars-backward " \t\n\f" lim)
4956 (setq p (point))
4957 (beginning-of-line)
f739b53b
SM
4958 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4959 ;; '(pod here-doc here-doc-delim))
4960 (if (get-text-property (point) 'here-doc-group)
4961 (progn
4962 (goto-char
4ab89e7b 4963 (cperl-beginning-of-property (point) 'here-doc-group))
f739b53b
SM
4964 (beginning-of-line 0)))
4965 (if (get-text-property (point) 'in-pod)
4966 (progn
4967 (goto-char
4ab89e7b 4968 (cperl-beginning-of-property (point) 'in-pod))
f739b53b 4969 (beginning-of-line 0)))
f83d2997 4970 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
5bd52f0e 4971 ;; Else: last iteration, or a label
f739b53b 4972 (cperl-to-comment-or-eol) ; Will not move past "." after a format
f83d2997
KH
4973 (skip-chars-backward " \t")
4974 (if (< p (point)) (goto-char p))
5bd52f0e
RS
4975 (setq p (point))
4976 (if (and (eq (preceding-char) ?:)
4977 (progn
4978 (forward-char -1)
4979 (skip-chars-backward " \t\n\f" lim)
4ab89e7b 4980 (memq (char-syntax (preceding-char)) '(?w ?_))))
5bd52f0e
RS
4981 (forward-sexp -1) ; Possibly label. Skip it
4982 (goto-char p)
4983 (setq stop t))))
bab27c0c
RS
4984 (or (bobp) ; ???? Needed
4985 (eq (point) lim)
029cb4d5 4986 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
f83d2997
KH
4987 (progn
4988 (if test (eval test)
4989 (or (memq (preceding-char) (append (or chars "{;") nil))
4990 (and (eq (preceding-char) ?\})
f739b53b
SM
4991 (cperl-after-block-p lim))
4992 (and (eq (following-char) ?.) ; in format: see comment above
4993 (eq (get-text-property (point) 'syntax-type)
4994 'format)))))))))
f83d2997 4995
4ab89e7b
SM
4996(defun cperl-backward-to-start-of-expr (&optional lim)
4997 (condition-case nil
4998 (progn
4999 (while (and (or (not lim)
5000 (> (point) lim))
5001 (not (cperl-after-expr-p lim)))
5002 (forward-sexp -1)
5003 ;; May be after $, @, $# etc of a variable
5004 (skip-chars-backward "$@%#")))
5005 (error nil)))
5006
5007(defun cperl-at-end-of-expr (&optional lim)
5008 ;; Since the SEXP approach below is very fragile, do some overengineering
5009 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
5010 (condition-case nil
5011 (save-excursion
5012 ;; If nothing interesting after, does as (forward-sexp -1);
5013 ;; otherwise fails, or ends at a start of following sexp.
5014 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
5015 ;; may be stuck after @ or $; just put some stupid workaround now:
5016 (let ((p (point)))
5017 (forward-sexp 1)
5018 (forward-sexp -1)
5019 (while (memq (preceding-char) (append "%&@$*" nil))
5020 (forward-char -1))
5021 (or (< (point) p)
5022 (cperl-after-expr-p lim))))
5023 (error t))))
5024
5025(defun cperl-forward-to-end-of-expr (&optional lim)
5026 (let ((p (point))))
5027 (condition-case nil
5028 (progn
5029 (while (and (< (point) (or lim (point-max)))
5030 (not (cperl-at-end-of-expr)))
5031 (forward-sexp 1)))
5032 (error nil)))
5033
f83d2997
KH
5034(defun cperl-backward-to-start-of-continued-exp (lim)
5035 (if (memq (preceding-char) (append ")]}\"'`" nil))
5036 (forward-sexp -1))
5037 (beginning-of-line)
5038 (if (<= (point) lim)
5039 (goto-char (1+ lim)))
5040 (skip-chars-forward " \t"))
5041
db133cb6
RS
5042(defun cperl-after-block-and-statement-beg (lim)
5043 ;; We assume that we are after ?\}
5c8b7eaf 5044 (and
db133cb6
RS
5045 (cperl-after-block-p lim)
5046 (save-excursion
5047 (forward-sexp -1)
5048 (cperl-backward-to-noncomment (point-min))
5049 (or (bobp)
bab27c0c 5050 (eq (point) lim)
db133cb6
RS
5051 (not (= (char-syntax (preceding-char)) ?w))
5052 (progn
5053 (forward-sexp -1)
5c8b7eaf 5054 (not
db133cb6
RS
5055 (looking-at
5056 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
5057
f83d2997 5058\f
f83d2997
KH
5059(defun cperl-indent-exp ()
5060 "Simple variant of indentation of continued-sexp.
5bd52f0e
RS
5061
5062Will not indent comment if it starts at `comment-indent' or looks like
5063continuation of the comment on the previous line.
db133cb6 5064
5c8b7eaf 5065If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 5066conditional/loop constructs."
f83d2997
KH
5067 (interactive)
5068 (save-excursion
5069 (let ((tmp-end (progn (end-of-line) (point))) top done)
5070 (save-excursion
5071 (beginning-of-line)
5072 (while (null done)
5073 (setq top (point))
4ab89e7b
SM
5074 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5075 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
f83d2997
KH
5076 (setq top (point))) ; Get the outermost parenths in line
5077 (goto-char top)
5078 (while (< (point) tmp-end)
5079 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5080 (or (eolp) (forward-sexp 1)))
4ab89e7b
SM
5081 (if (> (point) tmp-end) ; Yes, there an unfinished block
5082 nil
5083 (if (eq ?\) (preceding-char))
5084 (progn ;; Plan B: find by REGEXP block followup this line
5085 (setq top (point))
5086 (condition-case nil
5087 (progn
5088 (forward-sexp -2)
5089 (if (eq (following-char) ?$ ) ; for my $var (list)
5090 (progn
5091 (forward-sexp -1)
5092 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5093 (forward-sexp -1))))
5094 (if (looking-at
5095 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5096 "\\|for\\(each\\)?\\>\\(\\("
5097 cperl-maybe-white-and-comment-rex
5098 "\\(my\\|local\\|our\\)\\)?"
5099 cperl-maybe-white-and-comment-rex
5100 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5101 (progn
5102 (goto-char top)
5103 (forward-sexp 1)
5104 (setq top (point)))))
5105 (error (setq done t)))
5106 (goto-char top))
5107 (if (looking-at ; Try Plan C: continuation block
5108 (concat cperl-maybe-white-and-comment-rex
5109 "\\<\\(else\\|elsif\|continue\\)\\>"))
5110 (progn
5111 (goto-char (match-end 0))
5112 (save-excursion
5113 (end-of-line)
5114 (setq tmp-end (point))))
5115 (setq done t))))
5116 (save-excursion
5117 (end-of-line)
5118 (setq tmp-end (point))))
f83d2997
KH
5119 (goto-char tmp-end)
5120 (setq tmp-end (point-marker)))
db133cb6
RS
5121 (if cperl-indent-region-fix-constructs
5122 (cperl-fix-line-spacing tmp-end))
f83d2997
KH
5123 (cperl-indent-region (point) tmp-end))))
5124
5bd52f0e
RS
5125(defun cperl-fix-line-spacing (&optional end parse-data)
5126 "Improve whitespace in a conditional/loop construct.
5127Returns some position at the last line."
db133cb6
RS
5128 (interactive)
5129 (or end
5130 (setq end (point-max)))
83261a2f
SM
5131 (let ((ee (save-excursion (end-of-line) (point)))
5132 (cperl-indent-region-fix-constructs
5133 (or cperl-indent-region-fix-constructs 1))
5134 p pp ml have-brace ret)
db133cb6
RS
5135 (save-excursion
5136 (beginning-of-line)
5bd52f0e 5137 (setq ret (point))
5c8b7eaf 5138 ;; }? continue
5bd52f0e 5139 ;; blah; }
5c8b7eaf 5140 (if (not
5bd52f0e
RS
5141 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5142 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5143 nil ; Do not need to do anything
83261a2f
SM
5144 ;; Looking at:
5145 ;; }
5146 ;; else
4ab89e7b
SM
5147 (if cperl-merge-trailing-else
5148 (if (looking-at
5149 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5150 (progn
5151 (search-forward "}")
5152 (setq p (point))
5153 (skip-chars-forward " \t\n")
5154 (delete-region p (point))
b5b0cb34 5155 (insert (make-string cperl-indent-region-fix-constructs ?\s))
4ab89e7b
SM
5156 (beginning-of-line)))
5157 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5158 (save-excursion
5159 (search-forward "}")
5160 (delete-horizontal-space)
5161 (insert "\n")
5162 (setq ret (point))
5163 (if (cperl-indent-line parse-data)
5164 (progn
5165 (cperl-fix-line-spacing end parse-data)
5166 (setq ret (point)))))))
83261a2f
SM
5167 ;; Looking at:
5168 ;; } else
5169 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5170 (progn
5171 (search-forward "}")
5172 (delete-horizontal-space)
b5b0cb34 5173 (insert (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f
SM
5174 (beginning-of-line)))
5175 ;; Looking at:
5176 ;; else {
5177 (if (looking-at
5178 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5179 (progn
5180 (forward-word 1)
5181 (delete-horizontal-space)
b5b0cb34 5182 (insert (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f
SM
5183 (beginning-of-line)))
5184 ;; Looking at:
5185 ;; foreach my $var
5186 (if (looking-at
5187 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5188 (progn
5189 (forward-word 2)
5190 (delete-horizontal-space)
b5b0cb34 5191 (insert (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f
SM
5192 (beginning-of-line)))
5193 ;; Looking at:
5194 ;; foreach my $var (
5195 (if (looking-at
6c389151 5196 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
83261a2f 5197 (progn
f739b53b 5198 (forward-sexp 3)
83261a2f
SM
5199 (delete-horizontal-space)
5200 (insert
b5b0cb34 5201 (make-string cperl-indent-region-fix-constructs ?\s))
83261a2f 5202 (beginning-of-line)))
4ab89e7b
SM
5203 ;; Looking at (with or without "}" at start, ending after "({"):
5204 ;; } foreach my $var () OR {
83261a2f 5205 (if (looking-at
ce22dd53 5206 "[ \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 5207 (progn
4ab89e7b 5208 (setq ml (match-beginning 8)) ; "(" or "{" after control word
83261a2f
SM
5209 (re-search-forward "[({]")
5210 (forward-char -1)
5211 (setq p (point))
5212 (if (eq (following-char) ?\( )
5213 (progn
5214 (forward-sexp 1)
4ab89e7b 5215 (setq pp (point))) ; past parenth-group
83261a2f
SM
5216 ;; after `else' or nothing
5217 (if ml ; after `else'
5218 (skip-chars-backward " \t\n")
5219 (beginning-of-line))
5220 (setq pp nil))
5221 ;; Now after the sexp before the brace
5222 ;; Multiline expr should be special
5223 (setq ml (and pp (save-excursion (goto-char p)
5224 (search-forward "\n" pp t))))
4ab89e7b 5225 (if (and (or (not pp) (< pp end)) ; Do not go too far...
83261a2f
SM
5226 (looking-at "[ \t\n]*{"))
5227 (progn
5228 (cond
5229 ((bolp) ; Were before `{', no if/else/etc
5230 nil)
4ab89e7b 5231 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
83261a2f
SM
5232 (delete-horizontal-space)
5233 (if (if ml
5234 cperl-extra-newline-before-brace-multiline
5235 cperl-extra-newline-before-brace)
5236 (progn
5237 (delete-horizontal-space)
5238 (insert "\n")
5239 (setq ret (point))
5240 (if (cperl-indent-line parse-data)
5241 (progn
5242 (cperl-fix-line-spacing end parse-data)
5243 (setq ret (point)))))
5244 (insert
b5b0cb34 5245 (make-string cperl-indent-region-fix-constructs ?\s))))
83261a2f
SM
5246 ((and (looking-at "[ \t]*\n")
5247 (not (if ml
5248 cperl-extra-newline-before-brace-multiline
5249 cperl-extra-newline-before-brace)))
5250 (setq pp (point))
5251 (skip-chars-forward " \t\n")
5252 (delete-region pp (point))
db133cb6 5253 (insert
4ab89e7b
SM
5254 (make-string cperl-indent-region-fix-constructs ?\ )))
5255 ((and (looking-at "[\t ]*{")
5256 (if ml cperl-extra-newline-before-brace-multiline
5257 cperl-extra-newline-before-brace))
5258 (delete-horizontal-space)
5259 (insert "\n")
5260 (setq ret (point))
5261 (if (cperl-indent-line parse-data)
5262 (progn
5263 (cperl-fix-line-spacing end parse-data)
5264 (setq ret (point))))))
83261a2f
SM
5265 ;; Now we are before `{'
5266 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5267 (progn
5268 (skip-chars-forward " \t\n")
5269 (setq pp (point))
5270 (forward-sexp 1)
5271 (setq p (point))
5272 (goto-char pp)
5273 (setq ml (search-forward "\n" p t))
5274 (if (or cperl-break-one-line-blocks-when-indent ml)
5275 ;; not good: multi-line BLOCK
5276 (progn
5277 (goto-char (1+ pp))
5278 (delete-horizontal-space)
5279 (insert "\n")
5280 (setq ret (point))
5281 (if (cperl-indent-line parse-data)
5282 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5283 (beginning-of-line)
5284 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5285 ;; Now check whether there is a hanging `}'
5286 ;; Looking at:
5287 ;; } blah
5288 (if (and
5289 cperl-fix-hanging-brace-when-indent
5290 have-brace
5291 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5292 (condition-case nil
5293 (progn
5294 (up-list 1)
5295 (if (and (<= (point) pp)
5296 (eq (preceding-char) ?\} )
5297 (cperl-after-block-and-statement-beg (point-min)))
5298 t
5299 (goto-char p)
5300 nil))
5301 (error nil)))
5302 (progn
5303 (forward-char -1)
5304 (skip-chars-backward " \t")
5305 (if (bolp)
5306 ;; `}' was the first thing on the line, insert NL *after* it.
5307 (progn
5308 (cperl-indent-line parse-data)
5309 (search-forward "}")
5310 (delete-horizontal-space)
5311 (insert "\n"))
5312 (delete-horizontal-space)
5313 (or (eq (preceding-char) ?\;)
5314 (bolp)
5315 (and (eq (preceding-char) ?\} )
5316 (cperl-after-block-p (point-min)))
5317 (insert ";"))
5318 (insert "\n")
5319 (setq ret (point)))
5320 (if (cperl-indent-line parse-data)
5321 (setq ret (cperl-fix-line-spacing end parse-data)))
5322 (beginning-of-line)))))
5bd52f0e
RS
5323 ret))
5324
5325(defvar cperl-update-start) ; Do not need to make them local
5326(defvar cperl-update-end)
5327(defun cperl-delay-update-hook (beg end old-len)
5328 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5329 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
db133cb6 5330
f83d2997
KH
5331(defun cperl-indent-region (start end)
5332 "Simple variant of indentation of region in CPerl mode.
5c8b7eaf 5333Should be slow. Will not indent comment if it starts at `comment-indent'
f83d2997 5334or looks like continuation of the comment on the previous line.
5c8b7eaf
SS
5335Indents all the lines whose first character is between START and END
5336inclusive.
db133cb6 5337
5c8b7eaf 5338If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 5339conditional/loop constructs."
f83d2997 5340 (interactive "r")
5bd52f0e 5341 (cperl-update-syntaxification end end)
f83d2997 5342 (save-excursion
5bd52f0e 5343 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
83261a2f
SM
5344 (let ((indent-info (if cperl-emacs-can-parse
5345 (list nil nil nil) ; Cannot use '(), since will modify
5346 nil))
c326ddd1 5347 (pm 0)
83261a2f
SM
5348 after-change-functions ; Speed it up!
5349 st comm old-comm-indent new-comm-indent p pp i empty)
5bd52f0e 5350 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
83261a2f
SM
5351 (goto-char start)
5352 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5353 (current-column))
5354 new-comm-indent old-comm-indent)
5355 (goto-char start)
5356 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5357 (or (bolp) (beginning-of-line 2))
83261a2f 5358 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5bd52f0e
RS
5359 (setq st (point))
5360 (if (or
5361 (setq empty (looking-at "[ \t]*\n"))
5362 (and (setq comm (looking-at "[ \t]*#"))
83261a2f
SM
5363 (or (eq (current-indentation) (or old-comm-indent
5364 comment-column))
5bd52f0e 5365 (setq old-comm-indent nil))))
f83d2997 5366 (if (and old-comm-indent
5bd52f0e 5367 (not empty)
f83d2997 5368 (= (current-indentation) old-comm-indent)
5bd52f0e
RS
5369 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5370 (not (eq (get-text-property (point) 'syntax-table)
5371 cperl-st-cfence)))
83261a2f
SM
5372 (let ((comment-column new-comm-indent))
5373 (indent-for-comment)))
5374 (progn
5bd52f0e 5375 (setq i (cperl-indent-line indent-info))
f83d2997 5376 (or comm
db133cb6 5377 (not i)
f83d2997 5378 (progn
db133cb6 5379 (if cperl-indent-region-fix-constructs
5bd52f0e 5380 (goto-char (cperl-fix-line-spacing end indent-info)))
83261a2f
SM
5381 (if (setq old-comm-indent
5382 (and (cperl-to-comment-or-eol)
5383 (not (memq (get-text-property (point)
5384 'syntax-type)
5385 '(pod here-doc)))
5c8b7eaf 5386 (not (eq (get-text-property (point)
5bd52f0e
RS
5387 'syntax-table)
5388 cperl-st-cfence))
f83d2997
KH
5389 (current-column)))
5390 (progn (indent-for-comment)
5391 (skip-chars-backward " \t")
5392 (skip-chars-backward "#")
5393 (setq new-comm-indent (current-column))))))))
335dd1f1 5394 (beginning-of-line 2)))
5bd52f0e 5395 ;; Now run the update hooks
83261a2f
SM
5396 (and after-change-functions
5397 cperl-update-end
5398 (save-excursion
5399 (goto-char cperl-update-end)
5400 (insert " ")
5401 (delete-char -1)
5402 (goto-char cperl-update-start)
5403 (insert " ")
5404 (delete-char -1))))))
f83d2997 5405
f83d2997
KH
5406;; Stolen from lisp-mode with a lot of improvements
5407
5408(defun cperl-fill-paragraph (&optional justify iteration)
82eb0dae 5409 "Like `fill-paragraph', but handle CPerl comments.
f83d2997
KH
5410If any of the current line is a comment, fill the comment or the
5411block of it that point is in, preserving the comment's initial
5412indentation and initial hashes. Behaves usually outside of comment."
82eb0dae 5413 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
83261a2f 5414 (let (;; Non-nil if the current line contains a comment.
f83d2997 5415 has-comment
4ab89e7b 5416 fill-paragraph-function ; do not recurse
f83d2997
KH
5417 ;; If has-comment, the appropriate fill-prefix for the comment.
5418 comment-fill-prefix
5419 ;; Line that contains code and comment (or nil)
5420 start
5421 c spaces len dc (comment-column comment-column))
5422 ;; Figure out what kind of comment we are looking at.
5423 (save-excursion
5424 (beginning-of-line)
5425 (cond
5426
5427 ;; A line with nothing but a comment on it?
5428 ((looking-at "[ \t]*#[# \t]*")
5429 (setq has-comment t
5430 comment-fill-prefix (buffer-substring (match-beginning 0)
5431 (match-end 0))))
5432
5433 ;; A line with some code, followed by a comment? Remember that the
5434 ;; semi which starts the comment shouldn't be part of a string or
5435 ;; character.
5436 ((cperl-to-comment-or-eol)
5437 (setq has-comment t)
5438 (looking-at "#+[ \t]*")
5c8b7eaf 5439 (setq start (point) c (current-column)
f83d2997 5440 comment-fill-prefix
b5b0cb34 5441 (concat (make-string (current-column) ?\s)
f83d2997 5442 (buffer-substring (match-beginning 0) (match-end 0)))
5c8b7eaf 5443 spaces (progn (skip-chars-backward " \t")
f83d2997 5444 (buffer-substring (point) start))
5c8b7eaf 5445 dc (- c (current-column)) len (- start (point))
f83d2997
KH
5446 start (point-marker))
5447 (delete-char len)
4ab89e7b 5448 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
f83d2997 5449 (if (not has-comment)
83261a2f 5450 (fill-paragraph justify) ; Do the usual thing outside of comment
f83d2997
KH
5451 ;; Narrow to include only the comment, and then fill the region.
5452 (save-restriction
5453 (narrow-to-region
5454 ;; Find the first line we should include in the region to fill.
5455 (if start (progn (beginning-of-line) (point))
5456 (save-excursion
5457 (while (and (zerop (forward-line -1))
5458 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5459 ;; We may have gone to far. Go forward again.
5460 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5461 (forward-line 1))
5462 (point)))
5463 ;; Find the beginning of the first line past the region to fill.
5464 (save-excursion
5465 (while (progn (forward-line 1)
5466 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5467 (point)))
5468 ;; Remove existing hashes
cff301be 5469 (save-excursion
4ab89e7b
SM
5470 (goto-char (point-min))
5471 (while (progn (forward-line 1) (< (point) (point-max)))
5472 (skip-chars-forward " \t")
5473 (if (looking-at "#+")
5474 (progn
5475 (if (and (eq (point) (match-beginning 0))
5476 (not (eq (point) (match-end 0)))) nil
5477 (error
5478 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5479 (delete-char (- (match-end 0) (match-beginning 0)))))))
f83d2997
KH
5480
5481 ;; Lines with only hashes on them can be paragraph boundaries.
5482 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5483 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5484 (fill-prefix comment-fill-prefix))
5485 (fill-paragraph justify)))
5486 (if (and start)
5c8b7eaf 5487 (progn
f83d2997
KH
5488 (goto-char start)
5489 (if (> dc 0)
83261a2f 5490 (progn (delete-char dc) (insert spaces)))
f83d2997
KH
5491 (if (or (= (current-column) c) iteration) nil
5492 (setq comment-column c)
5493 (indent-for-comment)
5494 ;; Repeat once more, flagging as iteration
4ab89e7b
SM
5495 (cperl-fill-paragraph justify t))))))
5496 t)
f83d2997
KH
5497
5498(defun cperl-do-auto-fill ()
5499 ;; Break out if the line is short enough
5500 (if (> (save-excursion
5501 (end-of-line)
5502 (current-column))
5503 fill-column)
83261a2f
SM
5504 (let ((c (save-excursion (beginning-of-line)
5505 (cperl-to-comment-or-eol) (point)))
8038e2cf 5506 (s (memq (following-char) '(?\s ?\t))) marker)
82eb0dae
SM
5507 (if (>= c (point))
5508 ;; Don't break line inside code: only inside comment.
5509 nil
83261a2f 5510 (setq marker (point-marker))
82eb0dae 5511 (fill-paragraph nil)
83261a2f
SM
5512 (goto-char marker)
5513 ;; Is not enough, sometimes marker is a start of line
5514 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5515 (goto-char (match-end 0))))
5516 ;; Following space could have gone:
8038e2cf 5517 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
83261a2f
SM
5518 (insert " ")
5519 (backward-char 1))
5520 ;; Previous space could have gone:
8038e2cf 5521 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
f83d2997 5522
f83d2997
KH
5523(defun cperl-imenu-addback (lst &optional isback name)
5524 ;; We suppose that the lst is a DAG, unless the first element only
5525 ;; loops back, and ISBACK is set. Thus this function cannot be
5526 ;; applied twice without ISBACK set.
5527 (cond ((not cperl-imenu-addback) lst)
5528 (t
5c8b7eaf 5529 (or name
f83d2997 5530 (setq name "+++BACK+++"))
83261a2f
SM
5531 (mapcar (lambda (elt)
5532 (if (and (listp elt) (listp (cdr elt)))
5533 (progn
5534 ;; In the other order it goes up
5535 ;; one level only ;-(
5536 (setcdr elt (cons (cons name lst)
5537 (cdr elt)))
5538 (cperl-imenu-addback (cdr elt) t name))))
f83d2997
KH
5539 (if isback (cdr lst) lst))
5540 lst)))
5541
80585273 5542(defun cperl-imenu--create-perl-index (&optional regexp)
f83d2997 5543 (require 'imenu) ; May be called from TAGS creator
5c8b7eaf 5544 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
f83d2997
KH
5545 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5546 (index-meth-alist '()) meth
4ab89e7b
SM
5547 packages ends-ranges p marker is-proto
5548 (prev-pos 0) is-pack index index1 name (end-range 0) package)
f83d2997 5549 (goto-char (point-min))
6c389151 5550 (cperl-update-syntaxification (point-max) (point-max))
f83d2997
KH
5551 ;; Search for the function
5552 (progn ;;save-match-data
5553 (while (re-search-forward
80585273 5554 (or regexp cperl-imenu--function-name-regexp-perl)
f83d2997 5555 nil t)
4ab89e7b 5556 ;; 2=package-group, 5=package-name 8=sub-name
f83d2997
KH
5557 (cond
5558 ((and ; Skip some noise if building tags
4ab89e7b
SM
5559 (match-beginning 5) ; package name
5560 ;;(eq (char-after (match-beginning 2)) ?p) ; package
f83d2997 5561 (not (save-match-data
83261a2f 5562 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
f83d2997
KH
5563 nil)
5564 ((and
4ab89e7b
SM
5565 (or (match-beginning 2)
5566 (match-beginning 8)) ; package or sub
6c389151 5567 ;; Skip if quoted (will not skip multi-line ''-strings :-():
f83d2997
KH
5568 (null (get-text-property (match-beginning 1) 'syntax-table))
5569 (null (get-text-property (match-beginning 1) 'syntax-type))
5570 (null (get-text-property (match-beginning 1) 'in-pod)))
4ab89e7b 5571 (setq is-pack (match-beginning 2))
f83d2997
KH
5572 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5573 ;; (goto-char (match-end 0))) ; Messes what follows
4ab89e7b 5574 (setq meth nil
f83d2997
KH
5575 p (point))
5576 (while (and ends-ranges (>= p (car ends-ranges)))
5577 ;; delete obsolete entries
5578 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5579 (setq package (or (car packages) "")
5580 end-range (or (car ends-ranges) 0))
4ab89e7b
SM
5581 (if is-pack ; doing "package"
5582 (progn
5583 (if (match-beginning 5) ; named package
5584 (setq name (buffer-substring (match-beginning 5)
5585 (match-end 5))
5586 name (progn
5587 (set-text-properties 0 (length name) nil name)
5588 name)
5589 package (concat name "::")
5590 name (concat "package " name))
5591 ;; Support nameless packages
5592 (setq name "package;" package ""))
5593 (setq end-range
5594 (save-excursion
5595 (parse-partial-sexp (point) (point-max) -1) (point))
5596 ends-ranges (cons end-range ends-ranges)
5597 packages (cons package packages)))
5598 (setq is-proto
5599 (or (eq (following-char) ?\;)
5600 (eq 0 (get-text-property (point) 'attrib-group)))))
f83d2997 5601 ;; Skip this function name if it is a prototype declaration.
4ab89e7b
SM
5602 (if (and is-proto (not is-pack)) nil
5603 (or is-pack
5604 (setq name
5605 (buffer-substring (match-beginning 8) (match-end 8)))
5606 (set-text-properties 0 (length name) nil name))
5607 (setq marker (make-marker))
5608 (set-marker marker (match-end (if is-pack 2 8)))
5609 (cond (is-pack nil)
5610 ((string-match "[:']" name)
5611 (setq meth t))
5612 ((> p end-range) nil)
5613 (t
5614 (setq name (concat package name) meth t)))
6c389151 5615 (setq index (cons name marker))
4ab89e7b 5616 (if is-pack
f83d2997
KH
5617 (push index index-pack-alist)
5618 (push index index-alist))
5619 (if meth (push index index-meth-alist))
5620 (push index index-unsorted-alist)))
4ab89e7b
SM
5621 ((match-beginning 16) ; POD section
5622 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5623 marker (make-marker))
5624 (set-marker marker (match-beginning 17))
f83d2997 5625 (set-text-properties 0 (length name) nil name)
4ab89e7b
SM
5626 (setq name (concat (make-string
5627 (* 3 (- (char-after (match-beginning 16)) ?1))
5628 ?\ )
5629 name)
5630 index (cons name marker))
f83d2997
KH
5631 (setq index1 (cons (concat "=" name) (cdr index)))
5632 (push index index-pod-alist)
5633 (push index1 index-unsorted-alist)))))
5c8b7eaf 5634 (setq index-alist
f83d2997
KH
5635 (if (default-value 'imenu-sort-function)
5636 (sort index-alist (default-value 'imenu-sort-function))
83261a2f 5637 (nreverse index-alist)))
f83d2997
KH
5638 (and index-pod-alist
5639 (push (cons "+POD headers+..."
5640 (nreverse index-pod-alist))
5641 index-alist))
5642 (and (or index-pack-alist index-meth-alist)
5643 (let ((lst index-pack-alist) hier-list pack elt group name)
5644 ;; Remove "package ", reverse and uniquify.
5645 (while lst
5646 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5647 (if (assoc name hier-list) nil
5648 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5649 (setq lst index-meth-alist)
5650 (while lst
5651 (setq elt (car lst) lst (cdr lst))
5652 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5653 (setq pack (substring (car elt) 0 (match-beginning 0)))
5c8b7eaf 5654 (if (setq group (assoc pack hier-list))
f83d2997
KH
5655 (if (listp (cdr group))
5656 ;; Have some functions already
5c8b7eaf
SS
5657 (setcdr group
5658 (cons (cons (substring
f83d2997
KH
5659 (car elt)
5660 (+ 2 (match-beginning 0)))
5661 (cdr elt))
5662 (cdr group)))
5c8b7eaf 5663 (setcdr group (list (cons (substring
f83d2997
KH
5664 (car elt)
5665 (+ 2 (match-beginning 0)))
5666 (cdr elt)))))
5c8b7eaf
SS
5667 (setq hier-list
5668 (cons (cons pack
5669 (list (cons (substring
f83d2997
KH
5670 (car elt)
5671 (+ 2 (match-beginning 0)))
5672 (cdr elt))))
5673 hier-list))))))
5674 (push (cons "+Hierarchy+..."
5675 hier-list)
5676 index-alist)))
5677 (and index-pack-alist
5678 (push (cons "+Packages+..."
5679 (nreverse index-pack-alist))
5680 index-alist))
5c8b7eaf 5681 (and (or index-pack-alist index-pod-alist
f83d2997
KH
5682 (default-value 'imenu-sort-function))
5683 index-unsorted-alist
5684 (push (cons "+Unsorted List+..."
5685 (nreverse index-unsorted-alist))
5686 index-alist))
5687 (cperl-imenu-addback index-alist)))
5688
6c389151 5689\f
6c389151
SM
5690;; Suggested by Mark A. Hershberger
5691(defun cperl-outline-level ()
5692 (looking-at outline-regexp)
5693 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
4ab89e7b
SM
5694;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5695 ((match-beginning 2) 0) ; package
5696 ((match-beginning 8) 1) ; sub
5697 ((match-beginning 16)
5698 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5699 (t 5))) ; should not happen
6c389151
SM
5700
5701\f
a1506d29 5702(defvar cperl-compilation-error-regexp-alist
4ab89e7b 5703 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
f83d2997
KH
5704 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
5705 2 3))
5706 "Alist that specifies how to match errors in perl output.")
5707
f83d2997 5708
f83d2997
KH
5709(defun cperl-windowed-init ()
5710 "Initialization under windowed version."
f453f5a8
CY
5711 (cond ((featurep 'ps-print)
5712 (unless cperl-faces-init
5713 (if (boundp 'font-lock-multiline)
5714 (setq cperl-font-lock-multiline t))
5715 (cperl-init-faces)))
5716 ((not cperl-faces-init)
5717 (add-hook 'font-lock-mode-hook
5718 (function
5719 (lambda ()
5720 (if (memq major-mode '(perl-mode cperl-mode))
5721 (progn
5722 (or cperl-faces-init (cperl-init-faces)))))))
5723 (if (fboundp 'eval-after-load)
5724 (eval-after-load
5725 "ps-print"
5726 '(or cperl-faces-init (cperl-init-faces)))))))
db133cb6 5727
5efe6a56 5728(defvar cperl-font-lock-keywords-1 nil
80585273 5729 "Additional expressions to highlight in Perl mode. Minimal set.")
5efe6a56 5730(defvar cperl-font-lock-keywords nil
80585273 5731 "Additional expressions to highlight in Perl mode. Default set.")
5efe6a56 5732(defvar cperl-font-lock-keywords-2 nil
80585273
DL
5733 "Additional expressions to highlight in Perl mode. Maximal set")
5734
db133cb6
RS
5735(defun cperl-load-font-lock-keywords ()
5736 (or cperl-faces-init (cperl-init-faces))
5efe6a56 5737 cperl-font-lock-keywords)
db133cb6
RS
5738
5739(defun cperl-load-font-lock-keywords-1 ()
5740 (or cperl-faces-init (cperl-init-faces))
5efe6a56 5741 cperl-font-lock-keywords-1)
db133cb6
RS
5742
5743(defun cperl-load-font-lock-keywords-2 ()
5744 (or cperl-faces-init (cperl-init-faces))
5efe6a56 5745 cperl-font-lock-keywords-2)
f83d2997 5746
5bd52f0e
RS
5747(defun cperl-init-faces-weak ()
5748 ;; Allow `cperl-find-pods-heres' to run.
5749 (or (boundp 'font-lock-constant-face)
5750 (cperl-force-face font-lock-constant-face
4ab89e7b
SM
5751 "Face for constant and label names"))
5752 (or (boundp 'font-lock-warning-face)
5753 (cperl-force-face font-lock-warning-face
5754 "Face for things which should stand out"))
5755 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5756 )
5bd52f0e 5757
f83d2997 5758(defun cperl-init-faces ()
5bd52f0e 5759 (condition-case errs
f83d2997
KH
5760 (progn
5761 (require 'font-lock)
5762 (and (fboundp 'font-lock-fontify-anchored-keywords)
5763 (featurep 'font-lock-extra)
5764 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5765 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
f83d2997
KH
5766 (if (fboundp 'font-lock-fontify-anchored-keywords)
5767 (setq font-lock-anchored t))
5c8b7eaf 5768 (setq
f83d2997
KH
5769 t-font-lock-keywords
5770 (list
ac6857fb 5771 `("[ \t]+$" 0 ',cperl-invalid-face t)
f83d2997
KH
5772 (cons
5773 (concat
5774 "\\(^\\|[^$@%&\\]\\)\\<\\("
5775 (mapconcat
5776 'identity
5777 '("if" "until" "while" "elsif" "else" "unless" "for"
5778 "foreach" "continue" "exit" "die" "last" "goto" "next"
4ab89e7b 5779 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
6c389151 5780 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
f83d2997
KH
5781 "\\|") ; Flow control
5782 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5783 ; In what follows we use `type' style
5784 ; for overwritable builtins
5785 (list
5786 (concat
5787 "\\(^\\|[^$@%&\\]\\)\\<\\("
5788 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5789 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5790 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5791 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5792 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5793 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5794 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5795 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5796 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5797 ;; "gethostbyname" "gethostent" "getlogin"
5798 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5799 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5800 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5801 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5802 ;; "getservbyport" "getservent" "getsockname"
5803 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5804 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5bd52f0e 5805 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
f83d2997
KH
5806 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5807 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5808 ;; "quotemeta" "rand" "read" "readdir" "readline"
5809 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5810 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5811 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5812 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5813 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5814 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5815 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5816 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
6c389151 5817 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
f83d2997
KH
5818 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5819 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5820 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5c8b7eaf 5821 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
f83d2997
KH
5822 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5823 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5824 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5825 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5826 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5827 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5828 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5829 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5830 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5831 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5832 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5833 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5834 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5835 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5836 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5bd52f0e 5837 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
f83d2997
KH
5838 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5839 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5840 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5841 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5842 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5843 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5844 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5845 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
6c389151 5846 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
f83d2997
KH
5847 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5848 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5849 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5850 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5851 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5852 "\\)\\>") 2 'font-lock-type-face)
5853 ;; In what follows we use `other' style
5854 ;; for nonoverwritable builtins
5855 ;; Somehow 's', 'm' are not auto-generated???
5856 (list
5857 (concat
5858 "\\(^\\|[^$@%&\\]\\)\\<\\("
6c389151 5859 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
f83d2997
KH
5860 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5861 ;; "eval" "exists" "for" "foreach" "format" "goto"
5862 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
4ab89e7b 5863 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
f83d2997
KH
5864 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5865 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5866 ;; "undef" "unless" "unshift" "untie" "until" "use"
5867 ;; "while" "y"
6c389151 5868 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
f83d2997 5869 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
6c389151
SM
5870 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5871 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
f83d2997 5872 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5bd52f0e 5873 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
f83d2997
KH
5874 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5875 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5876 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5877 "\\|[sm]" ; Added manually
4ab89e7b 5878 "\\)\\>") 2 'cperl-nonoverridable-face)
f83d2997
KH
5879 ;; (mapconcat 'identity
5880 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5881 ;; "#include" "#define" "#undef")
5882 ;; "\\|")
5883 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5884 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
4ab89e7b
SM
5885 ;; This highlights declarations and definitions differenty.
5886 ;; We do not try to highlight in the case of attributes:
5887 ;; it is already done by `cperl-find-pods-heres'
5888 (list (concat "\\<sub"
5889 cperl-white-and-comment-rex ; whitespace/comments
5890 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5891 "\\("
5892 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5893 "([^()]*)\\)?" ; prototype
5894 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5895 "[{;]")
5896 2 (if cperl-font-lock-multiline
5897 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5898 'font-lock-function-name-face
5899 'font-lock-variable-name-face)
5900 ;; need to manually set 'multiline' for older font-locks
5901 '(progn
5902 (if (< 1 (count-lines (match-beginning 0)
5903 (match-end 0)))
5904 (put-text-property
5905 (+ 3 (match-beginning 0)) (match-end 0)
5906 'syntax-type 'multiline))
5907 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5908 'font-lock-function-name-face
5909 'font-lock-variable-name-face))))
f83d2997
KH
5910 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5911 2 font-lock-function-name-face)
5912 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5913 1 font-lock-function-name-face)
5914 (cond ((featurep 'font-lock-extra)
5c8b7eaf 5915 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
f83d2997
KH
5916 (2 font-lock-string-face t)
5917 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5918 (font-lock-anchored
5919 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5920 (2 font-lock-string-face t)
5921 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5922 nil nil
5923 (1 font-lock-string-face t))))
5924 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5925 2 font-lock-string-face t)))
db133cb6 5926 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
f83d2997 5927 font-lock-string-face t)
5c8b7eaf 5928 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
83261a2f 5929 font-lock-constant-face) ; labels
f83d2997 5930 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
883212ce 5931 2 font-lock-constant-face)
6c389151
SM
5932 ;; Uncomment to get perl-mode-like vars
5933 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5934 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5935 ;;; (2 (cons font-lock-variable-name-face '(underline))))
f83d2997 5936 (cond ((featurep 'font-lock-extra)
6c389151 5937 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
f83d2997
KH
5938 (3 font-lock-variable-name-face)
5939 (4 '(another 4 nil
5940 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5941 (1 font-lock-variable-name-face)
5c8b7eaf 5942 (2 '(restart 2 nil) nil t)))
f83d2997
KH
5943 nil t))) ; local variables, multiple
5944 (font-lock-anchored
4ab89e7b
SM
5945 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5946 (` ((, (concat "\\<\\(my\\|local\\|our\\)"
5947 cperl-maybe-white-and-comment-rex
5948 "\\(("
5949 cperl-maybe-white-and-comment-rex
5950 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)"))
5951 (5 (, (if cperl-font-lock-multiline
5952 'font-lock-variable-name-face
5953 '(progn (setq cperl-font-lock-multiline-start
5954 (match-beginning 0))
5955 'font-lock-variable-name-face))))
5956 ((, (concat "\\="
5957 cperl-maybe-white-and-comment-rex
5958 ","
5959 cperl-maybe-white-and-comment-rex
5960 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)"))
5961 ;; Bug in font-lock: limit is used not only to limit
5962 ;; searches, but to set the "extend window for
5963 ;; facification" property. Thus we need to minimize.
5964 (, (if cperl-font-lock-multiline
5965 '(if (match-beginning 3)
5966 (save-excursion
5967 (goto-char (match-beginning 3))
5968 (condition-case nil
5969 (forward-sexp 1)
5970 (error
5971 (condition-case nil
5972 (forward-char 200)
5973 (error nil)))) ; typeahead
5974 (1- (point))) ; report limit
5975 (forward-char -2)) ; disable continued expr
5976 '(if (match-beginning 3)
5977 (point-max) ; No limit for continuation
5978 (forward-char -2)))) ; disable continued expr
5979 (, (if cperl-font-lock-multiline
5980 nil
5981 '(progn ; Do at end
5982 ;; "my" may be already fontified (POD),
5983 ;; so cperl-font-lock-multiline-start is nil
5984 (if (or (not cperl-font-lock-multiline-start)
5985 (> 2 (count-lines
5986 cperl-font-lock-multiline-start
5987 (point))))
5988 nil
5989 (put-text-property
5990 (1+ cperl-font-lock-multiline-start) (point)
5991 'syntax-type 'multiline))
5992 (setq cperl-font-lock-multiline-start nil))))
5993 (3 font-lock-variable-name-face)))))
5994 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
f83d2997 5995 3 font-lock-variable-name-face)))
6c389151 5996 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
eb6121fc 5997 4 font-lock-variable-name-face)
9ed9fd35
DP
5998 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5999 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
eb6121fc 6000 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
a1506d29 6001 (setq
f83d2997
KH
6002 t-font-lock-keywords-1
6003 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
4ab89e7b
SM
6004 ;; not yet as of XEmacs 19.12, works with 21.1.11
6005 (or
6006 (not cperl-xemacs-p)
6007 (string< "21.1.9" emacs-version)
6008 (and (string< "21.1.10" emacs-version)
6009 (string< emacs-version "21.1.2")))
f83d2997
KH
6010 '(
6011 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
6012 (if (eq (char-after (match-beginning 2)) ?%)
4ab89e7b
SM
6013 'cperl-hash-face
6014 'cperl-array-face)
f83d2997
KH
6015 t) ; arrays and hashes
6016 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
6017 1
5c8b7eaf 6018 (if (= (- (match-end 2) (match-beginning 2)) 1)
f83d2997 6019 (if (eq (char-after (match-beginning 3)) ?{)
4ab89e7b
SM
6020 'cperl-hash-face
6021 'cperl-array-face) ; arrays and hashes
f83d2997
KH
6022 font-lock-variable-name-face) ; Just to put something
6023 t)
4ab89e7b
SM
6024 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
6025 (1 cperl-array-face)
6026 (2 font-lock-variable-name-face))
6027 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
6028 (1 cperl-hash-face)
6029 (2 font-lock-variable-name-face))
f83d2997
KH
6030 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
6031 ;;; Too much noise from \s* @s[ and friends
5c8b7eaf 6032 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
f83d2997
KH
6033 ;;(3 font-lock-function-name-face t t)
6034 ;;(4
6035 ;; (if (cperl-slash-is-regexp)
6036 ;; font-lock-function-name-face 'default) nil t))
6037 )))
6c389151
SM
6038 (if cperl-highlight-variables-indiscriminately
6039 (setq t-font-lock-keywords-1
6040 (append t-font-lock-keywords-1
4ab89e7b 6041 (list '("\\([$*]{?\\sw+\\)" 1
6c389151 6042 font-lock-variable-name-face)))))
a1506d29 6043 (setq cperl-font-lock-keywords-1
5bd52f0e
RS
6044 (if cperl-syntaxify-by-font-lock
6045 (cons 'cperl-fontify-update
6046 t-font-lock-keywords)
6047 t-font-lock-keywords)
5efe6a56
SM
6048 cperl-font-lock-keywords cperl-font-lock-keywords-1
6049 cperl-font-lock-keywords-2 (append
6c389151
SM
6050 cperl-font-lock-keywords-1
6051 t-font-lock-keywords-1)))
f83d2997
KH
6052 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
6053 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
db133cb6 6054 (eval ; Avoid a warning
83261a2f
SM
6055 '(font-lock-require-faces
6056 (list
6057 ;; Color-light Color-dark Gray-light Gray-dark Mono
6058 (list 'font-lock-comment-face
6059 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
6060 nil
6061 [nil nil t t t]
6062 [nil nil t t t]
6063 nil)
6064 (list 'font-lock-string-face
6065 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
6066 nil
6067 nil
6068 [nil nil t t t]
6069 nil)
6070 (list 'font-lock-function-name-face
6071 (vector
6072 "Blue" "LightSkyBlue" "Gray50" "LightGray"
6073 (cdr (assq 'background-color ; if mono
6074 (frame-parameters))))
6075 (vector
6076 nil nil nil nil
6077 (cdr (assq 'foreground-color ; if mono
6078 (frame-parameters))))
6079 [nil nil t t t]
6080 nil
6081 nil)
6082 (list 'font-lock-variable-name-face
6083 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6084 nil
6085 [nil nil t t t]
6086 [nil nil t t t]
6087 nil)
6088 (list 'font-lock-type-face
6089 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6090 nil
6091 [nil nil t t t]
6092 nil
6093 [nil nil t t t])
4ab89e7b
SM
6094 (list 'font-lock-warning-face
6095 ["Pink" "Red" "Gray50" "LightGray"]
6096 ["gray20" "gray90"
6097 "gray80" "gray20"]
6098 [nil nil t t t]
6099 nil
6100 [nil nil t t t]
6101 )
83261a2f
SM
6102 (list 'font-lock-constant-face
6103 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6104 nil
6105 [nil nil t t t]
6106 nil
6107 [nil nil t t t])
4ab89e7b 6108 (list 'cperl-nonoverridable-face
83261a2f
SM
6109 ["chartreuse3" ("orchid1" "orange")
6110 nil "Gray80"]
6111 [nil nil "gray90"]
6112 [nil nil nil t t]
6113 [nil nil t t]
6114 [nil nil t t t])
4ab89e7b 6115 (list 'cperl-array-face
83261a2f
SM
6116 ["blue" "yellow" nil "Gray80"]
6117 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6118 "gray90"]
6119 t
6120 nil
6121 nil)
4ab89e7b 6122 (list 'cperl-hash-face
83261a2f
SM
6123 ["red" "red" nil "Gray80"]
6124 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6125 "gray90"]
6126 t
6127 t
6128 nil))))
5bd52f0e 6129 ;; Do it the dull way, without choose-color
f83d2997
KH
6130 (defvar cperl-guessed-background nil
6131 "Display characteristics as guessed by cperl.")
83261a2f 6132 ;; (or (fboundp 'x-color-defined-p)
15ca5699 6133 ;; (defalias 'x-color-defined-p
83261a2f
SM
6134 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6135 ;; ;; XEmacs >= 19.12
6136 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6137 ;; ;; XEmacs 19.11
6138 ;; (t 'x-valid-color-name-p))))
5c8b7eaf 6139 (cperl-force-face font-lock-constant-face
5bd52f0e
RS
6140 "Face for constant and label names")
6141 (cperl-force-face font-lock-variable-name-face
6142 "Face for variable names")
6143 (cperl-force-face font-lock-type-face
6144 "Face for data types")
4ab89e7b 6145 (cperl-force-face cperl-nonoverridable-face
5bd52f0e 6146 "Face for data types from another group")
4ab89e7b
SM
6147 (cperl-force-face font-lock-warning-face
6148 "Face for things which should stand out")
5bd52f0e
RS
6149 (cperl-force-face font-lock-comment-face
6150 "Face for comments")
6151 (cperl-force-face font-lock-function-name-face
6152 "Face for function names")
4ab89e7b 6153 (cperl-force-face cperl-hash-face
5bd52f0e 6154 "Face for hashes")
4ab89e7b 6155 (cperl-force-face cperl-array-face
5bd52f0e
RS
6156 "Face for arrays")
6157 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6158 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6159 ;;(or (boundp 'font-lock-type-face)
6160 ;; (defconst font-lock-type-face
6161 ;; 'font-lock-type-face
6162 ;; "Face to use for data types."))
6163 ;;(or (boundp 'cperl-nonoverridable-face)
6164 ;; (defconst cperl-nonoverridable-face
4ab89e7b 6165 ;; 'cperl-nonoverridable-face
5bd52f0e
RS
6166 ;; "Face to use for data types from another group."))
6167 ;;(if (not cperl-xemacs-p) nil
6168 ;; (or (boundp 'font-lock-comment-face)
6169 ;; (defconst font-lock-comment-face
6170 ;; 'font-lock-comment-face
6171 ;; "Face to use for comments."))
6172 ;; (or (boundp 'font-lock-keyword-face)
6173 ;; (defconst font-lock-keyword-face
6174 ;; 'font-lock-keyword-face
6175 ;; "Face to use for keywords."))
6176 ;; (or (boundp 'font-lock-function-name-face)
6177 ;; (defconst font-lock-function-name-face
6178 ;; 'font-lock-function-name-face
6179 ;; "Face to use for function names.")))
6180 (if (and
4ab89e7b 6181 (not (cperl-is-face 'cperl-array-face))
5c8b7eaf 6182 (cperl-is-face 'font-lock-emphasized-face))
4ab89e7b 6183 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
5bd52f0e 6184 (if (and
4ab89e7b 6185 (not (cperl-is-face 'cperl-hash-face))
5c8b7eaf 6186 (cperl-is-face 'font-lock-other-emphasized-face))
4ab89e7b 6187 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
5bd52f0e 6188 (if (and
4ab89e7b 6189 (not (cperl-is-face 'cperl-nonoverridable-face))
5c8b7eaf 6190 (cperl-is-face 'font-lock-other-type-face))
4ab89e7b 6191 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
5bd52f0e
RS
6192 ;;(or (boundp 'cperl-hash-face)
6193 ;; (defconst cperl-hash-face
4ab89e7b 6194 ;; 'cperl-hash-face
5bd52f0e
RS
6195 ;; "Face to use for hashes."))
6196 ;;(or (boundp 'cperl-array-face)
6197 ;; (defconst cperl-array-face
4ab89e7b 6198 ;; 'cperl-array-face
5bd52f0e 6199 ;; "Face to use for arrays."))
f83d2997
KH
6200 ;; Here we try to guess background
6201 (let ((background
6202 (if (boundp 'font-lock-background-mode)
6203 font-lock-background-mode
5c8b7eaf 6204 'light))
83261a2f 6205 (face-list (and (fboundp 'face-list) (face-list))))
5bd52f0e
RS
6206;;;; (fset 'cperl-is-face
6207;;;; (cond ((fboundp 'find-face)
6208;;;; (symbol-function 'find-face))
6209;;;; (face-list
6210;;;; (function (lambda (face) (member face face-list))))
6211;;;; (t
6212;;;; (function (lambda (face) (boundp face))))))
f83d2997
KH
6213 (defvar cperl-guessed-background
6214 (if (and (boundp 'font-lock-display-type)
6215 (eq font-lock-display-type 'grayscale))
6216 'gray
6217 background)
6218 "Background as guessed by CPerl mode")
83261a2f
SM
6219 (and (not (cperl-is-face 'font-lock-constant-face))
6220 (cperl-is-face 'font-lock-reference-face)
6221 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
db133cb6 6222 (if (cperl-is-face 'font-lock-type-face) nil
f83d2997
KH
6223 (copy-face 'default 'font-lock-type-face)
6224 (cond
6225 ((eq background 'light)
6226 (set-face-foreground 'font-lock-type-face
6227 (if (x-color-defined-p "seagreen")
6228 "seagreen"
6229 "sea green")))
6230 ((eq background 'dark)
6231 (set-face-foreground 'font-lock-type-face
6232 (if (x-color-defined-p "os2pink")
6233 "os2pink"
6234 "pink")))
6235 (t
6236 (set-face-background 'font-lock-type-face "gray90"))))
4ab89e7b 6237 (if (cperl-is-face 'cperl-nonoverridable-face)
f83d2997 6238 nil
4ab89e7b 6239 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
f83d2997
KH
6240 (cond
6241 ((eq background 'light)
4ab89e7b 6242 (set-face-foreground 'cperl-nonoverridable-face
f83d2997
KH
6243 (if (x-color-defined-p "chartreuse3")
6244 "chartreuse3"
6245 "chartreuse")))
6246 ((eq background 'dark)
4ab89e7b 6247 (set-face-foreground 'cperl-nonoverridable-face
f83d2997
KH
6248 (if (x-color-defined-p "orchid1")
6249 "orchid1"
6250 "orange")))))
5bd52f0e
RS
6251;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6252;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6253;;; (cond
6254;;; ((eq background 'light)
6255;;; (set-face-background 'font-lock-other-emphasized-face
6256;;; (if (x-color-defined-p "lightyellow2")
6257;;; "lightyellow2"
6258;;; (if (x-color-defined-p "lightyellow")
6259;;; "lightyellow"
6260;;; "light yellow"))))
6261;;; ((eq background 'dark)
6262;;; (set-face-background 'font-lock-other-emphasized-face
6263;;; (if (x-color-defined-p "navy")
6264;;; "navy"
6265;;; (if (x-color-defined-p "darkgreen")
6266;;; "darkgreen"
6267;;; "dark green"))))
6268;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6269;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6270;;; (copy-face 'bold 'font-lock-emphasized-face)
6271;;; (cond
6272;;; ((eq background 'light)
6273;;; (set-face-background 'font-lock-emphasized-face
6274;;; (if (x-color-defined-p "lightyellow2")
6275;;; "lightyellow2"
6276;;; "lightyellow")))
6277;;; ((eq background 'dark)
6278;;; (set-face-background 'font-lock-emphasized-face
6279;;; (if (x-color-defined-p "navy")
6280;;; "navy"
6281;;; (if (x-color-defined-p "darkgreen")
6282;;; "darkgreen"
6283;;; "dark green"))))
6284;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
db133cb6 6285 (if (cperl-is-face 'font-lock-variable-name-face) nil
f83d2997 6286 (copy-face 'italic 'font-lock-variable-name-face))
db133cb6 6287 (if (cperl-is-face 'font-lock-constant-face) nil
883212ce 6288 (copy-face 'italic 'font-lock-constant-face))))
f83d2997 6289 (setq cperl-faces-init t))
5bd52f0e 6290 (error (message "cperl-init-faces (ignored): %s" errs))))
f83d2997
KH
6291
6292
6293(defun cperl-ps-print-init ()
6294 "Initialization of `ps-print' components for faces used in CPerl."
5bd52f0e
RS
6295 (eval-after-load "ps-print"
6296 '(setq ps-bold-faces
5c8b7eaf 6297 ;; font-lock-variable-name-face
5bd52f0e 6298 ;; font-lock-constant-face
4ab89e7b 6299 (append '(cperl-array-face cperl-hash-face)
5bd52f0e
RS
6300 ps-bold-faces)
6301 ps-italic-faces
6302 ;; font-lock-constant-face
4ab89e7b 6303 (append '(cperl-nonoverridable-face cperl-hash-face)
5bd52f0e
RS
6304 ps-italic-faces)
6305 ps-underlined-faces
6306 ;; font-lock-type-face
4ab89e7b 6307 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
5bd52f0e
RS
6308 ps-underlined-faces))))
6309
6310(defvar ps-print-face-extension-alist)
6311
6312(defun cperl-ps-print (&optional file)
6313 "Pretty-print in CPerl style.
6314If optional argument FILE is an empty string, prints to printer, otherwise
6315to the file FILE. If FILE is nil, prompts for a file name.
6316
6317Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6318 (interactive)
5c8b7eaf
SS
6319 (or file
6320 (setq file (read-from-minibuffer
5bd52f0e
RS
6321 "Print to file (if empty - to printer): "
6322 (concat (buffer-file-name) ".ps")
6323 nil nil 'file-name-history)))
6324 (or (> (length file) 0)
6325 (setq file nil))
6326 (require 'ps-print) ; To get ps-print-face-extension-alist
6327 (let ((ps-print-color-p t)
6328 (ps-print-face-extension-alist ps-print-face-extension-alist))
6329 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6330 (ps-print-buffer-with-faces file)))
6331
6332;;; (defun cperl-ps-print-init ()
6333;;; "Initialization of `ps-print' components for faces used in CPerl."
6334;;; ;; Guard against old versions
6335;;; (defvar ps-underlined-faces nil)
6336;;; (defvar ps-bold-faces nil)
6337;;; (defvar ps-italic-faces nil)
6338;;; (setq ps-bold-faces
6339;;; (append '(font-lock-emphasized-face
4ab89e7b 6340;;; cperl-array-face
5c8b7eaf
SS
6341;;; font-lock-keyword-face
6342;;; font-lock-variable-name-face
6343;;; font-lock-constant-face
6344;;; font-lock-reference-face
5bd52f0e 6345;;; font-lock-other-emphasized-face
4ab89e7b 6346;;; cperl-hash-face)
5bd52f0e
RS
6347;;; ps-bold-faces))
6348;;; (setq ps-italic-faces
4ab89e7b 6349;;; (append '(cperl-nonoverridable-face
5c8b7eaf
SS
6350;;; font-lock-constant-face
6351;;; font-lock-reference-face
5bd52f0e 6352;;; font-lock-other-emphasized-face
4ab89e7b 6353;;; cperl-hash-face)
5bd52f0e
RS
6354;;; ps-italic-faces))
6355;;; (setq ps-underlined-faces
6356;;; (append '(font-lock-emphasized-face
4ab89e7b 6357;;; cperl-array-face
5bd52f0e 6358;;; font-lock-other-emphasized-face
4ab89e7b
SM
6359;;; cperl-hash-face
6360;;; cperl-nonoverridable-face font-lock-type-face)
5bd52f0e
RS
6361;;; ps-underlined-faces))
6362;;; (cons 'font-lock-type-face ps-underlined-faces))
f83d2997
KH
6363
6364
6365(if (cperl-enable-font-lock) (cperl-windowed-init))
6366
db133cb6 6367(defconst cperl-styles-entries
5c8b7eaf
SS
6368 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6369 cperl-label-offset cperl-extra-newline-before-brace
4ab89e7b 6370 cperl-extra-newline-before-brace-multiline
bab27c0c 6371 cperl-merge-trailing-else
db133cb6
RS
6372 cperl-continued-statement-offset))
6373
4ab89e7b
SM
6374(defconst cperl-style-examples
6375"##### Numbers etc are: cperl-indent-level cperl-brace-offset
6376##### cperl-continued-brace-offset cperl-label-offset
6377##### cperl-continued-statement-offset
6378##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6379
6380########### (Do not forget cperl-extra-newline-before-brace-multiline)
6381
6382### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6383if (foo) {
6384 bar
6385 baz;
6386 label:
6387 {
6388 boon;
6389 }
6390} else {
6391 stop;
6392}
6393
6394### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6395if (foo) {
6396 bar
6397 baz;
6398 label:
6399 {
6400 boon;
6401 }
6402} else {
6403 stop;
6404}
6405
6406### GNU 2/0/0/-2/2/nil/t
6407if (foo)
6408 {
6409 bar
6410 baz;
6411 label:
6412 {
6413 boon;
6414 }
6415 }
6416else
6417 {
6418 stop;
6419 }
6420
6421### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6422if (foo)
6423{
6424 bar
6425 baz;
6426 label:
6427 {
6428 boon;
6429 }
6430}
6431else
6432{
6433 stop;
6434}
6435
6436### BSD (=C++, but will not change preexisting merge-trailing-else
6437### and extra-newline-before-brace ) 4/0/-4/-4/4
6438if (foo)
6439{
6440 bar
6441 baz;
6442 label:
6443 {
6444 boon;
6445 }
6446}
6447else
6448{
6449 stop;
6450}
6451
6452### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6453### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6454if (foo)
6455{
6456 bar
6457 baz;
6458 label:
6459 {
6460 boon;
6461 }
6462}
6463else
6464{
6465 stop;
6466}
6467
6468### Whitesmith (=PerlStyle, but will not change preexisting
6469### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6470if (foo)
6471 {
6472 bar
6473 baz;
6474 label:
6475 {
6476 boon;
6477 }
6478 }
6479else
6480 {
6481 stop;
6482 }
6483"
6484"Examples of if/else with different indent styles (with v4.23).")
6485
db133cb6 6486(defconst cperl-style-alist
4ab89e7b 6487 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
db133cb6
RS
6488 (cperl-indent-level . 2)
6489 (cperl-brace-offset . 0)
6490 (cperl-continued-brace-offset . 0)
6491 (cperl-label-offset . -2)
4ab89e7b 6492 (cperl-continued-statement-offset . 2)
db133cb6 6493 (cperl-extra-newline-before-brace . nil)
4ab89e7b
SM
6494 (cperl-extra-newline-before-brace-multiline . nil)
6495 (cperl-merge-trailing-else . t))
6496
83261a2f 6497 ("PerlStyle" ; CPerl with 4 as indent
db133cb6
RS
6498 (cperl-indent-level . 4)
6499 (cperl-brace-offset . 0)
6500 (cperl-continued-brace-offset . 0)
6501 (cperl-label-offset . -4)
4ab89e7b 6502 (cperl-continued-statement-offset . 4)
db133cb6 6503 (cperl-extra-newline-before-brace . nil)
4ab89e7b
SM
6504 (cperl-extra-newline-before-brace-multiline . nil)
6505 (cperl-merge-trailing-else . t))
6506
db133cb6
RS
6507 ("GNU"
6508 (cperl-indent-level . 2)
6509 (cperl-brace-offset . 0)
6510 (cperl-continued-brace-offset . 0)
6511 (cperl-label-offset . -2)
4ab89e7b 6512 (cperl-continued-statement-offset . 2)
db133cb6 6513 (cperl-extra-newline-before-brace . t)
4ab89e7b
SM
6514 (cperl-extra-newline-before-brace-multiline . t)
6515 (cperl-merge-trailing-else . nil))
6516
db133cb6
RS
6517 ("K&R"
6518 (cperl-indent-level . 5)
6519 (cperl-brace-offset . 0)
6520 (cperl-continued-brace-offset . -5)
6521 (cperl-label-offset . -5)
4ab89e7b 6522 (cperl-continued-statement-offset . 5)
db133cb6 6523 ;;(cperl-extra-newline-before-brace . nil) ; ???
4ab89e7b
SM
6524 ;;(cperl-extra-newline-before-brace-multiline . nil)
6525 (cperl-merge-trailing-else . nil))
6526
db133cb6
RS
6527 ("BSD"
6528 (cperl-indent-level . 4)
6529 (cperl-brace-offset . 0)
6530 (cperl-continued-brace-offset . -4)
6531 (cperl-label-offset . -4)
4ab89e7b 6532 (cperl-continued-statement-offset . 4)
db133cb6 6533 ;;(cperl-extra-newline-before-brace . nil) ; ???
4ab89e7b
SM
6534 ;;(cperl-extra-newline-before-brace-multiline . nil)
6535 ;;(cperl-merge-trailing-else . nil) ; ???
6536 )
6537
db133cb6
RS
6538 ("C++"
6539 (cperl-indent-level . 4)
6540 (cperl-brace-offset . 0)
6541 (cperl-continued-brace-offset . -4)
6542 (cperl-label-offset . -4)
6543 (cperl-continued-statement-offset . 4)
4ab89e7b
SM
6544 (cperl-extra-newline-before-brace . t)
6545 (cperl-extra-newline-before-brace-multiline . t)
6546 (cperl-merge-trailing-else . nil))
6547
db133cb6
RS
6548 ("Whitesmith"
6549 (cperl-indent-level . 4)
6550 (cperl-brace-offset . 0)
6551 (cperl-continued-brace-offset . 0)
6552 (cperl-label-offset . -4)
4ab89e7b 6553 (cperl-continued-statement-offset . 4)
db133cb6 6554 ;;(cperl-extra-newline-before-brace . nil) ; ???
4ab89e7b
SM
6555 ;;(cperl-extra-newline-before-brace-multiline . nil)
6556 ;;(cperl-merge-trailing-else . nil) ; ???
6557 )
6558 ("Current"))
6559 "List of variables to set to get a particular indentation style.
6560Should be used via `cperl-set-style' or via Perl menu.
6561
6562See examples in `cperl-style-examples'.")
db133cb6 6563
f83d2997 6564(defun cperl-set-style (style)
f94a632a 6565 "Set CPerl mode variables to use one of several different indentation styles.
f83d2997 6566The arguments are a string representing the desired style.
5c8b7eaf 6567The list of styles is in `cperl-style-alist', available styles
4ab89e7b 6568are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
db133cb6
RS
6569
6570The current value of style is memorized (unless there is a memorized
6571data already), may be restored by `cperl-set-style-back'.
6572
6573Chosing \"Current\" style will not change style, so this may be used for
4ab89e7b 6574side-effect of memorizing only. Examples in `cperl-style-examples'."
5c8b7eaf 6575 (interactive
15ca5699 6576 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
db133cb6 6577 cperl-style-alist)))
f83d2997 6578 (list (completing-read "Enter style: " list nil 'insist))))
db133cb6
RS
6579 (or cperl-old-style
6580 (setq cperl-old-style
6581 (mapcar (function
6582 (lambda (name)
6583 (cons name (eval name))))
6584 cperl-styles-entries)))
6585 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
f83d2997
KH
6586 (while style
6587 (setq setting (car style) style (cdr style))
db133cb6
RS
6588 (set (car setting) (cdr setting)))))
6589
6590(defun cperl-set-style-back ()
810fb442 6591 "Restore a style memorized by `cperl-set-style'."
db133cb6
RS
6592 (interactive)
6593 (or cperl-old-style (error "The style was not changed"))
6594 (let (setting)
6595 (while cperl-old-style
5c8b7eaf 6596 (setq setting (car cperl-old-style)
db133cb6
RS
6597 cperl-old-style (cdr cperl-old-style))
6598 (set (car setting) (cdr setting)))))
f83d2997
KH
6599
6600(defun cperl-check-syntax ()
6601 (interactive)
6602 (require 'mode-compile)
db133cb6
RS
6603 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6604 (eval '(mode-compile)))) ; Avoid a warning
f83d2997
KH
6605
6606(defun cperl-info-buffer (type)
6607 ;; Returns buffer with documentation. Creates if missing.
6608 ;; If TYPE, this vars buffer.
6609 ;; Special care is taken to not stomp over an existing info buffer
6610 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6611 (info (get-buffer bname))
6612 (oldbuf (get-buffer "*info*")))
6613 (if info info
6614 (save-window-excursion
6615 ;; Get Info running
6616 (require 'info)
6617 (cond (oldbuf
6618 (set-buffer oldbuf)
6619 (rename-buffer "*info-perl-tmp*")))
6620 (save-window-excursion
6621 (info))
6622 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6623 (set-buffer "*info*")
6624 (rename-buffer bname)
6625 (cond (oldbuf
6626 (set-buffer "*info-perl-tmp*")
6627 (rename-buffer "*info*")
6628 (set-buffer bname)))
029cb4d5 6629 (make-local-variable 'window-min-height)
f83d2997
KH
6630 (setq window-min-height 2)
6631 (current-buffer)))))
6632
6633(defun cperl-word-at-point (&optional p)
f94a632a 6634 "Return the word at point or at P."
f83d2997
KH
6635 (save-excursion
6636 (if p (goto-char p))
6637 (or (cperl-word-at-point-hard)
6638 (progn
6639 (require 'etags)
6640 (funcall (or (and (boundp 'find-tag-default-function)
6641 find-tag-default-function)
6642 (get major-mode 'find-tag-default-function)
6643 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6644 ;; automatically used within `find-tag-default':
6645 'find-tag-default))))))
6646
6647(defun cperl-info-on-command (command)
f94a632a 6648 "Show documentation for Perl command COMMAND in other window.
f83d2997
KH
6649If perl-info buffer is shown in some frame, uses this frame.
6650Customized by setting variables `cperl-shrink-wrap-info-frame',
6651`cperl-max-help-size'."
5c8b7eaf 6652 (interactive
f83d2997 6653 (let* ((default (cperl-word-at-point))
5c8b7eaf 6654 (read (read-string
83261a2f
SM
6655 (format "Find doc for Perl function (default %s): "
6656 default))))
5c8b7eaf 6657 (list (if (equal read "")
83261a2f
SM
6658 default
6659 read))))
f83d2997
KH
6660
6661 (let ((buffer (current-buffer))
6662 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6663 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6664 max-height char-height buf-list)
6665 (if (string-match "^-[a-zA-Z]$" command)
6666 (setq cmd-desc "^-X[ \t\n]"))
6667 (setq isvar (string-match "^[$@%]" command)
6668 buf (cperl-info-buffer isvar)
6669 iniwin (selected-window)
6670 fr1 (window-frame iniwin))
6671 (set-buffer buf)
fc49c9c6 6672 (goto-char (point-min))
5c8b7eaf 6673 (or isvar
f83d2997
KH
6674 (progn (re-search-forward "^-X[ \t\n]")
6675 (forward-line -1)))
6676 (if (re-search-forward cmd-desc nil t)
6677 (progn
6678 ;; Go back to beginning of the group (ex, for qq)
6679 (if (re-search-backward "^[ \t\n\f]")
6680 (forward-line 1))
6681 (beginning-of-line)
5c8b7eaf 6682 ;; Get some of
f83d2997
KH
6683 (setq pos (point)
6684 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6685 (while (and (not win) buf-list)
6686 (setq win (get-buffer-window (car buf-list) t))
6687 (setq buf-list (cdr buf-list)))
6688 (or (not win)
6689 (eq (window-buffer win) buf)
6690 (set-window-buffer win buf))
6691 (and win (setq fr2 (window-frame win)))
6692 (if (or (not fr2) (eq fr1 fr2))
6693 (pop-to-buffer buf)
6694 (special-display-popup-frame buf) ; Make it visible
6695 (select-window win))
6696 (goto-char pos) ; Needed (?!).
6697 ;; Resize
6698 (setq iniheight (window-height)
6699 frheight (frame-height)
6700 not-loner (< iniheight (1- frheight))) ; Are not alone
5c8b7eaf 6701 (cond ((if not-loner cperl-max-help-size
f83d2997 6702 cperl-shrink-wrap-info-frame)
5c8b7eaf
SS
6703 (setq height
6704 (+ 2
6705 (count-lines
6706 pos
f83d2997
KH
6707 (save-excursion
6708 (if (re-search-forward
6709 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6710 (match-beginning 0) (point-max)))))
5c8b7eaf 6711 max-height
f83d2997
KH
6712 (if not-loner
6713 (/ (* (- frheight 3) cperl-max-help-size) 100)
6714 (setq char-height (frame-char-height))
6715 ;; Non-functioning under OS/2:
6716 (if (eq char-height 1) (setq char-height 18))
6717 ;; Title, menubar, + 2 for slack
83261a2f 6718 (- (/ (x-display-pixel-height) char-height) 4)))
f83d2997
KH
6719 (if (> height max-height) (setq height max-height))
6720 ;;(message "was %s doing %s" iniheight height)
6721 (if not-loner
6722 (enlarge-window (- height iniheight))
6723 (set-frame-height (window-frame win) (1+ height)))))
6724 (set-window-start (selected-window) pos))
6725 (message "No entry for %s found." command))
6726 ;;(pop-to-buffer buffer)
6727 (select-window iniwin)))
6728
6729(defun cperl-info-on-current-command ()
029cb4d5 6730 "Show documentation for Perl command at point in other window."
f83d2997
KH
6731 (interactive)
6732 (cperl-info-on-command (cperl-word-at-point)))
6733
6734(defun cperl-imenu-info-imenu-search ()
6735 (if (looking-at "^-X[ \t\n]") nil
6736 (re-search-backward
6737 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6738 (forward-line 1)))
6739
5c8b7eaf 6740(defun cperl-imenu-info-imenu-name ()
f83d2997
KH
6741 (buffer-substring
6742 (match-beginning 1) (match-end 1)))
6743
6744(defun cperl-imenu-on-info ()
4ab89e7b
SM
6745 "Shows imenu for Perl Info Buffer.
6746Opens Perl Info buffer if needed."
f83d2997
KH
6747 (interactive)
6748 (let* ((buffer (current-buffer))
6749 imenu-create-index-function
5c8b7eaf
SS
6750 imenu-prev-index-position-function
6751 imenu-extract-index-name-function
f83d2997
KH
6752 (index-item (save-restriction
6753 (save-window-excursion
6754 (set-buffer (cperl-info-buffer nil))
5c8b7eaf 6755 (setq imenu-create-index-function
f83d2997
KH
6756 'imenu-default-create-index-function
6757 imenu-prev-index-position-function
6758 'cperl-imenu-info-imenu-search
6759 imenu-extract-index-name-function
6760 'cperl-imenu-info-imenu-name)
6761 (imenu-choose-buffer-index)))))
6762 (and index-item
6763 (progn
6764 (push-mark)
6765 (pop-to-buffer "*info-perl*")
6766 (cond
6767 ((markerp (cdr index-item))
6768 (goto-char (marker-position (cdr index-item))))
6769 (t
6770 (goto-char (cdr index-item))))
6771 (set-window-start (selected-window) (point))
6772 (pop-to-buffer buffer)))))
6773
6774(defun cperl-lineup (beg end &optional step minshift)
6775 "Lineup construction in a region.
6776Beginning of region should be at the start of a construction.
6777All first occurrences of this construction in the lines that are
6778partially contained in the region are lined up at the same column.
6779
6780MINSHIFT is the minimal amount of space to insert before the construction.
6781STEP is the tabwidth to position constructions.
029cb4d5 6782If STEP is nil, `cperl-lineup-step' will be used
15ca5699 6783\(or `cperl-indent-level', if `cperl-lineup-step' is nil).
f83d2997
KH
6784Will not move the position at the start to the left."
6785 (interactive "r")
4ab89e7b 6786 (let (search col tcol seen b)
f83d2997
KH
6787 (save-excursion
6788 (goto-char end)
6789 (end-of-line)
6790 (setq end (point-marker))
6791 (goto-char beg)
6792 (skip-chars-forward " \t\f")
6793 (setq beg (point-marker))
6794 (indent-region beg end nil)
6795 (goto-char beg)
6796 (setq col (current-column))
6797 (if (looking-at "[a-zA-Z0-9_]")
6798 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6799 (setq search
5c8b7eaf
SS
6800 (concat "\\<"
6801 (regexp-quote
f83d2997
KH
6802 (buffer-substring (match-beginning 0)
6803 (match-end 0))) "\\>"))
6804 (error "Cannot line up in a middle of the word"))
6805 (if (looking-at "$")
6806 (error "Cannot line up end of line"))
6807 (setq search (regexp-quote (char-to-string (following-char)))))
6808 (setq step (or step cperl-lineup-step cperl-indent-level))
6809 (or minshift (setq minshift 1))
6810 (while (progn
6811 (beginning-of-line 2)
5c8b7eaf 6812 (and (< (point) end)
f83d2997
KH
6813 (re-search-forward search end t)
6814 (goto-char (match-beginning 0))))
6815 (setq tcol (current-column) seen t)
6816 (if (> tcol col) (setq col tcol)))
6817 (or seen
6818 (error "The construction to line up occurred only once"))
6819 (goto-char beg)
6820 (setq col (+ col minshift))
6821 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
5c8b7eaf 6822 (while
f83d2997 6823 (progn
4ab89e7b 6824 (cperl-make-indent col)
5c8b7eaf
SS
6825 (beginning-of-line 2)
6826 (and (< (point) end)
f83d2997
KH
6827 (re-search-forward search end t)
6828 (goto-char (match-beginning 0)))))))) ; No body
6829
4ab89e7b 6830(defun cperl-etags (&optional add all files) ;; NOT USED???
f83d2997
KH
6831 "Run etags with appropriate options for Perl files.
6832If optional argument ALL is `recursive', will process Perl files
6833in subdirectories too."
6834 (interactive)
6835 (let ((cmd "etags")
4ab89e7b
SM
6836 (args '("-l" "none" "-r"
6837 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6838 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6839 "-r"
6840 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6841 "-r"
6842 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
f83d2997
KH
6843 res)
6844 (if add (setq args (cons "-a" args)))
6845 (or files (setq files (list buffer-file-name)))
6846 (cond
6847 ((eq all 'recursive)
6848 ;;(error "Not implemented: recursive")
5c8b7eaf 6849 (setq args (append (list "-e"
f83d2997
KH
6850 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6851 use File::Find;
6852 find(\\&wanted, '.');
5c8b7eaf 6853 exec @ARGV;"
f83d2997
KH
6854 cmd) args)
6855 cmd "perl"))
5c8b7eaf 6856 (all
f83d2997 6857 ;;(error "Not implemented: all")
5c8b7eaf 6858 (setq args (append (list "-e"
f83d2997 6859 "push @ARGV, <*.PL *.pl *.pm>;
5c8b7eaf 6860 exec @ARGV;"
f83d2997
KH
6861 cmd) args)
6862 cmd "perl"))
6863 (t
6864 (setq args (append args files))))
6865 (setq res (apply 'call-process cmd nil nil nil args))
6866 (or (eq res 0)
6867 (message "etags returned \"%s\"" res))))
6868
6869(defun cperl-toggle-auto-newline ()
6870 "Toggle the state of `cperl-auto-newline'."
6871 (interactive)
6872 (setq cperl-auto-newline (not cperl-auto-newline))
5c8b7eaf 6873 (message "Newlines will %sbe auto-inserted now."
f83d2997
KH
6874 (if cperl-auto-newline "" "not ")))
6875
6876(defun cperl-toggle-abbrev ()
6877 "Toggle the state of automatic keyword expansion in CPerl mode."
6878 (interactive)
6879 (abbrev-mode (if abbrev-mode 0 1))
5c8b7eaf 6880 (message "Perl control structure will %sbe auto-inserted now."
f83d2997
KH
6881 (if abbrev-mode "" "not ")))
6882
6883
6884(defun cperl-toggle-electric ()
6885 "Toggle the state of parentheses doubling in CPerl mode."
6886 (interactive)
6887 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
5c8b7eaf 6888 (message "Parentheses will %sbe auto-doubled now."
f83d2997
KH
6889 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6890
db133cb6 6891(defun cperl-toggle-autohelp ()
f739b53b
SM
6892 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6893Delay of auto-help controlled by `cperl-lazy-help-time'."
db133cb6
RS
6894 (interactive)
6895 (if (fboundp 'run-with-idle-timer)
6896 (progn
6897 (if cperl-lazy-installed
f739b53b 6898 (cperl-lazy-unstall)
db133cb6 6899 (cperl-lazy-install))
5c8b7eaf 6900 (message "Perl help messages will %sbe automatically shown now."
db133cb6
RS
6901 (if cperl-lazy-installed "" "not ")))
6902 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6903
6904(defun cperl-toggle-construct-fix ()
6905 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6906 (interactive)
5c8b7eaf 6907 (setq cperl-indent-region-fix-constructs
5bd52f0e
RS
6908 (if cperl-indent-region-fix-constructs
6909 nil
6910 1))
5c8b7eaf 6911 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
db133cb6
RS
6912 (if cperl-indent-region-fix-constructs "" "not ")))
6913
4ab89e7b
SM
6914(defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6915 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6916Nonpositive numeric argument disables debugging messages. The message
6917summarizes which regions it was decided to rescan for syntactic constructs.
6918
6919The message looks like this:
6920
6921 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6922
6923Numbers are character positions in the buffer. REQ provides the range to
6924rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6925for correct operation it should start and end outside any special syntactic
6926construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6927by CPerl."
6928 (interactive "P")
6929 (or arg
6930 (setq arg (if (eq cperl-syntaxify-by-font-lock
6931 (if backtrace 'backtrace 'message)) 0 1)))
6932 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6933 (setq cperl-syntaxify-by-font-lock arg)
6934 (message "Debugging messages of syntax unwind %sabled."
6935 (if (eq arg t) "dis" "en")))
6936
f83d2997
KH
6937;;;; Tags file creation.
6938
6939(defvar cperl-tmp-buffer " *cperl-tmp*")
6940
6941(defun cperl-setup-tmp-buf ()
6942 (set-buffer (get-buffer-create cperl-tmp-buffer))
6943 (set-syntax-table cperl-mode-syntax-table)
6944 (buffer-disable-undo)
6945 (auto-fill-mode 0)
6946 (if cperl-use-syntax-table-text-property-for-tags
6947 (progn
029cb4d5 6948 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997
KH
6949 ;; Do not introduce variable if not needed, we check it!
6950 (set 'parse-sexp-lookup-properties t))))
6951
6952(defun cperl-xsub-scan ()
f83d2997 6953 (require 'imenu)
5c8b7eaf 6954 (let ((index-alist '())
f83d2997
KH
6955 (prev-pos 0) index index1 name package prefix)
6956 (goto-char (point-min))
f83d2997
KH
6957 ;; Search for the function
6958 (progn ;;save-match-data
6959 (while (re-search-forward
6960 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6961 nil t)
f83d2997 6962 (cond
83261a2f 6963 ((match-beginning 2) ; SECTION
f83d2997
KH
6964 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6965 (goto-char (match-beginning 0))
6966 (skip-chars-forward " \t")
6967 (forward-char 1)
6968 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6969 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6970 (setq prefix nil)))
6971 ((not package) nil) ; C language section
6972 ((match-beginning 3) ; XSUB
6973 (goto-char (1+ (match-beginning 3)))
6974 (setq index (imenu-example--name-and-position))
6975 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6976 (if (and prefix (string-match (concat "^" prefix) name))
6977 (setq name (substring name (length prefix))))
6978 (cond ((string-match "::" name) nil)
6979 (t
6980 (setq index1 (cons (concat package "::" name) (cdr index)))
6981 (push index1 index-alist)))
6982 (setcar index name)
6983 (push index index-alist))
6984 (t ; BOOT: section
6985 ;; (beginning-of-line)
6986 (setq index (imenu-example--name-and-position))
6987 (setcar index (concat package "::BOOT:"))
6988 (push index index-alist)))))
f83d2997
KH
6989 index-alist))
6990
6c389151
SM
6991(defvar cperl-unreadable-ok nil)
6992
6993(defun cperl-find-tags (ifile xs topdir)
83261a2f
SM
6994 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6995 (cperl-pod-here-fontify nil) f file)
f83d2997
KH
6996 (save-excursion
6997 (if b (set-buffer b)
83261a2f 6998 (cperl-setup-tmp-buf))
f83d2997 6999 (erase-buffer)
6c389151
SM
7000 (condition-case err
7001 (setq file (car (insert-file-contents ifile)))
7002 (error (if cperl-unreadable-ok nil
7003 (if (y-or-n-p
7004 (format "File %s unreadable. Continue? " ifile))
7005 (setq cperl-unreadable-ok t)
7006 (error "Aborting: unreadable file %s" ifile)))))
a1506d29 7007 (if (not file)
6c389151 7008 (message "Unreadable file %s" ifile)
83261a2f
SM
7009 (message "Scanning file %s ..." file)
7010 (if (and cperl-use-syntax-table-text-property-for-tags
7011 (not xs))
7012 (condition-case err ; after __END__ may have garbage
7013 (cperl-find-pods-heres nil nil noninteractive)
7014 (error (message "While scanning for syntax: %s" err))))
7015 (if xs
7016 (setq lst (cperl-xsub-scan))
7017 (setq ind (cperl-imenu--create-perl-index))
7018 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
7019 (setq lst
7020 (mapcar
7021 (function
7022 (lambda (elt)
7023 (cond ((string-match "^[_a-zA-Z]" (car elt))
7024 (goto-char (cdr elt))
7025 (beginning-of-line) ; pos should be of the start of the line
7026 (list (car elt)
7027 (point)
7028 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
7029 (buffer-substring (progn
7030 (goto-char (cdr elt))
7031 ;; After name now...
7032 (or (eolp) (forward-char 1))
7033 (point))
7034 (progn
7035 (beginning-of-line)
7036 (point))))))))
7037 lst))
7038 (erase-buffer)
7039 (while lst
7040 (setq elt (car lst) lst (cdr lst))
7041 (if elt
7042 (progn
7043 (insert (elt elt 3)
7044 127
7045 (if (string-match "^package " (car elt))
7046 (substring (car elt) 8)
7047 (car elt) )
7048 1
7049 (number-to-string (elt elt 2)) ; Line
7050 ","
7051 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
7052 "\n")
7053 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
7054 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
7055 (elt elt 3)))
7056 ;; Need to insert the name without package as well
15ca5699 7057 (setq lst (cons (cons (substring (elt elt 3)
83261a2f
SM
7058 (match-beginning 1)
7059 (match-end 1))
7060 (cdr elt))
7061 lst))))))
7062 (setq pos (point))
7063 (goto-char 1)
7064 (setq rel file)
7065 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7066 (set-text-properties 0 (length rel) nil rel)
7067 (and (equal topdir (substring rel 0 (length topdir)))
7068 (setq rel (substring file (length topdir))))
7069 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
7070 (setq ret (buffer-substring 1 (point-max)))
7071 (erase-buffer)
7072 (or noninteractive
7073 (message "Scanning file %s finished" file))
7074 ret))))
f83d2997
KH
7075
7076(defun cperl-add-tags-recurse-noxs ()
4ab89e7b 7077 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
f83d2997
KH
7078Use as
7079 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
4ab89e7b 7080 -f cperl-add-tags-recurse-noxs
f83d2997
KH
7081"
7082 (cperl-write-tags nil nil t t nil t))
7083
4ab89e7b
SM
7084(defun cperl-add-tags-recurse-noxs-fullpath ()
7085 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7086Writes down fullpath, so TAGS is relocatable (but if the build directory
7087is relocated, the file TAGS inside it breaks). Use as
7088 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7089 -f cperl-add-tags-recurse-noxs-fullpath
7090"
7091 (cperl-write-tags nil nil t t nil t ""))
7092
f83d2997
KH
7093(defun cperl-add-tags-recurse ()
7094 "Add to TAGS file data for Perl files in the current directory and kids.
7095Use as
7096 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
5c8b7eaf 7097 -f cperl-add-tags-recurse
f83d2997
KH
7098"
7099 (cperl-write-tags nil nil t t))
7100
7101(defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7102 ;; If INBUFFER, do not select buffer, and do not save
7103 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7104 (require 'etags)
7105 (if file nil
7106 (setq file (if dir default-directory (buffer-file-name)))
7107 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7108 (or topdir
7109 (setq topdir default-directory))
7110 (let ((tags-file-name "TAGS")
7111 (case-fold-search (eq system-type 'emx))
6c389151 7112 xs rel tm)
f83d2997
KH
7113 (save-excursion
7114 (cond (inbuffer nil) ; Already there
7115 ((file-exists-p tags-file-name)
5bd52f0e
RS
7116 (if cperl-xemacs-p
7117 (visit-tags-table-buffer)
83261a2f 7118 (visit-tags-table-buffer tags-file-name)))
f83d2997
KH
7119 (t (set-buffer (find-file-noselect tags-file-name))))
7120 (cond
7121 (dir
7122 (cond ((eq erase 'ignore))
7123 (erase
7124 (erase-buffer)
7125 (setq erase 'ignore)))
a1506d29 7126 (let ((files
6c389151 7127 (condition-case err
a1506d29 7128 (directory-files file t
6c389151
SM
7129 (if recurse nil cperl-scan-files-regexp)
7130 t)
7131 (error
7132 (if cperl-unreadable-ok nil
7133 (if (y-or-n-p
7134 (format "Directory %s unreadable. Continue? " file))
a1506d29 7135 (setq cperl-unreadable-ok t
83261a2f 7136 tm nil) ; Return empty list
6c389151 7137 (error "Aborting: unreadable directory %s" file)))))))
15ca5699 7138 (mapcar (function
83261a2f
SM
7139 (lambda (file)
7140 (cond
7141 ((string-match cperl-noscan-files-regexp file)
7142 nil)
7143 ((not (file-directory-p file))
7144 (if (string-match cperl-scan-files-regexp file)
7145 (cperl-write-tags file erase recurse nil t noxs topdir)))
7146 ((not recurse) nil)
7147 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7148 files)))
f83d2997
KH
7149 (t
7150 (setq xs (string-match "\\.xs$" file))
7151 (if (not (and xs noxs))
7152 (progn
7153 (cond ((eq erase 'ignore) (goto-char (point-max)))
83261a2f
SM
7154 (erase (erase-buffer))
7155 (t
7156 (goto-char 1)
7157 (setq rel file)
7158 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7159 (set-text-properties 0 (length rel) nil rel)
7160 (and (equal topdir (substring rel 0 (length topdir)))
7161 (setq rel (substring file (length topdir))))
7162 (if (search-forward (concat "\f\n" rel ",") nil t)
7163 (progn
7164 (search-backward "\f\n")
7165 (delete-region (point)
7166 (save-excursion
7167 (forward-char 1)
7168 (if (search-forward "\f\n"
7169 nil 'toend)
7170 (- (point) 2)
7171 (point-max)))))
7172 (goto-char (point-max)))))
f83d2997 7173 (insert (cperl-find-tags file xs topdir))))))
83261a2f
SM
7174 (if inbuffer nil ; Delegate to the caller
7175 (save-buffer 0) ; No backup
f83d2997
KH
7176 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7177 (initialize-new-tags-table))))))
7178
7179(defvar cperl-tags-hier-regexp-list
5c8b7eaf 7180 (concat
f83d2997
KH
7181 "^\\("
7182 "\\(package\\)\\>"
7183 "\\|"
7184 "sub\\>[^\n]+::"
7185 "\\|"
7186 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7187 "\\|"
7188 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7189 "\\)"))
7190
7191(defvar cperl-hierarchy '(() ())
f94a632a 7192 "Global hierarchy of classes.")
f83d2997
KH
7193
7194(defun cperl-tags-hier-fill ()
7195 ;; Suppose we are in a tag table cooked by cperl.
7196 (goto-char 1)
7197 (let (type pack name pos line chunk ord cons1 file str info fileind)
7198 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
5c8b7eaf 7199 (setq pos (match-beginning 0)
f83d2997
KH
7200 pack (match-beginning 2))
7201 (beginning-of-line)
7202 (if (looking-at (concat
7203 "\\([^\n]+\\)"
7204 "\C-?"
7205 "\\([^\n]+\\)"
7206 "\C-a"
7207 "\\([0-9]+\\)"
7208 ","
7209 "\\([0-9]+\\)"))
7210 (progn
7211 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7212 name (buffer-substring (match-beginning 2) (match-end 2))
7213 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
5bd52f0e 7214 line (buffer-substring (match-beginning 3) (match-end 3))
f83d2997 7215 ord (if pack 1 0)
f83d2997 7216 file (file-of-tag)
5bd52f0e
RS
7217 fileind (format "%s:%s" file line)
7218 ;; Moves to beginning of the next line:
7219 info (cperl-etags-snarf-tag file line))
f83d2997
KH
7220 ;; Move back
7221 (forward-char -1)
7222 ;; Make new member of hierarchy name ==> file ==> pos if needed
7223 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7224 ;; Name known
7225 (setcdr cons1 (cons (cons fileind (vector file info))
7226 (cdr cons1)))
7227 ;; First occurrence of the name, start alist
7228 (setq cons1 (cons name (list (cons fileind (vector file info)))))
5c8b7eaf 7229 (if pack
f83d2997
KH
7230 (setcar (cdr cperl-hierarchy)
7231 (cons cons1 (nth 1 cperl-hierarchy)))
7232 (setcar cperl-hierarchy
7233 (cons cons1 (car cperl-hierarchy)))))))
7234 (end-of-line))))
7235
7236(defun cperl-tags-hier-init (&optional update)
7237 "Show hierarchical menu of classes and methods.
7238Finds info about classes by a scan of loaded TAGS files.
7239Supposes that the TAGS files contain fully qualified function names.
7240One may build such TAGS files from CPerl mode menu."
7241 (interactive)
7242 (require 'etags)
7243 (require 'imenu)
7244 (if (or update (null (nth 2 cperl-hierarchy)))
83261a2f
SM
7245 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7246 (or (nthcdr 2 elt)
7247 ;; Only in one file
7248 (setcdr elt (cdr (nth 1 elt)))))))
7249 pack name cons1 to l1 l2 l3 l4 b)
f83d2997
KH
7250 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7251 (setq cperl-hierarchy (list l1 l2 l3))
5bd52f0e
RS
7252 (if cperl-xemacs-p ; Not checked
7253 (progn
7254 (or tags-file-name
7255 ;; Does this work in XEmacs?
f83d2997
KH
7256 (call-interactively 'visit-tags-table))
7257 (message "Updating list of classes...")
5bd52f0e
RS
7258 (set-buffer (get-file-buffer tags-file-name))
7259 (cperl-tags-hier-fill))
7260 (or tags-table-list
7261 (call-interactively 'visit-tags-table))
4ab89e7b
SM
7262 (mapcar
7263 (function
7264 (lambda (tagsfile)
5bd52f0e 7265 (message "Updating list of classes... %s" tagsfile)
f83d2997
KH
7266 (set-buffer (get-file-buffer tagsfile))
7267 (cperl-tags-hier-fill)))
7268 tags-table-list)
5bd52f0e 7269 (message "Updating list of classes... postprocessing..."))
f83d2997
KH
7270 (mapcar remover (car cperl-hierarchy))
7271 (mapcar remover (nth 1 cperl-hierarchy))
7272 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7273 (cons "Methods: " (car cperl-hierarchy))))
7274 (cperl-tags-treeify to 1)
7275 (setcar (nthcdr 2 cperl-hierarchy)
7276 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7277 (message "Updating list of classes: done, requesting display...")
7278 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7279 ))
7280 (or (nth 2 cperl-hierarchy)
7281 (error "No items found"))
7282 (setq update
7283;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
83261a2f
SM
7284 (if (if (fboundp 'display-popup-menus-p)
7285 (let ((f 'display-popup-menus-p))
7286 (funcall f))
7287 window-system)
f83d2997
KH
7288 (x-popup-menu t (nth 2 cperl-hierarchy))
7289 (require 'tmm)
7290 (tmm-prompt (nth 2 cperl-hierarchy))))
7291 (if (and update (listp update))
7292 (progn (while (cdr update) (setq update (cdr update)))
7293 (setq update (car update)))) ; Get the last from the list
5c8b7eaf 7294 (if (vectorp update)
f83d2997
KH
7295 (progn
7296 (find-file (elt update 0))
5bd52f0e 7297 (cperl-etags-goto-tag-location (elt update 1))))
f83d2997
KH
7298 (if (eq update -999) (cperl-tags-hier-init t)))
7299
7300(defun cperl-tags-treeify (to level)
7301 ;; cadr of `to' is read-write. On start it is a cons
5c8b7eaf 7302 (let* ((regexp (concat "^\\(" (mapconcat
f83d2997
KH
7303 'identity
7304 (make-list level "[_a-zA-Z0-9]+")
7305 "::")
7306 "\\)\\(::\\)?"))
7307 (packages (cdr (nth 1 to)))
7308 (methods (cdr (nth 2 to)))
7309 l1 head tail cons1 cons2 ord writeto packs recurse
7310 root-packages root-functions ms many_ms same_name ps
7311 (move-deeper
5c8b7eaf 7312 (function
f83d2997
KH
7313 (lambda (elt)
7314 (cond ((and (string-match regexp (car elt))
7315 (or (eq ord 1) (match-end 2)))
7316 (setq head (substring (car elt) 0 (match-end 1))
5c8b7eaf 7317 tail (if (match-end 2) (substring (car elt)
f83d2997
KH
7318 (match-end 2)))
7319 recurse t)
7320 (if (setq cons1 (assoc head writeto)) nil
7321 ;; Need to init new head
7322 (setcdr writeto (cons (list head (list "Packages: ")
7323 (list "Methods: "))
7324 (cdr writeto)))
7325 (setq cons1 (nth 1 writeto)))
7326 (setq cons2 (nth ord cons1)) ; Either packs or meths
7327 (setcdr cons2 (cons elt (cdr cons2))))
7328 ((eq ord 2)
7329 (setq root-functions (cons elt root-functions)))
7330 (t
7331 (setq root-packages (cons elt root-packages))))))))
7332 (setcdr to l1) ; Init to dynamic space
7333 (setq writeto to)
7334 (setq ord 1)
7335 (mapcar move-deeper packages)
7336 (setq ord 2)
7337 (mapcar move-deeper methods)
7338 (if recurse
7339 (mapcar (function (lambda (elt)
7340 (cperl-tags-treeify elt (1+ level))))
7341 (cdr to)))
7342 ;;Now clean up leaders with one child only
7343 (mapcar (function (lambda (elt)
5c8b7eaf 7344 (if (not (and (listp (cdr elt))
f83d2997
KH
7345 (eq (length elt) 2))) nil
7346 (setcar elt (car (nth 1 elt)))
7347 (setcdr elt (cdr (nth 1 elt))))))
7348 (cdr to))
7349 ;; Sort the roots of subtrees
7350 (if (default-value 'imenu-sort-function)
7351 (setcdr to
7352 (sort (cdr to) (default-value 'imenu-sort-function))))
7353 ;; Now add back functions removed from display
7354 (mapcar (function (lambda (elt)
7355 (setcdr to (cons elt (cdr to)))))
7356 (if (default-value 'imenu-sort-function)
7357 (nreverse
7358 (sort root-functions (default-value 'imenu-sort-function)))
7359 root-functions))
7360 ;; Now add back packages removed from display
7361 (mapcar (function (lambda (elt)
5c8b7eaf
SS
7362 (setcdr to (cons (cons (concat "package " (car elt))
7363 (cdr elt))
f83d2997
KH
7364 (cdr to)))))
7365 (if (default-value 'imenu-sort-function)
5c8b7eaf 7366 (nreverse
f83d2997 7367 (sort root-packages (default-value 'imenu-sort-function)))
83261a2f 7368 root-packages))))
f83d2997
KH
7369
7370;;;(x-popup-menu t
5c8b7eaf 7371;;; '(keymap "Name1"
f83d2997 7372;;; ("Ret1" "aa")
5c8b7eaf
SS
7373;;; ("Head1" "ab"
7374;;; keymap "Name2"
f83d2997
KH
7375;;; ("Tail1" "x") ("Tail2" "y"))))
7376
7377(defun cperl-list-fold (list name limit)
7378 (let (list1 list2 elt1 (num 0))
7379 (if (<= (length list) limit) list
7380 (setq list1 nil list2 nil)
7381 (while list
5c8b7eaf 7382 (setq num (1+ num)
f83d2997
KH
7383 elt1 (car list)
7384 list (cdr list))
7385 (if (<= num imenu-max-items)
7386 (setq list2 (cons elt1 list2))
7387 (setq list1 (cons (cons name
7388 (nreverse list2))
7389 list1)
7390 list2 (list elt1)
7391 num 1)))
7392 (nreverse (cons (cons name
7393 (nreverse list2))
7394 list1)))))
7395
7396(defun cperl-menu-to-keymap (menu &optional name)
7397 (let (list)
5c8b7eaf
SS
7398 (cons 'keymap
7399 (mapcar
7400 (function
f83d2997
KH
7401 (lambda (elt)
7402 (cond ((listp (cdr elt))
7403 (setq list (cperl-list-fold
7404 (cdr elt) (car elt) imenu-max-items))
7405 (cons nil
7406 (cons (car elt)
7407 (cperl-menu-to-keymap list))))
7408 (t
7409 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7410 (cperl-list-fold menu "Root" imenu-max-items)))))
7411
7412\f
7413(defvar cperl-bad-style-regexp
7414 (mapconcat 'identity
83261a2f 7415 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
15ca5699 7416 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
83261a2f 7417 "\\|")
f83d2997
KH
7418 "Finds places such that insertion of a whitespace may help a lot.")
7419
5c8b7eaf 7420(defvar cperl-not-bad-style-regexp
15ca5699 7421 (mapconcat
83261a2f 7422 'identity
f83d2997
KH
7423 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7424 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7425 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
4ab89e7b 7426 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
5bd52f0e 7427 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
f83d2997
KH
7428 "-[0-9]" ; -5
7429 "\\+\\+" ; ++var
7430 "--" ; --var
7431 ".->" ; a->b
7432 "->" ; a SPACE ->b
7433 "\\[-" ; a[-1]
5bd52f0e 7434 "\\\\[&$@*\\\\]" ; \&func
f83d2997 7435 "^=" ; =head
5bd52f0e
RS
7436 "\\$." ; $|
7437 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
f83d2997
KH
7438 "||"
7439 "&&"
7440 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
83261a2f 7441 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
f83d2997
KH
7442 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7443 ;;"[*/+-|&<.]+="
7444 )
7445 "\\|")
7446 "If matches at the start of match found by `my-bad-c-style-regexp',
7447insertion of a whitespace will not help.")
7448
7449(defvar found-bad)
7450
7451(defun cperl-find-bad-style ()
7452 "Find places in the buffer where insertion of a whitespace may help.
7453Prompts user for insertion of spaces.
7454Currently it is tuned to C and Perl syntax."
7455 (interactive)
7456 (let (found-bad (p (point)))
7457 (setq last-nonmenu-event 13) ; To disable popup
4ab89e7b 7458 (goto-char (point-min))
f83d2997 7459 (map-y-or-n-p "Insert space here? "
83261a2f 7460 (lambda (arg) (insert " "))
f83d2997 7461 'cperl-next-bad-style
5c8b7eaf 7462 '("location" "locations" "insert a space into")
f83d2997
KH
7463 '((?\C-r (lambda (arg)
7464 (let ((buffer-quit-function
7465 'exit-recursive-edit))
7466 (message "Exit with Esc Esc")
7467 (recursive-edit)
7468 t)) ; Consider acted upon
5c8b7eaf 7469 "edit, exit with Esc Esc")
f83d2997
KH
7470 (?e (lambda (arg)
7471 (let ((buffer-quit-function
7472 'exit-recursive-edit))
7473 (message "Exit with Esc Esc")
7474 (recursive-edit)
7475 t)) ; Consider acted upon
7476 "edit, exit with Esc Esc"))
7477 t)
7478 (if found-bad (goto-char found-bad)
7479 (goto-char p)
7480 (message "No appropriate place found"))))
7481
7482(defun cperl-next-bad-style ()
7483 (let (p (not-found t) (point (point)) found)
7484 (while (and not-found
7485 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7486 (setq p (point))
7487 (goto-char (match-beginning 0))
7488 (if (or
7489 (looking-at cperl-not-bad-style-regexp)
7490 ;; Check for a < -b and friends
7491 (and (eq (following-char) ?\-)
7492 (save-excursion
7493 (skip-chars-backward " \t\n")
07cb2aa3 7494 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
f83d2997
KH
7495 ;; Now check for syntax type
7496 (save-match-data
7497 (setq found (point))
7498 (beginning-of-defun)
7499 (let ((pps (parse-partial-sexp (point) found)))
7500 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7501 (goto-char (match-end 0))
7502 (goto-char (1- p))
7503 (setq not-found nil
7504 found-bad found)))
7505 (not not-found)))
7506
f1d851ae 7507\f
f83d2997 7508;;; Getting help
5c8b7eaf 7509(defvar cperl-have-help-regexp
f83d2997
KH
7510 ;;(concat "\\("
7511 (mapconcat
7512 'identity
83261a2f 7513 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
f83d2997
KH
7514 "[$@]\\^[a-zA-Z]" ; Special variable
7515 "[$@][^ \n\t]" ; Special variable
7516 "-[a-zA-Z]" ; File test
7517 "\\\\[a-zA-Z0]" ; Special chars
83261a2f 7518 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
f83d2997
KH
7519 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7520 "[a-zA-Z_0-9:]+" ; symbol or number
7521 "x="
83261a2f 7522 "#!")
f83d2997 7523 ;;"\\)\\|\\("
83261a2f
SM
7524 "\\|")
7525 ;;"\\)"
7526 ;;)
f83d2997
KH
7527 "Matches places in the buffer we can find help for.")
7528
7529(defvar cperl-message-on-help-error t)
7530(defvar cperl-help-from-timer nil)
7531
7532(defun cperl-word-at-point-hard ()
7533 ;; Does not save-excursion
7534 ;; Get to the something meaningful
7535 (or (eobp) (eolp) (forward-char 1))
5c8b7eaf 7536 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
f83d2997
KH
7537 (save-excursion (beginning-of-line) (point))
7538 'to-beg)
7539 ;; (cond
7540 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7541 ;; (skip-chars-backward " \n\t\r({[]});,")
7542 ;; (or (bobp) (backward-char 1))))
7543 ;; Try to backtrace
7544 (cond
7545 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7546 (skip-chars-backward "a-zA-Z0-9_:")
5c8b7eaf 7547 (cond
f83d2997
KH
7548 ((and (eq (preceding-char) ?^) ; $^I
7549 (eq (char-after (- (point) 2)) ?\$))
7550 (forward-char -2))
7551 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7552 (forward-char -1))
7553 ((and (eq (preceding-char) ?\=)
7554 (eq (current-column) 1))
7555 (forward-char -1))) ; =head1
7556 (if (and (eq (preceding-char) ?\<)
7557 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7558 (forward-char -1)))
7559 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7560 (forward-char -1))
7561 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7562 (forward-char -1))
7563 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7564 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7565 (cond
7566 ((and (eq (preceding-char) ?\$)
7567 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7568 (forward-char -1))
7569 ((and (eq (following-char) ?\>)
7570 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7571 (save-excursion
7572 (forward-sexp -1)
7573 (and (eq (preceding-char) ?\<)
7574 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7575 (search-backward "<"))))
7576 ((and (eq (following-char) ?\$)
7577 (eq (preceding-char) ?\<)
7578 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7579 (forward-char -1)))
7580 (if (looking-at cperl-have-help-regexp)
7581 (buffer-substring (match-beginning 0) (match-end 0))))
7582
7583(defun cperl-get-help ()
7584 "Get one-line docs on the symbol at the point.
7585The data for these docs is a little bit obsolete and may be in fact longer
7586than a line. Your contribution to update/shorten it is appreciated."
7587 (interactive)
7588 (save-match-data ; May be called "inside" query-replace
7589 (save-excursion
7590 (let ((word (cperl-word-at-point-hard)))
7591 (if word
7592 (if (and cperl-help-from-timer ; Bail out if not in mainland
7593 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7594 (or (memq (get-text-property (point) 'face)
7595 '(font-lock-comment-face font-lock-string-face))
7596 (memq (get-text-property (point) 'syntax-type)
7597 '(pod here-doc format))))
7598 nil
7599 (cperl-describe-perl-symbol word))
7600 (if cperl-message-on-help-error
5c8b7eaf 7601 (message "Nothing found for %s..."
f83d2997
KH
7602 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7603
7604;;; Stolen from perl-descr.el by Johan Vromans:
7605
7606(defvar cperl-doc-buffer " *perl-doc*"
7607 "Where the documentation can be found.")
7608
7609(defun cperl-describe-perl-symbol (val)
7610 "Display the documentation of symbol at point, a Perl operator."
7611 (let ((enable-recursive-minibuffers t)
7612 args-file regexp)
7613 (cond
83261a2f
SM
7614 ((string-match "^[&*][a-zA-Z_]" val)
7615 (setq val (concat (substring val 0 1) "NAME")))
7616 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7617 (setq val (concat "@" (substring val 1 (match-end 1)))))
7618 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7619 (setq val (concat "%" (substring val 1 (match-end 1)))))
7620 ((and (string= val "x") (string-match "^x=" val))
7621 (setq val "x="))
7622 ((string-match "^\\$[\C-a-\C-z]" val)
7623 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7624 ((string-match "^CORE::" val)
7625 (setq val "CORE::"))
7626 ((string-match "^SUPER::" val)
7627 (setq val "SUPER::"))
7628 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7629 (setq val "<NAME>")))
5c8b7eaf 7630 (setq regexp (concat "^"
f83d2997 7631 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
5c8b7eaf 7632 (regexp-quote val)
f83d2997
KH
7633 "\\([ \t([/]\\|$\\)"))
7634
7635 ;; get the buffer with the documentation text
7636 (cperl-switch-to-doc-buffer)
7637
7638 ;; lookup in the doc
7639 (goto-char (point-min))
7640 (let ((case-fold-search nil))
5c8b7eaf 7641 (list
f83d2997
KH
7642 (if (re-search-forward regexp (point-max) t)
7643 (save-excursion
7644 (beginning-of-line 1)
7645 (let ((lnstart (point)))
7646 (end-of-line)
7647 (message "%s" (buffer-substring lnstart (point)))))
7648 (if cperl-message-on-help-error
7649 (message "No definition for %s" val)))))))
7650
83261a2f 7651(defvar cperl-short-docs 'please-ignore-this-line
f83d2997
KH
7652 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7653 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
f739b53b 7654... Range (list context); flip/flop [no flop when flip] (scalar context).
5c8b7eaf 7655! ... Logical negation.
f83d2997
KH
7656... != ... Numeric inequality.
7657... !~ ... Search pattern, substitution, or translation (negated).
7658$! In numeric context: errno. In a string context: error string.
7659$\" The separator which joins elements of arrays interpolated in strings.
f739b53b 7660$# The output format for printed numbers. Default is %.15g or close.
f83d2997
KH
7661$$ Process number of this script. Changes in the fork()ed child process.
7662$% The current page number of the currently selected output channel.
7663
7664 The following variables are always local to the current block:
7665
7666$1 Match of the 1st set of parentheses in the last match (auto-local).
7667$2 Match of the 2nd set of parentheses in the last match (auto-local).
7668$3 Match of the 3rd set of parentheses in the last match (auto-local).
7669$4 Match of the 4th set of parentheses in the last match (auto-local).
7670$5 Match of the 5th set of parentheses in the last match (auto-local).
7671$6 Match of the 6th set of parentheses in the last match (auto-local).
7672$7 Match of the 7th set of parentheses in the last match (auto-local).
7673$8 Match of the 8th set of parentheses in the last match (auto-local).
7674$9 Match of the 9th set of parentheses in the last match (auto-local).
7675$& The string matched by the last pattern match (auto-local).
7676$' The string after what was matched by the last match (auto-local).
7677$` The string before what was matched by the last match (auto-local).
7678
7679$( The real gid of this process.
7680$) The effective gid of this process.
7681$* Deprecated: Set to 1 to do multiline matching within a string.
7682$+ The last bracket matched by the last search pattern.
7683$, The output field separator for the print operator.
7684$- The number of lines left on the page.
7685$. The current input line number of the last filehandle that was read.
7686$/ The input record separator, newline by default.
f739b53b 7687$0 Name of the file containing the current perl script (read/write).
f83d2997
KH
7688$: String may be broken after these characters to fill ^-lines in a format.
7689$; Subscript separator for multi-dim array emulation. Default \"\\034\".
7690$< The real uid of this process.
7691$= The page length of the current output channel. Default is 60 lines.
7692$> The effective uid of this process.
7693$? The status returned by the last ``, pipe close or `system'.
7694$@ The perl error message from the last eval or do @var{EXPR} command.
7695$ARGV The name of the current file used with <> .
7696$[ Deprecated: The index of the first element/char in an array/string.
7697$\\ The output record separator for the print operator.
7698$] The perl version string as displayed with perl -v.
7699$^ The name of the current top-of-page format.
7700$^A The current value of the write() accumulator for format() lines.
7701$^D The value of the perl debug (-D) flags.
7702$^E Information about the last system error other than that provided by $!.
7703$^F The highest system file descriptor, ordinarily 2.
7704$^H The current set of syntax checks enabled by `use strict'.
7705$^I The value of the in-place edit extension (perl -i option).
d7584f0f 7706$^L What formats output to perform a formfeed. Default is \\f.
5bd52f0e 7707$^M A buffer for emergency memory allocation when running out of memory.
f83d2997
KH
7708$^O The operating system name under which this copy of Perl was built.
7709$^P Internal debugging flag.
7710$^T The time the script was started. Used by -A/-M/-C file tests.
7711$^W True if warnings are requested (perl -w flag).
7712$^X The name under which perl was invoked (argv[0] in C-speech).
7713$_ The default input and pattern-searching space.
5c8b7eaf 7714$| Auto-flush after write/print on current output channel? Default 0.
f83d2997
KH
7715$~ The name of the current report format.
7716... % ... Modulo division.
7717... %= ... Modulo division assignment.
7718%ENV Contains the current environment.
7719%INC List of files that have been require-d or do-ne.
7720%SIG Used to set signal handlers for various signals.
7721... & ... Bitwise and.
7722... && ... Logical and.
7723... &&= ... Logical and assignment.
7724... &= ... Bitwise and assignment.
7725... * ... Multiplication.
7726... ** ... Exponentiation.
7727*NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7728&NAME(arg0, ...) Subroutine call. Arguments go to @_.
7729... + ... Addition. +EXPR Makes EXPR into scalar context.
7730++ Auto-increment (magical on strings). ++EXPR EXPR++
7731... += ... Addition assignment.
7732, Comma operator.
7733... - ... Subtraction.
7734-- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7735... -= ... Subtraction assignment.
7736-A Access time in days since script started.
7737-B File is a non-text (binary) file.
7738-C Inode change time in days since script started.
7739-M Age in days since script started.
7740-O File is owned by real uid.
7741-R File is readable by real uid.
7742-S File is a socket .
7743-T File is a text file.
7744-W File is writable by real uid.
7745-X File is executable by real uid.
7746-b File is a block special file.
7747-c File is a character special file.
7748-d File is a directory.
7749-e File exists .
7750-f File is a plain file.
7751-g File has setgid bit set.
7752-k File has sticky bit set.
7753-l File is a symbolic link.
7754-o File is owned by effective uid.
7755-p File is a named pipe (FIFO).
7756-r File is readable by effective uid.
7757-s File has non-zero size.
7758-t Tests if filehandle (STDIN by default) is opened to a tty.
7759-u File has setuid bit set.
7760-w File is writable by effective uid.
7761-x File is executable by effective uid.
7762-z File has zero size.
7763. Concatenate strings.
f739b53b 7764.. Range (list context); flip/flop (scalar context) operator.
f83d2997
KH
7765.= Concatenate assignment strings
7766... / ... Division. /PATTERN/ioxsmg Pattern match
7767... /= ... Division assignment.
7768/PATTERN/ioxsmg Pattern match.
f739b53b 7769... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
f83d2997
KH
7770<NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7771<pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7772<> Reads line from union of files in @ARGV (= command line) and STDIN.
7773... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7774... <= ... Numeric less than or equal to.
7775... <=> ... Numeric compare.
7776... = ... Assignment.
7777... == ... Numeric equality.
7778... =~ ... Search pattern, substitution, or translation
7779... > ... Numeric greater than.
7780... >= ... Numeric greater than or equal to.
7781... >> ... Bitwise shift right.
7782... >>= ... Bitwise shift right assignment.
7783... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7784?PATTERN? One-time pattern match.
7785@ARGV Command line arguments (not including the command name - see $0).
7786@INC List of places to look for perl scripts during do/include/use.
f739b53b 7787@_ Parameter array for subroutines; result of split() unless in list context.
d7584f0f 7788\\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
f83d2997
KH
7789\\0 Octal char, e.g. \\033.
7790\\E Case modification terminator. See \\Q, \\L, and \\U.
d7584f0f
AS
7791\\L Lowercase until \\E . See also \\l, lc.
7792\\U Upcase until \\E . See also \\u, uc.
f83d2997
KH
7793\\Q Quote metacharacters until \\E . See also quotemeta.
7794\\a Alarm character (octal 007).
7795\\b Backspace character (octal 010).
7796\\c Control character, e.g. \\c[ .
7797\\e Escape character (octal 033).
7798\\f Formfeed character (octal 014).
7799\\l Lowercase the next character. See also \\L and \\u, lcfirst.
7800\\n Newline character (octal 012 on most systems).
7801\\r Return character (octal 015 on most systems).
7802\\t Tab character (octal 011).
7803\\u Upcase the next character. See also \\U and \\l, ucfirst.
7804\\x Hex character, e.g. \\x1b.
7805... ^ ... Bitwise exclusive or.
7806__END__ Ends program source.
7807__DATA__ Ends program source.
7808__FILE__ Current (source) filename.
7809__LINE__ Current line in current source.
7810__PACKAGE__ Current package.
7811ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7812ARGVOUT Output filehandle with -i flag.
7813BEGIN { ... } Immediately executed (during compilation) piece of code.
7814END { ... } Pseudo-subroutine executed after the script finishes.
6c389151
SM
7815CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7816INIT { ... } Pseudo-subroutine executed before the script starts running.
f83d2997
KH
7817DATA Input filehandle for what follows after __END__ or __DATA__.
7818accept(NEWSOCKET,GENERICSOCKET)
7819alarm(SECONDS)
7820atan2(X,Y)
7821bind(SOCKET,NAME)
7822binmode(FILEHANDLE)
7823caller[(LEVEL)]
7824chdir(EXPR)
7825chmod(LIST)
7826chop[(LIST|VAR)]
7827chown(LIST)
7828chroot(FILENAME)
7829close(FILEHANDLE)
7830closedir(DIRHANDLE)
7831... cmp ... String compare.
7832connect(SOCKET,NAME)
7833continue of { block } continue { block }. Is executed after `next' or at end.
7834cos(EXPR)
7835crypt(PLAINTEXT,SALT)
7836dbmclose(%HASH)
7837dbmopen(%HASH,DBNAME,MODE)
7838defined(EXPR)
7839delete($HASH{KEY})
7840die(LIST)
7841do { ... }|SUBR while|until EXPR executes at least once
7842do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7843dump LABEL
7844each(%HASH)
7845endgrent
7846endhostent
7847endnetent
7848endprotoent
7849endpwent
7850endservent
7851eof[([FILEHANDLE])]
7852... eq ... String equality.
7853eval(EXPR) or eval { BLOCK }
4ab89e7b 7854exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
f83d2997
KH
7855exit(EXPR)
7856exp(EXPR)
7857fcntl(FILEHANDLE,FUNCTION,SCALAR)
7858fileno(FILEHANDLE)
7859flock(FILEHANDLE,OPERATION)
7860for (EXPR;EXPR;EXPR) { ... }
7861foreach [VAR] (@ARRAY) { ... }
7862fork
7863... ge ... String greater than or equal.
7864getc[(FILEHANDLE)]
7865getgrent
7866getgrgid(GID)
7867getgrnam(NAME)
7868gethostbyaddr(ADDR,ADDRTYPE)
7869gethostbyname(NAME)
7870gethostent
7871getlogin
7872getnetbyaddr(ADDR,ADDRTYPE)
7873getnetbyname(NAME)
7874getnetent
7875getpeername(SOCKET)
7876getpgrp(PID)
7877getppid
7878getpriority(WHICH,WHO)
7879getprotobyname(NAME)
7880getprotobynumber(NUMBER)
7881getprotoent
7882getpwent
7883getpwnam(NAME)
7884getpwuid(UID)
7885getservbyname(NAME,PROTO)
7886getservbyport(PORT,PROTO)
7887getservent
7888getsockname(SOCKET)
7889getsockopt(SOCKET,LEVEL,OPTNAME)
7890gmtime(EXPR)
7891goto LABEL
f83d2997
KH
7892... gt ... String greater than.
7893hex(EXPR)
7894if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7895index(STR,SUBSTR[,OFFSET])
7896int(EXPR)
7897ioctl(FILEHANDLE,FUNCTION,SCALAR)
7898join(EXPR,LIST)
7899keys(%HASH)
7900kill(LIST)
7901last [LABEL]
7902... le ... String less than or equal.
7903length(EXPR)
7904link(OLDFILE,NEWFILE)
7905listen(SOCKET,QUEUESIZE)
7906local(LIST)
7907localtime(EXPR)
7908log(EXPR)
7909lstat(EXPR|FILEHANDLE|VAR)
7910... lt ... String less than.
7911m/PATTERN/iogsmx
7912mkdir(FILENAME,MODE)
7913msgctl(ID,CMD,ARG)
7914msgget(KEY,FLAGS)
7915msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7916msgsnd(ID,MSG,FLAGS)
7917my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
6c389151 7918our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
f83d2997
KH
7919... ne ... String inequality.
7920next [LABEL]
7921oct(EXPR)
7922open(FILEHANDLE[,EXPR])
7923opendir(DIRHANDLE,EXPR)
7924ord(EXPR) ASCII value of the first char of the string.
7925pack(TEMPLATE,LIST)
7926package NAME Introduces package context.
7927pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7928pop(ARRAY)
7929print [FILEHANDLE] [(LIST)]
7930printf [FILEHANDLE] (FORMAT,LIST)
7931push(ARRAY,LIST)
7932q/STRING/ Synonym for 'STRING'
7933qq/STRING/ Synonym for \"STRING\"
7934qx/STRING/ Synonym for `STRING`
7935rand[(EXPR)]
7936read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7937readdir(DIRHANDLE)
7938readlink(EXPR)
7939recv(SOCKET,SCALAR,LEN,FLAGS)
7940redo [LABEL]
7941rename(OLDNAME,NEWNAME)
7942require [FILENAME | PERL_VERSION]
7943reset[(EXPR)]
7944return(LIST)
7945reverse(LIST)
7946rewinddir(DIRHANDLE)
7947rindex(STR,SUBSTR[,OFFSET])
7948rmdir(FILENAME)
7949s/PATTERN/REPLACEMENT/gieoxsm
7950scalar(EXPR)
7951seek(FILEHANDLE,POSITION,WHENCE)
7952seekdir(DIRHANDLE,POS)
7953select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7954semctl(ID,SEMNUM,CMD,ARG)
7955semget(KEY,NSEMS,SIZE,FLAGS)
7956semop(KEY,...)
7957send(SOCKET,MSG,FLAGS[,TO])
7958setgrent
7959sethostent(STAYOPEN)
7960setnetent(STAYOPEN)
7961setpgrp(PID,PGRP)
7962setpriority(WHICH,WHO,PRIORITY)
7963setprotoent(STAYOPEN)
7964setpwent
7965setservent(STAYOPEN)
7966setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7967shift[(ARRAY)]
7968shmctl(ID,CMD,ARG)
7969shmget(KEY,SIZE,FLAGS)
7970shmread(ID,VAR,POS,SIZE)
7971shmwrite(ID,STRING,POS,SIZE)
7972shutdown(SOCKET,HOW)
7973sin(EXPR)
7974sleep[(EXPR)]
7975socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7976socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7977sort [SUBROUTINE] (LIST)
7978splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7979split[(/PATTERN/[,EXPR[,LIMIT]])]
7980sprintf(FORMAT,LIST)
7981sqrt(EXPR)
7982srand(EXPR)
7983stat(EXPR|FILEHANDLE|VAR)
7984study[(SCALAR)]
7985sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7986substr(EXPR,OFFSET[,LEN])
7987symlink(OLDFILE,NEWFILE)
7988syscall(LIST)
7989sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
4ab89e7b 7990system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
f83d2997
KH
7991syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7992tell[(FILEHANDLE)]
7993telldir(DIRHANDLE)
7994time
7995times
7996tr/SEARCHLIST/REPLACEMENTLIST/cds
7997truncate(FILE|EXPR,LENGTH)
7998umask[(EXPR)]
7999undef[(EXPR)]
8000unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
8001unlink(LIST)
8002unpack(TEMPLATE,EXPR)
8003unshift(ARRAY,LIST)
8004until (EXPR) { ... } EXPR until EXPR
8005utime(LIST)
8006values(%HASH)
8007vec(EXPR,OFFSET,BITS)
8008wait
8009waitpid(PID,FLAGS)
8010wantarray Returns true if the sub/eval is called in list context.
8011warn(LIST)
8012while (EXPR) { ... } EXPR while EXPR
8013write[(EXPR|FILEHANDLE)]
8014... x ... Repeat string or array.
8015x= ... Repetition assignment.
8016y/SEARCHLIST/REPLACEMENTLIST/
8017... | ... Bitwise or.
8018... || ... Logical or.
8019~ ... Unary bitwise complement.
db133cb6 8020#! OS interpreter indicator. If contains `perl', used for options, and -x.
f83d2997
KH
8021AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
8022CORE:: Prefix to access builtin function if imported sub obscures it.
8023SUPER:: Prefix to lookup for a method in @ISA classes.
8024DESTROY Shorthand for `sub DESTROY {...}'.
8025... EQ ... Obsolete synonym of `eq'.
8026... GE ... Obsolete synonym of `ge'.
8027... GT ... Obsolete synonym of `gt'.
8028... LE ... Obsolete synonym of `le'.
8029... LT ... Obsolete synonym of `lt'.
8030... NE ... Obsolete synonym of `ne'.
8031abs [ EXPR ] absolute value
8032... and ... Low-precedence synonym for &&.
8033bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
8034chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
8035chr Converts a number to char with the same ordinal.
8036else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
8037elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
83261a2f 8038exists $HASH{KEY} True if the key exists.
f83d2997
KH
8039format [NAME] = Start of output format. Ended by a single dot (.) on a line.
8040formline PICTURE, LIST Backdoor into \"format\" processing.
8041glob EXPR Synonym of <EXPR>.
8042lc [ EXPR ] Returns lowercased EXPR.
8043lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
db133cb6 8044grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
f83d2997
KH
8045map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
8046no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
8047not ... Low-precedence synonym for ! - negation.
8048... or ... Low-precedence synonym for ||.
8049pos STRING Set/Get end-position of the last match over this string, see \\G.
8050quotemeta [ EXPR ] Quote regexp metacharacters.
8051qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
8052readline FH Synonym of <FH>.
8053readpipe CMD Synonym of `CMD`.
8054ref [ EXPR ] Type of EXPR when dereferenced.
8055sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
8056tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
8057tied Returns internal object for a tied data.
8058uc [ EXPR ] Returns upcased EXPR.
8059ucfirst [ EXPR ] Returns EXPR with upcased first letter.
8060untie VAR Unlink an object from a simple Perl variable.
8061use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
8062... xor ... Low-precedence synonym for exclusive or.
d7584f0f 8063prototype \\&SUB Returns prototype of the function given a reference.
f83d2997
KH
8064=head1 Top-level heading.
8065=head2 Second-level heading.
8066=head3 Third-level heading (is there such?).
8067=over [ NUMBER ] Start list.
8068=item [ TITLE ] Start new item in the list.
8069=back End list.
8070=cut Switch from POD to Perl.
8071=pod Switch from Perl to POD.
8072")
8073
21df56d5 8074(defun cperl-switch-to-doc-buffer (&optional interactive)
f83d2997 8075 "Go to the perl documentation buffer and insert the documentation."
21df56d5 8076 (interactive "p")
f83d2997 8077 (let ((buf (get-buffer-create cperl-doc-buffer)))
21df56d5 8078 (if interactive
f83d2997
KH
8079 (switch-to-buffer-other-window buf)
8080 (set-buffer buf))
8081 (if (= (buffer-size) 0)
8082 (progn
8083 (insert (documentation-property 'cperl-short-docs
8084 'variable-documentation))
8085 (setq buffer-read-only t)))))
8086
6c389151 8087(defun cperl-beautify-regexp-piece (b e embed level)
f83d2997
KH
8088 ;; b is before the starting delimiter, e before the ending
8089 ;; e should be a marker, may be changed, but remains "correct".
6c389151 8090 ;; EMBED is nil iff we process the whole REx.
4ab89e7b 8091 ;; The REx is guaranteed to have //x
6c389151
SM
8092 ;; LEVEL shows how many levels deep to go
8093 ;; position at enter and at leave is not defined
8094 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
f83d2997
KH
8095 (if (not embed)
8096 (goto-char (1+ b))
8097 (goto-char b)
6c389151 8098 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
f83d2997
KH
8099 (forward-char 2)
8100 (delete-char 1)
8101 (forward-char 1))
8102 ((looking-at "(\\?[^a-zA-Z]")
8103 (forward-char 3))
8104 ((looking-at "(\\?") ; (?i)
8105 (forward-char 2))
8106 (t
8107 (forward-char 1))))
8108 (setq c (if embed (current-indentation) (1- (current-column)))
8109 c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
8110 (or (looking-at "[ \t]*[\n#]")
8111 (progn
8112 (insert "\n")))
8113 (goto-char e)
8114 (beginning-of-line)
8115 (if (re-search-forward "[^ \t]" e t)
83261a2f 8116 (progn ; Something before the ending delimiter
f83d2997 8117 (goto-char e)
6c389151 8118 (delete-horizontal-space)
f83d2997 8119 (insert "\n")
4ab89e7b 8120 (cperl-make-indent c)
f83d2997
KH
8121 (set-marker e (point))))
8122 (goto-char b)
8123 (end-of-line 2)
8124 (while (< (point) (marker-position e))
8125 (beginning-of-line)
8126 (setq s (point)
8127 inline t)
8128 (skip-chars-forward " \t")
8129 (delete-region s (point))
4ab89e7b 8130 (cperl-make-indent c1)
f83d2997
KH
8131 (while (and
8132 inline
5c8b7eaf 8133 (looking-at
f83d2997
KH
8134 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8135 "\\|" ; Embedded variable
8136 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8137 "\\|" ; $ ^
8138 "[$^]"
8139 "\\|" ; simple-code simple-code*?
8140 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8141 "\\|" ; Class
8142 "\\(\\[\\)" ; 6
8143 "\\|" ; Grouping
8144 "\\((\\(\\?\\)?\\)" ; 7 8
8145 "\\|" ; |
83261a2f 8146 "\\(|\\)"))) ; 9
f83d2997
KH
8147 (goto-char (match-end 0))
8148 (setq spaces t)
8149 (cond ((match-beginning 1) ; Alphanum word + junk
8150 (forward-char -1))
8151 ((or (match-beginning 3) ; $ab[12]
8152 (and (match-beginning 5) ; X* X+ X{2,3}
8153 (eq (preceding-char) ?\{)))
8154 (forward-char -1)
8155 (forward-sexp 1))
4ab89e7b
SM
8156 ((and ; [], already syntaxified
8157 (match-beginning 6)
8158 cperl-regexp-scan
8159 cperl-use-syntax-table-text-property)
8160 (forward-char -1)
8161 (forward-sexp 1)
8162 (or (eq (preceding-char) ?\])
8163 (error "[]-group not terminated"))
8164 (re-search-forward
8165 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
f83d2997
KH
8166 ((match-beginning 6) ; []
8167 (setq tmp (point))
8168 (if (looking-at "\\^?\\]")
8169 (goto-char (match-end 0)))
6c389151
SM
8170 ;; XXXX POSIX classes?!
8171 (while (and (not pos)
8172 (re-search-forward "\\[:\\|\\]" e t))
8173 (if (eq (preceding-char) ?:)
8174 (or (re-search-forward ":\\]" e t)
8175 (error "[:POSIX:]-group in []-group not terminated"))
8176 (setq pos t)))
8177 (or (eq (preceding-char) ?\])
8178 (error "[]-group not terminated"))
4ab89e7b
SM
8179 (re-search-forward
8180 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
f83d2997
KH
8181 ((match-beginning 7) ; ()
8182 (goto-char (match-beginning 0))
6c389151
SM
8183 (setq pos (current-column))
8184 (or (eq pos c1)
f83d2997 8185 (progn
6c389151 8186 (delete-horizontal-space)
f83d2997 8187 (insert "\n")
4ab89e7b 8188 (cperl-make-indent c1)))
f83d2997
KH
8189 (setq tmp (point))
8190 (forward-sexp 1)
8191 ;; (or (forward-sexp 1)
8192 ;; (progn
8193 ;; (goto-char tmp)
8194 ;; (error "()-group not terminated")))
8195 (set-marker m (1- (point)))
8196 (set-marker m1 (point))
6c389151
SM
8197 (if (= level 1)
8198 (if (progn ; indent rigidly if multiline
a1506d29 8199 ;; In fact does not make a lot of sense, since
6c389151
SM
8200 ;; the starting position can be already lost due
8201 ;; to insertion of "\n" and " "
8202 (goto-char tmp)
8203 (search-forward "\n" m1 t))
8204 (indent-rigidly (point) m1 (- c1 pos)))
8205 (setq level (1- level))
8206 (cond
8207 ((not (match-beginning 8))
8208 (cperl-beautify-regexp-piece tmp m t level))
8209 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8210 t)
8211 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8212 (goto-char (+ 2 tmp))
8213 (forward-sexp 1)
8214 (cperl-beautify-regexp-piece (point) m t level))
8215 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8216 (goto-char (+ 3 tmp))
8217 (cperl-beautify-regexp-piece (point) m t level))
8218 (t
8219 (cperl-beautify-regexp-piece tmp m t level))))
f83d2997
KH
8220 (goto-char m1)
8221 (cond ((looking-at "[*+?]\\??")
8222 (goto-char (match-end 0)))
8223 ((eq (following-char) ?\{)
8224 (forward-sexp 1)
8225 (if (eq (following-char) ?\?)
8226 (forward-char))))
8227 (skip-chars-forward " \t")
8228 (setq spaces nil)
8229 (if (looking-at "[#\n]")
8230 (progn
8231 (or (eolp) (indent-for-comment))
8232 (beginning-of-line 2))
6c389151 8233 (delete-horizontal-space)
f83d2997
KH
8234 (insert "\n"))
8235 (end-of-line)
8236 (setq inline nil))
8237 ((match-beginning 9) ; |
8238 (forward-char -1)
8239 (setq tmp (point))
8240 (beginning-of-line)
8241 (if (re-search-forward "[^ \t]" tmp t)
8242 (progn
8243 (goto-char tmp)
6c389151 8244 (delete-horizontal-space)
f83d2997
KH
8245 (insert "\n"))
8246 ;; first at line
8247 (delete-region (point) tmp))
4ab89e7b 8248 (cperl-make-indent c)
f83d2997
KH
8249 (forward-char 1)
8250 (skip-chars-forward " \t")
8251 (setq spaces nil)
8252 (if (looking-at "[#\n]")
8253 (beginning-of-line 2)
6c389151 8254 (delete-horizontal-space)
f83d2997
KH
8255 (insert "\n"))
8256 (end-of-line)
8257 (setq inline nil)))
8258 (or (looking-at "[ \t\n]")
8259 (not spaces)
8260 (insert " "))
8261 (skip-chars-forward " \t"))
83261a2f
SM
8262 (or (looking-at "[#\n]")
8263 (error "Unknown code `%s' in a regexp"
8264 (buffer-substring (point) (1+ (point)))))
8265 (and inline (end-of-line 2)))
f83d2997
KH
8266 ;; Special-case the last line of group
8267 (if (and (>= (point) (marker-position e))
8268 (/= (current-indentation) c))
8269 (progn
83261a2f 8270 (beginning-of-line)
4ab89e7b 8271 (cperl-make-indent c)))))
f83d2997
KH
8272
8273(defun cperl-make-regexp-x ()
db133cb6 8274 ;; Returns position of the start
6c389151 8275 ;; XXX this is called too often! Need to cache the result!
f83d2997
KH
8276 (save-excursion
8277 (or cperl-use-syntax-table-text-property
5bd52f0e 8278 (error "I need to have a regexp marked!"))
f83d2997 8279 ;; Find the start
db133cb6
RS
8280 (if (looking-at "\\s|")
8281 nil ; good already
5bd52f0e 8282 (if (looking-at "\\([smy]\\|qr\\)\\s|")
db133cb6 8283 (forward-char 1)
83261a2f 8284 (re-search-backward "\\s|"))) ; Assume it is scanned already.
f83d2997
KH
8285 ;;(forward-char 1)
8286 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8287 (sub-p (eq (preceding-char) ?s)) s)
8288 (forward-sexp 1)
8289 (set-marker e (1- (point)))
8290 (setq delim (preceding-char))
8291 (if (and sub-p (eq delim (char-after (- (point) 2))))
8292 (error "Possible s/blah// - do not know how to deal with"))
8293 (if sub-p (forward-sexp 1))
5c8b7eaf 8294 (if (looking-at "\\sw*x")
f83d2997
KH
8295 (setq have-x t)
8296 (insert "x"))
8297 ;; Protect fragile " ", "#"
8298 (if have-x nil
8299 (goto-char (1+ b))
8300 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8301 (forward-char -1)
8302 (insert "\\")
8303 (forward-char 1)))
8304 b)))
8305
6c389151 8306(defun cperl-beautify-regexp (&optional deep)
f94a632a 8307 "Do it. (Experimental, may change semantics, recheck the result.)
f83d2997 8308We suppose that the regexp is scanned already."
6c389151 8309 (interactive "P")
0c602a0f 8310 (setq deep (if deep (prefix-numeric-value deep) -1))
6c389151
SM
8311 (save-excursion
8312 (goto-char (cperl-make-regexp-x))
8313 (let ((b (point)) (e (make-marker)))
8314 (forward-sexp 1)
8315 (set-marker e (1- (point)))
8316 (cperl-beautify-regexp-piece b e nil deep))))
f83d2997 8317
db133cb6
RS
8318(defun cperl-regext-to-level-start ()
8319 "Goto start of an enclosing group in regexp.
f83d2997
KH
8320We suppose that the regexp is scanned already."
8321 (interactive)
db133cb6 8322 (let ((limit (cperl-make-regexp-x)) done)
f83d2997
KH
8323 (while (not done)
8324 (or (eq (following-char) ?\()
db133cb6 8325 (search-backward "(" (1+ limit) t)
f83d2997
KH
8326 (error "Cannot find `(' which starts a group"))
8327 (setq done
8328 (save-excursion
8329 (skip-chars-backward "\\")
8330 (looking-at "\\(\\\\\\\\\\)*(")))
db133cb6
RS
8331 (or done (forward-char -1)))))
8332
8333(defun cperl-contract-level ()
5bd52f0e 8334 "Find an enclosing group in regexp and contract it.
db133cb6
RS
8335\(Experimental, may change semantics, recheck the result.)
8336We suppose that the regexp is scanned already."
8337 (interactive)
6c389151 8338 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
83261a2f 8339 (cperl-regext-to-level-start)
4ab89e7b 8340 (let ((b (point)) (e (make-marker)) c)
83261a2f
SM
8341 (forward-sexp 1)
8342 (set-marker e (1- (point)))
8343 (goto-char b)
8344 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8345 (cond
8346 ((match-beginning 1) ; #-comment
8347 (or c (setq c (current-indentation)))
8348 (beginning-of-line 2) ; Skip
4ab89e7b 8349 (cperl-make-indent c))
83261a2f
SM
8350 (t
8351 (delete-char -1)
8352 (just-one-space))))))
db133cb6
RS
8353
8354(defun cperl-contract-levels ()
5bd52f0e 8355 "Find an enclosing group in regexp and contract all the kids.
db133cb6
RS
8356\(Experimental, may change semantics, recheck the result.)
8357We suppose that the regexp is scanned already."
8358 (interactive)
6c389151
SM
8359 (save-excursion
8360 (condition-case nil
8361 (cperl-regext-to-level-start)
8362 (error ; We are outside outermost group
5efe6a56
SM
8363 (goto-char (cperl-make-regexp-x))))
8364 (let ((b (point)) (e (make-marker)) s c)
8365 (forward-sexp 1)
8366 (set-marker e (1- (point)))
8367 (goto-char (1+ b))
8368 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
a1506d29 8369 (cond
6c389151
SM
8370 ((match-beginning 1) ; Skip
8371 nil)
8372 (t ; Group
8373 (cperl-contract-level)))))))
f83d2997 8374
6c389151 8375(defun cperl-beautify-level (&optional deep)
f83d2997
KH
8376 "Find an enclosing group in regexp and beautify it.
8377\(Experimental, may change semantics, recheck the result.)
8378We suppose that the regexp is scanned already."
6c389151 8379 (interactive "P")
0c602a0f 8380 (setq deep (if deep (prefix-numeric-value deep) -1))
6c389151
SM
8381 (save-excursion
8382 (cperl-regext-to-level-start)
8383 (let ((b (point)) (e (make-marker)))
8384 (forward-sexp 1)
8385 (set-marker e (1- (point)))
8386 (cperl-beautify-regexp-piece b e nil deep))))
db133cb6 8387
4ab89e7b
SM
8388(defun cperl-invert-if-unless-modifiers ()
8389 "Change `B if A;' into `if (A) {B}' etc if possible.
8390\(Unfinished.)"
8391 (interactive) ;
8392 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8393 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8394 (and (= (char-syntax (preceding-char)) ?w)
8395 (forward-sexp -1))
8396 (setq pre-if (point))
8397 (cperl-backward-to-start-of-expr)
8398 (setq pre-B (point))
8399 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8400 (cperl-forward-to-end-of-expr)
8401 (setq post-A (point))
8402 (goto-char pre-if)
8403 (or (looking-at w-rex)
8404 ;; Find the position
8405 (progn (goto-char post-A)
8406 (while (and
8407 (not (looking-at w-rex))
8408 (> (point) pre-B))
8409 (forward-sexp -1))
8410 (setq pre-if (point))))
8411 (or (looking-at w-rex)
8412 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8413 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8414 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8415 ;; First, simple part: find code boundaries
8416 (forward-sexp 1)
8417 (setq post-if (point))
8418 (forward-sexp -2)
8419 (forward-sexp 1)
8420 (setq post-B (point))
8421 (cperl-backward-to-start-of-expr)
8422 (setq pre-B (point))
8423 (setq B (buffer-substring pre-B post-B))
8424 (goto-char pre-if)
8425 (forward-sexp 2)
8426 (forward-sexp -1)
8427 ;; May be after $, @, $# etc of a variable
8428 (skip-chars-backward "$@%#")
8429 (setq pre-A (point))
8430 (cperl-forward-to-end-of-expr)
8431 (setq post-A (point))
8432 (setq A (buffer-substring pre-A post-A))
8433 ;; Now modify (from end, to not break the stuff)
8434 (skip-chars-forward " \t;")
8435 (delete-region pre-A (point)) ; we move to pre-A
8436 (insert "\n" B ";\n}")
8437 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8438 (delete-region pre-if post-if)
8439 (delete-region pre-B post-B)
8440 (goto-char pre-B)
8441 (insert if-string " (" A ") {")
8442 (setq post-B (point))
8443 (if (looking-at "[ \t]+$")
8444 (delete-horizontal-space)
8445 (if (looking-at "[ \t]*#")
8446 (cperl-indent-for-comment)
8447 (just-one-space)))
8448 (forward-line 1)
8449 (if (looking-at "[ \t]*$")
8450 (progn ; delete line
8451 (delete-horizontal-space)
8452 (delete-region (point) (1+ (point)))))
8453 (cperl-indent-line)
8454 (goto-char (1- post-B))
8455 (forward-sexp 1)
8456 (cperl-indent-line)
8457 (goto-char pre-B)))
8458
db133cb6 8459(defun cperl-invert-if-unless ()
4ab89e7b
SM
8460 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8461If the cursor is not on the leading keyword of the BLOCK flavor of
8462construct, will assume it is the STATEMENT flavor, so will try to find
8463the appropriate statement modifier."
db133cb6 8464 (interactive)
4ab89e7b
SM
8465 (and (= (char-syntax (preceding-char)) ?w)
8466 (forward-sexp -1))
6c389151 8467 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
4ab89e7b
SM
8468 (let ((pre-if (point))
8469 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8470 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
db133cb6 8471 (forward-sexp 2)
4ab89e7b 8472 (setq post-A (point))
db133cb6 8473 (forward-sexp -1)
4ab89e7b
SM
8474 (setq pre-A (point))
8475 (setq is-block (and (eq (following-char) ?\( )
8476 (save-excursion
8477 (condition-case nil
8478 (progn
8479 (forward-sexp 2)
8480 (forward-sexp -1)
8481 (eq (following-char) ?\{ ))
8482 (error nil)))))
8483 (if is-block
db133cb6 8484 (progn
4ab89e7b 8485 (goto-char post-A)
db133cb6 8486 (forward-sexp 1)
4ab89e7b 8487 (setq post-B (point))
db133cb6 8488 (forward-sexp -1)
4ab89e7b 8489 (setq pre-B (point))
db133cb6
RS
8490 (if (and (eq (following-char) ?\{ )
8491 (progn
4ab89e7b 8492 (cperl-backward-to-noncomment post-A)
db133cb6
RS
8493 (eq (preceding-char) ?\) )))
8494 (if (condition-case nil
8495 (progn
4ab89e7b 8496 (goto-char post-B)
db133cb6
RS
8497 (forward-sexp 1)
8498 (forward-sexp -1)
8499 (looking-at "\\<els\\(e\\|if\\)\\>"))
8500 (error nil))
8501 (error
4ab89e7b
SM
8502 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8503 (goto-char (1- post-B))
8504 (cperl-backward-to-noncomment pre-B)
db133cb6
RS
8505 (if (eq (preceding-char) ?\;)
8506 (forward-char -1))
4ab89e7b
SM
8507 (setq end-B-code (point))
8508 (goto-char pre-B)
8509 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
db133cb6 8510 (setq p (match-beginning 0)
4ab89e7b
SM
8511 A (buffer-substring p (match-end 0))
8512 state (parse-partial-sexp pre-B p))
5c8b7eaf 8513 (or (nth 3 state)
db133cb6
RS
8514 (nth 4 state)
8515 (nth 5 state)
4ab89e7b 8516 (error "`%s' inside `%s' BLOCK" A if-string))
db133cb6
RS
8517 (goto-char (match-end 0)))
8518 ;; Finally got it
4ab89e7b 8519 (goto-char (1+ pre-B))
db133cb6 8520 (skip-chars-forward " \t\n")
4ab89e7b
SM
8521 (setq B (buffer-substring (point) end-B-code))
8522 (goto-char end-B-code)
db133cb6
RS
8523 (or (looking-at ";?[ \t\n]*}")
8524 (progn
8525 (skip-chars-forward "; \t\n")
4ab89e7b
SM
8526 (setq B-comment
8527 (buffer-substring (point) (1- post-B)))))
8528 (and (equal B "")
8529 (setq B "1"))
8530 (goto-char (1- post-A))
8531 (cperl-backward-to-noncomment pre-A)
db133cb6 8532 (or (looking-at "[ \t\n]*)")
4ab89e7b 8533 (goto-char (1- post-A)))
db133cb6 8534 (setq p (point))
4ab89e7b 8535 (goto-char (1+ pre-A))
db133cb6 8536 (skip-chars-forward " \t\n")
4ab89e7b
SM
8537 (setq A (buffer-substring (point) p))
8538 (delete-region pre-B post-B)
8539 (delete-region pre-A post-A)
8540 (goto-char pre-if)
8541 (insert B " ")
8542 (and B-comment (insert B-comment " "))
db133cb6
RS
8543 (just-one-space)
8544 (forward-word 1)
4ab89e7b
SM
8545 (setq pre-A (point))
8546 (insert " " A ";")
6c389151 8547 (delete-horizontal-space)
4ab89e7b
SM
8548 (setq post-B (point))
8549 (if (looking-at "#")
8550 (indent-for-comment))
8551 (goto-char post-B)
db133cb6
RS
8552 (forward-char -1)
8553 (delete-horizontal-space)
4ab89e7b 8554 (goto-char pre-A)
db133cb6 8555 (just-one-space)
4ab89e7b
SM
8556 (goto-char pre-if)
8557 (setq pre-A (set-marker (make-marker) pre-A))
8558 (while (<= (point) (marker-position pre-A))
8559 (cperl-indent-line)
8560 (forward-line 1))
8561 (goto-char (marker-position pre-A))
8562 (if B-comment
8563 (progn
8564 (forward-line -1)
8565 (indent-for-comment)
8566 (goto-char (marker-position pre-A)))))
8567 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8568 ;; (error "`%s' not with an (EXPR)" if-string)
8569 (forward-sexp -1)
8570 (cperl-invert-if-unless-modifiers)))
8571 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8572 (cperl-invert-if-unless-modifiers)))
db133cb6 8573
5bd52f0e 8574;;; By Anthony Foiani <afoiani@uswest.com>
b7ec9e59
RS
8575;;; Getting help on modules in C-h f ?
8576;;; This is a modified version of `man'.
8577;;; Need to teach it how to lookup functions
4ab89e7b 8578;;;###autoload
b7ec9e59
RS
8579(defun cperl-perldoc (word)
8580 "Run `perldoc' on WORD."
8581 (interactive
8582 (list (let* ((default-entry (cperl-word-at-point))
8583 (input (read-string
8584 (format "perldoc entry%s: "
8585 (if (string= default-entry "")
8586 ""
8587 (format " (default %s)" default-entry))))))
8588 (if (string= input "")
8589 (if (string= default-entry "")
8590 (error "No perldoc args given")
8591 default-entry)
8592 input))))
a8e1e57f 8593 (require 'man)
f739b53b
SM
8594 (let* ((case-fold-search nil)
8595 (is-func (and
b7ec9e59
RS
8596 (string-match "^[a-z]+$" word)
8597 (string-match (concat "^" word "\\>")
8598 (documentation-property
8599 'cperl-short-docs
8600 'variable-documentation))))
8601 (manual-program (if is-func "perldoc -f" "perldoc")))
f739b53b
SM
8602 (cond
8603 (cperl-xemacs-p
8604 (let ((Manual-program "perldoc")
8605 (Manual-switches (if is-func (list "-f"))))
8606 (manual-entry word)))
8607 (t
8608 (Man-getpage-in-background word)))))
b7ec9e59 8609
4ab89e7b 8610;;;###autoload
b7ec9e59
RS
8611(defun cperl-perldoc-at-point ()
8612 "Run a `perldoc' on the word around point."
8613 (interactive)
8614 (cperl-perldoc (cperl-word-at-point)))
8615
8616(defcustom pod2man-program "pod2man"
8617 "*File name for `pod2man'."
8618 :type 'file
8619 :group 'cperl)
8620
5bd52f0e 8621;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
b7ec9e59
RS
8622(defun cperl-pod-to-manpage ()
8623 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8624 (interactive)
8625 (require 'man)
8626 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8627 (bufname (concat "Man " buffer-file-name))
8628 (buffer (generate-new-buffer bufname)))
8629 (save-excursion
8630 (set-buffer buffer)
8631 (let ((process-environment (copy-sequence process-environment)))
8632 ;; Prevent any attempt to use display terminal fanciness.
8633 (setenv "TERM" "dumb")
8634 (set-process-sentinel
8635 (start-process pod2man-program buffer "sh" "-c"
8636 (format (cperl-pod2man-build-command) pod2man-args))
8637 'Man-bgproc-sentinel)))))
8638
f739b53b
SM
8639;;; Updated version by him too
8640(defun cperl-build-manpage ()
8641 "Create a virtual manpage in Emacs from the POD in the file."
8642 (interactive)
8643 (require 'man)
8644 (cond
8645 (cperl-xemacs-p
8646 (let ((Manual-program "perldoc"))
8647 (manual-entry buffer-file-name)))
8648 (t
8649 (let* ((manual-program "perldoc"))
8650 (Man-getpage-in-background buffer-file-name)))))
8651
b7ec9e59
RS
8652(defun cperl-pod2man-build-command ()
8653 "Builds the entire background manpage and cleaning command."
8654 (let ((command (concat pod2man-program " %s 2>/dev/null"))
4ab89e7b 8655 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
b7ec9e59
RS
8656 (while (and flist (car flist))
8657 (let ((pcom (car (car flist)))
8658 (pargs (cdr (car flist))))
8659 (setq command
8660 (concat command " | " pcom " "
8661 (mapconcat '(lambda (phrase)
8662 (if (not (stringp phrase))
8663 (error "Malformed Man-filter-list"))
8664 phrase)
8665 pargs " ")))
8666 (setq flist (cdr flist))))
8667 command))
db133cb6 8668
4ab89e7b
SM
8669
8670(defun cperl-next-interpolated-REx-1 ()
8671 "Move point to next REx which has interpolated parts without //o.
8672Skips RExes consisting of one interpolated variable.
8673
8674Note that skipped RExen are not performance hits."
8675 (interactive "")
8676 (cperl-next-interpolated-REx 1))
8677
8678(defun cperl-next-interpolated-REx-0 ()
8679 "Move point to next REx which has interpolated parts without //o."
8680 (interactive "")
8681 (cperl-next-interpolated-REx 0))
8682
8683(defun cperl-next-interpolated-REx (&optional skip beg limit)
8684 "Move point to next REx which has interpolated parts.
8685SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8686point and the limit of search (default to point and end of buffer).
8687
8688SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8689semantic may be used as a numeric argument.
8690
8691Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8692a result of qr//, this is not a performance hit), t for the rest."
8693 (interactive "P")
8694 (if (numberp skip) (setq skip (list 0 skip)))
8695 (or beg (setq beg (point)))
8696 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8697 (let (pp)
8698 (and (eq (get-text-property beg 'syntax-type) 'string)
8699 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8700 (cperl-map-pods-heres
8701 (function (lambda (s e p)
8702 (if (memq (get-text-property s 'REx-interpolated) skip)
8703 t
8704 (setq pp s)
8705 nil))) ; nil stops
8706 'REx-interpolated beg limit)
8707 (if pp (goto-char pp)
8708 (message "No more interpolated REx"))))
8709
8710;;; Initial version contributed by Trey Belew
8711(defun cperl-here-doc-spell (&optional beg end)
8712 "Spell-check HERE-documents in the Perl buffer.
8713If a region is highlighted, restricts to the region."
8714 (interactive "")
8715 (cperl-pod-spell t beg end))
8716
8717(defun cperl-pod-spell (&optional do-heres beg end)
8718 "Spell-check POD documentation.
8719If invoked with prefix argument, will do HERE-DOCs instead.
8720If a region is highlighted, restricts to the region."
8721 (interactive "P")
8722 (save-excursion
8723 (let (beg end)
8724 (if (cperl-mark-active)
8725 (setq beg (min (mark) (point))
8726 end (max (mark) (point)))
8727 (setq beg (point-min)
8728 end (point-max)))
8729 (cperl-map-pods-heres (function
8730 (lambda (s e p)
8731 (if do-heres
8732 (setq e (save-excursion
8733 (goto-char e)
8734 (forward-line -1)
8735 (point))))
8736 (ispell-region s e)
8737 t))
8738 (if do-heres 'here-doc-group 'in-pod)
8739 beg end))))
8740
8741(defun cperl-map-pods-heres (func &optional prop s end)
8742 "Executes a function over regions of pods or here-documents.
8743PROP is the text-property to search for; default to `in-pod'. Stop when
8744function returns nil."
8745 (let (pos posend has-prop (cont t))
8746 (or prop (setq prop 'in-pod))
8747 (or s (setq s (point-min)))
8748 (or end (setq end (point-max)))
8749 (cperl-update-syntaxification end end)
8750 (save-excursion
8751 (goto-char (setq pos s))
8752 (while (and cont (< pos end))
8753 (setq has-prop (get-text-property pos prop))
8754 (setq posend (next-single-property-change pos prop nil end))
8755 (and has-prop
8756 (setq cont (funcall func pos posend prop)))
8757 (setq pos posend)))))
8758
8759;;; Based on code by Masatake YAMATO:
8760(defun cperl-get-here-doc-region (&optional pos pod)
8761 "Return HERE document region around the point.
8762Return nil if the point is not in a HERE document region. If POD is non-nil,
8763will return a POD section if point is in a POD section."
8764 (or pos (setq pos (point)))
8765 (cperl-update-syntaxification pos pos)
8766 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8767 (and pod
8768 (eq 'pod (get-text-property pos 'syntax-type))))
8769 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8770 (e (next-single-property-change pos 'syntax-type)))
8771 (cons b (or e (point-max))))))
8772
8773(defun cperl-narrow-to-here-doc (&optional pos)
8774 "Narrows editing region to the HERE-DOC at POS.
8775POS defaults to the point."
8776 (interactive "d")
8777 (or pos (setq pos (point)))
8778 (let ((p (cperl-get-here-doc-region pos)))
8779 (or p (error "Not inside a HERE document"))
8780 (narrow-to-region (car p) (cdr p))
8781 (message
8782 "When you are finished with narrow editing, type C-x n w")))
8783
8784(defun cperl-select-this-pod-or-here-doc (&optional pos)
8785 "Select the HERE-DOC (or POD section) at POS.
8786POS defaults to the point."
8787 (interactive "d")
8788 (let ((p (cperl-get-here-doc-region pos t)))
8789 (if p
8790 (progn
8791 (goto-char (car p))
8792 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8793 (message "I do not think POS is in POD or a HERE-doc..."))))
8794
8795(defun cperl-facemenu-add-face-function (face end)
8796 "A callback to process user-initiated font-change requests.
8797Translates `bold', `italic', and `bold-italic' requests to insertion of
8798corresponding POD directives, and `underline' to C<> POD directive.
8799
8800Such requests are usually bound to M-o LETTER."
8801 (or (get-text-property (point) 'in-pod)
8802 (error "Faces can only be set within POD"))
8803 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8804 (cdr (or (assq face '((bold . "B<")
8805 (italic . "I<")
8806 (bold-italic . "B<I<")
8807 (underline . "C<")))
8808 (error "Face %s not configured for cperl-mode"
8809 face))))
8810\f
8811(defun cperl-time-fontification (&optional l step lim)
8812 "Times how long it takes to do incremental fontification in a region.
8813L is the line to start at, STEP is the number of lines to skip when
8814doing next incremental fontification, LIM is the maximal number of
8815incremental fontification to perform. Messages are accumulated in
8816*Messages* buffer.
8817
8818May be used for pinpointing which construct slows down buffer fontification:
8819start with default arguments, then refine the slowdown regions."
8820 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8821 (or l (setq l 1))
8822 (or step (setq step 500))
8823 (or lim (setq lim 40))
8824 (let* ((timems (function (lambda ()
8825 (let ((tt (current-time)))
8826 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8827 (tt (funcall timems)) (c 0) delta tot)
8828 (goto-line l)
8829 (cperl-mode)
8830 (setq tot (- (- tt (setq tt (funcall timems)))))
8831 (message "cperl-mode at %s: %s" l tot)
8832 (while (and (< c lim) (not (eobp)))
8833 (forward-line step)
8834 (setq l (+ l step))
8835 (setq c (1+ c))
8836 (cperl-update-syntaxification (point) (point))
8837 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8838 (message "to %s:%6s,%7s" l delta tot))
8839 tot))
8840
8841(defun cperl-emulate-lazy-lock (&optional window-size)
8842 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8843Start fontifying the buffer from the start (or end) using the given
8844WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8845goes backwards; default is -50. This function is not CPerl-specific; it
8846may be used to debug problems with delayed incremental fontification."
8847 (interactive
8848 "nSize of window for incremental fontification, negative goes backwards: ")
8849 (or window-size (setq window-size -50))
8850 (let ((pos (if (> window-size 0)
8851 (point-min)
8852 (point-max)))
8853 p)
8854 (goto-char pos)
8855 (normal-mode)
8856 ;; Why needed??? With older font-locks???
8857 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8858 (while (if (> window-size 0)
8859 (< pos (point-max))
8860 (> pos (point-min)))
8861 (setq p (progn
8862 (forward-line window-size)
8863 (point)))
8864 (font-lock-fontify-region (min p pos) (max p pos))
8865 (setq pos p))))
8866
8867\f
db133cb6 8868(defun cperl-lazy-install ()) ; Avoid a warning
f739b53b 8869(defun cperl-lazy-unstall ()) ; Avoid a warning
f83d2997
KH
8870
8871(if (fboundp 'run-with-idle-timer)
8872 (progn
8873 (defvar cperl-help-shown nil
8874 "Non-nil means that the help was already shown now.")
8875
8876 (defvar cperl-lazy-installed nil
8877 "Non-nil means that the lazy-help handlers are installed now.")
8878
8879 (defun cperl-lazy-install ()
f739b53b
SM
8880 "Switches on Auto-Help on Perl constructs (put in the message area).
8881Delay of auto-help controlled by `cperl-lazy-help-time'."
f83d2997 8882 (interactive)
4ab89e7b 8883 (make-local-variable 'cperl-help-shown)
f83d2997
KH
8884 (if (and (cperl-val 'cperl-lazy-help-time)
8885 (not cperl-lazy-installed))
8886 (progn
8887 (add-hook 'post-command-hook 'cperl-lazy-hook)
5c8b7eaf
SS
8888 (run-with-idle-timer
8889 (cperl-val 'cperl-lazy-help-time 1000000 5)
8890 t
f83d2997
KH
8891 'cperl-get-help-defer)
8892 (setq cperl-lazy-installed t))))
8893
8894 (defun cperl-lazy-unstall ()
f739b53b
SM
8895 "Switches off Auto-Help on Perl constructs (put in the message area).
8896Delay of auto-help controlled by `cperl-lazy-help-time'."
f83d2997
KH
8897 (interactive)
8898 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8899 (cancel-function-timers 'cperl-get-help-defer)
8900 (setq cperl-lazy-installed nil))
8901
8902 (defun cperl-lazy-hook ()
8903 (setq cperl-help-shown nil))
8904
8905 (defun cperl-get-help-defer ()
83261a2f 8906 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
f83d2997
KH
8907 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8908 (cperl-get-help)
8909 (setq cperl-help-shown t))))
8910 (cperl-lazy-install)))
8911
db133cb6
RS
8912
8913;;; Plug for wrong font-lock:
8914
8915(defun cperl-font-lock-unfontify-region-function (beg end)
4ab89e7b
SM
8916 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8917 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8918 before-change-functions after-change-functions
8919 deactivate-mark buffer-file-name buffer-file-truename)
8920 (remove-text-properties beg end '(face nil))
8921 (if (and (not modified) (buffer-modified-p))
8922 (set-buffer-modified-p nil))))
8923
8924(defun cperl-font-lock-fontify-region-function (beg end loudly)
8925 "Extends the region to safe positions, then calls the default function.
8926Newer `font-lock's can do it themselves.
8927We unwind only as far as needed for fontification. Syntaxification may
8928do extra unwind via `cperl-unwind-to-safe'."
8929 (save-excursion
8930 (goto-char beg)
8931 (while (and beg
8932 (progn
8933 (beginning-of-line)
8934 (eq (get-text-property (setq beg (point)) 'syntax-type)
8935 'multiline)))
8936 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8937 (goto-char beg)))
8938 (setq beg (point))
8939 (goto-char end)
8940 (while (and end
8941 (progn
8942 (or (bolp) (condition-case nil
8943 (forward-line 1)
8944 (error nil)))
8945 (eq (get-text-property (setq end (point)) 'syntax-type)
8946 'multiline)))
8947 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8948 (goto-char end))
8949 (setq end (point)))
8950 (font-lock-default-fontify-region beg end loudly))
db133cb6
RS
8951
8952(defvar cperl-d-l nil)
8953(defun cperl-fontify-syntaxically (end)
5bd52f0e 8954 ;; Some vars for debugging only
6c389151 8955 ;; (message "Syntaxifying...")
4ab89e7b 8956 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
83261a2f 8957 (istate (car cperl-syntax-state))
4ab89e7b
SM
8958 start from-start edebug-backtrace-buffer)
8959 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8960 (progn
8961 (require 'edebug)
8962 (let ((f 'edebug-backtrace))
8963 (funcall f)))) ; Avoid compile-time warning
db133cb6 8964 (or cperl-syntax-done-to
4ab89e7b
SM
8965 (setq cperl-syntax-done-to (point-min)
8966 from-start t))
8967 (setq start (if (and cperl-hook-after-change
8968 (not from-start))
8969 cperl-syntax-done-to ; Fontify without change; ignore start
8970 ;; Need to forget what is after `start'
8971 (min cperl-syntax-done-to (point))))
8972 (goto-char start)
8973 (beginning-of-line)
8974 (setq start (point))
8975 (and cperl-syntaxify-unwind
8976 (setq end (cperl-unwind-to-safe t end)
8977 start (point)))
db133cb6
RS
8978 (and (> end start)
8979 (setq cperl-syntax-done-to start) ; In case what follows fails
8980 (cperl-find-pods-heres start end t nil t))
4ab89e7b
SM
8981 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8982 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8983 dbg iend start end idone cperl-syntax-done-to
5c8b7eaf 8984 istate (car cperl-syntax-state))) ; For debugging
83261a2f 8985 nil)) ; Do not iterate
db133cb6 8986
5bd52f0e 8987(defun cperl-fontify-update (end)
4ab89e7b
SM
8988 (let ((pos (point-min)) prop posend)
8989 (setq end (point-max))
5bd52f0e 8990 (while (< pos end)
4ab89e7b
SM
8991 (setq prop (get-text-property pos 'cperl-postpone)
8992 posend (next-single-property-change pos 'cperl-postpone nil end))
5bd52f0e
RS
8993 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8994 (setq pos posend)))
83261a2f 8995 nil) ; Do not iterate
5bd52f0e 8996
4ab89e7b
SM
8997(defun cperl-fontify-update-bad (end)
8998 ;; Since fontification happens with different region than syntaxification,
8999 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
9000 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
9001 (if prop
9002 (setq pos (or (cperl-beginning-of-property
9003 (cperl-1+ pos) 'cperl-postpone)
9004 (point-min))))
9005 (while (< pos end)
9006 (setq posend (next-single-property-change pos 'cperl-postpone))
9007 (and prop (put-text-property pos posend (car prop) (cdr prop)))
9008 (setq pos posend)
9009 (setq prop (get-text-property pos 'cperl-postpone))))
9010 nil) ; Do not iterate
9011
9012;; Called when any modification is made to buffer text.
9013(defun cperl-after-change-function (beg end old-len)
9014 ;; We should have been informed about changes by `font-lock'. Since it
9015 ;; does not inform as which calls are defered, do it ourselves
9016 (if cperl-syntax-done-to
9017 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
9018
5bd52f0e
RS
9019(defun cperl-update-syntaxification (from to)
9020 (if (and cperl-use-syntax-table-text-property
9021 cperl-syntaxify-by-font-lock
9022 (or (null cperl-syntax-done-to)
9023 (< cperl-syntax-done-to to)))
9024 (progn
9025 (save-excursion
9026 (goto-char from)
9027 (cperl-fontify-syntaxically to)))))
9028
5c8b7eaf 9029(defvar cperl-version
4ab89e7b 9030 (let ((v "Revision: 5.22"))
5bd52f0e
RS
9031 (string-match ":\\s *\\([0-9.]+\\)" v)
9032 (substring v (match-beginning 1) (match-end 1)))
9033 "Version of IZ-supported CPerl package this file is based on.")
9034
f83d2997
KH
9035(provide 'cperl-mode)
9036
ab5796a9 9037;;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
f83d2997 9038;;; cperl-mode.el ends here