Merge changes from emacs-23
[bpt/emacs.git] / lisp / progmodes / cperl-mode.el
1 ;;; cperl-mode.el --- Perl code editing commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 ;; 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 ;; 2010 Free Software Foundation, Inc.
6
7 ;; Author: Ilya Zakharevich
8 ;; Bob Olson
9 ;; Maintainer: Ilya Zakharevich <ilyaz@cpan.org>
10 ;; Keywords: languages, Perl
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Corrections made by Ilya Zakharevich ilyaz@cpan.org
28
29 ;;; Commentary:
30
31 ;; You can either fine-tune the bells and whistles of this mode or
32 ;; bulk enable them by putting
33
34 ;; (setq cperl-hairy t)
35
36 ;; in your .emacs file. (Emacs rulers do not consider it politically
37 ;; correct to make whistles enabled by default.)
38
39 ;; DO NOT FORGET to read micro-docs (available from `Perl' menu) <<<<<<
40 ;; or as help on variables `cperl-tips', `cperl-problems', <<<<<<
41 ;; `cperl-praise', `cperl-speed'. <<<<<<
42
43 ;; The mode information (on C-h m) provides some customization help.
44 ;; If you use font-lock feature of this mode, it is advisable to use
45 ;; either lazy-lock-mode or fast-lock-mode. I prefer lazy-lock.
46
47 ;; Faces used now: three faces for first-class and second-class keywords
48 ;; and control flow words, one for each: comments, string, labels,
49 ;; functions definitions and packages, arrays, hashes, and variable
50 ;; definitions. If you do not see all these faces, your font-lock does
51 ;; not define them, so you need to define them manually.
52
53 ;; This mode supports font-lock, imenu and mode-compile. In the
54 ;; hairy version font-lock is on, but you should activate imenu
55 ;; yourself (note that mode-compile is not standard yet). Well, you
56 ;; can use imenu from keyboard anyway (M-x imenu), but it is better
57 ;; to bind it like that:
58
59 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
60
61 ;;; Font lock bugs as of v4.32:
62
63 ;; The following kinds of Perl code erroneously start strings:
64 ;; \$` \$' \$"
65 ;; $opt::s $opt_s $opt{s} (s => ...) /\s+.../
66 ;; likewise with m, tr, y, q, qX instead of s
67
68 ;;; Code:
69 \f
70 (defvar vc-rcs-header)
71 (defvar vc-sccs-header)
72
73 (eval-when-compile
74 (condition-case nil
75 (require 'custom)
76 (error nil))
77 (condition-case nil
78 (require 'man)
79 (error nil))
80 (defvar cperl-can-font-lock
81 (or (featurep 'xemacs)
82 (and (boundp 'emacs-major-version)
83 (or window-system
84 (> emacs-major-version 20)))))
85 (if cperl-can-font-lock
86 (require 'font-lock))
87 (defvar msb-menu-cond)
88 (defvar gud-perldb-history)
89 (defvar font-lock-background-mode) ; not in Emacs
90 (defvar font-lock-display-type) ; ditto
91 (defvar paren-backwards-message) ; Not in newer XEmacs?
92 (or (fboundp 'defgroup)
93 (defmacro defgroup (name val doc &rest arr)
94 nil))
95 (or (fboundp 'custom-declare-variable)
96 (defmacro defcustom (name val doc &rest arr)
97 `(defvar ,name ,val ,doc)))
98 (or (and (fboundp 'custom-declare-variable)
99 (string< "19.31" emacs-version)) ; Checked with 19.30: defface does not work
100 (defmacro defface (&rest arr)
101 nil))
102 ;; Avoid warning (tmp definitions)
103 (or (fboundp 'x-color-defined-p)
104 (defmacro x-color-defined-p (col)
105 (cond ((fboundp 'color-defined-p) `(color-defined-p ,col))
106 ;; XEmacs >= 19.12
107 ((fboundp 'valid-color-name-p) `(valid-color-name-p ,col))
108 ;; XEmacs 19.11
109 ((fboundp 'x-valid-color-name-p) `(x-valid-color-name-p ,col))
110 (t '(error "Cannot implement color-defined-p")))))
111 (defmacro cperl-is-face (arg) ; Takes quoted arg
112 (cond ((fboundp 'find-face)
113 `(find-face ,arg))
114 (;;(and (fboundp 'face-list)
115 ;; (face-list))
116 (fboundp 'face-list)
117 `(member ,arg (and (fboundp 'face-list)
118 (face-list))))
119 (t
120 `(boundp ,arg))))
121 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
122 (cond ((fboundp 'make-face)
123 `(make-face (quote ,arg)))
124 (t
125 `(defvar ,arg (quote ,arg) ,descr))))
126 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
127 `(progn
128 (or (cperl-is-face (quote ,arg))
129 (cperl-make-face ,arg ,descr))
130 (or (boundp (quote ,arg)) ; We use unquoted variants too
131 (defvar ,arg (quote ,arg) ,descr))))
132 (if (featurep 'xemacs)
133 (defmacro cperl-etags-snarf-tag (file line)
134 `(progn
135 (beginning-of-line 2)
136 (list ,file ,line)))
137 (defmacro cperl-etags-snarf-tag (file line)
138 `(etags-snarf-tag)))
139 (if (featurep 'xemacs)
140 (defmacro cperl-etags-goto-tag-location (elt)
141 ;;(progn
142 ;; (switch-to-buffer (get-file-buffer (elt ,elt 0)))
143 ;; (set-buffer (get-file-buffer (elt ,elt 0)))
144 ;; Probably will not work due to some save-excursion???
145 ;; Or save-file-position?
146 ;; (message "Did I get to line %s?" (elt ,elt 1))
147 `(goto-line (string-to-int (elt ,elt 1))))
148 ;;)
149 (defmacro cperl-etags-goto-tag-location (elt)
150 `(etags-goto-tag-location ,elt))))
151
152 (defvar cperl-can-font-lock
153 (or (featurep 'xemacs)
154 (and (boundp 'emacs-major-version)
155 (or window-system
156 (> emacs-major-version 20)))))
157
158 (defun cperl-choose-color (&rest list)
159 (let (answer)
160 (while list
161 (or answer
162 (if (or (x-color-defined-p (car list))
163 (null (cdr list)))
164 (setq answer (car list))))
165 (setq list (cdr list)))
166 answer))
167
168 (defgroup cperl nil
169 "Major mode for editing Perl code."
170 :prefix "cperl-"
171 :group 'languages
172 :version "20.3")
173
174 (defgroup cperl-indentation-details nil
175 "Indentation."
176 :prefix "cperl-"
177 :group 'cperl)
178
179 (defgroup cperl-affected-by-hairy nil
180 "Variables affected by `cperl-hairy'."
181 :prefix "cperl-"
182 :group 'cperl)
183
184 (defgroup cperl-autoinsert-details nil
185 "Auto-insert tuneup."
186 :prefix "cperl-"
187 :group 'cperl)
188
189 (defgroup cperl-faces nil
190 "Fontification colors."
191 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
192 :prefix "cperl-"
193 :group 'cperl)
194
195 (defgroup cperl-speed nil
196 "Speed vs. validity tuneup."
197 :prefix "cperl-"
198 :group 'cperl)
199
200 (defgroup cperl-help-system nil
201 "Help system tuneup."
202 :prefix "cperl-"
203 :group 'cperl)
204
205 \f
206 (defcustom cperl-extra-newline-before-brace nil
207 "*Non-nil means that if, elsif, while, until, else, for, foreach
208 and do constructs look like:
209
210 if ()
211 {
212 }
213
214 instead of:
215
216 if () {
217 }"
218 :type 'boolean
219 :group 'cperl-autoinsert-details)
220
221 (defcustom cperl-extra-newline-before-brace-multiline
222 cperl-extra-newline-before-brace
223 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
224 for constructs with multiline if/unless/while/until/for/foreach condition."
225 :type 'boolean
226 :group 'cperl-autoinsert-details)
227
228 (defcustom cperl-indent-level 2
229 "*Indentation of CPerl statements with respect to containing block."
230 :type 'integer
231 :group 'cperl-indentation-details)
232
233 ;; Is is not unusual to put both things like perl-indent-level and
234 ;; cperl-indent-level in the local variable section of a file. If only
235 ;; one of perl-mode and cperl-mode is in use, a warning will be issued
236 ;; about the variable. Autoload these here, so that no warning is
237 ;; issued when using either perl-mode or cperl-mode.
238 ;;;###autoload(put 'cperl-indent-level 'safe-local-variable 'integerp)
239 ;;;###autoload(put 'cperl-brace-offset 'safe-local-variable 'integerp)
240 ;;;###autoload(put 'cperl-continued-brace-offset 'safe-local-variable 'integerp)
241 ;;;###autoload(put 'cperl-label-offset 'safe-local-variable 'integerp)
242 ;;;###autoload(put 'cperl-continued-statement-offset 'safe-local-variable 'integerp)
243 ;;;###autoload(put 'cperl-extra-newline-before-brace 'safe-local-variable 'booleanp)
244 ;;;###autoload(put 'cperl-merge-trailing-else 'safe-local-variable 'booleanp)
245
246 (defcustom cperl-lineup-step nil
247 "*`cperl-lineup' will always lineup at multiple of this number.
248 If nil, the value of `cperl-indent-level' will be used."
249 :type '(choice (const nil) integer)
250 :group 'cperl-indentation-details)
251
252 (defcustom cperl-brace-imaginary-offset 0
253 "*Imagined indentation of a Perl open brace that actually follows a statement.
254 An open brace following other text is treated as if it were this far
255 to the right of the start of its line."
256 :type 'integer
257 :group 'cperl-indentation-details)
258
259 (defcustom cperl-brace-offset 0
260 "*Extra indentation for braces, compared with other text in same context."
261 :type 'integer
262 :group 'cperl-indentation-details)
263 (defcustom cperl-label-offset -2
264 "*Offset of CPerl label lines relative to usual indentation."
265 :type 'integer
266 :group 'cperl-indentation-details)
267 (defcustom cperl-min-label-indent 1
268 "*Minimal offset of CPerl label lines."
269 :type 'integer
270 :group 'cperl-indentation-details)
271 (defcustom cperl-continued-statement-offset 2
272 "*Extra indent for lines not starting new statements."
273 :type 'integer
274 :group 'cperl-indentation-details)
275 (defcustom cperl-continued-brace-offset 0
276 "*Extra indent for substatements that start with open-braces.
277 This is in addition to cperl-continued-statement-offset."
278 :type 'integer
279 :group 'cperl-indentation-details)
280 (defcustom cperl-close-paren-offset -1
281 "*Extra indent for substatements that start with close-parenthesis."
282 :type 'integer
283 :group 'cperl-indentation-details)
284
285 (defcustom cperl-indent-wrt-brace t
286 "*Non-nil means indent statements in if/etc block relative brace, not if/etc.
287 Versions 5.2 ... 5.20 behaved as if this were `nil'."
288 :type 'boolean
289 :group 'cperl-indentation-details)
290
291 (defcustom cperl-auto-newline nil
292 "*Non-nil means automatically newline before and after braces,
293 and after colons and semicolons, inserted in CPerl code. The following
294 \\[cperl-electric-backspace] will remove the inserted whitespace.
295 Insertion after colons requires both this variable and
296 `cperl-auto-newline-after-colon' set."
297 :type 'boolean
298 :group 'cperl-autoinsert-details)
299
300 (defcustom cperl-autoindent-on-semi nil
301 "*Non-nil means automatically indent after insertion of (semi)colon.
302 Active if `cperl-auto-newline' is false."
303 :type 'boolean
304 :group 'cperl-autoinsert-details)
305
306 (defcustom cperl-auto-newline-after-colon nil
307 "*Non-nil means automatically newline even after colons.
308 Subject to `cperl-auto-newline' setting."
309 :type 'boolean
310 :group 'cperl-autoinsert-details)
311
312 (defcustom cperl-tab-always-indent t
313 "*Non-nil means TAB in CPerl mode should always reindent the current line,
314 regardless of where in the line point is when the TAB command is used."
315 :type 'boolean
316 :group 'cperl-indentation-details)
317
318 (defcustom cperl-font-lock nil
319 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
320 Can be overwritten by `cperl-hairy' if nil."
321 :type '(choice (const null) boolean)
322 :group 'cperl-affected-by-hairy)
323
324 (defcustom cperl-electric-lbrace-space nil
325 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
326 Can be overwritten by `cperl-hairy' if nil."
327 :type '(choice (const null) boolean)
328 :group 'cperl-affected-by-hairy)
329
330 (defcustom cperl-electric-parens-string "({[]})<"
331 "*String of parentheses that should be electric in CPerl.
332 Closing ones are electric only if the region is highlighted."
333 :type 'string
334 :group 'cperl-affected-by-hairy)
335
336 (defcustom cperl-electric-parens nil
337 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
338 Can be overwritten by `cperl-hairy' if nil."
339 :type '(choice (const null) boolean)
340 :group 'cperl-affected-by-hairy)
341
342 (defvar zmacs-regions) ; Avoid warning
343
344 (defcustom cperl-electric-parens-mark
345 (and window-system
346 (or (and (boundp 'transient-mark-mode) ; For Emacs
347 transient-mark-mode)
348 (and (boundp 'zmacs-regions) ; For XEmacs
349 zmacs-regions)))
350 "*Not-nil means that electric parens look for active mark.
351 Default is yes if there is visual feedback on mark."
352 :type 'boolean
353 :group 'cperl-autoinsert-details)
354
355 (defcustom cperl-electric-linefeed nil
356 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
357 In any case these two mean plain and hairy linefeeds together.
358 Can be overwritten by `cperl-hairy' if nil."
359 :type '(choice (const null) boolean)
360 :group 'cperl-affected-by-hairy)
361
362 (defcustom cperl-electric-keywords nil
363 "*Not-nil (and non-null) means keywords are electric in CPerl.
364 Can be overwritten by `cperl-hairy' if nil.
365
366 Uses `abbrev-mode' to do the expansion. If you want to use your
367 own abbrevs in cperl-mode, but do not want keywords to be
368 electric, you must redefine `cperl-mode-abbrev-table': do
369 \\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
370 that paragraph, delete the words that appear at the ends of lines and
371 that begin with \"cperl-electric\".
372 "
373 :type '(choice (const null) boolean)
374 :group 'cperl-affected-by-hairy)
375
376 (defcustom cperl-electric-backspace-untabify t
377 "*Not-nil means electric-backspace will untabify in CPerl."
378 :type 'boolean
379 :group 'cperl-autoinsert-details)
380
381 (defcustom cperl-hairy nil
382 "*Not-nil means most of the bells and whistles are enabled in CPerl.
383 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
384 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
385 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
386 `cperl-lazy-help-time'."
387 :type 'boolean
388 :group 'cperl-affected-by-hairy)
389
390 (defcustom cperl-comment-column 32
391 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
392 :type 'integer
393 :group 'cperl-indentation-details)
394
395 (defcustom cperl-indent-comment-at-column-0 nil
396 "*Non-nil means that comment started at column 0 should be indentable."
397 :type 'boolean
398 :group 'cperl-indentation-details)
399
400 (defcustom cperl-vc-sccs-header '("($sccs) = ('%W\%' =~ /(\\d+(\\.\\d+)+)/) ;")
401 "*Special version of `vc-sccs-header' that is used in CPerl mode buffers."
402 :type '(repeat string)
403 :group 'cperl)
404
405 (defcustom cperl-vc-rcs-header '("($rcs) = (' $Id\$ ' =~ /(\\d+(\\.\\d+)+)/);")
406 "*Special version of `vc-rcs-header' that is used in CPerl mode buffers."
407 :type '(repeat string)
408 :group 'cperl)
409
410 ;; This became obsolete...
411 (defvar cperl-vc-header-alist nil)
412 (make-obsolete-variable
413 'cperl-vc-header-alist
414 "use cperl-vc-rcs-header or cperl-vc-sccs-header instead."
415 "22.1")
416
417 (defcustom cperl-clobber-mode-lists
418 (not
419 (and
420 (boundp 'interpreter-mode-alist)
421 (assoc "miniperl" interpreter-mode-alist)
422 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
423 "*Whether to install us into `interpreter-' and `extension' mode lists."
424 :type 'boolean
425 :group 'cperl)
426
427 (defcustom cperl-info-on-command-no-prompt nil
428 "*Not-nil (and non-null) means not to prompt on C-h f.
429 The opposite behavior is always available if prefixed with C-c.
430 Can be overwritten by `cperl-hairy' if nil."
431 :type '(choice (const null) boolean)
432 :group 'cperl-affected-by-hairy)
433
434 (defcustom cperl-clobber-lisp-bindings nil
435 "*Not-nil (and non-null) means not overwrite C-h f.
436 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
437 Can be overwritten by `cperl-hairy' if nil."
438 :type '(choice (const null) boolean)
439 :group 'cperl-affected-by-hairy)
440
441 (defcustom cperl-lazy-help-time nil
442 "*Not-nil (and non-null) means to show lazy help after given idle time.
443 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
444 :type '(choice (const null) (const nil) integer)
445 :group 'cperl-affected-by-hairy)
446
447 (defcustom cperl-pod-face 'font-lock-comment-face
448 "*Face for POD highlighting."
449 :type 'face
450 :group 'cperl-faces)
451
452 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
453 "*Face for POD highlighting.
454 Font for POD headers."
455 :type 'face
456 :group 'cperl-faces)
457
458 (defcustom cperl-here-face 'font-lock-string-face
459 "*Face for here-docs highlighting."
460 :type 'face
461 :group 'cperl-faces)
462
463 ;;; Some double-evaluation happened with font-locks... Needed with 21.2...
464 (defvar cperl-singly-quote-face (featurep 'xemacs))
465
466 (defcustom cperl-invalid-face 'underline
467 "*Face for highlighting trailing whitespace."
468 :type 'face
469 :version "21.1"
470 :group 'cperl-faces)
471
472 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
473 "*Not-nil after evaluation means to highlight POD and here-docs sections."
474 :type 'boolean
475 :group 'cperl-faces)
476
477 (defcustom cperl-fontify-m-as-s t
478 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
479 :type 'boolean
480 :group 'cperl-faces)
481
482 (defcustom cperl-highlight-variables-indiscriminately nil
483 "*Non-nil means perform additional highlighting on variables.
484 Currently only changes how scalar variables are highlighted.
485 Note that that variable is only read at initialization time for
486 the variable `cperl-font-lock-keywords-2', so changing it after you've
487 entered CPerl mode the first time will have no effect."
488 :type 'boolean
489 :group 'cperl)
490
491 (defcustom cperl-pod-here-scan t
492 "*Not-nil means look for POD and here-docs sections during startup.
493 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
494 :type 'boolean
495 :group 'cperl-speed)
496
497 (defcustom cperl-regexp-scan t
498 "*Not-nil means make marking of regular expression more thorough.
499 Effective only with `cperl-pod-here-scan'."
500 :type 'boolean
501 :group 'cperl-speed)
502
503 (defcustom cperl-hook-after-change t
504 "*Not-nil means install hook to know which regions of buffer are changed.
505 May significantly speed up delayed fontification. Changes take effect
506 after reload."
507 :type 'boolean
508 :group 'cperl-speed)
509
510 (defcustom cperl-imenu-addback nil
511 "*Not-nil means add backreferences to generated `imenu's.
512 May require patched `imenu' and `imenu-go'. Obsolete."
513 :type 'boolean
514 :group 'cperl-help-system)
515
516 (defcustom cperl-max-help-size 66
517 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
518 :type '(choice integer (const nil))
519 :group 'cperl-help-system)
520
521 (defcustom cperl-shrink-wrap-info-frame t
522 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
523 :type 'boolean
524 :group 'cperl-help-system)
525
526 (defcustom cperl-info-page "perl"
527 "*Name of the info page containing perl docs.
528 Older version of this page was called `perl5', newer `perl'."
529 :type 'string
530 :group 'cperl-help-system)
531
532 (defcustom cperl-use-syntax-table-text-property
533 (boundp 'parse-sexp-lookup-properties)
534 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
535 :type 'boolean
536 :group 'cperl-speed)
537
538 (defcustom cperl-use-syntax-table-text-property-for-tags
539 cperl-use-syntax-table-text-property
540 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
541 :type 'boolean
542 :group 'cperl-speed)
543
544 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
545 "*Regexp to match files to scan when generating TAGS."
546 :type 'regexp
547 :group 'cperl)
548
549 (defcustom cperl-noscan-files-regexp
550 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
551 "*Regexp to match files/dirs to skip when generating TAGS."
552 :type 'regexp
553 :group 'cperl)
554
555 (defcustom cperl-regexp-indent-step nil
556 "*Indentation used when beautifying regexps.
557 If nil, the value of `cperl-indent-level' will be used."
558 :type '(choice integer (const nil))
559 :group 'cperl-indentation-details)
560
561 (defcustom cperl-indent-left-aligned-comments t
562 "*Non-nil means that the comment starting in leftmost column should indent."
563 :type 'boolean
564 :group 'cperl-indentation-details)
565
566 (defcustom cperl-under-as-char nil
567 "*Non-nil means that the _ (underline) should be treated as word char."
568 :type 'boolean
569 :group 'cperl)
570
571 (defcustom cperl-extra-perl-args ""
572 "*Extra arguments to use when starting Perl.
573 Currently used with `cperl-check-syntax' only."
574 :type 'string
575 :group 'cperl)
576
577 (defcustom cperl-message-electric-keyword t
578 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
579 :type 'boolean
580 :group 'cperl-help-system)
581
582 (defcustom cperl-indent-region-fix-constructs 1
583 "*Amount of space to insert between `}' and `else' or `elsif'
584 in `cperl-indent-region'. Set to nil to leave as is. Values other
585 than 1 and nil will probably not work."
586 :type '(choice (const nil) (const 1))
587 :group 'cperl-indentation-details)
588
589 (defcustom cperl-break-one-line-blocks-when-indent t
590 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
591 need to be reformatted into multiline ones when indenting a region."
592 :type 'boolean
593 :group 'cperl-indentation-details)
594
595 (defcustom cperl-fix-hanging-brace-when-indent t
596 "*Non-nil means that BLOCK-end `}' may be put on a separate line
597 when indenting a region.
598 Braces followed by else/elsif/while/until are excepted."
599 :type 'boolean
600 :group 'cperl-indentation-details)
601
602 (defcustom cperl-merge-trailing-else t
603 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
604 may be merged to be on the same line when indenting a region."
605 :type 'boolean
606 :group 'cperl-indentation-details)
607
608 (defcustom cperl-indent-parens-as-block nil
609 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
610 but for trailing \",\" inside the group, which won't increase indentation.
611 One should tune up `cperl-close-paren-offset' as well."
612 :type 'boolean
613 :group 'cperl-indentation-details)
614
615 (defcustom cperl-syntaxify-by-font-lock
616 (and cperl-can-font-lock
617 (boundp 'parse-sexp-lookup-properties))
618 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
619 :type '(choice (const message) boolean)
620 :group 'cperl-speed)
621
622 (defcustom cperl-syntaxify-unwind
623 t
624 "*Non-nil means that CPerl unwinds to a start of a long construction
625 when syntaxifying a chunk of buffer."
626 :type 'boolean
627 :group 'cperl-speed)
628
629 (defcustom cperl-syntaxify-for-menu
630 t
631 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
632 This way enabling/disabling of menu items is more correct."
633 :type 'boolean
634 :group 'cperl-speed)
635
636 (defcustom cperl-ps-print-face-properties
637 '((font-lock-keyword-face nil nil bold shadow)
638 (font-lock-variable-name-face nil nil bold)
639 (font-lock-function-name-face nil nil bold italic box)
640 (font-lock-constant-face nil "LightGray" bold)
641 (cperl-array-face nil "LightGray" bold underline)
642 (cperl-hash-face nil "LightGray" bold italic underline)
643 (font-lock-comment-face nil "LightGray" italic)
644 (font-lock-string-face nil nil italic underline)
645 (cperl-nonoverridable-face nil nil italic underline)
646 (font-lock-type-face nil nil underline)
647 (font-lock-warning-face nil "LightGray" bold italic box)
648 (underline nil "LightGray" strikeout))
649 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
650 :type '(repeat (cons symbol
651 (cons (choice (const nil) string)
652 (cons (choice (const nil) string)
653 (repeat symbol)))))
654 :group 'cperl-faces)
655
656 (defvar cperl-dark-background
657 (cperl-choose-color "navy" "os2blue" "darkgreen"))
658 (defvar cperl-dark-foreground
659 (cperl-choose-color "orchid1" "orange"))
660
661 (defface cperl-nonoverridable-face
662 `((((class grayscale) (background light))
663 (:background "Gray90" :slant italic :underline t))
664 (((class grayscale) (background dark))
665 (:foreground "Gray80" :slant italic :underline t :weight bold))
666 (((class color) (background light))
667 (:foreground "chartreuse3"))
668 (((class color) (background dark))
669 (:foreground ,cperl-dark-foreground))
670 (t (:weight bold :underline t)))
671 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
672 :group 'cperl-faces)
673
674 (defface cperl-array-face
675 `((((class grayscale) (background light))
676 (:background "Gray90" :weight bold))
677 (((class grayscale) (background dark))
678 (:foreground "Gray80" :weight bold))
679 (((class color) (background light))
680 (:foreground "Blue" :background "lightyellow2" :weight bold))
681 (((class color) (background dark))
682 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
683 (t (:weight bold)))
684 "Font Lock mode face used to highlight array names."
685 :group 'cperl-faces)
686
687 (defface cperl-hash-face
688 `((((class grayscale) (background light))
689 (:background "Gray90" :weight bold :slant italic))
690 (((class grayscale) (background dark))
691 (:foreground "Gray80" :weight bold :slant italic))
692 (((class color) (background light))
693 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
694 (((class color) (background dark))
695 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
696 (t (:weight bold :slant italic)))
697 "Font Lock mode face used to highlight hash names."
698 :group 'cperl-faces)
699
700 \f
701
702 ;;; Short extra-docs.
703
704 (defvar cperl-tips 'please-ignore-this-line
705 "Get maybe newer version of this package from
706 http://ilyaz.org/software/emacs
707 Subdirectory `cperl-mode' may contain yet newer development releases and/or
708 patches to related files.
709
710 For best results apply to an older Emacs the patches from
711 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
712 \(this upgrades syntax-parsing abilities of Emacsen v19.34 and
713 v20.2 up to the level of Emacs v20.3 - a must for a good Perl
714 mode.) As of beginning of 2003, XEmacs may provide a similar ability.
715
716 Get support packages choose-color.el (or font-lock-extra.el before
717 19.30), imenu-go.el from the same place. \(Look for other files there
718 too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
719 later you should use choose-color.el *instead* of font-lock-extra.el
720 \(and you will not get smart highlighting in C :-().
721
722 Note that to enable Compile choices in the menu you need to install
723 mode-compile.el.
724
725 If your Emacs does not default to `cperl-mode' on Perl files, and you
726 want it to: put the following into your .emacs file:
727
728 (defalias 'perl-mode 'cperl-mode)
729
730 Get perl5-info from
731 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
732 Also, one can generate a newer documentation running `pod2texi' converter
733 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
734
735 If you use imenu-go, run imenu on perl5-info buffer (you can do it
736 from Perl menu). If many files are related, generate TAGS files from
737 Tools/Tags submenu in Perl menu.
738
739 If some class structure is too complicated, use Tools/Hierarchy-view
740 from Perl menu, or hierarchic view of imenu. The second one uses the
741 current buffer only, the first one requires generation of TAGS from
742 Perl/Tools/Tags menu beforehand.
743
744 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
745
746 Switch auto-help on/off with Perl/Tools/Auto-help.
747
748 Though with contemporary Emaxen CPerl mode should maintain the correct
749 parsing of Perl even when editing, sometimes it may be lost. Fix this by
750
751 \\[normal-mode]
752
753 In cases of more severe confusion sometimes it is helpful to do
754
755 \\[load-library] cperl-mode RET
756 \\[normal-mode]
757
758 Before reporting (non-)problems look in the problem section of online
759 micro-docs on what I know about CPerl problems.")
760
761 (defvar cperl-problems 'please-ignore-this-line
762 "Description of problems in CPerl mode.
763 Some faces will not be shown on some versions of Emacs unless you
764 install choose-color.el, available from
765 http://ilyaz.org/software/emacs
766
767 `fill-paragraph' on a comment may leave the point behind the
768 paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
769 to detect it and bulk out).
770
771 See documentation of a variable `cperl-problems-old-emaxen' for the
772 problems which disappear if you upgrade Emacs to a reasonably new
773 version (20.3 for Emacs, and those of 2004 for XEmacs).")
774
775 (defvar cperl-problems-old-emaxen 'please-ignore-this-line
776 "Description of problems in CPerl mode specific for older Emacs versions.
777
778 Emacs had a _very_ restricted syntax parsing engine until version
779 20.1. Most problems below are corrected starting from this version of
780 Emacs, and all of them should be fixed in version 20.3. (Or apply
781 patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
782 this respect (until 2003).
783
784 Note that even with newer Emacsen in some very rare cases the details
785 of interaction of `font-lock' and syntaxification may be not cleaned
786 up yet. You may get slightly different colors basing on the order of
787 fontification and syntaxification. Say, the initial faces is correct,
788 but editing the buffer breaks this.
789
790 Even with older Emacsen CPerl mode tries to corrects some Emacs
791 misunderstandings, however, for efficiency reasons the degree of
792 correction is different for different operations. The partially
793 corrected problems are: POD sections, here-documents, regexps. The
794 operations are: highlighting, indentation, electric keywords, electric
795 braces.
796
797 This may be confusing, since the regexp s#//#/#\; may be highlighted
798 as a comment, but it will be recognized as a regexp by the indentation
799 code. Or the opposite case, when a POD section is highlighted, but
800 may break the indentation of the following code (though indentation
801 should work if the balance of delimiters is not broken by POD).
802
803 The main trick (to make $ a \"backslash\") makes constructions like
804 ${aaa} look like unbalanced braces. The only trick I can think of is
805 to insert it as $ {aaa} (valid in perl5, not in perl4).
806
807 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
808 as /($|\\s)/. Note that such a transposition is not always possible.
809
810 The solution is to upgrade your Emacs or patch an older one. Note
811 that Emacs 20.2 has some bugs related to `syntax-table' text
812 properties. Patches are available on the main CPerl download site,
813 and on CPAN.
814
815 If these bugs cannot be fixed on your machine (say, you have an inferior
816 environment and cannot recompile), you may still disable all the fancy stuff
817 via `cperl-use-syntax-table-text-property'.")
818
819 (defvar cperl-praise 'please-ignore-this-line
820 "Advantages of CPerl mode.
821
822 0) It uses the newest `syntax-table' property ;-);
823
824 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
825 mode - but the latter number may have improved too in last years) even
826 with old Emaxen which do not support `syntax-table' property.
827
828 When using `syntax-table' property for syntax assist hints, it should
829 handle 99.995% of lines correct - or somesuch. It automatically
830 updates syntax assist hints when you edit your script.
831
832 2) It is generally believed to be \"the most user-friendly Emacs
833 package\" whatever it may mean (I doubt that the people who say similar
834 things tried _all_ the rest of Emacs ;-), but this was not a lonely
835 voice);
836
837 3) Everything is customizable, one-by-one or in a big sweep;
838
839 4) It has many easily-accessible \"tools\":
840 a) Can run program, check syntax, start debugger;
841 b) Can lineup vertically \"middles\" of rows, like `=' in
842 a = b;
843 cc = d;
844 c) Can insert spaces where this impoves readability (in one
845 interactive sweep over the buffer);
846 d) Has support for imenu, including:
847 1) Separate unordered list of \"interesting places\";
848 2) Separate TOC of POD sections;
849 3) Separate list of packages;
850 4) Hierarchical view of methods in (sub)packages;
851 5) and functions (by the full name - with package);
852 e) Has an interface to INFO docs for Perl; The interface is
853 very flexible, including shrink-wrapping of
854 documentation buffer/frame;
855 f) Has a builtin list of one-line explanations for perl constructs.
856 g) Can show these explanations if you stay long enough at the
857 corresponding place (or on demand);
858 h) Has an enhanced fontification (using 3 or 4 additional faces
859 comparing to font-lock - basically, different
860 namespaces in Perl have different colors);
861 i) Can construct TAGS basing on its knowledge of Perl syntax,
862 the standard menu has 6 different way to generate
863 TAGS (if \"by directory\", .xs files - with C-language
864 bindings - are included in the scan);
865 j) Can build a hierarchical view of classes (via imenu) basing
866 on generated TAGS file;
867 k) Has electric parentheses, electric newlines, uses Abbrev
868 for electric logical constructs
869 while () {}
870 with different styles of expansion (context sensitive
871 to be not so bothering). Electric parentheses behave
872 \"as they should\" in a presence of a visible region.
873 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
874 m) Can convert from
875 if (A) { B }
876 to
877 B if A;
878
879 n) Highlights (by user-choice) either 3-delimiters constructs
880 (such as tr/a/b/), or regular expressions and `y/tr';
881 o) Highlights trailing whitespace;
882 p) Is able to manipulate Perl Regular Expressions to ease
883 conversion to a more readable form.
884 q) Can ispell POD sections and HERE-DOCs.
885 r) Understands comments and character classes inside regular
886 expressions; can find matching () and [] in a regular expression.
887 s) Allows indentation of //x-style regular expressions;
888 t) Highlights different symbols in regular expressions according
889 to their function; much less problems with backslashitis;
890 u) Allows to find regular expressions which contain interpolated parts.
891
892 5) The indentation engine was very smart, but most of tricks may be
893 not needed anymore with the support for `syntax-table' property. Has
894 progress indicator for indentation (with `imenu' loaded).
895
896 6) Indent-region improves inline-comments as well; also corrects
897 whitespace *inside* the conditional/loop constructs.
898
899 7) Fill-paragraph correctly handles multi-line comments;
900
901 8) Can switch to different indentation styles by one command, and restore
902 the settings present before the switch.
903
904 9) When doing indentation of control constructs, may correct
905 line-breaks/spacing between elements of the construct.
906
907 10) Uses a linear-time algorith for indentation of regions (on Emaxen with
908 capable syntax engines).
909
910 11) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
911 ")
912
913 (defvar cperl-speed 'please-ignore-this-line
914 "This is an incomplete compendium of what is available in other parts
915 of CPerl documentation. (Please inform me if I skept anything.)
916
917 There is a perception that CPerl is slower than alternatives. This part
918 of documentation is designed to overcome this misconception.
919
920 *By default* CPerl tries to enable the most comfortable settings.
921 From most points of view, correctly working package is infinitely more
922 comfortable than a non-correctly working one, thus by default CPerl
923 prefers correctness over speed. Below is the guide how to change
924 settings if your preferences are different.
925
926 A) Speed of loading the file. When loading file, CPerl may perform a
927 scan which indicates places which cannot be parsed by primitive Emacs
928 syntax-parsing routines, and marks them up so that either
929
930 A1) CPerl may work around these deficiencies (for big chunks, mostly
931 PODs and HERE-documents), or
932 A2) On capable Emaxen CPerl will use improved syntax-handlings
933 which reads mark-up hints directly.
934
935 The scan in case A2 is much more comprehensive, thus may be slower.
936
937 User can disable syntax-engine-helping scan of A2 by setting
938 `cperl-use-syntax-table-text-property'
939 variable to nil (if it is set to t).
940
941 One can disable the scan altogether (both A1 and A2) by setting
942 `cperl-pod-here-scan'
943 to nil.
944
945 B) Speed of editing operations.
946
947 One can add a (minor) speedup to editing operations by setting
948 `cperl-use-syntax-table-text-property'
949 variable to nil (if it is set to t). This will disable
950 syntax-engine-helping scan, thus will make many more Perl
951 constructs be wrongly recognized by CPerl, thus may lead to
952 wrongly matched parentheses, wrong indentation, etc.
953
954 One can unset `cperl-syntaxify-unwind'. This might speed up editing
955 of, say, long POD sections.")
956
957 (defvar cperl-tips-faces 'please-ignore-this-line
958 "CPerl mode uses following faces for highlighting:
959
960 `cperl-array-face' Array names
961 `cperl-hash-face' Hash names
962 `font-lock-comment-face' Comments, PODs and whatever is considered
963 syntaxically to be not code
964 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
965 2-arg operators s/y/tr/ or of RExen,
966 `font-lock-warning-face' Special-cased m// and s//foo/,
967 `font-lock-function-name-face' _ as a target of a file tests, file tests,
968 subroutine names at the moment of definition
969 (except those conflicting with Perl operators),
970 package names (when recognized), format names
971 `font-lock-keyword-face' Control flow switch constructs, declarators
972 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
973 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
974 literal parts and the terminator of formats
975 and whatever is syntaxically considered
976 as string literals
977 `font-lock-type-face' Overridable keywords
978 `font-lock-variable-name-face' Variable declarations, indirect array and
979 hash names, POD headers/item names
980 `cperl-invalid-face' Trailing whitespace
981
982 Note that in several situations the highlighting tries to inform about
983 possible confusion, such as different colors for function names in
984 declarations depending on what they (do not) override, or special cases
985 m// and s/// which do not do what one would expect them to do.
986
987 Help with best setup of these faces for printout requested (for each of
988 the faces: please specify bold, italic, underline, shadow and box.)
989
990 In regular expressions (including character classes):
991 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
992 `font-lock-constant-face': Delimiters
993 `font-lock-warning-face' Special-cased m// and s//foo/,
994 Mismatched closing delimiters, parens
995 we couldn't match, misplaced quantifiers,
996 unrecognized escape sequences
997 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
998 `font-lock-type-face' escape sequences with arguments (\\x \\23 \\p \\N)
999 and others match-a-char escape sequences
1000 `font-lock-keyword-face' Capturing parens, and |
1001 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
1002 \"Range -\" in character classes
1003 `font-lock-builtin-face' \"Remaining\" 0-length constructs, multipliers
1004 ?+*{}, not-capturing parens, leading
1005 backslashes of escape sequences
1006 `font-lock-variable-name-face' Interpolated constructs, embedded code,
1007 POSIX classes (inside charclasses)
1008 `font-lock-comment-face' Embedded comments
1009
1010 ")
1011
1012 \f
1013
1014 ;;; Portability stuff:
1015
1016 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
1017 `(define-key cperl-mode-map
1018 ,(if xemacs-key
1019 `(if (featurep 'xemacs) ,xemacs-key ,emacs-key)
1020 emacs-key)
1021 ,definition))
1022
1023 (defvar cperl-del-back-ch
1024 (car (append (where-is-internal 'delete-backward-char)
1025 (where-is-internal 'backward-delete-char-untabify)))
1026 "Character generated by key bound to `delete-backward-char'.")
1027
1028 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
1029 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1030
1031 (defun cperl-mark-active () (mark)) ; Avoid undefined warning
1032 (if (featurep 'xemacs)
1033 (progn
1034 ;; "Active regions" are on: use region only if active
1035 ;; "Active regions" are off: use region unconditionally
1036 (defun cperl-use-region-p ()
1037 (if zmacs-regions (mark) t)))
1038 (defun cperl-use-region-p ()
1039 (if transient-mark-mode mark-active t))
1040 (defun cperl-mark-active () mark-active))
1041
1042 (defsubst cperl-enable-font-lock ()
1043 cperl-can-font-lock)
1044
1045 (defun cperl-putback-char (c) ; Emacs 19
1046 (set 'unread-command-events (list c))) ; Avoid undefined warning
1047
1048 (if (featurep 'xemacs)
1049 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1050 (setq unread-command-events (list (eval '(character-to-event c))))))
1051
1052 (or (fboundp 'uncomment-region)
1053 (defun uncomment-region (beg end)
1054 (interactive "r")
1055 (comment-region beg end -1)))
1056
1057 (defvar cperl-do-not-fontify
1058 (if (string< emacs-version "19.30")
1059 'fontified
1060 'lazy-lock)
1061 "Text property which inhibits refontification.")
1062
1063 (defsubst cperl-put-do-not-fontify (from to &optional post)
1064 ;; If POST, do not do it with postponed fontification
1065 (if (and post cperl-syntaxify-by-font-lock)
1066 nil
1067 (put-text-property (max (point-min) (1- from))
1068 to cperl-do-not-fontify t)))
1069
1070 (defcustom cperl-mode-hook nil
1071 "Hook run by CPerl mode."
1072 :type 'hook
1073 :group 'cperl)
1074
1075 (defvar cperl-syntax-state nil)
1076 (defvar cperl-syntax-done-to nil)
1077 (defvar cperl-emacs-can-parse (> (length (save-excursion
1078 (parse-partial-sexp (point) (point)))) 9))
1079 \f
1080 ;; Make customization possible "in reverse"
1081 (defsubst cperl-val (symbol &optional default hairy)
1082 (cond
1083 ((eq (symbol-value symbol) 'null) default)
1084 (cperl-hairy (or hairy t))
1085 (t (symbol-value symbol))))
1086 \f
1087
1088 (defun cperl-make-indent (column &optional minimum keep)
1089 "Makes indent of the current line the requested amount.
1090 Unless KEEP, removes the old indentation. Works around a bug in ancient
1091 versions of Emacs."
1092 (let ((prop (get-text-property (point) 'syntax-type)))
1093 (or keep
1094 (delete-horizontal-space))
1095 (indent-to column minimum)
1096 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1097 (and prop
1098 (> (current-column) 0)
1099 (save-excursion
1100 (beginning-of-line)
1101 (or (get-text-property (point) 'syntax-type)
1102 (and (looking-at "\\=[ \t]")
1103 (put-text-property (point) (match-end 0)
1104 'syntax-type prop)))))))
1105
1106 ;;; Probably it is too late to set these guys already, but it can help later:
1107
1108 ;;;(and cperl-clobber-mode-lists
1109 ;;;(setq auto-mode-alist
1110 ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1111 ;;;(and (boundp 'interpreter-mode-alist)
1112 ;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1113 ;;; '(("miniperl" . perl-mode))))))
1114 (eval-when-compile
1115 (mapc (lambda (p)
1116 (condition-case nil
1117 (require p)
1118 (error nil)))
1119 '(imenu easymenu etags timer man info))
1120 (if (fboundp 'ps-extend-face-list)
1121 (defmacro cperl-ps-extend-face-list (arg)
1122 `(ps-extend-face-list ,arg))
1123 (defmacro cperl-ps-extend-face-list (arg)
1124 `(error "This version of Emacs has no `ps-extend-face-list'")))
1125 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1126 ;; macros instead of defsubsts don't work on Emacs, so we do the
1127 ;; expansion manually. Any other suggestions?
1128 (require 'cl))
1129
1130 (defvar cperl-mode-abbrev-table nil
1131 "Abbrev table in use in CPerl mode buffers.")
1132
1133 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1134
1135 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
1136
1137 (if cperl-mode-map nil
1138 (setq cperl-mode-map (make-sparse-keymap))
1139 (cperl-define-key "{" 'cperl-electric-lbrace)
1140 (cperl-define-key "[" 'cperl-electric-paren)
1141 (cperl-define-key "(" 'cperl-electric-paren)
1142 (cperl-define-key "<" 'cperl-electric-paren)
1143 (cperl-define-key "}" 'cperl-electric-brace)
1144 (cperl-define-key "]" 'cperl-electric-rparen)
1145 (cperl-define-key ")" 'cperl-electric-rparen)
1146 (cperl-define-key ";" 'cperl-electric-semi)
1147 (cperl-define-key ":" 'cperl-electric-terminator)
1148 (cperl-define-key "\C-j" 'newline-and-indent)
1149 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1150 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1151 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1152 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1153 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1154 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1155 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1156 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1157 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1158 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1159 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1160 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1161 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1162 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1163 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1164 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1165 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
1166 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1167 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1168 [(control meta |)])
1169 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1170 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1171 (cperl-define-key "\177" 'cperl-electric-backspace)
1172 (cperl-define-key "\t" 'cperl-indent-command)
1173 ;; don't clobber the backspace binding:
1174 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1175 [(control c) (control h) F])
1176 (if (cperl-val 'cperl-clobber-lisp-bindings)
1177 (progn
1178 (cperl-define-key "\C-hf"
1179 ;;(concat (char-to-string help-char) "f") ; does not work
1180 'cperl-info-on-command
1181 [(control h) f])
1182 (cperl-define-key "\C-hv"
1183 ;;(concat (char-to-string help-char) "v") ; does not work
1184 'cperl-get-help
1185 [(control h) v])
1186 (cperl-define-key "\C-c\C-hf"
1187 ;;(concat (char-to-string help-char) "f") ; does not work
1188 (key-binding "\C-hf")
1189 [(control c) (control h) f])
1190 (cperl-define-key "\C-c\C-hv"
1191 ;;(concat (char-to-string help-char) "v") ; does not work
1192 (key-binding "\C-hv")
1193 [(control c) (control h) v]))
1194 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1195 [(control c) (control h) f])
1196 (cperl-define-key "\C-c\C-hv"
1197 ;;(concat (char-to-string help-char) "v") ; does not work
1198 'cperl-get-help
1199 [(control c) (control h) v]))
1200 (if (and (featurep 'xemacs)
1201 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1202 (progn
1203 ;; substitute-key-definition is usefulness-deenhanced...
1204 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1205 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1206 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1207 (or (boundp 'fill-paragraph-function)
1208 (substitute-key-definition
1209 'fill-paragraph 'cperl-fill-paragraph
1210 cperl-mode-map global-map))
1211 (substitute-key-definition
1212 'indent-sexp 'cperl-indent-exp
1213 cperl-mode-map global-map)
1214 (substitute-key-definition
1215 'indent-region 'cperl-indent-region
1216 cperl-mode-map global-map)
1217 (substitute-key-definition
1218 'indent-for-comment 'cperl-indent-for-comment
1219 cperl-mode-map global-map)))
1220
1221 (defvar cperl-menu)
1222 (defvar cperl-lazy-installed)
1223 (defvar cperl-old-style nil)
1224 (condition-case nil
1225 (progn
1226 (require 'easymenu)
1227 (easy-menu-define
1228 cperl-menu cperl-mode-map "Menu for CPerl mode"
1229 '("Perl"
1230 ["Beginning of function" beginning-of-defun t]
1231 ["End of function" end-of-defun t]
1232 ["Mark function" mark-defun t]
1233 ["Indent expression" cperl-indent-exp t]
1234 ["Fill paragraph/comment" fill-paragraph t]
1235 "----"
1236 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1237 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1238 ("Regexp"
1239 ["Beautify" cperl-beautify-regexp
1240 cperl-use-syntax-table-text-property]
1241 ["Beautify one level deep" (cperl-beautify-regexp 1)
1242 cperl-use-syntax-table-text-property]
1243 ["Beautify a group" cperl-beautify-level
1244 cperl-use-syntax-table-text-property]
1245 ["Beautify a group one level deep" (cperl-beautify-level 1)
1246 cperl-use-syntax-table-text-property]
1247 ["Contract a group" cperl-contract-level
1248 cperl-use-syntax-table-text-property]
1249 ["Contract groups" cperl-contract-levels
1250 cperl-use-syntax-table-text-property]
1251 "----"
1252 ["Find next interpolated" cperl-next-interpolated-REx
1253 (next-single-property-change (point-min) 'REx-interpolated)]
1254 ["Find next interpolated (no //o)"
1255 cperl-next-interpolated-REx-0
1256 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1257 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1258 ["Find next interpolated (neither //o nor whole-REx)"
1259 cperl-next-interpolated-REx-1
1260 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1261 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1262 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1263 "----"
1264 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1265 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1266 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1267 "----"
1268 ["Run" mode-compile (fboundp 'mode-compile)]
1269 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1270 (get-buffer "*compilation*"))]
1271 ["Next error" next-error (get-buffer "*compilation*")]
1272 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1273 "----"
1274 ["Debugger" cperl-db t]
1275 "----"
1276 ("Tools"
1277 ["Imenu" imenu (fboundp 'imenu)]
1278 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1279 "----"
1280 ["Ispell PODs" cperl-pod-spell
1281 ;; Better not to update syntaxification here:
1282 ;; debugging syntaxificatio can be broken by this???
1283 (or
1284 (get-text-property (point-min) 'in-pod)
1285 (< (progn
1286 (and cperl-syntaxify-for-menu
1287 (cperl-update-syntaxification (point-max) (point-max)))
1288 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1289 (point-max)))]
1290 ["Ispell HERE-DOCs" cperl-here-doc-spell
1291 (< (progn
1292 (and cperl-syntaxify-for-menu
1293 (cperl-update-syntaxification (point-max) (point-max)))
1294 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1295 (point-max))]
1296 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1297 (eq 'here-doc (progn
1298 (and cperl-syntaxify-for-menu
1299 (cperl-update-syntaxification (point) (point)))
1300 (get-text-property (point) 'syntax-type)))]
1301 ["Select this HERE-DOC or POD section"
1302 cperl-select-this-pod-or-here-doc
1303 (memq (progn
1304 (and cperl-syntaxify-for-menu
1305 (cperl-update-syntaxification (point) (point)))
1306 (get-text-property (point) 'syntax-type))
1307 '(here-doc pod))]
1308 "----"
1309 ["CPerl pretty print (exprmntl)" cperl-ps-print
1310 (fboundp 'ps-extend-face-list)]
1311 "----"
1312 ["Syntaxify region" cperl-find-pods-heres-region
1313 (cperl-use-region-p)]
1314 ["Profile syntaxification" cperl-time-fontification t]
1315 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1316 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1317 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1318 (cperl-toggle-set-debug-unwind nil t) t]
1319 "----"
1320 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1321 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1322 ("Tags"
1323 ;;; ["Create tags for current file" cperl-etags t]
1324 ;;; ["Add tags for current file" (cperl-etags t) t]
1325 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1326 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1327 ;;; ["Create tags for Perl files in (sub)directories"
1328 ;;; (cperl-etags nil 'recursive) t]
1329 ;;; ["Add tags for Perl files in (sub)directories"
1330 ;;; (cperl-etags t 'recursive) t])
1331 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1332 ["Create tags for current file" (cperl-write-tags nil t) t]
1333 ["Add tags for current file" (cperl-write-tags) t]
1334 ["Create tags for Perl files in directory"
1335 (cperl-write-tags nil t nil t) t]
1336 ["Add tags for Perl files in directory"
1337 (cperl-write-tags nil nil nil t) t]
1338 ["Create tags for Perl files in (sub)directories"
1339 (cperl-write-tags nil t t t) t]
1340 ["Add tags for Perl files in (sub)directories"
1341 (cperl-write-tags nil nil t t) t]))
1342 ("Perl docs"
1343 ["Define word at point" imenu-go-find-at-position
1344 (fboundp 'imenu-go-find-at-position)]
1345 ["Help on function" cperl-info-on-command t]
1346 ["Help on function at point" cperl-info-on-current-command t]
1347 ["Help on symbol at point" cperl-get-help t]
1348 ["Perldoc" cperl-perldoc t]
1349 ["Perldoc on word at point" cperl-perldoc-at-point t]
1350 ["View manpage of POD in this file" cperl-build-manpage t]
1351 ["Auto-help on" cperl-lazy-install
1352 (and (fboundp 'run-with-idle-timer)
1353 (not cperl-lazy-installed))]
1354 ["Auto-help off" cperl-lazy-unstall
1355 (and (fboundp 'run-with-idle-timer)
1356 cperl-lazy-installed)])
1357 ("Toggle..."
1358 ["Auto newline" cperl-toggle-auto-newline t]
1359 ["Electric parens" cperl-toggle-electric t]
1360 ["Electric keywords" cperl-toggle-abbrev t]
1361 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1362 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1363 ["Auto fill" auto-fill-mode t])
1364 ("Indent styles..."
1365 ["CPerl" (cperl-set-style "CPerl") t]
1366 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1367 ["GNU" (cperl-set-style "GNU") t]
1368 ["C++" (cperl-set-style "C++") t]
1369 ["K&R" (cperl-set-style "K&R") t]
1370 ["BSD" (cperl-set-style "BSD") t]
1371 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1372 ["Memorize Current" (cperl-set-style "Current") t]
1373 ["Memorized" (cperl-set-style-back) cperl-old-style])
1374 ("Micro-docs"
1375 ["Tips" (describe-variable 'cperl-tips) t]
1376 ["Problems" (describe-variable 'cperl-problems) t]
1377 ["Speed" (describe-variable 'cperl-speed) t]
1378 ["Praise" (describe-variable 'cperl-praise) t]
1379 ["Faces" (describe-variable 'cperl-tips-faces) t]
1380 ["CPerl mode" (describe-function 'cperl-mode) t]
1381 ["CPerl version"
1382 (message "The version of master-file for this CPerl is %s-Emacs"
1383 cperl-version) t]))))
1384 (error nil))
1385
1386 (autoload 'c-macro-expand "cmacexp"
1387 "Display the result of expanding all C macros occurring in the region.
1388 The expansion is entirely correct because it uses the C preprocessor."
1389 t)
1390
1391 ;;; These two must be unwound, otherwise take exponential time
1392 (defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1393 "Regular expression to match optional whitespace with interpspersed comments.
1394 Should contain exactly one group.")
1395
1396 ;;; This one is tricky to unwind; still very inefficient...
1397 (defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1398 "Regular expression to match whitespace with interpspersed comments.
1399 Should contain exactly one group.")
1400
1401
1402 ;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1403 ;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1404 ;;; Details of groups in this may be used in several functions; see comments
1405 ;;; near mentioned above variable(s)...
1406 ;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1407 (defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1408 "Match the text after `sub' in a subroutine declaration.
1409 If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1410 of attributes (if present), or end of the name or prototype (whatever is
1411 the last)."
1412 (concat ; Assume n groups before this...
1413 "\\(" ; n+1=name-group
1414 cperl-white-and-comment-rex ; n+2=pre-name
1415 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1416 "\\)" ; END n+1=name-group
1417 (if named "" "?")
1418 "\\(" ; n+4=proto-group
1419 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1420 "\\(([^()]*)\\)" ; n+6=prototype
1421 "\\)?" ; END n+4=proto-group
1422 "\\(" ; n+7=attr-group
1423 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1424 "\\(" ; n+9=start-attr
1425 ":"
1426 (if attr (concat
1427 "\\("
1428 cperl-maybe-white-and-comment-rex ; whitespace-comments
1429 "\\(\\sw\\|_\\)+" ; attr-name
1430 ;; attr-arg (1 level of internal parens allowed!)
1431 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1432 "\\(" ; optional : (XXX allows trailing???)
1433 cperl-maybe-white-and-comment-rex ; whitespace-comments
1434 ":\\)?"
1435 "\\)+")
1436 "[^:]")
1437 "\\)"
1438 "\\)?" ; END n+6=proto-group
1439 ))
1440
1441 ;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1442 ;;; and `cperl-outline-level'.
1443 ;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1444 (defvar cperl-imenu--function-name-regexp-perl
1445 (concat
1446 "^\\(" ; 1 = all
1447 "\\([ \t]*package" ; 2 = package-group
1448 "\\(" ; 3 = package-name-group
1449 cperl-white-and-comment-rex ; 4 = pre-package-name
1450 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1451 "\\|"
1452 "[ \t]*sub"
1453 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1454 cperl-maybe-white-and-comment-rex ; 15=pre-block
1455 "\\|"
1456 "=head\\([1-4]\\)[ \t]+" ; 16=level
1457 "\\([^\n]+\\)$" ; 17=text
1458 "\\)"))
1459
1460 (defvar cperl-outline-regexp
1461 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1462
1463 (defvar cperl-mode-syntax-table nil
1464 "Syntax table in use in CPerl mode buffers.")
1465
1466 (defvar cperl-string-syntax-table nil
1467 "Syntax table in use in CPerl mode string-like chunks.")
1468
1469 (defsubst cperl-1- (p)
1470 (max (point-min) (1- p)))
1471
1472 (defsubst cperl-1+ (p)
1473 (min (point-max) (1+ p)))
1474
1475 (if cperl-mode-syntax-table
1476 ()
1477 (setq cperl-mode-syntax-table (make-syntax-table))
1478 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1479 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1480 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1481 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1482 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1483 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1484 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1485 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1486 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1487 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1488 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1489 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1490 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1491 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1492 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1493 (if cperl-under-as-char
1494 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1495 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1496 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1497 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1498 (modify-syntax-entry ?$ "." cperl-string-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)
1503 (modify-syntax-entry ?` "." cperl-string-syntax-table)
1504 (modify-syntax-entry ?# "." cperl-string-syntax-table)) ; (?# comment )
1505
1506
1507 \f
1508 (defvar cperl-faces-init nil)
1509 ;; Fix for msb.el
1510 (defvar cperl-msb-fixed nil)
1511 (defvar cperl-use-major-mode 'cperl-mode)
1512 (defvar cperl-font-lock-multiline-start nil)
1513 (defvar cperl-font-lock-multiline nil)
1514 (defvar cperl-font-locking nil)
1515
1516 ;; NB as it stands the code in cperl-mode assumes this only has one
1517 ;; element. If Xemacs 19 support were dropped, this could all be simplified.
1518 (defvar cperl-compilation-error-regexp-alist
1519 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
1520 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
1521 2 3))
1522 "Alist that specifies how to match errors in perl output.")
1523
1524 (defvar compilation-error-regexp-alist)
1525
1526 ;;;###autoload
1527 (defun cperl-mode ()
1528 "Major mode for editing Perl code.
1529 Expression and list commands understand all C brackets.
1530 Tab indents for Perl code.
1531 Paragraphs are separated by blank lines only.
1532 Delete converts tabs to spaces as it moves back.
1533
1534 Various characters in Perl almost always come in pairs: {}, (), [],
1535 sometimes <>. When the user types the first, she gets the second as
1536 well, with optional special formatting done on {}. (Disabled by
1537 default.) You can always quote (with \\[quoted-insert]) the left
1538 \"paren\" to avoid the expansion. The processing of < is special,
1539 since most the time you mean \"less\". CPerl mode tries to guess
1540 whether you want to type pair <>, and inserts is if it
1541 appropriate. You can set `cperl-electric-parens-string' to the string that
1542 contains the parenths from the above list you want to be electrical.
1543 Electricity of parenths is controlled by `cperl-electric-parens'.
1544 You may also set `cperl-electric-parens-mark' to have electric parens
1545 look for active mark and \"embrace\" a region if possible.'
1546
1547 CPerl mode provides expansion of the Perl control constructs:
1548
1549 if, else, elsif, unless, while, until, continue, do,
1550 for, foreach, formy and foreachmy.
1551
1552 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1553
1554 The user types the keyword immediately followed by a space, which
1555 causes the construct to be expanded, and the point is positioned where
1556 she is most likely to want to be. eg. when the user types a space
1557 following \"if\" the following appears in the buffer: if () { or if ()
1558 } { } and the cursor is between the parentheses. The user can then
1559 type some boolean expression within the parens. Having done that,
1560 typing \\[cperl-linefeed] places you - appropriately indented - on a
1561 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1562 directive line, then appropriate number of new lines is inserted).
1563
1564 If CPerl decides that you want to insert \"English\" style construct like
1565
1566 bite if angry;
1567
1568 it will not do any expansion. See also help on variable
1569 `cperl-extra-newline-before-brace'. (Note that one can switch the
1570 help message on expansion by setting `cperl-message-electric-keyword'
1571 to nil.)
1572
1573 \\[cperl-linefeed] is a convenience replacement for typing carriage
1574 return. It places you in the next line with proper indentation, or if
1575 you type it inside the inline block of control construct, like
1576
1577 foreach (@lines) {print; print}
1578
1579 and you are on a boundary of a statement inside braces, it will
1580 transform the construct into a multiline and will place you into an
1581 appropriately indented blank line. If you need a usual
1582 `newline-and-indent' behavior, it is on \\[newline-and-indent],
1583 see documentation on `cperl-electric-linefeed'.
1584
1585 Use \\[cperl-invert-if-unless] to change a construction of the form
1586
1587 if (A) { B }
1588
1589 into
1590
1591 B if A;
1592
1593 \\{cperl-mode-map}
1594
1595 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1596 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1597 on electric space between $ and {, `cperl-electric-parens-string' is
1598 the string that contains parentheses that should be electric in CPerl
1599 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1600 setting `cperl-electric-keywords' enables electric expansion of
1601 control structures in CPerl. `cperl-electric-linefeed' governs which
1602 one of two linefeed behavior is preferable. You can enable all these
1603 options simultaneously (recommended mode of use) by setting
1604 `cperl-hairy' to t. In this case you can switch separate options off
1605 by setting them to `null'. Note that one may undo the extra
1606 whitespace inserted by semis and braces in `auto-newline'-mode by
1607 consequent \\[cperl-electric-backspace].
1608
1609 If your site has perl5 documentation in info format, you can use commands
1610 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1611 These keys run commands `cperl-info-on-current-command' and
1612 `cperl-info-on-command', which one is which is controlled by variable
1613 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1614 \(in turn affected by `cperl-hairy').
1615
1616 Even if you have no info-format documentation, short one-liner-style
1617 help is available on \\[cperl-get-help], and one can run perldoc or
1618 man via menu.
1619
1620 It is possible to show this help automatically after some idle time.
1621 This is regulated by variable `cperl-lazy-help-time'. Default with
1622 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1623 secs idle time . It is also possible to switch this on/off from the
1624 menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1625
1626 Use \\[cperl-lineup] to vertically lineup some construction - put the
1627 beginning of the region at the start of construction, and make region
1628 span the needed amount of lines.
1629
1630 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1631 `cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1632 here-docs sections. With capable Emaxen results of scan are used
1633 for indentation too, otherwise they are used for highlighting only.
1634
1635 Variables controlling indentation style:
1636 `cperl-tab-always-indent'
1637 Non-nil means TAB in CPerl mode should always reindent the current line,
1638 regardless of where in the line point is when the TAB command is used.
1639 `cperl-indent-left-aligned-comments'
1640 Non-nil means that the comment starting in leftmost column should indent.
1641 `cperl-auto-newline'
1642 Non-nil means automatically newline before and after braces,
1643 and after colons and semicolons, inserted in Perl code. The following
1644 \\[cperl-electric-backspace] will remove the inserted whitespace.
1645 Insertion after colons requires both this variable and
1646 `cperl-auto-newline-after-colon' set.
1647 `cperl-auto-newline-after-colon'
1648 Non-nil means automatically newline even after colons.
1649 Subject to `cperl-auto-newline' setting.
1650 `cperl-indent-level'
1651 Indentation of Perl statements within surrounding block.
1652 The surrounding block's indentation is the indentation
1653 of the line on which the open-brace appears.
1654 `cperl-continued-statement-offset'
1655 Extra indentation given to a substatement, such as the
1656 then-clause of an if, or body of a while, or just a statement continuation.
1657 `cperl-continued-brace-offset'
1658 Extra indentation given to a brace that starts a substatement.
1659 This is in addition to `cperl-continued-statement-offset'.
1660 `cperl-brace-offset'
1661 Extra indentation for line if it starts with an open brace.
1662 `cperl-brace-imaginary-offset'
1663 An open brace following other text is treated as if it the line started
1664 this far to the right of the actual line indentation.
1665 `cperl-label-offset'
1666 Extra indentation for line that is a label.
1667 `cperl-min-label-indent'
1668 Minimal indentation for line that is a label.
1669
1670 Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1671 `cperl-indent-level' 5 4 2 4
1672 `cperl-brace-offset' 0 0 0 0
1673 `cperl-continued-brace-offset' -5 -4 0 0
1674 `cperl-label-offset' -5 -4 -2 -4
1675 `cperl-continued-statement-offset' 5 4 2 4
1676
1677 CPerl knows several indentation styles, and may bulk set the
1678 corresponding variables. Use \\[cperl-set-style] to do this. Use
1679 \\[cperl-set-style-back] to restore the memorized preexisting values
1680 \(both available from menu). See examples in `cperl-style-examples'.
1681
1682 Part of the indentation style is how different parts of if/elsif/else
1683 statements are broken into lines; in CPerl, this is reflected on how
1684 templates for these constructs are created (controlled by
1685 `cperl-extra-newline-before-brace'), and how reflow-logic should treat
1686 \"continuation\" blocks of else/elsif/continue, controlled by the same
1687 variable, and by `cperl-extra-newline-before-brace-multiline',
1688 `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1689
1690 If `cperl-indent-level' is 0, the statement after opening brace in
1691 column 0 is indented on
1692 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1693
1694 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1695 with no args.
1696
1697 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1698 or as help on variables `cperl-tips', `cperl-problems',
1699 `cperl-praise', `cperl-speed'."
1700 (interactive)
1701 (kill-all-local-variables)
1702 (use-local-map cperl-mode-map)
1703 (if (cperl-val 'cperl-electric-linefeed)
1704 (progn
1705 (local-set-key "\C-J" 'cperl-linefeed)
1706 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1707 (if (and
1708 (cperl-val 'cperl-clobber-lisp-bindings)
1709 (cperl-val 'cperl-info-on-command-no-prompt))
1710 (progn
1711 ;; don't clobber the backspace binding:
1712 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1713 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1714 [(control c) (control h) f])))
1715 (setq major-mode cperl-use-major-mode)
1716 (setq mode-name "CPerl")
1717 (let ((prev-a-c abbrevs-changed))
1718 (define-abbrev-table 'cperl-mode-abbrev-table '(
1719 ("if" "if" cperl-electric-keyword 0)
1720 ("elsif" "elsif" cperl-electric-keyword 0)
1721 ("while" "while" cperl-electric-keyword 0)
1722 ("until" "until" cperl-electric-keyword 0)
1723 ("unless" "unless" cperl-electric-keyword 0)
1724 ("else" "else" cperl-electric-else 0)
1725 ("continue" "continue" cperl-electric-else 0)
1726 ("for" "for" cperl-electric-keyword 0)
1727 ("foreach" "foreach" cperl-electric-keyword 0)
1728 ("formy" "formy" cperl-electric-keyword 0)
1729 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1730 ("do" "do" cperl-electric-keyword 0)
1731 ("=pod" "=pod" cperl-electric-pod 0)
1732 ("=over" "=over" cperl-electric-pod 0)
1733 ("=head1" "=head1" cperl-electric-pod 0)
1734 ("=head2" "=head2" cperl-electric-pod 0)
1735 ("pod" "pod" cperl-electric-pod 0)
1736 ("over" "over" cperl-electric-pod 0)
1737 ("head1" "head1" cperl-electric-pod 0)
1738 ("head2" "head2" cperl-electric-pod 0)))
1739 (setq abbrevs-changed prev-a-c))
1740 (setq local-abbrev-table cperl-mode-abbrev-table)
1741 (if (cperl-val 'cperl-electric-keywords)
1742 (abbrev-mode 1))
1743 (set-syntax-table cperl-mode-syntax-table)
1744 ;; Until Emacs is multi-threaded, we do not actually need it local:
1745 (make-local-variable 'cperl-font-lock-multiline-start)
1746 (make-local-variable 'cperl-font-locking)
1747 (make-local-variable 'outline-regexp)
1748 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1749 (setq outline-regexp cperl-outline-regexp)
1750 (make-local-variable 'outline-level)
1751 (setq outline-level 'cperl-outline-level)
1752 (make-local-variable 'paragraph-start)
1753 (setq paragraph-start (concat "^$\\|" page-delimiter))
1754 (make-local-variable 'paragraph-separate)
1755 (setq paragraph-separate paragraph-start)
1756 (make-local-variable 'paragraph-ignore-fill-prefix)
1757 (setq paragraph-ignore-fill-prefix t)
1758 (if (featurep 'xemacs)
1759 (progn
1760 (make-local-variable 'paren-backwards-message)
1761 (set 'paren-backwards-message t)))
1762 (make-local-variable 'indent-line-function)
1763 (setq indent-line-function 'cperl-indent-line)
1764 (make-local-variable 'require-final-newline)
1765 (setq require-final-newline mode-require-final-newline)
1766 (make-local-variable 'comment-start)
1767 (setq comment-start "# ")
1768 (make-local-variable 'comment-end)
1769 (setq comment-end "")
1770 (make-local-variable 'comment-column)
1771 (setq comment-column cperl-comment-column)
1772 (make-local-variable 'comment-start-skip)
1773 (setq comment-start-skip "#+ *")
1774 (make-local-variable 'defun-prompt-regexp)
1775 ;;; "[ \t]*sub"
1776 ;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1777 ;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1778 (setq defun-prompt-regexp
1779 (concat "^[ \t]*\\(sub"
1780 (cperl-after-sub-regexp 'named 'attr-groups)
1781 "\\|" ; per toke.c
1782 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1783 "\\)"
1784 cperl-maybe-white-and-comment-rex))
1785 (make-local-variable 'comment-indent-function)
1786 (setq comment-indent-function 'cperl-comment-indent)
1787 (and (boundp 'fill-paragraph-function)
1788 (progn
1789 (make-local-variable 'fill-paragraph-function)
1790 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
1791 (make-local-variable 'parse-sexp-ignore-comments)
1792 (setq parse-sexp-ignore-comments t)
1793 (make-local-variable 'indent-region-function)
1794 (setq indent-region-function 'cperl-indent-region)
1795 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1796 (make-local-variable 'imenu-create-index-function)
1797 (setq imenu-create-index-function
1798 (function cperl-imenu--create-perl-index))
1799 (make-local-variable 'imenu-sort-function)
1800 (setq imenu-sort-function nil)
1801 (make-local-variable 'vc-rcs-header)
1802 (set 'vc-rcs-header cperl-vc-rcs-header)
1803 (make-local-variable 'vc-sccs-header)
1804 (set 'vc-sccs-header cperl-vc-sccs-header)
1805 (when (featurep 'xemacs)
1806 ;; This one is obsolete...
1807 (make-local-variable 'vc-header-alist)
1808 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1809 `((SCCS ,(car cperl-vc-sccs-header))
1810 (RCS ,(car cperl-vc-rcs-header))))))
1811 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1812 (make-local-variable 'compilation-error-regexp-alist-alist)
1813 (set 'compilation-error-regexp-alist-alist
1814 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
1815 (symbol-value 'compilation-error-regexp-alist-alist)))
1816 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1817 (let ((f 'compilation-build-compilation-error-regexp-alist))
1818 (funcall f))
1819 (make-local-variable 'compilation-error-regexp-alist)
1820 (push 'cperl compilation-error-regexp-alist)))
1821 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1822 (make-local-variable 'compilation-error-regexp-alist)
1823 (set 'compilation-error-regexp-alist
1824 (append cperl-compilation-error-regexp-alist
1825 (symbol-value 'compilation-error-regexp-alist)))))
1826 (make-local-variable 'font-lock-defaults)
1827 (setq font-lock-defaults
1828 (cond
1829 ((string< emacs-version "19.30")
1830 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1831 ((string< emacs-version "19.33") ; Which one to use?
1832 '((cperl-font-lock-keywords
1833 cperl-font-lock-keywords-1
1834 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1835 (t
1836 '((cperl-load-font-lock-keywords
1837 cperl-load-font-lock-keywords-1
1838 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1839 (make-local-variable 'cperl-syntax-state)
1840 (setq cperl-syntax-state nil) ; reset syntaxification cache
1841 (if cperl-use-syntax-table-text-property
1842 (if (boundp 'syntax-propertize-function)
1843 (progn
1844 ;; Reset syntaxification cache.
1845 (set (make-local-variable 'cperl-syntax-done-to) nil)
1846 (set (make-local-variable 'syntax-propertize-function)
1847 (lambda (start end)
1848 (goto-char start) (cperl-fontify-syntaxically end))))
1849 (make-local-variable 'parse-sexp-lookup-properties)
1850 ;; Do not introduce variable if not needed, we check it!
1851 (set 'parse-sexp-lookup-properties t)
1852 ;; Fix broken font-lock:
1853 (or (boundp 'font-lock-unfontify-region-function)
1854 (set 'font-lock-unfontify-region-function
1855 'font-lock-default-unfontify-region))
1856 (unless (featurep 'xemacs) ; Our: just a plug for wrong font-lock
1857 (make-local-variable 'font-lock-unfontify-region-function)
1858 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1859 'cperl-font-lock-unfontify-region-function))
1860 (make-local-variable 'cperl-syntax-done-to)
1861 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1862 (make-local-variable 'font-lock-syntactic-keywords)
1863 (setq font-lock-syntactic-keywords
1864 (if cperl-syntaxify-by-font-lock
1865 '((cperl-fontify-syntaxically))
1866 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1867 ;; used to ignore syntax-table text-properties. (t) is a hack
1868 ;; to make font-lock think that font-lock-syntactic-keywords
1869 ;; are defined.
1870 '(t)))))
1871 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1872 (progn
1873 (setq cperl-font-lock-multiline t) ; Not localized...
1874 (set (make-local-variable 'font-lock-multiline) t))
1875 (make-local-variable 'font-lock-fontify-region-function)
1876 (set 'font-lock-fontify-region-function ; not present with old Emacs
1877 'cperl-font-lock-fontify-region-function))
1878 (make-local-variable 'font-lock-fontify-region-function)
1879 (set 'font-lock-fontify-region-function ; not present with old Emacs
1880 'cperl-font-lock-fontify-region-function)
1881 (make-local-variable 'cperl-old-style)
1882 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1883 (set (make-local-variable 'normal-auto-fill-function)
1884 'cperl-do-auto-fill)
1885 (or (fboundp 'cperl-old-auto-fill-mode)
1886 (progn
1887 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1888 (defun auto-fill-mode (&optional arg)
1889 (interactive "P")
1890 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1891 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1892 (setq auto-fill-function 'cperl-do-auto-fill))))))
1893 (if (cperl-enable-font-lock)
1894 (if (cperl-val 'cperl-font-lock)
1895 (progn (or cperl-faces-init (cperl-init-faces))
1896 (font-lock-mode 1))))
1897 (set (make-local-variable 'facemenu-add-face-function)
1898 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1899 (and (boundp 'msb-menu-cond)
1900 (not cperl-msb-fixed)
1901 (cperl-msb-fix))
1902 (if (featurep 'easymenu)
1903 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1904 (run-mode-hooks 'cperl-mode-hook)
1905 (if cperl-hook-after-change
1906 (add-hook 'after-change-functions 'cperl-after-change-function nil t))
1907 ;; After hooks since fontification will break this
1908 (if cperl-pod-here-scan
1909 (or cperl-syntaxify-by-font-lock
1910 (progn (or cperl-faces-init (cperl-init-faces-weak))
1911 (cperl-find-pods-heres)))))
1912 \f
1913 ;; Fix for perldb - make default reasonable
1914 (defun cperl-db ()
1915 (interactive)
1916 (require 'gud)
1917 (perldb (read-from-minibuffer "Run perldb (like this): "
1918 (if (consp gud-perldb-history)
1919 (car gud-perldb-history)
1920 (concat "perl " ;;(file-name-nondirectory
1921 ;; I have problems
1922 ;; in OS/2
1923 ;; otherwise
1924 (buffer-file-name)))
1925 nil nil
1926 '(gud-perldb-history . 1))))
1927 \f
1928 (defun cperl-msb-fix ()
1929 ;; Adds perl files to msb menu, supposes that msb is already loaded
1930 (setq cperl-msb-fixed t)
1931 (let* ((l (length msb-menu-cond))
1932 (last (nth (1- l) msb-menu-cond))
1933 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1934 (handle (1- (nth 1 last))))
1935 (setcdr precdr (list
1936 (list
1937 '(memq major-mode '(cperl-mode perl-mode))
1938 handle
1939 "Perl Files (%d)")
1940 last))))
1941 \f
1942 ;; This is used by indent-for-comment
1943 ;; to decide how much to indent a comment in CPerl code
1944 ;; based on its context. Do fallback if comment is found wrong.
1945
1946 (defvar cperl-wrong-comment)
1947 (defvar cperl-st-cfence '(14)) ; Comment-fence
1948 (defvar cperl-st-sfence '(15)) ; String-fence
1949 (defvar cperl-st-punct '(1))
1950 (defvar cperl-st-word '(2))
1951 (defvar cperl-st-bra '(4 . ?\>))
1952 (defvar cperl-st-ket '(5 . ?\<))
1953
1954
1955 (defun cperl-comment-indent () ; called at point at supposed comment
1956 (let ((p (point)) (c (current-column)) was phony)
1957 (if (and (not cperl-indent-comment-at-column-0)
1958 (looking-at "^#"))
1959 0 ; Existing comment at bol stays there.
1960 ;; Wrong comment found
1961 (save-excursion
1962 (setq was (cperl-to-comment-or-eol)
1963 phony (eq (get-text-property (point) 'syntax-table)
1964 cperl-st-cfence))
1965 (if phony
1966 (progn ; Too naive???
1967 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1968 (if (eq (preceding-char) ?\#)
1969 (forward-char -1))
1970 (setq was nil)))
1971 (if (= (point) p) ; Our caller found a correct place
1972 (progn
1973 (skip-chars-backward " \t")
1974 (setq was (current-column))
1975 (if (eq was 0)
1976 comment-column
1977 (max (1+ was) ; Else indent at comment column
1978 comment-column)))
1979 ;; No, the caller found a random place; we need to edit ourselves
1980 (if was nil
1981 (insert comment-start)
1982 (backward-char (length comment-start)))
1983 (setq cperl-wrong-comment t)
1984 (cperl-make-indent comment-column 1) ; Indent min 1
1985 c)))))
1986
1987 ;;;(defun cperl-comment-indent-fallback ()
1988 ;;; "Is called if the standard comment-search procedure fails.
1989 ;;;Point is at start of real comment."
1990 ;;; (let ((c (current-column)) target cnt prevc)
1991 ;;; (if (= c comment-column) nil
1992 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1993 ;;; (setq target (max (1+ (setq prevc
1994 ;;; (current-column))) ; Else indent at comment column
1995 ;;; comment-column))
1996 ;;; (if (= c comment-column) nil
1997 ;;; (delete-backward-char cnt)
1998 ;;; (while (< prevc target)
1999 ;;; (insert "\t")
2000 ;;; (setq prevc (current-column)))
2001 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
2002 ;;; (while (< prevc target)
2003 ;;; (insert " ")
2004 ;;; (setq prevc (current-column)))))))
2005
2006 (defun cperl-indent-for-comment ()
2007 "Substitute for `indent-for-comment' in CPerl."
2008 (interactive)
2009 (let (cperl-wrong-comment)
2010 (indent-for-comment)
2011 (if cperl-wrong-comment ; set by `cperl-comment-indent'
2012 (progn (cperl-to-comment-or-eol)
2013 (forward-char (length comment-start))))))
2014
2015 (defun cperl-comment-region (b e arg)
2016 "Comment or uncomment each line in the region in CPerl mode.
2017 See `comment-region'."
2018 (interactive "r\np")
2019 (let ((comment-start "#"))
2020 (comment-region b e arg)))
2021
2022 (defun cperl-uncomment-region (b e arg)
2023 "Uncomment or comment each line in the region in CPerl mode.
2024 See `comment-region'."
2025 (interactive "r\np")
2026 (let ((comment-start "#"))
2027 (comment-region b e (- arg))))
2028
2029 (defvar cperl-brace-recursing nil)
2030
2031 (defun cperl-electric-brace (arg &optional only-before)
2032 "Insert character and correct line's indentation.
2033 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2034 place (even in empty line), but not after. If after \")\" and the inserted
2035 char is \"{\", insert extra newline before only if
2036 `cperl-extra-newline-before-brace'."
2037 (interactive "P")
2038 (let (insertpos
2039 (other-end (if (and cperl-electric-parens-mark
2040 (cperl-mark-active)
2041 (< (mark) (point)))
2042 (mark)
2043 nil)))
2044 (if (and other-end
2045 (not cperl-brace-recursing)
2046 (cperl-val 'cperl-electric-parens)
2047 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2048 ;; Need to insert a matching pair
2049 (progn
2050 (save-excursion
2051 (setq insertpos (point-marker))
2052 (goto-char other-end)
2053 (setq last-command-event ?\{)
2054 (cperl-electric-lbrace arg insertpos))
2055 (forward-char 1))
2056 ;; Check whether we close something "usual" with `}'
2057 (if (and (eq last-command-event ?\})
2058 (not
2059 (condition-case nil
2060 (save-excursion
2061 (up-list (- (prefix-numeric-value arg)))
2062 ;;(cperl-after-block-p (point-min))
2063 (or (cperl-after-expr-p nil "{;)")
2064 ;; after sub, else, continue
2065 (cperl-after-block-p nil 'pre)))
2066 (error nil))))
2067 ;; Just insert the guy
2068 (self-insert-command (prefix-numeric-value arg))
2069 (if (and (not arg) ; No args, end (of empty line or auto)
2070 (eolp)
2071 (or (and (null only-before)
2072 (save-excursion
2073 (skip-chars-backward " \t")
2074 (bolp)))
2075 (and (eq last-command-event ?\{) ; Do not insert newline
2076 ;; if after ")" and `cperl-extra-newline-before-brace'
2077 ;; is nil, do not insert extra newline.
2078 (not cperl-extra-newline-before-brace)
2079 (save-excursion
2080 (skip-chars-backward " \t")
2081 (eq (preceding-char) ?\))))
2082 (if cperl-auto-newline
2083 (progn (cperl-indent-line) (newline) t) nil)))
2084 (progn
2085 (self-insert-command (prefix-numeric-value arg))
2086 (cperl-indent-line)
2087 (if cperl-auto-newline
2088 (setq insertpos (1- (point))))
2089 (if (and cperl-auto-newline (null only-before))
2090 (progn
2091 (newline)
2092 (cperl-indent-line)))
2093 (save-excursion
2094 (if insertpos (progn (goto-char insertpos)
2095 (search-forward (make-string
2096 1 last-command-event))
2097 (setq insertpos (1- (point)))))
2098 (delete-char -1))))
2099 (if insertpos
2100 (save-excursion
2101 (goto-char insertpos)
2102 (self-insert-command (prefix-numeric-value arg)))
2103 (self-insert-command (prefix-numeric-value arg)))))))
2104
2105 (defun cperl-electric-lbrace (arg &optional end)
2106 "Insert character, correct line's indentation, correct quoting by space."
2107 (interactive "P")
2108 (let ((cperl-brace-recursing t)
2109 (cperl-auto-newline cperl-auto-newline)
2110 (other-end (or end
2111 (if (and cperl-electric-parens-mark
2112 (cperl-mark-active)
2113 (> (mark) (point)))
2114 (save-excursion
2115 (goto-char (mark))
2116 (point-marker))
2117 nil)))
2118 pos after)
2119 (and (cperl-val 'cperl-electric-lbrace-space)
2120 (eq (preceding-char) ?$)
2121 (save-excursion
2122 (skip-chars-backward "$")
2123 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2124 (insert ?\s))
2125 ;; Check whether we are in comment
2126 (if (and
2127 (save-excursion
2128 (beginning-of-line)
2129 (not (looking-at "[ \t]*#")))
2130 (cperl-after-expr-p nil "{;)"))
2131 nil
2132 (setq cperl-auto-newline nil))
2133 (cperl-electric-brace arg)
2134 (and (cperl-val 'cperl-electric-parens)
2135 (eq last-command-event ?{)
2136 (memq last-command-event
2137 (append cperl-electric-parens-string nil))
2138 (or (if other-end (goto-char (marker-position other-end)))
2139 t)
2140 (setq last-command-event ?} pos (point))
2141 (progn (cperl-electric-brace arg t)
2142 (goto-char pos)))))
2143
2144 (defun cperl-electric-paren (arg)
2145 "Insert an opening parenthesis or a matching pair of parentheses.
2146 See `cperl-electric-parens'."
2147 (interactive "P")
2148 (let ((beg (save-excursion (beginning-of-line) (point)))
2149 (other-end (if (and cperl-electric-parens-mark
2150 (cperl-mark-active)
2151 (> (mark) (point)))
2152 (save-excursion
2153 (goto-char (mark))
2154 (point-marker))
2155 nil)))
2156 (if (and (cperl-val 'cperl-electric-parens)
2157 (memq last-command-event
2158 (append cperl-electric-parens-string nil))
2159 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2160 ;;(not (save-excursion (search-backward "#" beg t)))
2161 (if (eq last-command-event ?<)
2162 (progn
2163 ;; This code is too electric, see Bug#3943.
2164 ;; (and abbrev-mode ; later it is too late, may be after `for'
2165 ;; (expand-abbrev))
2166 (cperl-after-expr-p nil "{;(,:="))
2167 1))
2168 (progn
2169 (self-insert-command (prefix-numeric-value arg))
2170 (if other-end (goto-char (marker-position other-end)))
2171 (insert (make-string
2172 (prefix-numeric-value arg)
2173 (cdr (assoc last-command-event '((?{ .?})
2174 (?[ . ?])
2175 (?( . ?))
2176 (?< . ?>))))))
2177 (forward-char (- (prefix-numeric-value arg))))
2178 (self-insert-command (prefix-numeric-value arg)))))
2179
2180 (defun cperl-electric-rparen (arg)
2181 "Insert a matching pair of parentheses if marking is active.
2182 If not, or if we are not at the end of marking range, would self-insert.
2183 Affected by `cperl-electric-parens'."
2184 (interactive "P")
2185 (let ((beg (save-excursion (beginning-of-line) (point)))
2186 (other-end (if (and cperl-electric-parens-mark
2187 (cperl-val 'cperl-electric-parens)
2188 (memq last-command-event
2189 (append cperl-electric-parens-string nil))
2190 (cperl-mark-active)
2191 (< (mark) (point)))
2192 (mark)
2193 nil))
2194 p)
2195 (if (and other-end
2196 (cperl-val 'cperl-electric-parens)
2197 (memq last-command-event '( ?\) ?\] ?\} ?\> ))
2198 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2199 ;;(not (save-excursion (search-backward "#" beg t)))
2200 )
2201 (progn
2202 (self-insert-command (prefix-numeric-value arg))
2203 (setq p (point))
2204 (if other-end (goto-char other-end))
2205 (insert (make-string
2206 (prefix-numeric-value arg)
2207 (cdr (assoc last-command-event '((?\} . ?\{)
2208 (?\] . ?\[)
2209 (?\) . ?\()
2210 (?\> . ?\<))))))
2211 (goto-char (1+ p)))
2212 (self-insert-command (prefix-numeric-value arg)))))
2213
2214 (defun cperl-electric-keyword ()
2215 "Insert a construction appropriate after a keyword.
2216 Help message may be switched off by setting `cperl-message-electric-keyword'
2217 to nil."
2218 (let ((beg (save-excursion (beginning-of-line) (point)))
2219 (dollar (and (eq last-command-event ?$)
2220 (eq this-command 'self-insert-command)))
2221 (delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2222 (memq this-command '(self-insert-command newline))))
2223 my do)
2224 (and (save-excursion
2225 (condition-case nil
2226 (progn
2227 (backward-sexp 1)
2228 (setq do (looking-at "do\\>")))
2229 (error nil))
2230 (cperl-after-expr-p nil "{;:"))
2231 (save-excursion
2232 (not
2233 (re-search-backward
2234 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2235 beg t)))
2236 (save-excursion (or (not (re-search-backward "^=" nil t))
2237 (or
2238 (looking-at "=cut")
2239 (and cperl-use-syntax-table-text-property
2240 (not (eq (get-text-property (point)
2241 'syntax-type)
2242 'pod))))))
2243 (save-excursion (forward-sexp -1)
2244 (not (memq (following-char) (append "$@%&*" nil))))
2245 (progn
2246 (and (eq (preceding-char) ?y)
2247 (progn ; "foreachmy"
2248 (forward-char -2)
2249 (insert " ")
2250 (forward-char 2)
2251 (setq my t dollar t
2252 delete
2253 (memq this-command '(self-insert-command newline)))))
2254 (and dollar (insert " $"))
2255 (cperl-indent-line)
2256 ;;(insert " () {\n}")
2257 (cond
2258 (cperl-extra-newline-before-brace
2259 (insert (if do "\n" " ()\n"))
2260 (insert "{")
2261 (cperl-indent-line)
2262 (insert "\n")
2263 (cperl-indent-line)
2264 (insert "\n}")
2265 (and do (insert " while ();")))
2266 (t
2267 (insert (if do " {\n} while ();" " () {\n}"))))
2268 (or (looking-at "[ \t]\\|$") (insert " "))
2269 (cperl-indent-line)
2270 (if dollar (progn (search-backward "$")
2271 (if my
2272 (forward-char 1)
2273 (delete-char 1)))
2274 (search-backward ")")
2275 (if (eq last-command-event ?\()
2276 (progn ; Avoid "if (())"
2277 (delete-backward-char 1)
2278 (delete-backward-char -1))))
2279 (if delete
2280 (cperl-putback-char cperl-del-back-ch))
2281 (if cperl-message-electric-keyword
2282 (message "Precede char by C-q to avoid expansion"))))))
2283
2284 (defun cperl-ensure-newlines (n &optional pos)
2285 "Make sure there are N newlines after the point."
2286 (or pos (setq pos (point)))
2287 (if (looking-at "\n")
2288 (forward-char 1)
2289 (insert "\n"))
2290 (if (> n 1)
2291 (cperl-ensure-newlines (1- n) pos)
2292 (goto-char pos)))
2293
2294 (defun cperl-electric-pod ()
2295 "Insert a POD chunk appropriate after a =POD directive."
2296 (let ((delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2297 (memq this-command '(self-insert-command newline))))
2298 head1 notlast name p really-delete over)
2299 (and (save-excursion
2300 (forward-word -1)
2301 (and
2302 (eq (preceding-char) ?=)
2303 (progn
2304 (setq head1 (looking-at "head1\\>[ \t]*$"))
2305 (setq over (and (looking-at "over\\>[ \t]*$")
2306 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2307 (forward-char -1)
2308 (bolp))
2309 (or
2310 (get-text-property (point) 'in-pod)
2311 (cperl-after-expr-p nil "{;:")
2312 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2313 (not (looking-at "\n*=cut"))
2314 (or (not cperl-use-syntax-table-text-property)
2315 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2316 (progn
2317 (save-excursion
2318 (setq notlast (re-search-forward "^\n=" nil t)))
2319 (or notlast
2320 (progn
2321 (insert "\n\n=cut")
2322 (cperl-ensure-newlines 2)
2323 (forward-word -2)
2324 (if (and head1
2325 (not
2326 (save-excursion
2327 (forward-char -1)
2328 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2329 nil t)))) ; Only one
2330 (progn
2331 (forward-word 1)
2332 (setq name (file-name-sans-extension
2333 (file-name-nondirectory (buffer-file-name)))
2334 p (point))
2335 (insert " NAME\n\n" name
2336 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2337 "=head1 DESCRIPTION")
2338 (cperl-ensure-newlines 4)
2339 (goto-char p)
2340 (forward-word 2)
2341 (end-of-line)
2342 (setq really-delete t))
2343 (forward-word 1))))
2344 (if over
2345 (progn
2346 (setq p (point))
2347 (insert "\n\n=item \n\n\n\n"
2348 "=back")
2349 (cperl-ensure-newlines 2)
2350 (goto-char p)
2351 (forward-word 1)
2352 (end-of-line)
2353 (setq really-delete t)))
2354 (if (and delete really-delete)
2355 (cperl-putback-char cperl-del-back-ch))))))
2356
2357 (defun cperl-electric-else ()
2358 "Insert a construction appropriate after a keyword.
2359 Help message may be switched off by setting `cperl-message-electric-keyword'
2360 to nil."
2361 (let ((beg (save-excursion (beginning-of-line) (point))))
2362 (and (save-excursion
2363 (backward-sexp 1)
2364 (cperl-after-expr-p nil "{;:"))
2365 (save-excursion
2366 (not
2367 (re-search-backward
2368 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2369 beg t)))
2370 (save-excursion (or (not (re-search-backward "^=" nil t))
2371 (looking-at "=cut")
2372 (and cperl-use-syntax-table-text-property
2373 (not (eq (get-text-property (point)
2374 'syntax-type)
2375 'pod)))))
2376 (progn
2377 (cperl-indent-line)
2378 ;;(insert " {\n\n}")
2379 (cond
2380 (cperl-extra-newline-before-brace
2381 (insert "\n")
2382 (insert "{")
2383 (cperl-indent-line)
2384 (insert "\n\n}"))
2385 (t
2386 (insert " {\n\n}")))
2387 (or (looking-at "[ \t]\\|$") (insert " "))
2388 (cperl-indent-line)
2389 (forward-line -1)
2390 (cperl-indent-line)
2391 (cperl-putback-char cperl-del-back-ch)
2392 (setq this-command 'cperl-electric-else)
2393 (if cperl-message-electric-keyword
2394 (message "Precede char by C-q to avoid expansion"))))))
2395
2396 (defun cperl-linefeed ()
2397 "Go to end of line, open a new line and indent appropriately.
2398 If in POD, insert appropriate lines."
2399 (interactive)
2400 (let ((beg (save-excursion (beginning-of-line) (point)))
2401 (end (save-excursion (end-of-line) (point)))
2402 (pos (point)) start over cut res)
2403 (if (and ; Check if we need to split:
2404 ; i.e., on a boundary and inside "{...}"
2405 (save-excursion (cperl-to-comment-or-eol)
2406 (>= (point) pos)) ; Not in a comment
2407 (or (save-excursion
2408 (skip-chars-backward " \t" beg)
2409 (forward-char -1)
2410 (looking-at "[;{]")) ; After { or ; + spaces
2411 (looking-at "[ \t]*}") ; Before }
2412 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2413 (save-excursion
2414 (and
2415 (eq (car (parse-partial-sexp pos end -1)) -1)
2416 ; Leave the level of parens
2417 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2418 ; Are at end
2419 (cperl-after-block-p (point-min))
2420 (progn
2421 (backward-sexp 1)
2422 (setq start (point-marker))
2423 (<= start pos))))) ; Redundant? Are after the
2424 ; start of parens group.
2425 (progn
2426 (skip-chars-backward " \t")
2427 (or (memq (preceding-char) (append ";{" nil))
2428 (insert ";"))
2429 (insert "\n")
2430 (forward-line -1)
2431 (cperl-indent-line)
2432 (goto-char start)
2433 (or (looking-at "{[ \t]*$") ; If there is a statement
2434 ; before, move it to separate line
2435 (progn
2436 (forward-char 1)
2437 (insert "\n")
2438 (cperl-indent-line)))
2439 (forward-line 1) ; We are on the target line
2440 (cperl-indent-line)
2441 (beginning-of-line)
2442 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2443 ; after, move it to separate line
2444 (progn
2445 (end-of-line)
2446 (search-backward "}" beg)
2447 (skip-chars-backward " \t")
2448 (or (memq (preceding-char) (append ";{" nil))
2449 (insert ";"))
2450 (insert "\n")
2451 (cperl-indent-line)
2452 (forward-line -1)))
2453 (forward-line -1) ; We are on the line before target
2454 (end-of-line)
2455 (newline-and-indent))
2456 (end-of-line) ; else - no splitting
2457 (cond
2458 ((and (looking-at "\n[ \t]*{$")
2459 (save-excursion
2460 (skip-chars-backward " \t")
2461 (eq (preceding-char) ?\)))) ; Probably if () {} group
2462 ; with an extra newline.
2463 (forward-line 2)
2464 (cperl-indent-line))
2465 ((save-excursion ; In POD header
2466 (forward-paragraph -1)
2467 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2468 ;; We are after \n now, so look for the rest
2469 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2470 (progn
2471 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2472 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2473 t)))
2474 (if (and over
2475 (progn
2476 (forward-paragraph -1)
2477 (forward-word 1)
2478 (setq pos (point))
2479 (setq cut (buffer-substring (point)
2480 (save-excursion
2481 (end-of-line)
2482 (point))))
2483 (delete-char (- (save-excursion (end-of-line) (point))
2484 (point)))
2485 (setq res (expand-abbrev))
2486 (save-excursion
2487 (goto-char pos)
2488 (insert cut))
2489 res))
2490 nil
2491 (cperl-ensure-newlines (if cut 2 4))
2492 (forward-line 2)))
2493 ((get-text-property (point) 'in-pod) ; In POD section
2494 (cperl-ensure-newlines 4)
2495 (forward-line 2))
2496 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2497 (forward-line 1)
2498 (cperl-indent-line))
2499 (t
2500 (newline-and-indent))))))
2501
2502 (defun cperl-electric-semi (arg)
2503 "Insert character and correct line's indentation."
2504 (interactive "P")
2505 (if cperl-auto-newline
2506 (cperl-electric-terminator arg)
2507 (self-insert-command (prefix-numeric-value arg))
2508 (if cperl-autoindent-on-semi
2509 (cperl-indent-line))))
2510
2511 (defun cperl-electric-terminator (arg)
2512 "Insert character and correct line's indentation."
2513 (interactive "P")
2514 (let ((end (point))
2515 (auto (and cperl-auto-newline
2516 (or (not (eq last-command-event ?:))
2517 cperl-auto-newline-after-colon)))
2518 insertpos)
2519 (if (and ;;(not arg)
2520 (eolp)
2521 (not (save-excursion
2522 (beginning-of-line)
2523 (skip-chars-forward " \t")
2524 (or
2525 ;; Ignore in comment lines
2526 (= (following-char) ?#)
2527 ;; Colon is special only after a label
2528 ;; So quickly rule out most other uses of colon
2529 ;; and do no indentation for them.
2530 (and (eq last-command-event ?:)
2531 (save-excursion
2532 (forward-word 1)
2533 (skip-chars-forward " \t")
2534 (and (< (point) end)
2535 (progn (goto-char (- end 1))
2536 (not (looking-at ":"))))))
2537 (progn
2538 (beginning-of-defun)
2539 (let ((pps (parse-partial-sexp (point) end)))
2540 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2541 (progn
2542 (self-insert-command (prefix-numeric-value arg))
2543 ;;(forward-char -1)
2544 (if auto (setq insertpos (point-marker)))
2545 ;;(forward-char 1)
2546 (cperl-indent-line)
2547 (if auto
2548 (progn
2549 (newline)
2550 (cperl-indent-line)))
2551 (save-excursion
2552 (if insertpos (goto-char (1- (marker-position insertpos)))
2553 (forward-char -1))
2554 (delete-char 1))))
2555 (if insertpos
2556 (save-excursion
2557 (goto-char insertpos)
2558 (self-insert-command (prefix-numeric-value arg)))
2559 (self-insert-command (prefix-numeric-value arg)))))
2560
2561 (defun cperl-electric-backspace (arg)
2562 "Backspace, or remove whitespace around the point inserted by an electric key.
2563 Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2564 (interactive "p")
2565 (if (and cperl-auto-newline
2566 (memq last-command '(cperl-electric-semi
2567 cperl-electric-terminator
2568 cperl-electric-lbrace))
2569 (memq (preceding-char) '(?\s ?\t ?\n)))
2570 (let (p)
2571 (if (eq last-command 'cperl-electric-lbrace)
2572 (skip-chars-forward " \t\n"))
2573 (setq p (point))
2574 (skip-chars-backward " \t\n")
2575 (delete-region (point) p))
2576 (and (eq last-command 'cperl-electric-else)
2577 ;; We are removing the whitespace *inside* cperl-electric-else
2578 (setq this-command 'cperl-electric-else-really))
2579 (if (and cperl-auto-newline
2580 (eq last-command 'cperl-electric-else-really)
2581 (memq (preceding-char) '(?\s ?\t ?\n)))
2582 (let (p)
2583 (skip-chars-forward " \t\n")
2584 (setq p (point))
2585 (skip-chars-backward " \t\n")
2586 (delete-region (point) p))
2587 (if cperl-electric-backspace-untabify
2588 (backward-delete-char-untabify arg)
2589 (delete-backward-char arg)))))
2590
2591 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2592
2593 (defun cperl-inside-parens-p () ;; NOT USED????
2594 (condition-case ()
2595 (save-excursion
2596 (save-restriction
2597 (narrow-to-region (point)
2598 (progn (beginning-of-defun) (point)))
2599 (goto-char (point-max))
2600 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2601 (error nil)))
2602 \f
2603 (defun cperl-indent-command (&optional whole-exp)
2604 "Indent current line as Perl code, or in some cases insert a tab character.
2605 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2606 line. Otherwise, indent the current line only if point is at the left margin
2607 or in the line's indentation; otherwise insert a tab.
2608
2609 A numeric argument, regardless of its value,
2610 means indent rigidly all the lines of the expression starting after point
2611 so that this line becomes properly indented.
2612 The relative indentation among the lines of the expression are preserved."
2613 (interactive "P")
2614 (cperl-update-syntaxification (point) (point))
2615 (if whole-exp
2616 ;; If arg, always indent this line as Perl
2617 ;; and shift remaining lines of expression the same amount.
2618 (let ((shift-amt (cperl-indent-line))
2619 beg end)
2620 (save-excursion
2621 (if cperl-tab-always-indent
2622 (beginning-of-line))
2623 (setq beg (point))
2624 (forward-sexp 1)
2625 (setq end (point))
2626 (goto-char beg)
2627 (forward-line 1)
2628 (setq beg (point)))
2629 (if (and shift-amt (> end beg))
2630 (indent-code-rigidly beg end shift-amt "#")))
2631 (if (and (not cperl-tab-always-indent)
2632 (save-excursion
2633 (skip-chars-backward " \t")
2634 (not (bolp))))
2635 (insert-tab)
2636 (cperl-indent-line))))
2637
2638 (defun cperl-indent-line (&optional parse-data)
2639 "Indent current line as Perl code.
2640 Return the amount the indentation changed by."
2641 (let ((case-fold-search nil)
2642 (pos (- (point-max) (point)))
2643 indent i beg shift-amt)
2644 (setq indent (cperl-calculate-indent parse-data)
2645 i indent)
2646 (beginning-of-line)
2647 (setq beg (point))
2648 (cond ((or (eq indent nil) (eq indent t))
2649 (setq indent (current-indentation) i nil))
2650 ;;((eq indent t) ; Never?
2651 ;; (setq indent (cperl-calculate-indent-within-comment)))
2652 ;;((looking-at "[ \t]*#")
2653 ;; (setq indent 0))
2654 (t
2655 (skip-chars-forward " \t")
2656 (if (listp indent) (setq indent (car indent)))
2657 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2658 (not (looking-at "[smy]:\\|tr:")))
2659 (and (> indent 0)
2660 (setq indent (max cperl-min-label-indent
2661 (+ indent cperl-label-offset)))))
2662 ((= (following-char) ?})
2663 (setq indent (- indent cperl-indent-level)))
2664 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2665 (setq indent (+ indent cperl-close-paren-offset)))
2666 ((= (following-char) ?{)
2667 (setq indent (+ indent cperl-brace-offset))))))
2668 (skip-chars-forward " \t")
2669 (setq shift-amt (and i (- indent (current-column))))
2670 (if (or (not shift-amt)
2671 (zerop shift-amt))
2672 (if (> (- (point-max) pos) (point))
2673 (goto-char (- (point-max) pos)))
2674 ;;;(delete-region beg (point))
2675 ;;;(indent-to indent)
2676 (cperl-make-indent indent)
2677 ;; If initial point was within line's indentation,
2678 ;; position after the indentation. Else stay at same point in text.
2679 (if (> (- (point-max) pos) (point))
2680 (goto-char (- (point-max) pos))))
2681 shift-amt))
2682
2683 (defun cperl-after-label ()
2684 ;; Returns true if the point is after label. Does not do save-excursion.
2685 (and (eq (preceding-char) ?:)
2686 (memq (char-syntax (char-after (- (point) 2)))
2687 '(?w ?_))
2688 (progn
2689 (backward-sexp)
2690 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2691
2692 (defun cperl-get-state (&optional parse-start start-state)
2693 ;; returns list (START STATE DEPTH PRESTART),
2694 ;; START is a good place to start parsing, or equal to
2695 ;; PARSE-START if preset,
2696 ;; STATE is what is returned by `parse-partial-sexp'.
2697 ;; DEPTH is true is we are immediately after end of block
2698 ;; which contains START.
2699 ;; PRESTART is the position basing on which START was found.
2700 (save-excursion
2701 (let ((start-point (point)) depth state start prestart)
2702 (if (and parse-start
2703 (<= parse-start start-point))
2704 (goto-char parse-start)
2705 (beginning-of-defun)
2706 (setq start-state nil))
2707 (setq prestart (point))
2708 (if start-state nil
2709 ;; Try to go out, if sub is not on the outermost level
2710 (while (< (point) start-point)
2711 (setq start (point) parse-start start depth nil
2712 state (parse-partial-sexp start start-point -1))
2713 (if (> (car state) -1) nil
2714 ;; The current line could start like }}}, so the indentation
2715 ;; corresponds to a different level than what we reached
2716 (setq depth t)
2717 (beginning-of-line 2))) ; Go to the next line.
2718 (if start (goto-char start))) ; Not at the start of file
2719 (setq start (point))
2720 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2721 (list start state depth prestart))))
2722
2723 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2724
2725 (defun cperl-beginning-of-property (p prop &optional lim)
2726 "Given that P has a property PROP, find where the property starts.
2727 Will not look before LIM."
2728 ;;; XXXX What to do at point-max???
2729 (or (previous-single-property-change (cperl-1+ p) prop lim)
2730 (point-min))
2731 ;;; (cond ((eq p (point-min))
2732 ;;; p)
2733 ;;; ((and lim (<= p lim))
2734 ;;; p)
2735 ;;; ((not (get-text-property (1- p) prop))
2736 ;;; p)
2737 ;;; (t (or (previous-single-property-change p look-prop lim)
2738 ;;; (point-min))))
2739 )
2740
2741 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2742 ;; the sniffer logic to understand what the current line MEANS.
2743 (cperl-update-syntaxification (point) (point))
2744 (let ((res (get-text-property (point) 'syntax-type)))
2745 (save-excursion
2746 (cond
2747 ((and (memq res '(pod here-doc here-doc-delim format))
2748 (not (get-text-property (point) 'indentable)))
2749 (vector res))
2750 ;; before start of POD - whitespace found since do not have 'pod!
2751 ((looking-at "[ \t]*\n=")
2752 (error "Spaces before POD section!"))
2753 ((and (not cperl-indent-left-aligned-comments)
2754 (looking-at "^#"))
2755 [comment-special:at-beginning-of-line])
2756 ((get-text-property (point) 'in-pod)
2757 [in-pod])
2758 (t
2759 (beginning-of-line)
2760 (let* ((indent-point (point))
2761 (char-after-pos (save-excursion
2762 (skip-chars-forward " \t")
2763 (point)))
2764 (char-after (char-after char-after-pos))
2765 (pre-indent-point (point))
2766 p prop look-prop is-block delim)
2767 (save-excursion ; Know we are not in POD, find appropriate pos before
2768 (cperl-backward-to-noncomment nil)
2769 (setq p (max (point-min) (1- (point)))
2770 prop (get-text-property p 'syntax-type)
2771 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2772 'syntax-type))
2773 (if (memq prop '(pod here-doc format here-doc-delim))
2774 (progn
2775 (goto-char (cperl-beginning-of-property p look-prop))
2776 (beginning-of-line)
2777 (setq pre-indent-point (point)))))
2778 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2779 (let* ((case-fold-search nil)
2780 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2781 (start (or (nth 2 parse-data) ; last complete sexp terminated
2782 (nth 0 s-s))) ; Good place to start parsing
2783 (state (nth 1 s-s))
2784 (containing-sexp (car (cdr state)))
2785 old-indent)
2786 (if (and
2787 ;;containing-sexp ;; We are buggy at toplevel :-(
2788 parse-data)
2789 (progn
2790 (setcar parse-data pre-indent-point)
2791 (setcar (cdr parse-data) state)
2792 (or (nth 2 parse-data)
2793 (setcar (cddr parse-data) start))
2794 ;; Before this point: end of statement
2795 (setq old-indent (nth 3 parse-data))))
2796 (cond ((get-text-property (point) 'indentable)
2797 ;; indent to "after" the surrounding open
2798 ;; (same offset as `cperl-beautify-regexp-piece'),
2799 ;; skip blanks if we do not close the expression.
2800 (setq delim ; We do not close the expression
2801 (get-text-property
2802 (cperl-1+ char-after-pos) 'indentable)
2803 p (1+ (cperl-beginning-of-property
2804 (point) 'indentable))
2805 is-block ; misused for: preceeding line in REx
2806 (save-excursion ; Find preceeding line
2807 (cperl-backward-to-noncomment p)
2808 (beginning-of-line)
2809 (if (<= (point) p)
2810 (progn ; get indent from the first line
2811 (goto-char p)
2812 (skip-chars-forward " \t")
2813 (if (memq (char-after (point))
2814 (append "#\n" nil))
2815 nil ; Can't use intentation of this line...
2816 (point)))
2817 (skip-chars-forward " \t")
2818 (point)))
2819 prop (parse-partial-sexp p char-after-pos))
2820 (cond ((not delim) ; End the REx, ignore is-block
2821 (vector 'indentable 'terminator p is-block))
2822 (is-block ; Indent w.r.t. preceeding line
2823 (vector 'indentable 'cont-line char-after-pos
2824 is-block char-after p))
2825 (t ; No preceeding line...
2826 (vector 'indentable 'first-line p))))
2827 ((get-text-property char-after-pos 'REx-part2)
2828 (vector 'REx-part2 (point)))
2829 ((nth 4 state)
2830 [comment])
2831 ((nth 3 state)
2832 [string])
2833 ;; XXXX Do we need to special-case this?
2834 ((null containing-sexp)
2835 ;; Line is at top level. May be data or function definition,
2836 ;; or may be function argument declaration.
2837 ;; Indent like the previous top level line
2838 ;; unless that ends in a closeparen without semicolon,
2839 ;; in which case this line is the first argument decl.
2840 (skip-chars-forward " \t")
2841 (cperl-backward-to-noncomment (or old-indent (point-min)))
2842 (setq state
2843 (or (bobp)
2844 (eq (point) old-indent) ; old-indent was at comment
2845 (eq (preceding-char) ?\;)
2846 ;; Had ?\) too
2847 (and (eq (preceding-char) ?\})
2848 (cperl-after-block-and-statement-beg
2849 (point-min))) ; Was start - too close
2850 (memq char-after (append ")]}" nil))
2851 (and (eq (preceding-char) ?\:) ; label
2852 (progn
2853 (forward-sexp -1)
2854 (skip-chars-backward " \t")
2855 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2856 (get-text-property (point) 'first-format-line)))
2857
2858 ;; Look at previous line that's at column 0
2859 ;; to determine whether we are in top-level decls
2860 ;; or function's arg decls. Set basic-indent accordingly.
2861 ;; Now add a little if this is a continuation line.
2862 (and state
2863 parse-data
2864 (not (eq char-after ?\C-j))
2865 (setcdr (cddr parse-data)
2866 (list pre-indent-point)))
2867 (vector 'toplevel start char-after state (nth 2 s-s)))
2868 ((not
2869 (or (setq is-block
2870 (and (setq delim (= (char-after containing-sexp) ?{))
2871 (save-excursion ; Is it a hash?
2872 (goto-char containing-sexp)
2873 (cperl-block-p))))
2874 cperl-indent-parens-as-block))
2875 ;; group is an expression, not a block:
2876 ;; indent to just after the surrounding open parens,
2877 ;; skip blanks if we do not close the expression.
2878 (goto-char (1+ containing-sexp))
2879 (or (memq char-after
2880 (append (if delim "}" ")]}") nil))
2881 (looking-at "[ \t]*\\(#\\|$\\)")
2882 (skip-chars-forward " \t"))
2883 (setq old-indent (point)) ; delim=is-brace
2884 (vector 'in-parens char-after (point) delim containing-sexp))
2885 (t
2886 ;; Statement level. Is it a continuation or a new statement?
2887 ;; Find previous non-comment character.
2888 (goto-char pre-indent-point) ; Skip one level of POD/etc
2889 (cperl-backward-to-noncomment containing-sexp)
2890 ;; Back up over label lines, since they don't
2891 ;; affect whether our line is a continuation.
2892 ;; (Had \, too)
2893 (while;;(or (eq (preceding-char) ?\,)
2894 (and (eq (preceding-char) ?:)
2895 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2896 (memq (char-syntax (char-after (- (point) 2)))
2897 '(?w ?_))))
2898 ;;)
2899 ;; This is always FALSE?
2900 (if (eq (preceding-char) ?\,)
2901 ;; Will go to beginning of line, essentially.
2902 ;; Will ignore embedded sexpr XXXX.
2903 (cperl-backward-to-start-of-continued-exp containing-sexp))
2904 (beginning-of-line)
2905 (cperl-backward-to-noncomment containing-sexp))
2906 ;; Now we get non-label preceeding the indent point
2907 (if (not (or (eq (1- (point)) containing-sexp)
2908 (memq (preceding-char)
2909 (append (if is-block " ;{" " ,;{") '(nil)))
2910 (and (eq (preceding-char) ?\})
2911 (cperl-after-block-and-statement-beg
2912 containing-sexp))
2913 (get-text-property (point) 'first-format-line)))
2914 ;; This line is continuation of preceding line's statement;
2915 ;; indent `cperl-continued-statement-offset' more than the
2916 ;; previous line of the statement.
2917 ;;
2918 ;; There might be a label on this line, just
2919 ;; consider it bad style and ignore it.
2920 (progn
2921 (cperl-backward-to-start-of-continued-exp containing-sexp)
2922 (vector 'continuation (point) char-after is-block delim))
2923 ;; This line starts a new statement.
2924 ;; Position following last unclosed open brace
2925 (goto-char containing-sexp)
2926 ;; Is line first statement after an open-brace?
2927 (or
2928 ;; If no, find that first statement and indent like
2929 ;; it. If the first statement begins with label, do
2930 ;; not believe when the indentation of the label is too
2931 ;; small.
2932 (save-excursion
2933 (forward-char 1)
2934 (let ((colon-line-end 0))
2935 (while
2936 (progn (skip-chars-forward " \t\n")
2937 ;; s: foo : bar :x is NOT label
2938 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2939 (not (looking-at "[sym]:\\|tr:"))))
2940 ;; Skip over comments and labels following openbrace.
2941 (cond ((= (following-char) ?\#)
2942 (forward-line 1))
2943 ((= (following-char) ?\=)
2944 (goto-char
2945 (or (next-single-property-change (point) 'in-pod)
2946 (point-max)))) ; do not loop if no syntaxification
2947 ;; label:
2948 (t
2949 (save-excursion (end-of-line)
2950 (setq colon-line-end (point)))
2951 (search-forward ":"))))
2952 ;; We are at beginning of code (NOT label or comment)
2953 ;; First, the following code counts
2954 ;; if it is before the line we want to indent.
2955 (and (< (point) indent-point)
2956 (vector 'have-prev-sibling (point) colon-line-end
2957 containing-sexp))))
2958 (progn
2959 ;; If no previous statement,
2960 ;; indent it relative to line brace is on.
2961
2962 ;; For open-braces not the first thing in a line,
2963 ;; add in cperl-brace-imaginary-offset.
2964
2965 ;; If first thing on a line: ?????
2966 ;; Move back over whitespace before the openbrace.
2967 (setq ; brace first thing on a line
2968 old-indent (progn (skip-chars-backward " \t") (bolp)))
2969 ;; Should we indent w.r.t. earlier than start?
2970 ;; Move to start of control group, possibly on a different line
2971 (or cperl-indent-wrt-brace
2972 (cperl-backward-to-noncomment (point-min)))
2973 ;; If the openbrace is preceded by a parenthesized exp,
2974 ;; move to the beginning of that;
2975 (if (eq (preceding-char) ?\))
2976 (progn
2977 (forward-sexp -1)
2978 (cperl-backward-to-noncomment (point-min))))
2979 ;; In the case it starts a subroutine, indent with
2980 ;; respect to `sub', not with respect to the
2981 ;; first thing on the line, say in the case of
2982 ;; anonymous sub in a hash.
2983 (if (and;; Is it a sub in group starting on this line?
2984 (cond ((get-text-property (point) 'attrib-group)
2985 (goto-char (cperl-beginning-of-property
2986 (point) 'attrib-group)))
2987 ((eq (preceding-char) ?b)
2988 (forward-sexp -1)
2989 (looking-at "sub\\>")))
2990 (setq p (nth 1 ; start of innermost containing list
2991 (parse-partial-sexp
2992 (save-excursion (beginning-of-line)
2993 (point))
2994 (point)))))
2995 (progn
2996 (goto-char (1+ p)) ; enclosing block on the same line
2997 (skip-chars-forward " \t")
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 t (point) ; have something before...
3002 )
3003 ;;(current-column)
3004 )
3005 ;; Get initial indentation of the line we are on.
3006 ;; If line starts with label, calculate label indentation
3007 (vector 'code-start-in-block containing-sexp char-after
3008 (and delim (not is-block)) ; is a HASH
3009 old-indent ; brace first thing on a line
3010 nil (point))))))))))))))) ; nothing interesting before
3011
3012 (defvar cperl-indent-rules-alist
3013 '((pod nil) ; via `syntax-type' property
3014 (here-doc nil) ; via `syntax-type' property
3015 (here-doc-delim nil) ; via `syntax-type' property
3016 (format nil) ; via `syntax-type' property
3017 (in-pod nil) ; via `in-pod' property
3018 (comment-special:at-beginning-of-line nil)
3019 (string t)
3020 (comment nil))
3021 "Alist of indentation rules for CPerl mode.
3022 The values mean:
3023 nil: do not indent;
3024 number: add this amount of indentation.")
3025
3026 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3027 "Return appropriate indentation for current line as Perl code.
3028 In usual case returns an integer: the column to indent to.
3029 Returns nil if line starts inside a string, t if in a comment.
3030
3031 Will not correct the indentation for labels, but will correct it for braces
3032 and closing parentheses and brackets."
3033 ;; This code is still a broken architecture: in some cases we need to
3034 ;; compensate for some modifications which `cperl-indent-line' will add later
3035 (save-excursion
3036 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3037 (cond
3038 ;;((or (null i) (eq i t) (numberp i))
3039 ;; i)
3040 ((vectorp i)
3041 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3042 (cond
3043 (what (cadr what)) ; Load from table
3044 ;;
3045 ;; Indenters for regular expressions with //x and qw()
3046 ;;
3047 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3048 (goto-char (elt i 1))
3049 (condition-case nil ; Use indentation of the 1st part
3050 (forward-sexp -1))
3051 (current-column))
3052 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3053 (cond ;;; [indentable terminator start-pos is-block]
3054 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3055 (goto-char (elt i 2)) ; After opening parens
3056 (1- (current-column)))
3057 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3058 (goto-char (elt i 2))
3059 (+ (or cperl-regexp-indent-step cperl-indent-level)
3060 -1
3061 (current-column)))
3062 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3063 ;; Indent as the level after closing parens
3064 (goto-char (elt i 2)) ; indent line
3065 (skip-chars-forward " \t)") ; Skip closing parens
3066 (setq p (point))
3067 (goto-char (elt i 3)) ; previous line
3068 (skip-chars-forward " \t)") ; Skip closing parens
3069 ;; Number of parens in between:
3070 (setq p (nth 0 (parse-partial-sexp (point) p))
3071 what (elt i 4)) ; First char on current line
3072 (goto-char (elt i 3)) ; previous line
3073 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3074 (cond ((eq what ?\) )
3075 (- cperl-close-paren-offset)) ; compensate
3076 ((eq what ?\| )
3077 (- (or cperl-regexp-indent-step cperl-indent-level)))
3078 (t 0))
3079 (if (eq (following-char) ?\| )
3080 (or cperl-regexp-indent-step cperl-indent-level)
3081 0)
3082 (current-column)))
3083 (t
3084 (error "Unrecognized value of indent: %s" i))))
3085 ;;
3086 ;; Indenter for stuff at toplevel
3087 ;;
3088 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3089 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3090 (goto-char (elt i 1)) ; start = Good place to start parsing
3091 (- (current-indentation) ;
3092 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3093 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3094 ;; Look at previous line that's at column 0
3095 ;; to determine whether we are in top-level decls
3096 ;; or function's arg decls. Set basic-indent accordingly.
3097 ;; Now add a little if this is a continuation line.
3098 (if (elt i 3) ; state (XXX What is the semantic???)
3099 0
3100 cperl-continued-statement-offset)))
3101 ;;
3102 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3103 ;;
3104 ((eq 'in-parens (elt i 0))
3105 ;; in-parens char-after old-indent-point is-brace containing-sexp
3106
3107 ;; group is an expression, not a block:
3108 ;; indent to just after the surrounding open parens,
3109 ;; skip blanks if we do not close the expression.
3110 (+ (progn
3111 (goto-char (elt i 2)) ; old-indent-point
3112 (current-column))
3113 (if (and (elt i 3) ; is-brace
3114 (eq (elt i 1) ?\})) ; char-after
3115 ;; Correct indentation of trailing ?\}
3116 (+ cperl-indent-level cperl-close-paren-offset)
3117 0)))
3118 ;;
3119 ;; Indenter for continuation lines
3120 ;;
3121 ((eq 'continuation (elt i 0))
3122 ;; [continuation statement-start char-after is-block is-brace]
3123 (goto-char (elt i 1)) ; statement-start
3124 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3125 0 ; Closing parenth
3126 cperl-continued-statement-offset)
3127 (if (or (elt i 3) ; is-block
3128 (not (elt i 4)) ; is-brace
3129 (not (eq (elt i 2) ?\}))) ; char-after
3130 0
3131 ;; Now it is a hash reference
3132 (+ cperl-indent-level cperl-close-paren-offset))
3133 ;; Labels do not take :: ...
3134 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3135 (if (> (current-indentation) cperl-min-label-indent)
3136 (- (current-indentation) cperl-label-offset)
3137 ;; Do not move `parse-data', this should
3138 ;; be quick anyway (this comment comes
3139 ;; from different location):
3140 (cperl-calculate-indent))
3141 (current-column))
3142 (if (eq (elt i 2) ?\{) ; char-after
3143 cperl-continued-brace-offset 0)))
3144 ;;
3145 ;; Indenter for lines in a block which are not leading lines
3146 ;;
3147 ((eq 'have-prev-sibling (elt i 0))
3148 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3149 (goto-char (elt i 1)) ; sibling-beg
3150 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
3151 (if (> (current-indentation)
3152 cperl-min-label-indent)
3153 (- (current-indentation) cperl-label-offset)
3154 ;; Do not believe: `max' was involved in calculation of indent
3155 (+ cperl-indent-level
3156 (save-excursion
3157 (goto-char (elt i 3)) ; block-start
3158 (current-indentation))))
3159 (current-column)))
3160 ;;
3161 ;; Indenter for the first line in a block
3162 ;;
3163 ((eq 'code-start-in-block (elt i 0))
3164 ;;[code-start-in-block before-brace char-after
3165 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3166 ;; group-starts-before-start-of-sub start-of-control-group]
3167 (goto-char (elt i 1))
3168 ;; For open brace in column zero, don't let statement
3169 ;; start there too. If cperl-indent-level=0,
3170 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3171 (+ (if (and (bolp) (zerop cperl-indent-level))
3172 (+ cperl-brace-offset cperl-continued-statement-offset)
3173 cperl-indent-level)
3174 (if (and (elt i 3) ; is-a-HASH-ref
3175 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3176 (+ cperl-indent-level cperl-close-paren-offset)
3177 0)
3178 ;; Unless openbrace is the first nonwhite thing on the line,
3179 ;; add the cperl-brace-imaginary-offset.
3180 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3181 cperl-brace-imaginary-offset)
3182 (progn
3183 (goto-char (elt i 6)) ; start-of-control-group
3184 (if (elt i 5) ; group-starts-before-start-of-sub
3185 (current-column)
3186 ;; Get initial indentation of the line we are on.
3187 ;; If line starts with label, calculate label indentation
3188 (if (save-excursion
3189 (beginning-of-line)
3190 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3191 (if (> (current-indentation) cperl-min-label-indent)
3192 (- (current-indentation) cperl-label-offset)
3193 ;; Do not move `parse-data', this should
3194 ;; be quick anyway:
3195 (cperl-calculate-indent))
3196 (current-indentation))))))
3197 (t
3198 (error "Unrecognized value of indent: %s" i))))
3199 (t
3200 (error "Got strange value of indent: %s" i))))))
3201
3202 (defun cperl-calculate-indent-within-comment ()
3203 "Return the indentation amount for line, assuming that
3204 the current line is to be regarded as part of a block comment."
3205 (let (end star-start)
3206 (save-excursion
3207 (beginning-of-line)
3208 (skip-chars-forward " \t")
3209 (setq end (point))
3210 (and (= (following-char) ?#)
3211 (forward-line -1)
3212 (cperl-to-comment-or-eol)
3213 (setq end (point)))
3214 (goto-char end)
3215 (current-column))))
3216
3217
3218 (defun cperl-to-comment-or-eol ()
3219 "Go to position before comment on the current line, or to end of line.
3220 Returns true if comment is found. In POD will not move the point."
3221 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3222 ;; then looks for literal # or end-of-line.
3223 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3224 (or cperl-font-locking
3225 (cperl-update-syntaxification lim lim))
3226 (beginning-of-line)
3227 (if (setq pr (get-text-property (point) 'syntax-type))
3228 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3229 (if (or (eq pr 'pod)
3230 (if (or (not e) (> e lim)) ; deep inside a group
3231 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3232 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3233 ;; Else - need to do it the hard way
3234 (and (and e (<= e lim))
3235 (goto-char e))
3236 (while (not stop-in)
3237 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3238 ; stop at comment
3239 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3240 (if (nth 4 state) ; After `#';
3241 ; (nth 2 state) can be
3242 ; beginning of m,s,qq and so
3243 ; on
3244 (if (nth 2 state)
3245 (progn
3246 (setq cpoint (point))
3247 (goto-char (nth 2 state))
3248 (cond
3249 ((looking-at "\\(s\\|tr\\)\\>")
3250 (or (re-search-forward
3251 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3252 lim 'move)
3253 (setq stop-in t)))
3254 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3255 (or (re-search-forward
3256 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3257 lim 'move)
3258 (setq stop-in t)))
3259 (t ; It was fair comment
3260 (setq stop-in t) ; Finish
3261 (goto-char (1- cpoint)))))
3262 (setq stop-in t) ; Finish
3263 (forward-char -1))
3264 (setq stop-in t))) ; Finish
3265 (nth 4 state))))
3266
3267 (defsubst cperl-modify-syntax-type (at how)
3268 (if (< at (point-max))
3269 (progn
3270 (put-text-property at (1+ at) 'syntax-table how)
3271 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3272
3273 (defun cperl-protect-defun-start (s e)
3274 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3275 (save-excursion
3276 (goto-char s)
3277 (while (re-search-forward "^\\s(" e 'to-end)
3278 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3279
3280 (defun cperl-commentify (bb e string &optional noface)
3281 (if cperl-use-syntax-table-text-property
3282 (if (eq noface 'n) ; Only immediate
3283 nil
3284 ;; We suppose that e is _after_ the end of construction, as after eol.
3285 (setq string (if string cperl-st-sfence cperl-st-cfence))
3286 (if (> bb (- e 2))
3287 ;; one-char string/comment?!
3288 (cperl-modify-syntax-type bb cperl-st-punct)
3289 (cperl-modify-syntax-type bb string)
3290 (cperl-modify-syntax-type (1- e) string))
3291 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3292 (put-text-property (1+ bb) (1- e)
3293 'syntax-table cperl-string-syntax-table))
3294 (cperl-protect-defun-start bb e))
3295 ;; Fontify
3296 (or noface
3297 (not cperl-pod-here-fontify)
3298 (put-text-property bb e 'face (if string 'font-lock-string-face
3299 'font-lock-comment-face)))))
3300
3301 (defvar cperl-starters '(( ?\( . ?\) )
3302 ( ?\[ . ?\] )
3303 ( ?\{ . ?\} )
3304 ( ?\< . ?\> )))
3305
3306 (defun cperl-cached-syntax-table (st)
3307 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3308 All the entries of the syntax table are \".\", except for a backslash, which
3309 is quoting."
3310 (if (car-safe st)
3311 (car st)
3312 (setcar st (make-syntax-table))
3313 (setq st (car st))
3314 (let ((i 0))
3315 (while (< i 256)
3316 (modify-syntax-entry i "." st)
3317 (setq i (1+ i))))
3318 (modify-syntax-entry ?\\ "\\" st)
3319 st))
3320
3321 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3322 &optional ostart oend)
3323 "Find the end of a regular expression or a stringish construct (q[] etc).
3324 The point should be before the starting delimiter.
3325
3326 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3327 is s/// or tr/// like expression. If END is nil, generates an error
3328 message if needed. If SET-ST is non-nil, will use (or generate) a
3329 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3330 error message in its CAR (unless it already contains some error
3331 message). ARGUMENT should be the name of the construct (used in error
3332 messages). OSTART, OEND may be set in recursive calls when processing
3333 the second argument of 2ARG construct.
3334
3335 Works *before* syntax recognition is done. In IS-2ARG situation may
3336 modify syntax-type text property if the situation is too hard."
3337 (let (b starter ender st i i2 go-forward reset-st set-st)
3338 (skip-chars-forward " \t")
3339 ;; ender means matching-char matcher.
3340 (setq b (point)
3341 starter (if (eobp) 0 (char-after b))
3342 ender (cdr (assoc starter cperl-starters)))
3343 ;; What if starter == ?\\ ????
3344 (setq st (cperl-cached-syntax-table st-l))
3345 (setq set-st t)
3346 ;; Whether we have an intermediate point
3347 (setq i nil)
3348 ;; Prepare the syntax table:
3349 (if (not ender) ; m/blah/, s/x//, s/x/y/
3350 (modify-syntax-entry starter "$" st)
3351 (modify-syntax-entry starter (concat "(" (list ender)) st)
3352 (modify-syntax-entry ender (concat ")" (list starter)) st))
3353 (condition-case bb
3354 (progn
3355 ;; We use `$' syntax class to find matching stuff, but $$
3356 ;; is recognized the same as $, so we need to check this manually.
3357 (if (and (eq starter (char-after (cperl-1+ b)))
3358 (not ender))
3359 ;; $ has TeXish matching rules, so $$ equiv $...
3360 (forward-char 2)
3361 (setq reset-st (syntax-table))
3362 (set-syntax-table st)
3363 (forward-sexp 1)
3364 (if (<= (point) (1+ b))
3365 (error "Unfinished regular expression"))
3366 (set-syntax-table reset-st)
3367 (setq reset-st nil)
3368 ;; Now the problem is with m;blah;;
3369 (and (not ender)
3370 (eq (preceding-char)
3371 (char-after (- (point) 2)))
3372 (save-excursion
3373 (forward-char -2)
3374 (= 0 (% (skip-chars-backward "\\\\") 2)))
3375 (forward-char -1)))
3376 ;; Now we are after the first part.
3377 (and is-2arg ; Have trailing part
3378 (not ender)
3379 (eq (following-char) starter) ; Empty trailing part
3380 (progn
3381 (or (eq (char-syntax (following-char)) ?.)
3382 ;; Make trailing letter into punctuation
3383 (cperl-modify-syntax-type (point) cperl-st-punct))
3384 (setq is-2arg nil go-forward t))) ; Ignore the tail
3385 (if is-2arg ; Not number => have second part
3386 (progn
3387 (setq i (point) i2 i)
3388 (if ender
3389 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3390 (progn
3391 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3392 (goto-char (match-end 0))
3393 (skip-chars-forward " \t\n\f"))
3394 (setq i2 (point))))
3395 (forward-char -1))
3396 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3397 (if ender (modify-syntax-entry ender "." st))
3398 (setq set-st nil)
3399 (setq ender (cperl-forward-re lim end nil st-l err-l
3400 argument starter ender)
3401 ender (nth 2 ender)))))
3402 (error (goto-char lim)
3403 (setq set-st nil)
3404 (if reset-st
3405 (set-syntax-table reset-st))
3406 (or end
3407 (and cperl-brace-recursing
3408 (or (eq ostart ?\{)
3409 (eq starter ?\{)))
3410 (message
3411 "End of `%s%s%c ... %c' string/RE not found: %s"
3412 argument
3413 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3414 starter (or ender starter) bb)
3415 (or (car err-l) (setcar err-l b)))))
3416 (if set-st
3417 (progn
3418 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3419 (if ender (modify-syntax-entry ender "." st))))
3420 ;; i: have 2 args, after end of the first arg
3421 ;; i2: start of the second arg, if any (before delim if `ender').
3422 ;; ender: the last arg bounded by parens-like chars, the second one of them
3423 ;; starter: the starting delimiter of the first arg
3424 ;; go-forward: has 2 args, and the second part is empty
3425 (list i i2 ender starter go-forward)))
3426
3427 (defun cperl-forward-group-in-re (&optional st-l)
3428 "Find the end of a group in a REx.
3429 Return the error message (if any). Does not work if delimiter is `)'.
3430 Works before syntax recognition is done."
3431 ;; Works *before* syntax recognition is done
3432 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3433 (let (st b reset-st)
3434 (condition-case b
3435 (progn
3436 (setq st (cperl-cached-syntax-table st-l))
3437 (modify-syntax-entry ?\( "()" st)
3438 (modify-syntax-entry ?\) ")(" st)
3439 (setq reset-st (syntax-table))
3440 (set-syntax-table st)
3441 (forward-sexp 1))
3442 (error (message
3443 "cperl-forward-group-in-re: error %s" b)))
3444 ;; now restore the initial state
3445 (if st
3446 (progn
3447 (modify-syntax-entry ?\( "." st)
3448 (modify-syntax-entry ?\) "." st)))
3449 (if reset-st
3450 (set-syntax-table reset-st))
3451 b))
3452
3453
3454 (defvar font-lock-string-face)
3455 ;;(defvar font-lock-reference-face)
3456 (defvar font-lock-constant-face)
3457 (defsubst cperl-postpone-fontification (b e type val &optional now)
3458 ;; Do after syntactic fontification?
3459 (if cperl-syntaxify-by-font-lock
3460 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3461 (put-text-property b e type val)))
3462
3463 ;;; Here is how the global structures (those which cannot be
3464 ;;; recognized locally) are marked:
3465 ;; a) PODs:
3466 ;; Start-to-end is marked `in-pod' ==> t
3467 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3468 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3469 ;; b) HEREs:
3470 ;; Start-to-end is marked `here-doc-group' ==> t
3471 ;; The body is marked `syntax-type' ==> `here-doc'
3472 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3473 ;; c) FORMATs:
3474 ;; First line (to =) marked `first-format-line' ==> t
3475 ;; After-this--to-end is marked `syntax-type' ==> `format'
3476 ;; d) 'Q'uoted string:
3477 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3478 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3479 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3480 ;; e) Attributes of subroutines: `attrib-group' ==> t
3481 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3482 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3483
3484 ;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3485 ;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3486
3487 (defun cperl-unwind-to-safe (before &optional end)
3488 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3489 (let ((pos (point)) opos)
3490 (while (and pos (progn
3491 (beginning-of-line)
3492 (get-text-property (setq pos (point)) 'syntax-type)))
3493 (setq opos pos
3494 pos (cperl-beginning-of-property pos 'syntax-type))
3495 (if (eq pos (point-min))
3496 (setq pos nil))
3497 (if pos
3498 (if before
3499 (progn
3500 (goto-char (cperl-1- pos))
3501 (beginning-of-line)
3502 (setq pos (point)))
3503 (goto-char (setq pos (cperl-1- pos))))
3504 ;; Up to the start
3505 (goto-char (point-min))))
3506 ;; Skip empty lines
3507 (and (looking-at "\n*=")
3508 (/= 0 (skip-chars-backward "\n"))
3509 (forward-char))
3510 (setq pos (point))
3511 (if end
3512 ;; Do the same for end, going small steps
3513 (save-excursion
3514 (while (and end (get-text-property end 'syntax-type))
3515 (setq pos end
3516 end (next-single-property-change end 'syntax-type nil (point-max)))
3517 (if end (progn (goto-char end)
3518 (or (bolp) (forward-line 1))
3519 (setq end (point)))))
3520 (or end pos)))))
3521
3522 ;;; These are needed for byte-compile (at least with v19)
3523 (defvar cperl-nonoverridable-face)
3524 (defvar font-lock-variable-name-face)
3525 (defvar font-lock-function-name-face)
3526 (defvar font-lock-keyword-face)
3527 (defvar font-lock-builtin-face)
3528 (defvar font-lock-type-face)
3529 (defvar font-lock-comment-face)
3530 (defvar font-lock-warning-face)
3531
3532 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3533 "Syntaxically mark (and fontify) attributes of a subroutine.
3534 Should be called with the point before leading colon of an attribute."
3535 ;; Works *before* syntax recognition is done
3536 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3537 (let (st b p reset-st after-first (start (point)) start1 end1)
3538 (condition-case b
3539 (while (looking-at
3540 (concat
3541 "\\(" ; 1=optional? colon
3542 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3543 "\\)"
3544 (if after-first "?" "")
3545 ;; No space between name and paren allowed...
3546 "\\(\\sw+\\)" ; 3=name
3547 "\\((\\)?")) ; 4=optional paren
3548 (and (match-beginning 1)
3549 (cperl-postpone-fontification
3550 (match-beginning 0) (cperl-1+ (match-beginning 0))
3551 'face font-lock-constant-face))
3552 (setq start1 (match-beginning 3) end1 (match-end 3))
3553 (cperl-postpone-fontification start1 end1
3554 'face font-lock-constant-face)
3555 (goto-char end1) ; end or before `('
3556 (if (match-end 4) ; Have attribute arguments...
3557 (progn
3558 (if st nil
3559 (setq st (cperl-cached-syntax-table st-l))
3560 (modify-syntax-entry ?\( "()" st)
3561 (modify-syntax-entry ?\) ")(" st))
3562 (setq reset-st (syntax-table) p (point))
3563 (set-syntax-table st)
3564 (forward-sexp 1)
3565 (set-syntax-table reset-st)
3566 (setq reset-st nil)
3567 (cperl-commentify p (point) t))) ; mark as string
3568 (forward-comment (buffer-size))
3569 (setq after-first t))
3570 (error (message
3571 "L%d: attribute `%s': %s"
3572 (count-lines (point-min) (point))
3573 (and start1 end1 (buffer-substring start1 end1)) b)
3574 (setq start nil)))
3575 (and start
3576 (progn
3577 (put-text-property start (point)
3578 'attrib-group (if (looking-at "{") t 0))
3579 (and pos
3580 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3581 ;; Apparently, we do not need `multiline': faces added now
3582 (put-text-property (+ 3 pos) (cperl-1+ (point))
3583 'syntax-type 'sub-decl))
3584 (and b-fname ; Fontify here: the following condition
3585 (cperl-postpone-fontification ; is too hard to determine by
3586 b-fname e-fname 'face ; a REx, so do it here
3587 (if (looking-at "{")
3588 font-lock-function-name-face
3589 font-lock-variable-name-face)))))
3590 ;; now restore the initial state
3591 (if st
3592 (progn
3593 (modify-syntax-entry ?\( "." st)
3594 (modify-syntax-entry ?\) "." st)))
3595 (if reset-st
3596 (set-syntax-table reset-st))))
3597
3598 (defsubst cperl-look-at-leading-count (is-x-REx e)
3599 (if (and
3600 (< (point) e)
3601 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3602 (1- e) t)) ; return nil on failure, no moving
3603 (if (eq ?\{ (preceding-char)) nil
3604 (cperl-postpone-fontification
3605 (1- (point)) (point)
3606 'face font-lock-warning-face))))
3607
3608 ;; Do some smarter-highlighting
3609 ;; XXXX Currently ignores alphanum/dash delims,
3610 (defsubst cperl-highlight-charclass (endbracket dashface bsface onec-space)
3611 (let ((l '(1 5 7)) ll lle lll
3612 ;; 2 groups, the first takes the whole match (include \[trnfabe])
3613 (singleChar (concat "\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)")))
3614 (while ; look for unescaped - between non-classes
3615 (re-search-forward
3616 ;; On 19.33, certain simplifications lead
3617 ;; to bugs (as in [^a-z] \\| [trnfabe] )
3618 (concat ; 1: SingleChar (include \[trnfabe])
3619 singleChar
3620 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3621 "\\(" ; 3: DASH SingleChar (match optionally)
3622 "\\(-\\)" ; 4: DASH
3623 singleChar ; 5: SingleChar
3624 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3625 "\\)?"
3626 "\\|"
3627 "\\(" ; 7: other escapes
3628 "\\\\[pP]" "\\([^{]\\|{[^{}]*}\\)"
3629 "\\|" "\\\\[^pP]" "\\)"
3630 )
3631 endbracket 'toend)
3632 (if (match-beginning 4)
3633 (cperl-postpone-fontification
3634 (match-beginning 4) (match-end 4)
3635 'face dashface))
3636 ;; save match data (for looking-at)
3637 (setq lll (mapcar (function (lambda (elt) (cons (match-beginning elt)
3638 (match-end elt)))) l))
3639 (while lll
3640 (setq ll (car lll))
3641 (setq lle (cdr ll)
3642 ll (car ll))
3643 ;; (message "Got %s of %s" ll l)
3644 (if (and ll (eq (char-after ll) ?\\ ))
3645 (save-excursion
3646 (goto-char ll)
3647 (cperl-postpone-fontification ll (1+ ll)
3648 'face bsface)
3649 (if (looking-at "\\\\[a-zA-Z0-9]")
3650 (cperl-postpone-fontification (1+ ll) lle
3651 'face onec-space))))
3652 (setq lll (cdr lll))))
3653 (goto-char endbracket) ; just in case something misbehaves???
3654 t))
3655
3656 ;;; Debugging this may require (setq max-specpdl-size 2000)...
3657 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3658 "Scans the buffer for hard-to-parse Perl constructions.
3659 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3660 the sections using `cperl-pod-head-face', `cperl-pod-face',
3661 `cperl-here-face'."
3662 (interactive)
3663 (or min (setq min (point-min)
3664 cperl-syntax-state nil
3665 cperl-syntax-done-to min))
3666 (or max (setq max (point-max)))
3667 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3668 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3669 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3670 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3671 (modified (buffer-modified-p)) overshoot is-o-REx name
3672 (after-change-functions nil)
3673 (cperl-font-locking t)
3674 (use-syntax-state (and cperl-syntax-state
3675 (>= min (car cperl-syntax-state))))
3676 (state-point (if use-syntax-state
3677 (car cperl-syntax-state)
3678 (point-min)))
3679 (state (if use-syntax-state
3680 (cdr cperl-syntax-state)))
3681 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3682 (st-l (list nil)) (err-l (list nil))
3683 ;; Somehow font-lock may be not loaded yet...
3684 ;; (e.g., when building TAGS via command-line call)
3685 (font-lock-string-face (if (boundp 'font-lock-string-face)
3686 font-lock-string-face
3687 'font-lock-string-face))
3688 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3689 font-lock-constant-face
3690 'font-lock-constant-face))
3691 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3692 (if (boundp 'font-lock-function-name-face)
3693 font-lock-function-name-face
3694 'font-lock-function-name-face))
3695 (font-lock-variable-name-face ; interpolated vars and ({})-code
3696 (if (boundp 'font-lock-variable-name-face)
3697 font-lock-variable-name-face
3698 'font-lock-variable-name-face))
3699 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3700 (if (boundp 'font-lock-function-name-face)
3701 font-lock-function-name-face
3702 'font-lock-function-name-face))
3703 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3704 (if (boundp 'font-lock-constant-face)
3705 font-lock-constant-face
3706 'font-lock-constant-face))
3707 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3708 (if (boundp 'font-lock-builtin-face)
3709 font-lock-builtin-face
3710 'font-lock-builtin-face))
3711 (font-lock-comment-face
3712 (if (boundp 'font-lock-comment-face)
3713 font-lock-comment-face
3714 'font-lock-comment-face))
3715 (font-lock-warning-face
3716 (if (boundp 'font-lock-warning-face)
3717 font-lock-warning-face
3718 'font-lock-warning-face))
3719 (my-cperl-REx-ctl-face ; (|)
3720 (if (boundp 'font-lock-keyword-face)
3721 font-lock-keyword-face
3722 'font-lock-keyword-face))
3723 (my-cperl-REx-modifiers-face ; //gims
3724 (if (boundp 'cperl-nonoverridable-face)
3725 cperl-nonoverridable-face
3726 'cperl-nonoverridable-face))
3727 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3728 (if (boundp 'font-lock-type-face)
3729 font-lock-type-face
3730 'font-lock-type-face))
3731 (stop-point (if ignore-max
3732 (point-max)
3733 max))
3734 (search
3735 (concat
3736 "\\(\\`\n?\\|^\n\\)=" ; POD
3737 "\\|"
3738 ;; One extra () before this:
3739 "<<" ; HERE-DOC
3740 "\\(" ; 1 + 1
3741 ;; First variant "BLAH" or just ``.
3742 "[ \t]*" ; Yes, whitespace is allowed!
3743 "\\([\"'`]\\)" ; 2 + 1 = 3
3744 "\\([^\"'`\n]*\\)" ; 3 + 1
3745 "\\3"
3746 "\\|"
3747 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3748 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3749 ;; Do not have <<= or << 30 or <<30 or << $blah.
3750 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3751 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3752 "\\)"
3753 "\\|"
3754 ;; 1+6 extra () before this:
3755 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3756 (if cperl-use-syntax-table-text-property
3757 (concat
3758 "\\|"
3759 ;; 1+6+2=9 extra () before this:
3760 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3761 "\\|"
3762 ;; 1+6+2+1=10 extra () before this:
3763 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3764 "\\|"
3765 ;; 1+6+2+1+1=11 extra () before this
3766 "\\<sub\\>" ; sub with proto/attr
3767 "\\("
3768 cperl-white-and-comment-rex
3769 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3770 "\\("
3771 cperl-maybe-white-and-comment-rex
3772 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3773 "\\|"
3774 ;; 1+6+2+1+1+6=17 extra () before this:
3775 "\\$\\(['{]\\)" ; $' or ${foo}
3776 "\\|"
3777 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3778 ;; we do not support intervening comments...):
3779 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3780 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3781 "\\|"
3782 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3783 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3784 "\\|"
3785 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3786 ""))))
3787 (unwind-protect
3788 (progn
3789 (save-excursion
3790 (or non-inter
3791 (message "Scanning for \"hard\" Perl constructions..."))
3792 ;;(message "find: %s --> %s" min max)
3793 (and cperl-pod-here-fontify
3794 ;; We had evals here, do not know why...
3795 (setq face cperl-pod-face
3796 head-face cperl-pod-head-face
3797 here-face cperl-here-face))
3798 (remove-text-properties min max
3799 '(syntax-type t in-pod t syntax-table t
3800 attrib-group t
3801 REx-interpolated t
3802 cperl-postpone t
3803 syntax-subtype t
3804 rear-nonsticky t
3805 front-sticky t
3806 here-doc-group t
3807 first-format-line t
3808 REx-part2 t
3809 indentable t))
3810 ;; Need to remove face as well...
3811 (goto-char min)
3812 (and (eq system-type 'emx)
3813 (eq (point) 1)
3814 (let ((case-fold-search t))
3815 (looking-at "extproc[ \t]")) ; Analogue of #!
3816 (cperl-commentify min
3817 (save-excursion (end-of-line) (point))
3818 nil))
3819 (while (and
3820 (< (point) max)
3821 (re-search-forward search max t))
3822 (setq tmpend nil) ; Valid for most cases
3823 (setq b (match-beginning 0)
3824 state (save-excursion (parse-partial-sexp
3825 state-point b nil nil state))
3826 state-point b)
3827 (cond
3828 ;; 1+6+2+1+1+6=17 extra () before this:
3829 ;; "\\$\\(['{]\\)"
3830 ((match-beginning 18) ; $' or ${foo}
3831 (if (eq (preceding-char) ?\') ; $'
3832 (progn
3833 (setq b (1- (point))
3834 state (parse-partial-sexp
3835 state-point (1- b) nil nil state)
3836 state-point (1- b))
3837 (if (nth 3 state) ; in string
3838 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3839 (goto-char (1+ b)))
3840 ;; else: ${
3841 (setq bb (match-beginning 0))
3842 (cperl-modify-syntax-type bb cperl-st-punct)))
3843 ;; No processing in strings/comments beyond this point:
3844 ((or (nth 3 state) (nth 4 state))
3845 t) ; Do nothing in comment/string
3846 ((match-beginning 1) ; POD section
3847 ;; "\\(\\`\n?\\|^\n\\)="
3848 (setq b (match-beginning 0)
3849 state (parse-partial-sexp
3850 state-point b nil nil state)
3851 state-point b)
3852 (if (or (nth 3 state) (nth 4 state)
3853 (looking-at "cut\\>"))
3854 (if (or (nth 3 state) (nth 4 state) ignore-max)
3855 nil ; Doing a chunk only
3856 (message "=cut is not preceded by a POD section")
3857 (or (car err-l) (setcar err-l (point))))
3858 (beginning-of-line)
3859
3860 (setq b (point)
3861 bb b
3862 tb (match-beginning 0)
3863 b1 nil) ; error condition
3864 ;; We do not search to max, since we may be called from
3865 ;; some hook of fontification, and max is random
3866 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3867 (progn
3868 (goto-char b)
3869 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3870 (progn
3871 (message "=cut is not preceded by an empty line")
3872 (setq b1 t)
3873 (or (car err-l) (setcar err-l b))))))
3874 (beginning-of-line 2) ; An empty line after =cut is not POD!
3875 (setq e (point))
3876 (and (> e max)
3877 (progn
3878 (remove-text-properties
3879 max e '(syntax-type t in-pod t syntax-table t
3880 attrib-group t
3881 REx-interpolated t
3882 cperl-postpone t
3883 syntax-subtype t
3884 here-doc-group t
3885 rear-nonsticky t
3886 front-sticky t
3887 first-format-line t
3888 REx-part2 t
3889 indentable t))
3890 (setq tmpend tb)))
3891 (put-text-property b e 'in-pod t)
3892 (put-text-property b e 'syntax-type 'in-pod)
3893 (goto-char b)
3894 (while (re-search-forward "\n\n[ \t]" e t)
3895 ;; We start 'pod 1 char earlier to include the preceding line
3896 (beginning-of-line)
3897 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3898 (cperl-put-do-not-fontify b (point) t)
3899 ;; mark the non-literal parts as PODs
3900 (if cperl-pod-here-fontify
3901 (cperl-postpone-fontification b (point) 'face face t))
3902 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3903 (beginning-of-line)
3904 (setq b (point)))
3905 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3906 (cperl-put-do-not-fontify (point) e t)
3907 (if cperl-pod-here-fontify
3908 (progn
3909 ;; mark the non-literal parts as PODs
3910 (cperl-postpone-fontification (point) e 'face face t)
3911 (goto-char bb)
3912 (if (looking-at
3913 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3914 ;; mark the headers
3915 (cperl-postpone-fontification
3916 (match-beginning 1) (match-end 1)
3917 'face head-face))
3918 (while (re-search-forward
3919 ;; One paragraph
3920 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3921 e 'toend)
3922 ;; mark the headers
3923 (cperl-postpone-fontification
3924 (match-beginning 1) (match-end 1)
3925 'face head-face))))
3926 (cperl-commentify bb e nil)
3927 (goto-char e)
3928 (or (eq e (point-max))
3929 (forward-char -1)))) ; Prepare for immediate POD start.
3930 ;; Here document
3931 ;; We can do many here-per-line;
3932 ;; but multiline quote on the same line as <<HERE confuses us...
3933 ;; ;; One extra () before this:
3934 ;;"<<"
3935 ;; "\\(" ; 1 + 1
3936 ;; ;; First variant "BLAH" or just ``.
3937 ;; "[ \t]*" ; Yes, whitespace is allowed!
3938 ;; "\\([\"'`]\\)" ; 2 + 1
3939 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3940 ;; "\\3"
3941 ;; "\\|"
3942 ;; ;; Second variant: Identifier or \ID or empty
3943 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3944 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3945 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3946 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3947 ;; "\\)"
3948 ((match-beginning 2) ; 1 + 1
3949 (setq b (point)
3950 tb (match-beginning 0)
3951 c (and ; not HERE-DOC
3952 (match-beginning 5)
3953 (save-match-data
3954 (or (looking-at "[ \t]*(") ; << function_call()
3955 (save-excursion ; 1 << func_name, or $foo << 10
3956 (condition-case nil
3957 (progn
3958 (goto-char tb)
3959 ;;; XXX What to do: foo <<bar ???
3960 ;;; XXX Need to support print {a} <<B ???
3961 (forward-sexp -1)
3962 (save-match-data
3963 ; $foo << b; $f .= <<B;
3964 ; ($f+1) << b; a($f) . <<B;
3965 ; foo 1, <<B; $x{a} <<b;
3966 (cond
3967 ((looking-at "[0-9$({]")
3968 (forward-sexp 1)
3969 (and
3970 (looking-at "[ \t]*<<")
3971 (condition-case nil
3972 ;; print $foo <<EOF
3973 (progn
3974 (forward-sexp -2)
3975 (not
3976 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
3977 (error t)))))))
3978 (error nil))) ; func(<<EOF)
3979 (and (not (match-beginning 6)) ; Empty
3980 (looking-at
3981 "[ \t]*[=0-9$@%&(]"))))))
3982 (if c ; Not here-doc
3983 nil ; Skip it.
3984 (setq c (match-end 2)) ; 1 + 1
3985 (if (match-beginning 5) ;4 + 1
3986 (setq b1 (match-beginning 5) ; 4 + 1
3987 e1 (match-end 5)) ; 4 + 1
3988 (setq b1 (match-beginning 4) ; 3 + 1
3989 e1 (match-end 4))) ; 3 + 1
3990 (setq tag (buffer-substring b1 e1)
3991 qtag (regexp-quote tag))
3992 (cond (cperl-pod-here-fontify
3993 ;; Highlight the starting delimiter
3994 (cperl-postpone-fontification
3995 b1 e1 'face my-cperl-delimiters-face)
3996 (cperl-put-do-not-fontify b1 e1 t)))
3997 (forward-line)
3998 (setq i (point))
3999 (if end-of-here-doc
4000 (goto-char end-of-here-doc))
4001 (setq b (point))
4002 ;; We do not search to max, since we may be called from
4003 ;; some hook of fontification, and max is random
4004 (or (and (re-search-forward (concat "^" qtag "$")
4005 stop-point 'toend)
4006 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4007 )
4008 (progn ; Pretend we matched at the end
4009 (goto-char (point-max))
4010 (re-search-forward "\\'")
4011 (message "End of here-document `%s' not found." tag)
4012 (or (car err-l) (setcar err-l b))))
4013 (if cperl-pod-here-fontify
4014 (progn
4015 ;; Highlight the ending delimiter
4016 (cperl-postpone-fontification
4017 (match-beginning 0) (match-end 0)
4018 'face my-cperl-delimiters-face)
4019 (cperl-put-do-not-fontify b (match-end 0) t)
4020 ;; Highlight the HERE-DOC
4021 (cperl-postpone-fontification b (match-beginning 0)
4022 'face here-face)))
4023 (setq e1 (cperl-1+ (match-end 0)))
4024 (put-text-property b (match-beginning 0)
4025 'syntax-type 'here-doc)
4026 (put-text-property (match-beginning 0) e1
4027 'syntax-type 'here-doc-delim)
4028 (put-text-property b e1 'here-doc-group t)
4029 ;; This makes insertion at the start of HERE-DOC update
4030 ;; the whole construct:
4031 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
4032 (cperl-commentify b e1 nil)
4033 (cperl-put-do-not-fontify b (match-end 0) t)
4034 ;; Cache the syntax info...
4035 (setq cperl-syntax-state (cons state-point state))
4036 ;; ... and process the rest of the line...
4037 (setq overshoot
4038 (elt ; non-inter ignore-max
4039 (cperl-find-pods-heres c i t end t e1) 1))
4040 (if (and overshoot (> overshoot (point)))
4041 (goto-char overshoot)
4042 (setq overshoot e1))
4043 (if (> e1 max)
4044 (setq tmpend tb))))
4045 ;; format
4046 ((match-beginning 8)
4047 ;; 1+6=7 extra () before this:
4048 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4049 (setq b (point)
4050 name (if (match-beginning 8) ; 7 + 1
4051 (buffer-substring (match-beginning 8) ; 7 + 1
4052 (match-end 8)) ; 7 + 1
4053 "")
4054 tb (match-beginning 0))
4055 (setq argument nil)
4056 (put-text-property (save-excursion
4057 (beginning-of-line)
4058 (point))
4059 b 'first-format-line 't)
4060 (if cperl-pod-here-fontify
4061 (while (and (eq (forward-line) 0)
4062 (not (looking-at "^[.;]$")))
4063 (cond
4064 ((looking-at "^#")) ; Skip comments
4065 ((and argument ; Skip argument multi-lines
4066 (looking-at "^[ \t]*{"))
4067 (forward-sexp 1)
4068 (setq argument nil))
4069 (argument ; Skip argument lines
4070 (setq argument nil))
4071 (t ; Format line
4072 (setq b1 (point))
4073 (setq argument (looking-at "^[^\n]*[@^]"))
4074 (end-of-line)
4075 ;; Highlight the format line
4076 (cperl-postpone-fontification b1 (point)
4077 'face font-lock-string-face)
4078 (cperl-commentify b1 (point) nil)
4079 (cperl-put-do-not-fontify b1 (point) t))))
4080 ;; We do not search to max, since we may be called from
4081 ;; some hook of fontification, and max is random
4082 (re-search-forward "^[.;]$" stop-point 'toend))
4083 (beginning-of-line)
4084 (if (looking-at "^\\.$") ; ";" is not supported yet
4085 (progn
4086 ;; Highlight the ending delimiter
4087 (cperl-postpone-fontification (point) (+ (point) 2)
4088 'face font-lock-string-face)
4089 (cperl-commentify (point) (+ (point) 2) nil)
4090 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4091 (message "End of format `%s' not found." name)
4092 (or (car err-l) (setcar err-l b)))
4093 (forward-line)
4094 (if (> (point) max)
4095 (setq tmpend tb))
4096 (put-text-property b (point) 'syntax-type 'format))
4097 ;; qq-like String or Regexp:
4098 ((or (match-beginning 10) (match-beginning 11))
4099 ;; 1+6+2=9 extra () before this:
4100 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4101 ;; "\\|"
4102 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4103 (setq b1 (if (match-beginning 10) 10 11)
4104 argument (buffer-substring
4105 (match-beginning b1) (match-end b1))
4106 b (point) ; end of qq etc
4107 i b
4108 c (char-after (match-beginning b1))
4109 bb (char-after (1- (match-beginning b1))) ; tmp holder
4110 ;; bb == "Not a stringy"
4111 bb (if (eq b1 10) ; user variables/whatever
4112 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4113 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4114 ((eq bb ?\:) ; $opt::s
4115 (eq (char-after
4116 (- (match-beginning b1) 2))
4117 ?\:))
4118 ((eq bb ?\>) ; $foo->s
4119 (eq (char-after
4120 (- (match-beginning b1) 2))
4121 ?\-))
4122 ((eq bb ?\&)
4123 (not (eq (char-after ; &&m/blah/
4124 (- (match-beginning b1) 2))
4125 ?\&)))
4126 (t t)))
4127 ;; <file> or <$file>
4128 (and (eq c ?\<)
4129 ;; Do not stringify <FH>, <$fh> :
4130 (save-match-data
4131 (looking-at
4132 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4133 tb (match-beginning 0))
4134 (goto-char (match-beginning b1))
4135 (cperl-backward-to-noncomment (point-min))
4136 (or bb
4137 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4138 (setq argument ""
4139 b1 nil
4140 bb ; Not a regexp?
4141 (not
4142 ;; What is below: regexp-p?
4143 (and
4144 (or (memq (preceding-char)
4145 (append (if (memq c '(?\? ?\<))
4146 ;; $a++ ? 1 : 2
4147 "~{(=|&*!,;:["
4148 "~{(=|&+-*!,;:[") nil))
4149 (and (eq (preceding-char) ?\})
4150 (cperl-after-block-p (point-min)))
4151 (and (eq (char-syntax (preceding-char)) ?w)
4152 (progn
4153 (forward-sexp -1)
4154 ;; After these keywords `/' starts a RE. One should add all the
4155 ;; functions/builtins which expect an argument, but ...
4156 (if (eq (preceding-char) ?-)
4157 ;; -d ?foo? is a RE
4158 (looking-at "[a-zA-Z]\\>")
4159 (and
4160 (not (memq (preceding-char)
4161 '(?$ ?@ ?& ?%)))
4162 (looking-at
4163 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4164 (and (eq (preceding-char) ?.)
4165 (eq (char-after (- (point) 2)) ?.))
4166 (bobp))
4167 ;; m|blah| ? foo : bar;
4168 (not
4169 (and (eq c ?\?)
4170 cperl-use-syntax-table-text-property
4171 (not (bobp))
4172 (progn
4173 (forward-char -1)
4174 (looking-at "\\s|"))))))
4175 b (1- b))
4176 ;; s y tr m
4177 ;; Check for $a -> y
4178 (setq b1 (preceding-char)
4179 go (point))
4180 (if (and (eq b1 ?>)
4181 (eq (char-after (- go 2)) ?-))
4182 ;; Not a regexp
4183 (setq bb t))))
4184 (or bb
4185 (progn
4186 (goto-char b)
4187 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4188 (goto-char (match-end 0))
4189 (skip-chars-forward " \t\n\f"))
4190 (cond ((and (eq (following-char) ?\})
4191 (eq b1 ?\{))
4192 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4193 (goto-char (1- go))
4194 (skip-chars-backward " \t\n\f")
4195 (if (memq (preceding-char) (append "$@%&*" nil))
4196 (setq bb t) ; @{y}
4197 (condition-case nil
4198 (forward-sexp -1)
4199 (error nil)))
4200 (if (or bb
4201 (looking-at ; $foo -> {s}
4202 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4203 (and ; $foo[12] -> {s}
4204 (memq (following-char) '(?\{ ?\[))
4205 (progn
4206 (forward-sexp 1)
4207 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4208 (setq bb t)
4209 (goto-char b)))
4210 ((and (eq (following-char) ?=)
4211 (eq (char-after (1+ (point))) ?\>))
4212 ;; Check for { foo => 1, s => 2 }
4213 ;; Apparently s=> is never a substitution...
4214 (setq bb t))
4215 ((and (eq (following-char) ?:)
4216 (eq b1 ?\{) ; Check for $ { s::bar }
4217 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4218 (progn
4219 (goto-char (1- go))
4220 (skip-chars-backward " \t\n\f")
4221 (memq (preceding-char)
4222 (append "$@%&*" nil))))
4223 (setq bb t))
4224 ((eobp)
4225 (setq bb t)))))
4226 (if bb
4227 (goto-char i)
4228 ;; Skip whitespace and comments...
4229 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4230 (goto-char (match-end 0))
4231 (skip-chars-forward " \t\n\f"))
4232 (if (> (point) b)
4233 (put-text-property b (point) 'syntax-type 'prestring))
4234 ;; qtag means two-arg matcher, may be reset to
4235 ;; 2 or 3 later if some special quoting is needed.
4236 ;; e1 means matching-char matcher.
4237 (setq b (point) ; before the first delimiter
4238 ;; has 2 args
4239 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4240 ;; We do not search to max, since we may be called from
4241 ;; some hook of fontification, and max is random
4242 i (cperl-forward-re stop-point end
4243 i2
4244 st-l err-l argument)
4245 ;; If `go', then it is considered as 1-arg, `b1' is nil
4246 ;; as in s/foo//x; the point is before final "slash"
4247 b1 (nth 1 i) ; start of the second part
4248 tag (nth 2 i) ; ender-char, true if second part
4249 ; is with matching chars []
4250 go (nth 4 i) ; There is a 1-char part after the end
4251 i (car i) ; intermediate point
4252 e1 (point) ; end
4253 ;; Before end of the second part if non-matching: ///
4254 tail (if (and i (not tag))
4255 (1- e1))
4256 e (if i i e1) ; end of the first part
4257 qtag nil ; need to preserve backslashitis
4258 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4259 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4260 ;; Commenting \\ is dangerous, what about ( ?
4261 (and i tail
4262 (eq (char-after i) ?\\)
4263 (setq qtag t))
4264 (and (if go (looking-at ".\\sw*x")
4265 (looking-at "\\sw*x")) ; qr//x
4266 (setq is-x-REx t))
4267 (and (if go (looking-at ".\\sw*o")
4268 (looking-at "\\sw*o")) ; //o
4269 (setq is-o-REx t))
4270 (if (null i)
4271 ;; Considered as 1arg form
4272 (progn
4273 (cperl-commentify b (point) t)
4274 (put-text-property b (point) 'syntax-type 'string)
4275 (if (or is-x-REx
4276 ;; ignore other text properties:
4277 (string-match "^qw$" argument))
4278 (put-text-property b (point) 'indentable t))
4279 (and go
4280 (setq e1 (cperl-1+ e1))
4281 (or (eobp)
4282 (forward-char 1))))
4283 (cperl-commentify b i t)
4284 (if (looking-at "\\sw*e") ; s///e
4285 (progn
4286 ;; Cache the syntax info...
4287 (setq cperl-syntax-state (cons state-point state))
4288 (and
4289 ;; silent:
4290 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4291 ;; Error
4292 (goto-char (1+ max)))
4293 (if (and tag (eq (preceding-char) ?\>))
4294 (progn
4295 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4296 (cperl-modify-syntax-type i cperl-st-bra)))
4297 (put-text-property b i 'syntax-type 'string)
4298 (put-text-property i (point) 'syntax-type 'multiline)
4299 (if is-x-REx
4300 (put-text-property b i 'indentable t)))
4301 (cperl-commentify b1 (point) t)
4302 (put-text-property b (point) 'syntax-type 'string)
4303 (if is-x-REx
4304 (put-text-property b i 'indentable t))
4305 (if qtag
4306 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4307 (setq tail nil)))
4308 ;; Now: tail: if the second part is non-matching without ///e
4309 (if (eq (char-syntax (following-char)) ?w)
4310 (progn
4311 (forward-word 1) ; skip modifiers s///s
4312 (if tail (cperl-commentify tail (point) t))
4313 (cperl-postpone-fontification
4314 e1 (point) 'face my-cperl-REx-modifiers-face)))
4315 ;; Check whether it is m// which means "previous match"
4316 ;; and highlight differently
4317 (setq is-REx
4318 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4319 (or (not (= (length argument) 0))
4320 (not (eq c ?\<)))))
4321 (if (and is-REx
4322 (eq e (+ 2 b))
4323 ;; split // *is* using zero-pattern
4324 (save-excursion
4325 (condition-case nil
4326 (progn
4327 (goto-char tb)
4328 (forward-sexp -1)
4329 (not (looking-at "split\\>")))
4330 (error t))))
4331 (cperl-postpone-fontification
4332 b e 'face font-lock-warning-face)
4333 (if (or i2 ; Has 2 args
4334 (and cperl-fontify-m-as-s
4335 (or
4336 (string-match "^\\(m\\|qr\\)$" argument)
4337 (and (eq 0 (length argument))
4338 (not (eq ?\< (char-after b)))))))
4339 (progn
4340 (cperl-postpone-fontification
4341 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4342 (cperl-postpone-fontification
4343 (1- e) e 'face my-cperl-delimiters-face)))
4344 (if (and is-REx cperl-regexp-scan)
4345 ;; Process RExen: embedded comments, charclasses and ]
4346 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4347 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4348 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4349 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4350 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4351 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4352 (save-excursion
4353 (goto-char (1+ b))
4354 ;; First
4355 (cperl-look-at-leading-count is-x-REx e)
4356 (setq hairy-RE
4357 (concat
4358 (if is-x-REx
4359 (if (eq (char-after b) ?\#)
4360 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4361 "\\((\\?#\\)\\|\\(#\\)")
4362 ;; keep the same count: add a fake group
4363 (if (eq (char-after b) ?\#)
4364 "\\((\\?\\\\#\\)\\(\\)"
4365 "\\((\\?#\\)\\(\\)"))
4366 "\\|"
4367 "\\(\\[\\)" ; 3=[
4368 "\\|"
4369 "\\(]\\)" ; 4=]
4370 "\\|"
4371 ;; XXXX Will not be able to use it in s)))
4372 (if (eq (char-after b) ?\) )
4373 "\\())))\\)" ; Will never match
4374 (if (eq (char-after b) ?? )
4375 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4376 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4377 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4378 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4379 "\\(" ;; XXXX 1-char variables, exc. |()\s
4380 "[$@]"
4381 "\\("
4382 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4383 "\\|"
4384 "{[^{}]*}" ; only one-level allowed
4385 "\\|"
4386 "[^{(|) \t\r\n\f]"
4387 "\\)"
4388 "\\(" ;;8,9:code part of array/hash elt
4389 "\\(" "->" "\\)?"
4390 "\\[[^][]*\\]"
4391 "\\|"
4392 "{[^{}]*}"
4393 "\\)*"
4394 ;; XXXX: what if u is delim?
4395 "\\|"
4396 "[)^|$.*?+]"
4397 "\\|"
4398 "{[0-9]+}"
4399 "\\|"
4400 "{[0-9]+,[0-9]*}"
4401 "\\|"
4402 "\\\\[luLUEQbBAzZG]"
4403 "\\|"
4404 "(" ; Group opener
4405 "\\(" ; 10 group opener follower
4406 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4407 "\\|"
4408 "\\?[:=!>?{]" ; "?" something
4409 "\\|"
4410 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4411 "\\|"
4412 "\\?([0-9]+)" ; (?(1)foo|bar)
4413 "\\|"
4414 "\\?<[=!]"
4415 ;;;"\\|"
4416 ;;; "\\?"
4417 "\\)?"
4418 "\\)"
4419 "\\|"
4420 "\\\\\\(.\\)" ; 12=\SYMBOL
4421 ))
4422 (while
4423 (and (< (point) (1- e))
4424 (re-search-forward hairy-RE (1- e) 'to-end))
4425 (goto-char (match-beginning 0))
4426 (setq REx-subgr-start (point)
4427 was-subgr (following-char))
4428 (cond
4429 ((match-beginning 6) ; 0-length builtins, groups
4430 (goto-char (match-end 0))
4431 (if (match-beginning 11)
4432 (goto-char (match-beginning 11)))
4433 (if (>= (point) e)
4434 (goto-char (1- e)))
4435 (cperl-postpone-fontification
4436 (match-beginning 0) (point)
4437 'face
4438 (cond
4439 ((eq was-subgr ?\) )
4440 (condition-case nil
4441 (save-excursion
4442 (forward-sexp -1)
4443 (if (> (point) b)
4444 (if (if (eq (char-after b) ?? )
4445 (looking-at "(\\\\\\?")
4446 (eq (char-after (1+ (point))) ?\?))
4447 my-cperl-REx-0length-face
4448 my-cperl-REx-ctl-face)
4449 font-lock-warning-face))
4450 (error font-lock-warning-face)))
4451 ((eq was-subgr ?\| )
4452 my-cperl-REx-ctl-face)
4453 ((eq was-subgr ?\$ )
4454 (if (> (point) (1+ REx-subgr-start))
4455 (progn
4456 (put-text-property
4457 (match-beginning 0) (point)
4458 'REx-interpolated
4459 (if is-o-REx 0
4460 (if (and (eq (match-beginning 0)
4461 (1+ b))
4462 (eq (point)
4463 (1- e))) 1 t)))
4464 font-lock-variable-name-face)
4465 my-cperl-REx-spec-char-face))
4466 ((memq was-subgr (append "^." nil) )
4467 my-cperl-REx-spec-char-face)
4468 ((eq was-subgr ?\( )
4469 (if (not (match-beginning 10))
4470 my-cperl-REx-ctl-face
4471 my-cperl-REx-0length-face))
4472 (t my-cperl-REx-0length-face)))
4473 (if (and (memq was-subgr (append "(|" nil))
4474 (not (string-match "(\\?[-imsx]+)"
4475 (match-string 0))))
4476 (cperl-look-at-leading-count is-x-REx e))
4477 (setq was-subgr nil)) ; We do stuff here
4478 ((match-beginning 12) ; \SYMBOL
4479 (forward-char 2)
4480 (if (>= (point) e)
4481 (goto-char (1- e))
4482 ;; How many chars to not highlight:
4483 ;; 0-len special-alnums in other branch =>
4484 ;; Generic: \non-alnum (1), \alnum (1+face)
4485 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4486 (setq REx-subgr-start (point)
4487 qtag (preceding-char))
4488 (cperl-postpone-fontification
4489 (- (point) 2) (- (point) 1) 'face
4490 (if (memq qtag
4491 (append "ghijkmoqvFHIJKMORTVY" nil))
4492 font-lock-warning-face
4493 my-cperl-REx-0length-face))
4494 (if (and (eq (char-after b) qtag)
4495 (memq qtag (append ".])^$|*?+" nil)))
4496 (progn
4497 (if (and cperl-use-syntax-table-text-property
4498 (eq qtag ?\) ))
4499 (put-text-property
4500 REx-subgr-start (1- (point))
4501 'syntax-table cperl-st-punct))
4502 (cperl-postpone-fontification
4503 (1- (point)) (point) 'face
4504 ; \] can't appear below
4505 (if (memq qtag (append ".]^$" nil))
4506 'my-cperl-REx-spec-char-face
4507 (if (memq qtag (append "*?+" nil))
4508 'my-cperl-REx-0length-face
4509 'my-cperl-REx-ctl-face))))) ; )|
4510 ;; Test for arguments:
4511 (cond
4512 ;; This is not pretty: the 5.8.7 logic:
4513 ;; \0numx -> octal (up to total 3 dig)
4514 ;; \DIGIT -> backref unless \0
4515 ;; \DIGITs -> backref if valid
4516 ;; otherwise up to 3 -> octal
4517 ;; Do not try to distinguish, we guess
4518 ((or (and (memq qtag (append "01234567" nil))
4519 (re-search-forward
4520 "\\=[01234567]?[01234567]?"
4521 (1- e) 'to-end))
4522 (and (memq qtag (append "89" nil))
4523 (re-search-forward
4524 "\\=[0123456789]*" (1- e) 'to-end))
4525 (and (eq qtag ?x)
4526 (re-search-forward
4527 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4528 (1- e) 'to-end))
4529 (and (memq qtag (append "pPN" nil))
4530 (re-search-forward "\\={[^{}]+}\\|."
4531 (1- e) 'to-end))
4532 (eq (char-syntax qtag) ?w))
4533 (cperl-postpone-fontification
4534 (1- REx-subgr-start) (point)
4535 'face my-cperl-REx-length1-face))))
4536 (setq was-subgr nil)) ; We do stuff here
4537 ((match-beginning 3) ; [charclass]
4538 ;; Highlight leader, trailer, POSIX classes
4539 (forward-char 1)
4540 (if (eq (char-after b) ?^ )
4541 (and (eq (following-char) ?\\ )
4542 (eq (char-after (cperl-1+ (point)))
4543 ?^ )
4544 (forward-char 2))
4545 (and (eq (following-char) ?^ )
4546 (forward-char 1)))
4547 (setq argument b ; continue? & end of last POSIX
4548 tag nil ; list of POSIX classes
4549 qtag (point)) ; after leading ^ if present
4550 (if (eq (char-after b) ?\] )
4551 (and (eq (following-char) ?\\ )
4552 (eq (char-after (cperl-1+ (point)))
4553 ?\] )
4554 (setq qtag (1+ qtag))
4555 (forward-char 2))
4556 (and (eq (following-char) ?\] )
4557 (forward-char 1)))
4558 (setq REx-subgr-end qtag) ;EndOf smart-highlighed
4559 ;; Apparently, I can't put \] into a charclass
4560 ;; in m]]: m][\\\]\]] produces [\\]]
4561 ;;; POSIX? [:word:] [:^word:] only inside []
4562 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4563 (while ; look for unescaped ]
4564 (and argument
4565 (re-search-forward
4566 (if (eq (char-after b) ?\] )
4567 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4568 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4569 (1- e) 'toend))
4570 ;; Is this ] an end of POSIX class?
4571 (if (save-excursion
4572 (and
4573 (search-backward "[" argument t)
4574 (< REx-subgr-start (point))
4575 (setq argument (point)) ; POSIX-start
4576 (or ; Should work with delim = \
4577 (not (eq (preceding-char) ?\\ ))
4578 ;; XXXX Double \\ is needed with 19.33
4579 (= (% (skip-chars-backward "\\\\") 2) 0))
4580 (looking-at
4581 (cond
4582 ((eq (char-after b) ?\] )
4583 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4584 ((eq (char-after b) ?\: )
4585 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4586 ((eq (char-after b) ?^ )
4587 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4588 ((eq (char-syntax (char-after b))
4589 ?w)
4590 (concat
4591 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4592 (char-to-string (char-after b))
4593 "\\|\\sw\\)+:\]"))
4594 (t "\\\\*\\[:\\^?\\sw*:]")))
4595 (goto-char REx-subgr-end)
4596 (cperl-highlight-charclass
4597 argument my-cperl-REx-spec-char-face
4598 my-cperl-REx-0length-face my-cperl-REx-length1-face)))
4599 (setq tag (cons (cons argument (point))
4600 tag)
4601 argument (point)
4602 REx-subgr-end argument) ; continue
4603 (setq argument nil)))
4604 (and argument
4605 (message "Couldn't find end of charclass in a REx, pos=%s"
4606 REx-subgr-start))
4607 (setq argument (1- (point)))
4608 (goto-char REx-subgr-end)
4609 (cperl-highlight-charclass
4610 argument my-cperl-REx-spec-char-face
4611 my-cperl-REx-0length-face my-cperl-REx-length1-face)
4612 (forward-char 1)
4613 ;; Highlight starter, trailer, POSIX
4614 (if (and cperl-use-syntax-table-text-property
4615 (> (- (point) 2) REx-subgr-start))
4616 (put-text-property
4617 (1+ REx-subgr-start) (1- (point))
4618 'syntax-table cperl-st-punct))
4619 (cperl-postpone-fontification
4620 REx-subgr-start qtag
4621 'face my-cperl-REx-spec-char-face)
4622 (cperl-postpone-fontification
4623 (1- (point)) (point) 'face
4624 my-cperl-REx-spec-char-face)
4625 (if (eq (char-after b) ?\] )
4626 (cperl-postpone-fontification
4627 (- (point) 2) (1- (point))
4628 'face my-cperl-REx-0length-face))
4629 (while tag
4630 (cperl-postpone-fontification
4631 (car (car tag)) (cdr (car tag))
4632 'face font-lock-variable-name-face) ;my-cperl-REx-length1-face
4633 (setq tag (cdr tag)))
4634 (setq was-subgr nil)) ; did facing already
4635 ;; Now rare stuff:
4636 ((and (match-beginning 2) ; #-comment
4637 (/= (match-beginning 2) (match-end 2)))
4638 (beginning-of-line 2)
4639 (if (> (point) e)
4640 (goto-char (1- e))))
4641 ((match-beginning 4) ; character "]"
4642 (setq was-subgr nil) ; We do stuff here
4643 (goto-char (match-end 0))
4644 (if cperl-use-syntax-table-text-property
4645 (put-text-property
4646 (1- (point)) (point)
4647 'syntax-table cperl-st-punct))
4648 (cperl-postpone-fontification
4649 (1- (point)) (point)
4650 'face font-lock-warning-face))
4651 ((match-beginning 5) ; before (?{}) (??{})
4652 (setq tag (match-end 0))
4653 (if (or (setq qtag
4654 (cperl-forward-group-in-re st-l))
4655 (and (>= (point) e)
4656 (setq qtag "no matching `)' found"))
4657 (and (not (eq (char-after (- (point) 2))
4658 ?\} ))
4659 (setq qtag "Can't find })")))
4660 (progn
4661 (goto-char (1- e))
4662 (message "%s" qtag))
4663 (cperl-postpone-fontification
4664 (1- tag) (1- (point))
4665 'face font-lock-variable-name-face)
4666 (cperl-postpone-fontification
4667 REx-subgr-start (1- tag)
4668 'face my-cperl-REx-spec-char-face)
4669 (cperl-postpone-fontification
4670 (1- (point)) (point)
4671 'face my-cperl-REx-spec-char-face)
4672 (if cperl-use-syntax-table-text-property
4673 (progn
4674 (put-text-property
4675 (- (point) 2) (1- (point))
4676 'syntax-table cperl-st-cfence)
4677 (put-text-property
4678 (+ REx-subgr-start 2)
4679 (+ REx-subgr-start 3)
4680 'syntax-table cperl-st-cfence))))
4681 (setq was-subgr nil))
4682 (t ; (?#)-comment
4683 ;; Inside "(" and "\" arn't special in any way
4684 ;; Works also if the outside delimiters are ().
4685 (or;;(if (eq (char-after b) ?\) )
4686 ;;(re-search-forward
4687 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4688 ;; (1- e) 'toend)
4689 (search-forward ")" (1- e) 'toend)
4690 ;;)
4691 (message
4692 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4693 REx-subgr-start))))
4694 (if (>= (point) e)
4695 (goto-char (1- e)))
4696 (cond
4697 (was-subgr
4698 (setq REx-subgr-end (point))
4699 (cperl-commentify
4700 REx-subgr-start REx-subgr-end nil)
4701 (cperl-postpone-fontification
4702 REx-subgr-start REx-subgr-end
4703 'face font-lock-comment-face))))))
4704 (if (and is-REx is-x-REx)
4705 (put-text-property (1+ b) (1- e)
4706 'syntax-subtype 'x-REx)))
4707 (if (and i2 e1 (or (not b1) (> e1 b1)))
4708 (progn ; No errors finding the second part...
4709 (cperl-postpone-fontification
4710 (1- e1) e1 'face my-cperl-delimiters-face)
4711 (if (and (not (eobp))
4712 (assoc (char-after b) cperl-starters))
4713 (progn
4714 (cperl-postpone-fontification
4715 b1 (1+ b1) 'face my-cperl-delimiters-face)
4716 (put-text-property b1 (1+ b1)
4717 'REx-part2 t)))))
4718 (if (> (point) max)
4719 (setq tmpend tb))))
4720 ((match-beginning 17) ; sub with prototype or attribute
4721 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4722 ;;"\\<sub\\>\\(" ;12
4723 ;; cperl-white-and-comment-rex ;13
4724 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4725 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4726 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4727 (setq b1 (match-beginning 14) e1 (match-end 14))
4728 (if (memq (char-after (1- b))
4729 '(?\$ ?\@ ?\% ?\& ?\*))
4730 nil
4731 (goto-char b)
4732 (if (eq (char-after (match-beginning 17)) ?\( )
4733 (progn
4734 (cperl-commentify ; Prototypes; mark as string
4735 (match-beginning 17) (match-end 17) t)
4736 (goto-char (match-end 0))
4737 ;; Now look for attributes after prototype:
4738 (forward-comment (buffer-size))
4739 (and (looking-at ":[^:]")
4740 (cperl-find-sub-attrs st-l b1 e1 b)))
4741 ;; treat attributes without prototype
4742 (goto-char (match-beginning 17))
4743 (cperl-find-sub-attrs st-l b1 e1 b))))
4744 ;; 1+6+2+1+1+6+1=18 extra () before this:
4745 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4746 ((match-beginning 19) ; old $abc'efg syntax
4747 (setq bb (match-end 0))
4748 ;;;(if (nth 3 state) nil ; in string
4749 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4750 (goto-char bb))
4751 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4752 ;; "__\\(END\\|DATA\\)__"
4753 ((match-beginning 20) ; __END__, __DATA__
4754 (setq bb (match-end 0))
4755 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4756 (cperl-commentify b bb nil)
4757 (setq end t))
4758 ;; "\\\\\\(['`\"($]\\)"
4759 ((match-beginning 21)
4760 ;; Trailing backslash; make non-quoting outside string/comment
4761 (setq bb (match-end 0))
4762 (goto-char b)
4763 (skip-chars-backward "\\\\")
4764 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4765 (cperl-modify-syntax-type b cperl-st-punct)
4766 (goto-char bb))
4767 (t (error "Error in regexp of the sniffer")))
4768 (if (> (point) stop-point)
4769 (progn
4770 (if end
4771 (message "Garbage after __END__/__DATA__ ignored")
4772 (message "Unbalanced syntax found while scanning")
4773 (or (car err-l) (setcar err-l b)))
4774 (goto-char stop-point))))
4775 (setq cperl-syntax-state (cons state-point state)
4776 ;; Do not mark syntax as done past tmpend???
4777 cperl-syntax-done-to (or tmpend (max (point) max)))
4778 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4779 )
4780 (if (car err-l) (goto-char (car err-l))
4781 (or non-inter
4782 (message "Scanning for \"hard\" Perl constructions... done"))))
4783 (and (buffer-modified-p)
4784 (not modified)
4785 (set-buffer-modified-p nil))
4786 ;; I do not understand what this is doing here. It breaks font-locking
4787 ;; because it resets the syntax-table from font-lock-syntax-table to
4788 ;; cperl-mode-syntax-table.
4789 ;; (set-syntax-table cperl-mode-syntax-table)
4790 )
4791 (list (car err-l) overshoot)))
4792
4793 (defun cperl-find-pods-heres-region (min max)
4794 (interactive "r")
4795 (cperl-find-pods-heres min max))
4796
4797 (defun cperl-backward-to-noncomment (lim)
4798 ;; Stops at lim or after non-whitespace that is not in comment
4799 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4800 (let (stop p pr)
4801 (while (and (not stop) (> (point) (or lim (point-min))))
4802 (skip-chars-backward " \t\n\f" lim)
4803 (setq p (point))
4804 (beginning-of-line)
4805 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4806 '(pod here-doc here-doc-delim))
4807 (progn
4808 (cperl-unwind-to-safe nil)
4809 (setq pr (get-text-property (point) 'syntax-type))))
4810 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4811 (not (memq pr '(string prestring))))
4812 (progn (cperl-to-comment-or-eol) (bolp))
4813 (progn
4814 (skip-chars-backward " \t")
4815 (if (< p (point)) (goto-char p))
4816 (setq stop t))))))
4817
4818 ;; Used only in `cperl-calculate-indent'...
4819 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4820 ;; Positions is before ?\{. Checks whether it starts a block.
4821 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4822 (cperl-backward-to-noncomment (point-min))
4823 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4824 ; Label may be mixed up with `$blah :'
4825 (save-excursion (cperl-after-label))
4826 (get-text-property (cperl-1- (point)) 'attrib-group)
4827 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4828 (progn
4829 (backward-sexp)
4830 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4831 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4832 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4833 ;; sub bless::foo {}
4834 (progn
4835 (cperl-backward-to-noncomment (point-min))
4836 (and (eq (preceding-char) ?b)
4837 (progn
4838 (forward-sexp -1)
4839 (looking-at "sub[ \t\n\f#]")))))))))
4840
4841 ;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4842 ;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4843 ;;; may be a part of an in-statement construct, such as
4844 ;;; ${something()}, print {FH} $data.
4845 ;;; Moreover, one takes positive approach (looks for else,grep etc)
4846 ;;; another negative (looks for bless,tr etc)
4847 (defun cperl-after-block-p (lim &optional pre-block)
4848 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4849 Would not look before LIM. Assumes that LIM is a good place to begin a
4850 statement. The kind of block we treat here is one after which a new
4851 statement would start; thus the block in ${func()} does not count."
4852 (save-excursion
4853 (condition-case nil
4854 (progn
4855 (or pre-block (forward-sexp -1))
4856 (cperl-backward-to-noncomment lim)
4857 (or (eq (point) lim)
4858 ;; if () {} // sub f () {} // sub f :a(') {}
4859 (eq (preceding-char) ?\) )
4860 ;; label: {}
4861 (save-excursion (cperl-after-label))
4862 ;; sub :attr {}
4863 (get-text-property (cperl-1- (point)) 'attrib-group)
4864 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4865 (save-excursion
4866 (forward-sexp -1)
4867 ;; else {} but not else::func {}
4868 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4869 (not (looking-at "\\(\\sw\\|_\\)+::")))
4870 ;; sub f {}
4871 (progn
4872 (cperl-backward-to-noncomment lim)
4873 (and (eq (preceding-char) ?b)
4874 (progn
4875 (forward-sexp -1)
4876 (looking-at "sub[ \t\n\f#]"))))))
4877 ;; What preceeds is not word... XXXX Last statement in sub???
4878 (cperl-after-expr-p lim))))
4879 (error nil))))
4880
4881 (defun cperl-after-expr-p (&optional lim chars test)
4882 "Return true if the position is good for start of expression.
4883 TEST is the expression to evaluate at the found position. If absent,
4884 CHARS is a string that contains good characters to have before us (however,
4885 `}' is treated \"smartly\" if it is not in the list)."
4886 (let ((lim (or lim (point-min)))
4887 stop p pr)
4888 (cperl-update-syntaxification (point) (point))
4889 (save-excursion
4890 (while (and (not stop) (> (point) lim))
4891 (skip-chars-backward " \t\n\f" lim)
4892 (setq p (point))
4893 (beginning-of-line)
4894 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4895 ;; '(pod here-doc here-doc-delim))
4896 (if (get-text-property (point) 'here-doc-group)
4897 (progn
4898 (goto-char
4899 (cperl-beginning-of-property (point) 'here-doc-group))
4900 (beginning-of-line 0)))
4901 (if (get-text-property (point) 'in-pod)
4902 (progn
4903 (goto-char
4904 (cperl-beginning-of-property (point) 'in-pod))
4905 (beginning-of-line 0)))
4906 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4907 ;; Else: last iteration, or a label
4908 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4909 (skip-chars-backward " \t")
4910 (if (< p (point)) (goto-char p))
4911 (setq p (point))
4912 (if (and (eq (preceding-char) ?:)
4913 (progn
4914 (forward-char -1)
4915 (skip-chars-backward " \t\n\f" lim)
4916 (memq (char-syntax (preceding-char)) '(?w ?_))))
4917 (forward-sexp -1) ; Possibly label. Skip it
4918 (goto-char p)
4919 (setq stop t))))
4920 (or (bobp) ; ???? Needed
4921 (eq (point) lim)
4922 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4923 (progn
4924 (if test (eval test)
4925 (or (memq (preceding-char) (append (or chars "{;") nil))
4926 (and (eq (preceding-char) ?\})
4927 (cperl-after-block-p lim))
4928 (and (eq (following-char) ?.) ; in format: see comment above
4929 (eq (get-text-property (point) 'syntax-type)
4930 'format)))))))))
4931
4932 (defun cperl-backward-to-start-of-expr (&optional lim)
4933 (condition-case nil
4934 (progn
4935 (while (and (or (not lim)
4936 (> (point) lim))
4937 (not (cperl-after-expr-p lim)))
4938 (forward-sexp -1)
4939 ;; May be after $, @, $# etc of a variable
4940 (skip-chars-backward "$@%#")))
4941 (error nil)))
4942
4943 (defun cperl-at-end-of-expr (&optional lim)
4944 ;; Since the SEXP approach below is very fragile, do some overengineering
4945 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4946 (condition-case nil
4947 (save-excursion
4948 ;; If nothing interesting after, does as (forward-sexp -1);
4949 ;; otherwise fails, or ends at a start of following sexp.
4950 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4951 ;; may be stuck after @ or $; just put some stupid workaround now:
4952 (let ((p (point)))
4953 (forward-sexp 1)
4954 (forward-sexp -1)
4955 (while (memq (preceding-char) (append "%&@$*" nil))
4956 (forward-char -1))
4957 (or (< (point) p)
4958 (cperl-after-expr-p lim))))
4959 (error t))))
4960
4961 (defun cperl-forward-to-end-of-expr (&optional lim)
4962 (let ((p (point))))
4963 (condition-case nil
4964 (progn
4965 (while (and (< (point) (or lim (point-max)))
4966 (not (cperl-at-end-of-expr)))
4967 (forward-sexp 1)))
4968 (error nil)))
4969
4970 (defun cperl-backward-to-start-of-continued-exp (lim)
4971 (if (memq (preceding-char) (append ")]}\"'`" nil))
4972 (forward-sexp -1))
4973 (beginning-of-line)
4974 (if (<= (point) lim)
4975 (goto-char (1+ lim)))
4976 (skip-chars-forward " \t"))
4977
4978 (defun cperl-after-block-and-statement-beg (lim)
4979 ;; We assume that we are after ?\}
4980 (and
4981 (cperl-after-block-p lim)
4982 (save-excursion
4983 (forward-sexp -1)
4984 (cperl-backward-to-noncomment (point-min))
4985 (or (bobp)
4986 (eq (point) lim)
4987 (not (= (char-syntax (preceding-char)) ?w))
4988 (progn
4989 (forward-sexp -1)
4990 (not
4991 (looking-at
4992 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4993
4994 \f
4995 (defun cperl-indent-exp ()
4996 "Simple variant of indentation of continued-sexp.
4997
4998 Will not indent comment if it starts at `comment-indent' or looks like
4999 continuation of the comment on the previous line.
5000
5001 If `cperl-indent-region-fix-constructs', will improve spacing on
5002 conditional/loop constructs."
5003 (interactive)
5004 (save-excursion
5005 (let ((tmp-end (progn (end-of-line) (point))) top done)
5006 (save-excursion
5007 (beginning-of-line)
5008 (while (null done)
5009 (setq top (point))
5010 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5011 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
5012 (setq top (point))) ; Get the outermost parenths in line
5013 (goto-char top)
5014 (while (< (point) tmp-end)
5015 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5016 (or (eolp) (forward-sexp 1)))
5017 (if (> (point) tmp-end) ; Yes, there an unfinished block
5018 nil
5019 (if (eq ?\) (preceding-char))
5020 (progn ;; Plan B: find by REGEXP block followup this line
5021 (setq top (point))
5022 (condition-case nil
5023 (progn
5024 (forward-sexp -2)
5025 (if (eq (following-char) ?$ ) ; for my $var (list)
5026 (progn
5027 (forward-sexp -1)
5028 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5029 (forward-sexp -1))))
5030 (if (looking-at
5031 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5032 "\\|for\\(each\\)?\\>\\(\\("
5033 cperl-maybe-white-and-comment-rex
5034 "\\(my\\|local\\|our\\)\\)?"
5035 cperl-maybe-white-and-comment-rex
5036 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5037 (progn
5038 (goto-char top)
5039 (forward-sexp 1)
5040 (setq top (point)))))
5041 (error (setq done t)))
5042 (goto-char top))
5043 (if (looking-at ; Try Plan C: continuation block
5044 (concat cperl-maybe-white-and-comment-rex
5045 "\\<\\(else\\|elsif\|continue\\)\\>"))
5046 (progn
5047 (goto-char (match-end 0))
5048 (save-excursion
5049 (end-of-line)
5050 (setq tmp-end (point))))
5051 (setq done t))))
5052 (save-excursion
5053 (end-of-line)
5054 (setq tmp-end (point))))
5055 (goto-char tmp-end)
5056 (setq tmp-end (point-marker)))
5057 (if cperl-indent-region-fix-constructs
5058 (cperl-fix-line-spacing tmp-end))
5059 (cperl-indent-region (point) tmp-end))))
5060
5061 (defun cperl-fix-line-spacing (&optional end parse-data)
5062 "Improve whitespace in a conditional/loop construct.
5063 Returns some position at the last line."
5064 (interactive)
5065 (or end
5066 (setq end (point-max)))
5067 (let ((ee (save-excursion (end-of-line) (point)))
5068 (cperl-indent-region-fix-constructs
5069 (or cperl-indent-region-fix-constructs 1))
5070 p pp ml have-brace ret)
5071 (save-excursion
5072 (beginning-of-line)
5073 (setq ret (point))
5074 ;; }? continue
5075 ;; blah; }
5076 (if (not
5077 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5078 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5079 nil ; Do not need to do anything
5080 ;; Looking at:
5081 ;; }
5082 ;; else
5083 (if cperl-merge-trailing-else
5084 (if (looking-at
5085 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5086 (progn
5087 (search-forward "}")
5088 (setq p (point))
5089 (skip-chars-forward " \t\n")
5090 (delete-region p (point))
5091 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5092 (beginning-of-line)))
5093 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5094 (save-excursion
5095 (search-forward "}")
5096 (delete-horizontal-space)
5097 (insert "\n")
5098 (setq ret (point))
5099 (if (cperl-indent-line parse-data)
5100 (progn
5101 (cperl-fix-line-spacing end parse-data)
5102 (setq ret (point)))))))
5103 ;; Looking at:
5104 ;; } else
5105 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5106 (progn
5107 (search-forward "}")
5108 (delete-horizontal-space)
5109 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5110 (beginning-of-line)))
5111 ;; Looking at:
5112 ;; else {
5113 (if (looking-at
5114 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5115 (progn
5116 (forward-word 1)
5117 (delete-horizontal-space)
5118 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5119 (beginning-of-line)))
5120 ;; Looking at:
5121 ;; foreach my $var
5122 (if (looking-at
5123 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5124 (progn
5125 (forward-word 2)
5126 (delete-horizontal-space)
5127 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5128 (beginning-of-line)))
5129 ;; Looking at:
5130 ;; foreach my $var (
5131 (if (looking-at
5132 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5133 (progn
5134 (forward-sexp 3)
5135 (delete-horizontal-space)
5136 (insert
5137 (make-string cperl-indent-region-fix-constructs ?\s))
5138 (beginning-of-line)))
5139 ;; Looking at (with or without "}" at start, ending after "({"):
5140 ;; } foreach my $var () OR {
5141 (if (looking-at
5142 "[ \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]*{")
5143 (progn
5144 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5145 (re-search-forward "[({]")
5146 (forward-char -1)
5147 (setq p (point))
5148 (if (eq (following-char) ?\( )
5149 (progn
5150 (forward-sexp 1)
5151 (setq pp (point))) ; past parenth-group
5152 ;; after `else' or nothing
5153 (if ml ; after `else'
5154 (skip-chars-backward " \t\n")
5155 (beginning-of-line))
5156 (setq pp nil))
5157 ;; Now after the sexp before the brace
5158 ;; Multiline expr should be special
5159 (setq ml (and pp (save-excursion (goto-char p)
5160 (search-forward "\n" pp t))))
5161 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5162 (looking-at "[ \t\n]*{"))
5163 (progn
5164 (cond
5165 ((bolp) ; Were before `{', no if/else/etc
5166 nil)
5167 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5168 (delete-horizontal-space)
5169 (if (if ml
5170 cperl-extra-newline-before-brace-multiline
5171 cperl-extra-newline-before-brace)
5172 (progn
5173 (delete-horizontal-space)
5174 (insert "\n")
5175 (setq ret (point))
5176 (if (cperl-indent-line parse-data)
5177 (progn
5178 (cperl-fix-line-spacing end parse-data)
5179 (setq ret (point)))))
5180 (insert
5181 (make-string cperl-indent-region-fix-constructs ?\s))))
5182 ((and (looking-at "[ \t]*\n")
5183 (not (if ml
5184 cperl-extra-newline-before-brace-multiline
5185 cperl-extra-newline-before-brace)))
5186 (setq pp (point))
5187 (skip-chars-forward " \t\n")
5188 (delete-region pp (point))
5189 (insert
5190 (make-string cperl-indent-region-fix-constructs ?\ )))
5191 ((and (looking-at "[\t ]*{")
5192 (if ml cperl-extra-newline-before-brace-multiline
5193 cperl-extra-newline-before-brace))
5194 (delete-horizontal-space)
5195 (insert "\n")
5196 (setq ret (point))
5197 (if (cperl-indent-line parse-data)
5198 (progn
5199 (cperl-fix-line-spacing end parse-data)
5200 (setq ret (point))))))
5201 ;; Now we are before `{'
5202 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5203 (progn
5204 (skip-chars-forward " \t\n")
5205 (setq pp (point))
5206 (forward-sexp 1)
5207 (setq p (point))
5208 (goto-char pp)
5209 (setq ml (search-forward "\n" p t))
5210 (if (or cperl-break-one-line-blocks-when-indent ml)
5211 ;; not good: multi-line BLOCK
5212 (progn
5213 (goto-char (1+ pp))
5214 (delete-horizontal-space)
5215 (insert "\n")
5216 (setq ret (point))
5217 (if (cperl-indent-line parse-data)
5218 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5219 (beginning-of-line)
5220 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5221 ;; Now check whether there is a hanging `}'
5222 ;; Looking at:
5223 ;; } blah
5224 (if (and
5225 cperl-fix-hanging-brace-when-indent
5226 have-brace
5227 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5228 (condition-case nil
5229 (progn
5230 (up-list 1)
5231 (if (and (<= (point) pp)
5232 (eq (preceding-char) ?\} )
5233 (cperl-after-block-and-statement-beg (point-min)))
5234 t
5235 (goto-char p)
5236 nil))
5237 (error nil)))
5238 (progn
5239 (forward-char -1)
5240 (skip-chars-backward " \t")
5241 (if (bolp)
5242 ;; `}' was the first thing on the line, insert NL *after* it.
5243 (progn
5244 (cperl-indent-line parse-data)
5245 (search-forward "}")
5246 (delete-horizontal-space)
5247 (insert "\n"))
5248 (delete-horizontal-space)
5249 (or (eq (preceding-char) ?\;)
5250 (bolp)
5251 (and (eq (preceding-char) ?\} )
5252 (cperl-after-block-p (point-min)))
5253 (insert ";"))
5254 (insert "\n")
5255 (setq ret (point)))
5256 (if (cperl-indent-line parse-data)
5257 (setq ret (cperl-fix-line-spacing end parse-data)))
5258 (beginning-of-line)))))
5259 ret))
5260
5261 (defvar cperl-update-start) ; Do not need to make them local
5262 (defvar cperl-update-end)
5263 (defun cperl-delay-update-hook (beg end old-len)
5264 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5265 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5266
5267 (defun cperl-indent-region (start end)
5268 "Simple variant of indentation of region in CPerl mode.
5269 Should be slow. Will not indent comment if it starts at `comment-indent'
5270 or looks like continuation of the comment on the previous line.
5271 Indents all the lines whose first character is between START and END
5272 inclusive.
5273
5274 If `cperl-indent-region-fix-constructs', will improve spacing on
5275 conditional/loop constructs."
5276 (interactive "r")
5277 (cperl-update-syntaxification end end)
5278 (save-excursion
5279 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5280 (let ((indent-info (if cperl-emacs-can-parse
5281 (list nil nil nil) ; Cannot use '(), since will modify
5282 nil))
5283 (pm 0)
5284 after-change-functions ; Speed it up!
5285 st comm old-comm-indent new-comm-indent p pp i empty)
5286 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5287 (goto-char start)
5288 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5289 (current-column))
5290 new-comm-indent old-comm-indent)
5291 (goto-char start)
5292 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5293 (or (bolp) (beginning-of-line 2))
5294 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5295 (setq st (point))
5296 (if (or
5297 (setq empty (looking-at "[ \t]*\n"))
5298 (and (setq comm (looking-at "[ \t]*#"))
5299 (or (eq (current-indentation) (or old-comm-indent
5300 comment-column))
5301 (setq old-comm-indent nil))))
5302 (if (and old-comm-indent
5303 (not empty)
5304 (= (current-indentation) old-comm-indent)
5305 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5306 (not (eq (get-text-property (point) 'syntax-table)
5307 cperl-st-cfence)))
5308 (let ((comment-column new-comm-indent))
5309 (indent-for-comment)))
5310 (progn
5311 (setq i (cperl-indent-line indent-info))
5312 (or comm
5313 (not i)
5314 (progn
5315 (if cperl-indent-region-fix-constructs
5316 (goto-char (cperl-fix-line-spacing end indent-info)))
5317 (if (setq old-comm-indent
5318 (and (cperl-to-comment-or-eol)
5319 (not (memq (get-text-property (point)
5320 'syntax-type)
5321 '(pod here-doc)))
5322 (not (eq (get-text-property (point)
5323 'syntax-table)
5324 cperl-st-cfence))
5325 (current-column)))
5326 (progn (indent-for-comment)
5327 (skip-chars-backward " \t")
5328 (skip-chars-backward "#")
5329 (setq new-comm-indent (current-column))))))))
5330 (beginning-of-line 2)))
5331 ;; Now run the update hooks
5332 (and after-change-functions
5333 cperl-update-end
5334 (save-excursion
5335 (goto-char cperl-update-end)
5336 (insert " ")
5337 (delete-char -1)
5338 (goto-char cperl-update-start)
5339 (insert " ")
5340 (delete-char -1))))))
5341
5342 ;; Stolen from lisp-mode with a lot of improvements
5343
5344 (defun cperl-fill-paragraph (&optional justify iteration)
5345 "Like `fill-paragraph', but handle CPerl comments.
5346 If any of the current line is a comment, fill the comment or the
5347 block of it that point is in, preserving the comment's initial
5348 indentation and initial hashes. Behaves usually outside of comment."
5349 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5350 (let (;; Non-nil if the current line contains a comment.
5351 has-comment
5352 fill-paragraph-function ; do not recurse
5353 ;; If has-comment, the appropriate fill-prefix for the comment.
5354 comment-fill-prefix
5355 ;; Line that contains code and comment (or nil)
5356 start
5357 c spaces len dc (comment-column comment-column))
5358 ;; Figure out what kind of comment we are looking at.
5359 (save-excursion
5360 (beginning-of-line)
5361 (cond
5362
5363 ;; A line with nothing but a comment on it?
5364 ((looking-at "[ \t]*#[# \t]*")
5365 (setq has-comment t
5366 comment-fill-prefix (buffer-substring (match-beginning 0)
5367 (match-end 0))))
5368
5369 ;; A line with some code, followed by a comment? Remember that the
5370 ;; semi which starts the comment shouldn't be part of a string or
5371 ;; character.
5372 ((cperl-to-comment-or-eol)
5373 (setq has-comment t)
5374 (looking-at "#+[ \t]*")
5375 (setq start (point) c (current-column)
5376 comment-fill-prefix
5377 (concat (make-string (current-column) ?\s)
5378 (buffer-substring (match-beginning 0) (match-end 0)))
5379 spaces (progn (skip-chars-backward " \t")
5380 (buffer-substring (point) start))
5381 dc (- c (current-column)) len (- start (point))
5382 start (point-marker))
5383 (delete-char len)
5384 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5385 (if (not has-comment)
5386 (fill-paragraph justify) ; Do the usual thing outside of comment
5387 ;; Narrow to include only the comment, and then fill the region.
5388 (save-restriction
5389 (narrow-to-region
5390 ;; Find the first line we should include in the region to fill.
5391 (if start (progn (beginning-of-line) (point))
5392 (save-excursion
5393 (while (and (zerop (forward-line -1))
5394 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5395 ;; We may have gone to far. Go forward again.
5396 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5397 (forward-line 1))
5398 (point)))
5399 ;; Find the beginning of the first line past the region to fill.
5400 (save-excursion
5401 (while (progn (forward-line 1)
5402 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5403 (point)))
5404 ;; Remove existing hashes
5405 (goto-char (point-min))
5406 (save-excursion
5407 (while (progn (forward-line 1) (< (point) (point-max)))
5408 (skip-chars-forward " \t")
5409 (if (looking-at "#+")
5410 (progn
5411 (if (and (eq (point) (match-beginning 0))
5412 (not (eq (point) (match-end 0)))) nil
5413 (error
5414 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5415 (delete-char (- (match-end 0) (match-beginning 0)))))))
5416
5417 ;; Lines with only hashes on them can be paragraph boundaries.
5418 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5419 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5420 (fill-prefix comment-fill-prefix))
5421 (fill-paragraph justify)))
5422 (if (and start)
5423 (progn
5424 (goto-char start)
5425 (if (> dc 0)
5426 (progn (delete-char dc) (insert spaces)))
5427 (if (or (= (current-column) c) iteration) nil
5428 (setq comment-column c)
5429 (indent-for-comment)
5430 ;; Repeat once more, flagging as iteration
5431 (cperl-fill-paragraph justify t))))))
5432 t)
5433
5434 (defun cperl-do-auto-fill ()
5435 ;; Break out if the line is short enough
5436 (if (> (save-excursion
5437 (end-of-line)
5438 (current-column))
5439 fill-column)
5440 (let ((c (save-excursion (beginning-of-line)
5441 (cperl-to-comment-or-eol) (point)))
5442 (s (memq (following-char) '(?\s ?\t))) marker)
5443 (if (>= c (point))
5444 ;; Don't break line inside code: only inside comment.
5445 nil
5446 (setq marker (point-marker))
5447 (fill-paragraph nil)
5448 (goto-char marker)
5449 ;; Is not enough, sometimes marker is a start of line
5450 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5451 (goto-char (match-end 0))))
5452 ;; Following space could have gone:
5453 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5454 (insert " ")
5455 (backward-char 1))
5456 ;; Previous space could have gone:
5457 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5458
5459 (defun cperl-imenu-addback (lst &optional isback name)
5460 ;; We suppose that the lst is a DAG, unless the first element only
5461 ;; loops back, and ISBACK is set. Thus this function cannot be
5462 ;; applied twice without ISBACK set.
5463 (cond ((not cperl-imenu-addback) lst)
5464 (t
5465 (or name
5466 (setq name "+++BACK+++"))
5467 (mapc (lambda (elt)
5468 (if (and (listp elt) (listp (cdr elt)))
5469 (progn
5470 ;; In the other order it goes up
5471 ;; one level only ;-(
5472 (setcdr elt (cons (cons name lst)
5473 (cdr elt)))
5474 (cperl-imenu-addback (cdr elt) t name))))
5475 (if isback (cdr lst) lst))
5476 lst)))
5477
5478 (defun cperl-imenu--create-perl-index (&optional regexp)
5479 (require 'imenu) ; May be called from TAGS creator
5480 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5481 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5482 (index-meth-alist '()) meth
5483 packages ends-ranges p marker is-proto
5484 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5485 (goto-char (point-min))
5486 (cperl-update-syntaxification (point-max) (point-max))
5487 ;; Search for the function
5488 (progn ;;save-match-data
5489 (while (re-search-forward
5490 (or regexp cperl-imenu--function-name-regexp-perl)
5491 nil t)
5492 ;; 2=package-group, 5=package-name 8=sub-name
5493 (cond
5494 ((and ; Skip some noise if building tags
5495 (match-beginning 5) ; package name
5496 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5497 (not (save-match-data
5498 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5499 nil)
5500 ((and
5501 (or (match-beginning 2)
5502 (match-beginning 8)) ; package or sub
5503 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5504 (null (get-text-property (match-beginning 1) 'syntax-table))
5505 (null (get-text-property (match-beginning 1) 'syntax-type))
5506 (null (get-text-property (match-beginning 1) 'in-pod)))
5507 (setq is-pack (match-beginning 2))
5508 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5509 ;; (goto-char (match-end 0))) ; Messes what follows
5510 (setq meth nil
5511 p (point))
5512 (while (and ends-ranges (>= p (car ends-ranges)))
5513 ;; delete obsolete entries
5514 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5515 (setq package (or (car packages) "")
5516 end-range (or (car ends-ranges) 0))
5517 (if is-pack ; doing "package"
5518 (progn
5519 (if (match-beginning 5) ; named package
5520 (setq name (buffer-substring (match-beginning 5)
5521 (match-end 5))
5522 name (progn
5523 (set-text-properties 0 (length name) nil name)
5524 name)
5525 package (concat name "::")
5526 name (concat "package " name))
5527 ;; Support nameless packages
5528 (setq name "package;" package ""))
5529 (setq end-range
5530 (save-excursion
5531 (parse-partial-sexp (point) (point-max) -1) (point))
5532 ends-ranges (cons end-range ends-ranges)
5533 packages (cons package packages)))
5534 (setq is-proto
5535 (or (eq (following-char) ?\;)
5536 (eq 0 (get-text-property (point) 'attrib-group)))))
5537 ;; Skip this function name if it is a prototype declaration.
5538 (if (and is-proto (not is-pack)) nil
5539 (or is-pack
5540 (setq name
5541 (buffer-substring (match-beginning 8) (match-end 8)))
5542 (set-text-properties 0 (length name) nil name))
5543 (setq marker (make-marker))
5544 (set-marker marker (match-end (if is-pack 2 8)))
5545 (cond (is-pack nil)
5546 ((string-match "[:']" name)
5547 (setq meth t))
5548 ((> p end-range) nil)
5549 (t
5550 (setq name (concat package name) meth t)))
5551 (setq index (cons name marker))
5552 (if is-pack
5553 (push index index-pack-alist)
5554 (push index index-alist))
5555 (if meth (push index index-meth-alist))
5556 (push index index-unsorted-alist)))
5557 ((match-beginning 16) ; POD section
5558 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5559 marker (make-marker))
5560 (set-marker marker (match-beginning 17))
5561 (set-text-properties 0 (length name) nil name)
5562 (setq name (concat (make-string
5563 (* 3 (- (char-after (match-beginning 16)) ?1))
5564 ?\ )
5565 name)
5566 index (cons name marker))
5567 (setq index1 (cons (concat "=" name) (cdr index)))
5568 (push index index-pod-alist)
5569 (push index1 index-unsorted-alist)))))
5570 (setq index-alist
5571 (if (default-value 'imenu-sort-function)
5572 (sort index-alist (default-value 'imenu-sort-function))
5573 (nreverse index-alist)))
5574 (and index-pod-alist
5575 (push (cons "+POD headers+..."
5576 (nreverse index-pod-alist))
5577 index-alist))
5578 (and (or index-pack-alist index-meth-alist)
5579 (let ((lst index-pack-alist) hier-list pack elt group name)
5580 ;; Remove "package ", reverse and uniquify.
5581 (while lst
5582 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5583 (if (assoc name hier-list) nil
5584 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5585 (setq lst index-meth-alist)
5586 (while lst
5587 (setq elt (car lst) lst (cdr lst))
5588 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5589 (setq pack (substring (car elt) 0 (match-beginning 0)))
5590 (if (setq group (assoc pack hier-list))
5591 (if (listp (cdr group))
5592 ;; Have some functions already
5593 (setcdr group
5594 (cons (cons (substring
5595 (car elt)
5596 (+ 2 (match-beginning 0)))
5597 (cdr elt))
5598 (cdr group)))
5599 (setcdr group (list (cons (substring
5600 (car elt)
5601 (+ 2 (match-beginning 0)))
5602 (cdr elt)))))
5603 (setq hier-list
5604 (cons (cons pack
5605 (list (cons (substring
5606 (car elt)
5607 (+ 2 (match-beginning 0)))
5608 (cdr elt))))
5609 hier-list))))))
5610 (push (cons "+Hierarchy+..."
5611 hier-list)
5612 index-alist)))
5613 (and index-pack-alist
5614 (push (cons "+Packages+..."
5615 (nreverse index-pack-alist))
5616 index-alist))
5617 (and (or index-pack-alist index-pod-alist
5618 (default-value 'imenu-sort-function))
5619 index-unsorted-alist
5620 (push (cons "+Unsorted List+..."
5621 (nreverse index-unsorted-alist))
5622 index-alist))
5623 (cperl-imenu-addback index-alist)))
5624
5625 \f
5626 ;; Suggested by Mark A. Hershberger
5627 (defun cperl-outline-level ()
5628 (looking-at outline-regexp)
5629 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5630 ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5631 ((match-beginning 2) 0) ; package
5632 ((match-beginning 8) 1) ; sub
5633 ((match-beginning 16)
5634 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5635 (t 5))) ; should not happen
5636
5637 \f
5638 (defun cperl-windowed-init ()
5639 "Initialization under windowed version."
5640 (cond ((featurep 'ps-print)
5641 (or cperl-faces-init
5642 (progn
5643 (and (boundp 'font-lock-multiline)
5644 (setq cperl-font-lock-multiline t))
5645 (cperl-init-faces))))
5646 ((not cperl-faces-init)
5647 (add-hook 'font-lock-mode-hook
5648 (function
5649 (lambda ()
5650 (if (memq major-mode '(perl-mode cperl-mode))
5651 (progn
5652 (or cperl-faces-init (cperl-init-faces)))))))
5653 (if (fboundp 'eval-after-load)
5654 (eval-after-load
5655 "ps-print"
5656 '(or cperl-faces-init (cperl-init-faces)))))))
5657
5658 (defvar cperl-font-lock-keywords-1 nil
5659 "Additional expressions to highlight in Perl mode. Minimal set.")
5660 (defvar cperl-font-lock-keywords nil
5661 "Additional expressions to highlight in Perl mode. Default set.")
5662 (defvar cperl-font-lock-keywords-2 nil
5663 "Additional expressions to highlight in Perl mode. Maximal set")
5664
5665 (defun cperl-load-font-lock-keywords ()
5666 (or cperl-faces-init (cperl-init-faces))
5667 cperl-font-lock-keywords)
5668
5669 (defun cperl-load-font-lock-keywords-1 ()
5670 (or cperl-faces-init (cperl-init-faces))
5671 cperl-font-lock-keywords-1)
5672
5673 (defun cperl-load-font-lock-keywords-2 ()
5674 (or cperl-faces-init (cperl-init-faces))
5675 cperl-font-lock-keywords-2)
5676
5677 (defun cperl-init-faces-weak ()
5678 ;; Allow `cperl-find-pods-heres' to run.
5679 (or (boundp 'font-lock-constant-face)
5680 (cperl-force-face font-lock-constant-face
5681 "Face for constant and label names"))
5682 (or (boundp 'font-lock-warning-face)
5683 (cperl-force-face font-lock-warning-face
5684 "Face for things which should stand out"))
5685 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5686 )
5687
5688 (defun cperl-init-faces ()
5689 (condition-case errs
5690 (progn
5691 (require 'font-lock)
5692 (and (fboundp 'font-lock-fontify-anchored-keywords)
5693 (featurep 'font-lock-extra)
5694 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5695 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5696 (if (fboundp 'font-lock-fontify-anchored-keywords)
5697 (setq font-lock-anchored t))
5698 (setq
5699 t-font-lock-keywords
5700 (list
5701 `("[ \t]+$" 0 ',cperl-invalid-face t)
5702 (cons
5703 (concat
5704 "\\(^\\|[^$@%&\\]\\)\\<\\("
5705 (mapconcat
5706 'identity
5707 '("if" "until" "while" "elsif" "else" "unless" "for"
5708 "foreach" "continue" "exit" "die" "last" "goto" "next"
5709 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5710 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5711 "\\|") ; Flow control
5712 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5713 ; In what follows we use `type' style
5714 ; for overwritable builtins
5715 (list
5716 (concat
5717 "\\(^\\|[^$@%&\\]\\)\\<\\("
5718 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5719 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5720 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5721 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5722 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5723 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5724 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5725 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5726 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5727 ;; "gethostbyname" "gethostent" "getlogin"
5728 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5729 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5730 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5731 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5732 ;; "getservbyport" "getservent" "getsockname"
5733 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5734 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5735 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5736 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5737 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5738 ;; "quotemeta" "rand" "read" "readdir" "readline"
5739 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5740 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5741 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5742 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5743 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5744 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5745 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5746 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5747 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5748 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5749 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5750 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5751 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5752 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5753 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5754 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5755 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5756 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5757 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5758 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5759 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5760 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5761 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5762 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5763 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5764 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5765 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5766 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5767 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5768 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5769 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5770 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5771 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5772 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5773 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5774 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5775 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5776 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5777 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5778 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5779 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5780 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5781 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5782 "\\)\\>") 2 'font-lock-type-face)
5783 ;; In what follows we use `other' style
5784 ;; for nonoverwritable builtins
5785 ;; Somehow 's', 'm' are not auto-generated???
5786 (list
5787 (concat
5788 "\\(^\\|[^$@%&\\]\\)\\<\\("
5789 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5790 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5791 ;; "eval" "exists" "for" "foreach" "format" "goto"
5792 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5793 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5794 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5795 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5796 ;; "undef" "unless" "unshift" "untie" "until" "use"
5797 ;; "while" "y"
5798 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5799 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5800 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5801 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5802 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5803 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5804 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5805 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5806 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5807 "\\|[sm]" ; Added manually
5808 "\\)\\>") 2 'cperl-nonoverridable-face)
5809 ;; (mapconcat 'identity
5810 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5811 ;; "#include" "#define" "#undef")
5812 ;; "\\|")
5813 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5814 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5815 ;; This highlights declarations and definitions differenty.
5816 ;; We do not try to highlight in the case of attributes:
5817 ;; it is already done by `cperl-find-pods-heres'
5818 (list (concat "\\<sub"
5819 cperl-white-and-comment-rex ; whitespace/comments
5820 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5821 "\\("
5822 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5823 "([^()]*)\\)?" ; prototype
5824 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5825 "[{;]")
5826 2 (if cperl-font-lock-multiline
5827 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5828 'font-lock-function-name-face
5829 'font-lock-variable-name-face)
5830 ;; need to manually set 'multiline' for older font-locks
5831 '(progn
5832 (if (< 1 (count-lines (match-beginning 0)
5833 (match-end 0)))
5834 (put-text-property
5835 (+ 3 (match-beginning 0)) (match-end 0)
5836 'syntax-type 'multiline))
5837 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5838 'font-lock-function-name-face
5839 'font-lock-variable-name-face))))
5840 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5841 2 font-lock-function-name-face)
5842 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5843 1 font-lock-function-name-face)
5844 (cond ((featurep 'font-lock-extra)
5845 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5846 (2 font-lock-string-face t)
5847 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5848 (font-lock-anchored
5849 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5850 (2 font-lock-string-face t)
5851 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5852 nil nil
5853 (1 font-lock-string-face t))))
5854 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5855 2 font-lock-string-face t)))
5856 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5857 font-lock-string-face t)
5858 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5859 font-lock-constant-face) ; labels
5860 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5861 2 font-lock-constant-face)
5862 ;; Uncomment to get perl-mode-like vars
5863 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5864 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5865 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5866 (cond ((featurep 'font-lock-extra)
5867 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5868 (3 font-lock-variable-name-face)
5869 (4 '(another 4 nil
5870 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5871 (1 font-lock-variable-name-face)
5872 (2 '(restart 2 nil) nil t)))
5873 nil t))) ; local variables, multiple
5874 (font-lock-anchored
5875 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5876 `(,(concat "\\<\\(my\\|local\\|our\\)"
5877 cperl-maybe-white-and-comment-rex
5878 "\\(("
5879 cperl-maybe-white-and-comment-rex
5880 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5881 (5 ,(if cperl-font-lock-multiline
5882 'font-lock-variable-name-face
5883 '(progn (setq cperl-font-lock-multiline-start
5884 (match-beginning 0))
5885 'font-lock-variable-name-face)))
5886 (,(concat "\\="
5887 cperl-maybe-white-and-comment-rex
5888 ","
5889 cperl-maybe-white-and-comment-rex
5890 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5891 ;; Bug in font-lock: limit is used not only to limit
5892 ;; searches, but to set the "extend window for
5893 ;; facification" property. Thus we need to minimize.
5894 ,(if cperl-font-lock-multiline
5895 '(if (match-beginning 3)
5896 (save-excursion
5897 (goto-char (match-beginning 3))
5898 (condition-case nil
5899 (forward-sexp 1)
5900 (error
5901 (condition-case nil
5902 (forward-char 200)
5903 (error nil)))) ; typeahead
5904 (1- (point))) ; report limit
5905 (forward-char -2)) ; disable continued expr
5906 '(if (match-beginning 3)
5907 (point-max) ; No limit for continuation
5908 (forward-char -2))) ; disable continued expr
5909 ,(if cperl-font-lock-multiline
5910 nil
5911 '(progn ; Do at end
5912 ;; "my" may be already fontified (POD),
5913 ;; so cperl-font-lock-multiline-start is nil
5914 (if (or (not cperl-font-lock-multiline-start)
5915 (> 2 (count-lines
5916 cperl-font-lock-multiline-start
5917 (point))))
5918 nil
5919 (put-text-property
5920 (1+ cperl-font-lock-multiline-start) (point)
5921 'syntax-type 'multiline))
5922 (setq cperl-font-lock-multiline-start nil)))
5923 (3 font-lock-variable-name-face))))
5924 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5925 3 font-lock-variable-name-face)))
5926 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5927 4 font-lock-variable-name-face)
5928 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5929 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5930 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5931 (setq
5932 t-font-lock-keywords-1
5933 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
5934 ;; not yet as of XEmacs 19.12, works with 21.1.11
5935 (or
5936 (not (featurep 'xemacs))
5937 (string< "21.1.9" emacs-version)
5938 (and (string< "21.1.10" emacs-version)
5939 (string< emacs-version "21.1.2")))
5940 '(
5941 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5942 (if (eq (char-after (match-beginning 2)) ?%)
5943 'cperl-hash-face
5944 'cperl-array-face)
5945 t) ; arrays and hashes
5946 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5947 1
5948 (if (= (- (match-end 2) (match-beginning 2)) 1)
5949 (if (eq (char-after (match-beginning 3)) ?{)
5950 'cperl-hash-face
5951 'cperl-array-face) ; arrays and hashes
5952 font-lock-variable-name-face) ; Just to put something
5953 t)
5954 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5955 (1 cperl-array-face)
5956 (2 font-lock-variable-name-face))
5957 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5958 (1 cperl-hash-face)
5959 (2 font-lock-variable-name-face))
5960 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5961 ;;; Too much noise from \s* @s[ and friends
5962 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
5963 ;;(3 font-lock-function-name-face t t)
5964 ;;(4
5965 ;; (if (cperl-slash-is-regexp)
5966 ;; font-lock-function-name-face 'default) nil t))
5967 )))
5968 (if cperl-highlight-variables-indiscriminately
5969 (setq t-font-lock-keywords-1
5970 (append t-font-lock-keywords-1
5971 (list '("\\([$*]{?\\sw+\\)" 1
5972 font-lock-variable-name-face)))))
5973 (setq cperl-font-lock-keywords-1
5974 (if cperl-syntaxify-by-font-lock
5975 (cons 'cperl-fontify-update
5976 t-font-lock-keywords)
5977 t-font-lock-keywords)
5978 cperl-font-lock-keywords cperl-font-lock-keywords-1
5979 cperl-font-lock-keywords-2 (append
5980 cperl-font-lock-keywords-1
5981 t-font-lock-keywords-1)))
5982 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5983 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
5984 (eval ; Avoid a warning
5985 '(font-lock-require-faces
5986 (list
5987 ;; Color-light Color-dark Gray-light Gray-dark Mono
5988 (list 'font-lock-comment-face
5989 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5990 nil
5991 [nil nil t t t]
5992 [nil nil t t t]
5993 nil)
5994 (list 'font-lock-string-face
5995 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5996 nil
5997 nil
5998 [nil nil t t t]
5999 nil)
6000 (list 'font-lock-function-name-face
6001 (vector
6002 "Blue" "LightSkyBlue" "Gray50" "LightGray"
6003 (cdr (assq 'background-color ; if mono
6004 (frame-parameters))))
6005 (vector
6006 nil nil nil nil
6007 (cdr (assq 'foreground-color ; if mono
6008 (frame-parameters))))
6009 [nil nil t t t]
6010 nil
6011 nil)
6012 (list 'font-lock-variable-name-face
6013 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6014 nil
6015 [nil nil t t t]
6016 [nil nil t t t]
6017 nil)
6018 (list 'font-lock-type-face
6019 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6020 nil
6021 [nil nil t t t]
6022 nil
6023 [nil nil t t t])
6024 (list 'font-lock-warning-face
6025 ["Pink" "Red" "Gray50" "LightGray"]
6026 ["gray20" "gray90"
6027 "gray80" "gray20"]
6028 [nil nil t t t]
6029 nil
6030 [nil nil t t t]
6031 )
6032 (list 'font-lock-constant-face
6033 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6034 nil
6035 [nil nil t t t]
6036 nil
6037 [nil nil t t t])
6038 (list 'cperl-nonoverridable-face
6039 ["chartreuse3" ("orchid1" "orange")
6040 nil "Gray80"]
6041 [nil nil "gray90"]
6042 [nil nil nil t t]
6043 [nil nil t t]
6044 [nil nil t t t])
6045 (list 'cperl-array-face
6046 ["blue" "yellow" nil "Gray80"]
6047 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6048 "gray90"]
6049 t
6050 nil
6051 nil)
6052 (list 'cperl-hash-face
6053 ["red" "red" nil "Gray80"]
6054 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6055 "gray90"]
6056 t
6057 t
6058 nil))))
6059 ;; Do it the dull way, without choose-color
6060 (defvar cperl-guessed-background nil
6061 "Display characteristics as guessed by cperl.")
6062 ;; (or (fboundp 'x-color-defined-p)
6063 ;; (defalias 'x-color-defined-p
6064 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6065 ;; ;; XEmacs >= 19.12
6066 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6067 ;; ;; XEmacs 19.11
6068 ;; (t 'x-valid-color-name-p))))
6069 (cperl-force-face font-lock-constant-face
6070 "Face for constant and label names")
6071 (cperl-force-face font-lock-variable-name-face
6072 "Face for variable names")
6073 (cperl-force-face font-lock-type-face
6074 "Face for data types")
6075 (cperl-force-face cperl-nonoverridable-face
6076 "Face for data types from another group")
6077 (cperl-force-face font-lock-warning-face
6078 "Face for things which should stand out")
6079 (cperl-force-face font-lock-comment-face
6080 "Face for comments")
6081 (cperl-force-face font-lock-function-name-face
6082 "Face for function names")
6083 (cperl-force-face cperl-hash-face
6084 "Face for hashes")
6085 (cperl-force-face cperl-array-face
6086 "Face for arrays")
6087 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6088 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6089 ;;(or (boundp 'font-lock-type-face)
6090 ;; (defconst font-lock-type-face
6091 ;; 'font-lock-type-face
6092 ;; "Face to use for data types."))
6093 ;;(or (boundp 'cperl-nonoverridable-face)
6094 ;; (defconst cperl-nonoverridable-face
6095 ;; 'cperl-nonoverridable-face
6096 ;; "Face to use for data types from another group."))
6097 ;;(if (not (featurep 'xemacs)) nil
6098 ;; (or (boundp 'font-lock-comment-face)
6099 ;; (defconst font-lock-comment-face
6100 ;; 'font-lock-comment-face
6101 ;; "Face to use for comments."))
6102 ;; (or (boundp 'font-lock-keyword-face)
6103 ;; (defconst font-lock-keyword-face
6104 ;; 'font-lock-keyword-face
6105 ;; "Face to use for keywords."))
6106 ;; (or (boundp 'font-lock-function-name-face)
6107 ;; (defconst font-lock-function-name-face
6108 ;; 'font-lock-function-name-face
6109 ;; "Face to use for function names.")))
6110 (if (and
6111 (not (cperl-is-face 'cperl-array-face))
6112 (cperl-is-face 'font-lock-emphasized-face))
6113 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6114 (if (and
6115 (not (cperl-is-face 'cperl-hash-face))
6116 (cperl-is-face 'font-lock-other-emphasized-face))
6117 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6118 (if (and
6119 (not (cperl-is-face 'cperl-nonoverridable-face))
6120 (cperl-is-face 'font-lock-other-type-face))
6121 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6122 ;;(or (boundp 'cperl-hash-face)
6123 ;; (defconst cperl-hash-face
6124 ;; 'cperl-hash-face
6125 ;; "Face to use for hashes."))
6126 ;;(or (boundp 'cperl-array-face)
6127 ;; (defconst cperl-array-face
6128 ;; 'cperl-array-face
6129 ;; "Face to use for arrays."))
6130 ;; Here we try to guess background
6131 (let ((background
6132 (if (boundp 'font-lock-background-mode)
6133 font-lock-background-mode
6134 'light))
6135 (face-list (and (fboundp 'face-list) (face-list))))
6136 ;;;; (fset 'cperl-is-face
6137 ;;;; (cond ((fboundp 'find-face)
6138 ;;;; (symbol-function 'find-face))
6139 ;;;; (face-list
6140 ;;;; (function (lambda (face) (member face face-list))))
6141 ;;;; (t
6142 ;;;; (function (lambda (face) (boundp face))))))
6143 (defvar cperl-guessed-background
6144 (if (and (boundp 'font-lock-display-type)
6145 (eq font-lock-display-type 'grayscale))
6146 'gray
6147 background)
6148 "Background as guessed by CPerl mode")
6149 (and (not (cperl-is-face 'font-lock-constant-face))
6150 (cperl-is-face 'font-lock-reference-face)
6151 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6152 (if (cperl-is-face 'font-lock-type-face) nil
6153 (copy-face 'default 'font-lock-type-face)
6154 (cond
6155 ((eq background 'light)
6156 (set-face-foreground 'font-lock-type-face
6157 (if (x-color-defined-p "seagreen")
6158 "seagreen"
6159 "sea green")))
6160 ((eq background 'dark)
6161 (set-face-foreground 'font-lock-type-face
6162 (if (x-color-defined-p "os2pink")
6163 "os2pink"
6164 "pink")))
6165 (t
6166 (set-face-background 'font-lock-type-face "gray90"))))
6167 (if (cperl-is-face 'cperl-nonoverridable-face)
6168 nil
6169 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6170 (cond
6171 ((eq background 'light)
6172 (set-face-foreground 'cperl-nonoverridable-face
6173 (if (x-color-defined-p "chartreuse3")
6174 "chartreuse3"
6175 "chartreuse")))
6176 ((eq background 'dark)
6177 (set-face-foreground 'cperl-nonoverridable-face
6178 (if (x-color-defined-p "orchid1")
6179 "orchid1"
6180 "orange")))))
6181 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6182 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6183 ;;; (cond
6184 ;;; ((eq background 'light)
6185 ;;; (set-face-background 'font-lock-other-emphasized-face
6186 ;;; (if (x-color-defined-p "lightyellow2")
6187 ;;; "lightyellow2"
6188 ;;; (if (x-color-defined-p "lightyellow")
6189 ;;; "lightyellow"
6190 ;;; "light yellow"))))
6191 ;;; ((eq background 'dark)
6192 ;;; (set-face-background 'font-lock-other-emphasized-face
6193 ;;; (if (x-color-defined-p "navy")
6194 ;;; "navy"
6195 ;;; (if (x-color-defined-p "darkgreen")
6196 ;;; "darkgreen"
6197 ;;; "dark green"))))
6198 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6199 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6200 ;;; (copy-face 'bold 'font-lock-emphasized-face)
6201 ;;; (cond
6202 ;;; ((eq background 'light)
6203 ;;; (set-face-background 'font-lock-emphasized-face
6204 ;;; (if (x-color-defined-p "lightyellow2")
6205 ;;; "lightyellow2"
6206 ;;; "lightyellow")))
6207 ;;; ((eq background 'dark)
6208 ;;; (set-face-background 'font-lock-emphasized-face
6209 ;;; (if (x-color-defined-p "navy")
6210 ;;; "navy"
6211 ;;; (if (x-color-defined-p "darkgreen")
6212 ;;; "darkgreen"
6213 ;;; "dark green"))))
6214 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6215 (if (cperl-is-face 'font-lock-variable-name-face) nil
6216 (copy-face 'italic 'font-lock-variable-name-face))
6217 (if (cperl-is-face 'font-lock-constant-face) nil
6218 (copy-face 'italic 'font-lock-constant-face))))
6219 (setq cperl-faces-init t))
6220 (error (message "cperl-init-faces (ignored): %s" errs))))
6221
6222
6223 (defun cperl-ps-print-init ()
6224 "Initialization of `ps-print' components for faces used in CPerl."
6225 (eval-after-load "ps-print"
6226 '(setq ps-bold-faces
6227 ;; font-lock-variable-name-face
6228 ;; font-lock-constant-face
6229 (append '(cperl-array-face cperl-hash-face)
6230 ps-bold-faces)
6231 ps-italic-faces
6232 ;; font-lock-constant-face
6233 (append '(cperl-nonoverridable-face cperl-hash-face)
6234 ps-italic-faces)
6235 ps-underlined-faces
6236 ;; font-lock-type-face
6237 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6238 ps-underlined-faces))))
6239
6240 (defvar ps-print-face-extension-alist)
6241
6242 (defun cperl-ps-print (&optional file)
6243 "Pretty-print in CPerl style.
6244 If optional argument FILE is an empty string, prints to printer, otherwise
6245 to the file FILE. If FILE is nil, prompts for a file name.
6246
6247 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6248 (interactive)
6249 (or file
6250 (setq file (read-from-minibuffer
6251 "Print to file (if empty - to printer): "
6252 (concat (buffer-file-name) ".ps")
6253 nil nil 'file-name-history)))
6254 (or (> (length file) 0)
6255 (setq file nil))
6256 (require 'ps-print) ; To get ps-print-face-extension-alist
6257 (let ((ps-print-color-p t)
6258 (ps-print-face-extension-alist ps-print-face-extension-alist))
6259 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6260 (ps-print-buffer-with-faces file)))
6261
6262 ;;; (defun cperl-ps-print-init ()
6263 ;;; "Initialization of `ps-print' components for faces used in CPerl."
6264 ;;; ;; Guard against old versions
6265 ;;; (defvar ps-underlined-faces nil)
6266 ;;; (defvar ps-bold-faces nil)
6267 ;;; (defvar ps-italic-faces nil)
6268 ;;; (setq ps-bold-faces
6269 ;;; (append '(font-lock-emphasized-face
6270 ;;; cperl-array-face
6271 ;;; font-lock-keyword-face
6272 ;;; font-lock-variable-name-face
6273 ;;; font-lock-constant-face
6274 ;;; font-lock-reference-face
6275 ;;; font-lock-other-emphasized-face
6276 ;;; cperl-hash-face)
6277 ;;; ps-bold-faces))
6278 ;;; (setq ps-italic-faces
6279 ;;; (append '(cperl-nonoverridable-face
6280 ;;; font-lock-constant-face
6281 ;;; font-lock-reference-face
6282 ;;; font-lock-other-emphasized-face
6283 ;;; cperl-hash-face)
6284 ;;; ps-italic-faces))
6285 ;;; (setq ps-underlined-faces
6286 ;;; (append '(font-lock-emphasized-face
6287 ;;; cperl-array-face
6288 ;;; font-lock-other-emphasized-face
6289 ;;; cperl-hash-face
6290 ;;; cperl-nonoverridable-face font-lock-type-face)
6291 ;;; ps-underlined-faces))
6292 ;;; (cons 'font-lock-type-face ps-underlined-faces))
6293
6294
6295 (if (cperl-enable-font-lock) (cperl-windowed-init))
6296
6297 (defconst cperl-styles-entries
6298 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6299 cperl-label-offset cperl-extra-newline-before-brace
6300 cperl-extra-newline-before-brace-multiline
6301 cperl-merge-trailing-else
6302 cperl-continued-statement-offset))
6303
6304 (defconst cperl-style-examples
6305 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6306 ##### cperl-continued-brace-offset cperl-label-offset
6307 ##### cperl-continued-statement-offset
6308 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6309
6310 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6311
6312 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6313 if (foo) {
6314 bar
6315 baz;
6316 label:
6317 {
6318 boon;
6319 }
6320 } else {
6321 stop;
6322 }
6323
6324 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6325 if (foo) {
6326 bar
6327 baz;
6328 label:
6329 {
6330 boon;
6331 }
6332 } else {
6333 stop;
6334 }
6335
6336 ### GNU 2/0/0/-2/2/nil/t
6337 if (foo)
6338 {
6339 bar
6340 baz;
6341 label:
6342 {
6343 boon;
6344 }
6345 }
6346 else
6347 {
6348 stop;
6349 }
6350
6351 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6352 if (foo)
6353 {
6354 bar
6355 baz;
6356 label:
6357 {
6358 boon;
6359 }
6360 }
6361 else
6362 {
6363 stop;
6364 }
6365
6366 ### BSD (=C++, but will not change preexisting merge-trailing-else
6367 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6368 if (foo)
6369 {
6370 bar
6371 baz;
6372 label:
6373 {
6374 boon;
6375 }
6376 }
6377 else
6378 {
6379 stop;
6380 }
6381
6382 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6383 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6384 if (foo)
6385 {
6386 bar
6387 baz;
6388 label:
6389 {
6390 boon;
6391 }
6392 }
6393 else
6394 {
6395 stop;
6396 }
6397
6398 ### Whitesmith (=PerlStyle, but will not change preexisting
6399 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6400 if (foo)
6401 {
6402 bar
6403 baz;
6404 label:
6405 {
6406 boon;
6407 }
6408 }
6409 else
6410 {
6411 stop;
6412 }
6413 "
6414 "Examples of if/else with different indent styles (with v4.23).")
6415
6416 (defconst cperl-style-alist
6417 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6418 (cperl-indent-level . 2)
6419 (cperl-brace-offset . 0)
6420 (cperl-continued-brace-offset . 0)
6421 (cperl-label-offset . -2)
6422 (cperl-continued-statement-offset . 2)
6423 (cperl-extra-newline-before-brace . nil)
6424 (cperl-extra-newline-before-brace-multiline . nil)
6425 (cperl-merge-trailing-else . t))
6426
6427 ("PerlStyle" ; CPerl with 4 as indent
6428 (cperl-indent-level . 4)
6429 (cperl-brace-offset . 0)
6430 (cperl-continued-brace-offset . 0)
6431 (cperl-label-offset . -4)
6432 (cperl-continued-statement-offset . 4)
6433 (cperl-extra-newline-before-brace . nil)
6434 (cperl-extra-newline-before-brace-multiline . nil)
6435 (cperl-merge-trailing-else . t))
6436
6437 ("GNU"
6438 (cperl-indent-level . 2)
6439 (cperl-brace-offset . 0)
6440 (cperl-continued-brace-offset . 0)
6441 (cperl-label-offset . -2)
6442 (cperl-continued-statement-offset . 2)
6443 (cperl-extra-newline-before-brace . t)
6444 (cperl-extra-newline-before-brace-multiline . t)
6445 (cperl-merge-trailing-else . nil))
6446
6447 ("K&R"
6448 (cperl-indent-level . 5)
6449 (cperl-brace-offset . 0)
6450 (cperl-continued-brace-offset . -5)
6451 (cperl-label-offset . -5)
6452 (cperl-continued-statement-offset . 5)
6453 ;;(cperl-extra-newline-before-brace . nil) ; ???
6454 ;;(cperl-extra-newline-before-brace-multiline . nil)
6455 (cperl-merge-trailing-else . nil))
6456
6457 ("BSD"
6458 (cperl-indent-level . 4)
6459 (cperl-brace-offset . 0)
6460 (cperl-continued-brace-offset . -4)
6461 (cperl-label-offset . -4)
6462 (cperl-continued-statement-offset . 4)
6463 ;;(cperl-extra-newline-before-brace . nil) ; ???
6464 ;;(cperl-extra-newline-before-brace-multiline . nil)
6465 ;;(cperl-merge-trailing-else . nil) ; ???
6466 )
6467
6468 ("C++"
6469 (cperl-indent-level . 4)
6470 (cperl-brace-offset . 0)
6471 (cperl-continued-brace-offset . -4)
6472 (cperl-label-offset . -4)
6473 (cperl-continued-statement-offset . 4)
6474 (cperl-extra-newline-before-brace . t)
6475 (cperl-extra-newline-before-brace-multiline . t)
6476 (cperl-merge-trailing-else . nil))
6477
6478 ("Whitesmith"
6479 (cperl-indent-level . 4)
6480 (cperl-brace-offset . 0)
6481 (cperl-continued-brace-offset . 0)
6482 (cperl-label-offset . -4)
6483 (cperl-continued-statement-offset . 4)
6484 ;;(cperl-extra-newline-before-brace . nil) ; ???
6485 ;;(cperl-extra-newline-before-brace-multiline . nil)
6486 ;;(cperl-merge-trailing-else . nil) ; ???
6487 )
6488 ("Current"))
6489 "List of variables to set to get a particular indentation style.
6490 Should be used via `cperl-set-style' or via Perl menu.
6491
6492 See examples in `cperl-style-examples'.")
6493
6494 (defun cperl-set-style (style)
6495 "Set CPerl mode variables to use one of several different indentation styles.
6496 The arguments are a string representing the desired style.
6497 The list of styles is in `cperl-style-alist', available styles
6498 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6499
6500 The current value of style is memorized (unless there is a memorized
6501 data already), may be restored by `cperl-set-style-back'.
6502
6503 Chosing \"Current\" style will not change style, so this may be used for
6504 side-effect of memorizing only. Examples in `cperl-style-examples'."
6505 (interactive
6506 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6507 cperl-style-alist)))
6508 (list (completing-read "Enter style: " list nil 'insist))))
6509 (or cperl-old-style
6510 (setq cperl-old-style
6511 (mapcar (function
6512 (lambda (name)
6513 (cons name (eval name))))
6514 cperl-styles-entries)))
6515 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6516 (while style
6517 (setq setting (car style) style (cdr style))
6518 (set (car setting) (cdr setting)))))
6519
6520 (defun cperl-set-style-back ()
6521 "Restore a style memorized by `cperl-set-style'."
6522 (interactive)
6523 (or cperl-old-style (error "The style was not changed"))
6524 (let (setting)
6525 (while cperl-old-style
6526 (setq setting (car cperl-old-style)
6527 cperl-old-style (cdr cperl-old-style))
6528 (set (car setting) (cdr setting)))))
6529
6530 (defun cperl-check-syntax ()
6531 (interactive)
6532 (require 'mode-compile)
6533 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6534 (eval '(mode-compile)))) ; Avoid a warning
6535
6536 (defun cperl-info-buffer (type)
6537 ;; Returns buffer with documentation. Creates if missing.
6538 ;; If TYPE, this vars buffer.
6539 ;; Special care is taken to not stomp over an existing info buffer
6540 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6541 (info (get-buffer bname))
6542 (oldbuf (get-buffer "*info*")))
6543 (if info info
6544 (save-window-excursion
6545 ;; Get Info running
6546 (require 'info)
6547 (cond (oldbuf
6548 (set-buffer oldbuf)
6549 (rename-buffer "*info-perl-tmp*")))
6550 (save-window-excursion
6551 (info))
6552 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6553 (set-buffer "*info*")
6554 (rename-buffer bname)
6555 (cond (oldbuf
6556 (set-buffer "*info-perl-tmp*")
6557 (rename-buffer "*info*")
6558 (set-buffer bname)))
6559 (make-local-variable 'window-min-height)
6560 (setq window-min-height 2)
6561 (current-buffer)))))
6562
6563 (defun cperl-word-at-point (&optional p)
6564 "Return the word at point or at P."
6565 (save-excursion
6566 (if p (goto-char p))
6567 (or (cperl-word-at-point-hard)
6568 (progn
6569 (require 'etags)
6570 (funcall (or (and (boundp 'find-tag-default-function)
6571 find-tag-default-function)
6572 (get major-mode 'find-tag-default-function)
6573 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6574 ;; automatically used within `find-tag-default':
6575 'find-tag-default))))))
6576
6577 (defun cperl-info-on-command (command)
6578 "Show documentation for Perl command COMMAND in other window.
6579 If perl-info buffer is shown in some frame, uses this frame.
6580 Customized by setting variables `cperl-shrink-wrap-info-frame',
6581 `cperl-max-help-size'."
6582 (interactive
6583 (let* ((default (cperl-word-at-point))
6584 (read (read-string
6585 (format "Find doc for Perl function (default %s): "
6586 default))))
6587 (list (if (equal read "")
6588 default
6589 read))))
6590
6591 (let ((buffer (current-buffer))
6592 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6593 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6594 max-height char-height buf-list)
6595 (if (string-match "^-[a-zA-Z]$" command)
6596 (setq cmd-desc "^-X[ \t\n]"))
6597 (setq isvar (string-match "^[$@%]" command)
6598 buf (cperl-info-buffer isvar)
6599 iniwin (selected-window)
6600 fr1 (window-frame iniwin))
6601 (set-buffer buf)
6602 (goto-char (point-min))
6603 (or isvar
6604 (progn (re-search-forward "^-X[ \t\n]")
6605 (forward-line -1)))
6606 (if (re-search-forward cmd-desc nil t)
6607 (progn
6608 ;; Go back to beginning of the group (ex, for qq)
6609 (if (re-search-backward "^[ \t\n\f]")
6610 (forward-line 1))
6611 (beginning-of-line)
6612 ;; Get some of
6613 (setq pos (point)
6614 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6615 (while (and (not win) buf-list)
6616 (setq win (get-buffer-window (car buf-list) t))
6617 (setq buf-list (cdr buf-list)))
6618 (or (not win)
6619 (eq (window-buffer win) buf)
6620 (set-window-buffer win buf))
6621 (and win (setq fr2 (window-frame win)))
6622 (if (or (not fr2) (eq fr1 fr2))
6623 (pop-to-buffer buf)
6624 (special-display-popup-frame buf) ; Make it visible
6625 (select-window win))
6626 (goto-char pos) ; Needed (?!).
6627 ;; Resize
6628 (setq iniheight (window-height)
6629 frheight (frame-height)
6630 not-loner (< iniheight (1- frheight))) ; Are not alone
6631 (cond ((if not-loner cperl-max-help-size
6632 cperl-shrink-wrap-info-frame)
6633 (setq height
6634 (+ 2
6635 (count-lines
6636 pos
6637 (save-excursion
6638 (if (re-search-forward
6639 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6640 (match-beginning 0) (point-max)))))
6641 max-height
6642 (if not-loner
6643 (/ (* (- frheight 3) cperl-max-help-size) 100)
6644 (setq char-height (frame-char-height))
6645 ;; Non-functioning under OS/2:
6646 (if (eq char-height 1) (setq char-height 18))
6647 ;; Title, menubar, + 2 for slack
6648 (- (/ (display-pixel-height) char-height) 4)))
6649 (if (> height max-height) (setq height max-height))
6650 ;;(message "was %s doing %s" iniheight height)
6651 (if not-loner
6652 (enlarge-window (- height iniheight))
6653 (set-frame-height (window-frame win) (1+ height)))))
6654 (set-window-start (selected-window) pos))
6655 (message "No entry for %s found." command))
6656 ;;(pop-to-buffer buffer)
6657 (select-window iniwin)))
6658
6659 (defun cperl-info-on-current-command ()
6660 "Show documentation for Perl command at point in other window."
6661 (interactive)
6662 (cperl-info-on-command (cperl-word-at-point)))
6663
6664 (defun cperl-imenu-info-imenu-search ()
6665 (if (looking-at "^-X[ \t\n]") nil
6666 (re-search-backward
6667 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6668 (forward-line 1)))
6669
6670 (defun cperl-imenu-info-imenu-name ()
6671 (buffer-substring
6672 (match-beginning 1) (match-end 1)))
6673
6674 (defun cperl-imenu-on-info ()
6675 "Shows imenu for Perl Info Buffer.
6676 Opens Perl Info buffer if needed."
6677 (interactive)
6678 (let* ((buffer (current-buffer))
6679 imenu-create-index-function
6680 imenu-prev-index-position-function
6681 imenu-extract-index-name-function
6682 (index-item (save-restriction
6683 (save-window-excursion
6684 (set-buffer (cperl-info-buffer nil))
6685 (setq imenu-create-index-function
6686 'imenu-default-create-index-function
6687 imenu-prev-index-position-function
6688 'cperl-imenu-info-imenu-search
6689 imenu-extract-index-name-function
6690 'cperl-imenu-info-imenu-name)
6691 (imenu-choose-buffer-index)))))
6692 (and index-item
6693 (progn
6694 (push-mark)
6695 (pop-to-buffer "*info-perl*")
6696 (cond
6697 ((markerp (cdr index-item))
6698 (goto-char (marker-position (cdr index-item))))
6699 (t
6700 (goto-char (cdr index-item))))
6701 (set-window-start (selected-window) (point))
6702 (pop-to-buffer buffer)))))
6703
6704 (defun cperl-lineup (beg end &optional step minshift)
6705 "Lineup construction in a region.
6706 Beginning of region should be at the start of a construction.
6707 All first occurrences of this construction in the lines that are
6708 partially contained in the region are lined up at the same column.
6709
6710 MINSHIFT is the minimal amount of space to insert before the construction.
6711 STEP is the tabwidth to position constructions.
6712 If STEP is nil, `cperl-lineup-step' will be used
6713 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6714 Will not move the position at the start to the left."
6715 (interactive "r")
6716 (let (search col tcol seen b)
6717 (save-excursion
6718 (goto-char end)
6719 (end-of-line)
6720 (setq end (point-marker))
6721 (goto-char beg)
6722 (skip-chars-forward " \t\f")
6723 (setq beg (point-marker))
6724 (indent-region beg end nil)
6725 (goto-char beg)
6726 (setq col (current-column))
6727 (if (looking-at "[a-zA-Z0-9_]")
6728 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6729 (setq search
6730 (concat "\\<"
6731 (regexp-quote
6732 (buffer-substring (match-beginning 0)
6733 (match-end 0))) "\\>"))
6734 (error "Cannot line up in a middle of the word"))
6735 (if (looking-at "$")
6736 (error "Cannot line up end of line"))
6737 (setq search (regexp-quote (char-to-string (following-char)))))
6738 (setq step (or step cperl-lineup-step cperl-indent-level))
6739 (or minshift (setq minshift 1))
6740 (while (progn
6741 (beginning-of-line 2)
6742 (and (< (point) end)
6743 (re-search-forward search end t)
6744 (goto-char (match-beginning 0))))
6745 (setq tcol (current-column) seen t)
6746 (if (> tcol col) (setq col tcol)))
6747 (or seen
6748 (error "The construction to line up occurred only once"))
6749 (goto-char beg)
6750 (setq col (+ col minshift))
6751 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6752 (while
6753 (progn
6754 (cperl-make-indent col)
6755 (beginning-of-line 2)
6756 (and (< (point) end)
6757 (re-search-forward search end t)
6758 (goto-char (match-beginning 0)))))))) ; No body
6759
6760 (defun cperl-etags (&optional add all files) ;; NOT USED???
6761 "Run etags with appropriate options for Perl files.
6762 If optional argument ALL is `recursive', will process Perl files
6763 in subdirectories too."
6764 (interactive)
6765 (let ((cmd "etags")
6766 (args '("-l" "none" "-r"
6767 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6768 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6769 "-r"
6770 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6771 "-r"
6772 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6773 res)
6774 (if add (setq args (cons "-a" args)))
6775 (or files (setq files (list buffer-file-name)))
6776 (cond
6777 ((eq all 'recursive)
6778 ;;(error "Not implemented: recursive")
6779 (setq args (append (list "-e"
6780 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6781 use File::Find;
6782 find(\\&wanted, '.');
6783 exec @ARGV;"
6784 cmd) args)
6785 cmd "perl"))
6786 (all
6787 ;;(error "Not implemented: all")
6788 (setq args (append (list "-e"
6789 "push @ARGV, <*.PL *.pl *.pm>;
6790 exec @ARGV;"
6791 cmd) args)
6792 cmd "perl"))
6793 (t
6794 (setq args (append args files))))
6795 (setq res (apply 'call-process cmd nil nil nil args))
6796 (or (eq res 0)
6797 (message "etags returned \"%s\"" res))))
6798
6799 (defun cperl-toggle-auto-newline ()
6800 "Toggle the state of `cperl-auto-newline'."
6801 (interactive)
6802 (setq cperl-auto-newline (not cperl-auto-newline))
6803 (message "Newlines will %sbe auto-inserted now."
6804 (if cperl-auto-newline "" "not ")))
6805
6806 (defun cperl-toggle-abbrev ()
6807 "Toggle the state of automatic keyword expansion in CPerl mode."
6808 (interactive)
6809 (abbrev-mode (if abbrev-mode 0 1))
6810 (message "Perl control structure will %sbe auto-inserted now."
6811 (if abbrev-mode "" "not ")))
6812
6813
6814 (defun cperl-toggle-electric ()
6815 "Toggle the state of parentheses doubling in CPerl mode."
6816 (interactive)
6817 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6818 (message "Parentheses will %sbe auto-doubled now."
6819 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6820
6821 (defun cperl-toggle-autohelp ()
6822 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6823 Delay of auto-help controlled by `cperl-lazy-help-time'."
6824 (interactive)
6825 (if (fboundp 'run-with-idle-timer)
6826 (progn
6827 (if cperl-lazy-installed
6828 (cperl-lazy-unstall)
6829 (cperl-lazy-install))
6830 (message "Perl help messages will %sbe automatically shown now."
6831 (if cperl-lazy-installed "" "not ")))
6832 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6833
6834 (defun cperl-toggle-construct-fix ()
6835 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6836 (interactive)
6837 (setq cperl-indent-region-fix-constructs
6838 (if cperl-indent-region-fix-constructs
6839 nil
6840 1))
6841 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6842 (if cperl-indent-region-fix-constructs "" "not ")))
6843
6844 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6845 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6846 Nonpositive numeric argument disables debugging messages. The message
6847 summarizes which regions it was decided to rescan for syntactic constructs.
6848
6849 The message looks like this:
6850
6851 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6852
6853 Numbers are character positions in the buffer. REQ provides the range to
6854 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6855 for correct operation it should start and end outside any special syntactic
6856 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6857 by CPerl."
6858 (interactive "P")
6859 (or arg
6860 (setq arg (if (eq cperl-syntaxify-by-font-lock
6861 (if backtrace 'backtrace 'message)) 0 1)))
6862 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6863 (setq cperl-syntaxify-by-font-lock arg)
6864 (message "Debugging messages of syntax unwind %sabled."
6865 (if (eq arg t) "dis" "en")))
6866
6867 ;;;; Tags file creation.
6868
6869 (defvar cperl-tmp-buffer " *cperl-tmp*")
6870
6871 (defun cperl-setup-tmp-buf ()
6872 (set-buffer (get-buffer-create cperl-tmp-buffer))
6873 (set-syntax-table cperl-mode-syntax-table)
6874 (buffer-disable-undo)
6875 (auto-fill-mode 0)
6876 (if cperl-use-syntax-table-text-property-for-tags
6877 (progn
6878 (make-local-variable 'parse-sexp-lookup-properties)
6879 ;; Do not introduce variable if not needed, we check it!
6880 (set 'parse-sexp-lookup-properties t))))
6881
6882 ;; Copied from imenu-example--name-and-position.
6883 (defvar imenu-use-markers)
6884
6885 (defun cperl-imenu-name-and-position ()
6886 "Return the current/previous sexp and its (beginning) location.
6887 Does not move point."
6888 (save-excursion
6889 (forward-sexp -1)
6890 (let ((beg (if imenu-use-markers (point-marker) (point)))
6891 (end (progn (forward-sexp) (point))))
6892 (cons (buffer-substring beg end)
6893 beg))))
6894
6895 (defun cperl-xsub-scan ()
6896 (require 'imenu)
6897 (let ((index-alist '())
6898 (prev-pos 0) index index1 name package prefix)
6899 (goto-char (point-min))
6900 ;; Search for the function
6901 (progn ;;save-match-data
6902 (while (re-search-forward
6903 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6904 nil t)
6905 (cond
6906 ((match-beginning 2) ; SECTION
6907 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6908 (goto-char (match-beginning 0))
6909 (skip-chars-forward " \t")
6910 (forward-char 1)
6911 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6912 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6913 (setq prefix nil)))
6914 ((not package) nil) ; C language section
6915 ((match-beginning 3) ; XSUB
6916 (goto-char (1+ (match-beginning 3)))
6917 (setq index (cperl-imenu-name-and-position))
6918 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6919 (if (and prefix (string-match (concat "^" prefix) name))
6920 (setq name (substring name (length prefix))))
6921 (cond ((string-match "::" name) nil)
6922 (t
6923 (setq index1 (cons (concat package "::" name) (cdr index)))
6924 (push index1 index-alist)))
6925 (setcar index name)
6926 (push index index-alist))
6927 (t ; BOOT: section
6928 ;; (beginning-of-line)
6929 (setq index (cperl-imenu-name-and-position))
6930 (setcar index (concat package "::BOOT:"))
6931 (push index index-alist)))))
6932 index-alist))
6933
6934 (defvar cperl-unreadable-ok nil)
6935
6936 (defun cperl-find-tags (ifile xs topdir)
6937 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6938 (cperl-pod-here-fontify nil) f file)
6939 (save-excursion
6940 (if b (set-buffer b)
6941 (cperl-setup-tmp-buf))
6942 (erase-buffer)
6943 (condition-case err
6944 (setq file (car (insert-file-contents ifile)))
6945 (error (if cperl-unreadable-ok nil
6946 (if (y-or-n-p
6947 (format "File %s unreadable. Continue? " ifile))
6948 (setq cperl-unreadable-ok t)
6949 (error "Aborting: unreadable file %s" ifile)))))
6950 (if (not file)
6951 (message "Unreadable file %s" ifile)
6952 (message "Scanning file %s ..." file)
6953 (if (and cperl-use-syntax-table-text-property-for-tags
6954 (not xs))
6955 (condition-case err ; after __END__ may have garbage
6956 (cperl-find-pods-heres nil nil noninteractive)
6957 (error (message "While scanning for syntax: %s" err))))
6958 (if xs
6959 (setq lst (cperl-xsub-scan))
6960 (setq ind (cperl-imenu--create-perl-index))
6961 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6962 (setq lst
6963 (mapcar
6964 (function
6965 (lambda (elt)
6966 (cond ((string-match "^[_a-zA-Z]" (car elt))
6967 (goto-char (cdr elt))
6968 (beginning-of-line) ; pos should be of the start of the line
6969 (list (car elt)
6970 (point)
6971 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6972 (buffer-substring (progn
6973 (goto-char (cdr elt))
6974 ;; After name now...
6975 (or (eolp) (forward-char 1))
6976 (point))
6977 (progn
6978 (beginning-of-line)
6979 (point))))))))
6980 lst))
6981 (erase-buffer)
6982 (while lst
6983 (setq elt (car lst) lst (cdr lst))
6984 (if elt
6985 (progn
6986 (insert (elt elt 3)
6987 127
6988 (if (string-match "^package " (car elt))
6989 (substring (car elt) 8)
6990 (car elt) )
6991 1
6992 (number-to-string (elt elt 2)) ; Line
6993 ","
6994 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
6995 "\n")
6996 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
6997 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
6998 (elt elt 3)))
6999 ;; Need to insert the name without package as well
7000 (setq lst (cons (cons (substring (elt elt 3)
7001 (match-beginning 1)
7002 (match-end 1))
7003 (cdr elt))
7004 lst))))))
7005 (setq pos (point))
7006 (goto-char 1)
7007 (setq rel file)
7008 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7009 (set-text-properties 0 (length rel) nil rel)
7010 (and (equal topdir (substring rel 0 (length topdir)))
7011 (setq rel (substring file (length topdir))))
7012 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
7013 (setq ret (buffer-substring 1 (point-max)))
7014 (erase-buffer)
7015 (or noninteractive
7016 (message "Scanning file %s finished" file))
7017 ret))))
7018
7019 (defun cperl-add-tags-recurse-noxs ()
7020 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
7021 Use as
7022 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7023 -f cperl-add-tags-recurse-noxs
7024 "
7025 (cperl-write-tags nil nil t t nil t))
7026
7027 (defun cperl-add-tags-recurse-noxs-fullpath ()
7028 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7029 Writes down fullpath, so TAGS is relocatable (but if the build directory
7030 is relocated, the file TAGS inside it breaks). Use as
7031 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7032 -f cperl-add-tags-recurse-noxs-fullpath
7033 "
7034 (cperl-write-tags nil nil t t nil t ""))
7035
7036 (defun cperl-add-tags-recurse ()
7037 "Add to TAGS file data for Perl files in the current directory and kids.
7038 Use as
7039 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7040 -f cperl-add-tags-recurse
7041 "
7042 (cperl-write-tags nil nil t t))
7043
7044 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7045 ;; If INBUFFER, do not select buffer, and do not save
7046 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7047 (require 'etags)
7048 (if file nil
7049 (setq file (if dir default-directory (buffer-file-name)))
7050 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7051 (or topdir
7052 (setq topdir default-directory))
7053 (let ((tags-file-name "TAGS")
7054 (case-fold-search (eq system-type 'emx))
7055 xs rel tm)
7056 (save-excursion
7057 (cond (inbuffer nil) ; Already there
7058 ((file-exists-p tags-file-name)
7059 (if (featurep 'xemacs)
7060 (visit-tags-table-buffer)
7061 (visit-tags-table-buffer tags-file-name)))
7062 (t (set-buffer (find-file-noselect tags-file-name))))
7063 (cond
7064 (dir
7065 (cond ((eq erase 'ignore))
7066 (erase
7067 (erase-buffer)
7068 (setq erase 'ignore)))
7069 (let ((files
7070 (condition-case err
7071 (directory-files file t
7072 (if recurse nil cperl-scan-files-regexp)
7073 t)
7074 (error
7075 (if cperl-unreadable-ok nil
7076 (if (y-or-n-p
7077 (format "Directory %s unreadable. Continue? " file))
7078 (setq cperl-unreadable-ok t
7079 tm nil) ; Return empty list
7080 (error "Aborting: unreadable directory %s" file)))))))
7081 (mapc (function
7082 (lambda (file)
7083 (cond
7084 ((string-match cperl-noscan-files-regexp file)
7085 nil)
7086 ((not (file-directory-p file))
7087 (if (string-match cperl-scan-files-regexp file)
7088 (cperl-write-tags file erase recurse nil t noxs topdir)))
7089 ((not recurse) nil)
7090 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7091 files)))
7092 (t
7093 (setq xs (string-match "\\.xs$" file))
7094 (if (not (and xs noxs))
7095 (progn
7096 (cond ((eq erase 'ignore) (goto-char (point-max)))
7097 (erase (erase-buffer))
7098 (t
7099 (goto-char 1)
7100 (setq rel file)
7101 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7102 (set-text-properties 0 (length rel) nil rel)
7103 (and (equal topdir (substring rel 0 (length topdir)))
7104 (setq rel (substring file (length topdir))))
7105 (if (search-forward (concat "\f\n" rel ",") nil t)
7106 (progn
7107 (search-backward "\f\n")
7108 (delete-region (point)
7109 (save-excursion
7110 (forward-char 1)
7111 (if (search-forward "\f\n"
7112 nil 'toend)
7113 (- (point) 2)
7114 (point-max)))))
7115 (goto-char (point-max)))))
7116 (insert (cperl-find-tags file xs topdir))))))
7117 (if inbuffer nil ; Delegate to the caller
7118 (save-buffer 0) ; No backup
7119 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7120 (initialize-new-tags-table))))))
7121
7122 (defvar cperl-tags-hier-regexp-list
7123 (concat
7124 "^\\("
7125 "\\(package\\)\\>"
7126 "\\|"
7127 "sub\\>[^\n]+::"
7128 "\\|"
7129 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7130 "\\|"
7131 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7132 "\\)"))
7133
7134 (defvar cperl-hierarchy '(() ())
7135 "Global hierarchy of classes.")
7136
7137 (defun cperl-tags-hier-fill ()
7138 ;; Suppose we are in a tag table cooked by cperl.
7139 (goto-char 1)
7140 (let (type pack name pos line chunk ord cons1 file str info fileind)
7141 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7142 (setq pos (match-beginning 0)
7143 pack (match-beginning 2))
7144 (beginning-of-line)
7145 (if (looking-at (concat
7146 "\\([^\n]+\\)"
7147 "\C-?"
7148 "\\([^\n]+\\)"
7149 "\C-a"
7150 "\\([0-9]+\\)"
7151 ","
7152 "\\([0-9]+\\)"))
7153 (progn
7154 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7155 name (buffer-substring (match-beginning 2) (match-end 2))
7156 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7157 line (buffer-substring (match-beginning 3) (match-end 3))
7158 ord (if pack 1 0)
7159 file (file-of-tag)
7160 fileind (format "%s:%s" file line)
7161 ;; Moves to beginning of the next line:
7162 info (cperl-etags-snarf-tag file line))
7163 ;; Move back
7164 (forward-char -1)
7165 ;; Make new member of hierarchy name ==> file ==> pos if needed
7166 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7167 ;; Name known
7168 (setcdr cons1 (cons (cons fileind (vector file info))
7169 (cdr cons1)))
7170 ;; First occurrence of the name, start alist
7171 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7172 (if pack
7173 (setcar (cdr cperl-hierarchy)
7174 (cons cons1 (nth 1 cperl-hierarchy)))
7175 (setcar cperl-hierarchy
7176 (cons cons1 (car cperl-hierarchy)))))))
7177 (end-of-line))))
7178
7179 (declare-function x-popup-menu "menu.c" (position menu))
7180
7181 (defun cperl-tags-hier-init (&optional update)
7182 "Show hierarchical menu of classes and methods.
7183 Finds info about classes by a scan of loaded TAGS files.
7184 Supposes that the TAGS files contain fully qualified function names.
7185 One may build such TAGS files from CPerl mode menu."
7186 (interactive)
7187 (require 'etags)
7188 (require 'imenu)
7189 (if (or update (null (nth 2 cperl-hierarchy)))
7190 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7191 (or (nthcdr 2 elt)
7192 ;; Only in one file
7193 (setcdr elt (cdr (nth 1 elt)))))))
7194 pack name cons1 to l1 l2 l3 l4 b)
7195 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7196 (setq cperl-hierarchy (list l1 l2 l3))
7197 (if (featurep 'xemacs) ; Not checked
7198 (progn
7199 (or tags-file-name
7200 ;; Does this work in XEmacs?
7201 (call-interactively 'visit-tags-table))
7202 (message "Updating list of classes...")
7203 (set-buffer (get-file-buffer tags-file-name))
7204 (cperl-tags-hier-fill))
7205 (or tags-table-list
7206 (call-interactively 'visit-tags-table))
7207 (mapc
7208 (function
7209 (lambda (tagsfile)
7210 (message "Updating list of classes... %s" tagsfile)
7211 (set-buffer (get-file-buffer tagsfile))
7212 (cperl-tags-hier-fill)))
7213 tags-table-list)
7214 (message "Updating list of classes... postprocessing..."))
7215 (mapc remover (car cperl-hierarchy))
7216 (mapc remover (nth 1 cperl-hierarchy))
7217 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7218 (cons "Methods: " (car cperl-hierarchy))))
7219 (cperl-tags-treeify to 1)
7220 (setcar (nthcdr 2 cperl-hierarchy)
7221 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7222 (message "Updating list of classes: done, requesting display...")
7223 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7224 ))
7225 (or (nth 2 cperl-hierarchy)
7226 (error "No items found"))
7227 (setq update
7228 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7229 (if (if (fboundp 'display-popup-menus-p)
7230 (let ((f 'display-popup-menus-p))
7231 (funcall f))
7232 window-system)
7233 (x-popup-menu t (nth 2 cperl-hierarchy))
7234 (require 'tmm)
7235 (tmm-prompt (nth 2 cperl-hierarchy))))
7236 (if (and update (listp update))
7237 (progn (while (cdr update) (setq update (cdr update)))
7238 (setq update (car update)))) ; Get the last from the list
7239 (if (vectorp update)
7240 (progn
7241 (find-file (elt update 0))
7242 (cperl-etags-goto-tag-location (elt update 1))))
7243 (if (eq update -999) (cperl-tags-hier-init t)))
7244
7245 (defun cperl-tags-treeify (to level)
7246 ;; cadr of `to' is read-write. On start it is a cons
7247 (let* ((regexp (concat "^\\(" (mapconcat
7248 'identity
7249 (make-list level "[_a-zA-Z0-9]+")
7250 "::")
7251 "\\)\\(::\\)?"))
7252 (packages (cdr (nth 1 to)))
7253 (methods (cdr (nth 2 to)))
7254 l1 head tail cons1 cons2 ord writeto packs recurse
7255 root-packages root-functions ms many_ms same_name ps
7256 (move-deeper
7257 (function
7258 (lambda (elt)
7259 (cond ((and (string-match regexp (car elt))
7260 (or (eq ord 1) (match-end 2)))
7261 (setq head (substring (car elt) 0 (match-end 1))
7262 tail (if (match-end 2) (substring (car elt)
7263 (match-end 2)))
7264 recurse t)
7265 (if (setq cons1 (assoc head writeto)) nil
7266 ;; Need to init new head
7267 (setcdr writeto (cons (list head (list "Packages: ")
7268 (list "Methods: "))
7269 (cdr writeto)))
7270 (setq cons1 (nth 1 writeto)))
7271 (setq cons2 (nth ord cons1)) ; Either packs or meths
7272 (setcdr cons2 (cons elt (cdr cons2))))
7273 ((eq ord 2)
7274 (setq root-functions (cons elt root-functions)))
7275 (t
7276 (setq root-packages (cons elt root-packages))))))))
7277 (setcdr to l1) ; Init to dynamic space
7278 (setq writeto to)
7279 (setq ord 1)
7280 (mapc move-deeper packages)
7281 (setq ord 2)
7282 (mapc move-deeper methods)
7283 (if recurse
7284 (mapc (function (lambda (elt)
7285 (cperl-tags-treeify elt (1+ level))))
7286 (cdr to)))
7287 ;;Now clean up leaders with one child only
7288 (mapc (function (lambda (elt)
7289 (if (not (and (listp (cdr elt))
7290 (eq (length elt) 2))) nil
7291 (setcar elt (car (nth 1 elt)))
7292 (setcdr elt (cdr (nth 1 elt))))))
7293 (cdr to))
7294 ;; Sort the roots of subtrees
7295 (if (default-value 'imenu-sort-function)
7296 (setcdr to
7297 (sort (cdr to) (default-value 'imenu-sort-function))))
7298 ;; Now add back functions removed from display
7299 (mapc (function (lambda (elt)
7300 (setcdr to (cons elt (cdr to)))))
7301 (if (default-value 'imenu-sort-function)
7302 (nreverse
7303 (sort root-functions (default-value 'imenu-sort-function)))
7304 root-functions))
7305 ;; Now add back packages removed from display
7306 (mapc (function (lambda (elt)
7307 (setcdr to (cons (cons (concat "package " (car elt))
7308 (cdr elt))
7309 (cdr to)))))
7310 (if (default-value 'imenu-sort-function)
7311 (nreverse
7312 (sort root-packages (default-value 'imenu-sort-function)))
7313 root-packages))))
7314
7315 ;;;(x-popup-menu t
7316 ;;; '(keymap "Name1"
7317 ;;; ("Ret1" "aa")
7318 ;;; ("Head1" "ab"
7319 ;;; keymap "Name2"
7320 ;;; ("Tail1" "x") ("Tail2" "y"))))
7321
7322 (defun cperl-list-fold (list name limit)
7323 (let (list1 list2 elt1 (num 0))
7324 (if (<= (length list) limit) list
7325 (setq list1 nil list2 nil)
7326 (while list
7327 (setq num (1+ num)
7328 elt1 (car list)
7329 list (cdr list))
7330 (if (<= num imenu-max-items)
7331 (setq list2 (cons elt1 list2))
7332 (setq list1 (cons (cons name
7333 (nreverse list2))
7334 list1)
7335 list2 (list elt1)
7336 num 1)))
7337 (nreverse (cons (cons name
7338 (nreverse list2))
7339 list1)))))
7340
7341 (defun cperl-menu-to-keymap (menu &optional name)
7342 (let (list)
7343 (cons 'keymap
7344 (mapcar
7345 (function
7346 (lambda (elt)
7347 (cond ((listp (cdr elt))
7348 (setq list (cperl-list-fold
7349 (cdr elt) (car elt) imenu-max-items))
7350 (cons nil
7351 (cons (car elt)
7352 (cperl-menu-to-keymap list))))
7353 (t
7354 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7355 (cperl-list-fold menu "Root" imenu-max-items)))))
7356
7357 \f
7358 (defvar cperl-bad-style-regexp
7359 (mapconcat 'identity
7360 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7361 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7362 "\\|")
7363 "Finds places such that insertion of a whitespace may help a lot.")
7364
7365 (defvar cperl-not-bad-style-regexp
7366 (mapconcat
7367 'identity
7368 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7369 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7370 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7371 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7372 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7373 "-[0-9]" ; -5
7374 "\\+\\+" ; ++var
7375 "--" ; --var
7376 ".->" ; a->b
7377 "->" ; a SPACE ->b
7378 "\\[-" ; a[-1]
7379 "\\\\[&$@*\\\\]" ; \&func
7380 "^=" ; =head
7381 "\\$." ; $|
7382 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7383 "||"
7384 "&&"
7385 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7386 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7387 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7388 ;;"[*/+-|&<.]+="
7389 )
7390 "\\|")
7391 "If matches at the start of match found by `my-bad-c-style-regexp',
7392 insertion of a whitespace will not help.")
7393
7394 (defvar found-bad)
7395
7396 (defun cperl-find-bad-style ()
7397 "Find places in the buffer where insertion of a whitespace may help.
7398 Prompts user for insertion of spaces.
7399 Currently it is tuned to C and Perl syntax."
7400 (interactive)
7401 (let (found-bad (p (point)))
7402 (setq last-nonmenu-event 13) ; To disable popup
7403 (goto-char (point-min))
7404 (map-y-or-n-p "Insert space here? "
7405 (lambda (arg) (insert " "))
7406 'cperl-next-bad-style
7407 '("location" "locations" "insert a space into")
7408 '((?\C-r (lambda (arg)
7409 (let ((buffer-quit-function
7410 'exit-recursive-edit))
7411 (message "Exit with Esc Esc")
7412 (recursive-edit)
7413 t)) ; Consider acted upon
7414 "edit, exit with Esc Esc")
7415 (?e (lambda (arg)
7416 (let ((buffer-quit-function
7417 'exit-recursive-edit))
7418 (message "Exit with Esc Esc")
7419 (recursive-edit)
7420 t)) ; Consider acted upon
7421 "edit, exit with Esc Esc"))
7422 t)
7423 (if found-bad (goto-char found-bad)
7424 (goto-char p)
7425 (message "No appropriate place found"))))
7426
7427 (defun cperl-next-bad-style ()
7428 (let (p (not-found t) (point (point)) found)
7429 (while (and not-found
7430 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7431 (setq p (point))
7432 (goto-char (match-beginning 0))
7433 (if (or
7434 (looking-at cperl-not-bad-style-regexp)
7435 ;; Check for a < -b and friends
7436 (and (eq (following-char) ?\-)
7437 (save-excursion
7438 (skip-chars-backward " \t\n")
7439 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7440 ;; Now check for syntax type
7441 (save-match-data
7442 (setq found (point))
7443 (beginning-of-defun)
7444 (let ((pps (parse-partial-sexp (point) found)))
7445 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7446 (goto-char (match-end 0))
7447 (goto-char (1- p))
7448 (setq not-found nil
7449 found-bad found)))
7450 (not not-found)))
7451
7452 \f
7453 ;;; Getting help
7454 (defvar cperl-have-help-regexp
7455 ;;(concat "\\("
7456 (mapconcat
7457 'identity
7458 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7459 "[$@]\\^[a-zA-Z]" ; Special variable
7460 "[$@][^ \n\t]" ; Special variable
7461 "-[a-zA-Z]" ; File test
7462 "\\\\[a-zA-Z0]" ; Special chars
7463 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7464 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7465 "[a-zA-Z_0-9:]+" ; symbol or number
7466 "x="
7467 "#!")
7468 ;;"\\)\\|\\("
7469 "\\|")
7470 ;;"\\)"
7471 ;;)
7472 "Matches places in the buffer we can find help for.")
7473
7474 (defvar cperl-message-on-help-error t)
7475 (defvar cperl-help-from-timer nil)
7476
7477 (defun cperl-word-at-point-hard ()
7478 ;; Does not save-excursion
7479 ;; Get to the something meaningful
7480 (or (eobp) (eolp) (forward-char 1))
7481 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7482 (save-excursion (beginning-of-line) (point))
7483 'to-beg)
7484 ;; (cond
7485 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7486 ;; (skip-chars-backward " \n\t\r({[]});,")
7487 ;; (or (bobp) (backward-char 1))))
7488 ;; Try to backtrace
7489 (cond
7490 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7491 (skip-chars-backward "a-zA-Z0-9_:")
7492 (cond
7493 ((and (eq (preceding-char) ?^) ; $^I
7494 (eq (char-after (- (point) 2)) ?\$))
7495 (forward-char -2))
7496 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7497 (forward-char -1))
7498 ((and (eq (preceding-char) ?\=)
7499 (eq (current-column) 1))
7500 (forward-char -1))) ; =head1
7501 (if (and (eq (preceding-char) ?\<)
7502 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7503 (forward-char -1)))
7504 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7505 (forward-char -1))
7506 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7507 (forward-char -1))
7508 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7509 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7510 (cond
7511 ((and (eq (preceding-char) ?\$)
7512 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7513 (forward-char -1))
7514 ((and (eq (following-char) ?\>)
7515 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7516 (save-excursion
7517 (forward-sexp -1)
7518 (and (eq (preceding-char) ?\<)
7519 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7520 (search-backward "<"))))
7521 ((and (eq (following-char) ?\$)
7522 (eq (preceding-char) ?\<)
7523 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7524 (forward-char -1)))
7525 (if (looking-at cperl-have-help-regexp)
7526 (buffer-substring (match-beginning 0) (match-end 0))))
7527
7528 (defun cperl-get-help ()
7529 "Get one-line docs on the symbol at the point.
7530 The data for these docs is a little bit obsolete and may be in fact longer
7531 than a line. Your contribution to update/shorten it is appreciated."
7532 (interactive)
7533 (save-match-data ; May be called "inside" query-replace
7534 (save-excursion
7535 (let ((word (cperl-word-at-point-hard)))
7536 (if word
7537 (if (and cperl-help-from-timer ; Bail out if not in mainland
7538 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7539 (or (memq (get-text-property (point) 'face)
7540 '(font-lock-comment-face font-lock-string-face))
7541 (memq (get-text-property (point) 'syntax-type)
7542 '(pod here-doc format))))
7543 nil
7544 (cperl-describe-perl-symbol word))
7545 (if cperl-message-on-help-error
7546 (message "Nothing found for %s..."
7547 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7548
7549 ;;; Stolen from perl-descr.el by Johan Vromans:
7550
7551 (defvar cperl-doc-buffer " *perl-doc*"
7552 "Where the documentation can be found.")
7553
7554 (defun cperl-describe-perl-symbol (val)
7555 "Display the documentation of symbol at point, a Perl operator."
7556 (let ((enable-recursive-minibuffers t)
7557 args-file regexp)
7558 (cond
7559 ((string-match "^[&*][a-zA-Z_]" val)
7560 (setq val (concat (substring val 0 1) "NAME")))
7561 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7562 (setq val (concat "@" (substring val 1 (match-end 1)))))
7563 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7564 (setq val (concat "%" (substring val 1 (match-end 1)))))
7565 ((and (string= val "x") (string-match "^x=" val))
7566 (setq val "x="))
7567 ((string-match "^\\$[\C-a-\C-z]" val)
7568 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7569 ((string-match "^CORE::" val)
7570 (setq val "CORE::"))
7571 ((string-match "^SUPER::" val)
7572 (setq val "SUPER::"))
7573 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7574 (setq val "<NAME>")))
7575 (setq regexp (concat "^"
7576 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7577 (regexp-quote val)
7578 "\\([ \t([/]\\|$\\)"))
7579
7580 ;; get the buffer with the documentation text
7581 (cperl-switch-to-doc-buffer)
7582
7583 ;; lookup in the doc
7584 (goto-char (point-min))
7585 (let ((case-fold-search nil))
7586 (list
7587 (if (re-search-forward regexp (point-max) t)
7588 (save-excursion
7589 (beginning-of-line 1)
7590 (let ((lnstart (point)))
7591 (end-of-line)
7592 (message "%s" (buffer-substring lnstart (point)))))
7593 (if cperl-message-on-help-error
7594 (message "No definition for %s" val)))))))
7595
7596 (defvar cperl-short-docs 'please-ignore-this-line
7597 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7598 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7599 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7600 ! ... Logical negation.
7601 ... != ... Numeric inequality.
7602 ... !~ ... Search pattern, substitution, or translation (negated).
7603 $! In numeric context: errno. In a string context: error string.
7604 $\" The separator which joins elements of arrays interpolated in strings.
7605 $# The output format for printed numbers. Default is %.15g or close.
7606 $$ Process number of this script. Changes in the fork()ed child process.
7607 $% The current page number of the currently selected output channel.
7608
7609 The following variables are always local to the current block:
7610
7611 $1 Match of the 1st set of parentheses in the last match (auto-local).
7612 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7613 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7614 $4 Match of the 4th set of parentheses in the last match (auto-local).
7615 $5 Match of the 5th set of parentheses in the last match (auto-local).
7616 $6 Match of the 6th set of parentheses in the last match (auto-local).
7617 $7 Match of the 7th set of parentheses in the last match (auto-local).
7618 $8 Match of the 8th set of parentheses in the last match (auto-local).
7619 $9 Match of the 9th set of parentheses in the last match (auto-local).
7620 $& The string matched by the last pattern match (auto-local).
7621 $' The string after what was matched by the last match (auto-local).
7622 $` The string before what was matched by the last match (auto-local).
7623
7624 $( The real gid of this process.
7625 $) The effective gid of this process.
7626 $* Deprecated: Set to 1 to do multiline matching within a string.
7627 $+ The last bracket matched by the last search pattern.
7628 $, The output field separator for the print operator.
7629 $- The number of lines left on the page.
7630 $. The current input line number of the last filehandle that was read.
7631 $/ The input record separator, newline by default.
7632 $0 Name of the file containing the current perl script (read/write).
7633 $: String may be broken after these characters to fill ^-lines in a format.
7634 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7635 $< The real uid of this process.
7636 $= The page length of the current output channel. Default is 60 lines.
7637 $> The effective uid of this process.
7638 $? The status returned by the last ``, pipe close or `system'.
7639 $@ The perl error message from the last eval or do @var{EXPR} command.
7640 $ARGV The name of the current file used with <> .
7641 $[ Deprecated: The index of the first element/char in an array/string.
7642 $\\ The output record separator for the print operator.
7643 $] The perl version string as displayed with perl -v.
7644 $^ The name of the current top-of-page format.
7645 $^A The current value of the write() accumulator for format() lines.
7646 $^D The value of the perl debug (-D) flags.
7647 $^E Information about the last system error other than that provided by $!.
7648 $^F The highest system file descriptor, ordinarily 2.
7649 $^H The current set of syntax checks enabled by `use strict'.
7650 $^I The value of the in-place edit extension (perl -i option).
7651 $^L What formats output to perform a formfeed. Default is \\f.
7652 $^M A buffer for emergency memory allocation when running out of memory.
7653 $^O The operating system name under which this copy of Perl was built.
7654 $^P Internal debugging flag.
7655 $^T The time the script was started. Used by -A/-M/-C file tests.
7656 $^W True if warnings are requested (perl -w flag).
7657 $^X The name under which perl was invoked (argv[0] in C-speech).
7658 $_ The default input and pattern-searching space.
7659 $| Auto-flush after write/print on current output channel? Default 0.
7660 $~ The name of the current report format.
7661 ... % ... Modulo division.
7662 ... %= ... Modulo division assignment.
7663 %ENV Contains the current environment.
7664 %INC List of files that have been require-d or do-ne.
7665 %SIG Used to set signal handlers for various signals.
7666 ... & ... Bitwise and.
7667 ... && ... Logical and.
7668 ... &&= ... Logical and assignment.
7669 ... &= ... Bitwise and assignment.
7670 ... * ... Multiplication.
7671 ... ** ... Exponentiation.
7672 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7673 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7674 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7675 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7676 ... += ... Addition assignment.
7677 , Comma operator.
7678 ... - ... Subtraction.
7679 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7680 ... -= ... Subtraction assignment.
7681 -A Access time in days since script started.
7682 -B File is a non-text (binary) file.
7683 -C Inode change time in days since script started.
7684 -M Age in days since script started.
7685 -O File is owned by real uid.
7686 -R File is readable by real uid.
7687 -S File is a socket .
7688 -T File is a text file.
7689 -W File is writable by real uid.
7690 -X File is executable by real uid.
7691 -b File is a block special file.
7692 -c File is a character special file.
7693 -d File is a directory.
7694 -e File exists .
7695 -f File is a plain file.
7696 -g File has setgid bit set.
7697 -k File has sticky bit set.
7698 -l File is a symbolic link.
7699 -o File is owned by effective uid.
7700 -p File is a named pipe (FIFO).
7701 -r File is readable by effective uid.
7702 -s File has non-zero size.
7703 -t Tests if filehandle (STDIN by default) is opened to a tty.
7704 -u File has setuid bit set.
7705 -w File is writable by effective uid.
7706 -x File is executable by effective uid.
7707 -z File has zero size.
7708 . Concatenate strings.
7709 .. Range (list context); flip/flop (scalar context) operator.
7710 .= Concatenate assignment strings
7711 ... / ... Division. /PATTERN/ioxsmg Pattern match
7712 ... /= ... Division assignment.
7713 /PATTERN/ioxsmg Pattern match.
7714 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7715 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7716 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7717 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7718 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7719 ... <= ... Numeric less than or equal to.
7720 ... <=> ... Numeric compare.
7721 ... = ... Assignment.
7722 ... == ... Numeric equality.
7723 ... =~ ... Search pattern, substitution, or translation
7724 ... > ... Numeric greater than.
7725 ... >= ... Numeric greater than or equal to.
7726 ... >> ... Bitwise shift right.
7727 ... >>= ... Bitwise shift right assignment.
7728 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7729 ?PATTERN? One-time pattern match.
7730 @ARGV Command line arguments (not including the command name - see $0).
7731 @INC List of places to look for perl scripts during do/include/use.
7732 @_ Parameter array for subroutines; result of split() unless in list context.
7733 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7734 \\0 Octal char, e.g. \\033.
7735 \\E Case modification terminator. See \\Q, \\L, and \\U.
7736 \\L Lowercase until \\E . See also \\l, lc.
7737 \\U Upcase until \\E . See also \\u, uc.
7738 \\Q Quote metacharacters until \\E . See also quotemeta.
7739 \\a Alarm character (octal 007).
7740 \\b Backspace character (octal 010).
7741 \\c Control character, e.g. \\c[ .
7742 \\e Escape character (octal 033).
7743 \\f Formfeed character (octal 014).
7744 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7745 \\n Newline character (octal 012 on most systems).
7746 \\r Return character (octal 015 on most systems).
7747 \\t Tab character (octal 011).
7748 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7749 \\x Hex character, e.g. \\x1b.
7750 ... ^ ... Bitwise exclusive or.
7751 __END__ Ends program source.
7752 __DATA__ Ends program source.
7753 __FILE__ Current (source) filename.
7754 __LINE__ Current line in current source.
7755 __PACKAGE__ Current package.
7756 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7757 ARGVOUT Output filehandle with -i flag.
7758 BEGIN { ... } Immediately executed (during compilation) piece of code.
7759 END { ... } Pseudo-subroutine executed after the script finishes.
7760 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7761 INIT { ... } Pseudo-subroutine executed before the script starts running.
7762 DATA Input filehandle for what follows after __END__ or __DATA__.
7763 accept(NEWSOCKET,GENERICSOCKET)
7764 alarm(SECONDS)
7765 atan2(X,Y)
7766 bind(SOCKET,NAME)
7767 binmode(FILEHANDLE)
7768 caller[(LEVEL)]
7769 chdir(EXPR)
7770 chmod(LIST)
7771 chop[(LIST|VAR)]
7772 chown(LIST)
7773 chroot(FILENAME)
7774 close(FILEHANDLE)
7775 closedir(DIRHANDLE)
7776 ... cmp ... String compare.
7777 connect(SOCKET,NAME)
7778 continue of { block } continue { block }. Is executed after `next' or at end.
7779 cos(EXPR)
7780 crypt(PLAINTEXT,SALT)
7781 dbmclose(%HASH)
7782 dbmopen(%HASH,DBNAME,MODE)
7783 defined(EXPR)
7784 delete($HASH{KEY})
7785 die(LIST)
7786 do { ... }|SUBR while|until EXPR executes at least once
7787 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7788 dump LABEL
7789 each(%HASH)
7790 endgrent
7791 endhostent
7792 endnetent
7793 endprotoent
7794 endpwent
7795 endservent
7796 eof[([FILEHANDLE])]
7797 ... eq ... String equality.
7798 eval(EXPR) or eval { BLOCK }
7799 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7800 exit(EXPR)
7801 exp(EXPR)
7802 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7803 fileno(FILEHANDLE)
7804 flock(FILEHANDLE,OPERATION)
7805 for (EXPR;EXPR;EXPR) { ... }
7806 foreach [VAR] (@ARRAY) { ... }
7807 fork
7808 ... ge ... String greater than or equal.
7809 getc[(FILEHANDLE)]
7810 getgrent
7811 getgrgid(GID)
7812 getgrnam(NAME)
7813 gethostbyaddr(ADDR,ADDRTYPE)
7814 gethostbyname(NAME)
7815 gethostent
7816 getlogin
7817 getnetbyaddr(ADDR,ADDRTYPE)
7818 getnetbyname(NAME)
7819 getnetent
7820 getpeername(SOCKET)
7821 getpgrp(PID)
7822 getppid
7823 getpriority(WHICH,WHO)
7824 getprotobyname(NAME)
7825 getprotobynumber(NUMBER)
7826 getprotoent
7827 getpwent
7828 getpwnam(NAME)
7829 getpwuid(UID)
7830 getservbyname(NAME,PROTO)
7831 getservbyport(PORT,PROTO)
7832 getservent
7833 getsockname(SOCKET)
7834 getsockopt(SOCKET,LEVEL,OPTNAME)
7835 gmtime(EXPR)
7836 goto LABEL
7837 ... gt ... String greater than.
7838 hex(EXPR)
7839 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7840 index(STR,SUBSTR[,OFFSET])
7841 int(EXPR)
7842 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7843 join(EXPR,LIST)
7844 keys(%HASH)
7845 kill(LIST)
7846 last [LABEL]
7847 ... le ... String less than or equal.
7848 length(EXPR)
7849 link(OLDFILE,NEWFILE)
7850 listen(SOCKET,QUEUESIZE)
7851 local(LIST)
7852 localtime(EXPR)
7853 log(EXPR)
7854 lstat(EXPR|FILEHANDLE|VAR)
7855 ... lt ... String less than.
7856 m/PATTERN/iogsmx
7857 mkdir(FILENAME,MODE)
7858 msgctl(ID,CMD,ARG)
7859 msgget(KEY,FLAGS)
7860 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7861 msgsnd(ID,MSG,FLAGS)
7862 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7863 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7864 ... ne ... String inequality.
7865 next [LABEL]
7866 oct(EXPR)
7867 open(FILEHANDLE[,EXPR])
7868 opendir(DIRHANDLE,EXPR)
7869 ord(EXPR) ASCII value of the first char of the string.
7870 pack(TEMPLATE,LIST)
7871 package NAME Introduces package context.
7872 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7873 pop(ARRAY)
7874 print [FILEHANDLE] [(LIST)]
7875 printf [FILEHANDLE] (FORMAT,LIST)
7876 push(ARRAY,LIST)
7877 q/STRING/ Synonym for 'STRING'
7878 qq/STRING/ Synonym for \"STRING\"
7879 qx/STRING/ Synonym for `STRING`
7880 rand[(EXPR)]
7881 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7882 readdir(DIRHANDLE)
7883 readlink(EXPR)
7884 recv(SOCKET,SCALAR,LEN,FLAGS)
7885 redo [LABEL]
7886 rename(OLDNAME,NEWNAME)
7887 require [FILENAME | PERL_VERSION]
7888 reset[(EXPR)]
7889 return(LIST)
7890 reverse(LIST)
7891 rewinddir(DIRHANDLE)
7892 rindex(STR,SUBSTR[,OFFSET])
7893 rmdir(FILENAME)
7894 s/PATTERN/REPLACEMENT/gieoxsm
7895 scalar(EXPR)
7896 seek(FILEHANDLE,POSITION,WHENCE)
7897 seekdir(DIRHANDLE,POS)
7898 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7899 semctl(ID,SEMNUM,CMD,ARG)
7900 semget(KEY,NSEMS,SIZE,FLAGS)
7901 semop(KEY,...)
7902 send(SOCKET,MSG,FLAGS[,TO])
7903 setgrent
7904 sethostent(STAYOPEN)
7905 setnetent(STAYOPEN)
7906 setpgrp(PID,PGRP)
7907 setpriority(WHICH,WHO,PRIORITY)
7908 setprotoent(STAYOPEN)
7909 setpwent
7910 setservent(STAYOPEN)
7911 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7912 shift[(ARRAY)]
7913 shmctl(ID,CMD,ARG)
7914 shmget(KEY,SIZE,FLAGS)
7915 shmread(ID,VAR,POS,SIZE)
7916 shmwrite(ID,STRING,POS,SIZE)
7917 shutdown(SOCKET,HOW)
7918 sin(EXPR)
7919 sleep[(EXPR)]
7920 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7921 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7922 sort [SUBROUTINE] (LIST)
7923 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7924 split[(/PATTERN/[,EXPR[,LIMIT]])]
7925 sprintf(FORMAT,LIST)
7926 sqrt(EXPR)
7927 srand(EXPR)
7928 stat(EXPR|FILEHANDLE|VAR)
7929 study[(SCALAR)]
7930 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7931 substr(EXPR,OFFSET[,LEN])
7932 symlink(OLDFILE,NEWFILE)
7933 syscall(LIST)
7934 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7935 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7936 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7937 tell[(FILEHANDLE)]
7938 telldir(DIRHANDLE)
7939 time
7940 times
7941 tr/SEARCHLIST/REPLACEMENTLIST/cds
7942 truncate(FILE|EXPR,LENGTH)
7943 umask[(EXPR)]
7944 undef[(EXPR)]
7945 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7946 unlink(LIST)
7947 unpack(TEMPLATE,EXPR)
7948 unshift(ARRAY,LIST)
7949 until (EXPR) { ... } EXPR until EXPR
7950 utime(LIST)
7951 values(%HASH)
7952 vec(EXPR,OFFSET,BITS)
7953 wait
7954 waitpid(PID,FLAGS)
7955 wantarray Returns true if the sub/eval is called in list context.
7956 warn(LIST)
7957 while (EXPR) { ... } EXPR while EXPR
7958 write[(EXPR|FILEHANDLE)]
7959 ... x ... Repeat string or array.
7960 x= ... Repetition assignment.
7961 y/SEARCHLIST/REPLACEMENTLIST/
7962 ... | ... Bitwise or.
7963 ... || ... Logical or.
7964 ~ ... Unary bitwise complement.
7965 #! OS interpreter indicator. If contains `perl', used for options, and -x.
7966 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7967 CORE:: Prefix to access builtin function if imported sub obscures it.
7968 SUPER:: Prefix to lookup for a method in @ISA classes.
7969 DESTROY Shorthand for `sub DESTROY {...}'.
7970 ... EQ ... Obsolete synonym of `eq'.
7971 ... GE ... Obsolete synonym of `ge'.
7972 ... GT ... Obsolete synonym of `gt'.
7973 ... LE ... Obsolete synonym of `le'.
7974 ... LT ... Obsolete synonym of `lt'.
7975 ... NE ... Obsolete synonym of `ne'.
7976 abs [ EXPR ] absolute value
7977 ... and ... Low-precedence synonym for &&.
7978 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7979 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
7980 chr Converts a number to char with the same ordinal.
7981 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7982 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7983 exists $HASH{KEY} True if the key exists.
7984 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7985 formline PICTURE, LIST Backdoor into \"format\" processing.
7986 glob EXPR Synonym of <EXPR>.
7987 lc [ EXPR ] Returns lowercased EXPR.
7988 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
7989 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
7990 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
7991 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
7992 not ... Low-precedence synonym for ! - negation.
7993 ... or ... Low-precedence synonym for ||.
7994 pos STRING Set/Get end-position of the last match over this string, see \\G.
7995 quotemeta [ EXPR ] Quote regexp metacharacters.
7996 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
7997 readline FH Synonym of <FH>.
7998 readpipe CMD Synonym of `CMD`.
7999 ref [ EXPR ] Type of EXPR when dereferenced.
8000 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
8001 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
8002 tied Returns internal object for a tied data.
8003 uc [ EXPR ] Returns upcased EXPR.
8004 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
8005 untie VAR Unlink an object from a simple Perl variable.
8006 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
8007 ... xor ... Low-precedence synonym for exclusive or.
8008 prototype \\&SUB Returns prototype of the function given a reference.
8009 =head1 Top-level heading.
8010 =head2 Second-level heading.
8011 =head3 Third-level heading (is there such?).
8012 =over [ NUMBER ] Start list.
8013 =item [ TITLE ] Start new item in the list.
8014 =back End list.
8015 =cut Switch from POD to Perl.
8016 =pod Switch from Perl to POD.
8017 ")
8018
8019 (defun cperl-switch-to-doc-buffer (&optional interactive)
8020 "Go to the perl documentation buffer and insert the documentation."
8021 (interactive "p")
8022 (let ((buf (get-buffer-create cperl-doc-buffer)))
8023 (if interactive
8024 (switch-to-buffer-other-window buf)
8025 (set-buffer buf))
8026 (if (= (buffer-size) 0)
8027 (progn
8028 (insert (documentation-property 'cperl-short-docs
8029 'variable-documentation))
8030 (setq buffer-read-only t)))))
8031
8032 (defun cperl-beautify-regexp-piece (b e embed level)
8033 ;; b is before the starting delimiter, e before the ending
8034 ;; e should be a marker, may be changed, but remains "correct".
8035 ;; EMBED is nil if we process the whole REx.
8036 ;; The REx is guaranteed to have //x
8037 ;; LEVEL shows how many levels deep to go
8038 ;; position at enter and at leave is not defined
8039 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
8040 (if embed
8041 (progn
8042 (goto-char b)
8043 (setq c (if (eq embed t) (current-indentation) (current-column)))
8044 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
8045 (forward-char 2)
8046 (delete-char 1)
8047 (forward-char 1))
8048 ((looking-at "(\\?[^a-zA-Z]")
8049 (forward-char 3))
8050 ((looking-at "(\\?") ; (?i)
8051 (forward-char 2))
8052 (t
8053 (forward-char 1))))
8054 (goto-char (1+ b))
8055 (setq c (1- (current-column))))
8056 (setq c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
8057 (or (looking-at "[ \t]*[\n#]")
8058 (progn
8059 (insert "\n")))
8060 (goto-char e)
8061 (beginning-of-line)
8062 (if (re-search-forward "[^ \t]" e t)
8063 (progn ; Something before the ending delimiter
8064 (goto-char e)
8065 (delete-horizontal-space)
8066 (insert "\n")
8067 (cperl-make-indent c)
8068 (set-marker e (point))))
8069 (goto-char b)
8070 (end-of-line 2)
8071 (while (< (point) (marker-position e))
8072 (beginning-of-line)
8073 (setq s (point)
8074 inline t)
8075 (skip-chars-forward " \t")
8076 (delete-region s (point))
8077 (cperl-make-indent c1)
8078 (while (and
8079 inline
8080 (looking-at
8081 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8082 "\\|" ; Embedded variable
8083 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8084 "\\|" ; $ ^
8085 "[$^]"
8086 "\\|" ; simple-code simple-code*?
8087 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8088 "\\|" ; Class
8089 "\\(\\[\\)" ; 6
8090 "\\|" ; Grouping
8091 "\\((\\(\\?\\)?\\)" ; 7 8
8092 "\\|" ; |
8093 "\\(|\\)"))) ; 9
8094 (goto-char (match-end 0))
8095 (setq spaces t)
8096 (cond ((match-beginning 1) ; Alphanum word + junk
8097 (forward-char -1))
8098 ((or (match-beginning 3) ; $ab[12]
8099 (and (match-beginning 5) ; X* X+ X{2,3}
8100 (eq (preceding-char) ?\{)))
8101 (forward-char -1)
8102 (forward-sexp 1))
8103 ((and ; [], already syntaxified
8104 (match-beginning 6)
8105 cperl-regexp-scan
8106 cperl-use-syntax-table-text-property)
8107 (forward-char -1)
8108 (forward-sexp 1)
8109 (or (eq (preceding-char) ?\])
8110 (error "[]-group not terminated"))
8111 (re-search-forward
8112 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8113 ((match-beginning 6) ; []
8114 (setq tmp (point))
8115 (if (looking-at "\\^?\\]")
8116 (goto-char (match-end 0)))
8117 ;; XXXX POSIX classes?!
8118 (while (and (not pos)
8119 (re-search-forward "\\[:\\|\\]" e t))
8120 (if (eq (preceding-char) ?:)
8121 (or (re-search-forward ":\\]" e t)
8122 (error "[:POSIX:]-group in []-group not terminated"))
8123 (setq pos t)))
8124 (or (eq (preceding-char) ?\])
8125 (error "[]-group not terminated"))
8126 (re-search-forward
8127 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8128 ((match-beginning 7) ; ()
8129 (goto-char (match-beginning 0))
8130 (setq pos (current-column))
8131 (or (eq pos c1)
8132 (progn
8133 (delete-horizontal-space)
8134 (insert "\n")
8135 (cperl-make-indent c1)))
8136 (setq tmp (point))
8137 (forward-sexp 1)
8138 ;; (or (forward-sexp 1)
8139 ;; (progn
8140 ;; (goto-char tmp)
8141 ;; (error "()-group not terminated")))
8142 (set-marker m (1- (point)))
8143 (set-marker m1 (point))
8144 (if (= level 1)
8145 (if (progn ; indent rigidly if multiline
8146 ;; In fact does not make a lot of sense, since
8147 ;; the starting position can be already lost due
8148 ;; to insertion of "\n" and " "
8149 (goto-char tmp)
8150 (search-forward "\n" m1 t))
8151 (indent-rigidly (point) m1 (- c1 pos)))
8152 (setq level (1- level))
8153 (cond
8154 ((not (match-beginning 8))
8155 (cperl-beautify-regexp-piece tmp m t level))
8156 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8157 t)
8158 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8159 (goto-char (+ 2 tmp))
8160 (forward-sexp 1)
8161 (cperl-beautify-regexp-piece (point) m t level))
8162 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8163 (goto-char (+ 3 tmp))
8164 (cperl-beautify-regexp-piece (point) m t level))
8165 (t
8166 (cperl-beautify-regexp-piece tmp m t level))))
8167 (goto-char m1)
8168 (cond ((looking-at "[*+?]\\??")
8169 (goto-char (match-end 0)))
8170 ((eq (following-char) ?\{)
8171 (forward-sexp 1)
8172 (if (eq (following-char) ?\?)
8173 (forward-char))))
8174 (skip-chars-forward " \t")
8175 (setq spaces nil)
8176 (if (looking-at "[#\n]")
8177 (progn
8178 (or (eolp) (indent-for-comment))
8179 (beginning-of-line 2))
8180 (delete-horizontal-space)
8181 (insert "\n"))
8182 (end-of-line)
8183 (setq inline nil))
8184 ((match-beginning 9) ; |
8185 (forward-char -1)
8186 (setq tmp (point))
8187 (beginning-of-line)
8188 (if (re-search-forward "[^ \t]" tmp t)
8189 (progn
8190 (goto-char tmp)
8191 (delete-horizontal-space)
8192 (insert "\n"))
8193 ;; first at line
8194 (delete-region (point) tmp))
8195 (cperl-make-indent c)
8196 (forward-char 1)
8197 (skip-chars-forward " \t")
8198 (setq spaces nil)
8199 (if (looking-at "[#\n]")
8200 (beginning-of-line 2)
8201 (delete-horizontal-space)
8202 (insert "\n"))
8203 (end-of-line)
8204 (setq inline nil)))
8205 (or (looking-at "[ \t\n]")
8206 (not spaces)
8207 (insert " "))
8208 (skip-chars-forward " \t"))
8209 (or (looking-at "[#\n]")
8210 (error "Unknown code `%s' in a regexp"
8211 (buffer-substring (point) (1+ (point)))))
8212 (and inline (end-of-line 2)))
8213 ;; Special-case the last line of group
8214 (if (and (>= (point) (marker-position e))
8215 (/= (current-indentation) c))
8216 (progn
8217 (beginning-of-line)
8218 (cperl-make-indent c)))))
8219
8220 (defun cperl-make-regexp-x ()
8221 ;; Returns position of the start
8222 ;; XXX this is called too often! Need to cache the result!
8223 (save-excursion
8224 (or cperl-use-syntax-table-text-property
8225 (error "I need to have a regexp marked!"))
8226 ;; Find the start
8227 (if (looking-at "\\s|")
8228 nil ; good already
8229 (if (or (looking-at "\\([smy]\\|qr\\)\\s|")
8230 (and (eq (preceding-char) ?q)
8231 (looking-at "\\(r\\)\\s|")))
8232 (goto-char (match-end 1))
8233 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8234 ;;(forward-char 1)
8235 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8236 (sub-p (eq (preceding-char) ?s)) s)
8237 (forward-sexp 1)
8238 (set-marker e (1- (point)))
8239 (setq delim (preceding-char))
8240 (if (and sub-p (eq delim (char-after (- (point) 2))))
8241 (error "Possible s/blah// - do not know how to deal with"))
8242 (if sub-p (forward-sexp 1))
8243 (if (looking-at "\\sw*x")
8244 (setq have-x t)
8245 (insert "x"))
8246 ;; Protect fragile " ", "#"
8247 (if have-x nil
8248 (goto-char (1+ b))
8249 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8250 (forward-char -1)
8251 (insert "\\")
8252 (forward-char 1)))
8253 b)))
8254
8255 (defun cperl-beautify-regexp (&optional deep)
8256 "Do it. (Experimental, may change semantics, recheck the result.)
8257 We suppose that the regexp is scanned already."
8258 (interactive "P")
8259 (setq deep (if deep (prefix-numeric-value deep) -1))
8260 (save-excursion
8261 (goto-char (cperl-make-regexp-x))
8262 (let ((b (point)) (e (make-marker)))
8263 (forward-sexp 1)
8264 (set-marker e (1- (point)))
8265 (cperl-beautify-regexp-piece b e nil deep))))
8266
8267 (defun cperl-regext-to-level-start ()
8268 "Goto start of an enclosing group in regexp.
8269 We suppose that the regexp is scanned already."
8270 (interactive)
8271 (let ((limit (cperl-make-regexp-x)) done)
8272 (while (not done)
8273 (or (eq (following-char) ?\()
8274 (search-backward "(" (1+ limit) t)
8275 (error "Cannot find `(' which starts a group"))
8276 (setq done
8277 (save-excursion
8278 (skip-chars-backward "\\")
8279 (looking-at "\\(\\\\\\\\\\)*(")))
8280 (or done (forward-char -1)))))
8281
8282 (defun cperl-contract-level ()
8283 "Find an enclosing group in regexp and contract it.
8284 \(Experimental, may change semantics, recheck the result.)
8285 We suppose that the regexp is scanned already."
8286 (interactive)
8287 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8288 (cperl-regext-to-level-start)
8289 (let ((b (point)) (e (make-marker)) c)
8290 (forward-sexp 1)
8291 (set-marker e (1- (point)))
8292 (goto-char b)
8293 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8294 (cond
8295 ((match-beginning 1) ; #-comment
8296 (or c (setq c (current-indentation)))
8297 (beginning-of-line 2) ; Skip
8298 (cperl-make-indent c))
8299 (t
8300 (delete-char -1)
8301 (just-one-space))))))
8302
8303 (defun cperl-contract-levels ()
8304 "Find an enclosing group in regexp and contract all the kids.
8305 \(Experimental, may change semantics, recheck the result.)
8306 We suppose that the regexp is scanned already."
8307 (interactive)
8308 (save-excursion
8309 (condition-case nil
8310 (cperl-regext-to-level-start)
8311 (error ; We are outside outermost group
8312 (goto-char (cperl-make-regexp-x))))
8313 (let ((b (point)) (e (make-marker)) s c)
8314 (forward-sexp 1)
8315 (set-marker e (1- (point)))
8316 (goto-char (1+ b))
8317 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8318 (cond
8319 ((match-beginning 1) ; Skip
8320 nil)
8321 (t ; Group
8322 (cperl-contract-level)))))))
8323
8324 (defun cperl-beautify-level (&optional deep)
8325 "Find an enclosing group in regexp and beautify it.
8326 \(Experimental, may change semantics, recheck the result.)
8327 We suppose that the regexp is scanned already."
8328 (interactive "P")
8329 (setq deep (if deep (prefix-numeric-value deep) -1))
8330 (save-excursion
8331 (cperl-regext-to-level-start)
8332 (let ((b (point)) (e (make-marker)))
8333 (forward-sexp 1)
8334 (set-marker e (1- (point)))
8335 (cperl-beautify-regexp-piece b e 'level deep))))
8336
8337 (defun cperl-invert-if-unless-modifiers ()
8338 "Change `B if A;' into `if (A) {B}' etc if possible.
8339 \(Unfinished.)"
8340 (interactive)
8341 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8342 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8343 (and (= (char-syntax (preceding-char)) ?w)
8344 (forward-sexp -1))
8345 (setq pre-if (point))
8346 (cperl-backward-to-start-of-expr)
8347 (setq pre-B (point))
8348 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8349 (cperl-forward-to-end-of-expr)
8350 (setq post-A (point))
8351 (goto-char pre-if)
8352 (or (looking-at w-rex)
8353 ;; Find the position
8354 (progn (goto-char post-A)
8355 (while (and
8356 (not (looking-at w-rex))
8357 (> (point) pre-B))
8358 (forward-sexp -1))
8359 (setq pre-if (point))))
8360 (or (looking-at w-rex)
8361 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8362 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8363 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8364 ;; First, simple part: find code boundaries
8365 (forward-sexp 1)
8366 (setq post-if (point))
8367 (forward-sexp -2)
8368 (forward-sexp 1)
8369 (setq post-B (point))
8370 (cperl-backward-to-start-of-expr)
8371 (setq pre-B (point))
8372 (setq B (buffer-substring pre-B post-B))
8373 (goto-char pre-if)
8374 (forward-sexp 2)
8375 (forward-sexp -1)
8376 ;; May be after $, @, $# etc of a variable
8377 (skip-chars-backward "$@%#")
8378 (setq pre-A (point))
8379 (cperl-forward-to-end-of-expr)
8380 (setq post-A (point))
8381 (setq A (buffer-substring pre-A post-A))
8382 ;; Now modify (from end, to not break the stuff)
8383 (skip-chars-forward " \t;")
8384 (delete-region pre-A (point)) ; we move to pre-A
8385 (insert "\n" B ";\n}")
8386 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8387 (delete-region pre-if post-if)
8388 (delete-region pre-B post-B)
8389 (goto-char pre-B)
8390 (insert if-string " (" A ") {")
8391 (setq post-B (point))
8392 (if (looking-at "[ \t]+$")
8393 (delete-horizontal-space)
8394 (if (looking-at "[ \t]*#")
8395 (cperl-indent-for-comment)
8396 (just-one-space)))
8397 (forward-line 1)
8398 (if (looking-at "[ \t]*$")
8399 (progn ; delete line
8400 (delete-horizontal-space)
8401 (delete-region (point) (1+ (point)))))
8402 (cperl-indent-line)
8403 (goto-char (1- post-B))
8404 (forward-sexp 1)
8405 (cperl-indent-line)
8406 (goto-char pre-B)))
8407
8408 (defun cperl-invert-if-unless ()
8409 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8410 If the cursor is not on the leading keyword of the BLOCK flavor of
8411 construct, will assume it is the STATEMENT flavor, so will try to find
8412 the appropriate statement modifier."
8413 (interactive)
8414 (and (= (char-syntax (preceding-char)) ?w)
8415 (forward-sexp -1))
8416 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8417 (let ((pre-if (point))
8418 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8419 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8420 (forward-sexp 2)
8421 (setq post-A (point))
8422 (forward-sexp -1)
8423 (setq pre-A (point))
8424 (setq is-block (and (eq (following-char) ?\( )
8425 (save-excursion
8426 (condition-case nil
8427 (progn
8428 (forward-sexp 2)
8429 (forward-sexp -1)
8430 (eq (following-char) ?\{ ))
8431 (error nil)))))
8432 (if is-block
8433 (progn
8434 (goto-char post-A)
8435 (forward-sexp 1)
8436 (setq post-B (point))
8437 (forward-sexp -1)
8438 (setq pre-B (point))
8439 (if (and (eq (following-char) ?\{ )
8440 (progn
8441 (cperl-backward-to-noncomment post-A)
8442 (eq (preceding-char) ?\) )))
8443 (if (condition-case nil
8444 (progn
8445 (goto-char post-B)
8446 (forward-sexp 1)
8447 (forward-sexp -1)
8448 (looking-at "\\<els\\(e\\|if\\)\\>"))
8449 (error nil))
8450 (error
8451 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8452 (goto-char (1- post-B))
8453 (cperl-backward-to-noncomment pre-B)
8454 (if (eq (preceding-char) ?\;)
8455 (forward-char -1))
8456 (setq end-B-code (point))
8457 (goto-char pre-B)
8458 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8459 (setq p (match-beginning 0)
8460 A (buffer-substring p (match-end 0))
8461 state (parse-partial-sexp pre-B p))
8462 (or (nth 3 state)
8463 (nth 4 state)
8464 (nth 5 state)
8465 (error "`%s' inside `%s' BLOCK" A if-string))
8466 (goto-char (match-end 0)))
8467 ;; Finally got it
8468 (goto-char (1+ pre-B))
8469 (skip-chars-forward " \t\n")
8470 (setq B (buffer-substring (point) end-B-code))
8471 (goto-char end-B-code)
8472 (or (looking-at ";?[ \t\n]*}")
8473 (progn
8474 (skip-chars-forward "; \t\n")
8475 (setq B-comment
8476 (buffer-substring (point) (1- post-B)))))
8477 (and (equal B "")
8478 (setq B "1"))
8479 (goto-char (1- post-A))
8480 (cperl-backward-to-noncomment pre-A)
8481 (or (looking-at "[ \t\n]*)")
8482 (goto-char (1- post-A)))
8483 (setq p (point))
8484 (goto-char (1+ pre-A))
8485 (skip-chars-forward " \t\n")
8486 (setq A (buffer-substring (point) p))
8487 (delete-region pre-B post-B)
8488 (delete-region pre-A post-A)
8489 (goto-char pre-if)
8490 (insert B " ")
8491 (and B-comment (insert B-comment " "))
8492 (just-one-space)
8493 (forward-word 1)
8494 (setq pre-A (point))
8495 (insert " " A ";")
8496 (delete-horizontal-space)
8497 (setq post-B (point))
8498 (if (looking-at "#")
8499 (indent-for-comment))
8500 (goto-char post-B)
8501 (forward-char -1)
8502 (delete-horizontal-space)
8503 (goto-char pre-A)
8504 (just-one-space)
8505 (goto-char pre-if)
8506 (setq pre-A (set-marker (make-marker) pre-A))
8507 (while (<= (point) (marker-position pre-A))
8508 (cperl-indent-line)
8509 (forward-line 1))
8510 (goto-char (marker-position pre-A))
8511 (if B-comment
8512 (progn
8513 (forward-line -1)
8514 (indent-for-comment)
8515 (goto-char (marker-position pre-A)))))
8516 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8517 ;; (error "`%s' not with an (EXPR)" if-string)
8518 (forward-sexp -1)
8519 (cperl-invert-if-unless-modifiers)))
8520 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8521 (cperl-invert-if-unless-modifiers)))
8522
8523 ;;; By Anthony Foiani <afoiani@uswest.com>
8524 ;;; Getting help on modules in C-h f ?
8525 ;;; This is a modified version of `man'.
8526 ;;; Need to teach it how to lookup functions
8527 ;;;###autoload
8528 (defun cperl-perldoc (word)
8529 "Run `perldoc' on WORD."
8530 (interactive
8531 (list (let* ((default-entry (cperl-word-at-point))
8532 (input (read-string
8533 (format "perldoc entry%s: "
8534 (if (string= default-entry "")
8535 ""
8536 (format " (default %s)" default-entry))))))
8537 (if (string= input "")
8538 (if (string= default-entry "")
8539 (error "No perldoc args given")
8540 default-entry)
8541 input))))
8542 (require 'man)
8543 (let* ((case-fold-search nil)
8544 (is-func (and
8545 (string-match "^[a-z]+$" word)
8546 (string-match (concat "^" word "\\>")
8547 (documentation-property
8548 'cperl-short-docs
8549 'variable-documentation))))
8550 (Man-switches "")
8551 (manual-program (if is-func "perldoc -f" "perldoc")))
8552 (cond
8553 ((featurep 'xemacs)
8554 (let ((Manual-program "perldoc")
8555 (Manual-switches (if is-func (list "-f"))))
8556 (manual-entry word)))
8557 (t
8558 (Man-getpage-in-background word)))))
8559
8560 ;;;###autoload
8561 (defun cperl-perldoc-at-point ()
8562 "Run a `perldoc' on the word around point."
8563 (interactive)
8564 (cperl-perldoc (cperl-word-at-point)))
8565
8566 (defcustom pod2man-program "pod2man"
8567 "*File name for `pod2man'."
8568 :type 'file
8569 :group 'cperl)
8570
8571 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8572 (defun cperl-pod-to-manpage ()
8573 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8574 (interactive)
8575 (require 'man)
8576 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8577 (bufname (concat "Man " buffer-file-name))
8578 (buffer (generate-new-buffer bufname)))
8579 (with-current-buffer buffer
8580 (let ((process-environment (copy-sequence process-environment)))
8581 ;; Prevent any attempt to use display terminal fanciness.
8582 (setenv "TERM" "dumb")
8583 (set-process-sentinel
8584 (start-process pod2man-program buffer "sh" "-c"
8585 (format (cperl-pod2man-build-command) pod2man-args))
8586 'Man-bgproc-sentinel)))))
8587
8588 ;;; Updated version by him too
8589 (defun cperl-build-manpage ()
8590 "Create a virtual manpage in Emacs from the POD in the file."
8591 (interactive)
8592 (require 'man)
8593 (cond
8594 ((featurep 'xemacs)
8595 (let ((Manual-program "perldoc"))
8596 (manual-entry buffer-file-name)))
8597 (t
8598 (let* ((manual-program "perldoc")
8599 (Man-switches ""))
8600 (Man-getpage-in-background buffer-file-name)))))
8601
8602 (defun cperl-pod2man-build-command ()
8603 "Builds the entire background manpage and cleaning command."
8604 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8605 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8606 (while (and flist (car flist))
8607 (let ((pcom (car (car flist)))
8608 (pargs (cdr (car flist))))
8609 (setq command
8610 (concat command " | " pcom " "
8611 (mapconcat '(lambda (phrase)
8612 (if (not (stringp phrase))
8613 (error "Malformed Man-filter-list"))
8614 phrase)
8615 pargs " ")))
8616 (setq flist (cdr flist))))
8617 command))
8618
8619
8620 (defun cperl-next-interpolated-REx-1 ()
8621 "Move point to next REx which has interpolated parts without //o.
8622 Skips RExes consisting of one interpolated variable.
8623
8624 Note that skipped RExen are not performance hits."
8625 (interactive "")
8626 (cperl-next-interpolated-REx 1))
8627
8628 (defun cperl-next-interpolated-REx-0 ()
8629 "Move point to next REx which has interpolated parts without //o."
8630 (interactive "")
8631 (cperl-next-interpolated-REx 0))
8632
8633 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8634 "Move point to next REx which has interpolated parts.
8635 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8636 point and the limit of search (default to point and end of buffer).
8637
8638 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8639 semantic may be used as a numeric argument.
8640
8641 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8642 a result of qr//, this is not a performance hit), t for the rest."
8643 (interactive "P")
8644 (if (numberp skip) (setq skip (list 0 skip)))
8645 (or beg (setq beg (point)))
8646 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8647 (let (pp)
8648 (and (eq (get-text-property beg 'syntax-type) 'string)
8649 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8650 (cperl-map-pods-heres
8651 (function (lambda (s e p)
8652 (if (memq (get-text-property s 'REx-interpolated) skip)
8653 t
8654 (setq pp s)
8655 nil))) ; nil stops
8656 'REx-interpolated beg limit)
8657 (if pp (goto-char pp)
8658 (message "No more interpolated REx"))))
8659
8660 ;;; Initial version contributed by Trey Belew
8661 (defun cperl-here-doc-spell (&optional beg end)
8662 "Spell-check HERE-documents in the Perl buffer.
8663 If a region is highlighted, restricts to the region."
8664 (interactive "")
8665 (cperl-pod-spell t beg end))
8666
8667 (defun cperl-pod-spell (&optional do-heres beg end)
8668 "Spell-check POD documentation.
8669 If invoked with prefix argument, will do HERE-DOCs instead.
8670 If a region is highlighted, restricts to the region."
8671 (interactive "P")
8672 (save-excursion
8673 (let (beg end)
8674 (if (cperl-mark-active)
8675 (setq beg (min (mark) (point))
8676 end (max (mark) (point)))
8677 (setq beg (point-min)
8678 end (point-max)))
8679 (cperl-map-pods-heres (function
8680 (lambda (s e p)
8681 (if do-heres
8682 (setq e (save-excursion
8683 (goto-char e)
8684 (forward-line -1)
8685 (point))))
8686 (ispell-region s e)
8687 t))
8688 (if do-heres 'here-doc-group 'in-pod)
8689 beg end))))
8690
8691 (defun cperl-map-pods-heres (func &optional prop s end)
8692 "Executes a function over regions of pods or here-documents.
8693 PROP is the text-property to search for; default to `in-pod'. Stop when
8694 function returns nil."
8695 (let (pos posend has-prop (cont t))
8696 (or prop (setq prop 'in-pod))
8697 (or s (setq s (point-min)))
8698 (or end (setq end (point-max)))
8699 (cperl-update-syntaxification end end)
8700 (save-excursion
8701 (goto-char (setq pos s))
8702 (while (and cont (< pos end))
8703 (setq has-prop (get-text-property pos prop))
8704 (setq posend (next-single-property-change pos prop nil end))
8705 (and has-prop
8706 (setq cont (funcall func pos posend prop)))
8707 (setq pos posend)))))
8708
8709 ;;; Based on code by Masatake YAMATO:
8710 (defun cperl-get-here-doc-region (&optional pos pod)
8711 "Return HERE document region around the point.
8712 Return nil if the point is not in a HERE document region. If POD is non-nil,
8713 will return a POD section if point is in a POD section."
8714 (or pos (setq pos (point)))
8715 (cperl-update-syntaxification pos pos)
8716 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8717 (and pod
8718 (eq 'pod (get-text-property pos 'syntax-type))))
8719 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8720 (e (next-single-property-change pos 'syntax-type)))
8721 (cons b (or e (point-max))))))
8722
8723 (defun cperl-narrow-to-here-doc (&optional pos)
8724 "Narrows editing region to the HERE-DOC at POS.
8725 POS defaults to the point."
8726 (interactive "d")
8727 (or pos (setq pos (point)))
8728 (let ((p (cperl-get-here-doc-region pos)))
8729 (or p (error "Not inside a HERE document"))
8730 (narrow-to-region (car p) (cdr p))
8731 (message
8732 "When you are finished with narrow editing, type C-x n w")))
8733
8734 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8735 "Select the HERE-DOC (or POD section) at POS.
8736 POS defaults to the point."
8737 (interactive "d")
8738 (let ((p (cperl-get-here-doc-region pos t)))
8739 (if p
8740 (progn
8741 (goto-char (car p))
8742 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8743 (message "I do not think POS is in POD or a HERE-doc..."))))
8744
8745 (defun cperl-facemenu-add-face-function (face end)
8746 "A callback to process user-initiated font-change requests.
8747 Translates `bold', `italic', and `bold-italic' requests to insertion of
8748 corresponding POD directives, and `underline' to C<> POD directive.
8749
8750 Such requests are usually bound to M-o LETTER."
8751 (or (get-text-property (point) 'in-pod)
8752 (error "Faces can only be set within POD"))
8753 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8754 (cdr (or (assq face '((bold . "B<")
8755 (italic . "I<")
8756 (bold-italic . "B<I<")
8757 (underline . "C<")))
8758 (error "Face %s not configured for cperl-mode"
8759 face))))
8760 \f
8761 (defun cperl-time-fontification (&optional l step lim)
8762 "Times how long it takes to do incremental fontification in a region.
8763 L is the line to start at, STEP is the number of lines to skip when
8764 doing next incremental fontification, LIM is the maximal number of
8765 incremental fontification to perform. Messages are accumulated in
8766 *Messages* buffer.
8767
8768 May be used for pinpointing which construct slows down buffer fontification:
8769 start with default arguments, then refine the slowdown regions."
8770 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8771 (or l (setq l 1))
8772 (or step (setq step 500))
8773 (or lim (setq lim 40))
8774 (let* ((timems (function (lambda ()
8775 (let ((tt (current-time)))
8776 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8777 (tt (funcall timems)) (c 0) delta tot)
8778 (goto-char (point-min))
8779 (forward-line (1- l))
8780 (cperl-mode)
8781 (setq tot (- (- tt (setq tt (funcall timems)))))
8782 (message "cperl-mode at %s: %s" l tot)
8783 (while (and (< c lim) (not (eobp)))
8784 (forward-line step)
8785 (setq l (+ l step))
8786 (setq c (1+ c))
8787 (cperl-update-syntaxification (point) (point))
8788 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8789 (message "to %s:%6s,%7s" l delta tot))
8790 tot))
8791
8792 (defvar font-lock-cache-position)
8793
8794 (defun cperl-emulate-lazy-lock (&optional window-size)
8795 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8796 Start fontifying the buffer from the start (or end) using the given
8797 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8798 goes backwards; default is -50. This function is not CPerl-specific; it
8799 may be used to debug problems with delayed incremental fontification."
8800 (interactive
8801 "nSize of window for incremental fontification, negative goes backwards: ")
8802 (or window-size (setq window-size -50))
8803 (let ((pos (if (> window-size 0)
8804 (point-min)
8805 (point-max)))
8806 p)
8807 (goto-char pos)
8808 (normal-mode)
8809 ;; Why needed??? With older font-locks???
8810 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8811 (while (if (> window-size 0)
8812 (< pos (point-max))
8813 (> pos (point-min)))
8814 (setq p (progn
8815 (forward-line window-size)
8816 (point)))
8817 (font-lock-fontify-region (min p pos) (max p pos))
8818 (setq pos p))))
8819
8820 \f
8821 (defun cperl-lazy-install ()) ; Avoid a warning
8822 (defun cperl-lazy-unstall ()) ; Avoid a warning
8823
8824 (if (fboundp 'run-with-idle-timer)
8825 (progn
8826 (defvar cperl-help-shown nil
8827 "Non-nil means that the help was already shown now.")
8828
8829 (defvar cperl-lazy-installed nil
8830 "Non-nil means that the lazy-help handlers are installed now.")
8831
8832 (defun cperl-lazy-install ()
8833 "Switches on Auto-Help on Perl constructs (put in the message area).
8834 Delay of auto-help controlled by `cperl-lazy-help-time'."
8835 (interactive)
8836 (make-local-variable 'cperl-help-shown)
8837 (if (and (cperl-val 'cperl-lazy-help-time)
8838 (not cperl-lazy-installed))
8839 (progn
8840 (add-hook 'post-command-hook 'cperl-lazy-hook)
8841 (run-with-idle-timer
8842 (cperl-val 'cperl-lazy-help-time 1000000 5)
8843 t
8844 'cperl-get-help-defer)
8845 (setq cperl-lazy-installed t))))
8846
8847 (defun cperl-lazy-unstall ()
8848 "Switches off Auto-Help on Perl constructs (put in the message area).
8849 Delay of auto-help controlled by `cperl-lazy-help-time'."
8850 (interactive)
8851 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8852 (cancel-function-timers 'cperl-get-help-defer)
8853 (setq cperl-lazy-installed nil))
8854
8855 (defun cperl-lazy-hook ()
8856 (setq cperl-help-shown nil))
8857
8858 (defun cperl-get-help-defer ()
8859 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8860 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8861 (cperl-get-help)
8862 (setq cperl-help-shown t))))
8863 (cperl-lazy-install)))
8864
8865
8866 ;;; Plug for wrong font-lock:
8867
8868 (defun cperl-font-lock-unfontify-region-function (beg end)
8869 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8870 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8871 before-change-functions after-change-functions
8872 deactivate-mark buffer-file-name buffer-file-truename)
8873 (remove-text-properties beg end '(face nil))
8874 (if (and (not modified) (buffer-modified-p))
8875 (set-buffer-modified-p nil))))
8876
8877 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8878 "Extends the region to safe positions, then calls the default function.
8879 Newer `font-lock's can do it themselves.
8880 We unwind only as far as needed for fontification. Syntaxification may
8881 do extra unwind via `cperl-unwind-to-safe'."
8882 (save-excursion
8883 (goto-char beg)
8884 (while (and beg
8885 (progn
8886 (beginning-of-line)
8887 (eq (get-text-property (setq beg (point)) 'syntax-type)
8888 'multiline)))
8889 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8890 (goto-char beg)))
8891 (setq beg (point))
8892 (goto-char end)
8893 (while (and end
8894 (progn
8895 (or (bolp) (condition-case nil
8896 (forward-line 1)
8897 (error nil)))
8898 (eq (get-text-property (setq end (point)) 'syntax-type)
8899 'multiline)))
8900 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8901 (goto-char end))
8902 (setq end (point)))
8903 (font-lock-default-fontify-region beg end loudly))
8904
8905 (defvar cperl-d-l nil)
8906 (defun cperl-fontify-syntaxically (end)
8907 ;; Some vars for debugging only
8908 ;; (message "Syntaxifying...")
8909 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8910 (istate (car cperl-syntax-state))
8911 start from-start edebug-backtrace-buffer)
8912 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8913 (progn
8914 (require 'edebug)
8915 (let ((f 'edebug-backtrace))
8916 (funcall f)))) ; Avoid compile-time warning
8917 (or cperl-syntax-done-to
8918 (setq cperl-syntax-done-to (point-min)
8919 from-start t))
8920 (setq start (if (and cperl-hook-after-change
8921 (not from-start))
8922 cperl-syntax-done-to ; Fontify without change; ignore start
8923 ;; Need to forget what is after `start'
8924 (min cperl-syntax-done-to (point))))
8925 (goto-char start)
8926 (beginning-of-line)
8927 (setq start (point))
8928 (and cperl-syntaxify-unwind
8929 (setq end (cperl-unwind-to-safe t end)
8930 start (point)))
8931 (and (> end start)
8932 (setq cperl-syntax-done-to start) ; In case what follows fails
8933 (cperl-find-pods-heres start end t nil t))
8934 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8935 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8936 dbg iend start end idone cperl-syntax-done-to
8937 istate (car cperl-syntax-state))) ; For debugging
8938 nil)) ; Do not iterate
8939
8940 (defun cperl-fontify-update (end)
8941 (let ((pos (point-min)) prop posend)
8942 (setq end (point-max))
8943 (while (< pos end)
8944 (setq prop (get-text-property pos 'cperl-postpone)
8945 posend (next-single-property-change pos 'cperl-postpone nil end))
8946 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8947 (setq pos posend)))
8948 nil) ; Do not iterate
8949
8950 (defun cperl-fontify-update-bad (end)
8951 ;; Since fontification happens with different region than syntaxification,
8952 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8953 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8954 (if prop
8955 (setq pos (or (cperl-beginning-of-property
8956 (cperl-1+ pos) 'cperl-postpone)
8957 (point-min))))
8958 (while (< pos end)
8959 (setq posend (next-single-property-change pos 'cperl-postpone))
8960 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8961 (setq pos posend)
8962 (setq prop (get-text-property pos 'cperl-postpone))))
8963 nil) ; Do not iterate
8964
8965 ;; Called when any modification is made to buffer text.
8966 (defun cperl-after-change-function (beg end old-len)
8967 ;; We should have been informed about changes by `font-lock'. Since it
8968 ;; does not inform as which calls are defered, do it ourselves
8969 (if cperl-syntax-done-to
8970 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8971
8972 (defun cperl-update-syntaxification (from to)
8973 (if (and cperl-use-syntax-table-text-property
8974 cperl-syntaxify-by-font-lock
8975 (or (null cperl-syntax-done-to)
8976 (< cperl-syntax-done-to to)))
8977 (progn
8978 (save-excursion
8979 (goto-char from)
8980 (cperl-fontify-syntaxically to)))))
8981
8982 (defvar cperl-version
8983 (let ((v "Revision: 6.2"))
8984 (string-match ":\\s *\\([0-9.]+\\)" v)
8985 (substring v (match-beginning 1) (match-end 1)))
8986 "Version of IZ-supported CPerl package this file is based on.")
8987
8988 (defun cperl-mode-unload-function ()
8989 "Unload the Cperl mode library."
8990 (let ((new-mode (if (eq (symbol-function 'perl-mode) 'cperl-mode)
8991 'fundamental-mode
8992 'perl-mode)))
8993 (dolist (buf (buffer-list))
8994 (with-current-buffer buf
8995 (when (eq major-mode 'cperl-mode)
8996 (funcall new-mode)))))
8997 ;; continue standard unloading
8998 nil)
8999
9000 (provide 'cperl-mode)
9001
9002 ;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
9003 ;;; cperl-mode.el ends here