(read-expression-map): Just set it, no defvar.
[emacs.git] / lisp / progmodes / cperl-mode.el
blob3264e0e72f6ecc6d0a4e07f0891f4991a42b6ca6
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
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."
356 :type '(choice (const null) boolean)
357 :group 'cperl-affected-by-hairy)
359 (defcustom cperl-electric-backspace-untabify t
360 "*Not-nil means electric-backspace will untabify in CPerl."
361 :type 'boolean
362 :group 'cperl-autoinsert-details)
364 (defcustom cperl-hairy nil
365 "*Not-nil means most of the bells and whistles are enabled in CPerl.
366 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
367 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
368 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
369 `cperl-lazy-help-time'."
370 :type 'boolean
371 :group 'cperl-affected-by-hairy)
373 (defcustom cperl-comment-column 32
374 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
375 :type 'integer
376 :group 'cperl-indentation-details)
378 (defcustom cperl-indent-comment-at-column-0 nil
379 "*Non-nil means that comment started at column 0 should be indentable."
380 :type 'boolean
381 :group 'cperl-indentation-details)
383 (defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
384 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
385 :type '(repeat string)
386 :group 'cperl)
388 (defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
389 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
390 :type '(repeat string)
391 :group 'cperl)
393 ;; This became obsolete...
394 (defvar cperl-vc-header-alist nil)
395 (make-obsolete-variable
396 'cperl-vc-header-alist
397 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead.")
399 (defcustom cperl-clobber-mode-lists
400 (not
401 (and
402 (boundp 'interpreter-mode-alist)
403 (assoc "miniperl" interpreter-mode-alist)
404 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
405 "*Whether to install us into `interpreter-' and `extension' mode lists."
406 :type 'boolean
407 :group 'cperl)
409 (defcustom cperl-info-on-command-no-prompt nil
410 "*Not-nil (and non-null) means not to prompt on C-h f.
411 The opposite behavior is always available if prefixed with C-c.
412 Can be overwritten by `cperl-hairy' if nil."
413 :type '(choice (const null) boolean)
414 :group 'cperl-affected-by-hairy)
416 (defcustom cperl-clobber-lisp-bindings nil
417 "*Not-nil (and non-null) means not overwrite C-h f.
418 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
419 Can be overwritten by `cperl-hairy' if nil."
420 :type '(choice (const null) boolean)
421 :group 'cperl-affected-by-hairy)
423 (defcustom cperl-lazy-help-time nil
424 "*Not-nil (and non-null) means to show lazy help after given idle time.
425 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
426 :type '(choice (const null) (const nil) integer)
427 :group 'cperl-affected-by-hairy)
429 (defcustom cperl-pod-face 'font-lock-comment-face
430 "*Face for POD highlighting."
431 :type 'face
432 :group 'cperl-faces)
434 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
435 "*Face for POD highlighting.
436 Font for POD headers."
437 :type 'face
438 :group 'cperl-faces)
440 (defcustom cperl-here-face 'font-lock-string-face
441 "*Face for here-docs highlighting."
442 :type 'face
443 :group 'cperl-faces)
445 ;;; Some double-evaluation happened with font-locks... Needed with 21.2...
446 (defvar cperl-singly-quote-face cperl-xemacs-p)
448 (defcustom cperl-invalid-face ; Does not customize with '' on XEmacs
449 (if cperl-singly-quote-face
450 'underline ''underline) ; On older Emacsen was evaluated by `font-lock'
451 (if cperl-singly-quote-face
452 "*This face is used for highlighting trailing whitespace."
453 "*Face for highlighting trailing whitespace.")
454 :type 'face
455 :version "21.1"
456 :group 'cperl-faces)
458 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
459 "*Not-nil after evaluation means to highlight POD and here-docs sections."
460 :type 'boolean
461 :group 'cperl-faces)
463 (defcustom cperl-fontify-m-as-s t
464 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
465 :type 'boolean
466 :group 'cperl-faces)
468 (defcustom cperl-highlight-variables-indiscriminately nil
469 "*Non-nil means perform additional highlighting on variables.
470 Currently only changes how scalar variables are highlighted.
471 Note that that variable is only read at initialization time for
472 the variable `cperl-font-lock-keywords-2', so changing it after you've
473 entered CPerl mode the first time will have no effect."
474 :type 'boolean
475 :group 'cperl)
477 (defcustom cperl-pod-here-scan t
478 "*Not-nil means look for POD and here-docs sections during startup.
479 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
480 :type 'boolean
481 :group 'cperl-speed)
483 (defcustom cperl-regexp-scan t
484 "*Not-nil means make marking of regular expression more thorough.
485 Effective only with `cperl-pod-here-scan'."
486 :type 'boolean
487 :group 'cperl-speed)
489 (defcustom cperl-hook-after-change t
490 "*Not-nil means install hook to know which regions of buffer are changed.
491 May significantly speed up delayed fontification. Changes take effect
492 after reload."
493 :type 'boolean
494 :group 'cperl-speed)
496 (defcustom cperl-imenu-addback nil
497 "*Not-nil means add backreferences to generated `imenu's.
498 May require patched `imenu' and `imenu-go'. Obsolete."
499 :type 'boolean
500 :group 'cperl-help-system)
502 (defcustom cperl-max-help-size 66
503 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
504 :type '(choice integer (const nil))
505 :group 'cperl-help-system)
507 (defcustom cperl-shrink-wrap-info-frame t
508 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
509 :type 'boolean
510 :group 'cperl-help-system)
512 (defcustom cperl-info-page "perl"
513 "*Name of the info page containing perl docs.
514 Older version of this page was called `perl5', newer `perl'."
515 :type 'string
516 :group 'cperl-help-system)
518 (defcustom cperl-use-syntax-table-text-property
519 (boundp 'parse-sexp-lookup-properties)
520 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
521 :type 'boolean
522 :group 'cperl-speed)
524 (defcustom cperl-use-syntax-table-text-property-for-tags
525 cperl-use-syntax-table-text-property
526 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
527 :type 'boolean
528 :group 'cperl-speed)
530 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
531 "*Regexp to match files to scan when generating TAGS."
532 :type 'regexp
533 :group 'cperl)
535 (defcustom cperl-noscan-files-regexp
536 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
537 "*Regexp to match files/dirs to skip when generating TAGS."
538 :type 'regexp
539 :group 'cperl)
541 (defcustom cperl-regexp-indent-step nil
542 "*Indentation used when beautifying regexps.
543 If nil, the value of `cperl-indent-level' will be used."
544 :type '(choice integer (const nil))
545 :group 'cperl-indentation-details)
547 (defcustom cperl-indent-left-aligned-comments t
548 "*Non-nil means that the comment starting in leftmost column should indent."
549 :type 'boolean
550 :group 'cperl-indentation-details)
552 (defcustom cperl-under-as-char nil
553 "*Non-nil means that the _ (underline) should be treated as word char."
554 :type 'boolean
555 :group 'cperl)
557 (defcustom cperl-extra-perl-args ""
558 "*Extra arguments to use when starting Perl.
559 Currently used with `cperl-check-syntax' only."
560 :type 'string
561 :group 'cperl)
563 (defcustom cperl-message-electric-keyword t
564 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
565 :type 'boolean
566 :group 'cperl-help-system)
568 (defcustom cperl-indent-region-fix-constructs 1
569 "*Amount of space to insert between `}' and `else' or `elsif'
570 in `cperl-indent-region'. Set to nil to leave as is. Values other
571 than 1 and nil will probably not work."
572 :type '(choice (const nil) (const 1))
573 :group 'cperl-indentation-details)
575 (defcustom cperl-break-one-line-blocks-when-indent t
576 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
577 need to be reformatted into multiline ones when indenting a region."
578 :type 'boolean
579 :group 'cperl-indentation-details)
581 (defcustom cperl-fix-hanging-brace-when-indent t
582 "*Non-nil means that BLOCK-end `}' may be put on a separate line
583 when indenting a region.
584 Braces followed by else/elsif/while/until are excepted."
585 :type 'boolean
586 :group 'cperl-indentation-details)
588 (defcustom cperl-merge-trailing-else t
589 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
590 may be merged to be on the same line when indenting a region."
591 :type 'boolean
592 :group 'cperl-indentation-details)
594 (defcustom cperl-indent-parens-as-block nil
595 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
596 but for trailing \",\" inside the group, which won't increase indentation.
597 One should tune up `cperl-close-paren-offset' as well."
598 :type 'boolean
599 :group 'cperl-indentation-details)
601 (defcustom cperl-syntaxify-by-font-lock
602 (and cperl-can-font-lock
603 (boundp 'parse-sexp-lookup-properties))
604 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
605 :type '(choice (const message) boolean)
606 :group 'cperl-speed)
608 (defcustom cperl-syntaxify-unwind
610 "*Non-nil means that CPerl unwinds to a start of a long construction
611 when syntaxifying a chunk of buffer."
612 :type 'boolean
613 :group 'cperl-speed)
615 (defcustom cperl-syntaxify-for-menu
617 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
618 This way enabling/disabling of menu items is more correct."
619 :type 'boolean
620 :group 'cperl-speed)
622 (defcustom cperl-ps-print-face-properties
623 '((font-lock-keyword-face nil nil bold shadow)
624 (font-lock-variable-name-face nil nil bold)
625 (font-lock-function-name-face nil nil bold italic box)
626 (font-lock-constant-face nil "LightGray" bold)
627 (cperl-array-face nil "LightGray" bold underline)
628 (cperl-hash-face nil "LightGray" bold italic underline)
629 (font-lock-comment-face nil "LightGray" italic)
630 (font-lock-string-face nil nil italic underline)
631 (cperl-nonoverridable-face nil nil italic underline)
632 (font-lock-type-face nil nil underline)
633 (font-lock-warning-face nil "LightGray" bold italic box)
634 (underline nil "LightGray" strikeout))
635 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
636 :type '(repeat (cons symbol
637 (cons (choice (const nil) string)
638 (cons (choice (const nil) string)
639 (repeat symbol)))))
640 :group 'cperl-faces)
642 (defvar cperl-dark-background
643 (cperl-choose-color "navy" "os2blue" "darkgreen"))
644 (defvar cperl-dark-foreground
645 (cperl-choose-color "orchid1" "orange"))
647 (defface cperl-nonoverridable-face
648 `((((class grayscale) (background light))
649 (:background "Gray90" :slant italic :underline t))
650 (((class grayscale) (background dark))
651 (:foreground "Gray80" :slant italic :underline t :weight bold))
652 (((class color) (background light))
653 (:foreground "chartreuse3"))
654 (((class color) (background dark))
655 (:foreground ,cperl-dark-foreground))
656 (t (:weight bold :underline t)))
657 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
658 :group 'cperl-faces)
660 (defface cperl-array-face
661 `((((class grayscale) (background light))
662 (:background "Gray90" :weight bold))
663 (((class grayscale) (background dark))
664 (:foreground "Gray80" :weight bold))
665 (((class color) (background light))
666 (:foreground "Blue" :background "lightyellow2" :weight bold))
667 (((class color) (background dark))
668 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
669 (t (:weight bold)))
670 "Font Lock mode face used to highlight array names."
671 :group 'cperl-faces)
673 (defface cperl-hash-face
674 `((((class grayscale) (background light))
675 (:background "Gray90" :weight bold :slant italic))
676 (((class grayscale) (background dark))
677 (:foreground "Gray80" :weight bold :slant italic))
678 (((class color) (background light))
679 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
680 (((class color) (background dark))
681 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
682 (t (:weight bold :slant italic)))
683 "Font Lock mode face used to highlight hash names."
684 :group 'cperl-faces)
688 ;;; Short extra-docs.
690 (defvar cperl-tips 'please-ignore-this-line
691 "Get maybe newer version of this package from
692 http://ilyaz.org/software/emacs
693 Subdirectory `cperl-mode' may contain yet newer development releases and/or
694 patches to related files.
696 For best results apply to an older Emacs the patches from
697 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
698 \(this upgrades syntax-parsing abilities of Emacsen v19.34 and
699 v20.2 up to the level of Emacs v20.3 - a must for a good Perl
700 mode.) As of beginning of 2003, XEmacs may provide a similar ability.
702 Get support packages choose-color.el (or font-lock-extra.el before
703 19.30), imenu-go.el from the same place. \(Look for other files there
704 too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
705 later you should use choose-color.el *instead* of font-lock-extra.el
706 \(and you will not get smart highlighting in C :-().
708 Note that to enable Compile choices in the menu you need to install
709 mode-compile.el.
711 If your Emacs does not default to `cperl-mode' on Perl files, and you
712 want it to: put the following into your .emacs file:
714 (defalias 'perl-mode 'cperl-mode)
716 Get perl5-info from
717 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
718 Also, one can generate a newer documentation running `pod2texi' converter
719 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
721 If you use imenu-go, run imenu on perl5-info buffer (you can do it
722 from Perl menu). If many files are related, generate TAGS files from
723 Tools/Tags submenu in Perl menu.
725 If some class structure is too complicated, use Tools/Hierarchy-view
726 from Perl menu, or hierarchic view of imenu. The second one uses the
727 current buffer only, the first one requires generation of TAGS from
728 Perl/Tools/Tags menu beforehand.
730 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
732 Switch auto-help on/off with Perl/Tools/Auto-help.
734 Though with contemporary Emaxen CPerl mode should maintain the correct
735 parsing of Perl even when editing, sometimes it may be lost. Fix this by
737 \\[normal-mode]
739 In cases of more severe confusion sometimes it is helpful to do
741 \\[load-library] cperl-mode RET
742 \\[normal-mode]
744 Before reporting (non-)problems look in the problem section of online
745 micro-docs on what I know about CPerl problems.")
747 (defvar cperl-problems 'please-ignore-this-line
748 "Description of problems in CPerl mode.
749 Some faces will not be shown on some versions of Emacs unless you
750 install choose-color.el, available from
751 http://ilyaz.org/software/emacs
753 `fill-paragraph' on a comment may leave the point behind the
754 paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
755 to detect it and bulk out).
757 See documentation of a variable `cperl-problems-old-emaxen' for the
758 problems which disappear if you upgrade Emacs to a reasonably new
759 version (20.3 for Emacs, and those of 2004 for XEmacs).")
761 (defvar cperl-problems-old-emaxen 'please-ignore-this-line
762 "Description of problems in CPerl mode specific for older Emacs versions.
764 Emacs had a _very_ restricted syntax parsing engine until version
765 20.1. Most problems below are corrected starting from this version of
766 Emacs, and all of them should be fixed in version 20.3. (Or apply
767 patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
768 this respect (until 2003).
770 Note that even with newer Emacsen in some very rare cases the details
771 of interaction of `font-lock' and syntaxification may be not cleaned
772 up yet. You may get slightly different colors basing on the order of
773 fontification and syntaxification. Say, the initial faces is correct,
774 but editing the buffer breaks this.
776 Even with older Emacsen CPerl mode tries to corrects some Emacs
777 misunderstandings, however, for efficiency reasons the degree of
778 correction is different for different operations. The partially
779 corrected problems are: POD sections, here-documents, regexps. The
780 operations are: highlighting, indentation, electric keywords, electric
781 braces.
783 This may be confusing, since the regexp s#//#/#\; may be highlighted
784 as a comment, but it will be recognized as a regexp by the indentation
785 code. Or the opposite case, when a POD section is highlighted, but
786 may break the indentation of the following code (though indentation
787 should work if the balance of delimiters is not broken by POD).
789 The main trick (to make $ a \"backslash\") makes constructions like
790 ${aaa} look like unbalanced braces. The only trick I can think of is
791 to insert it as $ {aaa} (valid in perl5, not in perl4).
793 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
794 as /($|\\s)/. Note that such a transposition is not always possible.
796 The solution is to upgrade your Emacs or patch an older one. Note
797 that Emacs 20.2 has some bugs related to `syntax-table' text
798 properties. Patches are available on the main CPerl download site,
799 and on CPAN.
801 If these bugs cannot be fixed on your machine (say, you have an inferior
802 environment and cannot recompile), you may still disable all the fancy stuff
803 via `cperl-use-syntax-table-text-property'.")
805 (defvar cperl-praise 'please-ignore-this-line
806 "Advantages of CPerl mode.
808 0) It uses the newest `syntax-table' property ;-);
810 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
811 mode - but the latter number may have improved too in last years) even
812 with old Emaxen which do not support `syntax-table' property.
814 When using `syntax-table' property for syntax assist hints, it should
815 handle 99.995% of lines correct - or somesuch. It automatically
816 updates syntax assist hints when you edit your script.
818 2) It is generally believed to be \"the most user-friendly Emacs
819 package\" whatever it may mean (I doubt that the people who say similar
820 things tried _all_ the rest of Emacs ;-), but this was not a lonely
821 voice);
823 3) Everything is customizable, one-by-one or in a big sweep;
825 4) It has many easily-accessable \"tools\":
826 a) Can run program, check syntax, start debugger;
827 b) Can lineup vertically \"middles\" of rows, like `=' in
828 a = b;
829 cc = d;
830 c) Can insert spaces where this impoves readability (in one
831 interactive sweep over the buffer);
832 d) Has support for imenu, including:
833 1) Separate unordered list of \"interesting places\";
834 2) Separate TOC of POD sections;
835 3) Separate list of packages;
836 4) Hierarchical view of methods in (sub)packages;
837 5) and functions (by the full name - with package);
838 e) Has an interface to INFO docs for Perl; The interface is
839 very flexible, including shrink-wrapping of
840 documentation buffer/frame;
841 f) Has a builtin list of one-line explanations for perl constructs.
842 g) Can show these explanations if you stay long enough at the
843 corresponding place (or on demand);
844 h) Has an enhanced fontification (using 3 or 4 additional faces
845 comparing to font-lock - basically, different
846 namespaces in Perl have different colors);
847 i) Can construct TAGS basing on its knowledge of Perl syntax,
848 the standard menu has 6 different way to generate
849 TAGS (if \"by directory\", .xs files - with C-language
850 bindings - are included in the scan);
851 j) Can build a hierarchical view of classes (via imenu) basing
852 on generated TAGS file;
853 k) Has electric parentheses, electric newlines, uses Abbrev
854 for electric logical constructs
855 while () {}
856 with different styles of expansion (context sensitive
857 to be not so bothering). Electric parentheses behave
858 \"as they should\" in a presence of a visible region.
859 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
860 m) Can convert from
861 if (A) { B }
863 B if A;
865 n) Highlights (by user-choice) either 3-delimiters constructs
866 (such as tr/a/b/), or regular expressions and `y/tr';
867 o) Highlights trailing whitespace;
868 p) Is able to manipulate Perl Regular Expressions to ease
869 conversion to a more readable form.
870 q) Can ispell POD sections and HERE-DOCs.
871 r) Understands comments and character classes inside regular
872 expressions; can find matching () and [] in a regular expression.
873 s) Allows indentation of //x-style regular expressions;
874 t) Highlights different symbols in regular expressions according
875 to their function; much less problems with backslashitis;
876 u) Allows to find regular expressions which contain interpolated parts.
878 5) The indentation engine was very smart, but most of tricks may be
879 not needed anymore with the support for `syntax-table' property. Has
880 progress indicator for indentation (with `imenu' loaded).
882 6) Indent-region improves inline-comments as well; also corrects
883 whitespace *inside* the conditional/loop constructs.
885 7) Fill-paragraph correctly handles multi-line comments;
887 8) Can switch to different indentation styles by one command, and restore
888 the settings present before the switch.
890 9) When doing indentation of control constructs, may correct
891 line-breaks/spacing between elements of the construct.
893 10) Uses a linear-time algorith for indentation of regions (on Emaxen with
894 capable syntax engines).
896 11) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
899 (defvar cperl-speed 'please-ignore-this-line
900 "This is an incomplete compendium of what is available in other parts
901 of CPerl documentation. (Please inform me if I skept anything.)
903 There is a perception that CPerl is slower than alternatives. This part
904 of documentation is designed to overcome this misconception.
906 *By default* CPerl tries to enable the most comfortable settings.
907 From most points of view, correctly working package is infinitely more
908 comfortable than a non-correctly working one, thus by default CPerl
909 prefers correctness over speed. Below is the guide how to change
910 settings if your preferences are different.
912 A) Speed of loading the file. When loading file, CPerl may perform a
913 scan which indicates places which cannot be parsed by primitive Emacs
914 syntax-parsing routines, and marks them up so that either
916 A1) CPerl may work around these deficiencies (for big chunks, mostly
917 PODs and HERE-documents), or
918 A2) On capable Emaxen CPerl will use improved syntax-handlings
919 which reads mark-up hints directly.
921 The scan in case A2 is much more comprehensive, thus may be slower.
923 User can disable syntax-engine-helping scan of A2 by setting
924 `cperl-use-syntax-table-text-property'
925 variable to nil (if it is set to t).
927 One can disable the scan altogether (both A1 and A2) by setting
928 `cperl-pod-here-scan'
929 to nil.
931 B) Speed of editing operations.
933 One can add a (minor) speedup to editing operations by setting
934 `cperl-use-syntax-table-text-property'
935 variable to nil (if it is set to t). This will disable
936 syntax-engine-helping scan, thus will make many more Perl
937 constructs be wrongly recognized by CPerl, thus may lead to
938 wrongly matched parentheses, wrong indentation, etc.
940 One can unset `cperl-syntaxify-unwind'. This might speed up editing
941 of, say, long POD sections.")
943 (defvar cperl-tips-faces 'please-ignore-this-line
944 "CPerl mode uses following faces for highlighting:
946 `cperl-array-face' Array names
947 `cperl-hash-face' Hash names
948 `font-lock-comment-face' Comments, PODs and whatever is considered
949 syntaxically to be not code
950 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
951 2-arg operators s/y/tr/ or of RExen,
952 `font-lock-warning-face' Special-cased m// and s//foo/,
953 `font-lock-function-name-face' _ as a target of a file tests, file tests,
954 subroutine names at the moment of definition
955 (except those conflicting with Perl operators),
956 package names (when recognized), format names
957 `font-lock-keyword-face' Control flow switch constructs, declarators
958 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
959 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
960 literal parts and the terminator of formats
961 and whatever is syntaxically considered
962 as string literals
963 `font-lock-type-face' Overridable keywords
964 `font-lock-variable-name-face' Variable declarations, indirect array and
965 hash names, POD headers/item names
966 `cperl-invalid' Trailing whitespace
968 Note that in several situations the highlighting tries to inform about
969 possible confusion, such as different colors for function names in
970 declarations depending on what they (do not) override, or special cases
971 m// and s/// which do not do what one would expect them to do.
973 Help with best setup of these faces for printout requested (for each of
974 the faces: please specify bold, italic, underline, shadow and box.)
976 In regular expressions (except character classes):
977 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
978 `font-lock-constant-face': Delimiters
979 `font-lock-warning-face' Special-cased m// and s//foo/,
980 Mismatched closing delimiters, parens
981 we couldn't match, misplaced quantifiers,
982 unrecognized escape sequences
983 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
984 `font-lock-type-face' POSIX classes inside charclasses,
985 escape sequences with arguments (\x \23 \p \N)
986 and others match-a-char escape sequences
987 `font-lock-keyword-face' Capturing parens, and |
988 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
989 `font-lock-builtin-face' \"Remaining\" 0-length constructs, executable
990 parts of a REx, not-capturing parens
991 `font-lock-variable-name-face' Interpolated constructs, embedded code
992 `font-lock-comment-face' Embedded comments
998 ;;; Portability stuff:
1000 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
1001 `(define-key cperl-mode-map
1002 ,(if xemacs-key
1003 `(if cperl-xemacs-p ,xemacs-key ,emacs-key)
1004 emacs-key)
1005 ,definition))
1007 (defvar cperl-del-back-ch
1008 (car (append (where-is-internal 'delete-backward-char)
1009 (where-is-internal 'backward-delete-char-untabify)))
1010 "Character generated by key bound to `delete-backward-char'.")
1012 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
1013 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1015 (defun cperl-mark-active () (mark)) ; Avoid undefined warning
1016 (if cperl-xemacs-p
1017 (progn
1018 ;; "Active regions" are on: use region only if active
1019 ;; "Active regions" are off: use region unconditionally
1020 (defun cperl-use-region-p ()
1021 (if zmacs-regions (mark) t)))
1022 (defun cperl-use-region-p ()
1023 (if transient-mark-mode mark-active t))
1024 (defun cperl-mark-active () mark-active))
1026 (defsubst cperl-enable-font-lock ()
1027 cperl-can-font-lock)
1029 (defun cperl-putback-char (c) ; Emacs 19
1030 (set 'unread-command-events (list c))) ; Avoid undefined warning
1032 (if cperl-xemacs-p
1033 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1034 (setq unread-command-events (list (eval '(character-to-event c))))))
1036 (or (fboundp 'uncomment-region)
1037 (defun uncomment-region (beg end)
1038 (interactive "r")
1039 (comment-region beg end -1)))
1041 (defvar cperl-do-not-fontify
1042 (if (string< emacs-version "19.30")
1043 'fontified
1044 'lazy-lock)
1045 "Text property which inhibits refontification.")
1047 (defsubst cperl-put-do-not-fontify (from to &optional post)
1048 ;; If POST, do not do it with postponed fontification
1049 (if (and post cperl-syntaxify-by-font-lock)
1051 (put-text-property (max (point-min) (1- from))
1052 to cperl-do-not-fontify t)))
1054 (defcustom cperl-mode-hook nil
1055 "Hook run by CPerl mode."
1056 :type 'hook
1057 :group 'cperl)
1059 (defvar cperl-syntax-state nil)
1060 (defvar cperl-syntax-done-to nil)
1061 (defvar cperl-emacs-can-parse (> (length (save-excursion
1062 (parse-partial-sexp (point) (point)))) 9))
1064 ;; Make customization possible "in reverse"
1065 (defsubst cperl-val (symbol &optional default hairy)
1066 (cond
1067 ((eq (symbol-value symbol) 'null) default)
1068 (cperl-hairy (or hairy t))
1069 (t (symbol-value symbol))))
1072 (defun cperl-make-indent (column &optional minimum keep)
1073 "Makes indent of the current line the requested amount.
1074 Unless KEEP, removes the old indentation. Works around a bug in ancient
1075 versions of Emacs."
1076 (let ((prop (get-text-property (point) 'syntax-type)))
1077 (or keep
1078 (delete-horizontal-space))
1079 (indent-to column minimum)
1080 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1081 (and prop
1082 (> (current-column) 0)
1083 (save-excursion
1084 (beginning-of-line)
1085 (or (get-text-property (point) 'syntax-type)
1086 (and (looking-at "\\=[ \t]")
1087 (put-text-property (point) (match-end 0)
1088 'syntax-type prop)))))))
1090 ;;; Probably it is too late to set these guys already, but it can help later:
1092 ;;;(and cperl-clobber-mode-lists
1093 ;;;(setq auto-mode-alist
1094 ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1095 ;;;(and (boundp 'interpreter-mode-alist)
1096 ;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1097 ;;; '(("miniperl" . perl-mode))))))
1098 (eval-when-compile
1099 (mapcar (lambda (p)
1100 (condition-case nil
1101 (require p)
1102 (error nil)))
1103 '(imenu easymenu etags timer man info))
1104 (if (fboundp 'ps-extend-face-list)
1105 (defmacro cperl-ps-extend-face-list (arg)
1106 `(ps-extend-face-list ,arg))
1107 (defmacro cperl-ps-extend-face-list (arg)
1108 `(error "This version of Emacs has no `ps-extend-face-list'")))
1109 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1110 ;; macros instead of defsubsts don't work on Emacs, so we do the
1111 ;; expansion manually. Any other suggestions?
1112 (require 'cl))
1114 (defvar cperl-mode-abbrev-table nil
1115 "Abbrev table in use in CPerl mode buffers.")
1117 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1119 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
1121 (if cperl-mode-map nil
1122 (setq cperl-mode-map (make-sparse-keymap))
1123 (cperl-define-key "{" 'cperl-electric-lbrace)
1124 (cperl-define-key "[" 'cperl-electric-paren)
1125 (cperl-define-key "(" 'cperl-electric-paren)
1126 (cperl-define-key "<" 'cperl-electric-paren)
1127 (cperl-define-key "}" 'cperl-electric-brace)
1128 (cperl-define-key "]" 'cperl-electric-rparen)
1129 (cperl-define-key ")" 'cperl-electric-rparen)
1130 (cperl-define-key ";" 'cperl-electric-semi)
1131 (cperl-define-key ":" 'cperl-electric-terminator)
1132 (cperl-define-key "\C-j" 'newline-and-indent)
1133 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1134 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1135 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1136 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1137 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1138 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1139 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1140 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1141 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1142 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1143 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1144 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1145 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1146 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1147 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1148 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1149 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
1150 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1151 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1152 [(control meta |)])
1153 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1154 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1155 (cperl-define-key "\177" 'cperl-electric-backspace)
1156 (cperl-define-key "\t" 'cperl-indent-command)
1157 ;; don't clobber the backspace binding:
1158 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1159 [(control c) (control h) F])
1160 (if (cperl-val 'cperl-clobber-lisp-bindings)
1161 (progn
1162 (cperl-define-key "\C-hf"
1163 ;;(concat (char-to-string help-char) "f") ; does not work
1164 'cperl-info-on-command
1165 [(control h) f])
1166 (cperl-define-key "\C-hv"
1167 ;;(concat (char-to-string help-char) "v") ; does not work
1168 'cperl-get-help
1169 [(control h) v])
1170 (cperl-define-key "\C-c\C-hf"
1171 ;;(concat (char-to-string help-char) "f") ; does not work
1172 (key-binding "\C-hf")
1173 [(control c) (control h) f])
1174 (cperl-define-key "\C-c\C-hv"
1175 ;;(concat (char-to-string help-char) "v") ; does not work
1176 (key-binding "\C-hv")
1177 [(control c) (control h) v]))
1178 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1179 [(control c) (control h) f])
1180 (cperl-define-key "\C-c\C-hv"
1181 ;;(concat (char-to-string help-char) "v") ; does not work
1182 'cperl-get-help
1183 [(control c) (control h) v]))
1184 (if (and cperl-xemacs-p
1185 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1186 (progn
1187 ;; substitute-key-definition is usefulness-deenhanced...
1188 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1189 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1190 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1191 (or (boundp 'fill-paragraph-function)
1192 (substitute-key-definition
1193 'fill-paragraph 'cperl-fill-paragraph
1194 cperl-mode-map global-map))
1195 (substitute-key-definition
1196 'indent-sexp 'cperl-indent-exp
1197 cperl-mode-map global-map)
1198 (substitute-key-definition
1199 'indent-region 'cperl-indent-region
1200 cperl-mode-map global-map)
1201 (substitute-key-definition
1202 'indent-for-comment 'cperl-indent-for-comment
1203 cperl-mode-map global-map)))
1205 (defvar cperl-menu)
1206 (defvar cperl-lazy-installed)
1207 (defvar cperl-old-style nil)
1208 (condition-case nil
1209 (progn
1210 (require 'easymenu)
1211 (easy-menu-define
1212 cperl-menu cperl-mode-map "Menu for CPerl mode"
1213 '("Perl"
1214 ["Beginning of function" beginning-of-defun t]
1215 ["End of function" end-of-defun t]
1216 ["Mark function" mark-defun t]
1217 ["Indent expression" cperl-indent-exp t]
1218 ["Fill paragraph/comment" fill-paragraph t]
1219 "----"
1220 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1221 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1222 ("Regexp"
1223 ["Beautify" cperl-beautify-regexp
1224 cperl-use-syntax-table-text-property]
1225 ["Beautify one level deep" (cperl-beautify-regexp 1)
1226 cperl-use-syntax-table-text-property]
1227 ["Beautify a group" cperl-beautify-level
1228 cperl-use-syntax-table-text-property]
1229 ["Beautify a group one level deep" (cperl-beautify-level 1)
1230 cperl-use-syntax-table-text-property]
1231 ["Contract a group" cperl-contract-level
1232 cperl-use-syntax-table-text-property]
1233 ["Contract groups" cperl-contract-levels
1234 cperl-use-syntax-table-text-property]
1235 "----"
1236 ["Find next interpolated" cperl-next-interpolated-REx
1237 (next-single-property-change (point-min) 'REx-interpolated)]
1238 ["Find next interpolated (no //o)"
1239 cperl-next-interpolated-REx-0
1240 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1241 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1242 ["Find next interpolated (neither //o nor whole-REx)"
1243 cperl-next-interpolated-REx-1
1244 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1245 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1246 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1247 "----"
1248 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1249 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1250 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1251 "----"
1252 ["Run" mode-compile (fboundp 'mode-compile)]
1253 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1254 (get-buffer "*compilation*"))]
1255 ["Next error" next-error (get-buffer "*compilation*")]
1256 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1257 "----"
1258 ["Debugger" cperl-db t]
1259 "----"
1260 ("Tools"
1261 ["Imenu" imenu (fboundp 'imenu)]
1262 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1263 "----"
1264 ["Ispell PODs" cperl-pod-spell
1265 ;; Better not to update syntaxification here:
1266 ;; debugging syntaxificatio can be broken by this???
1268 (get-text-property (point-min) 'in-pod)
1269 (< (progn
1270 (and cperl-syntaxify-for-menu
1271 (cperl-update-syntaxification (point-max) (point-max)))
1272 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1273 (point-max)))]
1274 ["Ispell HERE-DOCs" cperl-here-doc-spell
1275 (< (progn
1276 (and cperl-syntaxify-for-menu
1277 (cperl-update-syntaxification (point-max) (point-max)))
1278 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1279 (point-max))]
1280 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1281 (eq 'here-doc (progn
1282 (and cperl-syntaxify-for-menu
1283 (cperl-update-syntaxification (point) (point)))
1284 (get-text-property (point) 'syntax-type)))]
1285 ["Select this HERE-DOC or POD section"
1286 cperl-select-this-pod-or-here-doc
1287 (memq (progn
1288 (and cperl-syntaxify-for-menu
1289 (cperl-update-syntaxification (point) (point)))
1290 (get-text-property (point) 'syntax-type))
1291 '(here-doc pod))]
1292 "----"
1293 ["CPerl pretty print (exprmntl)" cperl-ps-print
1294 (fboundp 'ps-extend-face-list)]
1295 "----"
1296 ["Syntaxify region" cperl-find-pods-heres-region
1297 (cperl-use-region-p)]
1298 ["Profile syntaxification" cperl-time-fontification t]
1299 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1300 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1301 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1302 (cperl-toggle-set-debug-unwind nil t) t]
1303 "----"
1304 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1305 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1306 ("Tags"
1307 ;;; ["Create tags for current file" cperl-etags t]
1308 ;;; ["Add tags for current file" (cperl-etags t) t]
1309 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1310 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1311 ;;; ["Create tags for Perl files in (sub)directories"
1312 ;;; (cperl-etags nil 'recursive) t]
1313 ;;; ["Add tags for Perl files in (sub)directories"
1314 ;;; (cperl-etags t 'recursive) t])
1315 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1316 ["Create tags for current file" (cperl-write-tags nil t) t]
1317 ["Add tags for current file" (cperl-write-tags) t]
1318 ["Create tags for Perl files in directory"
1319 (cperl-write-tags nil t nil t) t]
1320 ["Add tags for Perl files in directory"
1321 (cperl-write-tags nil nil nil t) t]
1322 ["Create tags for Perl files in (sub)directories"
1323 (cperl-write-tags nil t t t) t]
1324 ["Add tags for Perl files in (sub)directories"
1325 (cperl-write-tags nil nil t t) t]))
1326 ("Perl docs"
1327 ["Define word at point" imenu-go-find-at-position
1328 (fboundp 'imenu-go-find-at-position)]
1329 ["Help on function" cperl-info-on-command t]
1330 ["Help on function at point" cperl-info-on-current-command t]
1331 ["Help on symbol at point" cperl-get-help t]
1332 ["Perldoc" cperl-perldoc t]
1333 ["Perldoc on word at point" cperl-perldoc-at-point t]
1334 ["View manpage of POD in this file" cperl-build-manpage t]
1335 ["Auto-help on" cperl-lazy-install
1336 (and (fboundp 'run-with-idle-timer)
1337 (not cperl-lazy-installed))]
1338 ["Auto-help off" cperl-lazy-unstall
1339 (and (fboundp 'run-with-idle-timer)
1340 cperl-lazy-installed)])
1341 ("Toggle..."
1342 ["Auto newline" cperl-toggle-auto-newline t]
1343 ["Electric parens" cperl-toggle-electric t]
1344 ["Electric keywords" cperl-toggle-abbrev t]
1345 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1346 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1347 ["Auto fill" auto-fill-mode t])
1348 ("Indent styles..."
1349 ["CPerl" (cperl-set-style "CPerl") t]
1350 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1351 ["GNU" (cperl-set-style "GNU") t]
1352 ["C++" (cperl-set-style "C++") t]
1353 ["K&R" (cperl-set-style "K&R") t]
1354 ["BSD" (cperl-set-style "BSD") t]
1355 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1356 ["Memorize Current" (cperl-set-style "Current") t]
1357 ["Memorized" (cperl-set-style-back) cperl-old-style])
1358 ("Micro-docs"
1359 ["Tips" (describe-variable 'cperl-tips) t]
1360 ["Problems" (describe-variable 'cperl-problems) t]
1361 ["Speed" (describe-variable 'cperl-speed) t]
1362 ["Praise" (describe-variable 'cperl-praise) t]
1363 ["Faces" (describe-variable 'cperl-tips-faces) t]
1364 ["CPerl mode" (describe-function 'cperl-mode) t]
1365 ["CPerl version"
1366 (message "The version of master-file for this CPerl is %s-Emacs"
1367 cperl-version) t]))))
1368 (error nil))
1370 (autoload 'c-macro-expand "cmacexp"
1371 "Display the result of expanding all C macros occurring in the region.
1372 The expansion is entirely correct because it uses the C preprocessor."
1375 ;;; These two must be unwound, otherwise take exponential time
1376 (defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1377 "Regular expression to match optional whitespace with interpspersed comments.
1378 Should contain exactly one group.")
1380 ;;; This one is tricky to unwind; still very inefficient...
1381 (defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1382 "Regular expression to match whitespace with interpspersed comments.
1383 Should contain exactly one group.")
1386 ;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1387 ;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1388 ;;; Details of groups in this may be used in several functions; see comments
1389 ;;; near mentioned above variable(s)...
1390 ;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1391 (defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1392 "Match the text after `sub' in a subroutine declaration.
1393 If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1394 of attributes (if present), or end of the name or prototype (whatever is
1395 the last)."
1396 (concat ; Assume n groups before this...
1397 "\\(" ; n+1=name-group
1398 cperl-white-and-comment-rex ; n+2=pre-name
1399 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1400 "\\)" ; END n+1=name-group
1401 (if named "" "?")
1402 "\\(" ; n+4=proto-group
1403 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1404 "\\(([^()]*)\\)" ; n+6=prototype
1405 "\\)?" ; END n+4=proto-group
1406 "\\(" ; n+7=attr-group
1407 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1408 "\\(" ; n+9=start-attr
1410 (if attr (concat
1411 "\\("
1412 cperl-maybe-white-and-comment-rex ; whitespace-comments
1413 "\\(\\sw\\|_\\)+" ; attr-name
1414 ;; attr-arg (1 level of internal parens allowed!)
1415 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1416 "\\(" ; optional : (XXX allows trailing???)
1417 cperl-maybe-white-and-comment-rex ; whitespace-comments
1418 ":\\)?"
1419 "\\)+")
1420 "[^:]")
1421 "\\)"
1422 "\\)?" ; END n+6=proto-group
1425 ;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1426 ;;; and `cperl-outline-level'.
1427 ;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1428 (defvar cperl-imenu--function-name-regexp-perl
1429 (concat
1430 "^\\(" ; 1 = all
1431 "\\([ \t]*package" ; 2 = package-group
1432 "\\(" ; 3 = package-name-group
1433 cperl-white-and-comment-rex ; 4 = pre-package-name
1434 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1435 "\\|"
1436 "[ \t]*sub"
1437 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1438 cperl-maybe-white-and-comment-rex ; 15=pre-block
1439 "\\|"
1440 "=head\\([1-4]\\)[ \t]+" ; 16=level
1441 "\\([^\n]+\\)$" ; 17=text
1442 "\\)"))
1444 (defvar cperl-outline-regexp
1445 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1447 (defvar cperl-mode-syntax-table nil
1448 "Syntax table in use in CPerl mode buffers.")
1450 (defvar cperl-string-syntax-table nil
1451 "Syntax table in use in CPerl mode string-like chunks.")
1453 (defsubst cperl-1- (p)
1454 (max (point-min) (1- p)))
1456 (defsubst cperl-1+ (p)
1457 (min (point-max) (1+ p)))
1459 (if cperl-mode-syntax-table
1461 (setq cperl-mode-syntax-table (make-syntax-table))
1462 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1463 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1464 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1465 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1466 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1467 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1468 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1469 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1470 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1471 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1472 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1473 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1474 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1475 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1476 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1477 (if cperl-under-as-char
1478 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1479 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1480 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1481 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1482 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1483 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1484 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
1485 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
1489 (defvar cperl-faces-init nil)
1490 ;; Fix for msb.el
1491 (defvar cperl-msb-fixed nil)
1492 (defvar cperl-use-major-mode 'cperl-mode)
1493 (defvar cperl-font-lock-multiline-start nil)
1494 (defvar cperl-font-lock-multiline nil)
1495 (defvar cperl-compilation-error-regexp-alist nil)
1496 (defvar cperl-font-locking nil)
1498 ;;;###autoload
1499 (defun cperl-mode ()
1500 "Major mode for editing Perl code.
1501 Expression and list commands understand all C brackets.
1502 Tab indents for Perl code.
1503 Paragraphs are separated by blank lines only.
1504 Delete converts tabs to spaces as it moves back.
1506 Various characters in Perl almost always come in pairs: {}, (), [],
1507 sometimes <>. When the user types the first, she gets the second as
1508 well, with optional special formatting done on {}. (Disabled by
1509 default.) You can always quote (with \\[quoted-insert]) the left
1510 \"paren\" to avoid the expansion. The processing of < is special,
1511 since most the time you mean \"less\". CPerl mode tries to guess
1512 whether you want to type pair <>, and inserts is if it
1513 appropriate. You can set `cperl-electric-parens-string' to the string that
1514 contains the parenths from the above list you want to be electrical.
1515 Electricity of parenths is controlled by `cperl-electric-parens'.
1516 You may also set `cperl-electric-parens-mark' to have electric parens
1517 look for active mark and \"embrace\" a region if possible.'
1519 CPerl mode provides expansion of the Perl control constructs:
1521 if, else, elsif, unless, while, until, continue, do,
1522 for, foreach, formy and foreachmy.
1524 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1526 The user types the keyword immediately followed by a space, which
1527 causes the construct to be expanded, and the point is positioned where
1528 she is most likely to want to be. eg. when the user types a space
1529 following \"if\" the following appears in the buffer: if () { or if ()
1530 } { } and the cursor is between the parentheses. The user can then
1531 type some boolean expression within the parens. Having done that,
1532 typing \\[cperl-linefeed] places you - appropriately indented - on a
1533 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1534 directive line, then appropriate number of new lines is inserted).
1536 If CPerl decides that you want to insert \"English\" style construct like
1538 bite if angry;
1540 it will not do any expansion. See also help on variable
1541 `cperl-extra-newline-before-brace'. (Note that one can switch the
1542 help message on expansion by setting `cperl-message-electric-keyword'
1543 to nil.)
1545 \\[cperl-linefeed] is a convenience replacement for typing carriage
1546 return. It places you in the next line with proper indentation, or if
1547 you type it inside the inline block of control construct, like
1549 foreach (@lines) {print; print}
1551 and you are on a boundary of a statement inside braces, it will
1552 transform the construct into a multiline and will place you into an
1553 appropriately indented blank line. If you need a usual
1554 `newline-and-indent' behavior, it is on \\[newline-and-indent],
1555 see documentation on `cperl-electric-linefeed'.
1557 Use \\[cperl-invert-if-unless] to change a construction of the form
1559 if (A) { B }
1561 into
1563 B if A;
1565 \\{cperl-mode-map}
1567 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1568 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1569 on electric space between $ and {, `cperl-electric-parens-string' is
1570 the string that contains parentheses that should be electric in CPerl
1571 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1572 setting `cperl-electric-keywords' enables electric expansion of
1573 control structures in CPerl. `cperl-electric-linefeed' governs which
1574 one of two linefeed behavior is preferable. You can enable all these
1575 options simultaneously (recommended mode of use) by setting
1576 `cperl-hairy' to t. In this case you can switch separate options off
1577 by setting them to `null'. Note that one may undo the extra
1578 whitespace inserted by semis and braces in `auto-newline'-mode by
1579 consequent \\[cperl-electric-backspace].
1581 If your site has perl5 documentation in info format, you can use commands
1582 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1583 These keys run commands `cperl-info-on-current-command' and
1584 `cperl-info-on-command', which one is which is controlled by variable
1585 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1586 \(in turn affected by `cperl-hairy').
1588 Even if you have no info-format documentation, short one-liner-style
1589 help is available on \\[cperl-get-help], and one can run perldoc or
1590 man via menu.
1592 It is possible to show this help automatically after some idle time.
1593 This is regulated by variable `cperl-lazy-help-time'. Default with
1594 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1595 secs idle time . It is also possible to switch this on/off from the
1596 menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1598 Use \\[cperl-lineup] to vertically lineup some construction - put the
1599 beginning of the region at the start of construction, and make region
1600 span the needed amount of lines.
1602 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1603 `cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1604 here-docs sections. With capable Emaxen results of scan are used
1605 for indentation too, otherwise they are used for highlighting only.
1607 Variables controlling indentation style:
1608 `cperl-tab-always-indent'
1609 Non-nil means TAB in CPerl mode should always reindent the current line,
1610 regardless of where in the line point is when the TAB command is used.
1611 `cperl-indent-left-aligned-comments'
1612 Non-nil means that the comment starting in leftmost column should indent.
1613 `cperl-auto-newline'
1614 Non-nil means automatically newline before and after braces,
1615 and after colons and semicolons, inserted in Perl code. The following
1616 \\[cperl-electric-backspace] will remove the inserted whitespace.
1617 Insertion after colons requires both this variable and
1618 `cperl-auto-newline-after-colon' set.
1619 `cperl-auto-newline-after-colon'
1620 Non-nil means automatically newline even after colons.
1621 Subject to `cperl-auto-newline' setting.
1622 `cperl-indent-level'
1623 Indentation of Perl statements within surrounding block.
1624 The surrounding block's indentation is the indentation
1625 of the line on which the open-brace appears.
1626 `cperl-continued-statement-offset'
1627 Extra indentation given to a substatement, such as the
1628 then-clause of an if, or body of a while, or just a statement continuation.
1629 `cperl-continued-brace-offset'
1630 Extra indentation given to a brace that starts a substatement.
1631 This is in addition to `cperl-continued-statement-offset'.
1632 `cperl-brace-offset'
1633 Extra indentation for line if it starts with an open brace.
1634 `cperl-brace-imaginary-offset'
1635 An open brace following other text is treated as if it the line started
1636 this far to the right of the actual line indentation.
1637 `cperl-label-offset'
1638 Extra indentation for line that is a label.
1639 `cperl-min-label-indent'
1640 Minimal indentation for line that is a label.
1642 Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1643 `cperl-indent-level' 5 4 2 4
1644 `cperl-brace-offset' 0 0 0 0
1645 `cperl-continued-brace-offset' -5 -4 0 0
1646 `cperl-label-offset' -5 -4 -2 -4
1647 `cperl-continued-statement-offset' 5 4 2 4
1649 CPerl knows several indentation styles, and may bulk set the
1650 corresponding variables. Use \\[cperl-set-style] to do this. Use
1651 \\[cperl-set-style-back] to restore the memorized preexisting values
1652 \(both available from menu). See examples in `cperl-style-examples'.
1654 Part of the indentation style is how different parts of if/elsif/else
1655 statements are broken into lines; in CPerl, this is reflected on how
1656 templates for these constructs are created (controlled by
1657 `cperl-extra-newline-before-brace'), and how reflow-logic should treat \"continuation\" blocks of else/elsif/continue, controlled by the same variable,
1658 and by `cperl-extra-newline-before-brace-multiline',
1659 `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1661 If `cperl-indent-level' is 0, the statement after opening brace in
1662 column 0 is indented on
1663 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1665 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1666 with no args.
1668 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1669 or as help on variables `cperl-tips', `cperl-problems',
1670 `cperl-praise', `cperl-speed'."
1671 (interactive)
1672 (kill-all-local-variables)
1673 (use-local-map cperl-mode-map)
1674 (if (cperl-val 'cperl-electric-linefeed)
1675 (progn
1676 (local-set-key "\C-J" 'cperl-linefeed)
1677 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1678 (if (and
1679 (cperl-val 'cperl-clobber-lisp-bindings)
1680 (cperl-val 'cperl-info-on-command-no-prompt))
1681 (progn
1682 ;; don't clobber the backspace binding:
1683 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1684 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1685 [(control c) (control h) f])))
1686 (setq major-mode cperl-use-major-mode)
1687 (setq mode-name "CPerl")
1688 (if (not cperl-mode-abbrev-table)
1689 (let ((prev-a-c abbrevs-changed))
1690 (define-abbrev-table 'cperl-mode-abbrev-table '(
1691 ("if" "if" cperl-electric-keyword 0)
1692 ("elsif" "elsif" cperl-electric-keyword 0)
1693 ("while" "while" cperl-electric-keyword 0)
1694 ("until" "until" cperl-electric-keyword 0)
1695 ("unless" "unless" cperl-electric-keyword 0)
1696 ("else" "else" cperl-electric-else 0)
1697 ("continue" "continue" cperl-electric-else 0)
1698 ("for" "for" cperl-electric-keyword 0)
1699 ("foreach" "foreach" cperl-electric-keyword 0)
1700 ("formy" "formy" cperl-electric-keyword 0)
1701 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1702 ("do" "do" cperl-electric-keyword 0)
1703 ("=pod" "=pod" cperl-electric-pod 0)
1704 ("=over" "=over" cperl-electric-pod 0)
1705 ("=head1" "=head1" cperl-electric-pod 0)
1706 ("=head2" "=head2" cperl-electric-pod 0)
1707 ("pod" "pod" cperl-electric-pod 0)
1708 ("over" "over" cperl-electric-pod 0)
1709 ("head1" "head1" cperl-electric-pod 0)
1710 ("head2" "head2" cperl-electric-pod 0)))
1711 (setq abbrevs-changed prev-a-c)))
1712 (setq local-abbrev-table cperl-mode-abbrev-table)
1713 (if (cperl-val 'cperl-electric-keywords)
1714 (abbrev-mode 1))
1715 (set-syntax-table cperl-mode-syntax-table)
1716 ;; Until Emacs is multi-threaded, we do not actually need it local:
1717 (make-local-variable 'cperl-font-lock-multiline-start)
1718 (make-local-variable 'cperl-font-locking)
1719 (make-local-variable 'outline-regexp)
1720 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1721 (setq outline-regexp cperl-outline-regexp)
1722 (make-local-variable 'outline-level)
1723 (setq outline-level 'cperl-outline-level)
1724 (make-local-variable 'paragraph-start)
1725 (setq paragraph-start (concat "^$\\|" page-delimiter))
1726 (make-local-variable 'paragraph-separate)
1727 (setq paragraph-separate paragraph-start)
1728 (make-local-variable 'paragraph-ignore-fill-prefix)
1729 (setq paragraph-ignore-fill-prefix t)
1730 (if cperl-xemacs-p
1731 (progn
1732 (make-local-variable 'paren-backwards-message)
1733 (set 'paren-backwards-message t)))
1734 (make-local-variable 'indent-line-function)
1735 (setq indent-line-function 'cperl-indent-line)
1736 (make-local-variable 'require-final-newline)
1737 (setq require-final-newline mode-require-final-newline)
1738 (make-local-variable 'comment-start)
1739 (setq comment-start "# ")
1740 (make-local-variable 'comment-end)
1741 (setq comment-end "")
1742 (make-local-variable 'comment-column)
1743 (setq comment-column cperl-comment-column)
1744 (make-local-variable 'comment-start-skip)
1745 (setq comment-start-skip "#+ *")
1746 (make-local-variable 'defun-prompt-regexp)
1747 ;;; "[ \t]*sub"
1748 ;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1749 ;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1750 (setq defun-prompt-regexp
1751 (concat "^[ \t]*\\(sub"
1752 (cperl-after-sub-regexp 'named 'attr-groups)
1753 "\\|" ; per toke.c
1754 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1755 "\\)"
1756 cperl-maybe-white-and-comment-rex))
1757 (make-local-variable 'comment-indent-function)
1758 (setq comment-indent-function 'cperl-comment-indent)
1759 (and (boundp 'fill-paragraph-function)
1760 (progn
1761 (make-local-variable 'fill-paragraph-function)
1762 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
1763 (make-local-variable 'parse-sexp-ignore-comments)
1764 (setq parse-sexp-ignore-comments t)
1765 (make-local-variable 'indent-region-function)
1766 (setq indent-region-function 'cperl-indent-region)
1767 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1768 (make-local-variable 'imenu-create-index-function)
1769 (setq imenu-create-index-function
1770 (function cperl-imenu--create-perl-index))
1771 (make-local-variable 'imenu-sort-function)
1772 (setq imenu-sort-function nil)
1773 (make-local-variable 'vc-rcs-header)
1774 (set 'vc-rcs-header cperl-vc-rcs-header)
1775 (make-local-variable 'vc-sccs-header)
1776 (set 'vc-sccs-header cperl-vc-sccs-header)
1777 ;; This one is obsolete...
1778 (make-local-variable 'vc-header-alist)
1779 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1780 (` ((SCCS (, (car cperl-vc-sccs-header)))
1781 (RCS (, (car cperl-vc-rcs-header)))))))
1782 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1783 (make-local-variable 'compilation-error-regexp-alist-alist)
1784 (set 'compilation-error-regexp-alist-alist
1785 (cons (cons 'cperl cperl-compilation-error-regexp-alist)
1786 (symbol-value 'compilation-error-regexp-alist-alist)))
1787 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1788 (let ((f 'compilation-build-compilation-error-regexp-alist))
1789 (funcall f))
1790 (push 'cperl compilation-error-regexp-alist)))
1791 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1792 (make-local-variable 'compilation-error-regexp-alist)
1793 (set 'compilation-error-regexp-alist
1794 (cons cperl-compilation-error-regexp-alist
1795 (symbol-value 'compilation-error-regexp-alist)))))
1796 (make-local-variable 'font-lock-defaults)
1797 (setq font-lock-defaults
1798 (cond
1799 ((string< emacs-version "19.30")
1800 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1801 ((string< emacs-version "19.33") ; Which one to use?
1802 '((cperl-font-lock-keywords
1803 cperl-font-lock-keywords-1
1804 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1806 '((cperl-load-font-lock-keywords
1807 cperl-load-font-lock-keywords-1
1808 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1809 (make-local-variable 'cperl-syntax-state)
1810 (setq cperl-syntax-state nil) ; reset syntaxification cache
1811 (if cperl-use-syntax-table-text-property
1812 (progn
1813 (make-local-variable 'parse-sexp-lookup-properties)
1814 ;; Do not introduce variable if not needed, we check it!
1815 (set 'parse-sexp-lookup-properties t)
1816 ;; Fix broken font-lock:
1817 (or (boundp 'font-lock-unfontify-region-function)
1818 (set 'font-lock-unfontify-region-function
1819 'font-lock-default-unfontify-region))
1820 (unless cperl-xemacs-p ; Our: just a plug for wrong font-lock
1821 (make-local-variable 'font-lock-unfontify-region-function)
1822 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1823 'cperl-font-lock-unfontify-region-function))
1824 (make-local-variable 'cperl-syntax-done-to)
1825 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1826 (make-local-variable 'font-lock-syntactic-keywords)
1827 (setq font-lock-syntactic-keywords
1828 (if cperl-syntaxify-by-font-lock
1829 '((cperl-fontify-syntaxically))
1830 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1831 ;; used to ignore syntax-table text-properties. (t) is a hack
1832 ;; to make font-lock think that font-lock-syntactic-keywords
1833 ;; are defined.
1834 '(t)))))
1835 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1836 (progn
1837 (setq cperl-font-lock-multiline t) ; Not localized...
1838 (set 'font-lock-multiline t)) ; not present with old Emacs; auto-local
1839 (make-local-variable 'font-lock-fontify-region-function)
1840 (set 'font-lock-fontify-region-function ; not present with old Emacs
1841 'cperl-font-lock-fontify-region-function))
1842 (make-local-variable 'font-lock-fontify-region-function)
1843 (set 'font-lock-fontify-region-function ; not present with old Emacs
1844 'cperl-font-lock-fontify-region-function)
1845 (make-local-variable 'cperl-old-style)
1846 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1847 (set (make-local-variable 'normal-auto-fill-function)
1848 'cperl-do-auto-fill)
1849 (or (fboundp 'cperl-old-auto-fill-mode)
1850 (progn
1851 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1852 (defun auto-fill-mode (&optional arg)
1853 (interactive "P")
1854 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1855 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1856 (setq auto-fill-function 'cperl-do-auto-fill))))))
1857 (if (cperl-enable-font-lock)
1858 (if (cperl-val 'cperl-font-lock)
1859 (progn (or cperl-faces-init (cperl-init-faces))
1860 (font-lock-mode 1))))
1861 (set (make-local-variable 'facemenu-add-face-function)
1862 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1863 (and (boundp 'msb-menu-cond)
1864 (not cperl-msb-fixed)
1865 (cperl-msb-fix))
1866 (if (featurep 'easymenu)
1867 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1868 (run-mode-hooks 'cperl-mode-hook)
1869 (if cperl-hook-after-change
1870 (progn
1871 (make-local-hook 'after-change-functions)
1872 (add-hook 'after-change-functions 'cperl-after-change-function nil t)))
1873 ;; After hooks since fontification will break this
1874 (if cperl-pod-here-scan
1875 (or cperl-syntaxify-by-font-lock
1876 (progn (or cperl-faces-init (cperl-init-faces-weak))
1877 (cperl-find-pods-heres)))))
1879 ;; Fix for perldb - make default reasonable
1880 (defun cperl-db ()
1881 (interactive)
1882 (require 'gud)
1883 (perldb (read-from-minibuffer "Run perldb (like this): "
1884 (if (consp gud-perldb-history)
1885 (car gud-perldb-history)
1886 (concat "perl " ;;(file-name-nondirectory
1887 ;; I have problems
1888 ;; in OS/2
1889 ;; otherwise
1890 (buffer-file-name)))
1891 nil nil
1892 '(gud-perldb-history . 1))))
1894 (defun cperl-msb-fix ()
1895 ;; Adds perl files to msb menu, supposes that msb is already loaded
1896 (setq cperl-msb-fixed t)
1897 (let* ((l (length msb-menu-cond))
1898 (last (nth (1- l) msb-menu-cond))
1899 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1900 (handle (1- (nth 1 last))))
1901 (setcdr precdr (list
1902 (list
1903 '(memq major-mode '(cperl-mode perl-mode))
1904 handle
1905 "Perl Files (%d)")
1906 last))))
1908 ;; This is used by indent-for-comment
1909 ;; to decide how much to indent a comment in CPerl code
1910 ;; based on its context. Do fallback if comment is found wrong.
1912 (defvar cperl-wrong-comment)
1913 (defvar cperl-st-cfence '(14)) ; Comment-fence
1914 (defvar cperl-st-sfence '(15)) ; String-fence
1915 (defvar cperl-st-punct '(1))
1916 (defvar cperl-st-word '(2))
1917 (defvar cperl-st-bra '(4 . ?\>))
1918 (defvar cperl-st-ket '(5 . ?\<))
1921 (defun cperl-comment-indent () ; called at point at supposed comment
1922 (let ((p (point)) (c (current-column)) was phony)
1923 (if (and (not cperl-indent-comment-at-column-0)
1924 (looking-at "^#"))
1925 0 ; Existing comment at bol stays there.
1926 ;; Wrong comment found
1927 (save-excursion
1928 (setq was (cperl-to-comment-or-eol)
1929 phony (eq (get-text-property (point) 'syntax-table)
1930 cperl-st-cfence))
1931 (if phony
1932 (progn ; Too naive???
1933 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1934 (if (eq (preceding-char) ?\#)
1935 (forward-char -1))
1936 (setq was nil)))
1937 (if (= (point) p) ; Our caller found a correct place
1938 (progn
1939 (skip-chars-backward " \t")
1940 (setq was (current-column))
1941 (if (eq was 0)
1942 comment-column
1943 (max (1+ was) ; Else indent at comment column
1944 comment-column)))
1945 ;; No, the caller found a random place; we need to edit ourselves
1946 (if was nil
1947 (insert comment-start)
1948 (backward-char (length comment-start)))
1949 (setq cperl-wrong-comment t)
1950 (cperl-make-indent comment-column 1) ; Indent min 1
1951 c)))))
1953 ;;;(defun cperl-comment-indent-fallback ()
1954 ;;; "Is called if the standard comment-search procedure fails.
1955 ;;;Point is at start of real comment."
1956 ;;; (let ((c (current-column)) target cnt prevc)
1957 ;;; (if (= c comment-column) nil
1958 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1959 ;;; (setq target (max (1+ (setq prevc
1960 ;;; (current-column))) ; Else indent at comment column
1961 ;;; comment-column))
1962 ;;; (if (= c comment-column) nil
1963 ;;; (delete-backward-char cnt)
1964 ;;; (while (< prevc target)
1965 ;;; (insert "\t")
1966 ;;; (setq prevc (current-column)))
1967 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1968 ;;; (while (< prevc target)
1969 ;;; (insert " ")
1970 ;;; (setq prevc (current-column)))))))
1972 (defun cperl-indent-for-comment ()
1973 "Substitute for `indent-for-comment' in CPerl."
1974 (interactive)
1975 (let (cperl-wrong-comment)
1976 (indent-for-comment)
1977 (if cperl-wrong-comment ; set by `cperl-comment-indent'
1978 (progn (cperl-to-comment-or-eol)
1979 (forward-char (length comment-start))))))
1981 (defun cperl-comment-region (b e arg)
1982 "Comment or uncomment each line in the region in CPerl mode.
1983 See `comment-region'."
1984 (interactive "r\np")
1985 (let ((comment-start "#"))
1986 (comment-region b e arg)))
1988 (defun cperl-uncomment-region (b e arg)
1989 "Uncomment or comment each line in the region in CPerl mode.
1990 See `comment-region'."
1991 (interactive "r\np")
1992 (let ((comment-start "#"))
1993 (comment-region b e (- arg))))
1995 (defvar cperl-brace-recursing nil)
1997 (defun cperl-electric-brace (arg &optional only-before)
1998 "Insert character and correct line's indentation.
1999 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2000 place (even in empty line), but not after. If after \")\" and the inserted
2001 char is \"{\", insert extra newline before only if
2002 `cperl-extra-newline-before-brace'."
2003 (interactive "P")
2004 (let (insertpos
2005 (other-end (if (and cperl-electric-parens-mark
2006 (cperl-mark-active)
2007 (< (mark) (point)))
2008 (mark)
2009 nil)))
2010 (if (and other-end
2011 (not cperl-brace-recursing)
2012 (cperl-val 'cperl-electric-parens)
2013 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2014 ;; Need to insert a matching pair
2015 (progn
2016 (save-excursion
2017 (setq insertpos (point-marker))
2018 (goto-char other-end)
2019 (setq last-command-char ?\{)
2020 (cperl-electric-lbrace arg insertpos))
2021 (forward-char 1))
2022 ;; Check whether we close something "usual" with `}'
2023 (if (and (eq last-command-char ?\})
2024 (not
2025 (condition-case nil
2026 (save-excursion
2027 (up-list (- (prefix-numeric-value arg)))
2028 ;;(cperl-after-block-p (point-min))
2029 (or (cperl-after-expr-p nil "{;)")
2030 ;; after sub, else, continue
2031 (cperl-after-block-p nil 'pre)))
2032 (error nil))))
2033 ;; Just insert the guy
2034 (self-insert-command (prefix-numeric-value arg))
2035 (if (and (not arg) ; No args, end (of empty line or auto)
2036 (eolp)
2037 (or (and (null only-before)
2038 (save-excursion
2039 (skip-chars-backward " \t")
2040 (bolp)))
2041 (and (eq last-command-char ?\{) ; Do not insert newline
2042 ;; if after ")" and `cperl-extra-newline-before-brace'
2043 ;; is nil, do not insert extra newline.
2044 (not cperl-extra-newline-before-brace)
2045 (save-excursion
2046 (skip-chars-backward " \t")
2047 (eq (preceding-char) ?\))))
2048 (if cperl-auto-newline
2049 (progn (cperl-indent-line) (newline) t) nil)))
2050 (progn
2051 (self-insert-command (prefix-numeric-value arg))
2052 (cperl-indent-line)
2053 (if cperl-auto-newline
2054 (setq insertpos (1- (point))))
2055 (if (and cperl-auto-newline (null only-before))
2056 (progn
2057 (newline)
2058 (cperl-indent-line)))
2059 (save-excursion
2060 (if insertpos (progn (goto-char insertpos)
2061 (search-forward (make-string
2062 1 last-command-char))
2063 (setq insertpos (1- (point)))))
2064 (delete-char -1))))
2065 (if insertpos
2066 (save-excursion
2067 (goto-char insertpos)
2068 (self-insert-command (prefix-numeric-value arg)))
2069 (self-insert-command (prefix-numeric-value arg)))))))
2071 (defun cperl-electric-lbrace (arg &optional end)
2072 "Insert character, correct line's indentation, correct quoting by space."
2073 (interactive "P")
2074 (let ((cperl-brace-recursing t)
2075 (cperl-auto-newline cperl-auto-newline)
2076 (other-end (or end
2077 (if (and cperl-electric-parens-mark
2078 (cperl-mark-active)
2079 (> (mark) (point)))
2080 (save-excursion
2081 (goto-char (mark))
2082 (point-marker))
2083 nil)))
2084 pos after)
2085 (and (cperl-val 'cperl-electric-lbrace-space)
2086 (eq (preceding-char) ?$)
2087 (save-excursion
2088 (skip-chars-backward "$")
2089 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2090 (insert ?\s))
2091 ;; Check whether we are in comment
2092 (if (and
2093 (save-excursion
2094 (beginning-of-line)
2095 (not (looking-at "[ \t]*#")))
2096 (cperl-after-expr-p nil "{;)"))
2098 (setq cperl-auto-newline nil))
2099 (cperl-electric-brace arg)
2100 (and (cperl-val 'cperl-electric-parens)
2101 (eq last-command-char ?{)
2102 (memq last-command-char
2103 (append cperl-electric-parens-string nil))
2104 (or (if other-end (goto-char (marker-position other-end)))
2106 (setq last-command-char ?} pos (point))
2107 (progn (cperl-electric-brace arg t)
2108 (goto-char pos)))))
2110 (defun cperl-electric-paren (arg)
2111 "Insert an opening parenthesis or a matching pair of parentheses.
2112 See `cperl-electric-parens'."
2113 (interactive "P")
2114 (let ((beg (save-excursion (beginning-of-line) (point)))
2115 (other-end (if (and cperl-electric-parens-mark
2116 (cperl-mark-active)
2117 (> (mark) (point)))
2118 (save-excursion
2119 (goto-char (mark))
2120 (point-marker))
2121 nil)))
2122 (if (and (cperl-val 'cperl-electric-parens)
2123 (memq last-command-char
2124 (append cperl-electric-parens-string nil))
2125 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2126 ;;(not (save-excursion (search-backward "#" beg t)))
2127 (if (eq last-command-char ?<)
2128 (progn
2129 (and abbrev-mode ; later it is too late, may be after `for'
2130 (expand-abbrev))
2131 (cperl-after-expr-p nil "{;(,:="))
2133 (progn
2134 (self-insert-command (prefix-numeric-value arg))
2135 (if other-end (goto-char (marker-position other-end)))
2136 (insert (make-string
2137 (prefix-numeric-value arg)
2138 (cdr (assoc last-command-char '((?{ .?})
2139 (?[ . ?])
2140 (?( . ?))
2141 (?< . ?>))))))
2142 (forward-char (- (prefix-numeric-value arg))))
2143 (self-insert-command (prefix-numeric-value arg)))))
2145 (defun cperl-electric-rparen (arg)
2146 "Insert a matching pair of parentheses if marking is active.
2147 If not, or if we are not at the end of marking range, would self-insert.
2148 Affected by `cperl-electric-parens'."
2149 (interactive "P")
2150 (let ((beg (save-excursion (beginning-of-line) (point)))
2151 (other-end (if (and cperl-electric-parens-mark
2152 (cperl-val 'cperl-electric-parens)
2153 (memq last-command-char
2154 (append cperl-electric-parens-string nil))
2155 (cperl-mark-active)
2156 (< (mark) (point)))
2157 (mark)
2158 nil))
2160 (if (and other-end
2161 (cperl-val 'cperl-electric-parens)
2162 (memq last-command-char '( ?\) ?\] ?\} ?\> ))
2163 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2164 ;;(not (save-excursion (search-backward "#" beg t)))
2166 (progn
2167 (self-insert-command (prefix-numeric-value arg))
2168 (setq p (point))
2169 (if other-end (goto-char other-end))
2170 (insert (make-string
2171 (prefix-numeric-value arg)
2172 (cdr (assoc last-command-char '((?\} . ?\{)
2173 (?\] . ?\[)
2174 (?\) . ?\()
2175 (?\> . ?\<))))))
2176 (goto-char (1+ p)))
2177 (self-insert-command (prefix-numeric-value arg)))))
2179 (defun cperl-electric-keyword ()
2180 "Insert a construction appropriate after a keyword.
2181 Help message may be switched off by setting `cperl-message-electric-keyword'
2182 to nil."
2183 (let ((beg (save-excursion (beginning-of-line) (point)))
2184 (dollar (and (eq last-command-char ?$)
2185 (eq this-command 'self-insert-command)))
2186 (delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
2187 (memq this-command '(self-insert-command newline))))
2188 my do)
2189 (and (save-excursion
2190 (condition-case nil
2191 (progn
2192 (backward-sexp 1)
2193 (setq do (looking-at "do\\>")))
2194 (error nil))
2195 (cperl-after-expr-p nil "{;:"))
2196 (save-excursion
2197 (not
2198 (re-search-backward
2199 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2200 beg t)))
2201 (save-excursion (or (not (re-search-backward "^=" nil t))
2203 (looking-at "=cut")
2204 (and cperl-use-syntax-table-text-property
2205 (not (eq (get-text-property (point)
2206 'syntax-type)
2207 'pod))))))
2208 (save-excursion (forward-sexp -1)
2209 (not (memq (following-char) (append "$@%&*" nil))))
2210 (progn
2211 (and (eq (preceding-char) ?y)
2212 (progn ; "foreachmy"
2213 (forward-char -2)
2214 (insert " ")
2215 (forward-char 2)
2216 (setq my t dollar t
2217 delete
2218 (memq this-command '(self-insert-command newline)))))
2219 (and dollar (insert " $"))
2220 (cperl-indent-line)
2221 ;;(insert " () {\n}")
2222 (cond
2223 (cperl-extra-newline-before-brace
2224 (insert (if do "\n" " ()\n"))
2225 (insert "{")
2226 (cperl-indent-line)
2227 (insert "\n")
2228 (cperl-indent-line)
2229 (insert "\n}")
2230 (and do (insert " while ();")))
2232 (insert (if do " {\n} while ();" " () {\n}"))))
2233 (or (looking-at "[ \t]\\|$") (insert " "))
2234 (cperl-indent-line)
2235 (if dollar (progn (search-backward "$")
2236 (if my
2237 (forward-char 1)
2238 (delete-char 1)))
2239 (search-backward ")")
2240 (if (eq last-command-char ?\()
2241 (progn ; Avoid "if (())"
2242 (delete-backward-char 1)
2243 (delete-backward-char -1))))
2244 (if delete
2245 (cperl-putback-char cperl-del-back-ch))
2246 (if cperl-message-electric-keyword
2247 (message "Precede char by C-q to avoid expansion"))))))
2249 (defun cperl-ensure-newlines (n &optional pos)
2250 "Make sure there are N newlines after the point."
2251 (or pos (setq pos (point)))
2252 (if (looking-at "\n")
2253 (forward-char 1)
2254 (insert "\n"))
2255 (if (> n 1)
2256 (cperl-ensure-newlines (1- n) pos)
2257 (goto-char pos)))
2259 (defun cperl-electric-pod ()
2260 "Insert a POD chunk appropriate after a =POD directive."
2261 (let ((delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
2262 (memq this-command '(self-insert-command newline))))
2263 head1 notlast name p really-delete over)
2264 (and (save-excursion
2265 (forward-word -1)
2266 (and
2267 (eq (preceding-char) ?=)
2268 (progn
2269 (setq head1 (looking-at "head1\\>[ \t]*$"))
2270 (setq over (and (looking-at "over\\>[ \t]*$")
2271 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2272 (forward-char -1)
2273 (bolp))
2275 (get-text-property (point) 'in-pod)
2276 (cperl-after-expr-p nil "{;:")
2277 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2278 (not (looking-at "\n*=cut"))
2279 (or (not cperl-use-syntax-table-text-property)
2280 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2281 (progn
2282 (save-excursion
2283 (setq notlast (re-search-forward "^\n=" nil t)))
2284 (or notlast
2285 (progn
2286 (insert "\n\n=cut")
2287 (cperl-ensure-newlines 2)
2288 (forward-word -2)
2289 (if (and head1
2290 (not
2291 (save-excursion
2292 (forward-char -1)
2293 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2294 nil t)))) ; Only one
2295 (progn
2296 (forward-word 1)
2297 (setq name (file-name-sans-extension
2298 (file-name-nondirectory (buffer-file-name)))
2299 p (point))
2300 (insert " NAME\n\n" name
2301 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2302 "=head1 DESCRIPTION")
2303 (cperl-ensure-newlines 4)
2304 (goto-char p)
2305 (forward-word 2)
2306 (end-of-line)
2307 (setq really-delete t))
2308 (forward-word 1))))
2309 (if over
2310 (progn
2311 (setq p (point))
2312 (insert "\n\n=item \n\n\n\n"
2313 "=back")
2314 (cperl-ensure-newlines 2)
2315 (goto-char p)
2316 (forward-word 1)
2317 (end-of-line)
2318 (setq really-delete t)))
2319 (if (and delete really-delete)
2320 (cperl-putback-char cperl-del-back-ch))))))
2322 (defun cperl-electric-else ()
2323 "Insert a construction appropriate after a keyword.
2324 Help message may be switched off by setting `cperl-message-electric-keyword'
2325 to nil."
2326 (let ((beg (save-excursion (beginning-of-line) (point))))
2327 (and (save-excursion
2328 (backward-sexp 1)
2329 (cperl-after-expr-p nil "{;:"))
2330 (save-excursion
2331 (not
2332 (re-search-backward
2333 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2334 beg t)))
2335 (save-excursion (or (not (re-search-backward "^=" nil t))
2336 (looking-at "=cut")
2337 (and cperl-use-syntax-table-text-property
2338 (not (eq (get-text-property (point)
2339 'syntax-type)
2340 'pod)))))
2341 (progn
2342 (cperl-indent-line)
2343 ;;(insert " {\n\n}")
2344 (cond
2345 (cperl-extra-newline-before-brace
2346 (insert "\n")
2347 (insert "{")
2348 (cperl-indent-line)
2349 (insert "\n\n}"))
2351 (insert " {\n\n}")))
2352 (or (looking-at "[ \t]\\|$") (insert " "))
2353 (cperl-indent-line)
2354 (forward-line -1)
2355 (cperl-indent-line)
2356 (cperl-putback-char cperl-del-back-ch)
2357 (setq this-command 'cperl-electric-else)
2358 (if cperl-message-electric-keyword
2359 (message "Precede char by C-q to avoid expansion"))))))
2361 (defun cperl-linefeed ()
2362 "Go to end of line, open a new line and indent appropriately.
2363 If in POD, insert appropriate lines."
2364 (interactive)
2365 (let ((beg (save-excursion (beginning-of-line) (point)))
2366 (end (save-excursion (end-of-line) (point)))
2367 (pos (point)) start over cut res)
2368 (if (and ; Check if we need to split:
2369 ; i.e., on a boundary and inside "{...}"
2370 (save-excursion (cperl-to-comment-or-eol)
2371 (>= (point) pos)) ; Not in a comment
2372 (or (save-excursion
2373 (skip-chars-backward " \t" beg)
2374 (forward-char -1)
2375 (looking-at "[;{]")) ; After { or ; + spaces
2376 (looking-at "[ \t]*}") ; Before }
2377 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2378 (save-excursion
2379 (and
2380 (eq (car (parse-partial-sexp pos end -1)) -1)
2381 ; Leave the level of parens
2382 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2383 ; Are at end
2384 (cperl-after-block-p (point-min))
2385 (progn
2386 (backward-sexp 1)
2387 (setq start (point-marker))
2388 (<= start pos))))) ; Redundant? Are after the
2389 ; start of parens group.
2390 (progn
2391 (skip-chars-backward " \t")
2392 (or (memq (preceding-char) (append ";{" nil))
2393 (insert ";"))
2394 (insert "\n")
2395 (forward-line -1)
2396 (cperl-indent-line)
2397 (goto-char start)
2398 (or (looking-at "{[ \t]*$") ; If there is a statement
2399 ; before, move it to separate line
2400 (progn
2401 (forward-char 1)
2402 (insert "\n")
2403 (cperl-indent-line)))
2404 (forward-line 1) ; We are on the target line
2405 (cperl-indent-line)
2406 (beginning-of-line)
2407 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2408 ; after, move it to separate line
2409 (progn
2410 (end-of-line)
2411 (search-backward "}" beg)
2412 (skip-chars-backward " \t")
2413 (or (memq (preceding-char) (append ";{" nil))
2414 (insert ";"))
2415 (insert "\n")
2416 (cperl-indent-line)
2417 (forward-line -1)))
2418 (forward-line -1) ; We are on the line before target
2419 (end-of-line)
2420 (newline-and-indent))
2421 (end-of-line) ; else - no splitting
2422 (cond
2423 ((and (looking-at "\n[ \t]*{$")
2424 (save-excursion
2425 (skip-chars-backward " \t")
2426 (eq (preceding-char) ?\)))) ; Probably if () {} group
2427 ; with an extra newline.
2428 (forward-line 2)
2429 (cperl-indent-line))
2430 ((save-excursion ; In POD header
2431 (forward-paragraph -1)
2432 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2433 ;; We are after \n now, so look for the rest
2434 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2435 (progn
2436 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2437 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2438 t)))
2439 (if (and over
2440 (progn
2441 (forward-paragraph -1)
2442 (forward-word 1)
2443 (setq pos (point))
2444 (setq cut (buffer-substring (point)
2445 (save-excursion
2446 (end-of-line)
2447 (point))))
2448 (delete-char (- (save-excursion (end-of-line) (point))
2449 (point)))
2450 (setq res (expand-abbrev))
2451 (save-excursion
2452 (goto-char pos)
2453 (insert cut))
2454 res))
2456 (cperl-ensure-newlines (if cut 2 4))
2457 (forward-line 2)))
2458 ((get-text-property (point) 'in-pod) ; In POD section
2459 (cperl-ensure-newlines 4)
2460 (forward-line 2))
2461 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2462 (forward-line 1)
2463 (cperl-indent-line))
2465 (newline-and-indent))))))
2467 (defun cperl-electric-semi (arg)
2468 "Insert character and correct line's indentation."
2469 (interactive "P")
2470 (if cperl-auto-newline
2471 (cperl-electric-terminator arg)
2472 (self-insert-command (prefix-numeric-value arg))
2473 (if cperl-autoindent-on-semi
2474 (cperl-indent-line))))
2476 (defun cperl-electric-terminator (arg)
2477 "Insert character and correct line's indentation."
2478 (interactive "P")
2479 (let ((end (point))
2480 (auto (and cperl-auto-newline
2481 (or (not (eq last-command-char ?:))
2482 cperl-auto-newline-after-colon)))
2483 insertpos)
2484 (if (and ;;(not arg)
2485 (eolp)
2486 (not (save-excursion
2487 (beginning-of-line)
2488 (skip-chars-forward " \t")
2490 ;; Ignore in comment lines
2491 (= (following-char) ?#)
2492 ;; Colon is special only after a label
2493 ;; So quickly rule out most other uses of colon
2494 ;; and do no indentation for them.
2495 (and (eq last-command-char ?:)
2496 (save-excursion
2497 (forward-word 1)
2498 (skip-chars-forward " \t")
2499 (and (< (point) end)
2500 (progn (goto-char (- end 1))
2501 (not (looking-at ":"))))))
2502 (progn
2503 (beginning-of-defun)
2504 (let ((pps (parse-partial-sexp (point) end)))
2505 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2506 (progn
2507 (self-insert-command (prefix-numeric-value arg))
2508 ;;(forward-char -1)
2509 (if auto (setq insertpos (point-marker)))
2510 ;;(forward-char 1)
2511 (cperl-indent-line)
2512 (if auto
2513 (progn
2514 (newline)
2515 (cperl-indent-line)))
2516 (save-excursion
2517 (if insertpos (goto-char (1- (marker-position insertpos)))
2518 (forward-char -1))
2519 (delete-char 1))))
2520 (if insertpos
2521 (save-excursion
2522 (goto-char insertpos)
2523 (self-insert-command (prefix-numeric-value arg)))
2524 (self-insert-command (prefix-numeric-value arg)))))
2526 (defun cperl-electric-backspace (arg)
2527 "Backspace, or remove the whitespace around the point inserted by an electric
2528 key. Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2529 (interactive "p")
2530 (if (and cperl-auto-newline
2531 (memq last-command '(cperl-electric-semi
2532 cperl-electric-terminator
2533 cperl-electric-lbrace))
2534 (memq (preceding-char) '(?\s ?\t ?\n)))
2535 (let (p)
2536 (if (eq last-command 'cperl-electric-lbrace)
2537 (skip-chars-forward " \t\n"))
2538 (setq p (point))
2539 (skip-chars-backward " \t\n")
2540 (delete-region (point) p))
2541 (and (eq last-command 'cperl-electric-else)
2542 ;; We are removing the whitespace *inside* cperl-electric-else
2543 (setq this-command 'cperl-electric-else-really))
2544 (if (and cperl-auto-newline
2545 (eq last-command 'cperl-electric-else-really)
2546 (memq (preceding-char) '(?\s ?\t ?\n)))
2547 (let (p)
2548 (skip-chars-forward " \t\n")
2549 (setq p (point))
2550 (skip-chars-backward " \t\n")
2551 (delete-region (point) p))
2552 (if cperl-electric-backspace-untabify
2553 (backward-delete-char-untabify arg)
2554 (delete-backward-char arg)))))
2556 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2558 (defun cperl-inside-parens-p () ;; NOT USED????
2559 (condition-case ()
2560 (save-excursion
2561 (save-restriction
2562 (narrow-to-region (point)
2563 (progn (beginning-of-defun) (point)))
2564 (goto-char (point-max))
2565 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2566 (error nil)))
2568 (defun cperl-indent-command (&optional whole-exp)
2569 "Indent current line as Perl code, or in some cases insert a tab character.
2570 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2571 line. Otherwise, indent the current line only if point is at the left margin
2572 or in the line's indentation; otherwise insert a tab.
2574 A numeric argument, regardless of its value,
2575 means indent rigidly all the lines of the expression starting after point
2576 so that this line becomes properly indented.
2577 The relative indentation among the lines of the expression are preserved."
2578 (interactive "P")
2579 (cperl-update-syntaxification (point) (point))
2580 (if whole-exp
2581 ;; If arg, always indent this line as Perl
2582 ;; and shift remaining lines of expression the same amount.
2583 (let ((shift-amt (cperl-indent-line))
2584 beg end)
2585 (save-excursion
2586 (if cperl-tab-always-indent
2587 (beginning-of-line))
2588 (setq beg (point))
2589 (forward-sexp 1)
2590 (setq end (point))
2591 (goto-char beg)
2592 (forward-line 1)
2593 (setq beg (point)))
2594 (if (and shift-amt (> end beg))
2595 (indent-code-rigidly beg end shift-amt "#")))
2596 (if (and (not cperl-tab-always-indent)
2597 (save-excursion
2598 (skip-chars-backward " \t")
2599 (not (bolp))))
2600 (insert-tab)
2601 (cperl-indent-line))))
2603 (defun cperl-indent-line (&optional parse-data)
2604 "Indent current line as Perl code.
2605 Return the amount the indentation changed by."
2606 (let ((case-fold-search nil)
2607 (pos (- (point-max) (point)))
2608 indent i beg shift-amt)
2609 (setq indent (cperl-calculate-indent parse-data)
2610 i indent)
2611 (beginning-of-line)
2612 (setq beg (point))
2613 (cond ((or (eq indent nil) (eq indent t))
2614 (setq indent (current-indentation) i nil))
2615 ;;((eq indent t) ; Never?
2616 ;; (setq indent (cperl-calculate-indent-within-comment)))
2617 ;;((looking-at "[ \t]*#")
2618 ;; (setq indent 0))
2620 (skip-chars-forward " \t")
2621 (if (listp indent) (setq indent (car indent)))
2622 (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2623 (and (> indent 0)
2624 (setq indent (max cperl-min-label-indent
2625 (+ indent cperl-label-offset)))))
2626 ((= (following-char) ?})
2627 (setq indent (- indent cperl-indent-level)))
2628 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2629 (setq indent (+ indent cperl-close-paren-offset)))
2630 ((= (following-char) ?{)
2631 (setq indent (+ indent cperl-brace-offset))))))
2632 (skip-chars-forward " \t")
2633 (setq shift-amt (and i (- indent (current-column))))
2634 (if (or (not shift-amt)
2635 (zerop shift-amt))
2636 (if (> (- (point-max) pos) (point))
2637 (goto-char (- (point-max) pos)))
2638 ;;;(delete-region beg (point))
2639 ;;;(indent-to indent)
2640 (cperl-make-indent indent)
2641 ;; If initial point was within line's indentation,
2642 ;; position after the indentation. Else stay at same point in text.
2643 (if (> (- (point-max) pos) (point))
2644 (goto-char (- (point-max) pos))))
2645 shift-amt))
2647 (defun cperl-after-label ()
2648 ;; Returns true if the point is after label. Does not do save-excursion.
2649 (and (eq (preceding-char) ?:)
2650 (memq (char-syntax (char-after (- (point) 2)))
2651 '(?w ?_))
2652 (progn
2653 (backward-sexp)
2654 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2656 (defun cperl-get-state (&optional parse-start start-state)
2657 ;; returns list (START STATE DEPTH PRESTART),
2658 ;; START is a good place to start parsing, or equal to
2659 ;; PARSE-START if preset,
2660 ;; STATE is what is returned by `parse-partial-sexp'.
2661 ;; DEPTH is true is we are immediately after end of block
2662 ;; which contains START.
2663 ;; PRESTART is the position basing on which START was found.
2664 (save-excursion
2665 (let ((start-point (point)) depth state start prestart)
2666 (if (and parse-start
2667 (<= parse-start start-point))
2668 (goto-char parse-start)
2669 (beginning-of-defun)
2670 (setq start-state nil))
2671 (setq prestart (point))
2672 (if start-state nil
2673 ;; Try to go out, if sub is not on the outermost level
2674 (while (< (point) start-point)
2675 (setq start (point) parse-start start depth nil
2676 state (parse-partial-sexp start start-point -1))
2677 (if (> (car state) -1) nil
2678 ;; The current line could start like }}}, so the indentation
2679 ;; corresponds to a different level than what we reached
2680 (setq depth t)
2681 (beginning-of-line 2))) ; Go to the next line.
2682 (if start (goto-char start))) ; Not at the start of file
2683 (setq start (point))
2684 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2685 (list start state depth prestart))))
2687 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2689 (defun cperl-beginning-of-property (p prop &optional lim)
2690 "Given that P has a property PROP, find where the property starts.
2691 Will not look before LIM."
2692 ;;; XXXX What to do at point-max???
2693 (or (previous-single-property-change (cperl-1+ p) prop lim)
2694 (point-min))
2695 ;;; (cond ((eq p (point-min))
2696 ;;; p)
2697 ;;; ((and lim (<= p lim))
2698 ;;; p)
2699 ;;; ((not (get-text-property (1- p) prop))
2700 ;;; p)
2701 ;;; (t (or (previous-single-property-change p look-prop lim)
2702 ;;; (point-min))))
2705 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2706 ;; Old workhorse for calculation of indentation; the major problem
2707 ;; is that it mixes the sniffer logic to understand what the current line
2708 ;; MEANS with the logic to actually calculate where to indent it.
2709 ;; The latter part should be eventually moved to `cperl-calculate-indent';
2710 ;; actually, this is mostly done now...
2711 (cperl-update-syntaxification (point) (point))
2712 (let ((res (get-text-property (point) 'syntax-type)))
2713 (save-excursion
2714 (cond
2715 ((and (memq res '(pod here-doc here-doc-delim format))
2716 (not (get-text-property (point) 'indentable)))
2717 (vector res))
2718 ;; before start of POD - whitespace found since do not have 'pod!
2719 ((looking-at "[ \t]*\n=")
2720 (error "Spaces before POD section!"))
2721 ((and (not cperl-indent-left-aligned-comments)
2722 (looking-at "^#"))
2723 [comment-special:at-beginning-of-line])
2724 ((get-text-property (point) 'in-pod)
2725 [in-pod])
2727 (beginning-of-line)
2728 (let* ((indent-point (point))
2729 (char-after-pos (save-excursion
2730 (skip-chars-forward " \t")
2731 (point)))
2732 (char-after (char-after char-after-pos))
2733 (pre-indent-point (point))
2734 p prop look-prop is-block delim)
2735 (save-excursion ; Know we are not in POD, find appropriate pos before
2736 (cperl-backward-to-noncomment nil)
2737 (setq p (max (point-min) (1- (point)))
2738 prop (get-text-property p 'syntax-type)
2739 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2740 'syntax-type))
2741 (if (memq prop '(pod here-doc format here-doc-delim))
2742 (progn
2743 (goto-char (cperl-beginning-of-property p look-prop))
2744 (beginning-of-line)
2745 (setq pre-indent-point (point)))))
2746 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2747 (let* ((case-fold-search nil)
2748 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2749 (start (or (nth 2 parse-data) ; last complete sexp terminated
2750 (nth 0 s-s))) ; Good place to start parsing
2751 (state (nth 1 s-s))
2752 (containing-sexp (car (cdr state)))
2753 old-indent)
2754 (if (and
2755 ;;containing-sexp ;; We are buggy at toplevel :-(
2756 parse-data)
2757 (progn
2758 (setcar parse-data pre-indent-point)
2759 (setcar (cdr parse-data) state)
2760 (or (nth 2 parse-data)
2761 (setcar (cddr parse-data) start))
2762 ;; Before this point: end of statement
2763 (setq old-indent (nth 3 parse-data))))
2764 (cond ((get-text-property (point) 'indentable)
2765 ;; indent to "after" the surrounding open
2766 ;; (same offset as `cperl-beautify-regexp-piece'),
2767 ;; skip blanks if we do not close the expression.
2768 (setq delim ; We do not close the expression
2769 (get-text-property
2770 (cperl-1+ char-after-pos) 'indentable)
2771 p (1+ (cperl-beginning-of-property
2772 (point) 'indentable))
2773 is-block ; misused for: preceeding line in REx
2774 (save-excursion ; Find preceeding line
2775 (cperl-backward-to-noncomment p)
2776 (beginning-of-line)
2777 (if (<= (point) p)
2778 (progn ; get indent from the first line
2779 (goto-char p)
2780 (skip-chars-forward " \t")
2781 (if (memq (char-after (point))
2782 (append "#\n" nil))
2783 nil ; Can't use intentation of this line...
2784 (point)))
2785 (skip-chars-forward " \t")
2786 (point)))
2787 prop (parse-partial-sexp p char-after-pos))
2788 (cond ((not delim) ; End the REx, ignore is-block
2789 (vector 'indentable 'terminator p is-block))
2790 (is-block ; Indent w.r.t. preceeding line
2791 (vector 'indentable 'cont-line char-after-pos
2792 is-block char-after p))
2793 (t ; No preceeding line...
2794 (vector 'indentable 'first-line p))))
2795 ((get-text-property char-after-pos 'REx-part2)
2796 (vector 'REx-part2 (point)))
2797 ((nth 3 state)
2798 [comment])
2799 ((nth 4 state)
2800 [string])
2801 ;; XXXX Do we need to special-case this?
2802 ((null containing-sexp)
2803 ;; Line is at top level. May be data or function definition,
2804 ;; or may be function argument declaration.
2805 ;; Indent like the previous top level line
2806 ;; unless that ends in a closeparen without semicolon,
2807 ;; in which case this line is the first argument decl.
2808 (skip-chars-forward " \t")
2809 (cperl-backward-to-noncomment (or old-indent (point-min)))
2810 (setq state
2811 (or (bobp)
2812 (eq (point) old-indent) ; old-indent was at comment
2813 (eq (preceding-char) ?\;)
2814 ;; Had ?\) too
2815 (and (eq (preceding-char) ?\})
2816 (cperl-after-block-and-statement-beg
2817 (point-min))) ; Was start - too close
2818 (memq char-after (append ")]}" nil))
2819 (and (eq (preceding-char) ?\:) ; label
2820 (progn
2821 (forward-sexp -1)
2822 (skip-chars-backward " \t")
2823 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2824 (get-text-property (point) 'first-format-line)))
2826 ;; Look at previous line that's at column 0
2827 ;; to determine whether we are in top-level decls
2828 ;; or function's arg decls. Set basic-indent accordingly.
2829 ;; Now add a little if this is a continuation line.
2830 (and state
2831 parse-data
2832 (not (eq char-after ?\C-j))
2833 (setcdr (cddr parse-data)
2834 (list pre-indent-point)))
2835 (vector 'toplevel start char-after state (nth 2 s-s)))
2836 ((not
2837 (or (setq is-block
2838 (and (setq delim (= (char-after containing-sexp) ?{))
2839 (save-excursion ; Is it a hash?
2840 (goto-char containing-sexp)
2841 (cperl-block-p))))
2842 cperl-indent-parens-as-block))
2843 ;; group is an expression, not a block:
2844 ;; indent to just after the surrounding open parens,
2845 ;; skip blanks if we do not close the expression.
2846 (goto-char (1+ containing-sexp))
2847 (or (memq char-after
2848 (append (if delim "}" ")]}") nil))
2849 (looking-at "[ \t]*\\(#\\|$\\)")
2850 (skip-chars-forward " \t"))
2851 (setq old-indent (point)) ; delim=is-brace
2852 (vector 'in-parens char-after (point) delim containing-sexp))
2854 ;; Statement level. Is it a continuation or a new statement?
2855 ;; Find previous non-comment character.
2856 (goto-char pre-indent-point) ; Skip one level of POD/etc
2857 (cperl-backward-to-noncomment containing-sexp)
2858 ;; Back up over label lines, since they don't
2859 ;; affect whether our line is a continuation.
2860 ;; (Had \, too)
2861 (while;;(or (eq (preceding-char) ?\,)
2862 (and (eq (preceding-char) ?:)
2863 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2864 (memq (char-syntax (char-after (- (point) 2)))
2865 '(?w ?_))))
2867 ;; This is always FALSE?
2868 (if (eq (preceding-char) ?\,)
2869 ;; Will go to beginning of line, essentially.
2870 ;; Will ignore embedded sexpr XXXX.
2871 (cperl-backward-to-start-of-continued-exp containing-sexp))
2872 (beginning-of-line)
2873 (cperl-backward-to-noncomment containing-sexp))
2874 ;; Now we get non-label preceeding the indent point
2875 (if (not (or (eq (1- (point)) containing-sexp)
2876 (memq (preceding-char)
2877 (append (if is-block " ;{" " ,;{") '(nil)))
2878 (and (eq (preceding-char) ?\})
2879 (cperl-after-block-and-statement-beg
2880 containing-sexp))
2881 (get-text-property (point) 'first-format-line)))
2882 ;; This line is continuation of preceding line's statement;
2883 ;; indent `cperl-continued-statement-offset' more than the
2884 ;; previous line of the statement.
2886 ;; There might be a label on this line, just
2887 ;; consider it bad style and ignore it.
2888 (progn
2889 (cperl-backward-to-start-of-continued-exp containing-sexp)
2890 (vector 'continuation (point) char-after is-block delim))
2891 ;; This line starts a new statement.
2892 ;; Position following last unclosed open brace
2893 (goto-char containing-sexp)
2894 ;; Is line first statement after an open-brace?
2896 ;; If no, find that first statement and indent like
2897 ;; it. If the first statement begins with label, do
2898 ;; not believe when the indentation of the label is too
2899 ;; small.
2900 (save-excursion
2901 (forward-char 1)
2902 (let ((colon-line-end 0))
2903 (while
2904 (progn (skip-chars-forward " \t\n")
2905 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]\\|=[a-zA-Z]"))
2906 ;; Skip over comments and labels following openbrace.
2907 (cond ((= (following-char) ?\#)
2908 (forward-line 1))
2909 ((= (following-char) ?\=)
2910 (goto-char
2911 (or (next-single-property-change (point) 'in-pod)
2912 (point-max)))) ; do not loop if no syntaxification
2913 ;; label:
2915 (save-excursion (end-of-line)
2916 (setq colon-line-end (point)))
2917 (search-forward ":"))))
2918 ;; We are at beginning of code (NOT label or comment)
2919 ;; First, the following code counts
2920 ;; if it is before the line we want to indent.
2921 (and (< (point) indent-point)
2922 (vector 'have-prev-sibling (point) colon-line-end
2923 containing-sexp))))
2924 (progn
2925 ;; If no previous statement,
2926 ;; indent it relative to line brace is on.
2928 ;; For open-braces not the first thing in a line,
2929 ;; add in cperl-brace-imaginary-offset.
2931 ;; If first thing on a line: ?????
2932 ;; Move back over whitespace before the openbrace.
2933 (setq ; brace first thing on a line
2934 old-indent (progn (skip-chars-backward " \t") (bolp)))
2935 ;; Should we indent w.r.t. earlier than start?
2936 ;; Move to start of control group, possibly on a different line
2937 (or cperl-indent-wrt-brace
2938 (cperl-backward-to-noncomment (point-min)))
2939 ;; If the openbrace is preceded by a parenthesized exp,
2940 ;; move to the beginning of that;
2941 (if (eq (preceding-char) ?\))
2942 (progn
2943 (forward-sexp -1)
2944 (cperl-backward-to-noncomment (point-min))))
2945 ;; In the case it starts a subroutine, indent with
2946 ;; respect to `sub', not with respect to the
2947 ;; first thing on the line, say in the case of
2948 ;; anonymous sub in a hash.
2949 (if (and;; Is it a sub in group starting on this line?
2950 (cond ((get-text-property (point) 'attrib-group)
2951 (goto-char (cperl-beginning-of-property
2952 (point) 'attrib-group)))
2953 ((eq (preceding-char) ?b)
2954 (forward-sexp -1)
2955 (looking-at "sub\\>")))
2956 (setq p (nth 1 ; start of innermost containing list
2957 (parse-partial-sexp
2958 (save-excursion (beginning-of-line)
2959 (point))
2960 (point)))))
2961 (progn
2962 (goto-char (1+ p)) ; enclosing block on the same line
2963 (skip-chars-forward " \t")
2964 (vector 'code-start-in-block containing-sexp char-after
2965 (and delim (not is-block)) ; is a HASH
2966 old-indent ; brace first thing on a line
2967 t (point) ; have something before...
2969 ;;(current-column)
2971 ;; Get initial indentation of the line we are on.
2972 ;; If line starts with label, calculate label indentation
2973 (vector 'code-start-in-block containing-sexp char-after
2974 (and delim (not is-block)) ; is a HASH
2975 old-indent ; brace first thing on a line
2976 nil (point) ; nothing interesting before
2977 ))))))))))))))
2979 (defvar cperl-indent-rules-alist
2980 '((pod nil) ; via `syntax-type' property
2981 (here-doc nil) ; via `syntax-type' property
2982 (here-doc-delim nil) ; via `syntax-type' property
2983 (format nil) ; via `syntax-type' property
2984 (in-pod nil) ; via `in-pod' property
2985 (comment-special:at-beginning-of-line nil)
2986 (string t)
2987 (comment nil))
2988 "Alist of indentation rules for CPerl mode.
2989 The values mean:
2990 nil: do not indent;
2991 number: add this amount of indentation.
2993 Not finished.")
2995 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
2996 "Return appropriate indentation for current line as Perl code.
2997 In usual case returns an integer: the column to indent to.
2998 Returns nil if line starts inside a string, t if in a comment.
3000 Will not correct the indentation for labels, but will correct it for braces
3001 and closing parentheses and brackets."
3002 ;; This code is still a broken architecture: in some cases we need to
3003 ;; compensate for some modifications which `cperl-indent-line' will add later
3004 (save-excursion
3005 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3006 (cond
3007 ;;((or (null i) (eq i t) (numberp i))
3008 ;; i)
3009 ((vectorp i)
3010 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3011 (cond
3012 (what (cadr what)) ; Load from table
3014 ;; Indenters for regular expressions with //x and qw()
3016 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3017 (goto-char (elt i 1))
3018 (condition-case nil ; Use indentation of the 1st part
3019 (forward-sexp -1))
3020 (current-column))
3021 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3022 (cond ;;; [indentable terminator start-pos is-block]
3023 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3024 (goto-char (elt i 2)) ; After opening parens
3025 (1- (current-column)))
3026 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3027 (goto-char (elt i 2))
3028 (+ (or cperl-regexp-indent-step cperl-indent-level)
3030 (current-column)))
3031 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3032 ;; Indent as the level after closing parens
3033 (goto-char (elt i 2)) ; indent line
3034 (skip-chars-forward " \t)") ; Skip closing parens
3035 (setq p (point))
3036 (goto-char (elt i 3)) ; previous line
3037 (skip-chars-forward " \t)") ; Skip closing parens
3038 ;; Number of parens in between:
3039 (setq p (nth 0 (parse-partial-sexp (point) p))
3040 what (elt i 4)) ; First char on current line
3041 (goto-char (elt i 3)) ; previous line
3042 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3043 (cond ((eq what ?\) )
3044 (- cperl-close-paren-offset)) ; compensate
3045 ((eq what ?\| )
3046 (- (or cperl-regexp-indent-step cperl-indent-level)))
3047 (t 0))
3048 (if (eq (following-char) ?\| )
3049 (or cperl-regexp-indent-step cperl-indent-level)
3051 (current-column)))
3053 (error "Unrecognized value of indent: %s" i))))
3055 ;; Indenter for stuff at toplevel
3057 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3058 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3059 (goto-char (elt i 1)) ; start = Good place to start parsing
3060 (- (current-indentation) ;
3061 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3062 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3063 ;; Look at previous line that's at column 0
3064 ;; to determine whether we are in top-level decls
3065 ;; or function's arg decls. Set basic-indent accordingly.
3066 ;; Now add a little if this is a continuation line.
3067 (if (elt i 3) ; state (XXX What is the semantic???)
3069 cperl-continued-statement-offset)))
3071 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3073 ((eq 'in-parens (elt i 0))
3074 ;; in-parens char-after old-indent-point is-brace containing-sexp
3076 ;; group is an expression, not a block:
3077 ;; indent to just after the surrounding open parens,
3078 ;; skip blanks if we do not close the expression.
3079 (+ (progn
3080 (goto-char (elt i 2)) ; old-indent-point
3081 (current-column))
3082 (if (and (elt i 3) ; is-brace
3083 (eq (elt i 1) ?\})) ; char-after
3084 ;; Correct indentation of trailing ?\}
3085 (+ cperl-indent-level cperl-close-paren-offset)
3086 0)))
3088 ;; Indenter for continuation lines
3090 ((eq 'continuation (elt i 0))
3091 ;; [continuation statement-start char-after is-block is-brace]
3092 (goto-char (elt i 1)) ; statement-start
3093 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3094 0 ; Closing parenth
3095 cperl-continued-statement-offset)
3096 (if (or (elt i 3) ; is-block
3097 (not (elt i 4)) ; is-brace
3098 (not (eq (elt i 2) ?\}))) ; char-after
3100 ;; Now it is a hash reference
3101 (+ cperl-indent-level cperl-close-paren-offset))
3102 ;; Labels do not take :: ...
3103 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3104 (if (> (current-indentation) cperl-min-label-indent)
3105 (- (current-indentation) cperl-label-offset)
3106 ;; Do not move `parse-data', this should
3107 ;; be quick anyway (this comment comes
3108 ;; from different location):
3109 (cperl-calculate-indent))
3110 (current-column))
3111 (if (eq (elt i 2) ?\{) ; char-after
3112 cperl-continued-brace-offset 0)))
3114 ;; Indenter for lines in a block which are not leading lines
3116 ((eq 'have-prev-sibling (elt i 0))
3117 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3118 (goto-char (elt i 1))
3119 (if (> (elt i 2) (point)) ; colon-line-end; After-label, same line
3120 (if (> (current-indentation)
3121 cperl-min-label-indent)
3122 (- (current-indentation) cperl-label-offset)
3123 ;; Do not believe: `max' was involved in calculation of indent
3124 (+ cperl-indent-level
3125 (save-excursion
3126 (goto-char (elt i 3)) ; block-start
3127 (current-indentation))))
3128 (current-column)))
3130 ;; Indenter for the first line in a block
3132 ((eq 'code-start-in-block (elt i 0))
3133 ;;[code-start-in-block before-brace char-after
3134 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3135 ;; group-starts-before-start-of-sub start-of-control-group]
3136 (goto-char (elt i 1))
3137 ;; For open brace in column zero, don't let statement
3138 ;; start there too. If cperl-indent-level=0,
3139 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3140 (+ (if (and (bolp) (zerop cperl-indent-level))
3141 (+ cperl-brace-offset cperl-continued-statement-offset)
3142 cperl-indent-level)
3143 (if (and (elt i 3) ; is-a-HASH-ref
3144 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3145 (+ cperl-indent-level cperl-close-paren-offset)
3147 ;; Unless openbrace is the first nonwhite thing on the line,
3148 ;; add the cperl-brace-imaginary-offset.
3149 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3150 cperl-brace-imaginary-offset)
3151 (progn
3152 (goto-char (elt i 6)) ; start-of-control-group
3153 (if (elt i 5) ; group-starts-before-start-of-sub
3154 (current-column)
3155 ;; Get initial indentation of the line we are on.
3156 ;; If line starts with label, calculate label indentation
3157 (if (save-excursion
3158 (beginning-of-line)
3159 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3160 (if (> (current-indentation) cperl-min-label-indent)
3161 (- (current-indentation) cperl-label-offset)
3162 ;; Do not move `parse-data', this should
3163 ;; be quick anyway:
3164 (cperl-calculate-indent))
3165 (current-indentation))))))
3167 (error "Unrecognized value of indent: %s" i))))
3169 (error "Got strange value of indent: %s" i))))))
3171 (defvar cperl-indent-alist
3172 '((string nil)
3173 (comment nil)
3174 (toplevel 0)
3175 (toplevel-after-parenth 2)
3176 (toplevel-continued 2)
3177 (expression 1))
3178 "Alist of indentation rules for CPerl mode.
3179 The values mean:
3180 nil: do not indent;
3181 number: add this amount of indentation.
3183 Not finished, not used.")
3185 (defun cperl-where-am-i (&optional parse-start start-state)
3186 ;; Unfinished
3187 "Return a list of lists ((TYPE POS)...) of good points before the point.
3188 POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'.
3190 Not finished, not used."
3191 (save-excursion
3192 (let* ((start-point (point)) unused
3193 (s-s (cperl-get-state))
3194 (start (nth 0 s-s))
3195 (state (nth 1 s-s))
3196 (prestart (nth 3 s-s))
3197 (containing-sexp (car (cdr state)))
3198 (case-fold-search nil)
3199 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
3200 (cond ((nth 3 state) ; In string
3201 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
3202 ((nth 4 state) ; In comment
3203 (setq res (cons '(comment) res)))
3204 ((null containing-sexp)
3205 ;; Line is at top level.
3206 ;; Indent like the previous top level line
3207 ;; unless that ends in a closeparen without semicolon,
3208 ;; in which case this line is the first argument decl.
3209 (cperl-backward-to-noncomment (or parse-start (point-min)))
3210 ;;(skip-chars-backward " \t\f\n")
3211 (cond
3212 ((or (bobp)
3213 (memq (preceding-char) (append ";}" nil)))
3214 (setq res (cons (list 'toplevel start) res)))
3215 ((eq (preceding-char) ?\) )
3216 (setq res (cons (list 'toplevel-after-parenth start) res)))
3218 (setq res (cons (list 'toplevel-continued start) res)))))
3219 ((/= (char-after containing-sexp) ?{)
3220 ;; line is expression, not statement:
3221 ;; indent to just after the surrounding open.
3222 ;; skip blanks if we do not close the expression.
3223 (setq res (cons (list 'expression-blanks
3224 (progn
3225 (goto-char (1+ containing-sexp))
3226 (or (looking-at "[ \t]*\\(#\\|$\\)")
3227 (skip-chars-forward " \t"))
3228 (point)))
3229 (cons (list 'expression containing-sexp) res))))
3230 ((progn
3231 ;; Containing-expr starts with \{. Check whether it is a hash.
3232 (goto-char containing-sexp)
3233 (not (cperl-block-p)))
3234 (setq res (cons (list 'expression-blanks
3235 (progn
3236 (goto-char (1+ containing-sexp))
3237 (or (looking-at "[ \t]*\\(#\\|$\\)")
3238 (skip-chars-forward " \t"))
3239 (point)))
3240 (cons (list 'expression containing-sexp) res))))
3242 ;; Statement level.
3243 (setq res (cons (list 'in-block containing-sexp) res))
3244 ;; Is it a continuation or a new statement?
3245 ;; Find previous non-comment character.
3246 (cperl-backward-to-noncomment containing-sexp)
3247 ;; Back up over label lines, since they don't
3248 ;; affect whether our line is a continuation.
3249 ;; Back up comma-delimited lines too ?????
3250 (while (or (eq (preceding-char) ?\,)
3251 (save-excursion (cperl-after-label)))
3252 (if (eq (preceding-char) ?\,)
3253 ;; Will go to beginning of line, essentially
3254 ;; Will ignore embedded sexpr XXXX.
3255 (cperl-backward-to-start-of-continued-exp containing-sexp))
3256 (beginning-of-line)
3257 (cperl-backward-to-noncomment containing-sexp))
3258 ;; Now we get the answer.
3259 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
3260 ;; This line is continuation of preceding line's statement.
3261 (list (list 'statement-continued containing-sexp))
3262 ;; This line starts a new statement.
3263 ;; Position following last unclosed open.
3264 (goto-char containing-sexp)
3265 ;; Is line first statement after an open-brace?
3267 ;; If no, find that first statement and indent like
3268 ;; it. If the first statement begins with label, do
3269 ;; not believe when the indentation of the label is too
3270 ;; small.
3271 (save-excursion
3272 (forward-char 1)
3273 (let ((colon-line-end 0))
3274 (while (progn (skip-chars-forward " \t\n" start-point)
3275 (and (< (point) start-point)
3276 (looking-at
3277 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
3278 ;; Skip over comments and labels following openbrace.
3279 (cond ((= (following-char) ?\#)
3280 ;;(forward-line 1)
3281 (end-of-line))
3282 ;; label:
3284 (save-excursion (end-of-line)
3285 (setq colon-line-end (point)))
3286 (search-forward ":"))))
3287 ;; Now at the point, after label, or at start
3288 ;; of first statement in the block.
3289 (and (< (point) start-point)
3290 (if (> colon-line-end (point))
3291 ;; Before statement after label
3292 (if (> (current-indentation)
3293 cperl-min-label-indent)
3294 (list (list 'label-in-block (point)))
3295 ;; Do not believe: `max' is involved
3296 (list
3297 (list 'label-in-block-min-indent (point))))
3298 ;; Before statement
3299 (list 'statement-in-block (point))))))
3300 ;; If no previous statement,
3301 ;; indent it relative to line brace is on.
3302 ;; For open brace in column zero, don't let statement
3303 ;; start there too. If cperl-indent-level is zero,
3304 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3305 ;; For open-braces not the first thing in a line,
3306 ;; add in cperl-brace-imaginary-offset.
3308 ;; If first thing on a line: ?????
3309 (setq unused ; This is not finished...
3310 (+ (if (and (bolp) (zerop cperl-indent-level))
3311 (+ cperl-brace-offset cperl-continued-statement-offset)
3312 cperl-indent-level)
3313 ;; Move back over whitespace before the openbrace.
3314 ;; If openbrace is not first nonwhite thing on the line,
3315 ;; add the cperl-brace-imaginary-offset.
3316 (progn (skip-chars-backward " \t")
3317 (if (bolp) 0 cperl-brace-imaginary-offset))
3318 ;; If the openbrace is preceded by a parenthesized exp,
3319 ;; move to the beginning of that;
3320 ;; possibly a different line
3321 (progn
3322 (if (eq (preceding-char) ?\))
3323 (forward-sexp -1))
3324 ;; Get initial indentation of the line we are on.
3325 ;; If line starts with label, calculate label indentation
3326 (if (save-excursion
3327 (beginning-of-line)
3328 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3329 (if (> (current-indentation) cperl-min-label-indent)
3330 (- (current-indentation) cperl-label-offset)
3331 (cperl-calculate-indent))
3332 (current-indentation)))))))))
3333 res)))
3335 (defun cperl-calculate-indent-within-comment ()
3336 "Return the indentation amount for line, assuming that
3337 the current line is to be regarded as part of a block comment."
3338 (let (end star-start)
3339 (save-excursion
3340 (beginning-of-line)
3341 (skip-chars-forward " \t")
3342 (setq end (point))
3343 (and (= (following-char) ?#)
3344 (forward-line -1)
3345 (cperl-to-comment-or-eol)
3346 (setq end (point)))
3347 (goto-char end)
3348 (current-column))))
3351 (defun cperl-to-comment-or-eol ()
3352 "Go to position before comment on the current line, or to end of line.
3353 Returns true if comment is found. In POD will not move the point."
3354 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3355 ;; then looks for literal # or end-of-line.
3356 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3357 (or cperl-font-locking
3358 (cperl-update-syntaxification lim lim))
3359 (beginning-of-line)
3360 (if (setq pr (get-text-property (point) 'syntax-type))
3361 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3362 (if (or (eq pr 'pod)
3363 (if (or (not e) (> e lim)) ; deep inside a group
3364 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3365 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3366 ;; Else - need to do it the hard way
3367 (and (and e (<= e lim))
3368 (goto-char e))
3369 (while (not stop-in)
3370 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3371 ; stop at comment
3372 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3373 (if (nth 4 state) ; After `#';
3374 ; (nth 2 state) can be
3375 ; beginning of m,s,qq and so
3376 ; on
3377 (if (nth 2 state)
3378 (progn
3379 (setq cpoint (point))
3380 (goto-char (nth 2 state))
3381 (cond
3382 ((looking-at "\\(s\\|tr\\)\\>")
3383 (or (re-search-forward
3384 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3385 lim 'move)
3386 (setq stop-in t)))
3387 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3388 (or (re-search-forward
3389 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3390 lim 'move)
3391 (setq stop-in t)))
3392 (t ; It was fair comment
3393 (setq stop-in t) ; Finish
3394 (goto-char (1- cpoint)))))
3395 (setq stop-in t) ; Finish
3396 (forward-char -1))
3397 (setq stop-in t))) ; Finish
3398 (nth 4 state))))
3400 (defsubst cperl-modify-syntax-type (at how)
3401 (if (< at (point-max))
3402 (progn
3403 (put-text-property at (1+ at) 'syntax-table how)
3404 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3406 (defun cperl-protect-defun-start (s e)
3407 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3408 (save-excursion
3409 (goto-char s)
3410 (while (re-search-forward "^\\s(" e 'to-end)
3411 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3413 (defun cperl-commentify (bb e string &optional noface)
3414 (if cperl-use-syntax-table-text-property
3415 (if (eq noface 'n) ; Only immediate
3417 ;; We suppose that e is _after_ the end of construction, as after eol.
3418 (setq string (if string cperl-st-sfence cperl-st-cfence))
3419 (if (> bb (- e 2))
3420 ;; one-char string/comment?!
3421 (cperl-modify-syntax-type bb cperl-st-punct)
3422 (cperl-modify-syntax-type bb string)
3423 (cperl-modify-syntax-type (1- e) string))
3424 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3425 (put-text-property (1+ bb) (1- e)
3426 'syntax-table cperl-string-syntax-table))
3427 (cperl-protect-defun-start bb e))
3428 ;; Fontify
3429 (or noface
3430 (not cperl-pod-here-fontify)
3431 (put-text-property bb e 'face (if string 'font-lock-string-face
3432 'font-lock-comment-face)))))
3434 (defvar cperl-starters '(( ?\( . ?\) )
3435 ( ?\[ . ?\] )
3436 ( ?\{ . ?\} )
3437 ( ?\< . ?\> )))
3439 (defun cperl-cached-syntax-table (st)
3440 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3441 All the entries of the syntax table are \".\", except for a backslash, which
3442 is quoting."
3443 (if (car-safe st)
3444 (car st)
3445 (setcar st (make-syntax-table))
3446 (setq st (car st))
3447 (let ((i 0))
3448 (while (< i 256)
3449 (modify-syntax-entry i "." st)
3450 (setq i (1+ i))))
3451 (modify-syntax-entry ?\\ "\\" st)
3452 st))
3454 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3455 &optional ostart oend)
3456 "Find the end of a regular expression or a stringish construct (q[] etc).
3457 The point should be before the starting delimiter.
3459 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3460 is s/// or tr/// like expression. If END is nil, generates an error
3461 message if needed. If SET-ST is non-nil, will use (or generate) a
3462 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3463 error message in its CAR (unless it already contains some error
3464 message). ARGUMENT should be the name of the construct (used in error
3465 messages). OSTART, OEND may be set in recursive calls when processing
3466 the second argument of 2ARG construct.
3468 Works *before* syntax recognition is done. In IS-2ARG situation may
3469 modify syntax-type text property if the situation is too hard."
3470 (let (b starter ender st i i2 go-forward reset-st set-st)
3471 (skip-chars-forward " \t")
3472 ;; ender means matching-char matcher.
3473 (setq b (point)
3474 starter (if (eobp) 0 (char-after b))
3475 ender (cdr (assoc starter cperl-starters)))
3476 ;; What if starter == ?\\ ????
3477 (setq st (cperl-cached-syntax-table st-l))
3478 (setq set-st t)
3479 ;; Whether we have an intermediate point
3480 (setq i nil)
3481 ;; Prepare the syntax table:
3482 (if (not ender) ; m/blah/, s/x//, s/x/y/
3483 (modify-syntax-entry starter "$" st)
3484 (modify-syntax-entry starter (concat "(" (list ender)) st)
3485 (modify-syntax-entry ender (concat ")" (list starter)) st))
3486 (condition-case bb
3487 (progn
3488 ;; We use `$' syntax class to find matching stuff, but $$
3489 ;; is recognized the same as $, so we need to check this manually.
3490 (if (and (eq starter (char-after (cperl-1+ b)))
3491 (not ender))
3492 ;; $ has TeXish matching rules, so $$ equiv $...
3493 (forward-char 2)
3494 (setq reset-st (syntax-table))
3495 (set-syntax-table st)
3496 (forward-sexp 1)
3497 (if (<= (point) (1+ b))
3498 (error "Unfinished regular expression"))
3499 (set-syntax-table reset-st)
3500 (setq reset-st nil)
3501 ;; Now the problem is with m;blah;;
3502 (and (not ender)
3503 (eq (preceding-char)
3504 (char-after (- (point) 2)))
3505 (save-excursion
3506 (forward-char -2)
3507 (= 0 (% (skip-chars-backward "\\\\") 2)))
3508 (forward-char -1)))
3509 ;; Now we are after the first part.
3510 (and is-2arg ; Have trailing part
3511 (not ender)
3512 (eq (following-char) starter) ; Empty trailing part
3513 (progn
3514 (or (eq (char-syntax (following-char)) ?.)
3515 ;; Make trailing letter into punctuation
3516 (cperl-modify-syntax-type (point) cperl-st-punct))
3517 (setq is-2arg nil go-forward t))) ; Ignore the tail
3518 (if is-2arg ; Not number => have second part
3519 (progn
3520 (setq i (point) i2 i)
3521 (if ender
3522 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3523 (progn
3524 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3525 (goto-char (match-end 0))
3526 (skip-chars-forward " \t\n\f"))
3527 (setq i2 (point))))
3528 (forward-char -1))
3529 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3530 (if ender (modify-syntax-entry ender "." st))
3531 (setq set-st nil)
3532 (setq ender (cperl-forward-re lim end nil st-l err-l
3533 argument starter ender)
3534 ender (nth 2 ender)))))
3535 (error (goto-char lim)
3536 (setq set-st nil)
3537 (if reset-st
3538 (set-syntax-table reset-st))
3539 (or end
3540 (message
3541 "End of `%s%s%c ... %c' string/RE not found: %s"
3542 argument
3543 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3544 starter (or ender starter) bb)
3545 (or (car err-l) (setcar err-l b)))))
3546 (if set-st
3547 (progn
3548 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3549 (if ender (modify-syntax-entry ender "." st))))
3550 ;; i: have 2 args, after end of the first arg
3551 ;; i2: start of the second arg, if any (before delim iff `ender').
3552 ;; ender: the last arg bounded by parens-like chars, the second one of them
3553 ;; starter: the starting delimiter of the first arg
3554 ;; go-forward: has 2 args, and the second part is empty
3555 (list i i2 ender starter go-forward)))
3557 (defun cperl-forward-group-in-re (&optional st-l)
3558 "Find the end of a group in a REx.
3559 Return the error message (if any). Does not work if delimiter is `)'.
3560 Works before syntax recognition is done."
3561 ;; Works *before* syntax recognition is done
3562 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3563 (let (st b reset-st)
3564 (condition-case b
3565 (progn
3566 (setq st (cperl-cached-syntax-table st-l))
3567 (modify-syntax-entry ?\( "()" st)
3568 (modify-syntax-entry ?\) ")(" st)
3569 (setq reset-st (syntax-table))
3570 (set-syntax-table st)
3571 (forward-sexp 1))
3572 (error (message
3573 "cperl-forward-group-in-re: error %s" b)))
3574 ;; now restore the initial state
3575 (if st
3576 (progn
3577 (modify-syntax-entry ?\( "." st)
3578 (modify-syntax-entry ?\) "." st)))
3579 (if reset-st
3580 (set-syntax-table reset-st))
3584 (defvar font-lock-string-face)
3585 ;;(defvar font-lock-reference-face)
3586 (defvar font-lock-constant-face)
3587 (defsubst cperl-postpone-fontification (b e type val &optional now)
3588 ;; Do after syntactic fontification?
3589 (if cperl-syntaxify-by-font-lock
3590 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3591 (put-text-property b e type val)))
3593 ;;; Here is how the global structures (those which cannot be
3594 ;;; recognized locally) are marked:
3595 ;; a) PODs:
3596 ;; Start-to-end is marked `in-pod' ==> t
3597 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3598 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3599 ;; b) HEREs:
3600 ;; Start-to-end is marked `here-doc-group' ==> t
3601 ;; The body is marked `syntax-type' ==> `here-doc'
3602 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3603 ;; c) FORMATs:
3604 ;; First line (to =) marked `first-format-line' ==> t
3605 ;; After-this--to-end is marked `syntax-type' ==> `format'
3606 ;; d) 'Q'uoted string:
3607 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3608 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3609 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3610 ;; e) Attributes of subroutines: `attrib-group' ==> t
3611 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3612 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3614 ;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3615 ;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3617 (defun cperl-unwind-to-safe (before &optional end)
3618 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3619 (let ((pos (point)) opos)
3620 (while (and pos (progn
3621 (beginning-of-line)
3622 (get-text-property (setq pos (point)) 'syntax-type)))
3623 (setq opos pos
3624 pos (cperl-beginning-of-property pos 'syntax-type))
3625 (if (eq pos (point-min))
3626 (setq pos nil))
3627 (if pos
3628 (if before
3629 (progn
3630 (goto-char (cperl-1- pos))
3631 (beginning-of-line)
3632 (setq pos (point)))
3633 (goto-char (setq pos (cperl-1- pos))))
3634 ;; Up to the start
3635 (goto-char (point-min))))
3636 ;; Skip empty lines
3637 (and (looking-at "\n*=")
3638 (/= 0 (skip-chars-backward "\n"))
3639 (forward-char))
3640 (setq pos (point))
3641 (if end
3642 ;; Do the same for end, going small steps
3643 (save-excursion
3644 (while (and end (get-text-property end 'syntax-type))
3645 (setq pos end
3646 end (next-single-property-change end 'syntax-type nil (point-max)))
3647 (if end (progn (goto-char end)
3648 (or (bolp) (forward-line 1))
3649 (setq end (point)))))
3650 (or end pos)))))
3652 ;;; These are needed for byte-compile (at least with v19)
3653 (defvar cperl-nonoverridable-face)
3654 (defvar font-lock-variable-name-face)
3655 (defvar font-lock-function-name-face)
3656 (defvar font-lock-keyword-face)
3657 (defvar font-lock-builtin-face)
3658 (defvar font-lock-type-face)
3659 (defvar font-lock-comment-face)
3660 (defvar font-lock-warning-face)
3662 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3663 "Syntaxically mark (and fontify) attributes of a subroutine.
3664 Should be called with the point before leading colon of an attribute."
3665 ;; Works *before* syntax recognition is done
3666 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3667 (let (st b p reset-st after-first (start (point)) start1 end1)
3668 (condition-case b
3669 (while (looking-at
3670 (concat
3671 "\\(" ; 1=optional? colon
3672 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3673 "\\)"
3674 (if after-first "?" "")
3675 ;; No space between name and paren allowed...
3676 "\\(\\sw+\\)" ; 3=name
3677 "\\((\\)?")) ; 4=optional paren
3678 (and (match-beginning 1)
3679 (cperl-postpone-fontification
3680 (match-beginning 0) (cperl-1+ (match-beginning 0))
3681 'face font-lock-constant-face))
3682 (setq start1 (match-beginning 3) end1 (match-end 3))
3683 (cperl-postpone-fontification start1 end1
3684 'face font-lock-constant-face)
3685 (goto-char end1) ; end or before `('
3686 (if (match-end 4) ; Have attribute arguments...
3687 (progn
3688 (if st nil
3689 (setq st (cperl-cached-syntax-table st-l))
3690 (modify-syntax-entry ?\( "()" st)
3691 (modify-syntax-entry ?\) ")(" st))
3692 (setq reset-st (syntax-table) p (point))
3693 (set-syntax-table st)
3694 (forward-sexp 1)
3695 (set-syntax-table reset-st)
3696 (setq reset-st nil)
3697 (cperl-commentify p (point) t))) ; mark as string
3698 (forward-comment (buffer-size))
3699 (setq after-first t))
3700 (error (message
3701 "L%d: attribute `%s': %s"
3702 (count-lines (point-min) (point))
3703 (and start1 end1 (buffer-substring start1 end1)) b)
3704 (setq start nil)))
3705 (and start
3706 (progn
3707 (put-text-property start (point)
3708 'attrib-group (if (looking-at "{") t 0))
3709 (and pos
3710 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3711 ;; Apparently, we do not need `multiline': faces added now
3712 (put-text-property (+ 3 pos) (cperl-1+ (point))
3713 'syntax-type 'sub-decl))
3714 (and b-fname ; Fontify here: the following condition
3715 (cperl-postpone-fontification ; is too hard to determine by
3716 b-fname e-fname 'face ; a REx, so do it here
3717 (if (looking-at "{")
3718 font-lock-function-name-face
3719 font-lock-variable-name-face)))))
3720 ;; now restore the initial state
3721 (if st
3722 (progn
3723 (modify-syntax-entry ?\( "." st)
3724 (modify-syntax-entry ?\) "." st)))
3725 (if reset-st
3726 (set-syntax-table reset-st))))
3728 (defsubst cperl-look-at-leading-count (is-x-REx e)
3729 (if (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3730 (1- e) t) ; return nil on failure, no moving
3731 (if (eq ?\{ (preceding-char)) nil
3732 (cperl-postpone-fontification
3733 (1- (point)) (point)
3734 'face font-lock-warning-face))))
3736 ;;; Debugging this may require (setq max-specpdl-size 2000)...
3737 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3738 "Scans the buffer for hard-to-parse Perl constructions.
3739 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3740 the sections using `cperl-pod-head-face', `cperl-pod-face',
3741 `cperl-here-face'."
3742 (interactive)
3743 (or min (setq min (point-min)
3744 cperl-syntax-state nil
3745 cperl-syntax-done-to min))
3746 (or max (setq max (point-max)))
3747 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3748 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3749 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3750 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3751 (modified (buffer-modified-p)) overshoot is-o-REx
3752 (after-change-functions nil)
3753 (cperl-font-locking t)
3754 (use-syntax-state (and cperl-syntax-state
3755 (>= min (car cperl-syntax-state))))
3756 (state-point (if use-syntax-state
3757 (car cperl-syntax-state)
3758 (point-min)))
3759 (state (if use-syntax-state
3760 (cdr cperl-syntax-state)))
3761 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3762 (st-l (list nil)) (err-l (list nil))
3763 ;; Somehow font-lock may be not loaded yet...
3764 ;; (e.g., when building TAGS via command-line call)
3765 (font-lock-string-face (if (boundp 'font-lock-string-face)
3766 font-lock-string-face
3767 'font-lock-string-face))
3768 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3769 font-lock-constant-face
3770 'font-lock-constant-face))
3771 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3772 (if (boundp 'font-lock-function-name-face)
3773 font-lock-function-name-face
3774 'font-lock-function-name-face))
3775 (font-lock-variable-name-face ; interpolated vars and ({})-code
3776 (if (boundp 'font-lock-variable-name-face)
3777 font-lock-variable-name-face
3778 'font-lock-variable-name-face))
3779 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3780 (if (boundp 'font-lock-function-name-face)
3781 font-lock-function-name-face
3782 'font-lock-function-name-face))
3783 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3784 (if (boundp 'font-lock-constant-face)
3785 font-lock-constant-face
3786 'font-lock-constant-face))
3787 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3788 (if (boundp 'font-lock-builtin-face)
3789 font-lock-builtin-face
3790 'font-lock-builtin-face))
3791 (font-lock-comment-face
3792 (if (boundp 'font-lock-comment-face)
3793 font-lock-comment-face
3794 'font-lock-comment-face))
3795 (font-lock-warning-face
3796 (if (boundp 'font-lock-warning-face)
3797 font-lock-warning-face
3798 'font-lock-warning-face))
3799 (my-cperl-REx-ctl-face ; (|)
3800 (if (boundp 'font-lock-keyword-face)
3801 font-lock-keyword-face
3802 'font-lock-keyword-face))
3803 (my-cperl-REx-modifiers-face ; //gims
3804 (if (boundp 'cperl-nonoverridable-face)
3805 cperl-nonoverridable-face
3806 'cperl-nonoverridable-face))
3807 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3808 (if (boundp 'font-lock-type-face)
3809 font-lock-type-face
3810 'font-lock-type-face))
3811 (stop-point (if ignore-max
3812 (point-max)
3813 max))
3814 (search
3815 (concat
3816 "\\(\\`\n?\\|^\n\\)=" ; POD
3817 "\\|"
3818 ;; One extra () before this:
3819 "<<" ; HERE-DOC
3820 "\\(" ; 1 + 1
3821 ;; First variant "BLAH" or just ``.
3822 "[ \t]*" ; Yes, whitespace is allowed!
3823 "\\([\"'`]\\)" ; 2 + 1 = 3
3824 "\\([^\"'`\n]*\\)" ; 3 + 1
3825 "\\3"
3826 "\\|"
3827 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3828 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3829 ;; Do not have <<= or << 30 or <<30 or << $blah.
3830 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3831 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3832 "\\)"
3833 "\\|"
3834 ;; 1+6 extra () before this:
3835 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3836 (if cperl-use-syntax-table-text-property
3837 (concat
3838 "\\|"
3839 ;; 1+6+2=9 extra () before this:
3840 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3841 "\\|"
3842 ;; 1+6+2+1=10 extra () before this:
3843 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3844 "\\|"
3845 ;; 1+6+2+1+1=11 extra () before this
3846 "\\<sub\\>" ; sub with proto/attr
3847 "\\("
3848 cperl-white-and-comment-rex
3849 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3850 "\\("
3851 cperl-maybe-white-and-comment-rex
3852 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3853 "\\|"
3854 ;; 1+6+2+1+1+6=17 extra () before this:
3855 "\\$\\(['{]\\)" ; $' or ${foo}
3856 "\\|"
3857 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3858 ;; we do not support intervening comments...):
3859 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3860 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3861 "\\|"
3862 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3863 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3864 "\\|"
3865 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3866 ""))))
3867 (unwind-protect
3868 (progn
3869 (save-excursion
3870 (or non-inter
3871 (message "Scanning for \"hard\" Perl constructions..."))
3872 ;;(message "find: %s --> %s" min max)
3873 (and cperl-pod-here-fontify
3874 ;; We had evals here, do not know why...
3875 (setq face cperl-pod-face
3876 head-face cperl-pod-head-face
3877 here-face cperl-here-face))
3878 (remove-text-properties min max
3879 '(syntax-type t in-pod t syntax-table t
3880 attrib-group t
3881 REx-interpolated t
3882 cperl-postpone t
3883 syntax-subtype t
3884 rear-nonsticky t
3885 front-sticky t
3886 here-doc-group t
3887 first-format-line t
3888 REx-part2 t
3889 indentable t))
3890 ;; Need to remove face as well...
3891 (goto-char min)
3892 (and (eq system-type 'emx)
3893 (eq (point) 1)
3894 (let ((case-fold-search t))
3895 (looking-at "extproc[ \t]")) ; Analogue of #!
3896 (cperl-commentify min
3897 (save-excursion (end-of-line) (point))
3898 nil))
3899 (while (and
3900 (< (point) max)
3901 (re-search-forward search max t))
3902 (setq tmpend nil) ; Valid for most cases
3903 (setq b (match-beginning 0)
3904 state (save-excursion (parse-partial-sexp
3905 state-point b nil nil state))
3906 state-point b)
3907 (cond
3908 ;; 1+6+2+1+1+6=17 extra () before this:
3909 ;; "\\$\\(['{]\\)"
3910 ((match-beginning 18) ; $' or ${foo}
3911 (if (eq (preceding-char) ?\') ; $'
3912 (progn
3913 (setq b (1- (point))
3914 state (parse-partial-sexp
3915 state-point (1- b) nil nil state)
3916 state-point (1- b))
3917 (if (nth 3 state) ; in string
3918 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3919 (goto-char (1+ b)))
3920 ;; else: ${
3921 (setq bb (match-beginning 0))
3922 (cperl-modify-syntax-type bb cperl-st-punct)))
3923 ;; No processing in strings/comments beyond this point:
3924 ((or (nth 3 state) (nth 4 state))
3925 t) ; Do nothing in comment/string
3926 ((match-beginning 1) ; POD section
3927 ;; "\\(\\`\n?\\|^\n\\)="
3928 (setq b (match-beginning 0)
3929 state (parse-partial-sexp
3930 state-point b nil nil state)
3931 state-point b)
3932 (if (or (nth 3 state) (nth 4 state)
3933 (looking-at "cut\\>"))
3934 (if (or (nth 3 state) (nth 4 state) ignore-max)
3935 nil ; Doing a chunk only
3936 (message "=cut is not preceded by a POD section")
3937 (or (car err-l) (setcar err-l (point))))
3938 (beginning-of-line)
3940 (setq b (point)
3941 bb b
3942 tb (match-beginning 0)
3943 b1 nil) ; error condition
3944 ;; We do not search to max, since we may be called from
3945 ;; some hook of fontification, and max is random
3946 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3947 (progn
3948 (goto-char b)
3949 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3950 (progn
3951 (message "=cut is not preceded by an empty line")
3952 (setq b1 t)
3953 (or (car err-l) (setcar err-l b))))))
3954 (beginning-of-line 2) ; An empty line after =cut is not POD!
3955 (setq e (point))
3956 (and (> e max)
3957 (progn
3958 (remove-text-properties
3959 max e '(syntax-type t in-pod t syntax-table t
3960 attrib-group t
3961 REx-interpolated t
3962 cperl-postpone t
3963 syntax-subtype t
3964 here-doc-group t
3965 rear-nonsticky t
3966 front-sticky t
3967 first-format-line t
3968 REx-part2 t
3969 indentable t))
3970 (setq tmpend tb)))
3971 (put-text-property b e 'in-pod t)
3972 (put-text-property b e 'syntax-type 'in-pod)
3973 (goto-char b)
3974 (while (re-search-forward "\n\n[ \t]" e t)
3975 ;; We start 'pod 1 char earlier to include the preceding line
3976 (beginning-of-line)
3977 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3978 (cperl-put-do-not-fontify b (point) t)
3979 ;; mark the non-literal parts as PODs
3980 (if cperl-pod-here-fontify
3981 (cperl-postpone-fontification b (point) 'face face t))
3982 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3983 (beginning-of-line)
3984 (setq b (point)))
3985 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3986 (cperl-put-do-not-fontify (point) e t)
3987 (if cperl-pod-here-fontify
3988 (progn
3989 ;; mark the non-literal parts as PODs
3990 (cperl-postpone-fontification (point) e 'face face t)
3991 (goto-char bb)
3992 (if (looking-at
3993 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3994 ;; mark the headers
3995 (cperl-postpone-fontification
3996 (match-beginning 1) (match-end 1)
3997 'face head-face))
3998 (while (re-search-forward
3999 ;; One paragraph
4000 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
4001 e 'toend)
4002 ;; mark the headers
4003 (cperl-postpone-fontification
4004 (match-beginning 1) (match-end 1)
4005 'face head-face))))
4006 (cperl-commentify bb e nil)
4007 (goto-char e)
4008 (or (eq e (point-max))
4009 (forward-char -1)))) ; Prepare for immediate POD start.
4010 ;; Here document
4011 ;; We can do many here-per-line;
4012 ;; but multiline quote on the same line as <<HERE confuses us...
4013 ;; ;; One extra () before this:
4014 ;;"<<"
4015 ;; "\\(" ; 1 + 1
4016 ;; ;; First variant "BLAH" or just ``.
4017 ;; "[ \t]*" ; Yes, whitespace is allowed!
4018 ;; "\\([\"'`]\\)" ; 2 + 1
4019 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
4020 ;; "\\3"
4021 ;; "\\|"
4022 ;; ;; Second variant: Identifier or \ID or empty
4023 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
4024 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
4025 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
4026 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
4027 ;; "\\)"
4028 ((match-beginning 2) ; 1 + 1
4029 (setq b (point)
4030 tb (match-beginning 0)
4031 c (and ; not HERE-DOC
4032 (match-beginning 5)
4033 (save-match-data
4034 (or (looking-at "[ \t]*(") ; << function_call()
4035 (save-excursion ; 1 << func_name, or $foo << 10
4036 (condition-case nil
4037 (progn
4038 (goto-char tb)
4039 ;;; XXX What to do: foo <<bar ???
4040 ;;; XXX Need to support print {a} <<B ???
4041 (forward-sexp -1)
4042 (save-match-data
4043 ; $foo << b; $f .= <<B;
4044 ; ($f+1) << b; a($f) . <<B;
4045 ; foo 1, <<B; $x{a} <<b;
4046 (cond
4047 ((looking-at "[0-9$({]")
4048 (forward-sexp 1)
4049 (and
4050 (looking-at "[ \t]*<<")
4051 (condition-case nil
4052 ;; print $foo <<EOF
4053 (progn
4054 (forward-sexp -2)
4055 (not
4056 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
4057 (error t)))))))
4058 (error nil))) ; func(<<EOF)
4059 (and (not (match-beginning 6)) ; Empty
4060 (looking-at
4061 "[ \t]*[=0-9$@%&(]"))))))
4062 (if c ; Not here-doc
4063 nil ; Skip it.
4064 (setq c (match-end 2)) ; 1 + 1
4065 (if (match-beginning 5) ;4 + 1
4066 (setq b1 (match-beginning 5) ; 4 + 1
4067 e1 (match-end 5)) ; 4 + 1
4068 (setq b1 (match-beginning 4) ; 3 + 1
4069 e1 (match-end 4))) ; 3 + 1
4070 (setq tag (buffer-substring b1 e1)
4071 qtag (regexp-quote tag))
4072 (cond (cperl-pod-here-fontify
4073 ;; Highlight the starting delimiter
4074 (cperl-postpone-fontification
4075 b1 e1 'face my-cperl-delimiters-face)
4076 (cperl-put-do-not-fontify b1 e1 t)))
4077 (forward-line)
4078 (setq i (point))
4079 (if end-of-here-doc
4080 (goto-char end-of-here-doc))
4081 (setq b (point))
4082 ;; We do not search to max, since we may be called from
4083 ;; some hook of fontification, and max is random
4084 (or (and (re-search-forward (concat "^" qtag "$")
4085 stop-point 'toend)
4086 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4088 (progn ; Pretend we matched at the end
4089 (goto-char (point-max))
4090 (re-search-forward "\\'")
4091 (message "End of here-document `%s' not found." tag)
4092 (or (car err-l) (setcar err-l b))))
4093 (if cperl-pod-here-fontify
4094 (progn
4095 ;; Highlight the ending delimiter
4096 (cperl-postpone-fontification
4097 (match-beginning 0) (match-end 0)
4098 'face my-cperl-delimiters-face)
4099 (cperl-put-do-not-fontify b (match-end 0) t)
4100 ;; Highlight the HERE-DOC
4101 (cperl-postpone-fontification b (match-beginning 0)
4102 'face here-face)))
4103 (setq e1 (cperl-1+ (match-end 0)))
4104 (put-text-property b (match-beginning 0)
4105 'syntax-type 'here-doc)
4106 (put-text-property (match-beginning 0) e1
4107 'syntax-type 'here-doc-delim)
4108 (put-text-property b e1 'here-doc-group t)
4109 ;; This makes insertion at the start of HERE-DOC update
4110 ;; the whole construct:
4111 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
4112 (cperl-commentify b e1 nil)
4113 (cperl-put-do-not-fontify b (match-end 0) t)
4114 ;; Cache the syntax info...
4115 (setq cperl-syntax-state (cons state-point state))
4116 ;; ... and process the rest of the line...
4117 (setq overshoot
4118 (elt ; non-inter ignore-max
4119 (cperl-find-pods-heres c i t end t e1) 1))
4120 (if (and overshoot (> overshoot (point)))
4121 (goto-char overshoot)
4122 (setq overshoot e1))
4123 (if (> e1 max)
4124 (setq tmpend tb))))
4125 ;; format
4126 ((match-beginning 8)
4127 ;; 1+6=7 extra () before this:
4128 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4129 (setq b (point)
4130 name (if (match-beginning 8) ; 7 + 1
4131 (buffer-substring (match-beginning 8) ; 7 + 1
4132 (match-end 8)) ; 7 + 1
4134 tb (match-beginning 0))
4135 (setq argument nil)
4136 (put-text-property (save-excursion
4137 (beginning-of-line)
4138 (point))
4139 b 'first-format-line 't)
4140 (if cperl-pod-here-fontify
4141 (while (and (eq (forward-line) 0)
4142 (not (looking-at "^[.;]$")))
4143 (cond
4144 ((looking-at "^#")) ; Skip comments
4145 ((and argument ; Skip argument multi-lines
4146 (looking-at "^[ \t]*{"))
4147 (forward-sexp 1)
4148 (setq argument nil))
4149 (argument ; Skip argument lines
4150 (setq argument nil))
4151 (t ; Format line
4152 (setq b1 (point))
4153 (setq argument (looking-at "^[^\n]*[@^]"))
4154 (end-of-line)
4155 ;; Highlight the format line
4156 (cperl-postpone-fontification b1 (point)
4157 'face font-lock-string-face)
4158 (cperl-commentify b1 (point) nil)
4159 (cperl-put-do-not-fontify b1 (point) t))))
4160 ;; We do not search to max, since we may be called from
4161 ;; some hook of fontification, and max is random
4162 (re-search-forward "^[.;]$" stop-point 'toend))
4163 (beginning-of-line)
4164 (if (looking-at "^\\.$") ; ";" is not supported yet
4165 (progn
4166 ;; Highlight the ending delimiter
4167 (cperl-postpone-fontification (point) (+ (point) 2)
4168 'face font-lock-string-face)
4169 (cperl-commentify (point) (+ (point) 2) nil)
4170 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4171 (message "End of format `%s' not found." name)
4172 (or (car err-l) (setcar err-l b)))
4173 (forward-line)
4174 (if (> (point) max)
4175 (setq tmpend tb))
4176 (put-text-property b (point) 'syntax-type 'format))
4177 ;; qq-like String or Regexp:
4178 ((or (match-beginning 10) (match-beginning 11))
4179 ;; 1+6+2=9 extra () before this:
4180 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4181 ;; "\\|"
4182 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4183 (setq b1 (if (match-beginning 10) 10 11)
4184 argument (buffer-substring
4185 (match-beginning b1) (match-end b1))
4186 b (point) ; end of qq etc
4188 c (char-after (match-beginning b1))
4189 bb (char-after (1- (match-beginning b1))) ; tmp holder
4190 ;; bb == "Not a stringy"
4191 bb (if (eq b1 10) ; user variables/whatever
4192 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4193 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4194 ((eq bb ?\:) ; $opt::s
4195 (eq (char-after
4196 (- (match-beginning b1) 2))
4197 ?\:))
4198 ((eq bb ?\>) ; $foo->s
4199 (eq (char-after
4200 (- (match-beginning b1) 2))
4201 ?\-))
4202 ((eq bb ?\&)
4203 (not (eq (char-after ; &&m/blah/
4204 (- (match-beginning b1) 2))
4205 ?\&)))
4206 (t t)))
4207 ;; <file> or <$file>
4208 (and (eq c ?\<)
4209 ;; Do not stringify <FH>, <$fh> :
4210 (save-match-data
4211 (looking-at
4212 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4213 tb (match-beginning 0))
4214 (goto-char (match-beginning b1))
4215 (cperl-backward-to-noncomment (point-min))
4216 (or bb
4217 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4218 (setq argument ""
4219 b1 nil
4220 bb ; Not a regexp?
4221 (not
4222 ;; What is below: regexp-p?
4223 (and
4224 (or (memq (preceding-char)
4225 (append (if (memq c '(?\? ?\<))
4226 ;; $a++ ? 1 : 2
4227 "~{(=|&*!,;:["
4228 "~{(=|&+-*!,;:[") nil))
4229 (and (eq (preceding-char) ?\})
4230 (cperl-after-block-p (point-min)))
4231 (and (eq (char-syntax (preceding-char)) ?w)
4232 (progn
4233 (forward-sexp -1)
4234 ;; After these keywords `/' starts a RE. One should add all the
4235 ;; functions/builtins which expect an argument, but ...
4236 (if (eq (preceding-char) ?-)
4237 ;; -d ?foo? is a RE
4238 (looking-at "[a-zA-Z]\\>")
4239 (and
4240 (not (memq (preceding-char)
4241 '(?$ ?@ ?& ?%)))
4242 (looking-at
4243 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4244 (and (eq (preceding-char) ?.)
4245 (eq (char-after (- (point) 2)) ?.))
4246 (bobp))
4247 ;; m|blah| ? foo : bar;
4248 (not
4249 (and (eq c ?\?)
4250 cperl-use-syntax-table-text-property
4251 (not (bobp))
4252 (progn
4253 (forward-char -1)
4254 (looking-at "\\s|"))))))
4255 b (1- b))
4256 ;; s y tr m
4257 ;; Check for $a -> y
4258 (setq b1 (preceding-char)
4259 go (point))
4260 (if (and (eq b1 ?>)
4261 (eq (char-after (- go 2)) ?-))
4262 ;; Not a regexp
4263 (setq bb t))))
4264 (or bb
4265 (progn
4266 (goto-char b)
4267 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4268 (goto-char (match-end 0))
4269 (skip-chars-forward " \t\n\f"))
4270 (cond ((and (eq (following-char) ?\})
4271 (eq b1 ?\{))
4272 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4273 (goto-char (1- go))
4274 (skip-chars-backward " \t\n\f")
4275 (if (memq (preceding-char) (append "$@%&*" nil))
4276 (setq bb t) ; @{y}
4277 (condition-case nil
4278 (forward-sexp -1)
4279 (error nil)))
4280 (if (or bb
4281 (looking-at ; $foo -> {s}
4282 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4283 (and ; $foo[12] -> {s}
4284 (memq (following-char) '(?\{ ?\[))
4285 (progn
4286 (forward-sexp 1)
4287 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4288 (setq bb t)
4289 (goto-char b)))
4290 ((and (eq (following-char) ?=)
4291 (eq (char-after (1+ (point))) ?\>))
4292 ;; Check for { foo => 1, s => 2 }
4293 ;; Apparently s=> is never a substitution...
4294 (setq bb t))
4295 ((and (eq (following-char) ?:)
4296 (eq b1 ?\{) ; Check for $ { s::bar }
4297 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4298 (progn
4299 (goto-char (1- go))
4300 (skip-chars-backward " \t\n\f")
4301 (memq (preceding-char)
4302 (append "$@%&*" nil))))
4303 (setq bb t))
4304 ((eobp)
4305 (setq bb t)))))
4306 (if bb
4307 (goto-char i)
4308 ;; Skip whitespace and comments...
4309 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4310 (goto-char (match-end 0))
4311 (skip-chars-forward " \t\n\f"))
4312 (if (> (point) b)
4313 (put-text-property b (point) 'syntax-type 'prestring))
4314 ;; qtag means two-arg matcher, may be reset to
4315 ;; 2 or 3 later if some special quoting is needed.
4316 ;; e1 means matching-char matcher.
4317 (setq b (point) ; before the first delimiter
4318 ;; has 2 args
4319 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4320 ;; We do not search to max, since we may be called from
4321 ;; some hook of fontification, and max is random
4322 i (cperl-forward-re stop-point end
4324 st-l err-l argument)
4325 ;; If `go', then it is considered as 1-arg, `b1' is nil
4326 ;; as in s/foo//x; the point is before final "slash"
4327 b1 (nth 1 i) ; start of the second part
4328 tag (nth 2 i) ; ender-char, true if second part
4329 ; is with matching chars []
4330 go (nth 4 i) ; There is a 1-char part after the end
4331 i (car i) ; intermediate point
4332 e1 (point) ; end
4333 ;; Before end of the second part if non-matching: ///
4334 tail (if (and i (not tag))
4335 (1- e1))
4336 e (if i i e1) ; end of the first part
4337 qtag nil ; need to preserve backslashitis
4338 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4339 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4340 ;; Commenting \\ is dangerous, what about ( ?
4341 (and i tail
4342 (eq (char-after i) ?\\)
4343 (setq qtag t))
4344 (and (if go (looking-at ".\\sw*x")
4345 (looking-at "\\sw*x")) ; qr//x
4346 (setq is-x-REx t))
4347 (and (if go (looking-at ".\\sw*o")
4348 (looking-at "\\sw*o")) ; //o
4349 (setq is-o-REx t))
4350 (if (null i)
4351 ;; Considered as 1arg form
4352 (progn
4353 (cperl-commentify b (point) t)
4354 (put-text-property b (point) 'syntax-type 'string)
4355 (if (or is-x-REx
4356 ;; ignore other text properties:
4357 (string-match "^qw$" argument))
4358 (put-text-property b (point) 'indentable t))
4359 (and go
4360 (setq e1 (cperl-1+ e1))
4361 (or (eobp)
4362 (forward-char 1))))
4363 (cperl-commentify b i t)
4364 (if (looking-at "\\sw*e") ; s///e
4365 (progn
4366 ;; Cache the syntax info...
4367 (setq cperl-syntax-state (cons state-point state))
4368 (and
4369 ;; silent:
4370 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4371 ;; Error
4372 (goto-char (1+ max)))
4373 (if (and tag (eq (preceding-char) ?\>))
4374 (progn
4375 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4376 (cperl-modify-syntax-type i cperl-st-bra)))
4377 (put-text-property b i 'syntax-type 'string)
4378 (put-text-property i (point) 'syntax-type 'multiline)
4379 (if is-x-REx
4380 (put-text-property b i 'indentable t)))
4381 (cperl-commentify b1 (point) t)
4382 (put-text-property b (point) 'syntax-type 'string)
4383 (if is-x-REx
4384 (put-text-property b i 'indentable t))
4385 (if qtag
4386 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4387 (setq tail nil)))
4388 ;; Now: tail: if the second part is non-matching without ///e
4389 (if (eq (char-syntax (following-char)) ?w)
4390 (progn
4391 (forward-word 1) ; skip modifiers s///s
4392 (if tail (cperl-commentify tail (point) t))
4393 (cperl-postpone-fontification
4394 e1 (point) 'face my-cperl-REx-modifiers-face)))
4395 ;; Check whether it is m// which means "previous match"
4396 ;; and highlight differently
4397 (setq is-REx
4398 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4399 (or (not (= (length argument) 0))
4400 (not (eq c ?\<)))))
4401 (if (and is-REx
4402 (eq e (+ 2 b))
4403 ;; split // *is* using zero-pattern
4404 (save-excursion
4405 (condition-case nil
4406 (progn
4407 (goto-char tb)
4408 (forward-sexp -1)
4409 (not (looking-at "split\\>")))
4410 (error t))))
4411 (cperl-postpone-fontification
4412 b e 'face font-lock-warning-face)
4413 (if (or i2 ; Has 2 args
4414 (and cperl-fontify-m-as-s
4416 (string-match "^\\(m\\|qr\\)$" argument)
4417 (and (eq 0 (length argument))
4418 (not (eq ?\< (char-after b)))))))
4419 (progn
4420 (cperl-postpone-fontification
4421 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4422 (cperl-postpone-fontification
4423 (1- e) e 'face my-cperl-delimiters-face)))
4424 (if (and is-REx cperl-regexp-scan)
4425 ;; Process RExen: embedded comments, charclasses and ]
4426 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4427 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4428 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4429 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4430 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4431 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4432 (save-excursion
4433 (goto-char (1+ b))
4434 ;; First
4435 (cperl-look-at-leading-count is-x-REx e)
4436 (setq hairy-RE
4437 (concat
4438 (if is-x-REx
4439 (if (eq (char-after b) ?\#)
4440 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4441 "\\((\\?#\\)\\|\\(#\\)")
4442 ;; keep the same count: add a fake group
4443 (if (eq (char-after b) ?\#)
4444 "\\((\\?\\\\#\\)\\(\\)"
4445 "\\((\\?#\\)\\(\\)"))
4446 "\\|"
4447 "\\(\\[\\)" ; 3=[
4448 "\\|"
4449 "\\(]\\)" ; 4=]
4450 "\\|"
4451 ;; XXXX Will not be able to use it in s)))
4452 (if (eq (char-after b) ?\) )
4453 "\\())))\\)" ; Will never match
4454 (if (eq (char-after b) ?? )
4455 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4456 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4457 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4458 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4459 "\\(" ;; XXXX 1-char variables, exc. |()\s
4460 "[$@]"
4461 "\\("
4462 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4463 "\\|"
4464 "{[^{}]*}" ; only one-level allowed
4465 "\\|"
4466 "[^{(|) \t\r\n\f]"
4467 "\\)"
4468 "\\(" ;;8,9:code part of array/hash elt
4469 "\\(" "->" "\\)?"
4470 "\\[[^][]*\\]"
4471 "\\|"
4472 "{[^{}]*}"
4473 "\\)*"
4474 ;; XXXX: what if u is delim?
4475 "\\|"
4476 "[)^|$.*?+]"
4477 "\\|"
4478 "{[0-9]+}"
4479 "\\|"
4480 "{[0-9]+,[0-9]*}"
4481 "\\|"
4482 "\\\\[luLUEQbBAzZG]"
4483 "\\|"
4484 "(" ; Group opener
4485 "\\(" ; 10 group opener follower
4486 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4487 "\\|"
4488 "\\?[:=!>?{]" ; "?" something
4489 "\\|"
4490 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4491 "\\|"
4492 "\\?([0-9]+)" ; (?(1)foo|bar)
4493 "\\|"
4494 "\\?<[=!]"
4495 ;;;"\\|"
4496 ;;; "\\?"
4497 "\\)?"
4498 "\\)"
4499 "\\|"
4500 "\\\\\\(.\\)" ; 12=\SYMBOL
4502 (while
4503 (and (< (point) (1- e))
4504 (re-search-forward hairy-RE (1- e) 'to-end))
4505 (goto-char (match-beginning 0))
4506 (setq REx-subgr-start (point)
4507 was-subgr (following-char))
4508 (cond
4509 ((match-beginning 6) ; 0-length builtins, groups
4510 (goto-char (match-end 0))
4511 (if (match-beginning 11)
4512 (goto-char (match-beginning 11)))
4513 (if (>= (point) e)
4514 (goto-char (1- e)))
4515 (cperl-postpone-fontification
4516 (match-beginning 0) (point)
4517 'face
4518 (cond
4519 ((eq was-subgr ?\) )
4520 (condition-case nil
4521 (save-excursion
4522 (forward-sexp -1)
4523 (if (> (point) b)
4524 (if (if (eq (char-after b) ?? )
4525 (looking-at "(\\\\\\?")
4526 (eq (char-after (1+ (point))) ?\?))
4527 my-cperl-REx-0length-face
4528 my-cperl-REx-ctl-face)
4529 font-lock-warning-face))
4530 (error font-lock-warning-face)))
4531 ((eq was-subgr ?\| )
4532 my-cperl-REx-ctl-face)
4533 ((eq was-subgr ?\$ )
4534 (if (> (point) (1+ REx-subgr-start))
4535 (progn
4536 (put-text-property
4537 (match-beginning 0) (point)
4538 'REx-interpolated
4539 (if is-o-REx 0
4540 (if (and (eq (match-beginning 0)
4541 (1+ b))
4542 (eq (point)
4543 (1- e))) 1 t)))
4544 font-lock-variable-name-face)
4545 my-cperl-REx-spec-char-face))
4546 ((memq was-subgr (append "^." nil) )
4547 my-cperl-REx-spec-char-face)
4548 ((eq was-subgr ?\( )
4549 (if (not (match-beginning 10))
4550 my-cperl-REx-ctl-face
4551 my-cperl-REx-0length-face))
4552 (t my-cperl-REx-0length-face)))
4553 (if (and (memq was-subgr (append "(|" nil))
4554 (not (string-match "(\\?[-imsx]+)"
4555 (match-string 0))))
4556 (cperl-look-at-leading-count is-x-REx e))
4557 (setq was-subgr nil)) ; We do stuff here
4558 ((match-beginning 12) ; \SYMBOL
4559 (forward-char 2)
4560 (if (>= (point) e)
4561 (goto-char (1- e))
4562 ;; How many chars to not highlight:
4563 ;; 0-len special-alnums in other branch =>
4564 ;; Generic: \non-alnum (1), \alnum (1+face)
4565 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4566 (setq REx-subgr-start (point)
4567 qtag (preceding-char))
4568 (cperl-postpone-fontification
4569 (- (point) 2) (- (point) 1) 'face
4570 (if (memq qtag
4571 (append "ghijkmoqvFHIJKMORTVY" nil))
4572 font-lock-warning-face
4573 my-cperl-REx-0length-face))
4574 (if (and (eq (char-after b) qtag)
4575 (memq qtag (append ".])^$|*?+" nil)))
4576 (progn
4577 (if (and cperl-use-syntax-table-text-property
4578 (eq qtag ?\) ))
4579 (put-text-property
4580 REx-subgr-start (1- (point))
4581 'syntax-table cperl-st-punct))
4582 (cperl-postpone-fontification
4583 (1- (point)) (point) 'face
4584 ; \] can't appear below
4585 (if (memq qtag (append ".]^$" nil))
4586 'my-cperl-REx-spec-char-face
4587 (if (memq qtag (append "*?+" nil))
4588 'my-cperl-REx-0length-face
4589 'my-cperl-REx-ctl-face))))) ; )|
4590 ;; Test for arguments:
4591 (cond
4592 ;; This is not pretty: the 5.8.7 logic:
4593 ;; \0numx -> octal (up to total 3 dig)
4594 ;; \DIGIT -> backref unless \0
4595 ;; \DIGITs -> backref if legal
4596 ;; otherwise up to 3 -> octal
4597 ;; Do not try to distinguish, we guess
4598 ((or (and (memq qtag (append "01234567" nil))
4599 (re-search-forward
4600 "\\=[01234567]?[01234567]?"
4601 (1- e) 'to-end))
4602 (and (memq qtag (append "89" nil))
4603 (re-search-forward
4604 "\\=[0123456789]*" (1- e) 'to-end))
4605 (and (eq qtag ?x)
4606 (re-search-forward
4607 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4608 (1- e) 'to-end))
4609 (and (memq qtag (append "pPN" nil))
4610 (re-search-forward "\\={[^{}]+}\\|."
4611 (1- e) 'to-end))
4612 (eq (char-syntax qtag) ?w))
4613 (cperl-postpone-fontification
4614 (1- REx-subgr-start) (point)
4615 'face my-cperl-REx-length1-face))))
4616 (setq was-subgr nil)) ; We do stuff here
4617 ((match-beginning 3) ; [charclass]
4618 (forward-char 1)
4619 (if (eq (char-after b) ?^ )
4620 (and (eq (following-char) ?\\ )
4621 (eq (char-after (cperl-1+ (point)))
4622 ?^ )
4623 (forward-char 2))
4624 (and (eq (following-char) ?^ )
4625 (forward-char 1)))
4626 (setq argument b ; continue?
4627 tag nil ; list of POSIX classes
4628 qtag (point))
4629 (if (eq (char-after b) ?\] )
4630 (and (eq (following-char) ?\\ )
4631 (eq (char-after (cperl-1+ (point)))
4632 ?\] )
4633 (setq qtag (1+ qtag))
4634 (forward-char 2))
4635 (and (eq (following-char) ?\] )
4636 (forward-char 1)))
4637 ;; Apparently, I can't put \] into a charclass
4638 ;; in m]]: m][\\\]\]] produces [\\]]
4639 ;;; POSIX? [:word:] [:^word:] only inside []
4640 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4641 (while
4642 (and argument
4643 (re-search-forward
4644 (if (eq (char-after b) ?\] )
4645 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4646 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4647 (1- e) 'toend))
4648 ;; Is this ] an end of POSIX class?
4649 (if (save-excursion
4650 (and
4651 (search-backward "[" argument t)
4652 (< REx-subgr-start (point))
4653 (not
4654 (and ; Should work with delim = \
4655 (eq (preceding-char) ?\\ )
4656 (= (% (skip-chars-backward
4657 "\\\\") 2) 0)))
4658 (looking-at
4659 (cond
4660 ((eq (char-after b) ?\] )
4661 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4662 ((eq (char-after b) ?\: )
4663 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4664 ((eq (char-after b) ?^ )
4665 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4666 ((eq (char-syntax (char-after b))
4668 (concat
4669 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4670 (char-to-string (char-after b))
4671 "\\|\\sw\\)+:\]"))
4672 (t "\\\\*\\[:\\^?\\sw*:]")))
4673 (setq argument (point))))
4674 (setq tag (cons (cons argument (point))
4675 tag)
4676 argument (point)) ; continue
4677 (setq argument nil)))
4678 (and argument
4679 (message "Couldn't find end of charclass in a REx, pos=%s"
4680 REx-subgr-start))
4681 (if (and cperl-use-syntax-table-text-property
4682 (> (- (point) 2) REx-subgr-start))
4683 (put-text-property
4684 (1+ REx-subgr-start) (1- (point))
4685 'syntax-table cperl-st-punct))
4686 (cperl-postpone-fontification
4687 REx-subgr-start qtag
4688 'face my-cperl-REx-spec-char-face)
4689 (cperl-postpone-fontification
4690 (1- (point)) (point) 'face
4691 my-cperl-REx-spec-char-face)
4692 (if (eq (char-after b) ?\] )
4693 (cperl-postpone-fontification
4694 (- (point) 2) (1- (point))
4695 'face my-cperl-REx-0length-face))
4696 (while tag
4697 (cperl-postpone-fontification
4698 (car (car tag)) (cdr (car tag))
4699 'face my-cperl-REx-length1-face)
4700 (setq tag (cdr tag)))
4701 (setq was-subgr nil)) ; did facing already
4702 ;; Now rare stuff:
4703 ((and (match-beginning 2) ; #-comment
4704 (/= (match-beginning 2) (match-end 2)))
4705 (beginning-of-line 2)
4706 (if (> (point) e)
4707 (goto-char (1- e))))
4708 ((match-beginning 4) ; character "]"
4709 (setq was-subgr nil) ; We do stuff here
4710 (goto-char (match-end 0))
4711 (if cperl-use-syntax-table-text-property
4712 (put-text-property
4713 (1- (point)) (point)
4714 'syntax-table cperl-st-punct))
4715 (cperl-postpone-fontification
4716 (1- (point)) (point)
4717 'face font-lock-warning-face))
4718 ((match-beginning 5) ; before (?{}) (??{})
4719 (setq tag (match-end 0))
4720 (if (or (setq qtag
4721 (cperl-forward-group-in-re st-l))
4722 (and (>= (point) e)
4723 (setq qtag "no matching `)' found"))
4724 (and (not (eq (char-after (- (point) 2))
4725 ?\} ))
4726 (setq qtag "Can't find })")))
4727 (progn
4728 (goto-char (1- e))
4729 (message qtag))
4730 (cperl-postpone-fontification
4731 (1- tag) (1- (point))
4732 'face font-lock-variable-name-face)
4733 (cperl-postpone-fontification
4734 REx-subgr-start (1- tag)
4735 'face my-cperl-REx-spec-char-face)
4736 (cperl-postpone-fontification
4737 (1- (point)) (point)
4738 'face my-cperl-REx-spec-char-face)
4739 (if cperl-use-syntax-table-text-property
4740 (progn
4741 (put-text-property
4742 (- (point) 2) (1- (point))
4743 'syntax-table cperl-st-cfence)
4744 (put-text-property
4745 (+ REx-subgr-start 2)
4746 (+ REx-subgr-start 3)
4747 'syntax-table cperl-st-cfence))))
4748 (setq was-subgr nil))
4749 (t ; (?#)-comment
4750 ;; Inside "(" and "\" arn't special in any way
4751 ;; Works also if the outside delimiters are ().
4752 (or;;(if (eq (char-after b) ?\) )
4753 ;;(re-search-forward
4754 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4755 ;; (1- e) 'toend)
4756 (search-forward ")" (1- e) 'toend)
4758 (message
4759 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4760 REx-subgr-start))))
4761 (if (>= (point) e)
4762 (goto-char (1- e)))
4763 (cond
4764 (was-subgr
4765 (setq REx-subgr-end (point))
4766 (cperl-commentify
4767 REx-subgr-start REx-subgr-end nil)
4768 (cperl-postpone-fontification
4769 REx-subgr-start REx-subgr-end
4770 'face font-lock-comment-face))))))
4771 (if (and is-REx is-x-REx)
4772 (put-text-property (1+ b) (1- e)
4773 'syntax-subtype 'x-REx)))
4774 (if i2
4775 (progn
4776 (cperl-postpone-fontification
4777 (1- e1) e1 'face my-cperl-delimiters-face)
4778 (if (assoc (char-after b) cperl-starters)
4779 (progn
4780 (cperl-postpone-fontification
4781 b1 (1+ b1) 'face my-cperl-delimiters-face)
4782 (put-text-property b1 (1+ b1)
4783 'REx-part2 t)))))
4784 (if (> (point) max)
4785 (setq tmpend tb))))
4786 ((match-beginning 17) ; sub with prototype or attribute
4787 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4788 ;;"\\<sub\\>\\(" ;12
4789 ;; cperl-white-and-comment-rex ;13
4790 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4791 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4792 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4793 (setq b1 (match-beginning 14) e1 (match-end 14))
4794 (if (memq (char-after (1- b))
4795 '(?\$ ?\@ ?\% ?\& ?\*))
4797 (goto-char b)
4798 (if (eq (char-after (match-beginning 17)) ?\( )
4799 (progn
4800 (cperl-commentify ; Prototypes; mark as string
4801 (match-beginning 17) (match-end 17) t)
4802 (goto-char (match-end 0))
4803 ;; Now look for attributes after prototype:
4804 (forward-comment (buffer-size))
4805 (and (looking-at ":[^:]")
4806 (cperl-find-sub-attrs st-l b1 e1 b)))
4807 ;; treat attributes without prototype
4808 (goto-char (match-beginning 17))
4809 (cperl-find-sub-attrs st-l b1 e1 b))))
4810 ;; 1+6+2+1+1+6+1=18 extra () before this:
4811 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4812 ((match-beginning 19) ; old $abc'efg syntax
4813 (setq bb (match-end 0))
4814 ;;;(if (nth 3 state) nil ; in string
4815 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4816 (goto-char bb))
4817 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4818 ;; "__\\(END\\|DATA\\)__"
4819 ((match-beginning 20) ; __END__, __DATA__
4820 (setq bb (match-end 0))
4821 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4822 (cperl-commentify b bb nil)
4823 (setq end t))
4824 ;; "\\\\\\(['`\"($]\\)"
4825 ((match-beginning 21)
4826 ;; Trailing backslash; make non-quoting outside string/comment
4827 (setq bb (match-end 0))
4828 (goto-char b)
4829 (skip-chars-backward "\\\\")
4830 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4831 (cperl-modify-syntax-type b cperl-st-punct)
4832 (goto-char bb))
4833 (t (error "Error in regexp of the sniffer")))
4834 (if (> (point) stop-point)
4835 (progn
4836 (if end
4837 (message "Garbage after __END__/__DATA__ ignored")
4838 (message "Unbalanced syntax found while scanning")
4839 (or (car err-l) (setcar err-l b)))
4840 (goto-char stop-point))))
4841 (setq cperl-syntax-state (cons state-point state)
4842 ;; Do not mark syntax as done past tmpend???
4843 cperl-syntax-done-to (or tmpend (max (point) max)))
4844 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4846 (if (car err-l) (goto-char (car err-l))
4847 (or non-inter
4848 (message "Scanning for \"hard\" Perl constructions... done"))))
4849 (and (buffer-modified-p)
4850 (not modified)
4851 (set-buffer-modified-p nil))
4852 ;; I do not understand what this is doing here. It breaks font-locking
4853 ;; because it resets the syntax-table from font-lock-syntax-table to
4854 ;; cperl-mode-syntax-table.
4855 ;; (set-syntax-table cperl-mode-syntax-table)
4857 (list (car err-l) overshoot)))
4859 (defun cperl-find-pods-heres-region (min max)
4860 (interactive "r")
4861 (cperl-find-pods-heres min max))
4863 (defun cperl-backward-to-noncomment (lim)
4864 ;; Stops at lim or after non-whitespace that is not in comment
4865 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4866 (let (stop p pr)
4867 (while (and (not stop) (> (point) (or lim (point-min))))
4868 (skip-chars-backward " \t\n\f" lim)
4869 (setq p (point))
4870 (beginning-of-line)
4871 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4872 '(pod here-doc here-doc-delim))
4873 (cperl-unwind-to-safe nil)
4874 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4875 (not (memq pr '(string prestring))))
4876 (progn (cperl-to-comment-or-eol) (bolp))
4877 (progn
4878 (skip-chars-backward " \t")
4879 (if (< p (point)) (goto-char p))
4880 (setq stop t)))))))
4882 ;; Used only in `cperl-calculate-indent'...
4883 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4884 ;; Positions is before ?\{. Checks whether it starts a block.
4885 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4886 (cperl-backward-to-noncomment (point-min))
4887 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4888 ; Label may be mixed up with `$blah :'
4889 (save-excursion (cperl-after-label))
4890 (get-text-property (cperl-1- (point)) 'attrib-group)
4891 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4892 (progn
4893 (backward-sexp)
4894 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4895 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4896 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4897 ;; sub bless::foo {}
4898 (progn
4899 (cperl-backward-to-noncomment (point-min))
4900 (and (eq (preceding-char) ?b)
4901 (progn
4902 (forward-sexp -1)
4903 (looking-at "sub[ \t\n\f#]")))))))))
4905 ;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4906 ;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4907 ;;; may be a part of an in-statement construct, such as
4908 ;;; ${something()}, print {FH} $data.
4909 ;;; Moreover, one takes positive approach (looks for else,grep etc)
4910 ;;; another negative (looks for bless,tr etc)
4911 (defun cperl-after-block-p (lim &optional pre-block)
4912 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4913 Would not look before LIM. Assumes that LIM is a good place to begin a
4914 statement. The kind of block we treat here is one after which a new
4915 statement would start; thus the block in ${func()} does not count."
4916 (save-excursion
4917 (condition-case nil
4918 (progn
4919 (or pre-block (forward-sexp -1))
4920 (cperl-backward-to-noncomment lim)
4921 (or (eq (point) lim)
4922 ;; if () {} // sub f () {} // sub f :a(') {}
4923 (eq (preceding-char) ?\) )
4924 ;; label: {}
4925 (save-excursion (cperl-after-label))
4926 ;; sub :attr {}
4927 (get-text-property (cperl-1- (point)) 'attrib-group)
4928 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4929 (save-excursion
4930 (forward-sexp -1)
4931 ;; else {} but not else::func {}
4932 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4933 (not (looking-at "\\(\\sw\\|_\\)+::")))
4934 ;; sub f {}
4935 (progn
4936 (cperl-backward-to-noncomment lim)
4937 (and (eq (preceding-char) ?b)
4938 (progn
4939 (forward-sexp -1)
4940 (looking-at "sub[ \t\n\f#]"))))))
4941 ;; What preceeds is not word... XXXX Last statement in sub???
4942 (cperl-after-expr-p lim))))
4943 (error nil))))
4945 (defun cperl-after-expr-p (&optional lim chars test)
4946 "Return true if the position is good for start of expression.
4947 TEST is the expression to evaluate at the found position. If absent,
4948 CHARS is a string that contains good characters to have before us (however,
4949 `}' is treated \"smartly\" if it is not in the list)."
4950 (let ((lim (or lim (point-min)))
4951 stop p pr)
4952 (cperl-update-syntaxification (point) (point))
4953 (save-excursion
4954 (while (and (not stop) (> (point) lim))
4955 (skip-chars-backward " \t\n\f" lim)
4956 (setq p (point))
4957 (beginning-of-line)
4958 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4959 ;; '(pod here-doc here-doc-delim))
4960 (if (get-text-property (point) 'here-doc-group)
4961 (progn
4962 (goto-char
4963 (cperl-beginning-of-property (point) 'here-doc-group))
4964 (beginning-of-line 0)))
4965 (if (get-text-property (point) 'in-pod)
4966 (progn
4967 (goto-char
4968 (cperl-beginning-of-property (point) 'in-pod))
4969 (beginning-of-line 0)))
4970 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4971 ;; Else: last iteration, or a label
4972 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4973 (skip-chars-backward " \t")
4974 (if (< p (point)) (goto-char p))
4975 (setq p (point))
4976 (if (and (eq (preceding-char) ?:)
4977 (progn
4978 (forward-char -1)
4979 (skip-chars-backward " \t\n\f" lim)
4980 (memq (char-syntax (preceding-char)) '(?w ?_))))
4981 (forward-sexp -1) ; Possibly label. Skip it
4982 (goto-char p)
4983 (setq stop t))))
4984 (or (bobp) ; ???? Needed
4985 (eq (point) lim)
4986 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4987 (progn
4988 (if test (eval test)
4989 (or (memq (preceding-char) (append (or chars "{;") nil))
4990 (and (eq (preceding-char) ?\})
4991 (cperl-after-block-p lim))
4992 (and (eq (following-char) ?.) ; in format: see comment above
4993 (eq (get-text-property (point) 'syntax-type)
4994 'format)))))))))
4996 (defun cperl-backward-to-start-of-expr (&optional lim)
4997 (condition-case nil
4998 (progn
4999 (while (and (or (not lim)
5000 (> (point) lim))
5001 (not (cperl-after-expr-p lim)))
5002 (forward-sexp -1)
5003 ;; May be after $, @, $# etc of a variable
5004 (skip-chars-backward "$@%#")))
5005 (error nil)))
5007 (defun cperl-at-end-of-expr (&optional lim)
5008 ;; Since the SEXP approach below is very fragile, do some overengineering
5009 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
5010 (condition-case nil
5011 (save-excursion
5012 ;; If nothing interesting after, does as (forward-sexp -1);
5013 ;; otherwise fails, or ends at a start of following sexp.
5014 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
5015 ;; may be stuck after @ or $; just put some stupid workaround now:
5016 (let ((p (point)))
5017 (forward-sexp 1)
5018 (forward-sexp -1)
5019 (while (memq (preceding-char) (append "%&@$*" nil))
5020 (forward-char -1))
5021 (or (< (point) p)
5022 (cperl-after-expr-p lim))))
5023 (error t))))
5025 (defun cperl-forward-to-end-of-expr (&optional lim)
5026 (let ((p (point))))
5027 (condition-case nil
5028 (progn
5029 (while (and (< (point) (or lim (point-max)))
5030 (not (cperl-at-end-of-expr)))
5031 (forward-sexp 1)))
5032 (error nil)))
5034 (defun cperl-backward-to-start-of-continued-exp (lim)
5035 (if (memq (preceding-char) (append ")]}\"'`" nil))
5036 (forward-sexp -1))
5037 (beginning-of-line)
5038 (if (<= (point) lim)
5039 (goto-char (1+ lim)))
5040 (skip-chars-forward " \t"))
5042 (defun cperl-after-block-and-statement-beg (lim)
5043 ;; We assume that we are after ?\}
5044 (and
5045 (cperl-after-block-p lim)
5046 (save-excursion
5047 (forward-sexp -1)
5048 (cperl-backward-to-noncomment (point-min))
5049 (or (bobp)
5050 (eq (point) lim)
5051 (not (= (char-syntax (preceding-char)) ?w))
5052 (progn
5053 (forward-sexp -1)
5054 (not
5055 (looking-at
5056 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
5059 (defun cperl-indent-exp ()
5060 "Simple variant of indentation of continued-sexp.
5062 Will not indent comment if it starts at `comment-indent' or looks like
5063 continuation of the comment on the previous line.
5065 If `cperl-indent-region-fix-constructs', will improve spacing on
5066 conditional/loop constructs."
5067 (interactive)
5068 (save-excursion
5069 (let ((tmp-end (progn (end-of-line) (point))) top done)
5070 (save-excursion
5071 (beginning-of-line)
5072 (while (null done)
5073 (setq top (point))
5074 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5075 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
5076 (setq top (point))) ; Get the outermost parenths in line
5077 (goto-char top)
5078 (while (< (point) tmp-end)
5079 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5080 (or (eolp) (forward-sexp 1)))
5081 (if (> (point) tmp-end) ; Yes, there an unfinished block
5083 (if (eq ?\) (preceding-char))
5084 (progn ;; Plan B: find by REGEXP block followup this line
5085 (setq top (point))
5086 (condition-case nil
5087 (progn
5088 (forward-sexp -2)
5089 (if (eq (following-char) ?$ ) ; for my $var (list)
5090 (progn
5091 (forward-sexp -1)
5092 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5093 (forward-sexp -1))))
5094 (if (looking-at
5095 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5096 "\\|for\\(each\\)?\\>\\(\\("
5097 cperl-maybe-white-and-comment-rex
5098 "\\(my\\|local\\|our\\)\\)?"
5099 cperl-maybe-white-and-comment-rex
5100 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5101 (progn
5102 (goto-char top)
5103 (forward-sexp 1)
5104 (setq top (point)))))
5105 (error (setq done t)))
5106 (goto-char top))
5107 (if (looking-at ; Try Plan C: continuation block
5108 (concat cperl-maybe-white-and-comment-rex
5109 "\\<\\(else\\|elsif\|continue\\)\\>"))
5110 (progn
5111 (goto-char (match-end 0))
5112 (save-excursion
5113 (end-of-line)
5114 (setq tmp-end (point))))
5115 (setq done t))))
5116 (save-excursion
5117 (end-of-line)
5118 (setq tmp-end (point))))
5119 (goto-char tmp-end)
5120 (setq tmp-end (point-marker)))
5121 (if cperl-indent-region-fix-constructs
5122 (cperl-fix-line-spacing tmp-end))
5123 (cperl-indent-region (point) tmp-end))))
5125 (defun cperl-fix-line-spacing (&optional end parse-data)
5126 "Improve whitespace in a conditional/loop construct.
5127 Returns some position at the last line."
5128 (interactive)
5129 (or end
5130 (setq end (point-max)))
5131 (let ((ee (save-excursion (end-of-line) (point)))
5132 (cperl-indent-region-fix-constructs
5133 (or cperl-indent-region-fix-constructs 1))
5134 p pp ml have-brace ret)
5135 (save-excursion
5136 (beginning-of-line)
5137 (setq ret (point))
5138 ;; }? continue
5139 ;; blah; }
5140 (if (not
5141 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5142 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5143 nil ; Do not need to do anything
5144 ;; Looking at:
5145 ;; }
5146 ;; else
5147 (if cperl-merge-trailing-else
5148 (if (looking-at
5149 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5150 (progn
5151 (search-forward "}")
5152 (setq p (point))
5153 (skip-chars-forward " \t\n")
5154 (delete-region p (point))
5155 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5156 (beginning-of-line)))
5157 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5158 (save-excursion
5159 (search-forward "}")
5160 (delete-horizontal-space)
5161 (insert "\n")
5162 (setq ret (point))
5163 (if (cperl-indent-line parse-data)
5164 (progn
5165 (cperl-fix-line-spacing end parse-data)
5166 (setq ret (point)))))))
5167 ;; Looking at:
5168 ;; } else
5169 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5170 (progn
5171 (search-forward "}")
5172 (delete-horizontal-space)
5173 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5174 (beginning-of-line)))
5175 ;; Looking at:
5176 ;; else {
5177 (if (looking-at
5178 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5179 (progn
5180 (forward-word 1)
5181 (delete-horizontal-space)
5182 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5183 (beginning-of-line)))
5184 ;; Looking at:
5185 ;; foreach my $var
5186 (if (looking-at
5187 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5188 (progn
5189 (forward-word 2)
5190 (delete-horizontal-space)
5191 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5192 (beginning-of-line)))
5193 ;; Looking at:
5194 ;; foreach my $var (
5195 (if (looking-at
5196 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5197 (progn
5198 (forward-sexp 3)
5199 (delete-horizontal-space)
5200 (insert
5201 (make-string cperl-indent-region-fix-constructs ?\s))
5202 (beginning-of-line)))
5203 ;; Looking at (with or without "}" at start, ending after "({"):
5204 ;; } foreach my $var () OR {
5205 (if (looking-at
5206 "[ \t]*\\(}[ \t]*\\)?\\<\\(\\els\\(e\\|if\\)\\|continue\\|if\\|unless\\|while\\|for\\(each\\)?\\(\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\$[_a-zA-Z0-9]+\\)?\\|until\\)\\>\\([ \t]*(\\|[ \t\n]*{\\)\\|[ \t]*{")
5207 (progn
5208 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5209 (re-search-forward "[({]")
5210 (forward-char -1)
5211 (setq p (point))
5212 (if (eq (following-char) ?\( )
5213 (progn
5214 (forward-sexp 1)
5215 (setq pp (point))) ; past parenth-group
5216 ;; after `else' or nothing
5217 (if ml ; after `else'
5218 (skip-chars-backward " \t\n")
5219 (beginning-of-line))
5220 (setq pp nil))
5221 ;; Now after the sexp before the brace
5222 ;; Multiline expr should be special
5223 (setq ml (and pp (save-excursion (goto-char p)
5224 (search-forward "\n" pp t))))
5225 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5226 (looking-at "[ \t\n]*{"))
5227 (progn
5228 (cond
5229 ((bolp) ; Were before `{', no if/else/etc
5230 nil)
5231 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5232 (delete-horizontal-space)
5233 (if (if ml
5234 cperl-extra-newline-before-brace-multiline
5235 cperl-extra-newline-before-brace)
5236 (progn
5237 (delete-horizontal-space)
5238 (insert "\n")
5239 (setq ret (point))
5240 (if (cperl-indent-line parse-data)
5241 (progn
5242 (cperl-fix-line-spacing end parse-data)
5243 (setq ret (point)))))
5244 (insert
5245 (make-string cperl-indent-region-fix-constructs ?\s))))
5246 ((and (looking-at "[ \t]*\n")
5247 (not (if ml
5248 cperl-extra-newline-before-brace-multiline
5249 cperl-extra-newline-before-brace)))
5250 (setq pp (point))
5251 (skip-chars-forward " \t\n")
5252 (delete-region pp (point))
5253 (insert
5254 (make-string cperl-indent-region-fix-constructs ?\ )))
5255 ((and (looking-at "[\t ]*{")
5256 (if ml cperl-extra-newline-before-brace-multiline
5257 cperl-extra-newline-before-brace))
5258 (delete-horizontal-space)
5259 (insert "\n")
5260 (setq ret (point))
5261 (if (cperl-indent-line parse-data)
5262 (progn
5263 (cperl-fix-line-spacing end parse-data)
5264 (setq ret (point))))))
5265 ;; Now we are before `{'
5266 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5267 (progn
5268 (skip-chars-forward " \t\n")
5269 (setq pp (point))
5270 (forward-sexp 1)
5271 (setq p (point))
5272 (goto-char pp)
5273 (setq ml (search-forward "\n" p t))
5274 (if (or cperl-break-one-line-blocks-when-indent ml)
5275 ;; not good: multi-line BLOCK
5276 (progn
5277 (goto-char (1+ pp))
5278 (delete-horizontal-space)
5279 (insert "\n")
5280 (setq ret (point))
5281 (if (cperl-indent-line parse-data)
5282 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5283 (beginning-of-line)
5284 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5285 ;; Now check whether there is a hanging `}'
5286 ;; Looking at:
5287 ;; } blah
5288 (if (and
5289 cperl-fix-hanging-brace-when-indent
5290 have-brace
5291 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5292 (condition-case nil
5293 (progn
5294 (up-list 1)
5295 (if (and (<= (point) pp)
5296 (eq (preceding-char) ?\} )
5297 (cperl-after-block-and-statement-beg (point-min)))
5299 (goto-char p)
5300 nil))
5301 (error nil)))
5302 (progn
5303 (forward-char -1)
5304 (skip-chars-backward " \t")
5305 (if (bolp)
5306 ;; `}' was the first thing on the line, insert NL *after* it.
5307 (progn
5308 (cperl-indent-line parse-data)
5309 (search-forward "}")
5310 (delete-horizontal-space)
5311 (insert "\n"))
5312 (delete-horizontal-space)
5313 (or (eq (preceding-char) ?\;)
5314 (bolp)
5315 (and (eq (preceding-char) ?\} )
5316 (cperl-after-block-p (point-min)))
5317 (insert ";"))
5318 (insert "\n")
5319 (setq ret (point)))
5320 (if (cperl-indent-line parse-data)
5321 (setq ret (cperl-fix-line-spacing end parse-data)))
5322 (beginning-of-line)))))
5323 ret))
5325 (defvar cperl-update-start) ; Do not need to make them local
5326 (defvar cperl-update-end)
5327 (defun cperl-delay-update-hook (beg end old-len)
5328 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5329 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5331 (defun cperl-indent-region (start end)
5332 "Simple variant of indentation of region in CPerl mode.
5333 Should be slow. Will not indent comment if it starts at `comment-indent'
5334 or looks like continuation of the comment on the previous line.
5335 Indents all the lines whose first character is between START and END
5336 inclusive.
5338 If `cperl-indent-region-fix-constructs', will improve spacing on
5339 conditional/loop constructs."
5340 (interactive "r")
5341 (cperl-update-syntaxification end end)
5342 (save-excursion
5343 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5344 (let ((indent-info (if cperl-emacs-can-parse
5345 (list nil nil nil) ; Cannot use '(), since will modify
5346 nil))
5347 (pm 0)
5348 after-change-functions ; Speed it up!
5349 st comm old-comm-indent new-comm-indent p pp i empty)
5350 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5351 (goto-char start)
5352 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5353 (current-column))
5354 new-comm-indent old-comm-indent)
5355 (goto-char start)
5356 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5357 (or (bolp) (beginning-of-line 2))
5358 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5359 (setq st (point))
5360 (if (or
5361 (setq empty (looking-at "[ \t]*\n"))
5362 (and (setq comm (looking-at "[ \t]*#"))
5363 (or (eq (current-indentation) (or old-comm-indent
5364 comment-column))
5365 (setq old-comm-indent nil))))
5366 (if (and old-comm-indent
5367 (not empty)
5368 (= (current-indentation) old-comm-indent)
5369 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5370 (not (eq (get-text-property (point) 'syntax-table)
5371 cperl-st-cfence)))
5372 (let ((comment-column new-comm-indent))
5373 (indent-for-comment)))
5374 (progn
5375 (setq i (cperl-indent-line indent-info))
5376 (or comm
5377 (not i)
5378 (progn
5379 (if cperl-indent-region-fix-constructs
5380 (goto-char (cperl-fix-line-spacing end indent-info)))
5381 (if (setq old-comm-indent
5382 (and (cperl-to-comment-or-eol)
5383 (not (memq (get-text-property (point)
5384 'syntax-type)
5385 '(pod here-doc)))
5386 (not (eq (get-text-property (point)
5387 'syntax-table)
5388 cperl-st-cfence))
5389 (current-column)))
5390 (progn (indent-for-comment)
5391 (skip-chars-backward " \t")
5392 (skip-chars-backward "#")
5393 (setq new-comm-indent (current-column))))))))
5394 (beginning-of-line 2)))
5395 ;; Now run the update hooks
5396 (and after-change-functions
5397 cperl-update-end
5398 (save-excursion
5399 (goto-char cperl-update-end)
5400 (insert " ")
5401 (delete-char -1)
5402 (goto-char cperl-update-start)
5403 (insert " ")
5404 (delete-char -1))))))
5406 ;; Stolen from lisp-mode with a lot of improvements
5408 (defun cperl-fill-paragraph (&optional justify iteration)
5409 "Like `fill-paragraph', but handle CPerl comments.
5410 If any of the current line is a comment, fill the comment or the
5411 block of it that point is in, preserving the comment's initial
5412 indentation and initial hashes. Behaves usually outside of comment."
5413 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5414 (let (;; Non-nil if the current line contains a comment.
5415 has-comment
5416 fill-paragraph-function ; do not recurse
5417 ;; If has-comment, the appropriate fill-prefix for the comment.
5418 comment-fill-prefix
5419 ;; Line that contains code and comment (or nil)
5420 start
5421 c spaces len dc (comment-column comment-column))
5422 ;; Figure out what kind of comment we are looking at.
5423 (save-excursion
5424 (beginning-of-line)
5425 (cond
5427 ;; A line with nothing but a comment on it?
5428 ((looking-at "[ \t]*#[# \t]*")
5429 (setq has-comment t
5430 comment-fill-prefix (buffer-substring (match-beginning 0)
5431 (match-end 0))))
5433 ;; A line with some code, followed by a comment? Remember that the
5434 ;; semi which starts the comment shouldn't be part of a string or
5435 ;; character.
5436 ((cperl-to-comment-or-eol)
5437 (setq has-comment t)
5438 (looking-at "#+[ \t]*")
5439 (setq start (point) c (current-column)
5440 comment-fill-prefix
5441 (concat (make-string (current-column) ?\s)
5442 (buffer-substring (match-beginning 0) (match-end 0)))
5443 spaces (progn (skip-chars-backward " \t")
5444 (buffer-substring (point) start))
5445 dc (- c (current-column)) len (- start (point))
5446 start (point-marker))
5447 (delete-char len)
5448 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5449 (if (not has-comment)
5450 (fill-paragraph justify) ; Do the usual thing outside of comment
5451 ;; Narrow to include only the comment, and then fill the region.
5452 (save-restriction
5453 (narrow-to-region
5454 ;; Find the first line we should include in the region to fill.
5455 (if start (progn (beginning-of-line) (point))
5456 (save-excursion
5457 (while (and (zerop (forward-line -1))
5458 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5459 ;; We may have gone to far. Go forward again.
5460 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5461 (forward-line 1))
5462 (point)))
5463 ;; Find the beginning of the first line past the region to fill.
5464 (save-excursion
5465 (while (progn (forward-line 1)
5466 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5467 (point)))
5468 ;; Remove existing hashes
5469 (save-excursion
5470 (goto-char (point-min))
5471 (while (progn (forward-line 1) (< (point) (point-max)))
5472 (skip-chars-forward " \t")
5473 (if (looking-at "#+")
5474 (progn
5475 (if (and (eq (point) (match-beginning 0))
5476 (not (eq (point) (match-end 0)))) nil
5477 (error
5478 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5479 (delete-char (- (match-end 0) (match-beginning 0)))))))
5481 ;; Lines with only hashes on them can be paragraph boundaries.
5482 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5483 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5484 (fill-prefix comment-fill-prefix))
5485 (fill-paragraph justify)))
5486 (if (and start)
5487 (progn
5488 (goto-char start)
5489 (if (> dc 0)
5490 (progn (delete-char dc) (insert spaces)))
5491 (if (or (= (current-column) c) iteration) nil
5492 (setq comment-column c)
5493 (indent-for-comment)
5494 ;; Repeat once more, flagging as iteration
5495 (cperl-fill-paragraph justify t))))))
5498 (defun cperl-do-auto-fill ()
5499 ;; Break out if the line is short enough
5500 (if (> (save-excursion
5501 (end-of-line)
5502 (current-column))
5503 fill-column)
5504 (let ((c (save-excursion (beginning-of-line)
5505 (cperl-to-comment-or-eol) (point)))
5506 (s (memq (following-char) '(?\s ?\t))) marker)
5507 (if (>= c (point))
5508 ;; Don't break line inside code: only inside comment.
5510 (setq marker (point-marker))
5511 (fill-paragraph nil)
5512 (goto-char marker)
5513 ;; Is not enough, sometimes marker is a start of line
5514 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5515 (goto-char (match-end 0))))
5516 ;; Following space could have gone:
5517 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5518 (insert " ")
5519 (backward-char 1))
5520 ;; Previous space could have gone:
5521 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5523 (defun cperl-imenu-addback (lst &optional isback name)
5524 ;; We suppose that the lst is a DAG, unless the first element only
5525 ;; loops back, and ISBACK is set. Thus this function cannot be
5526 ;; applied twice without ISBACK set.
5527 (cond ((not cperl-imenu-addback) lst)
5529 (or name
5530 (setq name "+++BACK+++"))
5531 (mapcar (lambda (elt)
5532 (if (and (listp elt) (listp (cdr elt)))
5533 (progn
5534 ;; In the other order it goes up
5535 ;; one level only ;-(
5536 (setcdr elt (cons (cons name lst)
5537 (cdr elt)))
5538 (cperl-imenu-addback (cdr elt) t name))))
5539 (if isback (cdr lst) lst))
5540 lst)))
5542 (defun cperl-imenu--create-perl-index (&optional regexp)
5543 (require 'imenu) ; May be called from TAGS creator
5544 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5545 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5546 (index-meth-alist '()) meth
5547 packages ends-ranges p marker is-proto
5548 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5549 (goto-char (point-min))
5550 (cperl-update-syntaxification (point-max) (point-max))
5551 ;; Search for the function
5552 (progn ;;save-match-data
5553 (while (re-search-forward
5554 (or regexp cperl-imenu--function-name-regexp-perl)
5555 nil t)
5556 ;; 2=package-group, 5=package-name 8=sub-name
5557 (cond
5558 ((and ; Skip some noise if building tags
5559 (match-beginning 5) ; package name
5560 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5561 (not (save-match-data
5562 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5563 nil)
5564 ((and
5565 (or (match-beginning 2)
5566 (match-beginning 8)) ; package or sub
5567 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5568 (null (get-text-property (match-beginning 1) 'syntax-table))
5569 (null (get-text-property (match-beginning 1) 'syntax-type))
5570 (null (get-text-property (match-beginning 1) 'in-pod)))
5571 (setq is-pack (match-beginning 2))
5572 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5573 ;; (goto-char (match-end 0))) ; Messes what follows
5574 (setq meth nil
5575 p (point))
5576 (while (and ends-ranges (>= p (car ends-ranges)))
5577 ;; delete obsolete entries
5578 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5579 (setq package (or (car packages) "")
5580 end-range (or (car ends-ranges) 0))
5581 (if is-pack ; doing "package"
5582 (progn
5583 (if (match-beginning 5) ; named package
5584 (setq name (buffer-substring (match-beginning 5)
5585 (match-end 5))
5586 name (progn
5587 (set-text-properties 0 (length name) nil name)
5588 name)
5589 package (concat name "::")
5590 name (concat "package " name))
5591 ;; Support nameless packages
5592 (setq name "package;" package ""))
5593 (setq end-range
5594 (save-excursion
5595 (parse-partial-sexp (point) (point-max) -1) (point))
5596 ends-ranges (cons end-range ends-ranges)
5597 packages (cons package packages)))
5598 (setq is-proto
5599 (or (eq (following-char) ?\;)
5600 (eq 0 (get-text-property (point) 'attrib-group)))))
5601 ;; Skip this function name if it is a prototype declaration.
5602 (if (and is-proto (not is-pack)) nil
5603 (or is-pack
5604 (setq name
5605 (buffer-substring (match-beginning 8) (match-end 8)))
5606 (set-text-properties 0 (length name) nil name))
5607 (setq marker (make-marker))
5608 (set-marker marker (match-end (if is-pack 2 8)))
5609 (cond (is-pack nil)
5610 ((string-match "[:']" name)
5611 (setq meth t))
5612 ((> p end-range) nil)
5614 (setq name (concat package name) meth t)))
5615 (setq index (cons name marker))
5616 (if is-pack
5617 (push index index-pack-alist)
5618 (push index index-alist))
5619 (if meth (push index index-meth-alist))
5620 (push index index-unsorted-alist)))
5621 ((match-beginning 16) ; POD section
5622 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5623 marker (make-marker))
5624 (set-marker marker (match-beginning 17))
5625 (set-text-properties 0 (length name) nil name)
5626 (setq name (concat (make-string
5627 (* 3 (- (char-after (match-beginning 16)) ?1))
5628 ?\ )
5629 name)
5630 index (cons name marker))
5631 (setq index1 (cons (concat "=" name) (cdr index)))
5632 (push index index-pod-alist)
5633 (push index1 index-unsorted-alist)))))
5634 (setq index-alist
5635 (if (default-value 'imenu-sort-function)
5636 (sort index-alist (default-value 'imenu-sort-function))
5637 (nreverse index-alist)))
5638 (and index-pod-alist
5639 (push (cons "+POD headers+..."
5640 (nreverse index-pod-alist))
5641 index-alist))
5642 (and (or index-pack-alist index-meth-alist)
5643 (let ((lst index-pack-alist) hier-list pack elt group name)
5644 ;; Remove "package ", reverse and uniquify.
5645 (while lst
5646 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5647 (if (assoc name hier-list) nil
5648 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5649 (setq lst index-meth-alist)
5650 (while lst
5651 (setq elt (car lst) lst (cdr lst))
5652 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5653 (setq pack (substring (car elt) 0 (match-beginning 0)))
5654 (if (setq group (assoc pack hier-list))
5655 (if (listp (cdr group))
5656 ;; Have some functions already
5657 (setcdr group
5658 (cons (cons (substring
5659 (car elt)
5660 (+ 2 (match-beginning 0)))
5661 (cdr elt))
5662 (cdr group)))
5663 (setcdr group (list (cons (substring
5664 (car elt)
5665 (+ 2 (match-beginning 0)))
5666 (cdr elt)))))
5667 (setq hier-list
5668 (cons (cons pack
5669 (list (cons (substring
5670 (car elt)
5671 (+ 2 (match-beginning 0)))
5672 (cdr elt))))
5673 hier-list))))))
5674 (push (cons "+Hierarchy+..."
5675 hier-list)
5676 index-alist)))
5677 (and index-pack-alist
5678 (push (cons "+Packages+..."
5679 (nreverse index-pack-alist))
5680 index-alist))
5681 (and (or index-pack-alist index-pod-alist
5682 (default-value 'imenu-sort-function))
5683 index-unsorted-alist
5684 (push (cons "+Unsorted List+..."
5685 (nreverse index-unsorted-alist))
5686 index-alist))
5687 (cperl-imenu-addback index-alist)))
5690 ;; Suggested by Mark A. Hershberger
5691 (defun cperl-outline-level ()
5692 (looking-at outline-regexp)
5693 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5694 ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5695 ((match-beginning 2) 0) ; package
5696 ((match-beginning 8) 1) ; sub
5697 ((match-beginning 16)
5698 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5699 (t 5))) ; should not happen
5702 (defvar cperl-compilation-error-regexp-alist
5703 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
5704 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
5705 2 3))
5706 "Alist that specifies how to match errors in perl output.")
5709 (defun cperl-windowed-init ()
5710 "Initialization under windowed version."
5711 (if (or (featurep 'ps-print) cperl-faces-init)
5712 ;; Need to init anyway:
5713 (or cperl-faces-init (cperl-init-faces))
5714 (add-hook 'font-lock-mode-hook
5715 (function
5716 (lambda ()
5717 (if (memq major-mode '(perl-mode cperl-mode))
5718 (progn
5719 (or cperl-faces-init (cperl-init-faces)))))))
5720 (if (fboundp 'eval-after-load)
5721 (eval-after-load
5722 "ps-print"
5723 '(or cperl-faces-init (cperl-init-faces))))))
5725 (defvar cperl-font-lock-keywords-1 nil
5726 "Additional expressions to highlight in Perl mode. Minimal set.")
5727 (defvar cperl-font-lock-keywords nil
5728 "Additional expressions to highlight in Perl mode. Default set.")
5729 (defvar cperl-font-lock-keywords-2 nil
5730 "Additional expressions to highlight in Perl mode. Maximal set")
5732 (defun cperl-load-font-lock-keywords ()
5733 (or cperl-faces-init (cperl-init-faces))
5734 cperl-font-lock-keywords)
5736 (defun cperl-load-font-lock-keywords-1 ()
5737 (or cperl-faces-init (cperl-init-faces))
5738 cperl-font-lock-keywords-1)
5740 (defun cperl-load-font-lock-keywords-2 ()
5741 (or cperl-faces-init (cperl-init-faces))
5742 cperl-font-lock-keywords-2)
5744 (defun cperl-init-faces-weak ()
5745 ;; Allow `cperl-find-pods-heres' to run.
5746 (or (boundp 'font-lock-constant-face)
5747 (cperl-force-face font-lock-constant-face
5748 "Face for constant and label names"))
5749 (or (boundp 'font-lock-warning-face)
5750 (cperl-force-face font-lock-warning-face
5751 "Face for things which should stand out"))
5752 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5755 (defun cperl-init-faces ()
5756 (condition-case errs
5757 (progn
5758 (require 'font-lock)
5759 (and (fboundp 'font-lock-fontify-anchored-keywords)
5760 (featurep 'font-lock-extra)
5761 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5762 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5763 (if (fboundp 'font-lock-fontify-anchored-keywords)
5764 (setq font-lock-anchored t))
5765 (setq
5766 t-font-lock-keywords
5767 (list
5768 `("[ \t]+$" 0 ',cperl-invalid-face t)
5769 (cons
5770 (concat
5771 "\\(^\\|[^$@%&\\]\\)\\<\\("
5772 (mapconcat
5773 'identity
5774 '("if" "until" "while" "elsif" "else" "unless" "for"
5775 "foreach" "continue" "exit" "die" "last" "goto" "next"
5776 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5777 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5778 "\\|") ; Flow control
5779 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5780 ; In what follows we use `type' style
5781 ; for overwritable builtins
5782 (list
5783 (concat
5784 "\\(^\\|[^$@%&\\]\\)\\<\\("
5785 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5786 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5787 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5788 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5789 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5790 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5791 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5792 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5793 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5794 ;; "gethostbyname" "gethostent" "getlogin"
5795 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5796 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5797 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5798 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5799 ;; "getservbyport" "getservent" "getsockname"
5800 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5801 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5802 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5803 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5804 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5805 ;; "quotemeta" "rand" "read" "readdir" "readline"
5806 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5807 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5808 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5809 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5810 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5811 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5812 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5813 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5814 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5815 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5816 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5817 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5818 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5819 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5820 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5821 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5822 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5823 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5824 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5825 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5826 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5827 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5828 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5829 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5830 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5831 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5832 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5833 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5834 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5835 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5836 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5837 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5838 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5839 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5840 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5841 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5842 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5843 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5844 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5845 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5846 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5847 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5848 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5849 "\\)\\>") 2 'font-lock-type-face)
5850 ;; In what follows we use `other' style
5851 ;; for nonoverwritable builtins
5852 ;; Somehow 's', 'm' are not auto-generated???
5853 (list
5854 (concat
5855 "\\(^\\|[^$@%&\\]\\)\\<\\("
5856 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5857 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5858 ;; "eval" "exists" "for" "foreach" "format" "goto"
5859 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5860 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5861 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5862 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5863 ;; "undef" "unless" "unshift" "untie" "until" "use"
5864 ;; "while" "y"
5865 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5866 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5867 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5868 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5869 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5870 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5871 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5872 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5873 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5874 "\\|[sm]" ; Added manually
5875 "\\)\\>") 2 'cperl-nonoverridable-face)
5876 ;; (mapconcat 'identity
5877 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5878 ;; "#include" "#define" "#undef")
5879 ;; "\\|")
5880 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5881 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5882 ;; This highlights declarations and definitions differenty.
5883 ;; We do not try to highlight in the case of attributes:
5884 ;; it is already done by `cperl-find-pods-heres'
5885 (list (concat "\\<sub"
5886 cperl-white-and-comment-rex ; whitespace/comments
5887 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5888 "\\("
5889 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5890 "([^()]*)\\)?" ; prototype
5891 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5892 "[{;]")
5893 2 (if cperl-font-lock-multiline
5894 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5895 'font-lock-function-name-face
5896 'font-lock-variable-name-face)
5897 ;; need to manually set 'multiline' for older font-locks
5898 '(progn
5899 (if (< 1 (count-lines (match-beginning 0)
5900 (match-end 0)))
5901 (put-text-property
5902 (+ 3 (match-beginning 0)) (match-end 0)
5903 'syntax-type 'multiline))
5904 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5905 'font-lock-function-name-face
5906 'font-lock-variable-name-face))))
5907 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5908 2 font-lock-function-name-face)
5909 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5910 1 font-lock-function-name-face)
5911 (cond ((featurep 'font-lock-extra)
5912 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5913 (2 font-lock-string-face t)
5914 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5915 (font-lock-anchored
5916 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5917 (2 font-lock-string-face t)
5918 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5919 nil nil
5920 (1 font-lock-string-face t))))
5921 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5922 2 font-lock-string-face t)))
5923 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5924 font-lock-string-face t)
5925 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5926 font-lock-constant-face) ; labels
5927 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5928 2 font-lock-constant-face)
5929 ;; Uncomment to get perl-mode-like vars
5930 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5931 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5932 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5933 (cond ((featurep 'font-lock-extra)
5934 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5935 (3 font-lock-variable-name-face)
5936 (4 '(another 4 nil
5937 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5938 (1 font-lock-variable-name-face)
5939 (2 '(restart 2 nil) nil t)))
5940 nil t))) ; local variables, multiple
5941 (font-lock-anchored
5942 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5943 (` ((, (concat "\\<\\(my\\|local\\|our\\)"
5944 cperl-maybe-white-and-comment-rex
5945 "\\(("
5946 cperl-maybe-white-and-comment-rex
5947 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)"))
5948 (5 (, (if cperl-font-lock-multiline
5949 'font-lock-variable-name-face
5950 '(progn (setq cperl-font-lock-multiline-start
5951 (match-beginning 0))
5952 'font-lock-variable-name-face))))
5953 ((, (concat "\\="
5954 cperl-maybe-white-and-comment-rex
5956 cperl-maybe-white-and-comment-rex
5957 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)"))
5958 ;; Bug in font-lock: limit is used not only to limit
5959 ;; searches, but to set the "extend window for
5960 ;; facification" property. Thus we need to minimize.
5961 (, (if cperl-font-lock-multiline
5962 '(if (match-beginning 3)
5963 (save-excursion
5964 (goto-char (match-beginning 3))
5965 (condition-case nil
5966 (forward-sexp 1)
5967 (error
5968 (condition-case nil
5969 (forward-char 200)
5970 (error nil)))) ; typeahead
5971 (1- (point))) ; report limit
5972 (forward-char -2)) ; disable continued expr
5973 '(if (match-beginning 3)
5974 (point-max) ; No limit for continuation
5975 (forward-char -2)))) ; disable continued expr
5976 (, (if cperl-font-lock-multiline
5978 '(progn ; Do at end
5979 ;; "my" may be already fontified (POD),
5980 ;; so cperl-font-lock-multiline-start is nil
5981 (if (or (not cperl-font-lock-multiline-start)
5982 (> 2 (count-lines
5983 cperl-font-lock-multiline-start
5984 (point))))
5986 (put-text-property
5987 (1+ cperl-font-lock-multiline-start) (point)
5988 'syntax-type 'multiline))
5989 (setq cperl-font-lock-multiline-start nil))))
5990 (3 font-lock-variable-name-face)))))
5991 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5992 3 font-lock-variable-name-face)))
5993 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5994 4 font-lock-variable-name-face)
5995 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5996 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5997 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5998 (setq
5999 t-font-lock-keywords-1
6000 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
6001 ;; not yet as of XEmacs 19.12, works with 21.1.11
6003 (not cperl-xemacs-p)
6004 (string< "21.1.9" emacs-version)
6005 (and (string< "21.1.10" emacs-version)
6006 (string< emacs-version "21.1.2")))
6008 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
6009 (if (eq (char-after (match-beginning 2)) ?%)
6010 'cperl-hash-face
6011 'cperl-array-face)
6012 t) ; arrays and hashes
6013 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
6015 (if (= (- (match-end 2) (match-beginning 2)) 1)
6016 (if (eq (char-after (match-beginning 3)) ?{)
6017 'cperl-hash-face
6018 'cperl-array-face) ; arrays and hashes
6019 font-lock-variable-name-face) ; Just to put something
6021 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
6022 (1 cperl-array-face)
6023 (2 font-lock-variable-name-face))
6024 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
6025 (1 cperl-hash-face)
6026 (2 font-lock-variable-name-face))
6027 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
6028 ;;; Too much noise from \s* @s[ and friends
6029 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
6030 ;;(3 font-lock-function-name-face t t)
6031 ;;(4
6032 ;; (if (cperl-slash-is-regexp)
6033 ;; font-lock-function-name-face 'default) nil t))
6035 (if cperl-highlight-variables-indiscriminately
6036 (setq t-font-lock-keywords-1
6037 (append t-font-lock-keywords-1
6038 (list '("\\([$*]{?\\sw+\\)" 1
6039 font-lock-variable-name-face)))))
6040 (setq cperl-font-lock-keywords-1
6041 (if cperl-syntaxify-by-font-lock
6042 (cons 'cperl-fontify-update
6043 t-font-lock-keywords)
6044 t-font-lock-keywords)
6045 cperl-font-lock-keywords cperl-font-lock-keywords-1
6046 cperl-font-lock-keywords-2 (append
6047 cperl-font-lock-keywords-1
6048 t-font-lock-keywords-1)))
6049 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
6050 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
6051 (eval ; Avoid a warning
6052 '(font-lock-require-faces
6053 (list
6054 ;; Color-light Color-dark Gray-light Gray-dark Mono
6055 (list 'font-lock-comment-face
6056 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
6058 [nil nil t t t]
6059 [nil nil t t t]
6060 nil)
6061 (list 'font-lock-string-face
6062 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
6065 [nil nil t t t]
6066 nil)
6067 (list 'font-lock-function-name-face
6068 (vector
6069 "Blue" "LightSkyBlue" "Gray50" "LightGray"
6070 (cdr (assq 'background-color ; if mono
6071 (frame-parameters))))
6072 (vector
6073 nil nil nil nil
6074 (cdr (assq 'foreground-color ; if mono
6075 (frame-parameters))))
6076 [nil nil t t t]
6078 nil)
6079 (list 'font-lock-variable-name-face
6080 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6082 [nil nil t t t]
6083 [nil nil t t t]
6084 nil)
6085 (list 'font-lock-type-face
6086 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6088 [nil nil t t t]
6090 [nil nil t t t])
6091 (list 'font-lock-warning-face
6092 ["Pink" "Red" "Gray50" "LightGray"]
6093 ["gray20" "gray90"
6094 "gray80" "gray20"]
6095 [nil nil t t t]
6097 [nil nil t t t]
6099 (list 'font-lock-constant-face
6100 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6102 [nil nil t t t]
6104 [nil nil t t t])
6105 (list 'cperl-nonoverridable-face
6106 ["chartreuse3" ("orchid1" "orange")
6107 nil "Gray80"]
6108 [nil nil "gray90"]
6109 [nil nil nil t t]
6110 [nil nil t t]
6111 [nil nil t t t])
6112 (list 'cperl-array-face
6113 ["blue" "yellow" nil "Gray80"]
6114 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6115 "gray90"]
6118 nil)
6119 (list 'cperl-hash-face
6120 ["red" "red" nil "Gray80"]
6121 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6122 "gray90"]
6125 nil))))
6126 ;; Do it the dull way, without choose-color
6127 (defvar cperl-guessed-background nil
6128 "Display characteristics as guessed by cperl.")
6129 ;; (or (fboundp 'x-color-defined-p)
6130 ;; (defalias 'x-color-defined-p
6131 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6132 ;; ;; XEmacs >= 19.12
6133 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6134 ;; ;; XEmacs 19.11
6135 ;; (t 'x-valid-color-name-p))))
6136 (cperl-force-face font-lock-constant-face
6137 "Face for constant and label names")
6138 (cperl-force-face font-lock-variable-name-face
6139 "Face for variable names")
6140 (cperl-force-face font-lock-type-face
6141 "Face for data types")
6142 (cperl-force-face cperl-nonoverridable-face
6143 "Face for data types from another group")
6144 (cperl-force-face font-lock-warning-face
6145 "Face for things which should stand out")
6146 (cperl-force-face font-lock-comment-face
6147 "Face for comments")
6148 (cperl-force-face font-lock-function-name-face
6149 "Face for function names")
6150 (cperl-force-face cperl-hash-face
6151 "Face for hashes")
6152 (cperl-force-face cperl-array-face
6153 "Face for arrays")
6154 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6155 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6156 ;;(or (boundp 'font-lock-type-face)
6157 ;; (defconst font-lock-type-face
6158 ;; 'font-lock-type-face
6159 ;; "Face to use for data types."))
6160 ;;(or (boundp 'cperl-nonoverridable-face)
6161 ;; (defconst cperl-nonoverridable-face
6162 ;; 'cperl-nonoverridable-face
6163 ;; "Face to use for data types from another group."))
6164 ;;(if (not cperl-xemacs-p) nil
6165 ;; (or (boundp 'font-lock-comment-face)
6166 ;; (defconst font-lock-comment-face
6167 ;; 'font-lock-comment-face
6168 ;; "Face to use for comments."))
6169 ;; (or (boundp 'font-lock-keyword-face)
6170 ;; (defconst font-lock-keyword-face
6171 ;; 'font-lock-keyword-face
6172 ;; "Face to use for keywords."))
6173 ;; (or (boundp 'font-lock-function-name-face)
6174 ;; (defconst font-lock-function-name-face
6175 ;; 'font-lock-function-name-face
6176 ;; "Face to use for function names.")))
6177 (if (and
6178 (not (cperl-is-face 'cperl-array-face))
6179 (cperl-is-face 'font-lock-emphasized-face))
6180 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6181 (if (and
6182 (not (cperl-is-face 'cperl-hash-face))
6183 (cperl-is-face 'font-lock-other-emphasized-face))
6184 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6185 (if (and
6186 (not (cperl-is-face 'cperl-nonoverridable-face))
6187 (cperl-is-face 'font-lock-other-type-face))
6188 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6189 ;;(or (boundp 'cperl-hash-face)
6190 ;; (defconst cperl-hash-face
6191 ;; 'cperl-hash-face
6192 ;; "Face to use for hashes."))
6193 ;;(or (boundp 'cperl-array-face)
6194 ;; (defconst cperl-array-face
6195 ;; 'cperl-array-face
6196 ;; "Face to use for arrays."))
6197 ;; Here we try to guess background
6198 (let ((background
6199 (if (boundp 'font-lock-background-mode)
6200 font-lock-background-mode
6201 'light))
6202 (face-list (and (fboundp 'face-list) (face-list))))
6203 ;;;; (fset 'cperl-is-face
6204 ;;;; (cond ((fboundp 'find-face)
6205 ;;;; (symbol-function 'find-face))
6206 ;;;; (face-list
6207 ;;;; (function (lambda (face) (member face face-list))))
6208 ;;;; (t
6209 ;;;; (function (lambda (face) (boundp face))))))
6210 (defvar cperl-guessed-background
6211 (if (and (boundp 'font-lock-display-type)
6212 (eq font-lock-display-type 'grayscale))
6213 'gray
6214 background)
6215 "Background as guessed by CPerl mode")
6216 (and (not (cperl-is-face 'font-lock-constant-face))
6217 (cperl-is-face 'font-lock-reference-face)
6218 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6219 (if (cperl-is-face 'font-lock-type-face) nil
6220 (copy-face 'default 'font-lock-type-face)
6221 (cond
6222 ((eq background 'light)
6223 (set-face-foreground 'font-lock-type-face
6224 (if (x-color-defined-p "seagreen")
6225 "seagreen"
6226 "sea green")))
6227 ((eq background 'dark)
6228 (set-face-foreground 'font-lock-type-face
6229 (if (x-color-defined-p "os2pink")
6230 "os2pink"
6231 "pink")))
6233 (set-face-background 'font-lock-type-face "gray90"))))
6234 (if (cperl-is-face 'cperl-nonoverridable-face)
6236 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6237 (cond
6238 ((eq background 'light)
6239 (set-face-foreground 'cperl-nonoverridable-face
6240 (if (x-color-defined-p "chartreuse3")
6241 "chartreuse3"
6242 "chartreuse")))
6243 ((eq background 'dark)
6244 (set-face-foreground 'cperl-nonoverridable-face
6245 (if (x-color-defined-p "orchid1")
6246 "orchid1"
6247 "orange")))))
6248 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6249 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6250 ;;; (cond
6251 ;;; ((eq background 'light)
6252 ;;; (set-face-background 'font-lock-other-emphasized-face
6253 ;;; (if (x-color-defined-p "lightyellow2")
6254 ;;; "lightyellow2"
6255 ;;; (if (x-color-defined-p "lightyellow")
6256 ;;; "lightyellow"
6257 ;;; "light yellow"))))
6258 ;;; ((eq background 'dark)
6259 ;;; (set-face-background 'font-lock-other-emphasized-face
6260 ;;; (if (x-color-defined-p "navy")
6261 ;;; "navy"
6262 ;;; (if (x-color-defined-p "darkgreen")
6263 ;;; "darkgreen"
6264 ;;; "dark green"))))
6265 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6266 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6267 ;;; (copy-face 'bold 'font-lock-emphasized-face)
6268 ;;; (cond
6269 ;;; ((eq background 'light)
6270 ;;; (set-face-background 'font-lock-emphasized-face
6271 ;;; (if (x-color-defined-p "lightyellow2")
6272 ;;; "lightyellow2"
6273 ;;; "lightyellow")))
6274 ;;; ((eq background 'dark)
6275 ;;; (set-face-background 'font-lock-emphasized-face
6276 ;;; (if (x-color-defined-p "navy")
6277 ;;; "navy"
6278 ;;; (if (x-color-defined-p "darkgreen")
6279 ;;; "darkgreen"
6280 ;;; "dark green"))))
6281 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6282 (if (cperl-is-face 'font-lock-variable-name-face) nil
6283 (copy-face 'italic 'font-lock-variable-name-face))
6284 (if (cperl-is-face 'font-lock-constant-face) nil
6285 (copy-face 'italic 'font-lock-constant-face))))
6286 (setq cperl-faces-init t))
6287 (error (message "cperl-init-faces (ignored): %s" errs))))
6290 (defun cperl-ps-print-init ()
6291 "Initialization of `ps-print' components for faces used in CPerl."
6292 (eval-after-load "ps-print"
6293 '(setq ps-bold-faces
6294 ;; font-lock-variable-name-face
6295 ;; font-lock-constant-face
6296 (append '(cperl-array-face cperl-hash-face)
6297 ps-bold-faces)
6298 ps-italic-faces
6299 ;; font-lock-constant-face
6300 (append '(cperl-nonoverridable-face cperl-hash-face)
6301 ps-italic-faces)
6302 ps-underlined-faces
6303 ;; font-lock-type-face
6304 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6305 ps-underlined-faces))))
6307 (defvar ps-print-face-extension-alist)
6309 (defun cperl-ps-print (&optional file)
6310 "Pretty-print in CPerl style.
6311 If optional argument FILE is an empty string, prints to printer, otherwise
6312 to the file FILE. If FILE is nil, prompts for a file name.
6314 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6315 (interactive)
6316 (or file
6317 (setq file (read-from-minibuffer
6318 "Print to file (if empty - to printer): "
6319 (concat (buffer-file-name) ".ps")
6320 nil nil 'file-name-history)))
6321 (or (> (length file) 0)
6322 (setq file nil))
6323 (require 'ps-print) ; To get ps-print-face-extension-alist
6324 (let ((ps-print-color-p t)
6325 (ps-print-face-extension-alist ps-print-face-extension-alist))
6326 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6327 (ps-print-buffer-with-faces file)))
6329 ;;; (defun cperl-ps-print-init ()
6330 ;;; "Initialization of `ps-print' components for faces used in CPerl."
6331 ;;; ;; Guard against old versions
6332 ;;; (defvar ps-underlined-faces nil)
6333 ;;; (defvar ps-bold-faces nil)
6334 ;;; (defvar ps-italic-faces nil)
6335 ;;; (setq ps-bold-faces
6336 ;;; (append '(font-lock-emphasized-face
6337 ;;; cperl-array-face
6338 ;;; font-lock-keyword-face
6339 ;;; font-lock-variable-name-face
6340 ;;; font-lock-constant-face
6341 ;;; font-lock-reference-face
6342 ;;; font-lock-other-emphasized-face
6343 ;;; cperl-hash-face)
6344 ;;; ps-bold-faces))
6345 ;;; (setq ps-italic-faces
6346 ;;; (append '(cperl-nonoverridable-face
6347 ;;; font-lock-constant-face
6348 ;;; font-lock-reference-face
6349 ;;; font-lock-other-emphasized-face
6350 ;;; cperl-hash-face)
6351 ;;; ps-italic-faces))
6352 ;;; (setq ps-underlined-faces
6353 ;;; (append '(font-lock-emphasized-face
6354 ;;; cperl-array-face
6355 ;;; font-lock-other-emphasized-face
6356 ;;; cperl-hash-face
6357 ;;; cperl-nonoverridable-face font-lock-type-face)
6358 ;;; ps-underlined-faces))
6359 ;;; (cons 'font-lock-type-face ps-underlined-faces))
6362 (if (cperl-enable-font-lock) (cperl-windowed-init))
6364 (defconst cperl-styles-entries
6365 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6366 cperl-label-offset cperl-extra-newline-before-brace
6367 cperl-extra-newline-before-brace-multiline
6368 cperl-merge-trailing-else
6369 cperl-continued-statement-offset))
6371 (defconst cperl-style-examples
6372 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6373 ##### cperl-continued-brace-offset cperl-label-offset
6374 ##### cperl-continued-statement-offset
6375 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6377 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6379 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6380 if (foo) {
6382 baz;
6383 label:
6385 boon;
6387 } else {
6388 stop;
6391 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6392 if (foo) {
6394 baz;
6395 label:
6397 boon;
6399 } else {
6400 stop;
6403 ### GNU 2/0/0/-2/2/nil/t
6404 if (foo)
6407 baz;
6408 label:
6410 boon;
6413 else
6415 stop;
6418 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6419 if (foo)
6422 baz;
6423 label:
6425 boon;
6428 else
6430 stop;
6433 ### BSD (=C++, but will not change preexisting merge-trailing-else
6434 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6435 if (foo)
6438 baz;
6439 label:
6441 boon;
6444 else
6446 stop;
6449 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6450 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6451 if (foo)
6454 baz;
6455 label:
6457 boon;
6460 else
6462 stop;
6465 ### Whitesmith (=PerlStyle, but will not change preexisting
6466 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6467 if (foo)
6470 baz;
6471 label:
6473 boon;
6476 else
6478 stop;
6481 "Examples of if/else with different indent styles (with v4.23).")
6483 (defconst cperl-style-alist
6484 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6485 (cperl-indent-level . 2)
6486 (cperl-brace-offset . 0)
6487 (cperl-continued-brace-offset . 0)
6488 (cperl-label-offset . -2)
6489 (cperl-continued-statement-offset . 2)
6490 (cperl-extra-newline-before-brace . nil)
6491 (cperl-extra-newline-before-brace-multiline . nil)
6492 (cperl-merge-trailing-else . t))
6494 ("PerlStyle" ; CPerl with 4 as indent
6495 (cperl-indent-level . 4)
6496 (cperl-brace-offset . 0)
6497 (cperl-continued-brace-offset . 0)
6498 (cperl-label-offset . -4)
6499 (cperl-continued-statement-offset . 4)
6500 (cperl-extra-newline-before-brace . nil)
6501 (cperl-extra-newline-before-brace-multiline . nil)
6502 (cperl-merge-trailing-else . t))
6504 ("GNU"
6505 (cperl-indent-level . 2)
6506 (cperl-brace-offset . 0)
6507 (cperl-continued-brace-offset . 0)
6508 (cperl-label-offset . -2)
6509 (cperl-continued-statement-offset . 2)
6510 (cperl-extra-newline-before-brace . t)
6511 (cperl-extra-newline-before-brace-multiline . t)
6512 (cperl-merge-trailing-else . nil))
6514 ("K&R"
6515 (cperl-indent-level . 5)
6516 (cperl-brace-offset . 0)
6517 (cperl-continued-brace-offset . -5)
6518 (cperl-label-offset . -5)
6519 (cperl-continued-statement-offset . 5)
6520 ;;(cperl-extra-newline-before-brace . nil) ; ???
6521 ;;(cperl-extra-newline-before-brace-multiline . nil)
6522 (cperl-merge-trailing-else . nil))
6524 ("BSD"
6525 (cperl-indent-level . 4)
6526 (cperl-brace-offset . 0)
6527 (cperl-continued-brace-offset . -4)
6528 (cperl-label-offset . -4)
6529 (cperl-continued-statement-offset . 4)
6530 ;;(cperl-extra-newline-before-brace . nil) ; ???
6531 ;;(cperl-extra-newline-before-brace-multiline . nil)
6532 ;;(cperl-merge-trailing-else . nil) ; ???
6535 ("C++"
6536 (cperl-indent-level . 4)
6537 (cperl-brace-offset . 0)
6538 (cperl-continued-brace-offset . -4)
6539 (cperl-label-offset . -4)
6540 (cperl-continued-statement-offset . 4)
6541 (cperl-extra-newline-before-brace . t)
6542 (cperl-extra-newline-before-brace-multiline . t)
6543 (cperl-merge-trailing-else . nil))
6545 ("Whitesmith"
6546 (cperl-indent-level . 4)
6547 (cperl-brace-offset . 0)
6548 (cperl-continued-brace-offset . 0)
6549 (cperl-label-offset . -4)
6550 (cperl-continued-statement-offset . 4)
6551 ;;(cperl-extra-newline-before-brace . nil) ; ???
6552 ;;(cperl-extra-newline-before-brace-multiline . nil)
6553 ;;(cperl-merge-trailing-else . nil) ; ???
6555 ("Current"))
6556 "List of variables to set to get a particular indentation style.
6557 Should be used via `cperl-set-style' or via Perl menu.
6559 See examples in `cperl-style-examples'.")
6561 (defun cperl-set-style (style)
6562 "Set CPerl mode variables to use one of several different indentation styles.
6563 The arguments are a string representing the desired style.
6564 The list of styles is in `cperl-style-alist', available styles
6565 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6567 The current value of style is memorized (unless there is a memorized
6568 data already), may be restored by `cperl-set-style-back'.
6570 Chosing \"Current\" style will not change style, so this may be used for
6571 side-effect of memorizing only. Examples in `cperl-style-examples'."
6572 (interactive
6573 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6574 cperl-style-alist)))
6575 (list (completing-read "Enter style: " list nil 'insist))))
6576 (or cperl-old-style
6577 (setq cperl-old-style
6578 (mapcar (function
6579 (lambda (name)
6580 (cons name (eval name))))
6581 cperl-styles-entries)))
6582 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6583 (while style
6584 (setq setting (car style) style (cdr style))
6585 (set (car setting) (cdr setting)))))
6587 (defun cperl-set-style-back ()
6588 "Restore a style memorized by `cperl-set-style'."
6589 (interactive)
6590 (or cperl-old-style (error "The style was not changed"))
6591 (let (setting)
6592 (while cperl-old-style
6593 (setq setting (car cperl-old-style)
6594 cperl-old-style (cdr cperl-old-style))
6595 (set (car setting) (cdr setting)))))
6597 (defun cperl-check-syntax ()
6598 (interactive)
6599 (require 'mode-compile)
6600 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6601 (eval '(mode-compile)))) ; Avoid a warning
6603 (defun cperl-info-buffer (type)
6604 ;; Returns buffer with documentation. Creates if missing.
6605 ;; If TYPE, this vars buffer.
6606 ;; Special care is taken to not stomp over an existing info buffer
6607 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6608 (info (get-buffer bname))
6609 (oldbuf (get-buffer "*info*")))
6610 (if info info
6611 (save-window-excursion
6612 ;; Get Info running
6613 (require 'info)
6614 (cond (oldbuf
6615 (set-buffer oldbuf)
6616 (rename-buffer "*info-perl-tmp*")))
6617 (save-window-excursion
6618 (info))
6619 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6620 (set-buffer "*info*")
6621 (rename-buffer bname)
6622 (cond (oldbuf
6623 (set-buffer "*info-perl-tmp*")
6624 (rename-buffer "*info*")
6625 (set-buffer bname)))
6626 (make-local-variable 'window-min-height)
6627 (setq window-min-height 2)
6628 (current-buffer)))))
6630 (defun cperl-word-at-point (&optional p)
6631 "Return the word at point or at P."
6632 (save-excursion
6633 (if p (goto-char p))
6634 (or (cperl-word-at-point-hard)
6635 (progn
6636 (require 'etags)
6637 (funcall (or (and (boundp 'find-tag-default-function)
6638 find-tag-default-function)
6639 (get major-mode 'find-tag-default-function)
6640 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6641 ;; automatically used within `find-tag-default':
6642 'find-tag-default))))))
6644 (defun cperl-info-on-command (command)
6645 "Show documentation for Perl command COMMAND in other window.
6646 If perl-info buffer is shown in some frame, uses this frame.
6647 Customized by setting variables `cperl-shrink-wrap-info-frame',
6648 `cperl-max-help-size'."
6649 (interactive
6650 (let* ((default (cperl-word-at-point))
6651 (read (read-string
6652 (format "Find doc for Perl function (default %s): "
6653 default))))
6654 (list (if (equal read "")
6655 default
6656 read))))
6658 (let ((buffer (current-buffer))
6659 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6660 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6661 max-height char-height buf-list)
6662 (if (string-match "^-[a-zA-Z]$" command)
6663 (setq cmd-desc "^-X[ \t\n]"))
6664 (setq isvar (string-match "^[$@%]" command)
6665 buf (cperl-info-buffer isvar)
6666 iniwin (selected-window)
6667 fr1 (window-frame iniwin))
6668 (set-buffer buf)
6669 (goto-char (point-min))
6670 (or isvar
6671 (progn (re-search-forward "^-X[ \t\n]")
6672 (forward-line -1)))
6673 (if (re-search-forward cmd-desc nil t)
6674 (progn
6675 ;; Go back to beginning of the group (ex, for qq)
6676 (if (re-search-backward "^[ \t\n\f]")
6677 (forward-line 1))
6678 (beginning-of-line)
6679 ;; Get some of
6680 (setq pos (point)
6681 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6682 (while (and (not win) buf-list)
6683 (setq win (get-buffer-window (car buf-list) t))
6684 (setq buf-list (cdr buf-list)))
6685 (or (not win)
6686 (eq (window-buffer win) buf)
6687 (set-window-buffer win buf))
6688 (and win (setq fr2 (window-frame win)))
6689 (if (or (not fr2) (eq fr1 fr2))
6690 (pop-to-buffer buf)
6691 (special-display-popup-frame buf) ; Make it visible
6692 (select-window win))
6693 (goto-char pos) ; Needed (?!).
6694 ;; Resize
6695 (setq iniheight (window-height)
6696 frheight (frame-height)
6697 not-loner (< iniheight (1- frheight))) ; Are not alone
6698 (cond ((if not-loner cperl-max-help-size
6699 cperl-shrink-wrap-info-frame)
6700 (setq height
6701 (+ 2
6702 (count-lines
6704 (save-excursion
6705 (if (re-search-forward
6706 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6707 (match-beginning 0) (point-max)))))
6708 max-height
6709 (if not-loner
6710 (/ (* (- frheight 3) cperl-max-help-size) 100)
6711 (setq char-height (frame-char-height))
6712 ;; Non-functioning under OS/2:
6713 (if (eq char-height 1) (setq char-height 18))
6714 ;; Title, menubar, + 2 for slack
6715 (- (/ (x-display-pixel-height) char-height) 4)))
6716 (if (> height max-height) (setq height max-height))
6717 ;;(message "was %s doing %s" iniheight height)
6718 (if not-loner
6719 (enlarge-window (- height iniheight))
6720 (set-frame-height (window-frame win) (1+ height)))))
6721 (set-window-start (selected-window) pos))
6722 (message "No entry for %s found." command))
6723 ;;(pop-to-buffer buffer)
6724 (select-window iniwin)))
6726 (defun cperl-info-on-current-command ()
6727 "Show documentation for Perl command at point in other window."
6728 (interactive)
6729 (cperl-info-on-command (cperl-word-at-point)))
6731 (defun cperl-imenu-info-imenu-search ()
6732 (if (looking-at "^-X[ \t\n]") nil
6733 (re-search-backward
6734 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6735 (forward-line 1)))
6737 (defun cperl-imenu-info-imenu-name ()
6738 (buffer-substring
6739 (match-beginning 1) (match-end 1)))
6741 (defun cperl-imenu-on-info ()
6742 "Shows imenu for Perl Info Buffer.
6743 Opens Perl Info buffer if needed."
6744 (interactive)
6745 (let* ((buffer (current-buffer))
6746 imenu-create-index-function
6747 imenu-prev-index-position-function
6748 imenu-extract-index-name-function
6749 (index-item (save-restriction
6750 (save-window-excursion
6751 (set-buffer (cperl-info-buffer nil))
6752 (setq imenu-create-index-function
6753 'imenu-default-create-index-function
6754 imenu-prev-index-position-function
6755 'cperl-imenu-info-imenu-search
6756 imenu-extract-index-name-function
6757 'cperl-imenu-info-imenu-name)
6758 (imenu-choose-buffer-index)))))
6759 (and index-item
6760 (progn
6761 (push-mark)
6762 (pop-to-buffer "*info-perl*")
6763 (cond
6764 ((markerp (cdr index-item))
6765 (goto-char (marker-position (cdr index-item))))
6767 (goto-char (cdr index-item))))
6768 (set-window-start (selected-window) (point))
6769 (pop-to-buffer buffer)))))
6771 (defun cperl-lineup (beg end &optional step minshift)
6772 "Lineup construction in a region.
6773 Beginning of region should be at the start of a construction.
6774 All first occurrences of this construction in the lines that are
6775 partially contained in the region are lined up at the same column.
6777 MINSHIFT is the minimal amount of space to insert before the construction.
6778 STEP is the tabwidth to position constructions.
6779 If STEP is nil, `cperl-lineup-step' will be used
6780 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6781 Will not move the position at the start to the left."
6782 (interactive "r")
6783 (let (search col tcol seen b)
6784 (save-excursion
6785 (goto-char end)
6786 (end-of-line)
6787 (setq end (point-marker))
6788 (goto-char beg)
6789 (skip-chars-forward " \t\f")
6790 (setq beg (point-marker))
6791 (indent-region beg end nil)
6792 (goto-char beg)
6793 (setq col (current-column))
6794 (if (looking-at "[a-zA-Z0-9_]")
6795 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6796 (setq search
6797 (concat "\\<"
6798 (regexp-quote
6799 (buffer-substring (match-beginning 0)
6800 (match-end 0))) "\\>"))
6801 (error "Cannot line up in a middle of the word"))
6802 (if (looking-at "$")
6803 (error "Cannot line up end of line"))
6804 (setq search (regexp-quote (char-to-string (following-char)))))
6805 (setq step (or step cperl-lineup-step cperl-indent-level))
6806 (or minshift (setq minshift 1))
6807 (while (progn
6808 (beginning-of-line 2)
6809 (and (< (point) end)
6810 (re-search-forward search end t)
6811 (goto-char (match-beginning 0))))
6812 (setq tcol (current-column) seen t)
6813 (if (> tcol col) (setq col tcol)))
6814 (or seen
6815 (error "The construction to line up occurred only once"))
6816 (goto-char beg)
6817 (setq col (+ col minshift))
6818 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6819 (while
6820 (progn
6821 (cperl-make-indent col)
6822 (beginning-of-line 2)
6823 (and (< (point) end)
6824 (re-search-forward search end t)
6825 (goto-char (match-beginning 0)))))))) ; No body
6827 (defun cperl-etags (&optional add all files) ;; NOT USED???
6828 "Run etags with appropriate options for Perl files.
6829 If optional argument ALL is `recursive', will process Perl files
6830 in subdirectories too."
6831 (interactive)
6832 (let ((cmd "etags")
6833 (args '("-l" "none" "-r"
6834 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6835 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6836 "-r"
6837 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6838 "-r"
6839 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6840 res)
6841 (if add (setq args (cons "-a" args)))
6842 (or files (setq files (list buffer-file-name)))
6843 (cond
6844 ((eq all 'recursive)
6845 ;;(error "Not implemented: recursive")
6846 (setq args (append (list "-e"
6847 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6848 use File::Find;
6849 find(\\&wanted, '.');
6850 exec @ARGV;"
6851 cmd) args)
6852 cmd "perl"))
6853 (all
6854 ;;(error "Not implemented: all")
6855 (setq args (append (list "-e"
6856 "push @ARGV, <*.PL *.pl *.pm>;
6857 exec @ARGV;"
6858 cmd) args)
6859 cmd "perl"))
6861 (setq args (append args files))))
6862 (setq res (apply 'call-process cmd nil nil nil args))
6863 (or (eq res 0)
6864 (message "etags returned \"%s\"" res))))
6866 (defun cperl-toggle-auto-newline ()
6867 "Toggle the state of `cperl-auto-newline'."
6868 (interactive)
6869 (setq cperl-auto-newline (not cperl-auto-newline))
6870 (message "Newlines will %sbe auto-inserted now."
6871 (if cperl-auto-newline "" "not ")))
6873 (defun cperl-toggle-abbrev ()
6874 "Toggle the state of automatic keyword expansion in CPerl mode."
6875 (interactive)
6876 (abbrev-mode (if abbrev-mode 0 1))
6877 (message "Perl control structure will %sbe auto-inserted now."
6878 (if abbrev-mode "" "not ")))
6881 (defun cperl-toggle-electric ()
6882 "Toggle the state of parentheses doubling in CPerl mode."
6883 (interactive)
6884 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6885 (message "Parentheses will %sbe auto-doubled now."
6886 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6888 (defun cperl-toggle-autohelp ()
6889 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6890 Delay of auto-help controlled by `cperl-lazy-help-time'."
6891 (interactive)
6892 (if (fboundp 'run-with-idle-timer)
6893 (progn
6894 (if cperl-lazy-installed
6895 (cperl-lazy-unstall)
6896 (cperl-lazy-install))
6897 (message "Perl help messages will %sbe automatically shown now."
6898 (if cperl-lazy-installed "" "not ")))
6899 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6901 (defun cperl-toggle-construct-fix ()
6902 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6903 (interactive)
6904 (setq cperl-indent-region-fix-constructs
6905 (if cperl-indent-region-fix-constructs
6908 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6909 (if cperl-indent-region-fix-constructs "" "not ")))
6911 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6912 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6913 Nonpositive numeric argument disables debugging messages. The message
6914 summarizes which regions it was decided to rescan for syntactic constructs.
6916 The message looks like this:
6918 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6920 Numbers are character positions in the buffer. REQ provides the range to
6921 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6922 for correct operation it should start and end outside any special syntactic
6923 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6924 by CPerl."
6925 (interactive "P")
6926 (or arg
6927 (setq arg (if (eq cperl-syntaxify-by-font-lock
6928 (if backtrace 'backtrace 'message)) 0 1)))
6929 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6930 (setq cperl-syntaxify-by-font-lock arg)
6931 (message "Debugging messages of syntax unwind %sabled."
6932 (if (eq arg t) "dis" "en")))
6934 ;;;; Tags file creation.
6936 (defvar cperl-tmp-buffer " *cperl-tmp*")
6938 (defun cperl-setup-tmp-buf ()
6939 (set-buffer (get-buffer-create cperl-tmp-buffer))
6940 (set-syntax-table cperl-mode-syntax-table)
6941 (buffer-disable-undo)
6942 (auto-fill-mode 0)
6943 (if cperl-use-syntax-table-text-property-for-tags
6944 (progn
6945 (make-local-variable 'parse-sexp-lookup-properties)
6946 ;; Do not introduce variable if not needed, we check it!
6947 (set 'parse-sexp-lookup-properties t))))
6949 (defun cperl-xsub-scan ()
6950 (require 'imenu)
6951 (let ((index-alist '())
6952 (prev-pos 0) index index1 name package prefix)
6953 (goto-char (point-min))
6954 ;; Search for the function
6955 (progn ;;save-match-data
6956 (while (re-search-forward
6957 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6958 nil t)
6959 (cond
6960 ((match-beginning 2) ; SECTION
6961 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6962 (goto-char (match-beginning 0))
6963 (skip-chars-forward " \t")
6964 (forward-char 1)
6965 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6966 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6967 (setq prefix nil)))
6968 ((not package) nil) ; C language section
6969 ((match-beginning 3) ; XSUB
6970 (goto-char (1+ (match-beginning 3)))
6971 (setq index (imenu-example--name-and-position))
6972 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6973 (if (and prefix (string-match (concat "^" prefix) name))
6974 (setq name (substring name (length prefix))))
6975 (cond ((string-match "::" name) nil)
6977 (setq index1 (cons (concat package "::" name) (cdr index)))
6978 (push index1 index-alist)))
6979 (setcar index name)
6980 (push index index-alist))
6981 (t ; BOOT: section
6982 ;; (beginning-of-line)
6983 (setq index (imenu-example--name-and-position))
6984 (setcar index (concat package "::BOOT:"))
6985 (push index index-alist)))))
6986 index-alist))
6988 (defvar cperl-unreadable-ok nil)
6990 (defun cperl-find-tags (ifile xs topdir)
6991 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6992 (cperl-pod-here-fontify nil) f file)
6993 (save-excursion
6994 (if b (set-buffer b)
6995 (cperl-setup-tmp-buf))
6996 (erase-buffer)
6997 (condition-case err
6998 (setq file (car (insert-file-contents ifile)))
6999 (error (if cperl-unreadable-ok nil
7000 (if (y-or-n-p
7001 (format "File %s unreadable. Continue? " ifile))
7002 (setq cperl-unreadable-ok t)
7003 (error "Aborting: unreadable file %s" ifile)))))
7004 (if (not file)
7005 (message "Unreadable file %s" ifile)
7006 (message "Scanning file %s ..." file)
7007 (if (and cperl-use-syntax-table-text-property-for-tags
7008 (not xs))
7009 (condition-case err ; after __END__ may have garbage
7010 (cperl-find-pods-heres nil nil noninteractive)
7011 (error (message "While scanning for syntax: %s" err))))
7012 (if xs
7013 (setq lst (cperl-xsub-scan))
7014 (setq ind (cperl-imenu--create-perl-index))
7015 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
7016 (setq lst
7017 (mapcar
7018 (function
7019 (lambda (elt)
7020 (cond ((string-match "^[_a-zA-Z]" (car elt))
7021 (goto-char (cdr elt))
7022 (beginning-of-line) ; pos should be of the start of the line
7023 (list (car elt)
7024 (point)
7025 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
7026 (buffer-substring (progn
7027 (goto-char (cdr elt))
7028 ;; After name now...
7029 (or (eolp) (forward-char 1))
7030 (point))
7031 (progn
7032 (beginning-of-line)
7033 (point))))))))
7034 lst))
7035 (erase-buffer)
7036 (while lst
7037 (setq elt (car lst) lst (cdr lst))
7038 (if elt
7039 (progn
7040 (insert (elt elt 3)
7042 (if (string-match "^package " (car elt))
7043 (substring (car elt) 8)
7044 (car elt) )
7046 (number-to-string (elt elt 2)) ; Line
7048 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
7049 "\n")
7050 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
7051 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
7052 (elt elt 3)))
7053 ;; Need to insert the name without package as well
7054 (setq lst (cons (cons (substring (elt elt 3)
7055 (match-beginning 1)
7056 (match-end 1))
7057 (cdr elt))
7058 lst))))))
7059 (setq pos (point))
7060 (goto-char 1)
7061 (setq rel file)
7062 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7063 (set-text-properties 0 (length rel) nil rel)
7064 (and (equal topdir (substring rel 0 (length topdir)))
7065 (setq rel (substring file (length topdir))))
7066 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
7067 (setq ret (buffer-substring 1 (point-max)))
7068 (erase-buffer)
7069 (or noninteractive
7070 (message "Scanning file %s finished" file))
7071 ret))))
7073 (defun cperl-add-tags-recurse-noxs ()
7074 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
7075 Use as
7076 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7077 -f cperl-add-tags-recurse-noxs
7079 (cperl-write-tags nil nil t t nil t))
7081 (defun cperl-add-tags-recurse-noxs-fullpath ()
7082 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7083 Writes down fullpath, so TAGS is relocatable (but if the build directory
7084 is relocated, the file TAGS inside it breaks). Use as
7085 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7086 -f cperl-add-tags-recurse-noxs-fullpath
7088 (cperl-write-tags nil nil t t nil t ""))
7090 (defun cperl-add-tags-recurse ()
7091 "Add to TAGS file data for Perl files in the current directory and kids.
7092 Use as
7093 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7094 -f cperl-add-tags-recurse
7096 (cperl-write-tags nil nil t t))
7098 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7099 ;; If INBUFFER, do not select buffer, and do not save
7100 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7101 (require 'etags)
7102 (if file nil
7103 (setq file (if dir default-directory (buffer-file-name)))
7104 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7105 (or topdir
7106 (setq topdir default-directory))
7107 (let ((tags-file-name "TAGS")
7108 (case-fold-search (eq system-type 'emx))
7109 xs rel tm)
7110 (save-excursion
7111 (cond (inbuffer nil) ; Already there
7112 ((file-exists-p tags-file-name)
7113 (if cperl-xemacs-p
7114 (visit-tags-table-buffer)
7115 (visit-tags-table-buffer tags-file-name)))
7116 (t (set-buffer (find-file-noselect tags-file-name))))
7117 (cond
7118 (dir
7119 (cond ((eq erase 'ignore))
7120 (erase
7121 (erase-buffer)
7122 (setq erase 'ignore)))
7123 (let ((files
7124 (condition-case err
7125 (directory-files file t
7126 (if recurse nil cperl-scan-files-regexp)
7128 (error
7129 (if cperl-unreadable-ok nil
7130 (if (y-or-n-p
7131 (format "Directory %s unreadable. Continue? " file))
7132 (setq cperl-unreadable-ok t
7133 tm nil) ; Return empty list
7134 (error "Aborting: unreadable directory %s" file)))))))
7135 (mapcar (function
7136 (lambda (file)
7137 (cond
7138 ((string-match cperl-noscan-files-regexp file)
7139 nil)
7140 ((not (file-directory-p file))
7141 (if (string-match cperl-scan-files-regexp file)
7142 (cperl-write-tags file erase recurse nil t noxs topdir)))
7143 ((not recurse) nil)
7144 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7145 files)))
7147 (setq xs (string-match "\\.xs$" file))
7148 (if (not (and xs noxs))
7149 (progn
7150 (cond ((eq erase 'ignore) (goto-char (point-max)))
7151 (erase (erase-buffer))
7153 (goto-char 1)
7154 (setq rel file)
7155 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7156 (set-text-properties 0 (length rel) nil rel)
7157 (and (equal topdir (substring rel 0 (length topdir)))
7158 (setq rel (substring file (length topdir))))
7159 (if (search-forward (concat "\f\n" rel ",") nil t)
7160 (progn
7161 (search-backward "\f\n")
7162 (delete-region (point)
7163 (save-excursion
7164 (forward-char 1)
7165 (if (search-forward "\f\n"
7166 nil 'toend)
7167 (- (point) 2)
7168 (point-max)))))
7169 (goto-char (point-max)))))
7170 (insert (cperl-find-tags file xs topdir))))))
7171 (if inbuffer nil ; Delegate to the caller
7172 (save-buffer 0) ; No backup
7173 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7174 (initialize-new-tags-table))))))
7176 (defvar cperl-tags-hier-regexp-list
7177 (concat
7178 "^\\("
7179 "\\(package\\)\\>"
7180 "\\|"
7181 "sub\\>[^\n]+::"
7182 "\\|"
7183 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7184 "\\|"
7185 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7186 "\\)"))
7188 (defvar cperl-hierarchy '(() ())
7189 "Global hierarchy of classes.")
7191 (defun cperl-tags-hier-fill ()
7192 ;; Suppose we are in a tag table cooked by cperl.
7193 (goto-char 1)
7194 (let (type pack name pos line chunk ord cons1 file str info fileind)
7195 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7196 (setq pos (match-beginning 0)
7197 pack (match-beginning 2))
7198 (beginning-of-line)
7199 (if (looking-at (concat
7200 "\\([^\n]+\\)"
7201 "\C-?"
7202 "\\([^\n]+\\)"
7203 "\C-a"
7204 "\\([0-9]+\\)"
7206 "\\([0-9]+\\)"))
7207 (progn
7208 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7209 name (buffer-substring (match-beginning 2) (match-end 2))
7210 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7211 line (buffer-substring (match-beginning 3) (match-end 3))
7212 ord (if pack 1 0)
7213 file (file-of-tag)
7214 fileind (format "%s:%s" file line)
7215 ;; Moves to beginning of the next line:
7216 info (cperl-etags-snarf-tag file line))
7217 ;; Move back
7218 (forward-char -1)
7219 ;; Make new member of hierarchy name ==> file ==> pos if needed
7220 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7221 ;; Name known
7222 (setcdr cons1 (cons (cons fileind (vector file info))
7223 (cdr cons1)))
7224 ;; First occurrence of the name, start alist
7225 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7226 (if pack
7227 (setcar (cdr cperl-hierarchy)
7228 (cons cons1 (nth 1 cperl-hierarchy)))
7229 (setcar cperl-hierarchy
7230 (cons cons1 (car cperl-hierarchy)))))))
7231 (end-of-line))))
7233 (defun cperl-tags-hier-init (&optional update)
7234 "Show hierarchical menu of classes and methods.
7235 Finds info about classes by a scan of loaded TAGS files.
7236 Supposes that the TAGS files contain fully qualified function names.
7237 One may build such TAGS files from CPerl mode menu."
7238 (interactive)
7239 (require 'etags)
7240 (require 'imenu)
7241 (if (or update (null (nth 2 cperl-hierarchy)))
7242 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7243 (or (nthcdr 2 elt)
7244 ;; Only in one file
7245 (setcdr elt (cdr (nth 1 elt)))))))
7246 pack name cons1 to l1 l2 l3 l4 b)
7247 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7248 (setq cperl-hierarchy (list l1 l2 l3))
7249 (if cperl-xemacs-p ; Not checked
7250 (progn
7251 (or tags-file-name
7252 ;; Does this work in XEmacs?
7253 (call-interactively 'visit-tags-table))
7254 (message "Updating list of classes...")
7255 (set-buffer (get-file-buffer tags-file-name))
7256 (cperl-tags-hier-fill))
7257 (or tags-table-list
7258 (call-interactively 'visit-tags-table))
7259 (mapcar
7260 (function
7261 (lambda (tagsfile)
7262 (message "Updating list of classes... %s" tagsfile)
7263 (set-buffer (get-file-buffer tagsfile))
7264 (cperl-tags-hier-fill)))
7265 tags-table-list)
7266 (message "Updating list of classes... postprocessing..."))
7267 (mapcar remover (car cperl-hierarchy))
7268 (mapcar remover (nth 1 cperl-hierarchy))
7269 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7270 (cons "Methods: " (car cperl-hierarchy))))
7271 (cperl-tags-treeify to 1)
7272 (setcar (nthcdr 2 cperl-hierarchy)
7273 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7274 (message "Updating list of classes: done, requesting display...")
7275 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7277 (or (nth 2 cperl-hierarchy)
7278 (error "No items found"))
7279 (setq update
7280 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7281 (if (if (fboundp 'display-popup-menus-p)
7282 (let ((f 'display-popup-menus-p))
7283 (funcall f))
7284 window-system)
7285 (x-popup-menu t (nth 2 cperl-hierarchy))
7286 (require 'tmm)
7287 (tmm-prompt (nth 2 cperl-hierarchy))))
7288 (if (and update (listp update))
7289 (progn (while (cdr update) (setq update (cdr update)))
7290 (setq update (car update)))) ; Get the last from the list
7291 (if (vectorp update)
7292 (progn
7293 (find-file (elt update 0))
7294 (cperl-etags-goto-tag-location (elt update 1))))
7295 (if (eq update -999) (cperl-tags-hier-init t)))
7297 (defun cperl-tags-treeify (to level)
7298 ;; cadr of `to' is read-write. On start it is a cons
7299 (let* ((regexp (concat "^\\(" (mapconcat
7300 'identity
7301 (make-list level "[_a-zA-Z0-9]+")
7302 "::")
7303 "\\)\\(::\\)?"))
7304 (packages (cdr (nth 1 to)))
7305 (methods (cdr (nth 2 to)))
7306 l1 head tail cons1 cons2 ord writeto packs recurse
7307 root-packages root-functions ms many_ms same_name ps
7308 (move-deeper
7309 (function
7310 (lambda (elt)
7311 (cond ((and (string-match regexp (car elt))
7312 (or (eq ord 1) (match-end 2)))
7313 (setq head (substring (car elt) 0 (match-end 1))
7314 tail (if (match-end 2) (substring (car elt)
7315 (match-end 2)))
7316 recurse t)
7317 (if (setq cons1 (assoc head writeto)) nil
7318 ;; Need to init new head
7319 (setcdr writeto (cons (list head (list "Packages: ")
7320 (list "Methods: "))
7321 (cdr writeto)))
7322 (setq cons1 (nth 1 writeto)))
7323 (setq cons2 (nth ord cons1)) ; Either packs or meths
7324 (setcdr cons2 (cons elt (cdr cons2))))
7325 ((eq ord 2)
7326 (setq root-functions (cons elt root-functions)))
7328 (setq root-packages (cons elt root-packages))))))))
7329 (setcdr to l1) ; Init to dynamic space
7330 (setq writeto to)
7331 (setq ord 1)
7332 (mapcar move-deeper packages)
7333 (setq ord 2)
7334 (mapcar move-deeper methods)
7335 (if recurse
7336 (mapcar (function (lambda (elt)
7337 (cperl-tags-treeify elt (1+ level))))
7338 (cdr to)))
7339 ;;Now clean up leaders with one child only
7340 (mapcar (function (lambda (elt)
7341 (if (not (and (listp (cdr elt))
7342 (eq (length elt) 2))) nil
7343 (setcar elt (car (nth 1 elt)))
7344 (setcdr elt (cdr (nth 1 elt))))))
7345 (cdr to))
7346 ;; Sort the roots of subtrees
7347 (if (default-value 'imenu-sort-function)
7348 (setcdr to
7349 (sort (cdr to) (default-value 'imenu-sort-function))))
7350 ;; Now add back functions removed from display
7351 (mapcar (function (lambda (elt)
7352 (setcdr to (cons elt (cdr to)))))
7353 (if (default-value 'imenu-sort-function)
7354 (nreverse
7355 (sort root-functions (default-value 'imenu-sort-function)))
7356 root-functions))
7357 ;; Now add back packages removed from display
7358 (mapcar (function (lambda (elt)
7359 (setcdr to (cons (cons (concat "package " (car elt))
7360 (cdr elt))
7361 (cdr to)))))
7362 (if (default-value 'imenu-sort-function)
7363 (nreverse
7364 (sort root-packages (default-value 'imenu-sort-function)))
7365 root-packages))))
7367 ;;;(x-popup-menu t
7368 ;;; '(keymap "Name1"
7369 ;;; ("Ret1" "aa")
7370 ;;; ("Head1" "ab"
7371 ;;; keymap "Name2"
7372 ;;; ("Tail1" "x") ("Tail2" "y"))))
7374 (defun cperl-list-fold (list name limit)
7375 (let (list1 list2 elt1 (num 0))
7376 (if (<= (length list) limit) list
7377 (setq list1 nil list2 nil)
7378 (while list
7379 (setq num (1+ num)
7380 elt1 (car list)
7381 list (cdr list))
7382 (if (<= num imenu-max-items)
7383 (setq list2 (cons elt1 list2))
7384 (setq list1 (cons (cons name
7385 (nreverse list2))
7386 list1)
7387 list2 (list elt1)
7388 num 1)))
7389 (nreverse (cons (cons name
7390 (nreverse list2))
7391 list1)))))
7393 (defun cperl-menu-to-keymap (menu &optional name)
7394 (let (list)
7395 (cons 'keymap
7396 (mapcar
7397 (function
7398 (lambda (elt)
7399 (cond ((listp (cdr elt))
7400 (setq list (cperl-list-fold
7401 (cdr elt) (car elt) imenu-max-items))
7402 (cons nil
7403 (cons (car elt)
7404 (cperl-menu-to-keymap list))))
7406 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7407 (cperl-list-fold menu "Root" imenu-max-items)))))
7410 (defvar cperl-bad-style-regexp
7411 (mapconcat 'identity
7412 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7413 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7414 "\\|")
7415 "Finds places such that insertion of a whitespace may help a lot.")
7417 (defvar cperl-not-bad-style-regexp
7418 (mapconcat
7419 'identity
7420 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7421 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7422 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7423 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7424 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7425 "-[0-9]" ; -5
7426 "\\+\\+" ; ++var
7427 "--" ; --var
7428 ".->" ; a->b
7429 "->" ; a SPACE ->b
7430 "\\[-" ; a[-1]
7431 "\\\\[&$@*\\\\]" ; \&func
7432 "^=" ; =head
7433 "\\$." ; $|
7434 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7435 "||"
7436 "&&"
7437 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7438 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7439 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7440 ;;"[*/+-|&<.]+="
7442 "\\|")
7443 "If matches at the start of match found by `my-bad-c-style-regexp',
7444 insertion of a whitespace will not help.")
7446 (defvar found-bad)
7448 (defun cperl-find-bad-style ()
7449 "Find places in the buffer where insertion of a whitespace may help.
7450 Prompts user for insertion of spaces.
7451 Currently it is tuned to C and Perl syntax."
7452 (interactive)
7453 (let (found-bad (p (point)))
7454 (setq last-nonmenu-event 13) ; To disable popup
7455 (goto-char (point-min))
7456 (map-y-or-n-p "Insert space here? "
7457 (lambda (arg) (insert " "))
7458 'cperl-next-bad-style
7459 '("location" "locations" "insert a space into")
7460 '((?\C-r (lambda (arg)
7461 (let ((buffer-quit-function
7462 'exit-recursive-edit))
7463 (message "Exit with Esc Esc")
7464 (recursive-edit)
7465 t)) ; Consider acted upon
7466 "edit, exit with Esc Esc")
7467 (?e (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"))
7475 (if found-bad (goto-char found-bad)
7476 (goto-char p)
7477 (message "No appropriate place found"))))
7479 (defun cperl-next-bad-style ()
7480 (let (p (not-found t) (point (point)) found)
7481 (while (and not-found
7482 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7483 (setq p (point))
7484 (goto-char (match-beginning 0))
7485 (if (or
7486 (looking-at cperl-not-bad-style-regexp)
7487 ;; Check for a < -b and friends
7488 (and (eq (following-char) ?\-)
7489 (save-excursion
7490 (skip-chars-backward " \t\n")
7491 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7492 ;; Now check for syntax type
7493 (save-match-data
7494 (setq found (point))
7495 (beginning-of-defun)
7496 (let ((pps (parse-partial-sexp (point) found)))
7497 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7498 (goto-char (match-end 0))
7499 (goto-char (1- p))
7500 (setq not-found nil
7501 found-bad found)))
7502 (not not-found)))
7505 ;;; Getting help
7506 (defvar cperl-have-help-regexp
7507 ;;(concat "\\("
7508 (mapconcat
7509 'identity
7510 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7511 "[$@]\\^[a-zA-Z]" ; Special variable
7512 "[$@][^ \n\t]" ; Special variable
7513 "-[a-zA-Z]" ; File test
7514 "\\\\[a-zA-Z0]" ; Special chars
7515 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7516 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7517 "[a-zA-Z_0-9:]+" ; symbol or number
7518 "x="
7519 "#!")
7520 ;;"\\)\\|\\("
7521 "\\|")
7522 ;;"\\)"
7524 "Matches places in the buffer we can find help for.")
7526 (defvar cperl-message-on-help-error t)
7527 (defvar cperl-help-from-timer nil)
7529 (defun cperl-word-at-point-hard ()
7530 ;; Does not save-excursion
7531 ;; Get to the something meaningful
7532 (or (eobp) (eolp) (forward-char 1))
7533 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7534 (save-excursion (beginning-of-line) (point))
7535 'to-beg)
7536 ;; (cond
7537 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7538 ;; (skip-chars-backward " \n\t\r({[]});,")
7539 ;; (or (bobp) (backward-char 1))))
7540 ;; Try to backtrace
7541 (cond
7542 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7543 (skip-chars-backward "a-zA-Z0-9_:")
7544 (cond
7545 ((and (eq (preceding-char) ?^) ; $^I
7546 (eq (char-after (- (point) 2)) ?\$))
7547 (forward-char -2))
7548 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7549 (forward-char -1))
7550 ((and (eq (preceding-char) ?\=)
7551 (eq (current-column) 1))
7552 (forward-char -1))) ; =head1
7553 (if (and (eq (preceding-char) ?\<)
7554 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7555 (forward-char -1)))
7556 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7557 (forward-char -1))
7558 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7559 (forward-char -1))
7560 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7561 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7562 (cond
7563 ((and (eq (preceding-char) ?\$)
7564 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7565 (forward-char -1))
7566 ((and (eq (following-char) ?\>)
7567 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7568 (save-excursion
7569 (forward-sexp -1)
7570 (and (eq (preceding-char) ?\<)
7571 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7572 (search-backward "<"))))
7573 ((and (eq (following-char) ?\$)
7574 (eq (preceding-char) ?\<)
7575 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7576 (forward-char -1)))
7577 (if (looking-at cperl-have-help-regexp)
7578 (buffer-substring (match-beginning 0) (match-end 0))))
7580 (defun cperl-get-help ()
7581 "Get one-line docs on the symbol at the point.
7582 The data for these docs is a little bit obsolete and may be in fact longer
7583 than a line. Your contribution to update/shorten it is appreciated."
7584 (interactive)
7585 (save-match-data ; May be called "inside" query-replace
7586 (save-excursion
7587 (let ((word (cperl-word-at-point-hard)))
7588 (if word
7589 (if (and cperl-help-from-timer ; Bail out if not in mainland
7590 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7591 (or (memq (get-text-property (point) 'face)
7592 '(font-lock-comment-face font-lock-string-face))
7593 (memq (get-text-property (point) 'syntax-type)
7594 '(pod here-doc format))))
7596 (cperl-describe-perl-symbol word))
7597 (if cperl-message-on-help-error
7598 (message "Nothing found for %s..."
7599 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7601 ;;; Stolen from perl-descr.el by Johan Vromans:
7603 (defvar cperl-doc-buffer " *perl-doc*"
7604 "Where the documentation can be found.")
7606 (defun cperl-describe-perl-symbol (val)
7607 "Display the documentation of symbol at point, a Perl operator."
7608 (let ((enable-recursive-minibuffers t)
7609 args-file regexp)
7610 (cond
7611 ((string-match "^[&*][a-zA-Z_]" val)
7612 (setq val (concat (substring val 0 1) "NAME")))
7613 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7614 (setq val (concat "@" (substring val 1 (match-end 1)))))
7615 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7616 (setq val (concat "%" (substring val 1 (match-end 1)))))
7617 ((and (string= val "x") (string-match "^x=" val))
7618 (setq val "x="))
7619 ((string-match "^\\$[\C-a-\C-z]" val)
7620 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7621 ((string-match "^CORE::" val)
7622 (setq val "CORE::"))
7623 ((string-match "^SUPER::" val)
7624 (setq val "SUPER::"))
7625 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7626 (setq val "<NAME>")))
7627 (setq regexp (concat "^"
7628 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7629 (regexp-quote val)
7630 "\\([ \t([/]\\|$\\)"))
7632 ;; get the buffer with the documentation text
7633 (cperl-switch-to-doc-buffer)
7635 ;; lookup in the doc
7636 (goto-char (point-min))
7637 (let ((case-fold-search nil))
7638 (list
7639 (if (re-search-forward regexp (point-max) t)
7640 (save-excursion
7641 (beginning-of-line 1)
7642 (let ((lnstart (point)))
7643 (end-of-line)
7644 (message "%s" (buffer-substring lnstart (point)))))
7645 (if cperl-message-on-help-error
7646 (message "No definition for %s" val)))))))
7648 (defvar cperl-short-docs 'please-ignore-this-line
7649 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7650 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7651 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7652 ! ... Logical negation.
7653 ... != ... Numeric inequality.
7654 ... !~ ... Search pattern, substitution, or translation (negated).
7655 $! In numeric context: errno. In a string context: error string.
7656 $\" The separator which joins elements of arrays interpolated in strings.
7657 $# The output format for printed numbers. Default is %.15g or close.
7658 $$ Process number of this script. Changes in the fork()ed child process.
7659 $% The current page number of the currently selected output channel.
7661 The following variables are always local to the current block:
7663 $1 Match of the 1st set of parentheses in the last match (auto-local).
7664 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7665 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7666 $4 Match of the 4th set of parentheses in the last match (auto-local).
7667 $5 Match of the 5th set of parentheses in the last match (auto-local).
7668 $6 Match of the 6th set of parentheses in the last match (auto-local).
7669 $7 Match of the 7th set of parentheses in the last match (auto-local).
7670 $8 Match of the 8th set of parentheses in the last match (auto-local).
7671 $9 Match of the 9th set of parentheses in the last match (auto-local).
7672 $& The string matched by the last pattern match (auto-local).
7673 $' The string after what was matched by the last match (auto-local).
7674 $` The string before what was matched by the last match (auto-local).
7676 $( The real gid of this process.
7677 $) The effective gid of this process.
7678 $* Deprecated: Set to 1 to do multiline matching within a string.
7679 $+ The last bracket matched by the last search pattern.
7680 $, The output field separator for the print operator.
7681 $- The number of lines left on the page.
7682 $. The current input line number of the last filehandle that was read.
7683 $/ The input record separator, newline by default.
7684 $0 Name of the file containing the current perl script (read/write).
7685 $: String may be broken after these characters to fill ^-lines in a format.
7686 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7687 $< The real uid of this process.
7688 $= The page length of the current output channel. Default is 60 lines.
7689 $> The effective uid of this process.
7690 $? The status returned by the last ``, pipe close or `system'.
7691 $@ The perl error message from the last eval or do @var{EXPR} command.
7692 $ARGV The name of the current file used with <> .
7693 $[ Deprecated: The index of the first element/char in an array/string.
7694 $\\ The output record separator for the print operator.
7695 $] The perl version string as displayed with perl -v.
7696 $^ The name of the current top-of-page format.
7697 $^A The current value of the write() accumulator for format() lines.
7698 $^D The value of the perl debug (-D) flags.
7699 $^E Information about the last system error other than that provided by $!.
7700 $^F The highest system file descriptor, ordinarily 2.
7701 $^H The current set of syntax checks enabled by `use strict'.
7702 $^I The value of the in-place edit extension (perl -i option).
7703 $^L What formats output to perform a formfeed. Default is \\f.
7704 $^M A buffer for emergency memory allocation when running out of memory.
7705 $^O The operating system name under which this copy of Perl was built.
7706 $^P Internal debugging flag.
7707 $^T The time the script was started. Used by -A/-M/-C file tests.
7708 $^W True if warnings are requested (perl -w flag).
7709 $^X The name under which perl was invoked (argv[0] in C-speech).
7710 $_ The default input and pattern-searching space.
7711 $| Auto-flush after write/print on current output channel? Default 0.
7712 $~ The name of the current report format.
7713 ... % ... Modulo division.
7714 ... %= ... Modulo division assignment.
7715 %ENV Contains the current environment.
7716 %INC List of files that have been require-d or do-ne.
7717 %SIG Used to set signal handlers for various signals.
7718 ... & ... Bitwise and.
7719 ... && ... Logical and.
7720 ... &&= ... Logical and assignment.
7721 ... &= ... Bitwise and assignment.
7722 ... * ... Multiplication.
7723 ... ** ... Exponentiation.
7724 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7725 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7726 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7727 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7728 ... += ... Addition assignment.
7729 , Comma operator.
7730 ... - ... Subtraction.
7731 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7732 ... -= ... Subtraction assignment.
7733 -A Access time in days since script started.
7734 -B File is a non-text (binary) file.
7735 -C Inode change time in days since script started.
7736 -M Age in days since script started.
7737 -O File is owned by real uid.
7738 -R File is readable by real uid.
7739 -S File is a socket .
7740 -T File is a text file.
7741 -W File is writable by real uid.
7742 -X File is executable by real uid.
7743 -b File is a block special file.
7744 -c File is a character special file.
7745 -d File is a directory.
7746 -e File exists .
7747 -f File is a plain file.
7748 -g File has setgid bit set.
7749 -k File has sticky bit set.
7750 -l File is a symbolic link.
7751 -o File is owned by effective uid.
7752 -p File is a named pipe (FIFO).
7753 -r File is readable by effective uid.
7754 -s File has non-zero size.
7755 -t Tests if filehandle (STDIN by default) is opened to a tty.
7756 -u File has setuid bit set.
7757 -w File is writable by effective uid.
7758 -x File is executable by effective uid.
7759 -z File has zero size.
7760 . Concatenate strings.
7761 .. Range (list context); flip/flop (scalar context) operator.
7762 .= Concatenate assignment strings
7763 ... / ... Division. /PATTERN/ioxsmg Pattern match
7764 ... /= ... Division assignment.
7765 /PATTERN/ioxsmg Pattern match.
7766 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7767 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7768 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7769 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7770 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7771 ... <= ... Numeric less than or equal to.
7772 ... <=> ... Numeric compare.
7773 ... = ... Assignment.
7774 ... == ... Numeric equality.
7775 ... =~ ... Search pattern, substitution, or translation
7776 ... > ... Numeric greater than.
7777 ... >= ... Numeric greater than or equal to.
7778 ... >> ... Bitwise shift right.
7779 ... >>= ... Bitwise shift right assignment.
7780 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7781 ?PATTERN? One-time pattern match.
7782 @ARGV Command line arguments (not including the command name - see $0).
7783 @INC List of places to look for perl scripts during do/include/use.
7784 @_ Parameter array for subroutines; result of split() unless in list context.
7785 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7786 \\0 Octal char, e.g. \\033.
7787 \\E Case modification terminator. See \\Q, \\L, and \\U.
7788 \\L Lowercase until \\E . See also \\l, lc.
7789 \\U Upcase until \\E . See also \\u, uc.
7790 \\Q Quote metacharacters until \\E . See also quotemeta.
7791 \\a Alarm character (octal 007).
7792 \\b Backspace character (octal 010).
7793 \\c Control character, e.g. \\c[ .
7794 \\e Escape character (octal 033).
7795 \\f Formfeed character (octal 014).
7796 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7797 \\n Newline character (octal 012 on most systems).
7798 \\r Return character (octal 015 on most systems).
7799 \\t Tab character (octal 011).
7800 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7801 \\x Hex character, e.g. \\x1b.
7802 ... ^ ... Bitwise exclusive or.
7803 __END__ Ends program source.
7804 __DATA__ Ends program source.
7805 __FILE__ Current (source) filename.
7806 __LINE__ Current line in current source.
7807 __PACKAGE__ Current package.
7808 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7809 ARGVOUT Output filehandle with -i flag.
7810 BEGIN { ... } Immediately executed (during compilation) piece of code.
7811 END { ... } Pseudo-subroutine executed after the script finishes.
7812 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7813 INIT { ... } Pseudo-subroutine executed before the script starts running.
7814 DATA Input filehandle for what follows after __END__ or __DATA__.
7815 accept(NEWSOCKET,GENERICSOCKET)
7816 alarm(SECONDS)
7817 atan2(X,Y)
7818 bind(SOCKET,NAME)
7819 binmode(FILEHANDLE)
7820 caller[(LEVEL)]
7821 chdir(EXPR)
7822 chmod(LIST)
7823 chop[(LIST|VAR)]
7824 chown(LIST)
7825 chroot(FILENAME)
7826 close(FILEHANDLE)
7827 closedir(DIRHANDLE)
7828 ... cmp ... String compare.
7829 connect(SOCKET,NAME)
7830 continue of { block } continue { block }. Is executed after `next' or at end.
7831 cos(EXPR)
7832 crypt(PLAINTEXT,SALT)
7833 dbmclose(%HASH)
7834 dbmopen(%HASH,DBNAME,MODE)
7835 defined(EXPR)
7836 delete($HASH{KEY})
7837 die(LIST)
7838 do { ... }|SUBR while|until EXPR executes at least once
7839 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7840 dump LABEL
7841 each(%HASH)
7842 endgrent
7843 endhostent
7844 endnetent
7845 endprotoent
7846 endpwent
7847 endservent
7848 eof[([FILEHANDLE])]
7849 ... eq ... String equality.
7850 eval(EXPR) or eval { BLOCK }
7851 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7852 exit(EXPR)
7853 exp(EXPR)
7854 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7855 fileno(FILEHANDLE)
7856 flock(FILEHANDLE,OPERATION)
7857 for (EXPR;EXPR;EXPR) { ... }
7858 foreach [VAR] (@ARRAY) { ... }
7859 fork
7860 ... ge ... String greater than or equal.
7861 getc[(FILEHANDLE)]
7862 getgrent
7863 getgrgid(GID)
7864 getgrnam(NAME)
7865 gethostbyaddr(ADDR,ADDRTYPE)
7866 gethostbyname(NAME)
7867 gethostent
7868 getlogin
7869 getnetbyaddr(ADDR,ADDRTYPE)
7870 getnetbyname(NAME)
7871 getnetent
7872 getpeername(SOCKET)
7873 getpgrp(PID)
7874 getppid
7875 getpriority(WHICH,WHO)
7876 getprotobyname(NAME)
7877 getprotobynumber(NUMBER)
7878 getprotoent
7879 getpwent
7880 getpwnam(NAME)
7881 getpwuid(UID)
7882 getservbyname(NAME,PROTO)
7883 getservbyport(PORT,PROTO)
7884 getservent
7885 getsockname(SOCKET)
7886 getsockopt(SOCKET,LEVEL,OPTNAME)
7887 gmtime(EXPR)
7888 goto LABEL
7889 ... gt ... String greater than.
7890 hex(EXPR)
7891 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7892 index(STR,SUBSTR[,OFFSET])
7893 int(EXPR)
7894 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7895 join(EXPR,LIST)
7896 keys(%HASH)
7897 kill(LIST)
7898 last [LABEL]
7899 ... le ... String less than or equal.
7900 length(EXPR)
7901 link(OLDFILE,NEWFILE)
7902 listen(SOCKET,QUEUESIZE)
7903 local(LIST)
7904 localtime(EXPR)
7905 log(EXPR)
7906 lstat(EXPR|FILEHANDLE|VAR)
7907 ... lt ... String less than.
7908 m/PATTERN/iogsmx
7909 mkdir(FILENAME,MODE)
7910 msgctl(ID,CMD,ARG)
7911 msgget(KEY,FLAGS)
7912 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7913 msgsnd(ID,MSG,FLAGS)
7914 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7915 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7916 ... ne ... String inequality.
7917 next [LABEL]
7918 oct(EXPR)
7919 open(FILEHANDLE[,EXPR])
7920 opendir(DIRHANDLE,EXPR)
7921 ord(EXPR) ASCII value of the first char of the string.
7922 pack(TEMPLATE,LIST)
7923 package NAME Introduces package context.
7924 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7925 pop(ARRAY)
7926 print [FILEHANDLE] [(LIST)]
7927 printf [FILEHANDLE] (FORMAT,LIST)
7928 push(ARRAY,LIST)
7929 q/STRING/ Synonym for 'STRING'
7930 qq/STRING/ Synonym for \"STRING\"
7931 qx/STRING/ Synonym for `STRING`
7932 rand[(EXPR)]
7933 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7934 readdir(DIRHANDLE)
7935 readlink(EXPR)
7936 recv(SOCKET,SCALAR,LEN,FLAGS)
7937 redo [LABEL]
7938 rename(OLDNAME,NEWNAME)
7939 require [FILENAME | PERL_VERSION]
7940 reset[(EXPR)]
7941 return(LIST)
7942 reverse(LIST)
7943 rewinddir(DIRHANDLE)
7944 rindex(STR,SUBSTR[,OFFSET])
7945 rmdir(FILENAME)
7946 s/PATTERN/REPLACEMENT/gieoxsm
7947 scalar(EXPR)
7948 seek(FILEHANDLE,POSITION,WHENCE)
7949 seekdir(DIRHANDLE,POS)
7950 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7951 semctl(ID,SEMNUM,CMD,ARG)
7952 semget(KEY,NSEMS,SIZE,FLAGS)
7953 semop(KEY,...)
7954 send(SOCKET,MSG,FLAGS[,TO])
7955 setgrent
7956 sethostent(STAYOPEN)
7957 setnetent(STAYOPEN)
7958 setpgrp(PID,PGRP)
7959 setpriority(WHICH,WHO,PRIORITY)
7960 setprotoent(STAYOPEN)
7961 setpwent
7962 setservent(STAYOPEN)
7963 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7964 shift[(ARRAY)]
7965 shmctl(ID,CMD,ARG)
7966 shmget(KEY,SIZE,FLAGS)
7967 shmread(ID,VAR,POS,SIZE)
7968 shmwrite(ID,STRING,POS,SIZE)
7969 shutdown(SOCKET,HOW)
7970 sin(EXPR)
7971 sleep[(EXPR)]
7972 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7973 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7974 sort [SUBROUTINE] (LIST)
7975 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7976 split[(/PATTERN/[,EXPR[,LIMIT]])]
7977 sprintf(FORMAT,LIST)
7978 sqrt(EXPR)
7979 srand(EXPR)
7980 stat(EXPR|FILEHANDLE|VAR)
7981 study[(SCALAR)]
7982 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7983 substr(EXPR,OFFSET[,LEN])
7984 symlink(OLDFILE,NEWFILE)
7985 syscall(LIST)
7986 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7987 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7988 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7989 tell[(FILEHANDLE)]
7990 telldir(DIRHANDLE)
7991 time
7992 times
7993 tr/SEARCHLIST/REPLACEMENTLIST/cds
7994 truncate(FILE|EXPR,LENGTH)
7995 umask[(EXPR)]
7996 undef[(EXPR)]
7997 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7998 unlink(LIST)
7999 unpack(TEMPLATE,EXPR)
8000 unshift(ARRAY,LIST)
8001 until (EXPR) { ... } EXPR until EXPR
8002 utime(LIST)
8003 values(%HASH)
8004 vec(EXPR,OFFSET,BITS)
8005 wait
8006 waitpid(PID,FLAGS)
8007 wantarray Returns true if the sub/eval is called in list context.
8008 warn(LIST)
8009 while (EXPR) { ... } EXPR while EXPR
8010 write[(EXPR|FILEHANDLE)]
8011 ... x ... Repeat string or array.
8012 x= ... Repetition assignment.
8013 y/SEARCHLIST/REPLACEMENTLIST/
8014 ... | ... Bitwise or.
8015 ... || ... Logical or.
8016 ~ ... Unary bitwise complement.
8017 #! OS interpreter indicator. If contains `perl', used for options, and -x.
8018 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
8019 CORE:: Prefix to access builtin function if imported sub obscures it.
8020 SUPER:: Prefix to lookup for a method in @ISA classes.
8021 DESTROY Shorthand for `sub DESTROY {...}'.
8022 ... EQ ... Obsolete synonym of `eq'.
8023 ... GE ... Obsolete synonym of `ge'.
8024 ... GT ... Obsolete synonym of `gt'.
8025 ... LE ... Obsolete synonym of `le'.
8026 ... LT ... Obsolete synonym of `lt'.
8027 ... NE ... Obsolete synonym of `ne'.
8028 abs [ EXPR ] absolute value
8029 ... and ... Low-precedence synonym for &&.
8030 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
8031 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
8032 chr Converts a number to char with the same ordinal.
8033 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
8034 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
8035 exists $HASH{KEY} True if the key exists.
8036 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
8037 formline PICTURE, LIST Backdoor into \"format\" processing.
8038 glob EXPR Synonym of <EXPR>.
8039 lc [ EXPR ] Returns lowercased EXPR.
8040 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
8041 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
8042 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
8043 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
8044 not ... Low-precedence synonym for ! - negation.
8045 ... or ... Low-precedence synonym for ||.
8046 pos STRING Set/Get end-position of the last match over this string, see \\G.
8047 quotemeta [ EXPR ] Quote regexp metacharacters.
8048 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
8049 readline FH Synonym of <FH>.
8050 readpipe CMD Synonym of `CMD`.
8051 ref [ EXPR ] Type of EXPR when dereferenced.
8052 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
8053 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
8054 tied Returns internal object for a tied data.
8055 uc [ EXPR ] Returns upcased EXPR.
8056 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
8057 untie VAR Unlink an object from a simple Perl variable.
8058 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
8059 ... xor ... Low-precedence synonym for exclusive or.
8060 prototype \\&SUB Returns prototype of the function given a reference.
8061 =head1 Top-level heading.
8062 =head2 Second-level heading.
8063 =head3 Third-level heading (is there such?).
8064 =over [ NUMBER ] Start list.
8065 =item [ TITLE ] Start new item in the list.
8066 =back End list.
8067 =cut Switch from POD to Perl.
8068 =pod Switch from Perl to POD.
8071 (defun cperl-switch-to-doc-buffer (&optional interactive)
8072 "Go to the perl documentation buffer and insert the documentation."
8073 (interactive "p")
8074 (let ((buf (get-buffer-create cperl-doc-buffer)))
8075 (if interactive
8076 (switch-to-buffer-other-window buf)
8077 (set-buffer buf))
8078 (if (= (buffer-size) 0)
8079 (progn
8080 (insert (documentation-property 'cperl-short-docs
8081 'variable-documentation))
8082 (setq buffer-read-only t)))))
8084 (defun cperl-beautify-regexp-piece (b e embed level)
8085 ;; b is before the starting delimiter, e before the ending
8086 ;; e should be a marker, may be changed, but remains "correct".
8087 ;; EMBED is nil iff we process the whole REx.
8088 ;; The REx is guaranteed to have //x
8089 ;; LEVEL shows how many levels deep to go
8090 ;; position at enter and at leave is not defined
8091 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
8092 (if (not embed)
8093 (goto-char (1+ b))
8094 (goto-char b)
8095 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
8096 (forward-char 2)
8097 (delete-char 1)
8098 (forward-char 1))
8099 ((looking-at "(\\?[^a-zA-Z]")
8100 (forward-char 3))
8101 ((looking-at "(\\?") ; (?i)
8102 (forward-char 2))
8104 (forward-char 1))))
8105 (setq c (if embed (current-indentation) (1- (current-column)))
8106 c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
8107 (or (looking-at "[ \t]*[\n#]")
8108 (progn
8109 (insert "\n")))
8110 (goto-char e)
8111 (beginning-of-line)
8112 (if (re-search-forward "[^ \t]" e t)
8113 (progn ; Something before the ending delimiter
8114 (goto-char e)
8115 (delete-horizontal-space)
8116 (insert "\n")
8117 (cperl-make-indent c)
8118 (set-marker e (point))))
8119 (goto-char b)
8120 (end-of-line 2)
8121 (while (< (point) (marker-position e))
8122 (beginning-of-line)
8123 (setq s (point)
8124 inline t)
8125 (skip-chars-forward " \t")
8126 (delete-region s (point))
8127 (cperl-make-indent c1)
8128 (while (and
8129 inline
8130 (looking-at
8131 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8132 "\\|" ; Embedded variable
8133 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8134 "\\|" ; $ ^
8135 "[$^]"
8136 "\\|" ; simple-code simple-code*?
8137 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8138 "\\|" ; Class
8139 "\\(\\[\\)" ; 6
8140 "\\|" ; Grouping
8141 "\\((\\(\\?\\)?\\)" ; 7 8
8142 "\\|" ; |
8143 "\\(|\\)"))) ; 9
8144 (goto-char (match-end 0))
8145 (setq spaces t)
8146 (cond ((match-beginning 1) ; Alphanum word + junk
8147 (forward-char -1))
8148 ((or (match-beginning 3) ; $ab[12]
8149 (and (match-beginning 5) ; X* X+ X{2,3}
8150 (eq (preceding-char) ?\{)))
8151 (forward-char -1)
8152 (forward-sexp 1))
8153 ((and ; [], already syntaxified
8154 (match-beginning 6)
8155 cperl-regexp-scan
8156 cperl-use-syntax-table-text-property)
8157 (forward-char -1)
8158 (forward-sexp 1)
8159 (or (eq (preceding-char) ?\])
8160 (error "[]-group not terminated"))
8161 (re-search-forward
8162 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8163 ((match-beginning 6) ; []
8164 (setq tmp (point))
8165 (if (looking-at "\\^?\\]")
8166 (goto-char (match-end 0)))
8167 ;; XXXX POSIX classes?!
8168 (while (and (not pos)
8169 (re-search-forward "\\[:\\|\\]" e t))
8170 (if (eq (preceding-char) ?:)
8171 (or (re-search-forward ":\\]" e t)
8172 (error "[:POSIX:]-group in []-group not terminated"))
8173 (setq pos t)))
8174 (or (eq (preceding-char) ?\])
8175 (error "[]-group not terminated"))
8176 (re-search-forward
8177 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8178 ((match-beginning 7) ; ()
8179 (goto-char (match-beginning 0))
8180 (setq pos (current-column))
8181 (or (eq pos c1)
8182 (progn
8183 (delete-horizontal-space)
8184 (insert "\n")
8185 (cperl-make-indent c1)))
8186 (setq tmp (point))
8187 (forward-sexp 1)
8188 ;; (or (forward-sexp 1)
8189 ;; (progn
8190 ;; (goto-char tmp)
8191 ;; (error "()-group not terminated")))
8192 (set-marker m (1- (point)))
8193 (set-marker m1 (point))
8194 (if (= level 1)
8195 (if (progn ; indent rigidly if multiline
8196 ;; In fact does not make a lot of sense, since
8197 ;; the starting position can be already lost due
8198 ;; to insertion of "\n" and " "
8199 (goto-char tmp)
8200 (search-forward "\n" m1 t))
8201 (indent-rigidly (point) m1 (- c1 pos)))
8202 (setq level (1- level))
8203 (cond
8204 ((not (match-beginning 8))
8205 (cperl-beautify-regexp-piece tmp m t level))
8206 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8208 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8209 (goto-char (+ 2 tmp))
8210 (forward-sexp 1)
8211 (cperl-beautify-regexp-piece (point) m t level))
8212 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8213 (goto-char (+ 3 tmp))
8214 (cperl-beautify-regexp-piece (point) m t level))
8216 (cperl-beautify-regexp-piece tmp m t level))))
8217 (goto-char m1)
8218 (cond ((looking-at "[*+?]\\??")
8219 (goto-char (match-end 0)))
8220 ((eq (following-char) ?\{)
8221 (forward-sexp 1)
8222 (if (eq (following-char) ?\?)
8223 (forward-char))))
8224 (skip-chars-forward " \t")
8225 (setq spaces nil)
8226 (if (looking-at "[#\n]")
8227 (progn
8228 (or (eolp) (indent-for-comment))
8229 (beginning-of-line 2))
8230 (delete-horizontal-space)
8231 (insert "\n"))
8232 (end-of-line)
8233 (setq inline nil))
8234 ((match-beginning 9) ; |
8235 (forward-char -1)
8236 (setq tmp (point))
8237 (beginning-of-line)
8238 (if (re-search-forward "[^ \t]" tmp t)
8239 (progn
8240 (goto-char tmp)
8241 (delete-horizontal-space)
8242 (insert "\n"))
8243 ;; first at line
8244 (delete-region (point) tmp))
8245 (cperl-make-indent c)
8246 (forward-char 1)
8247 (skip-chars-forward " \t")
8248 (setq spaces nil)
8249 (if (looking-at "[#\n]")
8250 (beginning-of-line 2)
8251 (delete-horizontal-space)
8252 (insert "\n"))
8253 (end-of-line)
8254 (setq inline nil)))
8255 (or (looking-at "[ \t\n]")
8256 (not spaces)
8257 (insert " "))
8258 (skip-chars-forward " \t"))
8259 (or (looking-at "[#\n]")
8260 (error "Unknown code `%s' in a regexp"
8261 (buffer-substring (point) (1+ (point)))))
8262 (and inline (end-of-line 2)))
8263 ;; Special-case the last line of group
8264 (if (and (>= (point) (marker-position e))
8265 (/= (current-indentation) c))
8266 (progn
8267 (beginning-of-line)
8268 (cperl-make-indent c)))))
8270 (defun cperl-make-regexp-x ()
8271 ;; Returns position of the start
8272 ;; XXX this is called too often! Need to cache the result!
8273 (save-excursion
8274 (or cperl-use-syntax-table-text-property
8275 (error "I need to have a regexp marked!"))
8276 ;; Find the start
8277 (if (looking-at "\\s|")
8278 nil ; good already
8279 (if (looking-at "\\([smy]\\|qr\\)\\s|")
8280 (forward-char 1)
8281 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8282 ;;(forward-char 1)
8283 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8284 (sub-p (eq (preceding-char) ?s)) s)
8285 (forward-sexp 1)
8286 (set-marker e (1- (point)))
8287 (setq delim (preceding-char))
8288 (if (and sub-p (eq delim (char-after (- (point) 2))))
8289 (error "Possible s/blah// - do not know how to deal with"))
8290 (if sub-p (forward-sexp 1))
8291 (if (looking-at "\\sw*x")
8292 (setq have-x t)
8293 (insert "x"))
8294 ;; Protect fragile " ", "#"
8295 (if have-x nil
8296 (goto-char (1+ b))
8297 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8298 (forward-char -1)
8299 (insert "\\")
8300 (forward-char 1)))
8301 b)))
8303 (defun cperl-beautify-regexp (&optional deep)
8304 "Do it. (Experimental, may change semantics, recheck the result.)
8305 We suppose that the regexp is scanned already."
8306 (interactive "P")
8307 (setq deep (if deep (prefix-numeric-value deep) -1))
8308 (save-excursion
8309 (goto-char (cperl-make-regexp-x))
8310 (let ((b (point)) (e (make-marker)))
8311 (forward-sexp 1)
8312 (set-marker e (1- (point)))
8313 (cperl-beautify-regexp-piece b e nil deep))))
8315 (defun cperl-regext-to-level-start ()
8316 "Goto start of an enclosing group in regexp.
8317 We suppose that the regexp is scanned already."
8318 (interactive)
8319 (let ((limit (cperl-make-regexp-x)) done)
8320 (while (not done)
8321 (or (eq (following-char) ?\()
8322 (search-backward "(" (1+ limit) t)
8323 (error "Cannot find `(' which starts a group"))
8324 (setq done
8325 (save-excursion
8326 (skip-chars-backward "\\")
8327 (looking-at "\\(\\\\\\\\\\)*(")))
8328 (or done (forward-char -1)))))
8330 (defun cperl-contract-level ()
8331 "Find an enclosing group in regexp and contract it.
8332 \(Experimental, may change semantics, recheck the result.)
8333 We suppose that the regexp is scanned already."
8334 (interactive)
8335 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8336 (cperl-regext-to-level-start)
8337 (let ((b (point)) (e (make-marker)) c)
8338 (forward-sexp 1)
8339 (set-marker e (1- (point)))
8340 (goto-char b)
8341 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8342 (cond
8343 ((match-beginning 1) ; #-comment
8344 (or c (setq c (current-indentation)))
8345 (beginning-of-line 2) ; Skip
8346 (cperl-make-indent c))
8348 (delete-char -1)
8349 (just-one-space))))))
8351 (defun cperl-contract-levels ()
8352 "Find an enclosing group in regexp and contract all the kids.
8353 \(Experimental, may change semantics, recheck the result.)
8354 We suppose that the regexp is scanned already."
8355 (interactive)
8356 (save-excursion
8357 (condition-case nil
8358 (cperl-regext-to-level-start)
8359 (error ; We are outside outermost group
8360 (goto-char (cperl-make-regexp-x))))
8361 (let ((b (point)) (e (make-marker)) s c)
8362 (forward-sexp 1)
8363 (set-marker e (1- (point)))
8364 (goto-char (1+ b))
8365 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8366 (cond
8367 ((match-beginning 1) ; Skip
8368 nil)
8369 (t ; Group
8370 (cperl-contract-level)))))))
8372 (defun cperl-beautify-level (&optional deep)
8373 "Find an enclosing group in regexp and beautify it.
8374 \(Experimental, may change semantics, recheck the result.)
8375 We suppose that the regexp is scanned already."
8376 (interactive "P")
8377 (setq deep (if deep (prefix-numeric-value deep) -1))
8378 (save-excursion
8379 (cperl-regext-to-level-start)
8380 (let ((b (point)) (e (make-marker)))
8381 (forward-sexp 1)
8382 (set-marker e (1- (point)))
8383 (cperl-beautify-regexp-piece b e nil deep))))
8385 (defun cperl-invert-if-unless-modifiers ()
8386 "Change `B if A;' into `if (A) {B}' etc if possible.
8387 \(Unfinished.)"
8388 (interactive) ;
8389 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8390 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8391 (and (= (char-syntax (preceding-char)) ?w)
8392 (forward-sexp -1))
8393 (setq pre-if (point))
8394 (cperl-backward-to-start-of-expr)
8395 (setq pre-B (point))
8396 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8397 (cperl-forward-to-end-of-expr)
8398 (setq post-A (point))
8399 (goto-char pre-if)
8400 (or (looking-at w-rex)
8401 ;; Find the position
8402 (progn (goto-char post-A)
8403 (while (and
8404 (not (looking-at w-rex))
8405 (> (point) pre-B))
8406 (forward-sexp -1))
8407 (setq pre-if (point))))
8408 (or (looking-at w-rex)
8409 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8410 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8411 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8412 ;; First, simple part: find code boundaries
8413 (forward-sexp 1)
8414 (setq post-if (point))
8415 (forward-sexp -2)
8416 (forward-sexp 1)
8417 (setq post-B (point))
8418 (cperl-backward-to-start-of-expr)
8419 (setq pre-B (point))
8420 (setq B (buffer-substring pre-B post-B))
8421 (goto-char pre-if)
8422 (forward-sexp 2)
8423 (forward-sexp -1)
8424 ;; May be after $, @, $# etc of a variable
8425 (skip-chars-backward "$@%#")
8426 (setq pre-A (point))
8427 (cperl-forward-to-end-of-expr)
8428 (setq post-A (point))
8429 (setq A (buffer-substring pre-A post-A))
8430 ;; Now modify (from end, to not break the stuff)
8431 (skip-chars-forward " \t;")
8432 (delete-region pre-A (point)) ; we move to pre-A
8433 (insert "\n" B ";\n}")
8434 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8435 (delete-region pre-if post-if)
8436 (delete-region pre-B post-B)
8437 (goto-char pre-B)
8438 (insert if-string " (" A ") {")
8439 (setq post-B (point))
8440 (if (looking-at "[ \t]+$")
8441 (delete-horizontal-space)
8442 (if (looking-at "[ \t]*#")
8443 (cperl-indent-for-comment)
8444 (just-one-space)))
8445 (forward-line 1)
8446 (if (looking-at "[ \t]*$")
8447 (progn ; delete line
8448 (delete-horizontal-space)
8449 (delete-region (point) (1+ (point)))))
8450 (cperl-indent-line)
8451 (goto-char (1- post-B))
8452 (forward-sexp 1)
8453 (cperl-indent-line)
8454 (goto-char pre-B)))
8456 (defun cperl-invert-if-unless ()
8457 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8458 If the cursor is not on the leading keyword of the BLOCK flavor of
8459 construct, will assume it is the STATEMENT flavor, so will try to find
8460 the appropriate statement modifier."
8461 (interactive)
8462 (and (= (char-syntax (preceding-char)) ?w)
8463 (forward-sexp -1))
8464 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8465 (let ((pre-if (point))
8466 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8467 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8468 (forward-sexp 2)
8469 (setq post-A (point))
8470 (forward-sexp -1)
8471 (setq pre-A (point))
8472 (setq is-block (and (eq (following-char) ?\( )
8473 (save-excursion
8474 (condition-case nil
8475 (progn
8476 (forward-sexp 2)
8477 (forward-sexp -1)
8478 (eq (following-char) ?\{ ))
8479 (error nil)))))
8480 (if is-block
8481 (progn
8482 (goto-char post-A)
8483 (forward-sexp 1)
8484 (setq post-B (point))
8485 (forward-sexp -1)
8486 (setq pre-B (point))
8487 (if (and (eq (following-char) ?\{ )
8488 (progn
8489 (cperl-backward-to-noncomment post-A)
8490 (eq (preceding-char) ?\) )))
8491 (if (condition-case nil
8492 (progn
8493 (goto-char post-B)
8494 (forward-sexp 1)
8495 (forward-sexp -1)
8496 (looking-at "\\<els\\(e\\|if\\)\\>"))
8497 (error nil))
8498 (error
8499 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8500 (goto-char (1- post-B))
8501 (cperl-backward-to-noncomment pre-B)
8502 (if (eq (preceding-char) ?\;)
8503 (forward-char -1))
8504 (setq end-B-code (point))
8505 (goto-char pre-B)
8506 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8507 (setq p (match-beginning 0)
8508 A (buffer-substring p (match-end 0))
8509 state (parse-partial-sexp pre-B p))
8510 (or (nth 3 state)
8511 (nth 4 state)
8512 (nth 5 state)
8513 (error "`%s' inside `%s' BLOCK" A if-string))
8514 (goto-char (match-end 0)))
8515 ;; Finally got it
8516 (goto-char (1+ pre-B))
8517 (skip-chars-forward " \t\n")
8518 (setq B (buffer-substring (point) end-B-code))
8519 (goto-char end-B-code)
8520 (or (looking-at ";?[ \t\n]*}")
8521 (progn
8522 (skip-chars-forward "; \t\n")
8523 (setq B-comment
8524 (buffer-substring (point) (1- post-B)))))
8525 (and (equal B "")
8526 (setq B "1"))
8527 (goto-char (1- post-A))
8528 (cperl-backward-to-noncomment pre-A)
8529 (or (looking-at "[ \t\n]*)")
8530 (goto-char (1- post-A)))
8531 (setq p (point))
8532 (goto-char (1+ pre-A))
8533 (skip-chars-forward " \t\n")
8534 (setq A (buffer-substring (point) p))
8535 (delete-region pre-B post-B)
8536 (delete-region pre-A post-A)
8537 (goto-char pre-if)
8538 (insert B " ")
8539 (and B-comment (insert B-comment " "))
8540 (just-one-space)
8541 (forward-word 1)
8542 (setq pre-A (point))
8543 (insert " " A ";")
8544 (delete-horizontal-space)
8545 (setq post-B (point))
8546 (if (looking-at "#")
8547 (indent-for-comment))
8548 (goto-char post-B)
8549 (forward-char -1)
8550 (delete-horizontal-space)
8551 (goto-char pre-A)
8552 (just-one-space)
8553 (goto-char pre-if)
8554 (setq pre-A (set-marker (make-marker) pre-A))
8555 (while (<= (point) (marker-position pre-A))
8556 (cperl-indent-line)
8557 (forward-line 1))
8558 (goto-char (marker-position pre-A))
8559 (if B-comment
8560 (progn
8561 (forward-line -1)
8562 (indent-for-comment)
8563 (goto-char (marker-position pre-A)))))
8564 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8565 ;; (error "`%s' not with an (EXPR)" if-string)
8566 (forward-sexp -1)
8567 (cperl-invert-if-unless-modifiers)))
8568 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8569 (cperl-invert-if-unless-modifiers)))
8571 ;;; By Anthony Foiani <afoiani@uswest.com>
8572 ;;; Getting help on modules in C-h f ?
8573 ;;; This is a modified version of `man'.
8574 ;;; Need to teach it how to lookup functions
8575 ;;;###autoload
8576 (defun cperl-perldoc (word)
8577 "Run `perldoc' on WORD."
8578 (interactive
8579 (list (let* ((default-entry (cperl-word-at-point))
8580 (input (read-string
8581 (format "perldoc entry%s: "
8582 (if (string= default-entry "")
8584 (format " (default %s)" default-entry))))))
8585 (if (string= input "")
8586 (if (string= default-entry "")
8587 (error "No perldoc args given")
8588 default-entry)
8589 input))))
8590 (require 'man)
8591 (let* ((case-fold-search nil)
8592 (is-func (and
8593 (string-match "^[a-z]+$" word)
8594 (string-match (concat "^" word "\\>")
8595 (documentation-property
8596 'cperl-short-docs
8597 'variable-documentation))))
8598 (manual-program (if is-func "perldoc -f" "perldoc")))
8599 (cond
8600 (cperl-xemacs-p
8601 (let ((Manual-program "perldoc")
8602 (Manual-switches (if is-func (list "-f"))))
8603 (manual-entry word)))
8605 (Man-getpage-in-background word)))))
8607 ;;;###autoload
8608 (defun cperl-perldoc-at-point ()
8609 "Run a `perldoc' on the word around point."
8610 (interactive)
8611 (cperl-perldoc (cperl-word-at-point)))
8613 (defcustom pod2man-program "pod2man"
8614 "*File name for `pod2man'."
8615 :type 'file
8616 :group 'cperl)
8618 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8619 (defun cperl-pod-to-manpage ()
8620 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8621 (interactive)
8622 (require 'man)
8623 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8624 (bufname (concat "Man " buffer-file-name))
8625 (buffer (generate-new-buffer bufname)))
8626 (save-excursion
8627 (set-buffer buffer)
8628 (let ((process-environment (copy-sequence process-environment)))
8629 ;; Prevent any attempt to use display terminal fanciness.
8630 (setenv "TERM" "dumb")
8631 (set-process-sentinel
8632 (start-process pod2man-program buffer "sh" "-c"
8633 (format (cperl-pod2man-build-command) pod2man-args))
8634 'Man-bgproc-sentinel)))))
8636 ;;; Updated version by him too
8637 (defun cperl-build-manpage ()
8638 "Create a virtual manpage in Emacs from the POD in the file."
8639 (interactive)
8640 (require 'man)
8641 (cond
8642 (cperl-xemacs-p
8643 (let ((Manual-program "perldoc"))
8644 (manual-entry buffer-file-name)))
8646 (let* ((manual-program "perldoc"))
8647 (Man-getpage-in-background buffer-file-name)))))
8649 (defun cperl-pod2man-build-command ()
8650 "Builds the entire background manpage and cleaning command."
8651 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8652 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8653 (while (and flist (car flist))
8654 (let ((pcom (car (car flist)))
8655 (pargs (cdr (car flist))))
8656 (setq command
8657 (concat command " | " pcom " "
8658 (mapconcat '(lambda (phrase)
8659 (if (not (stringp phrase))
8660 (error "Malformed Man-filter-list"))
8661 phrase)
8662 pargs " ")))
8663 (setq flist (cdr flist))))
8664 command))
8667 (defun cperl-next-interpolated-REx-1 ()
8668 "Move point to next REx which has interpolated parts without //o.
8669 Skips RExes consisting of one interpolated variable.
8671 Note that skipped RExen are not performance hits."
8672 (interactive "")
8673 (cperl-next-interpolated-REx 1))
8675 (defun cperl-next-interpolated-REx-0 ()
8676 "Move point to next REx which has interpolated parts without //o."
8677 (interactive "")
8678 (cperl-next-interpolated-REx 0))
8680 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8681 "Move point to next REx which has interpolated parts.
8682 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8683 point and the limit of search (default to point and end of buffer).
8685 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8686 semantic may be used as a numeric argument.
8688 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8689 a result of qr//, this is not a performance hit), t for the rest."
8690 (interactive "P")
8691 (if (numberp skip) (setq skip (list 0 skip)))
8692 (or beg (setq beg (point)))
8693 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8694 (let (pp)
8695 (and (eq (get-text-property beg 'syntax-type) 'string)
8696 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8697 (cperl-map-pods-heres
8698 (function (lambda (s e p)
8699 (if (memq (get-text-property s 'REx-interpolated) skip)
8701 (setq pp s)
8702 nil))) ; nil stops
8703 'REx-interpolated beg limit)
8704 (if pp (goto-char pp)
8705 (message "No more interpolated REx"))))
8707 ;;; Initial version contributed by Trey Belew
8708 (defun cperl-here-doc-spell (&optional beg end)
8709 "Spell-check HERE-documents in the Perl buffer.
8710 If a region is highlighted, restricts to the region."
8711 (interactive "")
8712 (cperl-pod-spell t beg end))
8714 (defun cperl-pod-spell (&optional do-heres beg end)
8715 "Spell-check POD documentation.
8716 If invoked with prefix argument, will do HERE-DOCs instead.
8717 If a region is highlighted, restricts to the region."
8718 (interactive "P")
8719 (save-excursion
8720 (let (beg end)
8721 (if (cperl-mark-active)
8722 (setq beg (min (mark) (point))
8723 end (max (mark) (point)))
8724 (setq beg (point-min)
8725 end (point-max)))
8726 (cperl-map-pods-heres (function
8727 (lambda (s e p)
8728 (if do-heres
8729 (setq e (save-excursion
8730 (goto-char e)
8731 (forward-line -1)
8732 (point))))
8733 (ispell-region s e)
8735 (if do-heres 'here-doc-group 'in-pod)
8736 beg end))))
8738 (defun cperl-map-pods-heres (func &optional prop s end)
8739 "Executes a function over regions of pods or here-documents.
8740 PROP is the text-property to search for; default to `in-pod'. Stop when
8741 function returns nil."
8742 (let (pos posend has-prop (cont t))
8743 (or prop (setq prop 'in-pod))
8744 (or s (setq s (point-min)))
8745 (or end (setq end (point-max)))
8746 (cperl-update-syntaxification end end)
8747 (save-excursion
8748 (goto-char (setq pos s))
8749 (while (and cont (< pos end))
8750 (setq has-prop (get-text-property pos prop))
8751 (setq posend (next-single-property-change pos prop nil end))
8752 (and has-prop
8753 (setq cont (funcall func pos posend prop)))
8754 (setq pos posend)))))
8756 ;;; Based on code by Masatake YAMATO:
8757 (defun cperl-get-here-doc-region (&optional pos pod)
8758 "Return HERE document region around the point.
8759 Return nil if the point is not in a HERE document region. If POD is non-nil,
8760 will return a POD section if point is in a POD section."
8761 (or pos (setq pos (point)))
8762 (cperl-update-syntaxification pos pos)
8763 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8764 (and pod
8765 (eq 'pod (get-text-property pos 'syntax-type))))
8766 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8767 (e (next-single-property-change pos 'syntax-type)))
8768 (cons b (or e (point-max))))))
8770 (defun cperl-narrow-to-here-doc (&optional pos)
8771 "Narrows editing region to the HERE-DOC at POS.
8772 POS defaults to the point."
8773 (interactive "d")
8774 (or pos (setq pos (point)))
8775 (let ((p (cperl-get-here-doc-region pos)))
8776 (or p (error "Not inside a HERE document"))
8777 (narrow-to-region (car p) (cdr p))
8778 (message
8779 "When you are finished with narrow editing, type C-x n w")))
8781 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8782 "Select the HERE-DOC (or POD section) at POS.
8783 POS defaults to the point."
8784 (interactive "d")
8785 (let ((p (cperl-get-here-doc-region pos t)))
8786 (if p
8787 (progn
8788 (goto-char (car p))
8789 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8790 (message "I do not think POS is in POD or a HERE-doc..."))))
8792 (defun cperl-facemenu-add-face-function (face end)
8793 "A callback to process user-initiated font-change requests.
8794 Translates `bold', `italic', and `bold-italic' requests to insertion of
8795 corresponding POD directives, and `underline' to C<> POD directive.
8797 Such requests are usually bound to M-o LETTER."
8798 (or (get-text-property (point) 'in-pod)
8799 (error "Faces can only be set within POD"))
8800 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8801 (cdr (or (assq face '((bold . "B<")
8802 (italic . "I<")
8803 (bold-italic . "B<I<")
8804 (underline . "C<")))
8805 (error "Face %s not configured for cperl-mode"
8806 face))))
8808 (defun cperl-time-fontification (&optional l step lim)
8809 "Times how long it takes to do incremental fontification in a region.
8810 L is the line to start at, STEP is the number of lines to skip when
8811 doing next incremental fontification, LIM is the maximal number of
8812 incremental fontification to perform. Messages are accumulated in
8813 *Messages* buffer.
8815 May be used for pinpointing which construct slows down buffer fontification:
8816 start with default arguments, then refine the slowdown regions."
8817 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8818 (or l (setq l 1))
8819 (or step (setq step 500))
8820 (or lim (setq lim 40))
8821 (let* ((timems (function (lambda ()
8822 (let ((tt (current-time)))
8823 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8824 (tt (funcall timems)) (c 0) delta tot)
8825 (goto-line l)
8826 (cperl-mode)
8827 (setq tot (- (- tt (setq tt (funcall timems)))))
8828 (message "cperl-mode at %s: %s" l tot)
8829 (while (and (< c lim) (not (eobp)))
8830 (forward-line step)
8831 (setq l (+ l step))
8832 (setq c (1+ c))
8833 (cperl-update-syntaxification (point) (point))
8834 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8835 (message "to %s:%6s,%7s" l delta tot))
8836 tot))
8838 (defun cperl-emulate-lazy-lock (&optional window-size)
8839 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8840 Start fontifying the buffer from the start (or end) using the given
8841 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8842 goes backwards; default is -50. This function is not CPerl-specific; it
8843 may be used to debug problems with delayed incremental fontification."
8844 (interactive
8845 "nSize of window for incremental fontification, negative goes backwards: ")
8846 (or window-size (setq window-size -50))
8847 (let ((pos (if (> window-size 0)
8848 (point-min)
8849 (point-max)))
8851 (goto-char pos)
8852 (normal-mode)
8853 ;; Why needed??? With older font-locks???
8854 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8855 (while (if (> window-size 0)
8856 (< pos (point-max))
8857 (> pos (point-min)))
8858 (setq p (progn
8859 (forward-line window-size)
8860 (point)))
8861 (font-lock-fontify-region (min p pos) (max p pos))
8862 (setq pos p))))
8865 (defun cperl-lazy-install ()) ; Avoid a warning
8866 (defun cperl-lazy-unstall ()) ; Avoid a warning
8868 (if (fboundp 'run-with-idle-timer)
8869 (progn
8870 (defvar cperl-help-shown nil
8871 "Non-nil means that the help was already shown now.")
8873 (defvar cperl-lazy-installed nil
8874 "Non-nil means that the lazy-help handlers are installed now.")
8876 (defun cperl-lazy-install ()
8877 "Switches on Auto-Help on Perl constructs (put in the message area).
8878 Delay of auto-help controlled by `cperl-lazy-help-time'."
8879 (interactive)
8880 (make-local-variable 'cperl-help-shown)
8881 (if (and (cperl-val 'cperl-lazy-help-time)
8882 (not cperl-lazy-installed))
8883 (progn
8884 (add-hook 'post-command-hook 'cperl-lazy-hook)
8885 (run-with-idle-timer
8886 (cperl-val 'cperl-lazy-help-time 1000000 5)
8888 'cperl-get-help-defer)
8889 (setq cperl-lazy-installed t))))
8891 (defun cperl-lazy-unstall ()
8892 "Switches off Auto-Help on Perl constructs (put in the message area).
8893 Delay of auto-help controlled by `cperl-lazy-help-time'."
8894 (interactive)
8895 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8896 (cancel-function-timers 'cperl-get-help-defer)
8897 (setq cperl-lazy-installed nil))
8899 (defun cperl-lazy-hook ()
8900 (setq cperl-help-shown nil))
8902 (defun cperl-get-help-defer ()
8903 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8904 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8905 (cperl-get-help)
8906 (setq cperl-help-shown t))))
8907 (cperl-lazy-install)))
8910 ;;; Plug for wrong font-lock:
8912 (defun cperl-font-lock-unfontify-region-function (beg end)
8913 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8914 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8915 before-change-functions after-change-functions
8916 deactivate-mark buffer-file-name buffer-file-truename)
8917 (remove-text-properties beg end '(face nil))
8918 (if (and (not modified) (buffer-modified-p))
8919 (set-buffer-modified-p nil))))
8921 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8922 "Extends the region to safe positions, then calls the default function.
8923 Newer `font-lock's can do it themselves.
8924 We unwind only as far as needed for fontification. Syntaxification may
8925 do extra unwind via `cperl-unwind-to-safe'."
8926 (save-excursion
8927 (goto-char beg)
8928 (while (and beg
8929 (progn
8930 (beginning-of-line)
8931 (eq (get-text-property (setq beg (point)) 'syntax-type)
8932 'multiline)))
8933 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8934 (goto-char beg)))
8935 (setq beg (point))
8936 (goto-char end)
8937 (while (and end
8938 (progn
8939 (or (bolp) (condition-case nil
8940 (forward-line 1)
8941 (error nil)))
8942 (eq (get-text-property (setq end (point)) 'syntax-type)
8943 'multiline)))
8944 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8945 (goto-char end))
8946 (setq end (point)))
8947 (font-lock-default-fontify-region beg end loudly))
8949 (defvar cperl-d-l nil)
8950 (defun cperl-fontify-syntaxically (end)
8951 ;; Some vars for debugging only
8952 ;; (message "Syntaxifying...")
8953 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8954 (istate (car cperl-syntax-state))
8955 start from-start edebug-backtrace-buffer)
8956 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8957 (progn
8958 (require 'edebug)
8959 (let ((f 'edebug-backtrace))
8960 (funcall f)))) ; Avoid compile-time warning
8961 (or cperl-syntax-done-to
8962 (setq cperl-syntax-done-to (point-min)
8963 from-start t))
8964 (setq start (if (and cperl-hook-after-change
8965 (not from-start))
8966 cperl-syntax-done-to ; Fontify without change; ignore start
8967 ;; Need to forget what is after `start'
8968 (min cperl-syntax-done-to (point))))
8969 (goto-char start)
8970 (beginning-of-line)
8971 (setq start (point))
8972 (and cperl-syntaxify-unwind
8973 (setq end (cperl-unwind-to-safe t end)
8974 start (point)))
8975 (and (> end start)
8976 (setq cperl-syntax-done-to start) ; In case what follows fails
8977 (cperl-find-pods-heres start end t nil t))
8978 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8979 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8980 dbg iend start end idone cperl-syntax-done-to
8981 istate (car cperl-syntax-state))) ; For debugging
8982 nil)) ; Do not iterate
8984 (defun cperl-fontify-update (end)
8985 (let ((pos (point-min)) prop posend)
8986 (setq end (point-max))
8987 (while (< pos end)
8988 (setq prop (get-text-property pos 'cperl-postpone)
8989 posend (next-single-property-change pos 'cperl-postpone nil end))
8990 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8991 (setq pos posend)))
8992 nil) ; Do not iterate
8994 (defun cperl-fontify-update-bad (end)
8995 ;; Since fontification happens with different region than syntaxification,
8996 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8997 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8998 (if prop
8999 (setq pos (or (cperl-beginning-of-property
9000 (cperl-1+ pos) 'cperl-postpone)
9001 (point-min))))
9002 (while (< pos end)
9003 (setq posend (next-single-property-change pos 'cperl-postpone))
9004 (and prop (put-text-property pos posend (car prop) (cdr prop)))
9005 (setq pos posend)
9006 (setq prop (get-text-property pos 'cperl-postpone))))
9007 nil) ; Do not iterate
9009 ;; Called when any modification is made to buffer text.
9010 (defun cperl-after-change-function (beg end old-len)
9011 ;; We should have been informed about changes by `font-lock'. Since it
9012 ;; does not inform as which calls are defered, do it ourselves
9013 (if cperl-syntax-done-to
9014 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
9016 (defun cperl-update-syntaxification (from to)
9017 (if (and cperl-use-syntax-table-text-property
9018 cperl-syntaxify-by-font-lock
9019 (or (null cperl-syntax-done-to)
9020 (< cperl-syntax-done-to to)))
9021 (progn
9022 (save-excursion
9023 (goto-char from)
9024 (cperl-fontify-syntaxically to)))))
9026 (defvar cperl-version
9027 (let ((v "Revision: 5.22"))
9028 (string-match ":\\s *\\([0-9.]+\\)" v)
9029 (substring v (match-beginning 1) (match-end 1)))
9030 "Version of IZ-supported CPerl package this file is based on.")
9032 (provide 'cperl-mode)
9034 ;;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
9035 ;;; cperl-mode.el ends here