]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cperl-mode.el
Require rmail not just when compiling.
[gnu-emacs] / lisp / progmodes / cperl-mode.el
1 ;;; cperl-mode.el --- Perl code editing commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 ;; 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: Ilya Zakharevich
8 ;; Bob Olson
9 ;; Maintainer: Ilya Zakharevich <ilyaz@cpan.org>
10 ;; Keywords: languages, Perl
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
28
29 ;;; Commentary:
30
31 ;; You can either fine-tune the bells and whistles of this mode or
32 ;; bulk enable them by putting
33
34 ;; (setq cperl-hairy t)
35
36 ;; in your .emacs file. (Emacs rulers do not consider it politically
37 ;; correct to make whistles enabled by default.)
38
39 ;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<<
40 ;; or as help on variables `cperl-tips', `cperl-problems', <<<<<<
41 ;; `cperl-praise', `cperl-speed'. <<<<<<
42
43 ;; The mode information (on C-h m) provides some customization help.
44 ;; If you use font-lock feature of this mode, it is advisable to use
45 ;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock.
46
47 ;; Faces used now: three faces for first-class and second-class keywords
48 ;; and control flow words, one for each: comments, string, labels,
49 ;; functions definitions and packages, arrays, hashes, and variable
50 ;; definitions. If you do not see all these faces, your font-lock does
51 ;; not define them, so you need to define them manually.
52
53 ;; This mode supports font-lock, imenu and mode-compile. In the
54 ;; hairy version font-lock is on, but you should activate imenu
55 ;; yourself (note that mode-compile is not standard yet). Well, you
56 ;; can use imenu from keyboard anyway (M-x imenu), but it is better
57 ;; to bind it like that:
58
59 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
60
61 ;;; Font lock bugs as of v4.32:
62
63 ;; The following kinds of Perl code erroneously start strings:
64 ;; \$` \$' \$"
65 ;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
66 ;; likewise with m, tr, y, q, qX instead of s
67
68 ;;; Code:
69 \f
70 (defvar vc-rcs-header)
71 (defvar vc-sccs-header)
72
73 (eval-when-compile
74 (condition-case nil
75 (require 'custom)
76 (error nil))
77 (condition-case nil
78 (require 'man)
79 (error nil))
80 (defvar cperl-can-font-lock
81 (or (featurep 'xemacs)
82 (and (boundp 'emacs-major-version)
83 (or window-system
84 (> emacs-major-version 20)))))
85 (if cperl-can-font-lock
86 (require 'font-lock))
87 (defvar msb-menu-cond)
88 (defvar gud-perldb-history)
89 (defvar font-lock-background-mode) ; not in Emacs
90 (defvar font-lock-display-type) ; ditto
91 (defvar paren-backwards-message) ; Not in newer XEmacs?
92 (or (fboundp 'defgroup)
93 (defmacro defgroup (name val doc &rest arr)
94 nil))
95 (or (fboundp 'custom-declare-variable)
96 (defmacro defcustom (name val doc &rest arr)
97 `(defvar ,name ,val ,doc)))
98 (or (and (fboundp 'custom-declare-variable)
99 (string< "19.31" emacs-version)) ; Checked with 19.30: defface does not work
100 (defmacro defface (&rest arr)
101 nil))
102 ;; Avoid warning (tmp definitions)
103 (or (fboundp 'x-color-defined-p)
104 (defmacro x-color-defined-p (col)
105 (cond ((fboundp 'color-defined-p) `(color-defined-p ,col))
106 ;; XEmacs >= 19.12
107 ((fboundp 'valid-color-name-p) `(valid-color-name-p ,col))
108 ;; XEmacs 19.11
109 ((fboundp 'x-valid-color-name-p) `(x-valid-color-name-p ,col))
110 (t '(error "Cannot implement color-defined-p")))))
111 (defmacro cperl-is-face (arg) ; Takes quoted arg
112 (cond ((fboundp 'find-face)
113 `(find-face ,arg))
114 (;;(and (fboundp 'face-list)
115 ;; (face-list))
116 (fboundp 'face-list)
117 `(member ,arg (and (fboundp 'face-list)
118 (face-list))))
119 (t
120 `(boundp ,arg))))
121 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
122 (cond ((fboundp 'make-face)
123 `(make-face (quote ,arg)))
124 (t
125 `(defvar ,arg (quote ,arg) ,descr))))
126 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
127 `(progn
128 (or (cperl-is-face (quote ,arg))
129 (cperl-make-face ,arg ,descr))
130 (or (boundp (quote ,arg)) ; We use unquoted variants too
131 (defvar ,arg (quote ,arg) ,descr))))
132 (if (featurep 'xemacs)
133 (defmacro cperl-etags-snarf-tag (file line)
134 `(progn
135 (beginning-of-line 2)
136 (list ,file ,line)))
137 (defmacro cperl-etags-snarf-tag (file line)
138 `(etags-snarf-tag)))
139 (if (featurep 'xemacs)
140 (defmacro cperl-etags-goto-tag-location (elt)
141 ;;(progn
142 ;; (switch-to-buffer (get-file-buffer (elt ,elt 0)))
143 ;; (set-buffer (get-file-buffer (elt ,elt 0)))
144 ;; Probably will not work due to some save-excursion???
145 ;; Or save-file-position?
146 ;; (message "Did I get to line %s?" (elt ,elt 1))
147 `(goto-line (string-to-int (elt ,elt 1))))
148 ;;)
149 (defmacro cperl-etags-goto-tag-location (elt)
150 `(etags-goto-tag-location ,elt))))
151
152 (defvar cperl-can-font-lock
153 (or (featurep 'xemacs)
154 (and (boundp 'emacs-major-version)
155 (or window-system
156 (> emacs-major-version 20)))))
157
158 (defun cperl-choose-color (&rest list)
159 (let (answer)
160 (while list
161 (or answer
162 (if (or (x-color-defined-p (car list))
163 (null (cdr list)))
164 (setq answer (car list))))
165 (setq list (cdr list)))
166 answer))
167
168 (defgroup cperl nil
169 "Major mode for editing Perl code."
170 :prefix "cperl-"
171 :group 'languages
172 :version "20.3")
173
174 (defgroup cperl-indentation-details nil
175 "Indentation."
176 :prefix "cperl-"
177 :group 'cperl)
178
179 (defgroup cperl-affected-by-hairy nil
180 "Variables affected by `cperl-hairy'."
181 :prefix "cperl-"
182 :group 'cperl)
183
184 (defgroup cperl-autoinsert-details nil
185 "Auto-insert tuneup."
186 :prefix "cperl-"
187 :group 'cperl)
188
189 (defgroup cperl-faces nil
190 "Fontification colors."
191 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
192 :prefix "cperl-"
193 :group 'cperl)
194
195 (defgroup cperl-speed nil
196 "Speed vs. validity tuneup."
197 :prefix "cperl-"
198 :group 'cperl)
199
200 (defgroup cperl-help-system nil
201 "Help system tuneup."
202 :prefix "cperl-"
203 :group 'cperl)
204
205 \f
206 (defcustom cperl-extra-newline-before-brace nil
207 "*Non-nil means that if, elsif, while, until, else, for, foreach
208 and do constructs look like:
209
210 if ()
211 {
212 }
213
214 instead of:
215
216 if () {
217 }"
218 :type 'boolean
219 :group 'cperl-autoinsert-details)
220
221 (defcustom cperl-extra-newline-before-brace-multiline
222 cperl-extra-newline-before-brace
223 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
224 for constructs with multiline if/unless/while/until/for/foreach condition."
225 :type 'boolean
226 :group 'cperl-autoinsert-details)
227
228 (defcustom cperl-indent-level 2
229 "*Indentation of CPerl statements with respect to containing block."
230 :type 'integer
231 :group 'cperl-indentation-details)
232
233 ;; Is is not unusual to put both things like perl-indent-level and
234 ;; cperl-indent-level in the local variable section of a file. If only
235 ;; one of perl-mode and cperl-mode is in use, a warning will be issued
236 ;; about the variable. Autoload these here, so that no warning is
237 ;; issued when using either perl-mode or cperl-mode.
238 ;;;###autoload(put 'cperl-indent-level 'safe-local-variable 'integerp)
239 ;;;###autoload(put 'cperl-brace-offset 'safe-local-variable 'integerp)
240 ;;;###autoload(put 'cperl-continued-brace-offset 'safe-local-variable 'integerp)
241 ;;;###autoload(put 'cperl-label-offset 'safe-local-variable 'integerp)
242 ;;;###autoload(put 'cperl-continued-statement-offset 'safe-local-variable 'integerp)
243 ;;;###autoload(put 'cperl-extra-newline-before-brace 'safe-local-variable 'booleanp)
244 ;;;###autoload(put 'cperl-merge-trailing-else 'safe-local-variable 'booleanp)
245
246 (defcustom cperl-lineup-step nil
247 "*`cperl-lineup' will always lineup at multiple of this number.
248 If nil, the value of `cperl-indent-level' will be used."
249 :type '(choice (const nil) integer)
250 :group 'cperl-indentation-details)
251
252 (defcustom cperl-brace-imaginary-offset 0
253 "*Imagined indentation of a Perl open brace that actually follows a statement.
254 An open brace following other text is treated as if it were this far
255 to the right of the start of its line."
256 :type 'integer
257 :group 'cperl-indentation-details)
258
259 (defcustom cperl-brace-offset 0
260 "*Extra indentation for braces, compared with other text in same context."
261 :type 'integer
262 :group 'cperl-indentation-details)
263 (defcustom cperl-label-offset -2
264 "*Offset of CPerl label lines relative to usual indentation."
265 :type 'integer
266 :group 'cperl-indentation-details)
267 (defcustom cperl-min-label-indent 1
268 "*Minimal offset of CPerl label lines."
269 :type 'integer
270 :group 'cperl-indentation-details)
271 (defcustom cperl-continued-statement-offset 2
272 "*Extra indent for lines not starting new statements."
273 :type 'integer
274 :group 'cperl-indentation-details)
275 (defcustom cperl-continued-brace-offset 0
276 "*Extra indent for substatements that start with open-braces.
277 This is in addition to cperl-continued-statement-offset."
278 :type 'integer
279 :group 'cperl-indentation-details)
280 (defcustom cperl-close-paren-offset -1
281 "*Extra indent for substatements that start with close-parenthesis."
282 :type 'integer
283 :group 'cperl-indentation-details)
284
285 (defcustom cperl-indent-wrt-brace t
286 "*Non-nil means indent statements in if/etc block relative brace, not if/etc.
287 Versions 5.2 ... 5.20 behaved as if this were `nil'."
288 :type 'boolean
289 :group 'cperl-indentation-details)
290
291 (defcustom cperl-auto-newline nil
292 "*Non-nil means automatically newline before and after braces,
293 and after colons and semicolons, inserted in CPerl code. The following
294 \\[cperl-electric-backspace] will remove the inserted whitespace.
295 Insertion after colons requires both this variable and
296 `cperl-auto-newline-after-colon' set."
297 :type 'boolean
298 :group 'cperl-autoinsert-details)
299
300 (defcustom cperl-autoindent-on-semi nil
301 "*Non-nil means automatically indent after insertion of (semi)colon.
302 Active if `cperl-auto-newline' is false."
303 :type 'boolean
304 :group 'cperl-autoinsert-details)
305
306 (defcustom cperl-auto-newline-after-colon nil
307 "*Non-nil means automatically newline even after colons.
308 Subject to `cperl-auto-newline' setting."
309 :type 'boolean
310 :group 'cperl-autoinsert-details)
311
312 (defcustom cperl-tab-always-indent t
313 "*Non-nil means TAB in CPerl mode should always reindent the current line,
314 regardless of where in the line point is when the TAB command is used."
315 :type 'boolean
316 :group 'cperl-indentation-details)
317
318 (defcustom cperl-font-lock nil
319 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
320 Can be overwritten by `cperl-hairy' if nil."
321 :type '(choice (const null) boolean)
322 :group 'cperl-affected-by-hairy)
323
324 (defcustom cperl-electric-lbrace-space nil
325 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
326 Can be overwritten by `cperl-hairy' if nil."
327 :type '(choice (const null) boolean)
328 :group 'cperl-affected-by-hairy)
329
330 (defcustom cperl-electric-parens-string "({[]})<"
331 "*String of parentheses that should be electric in CPerl.
332 Closing ones are electric only if the region is highlighted."
333 :type 'string
334 :group 'cperl-affected-by-hairy)
335
336 (defcustom cperl-electric-parens nil
337 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
338 Can be overwritten by `cperl-hairy' if nil."
339 :type '(choice (const null) boolean)
340 :group 'cperl-affected-by-hairy)
341
342 (defvar zmacs-regions) ; Avoid warning
343
344 (defcustom cperl-electric-parens-mark
345 (and window-system
346 (or (and (boundp 'transient-mark-mode) ; For Emacs
347 transient-mark-mode)
348 (and (boundp 'zmacs-regions) ; For XEmacs
349 zmacs-regions)))
350 "*Not-nil means that electric parens look for active mark.
351 Default is yes if there is visual feedback on mark."
352 :type 'boolean
353 :group 'cperl-autoinsert-details)
354
355 (defcustom cperl-electric-linefeed nil
356 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
357 In any case these two mean plain and hairy linefeeds together.
358 Can be overwritten by `cperl-hairy' if nil."
359 :type '(choice (const null) boolean)
360 :group 'cperl-affected-by-hairy)
361
362 (defcustom cperl-electric-keywords nil
363 "*Not-nil (and non-null) means keywords are electric in CPerl.
364 Can be overwritten by `cperl-hairy' if nil.
365
366 Uses `abbrev-mode' to do the expansion. If you want to use your
367 own abbrevs in cperl-mode, but do not want keywords to be
368 electric, you must redefine `cperl-mode-abbrev-table': do
369 \\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
370 that paragraph, delete the words that appear at the ends of lines and
371 that begin with \"cperl-electric\".
372 "
373 :type '(choice (const null) boolean)
374 :group 'cperl-affected-by-hairy)
375
376 (defcustom cperl-electric-backspace-untabify t
377 "*Not-nil means electric-backspace will untabify in CPerl."
378 :type 'boolean
379 :group 'cperl-autoinsert-details)
380
381 (defcustom cperl-hairy nil
382 "*Not-nil means most of the bells and whistles are enabled in CPerl.
383 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
384 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
385 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
386 `cperl-lazy-help-time'."
387 :type 'boolean
388 :group 'cperl-affected-by-hairy)
389
390 (defcustom cperl-comment-column 32
391 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
392 :type 'integer
393 :group 'cperl-indentation-details)
394
395 (defcustom cperl-indent-comment-at-column-0 nil
396 "*Non-nil means that comment started at column 0 should be indentable."
397 :type 'boolean
398 :group 'cperl-indentation-details)
399
400 (defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
401 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
402 :type '(repeat string)
403 :group 'cperl)
404
405 (defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
406 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
407 :type '(repeat string)
408 :group 'cperl)
409
410 ;; This became obsolete...
411 (defvar cperl-vc-header-alist nil)
412 (make-obsolete-variable
413 'cperl-vc-header-alist
414 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead.")
415
416 (defcustom cperl-clobber-mode-lists
417 (not
418 (and
419 (boundp 'interpreter-mode-alist)
420 (assoc "miniperl" interpreter-mode-alist)
421 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
422 "*Whether to install us into `interpreter-' and `extension' mode lists."
423 :type 'boolean
424 :group 'cperl)
425
426 (defcustom cperl-info-on-command-no-prompt nil
427 "*Not-nil (and non-null) means not to prompt on C-h f.
428 The opposite behavior is always available if prefixed with C-c.
429 Can be overwritten by `cperl-hairy' if nil."
430 :type '(choice (const null) boolean)
431 :group 'cperl-affected-by-hairy)
432
433 (defcustom cperl-clobber-lisp-bindings nil
434 "*Not-nil (and non-null) means not overwrite C-h f.
435 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
436 Can be overwritten by `cperl-hairy' if nil."
437 :type '(choice (const null) boolean)
438 :group 'cperl-affected-by-hairy)
439
440 (defcustom cperl-lazy-help-time nil
441 "*Not-nil (and non-null) means to show lazy help after given idle time.
442 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
443 :type '(choice (const null) (const nil) integer)
444 :group 'cperl-affected-by-hairy)
445
446 (defcustom cperl-pod-face 'font-lock-comment-face
447 "*Face for POD highlighting."
448 :type 'face
449 :group 'cperl-faces)
450
451 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
452 "*Face for POD highlighting.
453 Font for POD headers."
454 :type 'face
455 :group 'cperl-faces)
456
457 (defcustom cperl-here-face 'font-lock-string-face
458 "*Face for here-docs highlighting."
459 :type 'face
460 :group 'cperl-faces)
461
462 ;;; Some double-evaluation happened with font-locks... Needed with 21.2...
463 (defvar cperl-singly-quote-face (featurep 'xemacs))
464
465 (defcustom cperl-invalid-face 'underline
466 "*Face for highlighting trailing whitespace."
467 :type 'face
468 :version "21.1"
469 :group 'cperl-faces)
470
471 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
472 "*Not-nil after evaluation means to highlight POD and here-docs sections."
473 :type 'boolean
474 :group 'cperl-faces)
475
476 (defcustom cperl-fontify-m-as-s t
477 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
478 :type 'boolean
479 :group 'cperl-faces)
480
481 (defcustom cperl-highlight-variables-indiscriminately nil
482 "*Non-nil means perform additional highlighting on variables.
483 Currently only changes how scalar variables are highlighted.
484 Note that that variable is only read at initialization time for
485 the variable `cperl-font-lock-keywords-2', so changing it after you've
486 entered CPerl mode the first time will have no effect."
487 :type 'boolean
488 :group 'cperl)
489
490 (defcustom cperl-pod-here-scan t
491 "*Not-nil means look for POD and here-docs sections during startup.
492 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
493 :type 'boolean
494 :group 'cperl-speed)
495
496 (defcustom cperl-regexp-scan t
497 "*Not-nil means make marking of regular expression more thorough.
498 Effective only with `cperl-pod-here-scan'."
499 :type 'boolean
500 :group 'cperl-speed)
501
502 (defcustom cperl-hook-after-change t
503 "*Not-nil means install hook to know which regions of buffer are changed.
504 May significantly speed up delayed fontification. Changes take effect
505 after reload."
506 :type 'boolean
507 :group 'cperl-speed)
508
509 (defcustom cperl-imenu-addback nil
510 "*Not-nil means add backreferences to generated `imenu's.
511 May require patched `imenu' and `imenu-go'. Obsolete."
512 :type 'boolean
513 :group 'cperl-help-system)
514
515 (defcustom cperl-max-help-size 66
516 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
517 :type '(choice integer (const nil))
518 :group 'cperl-help-system)
519
520 (defcustom cperl-shrink-wrap-info-frame t
521 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
522 :type 'boolean
523 :group 'cperl-help-system)
524
525 (defcustom cperl-info-page "perl"
526 "*Name of the info page containing perl docs.
527 Older version of this page was called `perl5', newer `perl'."
528 :type 'string
529 :group 'cperl-help-system)
530
531 (defcustom cperl-use-syntax-table-text-property
532 (boundp 'parse-sexp-lookup-properties)
533 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
534 :type 'boolean
535 :group 'cperl-speed)
536
537 (defcustom cperl-use-syntax-table-text-property-for-tags
538 cperl-use-syntax-table-text-property
539 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
540 :type 'boolean
541 :group 'cperl-speed)
542
543 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
544 "*Regexp to match files to scan when generating TAGS."
545 :type 'regexp
546 :group 'cperl)
547
548 (defcustom cperl-noscan-files-regexp
549 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
550 "*Regexp to match files/dirs to skip when generating TAGS."
551 :type 'regexp
552 :group 'cperl)
553
554 (defcustom cperl-regexp-indent-step nil
555 "*Indentation used when beautifying regexps.
556 If nil, the value of `cperl-indent-level' will be used."
557 :type '(choice integer (const nil))
558 :group 'cperl-indentation-details)
559
560 (defcustom cperl-indent-left-aligned-comments t
561 "*Non-nil means that the comment starting in leftmost column should indent."
562 :type 'boolean
563 :group 'cperl-indentation-details)
564
565 (defcustom cperl-under-as-char nil
566 "*Non-nil means that the _ (underline) should be treated as word char."
567 :type 'boolean
568 :group 'cperl)
569
570 (defcustom cperl-extra-perl-args ""
571 "*Extra arguments to use when starting Perl.
572 Currently used with `cperl-check-syntax' only."
573 :type 'string
574 :group 'cperl)
575
576 (defcustom cperl-message-electric-keyword t
577 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
578 :type 'boolean
579 :group 'cperl-help-system)
580
581 (defcustom cperl-indent-region-fix-constructs 1
582 "*Amount of space to insert between `}' and `else' or `elsif'
583 in `cperl-indent-region'. Set to nil to leave as is. Values other
584 than 1 and nil will probably not work."
585 :type '(choice (const nil) (const 1))
586 :group 'cperl-indentation-details)
587
588 (defcustom cperl-break-one-line-blocks-when-indent t
589 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
590 need to be reformatted into multiline ones when indenting a region."
591 :type 'boolean
592 :group 'cperl-indentation-details)
593
594 (defcustom cperl-fix-hanging-brace-when-indent t
595 "*Non-nil means that BLOCK-end `}' may be put on a separate line
596 when indenting a region.
597 Braces followed by else/elsif/while/until are excepted."
598 :type 'boolean
599 :group 'cperl-indentation-details)
600
601 (defcustom cperl-merge-trailing-else t
602 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
603 may be merged to be on the same line when indenting a region."
604 :type 'boolean
605 :group 'cperl-indentation-details)
606
607 (defcustom cperl-indent-parens-as-block nil
608 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
609 but for trailing \",\" inside the group, which won't increase indentation.
610 One should tune up `cperl-close-paren-offset' as well."
611 :type 'boolean
612 :group 'cperl-indentation-details)
613
614 (defcustom cperl-syntaxify-by-font-lock
615 (and cperl-can-font-lock
616 (boundp 'parse-sexp-lookup-properties))
617 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
618 :type '(choice (const message) boolean)
619 :group 'cperl-speed)
620
621 (defcustom cperl-syntaxify-unwind
622 t
623 "*Non-nil means that CPerl unwinds to a start of a long construction
624 when syntaxifying a chunk of buffer."
625 :type 'boolean
626 :group 'cperl-speed)
627
628 (defcustom cperl-syntaxify-for-menu
629 t
630 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
631 This way enabling/disabling of menu items is more correct."
632 :type 'boolean
633 :group 'cperl-speed)
634
635 (defcustom cperl-ps-print-face-properties
636 '((font-lock-keyword-face nil nil bold shadow)
637 (font-lock-variable-name-face nil nil bold)
638 (font-lock-function-name-face nil nil bold italic box)
639 (font-lock-constant-face nil "LightGray" bold)
640 (cperl-array-face nil "LightGray" bold underline)
641 (cperl-hash-face nil "LightGray" bold italic underline)
642 (font-lock-comment-face nil "LightGray" italic)
643 (font-lock-string-face nil nil italic underline)
644 (cperl-nonoverridable-face nil nil italic underline)
645 (font-lock-type-face nil nil underline)
646 (font-lock-warning-face nil "LightGray" bold italic box)
647 (underline nil "LightGray" strikeout))
648 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
649 :type '(repeat (cons symbol
650 (cons (choice (const nil) string)
651 (cons (choice (const nil) string)
652 (repeat symbol)))))
653 :group 'cperl-faces)
654
655 (defvar cperl-dark-background
656 (cperl-choose-color "navy" "os2blue" "darkgreen"))
657 (defvar cperl-dark-foreground
658 (cperl-choose-color "orchid1" "orange"))
659
660 (defface cperl-nonoverridable-face
661 `((((class grayscale) (background light))
662 (:background "Gray90" :slant italic :underline t))
663 (((class grayscale) (background dark))
664 (:foreground "Gray80" :slant italic :underline t :weight bold))
665 (((class color) (background light))
666 (:foreground "chartreuse3"))
667 (((class color) (background dark))
668 (:foreground ,cperl-dark-foreground))
669 (t (:weight bold :underline t)))
670 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
671 :group 'cperl-faces)
672
673 (defface cperl-array-face
674 `((((class grayscale) (background light))
675 (:background "Gray90" :weight bold))
676 (((class grayscale) (background dark))
677 (:foreground "Gray80" :weight bold))
678 (((class color) (background light))
679 (:foreground "Blue" :background "lightyellow2" :weight bold))
680 (((class color) (background dark))
681 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
682 (t (:weight bold)))
683 "Font Lock mode face used to highlight array names."
684 :group 'cperl-faces)
685
686 (defface cperl-hash-face
687 `((((class grayscale) (background light))
688 (:background "Gray90" :weight bold :slant italic))
689 (((class grayscale) (background dark))
690 (:foreground "Gray80" :weight bold :slant italic))
691 (((class color) (background light))
692 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
693 (((class color) (background dark))
694 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
695 (t (:weight bold :slant italic)))
696 "Font Lock mode face used to highlight hash names."
697 :group 'cperl-faces)
698
699 \f
700
701 ;;; Short extra-docs.
702
703 (defvar cperl-tips 'please-ignore-this-line
704 "Get maybe newer version of this package from
705 http://ilyaz.org/software/emacs
706 Subdirectory `cperl-mode' may contain yet newer development releases and/or
707 patches to related files.
708
709 For best results apply to an older Emacs the patches from
710 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
711 \(this upgrades syntax-parsing abilities of Emacsen v19.34 and
712 v20.2 up to the level of Emacs v20.3 - a must for a good Perl
713 mode.) As of beginning of 2003, XEmacs may provide a similar ability.
714
715 Get support packages choose-color.el (or font-lock-extra.el before
716 19.30), imenu-go.el from the same place. \(Look for other files there
717 too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
718 later you should use choose-color.el *instead* of font-lock-extra.el
719 \(and you will not get smart highlighting in C :-().
720
721 Note that to enable Compile choices in the menu you need to install
722 mode-compile.el.
723
724 If your Emacs does not default to `cperl-mode' on Perl files, and you
725 want it to: put the following into your .emacs file:
726
727 (defalias 'perl-mode 'cperl-mode)
728
729 Get perl5-info from
730 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
731 Also, one can generate a newer documentation running `pod2texi' converter
732 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
733
734 If you use imenu-go, run imenu on perl5-info buffer (you can do it
735 from Perl menu). If many files are related, generate TAGS files from
736 Tools/Tags submenu in Perl menu.
737
738 If some class structure is too complicated, use Tools/Hierarchy-view
739 from Perl menu, or hierarchic view of imenu. The second one uses the
740 current buffer only, the first one requires generation of TAGS from
741 Perl/Tools/Tags menu beforehand.
742
743 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
744
745 Switch auto-help on/off with Perl/Tools/Auto-help.
746
747 Though with contemporary Emaxen CPerl mode should maintain the correct
748 parsing of Perl even when editing, sometimes it may be lost. Fix this by
749
750 \\[normal-mode]
751
752 In cases of more severe confusion sometimes it is helpful to do
753
754 \\[load-library] cperl-mode RET
755 \\[normal-mode]
756
757 Before reporting (non-)problems look in the problem section of online
758 micro-docs on what I know about CPerl problems.")
759
760 (defvar cperl-problems 'please-ignore-this-line
761 "Description of problems in CPerl mode.
762 Some faces will not be shown on some versions of Emacs unless you
763 install choose-color.el, available from
764 http://ilyaz.org/software/emacs
765
766 `fill-paragraph' on a comment may leave the point behind the
767 paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
768 to detect it and bulk out).
769
770 See documentation of a variable `cperl-problems-old-emaxen' for the
771 problems which disappear if you upgrade Emacs to a reasonably new
772 version (20.3 for Emacs, and those of 2004 for XEmacs).")
773
774 (defvar cperl-problems-old-emaxen 'please-ignore-this-line
775 "Description of problems in CPerl mode specific for older Emacs versions.
776
777 Emacs had a _very_ restricted syntax parsing engine until version
778 20.1. Most problems below are corrected starting from this version of
779 Emacs, and all of them should be fixed in version 20.3. (Or apply
780 patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
781 this respect (until 2003).
782
783 Note that even with newer Emacsen in some very rare cases the details
784 of interaction of `font-lock' and syntaxification may be not cleaned
785 up yet. You may get slightly different colors basing on the order of
786 fontification and syntaxification. Say, the initial faces is correct,
787 but editing the buffer breaks this.
788
789 Even with older Emacsen CPerl mode tries to corrects some Emacs
790 misunderstandings, however, for efficiency reasons the degree of
791 correction is different for different operations. The partially
792 corrected problems are: POD sections, here-documents, regexps. The
793 operations are: highlighting, indentation, electric keywords, electric
794 braces.
795
796 This may be confusing, since the regexp s#//#/#\; may be highlighted
797 as a comment, but it will be recognized as a regexp by the indentation
798 code. Or the opposite case, when a POD section is highlighted, but
799 may break the indentation of the following code (though indentation
800 should work if the balance of delimiters is not broken by POD).
801
802 The main trick (to make $ a \"backslash\") makes constructions like
803 ${aaa} look like unbalanced braces. The only trick I can think of is
804 to insert it as $ {aaa} (valid in perl5, not in perl4).
805
806 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
807 as /($|\\s)/. Note that such a transposition is not always possible.
808
809 The solution is to upgrade your Emacs or patch an older one. Note
810 that Emacs 20.2 has some bugs related to `syntax-table' text
811 properties. Patches are available on the main CPerl download site,
812 and on CPAN.
813
814 If these bugs cannot be fixed on your machine (say, you have an inferior
815 environment and cannot recompile), you may still disable all the fancy stuff
816 via `cperl-use-syntax-table-text-property'.")
817
818 (defvar cperl-praise 'please-ignore-this-line
819 "Advantages of CPerl mode.
820
821 0) It uses the newest `syntax-table' property ;-);
822
823 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
824 mode - but the latter number may have improved too in last years) even
825 with old Emaxen which do not support `syntax-table' property.
826
827 When using `syntax-table' property for syntax assist hints, it should
828 handle 99.995% of lines correct - or somesuch. It automatically
829 updates syntax assist hints when you edit your script.
830
831 2) It is generally believed to be \"the most user-friendly Emacs
832 package\" whatever it may mean (I doubt that the people who say similar
833 things tried _all_ the rest of Emacs ;-), but this was not a lonely
834 voice);
835
836 3) Everything is customizable, one-by-one or in a big sweep;
837
838 4) It has many easily-accessible \"tools\":
839 a) Can run program, check syntax, start debugger;
840 b) Can lineup vertically \"middles\" of rows, like `=' in
841 a = b;
842 cc = d;
843 c) Can insert spaces where this impoves readability (in one
844 interactive sweep over the buffer);
845 d) Has support for imenu, including:
846 1) Separate unordered list of \"interesting places\";
847 2) Separate TOC of POD sections;
848 3) Separate list of packages;
849 4) Hierarchical view of methods in (sub)packages;
850 5) and functions (by the full name - with package);
851 e) Has an interface to INFO docs for Perl; The interface is
852 very flexible, including shrink-wrapping of
853 documentation buffer/frame;
854 f) Has a builtin list of one-line explanations for perl constructs.
855 g) Can show these explanations if you stay long enough at the
856 corresponding place (or on demand);
857 h) Has an enhanced fontification (using 3 or 4 additional faces
858 comparing to font-lock - basically, different
859 namespaces in Perl have different colors);
860 i) Can construct TAGS basing on its knowledge of Perl syntax,
861 the standard menu has 6 different way to generate
862 TAGS (if \"by directory\", .xs files - with C-language
863 bindings - are included in the scan);
864 j) Can build a hierarchical view of classes (via imenu) basing
865 on generated TAGS file;
866 k) Has electric parentheses, electric newlines, uses Abbrev
867 for electric logical constructs
868 while () {}
869 with different styles of expansion (context sensitive
870 to be not so bothering). Electric parentheses behave
871 \"as they should\" in a presence of a visible region.
872 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
873 m) Can convert from
874 if (A) { B }
875 to
876 B if A;
877
878 n) Highlights (by user-choice) either 3-delimiters constructs
879 (such as tr/a/b/), or regular expressions and `y/tr';
880 o) Highlights trailing whitespace;
881 p) Is able to manipulate Perl Regular Expressions to ease
882 conversion to a more readable form.
883 q) Can ispell POD sections and HERE-DOCs.
884 r) Understands comments and character classes inside regular
885 expressions; can find matching () and [] in a regular expression.
886 s) Allows indentation of //x-style regular expressions;
887 t) Highlights different symbols in regular expressions according
888 to their function; much less problems with backslashitis;
889 u) Allows to find regular expressions which contain interpolated parts.
890
891 5) The indentation engine was very smart, but most of tricks may be
892 not needed anymore with the support for `syntax-table' property. Has
893 progress indicator for indentation (with `imenu' loaded).
894
895 6) Indent-region improves inline-comments as well; also corrects
896 whitespace *inside* the conditional/loop constructs.
897
898 7) Fill-paragraph correctly handles multi-line comments;
899
900 8) Can switch to different indentation styles by one command, and restore
901 the settings present before the switch.
902
903 9) When doing indentation of control constructs, may correct
904 line-breaks/spacing between elements of the construct.
905
906 10) Uses a linear-time algorith for indentation of regions (on Emaxen with
907 capable syntax engines).
908
909 11) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
910 ")
911
912 (defvar cperl-speed 'please-ignore-this-line
913 "This is an incomplete compendium of what is available in other parts
914 of CPerl documentation. (Please inform me if I skept anything.)
915
916 There is a perception that CPerl is slower than alternatives. This part
917 of documentation is designed to overcome this misconception.
918
919 *By default* CPerl tries to enable the most comfortable settings.
920 From most points of view, correctly working package is infinitely more
921 comfortable than a non-correctly working one, thus by default CPerl
922 prefers correctness over speed. Below is the guide how to change
923 settings if your preferences are different.
924
925 A) Speed of loading the file. When loading file, CPerl may perform a
926 scan which indicates places which cannot be parsed by primitive Emacs
927 syntax-parsing routines, and marks them up so that either
928
929 A1) CPerl may work around these deficiencies (for big chunks, mostly
930 PODs and HERE-documents), or
931 A2) On capable Emaxen CPerl will use improved syntax-handlings
932 which reads mark-up hints directly.
933
934 The scan in case A2 is much more comprehensive, thus may be slower.
935
936 User can disable syntax-engine-helping scan of A2 by setting
937 `cperl-use-syntax-table-text-property'
938 variable to nil (if it is set to t).
939
940 One can disable the scan altogether (both A1 and A2) by setting
941 `cperl-pod-here-scan'
942 to nil.
943
944 B) Speed of editing operations.
945
946 One can add a (minor) speedup to editing operations by setting
947 `cperl-use-syntax-table-text-property'
948 variable to nil (if it is set to t). This will disable
949 syntax-engine-helping scan, thus will make many more Perl
950 constructs be wrongly recognized by CPerl, thus may lead to
951 wrongly matched parentheses, wrong indentation, etc.
952
953 One can unset `cperl-syntaxify-unwind'. This might speed up editing
954 of, say, long POD sections.")
955
956 (defvar cperl-tips-faces 'please-ignore-this-line
957 "CPerl mode uses following faces for highlighting:
958
959 `cperl-array-face' Array names
960 `cperl-hash-face' Hash names
961 `font-lock-comment-face' Comments, PODs and whatever is considered
962 syntaxically to be not code
963 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
964 2-arg operators s/y/tr/ or of RExen,
965 `font-lock-warning-face' Special-cased m// and s//foo/,
966 `font-lock-function-name-face' _ as a target of a file tests, file tests,
967 subroutine names at the moment of definition
968 (except those conflicting with Perl operators),
969 package names (when recognized), format names
970 `font-lock-keyword-face' Control flow switch constructs, declarators
971 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
972 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
973 literal parts and the terminator of formats
974 and whatever is syntaxically considered
975 as string literals
976 `font-lock-type-face' Overridable keywords
977 `font-lock-variable-name-face' Variable declarations, indirect array and
978 hash names, POD headers/item names
979 `cperl-invalid' Trailing whitespace
980
981 Note that in several situations the highlighting tries to inform about
982 possible confusion, such as different colors for function names in
983 declarations depending on what they (do not) override, or special cases
984 m// and s/// which do not do what one would expect them to do.
985
986 Help with best setup of these faces for printout requested (for each of
987 the faces: please specify bold, italic, underline, shadow and box.)
988
989 In regular expressions (except character classes):
990 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
991 `font-lock-constant-face': Delimiters
992 `font-lock-warning-face' Special-cased m// and s//foo/,
993 Mismatched closing delimiters, parens
994 we couldn't match, misplaced quantifiers,
995 unrecognized escape sequences
996 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
997 `font-lock-type-face' POSIX classes inside charclasses,
998 escape sequences with arguments (\x \23 \p \N)
999 and others match-a-char escape sequences
1000 `font-lock-keyword-face' Capturing parens, and |
1001 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
1002 `font-lock-builtin-face' \"Remaining\" 0-length constructs, executable
1003 parts of a REx, not-capturing parens
1004 `font-lock-variable-name-face' Interpolated constructs, embedded code
1005 `font-lock-comment-face' Embedded comments
1006
1007 ")
1008
1009 \f
1010
1011 ;;; Portability stuff:
1012
1013 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
1014 `(define-key cperl-mode-map
1015 ,(if xemacs-key
1016 `(if (featurep 'xemacs) ,xemacs-key ,emacs-key)
1017 emacs-key)
1018 ,definition))
1019
1020 (defvar cperl-del-back-ch
1021 (car (append (where-is-internal 'delete-backward-char)
1022 (where-is-internal 'backward-delete-char-untabify)))
1023 "Character generated by key bound to `delete-backward-char'.")
1024
1025 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
1026 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1027
1028 (defun cperl-mark-active () (mark)) ; Avoid undefined warning
1029 (if (featurep 'xemacs)
1030 (progn
1031 ;; "Active regions" are on: use region only if active
1032 ;; "Active regions" are off: use region unconditionally
1033 (defun cperl-use-region-p ()
1034 (if zmacs-regions (mark) t)))
1035 (defun cperl-use-region-p ()
1036 (if transient-mark-mode mark-active t))
1037 (defun cperl-mark-active () mark-active))
1038
1039 (defsubst cperl-enable-font-lock ()
1040 cperl-can-font-lock)
1041
1042 (defun cperl-putback-char (c) ; Emacs 19
1043 (set 'unread-command-events (list c))) ; Avoid undefined warning
1044
1045 (if (featurep 'xemacs)
1046 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1047 (setq unread-command-events (list (eval '(character-to-event c))))))
1048
1049 (or (fboundp 'uncomment-region)
1050 (defun uncomment-region (beg end)
1051 (interactive "r")
1052 (comment-region beg end -1)))
1053
1054 (defvar cperl-do-not-fontify
1055 (if (string< emacs-version "19.30")
1056 'fontified
1057 'lazy-lock)
1058 "Text property which inhibits refontification.")
1059
1060 (defsubst cperl-put-do-not-fontify (from to &optional post)
1061 ;; If POST, do not do it with postponed fontification
1062 (if (and post cperl-syntaxify-by-font-lock)
1063 nil
1064 (put-text-property (max (point-min) (1- from))
1065 to cperl-do-not-fontify t)))
1066
1067 (defcustom cperl-mode-hook nil
1068 "Hook run by CPerl mode."
1069 :type 'hook
1070 :group 'cperl)
1071
1072 (defvar cperl-syntax-state nil)
1073 (defvar cperl-syntax-done-to nil)
1074 (defvar cperl-emacs-can-parse (> (length (save-excursion
1075 (parse-partial-sexp (point) (point)))) 9))
1076 \f
1077 ;; Make customization possible "in reverse"
1078 (defsubst cperl-val (symbol &optional default hairy)
1079 (cond
1080 ((eq (symbol-value symbol) 'null) default)
1081 (cperl-hairy (or hairy t))
1082 (t (symbol-value symbol))))
1083 \f
1084
1085 (defun cperl-make-indent (column &optional minimum keep)
1086 "Makes indent of the current line the requested amount.
1087 Unless KEEP, removes the old indentation. Works around a bug in ancient
1088 versions of Emacs."
1089 (let ((prop (get-text-property (point) 'syntax-type)))
1090 (or keep
1091 (delete-horizontal-space))
1092 (indent-to column minimum)
1093 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1094 (and prop
1095 (> (current-column) 0)
1096 (save-excursion
1097 (beginning-of-line)
1098 (or (get-text-property (point) 'syntax-type)
1099 (and (looking-at "\\=[ \t]")
1100 (put-text-property (point) (match-end 0)
1101 'syntax-type prop)))))))
1102
1103 ;;; Probably it is too late to set these guys already, but it can help later:
1104
1105 ;;;(and cperl-clobber-mode-lists
1106 ;;;(setq auto-mode-alist
1107 ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1108 ;;;(and (boundp 'interpreter-mode-alist)
1109 ;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1110 ;;; '(("miniperl" . perl-mode))))))
1111 (eval-when-compile
1112 (mapc (lambda (p)
1113 (condition-case nil
1114 (require p)
1115 (error nil)))
1116 '(imenu easymenu etags timer man info))
1117 (if (fboundp 'ps-extend-face-list)
1118 (defmacro cperl-ps-extend-face-list (arg)
1119 `(ps-extend-face-list ,arg))
1120 (defmacro cperl-ps-extend-face-list (arg)
1121 `(error "This version of Emacs has no `ps-extend-face-list'")))
1122 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1123 ;; macros instead of defsubsts don't work on Emacs, so we do the
1124 ;; expansion manually. Any other suggestions?
1125 (require 'cl))
1126
1127 (defvar cperl-mode-abbrev-table nil
1128 "Abbrev table in use in CPerl mode buffers.")
1129
1130 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1131
1132 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
1133
1134 (if cperl-mode-map nil
1135 (setq cperl-mode-map (make-sparse-keymap))
1136 (cperl-define-key "{" 'cperl-electric-lbrace)
1137 (cperl-define-key "[" 'cperl-electric-paren)
1138 (cperl-define-key "(" 'cperl-electric-paren)
1139 (cperl-define-key "<" 'cperl-electric-paren)
1140 (cperl-define-key "}" 'cperl-electric-brace)
1141 (cperl-define-key "]" 'cperl-electric-rparen)
1142 (cperl-define-key ")" 'cperl-electric-rparen)
1143 (cperl-define-key ";" 'cperl-electric-semi)
1144 (cperl-define-key ":" 'cperl-electric-terminator)
1145 (cperl-define-key "\C-j" 'newline-and-indent)
1146 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1147 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1148 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1149 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1150 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1151 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1152 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1153 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1154 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1155 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1156 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1157 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1158 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1159 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1160 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1161 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1162 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
1163 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1164 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1165 [(control meta |)])
1166 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1167 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1168 (cperl-define-key "\177" 'cperl-electric-backspace)
1169 (cperl-define-key "\t" 'cperl-indent-command)
1170 ;; don't clobber the backspace binding:
1171 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1172 [(control c) (control h) F])
1173 (if (cperl-val 'cperl-clobber-lisp-bindings)
1174 (progn
1175 (cperl-define-key "\C-hf"
1176 ;;(concat (char-to-string help-char) "f") ; does not work
1177 'cperl-info-on-command
1178 [(control h) f])
1179 (cperl-define-key "\C-hv"
1180 ;;(concat (char-to-string help-char) "v") ; does not work
1181 'cperl-get-help
1182 [(control h) v])
1183 (cperl-define-key "\C-c\C-hf"
1184 ;;(concat (char-to-string help-char) "f") ; does not work
1185 (key-binding "\C-hf")
1186 [(control c) (control h) f])
1187 (cperl-define-key "\C-c\C-hv"
1188 ;;(concat (char-to-string help-char) "v") ; does not work
1189 (key-binding "\C-hv")
1190 [(control c) (control h) v]))
1191 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1192 [(control c) (control h) f])
1193 (cperl-define-key "\C-c\C-hv"
1194 ;;(concat (char-to-string help-char) "v") ; does not work
1195 'cperl-get-help
1196 [(control c) (control h) v]))
1197 (if (and (featurep 'xemacs)
1198 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1199 (progn
1200 ;; substitute-key-definition is usefulness-deenhanced...
1201 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1202 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1203 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1204 (or (boundp 'fill-paragraph-function)
1205 (substitute-key-definition
1206 'fill-paragraph 'cperl-fill-paragraph
1207 cperl-mode-map global-map))
1208 (substitute-key-definition
1209 'indent-sexp 'cperl-indent-exp
1210 cperl-mode-map global-map)
1211 (substitute-key-definition
1212 'indent-region 'cperl-indent-region
1213 cperl-mode-map global-map)
1214 (substitute-key-definition
1215 'indent-for-comment 'cperl-indent-for-comment
1216 cperl-mode-map global-map)))
1217
1218 (defvar cperl-menu)
1219 (defvar cperl-lazy-installed)
1220 (defvar cperl-old-style nil)
1221 (condition-case nil
1222 (progn
1223 (require 'easymenu)
1224 (easy-menu-define
1225 cperl-menu cperl-mode-map "Menu for CPerl mode"
1226 '("Perl"
1227 ["Beginning of function" beginning-of-defun t]
1228 ["End of function" end-of-defun t]
1229 ["Mark function" mark-defun t]
1230 ["Indent expression" cperl-indent-exp t]
1231 ["Fill paragraph/comment" fill-paragraph t]
1232 "----"
1233 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1234 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1235 ("Regexp"
1236 ["Beautify" cperl-beautify-regexp
1237 cperl-use-syntax-table-text-property]
1238 ["Beautify one level deep" (cperl-beautify-regexp 1)
1239 cperl-use-syntax-table-text-property]
1240 ["Beautify a group" cperl-beautify-level
1241 cperl-use-syntax-table-text-property]
1242 ["Beautify a group one level deep" (cperl-beautify-level 1)
1243 cperl-use-syntax-table-text-property]
1244 ["Contract a group" cperl-contract-level
1245 cperl-use-syntax-table-text-property]
1246 ["Contract groups" cperl-contract-levels
1247 cperl-use-syntax-table-text-property]
1248 "----"
1249 ["Find next interpolated" cperl-next-interpolated-REx
1250 (next-single-property-change (point-min) 'REx-interpolated)]
1251 ["Find next interpolated (no //o)"
1252 cperl-next-interpolated-REx-0
1253 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1254 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1255 ["Find next interpolated (neither //o nor whole-REx)"
1256 cperl-next-interpolated-REx-1
1257 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1258 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1259 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1260 "----"
1261 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1262 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1263 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1264 "----"
1265 ["Run" mode-compile (fboundp 'mode-compile)]
1266 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1267 (get-buffer "*compilation*"))]
1268 ["Next error" next-error (get-buffer "*compilation*")]
1269 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1270 "----"
1271 ["Debugger" cperl-db t]
1272 "----"
1273 ("Tools"
1274 ["Imenu" imenu (fboundp 'imenu)]
1275 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1276 "----"
1277 ["Ispell PODs" cperl-pod-spell
1278 ;; Better not to update syntaxification here:
1279 ;; debugging syntaxificatio can be broken by this???
1280 (or
1281 (get-text-property (point-min) 'in-pod)
1282 (< (progn
1283 (and cperl-syntaxify-for-menu
1284 (cperl-update-syntaxification (point-max) (point-max)))
1285 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1286 (point-max)))]
1287 ["Ispell HERE-DOCs" cperl-here-doc-spell
1288 (< (progn
1289 (and cperl-syntaxify-for-menu
1290 (cperl-update-syntaxification (point-max) (point-max)))
1291 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1292 (point-max))]
1293 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1294 (eq 'here-doc (progn
1295 (and cperl-syntaxify-for-menu
1296 (cperl-update-syntaxification (point) (point)))
1297 (get-text-property (point) 'syntax-type)))]
1298 ["Select this HERE-DOC or POD section"
1299 cperl-select-this-pod-or-here-doc
1300 (memq (progn
1301 (and cperl-syntaxify-for-menu
1302 (cperl-update-syntaxification (point) (point)))
1303 (get-text-property (point) 'syntax-type))
1304 '(here-doc pod))]
1305 "----"
1306 ["CPerl pretty print (exprmntl)" cperl-ps-print
1307 (fboundp 'ps-extend-face-list)]
1308 "----"
1309 ["Syntaxify region" cperl-find-pods-heres-region
1310 (cperl-use-region-p)]
1311 ["Profile syntaxification" cperl-time-fontification t]
1312 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1313 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1314 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1315 (cperl-toggle-set-debug-unwind nil t) t]
1316 "----"
1317 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1318 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1319 ("Tags"
1320 ;;; ["Create tags for current file" cperl-etags t]
1321 ;;; ["Add tags for current file" (cperl-etags t) t]
1322 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1323 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1324 ;;; ["Create tags for Perl files in (sub)directories"
1325 ;;; (cperl-etags nil 'recursive) t]
1326 ;;; ["Add tags for Perl files in (sub)directories"
1327 ;;; (cperl-etags t 'recursive) t])
1328 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1329 ["Create tags for current file" (cperl-write-tags nil t) t]
1330 ["Add tags for current file" (cperl-write-tags) t]
1331 ["Create tags for Perl files in directory"
1332 (cperl-write-tags nil t nil t) t]
1333 ["Add tags for Perl files in directory"
1334 (cperl-write-tags nil nil nil t) t]
1335 ["Create tags for Perl files in (sub)directories"
1336 (cperl-write-tags nil t t t) t]
1337 ["Add tags for Perl files in (sub)directories"
1338 (cperl-write-tags nil nil t t) t]))
1339 ("Perl docs"
1340 ["Define word at point" imenu-go-find-at-position
1341 (fboundp 'imenu-go-find-at-position)]
1342 ["Help on function" cperl-info-on-command t]
1343 ["Help on function at point" cperl-info-on-current-command t]
1344 ["Help on symbol at point" cperl-get-help t]
1345 ["Perldoc" cperl-perldoc t]
1346 ["Perldoc on word at point" cperl-perldoc-at-point t]
1347 ["View manpage of POD in this file" cperl-build-manpage t]
1348 ["Auto-help on" cperl-lazy-install
1349 (and (fboundp 'run-with-idle-timer)
1350 (not cperl-lazy-installed))]
1351 ["Auto-help off" cperl-lazy-unstall
1352 (and (fboundp 'run-with-idle-timer)
1353 cperl-lazy-installed)])
1354 ("Toggle..."
1355 ["Auto newline" cperl-toggle-auto-newline t]
1356 ["Electric parens" cperl-toggle-electric t]
1357 ["Electric keywords" cperl-toggle-abbrev t]
1358 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1359 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1360 ["Auto fill" auto-fill-mode t])
1361 ("Indent styles..."
1362 ["CPerl" (cperl-set-style "CPerl") t]
1363 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1364 ["GNU" (cperl-set-style "GNU") t]
1365 ["C++" (cperl-set-style "C++") t]
1366 ["K&R" (cperl-set-style "K&R") t]
1367 ["BSD" (cperl-set-style "BSD") t]
1368 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1369 ["Memorize Current" (cperl-set-style "Current") t]
1370 ["Memorized" (cperl-set-style-back) cperl-old-style])
1371 ("Micro-docs"
1372 ["Tips" (describe-variable 'cperl-tips) t]
1373 ["Problems" (describe-variable 'cperl-problems) t]
1374 ["Speed" (describe-variable 'cperl-speed) t]
1375 ["Praise" (describe-variable 'cperl-praise) t]
1376 ["Faces" (describe-variable 'cperl-tips-faces) t]
1377 ["CPerl mode" (describe-function 'cperl-mode) t]
1378 ["CPerl version"
1379 (message "The version of master-file for this CPerl is %s-Emacs"
1380 cperl-version) t]))))
1381 (error nil))
1382
1383 (autoload 'c-macro-expand "cmacexp"
1384 "Display the result of expanding all C macros occurring in the region.
1385 The expansion is entirely correct because it uses the C preprocessor."
1386 t)
1387
1388 ;;; These two must be unwound, otherwise take exponential time
1389 (defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1390 "Regular expression to match optional whitespace with interpspersed comments.
1391 Should contain exactly one group.")
1392
1393 ;;; This one is tricky to unwind; still very inefficient...
1394 (defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1395 "Regular expression to match whitespace with interpspersed comments.
1396 Should contain exactly one group.")
1397
1398
1399 ;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1400 ;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1401 ;;; Details of groups in this may be used in several functions; see comments
1402 ;;; near mentioned above variable(s)...
1403 ;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1404 (defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1405 "Match the text after `sub' in a subroutine declaration.
1406 If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1407 of attributes (if present), or end of the name or prototype (whatever is
1408 the last)."
1409 (concat ; Assume n groups before this...
1410 "\\(" ; n+1=name-group
1411 cperl-white-and-comment-rex ; n+2=pre-name
1412 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1413 "\\)" ; END n+1=name-group
1414 (if named "" "?")
1415 "\\(" ; n+4=proto-group
1416 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1417 "\\(([^()]*)\\)" ; n+6=prototype
1418 "\\)?" ; END n+4=proto-group
1419 "\\(" ; n+7=attr-group
1420 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1421 "\\(" ; n+9=start-attr
1422 ":"
1423 (if attr (concat
1424 "\\("
1425 cperl-maybe-white-and-comment-rex ; whitespace-comments
1426 "\\(\\sw\\|_\\)+" ; attr-name
1427 ;; attr-arg (1 level of internal parens allowed!)
1428 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1429 "\\(" ; optional : (XXX allows trailing???)
1430 cperl-maybe-white-and-comment-rex ; whitespace-comments
1431 ":\\)?"
1432 "\\)+")
1433 "[^:]")
1434 "\\)"
1435 "\\)?" ; END n+6=proto-group
1436 ))
1437
1438 ;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1439 ;;; and `cperl-outline-level'.
1440 ;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1441 (defvar cperl-imenu--function-name-regexp-perl
1442 (concat
1443 "^\\(" ; 1 = all
1444 "\\([ \t]*package" ; 2 = package-group
1445 "\\(" ; 3 = package-name-group
1446 cperl-white-and-comment-rex ; 4 = pre-package-name
1447 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1448 "\\|"
1449 "[ \t]*sub"
1450 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1451 cperl-maybe-white-and-comment-rex ; 15=pre-block
1452 "\\|"
1453 "=head\\([1-4]\\)[ \t]+" ; 16=level
1454 "\\([^\n]+\\)$" ; 17=text
1455 "\\)"))
1456
1457 (defvar cperl-outline-regexp
1458 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1459
1460 (defvar cperl-mode-syntax-table nil
1461 "Syntax table in use in CPerl mode buffers.")
1462
1463 (defvar cperl-string-syntax-table nil
1464 "Syntax table in use in CPerl mode string-like chunks.")
1465
1466 (defsubst cperl-1- (p)
1467 (max (point-min) (1- p)))
1468
1469 (defsubst cperl-1+ (p)
1470 (min (point-max) (1+ p)))
1471
1472 (if cperl-mode-syntax-table
1473 ()
1474 (setq cperl-mode-syntax-table (make-syntax-table))
1475 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1476 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1477 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1478 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1479 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1480 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1481 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1482 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1483 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1484 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1485 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1486 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1487 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1488 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1489 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1490 (if cperl-under-as-char
1491 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1492 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1493 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1494 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1495 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1496 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1497 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
1498 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
1499
1500
1501 \f
1502 (defvar cperl-faces-init nil)
1503 ;; Fix for msb.el
1504 (defvar cperl-msb-fixed nil)
1505 (defvar cperl-use-major-mode 'cperl-mode)
1506 (defvar cperl-font-lock-multiline-start nil)
1507 (defvar cperl-font-lock-multiline nil)
1508 (defvar cperl-font-locking nil)
1509
1510 ;; NB as it stands the code in cperl-mode assumes this only has one
1511 ;; element. If Xemacs 19 support were dropped, this could all be simplified.
1512 (defvar cperl-compilation-error-regexp-alist
1513 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
1514 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
1515 2 3))
1516 "Alist that specifies how to match errors in perl output.")
1517
1518 (defvar compilation-error-regexp-alist)
1519
1520 ;;;###autoload
1521 (defun cperl-mode ()
1522 "Major mode for editing Perl code.
1523 Expression and list commands understand all C brackets.
1524 Tab indents for Perl code.
1525 Paragraphs are separated by blank lines only.
1526 Delete converts tabs to spaces as it moves back.
1527
1528 Various characters in Perl almost always come in pairs: {}, (), [],
1529 sometimes <>. When the user types the first, she gets the second as
1530 well, with optional special formatting done on {}. (Disabled by
1531 default.) You can always quote (with \\[quoted-insert]) the left
1532 \"paren\" to avoid the expansion. The processing of < is special,
1533 since most the time you mean \"less\". CPerl mode tries to guess
1534 whether you want to type pair <>, and inserts is if it
1535 appropriate. You can set `cperl-electric-parens-string' to the string that
1536 contains the parenths from the above list you want to be electrical.
1537 Electricity of parenths is controlled by `cperl-electric-parens'.
1538 You may also set `cperl-electric-parens-mark' to have electric parens
1539 look for active mark and \"embrace\" a region if possible.'
1540
1541 CPerl mode provides expansion of the Perl control constructs:
1542
1543 if, else, elsif, unless, while, until, continue, do,
1544 for, foreach, formy and foreachmy.
1545
1546 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1547
1548 The user types the keyword immediately followed by a space, which
1549 causes the construct to be expanded, and the point is positioned where
1550 she is most likely to want to be. eg. when the user types a space
1551 following \"if\" the following appears in the buffer: if () { or if ()
1552 } { } and the cursor is between the parentheses. The user can then
1553 type some boolean expression within the parens. Having done that,
1554 typing \\[cperl-linefeed] places you - appropriately indented - on a
1555 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1556 directive line, then appropriate number of new lines is inserted).
1557
1558 If CPerl decides that you want to insert \"English\" style construct like
1559
1560 bite if angry;
1561
1562 it will not do any expansion. See also help on variable
1563 `cperl-extra-newline-before-brace'. (Note that one can switch the
1564 help message on expansion by setting `cperl-message-electric-keyword'
1565 to nil.)
1566
1567 \\[cperl-linefeed] is a convenience replacement for typing carriage
1568 return. It places you in the next line with proper indentation, or if
1569 you type it inside the inline block of control construct, like
1570
1571 foreach (@lines) {print; print}
1572
1573 and you are on a boundary of a statement inside braces, it will
1574 transform the construct into a multiline and will place you into an
1575 appropriately indented blank line. If you need a usual
1576 `newline-and-indent' behavior, it is on \\[newline-and-indent],
1577 see documentation on `cperl-electric-linefeed'.
1578
1579 Use \\[cperl-invert-if-unless] to change a construction of the form
1580
1581 if (A) { B }
1582
1583 into
1584
1585 B if A;
1586
1587 \\{cperl-mode-map}
1588
1589 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1590 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1591 on electric space between $ and {, `cperl-electric-parens-string' is
1592 the string that contains parentheses that should be electric in CPerl
1593 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1594 setting `cperl-electric-keywords' enables electric expansion of
1595 control structures in CPerl. `cperl-electric-linefeed' governs which
1596 one of two linefeed behavior is preferable. You can enable all these
1597 options simultaneously (recommended mode of use) by setting
1598 `cperl-hairy' to t. In this case you can switch separate options off
1599 by setting them to `null'. Note that one may undo the extra
1600 whitespace inserted by semis and braces in `auto-newline'-mode by
1601 consequent \\[cperl-electric-backspace].
1602
1603 If your site has perl5 documentation in info format, you can use commands
1604 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1605 These keys run commands `cperl-info-on-current-command' and
1606 `cperl-info-on-command', which one is which is controlled by variable
1607 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1608 \(in turn affected by `cperl-hairy').
1609
1610 Even if you have no info-format documentation, short one-liner-style
1611 help is available on \\[cperl-get-help], and one can run perldoc or
1612 man via menu.
1613
1614 It is possible to show this help automatically after some idle time.
1615 This is regulated by variable `cperl-lazy-help-time'. Default with
1616 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1617 secs idle time . It is also possible to switch this on/off from the
1618 menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1619
1620 Use \\[cperl-lineup] to vertically lineup some construction - put the
1621 beginning of the region at the start of construction, and make region
1622 span the needed amount of lines.
1623
1624 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1625 `cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1626 here-docs sections. With capable Emaxen results of scan are used
1627 for indentation too, otherwise they are used for highlighting only.
1628
1629 Variables controlling indentation style:
1630 `cperl-tab-always-indent'
1631 Non-nil means TAB in CPerl mode should always reindent the current line,
1632 regardless of where in the line point is when the TAB command is used.
1633 `cperl-indent-left-aligned-comments'
1634 Non-nil means that the comment starting in leftmost column should indent.
1635 `cperl-auto-newline'
1636 Non-nil means automatically newline before and after braces,
1637 and after colons and semicolons, inserted in Perl code. The following
1638 \\[cperl-electric-backspace] will remove the inserted whitespace.
1639 Insertion after colons requires both this variable and
1640 `cperl-auto-newline-after-colon' set.
1641 `cperl-auto-newline-after-colon'
1642 Non-nil means automatically newline even after colons.
1643 Subject to `cperl-auto-newline' setting.
1644 `cperl-indent-level'
1645 Indentation of Perl statements within surrounding block.
1646 The surrounding block's indentation is the indentation
1647 of the line on which the open-brace appears.
1648 `cperl-continued-statement-offset'
1649 Extra indentation given to a substatement, such as the
1650 then-clause of an if, or body of a while, or just a statement continuation.
1651 `cperl-continued-brace-offset'
1652 Extra indentation given to a brace that starts a substatement.
1653 This is in addition to `cperl-continued-statement-offset'.
1654 `cperl-brace-offset'
1655 Extra indentation for line if it starts with an open brace.
1656 `cperl-brace-imaginary-offset'
1657 An open brace following other text is treated as if it the line started
1658 this far to the right of the actual line indentation.
1659 `cperl-label-offset'
1660 Extra indentation for line that is a label.
1661 `cperl-min-label-indent'
1662 Minimal indentation for line that is a label.
1663
1664 Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1665 `cperl-indent-level' 5 4 2 4
1666 `cperl-brace-offset' 0 0 0 0
1667 `cperl-continued-brace-offset' -5 -4 0 0
1668 `cperl-label-offset' -5 -4 -2 -4
1669 `cperl-continued-statement-offset' 5 4 2 4
1670
1671 CPerl knows several indentation styles, and may bulk set the
1672 corresponding variables. Use \\[cperl-set-style] to do this. Use
1673 \\[cperl-set-style-back] to restore the memorized preexisting values
1674 \(both available from menu). See examples in `cperl-style-examples'.
1675
1676 Part of the indentation style is how different parts of if/elsif/else
1677 statements are broken into lines; in CPerl, this is reflected on how
1678 templates for these constructs are created (controlled by
1679 `cperl-extra-newline-before-brace'), and how reflow-logic should treat \"continuation\" blocks of else/elsif/continue, controlled by the same variable,
1680 and by `cperl-extra-newline-before-brace-multiline',
1681 `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1682
1683 If `cperl-indent-level' is 0, the statement after opening brace in
1684 column 0 is indented on
1685 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1686
1687 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1688 with no args.
1689
1690 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1691 or as help on variables `cperl-tips', `cperl-problems',
1692 `cperl-praise', `cperl-speed'."
1693 (interactive)
1694 (kill-all-local-variables)
1695 (use-local-map cperl-mode-map)
1696 (if (cperl-val 'cperl-electric-linefeed)
1697 (progn
1698 (local-set-key "\C-J" 'cperl-linefeed)
1699 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1700 (if (and
1701 (cperl-val 'cperl-clobber-lisp-bindings)
1702 (cperl-val 'cperl-info-on-command-no-prompt))
1703 (progn
1704 ;; don't clobber the backspace binding:
1705 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1706 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1707 [(control c) (control h) f])))
1708 (setq major-mode cperl-use-major-mode)
1709 (setq mode-name "CPerl")
1710 (let ((prev-a-c abbrevs-changed))
1711 (define-abbrev-table 'cperl-mode-abbrev-table '(
1712 ("if" "if" cperl-electric-keyword 0)
1713 ("elsif" "elsif" cperl-electric-keyword 0)
1714 ("while" "while" cperl-electric-keyword 0)
1715 ("until" "until" cperl-electric-keyword 0)
1716 ("unless" "unless" cperl-electric-keyword 0)
1717 ("else" "else" cperl-electric-else 0)
1718 ("continue" "continue" cperl-electric-else 0)
1719 ("for" "for" cperl-electric-keyword 0)
1720 ("foreach" "foreach" cperl-electric-keyword 0)
1721 ("formy" "formy" cperl-electric-keyword 0)
1722 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1723 ("do" "do" cperl-electric-keyword 0)
1724 ("=pod" "=pod" cperl-electric-pod 0)
1725 ("=over" "=over" cperl-electric-pod 0)
1726 ("=head1" "=head1" cperl-electric-pod 0)
1727 ("=head2" "=head2" cperl-electric-pod 0)
1728 ("pod" "pod" cperl-electric-pod 0)
1729 ("over" "over" cperl-electric-pod 0)
1730 ("head1" "head1" cperl-electric-pod 0)
1731 ("head2" "head2" cperl-electric-pod 0)))
1732 (setq abbrevs-changed prev-a-c))
1733 (setq local-abbrev-table cperl-mode-abbrev-table)
1734 (if (cperl-val 'cperl-electric-keywords)
1735 (abbrev-mode 1))
1736 (set-syntax-table cperl-mode-syntax-table)
1737 ;; Until Emacs is multi-threaded, we do not actually need it local:
1738 (make-local-variable 'cperl-font-lock-multiline-start)
1739 (make-local-variable 'cperl-font-locking)
1740 (make-local-variable 'outline-regexp)
1741 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1742 (setq outline-regexp cperl-outline-regexp)
1743 (make-local-variable 'outline-level)
1744 (setq outline-level 'cperl-outline-level)
1745 (make-local-variable 'paragraph-start)
1746 (setq paragraph-start (concat "^$\\|" page-delimiter))
1747 (make-local-variable 'paragraph-separate)
1748 (setq paragraph-separate paragraph-start)
1749 (make-local-variable 'paragraph-ignore-fill-prefix)
1750 (setq paragraph-ignore-fill-prefix t)
1751 (if (featurep 'xemacs)
1752 (progn
1753 (make-local-variable 'paren-backwards-message)
1754 (set 'paren-backwards-message t)))
1755 (make-local-variable 'indent-line-function)
1756 (setq indent-line-function 'cperl-indent-line)
1757 (make-local-variable 'require-final-newline)
1758 (setq require-final-newline mode-require-final-newline)
1759 (make-local-variable 'comment-start)
1760 (setq comment-start "# ")
1761 (make-local-variable 'comment-end)
1762 (setq comment-end "")
1763 (make-local-variable 'comment-column)
1764 (setq comment-column cperl-comment-column)
1765 (make-local-variable 'comment-start-skip)
1766 (setq comment-start-skip "#+ *")
1767 (make-local-variable 'defun-prompt-regexp)
1768 ;;; "[ \t]*sub"
1769 ;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1770 ;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1771 (setq defun-prompt-regexp
1772 (concat "^[ \t]*\\(sub"
1773 (cperl-after-sub-regexp 'named 'attr-groups)
1774 "\\|" ; per toke.c
1775 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1776 "\\)"
1777 cperl-maybe-white-and-comment-rex))
1778 (make-local-variable 'comment-indent-function)
1779 (setq comment-indent-function 'cperl-comment-indent)
1780 (and (boundp 'fill-paragraph-function)
1781 (progn
1782 (make-local-variable 'fill-paragraph-function)
1783 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
1784 (make-local-variable 'parse-sexp-ignore-comments)
1785 (setq parse-sexp-ignore-comments t)
1786 (make-local-variable 'indent-region-function)
1787 (setq indent-region-function 'cperl-indent-region)
1788 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1789 (make-local-variable 'imenu-create-index-function)
1790 (setq imenu-create-index-function
1791 (function cperl-imenu--create-perl-index))
1792 (make-local-variable 'imenu-sort-function)
1793 (setq imenu-sort-function nil)
1794 (make-local-variable 'vc-rcs-header)
1795 (set 'vc-rcs-header cperl-vc-rcs-header)
1796 (make-local-variable 'vc-sccs-header)
1797 (set 'vc-sccs-header cperl-vc-sccs-header)
1798 ;; This one is obsolete...
1799 (make-local-variable 'vc-header-alist)
1800 (with-no-warnings
1801 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1802 `((SCCS ,(car cperl-vc-sccs-header))
1803 (RCS ,(car cperl-vc-rcs-header)))))
1804 )
1805 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1806 (make-local-variable 'compilation-error-regexp-alist-alist)
1807 (set 'compilation-error-regexp-alist-alist
1808 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
1809 (symbol-value 'compilation-error-regexp-alist-alist)))
1810 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1811 (let ((f 'compilation-build-compilation-error-regexp-alist))
1812 (funcall f))
1813 (make-local-variable 'compilation-error-regexp-alist)
1814 (push 'cperl compilation-error-regexp-alist)))
1815 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1816 (make-local-variable 'compilation-error-regexp-alist)
1817 (set 'compilation-error-regexp-alist
1818 (append cperl-compilation-error-regexp-alist
1819 (symbol-value 'compilation-error-regexp-alist)))))
1820 (make-local-variable 'font-lock-defaults)
1821 (setq font-lock-defaults
1822 (cond
1823 ((string< emacs-version "19.30")
1824 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1825 ((string< emacs-version "19.33") ; Which one to use?
1826 '((cperl-font-lock-keywords
1827 cperl-font-lock-keywords-1
1828 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1829 (t
1830 '((cperl-load-font-lock-keywords
1831 cperl-load-font-lock-keywords-1
1832 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1833 (make-local-variable 'cperl-syntax-state)
1834 (setq cperl-syntax-state nil) ; reset syntaxification cache
1835 (if cperl-use-syntax-table-text-property
1836 (progn
1837 (make-local-variable 'parse-sexp-lookup-properties)
1838 ;; Do not introduce variable if not needed, we check it!
1839 (set 'parse-sexp-lookup-properties t)
1840 ;; Fix broken font-lock:
1841 (or (boundp 'font-lock-unfontify-region-function)
1842 (set 'font-lock-unfontify-region-function
1843 'font-lock-default-unfontify-region))
1844 (unless (featurep 'xemacs) ; Our: just a plug for wrong font-lock
1845 (make-local-variable 'font-lock-unfontify-region-function)
1846 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1847 'cperl-font-lock-unfontify-region-function))
1848 (make-local-variable 'cperl-syntax-done-to)
1849 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1850 (make-local-variable 'font-lock-syntactic-keywords)
1851 (setq font-lock-syntactic-keywords
1852 (if cperl-syntaxify-by-font-lock
1853 '((cperl-fontify-syntaxically))
1854 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1855 ;; used to ignore syntax-table text-properties. (t) is a hack
1856 ;; to make font-lock think that font-lock-syntactic-keywords
1857 ;; are defined.
1858 '(t)))))
1859 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1860 (progn
1861 (setq cperl-font-lock-multiline t) ; Not localized...
1862 (set (make-local-variable 'font-lock-multiline) t))
1863 (make-local-variable 'font-lock-fontify-region-function)
1864 (set 'font-lock-fontify-region-function ; not present with old Emacs
1865 'cperl-font-lock-fontify-region-function))
1866 (make-local-variable 'font-lock-fontify-region-function)
1867 (set 'font-lock-fontify-region-function ; not present with old Emacs
1868 'cperl-font-lock-fontify-region-function)
1869 (make-local-variable 'cperl-old-style)
1870 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1871 (set (make-local-variable 'normal-auto-fill-function)
1872 'cperl-do-auto-fill)
1873 (or (fboundp 'cperl-old-auto-fill-mode)
1874 (progn
1875 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1876 (defun auto-fill-mode (&optional arg)
1877 (interactive "P")
1878 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1879 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1880 (setq auto-fill-function 'cperl-do-auto-fill))))))
1881 (if (cperl-enable-font-lock)
1882 (if (cperl-val 'cperl-font-lock)
1883 (progn (or cperl-faces-init (cperl-init-faces))
1884 (font-lock-mode 1))))
1885 (set (make-local-variable 'facemenu-add-face-function)
1886 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1887 (and (boundp 'msb-menu-cond)
1888 (not cperl-msb-fixed)
1889 (cperl-msb-fix))
1890 (if (featurep 'easymenu)
1891 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1892 (run-mode-hooks 'cperl-mode-hook)
1893 (if cperl-hook-after-change
1894 (add-hook 'after-change-functions 'cperl-after-change-function nil t))
1895 ;; After hooks since fontification will break this
1896 (if cperl-pod-here-scan
1897 (or cperl-syntaxify-by-font-lock
1898 (progn (or cperl-faces-init (cperl-init-faces-weak))
1899 (cperl-find-pods-heres)))))
1900 \f
1901 ;; Fix for perldb - make default reasonable
1902 (defun cperl-db ()
1903 (interactive)
1904 (require 'gud)
1905 (perldb (read-from-minibuffer "Run perldb (like this): "
1906 (if (consp gud-perldb-history)
1907 (car gud-perldb-history)
1908 (concat "perl " ;;(file-name-nondirectory
1909 ;; I have problems
1910 ;; in OS/2
1911 ;; otherwise
1912 (buffer-file-name)))
1913 nil nil
1914 '(gud-perldb-history . 1))))
1915 \f
1916 (defun cperl-msb-fix ()
1917 ;; Adds perl files to msb menu, supposes that msb is already loaded
1918 (setq cperl-msb-fixed t)
1919 (let* ((l (length msb-menu-cond))
1920 (last (nth (1- l) msb-menu-cond))
1921 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1922 (handle (1- (nth 1 last))))
1923 (setcdr precdr (list
1924 (list
1925 '(memq major-mode '(cperl-mode perl-mode))
1926 handle
1927 "Perl Files (%d)")
1928 last))))
1929 \f
1930 ;; This is used by indent-for-comment
1931 ;; to decide how much to indent a comment in CPerl code
1932 ;; based on its context. Do fallback if comment is found wrong.
1933
1934 (defvar cperl-wrong-comment)
1935 (defvar cperl-st-cfence '(14)) ; Comment-fence
1936 (defvar cperl-st-sfence '(15)) ; String-fence
1937 (defvar cperl-st-punct '(1))
1938 (defvar cperl-st-word '(2))
1939 (defvar cperl-st-bra '(4 . ?\>))
1940 (defvar cperl-st-ket '(5 . ?\<))
1941
1942
1943 (defun cperl-comment-indent () ; called at point at supposed comment
1944 (let ((p (point)) (c (current-column)) was phony)
1945 (if (and (not cperl-indent-comment-at-column-0)
1946 (looking-at "^#"))
1947 0 ; Existing comment at bol stays there.
1948 ;; Wrong comment found
1949 (save-excursion
1950 (setq was (cperl-to-comment-or-eol)
1951 phony (eq (get-text-property (point) 'syntax-table)
1952 cperl-st-cfence))
1953 (if phony
1954 (progn ; Too naive???
1955 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1956 (if (eq (preceding-char) ?\#)
1957 (forward-char -1))
1958 (setq was nil)))
1959 (if (= (point) p) ; Our caller found a correct place
1960 (progn
1961 (skip-chars-backward " \t")
1962 (setq was (current-column))
1963 (if (eq was 0)
1964 comment-column
1965 (max (1+ was) ; Else indent at comment column
1966 comment-column)))
1967 ;; No, the caller found a random place; we need to edit ourselves
1968 (if was nil
1969 (insert comment-start)
1970 (backward-char (length comment-start)))
1971 (setq cperl-wrong-comment t)
1972 (cperl-make-indent comment-column 1) ; Indent min 1
1973 c)))))
1974
1975 ;;;(defun cperl-comment-indent-fallback ()
1976 ;;; "Is called if the standard comment-search procedure fails.
1977 ;;;Point is at start of real comment."
1978 ;;; (let ((c (current-column)) target cnt prevc)
1979 ;;; (if (= c comment-column) nil
1980 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1981 ;;; (setq target (max (1+ (setq prevc
1982 ;;; (current-column))) ; Else indent at comment column
1983 ;;; comment-column))
1984 ;;; (if (= c comment-column) nil
1985 ;;; (delete-backward-char cnt)
1986 ;;; (while (< prevc target)
1987 ;;; (insert "\t")
1988 ;;; (setq prevc (current-column)))
1989 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1990 ;;; (while (< prevc target)
1991 ;;; (insert " ")
1992 ;;; (setq prevc (current-column)))))))
1993
1994 (defun cperl-indent-for-comment ()
1995 "Substitute for `indent-for-comment' in CPerl."
1996 (interactive)
1997 (let (cperl-wrong-comment)
1998 (indent-for-comment)
1999 (if cperl-wrong-comment ; set by `cperl-comment-indent'
2000 (progn (cperl-to-comment-or-eol)
2001 (forward-char (length comment-start))))))
2002
2003 (defun cperl-comment-region (b e arg)
2004 "Comment or uncomment each line in the region in CPerl mode.
2005 See `comment-region'."
2006 (interactive "r\np")
2007 (let ((comment-start "#"))
2008 (comment-region b e arg)))
2009
2010 (defun cperl-uncomment-region (b e arg)
2011 "Uncomment or comment each line in the region in CPerl mode.
2012 See `comment-region'."
2013 (interactive "r\np")
2014 (let ((comment-start "#"))
2015 (comment-region b e (- arg))))
2016
2017 (defvar cperl-brace-recursing nil)
2018
2019 (defun cperl-electric-brace (arg &optional only-before)
2020 "Insert character and correct line's indentation.
2021 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2022 place (even in empty line), but not after. If after \")\" and the inserted
2023 char is \"{\", insert extra newline before only if
2024 `cperl-extra-newline-before-brace'."
2025 (interactive "P")
2026 (let (insertpos
2027 (other-end (if (and cperl-electric-parens-mark
2028 (cperl-mark-active)
2029 (< (mark) (point)))
2030 (mark)
2031 nil)))
2032 (if (and other-end
2033 (not cperl-brace-recursing)
2034 (cperl-val 'cperl-electric-parens)
2035 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2036 ;; Need to insert a matching pair
2037 (progn
2038 (save-excursion
2039 (setq insertpos (point-marker))
2040 (goto-char other-end)
2041 (setq last-command-event ?\{)
2042 (cperl-electric-lbrace arg insertpos))
2043 (forward-char 1))
2044 ;; Check whether we close something "usual" with `}'
2045 (if (and (eq last-command-event ?\})
2046 (not
2047 (condition-case nil
2048 (save-excursion
2049 (up-list (- (prefix-numeric-value arg)))
2050 ;;(cperl-after-block-p (point-min))
2051 (or (cperl-after-expr-p nil "{;)")
2052 ;; after sub, else, continue
2053 (cperl-after-block-p nil 'pre)))
2054 (error nil))))
2055 ;; Just insert the guy
2056 (self-insert-command (prefix-numeric-value arg))
2057 (if (and (not arg) ; No args, end (of empty line or auto)
2058 (eolp)
2059 (or (and (null only-before)
2060 (save-excursion
2061 (skip-chars-backward " \t")
2062 (bolp)))
2063 (and (eq last-command-event ?\{) ; Do not insert newline
2064 ;; if after ")" and `cperl-extra-newline-before-brace'
2065 ;; is nil, do not insert extra newline.
2066 (not cperl-extra-newline-before-brace)
2067 (save-excursion
2068 (skip-chars-backward " \t")
2069 (eq (preceding-char) ?\))))
2070 (if cperl-auto-newline
2071 (progn (cperl-indent-line) (newline) t) nil)))
2072 (progn
2073 (self-insert-command (prefix-numeric-value arg))
2074 (cperl-indent-line)
2075 (if cperl-auto-newline
2076 (setq insertpos (1- (point))))
2077 (if (and cperl-auto-newline (null only-before))
2078 (progn
2079 (newline)
2080 (cperl-indent-line)))
2081 (save-excursion
2082 (if insertpos (progn (goto-char insertpos)
2083 (search-forward (make-string
2084 1 last-command-event))
2085 (setq insertpos (1- (point)))))
2086 (delete-char -1))))
2087 (if insertpos
2088 (save-excursion
2089 (goto-char insertpos)
2090 (self-insert-command (prefix-numeric-value arg)))
2091 (self-insert-command (prefix-numeric-value arg)))))))
2092
2093 (defun cperl-electric-lbrace (arg &optional end)
2094 "Insert character, correct line's indentation, correct quoting by space."
2095 (interactive "P")
2096 (let ((cperl-brace-recursing t)
2097 (cperl-auto-newline cperl-auto-newline)
2098 (other-end (or end
2099 (if (and cperl-electric-parens-mark
2100 (cperl-mark-active)
2101 (> (mark) (point)))
2102 (save-excursion
2103 (goto-char (mark))
2104 (point-marker))
2105 nil)))
2106 pos after)
2107 (and (cperl-val 'cperl-electric-lbrace-space)
2108 (eq (preceding-char) ?$)
2109 (save-excursion
2110 (skip-chars-backward "$")
2111 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2112 (insert ?\s))
2113 ;; Check whether we are in comment
2114 (if (and
2115 (save-excursion
2116 (beginning-of-line)
2117 (not (looking-at "[ \t]*#")))
2118 (cperl-after-expr-p nil "{;)"))
2119 nil
2120 (setq cperl-auto-newline nil))
2121 (cperl-electric-brace arg)
2122 (and (cperl-val 'cperl-electric-parens)
2123 (eq last-command-event ?{)
2124 (memq last-command-event
2125 (append cperl-electric-parens-string nil))
2126 (or (if other-end (goto-char (marker-position other-end)))
2127 t)
2128 (setq last-command-event ?} pos (point))
2129 (progn (cperl-electric-brace arg t)
2130 (goto-char pos)))))
2131
2132 (defun cperl-electric-paren (arg)
2133 "Insert an opening parenthesis or a matching pair of parentheses.
2134 See `cperl-electric-parens'."
2135 (interactive "P")
2136 (let ((beg (save-excursion (beginning-of-line) (point)))
2137 (other-end (if (and cperl-electric-parens-mark
2138 (cperl-mark-active)
2139 (> (mark) (point)))
2140 (save-excursion
2141 (goto-char (mark))
2142 (point-marker))
2143 nil)))
2144 (if (and (cperl-val 'cperl-electric-parens)
2145 (memq last-command-event
2146 (append cperl-electric-parens-string nil))
2147 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2148 ;;(not (save-excursion (search-backward "#" beg t)))
2149 (if (eq last-command-event ?<)
2150 (progn
2151 (and abbrev-mode ; later it is too late, may be after `for'
2152 (expand-abbrev))
2153 (cperl-after-expr-p nil "{;(,:="))
2154 1))
2155 (progn
2156 (self-insert-command (prefix-numeric-value arg))
2157 (if other-end (goto-char (marker-position other-end)))
2158 (insert (make-string
2159 (prefix-numeric-value arg)
2160 (cdr (assoc last-command-event '((?{ .?})
2161 (?[ . ?])
2162 (?( . ?))
2163 (?< . ?>))))))
2164 (forward-char (- (prefix-numeric-value arg))))
2165 (self-insert-command (prefix-numeric-value arg)))))
2166
2167 (defun cperl-electric-rparen (arg)
2168 "Insert a matching pair of parentheses if marking is active.
2169 If not, or if we are not at the end of marking range, would self-insert.
2170 Affected by `cperl-electric-parens'."
2171 (interactive "P")
2172 (let ((beg (save-excursion (beginning-of-line) (point)))
2173 (other-end (if (and cperl-electric-parens-mark
2174 (cperl-val 'cperl-electric-parens)
2175 (memq last-command-event
2176 (append cperl-electric-parens-string nil))
2177 (cperl-mark-active)
2178 (< (mark) (point)))
2179 (mark)
2180 nil))
2181 p)
2182 (if (and other-end
2183 (cperl-val 'cperl-electric-parens)
2184 (memq last-command-event '( ?\) ?\] ?\} ?\> ))
2185 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2186 ;;(not (save-excursion (search-backward "#" beg t)))
2187 )
2188 (progn
2189 (self-insert-command (prefix-numeric-value arg))
2190 (setq p (point))
2191 (if other-end (goto-char other-end))
2192 (insert (make-string
2193 (prefix-numeric-value arg)
2194 (cdr (assoc last-command-event '((?\} . ?\{)
2195 (?\] . ?\[)
2196 (?\) . ?\()
2197 (?\> . ?\<))))))
2198 (goto-char (1+ p)))
2199 (self-insert-command (prefix-numeric-value arg)))))
2200
2201 (defun cperl-electric-keyword ()
2202 "Insert a construction appropriate after a keyword.
2203 Help message may be switched off by setting `cperl-message-electric-keyword'
2204 to nil."
2205 (let ((beg (save-excursion (beginning-of-line) (point)))
2206 (dollar (and (eq last-command-event ?$)
2207 (eq this-command 'self-insert-command)))
2208 (delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2209 (memq this-command '(self-insert-command newline))))
2210 my do)
2211 (and (save-excursion
2212 (condition-case nil
2213 (progn
2214 (backward-sexp 1)
2215 (setq do (looking-at "do\\>")))
2216 (error nil))
2217 (cperl-after-expr-p nil "{;:"))
2218 (save-excursion
2219 (not
2220 (re-search-backward
2221 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2222 beg t)))
2223 (save-excursion (or (not (re-search-backward "^=" nil t))
2224 (or
2225 (looking-at "=cut")
2226 (and cperl-use-syntax-table-text-property
2227 (not (eq (get-text-property (point)
2228 'syntax-type)
2229 'pod))))))
2230 (save-excursion (forward-sexp -1)
2231 (not (memq (following-char) (append "$@%&*" nil))))
2232 (progn
2233 (and (eq (preceding-char) ?y)
2234 (progn ; "foreachmy"
2235 (forward-char -2)
2236 (insert " ")
2237 (forward-char 2)
2238 (setq my t dollar t
2239 delete
2240 (memq this-command '(self-insert-command newline)))))
2241 (and dollar (insert " $"))
2242 (cperl-indent-line)
2243 ;;(insert " () {\n}")
2244 (cond
2245 (cperl-extra-newline-before-brace
2246 (insert (if do "\n" " ()\n"))
2247 (insert "{")
2248 (cperl-indent-line)
2249 (insert "\n")
2250 (cperl-indent-line)
2251 (insert "\n}")
2252 (and do (insert " while ();")))
2253 (t
2254 (insert (if do " {\n} while ();" " () {\n}"))))
2255 (or (looking-at "[ \t]\\|$") (insert " "))
2256 (cperl-indent-line)
2257 (if dollar (progn (search-backward "$")
2258 (if my
2259 (forward-char 1)
2260 (delete-char 1)))
2261 (search-backward ")")
2262 (if (eq last-command-event ?\()
2263 (progn ; Avoid "if (())"
2264 (delete-backward-char 1)
2265 (delete-backward-char -1))))
2266 (if delete
2267 (cperl-putback-char cperl-del-back-ch))
2268 (if cperl-message-electric-keyword
2269 (message "Precede char by C-q to avoid expansion"))))))
2270
2271 (defun cperl-ensure-newlines (n &optional pos)
2272 "Make sure there are N newlines after the point."
2273 (or pos (setq pos (point)))
2274 (if (looking-at "\n")
2275 (forward-char 1)
2276 (insert "\n"))
2277 (if (> n 1)
2278 (cperl-ensure-newlines (1- n) pos)
2279 (goto-char pos)))
2280
2281 (defun cperl-electric-pod ()
2282 "Insert a POD chunk appropriate after a =POD directive."
2283 (let ((delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2284 (memq this-command '(self-insert-command newline))))
2285 head1 notlast name p really-delete over)
2286 (and (save-excursion
2287 (forward-word -1)
2288 (and
2289 (eq (preceding-char) ?=)
2290 (progn
2291 (setq head1 (looking-at "head1\\>[ \t]*$"))
2292 (setq over (and (looking-at "over\\>[ \t]*$")
2293 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2294 (forward-char -1)
2295 (bolp))
2296 (or
2297 (get-text-property (point) 'in-pod)
2298 (cperl-after-expr-p nil "{;:")
2299 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2300 (not (looking-at "\n*=cut"))
2301 (or (not cperl-use-syntax-table-text-property)
2302 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2303 (progn
2304 (save-excursion
2305 (setq notlast (re-search-forward "^\n=" nil t)))
2306 (or notlast
2307 (progn
2308 (insert "\n\n=cut")
2309 (cperl-ensure-newlines 2)
2310 (forward-word -2)
2311 (if (and head1
2312 (not
2313 (save-excursion
2314 (forward-char -1)
2315 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2316 nil t)))) ; Only one
2317 (progn
2318 (forward-word 1)
2319 (setq name (file-name-sans-extension
2320 (file-name-nondirectory (buffer-file-name)))
2321 p (point))
2322 (insert " NAME\n\n" name
2323 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2324 "=head1 DESCRIPTION")
2325 (cperl-ensure-newlines 4)
2326 (goto-char p)
2327 (forward-word 2)
2328 (end-of-line)
2329 (setq really-delete t))
2330 (forward-word 1))))
2331 (if over
2332 (progn
2333 (setq p (point))
2334 (insert "\n\n=item \n\n\n\n"
2335 "=back")
2336 (cperl-ensure-newlines 2)
2337 (goto-char p)
2338 (forward-word 1)
2339 (end-of-line)
2340 (setq really-delete t)))
2341 (if (and delete really-delete)
2342 (cperl-putback-char cperl-del-back-ch))))))
2343
2344 (defun cperl-electric-else ()
2345 "Insert a construction appropriate after a keyword.
2346 Help message may be switched off by setting `cperl-message-electric-keyword'
2347 to nil."
2348 (let ((beg (save-excursion (beginning-of-line) (point))))
2349 (and (save-excursion
2350 (backward-sexp 1)
2351 (cperl-after-expr-p nil "{;:"))
2352 (save-excursion
2353 (not
2354 (re-search-backward
2355 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2356 beg t)))
2357 (save-excursion (or (not (re-search-backward "^=" nil t))
2358 (looking-at "=cut")
2359 (and cperl-use-syntax-table-text-property
2360 (not (eq (get-text-property (point)
2361 'syntax-type)
2362 'pod)))))
2363 (progn
2364 (cperl-indent-line)
2365 ;;(insert " {\n\n}")
2366 (cond
2367 (cperl-extra-newline-before-brace
2368 (insert "\n")
2369 (insert "{")
2370 (cperl-indent-line)
2371 (insert "\n\n}"))
2372 (t
2373 (insert " {\n\n}")))
2374 (or (looking-at "[ \t]\\|$") (insert " "))
2375 (cperl-indent-line)
2376 (forward-line -1)
2377 (cperl-indent-line)
2378 (cperl-putback-char cperl-del-back-ch)
2379 (setq this-command 'cperl-electric-else)
2380 (if cperl-message-electric-keyword
2381 (message "Precede char by C-q to avoid expansion"))))))
2382
2383 (defun cperl-linefeed ()
2384 "Go to end of line, open a new line and indent appropriately.
2385 If in POD, insert appropriate lines."
2386 (interactive)
2387 (let ((beg (save-excursion (beginning-of-line) (point)))
2388 (end (save-excursion (end-of-line) (point)))
2389 (pos (point)) start over cut res)
2390 (if (and ; Check if we need to split:
2391 ; i.e., on a boundary and inside "{...}"
2392 (save-excursion (cperl-to-comment-or-eol)
2393 (>= (point) pos)) ; Not in a comment
2394 (or (save-excursion
2395 (skip-chars-backward " \t" beg)
2396 (forward-char -1)
2397 (looking-at "[;{]")) ; After { or ; + spaces
2398 (looking-at "[ \t]*}") ; Before }
2399 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2400 (save-excursion
2401 (and
2402 (eq (car (parse-partial-sexp pos end -1)) -1)
2403 ; Leave the level of parens
2404 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2405 ; Are at end
2406 (cperl-after-block-p (point-min))
2407 (progn
2408 (backward-sexp 1)
2409 (setq start (point-marker))
2410 (<= start pos))))) ; Redundant? Are after the
2411 ; start of parens group.
2412 (progn
2413 (skip-chars-backward " \t")
2414 (or (memq (preceding-char) (append ";{" nil))
2415 (insert ";"))
2416 (insert "\n")
2417 (forward-line -1)
2418 (cperl-indent-line)
2419 (goto-char start)
2420 (or (looking-at "{[ \t]*$") ; If there is a statement
2421 ; before, move it to separate line
2422 (progn
2423 (forward-char 1)
2424 (insert "\n")
2425 (cperl-indent-line)))
2426 (forward-line 1) ; We are on the target line
2427 (cperl-indent-line)
2428 (beginning-of-line)
2429 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2430 ; after, move it to separate line
2431 (progn
2432 (end-of-line)
2433 (search-backward "}" beg)
2434 (skip-chars-backward " \t")
2435 (or (memq (preceding-char) (append ";{" nil))
2436 (insert ";"))
2437 (insert "\n")
2438 (cperl-indent-line)
2439 (forward-line -1)))
2440 (forward-line -1) ; We are on the line before target
2441 (end-of-line)
2442 (newline-and-indent))
2443 (end-of-line) ; else - no splitting
2444 (cond
2445 ((and (looking-at "\n[ \t]*{$")
2446 (save-excursion
2447 (skip-chars-backward " \t")
2448 (eq (preceding-char) ?\)))) ; Probably if () {} group
2449 ; with an extra newline.
2450 (forward-line 2)
2451 (cperl-indent-line))
2452 ((save-excursion ; In POD header
2453 (forward-paragraph -1)
2454 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2455 ;; We are after \n now, so look for the rest
2456 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2457 (progn
2458 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2459 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2460 t)))
2461 (if (and over
2462 (progn
2463 (forward-paragraph -1)
2464 (forward-word 1)
2465 (setq pos (point))
2466 (setq cut (buffer-substring (point)
2467 (save-excursion
2468 (end-of-line)
2469 (point))))
2470 (delete-char (- (save-excursion (end-of-line) (point))
2471 (point)))
2472 (setq res (expand-abbrev))
2473 (save-excursion
2474 (goto-char pos)
2475 (insert cut))
2476 res))
2477 nil
2478 (cperl-ensure-newlines (if cut 2 4))
2479 (forward-line 2)))
2480 ((get-text-property (point) 'in-pod) ; In POD section
2481 (cperl-ensure-newlines 4)
2482 (forward-line 2))
2483 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2484 (forward-line 1)
2485 (cperl-indent-line))
2486 (t
2487 (newline-and-indent))))))
2488
2489 (defun cperl-electric-semi (arg)
2490 "Insert character and correct line's indentation."
2491 (interactive "P")
2492 (if cperl-auto-newline
2493 (cperl-electric-terminator arg)
2494 (self-insert-command (prefix-numeric-value arg))
2495 (if cperl-autoindent-on-semi
2496 (cperl-indent-line))))
2497
2498 (defun cperl-electric-terminator (arg)
2499 "Insert character and correct line's indentation."
2500 (interactive "P")
2501 (let ((end (point))
2502 (auto (and cperl-auto-newline
2503 (or (not (eq last-command-event ?:))
2504 cperl-auto-newline-after-colon)))
2505 insertpos)
2506 (if (and ;;(not arg)
2507 (eolp)
2508 (not (save-excursion
2509 (beginning-of-line)
2510 (skip-chars-forward " \t")
2511 (or
2512 ;; Ignore in comment lines
2513 (= (following-char) ?#)
2514 ;; Colon is special only after a label
2515 ;; So quickly rule out most other uses of colon
2516 ;; and do no indentation for them.
2517 (and (eq last-command-event ?:)
2518 (save-excursion
2519 (forward-word 1)
2520 (skip-chars-forward " \t")
2521 (and (< (point) end)
2522 (progn (goto-char (- end 1))
2523 (not (looking-at ":"))))))
2524 (progn
2525 (beginning-of-defun)
2526 (let ((pps (parse-partial-sexp (point) end)))
2527 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2528 (progn
2529 (self-insert-command (prefix-numeric-value arg))
2530 ;;(forward-char -1)
2531 (if auto (setq insertpos (point-marker)))
2532 ;;(forward-char 1)
2533 (cperl-indent-line)
2534 (if auto
2535 (progn
2536 (newline)
2537 (cperl-indent-line)))
2538 (save-excursion
2539 (if insertpos (goto-char (1- (marker-position insertpos)))
2540 (forward-char -1))
2541 (delete-char 1))))
2542 (if insertpos
2543 (save-excursion
2544 (goto-char insertpos)
2545 (self-insert-command (prefix-numeric-value arg)))
2546 (self-insert-command (prefix-numeric-value arg)))))
2547
2548 (defun cperl-electric-backspace (arg)
2549 "Backspace, or remove the whitespace around the point inserted by an electric
2550 key. Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2551 (interactive "p")
2552 (if (and cperl-auto-newline
2553 (memq last-command '(cperl-electric-semi
2554 cperl-electric-terminator
2555 cperl-electric-lbrace))
2556 (memq (preceding-char) '(?\s ?\t ?\n)))
2557 (let (p)
2558 (if (eq last-command 'cperl-electric-lbrace)
2559 (skip-chars-forward " \t\n"))
2560 (setq p (point))
2561 (skip-chars-backward " \t\n")
2562 (delete-region (point) p))
2563 (and (eq last-command 'cperl-electric-else)
2564 ;; We are removing the whitespace *inside* cperl-electric-else
2565 (setq this-command 'cperl-electric-else-really))
2566 (if (and cperl-auto-newline
2567 (eq last-command 'cperl-electric-else-really)
2568 (memq (preceding-char) '(?\s ?\t ?\n)))
2569 (let (p)
2570 (skip-chars-forward " \t\n")
2571 (setq p (point))
2572 (skip-chars-backward " \t\n")
2573 (delete-region (point) p))
2574 (if cperl-electric-backspace-untabify
2575 (backward-delete-char-untabify arg)
2576 (delete-backward-char arg)))))
2577
2578 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2579
2580 (defun cperl-inside-parens-p () ;; NOT USED????
2581 (condition-case ()
2582 (save-excursion
2583 (save-restriction
2584 (narrow-to-region (point)
2585 (progn (beginning-of-defun) (point)))
2586 (goto-char (point-max))
2587 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2588 (error nil)))
2589 \f
2590 (defun cperl-indent-command (&optional whole-exp)
2591 "Indent current line as Perl code, or in some cases insert a tab character.
2592 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2593 line. Otherwise, indent the current line only if point is at the left margin
2594 or in the line's indentation; otherwise insert a tab.
2595
2596 A numeric argument, regardless of its value,
2597 means indent rigidly all the lines of the expression starting after point
2598 so that this line becomes properly indented.
2599 The relative indentation among the lines of the expression are preserved."
2600 (interactive "P")
2601 (cperl-update-syntaxification (point) (point))
2602 (if whole-exp
2603 ;; If arg, always indent this line as Perl
2604 ;; and shift remaining lines of expression the same amount.
2605 (let ((shift-amt (cperl-indent-line))
2606 beg end)
2607 (save-excursion
2608 (if cperl-tab-always-indent
2609 (beginning-of-line))
2610 (setq beg (point))
2611 (forward-sexp 1)
2612 (setq end (point))
2613 (goto-char beg)
2614 (forward-line 1)
2615 (setq beg (point)))
2616 (if (and shift-amt (> end beg))
2617 (indent-code-rigidly beg end shift-amt "#")))
2618 (if (and (not cperl-tab-always-indent)
2619 (save-excursion
2620 (skip-chars-backward " \t")
2621 (not (bolp))))
2622 (insert-tab)
2623 (cperl-indent-line))))
2624
2625 (defun cperl-indent-line (&optional parse-data)
2626 "Indent current line as Perl code.
2627 Return the amount the indentation changed by."
2628 (let ((case-fold-search nil)
2629 (pos (- (point-max) (point)))
2630 indent i beg shift-amt)
2631 (setq indent (cperl-calculate-indent parse-data)
2632 i indent)
2633 (beginning-of-line)
2634 (setq beg (point))
2635 (cond ((or (eq indent nil) (eq indent t))
2636 (setq indent (current-indentation) i nil))
2637 ;;((eq indent t) ; Never?
2638 ;; (setq indent (cperl-calculate-indent-within-comment)))
2639 ;;((looking-at "[ \t]*#")
2640 ;; (setq indent 0))
2641 (t
2642 (skip-chars-forward " \t")
2643 (if (listp indent) (setq indent (car indent)))
2644 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2645 (not (looking-at "[smy]:\\|tr:")))
2646 (and (> indent 0)
2647 (setq indent (max cperl-min-label-indent
2648 (+ indent cperl-label-offset)))))
2649 ((= (following-char) ?})
2650 (setq indent (- indent cperl-indent-level)))
2651 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2652 (setq indent (+ indent cperl-close-paren-offset)))
2653 ((= (following-char) ?{)
2654 (setq indent (+ indent cperl-brace-offset))))))
2655 (skip-chars-forward " \t")
2656 (setq shift-amt (and i (- indent (current-column))))
2657 (if (or (not shift-amt)
2658 (zerop shift-amt))
2659 (if (> (- (point-max) pos) (point))
2660 (goto-char (- (point-max) pos)))
2661 ;;;(delete-region beg (point))
2662 ;;;(indent-to indent)
2663 (cperl-make-indent indent)
2664 ;; If initial point was within line's indentation,
2665 ;; position after the indentation. Else stay at same point in text.
2666 (if (> (- (point-max) pos) (point))
2667 (goto-char (- (point-max) pos))))
2668 shift-amt))
2669
2670 (defun cperl-after-label ()
2671 ;; Returns true if the point is after label. Does not do save-excursion.
2672 (and (eq (preceding-char) ?:)
2673 (memq (char-syntax (char-after (- (point) 2)))
2674 '(?w ?_))
2675 (progn
2676 (backward-sexp)
2677 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2678
2679 (defun cperl-get-state (&optional parse-start start-state)
2680 ;; returns list (START STATE DEPTH PRESTART),
2681 ;; START is a good place to start parsing, or equal to
2682 ;; PARSE-START if preset,
2683 ;; STATE is what is returned by `parse-partial-sexp'.
2684 ;; DEPTH is true is we are immediately after end of block
2685 ;; which contains START.
2686 ;; PRESTART is the position basing on which START was found.
2687 (save-excursion
2688 (let ((start-point (point)) depth state start prestart)
2689 (if (and parse-start
2690 (<= parse-start start-point))
2691 (goto-char parse-start)
2692 (beginning-of-defun)
2693 (setq start-state nil))
2694 (setq prestart (point))
2695 (if start-state nil
2696 ;; Try to go out, if sub is not on the outermost level
2697 (while (< (point) start-point)
2698 (setq start (point) parse-start start depth nil
2699 state (parse-partial-sexp start start-point -1))
2700 (if (> (car state) -1) nil
2701 ;; The current line could start like }}}, so the indentation
2702 ;; corresponds to a different level than what we reached
2703 (setq depth t)
2704 (beginning-of-line 2))) ; Go to the next line.
2705 (if start (goto-char start))) ; Not at the start of file
2706 (setq start (point))
2707 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2708 (list start state depth prestart))))
2709
2710 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2711
2712 (defun cperl-beginning-of-property (p prop &optional lim)
2713 "Given that P has a property PROP, find where the property starts.
2714 Will not look before LIM."
2715 ;;; XXXX What to do at point-max???
2716 (or (previous-single-property-change (cperl-1+ p) prop lim)
2717 (point-min))
2718 ;;; (cond ((eq p (point-min))
2719 ;;; p)
2720 ;;; ((and lim (<= p lim))
2721 ;;; p)
2722 ;;; ((not (get-text-property (1- p) prop))
2723 ;;; p)
2724 ;;; (t (or (previous-single-property-change p look-prop lim)
2725 ;;; (point-min))))
2726 )
2727
2728 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2729 ;; Old workhorse for calculation of indentation; the major problem
2730 ;; is that it mixes the sniffer logic to understand what the current line
2731 ;; MEANS with the logic to actually calculate where to indent it.
2732 ;; The latter part should be eventually moved to `cperl-calculate-indent';
2733 ;; actually, this is mostly done now...
2734 (cperl-update-syntaxification (point) (point))
2735 (let ((res (get-text-property (point) 'syntax-type)))
2736 (save-excursion
2737 (cond
2738 ((and (memq res '(pod here-doc here-doc-delim format))
2739 (not (get-text-property (point) 'indentable)))
2740 (vector res))
2741 ;; before start of POD - whitespace found since do not have 'pod!
2742 ((looking-at "[ \t]*\n=")
2743 (error "Spaces before POD section!"))
2744 ((and (not cperl-indent-left-aligned-comments)
2745 (looking-at "^#"))
2746 [comment-special:at-beginning-of-line])
2747 ((get-text-property (point) 'in-pod)
2748 [in-pod])
2749 (t
2750 (beginning-of-line)
2751 (let* ((indent-point (point))
2752 (char-after-pos (save-excursion
2753 (skip-chars-forward " \t")
2754 (point)))
2755 (char-after (char-after char-after-pos))
2756 (pre-indent-point (point))
2757 p prop look-prop is-block delim)
2758 (save-excursion ; Know we are not in POD, find appropriate pos before
2759 (cperl-backward-to-noncomment nil)
2760 (setq p (max (point-min) (1- (point)))
2761 prop (get-text-property p 'syntax-type)
2762 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2763 'syntax-type))
2764 (if (memq prop '(pod here-doc format here-doc-delim))
2765 (progn
2766 (goto-char (cperl-beginning-of-property p look-prop))
2767 (beginning-of-line)
2768 (setq pre-indent-point (point)))))
2769 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2770 (let* ((case-fold-search nil)
2771 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2772 (start (or (nth 2 parse-data) ; last complete sexp terminated
2773 (nth 0 s-s))) ; Good place to start parsing
2774 (state (nth 1 s-s))
2775 (containing-sexp (car (cdr state)))
2776 old-indent)
2777 (if (and
2778 ;;containing-sexp ;; We are buggy at toplevel :-(
2779 parse-data)
2780 (progn
2781 (setcar parse-data pre-indent-point)
2782 (setcar (cdr parse-data) state)
2783 (or (nth 2 parse-data)
2784 (setcar (cddr parse-data) start))
2785 ;; Before this point: end of statement
2786 (setq old-indent (nth 3 parse-data))))
2787 (cond ((get-text-property (point) 'indentable)
2788 ;; indent to "after" the surrounding open
2789 ;; (same offset as `cperl-beautify-regexp-piece'),
2790 ;; skip blanks if we do not close the expression.
2791 (setq delim ; We do not close the expression
2792 (get-text-property
2793 (cperl-1+ char-after-pos) 'indentable)
2794 p (1+ (cperl-beginning-of-property
2795 (point) 'indentable))
2796 is-block ; misused for: preceeding line in REx
2797 (save-excursion ; Find preceeding line
2798 (cperl-backward-to-noncomment p)
2799 (beginning-of-line)
2800 (if (<= (point) p)
2801 (progn ; get indent from the first line
2802 (goto-char p)
2803 (skip-chars-forward " \t")
2804 (if (memq (char-after (point))
2805 (append "#\n" nil))
2806 nil ; Can't use intentation of this line...
2807 (point)))
2808 (skip-chars-forward " \t")
2809 (point)))
2810 prop (parse-partial-sexp p char-after-pos))
2811 (cond ((not delim) ; End the REx, ignore is-block
2812 (vector 'indentable 'terminator p is-block))
2813 (is-block ; Indent w.r.t. preceeding line
2814 (vector 'indentable 'cont-line char-after-pos
2815 is-block char-after p))
2816 (t ; No preceeding line...
2817 (vector 'indentable 'first-line p))))
2818 ((get-text-property char-after-pos 'REx-part2)
2819 (vector 'REx-part2 (point)))
2820 ((nth 4 state)
2821 [comment])
2822 ((nth 3 state)
2823 [string])
2824 ;; XXXX Do we need to special-case this?
2825 ((null containing-sexp)
2826 ;; Line is at top level. May be data or function definition,
2827 ;; or may be function argument declaration.
2828 ;; Indent like the previous top level line
2829 ;; unless that ends in a closeparen without semicolon,
2830 ;; in which case this line is the first argument decl.
2831 (skip-chars-forward " \t")
2832 (cperl-backward-to-noncomment (or old-indent (point-min)))
2833 (setq state
2834 (or (bobp)
2835 (eq (point) old-indent) ; old-indent was at comment
2836 (eq (preceding-char) ?\;)
2837 ;; Had ?\) too
2838 (and (eq (preceding-char) ?\})
2839 (cperl-after-block-and-statement-beg
2840 (point-min))) ; Was start - too close
2841 (memq char-after (append ")]}" nil))
2842 (and (eq (preceding-char) ?\:) ; label
2843 (progn
2844 (forward-sexp -1)
2845 (skip-chars-backward " \t")
2846 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2847 (get-text-property (point) 'first-format-line)))
2848
2849 ;; Look at previous line that's at column 0
2850 ;; to determine whether we are in top-level decls
2851 ;; or function's arg decls. Set basic-indent accordingly.
2852 ;; Now add a little if this is a continuation line.
2853 (and state
2854 parse-data
2855 (not (eq char-after ?\C-j))
2856 (setcdr (cddr parse-data)
2857 (list pre-indent-point)))
2858 (vector 'toplevel start char-after state (nth 2 s-s)))
2859 ((not
2860 (or (setq is-block
2861 (and (setq delim (= (char-after containing-sexp) ?{))
2862 (save-excursion ; Is it a hash?
2863 (goto-char containing-sexp)
2864 (cperl-block-p))))
2865 cperl-indent-parens-as-block))
2866 ;; group is an expression, not a block:
2867 ;; indent to just after the surrounding open parens,
2868 ;; skip blanks if we do not close the expression.
2869 (goto-char (1+ containing-sexp))
2870 (or (memq char-after
2871 (append (if delim "}" ")]}") nil))
2872 (looking-at "[ \t]*\\(#\\|$\\)")
2873 (skip-chars-forward " \t"))
2874 (setq old-indent (point)) ; delim=is-brace
2875 (vector 'in-parens char-after (point) delim containing-sexp))
2876 (t
2877 ;; Statement level. Is it a continuation or a new statement?
2878 ;; Find previous non-comment character.
2879 (goto-char pre-indent-point) ; Skip one level of POD/etc
2880 (cperl-backward-to-noncomment containing-sexp)
2881 ;; Back up over label lines, since they don't
2882 ;; affect whether our line is a continuation.
2883 ;; (Had \, too)
2884 (while;;(or (eq (preceding-char) ?\,)
2885 (and (eq (preceding-char) ?:)
2886 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2887 (memq (char-syntax (char-after (- (point) 2)))
2888 '(?w ?_))))
2889 ;;)
2890 ;; This is always FALSE?
2891 (if (eq (preceding-char) ?\,)
2892 ;; Will go to beginning of line, essentially.
2893 ;; Will ignore embedded sexpr XXXX.
2894 (cperl-backward-to-start-of-continued-exp containing-sexp))
2895 (beginning-of-line)
2896 (cperl-backward-to-noncomment containing-sexp))
2897 ;; Now we get non-label preceeding the indent point
2898 (if (not (or (eq (1- (point)) containing-sexp)
2899 (memq (preceding-char)
2900 (append (if is-block " ;{" " ,;{") '(nil)))
2901 (and (eq (preceding-char) ?\})
2902 (cperl-after-block-and-statement-beg
2903 containing-sexp))
2904 (get-text-property (point) 'first-format-line)))
2905 ;; This line is continuation of preceding line's statement;
2906 ;; indent `cperl-continued-statement-offset' more than the
2907 ;; previous line of the statement.
2908 ;;
2909 ;; There might be a label on this line, just
2910 ;; consider it bad style and ignore it.
2911 (progn
2912 (cperl-backward-to-start-of-continued-exp containing-sexp)
2913 (vector 'continuation (point) char-after is-block delim))
2914 ;; This line starts a new statement.
2915 ;; Position following last unclosed open brace
2916 (goto-char containing-sexp)
2917 ;; Is line first statement after an open-brace?
2918 (or
2919 ;; If no, find that first statement and indent like
2920 ;; it. If the first statement begins with label, do
2921 ;; not believe when the indentation of the label is too
2922 ;; small.
2923 (save-excursion
2924 (forward-char 1)
2925 (let ((colon-line-end 0))
2926 (while
2927 (progn (skip-chars-forward " \t\n")
2928 ;; s: foo : bar :x is NOT label
2929 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2930 (not (looking-at "[sym]:\\|tr:"))))
2931 ;; Skip over comments and labels following openbrace.
2932 (cond ((= (following-char) ?\#)
2933 (forward-line 1))
2934 ((= (following-char) ?\=)
2935 (goto-char
2936 (or (next-single-property-change (point) 'in-pod)
2937 (point-max)))) ; do not loop if no syntaxification
2938 ;; label:
2939 (t
2940 (save-excursion (end-of-line)
2941 (setq colon-line-end (point)))
2942 (search-forward ":"))))
2943 ;; We are at beginning of code (NOT label or comment)
2944 ;; First, the following code counts
2945 ;; if it is before the line we want to indent.
2946 (and (< (point) indent-point)
2947 (vector 'have-prev-sibling (point) colon-line-end
2948 containing-sexp))))
2949 (progn
2950 ;; If no previous statement,
2951 ;; indent it relative to line brace is on.
2952
2953 ;; For open-braces not the first thing in a line,
2954 ;; add in cperl-brace-imaginary-offset.
2955
2956 ;; If first thing on a line: ?????
2957 ;; Move back over whitespace before the openbrace.
2958 (setq ; brace first thing on a line
2959 old-indent (progn (skip-chars-backward " \t") (bolp)))
2960 ;; Should we indent w.r.t. earlier than start?
2961 ;; Move to start of control group, possibly on a different line
2962 (or cperl-indent-wrt-brace
2963 (cperl-backward-to-noncomment (point-min)))
2964 ;; If the openbrace is preceded by a parenthesized exp,
2965 ;; move to the beginning of that;
2966 (if (eq (preceding-char) ?\))
2967 (progn
2968 (forward-sexp -1)
2969 (cperl-backward-to-noncomment (point-min))))
2970 ;; In the case it starts a subroutine, indent with
2971 ;; respect to `sub', not with respect to the
2972 ;; first thing on the line, say in the case of
2973 ;; anonymous sub in a hash.
2974 (if (and;; Is it a sub in group starting on this line?
2975 (cond ((get-text-property (point) 'attrib-group)
2976 (goto-char (cperl-beginning-of-property
2977 (point) 'attrib-group)))
2978 ((eq (preceding-char) ?b)
2979 (forward-sexp -1)
2980 (looking-at "sub\\>")))
2981 (setq p (nth 1 ; start of innermost containing list
2982 (parse-partial-sexp
2983 (save-excursion (beginning-of-line)
2984 (point))
2985 (point)))))
2986 (progn
2987 (goto-char (1+ p)) ; enclosing block on the same line
2988 (skip-chars-forward " \t")
2989 (vector 'code-start-in-block containing-sexp char-after
2990 (and delim (not is-block)) ; is a HASH
2991 old-indent ; brace first thing on a line
2992 t (point) ; have something before...
2993 )
2994 ;;(current-column)
2995 )
2996 ;; Get initial indentation of the line we are on.
2997 ;; If line starts with label, calculate label indentation
2998 (vector 'code-start-in-block containing-sexp char-after
2999 (and delim (not is-block)) ; is a HASH
3000 old-indent ; brace first thing on a line
3001 nil (point))))))))))))))) ; nothing interesting before
3002
3003 (defvar cperl-indent-rules-alist
3004 '((pod nil) ; via `syntax-type' property
3005 (here-doc nil) ; via `syntax-type' property
3006 (here-doc-delim nil) ; via `syntax-type' property
3007 (format nil) ; via `syntax-type' property
3008 (in-pod nil) ; via `in-pod' property
3009 (comment-special:at-beginning-of-line nil)
3010 (string t)
3011 (comment nil))
3012 "Alist of indentation rules for CPerl mode.
3013 The values mean:
3014 nil: do not indent;
3015 number: add this amount of indentation.")
3016
3017 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3018 "Return appropriate indentation for current line as Perl code.
3019 In usual case returns an integer: the column to indent to.
3020 Returns nil if line starts inside a string, t if in a comment.
3021
3022 Will not correct the indentation for labels, but will correct it for braces
3023 and closing parentheses and brackets."
3024 ;; This code is still a broken architecture: in some cases we need to
3025 ;; compensate for some modifications which `cperl-indent-line' will add later
3026 (save-excursion
3027 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3028 (cond
3029 ;;((or (null i) (eq i t) (numberp i))
3030 ;; i)
3031 ((vectorp i)
3032 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3033 (cond
3034 (what (cadr what)) ; Load from table
3035 ;;
3036 ;; Indenters for regular expressions with //x and qw()
3037 ;;
3038 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3039 (goto-char (elt i 1))
3040 (condition-case nil ; Use indentation of the 1st part
3041 (forward-sexp -1))
3042 (current-column))
3043 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3044 (cond ;;; [indentable terminator start-pos is-block]
3045 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3046 (goto-char (elt i 2)) ; After opening parens
3047 (1- (current-column)))
3048 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3049 (goto-char (elt i 2))
3050 (+ (or cperl-regexp-indent-step cperl-indent-level)
3051 -1
3052 (current-column)))
3053 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3054 ;; Indent as the level after closing parens
3055 (goto-char (elt i 2)) ; indent line
3056 (skip-chars-forward " \t)") ; Skip closing parens
3057 (setq p (point))
3058 (goto-char (elt i 3)) ; previous line
3059 (skip-chars-forward " \t)") ; Skip closing parens
3060 ;; Number of parens in between:
3061 (setq p (nth 0 (parse-partial-sexp (point) p))
3062 what (elt i 4)) ; First char on current line
3063 (goto-char (elt i 3)) ; previous line
3064 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3065 (cond ((eq what ?\) )
3066 (- cperl-close-paren-offset)) ; compensate
3067 ((eq what ?\| )
3068 (- (or cperl-regexp-indent-step cperl-indent-level)))
3069 (t 0))
3070 (if (eq (following-char) ?\| )
3071 (or cperl-regexp-indent-step cperl-indent-level)
3072 0)
3073 (current-column)))
3074 (t
3075 (error "Unrecognized value of indent: %s" i))))
3076 ;;
3077 ;; Indenter for stuff at toplevel
3078 ;;
3079 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3080 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3081 (goto-char (elt i 1)) ; start = Good place to start parsing
3082 (- (current-indentation) ;
3083 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3084 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3085 ;; Look at previous line that's at column 0
3086 ;; to determine whether we are in top-level decls
3087 ;; or function's arg decls. Set basic-indent accordingly.
3088 ;; Now add a little if this is a continuation line.
3089 (if (elt i 3) ; state (XXX What is the semantic???)
3090 0
3091 cperl-continued-statement-offset)))
3092 ;;
3093 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3094 ;;
3095 ((eq 'in-parens (elt i 0))
3096 ;; in-parens char-after old-indent-point is-brace containing-sexp
3097
3098 ;; group is an expression, not a block:
3099 ;; indent to just after the surrounding open parens,
3100 ;; skip blanks if we do not close the expression.
3101 (+ (progn
3102 (goto-char (elt i 2)) ; old-indent-point
3103 (current-column))
3104 (if (and (elt i 3) ; is-brace
3105 (eq (elt i 1) ?\})) ; char-after
3106 ;; Correct indentation of trailing ?\}
3107 (+ cperl-indent-level cperl-close-paren-offset)
3108 0)))
3109 ;;
3110 ;; Indenter for continuation lines
3111 ;;
3112 ((eq 'continuation (elt i 0))
3113 ;; [continuation statement-start char-after is-block is-brace]
3114 (goto-char (elt i 1)) ; statement-start
3115 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3116 0 ; Closing parenth
3117 cperl-continued-statement-offset)
3118 (if (or (elt i 3) ; is-block
3119 (not (elt i 4)) ; is-brace
3120 (not (eq (elt i 2) ?\}))) ; char-after
3121 0
3122 ;; Now it is a hash reference
3123 (+ cperl-indent-level cperl-close-paren-offset))
3124 ;; Labels do not take :: ...
3125 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3126 (if (> (current-indentation) cperl-min-label-indent)
3127 (- (current-indentation) cperl-label-offset)
3128 ;; Do not move `parse-data', this should
3129 ;; be quick anyway (this comment comes
3130 ;; from different location):
3131 (cperl-calculate-indent))
3132 (current-column))
3133 (if (eq (elt i 2) ?\{) ; char-after
3134 cperl-continued-brace-offset 0)))
3135 ;;
3136 ;; Indenter for lines in a block which are not leading lines
3137 ;;
3138 ((eq 'have-prev-sibling (elt i 0))
3139 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3140 (goto-char (elt i 1)) ; sibling-beg
3141 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
3142 (if (> (current-indentation)
3143 cperl-min-label-indent)
3144 (- (current-indentation) cperl-label-offset)
3145 ;; Do not believe: `max' was involved in calculation of indent
3146 (+ cperl-indent-level
3147 (save-excursion
3148 (goto-char (elt i 3)) ; block-start
3149 (current-indentation))))
3150 (current-column)))
3151 ;;
3152 ;; Indenter for the first line in a block
3153 ;;
3154 ((eq 'code-start-in-block (elt i 0))
3155 ;;[code-start-in-block before-brace char-after
3156 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3157 ;; group-starts-before-start-of-sub start-of-control-group]
3158 (goto-char (elt i 1))
3159 ;; For open brace in column zero, don't let statement
3160 ;; start there too. If cperl-indent-level=0,
3161 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3162 (+ (if (and (bolp) (zerop cperl-indent-level))
3163 (+ cperl-brace-offset cperl-continued-statement-offset)
3164 cperl-indent-level)
3165 (if (and (elt i 3) ; is-a-HASH-ref
3166 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3167 (+ cperl-indent-level cperl-close-paren-offset)
3168 0)
3169 ;; Unless openbrace is the first nonwhite thing on the line,
3170 ;; add the cperl-brace-imaginary-offset.
3171 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3172 cperl-brace-imaginary-offset)
3173 (progn
3174 (goto-char (elt i 6)) ; start-of-control-group
3175 (if (elt i 5) ; group-starts-before-start-of-sub
3176 (current-column)
3177 ;; Get initial indentation of the line we are on.
3178 ;; If line starts with label, calculate label indentation
3179 (if (save-excursion
3180 (beginning-of-line)
3181 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3182 (if (> (current-indentation) cperl-min-label-indent)
3183 (- (current-indentation) cperl-label-offset)
3184 ;; Do not move `parse-data', this should
3185 ;; be quick anyway:
3186 (cperl-calculate-indent))
3187 (current-indentation))))))
3188 (t
3189 (error "Unrecognized value of indent: %s" i))))
3190 (t
3191 (error "Got strange value of indent: %s" i))))))
3192
3193 (defun cperl-calculate-indent-within-comment ()
3194 "Return the indentation amount for line, assuming that
3195 the current line is to be regarded as part of a block comment."
3196 (let (end star-start)
3197 (save-excursion
3198 (beginning-of-line)
3199 (skip-chars-forward " \t")
3200 (setq end (point))
3201 (and (= (following-char) ?#)
3202 (forward-line -1)
3203 (cperl-to-comment-or-eol)
3204 (setq end (point)))
3205 (goto-char end)
3206 (current-column))))
3207
3208
3209 (defun cperl-to-comment-or-eol ()
3210 "Go to position before comment on the current line, or to end of line.
3211 Returns true if comment is found. In POD will not move the point."
3212 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3213 ;; then looks for literal # or end-of-line.
3214 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3215 (or cperl-font-locking
3216 (cperl-update-syntaxification lim lim))
3217 (beginning-of-line)
3218 (if (setq pr (get-text-property (point) 'syntax-type))
3219 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3220 (if (or (eq pr 'pod)
3221 (if (or (not e) (> e lim)) ; deep inside a group
3222 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3223 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3224 ;; Else - need to do it the hard way
3225 (and (and e (<= e lim))
3226 (goto-char e))
3227 (while (not stop-in)
3228 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3229 ; stop at comment
3230 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3231 (if (nth 4 state) ; After `#';
3232 ; (nth 2 state) can be
3233 ; beginning of m,s,qq and so
3234 ; on
3235 (if (nth 2 state)
3236 (progn
3237 (setq cpoint (point))
3238 (goto-char (nth 2 state))
3239 (cond
3240 ((looking-at "\\(s\\|tr\\)\\>")
3241 (or (re-search-forward
3242 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3243 lim 'move)
3244 (setq stop-in t)))
3245 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3246 (or (re-search-forward
3247 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3248 lim 'move)
3249 (setq stop-in t)))
3250 (t ; It was fair comment
3251 (setq stop-in t) ; Finish
3252 (goto-char (1- cpoint)))))
3253 (setq stop-in t) ; Finish
3254 (forward-char -1))
3255 (setq stop-in t))) ; Finish
3256 (nth 4 state))))
3257
3258 (defsubst cperl-modify-syntax-type (at how)
3259 (if (< at (point-max))
3260 (progn
3261 (put-text-property at (1+ at) 'syntax-table how)
3262 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3263
3264 (defun cperl-protect-defun-start (s e)
3265 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3266 (save-excursion
3267 (goto-char s)
3268 (while (re-search-forward "^\\s(" e 'to-end)
3269 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3270
3271 (defun cperl-commentify (bb e string &optional noface)
3272 (if cperl-use-syntax-table-text-property
3273 (if (eq noface 'n) ; Only immediate
3274 nil
3275 ;; We suppose that e is _after_ the end of construction, as after eol.
3276 (setq string (if string cperl-st-sfence cperl-st-cfence))
3277 (if (> bb (- e 2))
3278 ;; one-char string/comment?!
3279 (cperl-modify-syntax-type bb cperl-st-punct)
3280 (cperl-modify-syntax-type bb string)
3281 (cperl-modify-syntax-type (1- e) string))
3282 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3283 (put-text-property (1+ bb) (1- e)
3284 'syntax-table cperl-string-syntax-table))
3285 (cperl-protect-defun-start bb e))
3286 ;; Fontify
3287 (or noface
3288 (not cperl-pod-here-fontify)
3289 (put-text-property bb e 'face (if string 'font-lock-string-face
3290 'font-lock-comment-face)))))
3291
3292 (defvar cperl-starters '(( ?\( . ?\) )
3293 ( ?\[ . ?\] )
3294 ( ?\{ . ?\} )
3295 ( ?\< . ?\> )))
3296
3297 (defun cperl-cached-syntax-table (st)
3298 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3299 All the entries of the syntax table are \".\", except for a backslash, which
3300 is quoting."
3301 (if (car-safe st)
3302 (car st)
3303 (setcar st (make-syntax-table))
3304 (setq st (car st))
3305 (let ((i 0))
3306 (while (< i 256)
3307 (modify-syntax-entry i "." st)
3308 (setq i (1+ i))))
3309 (modify-syntax-entry ?\\ "\\" st)
3310 st))
3311
3312 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3313 &optional ostart oend)
3314 "Find the end of a regular expression or a stringish construct (q[] etc).
3315 The point should be before the starting delimiter.
3316
3317 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3318 is s/// or tr/// like expression. If END is nil, generates an error
3319 message if needed. If SET-ST is non-nil, will use (or generate) a
3320 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3321 error message in its CAR (unless it already contains some error
3322 message). ARGUMENT should be the name of the construct (used in error
3323 messages). OSTART, OEND may be set in recursive calls when processing
3324 the second argument of 2ARG construct.
3325
3326 Works *before* syntax recognition is done. In IS-2ARG situation may
3327 modify syntax-type text property if the situation is too hard."
3328 (let (b starter ender st i i2 go-forward reset-st set-st)
3329 (skip-chars-forward " \t")
3330 ;; ender means matching-char matcher.
3331 (setq b (point)
3332 starter (if (eobp) 0 (char-after b))
3333 ender (cdr (assoc starter cperl-starters)))
3334 ;; What if starter == ?\\ ????
3335 (setq st (cperl-cached-syntax-table st-l))
3336 (setq set-st t)
3337 ;; Whether we have an intermediate point
3338 (setq i nil)
3339 ;; Prepare the syntax table:
3340 (if (not ender) ; m/blah/, s/x//, s/x/y/
3341 (modify-syntax-entry starter "$" st)
3342 (modify-syntax-entry starter (concat "(" (list ender)) st)
3343 (modify-syntax-entry ender (concat ")" (list starter)) st))
3344 (condition-case bb
3345 (progn
3346 ;; We use `$' syntax class to find matching stuff, but $$
3347 ;; is recognized the same as $, so we need to check this manually.
3348 (if (and (eq starter (char-after (cperl-1+ b)))
3349 (not ender))
3350 ;; $ has TeXish matching rules, so $$ equiv $...
3351 (forward-char 2)
3352 (setq reset-st (syntax-table))
3353 (set-syntax-table st)
3354 (forward-sexp 1)
3355 (if (<= (point) (1+ b))
3356 (error "Unfinished regular expression"))
3357 (set-syntax-table reset-st)
3358 (setq reset-st nil)
3359 ;; Now the problem is with m;blah;;
3360 (and (not ender)
3361 (eq (preceding-char)
3362 (char-after (- (point) 2)))
3363 (save-excursion
3364 (forward-char -2)
3365 (= 0 (% (skip-chars-backward "\\\\") 2)))
3366 (forward-char -1)))
3367 ;; Now we are after the first part.
3368 (and is-2arg ; Have trailing part
3369 (not ender)
3370 (eq (following-char) starter) ; Empty trailing part
3371 (progn
3372 (or (eq (char-syntax (following-char)) ?.)
3373 ;; Make trailing letter into punctuation
3374 (cperl-modify-syntax-type (point) cperl-st-punct))
3375 (setq is-2arg nil go-forward t))) ; Ignore the tail
3376 (if is-2arg ; Not number => have second part
3377 (progn
3378 (setq i (point) i2 i)
3379 (if ender
3380 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3381 (progn
3382 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3383 (goto-char (match-end 0))
3384 (skip-chars-forward " \t\n\f"))
3385 (setq i2 (point))))
3386 (forward-char -1))
3387 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3388 (if ender (modify-syntax-entry ender "." st))
3389 (setq set-st nil)
3390 (setq ender (cperl-forward-re lim end nil st-l err-l
3391 argument starter ender)
3392 ender (nth 2 ender)))))
3393 (error (goto-char lim)
3394 (setq set-st nil)
3395 (if reset-st
3396 (set-syntax-table reset-st))
3397 (or end
3398 (message
3399 "End of `%s%s%c ... %c' string/RE not found: %s"
3400 argument
3401 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3402 starter (or ender starter) bb)
3403 (or (car err-l) (setcar err-l b)))))
3404 (if set-st
3405 (progn
3406 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3407 (if ender (modify-syntax-entry ender "." st))))
3408 ;; i: have 2 args, after end of the first arg
3409 ;; i2: start of the second arg, if any (before delim if `ender').
3410 ;; ender: the last arg bounded by parens-like chars, the second one of them
3411 ;; starter: the starting delimiter of the first arg
3412 ;; go-forward: has 2 args, and the second part is empty
3413 (list i i2 ender starter go-forward)))
3414
3415 (defun cperl-forward-group-in-re (&optional st-l)
3416 "Find the end of a group in a REx.
3417 Return the error message (if any). Does not work if delimiter is `)'.
3418 Works before syntax recognition is done."
3419 ;; Works *before* syntax recognition is done
3420 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3421 (let (st b reset-st)
3422 (condition-case b
3423 (progn
3424 (setq st (cperl-cached-syntax-table st-l))
3425 (modify-syntax-entry ?\( "()" st)
3426 (modify-syntax-entry ?\) ")(" st)
3427 (setq reset-st (syntax-table))
3428 (set-syntax-table st)
3429 (forward-sexp 1))
3430 (error (message
3431 "cperl-forward-group-in-re: error %s" b)))
3432 ;; now restore the initial state
3433 (if st
3434 (progn
3435 (modify-syntax-entry ?\( "." st)
3436 (modify-syntax-entry ?\) "." st)))
3437 (if reset-st
3438 (set-syntax-table reset-st))
3439 b))
3440
3441
3442 (defvar font-lock-string-face)
3443 ;;(defvar font-lock-reference-face)
3444 (defvar font-lock-constant-face)
3445 (defsubst cperl-postpone-fontification (b e type val &optional now)
3446 ;; Do after syntactic fontification?
3447 (if cperl-syntaxify-by-font-lock
3448 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3449 (put-text-property b e type val)))
3450
3451 ;;; Here is how the global structures (those which cannot be
3452 ;;; recognized locally) are marked:
3453 ;; a) PODs:
3454 ;; Start-to-end is marked `in-pod' ==> t
3455 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3456 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3457 ;; b) HEREs:
3458 ;; Start-to-end is marked `here-doc-group' ==> t
3459 ;; The body is marked `syntax-type' ==> `here-doc'
3460 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3461 ;; c) FORMATs:
3462 ;; First line (to =) marked `first-format-line' ==> t
3463 ;; After-this--to-end is marked `syntax-type' ==> `format'
3464 ;; d) 'Q'uoted string:
3465 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3466 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3467 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3468 ;; e) Attributes of subroutines: `attrib-group' ==> t
3469 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3470 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3471
3472 ;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3473 ;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3474
3475 (defun cperl-unwind-to-safe (before &optional end)
3476 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3477 (let ((pos (point)) opos)
3478 (while (and pos (progn
3479 (beginning-of-line)
3480 (get-text-property (setq pos (point)) 'syntax-type)))
3481 (setq opos pos
3482 pos (cperl-beginning-of-property pos 'syntax-type))
3483 (if (eq pos (point-min))
3484 (setq pos nil))
3485 (if pos
3486 (if before
3487 (progn
3488 (goto-char (cperl-1- pos))
3489 (beginning-of-line)
3490 (setq pos (point)))
3491 (goto-char (setq pos (cperl-1- pos))))
3492 ;; Up to the start
3493 (goto-char (point-min))))
3494 ;; Skip empty lines
3495 (and (looking-at "\n*=")
3496 (/= 0 (skip-chars-backward "\n"))
3497 (forward-char))
3498 (setq pos (point))
3499 (if end
3500 ;; Do the same for end, going small steps
3501 (save-excursion
3502 (while (and end (get-text-property end 'syntax-type))
3503 (setq pos end
3504 end (next-single-property-change end 'syntax-type nil (point-max)))
3505 (if end (progn (goto-char end)
3506 (or (bolp) (forward-line 1))
3507 (setq end (point)))))
3508 (or end pos)))))
3509
3510 ;;; These are needed for byte-compile (at least with v19)
3511 (defvar cperl-nonoverridable-face)
3512 (defvar font-lock-variable-name-face)
3513 (defvar font-lock-function-name-face)
3514 (defvar font-lock-keyword-face)
3515 (defvar font-lock-builtin-face)
3516 (defvar font-lock-type-face)
3517 (defvar font-lock-comment-face)
3518 (defvar font-lock-warning-face)
3519
3520 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3521 "Syntaxically mark (and fontify) attributes of a subroutine.
3522 Should be called with the point before leading colon of an attribute."
3523 ;; Works *before* syntax recognition is done
3524 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3525 (let (st b p reset-st after-first (start (point)) start1 end1)
3526 (condition-case b
3527 (while (looking-at
3528 (concat
3529 "\\(" ; 1=optional? colon
3530 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3531 "\\)"
3532 (if after-first "?" "")
3533 ;; No space between name and paren allowed...
3534 "\\(\\sw+\\)" ; 3=name
3535 "\\((\\)?")) ; 4=optional paren
3536 (and (match-beginning 1)
3537 (cperl-postpone-fontification
3538 (match-beginning 0) (cperl-1+ (match-beginning 0))
3539 'face font-lock-constant-face))
3540 (setq start1 (match-beginning 3) end1 (match-end 3))
3541 (cperl-postpone-fontification start1 end1
3542 'face font-lock-constant-face)
3543 (goto-char end1) ; end or before `('
3544 (if (match-end 4) ; Have attribute arguments...
3545 (progn
3546 (if st nil
3547 (setq st (cperl-cached-syntax-table st-l))
3548 (modify-syntax-entry ?\( "()" st)
3549 (modify-syntax-entry ?\) ")(" st))
3550 (setq reset-st (syntax-table) p (point))
3551 (set-syntax-table st)
3552 (forward-sexp 1)
3553 (set-syntax-table reset-st)
3554 (setq reset-st nil)
3555 (cperl-commentify p (point) t))) ; mark as string
3556 (forward-comment (buffer-size))
3557 (setq after-first t))
3558 (error (message
3559 "L%d: attribute `%s': %s"
3560 (count-lines (point-min) (point))
3561 (and start1 end1 (buffer-substring start1 end1)) b)
3562 (setq start nil)))
3563 (and start
3564 (progn
3565 (put-text-property start (point)
3566 'attrib-group (if (looking-at "{") t 0))
3567 (and pos
3568 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3569 ;; Apparently, we do not need `multiline': faces added now
3570 (put-text-property (+ 3 pos) (cperl-1+ (point))
3571 'syntax-type 'sub-decl))
3572 (and b-fname ; Fontify here: the following condition
3573 (cperl-postpone-fontification ; is too hard to determine by
3574 b-fname e-fname 'face ; a REx, so do it here
3575 (if (looking-at "{")
3576 font-lock-function-name-face
3577 font-lock-variable-name-face)))))
3578 ;; now restore the initial state
3579 (if st
3580 (progn
3581 (modify-syntax-entry ?\( "." st)
3582 (modify-syntax-entry ?\) "." st)))
3583 (if reset-st
3584 (set-syntax-table reset-st))))
3585
3586 (defsubst cperl-look-at-leading-count (is-x-REx e)
3587 (if (and
3588 (< (point) e)
3589 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3590 (1- e) t)) ; return nil on failure, no moving
3591 (if (eq ?\{ (preceding-char)) nil
3592 (cperl-postpone-fontification
3593 (1- (point)) (point)
3594 'face font-lock-warning-face))))
3595
3596 ;;; Debugging this may require (setq max-specpdl-size 2000)...
3597 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3598 "Scans the buffer for hard-to-parse Perl constructions.
3599 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3600 the sections using `cperl-pod-head-face', `cperl-pod-face',
3601 `cperl-here-face'."
3602 (interactive)
3603 (or min (setq min (point-min)
3604 cperl-syntax-state nil
3605 cperl-syntax-done-to min))
3606 (or max (setq max (point-max)))
3607 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3608 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3609 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3610 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3611 (modified (buffer-modified-p)) overshoot is-o-REx
3612 (after-change-functions nil)
3613 (cperl-font-locking t)
3614 (use-syntax-state (and cperl-syntax-state
3615 (>= min (car cperl-syntax-state))))
3616 (state-point (if use-syntax-state
3617 (car cperl-syntax-state)
3618 (point-min)))
3619 (state (if use-syntax-state
3620 (cdr cperl-syntax-state)))
3621 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3622 (st-l (list nil)) (err-l (list nil))
3623 ;; Somehow font-lock may be not loaded yet...
3624 ;; (e.g., when building TAGS via command-line call)
3625 (font-lock-string-face (if (boundp 'font-lock-string-face)
3626 font-lock-string-face
3627 'font-lock-string-face))
3628 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3629 font-lock-constant-face
3630 'font-lock-constant-face))
3631 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3632 (if (boundp 'font-lock-function-name-face)
3633 font-lock-function-name-face
3634 'font-lock-function-name-face))
3635 (font-lock-variable-name-face ; interpolated vars and ({})-code
3636 (if (boundp 'font-lock-variable-name-face)
3637 font-lock-variable-name-face
3638 'font-lock-variable-name-face))
3639 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3640 (if (boundp 'font-lock-function-name-face)
3641 font-lock-function-name-face
3642 'font-lock-function-name-face))
3643 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3644 (if (boundp 'font-lock-constant-face)
3645 font-lock-constant-face
3646 'font-lock-constant-face))
3647 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3648 (if (boundp 'font-lock-builtin-face)
3649 font-lock-builtin-face
3650 'font-lock-builtin-face))
3651 (font-lock-comment-face
3652 (if (boundp 'font-lock-comment-face)
3653 font-lock-comment-face
3654 'font-lock-comment-face))
3655 (font-lock-warning-face
3656 (if (boundp 'font-lock-warning-face)
3657 font-lock-warning-face
3658 'font-lock-warning-face))
3659 (my-cperl-REx-ctl-face ; (|)
3660 (if (boundp 'font-lock-keyword-face)
3661 font-lock-keyword-face
3662 'font-lock-keyword-face))
3663 (my-cperl-REx-modifiers-face ; //gims
3664 (if (boundp 'cperl-nonoverridable-face)
3665 cperl-nonoverridable-face
3666 'cperl-nonoverridable-face))
3667 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3668 (if (boundp 'font-lock-type-face)
3669 font-lock-type-face
3670 'font-lock-type-face))
3671 (stop-point (if ignore-max
3672 (point-max)
3673 max))
3674 (search
3675 (concat
3676 "\\(\\`\n?\\|^\n\\)=" ; POD
3677 "\\|"
3678 ;; One extra () before this:
3679 "<<" ; HERE-DOC
3680 "\\(" ; 1 + 1
3681 ;; First variant "BLAH" or just ``.
3682 "[ \t]*" ; Yes, whitespace is allowed!
3683 "\\([\"'`]\\)" ; 2 + 1 = 3
3684 "\\([^\"'`\n]*\\)" ; 3 + 1
3685 "\\3"
3686 "\\|"
3687 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3688 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3689 ;; Do not have <<= or << 30 or <<30 or << $blah.
3690 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3691 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3692 "\\)"
3693 "\\|"
3694 ;; 1+6 extra () before this:
3695 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3696 (if cperl-use-syntax-table-text-property
3697 (concat
3698 "\\|"
3699 ;; 1+6+2=9 extra () before this:
3700 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3701 "\\|"
3702 ;; 1+6+2+1=10 extra () before this:
3703 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3704 "\\|"
3705 ;; 1+6+2+1+1=11 extra () before this
3706 "\\<sub\\>" ; sub with proto/attr
3707 "\\("
3708 cperl-white-and-comment-rex
3709 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3710 "\\("
3711 cperl-maybe-white-and-comment-rex
3712 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3713 "\\|"
3714 ;; 1+6+2+1+1+6=17 extra () before this:
3715 "\\$\\(['{]\\)" ; $' or ${foo}
3716 "\\|"
3717 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3718 ;; we do not support intervening comments...):
3719 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3720 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3721 "\\|"
3722 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3723 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3724 "\\|"
3725 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3726 ""))))
3727 (unwind-protect
3728 (progn
3729 (save-excursion
3730 (or non-inter
3731 (message "Scanning for \"hard\" Perl constructions..."))
3732 ;;(message "find: %s --> %s" min max)
3733 (and cperl-pod-here-fontify
3734 ;; We had evals here, do not know why...
3735 (setq face cperl-pod-face
3736 head-face cperl-pod-head-face
3737 here-face cperl-here-face))
3738 (remove-text-properties min max
3739 '(syntax-type t in-pod t syntax-table t
3740 attrib-group t
3741 REx-interpolated t
3742 cperl-postpone t
3743 syntax-subtype t
3744 rear-nonsticky t
3745 front-sticky t
3746 here-doc-group t
3747 first-format-line t
3748 REx-part2 t
3749 indentable t))
3750 ;; Need to remove face as well...
3751 (goto-char min)
3752 (and (eq system-type 'emx)
3753 (eq (point) 1)
3754 (let ((case-fold-search t))
3755 (looking-at "extproc[ \t]")) ; Analogue of #!
3756 (cperl-commentify min
3757 (save-excursion (end-of-line) (point))
3758 nil))
3759 (while (and
3760 (< (point) max)
3761 (re-search-forward search max t))
3762 (setq tmpend nil) ; Valid for most cases
3763 (setq b (match-beginning 0)
3764 state (save-excursion (parse-partial-sexp
3765 state-point b nil nil state))
3766 state-point b)
3767 (cond
3768 ;; 1+6+2+1+1+6=17 extra () before this:
3769 ;; "\\$\\(['{]\\)"
3770 ((match-beginning 18) ; $' or ${foo}
3771 (if (eq (preceding-char) ?\') ; $'
3772 (progn
3773 (setq b (1- (point))
3774 state (parse-partial-sexp
3775 state-point (1- b) nil nil state)
3776 state-point (1- b))
3777 (if (nth 3 state) ; in string
3778 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3779 (goto-char (1+ b)))
3780 ;; else: ${
3781 (setq bb (match-beginning 0))
3782 (cperl-modify-syntax-type bb cperl-st-punct)))
3783 ;; No processing in strings/comments beyond this point:
3784 ((or (nth 3 state) (nth 4 state))
3785 t) ; Do nothing in comment/string
3786 ((match-beginning 1) ; POD section
3787 ;; "\\(\\`\n?\\|^\n\\)="
3788 (setq b (match-beginning 0)
3789 state (parse-partial-sexp
3790 state-point b nil nil state)
3791 state-point b)
3792 (if (or (nth 3 state) (nth 4 state)
3793 (looking-at "cut\\>"))
3794 (if (or (nth 3 state) (nth 4 state) ignore-max)
3795 nil ; Doing a chunk only
3796 (message "=cut is not preceded by a POD section")
3797 (or (car err-l) (setcar err-l (point))))
3798 (beginning-of-line)
3799
3800 (setq b (point)
3801 bb b
3802 tb (match-beginning 0)
3803 b1 nil) ; error condition
3804 ;; We do not search to max, since we may be called from
3805 ;; some hook of fontification, and max is random
3806 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3807 (progn
3808 (goto-char b)
3809 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3810 (progn
3811 (message "=cut is not preceded by an empty line")
3812 (setq b1 t)
3813 (or (car err-l) (setcar err-l b))))))
3814 (beginning-of-line 2) ; An empty line after =cut is not POD!
3815 (setq e (point))
3816 (and (> e max)
3817 (progn
3818 (remove-text-properties
3819 max e '(syntax-type t in-pod t syntax-table t
3820 attrib-group t
3821 REx-interpolated t
3822 cperl-postpone t
3823 syntax-subtype t
3824 here-doc-group t
3825 rear-nonsticky t
3826 front-sticky t
3827 first-format-line t
3828 REx-part2 t
3829 indentable t))
3830 (setq tmpend tb)))
3831 (put-text-property b e 'in-pod t)
3832 (put-text-property b e 'syntax-type 'in-pod)
3833 (goto-char b)
3834 (while (re-search-forward "\n\n[ \t]" e t)
3835 ;; We start 'pod 1 char earlier to include the preceding line
3836 (beginning-of-line)
3837 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3838 (cperl-put-do-not-fontify b (point) t)
3839 ;; mark the non-literal parts as PODs
3840 (if cperl-pod-here-fontify
3841 (cperl-postpone-fontification b (point) 'face face t))
3842 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3843 (beginning-of-line)
3844 (setq b (point)))
3845 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3846 (cperl-put-do-not-fontify (point) e t)
3847 (if cperl-pod-here-fontify
3848 (progn
3849 ;; mark the non-literal parts as PODs
3850 (cperl-postpone-fontification (point) e 'face face t)
3851 (goto-char bb)
3852 (if (looking-at
3853 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3854 ;; mark the headers
3855 (cperl-postpone-fontification
3856 (match-beginning 1) (match-end 1)
3857 'face head-face))
3858 (while (re-search-forward
3859 ;; One paragraph
3860 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3861 e 'toend)
3862 ;; mark the headers
3863 (cperl-postpone-fontification
3864 (match-beginning 1) (match-end 1)
3865 'face head-face))))
3866 (cperl-commentify bb e nil)
3867 (goto-char e)
3868 (or (eq e (point-max))
3869 (forward-char -1)))) ; Prepare for immediate POD start.
3870 ;; Here document
3871 ;; We can do many here-per-line;
3872 ;; but multiline quote on the same line as <<HERE confuses us...
3873 ;; ;; One extra () before this:
3874 ;;"<<"
3875 ;; "\\(" ; 1 + 1
3876 ;; ;; First variant "BLAH" or just ``.
3877 ;; "[ \t]*" ; Yes, whitespace is allowed!
3878 ;; "\\([\"'`]\\)" ; 2 + 1
3879 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3880 ;; "\\3"
3881 ;; "\\|"
3882 ;; ;; Second variant: Identifier or \ID or empty
3883 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3884 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3885 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3886 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3887 ;; "\\)"
3888 ((match-beginning 2) ; 1 + 1
3889 (setq b (point)
3890 tb (match-beginning 0)
3891 c (and ; not HERE-DOC
3892 (match-beginning 5)
3893 (save-match-data
3894 (or (looking-at "[ \t]*(") ; << function_call()
3895 (save-excursion ; 1 << func_name, or $foo << 10
3896 (condition-case nil
3897 (progn
3898 (goto-char tb)
3899 ;;; XXX What to do: foo <<bar ???
3900 ;;; XXX Need to support print {a} <<B ???
3901 (forward-sexp -1)
3902 (save-match-data
3903 ; $foo << b; $f .= <<B;
3904 ; ($f+1) << b; a($f) . <<B;
3905 ; foo 1, <<B; $x{a} <<b;
3906 (cond
3907 ((looking-at "[0-9$({]")
3908 (forward-sexp 1)
3909 (and
3910 (looking-at "[ \t]*<<")
3911 (condition-case nil
3912 ;; print $foo <<EOF
3913 (progn
3914 (forward-sexp -2)
3915 (not
3916 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
3917 (error t)))))))
3918 (error nil))) ; func(<<EOF)
3919 (and (not (match-beginning 6)) ; Empty
3920 (looking-at
3921 "[ \t]*[=0-9$@%&(]"))))))
3922 (if c ; Not here-doc
3923 nil ; Skip it.
3924 (setq c (match-end 2)) ; 1 + 1
3925 (if (match-beginning 5) ;4 + 1
3926 (setq b1 (match-beginning 5) ; 4 + 1
3927 e1 (match-end 5)) ; 4 + 1
3928 (setq b1 (match-beginning 4) ; 3 + 1
3929 e1 (match-end 4))) ; 3 + 1
3930 (setq tag (buffer-substring b1 e1)
3931 qtag (regexp-quote tag))
3932 (cond (cperl-pod-here-fontify
3933 ;; Highlight the starting delimiter
3934 (cperl-postpone-fontification
3935 b1 e1 'face my-cperl-delimiters-face)
3936 (cperl-put-do-not-fontify b1 e1 t)))
3937 (forward-line)
3938 (setq i (point))
3939 (if end-of-here-doc
3940 (goto-char end-of-here-doc))
3941 (setq b (point))
3942 ;; We do not search to max, since we may be called from
3943 ;; some hook of fontification, and max is random
3944 (or (and (re-search-forward (concat "^" qtag "$")
3945 stop-point 'toend)
3946 ;;;(eq (following-char) ?\n) ; XXXX WHY???
3947 )
3948 (progn ; Pretend we matched at the end
3949 (goto-char (point-max))
3950 (re-search-forward "\\'")
3951 (message "End of here-document `%s' not found." tag)
3952 (or (car err-l) (setcar err-l b))))
3953 (if cperl-pod-here-fontify
3954 (progn
3955 ;; Highlight the ending delimiter
3956 (cperl-postpone-fontification
3957 (match-beginning 0) (match-end 0)
3958 'face my-cperl-delimiters-face)
3959 (cperl-put-do-not-fontify b (match-end 0) t)
3960 ;; Highlight the HERE-DOC
3961 (cperl-postpone-fontification b (match-beginning 0)
3962 'face here-face)))
3963 (setq e1 (cperl-1+ (match-end 0)))
3964 (put-text-property b (match-beginning 0)
3965 'syntax-type 'here-doc)
3966 (put-text-property (match-beginning 0) e1
3967 'syntax-type 'here-doc-delim)
3968 (put-text-property b e1 'here-doc-group t)
3969 ;; This makes insertion at the start of HERE-DOC update
3970 ;; the whole construct:
3971 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
3972 (cperl-commentify b e1 nil)
3973 (cperl-put-do-not-fontify b (match-end 0) t)
3974 ;; Cache the syntax info...
3975 (setq cperl-syntax-state (cons state-point state))
3976 ;; ... and process the rest of the line...
3977 (setq overshoot
3978 (elt ; non-inter ignore-max
3979 (cperl-find-pods-heres c i t end t e1) 1))
3980 (if (and overshoot (> overshoot (point)))
3981 (goto-char overshoot)
3982 (setq overshoot e1))
3983 (if (> e1 max)
3984 (setq tmpend tb))))
3985 ;; format
3986 ((match-beginning 8)
3987 ;; 1+6=7 extra () before this:
3988 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3989 (setq b (point)
3990 name (if (match-beginning 8) ; 7 + 1
3991 (buffer-substring (match-beginning 8) ; 7 + 1
3992 (match-end 8)) ; 7 + 1
3993 "")
3994 tb (match-beginning 0))
3995 (setq argument nil)
3996 (put-text-property (save-excursion
3997 (beginning-of-line)
3998 (point))
3999 b 'first-format-line 't)
4000 (if cperl-pod-here-fontify
4001 (while (and (eq (forward-line) 0)
4002 (not (looking-at "^[.;]$")))
4003 (cond
4004 ((looking-at "^#")) ; Skip comments
4005 ((and argument ; Skip argument multi-lines
4006 (looking-at "^[ \t]*{"))
4007 (forward-sexp 1)
4008 (setq argument nil))
4009 (argument ; Skip argument lines
4010 (setq argument nil))
4011 (t ; Format line
4012 (setq b1 (point))
4013 (setq argument (looking-at "^[^\n]*[@^]"))
4014 (end-of-line)
4015 ;; Highlight the format line
4016 (cperl-postpone-fontification b1 (point)
4017 'face font-lock-string-face)
4018 (cperl-commentify b1 (point) nil)
4019 (cperl-put-do-not-fontify b1 (point) t))))
4020 ;; We do not search to max, since we may be called from
4021 ;; some hook of fontification, and max is random
4022 (re-search-forward "^[.;]$" stop-point 'toend))
4023 (beginning-of-line)
4024 (if (looking-at "^\\.$") ; ";" is not supported yet
4025 (progn
4026 ;; Highlight the ending delimiter
4027 (cperl-postpone-fontification (point) (+ (point) 2)
4028 'face font-lock-string-face)
4029 (cperl-commentify (point) (+ (point) 2) nil)
4030 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4031 (message "End of format `%s' not found." name)
4032 (or (car err-l) (setcar err-l b)))
4033 (forward-line)
4034 (if (> (point) max)
4035 (setq tmpend tb))
4036 (put-text-property b (point) 'syntax-type 'format))
4037 ;; qq-like String or Regexp:
4038 ((or (match-beginning 10) (match-beginning 11))
4039 ;; 1+6+2=9 extra () before this:
4040 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4041 ;; "\\|"
4042 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4043 (setq b1 (if (match-beginning 10) 10 11)
4044 argument (buffer-substring
4045 (match-beginning b1) (match-end b1))
4046 b (point) ; end of qq etc
4047 i b
4048 c (char-after (match-beginning b1))
4049 bb (char-after (1- (match-beginning b1))) ; tmp holder
4050 ;; bb == "Not a stringy"
4051 bb (if (eq b1 10) ; user variables/whatever
4052 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4053 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4054 ((eq bb ?\:) ; $opt::s
4055 (eq (char-after
4056 (- (match-beginning b1) 2))
4057 ?\:))
4058 ((eq bb ?\>) ; $foo->s
4059 (eq (char-after
4060 (- (match-beginning b1) 2))
4061 ?\-))
4062 ((eq bb ?\&)
4063 (not (eq (char-after ; &&m/blah/
4064 (- (match-beginning b1) 2))
4065 ?\&)))
4066 (t t)))
4067 ;; <file> or <$file>
4068 (and (eq c ?\<)
4069 ;; Do not stringify <FH>, <$fh> :
4070 (save-match-data
4071 (looking-at
4072 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4073 tb (match-beginning 0))
4074 (goto-char (match-beginning b1))
4075 (cperl-backward-to-noncomment (point-min))
4076 (or bb
4077 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4078 (setq argument ""
4079 b1 nil
4080 bb ; Not a regexp?
4081 (not
4082 ;; What is below: regexp-p?
4083 (and
4084 (or (memq (preceding-char)
4085 (append (if (memq c '(?\? ?\<))
4086 ;; $a++ ? 1 : 2
4087 "~{(=|&*!,;:["
4088 "~{(=|&+-*!,;:[") nil))
4089 (and (eq (preceding-char) ?\})
4090 (cperl-after-block-p (point-min)))
4091 (and (eq (char-syntax (preceding-char)) ?w)
4092 (progn
4093 (forward-sexp -1)
4094 ;; After these keywords `/' starts a RE. One should add all the
4095 ;; functions/builtins which expect an argument, but ...
4096 (if (eq (preceding-char) ?-)
4097 ;; -d ?foo? is a RE
4098 (looking-at "[a-zA-Z]\\>")
4099 (and
4100 (not (memq (preceding-char)
4101 '(?$ ?@ ?& ?%)))
4102 (looking-at
4103 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4104 (and (eq (preceding-char) ?.)
4105 (eq (char-after (- (point) 2)) ?.))
4106 (bobp))
4107 ;; m|blah| ? foo : bar;
4108 (not
4109 (and (eq c ?\?)
4110 cperl-use-syntax-table-text-property
4111 (not (bobp))
4112 (progn
4113 (forward-char -1)
4114 (looking-at "\\s|"))))))
4115 b (1- b))
4116 ;; s y tr m
4117 ;; Check for $a -> y
4118 (setq b1 (preceding-char)
4119 go (point))
4120 (if (and (eq b1 ?>)
4121 (eq (char-after (- go 2)) ?-))
4122 ;; Not a regexp
4123 (setq bb t))))
4124 (or bb
4125 (progn
4126 (goto-char b)
4127 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4128 (goto-char (match-end 0))
4129 (skip-chars-forward " \t\n\f"))
4130 (cond ((and (eq (following-char) ?\})
4131 (eq b1 ?\{))
4132 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4133 (goto-char (1- go))
4134 (skip-chars-backward " \t\n\f")
4135 (if (memq (preceding-char) (append "$@%&*" nil))
4136 (setq bb t) ; @{y}
4137 (condition-case nil
4138 (forward-sexp -1)
4139 (error nil)))
4140 (if (or bb
4141 (looking-at ; $foo -> {s}
4142 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4143 (and ; $foo[12] -> {s}
4144 (memq (following-char) '(?\{ ?\[))
4145 (progn
4146 (forward-sexp 1)
4147 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4148 (setq bb t)
4149 (goto-char b)))
4150 ((and (eq (following-char) ?=)
4151 (eq (char-after (1+ (point))) ?\>))
4152 ;; Check for { foo => 1, s => 2 }
4153 ;; Apparently s=> is never a substitution...
4154 (setq bb t))
4155 ((and (eq (following-char) ?:)
4156 (eq b1 ?\{) ; Check for $ { s::bar }
4157 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4158 (progn
4159 (goto-char (1- go))
4160 (skip-chars-backward " \t\n\f")
4161 (memq (preceding-char)
4162 (append "$@%&*" nil))))
4163 (setq bb t))
4164 ((eobp)
4165 (setq bb t)))))
4166 (if bb
4167 (goto-char i)
4168 ;; Skip whitespace and comments...
4169 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4170 (goto-char (match-end 0))
4171 (skip-chars-forward " \t\n\f"))
4172 (if (> (point) b)
4173 (put-text-property b (point) 'syntax-type 'prestring))
4174 ;; qtag means two-arg matcher, may be reset to
4175 ;; 2 or 3 later if some special quoting is needed.
4176 ;; e1 means matching-char matcher.
4177 (setq b (point) ; before the first delimiter
4178 ;; has 2 args
4179 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4180 ;; We do not search to max, since we may be called from
4181 ;; some hook of fontification, and max is random
4182 i (cperl-forward-re stop-point end
4183 i2
4184 st-l err-l argument)
4185 ;; If `go', then it is considered as 1-arg, `b1' is nil
4186 ;; as in s/foo//x; the point is before final "slash"
4187 b1 (nth 1 i) ; start of the second part
4188 tag (nth 2 i) ; ender-char, true if second part
4189 ; is with matching chars []
4190 go (nth 4 i) ; There is a 1-char part after the end
4191 i (car i) ; intermediate point
4192 e1 (point) ; end
4193 ;; Before end of the second part if non-matching: ///
4194 tail (if (and i (not tag))
4195 (1- e1))
4196 e (if i i e1) ; end of the first part
4197 qtag nil ; need to preserve backslashitis
4198 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4199 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4200 ;; Commenting \\ is dangerous, what about ( ?
4201 (and i tail
4202 (eq (char-after i) ?\\)
4203 (setq qtag t))
4204 (and (if go (looking-at ".\\sw*x")
4205 (looking-at "\\sw*x")) ; qr//x
4206 (setq is-x-REx t))
4207 (and (if go (looking-at ".\\sw*o")
4208 (looking-at "\\sw*o")) ; //o
4209 (setq is-o-REx t))
4210 (if (null i)
4211 ;; Considered as 1arg form
4212 (progn
4213 (cperl-commentify b (point) t)
4214 (put-text-property b (point) 'syntax-type 'string)
4215 (if (or is-x-REx
4216 ;; ignore other text properties:
4217 (string-match "^qw$" argument))
4218 (put-text-property b (point) 'indentable t))
4219 (and go
4220 (setq e1 (cperl-1+ e1))
4221 (or (eobp)
4222 (forward-char 1))))
4223 (cperl-commentify b i t)
4224 (if (looking-at "\\sw*e") ; s///e
4225 (progn
4226 ;; Cache the syntax info...
4227 (setq cperl-syntax-state (cons state-point state))
4228 (and
4229 ;; silent:
4230 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4231 ;; Error
4232 (goto-char (1+ max)))
4233 (if (and tag (eq (preceding-char) ?\>))
4234 (progn
4235 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4236 (cperl-modify-syntax-type i cperl-st-bra)))
4237 (put-text-property b i 'syntax-type 'string)
4238 (put-text-property i (point) 'syntax-type 'multiline)
4239 (if is-x-REx
4240 (put-text-property b i 'indentable t)))
4241 (cperl-commentify b1 (point) t)
4242 (put-text-property b (point) 'syntax-type 'string)
4243 (if is-x-REx
4244 (put-text-property b i 'indentable t))
4245 (if qtag
4246 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4247 (setq tail nil)))
4248 ;; Now: tail: if the second part is non-matching without ///e
4249 (if (eq (char-syntax (following-char)) ?w)
4250 (progn
4251 (forward-word 1) ; skip modifiers s///s
4252 (if tail (cperl-commentify tail (point) t))
4253 (cperl-postpone-fontification
4254 e1 (point) 'face my-cperl-REx-modifiers-face)))
4255 ;; Check whether it is m// which means "previous match"
4256 ;; and highlight differently
4257 (setq is-REx
4258 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4259 (or (not (= (length argument) 0))
4260 (not (eq c ?\<)))))
4261 (if (and is-REx
4262 (eq e (+ 2 b))
4263 ;; split // *is* using zero-pattern
4264 (save-excursion
4265 (condition-case nil
4266 (progn
4267 (goto-char tb)
4268 (forward-sexp -1)
4269 (not (looking-at "split\\>")))
4270 (error t))))
4271 (cperl-postpone-fontification
4272 b e 'face font-lock-warning-face)
4273 (if (or i2 ; Has 2 args
4274 (and cperl-fontify-m-as-s
4275 (or
4276 (string-match "^\\(m\\|qr\\)$" argument)
4277 (and (eq 0 (length argument))
4278 (not (eq ?\< (char-after b)))))))
4279 (progn
4280 (cperl-postpone-fontification
4281 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4282 (cperl-postpone-fontification
4283 (1- e) e 'face my-cperl-delimiters-face)))
4284 (if (and is-REx cperl-regexp-scan)
4285 ;; Process RExen: embedded comments, charclasses and ]
4286 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4287 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4288 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4289 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4290 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4291 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4292 (save-excursion
4293 (goto-char (1+ b))
4294 ;; First
4295 (cperl-look-at-leading-count is-x-REx e)
4296 (setq hairy-RE
4297 (concat
4298 (if is-x-REx
4299 (if (eq (char-after b) ?\#)
4300 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4301 "\\((\\?#\\)\\|\\(#\\)")
4302 ;; keep the same count: add a fake group
4303 (if (eq (char-after b) ?\#)
4304 "\\((\\?\\\\#\\)\\(\\)"
4305 "\\((\\?#\\)\\(\\)"))
4306 "\\|"
4307 "\\(\\[\\)" ; 3=[
4308 "\\|"
4309 "\\(]\\)" ; 4=]
4310 "\\|"
4311 ;; XXXX Will not be able to use it in s)))
4312 (if (eq (char-after b) ?\) )
4313 "\\())))\\)" ; Will never match
4314 (if (eq (char-after b) ?? )
4315 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4316 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4317 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4318 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4319 "\\(" ;; XXXX 1-char variables, exc. |()\s
4320 "[$@]"
4321 "\\("
4322 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4323 "\\|"
4324 "{[^{}]*}" ; only one-level allowed
4325 "\\|"
4326 "[^{(|) \t\r\n\f]"
4327 "\\)"
4328 "\\(" ;;8,9:code part of array/hash elt
4329 "\\(" "->" "\\)?"
4330 "\\[[^][]*\\]"
4331 "\\|"
4332 "{[^{}]*}"
4333 "\\)*"
4334 ;; XXXX: what if u is delim?
4335 "\\|"
4336 "[)^|$.*?+]"
4337 "\\|"
4338 "{[0-9]+}"
4339 "\\|"
4340 "{[0-9]+,[0-9]*}"
4341 "\\|"
4342 "\\\\[luLUEQbBAzZG]"
4343 "\\|"
4344 "(" ; Group opener
4345 "\\(" ; 10 group opener follower
4346 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4347 "\\|"
4348 "\\?[:=!>?{]" ; "?" something
4349 "\\|"
4350 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4351 "\\|"
4352 "\\?([0-9]+)" ; (?(1)foo|bar)
4353 "\\|"
4354 "\\?<[=!]"
4355 ;;;"\\|"
4356 ;;; "\\?"
4357 "\\)?"
4358 "\\)"
4359 "\\|"
4360 "\\\\\\(.\\)" ; 12=\SYMBOL
4361 ))
4362 (while
4363 (and (< (point) (1- e))
4364 (re-search-forward hairy-RE (1- e) 'to-end))
4365 (goto-char (match-beginning 0))
4366 (setq REx-subgr-start (point)
4367 was-subgr (following-char))
4368 (cond
4369 ((match-beginning 6) ; 0-length builtins, groups
4370 (goto-char (match-end 0))
4371 (if (match-beginning 11)
4372 (goto-char (match-beginning 11)))
4373 (if (>= (point) e)
4374 (goto-char (1- e)))
4375 (cperl-postpone-fontification
4376 (match-beginning 0) (point)
4377 'face
4378 (cond
4379 ((eq was-subgr ?\) )
4380 (condition-case nil
4381 (save-excursion
4382 (forward-sexp -1)
4383 (if (> (point) b)
4384 (if (if (eq (char-after b) ?? )
4385 (looking-at "(\\\\\\?")
4386 (eq (char-after (1+ (point))) ?\?))
4387 my-cperl-REx-0length-face
4388 my-cperl-REx-ctl-face)
4389 font-lock-warning-face))
4390 (error font-lock-warning-face)))
4391 ((eq was-subgr ?\| )
4392 my-cperl-REx-ctl-face)
4393 ((eq was-subgr ?\$ )
4394 (if (> (point) (1+ REx-subgr-start))
4395 (progn
4396 (put-text-property
4397 (match-beginning 0) (point)
4398 'REx-interpolated
4399 (if is-o-REx 0
4400 (if (and (eq (match-beginning 0)
4401 (1+ b))
4402 (eq (point)
4403 (1- e))) 1 t)))
4404 font-lock-variable-name-face)
4405 my-cperl-REx-spec-char-face))
4406 ((memq was-subgr (append "^." nil) )
4407 my-cperl-REx-spec-char-face)
4408 ((eq was-subgr ?\( )
4409 (if (not (match-beginning 10))
4410 my-cperl-REx-ctl-face
4411 my-cperl-REx-0length-face))
4412 (t my-cperl-REx-0length-face)))
4413 (if (and (memq was-subgr (append "(|" nil))
4414 (not (string-match "(\\?[-imsx]+)"
4415 (match-string 0))))
4416 (cperl-look-at-leading-count is-x-REx e))
4417 (setq was-subgr nil)) ; We do stuff here
4418 ((match-beginning 12) ; \SYMBOL
4419 (forward-char 2)
4420 (if (>= (point) e)
4421 (goto-char (1- e))
4422 ;; How many chars to not highlight:
4423 ;; 0-len special-alnums in other branch =>
4424 ;; Generic: \non-alnum (1), \alnum (1+face)
4425 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4426 (setq REx-subgr-start (point)
4427 qtag (preceding-char))
4428 (cperl-postpone-fontification
4429 (- (point) 2) (- (point) 1) 'face
4430 (if (memq qtag
4431 (append "ghijkmoqvFHIJKMORTVY" nil))
4432 font-lock-warning-face
4433 my-cperl-REx-0length-face))
4434 (if (and (eq (char-after b) qtag)
4435 (memq qtag (append ".])^$|*?+" nil)))
4436 (progn
4437 (if (and cperl-use-syntax-table-text-property
4438 (eq qtag ?\) ))
4439 (put-text-property
4440 REx-subgr-start (1- (point))
4441 'syntax-table cperl-st-punct))
4442 (cperl-postpone-fontification
4443 (1- (point)) (point) 'face
4444 ; \] can't appear below
4445 (if (memq qtag (append ".]^$" nil))
4446 'my-cperl-REx-spec-char-face
4447 (if (memq qtag (append "*?+" nil))
4448 'my-cperl-REx-0length-face
4449 'my-cperl-REx-ctl-face))))) ; )|
4450 ;; Test for arguments:
4451 (cond
4452 ;; This is not pretty: the 5.8.7 logic:
4453 ;; \0numx -> octal (up to total 3 dig)
4454 ;; \DIGIT -> backref unless \0
4455 ;; \DIGITs -> backref if valid
4456 ;; otherwise up to 3 -> octal
4457 ;; Do not try to distinguish, we guess
4458 ((or (and (memq qtag (append "01234567" nil))
4459 (re-search-forward
4460 "\\=[01234567]?[01234567]?"
4461 (1- e) 'to-end))
4462 (and (memq qtag (append "89" nil))
4463 (re-search-forward
4464 "\\=[0123456789]*" (1- e) 'to-end))
4465 (and (eq qtag ?x)
4466 (re-search-forward
4467 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4468 (1- e) 'to-end))
4469 (and (memq qtag (append "pPN" nil))
4470 (re-search-forward "\\={[^{}]+}\\|."
4471 (1- e) 'to-end))
4472 (eq (char-syntax qtag) ?w))
4473 (cperl-postpone-fontification
4474 (1- REx-subgr-start) (point)
4475 'face my-cperl-REx-length1-face))))
4476 (setq was-subgr nil)) ; We do stuff here
4477 ((match-beginning 3) ; [charclass]
4478 (forward-char 1)
4479 (if (eq (char-after b) ?^ )
4480 (and (eq (following-char) ?\\ )
4481 (eq (char-after (cperl-1+ (point)))
4482 ?^ )
4483 (forward-char 2))
4484 (and (eq (following-char) ?^ )
4485 (forward-char 1)))
4486 (setq argument b ; continue?
4487 tag nil ; list of POSIX classes
4488 qtag (point))
4489 (if (eq (char-after b) ?\] )
4490 (and (eq (following-char) ?\\ )
4491 (eq (char-after (cperl-1+ (point)))
4492 ?\] )
4493 (setq qtag (1+ qtag))
4494 (forward-char 2))
4495 (and (eq (following-char) ?\] )
4496 (forward-char 1)))
4497 ;; Apparently, I can't put \] into a charclass
4498 ;; in m]]: m][\\\]\]] produces [\\]]
4499 ;;; POSIX? [:word:] [:^word:] only inside []
4500 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4501 (while
4502 (and argument
4503 (re-search-forward
4504 (if (eq (char-after b) ?\] )
4505 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4506 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4507 (1- e) 'toend))
4508 ;; Is this ] an end of POSIX class?
4509 (if (save-excursion
4510 (and
4511 (search-backward "[" argument t)
4512 (< REx-subgr-start (point))
4513 (not
4514 (and ; Should work with delim = \
4515 (eq (preceding-char) ?\\ )
4516 (= (% (skip-chars-backward
4517 "\\\\") 2) 0)))
4518 (looking-at
4519 (cond
4520 ((eq (char-after b) ?\] )
4521 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4522 ((eq (char-after b) ?\: )
4523 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4524 ((eq (char-after b) ?^ )
4525 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4526 ((eq (char-syntax (char-after b))
4527 ?w)
4528 (concat
4529 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4530 (char-to-string (char-after b))
4531 "\\|\\sw\\)+:\]"))
4532 (t "\\\\*\\[:\\^?\\sw*:]")))
4533 (setq argument (point))))
4534 (setq tag (cons (cons argument (point))
4535 tag)
4536 argument (point)) ; continue
4537 (setq argument nil)))
4538 (and argument
4539 (message "Couldn't find end of charclass in a REx, pos=%s"
4540 REx-subgr-start))
4541 (if (and cperl-use-syntax-table-text-property
4542 (> (- (point) 2) REx-subgr-start))
4543 (put-text-property
4544 (1+ REx-subgr-start) (1- (point))
4545 'syntax-table cperl-st-punct))
4546 (cperl-postpone-fontification
4547 REx-subgr-start qtag
4548 'face my-cperl-REx-spec-char-face)
4549 (cperl-postpone-fontification
4550 (1- (point)) (point) 'face
4551 my-cperl-REx-spec-char-face)
4552 (if (eq (char-after b) ?\] )
4553 (cperl-postpone-fontification
4554 (- (point) 2) (1- (point))
4555 'face my-cperl-REx-0length-face))
4556 (while tag
4557 (cperl-postpone-fontification
4558 (car (car tag)) (cdr (car tag))
4559 'face my-cperl-REx-length1-face)
4560 (setq tag (cdr tag)))
4561 (setq was-subgr nil)) ; did facing already
4562 ;; Now rare stuff:
4563 ((and (match-beginning 2) ; #-comment
4564 (/= (match-beginning 2) (match-end 2)))
4565 (beginning-of-line 2)
4566 (if (> (point) e)
4567 (goto-char (1- e))))
4568 ((match-beginning 4) ; character "]"
4569 (setq was-subgr nil) ; We do stuff here
4570 (goto-char (match-end 0))
4571 (if cperl-use-syntax-table-text-property
4572 (put-text-property
4573 (1- (point)) (point)
4574 'syntax-table cperl-st-punct))
4575 (cperl-postpone-fontification
4576 (1- (point)) (point)
4577 'face font-lock-warning-face))
4578 ((match-beginning 5) ; before (?{}) (??{})
4579 (setq tag (match-end 0))
4580 (if (or (setq qtag
4581 (cperl-forward-group-in-re st-l))
4582 (and (>= (point) e)
4583 (setq qtag "no matching `)' found"))
4584 (and (not (eq (char-after (- (point) 2))
4585 ?\} ))
4586 (setq qtag "Can't find })")))
4587 (progn
4588 (goto-char (1- e))
4589 (message "%s" qtag))
4590 (cperl-postpone-fontification
4591 (1- tag) (1- (point))
4592 'face font-lock-variable-name-face)
4593 (cperl-postpone-fontification
4594 REx-subgr-start (1- tag)
4595 'face my-cperl-REx-spec-char-face)
4596 (cperl-postpone-fontification
4597 (1- (point)) (point)
4598 'face my-cperl-REx-spec-char-face)
4599 (if cperl-use-syntax-table-text-property
4600 (progn
4601 (put-text-property
4602 (- (point) 2) (1- (point))
4603 'syntax-table cperl-st-cfence)
4604 (put-text-property
4605 (+ REx-subgr-start 2)
4606 (+ REx-subgr-start 3)
4607 'syntax-table cperl-st-cfence))))
4608 (setq was-subgr nil))
4609 (t ; (?#)-comment
4610 ;; Inside "(" and "\" arn't special in any way
4611 ;; Works also if the outside delimiters are ().
4612 (or;;(if (eq (char-after b) ?\) )
4613 ;;(re-search-forward
4614 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4615 ;; (1- e) 'toend)
4616 (search-forward ")" (1- e) 'toend)
4617 ;;)
4618 (message
4619 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4620 REx-subgr-start))))
4621 (if (>= (point) e)
4622 (goto-char (1- e)))
4623 (cond
4624 (was-subgr
4625 (setq REx-subgr-end (point))
4626 (cperl-commentify
4627 REx-subgr-start REx-subgr-end nil)
4628 (cperl-postpone-fontification
4629 REx-subgr-start REx-subgr-end
4630 'face font-lock-comment-face))))))
4631 (if (and is-REx is-x-REx)
4632 (put-text-property (1+ b) (1- e)
4633 'syntax-subtype 'x-REx)))
4634 (if (and i2 e1 b1 (> e1 b1))
4635 (progn ; No errors finding the second part...
4636 (cperl-postpone-fontification
4637 (1- e1) e1 'face my-cperl-delimiters-face)
4638 (if (and (not (eobp))
4639 (assoc (char-after b) cperl-starters))
4640 (progn
4641 (cperl-postpone-fontification
4642 b1 (1+ b1) 'face my-cperl-delimiters-face)
4643 (put-text-property b1 (1+ b1)
4644 'REx-part2 t)))))
4645 (if (> (point) max)
4646 (setq tmpend tb))))
4647 ((match-beginning 17) ; sub with prototype or attribute
4648 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4649 ;;"\\<sub\\>\\(" ;12
4650 ;; cperl-white-and-comment-rex ;13
4651 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4652 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4653 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4654 (setq b1 (match-beginning 14) e1 (match-end 14))
4655 (if (memq (char-after (1- b))
4656 '(?\$ ?\@ ?\% ?\& ?\*))
4657 nil
4658 (goto-char b)
4659 (if (eq (char-after (match-beginning 17)) ?\( )
4660 (progn
4661 (cperl-commentify ; Prototypes; mark as string
4662 (match-beginning 17) (match-end 17) t)
4663 (goto-char (match-end 0))
4664 ;; Now look for attributes after prototype:
4665 (forward-comment (buffer-size))
4666 (and (looking-at ":[^:]")
4667 (cperl-find-sub-attrs st-l b1 e1 b)))
4668 ;; treat attributes without prototype
4669 (goto-char (match-beginning 17))
4670 (cperl-find-sub-attrs st-l b1 e1 b))))
4671 ;; 1+6+2+1+1+6+1=18 extra () before this:
4672 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4673 ((match-beginning 19) ; old $abc'efg syntax
4674 (setq bb (match-end 0))
4675 ;;;(if (nth 3 state) nil ; in string
4676 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4677 (goto-char bb))
4678 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4679 ;; "__\\(END\\|DATA\\)__"
4680 ((match-beginning 20) ; __END__, __DATA__
4681 (setq bb (match-end 0))
4682 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4683 (cperl-commentify b bb nil)
4684 (setq end t))
4685 ;; "\\\\\\(['`\"($]\\)"
4686 ((match-beginning 21)
4687 ;; Trailing backslash; make non-quoting outside string/comment
4688 (setq bb (match-end 0))
4689 (goto-char b)
4690 (skip-chars-backward "\\\\")
4691 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4692 (cperl-modify-syntax-type b cperl-st-punct)
4693 (goto-char bb))
4694 (t (error "Error in regexp of the sniffer")))
4695 (if (> (point) stop-point)
4696 (progn
4697 (if end
4698 (message "Garbage after __END__/__DATA__ ignored")
4699 (message "Unbalanced syntax found while scanning")
4700 (or (car err-l) (setcar err-l b)))
4701 (goto-char stop-point))))
4702 (setq cperl-syntax-state (cons state-point state)
4703 ;; Do not mark syntax as done past tmpend???
4704 cperl-syntax-done-to (or tmpend (max (point) max)))
4705 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4706 )
4707 (if (car err-l) (goto-char (car err-l))
4708 (or non-inter
4709 (message "Scanning for \"hard\" Perl constructions... done"))))
4710 (and (buffer-modified-p)
4711 (not modified)
4712 (set-buffer-modified-p nil))
4713 ;; I do not understand what this is doing here. It breaks font-locking
4714 ;; because it resets the syntax-table from font-lock-syntax-table to
4715 ;; cperl-mode-syntax-table.
4716 ;; (set-syntax-table cperl-mode-syntax-table)
4717 )
4718 (list (car err-l) overshoot)))
4719
4720 (defun cperl-find-pods-heres-region (min max)
4721 (interactive "r")
4722 (cperl-find-pods-heres min max))
4723
4724 (defun cperl-backward-to-noncomment (lim)
4725 ;; Stops at lim or after non-whitespace that is not in comment
4726 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4727 (let (stop p pr)
4728 (while (and (not stop) (> (point) (or lim (point-min))))
4729 (skip-chars-backward " \t\n\f" lim)
4730 (setq p (point))
4731 (beginning-of-line)
4732 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4733 '(pod here-doc here-doc-delim))
4734 (progn
4735 (cperl-unwind-to-safe nil)
4736 (setq pr (get-text-property (point) 'syntax-type))))
4737 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4738 (not (memq pr '(string prestring))))
4739 (progn (cperl-to-comment-or-eol) (bolp))
4740 (progn
4741 (skip-chars-backward " \t")
4742 (if (< p (point)) (goto-char p))
4743 (setq stop t))))))
4744
4745 ;; Used only in `cperl-calculate-indent'...
4746 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4747 ;; Positions is before ?\{. Checks whether it starts a block.
4748 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4749 (cperl-backward-to-noncomment (point-min))
4750 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4751 ; Label may be mixed up with `$blah :'
4752 (save-excursion (cperl-after-label))
4753 (get-text-property (cperl-1- (point)) 'attrib-group)
4754 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4755 (progn
4756 (backward-sexp)
4757 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4758 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4759 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4760 ;; sub bless::foo {}
4761 (progn
4762 (cperl-backward-to-noncomment (point-min))
4763 (and (eq (preceding-char) ?b)
4764 (progn
4765 (forward-sexp -1)
4766 (looking-at "sub[ \t\n\f#]")))))))))
4767
4768 ;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4769 ;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4770 ;;; may be a part of an in-statement construct, such as
4771 ;;; ${something()}, print {FH} $data.
4772 ;;; Moreover, one takes positive approach (looks for else,grep etc)
4773 ;;; another negative (looks for bless,tr etc)
4774 (defun cperl-after-block-p (lim &optional pre-block)
4775 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4776 Would not look before LIM. Assumes that LIM is a good place to begin a
4777 statement. The kind of block we treat here is one after which a new
4778 statement would start; thus the block in ${func()} does not count."
4779 (save-excursion
4780 (condition-case nil
4781 (progn
4782 (or pre-block (forward-sexp -1))
4783 (cperl-backward-to-noncomment lim)
4784 (or (eq (point) lim)
4785 ;; if () {} // sub f () {} // sub f :a(') {}
4786 (eq (preceding-char) ?\) )
4787 ;; label: {}
4788 (save-excursion (cperl-after-label))
4789 ;; sub :attr {}
4790 (get-text-property (cperl-1- (point)) 'attrib-group)
4791 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4792 (save-excursion
4793 (forward-sexp -1)
4794 ;; else {} but not else::func {}
4795 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4796 (not (looking-at "\\(\\sw\\|_\\)+::")))
4797 ;; sub f {}
4798 (progn
4799 (cperl-backward-to-noncomment lim)
4800 (and (eq (preceding-char) ?b)
4801 (progn
4802 (forward-sexp -1)
4803 (looking-at "sub[ \t\n\f#]"))))))
4804 ;; What preceeds is not word... XXXX Last statement in sub???
4805 (cperl-after-expr-p lim))))
4806 (error nil))))
4807
4808 (defun cperl-after-expr-p (&optional lim chars test)
4809 "Return true if the position is good for start of expression.
4810 TEST is the expression to evaluate at the found position. If absent,
4811 CHARS is a string that contains good characters to have before us (however,
4812 `}' is treated \"smartly\" if it is not in the list)."
4813 (let ((lim (or lim (point-min)))
4814 stop p pr)
4815 (cperl-update-syntaxification (point) (point))
4816 (save-excursion
4817 (while (and (not stop) (> (point) lim))
4818 (skip-chars-backward " \t\n\f" lim)
4819 (setq p (point))
4820 (beginning-of-line)
4821 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4822 ;; '(pod here-doc here-doc-delim))
4823 (if (get-text-property (point) 'here-doc-group)
4824 (progn
4825 (goto-char
4826 (cperl-beginning-of-property (point) 'here-doc-group))
4827 (beginning-of-line 0)))
4828 (if (get-text-property (point) 'in-pod)
4829 (progn
4830 (goto-char
4831 (cperl-beginning-of-property (point) 'in-pod))
4832 (beginning-of-line 0)))
4833 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4834 ;; Else: last iteration, or a label
4835 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4836 (skip-chars-backward " \t")
4837 (if (< p (point)) (goto-char p))
4838 (setq p (point))
4839 (if (and (eq (preceding-char) ?:)
4840 (progn
4841 (forward-char -1)
4842 (skip-chars-backward " \t\n\f" lim)
4843 (memq (char-syntax (preceding-char)) '(?w ?_))))
4844 (forward-sexp -1) ; Possibly label. Skip it
4845 (goto-char p)
4846 (setq stop t))))
4847 (or (bobp) ; ???? Needed
4848 (eq (point) lim)
4849 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4850 (progn
4851 (if test (eval test)
4852 (or (memq (preceding-char) (append (or chars "{;") nil))
4853 (and (eq (preceding-char) ?\})
4854 (cperl-after-block-p lim))
4855 (and (eq (following-char) ?.) ; in format: see comment above
4856 (eq (get-text-property (point) 'syntax-type)
4857 'format)))))))))
4858
4859 (defun cperl-backward-to-start-of-expr (&optional lim)
4860 (condition-case nil
4861 (progn
4862 (while (and (or (not lim)
4863 (> (point) lim))
4864 (not (cperl-after-expr-p lim)))
4865 (forward-sexp -1)
4866 ;; May be after $, @, $# etc of a variable
4867 (skip-chars-backward "$@%#")))
4868 (error nil)))
4869
4870 (defun cperl-at-end-of-expr (&optional lim)
4871 ;; Since the SEXP approach below is very fragile, do some overengineering
4872 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4873 (condition-case nil
4874 (save-excursion
4875 ;; If nothing interesting after, does as (forward-sexp -1);
4876 ;; otherwise fails, or ends at a start of following sexp.
4877 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4878 ;; may be stuck after @ or $; just put some stupid workaround now:
4879 (let ((p (point)))
4880 (forward-sexp 1)
4881 (forward-sexp -1)
4882 (while (memq (preceding-char) (append "%&@$*" nil))
4883 (forward-char -1))
4884 (or (< (point) p)
4885 (cperl-after-expr-p lim))))
4886 (error t))))
4887
4888 (defun cperl-forward-to-end-of-expr (&optional lim)
4889 (let ((p (point))))
4890 (condition-case nil
4891 (progn
4892 (while (and (< (point) (or lim (point-max)))
4893 (not (cperl-at-end-of-expr)))
4894 (forward-sexp 1)))
4895 (error nil)))
4896
4897 (defun cperl-backward-to-start-of-continued-exp (lim)
4898 (if (memq (preceding-char) (append ")]}\"'`" nil))
4899 (forward-sexp -1))
4900 (beginning-of-line)
4901 (if (<= (point) lim)
4902 (goto-char (1+ lim)))
4903 (skip-chars-forward " \t"))
4904
4905 (defun cperl-after-block-and-statement-beg (lim)
4906 ;; We assume that we are after ?\}
4907 (and
4908 (cperl-after-block-p lim)
4909 (save-excursion
4910 (forward-sexp -1)
4911 (cperl-backward-to-noncomment (point-min))
4912 (or (bobp)
4913 (eq (point) lim)
4914 (not (= (char-syntax (preceding-char)) ?w))
4915 (progn
4916 (forward-sexp -1)
4917 (not
4918 (looking-at
4919 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4920
4921 \f
4922 (defun cperl-indent-exp ()
4923 "Simple variant of indentation of continued-sexp.
4924
4925 Will not indent comment if it starts at `comment-indent' or looks like
4926 continuation of the comment on the previous line.
4927
4928 If `cperl-indent-region-fix-constructs', will improve spacing on
4929 conditional/loop constructs."
4930 (interactive)
4931 (save-excursion
4932 (let ((tmp-end (progn (end-of-line) (point))) top done)
4933 (save-excursion
4934 (beginning-of-line)
4935 (while (null done)
4936 (setq top (point))
4937 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
4938 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
4939 (setq top (point))) ; Get the outermost parenths in line
4940 (goto-char top)
4941 (while (< (point) tmp-end)
4942 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
4943 (or (eolp) (forward-sexp 1)))
4944 (if (> (point) tmp-end) ; Yes, there an unfinished block
4945 nil
4946 (if (eq ?\) (preceding-char))
4947 (progn ;; Plan B: find by REGEXP block followup this line
4948 (setq top (point))
4949 (condition-case nil
4950 (progn
4951 (forward-sexp -2)
4952 (if (eq (following-char) ?$ ) ; for my $var (list)
4953 (progn
4954 (forward-sexp -1)
4955 (if (looking-at "\\(my\\|local\\|our\\)\\>")
4956 (forward-sexp -1))))
4957 (if (looking-at
4958 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
4959 "\\|for\\(each\\)?\\>\\(\\("
4960 cperl-maybe-white-and-comment-rex
4961 "\\(my\\|local\\|our\\)\\)?"
4962 cperl-maybe-white-and-comment-rex
4963 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
4964 (progn
4965 (goto-char top)
4966 (forward-sexp 1)
4967 (setq top (point)))))
4968 (error (setq done t)))
4969 (goto-char top))
4970 (if (looking-at ; Try Plan C: continuation block
4971 (concat cperl-maybe-white-and-comment-rex
4972 "\\<\\(else\\|elsif\|continue\\)\\>"))
4973 (progn
4974 (goto-char (match-end 0))
4975 (save-excursion
4976 (end-of-line)
4977 (setq tmp-end (point))))
4978 (setq done t))))
4979 (save-excursion
4980 (end-of-line)
4981 (setq tmp-end (point))))
4982 (goto-char tmp-end)
4983 (setq tmp-end (point-marker)))
4984 (if cperl-indent-region-fix-constructs
4985 (cperl-fix-line-spacing tmp-end))
4986 (cperl-indent-region (point) tmp-end))))
4987
4988 (defun cperl-fix-line-spacing (&optional end parse-data)
4989 "Improve whitespace in a conditional/loop construct.
4990 Returns some position at the last line."
4991 (interactive)
4992 (or end
4993 (setq end (point-max)))
4994 (let ((ee (save-excursion (end-of-line) (point)))
4995 (cperl-indent-region-fix-constructs
4996 (or cperl-indent-region-fix-constructs 1))
4997 p pp ml have-brace ret)
4998 (save-excursion
4999 (beginning-of-line)
5000 (setq ret (point))
5001 ;; }? continue
5002 ;; blah; }
5003 (if (not
5004 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5005 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5006 nil ; Do not need to do anything
5007 ;; Looking at:
5008 ;; }
5009 ;; else
5010 (if cperl-merge-trailing-else
5011 (if (looking-at
5012 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5013 (progn
5014 (search-forward "}")
5015 (setq p (point))
5016 (skip-chars-forward " \t\n")
5017 (delete-region p (point))
5018 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5019 (beginning-of-line)))
5020 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5021 (save-excursion
5022 (search-forward "}")
5023 (delete-horizontal-space)
5024 (insert "\n")
5025 (setq ret (point))
5026 (if (cperl-indent-line parse-data)
5027 (progn
5028 (cperl-fix-line-spacing end parse-data)
5029 (setq ret (point)))))))
5030 ;; Looking at:
5031 ;; } else
5032 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5033 (progn
5034 (search-forward "}")
5035 (delete-horizontal-space)
5036 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5037 (beginning-of-line)))
5038 ;; Looking at:
5039 ;; else {
5040 (if (looking-at
5041 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5042 (progn
5043 (forward-word 1)
5044 (delete-horizontal-space)
5045 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5046 (beginning-of-line)))
5047 ;; Looking at:
5048 ;; foreach my $var
5049 (if (looking-at
5050 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5051 (progn
5052 (forward-word 2)
5053 (delete-horizontal-space)
5054 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5055 (beginning-of-line)))
5056 ;; Looking at:
5057 ;; foreach my $var (
5058 (if (looking-at
5059 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5060 (progn
5061 (forward-sexp 3)
5062 (delete-horizontal-space)
5063 (insert
5064 (make-string cperl-indent-region-fix-constructs ?\s))
5065 (beginning-of-line)))
5066 ;; Looking at (with or without "}" at start, ending after "({"):
5067 ;; } foreach my $var () OR {
5068 (if (looking-at
5069 "[ \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]*{")
5070 (progn
5071 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5072 (re-search-forward "[({]")
5073 (forward-char -1)
5074 (setq p (point))
5075 (if (eq (following-char) ?\( )
5076 (progn
5077 (forward-sexp 1)
5078 (setq pp (point))) ; past parenth-group
5079 ;; after `else' or nothing
5080 (if ml ; after `else'
5081 (skip-chars-backward " \t\n")
5082 (beginning-of-line))
5083 (setq pp nil))
5084 ;; Now after the sexp before the brace
5085 ;; Multiline expr should be special
5086 (setq ml (and pp (save-excursion (goto-char p)
5087 (search-forward "\n" pp t))))
5088 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5089 (looking-at "[ \t\n]*{"))
5090 (progn
5091 (cond
5092 ((bolp) ; Were before `{', no if/else/etc
5093 nil)
5094 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5095 (delete-horizontal-space)
5096 (if (if ml
5097 cperl-extra-newline-before-brace-multiline
5098 cperl-extra-newline-before-brace)
5099 (progn
5100 (delete-horizontal-space)
5101 (insert "\n")
5102 (setq ret (point))
5103 (if (cperl-indent-line parse-data)
5104 (progn
5105 (cperl-fix-line-spacing end parse-data)
5106 (setq ret (point)))))
5107 (insert
5108 (make-string cperl-indent-region-fix-constructs ?\s))))
5109 ((and (looking-at "[ \t]*\n")
5110 (not (if ml
5111 cperl-extra-newline-before-brace-multiline
5112 cperl-extra-newline-before-brace)))
5113 (setq pp (point))
5114 (skip-chars-forward " \t\n")
5115 (delete-region pp (point))
5116 (insert
5117 (make-string cperl-indent-region-fix-constructs ?\ )))
5118 ((and (looking-at "[\t ]*{")
5119 (if ml cperl-extra-newline-before-brace-multiline
5120 cperl-extra-newline-before-brace))
5121 (delete-horizontal-space)
5122 (insert "\n")
5123 (setq ret (point))
5124 (if (cperl-indent-line parse-data)
5125 (progn
5126 (cperl-fix-line-spacing end parse-data)
5127 (setq ret (point))))))
5128 ;; Now we are before `{'
5129 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5130 (progn
5131 (skip-chars-forward " \t\n")
5132 (setq pp (point))
5133 (forward-sexp 1)
5134 (setq p (point))
5135 (goto-char pp)
5136 (setq ml (search-forward "\n" p t))
5137 (if (or cperl-break-one-line-blocks-when-indent ml)
5138 ;; not good: multi-line BLOCK
5139 (progn
5140 (goto-char (1+ pp))
5141 (delete-horizontal-space)
5142 (insert "\n")
5143 (setq ret (point))
5144 (if (cperl-indent-line parse-data)
5145 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5146 (beginning-of-line)
5147 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5148 ;; Now check whether there is a hanging `}'
5149 ;; Looking at:
5150 ;; } blah
5151 (if (and
5152 cperl-fix-hanging-brace-when-indent
5153 have-brace
5154 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5155 (condition-case nil
5156 (progn
5157 (up-list 1)
5158 (if (and (<= (point) pp)
5159 (eq (preceding-char) ?\} )
5160 (cperl-after-block-and-statement-beg (point-min)))
5161 t
5162 (goto-char p)
5163 nil))
5164 (error nil)))
5165 (progn
5166 (forward-char -1)
5167 (skip-chars-backward " \t")
5168 (if (bolp)
5169 ;; `}' was the first thing on the line, insert NL *after* it.
5170 (progn
5171 (cperl-indent-line parse-data)
5172 (search-forward "}")
5173 (delete-horizontal-space)
5174 (insert "\n"))
5175 (delete-horizontal-space)
5176 (or (eq (preceding-char) ?\;)
5177 (bolp)
5178 (and (eq (preceding-char) ?\} )
5179 (cperl-after-block-p (point-min)))
5180 (insert ";"))
5181 (insert "\n")
5182 (setq ret (point)))
5183 (if (cperl-indent-line parse-data)
5184 (setq ret (cperl-fix-line-spacing end parse-data)))
5185 (beginning-of-line)))))
5186 ret))
5187
5188 (defvar cperl-update-start) ; Do not need to make them local
5189 (defvar cperl-update-end)
5190 (defun cperl-delay-update-hook (beg end old-len)
5191 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5192 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5193
5194 (defun cperl-indent-region (start end)
5195 "Simple variant of indentation of region in CPerl mode.
5196 Should be slow. Will not indent comment if it starts at `comment-indent'
5197 or looks like continuation of the comment on the previous line.
5198 Indents all the lines whose first character is between START and END
5199 inclusive.
5200
5201 If `cperl-indent-region-fix-constructs', will improve spacing on
5202 conditional/loop constructs."
5203 (interactive "r")
5204 (cperl-update-syntaxification end end)
5205 (save-excursion
5206 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5207 (let ((indent-info (if cperl-emacs-can-parse
5208 (list nil nil nil) ; Cannot use '(), since will modify
5209 nil))
5210 (pm 0)
5211 after-change-functions ; Speed it up!
5212 st comm old-comm-indent new-comm-indent p pp i empty)
5213 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5214 (goto-char start)
5215 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5216 (current-column))
5217 new-comm-indent old-comm-indent)
5218 (goto-char start)
5219 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5220 (or (bolp) (beginning-of-line 2))
5221 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5222 (setq st (point))
5223 (if (or
5224 (setq empty (looking-at "[ \t]*\n"))
5225 (and (setq comm (looking-at "[ \t]*#"))
5226 (or (eq (current-indentation) (or old-comm-indent
5227 comment-column))
5228 (setq old-comm-indent nil))))
5229 (if (and old-comm-indent
5230 (not empty)
5231 (= (current-indentation) old-comm-indent)
5232 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5233 (not (eq (get-text-property (point) 'syntax-table)
5234 cperl-st-cfence)))
5235 (let ((comment-column new-comm-indent))
5236 (indent-for-comment)))
5237 (progn
5238 (setq i (cperl-indent-line indent-info))
5239 (or comm
5240 (not i)
5241 (progn
5242 (if cperl-indent-region-fix-constructs
5243 (goto-char (cperl-fix-line-spacing end indent-info)))
5244 (if (setq old-comm-indent
5245 (and (cperl-to-comment-or-eol)
5246 (not (memq (get-text-property (point)
5247 'syntax-type)
5248 '(pod here-doc)))
5249 (not (eq (get-text-property (point)
5250 'syntax-table)
5251 cperl-st-cfence))
5252 (current-column)))
5253 (progn (indent-for-comment)
5254 (skip-chars-backward " \t")
5255 (skip-chars-backward "#")
5256 (setq new-comm-indent (current-column))))))))
5257 (beginning-of-line 2)))
5258 ;; Now run the update hooks
5259 (and after-change-functions
5260 cperl-update-end
5261 (save-excursion
5262 (goto-char cperl-update-end)
5263 (insert " ")
5264 (delete-char -1)
5265 (goto-char cperl-update-start)
5266 (insert " ")
5267 (delete-char -1))))))
5268
5269 ;; Stolen from lisp-mode with a lot of improvements
5270
5271 (defun cperl-fill-paragraph (&optional justify iteration)
5272 "Like `fill-paragraph', but handle CPerl comments.
5273 If any of the current line is a comment, fill the comment or the
5274 block of it that point is in, preserving the comment's initial
5275 indentation and initial hashes. Behaves usually outside of comment."
5276 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5277 (let (;; Non-nil if the current line contains a comment.
5278 has-comment
5279 fill-paragraph-function ; do not recurse
5280 ;; If has-comment, the appropriate fill-prefix for the comment.
5281 comment-fill-prefix
5282 ;; Line that contains code and comment (or nil)
5283 start
5284 c spaces len dc (comment-column comment-column))
5285 ;; Figure out what kind of comment we are looking at.
5286 (save-excursion
5287 (beginning-of-line)
5288 (cond
5289
5290 ;; A line with nothing but a comment on it?
5291 ((looking-at "[ \t]*#[# \t]*")
5292 (setq has-comment t
5293 comment-fill-prefix (buffer-substring (match-beginning 0)
5294 (match-end 0))))
5295
5296 ;; A line with some code, followed by a comment? Remember that the
5297 ;; semi which starts the comment shouldn't be part of a string or
5298 ;; character.
5299 ((cperl-to-comment-or-eol)
5300 (setq has-comment t)
5301 (looking-at "#+[ \t]*")
5302 (setq start (point) c (current-column)
5303 comment-fill-prefix
5304 (concat (make-string (current-column) ?\s)
5305 (buffer-substring (match-beginning 0) (match-end 0)))
5306 spaces (progn (skip-chars-backward " \t")
5307 (buffer-substring (point) start))
5308 dc (- c (current-column)) len (- start (point))
5309 start (point-marker))
5310 (delete-char len)
5311 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5312 (if (not has-comment)
5313 (fill-paragraph justify) ; Do the usual thing outside of comment
5314 ;; Narrow to include only the comment, and then fill the region.
5315 (save-restriction
5316 (narrow-to-region
5317 ;; Find the first line we should include in the region to fill.
5318 (if start (progn (beginning-of-line) (point))
5319 (save-excursion
5320 (while (and (zerop (forward-line -1))
5321 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5322 ;; We may have gone to far. Go forward again.
5323 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5324 (forward-line 1))
5325 (point)))
5326 ;; Find the beginning of the first line past the region to fill.
5327 (save-excursion
5328 (while (progn (forward-line 1)
5329 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5330 (point)))
5331 ;; Remove existing hashes
5332 (save-excursion
5333 (goto-char (point-min))
5334 (while (progn (forward-line 1) (< (point) (point-max)))
5335 (skip-chars-forward " \t")
5336 (if (looking-at "#+")
5337 (progn
5338 (if (and (eq (point) (match-beginning 0))
5339 (not (eq (point) (match-end 0)))) nil
5340 (error
5341 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5342 (delete-char (- (match-end 0) (match-beginning 0)))))))
5343
5344 ;; Lines with only hashes on them can be paragraph boundaries.
5345 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5346 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5347 (fill-prefix comment-fill-prefix))
5348 (fill-paragraph justify)))
5349 (if (and start)
5350 (progn
5351 (goto-char start)
5352 (if (> dc 0)
5353 (progn (delete-char dc) (insert spaces)))
5354 (if (or (= (current-column) c) iteration) nil
5355 (setq comment-column c)
5356 (indent-for-comment)
5357 ;; Repeat once more, flagging as iteration
5358 (cperl-fill-paragraph justify t))))))
5359 t)
5360
5361 (defun cperl-do-auto-fill ()
5362 ;; Break out if the line is short enough
5363 (if (> (save-excursion
5364 (end-of-line)
5365 (current-column))
5366 fill-column)
5367 (let ((c (save-excursion (beginning-of-line)
5368 (cperl-to-comment-or-eol) (point)))
5369 (s (memq (following-char) '(?\s ?\t))) marker)
5370 (if (>= c (point))
5371 ;; Don't break line inside code: only inside comment.
5372 nil
5373 (setq marker (point-marker))
5374 (fill-paragraph nil)
5375 (goto-char marker)
5376 ;; Is not enough, sometimes marker is a start of line
5377 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5378 (goto-char (match-end 0))))
5379 ;; Following space could have gone:
5380 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5381 (insert " ")
5382 (backward-char 1))
5383 ;; Previous space could have gone:
5384 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5385
5386 (defun cperl-imenu-addback (lst &optional isback name)
5387 ;; We suppose that the lst is a DAG, unless the first element only
5388 ;; loops back, and ISBACK is set. Thus this function cannot be
5389 ;; applied twice without ISBACK set.
5390 (cond ((not cperl-imenu-addback) lst)
5391 (t
5392 (or name
5393 (setq name "+++BACK+++"))
5394 (mapc (lambda (elt)
5395 (if (and (listp elt) (listp (cdr elt)))
5396 (progn
5397 ;; In the other order it goes up
5398 ;; one level only ;-(
5399 (setcdr elt (cons (cons name lst)
5400 (cdr elt)))
5401 (cperl-imenu-addback (cdr elt) t name))))
5402 (if isback (cdr lst) lst))
5403 lst)))
5404
5405 (defun cperl-imenu--create-perl-index (&optional regexp)
5406 (require 'imenu) ; May be called from TAGS creator
5407 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5408 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5409 (index-meth-alist '()) meth
5410 packages ends-ranges p marker is-proto
5411 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5412 (goto-char (point-min))
5413 (cperl-update-syntaxification (point-max) (point-max))
5414 ;; Search for the function
5415 (progn ;;save-match-data
5416 (while (re-search-forward
5417 (or regexp cperl-imenu--function-name-regexp-perl)
5418 nil t)
5419 ;; 2=package-group, 5=package-name 8=sub-name
5420 (cond
5421 ((and ; Skip some noise if building tags
5422 (match-beginning 5) ; package name
5423 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5424 (not (save-match-data
5425 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5426 nil)
5427 ((and
5428 (or (match-beginning 2)
5429 (match-beginning 8)) ; package or sub
5430 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5431 (null (get-text-property (match-beginning 1) 'syntax-table))
5432 (null (get-text-property (match-beginning 1) 'syntax-type))
5433 (null (get-text-property (match-beginning 1) 'in-pod)))
5434 (setq is-pack (match-beginning 2))
5435 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5436 ;; (goto-char (match-end 0))) ; Messes what follows
5437 (setq meth nil
5438 p (point))
5439 (while (and ends-ranges (>= p (car ends-ranges)))
5440 ;; delete obsolete entries
5441 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5442 (setq package (or (car packages) "")
5443 end-range (or (car ends-ranges) 0))
5444 (if is-pack ; doing "package"
5445 (progn
5446 (if (match-beginning 5) ; named package
5447 (setq name (buffer-substring (match-beginning 5)
5448 (match-end 5))
5449 name (progn
5450 (set-text-properties 0 (length name) nil name)
5451 name)
5452 package (concat name "::")
5453 name (concat "package " name))
5454 ;; Support nameless packages
5455 (setq name "package;" package ""))
5456 (setq end-range
5457 (save-excursion
5458 (parse-partial-sexp (point) (point-max) -1) (point))
5459 ends-ranges (cons end-range ends-ranges)
5460 packages (cons package packages)))
5461 (setq is-proto
5462 (or (eq (following-char) ?\;)
5463 (eq 0 (get-text-property (point) 'attrib-group)))))
5464 ;; Skip this function name if it is a prototype declaration.
5465 (if (and is-proto (not is-pack)) nil
5466 (or is-pack
5467 (setq name
5468 (buffer-substring (match-beginning 8) (match-end 8)))
5469 (set-text-properties 0 (length name) nil name))
5470 (setq marker (make-marker))
5471 (set-marker marker (match-end (if is-pack 2 8)))
5472 (cond (is-pack nil)
5473 ((string-match "[:']" name)
5474 (setq meth t))
5475 ((> p end-range) nil)
5476 (t
5477 (setq name (concat package name) meth t)))
5478 (setq index (cons name marker))
5479 (if is-pack
5480 (push index index-pack-alist)
5481 (push index index-alist))
5482 (if meth (push index index-meth-alist))
5483 (push index index-unsorted-alist)))
5484 ((match-beginning 16) ; POD section
5485 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5486 marker (make-marker))
5487 (set-marker marker (match-beginning 17))
5488 (set-text-properties 0 (length name) nil name)
5489 (setq name (concat (make-string
5490 (* 3 (- (char-after (match-beginning 16)) ?1))
5491 ?\ )
5492 name)
5493 index (cons name marker))
5494 (setq index1 (cons (concat "=" name) (cdr index)))
5495 (push index index-pod-alist)
5496 (push index1 index-unsorted-alist)))))
5497 (setq index-alist
5498 (if (default-value 'imenu-sort-function)
5499 (sort index-alist (default-value 'imenu-sort-function))
5500 (nreverse index-alist)))
5501 (and index-pod-alist
5502 (push (cons "+POD headers+..."
5503 (nreverse index-pod-alist))
5504 index-alist))
5505 (and (or index-pack-alist index-meth-alist)
5506 (let ((lst index-pack-alist) hier-list pack elt group name)
5507 ;; Remove "package ", reverse and uniquify.
5508 (while lst
5509 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5510 (if (assoc name hier-list) nil
5511 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5512 (setq lst index-meth-alist)
5513 (while lst
5514 (setq elt (car lst) lst (cdr lst))
5515 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5516 (setq pack (substring (car elt) 0 (match-beginning 0)))
5517 (if (setq group (assoc pack hier-list))
5518 (if (listp (cdr group))
5519 ;; Have some functions already
5520 (setcdr group
5521 (cons (cons (substring
5522 (car elt)
5523 (+ 2 (match-beginning 0)))
5524 (cdr elt))
5525 (cdr group)))
5526 (setcdr group (list (cons (substring
5527 (car elt)
5528 (+ 2 (match-beginning 0)))
5529 (cdr elt)))))
5530 (setq hier-list
5531 (cons (cons pack
5532 (list (cons (substring
5533 (car elt)
5534 (+ 2 (match-beginning 0)))
5535 (cdr elt))))
5536 hier-list))))))
5537 (push (cons "+Hierarchy+..."
5538 hier-list)
5539 index-alist)))
5540 (and index-pack-alist
5541 (push (cons "+Packages+..."
5542 (nreverse index-pack-alist))
5543 index-alist))
5544 (and (or index-pack-alist index-pod-alist
5545 (default-value 'imenu-sort-function))
5546 index-unsorted-alist
5547 (push (cons "+Unsorted List+..."
5548 (nreverse index-unsorted-alist))
5549 index-alist))
5550 (cperl-imenu-addback index-alist)))
5551
5552 \f
5553 ;; Suggested by Mark A. Hershberger
5554 (defun cperl-outline-level ()
5555 (looking-at outline-regexp)
5556 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5557 ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5558 ((match-beginning 2) 0) ; package
5559 ((match-beginning 8) 1) ; sub
5560 ((match-beginning 16)
5561 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5562 (t 5))) ; should not happen
5563
5564 \f
5565 (defun cperl-windowed-init ()
5566 "Initialization under windowed version."
5567 (cond ((featurep 'ps-print)
5568 (or cperl-faces-init
5569 (progn
5570 (and (boundp 'font-lock-multiline)
5571 (setq cperl-font-lock-multiline t))
5572 (cperl-init-faces))))
5573 ((not cperl-faces-init)
5574 (add-hook 'font-lock-mode-hook
5575 (function
5576 (lambda ()
5577 (if (memq major-mode '(perl-mode cperl-mode))
5578 (progn
5579 (or cperl-faces-init (cperl-init-faces)))))))
5580 (if (fboundp 'eval-after-load)
5581 (eval-after-load
5582 "ps-print"
5583 '(or cperl-faces-init (cperl-init-faces)))))))
5584
5585 (defvar cperl-font-lock-keywords-1 nil
5586 "Additional expressions to highlight in Perl mode. Minimal set.")
5587 (defvar cperl-font-lock-keywords nil
5588 "Additional expressions to highlight in Perl mode. Default set.")
5589 (defvar cperl-font-lock-keywords-2 nil
5590 "Additional expressions to highlight in Perl mode. Maximal set")
5591
5592 (defun cperl-load-font-lock-keywords ()
5593 (or cperl-faces-init (cperl-init-faces))
5594 cperl-font-lock-keywords)
5595
5596 (defun cperl-load-font-lock-keywords-1 ()
5597 (or cperl-faces-init (cperl-init-faces))
5598 cperl-font-lock-keywords-1)
5599
5600 (defun cperl-load-font-lock-keywords-2 ()
5601 (or cperl-faces-init (cperl-init-faces))
5602 cperl-font-lock-keywords-2)
5603
5604 (defun cperl-init-faces-weak ()
5605 ;; Allow `cperl-find-pods-heres' to run.
5606 (or (boundp 'font-lock-constant-face)
5607 (cperl-force-face font-lock-constant-face
5608 "Face for constant and label names"))
5609 (or (boundp 'font-lock-warning-face)
5610 (cperl-force-face font-lock-warning-face
5611 "Face for things which should stand out"))
5612 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5613 )
5614
5615 (defun cperl-init-faces ()
5616 (condition-case errs
5617 (progn
5618 (require 'font-lock)
5619 (and (fboundp 'font-lock-fontify-anchored-keywords)
5620 (featurep 'font-lock-extra)
5621 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5622 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5623 (if (fboundp 'font-lock-fontify-anchored-keywords)
5624 (setq font-lock-anchored t))
5625 (setq
5626 t-font-lock-keywords
5627 (list
5628 `("[ \t]+$" 0 ',cperl-invalid-face t)
5629 (cons
5630 (concat
5631 "\\(^\\|[^$@%&\\]\\)\\<\\("
5632 (mapconcat
5633 'identity
5634 '("if" "until" "while" "elsif" "else" "unless" "for"
5635 "foreach" "continue" "exit" "die" "last" "goto" "next"
5636 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5637 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5638 "\\|") ; Flow control
5639 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5640 ; In what follows we use `type' style
5641 ; for overwritable builtins
5642 (list
5643 (concat
5644 "\\(^\\|[^$@%&\\]\\)\\<\\("
5645 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5646 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5647 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5648 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5649 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5650 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5651 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5652 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5653 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5654 ;; "gethostbyname" "gethostent" "getlogin"
5655 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5656 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5657 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5658 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5659 ;; "getservbyport" "getservent" "getsockname"
5660 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5661 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5662 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5663 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5664 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5665 ;; "quotemeta" "rand" "read" "readdir" "readline"
5666 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5667 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5668 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5669 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5670 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5671 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5672 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5673 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5674 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5675 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5676 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5677 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5678 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5679 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5680 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5681 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5682 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5683 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5684 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5685 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5686 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5687 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5688 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5689 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5690 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5691 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5692 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5693 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5694 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5695 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5696 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5697 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5698 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5699 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5700 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5701 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5702 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5703 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5704 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5705 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5706 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5707 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5708 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5709 "\\)\\>") 2 'font-lock-type-face)
5710 ;; In what follows we use `other' style
5711 ;; for nonoverwritable builtins
5712 ;; Somehow 's', 'm' are not auto-generated???
5713 (list
5714 (concat
5715 "\\(^\\|[^$@%&\\]\\)\\<\\("
5716 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5717 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5718 ;; "eval" "exists" "for" "foreach" "format" "goto"
5719 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5720 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5721 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5722 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5723 ;; "undef" "unless" "unshift" "untie" "until" "use"
5724 ;; "while" "y"
5725 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5726 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5727 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5728 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5729 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5730 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5731 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5732 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5733 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5734 "\\|[sm]" ; Added manually
5735 "\\)\\>") 2 'cperl-nonoverridable-face)
5736 ;; (mapconcat 'identity
5737 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5738 ;; "#include" "#define" "#undef")
5739 ;; "\\|")
5740 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5741 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5742 ;; This highlights declarations and definitions differenty.
5743 ;; We do not try to highlight in the case of attributes:
5744 ;; it is already done by `cperl-find-pods-heres'
5745 (list (concat "\\<sub"
5746 cperl-white-and-comment-rex ; whitespace/comments
5747 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5748 "\\("
5749 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5750 "([^()]*)\\)?" ; prototype
5751 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5752 "[{;]")
5753 2 (if cperl-font-lock-multiline
5754 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5755 'font-lock-function-name-face
5756 'font-lock-variable-name-face)
5757 ;; need to manually set 'multiline' for older font-locks
5758 '(progn
5759 (if (< 1 (count-lines (match-beginning 0)
5760 (match-end 0)))
5761 (put-text-property
5762 (+ 3 (match-beginning 0)) (match-end 0)
5763 'syntax-type 'multiline))
5764 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5765 'font-lock-function-name-face
5766 'font-lock-variable-name-face))))
5767 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5768 2 font-lock-function-name-face)
5769 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5770 1 font-lock-function-name-face)
5771 (cond ((featurep 'font-lock-extra)
5772 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5773 (2 font-lock-string-face t)
5774 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5775 (font-lock-anchored
5776 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5777 (2 font-lock-string-face t)
5778 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5779 nil nil
5780 (1 font-lock-string-face t))))
5781 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5782 2 font-lock-string-face t)))
5783 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5784 font-lock-string-face t)
5785 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5786 font-lock-constant-face) ; labels
5787 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5788 2 font-lock-constant-face)
5789 ;; Uncomment to get perl-mode-like vars
5790 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5791 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5792 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5793 (cond ((featurep 'font-lock-extra)
5794 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5795 (3 font-lock-variable-name-face)
5796 (4 '(another 4 nil
5797 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5798 (1 font-lock-variable-name-face)
5799 (2 '(restart 2 nil) nil t)))
5800 nil t))) ; local variables, multiple
5801 (font-lock-anchored
5802 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5803 `(,(concat "\\<\\(my\\|local\\|our\\)"
5804 cperl-maybe-white-and-comment-rex
5805 "\\(("
5806 cperl-maybe-white-and-comment-rex
5807 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5808 (5 ,(if cperl-font-lock-multiline
5809 'font-lock-variable-name-face
5810 '(progn (setq cperl-font-lock-multiline-start
5811 (match-beginning 0))
5812 'font-lock-variable-name-face)))
5813 (,(concat "\\="
5814 cperl-maybe-white-and-comment-rex
5815 ","
5816 cperl-maybe-white-and-comment-rex
5817 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5818 ;; Bug in font-lock: limit is used not only to limit
5819 ;; searches, but to set the "extend window for
5820 ;; facification" property. Thus we need to minimize.
5821 ,(if cperl-font-lock-multiline
5822 '(if (match-beginning 3)
5823 (save-excursion
5824 (goto-char (match-beginning 3))
5825 (condition-case nil
5826 (forward-sexp 1)
5827 (error
5828 (condition-case nil
5829 (forward-char 200)
5830 (error nil)))) ; typeahead
5831 (1- (point))) ; report limit
5832 (forward-char -2)) ; disable continued expr
5833 '(if (match-beginning 3)
5834 (point-max) ; No limit for continuation
5835 (forward-char -2))) ; disable continued expr
5836 ,(if cperl-font-lock-multiline
5837 nil
5838 '(progn ; Do at end
5839 ;; "my" may be already fontified (POD),
5840 ;; so cperl-font-lock-multiline-start is nil
5841 (if (or (not cperl-font-lock-multiline-start)
5842 (> 2 (count-lines
5843 cperl-font-lock-multiline-start
5844 (point))))
5845 nil
5846 (put-text-property
5847 (1+ cperl-font-lock-multiline-start) (point)
5848 'syntax-type 'multiline))
5849 (setq cperl-font-lock-multiline-start nil)))
5850 (3 font-lock-variable-name-face))))
5851 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5852 3 font-lock-variable-name-face)))
5853 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5854 4 font-lock-variable-name-face)
5855 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5856 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5857 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5858 (setq
5859 t-font-lock-keywords-1
5860 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
5861 ;; not yet as of XEmacs 19.12, works with 21.1.11
5862 (or
5863 (not (featurep 'xemacs))
5864 (string< "21.1.9" emacs-version)
5865 (and (string< "21.1.10" emacs-version)
5866 (string< emacs-version "21.1.2")))
5867 '(
5868 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5869 (if (eq (char-after (match-beginning 2)) ?%)
5870 'cperl-hash-face
5871 'cperl-array-face)
5872 t) ; arrays and hashes
5873 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5874 1
5875 (if (= (- (match-end 2) (match-beginning 2)) 1)
5876 (if (eq (char-after (match-beginning 3)) ?{)
5877 'cperl-hash-face
5878 'cperl-array-face) ; arrays and hashes
5879 font-lock-variable-name-face) ; Just to put something
5880 t)
5881 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5882 (1 cperl-array-face)
5883 (2 font-lock-variable-name-face))
5884 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5885 (1 cperl-hash-face)
5886 (2 font-lock-variable-name-face))
5887 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5888 ;;; Too much noise from \s* @s[ and friends
5889 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
5890 ;;(3 font-lock-function-name-face t t)
5891 ;;(4
5892 ;; (if (cperl-slash-is-regexp)
5893 ;; font-lock-function-name-face 'default) nil t))
5894 )))
5895 (if cperl-highlight-variables-indiscriminately
5896 (setq t-font-lock-keywords-1
5897 (append t-font-lock-keywords-1
5898 (list '("\\([$*]{?\\sw+\\)" 1
5899 font-lock-variable-name-face)))))
5900 (setq cperl-font-lock-keywords-1
5901 (if cperl-syntaxify-by-font-lock
5902 (cons 'cperl-fontify-update
5903 t-font-lock-keywords)
5904 t-font-lock-keywords)
5905 cperl-font-lock-keywords cperl-font-lock-keywords-1
5906 cperl-font-lock-keywords-2 (append
5907 cperl-font-lock-keywords-1
5908 t-font-lock-keywords-1)))
5909 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5910 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
5911 (eval ; Avoid a warning
5912 '(font-lock-require-faces
5913 (list
5914 ;; Color-light Color-dark Gray-light Gray-dark Mono
5915 (list 'font-lock-comment-face
5916 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5917 nil
5918 [nil nil t t t]
5919 [nil nil t t t]
5920 nil)
5921 (list 'font-lock-string-face
5922 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5923 nil
5924 nil
5925 [nil nil t t t]
5926 nil)
5927 (list 'font-lock-function-name-face
5928 (vector
5929 "Blue" "LightSkyBlue" "Gray50" "LightGray"
5930 (cdr (assq 'background-color ; if mono
5931 (frame-parameters))))
5932 (vector
5933 nil nil nil nil
5934 (cdr (assq 'foreground-color ; if mono
5935 (frame-parameters))))
5936 [nil nil t t t]
5937 nil
5938 nil)
5939 (list 'font-lock-variable-name-face
5940 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
5941 nil
5942 [nil nil t t t]
5943 [nil nil t t t]
5944 nil)
5945 (list 'font-lock-type-face
5946 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
5947 nil
5948 [nil nil t t t]
5949 nil
5950 [nil nil t t t])
5951 (list 'font-lock-warning-face
5952 ["Pink" "Red" "Gray50" "LightGray"]
5953 ["gray20" "gray90"
5954 "gray80" "gray20"]
5955 [nil nil t t t]
5956 nil
5957 [nil nil t t t]
5958 )
5959 (list 'font-lock-constant-face
5960 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
5961 nil
5962 [nil nil t t t]
5963 nil
5964 [nil nil t t t])
5965 (list 'cperl-nonoverridable-face
5966 ["chartreuse3" ("orchid1" "orange")
5967 nil "Gray80"]
5968 [nil nil "gray90"]
5969 [nil nil nil t t]
5970 [nil nil t t]
5971 [nil nil t t t])
5972 (list 'cperl-array-face
5973 ["blue" "yellow" nil "Gray80"]
5974 ["lightyellow2" ("navy" "os2blue" "darkgreen")
5975 "gray90"]
5976 t
5977 nil
5978 nil)
5979 (list 'cperl-hash-face
5980 ["red" "red" nil "Gray80"]
5981 ["lightyellow2" ("navy" "os2blue" "darkgreen")
5982 "gray90"]
5983 t
5984 t
5985 nil))))
5986 ;; Do it the dull way, without choose-color
5987 (defvar cperl-guessed-background nil
5988 "Display characteristics as guessed by cperl.")
5989 ;; (or (fboundp 'x-color-defined-p)
5990 ;; (defalias 'x-color-defined-p
5991 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
5992 ;; ;; XEmacs >= 19.12
5993 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
5994 ;; ;; XEmacs 19.11
5995 ;; (t 'x-valid-color-name-p))))
5996 (cperl-force-face font-lock-constant-face
5997 "Face for constant and label names")
5998 (cperl-force-face font-lock-variable-name-face
5999 "Face for variable names")
6000 (cperl-force-face font-lock-type-face
6001 "Face for data types")
6002 (cperl-force-face cperl-nonoverridable-face
6003 "Face for data types from another group")
6004 (cperl-force-face font-lock-warning-face
6005 "Face for things which should stand out")
6006 (cperl-force-face font-lock-comment-face
6007 "Face for comments")
6008 (cperl-force-face font-lock-function-name-face
6009 "Face for function names")
6010 (cperl-force-face cperl-hash-face
6011 "Face for hashes")
6012 (cperl-force-face cperl-array-face
6013 "Face for arrays")
6014 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6015 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6016 ;;(or (boundp 'font-lock-type-face)
6017 ;; (defconst font-lock-type-face
6018 ;; 'font-lock-type-face
6019 ;; "Face to use for data types."))
6020 ;;(or (boundp 'cperl-nonoverridable-face)
6021 ;; (defconst cperl-nonoverridable-face
6022 ;; 'cperl-nonoverridable-face
6023 ;; "Face to use for data types from another group."))
6024 ;;(if (not (featurep 'xemacs)) nil
6025 ;; (or (boundp 'font-lock-comment-face)
6026 ;; (defconst font-lock-comment-face
6027 ;; 'font-lock-comment-face
6028 ;; "Face to use for comments."))
6029 ;; (or (boundp 'font-lock-keyword-face)
6030 ;; (defconst font-lock-keyword-face
6031 ;; 'font-lock-keyword-face
6032 ;; "Face to use for keywords."))
6033 ;; (or (boundp 'font-lock-function-name-face)
6034 ;; (defconst font-lock-function-name-face
6035 ;; 'font-lock-function-name-face
6036 ;; "Face to use for function names.")))
6037 (if (and
6038 (not (cperl-is-face 'cperl-array-face))
6039 (cperl-is-face 'font-lock-emphasized-face))
6040 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6041 (if (and
6042 (not (cperl-is-face 'cperl-hash-face))
6043 (cperl-is-face 'font-lock-other-emphasized-face))
6044 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6045 (if (and
6046 (not (cperl-is-face 'cperl-nonoverridable-face))
6047 (cperl-is-face 'font-lock-other-type-face))
6048 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6049 ;;(or (boundp 'cperl-hash-face)
6050 ;; (defconst cperl-hash-face
6051 ;; 'cperl-hash-face
6052 ;; "Face to use for hashes."))
6053 ;;(or (boundp 'cperl-array-face)
6054 ;; (defconst cperl-array-face
6055 ;; 'cperl-array-face
6056 ;; "Face to use for arrays."))
6057 ;; Here we try to guess background
6058 (let ((background
6059 (if (boundp 'font-lock-background-mode)
6060 font-lock-background-mode
6061 'light))
6062 (face-list (and (fboundp 'face-list) (face-list))))
6063 ;;;; (fset 'cperl-is-face
6064 ;;;; (cond ((fboundp 'find-face)
6065 ;;;; (symbol-function 'find-face))
6066 ;;;; (face-list
6067 ;;;; (function (lambda (face) (member face face-list))))
6068 ;;;; (t
6069 ;;;; (function (lambda (face) (boundp face))))))
6070 (defvar cperl-guessed-background
6071 (if (and (boundp 'font-lock-display-type)
6072 (eq font-lock-display-type 'grayscale))
6073 'gray
6074 background)
6075 "Background as guessed by CPerl mode")
6076 (and (not (cperl-is-face 'font-lock-constant-face))
6077 (cperl-is-face 'font-lock-reference-face)
6078 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6079 (if (cperl-is-face 'font-lock-type-face) nil
6080 (copy-face 'default 'font-lock-type-face)
6081 (cond
6082 ((eq background 'light)
6083 (set-face-foreground 'font-lock-type-face
6084 (if (x-color-defined-p "seagreen")
6085 "seagreen"
6086 "sea green")))
6087 ((eq background 'dark)
6088 (set-face-foreground 'font-lock-type-face
6089 (if (x-color-defined-p "os2pink")
6090 "os2pink"
6091 "pink")))
6092 (t
6093 (set-face-background 'font-lock-type-face "gray90"))))
6094 (if (cperl-is-face 'cperl-nonoverridable-face)
6095 nil
6096 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6097 (cond
6098 ((eq background 'light)
6099 (set-face-foreground 'cperl-nonoverridable-face
6100 (if (x-color-defined-p "chartreuse3")
6101 "chartreuse3"
6102 "chartreuse")))
6103 ((eq background 'dark)
6104 (set-face-foreground 'cperl-nonoverridable-face
6105 (if (x-color-defined-p "orchid1")
6106 "orchid1"
6107 "orange")))))
6108 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6109 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6110 ;;; (cond
6111 ;;; ((eq background 'light)
6112 ;;; (set-face-background 'font-lock-other-emphasized-face
6113 ;;; (if (x-color-defined-p "lightyellow2")
6114 ;;; "lightyellow2"
6115 ;;; (if (x-color-defined-p "lightyellow")
6116 ;;; "lightyellow"
6117 ;;; "light yellow"))))
6118 ;;; ((eq background 'dark)
6119 ;;; (set-face-background 'font-lock-other-emphasized-face
6120 ;;; (if (x-color-defined-p "navy")
6121 ;;; "navy"
6122 ;;; (if (x-color-defined-p "darkgreen")
6123 ;;; "darkgreen"
6124 ;;; "dark green"))))
6125 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6126 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6127 ;;; (copy-face 'bold 'font-lock-emphasized-face)
6128 ;;; (cond
6129 ;;; ((eq background 'light)
6130 ;;; (set-face-background 'font-lock-emphasized-face
6131 ;;; (if (x-color-defined-p "lightyellow2")
6132 ;;; "lightyellow2"
6133 ;;; "lightyellow")))
6134 ;;; ((eq background 'dark)
6135 ;;; (set-face-background 'font-lock-emphasized-face
6136 ;;; (if (x-color-defined-p "navy")
6137 ;;; "navy"
6138 ;;; (if (x-color-defined-p "darkgreen")
6139 ;;; "darkgreen"
6140 ;;; "dark green"))))
6141 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6142 (if (cperl-is-face 'font-lock-variable-name-face) nil
6143 (copy-face 'italic 'font-lock-variable-name-face))
6144 (if (cperl-is-face 'font-lock-constant-face) nil
6145 (copy-face 'italic 'font-lock-constant-face))))
6146 (setq cperl-faces-init t))
6147 (error (message "cperl-init-faces (ignored): %s" errs))))
6148
6149
6150 (defun cperl-ps-print-init ()
6151 "Initialization of `ps-print' components for faces used in CPerl."
6152 (eval-after-load "ps-print"
6153 '(setq ps-bold-faces
6154 ;; font-lock-variable-name-face
6155 ;; font-lock-constant-face
6156 (append '(cperl-array-face cperl-hash-face)
6157 ps-bold-faces)
6158 ps-italic-faces
6159 ;; font-lock-constant-face
6160 (append '(cperl-nonoverridable-face cperl-hash-face)
6161 ps-italic-faces)
6162 ps-underlined-faces
6163 ;; font-lock-type-face
6164 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6165 ps-underlined-faces))))
6166
6167 (defvar ps-print-face-extension-alist)
6168
6169 (defun cperl-ps-print (&optional file)
6170 "Pretty-print in CPerl style.
6171 If optional argument FILE is an empty string, prints to printer, otherwise
6172 to the file FILE. If FILE is nil, prompts for a file name.
6173
6174 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6175 (interactive)
6176 (or file
6177 (setq file (read-from-minibuffer
6178 "Print to file (if empty - to printer): "
6179 (concat (buffer-file-name) ".ps")
6180 nil nil 'file-name-history)))
6181 (or (> (length file) 0)
6182 (setq file nil))
6183 (require 'ps-print) ; To get ps-print-face-extension-alist
6184 (let ((ps-print-color-p t)
6185 (ps-print-face-extension-alist ps-print-face-extension-alist))
6186 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6187 (ps-print-buffer-with-faces file)))
6188
6189 ;;; (defun cperl-ps-print-init ()
6190 ;;; "Initialization of `ps-print' components for faces used in CPerl."
6191 ;;; ;; Guard against old versions
6192 ;;; (defvar ps-underlined-faces nil)
6193 ;;; (defvar ps-bold-faces nil)
6194 ;;; (defvar ps-italic-faces nil)
6195 ;;; (setq ps-bold-faces
6196 ;;; (append '(font-lock-emphasized-face
6197 ;;; cperl-array-face
6198 ;;; font-lock-keyword-face
6199 ;;; font-lock-variable-name-face
6200 ;;; font-lock-constant-face
6201 ;;; font-lock-reference-face
6202 ;;; font-lock-other-emphasized-face
6203 ;;; cperl-hash-face)
6204 ;;; ps-bold-faces))
6205 ;;; (setq ps-italic-faces
6206 ;;; (append '(cperl-nonoverridable-face
6207 ;;; font-lock-constant-face
6208 ;;; font-lock-reference-face
6209 ;;; font-lock-other-emphasized-face
6210 ;;; cperl-hash-face)
6211 ;;; ps-italic-faces))
6212 ;;; (setq ps-underlined-faces
6213 ;;; (append '(font-lock-emphasized-face
6214 ;;; cperl-array-face
6215 ;;; font-lock-other-emphasized-face
6216 ;;; cperl-hash-face
6217 ;;; cperl-nonoverridable-face font-lock-type-face)
6218 ;;; ps-underlined-faces))
6219 ;;; (cons 'font-lock-type-face ps-underlined-faces))
6220
6221
6222 (if (cperl-enable-font-lock) (cperl-windowed-init))
6223
6224 (defconst cperl-styles-entries
6225 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6226 cperl-label-offset cperl-extra-newline-before-brace
6227 cperl-extra-newline-before-brace-multiline
6228 cperl-merge-trailing-else
6229 cperl-continued-statement-offset))
6230
6231 (defconst cperl-style-examples
6232 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6233 ##### cperl-continued-brace-offset cperl-label-offset
6234 ##### cperl-continued-statement-offset
6235 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6236
6237 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6238
6239 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6240 if (foo) {
6241 bar
6242 baz;
6243 label:
6244 {
6245 boon;
6246 }
6247 } else {
6248 stop;
6249 }
6250
6251 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6252 if (foo) {
6253 bar
6254 baz;
6255 label:
6256 {
6257 boon;
6258 }
6259 } else {
6260 stop;
6261 }
6262
6263 ### GNU 2/0/0/-2/2/nil/t
6264 if (foo)
6265 {
6266 bar
6267 baz;
6268 label:
6269 {
6270 boon;
6271 }
6272 }
6273 else
6274 {
6275 stop;
6276 }
6277
6278 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6279 if (foo)
6280 {
6281 bar
6282 baz;
6283 label:
6284 {
6285 boon;
6286 }
6287 }
6288 else
6289 {
6290 stop;
6291 }
6292
6293 ### BSD (=C++, but will not change preexisting merge-trailing-else
6294 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6295 if (foo)
6296 {
6297 bar
6298 baz;
6299 label:
6300 {
6301 boon;
6302 }
6303 }
6304 else
6305 {
6306 stop;
6307 }
6308
6309 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6310 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6311 if (foo)
6312 {
6313 bar
6314 baz;
6315 label:
6316 {
6317 boon;
6318 }
6319 }
6320 else
6321 {
6322 stop;
6323 }
6324
6325 ### Whitesmith (=PerlStyle, but will not change preexisting
6326 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6327 if (foo)
6328 {
6329 bar
6330 baz;
6331 label:
6332 {
6333 boon;
6334 }
6335 }
6336 else
6337 {
6338 stop;
6339 }
6340 "
6341 "Examples of if/else with different indent styles (with v4.23).")
6342
6343 (defconst cperl-style-alist
6344 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6345 (cperl-indent-level . 2)
6346 (cperl-brace-offset . 0)
6347 (cperl-continued-brace-offset . 0)
6348 (cperl-label-offset . -2)
6349 (cperl-continued-statement-offset . 2)
6350 (cperl-extra-newline-before-brace . nil)
6351 (cperl-extra-newline-before-brace-multiline . nil)
6352 (cperl-merge-trailing-else . t))
6353
6354 ("PerlStyle" ; CPerl with 4 as indent
6355 (cperl-indent-level . 4)
6356 (cperl-brace-offset . 0)
6357 (cperl-continued-brace-offset . 0)
6358 (cperl-label-offset . -4)
6359 (cperl-continued-statement-offset . 4)
6360 (cperl-extra-newline-before-brace . nil)
6361 (cperl-extra-newline-before-brace-multiline . nil)
6362 (cperl-merge-trailing-else . t))
6363
6364 ("GNU"
6365 (cperl-indent-level . 2)
6366 (cperl-brace-offset . 0)
6367 (cperl-continued-brace-offset . 0)
6368 (cperl-label-offset . -2)
6369 (cperl-continued-statement-offset . 2)
6370 (cperl-extra-newline-before-brace . t)
6371 (cperl-extra-newline-before-brace-multiline . t)
6372 (cperl-merge-trailing-else . nil))
6373
6374 ("K&R"
6375 (cperl-indent-level . 5)
6376 (cperl-brace-offset . 0)
6377 (cperl-continued-brace-offset . -5)
6378 (cperl-label-offset . -5)
6379 (cperl-continued-statement-offset . 5)
6380 ;;(cperl-extra-newline-before-brace . nil) ; ???
6381 ;;(cperl-extra-newline-before-brace-multiline . nil)
6382 (cperl-merge-trailing-else . nil))
6383
6384 ("BSD"
6385 (cperl-indent-level . 4)
6386 (cperl-brace-offset . 0)
6387 (cperl-continued-brace-offset . -4)
6388 (cperl-label-offset . -4)
6389 (cperl-continued-statement-offset . 4)
6390 ;;(cperl-extra-newline-before-brace . nil) ; ???
6391 ;;(cperl-extra-newline-before-brace-multiline . nil)
6392 ;;(cperl-merge-trailing-else . nil) ; ???
6393 )
6394
6395 ("C++"
6396 (cperl-indent-level . 4)
6397 (cperl-brace-offset . 0)
6398 (cperl-continued-brace-offset . -4)
6399 (cperl-label-offset . -4)
6400 (cperl-continued-statement-offset . 4)
6401 (cperl-extra-newline-before-brace . t)
6402 (cperl-extra-newline-before-brace-multiline . t)
6403 (cperl-merge-trailing-else . nil))
6404
6405 ("Whitesmith"
6406 (cperl-indent-level . 4)
6407 (cperl-brace-offset . 0)
6408 (cperl-continued-brace-offset . 0)
6409 (cperl-label-offset . -4)
6410 (cperl-continued-statement-offset . 4)
6411 ;;(cperl-extra-newline-before-brace . nil) ; ???
6412 ;;(cperl-extra-newline-before-brace-multiline . nil)
6413 ;;(cperl-merge-trailing-else . nil) ; ???
6414 )
6415 ("Current"))
6416 "List of variables to set to get a particular indentation style.
6417 Should be used via `cperl-set-style' or via Perl menu.
6418
6419 See examples in `cperl-style-examples'.")
6420
6421 (defun cperl-set-style (style)
6422 "Set CPerl mode variables to use one of several different indentation styles.
6423 The arguments are a string representing the desired style.
6424 The list of styles is in `cperl-style-alist', available styles
6425 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6426
6427 The current value of style is memorized (unless there is a memorized
6428 data already), may be restored by `cperl-set-style-back'.
6429
6430 Chosing \"Current\" style will not change style, so this may be used for
6431 side-effect of memorizing only. Examples in `cperl-style-examples'."
6432 (interactive
6433 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6434 cperl-style-alist)))
6435 (list (completing-read "Enter style: " list nil 'insist))))
6436 (or cperl-old-style
6437 (setq cperl-old-style
6438 (mapcar (function
6439 (lambda (name)
6440 (cons name (eval name))))
6441 cperl-styles-entries)))
6442 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6443 (while style
6444 (setq setting (car style) style (cdr style))
6445 (set (car setting) (cdr setting)))))
6446
6447 (defun cperl-set-style-back ()
6448 "Restore a style memorized by `cperl-set-style'."
6449 (interactive)
6450 (or cperl-old-style (error "The style was not changed"))
6451 (let (setting)
6452 (while cperl-old-style
6453 (setq setting (car cperl-old-style)
6454 cperl-old-style (cdr cperl-old-style))
6455 (set (car setting) (cdr setting)))))
6456
6457 (defun cperl-check-syntax ()
6458 (interactive)
6459 (require 'mode-compile)
6460 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6461 (eval '(mode-compile)))) ; Avoid a warning
6462
6463 (defun cperl-info-buffer (type)
6464 ;; Returns buffer with documentation. Creates if missing.
6465 ;; If TYPE, this vars buffer.
6466 ;; Special care is taken to not stomp over an existing info buffer
6467 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6468 (info (get-buffer bname))
6469 (oldbuf (get-buffer "*info*")))
6470 (if info info
6471 (save-window-excursion
6472 ;; Get Info running
6473 (require 'info)
6474 (cond (oldbuf
6475 (set-buffer oldbuf)
6476 (rename-buffer "*info-perl-tmp*")))
6477 (save-window-excursion
6478 (info))
6479 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6480 (set-buffer "*info*")
6481 (rename-buffer bname)
6482 (cond (oldbuf
6483 (set-buffer "*info-perl-tmp*")
6484 (rename-buffer "*info*")
6485 (set-buffer bname)))
6486 (make-local-variable 'window-min-height)
6487 (setq window-min-height 2)
6488 (current-buffer)))))
6489
6490 (defun cperl-word-at-point (&optional p)
6491 "Return the word at point or at P."
6492 (save-excursion
6493 (if p (goto-char p))
6494 (or (cperl-word-at-point-hard)
6495 (progn
6496 (require 'etags)
6497 (funcall (or (and (boundp 'find-tag-default-function)
6498 find-tag-default-function)
6499 (get major-mode 'find-tag-default-function)
6500 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6501 ;; automatically used within `find-tag-default':
6502 'find-tag-default))))))
6503
6504 (defun cperl-info-on-command (command)
6505 "Show documentation for Perl command COMMAND in other window.
6506 If perl-info buffer is shown in some frame, uses this frame.
6507 Customized by setting variables `cperl-shrink-wrap-info-frame',
6508 `cperl-max-help-size'."
6509 (interactive
6510 (let* ((default (cperl-word-at-point))
6511 (read (read-string
6512 (format "Find doc for Perl function (default %s): "
6513 default))))
6514 (list (if (equal read "")
6515 default
6516 read))))
6517
6518 (let ((buffer (current-buffer))
6519 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6520 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6521 max-height char-height buf-list)
6522 (if (string-match "^-[a-zA-Z]$" command)
6523 (setq cmd-desc "^-X[ \t\n]"))
6524 (setq isvar (string-match "^[$@%]" command)
6525 buf (cperl-info-buffer isvar)
6526 iniwin (selected-window)
6527 fr1 (window-frame iniwin))
6528 (set-buffer buf)
6529 (goto-char (point-min))
6530 (or isvar
6531 (progn (re-search-forward "^-X[ \t\n]")
6532 (forward-line -1)))
6533 (if (re-search-forward cmd-desc nil t)
6534 (progn
6535 ;; Go back to beginning of the group (ex, for qq)
6536 (if (re-search-backward "^[ \t\n\f]")
6537 (forward-line 1))
6538 (beginning-of-line)
6539 ;; Get some of
6540 (setq pos (point)
6541 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6542 (while (and (not win) buf-list)
6543 (setq win (get-buffer-window (car buf-list) t))
6544 (setq buf-list (cdr buf-list)))
6545 (or (not win)
6546 (eq (window-buffer win) buf)
6547 (set-window-buffer win buf))
6548 (and win (setq fr2 (window-frame win)))
6549 (if (or (not fr2) (eq fr1 fr2))
6550 (pop-to-buffer buf)
6551 (special-display-popup-frame buf) ; Make it visible
6552 (select-window win))
6553 (goto-char pos) ; Needed (?!).
6554 ;; Resize
6555 (setq iniheight (window-height)
6556 frheight (frame-height)
6557 not-loner (< iniheight (1- frheight))) ; Are not alone
6558 (cond ((if not-loner cperl-max-help-size
6559 cperl-shrink-wrap-info-frame)
6560 (setq height
6561 (+ 2
6562 (count-lines
6563 pos
6564 (save-excursion
6565 (if (re-search-forward
6566 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6567 (match-beginning 0) (point-max)))))
6568 max-height
6569 (if not-loner
6570 (/ (* (- frheight 3) cperl-max-help-size) 100)
6571 (setq char-height (frame-char-height))
6572 ;; Non-functioning under OS/2:
6573 (if (eq char-height 1) (setq char-height 18))
6574 ;; Title, menubar, + 2 for slack
6575 (- (/ (display-pixel-height) char-height) 4)))
6576 (if (> height max-height) (setq height max-height))
6577 ;;(message "was %s doing %s" iniheight height)
6578 (if not-loner
6579 (enlarge-window (- height iniheight))
6580 (set-frame-height (window-frame win) (1+ height)))))
6581 (set-window-start (selected-window) pos))
6582 (message "No entry for %s found." command))
6583 ;;(pop-to-buffer buffer)
6584 (select-window iniwin)))
6585
6586 (defun cperl-info-on-current-command ()
6587 "Show documentation for Perl command at point in other window."
6588 (interactive)
6589 (cperl-info-on-command (cperl-word-at-point)))
6590
6591 (defun cperl-imenu-info-imenu-search ()
6592 (if (looking-at "^-X[ \t\n]") nil
6593 (re-search-backward
6594 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6595 (forward-line 1)))
6596
6597 (defun cperl-imenu-info-imenu-name ()
6598 (buffer-substring
6599 (match-beginning 1) (match-end 1)))
6600
6601 (defun cperl-imenu-on-info ()
6602 "Shows imenu for Perl Info Buffer.
6603 Opens Perl Info buffer if needed."
6604 (interactive)
6605 (let* ((buffer (current-buffer))
6606 imenu-create-index-function
6607 imenu-prev-index-position-function
6608 imenu-extract-index-name-function
6609 (index-item (save-restriction
6610 (save-window-excursion
6611 (set-buffer (cperl-info-buffer nil))
6612 (setq imenu-create-index-function
6613 'imenu-default-create-index-function
6614 imenu-prev-index-position-function
6615 'cperl-imenu-info-imenu-search
6616 imenu-extract-index-name-function
6617 'cperl-imenu-info-imenu-name)
6618 (imenu-choose-buffer-index)))))
6619 (and index-item
6620 (progn
6621 (push-mark)
6622 (pop-to-buffer "*info-perl*")
6623 (cond
6624 ((markerp (cdr index-item))
6625 (goto-char (marker-position (cdr index-item))))
6626 (t
6627 (goto-char (cdr index-item))))
6628 (set-window-start (selected-window) (point))
6629 (pop-to-buffer buffer)))))
6630
6631 (defun cperl-lineup (beg end &optional step minshift)
6632 "Lineup construction in a region.
6633 Beginning of region should be at the start of a construction.
6634 All first occurrences of this construction in the lines that are
6635 partially contained in the region are lined up at the same column.
6636
6637 MINSHIFT is the minimal amount of space to insert before the construction.
6638 STEP is the tabwidth to position constructions.
6639 If STEP is nil, `cperl-lineup-step' will be used
6640 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6641 Will not move the position at the start to the left."
6642 (interactive "r")
6643 (let (search col tcol seen b)
6644 (save-excursion
6645 (goto-char end)
6646 (end-of-line)
6647 (setq end (point-marker))
6648 (goto-char beg)
6649 (skip-chars-forward " \t\f")
6650 (setq beg (point-marker))
6651 (indent-region beg end nil)
6652 (goto-char beg)
6653 (setq col (current-column))
6654 (if (looking-at "[a-zA-Z0-9_]")
6655 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6656 (setq search
6657 (concat "\\<"
6658 (regexp-quote
6659 (buffer-substring (match-beginning 0)
6660 (match-end 0))) "\\>"))
6661 (error "Cannot line up in a middle of the word"))
6662 (if (looking-at "$")
6663 (error "Cannot line up end of line"))
6664 (setq search (regexp-quote (char-to-string (following-char)))))
6665 (setq step (or step cperl-lineup-step cperl-indent-level))
6666 (or minshift (setq minshift 1))
6667 (while (progn
6668 (beginning-of-line 2)
6669 (and (< (point) end)
6670 (re-search-forward search end t)
6671 (goto-char (match-beginning 0))))
6672 (setq tcol (current-column) seen t)
6673 (if (> tcol col) (setq col tcol)))
6674 (or seen
6675 (error "The construction to line up occurred only once"))
6676 (goto-char beg)
6677 (setq col (+ col minshift))
6678 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6679 (while
6680 (progn
6681 (cperl-make-indent col)
6682 (beginning-of-line 2)
6683 (and (< (point) end)
6684 (re-search-forward search end t)
6685 (goto-char (match-beginning 0)))))))) ; No body
6686
6687 (defun cperl-etags (&optional add all files) ;; NOT USED???
6688 "Run etags with appropriate options for Perl files.
6689 If optional argument ALL is `recursive', will process Perl files
6690 in subdirectories too."
6691 (interactive)
6692 (let ((cmd "etags")
6693 (args '("-l" "none" "-r"
6694 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6695 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6696 "-r"
6697 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6698 "-r"
6699 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6700 res)
6701 (if add (setq args (cons "-a" args)))
6702 (or files (setq files (list buffer-file-name)))
6703 (cond
6704 ((eq all 'recursive)
6705 ;;(error "Not implemented: recursive")
6706 (setq args (append (list "-e"
6707 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6708 use File::Find;
6709 find(\\&wanted, '.');
6710 exec @ARGV;"
6711 cmd) args)
6712 cmd "perl"))
6713 (all
6714 ;;(error "Not implemented: all")
6715 (setq args (append (list "-e"
6716 "push @ARGV, <*.PL *.pl *.pm>;
6717 exec @ARGV;"
6718 cmd) args)
6719 cmd "perl"))
6720 (t
6721 (setq args (append args files))))
6722 (setq res (apply 'call-process cmd nil nil nil args))
6723 (or (eq res 0)
6724 (message "etags returned \"%s\"" res))))
6725
6726 (defun cperl-toggle-auto-newline ()
6727 "Toggle the state of `cperl-auto-newline'."
6728 (interactive)
6729 (setq cperl-auto-newline (not cperl-auto-newline))
6730 (message "Newlines will %sbe auto-inserted now."
6731 (if cperl-auto-newline "" "not ")))
6732
6733 (defun cperl-toggle-abbrev ()
6734 "Toggle the state of automatic keyword expansion in CPerl mode."
6735 (interactive)
6736 (abbrev-mode (if abbrev-mode 0 1))
6737 (message "Perl control structure will %sbe auto-inserted now."
6738 (if abbrev-mode "" "not ")))
6739
6740
6741 (defun cperl-toggle-electric ()
6742 "Toggle the state of parentheses doubling in CPerl mode."
6743 (interactive)
6744 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6745 (message "Parentheses will %sbe auto-doubled now."
6746 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6747
6748 (defun cperl-toggle-autohelp ()
6749 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6750 Delay of auto-help controlled by `cperl-lazy-help-time'."
6751 (interactive)
6752 (if (fboundp 'run-with-idle-timer)
6753 (progn
6754 (if cperl-lazy-installed
6755 (cperl-lazy-unstall)
6756 (cperl-lazy-install))
6757 (message "Perl help messages will %sbe automatically shown now."
6758 (if cperl-lazy-installed "" "not ")))
6759 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6760
6761 (defun cperl-toggle-construct-fix ()
6762 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6763 (interactive)
6764 (setq cperl-indent-region-fix-constructs
6765 (if cperl-indent-region-fix-constructs
6766 nil
6767 1))
6768 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6769 (if cperl-indent-region-fix-constructs "" "not ")))
6770
6771 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6772 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6773 Nonpositive numeric argument disables debugging messages. The message
6774 summarizes which regions it was decided to rescan for syntactic constructs.
6775
6776 The message looks like this:
6777
6778 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6779
6780 Numbers are character positions in the buffer. REQ provides the range to
6781 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6782 for correct operation it should start and end outside any special syntactic
6783 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6784 by CPerl."
6785 (interactive "P")
6786 (or arg
6787 (setq arg (if (eq cperl-syntaxify-by-font-lock
6788 (if backtrace 'backtrace 'message)) 0 1)))
6789 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6790 (setq cperl-syntaxify-by-font-lock arg)
6791 (message "Debugging messages of syntax unwind %sabled."
6792 (if (eq arg t) "dis" "en")))
6793
6794 ;;;; Tags file creation.
6795
6796 (defvar cperl-tmp-buffer " *cperl-tmp*")
6797
6798 (defun cperl-setup-tmp-buf ()
6799 (set-buffer (get-buffer-create cperl-tmp-buffer))
6800 (set-syntax-table cperl-mode-syntax-table)
6801 (buffer-disable-undo)
6802 (auto-fill-mode 0)
6803 (if cperl-use-syntax-table-text-property-for-tags
6804 (progn
6805 (make-local-variable 'parse-sexp-lookup-properties)
6806 ;; Do not introduce variable if not needed, we check it!
6807 (set 'parse-sexp-lookup-properties t))))
6808
6809 (defun cperl-xsub-scan ()
6810 (require 'imenu)
6811 (let ((index-alist '())
6812 (prev-pos 0) index index1 name package prefix)
6813 (goto-char (point-min))
6814 ;; Search for the function
6815 (progn ;;save-match-data
6816 (while (re-search-forward
6817 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6818 nil t)
6819 (cond
6820 ((match-beginning 2) ; SECTION
6821 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6822 (goto-char (match-beginning 0))
6823 (skip-chars-forward " \t")
6824 (forward-char 1)
6825 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6826 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6827 (setq prefix nil)))
6828 ((not package) nil) ; C language section
6829 ((match-beginning 3) ; XSUB
6830 (goto-char (1+ (match-beginning 3)))
6831 (setq index (imenu-example--name-and-position))
6832 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6833 (if (and prefix (string-match (concat "^" prefix) name))
6834 (setq name (substring name (length prefix))))
6835 (cond ((string-match "::" name) nil)
6836 (t
6837 (setq index1 (cons (concat package "::" name) (cdr index)))
6838 (push index1 index-alist)))
6839 (setcar index name)
6840 (push index index-alist))
6841 (t ; BOOT: section
6842 ;; (beginning-of-line)
6843 (setq index (imenu-example--name-and-position))
6844 (setcar index (concat package "::BOOT:"))
6845 (push index index-alist)))))
6846 index-alist))
6847
6848 (defvar cperl-unreadable-ok nil)
6849
6850 (defun cperl-find-tags (ifile xs topdir)
6851 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6852 (cperl-pod-here-fontify nil) f file)
6853 (save-excursion
6854 (if b (set-buffer b)
6855 (cperl-setup-tmp-buf))
6856 (erase-buffer)
6857 (condition-case err
6858 (setq file (car (insert-file-contents ifile)))
6859 (error (if cperl-unreadable-ok nil
6860 (if (y-or-n-p
6861 (format "File %s unreadable. Continue? " ifile))
6862 (setq cperl-unreadable-ok t)
6863 (error "Aborting: unreadable file %s" ifile)))))
6864 (if (not file)
6865 (message "Unreadable file %s" ifile)
6866 (message "Scanning file %s ..." file)
6867 (if (and cperl-use-syntax-table-text-property-for-tags
6868 (not xs))
6869 (condition-case err ; after __END__ may have garbage
6870 (cperl-find-pods-heres nil nil noninteractive)
6871 (error (message "While scanning for syntax: %s" err))))
6872 (if xs
6873 (setq lst (cperl-xsub-scan))
6874 (setq ind (cperl-imenu--create-perl-index))
6875 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6876 (setq lst
6877 (mapcar
6878 (function
6879 (lambda (elt)
6880 (cond ((string-match "^[_a-zA-Z]" (car elt))
6881 (goto-char (cdr elt))
6882 (beginning-of-line) ; pos should be of the start of the line
6883 (list (car elt)
6884 (point)
6885 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6886 (buffer-substring (progn
6887 (goto-char (cdr elt))
6888 ;; After name now...
6889 (or (eolp) (forward-char 1))
6890 (point))
6891 (progn
6892 (beginning-of-line)
6893 (point))))))))
6894 lst))
6895 (erase-buffer)
6896 (while lst
6897 (setq elt (car lst) lst (cdr lst))
6898 (if elt
6899 (progn
6900 (insert (elt elt 3)
6901 127
6902 (if (string-match "^package " (car elt))
6903 (substring (car elt) 8)
6904 (car elt) )
6905 1
6906 (number-to-string (elt elt 2)) ; Line
6907 ","
6908 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
6909 "\n")
6910 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
6911 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
6912 (elt elt 3)))
6913 ;; Need to insert the name without package as well
6914 (setq lst (cons (cons (substring (elt elt 3)
6915 (match-beginning 1)
6916 (match-end 1))
6917 (cdr elt))
6918 lst))))))
6919 (setq pos (point))
6920 (goto-char 1)
6921 (setq rel file)
6922 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
6923 (set-text-properties 0 (length rel) nil rel)
6924 (and (equal topdir (substring rel 0 (length topdir)))
6925 (setq rel (substring file (length topdir))))
6926 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
6927 (setq ret (buffer-substring 1 (point-max)))
6928 (erase-buffer)
6929 (or noninteractive
6930 (message "Scanning file %s finished" file))
6931 ret))))
6932
6933 (defun cperl-add-tags-recurse-noxs ()
6934 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
6935 Use as
6936 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6937 -f cperl-add-tags-recurse-noxs
6938 "
6939 (cperl-write-tags nil nil t t nil t))
6940
6941 (defun cperl-add-tags-recurse-noxs-fullpath ()
6942 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
6943 Writes down fullpath, so TAGS is relocatable (but if the build directory
6944 is relocated, the file TAGS inside it breaks). Use as
6945 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6946 -f cperl-add-tags-recurse-noxs-fullpath
6947 "
6948 (cperl-write-tags nil nil t t nil t ""))
6949
6950 (defun cperl-add-tags-recurse ()
6951 "Add to TAGS file data for Perl files in the current directory and kids.
6952 Use as
6953 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6954 -f cperl-add-tags-recurse
6955 "
6956 (cperl-write-tags nil nil t t))
6957
6958 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
6959 ;; If INBUFFER, do not select buffer, and do not save
6960 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
6961 (require 'etags)
6962 (if file nil
6963 (setq file (if dir default-directory (buffer-file-name)))
6964 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
6965 (or topdir
6966 (setq topdir default-directory))
6967 (let ((tags-file-name "TAGS")
6968 (case-fold-search (eq system-type 'emx))
6969 xs rel tm)
6970 (save-excursion
6971 (cond (inbuffer nil) ; Already there
6972 ((file-exists-p tags-file-name)
6973 (if (featurep 'xemacs)
6974 (visit-tags-table-buffer)
6975 (visit-tags-table-buffer tags-file-name)))
6976 (t (set-buffer (find-file-noselect tags-file-name))))
6977 (cond
6978 (dir
6979 (cond ((eq erase 'ignore))
6980 (erase
6981 (erase-buffer)
6982 (setq erase 'ignore)))
6983 (let ((files
6984 (condition-case err
6985 (directory-files file t
6986 (if recurse nil cperl-scan-files-regexp)
6987 t)
6988 (error
6989 (if cperl-unreadable-ok nil
6990 (if (y-or-n-p
6991 (format "Directory %s unreadable. Continue? " file))
6992 (setq cperl-unreadable-ok t
6993 tm nil) ; Return empty list
6994 (error "Aborting: unreadable directory %s" file)))))))
6995 (mapc (function
6996 (lambda (file)
6997 (cond
6998 ((string-match cperl-noscan-files-regexp file)
6999 nil)
7000 ((not (file-directory-p file))
7001 (if (string-match cperl-scan-files-regexp file)
7002 (cperl-write-tags file erase recurse nil t noxs topdir)))
7003 ((not recurse) nil)
7004 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7005 files)))
7006 (t
7007 (setq xs (string-match "\\.xs$" file))
7008 (if (not (and xs noxs))
7009 (progn
7010 (cond ((eq erase 'ignore) (goto-char (point-max)))
7011 (erase (erase-buffer))
7012 (t
7013 (goto-char 1)
7014 (setq rel file)
7015 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7016 (set-text-properties 0 (length rel) nil rel)
7017 (and (equal topdir (substring rel 0 (length topdir)))
7018 (setq rel (substring file (length topdir))))
7019 (if (search-forward (concat "\f\n" rel ",") nil t)
7020 (progn
7021 (search-backward "\f\n")
7022 (delete-region (point)
7023 (save-excursion
7024 (forward-char 1)
7025 (if (search-forward "\f\n"
7026 nil 'toend)
7027 (- (point) 2)
7028 (point-max)))))
7029 (goto-char (point-max)))))
7030 (insert (cperl-find-tags file xs topdir))))))
7031 (if inbuffer nil ; Delegate to the caller
7032 (save-buffer 0) ; No backup
7033 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7034 (initialize-new-tags-table))))))
7035
7036 (defvar cperl-tags-hier-regexp-list
7037 (concat
7038 "^\\("
7039 "\\(package\\)\\>"
7040 "\\|"
7041 "sub\\>[^\n]+::"
7042 "\\|"
7043 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7044 "\\|"
7045 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7046 "\\)"))
7047
7048 (defvar cperl-hierarchy '(() ())
7049 "Global hierarchy of classes.")
7050
7051 (defun cperl-tags-hier-fill ()
7052 ;; Suppose we are in a tag table cooked by cperl.
7053 (goto-char 1)
7054 (let (type pack name pos line chunk ord cons1 file str info fileind)
7055 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7056 (setq pos (match-beginning 0)
7057 pack (match-beginning 2))
7058 (beginning-of-line)
7059 (if (looking-at (concat
7060 "\\([^\n]+\\)"
7061 "\C-?"
7062 "\\([^\n]+\\)"
7063 "\C-a"
7064 "\\([0-9]+\\)"
7065 ","
7066 "\\([0-9]+\\)"))
7067 (progn
7068 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7069 name (buffer-substring (match-beginning 2) (match-end 2))
7070 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7071 line (buffer-substring (match-beginning 3) (match-end 3))
7072 ord (if pack 1 0)
7073 file (file-of-tag)
7074 fileind (format "%s:%s" file line)
7075 ;; Moves to beginning of the next line:
7076 info (cperl-etags-snarf-tag file line))
7077 ;; Move back
7078 (forward-char -1)
7079 ;; Make new member of hierarchy name ==> file ==> pos if needed
7080 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7081 ;; Name known
7082 (setcdr cons1 (cons (cons fileind (vector file info))
7083 (cdr cons1)))
7084 ;; First occurrence of the name, start alist
7085 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7086 (if pack
7087 (setcar (cdr cperl-hierarchy)
7088 (cons cons1 (nth 1 cperl-hierarchy)))
7089 (setcar cperl-hierarchy
7090 (cons cons1 (car cperl-hierarchy)))))))
7091 (end-of-line))))
7092
7093 (declare-function x-popup-menu "xmenu.c" (position menu))
7094
7095 (defun cperl-tags-hier-init (&optional update)
7096 "Show hierarchical menu of classes and methods.
7097 Finds info about classes by a scan of loaded TAGS files.
7098 Supposes that the TAGS files contain fully qualified function names.
7099 One may build such TAGS files from CPerl mode menu."
7100 (interactive)
7101 (require 'etags)
7102 (require 'imenu)
7103 (if (or update (null (nth 2 cperl-hierarchy)))
7104 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7105 (or (nthcdr 2 elt)
7106 ;; Only in one file
7107 (setcdr elt (cdr (nth 1 elt)))))))
7108 pack name cons1 to l1 l2 l3 l4 b)
7109 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7110 (setq cperl-hierarchy (list l1 l2 l3))
7111 (if (featurep 'xemacs) ; Not checked
7112 (progn
7113 (or tags-file-name
7114 ;; Does this work in XEmacs?
7115 (call-interactively 'visit-tags-table))
7116 (message "Updating list of classes...")
7117 (set-buffer (get-file-buffer tags-file-name))
7118 (cperl-tags-hier-fill))
7119 (or tags-table-list
7120 (call-interactively 'visit-tags-table))
7121 (mapc
7122 (function
7123 (lambda (tagsfile)
7124 (message "Updating list of classes... %s" tagsfile)
7125 (set-buffer (get-file-buffer tagsfile))
7126 (cperl-tags-hier-fill)))
7127 tags-table-list)
7128 (message "Updating list of classes... postprocessing..."))
7129 (mapc remover (car cperl-hierarchy))
7130 (mapc remover (nth 1 cperl-hierarchy))
7131 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7132 (cons "Methods: " (car cperl-hierarchy))))
7133 (cperl-tags-treeify to 1)
7134 (setcar (nthcdr 2 cperl-hierarchy)
7135 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7136 (message "Updating list of classes: done, requesting display...")
7137 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7138 ))
7139 (or (nth 2 cperl-hierarchy)
7140 (error "No items found"))
7141 (setq update
7142 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7143 (if (if (fboundp 'display-popup-menus-p)
7144 (let ((f 'display-popup-menus-p))
7145 (funcall f))
7146 window-system)
7147 (x-popup-menu t (nth 2 cperl-hierarchy))
7148 (require 'tmm)
7149 (tmm-prompt (nth 2 cperl-hierarchy))))
7150 (if (and update (listp update))
7151 (progn (while (cdr update) (setq update (cdr update)))
7152 (setq update (car update)))) ; Get the last from the list
7153 (if (vectorp update)
7154 (progn
7155 (find-file (elt update 0))
7156 (cperl-etags-goto-tag-location (elt update 1))))
7157 (if (eq update -999) (cperl-tags-hier-init t)))
7158
7159 (defun cperl-tags-treeify (to level)
7160 ;; cadr of `to' is read-write. On start it is a cons
7161 (let* ((regexp (concat "^\\(" (mapconcat
7162 'identity
7163 (make-list level "[_a-zA-Z0-9]+")
7164 "::")
7165 "\\)\\(::\\)?"))
7166 (packages (cdr (nth 1 to)))
7167 (methods (cdr (nth 2 to)))
7168 l1 head tail cons1 cons2 ord writeto packs recurse
7169 root-packages root-functions ms many_ms same_name ps
7170 (move-deeper
7171 (function
7172 (lambda (elt)
7173 (cond ((and (string-match regexp (car elt))
7174 (or (eq ord 1) (match-end 2)))
7175 (setq head (substring (car elt) 0 (match-end 1))
7176 tail (if (match-end 2) (substring (car elt)
7177 (match-end 2)))
7178 recurse t)
7179 (if (setq cons1 (assoc head writeto)) nil
7180 ;; Need to init new head
7181 (setcdr writeto (cons (list head (list "Packages: ")
7182 (list "Methods: "))
7183 (cdr writeto)))
7184 (setq cons1 (nth 1 writeto)))
7185 (setq cons2 (nth ord cons1)) ; Either packs or meths
7186 (setcdr cons2 (cons elt (cdr cons2))))
7187 ((eq ord 2)
7188 (setq root-functions (cons elt root-functions)))
7189 (t
7190 (setq root-packages (cons elt root-packages))))))))
7191 (setcdr to l1) ; Init to dynamic space
7192 (setq writeto to)
7193 (setq ord 1)
7194 (mapc move-deeper packages)
7195 (setq ord 2)
7196 (mapc move-deeper methods)
7197 (if recurse
7198 (mapc (function (lambda (elt)
7199 (cperl-tags-treeify elt (1+ level))))
7200 (cdr to)))
7201 ;;Now clean up leaders with one child only
7202 (mapc (function (lambda (elt)
7203 (if (not (and (listp (cdr elt))
7204 (eq (length elt) 2))) nil
7205 (setcar elt (car (nth 1 elt)))
7206 (setcdr elt (cdr (nth 1 elt))))))
7207 (cdr to))
7208 ;; Sort the roots of subtrees
7209 (if (default-value 'imenu-sort-function)
7210 (setcdr to
7211 (sort (cdr to) (default-value 'imenu-sort-function))))
7212 ;; Now add back functions removed from display
7213 (mapc (function (lambda (elt)
7214 (setcdr to (cons elt (cdr to)))))
7215 (if (default-value 'imenu-sort-function)
7216 (nreverse
7217 (sort root-functions (default-value 'imenu-sort-function)))
7218 root-functions))
7219 ;; Now add back packages removed from display
7220 (mapc (function (lambda (elt)
7221 (setcdr to (cons (cons (concat "package " (car elt))
7222 (cdr elt))
7223 (cdr to)))))
7224 (if (default-value 'imenu-sort-function)
7225 (nreverse
7226 (sort root-packages (default-value 'imenu-sort-function)))
7227 root-packages))))
7228
7229 ;;;(x-popup-menu t
7230 ;;; '(keymap "Name1"
7231 ;;; ("Ret1" "aa")
7232 ;;; ("Head1" "ab"
7233 ;;; keymap "Name2"
7234 ;;; ("Tail1" "x") ("Tail2" "y"))))
7235
7236 (defun cperl-list-fold (list name limit)
7237 (let (list1 list2 elt1 (num 0))
7238 (if (<= (length list) limit) list
7239 (setq list1 nil list2 nil)
7240 (while list
7241 (setq num (1+ num)
7242 elt1 (car list)
7243 list (cdr list))
7244 (if (<= num imenu-max-items)
7245 (setq list2 (cons elt1 list2))
7246 (setq list1 (cons (cons name
7247 (nreverse list2))
7248 list1)
7249 list2 (list elt1)
7250 num 1)))
7251 (nreverse (cons (cons name
7252 (nreverse list2))
7253 list1)))))
7254
7255 (defun cperl-menu-to-keymap (menu &optional name)
7256 (let (list)
7257 (cons 'keymap
7258 (mapcar
7259 (function
7260 (lambda (elt)
7261 (cond ((listp (cdr elt))
7262 (setq list (cperl-list-fold
7263 (cdr elt) (car elt) imenu-max-items))
7264 (cons nil
7265 (cons (car elt)
7266 (cperl-menu-to-keymap list))))
7267 (t
7268 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7269 (cperl-list-fold menu "Root" imenu-max-items)))))
7270
7271 \f
7272 (defvar cperl-bad-style-regexp
7273 (mapconcat 'identity
7274 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7275 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7276 "\\|")
7277 "Finds places such that insertion of a whitespace may help a lot.")
7278
7279 (defvar cperl-not-bad-style-regexp
7280 (mapconcat
7281 'identity
7282 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7283 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7284 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7285 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7286 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7287 "-[0-9]" ; -5
7288 "\\+\\+" ; ++var
7289 "--" ; --var
7290 ".->" ; a->b
7291 "->" ; a SPACE ->b
7292 "\\[-" ; a[-1]
7293 "\\\\[&$@*\\\\]" ; \&func
7294 "^=" ; =head
7295 "\\$." ; $|
7296 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7297 "||"
7298 "&&"
7299 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7300 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7301 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7302 ;;"[*/+-|&<.]+="
7303 )
7304 "\\|")
7305 "If matches at the start of match found by `my-bad-c-style-regexp',
7306 insertion of a whitespace will not help.")
7307
7308 (defvar found-bad)
7309
7310 (defun cperl-find-bad-style ()
7311 "Find places in the buffer where insertion of a whitespace may help.
7312 Prompts user for insertion of spaces.
7313 Currently it is tuned to C and Perl syntax."
7314 (interactive)
7315 (let (found-bad (p (point)))
7316 (setq last-nonmenu-event 13) ; To disable popup
7317 (goto-char (point-min))
7318 (map-y-or-n-p "Insert space here? "
7319 (lambda (arg) (insert " "))
7320 'cperl-next-bad-style
7321 '("location" "locations" "insert a space into")
7322 '((?\C-r (lambda (arg)
7323 (let ((buffer-quit-function
7324 'exit-recursive-edit))
7325 (message "Exit with Esc Esc")
7326 (recursive-edit)
7327 t)) ; Consider acted upon
7328 "edit, exit with Esc Esc")
7329 (?e (lambda (arg)
7330 (let ((buffer-quit-function
7331 'exit-recursive-edit))
7332 (message "Exit with Esc Esc")
7333 (recursive-edit)
7334 t)) ; Consider acted upon
7335 "edit, exit with Esc Esc"))
7336 t)
7337 (if found-bad (goto-char found-bad)
7338 (goto-char p)
7339 (message "No appropriate place found"))))
7340
7341 (defun cperl-next-bad-style ()
7342 (let (p (not-found t) (point (point)) found)
7343 (while (and not-found
7344 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7345 (setq p (point))
7346 (goto-char (match-beginning 0))
7347 (if (or
7348 (looking-at cperl-not-bad-style-regexp)
7349 ;; Check for a < -b and friends
7350 (and (eq (following-char) ?\-)
7351 (save-excursion
7352 (skip-chars-backward " \t\n")
7353 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7354 ;; Now check for syntax type
7355 (save-match-data
7356 (setq found (point))
7357 (beginning-of-defun)
7358 (let ((pps (parse-partial-sexp (point) found)))
7359 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7360 (goto-char (match-end 0))
7361 (goto-char (1- p))
7362 (setq not-found nil
7363 found-bad found)))
7364 (not not-found)))
7365
7366 \f
7367 ;;; Getting help
7368 (defvar cperl-have-help-regexp
7369 ;;(concat "\\("
7370 (mapconcat
7371 'identity
7372 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7373 "[$@]\\^[a-zA-Z]" ; Special variable
7374 "[$@][^ \n\t]" ; Special variable
7375 "-[a-zA-Z]" ; File test
7376 "\\\\[a-zA-Z0]" ; Special chars
7377 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7378 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7379 "[a-zA-Z_0-9:]+" ; symbol or number
7380 "x="
7381 "#!")
7382 ;;"\\)\\|\\("
7383 "\\|")
7384 ;;"\\)"
7385 ;;)
7386 "Matches places in the buffer we can find help for.")
7387
7388 (defvar cperl-message-on-help-error t)
7389 (defvar cperl-help-from-timer nil)
7390
7391 (defun cperl-word-at-point-hard ()
7392 ;; Does not save-excursion
7393 ;; Get to the something meaningful
7394 (or (eobp) (eolp) (forward-char 1))
7395 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7396 (save-excursion (beginning-of-line) (point))
7397 'to-beg)
7398 ;; (cond
7399 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7400 ;; (skip-chars-backward " \n\t\r({[]});,")
7401 ;; (or (bobp) (backward-char 1))))
7402 ;; Try to backtrace
7403 (cond
7404 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7405 (skip-chars-backward "a-zA-Z0-9_:")
7406 (cond
7407 ((and (eq (preceding-char) ?^) ; $^I
7408 (eq (char-after (- (point) 2)) ?\$))
7409 (forward-char -2))
7410 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7411 (forward-char -1))
7412 ((and (eq (preceding-char) ?\=)
7413 (eq (current-column) 1))
7414 (forward-char -1))) ; =head1
7415 (if (and (eq (preceding-char) ?\<)
7416 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7417 (forward-char -1)))
7418 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7419 (forward-char -1))
7420 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7421 (forward-char -1))
7422 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7423 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7424 (cond
7425 ((and (eq (preceding-char) ?\$)
7426 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7427 (forward-char -1))
7428 ((and (eq (following-char) ?\>)
7429 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7430 (save-excursion
7431 (forward-sexp -1)
7432 (and (eq (preceding-char) ?\<)
7433 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7434 (search-backward "<"))))
7435 ((and (eq (following-char) ?\$)
7436 (eq (preceding-char) ?\<)
7437 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7438 (forward-char -1)))
7439 (if (looking-at cperl-have-help-regexp)
7440 (buffer-substring (match-beginning 0) (match-end 0))))
7441
7442 (defun cperl-get-help ()
7443 "Get one-line docs on the symbol at the point.
7444 The data for these docs is a little bit obsolete and may be in fact longer
7445 than a line. Your contribution to update/shorten it is appreciated."
7446 (interactive)
7447 (save-match-data ; May be called "inside" query-replace
7448 (save-excursion
7449 (let ((word (cperl-word-at-point-hard)))
7450 (if word
7451 (if (and cperl-help-from-timer ; Bail out if not in mainland
7452 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7453 (or (memq (get-text-property (point) 'face)
7454 '(font-lock-comment-face font-lock-string-face))
7455 (memq (get-text-property (point) 'syntax-type)
7456 '(pod here-doc format))))
7457 nil
7458 (cperl-describe-perl-symbol word))
7459 (if cperl-message-on-help-error
7460 (message "Nothing found for %s..."
7461 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7462
7463 ;;; Stolen from perl-descr.el by Johan Vromans:
7464
7465 (defvar cperl-doc-buffer " *perl-doc*"
7466 "Where the documentation can be found.")
7467
7468 (defun cperl-describe-perl-symbol (val)
7469 "Display the documentation of symbol at point, a Perl operator."
7470 (let ((enable-recursive-minibuffers t)
7471 args-file regexp)
7472 (cond
7473 ((string-match "^[&*][a-zA-Z_]" val)
7474 (setq val (concat (substring val 0 1) "NAME")))
7475 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7476 (setq val (concat "@" (substring val 1 (match-end 1)))))
7477 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7478 (setq val (concat "%" (substring val 1 (match-end 1)))))
7479 ((and (string= val "x") (string-match "^x=" val))
7480 (setq val "x="))
7481 ((string-match "^\\$[\C-a-\C-z]" val)
7482 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7483 ((string-match "^CORE::" val)
7484 (setq val "CORE::"))
7485 ((string-match "^SUPER::" val)
7486 (setq val "SUPER::"))
7487 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7488 (setq val "<NAME>")))
7489 (setq regexp (concat "^"
7490 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7491 (regexp-quote val)
7492 "\\([ \t([/]\\|$\\)"))
7493
7494 ;; get the buffer with the documentation text
7495 (cperl-switch-to-doc-buffer)
7496
7497 ;; lookup in the doc
7498 (goto-char (point-min))
7499 (let ((case-fold-search nil))
7500 (list
7501 (if (re-search-forward regexp (point-max) t)
7502 (save-excursion
7503 (beginning-of-line 1)
7504 (let ((lnstart (point)))
7505 (end-of-line)
7506 (message "%s" (buffer-substring lnstart (point)))))
7507 (if cperl-message-on-help-error
7508 (message "No definition for %s" val)))))))
7509
7510 (defvar cperl-short-docs 'please-ignore-this-line
7511 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7512 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7513 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7514 ! ... Logical negation.
7515 ... != ... Numeric inequality.
7516 ... !~ ... Search pattern, substitution, or translation (negated).
7517 $! In numeric context: errno. In a string context: error string.
7518 $\" The separator which joins elements of arrays interpolated in strings.
7519 $# The output format for printed numbers. Default is %.15g or close.
7520 $$ Process number of this script. Changes in the fork()ed child process.
7521 $% The current page number of the currently selected output channel.
7522
7523 The following variables are always local to the current block:
7524
7525 $1 Match of the 1st set of parentheses in the last match (auto-local).
7526 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7527 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7528 $4 Match of the 4th set of parentheses in the last match (auto-local).
7529 $5 Match of the 5th set of parentheses in the last match (auto-local).
7530 $6 Match of the 6th set of parentheses in the last match (auto-local).
7531 $7 Match of the 7th set of parentheses in the last match (auto-local).
7532 $8 Match of the 8th set of parentheses in the last match (auto-local).
7533 $9 Match of the 9th set of parentheses in the last match (auto-local).
7534 $& The string matched by the last pattern match (auto-local).
7535 $' The string after what was matched by the last match (auto-local).
7536 $` The string before what was matched by the last match (auto-local).
7537
7538 $( The real gid of this process.
7539 $) The effective gid of this process.
7540 $* Deprecated: Set to 1 to do multiline matching within a string.
7541 $+ The last bracket matched by the last search pattern.
7542 $, The output field separator for the print operator.
7543 $- The number of lines left on the page.
7544 $. The current input line number of the last filehandle that was read.
7545 $/ The input record separator, newline by default.
7546 $0 Name of the file containing the current perl script (read/write).
7547 $: String may be broken after these characters to fill ^-lines in a format.
7548 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7549 $< The real uid of this process.
7550 $= The page length of the current output channel. Default is 60 lines.
7551 $> The effective uid of this process.
7552 $? The status returned by the last ``, pipe close or `system'.
7553 $@ The perl error message from the last eval or do @var{EXPR} command.
7554 $ARGV The name of the current file used with <> .
7555 $[ Deprecated: The index of the first element/char in an array/string.
7556 $\\ The output record separator for the print operator.
7557 $] The perl version string as displayed with perl -v.
7558 $^ The name of the current top-of-page format.
7559 $^A The current value of the write() accumulator for format() lines.
7560 $^D The value of the perl debug (-D) flags.
7561 $^E Information about the last system error other than that provided by $!.
7562 $^F The highest system file descriptor, ordinarily 2.
7563 $^H The current set of syntax checks enabled by `use strict'.
7564 $^I The value of the in-place edit extension (perl -i option).
7565 $^L What formats output to perform a formfeed. Default is \\f.
7566 $^M A buffer for emergency memory allocation when running out of memory.
7567 $^O The operating system name under which this copy of Perl was built.
7568 $^P Internal debugging flag.
7569 $^T The time the script was started. Used by -A/-M/-C file tests.
7570 $^W True if warnings are requested (perl -w flag).
7571 $^X The name under which perl was invoked (argv[0] in C-speech).
7572 $_ The default input and pattern-searching space.
7573 $| Auto-flush after write/print on current output channel? Default 0.
7574 $~ The name of the current report format.
7575 ... % ... Modulo division.
7576 ... %= ... Modulo division assignment.
7577 %ENV Contains the current environment.
7578 %INC List of files that have been require-d or do-ne.
7579 %SIG Used to set signal handlers for various signals.
7580 ... & ... Bitwise and.
7581 ... && ... Logical and.
7582 ... &&= ... Logical and assignment.
7583 ... &= ... Bitwise and assignment.
7584 ... * ... Multiplication.
7585 ... ** ... Exponentiation.
7586 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7587 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7588 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7589 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7590 ... += ... Addition assignment.
7591 , Comma operator.
7592 ... - ... Subtraction.
7593 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7594 ... -= ... Subtraction assignment.
7595 -A Access time in days since script started.
7596 -B File is a non-text (binary) file.
7597 -C Inode change time in days since script started.
7598 -M Age in days since script started.
7599 -O File is owned by real uid.
7600 -R File is readable by real uid.
7601 -S File is a socket .
7602 -T File is a text file.
7603 -W File is writable by real uid.
7604 -X File is executable by real uid.
7605 -b File is a block special file.
7606 -c File is a character special file.
7607 -d File is a directory.
7608 -e File exists .
7609 -f File is a plain file.
7610 -g File has setgid bit set.
7611 -k File has sticky bit set.
7612 -l File is a symbolic link.
7613 -o File is owned by effective uid.
7614 -p File is a named pipe (FIFO).
7615 -r File is readable by effective uid.
7616 -s File has non-zero size.
7617 -t Tests if filehandle (STDIN by default) is opened to a tty.
7618 -u File has setuid bit set.
7619 -w File is writable by effective uid.
7620 -x File is executable by effective uid.
7621 -z File has zero size.
7622 . Concatenate strings.
7623 .. Range (list context); flip/flop (scalar context) operator.
7624 .= Concatenate assignment strings
7625 ... / ... Division. /PATTERN/ioxsmg Pattern match
7626 ... /= ... Division assignment.
7627 /PATTERN/ioxsmg Pattern match.
7628 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7629 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7630 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7631 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7632 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7633 ... <= ... Numeric less than or equal to.
7634 ... <=> ... Numeric compare.
7635 ... = ... Assignment.
7636 ... == ... Numeric equality.
7637 ... =~ ... Search pattern, substitution, or translation
7638 ... > ... Numeric greater than.
7639 ... >= ... Numeric greater than or equal to.
7640 ... >> ... Bitwise shift right.
7641 ... >>= ... Bitwise shift right assignment.
7642 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7643 ?PATTERN? One-time pattern match.
7644 @ARGV Command line arguments (not including the command name - see $0).
7645 @INC List of places to look for perl scripts during do/include/use.
7646 @_ Parameter array for subroutines; result of split() unless in list context.
7647 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7648 \\0 Octal char, e.g. \\033.
7649 \\E Case modification terminator. See \\Q, \\L, and \\U.
7650 \\L Lowercase until \\E . See also \\l, lc.
7651 \\U Upcase until \\E . See also \\u, uc.
7652 \\Q Quote metacharacters until \\E . See also quotemeta.
7653 \\a Alarm character (octal 007).
7654 \\b Backspace character (octal 010).
7655 \\c Control character, e.g. \\c[ .
7656 \\e Escape character (octal 033).
7657 \\f Formfeed character (octal 014).
7658 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7659 \\n Newline character (octal 012 on most systems).
7660 \\r Return character (octal 015 on most systems).
7661 \\t Tab character (octal 011).
7662 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7663 \\x Hex character, e.g. \\x1b.
7664 ... ^ ... Bitwise exclusive or.
7665 __END__ Ends program source.
7666 __DATA__ Ends program source.
7667 __FILE__ Current (source) filename.
7668 __LINE__ Current line in current source.
7669 __PACKAGE__ Current package.
7670 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7671 ARGVOUT Output filehandle with -i flag.
7672 BEGIN { ... } Immediately executed (during compilation) piece of code.
7673 END { ... } Pseudo-subroutine executed after the script finishes.
7674 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7675 INIT { ... } Pseudo-subroutine executed before the script starts running.
7676 DATA Input filehandle for what follows after __END__ or __DATA__.
7677 accept(NEWSOCKET,GENERICSOCKET)
7678 alarm(SECONDS)
7679 atan2(X,Y)
7680 bind(SOCKET,NAME)
7681 binmode(FILEHANDLE)
7682 caller[(LEVEL)]
7683 chdir(EXPR)
7684 chmod(LIST)
7685 chop[(LIST|VAR)]
7686 chown(LIST)
7687 chroot(FILENAME)
7688 close(FILEHANDLE)
7689 closedir(DIRHANDLE)
7690 ... cmp ... String compare.
7691 connect(SOCKET,NAME)
7692 continue of { block } continue { block }. Is executed after `next' or at end.
7693 cos(EXPR)
7694 crypt(PLAINTEXT,SALT)
7695 dbmclose(%HASH)
7696 dbmopen(%HASH,DBNAME,MODE)
7697 defined(EXPR)
7698 delete($HASH{KEY})
7699 die(LIST)
7700 do { ... }|SUBR while|until EXPR executes at least once
7701 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7702 dump LABEL
7703 each(%HASH)
7704 endgrent
7705 endhostent
7706 endnetent
7707 endprotoent
7708 endpwent
7709 endservent
7710 eof[([FILEHANDLE])]
7711 ... eq ... String equality.
7712 eval(EXPR) or eval { BLOCK }
7713 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7714 exit(EXPR)
7715 exp(EXPR)
7716 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7717 fileno(FILEHANDLE)
7718 flock(FILEHANDLE,OPERATION)
7719 for (EXPR;EXPR;EXPR) { ... }
7720 foreach [VAR] (@ARRAY) { ... }
7721 fork
7722 ... ge ... String greater than or equal.
7723 getc[(FILEHANDLE)]
7724 getgrent
7725 getgrgid(GID)
7726 getgrnam(NAME)
7727 gethostbyaddr(ADDR,ADDRTYPE)
7728 gethostbyname(NAME)
7729 gethostent
7730 getlogin
7731 getnetbyaddr(ADDR,ADDRTYPE)
7732 getnetbyname(NAME)
7733 getnetent
7734 getpeername(SOCKET)
7735 getpgrp(PID)
7736 getppid
7737 getpriority(WHICH,WHO)
7738 getprotobyname(NAME)
7739 getprotobynumber(NUMBER)
7740 getprotoent
7741 getpwent
7742 getpwnam(NAME)
7743 getpwuid(UID)
7744 getservbyname(NAME,PROTO)
7745 getservbyport(PORT,PROTO)
7746 getservent
7747 getsockname(SOCKET)
7748 getsockopt(SOCKET,LEVEL,OPTNAME)
7749 gmtime(EXPR)
7750 goto LABEL
7751 ... gt ... String greater than.
7752 hex(EXPR)
7753 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7754 index(STR,SUBSTR[,OFFSET])
7755 int(EXPR)
7756 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7757 join(EXPR,LIST)
7758 keys(%HASH)
7759 kill(LIST)
7760 last [LABEL]
7761 ... le ... String less than or equal.
7762 length(EXPR)
7763 link(OLDFILE,NEWFILE)
7764 listen(SOCKET,QUEUESIZE)
7765 local(LIST)
7766 localtime(EXPR)
7767 log(EXPR)
7768 lstat(EXPR|FILEHANDLE|VAR)
7769 ... lt ... String less than.
7770 m/PATTERN/iogsmx
7771 mkdir(FILENAME,MODE)
7772 msgctl(ID,CMD,ARG)
7773 msgget(KEY,FLAGS)
7774 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7775 msgsnd(ID,MSG,FLAGS)
7776 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7777 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7778 ... ne ... String inequality.
7779 next [LABEL]
7780 oct(EXPR)
7781 open(FILEHANDLE[,EXPR])
7782 opendir(DIRHANDLE,EXPR)
7783 ord(EXPR) ASCII value of the first char of the string.
7784 pack(TEMPLATE,LIST)
7785 package NAME Introduces package context.
7786 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7787 pop(ARRAY)
7788 print [FILEHANDLE] [(LIST)]
7789 printf [FILEHANDLE] (FORMAT,LIST)
7790 push(ARRAY,LIST)
7791 q/STRING/ Synonym for 'STRING'
7792 qq/STRING/ Synonym for \"STRING\"
7793 qx/STRING/ Synonym for `STRING`
7794 rand[(EXPR)]
7795 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7796 readdir(DIRHANDLE)
7797 readlink(EXPR)
7798 recv(SOCKET,SCALAR,LEN,FLAGS)
7799 redo [LABEL]
7800 rename(OLDNAME,NEWNAME)
7801 require [FILENAME | PERL_VERSION]
7802 reset[(EXPR)]
7803 return(LIST)
7804 reverse(LIST)
7805 rewinddir(DIRHANDLE)
7806 rindex(STR,SUBSTR[,OFFSET])
7807 rmdir(FILENAME)
7808 s/PATTERN/REPLACEMENT/gieoxsm
7809 scalar(EXPR)
7810 seek(FILEHANDLE,POSITION,WHENCE)
7811 seekdir(DIRHANDLE,POS)
7812 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7813 semctl(ID,SEMNUM,CMD,ARG)
7814 semget(KEY,NSEMS,SIZE,FLAGS)
7815 semop(KEY,...)
7816 send(SOCKET,MSG,FLAGS[,TO])
7817 setgrent
7818 sethostent(STAYOPEN)
7819 setnetent(STAYOPEN)
7820 setpgrp(PID,PGRP)
7821 setpriority(WHICH,WHO,PRIORITY)
7822 setprotoent(STAYOPEN)
7823 setpwent
7824 setservent(STAYOPEN)
7825 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7826 shift[(ARRAY)]
7827 shmctl(ID,CMD,ARG)
7828 shmget(KEY,SIZE,FLAGS)
7829 shmread(ID,VAR,POS,SIZE)
7830 shmwrite(ID,STRING,POS,SIZE)
7831 shutdown(SOCKET,HOW)
7832 sin(EXPR)
7833 sleep[(EXPR)]
7834 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7835 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7836 sort [SUBROUTINE] (LIST)
7837 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7838 split[(/PATTERN/[,EXPR[,LIMIT]])]
7839 sprintf(FORMAT,LIST)
7840 sqrt(EXPR)
7841 srand(EXPR)
7842 stat(EXPR|FILEHANDLE|VAR)
7843 study[(SCALAR)]
7844 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7845 substr(EXPR,OFFSET[,LEN])
7846 symlink(OLDFILE,NEWFILE)
7847 syscall(LIST)
7848 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7849 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7850 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7851 tell[(FILEHANDLE)]
7852 telldir(DIRHANDLE)
7853 time
7854 times
7855 tr/SEARCHLIST/REPLACEMENTLIST/cds
7856 truncate(FILE|EXPR,LENGTH)
7857 umask[(EXPR)]
7858 undef[(EXPR)]
7859 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7860 unlink(LIST)
7861 unpack(TEMPLATE,EXPR)
7862 unshift(ARRAY,LIST)
7863 until (EXPR) { ... } EXPR until EXPR
7864 utime(LIST)
7865 values(%HASH)
7866 vec(EXPR,OFFSET,BITS)
7867 wait
7868 waitpid(PID,FLAGS)
7869 wantarray Returns true if the sub/eval is called in list context.
7870 warn(LIST)
7871 while (EXPR) { ... } EXPR while EXPR
7872 write[(EXPR|FILEHANDLE)]
7873 ... x ... Repeat string or array.
7874 x= ... Repetition assignment.
7875 y/SEARCHLIST/REPLACEMENTLIST/
7876 ... | ... Bitwise or.
7877 ... || ... Logical or.
7878 ~ ... Unary bitwise complement.
7879 #! OS interpreter indicator. If contains `perl', used for options, and -x.
7880 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7881 CORE:: Prefix to access builtin function if imported sub obscures it.
7882 SUPER:: Prefix to lookup for a method in @ISA classes.
7883 DESTROY Shorthand for `sub DESTROY {...}'.
7884 ... EQ ... Obsolete synonym of `eq'.
7885 ... GE ... Obsolete synonym of `ge'.
7886 ... GT ... Obsolete synonym of `gt'.
7887 ... LE ... Obsolete synonym of `le'.
7888 ... LT ... Obsolete synonym of `lt'.
7889 ... NE ... Obsolete synonym of `ne'.
7890 abs [ EXPR ] absolute value
7891 ... and ... Low-precedence synonym for &&.
7892 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7893 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
7894 chr Converts a number to char with the same ordinal.
7895 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7896 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7897 exists $HASH{KEY} True if the key exists.
7898 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7899 formline PICTURE, LIST Backdoor into \"format\" processing.
7900 glob EXPR Synonym of <EXPR>.
7901 lc [ EXPR ] Returns lowercased EXPR.
7902 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
7903 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
7904 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
7905 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
7906 not ... Low-precedence synonym for ! - negation.
7907 ... or ... Low-precedence synonym for ||.
7908 pos STRING Set/Get end-position of the last match over this string, see \\G.
7909 quotemeta [ EXPR ] Quote regexp metacharacters.
7910 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
7911 readline FH Synonym of <FH>.
7912 readpipe CMD Synonym of `CMD`.
7913 ref [ EXPR ] Type of EXPR when dereferenced.
7914 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
7915 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
7916 tied Returns internal object for a tied data.
7917 uc [ EXPR ] Returns upcased EXPR.
7918 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
7919 untie VAR Unlink an object from a simple Perl variable.
7920 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
7921 ... xor ... Low-precedence synonym for exclusive or.
7922 prototype \\&SUB Returns prototype of the function given a reference.
7923 =head1 Top-level heading.
7924 =head2 Second-level heading.
7925 =head3 Third-level heading (is there such?).
7926 =over [ NUMBER ] Start list.
7927 =item [ TITLE ] Start new item in the list.
7928 =back End list.
7929 =cut Switch from POD to Perl.
7930 =pod Switch from Perl to POD.
7931 ")
7932
7933 (defun cperl-switch-to-doc-buffer (&optional interactive)
7934 "Go to the perl documentation buffer and insert the documentation."
7935 (interactive "p")
7936 (let ((buf (get-buffer-create cperl-doc-buffer)))
7937 (if interactive
7938 (switch-to-buffer-other-window buf)
7939 (set-buffer buf))
7940 (if (= (buffer-size) 0)
7941 (progn
7942 (insert (documentation-property 'cperl-short-docs
7943 'variable-documentation))
7944 (setq buffer-read-only t)))))
7945
7946 (defun cperl-beautify-regexp-piece (b e embed level)
7947 ;; b is before the starting delimiter, e before the ending
7948 ;; e should be a marker, may be changed, but remains "correct".
7949 ;; EMBED is nil if we process the whole REx.
7950 ;; The REx is guaranteed to have //x
7951 ;; LEVEL shows how many levels deep to go
7952 ;; position at enter and at leave is not defined
7953 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
7954 (if (not embed)
7955 (goto-char (1+ b))
7956 (goto-char b)
7957 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
7958 (forward-char 2)
7959 (delete-char 1)
7960 (forward-char 1))
7961 ((looking-at "(\\?[^a-zA-Z]")
7962 (forward-char 3))
7963 ((looking-at "(\\?") ; (?i)
7964 (forward-char 2))
7965 (t
7966 (forward-char 1))))
7967 (setq c (if embed (current-indentation) (1- (current-column)))
7968 c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
7969 (or (looking-at "[ \t]*[\n#]")
7970 (progn
7971 (insert "\n")))
7972 (goto-char e)
7973 (beginning-of-line)
7974 (if (re-search-forward "[^ \t]" e t)
7975 (progn ; Something before the ending delimiter
7976 (goto-char e)
7977 (delete-horizontal-space)
7978 (insert "\n")
7979 (cperl-make-indent c)
7980 (set-marker e (point))))
7981 (goto-char b)
7982 (end-of-line 2)
7983 (while (< (point) (marker-position e))
7984 (beginning-of-line)
7985 (setq s (point)
7986 inline t)
7987 (skip-chars-forward " \t")
7988 (delete-region s (point))
7989 (cperl-make-indent c1)
7990 (while (and
7991 inline
7992 (looking-at
7993 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
7994 "\\|" ; Embedded variable
7995 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
7996 "\\|" ; $ ^
7997 "[$^]"
7998 "\\|" ; simple-code simple-code*?
7999 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8000 "\\|" ; Class
8001 "\\(\\[\\)" ; 6
8002 "\\|" ; Grouping
8003 "\\((\\(\\?\\)?\\)" ; 7 8
8004 "\\|" ; |
8005 "\\(|\\)"))) ; 9
8006 (goto-char (match-end 0))
8007 (setq spaces t)
8008 (cond ((match-beginning 1) ; Alphanum word + junk
8009 (forward-char -1))
8010 ((or (match-beginning 3) ; $ab[12]
8011 (and (match-beginning 5) ; X* X+ X{2,3}
8012 (eq (preceding-char) ?\{)))
8013 (forward-char -1)
8014 (forward-sexp 1))
8015 ((and ; [], already syntaxified
8016 (match-beginning 6)
8017 cperl-regexp-scan
8018 cperl-use-syntax-table-text-property)
8019 (forward-char -1)
8020 (forward-sexp 1)
8021 (or (eq (preceding-char) ?\])
8022 (error "[]-group not terminated"))
8023 (re-search-forward
8024 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8025 ((match-beginning 6) ; []
8026 (setq tmp (point))
8027 (if (looking-at "\\^?\\]")
8028 (goto-char (match-end 0)))
8029 ;; XXXX POSIX classes?!
8030 (while (and (not pos)
8031 (re-search-forward "\\[:\\|\\]" e t))
8032 (if (eq (preceding-char) ?:)
8033 (or (re-search-forward ":\\]" e t)
8034 (error "[:POSIX:]-group in []-group not terminated"))
8035 (setq pos t)))
8036 (or (eq (preceding-char) ?\])
8037 (error "[]-group not terminated"))
8038 (re-search-forward
8039 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8040 ((match-beginning 7) ; ()
8041 (goto-char (match-beginning 0))
8042 (setq pos (current-column))
8043 (or (eq pos c1)
8044 (progn
8045 (delete-horizontal-space)
8046 (insert "\n")
8047 (cperl-make-indent c1)))
8048 (setq tmp (point))
8049 (forward-sexp 1)
8050 ;; (or (forward-sexp 1)
8051 ;; (progn
8052 ;; (goto-char tmp)
8053 ;; (error "()-group not terminated")))
8054 (set-marker m (1- (point)))
8055 (set-marker m1 (point))
8056 (if (= level 1)
8057 (if (progn ; indent rigidly if multiline
8058 ;; In fact does not make a lot of sense, since
8059 ;; the starting position can be already lost due
8060 ;; to insertion of "\n" and " "
8061 (goto-char tmp)
8062 (search-forward "\n" m1 t))
8063 (indent-rigidly (point) m1 (- c1 pos)))
8064 (setq level (1- level))
8065 (cond
8066 ((not (match-beginning 8))
8067 (cperl-beautify-regexp-piece tmp m t level))
8068 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8069 t)
8070 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8071 (goto-char (+ 2 tmp))
8072 (forward-sexp 1)
8073 (cperl-beautify-regexp-piece (point) m t level))
8074 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8075 (goto-char (+ 3 tmp))
8076 (cperl-beautify-regexp-piece (point) m t level))
8077 (t
8078 (cperl-beautify-regexp-piece tmp m t level))))
8079 (goto-char m1)
8080 (cond ((looking-at "[*+?]\\??")
8081 (goto-char (match-end 0)))
8082 ((eq (following-char) ?\{)
8083 (forward-sexp 1)
8084 (if (eq (following-char) ?\?)
8085 (forward-char))))
8086 (skip-chars-forward " \t")
8087 (setq spaces nil)
8088 (if (looking-at "[#\n]")
8089 (progn
8090 (or (eolp) (indent-for-comment))
8091 (beginning-of-line 2))
8092 (delete-horizontal-space)
8093 (insert "\n"))
8094 (end-of-line)
8095 (setq inline nil))
8096 ((match-beginning 9) ; |
8097 (forward-char -1)
8098 (setq tmp (point))
8099 (beginning-of-line)
8100 (if (re-search-forward "[^ \t]" tmp t)
8101 (progn
8102 (goto-char tmp)
8103 (delete-horizontal-space)
8104 (insert "\n"))
8105 ;; first at line
8106 (delete-region (point) tmp))
8107 (cperl-make-indent c)
8108 (forward-char 1)
8109 (skip-chars-forward " \t")
8110 (setq spaces nil)
8111 (if (looking-at "[#\n]")
8112 (beginning-of-line 2)
8113 (delete-horizontal-space)
8114 (insert "\n"))
8115 (end-of-line)
8116 (setq inline nil)))
8117 (or (looking-at "[ \t\n]")
8118 (not spaces)
8119 (insert " "))
8120 (skip-chars-forward " \t"))
8121 (or (looking-at "[#\n]")
8122 (error "Unknown code `%s' in a regexp"
8123 (buffer-substring (point) (1+ (point)))))
8124 (and inline (end-of-line 2)))
8125 ;; Special-case the last line of group
8126 (if (and (>= (point) (marker-position e))
8127 (/= (current-indentation) c))
8128 (progn
8129 (beginning-of-line)
8130 (cperl-make-indent c)))))
8131
8132 (defun cperl-make-regexp-x ()
8133 ;; Returns position of the start
8134 ;; XXX this is called too often! Need to cache the result!
8135 (save-excursion
8136 (or cperl-use-syntax-table-text-property
8137 (error "I need to have a regexp marked!"))
8138 ;; Find the start
8139 (if (looking-at "\\s|")
8140 nil ; good already
8141 (if (looking-at "\\([smy]\\|qr\\)\\s|")
8142 (forward-char 1)
8143 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8144 ;;(forward-char 1)
8145 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8146 (sub-p (eq (preceding-char) ?s)) s)
8147 (forward-sexp 1)
8148 (set-marker e (1- (point)))
8149 (setq delim (preceding-char))
8150 (if (and sub-p (eq delim (char-after (- (point) 2))))
8151 (error "Possible s/blah// - do not know how to deal with"))
8152 (if sub-p (forward-sexp 1))
8153 (if (looking-at "\\sw*x")
8154 (setq have-x t)
8155 (insert "x"))
8156 ;; Protect fragile " ", "#"
8157 (if have-x nil
8158 (goto-char (1+ b))
8159 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8160 (forward-char -1)
8161 (insert "\\")
8162 (forward-char 1)))
8163 b)))
8164
8165 (defun cperl-beautify-regexp (&optional deep)
8166 "Do it. (Experimental, may change semantics, recheck the result.)
8167 We suppose that the regexp is scanned already."
8168 (interactive "P")
8169 (setq deep (if deep (prefix-numeric-value deep) -1))
8170 (save-excursion
8171 (goto-char (cperl-make-regexp-x))
8172 (let ((b (point)) (e (make-marker)))
8173 (forward-sexp 1)
8174 (set-marker e (1- (point)))
8175 (cperl-beautify-regexp-piece b e nil deep))))
8176
8177 (defun cperl-regext-to-level-start ()
8178 "Goto start of an enclosing group in regexp.
8179 We suppose that the regexp is scanned already."
8180 (interactive)
8181 (let ((limit (cperl-make-regexp-x)) done)
8182 (while (not done)
8183 (or (eq (following-char) ?\()
8184 (search-backward "(" (1+ limit) t)
8185 (error "Cannot find `(' which starts a group"))
8186 (setq done
8187 (save-excursion
8188 (skip-chars-backward "\\")
8189 (looking-at "\\(\\\\\\\\\\)*(")))
8190 (or done (forward-char -1)))))
8191
8192 (defun cperl-contract-level ()
8193 "Find an enclosing group in regexp and contract it.
8194 \(Experimental, may change semantics, recheck the result.)
8195 We suppose that the regexp is scanned already."
8196 (interactive)
8197 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8198 (cperl-regext-to-level-start)
8199 (let ((b (point)) (e (make-marker)) c)
8200 (forward-sexp 1)
8201 (set-marker e (1- (point)))
8202 (goto-char b)
8203 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8204 (cond
8205 ((match-beginning 1) ; #-comment
8206 (or c (setq c (current-indentation)))
8207 (beginning-of-line 2) ; Skip
8208 (cperl-make-indent c))
8209 (t
8210 (delete-char -1)
8211 (just-one-space))))))
8212
8213 (defun cperl-contract-levels ()
8214 "Find an enclosing group in regexp and contract all the kids.
8215 \(Experimental, may change semantics, recheck the result.)
8216 We suppose that the regexp is scanned already."
8217 (interactive)
8218 (save-excursion
8219 (condition-case nil
8220 (cperl-regext-to-level-start)
8221 (error ; We are outside outermost group
8222 (goto-char (cperl-make-regexp-x))))
8223 (let ((b (point)) (e (make-marker)) s c)
8224 (forward-sexp 1)
8225 (set-marker e (1- (point)))
8226 (goto-char (1+ b))
8227 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8228 (cond
8229 ((match-beginning 1) ; Skip
8230 nil)
8231 (t ; Group
8232 (cperl-contract-level)))))))
8233
8234 (defun cperl-beautify-level (&optional deep)
8235 "Find an enclosing group in regexp and beautify it.
8236 \(Experimental, may change semantics, recheck the result.)
8237 We suppose that the regexp is scanned already."
8238 (interactive "P")
8239 (setq deep (if deep (prefix-numeric-value deep) -1))
8240 (save-excursion
8241 (cperl-regext-to-level-start)
8242 (let ((b (point)) (e (make-marker)))
8243 (forward-sexp 1)
8244 (set-marker e (1- (point)))
8245 (cperl-beautify-regexp-piece b e nil deep))))
8246
8247 (defun cperl-invert-if-unless-modifiers ()
8248 "Change `B if A;' into `if (A) {B}' etc if possible.
8249 \(Unfinished.)"
8250 (interactive) ;
8251 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8252 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8253 (and (= (char-syntax (preceding-char)) ?w)
8254 (forward-sexp -1))
8255 (setq pre-if (point))
8256 (cperl-backward-to-start-of-expr)
8257 (setq pre-B (point))
8258 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8259 (cperl-forward-to-end-of-expr)
8260 (setq post-A (point))
8261 (goto-char pre-if)
8262 (or (looking-at w-rex)
8263 ;; Find the position
8264 (progn (goto-char post-A)
8265 (while (and
8266 (not (looking-at w-rex))
8267 (> (point) pre-B))
8268 (forward-sexp -1))
8269 (setq pre-if (point))))
8270 (or (looking-at w-rex)
8271 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8272 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8273 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8274 ;; First, simple part: find code boundaries
8275 (forward-sexp 1)
8276 (setq post-if (point))
8277 (forward-sexp -2)
8278 (forward-sexp 1)
8279 (setq post-B (point))
8280 (cperl-backward-to-start-of-expr)
8281 (setq pre-B (point))
8282 (setq B (buffer-substring pre-B post-B))
8283 (goto-char pre-if)
8284 (forward-sexp 2)
8285 (forward-sexp -1)
8286 ;; May be after $, @, $# etc of a variable
8287 (skip-chars-backward "$@%#")
8288 (setq pre-A (point))
8289 (cperl-forward-to-end-of-expr)
8290 (setq post-A (point))
8291 (setq A (buffer-substring pre-A post-A))
8292 ;; Now modify (from end, to not break the stuff)
8293 (skip-chars-forward " \t;")
8294 (delete-region pre-A (point)) ; we move to pre-A
8295 (insert "\n" B ";\n}")
8296 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8297 (delete-region pre-if post-if)
8298 (delete-region pre-B post-B)
8299 (goto-char pre-B)
8300 (insert if-string " (" A ") {")
8301 (setq post-B (point))
8302 (if (looking-at "[ \t]+$")
8303 (delete-horizontal-space)
8304 (if (looking-at "[ \t]*#")
8305 (cperl-indent-for-comment)
8306 (just-one-space)))
8307 (forward-line 1)
8308 (if (looking-at "[ \t]*$")
8309 (progn ; delete line
8310 (delete-horizontal-space)
8311 (delete-region (point) (1+ (point)))))
8312 (cperl-indent-line)
8313 (goto-char (1- post-B))
8314 (forward-sexp 1)
8315 (cperl-indent-line)
8316 (goto-char pre-B)))
8317
8318 (defun cperl-invert-if-unless ()
8319 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8320 If the cursor is not on the leading keyword of the BLOCK flavor of
8321 construct, will assume it is the STATEMENT flavor, so will try to find
8322 the appropriate statement modifier."
8323 (interactive)
8324 (and (= (char-syntax (preceding-char)) ?w)
8325 (forward-sexp -1))
8326 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8327 (let ((pre-if (point))
8328 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8329 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8330 (forward-sexp 2)
8331 (setq post-A (point))
8332 (forward-sexp -1)
8333 (setq pre-A (point))
8334 (setq is-block (and (eq (following-char) ?\( )
8335 (save-excursion
8336 (condition-case nil
8337 (progn
8338 (forward-sexp 2)
8339 (forward-sexp -1)
8340 (eq (following-char) ?\{ ))
8341 (error nil)))))
8342 (if is-block
8343 (progn
8344 (goto-char post-A)
8345 (forward-sexp 1)
8346 (setq post-B (point))
8347 (forward-sexp -1)
8348 (setq pre-B (point))
8349 (if (and (eq (following-char) ?\{ )
8350 (progn
8351 (cperl-backward-to-noncomment post-A)
8352 (eq (preceding-char) ?\) )))
8353 (if (condition-case nil
8354 (progn
8355 (goto-char post-B)
8356 (forward-sexp 1)
8357 (forward-sexp -1)
8358 (looking-at "\\<els\\(e\\|if\\)\\>"))
8359 (error nil))
8360 (error
8361 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8362 (goto-char (1- post-B))
8363 (cperl-backward-to-noncomment pre-B)
8364 (if (eq (preceding-char) ?\;)
8365 (forward-char -1))
8366 (setq end-B-code (point))
8367 (goto-char pre-B)
8368 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8369 (setq p (match-beginning 0)
8370 A (buffer-substring p (match-end 0))
8371 state (parse-partial-sexp pre-B p))
8372 (or (nth 3 state)
8373 (nth 4 state)
8374 (nth 5 state)
8375 (error "`%s' inside `%s' BLOCK" A if-string))
8376 (goto-char (match-end 0)))
8377 ;; Finally got it
8378 (goto-char (1+ pre-B))
8379 (skip-chars-forward " \t\n")
8380 (setq B (buffer-substring (point) end-B-code))
8381 (goto-char end-B-code)
8382 (or (looking-at ";?[ \t\n]*}")
8383 (progn
8384 (skip-chars-forward "; \t\n")
8385 (setq B-comment
8386 (buffer-substring (point) (1- post-B)))))
8387 (and (equal B "")
8388 (setq B "1"))
8389 (goto-char (1- post-A))
8390 (cperl-backward-to-noncomment pre-A)
8391 (or (looking-at "[ \t\n]*)")
8392 (goto-char (1- post-A)))
8393 (setq p (point))
8394 (goto-char (1+ pre-A))
8395 (skip-chars-forward " \t\n")
8396 (setq A (buffer-substring (point) p))
8397 (delete-region pre-B post-B)
8398 (delete-region pre-A post-A)
8399 (goto-char pre-if)
8400 (insert B " ")
8401 (and B-comment (insert B-comment " "))
8402 (just-one-space)
8403 (forward-word 1)
8404 (setq pre-A (point))
8405 (insert " " A ";")
8406 (delete-horizontal-space)
8407 (setq post-B (point))
8408 (if (looking-at "#")
8409 (indent-for-comment))
8410 (goto-char post-B)
8411 (forward-char -1)
8412 (delete-horizontal-space)
8413 (goto-char pre-A)
8414 (just-one-space)
8415 (goto-char pre-if)
8416 (setq pre-A (set-marker (make-marker) pre-A))
8417 (while (<= (point) (marker-position pre-A))
8418 (cperl-indent-line)
8419 (forward-line 1))
8420 (goto-char (marker-position pre-A))
8421 (if B-comment
8422 (progn
8423 (forward-line -1)
8424 (indent-for-comment)
8425 (goto-char (marker-position pre-A)))))
8426 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8427 ;; (error "`%s' not with an (EXPR)" if-string)
8428 (forward-sexp -1)
8429 (cperl-invert-if-unless-modifiers)))
8430 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8431 (cperl-invert-if-unless-modifiers)))
8432
8433 ;;; By Anthony Foiani <afoiani@uswest.com>
8434 ;;; Getting help on modules in C-h f ?
8435 ;;; This is a modified version of `man'.
8436 ;;; Need to teach it how to lookup functions
8437 ;;;###autoload
8438 (defun cperl-perldoc (word)
8439 "Run `perldoc' on WORD."
8440 (interactive
8441 (list (let* ((default-entry (cperl-word-at-point))
8442 (input (read-string
8443 (format "perldoc entry%s: "
8444 (if (string= default-entry "")
8445 ""
8446 (format " (default %s)" default-entry))))))
8447 (if (string= input "")
8448 (if (string= default-entry "")
8449 (error "No perldoc args given")
8450 default-entry)
8451 input))))
8452 (require 'man)
8453 (let* ((case-fold-search nil)
8454 (is-func (and
8455 (string-match "^[a-z]+$" word)
8456 (string-match (concat "^" word "\\>")
8457 (documentation-property
8458 'cperl-short-docs
8459 'variable-documentation))))
8460 (manual-program (if is-func "perldoc -f" "perldoc")))
8461 (cond
8462 ((featurep 'xemacs)
8463 (let ((Manual-program "perldoc")
8464 (Manual-switches (if is-func (list "-f"))))
8465 (manual-entry word)))
8466 (t
8467 (Man-getpage-in-background word)))))
8468
8469 ;;;###autoload
8470 (defun cperl-perldoc-at-point ()
8471 "Run a `perldoc' on the word around point."
8472 (interactive)
8473 (cperl-perldoc (cperl-word-at-point)))
8474
8475 (defcustom pod2man-program "pod2man"
8476 "*File name for `pod2man'."
8477 :type 'file
8478 :group 'cperl)
8479
8480 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8481 (defun cperl-pod-to-manpage ()
8482 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8483 (interactive)
8484 (require 'man)
8485 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8486 (bufname (concat "Man " buffer-file-name))
8487 (buffer (generate-new-buffer bufname)))
8488 (save-excursion
8489 (set-buffer buffer)
8490 (let ((process-environment (copy-sequence process-environment)))
8491 ;; Prevent any attempt to use display terminal fanciness.
8492 (setenv "TERM" "dumb")
8493 (set-process-sentinel
8494 (start-process pod2man-program buffer "sh" "-c"
8495 (format (cperl-pod2man-build-command) pod2man-args))
8496 'Man-bgproc-sentinel)))))
8497
8498 ;;; Updated version by him too
8499 (defun cperl-build-manpage ()
8500 "Create a virtual manpage in Emacs from the POD in the file."
8501 (interactive)
8502 (require 'man)
8503 (cond
8504 ((featurep 'xemacs)
8505 (let ((Manual-program "perldoc"))
8506 (manual-entry buffer-file-name)))
8507 (t
8508 (let* ((manual-program "perldoc"))
8509 (Man-getpage-in-background buffer-file-name)))))
8510
8511 (defun cperl-pod2man-build-command ()
8512 "Builds the entire background manpage and cleaning command."
8513 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8514 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8515 (while (and flist (car flist))
8516 (let ((pcom (car (car flist)))
8517 (pargs (cdr (car flist))))
8518 (setq command
8519 (concat command " | " pcom " "
8520 (mapconcat '(lambda (phrase)
8521 (if (not (stringp phrase))
8522 (error "Malformed Man-filter-list"))
8523 phrase)
8524 pargs " ")))
8525 (setq flist (cdr flist))))
8526 command))
8527
8528
8529 (defun cperl-next-interpolated-REx-1 ()
8530 "Move point to next REx which has interpolated parts without //o.
8531 Skips RExes consisting of one interpolated variable.
8532
8533 Note that skipped RExen are not performance hits."
8534 (interactive "")
8535 (cperl-next-interpolated-REx 1))
8536
8537 (defun cperl-next-interpolated-REx-0 ()
8538 "Move point to next REx which has interpolated parts without //o."
8539 (interactive "")
8540 (cperl-next-interpolated-REx 0))
8541
8542 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8543 "Move point to next REx which has interpolated parts.
8544 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8545 point and the limit of search (default to point and end of buffer).
8546
8547 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8548 semantic may be used as a numeric argument.
8549
8550 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8551 a result of qr//, this is not a performance hit), t for the rest."
8552 (interactive "P")
8553 (if (numberp skip) (setq skip (list 0 skip)))
8554 (or beg (setq beg (point)))
8555 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8556 (let (pp)
8557 (and (eq (get-text-property beg 'syntax-type) 'string)
8558 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8559 (cperl-map-pods-heres
8560 (function (lambda (s e p)
8561 (if (memq (get-text-property s 'REx-interpolated) skip)
8562 t
8563 (setq pp s)
8564 nil))) ; nil stops
8565 'REx-interpolated beg limit)
8566 (if pp (goto-char pp)
8567 (message "No more interpolated REx"))))
8568
8569 ;;; Initial version contributed by Trey Belew
8570 (defun cperl-here-doc-spell (&optional beg end)
8571 "Spell-check HERE-documents in the Perl buffer.
8572 If a region is highlighted, restricts to the region."
8573 (interactive "")
8574 (cperl-pod-spell t beg end))
8575
8576 (defun cperl-pod-spell (&optional do-heres beg end)
8577 "Spell-check POD documentation.
8578 If invoked with prefix argument, will do HERE-DOCs instead.
8579 If a region is highlighted, restricts to the region."
8580 (interactive "P")
8581 (save-excursion
8582 (let (beg end)
8583 (if (cperl-mark-active)
8584 (setq beg (min (mark) (point))
8585 end (max (mark) (point)))
8586 (setq beg (point-min)
8587 end (point-max)))
8588 (cperl-map-pods-heres (function
8589 (lambda (s e p)
8590 (if do-heres
8591 (setq e (save-excursion
8592 (goto-char e)
8593 (forward-line -1)
8594 (point))))
8595 (ispell-region s e)
8596 t))
8597 (if do-heres 'here-doc-group 'in-pod)
8598 beg end))))
8599
8600 (defun cperl-map-pods-heres (func &optional prop s end)
8601 "Executes a function over regions of pods or here-documents.
8602 PROP is the text-property to search for; default to `in-pod'. Stop when
8603 function returns nil."
8604 (let (pos posend has-prop (cont t))
8605 (or prop (setq prop 'in-pod))
8606 (or s (setq s (point-min)))
8607 (or end (setq end (point-max)))
8608 (cperl-update-syntaxification end end)
8609 (save-excursion
8610 (goto-char (setq pos s))
8611 (while (and cont (< pos end))
8612 (setq has-prop (get-text-property pos prop))
8613 (setq posend (next-single-property-change pos prop nil end))
8614 (and has-prop
8615 (setq cont (funcall func pos posend prop)))
8616 (setq pos posend)))))
8617
8618 ;;; Based on code by Masatake YAMATO:
8619 (defun cperl-get-here-doc-region (&optional pos pod)
8620 "Return HERE document region around the point.
8621 Return nil if the point is not in a HERE document region. If POD is non-nil,
8622 will return a POD section if point is in a POD section."
8623 (or pos (setq pos (point)))
8624 (cperl-update-syntaxification pos pos)
8625 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8626 (and pod
8627 (eq 'pod (get-text-property pos 'syntax-type))))
8628 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8629 (e (next-single-property-change pos 'syntax-type)))
8630 (cons b (or e (point-max))))))
8631
8632 (defun cperl-narrow-to-here-doc (&optional pos)
8633 "Narrows editing region to the HERE-DOC at POS.
8634 POS defaults to the point."
8635 (interactive "d")
8636 (or pos (setq pos (point)))
8637 (let ((p (cperl-get-here-doc-region pos)))
8638 (or p (error "Not inside a HERE document"))
8639 (narrow-to-region (car p) (cdr p))
8640 (message
8641 "When you are finished with narrow editing, type C-x n w")))
8642
8643 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8644 "Select the HERE-DOC (or POD section) at POS.
8645 POS defaults to the point."
8646 (interactive "d")
8647 (let ((p (cperl-get-here-doc-region pos t)))
8648 (if p
8649 (progn
8650 (goto-char (car p))
8651 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8652 (message "I do not think POS is in POD or a HERE-doc..."))))
8653
8654 (defun cperl-facemenu-add-face-function (face end)
8655 "A callback to process user-initiated font-change requests.
8656 Translates `bold', `italic', and `bold-italic' requests to insertion of
8657 corresponding POD directives, and `underline' to C<> POD directive.
8658
8659 Such requests are usually bound to M-o LETTER."
8660 (or (get-text-property (point) 'in-pod)
8661 (error "Faces can only be set within POD"))
8662 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8663 (cdr (or (assq face '((bold . "B<")
8664 (italic . "I<")
8665 (bold-italic . "B<I<")
8666 (underline . "C<")))
8667 (error "Face %s not configured for cperl-mode"
8668 face))))
8669 \f
8670 (defun cperl-time-fontification (&optional l step lim)
8671 "Times how long it takes to do incremental fontification in a region.
8672 L is the line to start at, STEP is the number of lines to skip when
8673 doing next incremental fontification, LIM is the maximal number of
8674 incremental fontification to perform. Messages are accumulated in
8675 *Messages* buffer.
8676
8677 May be used for pinpointing which construct slows down buffer fontification:
8678 start with default arguments, then refine the slowdown regions."
8679 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8680 (or l (setq l 1))
8681 (or step (setq step 500))
8682 (or lim (setq lim 40))
8683 (let* ((timems (function (lambda ()
8684 (let ((tt (current-time)))
8685 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8686 (tt (funcall timems)) (c 0) delta tot)
8687 (goto-line l)
8688 (cperl-mode)
8689 (setq tot (- (- tt (setq tt (funcall timems)))))
8690 (message "cperl-mode at %s: %s" l tot)
8691 (while (and (< c lim) (not (eobp)))
8692 (forward-line step)
8693 (setq l (+ l step))
8694 (setq c (1+ c))
8695 (cperl-update-syntaxification (point) (point))
8696 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8697 (message "to %s:%6s,%7s" l delta tot))
8698 tot))
8699
8700 (defvar font-lock-cache-position)
8701
8702 (defun cperl-emulate-lazy-lock (&optional window-size)
8703 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8704 Start fontifying the buffer from the start (or end) using the given
8705 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8706 goes backwards; default is -50. This function is not CPerl-specific; it
8707 may be used to debug problems with delayed incremental fontification."
8708 (interactive
8709 "nSize of window for incremental fontification, negative goes backwards: ")
8710 (or window-size (setq window-size -50))
8711 (let ((pos (if (> window-size 0)
8712 (point-min)
8713 (point-max)))
8714 p)
8715 (goto-char pos)
8716 (normal-mode)
8717 ;; Why needed??? With older font-locks???
8718 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8719 (while (if (> window-size 0)
8720 (< pos (point-max))
8721 (> pos (point-min)))
8722 (setq p (progn
8723 (forward-line window-size)
8724 (point)))
8725 (font-lock-fontify-region (min p pos) (max p pos))
8726 (setq pos p))))
8727
8728 \f
8729 (defun cperl-lazy-install ()) ; Avoid a warning
8730 (defun cperl-lazy-unstall ()) ; Avoid a warning
8731
8732 (if (fboundp 'run-with-idle-timer)
8733 (progn
8734 (defvar cperl-help-shown nil
8735 "Non-nil means that the help was already shown now.")
8736
8737 (defvar cperl-lazy-installed nil
8738 "Non-nil means that the lazy-help handlers are installed now.")
8739
8740 (defun cperl-lazy-install ()
8741 "Switches on Auto-Help on Perl constructs (put in the message area).
8742 Delay of auto-help controlled by `cperl-lazy-help-time'."
8743 (interactive)
8744 (make-local-variable 'cperl-help-shown)
8745 (if (and (cperl-val 'cperl-lazy-help-time)
8746 (not cperl-lazy-installed))
8747 (progn
8748 (add-hook 'post-command-hook 'cperl-lazy-hook)
8749 (run-with-idle-timer
8750 (cperl-val 'cperl-lazy-help-time 1000000 5)
8751 t
8752 'cperl-get-help-defer)
8753 (setq cperl-lazy-installed t))))
8754
8755 (defun cperl-lazy-unstall ()
8756 "Switches off Auto-Help on Perl constructs (put in the message area).
8757 Delay of auto-help controlled by `cperl-lazy-help-time'."
8758 (interactive)
8759 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8760 (cancel-function-timers 'cperl-get-help-defer)
8761 (setq cperl-lazy-installed nil))
8762
8763 (defun cperl-lazy-hook ()
8764 (setq cperl-help-shown nil))
8765
8766 (defun cperl-get-help-defer ()
8767 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8768 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8769 (cperl-get-help)
8770 (setq cperl-help-shown t))))
8771 (cperl-lazy-install)))
8772
8773
8774 ;;; Plug for wrong font-lock:
8775
8776 (defun cperl-font-lock-unfontify-region-function (beg end)
8777 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8778 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8779 before-change-functions after-change-functions
8780 deactivate-mark buffer-file-name buffer-file-truename)
8781 (remove-text-properties beg end '(face nil))
8782 (if (and (not modified) (buffer-modified-p))
8783 (set-buffer-modified-p nil))))
8784
8785 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8786 "Extends the region to safe positions, then calls the default function.
8787 Newer `font-lock's can do it themselves.
8788 We unwind only as far as needed for fontification. Syntaxification may
8789 do extra unwind via `cperl-unwind-to-safe'."
8790 (save-excursion
8791 (goto-char beg)
8792 (while (and beg
8793 (progn
8794 (beginning-of-line)
8795 (eq (get-text-property (setq beg (point)) 'syntax-type)
8796 'multiline)))
8797 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8798 (goto-char beg)))
8799 (setq beg (point))
8800 (goto-char end)
8801 (while (and end
8802 (progn
8803 (or (bolp) (condition-case nil
8804 (forward-line 1)
8805 (error nil)))
8806 (eq (get-text-property (setq end (point)) 'syntax-type)
8807 'multiline)))
8808 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8809 (goto-char end))
8810 (setq end (point)))
8811 (font-lock-default-fontify-region beg end loudly))
8812
8813 (defvar cperl-d-l nil)
8814 (defun cperl-fontify-syntaxically (end)
8815 ;; Some vars for debugging only
8816 ;; (message "Syntaxifying...")
8817 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8818 (istate (car cperl-syntax-state))
8819 start from-start edebug-backtrace-buffer)
8820 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8821 (progn
8822 (require 'edebug)
8823 (let ((f 'edebug-backtrace))
8824 (funcall f)))) ; Avoid compile-time warning
8825 (or cperl-syntax-done-to
8826 (setq cperl-syntax-done-to (point-min)
8827 from-start t))
8828 (setq start (if (and cperl-hook-after-change
8829 (not from-start))
8830 cperl-syntax-done-to ; Fontify without change; ignore start
8831 ;; Need to forget what is after `start'
8832 (min cperl-syntax-done-to (point))))
8833 (goto-char start)
8834 (beginning-of-line)
8835 (setq start (point))
8836 (and cperl-syntaxify-unwind
8837 (setq end (cperl-unwind-to-safe t end)
8838 start (point)))
8839 (and (> end start)
8840 (setq cperl-syntax-done-to start) ; In case what follows fails
8841 (cperl-find-pods-heres start end t nil t))
8842 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8843 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8844 dbg iend start end idone cperl-syntax-done-to
8845 istate (car cperl-syntax-state))) ; For debugging
8846 nil)) ; Do not iterate
8847
8848 (defun cperl-fontify-update (end)
8849 (let ((pos (point-min)) prop posend)
8850 (setq end (point-max))
8851 (while (< pos end)
8852 (setq prop (get-text-property pos 'cperl-postpone)
8853 posend (next-single-property-change pos 'cperl-postpone nil end))
8854 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8855 (setq pos posend)))
8856 nil) ; Do not iterate
8857
8858 (defun cperl-fontify-update-bad (end)
8859 ;; Since fontification happens with different region than syntaxification,
8860 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8861 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8862 (if prop
8863 (setq pos (or (cperl-beginning-of-property
8864 (cperl-1+ pos) 'cperl-postpone)
8865 (point-min))))
8866 (while (< pos end)
8867 (setq posend (next-single-property-change pos 'cperl-postpone))
8868 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8869 (setq pos posend)
8870 (setq prop (get-text-property pos 'cperl-postpone))))
8871 nil) ; Do not iterate
8872
8873 ;; Called when any modification is made to buffer text.
8874 (defun cperl-after-change-function (beg end old-len)
8875 ;; We should have been informed about changes by `font-lock'. Since it
8876 ;; does not inform as which calls are defered, do it ourselves
8877 (if cperl-syntax-done-to
8878 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8879
8880 (defun cperl-update-syntaxification (from to)
8881 (if (and cperl-use-syntax-table-text-property
8882 cperl-syntaxify-by-font-lock
8883 (or (null cperl-syntax-done-to)
8884 (< cperl-syntax-done-to to)))
8885 (progn
8886 (save-excursion
8887 (goto-char from)
8888 (cperl-fontify-syntaxically to)))))
8889
8890 (defvar cperl-version
8891 (let ((v "Revision: 5.23"))
8892 (string-match ":\\s *\\([0-9.]+\\)" v)
8893 (substring v (match-beginning 1) (match-end 1)))
8894 "Version of IZ-supported CPerl package this file is based on.")
8895
8896 (provide 'cperl-mode)
8897
8898 ;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
8899 ;;; cperl-mode.el ends here