(cperl-mode): Modify cperl-compilation-error-regexp-alist by appending.
[emacs.git] / lisp / progmodes / cperl-mode.el
blob0b8287503f860518516c94f42ebcf8698eb6156b
1 ;;; cperl-mode.el --- Perl code editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 ;; 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 ;; Free Software Foundation, Inc.
7 ;; Author: Ilya Zakharevich and Bob Olson
8 ;; Maintainer: Ilya Zakharevich <ilyaz@cpan.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 2, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
30 ;;; Commentary:
32 ;; You can either fine-tune the bells and whistles of this mode or
33 ;; bulk enable them by putting
35 ;; (setq cperl-hairy t)
37 ;; in your .emacs file. (Emacs rulers do not consider it politically
38 ;; correct to make whistles enabled by default.)
40 ;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<<
41 ;; or as help on variables `cperl-tips', `cperl-problems', <<<<<<
42 ;; `cperl-praise', `cperl-speed'. <<<<<<
44 ;; The mode information (on C-h m) provides some customization help.
45 ;; If you use font-lock feature of this mode, it is advisable to use
46 ;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock.
48 ;; Faces used now: three faces for first-class and second-class keywords
49 ;; and control flow words, one for each: comments, string, labels,
50 ;; functions definitions and packages, arrays, hashes, and variable
51 ;; definitions. If you do not see all these faces, your font-lock does
52 ;; not define them, so you need to define them manually.
54 ;; This mode supports font-lock, imenu and mode-compile. In the
55 ;; hairy version font-lock is on, but you should activate imenu
56 ;; yourself (note that mode-compile is not standard yet). Well, you
57 ;; can use imenu from keyboard anyway (M-x imenu), but it is better
58 ;; to bind it like that:
60 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
62 ;;; Font lock bugs as of v4.32:
64 ;; The following kinds of Perl code erroneously start strings:
65 ;; \$` \$' \$"
66 ;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
67 ;; likewise with m, tr, y, q, qX instead of s
69 ;;; Code:
71 (defvar vc-rcs-header)
72 (defvar vc-sccs-header)
74 (eval-when-compile
75 (condition-case nil
76 (require 'custom)
77 (error nil))
78 (condition-case nil
79 (require 'man)
80 (error nil))
81 (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
82 (defvar cperl-can-font-lock
83 (or cperl-xemacs-p
84 (and (boundp 'emacs-major-version)
85 (or window-system
86 (> emacs-major-version 20)))))
87 (if cperl-can-font-lock
88 (require 'font-lock))
89 (defvar msb-menu-cond)
90 (defvar gud-perldb-history)
91 (defvar font-lock-background-mode) ; not in Emacs
92 (defvar font-lock-display-type) ; ditto
93 (defvar paren-backwards-message) ; Not in newer XEmacs?
94 (or (fboundp 'defgroup)
95 (defmacro defgroup (name val doc &rest arr)
96 nil))
97 (or (fboundp 'custom-declare-variable)
98 (defmacro defcustom (name val doc &rest arr)
99 (` (defvar (, name) (, val) (, doc)))))
100 (or (and (fboundp 'custom-declare-variable)
101 (string< "19.31" emacs-version)) ; Checked with 19.30: defface does not work
102 (defmacro defface (&rest arr)
103 nil))
104 ;; Avoid warning (tmp definitions)
105 (or (fboundp 'x-color-defined-p)
106 (defmacro x-color-defined-p (col)
107 (cond ((fboundp 'color-defined-p) (` (color-defined-p (, col))))
108 ;; XEmacs >= 19.12
109 ((fboundp 'valid-color-name-p) (` (valid-color-name-p (, col))))
110 ;; XEmacs 19.11
111 ((fboundp 'x-valid-color-name-p) (` (x-valid-color-name-p (, col))))
112 (t '(error "Cannot implement color-defined-p")))))
113 (defmacro cperl-is-face (arg) ; Takes quoted arg
114 (cond ((fboundp 'find-face)
115 (` (find-face (, arg))))
116 (;;(and (fboundp 'face-list)
117 ;; (face-list))
118 (fboundp 'face-list)
119 (` (member (, arg) (and (fboundp 'face-list)
120 (face-list)))))
122 (` (boundp (, arg))))))
123 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
124 (cond ((fboundp 'make-face)
125 (` (make-face (quote (, arg)))))
127 (` (defvar (, arg) (quote (, arg)) (, descr))))))
128 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
129 (` (progn
130 (or (cperl-is-face (quote (, arg)))
131 (cperl-make-face (, arg) (, descr)))
132 (or (boundp (quote (, arg))) ; We use unquoted variants too
133 (defvar (, arg) (quote (, arg)) (, descr))))))
134 (if cperl-xemacs-p
135 (defmacro cperl-etags-snarf-tag (file line)
136 (` (progn
137 (beginning-of-line 2)
138 (list (, file) (, line)))))
139 (defmacro cperl-etags-snarf-tag (file line)
140 (` (etags-snarf-tag))))
141 (if cperl-xemacs-p
142 (defmacro cperl-etags-goto-tag-location (elt)
143 (`;;(progn
144 ;; (switch-to-buffer (get-file-buffer (elt (, elt) 0)))
145 ;; (set-buffer (get-file-buffer (elt (, elt) 0)))
146 ;; Probably will not work due to some save-excursion???
147 ;; Or save-file-position?
148 ;; (message "Did I get to line %s?" (elt (, elt) 1))
149 (goto-line (string-to-int (elt (, elt) 1)))))
151 (defmacro cperl-etags-goto-tag-location (elt)
152 (` (etags-goto-tag-location (, elt))))))
154 (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
156 (defvar cperl-can-font-lock
157 (or cperl-xemacs-p
158 (and (boundp 'emacs-major-version)
159 (or window-system
160 (> emacs-major-version 20)))))
162 (defun cperl-choose-color (&rest list)
163 (let (answer)
164 (while list
165 (or answer
166 (if (or (x-color-defined-p (car list))
167 (null (cdr list)))
168 (setq answer (car list))))
169 (setq list (cdr list)))
170 answer))
172 (defgroup cperl nil
173 "Major mode for editing Perl code."
174 :prefix "cperl-"
175 :group 'languages
176 :version "20.3")
178 (defgroup cperl-indentation-details nil
179 "Indentation."
180 :prefix "cperl-"
181 :group 'cperl)
183 (defgroup cperl-affected-by-hairy nil
184 "Variables affected by `cperl-hairy'."
185 :prefix "cperl-"
186 :group 'cperl)
188 (defgroup cperl-autoinsert-details nil
189 "Auto-insert tuneup."
190 :prefix "cperl-"
191 :group 'cperl)
193 (defgroup cperl-faces nil
194 "Fontification colors."
195 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
196 :prefix "cperl-"
197 :group 'cperl)
199 (defgroup cperl-speed nil
200 "Speed vs. validity tuneup."
201 :prefix "cperl-"
202 :group 'cperl)
204 (defgroup cperl-help-system nil
205 "Help system tuneup."
206 :prefix "cperl-"
207 :group 'cperl)
210 (defcustom cperl-extra-newline-before-brace nil
211 "*Non-nil means that if, elsif, while, until, else, for, foreach
212 and do constructs look like:
214 if ()
218 instead of:
220 if () {
222 :type 'boolean
223 :group 'cperl-autoinsert-details)
225 (defcustom cperl-extra-newline-before-brace-multiline
226 cperl-extra-newline-before-brace
227 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
228 for constructs with multiline if/unless/while/until/for/foreach condition."
229 :type 'boolean
230 :group 'cperl-autoinsert-details)
232 (defcustom cperl-indent-level 2
233 "*Indentation of CPerl statements with respect to containing block."
234 :type 'integer
235 :group 'cperl-indentation-details)
237 (defcustom cperl-lineup-step nil
238 "*`cperl-lineup' will always lineup at multiple of this number.
239 If nil, the value of `cperl-indent-level' will be used."
240 :type '(choice (const nil) integer)
241 :group 'cperl-indentation-details)
243 (defcustom cperl-brace-imaginary-offset 0
244 "*Imagined indentation of a Perl open brace that actually follows a statement.
245 An open brace following other text is treated as if it were this far
246 to the right of the start of its line."
247 :type 'integer
248 :group 'cperl-indentation-details)
250 (defcustom cperl-brace-offset 0
251 "*Extra indentation for braces, compared with other text in same context."
252 :type 'integer
253 :group 'cperl-indentation-details)
254 (defcustom cperl-label-offset -2
255 "*Offset of CPerl label lines relative to usual indentation."
256 :type 'integer
257 :group 'cperl-indentation-details)
258 (defcustom cperl-min-label-indent 1
259 "*Minimal offset of CPerl label lines."
260 :type 'integer
261 :group 'cperl-indentation-details)
262 (defcustom cperl-continued-statement-offset 2
263 "*Extra indent for lines not starting new statements."
264 :type 'integer
265 :group 'cperl-indentation-details)
266 (defcustom cperl-continued-brace-offset 0
267 "*Extra indent for substatements that start with open-braces.
268 This is in addition to cperl-continued-statement-offset."
269 :type 'integer
270 :group 'cperl-indentation-details)
271 (defcustom cperl-close-paren-offset -1
272 "*Extra indent for substatements that start with close-parenthesis."
273 :type 'integer
274 :group 'cperl-indentation-details)
276 (defcustom cperl-indent-wrt-brace t
277 "*Non-nil means indent statements in if/etc block relative brace, not if/etc.
278 Versions 5.2 ... 5.20 behaved as if this were `nil'."
279 :type 'boolean
280 :group 'cperl-indentation-details)
282 (defcustom cperl-auto-newline nil
283 "*Non-nil means automatically newline before and after braces,
284 and after colons and semicolons, inserted in CPerl code. The following
285 \\[cperl-electric-backspace] will remove the inserted whitespace.
286 Insertion after colons requires both this variable and
287 `cperl-auto-newline-after-colon' set."
288 :type 'boolean
289 :group 'cperl-autoinsert-details)
291 (defcustom cperl-autoindent-on-semi nil
292 "*Non-nil means automatically indent after insertion of (semi)colon.
293 Active if `cperl-auto-newline' is false."
294 :type 'boolean
295 :group 'cperl-autoinsert-details)
297 (defcustom cperl-auto-newline-after-colon nil
298 "*Non-nil means automatically newline even after colons.
299 Subject to `cperl-auto-newline' setting."
300 :type 'boolean
301 :group 'cperl-autoinsert-details)
303 (defcustom cperl-tab-always-indent t
304 "*Non-nil means TAB in CPerl mode should always reindent the current line,
305 regardless of where in the line point is when the TAB command is used."
306 :type 'boolean
307 :group 'cperl-indentation-details)
309 (defcustom cperl-font-lock nil
310 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
311 Can be overwritten by `cperl-hairy' if nil."
312 :type '(choice (const null) boolean)
313 :group 'cperl-affected-by-hairy)
315 (defcustom cperl-electric-lbrace-space nil
316 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
317 Can be overwritten by `cperl-hairy' if nil."
318 :type '(choice (const null) boolean)
319 :group 'cperl-affected-by-hairy)
321 (defcustom cperl-electric-parens-string "({[]})<"
322 "*String of parentheses that should be electric in CPerl.
323 Closing ones are electric only if the region is highlighted."
324 :type 'string
325 :group 'cperl-affected-by-hairy)
327 (defcustom cperl-electric-parens nil
328 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
329 Can be overwritten by `cperl-hairy' if nil."
330 :type '(choice (const null) boolean)
331 :group 'cperl-affected-by-hairy)
333 (defvar zmacs-regions) ; Avoid warning
335 (defcustom cperl-electric-parens-mark
336 (and window-system
337 (or (and (boundp 'transient-mark-mode) ; For Emacs
338 transient-mark-mode)
339 (and (boundp 'zmacs-regions) ; For XEmacs
340 zmacs-regions)))
341 "*Not-nil means that electric parens look for active mark.
342 Default is yes if there is visual feedback on mark."
343 :type 'boolean
344 :group 'cperl-autoinsert-details)
346 (defcustom cperl-electric-linefeed nil
347 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
348 In any case these two mean plain and hairy linefeeds together.
349 Can be overwritten by `cperl-hairy' if nil."
350 :type '(choice (const null) boolean)
351 :group 'cperl-affected-by-hairy)
353 (defcustom cperl-electric-keywords nil
354 "*Not-nil (and non-null) means keywords are electric in CPerl.
355 Can be overwritten by `cperl-hairy' if nil.
357 Uses `abbrev-mode' to do the expansion. If you want to use your
358 own abbrevs in cperl-mode, but do not want keywords to be
359 electric, you must redefine `cperl-mode-abbrev-table': do
360 \\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
361 that paragraph, delete the words that appear at the ends of lines and
362 that begin with \"cperl-electric\".
364 :type '(choice (const null) boolean)
365 :group 'cperl-affected-by-hairy)
367 (defcustom cperl-electric-backspace-untabify t
368 "*Not-nil means electric-backspace will untabify in CPerl."
369 :type 'boolean
370 :group 'cperl-autoinsert-details)
372 (defcustom cperl-hairy nil
373 "*Not-nil means most of the bells and whistles are enabled in CPerl.
374 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
375 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
376 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
377 `cperl-lazy-help-time'."
378 :type 'boolean
379 :group 'cperl-affected-by-hairy)
381 (defcustom cperl-comment-column 32
382 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
383 :type 'integer
384 :group 'cperl-indentation-details)
386 (defcustom cperl-indent-comment-at-column-0 nil
387 "*Non-nil means that comment started at column 0 should be indentable."
388 :type 'boolean
389 :group 'cperl-indentation-details)
391 (defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
392 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
393 :type '(repeat string)
394 :group 'cperl)
396 (defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
397 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
398 :type '(repeat string)
399 :group 'cperl)
401 ;; This became obsolete...
402 (defvar cperl-vc-header-alist nil)
403 (make-obsolete-variable
404 'cperl-vc-header-alist
405 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead.")
407 (defcustom cperl-clobber-mode-lists
408 (not
409 (and
410 (boundp 'interpreter-mode-alist)
411 (assoc "miniperl" interpreter-mode-alist)
412 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
413 "*Whether to install us into `interpreter-' and `extension' mode lists."
414 :type 'boolean
415 :group 'cperl)
417 (defcustom cperl-info-on-command-no-prompt nil
418 "*Not-nil (and non-null) means not to prompt on C-h f.
419 The opposite behavior is always available if prefixed with C-c.
420 Can be overwritten by `cperl-hairy' if nil."
421 :type '(choice (const null) boolean)
422 :group 'cperl-affected-by-hairy)
424 (defcustom cperl-clobber-lisp-bindings nil
425 "*Not-nil (and non-null) means not overwrite C-h f.
426 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
427 Can be overwritten by `cperl-hairy' if nil."
428 :type '(choice (const null) boolean)
429 :group 'cperl-affected-by-hairy)
431 (defcustom cperl-lazy-help-time nil
432 "*Not-nil (and non-null) means to show lazy help after given idle time.
433 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
434 :type '(choice (const null) (const nil) integer)
435 :group 'cperl-affected-by-hairy)
437 (defcustom cperl-pod-face 'font-lock-comment-face
438 "*Face for POD highlighting."
439 :type 'face
440 :group 'cperl-faces)
442 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
443 "*Face for POD highlighting.
444 Font for POD headers."
445 :type 'face
446 :group 'cperl-faces)
448 (defcustom cperl-here-face 'font-lock-string-face
449 "*Face for here-docs highlighting."
450 :type 'face
451 :group 'cperl-faces)
453 ;;; Some double-evaluation happened with font-locks... Needed with 21.2...
454 (defvar cperl-singly-quote-face cperl-xemacs-p)
456 (defcustom cperl-invalid-face 'underline
457 "*Face for highlighting trailing whitespace."
458 :type 'face
459 :version "21.1"
460 :group 'cperl-faces)
462 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
463 "*Not-nil after evaluation means to highlight POD and here-docs sections."
464 :type 'boolean
465 :group 'cperl-faces)
467 (defcustom cperl-fontify-m-as-s t
468 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
469 :type 'boolean
470 :group 'cperl-faces)
472 (defcustom cperl-highlight-variables-indiscriminately nil
473 "*Non-nil means perform additional highlighting on variables.
474 Currently only changes how scalar variables are highlighted.
475 Note that that variable is only read at initialization time for
476 the variable `cperl-font-lock-keywords-2', so changing it after you've
477 entered CPerl mode the first time will have no effect."
478 :type 'boolean
479 :group 'cperl)
481 (defcustom cperl-pod-here-scan t
482 "*Not-nil means look for POD and here-docs sections during startup.
483 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
484 :type 'boolean
485 :group 'cperl-speed)
487 (defcustom cperl-regexp-scan t
488 "*Not-nil means make marking of regular expression more thorough.
489 Effective only with `cperl-pod-here-scan'."
490 :type 'boolean
491 :group 'cperl-speed)
493 (defcustom cperl-hook-after-change t
494 "*Not-nil means install hook to know which regions of buffer are changed.
495 May significantly speed up delayed fontification. Changes take effect
496 after reload."
497 :type 'boolean
498 :group 'cperl-speed)
500 (defcustom cperl-imenu-addback nil
501 "*Not-nil means add backreferences to generated `imenu's.
502 May require patched `imenu' and `imenu-go'. Obsolete."
503 :type 'boolean
504 :group 'cperl-help-system)
506 (defcustom cperl-max-help-size 66
507 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
508 :type '(choice integer (const nil))
509 :group 'cperl-help-system)
511 (defcustom cperl-shrink-wrap-info-frame t
512 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
513 :type 'boolean
514 :group 'cperl-help-system)
516 (defcustom cperl-info-page "perl"
517 "*Name of the info page containing perl docs.
518 Older version of this page was called `perl5', newer `perl'."
519 :type 'string
520 :group 'cperl-help-system)
522 (defcustom cperl-use-syntax-table-text-property
523 (boundp 'parse-sexp-lookup-properties)
524 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
525 :type 'boolean
526 :group 'cperl-speed)
528 (defcustom cperl-use-syntax-table-text-property-for-tags
529 cperl-use-syntax-table-text-property
530 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
531 :type 'boolean
532 :group 'cperl-speed)
534 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
535 "*Regexp to match files to scan when generating TAGS."
536 :type 'regexp
537 :group 'cperl)
539 (defcustom cperl-noscan-files-regexp
540 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
541 "*Regexp to match files/dirs to skip when generating TAGS."
542 :type 'regexp
543 :group 'cperl)
545 (defcustom cperl-regexp-indent-step nil
546 "*Indentation used when beautifying regexps.
547 If nil, the value of `cperl-indent-level' will be used."
548 :type '(choice integer (const nil))
549 :group 'cperl-indentation-details)
551 (defcustom cperl-indent-left-aligned-comments t
552 "*Non-nil means that the comment starting in leftmost column should indent."
553 :type 'boolean
554 :group 'cperl-indentation-details)
556 (defcustom cperl-under-as-char nil
557 "*Non-nil means that the _ (underline) should be treated as word char."
558 :type 'boolean
559 :group 'cperl)
561 (defcustom cperl-extra-perl-args ""
562 "*Extra arguments to use when starting Perl.
563 Currently used with `cperl-check-syntax' only."
564 :type 'string
565 :group 'cperl)
567 (defcustom cperl-message-electric-keyword t
568 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
569 :type 'boolean
570 :group 'cperl-help-system)
572 (defcustom cperl-indent-region-fix-constructs 1
573 "*Amount of space to insert between `}' and `else' or `elsif'
574 in `cperl-indent-region'. Set to nil to leave as is. Values other
575 than 1 and nil will probably not work."
576 :type '(choice (const nil) (const 1))
577 :group 'cperl-indentation-details)
579 (defcustom cperl-break-one-line-blocks-when-indent t
580 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
581 need to be reformatted into multiline ones when indenting a region."
582 :type 'boolean
583 :group 'cperl-indentation-details)
585 (defcustom cperl-fix-hanging-brace-when-indent t
586 "*Non-nil means that BLOCK-end `}' may be put on a separate line
587 when indenting a region.
588 Braces followed by else/elsif/while/until are excepted."
589 :type 'boolean
590 :group 'cperl-indentation-details)
592 (defcustom cperl-merge-trailing-else t
593 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
594 may be merged to be on the same line when indenting a region."
595 :type 'boolean
596 :group 'cperl-indentation-details)
598 (defcustom cperl-indent-parens-as-block nil
599 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
600 but for trailing \",\" inside the group, which won't increase indentation.
601 One should tune up `cperl-close-paren-offset' as well."
602 :type 'boolean
603 :group 'cperl-indentation-details)
605 (defcustom cperl-syntaxify-by-font-lock
606 (and cperl-can-font-lock
607 (boundp 'parse-sexp-lookup-properties))
608 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
609 :type '(choice (const message) boolean)
610 :group 'cperl-speed)
612 (defcustom cperl-syntaxify-unwind
614 "*Non-nil means that CPerl unwinds to a start of a long construction
615 when syntaxifying a chunk of buffer."
616 :type 'boolean
617 :group 'cperl-speed)
619 (defcustom cperl-syntaxify-for-menu
621 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
622 This way enabling/disabling of menu items is more correct."
623 :type 'boolean
624 :group 'cperl-speed)
626 (defcustom cperl-ps-print-face-properties
627 '((font-lock-keyword-face nil nil bold shadow)
628 (font-lock-variable-name-face nil nil bold)
629 (font-lock-function-name-face nil nil bold italic box)
630 (font-lock-constant-face nil "LightGray" bold)
631 (cperl-array-face nil "LightGray" bold underline)
632 (cperl-hash-face nil "LightGray" bold italic underline)
633 (font-lock-comment-face nil "LightGray" italic)
634 (font-lock-string-face nil nil italic underline)
635 (cperl-nonoverridable-face nil nil italic underline)
636 (font-lock-type-face nil nil underline)
637 (font-lock-warning-face nil "LightGray" bold italic box)
638 (underline nil "LightGray" strikeout))
639 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
640 :type '(repeat (cons symbol
641 (cons (choice (const nil) string)
642 (cons (choice (const nil) string)
643 (repeat symbol)))))
644 :group 'cperl-faces)
646 (defvar cperl-dark-background
647 (cperl-choose-color "navy" "os2blue" "darkgreen"))
648 (defvar cperl-dark-foreground
649 (cperl-choose-color "orchid1" "orange"))
651 (defface cperl-nonoverridable-face
652 `((((class grayscale) (background light))
653 (:background "Gray90" :slant italic :underline t))
654 (((class grayscale) (background dark))
655 (:foreground "Gray80" :slant italic :underline t :weight bold))
656 (((class color) (background light))
657 (:foreground "chartreuse3"))
658 (((class color) (background dark))
659 (:foreground ,cperl-dark-foreground))
660 (t (:weight bold :underline t)))
661 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
662 :group 'cperl-faces)
664 (defface cperl-array-face
665 `((((class grayscale) (background light))
666 (:background "Gray90" :weight bold))
667 (((class grayscale) (background dark))
668 (:foreground "Gray80" :weight bold))
669 (((class color) (background light))
670 (:foreground "Blue" :background "lightyellow2" :weight bold))
671 (((class color) (background dark))
672 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
673 (t (:weight bold)))
674 "Font Lock mode face used to highlight array names."
675 :group 'cperl-faces)
677 (defface cperl-hash-face
678 `((((class grayscale) (background light))
679 (:background "Gray90" :weight bold :slant italic))
680 (((class grayscale) (background dark))
681 (:foreground "Gray80" :weight bold :slant italic))
682 (((class color) (background light))
683 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
684 (((class color) (background dark))
685 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
686 (t (:weight bold :slant italic)))
687 "Font Lock mode face used to highlight hash names."
688 :group 'cperl-faces)
692 ;;; Short extra-docs.
694 (defvar cperl-tips 'please-ignore-this-line
695 "Get maybe newer version of this package from
696 http://ilyaz.org/software/emacs
697 Subdirectory `cperl-mode' may contain yet newer development releases and/or
698 patches to related files.
700 For best results apply to an older Emacs the patches from
701 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
702 \(this upgrades syntax-parsing abilities of Emacsen v19.34 and
703 v20.2 up to the level of Emacs v20.3 - a must for a good Perl
704 mode.) As of beginning of 2003, XEmacs may provide a similar ability.
706 Get support packages choose-color.el (or font-lock-extra.el before
707 19.30), imenu-go.el from the same place. \(Look for other files there
708 too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
709 later you should use choose-color.el *instead* of font-lock-extra.el
710 \(and you will not get smart highlighting in C :-().
712 Note that to enable Compile choices in the menu you need to install
713 mode-compile.el.
715 If your Emacs does not default to `cperl-mode' on Perl files, and you
716 want it to: put the following into your .emacs file:
718 (defalias 'perl-mode 'cperl-mode)
720 Get perl5-info from
721 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
722 Also, one can generate a newer documentation running `pod2texi' converter
723 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
725 If you use imenu-go, run imenu on perl5-info buffer (you can do it
726 from Perl menu). If many files are related, generate TAGS files from
727 Tools/Tags submenu in Perl menu.
729 If some class structure is too complicated, use Tools/Hierarchy-view
730 from Perl menu, or hierarchic view of imenu. The second one uses the
731 current buffer only, the first one requires generation of TAGS from
732 Perl/Tools/Tags menu beforehand.
734 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
736 Switch auto-help on/off with Perl/Tools/Auto-help.
738 Though with contemporary Emaxen CPerl mode should maintain the correct
739 parsing of Perl even when editing, sometimes it may be lost. Fix this by
741 \\[normal-mode]
743 In cases of more severe confusion sometimes it is helpful to do
745 \\[load-library] cperl-mode RET
746 \\[normal-mode]
748 Before reporting (non-)problems look in the problem section of online
749 micro-docs on what I know about CPerl problems.")
751 (defvar cperl-problems 'please-ignore-this-line
752 "Description of problems in CPerl mode.
753 Some faces will not be shown on some versions of Emacs unless you
754 install choose-color.el, available from
755 http://ilyaz.org/software/emacs
757 `fill-paragraph' on a comment may leave the point behind the
758 paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
759 to detect it and bulk out).
761 See documentation of a variable `cperl-problems-old-emaxen' for the
762 problems which disappear if you upgrade Emacs to a reasonably new
763 version (20.3 for Emacs, and those of 2004 for XEmacs).")
765 (defvar cperl-problems-old-emaxen 'please-ignore-this-line
766 "Description of problems in CPerl mode specific for older Emacs versions.
768 Emacs had a _very_ restricted syntax parsing engine until version
769 20.1. Most problems below are corrected starting from this version of
770 Emacs, and all of them should be fixed in version 20.3. (Or apply
771 patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
772 this respect (until 2003).
774 Note that even with newer Emacsen in some very rare cases the details
775 of interaction of `font-lock' and syntaxification may be not cleaned
776 up yet. You may get slightly different colors basing on the order of
777 fontification and syntaxification. Say, the initial faces is correct,
778 but editing the buffer breaks this.
780 Even with older Emacsen CPerl mode tries to corrects some Emacs
781 misunderstandings, however, for efficiency reasons the degree of
782 correction is different for different operations. The partially
783 corrected problems are: POD sections, here-documents, regexps. The
784 operations are: highlighting, indentation, electric keywords, electric
785 braces.
787 This may be confusing, since the regexp s#//#/#\; may be highlighted
788 as a comment, but it will be recognized as a regexp by the indentation
789 code. Or the opposite case, when a POD section is highlighted, but
790 may break the indentation of the following code (though indentation
791 should work if the balance of delimiters is not broken by POD).
793 The main trick (to make $ a \"backslash\") makes constructions like
794 ${aaa} look like unbalanced braces. The only trick I can think of is
795 to insert it as $ {aaa} (valid in perl5, not in perl4).
797 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
798 as /($|\\s)/. Note that such a transposition is not always possible.
800 The solution is to upgrade your Emacs or patch an older one. Note
801 that Emacs 20.2 has some bugs related to `syntax-table' text
802 properties. Patches are available on the main CPerl download site,
803 and on CPAN.
805 If these bugs cannot be fixed on your machine (say, you have an inferior
806 environment and cannot recompile), you may still disable all the fancy stuff
807 via `cperl-use-syntax-table-text-property'.")
809 (defvar cperl-praise 'please-ignore-this-line
810 "Advantages of CPerl mode.
812 0) It uses the newest `syntax-table' property ;-);
814 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
815 mode - but the latter number may have improved too in last years) even
816 with old Emaxen which do not support `syntax-table' property.
818 When using `syntax-table' property for syntax assist hints, it should
819 handle 99.995% of lines correct - or somesuch. It automatically
820 updates syntax assist hints when you edit your script.
822 2) It is generally believed to be \"the most user-friendly Emacs
823 package\" whatever it may mean (I doubt that the people who say similar
824 things tried _all_ the rest of Emacs ;-), but this was not a lonely
825 voice);
827 3) Everything is customizable, one-by-one or in a big sweep;
829 4) It has many easily-accessable \"tools\":
830 a) Can run program, check syntax, start debugger;
831 b) Can lineup vertically \"middles\" of rows, like `=' in
832 a = b;
833 cc = d;
834 c) Can insert spaces where this impoves readability (in one
835 interactive sweep over the buffer);
836 d) Has support for imenu, including:
837 1) Separate unordered list of \"interesting places\";
838 2) Separate TOC of POD sections;
839 3) Separate list of packages;
840 4) Hierarchical view of methods in (sub)packages;
841 5) and functions (by the full name - with package);
842 e) Has an interface to INFO docs for Perl; The interface is
843 very flexible, including shrink-wrapping of
844 documentation buffer/frame;
845 f) Has a builtin list of one-line explanations for perl constructs.
846 g) Can show these explanations if you stay long enough at the
847 corresponding place (or on demand);
848 h) Has an enhanced fontification (using 3 or 4 additional faces
849 comparing to font-lock - basically, different
850 namespaces in Perl have different colors);
851 i) Can construct TAGS basing on its knowledge of Perl syntax,
852 the standard menu has 6 different way to generate
853 TAGS (if \"by directory\", .xs files - with C-language
854 bindings - are included in the scan);
855 j) Can build a hierarchical view of classes (via imenu) basing
856 on generated TAGS file;
857 k) Has electric parentheses, electric newlines, uses Abbrev
858 for electric logical constructs
859 while () {}
860 with different styles of expansion (context sensitive
861 to be not so bothering). Electric parentheses behave
862 \"as they should\" in a presence of a visible region.
863 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
864 m) Can convert from
865 if (A) { B }
867 B if A;
869 n) Highlights (by user-choice) either 3-delimiters constructs
870 (such as tr/a/b/), or regular expressions and `y/tr';
871 o) Highlights trailing whitespace;
872 p) Is able to manipulate Perl Regular Expressions to ease
873 conversion to a more readable form.
874 q) Can ispell POD sections and HERE-DOCs.
875 r) Understands comments and character classes inside regular
876 expressions; can find matching () and [] in a regular expression.
877 s) Allows indentation of //x-style regular expressions;
878 t) Highlights different symbols in regular expressions according
879 to their function; much less problems with backslashitis;
880 u) Allows to find regular expressions which contain interpolated parts.
882 5) The indentation engine was very smart, but most of tricks may be
883 not needed anymore with the support for `syntax-table' property. Has
884 progress indicator for indentation (with `imenu' loaded).
886 6) Indent-region improves inline-comments as well; also corrects
887 whitespace *inside* the conditional/loop constructs.
889 7) Fill-paragraph correctly handles multi-line comments;
891 8) Can switch to different indentation styles by one command, and restore
892 the settings present before the switch.
894 9) When doing indentation of control constructs, may correct
895 line-breaks/spacing between elements of the construct.
897 10) Uses a linear-time algorith for indentation of regions (on Emaxen with
898 capable syntax engines).
900 11) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
903 (defvar cperl-speed 'please-ignore-this-line
904 "This is an incomplete compendium of what is available in other parts
905 of CPerl documentation. (Please inform me if I skept anything.)
907 There is a perception that CPerl is slower than alternatives. This part
908 of documentation is designed to overcome this misconception.
910 *By default* CPerl tries to enable the most comfortable settings.
911 From most points of view, correctly working package is infinitely more
912 comfortable than a non-correctly working one, thus by default CPerl
913 prefers correctness over speed. Below is the guide how to change
914 settings if your preferences are different.
916 A) Speed of loading the file. When loading file, CPerl may perform a
917 scan which indicates places which cannot be parsed by primitive Emacs
918 syntax-parsing routines, and marks them up so that either
920 A1) CPerl may work around these deficiencies (for big chunks, mostly
921 PODs and HERE-documents), or
922 A2) On capable Emaxen CPerl will use improved syntax-handlings
923 which reads mark-up hints directly.
925 The scan in case A2 is much more comprehensive, thus may be slower.
927 User can disable syntax-engine-helping scan of A2 by setting
928 `cperl-use-syntax-table-text-property'
929 variable to nil (if it is set to t).
931 One can disable the scan altogether (both A1 and A2) by setting
932 `cperl-pod-here-scan'
933 to nil.
935 B) Speed of editing operations.
937 One can add a (minor) speedup to editing operations by setting
938 `cperl-use-syntax-table-text-property'
939 variable to nil (if it is set to t). This will disable
940 syntax-engine-helping scan, thus will make many more Perl
941 constructs be wrongly recognized by CPerl, thus may lead to
942 wrongly matched parentheses, wrong indentation, etc.
944 One can unset `cperl-syntaxify-unwind'. This might speed up editing
945 of, say, long POD sections.")
947 (defvar cperl-tips-faces 'please-ignore-this-line
948 "CPerl mode uses following faces for highlighting:
950 `cperl-array-face' Array names
951 `cperl-hash-face' Hash names
952 `font-lock-comment-face' Comments, PODs and whatever is considered
953 syntaxically to be not code
954 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
955 2-arg operators s/y/tr/ or of RExen,
956 `font-lock-warning-face' Special-cased m// and s//foo/,
957 `font-lock-function-name-face' _ as a target of a file tests, file tests,
958 subroutine names at the moment of definition
959 (except those conflicting with Perl operators),
960 package names (when recognized), format names
961 `font-lock-keyword-face' Control flow switch constructs, declarators
962 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
963 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
964 literal parts and the terminator of formats
965 and whatever is syntaxically considered
966 as string literals
967 `font-lock-type-face' Overridable keywords
968 `font-lock-variable-name-face' Variable declarations, indirect array and
969 hash names, POD headers/item names
970 `cperl-invalid' Trailing whitespace
972 Note that in several situations the highlighting tries to inform about
973 possible confusion, such as different colors for function names in
974 declarations depending on what they (do not) override, or special cases
975 m// and s/// which do not do what one would expect them to do.
977 Help with best setup of these faces for printout requested (for each of
978 the faces: please specify bold, italic, underline, shadow and box.)
980 In regular expressions (except character classes):
981 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
982 `font-lock-constant-face': Delimiters
983 `font-lock-warning-face' Special-cased m// and s//foo/,
984 Mismatched closing delimiters, parens
985 we couldn't match, misplaced quantifiers,
986 unrecognized escape sequences
987 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
988 `font-lock-type-face' POSIX classes inside charclasses,
989 escape sequences with arguments (\x \23 \p \N)
990 and others match-a-char escape sequences
991 `font-lock-keyword-face' Capturing parens, and |
992 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
993 `font-lock-builtin-face' \"Remaining\" 0-length constructs, executable
994 parts of a REx, not-capturing parens
995 `font-lock-variable-name-face' Interpolated constructs, embedded code
996 `font-lock-comment-face' Embedded comments
1002 ;;; Portability stuff:
1004 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
1005 `(define-key cperl-mode-map
1006 ,(if xemacs-key
1007 `(if cperl-xemacs-p ,xemacs-key ,emacs-key)
1008 emacs-key)
1009 ,definition))
1011 (defvar cperl-del-back-ch
1012 (car (append (where-is-internal 'delete-backward-char)
1013 (where-is-internal 'backward-delete-char-untabify)))
1014 "Character generated by key bound to `delete-backward-char'.")
1016 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
1017 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1019 (defun cperl-mark-active () (mark)) ; Avoid undefined warning
1020 (if cperl-xemacs-p
1021 (progn
1022 ;; "Active regions" are on: use region only if active
1023 ;; "Active regions" are off: use region unconditionally
1024 (defun cperl-use-region-p ()
1025 (if zmacs-regions (mark) t)))
1026 (defun cperl-use-region-p ()
1027 (if transient-mark-mode mark-active t))
1028 (defun cperl-mark-active () mark-active))
1030 (defsubst cperl-enable-font-lock ()
1031 cperl-can-font-lock)
1033 (defun cperl-putback-char (c) ; Emacs 19
1034 (set 'unread-command-events (list c))) ; Avoid undefined warning
1036 (if cperl-xemacs-p
1037 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1038 (setq unread-command-events (list (eval '(character-to-event c))))))
1040 (or (fboundp 'uncomment-region)
1041 (defun uncomment-region (beg end)
1042 (interactive "r")
1043 (comment-region beg end -1)))
1045 (defvar cperl-do-not-fontify
1046 (if (string< emacs-version "19.30")
1047 'fontified
1048 'lazy-lock)
1049 "Text property which inhibits refontification.")
1051 (defsubst cperl-put-do-not-fontify (from to &optional post)
1052 ;; If POST, do not do it with postponed fontification
1053 (if (and post cperl-syntaxify-by-font-lock)
1055 (put-text-property (max (point-min) (1- from))
1056 to cperl-do-not-fontify t)))
1058 (defcustom cperl-mode-hook nil
1059 "Hook run by CPerl mode."
1060 :type 'hook
1061 :group 'cperl)
1063 (defvar cperl-syntax-state nil)
1064 (defvar cperl-syntax-done-to nil)
1065 (defvar cperl-emacs-can-parse (> (length (save-excursion
1066 (parse-partial-sexp (point) (point)))) 9))
1068 ;; Make customization possible "in reverse"
1069 (defsubst cperl-val (symbol &optional default hairy)
1070 (cond
1071 ((eq (symbol-value symbol) 'null) default)
1072 (cperl-hairy (or hairy t))
1073 (t (symbol-value symbol))))
1076 (defun cperl-make-indent (column &optional minimum keep)
1077 "Makes indent of the current line the requested amount.
1078 Unless KEEP, removes the old indentation. Works around a bug in ancient
1079 versions of Emacs."
1080 (let ((prop (get-text-property (point) 'syntax-type)))
1081 (or keep
1082 (delete-horizontal-space))
1083 (indent-to column minimum)
1084 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1085 (and prop
1086 (> (current-column) 0)
1087 (save-excursion
1088 (beginning-of-line)
1089 (or (get-text-property (point) 'syntax-type)
1090 (and (looking-at "\\=[ \t]")
1091 (put-text-property (point) (match-end 0)
1092 'syntax-type prop)))))))
1094 ;;; Probably it is too late to set these guys already, but it can help later:
1096 ;;;(and cperl-clobber-mode-lists
1097 ;;;(setq auto-mode-alist
1098 ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1099 ;;;(and (boundp 'interpreter-mode-alist)
1100 ;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1101 ;;; '(("miniperl" . perl-mode))))))
1102 (eval-when-compile
1103 (mapcar (lambda (p)
1104 (condition-case nil
1105 (require p)
1106 (error nil)))
1107 '(imenu easymenu etags timer man info))
1108 (if (fboundp 'ps-extend-face-list)
1109 (defmacro cperl-ps-extend-face-list (arg)
1110 `(ps-extend-face-list ,arg))
1111 (defmacro cperl-ps-extend-face-list (arg)
1112 `(error "This version of Emacs has no `ps-extend-face-list'")))
1113 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1114 ;; macros instead of defsubsts don't work on Emacs, so we do the
1115 ;; expansion manually. Any other suggestions?
1116 (require 'cl))
1118 (defvar cperl-mode-abbrev-table nil
1119 "Abbrev table in use in CPerl mode buffers.")
1121 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1123 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
1125 (if cperl-mode-map nil
1126 (setq cperl-mode-map (make-sparse-keymap))
1127 (cperl-define-key "{" 'cperl-electric-lbrace)
1128 (cperl-define-key "[" 'cperl-electric-paren)
1129 (cperl-define-key "(" 'cperl-electric-paren)
1130 (cperl-define-key "<" 'cperl-electric-paren)
1131 (cperl-define-key "}" 'cperl-electric-brace)
1132 (cperl-define-key "]" 'cperl-electric-rparen)
1133 (cperl-define-key ")" 'cperl-electric-rparen)
1134 (cperl-define-key ";" 'cperl-electric-semi)
1135 (cperl-define-key ":" 'cperl-electric-terminator)
1136 (cperl-define-key "\C-j" 'newline-and-indent)
1137 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1138 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1139 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1140 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1141 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1142 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1143 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1144 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1145 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1146 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1147 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1148 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1149 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1150 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1151 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1152 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1153 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
1154 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1155 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1156 [(control meta |)])
1157 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1158 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1159 (cperl-define-key "\177" 'cperl-electric-backspace)
1160 (cperl-define-key "\t" 'cperl-indent-command)
1161 ;; don't clobber the backspace binding:
1162 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1163 [(control c) (control h) F])
1164 (if (cperl-val 'cperl-clobber-lisp-bindings)
1165 (progn
1166 (cperl-define-key "\C-hf"
1167 ;;(concat (char-to-string help-char) "f") ; does not work
1168 'cperl-info-on-command
1169 [(control h) f])
1170 (cperl-define-key "\C-hv"
1171 ;;(concat (char-to-string help-char) "v") ; does not work
1172 'cperl-get-help
1173 [(control h) v])
1174 (cperl-define-key "\C-c\C-hf"
1175 ;;(concat (char-to-string help-char) "f") ; does not work
1176 (key-binding "\C-hf")
1177 [(control c) (control h) f])
1178 (cperl-define-key "\C-c\C-hv"
1179 ;;(concat (char-to-string help-char) "v") ; does not work
1180 (key-binding "\C-hv")
1181 [(control c) (control h) v]))
1182 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1183 [(control c) (control h) f])
1184 (cperl-define-key "\C-c\C-hv"
1185 ;;(concat (char-to-string help-char) "v") ; does not work
1186 'cperl-get-help
1187 [(control c) (control h) v]))
1188 (if (and cperl-xemacs-p
1189 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1190 (progn
1191 ;; substitute-key-definition is usefulness-deenhanced...
1192 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1193 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1194 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1195 (or (boundp 'fill-paragraph-function)
1196 (substitute-key-definition
1197 'fill-paragraph 'cperl-fill-paragraph
1198 cperl-mode-map global-map))
1199 (substitute-key-definition
1200 'indent-sexp 'cperl-indent-exp
1201 cperl-mode-map global-map)
1202 (substitute-key-definition
1203 'indent-region 'cperl-indent-region
1204 cperl-mode-map global-map)
1205 (substitute-key-definition
1206 'indent-for-comment 'cperl-indent-for-comment
1207 cperl-mode-map global-map)))
1209 (defvar cperl-menu)
1210 (defvar cperl-lazy-installed)
1211 (defvar cperl-old-style nil)
1212 (condition-case nil
1213 (progn
1214 (require 'easymenu)
1215 (easy-menu-define
1216 cperl-menu cperl-mode-map "Menu for CPerl mode"
1217 '("Perl"
1218 ["Beginning of function" beginning-of-defun t]
1219 ["End of function" end-of-defun t]
1220 ["Mark function" mark-defun t]
1221 ["Indent expression" cperl-indent-exp t]
1222 ["Fill paragraph/comment" fill-paragraph t]
1223 "----"
1224 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1225 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1226 ("Regexp"
1227 ["Beautify" cperl-beautify-regexp
1228 cperl-use-syntax-table-text-property]
1229 ["Beautify one level deep" (cperl-beautify-regexp 1)
1230 cperl-use-syntax-table-text-property]
1231 ["Beautify a group" cperl-beautify-level
1232 cperl-use-syntax-table-text-property]
1233 ["Beautify a group one level deep" (cperl-beautify-level 1)
1234 cperl-use-syntax-table-text-property]
1235 ["Contract a group" cperl-contract-level
1236 cperl-use-syntax-table-text-property]
1237 ["Contract groups" cperl-contract-levels
1238 cperl-use-syntax-table-text-property]
1239 "----"
1240 ["Find next interpolated" cperl-next-interpolated-REx
1241 (next-single-property-change (point-min) 'REx-interpolated)]
1242 ["Find next interpolated (no //o)"
1243 cperl-next-interpolated-REx-0
1244 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1245 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1246 ["Find next interpolated (neither //o nor whole-REx)"
1247 cperl-next-interpolated-REx-1
1248 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1249 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1250 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1251 "----"
1252 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1253 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1254 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1255 "----"
1256 ["Run" mode-compile (fboundp 'mode-compile)]
1257 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1258 (get-buffer "*compilation*"))]
1259 ["Next error" next-error (get-buffer "*compilation*")]
1260 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1261 "----"
1262 ["Debugger" cperl-db t]
1263 "----"
1264 ("Tools"
1265 ["Imenu" imenu (fboundp 'imenu)]
1266 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1267 "----"
1268 ["Ispell PODs" cperl-pod-spell
1269 ;; Better not to update syntaxification here:
1270 ;; debugging syntaxificatio can be broken by this???
1272 (get-text-property (point-min) 'in-pod)
1273 (< (progn
1274 (and cperl-syntaxify-for-menu
1275 (cperl-update-syntaxification (point-max) (point-max)))
1276 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1277 (point-max)))]
1278 ["Ispell HERE-DOCs" cperl-here-doc-spell
1279 (< (progn
1280 (and cperl-syntaxify-for-menu
1281 (cperl-update-syntaxification (point-max) (point-max)))
1282 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1283 (point-max))]
1284 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1285 (eq 'here-doc (progn
1286 (and cperl-syntaxify-for-menu
1287 (cperl-update-syntaxification (point) (point)))
1288 (get-text-property (point) 'syntax-type)))]
1289 ["Select this HERE-DOC or POD section"
1290 cperl-select-this-pod-or-here-doc
1291 (memq (progn
1292 (and cperl-syntaxify-for-menu
1293 (cperl-update-syntaxification (point) (point)))
1294 (get-text-property (point) 'syntax-type))
1295 '(here-doc pod))]
1296 "----"
1297 ["CPerl pretty print (exprmntl)" cperl-ps-print
1298 (fboundp 'ps-extend-face-list)]
1299 "----"
1300 ["Syntaxify region" cperl-find-pods-heres-region
1301 (cperl-use-region-p)]
1302 ["Profile syntaxification" cperl-time-fontification t]
1303 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1304 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1305 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1306 (cperl-toggle-set-debug-unwind nil t) t]
1307 "----"
1308 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1309 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1310 ("Tags"
1311 ;;; ["Create tags for current file" cperl-etags t]
1312 ;;; ["Add tags for current file" (cperl-etags t) t]
1313 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1314 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1315 ;;; ["Create tags for Perl files in (sub)directories"
1316 ;;; (cperl-etags nil 'recursive) t]
1317 ;;; ["Add tags for Perl files in (sub)directories"
1318 ;;; (cperl-etags t 'recursive) t])
1319 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1320 ["Create tags for current file" (cperl-write-tags nil t) t]
1321 ["Add tags for current file" (cperl-write-tags) t]
1322 ["Create tags for Perl files in directory"
1323 (cperl-write-tags nil t nil t) t]
1324 ["Add tags for Perl files in directory"
1325 (cperl-write-tags nil nil nil t) t]
1326 ["Create tags for Perl files in (sub)directories"
1327 (cperl-write-tags nil t t t) t]
1328 ["Add tags for Perl files in (sub)directories"
1329 (cperl-write-tags nil nil t t) t]))
1330 ("Perl docs"
1331 ["Define word at point" imenu-go-find-at-position
1332 (fboundp 'imenu-go-find-at-position)]
1333 ["Help on function" cperl-info-on-command t]
1334 ["Help on function at point" cperl-info-on-current-command t]
1335 ["Help on symbol at point" cperl-get-help t]
1336 ["Perldoc" cperl-perldoc t]
1337 ["Perldoc on word at point" cperl-perldoc-at-point t]
1338 ["View manpage of POD in this file" cperl-build-manpage t]
1339 ["Auto-help on" cperl-lazy-install
1340 (and (fboundp 'run-with-idle-timer)
1341 (not cperl-lazy-installed))]
1342 ["Auto-help off" cperl-lazy-unstall
1343 (and (fboundp 'run-with-idle-timer)
1344 cperl-lazy-installed)])
1345 ("Toggle..."
1346 ["Auto newline" cperl-toggle-auto-newline t]
1347 ["Electric parens" cperl-toggle-electric t]
1348 ["Electric keywords" cperl-toggle-abbrev t]
1349 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1350 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1351 ["Auto fill" auto-fill-mode t])
1352 ("Indent styles..."
1353 ["CPerl" (cperl-set-style "CPerl") t]
1354 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1355 ["GNU" (cperl-set-style "GNU") t]
1356 ["C++" (cperl-set-style "C++") t]
1357 ["K&R" (cperl-set-style "K&R") t]
1358 ["BSD" (cperl-set-style "BSD") t]
1359 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1360 ["Memorize Current" (cperl-set-style "Current") t]
1361 ["Memorized" (cperl-set-style-back) cperl-old-style])
1362 ("Micro-docs"
1363 ["Tips" (describe-variable 'cperl-tips) t]
1364 ["Problems" (describe-variable 'cperl-problems) t]
1365 ["Speed" (describe-variable 'cperl-speed) t]
1366 ["Praise" (describe-variable 'cperl-praise) t]
1367 ["Faces" (describe-variable 'cperl-tips-faces) t]
1368 ["CPerl mode" (describe-function 'cperl-mode) t]
1369 ["CPerl version"
1370 (message "The version of master-file for this CPerl is %s-Emacs"
1371 cperl-version) t]))))
1372 (error nil))
1374 (autoload 'c-macro-expand "cmacexp"
1375 "Display the result of expanding all C macros occurring in the region.
1376 The expansion is entirely correct because it uses the C preprocessor."
1379 ;;; These two must be unwound, otherwise take exponential time
1380 (defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1381 "Regular expression to match optional whitespace with interpspersed comments.
1382 Should contain exactly one group.")
1384 ;;; This one is tricky to unwind; still very inefficient...
1385 (defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1386 "Regular expression to match whitespace with interpspersed comments.
1387 Should contain exactly one group.")
1390 ;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1391 ;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1392 ;;; Details of groups in this may be used in several functions; see comments
1393 ;;; near mentioned above variable(s)...
1394 ;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1395 (defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1396 "Match the text after `sub' in a subroutine declaration.
1397 If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1398 of attributes (if present), or end of the name or prototype (whatever is
1399 the last)."
1400 (concat ; Assume n groups before this...
1401 "\\(" ; n+1=name-group
1402 cperl-white-and-comment-rex ; n+2=pre-name
1403 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1404 "\\)" ; END n+1=name-group
1405 (if named "" "?")
1406 "\\(" ; n+4=proto-group
1407 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1408 "\\(([^()]*)\\)" ; n+6=prototype
1409 "\\)?" ; END n+4=proto-group
1410 "\\(" ; n+7=attr-group
1411 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1412 "\\(" ; n+9=start-attr
1414 (if attr (concat
1415 "\\("
1416 cperl-maybe-white-and-comment-rex ; whitespace-comments
1417 "\\(\\sw\\|_\\)+" ; attr-name
1418 ;; attr-arg (1 level of internal parens allowed!)
1419 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1420 "\\(" ; optional : (XXX allows trailing???)
1421 cperl-maybe-white-and-comment-rex ; whitespace-comments
1422 ":\\)?"
1423 "\\)+")
1424 "[^:]")
1425 "\\)"
1426 "\\)?" ; END n+6=proto-group
1429 ;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1430 ;;; and `cperl-outline-level'.
1431 ;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1432 (defvar cperl-imenu--function-name-regexp-perl
1433 (concat
1434 "^\\(" ; 1 = all
1435 "\\([ \t]*package" ; 2 = package-group
1436 "\\(" ; 3 = package-name-group
1437 cperl-white-and-comment-rex ; 4 = pre-package-name
1438 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1439 "\\|"
1440 "[ \t]*sub"
1441 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1442 cperl-maybe-white-and-comment-rex ; 15=pre-block
1443 "\\|"
1444 "=head\\([1-4]\\)[ \t]+" ; 16=level
1445 "\\([^\n]+\\)$" ; 17=text
1446 "\\)"))
1448 (defvar cperl-outline-regexp
1449 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1451 (defvar cperl-mode-syntax-table nil
1452 "Syntax table in use in CPerl mode buffers.")
1454 (defvar cperl-string-syntax-table nil
1455 "Syntax table in use in CPerl mode string-like chunks.")
1457 (defsubst cperl-1- (p)
1458 (max (point-min) (1- p)))
1460 (defsubst cperl-1+ (p)
1461 (min (point-max) (1+ p)))
1463 (if cperl-mode-syntax-table
1465 (setq cperl-mode-syntax-table (make-syntax-table))
1466 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1467 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1468 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1469 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1470 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1471 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1472 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1473 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1474 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1475 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1476 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1477 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1478 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1479 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1480 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1481 (if cperl-under-as-char
1482 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1483 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1484 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1485 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1486 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1487 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1488 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
1489 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
1493 (defvar cperl-faces-init nil)
1494 ;; Fix for msb.el
1495 (defvar cperl-msb-fixed nil)
1496 (defvar cperl-use-major-mode 'cperl-mode)
1497 (defvar cperl-font-lock-multiline-start nil)
1498 (defvar cperl-font-lock-multiline nil)
1499 (defvar cperl-compilation-error-regexp-alist nil)
1500 (defvar cperl-font-locking nil)
1502 ;;;###autoload
1503 (defun cperl-mode ()
1504 "Major mode for editing Perl code.
1505 Expression and list commands understand all C brackets.
1506 Tab indents for Perl code.
1507 Paragraphs are separated by blank lines only.
1508 Delete converts tabs to spaces as it moves back.
1510 Various characters in Perl almost always come in pairs: {}, (), [],
1511 sometimes <>. When the user types the first, she gets the second as
1512 well, with optional special formatting done on {}. (Disabled by
1513 default.) You can always quote (with \\[quoted-insert]) the left
1514 \"paren\" to avoid the expansion. The processing of < is special,
1515 since most the time you mean \"less\". CPerl mode tries to guess
1516 whether you want to type pair <>, and inserts is if it
1517 appropriate. You can set `cperl-electric-parens-string' to the string that
1518 contains the parenths from the above list you want to be electrical.
1519 Electricity of parenths is controlled by `cperl-electric-parens'.
1520 You may also set `cperl-electric-parens-mark' to have electric parens
1521 look for active mark and \"embrace\" a region if possible.'
1523 CPerl mode provides expansion of the Perl control constructs:
1525 if, else, elsif, unless, while, until, continue, do,
1526 for, foreach, formy and foreachmy.
1528 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1530 The user types the keyword immediately followed by a space, which
1531 causes the construct to be expanded, and the point is positioned where
1532 she is most likely to want to be. eg. when the user types a space
1533 following \"if\" the following appears in the buffer: if () { or if ()
1534 } { } and the cursor is between the parentheses. The user can then
1535 type some boolean expression within the parens. Having done that,
1536 typing \\[cperl-linefeed] places you - appropriately indented - on a
1537 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1538 directive line, then appropriate number of new lines is inserted).
1540 If CPerl decides that you want to insert \"English\" style construct like
1542 bite if angry;
1544 it will not do any expansion. See also help on variable
1545 `cperl-extra-newline-before-brace'. (Note that one can switch the
1546 help message on expansion by setting `cperl-message-electric-keyword'
1547 to nil.)
1549 \\[cperl-linefeed] is a convenience replacement for typing carriage
1550 return. It places you in the next line with proper indentation, or if
1551 you type it inside the inline block of control construct, like
1553 foreach (@lines) {print; print}
1555 and you are on a boundary of a statement inside braces, it will
1556 transform the construct into a multiline and will place you into an
1557 appropriately indented blank line. If you need a usual
1558 `newline-and-indent' behavior, it is on \\[newline-and-indent],
1559 see documentation on `cperl-electric-linefeed'.
1561 Use \\[cperl-invert-if-unless] to change a construction of the form
1563 if (A) { B }
1565 into
1567 B if A;
1569 \\{cperl-mode-map}
1571 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1572 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1573 on electric space between $ and {, `cperl-electric-parens-string' is
1574 the string that contains parentheses that should be electric in CPerl
1575 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1576 setting `cperl-electric-keywords' enables electric expansion of
1577 control structures in CPerl. `cperl-electric-linefeed' governs which
1578 one of two linefeed behavior is preferable. You can enable all these
1579 options simultaneously (recommended mode of use) by setting
1580 `cperl-hairy' to t. In this case you can switch separate options off
1581 by setting them to `null'. Note that one may undo the extra
1582 whitespace inserted by semis and braces in `auto-newline'-mode by
1583 consequent \\[cperl-electric-backspace].
1585 If your site has perl5 documentation in info format, you can use commands
1586 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1587 These keys run commands `cperl-info-on-current-command' and
1588 `cperl-info-on-command', which one is which is controlled by variable
1589 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1590 \(in turn affected by `cperl-hairy').
1592 Even if you have no info-format documentation, short one-liner-style
1593 help is available on \\[cperl-get-help], and one can run perldoc or
1594 man via menu.
1596 It is possible to show this help automatically after some idle time.
1597 This is regulated by variable `cperl-lazy-help-time'. Default with
1598 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1599 secs idle time . It is also possible to switch this on/off from the
1600 menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1602 Use \\[cperl-lineup] to vertically lineup some construction - put the
1603 beginning of the region at the start of construction, and make region
1604 span the needed amount of lines.
1606 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1607 `cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1608 here-docs sections. With capable Emaxen results of scan are used
1609 for indentation too, otherwise they are used for highlighting only.
1611 Variables controlling indentation style:
1612 `cperl-tab-always-indent'
1613 Non-nil means TAB in CPerl mode should always reindent the current line,
1614 regardless of where in the line point is when the TAB command is used.
1615 `cperl-indent-left-aligned-comments'
1616 Non-nil means that the comment starting in leftmost column should indent.
1617 `cperl-auto-newline'
1618 Non-nil means automatically newline before and after braces,
1619 and after colons and semicolons, inserted in Perl code. The following
1620 \\[cperl-electric-backspace] will remove the inserted whitespace.
1621 Insertion after colons requires both this variable and
1622 `cperl-auto-newline-after-colon' set.
1623 `cperl-auto-newline-after-colon'
1624 Non-nil means automatically newline even after colons.
1625 Subject to `cperl-auto-newline' setting.
1626 `cperl-indent-level'
1627 Indentation of Perl statements within surrounding block.
1628 The surrounding block's indentation is the indentation
1629 of the line on which the open-brace appears.
1630 `cperl-continued-statement-offset'
1631 Extra indentation given to a substatement, such as the
1632 then-clause of an if, or body of a while, or just a statement continuation.
1633 `cperl-continued-brace-offset'
1634 Extra indentation given to a brace that starts a substatement.
1635 This is in addition to `cperl-continued-statement-offset'.
1636 `cperl-brace-offset'
1637 Extra indentation for line if it starts with an open brace.
1638 `cperl-brace-imaginary-offset'
1639 An open brace following other text is treated as if it the line started
1640 this far to the right of the actual line indentation.
1641 `cperl-label-offset'
1642 Extra indentation for line that is a label.
1643 `cperl-min-label-indent'
1644 Minimal indentation for line that is a label.
1646 Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1647 `cperl-indent-level' 5 4 2 4
1648 `cperl-brace-offset' 0 0 0 0
1649 `cperl-continued-brace-offset' -5 -4 0 0
1650 `cperl-label-offset' -5 -4 -2 -4
1651 `cperl-continued-statement-offset' 5 4 2 4
1653 CPerl knows several indentation styles, and may bulk set the
1654 corresponding variables. Use \\[cperl-set-style] to do this. Use
1655 \\[cperl-set-style-back] to restore the memorized preexisting values
1656 \(both available from menu). See examples in `cperl-style-examples'.
1658 Part of the indentation style is how different parts of if/elsif/else
1659 statements are broken into lines; in CPerl, this is reflected on how
1660 templates for these constructs are created (controlled by
1661 `cperl-extra-newline-before-brace'), and how reflow-logic should treat \"continuation\" blocks of else/elsif/continue, controlled by the same variable,
1662 and by `cperl-extra-newline-before-brace-multiline',
1663 `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1665 If `cperl-indent-level' is 0, the statement after opening brace in
1666 column 0 is indented on
1667 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1669 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1670 with no args.
1672 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1673 or as help on variables `cperl-tips', `cperl-problems',
1674 `cperl-praise', `cperl-speed'."
1675 (interactive)
1676 (kill-all-local-variables)
1677 (use-local-map cperl-mode-map)
1678 (if (cperl-val 'cperl-electric-linefeed)
1679 (progn
1680 (local-set-key "\C-J" 'cperl-linefeed)
1681 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1682 (if (and
1683 (cperl-val 'cperl-clobber-lisp-bindings)
1684 (cperl-val 'cperl-info-on-command-no-prompt))
1685 (progn
1686 ;; don't clobber the backspace binding:
1687 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1688 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1689 [(control c) (control h) f])))
1690 (setq major-mode cperl-use-major-mode)
1691 (setq mode-name "CPerl")
1692 (let ((prev-a-c abbrevs-changed))
1693 (define-abbrev-table 'cperl-mode-abbrev-table '(
1694 ("if" "if" cperl-electric-keyword 0)
1695 ("elsif" "elsif" cperl-electric-keyword 0)
1696 ("while" "while" cperl-electric-keyword 0)
1697 ("until" "until" cperl-electric-keyword 0)
1698 ("unless" "unless" cperl-electric-keyword 0)
1699 ("else" "else" cperl-electric-else 0)
1700 ("continue" "continue" cperl-electric-else 0)
1701 ("for" "for" cperl-electric-keyword 0)
1702 ("foreach" "foreach" cperl-electric-keyword 0)
1703 ("formy" "formy" cperl-electric-keyword 0)
1704 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1705 ("do" "do" cperl-electric-keyword 0)
1706 ("=pod" "=pod" cperl-electric-pod 0)
1707 ("=over" "=over" cperl-electric-pod 0)
1708 ("=head1" "=head1" cperl-electric-pod 0)
1709 ("=head2" "=head2" cperl-electric-pod 0)
1710 ("pod" "pod" cperl-electric-pod 0)
1711 ("over" "over" cperl-electric-pod 0)
1712 ("head1" "head1" cperl-electric-pod 0)
1713 ("head2" "head2" cperl-electric-pod 0)))
1714 (setq abbrevs-changed prev-a-c))
1715 (setq local-abbrev-table cperl-mode-abbrev-table)
1716 (if (cperl-val 'cperl-electric-keywords)
1717 (abbrev-mode 1))
1718 (set-syntax-table cperl-mode-syntax-table)
1719 ;; Until Emacs is multi-threaded, we do not actually need it local:
1720 (make-local-variable 'cperl-font-lock-multiline-start)
1721 (make-local-variable 'cperl-font-locking)
1722 (make-local-variable 'outline-regexp)
1723 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1724 (setq outline-regexp cperl-outline-regexp)
1725 (make-local-variable 'outline-level)
1726 (setq outline-level 'cperl-outline-level)
1727 (make-local-variable 'paragraph-start)
1728 (setq paragraph-start (concat "^$\\|" page-delimiter))
1729 (make-local-variable 'paragraph-separate)
1730 (setq paragraph-separate paragraph-start)
1731 (make-local-variable 'paragraph-ignore-fill-prefix)
1732 (setq paragraph-ignore-fill-prefix t)
1733 (if cperl-xemacs-p
1734 (progn
1735 (make-local-variable 'paren-backwards-message)
1736 (set 'paren-backwards-message t)))
1737 (make-local-variable 'indent-line-function)
1738 (setq indent-line-function 'cperl-indent-line)
1739 (make-local-variable 'require-final-newline)
1740 (setq require-final-newline mode-require-final-newline)
1741 (make-local-variable 'comment-start)
1742 (setq comment-start "# ")
1743 (make-local-variable 'comment-end)
1744 (setq comment-end "")
1745 (make-local-variable 'comment-column)
1746 (setq comment-column cperl-comment-column)
1747 (make-local-variable 'comment-start-skip)
1748 (setq comment-start-skip "#+ *")
1749 (make-local-variable 'defun-prompt-regexp)
1750 ;;; "[ \t]*sub"
1751 ;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1752 ;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1753 (setq defun-prompt-regexp
1754 (concat "^[ \t]*\\(sub"
1755 (cperl-after-sub-regexp 'named 'attr-groups)
1756 "\\|" ; per toke.c
1757 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1758 "\\)"
1759 cperl-maybe-white-and-comment-rex))
1760 (make-local-variable 'comment-indent-function)
1761 (setq comment-indent-function 'cperl-comment-indent)
1762 (and (boundp 'fill-paragraph-function)
1763 (progn
1764 (make-local-variable 'fill-paragraph-function)
1765 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
1766 (make-local-variable 'parse-sexp-ignore-comments)
1767 (setq parse-sexp-ignore-comments t)
1768 (make-local-variable 'indent-region-function)
1769 (setq indent-region-function 'cperl-indent-region)
1770 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1771 (make-local-variable 'imenu-create-index-function)
1772 (setq imenu-create-index-function
1773 (function cperl-imenu--create-perl-index))
1774 (make-local-variable 'imenu-sort-function)
1775 (setq imenu-sort-function nil)
1776 (make-local-variable 'vc-rcs-header)
1777 (set 'vc-rcs-header cperl-vc-rcs-header)
1778 (make-local-variable 'vc-sccs-header)
1779 (set 'vc-sccs-header cperl-vc-sccs-header)
1780 ;; This one is obsolete...
1781 (make-local-variable 'vc-header-alist)
1782 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1783 (` ((SCCS (, (car cperl-vc-sccs-header)))
1784 (RCS (, (car cperl-vc-rcs-header)))))))
1785 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1786 (make-local-variable 'compilation-error-regexp-alist-alist)
1787 (set 'compilation-error-regexp-alist-alist
1788 (cons (cons 'cperl cperl-compilation-error-regexp-alist)
1789 (symbol-value 'compilation-error-regexp-alist-alist)))
1790 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1791 (let ((f 'compilation-build-compilation-error-regexp-alist))
1792 (funcall f))
1793 (make-local-variable 'compilation-error-regexp-alist)
1794 (push 'cperl compilation-error-regexp-alist)))
1795 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1796 (make-local-variable 'compilation-error-regexp-alist)
1797 (set 'compilation-error-regexp-alist
1798 (append cperl-compilation-error-regexp-alist
1799 (symbol-value 'compilation-error-regexp-alist)))))
1800 (make-local-variable 'font-lock-defaults)
1801 (setq font-lock-defaults
1802 (cond
1803 ((string< emacs-version "19.30")
1804 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1805 ((string< emacs-version "19.33") ; Which one to use?
1806 '((cperl-font-lock-keywords
1807 cperl-font-lock-keywords-1
1808 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1810 '((cperl-load-font-lock-keywords
1811 cperl-load-font-lock-keywords-1
1812 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1813 (make-local-variable 'cperl-syntax-state)
1814 (setq cperl-syntax-state nil) ; reset syntaxification cache
1815 (if cperl-use-syntax-table-text-property
1816 (progn
1817 (make-local-variable 'parse-sexp-lookup-properties)
1818 ;; Do not introduce variable if not needed, we check it!
1819 (set 'parse-sexp-lookup-properties t)
1820 ;; Fix broken font-lock:
1821 (or (boundp 'font-lock-unfontify-region-function)
1822 (set 'font-lock-unfontify-region-function
1823 'font-lock-default-unfontify-region))
1824 (unless cperl-xemacs-p ; Our: just a plug for wrong font-lock
1825 (make-local-variable 'font-lock-unfontify-region-function)
1826 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1827 'cperl-font-lock-unfontify-region-function))
1828 (make-local-variable 'cperl-syntax-done-to)
1829 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1830 (make-local-variable 'font-lock-syntactic-keywords)
1831 (setq font-lock-syntactic-keywords
1832 (if cperl-syntaxify-by-font-lock
1833 '((cperl-fontify-syntaxically))
1834 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1835 ;; used to ignore syntax-table text-properties. (t) is a hack
1836 ;; to make font-lock think that font-lock-syntactic-keywords
1837 ;; are defined.
1838 '(t)))))
1839 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1840 (progn
1841 (setq cperl-font-lock-multiline t) ; Not localized...
1842 (set (make-local-variable 'font-lock-multiline) t))
1843 (make-local-variable 'font-lock-fontify-region-function)
1844 (set 'font-lock-fontify-region-function ; not present with old Emacs
1845 'cperl-font-lock-fontify-region-function))
1846 (make-local-variable 'font-lock-fontify-region-function)
1847 (set 'font-lock-fontify-region-function ; not present with old Emacs
1848 'cperl-font-lock-fontify-region-function)
1849 (make-local-variable 'cperl-old-style)
1850 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1851 (set (make-local-variable 'normal-auto-fill-function)
1852 'cperl-do-auto-fill)
1853 (or (fboundp 'cperl-old-auto-fill-mode)
1854 (progn
1855 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1856 (defun auto-fill-mode (&optional arg)
1857 (interactive "P")
1858 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1859 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1860 (setq auto-fill-function 'cperl-do-auto-fill))))))
1861 (if (cperl-enable-font-lock)
1862 (if (cperl-val 'cperl-font-lock)
1863 (progn (or cperl-faces-init (cperl-init-faces))
1864 (font-lock-mode 1))))
1865 (set (make-local-variable 'facemenu-add-face-function)
1866 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1867 (and (boundp 'msb-menu-cond)
1868 (not cperl-msb-fixed)
1869 (cperl-msb-fix))
1870 (if (featurep 'easymenu)
1871 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1872 (run-mode-hooks 'cperl-mode-hook)
1873 (if cperl-hook-after-change
1874 (progn
1875 (make-local-hook 'after-change-functions)
1876 (add-hook 'after-change-functions 'cperl-after-change-function nil t)))
1877 ;; After hooks since fontification will break this
1878 (if cperl-pod-here-scan
1879 (or cperl-syntaxify-by-font-lock
1880 (progn (or cperl-faces-init (cperl-init-faces-weak))
1881 (cperl-find-pods-heres)))))
1883 ;; Fix for perldb - make default reasonable
1884 (defun cperl-db ()
1885 (interactive)
1886 (require 'gud)
1887 (perldb (read-from-minibuffer "Run perldb (like this): "
1888 (if (consp gud-perldb-history)
1889 (car gud-perldb-history)
1890 (concat "perl " ;;(file-name-nondirectory
1891 ;; I have problems
1892 ;; in OS/2
1893 ;; otherwise
1894 (buffer-file-name)))
1895 nil nil
1896 '(gud-perldb-history . 1))))
1898 (defun cperl-msb-fix ()
1899 ;; Adds perl files to msb menu, supposes that msb is already loaded
1900 (setq cperl-msb-fixed t)
1901 (let* ((l (length msb-menu-cond))
1902 (last (nth (1- l) msb-menu-cond))
1903 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1904 (handle (1- (nth 1 last))))
1905 (setcdr precdr (list
1906 (list
1907 '(memq major-mode '(cperl-mode perl-mode))
1908 handle
1909 "Perl Files (%d)")
1910 last))))
1912 ;; This is used by indent-for-comment
1913 ;; to decide how much to indent a comment in CPerl code
1914 ;; based on its context. Do fallback if comment is found wrong.
1916 (defvar cperl-wrong-comment)
1917 (defvar cperl-st-cfence '(14)) ; Comment-fence
1918 (defvar cperl-st-sfence '(15)) ; String-fence
1919 (defvar cperl-st-punct '(1))
1920 (defvar cperl-st-word '(2))
1921 (defvar cperl-st-bra '(4 . ?\>))
1922 (defvar cperl-st-ket '(5 . ?\<))
1925 (defun cperl-comment-indent () ; called at point at supposed comment
1926 (let ((p (point)) (c (current-column)) was phony)
1927 (if (and (not cperl-indent-comment-at-column-0)
1928 (looking-at "^#"))
1929 0 ; Existing comment at bol stays there.
1930 ;; Wrong comment found
1931 (save-excursion
1932 (setq was (cperl-to-comment-or-eol)
1933 phony (eq (get-text-property (point) 'syntax-table)
1934 cperl-st-cfence))
1935 (if phony
1936 (progn ; Too naive???
1937 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1938 (if (eq (preceding-char) ?\#)
1939 (forward-char -1))
1940 (setq was nil)))
1941 (if (= (point) p) ; Our caller found a correct place
1942 (progn
1943 (skip-chars-backward " \t")
1944 (setq was (current-column))
1945 (if (eq was 0)
1946 comment-column
1947 (max (1+ was) ; Else indent at comment column
1948 comment-column)))
1949 ;; No, the caller found a random place; we need to edit ourselves
1950 (if was nil
1951 (insert comment-start)
1952 (backward-char (length comment-start)))
1953 (setq cperl-wrong-comment t)
1954 (cperl-make-indent comment-column 1) ; Indent min 1
1955 c)))))
1957 ;;;(defun cperl-comment-indent-fallback ()
1958 ;;; "Is called if the standard comment-search procedure fails.
1959 ;;;Point is at start of real comment."
1960 ;;; (let ((c (current-column)) target cnt prevc)
1961 ;;; (if (= c comment-column) nil
1962 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1963 ;;; (setq target (max (1+ (setq prevc
1964 ;;; (current-column))) ; Else indent at comment column
1965 ;;; comment-column))
1966 ;;; (if (= c comment-column) nil
1967 ;;; (delete-backward-char cnt)
1968 ;;; (while (< prevc target)
1969 ;;; (insert "\t")
1970 ;;; (setq prevc (current-column)))
1971 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1972 ;;; (while (< prevc target)
1973 ;;; (insert " ")
1974 ;;; (setq prevc (current-column)))))))
1976 (defun cperl-indent-for-comment ()
1977 "Substitute for `indent-for-comment' in CPerl."
1978 (interactive)
1979 (let (cperl-wrong-comment)
1980 (indent-for-comment)
1981 (if cperl-wrong-comment ; set by `cperl-comment-indent'
1982 (progn (cperl-to-comment-or-eol)
1983 (forward-char (length comment-start))))))
1985 (defun cperl-comment-region (b e arg)
1986 "Comment or uncomment each line in the region in CPerl mode.
1987 See `comment-region'."
1988 (interactive "r\np")
1989 (let ((comment-start "#"))
1990 (comment-region b e arg)))
1992 (defun cperl-uncomment-region (b e arg)
1993 "Uncomment or comment each line in the region in CPerl mode.
1994 See `comment-region'."
1995 (interactive "r\np")
1996 (let ((comment-start "#"))
1997 (comment-region b e (- arg))))
1999 (defvar cperl-brace-recursing nil)
2001 (defun cperl-electric-brace (arg &optional only-before)
2002 "Insert character and correct line's indentation.
2003 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2004 place (even in empty line), but not after. If after \")\" and the inserted
2005 char is \"{\", insert extra newline before only if
2006 `cperl-extra-newline-before-brace'."
2007 (interactive "P")
2008 (let (insertpos
2009 (other-end (if (and cperl-electric-parens-mark
2010 (cperl-mark-active)
2011 (< (mark) (point)))
2012 (mark)
2013 nil)))
2014 (if (and other-end
2015 (not cperl-brace-recursing)
2016 (cperl-val 'cperl-electric-parens)
2017 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2018 ;; Need to insert a matching pair
2019 (progn
2020 (save-excursion
2021 (setq insertpos (point-marker))
2022 (goto-char other-end)
2023 (setq last-command-char ?\{)
2024 (cperl-electric-lbrace arg insertpos))
2025 (forward-char 1))
2026 ;; Check whether we close something "usual" with `}'
2027 (if (and (eq last-command-char ?\})
2028 (not
2029 (condition-case nil
2030 (save-excursion
2031 (up-list (- (prefix-numeric-value arg)))
2032 ;;(cperl-after-block-p (point-min))
2033 (or (cperl-after-expr-p nil "{;)")
2034 ;; after sub, else, continue
2035 (cperl-after-block-p nil 'pre)))
2036 (error nil))))
2037 ;; Just insert the guy
2038 (self-insert-command (prefix-numeric-value arg))
2039 (if (and (not arg) ; No args, end (of empty line or auto)
2040 (eolp)
2041 (or (and (null only-before)
2042 (save-excursion
2043 (skip-chars-backward " \t")
2044 (bolp)))
2045 (and (eq last-command-char ?\{) ; Do not insert newline
2046 ;; if after ")" and `cperl-extra-newline-before-brace'
2047 ;; is nil, do not insert extra newline.
2048 (not cperl-extra-newline-before-brace)
2049 (save-excursion
2050 (skip-chars-backward " \t")
2051 (eq (preceding-char) ?\))))
2052 (if cperl-auto-newline
2053 (progn (cperl-indent-line) (newline) t) nil)))
2054 (progn
2055 (self-insert-command (prefix-numeric-value arg))
2056 (cperl-indent-line)
2057 (if cperl-auto-newline
2058 (setq insertpos (1- (point))))
2059 (if (and cperl-auto-newline (null only-before))
2060 (progn
2061 (newline)
2062 (cperl-indent-line)))
2063 (save-excursion
2064 (if insertpos (progn (goto-char insertpos)
2065 (search-forward (make-string
2066 1 last-command-char))
2067 (setq insertpos (1- (point)))))
2068 (delete-char -1))))
2069 (if insertpos
2070 (save-excursion
2071 (goto-char insertpos)
2072 (self-insert-command (prefix-numeric-value arg)))
2073 (self-insert-command (prefix-numeric-value arg)))))))
2075 (defun cperl-electric-lbrace (arg &optional end)
2076 "Insert character, correct line's indentation, correct quoting by space."
2077 (interactive "P")
2078 (let ((cperl-brace-recursing t)
2079 (cperl-auto-newline cperl-auto-newline)
2080 (other-end (or end
2081 (if (and cperl-electric-parens-mark
2082 (cperl-mark-active)
2083 (> (mark) (point)))
2084 (save-excursion
2085 (goto-char (mark))
2086 (point-marker))
2087 nil)))
2088 pos after)
2089 (and (cperl-val 'cperl-electric-lbrace-space)
2090 (eq (preceding-char) ?$)
2091 (save-excursion
2092 (skip-chars-backward "$")
2093 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2094 (insert ?\s))
2095 ;; Check whether we are in comment
2096 (if (and
2097 (save-excursion
2098 (beginning-of-line)
2099 (not (looking-at "[ \t]*#")))
2100 (cperl-after-expr-p nil "{;)"))
2102 (setq cperl-auto-newline nil))
2103 (cperl-electric-brace arg)
2104 (and (cperl-val 'cperl-electric-parens)
2105 (eq last-command-char ?{)
2106 (memq last-command-char
2107 (append cperl-electric-parens-string nil))
2108 (or (if other-end (goto-char (marker-position other-end)))
2110 (setq last-command-char ?} pos (point))
2111 (progn (cperl-electric-brace arg t)
2112 (goto-char pos)))))
2114 (defun cperl-electric-paren (arg)
2115 "Insert an opening parenthesis or a matching pair of parentheses.
2116 See `cperl-electric-parens'."
2117 (interactive "P")
2118 (let ((beg (save-excursion (beginning-of-line) (point)))
2119 (other-end (if (and cperl-electric-parens-mark
2120 (cperl-mark-active)
2121 (> (mark) (point)))
2122 (save-excursion
2123 (goto-char (mark))
2124 (point-marker))
2125 nil)))
2126 (if (and (cperl-val 'cperl-electric-parens)
2127 (memq last-command-char
2128 (append cperl-electric-parens-string nil))
2129 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2130 ;;(not (save-excursion (search-backward "#" beg t)))
2131 (if (eq last-command-char ?<)
2132 (progn
2133 (and abbrev-mode ; later it is too late, may be after `for'
2134 (expand-abbrev))
2135 (cperl-after-expr-p nil "{;(,:="))
2137 (progn
2138 (self-insert-command (prefix-numeric-value arg))
2139 (if other-end (goto-char (marker-position other-end)))
2140 (insert (make-string
2141 (prefix-numeric-value arg)
2142 (cdr (assoc last-command-char '((?{ .?})
2143 (?[ . ?])
2144 (?( . ?))
2145 (?< . ?>))))))
2146 (forward-char (- (prefix-numeric-value arg))))
2147 (self-insert-command (prefix-numeric-value arg)))))
2149 (defun cperl-electric-rparen (arg)
2150 "Insert a matching pair of parentheses if marking is active.
2151 If not, or if we are not at the end of marking range, would self-insert.
2152 Affected by `cperl-electric-parens'."
2153 (interactive "P")
2154 (let ((beg (save-excursion (beginning-of-line) (point)))
2155 (other-end (if (and cperl-electric-parens-mark
2156 (cperl-val 'cperl-electric-parens)
2157 (memq last-command-char
2158 (append cperl-electric-parens-string nil))
2159 (cperl-mark-active)
2160 (< (mark) (point)))
2161 (mark)
2162 nil))
2164 (if (and other-end
2165 (cperl-val 'cperl-electric-parens)
2166 (memq last-command-char '( ?\) ?\] ?\} ?\> ))
2167 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2168 ;;(not (save-excursion (search-backward "#" beg t)))
2170 (progn
2171 (self-insert-command (prefix-numeric-value arg))
2172 (setq p (point))
2173 (if other-end (goto-char other-end))
2174 (insert (make-string
2175 (prefix-numeric-value arg)
2176 (cdr (assoc last-command-char '((?\} . ?\{)
2177 (?\] . ?\[)
2178 (?\) . ?\()
2179 (?\> . ?\<))))))
2180 (goto-char (1+ p)))
2181 (self-insert-command (prefix-numeric-value arg)))))
2183 (defun cperl-electric-keyword ()
2184 "Insert a construction appropriate after a keyword.
2185 Help message may be switched off by setting `cperl-message-electric-keyword'
2186 to nil."
2187 (let ((beg (save-excursion (beginning-of-line) (point)))
2188 (dollar (and (eq last-command-char ?$)
2189 (eq this-command 'self-insert-command)))
2190 (delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
2191 (memq this-command '(self-insert-command newline))))
2192 my do)
2193 (and (save-excursion
2194 (condition-case nil
2195 (progn
2196 (backward-sexp 1)
2197 (setq do (looking-at "do\\>")))
2198 (error nil))
2199 (cperl-after-expr-p nil "{;:"))
2200 (save-excursion
2201 (not
2202 (re-search-backward
2203 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2204 beg t)))
2205 (save-excursion (or (not (re-search-backward "^=" nil t))
2207 (looking-at "=cut")
2208 (and cperl-use-syntax-table-text-property
2209 (not (eq (get-text-property (point)
2210 'syntax-type)
2211 'pod))))))
2212 (save-excursion (forward-sexp -1)
2213 (not (memq (following-char) (append "$@%&*" nil))))
2214 (progn
2215 (and (eq (preceding-char) ?y)
2216 (progn ; "foreachmy"
2217 (forward-char -2)
2218 (insert " ")
2219 (forward-char 2)
2220 (setq my t dollar t
2221 delete
2222 (memq this-command '(self-insert-command newline)))))
2223 (and dollar (insert " $"))
2224 (cperl-indent-line)
2225 ;;(insert " () {\n}")
2226 (cond
2227 (cperl-extra-newline-before-brace
2228 (insert (if do "\n" " ()\n"))
2229 (insert "{")
2230 (cperl-indent-line)
2231 (insert "\n")
2232 (cperl-indent-line)
2233 (insert "\n}")
2234 (and do (insert " while ();")))
2236 (insert (if do " {\n} while ();" " () {\n}"))))
2237 (or (looking-at "[ \t]\\|$") (insert " "))
2238 (cperl-indent-line)
2239 (if dollar (progn (search-backward "$")
2240 (if my
2241 (forward-char 1)
2242 (delete-char 1)))
2243 (search-backward ")")
2244 (if (eq last-command-char ?\()
2245 (progn ; Avoid "if (())"
2246 (delete-backward-char 1)
2247 (delete-backward-char -1))))
2248 (if delete
2249 (cperl-putback-char cperl-del-back-ch))
2250 (if cperl-message-electric-keyword
2251 (message "Precede char by C-q to avoid expansion"))))))
2253 (defun cperl-ensure-newlines (n &optional pos)
2254 "Make sure there are N newlines after the point."
2255 (or pos (setq pos (point)))
2256 (if (looking-at "\n")
2257 (forward-char 1)
2258 (insert "\n"))
2259 (if (> n 1)
2260 (cperl-ensure-newlines (1- n) pos)
2261 (goto-char pos)))
2263 (defun cperl-electric-pod ()
2264 "Insert a POD chunk appropriate after a =POD directive."
2265 (let ((delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
2266 (memq this-command '(self-insert-command newline))))
2267 head1 notlast name p really-delete over)
2268 (and (save-excursion
2269 (forward-word -1)
2270 (and
2271 (eq (preceding-char) ?=)
2272 (progn
2273 (setq head1 (looking-at "head1\\>[ \t]*$"))
2274 (setq over (and (looking-at "over\\>[ \t]*$")
2275 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2276 (forward-char -1)
2277 (bolp))
2279 (get-text-property (point) 'in-pod)
2280 (cperl-after-expr-p nil "{;:")
2281 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2282 (not (looking-at "\n*=cut"))
2283 (or (not cperl-use-syntax-table-text-property)
2284 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2285 (progn
2286 (save-excursion
2287 (setq notlast (re-search-forward "^\n=" nil t)))
2288 (or notlast
2289 (progn
2290 (insert "\n\n=cut")
2291 (cperl-ensure-newlines 2)
2292 (forward-word -2)
2293 (if (and head1
2294 (not
2295 (save-excursion
2296 (forward-char -1)
2297 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2298 nil t)))) ; Only one
2299 (progn
2300 (forward-word 1)
2301 (setq name (file-name-sans-extension
2302 (file-name-nondirectory (buffer-file-name)))
2303 p (point))
2304 (insert " NAME\n\n" name
2305 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2306 "=head1 DESCRIPTION")
2307 (cperl-ensure-newlines 4)
2308 (goto-char p)
2309 (forward-word 2)
2310 (end-of-line)
2311 (setq really-delete t))
2312 (forward-word 1))))
2313 (if over
2314 (progn
2315 (setq p (point))
2316 (insert "\n\n=item \n\n\n\n"
2317 "=back")
2318 (cperl-ensure-newlines 2)
2319 (goto-char p)
2320 (forward-word 1)
2321 (end-of-line)
2322 (setq really-delete t)))
2323 (if (and delete really-delete)
2324 (cperl-putback-char cperl-del-back-ch))))))
2326 (defun cperl-electric-else ()
2327 "Insert a construction appropriate after a keyword.
2328 Help message may be switched off by setting `cperl-message-electric-keyword'
2329 to nil."
2330 (let ((beg (save-excursion (beginning-of-line) (point))))
2331 (and (save-excursion
2332 (backward-sexp 1)
2333 (cperl-after-expr-p nil "{;:"))
2334 (save-excursion
2335 (not
2336 (re-search-backward
2337 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2338 beg t)))
2339 (save-excursion (or (not (re-search-backward "^=" nil t))
2340 (looking-at "=cut")
2341 (and cperl-use-syntax-table-text-property
2342 (not (eq (get-text-property (point)
2343 'syntax-type)
2344 'pod)))))
2345 (progn
2346 (cperl-indent-line)
2347 ;;(insert " {\n\n}")
2348 (cond
2349 (cperl-extra-newline-before-brace
2350 (insert "\n")
2351 (insert "{")
2352 (cperl-indent-line)
2353 (insert "\n\n}"))
2355 (insert " {\n\n}")))
2356 (or (looking-at "[ \t]\\|$") (insert " "))
2357 (cperl-indent-line)
2358 (forward-line -1)
2359 (cperl-indent-line)
2360 (cperl-putback-char cperl-del-back-ch)
2361 (setq this-command 'cperl-electric-else)
2362 (if cperl-message-electric-keyword
2363 (message "Precede char by C-q to avoid expansion"))))))
2365 (defun cperl-linefeed ()
2366 "Go to end of line, open a new line and indent appropriately.
2367 If in POD, insert appropriate lines."
2368 (interactive)
2369 (let ((beg (save-excursion (beginning-of-line) (point)))
2370 (end (save-excursion (end-of-line) (point)))
2371 (pos (point)) start over cut res)
2372 (if (and ; Check if we need to split:
2373 ; i.e., on a boundary and inside "{...}"
2374 (save-excursion (cperl-to-comment-or-eol)
2375 (>= (point) pos)) ; Not in a comment
2376 (or (save-excursion
2377 (skip-chars-backward " \t" beg)
2378 (forward-char -1)
2379 (looking-at "[;{]")) ; After { or ; + spaces
2380 (looking-at "[ \t]*}") ; Before }
2381 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2382 (save-excursion
2383 (and
2384 (eq (car (parse-partial-sexp pos end -1)) -1)
2385 ; Leave the level of parens
2386 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2387 ; Are at end
2388 (cperl-after-block-p (point-min))
2389 (progn
2390 (backward-sexp 1)
2391 (setq start (point-marker))
2392 (<= start pos))))) ; Redundant? Are after the
2393 ; start of parens group.
2394 (progn
2395 (skip-chars-backward " \t")
2396 (or (memq (preceding-char) (append ";{" nil))
2397 (insert ";"))
2398 (insert "\n")
2399 (forward-line -1)
2400 (cperl-indent-line)
2401 (goto-char start)
2402 (or (looking-at "{[ \t]*$") ; If there is a statement
2403 ; before, move it to separate line
2404 (progn
2405 (forward-char 1)
2406 (insert "\n")
2407 (cperl-indent-line)))
2408 (forward-line 1) ; We are on the target line
2409 (cperl-indent-line)
2410 (beginning-of-line)
2411 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2412 ; after, move it to separate line
2413 (progn
2414 (end-of-line)
2415 (search-backward "}" beg)
2416 (skip-chars-backward " \t")
2417 (or (memq (preceding-char) (append ";{" nil))
2418 (insert ";"))
2419 (insert "\n")
2420 (cperl-indent-line)
2421 (forward-line -1)))
2422 (forward-line -1) ; We are on the line before target
2423 (end-of-line)
2424 (newline-and-indent))
2425 (end-of-line) ; else - no splitting
2426 (cond
2427 ((and (looking-at "\n[ \t]*{$")
2428 (save-excursion
2429 (skip-chars-backward " \t")
2430 (eq (preceding-char) ?\)))) ; Probably if () {} group
2431 ; with an extra newline.
2432 (forward-line 2)
2433 (cperl-indent-line))
2434 ((save-excursion ; In POD header
2435 (forward-paragraph -1)
2436 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2437 ;; We are after \n now, so look for the rest
2438 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2439 (progn
2440 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2441 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2442 t)))
2443 (if (and over
2444 (progn
2445 (forward-paragraph -1)
2446 (forward-word 1)
2447 (setq pos (point))
2448 (setq cut (buffer-substring (point)
2449 (save-excursion
2450 (end-of-line)
2451 (point))))
2452 (delete-char (- (save-excursion (end-of-line) (point))
2453 (point)))
2454 (setq res (expand-abbrev))
2455 (save-excursion
2456 (goto-char pos)
2457 (insert cut))
2458 res))
2460 (cperl-ensure-newlines (if cut 2 4))
2461 (forward-line 2)))
2462 ((get-text-property (point) 'in-pod) ; In POD section
2463 (cperl-ensure-newlines 4)
2464 (forward-line 2))
2465 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2466 (forward-line 1)
2467 (cperl-indent-line))
2469 (newline-and-indent))))))
2471 (defun cperl-electric-semi (arg)
2472 "Insert character and correct line's indentation."
2473 (interactive "P")
2474 (if cperl-auto-newline
2475 (cperl-electric-terminator arg)
2476 (self-insert-command (prefix-numeric-value arg))
2477 (if cperl-autoindent-on-semi
2478 (cperl-indent-line))))
2480 (defun cperl-electric-terminator (arg)
2481 "Insert character and correct line's indentation."
2482 (interactive "P")
2483 (let ((end (point))
2484 (auto (and cperl-auto-newline
2485 (or (not (eq last-command-char ?:))
2486 cperl-auto-newline-after-colon)))
2487 insertpos)
2488 (if (and ;;(not arg)
2489 (eolp)
2490 (not (save-excursion
2491 (beginning-of-line)
2492 (skip-chars-forward " \t")
2494 ;; Ignore in comment lines
2495 (= (following-char) ?#)
2496 ;; Colon is special only after a label
2497 ;; So quickly rule out most other uses of colon
2498 ;; and do no indentation for them.
2499 (and (eq last-command-char ?:)
2500 (save-excursion
2501 (forward-word 1)
2502 (skip-chars-forward " \t")
2503 (and (< (point) end)
2504 (progn (goto-char (- end 1))
2505 (not (looking-at ":"))))))
2506 (progn
2507 (beginning-of-defun)
2508 (let ((pps (parse-partial-sexp (point) end)))
2509 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2510 (progn
2511 (self-insert-command (prefix-numeric-value arg))
2512 ;;(forward-char -1)
2513 (if auto (setq insertpos (point-marker)))
2514 ;;(forward-char 1)
2515 (cperl-indent-line)
2516 (if auto
2517 (progn
2518 (newline)
2519 (cperl-indent-line)))
2520 (save-excursion
2521 (if insertpos (goto-char (1- (marker-position insertpos)))
2522 (forward-char -1))
2523 (delete-char 1))))
2524 (if insertpos
2525 (save-excursion
2526 (goto-char insertpos)
2527 (self-insert-command (prefix-numeric-value arg)))
2528 (self-insert-command (prefix-numeric-value arg)))))
2530 (defun cperl-electric-backspace (arg)
2531 "Backspace, or remove the whitespace around the point inserted by an electric
2532 key. Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2533 (interactive "p")
2534 (if (and cperl-auto-newline
2535 (memq last-command '(cperl-electric-semi
2536 cperl-electric-terminator
2537 cperl-electric-lbrace))
2538 (memq (preceding-char) '(?\s ?\t ?\n)))
2539 (let (p)
2540 (if (eq last-command 'cperl-electric-lbrace)
2541 (skip-chars-forward " \t\n"))
2542 (setq p (point))
2543 (skip-chars-backward " \t\n")
2544 (delete-region (point) p))
2545 (and (eq last-command 'cperl-electric-else)
2546 ;; We are removing the whitespace *inside* cperl-electric-else
2547 (setq this-command 'cperl-electric-else-really))
2548 (if (and cperl-auto-newline
2549 (eq last-command 'cperl-electric-else-really)
2550 (memq (preceding-char) '(?\s ?\t ?\n)))
2551 (let (p)
2552 (skip-chars-forward " \t\n")
2553 (setq p (point))
2554 (skip-chars-backward " \t\n")
2555 (delete-region (point) p))
2556 (if cperl-electric-backspace-untabify
2557 (backward-delete-char-untabify arg)
2558 (delete-backward-char arg)))))
2560 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2562 (defun cperl-inside-parens-p () ;; NOT USED????
2563 (condition-case ()
2564 (save-excursion
2565 (save-restriction
2566 (narrow-to-region (point)
2567 (progn (beginning-of-defun) (point)))
2568 (goto-char (point-max))
2569 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2570 (error nil)))
2572 (defun cperl-indent-command (&optional whole-exp)
2573 "Indent current line as Perl code, or in some cases insert a tab character.
2574 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2575 line. Otherwise, indent the current line only if point is at the left margin
2576 or in the line's indentation; otherwise insert a tab.
2578 A numeric argument, regardless of its value,
2579 means indent rigidly all the lines of the expression starting after point
2580 so that this line becomes properly indented.
2581 The relative indentation among the lines of the expression are preserved."
2582 (interactive "P")
2583 (cperl-update-syntaxification (point) (point))
2584 (if whole-exp
2585 ;; If arg, always indent this line as Perl
2586 ;; and shift remaining lines of expression the same amount.
2587 (let ((shift-amt (cperl-indent-line))
2588 beg end)
2589 (save-excursion
2590 (if cperl-tab-always-indent
2591 (beginning-of-line))
2592 (setq beg (point))
2593 (forward-sexp 1)
2594 (setq end (point))
2595 (goto-char beg)
2596 (forward-line 1)
2597 (setq beg (point)))
2598 (if (and shift-amt (> end beg))
2599 (indent-code-rigidly beg end shift-amt "#")))
2600 (if (and (not cperl-tab-always-indent)
2601 (save-excursion
2602 (skip-chars-backward " \t")
2603 (not (bolp))))
2604 (insert-tab)
2605 (cperl-indent-line))))
2607 (defun cperl-indent-line (&optional parse-data)
2608 "Indent current line as Perl code.
2609 Return the amount the indentation changed by."
2610 (let ((case-fold-search nil)
2611 (pos (- (point-max) (point)))
2612 indent i beg shift-amt)
2613 (setq indent (cperl-calculate-indent parse-data)
2614 i indent)
2615 (beginning-of-line)
2616 (setq beg (point))
2617 (cond ((or (eq indent nil) (eq indent t))
2618 (setq indent (current-indentation) i nil))
2619 ;;((eq indent t) ; Never?
2620 ;; (setq indent (cperl-calculate-indent-within-comment)))
2621 ;;((looking-at "[ \t]*#")
2622 ;; (setq indent 0))
2624 (skip-chars-forward " \t")
2625 (if (listp indent) (setq indent (car indent)))
2626 (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2627 (and (> indent 0)
2628 (setq indent (max cperl-min-label-indent
2629 (+ indent cperl-label-offset)))))
2630 ((= (following-char) ?})
2631 (setq indent (- indent cperl-indent-level)))
2632 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2633 (setq indent (+ indent cperl-close-paren-offset)))
2634 ((= (following-char) ?{)
2635 (setq indent (+ indent cperl-brace-offset))))))
2636 (skip-chars-forward " \t")
2637 (setq shift-amt (and i (- indent (current-column))))
2638 (if (or (not shift-amt)
2639 (zerop shift-amt))
2640 (if (> (- (point-max) pos) (point))
2641 (goto-char (- (point-max) pos)))
2642 ;;;(delete-region beg (point))
2643 ;;;(indent-to indent)
2644 (cperl-make-indent indent)
2645 ;; If initial point was within line's indentation,
2646 ;; position after the indentation. Else stay at same point in text.
2647 (if (> (- (point-max) pos) (point))
2648 (goto-char (- (point-max) pos))))
2649 shift-amt))
2651 (defun cperl-after-label ()
2652 ;; Returns true if the point is after label. Does not do save-excursion.
2653 (and (eq (preceding-char) ?:)
2654 (memq (char-syntax (char-after (- (point) 2)))
2655 '(?w ?_))
2656 (progn
2657 (backward-sexp)
2658 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2660 (defun cperl-get-state (&optional parse-start start-state)
2661 ;; returns list (START STATE DEPTH PRESTART),
2662 ;; START is a good place to start parsing, or equal to
2663 ;; PARSE-START if preset,
2664 ;; STATE is what is returned by `parse-partial-sexp'.
2665 ;; DEPTH is true is we are immediately after end of block
2666 ;; which contains START.
2667 ;; PRESTART is the position basing on which START was found.
2668 (save-excursion
2669 (let ((start-point (point)) depth state start prestart)
2670 (if (and parse-start
2671 (<= parse-start start-point))
2672 (goto-char parse-start)
2673 (beginning-of-defun)
2674 (setq start-state nil))
2675 (setq prestart (point))
2676 (if start-state nil
2677 ;; Try to go out, if sub is not on the outermost level
2678 (while (< (point) start-point)
2679 (setq start (point) parse-start start depth nil
2680 state (parse-partial-sexp start start-point -1))
2681 (if (> (car state) -1) nil
2682 ;; The current line could start like }}}, so the indentation
2683 ;; corresponds to a different level than what we reached
2684 (setq depth t)
2685 (beginning-of-line 2))) ; Go to the next line.
2686 (if start (goto-char start))) ; Not at the start of file
2687 (setq start (point))
2688 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2689 (list start state depth prestart))))
2691 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2693 (defun cperl-beginning-of-property (p prop &optional lim)
2694 "Given that P has a property PROP, find where the property starts.
2695 Will not look before LIM."
2696 ;;; XXXX What to do at point-max???
2697 (or (previous-single-property-change (cperl-1+ p) prop lim)
2698 (point-min))
2699 ;;; (cond ((eq p (point-min))
2700 ;;; p)
2701 ;;; ((and lim (<= p lim))
2702 ;;; p)
2703 ;;; ((not (get-text-property (1- p) prop))
2704 ;;; p)
2705 ;;; (t (or (previous-single-property-change p look-prop lim)
2706 ;;; (point-min))))
2709 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2710 ;; Old workhorse for calculation of indentation; the major problem
2711 ;; is that it mixes the sniffer logic to understand what the current line
2712 ;; MEANS with the logic to actually calculate where to indent it.
2713 ;; The latter part should be eventually moved to `cperl-calculate-indent';
2714 ;; actually, this is mostly done now...
2715 (cperl-update-syntaxification (point) (point))
2716 (let ((res (get-text-property (point) 'syntax-type)))
2717 (save-excursion
2718 (cond
2719 ((and (memq res '(pod here-doc here-doc-delim format))
2720 (not (get-text-property (point) 'indentable)))
2721 (vector res))
2722 ;; before start of POD - whitespace found since do not have 'pod!
2723 ((looking-at "[ \t]*\n=")
2724 (error "Spaces before POD section!"))
2725 ((and (not cperl-indent-left-aligned-comments)
2726 (looking-at "^#"))
2727 [comment-special:at-beginning-of-line])
2728 ((get-text-property (point) 'in-pod)
2729 [in-pod])
2731 (beginning-of-line)
2732 (let* ((indent-point (point))
2733 (char-after-pos (save-excursion
2734 (skip-chars-forward " \t")
2735 (point)))
2736 (char-after (char-after char-after-pos))
2737 (pre-indent-point (point))
2738 p prop look-prop is-block delim)
2739 (save-excursion ; Know we are not in POD, find appropriate pos before
2740 (cperl-backward-to-noncomment nil)
2741 (setq p (max (point-min) (1- (point)))
2742 prop (get-text-property p 'syntax-type)
2743 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2744 'syntax-type))
2745 (if (memq prop '(pod here-doc format here-doc-delim))
2746 (progn
2747 (goto-char (cperl-beginning-of-property p look-prop))
2748 (beginning-of-line)
2749 (setq pre-indent-point (point)))))
2750 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2751 (let* ((case-fold-search nil)
2752 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2753 (start (or (nth 2 parse-data) ; last complete sexp terminated
2754 (nth 0 s-s))) ; Good place to start parsing
2755 (state (nth 1 s-s))
2756 (containing-sexp (car (cdr state)))
2757 old-indent)
2758 (if (and
2759 ;;containing-sexp ;; We are buggy at toplevel :-(
2760 parse-data)
2761 (progn
2762 (setcar parse-data pre-indent-point)
2763 (setcar (cdr parse-data) state)
2764 (or (nth 2 parse-data)
2765 (setcar (cddr parse-data) start))
2766 ;; Before this point: end of statement
2767 (setq old-indent (nth 3 parse-data))))
2768 (cond ((get-text-property (point) 'indentable)
2769 ;; indent to "after" the surrounding open
2770 ;; (same offset as `cperl-beautify-regexp-piece'),
2771 ;; skip blanks if we do not close the expression.
2772 (setq delim ; We do not close the expression
2773 (get-text-property
2774 (cperl-1+ char-after-pos) 'indentable)
2775 p (1+ (cperl-beginning-of-property
2776 (point) 'indentable))
2777 is-block ; misused for: preceeding line in REx
2778 (save-excursion ; Find preceeding line
2779 (cperl-backward-to-noncomment p)
2780 (beginning-of-line)
2781 (if (<= (point) p)
2782 (progn ; get indent from the first line
2783 (goto-char p)
2784 (skip-chars-forward " \t")
2785 (if (memq (char-after (point))
2786 (append "#\n" nil))
2787 nil ; Can't use intentation of this line...
2788 (point)))
2789 (skip-chars-forward " \t")
2790 (point)))
2791 prop (parse-partial-sexp p char-after-pos))
2792 (cond ((not delim) ; End the REx, ignore is-block
2793 (vector 'indentable 'terminator p is-block))
2794 (is-block ; Indent w.r.t. preceeding line
2795 (vector 'indentable 'cont-line char-after-pos
2796 is-block char-after p))
2797 (t ; No preceeding line...
2798 (vector 'indentable 'first-line p))))
2799 ((get-text-property char-after-pos 'REx-part2)
2800 (vector 'REx-part2 (point)))
2801 ((nth 3 state)
2802 [comment])
2803 ((nth 4 state)
2804 [string])
2805 ;; XXXX Do we need to special-case this?
2806 ((null containing-sexp)
2807 ;; Line is at top level. May be data or function definition,
2808 ;; or may be function argument declaration.
2809 ;; Indent like the previous top level line
2810 ;; unless that ends in a closeparen without semicolon,
2811 ;; in which case this line is the first argument decl.
2812 (skip-chars-forward " \t")
2813 (cperl-backward-to-noncomment (or old-indent (point-min)))
2814 (setq state
2815 (or (bobp)
2816 (eq (point) old-indent) ; old-indent was at comment
2817 (eq (preceding-char) ?\;)
2818 ;; Had ?\) too
2819 (and (eq (preceding-char) ?\})
2820 (cperl-after-block-and-statement-beg
2821 (point-min))) ; Was start - too close
2822 (memq char-after (append ")]}" nil))
2823 (and (eq (preceding-char) ?\:) ; label
2824 (progn
2825 (forward-sexp -1)
2826 (skip-chars-backward " \t")
2827 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2828 (get-text-property (point) 'first-format-line)))
2830 ;; Look at previous line that's at column 0
2831 ;; to determine whether we are in top-level decls
2832 ;; or function's arg decls. Set basic-indent accordingly.
2833 ;; Now add a little if this is a continuation line.
2834 (and state
2835 parse-data
2836 (not (eq char-after ?\C-j))
2837 (setcdr (cddr parse-data)
2838 (list pre-indent-point)))
2839 (vector 'toplevel start char-after state (nth 2 s-s)))
2840 ((not
2841 (or (setq is-block
2842 (and (setq delim (= (char-after containing-sexp) ?{))
2843 (save-excursion ; Is it a hash?
2844 (goto-char containing-sexp)
2845 (cperl-block-p))))
2846 cperl-indent-parens-as-block))
2847 ;; group is an expression, not a block:
2848 ;; indent to just after the surrounding open parens,
2849 ;; skip blanks if we do not close the expression.
2850 (goto-char (1+ containing-sexp))
2851 (or (memq char-after
2852 (append (if delim "}" ")]}") nil))
2853 (looking-at "[ \t]*\\(#\\|$\\)")
2854 (skip-chars-forward " \t"))
2855 (setq old-indent (point)) ; delim=is-brace
2856 (vector 'in-parens char-after (point) delim containing-sexp))
2858 ;; Statement level. Is it a continuation or a new statement?
2859 ;; Find previous non-comment character.
2860 (goto-char pre-indent-point) ; Skip one level of POD/etc
2861 (cperl-backward-to-noncomment containing-sexp)
2862 ;; Back up over label lines, since they don't
2863 ;; affect whether our line is a continuation.
2864 ;; (Had \, too)
2865 (while;;(or (eq (preceding-char) ?\,)
2866 (and (eq (preceding-char) ?:)
2867 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2868 (memq (char-syntax (char-after (- (point) 2)))
2869 '(?w ?_))))
2871 ;; This is always FALSE?
2872 (if (eq (preceding-char) ?\,)
2873 ;; Will go to beginning of line, essentially.
2874 ;; Will ignore embedded sexpr XXXX.
2875 (cperl-backward-to-start-of-continued-exp containing-sexp))
2876 (beginning-of-line)
2877 (cperl-backward-to-noncomment containing-sexp))
2878 ;; Now we get non-label preceeding the indent point
2879 (if (not (or (eq (1- (point)) containing-sexp)
2880 (memq (preceding-char)
2881 (append (if is-block " ;{" " ,;{") '(nil)))
2882 (and (eq (preceding-char) ?\})
2883 (cperl-after-block-and-statement-beg
2884 containing-sexp))
2885 (get-text-property (point) 'first-format-line)))
2886 ;; This line is continuation of preceding line's statement;
2887 ;; indent `cperl-continued-statement-offset' more than the
2888 ;; previous line of the statement.
2890 ;; There might be a label on this line, just
2891 ;; consider it bad style and ignore it.
2892 (progn
2893 (cperl-backward-to-start-of-continued-exp containing-sexp)
2894 (vector 'continuation (point) char-after is-block delim))
2895 ;; This line starts a new statement.
2896 ;; Position following last unclosed open brace
2897 (goto-char containing-sexp)
2898 ;; Is line first statement after an open-brace?
2900 ;; If no, find that first statement and indent like
2901 ;; it. If the first statement begins with label, do
2902 ;; not believe when the indentation of the label is too
2903 ;; small.
2904 (save-excursion
2905 (forward-char 1)
2906 (let ((colon-line-end 0))
2907 (while
2908 (progn (skip-chars-forward " \t\n")
2909 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]\\|=[a-zA-Z]"))
2910 ;; Skip over comments and labels following openbrace.
2911 (cond ((= (following-char) ?\#)
2912 (forward-line 1))
2913 ((= (following-char) ?\=)
2914 (goto-char
2915 (or (next-single-property-change (point) 'in-pod)
2916 (point-max)))) ; do not loop if no syntaxification
2917 ;; label:
2919 (save-excursion (end-of-line)
2920 (setq colon-line-end (point)))
2921 (search-forward ":"))))
2922 ;; We are at beginning of code (NOT label or comment)
2923 ;; First, the following code counts
2924 ;; if it is before the line we want to indent.
2925 (and (< (point) indent-point)
2926 (vector 'have-prev-sibling (point) colon-line-end
2927 containing-sexp))))
2928 (progn
2929 ;; If no previous statement,
2930 ;; indent it relative to line brace is on.
2932 ;; For open-braces not the first thing in a line,
2933 ;; add in cperl-brace-imaginary-offset.
2935 ;; If first thing on a line: ?????
2936 ;; Move back over whitespace before the openbrace.
2937 (setq ; brace first thing on a line
2938 old-indent (progn (skip-chars-backward " \t") (bolp)))
2939 ;; Should we indent w.r.t. earlier than start?
2940 ;; Move to start of control group, possibly on a different line
2941 (or cperl-indent-wrt-brace
2942 (cperl-backward-to-noncomment (point-min)))
2943 ;; If the openbrace is preceded by a parenthesized exp,
2944 ;; move to the beginning of that;
2945 (if (eq (preceding-char) ?\))
2946 (progn
2947 (forward-sexp -1)
2948 (cperl-backward-to-noncomment (point-min))))
2949 ;; In the case it starts a subroutine, indent with
2950 ;; respect to `sub', not with respect to the
2951 ;; first thing on the line, say in the case of
2952 ;; anonymous sub in a hash.
2953 (if (and;; Is it a sub in group starting on this line?
2954 (cond ((get-text-property (point) 'attrib-group)
2955 (goto-char (cperl-beginning-of-property
2956 (point) 'attrib-group)))
2957 ((eq (preceding-char) ?b)
2958 (forward-sexp -1)
2959 (looking-at "sub\\>")))
2960 (setq p (nth 1 ; start of innermost containing list
2961 (parse-partial-sexp
2962 (save-excursion (beginning-of-line)
2963 (point))
2964 (point)))))
2965 (progn
2966 (goto-char (1+ p)) ; enclosing block on the same line
2967 (skip-chars-forward " \t")
2968 (vector 'code-start-in-block containing-sexp char-after
2969 (and delim (not is-block)) ; is a HASH
2970 old-indent ; brace first thing on a line
2971 t (point) ; have something before...
2973 ;;(current-column)
2975 ;; Get initial indentation of the line we are on.
2976 ;; If line starts with label, calculate label indentation
2977 (vector 'code-start-in-block containing-sexp char-after
2978 (and delim (not is-block)) ; is a HASH
2979 old-indent ; brace first thing on a line
2980 nil (point) ; nothing interesting before
2981 ))))))))))))))
2983 (defvar cperl-indent-rules-alist
2984 '((pod nil) ; via `syntax-type' property
2985 (here-doc nil) ; via `syntax-type' property
2986 (here-doc-delim nil) ; via `syntax-type' property
2987 (format nil) ; via `syntax-type' property
2988 (in-pod nil) ; via `in-pod' property
2989 (comment-special:at-beginning-of-line nil)
2990 (string t)
2991 (comment nil))
2992 "Alist of indentation rules for CPerl mode.
2993 The values mean:
2994 nil: do not indent;
2995 number: add this amount of indentation.
2997 Not finished.")
2999 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3000 "Return appropriate indentation for current line as Perl code.
3001 In usual case returns an integer: the column to indent to.
3002 Returns nil if line starts inside a string, t if in a comment.
3004 Will not correct the indentation for labels, but will correct it for braces
3005 and closing parentheses and brackets."
3006 ;; This code is still a broken architecture: in some cases we need to
3007 ;; compensate for some modifications which `cperl-indent-line' will add later
3008 (save-excursion
3009 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3010 (cond
3011 ;;((or (null i) (eq i t) (numberp i))
3012 ;; i)
3013 ((vectorp i)
3014 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3015 (cond
3016 (what (cadr what)) ; Load from table
3018 ;; Indenters for regular expressions with //x and qw()
3020 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3021 (goto-char (elt i 1))
3022 (condition-case nil ; Use indentation of the 1st part
3023 (forward-sexp -1))
3024 (current-column))
3025 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3026 (cond ;;; [indentable terminator start-pos is-block]
3027 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3028 (goto-char (elt i 2)) ; After opening parens
3029 (1- (current-column)))
3030 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3031 (goto-char (elt i 2))
3032 (+ (or cperl-regexp-indent-step cperl-indent-level)
3034 (current-column)))
3035 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3036 ;; Indent as the level after closing parens
3037 (goto-char (elt i 2)) ; indent line
3038 (skip-chars-forward " \t)") ; Skip closing parens
3039 (setq p (point))
3040 (goto-char (elt i 3)) ; previous line
3041 (skip-chars-forward " \t)") ; Skip closing parens
3042 ;; Number of parens in between:
3043 (setq p (nth 0 (parse-partial-sexp (point) p))
3044 what (elt i 4)) ; First char on current line
3045 (goto-char (elt i 3)) ; previous line
3046 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3047 (cond ((eq what ?\) )
3048 (- cperl-close-paren-offset)) ; compensate
3049 ((eq what ?\| )
3050 (- (or cperl-regexp-indent-step cperl-indent-level)))
3051 (t 0))
3052 (if (eq (following-char) ?\| )
3053 (or cperl-regexp-indent-step cperl-indent-level)
3055 (current-column)))
3057 (error "Unrecognized value of indent: %s" i))))
3059 ;; Indenter for stuff at toplevel
3061 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3062 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3063 (goto-char (elt i 1)) ; start = Good place to start parsing
3064 (- (current-indentation) ;
3065 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3066 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3067 ;; Look at previous line that's at column 0
3068 ;; to determine whether we are in top-level decls
3069 ;; or function's arg decls. Set basic-indent accordingly.
3070 ;; Now add a little if this is a continuation line.
3071 (if (elt i 3) ; state (XXX What is the semantic???)
3073 cperl-continued-statement-offset)))
3075 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3077 ((eq 'in-parens (elt i 0))
3078 ;; in-parens char-after old-indent-point is-brace containing-sexp
3080 ;; group is an expression, not a block:
3081 ;; indent to just after the surrounding open parens,
3082 ;; skip blanks if we do not close the expression.
3083 (+ (progn
3084 (goto-char (elt i 2)) ; old-indent-point
3085 (current-column))
3086 (if (and (elt i 3) ; is-brace
3087 (eq (elt i 1) ?\})) ; char-after
3088 ;; Correct indentation of trailing ?\}
3089 (+ cperl-indent-level cperl-close-paren-offset)
3090 0)))
3092 ;; Indenter for continuation lines
3094 ((eq 'continuation (elt i 0))
3095 ;; [continuation statement-start char-after is-block is-brace]
3096 (goto-char (elt i 1)) ; statement-start
3097 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3098 0 ; Closing parenth
3099 cperl-continued-statement-offset)
3100 (if (or (elt i 3) ; is-block
3101 (not (elt i 4)) ; is-brace
3102 (not (eq (elt i 2) ?\}))) ; char-after
3104 ;; Now it is a hash reference
3105 (+ cperl-indent-level cperl-close-paren-offset))
3106 ;; Labels do not take :: ...
3107 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3108 (if (> (current-indentation) cperl-min-label-indent)
3109 (- (current-indentation) cperl-label-offset)
3110 ;; Do not move `parse-data', this should
3111 ;; be quick anyway (this comment comes
3112 ;; from different location):
3113 (cperl-calculate-indent))
3114 (current-column))
3115 (if (eq (elt i 2) ?\{) ; char-after
3116 cperl-continued-brace-offset 0)))
3118 ;; Indenter for lines in a block which are not leading lines
3120 ((eq 'have-prev-sibling (elt i 0))
3121 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3122 (goto-char (elt i 1))
3123 (if (> (elt i 2) (point)) ; colon-line-end; After-label, same line
3124 (if (> (current-indentation)
3125 cperl-min-label-indent)
3126 (- (current-indentation) cperl-label-offset)
3127 ;; Do not believe: `max' was involved in calculation of indent
3128 (+ cperl-indent-level
3129 (save-excursion
3130 (goto-char (elt i 3)) ; block-start
3131 (current-indentation))))
3132 (current-column)))
3134 ;; Indenter for the first line in a block
3136 ((eq 'code-start-in-block (elt i 0))
3137 ;;[code-start-in-block before-brace char-after
3138 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3139 ;; group-starts-before-start-of-sub start-of-control-group]
3140 (goto-char (elt i 1))
3141 ;; For open brace in column zero, don't let statement
3142 ;; start there too. If cperl-indent-level=0,
3143 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3144 (+ (if (and (bolp) (zerop cperl-indent-level))
3145 (+ cperl-brace-offset cperl-continued-statement-offset)
3146 cperl-indent-level)
3147 (if (and (elt i 3) ; is-a-HASH-ref
3148 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3149 (+ cperl-indent-level cperl-close-paren-offset)
3151 ;; Unless openbrace is the first nonwhite thing on the line,
3152 ;; add the cperl-brace-imaginary-offset.
3153 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3154 cperl-brace-imaginary-offset)
3155 (progn
3156 (goto-char (elt i 6)) ; start-of-control-group
3157 (if (elt i 5) ; group-starts-before-start-of-sub
3158 (current-column)
3159 ;; Get initial indentation of the line we are on.
3160 ;; If line starts with label, calculate label indentation
3161 (if (save-excursion
3162 (beginning-of-line)
3163 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3164 (if (> (current-indentation) cperl-min-label-indent)
3165 (- (current-indentation) cperl-label-offset)
3166 ;; Do not move `parse-data', this should
3167 ;; be quick anyway:
3168 (cperl-calculate-indent))
3169 (current-indentation))))))
3171 (error "Unrecognized value of indent: %s" i))))
3173 (error "Got strange value of indent: %s" i))))))
3175 (defvar cperl-indent-alist
3176 '((string nil)
3177 (comment nil)
3178 (toplevel 0)
3179 (toplevel-after-parenth 2)
3180 (toplevel-continued 2)
3181 (expression 1))
3182 "Alist of indentation rules for CPerl mode.
3183 The values mean:
3184 nil: do not indent;
3185 number: add this amount of indentation.
3187 Not finished, not used.")
3189 (defun cperl-where-am-i (&optional parse-start start-state)
3190 ;; Unfinished
3191 "Return a list of lists ((TYPE POS)...) of good points before the point.
3192 POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'.
3194 Not finished, not used."
3195 (save-excursion
3196 (let* ((start-point (point)) unused
3197 (s-s (cperl-get-state))
3198 (start (nth 0 s-s))
3199 (state (nth 1 s-s))
3200 (prestart (nth 3 s-s))
3201 (containing-sexp (car (cdr state)))
3202 (case-fold-search nil)
3203 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
3204 (cond ((nth 3 state) ; In string
3205 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
3206 ((nth 4 state) ; In comment
3207 (setq res (cons '(comment) res)))
3208 ((null containing-sexp)
3209 ;; Line is at top level.
3210 ;; Indent like the previous top level line
3211 ;; unless that ends in a closeparen without semicolon,
3212 ;; in which case this line is the first argument decl.
3213 (cperl-backward-to-noncomment (or parse-start (point-min)))
3214 ;;(skip-chars-backward " \t\f\n")
3215 (cond
3216 ((or (bobp)
3217 (memq (preceding-char) (append ";}" nil)))
3218 (setq res (cons (list 'toplevel start) res)))
3219 ((eq (preceding-char) ?\) )
3220 (setq res (cons (list 'toplevel-after-parenth start) res)))
3222 (setq res (cons (list 'toplevel-continued start) res)))))
3223 ((/= (char-after containing-sexp) ?{)
3224 ;; line is expression, not statement:
3225 ;; indent to just after the surrounding open.
3226 ;; skip blanks if we do not close the expression.
3227 (setq res (cons (list 'expression-blanks
3228 (progn
3229 (goto-char (1+ containing-sexp))
3230 (or (looking-at "[ \t]*\\(#\\|$\\)")
3231 (skip-chars-forward " \t"))
3232 (point)))
3233 (cons (list 'expression containing-sexp) res))))
3234 ((progn
3235 ;; Containing-expr starts with \{. Check whether it is a hash.
3236 (goto-char containing-sexp)
3237 (not (cperl-block-p)))
3238 (setq res (cons (list 'expression-blanks
3239 (progn
3240 (goto-char (1+ containing-sexp))
3241 (or (looking-at "[ \t]*\\(#\\|$\\)")
3242 (skip-chars-forward " \t"))
3243 (point)))
3244 (cons (list 'expression containing-sexp) res))))
3246 ;; Statement level.
3247 (setq res (cons (list 'in-block containing-sexp) res))
3248 ;; Is it a continuation or a new statement?
3249 ;; Find previous non-comment character.
3250 (cperl-backward-to-noncomment containing-sexp)
3251 ;; Back up over label lines, since they don't
3252 ;; affect whether our line is a continuation.
3253 ;; Back up comma-delimited lines too ?????
3254 (while (or (eq (preceding-char) ?\,)
3255 (save-excursion (cperl-after-label)))
3256 (if (eq (preceding-char) ?\,)
3257 ;; Will go to beginning of line, essentially
3258 ;; Will ignore embedded sexpr XXXX.
3259 (cperl-backward-to-start-of-continued-exp containing-sexp))
3260 (beginning-of-line)
3261 (cperl-backward-to-noncomment containing-sexp))
3262 ;; Now we get the answer.
3263 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
3264 ;; This line is continuation of preceding line's statement.
3265 (list (list 'statement-continued containing-sexp))
3266 ;; This line starts a new statement.
3267 ;; Position following last unclosed open.
3268 (goto-char containing-sexp)
3269 ;; Is line first statement after an open-brace?
3271 ;; If no, find that first statement and indent like
3272 ;; it. If the first statement begins with label, do
3273 ;; not believe when the indentation of the label is too
3274 ;; small.
3275 (save-excursion
3276 (forward-char 1)
3277 (let ((colon-line-end 0))
3278 (while (progn (skip-chars-forward " \t\n" start-point)
3279 (and (< (point) start-point)
3280 (looking-at
3281 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
3282 ;; Skip over comments and labels following openbrace.
3283 (cond ((= (following-char) ?\#)
3284 ;;(forward-line 1)
3285 (end-of-line))
3286 ;; label:
3288 (save-excursion (end-of-line)
3289 (setq colon-line-end (point)))
3290 (search-forward ":"))))
3291 ;; Now at the point, after label, or at start
3292 ;; of first statement in the block.
3293 (and (< (point) start-point)
3294 (if (> colon-line-end (point))
3295 ;; Before statement after label
3296 (if (> (current-indentation)
3297 cperl-min-label-indent)
3298 (list (list 'label-in-block (point)))
3299 ;; Do not believe: `max' is involved
3300 (list
3301 (list 'label-in-block-min-indent (point))))
3302 ;; Before statement
3303 (list 'statement-in-block (point))))))
3304 ;; If no previous statement,
3305 ;; indent it relative to line brace is on.
3306 ;; For open brace in column zero, don't let statement
3307 ;; start there too. If cperl-indent-level is zero,
3308 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3309 ;; For open-braces not the first thing in a line,
3310 ;; add in cperl-brace-imaginary-offset.
3312 ;; If first thing on a line: ?????
3313 (setq unused ; This is not finished...
3314 (+ (if (and (bolp) (zerop cperl-indent-level))
3315 (+ cperl-brace-offset cperl-continued-statement-offset)
3316 cperl-indent-level)
3317 ;; Move back over whitespace before the openbrace.
3318 ;; If openbrace is not first nonwhite thing on the line,
3319 ;; add the cperl-brace-imaginary-offset.
3320 (progn (skip-chars-backward " \t")
3321 (if (bolp) 0 cperl-brace-imaginary-offset))
3322 ;; If the openbrace is preceded by a parenthesized exp,
3323 ;; move to the beginning of that;
3324 ;; possibly a different line
3325 (progn
3326 (if (eq (preceding-char) ?\))
3327 (forward-sexp -1))
3328 ;; Get initial indentation of the line we are on.
3329 ;; If line starts with label, calculate label indentation
3330 (if (save-excursion
3331 (beginning-of-line)
3332 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3333 (if (> (current-indentation) cperl-min-label-indent)
3334 (- (current-indentation) cperl-label-offset)
3335 (cperl-calculate-indent))
3336 (current-indentation)))))))))
3337 res)))
3339 (defun cperl-calculate-indent-within-comment ()
3340 "Return the indentation amount for line, assuming that
3341 the current line is to be regarded as part of a block comment."
3342 (let (end star-start)
3343 (save-excursion
3344 (beginning-of-line)
3345 (skip-chars-forward " \t")
3346 (setq end (point))
3347 (and (= (following-char) ?#)
3348 (forward-line -1)
3349 (cperl-to-comment-or-eol)
3350 (setq end (point)))
3351 (goto-char end)
3352 (current-column))))
3355 (defun cperl-to-comment-or-eol ()
3356 "Go to position before comment on the current line, or to end of line.
3357 Returns true if comment is found. In POD will not move the point."
3358 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3359 ;; then looks for literal # or end-of-line.
3360 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3361 (or cperl-font-locking
3362 (cperl-update-syntaxification lim lim))
3363 (beginning-of-line)
3364 (if (setq pr (get-text-property (point) 'syntax-type))
3365 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3366 (if (or (eq pr 'pod)
3367 (if (or (not e) (> e lim)) ; deep inside a group
3368 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3369 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3370 ;; Else - need to do it the hard way
3371 (and (and e (<= e lim))
3372 (goto-char e))
3373 (while (not stop-in)
3374 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3375 ; stop at comment
3376 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3377 (if (nth 4 state) ; After `#';
3378 ; (nth 2 state) can be
3379 ; beginning of m,s,qq and so
3380 ; on
3381 (if (nth 2 state)
3382 (progn
3383 (setq cpoint (point))
3384 (goto-char (nth 2 state))
3385 (cond
3386 ((looking-at "\\(s\\|tr\\)\\>")
3387 (or (re-search-forward
3388 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3389 lim 'move)
3390 (setq stop-in t)))
3391 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3392 (or (re-search-forward
3393 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3394 lim 'move)
3395 (setq stop-in t)))
3396 (t ; It was fair comment
3397 (setq stop-in t) ; Finish
3398 (goto-char (1- cpoint)))))
3399 (setq stop-in t) ; Finish
3400 (forward-char -1))
3401 (setq stop-in t))) ; Finish
3402 (nth 4 state))))
3404 (defsubst cperl-modify-syntax-type (at how)
3405 (if (< at (point-max))
3406 (progn
3407 (put-text-property at (1+ at) 'syntax-table how)
3408 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3410 (defun cperl-protect-defun-start (s e)
3411 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3412 (save-excursion
3413 (goto-char s)
3414 (while (re-search-forward "^\\s(" e 'to-end)
3415 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3417 (defun cperl-commentify (bb e string &optional noface)
3418 (if cperl-use-syntax-table-text-property
3419 (if (eq noface 'n) ; Only immediate
3421 ;; We suppose that e is _after_ the end of construction, as after eol.
3422 (setq string (if string cperl-st-sfence cperl-st-cfence))
3423 (if (> bb (- e 2))
3424 ;; one-char string/comment?!
3425 (cperl-modify-syntax-type bb cperl-st-punct)
3426 (cperl-modify-syntax-type bb string)
3427 (cperl-modify-syntax-type (1- e) string))
3428 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3429 (put-text-property (1+ bb) (1- e)
3430 'syntax-table cperl-string-syntax-table))
3431 (cperl-protect-defun-start bb e))
3432 ;; Fontify
3433 (or noface
3434 (not cperl-pod-here-fontify)
3435 (put-text-property bb e 'face (if string 'font-lock-string-face
3436 'font-lock-comment-face)))))
3438 (defvar cperl-starters '(( ?\( . ?\) )
3439 ( ?\[ . ?\] )
3440 ( ?\{ . ?\} )
3441 ( ?\< . ?\> )))
3443 (defun cperl-cached-syntax-table (st)
3444 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3445 All the entries of the syntax table are \".\", except for a backslash, which
3446 is quoting."
3447 (if (car-safe st)
3448 (car st)
3449 (setcar st (make-syntax-table))
3450 (setq st (car st))
3451 (let ((i 0))
3452 (while (< i 256)
3453 (modify-syntax-entry i "." st)
3454 (setq i (1+ i))))
3455 (modify-syntax-entry ?\\ "\\" st)
3456 st))
3458 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3459 &optional ostart oend)
3460 "Find the end of a regular expression or a stringish construct (q[] etc).
3461 The point should be before the starting delimiter.
3463 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3464 is s/// or tr/// like expression. If END is nil, generates an error
3465 message if needed. If SET-ST is non-nil, will use (or generate) a
3466 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3467 error message in its CAR (unless it already contains some error
3468 message). ARGUMENT should be the name of the construct (used in error
3469 messages). OSTART, OEND may be set in recursive calls when processing
3470 the second argument of 2ARG construct.
3472 Works *before* syntax recognition is done. In IS-2ARG situation may
3473 modify syntax-type text property if the situation is too hard."
3474 (let (b starter ender st i i2 go-forward reset-st set-st)
3475 (skip-chars-forward " \t")
3476 ;; ender means matching-char matcher.
3477 (setq b (point)
3478 starter (if (eobp) 0 (char-after b))
3479 ender (cdr (assoc starter cperl-starters)))
3480 ;; What if starter == ?\\ ????
3481 (setq st (cperl-cached-syntax-table st-l))
3482 (setq set-st t)
3483 ;; Whether we have an intermediate point
3484 (setq i nil)
3485 ;; Prepare the syntax table:
3486 (if (not ender) ; m/blah/, s/x//, s/x/y/
3487 (modify-syntax-entry starter "$" st)
3488 (modify-syntax-entry starter (concat "(" (list ender)) st)
3489 (modify-syntax-entry ender (concat ")" (list starter)) st))
3490 (condition-case bb
3491 (progn
3492 ;; We use `$' syntax class to find matching stuff, but $$
3493 ;; is recognized the same as $, so we need to check this manually.
3494 (if (and (eq starter (char-after (cperl-1+ b)))
3495 (not ender))
3496 ;; $ has TeXish matching rules, so $$ equiv $...
3497 (forward-char 2)
3498 (setq reset-st (syntax-table))
3499 (set-syntax-table st)
3500 (forward-sexp 1)
3501 (if (<= (point) (1+ b))
3502 (error "Unfinished regular expression"))
3503 (set-syntax-table reset-st)
3504 (setq reset-st nil)
3505 ;; Now the problem is with m;blah;;
3506 (and (not ender)
3507 (eq (preceding-char)
3508 (char-after (- (point) 2)))
3509 (save-excursion
3510 (forward-char -2)
3511 (= 0 (% (skip-chars-backward "\\\\") 2)))
3512 (forward-char -1)))
3513 ;; Now we are after the first part.
3514 (and is-2arg ; Have trailing part
3515 (not ender)
3516 (eq (following-char) starter) ; Empty trailing part
3517 (progn
3518 (or (eq (char-syntax (following-char)) ?.)
3519 ;; Make trailing letter into punctuation
3520 (cperl-modify-syntax-type (point) cperl-st-punct))
3521 (setq is-2arg nil go-forward t))) ; Ignore the tail
3522 (if is-2arg ; Not number => have second part
3523 (progn
3524 (setq i (point) i2 i)
3525 (if ender
3526 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3527 (progn
3528 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3529 (goto-char (match-end 0))
3530 (skip-chars-forward " \t\n\f"))
3531 (setq i2 (point))))
3532 (forward-char -1))
3533 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3534 (if ender (modify-syntax-entry ender "." st))
3535 (setq set-st nil)
3536 (setq ender (cperl-forward-re lim end nil st-l err-l
3537 argument starter ender)
3538 ender (nth 2 ender)))))
3539 (error (goto-char lim)
3540 (setq set-st nil)
3541 (if reset-st
3542 (set-syntax-table reset-st))
3543 (or end
3544 (message
3545 "End of `%s%s%c ... %c' string/RE not found: %s"
3546 argument
3547 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3548 starter (or ender starter) bb)
3549 (or (car err-l) (setcar err-l b)))))
3550 (if set-st
3551 (progn
3552 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3553 (if ender (modify-syntax-entry ender "." st))))
3554 ;; i: have 2 args, after end of the first arg
3555 ;; i2: start of the second arg, if any (before delim iff `ender').
3556 ;; ender: the last arg bounded by parens-like chars, the second one of them
3557 ;; starter: the starting delimiter of the first arg
3558 ;; go-forward: has 2 args, and the second part is empty
3559 (list i i2 ender starter go-forward)))
3561 (defun cperl-forward-group-in-re (&optional st-l)
3562 "Find the end of a group in a REx.
3563 Return the error message (if any). Does not work if delimiter is `)'.
3564 Works before syntax recognition is done."
3565 ;; Works *before* syntax recognition is done
3566 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3567 (let (st b reset-st)
3568 (condition-case b
3569 (progn
3570 (setq st (cperl-cached-syntax-table st-l))
3571 (modify-syntax-entry ?\( "()" st)
3572 (modify-syntax-entry ?\) ")(" st)
3573 (setq reset-st (syntax-table))
3574 (set-syntax-table st)
3575 (forward-sexp 1))
3576 (error (message
3577 "cperl-forward-group-in-re: error %s" b)))
3578 ;; now restore the initial state
3579 (if st
3580 (progn
3581 (modify-syntax-entry ?\( "." st)
3582 (modify-syntax-entry ?\) "." st)))
3583 (if reset-st
3584 (set-syntax-table reset-st))
3588 (defvar font-lock-string-face)
3589 ;;(defvar font-lock-reference-face)
3590 (defvar font-lock-constant-face)
3591 (defsubst cperl-postpone-fontification (b e type val &optional now)
3592 ;; Do after syntactic fontification?
3593 (if cperl-syntaxify-by-font-lock
3594 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3595 (put-text-property b e type val)))
3597 ;;; Here is how the global structures (those which cannot be
3598 ;;; recognized locally) are marked:
3599 ;; a) PODs:
3600 ;; Start-to-end is marked `in-pod' ==> t
3601 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3602 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3603 ;; b) HEREs:
3604 ;; Start-to-end is marked `here-doc-group' ==> t
3605 ;; The body is marked `syntax-type' ==> `here-doc'
3606 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3607 ;; c) FORMATs:
3608 ;; First line (to =) marked `first-format-line' ==> t
3609 ;; After-this--to-end is marked `syntax-type' ==> `format'
3610 ;; d) 'Q'uoted string:
3611 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3612 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3613 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3614 ;; e) Attributes of subroutines: `attrib-group' ==> t
3615 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3616 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3618 ;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3619 ;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3621 (defun cperl-unwind-to-safe (before &optional end)
3622 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3623 (let ((pos (point)) opos)
3624 (while (and pos (progn
3625 (beginning-of-line)
3626 (get-text-property (setq pos (point)) 'syntax-type)))
3627 (setq opos pos
3628 pos (cperl-beginning-of-property pos 'syntax-type))
3629 (if (eq pos (point-min))
3630 (setq pos nil))
3631 (if pos
3632 (if before
3633 (progn
3634 (goto-char (cperl-1- pos))
3635 (beginning-of-line)
3636 (setq pos (point)))
3637 (goto-char (setq pos (cperl-1- pos))))
3638 ;; Up to the start
3639 (goto-char (point-min))))
3640 ;; Skip empty lines
3641 (and (looking-at "\n*=")
3642 (/= 0 (skip-chars-backward "\n"))
3643 (forward-char))
3644 (setq pos (point))
3645 (if end
3646 ;; Do the same for end, going small steps
3647 (save-excursion
3648 (while (and end (get-text-property end 'syntax-type))
3649 (setq pos end
3650 end (next-single-property-change end 'syntax-type nil (point-max)))
3651 (if end (progn (goto-char end)
3652 (or (bolp) (forward-line 1))
3653 (setq end (point)))))
3654 (or end pos)))))
3656 ;;; These are needed for byte-compile (at least with v19)
3657 (defvar cperl-nonoverridable-face)
3658 (defvar font-lock-variable-name-face)
3659 (defvar font-lock-function-name-face)
3660 (defvar font-lock-keyword-face)
3661 (defvar font-lock-builtin-face)
3662 (defvar font-lock-type-face)
3663 (defvar font-lock-comment-face)
3664 (defvar font-lock-warning-face)
3666 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3667 "Syntaxically mark (and fontify) attributes of a subroutine.
3668 Should be called with the point before leading colon of an attribute."
3669 ;; Works *before* syntax recognition is done
3670 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3671 (let (st b p reset-st after-first (start (point)) start1 end1)
3672 (condition-case b
3673 (while (looking-at
3674 (concat
3675 "\\(" ; 1=optional? colon
3676 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3677 "\\)"
3678 (if after-first "?" "")
3679 ;; No space between name and paren allowed...
3680 "\\(\\sw+\\)" ; 3=name
3681 "\\((\\)?")) ; 4=optional paren
3682 (and (match-beginning 1)
3683 (cperl-postpone-fontification
3684 (match-beginning 0) (cperl-1+ (match-beginning 0))
3685 'face font-lock-constant-face))
3686 (setq start1 (match-beginning 3) end1 (match-end 3))
3687 (cperl-postpone-fontification start1 end1
3688 'face font-lock-constant-face)
3689 (goto-char end1) ; end or before `('
3690 (if (match-end 4) ; Have attribute arguments...
3691 (progn
3692 (if st nil
3693 (setq st (cperl-cached-syntax-table st-l))
3694 (modify-syntax-entry ?\( "()" st)
3695 (modify-syntax-entry ?\) ")(" st))
3696 (setq reset-st (syntax-table) p (point))
3697 (set-syntax-table st)
3698 (forward-sexp 1)
3699 (set-syntax-table reset-st)
3700 (setq reset-st nil)
3701 (cperl-commentify p (point) t))) ; mark as string
3702 (forward-comment (buffer-size))
3703 (setq after-first t))
3704 (error (message
3705 "L%d: attribute `%s': %s"
3706 (count-lines (point-min) (point))
3707 (and start1 end1 (buffer-substring start1 end1)) b)
3708 (setq start nil)))
3709 (and start
3710 (progn
3711 (put-text-property start (point)
3712 'attrib-group (if (looking-at "{") t 0))
3713 (and pos
3714 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3715 ;; Apparently, we do not need `multiline': faces added now
3716 (put-text-property (+ 3 pos) (cperl-1+ (point))
3717 'syntax-type 'sub-decl))
3718 (and b-fname ; Fontify here: the following condition
3719 (cperl-postpone-fontification ; is too hard to determine by
3720 b-fname e-fname 'face ; a REx, so do it here
3721 (if (looking-at "{")
3722 font-lock-function-name-face
3723 font-lock-variable-name-face)))))
3724 ;; now restore the initial state
3725 (if st
3726 (progn
3727 (modify-syntax-entry ?\( "." st)
3728 (modify-syntax-entry ?\) "." st)))
3729 (if reset-st
3730 (set-syntax-table reset-st))))
3732 (defsubst cperl-look-at-leading-count (is-x-REx e)
3733 (if (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3734 (1- e) t) ; return nil on failure, no moving
3735 (if (eq ?\{ (preceding-char)) nil
3736 (cperl-postpone-fontification
3737 (1- (point)) (point)
3738 'face font-lock-warning-face))))
3740 ;;; Debugging this may require (setq max-specpdl-size 2000)...
3741 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3742 "Scans the buffer for hard-to-parse Perl constructions.
3743 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3744 the sections using `cperl-pod-head-face', `cperl-pod-face',
3745 `cperl-here-face'."
3746 (interactive)
3747 (or min (setq min (point-min)
3748 cperl-syntax-state nil
3749 cperl-syntax-done-to min))
3750 (or max (setq max (point-max)))
3751 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3752 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3753 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3754 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3755 (modified (buffer-modified-p)) overshoot is-o-REx
3756 (after-change-functions nil)
3757 (cperl-font-locking t)
3758 (use-syntax-state (and cperl-syntax-state
3759 (>= min (car cperl-syntax-state))))
3760 (state-point (if use-syntax-state
3761 (car cperl-syntax-state)
3762 (point-min)))
3763 (state (if use-syntax-state
3764 (cdr cperl-syntax-state)))
3765 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3766 (st-l (list nil)) (err-l (list nil))
3767 ;; Somehow font-lock may be not loaded yet...
3768 ;; (e.g., when building TAGS via command-line call)
3769 (font-lock-string-face (if (boundp 'font-lock-string-face)
3770 font-lock-string-face
3771 'font-lock-string-face))
3772 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3773 font-lock-constant-face
3774 'font-lock-constant-face))
3775 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3776 (if (boundp 'font-lock-function-name-face)
3777 font-lock-function-name-face
3778 'font-lock-function-name-face))
3779 (font-lock-variable-name-face ; interpolated vars and ({})-code
3780 (if (boundp 'font-lock-variable-name-face)
3781 font-lock-variable-name-face
3782 'font-lock-variable-name-face))
3783 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3784 (if (boundp 'font-lock-function-name-face)
3785 font-lock-function-name-face
3786 'font-lock-function-name-face))
3787 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3788 (if (boundp 'font-lock-constant-face)
3789 font-lock-constant-face
3790 'font-lock-constant-face))
3791 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3792 (if (boundp 'font-lock-builtin-face)
3793 font-lock-builtin-face
3794 'font-lock-builtin-face))
3795 (font-lock-comment-face
3796 (if (boundp 'font-lock-comment-face)
3797 font-lock-comment-face
3798 'font-lock-comment-face))
3799 (font-lock-warning-face
3800 (if (boundp 'font-lock-warning-face)
3801 font-lock-warning-face
3802 'font-lock-warning-face))
3803 (my-cperl-REx-ctl-face ; (|)
3804 (if (boundp 'font-lock-keyword-face)
3805 font-lock-keyword-face
3806 'font-lock-keyword-face))
3807 (my-cperl-REx-modifiers-face ; //gims
3808 (if (boundp 'cperl-nonoverridable-face)
3809 cperl-nonoverridable-face
3810 'cperl-nonoverridable-face))
3811 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3812 (if (boundp 'font-lock-type-face)
3813 font-lock-type-face
3814 'font-lock-type-face))
3815 (stop-point (if ignore-max
3816 (point-max)
3817 max))
3818 (search
3819 (concat
3820 "\\(\\`\n?\\|^\n\\)=" ; POD
3821 "\\|"
3822 ;; One extra () before this:
3823 "<<" ; HERE-DOC
3824 "\\(" ; 1 + 1
3825 ;; First variant "BLAH" or just ``.
3826 "[ \t]*" ; Yes, whitespace is allowed!
3827 "\\([\"'`]\\)" ; 2 + 1 = 3
3828 "\\([^\"'`\n]*\\)" ; 3 + 1
3829 "\\3"
3830 "\\|"
3831 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3832 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3833 ;; Do not have <<= or << 30 or <<30 or << $blah.
3834 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3835 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3836 "\\)"
3837 "\\|"
3838 ;; 1+6 extra () before this:
3839 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3840 (if cperl-use-syntax-table-text-property
3841 (concat
3842 "\\|"
3843 ;; 1+6+2=9 extra () before this:
3844 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3845 "\\|"
3846 ;; 1+6+2+1=10 extra () before this:
3847 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3848 "\\|"
3849 ;; 1+6+2+1+1=11 extra () before this
3850 "\\<sub\\>" ; sub with proto/attr
3851 "\\("
3852 cperl-white-and-comment-rex
3853 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3854 "\\("
3855 cperl-maybe-white-and-comment-rex
3856 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3857 "\\|"
3858 ;; 1+6+2+1+1+6=17 extra () before this:
3859 "\\$\\(['{]\\)" ; $' or ${foo}
3860 "\\|"
3861 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3862 ;; we do not support intervening comments...):
3863 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3864 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3865 "\\|"
3866 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3867 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3868 "\\|"
3869 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3870 ""))))
3871 (unwind-protect
3872 (progn
3873 (save-excursion
3874 (or non-inter
3875 (message "Scanning for \"hard\" Perl constructions..."))
3876 ;;(message "find: %s --> %s" min max)
3877 (and cperl-pod-here-fontify
3878 ;; We had evals here, do not know why...
3879 (setq face cperl-pod-face
3880 head-face cperl-pod-head-face
3881 here-face cperl-here-face))
3882 (remove-text-properties min max
3883 '(syntax-type t in-pod t syntax-table t
3884 attrib-group t
3885 REx-interpolated t
3886 cperl-postpone t
3887 syntax-subtype t
3888 rear-nonsticky t
3889 front-sticky t
3890 here-doc-group t
3891 first-format-line t
3892 REx-part2 t
3893 indentable t))
3894 ;; Need to remove face as well...
3895 (goto-char min)
3896 (and (eq system-type 'emx)
3897 (eq (point) 1)
3898 (let ((case-fold-search t))
3899 (looking-at "extproc[ \t]")) ; Analogue of #!
3900 (cperl-commentify min
3901 (save-excursion (end-of-line) (point))
3902 nil))
3903 (while (and
3904 (< (point) max)
3905 (re-search-forward search max t))
3906 (setq tmpend nil) ; Valid for most cases
3907 (setq b (match-beginning 0)
3908 state (save-excursion (parse-partial-sexp
3909 state-point b nil nil state))
3910 state-point b)
3911 (cond
3912 ;; 1+6+2+1+1+6=17 extra () before this:
3913 ;; "\\$\\(['{]\\)"
3914 ((match-beginning 18) ; $' or ${foo}
3915 (if (eq (preceding-char) ?\') ; $'
3916 (progn
3917 (setq b (1- (point))
3918 state (parse-partial-sexp
3919 state-point (1- b) nil nil state)
3920 state-point (1- b))
3921 (if (nth 3 state) ; in string
3922 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3923 (goto-char (1+ b)))
3924 ;; else: ${
3925 (setq bb (match-beginning 0))
3926 (cperl-modify-syntax-type bb cperl-st-punct)))
3927 ;; No processing in strings/comments beyond this point:
3928 ((or (nth 3 state) (nth 4 state))
3929 t) ; Do nothing in comment/string
3930 ((match-beginning 1) ; POD section
3931 ;; "\\(\\`\n?\\|^\n\\)="
3932 (setq b (match-beginning 0)
3933 state (parse-partial-sexp
3934 state-point b nil nil state)
3935 state-point b)
3936 (if (or (nth 3 state) (nth 4 state)
3937 (looking-at "cut\\>"))
3938 (if (or (nth 3 state) (nth 4 state) ignore-max)
3939 nil ; Doing a chunk only
3940 (message "=cut is not preceded by a POD section")
3941 (or (car err-l) (setcar err-l (point))))
3942 (beginning-of-line)
3944 (setq b (point)
3945 bb b
3946 tb (match-beginning 0)
3947 b1 nil) ; error condition
3948 ;; We do not search to max, since we may be called from
3949 ;; some hook of fontification, and max is random
3950 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3951 (progn
3952 (goto-char b)
3953 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3954 (progn
3955 (message "=cut is not preceded by an empty line")
3956 (setq b1 t)
3957 (or (car err-l) (setcar err-l b))))))
3958 (beginning-of-line 2) ; An empty line after =cut is not POD!
3959 (setq e (point))
3960 (and (> e max)
3961 (progn
3962 (remove-text-properties
3963 max e '(syntax-type t in-pod t syntax-table t
3964 attrib-group t
3965 REx-interpolated t
3966 cperl-postpone t
3967 syntax-subtype t
3968 here-doc-group t
3969 rear-nonsticky t
3970 front-sticky t
3971 first-format-line t
3972 REx-part2 t
3973 indentable t))
3974 (setq tmpend tb)))
3975 (put-text-property b e 'in-pod t)
3976 (put-text-property b e 'syntax-type 'in-pod)
3977 (goto-char b)
3978 (while (re-search-forward "\n\n[ \t]" e t)
3979 ;; We start 'pod 1 char earlier to include the preceding line
3980 (beginning-of-line)
3981 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3982 (cperl-put-do-not-fontify b (point) t)
3983 ;; mark the non-literal parts as PODs
3984 (if cperl-pod-here-fontify
3985 (cperl-postpone-fontification b (point) 'face face t))
3986 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3987 (beginning-of-line)
3988 (setq b (point)))
3989 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3990 (cperl-put-do-not-fontify (point) e t)
3991 (if cperl-pod-here-fontify
3992 (progn
3993 ;; mark the non-literal parts as PODs
3994 (cperl-postpone-fontification (point) e 'face face t)
3995 (goto-char bb)
3996 (if (looking-at
3997 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3998 ;; mark the headers
3999 (cperl-postpone-fontification
4000 (match-beginning 1) (match-end 1)
4001 'face head-face))
4002 (while (re-search-forward
4003 ;; One paragraph
4004 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
4005 e 'toend)
4006 ;; mark the headers
4007 (cperl-postpone-fontification
4008 (match-beginning 1) (match-end 1)
4009 'face head-face))))
4010 (cperl-commentify bb e nil)
4011 (goto-char e)
4012 (or (eq e (point-max))
4013 (forward-char -1)))) ; Prepare for immediate POD start.
4014 ;; Here document
4015 ;; We can do many here-per-line;
4016 ;; but multiline quote on the same line as <<HERE confuses us...
4017 ;; ;; One extra () before this:
4018 ;;"<<"
4019 ;; "\\(" ; 1 + 1
4020 ;; ;; First variant "BLAH" or just ``.
4021 ;; "[ \t]*" ; Yes, whitespace is allowed!
4022 ;; "\\([\"'`]\\)" ; 2 + 1
4023 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
4024 ;; "\\3"
4025 ;; "\\|"
4026 ;; ;; Second variant: Identifier or \ID or empty
4027 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
4028 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
4029 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
4030 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
4031 ;; "\\)"
4032 ((match-beginning 2) ; 1 + 1
4033 (setq b (point)
4034 tb (match-beginning 0)
4035 c (and ; not HERE-DOC
4036 (match-beginning 5)
4037 (save-match-data
4038 (or (looking-at "[ \t]*(") ; << function_call()
4039 (save-excursion ; 1 << func_name, or $foo << 10
4040 (condition-case nil
4041 (progn
4042 (goto-char tb)
4043 ;;; XXX What to do: foo <<bar ???
4044 ;;; XXX Need to support print {a} <<B ???
4045 (forward-sexp -1)
4046 (save-match-data
4047 ; $foo << b; $f .= <<B;
4048 ; ($f+1) << b; a($f) . <<B;
4049 ; foo 1, <<B; $x{a} <<b;
4050 (cond
4051 ((looking-at "[0-9$({]")
4052 (forward-sexp 1)
4053 (and
4054 (looking-at "[ \t]*<<")
4055 (condition-case nil
4056 ;; print $foo <<EOF
4057 (progn
4058 (forward-sexp -2)
4059 (not
4060 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
4061 (error t)))))))
4062 (error nil))) ; func(<<EOF)
4063 (and (not (match-beginning 6)) ; Empty
4064 (looking-at
4065 "[ \t]*[=0-9$@%&(]"))))))
4066 (if c ; Not here-doc
4067 nil ; Skip it.
4068 (setq c (match-end 2)) ; 1 + 1
4069 (if (match-beginning 5) ;4 + 1
4070 (setq b1 (match-beginning 5) ; 4 + 1
4071 e1 (match-end 5)) ; 4 + 1
4072 (setq b1 (match-beginning 4) ; 3 + 1
4073 e1 (match-end 4))) ; 3 + 1
4074 (setq tag (buffer-substring b1 e1)
4075 qtag (regexp-quote tag))
4076 (cond (cperl-pod-here-fontify
4077 ;; Highlight the starting delimiter
4078 (cperl-postpone-fontification
4079 b1 e1 'face my-cperl-delimiters-face)
4080 (cperl-put-do-not-fontify b1 e1 t)))
4081 (forward-line)
4082 (setq i (point))
4083 (if end-of-here-doc
4084 (goto-char end-of-here-doc))
4085 (setq b (point))
4086 ;; We do not search to max, since we may be called from
4087 ;; some hook of fontification, and max is random
4088 (or (and (re-search-forward (concat "^" qtag "$")
4089 stop-point 'toend)
4090 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4092 (progn ; Pretend we matched at the end
4093 (goto-char (point-max))
4094 (re-search-forward "\\'")
4095 (message "End of here-document `%s' not found." tag)
4096 (or (car err-l) (setcar err-l b))))
4097 (if cperl-pod-here-fontify
4098 (progn
4099 ;; Highlight the ending delimiter
4100 (cperl-postpone-fontification
4101 (match-beginning 0) (match-end 0)
4102 'face my-cperl-delimiters-face)
4103 (cperl-put-do-not-fontify b (match-end 0) t)
4104 ;; Highlight the HERE-DOC
4105 (cperl-postpone-fontification b (match-beginning 0)
4106 'face here-face)))
4107 (setq e1 (cperl-1+ (match-end 0)))
4108 (put-text-property b (match-beginning 0)
4109 'syntax-type 'here-doc)
4110 (put-text-property (match-beginning 0) e1
4111 'syntax-type 'here-doc-delim)
4112 (put-text-property b e1 'here-doc-group t)
4113 ;; This makes insertion at the start of HERE-DOC update
4114 ;; the whole construct:
4115 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
4116 (cperl-commentify b e1 nil)
4117 (cperl-put-do-not-fontify b (match-end 0) t)
4118 ;; Cache the syntax info...
4119 (setq cperl-syntax-state (cons state-point state))
4120 ;; ... and process the rest of the line...
4121 (setq overshoot
4122 (elt ; non-inter ignore-max
4123 (cperl-find-pods-heres c i t end t e1) 1))
4124 (if (and overshoot (> overshoot (point)))
4125 (goto-char overshoot)
4126 (setq overshoot e1))
4127 (if (> e1 max)
4128 (setq tmpend tb))))
4129 ;; format
4130 ((match-beginning 8)
4131 ;; 1+6=7 extra () before this:
4132 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4133 (setq b (point)
4134 name (if (match-beginning 8) ; 7 + 1
4135 (buffer-substring (match-beginning 8) ; 7 + 1
4136 (match-end 8)) ; 7 + 1
4138 tb (match-beginning 0))
4139 (setq argument nil)
4140 (put-text-property (save-excursion
4141 (beginning-of-line)
4142 (point))
4143 b 'first-format-line 't)
4144 (if cperl-pod-here-fontify
4145 (while (and (eq (forward-line) 0)
4146 (not (looking-at "^[.;]$")))
4147 (cond
4148 ((looking-at "^#")) ; Skip comments
4149 ((and argument ; Skip argument multi-lines
4150 (looking-at "^[ \t]*{"))
4151 (forward-sexp 1)
4152 (setq argument nil))
4153 (argument ; Skip argument lines
4154 (setq argument nil))
4155 (t ; Format line
4156 (setq b1 (point))
4157 (setq argument (looking-at "^[^\n]*[@^]"))
4158 (end-of-line)
4159 ;; Highlight the format line
4160 (cperl-postpone-fontification b1 (point)
4161 'face font-lock-string-face)
4162 (cperl-commentify b1 (point) nil)
4163 (cperl-put-do-not-fontify b1 (point) t))))
4164 ;; We do not search to max, since we may be called from
4165 ;; some hook of fontification, and max is random
4166 (re-search-forward "^[.;]$" stop-point 'toend))
4167 (beginning-of-line)
4168 (if (looking-at "^\\.$") ; ";" is not supported yet
4169 (progn
4170 ;; Highlight the ending delimiter
4171 (cperl-postpone-fontification (point) (+ (point) 2)
4172 'face font-lock-string-face)
4173 (cperl-commentify (point) (+ (point) 2) nil)
4174 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4175 (message "End of format `%s' not found." name)
4176 (or (car err-l) (setcar err-l b)))
4177 (forward-line)
4178 (if (> (point) max)
4179 (setq tmpend tb))
4180 (put-text-property b (point) 'syntax-type 'format))
4181 ;; qq-like String or Regexp:
4182 ((or (match-beginning 10) (match-beginning 11))
4183 ;; 1+6+2=9 extra () before this:
4184 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4185 ;; "\\|"
4186 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4187 (setq b1 (if (match-beginning 10) 10 11)
4188 argument (buffer-substring
4189 (match-beginning b1) (match-end b1))
4190 b (point) ; end of qq etc
4192 c (char-after (match-beginning b1))
4193 bb (char-after (1- (match-beginning b1))) ; tmp holder
4194 ;; bb == "Not a stringy"
4195 bb (if (eq b1 10) ; user variables/whatever
4196 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4197 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4198 ((eq bb ?\:) ; $opt::s
4199 (eq (char-after
4200 (- (match-beginning b1) 2))
4201 ?\:))
4202 ((eq bb ?\>) ; $foo->s
4203 (eq (char-after
4204 (- (match-beginning b1) 2))
4205 ?\-))
4206 ((eq bb ?\&)
4207 (not (eq (char-after ; &&m/blah/
4208 (- (match-beginning b1) 2))
4209 ?\&)))
4210 (t t)))
4211 ;; <file> or <$file>
4212 (and (eq c ?\<)
4213 ;; Do not stringify <FH>, <$fh> :
4214 (save-match-data
4215 (looking-at
4216 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4217 tb (match-beginning 0))
4218 (goto-char (match-beginning b1))
4219 (cperl-backward-to-noncomment (point-min))
4220 (or bb
4221 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4222 (setq argument ""
4223 b1 nil
4224 bb ; Not a regexp?
4225 (not
4226 ;; What is below: regexp-p?
4227 (and
4228 (or (memq (preceding-char)
4229 (append (if (memq c '(?\? ?\<))
4230 ;; $a++ ? 1 : 2
4231 "~{(=|&*!,;:["
4232 "~{(=|&+-*!,;:[") nil))
4233 (and (eq (preceding-char) ?\})
4234 (cperl-after-block-p (point-min)))
4235 (and (eq (char-syntax (preceding-char)) ?w)
4236 (progn
4237 (forward-sexp -1)
4238 ;; After these keywords `/' starts a RE. One should add all the
4239 ;; functions/builtins which expect an argument, but ...
4240 (if (eq (preceding-char) ?-)
4241 ;; -d ?foo? is a RE
4242 (looking-at "[a-zA-Z]\\>")
4243 (and
4244 (not (memq (preceding-char)
4245 '(?$ ?@ ?& ?%)))
4246 (looking-at
4247 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4248 (and (eq (preceding-char) ?.)
4249 (eq (char-after (- (point) 2)) ?.))
4250 (bobp))
4251 ;; m|blah| ? foo : bar;
4252 (not
4253 (and (eq c ?\?)
4254 cperl-use-syntax-table-text-property
4255 (not (bobp))
4256 (progn
4257 (forward-char -1)
4258 (looking-at "\\s|"))))))
4259 b (1- b))
4260 ;; s y tr m
4261 ;; Check for $a -> y
4262 (setq b1 (preceding-char)
4263 go (point))
4264 (if (and (eq b1 ?>)
4265 (eq (char-after (- go 2)) ?-))
4266 ;; Not a regexp
4267 (setq bb t))))
4268 (or bb
4269 (progn
4270 (goto-char b)
4271 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4272 (goto-char (match-end 0))
4273 (skip-chars-forward " \t\n\f"))
4274 (cond ((and (eq (following-char) ?\})
4275 (eq b1 ?\{))
4276 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4277 (goto-char (1- go))
4278 (skip-chars-backward " \t\n\f")
4279 (if (memq (preceding-char) (append "$@%&*" nil))
4280 (setq bb t) ; @{y}
4281 (condition-case nil
4282 (forward-sexp -1)
4283 (error nil)))
4284 (if (or bb
4285 (looking-at ; $foo -> {s}
4286 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4287 (and ; $foo[12] -> {s}
4288 (memq (following-char) '(?\{ ?\[))
4289 (progn
4290 (forward-sexp 1)
4291 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4292 (setq bb t)
4293 (goto-char b)))
4294 ((and (eq (following-char) ?=)
4295 (eq (char-after (1+ (point))) ?\>))
4296 ;; Check for { foo => 1, s => 2 }
4297 ;; Apparently s=> is never a substitution...
4298 (setq bb t))
4299 ((and (eq (following-char) ?:)
4300 (eq b1 ?\{) ; Check for $ { s::bar }
4301 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4302 (progn
4303 (goto-char (1- go))
4304 (skip-chars-backward " \t\n\f")
4305 (memq (preceding-char)
4306 (append "$@%&*" nil))))
4307 (setq bb t))
4308 ((eobp)
4309 (setq bb t)))))
4310 (if bb
4311 (goto-char i)
4312 ;; Skip whitespace and comments...
4313 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4314 (goto-char (match-end 0))
4315 (skip-chars-forward " \t\n\f"))
4316 (if (> (point) b)
4317 (put-text-property b (point) 'syntax-type 'prestring))
4318 ;; qtag means two-arg matcher, may be reset to
4319 ;; 2 or 3 later if some special quoting is needed.
4320 ;; e1 means matching-char matcher.
4321 (setq b (point) ; before the first delimiter
4322 ;; has 2 args
4323 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4324 ;; We do not search to max, since we may be called from
4325 ;; some hook of fontification, and max is random
4326 i (cperl-forward-re stop-point end
4328 st-l err-l argument)
4329 ;; If `go', then it is considered as 1-arg, `b1' is nil
4330 ;; as in s/foo//x; the point is before final "slash"
4331 b1 (nth 1 i) ; start of the second part
4332 tag (nth 2 i) ; ender-char, true if second part
4333 ; is with matching chars []
4334 go (nth 4 i) ; There is a 1-char part after the end
4335 i (car i) ; intermediate point
4336 e1 (point) ; end
4337 ;; Before end of the second part if non-matching: ///
4338 tail (if (and i (not tag))
4339 (1- e1))
4340 e (if i i e1) ; end of the first part
4341 qtag nil ; need to preserve backslashitis
4342 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4343 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4344 ;; Commenting \\ is dangerous, what about ( ?
4345 (and i tail
4346 (eq (char-after i) ?\\)
4347 (setq qtag t))
4348 (and (if go (looking-at ".\\sw*x")
4349 (looking-at "\\sw*x")) ; qr//x
4350 (setq is-x-REx t))
4351 (and (if go (looking-at ".\\sw*o")
4352 (looking-at "\\sw*o")) ; //o
4353 (setq is-o-REx t))
4354 (if (null i)
4355 ;; Considered as 1arg form
4356 (progn
4357 (cperl-commentify b (point) t)
4358 (put-text-property b (point) 'syntax-type 'string)
4359 (if (or is-x-REx
4360 ;; ignore other text properties:
4361 (string-match "^qw$" argument))
4362 (put-text-property b (point) 'indentable t))
4363 (and go
4364 (setq e1 (cperl-1+ e1))
4365 (or (eobp)
4366 (forward-char 1))))
4367 (cperl-commentify b i t)
4368 (if (looking-at "\\sw*e") ; s///e
4369 (progn
4370 ;; Cache the syntax info...
4371 (setq cperl-syntax-state (cons state-point state))
4372 (and
4373 ;; silent:
4374 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4375 ;; Error
4376 (goto-char (1+ max)))
4377 (if (and tag (eq (preceding-char) ?\>))
4378 (progn
4379 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4380 (cperl-modify-syntax-type i cperl-st-bra)))
4381 (put-text-property b i 'syntax-type 'string)
4382 (put-text-property i (point) 'syntax-type 'multiline)
4383 (if is-x-REx
4384 (put-text-property b i 'indentable t)))
4385 (cperl-commentify b1 (point) t)
4386 (put-text-property b (point) 'syntax-type 'string)
4387 (if is-x-REx
4388 (put-text-property b i 'indentable t))
4389 (if qtag
4390 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4391 (setq tail nil)))
4392 ;; Now: tail: if the second part is non-matching without ///e
4393 (if (eq (char-syntax (following-char)) ?w)
4394 (progn
4395 (forward-word 1) ; skip modifiers s///s
4396 (if tail (cperl-commentify tail (point) t))
4397 (cperl-postpone-fontification
4398 e1 (point) 'face my-cperl-REx-modifiers-face)))
4399 ;; Check whether it is m// which means "previous match"
4400 ;; and highlight differently
4401 (setq is-REx
4402 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4403 (or (not (= (length argument) 0))
4404 (not (eq c ?\<)))))
4405 (if (and is-REx
4406 (eq e (+ 2 b))
4407 ;; split // *is* using zero-pattern
4408 (save-excursion
4409 (condition-case nil
4410 (progn
4411 (goto-char tb)
4412 (forward-sexp -1)
4413 (not (looking-at "split\\>")))
4414 (error t))))
4415 (cperl-postpone-fontification
4416 b e 'face font-lock-warning-face)
4417 (if (or i2 ; Has 2 args
4418 (and cperl-fontify-m-as-s
4420 (string-match "^\\(m\\|qr\\)$" argument)
4421 (and (eq 0 (length argument))
4422 (not (eq ?\< (char-after b)))))))
4423 (progn
4424 (cperl-postpone-fontification
4425 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4426 (cperl-postpone-fontification
4427 (1- e) e 'face my-cperl-delimiters-face)))
4428 (if (and is-REx cperl-regexp-scan)
4429 ;; Process RExen: embedded comments, charclasses and ]
4430 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4431 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4432 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4433 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4434 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4435 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4436 (save-excursion
4437 (goto-char (1+ b))
4438 ;; First
4439 (cperl-look-at-leading-count is-x-REx e)
4440 (setq hairy-RE
4441 (concat
4442 (if is-x-REx
4443 (if (eq (char-after b) ?\#)
4444 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4445 "\\((\\?#\\)\\|\\(#\\)")
4446 ;; keep the same count: add a fake group
4447 (if (eq (char-after b) ?\#)
4448 "\\((\\?\\\\#\\)\\(\\)"
4449 "\\((\\?#\\)\\(\\)"))
4450 "\\|"
4451 "\\(\\[\\)" ; 3=[
4452 "\\|"
4453 "\\(]\\)" ; 4=]
4454 "\\|"
4455 ;; XXXX Will not be able to use it in s)))
4456 (if (eq (char-after b) ?\) )
4457 "\\())))\\)" ; Will never match
4458 (if (eq (char-after b) ?? )
4459 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4460 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4461 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4462 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4463 "\\(" ;; XXXX 1-char variables, exc. |()\s
4464 "[$@]"
4465 "\\("
4466 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4467 "\\|"
4468 "{[^{}]*}" ; only one-level allowed
4469 "\\|"
4470 "[^{(|) \t\r\n\f]"
4471 "\\)"
4472 "\\(" ;;8,9:code part of array/hash elt
4473 "\\(" "->" "\\)?"
4474 "\\[[^][]*\\]"
4475 "\\|"
4476 "{[^{}]*}"
4477 "\\)*"
4478 ;; XXXX: what if u is delim?
4479 "\\|"
4480 "[)^|$.*?+]"
4481 "\\|"
4482 "{[0-9]+}"
4483 "\\|"
4484 "{[0-9]+,[0-9]*}"
4485 "\\|"
4486 "\\\\[luLUEQbBAzZG]"
4487 "\\|"
4488 "(" ; Group opener
4489 "\\(" ; 10 group opener follower
4490 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4491 "\\|"
4492 "\\?[:=!>?{]" ; "?" something
4493 "\\|"
4494 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4495 "\\|"
4496 "\\?([0-9]+)" ; (?(1)foo|bar)
4497 "\\|"
4498 "\\?<[=!]"
4499 ;;;"\\|"
4500 ;;; "\\?"
4501 "\\)?"
4502 "\\)"
4503 "\\|"
4504 "\\\\\\(.\\)" ; 12=\SYMBOL
4506 (while
4507 (and (< (point) (1- e))
4508 (re-search-forward hairy-RE (1- e) 'to-end))
4509 (goto-char (match-beginning 0))
4510 (setq REx-subgr-start (point)
4511 was-subgr (following-char))
4512 (cond
4513 ((match-beginning 6) ; 0-length builtins, groups
4514 (goto-char (match-end 0))
4515 (if (match-beginning 11)
4516 (goto-char (match-beginning 11)))
4517 (if (>= (point) e)
4518 (goto-char (1- e)))
4519 (cperl-postpone-fontification
4520 (match-beginning 0) (point)
4521 'face
4522 (cond
4523 ((eq was-subgr ?\) )
4524 (condition-case nil
4525 (save-excursion
4526 (forward-sexp -1)
4527 (if (> (point) b)
4528 (if (if (eq (char-after b) ?? )
4529 (looking-at "(\\\\\\?")
4530 (eq (char-after (1+ (point))) ?\?))
4531 my-cperl-REx-0length-face
4532 my-cperl-REx-ctl-face)
4533 font-lock-warning-face))
4534 (error font-lock-warning-face)))
4535 ((eq was-subgr ?\| )
4536 my-cperl-REx-ctl-face)
4537 ((eq was-subgr ?\$ )
4538 (if (> (point) (1+ REx-subgr-start))
4539 (progn
4540 (put-text-property
4541 (match-beginning 0) (point)
4542 'REx-interpolated
4543 (if is-o-REx 0
4544 (if (and (eq (match-beginning 0)
4545 (1+ b))
4546 (eq (point)
4547 (1- e))) 1 t)))
4548 font-lock-variable-name-face)
4549 my-cperl-REx-spec-char-face))
4550 ((memq was-subgr (append "^." nil) )
4551 my-cperl-REx-spec-char-face)
4552 ((eq was-subgr ?\( )
4553 (if (not (match-beginning 10))
4554 my-cperl-REx-ctl-face
4555 my-cperl-REx-0length-face))
4556 (t my-cperl-REx-0length-face)))
4557 (if (and (memq was-subgr (append "(|" nil))
4558 (not (string-match "(\\?[-imsx]+)"
4559 (match-string 0))))
4560 (cperl-look-at-leading-count is-x-REx e))
4561 (setq was-subgr nil)) ; We do stuff here
4562 ((match-beginning 12) ; \SYMBOL
4563 (forward-char 2)
4564 (if (>= (point) e)
4565 (goto-char (1- e))
4566 ;; How many chars to not highlight:
4567 ;; 0-len special-alnums in other branch =>
4568 ;; Generic: \non-alnum (1), \alnum (1+face)
4569 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4570 (setq REx-subgr-start (point)
4571 qtag (preceding-char))
4572 (cperl-postpone-fontification
4573 (- (point) 2) (- (point) 1) 'face
4574 (if (memq qtag
4575 (append "ghijkmoqvFHIJKMORTVY" nil))
4576 font-lock-warning-face
4577 my-cperl-REx-0length-face))
4578 (if (and (eq (char-after b) qtag)
4579 (memq qtag (append ".])^$|*?+" nil)))
4580 (progn
4581 (if (and cperl-use-syntax-table-text-property
4582 (eq qtag ?\) ))
4583 (put-text-property
4584 REx-subgr-start (1- (point))
4585 'syntax-table cperl-st-punct))
4586 (cperl-postpone-fontification
4587 (1- (point)) (point) 'face
4588 ; \] can't appear below
4589 (if (memq qtag (append ".]^$" nil))
4590 'my-cperl-REx-spec-char-face
4591 (if (memq qtag (append "*?+" nil))
4592 'my-cperl-REx-0length-face
4593 'my-cperl-REx-ctl-face))))) ; )|
4594 ;; Test for arguments:
4595 (cond
4596 ;; This is not pretty: the 5.8.7 logic:
4597 ;; \0numx -> octal (up to total 3 dig)
4598 ;; \DIGIT -> backref unless \0
4599 ;; \DIGITs -> backref if legal
4600 ;; otherwise up to 3 -> octal
4601 ;; Do not try to distinguish, we guess
4602 ((or (and (memq qtag (append "01234567" nil))
4603 (re-search-forward
4604 "\\=[01234567]?[01234567]?"
4605 (1- e) 'to-end))
4606 (and (memq qtag (append "89" nil))
4607 (re-search-forward
4608 "\\=[0123456789]*" (1- e) 'to-end))
4609 (and (eq qtag ?x)
4610 (re-search-forward
4611 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4612 (1- e) 'to-end))
4613 (and (memq qtag (append "pPN" nil))
4614 (re-search-forward "\\={[^{}]+}\\|."
4615 (1- e) 'to-end))
4616 (eq (char-syntax qtag) ?w))
4617 (cperl-postpone-fontification
4618 (1- REx-subgr-start) (point)
4619 'face my-cperl-REx-length1-face))))
4620 (setq was-subgr nil)) ; We do stuff here
4621 ((match-beginning 3) ; [charclass]
4622 (forward-char 1)
4623 (if (eq (char-after b) ?^ )
4624 (and (eq (following-char) ?\\ )
4625 (eq (char-after (cperl-1+ (point)))
4626 ?^ )
4627 (forward-char 2))
4628 (and (eq (following-char) ?^ )
4629 (forward-char 1)))
4630 (setq argument b ; continue?
4631 tag nil ; list of POSIX classes
4632 qtag (point))
4633 (if (eq (char-after b) ?\] )
4634 (and (eq (following-char) ?\\ )
4635 (eq (char-after (cperl-1+ (point)))
4636 ?\] )
4637 (setq qtag (1+ qtag))
4638 (forward-char 2))
4639 (and (eq (following-char) ?\] )
4640 (forward-char 1)))
4641 ;; Apparently, I can't put \] into a charclass
4642 ;; in m]]: m][\\\]\]] produces [\\]]
4643 ;;; POSIX? [:word:] [:^word:] only inside []
4644 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4645 (while
4646 (and argument
4647 (re-search-forward
4648 (if (eq (char-after b) ?\] )
4649 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4650 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4651 (1- e) 'toend))
4652 ;; Is this ] an end of POSIX class?
4653 (if (save-excursion
4654 (and
4655 (search-backward "[" argument t)
4656 (< REx-subgr-start (point))
4657 (not
4658 (and ; Should work with delim = \
4659 (eq (preceding-char) ?\\ )
4660 (= (% (skip-chars-backward
4661 "\\\\") 2) 0)))
4662 (looking-at
4663 (cond
4664 ((eq (char-after b) ?\] )
4665 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4666 ((eq (char-after b) ?\: )
4667 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4668 ((eq (char-after b) ?^ )
4669 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4670 ((eq (char-syntax (char-after b))
4672 (concat
4673 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4674 (char-to-string (char-after b))
4675 "\\|\\sw\\)+:\]"))
4676 (t "\\\\*\\[:\\^?\\sw*:]")))
4677 (setq argument (point))))
4678 (setq tag (cons (cons argument (point))
4679 tag)
4680 argument (point)) ; continue
4681 (setq argument nil)))
4682 (and argument
4683 (message "Couldn't find end of charclass in a REx, pos=%s"
4684 REx-subgr-start))
4685 (if (and cperl-use-syntax-table-text-property
4686 (> (- (point) 2) REx-subgr-start))
4687 (put-text-property
4688 (1+ REx-subgr-start) (1- (point))
4689 'syntax-table cperl-st-punct))
4690 (cperl-postpone-fontification
4691 REx-subgr-start qtag
4692 'face my-cperl-REx-spec-char-face)
4693 (cperl-postpone-fontification
4694 (1- (point)) (point) 'face
4695 my-cperl-REx-spec-char-face)
4696 (if (eq (char-after b) ?\] )
4697 (cperl-postpone-fontification
4698 (- (point) 2) (1- (point))
4699 'face my-cperl-REx-0length-face))
4700 (while tag
4701 (cperl-postpone-fontification
4702 (car (car tag)) (cdr (car tag))
4703 'face my-cperl-REx-length1-face)
4704 (setq tag (cdr tag)))
4705 (setq was-subgr nil)) ; did facing already
4706 ;; Now rare stuff:
4707 ((and (match-beginning 2) ; #-comment
4708 (/= (match-beginning 2) (match-end 2)))
4709 (beginning-of-line 2)
4710 (if (> (point) e)
4711 (goto-char (1- e))))
4712 ((match-beginning 4) ; character "]"
4713 (setq was-subgr nil) ; We do stuff here
4714 (goto-char (match-end 0))
4715 (if cperl-use-syntax-table-text-property
4716 (put-text-property
4717 (1- (point)) (point)
4718 'syntax-table cperl-st-punct))
4719 (cperl-postpone-fontification
4720 (1- (point)) (point)
4721 'face font-lock-warning-face))
4722 ((match-beginning 5) ; before (?{}) (??{})
4723 (setq tag (match-end 0))
4724 (if (or (setq qtag
4725 (cperl-forward-group-in-re st-l))
4726 (and (>= (point) e)
4727 (setq qtag "no matching `)' found"))
4728 (and (not (eq (char-after (- (point) 2))
4729 ?\} ))
4730 (setq qtag "Can't find })")))
4731 (progn
4732 (goto-char (1- e))
4733 (message qtag))
4734 (cperl-postpone-fontification
4735 (1- tag) (1- (point))
4736 'face font-lock-variable-name-face)
4737 (cperl-postpone-fontification
4738 REx-subgr-start (1- tag)
4739 'face my-cperl-REx-spec-char-face)
4740 (cperl-postpone-fontification
4741 (1- (point)) (point)
4742 'face my-cperl-REx-spec-char-face)
4743 (if cperl-use-syntax-table-text-property
4744 (progn
4745 (put-text-property
4746 (- (point) 2) (1- (point))
4747 'syntax-table cperl-st-cfence)
4748 (put-text-property
4749 (+ REx-subgr-start 2)
4750 (+ REx-subgr-start 3)
4751 'syntax-table cperl-st-cfence))))
4752 (setq was-subgr nil))
4753 (t ; (?#)-comment
4754 ;; Inside "(" and "\" arn't special in any way
4755 ;; Works also if the outside delimiters are ().
4756 (or;;(if (eq (char-after b) ?\) )
4757 ;;(re-search-forward
4758 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4759 ;; (1- e) 'toend)
4760 (search-forward ")" (1- e) 'toend)
4762 (message
4763 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4764 REx-subgr-start))))
4765 (if (>= (point) e)
4766 (goto-char (1- e)))
4767 (cond
4768 (was-subgr
4769 (setq REx-subgr-end (point))
4770 (cperl-commentify
4771 REx-subgr-start REx-subgr-end nil)
4772 (cperl-postpone-fontification
4773 REx-subgr-start REx-subgr-end
4774 'face font-lock-comment-face))))))
4775 (if (and is-REx is-x-REx)
4776 (put-text-property (1+ b) (1- e)
4777 'syntax-subtype 'x-REx)))
4778 (if i2
4779 (progn
4780 (cperl-postpone-fontification
4781 (1- e1) e1 'face my-cperl-delimiters-face)
4782 (if (assoc (char-after b) cperl-starters)
4783 (progn
4784 (cperl-postpone-fontification
4785 b1 (1+ b1) 'face my-cperl-delimiters-face)
4786 (put-text-property b1 (1+ b1)
4787 'REx-part2 t)))))
4788 (if (> (point) max)
4789 (setq tmpend tb))))
4790 ((match-beginning 17) ; sub with prototype or attribute
4791 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4792 ;;"\\<sub\\>\\(" ;12
4793 ;; cperl-white-and-comment-rex ;13
4794 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4795 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4796 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4797 (setq b1 (match-beginning 14) e1 (match-end 14))
4798 (if (memq (char-after (1- b))
4799 '(?\$ ?\@ ?\% ?\& ?\*))
4801 (goto-char b)
4802 (if (eq (char-after (match-beginning 17)) ?\( )
4803 (progn
4804 (cperl-commentify ; Prototypes; mark as string
4805 (match-beginning 17) (match-end 17) t)
4806 (goto-char (match-end 0))
4807 ;; Now look for attributes after prototype:
4808 (forward-comment (buffer-size))
4809 (and (looking-at ":[^:]")
4810 (cperl-find-sub-attrs st-l b1 e1 b)))
4811 ;; treat attributes without prototype
4812 (goto-char (match-beginning 17))
4813 (cperl-find-sub-attrs st-l b1 e1 b))))
4814 ;; 1+6+2+1+1+6+1=18 extra () before this:
4815 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4816 ((match-beginning 19) ; old $abc'efg syntax
4817 (setq bb (match-end 0))
4818 ;;;(if (nth 3 state) nil ; in string
4819 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4820 (goto-char bb))
4821 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4822 ;; "__\\(END\\|DATA\\)__"
4823 ((match-beginning 20) ; __END__, __DATA__
4824 (setq bb (match-end 0))
4825 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4826 (cperl-commentify b bb nil)
4827 (setq end t))
4828 ;; "\\\\\\(['`\"($]\\)"
4829 ((match-beginning 21)
4830 ;; Trailing backslash; make non-quoting outside string/comment
4831 (setq bb (match-end 0))
4832 (goto-char b)
4833 (skip-chars-backward "\\\\")
4834 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4835 (cperl-modify-syntax-type b cperl-st-punct)
4836 (goto-char bb))
4837 (t (error "Error in regexp of the sniffer")))
4838 (if (> (point) stop-point)
4839 (progn
4840 (if end
4841 (message "Garbage after __END__/__DATA__ ignored")
4842 (message "Unbalanced syntax found while scanning")
4843 (or (car err-l) (setcar err-l b)))
4844 (goto-char stop-point))))
4845 (setq cperl-syntax-state (cons state-point state)
4846 ;; Do not mark syntax as done past tmpend???
4847 cperl-syntax-done-to (or tmpend (max (point) max)))
4848 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4850 (if (car err-l) (goto-char (car err-l))
4851 (or non-inter
4852 (message "Scanning for \"hard\" Perl constructions... done"))))
4853 (and (buffer-modified-p)
4854 (not modified)
4855 (set-buffer-modified-p nil))
4856 ;; I do not understand what this is doing here. It breaks font-locking
4857 ;; because it resets the syntax-table from font-lock-syntax-table to
4858 ;; cperl-mode-syntax-table.
4859 ;; (set-syntax-table cperl-mode-syntax-table)
4861 (list (car err-l) overshoot)))
4863 (defun cperl-find-pods-heres-region (min max)
4864 (interactive "r")
4865 (cperl-find-pods-heres min max))
4867 (defun cperl-backward-to-noncomment (lim)
4868 ;; Stops at lim or after non-whitespace that is not in comment
4869 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4870 (let (stop p pr)
4871 (while (and (not stop) (> (point) (or lim (point-min))))
4872 (skip-chars-backward " \t\n\f" lim)
4873 (setq p (point))
4874 (beginning-of-line)
4875 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4876 '(pod here-doc here-doc-delim))
4877 (cperl-unwind-to-safe nil)
4878 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4879 (not (memq pr '(string prestring))))
4880 (progn (cperl-to-comment-or-eol) (bolp))
4881 (progn
4882 (skip-chars-backward " \t")
4883 (if (< p (point)) (goto-char p))
4884 (setq stop t)))))))
4886 ;; Used only in `cperl-calculate-indent'...
4887 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4888 ;; Positions is before ?\{. Checks whether it starts a block.
4889 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4890 (cperl-backward-to-noncomment (point-min))
4891 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4892 ; Label may be mixed up with `$blah :'
4893 (save-excursion (cperl-after-label))
4894 (get-text-property (cperl-1- (point)) 'attrib-group)
4895 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4896 (progn
4897 (backward-sexp)
4898 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4899 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4900 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4901 ;; sub bless::foo {}
4902 (progn
4903 (cperl-backward-to-noncomment (point-min))
4904 (and (eq (preceding-char) ?b)
4905 (progn
4906 (forward-sexp -1)
4907 (looking-at "sub[ \t\n\f#]")))))))))
4909 ;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4910 ;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4911 ;;; may be a part of an in-statement construct, such as
4912 ;;; ${something()}, print {FH} $data.
4913 ;;; Moreover, one takes positive approach (looks for else,grep etc)
4914 ;;; another negative (looks for bless,tr etc)
4915 (defun cperl-after-block-p (lim &optional pre-block)
4916 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4917 Would not look before LIM. Assumes that LIM is a good place to begin a
4918 statement. The kind of block we treat here is one after which a new
4919 statement would start; thus the block in ${func()} does not count."
4920 (save-excursion
4921 (condition-case nil
4922 (progn
4923 (or pre-block (forward-sexp -1))
4924 (cperl-backward-to-noncomment lim)
4925 (or (eq (point) lim)
4926 ;; if () {} // sub f () {} // sub f :a(') {}
4927 (eq (preceding-char) ?\) )
4928 ;; label: {}
4929 (save-excursion (cperl-after-label))
4930 ;; sub :attr {}
4931 (get-text-property (cperl-1- (point)) 'attrib-group)
4932 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4933 (save-excursion
4934 (forward-sexp -1)
4935 ;; else {} but not else::func {}
4936 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4937 (not (looking-at "\\(\\sw\\|_\\)+::")))
4938 ;; sub f {}
4939 (progn
4940 (cperl-backward-to-noncomment lim)
4941 (and (eq (preceding-char) ?b)
4942 (progn
4943 (forward-sexp -1)
4944 (looking-at "sub[ \t\n\f#]"))))))
4945 ;; What preceeds is not word... XXXX Last statement in sub???
4946 (cperl-after-expr-p lim))))
4947 (error nil))))
4949 (defun cperl-after-expr-p (&optional lim chars test)
4950 "Return true if the position is good for start of expression.
4951 TEST is the expression to evaluate at the found position. If absent,
4952 CHARS is a string that contains good characters to have before us (however,
4953 `}' is treated \"smartly\" if it is not in the list)."
4954 (let ((lim (or lim (point-min)))
4955 stop p pr)
4956 (cperl-update-syntaxification (point) (point))
4957 (save-excursion
4958 (while (and (not stop) (> (point) lim))
4959 (skip-chars-backward " \t\n\f" lim)
4960 (setq p (point))
4961 (beginning-of-line)
4962 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4963 ;; '(pod here-doc here-doc-delim))
4964 (if (get-text-property (point) 'here-doc-group)
4965 (progn
4966 (goto-char
4967 (cperl-beginning-of-property (point) 'here-doc-group))
4968 (beginning-of-line 0)))
4969 (if (get-text-property (point) 'in-pod)
4970 (progn
4971 (goto-char
4972 (cperl-beginning-of-property (point) 'in-pod))
4973 (beginning-of-line 0)))
4974 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4975 ;; Else: last iteration, or a label
4976 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4977 (skip-chars-backward " \t")
4978 (if (< p (point)) (goto-char p))
4979 (setq p (point))
4980 (if (and (eq (preceding-char) ?:)
4981 (progn
4982 (forward-char -1)
4983 (skip-chars-backward " \t\n\f" lim)
4984 (memq (char-syntax (preceding-char)) '(?w ?_))))
4985 (forward-sexp -1) ; Possibly label. Skip it
4986 (goto-char p)
4987 (setq stop t))))
4988 (or (bobp) ; ???? Needed
4989 (eq (point) lim)
4990 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4991 (progn
4992 (if test (eval test)
4993 (or (memq (preceding-char) (append (or chars "{;") nil))
4994 (and (eq (preceding-char) ?\})
4995 (cperl-after-block-p lim))
4996 (and (eq (following-char) ?.) ; in format: see comment above
4997 (eq (get-text-property (point) 'syntax-type)
4998 'format)))))))))
5000 (defun cperl-backward-to-start-of-expr (&optional lim)
5001 (condition-case nil
5002 (progn
5003 (while (and (or (not lim)
5004 (> (point) lim))
5005 (not (cperl-after-expr-p lim)))
5006 (forward-sexp -1)
5007 ;; May be after $, @, $# etc of a variable
5008 (skip-chars-backward "$@%#")))
5009 (error nil)))
5011 (defun cperl-at-end-of-expr (&optional lim)
5012 ;; Since the SEXP approach below is very fragile, do some overengineering
5013 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
5014 (condition-case nil
5015 (save-excursion
5016 ;; If nothing interesting after, does as (forward-sexp -1);
5017 ;; otherwise fails, or ends at a start of following sexp.
5018 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
5019 ;; may be stuck after @ or $; just put some stupid workaround now:
5020 (let ((p (point)))
5021 (forward-sexp 1)
5022 (forward-sexp -1)
5023 (while (memq (preceding-char) (append "%&@$*" nil))
5024 (forward-char -1))
5025 (or (< (point) p)
5026 (cperl-after-expr-p lim))))
5027 (error t))))
5029 (defun cperl-forward-to-end-of-expr (&optional lim)
5030 (let ((p (point))))
5031 (condition-case nil
5032 (progn
5033 (while (and (< (point) (or lim (point-max)))
5034 (not (cperl-at-end-of-expr)))
5035 (forward-sexp 1)))
5036 (error nil)))
5038 (defun cperl-backward-to-start-of-continued-exp (lim)
5039 (if (memq (preceding-char) (append ")]}\"'`" nil))
5040 (forward-sexp -1))
5041 (beginning-of-line)
5042 (if (<= (point) lim)
5043 (goto-char (1+ lim)))
5044 (skip-chars-forward " \t"))
5046 (defun cperl-after-block-and-statement-beg (lim)
5047 ;; We assume that we are after ?\}
5048 (and
5049 (cperl-after-block-p lim)
5050 (save-excursion
5051 (forward-sexp -1)
5052 (cperl-backward-to-noncomment (point-min))
5053 (or (bobp)
5054 (eq (point) lim)
5055 (not (= (char-syntax (preceding-char)) ?w))
5056 (progn
5057 (forward-sexp -1)
5058 (not
5059 (looking-at
5060 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
5063 (defun cperl-indent-exp ()
5064 "Simple variant of indentation of continued-sexp.
5066 Will not indent comment if it starts at `comment-indent' or looks like
5067 continuation of the comment on the previous line.
5069 If `cperl-indent-region-fix-constructs', will improve spacing on
5070 conditional/loop constructs."
5071 (interactive)
5072 (save-excursion
5073 (let ((tmp-end (progn (end-of-line) (point))) top done)
5074 (save-excursion
5075 (beginning-of-line)
5076 (while (null done)
5077 (setq top (point))
5078 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5079 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
5080 (setq top (point))) ; Get the outermost parenths in line
5081 (goto-char top)
5082 (while (< (point) tmp-end)
5083 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5084 (or (eolp) (forward-sexp 1)))
5085 (if (> (point) tmp-end) ; Yes, there an unfinished block
5087 (if (eq ?\) (preceding-char))
5088 (progn ;; Plan B: find by REGEXP block followup this line
5089 (setq top (point))
5090 (condition-case nil
5091 (progn
5092 (forward-sexp -2)
5093 (if (eq (following-char) ?$ ) ; for my $var (list)
5094 (progn
5095 (forward-sexp -1)
5096 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5097 (forward-sexp -1))))
5098 (if (looking-at
5099 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5100 "\\|for\\(each\\)?\\>\\(\\("
5101 cperl-maybe-white-and-comment-rex
5102 "\\(my\\|local\\|our\\)\\)?"
5103 cperl-maybe-white-and-comment-rex
5104 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5105 (progn
5106 (goto-char top)
5107 (forward-sexp 1)
5108 (setq top (point)))))
5109 (error (setq done t)))
5110 (goto-char top))
5111 (if (looking-at ; Try Plan C: continuation block
5112 (concat cperl-maybe-white-and-comment-rex
5113 "\\<\\(else\\|elsif\|continue\\)\\>"))
5114 (progn
5115 (goto-char (match-end 0))
5116 (save-excursion
5117 (end-of-line)
5118 (setq tmp-end (point))))
5119 (setq done t))))
5120 (save-excursion
5121 (end-of-line)
5122 (setq tmp-end (point))))
5123 (goto-char tmp-end)
5124 (setq tmp-end (point-marker)))
5125 (if cperl-indent-region-fix-constructs
5126 (cperl-fix-line-spacing tmp-end))
5127 (cperl-indent-region (point) tmp-end))))
5129 (defun cperl-fix-line-spacing (&optional end parse-data)
5130 "Improve whitespace in a conditional/loop construct.
5131 Returns some position at the last line."
5132 (interactive)
5133 (or end
5134 (setq end (point-max)))
5135 (let ((ee (save-excursion (end-of-line) (point)))
5136 (cperl-indent-region-fix-constructs
5137 (or cperl-indent-region-fix-constructs 1))
5138 p pp ml have-brace ret)
5139 (save-excursion
5140 (beginning-of-line)
5141 (setq ret (point))
5142 ;; }? continue
5143 ;; blah; }
5144 (if (not
5145 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5146 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5147 nil ; Do not need to do anything
5148 ;; Looking at:
5149 ;; }
5150 ;; else
5151 (if cperl-merge-trailing-else
5152 (if (looking-at
5153 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5154 (progn
5155 (search-forward "}")
5156 (setq p (point))
5157 (skip-chars-forward " \t\n")
5158 (delete-region p (point))
5159 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5160 (beginning-of-line)))
5161 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5162 (save-excursion
5163 (search-forward "}")
5164 (delete-horizontal-space)
5165 (insert "\n")
5166 (setq ret (point))
5167 (if (cperl-indent-line parse-data)
5168 (progn
5169 (cperl-fix-line-spacing end parse-data)
5170 (setq ret (point)))))))
5171 ;; Looking at:
5172 ;; } else
5173 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5174 (progn
5175 (search-forward "}")
5176 (delete-horizontal-space)
5177 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5178 (beginning-of-line)))
5179 ;; Looking at:
5180 ;; else {
5181 (if (looking-at
5182 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5183 (progn
5184 (forward-word 1)
5185 (delete-horizontal-space)
5186 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5187 (beginning-of-line)))
5188 ;; Looking at:
5189 ;; foreach my $var
5190 (if (looking-at
5191 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5192 (progn
5193 (forward-word 2)
5194 (delete-horizontal-space)
5195 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5196 (beginning-of-line)))
5197 ;; Looking at:
5198 ;; foreach my $var (
5199 (if (looking-at
5200 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5201 (progn
5202 (forward-sexp 3)
5203 (delete-horizontal-space)
5204 (insert
5205 (make-string cperl-indent-region-fix-constructs ?\s))
5206 (beginning-of-line)))
5207 ;; Looking at (with or without "}" at start, ending after "({"):
5208 ;; } foreach my $var () OR {
5209 (if (looking-at
5210 "[ \t]*\\(}[ \t]*\\)?\\<\\(\\els\\(e\\|if\\)\\|continue\\|if\\|unless\\|while\\|for\\(each\\)?\\(\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\$[_a-zA-Z0-9]+\\)?\\|until\\)\\>\\([ \t]*(\\|[ \t\n]*{\\)\\|[ \t]*{")
5211 (progn
5212 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5213 (re-search-forward "[({]")
5214 (forward-char -1)
5215 (setq p (point))
5216 (if (eq (following-char) ?\( )
5217 (progn
5218 (forward-sexp 1)
5219 (setq pp (point))) ; past parenth-group
5220 ;; after `else' or nothing
5221 (if ml ; after `else'
5222 (skip-chars-backward " \t\n")
5223 (beginning-of-line))
5224 (setq pp nil))
5225 ;; Now after the sexp before the brace
5226 ;; Multiline expr should be special
5227 (setq ml (and pp (save-excursion (goto-char p)
5228 (search-forward "\n" pp t))))
5229 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5230 (looking-at "[ \t\n]*{"))
5231 (progn
5232 (cond
5233 ((bolp) ; Were before `{', no if/else/etc
5234 nil)
5235 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5236 (delete-horizontal-space)
5237 (if (if ml
5238 cperl-extra-newline-before-brace-multiline
5239 cperl-extra-newline-before-brace)
5240 (progn
5241 (delete-horizontal-space)
5242 (insert "\n")
5243 (setq ret (point))
5244 (if (cperl-indent-line parse-data)
5245 (progn
5246 (cperl-fix-line-spacing end parse-data)
5247 (setq ret (point)))))
5248 (insert
5249 (make-string cperl-indent-region-fix-constructs ?\s))))
5250 ((and (looking-at "[ \t]*\n")
5251 (not (if ml
5252 cperl-extra-newline-before-brace-multiline
5253 cperl-extra-newline-before-brace)))
5254 (setq pp (point))
5255 (skip-chars-forward " \t\n")
5256 (delete-region pp (point))
5257 (insert
5258 (make-string cperl-indent-region-fix-constructs ?\ )))
5259 ((and (looking-at "[\t ]*{")
5260 (if ml cperl-extra-newline-before-brace-multiline
5261 cperl-extra-newline-before-brace))
5262 (delete-horizontal-space)
5263 (insert "\n")
5264 (setq ret (point))
5265 (if (cperl-indent-line parse-data)
5266 (progn
5267 (cperl-fix-line-spacing end parse-data)
5268 (setq ret (point))))))
5269 ;; Now we are before `{'
5270 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5271 (progn
5272 (skip-chars-forward " \t\n")
5273 (setq pp (point))
5274 (forward-sexp 1)
5275 (setq p (point))
5276 (goto-char pp)
5277 (setq ml (search-forward "\n" p t))
5278 (if (or cperl-break-one-line-blocks-when-indent ml)
5279 ;; not good: multi-line BLOCK
5280 (progn
5281 (goto-char (1+ pp))
5282 (delete-horizontal-space)
5283 (insert "\n")
5284 (setq ret (point))
5285 (if (cperl-indent-line parse-data)
5286 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5287 (beginning-of-line)
5288 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5289 ;; Now check whether there is a hanging `}'
5290 ;; Looking at:
5291 ;; } blah
5292 (if (and
5293 cperl-fix-hanging-brace-when-indent
5294 have-brace
5295 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5296 (condition-case nil
5297 (progn
5298 (up-list 1)
5299 (if (and (<= (point) pp)
5300 (eq (preceding-char) ?\} )
5301 (cperl-after-block-and-statement-beg (point-min)))
5303 (goto-char p)
5304 nil))
5305 (error nil)))
5306 (progn
5307 (forward-char -1)
5308 (skip-chars-backward " \t")
5309 (if (bolp)
5310 ;; `}' was the first thing on the line, insert NL *after* it.
5311 (progn
5312 (cperl-indent-line parse-data)
5313 (search-forward "}")
5314 (delete-horizontal-space)
5315 (insert "\n"))
5316 (delete-horizontal-space)
5317 (or (eq (preceding-char) ?\;)
5318 (bolp)
5319 (and (eq (preceding-char) ?\} )
5320 (cperl-after-block-p (point-min)))
5321 (insert ";"))
5322 (insert "\n")
5323 (setq ret (point)))
5324 (if (cperl-indent-line parse-data)
5325 (setq ret (cperl-fix-line-spacing end parse-data)))
5326 (beginning-of-line)))))
5327 ret))
5329 (defvar cperl-update-start) ; Do not need to make them local
5330 (defvar cperl-update-end)
5331 (defun cperl-delay-update-hook (beg end old-len)
5332 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5333 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5335 (defun cperl-indent-region (start end)
5336 "Simple variant of indentation of region in CPerl mode.
5337 Should be slow. Will not indent comment if it starts at `comment-indent'
5338 or looks like continuation of the comment on the previous line.
5339 Indents all the lines whose first character is between START and END
5340 inclusive.
5342 If `cperl-indent-region-fix-constructs', will improve spacing on
5343 conditional/loop constructs."
5344 (interactive "r")
5345 (cperl-update-syntaxification end end)
5346 (save-excursion
5347 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5348 (let ((indent-info (if cperl-emacs-can-parse
5349 (list nil nil nil) ; Cannot use '(), since will modify
5350 nil))
5351 (pm 0)
5352 after-change-functions ; Speed it up!
5353 st comm old-comm-indent new-comm-indent p pp i empty)
5354 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5355 (goto-char start)
5356 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5357 (current-column))
5358 new-comm-indent old-comm-indent)
5359 (goto-char start)
5360 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5361 (or (bolp) (beginning-of-line 2))
5362 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5363 (setq st (point))
5364 (if (or
5365 (setq empty (looking-at "[ \t]*\n"))
5366 (and (setq comm (looking-at "[ \t]*#"))
5367 (or (eq (current-indentation) (or old-comm-indent
5368 comment-column))
5369 (setq old-comm-indent nil))))
5370 (if (and old-comm-indent
5371 (not empty)
5372 (= (current-indentation) old-comm-indent)
5373 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5374 (not (eq (get-text-property (point) 'syntax-table)
5375 cperl-st-cfence)))
5376 (let ((comment-column new-comm-indent))
5377 (indent-for-comment)))
5378 (progn
5379 (setq i (cperl-indent-line indent-info))
5380 (or comm
5381 (not i)
5382 (progn
5383 (if cperl-indent-region-fix-constructs
5384 (goto-char (cperl-fix-line-spacing end indent-info)))
5385 (if (setq old-comm-indent
5386 (and (cperl-to-comment-or-eol)
5387 (not (memq (get-text-property (point)
5388 'syntax-type)
5389 '(pod here-doc)))
5390 (not (eq (get-text-property (point)
5391 'syntax-table)
5392 cperl-st-cfence))
5393 (current-column)))
5394 (progn (indent-for-comment)
5395 (skip-chars-backward " \t")
5396 (skip-chars-backward "#")
5397 (setq new-comm-indent (current-column))))))))
5398 (beginning-of-line 2)))
5399 ;; Now run the update hooks
5400 (and after-change-functions
5401 cperl-update-end
5402 (save-excursion
5403 (goto-char cperl-update-end)
5404 (insert " ")
5405 (delete-char -1)
5406 (goto-char cperl-update-start)
5407 (insert " ")
5408 (delete-char -1))))))
5410 ;; Stolen from lisp-mode with a lot of improvements
5412 (defun cperl-fill-paragraph (&optional justify iteration)
5413 "Like `fill-paragraph', but handle CPerl comments.
5414 If any of the current line is a comment, fill the comment or the
5415 block of it that point is in, preserving the comment's initial
5416 indentation and initial hashes. Behaves usually outside of comment."
5417 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5418 (let (;; Non-nil if the current line contains a comment.
5419 has-comment
5420 fill-paragraph-function ; do not recurse
5421 ;; If has-comment, the appropriate fill-prefix for the comment.
5422 comment-fill-prefix
5423 ;; Line that contains code and comment (or nil)
5424 start
5425 c spaces len dc (comment-column comment-column))
5426 ;; Figure out what kind of comment we are looking at.
5427 (save-excursion
5428 (beginning-of-line)
5429 (cond
5431 ;; A line with nothing but a comment on it?
5432 ((looking-at "[ \t]*#[# \t]*")
5433 (setq has-comment t
5434 comment-fill-prefix (buffer-substring (match-beginning 0)
5435 (match-end 0))))
5437 ;; A line with some code, followed by a comment? Remember that the
5438 ;; semi which starts the comment shouldn't be part of a string or
5439 ;; character.
5440 ((cperl-to-comment-or-eol)
5441 (setq has-comment t)
5442 (looking-at "#+[ \t]*")
5443 (setq start (point) c (current-column)
5444 comment-fill-prefix
5445 (concat (make-string (current-column) ?\s)
5446 (buffer-substring (match-beginning 0) (match-end 0)))
5447 spaces (progn (skip-chars-backward " \t")
5448 (buffer-substring (point) start))
5449 dc (- c (current-column)) len (- start (point))
5450 start (point-marker))
5451 (delete-char len)
5452 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5453 (if (not has-comment)
5454 (fill-paragraph justify) ; Do the usual thing outside of comment
5455 ;; Narrow to include only the comment, and then fill the region.
5456 (save-restriction
5457 (narrow-to-region
5458 ;; Find the first line we should include in the region to fill.
5459 (if start (progn (beginning-of-line) (point))
5460 (save-excursion
5461 (while (and (zerop (forward-line -1))
5462 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5463 ;; We may have gone to far. Go forward again.
5464 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5465 (forward-line 1))
5466 (point)))
5467 ;; Find the beginning of the first line past the region to fill.
5468 (save-excursion
5469 (while (progn (forward-line 1)
5470 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5471 (point)))
5472 ;; Remove existing hashes
5473 (save-excursion
5474 (goto-char (point-min))
5475 (while (progn (forward-line 1) (< (point) (point-max)))
5476 (skip-chars-forward " \t")
5477 (if (looking-at "#+")
5478 (progn
5479 (if (and (eq (point) (match-beginning 0))
5480 (not (eq (point) (match-end 0)))) nil
5481 (error
5482 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5483 (delete-char (- (match-end 0) (match-beginning 0)))))))
5485 ;; Lines with only hashes on them can be paragraph boundaries.
5486 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5487 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5488 (fill-prefix comment-fill-prefix))
5489 (fill-paragraph justify)))
5490 (if (and start)
5491 (progn
5492 (goto-char start)
5493 (if (> dc 0)
5494 (progn (delete-char dc) (insert spaces)))
5495 (if (or (= (current-column) c) iteration) nil
5496 (setq comment-column c)
5497 (indent-for-comment)
5498 ;; Repeat once more, flagging as iteration
5499 (cperl-fill-paragraph justify t))))))
5502 (defun cperl-do-auto-fill ()
5503 ;; Break out if the line is short enough
5504 (if (> (save-excursion
5505 (end-of-line)
5506 (current-column))
5507 fill-column)
5508 (let ((c (save-excursion (beginning-of-line)
5509 (cperl-to-comment-or-eol) (point)))
5510 (s (memq (following-char) '(?\s ?\t))) marker)
5511 (if (>= c (point))
5512 ;; Don't break line inside code: only inside comment.
5514 (setq marker (point-marker))
5515 (fill-paragraph nil)
5516 (goto-char marker)
5517 ;; Is not enough, sometimes marker is a start of line
5518 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5519 (goto-char (match-end 0))))
5520 ;; Following space could have gone:
5521 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5522 (insert " ")
5523 (backward-char 1))
5524 ;; Previous space could have gone:
5525 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5527 (defun cperl-imenu-addback (lst &optional isback name)
5528 ;; We suppose that the lst is a DAG, unless the first element only
5529 ;; loops back, and ISBACK is set. Thus this function cannot be
5530 ;; applied twice without ISBACK set.
5531 (cond ((not cperl-imenu-addback) lst)
5533 (or name
5534 (setq name "+++BACK+++"))
5535 (mapcar (lambda (elt)
5536 (if (and (listp elt) (listp (cdr elt)))
5537 (progn
5538 ;; In the other order it goes up
5539 ;; one level only ;-(
5540 (setcdr elt (cons (cons name lst)
5541 (cdr elt)))
5542 (cperl-imenu-addback (cdr elt) t name))))
5543 (if isback (cdr lst) lst))
5544 lst)))
5546 (defun cperl-imenu--create-perl-index (&optional regexp)
5547 (require 'imenu) ; May be called from TAGS creator
5548 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5549 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5550 (index-meth-alist '()) meth
5551 packages ends-ranges p marker is-proto
5552 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5553 (goto-char (point-min))
5554 (cperl-update-syntaxification (point-max) (point-max))
5555 ;; Search for the function
5556 (progn ;;save-match-data
5557 (while (re-search-forward
5558 (or regexp cperl-imenu--function-name-regexp-perl)
5559 nil t)
5560 ;; 2=package-group, 5=package-name 8=sub-name
5561 (cond
5562 ((and ; Skip some noise if building tags
5563 (match-beginning 5) ; package name
5564 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5565 (not (save-match-data
5566 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5567 nil)
5568 ((and
5569 (or (match-beginning 2)
5570 (match-beginning 8)) ; package or sub
5571 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5572 (null (get-text-property (match-beginning 1) 'syntax-table))
5573 (null (get-text-property (match-beginning 1) 'syntax-type))
5574 (null (get-text-property (match-beginning 1) 'in-pod)))
5575 (setq is-pack (match-beginning 2))
5576 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5577 ;; (goto-char (match-end 0))) ; Messes what follows
5578 (setq meth nil
5579 p (point))
5580 (while (and ends-ranges (>= p (car ends-ranges)))
5581 ;; delete obsolete entries
5582 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5583 (setq package (or (car packages) "")
5584 end-range (or (car ends-ranges) 0))
5585 (if is-pack ; doing "package"
5586 (progn
5587 (if (match-beginning 5) ; named package
5588 (setq name (buffer-substring (match-beginning 5)
5589 (match-end 5))
5590 name (progn
5591 (set-text-properties 0 (length name) nil name)
5592 name)
5593 package (concat name "::")
5594 name (concat "package " name))
5595 ;; Support nameless packages
5596 (setq name "package;" package ""))
5597 (setq end-range
5598 (save-excursion
5599 (parse-partial-sexp (point) (point-max) -1) (point))
5600 ends-ranges (cons end-range ends-ranges)
5601 packages (cons package packages)))
5602 (setq is-proto
5603 (or (eq (following-char) ?\;)
5604 (eq 0 (get-text-property (point) 'attrib-group)))))
5605 ;; Skip this function name if it is a prototype declaration.
5606 (if (and is-proto (not is-pack)) nil
5607 (or is-pack
5608 (setq name
5609 (buffer-substring (match-beginning 8) (match-end 8)))
5610 (set-text-properties 0 (length name) nil name))
5611 (setq marker (make-marker))
5612 (set-marker marker (match-end (if is-pack 2 8)))
5613 (cond (is-pack nil)
5614 ((string-match "[:']" name)
5615 (setq meth t))
5616 ((> p end-range) nil)
5618 (setq name (concat package name) meth t)))
5619 (setq index (cons name marker))
5620 (if is-pack
5621 (push index index-pack-alist)
5622 (push index index-alist))
5623 (if meth (push index index-meth-alist))
5624 (push index index-unsorted-alist)))
5625 ((match-beginning 16) ; POD section
5626 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5627 marker (make-marker))
5628 (set-marker marker (match-beginning 17))
5629 (set-text-properties 0 (length name) nil name)
5630 (setq name (concat (make-string
5631 (* 3 (- (char-after (match-beginning 16)) ?1))
5632 ?\ )
5633 name)
5634 index (cons name marker))
5635 (setq index1 (cons (concat "=" name) (cdr index)))
5636 (push index index-pod-alist)
5637 (push index1 index-unsorted-alist)))))
5638 (setq index-alist
5639 (if (default-value 'imenu-sort-function)
5640 (sort index-alist (default-value 'imenu-sort-function))
5641 (nreverse index-alist)))
5642 (and index-pod-alist
5643 (push (cons "+POD headers+..."
5644 (nreverse index-pod-alist))
5645 index-alist))
5646 (and (or index-pack-alist index-meth-alist)
5647 (let ((lst index-pack-alist) hier-list pack elt group name)
5648 ;; Remove "package ", reverse and uniquify.
5649 (while lst
5650 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5651 (if (assoc name hier-list) nil
5652 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5653 (setq lst index-meth-alist)
5654 (while lst
5655 (setq elt (car lst) lst (cdr lst))
5656 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5657 (setq pack (substring (car elt) 0 (match-beginning 0)))
5658 (if (setq group (assoc pack hier-list))
5659 (if (listp (cdr group))
5660 ;; Have some functions already
5661 (setcdr group
5662 (cons (cons (substring
5663 (car elt)
5664 (+ 2 (match-beginning 0)))
5665 (cdr elt))
5666 (cdr group)))
5667 (setcdr group (list (cons (substring
5668 (car elt)
5669 (+ 2 (match-beginning 0)))
5670 (cdr elt)))))
5671 (setq hier-list
5672 (cons (cons pack
5673 (list (cons (substring
5674 (car elt)
5675 (+ 2 (match-beginning 0)))
5676 (cdr elt))))
5677 hier-list))))))
5678 (push (cons "+Hierarchy+..."
5679 hier-list)
5680 index-alist)))
5681 (and index-pack-alist
5682 (push (cons "+Packages+..."
5683 (nreverse index-pack-alist))
5684 index-alist))
5685 (and (or index-pack-alist index-pod-alist
5686 (default-value 'imenu-sort-function))
5687 index-unsorted-alist
5688 (push (cons "+Unsorted List+..."
5689 (nreverse index-unsorted-alist))
5690 index-alist))
5691 (cperl-imenu-addback index-alist)))
5694 ;; Suggested by Mark A. Hershberger
5695 (defun cperl-outline-level ()
5696 (looking-at outline-regexp)
5697 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5698 ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5699 ((match-beginning 2) 0) ; package
5700 ((match-beginning 8) 1) ; sub
5701 ((match-beginning 16)
5702 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5703 (t 5))) ; should not happen
5706 (defvar cperl-compilation-error-regexp-alist
5707 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
5708 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
5709 2 3))
5710 "Alist that specifies how to match errors in perl output.")
5713 (defun cperl-windowed-init ()
5714 "Initialization under windowed version."
5715 (cond ((featurep 'ps-print)
5716 (unless cperl-faces-init
5717 (if (boundp 'font-lock-multiline)
5718 (setq cperl-font-lock-multiline t))
5719 (cperl-init-faces)))
5720 ((not cperl-faces-init)
5721 (add-hook 'font-lock-mode-hook
5722 (function
5723 (lambda ()
5724 (if (memq major-mode '(perl-mode cperl-mode))
5725 (progn
5726 (or cperl-faces-init (cperl-init-faces)))))))
5727 (if (fboundp 'eval-after-load)
5728 (eval-after-load
5729 "ps-print"
5730 '(or cperl-faces-init (cperl-init-faces)))))))
5732 (defvar cperl-font-lock-keywords-1 nil
5733 "Additional expressions to highlight in Perl mode. Minimal set.")
5734 (defvar cperl-font-lock-keywords nil
5735 "Additional expressions to highlight in Perl mode. Default set.")
5736 (defvar cperl-font-lock-keywords-2 nil
5737 "Additional expressions to highlight in Perl mode. Maximal set")
5739 (defun cperl-load-font-lock-keywords ()
5740 (or cperl-faces-init (cperl-init-faces))
5741 cperl-font-lock-keywords)
5743 (defun cperl-load-font-lock-keywords-1 ()
5744 (or cperl-faces-init (cperl-init-faces))
5745 cperl-font-lock-keywords-1)
5747 (defun cperl-load-font-lock-keywords-2 ()
5748 (or cperl-faces-init (cperl-init-faces))
5749 cperl-font-lock-keywords-2)
5751 (defun cperl-init-faces-weak ()
5752 ;; Allow `cperl-find-pods-heres' to run.
5753 (or (boundp 'font-lock-constant-face)
5754 (cperl-force-face font-lock-constant-face
5755 "Face for constant and label names"))
5756 (or (boundp 'font-lock-warning-face)
5757 (cperl-force-face font-lock-warning-face
5758 "Face for things which should stand out"))
5759 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5762 (defun cperl-init-faces ()
5763 (condition-case errs
5764 (progn
5765 (require 'font-lock)
5766 (and (fboundp 'font-lock-fontify-anchored-keywords)
5767 (featurep 'font-lock-extra)
5768 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5769 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5770 (if (fboundp 'font-lock-fontify-anchored-keywords)
5771 (setq font-lock-anchored t))
5772 (setq
5773 t-font-lock-keywords
5774 (list
5775 `("[ \t]+$" 0 ',cperl-invalid-face t)
5776 (cons
5777 (concat
5778 "\\(^\\|[^$@%&\\]\\)\\<\\("
5779 (mapconcat
5780 'identity
5781 '("if" "until" "while" "elsif" "else" "unless" "for"
5782 "foreach" "continue" "exit" "die" "last" "goto" "next"
5783 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5784 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5785 "\\|") ; Flow control
5786 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5787 ; In what follows we use `type' style
5788 ; for overwritable builtins
5789 (list
5790 (concat
5791 "\\(^\\|[^$@%&\\]\\)\\<\\("
5792 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5793 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5794 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5795 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5796 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5797 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5798 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5799 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5800 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5801 ;; "gethostbyname" "gethostent" "getlogin"
5802 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5803 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5804 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5805 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5806 ;; "getservbyport" "getservent" "getsockname"
5807 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5808 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5809 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5810 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5811 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5812 ;; "quotemeta" "rand" "read" "readdir" "readline"
5813 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5814 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5815 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5816 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5817 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5818 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5819 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5820 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5821 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5822 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5823 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5824 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5825 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5826 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5827 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5828 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5829 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5830 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5831 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5832 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5833 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5834 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5835 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5836 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5837 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5838 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5839 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5840 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5841 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5842 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5843 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5844 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5845 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5846 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5847 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5848 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5849 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5850 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5851 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5852 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5853 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5854 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5855 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5856 "\\)\\>") 2 'font-lock-type-face)
5857 ;; In what follows we use `other' style
5858 ;; for nonoverwritable builtins
5859 ;; Somehow 's', 'm' are not auto-generated???
5860 (list
5861 (concat
5862 "\\(^\\|[^$@%&\\]\\)\\<\\("
5863 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5864 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5865 ;; "eval" "exists" "for" "foreach" "format" "goto"
5866 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5867 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5868 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5869 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5870 ;; "undef" "unless" "unshift" "untie" "until" "use"
5871 ;; "while" "y"
5872 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5873 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5874 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5875 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5876 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5877 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5878 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5879 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5880 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5881 "\\|[sm]" ; Added manually
5882 "\\)\\>") 2 'cperl-nonoverridable-face)
5883 ;; (mapconcat 'identity
5884 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5885 ;; "#include" "#define" "#undef")
5886 ;; "\\|")
5887 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5888 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5889 ;; This highlights declarations and definitions differenty.
5890 ;; We do not try to highlight in the case of attributes:
5891 ;; it is already done by `cperl-find-pods-heres'
5892 (list (concat "\\<sub"
5893 cperl-white-and-comment-rex ; whitespace/comments
5894 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5895 "\\("
5896 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5897 "([^()]*)\\)?" ; prototype
5898 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5899 "[{;]")
5900 2 (if cperl-font-lock-multiline
5901 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5902 'font-lock-function-name-face
5903 'font-lock-variable-name-face)
5904 ;; need to manually set 'multiline' for older font-locks
5905 '(progn
5906 (if (< 1 (count-lines (match-beginning 0)
5907 (match-end 0)))
5908 (put-text-property
5909 (+ 3 (match-beginning 0)) (match-end 0)
5910 'syntax-type 'multiline))
5911 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5912 'font-lock-function-name-face
5913 'font-lock-variable-name-face))))
5914 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5915 2 font-lock-function-name-face)
5916 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5917 1 font-lock-function-name-face)
5918 (cond ((featurep 'font-lock-extra)
5919 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5920 (2 font-lock-string-face t)
5921 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5922 (font-lock-anchored
5923 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5924 (2 font-lock-string-face t)
5925 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5926 nil nil
5927 (1 font-lock-string-face t))))
5928 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5929 2 font-lock-string-face t)))
5930 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5931 font-lock-string-face t)
5932 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5933 font-lock-constant-face) ; labels
5934 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5935 2 font-lock-constant-face)
5936 ;; Uncomment to get perl-mode-like vars
5937 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5938 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5939 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5940 (cond ((featurep 'font-lock-extra)
5941 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5942 (3 font-lock-variable-name-face)
5943 (4 '(another 4 nil
5944 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5945 (1 font-lock-variable-name-face)
5946 (2 '(restart 2 nil) nil t)))
5947 nil t))) ; local variables, multiple
5948 (font-lock-anchored
5949 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5950 (` ((, (concat "\\<\\(my\\|local\\|our\\)"
5951 cperl-maybe-white-and-comment-rex
5952 "\\(("
5953 cperl-maybe-white-and-comment-rex
5954 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)"))
5955 (5 (, (if cperl-font-lock-multiline
5956 'font-lock-variable-name-face
5957 '(progn (setq cperl-font-lock-multiline-start
5958 (match-beginning 0))
5959 'font-lock-variable-name-face))))
5960 ((, (concat "\\="
5961 cperl-maybe-white-and-comment-rex
5963 cperl-maybe-white-and-comment-rex
5964 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)"))
5965 ;; Bug in font-lock: limit is used not only to limit
5966 ;; searches, but to set the "extend window for
5967 ;; facification" property. Thus we need to minimize.
5968 (, (if cperl-font-lock-multiline
5969 '(if (match-beginning 3)
5970 (save-excursion
5971 (goto-char (match-beginning 3))
5972 (condition-case nil
5973 (forward-sexp 1)
5974 (error
5975 (condition-case nil
5976 (forward-char 200)
5977 (error nil)))) ; typeahead
5978 (1- (point))) ; report limit
5979 (forward-char -2)) ; disable continued expr
5980 '(if (match-beginning 3)
5981 (point-max) ; No limit for continuation
5982 (forward-char -2)))) ; disable continued expr
5983 (, (if cperl-font-lock-multiline
5985 '(progn ; Do at end
5986 ;; "my" may be already fontified (POD),
5987 ;; so cperl-font-lock-multiline-start is nil
5988 (if (or (not cperl-font-lock-multiline-start)
5989 (> 2 (count-lines
5990 cperl-font-lock-multiline-start
5991 (point))))
5993 (put-text-property
5994 (1+ cperl-font-lock-multiline-start) (point)
5995 'syntax-type 'multiline))
5996 (setq cperl-font-lock-multiline-start nil))))
5997 (3 font-lock-variable-name-face)))))
5998 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5999 3 font-lock-variable-name-face)))
6000 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
6001 4 font-lock-variable-name-face)
6002 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
6003 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
6004 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
6005 (setq
6006 t-font-lock-keywords-1
6007 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
6008 ;; not yet as of XEmacs 19.12, works with 21.1.11
6010 (not cperl-xemacs-p)
6011 (string< "21.1.9" emacs-version)
6012 (and (string< "21.1.10" emacs-version)
6013 (string< emacs-version "21.1.2")))
6015 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
6016 (if (eq (char-after (match-beginning 2)) ?%)
6017 'cperl-hash-face
6018 'cperl-array-face)
6019 t) ; arrays and hashes
6020 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
6022 (if (= (- (match-end 2) (match-beginning 2)) 1)
6023 (if (eq (char-after (match-beginning 3)) ?{)
6024 'cperl-hash-face
6025 'cperl-array-face) ; arrays and hashes
6026 font-lock-variable-name-face) ; Just to put something
6028 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
6029 (1 cperl-array-face)
6030 (2 font-lock-variable-name-face))
6031 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
6032 (1 cperl-hash-face)
6033 (2 font-lock-variable-name-face))
6034 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
6035 ;;; Too much noise from \s* @s[ and friends
6036 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
6037 ;;(3 font-lock-function-name-face t t)
6038 ;;(4
6039 ;; (if (cperl-slash-is-regexp)
6040 ;; font-lock-function-name-face 'default) nil t))
6042 (if cperl-highlight-variables-indiscriminately
6043 (setq t-font-lock-keywords-1
6044 (append t-font-lock-keywords-1
6045 (list '("\\([$*]{?\\sw+\\)" 1
6046 font-lock-variable-name-face)))))
6047 (setq cperl-font-lock-keywords-1
6048 (if cperl-syntaxify-by-font-lock
6049 (cons 'cperl-fontify-update
6050 t-font-lock-keywords)
6051 t-font-lock-keywords)
6052 cperl-font-lock-keywords cperl-font-lock-keywords-1
6053 cperl-font-lock-keywords-2 (append
6054 cperl-font-lock-keywords-1
6055 t-font-lock-keywords-1)))
6056 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
6057 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
6058 (eval ; Avoid a warning
6059 '(font-lock-require-faces
6060 (list
6061 ;; Color-light Color-dark Gray-light Gray-dark Mono
6062 (list 'font-lock-comment-face
6063 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
6065 [nil nil t t t]
6066 [nil nil t t t]
6067 nil)
6068 (list 'font-lock-string-face
6069 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
6072 [nil nil t t t]
6073 nil)
6074 (list 'font-lock-function-name-face
6075 (vector
6076 "Blue" "LightSkyBlue" "Gray50" "LightGray"
6077 (cdr (assq 'background-color ; if mono
6078 (frame-parameters))))
6079 (vector
6080 nil nil nil nil
6081 (cdr (assq 'foreground-color ; if mono
6082 (frame-parameters))))
6083 [nil nil t t t]
6085 nil)
6086 (list 'font-lock-variable-name-face
6087 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6089 [nil nil t t t]
6090 [nil nil t t t]
6091 nil)
6092 (list 'font-lock-type-face
6093 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6095 [nil nil t t t]
6097 [nil nil t t t])
6098 (list 'font-lock-warning-face
6099 ["Pink" "Red" "Gray50" "LightGray"]
6100 ["gray20" "gray90"
6101 "gray80" "gray20"]
6102 [nil nil t t t]
6104 [nil nil t t t]
6106 (list 'font-lock-constant-face
6107 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6109 [nil nil t t t]
6111 [nil nil t t t])
6112 (list 'cperl-nonoverridable-face
6113 ["chartreuse3" ("orchid1" "orange")
6114 nil "Gray80"]
6115 [nil nil "gray90"]
6116 [nil nil nil t t]
6117 [nil nil t t]
6118 [nil nil t t t])
6119 (list 'cperl-array-face
6120 ["blue" "yellow" nil "Gray80"]
6121 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6122 "gray90"]
6125 nil)
6126 (list 'cperl-hash-face
6127 ["red" "red" nil "Gray80"]
6128 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6129 "gray90"]
6132 nil))))
6133 ;; Do it the dull way, without choose-color
6134 (defvar cperl-guessed-background nil
6135 "Display characteristics as guessed by cperl.")
6136 ;; (or (fboundp 'x-color-defined-p)
6137 ;; (defalias 'x-color-defined-p
6138 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6139 ;; ;; XEmacs >= 19.12
6140 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6141 ;; ;; XEmacs 19.11
6142 ;; (t 'x-valid-color-name-p))))
6143 (cperl-force-face font-lock-constant-face
6144 "Face for constant and label names")
6145 (cperl-force-face font-lock-variable-name-face
6146 "Face for variable names")
6147 (cperl-force-face font-lock-type-face
6148 "Face for data types")
6149 (cperl-force-face cperl-nonoverridable-face
6150 "Face for data types from another group")
6151 (cperl-force-face font-lock-warning-face
6152 "Face for things which should stand out")
6153 (cperl-force-face font-lock-comment-face
6154 "Face for comments")
6155 (cperl-force-face font-lock-function-name-face
6156 "Face for function names")
6157 (cperl-force-face cperl-hash-face
6158 "Face for hashes")
6159 (cperl-force-face cperl-array-face
6160 "Face for arrays")
6161 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6162 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6163 ;;(or (boundp 'font-lock-type-face)
6164 ;; (defconst font-lock-type-face
6165 ;; 'font-lock-type-face
6166 ;; "Face to use for data types."))
6167 ;;(or (boundp 'cperl-nonoverridable-face)
6168 ;; (defconst cperl-nonoverridable-face
6169 ;; 'cperl-nonoverridable-face
6170 ;; "Face to use for data types from another group."))
6171 ;;(if (not cperl-xemacs-p) nil
6172 ;; (or (boundp 'font-lock-comment-face)
6173 ;; (defconst font-lock-comment-face
6174 ;; 'font-lock-comment-face
6175 ;; "Face to use for comments."))
6176 ;; (or (boundp 'font-lock-keyword-face)
6177 ;; (defconst font-lock-keyword-face
6178 ;; 'font-lock-keyword-face
6179 ;; "Face to use for keywords."))
6180 ;; (or (boundp 'font-lock-function-name-face)
6181 ;; (defconst font-lock-function-name-face
6182 ;; 'font-lock-function-name-face
6183 ;; "Face to use for function names.")))
6184 (if (and
6185 (not (cperl-is-face 'cperl-array-face))
6186 (cperl-is-face 'font-lock-emphasized-face))
6187 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6188 (if (and
6189 (not (cperl-is-face 'cperl-hash-face))
6190 (cperl-is-face 'font-lock-other-emphasized-face))
6191 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6192 (if (and
6193 (not (cperl-is-face 'cperl-nonoverridable-face))
6194 (cperl-is-face 'font-lock-other-type-face))
6195 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6196 ;;(or (boundp 'cperl-hash-face)
6197 ;; (defconst cperl-hash-face
6198 ;; 'cperl-hash-face
6199 ;; "Face to use for hashes."))
6200 ;;(or (boundp 'cperl-array-face)
6201 ;; (defconst cperl-array-face
6202 ;; 'cperl-array-face
6203 ;; "Face to use for arrays."))
6204 ;; Here we try to guess background
6205 (let ((background
6206 (if (boundp 'font-lock-background-mode)
6207 font-lock-background-mode
6208 'light))
6209 (face-list (and (fboundp 'face-list) (face-list))))
6210 ;;;; (fset 'cperl-is-face
6211 ;;;; (cond ((fboundp 'find-face)
6212 ;;;; (symbol-function 'find-face))
6213 ;;;; (face-list
6214 ;;;; (function (lambda (face) (member face face-list))))
6215 ;;;; (t
6216 ;;;; (function (lambda (face) (boundp face))))))
6217 (defvar cperl-guessed-background
6218 (if (and (boundp 'font-lock-display-type)
6219 (eq font-lock-display-type 'grayscale))
6220 'gray
6221 background)
6222 "Background as guessed by CPerl mode")
6223 (and (not (cperl-is-face 'font-lock-constant-face))
6224 (cperl-is-face 'font-lock-reference-face)
6225 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6226 (if (cperl-is-face 'font-lock-type-face) nil
6227 (copy-face 'default 'font-lock-type-face)
6228 (cond
6229 ((eq background 'light)
6230 (set-face-foreground 'font-lock-type-face
6231 (if (x-color-defined-p "seagreen")
6232 "seagreen"
6233 "sea green")))
6234 ((eq background 'dark)
6235 (set-face-foreground 'font-lock-type-face
6236 (if (x-color-defined-p "os2pink")
6237 "os2pink"
6238 "pink")))
6240 (set-face-background 'font-lock-type-face "gray90"))))
6241 (if (cperl-is-face 'cperl-nonoverridable-face)
6243 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6244 (cond
6245 ((eq background 'light)
6246 (set-face-foreground 'cperl-nonoverridable-face
6247 (if (x-color-defined-p "chartreuse3")
6248 "chartreuse3"
6249 "chartreuse")))
6250 ((eq background 'dark)
6251 (set-face-foreground 'cperl-nonoverridable-face
6252 (if (x-color-defined-p "orchid1")
6253 "orchid1"
6254 "orange")))))
6255 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6256 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6257 ;;; (cond
6258 ;;; ((eq background 'light)
6259 ;;; (set-face-background 'font-lock-other-emphasized-face
6260 ;;; (if (x-color-defined-p "lightyellow2")
6261 ;;; "lightyellow2"
6262 ;;; (if (x-color-defined-p "lightyellow")
6263 ;;; "lightyellow"
6264 ;;; "light yellow"))))
6265 ;;; ((eq background 'dark)
6266 ;;; (set-face-background 'font-lock-other-emphasized-face
6267 ;;; (if (x-color-defined-p "navy")
6268 ;;; "navy"
6269 ;;; (if (x-color-defined-p "darkgreen")
6270 ;;; "darkgreen"
6271 ;;; "dark green"))))
6272 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6273 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6274 ;;; (copy-face 'bold 'font-lock-emphasized-face)
6275 ;;; (cond
6276 ;;; ((eq background 'light)
6277 ;;; (set-face-background 'font-lock-emphasized-face
6278 ;;; (if (x-color-defined-p "lightyellow2")
6279 ;;; "lightyellow2"
6280 ;;; "lightyellow")))
6281 ;;; ((eq background 'dark)
6282 ;;; (set-face-background 'font-lock-emphasized-face
6283 ;;; (if (x-color-defined-p "navy")
6284 ;;; "navy"
6285 ;;; (if (x-color-defined-p "darkgreen")
6286 ;;; "darkgreen"
6287 ;;; "dark green"))))
6288 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6289 (if (cperl-is-face 'font-lock-variable-name-face) nil
6290 (copy-face 'italic 'font-lock-variable-name-face))
6291 (if (cperl-is-face 'font-lock-constant-face) nil
6292 (copy-face 'italic 'font-lock-constant-face))))
6293 (setq cperl-faces-init t))
6294 (error (message "cperl-init-faces (ignored): %s" errs))))
6297 (defun cperl-ps-print-init ()
6298 "Initialization of `ps-print' components for faces used in CPerl."
6299 (eval-after-load "ps-print"
6300 '(setq ps-bold-faces
6301 ;; font-lock-variable-name-face
6302 ;; font-lock-constant-face
6303 (append '(cperl-array-face cperl-hash-face)
6304 ps-bold-faces)
6305 ps-italic-faces
6306 ;; font-lock-constant-face
6307 (append '(cperl-nonoverridable-face cperl-hash-face)
6308 ps-italic-faces)
6309 ps-underlined-faces
6310 ;; font-lock-type-face
6311 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6312 ps-underlined-faces))))
6314 (defvar ps-print-face-extension-alist)
6316 (defun cperl-ps-print (&optional file)
6317 "Pretty-print in CPerl style.
6318 If optional argument FILE is an empty string, prints to printer, otherwise
6319 to the file FILE. If FILE is nil, prompts for a file name.
6321 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6322 (interactive)
6323 (or file
6324 (setq file (read-from-minibuffer
6325 "Print to file (if empty - to printer): "
6326 (concat (buffer-file-name) ".ps")
6327 nil nil 'file-name-history)))
6328 (or (> (length file) 0)
6329 (setq file nil))
6330 (require 'ps-print) ; To get ps-print-face-extension-alist
6331 (let ((ps-print-color-p t)
6332 (ps-print-face-extension-alist ps-print-face-extension-alist))
6333 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6334 (ps-print-buffer-with-faces file)))
6336 ;;; (defun cperl-ps-print-init ()
6337 ;;; "Initialization of `ps-print' components for faces used in CPerl."
6338 ;;; ;; Guard against old versions
6339 ;;; (defvar ps-underlined-faces nil)
6340 ;;; (defvar ps-bold-faces nil)
6341 ;;; (defvar ps-italic-faces nil)
6342 ;;; (setq ps-bold-faces
6343 ;;; (append '(font-lock-emphasized-face
6344 ;;; cperl-array-face
6345 ;;; font-lock-keyword-face
6346 ;;; font-lock-variable-name-face
6347 ;;; font-lock-constant-face
6348 ;;; font-lock-reference-face
6349 ;;; font-lock-other-emphasized-face
6350 ;;; cperl-hash-face)
6351 ;;; ps-bold-faces))
6352 ;;; (setq ps-italic-faces
6353 ;;; (append '(cperl-nonoverridable-face
6354 ;;; font-lock-constant-face
6355 ;;; font-lock-reference-face
6356 ;;; font-lock-other-emphasized-face
6357 ;;; cperl-hash-face)
6358 ;;; ps-italic-faces))
6359 ;;; (setq ps-underlined-faces
6360 ;;; (append '(font-lock-emphasized-face
6361 ;;; cperl-array-face
6362 ;;; font-lock-other-emphasized-face
6363 ;;; cperl-hash-face
6364 ;;; cperl-nonoverridable-face font-lock-type-face)
6365 ;;; ps-underlined-faces))
6366 ;;; (cons 'font-lock-type-face ps-underlined-faces))
6369 (if (cperl-enable-font-lock) (cperl-windowed-init))
6371 (defconst cperl-styles-entries
6372 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6373 cperl-label-offset cperl-extra-newline-before-brace
6374 cperl-extra-newline-before-brace-multiline
6375 cperl-merge-trailing-else
6376 cperl-continued-statement-offset))
6378 (defconst cperl-style-examples
6379 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6380 ##### cperl-continued-brace-offset cperl-label-offset
6381 ##### cperl-continued-statement-offset
6382 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6384 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6386 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6387 if (foo) {
6389 baz;
6390 label:
6392 boon;
6394 } else {
6395 stop;
6398 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6399 if (foo) {
6401 baz;
6402 label:
6404 boon;
6406 } else {
6407 stop;
6410 ### GNU 2/0/0/-2/2/nil/t
6411 if (foo)
6414 baz;
6415 label:
6417 boon;
6420 else
6422 stop;
6425 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6426 if (foo)
6429 baz;
6430 label:
6432 boon;
6435 else
6437 stop;
6440 ### BSD (=C++, but will not change preexisting merge-trailing-else
6441 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6442 if (foo)
6445 baz;
6446 label:
6448 boon;
6451 else
6453 stop;
6456 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6457 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6458 if (foo)
6461 baz;
6462 label:
6464 boon;
6467 else
6469 stop;
6472 ### Whitesmith (=PerlStyle, but will not change preexisting
6473 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6474 if (foo)
6477 baz;
6478 label:
6480 boon;
6483 else
6485 stop;
6488 "Examples of if/else with different indent styles (with v4.23).")
6490 (defconst cperl-style-alist
6491 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6492 (cperl-indent-level . 2)
6493 (cperl-brace-offset . 0)
6494 (cperl-continued-brace-offset . 0)
6495 (cperl-label-offset . -2)
6496 (cperl-continued-statement-offset . 2)
6497 (cperl-extra-newline-before-brace . nil)
6498 (cperl-extra-newline-before-brace-multiline . nil)
6499 (cperl-merge-trailing-else . t))
6501 ("PerlStyle" ; CPerl with 4 as indent
6502 (cperl-indent-level . 4)
6503 (cperl-brace-offset . 0)
6504 (cperl-continued-brace-offset . 0)
6505 (cperl-label-offset . -4)
6506 (cperl-continued-statement-offset . 4)
6507 (cperl-extra-newline-before-brace . nil)
6508 (cperl-extra-newline-before-brace-multiline . nil)
6509 (cperl-merge-trailing-else . t))
6511 ("GNU"
6512 (cperl-indent-level . 2)
6513 (cperl-brace-offset . 0)
6514 (cperl-continued-brace-offset . 0)
6515 (cperl-label-offset . -2)
6516 (cperl-continued-statement-offset . 2)
6517 (cperl-extra-newline-before-brace . t)
6518 (cperl-extra-newline-before-brace-multiline . t)
6519 (cperl-merge-trailing-else . nil))
6521 ("K&R"
6522 (cperl-indent-level . 5)
6523 (cperl-brace-offset . 0)
6524 (cperl-continued-brace-offset . -5)
6525 (cperl-label-offset . -5)
6526 (cperl-continued-statement-offset . 5)
6527 ;;(cperl-extra-newline-before-brace . nil) ; ???
6528 ;;(cperl-extra-newline-before-brace-multiline . nil)
6529 (cperl-merge-trailing-else . nil))
6531 ("BSD"
6532 (cperl-indent-level . 4)
6533 (cperl-brace-offset . 0)
6534 (cperl-continued-brace-offset . -4)
6535 (cperl-label-offset . -4)
6536 (cperl-continued-statement-offset . 4)
6537 ;;(cperl-extra-newline-before-brace . nil) ; ???
6538 ;;(cperl-extra-newline-before-brace-multiline . nil)
6539 ;;(cperl-merge-trailing-else . nil) ; ???
6542 ("C++"
6543 (cperl-indent-level . 4)
6544 (cperl-brace-offset . 0)
6545 (cperl-continued-brace-offset . -4)
6546 (cperl-label-offset . -4)
6547 (cperl-continued-statement-offset . 4)
6548 (cperl-extra-newline-before-brace . t)
6549 (cperl-extra-newline-before-brace-multiline . t)
6550 (cperl-merge-trailing-else . nil))
6552 ("Whitesmith"
6553 (cperl-indent-level . 4)
6554 (cperl-brace-offset . 0)
6555 (cperl-continued-brace-offset . 0)
6556 (cperl-label-offset . -4)
6557 (cperl-continued-statement-offset . 4)
6558 ;;(cperl-extra-newline-before-brace . nil) ; ???
6559 ;;(cperl-extra-newline-before-brace-multiline . nil)
6560 ;;(cperl-merge-trailing-else . nil) ; ???
6562 ("Current"))
6563 "List of variables to set to get a particular indentation style.
6564 Should be used via `cperl-set-style' or via Perl menu.
6566 See examples in `cperl-style-examples'.")
6568 (defun cperl-set-style (style)
6569 "Set CPerl mode variables to use one of several different indentation styles.
6570 The arguments are a string representing the desired style.
6571 The list of styles is in `cperl-style-alist', available styles
6572 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6574 The current value of style is memorized (unless there is a memorized
6575 data already), may be restored by `cperl-set-style-back'.
6577 Chosing \"Current\" style will not change style, so this may be used for
6578 side-effect of memorizing only. Examples in `cperl-style-examples'."
6579 (interactive
6580 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6581 cperl-style-alist)))
6582 (list (completing-read "Enter style: " list nil 'insist))))
6583 (or cperl-old-style
6584 (setq cperl-old-style
6585 (mapcar (function
6586 (lambda (name)
6587 (cons name (eval name))))
6588 cperl-styles-entries)))
6589 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6590 (while style
6591 (setq setting (car style) style (cdr style))
6592 (set (car setting) (cdr setting)))))
6594 (defun cperl-set-style-back ()
6595 "Restore a style memorized by `cperl-set-style'."
6596 (interactive)
6597 (or cperl-old-style (error "The style was not changed"))
6598 (let (setting)
6599 (while cperl-old-style
6600 (setq setting (car cperl-old-style)
6601 cperl-old-style (cdr cperl-old-style))
6602 (set (car setting) (cdr setting)))))
6604 (defun cperl-check-syntax ()
6605 (interactive)
6606 (require 'mode-compile)
6607 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6608 (eval '(mode-compile)))) ; Avoid a warning
6610 (defun cperl-info-buffer (type)
6611 ;; Returns buffer with documentation. Creates if missing.
6612 ;; If TYPE, this vars buffer.
6613 ;; Special care is taken to not stomp over an existing info buffer
6614 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6615 (info (get-buffer bname))
6616 (oldbuf (get-buffer "*info*")))
6617 (if info info
6618 (save-window-excursion
6619 ;; Get Info running
6620 (require 'info)
6621 (cond (oldbuf
6622 (set-buffer oldbuf)
6623 (rename-buffer "*info-perl-tmp*")))
6624 (save-window-excursion
6625 (info))
6626 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6627 (set-buffer "*info*")
6628 (rename-buffer bname)
6629 (cond (oldbuf
6630 (set-buffer "*info-perl-tmp*")
6631 (rename-buffer "*info*")
6632 (set-buffer bname)))
6633 (make-local-variable 'window-min-height)
6634 (setq window-min-height 2)
6635 (current-buffer)))))
6637 (defun cperl-word-at-point (&optional p)
6638 "Return the word at point or at P."
6639 (save-excursion
6640 (if p (goto-char p))
6641 (or (cperl-word-at-point-hard)
6642 (progn
6643 (require 'etags)
6644 (funcall (or (and (boundp 'find-tag-default-function)
6645 find-tag-default-function)
6646 (get major-mode 'find-tag-default-function)
6647 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6648 ;; automatically used within `find-tag-default':
6649 'find-tag-default))))))
6651 (defun cperl-info-on-command (command)
6652 "Show documentation for Perl command COMMAND in other window.
6653 If perl-info buffer is shown in some frame, uses this frame.
6654 Customized by setting variables `cperl-shrink-wrap-info-frame',
6655 `cperl-max-help-size'."
6656 (interactive
6657 (let* ((default (cperl-word-at-point))
6658 (read (read-string
6659 (format "Find doc for Perl function (default %s): "
6660 default))))
6661 (list (if (equal read "")
6662 default
6663 read))))
6665 (let ((buffer (current-buffer))
6666 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6667 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6668 max-height char-height buf-list)
6669 (if (string-match "^-[a-zA-Z]$" command)
6670 (setq cmd-desc "^-X[ \t\n]"))
6671 (setq isvar (string-match "^[$@%]" command)
6672 buf (cperl-info-buffer isvar)
6673 iniwin (selected-window)
6674 fr1 (window-frame iniwin))
6675 (set-buffer buf)
6676 (goto-char (point-min))
6677 (or isvar
6678 (progn (re-search-forward "^-X[ \t\n]")
6679 (forward-line -1)))
6680 (if (re-search-forward cmd-desc nil t)
6681 (progn
6682 ;; Go back to beginning of the group (ex, for qq)
6683 (if (re-search-backward "^[ \t\n\f]")
6684 (forward-line 1))
6685 (beginning-of-line)
6686 ;; Get some of
6687 (setq pos (point)
6688 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6689 (while (and (not win) buf-list)
6690 (setq win (get-buffer-window (car buf-list) t))
6691 (setq buf-list (cdr buf-list)))
6692 (or (not win)
6693 (eq (window-buffer win) buf)
6694 (set-window-buffer win buf))
6695 (and win (setq fr2 (window-frame win)))
6696 (if (or (not fr2) (eq fr1 fr2))
6697 (pop-to-buffer buf)
6698 (special-display-popup-frame buf) ; Make it visible
6699 (select-window win))
6700 (goto-char pos) ; Needed (?!).
6701 ;; Resize
6702 (setq iniheight (window-height)
6703 frheight (frame-height)
6704 not-loner (< iniheight (1- frheight))) ; Are not alone
6705 (cond ((if not-loner cperl-max-help-size
6706 cperl-shrink-wrap-info-frame)
6707 (setq height
6708 (+ 2
6709 (count-lines
6711 (save-excursion
6712 (if (re-search-forward
6713 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6714 (match-beginning 0) (point-max)))))
6715 max-height
6716 (if not-loner
6717 (/ (* (- frheight 3) cperl-max-help-size) 100)
6718 (setq char-height (frame-char-height))
6719 ;; Non-functioning under OS/2:
6720 (if (eq char-height 1) (setq char-height 18))
6721 ;; Title, menubar, + 2 for slack
6722 (- (/ (x-display-pixel-height) char-height) 4)))
6723 (if (> height max-height) (setq height max-height))
6724 ;;(message "was %s doing %s" iniheight height)
6725 (if not-loner
6726 (enlarge-window (- height iniheight))
6727 (set-frame-height (window-frame win) (1+ height)))))
6728 (set-window-start (selected-window) pos))
6729 (message "No entry for %s found." command))
6730 ;;(pop-to-buffer buffer)
6731 (select-window iniwin)))
6733 (defun cperl-info-on-current-command ()
6734 "Show documentation for Perl command at point in other window."
6735 (interactive)
6736 (cperl-info-on-command (cperl-word-at-point)))
6738 (defun cperl-imenu-info-imenu-search ()
6739 (if (looking-at "^-X[ \t\n]") nil
6740 (re-search-backward
6741 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6742 (forward-line 1)))
6744 (defun cperl-imenu-info-imenu-name ()
6745 (buffer-substring
6746 (match-beginning 1) (match-end 1)))
6748 (defun cperl-imenu-on-info ()
6749 "Shows imenu for Perl Info Buffer.
6750 Opens Perl Info buffer if needed."
6751 (interactive)
6752 (let* ((buffer (current-buffer))
6753 imenu-create-index-function
6754 imenu-prev-index-position-function
6755 imenu-extract-index-name-function
6756 (index-item (save-restriction
6757 (save-window-excursion
6758 (set-buffer (cperl-info-buffer nil))
6759 (setq imenu-create-index-function
6760 'imenu-default-create-index-function
6761 imenu-prev-index-position-function
6762 'cperl-imenu-info-imenu-search
6763 imenu-extract-index-name-function
6764 'cperl-imenu-info-imenu-name)
6765 (imenu-choose-buffer-index)))))
6766 (and index-item
6767 (progn
6768 (push-mark)
6769 (pop-to-buffer "*info-perl*")
6770 (cond
6771 ((markerp (cdr index-item))
6772 (goto-char (marker-position (cdr index-item))))
6774 (goto-char (cdr index-item))))
6775 (set-window-start (selected-window) (point))
6776 (pop-to-buffer buffer)))))
6778 (defun cperl-lineup (beg end &optional step minshift)
6779 "Lineup construction in a region.
6780 Beginning of region should be at the start of a construction.
6781 All first occurrences of this construction in the lines that are
6782 partially contained in the region are lined up at the same column.
6784 MINSHIFT is the minimal amount of space to insert before the construction.
6785 STEP is the tabwidth to position constructions.
6786 If STEP is nil, `cperl-lineup-step' will be used
6787 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6788 Will not move the position at the start to the left."
6789 (interactive "r")
6790 (let (search col tcol seen b)
6791 (save-excursion
6792 (goto-char end)
6793 (end-of-line)
6794 (setq end (point-marker))
6795 (goto-char beg)
6796 (skip-chars-forward " \t\f")
6797 (setq beg (point-marker))
6798 (indent-region beg end nil)
6799 (goto-char beg)
6800 (setq col (current-column))
6801 (if (looking-at "[a-zA-Z0-9_]")
6802 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6803 (setq search
6804 (concat "\\<"
6805 (regexp-quote
6806 (buffer-substring (match-beginning 0)
6807 (match-end 0))) "\\>"))
6808 (error "Cannot line up in a middle of the word"))
6809 (if (looking-at "$")
6810 (error "Cannot line up end of line"))
6811 (setq search (regexp-quote (char-to-string (following-char)))))
6812 (setq step (or step cperl-lineup-step cperl-indent-level))
6813 (or minshift (setq minshift 1))
6814 (while (progn
6815 (beginning-of-line 2)
6816 (and (< (point) end)
6817 (re-search-forward search end t)
6818 (goto-char (match-beginning 0))))
6819 (setq tcol (current-column) seen t)
6820 (if (> tcol col) (setq col tcol)))
6821 (or seen
6822 (error "The construction to line up occurred only once"))
6823 (goto-char beg)
6824 (setq col (+ col minshift))
6825 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6826 (while
6827 (progn
6828 (cperl-make-indent col)
6829 (beginning-of-line 2)
6830 (and (< (point) end)
6831 (re-search-forward search end t)
6832 (goto-char (match-beginning 0)))))))) ; No body
6834 (defun cperl-etags (&optional add all files) ;; NOT USED???
6835 "Run etags with appropriate options for Perl files.
6836 If optional argument ALL is `recursive', will process Perl files
6837 in subdirectories too."
6838 (interactive)
6839 (let ((cmd "etags")
6840 (args '("-l" "none" "-r"
6841 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6842 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6843 "-r"
6844 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6845 "-r"
6846 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6847 res)
6848 (if add (setq args (cons "-a" args)))
6849 (or files (setq files (list buffer-file-name)))
6850 (cond
6851 ((eq all 'recursive)
6852 ;;(error "Not implemented: recursive")
6853 (setq args (append (list "-e"
6854 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6855 use File::Find;
6856 find(\\&wanted, '.');
6857 exec @ARGV;"
6858 cmd) args)
6859 cmd "perl"))
6860 (all
6861 ;;(error "Not implemented: all")
6862 (setq args (append (list "-e"
6863 "push @ARGV, <*.PL *.pl *.pm>;
6864 exec @ARGV;"
6865 cmd) args)
6866 cmd "perl"))
6868 (setq args (append args files))))
6869 (setq res (apply 'call-process cmd nil nil nil args))
6870 (or (eq res 0)
6871 (message "etags returned \"%s\"" res))))
6873 (defun cperl-toggle-auto-newline ()
6874 "Toggle the state of `cperl-auto-newline'."
6875 (interactive)
6876 (setq cperl-auto-newline (not cperl-auto-newline))
6877 (message "Newlines will %sbe auto-inserted now."
6878 (if cperl-auto-newline "" "not ")))
6880 (defun cperl-toggle-abbrev ()
6881 "Toggle the state of automatic keyword expansion in CPerl mode."
6882 (interactive)
6883 (abbrev-mode (if abbrev-mode 0 1))
6884 (message "Perl control structure will %sbe auto-inserted now."
6885 (if abbrev-mode "" "not ")))
6888 (defun cperl-toggle-electric ()
6889 "Toggle the state of parentheses doubling in CPerl mode."
6890 (interactive)
6891 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6892 (message "Parentheses will %sbe auto-doubled now."
6893 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6895 (defun cperl-toggle-autohelp ()
6896 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6897 Delay of auto-help controlled by `cperl-lazy-help-time'."
6898 (interactive)
6899 (if (fboundp 'run-with-idle-timer)
6900 (progn
6901 (if cperl-lazy-installed
6902 (cperl-lazy-unstall)
6903 (cperl-lazy-install))
6904 (message "Perl help messages will %sbe automatically shown now."
6905 (if cperl-lazy-installed "" "not ")))
6906 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6908 (defun cperl-toggle-construct-fix ()
6909 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6910 (interactive)
6911 (setq cperl-indent-region-fix-constructs
6912 (if cperl-indent-region-fix-constructs
6915 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6916 (if cperl-indent-region-fix-constructs "" "not ")))
6918 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6919 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6920 Nonpositive numeric argument disables debugging messages. The message
6921 summarizes which regions it was decided to rescan for syntactic constructs.
6923 The message looks like this:
6925 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6927 Numbers are character positions in the buffer. REQ provides the range to
6928 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6929 for correct operation it should start and end outside any special syntactic
6930 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6931 by CPerl."
6932 (interactive "P")
6933 (or arg
6934 (setq arg (if (eq cperl-syntaxify-by-font-lock
6935 (if backtrace 'backtrace 'message)) 0 1)))
6936 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6937 (setq cperl-syntaxify-by-font-lock arg)
6938 (message "Debugging messages of syntax unwind %sabled."
6939 (if (eq arg t) "dis" "en")))
6941 ;;;; Tags file creation.
6943 (defvar cperl-tmp-buffer " *cperl-tmp*")
6945 (defun cperl-setup-tmp-buf ()
6946 (set-buffer (get-buffer-create cperl-tmp-buffer))
6947 (set-syntax-table cperl-mode-syntax-table)
6948 (buffer-disable-undo)
6949 (auto-fill-mode 0)
6950 (if cperl-use-syntax-table-text-property-for-tags
6951 (progn
6952 (make-local-variable 'parse-sexp-lookup-properties)
6953 ;; Do not introduce variable if not needed, we check it!
6954 (set 'parse-sexp-lookup-properties t))))
6956 (defun cperl-xsub-scan ()
6957 (require 'imenu)
6958 (let ((index-alist '())
6959 (prev-pos 0) index index1 name package prefix)
6960 (goto-char (point-min))
6961 ;; Search for the function
6962 (progn ;;save-match-data
6963 (while (re-search-forward
6964 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6965 nil t)
6966 (cond
6967 ((match-beginning 2) ; SECTION
6968 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6969 (goto-char (match-beginning 0))
6970 (skip-chars-forward " \t")
6971 (forward-char 1)
6972 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6973 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6974 (setq prefix nil)))
6975 ((not package) nil) ; C language section
6976 ((match-beginning 3) ; XSUB
6977 (goto-char (1+ (match-beginning 3)))
6978 (setq index (imenu-example--name-and-position))
6979 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6980 (if (and prefix (string-match (concat "^" prefix) name))
6981 (setq name (substring name (length prefix))))
6982 (cond ((string-match "::" name) nil)
6984 (setq index1 (cons (concat package "::" name) (cdr index)))
6985 (push index1 index-alist)))
6986 (setcar index name)
6987 (push index index-alist))
6988 (t ; BOOT: section
6989 ;; (beginning-of-line)
6990 (setq index (imenu-example--name-and-position))
6991 (setcar index (concat package "::BOOT:"))
6992 (push index index-alist)))))
6993 index-alist))
6995 (defvar cperl-unreadable-ok nil)
6997 (defun cperl-find-tags (ifile xs topdir)
6998 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6999 (cperl-pod-here-fontify nil) f file)
7000 (save-excursion
7001 (if b (set-buffer b)
7002 (cperl-setup-tmp-buf))
7003 (erase-buffer)
7004 (condition-case err
7005 (setq file (car (insert-file-contents ifile)))
7006 (error (if cperl-unreadable-ok nil
7007 (if (y-or-n-p
7008 (format "File %s unreadable. Continue? " ifile))
7009 (setq cperl-unreadable-ok t)
7010 (error "Aborting: unreadable file %s" ifile)))))
7011 (if (not file)
7012 (message "Unreadable file %s" ifile)
7013 (message "Scanning file %s ..." file)
7014 (if (and cperl-use-syntax-table-text-property-for-tags
7015 (not xs))
7016 (condition-case err ; after __END__ may have garbage
7017 (cperl-find-pods-heres nil nil noninteractive)
7018 (error (message "While scanning for syntax: %s" err))))
7019 (if xs
7020 (setq lst (cperl-xsub-scan))
7021 (setq ind (cperl-imenu--create-perl-index))
7022 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
7023 (setq lst
7024 (mapcar
7025 (function
7026 (lambda (elt)
7027 (cond ((string-match "^[_a-zA-Z]" (car elt))
7028 (goto-char (cdr elt))
7029 (beginning-of-line) ; pos should be of the start of the line
7030 (list (car elt)
7031 (point)
7032 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
7033 (buffer-substring (progn
7034 (goto-char (cdr elt))
7035 ;; After name now...
7036 (or (eolp) (forward-char 1))
7037 (point))
7038 (progn
7039 (beginning-of-line)
7040 (point))))))))
7041 lst))
7042 (erase-buffer)
7043 (while lst
7044 (setq elt (car lst) lst (cdr lst))
7045 (if elt
7046 (progn
7047 (insert (elt elt 3)
7049 (if (string-match "^package " (car elt))
7050 (substring (car elt) 8)
7051 (car elt) )
7053 (number-to-string (elt elt 2)) ; Line
7055 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
7056 "\n")
7057 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
7058 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
7059 (elt elt 3)))
7060 ;; Need to insert the name without package as well
7061 (setq lst (cons (cons (substring (elt elt 3)
7062 (match-beginning 1)
7063 (match-end 1))
7064 (cdr elt))
7065 lst))))))
7066 (setq pos (point))
7067 (goto-char 1)
7068 (setq rel file)
7069 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7070 (set-text-properties 0 (length rel) nil rel)
7071 (and (equal topdir (substring rel 0 (length topdir)))
7072 (setq rel (substring file (length topdir))))
7073 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
7074 (setq ret (buffer-substring 1 (point-max)))
7075 (erase-buffer)
7076 (or noninteractive
7077 (message "Scanning file %s finished" file))
7078 ret))))
7080 (defun cperl-add-tags-recurse-noxs ()
7081 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
7082 Use as
7083 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7084 -f cperl-add-tags-recurse-noxs
7086 (cperl-write-tags nil nil t t nil t))
7088 (defun cperl-add-tags-recurse-noxs-fullpath ()
7089 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7090 Writes down fullpath, so TAGS is relocatable (but if the build directory
7091 is relocated, the file TAGS inside it breaks). Use as
7092 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7093 -f cperl-add-tags-recurse-noxs-fullpath
7095 (cperl-write-tags nil nil t t nil t ""))
7097 (defun cperl-add-tags-recurse ()
7098 "Add to TAGS file data for Perl files in the current directory and kids.
7099 Use as
7100 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7101 -f cperl-add-tags-recurse
7103 (cperl-write-tags nil nil t t))
7105 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7106 ;; If INBUFFER, do not select buffer, and do not save
7107 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7108 (require 'etags)
7109 (if file nil
7110 (setq file (if dir default-directory (buffer-file-name)))
7111 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7112 (or topdir
7113 (setq topdir default-directory))
7114 (let ((tags-file-name "TAGS")
7115 (case-fold-search (eq system-type 'emx))
7116 xs rel tm)
7117 (save-excursion
7118 (cond (inbuffer nil) ; Already there
7119 ((file-exists-p tags-file-name)
7120 (if cperl-xemacs-p
7121 (visit-tags-table-buffer)
7122 (visit-tags-table-buffer tags-file-name)))
7123 (t (set-buffer (find-file-noselect tags-file-name))))
7124 (cond
7125 (dir
7126 (cond ((eq erase 'ignore))
7127 (erase
7128 (erase-buffer)
7129 (setq erase 'ignore)))
7130 (let ((files
7131 (condition-case err
7132 (directory-files file t
7133 (if recurse nil cperl-scan-files-regexp)
7135 (error
7136 (if cperl-unreadable-ok nil
7137 (if (y-or-n-p
7138 (format "Directory %s unreadable. Continue? " file))
7139 (setq cperl-unreadable-ok t
7140 tm nil) ; Return empty list
7141 (error "Aborting: unreadable directory %s" file)))))))
7142 (mapcar (function
7143 (lambda (file)
7144 (cond
7145 ((string-match cperl-noscan-files-regexp file)
7146 nil)
7147 ((not (file-directory-p file))
7148 (if (string-match cperl-scan-files-regexp file)
7149 (cperl-write-tags file erase recurse nil t noxs topdir)))
7150 ((not recurse) nil)
7151 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7152 files)))
7154 (setq xs (string-match "\\.xs$" file))
7155 (if (not (and xs noxs))
7156 (progn
7157 (cond ((eq erase 'ignore) (goto-char (point-max)))
7158 (erase (erase-buffer))
7160 (goto-char 1)
7161 (setq rel file)
7162 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7163 (set-text-properties 0 (length rel) nil rel)
7164 (and (equal topdir (substring rel 0 (length topdir)))
7165 (setq rel (substring file (length topdir))))
7166 (if (search-forward (concat "\f\n" rel ",") nil t)
7167 (progn
7168 (search-backward "\f\n")
7169 (delete-region (point)
7170 (save-excursion
7171 (forward-char 1)
7172 (if (search-forward "\f\n"
7173 nil 'toend)
7174 (- (point) 2)
7175 (point-max)))))
7176 (goto-char (point-max)))))
7177 (insert (cperl-find-tags file xs topdir))))))
7178 (if inbuffer nil ; Delegate to the caller
7179 (save-buffer 0) ; No backup
7180 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7181 (initialize-new-tags-table))))))
7183 (defvar cperl-tags-hier-regexp-list
7184 (concat
7185 "^\\("
7186 "\\(package\\)\\>"
7187 "\\|"
7188 "sub\\>[^\n]+::"
7189 "\\|"
7190 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7191 "\\|"
7192 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7193 "\\)"))
7195 (defvar cperl-hierarchy '(() ())
7196 "Global hierarchy of classes.")
7198 (defun cperl-tags-hier-fill ()
7199 ;; Suppose we are in a tag table cooked by cperl.
7200 (goto-char 1)
7201 (let (type pack name pos line chunk ord cons1 file str info fileind)
7202 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7203 (setq pos (match-beginning 0)
7204 pack (match-beginning 2))
7205 (beginning-of-line)
7206 (if (looking-at (concat
7207 "\\([^\n]+\\)"
7208 "\C-?"
7209 "\\([^\n]+\\)"
7210 "\C-a"
7211 "\\([0-9]+\\)"
7213 "\\([0-9]+\\)"))
7214 (progn
7215 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7216 name (buffer-substring (match-beginning 2) (match-end 2))
7217 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7218 line (buffer-substring (match-beginning 3) (match-end 3))
7219 ord (if pack 1 0)
7220 file (file-of-tag)
7221 fileind (format "%s:%s" file line)
7222 ;; Moves to beginning of the next line:
7223 info (cperl-etags-snarf-tag file line))
7224 ;; Move back
7225 (forward-char -1)
7226 ;; Make new member of hierarchy name ==> file ==> pos if needed
7227 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7228 ;; Name known
7229 (setcdr cons1 (cons (cons fileind (vector file info))
7230 (cdr cons1)))
7231 ;; First occurrence of the name, start alist
7232 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7233 (if pack
7234 (setcar (cdr cperl-hierarchy)
7235 (cons cons1 (nth 1 cperl-hierarchy)))
7236 (setcar cperl-hierarchy
7237 (cons cons1 (car cperl-hierarchy)))))))
7238 (end-of-line))))
7240 (defun cperl-tags-hier-init (&optional update)
7241 "Show hierarchical menu of classes and methods.
7242 Finds info about classes by a scan of loaded TAGS files.
7243 Supposes that the TAGS files contain fully qualified function names.
7244 One may build such TAGS files from CPerl mode menu."
7245 (interactive)
7246 (require 'etags)
7247 (require 'imenu)
7248 (if (or update (null (nth 2 cperl-hierarchy)))
7249 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7250 (or (nthcdr 2 elt)
7251 ;; Only in one file
7252 (setcdr elt (cdr (nth 1 elt)))))))
7253 pack name cons1 to l1 l2 l3 l4 b)
7254 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7255 (setq cperl-hierarchy (list l1 l2 l3))
7256 (if cperl-xemacs-p ; Not checked
7257 (progn
7258 (or tags-file-name
7259 ;; Does this work in XEmacs?
7260 (call-interactively 'visit-tags-table))
7261 (message "Updating list of classes...")
7262 (set-buffer (get-file-buffer tags-file-name))
7263 (cperl-tags-hier-fill))
7264 (or tags-table-list
7265 (call-interactively 'visit-tags-table))
7266 (mapcar
7267 (function
7268 (lambda (tagsfile)
7269 (message "Updating list of classes... %s" tagsfile)
7270 (set-buffer (get-file-buffer tagsfile))
7271 (cperl-tags-hier-fill)))
7272 tags-table-list)
7273 (message "Updating list of classes... postprocessing..."))
7274 (mapcar remover (car cperl-hierarchy))
7275 (mapcar remover (nth 1 cperl-hierarchy))
7276 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7277 (cons "Methods: " (car cperl-hierarchy))))
7278 (cperl-tags-treeify to 1)
7279 (setcar (nthcdr 2 cperl-hierarchy)
7280 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7281 (message "Updating list of classes: done, requesting display...")
7282 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7284 (or (nth 2 cperl-hierarchy)
7285 (error "No items found"))
7286 (setq update
7287 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7288 (if (if (fboundp 'display-popup-menus-p)
7289 (let ((f 'display-popup-menus-p))
7290 (funcall f))
7291 window-system)
7292 (x-popup-menu t (nth 2 cperl-hierarchy))
7293 (require 'tmm)
7294 (tmm-prompt (nth 2 cperl-hierarchy))))
7295 (if (and update (listp update))
7296 (progn (while (cdr update) (setq update (cdr update)))
7297 (setq update (car update)))) ; Get the last from the list
7298 (if (vectorp update)
7299 (progn
7300 (find-file (elt update 0))
7301 (cperl-etags-goto-tag-location (elt update 1))))
7302 (if (eq update -999) (cperl-tags-hier-init t)))
7304 (defun cperl-tags-treeify (to level)
7305 ;; cadr of `to' is read-write. On start it is a cons
7306 (let* ((regexp (concat "^\\(" (mapconcat
7307 'identity
7308 (make-list level "[_a-zA-Z0-9]+")
7309 "::")
7310 "\\)\\(::\\)?"))
7311 (packages (cdr (nth 1 to)))
7312 (methods (cdr (nth 2 to)))
7313 l1 head tail cons1 cons2 ord writeto packs recurse
7314 root-packages root-functions ms many_ms same_name ps
7315 (move-deeper
7316 (function
7317 (lambda (elt)
7318 (cond ((and (string-match regexp (car elt))
7319 (or (eq ord 1) (match-end 2)))
7320 (setq head (substring (car elt) 0 (match-end 1))
7321 tail (if (match-end 2) (substring (car elt)
7322 (match-end 2)))
7323 recurse t)
7324 (if (setq cons1 (assoc head writeto)) nil
7325 ;; Need to init new head
7326 (setcdr writeto (cons (list head (list "Packages: ")
7327 (list "Methods: "))
7328 (cdr writeto)))
7329 (setq cons1 (nth 1 writeto)))
7330 (setq cons2 (nth ord cons1)) ; Either packs or meths
7331 (setcdr cons2 (cons elt (cdr cons2))))
7332 ((eq ord 2)
7333 (setq root-functions (cons elt root-functions)))
7335 (setq root-packages (cons elt root-packages))))))))
7336 (setcdr to l1) ; Init to dynamic space
7337 (setq writeto to)
7338 (setq ord 1)
7339 (mapcar move-deeper packages)
7340 (setq ord 2)
7341 (mapcar move-deeper methods)
7342 (if recurse
7343 (mapcar (function (lambda (elt)
7344 (cperl-tags-treeify elt (1+ level))))
7345 (cdr to)))
7346 ;;Now clean up leaders with one child only
7347 (mapcar (function (lambda (elt)
7348 (if (not (and (listp (cdr elt))
7349 (eq (length elt) 2))) nil
7350 (setcar elt (car (nth 1 elt)))
7351 (setcdr elt (cdr (nth 1 elt))))))
7352 (cdr to))
7353 ;; Sort the roots of subtrees
7354 (if (default-value 'imenu-sort-function)
7355 (setcdr to
7356 (sort (cdr to) (default-value 'imenu-sort-function))))
7357 ;; Now add back functions removed from display
7358 (mapcar (function (lambda (elt)
7359 (setcdr to (cons elt (cdr to)))))
7360 (if (default-value 'imenu-sort-function)
7361 (nreverse
7362 (sort root-functions (default-value 'imenu-sort-function)))
7363 root-functions))
7364 ;; Now add back packages removed from display
7365 (mapcar (function (lambda (elt)
7366 (setcdr to (cons (cons (concat "package " (car elt))
7367 (cdr elt))
7368 (cdr to)))))
7369 (if (default-value 'imenu-sort-function)
7370 (nreverse
7371 (sort root-packages (default-value 'imenu-sort-function)))
7372 root-packages))))
7374 ;;;(x-popup-menu t
7375 ;;; '(keymap "Name1"
7376 ;;; ("Ret1" "aa")
7377 ;;; ("Head1" "ab"
7378 ;;; keymap "Name2"
7379 ;;; ("Tail1" "x") ("Tail2" "y"))))
7381 (defun cperl-list-fold (list name limit)
7382 (let (list1 list2 elt1 (num 0))
7383 (if (<= (length list) limit) list
7384 (setq list1 nil list2 nil)
7385 (while list
7386 (setq num (1+ num)
7387 elt1 (car list)
7388 list (cdr list))
7389 (if (<= num imenu-max-items)
7390 (setq list2 (cons elt1 list2))
7391 (setq list1 (cons (cons name
7392 (nreverse list2))
7393 list1)
7394 list2 (list elt1)
7395 num 1)))
7396 (nreverse (cons (cons name
7397 (nreverse list2))
7398 list1)))))
7400 (defun cperl-menu-to-keymap (menu &optional name)
7401 (let (list)
7402 (cons 'keymap
7403 (mapcar
7404 (function
7405 (lambda (elt)
7406 (cond ((listp (cdr elt))
7407 (setq list (cperl-list-fold
7408 (cdr elt) (car elt) imenu-max-items))
7409 (cons nil
7410 (cons (car elt)
7411 (cperl-menu-to-keymap list))))
7413 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7414 (cperl-list-fold menu "Root" imenu-max-items)))))
7417 (defvar cperl-bad-style-regexp
7418 (mapconcat 'identity
7419 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7420 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7421 "\\|")
7422 "Finds places such that insertion of a whitespace may help a lot.")
7424 (defvar cperl-not-bad-style-regexp
7425 (mapconcat
7426 'identity
7427 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7428 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7429 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7430 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7431 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7432 "-[0-9]" ; -5
7433 "\\+\\+" ; ++var
7434 "--" ; --var
7435 ".->" ; a->b
7436 "->" ; a SPACE ->b
7437 "\\[-" ; a[-1]
7438 "\\\\[&$@*\\\\]" ; \&func
7439 "^=" ; =head
7440 "\\$." ; $|
7441 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7442 "||"
7443 "&&"
7444 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7445 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7446 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7447 ;;"[*/+-|&<.]+="
7449 "\\|")
7450 "If matches at the start of match found by `my-bad-c-style-regexp',
7451 insertion of a whitespace will not help.")
7453 (defvar found-bad)
7455 (defun cperl-find-bad-style ()
7456 "Find places in the buffer where insertion of a whitespace may help.
7457 Prompts user for insertion of spaces.
7458 Currently it is tuned to C and Perl syntax."
7459 (interactive)
7460 (let (found-bad (p (point)))
7461 (setq last-nonmenu-event 13) ; To disable popup
7462 (goto-char (point-min))
7463 (map-y-or-n-p "Insert space here? "
7464 (lambda (arg) (insert " "))
7465 'cperl-next-bad-style
7466 '("location" "locations" "insert a space into")
7467 '((?\C-r (lambda (arg)
7468 (let ((buffer-quit-function
7469 'exit-recursive-edit))
7470 (message "Exit with Esc Esc")
7471 (recursive-edit)
7472 t)) ; Consider acted upon
7473 "edit, exit with Esc Esc")
7474 (?e (lambda (arg)
7475 (let ((buffer-quit-function
7476 'exit-recursive-edit))
7477 (message "Exit with Esc Esc")
7478 (recursive-edit)
7479 t)) ; Consider acted upon
7480 "edit, exit with Esc Esc"))
7482 (if found-bad (goto-char found-bad)
7483 (goto-char p)
7484 (message "No appropriate place found"))))
7486 (defun cperl-next-bad-style ()
7487 (let (p (not-found t) (point (point)) found)
7488 (while (and not-found
7489 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7490 (setq p (point))
7491 (goto-char (match-beginning 0))
7492 (if (or
7493 (looking-at cperl-not-bad-style-regexp)
7494 ;; Check for a < -b and friends
7495 (and (eq (following-char) ?\-)
7496 (save-excursion
7497 (skip-chars-backward " \t\n")
7498 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7499 ;; Now check for syntax type
7500 (save-match-data
7501 (setq found (point))
7502 (beginning-of-defun)
7503 (let ((pps (parse-partial-sexp (point) found)))
7504 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7505 (goto-char (match-end 0))
7506 (goto-char (1- p))
7507 (setq not-found nil
7508 found-bad found)))
7509 (not not-found)))
7512 ;;; Getting help
7513 (defvar cperl-have-help-regexp
7514 ;;(concat "\\("
7515 (mapconcat
7516 'identity
7517 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7518 "[$@]\\^[a-zA-Z]" ; Special variable
7519 "[$@][^ \n\t]" ; Special variable
7520 "-[a-zA-Z]" ; File test
7521 "\\\\[a-zA-Z0]" ; Special chars
7522 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7523 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7524 "[a-zA-Z_0-9:]+" ; symbol or number
7525 "x="
7526 "#!")
7527 ;;"\\)\\|\\("
7528 "\\|")
7529 ;;"\\)"
7531 "Matches places in the buffer we can find help for.")
7533 (defvar cperl-message-on-help-error t)
7534 (defvar cperl-help-from-timer nil)
7536 (defun cperl-word-at-point-hard ()
7537 ;; Does not save-excursion
7538 ;; Get to the something meaningful
7539 (or (eobp) (eolp) (forward-char 1))
7540 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7541 (save-excursion (beginning-of-line) (point))
7542 'to-beg)
7543 ;; (cond
7544 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7545 ;; (skip-chars-backward " \n\t\r({[]});,")
7546 ;; (or (bobp) (backward-char 1))))
7547 ;; Try to backtrace
7548 (cond
7549 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7550 (skip-chars-backward "a-zA-Z0-9_:")
7551 (cond
7552 ((and (eq (preceding-char) ?^) ; $^I
7553 (eq (char-after (- (point) 2)) ?\$))
7554 (forward-char -2))
7555 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7556 (forward-char -1))
7557 ((and (eq (preceding-char) ?\=)
7558 (eq (current-column) 1))
7559 (forward-char -1))) ; =head1
7560 (if (and (eq (preceding-char) ?\<)
7561 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7562 (forward-char -1)))
7563 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7564 (forward-char -1))
7565 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7566 (forward-char -1))
7567 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7568 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7569 (cond
7570 ((and (eq (preceding-char) ?\$)
7571 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7572 (forward-char -1))
7573 ((and (eq (following-char) ?\>)
7574 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7575 (save-excursion
7576 (forward-sexp -1)
7577 (and (eq (preceding-char) ?\<)
7578 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7579 (search-backward "<"))))
7580 ((and (eq (following-char) ?\$)
7581 (eq (preceding-char) ?\<)
7582 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7583 (forward-char -1)))
7584 (if (looking-at cperl-have-help-regexp)
7585 (buffer-substring (match-beginning 0) (match-end 0))))
7587 (defun cperl-get-help ()
7588 "Get one-line docs on the symbol at the point.
7589 The data for these docs is a little bit obsolete and may be in fact longer
7590 than a line. Your contribution to update/shorten it is appreciated."
7591 (interactive)
7592 (save-match-data ; May be called "inside" query-replace
7593 (save-excursion
7594 (let ((word (cperl-word-at-point-hard)))
7595 (if word
7596 (if (and cperl-help-from-timer ; Bail out if not in mainland
7597 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7598 (or (memq (get-text-property (point) 'face)
7599 '(font-lock-comment-face font-lock-string-face))
7600 (memq (get-text-property (point) 'syntax-type)
7601 '(pod here-doc format))))
7603 (cperl-describe-perl-symbol word))
7604 (if cperl-message-on-help-error
7605 (message "Nothing found for %s..."
7606 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7608 ;;; Stolen from perl-descr.el by Johan Vromans:
7610 (defvar cperl-doc-buffer " *perl-doc*"
7611 "Where the documentation can be found.")
7613 (defun cperl-describe-perl-symbol (val)
7614 "Display the documentation of symbol at point, a Perl operator."
7615 (let ((enable-recursive-minibuffers t)
7616 args-file regexp)
7617 (cond
7618 ((string-match "^[&*][a-zA-Z_]" val)
7619 (setq val (concat (substring val 0 1) "NAME")))
7620 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7621 (setq val (concat "@" (substring val 1 (match-end 1)))))
7622 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7623 (setq val (concat "%" (substring val 1 (match-end 1)))))
7624 ((and (string= val "x") (string-match "^x=" val))
7625 (setq val "x="))
7626 ((string-match "^\\$[\C-a-\C-z]" val)
7627 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7628 ((string-match "^CORE::" val)
7629 (setq val "CORE::"))
7630 ((string-match "^SUPER::" val)
7631 (setq val "SUPER::"))
7632 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7633 (setq val "<NAME>")))
7634 (setq regexp (concat "^"
7635 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7636 (regexp-quote val)
7637 "\\([ \t([/]\\|$\\)"))
7639 ;; get the buffer with the documentation text
7640 (cperl-switch-to-doc-buffer)
7642 ;; lookup in the doc
7643 (goto-char (point-min))
7644 (let ((case-fold-search nil))
7645 (list
7646 (if (re-search-forward regexp (point-max) t)
7647 (save-excursion
7648 (beginning-of-line 1)
7649 (let ((lnstart (point)))
7650 (end-of-line)
7651 (message "%s" (buffer-substring lnstart (point)))))
7652 (if cperl-message-on-help-error
7653 (message "No definition for %s" val)))))))
7655 (defvar cperl-short-docs 'please-ignore-this-line
7656 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7657 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7658 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7659 ! ... Logical negation.
7660 ... != ... Numeric inequality.
7661 ... !~ ... Search pattern, substitution, or translation (negated).
7662 $! In numeric context: errno. In a string context: error string.
7663 $\" The separator which joins elements of arrays interpolated in strings.
7664 $# The output format for printed numbers. Default is %.15g or close.
7665 $$ Process number of this script. Changes in the fork()ed child process.
7666 $% The current page number of the currently selected output channel.
7668 The following variables are always local to the current block:
7670 $1 Match of the 1st set of parentheses in the last match (auto-local).
7671 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7672 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7673 $4 Match of the 4th set of parentheses in the last match (auto-local).
7674 $5 Match of the 5th set of parentheses in the last match (auto-local).
7675 $6 Match of the 6th set of parentheses in the last match (auto-local).
7676 $7 Match of the 7th set of parentheses in the last match (auto-local).
7677 $8 Match of the 8th set of parentheses in the last match (auto-local).
7678 $9 Match of the 9th set of parentheses in the last match (auto-local).
7679 $& The string matched by the last pattern match (auto-local).
7680 $' The string after what was matched by the last match (auto-local).
7681 $` The string before what was matched by the last match (auto-local).
7683 $( The real gid of this process.
7684 $) The effective gid of this process.
7685 $* Deprecated: Set to 1 to do multiline matching within a string.
7686 $+ The last bracket matched by the last search pattern.
7687 $, The output field separator for the print operator.
7688 $- The number of lines left on the page.
7689 $. The current input line number of the last filehandle that was read.
7690 $/ The input record separator, newline by default.
7691 $0 Name of the file containing the current perl script (read/write).
7692 $: String may be broken after these characters to fill ^-lines in a format.
7693 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7694 $< The real uid of this process.
7695 $= The page length of the current output channel. Default is 60 lines.
7696 $> The effective uid of this process.
7697 $? The status returned by the last ``, pipe close or `system'.
7698 $@ The perl error message from the last eval or do @var{EXPR} command.
7699 $ARGV The name of the current file used with <> .
7700 $[ Deprecated: The index of the first element/char in an array/string.
7701 $\\ The output record separator for the print operator.
7702 $] The perl version string as displayed with perl -v.
7703 $^ The name of the current top-of-page format.
7704 $^A The current value of the write() accumulator for format() lines.
7705 $^D The value of the perl debug (-D) flags.
7706 $^E Information about the last system error other than that provided by $!.
7707 $^F The highest system file descriptor, ordinarily 2.
7708 $^H The current set of syntax checks enabled by `use strict'.
7709 $^I The value of the in-place edit extension (perl -i option).
7710 $^L What formats output to perform a formfeed. Default is \\f.
7711 $^M A buffer for emergency memory allocation when running out of memory.
7712 $^O The operating system name under which this copy of Perl was built.
7713 $^P Internal debugging flag.
7714 $^T The time the script was started. Used by -A/-M/-C file tests.
7715 $^W True if warnings are requested (perl -w flag).
7716 $^X The name under which perl was invoked (argv[0] in C-speech).
7717 $_ The default input and pattern-searching space.
7718 $| Auto-flush after write/print on current output channel? Default 0.
7719 $~ The name of the current report format.
7720 ... % ... Modulo division.
7721 ... %= ... Modulo division assignment.
7722 %ENV Contains the current environment.
7723 %INC List of files that have been require-d or do-ne.
7724 %SIG Used to set signal handlers for various signals.
7725 ... & ... Bitwise and.
7726 ... && ... Logical and.
7727 ... &&= ... Logical and assignment.
7728 ... &= ... Bitwise and assignment.
7729 ... * ... Multiplication.
7730 ... ** ... Exponentiation.
7731 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7732 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7733 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7734 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7735 ... += ... Addition assignment.
7736 , Comma operator.
7737 ... - ... Subtraction.
7738 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7739 ... -= ... Subtraction assignment.
7740 -A Access time in days since script started.
7741 -B File is a non-text (binary) file.
7742 -C Inode change time in days since script started.
7743 -M Age in days since script started.
7744 -O File is owned by real uid.
7745 -R File is readable by real uid.
7746 -S File is a socket .
7747 -T File is a text file.
7748 -W File is writable by real uid.
7749 -X File is executable by real uid.
7750 -b File is a block special file.
7751 -c File is a character special file.
7752 -d File is a directory.
7753 -e File exists .
7754 -f File is a plain file.
7755 -g File has setgid bit set.
7756 -k File has sticky bit set.
7757 -l File is a symbolic link.
7758 -o File is owned by effective uid.
7759 -p File is a named pipe (FIFO).
7760 -r File is readable by effective uid.
7761 -s File has non-zero size.
7762 -t Tests if filehandle (STDIN by default) is opened to a tty.
7763 -u File has setuid bit set.
7764 -w File is writable by effective uid.
7765 -x File is executable by effective uid.
7766 -z File has zero size.
7767 . Concatenate strings.
7768 .. Range (list context); flip/flop (scalar context) operator.
7769 .= Concatenate assignment strings
7770 ... / ... Division. /PATTERN/ioxsmg Pattern match
7771 ... /= ... Division assignment.
7772 /PATTERN/ioxsmg Pattern match.
7773 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7774 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7775 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7776 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7777 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7778 ... <= ... Numeric less than or equal to.
7779 ... <=> ... Numeric compare.
7780 ... = ... Assignment.
7781 ... == ... Numeric equality.
7782 ... =~ ... Search pattern, substitution, or translation
7783 ... > ... Numeric greater than.
7784 ... >= ... Numeric greater than or equal to.
7785 ... >> ... Bitwise shift right.
7786 ... >>= ... Bitwise shift right assignment.
7787 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7788 ?PATTERN? One-time pattern match.
7789 @ARGV Command line arguments (not including the command name - see $0).
7790 @INC List of places to look for perl scripts during do/include/use.
7791 @_ Parameter array for subroutines; result of split() unless in list context.
7792 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7793 \\0 Octal char, e.g. \\033.
7794 \\E Case modification terminator. See \\Q, \\L, and \\U.
7795 \\L Lowercase until \\E . See also \\l, lc.
7796 \\U Upcase until \\E . See also \\u, uc.
7797 \\Q Quote metacharacters until \\E . See also quotemeta.
7798 \\a Alarm character (octal 007).
7799 \\b Backspace character (octal 010).
7800 \\c Control character, e.g. \\c[ .
7801 \\e Escape character (octal 033).
7802 \\f Formfeed character (octal 014).
7803 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7804 \\n Newline character (octal 012 on most systems).
7805 \\r Return character (octal 015 on most systems).
7806 \\t Tab character (octal 011).
7807 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7808 \\x Hex character, e.g. \\x1b.
7809 ... ^ ... Bitwise exclusive or.
7810 __END__ Ends program source.
7811 __DATA__ Ends program source.
7812 __FILE__ Current (source) filename.
7813 __LINE__ Current line in current source.
7814 __PACKAGE__ Current package.
7815 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7816 ARGVOUT Output filehandle with -i flag.
7817 BEGIN { ... } Immediately executed (during compilation) piece of code.
7818 END { ... } Pseudo-subroutine executed after the script finishes.
7819 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7820 INIT { ... } Pseudo-subroutine executed before the script starts running.
7821 DATA Input filehandle for what follows after __END__ or __DATA__.
7822 accept(NEWSOCKET,GENERICSOCKET)
7823 alarm(SECONDS)
7824 atan2(X,Y)
7825 bind(SOCKET,NAME)
7826 binmode(FILEHANDLE)
7827 caller[(LEVEL)]
7828 chdir(EXPR)
7829 chmod(LIST)
7830 chop[(LIST|VAR)]
7831 chown(LIST)
7832 chroot(FILENAME)
7833 close(FILEHANDLE)
7834 closedir(DIRHANDLE)
7835 ... cmp ... String compare.
7836 connect(SOCKET,NAME)
7837 continue of { block } continue { block }. Is executed after `next' or at end.
7838 cos(EXPR)
7839 crypt(PLAINTEXT,SALT)
7840 dbmclose(%HASH)
7841 dbmopen(%HASH,DBNAME,MODE)
7842 defined(EXPR)
7843 delete($HASH{KEY})
7844 die(LIST)
7845 do { ... }|SUBR while|until EXPR executes at least once
7846 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7847 dump LABEL
7848 each(%HASH)
7849 endgrent
7850 endhostent
7851 endnetent
7852 endprotoent
7853 endpwent
7854 endservent
7855 eof[([FILEHANDLE])]
7856 ... eq ... String equality.
7857 eval(EXPR) or eval { BLOCK }
7858 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7859 exit(EXPR)
7860 exp(EXPR)
7861 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7862 fileno(FILEHANDLE)
7863 flock(FILEHANDLE,OPERATION)
7864 for (EXPR;EXPR;EXPR) { ... }
7865 foreach [VAR] (@ARRAY) { ... }
7866 fork
7867 ... ge ... String greater than or equal.
7868 getc[(FILEHANDLE)]
7869 getgrent
7870 getgrgid(GID)
7871 getgrnam(NAME)
7872 gethostbyaddr(ADDR,ADDRTYPE)
7873 gethostbyname(NAME)
7874 gethostent
7875 getlogin
7876 getnetbyaddr(ADDR,ADDRTYPE)
7877 getnetbyname(NAME)
7878 getnetent
7879 getpeername(SOCKET)
7880 getpgrp(PID)
7881 getppid
7882 getpriority(WHICH,WHO)
7883 getprotobyname(NAME)
7884 getprotobynumber(NUMBER)
7885 getprotoent
7886 getpwent
7887 getpwnam(NAME)
7888 getpwuid(UID)
7889 getservbyname(NAME,PROTO)
7890 getservbyport(PORT,PROTO)
7891 getservent
7892 getsockname(SOCKET)
7893 getsockopt(SOCKET,LEVEL,OPTNAME)
7894 gmtime(EXPR)
7895 goto LABEL
7896 ... gt ... String greater than.
7897 hex(EXPR)
7898 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7899 index(STR,SUBSTR[,OFFSET])
7900 int(EXPR)
7901 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7902 join(EXPR,LIST)
7903 keys(%HASH)
7904 kill(LIST)
7905 last [LABEL]
7906 ... le ... String less than or equal.
7907 length(EXPR)
7908 link(OLDFILE,NEWFILE)
7909 listen(SOCKET,QUEUESIZE)
7910 local(LIST)
7911 localtime(EXPR)
7912 log(EXPR)
7913 lstat(EXPR|FILEHANDLE|VAR)
7914 ... lt ... String less than.
7915 m/PATTERN/iogsmx
7916 mkdir(FILENAME,MODE)
7917 msgctl(ID,CMD,ARG)
7918 msgget(KEY,FLAGS)
7919 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7920 msgsnd(ID,MSG,FLAGS)
7921 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7922 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7923 ... ne ... String inequality.
7924 next [LABEL]
7925 oct(EXPR)
7926 open(FILEHANDLE[,EXPR])
7927 opendir(DIRHANDLE,EXPR)
7928 ord(EXPR) ASCII value of the first char of the string.
7929 pack(TEMPLATE,LIST)
7930 package NAME Introduces package context.
7931 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7932 pop(ARRAY)
7933 print [FILEHANDLE] [(LIST)]
7934 printf [FILEHANDLE] (FORMAT,LIST)
7935 push(ARRAY,LIST)
7936 q/STRING/ Synonym for 'STRING'
7937 qq/STRING/ Synonym for \"STRING\"
7938 qx/STRING/ Synonym for `STRING`
7939 rand[(EXPR)]
7940 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7941 readdir(DIRHANDLE)
7942 readlink(EXPR)
7943 recv(SOCKET,SCALAR,LEN,FLAGS)
7944 redo [LABEL]
7945 rename(OLDNAME,NEWNAME)
7946 require [FILENAME | PERL_VERSION]
7947 reset[(EXPR)]
7948 return(LIST)
7949 reverse(LIST)
7950 rewinddir(DIRHANDLE)
7951 rindex(STR,SUBSTR[,OFFSET])
7952 rmdir(FILENAME)
7953 s/PATTERN/REPLACEMENT/gieoxsm
7954 scalar(EXPR)
7955 seek(FILEHANDLE,POSITION,WHENCE)
7956 seekdir(DIRHANDLE,POS)
7957 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7958 semctl(ID,SEMNUM,CMD,ARG)
7959 semget(KEY,NSEMS,SIZE,FLAGS)
7960 semop(KEY,...)
7961 send(SOCKET,MSG,FLAGS[,TO])
7962 setgrent
7963 sethostent(STAYOPEN)
7964 setnetent(STAYOPEN)
7965 setpgrp(PID,PGRP)
7966 setpriority(WHICH,WHO,PRIORITY)
7967 setprotoent(STAYOPEN)
7968 setpwent
7969 setservent(STAYOPEN)
7970 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7971 shift[(ARRAY)]
7972 shmctl(ID,CMD,ARG)
7973 shmget(KEY,SIZE,FLAGS)
7974 shmread(ID,VAR,POS,SIZE)
7975 shmwrite(ID,STRING,POS,SIZE)
7976 shutdown(SOCKET,HOW)
7977 sin(EXPR)
7978 sleep[(EXPR)]
7979 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7980 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7981 sort [SUBROUTINE] (LIST)
7982 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7983 split[(/PATTERN/[,EXPR[,LIMIT]])]
7984 sprintf(FORMAT,LIST)
7985 sqrt(EXPR)
7986 srand(EXPR)
7987 stat(EXPR|FILEHANDLE|VAR)
7988 study[(SCALAR)]
7989 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7990 substr(EXPR,OFFSET[,LEN])
7991 symlink(OLDFILE,NEWFILE)
7992 syscall(LIST)
7993 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7994 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7995 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7996 tell[(FILEHANDLE)]
7997 telldir(DIRHANDLE)
7998 time
7999 times
8000 tr/SEARCHLIST/REPLACEMENTLIST/cds
8001 truncate(FILE|EXPR,LENGTH)
8002 umask[(EXPR)]
8003 undef[(EXPR)]
8004 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
8005 unlink(LIST)
8006 unpack(TEMPLATE,EXPR)
8007 unshift(ARRAY,LIST)
8008 until (EXPR) { ... } EXPR until EXPR
8009 utime(LIST)
8010 values(%HASH)
8011 vec(EXPR,OFFSET,BITS)
8012 wait
8013 waitpid(PID,FLAGS)
8014 wantarray Returns true if the sub/eval is called in list context.
8015 warn(LIST)
8016 while (EXPR) { ... } EXPR while EXPR
8017 write[(EXPR|FILEHANDLE)]
8018 ... x ... Repeat string or array.
8019 x= ... Repetition assignment.
8020 y/SEARCHLIST/REPLACEMENTLIST/
8021 ... | ... Bitwise or.
8022 ... || ... Logical or.
8023 ~ ... Unary bitwise complement.
8024 #! OS interpreter indicator. If contains `perl', used for options, and -x.
8025 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
8026 CORE:: Prefix to access builtin function if imported sub obscures it.
8027 SUPER:: Prefix to lookup for a method in @ISA classes.
8028 DESTROY Shorthand for `sub DESTROY {...}'.
8029 ... EQ ... Obsolete synonym of `eq'.
8030 ... GE ... Obsolete synonym of `ge'.
8031 ... GT ... Obsolete synonym of `gt'.
8032 ... LE ... Obsolete synonym of `le'.
8033 ... LT ... Obsolete synonym of `lt'.
8034 ... NE ... Obsolete synonym of `ne'.
8035 abs [ EXPR ] absolute value
8036 ... and ... Low-precedence synonym for &&.
8037 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
8038 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
8039 chr Converts a number to char with the same ordinal.
8040 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
8041 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
8042 exists $HASH{KEY} True if the key exists.
8043 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
8044 formline PICTURE, LIST Backdoor into \"format\" processing.
8045 glob EXPR Synonym of <EXPR>.
8046 lc [ EXPR ] Returns lowercased EXPR.
8047 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
8048 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
8049 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
8050 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
8051 not ... Low-precedence synonym for ! - negation.
8052 ... or ... Low-precedence synonym for ||.
8053 pos STRING Set/Get end-position of the last match over this string, see \\G.
8054 quotemeta [ EXPR ] Quote regexp metacharacters.
8055 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
8056 readline FH Synonym of <FH>.
8057 readpipe CMD Synonym of `CMD`.
8058 ref [ EXPR ] Type of EXPR when dereferenced.
8059 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
8060 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
8061 tied Returns internal object for a tied data.
8062 uc [ EXPR ] Returns upcased EXPR.
8063 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
8064 untie VAR Unlink an object from a simple Perl variable.
8065 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
8066 ... xor ... Low-precedence synonym for exclusive or.
8067 prototype \\&SUB Returns prototype of the function given a reference.
8068 =head1 Top-level heading.
8069 =head2 Second-level heading.
8070 =head3 Third-level heading (is there such?).
8071 =over [ NUMBER ] Start list.
8072 =item [ TITLE ] Start new item in the list.
8073 =back End list.
8074 =cut Switch from POD to Perl.
8075 =pod Switch from Perl to POD.
8078 (defun cperl-switch-to-doc-buffer (&optional interactive)
8079 "Go to the perl documentation buffer and insert the documentation."
8080 (interactive "p")
8081 (let ((buf (get-buffer-create cperl-doc-buffer)))
8082 (if interactive
8083 (switch-to-buffer-other-window buf)
8084 (set-buffer buf))
8085 (if (= (buffer-size) 0)
8086 (progn
8087 (insert (documentation-property 'cperl-short-docs
8088 'variable-documentation))
8089 (setq buffer-read-only t)))))
8091 (defun cperl-beautify-regexp-piece (b e embed level)
8092 ;; b is before the starting delimiter, e before the ending
8093 ;; e should be a marker, may be changed, but remains "correct".
8094 ;; EMBED is nil iff we process the whole REx.
8095 ;; The REx is guaranteed to have //x
8096 ;; LEVEL shows how many levels deep to go
8097 ;; position at enter and at leave is not defined
8098 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
8099 (if (not embed)
8100 (goto-char (1+ b))
8101 (goto-char b)
8102 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
8103 (forward-char 2)
8104 (delete-char 1)
8105 (forward-char 1))
8106 ((looking-at "(\\?[^a-zA-Z]")
8107 (forward-char 3))
8108 ((looking-at "(\\?") ; (?i)
8109 (forward-char 2))
8111 (forward-char 1))))
8112 (setq c (if embed (current-indentation) (1- (current-column)))
8113 c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
8114 (or (looking-at "[ \t]*[\n#]")
8115 (progn
8116 (insert "\n")))
8117 (goto-char e)
8118 (beginning-of-line)
8119 (if (re-search-forward "[^ \t]" e t)
8120 (progn ; Something before the ending delimiter
8121 (goto-char e)
8122 (delete-horizontal-space)
8123 (insert "\n")
8124 (cperl-make-indent c)
8125 (set-marker e (point))))
8126 (goto-char b)
8127 (end-of-line 2)
8128 (while (< (point) (marker-position e))
8129 (beginning-of-line)
8130 (setq s (point)
8131 inline t)
8132 (skip-chars-forward " \t")
8133 (delete-region s (point))
8134 (cperl-make-indent c1)
8135 (while (and
8136 inline
8137 (looking-at
8138 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8139 "\\|" ; Embedded variable
8140 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8141 "\\|" ; $ ^
8142 "[$^]"
8143 "\\|" ; simple-code simple-code*?
8144 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8145 "\\|" ; Class
8146 "\\(\\[\\)" ; 6
8147 "\\|" ; Grouping
8148 "\\((\\(\\?\\)?\\)" ; 7 8
8149 "\\|" ; |
8150 "\\(|\\)"))) ; 9
8151 (goto-char (match-end 0))
8152 (setq spaces t)
8153 (cond ((match-beginning 1) ; Alphanum word + junk
8154 (forward-char -1))
8155 ((or (match-beginning 3) ; $ab[12]
8156 (and (match-beginning 5) ; X* X+ X{2,3}
8157 (eq (preceding-char) ?\{)))
8158 (forward-char -1)
8159 (forward-sexp 1))
8160 ((and ; [], already syntaxified
8161 (match-beginning 6)
8162 cperl-regexp-scan
8163 cperl-use-syntax-table-text-property)
8164 (forward-char -1)
8165 (forward-sexp 1)
8166 (or (eq (preceding-char) ?\])
8167 (error "[]-group not terminated"))
8168 (re-search-forward
8169 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8170 ((match-beginning 6) ; []
8171 (setq tmp (point))
8172 (if (looking-at "\\^?\\]")
8173 (goto-char (match-end 0)))
8174 ;; XXXX POSIX classes?!
8175 (while (and (not pos)
8176 (re-search-forward "\\[:\\|\\]" e t))
8177 (if (eq (preceding-char) ?:)
8178 (or (re-search-forward ":\\]" e t)
8179 (error "[:POSIX:]-group in []-group not terminated"))
8180 (setq pos t)))
8181 (or (eq (preceding-char) ?\])
8182 (error "[]-group not terminated"))
8183 (re-search-forward
8184 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8185 ((match-beginning 7) ; ()
8186 (goto-char (match-beginning 0))
8187 (setq pos (current-column))
8188 (or (eq pos c1)
8189 (progn
8190 (delete-horizontal-space)
8191 (insert "\n")
8192 (cperl-make-indent c1)))
8193 (setq tmp (point))
8194 (forward-sexp 1)
8195 ;; (or (forward-sexp 1)
8196 ;; (progn
8197 ;; (goto-char tmp)
8198 ;; (error "()-group not terminated")))
8199 (set-marker m (1- (point)))
8200 (set-marker m1 (point))
8201 (if (= level 1)
8202 (if (progn ; indent rigidly if multiline
8203 ;; In fact does not make a lot of sense, since
8204 ;; the starting position can be already lost due
8205 ;; to insertion of "\n" and " "
8206 (goto-char tmp)
8207 (search-forward "\n" m1 t))
8208 (indent-rigidly (point) m1 (- c1 pos)))
8209 (setq level (1- level))
8210 (cond
8211 ((not (match-beginning 8))
8212 (cperl-beautify-regexp-piece tmp m t level))
8213 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8215 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8216 (goto-char (+ 2 tmp))
8217 (forward-sexp 1)
8218 (cperl-beautify-regexp-piece (point) m t level))
8219 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8220 (goto-char (+ 3 tmp))
8221 (cperl-beautify-regexp-piece (point) m t level))
8223 (cperl-beautify-regexp-piece tmp m t level))))
8224 (goto-char m1)
8225 (cond ((looking-at "[*+?]\\??")
8226 (goto-char (match-end 0)))
8227 ((eq (following-char) ?\{)
8228 (forward-sexp 1)
8229 (if (eq (following-char) ?\?)
8230 (forward-char))))
8231 (skip-chars-forward " \t")
8232 (setq spaces nil)
8233 (if (looking-at "[#\n]")
8234 (progn
8235 (or (eolp) (indent-for-comment))
8236 (beginning-of-line 2))
8237 (delete-horizontal-space)
8238 (insert "\n"))
8239 (end-of-line)
8240 (setq inline nil))
8241 ((match-beginning 9) ; |
8242 (forward-char -1)
8243 (setq tmp (point))
8244 (beginning-of-line)
8245 (if (re-search-forward "[^ \t]" tmp t)
8246 (progn
8247 (goto-char tmp)
8248 (delete-horizontal-space)
8249 (insert "\n"))
8250 ;; first at line
8251 (delete-region (point) tmp))
8252 (cperl-make-indent c)
8253 (forward-char 1)
8254 (skip-chars-forward " \t")
8255 (setq spaces nil)
8256 (if (looking-at "[#\n]")
8257 (beginning-of-line 2)
8258 (delete-horizontal-space)
8259 (insert "\n"))
8260 (end-of-line)
8261 (setq inline nil)))
8262 (or (looking-at "[ \t\n]")
8263 (not spaces)
8264 (insert " "))
8265 (skip-chars-forward " \t"))
8266 (or (looking-at "[#\n]")
8267 (error "Unknown code `%s' in a regexp"
8268 (buffer-substring (point) (1+ (point)))))
8269 (and inline (end-of-line 2)))
8270 ;; Special-case the last line of group
8271 (if (and (>= (point) (marker-position e))
8272 (/= (current-indentation) c))
8273 (progn
8274 (beginning-of-line)
8275 (cperl-make-indent c)))))
8277 (defun cperl-make-regexp-x ()
8278 ;; Returns position of the start
8279 ;; XXX this is called too often! Need to cache the result!
8280 (save-excursion
8281 (or cperl-use-syntax-table-text-property
8282 (error "I need to have a regexp marked!"))
8283 ;; Find the start
8284 (if (looking-at "\\s|")
8285 nil ; good already
8286 (if (looking-at "\\([smy]\\|qr\\)\\s|")
8287 (forward-char 1)
8288 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8289 ;;(forward-char 1)
8290 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8291 (sub-p (eq (preceding-char) ?s)) s)
8292 (forward-sexp 1)
8293 (set-marker e (1- (point)))
8294 (setq delim (preceding-char))
8295 (if (and sub-p (eq delim (char-after (- (point) 2))))
8296 (error "Possible s/blah// - do not know how to deal with"))
8297 (if sub-p (forward-sexp 1))
8298 (if (looking-at "\\sw*x")
8299 (setq have-x t)
8300 (insert "x"))
8301 ;; Protect fragile " ", "#"
8302 (if have-x nil
8303 (goto-char (1+ b))
8304 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8305 (forward-char -1)
8306 (insert "\\")
8307 (forward-char 1)))
8308 b)))
8310 (defun cperl-beautify-regexp (&optional deep)
8311 "Do it. (Experimental, may change semantics, recheck the result.)
8312 We suppose that the regexp is scanned already."
8313 (interactive "P")
8314 (setq deep (if deep (prefix-numeric-value deep) -1))
8315 (save-excursion
8316 (goto-char (cperl-make-regexp-x))
8317 (let ((b (point)) (e (make-marker)))
8318 (forward-sexp 1)
8319 (set-marker e (1- (point)))
8320 (cperl-beautify-regexp-piece b e nil deep))))
8322 (defun cperl-regext-to-level-start ()
8323 "Goto start of an enclosing group in regexp.
8324 We suppose that the regexp is scanned already."
8325 (interactive)
8326 (let ((limit (cperl-make-regexp-x)) done)
8327 (while (not done)
8328 (or (eq (following-char) ?\()
8329 (search-backward "(" (1+ limit) t)
8330 (error "Cannot find `(' which starts a group"))
8331 (setq done
8332 (save-excursion
8333 (skip-chars-backward "\\")
8334 (looking-at "\\(\\\\\\\\\\)*(")))
8335 (or done (forward-char -1)))))
8337 (defun cperl-contract-level ()
8338 "Find an enclosing group in regexp and contract it.
8339 \(Experimental, may change semantics, recheck the result.)
8340 We suppose that the regexp is scanned already."
8341 (interactive)
8342 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8343 (cperl-regext-to-level-start)
8344 (let ((b (point)) (e (make-marker)) c)
8345 (forward-sexp 1)
8346 (set-marker e (1- (point)))
8347 (goto-char b)
8348 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8349 (cond
8350 ((match-beginning 1) ; #-comment
8351 (or c (setq c (current-indentation)))
8352 (beginning-of-line 2) ; Skip
8353 (cperl-make-indent c))
8355 (delete-char -1)
8356 (just-one-space))))))
8358 (defun cperl-contract-levels ()
8359 "Find an enclosing group in regexp and contract all the kids.
8360 \(Experimental, may change semantics, recheck the result.)
8361 We suppose that the regexp is scanned already."
8362 (interactive)
8363 (save-excursion
8364 (condition-case nil
8365 (cperl-regext-to-level-start)
8366 (error ; We are outside outermost group
8367 (goto-char (cperl-make-regexp-x))))
8368 (let ((b (point)) (e (make-marker)) s c)
8369 (forward-sexp 1)
8370 (set-marker e (1- (point)))
8371 (goto-char (1+ b))
8372 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8373 (cond
8374 ((match-beginning 1) ; Skip
8375 nil)
8376 (t ; Group
8377 (cperl-contract-level)))))))
8379 (defun cperl-beautify-level (&optional deep)
8380 "Find an enclosing group in regexp and beautify it.
8381 \(Experimental, may change semantics, recheck the result.)
8382 We suppose that the regexp is scanned already."
8383 (interactive "P")
8384 (setq deep (if deep (prefix-numeric-value deep) -1))
8385 (save-excursion
8386 (cperl-regext-to-level-start)
8387 (let ((b (point)) (e (make-marker)))
8388 (forward-sexp 1)
8389 (set-marker e (1- (point)))
8390 (cperl-beautify-regexp-piece b e nil deep))))
8392 (defun cperl-invert-if-unless-modifiers ()
8393 "Change `B if A;' into `if (A) {B}' etc if possible.
8394 \(Unfinished.)"
8395 (interactive) ;
8396 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8397 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8398 (and (= (char-syntax (preceding-char)) ?w)
8399 (forward-sexp -1))
8400 (setq pre-if (point))
8401 (cperl-backward-to-start-of-expr)
8402 (setq pre-B (point))
8403 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8404 (cperl-forward-to-end-of-expr)
8405 (setq post-A (point))
8406 (goto-char pre-if)
8407 (or (looking-at w-rex)
8408 ;; Find the position
8409 (progn (goto-char post-A)
8410 (while (and
8411 (not (looking-at w-rex))
8412 (> (point) pre-B))
8413 (forward-sexp -1))
8414 (setq pre-if (point))))
8415 (or (looking-at w-rex)
8416 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8417 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8418 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8419 ;; First, simple part: find code boundaries
8420 (forward-sexp 1)
8421 (setq post-if (point))
8422 (forward-sexp -2)
8423 (forward-sexp 1)
8424 (setq post-B (point))
8425 (cperl-backward-to-start-of-expr)
8426 (setq pre-B (point))
8427 (setq B (buffer-substring pre-B post-B))
8428 (goto-char pre-if)
8429 (forward-sexp 2)
8430 (forward-sexp -1)
8431 ;; May be after $, @, $# etc of a variable
8432 (skip-chars-backward "$@%#")
8433 (setq pre-A (point))
8434 (cperl-forward-to-end-of-expr)
8435 (setq post-A (point))
8436 (setq A (buffer-substring pre-A post-A))
8437 ;; Now modify (from end, to not break the stuff)
8438 (skip-chars-forward " \t;")
8439 (delete-region pre-A (point)) ; we move to pre-A
8440 (insert "\n" B ";\n}")
8441 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8442 (delete-region pre-if post-if)
8443 (delete-region pre-B post-B)
8444 (goto-char pre-B)
8445 (insert if-string " (" A ") {")
8446 (setq post-B (point))
8447 (if (looking-at "[ \t]+$")
8448 (delete-horizontal-space)
8449 (if (looking-at "[ \t]*#")
8450 (cperl-indent-for-comment)
8451 (just-one-space)))
8452 (forward-line 1)
8453 (if (looking-at "[ \t]*$")
8454 (progn ; delete line
8455 (delete-horizontal-space)
8456 (delete-region (point) (1+ (point)))))
8457 (cperl-indent-line)
8458 (goto-char (1- post-B))
8459 (forward-sexp 1)
8460 (cperl-indent-line)
8461 (goto-char pre-B)))
8463 (defun cperl-invert-if-unless ()
8464 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8465 If the cursor is not on the leading keyword of the BLOCK flavor of
8466 construct, will assume it is the STATEMENT flavor, so will try to find
8467 the appropriate statement modifier."
8468 (interactive)
8469 (and (= (char-syntax (preceding-char)) ?w)
8470 (forward-sexp -1))
8471 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8472 (let ((pre-if (point))
8473 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8474 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8475 (forward-sexp 2)
8476 (setq post-A (point))
8477 (forward-sexp -1)
8478 (setq pre-A (point))
8479 (setq is-block (and (eq (following-char) ?\( )
8480 (save-excursion
8481 (condition-case nil
8482 (progn
8483 (forward-sexp 2)
8484 (forward-sexp -1)
8485 (eq (following-char) ?\{ ))
8486 (error nil)))))
8487 (if is-block
8488 (progn
8489 (goto-char post-A)
8490 (forward-sexp 1)
8491 (setq post-B (point))
8492 (forward-sexp -1)
8493 (setq pre-B (point))
8494 (if (and (eq (following-char) ?\{ )
8495 (progn
8496 (cperl-backward-to-noncomment post-A)
8497 (eq (preceding-char) ?\) )))
8498 (if (condition-case nil
8499 (progn
8500 (goto-char post-B)
8501 (forward-sexp 1)
8502 (forward-sexp -1)
8503 (looking-at "\\<els\\(e\\|if\\)\\>"))
8504 (error nil))
8505 (error
8506 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8507 (goto-char (1- post-B))
8508 (cperl-backward-to-noncomment pre-B)
8509 (if (eq (preceding-char) ?\;)
8510 (forward-char -1))
8511 (setq end-B-code (point))
8512 (goto-char pre-B)
8513 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8514 (setq p (match-beginning 0)
8515 A (buffer-substring p (match-end 0))
8516 state (parse-partial-sexp pre-B p))
8517 (or (nth 3 state)
8518 (nth 4 state)
8519 (nth 5 state)
8520 (error "`%s' inside `%s' BLOCK" A if-string))
8521 (goto-char (match-end 0)))
8522 ;; Finally got it
8523 (goto-char (1+ pre-B))
8524 (skip-chars-forward " \t\n")
8525 (setq B (buffer-substring (point) end-B-code))
8526 (goto-char end-B-code)
8527 (or (looking-at ";?[ \t\n]*}")
8528 (progn
8529 (skip-chars-forward "; \t\n")
8530 (setq B-comment
8531 (buffer-substring (point) (1- post-B)))))
8532 (and (equal B "")
8533 (setq B "1"))
8534 (goto-char (1- post-A))
8535 (cperl-backward-to-noncomment pre-A)
8536 (or (looking-at "[ \t\n]*)")
8537 (goto-char (1- post-A)))
8538 (setq p (point))
8539 (goto-char (1+ pre-A))
8540 (skip-chars-forward " \t\n")
8541 (setq A (buffer-substring (point) p))
8542 (delete-region pre-B post-B)
8543 (delete-region pre-A post-A)
8544 (goto-char pre-if)
8545 (insert B " ")
8546 (and B-comment (insert B-comment " "))
8547 (just-one-space)
8548 (forward-word 1)
8549 (setq pre-A (point))
8550 (insert " " A ";")
8551 (delete-horizontal-space)
8552 (setq post-B (point))
8553 (if (looking-at "#")
8554 (indent-for-comment))
8555 (goto-char post-B)
8556 (forward-char -1)
8557 (delete-horizontal-space)
8558 (goto-char pre-A)
8559 (just-one-space)
8560 (goto-char pre-if)
8561 (setq pre-A (set-marker (make-marker) pre-A))
8562 (while (<= (point) (marker-position pre-A))
8563 (cperl-indent-line)
8564 (forward-line 1))
8565 (goto-char (marker-position pre-A))
8566 (if B-comment
8567 (progn
8568 (forward-line -1)
8569 (indent-for-comment)
8570 (goto-char (marker-position pre-A)))))
8571 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8572 ;; (error "`%s' not with an (EXPR)" if-string)
8573 (forward-sexp -1)
8574 (cperl-invert-if-unless-modifiers)))
8575 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8576 (cperl-invert-if-unless-modifiers)))
8578 ;;; By Anthony Foiani <afoiani@uswest.com>
8579 ;;; Getting help on modules in C-h f ?
8580 ;;; This is a modified version of `man'.
8581 ;;; Need to teach it how to lookup functions
8582 ;;;###autoload
8583 (defun cperl-perldoc (word)
8584 "Run `perldoc' on WORD."
8585 (interactive
8586 (list (let* ((default-entry (cperl-word-at-point))
8587 (input (read-string
8588 (format "perldoc entry%s: "
8589 (if (string= default-entry "")
8591 (format " (default %s)" default-entry))))))
8592 (if (string= input "")
8593 (if (string= default-entry "")
8594 (error "No perldoc args given")
8595 default-entry)
8596 input))))
8597 (require 'man)
8598 (let* ((case-fold-search nil)
8599 (is-func (and
8600 (string-match "^[a-z]+$" word)
8601 (string-match (concat "^" word "\\>")
8602 (documentation-property
8603 'cperl-short-docs
8604 'variable-documentation))))
8605 (manual-program (if is-func "perldoc -f" "perldoc")))
8606 (cond
8607 (cperl-xemacs-p
8608 (let ((Manual-program "perldoc")
8609 (Manual-switches (if is-func (list "-f"))))
8610 (manual-entry word)))
8612 (Man-getpage-in-background word)))))
8614 ;;;###autoload
8615 (defun cperl-perldoc-at-point ()
8616 "Run a `perldoc' on the word around point."
8617 (interactive)
8618 (cperl-perldoc (cperl-word-at-point)))
8620 (defcustom pod2man-program "pod2man"
8621 "*File name for `pod2man'."
8622 :type 'file
8623 :group 'cperl)
8625 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8626 (defun cperl-pod-to-manpage ()
8627 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8628 (interactive)
8629 (require 'man)
8630 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8631 (bufname (concat "Man " buffer-file-name))
8632 (buffer (generate-new-buffer bufname)))
8633 (save-excursion
8634 (set-buffer buffer)
8635 (let ((process-environment (copy-sequence process-environment)))
8636 ;; Prevent any attempt to use display terminal fanciness.
8637 (setenv "TERM" "dumb")
8638 (set-process-sentinel
8639 (start-process pod2man-program buffer "sh" "-c"
8640 (format (cperl-pod2man-build-command) pod2man-args))
8641 'Man-bgproc-sentinel)))))
8643 ;;; Updated version by him too
8644 (defun cperl-build-manpage ()
8645 "Create a virtual manpage in Emacs from the POD in the file."
8646 (interactive)
8647 (require 'man)
8648 (cond
8649 (cperl-xemacs-p
8650 (let ((Manual-program "perldoc"))
8651 (manual-entry buffer-file-name)))
8653 (let* ((manual-program "perldoc"))
8654 (Man-getpage-in-background buffer-file-name)))))
8656 (defun cperl-pod2man-build-command ()
8657 "Builds the entire background manpage and cleaning command."
8658 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8659 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8660 (while (and flist (car flist))
8661 (let ((pcom (car (car flist)))
8662 (pargs (cdr (car flist))))
8663 (setq command
8664 (concat command " | " pcom " "
8665 (mapconcat '(lambda (phrase)
8666 (if (not (stringp phrase))
8667 (error "Malformed Man-filter-list"))
8668 phrase)
8669 pargs " ")))
8670 (setq flist (cdr flist))))
8671 command))
8674 (defun cperl-next-interpolated-REx-1 ()
8675 "Move point to next REx which has interpolated parts without //o.
8676 Skips RExes consisting of one interpolated variable.
8678 Note that skipped RExen are not performance hits."
8679 (interactive "")
8680 (cperl-next-interpolated-REx 1))
8682 (defun cperl-next-interpolated-REx-0 ()
8683 "Move point to next REx which has interpolated parts without //o."
8684 (interactive "")
8685 (cperl-next-interpolated-REx 0))
8687 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8688 "Move point to next REx which has interpolated parts.
8689 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8690 point and the limit of search (default to point and end of buffer).
8692 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8693 semantic may be used as a numeric argument.
8695 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8696 a result of qr//, this is not a performance hit), t for the rest."
8697 (interactive "P")
8698 (if (numberp skip) (setq skip (list 0 skip)))
8699 (or beg (setq beg (point)))
8700 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8701 (let (pp)
8702 (and (eq (get-text-property beg 'syntax-type) 'string)
8703 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8704 (cperl-map-pods-heres
8705 (function (lambda (s e p)
8706 (if (memq (get-text-property s 'REx-interpolated) skip)
8708 (setq pp s)
8709 nil))) ; nil stops
8710 'REx-interpolated beg limit)
8711 (if pp (goto-char pp)
8712 (message "No more interpolated REx"))))
8714 ;;; Initial version contributed by Trey Belew
8715 (defun cperl-here-doc-spell (&optional beg end)
8716 "Spell-check HERE-documents in the Perl buffer.
8717 If a region is highlighted, restricts to the region."
8718 (interactive "")
8719 (cperl-pod-spell t beg end))
8721 (defun cperl-pod-spell (&optional do-heres beg end)
8722 "Spell-check POD documentation.
8723 If invoked with prefix argument, will do HERE-DOCs instead.
8724 If a region is highlighted, restricts to the region."
8725 (interactive "P")
8726 (save-excursion
8727 (let (beg end)
8728 (if (cperl-mark-active)
8729 (setq beg (min (mark) (point))
8730 end (max (mark) (point)))
8731 (setq beg (point-min)
8732 end (point-max)))
8733 (cperl-map-pods-heres (function
8734 (lambda (s e p)
8735 (if do-heres
8736 (setq e (save-excursion
8737 (goto-char e)
8738 (forward-line -1)
8739 (point))))
8740 (ispell-region s e)
8742 (if do-heres 'here-doc-group 'in-pod)
8743 beg end))))
8745 (defun cperl-map-pods-heres (func &optional prop s end)
8746 "Executes a function over regions of pods or here-documents.
8747 PROP is the text-property to search for; default to `in-pod'. Stop when
8748 function returns nil."
8749 (let (pos posend has-prop (cont t))
8750 (or prop (setq prop 'in-pod))
8751 (or s (setq s (point-min)))
8752 (or end (setq end (point-max)))
8753 (cperl-update-syntaxification end end)
8754 (save-excursion
8755 (goto-char (setq pos s))
8756 (while (and cont (< pos end))
8757 (setq has-prop (get-text-property pos prop))
8758 (setq posend (next-single-property-change pos prop nil end))
8759 (and has-prop
8760 (setq cont (funcall func pos posend prop)))
8761 (setq pos posend)))))
8763 ;;; Based on code by Masatake YAMATO:
8764 (defun cperl-get-here-doc-region (&optional pos pod)
8765 "Return HERE document region around the point.
8766 Return nil if the point is not in a HERE document region. If POD is non-nil,
8767 will return a POD section if point is in a POD section."
8768 (or pos (setq pos (point)))
8769 (cperl-update-syntaxification pos pos)
8770 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8771 (and pod
8772 (eq 'pod (get-text-property pos 'syntax-type))))
8773 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8774 (e (next-single-property-change pos 'syntax-type)))
8775 (cons b (or e (point-max))))))
8777 (defun cperl-narrow-to-here-doc (&optional pos)
8778 "Narrows editing region to the HERE-DOC at POS.
8779 POS defaults to the point."
8780 (interactive "d")
8781 (or pos (setq pos (point)))
8782 (let ((p (cperl-get-here-doc-region pos)))
8783 (or p (error "Not inside a HERE document"))
8784 (narrow-to-region (car p) (cdr p))
8785 (message
8786 "When you are finished with narrow editing, type C-x n w")))
8788 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8789 "Select the HERE-DOC (or POD section) at POS.
8790 POS defaults to the point."
8791 (interactive "d")
8792 (let ((p (cperl-get-here-doc-region pos t)))
8793 (if p
8794 (progn
8795 (goto-char (car p))
8796 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8797 (message "I do not think POS is in POD or a HERE-doc..."))))
8799 (defun cperl-facemenu-add-face-function (face end)
8800 "A callback to process user-initiated font-change requests.
8801 Translates `bold', `italic', and `bold-italic' requests to insertion of
8802 corresponding POD directives, and `underline' to C<> POD directive.
8804 Such requests are usually bound to M-o LETTER."
8805 (or (get-text-property (point) 'in-pod)
8806 (error "Faces can only be set within POD"))
8807 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8808 (cdr (or (assq face '((bold . "B<")
8809 (italic . "I<")
8810 (bold-italic . "B<I<")
8811 (underline . "C<")))
8812 (error "Face %s not configured for cperl-mode"
8813 face))))
8815 (defun cperl-time-fontification (&optional l step lim)
8816 "Times how long it takes to do incremental fontification in a region.
8817 L is the line to start at, STEP is the number of lines to skip when
8818 doing next incremental fontification, LIM is the maximal number of
8819 incremental fontification to perform. Messages are accumulated in
8820 *Messages* buffer.
8822 May be used for pinpointing which construct slows down buffer fontification:
8823 start with default arguments, then refine the slowdown regions."
8824 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8825 (or l (setq l 1))
8826 (or step (setq step 500))
8827 (or lim (setq lim 40))
8828 (let* ((timems (function (lambda ()
8829 (let ((tt (current-time)))
8830 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8831 (tt (funcall timems)) (c 0) delta tot)
8832 (goto-line l)
8833 (cperl-mode)
8834 (setq tot (- (- tt (setq tt (funcall timems)))))
8835 (message "cperl-mode at %s: %s" l tot)
8836 (while (and (< c lim) (not (eobp)))
8837 (forward-line step)
8838 (setq l (+ l step))
8839 (setq c (1+ c))
8840 (cperl-update-syntaxification (point) (point))
8841 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8842 (message "to %s:%6s,%7s" l delta tot))
8843 tot))
8845 (defun cperl-emulate-lazy-lock (&optional window-size)
8846 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8847 Start fontifying the buffer from the start (or end) using the given
8848 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8849 goes backwards; default is -50. This function is not CPerl-specific; it
8850 may be used to debug problems with delayed incremental fontification."
8851 (interactive
8852 "nSize of window for incremental fontification, negative goes backwards: ")
8853 (or window-size (setq window-size -50))
8854 (let ((pos (if (> window-size 0)
8855 (point-min)
8856 (point-max)))
8858 (goto-char pos)
8859 (normal-mode)
8860 ;; Why needed??? With older font-locks???
8861 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8862 (while (if (> window-size 0)
8863 (< pos (point-max))
8864 (> pos (point-min)))
8865 (setq p (progn
8866 (forward-line window-size)
8867 (point)))
8868 (font-lock-fontify-region (min p pos) (max p pos))
8869 (setq pos p))))
8872 (defun cperl-lazy-install ()) ; Avoid a warning
8873 (defun cperl-lazy-unstall ()) ; Avoid a warning
8875 (if (fboundp 'run-with-idle-timer)
8876 (progn
8877 (defvar cperl-help-shown nil
8878 "Non-nil means that the help was already shown now.")
8880 (defvar cperl-lazy-installed nil
8881 "Non-nil means that the lazy-help handlers are installed now.")
8883 (defun cperl-lazy-install ()
8884 "Switches on Auto-Help on Perl constructs (put in the message area).
8885 Delay of auto-help controlled by `cperl-lazy-help-time'."
8886 (interactive)
8887 (make-local-variable 'cperl-help-shown)
8888 (if (and (cperl-val 'cperl-lazy-help-time)
8889 (not cperl-lazy-installed))
8890 (progn
8891 (add-hook 'post-command-hook 'cperl-lazy-hook)
8892 (run-with-idle-timer
8893 (cperl-val 'cperl-lazy-help-time 1000000 5)
8895 'cperl-get-help-defer)
8896 (setq cperl-lazy-installed t))))
8898 (defun cperl-lazy-unstall ()
8899 "Switches off Auto-Help on Perl constructs (put in the message area).
8900 Delay of auto-help controlled by `cperl-lazy-help-time'."
8901 (interactive)
8902 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8903 (cancel-function-timers 'cperl-get-help-defer)
8904 (setq cperl-lazy-installed nil))
8906 (defun cperl-lazy-hook ()
8907 (setq cperl-help-shown nil))
8909 (defun cperl-get-help-defer ()
8910 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8911 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8912 (cperl-get-help)
8913 (setq cperl-help-shown t))))
8914 (cperl-lazy-install)))
8917 ;;; Plug for wrong font-lock:
8919 (defun cperl-font-lock-unfontify-region-function (beg end)
8920 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8921 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8922 before-change-functions after-change-functions
8923 deactivate-mark buffer-file-name buffer-file-truename)
8924 (remove-text-properties beg end '(face nil))
8925 (if (and (not modified) (buffer-modified-p))
8926 (set-buffer-modified-p nil))))
8928 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8929 "Extends the region to safe positions, then calls the default function.
8930 Newer `font-lock's can do it themselves.
8931 We unwind only as far as needed for fontification. Syntaxification may
8932 do extra unwind via `cperl-unwind-to-safe'."
8933 (save-excursion
8934 (goto-char beg)
8935 (while (and beg
8936 (progn
8937 (beginning-of-line)
8938 (eq (get-text-property (setq beg (point)) 'syntax-type)
8939 'multiline)))
8940 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8941 (goto-char beg)))
8942 (setq beg (point))
8943 (goto-char end)
8944 (while (and end
8945 (progn
8946 (or (bolp) (condition-case nil
8947 (forward-line 1)
8948 (error nil)))
8949 (eq (get-text-property (setq end (point)) 'syntax-type)
8950 'multiline)))
8951 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8952 (goto-char end))
8953 (setq end (point)))
8954 (font-lock-default-fontify-region beg end loudly))
8956 (defvar cperl-d-l nil)
8957 (defun cperl-fontify-syntaxically (end)
8958 ;; Some vars for debugging only
8959 ;; (message "Syntaxifying...")
8960 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8961 (istate (car cperl-syntax-state))
8962 start from-start edebug-backtrace-buffer)
8963 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8964 (progn
8965 (require 'edebug)
8966 (let ((f 'edebug-backtrace))
8967 (funcall f)))) ; Avoid compile-time warning
8968 (or cperl-syntax-done-to
8969 (setq cperl-syntax-done-to (point-min)
8970 from-start t))
8971 (setq start (if (and cperl-hook-after-change
8972 (not from-start))
8973 cperl-syntax-done-to ; Fontify without change; ignore start
8974 ;; Need to forget what is after `start'
8975 (min cperl-syntax-done-to (point))))
8976 (goto-char start)
8977 (beginning-of-line)
8978 (setq start (point))
8979 (and cperl-syntaxify-unwind
8980 (setq end (cperl-unwind-to-safe t end)
8981 start (point)))
8982 (and (> end start)
8983 (setq cperl-syntax-done-to start) ; In case what follows fails
8984 (cperl-find-pods-heres start end t nil t))
8985 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8986 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8987 dbg iend start end idone cperl-syntax-done-to
8988 istate (car cperl-syntax-state))) ; For debugging
8989 nil)) ; Do not iterate
8991 (defun cperl-fontify-update (end)
8992 (let ((pos (point-min)) prop posend)
8993 (setq end (point-max))
8994 (while (< pos end)
8995 (setq prop (get-text-property pos 'cperl-postpone)
8996 posend (next-single-property-change pos 'cperl-postpone nil end))
8997 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8998 (setq pos posend)))
8999 nil) ; Do not iterate
9001 (defun cperl-fontify-update-bad (end)
9002 ;; Since fontification happens with different region than syntaxification,
9003 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
9004 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
9005 (if prop
9006 (setq pos (or (cperl-beginning-of-property
9007 (cperl-1+ pos) 'cperl-postpone)
9008 (point-min))))
9009 (while (< pos end)
9010 (setq posend (next-single-property-change pos 'cperl-postpone))
9011 (and prop (put-text-property pos posend (car prop) (cdr prop)))
9012 (setq pos posend)
9013 (setq prop (get-text-property pos 'cperl-postpone))))
9014 nil) ; Do not iterate
9016 ;; Called when any modification is made to buffer text.
9017 (defun cperl-after-change-function (beg end old-len)
9018 ;; We should have been informed about changes by `font-lock'. Since it
9019 ;; does not inform as which calls are defered, do it ourselves
9020 (if cperl-syntax-done-to
9021 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
9023 (defun cperl-update-syntaxification (from to)
9024 (if (and cperl-use-syntax-table-text-property
9025 cperl-syntaxify-by-font-lock
9026 (or (null cperl-syntax-done-to)
9027 (< cperl-syntax-done-to to)))
9028 (progn
9029 (save-excursion
9030 (goto-char from)
9031 (cperl-fontify-syntaxically to)))))
9033 (defvar cperl-version
9034 (let ((v "Revision: 5.22"))
9035 (string-match ":\\s *\\([0-9.]+\\)" v)
9036 (substring v (match-beginning 1) (match-end 1)))
9037 "Version of IZ-supported CPerl package this file is based on.")
9039 (provide 'cperl-mode)
9041 ;;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
9042 ;;; cperl-mode.el ends here