]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cperl-mode.el
(c-before-font-lock-function): Correct a typo in the doc string.
[gnu-emacs] / lisp / progmodes / cperl-mode.el
1 ;;; cperl-mode.el --- Perl code editing commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 ;; 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: Ilya Zakharevich and Bob Olson
8 ;; Maintainer: Ilya Zakharevich <ilyaz@cpan.org>
9 ;; Keywords: languages, Perl
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
29
30 ;;; Commentary:
31
32 ;; You can either fine-tune the bells and whistles of this mode or
33 ;; bulk enable them by putting
34
35 ;; (setq cperl-hairy t)
36
37 ;; in your .emacs file. (Emacs rulers do not consider it politically
38 ;; correct to make whistles enabled by default.)
39
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'. <<<<<<
43
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.
47
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.
53
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:
59
60 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
61
62 ;;; Font lock bugs as of v4.32:
63
64 ;; The following kinds of Perl code erroneously start strings:
65 ;; \$` \$' \$"
66 ;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
67 ;; likewise with m, tr, y, q, qX instead of s
68
69 ;;; Code:
70 \f
71 (defvar vc-rcs-header)
72 (defvar vc-sccs-header)
73
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))))
121 (t
122 `(boundp ,arg))))
123 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
124 (cond ((fboundp 'make-face)
125 `(make-face (quote ,arg)))
126 (t
127 `(defvar ,arg (quote ,arg) ,descr))))
128 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
129 `(progn
130 (or (cperl-is-face (quote ,arg))
131 (cperl-make-face ,arg ,descr))
132 (or (boundp (quote ,arg)) ; We use unquoted variants too
133 (defvar ,arg (quote ,arg) ,descr))))
134 (if cperl-xemacs-p
135 (defmacro cperl-etags-snarf-tag (file line)
136 `(progn
137 (beginning-of-line 2)
138 (list ,file ,line)))
139 (defmacro cperl-etags-snarf-tag (file line)
140 `(etags-snarf-tag)))
141 (if cperl-xemacs-p
142 (defmacro cperl-etags-goto-tag-location (elt)
143 ;;(progn
144 ;; (switch-to-buffer (get-file-buffer (elt ,elt 0)))
145 ;; (set-buffer (get-file-buffer (elt ,elt 0)))
146 ;; Probably will not work due to some save-excursion???
147 ;; Or save-file-position?
148 ;; (message "Did I get to line %s?" (elt ,elt 1))
149 `(goto-line (string-to-int (elt ,elt 1))))
150 ;;)
151 (defmacro cperl-etags-goto-tag-location (elt)
152 `(etags-goto-tag-location ,elt))))
153
154 (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
155
156 (defvar cperl-can-font-lock
157 (or cperl-xemacs-p
158 (and (boundp 'emacs-major-version)
159 (or window-system
160 (> emacs-major-version 20)))))
161
162 (defun cperl-choose-color (&rest list)
163 (let (answer)
164 (while list
165 (or answer
166 (if (or (x-color-defined-p (car list))
167 (null (cdr list)))
168 (setq answer (car list))))
169 (setq list (cdr list)))
170 answer))
171
172 (defgroup cperl nil
173 "Major mode for editing Perl code."
174 :prefix "cperl-"
175 :group 'languages
176 :version "20.3")
177
178 (defgroup cperl-indentation-details nil
179 "Indentation."
180 :prefix "cperl-"
181 :group 'cperl)
182
183 (defgroup cperl-affected-by-hairy nil
184 "Variables affected by `cperl-hairy'."
185 :prefix "cperl-"
186 :group 'cperl)
187
188 (defgroup cperl-autoinsert-details nil
189 "Auto-insert tuneup."
190 :prefix "cperl-"
191 :group 'cperl)
192
193 (defgroup cperl-faces nil
194 "Fontification colors."
195 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
196 :prefix "cperl-"
197 :group 'cperl)
198
199 (defgroup cperl-speed nil
200 "Speed vs. validity tuneup."
201 :prefix "cperl-"
202 :group 'cperl)
203
204 (defgroup cperl-help-system nil
205 "Help system tuneup."
206 :prefix "cperl-"
207 :group 'cperl)
208
209 \f
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:
213
214 if ()
215 {
216 }
217
218 instead of:
219
220 if () {
221 }"
222 :type 'boolean
223 :group 'cperl-autoinsert-details)
224
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)
231
232 (defcustom cperl-indent-level 2
233 "*Indentation of CPerl statements with respect to containing block."
234 :type 'integer
235 :group 'cperl-indentation-details)
236
237 ;; Is is not unusual to put both things like perl-indent-level and
238 ;; cperl-indent-level in the local variable section of a file. If only
239 ;; one of perl-mode and cperl-mode is in use, a warning will be issued
240 ;; about the variable. Autoload these here, so that no warning is
241 ;; issued when using either perl-mode or cperl-mode.
242 ;;;###autoload(put 'cperl-indent-level 'safe-local-variable 'integerp)
243 ;;;###autoload(put 'cperl-brace-offset 'safe-local-variable 'integerp)
244 ;;;###autoload(put 'cperl-continued-brace-offset 'safe-local-variable 'integerp)
245 ;;;###autoload(put 'cperl-label-offset 'safe-local-variable 'integerp)
246 ;;;###autoload(put 'cperl-continued-statement-offset 'safe-local-variable 'integerp)
247 ;;;###autoload(put 'cperl-extra-newline-before-brace 'safe-local-variable 'booleanp)
248 ;;;###autoload(put 'cperl-merge-trailing-else 'safe-local-variable 'booleanp)
249
250 (defcustom cperl-lineup-step nil
251 "*`cperl-lineup' will always lineup at multiple of this number.
252 If nil, the value of `cperl-indent-level' will be used."
253 :type '(choice (const nil) integer)
254 :group 'cperl-indentation-details)
255
256 (defcustom cperl-brace-imaginary-offset 0
257 "*Imagined indentation of a Perl open brace that actually follows a statement.
258 An open brace following other text is treated as if it were this far
259 to the right of the start of its line."
260 :type 'integer
261 :group 'cperl-indentation-details)
262
263 (defcustom cperl-brace-offset 0
264 "*Extra indentation for braces, compared with other text in same context."
265 :type 'integer
266 :group 'cperl-indentation-details)
267 (defcustom cperl-label-offset -2
268 "*Offset of CPerl label lines relative to usual indentation."
269 :type 'integer
270 :group 'cperl-indentation-details)
271 (defcustom cperl-min-label-indent 1
272 "*Minimal offset of CPerl label lines."
273 :type 'integer
274 :group 'cperl-indentation-details)
275 (defcustom cperl-continued-statement-offset 2
276 "*Extra indent for lines not starting new statements."
277 :type 'integer
278 :group 'cperl-indentation-details)
279 (defcustom cperl-continued-brace-offset 0
280 "*Extra indent for substatements that start with open-braces.
281 This is in addition to cperl-continued-statement-offset."
282 :type 'integer
283 :group 'cperl-indentation-details)
284 (defcustom cperl-close-paren-offset -1
285 "*Extra indent for substatements that start with close-parenthesis."
286 :type 'integer
287 :group 'cperl-indentation-details)
288
289 (defcustom cperl-indent-wrt-brace t
290 "*Non-nil means indent statements in if/etc block relative brace, not if/etc.
291 Versions 5.2 ... 5.20 behaved as if this were `nil'."
292 :type 'boolean
293 :group 'cperl-indentation-details)
294
295 (defcustom cperl-auto-newline nil
296 "*Non-nil means automatically newline before and after braces,
297 and after colons and semicolons, inserted in CPerl code. The following
298 \\[cperl-electric-backspace] will remove the inserted whitespace.
299 Insertion after colons requires both this variable and
300 `cperl-auto-newline-after-colon' set."
301 :type 'boolean
302 :group 'cperl-autoinsert-details)
303
304 (defcustom cperl-autoindent-on-semi nil
305 "*Non-nil means automatically indent after insertion of (semi)colon.
306 Active if `cperl-auto-newline' is false."
307 :type 'boolean
308 :group 'cperl-autoinsert-details)
309
310 (defcustom cperl-auto-newline-after-colon nil
311 "*Non-nil means automatically newline even after colons.
312 Subject to `cperl-auto-newline' setting."
313 :type 'boolean
314 :group 'cperl-autoinsert-details)
315
316 (defcustom cperl-tab-always-indent t
317 "*Non-nil means TAB in CPerl mode should always reindent the current line,
318 regardless of where in the line point is when the TAB command is used."
319 :type 'boolean
320 :group 'cperl-indentation-details)
321
322 (defcustom cperl-font-lock nil
323 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
324 Can be overwritten by `cperl-hairy' if nil."
325 :type '(choice (const null) boolean)
326 :group 'cperl-affected-by-hairy)
327
328 (defcustom cperl-electric-lbrace-space nil
329 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
330 Can be overwritten by `cperl-hairy' if nil."
331 :type '(choice (const null) boolean)
332 :group 'cperl-affected-by-hairy)
333
334 (defcustom cperl-electric-parens-string "({[]})<"
335 "*String of parentheses that should be electric in CPerl.
336 Closing ones are electric only if the region is highlighted."
337 :type 'string
338 :group 'cperl-affected-by-hairy)
339
340 (defcustom cperl-electric-parens nil
341 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
342 Can be overwritten by `cperl-hairy' if nil."
343 :type '(choice (const null) boolean)
344 :group 'cperl-affected-by-hairy)
345
346 (defvar zmacs-regions) ; Avoid warning
347
348 (defcustom cperl-electric-parens-mark
349 (and window-system
350 (or (and (boundp 'transient-mark-mode) ; For Emacs
351 transient-mark-mode)
352 (and (boundp 'zmacs-regions) ; For XEmacs
353 zmacs-regions)))
354 "*Not-nil means that electric parens look for active mark.
355 Default is yes if there is visual feedback on mark."
356 :type 'boolean
357 :group 'cperl-autoinsert-details)
358
359 (defcustom cperl-electric-linefeed nil
360 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
361 In any case these two mean plain and hairy linefeeds together.
362 Can be overwritten by `cperl-hairy' if nil."
363 :type '(choice (const null) boolean)
364 :group 'cperl-affected-by-hairy)
365
366 (defcustom cperl-electric-keywords nil
367 "*Not-nil (and non-null) means keywords are electric in CPerl.
368 Can be overwritten by `cperl-hairy' if nil.
369
370 Uses `abbrev-mode' to do the expansion. If you want to use your
371 own abbrevs in cperl-mode, but do not want keywords to be
372 electric, you must redefine `cperl-mode-abbrev-table': do
373 \\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
374 that paragraph, delete the words that appear at the ends of lines and
375 that begin with \"cperl-electric\".
376 "
377 :type '(choice (const null) boolean)
378 :group 'cperl-affected-by-hairy)
379
380 (defcustom cperl-electric-backspace-untabify t
381 "*Not-nil means electric-backspace will untabify in CPerl."
382 :type 'boolean
383 :group 'cperl-autoinsert-details)
384
385 (defcustom cperl-hairy nil
386 "*Not-nil means most of the bells and whistles are enabled in CPerl.
387 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
388 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
389 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
390 `cperl-lazy-help-time'."
391 :type 'boolean
392 :group 'cperl-affected-by-hairy)
393
394 (defcustom cperl-comment-column 32
395 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
396 :type 'integer
397 :group 'cperl-indentation-details)
398
399 (defcustom cperl-indent-comment-at-column-0 nil
400 "*Non-nil means that comment started at column 0 should be indentable."
401 :type 'boolean
402 :group 'cperl-indentation-details)
403
404 (defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
405 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
406 :type '(repeat string)
407 :group 'cperl)
408
409 (defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
410 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
411 :type '(repeat string)
412 :group 'cperl)
413
414 ;; This became obsolete...
415 (defvar cperl-vc-header-alist nil)
416 (make-obsolete-variable
417 'cperl-vc-header-alist
418 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead.")
419
420 (defcustom cperl-clobber-mode-lists
421 (not
422 (and
423 (boundp 'interpreter-mode-alist)
424 (assoc "miniperl" interpreter-mode-alist)
425 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
426 "*Whether to install us into `interpreter-' and `extension' mode lists."
427 :type 'boolean
428 :group 'cperl)
429
430 (defcustom cperl-info-on-command-no-prompt nil
431 "*Not-nil (and non-null) means not to prompt on C-h f.
432 The opposite behavior is always available if prefixed with C-c.
433 Can be overwritten by `cperl-hairy' if nil."
434 :type '(choice (const null) boolean)
435 :group 'cperl-affected-by-hairy)
436
437 (defcustom cperl-clobber-lisp-bindings nil
438 "*Not-nil (and non-null) means not overwrite C-h f.
439 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
440 Can be overwritten by `cperl-hairy' if nil."
441 :type '(choice (const null) boolean)
442 :group 'cperl-affected-by-hairy)
443
444 (defcustom cperl-lazy-help-time nil
445 "*Not-nil (and non-null) means to show lazy help after given idle time.
446 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
447 :type '(choice (const null) (const nil) integer)
448 :group 'cperl-affected-by-hairy)
449
450 (defcustom cperl-pod-face 'font-lock-comment-face
451 "*Face for POD highlighting."
452 :type 'face
453 :group 'cperl-faces)
454
455 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
456 "*Face for POD highlighting.
457 Font for POD headers."
458 :type 'face
459 :group 'cperl-faces)
460
461 (defcustom cperl-here-face 'font-lock-string-face
462 "*Face for here-docs highlighting."
463 :type 'face
464 :group 'cperl-faces)
465
466 ;;; Some double-evaluation happened with font-locks... Needed with 21.2...
467 (defvar cperl-singly-quote-face cperl-xemacs-p)
468
469 (defcustom cperl-invalid-face 'underline
470 "*Face for highlighting trailing whitespace."
471 :type 'face
472 :version "21.1"
473 :group 'cperl-faces)
474
475 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
476 "*Not-nil after evaluation means to highlight POD and here-docs sections."
477 :type 'boolean
478 :group 'cperl-faces)
479
480 (defcustom cperl-fontify-m-as-s t
481 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
482 :type 'boolean
483 :group 'cperl-faces)
484
485 (defcustom cperl-highlight-variables-indiscriminately nil
486 "*Non-nil means perform additional highlighting on variables.
487 Currently only changes how scalar variables are highlighted.
488 Note that that variable is only read at initialization time for
489 the variable `cperl-font-lock-keywords-2', so changing it after you've
490 entered CPerl mode the first time will have no effect."
491 :type 'boolean
492 :group 'cperl)
493
494 (defcustom cperl-pod-here-scan t
495 "*Not-nil means look for POD and here-docs sections during startup.
496 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
497 :type 'boolean
498 :group 'cperl-speed)
499
500 (defcustom cperl-regexp-scan t
501 "*Not-nil means make marking of regular expression more thorough.
502 Effective only with `cperl-pod-here-scan'."
503 :type 'boolean
504 :group 'cperl-speed)
505
506 (defcustom cperl-hook-after-change t
507 "*Not-nil means install hook to know which regions of buffer are changed.
508 May significantly speed up delayed fontification. Changes take effect
509 after reload."
510 :type 'boolean
511 :group 'cperl-speed)
512
513 (defcustom cperl-imenu-addback nil
514 "*Not-nil means add backreferences to generated `imenu's.
515 May require patched `imenu' and `imenu-go'. Obsolete."
516 :type 'boolean
517 :group 'cperl-help-system)
518
519 (defcustom cperl-max-help-size 66
520 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
521 :type '(choice integer (const nil))
522 :group 'cperl-help-system)
523
524 (defcustom cperl-shrink-wrap-info-frame t
525 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
526 :type 'boolean
527 :group 'cperl-help-system)
528
529 (defcustom cperl-info-page "perl"
530 "*Name of the info page containing perl docs.
531 Older version of this page was called `perl5', newer `perl'."
532 :type 'string
533 :group 'cperl-help-system)
534
535 (defcustom cperl-use-syntax-table-text-property
536 (boundp 'parse-sexp-lookup-properties)
537 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
538 :type 'boolean
539 :group 'cperl-speed)
540
541 (defcustom cperl-use-syntax-table-text-property-for-tags
542 cperl-use-syntax-table-text-property
543 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
544 :type 'boolean
545 :group 'cperl-speed)
546
547 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
548 "*Regexp to match files to scan when generating TAGS."
549 :type 'regexp
550 :group 'cperl)
551
552 (defcustom cperl-noscan-files-regexp
553 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
554 "*Regexp to match files/dirs to skip when generating TAGS."
555 :type 'regexp
556 :group 'cperl)
557
558 (defcustom cperl-regexp-indent-step nil
559 "*Indentation used when beautifying regexps.
560 If nil, the value of `cperl-indent-level' will be used."
561 :type '(choice integer (const nil))
562 :group 'cperl-indentation-details)
563
564 (defcustom cperl-indent-left-aligned-comments t
565 "*Non-nil means that the comment starting in leftmost column should indent."
566 :type 'boolean
567 :group 'cperl-indentation-details)
568
569 (defcustom cperl-under-as-char nil
570 "*Non-nil means that the _ (underline) should be treated as word char."
571 :type 'boolean
572 :group 'cperl)
573
574 (defcustom cperl-extra-perl-args ""
575 "*Extra arguments to use when starting Perl.
576 Currently used with `cperl-check-syntax' only."
577 :type 'string
578 :group 'cperl)
579
580 (defcustom cperl-message-electric-keyword t
581 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
582 :type 'boolean
583 :group 'cperl-help-system)
584
585 (defcustom cperl-indent-region-fix-constructs 1
586 "*Amount of space to insert between `}' and `else' or `elsif'
587 in `cperl-indent-region'. Set to nil to leave as is. Values other
588 than 1 and nil will probably not work."
589 :type '(choice (const nil) (const 1))
590 :group 'cperl-indentation-details)
591
592 (defcustom cperl-break-one-line-blocks-when-indent t
593 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
594 need to be reformatted into multiline ones when indenting a region."
595 :type 'boolean
596 :group 'cperl-indentation-details)
597
598 (defcustom cperl-fix-hanging-brace-when-indent t
599 "*Non-nil means that BLOCK-end `}' may be put on a separate line
600 when indenting a region.
601 Braces followed by else/elsif/while/until are excepted."
602 :type 'boolean
603 :group 'cperl-indentation-details)
604
605 (defcustom cperl-merge-trailing-else t
606 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
607 may be merged to be on the same line when indenting a region."
608 :type 'boolean
609 :group 'cperl-indentation-details)
610
611 (defcustom cperl-indent-parens-as-block nil
612 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
613 but for trailing \",\" inside the group, which won't increase indentation.
614 One should tune up `cperl-close-paren-offset' as well."
615 :type 'boolean
616 :group 'cperl-indentation-details)
617
618 (defcustom cperl-syntaxify-by-font-lock
619 (and cperl-can-font-lock
620 (boundp 'parse-sexp-lookup-properties))
621 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
622 :type '(choice (const message) boolean)
623 :group 'cperl-speed)
624
625 (defcustom cperl-syntaxify-unwind
626 t
627 "*Non-nil means that CPerl unwinds to a start of a long construction
628 when syntaxifying a chunk of buffer."
629 :type 'boolean
630 :group 'cperl-speed)
631
632 (defcustom cperl-syntaxify-for-menu
633 t
634 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
635 This way enabling/disabling of menu items is more correct."
636 :type 'boolean
637 :group 'cperl-speed)
638
639 (defcustom cperl-ps-print-face-properties
640 '((font-lock-keyword-face nil nil bold shadow)
641 (font-lock-variable-name-face nil nil bold)
642 (font-lock-function-name-face nil nil bold italic box)
643 (font-lock-constant-face nil "LightGray" bold)
644 (cperl-array-face nil "LightGray" bold underline)
645 (cperl-hash-face nil "LightGray" bold italic underline)
646 (font-lock-comment-face nil "LightGray" italic)
647 (font-lock-string-face nil nil italic underline)
648 (cperl-nonoverridable-face nil nil italic underline)
649 (font-lock-type-face nil nil underline)
650 (font-lock-warning-face nil "LightGray" bold italic box)
651 (underline nil "LightGray" strikeout))
652 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
653 :type '(repeat (cons symbol
654 (cons (choice (const nil) string)
655 (cons (choice (const nil) string)
656 (repeat symbol)))))
657 :group 'cperl-faces)
658
659 (defvar cperl-dark-background
660 (cperl-choose-color "navy" "os2blue" "darkgreen"))
661 (defvar cperl-dark-foreground
662 (cperl-choose-color "orchid1" "orange"))
663
664 (defface cperl-nonoverridable-face
665 `((((class grayscale) (background light))
666 (:background "Gray90" :slant italic :underline t))
667 (((class grayscale) (background dark))
668 (:foreground "Gray80" :slant italic :underline t :weight bold))
669 (((class color) (background light))
670 (:foreground "chartreuse3"))
671 (((class color) (background dark))
672 (:foreground ,cperl-dark-foreground))
673 (t (:weight bold :underline t)))
674 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
675 :group 'cperl-faces)
676
677 (defface cperl-array-face
678 `((((class grayscale) (background light))
679 (:background "Gray90" :weight bold))
680 (((class grayscale) (background dark))
681 (:foreground "Gray80" :weight bold))
682 (((class color) (background light))
683 (:foreground "Blue" :background "lightyellow2" :weight bold))
684 (((class color) (background dark))
685 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
686 (t (:weight bold)))
687 "Font Lock mode face used to highlight array names."
688 :group 'cperl-faces)
689
690 (defface cperl-hash-face
691 `((((class grayscale) (background light))
692 (:background "Gray90" :weight bold :slant italic))
693 (((class grayscale) (background dark))
694 (:foreground "Gray80" :weight bold :slant italic))
695 (((class color) (background light))
696 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
697 (((class color) (background dark))
698 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
699 (t (:weight bold :slant italic)))
700 "Font Lock mode face used to highlight hash names."
701 :group 'cperl-faces)
702
703 \f
704
705 ;;; Short extra-docs.
706
707 (defvar cperl-tips 'please-ignore-this-line
708 "Get maybe newer version of this package from
709 http://ilyaz.org/software/emacs
710 Subdirectory `cperl-mode' may contain yet newer development releases and/or
711 patches to related files.
712
713 For best results apply to an older Emacs the patches from
714 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
715 \(this upgrades syntax-parsing abilities of Emacsen v19.34 and
716 v20.2 up to the level of Emacs v20.3 - a must for a good Perl
717 mode.) As of beginning of 2003, XEmacs may provide a similar ability.
718
719 Get support packages choose-color.el (or font-lock-extra.el before
720 19.30), imenu-go.el from the same place. \(Look for other files there
721 too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
722 later you should use choose-color.el *instead* of font-lock-extra.el
723 \(and you will not get smart highlighting in C :-().
724
725 Note that to enable Compile choices in the menu you need to install
726 mode-compile.el.
727
728 If your Emacs does not default to `cperl-mode' on Perl files, and you
729 want it to: put the following into your .emacs file:
730
731 (defalias 'perl-mode 'cperl-mode)
732
733 Get perl5-info from
734 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
735 Also, one can generate a newer documentation running `pod2texi' converter
736 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
737
738 If you use imenu-go, run imenu on perl5-info buffer (you can do it
739 from Perl menu). If many files are related, generate TAGS files from
740 Tools/Tags submenu in Perl menu.
741
742 If some class structure is too complicated, use Tools/Hierarchy-view
743 from Perl menu, or hierarchic view of imenu. The second one uses the
744 current buffer only, the first one requires generation of TAGS from
745 Perl/Tools/Tags menu beforehand.
746
747 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
748
749 Switch auto-help on/off with Perl/Tools/Auto-help.
750
751 Though with contemporary Emaxen CPerl mode should maintain the correct
752 parsing of Perl even when editing, sometimes it may be lost. Fix this by
753
754 \\[normal-mode]
755
756 In cases of more severe confusion sometimes it is helpful to do
757
758 \\[load-library] cperl-mode RET
759 \\[normal-mode]
760
761 Before reporting (non-)problems look in the problem section of online
762 micro-docs on what I know about CPerl problems.")
763
764 (defvar cperl-problems 'please-ignore-this-line
765 "Description of problems in CPerl mode.
766 Some faces will not be shown on some versions of Emacs unless you
767 install choose-color.el, available from
768 http://ilyaz.org/software/emacs
769
770 `fill-paragraph' on a comment may leave the point behind the
771 paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
772 to detect it and bulk out).
773
774 See documentation of a variable `cperl-problems-old-emaxen' for the
775 problems which disappear if you upgrade Emacs to a reasonably new
776 version (20.3 for Emacs, and those of 2004 for XEmacs).")
777
778 (defvar cperl-problems-old-emaxen 'please-ignore-this-line
779 "Description of problems in CPerl mode specific for older Emacs versions.
780
781 Emacs had a _very_ restricted syntax parsing engine until version
782 20.1. Most problems below are corrected starting from this version of
783 Emacs, and all of them should be fixed in version 20.3. (Or apply
784 patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
785 this respect (until 2003).
786
787 Note that even with newer Emacsen in some very rare cases the details
788 of interaction of `font-lock' and syntaxification may be not cleaned
789 up yet. You may get slightly different colors basing on the order of
790 fontification and syntaxification. Say, the initial faces is correct,
791 but editing the buffer breaks this.
792
793 Even with older Emacsen CPerl mode tries to corrects some Emacs
794 misunderstandings, however, for efficiency reasons the degree of
795 correction is different for different operations. The partially
796 corrected problems are: POD sections, here-documents, regexps. The
797 operations are: highlighting, indentation, electric keywords, electric
798 braces.
799
800 This may be confusing, since the regexp s#//#/#\; may be highlighted
801 as a comment, but it will be recognized as a regexp by the indentation
802 code. Or the opposite case, when a POD section is highlighted, but
803 may break the indentation of the following code (though indentation
804 should work if the balance of delimiters is not broken by POD).
805
806 The main trick (to make $ a \"backslash\") makes constructions like
807 ${aaa} look like unbalanced braces. The only trick I can think of is
808 to insert it as $ {aaa} (valid in perl5, not in perl4).
809
810 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
811 as /($|\\s)/. Note that such a transposition is not always possible.
812
813 The solution is to upgrade your Emacs or patch an older one. Note
814 that Emacs 20.2 has some bugs related to `syntax-table' text
815 properties. Patches are available on the main CPerl download site,
816 and on CPAN.
817
818 If these bugs cannot be fixed on your machine (say, you have an inferior
819 environment and cannot recompile), you may still disable all the fancy stuff
820 via `cperl-use-syntax-table-text-property'.")
821
822 (defvar cperl-praise 'please-ignore-this-line
823 "Advantages of CPerl mode.
824
825 0) It uses the newest `syntax-table' property ;-);
826
827 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
828 mode - but the latter number may have improved too in last years) even
829 with old Emaxen which do not support `syntax-table' property.
830
831 When using `syntax-table' property for syntax assist hints, it should
832 handle 99.995% of lines correct - or somesuch. It automatically
833 updates syntax assist hints when you edit your script.
834
835 2) It is generally believed to be \"the most user-friendly Emacs
836 package\" whatever it may mean (I doubt that the people who say similar
837 things tried _all_ the rest of Emacs ;-), but this was not a lonely
838 voice);
839
840 3) Everything is customizable, one-by-one or in a big sweep;
841
842 4) It has many easily-accessable \"tools\":
843 a) Can run program, check syntax, start debugger;
844 b) Can lineup vertically \"middles\" of rows, like `=' in
845 a = b;
846 cc = d;
847 c) Can insert spaces where this impoves readability (in one
848 interactive sweep over the buffer);
849 d) Has support for imenu, including:
850 1) Separate unordered list of \"interesting places\";
851 2) Separate TOC of POD sections;
852 3) Separate list of packages;
853 4) Hierarchical view of methods in (sub)packages;
854 5) and functions (by the full name - with package);
855 e) Has an interface to INFO docs for Perl; The interface is
856 very flexible, including shrink-wrapping of
857 documentation buffer/frame;
858 f) Has a builtin list of one-line explanations for perl constructs.
859 g) Can show these explanations if you stay long enough at the
860 corresponding place (or on demand);
861 h) Has an enhanced fontification (using 3 or 4 additional faces
862 comparing to font-lock - basically, different
863 namespaces in Perl have different colors);
864 i) Can construct TAGS basing on its knowledge of Perl syntax,
865 the standard menu has 6 different way to generate
866 TAGS (if \"by directory\", .xs files - with C-language
867 bindings - are included in the scan);
868 j) Can build a hierarchical view of classes (via imenu) basing
869 on generated TAGS file;
870 k) Has electric parentheses, electric newlines, uses Abbrev
871 for electric logical constructs
872 while () {}
873 with different styles of expansion (context sensitive
874 to be not so bothering). Electric parentheses behave
875 \"as they should\" in a presence of a visible region.
876 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
877 m) Can convert from
878 if (A) { B }
879 to
880 B if A;
881
882 n) Highlights (by user-choice) either 3-delimiters constructs
883 (such as tr/a/b/), or regular expressions and `y/tr';
884 o) Highlights trailing whitespace;
885 p) Is able to manipulate Perl Regular Expressions to ease
886 conversion to a more readable form.
887 q) Can ispell POD sections and HERE-DOCs.
888 r) Understands comments and character classes inside regular
889 expressions; can find matching () and [] in a regular expression.
890 s) Allows indentation of //x-style regular expressions;
891 t) Highlights different symbols in regular expressions according
892 to their function; much less problems with backslashitis;
893 u) Allows to find regular expressions which contain interpolated parts.
894
895 5) The indentation engine was very smart, but most of tricks may be
896 not needed anymore with the support for `syntax-table' property. Has
897 progress indicator for indentation (with `imenu' loaded).
898
899 6) Indent-region improves inline-comments as well; also corrects
900 whitespace *inside* the conditional/loop constructs.
901
902 7) Fill-paragraph correctly handles multi-line comments;
903
904 8) Can switch to different indentation styles by one command, and restore
905 the settings present before the switch.
906
907 9) When doing indentation of control constructs, may correct
908 line-breaks/spacing between elements of the construct.
909
910 10) Uses a linear-time algorith for indentation of regions (on Emaxen with
911 capable syntax engines).
912
913 11) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
914 ")
915
916 (defvar cperl-speed 'please-ignore-this-line
917 "This is an incomplete compendium of what is available in other parts
918 of CPerl documentation. (Please inform me if I skept anything.)
919
920 There is a perception that CPerl is slower than alternatives. This part
921 of documentation is designed to overcome this misconception.
922
923 *By default* CPerl tries to enable the most comfortable settings.
924 From most points of view, correctly working package is infinitely more
925 comfortable than a non-correctly working one, thus by default CPerl
926 prefers correctness over speed. Below is the guide how to change
927 settings if your preferences are different.
928
929 A) Speed of loading the file. When loading file, CPerl may perform a
930 scan which indicates places which cannot be parsed by primitive Emacs
931 syntax-parsing routines, and marks them up so that either
932
933 A1) CPerl may work around these deficiencies (for big chunks, mostly
934 PODs and HERE-documents), or
935 A2) On capable Emaxen CPerl will use improved syntax-handlings
936 which reads mark-up hints directly.
937
938 The scan in case A2 is much more comprehensive, thus may be slower.
939
940 User can disable syntax-engine-helping scan of A2 by setting
941 `cperl-use-syntax-table-text-property'
942 variable to nil (if it is set to t).
943
944 One can disable the scan altogether (both A1 and A2) by setting
945 `cperl-pod-here-scan'
946 to nil.
947
948 B) Speed of editing operations.
949
950 One can add a (minor) speedup to editing operations by setting
951 `cperl-use-syntax-table-text-property'
952 variable to nil (if it is set to t). This will disable
953 syntax-engine-helping scan, thus will make many more Perl
954 constructs be wrongly recognized by CPerl, thus may lead to
955 wrongly matched parentheses, wrong indentation, etc.
956
957 One can unset `cperl-syntaxify-unwind'. This might speed up editing
958 of, say, long POD sections.")
959
960 (defvar cperl-tips-faces 'please-ignore-this-line
961 "CPerl mode uses following faces for highlighting:
962
963 `cperl-array-face' Array names
964 `cperl-hash-face' Hash names
965 `font-lock-comment-face' Comments, PODs and whatever is considered
966 syntaxically to be not code
967 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
968 2-arg operators s/y/tr/ or of RExen,
969 `font-lock-warning-face' Special-cased m// and s//foo/,
970 `font-lock-function-name-face' _ as a target of a file tests, file tests,
971 subroutine names at the moment of definition
972 (except those conflicting with Perl operators),
973 package names (when recognized), format names
974 `font-lock-keyword-face' Control flow switch constructs, declarators
975 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
976 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
977 literal parts and the terminator of formats
978 and whatever is syntaxically considered
979 as string literals
980 `font-lock-type-face' Overridable keywords
981 `font-lock-variable-name-face' Variable declarations, indirect array and
982 hash names, POD headers/item names
983 `cperl-invalid' Trailing whitespace
984
985 Note that in several situations the highlighting tries to inform about
986 possible confusion, such as different colors for function names in
987 declarations depending on what they (do not) override, or special cases
988 m// and s/// which do not do what one would expect them to do.
989
990 Help with best setup of these faces for printout requested (for each of
991 the faces: please specify bold, italic, underline, shadow and box.)
992
993 In regular expressions (except character classes):
994 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
995 `font-lock-constant-face': Delimiters
996 `font-lock-warning-face' Special-cased m// and s//foo/,
997 Mismatched closing delimiters, parens
998 we couldn't match, misplaced quantifiers,
999 unrecognized escape sequences
1000 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
1001 `font-lock-type-face' POSIX classes inside charclasses,
1002 escape sequences with arguments (\x \23 \p \N)
1003 and others match-a-char escape sequences
1004 `font-lock-keyword-face' Capturing parens, and |
1005 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
1006 `font-lock-builtin-face' \"Remaining\" 0-length constructs, executable
1007 parts of a REx, not-capturing parens
1008 `font-lock-variable-name-face' Interpolated constructs, embedded code
1009 `font-lock-comment-face' Embedded comments
1010
1011 ")
1012
1013 \f
1014
1015 ;;; Portability stuff:
1016
1017 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
1018 `(define-key cperl-mode-map
1019 ,(if xemacs-key
1020 `(if cperl-xemacs-p ,xemacs-key ,emacs-key)
1021 emacs-key)
1022 ,definition))
1023
1024 (defvar cperl-del-back-ch
1025 (car (append (where-is-internal 'delete-backward-char)
1026 (where-is-internal 'backward-delete-char-untabify)))
1027 "Character generated by key bound to `delete-backward-char'.")
1028
1029 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
1030 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1031
1032 (defun cperl-mark-active () (mark)) ; Avoid undefined warning
1033 (if cperl-xemacs-p
1034 (progn
1035 ;; "Active regions" are on: use region only if active
1036 ;; "Active regions" are off: use region unconditionally
1037 (defun cperl-use-region-p ()
1038 (if zmacs-regions (mark) t)))
1039 (defun cperl-use-region-p ()
1040 (if transient-mark-mode mark-active t))
1041 (defun cperl-mark-active () mark-active))
1042
1043 (defsubst cperl-enable-font-lock ()
1044 cperl-can-font-lock)
1045
1046 (defun cperl-putback-char (c) ; Emacs 19
1047 (set 'unread-command-events (list c))) ; Avoid undefined warning
1048
1049 (if cperl-xemacs-p
1050 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1051 (setq unread-command-events (list (eval '(character-to-event c))))))
1052
1053 (or (fboundp 'uncomment-region)
1054 (defun uncomment-region (beg end)
1055 (interactive "r")
1056 (comment-region beg end -1)))
1057
1058 (defvar cperl-do-not-fontify
1059 (if (string< emacs-version "19.30")
1060 'fontified
1061 'lazy-lock)
1062 "Text property which inhibits refontification.")
1063
1064 (defsubst cperl-put-do-not-fontify (from to &optional post)
1065 ;; If POST, do not do it with postponed fontification
1066 (if (and post cperl-syntaxify-by-font-lock)
1067 nil
1068 (put-text-property (max (point-min) (1- from))
1069 to cperl-do-not-fontify t)))
1070
1071 (defcustom cperl-mode-hook nil
1072 "Hook run by CPerl mode."
1073 :type 'hook
1074 :group 'cperl)
1075
1076 (defvar cperl-syntax-state nil)
1077 (defvar cperl-syntax-done-to nil)
1078 (defvar cperl-emacs-can-parse (> (length (save-excursion
1079 (parse-partial-sexp (point) (point)))) 9))
1080 \f
1081 ;; Make customization possible "in reverse"
1082 (defsubst cperl-val (symbol &optional default hairy)
1083 (cond
1084 ((eq (symbol-value symbol) 'null) default)
1085 (cperl-hairy (or hairy t))
1086 (t (symbol-value symbol))))
1087 \f
1088
1089 (defun cperl-make-indent (column &optional minimum keep)
1090 "Makes indent of the current line the requested amount.
1091 Unless KEEP, removes the old indentation. Works around a bug in ancient
1092 versions of Emacs."
1093 (let ((prop (get-text-property (point) 'syntax-type)))
1094 (or keep
1095 (delete-horizontal-space))
1096 (indent-to column minimum)
1097 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1098 (and prop
1099 (> (current-column) 0)
1100 (save-excursion
1101 (beginning-of-line)
1102 (or (get-text-property (point) 'syntax-type)
1103 (and (looking-at "\\=[ \t]")
1104 (put-text-property (point) (match-end 0)
1105 'syntax-type prop)))))))
1106
1107 ;;; Probably it is too late to set these guys already, but it can help later:
1108
1109 ;;;(and cperl-clobber-mode-lists
1110 ;;;(setq auto-mode-alist
1111 ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1112 ;;;(and (boundp 'interpreter-mode-alist)
1113 ;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1114 ;;; '(("miniperl" . perl-mode))))))
1115 (eval-when-compile
1116 (mapcar (lambda (p)
1117 (condition-case nil
1118 (require p)
1119 (error nil)))
1120 '(imenu easymenu etags timer man info))
1121 (if (fboundp 'ps-extend-face-list)
1122 (defmacro cperl-ps-extend-face-list (arg)
1123 `(ps-extend-face-list ,arg))
1124 (defmacro cperl-ps-extend-face-list (arg)
1125 `(error "This version of Emacs has no `ps-extend-face-list'")))
1126 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1127 ;; macros instead of defsubsts don't work on Emacs, so we do the
1128 ;; expansion manually. Any other suggestions?
1129 (require 'cl))
1130
1131 (defvar cperl-mode-abbrev-table nil
1132 "Abbrev table in use in CPerl mode buffers.")
1133
1134 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1135
1136 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
1137
1138 (if cperl-mode-map nil
1139 (setq cperl-mode-map (make-sparse-keymap))
1140 (cperl-define-key "{" 'cperl-electric-lbrace)
1141 (cperl-define-key "[" 'cperl-electric-paren)
1142 (cperl-define-key "(" 'cperl-electric-paren)
1143 (cperl-define-key "<" 'cperl-electric-paren)
1144 (cperl-define-key "}" 'cperl-electric-brace)
1145 (cperl-define-key "]" 'cperl-electric-rparen)
1146 (cperl-define-key ")" 'cperl-electric-rparen)
1147 (cperl-define-key ";" 'cperl-electric-semi)
1148 (cperl-define-key ":" 'cperl-electric-terminator)
1149 (cperl-define-key "\C-j" 'newline-and-indent)
1150 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1151 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1152 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1153 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1154 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1155 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1156 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1157 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1158 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1159 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1160 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1161 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1162 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1163 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1164 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1165 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1166 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
1167 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1168 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1169 [(control meta |)])
1170 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1171 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1172 (cperl-define-key "\177" 'cperl-electric-backspace)
1173 (cperl-define-key "\t" 'cperl-indent-command)
1174 ;; don't clobber the backspace binding:
1175 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1176 [(control c) (control h) F])
1177 (if (cperl-val 'cperl-clobber-lisp-bindings)
1178 (progn
1179 (cperl-define-key "\C-hf"
1180 ;;(concat (char-to-string help-char) "f") ; does not work
1181 'cperl-info-on-command
1182 [(control h) f])
1183 (cperl-define-key "\C-hv"
1184 ;;(concat (char-to-string help-char) "v") ; does not work
1185 'cperl-get-help
1186 [(control h) v])
1187 (cperl-define-key "\C-c\C-hf"
1188 ;;(concat (char-to-string help-char) "f") ; does not work
1189 (key-binding "\C-hf")
1190 [(control c) (control h) f])
1191 (cperl-define-key "\C-c\C-hv"
1192 ;;(concat (char-to-string help-char) "v") ; does not work
1193 (key-binding "\C-hv")
1194 [(control c) (control h) v]))
1195 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1196 [(control c) (control h) f])
1197 (cperl-define-key "\C-c\C-hv"
1198 ;;(concat (char-to-string help-char) "v") ; does not work
1199 'cperl-get-help
1200 [(control c) (control h) v]))
1201 (if (and cperl-xemacs-p
1202 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1203 (progn
1204 ;; substitute-key-definition is usefulness-deenhanced...
1205 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1206 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1207 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1208 (or (boundp 'fill-paragraph-function)
1209 (substitute-key-definition
1210 'fill-paragraph 'cperl-fill-paragraph
1211 cperl-mode-map global-map))
1212 (substitute-key-definition
1213 'indent-sexp 'cperl-indent-exp
1214 cperl-mode-map global-map)
1215 (substitute-key-definition
1216 'indent-region 'cperl-indent-region
1217 cperl-mode-map global-map)
1218 (substitute-key-definition
1219 'indent-for-comment 'cperl-indent-for-comment
1220 cperl-mode-map global-map)))
1221
1222 (defvar cperl-menu)
1223 (defvar cperl-lazy-installed)
1224 (defvar cperl-old-style nil)
1225 (condition-case nil
1226 (progn
1227 (require 'easymenu)
1228 (easy-menu-define
1229 cperl-menu cperl-mode-map "Menu for CPerl mode"
1230 '("Perl"
1231 ["Beginning of function" beginning-of-defun t]
1232 ["End of function" end-of-defun t]
1233 ["Mark function" mark-defun t]
1234 ["Indent expression" cperl-indent-exp t]
1235 ["Fill paragraph/comment" fill-paragraph t]
1236 "----"
1237 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1238 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1239 ("Regexp"
1240 ["Beautify" cperl-beautify-regexp
1241 cperl-use-syntax-table-text-property]
1242 ["Beautify one level deep" (cperl-beautify-regexp 1)
1243 cperl-use-syntax-table-text-property]
1244 ["Beautify a group" cperl-beautify-level
1245 cperl-use-syntax-table-text-property]
1246 ["Beautify a group one level deep" (cperl-beautify-level 1)
1247 cperl-use-syntax-table-text-property]
1248 ["Contract a group" cperl-contract-level
1249 cperl-use-syntax-table-text-property]
1250 ["Contract groups" cperl-contract-levels
1251 cperl-use-syntax-table-text-property]
1252 "----"
1253 ["Find next interpolated" cperl-next-interpolated-REx
1254 (next-single-property-change (point-min) 'REx-interpolated)]
1255 ["Find next interpolated (no //o)"
1256 cperl-next-interpolated-REx-0
1257 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1258 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1259 ["Find next interpolated (neither //o nor whole-REx)"
1260 cperl-next-interpolated-REx-1
1261 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1262 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1263 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1264 "----"
1265 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1266 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1267 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1268 "----"
1269 ["Run" mode-compile (fboundp 'mode-compile)]
1270 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1271 (get-buffer "*compilation*"))]
1272 ["Next error" next-error (get-buffer "*compilation*")]
1273 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1274 "----"
1275 ["Debugger" cperl-db t]
1276 "----"
1277 ("Tools"
1278 ["Imenu" imenu (fboundp 'imenu)]
1279 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1280 "----"
1281 ["Ispell PODs" cperl-pod-spell
1282 ;; Better not to update syntaxification here:
1283 ;; debugging syntaxificatio can be broken by this???
1284 (or
1285 (get-text-property (point-min) 'in-pod)
1286 (< (progn
1287 (and cperl-syntaxify-for-menu
1288 (cperl-update-syntaxification (point-max) (point-max)))
1289 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1290 (point-max)))]
1291 ["Ispell HERE-DOCs" cperl-here-doc-spell
1292 (< (progn
1293 (and cperl-syntaxify-for-menu
1294 (cperl-update-syntaxification (point-max) (point-max)))
1295 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1296 (point-max))]
1297 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1298 (eq 'here-doc (progn
1299 (and cperl-syntaxify-for-menu
1300 (cperl-update-syntaxification (point) (point)))
1301 (get-text-property (point) 'syntax-type)))]
1302 ["Select this HERE-DOC or POD section"
1303 cperl-select-this-pod-or-here-doc
1304 (memq (progn
1305 (and cperl-syntaxify-for-menu
1306 (cperl-update-syntaxification (point) (point)))
1307 (get-text-property (point) 'syntax-type))
1308 '(here-doc pod))]
1309 "----"
1310 ["CPerl pretty print (exprmntl)" cperl-ps-print
1311 (fboundp 'ps-extend-face-list)]
1312 "----"
1313 ["Syntaxify region" cperl-find-pods-heres-region
1314 (cperl-use-region-p)]
1315 ["Profile syntaxification" cperl-time-fontification t]
1316 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1317 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1318 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1319 (cperl-toggle-set-debug-unwind nil t) t]
1320 "----"
1321 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1322 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1323 ("Tags"
1324 ;;; ["Create tags for current file" cperl-etags t]
1325 ;;; ["Add tags for current file" (cperl-etags t) t]
1326 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1327 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1328 ;;; ["Create tags for Perl files in (sub)directories"
1329 ;;; (cperl-etags nil 'recursive) t]
1330 ;;; ["Add tags for Perl files in (sub)directories"
1331 ;;; (cperl-etags t 'recursive) t])
1332 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1333 ["Create tags for current file" (cperl-write-tags nil t) t]
1334 ["Add tags for current file" (cperl-write-tags) t]
1335 ["Create tags for Perl files in directory"
1336 (cperl-write-tags nil t nil t) t]
1337 ["Add tags for Perl files in directory"
1338 (cperl-write-tags nil nil nil t) t]
1339 ["Create tags for Perl files in (sub)directories"
1340 (cperl-write-tags nil t t t) t]
1341 ["Add tags for Perl files in (sub)directories"
1342 (cperl-write-tags nil nil t t) t]))
1343 ("Perl docs"
1344 ["Define word at point" imenu-go-find-at-position
1345 (fboundp 'imenu-go-find-at-position)]
1346 ["Help on function" cperl-info-on-command t]
1347 ["Help on function at point" cperl-info-on-current-command t]
1348 ["Help on symbol at point" cperl-get-help t]
1349 ["Perldoc" cperl-perldoc t]
1350 ["Perldoc on word at point" cperl-perldoc-at-point t]
1351 ["View manpage of POD in this file" cperl-build-manpage t]
1352 ["Auto-help on" cperl-lazy-install
1353 (and (fboundp 'run-with-idle-timer)
1354 (not cperl-lazy-installed))]
1355 ["Auto-help off" cperl-lazy-unstall
1356 (and (fboundp 'run-with-idle-timer)
1357 cperl-lazy-installed)])
1358 ("Toggle..."
1359 ["Auto newline" cperl-toggle-auto-newline t]
1360 ["Electric parens" cperl-toggle-electric t]
1361 ["Electric keywords" cperl-toggle-abbrev t]
1362 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1363 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1364 ["Auto fill" auto-fill-mode t])
1365 ("Indent styles..."
1366 ["CPerl" (cperl-set-style "CPerl") t]
1367 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1368 ["GNU" (cperl-set-style "GNU") t]
1369 ["C++" (cperl-set-style "C++") t]
1370 ["K&R" (cperl-set-style "K&R") t]
1371 ["BSD" (cperl-set-style "BSD") t]
1372 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1373 ["Memorize Current" (cperl-set-style "Current") t]
1374 ["Memorized" (cperl-set-style-back) cperl-old-style])
1375 ("Micro-docs"
1376 ["Tips" (describe-variable 'cperl-tips) t]
1377 ["Problems" (describe-variable 'cperl-problems) t]
1378 ["Speed" (describe-variable 'cperl-speed) t]
1379 ["Praise" (describe-variable 'cperl-praise) t]
1380 ["Faces" (describe-variable 'cperl-tips-faces) t]
1381 ["CPerl mode" (describe-function 'cperl-mode) t]
1382 ["CPerl version"
1383 (message "The version of master-file for this CPerl is %s-Emacs"
1384 cperl-version) t]))))
1385 (error nil))
1386
1387 (autoload 'c-macro-expand "cmacexp"
1388 "Display the result of expanding all C macros occurring in the region.
1389 The expansion is entirely correct because it uses the C preprocessor."
1390 t)
1391
1392 ;;; These two must be unwound, otherwise take exponential time
1393 (defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1394 "Regular expression to match optional whitespace with interpspersed comments.
1395 Should contain exactly one group.")
1396
1397 ;;; This one is tricky to unwind; still very inefficient...
1398 (defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1399 "Regular expression to match whitespace with interpspersed comments.
1400 Should contain exactly one group.")
1401
1402
1403 ;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1404 ;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1405 ;;; Details of groups in this may be used in several functions; see comments
1406 ;;; near mentioned above variable(s)...
1407 ;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1408 (defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1409 "Match the text after `sub' in a subroutine declaration.
1410 If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1411 of attributes (if present), or end of the name or prototype (whatever is
1412 the last)."
1413 (concat ; Assume n groups before this...
1414 "\\(" ; n+1=name-group
1415 cperl-white-and-comment-rex ; n+2=pre-name
1416 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1417 "\\)" ; END n+1=name-group
1418 (if named "" "?")
1419 "\\(" ; n+4=proto-group
1420 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1421 "\\(([^()]*)\\)" ; n+6=prototype
1422 "\\)?" ; END n+4=proto-group
1423 "\\(" ; n+7=attr-group
1424 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1425 "\\(" ; n+9=start-attr
1426 ":"
1427 (if attr (concat
1428 "\\("
1429 cperl-maybe-white-and-comment-rex ; whitespace-comments
1430 "\\(\\sw\\|_\\)+" ; attr-name
1431 ;; attr-arg (1 level of internal parens allowed!)
1432 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1433 "\\(" ; optional : (XXX allows trailing???)
1434 cperl-maybe-white-and-comment-rex ; whitespace-comments
1435 ":\\)?"
1436 "\\)+")
1437 "[^:]")
1438 "\\)"
1439 "\\)?" ; END n+6=proto-group
1440 ))
1441
1442 ;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1443 ;;; and `cperl-outline-level'.
1444 ;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1445 (defvar cperl-imenu--function-name-regexp-perl
1446 (concat
1447 "^\\(" ; 1 = all
1448 "\\([ \t]*package" ; 2 = package-group
1449 "\\(" ; 3 = package-name-group
1450 cperl-white-and-comment-rex ; 4 = pre-package-name
1451 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1452 "\\|"
1453 "[ \t]*sub"
1454 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1455 cperl-maybe-white-and-comment-rex ; 15=pre-block
1456 "\\|"
1457 "=head\\([1-4]\\)[ \t]+" ; 16=level
1458 "\\([^\n]+\\)$" ; 17=text
1459 "\\)"))
1460
1461 (defvar cperl-outline-regexp
1462 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1463
1464 (defvar cperl-mode-syntax-table nil
1465 "Syntax table in use in CPerl mode buffers.")
1466
1467 (defvar cperl-string-syntax-table nil
1468 "Syntax table in use in CPerl mode string-like chunks.")
1469
1470 (defsubst cperl-1- (p)
1471 (max (point-min) (1- p)))
1472
1473 (defsubst cperl-1+ (p)
1474 (min (point-max) (1+ p)))
1475
1476 (if cperl-mode-syntax-table
1477 ()
1478 (setq cperl-mode-syntax-table (make-syntax-table))
1479 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1480 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1481 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1482 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1483 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1484 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1485 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1486 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1487 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1488 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1489 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1490 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1491 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1492 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1493 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1494 (if cperl-under-as-char
1495 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1496 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1497 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1498 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1499 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1500 (modify-syntax-entry ?\{ "." cperl-string-syntax-table)
1501 (modify-syntax-entry ?\} "." cperl-string-syntax-table)
1502 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
1503
1504
1505 \f
1506 (defvar cperl-faces-init nil)
1507 ;; Fix for msb.el
1508 (defvar cperl-msb-fixed nil)
1509 (defvar cperl-use-major-mode 'cperl-mode)
1510 (defvar cperl-font-lock-multiline-start nil)
1511 (defvar cperl-font-lock-multiline nil)
1512 (defvar cperl-font-locking nil)
1513
1514 ;; NB as it stands the code in cperl-mode assumes this only has one
1515 ;; element. If Xemacs 19 support were dropped, this could all be simplified.
1516 (defvar cperl-compilation-error-regexp-alist
1517 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
1518 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
1519 2 3))
1520 "Alist that specifies how to match errors in perl output.")
1521
1522 ;;;###autoload
1523 (defun cperl-mode ()
1524 "Major mode for editing Perl code.
1525 Expression and list commands understand all C brackets.
1526 Tab indents for Perl code.
1527 Paragraphs are separated by blank lines only.
1528 Delete converts tabs to spaces as it moves back.
1529
1530 Various characters in Perl almost always come in pairs: {}, (), [],
1531 sometimes <>. When the user types the first, she gets the second as
1532 well, with optional special formatting done on {}. (Disabled by
1533 default.) You can always quote (with \\[quoted-insert]) the left
1534 \"paren\" to avoid the expansion. The processing of < is special,
1535 since most the time you mean \"less\". CPerl mode tries to guess
1536 whether you want to type pair <>, and inserts is if it
1537 appropriate. You can set `cperl-electric-parens-string' to the string that
1538 contains the parenths from the above list you want to be electrical.
1539 Electricity of parenths is controlled by `cperl-electric-parens'.
1540 You may also set `cperl-electric-parens-mark' to have electric parens
1541 look for active mark and \"embrace\" a region if possible.'
1542
1543 CPerl mode provides expansion of the Perl control constructs:
1544
1545 if, else, elsif, unless, while, until, continue, do,
1546 for, foreach, formy and foreachmy.
1547
1548 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1549
1550 The user types the keyword immediately followed by a space, which
1551 causes the construct to be expanded, and the point is positioned where
1552 she is most likely to want to be. eg. when the user types a space
1553 following \"if\" the following appears in the buffer: if () { or if ()
1554 } { } and the cursor is between the parentheses. The user can then
1555 type some boolean expression within the parens. Having done that,
1556 typing \\[cperl-linefeed] places you - appropriately indented - on a
1557 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1558 directive line, then appropriate number of new lines is inserted).
1559
1560 If CPerl decides that you want to insert \"English\" style construct like
1561
1562 bite if angry;
1563
1564 it will not do any expansion. See also help on variable
1565 `cperl-extra-newline-before-brace'. (Note that one can switch the
1566 help message on expansion by setting `cperl-message-electric-keyword'
1567 to nil.)
1568
1569 \\[cperl-linefeed] is a convenience replacement for typing carriage
1570 return. It places you in the next line with proper indentation, or if
1571 you type it inside the inline block of control construct, like
1572
1573 foreach (@lines) {print; print}
1574
1575 and you are on a boundary of a statement inside braces, it will
1576 transform the construct into a multiline and will place you into an
1577 appropriately indented blank line. If you need a usual
1578 `newline-and-indent' behavior, it is on \\[newline-and-indent],
1579 see documentation on `cperl-electric-linefeed'.
1580
1581 Use \\[cperl-invert-if-unless] to change a construction of the form
1582
1583 if (A) { B }
1584
1585 into
1586
1587 B if A;
1588
1589 \\{cperl-mode-map}
1590
1591 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1592 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1593 on electric space between $ and {, `cperl-electric-parens-string' is
1594 the string that contains parentheses that should be electric in CPerl
1595 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1596 setting `cperl-electric-keywords' enables electric expansion of
1597 control structures in CPerl. `cperl-electric-linefeed' governs which
1598 one of two linefeed behavior is preferable. You can enable all these
1599 options simultaneously (recommended mode of use) by setting
1600 `cperl-hairy' to t. In this case you can switch separate options off
1601 by setting them to `null'. Note that one may undo the extra
1602 whitespace inserted by semis and braces in `auto-newline'-mode by
1603 consequent \\[cperl-electric-backspace].
1604
1605 If your site has perl5 documentation in info format, you can use commands
1606 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1607 These keys run commands `cperl-info-on-current-command' and
1608 `cperl-info-on-command', which one is which is controlled by variable
1609 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1610 \(in turn affected by `cperl-hairy').
1611
1612 Even if you have no info-format documentation, short one-liner-style
1613 help is available on \\[cperl-get-help], and one can run perldoc or
1614 man via menu.
1615
1616 It is possible to show this help automatically after some idle time.
1617 This is regulated by variable `cperl-lazy-help-time'. Default with
1618 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1619 secs idle time . It is also possible to switch this on/off from the
1620 menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1621
1622 Use \\[cperl-lineup] to vertically lineup some construction - put the
1623 beginning of the region at the start of construction, and make region
1624 span the needed amount of lines.
1625
1626 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1627 `cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1628 here-docs sections. With capable Emaxen results of scan are used
1629 for indentation too, otherwise they are used for highlighting only.
1630
1631 Variables controlling indentation style:
1632 `cperl-tab-always-indent'
1633 Non-nil means TAB in CPerl mode should always reindent the current line,
1634 regardless of where in the line point is when the TAB command is used.
1635 `cperl-indent-left-aligned-comments'
1636 Non-nil means that the comment starting in leftmost column should indent.
1637 `cperl-auto-newline'
1638 Non-nil means automatically newline before and after braces,
1639 and after colons and semicolons, inserted in Perl code. The following
1640 \\[cperl-electric-backspace] will remove the inserted whitespace.
1641 Insertion after colons requires both this variable and
1642 `cperl-auto-newline-after-colon' set.
1643 `cperl-auto-newline-after-colon'
1644 Non-nil means automatically newline even after colons.
1645 Subject to `cperl-auto-newline' setting.
1646 `cperl-indent-level'
1647 Indentation of Perl statements within surrounding block.
1648 The surrounding block's indentation is the indentation
1649 of the line on which the open-brace appears.
1650 `cperl-continued-statement-offset'
1651 Extra indentation given to a substatement, such as the
1652 then-clause of an if, or body of a while, or just a statement continuation.
1653 `cperl-continued-brace-offset'
1654 Extra indentation given to a brace that starts a substatement.
1655 This is in addition to `cperl-continued-statement-offset'.
1656 `cperl-brace-offset'
1657 Extra indentation for line if it starts with an open brace.
1658 `cperl-brace-imaginary-offset'
1659 An open brace following other text is treated as if it the line started
1660 this far to the right of the actual line indentation.
1661 `cperl-label-offset'
1662 Extra indentation for line that is a label.
1663 `cperl-min-label-indent'
1664 Minimal indentation for line that is a label.
1665
1666 Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1667 `cperl-indent-level' 5 4 2 4
1668 `cperl-brace-offset' 0 0 0 0
1669 `cperl-continued-brace-offset' -5 -4 0 0
1670 `cperl-label-offset' -5 -4 -2 -4
1671 `cperl-continued-statement-offset' 5 4 2 4
1672
1673 CPerl knows several indentation styles, and may bulk set the
1674 corresponding variables. Use \\[cperl-set-style] to do this. Use
1675 \\[cperl-set-style-back] to restore the memorized preexisting values
1676 \(both available from menu). See examples in `cperl-style-examples'.
1677
1678 Part of the indentation style is how different parts of if/elsif/else
1679 statements are broken into lines; in CPerl, this is reflected on how
1680 templates for these constructs are created (controlled by
1681 `cperl-extra-newline-before-brace'), and how reflow-logic should treat \"continuation\" blocks of else/elsif/continue, controlled by the same variable,
1682 and by `cperl-extra-newline-before-brace-multiline',
1683 `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1684
1685 If `cperl-indent-level' is 0, the statement after opening brace in
1686 column 0 is indented on
1687 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1688
1689 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1690 with no args.
1691
1692 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1693 or as help on variables `cperl-tips', `cperl-problems',
1694 `cperl-praise', `cperl-speed'."
1695 (interactive)
1696 (kill-all-local-variables)
1697 (use-local-map cperl-mode-map)
1698 (if (cperl-val 'cperl-electric-linefeed)
1699 (progn
1700 (local-set-key "\C-J" 'cperl-linefeed)
1701 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1702 (if (and
1703 (cperl-val 'cperl-clobber-lisp-bindings)
1704 (cperl-val 'cperl-info-on-command-no-prompt))
1705 (progn
1706 ;; don't clobber the backspace binding:
1707 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1708 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1709 [(control c) (control h) f])))
1710 (setq major-mode cperl-use-major-mode)
1711 (setq mode-name "CPerl")
1712 (let ((prev-a-c abbrevs-changed))
1713 (define-abbrev-table 'cperl-mode-abbrev-table '(
1714 ("if" "if" cperl-electric-keyword 0)
1715 ("elsif" "elsif" cperl-electric-keyword 0)
1716 ("while" "while" cperl-electric-keyword 0)
1717 ("until" "until" cperl-electric-keyword 0)
1718 ("unless" "unless" cperl-electric-keyword 0)
1719 ("else" "else" cperl-electric-else 0)
1720 ("continue" "continue" cperl-electric-else 0)
1721 ("for" "for" cperl-electric-keyword 0)
1722 ("foreach" "foreach" cperl-electric-keyword 0)
1723 ("formy" "formy" cperl-electric-keyword 0)
1724 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1725 ("do" "do" cperl-electric-keyword 0)
1726 ("=pod" "=pod" cperl-electric-pod 0)
1727 ("=over" "=over" cperl-electric-pod 0)
1728 ("=head1" "=head1" cperl-electric-pod 0)
1729 ("=head2" "=head2" cperl-electric-pod 0)
1730 ("pod" "pod" cperl-electric-pod 0)
1731 ("over" "over" cperl-electric-pod 0)
1732 ("head1" "head1" cperl-electric-pod 0)
1733 ("head2" "head2" cperl-electric-pod 0)))
1734 (setq abbrevs-changed prev-a-c))
1735 (setq local-abbrev-table cperl-mode-abbrev-table)
1736 (if (cperl-val 'cperl-electric-keywords)
1737 (abbrev-mode 1))
1738 (set-syntax-table cperl-mode-syntax-table)
1739 ;; Until Emacs is multi-threaded, we do not actually need it local:
1740 (make-local-variable 'cperl-font-lock-multiline-start)
1741 (make-local-variable 'cperl-font-locking)
1742 (make-local-variable 'outline-regexp)
1743 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1744 (setq outline-regexp cperl-outline-regexp)
1745 (make-local-variable 'outline-level)
1746 (setq outline-level 'cperl-outline-level)
1747 (make-local-variable 'paragraph-start)
1748 (setq paragraph-start (concat "^$\\|" page-delimiter))
1749 (make-local-variable 'paragraph-separate)
1750 (setq paragraph-separate paragraph-start)
1751 (make-local-variable 'paragraph-ignore-fill-prefix)
1752 (setq paragraph-ignore-fill-prefix t)
1753 (if cperl-xemacs-p
1754 (progn
1755 (make-local-variable 'paren-backwards-message)
1756 (set 'paren-backwards-message t)))
1757 (make-local-variable 'indent-line-function)
1758 (setq indent-line-function 'cperl-indent-line)
1759 (make-local-variable 'require-final-newline)
1760 (setq require-final-newline mode-require-final-newline)
1761 (make-local-variable 'comment-start)
1762 (setq comment-start "# ")
1763 (make-local-variable 'comment-end)
1764 (setq comment-end "")
1765 (make-local-variable 'comment-column)
1766 (setq comment-column cperl-comment-column)
1767 (make-local-variable 'comment-start-skip)
1768 (setq comment-start-skip "#+ *")
1769 (make-local-variable 'defun-prompt-regexp)
1770 ;;; "[ \t]*sub"
1771 ;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1772 ;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1773 (setq defun-prompt-regexp
1774 (concat "^[ \t]*\\(sub"
1775 (cperl-after-sub-regexp 'named 'attr-groups)
1776 "\\|" ; per toke.c
1777 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1778 "\\)"
1779 cperl-maybe-white-and-comment-rex))
1780 (make-local-variable 'comment-indent-function)
1781 (setq comment-indent-function 'cperl-comment-indent)
1782 (and (boundp 'fill-paragraph-function)
1783 (progn
1784 (make-local-variable 'fill-paragraph-function)
1785 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
1786 (make-local-variable 'parse-sexp-ignore-comments)
1787 (setq parse-sexp-ignore-comments t)
1788 (make-local-variable 'indent-region-function)
1789 (setq indent-region-function 'cperl-indent-region)
1790 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1791 (make-local-variable 'imenu-create-index-function)
1792 (setq imenu-create-index-function
1793 (function cperl-imenu--create-perl-index))
1794 (make-local-variable 'imenu-sort-function)
1795 (setq imenu-sort-function nil)
1796 (make-local-variable 'vc-rcs-header)
1797 (set 'vc-rcs-header cperl-vc-rcs-header)
1798 (make-local-variable 'vc-sccs-header)
1799 (set 'vc-sccs-header cperl-vc-sccs-header)
1800 ;; This one is obsolete...
1801 (make-local-variable 'vc-header-alist)
1802 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1803 `((SCCS ,(car cperl-vc-sccs-header))
1804 (RCS ,(car cperl-vc-rcs-header)))))
1805 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1806 (make-local-variable 'compilation-error-regexp-alist-alist)
1807 (set 'compilation-error-regexp-alist-alist
1808 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
1809 (symbol-value 'compilation-error-regexp-alist-alist)))
1810 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1811 (let ((f 'compilation-build-compilation-error-regexp-alist))
1812 (funcall f))
1813 (make-local-variable 'compilation-error-regexp-alist)
1814 (push 'cperl compilation-error-regexp-alist)))
1815 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1816 (make-local-variable 'compilation-error-regexp-alist)
1817 (set 'compilation-error-regexp-alist
1818 (append cperl-compilation-error-regexp-alist
1819 (symbol-value 'compilation-error-regexp-alist)))))
1820 (make-local-variable 'font-lock-defaults)
1821 (setq font-lock-defaults
1822 (cond
1823 ((string< emacs-version "19.30")
1824 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1825 ((string< emacs-version "19.33") ; Which one to use?
1826 '((cperl-font-lock-keywords
1827 cperl-font-lock-keywords-1
1828 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1829 (t
1830 '((cperl-load-font-lock-keywords
1831 cperl-load-font-lock-keywords-1
1832 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1833 (make-local-variable 'cperl-syntax-state)
1834 (setq cperl-syntax-state nil) ; reset syntaxification cache
1835 (if cperl-use-syntax-table-text-property
1836 (progn
1837 (make-local-variable 'parse-sexp-lookup-properties)
1838 ;; Do not introduce variable if not needed, we check it!
1839 (set 'parse-sexp-lookup-properties t)
1840 ;; Fix broken font-lock:
1841 (or (boundp 'font-lock-unfontify-region-function)
1842 (set 'font-lock-unfontify-region-function
1843 'font-lock-default-unfontify-region))
1844 (unless cperl-xemacs-p ; Our: just a plug for wrong font-lock
1845 (make-local-variable 'font-lock-unfontify-region-function)
1846 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1847 'cperl-font-lock-unfontify-region-function))
1848 (make-local-variable 'cperl-syntax-done-to)
1849 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1850 (make-local-variable 'font-lock-syntactic-keywords)
1851 (setq font-lock-syntactic-keywords
1852 (if cperl-syntaxify-by-font-lock
1853 '((cperl-fontify-syntaxically))
1854 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1855 ;; used to ignore syntax-table text-properties. (t) is a hack
1856 ;; to make font-lock think that font-lock-syntactic-keywords
1857 ;; are defined.
1858 '(t)))))
1859 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1860 (progn
1861 (setq cperl-font-lock-multiline t) ; Not localized...
1862 (set (make-local-variable 'font-lock-multiline) t))
1863 (make-local-variable 'font-lock-fontify-region-function)
1864 (set 'font-lock-fontify-region-function ; not present with old Emacs
1865 'cperl-font-lock-fontify-region-function))
1866 (make-local-variable 'font-lock-fontify-region-function)
1867 (set 'font-lock-fontify-region-function ; not present with old Emacs
1868 'cperl-font-lock-fontify-region-function)
1869 (make-local-variable 'cperl-old-style)
1870 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1871 (set (make-local-variable 'normal-auto-fill-function)
1872 'cperl-do-auto-fill)
1873 (or (fboundp 'cperl-old-auto-fill-mode)
1874 (progn
1875 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1876 (defun auto-fill-mode (&optional arg)
1877 (interactive "P")
1878 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1879 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1880 (setq auto-fill-function 'cperl-do-auto-fill))))))
1881 (if (cperl-enable-font-lock)
1882 (if (cperl-val 'cperl-font-lock)
1883 (progn (or cperl-faces-init (cperl-init-faces))
1884 (font-lock-mode 1))))
1885 (set (make-local-variable 'facemenu-add-face-function)
1886 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1887 (and (boundp 'msb-menu-cond)
1888 (not cperl-msb-fixed)
1889 (cperl-msb-fix))
1890 (if (featurep 'easymenu)
1891 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1892 (run-mode-hooks 'cperl-mode-hook)
1893 (if cperl-hook-after-change
1894 (add-hook 'after-change-functions 'cperl-after-change-function nil t))
1895 ;; After hooks since fontification will break this
1896 (if cperl-pod-here-scan
1897 (or cperl-syntaxify-by-font-lock
1898 (progn (or cperl-faces-init (cperl-init-faces-weak))
1899 (cperl-find-pods-heres)))))
1900 \f
1901 ;; Fix for perldb - make default reasonable
1902 (defun cperl-db ()
1903 (interactive)
1904 (require 'gud)
1905 (perldb (read-from-minibuffer "Run perldb (like this): "
1906 (if (consp gud-perldb-history)
1907 (car gud-perldb-history)
1908 (concat "perl " ;;(file-name-nondirectory
1909 ;; I have problems
1910 ;; in OS/2
1911 ;; otherwise
1912 (buffer-file-name)))
1913 nil nil
1914 '(gud-perldb-history . 1))))
1915 \f
1916 (defun cperl-msb-fix ()
1917 ;; Adds perl files to msb menu, supposes that msb is already loaded
1918 (setq cperl-msb-fixed t)
1919 (let* ((l (length msb-menu-cond))
1920 (last (nth (1- l) msb-menu-cond))
1921 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1922 (handle (1- (nth 1 last))))
1923 (setcdr precdr (list
1924 (list
1925 '(memq major-mode '(cperl-mode perl-mode))
1926 handle
1927 "Perl Files (%d)")
1928 last))))
1929 \f
1930 ;; This is used by indent-for-comment
1931 ;; to decide how much to indent a comment in CPerl code
1932 ;; based on its context. Do fallback if comment is found wrong.
1933
1934 (defvar cperl-wrong-comment)
1935 (defvar cperl-st-cfence '(14)) ; Comment-fence
1936 (defvar cperl-st-sfence '(15)) ; String-fence
1937 (defvar cperl-st-punct '(1))
1938 (defvar cperl-st-word '(2))
1939 (defvar cperl-st-bra '(4 . ?\>))
1940 (defvar cperl-st-ket '(5 . ?\<))
1941
1942
1943 (defun cperl-comment-indent () ; called at point at supposed comment
1944 (let ((p (point)) (c (current-column)) was phony)
1945 (if (and (not cperl-indent-comment-at-column-0)
1946 (looking-at "^#"))
1947 0 ; Existing comment at bol stays there.
1948 ;; Wrong comment found
1949 (save-excursion
1950 (setq was (cperl-to-comment-or-eol)
1951 phony (eq (get-text-property (point) 'syntax-table)
1952 cperl-st-cfence))
1953 (if phony
1954 (progn ; Too naive???
1955 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1956 (if (eq (preceding-char) ?\#)
1957 (forward-char -1))
1958 (setq was nil)))
1959 (if (= (point) p) ; Our caller found a correct place
1960 (progn
1961 (skip-chars-backward " \t")
1962 (setq was (current-column))
1963 (if (eq was 0)
1964 comment-column
1965 (max (1+ was) ; Else indent at comment column
1966 comment-column)))
1967 ;; No, the caller found a random place; we need to edit ourselves
1968 (if was nil
1969 (insert comment-start)
1970 (backward-char (length comment-start)))
1971 (setq cperl-wrong-comment t)
1972 (cperl-make-indent comment-column 1) ; Indent min 1
1973 c)))))
1974
1975 ;;;(defun cperl-comment-indent-fallback ()
1976 ;;; "Is called if the standard comment-search procedure fails.
1977 ;;;Point is at start of real comment."
1978 ;;; (let ((c (current-column)) target cnt prevc)
1979 ;;; (if (= c comment-column) nil
1980 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1981 ;;; (setq target (max (1+ (setq prevc
1982 ;;; (current-column))) ; Else indent at comment column
1983 ;;; comment-column))
1984 ;;; (if (= c comment-column) nil
1985 ;;; (delete-backward-char cnt)
1986 ;;; (while (< prevc target)
1987 ;;; (insert "\t")
1988 ;;; (setq prevc (current-column)))
1989 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1990 ;;; (while (< prevc target)
1991 ;;; (insert " ")
1992 ;;; (setq prevc (current-column)))))))
1993
1994 (defun cperl-indent-for-comment ()
1995 "Substitute for `indent-for-comment' in CPerl."
1996 (interactive)
1997 (let (cperl-wrong-comment)
1998 (indent-for-comment)
1999 (if cperl-wrong-comment ; set by `cperl-comment-indent'
2000 (progn (cperl-to-comment-or-eol)
2001 (forward-char (length comment-start))))))
2002
2003 (defun cperl-comment-region (b e arg)
2004 "Comment or uncomment each line in the region in CPerl mode.
2005 See `comment-region'."
2006 (interactive "r\np")
2007 (let ((comment-start "#"))
2008 (comment-region b e arg)))
2009
2010 (defun cperl-uncomment-region (b e arg)
2011 "Uncomment or comment each line in the region in CPerl mode.
2012 See `comment-region'."
2013 (interactive "r\np")
2014 (let ((comment-start "#"))
2015 (comment-region b e (- arg))))
2016
2017 (defvar cperl-brace-recursing nil)
2018
2019 (defun cperl-electric-brace (arg &optional only-before)
2020 "Insert character and correct line's indentation.
2021 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2022 place (even in empty line), but not after. If after \")\" and the inserted
2023 char is \"{\", insert extra newline before only if
2024 `cperl-extra-newline-before-brace'."
2025 (interactive "P")
2026 (let (insertpos
2027 (other-end (if (and cperl-electric-parens-mark
2028 (cperl-mark-active)
2029 (< (mark) (point)))
2030 (mark)
2031 nil)))
2032 (if (and other-end
2033 (not cperl-brace-recursing)
2034 (cperl-val 'cperl-electric-parens)
2035 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2036 ;; Need to insert a matching pair
2037 (progn
2038 (save-excursion
2039 (setq insertpos (point-marker))
2040 (goto-char other-end)
2041 (setq last-command-char ?\{)
2042 (cperl-electric-lbrace arg insertpos))
2043 (forward-char 1))
2044 ;; Check whether we close something "usual" with `}'
2045 (if (and (eq last-command-char ?\})
2046 (not
2047 (condition-case nil
2048 (save-excursion
2049 (up-list (- (prefix-numeric-value arg)))
2050 ;;(cperl-after-block-p (point-min))
2051 (or (cperl-after-expr-p nil "{;)")
2052 ;; after sub, else, continue
2053 (cperl-after-block-p nil 'pre)))
2054 (error nil))))
2055 ;; Just insert the guy
2056 (self-insert-command (prefix-numeric-value arg))
2057 (if (and (not arg) ; No args, end (of empty line or auto)
2058 (eolp)
2059 (or (and (null only-before)
2060 (save-excursion
2061 (skip-chars-backward " \t")
2062 (bolp)))
2063 (and (eq last-command-char ?\{) ; Do not insert newline
2064 ;; if after ")" and `cperl-extra-newline-before-brace'
2065 ;; is nil, do not insert extra newline.
2066 (not cperl-extra-newline-before-brace)
2067 (save-excursion
2068 (skip-chars-backward " \t")
2069 (eq (preceding-char) ?\))))
2070 (if cperl-auto-newline
2071 (progn (cperl-indent-line) (newline) t) nil)))
2072 (progn
2073 (self-insert-command (prefix-numeric-value arg))
2074 (cperl-indent-line)
2075 (if cperl-auto-newline
2076 (setq insertpos (1- (point))))
2077 (if (and cperl-auto-newline (null only-before))
2078 (progn
2079 (newline)
2080 (cperl-indent-line)))
2081 (save-excursion
2082 (if insertpos (progn (goto-char insertpos)
2083 (search-forward (make-string
2084 1 last-command-char))
2085 (setq insertpos (1- (point)))))
2086 (delete-char -1))))
2087 (if insertpos
2088 (save-excursion
2089 (goto-char insertpos)
2090 (self-insert-command (prefix-numeric-value arg)))
2091 (self-insert-command (prefix-numeric-value arg)))))))
2092
2093 (defun cperl-electric-lbrace (arg &optional end)
2094 "Insert character, correct line's indentation, correct quoting by space."
2095 (interactive "P")
2096 (let ((cperl-brace-recursing t)
2097 (cperl-auto-newline cperl-auto-newline)
2098 (other-end (or end
2099 (if (and cperl-electric-parens-mark
2100 (cperl-mark-active)
2101 (> (mark) (point)))
2102 (save-excursion
2103 (goto-char (mark))
2104 (point-marker))
2105 nil)))
2106 pos after)
2107 (and (cperl-val 'cperl-electric-lbrace-space)
2108 (eq (preceding-char) ?$)
2109 (save-excursion
2110 (skip-chars-backward "$")
2111 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2112 (insert ?\s))
2113 ;; Check whether we are in comment
2114 (if (and
2115 (save-excursion
2116 (beginning-of-line)
2117 (not (looking-at "[ \t]*#")))
2118 (cperl-after-expr-p nil "{;)"))
2119 nil
2120 (setq cperl-auto-newline nil))
2121 (cperl-electric-brace arg)
2122 (and (cperl-val 'cperl-electric-parens)
2123 (eq last-command-char ?{)
2124 (memq last-command-char
2125 (append cperl-electric-parens-string nil))
2126 (or (if other-end (goto-char (marker-position other-end)))
2127 t)
2128 (setq last-command-char ?} pos (point))
2129 (progn (cperl-electric-brace arg t)
2130 (goto-char pos)))))
2131
2132 (defun cperl-electric-paren (arg)
2133 "Insert an opening parenthesis or a matching pair of parentheses.
2134 See `cperl-electric-parens'."
2135 (interactive "P")
2136 (let ((beg (save-excursion (beginning-of-line) (point)))
2137 (other-end (if (and cperl-electric-parens-mark
2138 (cperl-mark-active)
2139 (> (mark) (point)))
2140 (save-excursion
2141 (goto-char (mark))
2142 (point-marker))
2143 nil)))
2144 (if (and (cperl-val 'cperl-electric-parens)
2145 (memq last-command-char
2146 (append cperl-electric-parens-string nil))
2147 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2148 ;;(not (save-excursion (search-backward "#" beg t)))
2149 (if (eq last-command-char ?<)
2150 (progn
2151 (and abbrev-mode ; later it is too late, may be after `for'
2152 (expand-abbrev))
2153 (cperl-after-expr-p nil "{;(,:="))
2154 1))
2155 (progn
2156 (self-insert-command (prefix-numeric-value arg))
2157 (if other-end (goto-char (marker-position other-end)))
2158 (insert (make-string
2159 (prefix-numeric-value arg)
2160 (cdr (assoc last-command-char '((?{ .?})
2161 (?[ . ?])
2162 (?( . ?))
2163 (?< . ?>))))))
2164 (forward-char (- (prefix-numeric-value arg))))
2165 (self-insert-command (prefix-numeric-value arg)))))
2166
2167 (defun cperl-electric-rparen (arg)
2168 "Insert a matching pair of parentheses if marking is active.
2169 If not, or if we are not at the end of marking range, would self-insert.
2170 Affected by `cperl-electric-parens'."
2171 (interactive "P")
2172 (let ((beg (save-excursion (beginning-of-line) (point)))
2173 (other-end (if (and cperl-electric-parens-mark
2174 (cperl-val 'cperl-electric-parens)
2175 (memq last-command-char
2176 (append cperl-electric-parens-string nil))
2177 (cperl-mark-active)
2178 (< (mark) (point)))
2179 (mark)
2180 nil))
2181 p)
2182 (if (and other-end
2183 (cperl-val 'cperl-electric-parens)
2184 (memq last-command-char '( ?\) ?\] ?\} ?\> ))
2185 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2186 ;;(not (save-excursion (search-backward "#" beg t)))
2187 )
2188 (progn
2189 (self-insert-command (prefix-numeric-value arg))
2190 (setq p (point))
2191 (if other-end (goto-char other-end))
2192 (insert (make-string
2193 (prefix-numeric-value arg)
2194 (cdr (assoc last-command-char '((?\} . ?\{)
2195 (?\] . ?\[)
2196 (?\) . ?\()
2197 (?\> . ?\<))))))
2198 (goto-char (1+ p)))
2199 (self-insert-command (prefix-numeric-value arg)))))
2200
2201 (defun cperl-electric-keyword ()
2202 "Insert a construction appropriate after a keyword.
2203 Help message may be switched off by setting `cperl-message-electric-keyword'
2204 to nil."
2205 (let ((beg (save-excursion (beginning-of-line) (point)))
2206 (dollar (and (eq last-command-char ?$)
2207 (eq this-command 'self-insert-command)))
2208 (delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
2209 (memq this-command '(self-insert-command newline))))
2210 my do)
2211 (and (save-excursion
2212 (condition-case nil
2213 (progn
2214 (backward-sexp 1)
2215 (setq do (looking-at "do\\>")))
2216 (error nil))
2217 (cperl-after-expr-p nil "{;:"))
2218 (save-excursion
2219 (not
2220 (re-search-backward
2221 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2222 beg t)))
2223 (save-excursion (or (not (re-search-backward "^=" nil t))
2224 (or
2225 (looking-at "=cut")
2226 (and cperl-use-syntax-table-text-property
2227 (not (eq (get-text-property (point)
2228 'syntax-type)
2229 'pod))))))
2230 (save-excursion (forward-sexp -1)
2231 (not (memq (following-char) (append "$@%&*" nil))))
2232 (progn
2233 (and (eq (preceding-char) ?y)
2234 (progn ; "foreachmy"
2235 (forward-char -2)
2236 (insert " ")
2237 (forward-char 2)
2238 (setq my t dollar t
2239 delete
2240 (memq this-command '(self-insert-command newline)))))
2241 (and dollar (insert " $"))
2242 (cperl-indent-line)
2243 ;;(insert " () {\n}")
2244 (cond
2245 (cperl-extra-newline-before-brace
2246 (insert (if do "\n" " ()\n"))
2247 (insert "{")
2248 (cperl-indent-line)
2249 (insert "\n")
2250 (cperl-indent-line)
2251 (insert "\n}")
2252 (and do (insert " while ();")))
2253 (t
2254 (insert (if do " {\n} while ();" " () {\n}"))))
2255 (or (looking-at "[ \t]\\|$") (insert " "))
2256 (cperl-indent-line)
2257 (if dollar (progn (search-backward "$")
2258 (if my
2259 (forward-char 1)
2260 (delete-char 1)))
2261 (search-backward ")")
2262 (if (eq last-command-char ?\()
2263 (progn ; Avoid "if (())"
2264 (delete-backward-char 1)
2265 (delete-backward-char -1))))
2266 (if delete
2267 (cperl-putback-char cperl-del-back-ch))
2268 (if cperl-message-electric-keyword
2269 (message "Precede char by C-q to avoid expansion"))))))
2270
2271 (defun cperl-ensure-newlines (n &optional pos)
2272 "Make sure there are N newlines after the point."
2273 (or pos (setq pos (point)))
2274 (if (looking-at "\n")
2275 (forward-char 1)
2276 (insert "\n"))
2277 (if (> n 1)
2278 (cperl-ensure-newlines (1- n) pos)
2279 (goto-char pos)))
2280
2281 (defun cperl-electric-pod ()
2282 "Insert a POD chunk appropriate after a =POD directive."
2283 (let ((delete (and (memq last-command-char '(?\s ?\n ?\t ?\f))
2284 (memq this-command '(self-insert-command newline))))
2285 head1 notlast name p really-delete over)
2286 (and (save-excursion
2287 (forward-word -1)
2288 (and
2289 (eq (preceding-char) ?=)
2290 (progn
2291 (setq head1 (looking-at "head1\\>[ \t]*$"))
2292 (setq over (and (looking-at "over\\>[ \t]*$")
2293 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2294 (forward-char -1)
2295 (bolp))
2296 (or
2297 (get-text-property (point) 'in-pod)
2298 (cperl-after-expr-p nil "{;:")
2299 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2300 (not (looking-at "\n*=cut"))
2301 (or (not cperl-use-syntax-table-text-property)
2302 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2303 (progn
2304 (save-excursion
2305 (setq notlast (re-search-forward "^\n=" nil t)))
2306 (or notlast
2307 (progn
2308 (insert "\n\n=cut")
2309 (cperl-ensure-newlines 2)
2310 (forward-word -2)
2311 (if (and head1
2312 (not
2313 (save-excursion
2314 (forward-char -1)
2315 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2316 nil t)))) ; Only one
2317 (progn
2318 (forward-word 1)
2319 (setq name (file-name-sans-extension
2320 (file-name-nondirectory (buffer-file-name)))
2321 p (point))
2322 (insert " NAME\n\n" name
2323 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2324 "=head1 DESCRIPTION")
2325 (cperl-ensure-newlines 4)
2326 (goto-char p)
2327 (forward-word 2)
2328 (end-of-line)
2329 (setq really-delete t))
2330 (forward-word 1))))
2331 (if over
2332 (progn
2333 (setq p (point))
2334 (insert "\n\n=item \n\n\n\n"
2335 "=back")
2336 (cperl-ensure-newlines 2)
2337 (goto-char p)
2338 (forward-word 1)
2339 (end-of-line)
2340 (setq really-delete t)))
2341 (if (and delete really-delete)
2342 (cperl-putback-char cperl-del-back-ch))))))
2343
2344 (defun cperl-electric-else ()
2345 "Insert a construction appropriate after a keyword.
2346 Help message may be switched off by setting `cperl-message-electric-keyword'
2347 to nil."
2348 (let ((beg (save-excursion (beginning-of-line) (point))))
2349 (and (save-excursion
2350 (backward-sexp 1)
2351 (cperl-after-expr-p nil "{;:"))
2352 (save-excursion
2353 (not
2354 (re-search-backward
2355 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2356 beg t)))
2357 (save-excursion (or (not (re-search-backward "^=" nil t))
2358 (looking-at "=cut")
2359 (and cperl-use-syntax-table-text-property
2360 (not (eq (get-text-property (point)
2361 'syntax-type)
2362 'pod)))))
2363 (progn
2364 (cperl-indent-line)
2365 ;;(insert " {\n\n}")
2366 (cond
2367 (cperl-extra-newline-before-brace
2368 (insert "\n")
2369 (insert "{")
2370 (cperl-indent-line)
2371 (insert "\n\n}"))
2372 (t
2373 (insert " {\n\n}")))
2374 (or (looking-at "[ \t]\\|$") (insert " "))
2375 (cperl-indent-line)
2376 (forward-line -1)
2377 (cperl-indent-line)
2378 (cperl-putback-char cperl-del-back-ch)
2379 (setq this-command 'cperl-electric-else)
2380 (if cperl-message-electric-keyword
2381 (message "Precede char by C-q to avoid expansion"))))))
2382
2383 (defun cperl-linefeed ()
2384 "Go to end of line, open a new line and indent appropriately.
2385 If in POD, insert appropriate lines."
2386 (interactive)
2387 (let ((beg (save-excursion (beginning-of-line) (point)))
2388 (end (save-excursion (end-of-line) (point)))
2389 (pos (point)) start over cut res)
2390 (if (and ; Check if we need to split:
2391 ; i.e., on a boundary and inside "{...}"
2392 (save-excursion (cperl-to-comment-or-eol)
2393 (>= (point) pos)) ; Not in a comment
2394 (or (save-excursion
2395 (skip-chars-backward " \t" beg)
2396 (forward-char -1)
2397 (looking-at "[;{]")) ; After { or ; + spaces
2398 (looking-at "[ \t]*}") ; Before }
2399 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2400 (save-excursion
2401 (and
2402 (eq (car (parse-partial-sexp pos end -1)) -1)
2403 ; Leave the level of parens
2404 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2405 ; Are at end
2406 (cperl-after-block-p (point-min))
2407 (progn
2408 (backward-sexp 1)
2409 (setq start (point-marker))
2410 (<= start pos))))) ; Redundant? Are after the
2411 ; start of parens group.
2412 (progn
2413 (skip-chars-backward " \t")
2414 (or (memq (preceding-char) (append ";{" nil))
2415 (insert ";"))
2416 (insert "\n")
2417 (forward-line -1)
2418 (cperl-indent-line)
2419 (goto-char start)
2420 (or (looking-at "{[ \t]*$") ; If there is a statement
2421 ; before, move it to separate line
2422 (progn
2423 (forward-char 1)
2424 (insert "\n")
2425 (cperl-indent-line)))
2426 (forward-line 1) ; We are on the target line
2427 (cperl-indent-line)
2428 (beginning-of-line)
2429 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2430 ; after, move it to separate line
2431 (progn
2432 (end-of-line)
2433 (search-backward "}" beg)
2434 (skip-chars-backward " \t")
2435 (or (memq (preceding-char) (append ";{" nil))
2436 (insert ";"))
2437 (insert "\n")
2438 (cperl-indent-line)
2439 (forward-line -1)))
2440 (forward-line -1) ; We are on the line before target
2441 (end-of-line)
2442 (newline-and-indent))
2443 (end-of-line) ; else - no splitting
2444 (cond
2445 ((and (looking-at "\n[ \t]*{$")
2446 (save-excursion
2447 (skip-chars-backward " \t")
2448 (eq (preceding-char) ?\)))) ; Probably if () {} group
2449 ; with an extra newline.
2450 (forward-line 2)
2451 (cperl-indent-line))
2452 ((save-excursion ; In POD header
2453 (forward-paragraph -1)
2454 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2455 ;; We are after \n now, so look for the rest
2456 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2457 (progn
2458 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2459 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2460 t)))
2461 (if (and over
2462 (progn
2463 (forward-paragraph -1)
2464 (forward-word 1)
2465 (setq pos (point))
2466 (setq cut (buffer-substring (point)
2467 (save-excursion
2468 (end-of-line)
2469 (point))))
2470 (delete-char (- (save-excursion (end-of-line) (point))
2471 (point)))
2472 (setq res (expand-abbrev))
2473 (save-excursion
2474 (goto-char pos)
2475 (insert cut))
2476 res))
2477 nil
2478 (cperl-ensure-newlines (if cut 2 4))
2479 (forward-line 2)))
2480 ((get-text-property (point) 'in-pod) ; In POD section
2481 (cperl-ensure-newlines 4)
2482 (forward-line 2))
2483 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2484 (forward-line 1)
2485 (cperl-indent-line))
2486 (t
2487 (newline-and-indent))))))
2488
2489 (defun cperl-electric-semi (arg)
2490 "Insert character and correct line's indentation."
2491 (interactive "P")
2492 (if cperl-auto-newline
2493 (cperl-electric-terminator arg)
2494 (self-insert-command (prefix-numeric-value arg))
2495 (if cperl-autoindent-on-semi
2496 (cperl-indent-line))))
2497
2498 (defun cperl-electric-terminator (arg)
2499 "Insert character and correct line's indentation."
2500 (interactive "P")
2501 (let ((end (point))
2502 (auto (and cperl-auto-newline
2503 (or (not (eq last-command-char ?:))
2504 cperl-auto-newline-after-colon)))
2505 insertpos)
2506 (if (and ;;(not arg)
2507 (eolp)
2508 (not (save-excursion
2509 (beginning-of-line)
2510 (skip-chars-forward " \t")
2511 (or
2512 ;; Ignore in comment lines
2513 (= (following-char) ?#)
2514 ;; Colon is special only after a label
2515 ;; So quickly rule out most other uses of colon
2516 ;; and do no indentation for them.
2517 (and (eq last-command-char ?:)
2518 (save-excursion
2519 (forward-word 1)
2520 (skip-chars-forward " \t")
2521 (and (< (point) end)
2522 (progn (goto-char (- end 1))
2523 (not (looking-at ":"))))))
2524 (progn
2525 (beginning-of-defun)
2526 (let ((pps (parse-partial-sexp (point) end)))
2527 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2528 (progn
2529 (self-insert-command (prefix-numeric-value arg))
2530 ;;(forward-char -1)
2531 (if auto (setq insertpos (point-marker)))
2532 ;;(forward-char 1)
2533 (cperl-indent-line)
2534 (if auto
2535 (progn
2536 (newline)
2537 (cperl-indent-line)))
2538 (save-excursion
2539 (if insertpos (goto-char (1- (marker-position insertpos)))
2540 (forward-char -1))
2541 (delete-char 1))))
2542 (if insertpos
2543 (save-excursion
2544 (goto-char insertpos)
2545 (self-insert-command (prefix-numeric-value arg)))
2546 (self-insert-command (prefix-numeric-value arg)))))
2547
2548 (defun cperl-electric-backspace (arg)
2549 "Backspace, or remove the whitespace around the point inserted by an electric
2550 key. Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2551 (interactive "p")
2552 (if (and cperl-auto-newline
2553 (memq last-command '(cperl-electric-semi
2554 cperl-electric-terminator
2555 cperl-electric-lbrace))
2556 (memq (preceding-char) '(?\s ?\t ?\n)))
2557 (let (p)
2558 (if (eq last-command 'cperl-electric-lbrace)
2559 (skip-chars-forward " \t\n"))
2560 (setq p (point))
2561 (skip-chars-backward " \t\n")
2562 (delete-region (point) p))
2563 (and (eq last-command 'cperl-electric-else)
2564 ;; We are removing the whitespace *inside* cperl-electric-else
2565 (setq this-command 'cperl-electric-else-really))
2566 (if (and cperl-auto-newline
2567 (eq last-command 'cperl-electric-else-really)
2568 (memq (preceding-char) '(?\s ?\t ?\n)))
2569 (let (p)
2570 (skip-chars-forward " \t\n")
2571 (setq p (point))
2572 (skip-chars-backward " \t\n")
2573 (delete-region (point) p))
2574 (if cperl-electric-backspace-untabify
2575 (backward-delete-char-untabify arg)
2576 (delete-backward-char arg)))))
2577
2578 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2579
2580 (defun cperl-inside-parens-p () ;; NOT USED????
2581 (condition-case ()
2582 (save-excursion
2583 (save-restriction
2584 (narrow-to-region (point)
2585 (progn (beginning-of-defun) (point)))
2586 (goto-char (point-max))
2587 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2588 (error nil)))
2589 \f
2590 (defun cperl-indent-command (&optional whole-exp)
2591 "Indent current line as Perl code, or in some cases insert a tab character.
2592 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2593 line. Otherwise, indent the current line only if point is at the left margin
2594 or in the line's indentation; otherwise insert a tab.
2595
2596 A numeric argument, regardless of its value,
2597 means indent rigidly all the lines of the expression starting after point
2598 so that this line becomes properly indented.
2599 The relative indentation among the lines of the expression are preserved."
2600 (interactive "P")
2601 (cperl-update-syntaxification (point) (point))
2602 (if whole-exp
2603 ;; If arg, always indent this line as Perl
2604 ;; and shift remaining lines of expression the same amount.
2605 (let ((shift-amt (cperl-indent-line))
2606 beg end)
2607 (save-excursion
2608 (if cperl-tab-always-indent
2609 (beginning-of-line))
2610 (setq beg (point))
2611 (forward-sexp 1)
2612 (setq end (point))
2613 (goto-char beg)
2614 (forward-line 1)
2615 (setq beg (point)))
2616 (if (and shift-amt (> end beg))
2617 (indent-code-rigidly beg end shift-amt "#")))
2618 (if (and (not cperl-tab-always-indent)
2619 (save-excursion
2620 (skip-chars-backward " \t")
2621 (not (bolp))))
2622 (insert-tab)
2623 (cperl-indent-line))))
2624
2625 (defun cperl-indent-line (&optional parse-data)
2626 "Indent current line as Perl code.
2627 Return the amount the indentation changed by."
2628 (let ((case-fold-search nil)
2629 (pos (- (point-max) (point)))
2630 indent i beg shift-amt)
2631 (setq indent (cperl-calculate-indent parse-data)
2632 i indent)
2633 (beginning-of-line)
2634 (setq beg (point))
2635 (cond ((or (eq indent nil) (eq indent t))
2636 (setq indent (current-indentation) i nil))
2637 ;;((eq indent t) ; Never?
2638 ;; (setq indent (cperl-calculate-indent-within-comment)))
2639 ;;((looking-at "[ \t]*#")
2640 ;; (setq indent 0))
2641 (t
2642 (skip-chars-forward " \t")
2643 (if (listp indent) (setq indent (car indent)))
2644 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2645 (not (looking-at "[smy]:\\|tr:")))
2646 (and (> indent 0)
2647 (setq indent (max cperl-min-label-indent
2648 (+ indent cperl-label-offset)))))
2649 ((= (following-char) ?})
2650 (setq indent (- indent cperl-indent-level)))
2651 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2652 (setq indent (+ indent cperl-close-paren-offset)))
2653 ((= (following-char) ?{)
2654 (setq indent (+ indent cperl-brace-offset))))))
2655 (skip-chars-forward " \t")
2656 (setq shift-amt (and i (- indent (current-column))))
2657 (if (or (not shift-amt)
2658 (zerop shift-amt))
2659 (if (> (- (point-max) pos) (point))
2660 (goto-char (- (point-max) pos)))
2661 ;;;(delete-region beg (point))
2662 ;;;(indent-to indent)
2663 (cperl-make-indent indent)
2664 ;; If initial point was within line's indentation,
2665 ;; position after the indentation. Else stay at same point in text.
2666 (if (> (- (point-max) pos) (point))
2667 (goto-char (- (point-max) pos))))
2668 shift-amt))
2669
2670 (defun cperl-after-label ()
2671 ;; Returns true if the point is after label. Does not do save-excursion.
2672 (and (eq (preceding-char) ?:)
2673 (memq (char-syntax (char-after (- (point) 2)))
2674 '(?w ?_))
2675 (progn
2676 (backward-sexp)
2677 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2678
2679 (defun cperl-get-state (&optional parse-start start-state)
2680 ;; returns list (START STATE DEPTH PRESTART),
2681 ;; START is a good place to start parsing, or equal to
2682 ;; PARSE-START if preset,
2683 ;; STATE is what is returned by `parse-partial-sexp'.
2684 ;; DEPTH is true is we are immediately after end of block
2685 ;; which contains START.
2686 ;; PRESTART is the position basing on which START was found.
2687 (save-excursion
2688 (let ((start-point (point)) depth state start prestart)
2689 (if (and parse-start
2690 (<= parse-start start-point))
2691 (goto-char parse-start)
2692 (beginning-of-defun)
2693 (setq start-state nil))
2694 (setq prestart (point))
2695 (if start-state nil
2696 ;; Try to go out, if sub is not on the outermost level
2697 (while (< (point) start-point)
2698 (setq start (point) parse-start start depth nil
2699 state (parse-partial-sexp start start-point -1))
2700 (if (> (car state) -1) nil
2701 ;; The current line could start like }}}, so the indentation
2702 ;; corresponds to a different level than what we reached
2703 (setq depth t)
2704 (beginning-of-line 2))) ; Go to the next line.
2705 (if start (goto-char start))) ; Not at the start of file
2706 (setq start (point))
2707 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2708 (list start state depth prestart))))
2709
2710 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2711
2712 (defun cperl-beginning-of-property (p prop &optional lim)
2713 "Given that P has a property PROP, find where the property starts.
2714 Will not look before LIM."
2715 ;;; XXXX What to do at point-max???
2716 (or (previous-single-property-change (cperl-1+ p) prop lim)
2717 (point-min))
2718 ;;; (cond ((eq p (point-min))
2719 ;;; p)
2720 ;;; ((and lim (<= p lim))
2721 ;;; p)
2722 ;;; ((not (get-text-property (1- p) prop))
2723 ;;; p)
2724 ;;; (t (or (previous-single-property-change p look-prop lim)
2725 ;;; (point-min))))
2726 )
2727
2728 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2729 ;; Old workhorse for calculation of indentation; the major problem
2730 ;; is that it mixes the sniffer logic to understand what the current line
2731 ;; MEANS with the logic to actually calculate where to indent it.
2732 ;; The latter part should be eventually moved to `cperl-calculate-indent';
2733 ;; actually, this is mostly done now...
2734 (cperl-update-syntaxification (point) (point))
2735 (let ((res (get-text-property (point) 'syntax-type)))
2736 (save-excursion
2737 (cond
2738 ((and (memq res '(pod here-doc here-doc-delim format))
2739 (not (get-text-property (point) 'indentable)))
2740 (vector res))
2741 ;; before start of POD - whitespace found since do not have 'pod!
2742 ((looking-at "[ \t]*\n=")
2743 (error "Spaces before POD section!"))
2744 ((and (not cperl-indent-left-aligned-comments)
2745 (looking-at "^#"))
2746 [comment-special:at-beginning-of-line])
2747 ((get-text-property (point) 'in-pod)
2748 [in-pod])
2749 (t
2750 (beginning-of-line)
2751 (let* ((indent-point (point))
2752 (char-after-pos (save-excursion
2753 (skip-chars-forward " \t")
2754 (point)))
2755 (char-after (char-after char-after-pos))
2756 (pre-indent-point (point))
2757 p prop look-prop is-block delim)
2758 (save-excursion ; Know we are not in POD, find appropriate pos before
2759 (cperl-backward-to-noncomment nil)
2760 (setq p (max (point-min) (1- (point)))
2761 prop (get-text-property p 'syntax-type)
2762 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2763 'syntax-type))
2764 (if (memq prop '(pod here-doc format here-doc-delim))
2765 (progn
2766 (goto-char (cperl-beginning-of-property p look-prop))
2767 (beginning-of-line)
2768 (setq pre-indent-point (point)))))
2769 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2770 (let* ((case-fold-search nil)
2771 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2772 (start (or (nth 2 parse-data) ; last complete sexp terminated
2773 (nth 0 s-s))) ; Good place to start parsing
2774 (state (nth 1 s-s))
2775 (containing-sexp (car (cdr state)))
2776 old-indent)
2777 (if (and
2778 ;;containing-sexp ;; We are buggy at toplevel :-(
2779 parse-data)
2780 (progn
2781 (setcar parse-data pre-indent-point)
2782 (setcar (cdr parse-data) state)
2783 (or (nth 2 parse-data)
2784 (setcar (cddr parse-data) start))
2785 ;; Before this point: end of statement
2786 (setq old-indent (nth 3 parse-data))))
2787 (cond ((get-text-property (point) 'indentable)
2788 ;; indent to "after" the surrounding open
2789 ;; (same offset as `cperl-beautify-regexp-piece'),
2790 ;; skip blanks if we do not close the expression.
2791 (setq delim ; We do not close the expression
2792 (get-text-property
2793 (cperl-1+ char-after-pos) 'indentable)
2794 p (1+ (cperl-beginning-of-property
2795 (point) 'indentable))
2796 is-block ; misused for: preceeding line in REx
2797 (save-excursion ; Find preceeding line
2798 (cperl-backward-to-noncomment p)
2799 (beginning-of-line)
2800 (if (<= (point) p)
2801 (progn ; get indent from the first line
2802 (goto-char p)
2803 (skip-chars-forward " \t")
2804 (if (memq (char-after (point))
2805 (append "#\n" nil))
2806 nil ; Can't use intentation of this line...
2807 (point)))
2808 (skip-chars-forward " \t")
2809 (point)))
2810 prop (parse-partial-sexp p char-after-pos))
2811 (cond ((not delim) ; End the REx, ignore is-block
2812 (vector 'indentable 'terminator p is-block))
2813 (is-block ; Indent w.r.t. preceeding line
2814 (vector 'indentable 'cont-line char-after-pos
2815 is-block char-after p))
2816 (t ; No preceeding line...
2817 (vector 'indentable 'first-line p))))
2818 ((get-text-property char-after-pos 'REx-part2)
2819 (vector 'REx-part2 (point)))
2820 ((nth 4 state)
2821 [comment])
2822 ((nth 3 state)
2823 [string])
2824 ;; XXXX Do we need to special-case this?
2825 ((null containing-sexp)
2826 ;; Line is at top level. May be data or function definition,
2827 ;; or may be function argument declaration.
2828 ;; Indent like the previous top level line
2829 ;; unless that ends in a closeparen without semicolon,
2830 ;; in which case this line is the first argument decl.
2831 (skip-chars-forward " \t")
2832 (cperl-backward-to-noncomment (or old-indent (point-min)))
2833 (setq state
2834 (or (bobp)
2835 (eq (point) old-indent) ; old-indent was at comment
2836 (eq (preceding-char) ?\;)
2837 ;; Had ?\) too
2838 (and (eq (preceding-char) ?\})
2839 (cperl-after-block-and-statement-beg
2840 (point-min))) ; Was start - too close
2841 (memq char-after (append ")]}" nil))
2842 (and (eq (preceding-char) ?\:) ; label
2843 (progn
2844 (forward-sexp -1)
2845 (skip-chars-backward " \t")
2846 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2847 (get-text-property (point) 'first-format-line)))
2848
2849 ;; Look at previous line that's at column 0
2850 ;; to determine whether we are in top-level decls
2851 ;; or function's arg decls. Set basic-indent accordingly.
2852 ;; Now add a little if this is a continuation line.
2853 (and state
2854 parse-data
2855 (not (eq char-after ?\C-j))
2856 (setcdr (cddr parse-data)
2857 (list pre-indent-point)))
2858 (vector 'toplevel start char-after state (nth 2 s-s)))
2859 ((not
2860 (or (setq is-block
2861 (and (setq delim (= (char-after containing-sexp) ?{))
2862 (save-excursion ; Is it a hash?
2863 (goto-char containing-sexp)
2864 (cperl-block-p))))
2865 cperl-indent-parens-as-block))
2866 ;; group is an expression, not a block:
2867 ;; indent to just after the surrounding open parens,
2868 ;; skip blanks if we do not close the expression.
2869 (goto-char (1+ containing-sexp))
2870 (or (memq char-after
2871 (append (if delim "}" ")]}") nil))
2872 (looking-at "[ \t]*\\(#\\|$\\)")
2873 (skip-chars-forward " \t"))
2874 (setq old-indent (point)) ; delim=is-brace
2875 (vector 'in-parens char-after (point) delim containing-sexp))
2876 (t
2877 ;; Statement level. Is it a continuation or a new statement?
2878 ;; Find previous non-comment character.
2879 (goto-char pre-indent-point) ; Skip one level of POD/etc
2880 (cperl-backward-to-noncomment containing-sexp)
2881 ;; Back up over label lines, since they don't
2882 ;; affect whether our line is a continuation.
2883 ;; (Had \, too)
2884 (while;;(or (eq (preceding-char) ?\,)
2885 (and (eq (preceding-char) ?:)
2886 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2887 (memq (char-syntax (char-after (- (point) 2)))
2888 '(?w ?_))))
2889 ;;)
2890 ;; This is always FALSE?
2891 (if (eq (preceding-char) ?\,)
2892 ;; Will go to beginning of line, essentially.
2893 ;; Will ignore embedded sexpr XXXX.
2894 (cperl-backward-to-start-of-continued-exp containing-sexp))
2895 (beginning-of-line)
2896 (cperl-backward-to-noncomment containing-sexp))
2897 ;; Now we get non-label preceeding the indent point
2898 (if (not (or (eq (1- (point)) containing-sexp)
2899 (memq (preceding-char)
2900 (append (if is-block " ;{" " ,;{") '(nil)))
2901 (and (eq (preceding-char) ?\})
2902 (cperl-after-block-and-statement-beg
2903 containing-sexp))
2904 (get-text-property (point) 'first-format-line)))
2905 ;; This line is continuation of preceding line's statement;
2906 ;; indent `cperl-continued-statement-offset' more than the
2907 ;; previous line of the statement.
2908 ;;
2909 ;; There might be a label on this line, just
2910 ;; consider it bad style and ignore it.
2911 (progn
2912 (cperl-backward-to-start-of-continued-exp containing-sexp)
2913 (vector 'continuation (point) char-after is-block delim))
2914 ;; This line starts a new statement.
2915 ;; Position following last unclosed open brace
2916 (goto-char containing-sexp)
2917 ;; Is line first statement after an open-brace?
2918 (or
2919 ;; If no, find that first statement and indent like
2920 ;; it. If the first statement begins with label, do
2921 ;; not believe when the indentation of the label is too
2922 ;; small.
2923 (save-excursion
2924 (forward-char 1)
2925 (let ((colon-line-end 0))
2926 (while
2927 (progn (skip-chars-forward " \t\n")
2928 ;; s: foo : bar :x is NOT label
2929 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2930 (not (looking-at "[sym]:\\|tr:"))))
2931 ;; Skip over comments and labels following openbrace.
2932 (cond ((= (following-char) ?\#)
2933 (forward-line 1))
2934 ((= (following-char) ?\=)
2935 (goto-char
2936 (or (next-single-property-change (point) 'in-pod)
2937 (point-max)))) ; do not loop if no syntaxification
2938 ;; label:
2939 (t
2940 (save-excursion (end-of-line)
2941 (setq colon-line-end (point)))
2942 (search-forward ":"))))
2943 ;; We are at beginning of code (NOT label or comment)
2944 ;; First, the following code counts
2945 ;; if it is before the line we want to indent.
2946 (and (< (point) indent-point)
2947 (vector 'have-prev-sibling (point) colon-line-end
2948 containing-sexp))))
2949 (progn
2950 ;; If no previous statement,
2951 ;; indent it relative to line brace is on.
2952
2953 ;; For open-braces not the first thing in a line,
2954 ;; add in cperl-brace-imaginary-offset.
2955
2956 ;; If first thing on a line: ?????
2957 ;; Move back over whitespace before the openbrace.
2958 (setq ; brace first thing on a line
2959 old-indent (progn (skip-chars-backward " \t") (bolp)))
2960 ;; Should we indent w.r.t. earlier than start?
2961 ;; Move to start of control group, possibly on a different line
2962 (or cperl-indent-wrt-brace
2963 (cperl-backward-to-noncomment (point-min)))
2964 ;; If the openbrace is preceded by a parenthesized exp,
2965 ;; move to the beginning of that;
2966 (if (eq (preceding-char) ?\))
2967 (progn
2968 (forward-sexp -1)
2969 (cperl-backward-to-noncomment (point-min))))
2970 ;; In the case it starts a subroutine, indent with
2971 ;; respect to `sub', not with respect to the
2972 ;; first thing on the line, say in the case of
2973 ;; anonymous sub in a hash.
2974 (if (and;; Is it a sub in group starting on this line?
2975 (cond ((get-text-property (point) 'attrib-group)
2976 (goto-char (cperl-beginning-of-property
2977 (point) 'attrib-group)))
2978 ((eq (preceding-char) ?b)
2979 (forward-sexp -1)
2980 (looking-at "sub\\>")))
2981 (setq p (nth 1 ; start of innermost containing list
2982 (parse-partial-sexp
2983 (save-excursion (beginning-of-line)
2984 (point))
2985 (point)))))
2986 (progn
2987 (goto-char (1+ p)) ; enclosing block on the same line
2988 (skip-chars-forward " \t")
2989 (vector 'code-start-in-block containing-sexp char-after
2990 (and delim (not is-block)) ; is a HASH
2991 old-indent ; brace first thing on a line
2992 t (point) ; have something before...
2993 )
2994 ;;(current-column)
2995 )
2996 ;; Get initial indentation of the line we are on.
2997 ;; If line starts with label, calculate label indentation
2998 (vector 'code-start-in-block containing-sexp char-after
2999 (and delim (not is-block)) ; is a HASH
3000 old-indent ; brace first thing on a line
3001 nil (point))))))))))))))) ; nothing interesting before
3002
3003 (defvar cperl-indent-rules-alist
3004 '((pod nil) ; via `syntax-type' property
3005 (here-doc nil) ; via `syntax-type' property
3006 (here-doc-delim nil) ; via `syntax-type' property
3007 (format nil) ; via `syntax-type' property
3008 (in-pod nil) ; via `in-pod' property
3009 (comment-special:at-beginning-of-line nil)
3010 (string t)
3011 (comment nil))
3012 "Alist of indentation rules for CPerl mode.
3013 The values mean:
3014 nil: do not indent;
3015 number: add this amount of indentation.")
3016
3017 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3018 "Return appropriate indentation for current line as Perl code.
3019 In usual case returns an integer: the column to indent to.
3020 Returns nil if line starts inside a string, t if in a comment.
3021
3022 Will not correct the indentation for labels, but will correct it for braces
3023 and closing parentheses and brackets."
3024 ;; This code is still a broken architecture: in some cases we need to
3025 ;; compensate for some modifications which `cperl-indent-line' will add later
3026 (save-excursion
3027 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3028 (cond
3029 ;;((or (null i) (eq i t) (numberp i))
3030 ;; i)
3031 ((vectorp i)
3032 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3033 (cond
3034 (what (cadr what)) ; Load from table
3035 ;;
3036 ;; Indenters for regular expressions with //x and qw()
3037 ;;
3038 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3039 (goto-char (elt i 1))
3040 (condition-case nil ; Use indentation of the 1st part
3041 (forward-sexp -1))
3042 (current-column))
3043 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3044 (cond ;;; [indentable terminator start-pos is-block]
3045 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3046 (goto-char (elt i 2)) ; After opening parens
3047 (1- (current-column)))
3048 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3049 (goto-char (elt i 2))
3050 (+ (or cperl-regexp-indent-step cperl-indent-level)
3051 -1
3052 (current-column)))
3053 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3054 ;; Indent as the level after closing parens
3055 (goto-char (elt i 2)) ; indent line
3056 (skip-chars-forward " \t)") ; Skip closing parens
3057 (setq p (point))
3058 (goto-char (elt i 3)) ; previous line
3059 (skip-chars-forward " \t)") ; Skip closing parens
3060 ;; Number of parens in between:
3061 (setq p (nth 0 (parse-partial-sexp (point) p))
3062 what (elt i 4)) ; First char on current line
3063 (goto-char (elt i 3)) ; previous line
3064 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3065 (cond ((eq what ?\) )
3066 (- cperl-close-paren-offset)) ; compensate
3067 ((eq what ?\| )
3068 (- (or cperl-regexp-indent-step cperl-indent-level)))
3069 (t 0))
3070 (if (eq (following-char) ?\| )
3071 (or cperl-regexp-indent-step cperl-indent-level)
3072 0)
3073 (current-column)))
3074 (t
3075 (error "Unrecognized value of indent: %s" i))))
3076 ;;
3077 ;; Indenter for stuff at toplevel
3078 ;;
3079 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3080 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3081 (goto-char (elt i 1)) ; start = Good place to start parsing
3082 (- (current-indentation) ;
3083 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3084 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3085 ;; Look at previous line that's at column 0
3086 ;; to determine whether we are in top-level decls
3087 ;; or function's arg decls. Set basic-indent accordingly.
3088 ;; Now add a little if this is a continuation line.
3089 (if (elt i 3) ; state (XXX What is the semantic???)
3090 0
3091 cperl-continued-statement-offset)))
3092 ;;
3093 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3094 ;;
3095 ((eq 'in-parens (elt i 0))
3096 ;; in-parens char-after old-indent-point is-brace containing-sexp
3097
3098 ;; group is an expression, not a block:
3099 ;; indent to just after the surrounding open parens,
3100 ;; skip blanks if we do not close the expression.
3101 (+ (progn
3102 (goto-char (elt i 2)) ; old-indent-point
3103 (current-column))
3104 (if (and (elt i 3) ; is-brace
3105 (eq (elt i 1) ?\})) ; char-after
3106 ;; Correct indentation of trailing ?\}
3107 (+ cperl-indent-level cperl-close-paren-offset)
3108 0)))
3109 ;;
3110 ;; Indenter for continuation lines
3111 ;;
3112 ((eq 'continuation (elt i 0))
3113 ;; [continuation statement-start char-after is-block is-brace]
3114 (goto-char (elt i 1)) ; statement-start
3115 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3116 0 ; Closing parenth
3117 cperl-continued-statement-offset)
3118 (if (or (elt i 3) ; is-block
3119 (not (elt i 4)) ; is-brace
3120 (not (eq (elt i 2) ?\}))) ; char-after
3121 0
3122 ;; Now it is a hash reference
3123 (+ cperl-indent-level cperl-close-paren-offset))
3124 ;; Labels do not take :: ...
3125 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3126 (if (> (current-indentation) cperl-min-label-indent)
3127 (- (current-indentation) cperl-label-offset)
3128 ;; Do not move `parse-data', this should
3129 ;; be quick anyway (this comment comes
3130 ;; from different location):
3131 (cperl-calculate-indent))
3132 (current-column))
3133 (if (eq (elt i 2) ?\{) ; char-after
3134 cperl-continued-brace-offset 0)))
3135 ;;
3136 ;; Indenter for lines in a block which are not leading lines
3137 ;;
3138 ((eq 'have-prev-sibling (elt i 0))
3139 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3140 (goto-char (elt i 1)) ; sibling-beg
3141 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
3142 (if (> (current-indentation)
3143 cperl-min-label-indent)
3144 (- (current-indentation) cperl-label-offset)
3145 ;; Do not believe: `max' was involved in calculation of indent
3146 (+ cperl-indent-level
3147 (save-excursion
3148 (goto-char (elt i 3)) ; block-start
3149 (current-indentation))))
3150 (current-column)))
3151 ;;
3152 ;; Indenter for the first line in a block
3153 ;;
3154 ((eq 'code-start-in-block (elt i 0))
3155 ;;[code-start-in-block before-brace char-after
3156 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3157 ;; group-starts-before-start-of-sub start-of-control-group]
3158 (goto-char (elt i 1))
3159 ;; For open brace in column zero, don't let statement
3160 ;; start there too. If cperl-indent-level=0,
3161 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3162 (+ (if (and (bolp) (zerop cperl-indent-level))
3163 (+ cperl-brace-offset cperl-continued-statement-offset)
3164 cperl-indent-level)
3165 (if (and (elt i 3) ; is-a-HASH-ref
3166 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3167 (+ cperl-indent-level cperl-close-paren-offset)
3168 0)
3169 ;; Unless openbrace is the first nonwhite thing on the line,
3170 ;; add the cperl-brace-imaginary-offset.
3171 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3172 cperl-brace-imaginary-offset)
3173 (progn
3174 (goto-char (elt i 6)) ; start-of-control-group
3175 (if (elt i 5) ; group-starts-before-start-of-sub
3176 (current-column)
3177 ;; Get initial indentation of the line we are on.
3178 ;; If line starts with label, calculate label indentation
3179 (if (save-excursion
3180 (beginning-of-line)
3181 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3182 (if (> (current-indentation) cperl-min-label-indent)
3183 (- (current-indentation) cperl-label-offset)
3184 ;; Do not move `parse-data', this should
3185 ;; be quick anyway:
3186 (cperl-calculate-indent))
3187 (current-indentation))))))
3188 (t
3189 (error "Unrecognized value of indent: %s" i))))
3190 (t
3191 (error "Got strange value of indent: %s" i))))))
3192
3193 (defun cperl-calculate-indent-within-comment ()
3194 "Return the indentation amount for line, assuming that
3195 the current line is to be regarded as part of a block comment."
3196 (let (end star-start)
3197 (save-excursion
3198 (beginning-of-line)
3199 (skip-chars-forward " \t")
3200 (setq end (point))
3201 (and (= (following-char) ?#)
3202 (forward-line -1)
3203 (cperl-to-comment-or-eol)
3204 (setq end (point)))
3205 (goto-char end)
3206 (current-column))))
3207
3208
3209 (defun cperl-to-comment-or-eol ()
3210 "Go to position before comment on the current line, or to end of line.
3211 Returns true if comment is found. In POD will not move the point."
3212 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3213 ;; then looks for literal # or end-of-line.
3214 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3215 (or cperl-font-locking
3216 (cperl-update-syntaxification lim lim))
3217 (beginning-of-line)
3218 (if (setq pr (get-text-property (point) 'syntax-type))
3219 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3220 (if (or (eq pr 'pod)
3221 (if (or (not e) (> e lim)) ; deep inside a group
3222 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3223 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3224 ;; Else - need to do it the hard way
3225 (and (and e (<= e lim))
3226 (goto-char e))
3227 (while (not stop-in)
3228 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3229 ; stop at comment
3230 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3231 (if (nth 4 state) ; After `#';
3232 ; (nth 2 state) can be
3233 ; beginning of m,s,qq and so
3234 ; on
3235 (if (nth 2 state)
3236 (progn
3237 (setq cpoint (point))
3238 (goto-char (nth 2 state))
3239 (cond
3240 ((looking-at "\\(s\\|tr\\)\\>")
3241 (or (re-search-forward
3242 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3243 lim 'move)
3244 (setq stop-in t)))
3245 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3246 (or (re-search-forward
3247 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3248 lim 'move)
3249 (setq stop-in t)))
3250 (t ; It was fair comment
3251 (setq stop-in t) ; Finish
3252 (goto-char (1- cpoint)))))
3253 (setq stop-in t) ; Finish
3254 (forward-char -1))
3255 (setq stop-in t))) ; Finish
3256 (nth 4 state))))
3257
3258 (defsubst cperl-modify-syntax-type (at how)
3259 (if (< at (point-max))
3260 (progn
3261 (put-text-property at (1+ at) 'syntax-table how)
3262 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3263
3264 (defun cperl-protect-defun-start (s e)
3265 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3266 (save-excursion
3267 (goto-char s)
3268 (while (re-search-forward "^\\s(" e 'to-end)
3269 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3270
3271 (defun cperl-commentify (bb e string &optional noface)
3272 (if cperl-use-syntax-table-text-property
3273 (if (eq noface 'n) ; Only immediate
3274 nil
3275 ;; We suppose that e is _after_ the end of construction, as after eol.
3276 (setq string (if string cperl-st-sfence cperl-st-cfence))
3277 (if (> bb (- e 2))
3278 ;; one-char string/comment?!
3279 (cperl-modify-syntax-type bb cperl-st-punct)
3280 (cperl-modify-syntax-type bb string)
3281 (cperl-modify-syntax-type (1- e) string))
3282 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3283 (put-text-property (1+ bb) (1- e)
3284 'syntax-table cperl-string-syntax-table))
3285 (cperl-protect-defun-start bb e))
3286 ;; Fontify
3287 (or noface
3288 (not cperl-pod-here-fontify)
3289 (put-text-property bb e 'face (if string 'font-lock-string-face
3290 'font-lock-comment-face)))))
3291
3292 (defvar cperl-starters '(( ?\( . ?\) )
3293 ( ?\[ . ?\] )
3294 ( ?\{ . ?\} )
3295 ( ?\< . ?\> )))
3296
3297 (defun cperl-cached-syntax-table (st)
3298 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3299 All the entries of the syntax table are \".\", except for a backslash, which
3300 is quoting."
3301 (if (car-safe st)
3302 (car st)
3303 (setcar st (make-syntax-table))
3304 (setq st (car st))
3305 (let ((i 0))
3306 (while (< i 256)
3307 (modify-syntax-entry i "." st)
3308 (setq i (1+ i))))
3309 (modify-syntax-entry ?\\ "\\" st)
3310 st))
3311
3312 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3313 &optional ostart oend)
3314 "Find the end of a regular expression or a stringish construct (q[] etc).
3315 The point should be before the starting delimiter.
3316
3317 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3318 is s/// or tr/// like expression. If END is nil, generates an error
3319 message if needed. If SET-ST is non-nil, will use (or generate) a
3320 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3321 error message in its CAR (unless it already contains some error
3322 message). ARGUMENT should be the name of the construct (used in error
3323 messages). OSTART, OEND may be set in recursive calls when processing
3324 the second argument of 2ARG construct.
3325
3326 Works *before* syntax recognition is done. In IS-2ARG situation may
3327 modify syntax-type text property if the situation is too hard."
3328 (let (b starter ender st i i2 go-forward reset-st set-st)
3329 (skip-chars-forward " \t")
3330 ;; ender means matching-char matcher.
3331 (setq b (point)
3332 starter (if (eobp) 0 (char-after b))
3333 ender (cdr (assoc starter cperl-starters)))
3334 ;; What if starter == ?\\ ????
3335 (setq st (cperl-cached-syntax-table st-l))
3336 (setq set-st t)
3337 ;; Whether we have an intermediate point
3338 (setq i nil)
3339 ;; Prepare the syntax table:
3340 (if (not ender) ; m/blah/, s/x//, s/x/y/
3341 (modify-syntax-entry starter "$" st)
3342 (modify-syntax-entry starter (concat "(" (list ender)) st)
3343 (modify-syntax-entry ender (concat ")" (list starter)) st))
3344 (condition-case bb
3345 (progn
3346 ;; We use `$' syntax class to find matching stuff, but $$
3347 ;; is recognized the same as $, so we need to check this manually.
3348 (if (and (eq starter (char-after (cperl-1+ b)))
3349 (not ender))
3350 ;; $ has TeXish matching rules, so $$ equiv $...
3351 (forward-char 2)
3352 (setq reset-st (syntax-table))
3353 (set-syntax-table st)
3354 (forward-sexp 1)
3355 (if (<= (point) (1+ b))
3356 (error "Unfinished regular expression"))
3357 (set-syntax-table reset-st)
3358 (setq reset-st nil)
3359 ;; Now the problem is with m;blah;;
3360 (and (not ender)
3361 (eq (preceding-char)
3362 (char-after (- (point) 2)))
3363 (save-excursion
3364 (forward-char -2)
3365 (= 0 (% (skip-chars-backward "\\\\") 2)))
3366 (forward-char -1)))
3367 ;; Now we are after the first part.
3368 (and is-2arg ; Have trailing part
3369 (not ender)
3370 (eq (following-char) starter) ; Empty trailing part
3371 (progn
3372 (or (eq (char-syntax (following-char)) ?.)
3373 ;; Make trailing letter into punctuation
3374 (cperl-modify-syntax-type (point) cperl-st-punct))
3375 (setq is-2arg nil go-forward t))) ; Ignore the tail
3376 (if is-2arg ; Not number => have second part
3377 (progn
3378 (setq i (point) i2 i)
3379 (if ender
3380 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3381 (progn
3382 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3383 (goto-char (match-end 0))
3384 (skip-chars-forward " \t\n\f"))
3385 (setq i2 (point))))
3386 (forward-char -1))
3387 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3388 (if ender (modify-syntax-entry ender "." st))
3389 (setq set-st nil)
3390 (setq ender (cperl-forward-re lim end nil st-l err-l
3391 argument starter ender)
3392 ender (nth 2 ender)))))
3393 (error (goto-char lim)
3394 (setq set-st nil)
3395 (if reset-st
3396 (set-syntax-table reset-st))
3397 (or end
3398 (message
3399 "End of `%s%s%c ... %c' string/RE not found: %s"
3400 argument
3401 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3402 starter (or ender starter) bb)
3403 (or (car err-l) (setcar err-l b)))))
3404 (if set-st
3405 (progn
3406 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3407 (if ender (modify-syntax-entry ender "." st))))
3408 ;; i: have 2 args, after end of the first arg
3409 ;; i2: start of the second arg, if any (before delim if `ender').
3410 ;; ender: the last arg bounded by parens-like chars, the second one of them
3411 ;; starter: the starting delimiter of the first arg
3412 ;; go-forward: has 2 args, and the second part is empty
3413 (list i i2 ender starter go-forward)))
3414
3415 (defun cperl-forward-group-in-re (&optional st-l)
3416 "Find the end of a group in a REx.
3417 Return the error message (if any). Does not work if delimiter is `)'.
3418 Works before syntax recognition is done."
3419 ;; Works *before* syntax recognition is done
3420 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3421 (let (st b reset-st)
3422 (condition-case b
3423 (progn
3424 (setq st (cperl-cached-syntax-table st-l))
3425 (modify-syntax-entry ?\( "()" st)
3426 (modify-syntax-entry ?\) ")(" st)
3427 (setq reset-st (syntax-table))
3428 (set-syntax-table st)
3429 (forward-sexp 1))
3430 (error (message
3431 "cperl-forward-group-in-re: error %s" b)))
3432 ;; now restore the initial state
3433 (if st
3434 (progn
3435 (modify-syntax-entry ?\( "." st)
3436 (modify-syntax-entry ?\) "." st)))
3437 (if reset-st
3438 (set-syntax-table reset-st))
3439 b))
3440
3441
3442 (defvar font-lock-string-face)
3443 ;;(defvar font-lock-reference-face)
3444 (defvar font-lock-constant-face)
3445 (defsubst cperl-postpone-fontification (b e type val &optional now)
3446 ;; Do after syntactic fontification?
3447 (if cperl-syntaxify-by-font-lock
3448 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3449 (put-text-property b e type val)))
3450
3451 ;;; Here is how the global structures (those which cannot be
3452 ;;; recognized locally) are marked:
3453 ;; a) PODs:
3454 ;; Start-to-end is marked `in-pod' ==> t
3455 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3456 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3457 ;; b) HEREs:
3458 ;; Start-to-end is marked `here-doc-group' ==> t
3459 ;; The body is marked `syntax-type' ==> `here-doc'
3460 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3461 ;; c) FORMATs:
3462 ;; First line (to =) marked `first-format-line' ==> t
3463 ;; After-this--to-end is marked `syntax-type' ==> `format'
3464 ;; d) 'Q'uoted string:
3465 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3466 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3467 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3468 ;; e) Attributes of subroutines: `attrib-group' ==> t
3469 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3470 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3471
3472 ;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3473 ;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3474
3475 (defun cperl-unwind-to-safe (before &optional end)
3476 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3477 (let ((pos (point)) opos)
3478 (while (and pos (progn
3479 (beginning-of-line)
3480 (get-text-property (setq pos (point)) 'syntax-type)))
3481 (setq opos pos
3482 pos (cperl-beginning-of-property pos 'syntax-type))
3483 (if (eq pos (point-min))
3484 (setq pos nil))
3485 (if pos
3486 (if before
3487 (progn
3488 (goto-char (cperl-1- pos))
3489 (beginning-of-line)
3490 (setq pos (point)))
3491 (goto-char (setq pos (cperl-1- pos))))
3492 ;; Up to the start
3493 (goto-char (point-min))))
3494 ;; Skip empty lines
3495 (and (looking-at "\n*=")
3496 (/= 0 (skip-chars-backward "\n"))
3497 (forward-char))
3498 (setq pos (point))
3499 (if end
3500 ;; Do the same for end, going small steps
3501 (save-excursion
3502 (while (and end (get-text-property end 'syntax-type))
3503 (setq pos end
3504 end (next-single-property-change end 'syntax-type nil (point-max)))
3505 (if end (progn (goto-char end)
3506 (or (bolp) (forward-line 1))
3507 (setq end (point)))))
3508 (or end pos)))))
3509
3510 ;;; These are needed for byte-compile (at least with v19)
3511 (defvar cperl-nonoverridable-face)
3512 (defvar font-lock-variable-name-face)
3513 (defvar font-lock-function-name-face)
3514 (defvar font-lock-keyword-face)
3515 (defvar font-lock-builtin-face)
3516 (defvar font-lock-type-face)
3517 (defvar font-lock-comment-face)
3518 (defvar font-lock-warning-face)
3519
3520 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3521 "Syntaxically mark (and fontify) attributes of a subroutine.
3522 Should be called with the point before leading colon of an attribute."
3523 ;; Works *before* syntax recognition is done
3524 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3525 (let (st b p reset-st after-first (start (point)) start1 end1)
3526 (condition-case b
3527 (while (looking-at
3528 (concat
3529 "\\(" ; 1=optional? colon
3530 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3531 "\\)"
3532 (if after-first "?" "")
3533 ;; No space between name and paren allowed...
3534 "\\(\\sw+\\)" ; 3=name
3535 "\\((\\)?")) ; 4=optional paren
3536 (and (match-beginning 1)
3537 (cperl-postpone-fontification
3538 (match-beginning 0) (cperl-1+ (match-beginning 0))
3539 'face font-lock-constant-face))
3540 (setq start1 (match-beginning 3) end1 (match-end 3))
3541 (cperl-postpone-fontification start1 end1
3542 'face font-lock-constant-face)
3543 (goto-char end1) ; end or before `('
3544 (if (match-end 4) ; Have attribute arguments...
3545 (progn
3546 (if st nil
3547 (setq st (cperl-cached-syntax-table st-l))
3548 (modify-syntax-entry ?\( "()" st)
3549 (modify-syntax-entry ?\) ")(" st))
3550 (setq reset-st (syntax-table) p (point))
3551 (set-syntax-table st)
3552 (forward-sexp 1)
3553 (set-syntax-table reset-st)
3554 (setq reset-st nil)
3555 (cperl-commentify p (point) t))) ; mark as string
3556 (forward-comment (buffer-size))
3557 (setq after-first t))
3558 (error (message
3559 "L%d: attribute `%s': %s"
3560 (count-lines (point-min) (point))
3561 (and start1 end1 (buffer-substring start1 end1)) b)
3562 (setq start nil)))
3563 (and start
3564 (progn
3565 (put-text-property start (point)
3566 'attrib-group (if (looking-at "{") t 0))
3567 (and pos
3568 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3569 ;; Apparently, we do not need `multiline': faces added now
3570 (put-text-property (+ 3 pos) (cperl-1+ (point))
3571 'syntax-type 'sub-decl))
3572 (and b-fname ; Fontify here: the following condition
3573 (cperl-postpone-fontification ; is too hard to determine by
3574 b-fname e-fname 'face ; a REx, so do it here
3575 (if (looking-at "{")
3576 font-lock-function-name-face
3577 font-lock-variable-name-face)))))
3578 ;; now restore the initial state
3579 (if st
3580 (progn
3581 (modify-syntax-entry ?\( "." st)
3582 (modify-syntax-entry ?\) "." st)))
3583 (if reset-st
3584 (set-syntax-table reset-st))))
3585
3586 (defsubst cperl-look-at-leading-count (is-x-REx e)
3587 (if (and
3588 (< (point) e)
3589 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3590 (1- e) t)) ; return nil on failure, no moving
3591 (if (eq ?\{ (preceding-char)) nil
3592 (cperl-postpone-fontification
3593 (1- (point)) (point)
3594 'face font-lock-warning-face))))
3595
3596 ;;; Debugging this may require (setq max-specpdl-size 2000)...
3597 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3598 "Scans the buffer for hard-to-parse Perl constructions.
3599 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3600 the sections using `cperl-pod-head-face', `cperl-pod-face',
3601 `cperl-here-face'."
3602 (interactive)
3603 (or min (setq min (point-min)
3604 cperl-syntax-state nil
3605 cperl-syntax-done-to min))
3606 (or max (setq max (point-max)))
3607 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3608 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3609 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3610 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3611 (modified (buffer-modified-p)) overshoot is-o-REx
3612 (after-change-functions nil)
3613 (cperl-font-locking t)
3614 (use-syntax-state (and cperl-syntax-state
3615 (>= min (car cperl-syntax-state))))
3616 (state-point (if use-syntax-state
3617 (car cperl-syntax-state)
3618 (point-min)))
3619 (state (if use-syntax-state
3620 (cdr cperl-syntax-state)))
3621 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3622 (st-l (list nil)) (err-l (list nil))
3623 ;; Somehow font-lock may be not loaded yet...
3624 ;; (e.g., when building TAGS via command-line call)
3625 (font-lock-string-face (if (boundp 'font-lock-string-face)
3626 font-lock-string-face
3627 'font-lock-string-face))
3628 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3629 font-lock-constant-face
3630 'font-lock-constant-face))
3631 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3632 (if (boundp 'font-lock-function-name-face)
3633 font-lock-function-name-face
3634 'font-lock-function-name-face))
3635 (font-lock-variable-name-face ; interpolated vars and ({})-code
3636 (if (boundp 'font-lock-variable-name-face)
3637 font-lock-variable-name-face
3638 'font-lock-variable-name-face))
3639 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3640 (if (boundp 'font-lock-function-name-face)
3641 font-lock-function-name-face
3642 'font-lock-function-name-face))
3643 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3644 (if (boundp 'font-lock-constant-face)
3645 font-lock-constant-face
3646 'font-lock-constant-face))
3647 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3648 (if (boundp 'font-lock-builtin-face)
3649 font-lock-builtin-face
3650 'font-lock-builtin-face))
3651 (font-lock-comment-face
3652 (if (boundp 'font-lock-comment-face)
3653 font-lock-comment-face
3654 'font-lock-comment-face))
3655 (font-lock-warning-face
3656 (if (boundp 'font-lock-warning-face)
3657 font-lock-warning-face
3658 'font-lock-warning-face))
3659 (my-cperl-REx-ctl-face ; (|)
3660 (if (boundp 'font-lock-keyword-face)
3661 font-lock-keyword-face
3662 'font-lock-keyword-face))
3663 (my-cperl-REx-modifiers-face ; //gims
3664 (if (boundp 'cperl-nonoverridable-face)
3665 cperl-nonoverridable-face
3666 'cperl-nonoverridable-face))
3667 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3668 (if (boundp 'font-lock-type-face)
3669 font-lock-type-face
3670 'font-lock-type-face))
3671 (stop-point (if ignore-max
3672 (point-max)
3673 max))
3674 (search
3675 (concat
3676 "\\(\\`\n?\\|^\n\\)=" ; POD
3677 "\\|"
3678 ;; One extra () before this:
3679 "<<" ; HERE-DOC
3680 "\\(" ; 1 + 1
3681 ;; First variant "BLAH" or just ``.
3682 "[ \t]*" ; Yes, whitespace is allowed!
3683 "\\([\"'`]\\)" ; 2 + 1 = 3
3684 "\\([^\"'`\n]*\\)" ; 3 + 1
3685 "\\3"
3686 "\\|"
3687 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3688 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3689 ;; Do not have <<= or << 30 or <<30 or << $blah.
3690 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3691 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3692 "\\)"
3693 "\\|"
3694 ;; 1+6 extra () before this:
3695 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3696 (if cperl-use-syntax-table-text-property
3697 (concat
3698 "\\|"
3699 ;; 1+6+2=9 extra () before this:
3700 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3701 "\\|"
3702 ;; 1+6+2+1=10 extra () before this:
3703 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3704 "\\|"
3705 ;; 1+6+2+1+1=11 extra () before this
3706 "\\<sub\\>" ; sub with proto/attr
3707 "\\("
3708 cperl-white-and-comment-rex
3709 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3710 "\\("
3711 cperl-maybe-white-and-comment-rex
3712 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3713 "\\|"
3714 ;; 1+6+2+1+1+6=17 extra () before this:
3715 "\\$\\(['{]\\)" ; $' or ${foo}
3716 "\\|"
3717 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3718 ;; we do not support intervening comments...):
3719 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3720 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3721 "\\|"
3722 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3723 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3724 "\\|"
3725 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3726 ""))))
3727 (unwind-protect
3728 (progn
3729 (save-excursion
3730 (or non-inter
3731 (message "Scanning for \"hard\" Perl constructions..."))
3732 ;;(message "find: %s --> %s" min max)
3733 (and cperl-pod-here-fontify
3734 ;; We had evals here, do not know why...
3735 (setq face cperl-pod-face
3736 head-face cperl-pod-head-face
3737 here-face cperl-here-face))
3738 (remove-text-properties min max
3739 '(syntax-type t in-pod t syntax-table t
3740 attrib-group t
3741 REx-interpolated t
3742 cperl-postpone t
3743 syntax-subtype t
3744 rear-nonsticky t
3745 front-sticky t
3746 here-doc-group t
3747 first-format-line t
3748 REx-part2 t
3749 indentable t))
3750 ;; Need to remove face as well...
3751 (goto-char min)
3752 (and (eq system-type 'emx)
3753 (eq (point) 1)
3754 (let ((case-fold-search t))
3755 (looking-at "extproc[ \t]")) ; Analogue of #!
3756 (cperl-commentify min
3757 (save-excursion (end-of-line) (point))
3758 nil))
3759 (while (and
3760 (< (point) max)
3761 (re-search-forward search max t))
3762 (setq tmpend nil) ; Valid for most cases
3763 (setq b (match-beginning 0)
3764 state (save-excursion (parse-partial-sexp
3765 state-point b nil nil state))
3766 state-point b)
3767 (cond
3768 ;; 1+6+2+1+1+6=17 extra () before this:
3769 ;; "\\$\\(['{]\\)"
3770 ((match-beginning 18) ; $' or ${foo}
3771 (if (eq (preceding-char) ?\') ; $'
3772 (progn
3773 (setq b (1- (point))
3774 state (parse-partial-sexp
3775 state-point (1- b) nil nil state)
3776 state-point (1- b))
3777 (if (nth 3 state) ; in string
3778 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3779 (goto-char (1+ b)))
3780 ;; else: ${
3781 (setq bb (match-beginning 0))
3782 (cperl-modify-syntax-type bb cperl-st-punct)))
3783 ;; No processing in strings/comments beyond this point:
3784 ((or (nth 3 state) (nth 4 state))
3785 t) ; Do nothing in comment/string
3786 ((match-beginning 1) ; POD section
3787 ;; "\\(\\`\n?\\|^\n\\)="
3788 (setq b (match-beginning 0)
3789 state (parse-partial-sexp
3790 state-point b nil nil state)
3791 state-point b)
3792 (if (or (nth 3 state) (nth 4 state)
3793 (looking-at "cut\\>"))
3794 (if (or (nth 3 state) (nth 4 state) ignore-max)
3795 nil ; Doing a chunk only
3796 (message "=cut is not preceded by a POD section")
3797 (or (car err-l) (setcar err-l (point))))
3798 (beginning-of-line)
3799
3800 (setq b (point)
3801 bb b
3802 tb (match-beginning 0)
3803 b1 nil) ; error condition
3804 ;; We do not search to max, since we may be called from
3805 ;; some hook of fontification, and max is random
3806 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3807 (progn
3808 (goto-char b)
3809 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3810 (progn
3811 (message "=cut is not preceded by an empty line")
3812 (setq b1 t)
3813 (or (car err-l) (setcar err-l b))))))
3814 (beginning-of-line 2) ; An empty line after =cut is not POD!
3815 (setq e (point))
3816 (and (> e max)
3817 (progn
3818 (remove-text-properties
3819 max e '(syntax-type t in-pod t syntax-table t
3820 attrib-group t
3821 REx-interpolated t
3822 cperl-postpone t
3823 syntax-subtype t
3824 here-doc-group t
3825 rear-nonsticky t
3826 front-sticky t
3827 first-format-line t
3828 REx-part2 t
3829 indentable t))
3830 (setq tmpend tb)))
3831 (put-text-property b e 'in-pod t)
3832 (put-text-property b e 'syntax-type 'in-pod)
3833 (goto-char b)
3834 (while (re-search-forward "\n\n[ \t]" e t)
3835 ;; We start 'pod 1 char earlier to include the preceding line
3836 (beginning-of-line)
3837 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3838 (cperl-put-do-not-fontify b (point) t)
3839 ;; mark the non-literal parts as PODs
3840 (if cperl-pod-here-fontify
3841 (cperl-postpone-fontification b (point) 'face face t))
3842 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3843 (beginning-of-line)
3844 (setq b (point)))
3845 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3846 (cperl-put-do-not-fontify (point) e t)
3847 (if cperl-pod-here-fontify
3848 (progn
3849 ;; mark the non-literal parts as PODs
3850 (cperl-postpone-fontification (point) e 'face face t)
3851 (goto-char bb)
3852 (if (looking-at
3853 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3854 ;; mark the headers
3855 (cperl-postpone-fontification
3856 (match-beginning 1) (match-end 1)
3857 'face head-face))
3858 (while (re-search-forward
3859 ;; One paragraph
3860 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3861 e 'toend)
3862 ;; mark the headers
3863 (cperl-postpone-fontification
3864 (match-beginning 1) (match-end 1)
3865 'face head-face))))
3866 (cperl-commentify bb e nil)
3867 (goto-char e)
3868 (or (eq e (point-max))
3869 (forward-char -1)))) ; Prepare for immediate POD start.
3870 ;; Here document
3871 ;; We can do many here-per-line;
3872 ;; but multiline quote on the same line as <<HERE confuses us...
3873 ;; ;; One extra () before this:
3874 ;;"<<"
3875 ;; "\\(" ; 1 + 1
3876 ;; ;; First variant "BLAH" or just ``.
3877 ;; "[ \t]*" ; Yes, whitespace is allowed!
3878 ;; "\\([\"'`]\\)" ; 2 + 1
3879 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3880 ;; "\\3"
3881 ;; "\\|"
3882 ;; ;; Second variant: Identifier or \ID or empty
3883 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3884 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3885 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3886 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3887 ;; "\\)"
3888 ((match-beginning 2) ; 1 + 1
3889 (setq b (point)
3890 tb (match-beginning 0)
3891 c (and ; not HERE-DOC
3892 (match-beginning 5)
3893 (save-match-data
3894 (or (looking-at "[ \t]*(") ; << function_call()
3895 (save-excursion ; 1 << func_name, or $foo << 10
3896 (condition-case nil
3897 (progn
3898 (goto-char tb)
3899 ;;; XXX What to do: foo <<bar ???
3900 ;;; XXX Need to support print {a} <<B ???
3901 (forward-sexp -1)
3902 (save-match-data
3903 ; $foo << b; $f .= <<B;
3904 ; ($f+1) << b; a($f) . <<B;
3905 ; foo 1, <<B; $x{a} <<b;
3906 (cond
3907 ((looking-at "[0-9$({]")
3908 (forward-sexp 1)
3909 (and
3910 (looking-at "[ \t]*<<")
3911 (condition-case nil
3912 ;; print $foo <<EOF
3913 (progn
3914 (forward-sexp -2)
3915 (not
3916 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
3917 (error t)))))))
3918 (error nil))) ; func(<<EOF)
3919 (and (not (match-beginning 6)) ; Empty
3920 (looking-at
3921 "[ \t]*[=0-9$@%&(]"))))))
3922 (if c ; Not here-doc
3923 nil ; Skip it.
3924 (setq c (match-end 2)) ; 1 + 1
3925 (if (match-beginning 5) ;4 + 1
3926 (setq b1 (match-beginning 5) ; 4 + 1
3927 e1 (match-end 5)) ; 4 + 1
3928 (setq b1 (match-beginning 4) ; 3 + 1
3929 e1 (match-end 4))) ; 3 + 1
3930 (setq tag (buffer-substring b1 e1)
3931 qtag (regexp-quote tag))
3932 (cond (cperl-pod-here-fontify
3933 ;; Highlight the starting delimiter
3934 (cperl-postpone-fontification
3935 b1 e1 'face my-cperl-delimiters-face)
3936 (cperl-put-do-not-fontify b1 e1 t)))
3937 (forward-line)
3938 (setq i (point))
3939 (if end-of-here-doc
3940 (goto-char end-of-here-doc))
3941 (setq b (point))
3942 ;; We do not search to max, since we may be called from
3943 ;; some hook of fontification, and max is random
3944 (or (and (re-search-forward (concat "^" qtag "$")
3945 stop-point 'toend)
3946 ;;;(eq (following-char) ?\n) ; XXXX WHY???
3947 )
3948 (progn ; Pretend we matched at the end
3949 (goto-char (point-max))
3950 (re-search-forward "\\'")
3951 (message "End of here-document `%s' not found." tag)
3952 (or (car err-l) (setcar err-l b))))
3953 (if cperl-pod-here-fontify
3954 (progn
3955 ;; Highlight the ending delimiter
3956 (cperl-postpone-fontification
3957 (match-beginning 0) (match-end 0)
3958 'face my-cperl-delimiters-face)
3959 (cperl-put-do-not-fontify b (match-end 0) t)
3960 ;; Highlight the HERE-DOC
3961 (cperl-postpone-fontification b (match-beginning 0)
3962 'face here-face)))
3963 (setq e1 (cperl-1+ (match-end 0)))
3964 (put-text-property b (match-beginning 0)
3965 'syntax-type 'here-doc)
3966 (put-text-property (match-beginning 0) e1
3967 'syntax-type 'here-doc-delim)
3968 (put-text-property b e1 'here-doc-group t)
3969 ;; This makes insertion at the start of HERE-DOC update
3970 ;; the whole construct:
3971 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
3972 (cperl-commentify b e1 nil)
3973 (cperl-put-do-not-fontify b (match-end 0) t)
3974 ;; Cache the syntax info...
3975 (setq cperl-syntax-state (cons state-point state))
3976 ;; ... and process the rest of the line...
3977 (setq overshoot
3978 (elt ; non-inter ignore-max
3979 (cperl-find-pods-heres c i t end t e1) 1))
3980 (if (and overshoot (> overshoot (point)))
3981 (goto-char overshoot)
3982 (setq overshoot e1))
3983 (if (> e1 max)
3984 (setq tmpend tb))))
3985 ;; format
3986 ((match-beginning 8)
3987 ;; 1+6=7 extra () before this:
3988 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3989 (setq b (point)
3990 name (if (match-beginning 8) ; 7 + 1
3991 (buffer-substring (match-beginning 8) ; 7 + 1
3992 (match-end 8)) ; 7 + 1
3993 "")
3994 tb (match-beginning 0))
3995 (setq argument nil)
3996 (put-text-property (save-excursion
3997 (beginning-of-line)
3998 (point))
3999 b 'first-format-line 't)
4000 (if cperl-pod-here-fontify
4001 (while (and (eq (forward-line) 0)
4002 (not (looking-at "^[.;]$")))
4003 (cond
4004 ((looking-at "^#")) ; Skip comments
4005 ((and argument ; Skip argument multi-lines
4006 (looking-at "^[ \t]*{"))
4007 (forward-sexp 1)
4008 (setq argument nil))
4009 (argument ; Skip argument lines
4010 (setq argument nil))
4011 (t ; Format line
4012 (setq b1 (point))
4013 (setq argument (looking-at "^[^\n]*[@^]"))
4014 (end-of-line)
4015 ;; Highlight the format line
4016 (cperl-postpone-fontification b1 (point)
4017 'face font-lock-string-face)
4018 (cperl-commentify b1 (point) nil)
4019 (cperl-put-do-not-fontify b1 (point) t))))
4020 ;; We do not search to max, since we may be called from
4021 ;; some hook of fontification, and max is random
4022 (re-search-forward "^[.;]$" stop-point 'toend))
4023 (beginning-of-line)
4024 (if (looking-at "^\\.$") ; ";" is not supported yet
4025 (progn
4026 ;; Highlight the ending delimiter
4027 (cperl-postpone-fontification (point) (+ (point) 2)
4028 'face font-lock-string-face)
4029 (cperl-commentify (point) (+ (point) 2) nil)
4030 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4031 (message "End of format `%s' not found." name)
4032 (or (car err-l) (setcar err-l b)))
4033 (forward-line)
4034 (if (> (point) max)
4035 (setq tmpend tb))
4036 (put-text-property b (point) 'syntax-type 'format))
4037 ;; qq-like String or Regexp:
4038 ((or (match-beginning 10) (match-beginning 11))
4039 ;; 1+6+2=9 extra () before this:
4040 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4041 ;; "\\|"
4042 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4043 (setq b1 (if (match-beginning 10) 10 11)
4044 argument (buffer-substring
4045 (match-beginning b1) (match-end b1))
4046 b (point) ; end of qq etc
4047 i b
4048 c (char-after (match-beginning b1))
4049 bb (char-after (1- (match-beginning b1))) ; tmp holder
4050 ;; bb == "Not a stringy"
4051 bb (if (eq b1 10) ; user variables/whatever
4052 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4053 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4054 ((eq bb ?\:) ; $opt::s
4055 (eq (char-after
4056 (- (match-beginning b1) 2))
4057 ?\:))
4058 ((eq bb ?\>) ; $foo->s
4059 (eq (char-after
4060 (- (match-beginning b1) 2))
4061 ?\-))
4062 ((eq bb ?\&)
4063 (not (eq (char-after ; &&m/blah/
4064 (- (match-beginning b1) 2))
4065 ?\&)))
4066 (t t)))
4067 ;; <file> or <$file>
4068 (and (eq c ?\<)
4069 ;; Do not stringify <FH>, <$fh> :
4070 (save-match-data
4071 (looking-at
4072 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4073 tb (match-beginning 0))
4074 (goto-char (match-beginning b1))
4075 (cperl-backward-to-noncomment (point-min))
4076 (or bb
4077 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4078 (setq argument ""
4079 b1 nil
4080 bb ; Not a regexp?
4081 (not
4082 ;; What is below: regexp-p?
4083 (and
4084 (or (memq (preceding-char)
4085 (append (if (memq c '(?\? ?\<))
4086 ;; $a++ ? 1 : 2
4087 "~{(=|&*!,;:["
4088 "~{(=|&+-*!,;:[") nil))
4089 (and (eq (preceding-char) ?\})
4090 (cperl-after-block-p (point-min)))
4091 (and (eq (char-syntax (preceding-char)) ?w)
4092 (progn
4093 (forward-sexp -1)
4094 ;; After these keywords `/' starts a RE. One should add all the
4095 ;; functions/builtins which expect an argument, but ...
4096 (if (eq (preceding-char) ?-)
4097 ;; -d ?foo? is a RE
4098 (looking-at "[a-zA-Z]\\>")
4099 (and
4100 (not (memq (preceding-char)
4101 '(?$ ?@ ?& ?%)))
4102 (looking-at
4103 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4104 (and (eq (preceding-char) ?.)
4105 (eq (char-after (- (point) 2)) ?.))
4106 (bobp))
4107 ;; m|blah| ? foo : bar;
4108 (not
4109 (and (eq c ?\?)
4110 cperl-use-syntax-table-text-property
4111 (not (bobp))
4112 (progn
4113 (forward-char -1)
4114 (looking-at "\\s|"))))))
4115 b (1- b))
4116 ;; s y tr m
4117 ;; Check for $a -> y
4118 (setq b1 (preceding-char)
4119 go (point))
4120 (if (and (eq b1 ?>)
4121 (eq (char-after (- go 2)) ?-))
4122 ;; Not a regexp
4123 (setq bb t))))
4124 (or bb
4125 (progn
4126 (goto-char b)
4127 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4128 (goto-char (match-end 0))
4129 (skip-chars-forward " \t\n\f"))
4130 (cond ((and (eq (following-char) ?\})
4131 (eq b1 ?\{))
4132 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4133 (goto-char (1- go))
4134 (skip-chars-backward " \t\n\f")
4135 (if (memq (preceding-char) (append "$@%&*" nil))
4136 (setq bb t) ; @{y}
4137 (condition-case nil
4138 (forward-sexp -1)
4139 (error nil)))
4140 (if (or bb
4141 (looking-at ; $foo -> {s}
4142 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4143 (and ; $foo[12] -> {s}
4144 (memq (following-char) '(?\{ ?\[))
4145 (progn
4146 (forward-sexp 1)
4147 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4148 (setq bb t)
4149 (goto-char b)))
4150 ((and (eq (following-char) ?=)
4151 (eq (char-after (1+ (point))) ?\>))
4152 ;; Check for { foo => 1, s => 2 }
4153 ;; Apparently s=> is never a substitution...
4154 (setq bb t))
4155 ((and (eq (following-char) ?:)
4156 (eq b1 ?\{) ; Check for $ { s::bar }
4157 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4158 (progn
4159 (goto-char (1- go))
4160 (skip-chars-backward " \t\n\f")
4161 (memq (preceding-char)
4162 (append "$@%&*" nil))))
4163 (setq bb t))
4164 ((eobp)
4165 (setq bb t)))))
4166 (if bb
4167 (goto-char i)
4168 ;; Skip whitespace and comments...
4169 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4170 (goto-char (match-end 0))
4171 (skip-chars-forward " \t\n\f"))
4172 (if (> (point) b)
4173 (put-text-property b (point) 'syntax-type 'prestring))
4174 ;; qtag means two-arg matcher, may be reset to
4175 ;; 2 or 3 later if some special quoting is needed.
4176 ;; e1 means matching-char matcher.
4177 (setq b (point) ; before the first delimiter
4178 ;; has 2 args
4179 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4180 ;; We do not search to max, since we may be called from
4181 ;; some hook of fontification, and max is random
4182 i (cperl-forward-re stop-point end
4183 i2
4184 st-l err-l argument)
4185 ;; If `go', then it is considered as 1-arg, `b1' is nil
4186 ;; as in s/foo//x; the point is before final "slash"
4187 b1 (nth 1 i) ; start of the second part
4188 tag (nth 2 i) ; ender-char, true if second part
4189 ; is with matching chars []
4190 go (nth 4 i) ; There is a 1-char part after the end
4191 i (car i) ; intermediate point
4192 e1 (point) ; end
4193 ;; Before end of the second part if non-matching: ///
4194 tail (if (and i (not tag))
4195 (1- e1))
4196 e (if i i e1) ; end of the first part
4197 qtag nil ; need to preserve backslashitis
4198 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4199 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4200 ;; Commenting \\ is dangerous, what about ( ?
4201 (and i tail
4202 (eq (char-after i) ?\\)
4203 (setq qtag t))
4204 (and (if go (looking-at ".\\sw*x")
4205 (looking-at "\\sw*x")) ; qr//x
4206 (setq is-x-REx t))
4207 (and (if go (looking-at ".\\sw*o")
4208 (looking-at "\\sw*o")) ; //o
4209 (setq is-o-REx t))
4210 (if (null i)
4211 ;; Considered as 1arg form
4212 (progn
4213 (cperl-commentify b (point) t)
4214 (put-text-property b (point) 'syntax-type 'string)
4215 (if (or is-x-REx
4216 ;; ignore other text properties:
4217 (string-match "^qw$" argument))
4218 (put-text-property b (point) 'indentable t))
4219 (and go
4220 (setq e1 (cperl-1+ e1))
4221 (or (eobp)
4222 (forward-char 1))))
4223 (cperl-commentify b i t)
4224 (if (looking-at "\\sw*e") ; s///e
4225 (progn
4226 ;; Cache the syntax info...
4227 (setq cperl-syntax-state (cons state-point state))
4228 (and
4229 ;; silent:
4230 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4231 ;; Error
4232 (goto-char (1+ max)))
4233 (if (and tag (eq (preceding-char) ?\>))
4234 (progn
4235 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4236 (cperl-modify-syntax-type i cperl-st-bra)))
4237 (put-text-property b i 'syntax-type 'string)
4238 (put-text-property i (point) 'syntax-type 'multiline)
4239 (if is-x-REx
4240 (put-text-property b i 'indentable t)))
4241 (cperl-commentify b1 (point) t)
4242 (put-text-property b (point) 'syntax-type 'string)
4243 (if is-x-REx
4244 (put-text-property b i 'indentable t))
4245 (if qtag
4246 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4247 (setq tail nil)))
4248 ;; Now: tail: if the second part is non-matching without ///e
4249 (if (eq (char-syntax (following-char)) ?w)
4250 (progn
4251 (forward-word 1) ; skip modifiers s///s
4252 (if tail (cperl-commentify tail (point) t))
4253 (cperl-postpone-fontification
4254 e1 (point) 'face my-cperl-REx-modifiers-face)))
4255 ;; Check whether it is m// which means "previous match"
4256 ;; and highlight differently
4257 (setq is-REx
4258 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4259 (or (not (= (length argument) 0))
4260 (not (eq c ?\<)))))
4261 (if (and is-REx
4262 (eq e (+ 2 b))
4263 ;; split // *is* using zero-pattern
4264 (save-excursion
4265 (condition-case nil
4266 (progn
4267 (goto-char tb)
4268 (forward-sexp -1)
4269 (not (looking-at "split\\>")))
4270 (error t))))
4271 (cperl-postpone-fontification
4272 b e 'face font-lock-warning-face)
4273 (if (or i2 ; Has 2 args
4274 (and cperl-fontify-m-as-s
4275 (or
4276 (string-match "^\\(m\\|qr\\)$" argument)
4277 (and (eq 0 (length argument))
4278 (not (eq ?\< (char-after b)))))))
4279 (progn
4280 (cperl-postpone-fontification
4281 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4282 (cperl-postpone-fontification
4283 (1- e) e 'face my-cperl-delimiters-face)))
4284 (if (and is-REx cperl-regexp-scan)
4285 ;; Process RExen: embedded comments, charclasses and ]
4286 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4287 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4288 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4289 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4290 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4291 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4292 (save-excursion
4293 (goto-char (1+ b))
4294 ;; First
4295 (cperl-look-at-leading-count is-x-REx e)
4296 (setq hairy-RE
4297 (concat
4298 (if is-x-REx
4299 (if (eq (char-after b) ?\#)
4300 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4301 "\\((\\?#\\)\\|\\(#\\)")
4302 ;; keep the same count: add a fake group
4303 (if (eq (char-after b) ?\#)
4304 "\\((\\?\\\\#\\)\\(\\)"
4305 "\\((\\?#\\)\\(\\)"))
4306 "\\|"
4307 "\\(\\[\\)" ; 3=[
4308 "\\|"
4309 "\\(]\\)" ; 4=]
4310 "\\|"
4311 ;; XXXX Will not be able to use it in s)))
4312 (if (eq (char-after b) ?\) )
4313 "\\())))\\)" ; Will never match
4314 (if (eq (char-after b) ?? )
4315 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4316 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4317 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4318 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4319 "\\(" ;; XXXX 1-char variables, exc. |()\s
4320 "[$@]"
4321 "\\("
4322 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4323 "\\|"
4324 "{[^{}]*}" ; only one-level allowed
4325 "\\|"
4326 "[^{(|) \t\r\n\f]"
4327 "\\)"
4328 "\\(" ;;8,9:code part of array/hash elt
4329 "\\(" "->" "\\)?"
4330 "\\[[^][]*\\]"
4331 "\\|"
4332 "{[^{}]*}"
4333 "\\)*"
4334 ;; XXXX: what if u is delim?
4335 "\\|"
4336 "[)^|$.*?+]"
4337 "\\|"
4338 "{[0-9]+}"
4339 "\\|"
4340 "{[0-9]+,[0-9]*}"
4341 "\\|"
4342 "\\\\[luLUEQbBAzZG]"
4343 "\\|"
4344 "(" ; Group opener
4345 "\\(" ; 10 group opener follower
4346 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4347 "\\|"
4348 "\\?[:=!>?{]" ; "?" something
4349 "\\|"
4350 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4351 "\\|"
4352 "\\?([0-9]+)" ; (?(1)foo|bar)
4353 "\\|"
4354 "\\?<[=!]"
4355 ;;;"\\|"
4356 ;;; "\\?"
4357 "\\)?"
4358 "\\)"
4359 "\\|"
4360 "\\\\\\(.\\)" ; 12=\SYMBOL
4361 ))
4362 (while
4363 (and (< (point) (1- e))
4364 (re-search-forward hairy-RE (1- e) 'to-end))
4365 (goto-char (match-beginning 0))
4366 (setq REx-subgr-start (point)
4367 was-subgr (following-char))
4368 (cond
4369 ((match-beginning 6) ; 0-length builtins, groups
4370 (goto-char (match-end 0))
4371 (if (match-beginning 11)
4372 (goto-char (match-beginning 11)))
4373 (if (>= (point) e)
4374 (goto-char (1- e)))
4375 (cperl-postpone-fontification
4376 (match-beginning 0) (point)
4377 'face
4378 (cond
4379 ((eq was-subgr ?\) )
4380 (condition-case nil
4381 (save-excursion
4382 (forward-sexp -1)
4383 (if (> (point) b)
4384 (if (if (eq (char-after b) ?? )
4385 (looking-at "(\\\\\\?")
4386 (eq (char-after (1+ (point))) ?\?))
4387 my-cperl-REx-0length-face
4388 my-cperl-REx-ctl-face)
4389 font-lock-warning-face))
4390 (error font-lock-warning-face)))
4391 ((eq was-subgr ?\| )
4392 my-cperl-REx-ctl-face)
4393 ((eq was-subgr ?\$ )
4394 (if (> (point) (1+ REx-subgr-start))
4395 (progn
4396 (put-text-property
4397 (match-beginning 0) (point)
4398 'REx-interpolated
4399 (if is-o-REx 0
4400 (if (and (eq (match-beginning 0)
4401 (1+ b))
4402 (eq (point)
4403 (1- e))) 1 t)))
4404 font-lock-variable-name-face)
4405 my-cperl-REx-spec-char-face))
4406 ((memq was-subgr (append "^." nil) )
4407 my-cperl-REx-spec-char-face)
4408 ((eq was-subgr ?\( )
4409 (if (not (match-beginning 10))
4410 my-cperl-REx-ctl-face
4411 my-cperl-REx-0length-face))
4412 (t my-cperl-REx-0length-face)))
4413 (if (and (memq was-subgr (append "(|" nil))
4414 (not (string-match "(\\?[-imsx]+)"
4415 (match-string 0))))
4416 (cperl-look-at-leading-count is-x-REx e))
4417 (setq was-subgr nil)) ; We do stuff here
4418 ((match-beginning 12) ; \SYMBOL
4419 (forward-char 2)
4420 (if (>= (point) e)
4421 (goto-char (1- e))
4422 ;; How many chars to not highlight:
4423 ;; 0-len special-alnums in other branch =>
4424 ;; Generic: \non-alnum (1), \alnum (1+face)
4425 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4426 (setq REx-subgr-start (point)
4427 qtag (preceding-char))
4428 (cperl-postpone-fontification
4429 (- (point) 2) (- (point) 1) 'face
4430 (if (memq qtag
4431 (append "ghijkmoqvFHIJKMORTVY" nil))
4432 font-lock-warning-face
4433 my-cperl-REx-0length-face))
4434 (if (and (eq (char-after b) qtag)
4435 (memq qtag (append ".])^$|*?+" nil)))
4436 (progn
4437 (if (and cperl-use-syntax-table-text-property
4438 (eq qtag ?\) ))
4439 (put-text-property
4440 REx-subgr-start (1- (point))
4441 'syntax-table cperl-st-punct))
4442 (cperl-postpone-fontification
4443 (1- (point)) (point) 'face
4444 ; \] can't appear below
4445 (if (memq qtag (append ".]^$" nil))
4446 'my-cperl-REx-spec-char-face
4447 (if (memq qtag (append "*?+" nil))
4448 'my-cperl-REx-0length-face
4449 'my-cperl-REx-ctl-face))))) ; )|
4450 ;; Test for arguments:
4451 (cond
4452 ;; This is not pretty: the 5.8.7 logic:
4453 ;; \0numx -> octal (up to total 3 dig)
4454 ;; \DIGIT -> backref unless \0
4455 ;; \DIGITs -> backref if valid
4456 ;; otherwise up to 3 -> octal
4457 ;; Do not try to distinguish, we guess
4458 ((or (and (memq qtag (append "01234567" nil))
4459 (re-search-forward
4460 "\\=[01234567]?[01234567]?"
4461 (1- e) 'to-end))
4462 (and (memq qtag (append "89" nil))
4463 (re-search-forward
4464 "\\=[0123456789]*" (1- e) 'to-end))
4465 (and (eq qtag ?x)
4466 (re-search-forward
4467 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4468 (1- e) 'to-end))
4469 (and (memq qtag (append "pPN" nil))
4470 (re-search-forward "\\={[^{}]+}\\|."
4471 (1- e) 'to-end))
4472 (eq (char-syntax qtag) ?w))
4473 (cperl-postpone-fontification
4474 (1- REx-subgr-start) (point)
4475 'face my-cperl-REx-length1-face))))
4476 (setq was-subgr nil)) ; We do stuff here
4477 ((match-beginning 3) ; [charclass]
4478 (forward-char 1)
4479 (if (eq (char-after b) ?^ )
4480 (and (eq (following-char) ?\\ )
4481 (eq (char-after (cperl-1+ (point)))
4482 ?^ )
4483 (forward-char 2))
4484 (and (eq (following-char) ?^ )
4485 (forward-char 1)))
4486 (setq argument b ; continue?
4487 tag nil ; list of POSIX classes
4488 qtag (point))
4489 (if (eq (char-after b) ?\] )
4490 (and (eq (following-char) ?\\ )
4491 (eq (char-after (cperl-1+ (point)))
4492 ?\] )
4493 (setq qtag (1+ qtag))
4494 (forward-char 2))
4495 (and (eq (following-char) ?\] )
4496 (forward-char 1)))
4497 ;; Apparently, I can't put \] into a charclass
4498 ;; in m]]: m][\\\]\]] produces [\\]]
4499 ;;; POSIX? [:word:] [:^word:] only inside []
4500 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4501 (while
4502 (and argument
4503 (re-search-forward
4504 (if (eq (char-after b) ?\] )
4505 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4506 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4507 (1- e) 'toend))
4508 ;; Is this ] an end of POSIX class?
4509 (if (save-excursion
4510 (and
4511 (search-backward "[" argument t)
4512 (< REx-subgr-start (point))
4513 (not
4514 (and ; Should work with delim = \
4515 (eq (preceding-char) ?\\ )
4516 (= (% (skip-chars-backward
4517 "\\\\") 2) 0)))
4518 (looking-at
4519 (cond
4520 ((eq (char-after b) ?\] )
4521 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4522 ((eq (char-after b) ?\: )
4523 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4524 ((eq (char-after b) ?^ )
4525 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4526 ((eq (char-syntax (char-after b))
4527 ?w)
4528 (concat
4529 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4530 (char-to-string (char-after b))
4531 "\\|\\sw\\)+:\]"))
4532 (t "\\\\*\\[:\\^?\\sw*:]")))
4533 (setq argument (point))))
4534 (setq tag (cons (cons argument (point))
4535 tag)
4536 argument (point)) ; continue
4537 (setq argument nil)))
4538 (and argument
4539 (message "Couldn't find end of charclass in a REx, pos=%s"
4540 REx-subgr-start))
4541 (if (and cperl-use-syntax-table-text-property
4542 (> (- (point) 2) REx-subgr-start))
4543 (put-text-property
4544 (1+ REx-subgr-start) (1- (point))
4545 'syntax-table cperl-st-punct))
4546 (cperl-postpone-fontification
4547 REx-subgr-start qtag
4548 'face my-cperl-REx-spec-char-face)
4549 (cperl-postpone-fontification
4550 (1- (point)) (point) 'face
4551 my-cperl-REx-spec-char-face)
4552 (if (eq (char-after b) ?\] )
4553 (cperl-postpone-fontification
4554 (- (point) 2) (1- (point))
4555 'face my-cperl-REx-0length-face))
4556 (while tag
4557 (cperl-postpone-fontification
4558 (car (car tag)) (cdr (car tag))
4559 'face my-cperl-REx-length1-face)
4560 (setq tag (cdr tag)))
4561 (setq was-subgr nil)) ; did facing already
4562 ;; Now rare stuff:
4563 ((and (match-beginning 2) ; #-comment
4564 (/= (match-beginning 2) (match-end 2)))
4565 (beginning-of-line 2)
4566 (if (> (point) e)
4567 (goto-char (1- e))))
4568 ((match-beginning 4) ; character "]"
4569 (setq was-subgr nil) ; We do stuff here
4570 (goto-char (match-end 0))
4571 (if cperl-use-syntax-table-text-property
4572 (put-text-property
4573 (1- (point)) (point)
4574 'syntax-table cperl-st-punct))
4575 (cperl-postpone-fontification
4576 (1- (point)) (point)
4577 'face font-lock-warning-face))
4578 ((match-beginning 5) ; before (?{}) (??{})
4579 (setq tag (match-end 0))
4580 (if (or (setq qtag
4581 (cperl-forward-group-in-re st-l))
4582 (and (>= (point) e)
4583 (setq qtag "no matching `)' found"))
4584 (and (not (eq (char-after (- (point) 2))
4585 ?\} ))
4586 (setq qtag "Can't find })")))
4587 (progn
4588 (goto-char (1- e))
4589 (message qtag))
4590 (cperl-postpone-fontification
4591 (1- tag) (1- (point))
4592 'face font-lock-variable-name-face)
4593 (cperl-postpone-fontification
4594 REx-subgr-start (1- tag)
4595 'face my-cperl-REx-spec-char-face)
4596 (cperl-postpone-fontification
4597 (1- (point)) (point)
4598 'face my-cperl-REx-spec-char-face)
4599 (if cperl-use-syntax-table-text-property
4600 (progn
4601 (put-text-property
4602 (- (point) 2) (1- (point))
4603 'syntax-table cperl-st-cfence)
4604 (put-text-property
4605 (+ REx-subgr-start 2)
4606 (+ REx-subgr-start 3)
4607 'syntax-table cperl-st-cfence))))
4608 (setq was-subgr nil))
4609 (t ; (?#)-comment
4610 ;; Inside "(" and "\" arn't special in any way
4611 ;; Works also if the outside delimiters are ().
4612 (or;;(if (eq (char-after b) ?\) )
4613 ;;(re-search-forward
4614 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4615 ;; (1- e) 'toend)
4616 (search-forward ")" (1- e) 'toend)
4617 ;;)
4618 (message
4619 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4620 REx-subgr-start))))
4621 (if (>= (point) e)
4622 (goto-char (1- e)))
4623 (cond
4624 (was-subgr
4625 (setq REx-subgr-end (point))
4626 (cperl-commentify
4627 REx-subgr-start REx-subgr-end nil)
4628 (cperl-postpone-fontification
4629 REx-subgr-start REx-subgr-end
4630 'face font-lock-comment-face))))))
4631 (if (and is-REx is-x-REx)
4632 (put-text-property (1+ b) (1- e)
4633 'syntax-subtype 'x-REx)))
4634 (if (and i2 e1 b1 (> e1 b1))
4635 (progn ; No errors finding the second part...
4636 (cperl-postpone-fontification
4637 (1- e1) e1 'face my-cperl-delimiters-face)
4638 (if (and (not (eobp))
4639 (assoc (char-after b) cperl-starters))
4640 (progn
4641 (cperl-postpone-fontification
4642 b1 (1+ b1) 'face my-cperl-delimiters-face)
4643 (put-text-property b1 (1+ b1)
4644 'REx-part2 t)))))
4645 (if (> (point) max)
4646 (setq tmpend tb))))
4647 ((match-beginning 17) ; sub with prototype or attribute
4648 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4649 ;;"\\<sub\\>\\(" ;12
4650 ;; cperl-white-and-comment-rex ;13
4651 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4652 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4653 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4654 (setq b1 (match-beginning 14) e1 (match-end 14))
4655 (if (memq (char-after (1- b))
4656 '(?\$ ?\@ ?\% ?\& ?\*))
4657 nil
4658 (goto-char b)
4659 (if (eq (char-after (match-beginning 17)) ?\( )
4660 (progn
4661 (cperl-commentify ; Prototypes; mark as string
4662 (match-beginning 17) (match-end 17) t)
4663 (goto-char (match-end 0))
4664 ;; Now look for attributes after prototype:
4665 (forward-comment (buffer-size))
4666 (and (looking-at ":[^:]")
4667 (cperl-find-sub-attrs st-l b1 e1 b)))
4668 ;; treat attributes without prototype
4669 (goto-char (match-beginning 17))
4670 (cperl-find-sub-attrs st-l b1 e1 b))))
4671 ;; 1+6+2+1+1+6+1=18 extra () before this:
4672 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4673 ((match-beginning 19) ; old $abc'efg syntax
4674 (setq bb (match-end 0))
4675 ;;;(if (nth 3 state) nil ; in string
4676 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4677 (goto-char bb))
4678 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4679 ;; "__\\(END\\|DATA\\)__"
4680 ((match-beginning 20) ; __END__, __DATA__
4681 (setq bb (match-end 0))
4682 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4683 (cperl-commentify b bb nil)
4684 (setq end t))
4685 ;; "\\\\\\(['`\"($]\\)"
4686 ((match-beginning 21)
4687 ;; Trailing backslash; make non-quoting outside string/comment
4688 (setq bb (match-end 0))
4689 (goto-char b)
4690 (skip-chars-backward "\\\\")
4691 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4692 (cperl-modify-syntax-type b cperl-st-punct)
4693 (goto-char bb))
4694 (t (error "Error in regexp of the sniffer")))
4695 (if (> (point) stop-point)
4696 (progn
4697 (if end
4698 (message "Garbage after __END__/__DATA__ ignored")
4699 (message "Unbalanced syntax found while scanning")
4700 (or (car err-l) (setcar err-l b)))
4701 (goto-char stop-point))))
4702 (setq cperl-syntax-state (cons state-point state)
4703 ;; Do not mark syntax as done past tmpend???
4704 cperl-syntax-done-to (or tmpend (max (point) max)))
4705 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4706 )
4707 (if (car err-l) (goto-char (car err-l))
4708 (or non-inter
4709 (message "Scanning for \"hard\" Perl constructions... done"))))
4710 (and (buffer-modified-p)
4711 (not modified)
4712 (set-buffer-modified-p nil))
4713 ;; I do not understand what this is doing here. It breaks font-locking
4714 ;; because it resets the syntax-table from font-lock-syntax-table to
4715 ;; cperl-mode-syntax-table.
4716 ;; (set-syntax-table cperl-mode-syntax-table)
4717 )
4718 (list (car err-l) overshoot)))
4719
4720 (defun cperl-find-pods-heres-region (min max)
4721 (interactive "r")
4722 (cperl-find-pods-heres min max))
4723
4724 (defun cperl-backward-to-noncomment (lim)
4725 ;; Stops at lim or after non-whitespace that is not in comment
4726 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4727 (let (stop p pr)
4728 (while (and (not stop) (> (point) (or lim (point-min))))
4729 (skip-chars-backward " \t\n\f" lim)
4730 (setq p (point))
4731 (beginning-of-line)
4732 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4733 '(pod here-doc here-doc-delim))
4734 (progn
4735 (cperl-unwind-to-safe nil)
4736 (setq pr (get-text-property (point) 'syntax-type))))
4737 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4738 (not (memq pr '(string prestring))))
4739 (progn (cperl-to-comment-or-eol) (bolp))
4740 (progn
4741 (skip-chars-backward " \t")
4742 (if (< p (point)) (goto-char p))
4743 (setq stop t))))))
4744
4745 ;; Used only in `cperl-calculate-indent'...
4746 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4747 ;; Positions is before ?\{. Checks whether it starts a block.
4748 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4749 (cperl-backward-to-noncomment (point-min))
4750 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4751 ; Label may be mixed up with `$blah :'
4752 (save-excursion (cperl-after-label))
4753 (get-text-property (cperl-1- (point)) 'attrib-group)
4754 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4755 (progn
4756 (backward-sexp)
4757 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4758 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4759 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4760 ;; sub bless::foo {}
4761 (progn
4762 (cperl-backward-to-noncomment (point-min))
4763 (and (eq (preceding-char) ?b)
4764 (progn
4765 (forward-sexp -1)
4766 (looking-at "sub[ \t\n\f#]")))))))))
4767
4768 ;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4769 ;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4770 ;;; may be a part of an in-statement construct, such as
4771 ;;; ${something()}, print {FH} $data.
4772 ;;; Moreover, one takes positive approach (looks for else,grep etc)
4773 ;;; another negative (looks for bless,tr etc)
4774 (defun cperl-after-block-p (lim &optional pre-block)
4775 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4776 Would not look before LIM. Assumes that LIM is a good place to begin a
4777 statement. The kind of block we treat here is one after which a new
4778 statement would start; thus the block in ${func()} does not count."
4779 (save-excursion
4780 (condition-case nil
4781 (progn
4782 (or pre-block (forward-sexp -1))
4783 (cperl-backward-to-noncomment lim)
4784 (or (eq (point) lim)
4785 ;; if () {} // sub f () {} // sub f :a(') {}
4786 (eq (preceding-char) ?\) )
4787 ;; label: {}
4788 (save-excursion (cperl-after-label))
4789 ;; sub :attr {}
4790 (get-text-property (cperl-1- (point)) 'attrib-group)
4791 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4792 (save-excursion
4793 (forward-sexp -1)
4794 ;; else {} but not else::func {}
4795 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4796 (not (looking-at "\\(\\sw\\|_\\)+::")))
4797 ;; sub f {}
4798 (progn
4799 (cperl-backward-to-noncomment lim)
4800 (and (eq (preceding-char) ?b)
4801 (progn
4802 (forward-sexp -1)
4803 (looking-at "sub[ \t\n\f#]"))))))
4804 ;; What preceeds is not word... XXXX Last statement in sub???
4805 (cperl-after-expr-p lim))))
4806 (error nil))))
4807
4808 (defun cperl-after-expr-p (&optional lim chars test)
4809 "Return true if the position is good for start of expression.
4810 TEST is the expression to evaluate at the found position. If absent,
4811 CHARS is a string that contains good characters to have before us (however,
4812 `}' is treated \"smartly\" if it is not in the list)."
4813 (let ((lim (or lim (point-min)))
4814 stop p pr)
4815 (cperl-update-syntaxification (point) (point))
4816 (save-excursion
4817 (while (and (not stop) (> (point) lim))
4818 (skip-chars-backward " \t\n\f" lim)
4819 (setq p (point))
4820 (beginning-of-line)
4821 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4822 ;; '(pod here-doc here-doc-delim))
4823 (if (get-text-property (point) 'here-doc-group)
4824 (progn
4825 (goto-char
4826 (cperl-beginning-of-property (point) 'here-doc-group))
4827 (beginning-of-line 0)))
4828 (if (get-text-property (point) 'in-pod)
4829 (progn
4830 (goto-char
4831 (cperl-beginning-of-property (point) 'in-pod))
4832 (beginning-of-line 0)))
4833 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4834 ;; Else: last iteration, or a label
4835 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4836 (skip-chars-backward " \t")
4837 (if (< p (point)) (goto-char p))
4838 (setq p (point))
4839 (if (and (eq (preceding-char) ?:)
4840 (progn
4841 (forward-char -1)
4842 (skip-chars-backward " \t\n\f" lim)
4843 (memq (char-syntax (preceding-char)) '(?w ?_))))
4844 (forward-sexp -1) ; Possibly label. Skip it
4845 (goto-char p)
4846 (setq stop t))))
4847 (or (bobp) ; ???? Needed
4848 (eq (point) lim)
4849 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4850 (progn
4851 (if test (eval test)
4852 (or (memq (preceding-char) (append (or chars "{;") nil))
4853 (and (eq (preceding-char) ?\})
4854 (cperl-after-block-p lim))
4855 (and (eq (following-char) ?.) ; in format: see comment above
4856 (eq (get-text-property (point) 'syntax-type)
4857 'format)))))))))
4858
4859 (defun cperl-backward-to-start-of-expr (&optional lim)
4860 (condition-case nil
4861 (progn
4862 (while (and (or (not lim)
4863 (> (point) lim))
4864 (not (cperl-after-expr-p lim)))
4865 (forward-sexp -1)
4866 ;; May be after $, @, $# etc of a variable
4867 (skip-chars-backward "$@%#")))
4868 (error nil)))
4869
4870 (defun cperl-at-end-of-expr (&optional lim)
4871 ;; Since the SEXP approach below is very fragile, do some overengineering
4872 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4873 (condition-case nil
4874 (save-excursion
4875 ;; If nothing interesting after, does as (forward-sexp -1);
4876 ;; otherwise fails, or ends at a start of following sexp.
4877 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4878 ;; may be stuck after @ or $; just put some stupid workaround now:
4879 (let ((p (point)))
4880 (forward-sexp 1)
4881 (forward-sexp -1)
4882 (while (memq (preceding-char) (append "%&@$*" nil))
4883 (forward-char -1))
4884 (or (< (point) p)
4885 (cperl-after-expr-p lim))))
4886 (error t))))
4887
4888 (defun cperl-forward-to-end-of-expr (&optional lim)
4889 (let ((p (point))))
4890 (condition-case nil
4891 (progn
4892 (while (and (< (point) (or lim (point-max)))
4893 (not (cperl-at-end-of-expr)))
4894 (forward-sexp 1)))
4895 (error nil)))
4896
4897 (defun cperl-backward-to-start-of-continued-exp (lim)
4898 (if (memq (preceding-char) (append ")]}\"'`" nil))
4899 (forward-sexp -1))
4900 (beginning-of-line)
4901 (if (<= (point) lim)
4902 (goto-char (1+ lim)))
4903 (skip-chars-forward " \t"))
4904
4905 (defun cperl-after-block-and-statement-beg (lim)
4906 ;; We assume that we are after ?\}
4907 (and
4908 (cperl-after-block-p lim)
4909 (save-excursion
4910 (forward-sexp -1)
4911 (cperl-backward-to-noncomment (point-min))
4912 (or (bobp)
4913 (eq (point) lim)
4914 (not (= (char-syntax (preceding-char)) ?w))
4915 (progn
4916 (forward-sexp -1)
4917 (not
4918 (looking-at
4919 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4920
4921 \f
4922 (defun cperl-indent-exp ()
4923 "Simple variant of indentation of continued-sexp.
4924
4925 Will not indent comment if it starts at `comment-indent' or looks like
4926 continuation of the comment on the previous line.
4927
4928 If `cperl-indent-region-fix-constructs', will improve spacing on
4929 conditional/loop constructs."
4930 (interactive)
4931 (save-excursion
4932 (let ((tmp-end (progn (end-of-line) (point))) top done)
4933 (save-excursion
4934 (beginning-of-line)
4935 (while (null done)
4936 (setq top (point))
4937 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
4938 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
4939 (setq top (point))) ; Get the outermost parenths in line
4940 (goto-char top)
4941 (while (< (point) tmp-end)
4942 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
4943 (or (eolp) (forward-sexp 1)))
4944 (if (> (point) tmp-end) ; Yes, there an unfinished block
4945 nil
4946 (if (eq ?\) (preceding-char))
4947 (progn ;; Plan B: find by REGEXP block followup this line
4948 (setq top (point))
4949 (condition-case nil
4950 (progn
4951 (forward-sexp -2)
4952 (if (eq (following-char) ?$ ) ; for my $var (list)
4953 (progn
4954 (forward-sexp -1)
4955 (if (looking-at "\\(my\\|local\\|our\\)\\>")
4956 (forward-sexp -1))))
4957 (if (looking-at
4958 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
4959 "\\|for\\(each\\)?\\>\\(\\("
4960 cperl-maybe-white-and-comment-rex
4961 "\\(my\\|local\\|our\\)\\)?"
4962 cperl-maybe-white-and-comment-rex
4963 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
4964 (progn
4965 (goto-char top)
4966 (forward-sexp 1)
4967 (setq top (point)))))
4968 (error (setq done t)))
4969 (goto-char top))
4970 (if (looking-at ; Try Plan C: continuation block
4971 (concat cperl-maybe-white-and-comment-rex
4972 "\\<\\(else\\|elsif\|continue\\)\\>"))
4973 (progn
4974 (goto-char (match-end 0))
4975 (save-excursion
4976 (end-of-line)
4977 (setq tmp-end (point))))
4978 (setq done t))))
4979 (save-excursion
4980 (end-of-line)
4981 (setq tmp-end (point))))
4982 (goto-char tmp-end)
4983 (setq tmp-end (point-marker)))
4984 (if cperl-indent-region-fix-constructs
4985 (cperl-fix-line-spacing tmp-end))
4986 (cperl-indent-region (point) tmp-end))))
4987
4988 (defun cperl-fix-line-spacing (&optional end parse-data)
4989 "Improve whitespace in a conditional/loop construct.
4990 Returns some position at the last line."
4991 (interactive)
4992 (or end
4993 (setq end (point-max)))
4994 (let ((ee (save-excursion (end-of-line) (point)))
4995 (cperl-indent-region-fix-constructs
4996 (or cperl-indent-region-fix-constructs 1))
4997 p pp ml have-brace ret)
4998 (save-excursion
4999 (beginning-of-line)
5000 (setq ret (point))
5001 ;; }? continue
5002 ;; blah; }
5003 (if (not
5004 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5005 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5006 nil ; Do not need to do anything
5007 ;; Looking at:
5008 ;; }
5009 ;; else
5010 (if cperl-merge-trailing-else
5011 (if (looking-at
5012 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5013 (progn
5014 (search-forward "}")
5015 (setq p (point))
5016 (skip-chars-forward " \t\n")
5017 (delete-region p (point))
5018 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5019 (beginning-of-line)))
5020 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5021 (save-excursion
5022 (search-forward "}")
5023 (delete-horizontal-space)
5024 (insert "\n")
5025 (setq ret (point))
5026 (if (cperl-indent-line parse-data)
5027 (progn
5028 (cperl-fix-line-spacing end parse-data)
5029 (setq ret (point)))))))
5030 ;; Looking at:
5031 ;; } else
5032 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5033 (progn
5034 (search-forward "}")
5035 (delete-horizontal-space)
5036 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5037 (beginning-of-line)))
5038 ;; Looking at:
5039 ;; else {
5040 (if (looking-at
5041 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5042 (progn
5043 (forward-word 1)
5044 (delete-horizontal-space)
5045 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5046 (beginning-of-line)))
5047 ;; Looking at:
5048 ;; foreach my $var
5049 (if (looking-at
5050 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5051 (progn
5052 (forward-word 2)
5053 (delete-horizontal-space)
5054 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5055 (beginning-of-line)))
5056 ;; Looking at:
5057 ;; foreach my $var (
5058 (if (looking-at
5059 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5060 (progn
5061 (forward-sexp 3)
5062 (delete-horizontal-space)
5063 (insert
5064 (make-string cperl-indent-region-fix-constructs ?\s))
5065 (beginning-of-line)))
5066 ;; Looking at (with or without "}" at start, ending after "({"):
5067 ;; } foreach my $var () OR {
5068 (if (looking-at
5069 "[ \t]*\\(}[ \t]*\\)?\\<\\(\\els\\(e\\|if\\)\\|continue\\|if\\|unless\\|while\\|for\\(each\\)?\\(\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\$[_a-zA-Z0-9]+\\)?\\|until\\)\\>\\([ \t]*(\\|[ \t\n]*{\\)\\|[ \t]*{")
5070 (progn
5071 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5072 (re-search-forward "[({]")
5073 (forward-char -1)
5074 (setq p (point))
5075 (if (eq (following-char) ?\( )
5076 (progn
5077 (forward-sexp 1)
5078 (setq pp (point))) ; past parenth-group
5079 ;; after `else' or nothing
5080 (if ml ; after `else'
5081 (skip-chars-backward " \t\n")
5082 (beginning-of-line))
5083 (setq pp nil))
5084 ;; Now after the sexp before the brace
5085 ;; Multiline expr should be special
5086 (setq ml (and pp (save-excursion (goto-char p)
5087 (search-forward "\n" pp t))))
5088 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5089 (looking-at "[ \t\n]*{"))
5090 (progn
5091 (cond
5092 ((bolp) ; Were before `{', no if/else/etc
5093 nil)
5094 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5095 (delete-horizontal-space)
5096 (if (if ml
5097 cperl-extra-newline-before-brace-multiline
5098 cperl-extra-newline-before-brace)
5099 (progn
5100 (delete-horizontal-space)
5101 (insert "\n")
5102 (setq ret (point))
5103 (if (cperl-indent-line parse-data)
5104 (progn
5105 (cperl-fix-line-spacing end parse-data)
5106 (setq ret (point)))))
5107 (insert
5108 (make-string cperl-indent-region-fix-constructs ?\s))))
5109 ((and (looking-at "[ \t]*\n")
5110 (not (if ml
5111 cperl-extra-newline-before-brace-multiline
5112 cperl-extra-newline-before-brace)))
5113 (setq pp (point))
5114 (skip-chars-forward " \t\n")
5115 (delete-region pp (point))
5116 (insert
5117 (make-string cperl-indent-region-fix-constructs ?\ )))
5118 ((and (looking-at "[\t ]*{")
5119 (if ml cperl-extra-newline-before-brace-multiline
5120 cperl-extra-newline-before-brace))
5121 (delete-horizontal-space)
5122 (insert "\n")
5123 (setq ret (point))
5124 (if (cperl-indent-line parse-data)
5125 (progn
5126 (cperl-fix-line-spacing end parse-data)
5127 (setq ret (point))))))
5128 ;; Now we are before `{'
5129 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5130 (progn
5131 (skip-chars-forward " \t\n")
5132 (setq pp (point))
5133 (forward-sexp 1)
5134 (setq p (point))
5135 (goto-char pp)
5136 (setq ml (search-forward "\n" p t))
5137 (if (or cperl-break-one-line-blocks-when-indent ml)
5138 ;; not good: multi-line BLOCK
5139 (progn
5140 (goto-char (1+ pp))
5141 (delete-horizontal-space)
5142 (insert "\n")
5143 (setq ret (point))
5144 (if (cperl-indent-line parse-data)
5145 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5146 (beginning-of-line)
5147 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5148 ;; Now check whether there is a hanging `}'
5149 ;; Looking at:
5150 ;; } blah
5151 (if (and
5152 cperl-fix-hanging-brace-when-indent
5153 have-brace
5154 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5155 (condition-case nil
5156 (progn
5157 (up-list 1)
5158 (if (and (<= (point) pp)
5159 (eq (preceding-char) ?\} )
5160 (cperl-after-block-and-statement-beg (point-min)))
5161 t
5162 (goto-char p)
5163 nil))
5164 (error nil)))
5165 (progn
5166 (forward-char -1)
5167 (skip-chars-backward " \t")
5168 (if (bolp)
5169 ;; `}' was the first thing on the line, insert NL *after* it.
5170 (progn
5171 (cperl-indent-line parse-data)
5172 (search-forward "}")
5173 (delete-horizontal-space)
5174 (insert "\n"))
5175 (delete-horizontal-space)
5176 (or (eq (preceding-char) ?\;)
5177 (bolp)
5178 (and (eq (preceding-char) ?\} )
5179 (cperl-after-block-p (point-min)))
5180 (insert ";"))
5181 (insert "\n")
5182 (setq ret (point)))
5183 (if (cperl-indent-line parse-data)
5184 (setq ret (cperl-fix-line-spacing end parse-data)))
5185 (beginning-of-line)))))
5186 ret))
5187
5188 (defvar cperl-update-start) ; Do not need to make them local
5189 (defvar cperl-update-end)
5190 (defun cperl-delay-update-hook (beg end old-len)
5191 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5192 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5193
5194 (defun cperl-indent-region (start end)
5195 "Simple variant of indentation of region in CPerl mode.
5196 Should be slow. Will not indent comment if it starts at `comment-indent'
5197 or looks like continuation of the comment on the previous line.
5198 Indents all the lines whose first character is between START and END
5199 inclusive.
5200
5201 If `cperl-indent-region-fix-constructs', will improve spacing on
5202 conditional/loop constructs."
5203 (interactive "r")
5204 (cperl-update-syntaxification end end)
5205 (save-excursion
5206 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5207 (let ((indent-info (if cperl-emacs-can-parse
5208 (list nil nil nil) ; Cannot use '(), since will modify
5209 nil))
5210 (pm 0)
5211 after-change-functions ; Speed it up!
5212 st comm old-comm-indent new-comm-indent p pp i empty)
5213 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5214 (goto-char start)
5215 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5216 (current-column))
5217 new-comm-indent old-comm-indent)
5218 (goto-char start)
5219 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5220 (or (bolp) (beginning-of-line 2))
5221 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5222 (setq st (point))
5223 (if (or
5224 (setq empty (looking-at "[ \t]*\n"))
5225 (and (setq comm (looking-at "[ \t]*#"))
5226 (or (eq (current-indentation) (or old-comm-indent
5227 comment-column))
5228 (setq old-comm-indent nil))))
5229 (if (and old-comm-indent
5230 (not empty)
5231 (= (current-indentation) old-comm-indent)
5232 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5233 (not (eq (get-text-property (point) 'syntax-table)
5234 cperl-st-cfence)))
5235 (let ((comment-column new-comm-indent))
5236 (indent-for-comment)))
5237 (progn
5238 (setq i (cperl-indent-line indent-info))
5239 (or comm
5240 (not i)
5241 (progn
5242 (if cperl-indent-region-fix-constructs
5243 (goto-char (cperl-fix-line-spacing end indent-info)))
5244 (if (setq old-comm-indent
5245 (and (cperl-to-comment-or-eol)
5246 (not (memq (get-text-property (point)
5247 'syntax-type)
5248 '(pod here-doc)))
5249 (not (eq (get-text-property (point)
5250 'syntax-table)
5251 cperl-st-cfence))
5252 (current-column)))
5253 (progn (indent-for-comment)
5254 (skip-chars-backward " \t")
5255 (skip-chars-backward "#")
5256 (setq new-comm-indent (current-column))))))))
5257 (beginning-of-line 2)))
5258 ;; Now run the update hooks
5259 (and after-change-functions
5260 cperl-update-end
5261 (save-excursion
5262 (goto-char cperl-update-end)
5263 (insert " ")
5264 (delete-char -1)
5265 (goto-char cperl-update-start)
5266 (insert " ")
5267 (delete-char -1))))))
5268
5269 ;; Stolen from lisp-mode with a lot of improvements
5270
5271 (defun cperl-fill-paragraph (&optional justify iteration)
5272 "Like `fill-paragraph', but handle CPerl comments.
5273 If any of the current line is a comment, fill the comment or the
5274 block of it that point is in, preserving the comment's initial
5275 indentation and initial hashes. Behaves usually outside of comment."
5276 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5277 (let (;; Non-nil if the current line contains a comment.
5278 has-comment
5279 fill-paragraph-function ; do not recurse
5280 ;; If has-comment, the appropriate fill-prefix for the comment.
5281 comment-fill-prefix
5282 ;; Line that contains code and comment (or nil)
5283 start
5284 c spaces len dc (comment-column comment-column))
5285 ;; Figure out what kind of comment we are looking at.
5286 (save-excursion
5287 (beginning-of-line)
5288 (cond
5289
5290 ;; A line with nothing but a comment on it?
5291 ((looking-at "[ \t]*#[# \t]*")
5292 (setq has-comment t
5293 comment-fill-prefix (buffer-substring (match-beginning 0)
5294 (match-end 0))))
5295
5296 ;; A line with some code, followed by a comment? Remember that the
5297 ;; semi which starts the comment shouldn't be part of a string or
5298 ;; character.
5299 ((cperl-to-comment-or-eol)
5300 (setq has-comment t)
5301 (looking-at "#+[ \t]*")
5302 (setq start (point) c (current-column)
5303 comment-fill-prefix
5304 (concat (make-string (current-column) ?\s)
5305 (buffer-substring (match-beginning 0) (match-end 0)))
5306 spaces (progn (skip-chars-backward " \t")
5307 (buffer-substring (point) start))
5308 dc (- c (current-column)) len (- start (point))
5309 start (point-marker))
5310 (delete-char len)
5311 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5312 (if (not has-comment)
5313 (fill-paragraph justify) ; Do the usual thing outside of comment
5314 ;; Narrow to include only the comment, and then fill the region.
5315 (save-restriction
5316 (narrow-to-region
5317 ;; Find the first line we should include in the region to fill.
5318 (if start (progn (beginning-of-line) (point))
5319 (save-excursion
5320 (while (and (zerop (forward-line -1))
5321 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5322 ;; We may have gone to far. Go forward again.
5323 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5324 (forward-line 1))
5325 (point)))
5326 ;; Find the beginning of the first line past the region to fill.
5327 (save-excursion
5328 (while (progn (forward-line 1)
5329 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5330 (point)))
5331 ;; Remove existing hashes
5332 (save-excursion
5333 (goto-char (point-min))
5334 (while (progn (forward-line 1) (< (point) (point-max)))
5335 (skip-chars-forward " \t")
5336 (if (looking-at "#+")
5337 (progn
5338 (if (and (eq (point) (match-beginning 0))
5339 (not (eq (point) (match-end 0)))) nil
5340 (error
5341 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5342 (delete-char (- (match-end 0) (match-beginning 0)))))))
5343
5344 ;; Lines with only hashes on them can be paragraph boundaries.
5345 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5346 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5347 (fill-prefix comment-fill-prefix))
5348 (fill-paragraph justify)))
5349 (if (and start)
5350 (progn
5351 (goto-char start)
5352 (if (> dc 0)
5353 (progn (delete-char dc) (insert spaces)))
5354 (if (or (= (current-column) c) iteration) nil
5355 (setq comment-column c)
5356 (indent-for-comment)
5357 ;; Repeat once more, flagging as iteration
5358 (cperl-fill-paragraph justify t))))))
5359 t)
5360
5361 (defun cperl-do-auto-fill ()
5362 ;; Break out if the line is short enough
5363 (if (> (save-excursion
5364 (end-of-line)
5365 (current-column))
5366 fill-column)
5367 (let ((c (save-excursion (beginning-of-line)
5368 (cperl-to-comment-or-eol) (point)))
5369 (s (memq (following-char) '(?\s ?\t))) marker)
5370 (if (>= c (point))
5371 ;; Don't break line inside code: only inside comment.
5372 nil
5373 (setq marker (point-marker))
5374 (fill-paragraph nil)
5375 (goto-char marker)
5376 ;; Is not enough, sometimes marker is a start of line
5377 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5378 (goto-char (match-end 0))))
5379 ;; Following space could have gone:
5380 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5381 (insert " ")
5382 (backward-char 1))
5383 ;; Previous space could have gone:
5384 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5385
5386 (defun cperl-imenu-addback (lst &optional isback name)
5387 ;; We suppose that the lst is a DAG, unless the first element only
5388 ;; loops back, and ISBACK is set. Thus this function cannot be
5389 ;; applied twice without ISBACK set.
5390 (cond ((not cperl-imenu-addback) lst)
5391 (t
5392 (or name
5393 (setq name "+++BACK+++"))
5394 (mapcar (lambda (elt)
5395 (if (and (listp elt) (listp (cdr elt)))
5396 (progn
5397 ;; In the other order it goes up
5398 ;; one level only ;-(
5399 (setcdr elt (cons (cons name lst)
5400 (cdr elt)))
5401 (cperl-imenu-addback (cdr elt) t name))))
5402 (if isback (cdr lst) lst))
5403 lst)))
5404
5405 (defun cperl-imenu--create-perl-index (&optional regexp)
5406 (require 'imenu) ; May be called from TAGS creator
5407 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5408 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5409 (index-meth-alist '()) meth
5410 packages ends-ranges p marker is-proto
5411 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5412 (goto-char (point-min))
5413 (cperl-update-syntaxification (point-max) (point-max))
5414 ;; Search for the function
5415 (progn ;;save-match-data
5416 (while (re-search-forward
5417 (or regexp cperl-imenu--function-name-regexp-perl)
5418 nil t)
5419 ;; 2=package-group, 5=package-name 8=sub-name
5420 (cond
5421 ((and ; Skip some noise if building tags
5422 (match-beginning 5) ; package name
5423 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5424 (not (save-match-data
5425 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5426 nil)
5427 ((and
5428 (or (match-beginning 2)
5429 (match-beginning 8)) ; package or sub
5430 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5431 (null (get-text-property (match-beginning 1) 'syntax-table))
5432 (null (get-text-property (match-beginning 1) 'syntax-type))
5433 (null (get-text-property (match-beginning 1) 'in-pod)))
5434 (setq is-pack (match-beginning 2))
5435 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5436 ;; (goto-char (match-end 0))) ; Messes what follows
5437 (setq meth nil
5438 p (point))
5439 (while (and ends-ranges (>= p (car ends-ranges)))
5440 ;; delete obsolete entries
5441 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5442 (setq package (or (car packages) "")
5443 end-range (or (car ends-ranges) 0))
5444 (if is-pack ; doing "package"
5445 (progn
5446 (if (match-beginning 5) ; named package
5447 (setq name (buffer-substring (match-beginning 5)
5448 (match-end 5))
5449 name (progn
5450 (set-text-properties 0 (length name) nil name)
5451 name)
5452 package (concat name "::")
5453 name (concat "package " name))
5454 ;; Support nameless packages
5455 (setq name "package;" package ""))
5456 (setq end-range
5457 (save-excursion
5458 (parse-partial-sexp (point) (point-max) -1) (point))
5459 ends-ranges (cons end-range ends-ranges)
5460 packages (cons package packages)))
5461 (setq is-proto
5462 (or (eq (following-char) ?\;)
5463 (eq 0 (get-text-property (point) 'attrib-group)))))
5464 ;; Skip this function name if it is a prototype declaration.
5465 (if (and is-proto (not is-pack)) nil
5466 (or is-pack
5467 (setq name
5468 (buffer-substring (match-beginning 8) (match-end 8)))
5469 (set-text-properties 0 (length name) nil name))
5470 (setq marker (make-marker))
5471 (set-marker marker (match-end (if is-pack 2 8)))
5472 (cond (is-pack nil)
5473 ((string-match "[:']" name)
5474 (setq meth t))
5475 ((> p end-range) nil)
5476 (t
5477 (setq name (concat package name) meth t)))
5478 (setq index (cons name marker))
5479 (if is-pack
5480 (push index index-pack-alist)
5481 (push index index-alist))
5482 (if meth (push index index-meth-alist))
5483 (push index index-unsorted-alist)))
5484 ((match-beginning 16) ; POD section
5485 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5486 marker (make-marker))
5487 (set-marker marker (match-beginning 17))
5488 (set-text-properties 0 (length name) nil name)
5489 (setq name (concat (make-string
5490 (* 3 (- (char-after (match-beginning 16)) ?1))
5491 ?\ )
5492 name)
5493 index (cons name marker))
5494 (setq index1 (cons (concat "=" name) (cdr index)))
5495 (push index index-pod-alist)
5496 (push index1 index-unsorted-alist)))))
5497 (setq index-alist
5498 (if (default-value 'imenu-sort-function)
5499 (sort index-alist (default-value 'imenu-sort-function))
5500 (nreverse index-alist)))
5501 (and index-pod-alist
5502 (push (cons "+POD headers+..."
5503 (nreverse index-pod-alist))
5504 index-alist))
5505 (and (or index-pack-alist index-meth-alist)
5506 (let ((lst index-pack-alist) hier-list pack elt group name)
5507 ;; Remove "package ", reverse and uniquify.
5508 (while lst
5509 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5510 (if (assoc name hier-list) nil
5511 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5512 (setq lst index-meth-alist)
5513 (while lst
5514 (setq elt (car lst) lst (cdr lst))
5515 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5516 (setq pack (substring (car elt) 0 (match-beginning 0)))
5517 (if (setq group (assoc pack hier-list))
5518 (if (listp (cdr group))
5519 ;; Have some functions already
5520 (setcdr group
5521 (cons (cons (substring
5522 (car elt)
5523 (+ 2 (match-beginning 0)))
5524 (cdr elt))
5525 (cdr group)))
5526 (setcdr group (list (cons (substring
5527 (car elt)
5528 (+ 2 (match-beginning 0)))
5529 (cdr elt)))))
5530 (setq hier-list
5531 (cons (cons pack
5532 (list (cons (substring
5533 (car elt)
5534 (+ 2 (match-beginning 0)))
5535 (cdr elt))))
5536 hier-list))))))
5537 (push (cons "+Hierarchy+..."
5538 hier-list)
5539 index-alist)))
5540 (and index-pack-alist
5541 (push (cons "+Packages+..."
5542 (nreverse index-pack-alist))
5543 index-alist))
5544 (and (or index-pack-alist index-pod-alist
5545 (default-value 'imenu-sort-function))
5546 index-unsorted-alist
5547 (push (cons "+Unsorted List+..."
5548 (nreverse index-unsorted-alist))
5549 index-alist))
5550 (cperl-imenu-addback index-alist)))
5551
5552 \f
5553 ;; Suggested by Mark A. Hershberger
5554 (defun cperl-outline-level ()
5555 (looking-at outline-regexp)
5556 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5557 ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5558 ((match-beginning 2) 0) ; package
5559 ((match-beginning 8) 1) ; sub
5560 ((match-beginning 16)
5561 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5562 (t 5))) ; should not happen
5563
5564 \f
5565 (defun cperl-windowed-init ()
5566 "Initialization under windowed version."
5567 (cond ((featurep 'ps-print)
5568 (or cperl-faces-init
5569 (progn
5570 (and (boundp 'font-lock-multiline)
5571 (setq cperl-font-lock-multiline t))
5572 (cperl-init-faces))))
5573 ((not cperl-faces-init)
5574 (add-hook 'font-lock-mode-hook
5575 (function
5576 (lambda ()
5577 (if (memq major-mode '(perl-mode cperl-mode))
5578 (progn
5579 (or cperl-faces-init (cperl-init-faces)))))))
5580 (if (fboundp 'eval-after-load)
5581 (eval-after-load
5582 "ps-print"
5583 '(or cperl-faces-init (cperl-init-faces)))))))
5584
5585 (defvar cperl-font-lock-keywords-1 nil
5586 "Additional expressions to highlight in Perl mode. Minimal set.")
5587 (defvar cperl-font-lock-keywords nil
5588 "Additional expressions to highlight in Perl mode. Default set.")
5589 (defvar cperl-font-lock-keywords-2 nil
5590 "Additional expressions to highlight in Perl mode. Maximal set")
5591
5592 (defun cperl-load-font-lock-keywords ()
5593 (or cperl-faces-init (cperl-init-faces))
5594 cperl-font-lock-keywords)
5595
5596 (defun cperl-load-font-lock-keywords-1 ()
5597 (or cperl-faces-init (cperl-init-faces))
5598 cperl-font-lock-keywords-1)
5599
5600 (defun cperl-load-font-lock-keywords-2 ()
5601 (or cperl-faces-init (cperl-init-faces))
5602 cperl-font-lock-keywords-2)
5603
5604 (defun cperl-init-faces-weak ()
5605 ;; Allow `cperl-find-pods-heres' to run.
5606 (or (boundp 'font-lock-constant-face)
5607 (cperl-force-face font-lock-constant-face
5608 "Face for constant and label names"))
5609 (or (boundp 'font-lock-warning-face)
5610 (cperl-force-face font-lock-warning-face
5611 "Face for things which should stand out"))
5612 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5613 )
5614
5615 (defun cperl-init-faces ()
5616 (condition-case errs
5617 (progn
5618 (require 'font-lock)
5619 (and (fboundp 'font-lock-fontify-anchored-keywords)
5620 (featurep 'font-lock-extra)
5621 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5622 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5623 (if (fboundp 'font-lock-fontify-anchored-keywords)
5624 (setq font-lock-anchored t))
5625 (setq
5626 t-font-lock-keywords
5627 (list
5628 `("[ \t]+$" 0 ',cperl-invalid-face t)
5629 (cons
5630 (concat
5631 "\\(^\\|[^$@%&\\]\\)\\<\\("
5632 (mapconcat
5633 'identity
5634 '("if" "until" "while" "elsif" "else" "unless" "for"
5635 "foreach" "continue" "exit" "die" "last" "goto" "next"
5636 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5637 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5638 "\\|") ; Flow control
5639 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5640 ; In what follows we use `type' style
5641 ; for overwritable builtins
5642 (list
5643 (concat
5644 "\\(^\\|[^$@%&\\]\\)\\<\\("
5645 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5646 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5647 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5648 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5649 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5650 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5651 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5652 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5653 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5654 ;; "gethostbyname" "gethostent" "getlogin"
5655 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5656 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5657 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5658 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5659 ;; "getservbyport" "getservent" "getsockname"
5660 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5661 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5662 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5663 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5664 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5665 ;; "quotemeta" "rand" "read" "readdir" "readline"
5666 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5667 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5668 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5669 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5670 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5671 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5672 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5673 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5674 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5675 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5676 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5677 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5678 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5679 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5680 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5681 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5682 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5683 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5684 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5685 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5686 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5687 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5688 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5689 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5690 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5691 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5692 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5693 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5694 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5695 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5696 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5697 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5698 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5699 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5700 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5701 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5702 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5703 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5704 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5705 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5706 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5707 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5708 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5709 "\\)\\>") 2 'font-lock-type-face)
5710 ;; In what follows we use `other' style
5711 ;; for nonoverwritable builtins
5712 ;; Somehow 's', 'm' are not auto-generated???
5713 (list
5714 (concat
5715 "\\(^\\|[^$@%&\\]\\)\\<\\("
5716 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5717 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5718 ;; "eval" "exists" "for" "foreach" "format" "goto"
5719 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5720 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5721 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5722 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5723 ;; "undef" "unless" "unshift" "untie" "until" "use"
5724 ;; "while" "y"
5725 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5726 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5727 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5728 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5729 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5730 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5731 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5732 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5733 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5734 "\\|[sm]" ; Added manually
5735 "\\)\\>") 2 'cperl-nonoverridable-face)
5736 ;; (mapconcat 'identity
5737 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5738 ;; "#include" "#define" "#undef")
5739 ;; "\\|")
5740 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5741 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5742 ;; This highlights declarations and definitions differenty.
5743 ;; We do not try to highlight in the case of attributes:
5744 ;; it is already done by `cperl-find-pods-heres'
5745 (list (concat "\\<sub"
5746 cperl-white-and-comment-rex ; whitespace/comments
5747 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5748 "\\("
5749 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5750 "([^()]*)\\)?" ; prototype
5751 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5752 "[{;]")
5753 2 (if cperl-font-lock-multiline
5754 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5755 'font-lock-function-name-face
5756 'font-lock-variable-name-face)
5757 ;; need to manually set 'multiline' for older font-locks
5758 '(progn
5759 (if (< 1 (count-lines (match-beginning 0)
5760 (match-end 0)))
5761 (put-text-property
5762 (+ 3 (match-beginning 0)) (match-end 0)
5763 'syntax-type 'multiline))
5764 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5765 'font-lock-function-name-face
5766 'font-lock-variable-name-face))))
5767 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5768 2 font-lock-function-name-face)
5769 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5770 1 font-lock-function-name-face)
5771 (cond ((featurep 'font-lock-extra)
5772 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5773 (2 font-lock-string-face t)
5774 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5775 (font-lock-anchored
5776 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5777 (2 font-lock-string-face t)
5778 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5779 nil nil
5780 (1 font-lock-string-face t))))
5781 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5782 2 font-lock-string-face t)))
5783 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5784 font-lock-string-face t)
5785 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5786 font-lock-constant-face) ; labels
5787 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5788 2 font-lock-constant-face)
5789 ;; Uncomment to get perl-mode-like vars
5790 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5791 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5792 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5793 (cond ((featurep 'font-lock-extra)
5794 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5795 (3 font-lock-variable-name-face)
5796 (4 '(another 4 nil
5797 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5798 (1 font-lock-variable-name-face)
5799 (2 '(restart 2 nil) nil t)))
5800 nil t))) ; local variables, multiple
5801 (font-lock-anchored
5802 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5803 `(,(concat "\\<\\(my\\|local\\|our\\)"
5804 cperl-maybe-white-and-comment-rex
5805 "\\(("
5806 cperl-maybe-white-and-comment-rex
5807 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5808 (5 ,(if cperl-font-lock-multiline
5809 'font-lock-variable-name-face
5810 '(progn (setq cperl-font-lock-multiline-start
5811 (match-beginning 0))
5812 'font-lock-variable-name-face)))
5813 (,(concat "\\="
5814 cperl-maybe-white-and-comment-rex
5815 ","
5816 cperl-maybe-white-and-comment-rex
5817 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5818 ;; Bug in font-lock: limit is used not only to limit
5819 ;; searches, but to set the "extend window for
5820 ;; facification" property. Thus we need to minimize.
5821 ,(if cperl-font-lock-multiline
5822 '(if (match-beginning 3)
5823 (save-excursion
5824 (goto-char (match-beginning 3))
5825 (condition-case nil
5826 (forward-sexp 1)
5827 (error
5828 (condition-case nil
5829 (forward-char 200)
5830 (error nil)))) ; typeahead
5831 (1- (point))) ; report limit
5832 (forward-char -2)) ; disable continued expr
5833 '(if (match-beginning 3)
5834 (point-max) ; No limit for continuation
5835 (forward-char -2))) ; disable continued expr
5836 ,(if cperl-font-lock-multiline
5837 nil
5838 '(progn ; Do at end
5839 ;; "my" may be already fontified (POD),
5840 ;; so cperl-font-lock-multiline-start is nil
5841 (if (or (not cperl-font-lock-multiline-start)
5842 (> 2 (count-lines
5843 cperl-font-lock-multiline-start
5844 (point))))
5845 nil
5846 (put-text-property
5847 (1+ cperl-font-lock-multiline-start) (point)
5848 'syntax-type 'multiline))
5849 (setq cperl-font-lock-multiline-start nil)))
5850 (3 font-lock-variable-name-face))))
5851 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5852 3 font-lock-variable-name-face)))
5853 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5854 4 font-lock-variable-name-face)
5855 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5856 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5857 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5858 (setq
5859 t-font-lock-keywords-1
5860 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
5861 ;; not yet as of XEmacs 19.12, works with 21.1.11
5862 (or
5863 (not cperl-xemacs-p)
5864 (string< "21.1.9" emacs-version)
5865 (and (string< "21.1.10" emacs-version)
5866 (string< emacs-version "21.1.2")))
5867 '(
5868 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5869 (if (eq (char-after (match-beginning 2)) ?%)
5870 'cperl-hash-face
5871 'cperl-array-face)
5872 t) ; arrays and hashes
5873 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5874 1
5875 (if (= (- (match-end 2) (match-beginning 2)) 1)
5876 (if (eq (char-after (match-beginning 3)) ?{)
5877 'cperl-hash-face
5878 'cperl-array-face) ; arrays and hashes
5879 font-lock-variable-name-face) ; Just to put something
5880 t)
5881 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5882 (1 cperl-array-face)
5883 (2 font-lock-variable-name-face))
5884 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5885 (1 cperl-hash-face)
5886 (2 font-lock-variable-name-face))
5887 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5888 ;;; Too much noise from \s* @s[ and friends
5889 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
5890 ;;(3 font-lock-function-name-face t t)
5891 ;;(4
5892 ;; (if (cperl-slash-is-regexp)
5893 ;; font-lock-function-name-face 'default) nil t))
5894 )))
5895 (if cperl-highlight-variables-indiscriminately
5896 (setq t-font-lock-keywords-1
5897 (append t-font-lock-keywords-1
5898 (list '("\\([$*]{?\\sw+\\)" 1
5899 font-lock-variable-name-face)))))
5900 (setq cperl-font-lock-keywords-1
5901 (if cperl-syntaxify-by-font-lock
5902 (cons 'cperl-fontify-update
5903 t-font-lock-keywords)
5904 t-font-lock-keywords)
5905 cperl-font-lock-keywords cperl-font-lock-keywords-1
5906 cperl-font-lock-keywords-2 (append
5907 cperl-font-lock-keywords-1
5908 t-font-lock-keywords-1)))
5909 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5910 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
5911 (eval ; Avoid a warning
5912 '(font-lock-require-faces
5913 (list
5914 ;; Color-light Color-dark Gray-light Gray-dark Mono
5915 (list 'font-lock-comment-face
5916 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5917 nil
5918 [nil nil t t t]
5919 [nil nil t t t]
5920 nil)
5921 (list 'font-lock-string-face
5922 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5923 nil
5924 nil
5925 [nil nil t t t]
5926 nil)
5927 (list 'font-lock-function-name-face
5928 (vector
5929 "Blue" "LightSkyBlue" "Gray50" "LightGray"
5930 (cdr (assq 'background-color ; if mono
5931 (frame-parameters))))
5932 (vector
5933 nil nil nil nil
5934 (cdr (assq 'foreground-color ; if mono
5935 (frame-parameters))))
5936 [nil nil t t t]
5937 nil
5938 nil)
5939 (list 'font-lock-variable-name-face
5940 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
5941 nil
5942 [nil nil t t t]
5943 [nil nil t t t]
5944 nil)
5945 (list 'font-lock-type-face
5946 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
5947 nil
5948 [nil nil t t t]
5949 nil
5950 [nil nil t t t])
5951 (list 'font-lock-warning-face
5952 ["Pink" "Red" "Gray50" "LightGray"]
5953 ["gray20" "gray90"
5954 "gray80" "gray20"]
5955 [nil nil t t t]
5956 nil
5957 [nil nil t t t]
5958 )
5959 (list 'font-lock-constant-face
5960 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
5961 nil
5962 [nil nil t t t]
5963 nil
5964 [nil nil t t t])
5965 (list 'cperl-nonoverridable-face
5966 ["chartreuse3" ("orchid1" "orange")
5967 nil "Gray80"]
5968 [nil nil "gray90"]
5969 [nil nil nil t t]
5970 [nil nil t t]
5971 [nil nil t t t])
5972 (list 'cperl-array-face
5973 ["blue" "yellow" nil "Gray80"]
5974 ["lightyellow2" ("navy" "os2blue" "darkgreen")
5975 "gray90"]
5976 t
5977 nil
5978 nil)
5979 (list 'cperl-hash-face
5980 ["red" "red" nil "Gray80"]
5981 ["lightyellow2" ("navy" "os2blue" "darkgreen")
5982 "gray90"]
5983 t
5984 t
5985 nil))))
5986 ;; Do it the dull way, without choose-color
5987 (defvar cperl-guessed-background nil
5988 "Display characteristics as guessed by cperl.")
5989 ;; (or (fboundp 'x-color-defined-p)
5990 ;; (defalias 'x-color-defined-p
5991 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
5992 ;; ;; XEmacs >= 19.12
5993 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
5994 ;; ;; XEmacs 19.11
5995 ;; (t 'x-valid-color-name-p))))
5996 (cperl-force-face font-lock-constant-face
5997 "Face for constant and label names")
5998 (cperl-force-face font-lock-variable-name-face
5999 "Face for variable names")
6000 (cperl-force-face font-lock-type-face
6001 "Face for data types")
6002 (cperl-force-face cperl-nonoverridable-face
6003 "Face for data types from another group")
6004 (cperl-force-face font-lock-warning-face
6005 "Face for things which should stand out")
6006 (cperl-force-face font-lock-comment-face
6007 "Face for comments")
6008 (cperl-force-face font-lock-function-name-face
6009 "Face for function names")
6010 (cperl-force-face cperl-hash-face
6011 "Face for hashes")
6012 (cperl-force-face cperl-array-face
6013 "Face for arrays")
6014 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6015 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6016 ;;(or (boundp 'font-lock-type-face)
6017 ;; (defconst font-lock-type-face
6018 ;; 'font-lock-type-face
6019 ;; "Face to use for data types."))
6020 ;;(or (boundp 'cperl-nonoverridable-face)
6021 ;; (defconst cperl-nonoverridable-face
6022 ;; 'cperl-nonoverridable-face
6023 ;; "Face to use for data types from another group."))
6024 ;;(if (not cperl-xemacs-p) nil
6025 ;; (or (boundp 'font-lock-comment-face)
6026 ;; (defconst font-lock-comment-face
6027 ;; 'font-lock-comment-face
6028 ;; "Face to use for comments."))
6029 ;; (or (boundp 'font-lock-keyword-face)
6030 ;; (defconst font-lock-keyword-face
6031 ;; 'font-lock-keyword-face
6032 ;; "Face to use for keywords."))
6033 ;; (or (boundp 'font-lock-function-name-face)
6034 ;; (defconst font-lock-function-name-face
6035 ;; 'font-lock-function-name-face
6036 ;; "Face to use for function names.")))
6037 (if (and
6038 (not (cperl-is-face 'cperl-array-face))
6039 (cperl-is-face 'font-lock-emphasized-face))
6040 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6041 (if (and
6042 (not (cperl-is-face 'cperl-hash-face))
6043 (cperl-is-face 'font-lock-other-emphasized-face))
6044 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6045 (if (and
6046 (not (cperl-is-face 'cperl-nonoverridable-face))
6047 (cperl-is-face 'font-lock-other-type-face))
6048 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6049 ;;(or (boundp 'cperl-hash-face)
6050 ;; (defconst cperl-hash-face
6051 ;; 'cperl-hash-face
6052 ;; "Face to use for hashes."))
6053 ;;(or (boundp 'cperl-array-face)
6054 ;; (defconst cperl-array-face
6055 ;; 'cperl-array-face
6056 ;; "Face to use for arrays."))
6057 ;; Here we try to guess background
6058 (let ((background
6059 (if (boundp 'font-lock-background-mode)
6060 font-lock-background-mode
6061 'light))
6062 (face-list (and (fboundp 'face-list) (face-list))))
6063 ;;;; (fset 'cperl-is-face
6064 ;;;; (cond ((fboundp 'find-face)
6065 ;;;; (symbol-function 'find-face))
6066 ;;;; (face-list
6067 ;;;; (function (lambda (face) (member face face-list))))
6068 ;;;; (t
6069 ;;;; (function (lambda (face) (boundp face))))))
6070 (defvar cperl-guessed-background
6071 (if (and (boundp 'font-lock-display-type)
6072 (eq font-lock-display-type 'grayscale))
6073 'gray
6074 background)
6075 "Background as guessed by CPerl mode")
6076 (and (not (cperl-is-face 'font-lock-constant-face))
6077 (cperl-is-face 'font-lock-reference-face)
6078 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6079 (if (cperl-is-face 'font-lock-type-face) nil
6080 (copy-face 'default 'font-lock-type-face)
6081 (cond
6082 ((eq background 'light)
6083 (set-face-foreground 'font-lock-type-face
6084 (if (x-color-defined-p "seagreen")
6085 "seagreen"
6086 "sea green")))
6087 ((eq background 'dark)
6088 (set-face-foreground 'font-lock-type-face
6089 (if (x-color-defined-p "os2pink")
6090 "os2pink"
6091 "pink")))
6092 (t
6093 (set-face-background 'font-lock-type-face "gray90"))))
6094 (if (cperl-is-face 'cperl-nonoverridable-face)
6095 nil
6096 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6097 (cond
6098 ((eq background 'light)
6099 (set-face-foreground 'cperl-nonoverridable-face
6100 (if (x-color-defined-p "chartreuse3")
6101 "chartreuse3"
6102 "chartreuse")))
6103 ((eq background 'dark)
6104 (set-face-foreground 'cperl-nonoverridable-face
6105 (if (x-color-defined-p "orchid1")
6106 "orchid1"
6107 "orange")))))
6108 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6109 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6110 ;;; (cond
6111 ;;; ((eq background 'light)
6112 ;;; (set-face-background 'font-lock-other-emphasized-face
6113 ;;; (if (x-color-defined-p "lightyellow2")
6114 ;;; "lightyellow2"
6115 ;;; (if (x-color-defined-p "lightyellow")
6116 ;;; "lightyellow"
6117 ;;; "light yellow"))))
6118 ;;; ((eq background 'dark)
6119 ;;; (set-face-background 'font-lock-other-emphasized-face
6120 ;;; (if (x-color-defined-p "navy")
6121 ;;; "navy"
6122 ;;; (if (x-color-defined-p "darkgreen")
6123 ;;; "darkgreen"
6124 ;;; "dark green"))))
6125 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6126 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6127 ;;; (copy-face 'bold 'font-lock-emphasized-face)
6128 ;;; (cond
6129 ;;; ((eq background 'light)
6130 ;;; (set-face-background 'font-lock-emphasized-face
6131 ;;; (if (x-color-defined-p "lightyellow2")
6132 ;;; "lightyellow2"
6133 ;;; "lightyellow")))
6134 ;;; ((eq background 'dark)
6135 ;;; (set-face-background 'font-lock-emphasized-face
6136 ;;; (if (x-color-defined-p "navy")
6137 ;;; "navy"
6138 ;;; (if (x-color-defined-p "darkgreen")
6139 ;;; "darkgreen"
6140 ;;; "dark green"))))
6141 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6142 (if (cperl-is-face 'font-lock-variable-name-face) nil
6143 (copy-face 'italic 'font-lock-variable-name-face))
6144 (if (cperl-is-face 'font-lock-constant-face) nil
6145 (copy-face 'italic 'font-lock-constant-face))))
6146 (setq cperl-faces-init t))
6147 (error (message "cperl-init-faces (ignored): %s" errs))))
6148
6149
6150 (defun cperl-ps-print-init ()
6151 "Initialization of `ps-print' components for faces used in CPerl."
6152 (eval-after-load "ps-print"
6153 '(setq ps-bold-faces
6154 ;; font-lock-variable-name-face
6155 ;; font-lock-constant-face
6156 (append '(cperl-array-face cperl-hash-face)
6157 ps-bold-faces)
6158 ps-italic-faces
6159 ;; font-lock-constant-face
6160 (append '(cperl-nonoverridable-face cperl-hash-face)
6161 ps-italic-faces)
6162 ps-underlined-faces
6163 ;; font-lock-type-face
6164 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6165 ps-underlined-faces))))
6166
6167 (defvar ps-print-face-extension-alist)
6168
6169 (defun cperl-ps-print (&optional file)
6170 "Pretty-print in CPerl style.
6171 If optional argument FILE is an empty string, prints to printer, otherwise
6172 to the file FILE. If FILE is nil, prompts for a file name.
6173
6174 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6175 (interactive)
6176 (or file
6177 (setq file (read-from-minibuffer
6178 "Print to file (if empty - to printer): "
6179 (concat (buffer-file-name) ".ps")
6180 nil nil 'file-name-history)))
6181 (or (> (length file) 0)
6182 (setq file nil))
6183 (require 'ps-print) ; To get ps-print-face-extension-alist
6184 (let ((ps-print-color-p t)
6185 (ps-print-face-extension-alist ps-print-face-extension-alist))
6186 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6187 (ps-print-buffer-with-faces file)))
6188
6189 ;;; (defun cperl-ps-print-init ()
6190 ;;; "Initialization of `ps-print' components for faces used in CPerl."
6191 ;;; ;; Guard against old versions
6192 ;;; (defvar ps-underlined-faces nil)
6193 ;;; (defvar ps-bold-faces nil)
6194 ;;; (defvar ps-italic-faces nil)
6195 ;;; (setq ps-bold-faces
6196 ;;; (append '(font-lock-emphasized-face
6197 ;;; cperl-array-face
6198 ;;; font-lock-keyword-face
6199 ;;; font-lock-variable-name-face
6200 ;;; font-lock-constant-face
6201 ;;; font-lock-reference-face
6202 ;;; font-lock-other-emphasized-face
6203 ;;; cperl-hash-face)
6204 ;;; ps-bold-faces))
6205 ;;; (setq ps-italic-faces
6206 ;;; (append '(cperl-nonoverridable-face
6207 ;;; font-lock-constant-face
6208 ;;; font-lock-reference-face
6209 ;;; font-lock-other-emphasized-face
6210 ;;; cperl-hash-face)
6211 ;;; ps-italic-faces))
6212 ;;; (setq ps-underlined-faces
6213 ;;; (append '(font-lock-emphasized-face
6214 ;;; cperl-array-face
6215 ;;; font-lock-other-emphasized-face
6216 ;;; cperl-hash-face
6217 ;;; cperl-nonoverridable-face font-lock-type-face)
6218 ;;; ps-underlined-faces))
6219 ;;; (cons 'font-lock-type-face ps-underlined-faces))
6220
6221
6222 (if (cperl-enable-font-lock) (cperl-windowed-init))
6223
6224 (defconst cperl-styles-entries
6225 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6226 cperl-label-offset cperl-extra-newline-before-brace
6227 cperl-extra-newline-before-brace-multiline
6228 cperl-merge-trailing-else
6229 cperl-continued-statement-offset))
6230
6231 (defconst cperl-style-examples
6232 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6233 ##### cperl-continued-brace-offset cperl-label-offset
6234 ##### cperl-continued-statement-offset
6235 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6236
6237 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6238
6239 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6240 if (foo) {
6241 bar
6242 baz;
6243 label:
6244 {
6245 boon;
6246 }
6247 } else {
6248 stop;
6249 }
6250
6251 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6252 if (foo) {
6253 bar
6254 baz;
6255 label:
6256 {
6257 boon;
6258 }
6259 } else {
6260 stop;
6261 }
6262
6263 ### GNU 2/0/0/-2/2/nil/t
6264 if (foo)
6265 {
6266 bar
6267 baz;
6268 label:
6269 {
6270 boon;
6271 }
6272 }
6273 else
6274 {
6275 stop;
6276 }
6277
6278 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6279 if (foo)
6280 {
6281 bar
6282 baz;
6283 label:
6284 {
6285 boon;
6286 }
6287 }
6288 else
6289 {
6290 stop;
6291 }
6292
6293 ### BSD (=C++, but will not change preexisting merge-trailing-else
6294 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6295 if (foo)
6296 {
6297 bar
6298 baz;
6299 label:
6300 {
6301 boon;
6302 }
6303 }
6304 else
6305 {
6306 stop;
6307 }
6308
6309 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6310 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6311 if (foo)
6312 {
6313 bar
6314 baz;
6315 label:
6316 {
6317 boon;
6318 }
6319 }
6320 else
6321 {
6322 stop;
6323 }
6324
6325 ### Whitesmith (=PerlStyle, but will not change preexisting
6326 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6327 if (foo)
6328 {
6329 bar
6330 baz;
6331 label:
6332 {
6333 boon;
6334 }
6335 }
6336 else
6337 {
6338 stop;
6339 }
6340 "
6341 "Examples of if/else with different indent styles (with v4.23).")
6342
6343 (defconst cperl-style-alist
6344 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6345 (cperl-indent-level . 2)
6346 (cperl-brace-offset . 0)
6347 (cperl-continued-brace-offset . 0)
6348 (cperl-label-offset . -2)
6349 (cperl-continued-statement-offset . 2)
6350 (cperl-extra-newline-before-brace . nil)
6351 (cperl-extra-newline-before-brace-multiline . nil)
6352 (cperl-merge-trailing-else . t))
6353
6354 ("PerlStyle" ; CPerl with 4 as indent
6355 (cperl-indent-level . 4)
6356 (cperl-brace-offset . 0)
6357 (cperl-continued-brace-offset . 0)
6358 (cperl-label-offset . -4)
6359 (cperl-continued-statement-offset . 4)
6360 (cperl-extra-newline-before-brace . nil)
6361 (cperl-extra-newline-before-brace-multiline . nil)
6362 (cperl-merge-trailing-else . t))
6363
6364 ("GNU"
6365 (cperl-indent-level . 2)
6366 (cperl-brace-offset . 0)
6367 (cperl-continued-brace-offset . 0)
6368 (cperl-label-offset . -2)
6369 (cperl-continued-statement-offset . 2)
6370 (cperl-extra-newline-before-brace . t)
6371 (cperl-extra-newline-before-brace-multiline . t)
6372 (cperl-merge-trailing-else . nil))
6373
6374 ("K&R"
6375 (cperl-indent-level . 5)
6376 (cperl-brace-offset . 0)
6377 (cperl-continued-brace-offset . -5)
6378 (cperl-label-offset . -5)
6379 (cperl-continued-statement-offset . 5)
6380 ;;(cperl-extra-newline-before-brace . nil) ; ???
6381 ;;(cperl-extra-newline-before-brace-multiline . nil)
6382 (cperl-merge-trailing-else . nil))
6383
6384 ("BSD"
6385 (cperl-indent-level . 4)
6386 (cperl-brace-offset . 0)
6387 (cperl-continued-brace-offset . -4)
6388 (cperl-label-offset . -4)
6389 (cperl-continued-statement-offset . 4)
6390 ;;(cperl-extra-newline-before-brace . nil) ; ???
6391 ;;(cperl-extra-newline-before-brace-multiline . nil)
6392 ;;(cperl-merge-trailing-else . nil) ; ???
6393 )
6394
6395 ("C++"
6396 (cperl-indent-level . 4)
6397 (cperl-brace-offset . 0)
6398 (cperl-continued-brace-offset . -4)
6399 (cperl-label-offset . -4)
6400 (cperl-continued-statement-offset . 4)
6401 (cperl-extra-newline-before-brace . t)
6402 (cperl-extra-newline-before-brace-multiline . t)
6403 (cperl-merge-trailing-else . nil))
6404
6405 ("Whitesmith"
6406 (cperl-indent-level . 4)
6407 (cperl-brace-offset . 0)
6408 (cperl-continued-brace-offset . 0)
6409 (cperl-label-offset . -4)
6410 (cperl-continued-statement-offset . 4)
6411 ;;(cperl-extra-newline-before-brace . nil) ; ???
6412 ;;(cperl-extra-newline-before-brace-multiline . nil)
6413 ;;(cperl-merge-trailing-else . nil) ; ???
6414 )
6415 ("Current"))
6416 "List of variables to set to get a particular indentation style.
6417 Should be used via `cperl-set-style' or via Perl menu.
6418
6419 See examples in `cperl-style-examples'.")
6420
6421 (defun cperl-set-style (style)
6422 "Set CPerl mode variables to use one of several different indentation styles.
6423 The arguments are a string representing the desired style.
6424 The list of styles is in `cperl-style-alist', available styles
6425 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6426
6427 The current value of style is memorized (unless there is a memorized
6428 data already), may be restored by `cperl-set-style-back'.
6429
6430 Chosing \"Current\" style will not change style, so this may be used for
6431 side-effect of memorizing only. Examples in `cperl-style-examples'."
6432 (interactive
6433 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6434 cperl-style-alist)))
6435 (list (completing-read "Enter style: " list nil 'insist))))
6436 (or cperl-old-style
6437 (setq cperl-old-style
6438 (mapcar (function
6439 (lambda (name)
6440 (cons name (eval name))))
6441 cperl-styles-entries)))
6442 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6443 (while style
6444 (setq setting (car style) style (cdr style))
6445 (set (car setting) (cdr setting)))))
6446
6447 (defun cperl-set-style-back ()
6448 "Restore a style memorized by `cperl-set-style'."
6449 (interactive)
6450 (or cperl-old-style (error "The style was not changed"))
6451 (let (setting)
6452 (while cperl-old-style
6453 (setq setting (car cperl-old-style)
6454 cperl-old-style (cdr cperl-old-style))
6455 (set (car setting) (cdr setting)))))
6456
6457 (defun cperl-check-syntax ()
6458 (interactive)
6459 (require 'mode-compile)
6460 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6461 (eval '(mode-compile)))) ; Avoid a warning
6462
6463 (defun cperl-info-buffer (type)
6464 ;; Returns buffer with documentation. Creates if missing.
6465 ;; If TYPE, this vars buffer.
6466 ;; Special care is taken to not stomp over an existing info buffer
6467 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6468 (info (get-buffer bname))
6469 (oldbuf (get-buffer "*info*")))
6470 (if info info
6471 (save-window-excursion
6472 ;; Get Info running
6473 (require 'info)
6474 (cond (oldbuf
6475 (set-buffer oldbuf)
6476 (rename-buffer "*info-perl-tmp*")))
6477 (save-window-excursion
6478 (info))
6479 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6480 (set-buffer "*info*")
6481 (rename-buffer bname)
6482 (cond (oldbuf
6483 (set-buffer "*info-perl-tmp*")
6484 (rename-buffer "*info*")
6485 (set-buffer bname)))
6486 (make-local-variable 'window-min-height)
6487 (setq window-min-height 2)
6488 (current-buffer)))))
6489
6490 (defun cperl-word-at-point (&optional p)
6491 "Return the word at point or at P."
6492 (save-excursion
6493 (if p (goto-char p))
6494 (or (cperl-word-at-point-hard)
6495 (progn
6496 (require 'etags)
6497 (funcall (or (and (boundp 'find-tag-default-function)
6498 find-tag-default-function)
6499 (get major-mode 'find-tag-default-function)
6500 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6501 ;; automatically used within `find-tag-default':
6502 'find-tag-default))))))
6503
6504 (defun cperl-info-on-command (command)
6505 "Show documentation for Perl command COMMAND in other window.
6506 If perl-info buffer is shown in some frame, uses this frame.
6507 Customized by setting variables `cperl-shrink-wrap-info-frame',
6508 `cperl-max-help-size'."
6509 (interactive
6510 (let* ((default (cperl-word-at-point))
6511 (read (read-string
6512 (format "Find doc for Perl function (default %s): "
6513 default))))
6514 (list (if (equal read "")
6515 default
6516 read))))
6517
6518 (let ((buffer (current-buffer))
6519 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6520 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6521 max-height char-height buf-list)
6522 (if (string-match "^-[a-zA-Z]$" command)
6523 (setq cmd-desc "^-X[ \t\n]"))
6524 (setq isvar (string-match "^[$@%]" command)
6525 buf (cperl-info-buffer isvar)
6526 iniwin (selected-window)
6527 fr1 (window-frame iniwin))
6528 (set-buffer buf)
6529 (goto-char (point-min))
6530 (or isvar
6531 (progn (re-search-forward "^-X[ \t\n]")
6532 (forward-line -1)))
6533 (if (re-search-forward cmd-desc nil t)
6534 (progn
6535 ;; Go back to beginning of the group (ex, for qq)
6536 (if (re-search-backward "^[ \t\n\f]")
6537 (forward-line 1))
6538 (beginning-of-line)
6539 ;; Get some of
6540 (setq pos (point)
6541 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6542 (while (and (not win) buf-list)
6543 (setq win (get-buffer-window (car buf-list) t))
6544 (setq buf-list (cdr buf-list)))
6545 (or (not win)
6546 (eq (window-buffer win) buf)
6547 (set-window-buffer win buf))
6548 (and win (setq fr2 (window-frame win)))
6549 (if (or (not fr2) (eq fr1 fr2))
6550 (pop-to-buffer buf)
6551 (special-display-popup-frame buf) ; Make it visible
6552 (select-window win))
6553 (goto-char pos) ; Needed (?!).
6554 ;; Resize
6555 (setq iniheight (window-height)
6556 frheight (frame-height)
6557 not-loner (< iniheight (1- frheight))) ; Are not alone
6558 (cond ((if not-loner cperl-max-help-size
6559 cperl-shrink-wrap-info-frame)
6560 (setq height
6561 (+ 2
6562 (count-lines
6563 pos
6564 (save-excursion
6565 (if (re-search-forward
6566 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6567 (match-beginning 0) (point-max)))))
6568 max-height
6569 (if not-loner
6570 (/ (* (- frheight 3) cperl-max-help-size) 100)
6571 (setq char-height (frame-char-height))
6572 ;; Non-functioning under OS/2:
6573 (if (eq char-height 1) (setq char-height 18))
6574 ;; Title, menubar, + 2 for slack
6575 (- (/ (x-display-pixel-height) char-height) 4)))
6576 (if (> height max-height) (setq height max-height))
6577 ;;(message "was %s doing %s" iniheight height)
6578 (if not-loner
6579 (enlarge-window (- height iniheight))
6580 (set-frame-height (window-frame win) (1+ height)))))
6581 (set-window-start (selected-window) pos))
6582 (message "No entry for %s found." command))
6583 ;;(pop-to-buffer buffer)
6584 (select-window iniwin)))
6585
6586 (defun cperl-info-on-current-command ()
6587 "Show documentation for Perl command at point in other window."
6588 (interactive)
6589 (cperl-info-on-command (cperl-word-at-point)))
6590
6591 (defun cperl-imenu-info-imenu-search ()
6592 (if (looking-at "^-X[ \t\n]") nil
6593 (re-search-backward
6594 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6595 (forward-line 1)))
6596
6597 (defun cperl-imenu-info-imenu-name ()
6598 (buffer-substring
6599 (match-beginning 1) (match-end 1)))
6600
6601 (defun cperl-imenu-on-info ()
6602 "Shows imenu for Perl Info Buffer.
6603 Opens Perl Info buffer if needed."
6604 (interactive)
6605 (let* ((buffer (current-buffer))
6606 imenu-create-index-function
6607 imenu-prev-index-position-function
6608 imenu-extract-index-name-function
6609 (index-item (save-restriction
6610 (save-window-excursion
6611 (set-buffer (cperl-info-buffer nil))
6612 (setq imenu-create-index-function
6613 'imenu-default-create-index-function
6614 imenu-prev-index-position-function
6615 'cperl-imenu-info-imenu-search
6616 imenu-extract-index-name-function
6617 'cperl-imenu-info-imenu-name)
6618 (imenu-choose-buffer-index)))))
6619 (and index-item
6620 (progn
6621 (push-mark)
6622 (pop-to-buffer "*info-perl*")
6623 (cond
6624 ((markerp (cdr index-item))
6625 (goto-char (marker-position (cdr index-item))))
6626 (t
6627 (goto-char (cdr index-item))))
6628 (set-window-start (selected-window) (point))
6629 (pop-to-buffer buffer)))))
6630
6631 (defun cperl-lineup (beg end &optional step minshift)
6632 "Lineup construction in a region.
6633 Beginning of region should be at the start of a construction.
6634 All first occurrences of this construction in the lines that are
6635 partially contained in the region are lined up at the same column.
6636
6637 MINSHIFT is the minimal amount of space to insert before the construction.
6638 STEP is the tabwidth to position constructions.
6639 If STEP is nil, `cperl-lineup-step' will be used
6640 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6641 Will not move the position at the start to the left."
6642 (interactive "r")
6643 (let (search col tcol seen b)
6644 (save-excursion
6645 (goto-char end)
6646 (end-of-line)
6647 (setq end (point-marker))
6648 (goto-char beg)
6649 (skip-chars-forward " \t\f")
6650 (setq beg (point-marker))
6651 (indent-region beg end nil)
6652 (goto-char beg)
6653 (setq col (current-column))
6654 (if (looking-at "[a-zA-Z0-9_]")
6655 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6656 (setq search
6657 (concat "\\<"
6658 (regexp-quote
6659 (buffer-substring (match-beginning 0)
6660 (match-end 0))) "\\>"))
6661 (error "Cannot line up in a middle of the word"))
6662 (if (looking-at "$")
6663 (error "Cannot line up end of line"))
6664 (setq search (regexp-quote (char-to-string (following-char)))))
6665 (setq step (or step cperl-lineup-step cperl-indent-level))
6666 (or minshift (setq minshift 1))
6667 (while (progn
6668 (beginning-of-line 2)
6669 (and (< (point) end)
6670 (re-search-forward search end t)
6671 (goto-char (match-beginning 0))))
6672 (setq tcol (current-column) seen t)
6673 (if (> tcol col) (setq col tcol)))
6674 (or seen
6675 (error "The construction to line up occurred only once"))
6676 (goto-char beg)
6677 (setq col (+ col minshift))
6678 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6679 (while
6680 (progn
6681 (cperl-make-indent col)
6682 (beginning-of-line 2)
6683 (and (< (point) end)
6684 (re-search-forward search end t)
6685 (goto-char (match-beginning 0)))))))) ; No body
6686
6687 (defun cperl-etags (&optional add all files) ;; NOT USED???
6688 "Run etags with appropriate options for Perl files.
6689 If optional argument ALL is `recursive', will process Perl files
6690 in subdirectories too."
6691 (interactive)
6692 (let ((cmd "etags")
6693 (args '("-l" "none" "-r"
6694 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6695 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6696 "-r"
6697 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6698 "-r"
6699 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6700 res)
6701 (if add (setq args (cons "-a" args)))
6702 (or files (setq files (list buffer-file-name)))
6703 (cond
6704 ((eq all 'recursive)
6705 ;;(error "Not implemented: recursive")
6706 (setq args (append (list "-e"
6707 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6708 use File::Find;
6709 find(\\&wanted, '.');
6710 exec @ARGV;"
6711 cmd) args)
6712 cmd "perl"))
6713 (all
6714 ;;(error "Not implemented: all")
6715 (setq args (append (list "-e"
6716 "push @ARGV, <*.PL *.pl *.pm>;
6717 exec @ARGV;"
6718 cmd) args)
6719 cmd "perl"))
6720 (t
6721 (setq args (append args files))))
6722 (setq res (apply 'call-process cmd nil nil nil args))
6723 (or (eq res 0)
6724 (message "etags returned \"%s\"" res))))
6725
6726 (defun cperl-toggle-auto-newline ()
6727 "Toggle the state of `cperl-auto-newline'."
6728 (interactive)
6729 (setq cperl-auto-newline (not cperl-auto-newline))
6730 (message "Newlines will %sbe auto-inserted now."
6731 (if cperl-auto-newline "" "not ")))
6732
6733 (defun cperl-toggle-abbrev ()
6734 "Toggle the state of automatic keyword expansion in CPerl mode."
6735 (interactive)
6736 (abbrev-mode (if abbrev-mode 0 1))
6737 (message "Perl control structure will %sbe auto-inserted now."
6738 (if abbrev-mode "" "not ")))
6739
6740
6741 (defun cperl-toggle-electric ()
6742 "Toggle the state of parentheses doubling in CPerl mode."
6743 (interactive)
6744 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6745 (message "Parentheses will %sbe auto-doubled now."
6746 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6747
6748 (defun cperl-toggle-autohelp ()
6749 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6750 Delay of auto-help controlled by `cperl-lazy-help-time'."
6751 (interactive)
6752 (if (fboundp 'run-with-idle-timer)
6753 (progn
6754 (if cperl-lazy-installed
6755 (cperl-lazy-unstall)
6756 (cperl-lazy-install))
6757 (message "Perl help messages will %sbe automatically shown now."
6758 (if cperl-lazy-installed "" "not ")))
6759 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6760
6761 (defun cperl-toggle-construct-fix ()
6762 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6763 (interactive)
6764 (setq cperl-indent-region-fix-constructs
6765 (if cperl-indent-region-fix-constructs
6766 nil
6767 1))
6768 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6769 (if cperl-indent-region-fix-constructs "" "not ")))
6770
6771 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6772 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6773 Nonpositive numeric argument disables debugging messages. The message
6774 summarizes which regions it was decided to rescan for syntactic constructs.
6775
6776 The message looks like this:
6777
6778 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6779
6780 Numbers are character positions in the buffer. REQ provides the range to
6781 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6782 for correct operation it should start and end outside any special syntactic
6783 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6784 by CPerl."
6785 (interactive "P")
6786 (or arg
6787 (setq arg (if (eq cperl-syntaxify-by-font-lock
6788 (if backtrace 'backtrace 'message)) 0 1)))
6789 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6790 (setq cperl-syntaxify-by-font-lock arg)
6791 (message "Debugging messages of syntax unwind %sabled."
6792 (if (eq arg t) "dis" "en")))
6793
6794 ;;;; Tags file creation.
6795
6796 (defvar cperl-tmp-buffer " *cperl-tmp*")
6797
6798 (defun cperl-setup-tmp-buf ()
6799 (set-buffer (get-buffer-create cperl-tmp-buffer))
6800 (set-syntax-table cperl-mode-syntax-table)
6801 (buffer-disable-undo)
6802 (auto-fill-mode 0)
6803 (if cperl-use-syntax-table-text-property-for-tags
6804 (progn
6805 (make-local-variable 'parse-sexp-lookup-properties)
6806 ;; Do not introduce variable if not needed, we check it!
6807 (set 'parse-sexp-lookup-properties t))))
6808
6809 (defun cperl-xsub-scan ()
6810 (require 'imenu)
6811 (let ((index-alist '())
6812 (prev-pos 0) index index1 name package prefix)
6813 (goto-char (point-min))
6814 ;; Search for the function
6815 (progn ;;save-match-data
6816 (while (re-search-forward
6817 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6818 nil t)
6819 (cond
6820 ((match-beginning 2) ; SECTION
6821 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6822 (goto-char (match-beginning 0))
6823 (skip-chars-forward " \t")
6824 (forward-char 1)
6825 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6826 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6827 (setq prefix nil)))
6828 ((not package) nil) ; C language section
6829 ((match-beginning 3) ; XSUB
6830 (goto-char (1+ (match-beginning 3)))
6831 (setq index (imenu-example--name-and-position))
6832 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6833 (if (and prefix (string-match (concat "^" prefix) name))
6834 (setq name (substring name (length prefix))))
6835 (cond ((string-match "::" name) nil)
6836 (t
6837 (setq index1 (cons (concat package "::" name) (cdr index)))
6838 (push index1 index-alist)))
6839 (setcar index name)
6840 (push index index-alist))
6841 (t ; BOOT: section
6842 ;; (beginning-of-line)
6843 (setq index (imenu-example--name-and-position))
6844 (setcar index (concat package "::BOOT:"))
6845 (push index index-alist)))))
6846 index-alist))
6847
6848 (defvar cperl-unreadable-ok nil)
6849
6850 (defun cperl-find-tags (ifile xs topdir)
6851 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6852 (cperl-pod-here-fontify nil) f file)
6853 (save-excursion
6854 (if b (set-buffer b)
6855 (cperl-setup-tmp-buf))
6856 (erase-buffer)
6857 (condition-case err
6858 (setq file (car (insert-file-contents ifile)))
6859 (error (if cperl-unreadable-ok nil
6860 (if (y-or-n-p
6861 (format "File %s unreadable. Continue? " ifile))
6862 (setq cperl-unreadable-ok t)
6863 (error "Aborting: unreadable file %s" ifile)))))
6864 (if (not file)
6865 (message "Unreadable file %s" ifile)
6866 (message "Scanning file %s ..." file)
6867 (if (and cperl-use-syntax-table-text-property-for-tags
6868 (not xs))
6869 (condition-case err ; after __END__ may have garbage
6870 (cperl-find-pods-heres nil nil noninteractive)
6871 (error (message "While scanning for syntax: %s" err))))
6872 (if xs
6873 (setq lst (cperl-xsub-scan))
6874 (setq ind (cperl-imenu--create-perl-index))
6875 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6876 (setq lst
6877 (mapcar
6878 (function
6879 (lambda (elt)
6880 (cond ((string-match "^[_a-zA-Z]" (car elt))
6881 (goto-char (cdr elt))
6882 (beginning-of-line) ; pos should be of the start of the line
6883 (list (car elt)
6884 (point)
6885 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6886 (buffer-substring (progn
6887 (goto-char (cdr elt))
6888 ;; After name now...
6889 (or (eolp) (forward-char 1))
6890 (point))
6891 (progn
6892 (beginning-of-line)
6893 (point))))))))
6894 lst))
6895 (erase-buffer)
6896 (while lst
6897 (setq elt (car lst) lst (cdr lst))
6898 (if elt
6899 (progn
6900 (insert (elt elt 3)
6901 127
6902 (if (string-match "^package " (car elt))
6903 (substring (car elt) 8)
6904 (car elt) )
6905 1
6906 (number-to-string (elt elt 2)) ; Line
6907 ","
6908 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
6909 "\n")
6910 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
6911 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
6912 (elt elt 3)))
6913 ;; Need to insert the name without package as well
6914 (setq lst (cons (cons (substring (elt elt 3)
6915 (match-beginning 1)
6916 (match-end 1))
6917 (cdr elt))
6918 lst))))))
6919 (setq pos (point))
6920 (goto-char 1)
6921 (setq rel file)
6922 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
6923 (set-text-properties 0 (length rel) nil rel)
6924 (and (equal topdir (substring rel 0 (length topdir)))
6925 (setq rel (substring file (length topdir))))
6926 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
6927 (setq ret (buffer-substring 1 (point-max)))
6928 (erase-buffer)
6929 (or noninteractive
6930 (message "Scanning file %s finished" file))
6931 ret))))
6932
6933 (defun cperl-add-tags-recurse-noxs ()
6934 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
6935 Use as
6936 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6937 -f cperl-add-tags-recurse-noxs
6938 "
6939 (cperl-write-tags nil nil t t nil t))
6940
6941 (defun cperl-add-tags-recurse-noxs-fullpath ()
6942 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
6943 Writes down fullpath, so TAGS is relocatable (but if the build directory
6944 is relocated, the file TAGS inside it breaks). Use as
6945 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6946 -f cperl-add-tags-recurse-noxs-fullpath
6947 "
6948 (cperl-write-tags nil nil t t nil t ""))
6949
6950 (defun cperl-add-tags-recurse ()
6951 "Add to TAGS file data for Perl files in the current directory and kids.
6952 Use as
6953 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
6954 -f cperl-add-tags-recurse
6955 "
6956 (cperl-write-tags nil nil t t))
6957
6958 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
6959 ;; If INBUFFER, do not select buffer, and do not save
6960 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
6961 (require 'etags)
6962 (if file nil
6963 (setq file (if dir default-directory (buffer-file-name)))
6964 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
6965 (or topdir
6966 (setq topdir default-directory))
6967 (let ((tags-file-name "TAGS")
6968 (case-fold-search (eq system-type 'emx))
6969 xs rel tm)
6970 (save-excursion
6971 (cond (inbuffer nil) ; Already there
6972 ((file-exists-p tags-file-name)
6973 (if cperl-xemacs-p
6974 (visit-tags-table-buffer)
6975 (visit-tags-table-buffer tags-file-name)))
6976 (t (set-buffer (find-file-noselect tags-file-name))))
6977 (cond
6978 (dir
6979 (cond ((eq erase 'ignore))
6980 (erase
6981 (erase-buffer)
6982 (setq erase 'ignore)))
6983 (let ((files
6984 (condition-case err
6985 (directory-files file t
6986 (if recurse nil cperl-scan-files-regexp)
6987 t)
6988 (error
6989 (if cperl-unreadable-ok nil
6990 (if (y-or-n-p
6991 (format "Directory %s unreadable. Continue? " file))
6992 (setq cperl-unreadable-ok t
6993 tm nil) ; Return empty list
6994 (error "Aborting: unreadable directory %s" file)))))))
6995 (mapcar (function
6996 (lambda (file)
6997 (cond
6998 ((string-match cperl-noscan-files-regexp file)
6999 nil)
7000 ((not (file-directory-p file))
7001 (if (string-match cperl-scan-files-regexp file)
7002 (cperl-write-tags file erase recurse nil t noxs topdir)))
7003 ((not recurse) nil)
7004 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7005 files)))
7006 (t
7007 (setq xs (string-match "\\.xs$" file))
7008 (if (not (and xs noxs))
7009 (progn
7010 (cond ((eq erase 'ignore) (goto-char (point-max)))
7011 (erase (erase-buffer))
7012 (t
7013 (goto-char 1)
7014 (setq rel file)
7015 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7016 (set-text-properties 0 (length rel) nil rel)
7017 (and (equal topdir (substring rel 0 (length topdir)))
7018 (setq rel (substring file (length topdir))))
7019 (if (search-forward (concat "\f\n" rel ",") nil t)
7020 (progn
7021 (search-backward "\f\n")
7022 (delete-region (point)
7023 (save-excursion
7024 (forward-char 1)
7025 (if (search-forward "\f\n"
7026 nil 'toend)
7027 (- (point) 2)
7028 (point-max)))))
7029 (goto-char (point-max)))))
7030 (insert (cperl-find-tags file xs topdir))))))
7031 (if inbuffer nil ; Delegate to the caller
7032 (save-buffer 0) ; No backup
7033 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7034 (initialize-new-tags-table))))))
7035
7036 (defvar cperl-tags-hier-regexp-list
7037 (concat
7038 "^\\("
7039 "\\(package\\)\\>"
7040 "\\|"
7041 "sub\\>[^\n]+::"
7042 "\\|"
7043 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7044 "\\|"
7045 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7046 "\\)"))
7047
7048 (defvar cperl-hierarchy '(() ())
7049 "Global hierarchy of classes.")
7050
7051 (defun cperl-tags-hier-fill ()
7052 ;; Suppose we are in a tag table cooked by cperl.
7053 (goto-char 1)
7054 (let (type pack name pos line chunk ord cons1 file str info fileind)
7055 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7056 (setq pos (match-beginning 0)
7057 pack (match-beginning 2))
7058 (beginning-of-line)
7059 (if (looking-at (concat
7060 "\\([^\n]+\\)"
7061 "\C-?"
7062 "\\([^\n]+\\)"
7063 "\C-a"
7064 "\\([0-9]+\\)"
7065 ","
7066 "\\([0-9]+\\)"))
7067 (progn
7068 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7069 name (buffer-substring (match-beginning 2) (match-end 2))
7070 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7071 line (buffer-substring (match-beginning 3) (match-end 3))
7072 ord (if pack 1 0)
7073 file (file-of-tag)
7074 fileind (format "%s:%s" file line)
7075 ;; Moves to beginning of the next line:
7076 info (cperl-etags-snarf-tag file line))
7077 ;; Move back
7078 (forward-char -1)
7079 ;; Make new member of hierarchy name ==> file ==> pos if needed
7080 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7081 ;; Name known
7082 (setcdr cons1 (cons (cons fileind (vector file info))
7083 (cdr cons1)))
7084 ;; First occurrence of the name, start alist
7085 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7086 (if pack
7087 (setcar (cdr cperl-hierarchy)
7088 (cons cons1 (nth 1 cperl-hierarchy)))
7089 (setcar cperl-hierarchy
7090 (cons cons1 (car cperl-hierarchy)))))))
7091 (end-of-line))))
7092
7093 (defun cperl-tags-hier-init (&optional update)
7094 "Show hierarchical menu of classes and methods.
7095 Finds info about classes by a scan of loaded TAGS files.
7096 Supposes that the TAGS files contain fully qualified function names.
7097 One may build such TAGS files from CPerl mode menu."
7098 (interactive)
7099 (require 'etags)
7100 (require 'imenu)
7101 (if (or update (null (nth 2 cperl-hierarchy)))
7102 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7103 (or (nthcdr 2 elt)
7104 ;; Only in one file
7105 (setcdr elt (cdr (nth 1 elt)))))))
7106 pack name cons1 to l1 l2 l3 l4 b)
7107 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7108 (setq cperl-hierarchy (list l1 l2 l3))
7109 (if cperl-xemacs-p ; Not checked
7110 (progn
7111 (or tags-file-name
7112 ;; Does this work in XEmacs?
7113 (call-interactively 'visit-tags-table))
7114 (message "Updating list of classes...")
7115 (set-buffer (get-file-buffer tags-file-name))
7116 (cperl-tags-hier-fill))
7117 (or tags-table-list
7118 (call-interactively 'visit-tags-table))
7119 (mapcar
7120 (function
7121 (lambda (tagsfile)
7122 (message "Updating list of classes... %s" tagsfile)
7123 (set-buffer (get-file-buffer tagsfile))
7124 (cperl-tags-hier-fill)))
7125 tags-table-list)
7126 (message "Updating list of classes... postprocessing..."))
7127 (mapcar remover (car cperl-hierarchy))
7128 (mapcar remover (nth 1 cperl-hierarchy))
7129 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7130 (cons "Methods: " (car cperl-hierarchy))))
7131 (cperl-tags-treeify to 1)
7132 (setcar (nthcdr 2 cperl-hierarchy)
7133 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7134 (message "Updating list of classes: done, requesting display...")
7135 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7136 ))
7137 (or (nth 2 cperl-hierarchy)
7138 (error "No items found"))
7139 (setq update
7140 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7141 (if (if (fboundp 'display-popup-menus-p)
7142 (let ((f 'display-popup-menus-p))
7143 (funcall f))
7144 window-system)
7145 (x-popup-menu t (nth 2 cperl-hierarchy))
7146 (require 'tmm)
7147 (tmm-prompt (nth 2 cperl-hierarchy))))
7148 (if (and update (listp update))
7149 (progn (while (cdr update) (setq update (cdr update)))
7150 (setq update (car update)))) ; Get the last from the list
7151 (if (vectorp update)
7152 (progn
7153 (find-file (elt update 0))
7154 (cperl-etags-goto-tag-location (elt update 1))))
7155 (if (eq update -999) (cperl-tags-hier-init t)))
7156
7157 (defun cperl-tags-treeify (to level)
7158 ;; cadr of `to' is read-write. On start it is a cons
7159 (let* ((regexp (concat "^\\(" (mapconcat
7160 'identity
7161 (make-list level "[_a-zA-Z0-9]+")
7162 "::")
7163 "\\)\\(::\\)?"))
7164 (packages (cdr (nth 1 to)))
7165 (methods (cdr (nth 2 to)))
7166 l1 head tail cons1 cons2 ord writeto packs recurse
7167 root-packages root-functions ms many_ms same_name ps
7168 (move-deeper
7169 (function
7170 (lambda (elt)
7171 (cond ((and (string-match regexp (car elt))
7172 (or (eq ord 1) (match-end 2)))
7173 (setq head (substring (car elt) 0 (match-end 1))
7174 tail (if (match-end 2) (substring (car elt)
7175 (match-end 2)))
7176 recurse t)
7177 (if (setq cons1 (assoc head writeto)) nil
7178 ;; Need to init new head
7179 (setcdr writeto (cons (list head (list "Packages: ")
7180 (list "Methods: "))
7181 (cdr writeto)))
7182 (setq cons1 (nth 1 writeto)))
7183 (setq cons2 (nth ord cons1)) ; Either packs or meths
7184 (setcdr cons2 (cons elt (cdr cons2))))
7185 ((eq ord 2)
7186 (setq root-functions (cons elt root-functions)))
7187 (t
7188 (setq root-packages (cons elt root-packages))))))))
7189 (setcdr to l1) ; Init to dynamic space
7190 (setq writeto to)
7191 (setq ord 1)
7192 (mapcar move-deeper packages)
7193 (setq ord 2)
7194 (mapcar move-deeper methods)
7195 (if recurse
7196 (mapcar (function (lambda (elt)
7197 (cperl-tags-treeify elt (1+ level))))
7198 (cdr to)))
7199 ;;Now clean up leaders with one child only
7200 (mapcar (function (lambda (elt)
7201 (if (not (and (listp (cdr elt))
7202 (eq (length elt) 2))) nil
7203 (setcar elt (car (nth 1 elt)))
7204 (setcdr elt (cdr (nth 1 elt))))))
7205 (cdr to))
7206 ;; Sort the roots of subtrees
7207 (if (default-value 'imenu-sort-function)
7208 (setcdr to
7209 (sort (cdr to) (default-value 'imenu-sort-function))))
7210 ;; Now add back functions removed from display
7211 (mapcar (function (lambda (elt)
7212 (setcdr to (cons elt (cdr to)))))
7213 (if (default-value 'imenu-sort-function)
7214 (nreverse
7215 (sort root-functions (default-value 'imenu-sort-function)))
7216 root-functions))
7217 ;; Now add back packages removed from display
7218 (mapcar (function (lambda (elt)
7219 (setcdr to (cons (cons (concat "package " (car elt))
7220 (cdr elt))
7221 (cdr to)))))
7222 (if (default-value 'imenu-sort-function)
7223 (nreverse
7224 (sort root-packages (default-value 'imenu-sort-function)))
7225 root-packages))))
7226
7227 ;;;(x-popup-menu t
7228 ;;; '(keymap "Name1"
7229 ;;; ("Ret1" "aa")
7230 ;;; ("Head1" "ab"
7231 ;;; keymap "Name2"
7232 ;;; ("Tail1" "x") ("Tail2" "y"))))
7233
7234 (defun cperl-list-fold (list name limit)
7235 (let (list1 list2 elt1 (num 0))
7236 (if (<= (length list) limit) list
7237 (setq list1 nil list2 nil)
7238 (while list
7239 (setq num (1+ num)
7240 elt1 (car list)
7241 list (cdr list))
7242 (if (<= num imenu-max-items)
7243 (setq list2 (cons elt1 list2))
7244 (setq list1 (cons (cons name
7245 (nreverse list2))
7246 list1)
7247 list2 (list elt1)
7248 num 1)))
7249 (nreverse (cons (cons name
7250 (nreverse list2))
7251 list1)))))
7252
7253 (defun cperl-menu-to-keymap (menu &optional name)
7254 (let (list)
7255 (cons 'keymap
7256 (mapcar
7257 (function
7258 (lambda (elt)
7259 (cond ((listp (cdr elt))
7260 (setq list (cperl-list-fold
7261 (cdr elt) (car elt) imenu-max-items))
7262 (cons nil
7263 (cons (car elt)
7264 (cperl-menu-to-keymap list))))
7265 (t
7266 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7267 (cperl-list-fold menu "Root" imenu-max-items)))))
7268
7269 \f
7270 (defvar cperl-bad-style-regexp
7271 (mapconcat 'identity
7272 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7273 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7274 "\\|")
7275 "Finds places such that insertion of a whitespace may help a lot.")
7276
7277 (defvar cperl-not-bad-style-regexp
7278 (mapconcat
7279 'identity
7280 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7281 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7282 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7283 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7284 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7285 "-[0-9]" ; -5
7286 "\\+\\+" ; ++var
7287 "--" ; --var
7288 ".->" ; a->b
7289 "->" ; a SPACE ->b
7290 "\\[-" ; a[-1]
7291 "\\\\[&$@*\\\\]" ; \&func
7292 "^=" ; =head
7293 "\\$." ; $|
7294 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7295 "||"
7296 "&&"
7297 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7298 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7299 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7300 ;;"[*/+-|&<.]+="
7301 )
7302 "\\|")
7303 "If matches at the start of match found by `my-bad-c-style-regexp',
7304 insertion of a whitespace will not help.")
7305
7306 (defvar found-bad)
7307
7308 (defun cperl-find-bad-style ()
7309 "Find places in the buffer where insertion of a whitespace may help.
7310 Prompts user for insertion of spaces.
7311 Currently it is tuned to C and Perl syntax."
7312 (interactive)
7313 (let (found-bad (p (point)))
7314 (setq last-nonmenu-event 13) ; To disable popup
7315 (goto-char (point-min))
7316 (map-y-or-n-p "Insert space here? "
7317 (lambda (arg) (insert " "))
7318 'cperl-next-bad-style
7319 '("location" "locations" "insert a space into")
7320 '((?\C-r (lambda (arg)
7321 (let ((buffer-quit-function
7322 'exit-recursive-edit))
7323 (message "Exit with Esc Esc")
7324 (recursive-edit)
7325 t)) ; Consider acted upon
7326 "edit, exit with Esc Esc")
7327 (?e (lambda (arg)
7328 (let ((buffer-quit-function
7329 'exit-recursive-edit))
7330 (message "Exit with Esc Esc")
7331 (recursive-edit)
7332 t)) ; Consider acted upon
7333 "edit, exit with Esc Esc"))
7334 t)
7335 (if found-bad (goto-char found-bad)
7336 (goto-char p)
7337 (message "No appropriate place found"))))
7338
7339 (defun cperl-next-bad-style ()
7340 (let (p (not-found t) (point (point)) found)
7341 (while (and not-found
7342 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7343 (setq p (point))
7344 (goto-char (match-beginning 0))
7345 (if (or
7346 (looking-at cperl-not-bad-style-regexp)
7347 ;; Check for a < -b and friends
7348 (and (eq (following-char) ?\-)
7349 (save-excursion
7350 (skip-chars-backward " \t\n")
7351 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7352 ;; Now check for syntax type
7353 (save-match-data
7354 (setq found (point))
7355 (beginning-of-defun)
7356 (let ((pps (parse-partial-sexp (point) found)))
7357 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7358 (goto-char (match-end 0))
7359 (goto-char (1- p))
7360 (setq not-found nil
7361 found-bad found)))
7362 (not not-found)))
7363
7364 \f
7365 ;;; Getting help
7366 (defvar cperl-have-help-regexp
7367 ;;(concat "\\("
7368 (mapconcat
7369 'identity
7370 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7371 "[$@]\\^[a-zA-Z]" ; Special variable
7372 "[$@][^ \n\t]" ; Special variable
7373 "-[a-zA-Z]" ; File test
7374 "\\\\[a-zA-Z0]" ; Special chars
7375 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7376 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7377 "[a-zA-Z_0-9:]+" ; symbol or number
7378 "x="
7379 "#!")
7380 ;;"\\)\\|\\("
7381 "\\|")
7382 ;;"\\)"
7383 ;;)
7384 "Matches places in the buffer we can find help for.")
7385
7386 (defvar cperl-message-on-help-error t)
7387 (defvar cperl-help-from-timer nil)
7388
7389 (defun cperl-word-at-point-hard ()
7390 ;; Does not save-excursion
7391 ;; Get to the something meaningful
7392 (or (eobp) (eolp) (forward-char 1))
7393 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7394 (save-excursion (beginning-of-line) (point))
7395 'to-beg)
7396 ;; (cond
7397 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7398 ;; (skip-chars-backward " \n\t\r({[]});,")
7399 ;; (or (bobp) (backward-char 1))))
7400 ;; Try to backtrace
7401 (cond
7402 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7403 (skip-chars-backward "a-zA-Z0-9_:")
7404 (cond
7405 ((and (eq (preceding-char) ?^) ; $^I
7406 (eq (char-after (- (point) 2)) ?\$))
7407 (forward-char -2))
7408 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7409 (forward-char -1))
7410 ((and (eq (preceding-char) ?\=)
7411 (eq (current-column) 1))
7412 (forward-char -1))) ; =head1
7413 (if (and (eq (preceding-char) ?\<)
7414 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7415 (forward-char -1)))
7416 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7417 (forward-char -1))
7418 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7419 (forward-char -1))
7420 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7421 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7422 (cond
7423 ((and (eq (preceding-char) ?\$)
7424 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7425 (forward-char -1))
7426 ((and (eq (following-char) ?\>)
7427 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7428 (save-excursion
7429 (forward-sexp -1)
7430 (and (eq (preceding-char) ?\<)
7431 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7432 (search-backward "<"))))
7433 ((and (eq (following-char) ?\$)
7434 (eq (preceding-char) ?\<)
7435 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7436 (forward-char -1)))
7437 (if (looking-at cperl-have-help-regexp)
7438 (buffer-substring (match-beginning 0) (match-end 0))))
7439
7440 (defun cperl-get-help ()
7441 "Get one-line docs on the symbol at the point.
7442 The data for these docs is a little bit obsolete and may be in fact longer
7443 than a line. Your contribution to update/shorten it is appreciated."
7444 (interactive)
7445 (save-match-data ; May be called "inside" query-replace
7446 (save-excursion
7447 (let ((word (cperl-word-at-point-hard)))
7448 (if word
7449 (if (and cperl-help-from-timer ; Bail out if not in mainland
7450 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7451 (or (memq (get-text-property (point) 'face)
7452 '(font-lock-comment-face font-lock-string-face))
7453 (memq (get-text-property (point) 'syntax-type)
7454 '(pod here-doc format))))
7455 nil
7456 (cperl-describe-perl-symbol word))
7457 (if cperl-message-on-help-error
7458 (message "Nothing found for %s..."
7459 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7460
7461 ;;; Stolen from perl-descr.el by Johan Vromans:
7462
7463 (defvar cperl-doc-buffer " *perl-doc*"
7464 "Where the documentation can be found.")
7465
7466 (defun cperl-describe-perl-symbol (val)
7467 "Display the documentation of symbol at point, a Perl operator."
7468 (let ((enable-recursive-minibuffers t)
7469 args-file regexp)
7470 (cond
7471 ((string-match "^[&*][a-zA-Z_]" val)
7472 (setq val (concat (substring val 0 1) "NAME")))
7473 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7474 (setq val (concat "@" (substring val 1 (match-end 1)))))
7475 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7476 (setq val (concat "%" (substring val 1 (match-end 1)))))
7477 ((and (string= val "x") (string-match "^x=" val))
7478 (setq val "x="))
7479 ((string-match "^\\$[\C-a-\C-z]" val)
7480 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7481 ((string-match "^CORE::" val)
7482 (setq val "CORE::"))
7483 ((string-match "^SUPER::" val)
7484 (setq val "SUPER::"))
7485 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7486 (setq val "<NAME>")))
7487 (setq regexp (concat "^"
7488 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7489 (regexp-quote val)
7490 "\\([ \t([/]\\|$\\)"))
7491
7492 ;; get the buffer with the documentation text
7493 (cperl-switch-to-doc-buffer)
7494
7495 ;; lookup in the doc
7496 (goto-char (point-min))
7497 (let ((case-fold-search nil))
7498 (list
7499 (if (re-search-forward regexp (point-max) t)
7500 (save-excursion
7501 (beginning-of-line 1)
7502 (let ((lnstart (point)))
7503 (end-of-line)
7504 (message "%s" (buffer-substring lnstart (point)))))
7505 (if cperl-message-on-help-error
7506 (message "No definition for %s" val)))))))
7507
7508 (defvar cperl-short-docs 'please-ignore-this-line
7509 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7510 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7511 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7512 ! ... Logical negation.
7513 ... != ... Numeric inequality.
7514 ... !~ ... Search pattern, substitution, or translation (negated).
7515 $! In numeric context: errno. In a string context: error string.
7516 $\" The separator which joins elements of arrays interpolated in strings.
7517 $# The output format for printed numbers. Default is %.15g or close.
7518 $$ Process number of this script. Changes in the fork()ed child process.
7519 $% The current page number of the currently selected output channel.
7520
7521 The following variables are always local to the current block:
7522
7523 $1 Match of the 1st set of parentheses in the last match (auto-local).
7524 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7525 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7526 $4 Match of the 4th set of parentheses in the last match (auto-local).
7527 $5 Match of the 5th set of parentheses in the last match (auto-local).
7528 $6 Match of the 6th set of parentheses in the last match (auto-local).
7529 $7 Match of the 7th set of parentheses in the last match (auto-local).
7530 $8 Match of the 8th set of parentheses in the last match (auto-local).
7531 $9 Match of the 9th set of parentheses in the last match (auto-local).
7532 $& The string matched by the last pattern match (auto-local).
7533 $' The string after what was matched by the last match (auto-local).
7534 $` The string before what was matched by the last match (auto-local).
7535
7536 $( The real gid of this process.
7537 $) The effective gid of this process.
7538 $* Deprecated: Set to 1 to do multiline matching within a string.
7539 $+ The last bracket matched by the last search pattern.
7540 $, The output field separator for the print operator.
7541 $- The number of lines left on the page.
7542 $. The current input line number of the last filehandle that was read.
7543 $/ The input record separator, newline by default.
7544 $0 Name of the file containing the current perl script (read/write).
7545 $: String may be broken after these characters to fill ^-lines in a format.
7546 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7547 $< The real uid of this process.
7548 $= The page length of the current output channel. Default is 60 lines.
7549 $> The effective uid of this process.
7550 $? The status returned by the last ``, pipe close or `system'.
7551 $@ The perl error message from the last eval or do @var{EXPR} command.
7552 $ARGV The name of the current file used with <> .
7553 $[ Deprecated: The index of the first element/char in an array/string.
7554 $\\ The output record separator for the print operator.
7555 $] The perl version string as displayed with perl -v.
7556 $^ The name of the current top-of-page format.
7557 $^A The current value of the write() accumulator for format() lines.
7558 $^D The value of the perl debug (-D) flags.
7559 $^E Information about the last system error other than that provided by $!.
7560 $^F The highest system file descriptor, ordinarily 2.
7561 $^H The current set of syntax checks enabled by `use strict'.
7562 $^I The value of the in-place edit extension (perl -i option).
7563 $^L What formats output to perform a formfeed. Default is \\f.
7564 $^M A buffer for emergency memory allocation when running out of memory.
7565 $^O The operating system name under which this copy of Perl was built.
7566 $^P Internal debugging flag.
7567 $^T The time the script was started. Used by -A/-M/-C file tests.
7568 $^W True if warnings are requested (perl -w flag).
7569 $^X The name under which perl was invoked (argv[0] in C-speech).
7570 $_ The default input and pattern-searching space.
7571 $| Auto-flush after write/print on current output channel? Default 0.
7572 $~ The name of the current report format.
7573 ... % ... Modulo division.
7574 ... %= ... Modulo division assignment.
7575 %ENV Contains the current environment.
7576 %INC List of files that have been require-d or do-ne.
7577 %SIG Used to set signal handlers for various signals.
7578 ... & ... Bitwise and.
7579 ... && ... Logical and.
7580 ... &&= ... Logical and assignment.
7581 ... &= ... Bitwise and assignment.
7582 ... * ... Multiplication.
7583 ... ** ... Exponentiation.
7584 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7585 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7586 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7587 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7588 ... += ... Addition assignment.
7589 , Comma operator.
7590 ... - ... Subtraction.
7591 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7592 ... -= ... Subtraction assignment.
7593 -A Access time in days since script started.
7594 -B File is a non-text (binary) file.
7595 -C Inode change time in days since script started.
7596 -M Age in days since script started.
7597 -O File is owned by real uid.
7598 -R File is readable by real uid.
7599 -S File is a socket .
7600 -T File is a text file.
7601 -W File is writable by real uid.
7602 -X File is executable by real uid.
7603 -b File is a block special file.
7604 -c File is a character special file.
7605 -d File is a directory.
7606 -e File exists .
7607 -f File is a plain file.
7608 -g File has setgid bit set.
7609 -k File has sticky bit set.
7610 -l File is a symbolic link.
7611 -o File is owned by effective uid.
7612 -p File is a named pipe (FIFO).
7613 -r File is readable by effective uid.
7614 -s File has non-zero size.
7615 -t Tests if filehandle (STDIN by default) is opened to a tty.
7616 -u File has setuid bit set.
7617 -w File is writable by effective uid.
7618 -x File is executable by effective uid.
7619 -z File has zero size.
7620 . Concatenate strings.
7621 .. Range (list context); flip/flop (scalar context) operator.
7622 .= Concatenate assignment strings
7623 ... / ... Division. /PATTERN/ioxsmg Pattern match
7624 ... /= ... Division assignment.
7625 /PATTERN/ioxsmg Pattern match.
7626 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7627 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7628 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7629 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7630 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7631 ... <= ... Numeric less than or equal to.
7632 ... <=> ... Numeric compare.
7633 ... = ... Assignment.
7634 ... == ... Numeric equality.
7635 ... =~ ... Search pattern, substitution, or translation
7636 ... > ... Numeric greater than.
7637 ... >= ... Numeric greater than or equal to.
7638 ... >> ... Bitwise shift right.
7639 ... >>= ... Bitwise shift right assignment.
7640 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7641 ?PATTERN? One-time pattern match.
7642 @ARGV Command line arguments (not including the command name - see $0).
7643 @INC List of places to look for perl scripts during do/include/use.
7644 @_ Parameter array for subroutines; result of split() unless in list context.
7645 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7646 \\0 Octal char, e.g. \\033.
7647 \\E Case modification terminator. See \\Q, \\L, and \\U.
7648 \\L Lowercase until \\E . See also \\l, lc.
7649 \\U Upcase until \\E . See also \\u, uc.
7650 \\Q Quote metacharacters until \\E . See also quotemeta.
7651 \\a Alarm character (octal 007).
7652 \\b Backspace character (octal 010).
7653 \\c Control character, e.g. \\c[ .
7654 \\e Escape character (octal 033).
7655 \\f Formfeed character (octal 014).
7656 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7657 \\n Newline character (octal 012 on most systems).
7658 \\r Return character (octal 015 on most systems).
7659 \\t Tab character (octal 011).
7660 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7661 \\x Hex character, e.g. \\x1b.
7662 ... ^ ... Bitwise exclusive or.
7663 __END__ Ends program source.
7664 __DATA__ Ends program source.
7665 __FILE__ Current (source) filename.
7666 __LINE__ Current line in current source.
7667 __PACKAGE__ Current package.
7668 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7669 ARGVOUT Output filehandle with -i flag.
7670 BEGIN { ... } Immediately executed (during compilation) piece of code.
7671 END { ... } Pseudo-subroutine executed after the script finishes.
7672 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7673 INIT { ... } Pseudo-subroutine executed before the script starts running.
7674 DATA Input filehandle for what follows after __END__ or __DATA__.
7675 accept(NEWSOCKET,GENERICSOCKET)
7676 alarm(SECONDS)
7677 atan2(X,Y)
7678 bind(SOCKET,NAME)
7679 binmode(FILEHANDLE)
7680 caller[(LEVEL)]
7681 chdir(EXPR)
7682 chmod(LIST)
7683 chop[(LIST|VAR)]
7684 chown(LIST)
7685 chroot(FILENAME)
7686 close(FILEHANDLE)
7687 closedir(DIRHANDLE)
7688 ... cmp ... String compare.
7689 connect(SOCKET,NAME)
7690 continue of { block } continue { block }. Is executed after `next' or at end.
7691 cos(EXPR)
7692 crypt(PLAINTEXT,SALT)
7693 dbmclose(%HASH)
7694 dbmopen(%HASH,DBNAME,MODE)
7695 defined(EXPR)
7696 delete($HASH{KEY})
7697 die(LIST)
7698 do { ... }|SUBR while|until EXPR executes at least once
7699 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7700 dump LABEL
7701 each(%HASH)
7702 endgrent
7703 endhostent
7704 endnetent
7705 endprotoent
7706 endpwent
7707 endservent
7708 eof[([FILEHANDLE])]
7709 ... eq ... String equality.
7710 eval(EXPR) or eval { BLOCK }
7711 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7712 exit(EXPR)
7713 exp(EXPR)
7714 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7715 fileno(FILEHANDLE)
7716 flock(FILEHANDLE,OPERATION)
7717 for (EXPR;EXPR;EXPR) { ... }
7718 foreach [VAR] (@ARRAY) { ... }
7719 fork
7720 ... ge ... String greater than or equal.
7721 getc[(FILEHANDLE)]
7722 getgrent
7723 getgrgid(GID)
7724 getgrnam(NAME)
7725 gethostbyaddr(ADDR,ADDRTYPE)
7726 gethostbyname(NAME)
7727 gethostent
7728 getlogin
7729 getnetbyaddr(ADDR,ADDRTYPE)
7730 getnetbyname(NAME)
7731 getnetent
7732 getpeername(SOCKET)
7733 getpgrp(PID)
7734 getppid
7735 getpriority(WHICH,WHO)
7736 getprotobyname(NAME)
7737 getprotobynumber(NUMBER)
7738 getprotoent
7739 getpwent
7740 getpwnam(NAME)
7741 getpwuid(UID)
7742 getservbyname(NAME,PROTO)
7743 getservbyport(PORT,PROTO)
7744 getservent
7745 getsockname(SOCKET)
7746 getsockopt(SOCKET,LEVEL,OPTNAME)
7747 gmtime(EXPR)
7748 goto LABEL
7749 ... gt ... String greater than.
7750 hex(EXPR)
7751 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7752 index(STR,SUBSTR[,OFFSET])
7753 int(EXPR)
7754 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7755 join(EXPR,LIST)
7756 keys(%HASH)
7757 kill(LIST)
7758 last [LABEL]
7759 ... le ... String less than or equal.
7760 length(EXPR)
7761 link(OLDFILE,NEWFILE)
7762 listen(SOCKET,QUEUESIZE)
7763 local(LIST)
7764 localtime(EXPR)
7765 log(EXPR)
7766 lstat(EXPR|FILEHANDLE|VAR)
7767 ... lt ... String less than.
7768 m/PATTERN/iogsmx
7769 mkdir(FILENAME,MODE)
7770 msgctl(ID,CMD,ARG)
7771 msgget(KEY,FLAGS)
7772 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7773 msgsnd(ID,MSG,FLAGS)
7774 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7775 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7776 ... ne ... String inequality.
7777 next [LABEL]
7778 oct(EXPR)
7779 open(FILEHANDLE[,EXPR])
7780 opendir(DIRHANDLE,EXPR)
7781 ord(EXPR) ASCII value of the first char of the string.
7782 pack(TEMPLATE,LIST)
7783 package NAME Introduces package context.
7784 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7785 pop(ARRAY)
7786 print [FILEHANDLE] [(LIST)]
7787 printf [FILEHANDLE] (FORMAT,LIST)
7788 push(ARRAY,LIST)
7789 q/STRING/ Synonym for 'STRING'
7790 qq/STRING/ Synonym for \"STRING\"
7791 qx/STRING/ Synonym for `STRING`
7792 rand[(EXPR)]
7793 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7794 readdir(DIRHANDLE)
7795 readlink(EXPR)
7796 recv(SOCKET,SCALAR,LEN,FLAGS)
7797 redo [LABEL]
7798 rename(OLDNAME,NEWNAME)
7799 require [FILENAME | PERL_VERSION]
7800 reset[(EXPR)]
7801 return(LIST)
7802 reverse(LIST)
7803 rewinddir(DIRHANDLE)
7804 rindex(STR,SUBSTR[,OFFSET])
7805 rmdir(FILENAME)
7806 s/PATTERN/REPLACEMENT/gieoxsm
7807 scalar(EXPR)
7808 seek(FILEHANDLE,POSITION,WHENCE)
7809 seekdir(DIRHANDLE,POS)
7810 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7811 semctl(ID,SEMNUM,CMD,ARG)
7812 semget(KEY,NSEMS,SIZE,FLAGS)
7813 semop(KEY,...)
7814 send(SOCKET,MSG,FLAGS[,TO])
7815 setgrent
7816 sethostent(STAYOPEN)
7817 setnetent(STAYOPEN)
7818 setpgrp(PID,PGRP)
7819 setpriority(WHICH,WHO,PRIORITY)
7820 setprotoent(STAYOPEN)
7821 setpwent
7822 setservent(STAYOPEN)
7823 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7824 shift[(ARRAY)]
7825 shmctl(ID,CMD,ARG)
7826 shmget(KEY,SIZE,FLAGS)
7827 shmread(ID,VAR,POS,SIZE)
7828 shmwrite(ID,STRING,POS,SIZE)
7829 shutdown(SOCKET,HOW)
7830 sin(EXPR)
7831 sleep[(EXPR)]
7832 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7833 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7834 sort [SUBROUTINE] (LIST)
7835 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7836 split[(/PATTERN/[,EXPR[,LIMIT]])]
7837 sprintf(FORMAT,LIST)
7838 sqrt(EXPR)
7839 srand(EXPR)
7840 stat(EXPR|FILEHANDLE|VAR)
7841 study[(SCALAR)]
7842 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7843 substr(EXPR,OFFSET[,LEN])
7844 symlink(OLDFILE,NEWFILE)
7845 syscall(LIST)
7846 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7847 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7848 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7849 tell[(FILEHANDLE)]
7850 telldir(DIRHANDLE)
7851 time
7852 times
7853 tr/SEARCHLIST/REPLACEMENTLIST/cds
7854 truncate(FILE|EXPR,LENGTH)
7855 umask[(EXPR)]
7856 undef[(EXPR)]
7857 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7858 unlink(LIST)
7859 unpack(TEMPLATE,EXPR)
7860 unshift(ARRAY,LIST)
7861 until (EXPR) { ... } EXPR until EXPR
7862 utime(LIST)
7863 values(%HASH)
7864 vec(EXPR,OFFSET,BITS)
7865 wait
7866 waitpid(PID,FLAGS)
7867 wantarray Returns true if the sub/eval is called in list context.
7868 warn(LIST)
7869 while (EXPR) { ... } EXPR while EXPR
7870 write[(EXPR|FILEHANDLE)]
7871 ... x ... Repeat string or array.
7872 x= ... Repetition assignment.
7873 y/SEARCHLIST/REPLACEMENTLIST/
7874 ... | ... Bitwise or.
7875 ... || ... Logical or.
7876 ~ ... Unary bitwise complement.
7877 #! OS interpreter indicator. If contains `perl', used for options, and -x.
7878 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7879 CORE:: Prefix to access builtin function if imported sub obscures it.
7880 SUPER:: Prefix to lookup for a method in @ISA classes.
7881 DESTROY Shorthand for `sub DESTROY {...}'.
7882 ... EQ ... Obsolete synonym of `eq'.
7883 ... GE ... Obsolete synonym of `ge'.
7884 ... GT ... Obsolete synonym of `gt'.
7885 ... LE ... Obsolete synonym of `le'.
7886 ... LT ... Obsolete synonym of `lt'.
7887 ... NE ... Obsolete synonym of `ne'.
7888 abs [ EXPR ] absolute value
7889 ... and ... Low-precedence synonym for &&.
7890 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7891 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
7892 chr Converts a number to char with the same ordinal.
7893 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7894 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7895 exists $HASH{KEY} True if the key exists.
7896 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7897 formline PICTURE, LIST Backdoor into \"format\" processing.
7898 glob EXPR Synonym of <EXPR>.
7899 lc [ EXPR ] Returns lowercased EXPR.
7900 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
7901 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
7902 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
7903 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
7904 not ... Low-precedence synonym for ! - negation.
7905 ... or ... Low-precedence synonym for ||.
7906 pos STRING Set/Get end-position of the last match over this string, see \\G.
7907 quotemeta [ EXPR ] Quote regexp metacharacters.
7908 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
7909 readline FH Synonym of <FH>.
7910 readpipe CMD Synonym of `CMD`.
7911 ref [ EXPR ] Type of EXPR when dereferenced.
7912 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
7913 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
7914 tied Returns internal object for a tied data.
7915 uc [ EXPR ] Returns upcased EXPR.
7916 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
7917 untie VAR Unlink an object from a simple Perl variable.
7918 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
7919 ... xor ... Low-precedence synonym for exclusive or.
7920 prototype \\&SUB Returns prototype of the function given a reference.
7921 =head1 Top-level heading.
7922 =head2 Second-level heading.
7923 =head3 Third-level heading (is there such?).
7924 =over [ NUMBER ] Start list.
7925 =item [ TITLE ] Start new item in the list.
7926 =back End list.
7927 =cut Switch from POD to Perl.
7928 =pod Switch from Perl to POD.
7929 ")
7930
7931 (defun cperl-switch-to-doc-buffer (&optional interactive)
7932 "Go to the perl documentation buffer and insert the documentation."
7933 (interactive "p")
7934 (let ((buf (get-buffer-create cperl-doc-buffer)))
7935 (if interactive
7936 (switch-to-buffer-other-window buf)
7937 (set-buffer buf))
7938 (if (= (buffer-size) 0)
7939 (progn
7940 (insert (documentation-property 'cperl-short-docs
7941 'variable-documentation))
7942 (setq buffer-read-only t)))))
7943
7944 (defun cperl-beautify-regexp-piece (b e embed level)
7945 ;; b is before the starting delimiter, e before the ending
7946 ;; e should be a marker, may be changed, but remains "correct".
7947 ;; EMBED is nil if we process the whole REx.
7948 ;; The REx is guaranteed to have //x
7949 ;; LEVEL shows how many levels deep to go
7950 ;; position at enter and at leave is not defined
7951 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
7952 (if (not embed)
7953 (goto-char (1+ b))
7954 (goto-char b)
7955 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
7956 (forward-char 2)
7957 (delete-char 1)
7958 (forward-char 1))
7959 ((looking-at "(\\?[^a-zA-Z]")
7960 (forward-char 3))
7961 ((looking-at "(\\?") ; (?i)
7962 (forward-char 2))
7963 (t
7964 (forward-char 1))))
7965 (setq c (if embed (current-indentation) (1- (current-column)))
7966 c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
7967 (or (looking-at "[ \t]*[\n#]")
7968 (progn
7969 (insert "\n")))
7970 (goto-char e)
7971 (beginning-of-line)
7972 (if (re-search-forward "[^ \t]" e t)
7973 (progn ; Something before the ending delimiter
7974 (goto-char e)
7975 (delete-horizontal-space)
7976 (insert "\n")
7977 (cperl-make-indent c)
7978 (set-marker e (point))))
7979 (goto-char b)
7980 (end-of-line 2)
7981 (while (< (point) (marker-position e))
7982 (beginning-of-line)
7983 (setq s (point)
7984 inline t)
7985 (skip-chars-forward " \t")
7986 (delete-region s (point))
7987 (cperl-make-indent c1)
7988 (while (and
7989 inline
7990 (looking-at
7991 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
7992 "\\|" ; Embedded variable
7993 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
7994 "\\|" ; $ ^
7995 "[$^]"
7996 "\\|" ; simple-code simple-code*?
7997 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
7998 "\\|" ; Class
7999 "\\(\\[\\)" ; 6
8000 "\\|" ; Grouping
8001 "\\((\\(\\?\\)?\\)" ; 7 8
8002 "\\|" ; |
8003 "\\(|\\)"))) ; 9
8004 (goto-char (match-end 0))
8005 (setq spaces t)
8006 (cond ((match-beginning 1) ; Alphanum word + junk
8007 (forward-char -1))
8008 ((or (match-beginning 3) ; $ab[12]
8009 (and (match-beginning 5) ; X* X+ X{2,3}
8010 (eq (preceding-char) ?\{)))
8011 (forward-char -1)
8012 (forward-sexp 1))
8013 ((and ; [], already syntaxified
8014 (match-beginning 6)
8015 cperl-regexp-scan
8016 cperl-use-syntax-table-text-property)
8017 (forward-char -1)
8018 (forward-sexp 1)
8019 (or (eq (preceding-char) ?\])
8020 (error "[]-group not terminated"))
8021 (re-search-forward
8022 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8023 ((match-beginning 6) ; []
8024 (setq tmp (point))
8025 (if (looking-at "\\^?\\]")
8026 (goto-char (match-end 0)))
8027 ;; XXXX POSIX classes?!
8028 (while (and (not pos)
8029 (re-search-forward "\\[:\\|\\]" e t))
8030 (if (eq (preceding-char) ?:)
8031 (or (re-search-forward ":\\]" e t)
8032 (error "[:POSIX:]-group in []-group not terminated"))
8033 (setq pos t)))
8034 (or (eq (preceding-char) ?\])
8035 (error "[]-group not terminated"))
8036 (re-search-forward
8037 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8038 ((match-beginning 7) ; ()
8039 (goto-char (match-beginning 0))
8040 (setq pos (current-column))
8041 (or (eq pos c1)
8042 (progn
8043 (delete-horizontal-space)
8044 (insert "\n")
8045 (cperl-make-indent c1)))
8046 (setq tmp (point))
8047 (forward-sexp 1)
8048 ;; (or (forward-sexp 1)
8049 ;; (progn
8050 ;; (goto-char tmp)
8051 ;; (error "()-group not terminated")))
8052 (set-marker m (1- (point)))
8053 (set-marker m1 (point))
8054 (if (= level 1)
8055 (if (progn ; indent rigidly if multiline
8056 ;; In fact does not make a lot of sense, since
8057 ;; the starting position can be already lost due
8058 ;; to insertion of "\n" and " "
8059 (goto-char tmp)
8060 (search-forward "\n" m1 t))
8061 (indent-rigidly (point) m1 (- c1 pos)))
8062 (setq level (1- level))
8063 (cond
8064 ((not (match-beginning 8))
8065 (cperl-beautify-regexp-piece tmp m t level))
8066 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8067 t)
8068 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8069 (goto-char (+ 2 tmp))
8070 (forward-sexp 1)
8071 (cperl-beautify-regexp-piece (point) m t level))
8072 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8073 (goto-char (+ 3 tmp))
8074 (cperl-beautify-regexp-piece (point) m t level))
8075 (t
8076 (cperl-beautify-regexp-piece tmp m t level))))
8077 (goto-char m1)
8078 (cond ((looking-at "[*+?]\\??")
8079 (goto-char (match-end 0)))
8080 ((eq (following-char) ?\{)
8081 (forward-sexp 1)
8082 (if (eq (following-char) ?\?)
8083 (forward-char))))
8084 (skip-chars-forward " \t")
8085 (setq spaces nil)
8086 (if (looking-at "[#\n]")
8087 (progn
8088 (or (eolp) (indent-for-comment))
8089 (beginning-of-line 2))
8090 (delete-horizontal-space)
8091 (insert "\n"))
8092 (end-of-line)
8093 (setq inline nil))
8094 ((match-beginning 9) ; |
8095 (forward-char -1)
8096 (setq tmp (point))
8097 (beginning-of-line)
8098 (if (re-search-forward "[^ \t]" tmp t)
8099 (progn
8100 (goto-char tmp)
8101 (delete-horizontal-space)
8102 (insert "\n"))
8103 ;; first at line
8104 (delete-region (point) tmp))
8105 (cperl-make-indent c)
8106 (forward-char 1)
8107 (skip-chars-forward " \t")
8108 (setq spaces nil)
8109 (if (looking-at "[#\n]")
8110 (beginning-of-line 2)
8111 (delete-horizontal-space)
8112 (insert "\n"))
8113 (end-of-line)
8114 (setq inline nil)))
8115 (or (looking-at "[ \t\n]")
8116 (not spaces)
8117 (insert " "))
8118 (skip-chars-forward " \t"))
8119 (or (looking-at "[#\n]")
8120 (error "Unknown code `%s' in a regexp"
8121 (buffer-substring (point) (1+ (point)))))
8122 (and inline (end-of-line 2)))
8123 ;; Special-case the last line of group
8124 (if (and (>= (point) (marker-position e))
8125 (/= (current-indentation) c))
8126 (progn
8127 (beginning-of-line)
8128 (cperl-make-indent c)))))
8129
8130 (defun cperl-make-regexp-x ()
8131 ;; Returns position of the start
8132 ;; XXX this is called too often! Need to cache the result!
8133 (save-excursion
8134 (or cperl-use-syntax-table-text-property
8135 (error "I need to have a regexp marked!"))
8136 ;; Find the start
8137 (if (looking-at "\\s|")
8138 nil ; good already
8139 (if (looking-at "\\([smy]\\|qr\\)\\s|")
8140 (forward-char 1)
8141 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8142 ;;(forward-char 1)
8143 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8144 (sub-p (eq (preceding-char) ?s)) s)
8145 (forward-sexp 1)
8146 (set-marker e (1- (point)))
8147 (setq delim (preceding-char))
8148 (if (and sub-p (eq delim (char-after (- (point) 2))))
8149 (error "Possible s/blah// - do not know how to deal with"))
8150 (if sub-p (forward-sexp 1))
8151 (if (looking-at "\\sw*x")
8152 (setq have-x t)
8153 (insert "x"))
8154 ;; Protect fragile " ", "#"
8155 (if have-x nil
8156 (goto-char (1+ b))
8157 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8158 (forward-char -1)
8159 (insert "\\")
8160 (forward-char 1)))
8161 b)))
8162
8163 (defun cperl-beautify-regexp (&optional deep)
8164 "Do it. (Experimental, may change semantics, recheck the result.)
8165 We suppose that the regexp is scanned already."
8166 (interactive "P")
8167 (setq deep (if deep (prefix-numeric-value deep) -1))
8168 (save-excursion
8169 (goto-char (cperl-make-regexp-x))
8170 (let ((b (point)) (e (make-marker)))
8171 (forward-sexp 1)
8172 (set-marker e (1- (point)))
8173 (cperl-beautify-regexp-piece b e nil deep))))
8174
8175 (defun cperl-regext-to-level-start ()
8176 "Goto start of an enclosing group in regexp.
8177 We suppose that the regexp is scanned already."
8178 (interactive)
8179 (let ((limit (cperl-make-regexp-x)) done)
8180 (while (not done)
8181 (or (eq (following-char) ?\()
8182 (search-backward "(" (1+ limit) t)
8183 (error "Cannot find `(' which starts a group"))
8184 (setq done
8185 (save-excursion
8186 (skip-chars-backward "\\")
8187 (looking-at "\\(\\\\\\\\\\)*(")))
8188 (or done (forward-char -1)))))
8189
8190 (defun cperl-contract-level ()
8191 "Find an enclosing group in regexp and contract it.
8192 \(Experimental, may change semantics, recheck the result.)
8193 We suppose that the regexp is scanned already."
8194 (interactive)
8195 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8196 (cperl-regext-to-level-start)
8197 (let ((b (point)) (e (make-marker)) c)
8198 (forward-sexp 1)
8199 (set-marker e (1- (point)))
8200 (goto-char b)
8201 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8202 (cond
8203 ((match-beginning 1) ; #-comment
8204 (or c (setq c (current-indentation)))
8205 (beginning-of-line 2) ; Skip
8206 (cperl-make-indent c))
8207 (t
8208 (delete-char -1)
8209 (just-one-space))))))
8210
8211 (defun cperl-contract-levels ()
8212 "Find an enclosing group in regexp and contract all the kids.
8213 \(Experimental, may change semantics, recheck the result.)
8214 We suppose that the regexp is scanned already."
8215 (interactive)
8216 (save-excursion
8217 (condition-case nil
8218 (cperl-regext-to-level-start)
8219 (error ; We are outside outermost group
8220 (goto-char (cperl-make-regexp-x))))
8221 (let ((b (point)) (e (make-marker)) s c)
8222 (forward-sexp 1)
8223 (set-marker e (1- (point)))
8224 (goto-char (1+ b))
8225 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8226 (cond
8227 ((match-beginning 1) ; Skip
8228 nil)
8229 (t ; Group
8230 (cperl-contract-level)))))))
8231
8232 (defun cperl-beautify-level (&optional deep)
8233 "Find an enclosing group in regexp and beautify it.
8234 \(Experimental, may change semantics, recheck the result.)
8235 We suppose that the regexp is scanned already."
8236 (interactive "P")
8237 (setq deep (if deep (prefix-numeric-value deep) -1))
8238 (save-excursion
8239 (cperl-regext-to-level-start)
8240 (let ((b (point)) (e (make-marker)))
8241 (forward-sexp 1)
8242 (set-marker e (1- (point)))
8243 (cperl-beautify-regexp-piece b e nil deep))))
8244
8245 (defun cperl-invert-if-unless-modifiers ()
8246 "Change `B if A;' into `if (A) {B}' etc if possible.
8247 \(Unfinished.)"
8248 (interactive) ;
8249 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8250 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8251 (and (= (char-syntax (preceding-char)) ?w)
8252 (forward-sexp -1))
8253 (setq pre-if (point))
8254 (cperl-backward-to-start-of-expr)
8255 (setq pre-B (point))
8256 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8257 (cperl-forward-to-end-of-expr)
8258 (setq post-A (point))
8259 (goto-char pre-if)
8260 (or (looking-at w-rex)
8261 ;; Find the position
8262 (progn (goto-char post-A)
8263 (while (and
8264 (not (looking-at w-rex))
8265 (> (point) pre-B))
8266 (forward-sexp -1))
8267 (setq pre-if (point))))
8268 (or (looking-at w-rex)
8269 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8270 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8271 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8272 ;; First, simple part: find code boundaries
8273 (forward-sexp 1)
8274 (setq post-if (point))
8275 (forward-sexp -2)
8276 (forward-sexp 1)
8277 (setq post-B (point))
8278 (cperl-backward-to-start-of-expr)
8279 (setq pre-B (point))
8280 (setq B (buffer-substring pre-B post-B))
8281 (goto-char pre-if)
8282 (forward-sexp 2)
8283 (forward-sexp -1)
8284 ;; May be after $, @, $# etc of a variable
8285 (skip-chars-backward "$@%#")
8286 (setq pre-A (point))
8287 (cperl-forward-to-end-of-expr)
8288 (setq post-A (point))
8289 (setq A (buffer-substring pre-A post-A))
8290 ;; Now modify (from end, to not break the stuff)
8291 (skip-chars-forward " \t;")
8292 (delete-region pre-A (point)) ; we move to pre-A
8293 (insert "\n" B ";\n}")
8294 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8295 (delete-region pre-if post-if)
8296 (delete-region pre-B post-B)
8297 (goto-char pre-B)
8298 (insert if-string " (" A ") {")
8299 (setq post-B (point))
8300 (if (looking-at "[ \t]+$")
8301 (delete-horizontal-space)
8302 (if (looking-at "[ \t]*#")
8303 (cperl-indent-for-comment)
8304 (just-one-space)))
8305 (forward-line 1)
8306 (if (looking-at "[ \t]*$")
8307 (progn ; delete line
8308 (delete-horizontal-space)
8309 (delete-region (point) (1+ (point)))))
8310 (cperl-indent-line)
8311 (goto-char (1- post-B))
8312 (forward-sexp 1)
8313 (cperl-indent-line)
8314 (goto-char pre-B)))
8315
8316 (defun cperl-invert-if-unless ()
8317 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8318 If the cursor is not on the leading keyword of the BLOCK flavor of
8319 construct, will assume it is the STATEMENT flavor, so will try to find
8320 the appropriate statement modifier."
8321 (interactive)
8322 (and (= (char-syntax (preceding-char)) ?w)
8323 (forward-sexp -1))
8324 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8325 (let ((pre-if (point))
8326 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8327 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8328 (forward-sexp 2)
8329 (setq post-A (point))
8330 (forward-sexp -1)
8331 (setq pre-A (point))
8332 (setq is-block (and (eq (following-char) ?\( )
8333 (save-excursion
8334 (condition-case nil
8335 (progn
8336 (forward-sexp 2)
8337 (forward-sexp -1)
8338 (eq (following-char) ?\{ ))
8339 (error nil)))))
8340 (if is-block
8341 (progn
8342 (goto-char post-A)
8343 (forward-sexp 1)
8344 (setq post-B (point))
8345 (forward-sexp -1)
8346 (setq pre-B (point))
8347 (if (and (eq (following-char) ?\{ )
8348 (progn
8349 (cperl-backward-to-noncomment post-A)
8350 (eq (preceding-char) ?\) )))
8351 (if (condition-case nil
8352 (progn
8353 (goto-char post-B)
8354 (forward-sexp 1)
8355 (forward-sexp -1)
8356 (looking-at "\\<els\\(e\\|if\\)\\>"))
8357 (error nil))
8358 (error
8359 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8360 (goto-char (1- post-B))
8361 (cperl-backward-to-noncomment pre-B)
8362 (if (eq (preceding-char) ?\;)
8363 (forward-char -1))
8364 (setq end-B-code (point))
8365 (goto-char pre-B)
8366 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8367 (setq p (match-beginning 0)
8368 A (buffer-substring p (match-end 0))
8369 state (parse-partial-sexp pre-B p))
8370 (or (nth 3 state)
8371 (nth 4 state)
8372 (nth 5 state)
8373 (error "`%s' inside `%s' BLOCK" A if-string))
8374 (goto-char (match-end 0)))
8375 ;; Finally got it
8376 (goto-char (1+ pre-B))
8377 (skip-chars-forward " \t\n")
8378 (setq B (buffer-substring (point) end-B-code))
8379 (goto-char end-B-code)
8380 (or (looking-at ";?[ \t\n]*}")
8381 (progn
8382 (skip-chars-forward "; \t\n")
8383 (setq B-comment
8384 (buffer-substring (point) (1- post-B)))))
8385 (and (equal B "")
8386 (setq B "1"))
8387 (goto-char (1- post-A))
8388 (cperl-backward-to-noncomment pre-A)
8389 (or (looking-at "[ \t\n]*)")
8390 (goto-char (1- post-A)))
8391 (setq p (point))
8392 (goto-char (1+ pre-A))
8393 (skip-chars-forward " \t\n")
8394 (setq A (buffer-substring (point) p))
8395 (delete-region pre-B post-B)
8396 (delete-region pre-A post-A)
8397 (goto-char pre-if)
8398 (insert B " ")
8399 (and B-comment (insert B-comment " "))
8400 (just-one-space)
8401 (forward-word 1)
8402 (setq pre-A (point))
8403 (insert " " A ";")
8404 (delete-horizontal-space)
8405 (setq post-B (point))
8406 (if (looking-at "#")
8407 (indent-for-comment))
8408 (goto-char post-B)
8409 (forward-char -1)
8410 (delete-horizontal-space)
8411 (goto-char pre-A)
8412 (just-one-space)
8413 (goto-char pre-if)
8414 (setq pre-A (set-marker (make-marker) pre-A))
8415 (while (<= (point) (marker-position pre-A))
8416 (cperl-indent-line)
8417 (forward-line 1))
8418 (goto-char (marker-position pre-A))
8419 (if B-comment
8420 (progn
8421 (forward-line -1)
8422 (indent-for-comment)
8423 (goto-char (marker-position pre-A)))))
8424 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8425 ;; (error "`%s' not with an (EXPR)" if-string)
8426 (forward-sexp -1)
8427 (cperl-invert-if-unless-modifiers)))
8428 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8429 (cperl-invert-if-unless-modifiers)))
8430
8431 ;;; By Anthony Foiani <afoiani@uswest.com>
8432 ;;; Getting help on modules in C-h f ?
8433 ;;; This is a modified version of `man'.
8434 ;;; Need to teach it how to lookup functions
8435 ;;;###autoload
8436 (defun cperl-perldoc (word)
8437 "Run `perldoc' on WORD."
8438 (interactive
8439 (list (let* ((default-entry (cperl-word-at-point))
8440 (input (read-string
8441 (format "perldoc entry%s: "
8442 (if (string= default-entry "")
8443 ""
8444 (format " (default %s)" default-entry))))))
8445 (if (string= input "")
8446 (if (string= default-entry "")
8447 (error "No perldoc args given")
8448 default-entry)
8449 input))))
8450 (require 'man)
8451 (let* ((case-fold-search nil)
8452 (is-func (and
8453 (string-match "^[a-z]+$" word)
8454 (string-match (concat "^" word "\\>")
8455 (documentation-property
8456 'cperl-short-docs
8457 'variable-documentation))))
8458 (manual-program (if is-func "perldoc -f" "perldoc")))
8459 (cond
8460 (cperl-xemacs-p
8461 (let ((Manual-program "perldoc")
8462 (Manual-switches (if is-func (list "-f"))))
8463 (manual-entry word)))
8464 (t
8465 (Man-getpage-in-background word)))))
8466
8467 ;;;###autoload
8468 (defun cperl-perldoc-at-point ()
8469 "Run a `perldoc' on the word around point."
8470 (interactive)
8471 (cperl-perldoc (cperl-word-at-point)))
8472
8473 (defcustom pod2man-program "pod2man"
8474 "*File name for `pod2man'."
8475 :type 'file
8476 :group 'cperl)
8477
8478 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8479 (defun cperl-pod-to-manpage ()
8480 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8481 (interactive)
8482 (require 'man)
8483 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8484 (bufname (concat "Man " buffer-file-name))
8485 (buffer (generate-new-buffer bufname)))
8486 (save-excursion
8487 (set-buffer buffer)
8488 (let ((process-environment (copy-sequence process-environment)))
8489 ;; Prevent any attempt to use display terminal fanciness.
8490 (setenv "TERM" "dumb")
8491 (set-process-sentinel
8492 (start-process pod2man-program buffer "sh" "-c"
8493 (format (cperl-pod2man-build-command) pod2man-args))
8494 'Man-bgproc-sentinel)))))
8495
8496 ;;; Updated version by him too
8497 (defun cperl-build-manpage ()
8498 "Create a virtual manpage in Emacs from the POD in the file."
8499 (interactive)
8500 (require 'man)
8501 (cond
8502 (cperl-xemacs-p
8503 (let ((Manual-program "perldoc"))
8504 (manual-entry buffer-file-name)))
8505 (t
8506 (let* ((manual-program "perldoc"))
8507 (Man-getpage-in-background buffer-file-name)))))
8508
8509 (defun cperl-pod2man-build-command ()
8510 "Builds the entire background manpage and cleaning command."
8511 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8512 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8513 (while (and flist (car flist))
8514 (let ((pcom (car (car flist)))
8515 (pargs (cdr (car flist))))
8516 (setq command
8517 (concat command " | " pcom " "
8518 (mapconcat '(lambda (phrase)
8519 (if (not (stringp phrase))
8520 (error "Malformed Man-filter-list"))
8521 phrase)
8522 pargs " ")))
8523 (setq flist (cdr flist))))
8524 command))
8525
8526
8527 (defun cperl-next-interpolated-REx-1 ()
8528 "Move point to next REx which has interpolated parts without //o.
8529 Skips RExes consisting of one interpolated variable.
8530
8531 Note that skipped RExen are not performance hits."
8532 (interactive "")
8533 (cperl-next-interpolated-REx 1))
8534
8535 (defun cperl-next-interpolated-REx-0 ()
8536 "Move point to next REx which has interpolated parts without //o."
8537 (interactive "")
8538 (cperl-next-interpolated-REx 0))
8539
8540 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8541 "Move point to next REx which has interpolated parts.
8542 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8543 point and the limit of search (default to point and end of buffer).
8544
8545 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8546 semantic may be used as a numeric argument.
8547
8548 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8549 a result of qr//, this is not a performance hit), t for the rest."
8550 (interactive "P")
8551 (if (numberp skip) (setq skip (list 0 skip)))
8552 (or beg (setq beg (point)))
8553 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8554 (let (pp)
8555 (and (eq (get-text-property beg 'syntax-type) 'string)
8556 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8557 (cperl-map-pods-heres
8558 (function (lambda (s e p)
8559 (if (memq (get-text-property s 'REx-interpolated) skip)
8560 t
8561 (setq pp s)
8562 nil))) ; nil stops
8563 'REx-interpolated beg limit)
8564 (if pp (goto-char pp)
8565 (message "No more interpolated REx"))))
8566
8567 ;;; Initial version contributed by Trey Belew
8568 (defun cperl-here-doc-spell (&optional beg end)
8569 "Spell-check HERE-documents in the Perl buffer.
8570 If a region is highlighted, restricts to the region."
8571 (interactive "")
8572 (cperl-pod-spell t beg end))
8573
8574 (defun cperl-pod-spell (&optional do-heres beg end)
8575 "Spell-check POD documentation.
8576 If invoked with prefix argument, will do HERE-DOCs instead.
8577 If a region is highlighted, restricts to the region."
8578 (interactive "P")
8579 (save-excursion
8580 (let (beg end)
8581 (if (cperl-mark-active)
8582 (setq beg (min (mark) (point))
8583 end (max (mark) (point)))
8584 (setq beg (point-min)
8585 end (point-max)))
8586 (cperl-map-pods-heres (function
8587 (lambda (s e p)
8588 (if do-heres
8589 (setq e (save-excursion
8590 (goto-char e)
8591 (forward-line -1)
8592 (point))))
8593 (ispell-region s e)
8594 t))
8595 (if do-heres 'here-doc-group 'in-pod)
8596 beg end))))
8597
8598 (defun cperl-map-pods-heres (func &optional prop s end)
8599 "Executes a function over regions of pods or here-documents.
8600 PROP is the text-property to search for; default to `in-pod'. Stop when
8601 function returns nil."
8602 (let (pos posend has-prop (cont t))
8603 (or prop (setq prop 'in-pod))
8604 (or s (setq s (point-min)))
8605 (or end (setq end (point-max)))
8606 (cperl-update-syntaxification end end)
8607 (save-excursion
8608 (goto-char (setq pos s))
8609 (while (and cont (< pos end))
8610 (setq has-prop (get-text-property pos prop))
8611 (setq posend (next-single-property-change pos prop nil end))
8612 (and has-prop
8613 (setq cont (funcall func pos posend prop)))
8614 (setq pos posend)))))
8615
8616 ;;; Based on code by Masatake YAMATO:
8617 (defun cperl-get-here-doc-region (&optional pos pod)
8618 "Return HERE document region around the point.
8619 Return nil if the point is not in a HERE document region. If POD is non-nil,
8620 will return a POD section if point is in a POD section."
8621 (or pos (setq pos (point)))
8622 (cperl-update-syntaxification pos pos)
8623 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8624 (and pod
8625 (eq 'pod (get-text-property pos 'syntax-type))))
8626 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8627 (e (next-single-property-change pos 'syntax-type)))
8628 (cons b (or e (point-max))))))
8629
8630 (defun cperl-narrow-to-here-doc (&optional pos)
8631 "Narrows editing region to the HERE-DOC at POS.
8632 POS defaults to the point."
8633 (interactive "d")
8634 (or pos (setq pos (point)))
8635 (let ((p (cperl-get-here-doc-region pos)))
8636 (or p (error "Not inside a HERE document"))
8637 (narrow-to-region (car p) (cdr p))
8638 (message
8639 "When you are finished with narrow editing, type C-x n w")))
8640
8641 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8642 "Select the HERE-DOC (or POD section) at POS.
8643 POS defaults to the point."
8644 (interactive "d")
8645 (let ((p (cperl-get-here-doc-region pos t)))
8646 (if p
8647 (progn
8648 (goto-char (car p))
8649 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8650 (message "I do not think POS is in POD or a HERE-doc..."))))
8651
8652 (defun cperl-facemenu-add-face-function (face end)
8653 "A callback to process user-initiated font-change requests.
8654 Translates `bold', `italic', and `bold-italic' requests to insertion of
8655 corresponding POD directives, and `underline' to C<> POD directive.
8656
8657 Such requests are usually bound to M-o LETTER."
8658 (or (get-text-property (point) 'in-pod)
8659 (error "Faces can only be set within POD"))
8660 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8661 (cdr (or (assq face '((bold . "B<")
8662 (italic . "I<")
8663 (bold-italic . "B<I<")
8664 (underline . "C<")))
8665 (error "Face %s not configured for cperl-mode"
8666 face))))
8667 \f
8668 (defun cperl-time-fontification (&optional l step lim)
8669 "Times how long it takes to do incremental fontification in a region.
8670 L is the line to start at, STEP is the number of lines to skip when
8671 doing next incremental fontification, LIM is the maximal number of
8672 incremental fontification to perform. Messages are accumulated in
8673 *Messages* buffer.
8674
8675 May be used for pinpointing which construct slows down buffer fontification:
8676 start with default arguments, then refine the slowdown regions."
8677 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8678 (or l (setq l 1))
8679 (or step (setq step 500))
8680 (or lim (setq lim 40))
8681 (let* ((timems (function (lambda ()
8682 (let ((tt (current-time)))
8683 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8684 (tt (funcall timems)) (c 0) delta tot)
8685 (goto-line l)
8686 (cperl-mode)
8687 (setq tot (- (- tt (setq tt (funcall timems)))))
8688 (message "cperl-mode at %s: %s" l tot)
8689 (while (and (< c lim) (not (eobp)))
8690 (forward-line step)
8691 (setq l (+ l step))
8692 (setq c (1+ c))
8693 (cperl-update-syntaxification (point) (point))
8694 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8695 (message "to %s:%6s,%7s" l delta tot))
8696 tot))
8697
8698 (defun cperl-emulate-lazy-lock (&optional window-size)
8699 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8700 Start fontifying the buffer from the start (or end) using the given
8701 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8702 goes backwards; default is -50. This function is not CPerl-specific; it
8703 may be used to debug problems with delayed incremental fontification."
8704 (interactive
8705 "nSize of window for incremental fontification, negative goes backwards: ")
8706 (or window-size (setq window-size -50))
8707 (let ((pos (if (> window-size 0)
8708 (point-min)
8709 (point-max)))
8710 p)
8711 (goto-char pos)
8712 (normal-mode)
8713 ;; Why needed??? With older font-locks???
8714 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8715 (while (if (> window-size 0)
8716 (< pos (point-max))
8717 (> pos (point-min)))
8718 (setq p (progn
8719 (forward-line window-size)
8720 (point)))
8721 (font-lock-fontify-region (min p pos) (max p pos))
8722 (setq pos p))))
8723
8724 \f
8725 (defun cperl-lazy-install ()) ; Avoid a warning
8726 (defun cperl-lazy-unstall ()) ; Avoid a warning
8727
8728 (if (fboundp 'run-with-idle-timer)
8729 (progn
8730 (defvar cperl-help-shown nil
8731 "Non-nil means that the help was already shown now.")
8732
8733 (defvar cperl-lazy-installed nil
8734 "Non-nil means that the lazy-help handlers are installed now.")
8735
8736 (defun cperl-lazy-install ()
8737 "Switches on Auto-Help on Perl constructs (put in the message area).
8738 Delay of auto-help controlled by `cperl-lazy-help-time'."
8739 (interactive)
8740 (make-local-variable 'cperl-help-shown)
8741 (if (and (cperl-val 'cperl-lazy-help-time)
8742 (not cperl-lazy-installed))
8743 (progn
8744 (add-hook 'post-command-hook 'cperl-lazy-hook)
8745 (run-with-idle-timer
8746 (cperl-val 'cperl-lazy-help-time 1000000 5)
8747 t
8748 'cperl-get-help-defer)
8749 (setq cperl-lazy-installed t))))
8750
8751 (defun cperl-lazy-unstall ()
8752 "Switches off Auto-Help on Perl constructs (put in the message area).
8753 Delay of auto-help controlled by `cperl-lazy-help-time'."
8754 (interactive)
8755 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8756 (cancel-function-timers 'cperl-get-help-defer)
8757 (setq cperl-lazy-installed nil))
8758
8759 (defun cperl-lazy-hook ()
8760 (setq cperl-help-shown nil))
8761
8762 (defun cperl-get-help-defer ()
8763 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8764 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8765 (cperl-get-help)
8766 (setq cperl-help-shown t))))
8767 (cperl-lazy-install)))
8768
8769
8770 ;;; Plug for wrong font-lock:
8771
8772 (defun cperl-font-lock-unfontify-region-function (beg end)
8773 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8774 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8775 before-change-functions after-change-functions
8776 deactivate-mark buffer-file-name buffer-file-truename)
8777 (remove-text-properties beg end '(face nil))
8778 (if (and (not modified) (buffer-modified-p))
8779 (set-buffer-modified-p nil))))
8780
8781 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8782 "Extends the region to safe positions, then calls the default function.
8783 Newer `font-lock's can do it themselves.
8784 We unwind only as far as needed for fontification. Syntaxification may
8785 do extra unwind via `cperl-unwind-to-safe'."
8786 (save-excursion
8787 (goto-char beg)
8788 (while (and beg
8789 (progn
8790 (beginning-of-line)
8791 (eq (get-text-property (setq beg (point)) 'syntax-type)
8792 'multiline)))
8793 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8794 (goto-char beg)))
8795 (setq beg (point))
8796 (goto-char end)
8797 (while (and end
8798 (progn
8799 (or (bolp) (condition-case nil
8800 (forward-line 1)
8801 (error nil)))
8802 (eq (get-text-property (setq end (point)) 'syntax-type)
8803 'multiline)))
8804 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8805 (goto-char end))
8806 (setq end (point)))
8807 (font-lock-default-fontify-region beg end loudly))
8808
8809 (defvar cperl-d-l nil)
8810 (defun cperl-fontify-syntaxically (end)
8811 ;; Some vars for debugging only
8812 ;; (message "Syntaxifying...")
8813 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8814 (istate (car cperl-syntax-state))
8815 start from-start edebug-backtrace-buffer)
8816 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8817 (progn
8818 (require 'edebug)
8819 (let ((f 'edebug-backtrace))
8820 (funcall f)))) ; Avoid compile-time warning
8821 (or cperl-syntax-done-to
8822 (setq cperl-syntax-done-to (point-min)
8823 from-start t))
8824 (setq start (if (and cperl-hook-after-change
8825 (not from-start))
8826 cperl-syntax-done-to ; Fontify without change; ignore start
8827 ;; Need to forget what is after `start'
8828 (min cperl-syntax-done-to (point))))
8829 (goto-char start)
8830 (beginning-of-line)
8831 (setq start (point))
8832 (and cperl-syntaxify-unwind
8833 (setq end (cperl-unwind-to-safe t end)
8834 start (point)))
8835 (and (> end start)
8836 (setq cperl-syntax-done-to start) ; In case what follows fails
8837 (cperl-find-pods-heres start end t nil t))
8838 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8839 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8840 dbg iend start end idone cperl-syntax-done-to
8841 istate (car cperl-syntax-state))) ; For debugging
8842 nil)) ; Do not iterate
8843
8844 (defun cperl-fontify-update (end)
8845 (let ((pos (point-min)) prop posend)
8846 (setq end (point-max))
8847 (while (< pos end)
8848 (setq prop (get-text-property pos 'cperl-postpone)
8849 posend (next-single-property-change pos 'cperl-postpone nil end))
8850 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8851 (setq pos posend)))
8852 nil) ; Do not iterate
8853
8854 (defun cperl-fontify-update-bad (end)
8855 ;; Since fontification happens with different region than syntaxification,
8856 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8857 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8858 (if prop
8859 (setq pos (or (cperl-beginning-of-property
8860 (cperl-1+ pos) 'cperl-postpone)
8861 (point-min))))
8862 (while (< pos end)
8863 (setq posend (next-single-property-change pos 'cperl-postpone))
8864 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8865 (setq pos posend)
8866 (setq prop (get-text-property pos 'cperl-postpone))))
8867 nil) ; Do not iterate
8868
8869 ;; Called when any modification is made to buffer text.
8870 (defun cperl-after-change-function (beg end old-len)
8871 ;; We should have been informed about changes by `font-lock'. Since it
8872 ;; does not inform as which calls are defered, do it ourselves
8873 (if cperl-syntax-done-to
8874 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8875
8876 (defun cperl-update-syntaxification (from to)
8877 (if (and cperl-use-syntax-table-text-property
8878 cperl-syntaxify-by-font-lock
8879 (or (null cperl-syntax-done-to)
8880 (< cperl-syntax-done-to to)))
8881 (progn
8882 (save-excursion
8883 (goto-char from)
8884 (cperl-fontify-syntaxically to)))))
8885
8886 (defvar cperl-version
8887 (let ((v "Revision: 5.23"))
8888 (string-match ":\\s *\\([0-9.]+\\)" v)
8889 (substring v (match-beginning 1) (match-end 1)))
8890 "Version of IZ-supported CPerl package this file is based on.")
8891
8892 (provide 'cperl-mode)
8893
8894 ;;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
8895 ;;; cperl-mode.el ends here