* lisp/progmodes/cperl-mode.el: Use cl-lib. Fix comment convention
[emacs.git] / lisp / progmodes / cperl-mode.el
blobc4f1ff2ec76b919bee5377309b52ab6ebab1a25f
1 ;;; cperl-mode.el --- Perl code editing commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1987, 1991-2017 Free Software Foundation, Inc.
5 ;; Author: Ilya Zakharevich
6 ;; Bob Olson
7 ;; Jonathan Rockway <jon@jrock.us>
8 ;; Maintainer: emacs-devel@gnu.org
9 ;; Keywords: languages, Perl
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
28 ;;; Commentary:
30 ;; This version of the file contains support for the syntax added by
31 ;; the MooseX::Declare CPAN module, as well as Perl 5.10 keyword
32 ;; support.
34 ;; The latest version is available from
35 ;; http://github.com/jrockway/cperl-mode
37 ;; (perhaps in the moosex-declare branch)
39 ;; You can either fine-tune the bells and whistles of this mode or
40 ;; bulk enable them by putting
42 ;; (setq cperl-hairy t)
44 ;; in your .emacs file. (Emacs rulers do not consider it politically
45 ;; correct to make whistles enabled by default.)
47 ;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<<
48 ;; or as help on variables `cperl-tips', `cperl-problems', <<<<<<
49 ;; `cperl-praise', `cperl-speed'. <<<<<<
51 ;; The mode information (on C-h m) provides some customization help.
52 ;; If you use font-lock feature of this mode, it is advisable to use
53 ;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock.
55 ;; Faces used now: three faces for first-class and second-class keywords
56 ;; and control flow words, one for each: comments, string, labels,
57 ;; functions definitions and packages, arrays, hashes, and variable
58 ;; definitions. If you do not see all these faces, your font-lock does
59 ;; not define them, so you need to define them manually.
61 ;; This mode supports font-lock, imenu and mode-compile. In the
62 ;; hairy version font-lock is on, but you should activate imenu
63 ;; yourself (note that mode-compile is not standard yet). Well, you
64 ;; can use imenu from keyboard anyway (M-x imenu), but it is better
65 ;; to bind it like that:
67 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
69 ;;;; Font lock bugs as of v4.32:
71 ;; The following kinds of Perl code erroneously start strings:
72 ;; \$` \$' \$"
73 ;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
74 ;; likewise with m, tr, y, q, qX instead of s
76 ;;; Code:
78 (eval-when-compile (require 'cl-lib))
80 (defvar vc-rcs-header)
81 (defvar vc-sccs-header)
83 (eval-when-compile
84 (condition-case nil
85 (require 'custom)
86 (error nil))
87 (condition-case nil
88 (require 'man)
89 (error nil))
90 (defvar msb-menu-cond)
91 (defvar gud-perldb-history)
92 (defvar font-lock-background-mode) ; not in Emacs
93 (defvar font-lock-display-type) ; ditto
94 (defvar paren-backwards-message) ; Not in newer XEmacs?
95 (defmacro cperl-is-face (arg) ; Takes quoted arg
96 (cond ((fboundp 'find-face)
97 `(find-face ,arg))
98 (;;(and (fboundp 'face-list)
99 ;; (face-list))
100 (fboundp 'face-list)
101 `(member ,arg (and (fboundp 'face-list)
102 (face-list))))
104 `(boundp ,arg))))
105 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
106 (cond ((fboundp 'make-face)
107 `(make-face (quote ,arg)))
109 `(defvar ,arg (quote ,arg) ,descr))))
110 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
111 `(progn
112 (or (cperl-is-face (quote ,arg))
113 (cperl-make-face ,arg ,descr))
114 (or (boundp (quote ,arg)) ; We use unquoted variants too
115 (defvar ,arg (quote ,arg) ,descr))))
116 (if (featurep 'xemacs)
117 (defmacro cperl-etags-snarf-tag (file line)
118 `(progn
119 (beginning-of-line 2)
120 (list ,file ,line)))
121 (defmacro cperl-etags-snarf-tag (_file _line)
122 `(etags-snarf-tag)))
123 (if (featurep 'xemacs)
124 (defmacro cperl-etags-goto-tag-location (elt)
125 ;;(progn
126 ;; (switch-to-buffer (get-file-buffer (elt ,elt 0)))
127 ;; (set-buffer (get-file-buffer (elt ,elt 0)))
128 ;; Probably will not work due to some save-excursion???
129 ;; Or save-file-position?
130 ;; (message "Did I get to line %s?" (elt ,elt 1))
131 `(goto-line (string-to-int (elt ,elt 1))))
133 (defmacro cperl-etags-goto-tag-location (elt)
134 `(etags-goto-tag-location ,elt))))
136 (defun cperl-choose-color (&rest list)
137 (let (answer)
138 (while list
139 (or answer
140 (if (or (x-color-defined-p (car list))
141 (null (cdr list)))
142 (setq answer (car list))))
143 (setq list (cdr list)))
144 answer))
146 (defgroup cperl nil
147 "Major mode for editing Perl code."
148 :prefix "cperl-"
149 :group 'languages
150 :version "20.3")
152 (defgroup cperl-indentation-details nil
153 "Indentation."
154 :prefix "cperl-"
155 :group 'cperl)
157 (defgroup cperl-affected-by-hairy nil
158 "Variables affected by `cperl-hairy'."
159 :prefix "cperl-"
160 :group 'cperl)
162 (defgroup cperl-autoinsert-details nil
163 "Auto-insert tuneup."
164 :prefix "cperl-"
165 :group 'cperl)
167 (defgroup cperl-faces nil
168 "Fontification colors."
169 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
170 :prefix "cperl-"
171 :group 'cperl)
173 (defgroup cperl-speed nil
174 "Speed vs. validity tuneup."
175 :prefix "cperl-"
176 :group 'cperl)
178 (defgroup cperl-help-system nil
179 "Help system tuneup."
180 :prefix "cperl-"
181 :group 'cperl)
184 (defcustom cperl-extra-newline-before-brace nil
185 "Non-nil means that if, elsif, while, until, else, for, foreach
186 and do constructs look like:
188 if ()
192 instead of:
194 if () {
196 :type 'boolean
197 :group 'cperl-autoinsert-details)
199 (defcustom cperl-extra-newline-before-brace-multiline
200 cperl-extra-newline-before-brace
201 "Non-nil means the same as `cperl-extra-newline-before-brace', but
202 for constructs with multiline if/unless/while/until/for/foreach condition."
203 :type 'boolean
204 :group 'cperl-autoinsert-details)
206 (defcustom cperl-indent-level 2
207 "Indentation of CPerl statements with respect to containing block."
208 :type 'integer
209 :group 'cperl-indentation-details)
211 ;; It is not unusual to put both things like perl-indent-level and
212 ;; cperl-indent-level in the local variable section of a file. If only
213 ;; one of perl-mode and cperl-mode is in use, a warning will be issued
214 ;; about the variable. Autoload these here, so that no warning is
215 ;; issued when using either perl-mode or cperl-mode.
216 ;;;###autoload(put 'cperl-indent-level 'safe-local-variable 'integerp)
217 ;;;###autoload(put 'cperl-brace-offset 'safe-local-variable 'integerp)
218 ;;;###autoload(put 'cperl-continued-brace-offset 'safe-local-variable 'integerp)
219 ;;;###autoload(put 'cperl-label-offset 'safe-local-variable 'integerp)
220 ;;;###autoload(put 'cperl-continued-statement-offset 'safe-local-variable 'integerp)
221 ;;;###autoload(put 'cperl-extra-newline-before-brace 'safe-local-variable 'booleanp)
222 ;;;###autoload(put 'cperl-merge-trailing-else 'safe-local-variable 'booleanp)
224 (defcustom cperl-lineup-step nil
225 "`cperl-lineup' will always lineup at multiple of this number.
226 If nil, the value of `cperl-indent-level' will be used."
227 :type '(choice (const nil) integer)
228 :group 'cperl-indentation-details)
230 (defcustom cperl-brace-imaginary-offset 0
231 "Imagined indentation of a Perl open brace that actually follows a statement.
232 An open brace following other text is treated as if it were this far
233 to the right of the start of its line."
234 :type 'integer
235 :group 'cperl-indentation-details)
237 (defcustom cperl-brace-offset 0
238 "Extra indentation for braces, compared with other text in same context."
239 :type 'integer
240 :group 'cperl-indentation-details)
241 (defcustom cperl-label-offset -2
242 "Offset of CPerl label lines relative to usual indentation."
243 :type 'integer
244 :group 'cperl-indentation-details)
245 (defcustom cperl-min-label-indent 1
246 "Minimal offset of CPerl label lines."
247 :type 'integer
248 :group 'cperl-indentation-details)
249 (defcustom cperl-continued-statement-offset 2
250 "Extra indent for lines not starting new statements."
251 :type 'integer
252 :group 'cperl-indentation-details)
253 (defcustom cperl-continued-brace-offset 0
254 "Extra indent for substatements that start with open-braces.
255 This is in addition to cperl-continued-statement-offset."
256 :type 'integer
257 :group 'cperl-indentation-details)
258 (defcustom cperl-close-paren-offset -1
259 "Extra indent for substatements that start with close-parenthesis."
260 :type 'integer
261 :group 'cperl-indentation-details)
263 (defcustom cperl-indent-wrt-brace t
264 "Non-nil means indent statements in if/etc block relative brace, not if/etc.
265 Versions 5.2 ... 5.20 behaved as if this were nil."
266 :type 'boolean
267 :group 'cperl-indentation-details)
269 (defcustom cperl-indent-subs-specially t
270 "*Non-nil means indent subs that are inside other blocks (hash values, for example) relative to the beginning of the \"sub\" keyword, rather than relative to the statement that contains the declaration."
271 :type 'boolean
272 :group 'cperl-indentation-details)
274 (defcustom cperl-auto-newline nil
275 "Non-nil means automatically newline before and after braces,
276 and after colons and semicolons, inserted in CPerl code. The following
277 \\[cperl-electric-backspace] will remove the inserted whitespace.
278 Insertion after colons requires both this variable and
279 `cperl-auto-newline-after-colon' set."
280 :type 'boolean
281 :group 'cperl-autoinsert-details)
283 (defcustom cperl-autoindent-on-semi nil
284 "Non-nil means automatically indent after insertion of (semi)colon.
285 Active if `cperl-auto-newline' is false."
286 :type 'boolean
287 :group 'cperl-autoinsert-details)
289 (defcustom cperl-auto-newline-after-colon nil
290 "Non-nil means automatically newline even after colons.
291 Subject to `cperl-auto-newline' setting."
292 :type 'boolean
293 :group 'cperl-autoinsert-details)
295 (defcustom cperl-tab-always-indent t
296 "Non-nil means TAB in CPerl mode should always reindent the current line,
297 regardless of where in the line point is when the TAB command is used."
298 :type 'boolean
299 :group 'cperl-indentation-details)
301 (defcustom cperl-font-lock nil
302 "Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
303 Can be overwritten by `cperl-hairy' if nil."
304 :type '(choice (const null) boolean)
305 :group 'cperl-affected-by-hairy)
307 (defcustom cperl-electric-lbrace-space nil
308 "Non-nil (and non-null) means { after $ should be preceded by ` '.
309 Can be overwritten by `cperl-hairy' if nil."
310 :type '(choice (const null) boolean)
311 :group 'cperl-affected-by-hairy)
313 (defcustom cperl-electric-parens-string "({[]})<"
314 "String of parentheses that should be electric in CPerl.
315 Closing ones are electric only if the region is highlighted."
316 :type 'string
317 :group 'cperl-affected-by-hairy)
319 (defcustom cperl-electric-parens nil
320 "Non-nil (and non-null) means parentheses should be electric in CPerl.
321 Can be overwritten by `cperl-hairy' if nil."
322 :type '(choice (const null) boolean)
323 :group 'cperl-affected-by-hairy)
325 (defvar zmacs-regions) ; Avoid warning
327 (defcustom cperl-electric-parens-mark
328 (and window-system
329 (or (and (boundp 'transient-mark-mode) ; For Emacs
330 transient-mark-mode)
331 (and (boundp 'zmacs-regions) ; For XEmacs
332 zmacs-regions)))
333 "Not-nil means that electric parens look for active mark.
334 Default is yes if there is visual feedback on mark."
335 :type 'boolean
336 :group 'cperl-autoinsert-details)
338 (defcustom cperl-electric-linefeed nil
339 "If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
340 In any case these two mean plain and hairy linefeeds together.
341 Can be overwritten by `cperl-hairy' if nil."
342 :type '(choice (const null) boolean)
343 :group 'cperl-affected-by-hairy)
345 (defcustom cperl-electric-keywords nil
346 "Not-nil (and non-null) means keywords are electric in CPerl.
347 Can be overwritten by `cperl-hairy' if nil.
349 Uses `abbrev-mode' to do the expansion. If you want to use your
350 own abbrevs in cperl-mode, but do not want keywords to be
351 electric, you must redefine `cperl-mode-abbrev-table': do
352 \\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
353 that paragraph, delete the words that appear at the ends of lines and
354 that begin with \"cperl-electric\".
356 :type '(choice (const null) boolean)
357 :group 'cperl-affected-by-hairy)
359 (defcustom cperl-electric-backspace-untabify t
360 "Not-nil means electric-backspace will untabify in CPerl."
361 :type 'boolean
362 :group 'cperl-autoinsert-details)
364 (defcustom cperl-hairy nil
365 "Not-nil means most of the bells and whistles are enabled in CPerl.
366 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
367 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
368 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
369 `cperl-lazy-help-time'."
370 :type 'boolean
371 :group 'cperl-affected-by-hairy)
373 (defcustom cperl-comment-column 32
374 "Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
375 :type 'integer
376 :group 'cperl-indentation-details)
378 (defcustom cperl-indent-comment-at-column-0 nil
379 "Non-nil means that comment started at column 0 should be indentable."
380 :type 'boolean
381 :group 'cperl-indentation-details)
383 (defcustom cperl-vc-sccs-header '("($sccs) = ('%W\ %' =~ /(\\d+(\\.\\d+)+)/) ;")
384 "Special version of `vc-sccs-header' that is used in CPerl mode buffers."
385 :type '(repeat string)
386 :group 'cperl)
388 (defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\ $ ' =~ /(\\d+(\\.\\d+)+)/);")
389 "Special version of `vc-rcs-header' that is used in CPerl mode buffers."
390 :type '(repeat string)
391 :group 'cperl)
393 ;; This became obsolete...
394 (defvar cperl-vc-header-alist nil)
395 (make-obsolete-variable
396 'cperl-vc-header-alist
397 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead."
398 "22.1")
400 ;; (defcustom cperl-clobber-mode-lists
401 ;; (not
402 ;; (and
403 ;; (boundp 'interpreter-mode-alist)
404 ;; (assoc "miniperl" interpreter-mode-alist)
405 ;; (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
406 ;; "Whether to install us into `interpreter-' and `extension' mode lists."
407 ;; :type 'boolean
408 ;; :group 'cperl)
410 (defcustom cperl-info-on-command-no-prompt nil
411 "Not-nil (and non-null) means not to prompt on C-h f.
412 The opposite behavior is always available if prefixed with C-c.
413 Can be overwritten by `cperl-hairy' if nil."
414 :type '(choice (const null) boolean)
415 :group 'cperl-affected-by-hairy)
417 (defcustom cperl-clobber-lisp-bindings nil
418 "Not-nil (and non-null) means not overwrite C-h f.
419 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
420 Can be overwritten by `cperl-hairy' if nil."
421 :type '(choice (const null) boolean)
422 :group 'cperl-affected-by-hairy)
424 (defcustom cperl-lazy-help-time nil
425 "Not-nil (and non-null) means to show lazy help after given idle time.
426 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
427 :type '(choice (const null) (const nil) integer)
428 :group 'cperl-affected-by-hairy)
430 (defcustom cperl-pod-face 'font-lock-comment-face
431 "Face for POD highlighting."
432 :type 'face
433 :group 'cperl-faces)
435 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
436 "Face for POD highlighting.
437 Font for POD headers."
438 :type 'face
439 :group 'cperl-faces)
441 (defcustom cperl-here-face 'font-lock-string-face
442 "Face for here-docs highlighting."
443 :type 'face
444 :group 'cperl-faces)
446 ;; Some double-evaluation happened with font-locks... Needed with 21.2...
447 (defvar cperl-singly-quote-face (featurep 'xemacs))
449 (defcustom cperl-invalid-face 'underline
450 "Face for highlighting trailing whitespace."
451 :type 'face
452 :version "21.1"
453 :group 'cperl-faces)
455 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
456 "Not-nil after evaluation means to highlight POD and here-docs sections."
457 :type 'boolean
458 :group 'cperl-faces)
460 (defcustom cperl-fontify-m-as-s t
461 "Not-nil means highlight 1arg regular expressions operators same as 2arg."
462 :type 'boolean
463 :group 'cperl-faces)
465 (defcustom cperl-highlight-variables-indiscriminately nil
466 "Non-nil means perform additional highlighting on variables.
467 Currently only changes how scalar variables are highlighted.
468 Note that the variable is only read at initialization time for
469 the variable `cperl-font-lock-keywords-2', so changing it after you've
470 entered CPerl mode the first time will have no effect."
471 :type 'boolean
472 :group 'cperl)
474 (defcustom cperl-pod-here-scan t
475 "Not-nil means look for POD and here-docs sections during startup.
476 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
477 :type 'boolean
478 :group 'cperl-speed)
480 (defcustom cperl-regexp-scan t
481 "Not-nil means make marking of regular expression more thorough.
482 Effective only with `cperl-pod-here-scan'."
483 :type 'boolean
484 :group 'cperl-speed)
486 (defcustom cperl-hook-after-change t
487 "Not-nil means install hook to know which regions of buffer are changed.
488 May significantly speed up delayed fontification. Changes take effect
489 after reload."
490 :type 'boolean
491 :group 'cperl-speed)
493 (defcustom cperl-imenu-addback nil
494 "Not-nil means add backreferences to generated `imenu's.
495 May require patched `imenu' and `imenu-go'. Obsolete."
496 :type 'boolean
497 :group 'cperl-help-system)
499 (defcustom cperl-max-help-size 66
500 "Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
501 :type '(choice integer (const nil))
502 :group 'cperl-help-system)
504 (defcustom cperl-shrink-wrap-info-frame t
505 "Non-nil means shrink-wrapping of info-buffer-frame allowed."
506 :type 'boolean
507 :group 'cperl-help-system)
509 (defcustom cperl-info-page "perl"
510 "Name of the info page containing perl docs.
511 Older version of this page was called `perl5', newer `perl'."
512 :type 'string
513 :group 'cperl-help-system)
515 (defcustom cperl-use-syntax-table-text-property
516 (boundp 'parse-sexp-lookup-properties)
517 "Non-nil means CPerl sets up and uses `syntax-table' text property."
518 :type 'boolean
519 :group 'cperl-speed)
521 (defcustom cperl-use-syntax-table-text-property-for-tags
522 cperl-use-syntax-table-text-property
523 "Non-nil means: set up and use `syntax-table' text property generating TAGS."
524 :type 'boolean
525 :group 'cperl-speed)
527 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
528 "Regexp to match files to scan when generating TAGS."
529 :type 'regexp
530 :group 'cperl)
532 (defcustom cperl-noscan-files-regexp
533 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
534 "Regexp to match files/dirs to skip when generating TAGS."
535 :type 'regexp
536 :group 'cperl)
538 (defcustom cperl-regexp-indent-step nil
539 "Indentation used when beautifying regexps.
540 If nil, the value of `cperl-indent-level' will be used."
541 :type '(choice integer (const nil))
542 :group 'cperl-indentation-details)
544 (defcustom cperl-indent-left-aligned-comments t
545 "Non-nil means that the comment starting in leftmost column should indent."
546 :type 'boolean
547 :group 'cperl-indentation-details)
549 (defcustom cperl-under-as-char nil
550 "Non-nil means that the _ (underline) should be treated as word char."
551 :type 'boolean
552 :group 'cperl)
553 (make-obsolete-variable 'cperl-under-as-char 'superword-mode "24.4")
555 (defcustom cperl-extra-perl-args ""
556 "Extra arguments to use when starting Perl.
557 Currently used with `cperl-check-syntax' only."
558 :type 'string
559 :group 'cperl)
561 (defcustom cperl-message-electric-keyword t
562 "Non-nil means that the `cperl-electric-keyword' prints a help message."
563 :type 'boolean
564 :group 'cperl-help-system)
566 (defcustom cperl-indent-region-fix-constructs 1
567 "Amount of space to insert between `}' and `else' or `elsif'
568 in `cperl-indent-region'. Set to nil to leave as is. Values other
569 than 1 and nil will probably not work."
570 :type '(choice (const nil) (const 1))
571 :group 'cperl-indentation-details)
573 (defcustom cperl-break-one-line-blocks-when-indent t
574 "Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
575 need to be reformatted into multiline ones when indenting a region."
576 :type 'boolean
577 :group 'cperl-indentation-details)
579 (defcustom cperl-fix-hanging-brace-when-indent t
580 "Non-nil means that BLOCK-end `}' may be put on a separate line
581 when indenting a region.
582 Braces followed by else/elsif/while/until are excepted."
583 :type 'boolean
584 :group 'cperl-indentation-details)
586 (defcustom cperl-merge-trailing-else t
587 "Non-nil means that BLOCK-end `}' followed by else/elsif/continue
588 may be merged to be on the same line when indenting a region."
589 :type 'boolean
590 :group 'cperl-indentation-details)
592 (defcustom cperl-indent-parens-as-block nil
593 "Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
594 but for trailing \",\" inside the group, which won't increase indentation.
595 One should tune up `cperl-close-paren-offset' as well."
596 :type 'boolean
597 :group 'cperl-indentation-details)
599 (defcustom cperl-syntaxify-by-font-lock
600 (boundp 'parse-sexp-lookup-properties)
601 "Non-nil means that CPerl uses the `font-lock' routines for syntaxification."
602 :type '(choice (const message) boolean)
603 :group 'cperl-speed)
605 (defcustom cperl-syntaxify-unwind
607 "Non-nil means that CPerl unwinds to a start of a long construction
608 when syntaxifying a chunk of buffer."
609 :type 'boolean
610 :group 'cperl-speed)
612 (defcustom cperl-syntaxify-for-menu
614 "Non-nil means that CPerl syntaxifies up to the point before showing menu.
615 This way enabling/disabling of menu items is more correct."
616 :type 'boolean
617 :group 'cperl-speed)
619 (defcustom cperl-ps-print-face-properties
620 '((font-lock-keyword-face nil nil bold shadow)
621 (font-lock-variable-name-face nil nil bold)
622 (font-lock-function-name-face nil nil bold italic box)
623 (font-lock-constant-face nil "LightGray" bold)
624 (cperl-array-face nil "LightGray" bold underline)
625 (cperl-hash-face nil "LightGray" bold italic underline)
626 (font-lock-comment-face nil "LightGray" italic)
627 (font-lock-string-face nil nil italic underline)
628 (cperl-nonoverridable-face nil nil italic underline)
629 (font-lock-type-face nil nil underline)
630 (font-lock-warning-face nil "LightGray" bold italic box)
631 (underline nil "LightGray" strikeout))
632 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
633 :type '(repeat (cons symbol
634 (cons (choice (const nil) string)
635 (cons (choice (const nil) string)
636 (repeat symbol)))))
637 :group 'cperl-faces)
639 (defvar cperl-dark-background
640 (cperl-choose-color "navy" "os2blue" "darkgreen"))
641 (defvar cperl-dark-foreground
642 (cperl-choose-color "orchid1" "orange"))
644 (defface cperl-nonoverridable-face
645 `((((class grayscale) (background light))
646 (:background "Gray90" :slant italic :underline t))
647 (((class grayscale) (background dark))
648 (:foreground "Gray80" :slant italic :underline t :weight bold))
649 (((class color) (background light))
650 (:foreground "chartreuse3"))
651 (((class color) (background dark))
652 (:foreground ,cperl-dark-foreground))
653 (t (:weight bold :underline t)))
654 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
655 :group 'cperl-faces)
657 (defface cperl-array-face
658 `((((class grayscale) (background light))
659 (:background "Gray90" :weight bold))
660 (((class grayscale) (background dark))
661 (:foreground "Gray80" :weight bold))
662 (((class color) (background light))
663 (:foreground "Blue" :background "lightyellow2" :weight bold))
664 (((class color) (background dark))
665 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
666 (t (:weight bold)))
667 "Font Lock mode face used to highlight array names."
668 :group 'cperl-faces)
670 (defface cperl-hash-face
671 `((((class grayscale) (background light))
672 (:background "Gray90" :weight bold :slant italic))
673 (((class grayscale) (background dark))
674 (:foreground "Gray80" :weight bold :slant italic))
675 (((class color) (background light))
676 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
677 (((class color) (background dark))
678 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
679 (t (:weight bold :slant italic)))
680 "Font Lock mode face used to highlight hash names."
681 :group 'cperl-faces)
685 ;;; Short extra-docs.
687 (defvar cperl-tips 'please-ignore-this-line
688 "Note that to enable Compile choices in the menu you need to install
689 mode-compile.el.
691 If your Emacs does not default to `cperl-mode' on Perl files, and you
692 want it to: put the following into your .emacs file:
694 (defalias \\='perl-mode \\='cperl-mode)
696 Get perl5-info from
697 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
698 Also, one can generate a newer documentation running `pod2texi' converter
699 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
701 If you use imenu-go, run imenu on perl5-info buffer (you can do it
702 from Perl menu). If many files are related, generate TAGS files from
703 Tools/Tags submenu in Perl menu.
705 If some class structure is too complicated, use Tools/Hierarchy-view
706 from Perl menu, or hierarchic view of imenu. The second one uses the
707 current buffer only, the first one requires generation of TAGS from
708 Perl/Tools/Tags menu beforehand.
710 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
712 Switch auto-help on/off with Perl/Tools/Auto-help.
714 Though with contemporary Emaxen CPerl mode should maintain the correct
715 parsing of Perl even when editing, sometimes it may be lost. Fix this by
717 \\[normal-mode]
719 In cases of more severe confusion sometimes it is helpful to do
721 \\[load-library] cperl-mode RET
722 \\[normal-mode]
724 Before reporting (non-)problems look in the problem section of online
725 micro-docs on what I know about CPerl problems.")
727 (defvar cperl-problems 'please-ignore-this-line
728 "Description of problems in CPerl mode.
729 Some faces will not be shown on some versions of Emacs unless you
730 install choose-color.el, available from
731 http://ilyaz.org/software/emacs
733 `fill-paragraph' on a comment may leave the point behind the
734 paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
735 to detect it and bulk out).
737 See documentation of a variable `cperl-problems-old-emaxen' for the
738 problems which disappear if you upgrade Emacs to a reasonably new
739 version (20.3 for Emacs, and those of 2004 for XEmacs).")
741 (defvar cperl-problems-old-emaxen 'please-ignore-this-line
742 "Description of problems in CPerl mode specific for older Emacs versions.
744 Emacs had a _very_ restricted syntax parsing engine until version
745 20.1. Most problems below are corrected starting from this version of
746 Emacs, and all of them should be fixed in version 20.3. (Or apply
747 patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
748 this respect (until 2003).
750 Note that even with newer Emacsen in some very rare cases the details
751 of interaction of `font-lock' and syntaxification may be not cleaned
752 up yet. You may get slightly different colors basing on the order of
753 fontification and syntaxification. Say, the initial faces is correct,
754 but editing the buffer breaks this.
756 Even with older Emacsen CPerl mode tries to corrects some Emacs
757 misunderstandings, however, for efficiency reasons the degree of
758 correction is different for different operations. The partially
759 corrected problems are: POD sections, here-documents, regexps. The
760 operations are: highlighting, indentation, electric keywords, electric
761 braces.
763 This may be confusing, since the regexp s#//#/#; may be highlighted
764 as a comment, but it will be recognized as a regexp by the indentation
765 code. Or the opposite case, when a POD section is highlighted, but
766 may break the indentation of the following code (though indentation
767 should work if the balance of delimiters is not broken by POD).
769 The main trick (to make $ a \"backslash\") makes constructions like
770 ${aaa} look like unbalanced braces. The only trick I can think of is
771 to insert it as $ {aaa} (valid in perl5, not in perl4).
773 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
774 as /($|\\s)/. Note that such a transposition is not always possible.
776 The solution is to upgrade your Emacs or patch an older one. Note
777 that Emacs 20.2 has some bugs related to `syntax-table' text
778 properties. Patches are available on the main CPerl download site,
779 and on CPAN.
781 If these bugs cannot be fixed on your machine (say, you have an inferior
782 environment and cannot recompile), you may still disable all the fancy stuff
783 via `cperl-use-syntax-table-text-property'.")
785 (defvar cperl-praise 'please-ignore-this-line
786 "Advantages of CPerl mode.
788 0) It uses the newest `syntax-table' property ;-);
790 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
791 mode - but the latter number may have improved too in last years) even
792 with old Emaxen which do not support `syntax-table' property.
794 When using `syntax-table' property for syntax assist hints, it should
795 handle 99.995% of lines correct - or somesuch. It automatically
796 updates syntax assist hints when you edit your script.
798 2) It is generally believed to be \"the most user-friendly Emacs
799 package\" whatever it may mean (I doubt that the people who say similar
800 things tried _all_ the rest of Emacs ;-), but this was not a lonely
801 voice);
803 3) Everything is customizable, one-by-one or in a big sweep;
805 4) It has many easily-accessible \"tools\":
806 a) Can run program, check syntax, start debugger;
807 b) Can lineup vertically \"middles\" of rows, like `=' in
808 a = b;
809 cc = d;
810 c) Can insert spaces where this improves readability (in one
811 interactive sweep over the buffer);
812 d) Has support for imenu, including:
813 1) Separate unordered list of \"interesting places\";
814 2) Separate TOC of POD sections;
815 3) Separate list of packages;
816 4) Hierarchical view of methods in (sub)packages;
817 5) and functions (by the full name - with package);
818 e) Has an interface to INFO docs for Perl; The interface is
819 very flexible, including shrink-wrapping of
820 documentation buffer/frame;
821 f) Has a builtin list of one-line explanations for perl constructs.
822 g) Can show these explanations if you stay long enough at the
823 corresponding place (or on demand);
824 h) Has an enhanced fontification (using 3 or 4 additional faces
825 comparing to font-lock - basically, different
826 namespaces in Perl have different colors);
827 i) Can construct TAGS basing on its knowledge of Perl syntax,
828 the standard menu has 6 different way to generate
829 TAGS (if \"by directory\", .xs files - with C-language
830 bindings - are included in the scan);
831 j) Can build a hierarchical view of classes (via imenu) basing
832 on generated TAGS file;
833 k) Has electric parentheses, electric newlines, uses Abbrev
834 for electric logical constructs
835 while () {}
836 with different styles of expansion (context sensitive
837 to be not so bothering). Electric parentheses behave
838 \"as they should\" in a presence of a visible region.
839 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
840 m) Can convert from
841 if (A) { B }
843 B if A;
845 n) Highlights (by user-choice) either 3-delimiters constructs
846 (such as tr/a/b/), or regular expressions and `y/tr';
847 o) Highlights trailing whitespace;
848 p) Is able to manipulate Perl Regular Expressions to ease
849 conversion to a more readable form.
850 q) Can ispell POD sections and HERE-DOCs.
851 r) Understands comments and character classes inside regular
852 expressions; can find matching () and [] in a regular expression.
853 s) Allows indentation of //x-style regular expressions;
854 t) Highlights different symbols in regular expressions according
855 to their function; much less problems with backslashitis;
856 u) Allows to find regular expressions which contain interpolated parts.
858 5) The indentation engine was very smart, but most of tricks may be
859 not needed anymore with the support for `syntax-table' property. Has
860 progress indicator for indentation (with `imenu' loaded).
862 6) Indent-region improves inline-comments as well; also corrects
863 whitespace *inside* the conditional/loop constructs.
865 7) Fill-paragraph correctly handles multi-line comments;
867 8) Can switch to different indentation styles by one command, and restore
868 the settings present before the switch.
870 9) When doing indentation of control constructs, may correct
871 line-breaks/spacing between elements of the construct.
873 10) Uses a linear-time algorithm for indentation of regions (on Emaxen with
874 capable syntax engines).
876 11) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
879 (defvar cperl-speed 'please-ignore-this-line
880 "This is an incomplete compendium of what is available in other parts
881 of CPerl documentation. (Please inform me if I skept anything.)
883 There is a perception that CPerl is slower than alternatives. This part
884 of documentation is designed to overcome this misconception.
886 *By default* CPerl tries to enable the most comfortable settings.
887 From most points of view, correctly working package is infinitely more
888 comfortable than a non-correctly working one, thus by default CPerl
889 prefers correctness over speed. Below is the guide how to change
890 settings if your preferences are different.
892 A) Speed of loading the file. When loading file, CPerl may perform a
893 scan which indicates places which cannot be parsed by primitive Emacs
894 syntax-parsing routines, and marks them up so that either
896 A1) CPerl may work around these deficiencies (for big chunks, mostly
897 PODs and HERE-documents), or
898 A2) On capable Emaxen CPerl will use improved syntax-handling
899 which reads mark-up hints directly.
901 The scan in case A2 is much more comprehensive, thus may be slower.
903 User can disable syntax-engine-helping scan of A2 by setting
904 `cperl-use-syntax-table-text-property'
905 variable to nil (if it is set to t).
907 One can disable the scan altogether (both A1 and A2) by setting
908 `cperl-pod-here-scan'
909 to nil.
911 B) Speed of editing operations.
913 One can add a (minor) speedup to editing operations by setting
914 `cperl-use-syntax-table-text-property'
915 variable to nil (if it is set to t). This will disable
916 syntax-engine-helping scan, thus will make many more Perl
917 constructs be wrongly recognized by CPerl, thus may lead to
918 wrongly matched parentheses, wrong indentation, etc.
920 One can unset `cperl-syntaxify-unwind'. This might speed up editing
921 of, say, long POD sections.")
923 (defvar cperl-tips-faces 'please-ignore-this-line
924 "CPerl mode uses following faces for highlighting:
926 `cperl-array-face' Array names
927 `cperl-hash-face' Hash names
928 `font-lock-comment-face' Comments, PODs and whatever is considered
929 syntactically to be not code
930 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
931 2-arg operators s/y/tr/ or of RExen,
932 `font-lock-warning-face' Special-cased m// and s//foo/,
933 `font-lock-function-name-face' _ as a target of a file tests, file tests,
934 subroutine names at the moment of definition
935 (except those conflicting with Perl operators),
936 package names (when recognized), format names
937 `font-lock-keyword-face' Control flow switch constructs, declarators
938 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
939 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
940 literal parts and the terminator of formats
941 and whatever is syntactically considered
942 as string literals
943 `font-lock-type-face' Overridable keywords
944 `font-lock-variable-name-face' Variable declarations, indirect array and
945 hash names, POD headers/item names
946 `cperl-invalid-face' Trailing whitespace
948 Note that in several situations the highlighting tries to inform about
949 possible confusion, such as different colors for function names in
950 declarations depending on what they (do not) override, or special cases
951 m// and s/// which do not do what one would expect them to do.
953 Help with best setup of these faces for printout requested (for each of
954 the faces: please specify bold, italic, underline, shadow and box.)
956 In regular expressions (including character classes):
957 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
958 `font-lock-constant-face': Delimiters
959 `font-lock-warning-face' Special-cased m// and s//foo/,
960 Mismatched closing delimiters, parens
961 we couldn't match, misplaced quantifiers,
962 unrecognized escape sequences
963 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
964 `font-lock-type-face' escape sequences with arguments (\\x \\23 \\p \\N)
965 and others match-a-char escape sequences
966 `font-lock-keyword-face' Capturing parens, and |
967 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
968 \"Range -\" in character classes
969 `font-lock-builtin-face' \"Remaining\" 0-length constructs, multipliers
970 ?+*{}, not-capturing parens, leading
971 backslashes of escape sequences
972 `font-lock-variable-name-face' Interpolated constructs, embedded code,
973 POSIX classes (inside charclasses)
974 `font-lock-comment-face' Embedded comments
980 ;;; Portability stuff:
982 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
983 `(define-key cperl-mode-map
984 ,(if xemacs-key
985 `(if (featurep 'xemacs) ,xemacs-key ,emacs-key)
986 emacs-key)
987 ,definition))
989 (defvar cperl-del-back-ch
990 (car (append (where-is-internal 'delete-backward-char)
991 (where-is-internal 'backward-delete-char-untabify)))
992 "Character generated by key bound to `delete-backward-char'.")
994 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
995 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
997 (defun cperl-putback-char (c) ; Emacs 19
998 (push c unread-command-events)) ; Avoid undefined warning
1000 (if (featurep 'xemacs)
1001 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1002 (push (character-to-event c) unread-command-events)))
1004 (defvar cperl-do-not-fontify
1005 ;; FIXME: This is not doing what it claims!
1006 (if (string< emacs-version "19.30")
1007 'fontified
1008 'lazy-lock)
1009 "Text property which inhibits refontification.")
1011 (defsubst cperl-put-do-not-fontify (from to &optional post)
1012 ;; If POST, do not do it with postponed fontification
1013 (if (and post cperl-syntaxify-by-font-lock)
1015 (put-text-property (max (point-min) (1- from))
1016 to cperl-do-not-fontify t)))
1018 (defcustom cperl-mode-hook nil
1019 "Hook run by CPerl mode."
1020 :type 'hook
1021 :group 'cperl)
1023 (defvar cperl-syntax-state nil)
1024 (defvar cperl-syntax-done-to nil)
1026 ;; Make customization possible "in reverse"
1027 (defsubst cperl-val (symbol &optional default hairy)
1028 (cond
1029 ((eq (symbol-value symbol) 'null) default)
1030 (cperl-hairy (or hairy t))
1031 (t (symbol-value symbol))))
1034 (defun cperl-make-indent (column &optional minimum keep)
1035 "Makes indent of the current line the requested amount.
1036 Unless KEEP, removes the old indentation. Works around a bug in ancient
1037 versions of Emacs."
1038 (let ((prop (get-text-property (point) 'syntax-type)))
1039 (or keep
1040 (delete-horizontal-space))
1041 (indent-to column minimum)
1042 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1043 (and prop
1044 (> (current-column) 0)
1045 (save-excursion
1046 (beginning-of-line)
1047 (or (get-text-property (point) 'syntax-type)
1048 (and (looking-at "\\=[ \t]")
1049 (put-text-property (point) (match-end 0)
1050 'syntax-type prop)))))))
1052 ;; Probably it is too late to set these guys already, but it can help later:
1054 ;;(and cperl-clobber-mode-lists
1055 ;;(setq auto-mode-alist
1056 ;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1057 ;;(and (boundp 'interpreter-mode-alist)
1058 ;; (setq interpreter-mode-alist (append interpreter-mode-alist
1059 ;; '(("miniperl" . perl-mode))))))
1060 (eval-when-compile
1061 (mapc #'require '(imenu easymenu etags timer man info)))
1063 (define-abbrev-table 'cperl-mode-abbrev-table
1064 ;; FIXME: Use a separate abbrev table for that, enabled conditionally,
1065 ;; as we did with python-mode-skeleton-abbrev-table!
1066 (when (cperl-val 'cperl-electric-keywords)
1068 ("if" "if" cperl-electric-keyword :system t)
1069 ("elsif" "elsif" cperl-electric-keyword :system t)
1070 ("while" "while" cperl-electric-keyword :system t)
1071 ("until" "until" cperl-electric-keyword :system t)
1072 ("unless" "unless" cperl-electric-keyword :system t)
1073 ("else" "else" cperl-electric-else :system t)
1074 ("continue" "continue" cperl-electric-else :system t)
1075 ("for" "for" cperl-electric-keyword :system t)
1076 ("foreach" "foreach" cperl-electric-keyword :system t)
1077 ("formy" "formy" cperl-electric-keyword :system t)
1078 ("foreachmy" "foreachmy" cperl-electric-keyword :system t)
1079 ("do" "do" cperl-electric-keyword :system t)
1080 ("=pod" "=pod" cperl-electric-pod :system t)
1081 ("=begin" "=begin" cperl-electric-pod 0 :system t)
1082 ("=over" "=over" cperl-electric-pod :system t)
1083 ("=head1" "=head1" cperl-electric-pod :system t)
1084 ("=head2" "=head2" cperl-electric-pod :system t)
1085 ("pod" "pod" cperl-electric-pod :system t)
1086 ("over" "over" cperl-electric-pod :system t)
1087 ("head1" "head1" cperl-electric-pod :system t)
1088 ("head2" "head2" cperl-electric-pod :system t)))
1089 "Abbrev table in use in CPerl mode buffers.")
1091 (when (boundp 'edit-var-mode-alist)
1092 (add-to-list 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-"))))
1094 (defvar cperl-mode-map
1095 (let ((map (make-sparse-keymap)))
1096 (define-key map "{" 'cperl-electric-lbrace)
1097 (define-key map "[" 'cperl-electric-paren)
1098 (define-key map "(" 'cperl-electric-paren)
1099 (define-key map "<" 'cperl-electric-paren)
1100 (define-key map "}" 'cperl-electric-brace)
1101 (define-key map "]" 'cperl-electric-rparen)
1102 (define-key map ")" 'cperl-electric-rparen)
1103 (define-key map ";" 'cperl-electric-semi)
1104 (define-key map ":" 'cperl-electric-terminator)
1105 (define-key map "\C-j" 'newline-and-indent)
1106 (define-key map "\C-c\C-j" 'cperl-linefeed)
1107 (define-key map "\C-c\C-t" 'cperl-invert-if-unless)
1108 (define-key map "\C-c\C-a" 'cperl-toggle-auto-newline)
1109 (define-key map "\C-c\C-k" 'cperl-toggle-abbrev)
1110 (define-key map "\C-c\C-w" 'cperl-toggle-construct-fix)
1111 (define-key map "\C-c\C-f" 'auto-fill-mode)
1112 (define-key map "\C-c\C-e" 'cperl-toggle-electric)
1113 (define-key map "\C-c\C-b" 'cperl-find-bad-style)
1114 (define-key map "\C-c\C-p" 'cperl-pod-spell)
1115 (define-key map "\C-c\C-d" 'cperl-here-doc-spell)
1116 (define-key map "\C-c\C-n" 'cperl-narrow-to-here-doc)
1117 (define-key map "\C-c\C-v" 'cperl-next-interpolated-REx)
1118 (define-key map "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1119 (define-key map "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1120 (define-key map "\C-c\C-ha" 'cperl-toggle-autohelp)
1121 (define-key map "\C-c\C-hp" 'cperl-perldoc)
1122 (define-key map "\C-c\C-hP" 'cperl-perldoc-at-point)
1123 (define-key map "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1124 (define-key map [(control meta ?|)] 'cperl-lineup)
1125 ;;(define-key map "\M-q" 'cperl-fill-paragraph)
1126 ;;(define-key map "\e;" 'cperl-indent-for-comment)
1127 (define-key map "\177" 'cperl-electric-backspace)
1128 (define-key map "\t" 'cperl-indent-command)
1129 ;; don't clobber the backspace binding:
1130 (define-key map [(control ?c) (control ?h) ?F] 'cperl-info-on-command)
1131 (if (cperl-val 'cperl-clobber-lisp-bindings)
1132 (progn
1133 (define-key map [(control ?h) ?f]
1134 ;;(concat (char-to-string help-char) "f") ; does not work
1135 'cperl-info-on-command)
1136 (define-key map [(control ?h) ?v]
1137 ;;(concat (char-to-string help-char) "v") ; does not work
1138 'cperl-get-help)
1139 (define-key map [(control ?c) (control ?h) ?f]
1140 ;;(concat (char-to-string help-char) "f") ; does not work
1141 (key-binding "\C-hf"))
1142 (define-key map [(control ?c) (control ?h) ?v]
1143 ;;(concat (char-to-string help-char) "v") ; does not work
1144 (key-binding "\C-hv")))
1145 (define-key map [(control ?c) (control ?h) ?f]
1146 'cperl-info-on-current-command)
1147 (define-key map [(control ?c) (control ?h) ?v]
1148 ;;(concat (char-to-string help-char) "v") ; does not work
1149 'cperl-get-help))
1150 (or (boundp 'fill-paragraph-function)
1151 (substitute-key-definition
1152 'fill-paragraph 'cperl-fill-paragraph
1153 map global-map))
1154 (substitute-key-definition
1155 'indent-sexp 'cperl-indent-exp
1156 map global-map)
1157 (substitute-key-definition
1158 'indent-region 'cperl-indent-region
1159 map global-map)
1160 (substitute-key-definition
1161 'indent-for-comment 'cperl-indent-for-comment
1162 map global-map)
1163 map)
1164 "Keymap used in CPerl mode.")
1166 (defvar cperl-menu)
1167 (defvar cperl-lazy-installed)
1168 (defvar cperl-old-style nil)
1169 (condition-case nil
1170 (progn
1171 (require 'easymenu)
1172 (easy-menu-define
1173 cperl-menu cperl-mode-map "Menu for CPerl mode"
1174 '("Perl"
1175 ["Beginning of function" beginning-of-defun t]
1176 ["End of function" end-of-defun t]
1177 ["Mark function" mark-defun t]
1178 ["Indent expression" cperl-indent-exp t]
1179 ["Fill paragraph/comment" fill-paragraph t]
1180 "----"
1181 ["Line up a construction" cperl-lineup (use-region-p)]
1182 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1183 ("Regexp"
1184 ["Beautify" cperl-beautify-regexp
1185 cperl-use-syntax-table-text-property]
1186 ["Beautify one level deep" (cperl-beautify-regexp 1)
1187 cperl-use-syntax-table-text-property]
1188 ["Beautify a group" cperl-beautify-level
1189 cperl-use-syntax-table-text-property]
1190 ["Beautify a group one level deep" (cperl-beautify-level 1)
1191 cperl-use-syntax-table-text-property]
1192 ["Contract a group" cperl-contract-level
1193 cperl-use-syntax-table-text-property]
1194 ["Contract groups" cperl-contract-levels
1195 cperl-use-syntax-table-text-property]
1196 "----"
1197 ["Find next interpolated" cperl-next-interpolated-REx
1198 (next-single-property-change (point-min) 'REx-interpolated)]
1199 ["Find next interpolated (no //o)"
1200 cperl-next-interpolated-REx-0
1201 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1202 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1203 ["Find next interpolated (neither //o nor whole-REx)"
1204 cperl-next-interpolated-REx-1
1205 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1206 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1207 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1208 "----"
1209 ["Indent region" cperl-indent-region (use-region-p)]
1210 ["Comment region" cperl-comment-region (use-region-p)]
1211 ["Uncomment region" cperl-uncomment-region (use-region-p)]
1212 "----"
1213 ["Run" mode-compile (fboundp 'mode-compile)]
1214 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1215 (get-buffer "*compilation*"))]
1216 ["Next error" next-error (get-buffer "*compilation*")]
1217 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1218 "----"
1219 ["Debugger" cperl-db t]
1220 "----"
1221 ("Tools"
1222 ["Imenu" imenu (fboundp 'imenu)]
1223 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1224 "----"
1225 ["Ispell PODs" cperl-pod-spell
1226 ;; Better not to update syntaxification here:
1227 ;; debugging syntaxification can be broken by this???
1229 (get-text-property (point-min) 'in-pod)
1230 (< (progn
1231 (and cperl-syntaxify-for-menu
1232 (cperl-update-syntaxification (point-max) (point-max)))
1233 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1234 (point-max)))]
1235 ["Ispell HERE-DOCs" cperl-here-doc-spell
1236 (< (progn
1237 (and cperl-syntaxify-for-menu
1238 (cperl-update-syntaxification (point-max) (point-max)))
1239 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1240 (point-max))]
1241 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1242 (eq 'here-doc (progn
1243 (and cperl-syntaxify-for-menu
1244 (cperl-update-syntaxification (point) (point)))
1245 (get-text-property (point) 'syntax-type)))]
1246 ["Select this HERE-DOC or POD section"
1247 cperl-select-this-pod-or-here-doc
1248 (memq (progn
1249 (and cperl-syntaxify-for-menu
1250 (cperl-update-syntaxification (point) (point)))
1251 (get-text-property (point) 'syntax-type))
1252 '(here-doc pod))]
1253 "----"
1254 ["CPerl pretty print (experimental)" cperl-ps-print
1255 (fboundp 'ps-extend-face-list)]
1256 "----"
1257 ["Syntaxify region" cperl-find-pods-heres-region
1258 (use-region-p)]
1259 ["Profile syntaxification" cperl-time-fontification t]
1260 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1261 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1262 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1263 (cperl-toggle-set-debug-unwind nil t) t]
1264 "----"
1265 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1266 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1267 ("Tags"
1268 ;; ["Create tags for current file" cperl-etags t]
1269 ;; ["Add tags for current file" (cperl-etags t) t]
1270 ;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1271 ;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1272 ;; ["Create tags for Perl files in (sub)directories"
1273 ;; (cperl-etags nil 'recursive) t]
1274 ;; ["Add tags for Perl files in (sub)directories"
1275 ;; (cperl-etags t 'recursive) t])
1276 ;; ;;? cperl-write-tags (&optional file erase recurse dir inbuffer)
1277 ["Create tags for current file" (cperl-write-tags nil t) t]
1278 ["Add tags for current file" (cperl-write-tags) t]
1279 ["Create tags for Perl files in directory"
1280 (cperl-write-tags nil t nil t) t]
1281 ["Add tags for Perl files in directory"
1282 (cperl-write-tags nil nil nil t) t]
1283 ["Create tags for Perl files in (sub)directories"
1284 (cperl-write-tags nil t t t) t]
1285 ["Add tags for Perl files in (sub)directories"
1286 (cperl-write-tags nil nil t t) t]))
1287 ("Perl docs"
1288 ["Define word at point" imenu-go-find-at-position
1289 (fboundp 'imenu-go-find-at-position)]
1290 ["Help on function" cperl-info-on-command t]
1291 ["Help on function at point" cperl-info-on-current-command t]
1292 ["Help on symbol at point" cperl-get-help t]
1293 ["Perldoc" cperl-perldoc t]
1294 ["Perldoc on word at point" cperl-perldoc-at-point t]
1295 ["View manpage of POD in this file" cperl-build-manpage t]
1296 ["Auto-help on" cperl-lazy-install
1297 (not cperl-lazy-installed)]
1298 ["Auto-help off" cperl-lazy-unstall
1299 cperl-lazy-installed])
1300 ("Toggle..."
1301 ["Auto newline" cperl-toggle-auto-newline t]
1302 ["Electric parens" cperl-toggle-electric t]
1303 ["Electric keywords" cperl-toggle-abbrev t]
1304 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1305 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1306 ["Auto fill" auto-fill-mode t])
1307 ("Indent styles..."
1308 ["CPerl" (cperl-set-style "CPerl") t]
1309 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1310 ["GNU" (cperl-set-style "GNU") t]
1311 ["C++" (cperl-set-style "C++") t]
1312 ["K&R" (cperl-set-style "K&R") t]
1313 ["BSD" (cperl-set-style "BSD") t]
1314 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1315 ["Memorize Current" (cperl-set-style "Current") t]
1316 ["Memorized" (cperl-set-style-back) cperl-old-style])
1317 ("Micro-docs"
1318 ["Tips" (describe-variable 'cperl-tips) t]
1319 ["Problems" (describe-variable 'cperl-problems) t]
1320 ["Speed" (describe-variable 'cperl-speed) t]
1321 ["Praise" (describe-variable 'cperl-praise) t]
1322 ["Faces" (describe-variable 'cperl-tips-faces) t]
1323 ["CPerl mode" (describe-function 'cperl-mode) t]
1324 ["CPerl version"
1325 (message "The version of master-file for this CPerl is %s-Emacs"
1326 cperl-version)
1327 t]))))
1328 (error nil))
1330 (autoload 'c-macro-expand "cmacexp"
1331 "Display the result of expanding all C macros occurring in the region.
1332 The expansion is entirely correct because it uses the C preprocessor."
1335 ;; These two must be unwound, otherwise take exponential time
1336 (defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1337 "Regular expression to match optional whitespace with interspersed comments.
1338 Should contain exactly one group.")
1340 ;; This one is tricky to unwind; still very inefficient...
1341 (defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1342 "Regular expression to match whitespace with interspersed comments.
1343 Should contain exactly one group.")
1346 ;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1347 ;; `cperl-outline-regexp', `defun-prompt-regexp'.
1348 ;; Details of groups in this may be used in several functions; see comments
1349 ;; near mentioned above variable(s)...
1350 ;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1351 (defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1352 "Match the text after `sub' in a subroutine declaration.
1353 If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1354 of attributes (if present), or end of the name or prototype (whatever is
1355 the last)."
1356 (concat ; Assume n groups before this...
1357 "\\(" ; n+1=name-group
1358 cperl-white-and-comment-rex ; n+2=pre-name
1359 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1360 "\\)" ; END n+1=name-group
1361 (if named "" "?")
1362 "\\(" ; n+4=proto-group
1363 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1364 "\\(([^()]*)\\)" ; n+6=prototype
1365 "\\)?" ; END n+4=proto-group
1366 "\\(" ; n+7=attr-group
1367 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1368 "\\(" ; n+9=start-attr
1370 (if attr (concat
1371 "\\("
1372 cperl-maybe-white-and-comment-rex ; whitespace-comments
1373 "\\(\\sw\\|_\\)+" ; attr-name
1374 ;; attr-arg (1 level of internal parens allowed!)
1375 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1376 "\\(" ; optional : (XXX allows trailing???)
1377 cperl-maybe-white-and-comment-rex ; whitespace-comments
1378 ":\\)?"
1379 "\\)+")
1380 "[^:]")
1381 "\\)"
1382 "\\)?" ; END n+6=proto-group
1385 ;; Tired of editing this in 8 places every time I remember that there
1386 ;; is another method-defining keyword
1387 (defvar cperl-sub-keywords
1388 '("sub"))
1390 (defvar cperl-sub-regexp (regexp-opt cperl-sub-keywords))
1392 (defun cperl-char-ends-sub-keyword-p (char)
1393 "Return T if CHAR is the last character of a perl sub keyword."
1394 (cl-loop for keyword in cperl-sub-keywords
1395 when (eq char (aref keyword (1- (length keyword))))
1396 return t))
1398 ;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1399 ;; and `cperl-outline-level'.
1400 ;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1401 (defvar cperl-imenu--function-name-regexp-perl
1402 (concat
1403 "^\\(" ; 1 = all
1404 "\\([ \t]*package" ; 2 = package-group
1405 "\\(" ; 3 = package-name-group
1406 cperl-white-and-comment-rex ; 4 = pre-package-name
1407 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1408 "\\|"
1409 "[ \t]*"
1410 cperl-sub-regexp
1411 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1412 cperl-maybe-white-and-comment-rex ; 15=pre-block
1413 "\\|"
1414 "=head\\([1-4]\\)[ \t]+" ; 16=level
1415 "\\([^\n]+\\)$" ; 17=text
1416 "\\)"))
1418 (defvar cperl-outline-regexp
1419 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1421 (defvar cperl-mode-syntax-table nil
1422 "Syntax table in use in CPerl mode buffers.")
1424 (defvar cperl-string-syntax-table nil
1425 "Syntax table in use in CPerl mode string-like chunks.")
1427 (defsubst cperl-1- (p)
1428 (max (point-min) (1- p)))
1430 (defsubst cperl-1+ (p)
1431 (min (point-max) (1+ p)))
1433 (if cperl-mode-syntax-table
1435 (setq cperl-mode-syntax-table (make-syntax-table))
1436 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1437 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1438 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1439 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1440 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1441 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1442 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1443 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1444 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1445 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1446 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1447 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1448 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1449 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1450 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1451 (if cperl-under-as-char
1452 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1453 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1454 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1455 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1456 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1457 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1458 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
1459 (modify-syntax-entry ?\" "." cperl-string-syntax-table)
1460 (modify-syntax-entry ?' "." cperl-string-syntax-table)
1461 (modify-syntax-entry ?` "." cperl-string-syntax-table)
1462 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
1466 (defvar cperl-faces-init nil)
1467 ;; Fix for msb.el
1468 (defvar cperl-msb-fixed nil)
1469 (defvar cperl-use-major-mode 'cperl-mode)
1470 (defvar cperl-font-lock-multiline-start nil)
1471 (defvar cperl-font-lock-multiline nil)
1472 (defvar cperl-font-locking nil)
1474 ;; NB as it stands the code in cperl-mode assumes this only has one
1475 ;; element. If XEmacs 19 support were dropped, this could all be simplified.
1476 (defvar cperl-compilation-error-regexp-alist
1477 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
1478 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
1479 2 3))
1480 "Alist that specifies how to match errors in perl output.")
1482 (defvar compilation-error-regexp-alist)
1484 ;;;###autoload
1485 (define-derived-mode cperl-mode prog-mode "CPerl"
1486 "Major mode for editing Perl code.
1487 Expression and list commands understand all C brackets.
1488 Tab indents for Perl code.
1489 Paragraphs are separated by blank lines only.
1490 Delete converts tabs to spaces as it moves back.
1492 Various characters in Perl almost always come in pairs: {}, (), [],
1493 sometimes <>. When the user types the first, she gets the second as
1494 well, with optional special formatting done on {}. (Disabled by
1495 default.) You can always quote (with \\[quoted-insert]) the left
1496 \"paren\" to avoid the expansion. The processing of < is special,
1497 since most the time you mean \"less\". CPerl mode tries to guess
1498 whether you want to type pair <>, and inserts is if it
1499 appropriate. You can set `cperl-electric-parens-string' to the string that
1500 contains the parens from the above list you want to be electrical.
1501 Electricity of parens is controlled by `cperl-electric-parens'.
1502 You may also set `cperl-electric-parens-mark' to have electric parens
1503 look for active mark and \"embrace\" a region if possible.'
1505 CPerl mode provides expansion of the Perl control constructs:
1507 if, else, elsif, unless, while, until, continue, do,
1508 for, foreach, formy and foreachmy.
1510 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1512 The user types the keyword immediately followed by a space, which
1513 causes the construct to be expanded, and the point is positioned where
1514 she is most likely to want to be. E.g., when the user types a space
1515 following \"if\" the following appears in the buffer: if () { or if ()
1516 } { } and the cursor is between the parentheses. The user can then
1517 type some boolean expression within the parens. Having done that,
1518 typing \\[cperl-linefeed] places you - appropriately indented - on a
1519 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1520 directive line, then appropriate number of new lines is inserted).
1522 If CPerl decides that you want to insert \"English\" style construct like
1524 bite if angry;
1526 it will not do any expansion. See also help on variable
1527 `cperl-extra-newline-before-brace'. (Note that one can switch the
1528 help message on expansion by setting `cperl-message-electric-keyword'
1529 to nil.)
1531 \\[cperl-linefeed] is a convenience replacement for typing carriage
1532 return. It places you in the next line with proper indentation, or if
1533 you type it inside the inline block of control construct, like
1535 foreach (@lines) {print; print}
1537 and you are on a boundary of a statement inside braces, it will
1538 transform the construct into a multiline and will place you into an
1539 appropriately indented blank line. If you need a usual
1540 `newline-and-indent' behavior, it is on \\[newline-and-indent],
1541 see documentation on `cperl-electric-linefeed'.
1543 Use \\[cperl-invert-if-unless] to change a construction of the form
1545 if (A) { B }
1547 into
1549 B if A;
1551 \\{cperl-mode-map}
1553 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1554 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1555 on electric space between $ and {, `cperl-electric-parens-string' is
1556 the string that contains parentheses that should be electric in CPerl
1557 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1558 setting `cperl-electric-keywords' enables electric expansion of
1559 control structures in CPerl. `cperl-electric-linefeed' governs which
1560 one of two linefeed behavior is preferable. You can enable all these
1561 options simultaneously (recommended mode of use) by setting
1562 `cperl-hairy' to t. In this case you can switch separate options off
1563 by setting them to `null'. Note that one may undo the extra
1564 whitespace inserted by semis and braces in `auto-newline'-mode by
1565 consequent \\[cperl-electric-backspace].
1567 If your site has perl5 documentation in info format, you can use commands
1568 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1569 These keys run commands `cperl-info-on-current-command' and
1570 `cperl-info-on-command', which one is which is controlled by variable
1571 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1572 \(in turn affected by `cperl-hairy').
1574 Even if you have no info-format documentation, short one-liner-style
1575 help is available on \\[cperl-get-help], and one can run perldoc or
1576 man via menu.
1578 It is possible to show this help automatically after some idle time.
1579 This is regulated by variable `cperl-lazy-help-time'. Default with
1580 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1581 secs idle time . It is also possible to switch this on/off from the
1582 menu, or via \\[cperl-toggle-autohelp].
1584 Use \\[cperl-lineup] to vertically lineup some construction - put the
1585 beginning of the region at the start of construction, and make region
1586 span the needed amount of lines.
1588 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1589 `cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1590 here-docs sections. With capable Emaxen results of scan are used
1591 for indentation too, otherwise they are used for highlighting only.
1593 Variables controlling indentation style:
1594 `cperl-tab-always-indent'
1595 Non-nil means TAB in CPerl mode should always reindent the current line,
1596 regardless of where in the line point is when the TAB command is used.
1597 `cperl-indent-left-aligned-comments'
1598 Non-nil means that the comment starting in leftmost column should indent.
1599 `cperl-auto-newline'
1600 Non-nil means automatically newline before and after braces,
1601 and after colons and semicolons, inserted in Perl code. The following
1602 \\[cperl-electric-backspace] will remove the inserted whitespace.
1603 Insertion after colons requires both this variable and
1604 `cperl-auto-newline-after-colon' set.
1605 `cperl-auto-newline-after-colon'
1606 Non-nil means automatically newline even after colons.
1607 Subject to `cperl-auto-newline' setting.
1608 `cperl-indent-level'
1609 Indentation of Perl statements within surrounding block.
1610 The surrounding block's indentation is the indentation
1611 of the line on which the open-brace appears.
1612 `cperl-continued-statement-offset'
1613 Extra indentation given to a substatement, such as the
1614 then-clause of an if, or body of a while, or just a statement continuation.
1615 `cperl-continued-brace-offset'
1616 Extra indentation given to a brace that starts a substatement.
1617 This is in addition to `cperl-continued-statement-offset'.
1618 `cperl-brace-offset'
1619 Extra indentation for line if it starts with an open brace.
1620 `cperl-brace-imaginary-offset'
1621 An open brace following other text is treated as if it the line started
1622 this far to the right of the actual line indentation.
1623 `cperl-label-offset'
1624 Extra indentation for line that is a label.
1625 `cperl-min-label-indent'
1626 Minimal indentation for line that is a label.
1628 Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1629 `cperl-indent-level' 5 4 2 4
1630 `cperl-brace-offset' 0 0 0 0
1631 `cperl-continued-brace-offset' -5 -4 0 0
1632 `cperl-label-offset' -5 -4 -2 -4
1633 `cperl-continued-statement-offset' 5 4 2 4
1635 CPerl knows several indentation styles, and may bulk set the
1636 corresponding variables. Use \\[cperl-set-style] to do this. Use
1637 \\[cperl-set-style-back] to restore the memorized preexisting values
1638 \(both available from menu). See examples in `cperl-style-examples'.
1640 Part of the indentation style is how different parts of if/elsif/else
1641 statements are broken into lines; in CPerl, this is reflected on how
1642 templates for these constructs are created (controlled by
1643 `cperl-extra-newline-before-brace'), and how reflow-logic should treat
1644 \"continuation\" blocks of else/elsif/continue, controlled by the same
1645 variable, and by `cperl-extra-newline-before-brace-multiline',
1646 `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1648 If `cperl-indent-level' is 0, the statement after opening brace in
1649 column 0 is indented on
1650 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1652 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1653 with no args.
1655 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1656 or as help on variables `cperl-tips', `cperl-problems',
1657 `cperl-praise', `cperl-speed'."
1658 (if (cperl-val 'cperl-electric-linefeed)
1659 (progn
1660 (local-set-key "\C-J" 'cperl-linefeed)
1661 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1662 (if (and
1663 (cperl-val 'cperl-clobber-lisp-bindings)
1664 (cperl-val 'cperl-info-on-command-no-prompt))
1665 (progn
1666 ;; don't clobber the backspace binding:
1667 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1668 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1669 [(control c) (control h) f])))
1670 (setq local-abbrev-table cperl-mode-abbrev-table)
1671 (if (cperl-val 'cperl-electric-keywords)
1672 (abbrev-mode 1))
1673 (set-syntax-table cperl-mode-syntax-table)
1674 ;; Until Emacs is multi-threaded, we do not actually need it local:
1675 (make-local-variable 'cperl-font-lock-multiline-start)
1676 (make-local-variable 'cperl-font-locking)
1677 (set (make-local-variable 'outline-regexp) cperl-outline-regexp)
1678 (set (make-local-variable 'outline-level) 'cperl-outline-level)
1679 (set (make-local-variable 'add-log-current-defun-function)
1680 (lambda ()
1681 (save-excursion
1682 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
1683 (match-string-no-properties 1)))))
1685 (set (make-local-variable 'paragraph-start) (concat "^$\\|" page-delimiter))
1686 (set (make-local-variable 'paragraph-separate) paragraph-start)
1687 (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
1688 (if (featurep 'xemacs)
1689 (set (make-local-variable 'paren-backwards-message) t))
1690 (set (make-local-variable 'indent-line-function) #'cperl-indent-line)
1691 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
1692 (set (make-local-variable 'comment-start) "# ")
1693 (set (make-local-variable 'comment-end) "")
1694 (set (make-local-variable 'comment-column) cperl-comment-column)
1695 (set (make-local-variable 'comment-start-skip) "#+ *")
1697 ;; "[ \t]*sub"
1698 ;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1699 ;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1700 (set (make-local-variable 'defun-prompt-regexp)
1701 (concat "^[ \t]*\\("
1702 cperl-sub-regexp
1703 (cperl-after-sub-regexp 'named 'attr-groups)
1704 "\\|" ; per toke.c
1705 "\\(BEGIN\\|UNITCHECK\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1706 "\\)"
1707 cperl-maybe-white-and-comment-rex))
1708 (set (make-local-variable 'comment-indent-function) #'cperl-comment-indent)
1709 (and (boundp 'fill-paragraph-function)
1710 (set (make-local-variable 'fill-paragraph-function)
1711 #'cperl-fill-paragraph))
1712 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1713 (set (make-local-variable 'indent-region-function) #'cperl-indent-region)
1714 ;;(setq auto-fill-function #'cperl-do-auto-fill) ; Need to switch on and off!
1715 (set (make-local-variable 'imenu-create-index-function)
1716 #'cperl-imenu--create-perl-index)
1717 (set (make-local-variable 'imenu-sort-function) nil)
1718 (set (make-local-variable 'vc-rcs-header) cperl-vc-rcs-header)
1719 (set (make-local-variable 'vc-sccs-header) cperl-vc-sccs-header)
1720 (when (featurep 'xemacs)
1721 ;; This one is obsolete...
1722 (set (make-local-variable 'vc-header-alist)
1723 (or cperl-vc-header-alist ; Avoid warning
1724 `((SCCS ,(car cperl-vc-sccs-header))
1725 (RCS ,(car cperl-vc-rcs-header))))))
1726 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1727 (set (make-local-variable 'compilation-error-regexp-alist-alist)
1728 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
1729 compilation-error-regexp-alist-alist))
1730 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1731 (let ((f 'compilation-build-compilation-error-regexp-alist))
1732 (funcall f))
1733 (make-local-variable 'compilation-error-regexp-alist)
1734 (push 'cperl compilation-error-regexp-alist)))
1735 ((boundp 'compilation-error-regexp-alist);; xemacs 19.x
1736 (set (make-local-variable 'compilation-error-regexp-alist)
1737 (append cperl-compilation-error-regexp-alist
1738 compilation-error-regexp-alist))))
1739 (set (make-local-variable 'font-lock-defaults)
1740 '((cperl-load-font-lock-keywords
1741 cperl-load-font-lock-keywords-1
1742 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))
1743 ;; Reset syntaxification cache.
1744 (set (make-local-variable 'cperl-syntax-state) nil)
1745 (if cperl-use-syntax-table-text-property
1746 (if (eval-when-compile (fboundp 'syntax-propertize-rules))
1747 (progn
1748 ;; Reset syntaxification cache.
1749 (set (make-local-variable 'cperl-syntax-done-to) nil)
1750 (set (make-local-variable 'syntax-propertize-function)
1751 (lambda (start end)
1752 (goto-char start)
1753 ;; Even if cperl-fontify-syntaxically has already gone
1754 ;; beyond `start', syntax-propertize has just removed
1755 ;; syntax-table properties between start and end, so we have
1756 ;; to re-apply them.
1757 (setq cperl-syntax-done-to start)
1758 (cperl-fontify-syntaxically end))))
1759 ;; Do not introduce variable if not needed, we check it!
1760 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1761 ;; Fix broken font-lock:
1762 (or (boundp 'font-lock-unfontify-region-function)
1763 (setq font-lock-unfontify-region-function
1764 #'font-lock-default-unfontify-region))
1765 (unless (featurep 'xemacs) ; Our: just a plug for wrong font-lock
1766 (set (make-local-variable 'font-lock-unfontify-region-function)
1767 ;; not present with old Emacs
1768 #'cperl-font-lock-unfontify-region-function))
1769 ;; Reset syntaxification cache.
1770 (set (make-local-variable 'cperl-syntax-done-to) nil)
1771 (set (make-local-variable 'font-lock-syntactic-keywords)
1772 (if cperl-syntaxify-by-font-lock
1773 '((cperl-fontify-syntaxically))
1774 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1775 ;; used to ignore syntax-table text-properties. (t) is a hack
1776 ;; to make font-lock think that font-lock-syntactic-keywords
1777 ;; are defined.
1778 '(t)))))
1779 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1780 (progn
1781 (setq cperl-font-lock-multiline t) ; Not localized...
1782 (set (make-local-variable 'font-lock-multiline) t))
1783 (set (make-local-variable 'font-lock-fontify-region-function)
1784 ;; not present with old Emacs
1785 #'cperl-font-lock-fontify-region-function))
1786 (set (make-local-variable 'font-lock-fontify-region-function)
1787 #'cperl-font-lock-fontify-region-function)
1788 (make-local-variable 'cperl-old-style)
1789 (set (make-local-variable 'normal-auto-fill-function)
1790 #'cperl-do-auto-fill)
1791 (if (cperl-val 'cperl-font-lock)
1792 (progn (or cperl-faces-init (cperl-init-faces))
1793 (font-lock-mode 1)))
1794 (set (make-local-variable 'facemenu-add-face-function)
1795 #'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1796 (and (boundp 'msb-menu-cond)
1797 (not cperl-msb-fixed)
1798 (cperl-msb-fix))
1799 (if (fboundp 'easy-menu-add)
1800 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1801 (if cperl-hook-after-change
1802 (add-hook 'after-change-functions #'cperl-after-change-function nil t))
1803 ;; After hooks since fontification will break this
1804 (if cperl-pod-here-scan
1805 (or cperl-syntaxify-by-font-lock
1806 (progn (or cperl-faces-init (cperl-init-faces-weak))
1807 (cperl-find-pods-heres))))
1808 ;; Setup Flymake
1809 (add-hook 'flymake-diagnostic-functions #'perl-flymake nil t))
1811 ;; Fix for perldb - make default reasonable
1812 (defun cperl-db ()
1813 (interactive)
1814 (require 'gud)
1815 ;; FIXME: Use `read-string' or `read-shell-command'?
1816 (perldb (read-from-minibuffer "Run perldb (like this): "
1817 (if (consp gud-perldb-history)
1818 (car gud-perldb-history)
1819 (concat "perl -d "
1820 (buffer-file-name)))
1821 nil nil
1822 '(gud-perldb-history . 1))))
1824 (defun cperl-msb-fix ()
1825 ;; Adds perl files to msb menu, supposes that msb is already loaded
1826 (setq cperl-msb-fixed t)
1827 (let* ((l (length msb-menu-cond))
1828 (last (nth (1- l) msb-menu-cond))
1829 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1830 (handle (1- (nth 1 last))))
1831 (setcdr precdr (list
1832 (list
1833 '(memq major-mode '(cperl-mode perl-mode))
1834 handle
1835 "Perl Files (%d)")
1836 last))))
1838 ;; This is used by indent-for-comment
1839 ;; to decide how much to indent a comment in CPerl code
1840 ;; based on its context. Do fallback if comment is found wrong.
1842 (defvar cperl-wrong-comment)
1843 (defvar cperl-st-cfence '(14)) ; Comment-fence
1844 (defvar cperl-st-sfence '(15)) ; String-fence
1845 (defvar cperl-st-punct '(1))
1846 (defvar cperl-st-word '(2))
1847 (defvar cperl-st-bra '(4 . ?\>))
1848 (defvar cperl-st-ket '(5 . ?\<))
1851 (defun cperl-comment-indent () ; called at point at supposed comment
1852 (let ((p (point)) (c (current-column)) was phony)
1853 (if (and (not cperl-indent-comment-at-column-0)
1854 (looking-at "^#"))
1855 0 ; Existing comment at bol stays there.
1856 ;; Wrong comment found
1857 (save-excursion
1858 (setq was (cperl-to-comment-or-eol)
1859 phony (eq (get-text-property (point) 'syntax-table)
1860 cperl-st-cfence))
1861 (if phony
1862 (progn ; Too naive???
1863 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1864 (if (eq (preceding-char) ?\#)
1865 (forward-char -1))
1866 (setq was nil)))
1867 (if (= (point) p) ; Our caller found a correct place
1868 (progn
1869 (skip-chars-backward " \t")
1870 (setq was (current-column))
1871 (if (eq was 0)
1872 comment-column
1873 (max (1+ was) ; Else indent at comment column
1874 comment-column)))
1875 ;; No, the caller found a random place; we need to edit ourselves
1876 (if was nil
1877 (insert comment-start)
1878 (backward-char (length comment-start)))
1879 (setq cperl-wrong-comment t)
1880 (cperl-make-indent comment-column 1) ; Indent min 1
1881 c)))))
1883 ;;(defun cperl-comment-indent-fallback ()
1884 ;; "Is called if the standard comment-search procedure fails.
1885 ;;Point is at start of real comment."
1886 ;; (let ((c (current-column)) target cnt prevc)
1887 ;; (if (= c comment-column) nil
1888 ;; (setq cnt (skip-chars-backward "[ \t]"))
1889 ;; (setq target (max (1+ (setq prevc
1890 ;; (current-column))) ; Else indent at comment column
1891 ;; comment-column))
1892 ;; (if (= c comment-column) nil
1893 ;; (delete-backward-char cnt)
1894 ;; (while (< prevc target)
1895 ;; (insert "\t")
1896 ;; (setq prevc (current-column)))
1897 ;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1898 ;; (while (< prevc target)
1899 ;; (insert " ")
1900 ;; (setq prevc (current-column)))))))
1902 (defun cperl-indent-for-comment ()
1903 "Substitute for `indent-for-comment' in CPerl."
1904 (interactive)
1905 (let (cperl-wrong-comment)
1906 (indent-for-comment)
1907 (if cperl-wrong-comment ; set by `cperl-comment-indent'
1908 (progn (cperl-to-comment-or-eol)
1909 (forward-char (length comment-start))))))
1911 (defun cperl-comment-region (b e arg)
1912 "Comment or uncomment each line in the region in CPerl mode.
1913 See `comment-region'."
1914 (interactive "r\np")
1915 (let ((comment-start "#"))
1916 (comment-region b e arg)))
1918 (defun cperl-uncomment-region (b e arg)
1919 "Uncomment or comment each line in the region in CPerl mode.
1920 See `comment-region'."
1921 (interactive "r\np")
1922 (let ((comment-start "#"))
1923 (comment-region b e (- arg))))
1925 (defvar cperl-brace-recursing nil)
1927 (defun cperl-electric-brace (arg &optional only-before)
1928 "Insert character and correct line's indentation.
1929 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
1930 place (even in empty line), but not after. If after \")\" and the inserted
1931 char is \"{\", insert extra newline before only if
1932 `cperl-extra-newline-before-brace'."
1933 (interactive "P")
1934 (let (insertpos
1935 (other-end (if (and cperl-electric-parens-mark
1936 (region-active-p)
1937 (< (mark) (point)))
1938 (mark)
1939 nil)))
1940 (if (and other-end
1941 (not cperl-brace-recursing)
1942 (cperl-val 'cperl-electric-parens)
1943 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
1944 ;; Need to insert a matching pair
1945 (progn
1946 (save-excursion
1947 (setq insertpos (point-marker))
1948 (goto-char other-end)
1949 (setq last-command-event ?\{)
1950 (cperl-electric-lbrace arg insertpos))
1951 (forward-char 1))
1952 ;; Check whether we close something "usual" with `}'
1953 (if (and (eq last-command-event ?\})
1954 (not
1955 (condition-case nil
1956 (save-excursion
1957 (up-list (- (prefix-numeric-value arg)))
1958 ;;(cperl-after-block-p (point-min))
1959 (or (cperl-after-expr-p nil "{;)")
1960 ;; after sub, else, continue
1961 (cperl-after-block-p nil 'pre)))
1962 (error nil))))
1963 ;; Just insert the guy
1964 (self-insert-command (prefix-numeric-value arg))
1965 (if (and (not arg) ; No args, end (of empty line or auto)
1966 (eolp)
1967 (or (and (null only-before)
1968 (save-excursion
1969 (skip-chars-backward " \t")
1970 (bolp)))
1971 (and (eq last-command-event ?\{) ; Do not insert newline
1972 ;; if after ")" and `cperl-extra-newline-before-brace'
1973 ;; is nil, do not insert extra newline.
1974 (not cperl-extra-newline-before-brace)
1975 (save-excursion
1976 (skip-chars-backward " \t")
1977 (eq (preceding-char) ?\))))
1978 (if cperl-auto-newline
1979 (progn (cperl-indent-line) (newline) t) nil)))
1980 (progn
1981 (self-insert-command (prefix-numeric-value arg))
1982 (cperl-indent-line)
1983 (if cperl-auto-newline
1984 (setq insertpos (1- (point))))
1985 (if (and cperl-auto-newline (null only-before))
1986 (progn
1987 (newline)
1988 (cperl-indent-line)))
1989 (save-excursion
1990 (if insertpos (progn (goto-char insertpos)
1991 (search-forward (make-string
1992 1 last-command-event))
1993 (setq insertpos (1- (point)))))
1994 (delete-char -1))))
1995 (if insertpos
1996 (save-excursion
1997 (goto-char insertpos)
1998 (self-insert-command (prefix-numeric-value arg)))
1999 (self-insert-command (prefix-numeric-value arg)))))))
2001 (defun cperl-electric-lbrace (arg &optional end)
2002 "Insert character, correct line's indentation, correct quoting by space."
2003 (interactive "P")
2004 (let ((cperl-brace-recursing t)
2005 (cperl-auto-newline cperl-auto-newline)
2006 (other-end (or end
2007 (if (and cperl-electric-parens-mark
2008 (region-active-p)
2009 (> (mark) (point)))
2010 (save-excursion
2011 (goto-char (mark))
2012 (point-marker))
2013 nil)))
2014 pos)
2015 (and (cperl-val 'cperl-electric-lbrace-space)
2016 (eq (preceding-char) ?$)
2017 (save-excursion
2018 (skip-chars-backward "$")
2019 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2020 (insert ?\s))
2021 ;; Check whether we are in comment
2022 (if (and
2023 (save-excursion
2024 (beginning-of-line)
2025 (not (looking-at "[ \t]*#")))
2026 (cperl-after-expr-p nil "{;)"))
2028 (setq cperl-auto-newline nil))
2029 (cperl-electric-brace arg)
2030 (and (cperl-val 'cperl-electric-parens)
2031 (eq last-command-event ?{)
2032 (memq last-command-event
2033 (append cperl-electric-parens-string nil))
2034 (or (if other-end (goto-char (marker-position other-end)))
2036 (setq last-command-event ?} pos (point))
2037 (progn (cperl-electric-brace arg t)
2038 (goto-char pos)))))
2040 (defun cperl-electric-paren (arg)
2041 "Insert an opening parenthesis or a matching pair of parentheses.
2042 See `cperl-electric-parens'."
2043 (interactive "P")
2044 (let ((other-end (if (and cperl-electric-parens-mark
2045 (region-active-p)
2046 (> (mark) (point)))
2047 (save-excursion
2048 (goto-char (mark))
2049 (point-marker))
2050 nil)))
2051 (if (and (cperl-val 'cperl-electric-parens)
2052 (memq last-command-event
2053 (append cperl-electric-parens-string nil))
2054 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2055 (if (eq last-command-event ?<)
2056 (progn
2057 ;; This code is too electric, see Bug#3943.
2058 ;; (and abbrev-mode ; later it is too late, may be after `for'
2059 ;; (expand-abbrev))
2060 (cperl-after-expr-p nil "{;(,:="))
2062 (progn
2063 (self-insert-command (prefix-numeric-value arg))
2064 (if other-end (goto-char (marker-position other-end)))
2065 (insert (make-string
2066 (prefix-numeric-value arg)
2067 (cdr (assoc last-command-event '((?{ .?})
2068 (?\[ . ?\])
2069 (?\( . ?\))
2070 (?< . ?>))))))
2071 (forward-char (- (prefix-numeric-value arg))))
2072 (self-insert-command (prefix-numeric-value arg)))))
2074 (defun cperl-electric-rparen (arg)
2075 "Insert a matching pair of parentheses if marking is active.
2076 If not, or if we are not at the end of marking range, would self-insert.
2077 Affected by `cperl-electric-parens'."
2078 (interactive "P")
2079 (let ((other-end (if (and cperl-electric-parens-mark
2080 (cperl-val 'cperl-electric-parens)
2081 (memq last-command-event
2082 (append cperl-electric-parens-string nil))
2083 (region-active-p)
2084 (< (mark) (point)))
2085 (mark)
2086 nil))
2088 (if (and other-end
2089 (cperl-val 'cperl-electric-parens)
2090 (memq last-command-event '( ?\) ?\] ?\} ?\> ))
2091 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2093 (progn
2094 (self-insert-command (prefix-numeric-value arg))
2095 (setq p (point))
2096 (if other-end (goto-char other-end))
2097 (insert (make-string
2098 (prefix-numeric-value arg)
2099 (cdr (assoc last-command-event '((?\} . ?\{)
2100 (?\] . ?\[)
2101 (?\) . ?\()
2102 (?\> . ?\<))))))
2103 (goto-char (1+ p)))
2104 (self-insert-command (prefix-numeric-value arg)))))
2106 (defun cperl-electric-keyword ()
2107 "Insert a construction appropriate after a keyword.
2108 Help message may be switched off by setting `cperl-message-electric-keyword'
2109 to nil."
2110 (let ((beg (point-at-bol))
2111 (dollar (and (eq last-command-event ?$)
2112 (eq this-command 'self-insert-command)))
2113 (delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2114 (memq this-command '(self-insert-command newline))))
2115 my do)
2116 (and (save-excursion
2117 (condition-case nil
2118 (progn
2119 (backward-sexp 1)
2120 (setq do (looking-at "do\\>")))
2121 (error nil))
2122 (cperl-after-expr-p nil "{;:"))
2123 (save-excursion
2124 (not
2125 (re-search-backward
2126 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2127 beg t)))
2128 (save-excursion (or (not (re-search-backward "^=" nil t))
2130 (looking-at "=cut")
2131 (looking-at "=end")
2132 (and cperl-use-syntax-table-text-property
2133 (not (eq (get-text-property (point)
2134 'syntax-type)
2135 'pod))))))
2136 (save-excursion (forward-sexp -1)
2137 (not (memq (following-char) (append "$@%&*" nil))))
2138 (progn
2139 (and (eq (preceding-char) ?y)
2140 (progn ; "foreachmy"
2141 (forward-char -2)
2142 (insert " ")
2143 (forward-char 2)
2144 (setq my t dollar t
2145 delete
2146 (memq this-command '(self-insert-command newline)))))
2147 (and dollar (insert " $"))
2148 (cperl-indent-line)
2149 ;;(insert " () {\n}")
2150 (cond
2151 (cperl-extra-newline-before-brace
2152 (insert (if do "\n" " ()\n"))
2153 (insert "{")
2154 (cperl-indent-line)
2155 (insert "\n")
2156 (cperl-indent-line)
2157 (insert "\n}")
2158 (and do (insert " while ();")))
2160 (insert (if do " {\n} while ();" " () {\n}"))))
2161 (or (looking-at "[ \t]\\|$") (insert " "))
2162 (cperl-indent-line)
2163 (if dollar (progn (search-backward "$")
2164 (if my
2165 (forward-char 1)
2166 (delete-char 1)))
2167 (search-backward ")")
2168 (if (eq last-command-event ?\()
2169 (progn ; Avoid "if (())"
2170 (delete-char -1)
2171 (delete-char 1))))
2172 (if delete
2173 (cperl-putback-char cperl-del-back-ch))
2174 (if cperl-message-electric-keyword
2175 (message "Precede char by C-q to avoid expansion"))))))
2177 (defun cperl-ensure-newlines (n &optional pos)
2178 "Make sure there are N newlines after the point."
2179 (or pos (setq pos (point)))
2180 (if (looking-at "\n")
2181 (forward-char 1)
2182 (insert "\n"))
2183 (if (> n 1)
2184 (cperl-ensure-newlines (1- n) pos)
2185 (goto-char pos)))
2187 (defun cperl-electric-pod ()
2188 "Insert a POD chunk appropriate after a =POD directive."
2189 (let ((delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2190 (memq this-command '(self-insert-command newline))))
2191 head1 notlast name p really-delete over)
2192 (and (save-excursion
2193 (forward-word-strictly -1)
2194 (and
2195 (eq (preceding-char) ?=)
2196 (progn
2197 (setq head1 (looking-at "head1\\>[ \t]*$"))
2198 (setq over (and (looking-at "over\\>[ \t]*$")
2199 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2200 (forward-char -1)
2201 (bolp))
2203 (get-text-property (point) 'in-pod)
2204 (cperl-after-expr-p nil "{;:")
2205 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2206 (not (or (looking-at "\n*=cut") (looking-at "\n*=end")))
2207 (or (not cperl-use-syntax-table-text-property)
2208 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2209 (progn
2210 (save-excursion
2211 (setq notlast (re-search-forward "^\n=" nil t)))
2212 (or notlast
2213 (progn
2214 (insert "\n\n=cut")
2215 (cperl-ensure-newlines 2)
2216 (forward-word-strictly -2)
2217 (if (and head1
2218 (not
2219 (save-excursion
2220 (forward-char -1)
2221 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2222 nil t)))) ; Only one
2223 (progn
2224 (forward-word-strictly 1)
2225 (setq name (file-name-base (buffer-file-name))
2226 p (point))
2227 (insert " NAME\n\n" name
2228 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2229 "=head1 DESCRIPTION")
2230 (cperl-ensure-newlines 4)
2231 (goto-char p)
2232 (forward-word-strictly 2)
2233 (end-of-line)
2234 (setq really-delete t))
2235 (forward-word-strictly 1))))
2236 (if over
2237 (progn
2238 (setq p (point))
2239 (insert "\n\n=item \n\n\n\n"
2240 "=back")
2241 (cperl-ensure-newlines 2)
2242 (goto-char p)
2243 (forward-word-strictly 1)
2244 (end-of-line)
2245 (setq really-delete t)))
2246 (if (and delete really-delete)
2247 (cperl-putback-char cperl-del-back-ch))))))
2249 (defun cperl-electric-else ()
2250 "Insert a construction appropriate after a keyword.
2251 Help message may be switched off by setting `cperl-message-electric-keyword'
2252 to nil."
2253 (let ((beg (point-at-bol)))
2254 (and (save-excursion
2255 (backward-sexp 1)
2256 (cperl-after-expr-p nil "{;:"))
2257 (save-excursion
2258 (not
2259 (re-search-backward
2260 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2261 beg t)))
2262 (save-excursion (or (not (re-search-backward "^=" nil t))
2263 (looking-at "=cut")
2264 (looking-at "=end")
2265 (and cperl-use-syntax-table-text-property
2266 (not (eq (get-text-property (point)
2267 'syntax-type)
2268 'pod)))))
2269 (progn
2270 (cperl-indent-line)
2271 ;;(insert " {\n\n}")
2272 (cond
2273 (cperl-extra-newline-before-brace
2274 (insert "\n")
2275 (insert "{")
2276 (cperl-indent-line)
2277 (insert "\n\n}"))
2279 (insert " {\n\n}")))
2280 (or (looking-at "[ \t]\\|$") (insert " "))
2281 (cperl-indent-line)
2282 (forward-line -1)
2283 (cperl-indent-line)
2284 (cperl-putback-char cperl-del-back-ch)
2285 (setq this-command 'cperl-electric-else)
2286 (if cperl-message-electric-keyword
2287 (message "Precede char by C-q to avoid expansion"))))))
2289 (defun cperl-linefeed ()
2290 "Go to end of line, open a new line and indent appropriately.
2291 If in POD, insert appropriate lines."
2292 (interactive)
2293 (let ((beg (point-at-bol))
2294 (end (point-at-eol))
2295 (pos (point)) start over cut res)
2296 (if (and ; Check if we need to split:
2297 ; i.e., on a boundary and inside "{...}"
2298 (save-excursion (cperl-to-comment-or-eol)
2299 (>= (point) pos)) ; Not in a comment
2300 (or (save-excursion
2301 (skip-chars-backward " \t" beg)
2302 (forward-char -1)
2303 (looking-at "[;{]")) ; After { or ; + spaces
2304 (looking-at "[ \t]*}") ; Before }
2305 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2306 (save-excursion
2307 (and
2308 (eq (car (parse-partial-sexp pos end -1)) -1)
2309 ; Leave the level of parens
2310 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2311 ; Are at end
2312 (cperl-after-block-p (point-min))
2313 (progn
2314 (backward-sexp 1)
2315 (setq start (point-marker))
2316 (<= start pos))))) ; Redundant? Are after the
2317 ; start of parens group.
2318 (progn
2319 (skip-chars-backward " \t")
2320 (or (memq (preceding-char) (append ";{" nil))
2321 (insert ";"))
2322 (insert "\n")
2323 (forward-line -1)
2324 (cperl-indent-line)
2325 (goto-char start)
2326 (or (looking-at "{[ \t]*$") ; If there is a statement
2327 ; before, move it to separate line
2328 (progn
2329 (forward-char 1)
2330 (insert "\n")
2331 (cperl-indent-line)))
2332 (forward-line 1) ; We are on the target line
2333 (cperl-indent-line)
2334 (beginning-of-line)
2335 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2336 ; after, move it to separate line
2337 (progn
2338 (end-of-line)
2339 (search-backward "}" beg)
2340 (skip-chars-backward " \t")
2341 (or (memq (preceding-char) (append ";{" nil))
2342 (insert ";"))
2343 (insert "\n")
2344 (cperl-indent-line)
2345 (forward-line -1)))
2346 (forward-line -1) ; We are on the line before target
2347 (end-of-line)
2348 (newline-and-indent))
2349 (end-of-line) ; else - no splitting
2350 (cond
2351 ((and (looking-at "\n[ \t]*{$")
2352 (save-excursion
2353 (skip-chars-backward " \t")
2354 (eq (preceding-char) ?\)))) ; Probably if () {} group
2355 ; with an extra newline.
2356 (forward-line 2)
2357 (cperl-indent-line))
2358 ((save-excursion ; In POD header
2359 (forward-paragraph -1)
2360 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2361 ;; We are after \n now, so look for the rest
2362 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2363 (progn
2364 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=\\(cut\\|end\\)\\>"))
2365 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2366 t)))
2367 (if (and over
2368 (progn
2369 (forward-paragraph -1)
2370 (forward-word-strictly 1)
2371 (setq pos (point))
2372 (setq cut (buffer-substring (point) (point-at-eol)))
2373 (delete-char (- (point-at-eol) (point)))
2374 (setq res (expand-abbrev))
2375 (save-excursion
2376 (goto-char pos)
2377 (insert cut))
2378 res))
2380 (cperl-ensure-newlines (if cut 2 4))
2381 (forward-line 2)))
2382 ((get-text-property (point) 'in-pod) ; In POD section
2383 (cperl-ensure-newlines 4)
2384 (forward-line 2))
2385 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2386 (forward-line 1)
2387 (cperl-indent-line))
2389 (newline-and-indent))))))
2391 (defun cperl-electric-semi (arg)
2392 "Insert character and correct line's indentation."
2393 (interactive "P")
2394 (if cperl-auto-newline
2395 (cperl-electric-terminator arg)
2396 (self-insert-command (prefix-numeric-value arg))
2397 (if cperl-autoindent-on-semi
2398 (cperl-indent-line))))
2400 (defun cperl-electric-terminator (arg)
2401 "Insert character and correct line's indentation."
2402 (interactive "P")
2403 (let ((end (point))
2404 (auto (and cperl-auto-newline
2405 (or (not (eq last-command-event ?:))
2406 cperl-auto-newline-after-colon)))
2407 insertpos)
2408 (if (and ;;(not arg)
2409 (eolp)
2410 (not (save-excursion
2411 (beginning-of-line)
2412 (skip-chars-forward " \t")
2414 ;; Ignore in comment lines
2415 (= (following-char) ?#)
2416 ;; Colon is special only after a label
2417 ;; So quickly rule out most other uses of colon
2418 ;; and do no indentation for them.
2419 (and (eq last-command-event ?:)
2420 (save-excursion
2421 (forward-word-strictly 1)
2422 (skip-chars-forward " \t")
2423 (and (< (point) end)
2424 (progn (goto-char (- end 1))
2425 (not (looking-at ":"))))))
2426 (progn
2427 (beginning-of-defun)
2428 (let ((pps (parse-partial-sexp (point) end)))
2429 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2430 (progn
2431 (self-insert-command (prefix-numeric-value arg))
2432 ;;(forward-char -1)
2433 (if auto (setq insertpos (point-marker)))
2434 ;;(forward-char 1)
2435 (cperl-indent-line)
2436 (if auto
2437 (progn
2438 (newline)
2439 (cperl-indent-line)))
2440 (save-excursion
2441 (if insertpos (goto-char (1- (marker-position insertpos)))
2442 (forward-char -1))
2443 (delete-char 1))))
2444 (if insertpos
2445 (save-excursion
2446 (goto-char insertpos)
2447 (self-insert-command (prefix-numeric-value arg)))
2448 (self-insert-command (prefix-numeric-value arg)))))
2450 (defun cperl-electric-backspace (arg)
2451 "Backspace, or remove whitespace around the point inserted by an electric key.
2452 Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2453 (interactive "p")
2454 (if (and cperl-auto-newline
2455 (memq last-command '(cperl-electric-semi
2456 cperl-electric-terminator
2457 cperl-electric-lbrace))
2458 (memq (preceding-char) '(?\s ?\t ?\n)))
2459 (let (p)
2460 (if (eq last-command 'cperl-electric-lbrace)
2461 (skip-chars-forward " \t\n"))
2462 (setq p (point))
2463 (skip-chars-backward " \t\n")
2464 (delete-region (point) p))
2465 (and (eq last-command 'cperl-electric-else)
2466 ;; We are removing the whitespace *inside* cperl-electric-else
2467 (setq this-command 'cperl-electric-else-really))
2468 (if (and cperl-auto-newline
2469 (eq last-command 'cperl-electric-else-really)
2470 (memq (preceding-char) '(?\s ?\t ?\n)))
2471 (let (p)
2472 (skip-chars-forward " \t\n")
2473 (setq p (point))
2474 (skip-chars-backward " \t\n")
2475 (delete-region (point) p))
2476 (if cperl-electric-backspace-untabify
2477 (backward-delete-char-untabify arg)
2478 (call-interactively 'delete-backward-char)))))
2480 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2482 (defun cperl-inside-parens-p () ;; NOT USED????
2483 (condition-case ()
2484 (save-excursion
2485 (save-restriction
2486 (narrow-to-region (point)
2487 (progn (beginning-of-defun) (point)))
2488 (goto-char (point-max))
2489 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2490 (error nil)))
2492 (defun cperl-indent-command (&optional whole-exp)
2493 "Indent current line as Perl code, or in some cases insert a tab character.
2494 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2495 line. Otherwise, indent the current line only if point is at the left margin
2496 or in the line's indentation; otherwise insert a tab.
2498 A numeric argument, regardless of its value,
2499 means indent rigidly all the lines of the expression starting after point
2500 so that this line becomes properly indented.
2501 The relative indentation among the lines of the expression are preserved."
2502 (interactive "P")
2503 (cperl-update-syntaxification (point) (point))
2504 (if whole-exp
2505 ;; If arg, always indent this line as Perl
2506 ;; and shift remaining lines of expression the same amount.
2507 (let ((shift-amt (cperl-indent-line))
2508 beg end)
2509 (save-excursion
2510 (if cperl-tab-always-indent
2511 (beginning-of-line))
2512 (setq beg (point))
2513 (forward-sexp 1)
2514 (setq end (point))
2515 (goto-char beg)
2516 (forward-line 1)
2517 (setq beg (point)))
2518 (if (and shift-amt (> end beg))
2519 (indent-code-rigidly beg end shift-amt "#")))
2520 (if (and (not cperl-tab-always-indent)
2521 (save-excursion
2522 (skip-chars-backward " \t")
2523 (not (bolp))))
2524 (insert-tab)
2525 (cperl-indent-line))))
2527 (defun cperl-indent-line (&optional parse-data)
2528 "Indent current line as Perl code.
2529 Return the amount the indentation changed by."
2530 (let ((case-fold-search nil)
2531 (pos (- (point-max) (point)))
2532 indent i shift-amt)
2533 (setq indent (cperl-calculate-indent parse-data)
2534 i indent)
2535 (beginning-of-line)
2536 (cond ((or (eq indent nil) (eq indent t))
2537 (setq indent (current-indentation) i nil))
2538 ;;((eq indent t) ; Never?
2539 ;; (setq indent (cperl-calculate-indent-within-comment)))
2540 ;;((looking-at "[ \t]*#")
2541 ;; (setq indent 0))
2543 (skip-chars-forward " \t")
2544 (if (listp indent) (setq indent (car indent)))
2545 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2546 (not (looking-at "[smy]:\\|tr:")))
2547 (and (> indent 0)
2548 (setq indent (max cperl-min-label-indent
2549 (+ indent cperl-label-offset)))))
2550 ((= (following-char) ?})
2551 (setq indent (- indent cperl-indent-level)))
2552 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2553 (setq indent (+ indent cperl-close-paren-offset)))
2554 ((= (following-char) ?{)
2555 (setq indent (+ indent cperl-brace-offset))))))
2556 (skip-chars-forward " \t")
2557 (setq shift-amt (and i (- indent (current-column))))
2558 (if (or (not shift-amt)
2559 (zerop shift-amt))
2560 (if (> (- (point-max) pos) (point))
2561 (goto-char (- (point-max) pos)))
2562 ;;(delete-region beg (point))
2563 ;;(indent-to indent)
2564 (cperl-make-indent indent)
2565 ;; If initial point was within line's indentation,
2566 ;; position after the indentation. Else stay at same point in text.
2567 (if (> (- (point-max) pos) (point))
2568 (goto-char (- (point-max) pos))))
2569 shift-amt))
2571 (defun cperl-after-label ()
2572 ;; Returns true if the point is after label. Does not do save-excursion.
2573 (and (eq (preceding-char) ?:)
2574 (memq (char-syntax (char-after (- (point) 2)))
2575 '(?w ?_))
2576 (progn
2577 (backward-sexp)
2578 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2580 (defun cperl-get-state (&optional parse-start start-state)
2581 "Return list (START STATE DEPTH PRESTART),
2582 START is a good place to start parsing, or equal to
2583 PARSE-START if preset,
2584 STATE is what is returned by `parse-partial-sexp'.
2585 DEPTH is true is we are immediately after end of block
2586 which contains START.
2587 PRESTART is the position basing on which START was found."
2588 (save-excursion
2589 (let ((start-point (point)) depth state start prestart)
2590 (if (and parse-start
2591 (<= parse-start start-point))
2592 (goto-char parse-start)
2593 (beginning-of-defun)
2594 (setq start-state nil))
2595 (setq prestart (point))
2596 (if start-state nil
2597 ;; Try to go out, if sub is not on the outermost level
2598 (while (< (point) start-point)
2599 (setq start (point) parse-start start depth nil
2600 state (parse-partial-sexp start start-point -1))
2601 (if (> (car state) -1) nil
2602 ;; The current line could start like }}}, so the indentation
2603 ;; corresponds to a different level than what we reached
2604 (setq depth t)
2605 (beginning-of-line 2))) ; Go to the next line.
2606 (if start (goto-char start))) ; Not at the start of file
2607 (setq start (point))
2608 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2609 (list start state depth prestart))))
2611 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2613 (defun cperl-beginning-of-property (p prop &optional lim)
2614 "Given that P has a property PROP, find where the property starts.
2615 Will not look before LIM."
2616 ;;; XXXX What to do at point-max???
2617 (or (previous-single-property-change (cperl-1+ p) prop lim)
2618 (point-min))
2619 ;; (cond ((eq p (point-min))
2620 ;; p)
2621 ;; ((and lim (<= p lim))
2622 ;; p)
2623 ;; ((not (get-text-property (1- p) prop))
2624 ;; p)
2625 ;; (t (or (previous-single-property-change p look-prop lim)
2626 ;; (point-min))))
2629 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2630 ;; the sniffer logic to understand what the current line MEANS.
2631 (cperl-update-syntaxification (point) (point))
2632 (let ((res (get-text-property (point) 'syntax-type)))
2633 (save-excursion
2634 (cond
2635 ((and (memq res '(pod here-doc here-doc-delim format))
2636 (not (get-text-property (point) 'indentable)))
2637 (vector res))
2638 ;; before start of POD - whitespace found since do not have 'pod!
2639 ((looking-at "[ \t]*\n=")
2640 (error "Spaces before POD section!"))
2641 ((and (not cperl-indent-left-aligned-comments)
2642 (looking-at "^#"))
2643 [comment-special:at-beginning-of-line])
2644 ((get-text-property (point) 'in-pod)
2645 [in-pod])
2647 (beginning-of-line)
2648 (let* ((indent-point (point))
2649 (char-after-pos (save-excursion
2650 (skip-chars-forward " \t")
2651 (point)))
2652 (char-after (char-after char-after-pos))
2653 (pre-indent-point (point))
2654 p prop look-prop is-block delim)
2655 (save-excursion ; Know we are not in POD, find appropriate pos before
2656 (cperl-backward-to-noncomment nil)
2657 (setq p (max (point-min) (1- (point)))
2658 prop (get-text-property p 'syntax-type)
2659 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2660 'syntax-type))
2661 (if (memq prop '(pod here-doc format here-doc-delim))
2662 (progn
2663 (goto-char (cperl-beginning-of-property p look-prop))
2664 (beginning-of-line)
2665 (setq pre-indent-point (point)))))
2666 (goto-char pre-indent-point) ; Orig line skipping preceding pod/etc
2667 (let* ((case-fold-search nil)
2668 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2669 (start (or (nth 2 parse-data) ; last complete sexp terminated
2670 (nth 0 s-s))) ; Good place to start parsing
2671 (state (nth 1 s-s))
2672 (containing-sexp (car (cdr state)))
2673 old-indent)
2674 (if (and
2675 ;;containing-sexp ;; We are buggy at toplevel :-(
2676 parse-data)
2677 (progn
2678 (setcar parse-data pre-indent-point)
2679 (setcar (cdr parse-data) state)
2680 (or (nth 2 parse-data)
2681 (setcar (cddr parse-data) start))
2682 ;; Before this point: end of statement
2683 (setq old-indent (nth 3 parse-data))))
2684 (cond ((get-text-property (point) 'indentable)
2685 ;; indent to "after" the surrounding open
2686 ;; (same offset as `cperl-beautify-regexp-piece'),
2687 ;; skip blanks if we do not close the expression.
2688 (setq delim ; We do not close the expression
2689 (get-text-property
2690 (cperl-1+ char-after-pos) 'indentable)
2691 p (1+ (cperl-beginning-of-property
2692 (point) 'indentable))
2693 is-block ; misused for: preceding line in REx
2694 (save-excursion ; Find preceding line
2695 (cperl-backward-to-noncomment p)
2696 (beginning-of-line)
2697 (if (<= (point) p)
2698 (progn ; get indent from the first line
2699 (goto-char p)
2700 (skip-chars-forward " \t")
2701 (if (memq (char-after (point))
2702 (append "#\n" nil))
2703 nil ; Can't use indentation of this line...
2704 (point)))
2705 (skip-chars-forward " \t")
2706 (point)))
2707 prop (parse-partial-sexp p char-after-pos))
2708 (cond ((not delim) ; End the REx, ignore is-block
2709 (vector 'indentable 'terminator p is-block))
2710 (is-block ; Indent w.r.t. preceding line
2711 (vector 'indentable 'cont-line char-after-pos
2712 is-block char-after p))
2713 (t ; No preceding line...
2714 (vector 'indentable 'first-line p))))
2715 ((get-text-property char-after-pos 'REx-part2)
2716 (vector 'REx-part2 (point)))
2717 ((nth 4 state)
2718 [comment])
2719 ((nth 3 state)
2720 [string])
2721 ;; XXXX Do we need to special-case this?
2722 ((null containing-sexp)
2723 ;; Line is at top level. May be data or function definition,
2724 ;; or may be function argument declaration.
2725 ;; Indent like the previous top level line
2726 ;; unless that ends in a closeparen without semicolon,
2727 ;; in which case this line is the first argument decl.
2728 (skip-chars-forward " \t")
2729 (cperl-backward-to-noncomment (or old-indent (point-min)))
2730 (setq state
2731 (or (bobp)
2732 (eq (point) old-indent) ; old-indent was at comment
2733 (eq (preceding-char) ?\;)
2734 ;; Had ?\) too
2735 (and (eq (preceding-char) ?\})
2736 (cperl-after-block-and-statement-beg
2737 (point-min))) ; Was start - too close
2738 (memq char-after (append ")]}" nil))
2739 (and (eq (preceding-char) ?\:) ; label
2740 (progn
2741 (forward-sexp -1)
2742 (skip-chars-backward " \t")
2743 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2744 (get-text-property (point) 'first-format-line)))
2746 ;; Look at previous line that's at column 0
2747 ;; to determine whether we are in top-level decls
2748 ;; or function's arg decls. Set basic-indent accordingly.
2749 ;; Now add a little if this is a continuation line.
2750 (and state
2751 parse-data
2752 (not (eq char-after ?\C-j))
2753 (setcdr (cddr parse-data)
2754 (list pre-indent-point)))
2755 (vector 'toplevel start char-after state (nth 2 s-s)))
2756 ((not
2757 (or (setq is-block
2758 (and (setq delim (= (char-after containing-sexp) ?{))
2759 (save-excursion ; Is it a hash?
2760 (goto-char containing-sexp)
2761 (cperl-block-p))))
2762 cperl-indent-parens-as-block))
2763 ;; group is an expression, not a block:
2764 ;; indent to just after the surrounding open parens,
2765 ;; skip blanks if we do not close the expression.
2766 (goto-char (1+ containing-sexp))
2767 (or (memq char-after
2768 (append (if delim "}" ")]}") nil))
2769 (looking-at "[ \t]*\\(#\\|$\\)")
2770 (skip-chars-forward " \t"))
2771 (setq old-indent (point)) ; delim=is-brace
2772 (vector 'in-parens char-after (point) delim containing-sexp))
2774 ;; Statement level. Is it a continuation or a new statement?
2775 ;; Find previous non-comment character.
2776 (goto-char pre-indent-point) ; Skip one level of POD/etc
2777 (cperl-backward-to-noncomment containing-sexp)
2778 ;; Back up over label lines, since they don't
2779 ;; affect whether our line is a continuation.
2780 ;; (Had \, too)
2781 (while;;(or (eq (preceding-char) ?\,)
2782 (and (eq (preceding-char) ?:)
2783 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2784 (memq (char-syntax (char-after (- (point) 2)))
2785 '(?w ?_))))
2787 ;; This is always FALSE?
2788 (if (eq (preceding-char) ?\,)
2789 ;; Will go to beginning of line, essentially.
2790 ;; Will ignore embedded sexpr XXXX.
2791 (cperl-backward-to-start-of-continued-exp containing-sexp))
2792 (beginning-of-line)
2793 (cperl-backward-to-noncomment containing-sexp))
2794 ;; Now we get non-label preceding the indent point
2795 (if (not (or (eq (1- (point)) containing-sexp)
2796 (and cperl-indent-parens-as-block
2797 (not is-block))
2798 (memq (preceding-char)
2799 (append (if is-block " ;{" " ,;{") '(nil)))
2800 (and (eq (preceding-char) ?\})
2801 (cperl-after-block-and-statement-beg
2802 containing-sexp))
2803 (get-text-property (point) 'first-format-line)))
2804 ;; This line is continuation of preceding line's statement;
2805 ;; indent `cperl-continued-statement-offset' more than the
2806 ;; previous line of the statement.
2808 ;; There might be a label on this line, just
2809 ;; consider it bad style and ignore it.
2810 (progn
2811 (cperl-backward-to-start-of-continued-exp containing-sexp)
2812 (vector 'continuation (point) char-after is-block delim))
2813 ;; This line starts a new statement.
2814 ;; Position following last unclosed open brace
2815 (goto-char containing-sexp)
2816 ;; Is line first statement after an open-brace?
2818 ;; If no, find that first statement and indent like
2819 ;; it. If the first statement begins with label, do
2820 ;; not believe when the indentation of the label is too
2821 ;; small.
2822 (save-excursion
2823 (forward-char 1)
2824 (let ((colon-line-end 0))
2825 (while
2826 (progn (skip-chars-forward " \t\n")
2827 ;; s: foo : bar :x is NOT label
2828 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2829 (not (looking-at "[sym]:\\|tr:"))))
2830 ;; Skip over comments and labels following openbrace.
2831 (cond ((= (following-char) ?\#)
2832 (forward-line 1))
2833 ((= (following-char) ?\=)
2834 (goto-char
2835 (or (next-single-property-change (point) 'in-pod)
2836 (point-max)))) ; do not loop if no syntaxification
2837 ;; label:
2839 (setq colon-line-end (point-at-eol))
2840 (search-forward ":"))))
2841 ;; We are at beginning of code (NOT label or comment)
2842 ;; First, the following code counts
2843 ;; if it is before the line we want to indent.
2844 (and (< (point) indent-point)
2845 (vector 'have-prev-sibling (point) colon-line-end
2846 containing-sexp))))
2847 (progn
2848 ;; If no previous statement,
2849 ;; indent it relative to line brace is on.
2851 ;; For open-braces not the first thing in a line,
2852 ;; add in cperl-brace-imaginary-offset.
2854 ;; If first thing on a line: ?????
2855 ;; Move back over whitespace before the openbrace.
2856 (setq ; brace first thing on a line
2857 old-indent (progn (skip-chars-backward " \t") (bolp)))
2858 ;; Should we indent w.r.t. earlier than start?
2859 ;; Move to start of control group, possibly on a different line
2860 (or cperl-indent-wrt-brace
2861 (cperl-backward-to-noncomment (point-min)))
2862 ;; If the openbrace is preceded by a parenthesized exp,
2863 ;; move to the beginning of that;
2864 (if (eq (preceding-char) ?\))
2865 (progn
2866 (forward-sexp -1)
2867 (cperl-backward-to-noncomment (point-min))))
2868 ;; In the case it starts a subroutine, indent with
2869 ;; respect to `sub', not with respect to the
2870 ;; first thing on the line, say in the case of
2871 ;; anonymous sub in a hash.
2872 (if (and;; Is it a sub in group starting on this line?
2873 cperl-indent-subs-specially
2874 (cond ((get-text-property (point) 'attrib-group)
2875 (goto-char (cperl-beginning-of-property
2876 (point) 'attrib-group)))
2877 ((eq (preceding-char) ?b)
2878 (forward-sexp -1)
2879 (looking-at (concat cperl-sub-regexp "\\>"))))
2880 (setq p (nth 1 ; start of innermost containing list
2881 (parse-partial-sexp
2882 (point-at-bol)
2883 (point)))))
2884 (progn
2885 (goto-char (1+ p)) ; enclosing block on the same line
2886 (skip-chars-forward " \t")
2887 (vector 'code-start-in-block containing-sexp char-after
2888 (and delim (not is-block)) ; is a HASH
2889 old-indent ; brace first thing on a line
2890 t (point) ; have something before...
2892 ;;(current-column)
2894 ;; Get initial indentation of the line we are on.
2895 ;; If line starts with label, calculate label indentation
2896 (vector 'code-start-in-block containing-sexp char-after
2897 (and delim (not is-block)) ; is a HASH
2898 old-indent ; brace first thing on a line
2899 nil (point))))))))))))))) ; nothing interesting before
2901 (defvar cperl-indent-rules-alist
2902 '((pod nil) ; via `syntax-type' property
2903 (here-doc nil) ; via `syntax-type' property
2904 (here-doc-delim nil) ; via `syntax-type' property
2905 (format nil) ; via `syntax-type' property
2906 (in-pod nil) ; via `in-pod' property
2907 (comment-special:at-beginning-of-line nil)
2908 (string t)
2909 (comment nil))
2910 "Alist of indentation rules for CPerl mode.
2911 The values mean:
2912 nil: do not indent;
2913 FUNCTION: a function to compute the indentation to use.
2914 Takes a single argument which provides the currently computed indentation
2915 context, and should return the column to which to indent.
2916 NUMBER: add this amount of indentation.")
2918 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
2919 "Return appropriate indentation for current line as Perl code.
2920 In usual case returns an integer: the column to indent to.
2921 Returns nil if line starts inside a string, t if in a comment.
2923 Will not correct the indentation for labels, but will correct it for braces
2924 and closing parentheses and brackets."
2925 ;; This code is still a broken architecture: in some cases we need to
2926 ;; compensate for some modifications which `cperl-indent-line' will add later
2927 (save-excursion
2928 (let ((i (cperl-sniff-for-indent parse-data)) what p)
2929 (cond
2930 ;;((or (null i) (eq i t) (numberp i))
2931 ;; i)
2932 ((vectorp i)
2933 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
2934 (cond
2935 (what
2936 (let ((action (cadr what)))
2937 (cond ((functionp action) (apply action (list i parse-data)))
2938 ((numberp action) (+ action (current-indentation)))
2939 (t action))))
2941 ;; Indenters for regular expressions with //x and qw()
2943 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
2944 (goto-char (elt i 1))
2945 (condition-case nil ; Use indentation of the 1st part
2946 (forward-sexp -1))
2947 (current-column))
2948 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
2949 (cond ;;; [indentable terminator start-pos is-block]
2950 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
2951 (goto-char (elt i 2)) ; After opening parens
2952 (1- (current-column)))
2953 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
2954 (goto-char (elt i 2))
2955 (+ (or cperl-regexp-indent-step cperl-indent-level)
2957 (current-column)))
2958 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
2959 ;; Indent as the level after closing parens
2960 (goto-char (elt i 2)) ; indent line
2961 (skip-chars-forward " \t)") ; Skip closing parens
2962 (setq p (point))
2963 (goto-char (elt i 3)) ; previous line
2964 (skip-chars-forward " \t)") ; Skip closing parens
2965 ;; Number of parens in between:
2966 (setq p (nth 0 (parse-partial-sexp (point) p))
2967 what (elt i 4)) ; First char on current line
2968 (goto-char (elt i 3)) ; previous line
2969 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
2970 (cond ((eq what ?\) )
2971 (- cperl-close-paren-offset)) ; compensate
2972 ((eq what ?\| )
2973 (- (or cperl-regexp-indent-step cperl-indent-level)))
2974 (t 0))
2975 (if (eq (following-char) ?\| )
2976 (or cperl-regexp-indent-step cperl-indent-level)
2978 (current-column)))
2980 (error "Unrecognized value of indent: %s" i))))
2982 ;; Indenter for stuff at toplevel
2984 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
2985 (+ (save-excursion ; To beg-of-defun, or end of last sexp
2986 (goto-char (elt i 1)) ; start = Good place to start parsing
2987 (- (current-indentation) ;
2988 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
2989 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
2990 ;; Look at previous line that's at column 0
2991 ;; to determine whether we are in top-level decls
2992 ;; or function's arg decls. Set basic-indent accordingly.
2993 ;; Now add a little if this is a continuation line.
2994 (if (elt i 3) ; state (XXX What is the semantic???)
2996 cperl-continued-statement-offset)))
2998 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3000 ((eq 'in-parens (elt i 0))
3001 ;; in-parens char-after old-indent-point is-brace containing-sexp
3003 ;; group is an expression, not a block:
3004 ;; indent to just after the surrounding open parens,
3005 ;; skip blanks if we do not close the expression.
3006 (+ (progn
3007 (goto-char (elt i 2)) ; old-indent-point
3008 (current-column))
3009 (if (and (elt i 3) ; is-brace
3010 (eq (elt i 1) ?\})) ; char-after
3011 ;; Correct indentation of trailing ?\}
3012 (+ cperl-indent-level cperl-close-paren-offset)
3013 0)))
3015 ;; Indenter for continuation lines
3017 ((eq 'continuation (elt i 0))
3018 ;; [continuation statement-start char-after is-block is-brace]
3019 (goto-char (elt i 1)) ; statement-start
3020 (+ (if (or (memq (elt i 2) (append "}])" nil)) ; char-after
3021 (eq 'continuation ; do not stagger continuations
3022 (elt (cperl-sniff-for-indent parse-data) 0)))
3023 0 ; Closing parenthesis or continuation of a continuation
3024 cperl-continued-statement-offset)
3025 (if (or (elt i 3) ; is-block
3026 (not (elt i 4)) ; is-brace
3027 (not (eq (elt i 2) ?\}))) ; char-after
3029 ;; Now it is a hash reference
3030 (+ cperl-indent-level cperl-close-paren-offset))
3031 ;; Labels do not take :: ...
3032 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3033 (if (> (current-indentation) cperl-min-label-indent)
3034 (- (current-indentation) cperl-label-offset)
3035 ;; Do not move `parse-data', this should
3036 ;; be quick anyway (this comment comes
3037 ;; from different location):
3038 (cperl-calculate-indent))
3039 (current-column))
3040 (if (eq (elt i 2) ?\{) ; char-after
3041 cperl-continued-brace-offset 0)))
3043 ;; Indenter for lines in a block which are not leading lines
3045 ((eq 'have-prev-sibling (elt i 0))
3046 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3047 (goto-char (elt i 1)) ; sibling-beg
3048 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
3049 (if (> (current-indentation)
3050 cperl-min-label-indent)
3051 (- (current-indentation) cperl-label-offset)
3052 ;; Do not believe: `max' was involved in calculation of indent
3053 (+ cperl-indent-level
3054 (save-excursion
3055 (goto-char (elt i 3)) ; block-start
3056 (current-indentation))))
3057 (current-column)))
3059 ;; Indenter for the first line in a block
3061 ((eq 'code-start-in-block (elt i 0))
3062 ;;[code-start-in-block before-brace char-after
3063 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3064 ;; group-starts-before-start-of-sub start-of-control-group]
3065 (goto-char (elt i 1))
3066 ;; For open brace in column zero, don't let statement
3067 ;; start there too. If cperl-indent-level=0,
3068 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3069 (+ (if (and (bolp) (zerop cperl-indent-level))
3070 (+ cperl-brace-offset cperl-continued-statement-offset)
3071 cperl-indent-level)
3072 (if (and (elt i 3) ; is-a-HASH-ref
3073 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3074 (+ cperl-indent-level cperl-close-paren-offset)
3076 ;; Unless openbrace is the first nonwhite thing on the line,
3077 ;; add the cperl-brace-imaginary-offset.
3078 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3079 cperl-brace-imaginary-offset)
3080 (progn
3081 (goto-char (elt i 6)) ; start-of-control-group
3082 (if (elt i 5) ; group-starts-before-start-of-sub
3083 (current-column)
3084 ;; Get initial indentation of the line we are on.
3085 ;; If line starts with label, calculate label indentation
3086 (if (save-excursion
3087 (beginning-of-line)
3088 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3089 (if (> (current-indentation) cperl-min-label-indent)
3090 (- (current-indentation) cperl-label-offset)
3091 ;; Do not move `parse-data', this should
3092 ;; be quick anyway:
3093 (cperl-calculate-indent))
3094 (current-indentation))))))
3096 (error "Unrecognized value of indent: %s" i))))
3098 (error "Got strange value of indent: %s" i))))))
3100 (defun cperl-calculate-indent-within-comment ()
3101 "Return the indentation amount for line, assuming that
3102 the current line is to be regarded as part of a block comment."
3103 (let (end)
3104 (save-excursion
3105 (beginning-of-line)
3106 (skip-chars-forward " \t")
3107 (setq end (point))
3108 (and (= (following-char) ?#)
3109 (forward-line -1)
3110 (cperl-to-comment-or-eol)
3111 (setq end (point)))
3112 (goto-char end)
3113 (current-column))))
3116 (defun cperl-to-comment-or-eol ()
3117 "Go to position before comment on the current line, or to end of line.
3118 Returns true if comment is found. In POD will not move the point."
3119 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3120 ;; then looks for literal # or end-of-line.
3121 (let (state stop-in cpoint (lim (point-at-eol)) pr e)
3122 (or cperl-font-locking
3123 (cperl-update-syntaxification lim lim))
3124 (beginning-of-line)
3125 (if (setq pr (get-text-property (point) 'syntax-type))
3126 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3127 (if (or (eq pr 'pod)
3128 (if (or (not e) (> e lim)) ; deep inside a group
3129 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3130 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3131 ;; Else - need to do it the hard way
3132 (and (and e (<= e lim))
3133 (goto-char e))
3134 (while (not stop-in)
3135 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3136 ; stop at comment
3137 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3138 (if (nth 4 state) ; After `#';
3139 ; (nth 2 state) can be
3140 ; beginning of m,s,qq and so
3141 ; on
3142 (if (nth 2 state)
3143 (progn
3144 (setq cpoint (point))
3145 (goto-char (nth 2 state))
3146 (cond
3147 ((looking-at "\\(s\\|tr\\)\\>")
3148 (or (re-search-forward
3149 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3150 lim 'move)
3151 (setq stop-in t)))
3152 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3153 (or (re-search-forward
3154 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3155 lim 'move)
3156 (setq stop-in t)))
3157 (t ; It was fair comment
3158 (setq stop-in t) ; Finish
3159 (goto-char (1- cpoint)))))
3160 (setq stop-in t) ; Finish
3161 (forward-char -1))
3162 (setq stop-in t))) ; Finish
3163 (nth 4 state))))
3165 (defsubst cperl-modify-syntax-type (at how)
3166 (if (< at (point-max))
3167 (progn
3168 (put-text-property at (1+ at) 'syntax-table how)
3169 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3171 (defun cperl-protect-defun-start (s e)
3172 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3173 (save-excursion
3174 (goto-char s)
3175 (while (re-search-forward "^\\s(" e 'to-end)
3176 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3178 (defun cperl-commentify (bb e string &optional noface)
3179 (if cperl-use-syntax-table-text-property
3180 (if (eq noface 'n) ; Only immediate
3182 ;; We suppose that e is _after_ the end of construction, as after eol.
3183 (setq string (if string cperl-st-sfence cperl-st-cfence))
3184 (if (> bb (- e 2))
3185 ;; one-char string/comment?!
3186 (cperl-modify-syntax-type bb cperl-st-punct)
3187 (cperl-modify-syntax-type bb string)
3188 (cperl-modify-syntax-type (1- e) string))
3189 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3190 (put-text-property (1+ bb) (1- e)
3191 'syntax-table cperl-string-syntax-table))
3192 (cperl-protect-defun-start bb e))
3193 ;; Fontify
3194 (or noface
3195 (not cperl-pod-here-fontify)
3196 (put-text-property bb e 'face (if string 'font-lock-string-face
3197 'font-lock-comment-face)))))
3199 (defvar cperl-starters '(( ?\( . ?\) )
3200 ( ?\[ . ?\] )
3201 ( ?\{ . ?\} )
3202 ( ?\< . ?\> )))
3204 (defun cperl-cached-syntax-table (st)
3205 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3206 All the entries of the syntax table are \".\", except for a backslash, which
3207 is quoting."
3208 (if (car-safe st)
3209 (car st)
3210 (setcar st (make-syntax-table))
3211 (setq st (car st))
3212 (let ((i 0))
3213 (while (< i 256)
3214 (modify-syntax-entry i "." st)
3215 (setq i (1+ i))))
3216 (modify-syntax-entry ?\\ "\\" st)
3217 st))
3219 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3220 &optional ostart oend)
3221 "Find the end of a regular expression or a stringish construct (q[] etc).
3222 The point should be before the starting delimiter.
3224 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3225 is s/// or tr/// like expression. If END is nil, generates an error
3226 message if needed. If SET-ST is non-nil, will use (or generate) a
3227 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3228 error message in its CAR (unless it already contains some error
3229 message). ARGUMENT should be the name of the construct (used in error
3230 messages). OSTART, OEND may be set in recursive calls when processing
3231 the second argument of 2ARG construct.
3233 Works *before* syntax recognition is done. In IS-2ARG situation may
3234 modify syntax-type text property if the situation is too hard."
3235 (let (b starter ender st i i2 go-forward reset-st set-st)
3236 (skip-chars-forward " \t")
3237 ;; ender means matching-char matcher.
3238 (setq b (point)
3239 starter (if (eobp) 0 (char-after b))
3240 ender (cdr (assoc starter cperl-starters)))
3241 ;; What if starter == ?\\ ????
3242 (setq st (cperl-cached-syntax-table st-l))
3243 (setq set-st t)
3244 ;; Whether we have an intermediate point
3245 (setq i nil)
3246 ;; Prepare the syntax table:
3247 (if (not ender) ; m/blah/, s/x//, s/x/y/
3248 (modify-syntax-entry starter "$" st)
3249 (modify-syntax-entry starter (concat "(" (list ender)) st)
3250 (modify-syntax-entry ender (concat ")" (list starter)) st))
3251 (condition-case bb
3252 (progn
3253 ;; We use `$' syntax class to find matching stuff, but $$
3254 ;; is recognized the same as $, so we need to check this manually.
3255 (if (and (eq starter (char-after (cperl-1+ b)))
3256 (not ender))
3257 ;; $ has TeXish matching rules, so $$ equiv $...
3258 (forward-char 2)
3259 (setq reset-st (syntax-table))
3260 (set-syntax-table st)
3261 (forward-sexp 1)
3262 (if (<= (point) (1+ b))
3263 (error "Unfinished regular expression"))
3264 (set-syntax-table reset-st)
3265 (setq reset-st nil)
3266 ;; Now the problem is with m;blah;;
3267 (and (not ender)
3268 (eq (preceding-char)
3269 (char-after (- (point) 2)))
3270 (save-excursion
3271 (forward-char -2)
3272 (= 0 (% (skip-chars-backward "\\\\") 2)))
3273 (forward-char -1)))
3274 ;; Now we are after the first part.
3275 (and is-2arg ; Have trailing part
3276 (not ender)
3277 (eq (following-char) starter) ; Empty trailing part
3278 (progn
3279 (or (eq (char-syntax (following-char)) ?.)
3280 ;; Make trailing letter into punctuation
3281 (cperl-modify-syntax-type (point) cperl-st-punct))
3282 (setq is-2arg nil go-forward t))) ; Ignore the tail
3283 (if is-2arg ; Not number => have second part
3284 (progn
3285 (setq i (point) i2 i)
3286 (if ender
3287 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3288 (progn
3289 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3290 (goto-char (match-end 0))
3291 (skip-chars-forward " \t\n\f"))
3292 (setq i2 (point))))
3293 (forward-char -1))
3294 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3295 (if ender (modify-syntax-entry ender "." st))
3296 (setq set-st nil)
3297 (setq ender (cperl-forward-re lim end nil st-l err-l
3298 argument starter ender)
3299 ender (nth 2 ender)))))
3300 (error (goto-char lim)
3301 (setq set-st nil)
3302 (if reset-st
3303 (set-syntax-table reset-st))
3304 (or end
3305 (and cperl-brace-recursing
3306 (or (eq ostart ?\{)
3307 (eq starter ?\{)))
3308 (message
3309 "End of `%s%s%c ... %c' string/RE not found: %s"
3310 argument
3311 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3312 starter (or ender starter) bb)
3313 (or (car err-l) (setcar err-l b)))))
3314 (if set-st
3315 (progn
3316 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3317 (if ender (modify-syntax-entry ender "." st))))
3318 ;; i: have 2 args, after end of the first arg
3319 ;; i2: start of the second arg, if any (before delim if `ender').
3320 ;; ender: the last arg bounded by parens-like chars, the second one of them
3321 ;; starter: the starting delimiter of the first arg
3322 ;; go-forward: has 2 args, and the second part is empty
3323 (list i i2 ender starter go-forward)))
3325 (defun cperl-forward-group-in-re (&optional st-l)
3326 "Find the end of a group in a REx.
3327 Return the error message (if any). Does not work if delimiter is `)'.
3328 Works before syntax recognition is done."
3329 ;; Works *before* syntax recognition is done
3330 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3331 (let (st b reset-st)
3332 (condition-case b
3333 (progn
3334 (setq st (cperl-cached-syntax-table st-l))
3335 (modify-syntax-entry ?\( "()" st)
3336 (modify-syntax-entry ?\) ")(" st)
3337 (setq reset-st (syntax-table))
3338 (set-syntax-table st)
3339 (forward-sexp 1))
3340 (error (message
3341 "cperl-forward-group-in-re: error %s" b)))
3342 ;; now restore the initial state
3343 (if st
3344 (progn
3345 (modify-syntax-entry ?\( "." st)
3346 (modify-syntax-entry ?\) "." st)))
3347 (if reset-st
3348 (set-syntax-table reset-st))
3352 (defvar font-lock-string-face)
3353 ;;(defvar font-lock-reference-face)
3354 (defvar font-lock-constant-face)
3355 (defsubst cperl-postpone-fontification (b e type val &optional now)
3356 ;; Do after syntactic fontification?
3357 (if cperl-syntaxify-by-font-lock
3358 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3359 (put-text-property b e type val)))
3361 ;; Here is how the global structures (those which cannot be
3362 ;; recognized locally) are marked:
3363 ;; a) PODs:
3364 ;; Start-to-end is marked `in-pod' ==> t
3365 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3366 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3367 ;; b) HEREs:
3368 ;; Start-to-end is marked `here-doc-group' ==> t
3369 ;; The body is marked `syntax-type' ==> `here-doc'
3370 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3371 ;; c) FORMATs:
3372 ;; First line (to =) marked `first-format-line' ==> t
3373 ;; After-this--to-end is marked `syntax-type' ==> `format'
3374 ;; d) 'Q'uoted string:
3375 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3376 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3377 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3378 ;; e) Attributes of subroutines: `attrib-group' ==> t
3379 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3380 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3382 ;; In addition, some parts of RExes may be marked as `REx-interpolated'
3383 ;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3385 (defun cperl-unwind-to-safe (before &optional end)
3386 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3387 (let ((pos (point)))
3388 (while (and pos (progn
3389 (beginning-of-line)
3390 (get-text-property (setq pos (point)) 'syntax-type)))
3391 (setq pos (cperl-beginning-of-property pos 'syntax-type))
3392 (if (eq pos (point-min))
3393 (setq pos nil))
3394 (if pos
3395 (if before
3396 (progn
3397 (goto-char (cperl-1- pos))
3398 (beginning-of-line)
3399 (setq pos (point)))
3400 (goto-char (setq pos (cperl-1- pos))))
3401 ;; Up to the start
3402 (goto-char (point-min))))
3403 ;; Skip empty lines
3404 (and (looking-at "\n*=")
3405 (/= 0 (skip-chars-backward "\n"))
3406 (forward-char))
3407 (setq pos (point))
3408 (if end
3409 ;; Do the same for end, going small steps
3410 (save-excursion
3411 (while (and end (< end (point-max))
3412 (get-text-property end 'syntax-type))
3413 (setq pos end
3414 end (next-single-property-change end 'syntax-type nil (point-max)))
3415 (if end (progn (goto-char end)
3416 (or (bolp) (forward-line 1))
3417 (setq end (point)))))
3418 (or end pos)))))
3420 ;; These are needed for byte-compile (at least with v19)
3421 (defvar cperl-nonoverridable-face)
3422 (defvar font-lock-variable-name-face)
3423 (defvar font-lock-function-name-face)
3424 (defvar font-lock-keyword-face)
3425 (defvar font-lock-builtin-face)
3426 (defvar font-lock-type-face)
3427 (defvar font-lock-comment-face)
3428 (defvar font-lock-warning-face)
3430 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3431 "Syntactically mark (and fontify) attributes of a subroutine.
3432 Should be called with the point before leading colon of an attribute."
3433 ;; Works *before* syntax recognition is done
3434 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3435 (let (st p reset-st after-first (start (point)) start1 end1)
3436 (condition-case b
3437 (while (looking-at
3438 (concat
3439 "\\(" ; 1=optional? colon
3440 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3441 "\\)"
3442 (if after-first "?" "")
3443 ;; No space between name and paren allowed...
3444 "\\(\\sw+\\)" ; 3=name
3445 "\\((\\)?")) ; 4=optional paren
3446 (and (match-beginning 1)
3447 (cperl-postpone-fontification
3448 (match-beginning 0) (cperl-1+ (match-beginning 0))
3449 'face font-lock-constant-face))
3450 (setq start1 (match-beginning 3) end1 (match-end 3))
3451 (cperl-postpone-fontification start1 end1
3452 'face font-lock-constant-face)
3453 (goto-char end1) ; end or before `('
3454 (if (match-end 4) ; Have attribute arguments...
3455 (progn
3456 (if st nil
3457 (setq st (cperl-cached-syntax-table st-l))
3458 (modify-syntax-entry ?\( "()" st)
3459 (modify-syntax-entry ?\) ")(" st))
3460 (setq reset-st (syntax-table) p (point))
3461 (set-syntax-table st)
3462 (forward-sexp 1)
3463 (set-syntax-table reset-st)
3464 (setq reset-st nil)
3465 (cperl-commentify p (point) t))) ; mark as string
3466 (forward-comment (buffer-size))
3467 (setq after-first t))
3468 (error (message
3469 "L%d: attribute `%s': %s"
3470 (count-lines (point-min) (point))
3471 (and start1 end1 (buffer-substring start1 end1)) b)
3472 (setq start nil)))
3473 (and start
3474 (progn
3475 (put-text-property start (point)
3476 'attrib-group (if (looking-at "{") t 0))
3477 (and pos
3478 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3479 ;; Apparently, we do not need `multiline': faces added now
3480 (put-text-property (+ 3 pos) (cperl-1+ (point))
3481 'syntax-type 'sub-decl))
3482 (and b-fname ; Fontify here: the following condition
3483 (cperl-postpone-fontification ; is too hard to determine by
3484 b-fname e-fname 'face ; a REx, so do it here
3485 (if (looking-at "{")
3486 font-lock-function-name-face
3487 font-lock-variable-name-face)))))
3488 ;; now restore the initial state
3489 (if st
3490 (progn
3491 (modify-syntax-entry ?\( "." st)
3492 (modify-syntax-entry ?\) "." st)))
3493 (if reset-st
3494 (set-syntax-table reset-st))))
3496 (defsubst cperl-look-at-leading-count (is-x-REx e)
3497 (if (and
3498 (< (point) e)
3499 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3500 (1- e) t)) ; return nil on failure, no moving
3501 (if (eq ?\{ (preceding-char)) nil
3502 (cperl-postpone-fontification
3503 (1- (point)) (point)
3504 'face font-lock-warning-face))))
3506 ;; Do some smarter-highlighting
3507 ;; XXXX Currently ignores alphanum/dash delims,
3508 (defsubst cperl-highlight-charclass (endbracket dashface bsface onec-space)
3509 (let ((l '(1 5 7)) ll lle lll
3510 ;; 2 groups, the first takes the whole match (include \[trnfabe])
3511 (singleChar (concat "\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)")))
3512 (while ; look for unescaped - between non-classes
3513 (re-search-forward
3514 ;; On 19.33, certain simplifications lead
3515 ;; to bugs (as in [^a-z] \\| [trnfabe] )
3516 (concat ; 1: SingleChar (include \[trnfabe])
3517 singleChar
3518 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3519 "\\(" ; 3: DASH SingleChar (match optionally)
3520 "\\(-\\)" ; 4: DASH
3521 singleChar ; 5: SingleChar
3522 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3523 "\\)?"
3524 "\\|"
3525 "\\(" ; 7: other escapes
3526 "\\\\[pP]" "\\([^{]\\|{[^{}]*}\\)"
3527 "\\|" "\\\\[^pP]" "\\)"
3529 endbracket 'toend)
3530 (if (match-beginning 4)
3531 (cperl-postpone-fontification
3532 (match-beginning 4) (match-end 4)
3533 'face dashface))
3534 ;; save match data (for looking-at)
3535 (setq lll (mapcar (function (lambda (elt) (cons (match-beginning elt)
3536 (match-end elt))))
3538 (while lll
3539 (setq ll (car lll))
3540 (setq lle (cdr ll)
3541 ll (car ll))
3542 ;; (message "Got %s of %s" ll l)
3543 (if (and ll (eq (char-after ll) ?\\ ))
3544 (save-excursion
3545 (goto-char ll)
3546 (cperl-postpone-fontification ll (1+ ll)
3547 'face bsface)
3548 (if (looking-at "\\\\[a-zA-Z0-9]")
3549 (cperl-postpone-fontification (1+ ll) lle
3550 'face onec-space))))
3551 (setq lll (cdr lll))))
3552 (goto-char endbracket) ; just in case something misbehaves???
3555 ;; Debugging this may require (setq max-specpdl-size 2000)...
3556 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3557 "Scans the buffer for hard-to-parse Perl constructions.
3558 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3559 the sections using `cperl-pod-head-face', `cperl-pod-face',
3560 `cperl-here-face'."
3561 (interactive)
3562 (or min (setq min (point-min)
3563 cperl-syntax-state nil
3564 cperl-syntax-done-to min))
3565 (or max (setq max (point-max)))
3566 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3567 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3568 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3569 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3570 (modified (buffer-modified-p)) overshoot is-o-REx name
3571 (inhibit-modification-hooks t)
3572 (cperl-font-locking t)
3573 (use-syntax-state (and cperl-syntax-state
3574 (>= min (car cperl-syntax-state))))
3575 (state-point (if use-syntax-state
3576 (car cperl-syntax-state)
3577 (point-min)))
3578 (state (if use-syntax-state
3579 (cdr cperl-syntax-state)))
3580 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3581 (st-l (list nil)) (err-l (list nil))
3582 ;; Somehow font-lock may be not loaded yet...
3583 ;; (e.g., when building TAGS via command-line call)
3584 (font-lock-string-face (if (boundp 'font-lock-string-face)
3585 font-lock-string-face
3586 'font-lock-string-face))
3587 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3588 font-lock-constant-face
3589 'font-lock-constant-face))
3590 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3591 (if (boundp 'font-lock-function-name-face)
3592 font-lock-function-name-face
3593 'font-lock-function-name-face))
3594 (font-lock-variable-name-face ; interpolated vars and ({})-code
3595 (if (boundp 'font-lock-variable-name-face)
3596 font-lock-variable-name-face
3597 'font-lock-variable-name-face))
3598 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3599 (if (boundp 'font-lock-function-name-face)
3600 font-lock-function-name-face
3601 'font-lock-function-name-face))
3602 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3603 (if (boundp 'font-lock-constant-face)
3604 font-lock-constant-face
3605 'font-lock-constant-face))
3606 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3607 (if (boundp 'font-lock-builtin-face)
3608 font-lock-builtin-face
3609 'font-lock-builtin-face))
3610 (font-lock-comment-face
3611 (if (boundp 'font-lock-comment-face)
3612 font-lock-comment-face
3613 'font-lock-comment-face))
3614 (font-lock-warning-face
3615 (if (boundp 'font-lock-warning-face)
3616 font-lock-warning-face
3617 'font-lock-warning-face))
3618 (my-cperl-REx-ctl-face ; (|)
3619 (if (boundp 'font-lock-keyword-face)
3620 font-lock-keyword-face
3621 'font-lock-keyword-face))
3622 (my-cperl-REx-modifiers-face ; //gims
3623 (if (boundp 'cperl-nonoverridable-face)
3624 cperl-nonoverridable-face
3625 'cperl-nonoverridable-face))
3626 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3627 (if (boundp 'font-lock-type-face)
3628 font-lock-type-face
3629 'font-lock-type-face))
3630 (stop-point (if ignore-max
3631 (point-max)
3632 max))
3633 (search
3634 (concat
3635 "\\(\\`\n?\\|^\n\\)=" ; POD
3636 "\\|"
3637 ;; One extra () before this:
3638 "<<~?" ; HERE-DOC
3639 "\\(" ; 1 + 1
3640 ;; First variant "BLAH" or just ``.
3641 "[ \t]*" ; Yes, whitespace is allowed!
3642 "\\([\"'`]\\)" ; 2 + 1 = 3
3643 "\\([^\"'`\n]*\\)" ; 3 + 1
3644 "\\3"
3645 "\\|"
3646 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3647 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3648 ;; Do not have <<= or << 30 or <<30 or << $blah.
3649 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3650 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3651 "\\)"
3652 "\\|"
3653 ;; 1+6 extra () before this:
3654 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3655 (if cperl-use-syntax-table-text-property
3656 (concat
3657 "\\|"
3658 ;; 1+6+2=9 extra () before this:
3659 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3660 "\\|"
3661 ;; 1+6+2+1=10 extra () before this:
3662 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3663 "\\|"
3664 ;; 1+6+2+1+1=11 extra () before this
3665 "\\<" cperl-sub-regexp "\\>" ; sub with proto/attr
3666 "\\("
3667 cperl-white-and-comment-rex
3668 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3669 "\\("
3670 cperl-maybe-white-and-comment-rex
3671 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3672 "\\|"
3673 ;; 1+6+2+1+1+6=17 extra () before this:
3674 "\\$\\(['{]\\)" ; $' or ${foo}
3675 "\\|"
3676 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3677 ;; we do not support intervening comments...):
3678 "\\(\\<" cperl-sub-regexp "[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3679 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3680 "\\|"
3681 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3682 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3683 "\\|"
3684 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3685 ""))))
3686 (unwind-protect
3687 (progn
3688 (save-excursion
3689 (or non-inter
3690 (message "Scanning for \"hard\" Perl constructions..."))
3691 ;;(message "find: %s --> %s" min max)
3692 (and cperl-pod-here-fontify
3693 ;; We had evals here, do not know why...
3694 (setq face cperl-pod-face
3695 head-face cperl-pod-head-face
3696 here-face cperl-here-face))
3697 (remove-text-properties min max
3698 '(syntax-type t in-pod t syntax-table t
3699 attrib-group t
3700 REx-interpolated t
3701 cperl-postpone t
3702 syntax-subtype t
3703 rear-nonsticky t
3704 front-sticky t
3705 here-doc-group t
3706 first-format-line t
3707 REx-part2 t
3708 indentable t))
3709 ;; Need to remove face as well...
3710 (goto-char min)
3711 ;; 'emx not supported by Emacs since at least 21.1.
3712 (and (featurep 'xemacs) (eq system-type 'emx)
3713 (eq (point) 1)
3714 (let ((case-fold-search t))
3715 (looking-at "extproc[ \t]")) ; Analogue of #!
3716 (cperl-commentify min
3717 (point-at-eol)
3718 nil))
3719 (while (and
3720 (< (point) max)
3721 (re-search-forward search max t))
3722 (setq tmpend nil) ; Valid for most cases
3723 (setq b (match-beginning 0)
3724 state (save-excursion (parse-partial-sexp
3725 state-point b nil nil state))
3726 state-point b)
3727 (cond
3728 ;; 1+6+2+1+1+6=17 extra () before this:
3729 ;; "\\$\\(['{]\\)"
3730 ((match-beginning 18) ; $' or ${foo}
3731 (if (eq (preceding-char) ?\') ; $'
3732 (progn
3733 (setq b (1- (point))
3734 state (parse-partial-sexp
3735 state-point (1- b) nil nil state)
3736 state-point (1- b))
3737 (if (nth 3 state) ; in string
3738 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3739 (goto-char (1+ b)))
3740 ;; else: ${
3741 (setq bb (match-beginning 0))
3742 (cperl-modify-syntax-type bb cperl-st-punct)))
3743 ;; No processing in strings/comments beyond this point:
3744 ((or (nth 3 state) (nth 4 state))
3745 t) ; Do nothing in comment/string
3746 ((match-beginning 1) ; POD section
3747 ;; "\\(\\`\n?\\|^\n\\)="
3748 (setq b (match-beginning 0)
3749 state (parse-partial-sexp
3750 state-point b nil nil state)
3751 state-point b)
3752 (if (or (nth 3 state) (nth 4 state)
3753 (looking-at "\\(cut\\|\\end\\)\\>"))
3754 (if (or (nth 3 state) (nth 4 state) ignore-max)
3755 nil ; Doing a chunk only
3756 (message "=cut is not preceded by a POD section")
3757 (or (car err-l) (setcar err-l (point))))
3758 (beginning-of-line)
3760 (setq b (point)
3761 bb b
3762 tb (match-beginning 0)
3763 b1 nil) ; error condition
3764 ;; We do not search to max, since we may be called from
3765 ;; some hook of fontification, and max is random
3766 (or (re-search-forward "^\n=\\(cut\\|\\end\\)\\>" stop-point 'toend)
3767 (progn
3768 (goto-char b)
3769 (if (re-search-forward "\n=\\(cut\\|\\end\\)\\>" stop-point 'toend)
3770 (progn
3771 (message "=cut is not preceded by an empty line")
3772 (setq b1 t)
3773 (or (car err-l) (setcar err-l b))))))
3774 (beginning-of-line 2) ; An empty line after =cut is not POD!
3775 (setq e (point))
3776 (and (> e max)
3777 (progn
3778 (remove-text-properties
3779 max e '(syntax-type t in-pod t syntax-table t
3780 attrib-group t
3781 REx-interpolated t
3782 cperl-postpone t
3783 syntax-subtype t
3784 here-doc-group t
3785 rear-nonsticky t
3786 front-sticky t
3787 first-format-line t
3788 REx-part2 t
3789 indentable t))
3790 (setq tmpend tb)))
3791 (put-text-property b e 'in-pod t)
3792 (put-text-property b e 'syntax-type 'in-pod)
3793 (goto-char b)
3794 (while (re-search-forward "\n\n[ \t]" e t)
3795 ;; We start 'pod 1 char earlier to include the preceding line
3796 (beginning-of-line)
3797 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3798 (cperl-put-do-not-fontify b (point) t)
3799 ;; mark the non-literal parts as PODs
3800 (if cperl-pod-here-fontify
3801 (cperl-postpone-fontification b (point) 'face face t))
3802 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3803 (beginning-of-line)
3804 (setq b (point)))
3805 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3806 (cperl-put-do-not-fontify (point) e t)
3807 (if cperl-pod-here-fontify
3808 (progn
3809 ;; mark the non-literal parts as PODs
3810 (cperl-postpone-fontification (point) e 'face face t)
3811 (goto-char bb)
3812 (if (looking-at
3813 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3814 ;; mark the headers
3815 (cperl-postpone-fontification
3816 (match-beginning 1) (match-end 1)
3817 'face head-face))
3818 (while (re-search-forward
3819 ;; One paragraph
3820 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3821 e 'toend)
3822 ;; mark the headers
3823 (cperl-postpone-fontification
3824 (match-beginning 1) (match-end 1)
3825 'face head-face))))
3826 (cperl-commentify bb e nil)
3827 (goto-char e)
3828 (or (eq e (point-max))
3829 (forward-char -1)))) ; Prepare for immediate POD start.
3830 ;; Here document
3831 ;; We can do many here-per-line;
3832 ;; but multiline quote on the same line as <<HERE confuses us...
3833 ;; ;; One extra () before this:
3834 ;;"<<"
3835 ;; "\\(" ; 1 + 1
3836 ;; ;; First variant "BLAH" or just ``.
3837 ;; "[ \t]*" ; Yes, whitespace is allowed!
3838 ;; "\\([\"'`]\\)" ; 2 + 1
3839 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3840 ;; "\\3"
3841 ;; "\\|"
3842 ;; ;; Second variant: Identifier or \ID or empty
3843 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3844 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3845 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3846 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3847 ;; "\\)"
3848 ((match-beginning 2) ; 1 + 1
3849 (setq b (point)
3850 tb (match-beginning 0)
3851 c (and ; not HERE-DOC
3852 (match-beginning 5)
3853 (save-match-data
3854 (or (looking-at "[ \t]*(") ; << function_call()
3855 (save-excursion ; 1 << func_name, or $foo << 10
3856 (condition-case nil
3857 (progn
3858 (goto-char tb)
3859 ;;; XXX What to do: foo <<bar ???
3860 ;;; XXX Need to support print {a} <<B ???
3861 (forward-sexp -1)
3862 (save-match-data
3863 ; $foo << b; $f .= <<B;
3864 ; ($f+1) << b; a($f) . <<B;
3865 ; foo 1, <<B; $x{a} <<b;
3866 (cond
3867 ((looking-at "[0-9$({]")
3868 (forward-sexp 1)
3869 (and
3870 (looking-at "[ \t]*<<")
3871 (condition-case nil
3872 ;; print $foo <<EOF
3873 (progn
3874 (forward-sexp -2)
3875 (not
3876 (looking-at "\\(printf?\\|say\\|system\\|exec\\|sort\\)\\>")))
3877 (error t)))))))
3878 (error nil))) ; func(<<EOF)
3879 (and (not (match-beginning 6)) ; Empty
3880 (looking-at
3881 "[ \t]*[=0-9$@%&(]"))))))
3882 (if c ; Not here-doc
3883 nil ; Skip it.
3884 (setq c (match-end 2)) ; 1 + 1
3885 (if (match-beginning 5) ;4 + 1
3886 (setq b1 (match-beginning 5) ; 4 + 1
3887 e1 (match-end 5)) ; 4 + 1
3888 (setq b1 (match-beginning 4) ; 3 + 1
3889 e1 (match-end 4))) ; 3 + 1
3890 (setq tag (buffer-substring b1 e1)
3891 qtag (regexp-quote tag))
3892 (cond (cperl-pod-here-fontify
3893 ;; Highlight the starting delimiter
3894 (cperl-postpone-fontification
3895 b1 e1 'face my-cperl-delimiters-face)
3896 (cperl-put-do-not-fontify b1 e1 t)))
3897 (forward-line)
3898 (setq i (point))
3899 (if end-of-here-doc
3900 (goto-char end-of-here-doc))
3901 (setq b (point))
3902 ;; We do not search to max, since we may be called from
3903 ;; some hook of fontification, and max is random
3904 (or (and (re-search-forward (concat "^[ \t]*" qtag "$")
3905 stop-point 'toend)
3906 ;;;(eq (following-char) ?\n) ; XXXX WHY???
3908 (progn ; Pretend we matched at the end
3909 (goto-char (point-max))
3910 (re-search-forward "\\'")
3911 (message "End of here-document `%s' not found." tag)
3912 (or (car err-l) (setcar err-l b))))
3913 (if cperl-pod-here-fontify
3914 (progn
3915 ;; Highlight the ending delimiter
3916 (cperl-postpone-fontification
3917 (match-beginning 0) (match-end 0)
3918 'face my-cperl-delimiters-face)
3919 (cperl-put-do-not-fontify b (match-end 0) t)
3920 ;; Highlight the HERE-DOC
3921 (cperl-postpone-fontification b (match-beginning 0)
3922 'face here-face)))
3923 (setq e1 (cperl-1+ (match-end 0)))
3924 (put-text-property b (match-beginning 0)
3925 'syntax-type 'here-doc)
3926 (put-text-property (match-beginning 0) e1
3927 'syntax-type 'here-doc-delim)
3928 (put-text-property b e1 'here-doc-group t)
3929 ;; This makes insertion at the start of HERE-DOC update
3930 ;; the whole construct:
3931 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
3932 (cperl-commentify b e1 nil)
3933 (cperl-put-do-not-fontify b (match-end 0) t)
3934 ;; Cache the syntax info...
3935 (setq cperl-syntax-state (cons state-point state))
3936 ;; ... and process the rest of the line...
3937 (setq overshoot
3938 (elt ; non-inter ignore-max
3939 (cperl-find-pods-heres c i t end t e1) 1))
3940 (if (and overshoot (> overshoot (point)))
3941 (goto-char overshoot)
3942 (setq overshoot e1))
3943 (if (> e1 max)
3944 (setq tmpend tb))))
3945 ;; format
3946 ((match-beginning 8)
3947 ;; 1+6=7 extra () before this:
3948 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3949 (setq b (point)
3950 name (if (match-beginning 8) ; 7 + 1
3951 (buffer-substring (match-beginning 8) ; 7 + 1
3952 (match-end 8)) ; 7 + 1
3954 tb (match-beginning 0))
3955 (setq argument nil)
3956 (put-text-property (point-at-bol) b 'first-format-line 't)
3957 (if cperl-pod-here-fontify
3958 (while (and (eq (forward-line) 0)
3959 (not (looking-at "^[.;]$")))
3960 (cond
3961 ((looking-at "^#")) ; Skip comments
3962 ((and argument ; Skip argument multi-lines
3963 (looking-at "^[ \t]*{"))
3964 (forward-sexp 1)
3965 (setq argument nil))
3966 (argument ; Skip argument lines
3967 (setq argument nil))
3968 (t ; Format line
3969 (setq b1 (point))
3970 (setq argument (looking-at "^[^\n]*[@^]"))
3971 (end-of-line)
3972 ;; Highlight the format line
3973 (cperl-postpone-fontification b1 (point)
3974 'face font-lock-string-face)
3975 (cperl-commentify b1 (point) nil)
3976 (cperl-put-do-not-fontify b1 (point) t))))
3977 ;; We do not search to max, since we may be called from
3978 ;; some hook of fontification, and max is random
3979 (re-search-forward "^[.;]$" stop-point 'toend))
3980 (beginning-of-line)
3981 (if (looking-at "^\\.$") ; ";" is not supported yet
3982 (progn
3983 ;; Highlight the ending delimiter
3984 (cperl-postpone-fontification (point) (+ (point) 2)
3985 'face font-lock-string-face)
3986 (cperl-commentify (point) (+ (point) 2) nil)
3987 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
3988 (message "End of format `%s' not found." name)
3989 (or (car err-l) (setcar err-l b)))
3990 (forward-line)
3991 (if (> (point) max)
3992 (setq tmpend tb))
3993 (put-text-property b (point) 'syntax-type 'format))
3994 ;; qq-like String or Regexp:
3995 ((or (match-beginning 10) (match-beginning 11))
3996 ;; 1+6+2=9 extra () before this:
3997 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
3998 ;; "\\|"
3999 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4000 (setq b1 (if (match-beginning 10) 10 11)
4001 argument (buffer-substring
4002 (match-beginning b1) (match-end b1))
4003 b (point) ; end of qq etc
4005 c (char-after (match-beginning b1))
4006 bb (char-after (1- (match-beginning b1))) ; tmp holder
4007 ;; bb == "Not a stringy"
4008 bb (if (eq b1 10) ; user variables/whatever
4009 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4010 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4011 ((eq bb ?\:) ; $opt::s
4012 (eq (char-after
4013 (- (match-beginning b1) 2))
4014 ?\:))
4015 ((eq bb ?\>) ; $foo->s
4016 (eq (char-after
4017 (- (match-beginning b1) 2))
4018 ?\-))
4019 ((eq bb ?\&)
4020 (not (eq (char-after ; &&m/blah/
4021 (- (match-beginning b1) 2))
4022 ?\&)))
4023 (t t)))
4024 ;; <file> or <$file>
4025 (and (eq c ?\<)
4026 ;; Do not stringify <FH>, <$fh> :
4027 (save-match-data
4028 (looking-at
4029 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4030 tb (match-beginning 0))
4031 (goto-char (match-beginning b1))
4032 (cperl-backward-to-noncomment (point-min))
4033 (or bb
4034 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4035 (setq argument ""
4036 b1 nil
4037 bb ; Not a regexp?
4038 (not
4039 ;; What is below: regexp-p?
4040 (and
4041 (or (memq (preceding-char)
4042 (append (if (memq c '(?\? ?\<))
4043 ;; $a++ ? 1 : 2
4044 "~{(=|&*!,;:["
4045 "~{(=|&+-*!,;:[") nil))
4046 (and (eq (preceding-char) ?\})
4047 (cperl-after-block-p (point-min)))
4048 (and (eq (char-syntax (preceding-char)) ?w)
4049 (progn
4050 (forward-sexp -1)
4051 ;; After these keywords `/' starts a RE. One should add all the
4052 ;; functions/builtins which expect an argument, but ...
4053 (if (eq (preceding-char) ?-)
4054 ;; -d ?foo? is a RE
4055 (looking-at "[a-zA-Z]\\>")
4056 (and
4057 (not (memq (preceding-char)
4058 '(?$ ?@ ?& ?%)))
4059 (looking-at
4060 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\|say\\)\\>")))))
4061 (and (eq (preceding-char) ?.)
4062 (eq (char-after (- (point) 2)) ?.))
4063 (bobp))
4064 ;; m|blah| ? foo : bar;
4065 (not
4066 (and (eq c ?\?)
4067 cperl-use-syntax-table-text-property
4068 (not (bobp))
4069 (progn
4070 (forward-char -1)
4071 (looking-at "\\s|"))))))
4072 b (1- b))
4073 ;; s y tr m
4074 ;; Check for $a -> y
4075 (setq b1 (preceding-char)
4076 go (point))
4077 (if (and (eq b1 ?>)
4078 (eq (char-after (- go 2)) ?-))
4079 ;; Not a regexp
4080 (setq bb t))))
4081 (or bb
4082 (progn
4083 (goto-char b)
4084 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4085 (goto-char (match-end 0))
4086 (skip-chars-forward " \t\n\f"))
4087 (cond ((and (eq (following-char) ?\})
4088 (eq b1 ?\{))
4089 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4090 (goto-char (1- go))
4091 (skip-chars-backward " \t\n\f")
4092 (if (memq (preceding-char) (append "$@%&*" nil))
4093 (setq bb t) ; @{y}
4094 (condition-case nil
4095 (forward-sexp -1)
4096 (error nil)))
4097 (if (or bb
4098 (looking-at ; $foo -> {s}
4099 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4100 (and ; $foo[12] -> {s}
4101 (memq (following-char) '(?\{ ?\[))
4102 (progn
4103 (forward-sexp 1)
4104 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4105 (setq bb t)
4106 (goto-char b)))
4107 ((and (eq (following-char) ?=)
4108 (eq (char-after (1+ (point))) ?\>))
4109 ;; Check for { foo => 1, s => 2 }
4110 ;; Apparently s=> is never a substitution...
4111 (setq bb t))
4112 ((and (eq (following-char) ?:)
4113 (eq b1 ?\{) ; Check for $ { s::bar }
4114 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4115 (progn
4116 (goto-char (1- go))
4117 (skip-chars-backward " \t\n\f")
4118 (memq (preceding-char)
4119 (append "$@%&*" nil))))
4120 (setq bb t))
4121 ((eobp)
4122 (setq bb t)))))
4123 (if bb
4124 (goto-char i)
4125 ;; Skip whitespace and comments...
4126 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4127 (goto-char (match-end 0))
4128 (skip-chars-forward " \t\n\f"))
4129 (if (> (point) b)
4130 (put-text-property b (point) 'syntax-type 'prestring))
4131 ;; qtag means two-arg matcher, may be reset to
4132 ;; 2 or 3 later if some special quoting is needed.
4133 ;; e1 means matching-char matcher.
4134 (setq b (point) ; before the first delimiter
4135 ;; has 2 args
4136 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4137 ;; We do not search to max, since we may be called from
4138 ;; some hook of fontification, and max is random
4139 i (cperl-forward-re stop-point end
4141 st-l err-l argument)
4142 ;; If `go', then it is considered as 1-arg, `b1' is nil
4143 ;; as in s/foo//x; the point is before final "slash"
4144 b1 (nth 1 i) ; start of the second part
4145 tag (nth 2 i) ; ender-char, true if second part
4146 ; is with matching chars []
4147 go (nth 4 i) ; There is a 1-char part after the end
4148 i (car i) ; intermediate point
4149 e1 (point) ; end
4150 ;; Before end of the second part if non-matching: ///
4151 tail (if (and i (not tag))
4152 (1- e1))
4153 e (if i i e1) ; end of the first part
4154 qtag nil ; need to preserve backslashitis
4155 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4156 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4157 ;; Commenting \\ is dangerous, what about ( ?
4158 (and i tail
4159 (eq (char-after i) ?\\)
4160 (setq qtag t))
4161 (and (if go (looking-at ".\\sw*x")
4162 (looking-at "\\sw*x")) ; qr//x
4163 (setq is-x-REx t))
4164 (and (if go (looking-at ".\\sw*o")
4165 (looking-at "\\sw*o")) ; //o
4166 (setq is-o-REx t))
4167 (if (null i)
4168 ;; Considered as 1arg form
4169 (progn
4170 (cperl-commentify b (point) t)
4171 (put-text-property b (point) 'syntax-type 'string)
4172 (if (or is-x-REx
4173 ;; ignore other text properties:
4174 (string-match "^qw$" argument))
4175 (put-text-property b (point) 'indentable t))
4176 (and go
4177 (setq e1 (cperl-1+ e1))
4178 (or (eobp)
4179 (forward-char 1))))
4180 (cperl-commentify b i t)
4181 (if (looking-at "\\sw*e") ; s///e
4182 (progn
4183 ;; Cache the syntax info...
4184 (setq cperl-syntax-state (cons state-point state))
4185 (and
4186 ;; silent:
4187 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4188 ;; Error
4189 (goto-char (1+ max)))
4190 (if (and tag (eq (preceding-char) ?\>))
4191 (progn
4192 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4193 (cperl-modify-syntax-type i cperl-st-bra)))
4194 (put-text-property b i 'syntax-type 'string)
4195 (put-text-property i (point) 'syntax-type 'multiline)
4196 (if is-x-REx
4197 (put-text-property b i 'indentable t)))
4198 (cperl-commentify b1 (point) t)
4199 (put-text-property b (point) 'syntax-type 'string)
4200 (if is-x-REx
4201 (put-text-property b i 'indentable t))
4202 (if qtag
4203 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4204 (setq tail nil)))
4205 ;; Now: tail: if the second part is non-matching without ///e
4206 (if (eq (char-syntax (following-char)) ?w)
4207 (progn
4208 (forward-word-strictly 1) ; skip modifiers s///s
4209 (if tail (cperl-commentify tail (point) t))
4210 (cperl-postpone-fontification
4211 e1 (point) 'face my-cperl-REx-modifiers-face)))
4212 ;; Check whether it is m// which means "previous match"
4213 ;; and highlight differently
4214 (setq is-REx
4215 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4216 (or (not (= (length argument) 0))
4217 (not (eq c ?\<)))))
4218 (if (and is-REx
4219 (eq e (+ 2 b))
4220 ;; split // *is* using zero-pattern
4221 (save-excursion
4222 (condition-case nil
4223 (progn
4224 (goto-char tb)
4225 (forward-sexp -1)
4226 (not (looking-at "split\\>")))
4227 (error t))))
4228 (cperl-postpone-fontification
4229 b e 'face font-lock-warning-face)
4230 (if (or i2 ; Has 2 args
4231 (and cperl-fontify-m-as-s
4233 (string-match "^\\(m\\|qr\\)$" argument)
4234 (and (eq 0 (length argument))
4235 (not (eq ?\< (char-after b)))))))
4236 (progn
4237 (cperl-postpone-fontification
4238 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4239 (cperl-postpone-fontification
4240 (1- e) e 'face my-cperl-delimiters-face)))
4241 (if (and is-REx cperl-regexp-scan)
4242 ;; Process RExen: embedded comments, charclasses and ]
4243 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4244 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4245 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4246 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4247 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4248 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4249 (save-excursion
4250 (goto-char (1+ b))
4251 ;; First
4252 (cperl-look-at-leading-count is-x-REx e)
4253 (setq hairy-RE
4254 (concat
4255 (if is-x-REx
4256 (if (eq (char-after b) ?\#)
4257 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4258 "\\((\\?#\\)\\|\\(#\\)")
4259 ;; keep the same count: add a fake group
4260 (if (eq (char-after b) ?\#)
4261 "\\((\\?\\\\#\\)\\(\\)"
4262 "\\((\\?#\\)\\(\\)"))
4263 "\\|"
4264 "\\(\\[\\)" ; 3=[
4265 "\\|"
4266 "\\(]\\)" ; 4=]
4267 "\\|"
4268 ;; XXXX Will not be able to use it in s)))
4269 (if (eq (char-after b) ?\) )
4270 "\\())))\\)" ; Will never match
4271 (if (eq (char-after b) ?? )
4272 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4273 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4274 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4275 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4276 "\\(" ;; XXXX 1-char variables, exc. |()\s
4277 "[$@]"
4278 "\\("
4279 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4280 "\\|"
4281 "{[^{}]*}" ; only one-level allowed
4282 "\\|"
4283 "[^{(|) \t\r\n\f]"
4284 "\\)"
4285 "\\(" ;;8,9:code part of array/hash elt
4286 "\\(" "->" "\\)?"
4287 "\\[[^][]*\\]"
4288 "\\|"
4289 "{[^{}]*}"
4290 "\\)*"
4291 ;; XXXX: what if u is delim?
4292 "\\|"
4293 "[)^|$.*?+]"
4294 "\\|"
4295 "{[0-9]+}"
4296 "\\|"
4297 "{[0-9]+,[0-9]*}"
4298 "\\|"
4299 "\\\\[luLUEQbBAzZG]"
4300 "\\|"
4301 "(" ; Group opener
4302 "\\(" ; 10 group opener follower
4303 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4304 "\\|"
4305 "\\?[:=!>?{]" ; "?" something
4306 "\\|"
4307 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4308 "\\|"
4309 "\\?([0-9]+)" ; (?(1)foo|bar)
4310 "\\|"
4311 "\\?<[=!]"
4312 ;;;"\\|"
4313 ;;; "\\?"
4314 "\\)?"
4315 "\\)"
4316 "\\|"
4317 "\\\\\\(.\\)" ; 12=\SYMBOL
4319 (while
4320 (and (< (point) (1- e))
4321 (re-search-forward hairy-RE (1- e) 'to-end))
4322 (goto-char (match-beginning 0))
4323 (setq REx-subgr-start (point)
4324 was-subgr (following-char))
4325 (cond
4326 ((match-beginning 6) ; 0-length builtins, groups
4327 (goto-char (match-end 0))
4328 (if (match-beginning 11)
4329 (goto-char (match-beginning 11)))
4330 (if (>= (point) e)
4331 (goto-char (1- e)))
4332 (cperl-postpone-fontification
4333 (match-beginning 0) (point)
4334 'face
4335 (cond
4336 ((eq was-subgr ?\) )
4337 (condition-case nil
4338 (save-excursion
4339 (forward-sexp -1)
4340 (if (> (point) b)
4341 (if (if (eq (char-after b) ?? )
4342 (looking-at "(\\\\\\?")
4343 (eq (char-after (1+ (point))) ?\?))
4344 my-cperl-REx-0length-face
4345 my-cperl-REx-ctl-face)
4346 font-lock-warning-face))
4347 (error font-lock-warning-face)))
4348 ((eq was-subgr ?\| )
4349 my-cperl-REx-ctl-face)
4350 ((eq was-subgr ?\$ )
4351 (if (> (point) (1+ REx-subgr-start))
4352 (progn
4353 (put-text-property
4354 (match-beginning 0) (point)
4355 'REx-interpolated
4356 (if is-o-REx 0
4357 (if (and (eq (match-beginning 0)
4358 (1+ b))
4359 (eq (point)
4360 (1- e))) 1 t)))
4361 font-lock-variable-name-face)
4362 my-cperl-REx-spec-char-face))
4363 ((memq was-subgr (append "^." nil) )
4364 my-cperl-REx-spec-char-face)
4365 ((eq was-subgr ?\( )
4366 (if (not (match-beginning 10))
4367 my-cperl-REx-ctl-face
4368 my-cperl-REx-0length-face))
4369 (t my-cperl-REx-0length-face)))
4370 (if (and (memq was-subgr (append "(|" nil))
4371 (not (string-match "(\\?[-imsx]+)"
4372 (match-string 0))))
4373 (cperl-look-at-leading-count is-x-REx e))
4374 (setq was-subgr nil)) ; We do stuff here
4375 ((match-beginning 12) ; \SYMBOL
4376 (forward-char 2)
4377 (if (>= (point) e)
4378 (goto-char (1- e))
4379 ;; How many chars to not highlight:
4380 ;; 0-len special-alnums in other branch =>
4381 ;; Generic: \non-alnum (1), \alnum (1+face)
4382 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4383 (setq REx-subgr-start (point)
4384 qtag (preceding-char))
4385 (cperl-postpone-fontification
4386 (- (point) 2) (- (point) 1) 'face
4387 (if (memq qtag
4388 (append "ghijkmoqvFHIJKMORTVY" nil))
4389 font-lock-warning-face
4390 my-cperl-REx-0length-face))
4391 (if (and (eq (char-after b) qtag)
4392 (memq qtag (append ".])^$|*?+" nil)))
4393 (progn
4394 (if (and cperl-use-syntax-table-text-property
4395 (eq qtag ?\) ))
4396 (put-text-property
4397 REx-subgr-start (1- (point))
4398 'syntax-table cperl-st-punct))
4399 (cperl-postpone-fontification
4400 (1- (point)) (point) 'face
4401 ; \] can't appear below
4402 (if (memq qtag (append ".]^$" nil))
4403 'my-cperl-REx-spec-char-face
4404 (if (memq qtag (append "*?+" nil))
4405 'my-cperl-REx-0length-face
4406 'my-cperl-REx-ctl-face))))) ; )|
4407 ;; Test for arguments:
4408 (cond
4409 ;; This is not pretty: the 5.8.7 logic:
4410 ;; \0numx -> octal (up to total 3 dig)
4411 ;; \DIGIT -> backref unless \0
4412 ;; \DIGITs -> backref if valid
4413 ;; otherwise up to 3 -> octal
4414 ;; Do not try to distinguish, we guess
4415 ((or (and (memq qtag (append "01234567" nil))
4416 (re-search-forward
4417 "\\=[01234567]?[01234567]?"
4418 (1- e) 'to-end))
4419 (and (memq qtag (append "89" nil))
4420 (re-search-forward
4421 "\\=[0123456789]*" (1- e) 'to-end))
4422 (and (eq qtag ?x)
4423 (re-search-forward
4424 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4425 (1- e) 'to-end))
4426 (and (memq qtag (append "pPN" nil))
4427 (re-search-forward "\\={[^{}]+}\\|."
4428 (1- e) 'to-end))
4429 (eq (char-syntax qtag) ?w))
4430 (cperl-postpone-fontification
4431 (1- REx-subgr-start) (point)
4432 'face my-cperl-REx-length1-face))))
4433 (setq was-subgr nil)) ; We do stuff here
4434 ((match-beginning 3) ; [charclass]
4435 ;; Highlight leader, trailer, POSIX classes
4436 (forward-char 1)
4437 (if (eq (char-after b) ?^ )
4438 (and (eq (following-char) ?\\ )
4439 (eq (char-after (cperl-1+ (point)))
4440 ?^ )
4441 (forward-char 2))
4442 (and (eq (following-char) ?^ )
4443 (forward-char 1)))
4444 (setq argument b ; continue? & end of last POSIX
4445 tag nil ; list of POSIX classes
4446 qtag (point)) ; after leading ^ if present
4447 (if (eq (char-after b) ?\] )
4448 (and (eq (following-char) ?\\ )
4449 (eq (char-after (cperl-1+ (point)))
4450 ?\] )
4451 (setq qtag (1+ qtag))
4452 (forward-char 2))
4453 (and (eq (following-char) ?\] )
4454 (forward-char 1)))
4455 (setq REx-subgr-end qtag) ;End smart-highlighted
4456 ;; Apparently, I can't put \] into a charclass
4457 ;; in m]]: m][\\\]\]] produces [\\]]
4458 ;;; POSIX? [:word:] [:^word:] only inside []
4459 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4460 (while ; look for unescaped ]
4461 (and argument
4462 (re-search-forward
4463 (if (eq (char-after b) ?\] )
4464 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4465 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4466 (1- e) 'toend))
4467 ;; Is this ] an end of POSIX class?
4468 (if (save-excursion
4469 (and
4470 (search-backward "[" argument t)
4471 (< REx-subgr-start (point))
4472 (setq argument (point)) ; POSIX-start
4473 (or ; Should work with delim = \
4474 (not (eq (preceding-char) ?\\ ))
4475 ;; XXXX Double \\ is needed with 19.33
4476 (= (% (skip-chars-backward "\\\\") 2) 0))
4477 (looking-at
4478 (cond
4479 ((eq (char-after b) ?\] )
4480 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4481 ((eq (char-after b) ?\: )
4482 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4483 ((eq (char-after b) ?^ )
4484 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:]")
4485 ((eq (char-syntax (char-after b))
4487 (concat
4488 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4489 (char-to-string (char-after b))
4490 "\\|\\sw\\)+:]"))
4491 (t "\\\\*\\[:\\^?\\sw*:]")))
4492 (goto-char REx-subgr-end)
4493 (cperl-highlight-charclass
4494 argument my-cperl-REx-spec-char-face
4495 my-cperl-REx-0length-face my-cperl-REx-length1-face)))
4496 (setq tag (cons (cons argument (point))
4497 tag)
4498 argument (point)
4499 REx-subgr-end argument) ; continue
4500 (setq argument nil)))
4501 (and argument
4502 (message "Couldn't find end of charclass in a REx, pos=%s"
4503 REx-subgr-start))
4504 (setq argument (1- (point)))
4505 (goto-char REx-subgr-end)
4506 (cperl-highlight-charclass
4507 argument my-cperl-REx-spec-char-face
4508 my-cperl-REx-0length-face my-cperl-REx-length1-face)
4509 (forward-char 1)
4510 ;; Highlight starter, trailer, POSIX
4511 (if (and cperl-use-syntax-table-text-property
4512 (> (- (point) 2) REx-subgr-start))
4513 (put-text-property
4514 (1+ REx-subgr-start) (1- (point))
4515 'syntax-table cperl-st-punct))
4516 (cperl-postpone-fontification
4517 REx-subgr-start qtag
4518 'face my-cperl-REx-spec-char-face)
4519 (cperl-postpone-fontification
4520 (1- (point)) (point) 'face
4521 my-cperl-REx-spec-char-face)
4522 (if (eq (char-after b) ?\] )
4523 (cperl-postpone-fontification
4524 (- (point) 2) (1- (point))
4525 'face my-cperl-REx-0length-face))
4526 (while tag
4527 (cperl-postpone-fontification
4528 (car (car tag)) (cdr (car tag))
4529 'face font-lock-variable-name-face) ;my-cperl-REx-length1-face
4530 (setq tag (cdr tag)))
4531 (setq was-subgr nil)) ; did facing already
4532 ;; Now rare stuff:
4533 ((and (match-beginning 2) ; #-comment
4534 (/= (match-beginning 2) (match-end 2)))
4535 (beginning-of-line 2)
4536 (if (> (point) e)
4537 (goto-char (1- e))))
4538 ((match-beginning 4) ; character "]"
4539 (setq was-subgr nil) ; We do stuff here
4540 (goto-char (match-end 0))
4541 (if cperl-use-syntax-table-text-property
4542 (put-text-property
4543 (1- (point)) (point)
4544 'syntax-table cperl-st-punct))
4545 (cperl-postpone-fontification
4546 (1- (point)) (point)
4547 'face font-lock-warning-face))
4548 ((match-beginning 5) ; before (?{}) (??{})
4549 (setq tag (match-end 0))
4550 (if (or (setq qtag
4551 (cperl-forward-group-in-re st-l))
4552 (and (>= (point) e)
4553 (setq qtag "no matching `)' found"))
4554 (and (not (eq (char-after (- (point) 2))
4555 ?\} ))
4556 (setq qtag "Can't find })")))
4557 (progn
4558 (goto-char (1- e))
4559 (message "%s" qtag))
4560 (cperl-postpone-fontification
4561 (1- tag) (1- (point))
4562 'face font-lock-variable-name-face)
4563 (cperl-postpone-fontification
4564 REx-subgr-start (1- tag)
4565 'face my-cperl-REx-spec-char-face)
4566 (cperl-postpone-fontification
4567 (1- (point)) (point)
4568 'face my-cperl-REx-spec-char-face)
4569 (if cperl-use-syntax-table-text-property
4570 (progn
4571 (put-text-property
4572 (- (point) 2) (1- (point))
4573 'syntax-table cperl-st-cfence)
4574 (put-text-property
4575 (+ REx-subgr-start 2)
4576 (+ REx-subgr-start 3)
4577 'syntax-table cperl-st-cfence))))
4578 (setq was-subgr nil))
4579 (t ; (?#)-comment
4580 ;; Inside "(" and "\" arn't special in any way
4581 ;; Works also if the outside delimiters are ().
4582 (or;;(if (eq (char-after b) ?\) )
4583 ;;(re-search-forward
4584 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4585 ;; (1- e) 'toend)
4586 (search-forward ")" (1- e) 'toend)
4588 (message
4589 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4590 REx-subgr-start))))
4591 (if (>= (point) e)
4592 (goto-char (1- e)))
4593 (cond
4594 (was-subgr
4595 (setq REx-subgr-end (point))
4596 (cperl-commentify
4597 REx-subgr-start REx-subgr-end nil)
4598 (cperl-postpone-fontification
4599 REx-subgr-start REx-subgr-end
4600 'face font-lock-comment-face))))))
4601 (if (and is-REx is-x-REx)
4602 (put-text-property (1+ b) (1- e)
4603 'syntax-subtype 'x-REx)))
4604 (if (and i2 e1 (or (not b1) (> e1 b1)))
4605 (progn ; No errors finding the second part...
4606 (cperl-postpone-fontification
4607 (1- e1) e1 'face my-cperl-delimiters-face)
4608 (if (and (not (eobp))
4609 (assoc (char-after b) cperl-starters))
4610 (progn
4611 (cperl-postpone-fontification
4612 b1 (1+ b1) 'face my-cperl-delimiters-face)
4613 (put-text-property b1 (1+ b1)
4614 'REx-part2 t)))))
4615 (if (> (point) max)
4616 (setq tmpend tb))))
4617 ((match-beginning 17) ; sub with prototype or attribute
4618 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4619 ;;"\\<sub\\>\\(" ;12
4620 ;; cperl-white-and-comment-rex ;13
4621 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4622 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4623 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4624 (setq b1 (match-beginning 14) e1 (match-end 14))
4625 (if (memq (char-after (1- b))
4626 '(?\$ ?\@ ?\% ?\& ?\*))
4628 (goto-char b)
4629 (if (eq (char-after (match-beginning 17)) ?\( )
4630 (progn
4631 (cperl-commentify ; Prototypes; mark as string
4632 (match-beginning 17) (match-end 17) t)
4633 (goto-char (match-end 0))
4634 ;; Now look for attributes after prototype:
4635 (forward-comment (buffer-size))
4636 (and (looking-at ":[^:]")
4637 (cperl-find-sub-attrs st-l b1 e1 b)))
4638 ;; treat attributes without prototype
4639 (goto-char (match-beginning 17))
4640 (cperl-find-sub-attrs st-l b1 e1 b))))
4641 ;; 1+6+2+1+1+6+1=18 extra () before this:
4642 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4643 ((match-beginning 19) ; old $abc'efg syntax
4644 (setq bb (match-end 0))
4645 ;;;(if (nth 3 state) nil ; in string
4646 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4647 (goto-char bb))
4648 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4649 ;; "__\\(END\\|DATA\\)__"
4650 ((match-beginning 20) ; __END__, __DATA__
4651 (setq bb (match-end 0))
4652 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4653 (cperl-commentify b bb nil)
4654 (setq end t))
4655 ;; "\\\\\\(['`\"($]\\)"
4656 ((match-beginning 21)
4657 ;; Trailing backslash; make non-quoting outside string/comment
4658 (setq bb (match-end 0))
4659 (goto-char b)
4660 (skip-chars-backward "\\\\")
4661 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4662 (cperl-modify-syntax-type b cperl-st-punct)
4663 (goto-char bb))
4664 (t (error "Error in regexp of the sniffer")))
4665 (if (> (point) stop-point)
4666 (progn
4667 (if end
4668 (message "Garbage after __END__/__DATA__ ignored")
4669 (message "Unbalanced syntax found while scanning")
4670 (or (car err-l) (setcar err-l b)))
4671 (goto-char stop-point))))
4672 (setq cperl-syntax-state (cons state-point state)
4673 ;; Do not mark syntax as done past tmpend???
4674 cperl-syntax-done-to (or tmpend (max (point) max)))
4675 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4677 (if (car err-l) (goto-char (car err-l))
4678 (or non-inter
4679 (message "Scanning for \"hard\" Perl constructions... done"))))
4680 (and (buffer-modified-p)
4681 (not modified)
4682 (set-buffer-modified-p nil))
4683 ;; I do not understand what this is doing here. It breaks font-locking
4684 ;; because it resets the syntax-table from font-lock-syntax-table to
4685 ;; cperl-mode-syntax-table.
4686 ;; (set-syntax-table cperl-mode-syntax-table)
4688 (list (car err-l) overshoot)))
4690 (defun cperl-find-pods-heres-region (min max)
4691 (interactive "r")
4692 (cperl-find-pods-heres min max))
4694 (defun cperl-backward-to-noncomment (lim)
4695 ;; Stops at lim or after non-whitespace that is not in comment
4696 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4697 (let (stop p pr)
4698 (while (and (not stop) (> (point) (or lim (point-min))))
4699 (skip-chars-backward " \t\n\f" lim)
4700 (setq p (point))
4701 (beginning-of-line)
4702 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4703 '(pod here-doc here-doc-delim))
4704 (progn
4705 (cperl-unwind-to-safe nil)
4706 (setq pr (get-text-property (point) 'syntax-type))))
4707 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4708 (not (memq pr '(string prestring))))
4709 (progn (cperl-to-comment-or-eol) (bolp))
4710 (progn
4711 (skip-chars-backward " \t")
4712 (if (< p (point)) (goto-char p))
4713 (setq stop t))))))
4715 ;; Used only in `cperl-calculate-indent'...
4716 (defun cperl-block-p ()
4717 "Point is before ?\\{. Checks whether it starts a block."
4718 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4719 (cperl-backward-to-noncomment (point-min))
4720 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4721 ; Label may be mixed up with `$blah :'
4722 (save-excursion (cperl-after-label))
4723 (get-text-property (cperl-1- (point)) 'attrib-group)
4724 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4725 (progn
4726 (backward-sexp)
4727 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr', `constant'
4728 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4729 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\|constant\\)\\>")))
4730 ;; sub bless::foo {}
4731 (progn
4732 (cperl-backward-to-noncomment (point-min))
4733 (and (eq (preceding-char) ?b)
4734 (progn
4735 (forward-sexp -1)
4736 (looking-at (concat cperl-sub-regexp "[ \t\n\f#]"))))))))))
4738 ;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4739 ;; No save-excursion; condition-case ... In (cperl-block-p) the block
4740 ;; may be a part of an in-statement construct, such as
4741 ;; ${something()}, print {FH} $data.
4742 ;; Moreover, one takes positive approach (looks for else,grep etc)
4743 ;; another negative (looks for bless,tr etc)
4744 (defun cperl-after-block-p (lim &optional pre-block)
4745 "Return true if the preceding } (if PRE-BLOCK, following {) delimits a block.
4746 Would not look before LIM. Assumes that LIM is a good place to begin a
4747 statement. The kind of block we treat here is one after which a new
4748 statement would start; thus the block in ${func()} does not count."
4749 (save-excursion
4750 (condition-case nil
4751 (progn
4752 (or pre-block (forward-sexp -1))
4753 (cperl-backward-to-noncomment lim)
4754 (or (eq (point) lim)
4755 ;; if () {} // sub f () {} // sub f :a(') {}
4756 (eq (preceding-char) ?\) )
4757 ;; label: {}
4758 (save-excursion (cperl-after-label))
4759 ;; sub :attr {}
4760 (get-text-property (cperl-1- (point)) 'attrib-group)
4761 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4762 (save-excursion
4763 (forward-sexp -1)
4764 ;; else {} but not else::func {}
4765 (or (and (looking-at "\\(else\\|catch\\|try\\|continue\\|grep\\|map\\|BEGIN\\|END\\|UNITCHECK\\|CHECK\\|INIT\\)\\>")
4766 (not (looking-at "\\(\\sw\\|_\\)+::")))
4767 ;; sub f {}
4768 (progn
4769 (cperl-backward-to-noncomment lim)
4770 (and (cperl-char-ends-sub-keyword-p (preceding-char))
4771 (progn
4772 (forward-sexp -1)
4773 (looking-at
4774 (concat cperl-sub-regexp "[ \t\n\f#]")))))))
4775 ;; What precedes is not word... XXXX Last statement in sub???
4776 (cperl-after-expr-p lim))))
4777 (error nil))))
4779 (defun cperl-after-expr-p (&optional lim chars test)
4780 "Return true if the position is good for start of expression.
4781 TEST is the expression to evaluate at the found position. If absent,
4782 CHARS is a string that contains good characters to have before us (however,
4783 `}' is treated \"smartly\" if it is not in the list)."
4784 (let ((lim (or lim (point-min)))
4785 stop p)
4786 (cperl-update-syntaxification (point) (point))
4787 (save-excursion
4788 (while (and (not stop) (> (point) lim))
4789 (skip-chars-backward " \t\n\f" lim)
4790 (setq p (point))
4791 (beginning-of-line)
4792 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4793 ;; '(pod here-doc here-doc-delim))
4794 (if (get-text-property (point) 'here-doc-group)
4795 (progn
4796 (goto-char
4797 (cperl-beginning-of-property (point) 'here-doc-group))
4798 (beginning-of-line 0)))
4799 (if (get-text-property (point) 'in-pod)
4800 (progn
4801 (goto-char
4802 (cperl-beginning-of-property (point) 'in-pod))
4803 (beginning-of-line 0)))
4804 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4805 ;; Else: last iteration, or a label
4806 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4807 (skip-chars-backward " \t")
4808 (if (< p (point)) (goto-char p))
4809 (setq p (point))
4810 (if (and (eq (preceding-char) ?:)
4811 (progn
4812 (forward-char -1)
4813 (skip-chars-backward " \t\n\f" lim)
4814 (memq (char-syntax (preceding-char)) '(?w ?_))))
4815 (forward-sexp -1) ; Possibly label. Skip it
4816 (goto-char p)
4817 (setq stop t))))
4818 (or (bobp) ; ???? Needed
4819 (eq (point) lim)
4820 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4821 (progn
4822 (if test (eval test)
4823 (or (memq (preceding-char) (append (or chars "{;") nil))
4824 (and (eq (preceding-char) ?\})
4825 (cperl-after-block-p lim))
4826 (and (eq (following-char) ?.) ; in format: see comment above
4827 (eq (get-text-property (point) 'syntax-type)
4828 'format)))))))))
4830 (defun cperl-backward-to-start-of-expr (&optional lim)
4831 (condition-case nil
4832 (progn
4833 (while (and (or (not lim)
4834 (> (point) lim))
4835 (not (cperl-after-expr-p lim)))
4836 (forward-sexp -1)
4837 ;; May be after $, @, $# etc of a variable
4838 (skip-chars-backward "$@%#")))
4839 (error nil)))
4841 (defun cperl-at-end-of-expr (&optional lim)
4842 ;; Since the SEXP approach below is very fragile, do some overengineering
4843 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4844 (condition-case nil
4845 (save-excursion
4846 ;; If nothing interesting after, does as (forward-sexp -1);
4847 ;; otherwise fails, or ends at a start of following sexp.
4848 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4849 ;; may be stuck after @ or $; just put some stupid workaround now:
4850 (let ((p (point)))
4851 (forward-sexp 1)
4852 (forward-sexp -1)
4853 (while (memq (preceding-char) (append "%&@$*" nil))
4854 (forward-char -1))
4855 (or (< (point) p)
4856 (cperl-after-expr-p lim))))
4857 (error t))))
4859 (defun cperl-forward-to-end-of-expr (&optional lim)
4860 (condition-case nil
4861 (progn
4862 (while (and (< (point) (or lim (point-max)))
4863 (not (cperl-at-end-of-expr)))
4864 (forward-sexp 1)))
4865 (error nil)))
4867 (defun cperl-backward-to-start-of-continued-exp (lim)
4868 (if (memq (preceding-char) (append ")]}\"'`" nil))
4869 (forward-sexp -1))
4870 (beginning-of-line)
4871 (if (<= (point) lim)
4872 (goto-char (1+ lim)))
4873 (skip-chars-forward " \t"))
4875 (defun cperl-after-block-and-statement-beg (lim)
4876 ;; We assume that we are after ?\}
4877 (and
4878 (cperl-after-block-p lim)
4879 (save-excursion
4880 (forward-sexp -1)
4881 (cperl-backward-to-noncomment (point-min))
4882 (or (bobp)
4883 (eq (point) lim)
4884 (not (= (char-syntax (preceding-char)) ?w))
4885 (progn
4886 (forward-sexp -1)
4887 (not
4888 (looking-at
4889 "\\(map\\|grep\\|say\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4892 (defun cperl-indent-exp ()
4893 "Simple variant of indentation of continued-sexp.
4895 Will not indent comment if it starts at `comment-indent' or looks like
4896 continuation of the comment on the previous line.
4898 If `cperl-indent-region-fix-constructs', will improve spacing on
4899 conditional/loop constructs."
4900 (interactive)
4901 (save-excursion
4902 (let ((tmp-end (point-at-eol)) top done)
4903 (save-excursion
4904 (beginning-of-line)
4905 (while (null done)
4906 (setq top (point))
4907 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
4908 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
4909 (setq top (point))) ; Get the outermost parens in line
4910 (goto-char top)
4911 (while (< (point) tmp-end)
4912 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
4913 (or (eolp) (forward-sexp 1)))
4914 (if (> (point) tmp-end) ; Yes, there an unfinished block
4916 (if (eq ?\) (preceding-char))
4917 (progn ;; Plan B: find by REGEXP block followup this line
4918 (setq top (point))
4919 (condition-case nil
4920 (progn
4921 (forward-sexp -2)
4922 (if (eq (following-char) ?$ ) ; for my $var (list)
4923 (progn
4924 (forward-sexp -1)
4925 (if (looking-at "\\(state\\|my\\|local\\|our\\)\\>")
4926 (forward-sexp -1))))
4927 (if (looking-at
4928 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
4929 "\\|for\\(each\\)?\\>\\(\\("
4930 cperl-maybe-white-and-comment-rex
4931 "\\(state\\|my\\|local\\|our\\)\\)?"
4932 cperl-maybe-white-and-comment-rex
4933 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
4934 (progn
4935 (goto-char top)
4936 (forward-sexp 1)
4937 (setq top (point)))))
4938 (error (setq done t)))
4939 (goto-char top))
4940 (if (looking-at ; Try Plan C: continuation block
4941 (concat cperl-maybe-white-and-comment-rex
4942 "\\<\\(else\\|elsif\\|continue\\)\\>"))
4943 (progn
4944 (goto-char (match-end 0))
4945 (setq tmp-end (point-at-eol)))
4946 (setq done t))))
4947 (setq tmp-end (point-at-eol)))
4948 (goto-char tmp-end)
4949 (setq tmp-end (point-marker)))
4950 (if cperl-indent-region-fix-constructs
4951 (cperl-fix-line-spacing tmp-end))
4952 (cperl-indent-region (point) tmp-end))))
4954 (defun cperl-fix-line-spacing (&optional end parse-data)
4955 "Improve whitespace in a conditional/loop construct.
4956 Returns some position at the last line."
4957 (interactive)
4958 (or end
4959 (setq end (point-max)))
4960 (let ((ee (point-at-eol))
4961 (cperl-indent-region-fix-constructs
4962 (or cperl-indent-region-fix-constructs 1))
4963 p pp ml have-brace ret)
4964 (save-excursion
4965 (beginning-of-line)
4966 (setq ret (point))
4967 ;; }? continue
4968 ;; blah; }
4969 (if (not
4970 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
4971 (setq have-brace (save-excursion (search-forward "}" ee t)))))
4972 nil ; Do not need to do anything
4973 ;; Looking at:
4974 ;; }
4975 ;; else
4976 (if cperl-merge-trailing-else
4977 (if (looking-at
4978 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
4979 (progn
4980 (search-forward "}")
4981 (setq p (point))
4982 (skip-chars-forward " \t\n")
4983 (delete-region p (point))
4984 (insert (make-string cperl-indent-region-fix-constructs ?\s))
4985 (beginning-of-line)))
4986 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
4987 (save-excursion
4988 (search-forward "}")
4989 (delete-horizontal-space)
4990 (insert "\n")
4991 (setq ret (point))
4992 (if (cperl-indent-line parse-data)
4993 (progn
4994 (cperl-fix-line-spacing end parse-data)
4995 (setq ret (point)))))))
4996 ;; Looking at:
4997 ;; } else
4998 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
4999 (progn
5000 (search-forward "}")
5001 (delete-horizontal-space)
5002 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5003 (beginning-of-line)))
5004 ;; Looking at:
5005 ;; else {
5006 (if (looking-at
5007 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5008 (progn
5009 (forward-word-strictly 1)
5010 (delete-horizontal-space)
5011 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5012 (beginning-of-line)))
5013 ;; Looking at:
5014 ;; foreach my $var
5015 (if (looking-at
5016 "[ \t]*\\<for\\(each\\)?[ \t]+\\(state\\|my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5017 (progn
5018 (forward-word-strictly 2)
5019 (delete-horizontal-space)
5020 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5021 (beginning-of-line)))
5022 ;; Looking at:
5023 ;; foreach my $var (
5024 (if (looking-at
5025 "[ \t]*\\<for\\(each\\)?[ \t]+\\(state\\|my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5026 (progn
5027 (forward-sexp 3)
5028 (delete-horizontal-space)
5029 (insert
5030 (make-string cperl-indent-region-fix-constructs ?\s))
5031 (beginning-of-line)))
5032 ;; Looking at (with or without "}" at start, ending after "({"):
5033 ;; } foreach my $var () OR {
5034 (if (looking-at
5035 "[ \t]*\\(}[ \t]*\\)?\\<\\(\\els\\(e\\|if\\)\\|continue\\|if\\|unless\\|while\\|for\\(each\\)?\\(\\([ \t]+\\(state\\|my\\|local\\|our\\)\\)?[ \t]*\\$[_a-zA-Z0-9]+\\)?\\|until\\)\\>\\([ \t]*(\\|[ \t\n]*{\\)\\|[ \t]*{")
5036 (progn
5037 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5038 (re-search-forward "[({]")
5039 (forward-char -1)
5040 (setq p (point))
5041 (if (eq (following-char) ?\( )
5042 (progn
5043 (forward-sexp 1)
5044 (setq pp (point))) ; past parenthesis-group
5045 ;; after `else' or nothing
5046 (if ml ; after `else'
5047 (skip-chars-backward " \t\n")
5048 (beginning-of-line))
5049 (setq pp nil))
5050 ;; Now after the sexp before the brace
5051 ;; Multiline expr should be special
5052 (setq ml (and pp (save-excursion (goto-char p)
5053 (search-forward "\n" pp t))))
5054 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5055 (looking-at "[ \t\n]*{"))
5056 (progn
5057 (cond
5058 ((bolp) ; Were before `{', no if/else/etc
5059 nil)
5060 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5061 (delete-horizontal-space)
5062 (if (if ml
5063 cperl-extra-newline-before-brace-multiline
5064 cperl-extra-newline-before-brace)
5065 (progn
5066 (delete-horizontal-space)
5067 (insert "\n")
5068 (setq ret (point))
5069 (if (cperl-indent-line parse-data)
5070 (progn
5071 (cperl-fix-line-spacing end parse-data)
5072 (setq ret (point)))))
5073 (insert
5074 (make-string cperl-indent-region-fix-constructs ?\s))))
5075 ((and (looking-at "[ \t]*\n")
5076 (not (if ml
5077 cperl-extra-newline-before-brace-multiline
5078 cperl-extra-newline-before-brace)))
5079 (setq pp (point))
5080 (skip-chars-forward " \t\n")
5081 (delete-region pp (point))
5082 (insert
5083 (make-string cperl-indent-region-fix-constructs ?\ )))
5084 ((and (looking-at "[\t ]*{")
5085 (if ml cperl-extra-newline-before-brace-multiline
5086 cperl-extra-newline-before-brace))
5087 (delete-horizontal-space)
5088 (insert "\n")
5089 (setq ret (point))
5090 (if (cperl-indent-line parse-data)
5091 (progn
5092 (cperl-fix-line-spacing end parse-data)
5093 (setq ret (point))))))
5094 ;; Now we are before `{'
5095 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5096 (progn
5097 (skip-chars-forward " \t\n")
5098 (setq pp (point))
5099 (forward-sexp 1)
5100 (setq p (point))
5101 (goto-char pp)
5102 (setq ml (search-forward "\n" p t))
5103 (if (or cperl-break-one-line-blocks-when-indent ml)
5104 ;; not good: multi-line BLOCK
5105 (progn
5106 (goto-char (1+ pp))
5107 (delete-horizontal-space)
5108 (insert "\n")
5109 (setq ret (point))
5110 (if (cperl-indent-line parse-data)
5111 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5112 (beginning-of-line)
5113 (setq p (point) pp (point-at-eol)) ; May be different from ee.
5114 ;; Now check whether there is a hanging `}'
5115 ;; Looking at:
5116 ;; } blah
5117 (if (and
5118 cperl-fix-hanging-brace-when-indent
5119 have-brace
5120 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5121 (condition-case nil
5122 (progn
5123 (up-list 1)
5124 (if (and (<= (point) pp)
5125 (eq (preceding-char) ?\} )
5126 (cperl-after-block-and-statement-beg (point-min)))
5128 (goto-char p)
5129 nil))
5130 (error nil)))
5131 (progn
5132 (forward-char -1)
5133 (skip-chars-backward " \t")
5134 (if (bolp)
5135 ;; `}' was the first thing on the line, insert NL *after* it.
5136 (progn
5137 (cperl-indent-line parse-data)
5138 (search-forward "}")
5139 (delete-horizontal-space)
5140 (insert "\n"))
5141 (delete-horizontal-space)
5142 (or (eq (preceding-char) ?\;)
5143 (bolp)
5144 (and (eq (preceding-char) ?\} )
5145 (cperl-after-block-p (point-min)))
5146 (insert ";"))
5147 (insert "\n")
5148 (setq ret (point)))
5149 (if (cperl-indent-line parse-data)
5150 (setq ret (cperl-fix-line-spacing end parse-data)))
5151 (beginning-of-line)))))
5152 ret))
5154 (defvar cperl-update-start) ; Do not need to make them local
5155 (defvar cperl-update-end)
5156 (defun cperl-delay-update-hook (beg end _old-len)
5157 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5158 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5160 (defun cperl-indent-region (start end)
5161 "Simple variant of indentation of region in CPerl mode.
5162 Should be slow. Will not indent comment if it starts at `comment-indent'
5163 or looks like continuation of the comment on the previous line.
5164 Indents all the lines whose first character is between START and END
5165 inclusive.
5167 If `cperl-indent-region-fix-constructs', will improve spacing on
5168 conditional/loop constructs."
5169 (interactive "r")
5170 (cperl-update-syntaxification end end)
5171 (save-excursion
5172 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5173 (let ((indent-info (list nil nil nil) ; Cannot use '(), since will modify
5175 after-change-functions ; Speed it up!
5176 comm old-comm-indent new-comm-indent i empty)
5177 (if h-a-c (add-hook 'after-change-functions #'cperl-delay-update-hook))
5178 (goto-char start)
5179 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5180 (current-column))
5181 new-comm-indent old-comm-indent)
5182 (goto-char start)
5183 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5184 (or (bolp) (beginning-of-line 2))
5185 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5186 (if (or
5187 (setq empty (looking-at "[ \t]*\n"))
5188 (and (setq comm (looking-at "[ \t]*#"))
5189 (or (eq (current-indentation) (or old-comm-indent
5190 comment-column))
5191 (setq old-comm-indent nil))))
5192 (if (and old-comm-indent
5193 (not empty)
5194 (= (current-indentation) old-comm-indent)
5195 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5196 (not (eq (get-text-property (point) 'syntax-table)
5197 cperl-st-cfence)))
5198 (let ((comment-column new-comm-indent))
5199 (indent-for-comment)))
5200 (progn
5201 (setq i (cperl-indent-line indent-info))
5202 (or comm
5203 (not i)
5204 (progn
5205 (if cperl-indent-region-fix-constructs
5206 (goto-char (cperl-fix-line-spacing end indent-info)))
5207 (if (setq old-comm-indent
5208 (and (cperl-to-comment-or-eol)
5209 (not (memq (get-text-property (point)
5210 'syntax-type)
5211 '(pod here-doc)))
5212 (not (eq (get-text-property (point)
5213 'syntax-table)
5214 cperl-st-cfence))
5215 (current-column)))
5216 (progn (indent-for-comment)
5217 (skip-chars-backward " \t")
5218 (skip-chars-backward "#")
5219 (setq new-comm-indent (current-column))))))))
5220 (beginning-of-line 2)))
5221 ;; Now run the update hooks
5222 (and after-change-functions
5223 cperl-update-end
5224 (save-excursion
5225 (goto-char cperl-update-end)
5226 (insert " ")
5227 (delete-char -1)
5228 (goto-char cperl-update-start)
5229 (insert " ")
5230 (delete-char -1))))))
5232 ;; Stolen from lisp-mode with a lot of improvements
5234 (defun cperl-fill-paragraph (&optional justify iteration)
5235 "Like `fill-paragraph', but handle CPerl comments.
5236 If any of the current line is a comment, fill the comment or the
5237 block of it that point is in, preserving the comment's initial
5238 indentation and initial hashes. Behaves usually outside of comment."
5239 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5240 (let (;; Non-nil if the current line contains a comment.
5241 has-comment
5242 fill-paragraph-function ; do not recurse
5243 ;; If has-comment, the appropriate fill-prefix for the comment.
5244 comment-fill-prefix
5245 ;; Line that contains code and comment (or nil)
5246 start
5247 c spaces len dc (comment-column comment-column))
5248 ;; Figure out what kind of comment we are looking at.
5249 (save-excursion
5250 (beginning-of-line)
5251 (cond
5253 ;; A line with nothing but a comment on it?
5254 ((looking-at "[ \t]*#[# \t]*")
5255 (setq has-comment t
5256 comment-fill-prefix (buffer-substring (match-beginning 0)
5257 (match-end 0))))
5259 ;; A line with some code, followed by a comment? Remember that the
5260 ;; semi which starts the comment shouldn't be part of a string or
5261 ;; character.
5262 ((cperl-to-comment-or-eol)
5263 (setq has-comment t)
5264 (looking-at "#+[ \t]*")
5265 (setq start (point) c (current-column)
5266 comment-fill-prefix
5267 (concat (make-string (current-column) ?\s)
5268 (buffer-substring (match-beginning 0) (match-end 0)))
5269 spaces (progn (skip-chars-backward " \t")
5270 (buffer-substring (point) start))
5271 dc (- c (current-column)) len (- start (point))
5272 start (point-marker))
5273 (delete-char len)
5274 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5275 (if (not has-comment)
5276 (fill-paragraph justify) ; Do the usual thing outside of comment
5277 ;; Narrow to include only the comment, and then fill the region.
5278 (save-restriction
5279 (narrow-to-region
5280 ;; Find the first line we should include in the region to fill.
5281 (if start (progn (beginning-of-line) (point))
5282 (save-excursion
5283 (while (and (zerop (forward-line -1))
5284 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5285 ;; We may have gone to far. Go forward again.
5286 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5287 (forward-line 1))
5288 (point)))
5289 ;; Find the beginning of the first line past the region to fill.
5290 (save-excursion
5291 (while (progn (forward-line 1)
5292 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5293 (point)))
5294 ;; Remove existing hashes
5295 (goto-char (point-min))
5296 (save-excursion
5297 (while (progn (forward-line 1) (< (point) (point-max)))
5298 (skip-chars-forward " \t")
5299 (if (looking-at "#+")
5300 (progn
5301 (if (and (eq (point) (match-beginning 0))
5302 (not (eq (point) (match-end 0)))) nil
5303 (error
5304 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5305 (delete-char (- (match-end 0) (match-beginning 0)))))))
5307 ;; Lines with only hashes on them can be paragraph boundaries.
5308 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5309 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5310 (fill-prefix comment-fill-prefix))
5311 (fill-paragraph justify)))
5312 (if (and start)
5313 (progn
5314 (goto-char start)
5315 (if (> dc 0)
5316 (progn (delete-char dc) (insert spaces)))
5317 (if (or (= (current-column) c) iteration) nil
5318 (setq comment-column c)
5319 (indent-for-comment)
5320 ;; Repeat once more, flagging as iteration
5321 (cperl-fill-paragraph justify t))))))
5324 (defun cperl-do-auto-fill ()
5325 ;; Break out if the line is short enough
5326 (if (> (save-excursion
5327 (end-of-line)
5328 (current-column))
5329 fill-column)
5330 (let ((c (save-excursion (beginning-of-line)
5331 (cperl-to-comment-or-eol) (point)))
5332 (s (memq (following-char) '(?\s ?\t))) marker)
5333 (if (>= c (point))
5334 ;; Don't break line inside code: only inside comment.
5336 (setq marker (point-marker))
5337 (fill-paragraph nil)
5338 (goto-char marker)
5339 ;; Is not enough, sometimes marker is a start of line
5340 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5341 (goto-char (match-end 0))))
5342 ;; Following space could have gone:
5343 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5344 (insert " ")
5345 (backward-char 1))
5346 ;; Previous space could have gone:
5347 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5349 (defun cperl-imenu-addback (lst &optional isback name)
5350 ;; We suppose that the lst is a DAG, unless the first element only
5351 ;; loops back, and ISBACK is set. Thus this function cannot be
5352 ;; applied twice without ISBACK set.
5353 (cond ((not cperl-imenu-addback) lst)
5355 (or name
5356 (setq name "+++BACK+++"))
5357 (mapc (lambda (elt)
5358 (if (and (listp elt) (listp (cdr elt)))
5359 (progn
5360 ;; In the other order it goes up
5361 ;; one level only ;-(
5362 (setcdr elt (cons (cons name lst)
5363 (cdr elt)))
5364 (cperl-imenu-addback (cdr elt) t name))))
5365 (if isback (cdr lst) lst))
5366 lst)))
5368 (defun cperl-imenu--create-perl-index (&optional regexp)
5369 (require 'imenu) ; May be called from TAGS creator
5370 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5371 (index-unsorted-alist '())
5372 (index-meth-alist '()) meth
5373 packages ends-ranges p marker is-proto
5374 is-pack index index1 name (end-range 0) package)
5375 (goto-char (point-min))
5376 (cperl-update-syntaxification (point-max) (point-max))
5377 ;; Search for the function
5378 (progn ;;save-match-data
5379 (while (re-search-forward
5380 (or regexp cperl-imenu--function-name-regexp-perl)
5381 nil t)
5382 ;; 2=package-group, 5=package-name 8=sub-name
5383 (cond
5384 ((and ; Skip some noise if building tags
5385 (match-beginning 5) ; package name
5386 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5387 (not (save-match-data
5388 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5389 nil)
5390 ((and
5391 (or (match-beginning 2)
5392 (match-beginning 8)) ; package or sub
5393 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5394 (null (get-text-property (match-beginning 1) 'syntax-table))
5395 (null (get-text-property (match-beginning 1) 'syntax-type))
5396 (null (get-text-property (match-beginning 1) 'in-pod)))
5397 (setq is-pack (match-beginning 2))
5398 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5399 ;; (goto-char (match-end 0))) ; Messes what follows
5400 (setq meth nil
5401 p (point))
5402 (while (and ends-ranges (>= p (car ends-ranges)))
5403 ;; delete obsolete entries
5404 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5405 (setq package (or (car packages) "")
5406 end-range (or (car ends-ranges) 0))
5407 (if is-pack ; doing "package"
5408 (progn
5409 (if (match-beginning 5) ; named package
5410 (setq name (buffer-substring (match-beginning 5)
5411 (match-end 5))
5412 name (progn
5413 (set-text-properties 0 (length name) nil name)
5414 name)
5415 package (concat name "::")
5416 name (concat "package " name))
5417 ;; Support nameless packages
5418 (setq name "package;" package ""))
5419 (setq end-range
5420 (save-excursion
5421 (parse-partial-sexp (point) (point-max) -1) (point))
5422 ends-ranges (cons end-range ends-ranges)
5423 packages (cons package packages)))
5424 (setq is-proto
5425 (or (eq (following-char) ?\;)
5426 (eq 0 (get-text-property (point) 'attrib-group)))))
5427 ;; Skip this function name if it is a prototype declaration.
5428 (if (and is-proto (not is-pack)) nil
5429 (or is-pack
5430 (setq name
5431 (buffer-substring (match-beginning 8) (match-end 8)))
5432 (set-text-properties 0 (length name) nil name))
5433 (setq marker (make-marker))
5434 (set-marker marker (match-end (if is-pack 2 8)))
5435 (cond (is-pack nil)
5436 ((string-match "[:']" name)
5437 (setq meth t))
5438 ((> p end-range) nil)
5440 (setq name (concat package name) meth t)))
5441 (setq index (cons name marker))
5442 (if is-pack
5443 (push index index-pack-alist)
5444 (push index index-alist))
5445 (if meth (push index index-meth-alist))
5446 (push index index-unsorted-alist)))
5447 ((match-beginning 16) ; POD section
5448 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5449 marker (make-marker))
5450 (set-marker marker (match-beginning 17))
5451 (set-text-properties 0 (length name) nil name)
5452 (setq name (concat (make-string
5453 (* 3 (- (char-after (match-beginning 16)) ?1))
5454 ?\ )
5455 name)
5456 index (cons name marker))
5457 (setq index1 (cons (concat "=" name) (cdr index)))
5458 (push index index-pod-alist)
5459 (push index1 index-unsorted-alist)))))
5460 (setq index-alist
5461 (if (default-value 'imenu-sort-function)
5462 (sort index-alist (default-value 'imenu-sort-function))
5463 (nreverse index-alist)))
5464 (and index-pod-alist
5465 (push (cons "+POD headers+..."
5466 (nreverse index-pod-alist))
5467 index-alist))
5468 (and (or index-pack-alist index-meth-alist)
5469 (let ((lst index-pack-alist) hier-list pack elt group name)
5470 ;; Remove "package ", reverse and uniquify.
5471 (while lst
5472 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5473 (if (assoc name hier-list) nil
5474 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5475 (setq lst index-meth-alist)
5476 (while lst
5477 (setq elt (car lst) lst (cdr lst))
5478 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5479 (setq pack (substring (car elt) 0 (match-beginning 0)))
5480 (if (setq group (assoc pack hier-list))
5481 (if (listp (cdr group))
5482 ;; Have some functions already
5483 (setcdr group
5484 (cons (cons (substring
5485 (car elt)
5486 (+ 2 (match-beginning 0)))
5487 (cdr elt))
5488 (cdr group)))
5489 (setcdr group (list (cons (substring
5490 (car elt)
5491 (+ 2 (match-beginning 0)))
5492 (cdr elt)))))
5493 (setq hier-list
5494 (cons (cons pack
5495 (list (cons (substring
5496 (car elt)
5497 (+ 2 (match-beginning 0)))
5498 (cdr elt))))
5499 hier-list))))))
5500 (push (cons "+Hierarchy+..."
5501 hier-list)
5502 index-alist)))
5503 (and index-pack-alist
5504 (push (cons "+Packages+..."
5505 (nreverse index-pack-alist))
5506 index-alist))
5507 (and (or index-pack-alist index-pod-alist
5508 (default-value 'imenu-sort-function))
5509 index-unsorted-alist
5510 (push (cons "+Unsorted List+..."
5511 (nreverse index-unsorted-alist))
5512 index-alist))
5513 (cperl-imenu-addback index-alist)))
5516 ;; Suggested by Mark A. Hershberger
5517 (defun cperl-outline-level ()
5518 (looking-at outline-regexp)
5519 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5520 ;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5521 ((match-beginning 2) 0) ; package
5522 ((match-beginning 8) 1) ; sub
5523 ((match-beginning 16)
5524 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5525 (t 5))) ; should not happen
5528 (defun cperl-windowed-init ()
5529 "Initialization under windowed version."
5530 (cond ((featurep 'ps-print)
5531 (or cperl-faces-init
5532 (progn
5533 (and (boundp 'font-lock-multiline)
5534 (setq cperl-font-lock-multiline t))
5535 (cperl-init-faces))))
5536 ((not cperl-faces-init)
5537 (add-hook 'font-lock-mode-hook
5538 (function
5539 (lambda ()
5540 (if (memq major-mode '(perl-mode cperl-mode))
5541 (progn
5542 (or cperl-faces-init (cperl-init-faces)))))))
5543 (eval-after-load
5544 "ps-print"
5545 '(or cperl-faces-init (cperl-init-faces))))))
5547 (defvar cperl-font-lock-keywords-1 nil
5548 "Additional expressions to highlight in Perl mode. Minimal set.")
5549 (defvar cperl-font-lock-keywords nil
5550 "Additional expressions to highlight in Perl mode. Default set.")
5551 (defvar cperl-font-lock-keywords-2 nil
5552 "Additional expressions to highlight in Perl mode. Maximal set")
5554 (defun cperl-load-font-lock-keywords ()
5555 (or cperl-faces-init (cperl-init-faces))
5556 cperl-font-lock-keywords)
5558 (defun cperl-load-font-lock-keywords-1 ()
5559 (or cperl-faces-init (cperl-init-faces))
5560 cperl-font-lock-keywords-1)
5562 (defun cperl-load-font-lock-keywords-2 ()
5563 (or cperl-faces-init (cperl-init-faces))
5564 cperl-font-lock-keywords-2)
5566 (defun cperl-init-faces-weak ()
5567 ;; Allow `cperl-find-pods-heres' to run.
5568 (or (boundp 'font-lock-constant-face)
5569 (cperl-force-face font-lock-constant-face
5570 "Face for constant and label names"))
5571 (or (boundp 'font-lock-warning-face)
5572 (cperl-force-face font-lock-warning-face
5573 "Face for things which should stand out"))
5574 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5577 (defun cperl-init-faces ()
5578 (condition-case errs
5579 (progn
5580 (require 'font-lock)
5581 (and (fboundp 'font-lock-fontify-anchored-keywords)
5582 (featurep 'font-lock-extra)
5583 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5584 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5585 (if (fboundp 'font-lock-fontify-anchored-keywords)
5586 (setq font-lock-anchored t))
5587 (setq
5588 t-font-lock-keywords
5589 (list
5590 `("[ \t]+$" 0 ',cperl-invalid-face t)
5591 (cons
5592 (concat
5593 "\\(^\\|[^$@%&\\]\\)\\<\\("
5594 ;; FIXME: Use regexp-opt.
5595 (mapconcat
5596 #'identity
5597 (append
5598 cperl-sub-keywords
5599 '("if" "until" "while" "elsif" "else"
5600 "given" "when" "default" "break"
5601 "unless" "for"
5602 "try" "catch" "finally"
5603 "foreach" "continue" "exit" "die" "last" "goto" "next"
5604 "redo" "return" "local" "exec"
5605 "do" "dump"
5606 "use" "our"
5607 "require" "package" "eval" "evalbytes" "my" "state"
5608 "BEGIN" "END" "CHECK" "INIT" "UNITCHECK"))
5609 "\\|") ; Flow control
5610 "\\)\\>") 2) ; was "\\)[ \n\t;():,|&]"
5611 ; In what follows we use `type' style
5612 ; for overwritable builtins
5613 (list
5614 (concat
5615 "\\(^\\|[^$@%&\\]\\)\\<\\("
5616 ;; FIXME: Use regexp-opt.
5617 ;; "CORE" "__FILE__" "__LINE__" "__SUB__" "abs" "accept" "alarm"
5618 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5619 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5620 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5621 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5622 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5623 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fc" "fcntl"
5624 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5625 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5626 ;; "gethostbyname" "gethostent" "getlogin"
5627 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5628 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5629 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5630 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5631 ;; "getservbyport" "getservent" "getsockname"
5632 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5633 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5634 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5635 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5636 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5637 ;; "quotemeta" "rand" "read" "readdir" "readline"
5638 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5639 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5640 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5641 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5642 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5643 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5644 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5645 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5646 ;; "syscall" "sysopen" "sysread" "sysseek" "system" "syswrite" "tell"
5647 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5648 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5649 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5650 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5651 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5652 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5653 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5654 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5655 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5656 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5657 "f\\(ileno\\|c\\(ntl\\)?\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5658 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5659 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5660 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5661 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5662 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5663 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5664 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5665 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5666 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5667 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5668 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5669 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5670 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5671 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5672 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5673 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5674 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5675 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\|seek\\)\\|"
5676 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5677 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5678 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5679 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5680 "x\\(\\|or\\)\\|__\\(FILE\\|LINE\\|PACKAGE\\|SUB\\)__"
5681 "\\)\\>") 2 'font-lock-type-face)
5682 ;; In what follows we use `other' style
5683 ;; for nonoverwritable builtins
5684 ;; Somehow 's', 'm' are not auto-generated???
5685 (list
5686 (concat
5687 "\\(^\\|[^$@%&\\]\\)\\<\\("
5688 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "UNITCHECK" "__END__" "chomp"
5689 ;; "break" "chop" "default" "defined" "delete" "do" "each" "else" "elsif"
5690 ;; "eval" "evalbytes" "exists" "for" "foreach" "format" "given" "goto"
5691 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5692 ;; "no" "our" "package" "pop" "pos" "print" "printf" "prototype" "push"
5693 ;; "q" "qq" "qw" "qx" "redo" "return" "say" "scalar" "shift"
5694 ;; "sort" "splice" "split" "state" "study" "sub" "tie" "tr"
5695 ;; "undef" "unless" "unshift" "untie" "until" "use"
5696 ;; "when" "while" "y"
5697 "AUTOLOAD\\|BEGIN\\|\\(UNIT\\)?CHECK\\|break\\|c\\(atch\\|ho\\(p\\|mp\\)\\)\\|d\\(e\\(f\\(inally\\|ault\\|ined\\)\\|lete\\)\\|"
5698 "o\\)\\|DESTROY\\|e\\(ach\\|val\\(bytes\\)?\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5699 "END\\|for\\(\\|each\\|mat\\)\\|g\\(iven\\|rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5700 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5701 "p\\(ackage\\|rototype\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5702 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(ay\\|pli\\(ce\\|t\\)\\|"
5703 "calar\\|t\\(ate\\|udy\\)\\|ub\\|hift\\|ort\\)\\|t\\(ry?\\|ied?\\)\\|"
5704 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5705 "wh\\(en\\|ile\\)\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5706 "\\|[sm]" ; Added manually
5707 "\\)\\>")
5708 2 'cperl-nonoverridable-face)
5709 ;; (mapconcat #'identity
5710 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5711 ;; "#include" "#define" "#undef")
5712 ;; "\\|")
5713 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5714 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5715 ;; This highlights declarations and definitions differently.
5716 ;; We do not try to highlight in the case of attributes:
5717 ;; it is already done by `cperl-find-pods-heres'
5718 (list (concat "\\<" cperl-sub-regexp
5719 cperl-white-and-comment-rex ; whitespace/comments
5720 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5721 "\\("
5722 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5723 "([^()]*)\\)?" ; prototype
5724 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5725 "[{;]")
5726 2 (if cperl-font-lock-multiline
5727 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5728 'font-lock-function-name-face
5729 'font-lock-variable-name-face)
5730 ;; need to manually set 'multiline' for older font-locks
5731 '(progn
5732 (if (< 1 (count-lines (match-beginning 0)
5733 (match-end 0)))
5734 (put-text-property
5735 (+ 3 (match-beginning 0)) (match-end 0)
5736 'syntax-type 'multiline))
5737 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5738 'font-lock-function-name-face
5739 'font-lock-variable-name-face))))
5740 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5741 2 font-lock-function-name-face)
5742 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5743 1 font-lock-function-name-face)
5744 (cond ((featurep 'font-lock-extra)
5745 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5746 (2 font-lock-string-face t)
5747 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5748 (font-lock-anchored
5749 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5750 (2 font-lock-string-face t)
5751 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5752 nil nil
5753 (1 font-lock-string-face t))))
5754 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5755 2 font-lock-string-face t)))
5756 '("[[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5757 font-lock-string-face t)
5758 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5759 font-lock-constant-face) ; labels
5760 '("\\<\\(continue\\|next\\|last\\|redo\\|break\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5761 2 font-lock-constant-face)
5762 ;; Uncomment to get perl-mode-like vars
5763 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5764 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5765 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5766 (cond ((featurep 'font-lock-extra)
5767 '("^[ \t]*\\(state\\|my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5768 (3 font-lock-variable-name-face)
5769 (4 '(another 4 nil
5770 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5771 (1 font-lock-variable-name-face)
5772 (2 '(restart 2 nil) nil t)))
5773 nil t))) ; local variables, multiple
5774 (font-lock-anchored
5775 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5776 `(,(concat "\\<\\(state\\|my\\|local\\|our\\)"
5777 cperl-maybe-white-and-comment-rex
5778 "\\(("
5779 cperl-maybe-white-and-comment-rex
5780 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5781 (5 ,(if cperl-font-lock-multiline
5782 'font-lock-variable-name-face
5783 '(progn (setq cperl-font-lock-multiline-start
5784 (match-beginning 0))
5785 'font-lock-variable-name-face)))
5786 (,(concat "\\="
5787 cperl-maybe-white-and-comment-rex
5789 cperl-maybe-white-and-comment-rex
5790 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5791 ;; Bug in font-lock: limit is used not only to limit
5792 ;; searches, but to set the "extend window for
5793 ;; facification" property. Thus we need to minimize.
5794 ,(if cperl-font-lock-multiline
5795 '(if (match-beginning 3)
5796 (save-excursion
5797 (goto-char (match-beginning 3))
5798 (condition-case nil
5799 (forward-sexp 1)
5800 (error
5801 (condition-case nil
5802 (forward-char 200)
5803 (error nil)))) ; typeahead
5804 (1- (point))) ; report limit
5805 (forward-char -2)) ; disable continued expr
5806 '(if (match-beginning 3)
5807 (point-max) ; No limit for continuation
5808 (forward-char -2))) ; disable continued expr
5809 ,(if cperl-font-lock-multiline
5811 '(progn ; Do at end
5812 ;; "my" may be already fontified (POD),
5813 ;; so cperl-font-lock-multiline-start is nil
5814 (if (or (not cperl-font-lock-multiline-start)
5815 (> 2 (count-lines
5816 cperl-font-lock-multiline-start
5817 (point))))
5819 (put-text-property
5820 (1+ cperl-font-lock-multiline-start) (point)
5821 'syntax-type 'multiline))
5822 (setq cperl-font-lock-multiline-start nil)))
5823 (3 font-lock-variable-name-face))))
5824 (t '("^[ \t{}]*\\(state\\|my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5825 3 font-lock-variable-name-face)))
5826 '("\\<for\\(each\\)?\\([ \t]+\\(state\\|my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5827 4 font-lock-variable-name-face)
5828 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntactically
5829 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5830 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5831 (setq
5832 t-font-lock-keywords-1
5834 ("\\(\\([@%]\\|\\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5835 (if (eq (char-after (match-beginning 2)) ?%)
5836 'cperl-hash-face
5837 'cperl-array-face)
5838 t) ; arrays and hashes
5839 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5841 (if (= (- (match-end 2) (match-beginning 2)) 1)
5842 (if (eq (char-after (match-beginning 3)) ?{)
5843 'cperl-hash-face
5844 'cperl-array-face) ; arrays and hashes
5845 font-lock-variable-name-face) ; Just to put something
5847 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5848 (1 cperl-array-face)
5849 (2 font-lock-variable-name-face))
5850 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5851 (1 cperl-hash-face)
5852 (2 font-lock-variable-name-face))
5853 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5854 ;;; Too much noise from \s* @s[ and friends
5855 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
5856 ;;(3 font-lock-function-name-face t t)
5857 ;;(4
5858 ;; (if (cperl-slash-is-regexp)
5859 ;; font-lock-function-name-face 'default) nil t))
5861 (if cperl-highlight-variables-indiscriminately
5862 (setq t-font-lock-keywords-1
5863 (append t-font-lock-keywords-1
5864 (list '("\\([$*]{?\\(?:\\sw+\\|::\\)+\\)" 1
5865 font-lock-variable-name-face)))))
5866 (setq cperl-font-lock-keywords-1
5867 (if cperl-syntaxify-by-font-lock
5868 (cons 'cperl-fontify-update
5869 t-font-lock-keywords)
5870 t-font-lock-keywords)
5871 cperl-font-lock-keywords cperl-font-lock-keywords-1
5872 cperl-font-lock-keywords-2 (append
5873 cperl-font-lock-keywords-1
5874 t-font-lock-keywords-1)))
5875 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5876 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
5877 (eval ; Avoid a warning
5878 '(font-lock-require-faces
5879 (list
5880 ;; Color-light Color-dark Gray-light Gray-dark Mono
5881 (list 'font-lock-comment-face
5882 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5884 [nil nil t t t]
5885 [nil nil t t t]
5886 nil)
5887 (list 'font-lock-string-face
5888 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5891 [nil nil t t t]
5892 nil)
5893 (list 'font-lock-function-name-face
5894 (vector
5895 "Blue" "LightSkyBlue" "Gray50" "LightGray"
5896 (cdr (assq 'background-color ; if mono
5897 (frame-parameters))))
5898 (vector
5899 nil nil nil nil
5900 (cdr (assq 'foreground-color ; if mono
5901 (frame-parameters))))
5902 [nil nil t t t]
5904 nil)
5905 (list 'font-lock-variable-name-face
5906 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
5908 [nil nil t t t]
5909 [nil nil t t t]
5910 nil)
5911 (list 'font-lock-type-face
5912 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
5914 [nil nil t t t]
5916 [nil nil t t t])
5917 (list 'font-lock-warning-face
5918 ["Pink" "Red" "Gray50" "LightGray"]
5919 ["gray20" "gray90"
5920 "gray80" "gray20"]
5921 [nil nil t t t]
5923 [nil nil t t t]
5925 (list 'font-lock-constant-face
5926 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
5928 [nil nil t t t]
5930 [nil nil t t t])
5931 (list 'cperl-nonoverridable-face
5932 ["chartreuse3" ("orchid1" "orange")
5933 nil "Gray80"]
5934 [nil nil "gray90"]
5935 [nil nil nil t t]
5936 [nil nil t t]
5937 [nil nil t t t])
5938 (list 'cperl-array-face
5939 ["blue" "yellow" nil "Gray80"]
5940 ["lightyellow2" ("navy" "os2blue" "darkgreen")
5941 "gray90"]
5944 nil)
5945 (list 'cperl-hash-face
5946 ["red" "red" nil "Gray80"]
5947 ["lightyellow2" ("navy" "os2blue" "darkgreen")
5948 "gray90"]
5951 nil))))
5952 ;; Do it the dull way, without choose-color
5953 (defvar cperl-guessed-background nil
5954 "Display characteristics as guessed by cperl.")
5955 (cperl-force-face font-lock-constant-face
5956 "Face for constant and label names")
5957 (cperl-force-face font-lock-variable-name-face
5958 "Face for variable names")
5959 (cperl-force-face font-lock-type-face
5960 "Face for data types")
5961 (cperl-force-face cperl-nonoverridable-face
5962 "Face for data types from another group")
5963 (cperl-force-face font-lock-warning-face
5964 "Face for things which should stand out")
5965 (cperl-force-face font-lock-comment-face
5966 "Face for comments")
5967 (cperl-force-face font-lock-function-name-face
5968 "Face for function names")
5969 (cperl-force-face cperl-hash-face
5970 "Face for hashes")
5971 (cperl-force-face cperl-array-face
5972 "Face for arrays")
5973 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
5974 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
5975 ;;(or (boundp 'font-lock-type-face)
5976 ;; (defconst font-lock-type-face
5977 ;; 'font-lock-type-face
5978 ;; "Face to use for data types."))
5979 ;;(or (boundp 'cperl-nonoverridable-face)
5980 ;; (defconst cperl-nonoverridable-face
5981 ;; 'cperl-nonoverridable-face
5982 ;; "Face to use for data types from another group."))
5983 ;;(if (not (featurep 'xemacs)) nil
5984 ;; (or (boundp 'font-lock-comment-face)
5985 ;; (defconst font-lock-comment-face
5986 ;; 'font-lock-comment-face
5987 ;; "Face to use for comments."))
5988 ;; (or (boundp 'font-lock-keyword-face)
5989 ;; (defconst font-lock-keyword-face
5990 ;; 'font-lock-keyword-face
5991 ;; "Face to use for keywords."))
5992 ;; (or (boundp 'font-lock-function-name-face)
5993 ;; (defconst font-lock-function-name-face
5994 ;; 'font-lock-function-name-face
5995 ;; "Face to use for function names.")))
5996 (if (and
5997 (not (cperl-is-face 'cperl-array-face))
5998 (cperl-is-face 'font-lock-emphasized-face))
5999 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6000 (if (and
6001 (not (cperl-is-face 'cperl-hash-face))
6002 (cperl-is-face 'font-lock-other-emphasized-face))
6003 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6004 (if (and
6005 (not (cperl-is-face 'cperl-nonoverridable-face))
6006 (cperl-is-face 'font-lock-other-type-face))
6007 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6008 ;;(or (boundp 'cperl-hash-face)
6009 ;; (defconst cperl-hash-face
6010 ;; 'cperl-hash-face
6011 ;; "Face to use for hashes."))
6012 ;;(or (boundp 'cperl-array-face)
6013 ;; (defconst cperl-array-face
6014 ;; 'cperl-array-face
6015 ;; "Face to use for arrays."))
6016 ;; Here we try to guess background
6017 (let ((background
6018 (if (boundp 'font-lock-background-mode)
6019 font-lock-background-mode
6020 'light)))
6021 (defvar cperl-guessed-background
6022 (if (and (boundp 'font-lock-display-type)
6023 (eq font-lock-display-type 'grayscale))
6024 'gray
6025 background)
6026 "Background as guessed by CPerl mode")
6027 (and (not (cperl-is-face 'font-lock-constant-face))
6028 (cperl-is-face 'font-lock-reference-face)
6029 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6030 (if (cperl-is-face 'font-lock-type-face) nil
6031 (copy-face 'default 'font-lock-type-face)
6032 (cond
6033 ((eq background 'light)
6034 (set-face-foreground 'font-lock-type-face
6035 (if (x-color-defined-p "seagreen")
6036 "seagreen"
6037 "sea green")))
6038 ((eq background 'dark)
6039 (set-face-foreground 'font-lock-type-face
6040 (if (x-color-defined-p "os2pink")
6041 "os2pink"
6042 "pink")))
6044 (set-face-background 'font-lock-type-face "gray90"))))
6045 (if (cperl-is-face 'cperl-nonoverridable-face)
6047 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6048 (cond
6049 ((eq background 'light)
6050 (set-face-foreground 'cperl-nonoverridable-face
6051 (if (x-color-defined-p "chartreuse3")
6052 "chartreuse3"
6053 "chartreuse")))
6054 ((eq background 'dark)
6055 (set-face-foreground 'cperl-nonoverridable-face
6056 (if (x-color-defined-p "orchid1")
6057 "orchid1"
6058 "orange")))))
6059 ;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6060 ;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6061 ;; (cond
6062 ;; ((eq background 'light)
6063 ;; (set-face-background 'font-lock-other-emphasized-face
6064 ;; (if (x-color-defined-p "lightyellow2")
6065 ;; "lightyellow2"
6066 ;; (if (x-color-defined-p "lightyellow")
6067 ;; "lightyellow"
6068 ;; "light yellow"))))
6069 ;; ((eq background 'dark)
6070 ;; (set-face-background 'font-lock-other-emphasized-face
6071 ;; (if (x-color-defined-p "navy")
6072 ;; "navy"
6073 ;; (if (x-color-defined-p "darkgreen")
6074 ;; "darkgreen"
6075 ;; "dark green"))))
6076 ;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6077 ;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6078 ;; (copy-face 'bold 'font-lock-emphasized-face)
6079 ;; (cond
6080 ;; ((eq background 'light)
6081 ;; (set-face-background 'font-lock-emphasized-face
6082 ;; (if (x-color-defined-p "lightyellow2")
6083 ;; "lightyellow2"
6084 ;; "lightyellow")))
6085 ;; ((eq background 'dark)
6086 ;; (set-face-background 'font-lock-emphasized-face
6087 ;; (if (x-color-defined-p "navy")
6088 ;; "navy"
6089 ;; (if (x-color-defined-p "darkgreen")
6090 ;; "darkgreen"
6091 ;; "dark green"))))
6092 ;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6093 (if (cperl-is-face 'font-lock-variable-name-face) nil
6094 (copy-face 'italic 'font-lock-variable-name-face))
6095 (if (cperl-is-face 'font-lock-constant-face) nil
6096 (copy-face 'italic 'font-lock-constant-face))))
6097 (setq cperl-faces-init t))
6098 (error (message "cperl-init-faces (ignored): %s" errs))))
6101 (defvar ps-bold-faces)
6102 (defvar ps-italic-faces)
6103 (defvar ps-underlined-faces)
6105 (defun cperl-ps-print-init ()
6106 "Initialization of `ps-print' components for faces used in CPerl."
6107 (eval-after-load "ps-print"
6108 '(setq ps-bold-faces
6109 ;; font-lock-variable-name-face
6110 ;; font-lock-constant-face
6111 (append '(cperl-array-face cperl-hash-face)
6112 ps-bold-faces)
6113 ps-italic-faces
6114 ;; font-lock-constant-face
6115 (append '(cperl-nonoverridable-face cperl-hash-face)
6116 ps-italic-faces)
6117 ps-underlined-faces
6118 ;; font-lock-type-face
6119 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6120 ps-underlined-faces))))
6122 (defvar ps-print-face-extension-alist)
6124 (defun cperl-ps-print (&optional file)
6125 "Pretty-print in CPerl style.
6126 If optional argument FILE is an empty string, prints to printer, otherwise
6127 to the file FILE. If FILE is nil, prompts for a file name.
6129 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6130 (interactive)
6131 (or file
6132 (setq file (read-from-minibuffer
6133 "Print to file (if empty - to printer): "
6134 (concat (buffer-file-name) ".ps")
6135 nil nil 'file-name-history)))
6136 (or (> (length file) 0)
6137 (setq file nil))
6138 (require 'ps-print) ; To get ps-print-face-extension-alist
6139 (let ((ps-print-color-p t)
6140 (ps-print-face-extension-alist ps-print-face-extension-alist))
6141 (ps-extend-face-list cperl-ps-print-face-properties)
6142 (ps-print-buffer-with-faces file)))
6144 ;; (defun cperl-ps-print-init ()
6145 ;; "Initialization of `ps-print' components for faces used in CPerl."
6146 ;; ;; Guard against old versions
6147 ;; (defvar ps-underlined-faces nil)
6148 ;; (defvar ps-bold-faces nil)
6149 ;; (defvar ps-italic-faces nil)
6150 ;; (setq ps-bold-faces
6151 ;; (append '(font-lock-emphasized-face
6152 ;; cperl-array-face
6153 ;; font-lock-keyword-face
6154 ;; font-lock-variable-name-face
6155 ;; font-lock-constant-face
6156 ;; font-lock-reference-face
6157 ;; font-lock-other-emphasized-face
6158 ;; cperl-hash-face)
6159 ;; ps-bold-faces))
6160 ;; (setq ps-italic-faces
6161 ;; (append '(cperl-nonoverridable-face
6162 ;; font-lock-constant-face
6163 ;; font-lock-reference-face
6164 ;; font-lock-other-emphasized-face
6165 ;; cperl-hash-face)
6166 ;; ps-italic-faces))
6167 ;; (setq ps-underlined-faces
6168 ;; (append '(font-lock-emphasized-face
6169 ;; cperl-array-face
6170 ;; font-lock-other-emphasized-face
6171 ;; cperl-hash-face
6172 ;; cperl-nonoverridable-face font-lock-type-face)
6173 ;; ps-underlined-faces))
6174 ;; (cons 'font-lock-type-face ps-underlined-faces))
6177 (cperl-windowed-init)
6179 (defconst cperl-styles-entries
6180 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6181 cperl-label-offset cperl-extra-newline-before-brace
6182 cperl-extra-newline-before-brace-multiline
6183 cperl-merge-trailing-else
6184 cperl-continued-statement-offset))
6186 (defconst cperl-style-examples
6187 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6188 ##### cperl-continued-brace-offset cperl-label-offset
6189 ##### cperl-continued-statement-offset
6190 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6192 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6194 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6195 if (foo) {
6197 baz;
6198 label:
6200 boon;
6202 } else {
6203 stop;
6206 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6207 if (foo) {
6209 baz;
6210 label:
6212 boon;
6214 } else {
6215 stop;
6218 ### GNU 2/0/0/-2/2/nil/t
6219 if (foo)
6222 baz;
6223 label:
6225 boon;
6228 else
6230 stop;
6233 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6234 if (foo)
6237 baz;
6238 label:
6240 boon;
6243 else
6245 stop;
6248 ### BSD (=C++, but will not change preexisting merge-trailing-else
6249 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6250 if (foo)
6253 baz;
6254 label:
6256 boon;
6259 else
6261 stop;
6264 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6265 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6266 if (foo)
6269 baz;
6270 label:
6272 boon;
6275 else
6277 stop;
6280 ### Whitesmith (=PerlStyle, but will not change preexisting
6281 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6282 if (foo)
6285 baz;
6286 label:
6288 boon;
6291 else
6293 stop;
6296 "Examples of if/else with different indent styles (with v4.23).")
6298 (defconst cperl-style-alist
6299 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6300 (cperl-indent-level . 2)
6301 (cperl-brace-offset . 0)
6302 (cperl-continued-brace-offset . 0)
6303 (cperl-label-offset . -2)
6304 (cperl-continued-statement-offset . 2)
6305 (cperl-extra-newline-before-brace . nil)
6306 (cperl-extra-newline-before-brace-multiline . nil)
6307 (cperl-merge-trailing-else . t))
6309 ("PerlStyle" ; CPerl with 4 as indent
6310 (cperl-indent-level . 4)
6311 (cperl-brace-offset . 0)
6312 (cperl-continued-brace-offset . 0)
6313 (cperl-label-offset . -4)
6314 (cperl-continued-statement-offset . 4)
6315 (cperl-extra-newline-before-brace . nil)
6316 (cperl-extra-newline-before-brace-multiline . nil)
6317 (cperl-merge-trailing-else . t))
6319 ("GNU"
6320 (cperl-indent-level . 2)
6321 (cperl-brace-offset . 0)
6322 (cperl-continued-brace-offset . 0)
6323 (cperl-label-offset . -2)
6324 (cperl-continued-statement-offset . 2)
6325 (cperl-extra-newline-before-brace . t)
6326 (cperl-extra-newline-before-brace-multiline . t)
6327 (cperl-merge-trailing-else . nil))
6329 ("K&R"
6330 (cperl-indent-level . 5)
6331 (cperl-brace-offset . 0)
6332 (cperl-continued-brace-offset . -5)
6333 (cperl-label-offset . -5)
6334 (cperl-continued-statement-offset . 5)
6335 ;;(cperl-extra-newline-before-brace . nil) ; ???
6336 ;;(cperl-extra-newline-before-brace-multiline . nil)
6337 (cperl-merge-trailing-else . nil))
6339 ("BSD"
6340 (cperl-indent-level . 4)
6341 (cperl-brace-offset . 0)
6342 (cperl-continued-brace-offset . -4)
6343 (cperl-label-offset . -4)
6344 (cperl-continued-statement-offset . 4)
6345 ;;(cperl-extra-newline-before-brace . nil) ; ???
6346 ;;(cperl-extra-newline-before-brace-multiline . nil)
6347 ;;(cperl-merge-trailing-else . nil) ; ???
6350 ("C++"
6351 (cperl-indent-level . 4)
6352 (cperl-brace-offset . 0)
6353 (cperl-continued-brace-offset . -4)
6354 (cperl-label-offset . -4)
6355 (cperl-continued-statement-offset . 4)
6356 (cperl-extra-newline-before-brace . t)
6357 (cperl-extra-newline-before-brace-multiline . t)
6358 (cperl-merge-trailing-else . nil))
6360 ("Whitesmith"
6361 (cperl-indent-level . 4)
6362 (cperl-brace-offset . 0)
6363 (cperl-continued-brace-offset . 0)
6364 (cperl-label-offset . -4)
6365 (cperl-continued-statement-offset . 4)
6366 ;;(cperl-extra-newline-before-brace . nil) ; ???
6367 ;;(cperl-extra-newline-before-brace-multiline . nil)
6368 ;;(cperl-merge-trailing-else . nil) ; ???
6370 ("Current"))
6371 "List of variables to set to get a particular indentation style.
6372 Should be used via `cperl-set-style' or via Perl menu.
6374 See examples in `cperl-style-examples'.")
6376 (defun cperl-set-style (style)
6377 "Set CPerl mode variables to use one of several different indentation styles.
6378 The arguments are a string representing the desired style.
6379 The list of styles is in `cperl-style-alist', available styles
6380 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6382 The current value of style is memorized (unless there is a memorized
6383 data already), may be restored by `cperl-set-style-back'.
6385 Choosing \"Current\" style will not change style, so this may be used for
6386 side-effect of memorizing only. Examples in `cperl-style-examples'."
6387 (interactive
6388 (list (completing-read "Enter style: " cperl-style-alist nil 'insist)))
6389 (or cperl-old-style
6390 (setq cperl-old-style
6391 (mapcar (function
6392 (lambda (name)
6393 (cons name (eval name))))
6394 cperl-styles-entries)))
6395 (let ((style (cdr (assoc style cperl-style-alist))) setting)
6396 (while style
6397 (setq setting (car style) style (cdr style))
6398 (set (car setting) (cdr setting)))))
6400 (defun cperl-set-style-back ()
6401 "Restore a style memorized by `cperl-set-style'."
6402 (interactive)
6403 (or cperl-old-style (error "The style was not changed"))
6404 (let (setting)
6405 (while cperl-old-style
6406 (setq setting (car cperl-old-style)
6407 cperl-old-style (cdr cperl-old-style))
6408 (set (car setting) (cdr setting)))))
6410 (defvar perl-dbg-flags)
6411 (defun cperl-check-syntax ()
6412 (interactive)
6413 (require 'mode-compile)
6414 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6415 (eval '(mode-compile)))) ; Avoid a warning
6417 (declare-function Info-find-node "info"
6418 (filename nodename &optional no-going-back strict-case))
6420 (defun cperl-info-buffer (type)
6421 ;; Returns buffer with documentation. Creates if missing.
6422 ;; If TYPE, this vars buffer.
6423 ;; Special care is taken to not stomp over an existing info buffer
6424 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6425 (info (get-buffer bname))
6426 (oldbuf (get-buffer "*info*")))
6427 (if info info
6428 (save-window-excursion
6429 ;; Get Info running
6430 (require 'info)
6431 (cond (oldbuf
6432 (set-buffer oldbuf)
6433 (rename-buffer "*info-perl-tmp*")))
6434 (save-window-excursion
6435 (info))
6436 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6437 (set-buffer "*info*")
6438 (rename-buffer bname)
6439 (cond (oldbuf
6440 (set-buffer "*info-perl-tmp*")
6441 (rename-buffer "*info*")
6442 (set-buffer bname)))
6443 (set (make-local-variable 'window-min-height) 2)
6444 (current-buffer)))))
6446 (defun cperl-word-at-point (&optional p)
6447 "Return the word at point or at P."
6448 (save-excursion
6449 (if p (goto-char p))
6450 (or (cperl-word-at-point-hard)
6451 (progn
6452 (require 'etags)
6453 (funcall (or (and (boundp 'find-tag-default-function)
6454 find-tag-default-function)
6455 (get major-mode 'find-tag-default-function)
6456 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6457 ;; automatically used within `find-tag-default':
6458 'find-tag-default))))))
6460 (defun cperl-info-on-command (command)
6461 "Show documentation for Perl command COMMAND in other window.
6462 If perl-info buffer is shown in some frame, uses this frame.
6463 Customized by setting variables `cperl-shrink-wrap-info-frame',
6464 `cperl-max-help-size'."
6465 (interactive
6466 (let* ((default (cperl-word-at-point))
6467 (read (read-string
6468 (format "Find doc for Perl function (default %s): "
6469 default))))
6470 (list (if (equal read "")
6471 default
6472 read))))
6474 (let ((cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6475 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6476 max-height char-height buf-list)
6477 (if (string-match "^-[a-zA-Z]$" command)
6478 (setq cmd-desc "^-X[ \t\n]"))
6479 (setq isvar (string-match "^[$@%]" command)
6480 buf (cperl-info-buffer isvar)
6481 iniwin (selected-window)
6482 fr1 (window-frame iniwin))
6483 (set-buffer buf)
6484 (goto-char (point-min))
6485 (or isvar
6486 (progn (re-search-forward "^-X[ \t\n]")
6487 (forward-line -1)))
6488 (if (re-search-forward cmd-desc nil t)
6489 (progn
6490 ;; Go back to beginning of the group (ex, for qq)
6491 (if (re-search-backward "^[ \t\n\f]")
6492 (forward-line 1))
6493 (beginning-of-line)
6494 ;; Get some of
6495 (setq pos (point)
6496 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6497 (while (and (not win) buf-list)
6498 (setq win (get-buffer-window (car buf-list) t))
6499 (setq buf-list (cdr buf-list)))
6500 (or (not win)
6501 (eq (window-buffer win) buf)
6502 (set-window-buffer win buf))
6503 (and win (setq fr2 (window-frame win)))
6504 (if (or (not fr2) (eq fr1 fr2))
6505 (pop-to-buffer buf)
6506 (special-display-popup-frame buf) ; Make it visible
6507 (select-window win))
6508 (goto-char pos) ; Needed (?!).
6509 ;; Resize
6510 (setq iniheight (window-height)
6511 frheight (frame-height)
6512 not-loner (< iniheight (1- frheight))) ; Are not alone
6513 (cond ((if not-loner cperl-max-help-size
6514 cperl-shrink-wrap-info-frame)
6515 (setq height
6516 (+ 2
6517 (count-lines
6519 (save-excursion
6520 (if (re-search-forward
6521 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6522 (match-beginning 0) (point-max)))))
6523 max-height
6524 (if not-loner
6525 (/ (* (- frheight 3) cperl-max-help-size) 100)
6526 (setq char-height (frame-char-height))
6527 (if (eq char-height 1) (setq char-height 18))
6528 ;; Title, menubar, + 2 for slack
6529 (- (/ (display-pixel-height) char-height) 4)))
6530 (if (> height max-height) (setq height max-height))
6531 ;;(message "was %s doing %s" iniheight height)
6532 (if not-loner
6533 (enlarge-window (- height iniheight))
6534 (set-frame-height (window-frame win) (1+ height)))))
6535 (set-window-start (selected-window) pos))
6536 (message "No entry for %s found." command))
6537 ;;(pop-to-buffer buffer)
6538 (select-window iniwin)))
6540 (defun cperl-info-on-current-command ()
6541 "Show documentation for Perl command at point in other window."
6542 (interactive)
6543 (cperl-info-on-command (cperl-word-at-point)))
6545 (defun cperl-imenu-info-imenu-search ()
6546 (if (looking-at "^-X[ \t\n]") nil
6547 (re-search-backward
6548 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6549 (forward-line 1)))
6551 (defun cperl-imenu-info-imenu-name ()
6552 (buffer-substring
6553 (match-beginning 1) (match-end 1)))
6555 (declare-function imenu-choose-buffer-index "imenu" (&optional prompt alist))
6557 (defun cperl-imenu-on-info ()
6558 "Shows imenu for Perl Info Buffer.
6559 Opens Perl Info buffer if needed."
6560 (interactive)
6561 (require 'imenu)
6562 (let* ((buffer (current-buffer))
6563 imenu-create-index-function
6564 imenu-prev-index-position-function
6565 imenu-extract-index-name-function
6566 (index-item (save-restriction
6567 (save-window-excursion
6568 (set-buffer (cperl-info-buffer nil))
6569 (setq imenu-create-index-function
6570 'imenu-default-create-index-function
6571 imenu-prev-index-position-function
6572 #'cperl-imenu-info-imenu-search
6573 imenu-extract-index-name-function
6574 #'cperl-imenu-info-imenu-name)
6575 (imenu-choose-buffer-index)))))
6576 (and index-item
6577 (progn
6578 (push-mark)
6579 (pop-to-buffer "*info-perl*")
6580 (cond
6581 ((markerp (cdr index-item))
6582 (goto-char (marker-position (cdr index-item))))
6584 (goto-char (cdr index-item))))
6585 (set-window-start (selected-window) (point))
6586 (pop-to-buffer buffer)))))
6588 (defun cperl-lineup (beg end &optional step minshift)
6589 "Lineup construction in a region.
6590 Beginning of region should be at the start of a construction.
6591 All first occurrences of this construction in the lines that are
6592 partially contained in the region are lined up at the same column.
6594 MINSHIFT is the minimal amount of space to insert before the construction.
6595 STEP is the tabwidth to position constructions.
6596 If STEP is nil, `cperl-lineup-step' will be used
6597 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6598 Will not move the position at the start to the left."
6599 (interactive "r")
6600 (let (search col tcol seen)
6601 (save-excursion
6602 (goto-char end)
6603 (end-of-line)
6604 (setq end (point-marker))
6605 (goto-char beg)
6606 (skip-chars-forward " \t\f")
6607 (setq beg (point-marker))
6608 (indent-region beg end nil)
6609 (goto-char beg)
6610 (setq col (current-column))
6611 (if (looking-at "[a-zA-Z0-9_]")
6612 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6613 (setq search
6614 (concat "\\<"
6615 (regexp-quote
6616 (buffer-substring (match-beginning 0)
6617 (match-end 0))) "\\>"))
6618 (error "Cannot line up in a middle of the word"))
6619 (if (looking-at "$")
6620 (error "Cannot line up end of line"))
6621 (setq search (regexp-quote (char-to-string (following-char)))))
6622 (setq step (or step cperl-lineup-step cperl-indent-level))
6623 (or minshift (setq minshift 1))
6624 (while (progn
6625 (beginning-of-line 2)
6626 (and (< (point) end)
6627 (re-search-forward search end t)
6628 (goto-char (match-beginning 0))))
6629 (setq tcol (current-column) seen t)
6630 (if (> tcol col) (setq col tcol)))
6631 (or seen
6632 (error "The construction to line up occurred only once"))
6633 (goto-char beg)
6634 (setq col (+ col minshift))
6635 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6636 (while
6637 (progn
6638 (cperl-make-indent col)
6639 (beginning-of-line 2)
6640 (and (< (point) end)
6641 (re-search-forward search end t)
6642 (goto-char (match-beginning 0)))))))) ; No body
6644 (defun cperl-etags (&optional add all files) ;; NOT USED???
6645 "Run etags with appropriate options for Perl files.
6646 If optional argument ALL is `recursive', will process Perl files
6647 in subdirectories too."
6648 (interactive)
6649 (let ((cmd "etags")
6650 (args '("-l" "none" "-r"
6651 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6652 "/\\<" cperl-sub-regexp "[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6653 "-r"
6654 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6655 "-r"
6656 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6657 res)
6658 (if add (setq args (cons "-a" args)))
6659 (or files (setq files (list buffer-file-name)))
6660 (cond
6661 ((eq all 'recursive)
6662 ;;(error "Not implemented: recursive")
6663 (setq args (append (list "-e"
6664 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6665 use File::Find;
6666 find(\\&wanted, '.');
6667 exec @ARGV;"
6668 cmd) args)
6669 cmd "perl"))
6670 (all
6671 ;;(error "Not implemented: all")
6672 (setq args (append (list "-e"
6673 "push @ARGV, <*.PL *.pl *.pm>;
6674 exec @ARGV;"
6675 cmd) args)
6676 cmd "perl"))
6678 (setq args (append args files))))
6679 (setq res (apply 'call-process cmd nil nil nil args))
6680 (or (eq res 0)
6681 (message "etags returned \"%s\"" res))))
6683 (defun cperl-toggle-auto-newline ()
6684 "Toggle the state of `cperl-auto-newline'."
6685 (interactive)
6686 (setq cperl-auto-newline (not cperl-auto-newline))
6687 (message "Newlines will %sbe auto-inserted now."
6688 (if cperl-auto-newline "" "not ")))
6690 (defun cperl-toggle-abbrev ()
6691 "Toggle the state of automatic keyword expansion in CPerl mode."
6692 (interactive)
6693 (abbrev-mode (if abbrev-mode 0 1))
6694 (message "Perl control structure will %sbe auto-inserted now."
6695 (if abbrev-mode "" "not ")))
6698 (defun cperl-toggle-electric ()
6699 "Toggle the state of parentheses doubling in CPerl mode."
6700 (interactive)
6701 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6702 (message "Parentheses will %sbe auto-doubled now."
6703 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6705 (defun cperl-toggle-autohelp ()
6706 ;; FIXME: Turn me into a minor mode. Fix menu entries for "Auto-help on" as
6707 ;; well.
6708 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6709 Delay of auto-help controlled by `cperl-lazy-help-time'."
6710 (interactive)
6711 (if cperl-lazy-installed
6712 (cperl-lazy-unstall)
6713 (cperl-lazy-install))
6714 (message "Perl help messages will %sbe automatically shown now."
6715 (if cperl-lazy-installed "" "not ")))
6717 (defun cperl-toggle-construct-fix ()
6718 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6719 (interactive)
6720 (setq cperl-indent-region-fix-constructs
6721 (if cperl-indent-region-fix-constructs
6724 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6725 (if cperl-indent-region-fix-constructs "" "not ")))
6727 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6728 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6729 Nonpositive numeric argument disables debugging messages. The message
6730 summarizes which regions it was decided to rescan for syntactic constructs.
6732 The message looks like this:
6734 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6736 Numbers are character positions in the buffer. REQ provides the range to
6737 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6738 for correct operation it should start and end outside any special syntactic
6739 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6740 by CPerl."
6741 (interactive "P")
6742 (or arg
6743 (setq arg (if (eq cperl-syntaxify-by-font-lock
6744 (if backtrace 'backtrace 'message))
6745 0 1)))
6746 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6747 (setq cperl-syntaxify-by-font-lock arg)
6748 (message "Debugging messages of syntax unwind %sabled."
6749 (if (eq arg t) "dis" "en")))
6751 ;;;; Tags file creation.
6753 (defvar cperl-tmp-buffer " *cperl-tmp*")
6755 (defun cperl-setup-tmp-buf ()
6756 (set-buffer (get-buffer-create cperl-tmp-buffer))
6757 (set-syntax-table cperl-mode-syntax-table)
6758 (buffer-disable-undo)
6759 (auto-fill-mode 0)
6760 (if cperl-use-syntax-table-text-property-for-tags
6761 (progn
6762 ;; Do not introduce variable if not needed, we check it!
6763 (set (make-local-variable 'parse-sexp-lookup-properties) t))))
6765 ;; Copied from imenu-example--name-and-position.
6766 (defvar imenu-use-markers)
6768 (defun cperl-imenu-name-and-position ()
6769 "Return the current/previous sexp and its (beginning) location.
6770 Does not move point."
6771 (save-excursion
6772 (forward-sexp -1)
6773 (let ((beg (if imenu-use-markers (point-marker) (point)))
6774 (end (progn (forward-sexp) (point))))
6775 (cons (buffer-substring beg end)
6776 beg))))
6778 (defun cperl-xsub-scan ()
6779 (require 'imenu)
6780 (let ((index-alist '())
6781 index index1 name package prefix)
6782 (goto-char (point-min))
6783 ;; Search for the function
6784 (progn ;;save-match-data
6785 (while (re-search-forward
6786 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6787 nil t)
6788 (cond
6789 ((match-beginning 2) ; SECTION
6790 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6791 (goto-char (match-beginning 0))
6792 (skip-chars-forward " \t")
6793 (forward-char 1)
6794 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6795 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6796 (setq prefix nil)))
6797 ((not package) nil) ; C language section
6798 ((match-beginning 3) ; XSUB
6799 (goto-char (1+ (match-beginning 3)))
6800 (setq index (cperl-imenu-name-and-position))
6801 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6802 (if (and prefix (string-match (concat "^" prefix) name))
6803 (setq name (substring name (length prefix))))
6804 (cond ((string-match "::" name) nil)
6806 (setq index1 (cons (concat package "::" name) (cdr index)))
6807 (push index1 index-alist)))
6808 (setcar index name)
6809 (push index index-alist))
6810 (t ; BOOT: section
6811 ;; (beginning-of-line)
6812 (setq index (cperl-imenu-name-and-position))
6813 (setcar index (concat package "::BOOT:"))
6814 (push index index-alist)))))
6815 index-alist))
6817 (defvar cperl-unreadable-ok nil)
6819 (defun cperl-find-tags (ifile xs topdir)
6820 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6821 (cperl-pod-here-fontify nil) file)
6822 (save-excursion
6823 (if b (set-buffer b)
6824 (cperl-setup-tmp-buf))
6825 (erase-buffer)
6826 (condition-case nil
6827 (setq file (car (insert-file-contents ifile)))
6828 (error (if cperl-unreadable-ok nil
6829 (if (y-or-n-p
6830 (format "File %s unreadable. Continue? " ifile))
6831 (setq cperl-unreadable-ok t)
6832 (error "Aborting: unreadable file %s" ifile)))))
6833 (if (not file)
6834 (message "Unreadable file %s" ifile)
6835 (message "Scanning file %s ..." file)
6836 (if (and cperl-use-syntax-table-text-property-for-tags
6837 (not xs))
6838 (condition-case err ; after __END__ may have garbage
6839 (cperl-find-pods-heres nil nil noninteractive)
6840 (error (message "While scanning for syntax: %S" err))))
6841 (if xs
6842 (setq lst (cperl-xsub-scan))
6843 (setq ind (cperl-imenu--create-perl-index))
6844 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6845 (setq lst
6846 (mapcar
6847 (function
6848 (lambda (elt)
6849 (cond ((string-match "^[_a-zA-Z]" (car elt))
6850 (goto-char (cdr elt))
6851 (beginning-of-line) ; pos should be of the start of the line
6852 (list (car elt)
6853 (point)
6854 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6855 (buffer-substring (progn
6856 (goto-char (cdr elt))
6857 ;; After name now...
6858 (or (eolp) (forward-char 1))
6859 (point))
6860 (progn
6861 (beginning-of-line)
6862 (point))))))))
6863 lst))
6864 (erase-buffer)
6865 (while lst
6866 (setq elt (car lst) lst (cdr lst))
6867 (if elt
6868 (progn
6869 (insert (elt elt 3)
6871 (if (string-match "^package " (car elt))
6872 (substring (car elt) 8)
6873 (car elt) )
6875 (number-to-string (elt elt 2)) ; Line
6877 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
6878 "\n")
6879 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
6880 (string-match (concat "^" cperl-sub-regexp "[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]")
6881 (elt elt 3)))
6882 ;; Need to insert the name without package as well
6883 (setq lst (cons (cons (substring (elt elt 3)
6884 (match-beginning 1)
6885 (match-end 1))
6886 (cdr elt))
6887 lst))))))
6888 (setq pos (point))
6889 (goto-char 1)
6890 (setq rel file)
6891 ;; On case-preserving filesystems case might be encoded in properties
6892 (set-text-properties 0 (length rel) nil rel)
6893 (and (equal topdir (substring rel 0 (length topdir)))
6894 (setq rel (substring file (length topdir))))
6895 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
6896 (setq ret (buffer-substring 1 (point-max)))
6897 (erase-buffer)
6898 (or noninteractive
6899 (message "Scanning file %s finished" file))
6900 ret))))
6902 (defun cperl-add-tags-recurse-noxs ()
6903 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
6904 Use as
6905 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6906 -f cperl-add-tags-recurse-noxs
6908 (cperl-write-tags nil nil t t nil t))
6910 (defun cperl-add-tags-recurse-noxs-fullpath ()
6911 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
6912 Writes down fullpath, so TAGS is relocatable (but if the build directory
6913 is relocated, the file TAGS inside it breaks). Use as
6914 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6915 -f cperl-add-tags-recurse-noxs-fullpath
6917 (cperl-write-tags nil nil t t nil t ""))
6919 (defun cperl-add-tags-recurse ()
6920 "Add to TAGS file data for Perl files in the current directory and kids.
6921 Use as
6922 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6923 -f cperl-add-tags-recurse
6925 (cperl-write-tags nil nil t t))
6927 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
6928 ;; If INBUFFER, do not select buffer, and do not save
6929 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
6930 (require 'etags)
6931 (if file nil
6932 (setq file (if dir default-directory (buffer-file-name)))
6933 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
6934 (or topdir
6935 (setq topdir default-directory))
6936 (let ((tags-file-name "TAGS")
6937 (case-fold-search (and (featurep 'xemacs) (eq system-type 'emx)))
6938 xs rel)
6939 (save-excursion
6940 (cond (inbuffer nil) ; Already there
6941 ((file-exists-p tags-file-name)
6942 (if (featurep 'xemacs)
6943 (visit-tags-table-buffer)
6944 (visit-tags-table-buffer tags-file-name)))
6945 (t (set-buffer (find-file-noselect tags-file-name))))
6946 (cond
6947 (dir
6948 (cond ((eq erase 'ignore))
6949 (erase
6950 (erase-buffer)
6951 (setq erase 'ignore)))
6952 (let ((files
6953 (condition-case nil
6954 (directory-files file t
6955 (if recurse nil cperl-scan-files-regexp)
6957 (error
6958 (if cperl-unreadable-ok nil
6959 (if (y-or-n-p
6960 (format "Directory %s unreadable. Continue? " file))
6961 (progn
6962 (setq cperl-unreadable-ok t)
6963 nil) ; Return empty list
6964 (error "Aborting: unreadable directory %s" file)))))))
6965 (mapc (function
6966 (lambda (file)
6967 (cond
6968 ((string-match cperl-noscan-files-regexp file)
6969 nil)
6970 ((not (file-directory-p file))
6971 (if (string-match cperl-scan-files-regexp file)
6972 (cperl-write-tags file erase recurse nil t noxs topdir)))
6973 ((not recurse) nil)
6974 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
6975 files)))
6977 (setq xs (string-match "\\.xs$" file))
6978 (if (not (and xs noxs))
6979 (progn
6980 (cond ((eq erase 'ignore) (goto-char (point-max)))
6981 (erase (erase-buffer))
6983 (goto-char 1)
6984 (setq rel file)
6985 ;; On case-preserving filesystems case might be encoded in properties
6986 (set-text-properties 0 (length rel) nil rel)
6987 (and (equal topdir (substring rel 0 (length topdir)))
6988 (setq rel (substring file (length topdir))))
6989 (if (search-forward (concat "\f\n" rel ",") nil t)
6990 (progn
6991 (search-backward "\f\n")
6992 (delete-region (point)
6993 (save-excursion
6994 (forward-char 1)
6995 (if (search-forward "\f\n"
6996 nil 'toend)
6997 (- (point) 2)
6998 (point-max)))))
6999 (goto-char (point-max)))))
7000 (insert (cperl-find-tags file xs topdir))))))
7001 (if inbuffer nil ; Delegate to the caller
7002 (save-buffer 0) ; No backup
7003 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7004 (initialize-new-tags-table))))))
7006 (defvar cperl-tags-hier-regexp-list
7007 (concat
7008 "^\\("
7009 "\\(package\\)\\>"
7010 "\\|"
7011 cperl-sub-regexp "\\>[^\n]+::"
7012 "\\|"
7013 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7014 "\\|"
7015 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7016 "\\)"))
7018 (defvar cperl-hierarchy '(() ())
7019 "Global hierarchy of classes.")
7021 ;; Follows call to (autoloaded) visit-tags-table.
7022 (declare-function file-of-tag "etags" (&optional relative))
7023 (declare-function etags-snarf-tag "etags" (&optional use-explicit))
7025 (defun cperl-tags-hier-fill ()
7026 ;; Suppose we are in a tag table cooked by cperl.
7027 (goto-char 1)
7028 (let (pack name line ord cons1 file info fileind)
7029 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7030 (setq pack (match-beginning 2))
7031 (beginning-of-line)
7032 (if (looking-at (concat
7033 "\\([^\n]+\\)"
7034 "\C-?"
7035 "\\([^\n]+\\)"
7036 "\C-a"
7037 "\\([0-9]+\\)"
7039 "\\([0-9]+\\)"))
7040 (progn
7041 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7042 name (buffer-substring (match-beginning 2) (match-end 2))
7043 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7044 line (buffer-substring (match-beginning 3) (match-end 3))
7045 ord (if pack 1 0)
7046 file (file-of-tag)
7047 fileind (format "%s:%s" file line)
7048 ;; Moves to beginning of the next line:
7049 info (cperl-etags-snarf-tag file line))
7050 ;; Move back
7051 (forward-char -1)
7052 ;; Make new member of hierarchy name ==> file ==> pos if needed
7053 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7054 ;; Name known
7055 (setcdr cons1 (cons (cons fileind (vector file info))
7056 (cdr cons1)))
7057 ;; First occurrence of the name, start alist
7058 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7059 (if pack
7060 (setcar (cdr cperl-hierarchy)
7061 (cons cons1 (nth 1 cperl-hierarchy)))
7062 (setcar cperl-hierarchy
7063 (cons cons1 (car cperl-hierarchy)))))))
7064 (end-of-line))))
7066 (declare-function x-popup-menu "menu.c" (position menu))
7067 (declare-function etags-goto-tag-location "etags" (tag-info))
7069 (defun cperl-tags-hier-init (&optional update)
7070 "Show hierarchical menu of classes and methods.
7071 Finds info about classes by a scan of loaded TAGS files.
7072 Supposes that the TAGS files contain fully qualified function names.
7073 One may build such TAGS files from CPerl mode menu."
7074 (interactive)
7075 (require 'etags)
7076 (require 'imenu)
7077 (if (or update (null (nth 2 cperl-hierarchy)))
7078 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7079 (or (nthcdr 2 elt)
7080 ;; Only in one file
7081 (setcdr elt (cdr (nth 1 elt)))))))
7082 to l1 l2 l3)
7083 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7084 (setq cperl-hierarchy (list l1 l2 l3))
7085 (if (featurep 'xemacs) ; Not checked
7086 (progn
7087 (or tags-file-name
7088 ;; Does this work in XEmacs?
7089 (call-interactively 'visit-tags-table))
7090 (message "Updating list of classes...")
7091 (set-buffer (get-file-buffer tags-file-name))
7092 (cperl-tags-hier-fill))
7093 (or tags-table-list
7094 (call-interactively 'visit-tags-table))
7095 (mapc
7096 (function
7097 (lambda (tagsfile)
7098 (message "Updating list of classes... %s" tagsfile)
7099 (set-buffer (get-file-buffer tagsfile))
7100 (cperl-tags-hier-fill)))
7101 tags-table-list)
7102 (message "Updating list of classes... postprocessing..."))
7103 (mapc remover (car cperl-hierarchy))
7104 (mapc remover (nth 1 cperl-hierarchy))
7105 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7106 (cons "Methods: " (car cperl-hierarchy))))
7107 (cperl-tags-treeify to 1)
7108 (setcar (nthcdr 2 cperl-hierarchy)
7109 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7110 (message "Updating list of classes: done, requesting display...")
7111 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7113 (or (nth 2 cperl-hierarchy)
7114 (error "No items found"))
7115 (setq update
7116 ;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7117 (if (if (fboundp 'display-popup-menus-p)
7118 (display-popup-menus-p)
7119 window-system)
7120 (x-popup-menu t (nth 2 cperl-hierarchy))
7121 (require 'tmm)
7122 (tmm-prompt (nth 2 cperl-hierarchy))))
7123 (if (and update (listp update))
7124 (progn (while (cdr update) (setq update (cdr update)))
7125 (setq update (car update)))) ; Get the last from the list
7126 (if (vectorp update)
7127 (progn
7128 (find-file (elt update 0))
7129 (cperl-etags-goto-tag-location (elt update 1))))
7130 (if (eq update -999) (cperl-tags-hier-init t)))
7132 (defun cperl-tags-treeify (to level)
7133 ;; cadr of `to' is read-write. On start it is a cons
7134 (let* ((regexp (concat "^\\(" (mapconcat
7135 #'identity
7136 (make-list level "[_a-zA-Z0-9]+")
7137 "::")
7138 "\\)\\(::\\)?"))
7139 (packages (cdr (nth 1 to)))
7140 (methods (cdr (nth 2 to)))
7141 l1 head cons1 cons2 ord writeto recurse
7142 root-packages root-functions
7143 (move-deeper
7144 (function
7145 (lambda (elt)
7146 (cond ((and (string-match regexp (car elt))
7147 (or (eq ord 1) (match-end 2)))
7148 (setq head (substring (car elt) 0 (match-end 1))
7149 recurse t)
7150 (if (setq cons1 (assoc head writeto)) nil
7151 ;; Need to init new head
7152 (setcdr writeto (cons (list head (list "Packages: ")
7153 (list "Methods: "))
7154 (cdr writeto)))
7155 (setq cons1 (nth 1 writeto)))
7156 (setq cons2 (nth ord cons1)) ; Either packs or meths
7157 (setcdr cons2 (cons elt (cdr cons2))))
7158 ((eq ord 2)
7159 (setq root-functions (cons elt root-functions)))
7161 (setq root-packages (cons elt root-packages))))))))
7162 (setcdr to l1) ; Init to dynamic space
7163 (setq writeto to)
7164 (setq ord 1)
7165 (mapc move-deeper packages)
7166 (setq ord 2)
7167 (mapc move-deeper methods)
7168 (if recurse
7169 (mapc (function (lambda (elt)
7170 (cperl-tags-treeify elt (1+ level))))
7171 (cdr to)))
7172 ;;Now clean up leaders with one child only
7173 (mapc (function (lambda (elt)
7174 (if (not (and (listp (cdr elt))
7175 (eq (length elt) 2)))
7177 (setcar elt (car (nth 1 elt)))
7178 (setcdr elt (cdr (nth 1 elt))))))
7179 (cdr to))
7180 ;; Sort the roots of subtrees
7181 (if (default-value 'imenu-sort-function)
7182 (setcdr to
7183 (sort (cdr to) (default-value 'imenu-sort-function))))
7184 ;; Now add back functions removed from display
7185 (mapc (function (lambda (elt)
7186 (setcdr to (cons elt (cdr to)))))
7187 (if (default-value 'imenu-sort-function)
7188 (nreverse
7189 (sort root-functions (default-value 'imenu-sort-function)))
7190 root-functions))
7191 ;; Now add back packages removed from display
7192 (mapc (function (lambda (elt)
7193 (setcdr to (cons (cons (concat "package " (car elt))
7194 (cdr elt))
7195 (cdr to)))))
7196 (if (default-value 'imenu-sort-function)
7197 (nreverse
7198 (sort root-packages (default-value 'imenu-sort-function)))
7199 root-packages))))
7201 ;;(x-popup-menu t
7202 ;; '(keymap "Name1"
7203 ;; ("Ret1" "aa")
7204 ;; ("Head1" "ab"
7205 ;; keymap "Name2"
7206 ;; ("Tail1" "x") ("Tail2" "y"))))
7208 (defun cperl-list-fold (list name limit)
7209 (let (list1 list2 elt1 (num 0))
7210 (if (<= (length list) limit) list
7211 (setq list1 nil list2 nil)
7212 (while list
7213 (setq num (1+ num)
7214 elt1 (car list)
7215 list (cdr list))
7216 (if (<= num imenu-max-items)
7217 (setq list2 (cons elt1 list2))
7218 (setq list1 (cons (cons name
7219 (nreverse list2))
7220 list1)
7221 list2 (list elt1)
7222 num 1)))
7223 (nreverse (cons (cons name
7224 (nreverse list2))
7225 list1)))))
7227 (defun cperl-menu-to-keymap (menu)
7228 (let (list)
7229 (cons 'keymap
7230 (mapcar
7231 (function
7232 (lambda (elt)
7233 (cond ((listp (cdr elt))
7234 (setq list (cperl-list-fold
7235 (cdr elt) (car elt) imenu-max-items))
7236 (cons nil
7237 (cons (car elt)
7238 (cperl-menu-to-keymap list))))
7240 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7241 (cperl-list-fold menu "Root" imenu-max-items)))))
7244 (defvar cperl-bad-style-regexp
7245 (mapconcat #'identity
7246 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7247 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7248 "\\|")
7249 "Finds places such that insertion of a whitespace may help a lot.")
7251 (defvar cperl-not-bad-style-regexp
7252 (mapconcat
7253 #'identity
7254 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7255 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7256 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7257 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7258 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7259 "-[0-9]" ; -5
7260 "\\+\\+" ; ++var
7261 "--" ; --var
7262 ".->" ; a->b
7263 "->" ; a SPACE ->b
7264 "\\[-" ; a[-1]
7265 "\\\\[&$@*\\\\]" ; \&func
7266 "^=" ; =head
7267 "\\$." ; $|
7268 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7269 "||"
7270 "//"
7271 "&&"
7272 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7273 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7274 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7275 ;;"[*/+-|&<.]+="
7277 "\\|")
7278 "If matches at the start of match found by `my-bad-c-style-regexp',
7279 insertion of a whitespace will not help.")
7281 (defvar found-bad)
7283 (defun cperl-find-bad-style ()
7284 "Find places in the buffer where insertion of a whitespace may help.
7285 Prompts user for insertion of spaces.
7286 Currently it is tuned to C and Perl syntax."
7287 (interactive)
7288 (let (found-bad (p (point)))
7289 (setq last-nonmenu-event 13) ; To disable popup
7290 (goto-char (point-min))
7291 (map-y-or-n-p "Insert space here? "
7292 (lambda (_) (insert " "))
7293 'cperl-next-bad-style
7294 '("location" "locations" "insert a space into")
7295 `((?\C-r ,(lambda (_)
7296 (let ((buffer-quit-function
7297 #'exit-recursive-edit))
7298 (message "Exit with Esc Esc")
7299 (recursive-edit)
7300 t)) ; Consider acted upon
7301 "edit, exit with Esc Esc")
7302 (?e ,(lambda (_)
7303 (let ((buffer-quit-function
7304 #'exit-recursive-edit))
7305 (message "Exit with Esc Esc")
7306 (recursive-edit)
7307 t)) ; Consider acted upon
7308 "edit, exit with Esc Esc"))
7310 (if found-bad (goto-char found-bad)
7311 (goto-char p)
7312 (message "No appropriate place found"))))
7314 (defun cperl-next-bad-style ()
7315 (let (p (not-found t) found)
7316 (while (and not-found
7317 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7318 (setq p (point))
7319 (goto-char (match-beginning 0))
7320 (if (or
7321 (looking-at cperl-not-bad-style-regexp)
7322 ;; Check for a < -b and friends
7323 (and (eq (following-char) ?\-)
7324 (save-excursion
7325 (skip-chars-backward " \t\n")
7326 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7327 ;; Now check for syntax type
7328 (save-match-data
7329 (setq found (point))
7330 (beginning-of-defun)
7331 (let ((pps (parse-partial-sexp (point) found)))
7332 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7333 (goto-char (match-end 0))
7334 (goto-char (1- p))
7335 (setq not-found nil
7336 found-bad found)))
7337 (not not-found)))
7340 ;;; Getting help
7341 (defvar cperl-have-help-regexp
7342 ;;(concat "\\("
7343 (mapconcat
7344 #'identity
7345 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7346 "[$@]\\^[a-zA-Z]" ; Special variable
7347 "[$@][^ \n\t]" ; Special variable
7348 "-[a-zA-Z]" ; File test
7349 "\\\\[a-zA-Z0]" ; Special chars
7350 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7351 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7352 "[a-zA-Z_0-9:]+" ; symbol or number
7353 "x="
7354 "#!")
7355 ;;"\\)\\|\\("
7356 "\\|")
7357 ;;"\\)"
7359 "Matches places in the buffer we can find help for.")
7361 (defvar cperl-message-on-help-error t)
7362 (defvar cperl-help-from-timer nil)
7364 (defun cperl-word-at-point-hard ()
7365 ;; Does not save-excursion
7366 ;; Get to the something meaningful
7367 (or (eobp) (eolp) (forward-char 1))
7368 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7369 (point-at-bol)
7370 'to-beg)
7371 ;; (cond
7372 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7373 ;; (skip-chars-backward " \n\t\r({[]});,")
7374 ;; (or (bobp) (backward-char 1))))
7375 ;; Try to backtrace
7376 (cond
7377 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7378 (skip-chars-backward "a-zA-Z0-9_:")
7379 (cond
7380 ((and (eq (preceding-char) ?^) ; $^I
7381 (eq (char-after (- (point) 2)) ?\$))
7382 (forward-char -2))
7383 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7384 (forward-char -1))
7385 ((and (eq (preceding-char) ?\=)
7386 (eq (current-column) 1))
7387 (forward-char -1))) ; =head1
7388 (if (and (eq (preceding-char) ?\<)
7389 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7390 (forward-char -1)))
7391 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7392 (forward-char -1))
7393 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7394 (forward-char -1))
7395 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7396 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7397 (cond
7398 ((and (eq (preceding-char) ?\$)
7399 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7400 (forward-char -1))
7401 ((and (eq (following-char) ?\>)
7402 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7403 (save-excursion
7404 (forward-sexp -1)
7405 (and (eq (preceding-char) ?\<)
7406 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7407 (search-backward "<"))))
7408 ((and (eq (following-char) ?\$)
7409 (eq (preceding-char) ?\<)
7410 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7411 (forward-char -1)))
7412 (if (looking-at cperl-have-help-regexp)
7413 (buffer-substring (match-beginning 0) (match-end 0))))
7415 (defun cperl-get-help ()
7416 "Get one-line docs on the symbol at the point.
7417 The data for these docs is a little bit obsolete and may be in fact longer
7418 than a line. Your contribution to update/shorten it is appreciated."
7419 (interactive)
7420 (save-match-data ; May be called "inside" query-replace
7421 (save-excursion
7422 (let ((word (cperl-word-at-point-hard)))
7423 (if word
7424 (if (and cperl-help-from-timer ; Bail out if not in mainland
7425 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7426 (or (memq (get-text-property (point) 'face)
7427 '(font-lock-comment-face font-lock-string-face))
7428 (memq (get-text-property (point) 'syntax-type)
7429 '(pod here-doc format))))
7431 (cperl-describe-perl-symbol word))
7432 (if cperl-message-on-help-error
7433 (message "Nothing found for %s..."
7434 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7436 ;;; Stolen from perl-descr.el by Johan Vromans:
7438 (defvar cperl-doc-buffer " *perl-doc*"
7439 "Where the documentation can be found.")
7441 (defun cperl-describe-perl-symbol (val)
7442 "Display the documentation of symbol at point, a Perl operator."
7443 (let ((enable-recursive-minibuffers t)
7444 regexp)
7445 (cond
7446 ((string-match "^[&*][a-zA-Z_]" val)
7447 (setq val (concat (substring val 0 1) "NAME")))
7448 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7449 (setq val (concat "@" (substring val 1 (match-end 1)))))
7450 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7451 (setq val (concat "%" (substring val 1 (match-end 1)))))
7452 ((and (string= val "x") (string-match "^x=" val))
7453 (setq val "x="))
7454 ((string-match "^\\$[\C-a-\C-z]" val)
7455 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7456 ((string-match "^CORE::" val)
7457 (setq val "CORE::"))
7458 ((string-match "^SUPER::" val)
7459 (setq val "SUPER::"))
7460 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7461 (setq val "<NAME>")))
7462 (setq regexp (concat "^"
7463 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7464 (regexp-quote val)
7465 "\\([ \t([/]\\|$\\)"))
7467 ;; get the buffer with the documentation text
7468 (cperl-switch-to-doc-buffer)
7470 ;; lookup in the doc
7471 (goto-char (point-min))
7472 (let ((case-fold-search nil))
7473 (list
7474 (if (re-search-forward regexp (point-max) t)
7475 (save-excursion
7476 (beginning-of-line 1)
7477 (let ((lnstart (point)))
7478 (end-of-line)
7479 (message "%s" (buffer-substring lnstart (point)))))
7480 (if cperl-message-on-help-error
7481 (message "No definition for %s" val)))))))
7483 (defvar cperl-short-docs 'please-ignore-this-line
7484 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7485 "# based on \\='@(#)@ perl-descr.el 1.9 - describe-perl-symbol\\=' [Perl 5]
7486 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7487 ! ... Logical negation.
7488 ... != ... Numeric inequality.
7489 ... !~ ... Search pattern, substitution, or translation (negated).
7490 $! In numeric context: errno. In a string context: error string.
7491 $\" The separator which joins elements of arrays interpolated in strings.
7492 $# The output format for printed numbers. Default is %.15g or close.
7493 $$ Process number of this script. Changes in the fork()ed child process.
7494 $% The current page number of the currently selected output channel.
7496 The following variables are always local to the current block:
7498 $1 Match of the 1st set of parentheses in the last match (auto-local).
7499 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7500 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7501 $4 Match of the 4th set of parentheses in the last match (auto-local).
7502 $5 Match of the 5th set of parentheses in the last match (auto-local).
7503 $6 Match of the 6th set of parentheses in the last match (auto-local).
7504 $7 Match of the 7th set of parentheses in the last match (auto-local).
7505 $8 Match of the 8th set of parentheses in the last match (auto-local).
7506 $9 Match of the 9th set of parentheses in the last match (auto-local).
7507 $& The string matched by the last pattern match (auto-local).
7508 $\\=' The string after what was matched by the last match (auto-local).
7509 $\\=` The string before what was matched by the last match (auto-local).
7511 $( The real gid of this process.
7512 $) The effective gid of this process.
7513 $* Deprecated: Set to 1 to do multiline matching within a string.
7514 $+ The last bracket matched by the last search pattern.
7515 $, The output field separator for the print operator.
7516 $- The number of lines left on the page.
7517 $. The current input line number of the last filehandle that was read.
7518 $/ The input record separator, newline by default.
7519 $0 Name of the file containing the current perl script (read/write).
7520 $: String may be broken after these characters to fill ^-lines in a format.
7521 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7522 $< The real uid of this process.
7523 $= The page length of the current output channel. Default is 60 lines.
7524 $> The effective uid of this process.
7525 $? The status returned by the last \\=`\\=`, pipe close or `system'.
7526 $@ The perl error message from the last eval or do @var{EXPR} command.
7527 $ARGV The name of the current file used with <> .
7528 $[ Deprecated: The index of the first element/char in an array/string.
7529 $\\ The output record separator for the print operator.
7530 $] The perl version string as displayed with perl -v.
7531 $^ The name of the current top-of-page format.
7532 $^A The current value of the write() accumulator for format() lines.
7533 $^D The value of the perl debug (-D) flags.
7534 $^E Information about the last system error other than that provided by $!.
7535 $^F The highest system file descriptor, ordinarily 2.
7536 $^H The current set of syntax checks enabled by `use strict'.
7537 $^I The value of the in-place edit extension (perl -i option).
7538 $^L What formats output to perform a formfeed. Default is \\f.
7539 $^M A buffer for emergency memory allocation when running out of memory.
7540 $^O The operating system name under which this copy of Perl was built.
7541 $^P Internal debugging flag.
7542 $^T The time the script was started. Used by -A/-M/-C file tests.
7543 $^W True if warnings are requested (perl -w flag).
7544 $^X The name under which perl was invoked (argv[0] in C-speech).
7545 $_ The default input and pattern-searching space.
7546 $| Auto-flush after write/print on current output channel? Default 0.
7547 $~ The name of the current report format.
7548 ... % ... Modulo division.
7549 ... %= ... Modulo division assignment.
7550 %ENV Contains the current environment.
7551 %INC List of files that have been require-d or do-ne.
7552 %SIG Used to set signal handlers for various signals.
7553 ... & ... Bitwise and.
7554 ... && ... Logical and.
7555 ... &&= ... Logical and assignment.
7556 ... &= ... Bitwise and assignment.
7557 ... * ... Multiplication.
7558 ... ** ... Exponentiation.
7559 *NAME Glob: all objects referred by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7560 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7561 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7562 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7563 ... += ... Addition assignment.
7564 , Comma operator.
7565 ... - ... Subtraction.
7566 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7567 ... -= ... Subtraction assignment.
7568 -A Access time in days since script started.
7569 -B File is a non-text (binary) file.
7570 -C Inode change time in days since script started.
7571 -M Age in days since script started.
7572 -O File is owned by real uid.
7573 -R File is readable by real uid.
7574 -S File is a socket .
7575 -T File is a text file.
7576 -W File is writable by real uid.
7577 -X File is executable by real uid.
7578 -b File is a block special file.
7579 -c File is a character special file.
7580 -d File is a directory.
7581 -e File exists .
7582 -f File is a plain file.
7583 -g File has setgid bit set.
7584 -k File has sticky bit set.
7585 -l File is a symbolic link.
7586 -o File is owned by effective uid.
7587 -p File is a named pipe (FIFO).
7588 -r File is readable by effective uid.
7589 -s File has non-zero size.
7590 -t Tests if filehandle (STDIN by default) is opened to a tty.
7591 -u File has setuid bit set.
7592 -w File is writable by effective uid.
7593 -x File is executable by effective uid.
7594 -z File has zero size.
7595 . Concatenate strings.
7596 .. Range (list context); flip/flop (scalar context) operator.
7597 .= Concatenate assignment strings
7598 ... / ... Division. /PATTERN/ioxsmg Pattern match
7599 ... /= ... Division assignment.
7600 /PATTERN/ioxsmg Pattern match.
7601 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7602 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7603 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7604 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7605 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7606 ... <= ... Numeric less than or equal to.
7607 ... <=> ... Numeric compare.
7608 ... = ... Assignment.
7609 ... == ... Numeric equality.
7610 ... =~ ... Search pattern, substitution, or translation
7611 ... ~~ .. Smart match
7612 ... > ... Numeric greater than.
7613 ... >= ... Numeric greater than or equal to.
7614 ... >> ... Bitwise shift right.
7615 ... >>= ... Bitwise shift right assignment.
7616 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7617 ?PATTERN? One-time pattern match.
7618 @ARGV Command line arguments (not including the command name - see $0).
7619 @INC List of places to look for perl scripts during do/include/use.
7620 @_ Parameter array for subroutines; result of split() unless in list context.
7621 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7622 \\0 Octal char, e.g. \\033.
7623 \\E Case modification terminator. See \\Q, \\L, and \\U.
7624 \\L Lowercase until \\E . See also \\l, lc.
7625 \\U Upcase until \\E . See also \\u, uc.
7626 \\Q Quote metacharacters until \\E . See also quotemeta.
7627 \\a Alarm character (octal 007).
7628 \\b Backspace character (octal 010).
7629 \\c Control character, e.g. \\c[ .
7630 \\e Escape character (octal 033).
7631 \\f Formfeed character (octal 014).
7632 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7633 \\n Newline character (octal 012 on most systems).
7634 \\r Return character (octal 015 on most systems).
7635 \\t Tab character (octal 011).
7636 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7637 \\x Hex character, e.g. \\x1b.
7638 ... ^ ... Bitwise exclusive or.
7639 __END__ Ends program source.
7640 __DATA__ Ends program source.
7641 __FILE__ Current (source) filename.
7642 __LINE__ Current line in current source.
7643 __PACKAGE__ Current package.
7644 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7645 ARGVOUT Output filehandle with -i flag.
7646 BEGIN { ... } Immediately executed (during compilation) piece of code.
7647 END { ... } Pseudo-subroutine executed after the script finishes.
7648 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7649 UNITCHECK { ... }
7650 INIT { ... } Pseudo-subroutine executed before the script starts running.
7651 DATA Input filehandle for what follows after __END__ or __DATA__.
7652 accept(NEWSOCKET,GENERICSOCKET)
7653 alarm(SECONDS)
7654 atan2(X,Y)
7655 bind(SOCKET,NAME)
7656 binmode(FILEHANDLE)
7657 break Break out of a given/when statement
7658 caller[(LEVEL)]
7659 chdir(EXPR)
7660 chmod(LIST)
7661 chop[(LIST|VAR)]
7662 chown(LIST)
7663 chroot(FILENAME)
7664 close(FILEHANDLE)
7665 closedir(DIRHANDLE)
7666 ... cmp ... String compare.
7667 connect(SOCKET,NAME)
7668 continue of { block } continue { block }. Is executed after `next' or at end.
7669 cos(EXPR)
7670 crypt(PLAINTEXT,SALT)
7671 dbmclose(%HASH)
7672 dbmopen(%HASH,DBNAME,MODE)
7673 default { ... } default case for given/when block
7674 defined(EXPR)
7675 delete($HASH{KEY})
7676 die(LIST)
7677 do { ... }|SUBR while|until EXPR executes at least once
7678 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7679 dump LABEL
7680 each(%HASH)
7681 endgrent
7682 endhostent
7683 endnetent
7684 endprotoent
7685 endpwent
7686 endservent
7687 eof[([FILEHANDLE])]
7688 ... eq ... String equality.
7689 eval(EXPR) or eval { BLOCK }
7690 evalbytes See eval.
7691 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7692 exit(EXPR)
7693 exp(EXPR)
7694 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7695 fileno(FILEHANDLE)
7696 flock(FILEHANDLE,OPERATION)
7697 for (EXPR;EXPR;EXPR) { ... }
7698 foreach [VAR] (@ARRAY) { ... }
7699 fork
7700 ... ge ... String greater than or equal.
7701 getc[(FILEHANDLE)]
7702 getgrent
7703 getgrgid(GID)
7704 getgrnam(NAME)
7705 gethostbyaddr(ADDR,ADDRTYPE)
7706 gethostbyname(NAME)
7707 gethostent
7708 getlogin
7709 getnetbyaddr(ADDR,ADDRTYPE)
7710 getnetbyname(NAME)
7711 getnetent
7712 getpeername(SOCKET)
7713 getpgrp(PID)
7714 getppid
7715 getpriority(WHICH,WHO)
7716 getprotobyname(NAME)
7717 getprotobynumber(NUMBER)
7718 getprotoent
7719 getpwent
7720 getpwnam(NAME)
7721 getpwuid(UID)
7722 getservbyname(NAME,PROTO)
7723 getservbyport(PORT,PROTO)
7724 getservent
7725 getsockname(SOCKET)
7726 getsockopt(SOCKET,LEVEL,OPTNAME)
7727 given (EXPR) { [ when (EXPR) { ... } ]+ [ default { ... } ]? }
7728 gmtime(EXPR)
7729 goto LABEL
7730 ... gt ... String greater than.
7731 hex(EXPR)
7732 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7733 index(STR,SUBSTR[,OFFSET])
7734 int(EXPR)
7735 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7736 join(EXPR,LIST)
7737 keys(%HASH)
7738 kill(LIST)
7739 last [LABEL]
7740 ... le ... String less than or equal.
7741 length(EXPR)
7742 link(OLDFILE,NEWFILE)
7743 listen(SOCKET,QUEUESIZE)
7744 local(LIST)
7745 localtime(EXPR)
7746 log(EXPR)
7747 lstat(EXPR|FILEHANDLE|VAR)
7748 ... lt ... String less than.
7749 m/PATTERN/iogsmx
7750 mkdir(FILENAME,MODE)
7751 msgctl(ID,CMD,ARG)
7752 msgget(KEY,FLAGS)
7753 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7754 msgsnd(ID,MSG,FLAGS)
7755 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7756 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7757 ... ne ... String inequality.
7758 next [LABEL]
7759 oct(EXPR)
7760 open(FILEHANDLE[,EXPR])
7761 opendir(DIRHANDLE,EXPR)
7762 ord(EXPR) ASCII value of the first char of the string.
7763 pack(TEMPLATE,LIST)
7764 package NAME Introduces package context.
7765 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7766 pop(ARRAY)
7767 print [FILEHANDLE] [(LIST)]
7768 printf [FILEHANDLE] (FORMAT,LIST)
7769 push(ARRAY,LIST)
7770 q/STRING/ Synonym for \\='STRING\\='
7771 qq/STRING/ Synonym for \"STRING\"
7772 qx/STRING/ Synonym for \\=`STRING\\=`
7773 rand[(EXPR)]
7774 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7775 readdir(DIRHANDLE)
7776 readlink(EXPR)
7777 recv(SOCKET,SCALAR,LEN,FLAGS)
7778 redo [LABEL]
7779 rename(OLDNAME,NEWNAME)
7780 require [FILENAME | PERL_VERSION]
7781 reset[(EXPR)]
7782 return(LIST)
7783 reverse(LIST)
7784 rewinddir(DIRHANDLE)
7785 rindex(STR,SUBSTR[,OFFSET])
7786 rmdir(FILENAME)
7787 s/PATTERN/REPLACEMENT/gieoxsm
7788 say [FILEHANDLE] [(LIST)]
7789 scalar(EXPR)
7790 seek(FILEHANDLE,POSITION,WHENCE)
7791 seekdir(DIRHANDLE,POS)
7792 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7793 semctl(ID,SEMNUM,CMD,ARG)
7794 semget(KEY,NSEMS,SIZE,FLAGS)
7795 semop(KEY,...)
7796 send(SOCKET,MSG,FLAGS[,TO])
7797 setgrent
7798 sethostent(STAYOPEN)
7799 setnetent(STAYOPEN)
7800 setpgrp(PID,PGRP)
7801 setpriority(WHICH,WHO,PRIORITY)
7802 setprotoent(STAYOPEN)
7803 setpwent
7804 setservent(STAYOPEN)
7805 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7806 shift[(ARRAY)]
7807 shmctl(ID,CMD,ARG)
7808 shmget(KEY,SIZE,FLAGS)
7809 shmread(ID,VAR,POS,SIZE)
7810 shmwrite(ID,STRING,POS,SIZE)
7811 shutdown(SOCKET,HOW)
7812 sin(EXPR)
7813 sleep[(EXPR)]
7814 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7815 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7816 sort [SUBROUTINE] (LIST)
7817 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7818 split[(/PATTERN/[,EXPR[,LIMIT]])]
7819 sprintf(FORMAT,LIST)
7820 sqrt(EXPR)
7821 srand(EXPR)
7822 stat(EXPR|FILEHANDLE|VAR)
7823 state VAR or state (VAR1,...) Introduces a static lexical variable
7824 study[(SCALAR)]
7825 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7826 substr(EXPR,OFFSET[,LEN])
7827 symlink(OLDFILE,NEWFILE)
7828 syscall(LIST)
7829 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7830 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7831 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7832 tell[(FILEHANDLE)]
7833 telldir(DIRHANDLE)
7834 time
7835 times
7836 tr/SEARCHLIST/REPLACEMENTLIST/cds
7837 truncate(FILE|EXPR,LENGTH)
7838 umask[(EXPR)]
7839 undef[(EXPR)]
7840 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7841 unlink(LIST)
7842 unpack(TEMPLATE,EXPR)
7843 unshift(ARRAY,LIST)
7844 until (EXPR) { ... } EXPR until EXPR
7845 utime(LIST)
7846 values(%HASH)
7847 vec(EXPR,OFFSET,BITS)
7848 wait
7849 waitpid(PID,FLAGS)
7850 wantarray Returns true if the sub/eval is called in list context.
7851 warn(LIST)
7852 while (EXPR) { ... } EXPR while EXPR
7853 write[(EXPR|FILEHANDLE)]
7854 ... x ... Repeat string or array.
7855 x= ... Repetition assignment.
7856 y/SEARCHLIST/REPLACEMENTLIST/
7857 ... | ... Bitwise or.
7858 ... || ... Logical or.
7859 ... // ... Defined-or.
7860 ~ ... Unary bitwise complement.
7861 #! OS interpreter indicator. If contains `perl', used for options, and -x.
7862 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7863 CORE:: Prefix to access builtin function if imported sub obscures it.
7864 SUPER:: Prefix to lookup for a method in @ISA classes.
7865 DESTROY Shorthand for `sub DESTROY {...}'.
7866 ... EQ ... Obsolete synonym of `eq'.
7867 ... GE ... Obsolete synonym of `ge'.
7868 ... GT ... Obsolete synonym of `gt'.
7869 ... LE ... Obsolete synonym of `le'.
7870 ... LT ... Obsolete synonym of `lt'.
7871 ... NE ... Obsolete synonym of `ne'.
7872 abs [ EXPR ] absolute value
7873 ... and ... Low-precedence synonym for &&.
7874 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7875 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq \\='\\='!
7876 chr Converts a number to char with the same ordinal.
7877 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7878 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7879 exists $HASH{KEY} True if the key exists.
7880 fc EXPR Returns the casefolded version of EXPR.
7881 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7882 formline PICTURE, LIST Backdoor into \"format\" processing.
7883 glob EXPR Synonym of <EXPR>.
7884 lc [ EXPR ] Returns lowercased EXPR.
7885 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
7886 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
7887 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
7888 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
7889 not ... Low-precedence synonym for ! - negation.
7890 ... or ... Low-precedence synonym for ||.
7891 pos STRING Set/Get end-position of the last match over this string, see \\G.
7892 prototype FUNC Returns the prototype of a function as a string, or undef.
7893 quotemeta [ EXPR ] Quote regexp metacharacters.
7894 qw/WORD1 .../ Synonym of split(\\='\\=', \\='WORD1 ...\\=')
7895 readline FH Synonym of <FH>.
7896 readpipe CMD Synonym of \\=`CMD\\=`.
7897 ref [ EXPR ] Type of EXPR when dereferenced.
7898 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
7899 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
7900 tied Returns internal object for a tied data.
7901 uc [ EXPR ] Returns upcased EXPR.
7902 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
7903 untie VAR Unlink an object from a simple Perl variable.
7904 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
7905 ... xor ... Low-precedence synonym for exclusive or.
7906 prototype \\&SUB Returns prototype of the function given a reference.
7907 =head1 Top-level heading.
7908 =head2 Second-level heading.
7909 =head3 Third-level heading (is there such?).
7910 =over [ NUMBER ] Start list.
7911 =item [ TITLE ] Start new item in the list.
7912 =back End list.
7913 =cut Switch from POD to Perl.
7914 =pod Switch from Perl to POD.
7915 =begin Switch from Perl6 to POD.
7916 =end Switch from POD to Perl6.
7919 (defun cperl-switch-to-doc-buffer (&optional interactive)
7920 "Go to the perl documentation buffer and insert the documentation."
7921 (interactive "p")
7922 (let ((buf (get-buffer-create cperl-doc-buffer)))
7923 (if interactive
7924 (switch-to-buffer-other-window buf)
7925 (set-buffer buf))
7926 (if (= (buffer-size) 0)
7927 (progn
7928 (insert (documentation-property 'cperl-short-docs
7929 'variable-documentation))
7930 (setq buffer-read-only t)))))
7932 (defun cperl-beautify-regexp-piece (b e embed level)
7933 ;; b is before the starting delimiter, e before the ending
7934 ;; e should be a marker, may be changed, but remains "correct".
7935 ;; EMBED is nil if we process the whole REx.
7936 ;; The REx is guaranteed to have //x
7937 ;; LEVEL shows how many levels deep to go
7938 ;; position at enter and at leave is not defined
7939 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline pos)
7940 (if embed
7941 (progn
7942 (goto-char b)
7943 (setq c (if (eq embed t) (current-indentation) (current-column)))
7944 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
7945 (forward-char 2)
7946 (delete-char 1)
7947 (forward-char 1))
7948 ((looking-at "(\\?[^a-zA-Z]")
7949 (forward-char 3))
7950 ((looking-at "(\\?") ; (?i)
7951 (forward-char 2))
7953 (forward-char 1))))
7954 (goto-char (1+ b))
7955 (setq c (1- (current-column))))
7956 (setq c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
7957 (or (looking-at "[ \t]*[\n#]")
7958 (progn
7959 (insert "\n")))
7960 (goto-char e)
7961 (beginning-of-line)
7962 (if (re-search-forward "[^ \t]" e t)
7963 (progn ; Something before the ending delimiter
7964 (goto-char e)
7965 (delete-horizontal-space)
7966 (insert "\n")
7967 (cperl-make-indent c)
7968 (set-marker e (point))))
7969 (goto-char b)
7970 (end-of-line 2)
7971 (while (< (point) (marker-position e))
7972 (beginning-of-line)
7973 (setq s (point)
7974 inline t)
7975 (skip-chars-forward " \t")
7976 (delete-region s (point))
7977 (cperl-make-indent c1)
7978 (while (and
7979 inline
7980 (looking-at
7981 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
7982 "\\|" ; Embedded variable
7983 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
7984 "\\|" ; $ ^
7985 "[$^]"
7986 "\\|" ; simple-code simple-code*?
7987 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
7988 "\\|" ; Class
7989 "\\(\\[\\)" ; 6
7990 "\\|" ; Grouping
7991 "\\((\\(\\?\\)?\\)" ; 7 8
7992 "\\|" ; |
7993 "\\(|\\)"))) ; 9
7994 (goto-char (match-end 0))
7995 (setq spaces t)
7996 (cond ((match-beginning 1) ; Alphanum word + junk
7997 (forward-char -1))
7998 ((or (match-beginning 3) ; $ab[12]
7999 (and (match-beginning 5) ; X* X+ X{2,3}
8000 (eq (preceding-char) ?\{)))
8001 (forward-char -1)
8002 (forward-sexp 1))
8003 ((and ; [], already syntaxified
8004 (match-beginning 6)
8005 cperl-regexp-scan
8006 cperl-use-syntax-table-text-property)
8007 (forward-char -1)
8008 (forward-sexp 1)
8009 (or (eq (preceding-char) ?\])
8010 (error "[]-group not terminated"))
8011 (re-search-forward
8012 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8013 ((match-beginning 6) ; []
8014 (setq tmp (point))
8015 (if (looking-at "\\^?\\]")
8016 (goto-char (match-end 0)))
8017 ;; XXXX POSIX classes?!
8018 (while (and (not pos)
8019 (re-search-forward "\\[:\\|\\]" e t))
8020 (if (eq (preceding-char) ?:)
8021 (or (re-search-forward ":\\]" e t)
8022 (error "[:POSIX:]-group in []-group not terminated"))
8023 (setq pos t)))
8024 (or (eq (preceding-char) ?\])
8025 (error "[]-group not terminated"))
8026 (re-search-forward
8027 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8028 ((match-beginning 7) ; ()
8029 (goto-char (match-beginning 0))
8030 (setq pos (current-column))
8031 (or (eq pos c1)
8032 (progn
8033 (delete-horizontal-space)
8034 (insert "\n")
8035 (cperl-make-indent c1)))
8036 (setq tmp (point))
8037 (forward-sexp 1)
8038 ;; (or (forward-sexp 1)
8039 ;; (progn
8040 ;; (goto-char tmp)
8041 ;; (error "()-group not terminated")))
8042 (set-marker m (1- (point)))
8043 (set-marker m1 (point))
8044 (if (= level 1)
8045 (if (progn ; indent rigidly if multiline
8046 ;; In fact does not make a lot of sense, since
8047 ;; the starting position can be already lost due
8048 ;; to insertion of "\n" and " "
8049 (goto-char tmp)
8050 (search-forward "\n" m1 t))
8051 (indent-rigidly (point) m1 (- c1 pos)))
8052 (setq level (1- level))
8053 (cond
8054 ((not (match-beginning 8))
8055 (cperl-beautify-regexp-piece tmp m t level))
8056 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8058 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8059 (goto-char (+ 2 tmp))
8060 (forward-sexp 1)
8061 (cperl-beautify-regexp-piece (point) m t level))
8062 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8063 (goto-char (+ 3 tmp))
8064 (cperl-beautify-regexp-piece (point) m t level))
8066 (cperl-beautify-regexp-piece tmp m t level))))
8067 (goto-char m1)
8068 (cond ((looking-at "[*+?]\\??")
8069 (goto-char (match-end 0)))
8070 ((eq (following-char) ?\{)
8071 (forward-sexp 1)
8072 (if (eq (following-char) ?\?)
8073 (forward-char))))
8074 (skip-chars-forward " \t")
8075 (setq spaces nil)
8076 (if (looking-at "[#\n]")
8077 (progn
8078 (or (eolp) (indent-for-comment))
8079 (beginning-of-line 2))
8080 (delete-horizontal-space)
8081 (insert "\n"))
8082 (end-of-line)
8083 (setq inline nil))
8084 ((match-beginning 9) ; |
8085 (forward-char -1)
8086 (setq tmp (point))
8087 (beginning-of-line)
8088 (if (re-search-forward "[^ \t]" tmp t)
8089 (progn
8090 (goto-char tmp)
8091 (delete-horizontal-space)
8092 (insert "\n"))
8093 ;; first at line
8094 (delete-region (point) tmp))
8095 (cperl-make-indent c)
8096 (forward-char 1)
8097 (skip-chars-forward " \t")
8098 (setq spaces nil)
8099 (if (looking-at "[#\n]")
8100 (beginning-of-line 2)
8101 (delete-horizontal-space)
8102 (insert "\n"))
8103 (end-of-line)
8104 (setq inline nil)))
8105 (or (looking-at "[ \t\n]")
8106 (not spaces)
8107 (insert " "))
8108 (skip-chars-forward " \t"))
8109 (or (looking-at "[#\n]")
8110 (error "Unknown code `%s' in a regexp"
8111 (buffer-substring (point) (1+ (point)))))
8112 (and inline (end-of-line 2)))
8113 ;; Special-case the last line of group
8114 (if (and (>= (point) (marker-position e))
8115 (/= (current-indentation) c))
8116 (progn
8117 (beginning-of-line)
8118 (cperl-make-indent c)))))
8120 (defun cperl-make-regexp-x ()
8121 ;; Returns position of the start
8122 ;; XXX this is called too often! Need to cache the result!
8123 (save-excursion
8124 (or cperl-use-syntax-table-text-property
8125 (error "I need to have a regexp marked!"))
8126 ;; Find the start
8127 (if (looking-at "\\s|")
8128 nil ; good already
8129 (if (or (looking-at "\\([smy]\\|qr\\)\\s|")
8130 (and (eq (preceding-char) ?q)
8131 (looking-at "\\(r\\)\\s|")))
8132 (goto-char (match-end 1))
8133 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8134 ;;(forward-char 1)
8135 (let ((b (point)) (e (make-marker)) have-x delim
8136 (sub-p (eq (preceding-char) ?s)))
8137 (forward-sexp 1)
8138 (set-marker e (1- (point)))
8139 (setq delim (preceding-char))
8140 (if (and sub-p (eq delim (char-after (- (point) 2))))
8141 (error "Possible s/blah// - do not know how to deal with"))
8142 (if sub-p (forward-sexp 1))
8143 (if (looking-at "\\sw*x")
8144 (setq have-x t)
8145 (insert "x"))
8146 ;; Protect fragile " ", "#"
8147 (if have-x nil
8148 (goto-char (1+ b))
8149 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8150 (forward-char -1)
8151 (insert "\\")
8152 (forward-char 1)))
8153 b)))
8155 (defun cperl-beautify-regexp (&optional deep)
8156 "Do it. (Experimental, may change semantics, recheck the result.)
8157 We suppose that the regexp is scanned already."
8158 (interactive "P")
8159 (setq deep (if deep (prefix-numeric-value deep) -1))
8160 (save-excursion
8161 (goto-char (cperl-make-regexp-x))
8162 (let ((b (point)) (e (make-marker)))
8163 (forward-sexp 1)
8164 (set-marker e (1- (point)))
8165 (cperl-beautify-regexp-piece b e nil deep))))
8167 (defun cperl-regext-to-level-start ()
8168 "Goto start of an enclosing group in regexp.
8169 We suppose that the regexp is scanned already."
8170 (interactive)
8171 (let ((limit (cperl-make-regexp-x)) done)
8172 (while (not done)
8173 (or (eq (following-char) ?\()
8174 (search-backward "(" (1+ limit) t)
8175 (error "Cannot find `(' which starts a group"))
8176 (setq done
8177 (save-excursion
8178 (skip-chars-backward "\\")
8179 (looking-at "\\(\\\\\\\\\\)*(")))
8180 (or done (forward-char -1)))))
8182 (defun cperl-contract-level ()
8183 "Find an enclosing group in regexp and contract it.
8184 \(Experimental, may change semantics, recheck the result.)
8185 We suppose that the regexp is scanned already."
8186 (interactive)
8187 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8188 (cperl-regext-to-level-start)
8189 (let ((b (point)) (e (make-marker)) c)
8190 (forward-sexp 1)
8191 (set-marker e (1- (point)))
8192 (goto-char b)
8193 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8194 (cond
8195 ((match-beginning 1) ; #-comment
8196 (or c (setq c (current-indentation)))
8197 (beginning-of-line 2) ; Skip
8198 (cperl-make-indent c))
8200 (delete-char -1)
8201 (just-one-space))))))
8203 (defun cperl-contract-levels ()
8204 "Find an enclosing group in regexp and contract all the kids.
8205 \(Experimental, may change semantics, recheck the result.)
8206 We suppose that the regexp is scanned already."
8207 (interactive)
8208 (save-excursion
8209 (condition-case nil
8210 (cperl-regext-to-level-start)
8211 (error ; We are outside outermost group
8212 (goto-char (cperl-make-regexp-x))))
8213 (let ((b (point)) (e (make-marker)))
8214 (forward-sexp 1)
8215 (set-marker e (1- (point)))
8216 (goto-char (1+ b))
8217 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8218 (cond
8219 ((match-beginning 1) ; Skip
8220 nil)
8221 (t ; Group
8222 (cperl-contract-level)))))))
8224 (defun cperl-beautify-level (&optional deep)
8225 "Find an enclosing group in regexp and beautify it.
8226 \(Experimental, may change semantics, recheck the result.)
8227 We suppose that the regexp is scanned already."
8228 (interactive "P")
8229 (setq deep (if deep (prefix-numeric-value deep) -1))
8230 (save-excursion
8231 (cperl-regext-to-level-start)
8232 (let ((b (point)) (e (make-marker)))
8233 (forward-sexp 1)
8234 (set-marker e (1- (point)))
8235 (cperl-beautify-regexp-piece b e 'level deep))))
8237 (defun cperl-invert-if-unless-modifiers ()
8238 "Change `B if A;' into `if (A) {B}' etc if possible.
8239 \(Unfinished.)"
8240 (interactive)
8241 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8242 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8243 (and (= (char-syntax (preceding-char)) ?w)
8244 (forward-sexp -1))
8245 (setq pre-if (point))
8246 (cperl-backward-to-start-of-expr)
8247 (setq pre-B (point))
8248 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8249 (cperl-forward-to-end-of-expr)
8250 (setq post-A (point))
8251 (goto-char pre-if)
8252 (or (looking-at w-rex)
8253 ;; Find the position
8254 (progn (goto-char post-A)
8255 (while (and
8256 (not (looking-at w-rex))
8257 (> (point) pre-B))
8258 (forward-sexp -1))
8259 (setq pre-if (point))))
8260 (or (looking-at w-rex)
8261 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8262 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8263 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8264 ;; First, simple part: find code boundaries
8265 (forward-sexp 1)
8266 (setq post-if (point))
8267 (forward-sexp -2)
8268 (forward-sexp 1)
8269 (setq post-B (point))
8270 (cperl-backward-to-start-of-expr)
8271 (setq pre-B (point))
8272 (setq B (buffer-substring pre-B post-B))
8273 (goto-char pre-if)
8274 (forward-sexp 2)
8275 (forward-sexp -1)
8276 ;; May be after $, @, $# etc of a variable
8277 (skip-chars-backward "$@%#")
8278 (setq pre-A (point))
8279 (cperl-forward-to-end-of-expr)
8280 (setq post-A (point))
8281 (setq A (buffer-substring pre-A post-A))
8282 ;; Now modify (from end, to not break the stuff)
8283 (skip-chars-forward " \t;")
8284 (delete-region pre-A (point)) ; we move to pre-A
8285 (insert "\n" B ";\n}")
8286 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8287 (delete-region pre-if post-if)
8288 (delete-region pre-B post-B)
8289 (goto-char pre-B)
8290 (insert if-string " (" A ") {")
8291 (setq post-B (point))
8292 (if (looking-at "[ \t]+$")
8293 (delete-horizontal-space)
8294 (if (looking-at "[ \t]*#")
8295 (cperl-indent-for-comment)
8296 (just-one-space)))
8297 (forward-line 1)
8298 (if (looking-at "[ \t]*$")
8299 (progn ; delete line
8300 (delete-horizontal-space)
8301 (delete-region (point) (1+ (point)))))
8302 (cperl-indent-line)
8303 (goto-char (1- post-B))
8304 (forward-sexp 1)
8305 (cperl-indent-line)
8306 (goto-char pre-B)))
8308 (defun cperl-invert-if-unless ()
8309 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8310 If the cursor is not on the leading keyword of the BLOCK flavor of
8311 construct, will assume it is the STATEMENT flavor, so will try to find
8312 the appropriate statement modifier."
8313 (interactive)
8314 (and (= (char-syntax (preceding-char)) ?w)
8315 (forward-sexp -1))
8316 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8317 (let ((pre-if (point))
8318 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8319 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8320 (forward-sexp 2)
8321 (setq post-A (point))
8322 (forward-sexp -1)
8323 (setq pre-A (point))
8324 (setq is-block (and (eq (following-char) ?\( )
8325 (save-excursion
8326 (condition-case nil
8327 (progn
8328 (forward-sexp 2)
8329 (forward-sexp -1)
8330 (eq (following-char) ?\{ ))
8331 (error nil)))))
8332 (if is-block
8333 (progn
8334 (goto-char post-A)
8335 (forward-sexp 1)
8336 (setq post-B (point))
8337 (forward-sexp -1)
8338 (setq pre-B (point))
8339 (if (and (eq (following-char) ?\{ )
8340 (progn
8341 (cperl-backward-to-noncomment post-A)
8342 (eq (preceding-char) ?\) )))
8343 (if (condition-case nil
8344 (progn
8345 (goto-char post-B)
8346 (forward-sexp 1)
8347 (forward-sexp -1)
8348 (looking-at "\\<els\\(e\\|if\\)\\>"))
8349 (error nil))
8350 (error
8351 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8352 (goto-char (1- post-B))
8353 (cperl-backward-to-noncomment pre-B)
8354 (if (eq (preceding-char) ?\;)
8355 (forward-char -1))
8356 (setq end-B-code (point))
8357 (goto-char pre-B)
8358 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8359 (setq p (match-beginning 0)
8360 A (buffer-substring p (match-end 0))
8361 state (parse-partial-sexp pre-B p))
8362 (or (nth 3 state)
8363 (nth 4 state)
8364 (nth 5 state)
8365 (error "`%s' inside `%s' BLOCK" A if-string))
8366 (goto-char (match-end 0)))
8367 ;; Finally got it
8368 (goto-char (1+ pre-B))
8369 (skip-chars-forward " \t\n")
8370 (setq B (buffer-substring (point) end-B-code))
8371 (goto-char end-B-code)
8372 (or (looking-at ";?[ \t\n]*}")
8373 (progn
8374 (skip-chars-forward "; \t\n")
8375 (setq B-comment
8376 (buffer-substring (point) (1- post-B)))))
8377 (and (equal B "")
8378 (setq B "1"))
8379 (goto-char (1- post-A))
8380 (cperl-backward-to-noncomment pre-A)
8381 (or (looking-at "[ \t\n]*)")
8382 (goto-char (1- post-A)))
8383 (setq p (point))
8384 (goto-char (1+ pre-A))
8385 (skip-chars-forward " \t\n")
8386 (setq A (buffer-substring (point) p))
8387 (delete-region pre-B post-B)
8388 (delete-region pre-A post-A)
8389 (goto-char pre-if)
8390 (insert B " ")
8391 (and B-comment (insert B-comment " "))
8392 (just-one-space)
8393 (forward-word-strictly 1)
8394 (setq pre-A (point))
8395 (insert " " A ";")
8396 (delete-horizontal-space)
8397 (setq post-B (point))
8398 (if (looking-at "#")
8399 (indent-for-comment))
8400 (goto-char post-B)
8401 (forward-char -1)
8402 (delete-horizontal-space)
8403 (goto-char pre-A)
8404 (just-one-space)
8405 (goto-char pre-if)
8406 (setq pre-A (set-marker (make-marker) pre-A))
8407 (while (<= (point) (marker-position pre-A))
8408 (cperl-indent-line)
8409 (forward-line 1))
8410 (goto-char (marker-position pre-A))
8411 (if B-comment
8412 (progn
8413 (forward-line -1)
8414 (indent-for-comment)
8415 (goto-char (marker-position pre-A)))))
8416 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8417 ;; (error "`%s' not with an (EXPR)" if-string)
8418 (forward-sexp -1)
8419 (cperl-invert-if-unless-modifiers)))
8420 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8421 (cperl-invert-if-unless-modifiers)))
8423 (declare-function Man-getpage-in-background "man" (topic))
8425 ;; By Anthony Foiani <afoiani@uswest.com>
8426 ;; Getting help on modules in C-h f ?
8427 ;; This is a modified version of `man'.
8428 ;; Need to teach it how to lookup functions
8429 ;;;###autoload
8430 (defun cperl-perldoc (word)
8431 "Run `perldoc' on WORD."
8432 (interactive
8433 (list (let* ((default-entry (cperl-word-at-point))
8434 (input (read-string
8435 (format "perldoc entry%s: "
8436 (if (string= default-entry "")
8438 (format " (default %s)" default-entry))))))
8439 (if (string= input "")
8440 (if (string= default-entry "")
8441 (error "No perldoc args given")
8442 default-entry)
8443 input))))
8444 (require 'man)
8445 (let* ((case-fold-search nil)
8446 (is-func (and
8447 (string-match "^[a-z]+$" word)
8448 (string-match (concat "^" word "\\>")
8449 (documentation-property
8450 'cperl-short-docs
8451 'variable-documentation))))
8452 (Man-switches "")
8453 (manual-program (if is-func "perldoc -f" "perldoc")))
8454 (cond
8455 ((featurep 'xemacs)
8456 (defvar Manual-program)
8457 (defvar Manual-switches)
8458 (let ((Manual-program "perldoc")
8459 (Manual-switches (if is-func (list "-f"))))
8460 (manual-entry word)))
8462 (Man-getpage-in-background word)))))
8464 ;;;###autoload
8465 (defun cperl-perldoc-at-point ()
8466 "Run a `perldoc' on the word around point."
8467 (interactive)
8468 (cperl-perldoc (cperl-word-at-point)))
8470 (defcustom pod2man-program "pod2man"
8471 "File name for `pod2man'."
8472 :type 'file
8473 :group 'cperl)
8475 ;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8476 (defun cperl-pod-to-manpage ()
8477 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8478 (interactive)
8479 (require 'man)
8480 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8481 (bufname (concat "Man " buffer-file-name))
8482 (buffer (generate-new-buffer bufname)))
8483 (with-current-buffer buffer
8484 (let ((process-environment (copy-sequence process-environment)))
8485 ;; Prevent any attempt to use display terminal fanciness.
8486 (setenv "TERM" "dumb")
8487 (set-process-sentinel
8488 (start-process pod2man-program buffer "sh" "-c"
8489 (format (cperl-pod2man-build-command) pod2man-args))
8490 'Man-bgproc-sentinel)))))
8492 ;; Updated version by him too
8493 (defun cperl-build-manpage ()
8494 "Create a virtual manpage in Emacs from the POD in the file."
8495 (interactive)
8496 (require 'man)
8497 (cond
8498 ((featurep 'xemacs)
8499 (defvar Manual-program)
8500 (let ((Manual-program "perldoc"))
8501 (manual-entry buffer-file-name)))
8503 (let* ((manual-program "perldoc")
8504 (Man-switches ""))
8505 (Man-getpage-in-background buffer-file-name)))))
8507 (defun cperl-pod2man-build-command ()
8508 "Builds the entire background manpage and cleaning command."
8509 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8510 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8511 (while (and flist (car flist))
8512 (let ((pcom (car (car flist)))
8513 (pargs (cdr (car flist))))
8514 (setq command
8515 (concat command " | " pcom " "
8516 (mapconcat (lambda (phrase)
8517 (if (not (stringp phrase))
8518 (error "Malformed Man-filter-list"))
8519 phrase)
8520 pargs " ")))
8521 (setq flist (cdr flist))))
8522 command))
8525 (defun cperl-next-interpolated-REx-1 ()
8526 "Move point to next REx which has interpolated parts without //o.
8527 Skips RExes consisting of one interpolated variable.
8529 Note that skipped RExen are not performance hits."
8530 (interactive "")
8531 (cperl-next-interpolated-REx 1))
8533 (defun cperl-next-interpolated-REx-0 ()
8534 "Move point to next REx which has interpolated parts without //o."
8535 (interactive "")
8536 (cperl-next-interpolated-REx 0))
8538 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8539 "Move point to next REx which has interpolated parts.
8540 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8541 point and the limit of search (default to point and end of buffer).
8543 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8544 semantic may be used as a numeric argument.
8546 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8547 a result of qr//, this is not a performance hit), t for the rest."
8548 (interactive "P")
8549 (if (numberp skip) (setq skip (list 0 skip)))
8550 (or beg (setq beg (point)))
8551 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8552 (let (pp)
8553 (and (eq (get-text-property beg 'syntax-type) 'string)
8554 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8555 (cperl-map-pods-heres
8556 (function (lambda (s _e _p)
8557 (if (memq (get-text-property s 'REx-interpolated) skip)
8559 (setq pp s)
8560 nil))) ; nil stops
8561 'REx-interpolated beg limit)
8562 (if pp (goto-char pp)
8563 (message "No more interpolated REx"))))
8565 ;; Initial version contributed by Trey Belew
8566 (defun cperl-here-doc-spell ()
8567 "Spell-check HERE-documents in the Perl buffer.
8568 If a region is highlighted, restricts to the region."
8569 (interactive)
8570 (cperl-pod-spell t))
8572 (defun cperl-pod-spell (&optional do-heres)
8573 "Spell-check POD documentation.
8574 If invoked with prefix argument, will do HERE-DOCs instead.
8575 If a region is highlighted, restricts to the region."
8576 (interactive "P")
8577 (save-excursion
8578 (let (beg end)
8579 (if (region-active-p)
8580 (setq beg (min (mark) (point))
8581 end (max (mark) (point)))
8582 (setq beg (point-min)
8583 end (point-max)))
8584 (cperl-map-pods-heres (function
8585 (lambda (s e _p)
8586 (if do-heres
8587 (setq e (save-excursion
8588 (goto-char e)
8589 (forward-line -1)
8590 (point))))
8591 (ispell-region s e)
8593 (if do-heres 'here-doc-group 'in-pod)
8594 beg end))))
8596 (defun cperl-map-pods-heres (func &optional prop s end)
8597 "Executes a function over regions of pods or here-documents.
8598 PROP is the text-property to search for; default to `in-pod'. Stop when
8599 function returns nil."
8600 (let (pos posend has-prop (cont t))
8601 (or prop (setq prop 'in-pod))
8602 (or s (setq s (point-min)))
8603 (or end (setq end (point-max)))
8604 (cperl-update-syntaxification end end)
8605 (save-excursion
8606 (goto-char (setq pos s))
8607 (while (and cont (< pos end))
8608 (setq has-prop (get-text-property pos prop))
8609 (setq posend (next-single-property-change pos prop nil end))
8610 (and has-prop
8611 (setq cont (funcall func pos posend prop)))
8612 (setq pos posend)))))
8614 ;; Based on code by Masatake YAMATO:
8615 (defun cperl-get-here-doc-region (&optional pos pod)
8616 "Return HERE document region around the point.
8617 Return nil if the point is not in a HERE document region. If POD is non-nil,
8618 will return a POD section if point is in a POD section."
8619 (or pos (setq pos (point)))
8620 (cperl-update-syntaxification pos pos)
8621 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8622 (and pod
8623 (eq 'pod (get-text-property pos 'syntax-type))))
8624 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8625 (e (next-single-property-change pos 'syntax-type)))
8626 (cons b (or e (point-max))))))
8628 (defun cperl-narrow-to-here-doc (&optional pos)
8629 "Narrows editing region to the HERE-DOC at POS.
8630 POS defaults to the point."
8631 (interactive "d")
8632 (or pos (setq pos (point)))
8633 (let ((p (cperl-get-here-doc-region pos)))
8634 (or p (error "Not inside a HERE document"))
8635 (narrow-to-region (car p) (cdr p))
8636 (message
8637 "When you are finished with narrow editing, type C-x n w")))
8639 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8640 "Select the HERE-DOC (or POD section) at POS.
8641 POS defaults to the point."
8642 (interactive "d")
8643 (let ((p (cperl-get-here-doc-region pos t)))
8644 (if p
8645 (progn
8646 (goto-char (car p))
8647 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8648 (message "I do not think POS is in POD or a HERE-doc..."))))
8650 (defun cperl-facemenu-add-face-function (face _end)
8651 "A callback to process user-initiated font-change requests.
8652 Translates `bold', `italic', and `bold-italic' requests to insertion of
8653 corresponding POD directives, and `underline' to C<> POD directive.
8655 Such requests are usually bound to M-o LETTER."
8656 (or (get-text-property (point) 'in-pod)
8657 (error "Faces can only be set within POD"))
8658 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8659 (cdr (or (assq face '((bold . "B<")
8660 (italic . "I<")
8661 (bold-italic . "B<I<")
8662 (underline . "C<")))
8663 (error "Face %S not configured for cperl-mode"
8664 face))))
8666 (defun cperl-time-fontification (&optional l step lim)
8667 "Times how long it takes to do incremental fontification in a region.
8668 L is the line to start at, STEP is the number of lines to skip when
8669 doing next incremental fontification, LIM is the maximal number of
8670 incremental fontification to perform. Messages are accumulated in
8671 *Messages* buffer.
8673 May be used for pinpointing which construct slows down buffer fontification:
8674 start with default arguments, then refine the slowdown regions."
8675 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8676 (or l (setq l 1))
8677 (or step (setq step 500))
8678 (or lim (setq lim 40))
8679 (let* ((timems (function (lambda ()
8680 (let ((tt (current-time)))
8681 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8682 (tt (funcall timems)) (c 0) delta tot)
8683 (goto-char (point-min))
8684 (forward-line (1- l))
8685 (cperl-mode)
8686 (setq tot (- (- tt (setq tt (funcall timems)))))
8687 (message "cperl-mode at %s: %s" l tot)
8688 (while (and (< c lim) (not (eobp)))
8689 (forward-line step)
8690 (setq l (+ l step))
8691 (setq c (1+ c))
8692 (cperl-update-syntaxification (point) (point))
8693 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8694 (message "to %s:%6s,%7s" l delta tot))
8695 tot))
8697 (defvar font-lock-cache-position)
8699 (defun cperl-emulate-lazy-lock (&optional window-size)
8700 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8701 Start fontifying the buffer from the start (or end) using the given
8702 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8703 goes backwards; default is -50. This function is not CPerl-specific; it
8704 may be used to debug problems with delayed incremental fontification."
8705 (interactive
8706 "nSize of window for incremental fontification, negative goes backwards: ")
8707 (or window-size (setq window-size -50))
8708 (let ((pos (if (> window-size 0)
8709 (point-min)
8710 (point-max)))
8712 (goto-char pos)
8713 (normal-mode)
8714 ;; Why needed??? With older font-locks???
8715 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8716 (while (if (> window-size 0)
8717 (< pos (point-max))
8718 (> pos (point-min)))
8719 (setq p (progn
8720 (forward-line window-size)
8721 (point)))
8722 (font-lock-fontify-region (min p pos) (max p pos))
8723 (setq pos p))))
8726 (defvar cperl-help-shown nil
8727 "Non-nil means that the help was already shown now.")
8729 (defvar cperl-lazy-installed nil
8730 "Non-nil means that the lazy-help handlers are installed now.")
8732 ;; FIXME: Use eldoc?
8733 (defun cperl-lazy-install ()
8734 "Switch on Auto-Help on Perl constructs (put in the message area).
8735 Delay of auto-help controlled by `cperl-lazy-help-time'."
8736 (interactive)
8737 (make-local-variable 'cperl-help-shown)
8738 (if (and (cperl-val 'cperl-lazy-help-time)
8739 (not cperl-lazy-installed))
8740 (progn
8741 (add-hook 'post-command-hook #'cperl-lazy-hook)
8742 (run-with-idle-timer
8743 (cperl-val 'cperl-lazy-help-time 1000000 5)
8745 #'cperl-get-help-defer)
8746 (setq cperl-lazy-installed t))))
8748 (defun cperl-lazy-unstall ()
8749 "Switch off Auto-Help on Perl constructs (put in the message area).
8750 Delay of auto-help controlled by `cperl-lazy-help-time'."
8751 (interactive)
8752 (remove-hook 'post-command-hook #'cperl-lazy-hook)
8753 (cancel-function-timers #'cperl-get-help-defer)
8754 (setq cperl-lazy-installed nil))
8756 (defun cperl-lazy-hook ()
8757 (setq cperl-help-shown nil))
8759 (defun cperl-get-help-defer ()
8760 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8761 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8762 (cperl-get-help)
8763 (setq cperl-help-shown t))))
8764 (cperl-lazy-install)
8767 ;;; Plug for wrong font-lock:
8769 (defun cperl-font-lock-unfontify-region-function (beg end)
8770 (with-silent-modifications
8771 (remove-text-properties beg end '(face nil))))
8773 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8774 "Extends the region to safe positions, then calls the default function.
8775 Newer `font-lock's can do it themselves.
8776 We unwind only as far as needed for fontification. Syntaxification may
8777 do extra unwind via `cperl-unwind-to-safe'."
8778 (save-excursion
8779 (goto-char beg)
8780 (while (and beg
8781 (progn
8782 (beginning-of-line)
8783 (eq (get-text-property (setq beg (point)) 'syntax-type)
8784 'multiline)))
8785 (let ((new-beg (cperl-beginning-of-property beg 'syntax-type)))
8786 (setq beg (if (= new-beg beg) nil new-beg))
8787 (goto-char new-beg)))
8788 (setq beg (point))
8789 (goto-char end)
8790 (while (and end
8791 (progn
8792 (or (bolp) (condition-case nil
8793 (forward-line 1)
8794 (error nil)))
8795 (eq (get-text-property (setq end (point)) 'syntax-type)
8796 'multiline)))
8797 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8798 (goto-char end))
8799 (setq end (point)))
8800 (font-lock-default-fontify-region beg end loudly))
8802 (defvar cperl-d-l nil)
8803 (defvar edebug-backtrace-buffer) ;FIXME: Why?
8804 (defun cperl-fontify-syntaxically (end)
8805 ;; Some vars for debugging only
8806 ;; (message "Syntaxifying...")
8807 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8808 (istate (car cperl-syntax-state))
8809 start from-start edebug-backtrace-buffer)
8810 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8811 (progn
8812 (require 'edebug)
8813 (let ((f 'edebug-backtrace))
8814 (funcall f)))) ; Avoid compile-time warning
8815 (or cperl-syntax-done-to
8816 (setq cperl-syntax-done-to (point-min)
8817 from-start t))
8818 (setq start (if (and cperl-hook-after-change
8819 (not from-start))
8820 cperl-syntax-done-to ; Fontify without change; ignore start
8821 ;; Need to forget what is after `start'
8822 (min cperl-syntax-done-to (point))))
8823 (goto-char start)
8824 (beginning-of-line)
8825 (setq start (point))
8826 (and cperl-syntaxify-unwind
8827 (setq end (cperl-unwind-to-safe t end)
8828 start (point)))
8829 (and (> end start)
8830 (setq cperl-syntax-done-to start) ; In case what follows fails
8831 (cperl-find-pods-heres start end t nil t))
8832 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8833 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8834 dbg iend start end idone cperl-syntax-done-to
8835 istate (car cperl-syntax-state))) ; For debugging
8836 nil)) ; Do not iterate
8838 (defun cperl-fontify-update (end)
8839 (let ((pos (point-min)) prop posend)
8840 (setq end (point-max))
8841 (while (< pos end)
8842 (setq prop (get-text-property pos 'cperl-postpone)
8843 posend (next-single-property-change pos 'cperl-postpone nil end))
8844 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8845 (setq pos posend)))
8846 nil) ; Do not iterate
8848 (defun cperl-fontify-update-bad (end)
8849 ;; Since fontification happens with different region than syntaxification,
8850 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8851 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8852 (if prop
8853 (setq pos (or (cperl-beginning-of-property
8854 (cperl-1+ pos) 'cperl-postpone)
8855 (point-min))))
8856 (while (< pos end)
8857 (setq posend (next-single-property-change pos 'cperl-postpone))
8858 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8859 (setq pos posend)
8860 (setq prop (get-text-property pos 'cperl-postpone))))
8861 nil) ; Do not iterate
8863 ;; Called when any modification is made to buffer text.
8864 (defun cperl-after-change-function (beg _end _old-len)
8865 ;; We should have been informed about changes by `font-lock'. Since it
8866 ;; does not inform as which calls are deferred, do it ourselves
8867 (if cperl-syntax-done-to
8868 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8870 (defun cperl-update-syntaxification (from to)
8871 (cond
8872 ((not cperl-use-syntax-table-text-property) nil)
8873 ((fboundp 'syntax-propertize) (syntax-propertize to))
8874 ((and cperl-syntaxify-by-font-lock
8875 (or (null cperl-syntax-done-to)
8876 (< cperl-syntax-done-to to)))
8877 (save-excursion
8878 (goto-char from)
8879 (cperl-fontify-syntaxically to)))))
8881 (defvar cperl-version
8882 (let ((v "Revision: 6.2"))
8883 (string-match ":\\s *\\([0-9.]+\\)" v)
8884 (substring v (match-beginning 1) (match-end 1)))
8885 "Version of IZ-supported CPerl package this file is based on.")
8887 (provide 'cperl-mode)
8889 ;;; cperl-mode.el ends here