merge trunk
[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, 2010
5 ;; 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 ;; This one is obsolete...
1806 (make-local-variable 'vc-header-alist)
1807 (with-no-warnings
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 )
1812 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1813 (make-local-variable 'compilation-error-regexp-alist-alist)
1814 (set 'compilation-error-regexp-alist-alist
1815 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
1816 (symbol-value 'compilation-error-regexp-alist-alist)))
1817 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1818 (let ((f 'compilation-build-compilation-error-regexp-alist))
1819 (funcall f))
1820 (make-local-variable 'compilation-error-regexp-alist)
1821 (push 'cperl compilation-error-regexp-alist)))
1822 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1823 (make-local-variable 'compilation-error-regexp-alist)
1824 (set 'compilation-error-regexp-alist
1825 (append cperl-compilation-error-regexp-alist
1826 (symbol-value 'compilation-error-regexp-alist)))))
1827 (make-local-variable 'font-lock-defaults)
1828 (setq font-lock-defaults
1829 (cond
1830 ((string< emacs-version "19.30")
1831 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1832 ((string< emacs-version "19.33") ; Which one to use?
1833 '((cperl-font-lock-keywords
1834 cperl-font-lock-keywords-1
1835 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1836 (t
1837 '((cperl-load-font-lock-keywords
1838 cperl-load-font-lock-keywords-1
1839 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1840 (make-local-variable 'cperl-syntax-state)
1841 (setq cperl-syntax-state nil) ; reset syntaxification cache
1842 (if cperl-use-syntax-table-text-property
1843 (if (boundp 'syntax-propertize-function)
1844 (progn
1845 ;; Reset syntaxification cache.
1846 (set (make-local-variable 'cperl-syntax-done-to) nil)
1847 (set (make-local-variable 'syntax-propertize-function)
1848 (lambda (start end)
1849 (goto-char start) (cperl-fontify-syntaxically end))))
1850 (make-local-variable 'parse-sexp-lookup-properties)
1851 ;; Do not introduce variable if not needed, we check it!
1852 (set 'parse-sexp-lookup-properties t)
1853 ;; Fix broken font-lock:
1854 (or (boundp 'font-lock-unfontify-region-function)
1855 (set 'font-lock-unfontify-region-function
1856 'font-lock-default-unfontify-region))
1857 (unless (featurep 'xemacs) ; Our: just a plug for wrong font-lock
1858 (make-local-variable 'font-lock-unfontify-region-function)
1859 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1860 'cperl-font-lock-unfontify-region-function))
1861 (make-local-variable 'cperl-syntax-done-to)
1862 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1863 (make-local-variable 'font-lock-syntactic-keywords)
1864 (setq font-lock-syntactic-keywords
1865 (if cperl-syntaxify-by-font-lock
1866 '((cperl-fontify-syntaxically))
1867 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1868 ;; used to ignore syntax-table text-properties. (t) is a hack
1869 ;; to make font-lock think that font-lock-syntactic-keywords
1870 ;; are defined.
1871 '(t)))))
1872 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1873 (progn
1874 (setq cperl-font-lock-multiline t) ; Not localized...
1875 (set (make-local-variable 'font-lock-multiline) t))
1876 (make-local-variable 'font-lock-fontify-region-function)
1877 (set 'font-lock-fontify-region-function ; not present with old Emacs
1878 'cperl-font-lock-fontify-region-function))
1879 (make-local-variable 'font-lock-fontify-region-function)
1880 (set 'font-lock-fontify-region-function ; not present with old Emacs
1881 'cperl-font-lock-fontify-region-function)
1882 (make-local-variable 'cperl-old-style)
1883 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1884 (set (make-local-variable 'normal-auto-fill-function)
1885 'cperl-do-auto-fill)
1886 (or (fboundp 'cperl-old-auto-fill-mode)
1887 (progn
1888 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1889 (defun auto-fill-mode (&optional arg)
1890 (interactive "P")
1891 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1892 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1893 (setq auto-fill-function 'cperl-do-auto-fill))))))
1894 (if (cperl-enable-font-lock)
1895 (if (cperl-val 'cperl-font-lock)
1896 (progn (or cperl-faces-init (cperl-init-faces))
1897 (font-lock-mode 1))))
1898 (set (make-local-variable 'facemenu-add-face-function)
1899 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1900 (and (boundp 'msb-menu-cond)
1901 (not cperl-msb-fixed)
1902 (cperl-msb-fix))
1903 (if (featurep 'easymenu)
1904 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1905 (run-mode-hooks 'cperl-mode-hook)
1906 (if cperl-hook-after-change
1907 (add-hook 'after-change-functions 'cperl-after-change-function nil t))
1908 ;; After hooks since fontification will break this
1909 (if cperl-pod-here-scan
1910 (or cperl-syntaxify-by-font-lock
1911 (progn (or cperl-faces-init (cperl-init-faces-weak))
1912 (cperl-find-pods-heres)))))
1913 \f
1914 ;; Fix for perldb - make default reasonable
1915 (defun cperl-db ()
1916 (interactive)
1917 (require 'gud)
1918 (perldb (read-from-minibuffer "Run perldb (like this): "
1919 (if (consp gud-perldb-history)
1920 (car gud-perldb-history)
1921 (concat "perl " ;;(file-name-nondirectory
1922 ;; I have problems
1923 ;; in OS/2
1924 ;; otherwise
1925 (buffer-file-name)))
1926 nil nil
1927 '(gud-perldb-history . 1))))
1928 \f
1929 (defun cperl-msb-fix ()
1930 ;; Adds perl files to msb menu, supposes that msb is already loaded
1931 (setq cperl-msb-fixed t)
1932 (let* ((l (length msb-menu-cond))
1933 (last (nth (1- l) msb-menu-cond))
1934 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1935 (handle (1- (nth 1 last))))
1936 (setcdr precdr (list
1937 (list
1938 '(memq major-mode '(cperl-mode perl-mode))
1939 handle
1940 "Perl Files (%d)")
1941 last))))
1942 \f
1943 ;; This is used by indent-for-comment
1944 ;; to decide how much to indent a comment in CPerl code
1945 ;; based on its context. Do fallback if comment is found wrong.
1946
1947 (defvar cperl-wrong-comment)
1948 (defvar cperl-st-cfence '(14)) ; Comment-fence
1949 (defvar cperl-st-sfence '(15)) ; String-fence
1950 (defvar cperl-st-punct '(1))
1951 (defvar cperl-st-word '(2))
1952 (defvar cperl-st-bra '(4 . ?\>))
1953 (defvar cperl-st-ket '(5 . ?\<))
1954
1955
1956 (defun cperl-comment-indent () ; called at point at supposed comment
1957 (let ((p (point)) (c (current-column)) was phony)
1958 (if (and (not cperl-indent-comment-at-column-0)
1959 (looking-at "^#"))
1960 0 ; Existing comment at bol stays there.
1961 ;; Wrong comment found
1962 (save-excursion
1963 (setq was (cperl-to-comment-or-eol)
1964 phony (eq (get-text-property (point) 'syntax-table)
1965 cperl-st-cfence))
1966 (if phony
1967 (progn ; Too naive???
1968 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1969 (if (eq (preceding-char) ?\#)
1970 (forward-char -1))
1971 (setq was nil)))
1972 (if (= (point) p) ; Our caller found a correct place
1973 (progn
1974 (skip-chars-backward " \t")
1975 (setq was (current-column))
1976 (if (eq was 0)
1977 comment-column
1978 (max (1+ was) ; Else indent at comment column
1979 comment-column)))
1980 ;; No, the caller found a random place; we need to edit ourselves
1981 (if was nil
1982 (insert comment-start)
1983 (backward-char (length comment-start)))
1984 (setq cperl-wrong-comment t)
1985 (cperl-make-indent comment-column 1) ; Indent min 1
1986 c)))))
1987
1988 ;;;(defun cperl-comment-indent-fallback ()
1989 ;;; "Is called if the standard comment-search procedure fails.
1990 ;;;Point is at start of real comment."
1991 ;;; (let ((c (current-column)) target cnt prevc)
1992 ;;; (if (= c comment-column) nil
1993 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1994 ;;; (setq target (max (1+ (setq prevc
1995 ;;; (current-column))) ; Else indent at comment column
1996 ;;; comment-column))
1997 ;;; (if (= c comment-column) nil
1998 ;;; (delete-backward-char cnt)
1999 ;;; (while (< prevc target)
2000 ;;; (insert "\t")
2001 ;;; (setq prevc (current-column)))
2002 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
2003 ;;; (while (< prevc target)
2004 ;;; (insert " ")
2005 ;;; (setq prevc (current-column)))))))
2006
2007 (defun cperl-indent-for-comment ()
2008 "Substitute for `indent-for-comment' in CPerl."
2009 (interactive)
2010 (let (cperl-wrong-comment)
2011 (indent-for-comment)
2012 (if cperl-wrong-comment ; set by `cperl-comment-indent'
2013 (progn (cperl-to-comment-or-eol)
2014 (forward-char (length comment-start))))))
2015
2016 (defun cperl-comment-region (b e arg)
2017 "Comment or uncomment each line in the region in CPerl mode.
2018 See `comment-region'."
2019 (interactive "r\np")
2020 (let ((comment-start "#"))
2021 (comment-region b e arg)))
2022
2023 (defun cperl-uncomment-region (b e arg)
2024 "Uncomment or comment each line in the region in CPerl mode.
2025 See `comment-region'."
2026 (interactive "r\np")
2027 (let ((comment-start "#"))
2028 (comment-region b e (- arg))))
2029
2030 (defvar cperl-brace-recursing nil)
2031
2032 (defun cperl-electric-brace (arg &optional only-before)
2033 "Insert character and correct line's indentation.
2034 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2035 place (even in empty line), but not after. If after \")\" and the inserted
2036 char is \"{\", insert extra newline before only if
2037 `cperl-extra-newline-before-brace'."
2038 (interactive "P")
2039 (let (insertpos
2040 (other-end (if (and cperl-electric-parens-mark
2041 (cperl-mark-active)
2042 (< (mark) (point)))
2043 (mark)
2044 nil)))
2045 (if (and other-end
2046 (not cperl-brace-recursing)
2047 (cperl-val 'cperl-electric-parens)
2048 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2049 ;; Need to insert a matching pair
2050 (progn
2051 (save-excursion
2052 (setq insertpos (point-marker))
2053 (goto-char other-end)
2054 (setq last-command-event ?\{)
2055 (cperl-electric-lbrace arg insertpos))
2056 (forward-char 1))
2057 ;; Check whether we close something "usual" with `}'
2058 (if (and (eq last-command-event ?\})
2059 (not
2060 (condition-case nil
2061 (save-excursion
2062 (up-list (- (prefix-numeric-value arg)))
2063 ;;(cperl-after-block-p (point-min))
2064 (or (cperl-after-expr-p nil "{;)")
2065 ;; after sub, else, continue
2066 (cperl-after-block-p nil 'pre)))
2067 (error nil))))
2068 ;; Just insert the guy
2069 (self-insert-command (prefix-numeric-value arg))
2070 (if (and (not arg) ; No args, end (of empty line or auto)
2071 (eolp)
2072 (or (and (null only-before)
2073 (save-excursion
2074 (skip-chars-backward " \t")
2075 (bolp)))
2076 (and (eq last-command-event ?\{) ; Do not insert newline
2077 ;; if after ")" and `cperl-extra-newline-before-brace'
2078 ;; is nil, do not insert extra newline.
2079 (not cperl-extra-newline-before-brace)
2080 (save-excursion
2081 (skip-chars-backward " \t")
2082 (eq (preceding-char) ?\))))
2083 (if cperl-auto-newline
2084 (progn (cperl-indent-line) (newline) t) nil)))
2085 (progn
2086 (self-insert-command (prefix-numeric-value arg))
2087 (cperl-indent-line)
2088 (if cperl-auto-newline
2089 (setq insertpos (1- (point))))
2090 (if (and cperl-auto-newline (null only-before))
2091 (progn
2092 (newline)
2093 (cperl-indent-line)))
2094 (save-excursion
2095 (if insertpos (progn (goto-char insertpos)
2096 (search-forward (make-string
2097 1 last-command-event))
2098 (setq insertpos (1- (point)))))
2099 (delete-char -1))))
2100 (if insertpos
2101 (save-excursion
2102 (goto-char insertpos)
2103 (self-insert-command (prefix-numeric-value arg)))
2104 (self-insert-command (prefix-numeric-value arg)))))))
2105
2106 (defun cperl-electric-lbrace (arg &optional end)
2107 "Insert character, correct line's indentation, correct quoting by space."
2108 (interactive "P")
2109 (let ((cperl-brace-recursing t)
2110 (cperl-auto-newline cperl-auto-newline)
2111 (other-end (or end
2112 (if (and cperl-electric-parens-mark
2113 (cperl-mark-active)
2114 (> (mark) (point)))
2115 (save-excursion
2116 (goto-char (mark))
2117 (point-marker))
2118 nil)))
2119 pos after)
2120 (and (cperl-val 'cperl-electric-lbrace-space)
2121 (eq (preceding-char) ?$)
2122 (save-excursion
2123 (skip-chars-backward "$")
2124 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2125 (insert ?\s))
2126 ;; Check whether we are in comment
2127 (if (and
2128 (save-excursion
2129 (beginning-of-line)
2130 (not (looking-at "[ \t]*#")))
2131 (cperl-after-expr-p nil "{;)"))
2132 nil
2133 (setq cperl-auto-newline nil))
2134 (cperl-electric-brace arg)
2135 (and (cperl-val 'cperl-electric-parens)
2136 (eq last-command-event ?{)
2137 (memq last-command-event
2138 (append cperl-electric-parens-string nil))
2139 (or (if other-end (goto-char (marker-position other-end)))
2140 t)
2141 (setq last-command-event ?} pos (point))
2142 (progn (cperl-electric-brace arg t)
2143 (goto-char pos)))))
2144
2145 (defun cperl-electric-paren (arg)
2146 "Insert an opening parenthesis or a matching pair of parentheses.
2147 See `cperl-electric-parens'."
2148 (interactive "P")
2149 (let ((beg (save-excursion (beginning-of-line) (point)))
2150 (other-end (if (and cperl-electric-parens-mark
2151 (cperl-mark-active)
2152 (> (mark) (point)))
2153 (save-excursion
2154 (goto-char (mark))
2155 (point-marker))
2156 nil)))
2157 (if (and (cperl-val 'cperl-electric-parens)
2158 (memq last-command-event
2159 (append cperl-electric-parens-string nil))
2160 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2161 ;;(not (save-excursion (search-backward "#" beg t)))
2162 (if (eq last-command-event ?<)
2163 (progn
2164 ;; This code is too electric, see Bug#3943.
2165 ;; (and abbrev-mode ; later it is too late, may be after `for'
2166 ;; (expand-abbrev))
2167 (cperl-after-expr-p nil "{;(,:="))
2168 1))
2169 (progn
2170 (self-insert-command (prefix-numeric-value arg))
2171 (if other-end (goto-char (marker-position other-end)))
2172 (insert (make-string
2173 (prefix-numeric-value arg)
2174 (cdr (assoc last-command-event '((?{ .?})
2175 (?[ . ?])
2176 (?( . ?))
2177 (?< . ?>))))))
2178 (forward-char (- (prefix-numeric-value arg))))
2179 (self-insert-command (prefix-numeric-value arg)))))
2180
2181 (defun cperl-electric-rparen (arg)
2182 "Insert a matching pair of parentheses if marking is active.
2183 If not, or if we are not at the end of marking range, would self-insert.
2184 Affected by `cperl-electric-parens'."
2185 (interactive "P")
2186 (let ((beg (save-excursion (beginning-of-line) (point)))
2187 (other-end (if (and cperl-electric-parens-mark
2188 (cperl-val 'cperl-electric-parens)
2189 (memq last-command-event
2190 (append cperl-electric-parens-string nil))
2191 (cperl-mark-active)
2192 (< (mark) (point)))
2193 (mark)
2194 nil))
2195 p)
2196 (if (and other-end
2197 (cperl-val 'cperl-electric-parens)
2198 (memq last-command-event '( ?\) ?\] ?\} ?\> ))
2199 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2200 ;;(not (save-excursion (search-backward "#" beg t)))
2201 )
2202 (progn
2203 (self-insert-command (prefix-numeric-value arg))
2204 (setq p (point))
2205 (if other-end (goto-char other-end))
2206 (insert (make-string
2207 (prefix-numeric-value arg)
2208 (cdr (assoc last-command-event '((?\} . ?\{)
2209 (?\] . ?\[)
2210 (?\) . ?\()
2211 (?\> . ?\<))))))
2212 (goto-char (1+ p)))
2213 (self-insert-command (prefix-numeric-value arg)))))
2214
2215 (defun cperl-electric-keyword ()
2216 "Insert a construction appropriate after a keyword.
2217 Help message may be switched off by setting `cperl-message-electric-keyword'
2218 to nil."
2219 (let ((beg (save-excursion (beginning-of-line) (point)))
2220 (dollar (and (eq last-command-event ?$)
2221 (eq this-command 'self-insert-command)))
2222 (delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2223 (memq this-command '(self-insert-command newline))))
2224 my do)
2225 (and (save-excursion
2226 (condition-case nil
2227 (progn
2228 (backward-sexp 1)
2229 (setq do (looking-at "do\\>")))
2230 (error nil))
2231 (cperl-after-expr-p nil "{;:"))
2232 (save-excursion
2233 (not
2234 (re-search-backward
2235 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2236 beg t)))
2237 (save-excursion (or (not (re-search-backward "^=" nil t))
2238 (or
2239 (looking-at "=cut")
2240 (and cperl-use-syntax-table-text-property
2241 (not (eq (get-text-property (point)
2242 'syntax-type)
2243 'pod))))))
2244 (save-excursion (forward-sexp -1)
2245 (not (memq (following-char) (append "$@%&*" nil))))
2246 (progn
2247 (and (eq (preceding-char) ?y)
2248 (progn ; "foreachmy"
2249 (forward-char -2)
2250 (insert " ")
2251 (forward-char 2)
2252 (setq my t dollar t
2253 delete
2254 (memq this-command '(self-insert-command newline)))))
2255 (and dollar (insert " $"))
2256 (cperl-indent-line)
2257 ;;(insert " () {\n}")
2258 (cond
2259 (cperl-extra-newline-before-brace
2260 (insert (if do "\n" " ()\n"))
2261 (insert "{")
2262 (cperl-indent-line)
2263 (insert "\n")
2264 (cperl-indent-line)
2265 (insert "\n}")
2266 (and do (insert " while ();")))
2267 (t
2268 (insert (if do " {\n} while ();" " () {\n}"))))
2269 (or (looking-at "[ \t]\\|$") (insert " "))
2270 (cperl-indent-line)
2271 (if dollar (progn (search-backward "$")
2272 (if my
2273 (forward-char 1)
2274 (delete-char 1)))
2275 (search-backward ")")
2276 (if (eq last-command-event ?\()
2277 (progn ; Avoid "if (())"
2278 (delete-backward-char 1)
2279 (delete-backward-char -1))))
2280 (if delete
2281 (cperl-putback-char cperl-del-back-ch))
2282 (if cperl-message-electric-keyword
2283 (message "Precede char by C-q to avoid expansion"))))))
2284
2285 (defun cperl-ensure-newlines (n &optional pos)
2286 "Make sure there are N newlines after the point."
2287 (or pos (setq pos (point)))
2288 (if (looking-at "\n")
2289 (forward-char 1)
2290 (insert "\n"))
2291 (if (> n 1)
2292 (cperl-ensure-newlines (1- n) pos)
2293 (goto-char pos)))
2294
2295 (defun cperl-electric-pod ()
2296 "Insert a POD chunk appropriate after a =POD directive."
2297 (let ((delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2298 (memq this-command '(self-insert-command newline))))
2299 head1 notlast name p really-delete over)
2300 (and (save-excursion
2301 (forward-word -1)
2302 (and
2303 (eq (preceding-char) ?=)
2304 (progn
2305 (setq head1 (looking-at "head1\\>[ \t]*$"))
2306 (setq over (and (looking-at "over\\>[ \t]*$")
2307 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2308 (forward-char -1)
2309 (bolp))
2310 (or
2311 (get-text-property (point) 'in-pod)
2312 (cperl-after-expr-p nil "{;:")
2313 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2314 (not (looking-at "\n*=cut"))
2315 (or (not cperl-use-syntax-table-text-property)
2316 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2317 (progn
2318 (save-excursion
2319 (setq notlast (re-search-forward "^\n=" nil t)))
2320 (or notlast
2321 (progn
2322 (insert "\n\n=cut")
2323 (cperl-ensure-newlines 2)
2324 (forward-word -2)
2325 (if (and head1
2326 (not
2327 (save-excursion
2328 (forward-char -1)
2329 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2330 nil t)))) ; Only one
2331 (progn
2332 (forward-word 1)
2333 (setq name (file-name-sans-extension
2334 (file-name-nondirectory (buffer-file-name)))
2335 p (point))
2336 (insert " NAME\n\n" name
2337 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2338 "=head1 DESCRIPTION")
2339 (cperl-ensure-newlines 4)
2340 (goto-char p)
2341 (forward-word 2)
2342 (end-of-line)
2343 (setq really-delete t))
2344 (forward-word 1))))
2345 (if over
2346 (progn
2347 (setq p (point))
2348 (insert "\n\n=item \n\n\n\n"
2349 "=back")
2350 (cperl-ensure-newlines 2)
2351 (goto-char p)
2352 (forward-word 1)
2353 (end-of-line)
2354 (setq really-delete t)))
2355 (if (and delete really-delete)
2356 (cperl-putback-char cperl-del-back-ch))))))
2357
2358 (defun cperl-electric-else ()
2359 "Insert a construction appropriate after a keyword.
2360 Help message may be switched off by setting `cperl-message-electric-keyword'
2361 to nil."
2362 (let ((beg (save-excursion (beginning-of-line) (point))))
2363 (and (save-excursion
2364 (backward-sexp 1)
2365 (cperl-after-expr-p nil "{;:"))
2366 (save-excursion
2367 (not
2368 (re-search-backward
2369 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2370 beg t)))
2371 (save-excursion (or (not (re-search-backward "^=" nil t))
2372 (looking-at "=cut")
2373 (and cperl-use-syntax-table-text-property
2374 (not (eq (get-text-property (point)
2375 'syntax-type)
2376 'pod)))))
2377 (progn
2378 (cperl-indent-line)
2379 ;;(insert " {\n\n}")
2380 (cond
2381 (cperl-extra-newline-before-brace
2382 (insert "\n")
2383 (insert "{")
2384 (cperl-indent-line)
2385 (insert "\n\n}"))
2386 (t
2387 (insert " {\n\n}")))
2388 (or (looking-at "[ \t]\\|$") (insert " "))
2389 (cperl-indent-line)
2390 (forward-line -1)
2391 (cperl-indent-line)
2392 (cperl-putback-char cperl-del-back-ch)
2393 (setq this-command 'cperl-electric-else)
2394 (if cperl-message-electric-keyword
2395 (message "Precede char by C-q to avoid expansion"))))))
2396
2397 (defun cperl-linefeed ()
2398 "Go to end of line, open a new line and indent appropriately.
2399 If in POD, insert appropriate lines."
2400 (interactive)
2401 (let ((beg (save-excursion (beginning-of-line) (point)))
2402 (end (save-excursion (end-of-line) (point)))
2403 (pos (point)) start over cut res)
2404 (if (and ; Check if we need to split:
2405 ; i.e., on a boundary and inside "{...}"
2406 (save-excursion (cperl-to-comment-or-eol)
2407 (>= (point) pos)) ; Not in a comment
2408 (or (save-excursion
2409 (skip-chars-backward " \t" beg)
2410 (forward-char -1)
2411 (looking-at "[;{]")) ; After { or ; + spaces
2412 (looking-at "[ \t]*}") ; Before }
2413 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2414 (save-excursion
2415 (and
2416 (eq (car (parse-partial-sexp pos end -1)) -1)
2417 ; Leave the level of parens
2418 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2419 ; Are at end
2420 (cperl-after-block-p (point-min))
2421 (progn
2422 (backward-sexp 1)
2423 (setq start (point-marker))
2424 (<= start pos))))) ; Redundant? Are after the
2425 ; start of parens group.
2426 (progn
2427 (skip-chars-backward " \t")
2428 (or (memq (preceding-char) (append ";{" nil))
2429 (insert ";"))
2430 (insert "\n")
2431 (forward-line -1)
2432 (cperl-indent-line)
2433 (goto-char start)
2434 (or (looking-at "{[ \t]*$") ; If there is a statement
2435 ; before, move it to separate line
2436 (progn
2437 (forward-char 1)
2438 (insert "\n")
2439 (cperl-indent-line)))
2440 (forward-line 1) ; We are on the target line
2441 (cperl-indent-line)
2442 (beginning-of-line)
2443 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2444 ; after, move it to separate line
2445 (progn
2446 (end-of-line)
2447 (search-backward "}" beg)
2448 (skip-chars-backward " \t")
2449 (or (memq (preceding-char) (append ";{" nil))
2450 (insert ";"))
2451 (insert "\n")
2452 (cperl-indent-line)
2453 (forward-line -1)))
2454 (forward-line -1) ; We are on the line before target
2455 (end-of-line)
2456 (newline-and-indent))
2457 (end-of-line) ; else - no splitting
2458 (cond
2459 ((and (looking-at "\n[ \t]*{$")
2460 (save-excursion
2461 (skip-chars-backward " \t")
2462 (eq (preceding-char) ?\)))) ; Probably if () {} group
2463 ; with an extra newline.
2464 (forward-line 2)
2465 (cperl-indent-line))
2466 ((save-excursion ; In POD header
2467 (forward-paragraph -1)
2468 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2469 ;; We are after \n now, so look for the rest
2470 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2471 (progn
2472 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2473 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2474 t)))
2475 (if (and over
2476 (progn
2477 (forward-paragraph -1)
2478 (forward-word 1)
2479 (setq pos (point))
2480 (setq cut (buffer-substring (point)
2481 (save-excursion
2482 (end-of-line)
2483 (point))))
2484 (delete-char (- (save-excursion (end-of-line) (point))
2485 (point)))
2486 (setq res (expand-abbrev))
2487 (save-excursion
2488 (goto-char pos)
2489 (insert cut))
2490 res))
2491 nil
2492 (cperl-ensure-newlines (if cut 2 4))
2493 (forward-line 2)))
2494 ((get-text-property (point) 'in-pod) ; In POD section
2495 (cperl-ensure-newlines 4)
2496 (forward-line 2))
2497 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2498 (forward-line 1)
2499 (cperl-indent-line))
2500 (t
2501 (newline-and-indent))))))
2502
2503 (defun cperl-electric-semi (arg)
2504 "Insert character and correct line's indentation."
2505 (interactive "P")
2506 (if cperl-auto-newline
2507 (cperl-electric-terminator arg)
2508 (self-insert-command (prefix-numeric-value arg))
2509 (if cperl-autoindent-on-semi
2510 (cperl-indent-line))))
2511
2512 (defun cperl-electric-terminator (arg)
2513 "Insert character and correct line's indentation."
2514 (interactive "P")
2515 (let ((end (point))
2516 (auto (and cperl-auto-newline
2517 (or (not (eq last-command-event ?:))
2518 cperl-auto-newline-after-colon)))
2519 insertpos)
2520 (if (and ;;(not arg)
2521 (eolp)
2522 (not (save-excursion
2523 (beginning-of-line)
2524 (skip-chars-forward " \t")
2525 (or
2526 ;; Ignore in comment lines
2527 (= (following-char) ?#)
2528 ;; Colon is special only after a label
2529 ;; So quickly rule out most other uses of colon
2530 ;; and do no indentation for them.
2531 (and (eq last-command-event ?:)
2532 (save-excursion
2533 (forward-word 1)
2534 (skip-chars-forward " \t")
2535 (and (< (point) end)
2536 (progn (goto-char (- end 1))
2537 (not (looking-at ":"))))))
2538 (progn
2539 (beginning-of-defun)
2540 (let ((pps (parse-partial-sexp (point) end)))
2541 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2542 (progn
2543 (self-insert-command (prefix-numeric-value arg))
2544 ;;(forward-char -1)
2545 (if auto (setq insertpos (point-marker)))
2546 ;;(forward-char 1)
2547 (cperl-indent-line)
2548 (if auto
2549 (progn
2550 (newline)
2551 (cperl-indent-line)))
2552 (save-excursion
2553 (if insertpos (goto-char (1- (marker-position insertpos)))
2554 (forward-char -1))
2555 (delete-char 1))))
2556 (if insertpos
2557 (save-excursion
2558 (goto-char insertpos)
2559 (self-insert-command (prefix-numeric-value arg)))
2560 (self-insert-command (prefix-numeric-value arg)))))
2561
2562 (defun cperl-electric-backspace (arg)
2563 "Backspace, or remove whitespace around the point inserted by an electric key.
2564 Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2565 (interactive "p")
2566 (if (and cperl-auto-newline
2567 (memq last-command '(cperl-electric-semi
2568 cperl-electric-terminator
2569 cperl-electric-lbrace))
2570 (memq (preceding-char) '(?\s ?\t ?\n)))
2571 (let (p)
2572 (if (eq last-command 'cperl-electric-lbrace)
2573 (skip-chars-forward " \t\n"))
2574 (setq p (point))
2575 (skip-chars-backward " \t\n")
2576 (delete-region (point) p))
2577 (and (eq last-command 'cperl-electric-else)
2578 ;; We are removing the whitespace *inside* cperl-electric-else
2579 (setq this-command 'cperl-electric-else-really))
2580 (if (and cperl-auto-newline
2581 (eq last-command 'cperl-electric-else-really)
2582 (memq (preceding-char) '(?\s ?\t ?\n)))
2583 (let (p)
2584 (skip-chars-forward " \t\n")
2585 (setq p (point))
2586 (skip-chars-backward " \t\n")
2587 (delete-region (point) p))
2588 (if cperl-electric-backspace-untabify
2589 (backward-delete-char-untabify arg)
2590 (delete-backward-char arg)))))
2591
2592 (put 'cperl-electric-backspace 'delete-selection 'supersede)
2593
2594 (defun cperl-inside-parens-p () ;; NOT USED????
2595 (condition-case ()
2596 (save-excursion
2597 (save-restriction
2598 (narrow-to-region (point)
2599 (progn (beginning-of-defun) (point)))
2600 (goto-char (point-max))
2601 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2602 (error nil)))
2603 \f
2604 (defun cperl-indent-command (&optional whole-exp)
2605 "Indent current line as Perl code, or in some cases insert a tab character.
2606 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2607 line. Otherwise, indent the current line only if point is at the left margin
2608 or in the line's indentation; otherwise insert a tab.
2609
2610 A numeric argument, regardless of its value,
2611 means indent rigidly all the lines of the expression starting after point
2612 so that this line becomes properly indented.
2613 The relative indentation among the lines of the expression are preserved."
2614 (interactive "P")
2615 (cperl-update-syntaxification (point) (point))
2616 (if whole-exp
2617 ;; If arg, always indent this line as Perl
2618 ;; and shift remaining lines of expression the same amount.
2619 (let ((shift-amt (cperl-indent-line))
2620 beg end)
2621 (save-excursion
2622 (if cperl-tab-always-indent
2623 (beginning-of-line))
2624 (setq beg (point))
2625 (forward-sexp 1)
2626 (setq end (point))
2627 (goto-char beg)
2628 (forward-line 1)
2629 (setq beg (point)))
2630 (if (and shift-amt (> end beg))
2631 (indent-code-rigidly beg end shift-amt "#")))
2632 (if (and (not cperl-tab-always-indent)
2633 (save-excursion
2634 (skip-chars-backward " \t")
2635 (not (bolp))))
2636 (insert-tab)
2637 (cperl-indent-line))))
2638
2639 (defun cperl-indent-line (&optional parse-data)
2640 "Indent current line as Perl code.
2641 Return the amount the indentation changed by."
2642 (let ((case-fold-search nil)
2643 (pos (- (point-max) (point)))
2644 indent i beg shift-amt)
2645 (setq indent (cperl-calculate-indent parse-data)
2646 i indent)
2647 (beginning-of-line)
2648 (setq beg (point))
2649 (cond ((or (eq indent nil) (eq indent t))
2650 (setq indent (current-indentation) i nil))
2651 ;;((eq indent t) ; Never?
2652 ;; (setq indent (cperl-calculate-indent-within-comment)))
2653 ;;((looking-at "[ \t]*#")
2654 ;; (setq indent 0))
2655 (t
2656 (skip-chars-forward " \t")
2657 (if (listp indent) (setq indent (car indent)))
2658 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2659 (not (looking-at "[smy]:\\|tr:")))
2660 (and (> indent 0)
2661 (setq indent (max cperl-min-label-indent
2662 (+ indent cperl-label-offset)))))
2663 ((= (following-char) ?})
2664 (setq indent (- indent cperl-indent-level)))
2665 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2666 (setq indent (+ indent cperl-close-paren-offset)))
2667 ((= (following-char) ?{)
2668 (setq indent (+ indent cperl-brace-offset))))))
2669 (skip-chars-forward " \t")
2670 (setq shift-amt (and i (- indent (current-column))))
2671 (if (or (not shift-amt)
2672 (zerop shift-amt))
2673 (if (> (- (point-max) pos) (point))
2674 (goto-char (- (point-max) pos)))
2675 ;;;(delete-region beg (point))
2676 ;;;(indent-to indent)
2677 (cperl-make-indent indent)
2678 ;; If initial point was within line's indentation,
2679 ;; position after the indentation. Else stay at same point in text.
2680 (if (> (- (point-max) pos) (point))
2681 (goto-char (- (point-max) pos))))
2682 shift-amt))
2683
2684 (defun cperl-after-label ()
2685 ;; Returns true if the point is after label. Does not do save-excursion.
2686 (and (eq (preceding-char) ?:)
2687 (memq (char-syntax (char-after (- (point) 2)))
2688 '(?w ?_))
2689 (progn
2690 (backward-sexp)
2691 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2692
2693 (defun cperl-get-state (&optional parse-start start-state)
2694 ;; returns list (START STATE DEPTH PRESTART),
2695 ;; START is a good place to start parsing, or equal to
2696 ;; PARSE-START if preset,
2697 ;; STATE is what is returned by `parse-partial-sexp'.
2698 ;; DEPTH is true is we are immediately after end of block
2699 ;; which contains START.
2700 ;; PRESTART is the position basing on which START was found.
2701 (save-excursion
2702 (let ((start-point (point)) depth state start prestart)
2703 (if (and parse-start
2704 (<= parse-start start-point))
2705 (goto-char parse-start)
2706 (beginning-of-defun)
2707 (setq start-state nil))
2708 (setq prestart (point))
2709 (if start-state nil
2710 ;; Try to go out, if sub is not on the outermost level
2711 (while (< (point) start-point)
2712 (setq start (point) parse-start start depth nil
2713 state (parse-partial-sexp start start-point -1))
2714 (if (> (car state) -1) nil
2715 ;; The current line could start like }}}, so the indentation
2716 ;; corresponds to a different level than what we reached
2717 (setq depth t)
2718 (beginning-of-line 2))) ; Go to the next line.
2719 (if start (goto-char start))) ; Not at the start of file
2720 (setq start (point))
2721 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2722 (list start state depth prestart))))
2723
2724 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2725
2726 (defun cperl-beginning-of-property (p prop &optional lim)
2727 "Given that P has a property PROP, find where the property starts.
2728 Will not look before LIM."
2729 ;;; XXXX What to do at point-max???
2730 (or (previous-single-property-change (cperl-1+ p) prop lim)
2731 (point-min))
2732 ;;; (cond ((eq p (point-min))
2733 ;;; p)
2734 ;;; ((and lim (<= p lim))
2735 ;;; p)
2736 ;;; ((not (get-text-property (1- p) prop))
2737 ;;; p)
2738 ;;; (t (or (previous-single-property-change p look-prop lim)
2739 ;;; (point-min))))
2740 )
2741
2742 (defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2743 ;; the sniffer logic to understand what the current line MEANS.
2744 (cperl-update-syntaxification (point) (point))
2745 (let ((res (get-text-property (point) 'syntax-type)))
2746 (save-excursion
2747 (cond
2748 ((and (memq res '(pod here-doc here-doc-delim format))
2749 (not (get-text-property (point) 'indentable)))
2750 (vector res))
2751 ;; before start of POD - whitespace found since do not have 'pod!
2752 ((looking-at "[ \t]*\n=")
2753 (error "Spaces before POD section!"))
2754 ((and (not cperl-indent-left-aligned-comments)
2755 (looking-at "^#"))
2756 [comment-special:at-beginning-of-line])
2757 ((get-text-property (point) 'in-pod)
2758 [in-pod])
2759 (t
2760 (beginning-of-line)
2761 (let* ((indent-point (point))
2762 (char-after-pos (save-excursion
2763 (skip-chars-forward " \t")
2764 (point)))
2765 (char-after (char-after char-after-pos))
2766 (pre-indent-point (point))
2767 p prop look-prop is-block delim)
2768 (save-excursion ; Know we are not in POD, find appropriate pos before
2769 (cperl-backward-to-noncomment nil)
2770 (setq p (max (point-min) (1- (point)))
2771 prop (get-text-property p 'syntax-type)
2772 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2773 'syntax-type))
2774 (if (memq prop '(pod here-doc format here-doc-delim))
2775 (progn
2776 (goto-char (cperl-beginning-of-property p look-prop))
2777 (beginning-of-line)
2778 (setq pre-indent-point (point)))))
2779 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2780 (let* ((case-fold-search nil)
2781 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2782 (start (or (nth 2 parse-data) ; last complete sexp terminated
2783 (nth 0 s-s))) ; Good place to start parsing
2784 (state (nth 1 s-s))
2785 (containing-sexp (car (cdr state)))
2786 old-indent)
2787 (if (and
2788 ;;containing-sexp ;; We are buggy at toplevel :-(
2789 parse-data)
2790 (progn
2791 (setcar parse-data pre-indent-point)
2792 (setcar (cdr parse-data) state)
2793 (or (nth 2 parse-data)
2794 (setcar (cddr parse-data) start))
2795 ;; Before this point: end of statement
2796 (setq old-indent (nth 3 parse-data))))
2797 (cond ((get-text-property (point) 'indentable)
2798 ;; indent to "after" the surrounding open
2799 ;; (same offset as `cperl-beautify-regexp-piece'),
2800 ;; skip blanks if we do not close the expression.
2801 (setq delim ; We do not close the expression
2802 (get-text-property
2803 (cperl-1+ char-after-pos) 'indentable)
2804 p (1+ (cperl-beginning-of-property
2805 (point) 'indentable))
2806 is-block ; misused for: preceeding line in REx
2807 (save-excursion ; Find preceeding line
2808 (cperl-backward-to-noncomment p)
2809 (beginning-of-line)
2810 (if (<= (point) p)
2811 (progn ; get indent from the first line
2812 (goto-char p)
2813 (skip-chars-forward " \t")
2814 (if (memq (char-after (point))
2815 (append "#\n" nil))
2816 nil ; Can't use intentation of this line...
2817 (point)))
2818 (skip-chars-forward " \t")
2819 (point)))
2820 prop (parse-partial-sexp p char-after-pos))
2821 (cond ((not delim) ; End the REx, ignore is-block
2822 (vector 'indentable 'terminator p is-block))
2823 (is-block ; Indent w.r.t. preceeding line
2824 (vector 'indentable 'cont-line char-after-pos
2825 is-block char-after p))
2826 (t ; No preceeding line...
2827 (vector 'indentable 'first-line p))))
2828 ((get-text-property char-after-pos 'REx-part2)
2829 (vector 'REx-part2 (point)))
2830 ((nth 4 state)
2831 [comment])
2832 ((nth 3 state)
2833 [string])
2834 ;; XXXX Do we need to special-case this?
2835 ((null containing-sexp)
2836 ;; Line is at top level. May be data or function definition,
2837 ;; or may be function argument declaration.
2838 ;; Indent like the previous top level line
2839 ;; unless that ends in a closeparen without semicolon,
2840 ;; in which case this line is the first argument decl.
2841 (skip-chars-forward " \t")
2842 (cperl-backward-to-noncomment (or old-indent (point-min)))
2843 (setq state
2844 (or (bobp)
2845 (eq (point) old-indent) ; old-indent was at comment
2846 (eq (preceding-char) ?\;)
2847 ;; Had ?\) too
2848 (and (eq (preceding-char) ?\})
2849 (cperl-after-block-and-statement-beg
2850 (point-min))) ; Was start - too close
2851 (memq char-after (append ")]}" nil))
2852 (and (eq (preceding-char) ?\:) ; label
2853 (progn
2854 (forward-sexp -1)
2855 (skip-chars-backward " \t")
2856 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2857 (get-text-property (point) 'first-format-line)))
2858
2859 ;; Look at previous line that's at column 0
2860 ;; to determine whether we are in top-level decls
2861 ;; or function's arg decls. Set basic-indent accordingly.
2862 ;; Now add a little if this is a continuation line.
2863 (and state
2864 parse-data
2865 (not (eq char-after ?\C-j))
2866 (setcdr (cddr parse-data)
2867 (list pre-indent-point)))
2868 (vector 'toplevel start char-after state (nth 2 s-s)))
2869 ((not
2870 (or (setq is-block
2871 (and (setq delim (= (char-after containing-sexp) ?{))
2872 (save-excursion ; Is it a hash?
2873 (goto-char containing-sexp)
2874 (cperl-block-p))))
2875 cperl-indent-parens-as-block))
2876 ;; group is an expression, not a block:
2877 ;; indent to just after the surrounding open parens,
2878 ;; skip blanks if we do not close the expression.
2879 (goto-char (1+ containing-sexp))
2880 (or (memq char-after
2881 (append (if delim "}" ")]}") nil))
2882 (looking-at "[ \t]*\\(#\\|$\\)")
2883 (skip-chars-forward " \t"))
2884 (setq old-indent (point)) ; delim=is-brace
2885 (vector 'in-parens char-after (point) delim containing-sexp))
2886 (t
2887 ;; Statement level. Is it a continuation or a new statement?
2888 ;; Find previous non-comment character.
2889 (goto-char pre-indent-point) ; Skip one level of POD/etc
2890 (cperl-backward-to-noncomment containing-sexp)
2891 ;; Back up over label lines, since they don't
2892 ;; affect whether our line is a continuation.
2893 ;; (Had \, too)
2894 (while;;(or (eq (preceding-char) ?\,)
2895 (and (eq (preceding-char) ?:)
2896 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2897 (memq (char-syntax (char-after (- (point) 2)))
2898 '(?w ?_))))
2899 ;;)
2900 ;; This is always FALSE?
2901 (if (eq (preceding-char) ?\,)
2902 ;; Will go to beginning of line, essentially.
2903 ;; Will ignore embedded sexpr XXXX.
2904 (cperl-backward-to-start-of-continued-exp containing-sexp))
2905 (beginning-of-line)
2906 (cperl-backward-to-noncomment containing-sexp))
2907 ;; Now we get non-label preceeding the indent point
2908 (if (not (or (eq (1- (point)) containing-sexp)
2909 (memq (preceding-char)
2910 (append (if is-block " ;{" " ,;{") '(nil)))
2911 (and (eq (preceding-char) ?\})
2912 (cperl-after-block-and-statement-beg
2913 containing-sexp))
2914 (get-text-property (point) 'first-format-line)))
2915 ;; This line is continuation of preceding line's statement;
2916 ;; indent `cperl-continued-statement-offset' more than the
2917 ;; previous line of the statement.
2918 ;;
2919 ;; There might be a label on this line, just
2920 ;; consider it bad style and ignore it.
2921 (progn
2922 (cperl-backward-to-start-of-continued-exp containing-sexp)
2923 (vector 'continuation (point) char-after is-block delim))
2924 ;; This line starts a new statement.
2925 ;; Position following last unclosed open brace
2926 (goto-char containing-sexp)
2927 ;; Is line first statement after an open-brace?
2928 (or
2929 ;; If no, find that first statement and indent like
2930 ;; it. If the first statement begins with label, do
2931 ;; not believe when the indentation of the label is too
2932 ;; small.
2933 (save-excursion
2934 (forward-char 1)
2935 (let ((colon-line-end 0))
2936 (while
2937 (progn (skip-chars-forward " \t\n")
2938 ;; s: foo : bar :x is NOT label
2939 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2940 (not (looking-at "[sym]:\\|tr:"))))
2941 ;; Skip over comments and labels following openbrace.
2942 (cond ((= (following-char) ?\#)
2943 (forward-line 1))
2944 ((= (following-char) ?\=)
2945 (goto-char
2946 (or (next-single-property-change (point) 'in-pod)
2947 (point-max)))) ; do not loop if no syntaxification
2948 ;; label:
2949 (t
2950 (save-excursion (end-of-line)
2951 (setq colon-line-end (point)))
2952 (search-forward ":"))))
2953 ;; We are at beginning of code (NOT label or comment)
2954 ;; First, the following code counts
2955 ;; if it is before the line we want to indent.
2956 (and (< (point) indent-point)
2957 (vector 'have-prev-sibling (point) colon-line-end
2958 containing-sexp))))
2959 (progn
2960 ;; If no previous statement,
2961 ;; indent it relative to line brace is on.
2962
2963 ;; For open-braces not the first thing in a line,
2964 ;; add in cperl-brace-imaginary-offset.
2965
2966 ;; If first thing on a line: ?????
2967 ;; Move back over whitespace before the openbrace.
2968 (setq ; brace first thing on a line
2969 old-indent (progn (skip-chars-backward " \t") (bolp)))
2970 ;; Should we indent w.r.t. earlier than start?
2971 ;; Move to start of control group, possibly on a different line
2972 (or cperl-indent-wrt-brace
2973 (cperl-backward-to-noncomment (point-min)))
2974 ;; If the openbrace is preceded by a parenthesized exp,
2975 ;; move to the beginning of that;
2976 (if (eq (preceding-char) ?\))
2977 (progn
2978 (forward-sexp -1)
2979 (cperl-backward-to-noncomment (point-min))))
2980 ;; In the case it starts a subroutine, indent with
2981 ;; respect to `sub', not with respect to the
2982 ;; first thing on the line, say in the case of
2983 ;; anonymous sub in a hash.
2984 (if (and;; Is it a sub in group starting on this line?
2985 (cond ((get-text-property (point) 'attrib-group)
2986 (goto-char (cperl-beginning-of-property
2987 (point) 'attrib-group)))
2988 ((eq (preceding-char) ?b)
2989 (forward-sexp -1)
2990 (looking-at "sub\\>")))
2991 (setq p (nth 1 ; start of innermost containing list
2992 (parse-partial-sexp
2993 (save-excursion (beginning-of-line)
2994 (point))
2995 (point)))))
2996 (progn
2997 (goto-char (1+ p)) ; enclosing block on the same line
2998 (skip-chars-forward " \t")
2999 (vector 'code-start-in-block containing-sexp char-after
3000 (and delim (not is-block)) ; is a HASH
3001 old-indent ; brace first thing on a line
3002 t (point) ; have something before...
3003 )
3004 ;;(current-column)
3005 )
3006 ;; Get initial indentation of the line we are on.
3007 ;; If line starts with label, calculate label indentation
3008 (vector 'code-start-in-block containing-sexp char-after
3009 (and delim (not is-block)) ; is a HASH
3010 old-indent ; brace first thing on a line
3011 nil (point))))))))))))))) ; nothing interesting before
3012
3013 (defvar cperl-indent-rules-alist
3014 '((pod nil) ; via `syntax-type' property
3015 (here-doc nil) ; via `syntax-type' property
3016 (here-doc-delim nil) ; via `syntax-type' property
3017 (format nil) ; via `syntax-type' property
3018 (in-pod nil) ; via `in-pod' property
3019 (comment-special:at-beginning-of-line nil)
3020 (string t)
3021 (comment nil))
3022 "Alist of indentation rules for CPerl mode.
3023 The values mean:
3024 nil: do not indent;
3025 number: add this amount of indentation.")
3026
3027 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3028 "Return appropriate indentation for current line as Perl code.
3029 In usual case returns an integer: the column to indent to.
3030 Returns nil if line starts inside a string, t if in a comment.
3031
3032 Will not correct the indentation for labels, but will correct it for braces
3033 and closing parentheses and brackets."
3034 ;; This code is still a broken architecture: in some cases we need to
3035 ;; compensate for some modifications which `cperl-indent-line' will add later
3036 (save-excursion
3037 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3038 (cond
3039 ;;((or (null i) (eq i t) (numberp i))
3040 ;; i)
3041 ((vectorp i)
3042 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3043 (cond
3044 (what (cadr what)) ; Load from table
3045 ;;
3046 ;; Indenters for regular expressions with //x and qw()
3047 ;;
3048 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3049 (goto-char (elt i 1))
3050 (condition-case nil ; Use indentation of the 1st part
3051 (forward-sexp -1))
3052 (current-column))
3053 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3054 (cond ;;; [indentable terminator start-pos is-block]
3055 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3056 (goto-char (elt i 2)) ; After opening parens
3057 (1- (current-column)))
3058 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3059 (goto-char (elt i 2))
3060 (+ (or cperl-regexp-indent-step cperl-indent-level)
3061 -1
3062 (current-column)))
3063 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3064 ;; Indent as the level after closing parens
3065 (goto-char (elt i 2)) ; indent line
3066 (skip-chars-forward " \t)") ; Skip closing parens
3067 (setq p (point))
3068 (goto-char (elt i 3)) ; previous line
3069 (skip-chars-forward " \t)") ; Skip closing parens
3070 ;; Number of parens in between:
3071 (setq p (nth 0 (parse-partial-sexp (point) p))
3072 what (elt i 4)) ; First char on current line
3073 (goto-char (elt i 3)) ; previous line
3074 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3075 (cond ((eq what ?\) )
3076 (- cperl-close-paren-offset)) ; compensate
3077 ((eq what ?\| )
3078 (- (or cperl-regexp-indent-step cperl-indent-level)))
3079 (t 0))
3080 (if (eq (following-char) ?\| )
3081 (or cperl-regexp-indent-step cperl-indent-level)
3082 0)
3083 (current-column)))
3084 (t
3085 (error "Unrecognized value of indent: %s" i))))
3086 ;;
3087 ;; Indenter for stuff at toplevel
3088 ;;
3089 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3090 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3091 (goto-char (elt i 1)) ; start = Good place to start parsing
3092 (- (current-indentation) ;
3093 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3094 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3095 ;; Look at previous line that's at column 0
3096 ;; to determine whether we are in top-level decls
3097 ;; or function's arg decls. Set basic-indent accordingly.
3098 ;; Now add a little if this is a continuation line.
3099 (if (elt i 3) ; state (XXX What is the semantic???)
3100 0
3101 cperl-continued-statement-offset)))
3102 ;;
3103 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3104 ;;
3105 ((eq 'in-parens (elt i 0))
3106 ;; in-parens char-after old-indent-point is-brace containing-sexp
3107
3108 ;; group is an expression, not a block:
3109 ;; indent to just after the surrounding open parens,
3110 ;; skip blanks if we do not close the expression.
3111 (+ (progn
3112 (goto-char (elt i 2)) ; old-indent-point
3113 (current-column))
3114 (if (and (elt i 3) ; is-brace
3115 (eq (elt i 1) ?\})) ; char-after
3116 ;; Correct indentation of trailing ?\}
3117 (+ cperl-indent-level cperl-close-paren-offset)
3118 0)))
3119 ;;
3120 ;; Indenter for continuation lines
3121 ;;
3122 ((eq 'continuation (elt i 0))
3123 ;; [continuation statement-start char-after is-block is-brace]
3124 (goto-char (elt i 1)) ; statement-start
3125 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3126 0 ; Closing parenth
3127 cperl-continued-statement-offset)
3128 (if (or (elt i 3) ; is-block
3129 (not (elt i 4)) ; is-brace
3130 (not (eq (elt i 2) ?\}))) ; char-after
3131 0
3132 ;; Now it is a hash reference
3133 (+ cperl-indent-level cperl-close-paren-offset))
3134 ;; Labels do not take :: ...
3135 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3136 (if (> (current-indentation) cperl-min-label-indent)
3137 (- (current-indentation) cperl-label-offset)
3138 ;; Do not move `parse-data', this should
3139 ;; be quick anyway (this comment comes
3140 ;; from different location):
3141 (cperl-calculate-indent))
3142 (current-column))
3143 (if (eq (elt i 2) ?\{) ; char-after
3144 cperl-continued-brace-offset 0)))
3145 ;;
3146 ;; Indenter for lines in a block which are not leading lines
3147 ;;
3148 ((eq 'have-prev-sibling (elt i 0))
3149 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3150 (goto-char (elt i 1)) ; sibling-beg
3151 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
3152 (if (> (current-indentation)
3153 cperl-min-label-indent)
3154 (- (current-indentation) cperl-label-offset)
3155 ;; Do not believe: `max' was involved in calculation of indent
3156 (+ cperl-indent-level
3157 (save-excursion
3158 (goto-char (elt i 3)) ; block-start
3159 (current-indentation))))
3160 (current-column)))
3161 ;;
3162 ;; Indenter for the first line in a block
3163 ;;
3164 ((eq 'code-start-in-block (elt i 0))
3165 ;;[code-start-in-block before-brace char-after
3166 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3167 ;; group-starts-before-start-of-sub start-of-control-group]
3168 (goto-char (elt i 1))
3169 ;; For open brace in column zero, don't let statement
3170 ;; start there too. If cperl-indent-level=0,
3171 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3172 (+ (if (and (bolp) (zerop cperl-indent-level))
3173 (+ cperl-brace-offset cperl-continued-statement-offset)
3174 cperl-indent-level)
3175 (if (and (elt i 3) ; is-a-HASH-ref
3176 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3177 (+ cperl-indent-level cperl-close-paren-offset)
3178 0)
3179 ;; Unless openbrace is the first nonwhite thing on the line,
3180 ;; add the cperl-brace-imaginary-offset.
3181 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3182 cperl-brace-imaginary-offset)
3183 (progn
3184 (goto-char (elt i 6)) ; start-of-control-group
3185 (if (elt i 5) ; group-starts-before-start-of-sub
3186 (current-column)
3187 ;; Get initial indentation of the line we are on.
3188 ;; If line starts with label, calculate label indentation
3189 (if (save-excursion
3190 (beginning-of-line)
3191 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3192 (if (> (current-indentation) cperl-min-label-indent)
3193 (- (current-indentation) cperl-label-offset)
3194 ;; Do not move `parse-data', this should
3195 ;; be quick anyway:
3196 (cperl-calculate-indent))
3197 (current-indentation))))))
3198 (t
3199 (error "Unrecognized value of indent: %s" i))))
3200 (t
3201 (error "Got strange value of indent: %s" i))))))
3202
3203 (defun cperl-calculate-indent-within-comment ()
3204 "Return the indentation amount for line, assuming that
3205 the current line is to be regarded as part of a block comment."
3206 (let (end star-start)
3207 (save-excursion
3208 (beginning-of-line)
3209 (skip-chars-forward " \t")
3210 (setq end (point))
3211 (and (= (following-char) ?#)
3212 (forward-line -1)
3213 (cperl-to-comment-or-eol)
3214 (setq end (point)))
3215 (goto-char end)
3216 (current-column))))
3217
3218
3219 (defun cperl-to-comment-or-eol ()
3220 "Go to position before comment on the current line, or to end of line.
3221 Returns true if comment is found. In POD will not move the point."
3222 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3223 ;; then looks for literal # or end-of-line.
3224 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3225 (or cperl-font-locking
3226 (cperl-update-syntaxification lim lim))
3227 (beginning-of-line)
3228 (if (setq pr (get-text-property (point) 'syntax-type))
3229 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3230 (if (or (eq pr 'pod)
3231 (if (or (not e) (> e lim)) ; deep inside a group
3232 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3233 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3234 ;; Else - need to do it the hard way
3235 (and (and e (<= e lim))
3236 (goto-char e))
3237 (while (not stop-in)
3238 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3239 ; stop at comment
3240 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3241 (if (nth 4 state) ; After `#';
3242 ; (nth 2 state) can be
3243 ; beginning of m,s,qq and so
3244 ; on
3245 (if (nth 2 state)
3246 (progn
3247 (setq cpoint (point))
3248 (goto-char (nth 2 state))
3249 (cond
3250 ((looking-at "\\(s\\|tr\\)\\>")
3251 (or (re-search-forward
3252 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3253 lim 'move)
3254 (setq stop-in t)))
3255 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3256 (or (re-search-forward
3257 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3258 lim 'move)
3259 (setq stop-in t)))
3260 (t ; It was fair comment
3261 (setq stop-in t) ; Finish
3262 (goto-char (1- cpoint)))))
3263 (setq stop-in t) ; Finish
3264 (forward-char -1))
3265 (setq stop-in t))) ; Finish
3266 (nth 4 state))))
3267
3268 (defsubst cperl-modify-syntax-type (at how)
3269 (if (< at (point-max))
3270 (progn
3271 (put-text-property at (1+ at) 'syntax-table how)
3272 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3273
3274 (defun cperl-protect-defun-start (s e)
3275 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3276 (save-excursion
3277 (goto-char s)
3278 (while (re-search-forward "^\\s(" e 'to-end)
3279 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3280
3281 (defun cperl-commentify (bb e string &optional noface)
3282 (if cperl-use-syntax-table-text-property
3283 (if (eq noface 'n) ; Only immediate
3284 nil
3285 ;; We suppose that e is _after_ the end of construction, as after eol.
3286 (setq string (if string cperl-st-sfence cperl-st-cfence))
3287 (if (> bb (- e 2))
3288 ;; one-char string/comment?!
3289 (cperl-modify-syntax-type bb cperl-st-punct)
3290 (cperl-modify-syntax-type bb string)
3291 (cperl-modify-syntax-type (1- e) string))
3292 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3293 (put-text-property (1+ bb) (1- e)
3294 'syntax-table cperl-string-syntax-table))
3295 (cperl-protect-defun-start bb e))
3296 ;; Fontify
3297 (or noface
3298 (not cperl-pod-here-fontify)
3299 (put-text-property bb e 'face (if string 'font-lock-string-face
3300 'font-lock-comment-face)))))
3301
3302 (defvar cperl-starters '(( ?\( . ?\) )
3303 ( ?\[ . ?\] )
3304 ( ?\{ . ?\} )
3305 ( ?\< . ?\> )))
3306
3307 (defun cperl-cached-syntax-table (st)
3308 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3309 All the entries of the syntax table are \".\", except for a backslash, which
3310 is quoting."
3311 (if (car-safe st)
3312 (car st)
3313 (setcar st (make-syntax-table))
3314 (setq st (car st))
3315 (let ((i 0))
3316 (while (< i 256)
3317 (modify-syntax-entry i "." st)
3318 (setq i (1+ i))))
3319 (modify-syntax-entry ?\\ "\\" st)
3320 st))
3321
3322 (defun cperl-forward-re (lim end is-2arg st-l err-l argument
3323 &optional ostart oend)
3324 "Find the end of a regular expression or a stringish construct (q[] etc).
3325 The point should be before the starting delimiter.
3326
3327 Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3328 is s/// or tr/// like expression. If END is nil, generates an error
3329 message if needed. If SET-ST is non-nil, will use (or generate) a
3330 cached syntax table in ST-L. If ERR-L is non-nil, will store the
3331 error message in its CAR (unless it already contains some error
3332 message). ARGUMENT should be the name of the construct (used in error
3333 messages). OSTART, OEND may be set in recursive calls when processing
3334 the second argument of 2ARG construct.
3335
3336 Works *before* syntax recognition is done. In IS-2ARG situation may
3337 modify syntax-type text property if the situation is too hard."
3338 (let (b starter ender st i i2 go-forward reset-st set-st)
3339 (skip-chars-forward " \t")
3340 ;; ender means matching-char matcher.
3341 (setq b (point)
3342 starter (if (eobp) 0 (char-after b))
3343 ender (cdr (assoc starter cperl-starters)))
3344 ;; What if starter == ?\\ ????
3345 (setq st (cperl-cached-syntax-table st-l))
3346 (setq set-st t)
3347 ;; Whether we have an intermediate point
3348 (setq i nil)
3349 ;; Prepare the syntax table:
3350 (if (not ender) ; m/blah/, s/x//, s/x/y/
3351 (modify-syntax-entry starter "$" st)
3352 (modify-syntax-entry starter (concat "(" (list ender)) st)
3353 (modify-syntax-entry ender (concat ")" (list starter)) st))
3354 (condition-case bb
3355 (progn
3356 ;; We use `$' syntax class to find matching stuff, but $$
3357 ;; is recognized the same as $, so we need to check this manually.
3358 (if (and (eq starter (char-after (cperl-1+ b)))
3359 (not ender))
3360 ;; $ has TeXish matching rules, so $$ equiv $...
3361 (forward-char 2)
3362 (setq reset-st (syntax-table))
3363 (set-syntax-table st)
3364 (forward-sexp 1)
3365 (if (<= (point) (1+ b))
3366 (error "Unfinished regular expression"))
3367 (set-syntax-table reset-st)
3368 (setq reset-st nil)
3369 ;; Now the problem is with m;blah;;
3370 (and (not ender)
3371 (eq (preceding-char)
3372 (char-after (- (point) 2)))
3373 (save-excursion
3374 (forward-char -2)
3375 (= 0 (% (skip-chars-backward "\\\\") 2)))
3376 (forward-char -1)))
3377 ;; Now we are after the first part.
3378 (and is-2arg ; Have trailing part
3379 (not ender)
3380 (eq (following-char) starter) ; Empty trailing part
3381 (progn
3382 (or (eq (char-syntax (following-char)) ?.)
3383 ;; Make trailing letter into punctuation
3384 (cperl-modify-syntax-type (point) cperl-st-punct))
3385 (setq is-2arg nil go-forward t))) ; Ignore the tail
3386 (if is-2arg ; Not number => have second part
3387 (progn
3388 (setq i (point) i2 i)
3389 (if ender
3390 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3391 (progn
3392 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3393 (goto-char (match-end 0))
3394 (skip-chars-forward " \t\n\f"))
3395 (setq i2 (point))))
3396 (forward-char -1))
3397 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3398 (if ender (modify-syntax-entry ender "." st))
3399 (setq set-st nil)
3400 (setq ender (cperl-forward-re lim end nil st-l err-l
3401 argument starter ender)
3402 ender (nth 2 ender)))))
3403 (error (goto-char lim)
3404 (setq set-st nil)
3405 (if reset-st
3406 (set-syntax-table reset-st))
3407 (or end
3408 (and cperl-brace-recursing
3409 (or (eq ostart ?\{)
3410 (eq starter ?\{)))
3411 (message
3412 "End of `%s%s%c ... %c' string/RE not found: %s"
3413 argument
3414 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3415 starter (or ender starter) bb)
3416 (or (car err-l) (setcar err-l b)))))
3417 (if set-st
3418 (progn
3419 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3420 (if ender (modify-syntax-entry ender "." st))))
3421 ;; i: have 2 args, after end of the first arg
3422 ;; i2: start of the second arg, if any (before delim if `ender').
3423 ;; ender: the last arg bounded by parens-like chars, the second one of them
3424 ;; starter: the starting delimiter of the first arg
3425 ;; go-forward: has 2 args, and the second part is empty
3426 (list i i2 ender starter go-forward)))
3427
3428 (defun cperl-forward-group-in-re (&optional st-l)
3429 "Find the end of a group in a REx.
3430 Return the error message (if any). Does not work if delimiter is `)'.
3431 Works before syntax recognition is done."
3432 ;; Works *before* syntax recognition is done
3433 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3434 (let (st b reset-st)
3435 (condition-case b
3436 (progn
3437 (setq st (cperl-cached-syntax-table st-l))
3438 (modify-syntax-entry ?\( "()" st)
3439 (modify-syntax-entry ?\) ")(" st)
3440 (setq reset-st (syntax-table))
3441 (set-syntax-table st)
3442 (forward-sexp 1))
3443 (error (message
3444 "cperl-forward-group-in-re: error %s" b)))
3445 ;; now restore the initial state
3446 (if st
3447 (progn
3448 (modify-syntax-entry ?\( "." st)
3449 (modify-syntax-entry ?\) "." st)))
3450 (if reset-st
3451 (set-syntax-table reset-st))
3452 b))
3453
3454
3455 (defvar font-lock-string-face)
3456 ;;(defvar font-lock-reference-face)
3457 (defvar font-lock-constant-face)
3458 (defsubst cperl-postpone-fontification (b e type val &optional now)
3459 ;; Do after syntactic fontification?
3460 (if cperl-syntaxify-by-font-lock
3461 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3462 (put-text-property b e type val)))
3463
3464 ;;; Here is how the global structures (those which cannot be
3465 ;;; recognized locally) are marked:
3466 ;; a) PODs:
3467 ;; Start-to-end is marked `in-pod' ==> t
3468 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3469 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3470 ;; b) HEREs:
3471 ;; Start-to-end is marked `here-doc-group' ==> t
3472 ;; The body is marked `syntax-type' ==> `here-doc'
3473 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3474 ;; c) FORMATs:
3475 ;; First line (to =) marked `first-format-line' ==> t
3476 ;; After-this--to-end is marked `syntax-type' ==> `format'
3477 ;; d) 'Q'uoted string:
3478 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3479 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3480 ;; second part of s///e is marked `syntax-type' ==> `multiline'
3481 ;; e) Attributes of subroutines: `attrib-group' ==> t
3482 ;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3483 ;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3484
3485 ;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3486 ;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3487
3488 (defun cperl-unwind-to-safe (before &optional end)
3489 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3490 (let ((pos (point)) opos)
3491 (while (and pos (progn
3492 (beginning-of-line)
3493 (get-text-property (setq pos (point)) 'syntax-type)))
3494 (setq opos pos
3495 pos (cperl-beginning-of-property pos 'syntax-type))
3496 (if (eq pos (point-min))
3497 (setq pos nil))
3498 (if pos
3499 (if before
3500 (progn
3501 (goto-char (cperl-1- pos))
3502 (beginning-of-line)
3503 (setq pos (point)))
3504 (goto-char (setq pos (cperl-1- pos))))
3505 ;; Up to the start
3506 (goto-char (point-min))))
3507 ;; Skip empty lines
3508 (and (looking-at "\n*=")
3509 (/= 0 (skip-chars-backward "\n"))
3510 (forward-char))
3511 (setq pos (point))
3512 (if end
3513 ;; Do the same for end, going small steps
3514 (save-excursion
3515 (while (and end (get-text-property end 'syntax-type))
3516 (setq pos end
3517 end (next-single-property-change end 'syntax-type nil (point-max)))
3518 (if end (progn (goto-char end)
3519 (or (bolp) (forward-line 1))
3520 (setq end (point)))))
3521 (or end pos)))))
3522
3523 ;;; These are needed for byte-compile (at least with v19)
3524 (defvar cperl-nonoverridable-face)
3525 (defvar font-lock-variable-name-face)
3526 (defvar font-lock-function-name-face)
3527 (defvar font-lock-keyword-face)
3528 (defvar font-lock-builtin-face)
3529 (defvar font-lock-type-face)
3530 (defvar font-lock-comment-face)
3531 (defvar font-lock-warning-face)
3532
3533 (defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3534 "Syntaxically mark (and fontify) attributes of a subroutine.
3535 Should be called with the point before leading colon of an attribute."
3536 ;; Works *before* syntax recognition is done
3537 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3538 (let (st b p reset-st after-first (start (point)) start1 end1)
3539 (condition-case b
3540 (while (looking-at
3541 (concat
3542 "\\(" ; 1=optional? colon
3543 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3544 "\\)"
3545 (if after-first "?" "")
3546 ;; No space between name and paren allowed...
3547 "\\(\\sw+\\)" ; 3=name
3548 "\\((\\)?")) ; 4=optional paren
3549 (and (match-beginning 1)
3550 (cperl-postpone-fontification
3551 (match-beginning 0) (cperl-1+ (match-beginning 0))
3552 'face font-lock-constant-face))
3553 (setq start1 (match-beginning 3) end1 (match-end 3))
3554 (cperl-postpone-fontification start1 end1
3555 'face font-lock-constant-face)
3556 (goto-char end1) ; end or before `('
3557 (if (match-end 4) ; Have attribute arguments...
3558 (progn
3559 (if st nil
3560 (setq st (cperl-cached-syntax-table st-l))
3561 (modify-syntax-entry ?\( "()" st)
3562 (modify-syntax-entry ?\) ")(" st))
3563 (setq reset-st (syntax-table) p (point))
3564 (set-syntax-table st)
3565 (forward-sexp 1)
3566 (set-syntax-table reset-st)
3567 (setq reset-st nil)
3568 (cperl-commentify p (point) t))) ; mark as string
3569 (forward-comment (buffer-size))
3570 (setq after-first t))
3571 (error (message
3572 "L%d: attribute `%s': %s"
3573 (count-lines (point-min) (point))
3574 (and start1 end1 (buffer-substring start1 end1)) b)
3575 (setq start nil)))
3576 (and start
3577 (progn
3578 (put-text-property start (point)
3579 'attrib-group (if (looking-at "{") t 0))
3580 (and pos
3581 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3582 ;; Apparently, we do not need `multiline': faces added now
3583 (put-text-property (+ 3 pos) (cperl-1+ (point))
3584 'syntax-type 'sub-decl))
3585 (and b-fname ; Fontify here: the following condition
3586 (cperl-postpone-fontification ; is too hard to determine by
3587 b-fname e-fname 'face ; a REx, so do it here
3588 (if (looking-at "{")
3589 font-lock-function-name-face
3590 font-lock-variable-name-face)))))
3591 ;; now restore the initial state
3592 (if st
3593 (progn
3594 (modify-syntax-entry ?\( "." st)
3595 (modify-syntax-entry ?\) "." st)))
3596 (if reset-st
3597 (set-syntax-table reset-st))))
3598
3599 (defsubst cperl-look-at-leading-count (is-x-REx e)
3600 (if (and
3601 (< (point) e)
3602 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3603 (1- e) t)) ; return nil on failure, no moving
3604 (if (eq ?\{ (preceding-char)) nil
3605 (cperl-postpone-fontification
3606 (1- (point)) (point)
3607 'face font-lock-warning-face))))
3608
3609 ;; Do some smarter-highlighting
3610 ;; XXXX Currently ignores alphanum/dash delims,
3611 (defsubst cperl-highlight-charclass (endbracket dashface bsface onec-space)
3612 (let ((l '(1 5 7)) ll lle lll
3613 ;; 2 groups, the first takes the whole match (include \[trnfabe])
3614 (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{[^{}]*}" "\\)")))
3615 (while ; look for unescaped - between non-classes
3616 (re-search-forward
3617 ;; On 19.33, certain simplifications lead
3618 ;; to bugs (as in [^a-z] \\| [trnfabe] )
3619 (concat ; 1: SingleChar (include \[trnfabe])
3620 singleChar
3621 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^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{[^{}]*}" "\\)"
3622 "\\(" ; 3: DASH SingleChar (match optionally)
3623 "\\(-\\)" ; 4: DASH
3624 singleChar ; 5: SingleChar
3625 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^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{[^{}]*}" "\\)"
3626 "\\)?"
3627 "\\|"
3628 "\\(" ; 7: other escapes
3629 "\\\\[pP]" "\\([^{]\\|{[^{}]*}\\)"
3630 "\\|" "\\\\[^pP]" "\\)"
3631 )
3632 endbracket 'toend)
3633 (if (match-beginning 4)
3634 (cperl-postpone-fontification
3635 (match-beginning 4) (match-end 4)
3636 'face dashface))
3637 ;; save match data (for looking-at)
3638 (setq lll (mapcar (function (lambda (elt) (cons (match-beginning elt)
3639 (match-end elt)))) l))
3640 (while lll
3641 (setq ll (car lll))
3642 (setq lle (cdr ll)
3643 ll (car ll))
3644 ;; (message "Got %s of %s" ll l)
3645 (if (and ll (eq (char-after ll) ?\\ ))
3646 (save-excursion
3647 (goto-char ll)
3648 (cperl-postpone-fontification ll (1+ ll)
3649 'face bsface)
3650 (if (looking-at "\\\\[a-zA-Z0-9]")
3651 (cperl-postpone-fontification (1+ ll) lle
3652 'face onec-space))))
3653 (setq lll (cdr lll))))
3654 (goto-char endbracket) ; just in case something misbehaves???
3655 t))
3656
3657 ;;; Debugging this may require (setq max-specpdl-size 2000)...
3658 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3659 "Scans the buffer for hard-to-parse Perl constructions.
3660 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3661 the sections using `cperl-pod-head-face', `cperl-pod-face',
3662 `cperl-here-face'."
3663 (interactive)
3664 (or min (setq min (point-min)
3665 cperl-syntax-state nil
3666 cperl-syntax-done-to min))
3667 (or max (setq max (point-max)))
3668 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3669 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3670 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3671 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3672 (modified (buffer-modified-p)) overshoot is-o-REx name
3673 (after-change-functions nil)
3674 (cperl-font-locking t)
3675 (use-syntax-state (and cperl-syntax-state
3676 (>= min (car cperl-syntax-state))))
3677 (state-point (if use-syntax-state
3678 (car cperl-syntax-state)
3679 (point-min)))
3680 (state (if use-syntax-state
3681 (cdr cperl-syntax-state)))
3682 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3683 (st-l (list nil)) (err-l (list nil))
3684 ;; Somehow font-lock may be not loaded yet...
3685 ;; (e.g., when building TAGS via command-line call)
3686 (font-lock-string-face (if (boundp 'font-lock-string-face)
3687 font-lock-string-face
3688 'font-lock-string-face))
3689 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3690 font-lock-constant-face
3691 'font-lock-constant-face))
3692 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3693 (if (boundp 'font-lock-function-name-face)
3694 font-lock-function-name-face
3695 'font-lock-function-name-face))
3696 (font-lock-variable-name-face ; interpolated vars and ({})-code
3697 (if (boundp 'font-lock-variable-name-face)
3698 font-lock-variable-name-face
3699 'font-lock-variable-name-face))
3700 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3701 (if (boundp 'font-lock-function-name-face)
3702 font-lock-function-name-face
3703 'font-lock-function-name-face))
3704 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3705 (if (boundp 'font-lock-constant-face)
3706 font-lock-constant-face
3707 'font-lock-constant-face))
3708 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3709 (if (boundp 'font-lock-builtin-face)
3710 font-lock-builtin-face
3711 'font-lock-builtin-face))
3712 (font-lock-comment-face
3713 (if (boundp 'font-lock-comment-face)
3714 font-lock-comment-face
3715 'font-lock-comment-face))
3716 (font-lock-warning-face
3717 (if (boundp 'font-lock-warning-face)
3718 font-lock-warning-face
3719 'font-lock-warning-face))
3720 (my-cperl-REx-ctl-face ; (|)
3721 (if (boundp 'font-lock-keyword-face)
3722 font-lock-keyword-face
3723 'font-lock-keyword-face))
3724 (my-cperl-REx-modifiers-face ; //gims
3725 (if (boundp 'cperl-nonoverridable-face)
3726 cperl-nonoverridable-face
3727 'cperl-nonoverridable-face))
3728 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3729 (if (boundp 'font-lock-type-face)
3730 font-lock-type-face
3731 'font-lock-type-face))
3732 (stop-point (if ignore-max
3733 (point-max)
3734 max))
3735 (search
3736 (concat
3737 "\\(\\`\n?\\|^\n\\)=" ; POD
3738 "\\|"
3739 ;; One extra () before this:
3740 "<<" ; HERE-DOC
3741 "\\(" ; 1 + 1
3742 ;; First variant "BLAH" or just ``.
3743 "[ \t]*" ; Yes, whitespace is allowed!
3744 "\\([\"'`]\\)" ; 2 + 1 = 3
3745 "\\([^\"'`\n]*\\)" ; 3 + 1
3746 "\\3"
3747 "\\|"
3748 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3749 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3750 ;; Do not have <<= or << 30 or <<30 or << $blah.
3751 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3752 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3753 "\\)"
3754 "\\|"
3755 ;; 1+6 extra () before this:
3756 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3757 (if cperl-use-syntax-table-text-property
3758 (concat
3759 "\\|"
3760 ;; 1+6+2=9 extra () before this:
3761 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3762 "\\|"
3763 ;; 1+6+2+1=10 extra () before this:
3764 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3765 "\\|"
3766 ;; 1+6+2+1+1=11 extra () before this
3767 "\\<sub\\>" ; sub with proto/attr
3768 "\\("
3769 cperl-white-and-comment-rex
3770 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3771 "\\("
3772 cperl-maybe-white-and-comment-rex
3773 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3774 "\\|"
3775 ;; 1+6+2+1+1+6=17 extra () before this:
3776 "\\$\\(['{]\\)" ; $' or ${foo}
3777 "\\|"
3778 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3779 ;; we do not support intervening comments...):
3780 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3781 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3782 "\\|"
3783 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3784 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3785 "\\|"
3786 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3787 ""))))
3788 (unwind-protect
3789 (progn
3790 (save-excursion
3791 (or non-inter
3792 (message "Scanning for \"hard\" Perl constructions..."))
3793 ;;(message "find: %s --> %s" min max)
3794 (and cperl-pod-here-fontify
3795 ;; We had evals here, do not know why...
3796 (setq face cperl-pod-face
3797 head-face cperl-pod-head-face
3798 here-face cperl-here-face))
3799 (remove-text-properties min max
3800 '(syntax-type t in-pod t syntax-table t
3801 attrib-group t
3802 REx-interpolated t
3803 cperl-postpone t
3804 syntax-subtype t
3805 rear-nonsticky t
3806 front-sticky t
3807 here-doc-group t
3808 first-format-line t
3809 REx-part2 t
3810 indentable t))
3811 ;; Need to remove face as well...
3812 (goto-char min)
3813 (and (eq system-type 'emx)
3814 (eq (point) 1)
3815 (let ((case-fold-search t))
3816 (looking-at "extproc[ \t]")) ; Analogue of #!
3817 (cperl-commentify min
3818 (save-excursion (end-of-line) (point))
3819 nil))
3820 (while (and
3821 (< (point) max)
3822 (re-search-forward search max t))
3823 (setq tmpend nil) ; Valid for most cases
3824 (setq b (match-beginning 0)
3825 state (save-excursion (parse-partial-sexp
3826 state-point b nil nil state))
3827 state-point b)
3828 (cond
3829 ;; 1+6+2+1+1+6=17 extra () before this:
3830 ;; "\\$\\(['{]\\)"
3831 ((match-beginning 18) ; $' or ${foo}
3832 (if (eq (preceding-char) ?\') ; $'
3833 (progn
3834 (setq b (1- (point))
3835 state (parse-partial-sexp
3836 state-point (1- b) nil nil state)
3837 state-point (1- b))
3838 (if (nth 3 state) ; in string
3839 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3840 (goto-char (1+ b)))
3841 ;; else: ${
3842 (setq bb (match-beginning 0))
3843 (cperl-modify-syntax-type bb cperl-st-punct)))
3844 ;; No processing in strings/comments beyond this point:
3845 ((or (nth 3 state) (nth 4 state))
3846 t) ; Do nothing in comment/string
3847 ((match-beginning 1) ; POD section
3848 ;; "\\(\\`\n?\\|^\n\\)="
3849 (setq b (match-beginning 0)
3850 state (parse-partial-sexp
3851 state-point b nil nil state)
3852 state-point b)
3853 (if (or (nth 3 state) (nth 4 state)
3854 (looking-at "cut\\>"))
3855 (if (or (nth 3 state) (nth 4 state) ignore-max)
3856 nil ; Doing a chunk only
3857 (message "=cut is not preceded by a POD section")
3858 (or (car err-l) (setcar err-l (point))))
3859 (beginning-of-line)
3860
3861 (setq b (point)
3862 bb b
3863 tb (match-beginning 0)
3864 b1 nil) ; error condition
3865 ;; We do not search to max, since we may be called from
3866 ;; some hook of fontification, and max is random
3867 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3868 (progn
3869 (goto-char b)
3870 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3871 (progn
3872 (message "=cut is not preceded by an empty line")
3873 (setq b1 t)
3874 (or (car err-l) (setcar err-l b))))))
3875 (beginning-of-line 2) ; An empty line after =cut is not POD!
3876 (setq e (point))
3877 (and (> e max)
3878 (progn
3879 (remove-text-properties
3880 max e '(syntax-type t in-pod t syntax-table t
3881 attrib-group t
3882 REx-interpolated t
3883 cperl-postpone t
3884 syntax-subtype t
3885 here-doc-group t
3886 rear-nonsticky t
3887 front-sticky t
3888 first-format-line t
3889 REx-part2 t
3890 indentable t))
3891 (setq tmpend tb)))
3892 (put-text-property b e 'in-pod t)
3893 (put-text-property b e 'syntax-type 'in-pod)
3894 (goto-char b)
3895 (while (re-search-forward "\n\n[ \t]" e t)
3896 ;; We start 'pod 1 char earlier to include the preceding line
3897 (beginning-of-line)
3898 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3899 (cperl-put-do-not-fontify b (point) t)
3900 ;; mark the non-literal parts as PODs
3901 (if cperl-pod-here-fontify
3902 (cperl-postpone-fontification b (point) 'face face t))
3903 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3904 (beginning-of-line)
3905 (setq b (point)))
3906 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3907 (cperl-put-do-not-fontify (point) e t)
3908 (if cperl-pod-here-fontify
3909 (progn
3910 ;; mark the non-literal parts as PODs
3911 (cperl-postpone-fontification (point) e 'face face t)
3912 (goto-char bb)
3913 (if (looking-at
3914 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3915 ;; mark the headers
3916 (cperl-postpone-fontification
3917 (match-beginning 1) (match-end 1)
3918 'face head-face))
3919 (while (re-search-forward
3920 ;; One paragraph
3921 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3922 e 'toend)
3923 ;; mark the headers
3924 (cperl-postpone-fontification
3925 (match-beginning 1) (match-end 1)
3926 'face head-face))))
3927 (cperl-commentify bb e nil)
3928 (goto-char e)
3929 (or (eq e (point-max))
3930 (forward-char -1)))) ; Prepare for immediate POD start.
3931 ;; Here document
3932 ;; We can do many here-per-line;
3933 ;; but multiline quote on the same line as <<HERE confuses us...
3934 ;; ;; One extra () before this:
3935 ;;"<<"
3936 ;; "\\(" ; 1 + 1
3937 ;; ;; First variant "BLAH" or just ``.
3938 ;; "[ \t]*" ; Yes, whitespace is allowed!
3939 ;; "\\([\"'`]\\)" ; 2 + 1
3940 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3941 ;; "\\3"
3942 ;; "\\|"
3943 ;; ;; Second variant: Identifier or \ID or empty
3944 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3945 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3946 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3947 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3948 ;; "\\)"
3949 ((match-beginning 2) ; 1 + 1
3950 (setq b (point)
3951 tb (match-beginning 0)
3952 c (and ; not HERE-DOC
3953 (match-beginning 5)
3954 (save-match-data
3955 (or (looking-at "[ \t]*(") ; << function_call()
3956 (save-excursion ; 1 << func_name, or $foo << 10
3957 (condition-case nil
3958 (progn
3959 (goto-char tb)
3960 ;;; XXX What to do: foo <<bar ???
3961 ;;; XXX Need to support print {a} <<B ???
3962 (forward-sexp -1)
3963 (save-match-data
3964 ; $foo << b; $f .= <<B;
3965 ; ($f+1) << b; a($f) . <<B;
3966 ; foo 1, <<B; $x{a} <<b;
3967 (cond
3968 ((looking-at "[0-9$({]")
3969 (forward-sexp 1)
3970 (and
3971 (looking-at "[ \t]*<<")
3972 (condition-case nil
3973 ;; print $foo <<EOF
3974 (progn
3975 (forward-sexp -2)
3976 (not
3977 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
3978 (error t)))))))
3979 (error nil))) ; func(<<EOF)
3980 (and (not (match-beginning 6)) ; Empty
3981 (looking-at
3982 "[ \t]*[=0-9$@%&(]"))))))
3983 (if c ; Not here-doc
3984 nil ; Skip it.
3985 (setq c (match-end 2)) ; 1 + 1
3986 (if (match-beginning 5) ;4 + 1
3987 (setq b1 (match-beginning 5) ; 4 + 1
3988 e1 (match-end 5)) ; 4 + 1
3989 (setq b1 (match-beginning 4) ; 3 + 1
3990 e1 (match-end 4))) ; 3 + 1
3991 (setq tag (buffer-substring b1 e1)
3992 qtag (regexp-quote tag))
3993 (cond (cperl-pod-here-fontify
3994 ;; Highlight the starting delimiter
3995 (cperl-postpone-fontification
3996 b1 e1 'face my-cperl-delimiters-face)
3997 (cperl-put-do-not-fontify b1 e1 t)))
3998 (forward-line)
3999 (setq i (point))
4000 (if end-of-here-doc
4001 (goto-char end-of-here-doc))
4002 (setq b (point))
4003 ;; We do not search to max, since we may be called from
4004 ;; some hook of fontification, and max is random
4005 (or (and (re-search-forward (concat "^" qtag "$")
4006 stop-point 'toend)
4007 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4008 )
4009 (progn ; Pretend we matched at the end
4010 (goto-char (point-max))
4011 (re-search-forward "\\'")
4012 (message "End of here-document `%s' not found." tag)
4013 (or (car err-l) (setcar err-l b))))
4014 (if cperl-pod-here-fontify
4015 (progn
4016 ;; Highlight the ending delimiter
4017 (cperl-postpone-fontification
4018 (match-beginning 0) (match-end 0)
4019 'face my-cperl-delimiters-face)
4020 (cperl-put-do-not-fontify b (match-end 0) t)
4021 ;; Highlight the HERE-DOC
4022 (cperl-postpone-fontification b (match-beginning 0)
4023 'face here-face)))
4024 (setq e1 (cperl-1+ (match-end 0)))
4025 (put-text-property b (match-beginning 0)
4026 'syntax-type 'here-doc)
4027 (put-text-property (match-beginning 0) e1
4028 'syntax-type 'here-doc-delim)
4029 (put-text-property b e1 'here-doc-group t)
4030 ;; This makes insertion at the start of HERE-DOC update
4031 ;; the whole construct:
4032 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
4033 (cperl-commentify b e1 nil)
4034 (cperl-put-do-not-fontify b (match-end 0) t)
4035 ;; Cache the syntax info...
4036 (setq cperl-syntax-state (cons state-point state))
4037 ;; ... and process the rest of the line...
4038 (setq overshoot
4039 (elt ; non-inter ignore-max
4040 (cperl-find-pods-heres c i t end t e1) 1))
4041 (if (and overshoot (> overshoot (point)))
4042 (goto-char overshoot)
4043 (setq overshoot e1))
4044 (if (> e1 max)
4045 (setq tmpend tb))))
4046 ;; format
4047 ((match-beginning 8)
4048 ;; 1+6=7 extra () before this:
4049 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4050 (setq b (point)
4051 name (if (match-beginning 8) ; 7 + 1
4052 (buffer-substring (match-beginning 8) ; 7 + 1
4053 (match-end 8)) ; 7 + 1
4054 "")
4055 tb (match-beginning 0))
4056 (setq argument nil)
4057 (put-text-property (save-excursion
4058 (beginning-of-line)
4059 (point))
4060 b 'first-format-line 't)
4061 (if cperl-pod-here-fontify
4062 (while (and (eq (forward-line) 0)
4063 (not (looking-at "^[.;]$")))
4064 (cond
4065 ((looking-at "^#")) ; Skip comments
4066 ((and argument ; Skip argument multi-lines
4067 (looking-at "^[ \t]*{"))
4068 (forward-sexp 1)
4069 (setq argument nil))
4070 (argument ; Skip argument lines
4071 (setq argument nil))
4072 (t ; Format line
4073 (setq b1 (point))
4074 (setq argument (looking-at "^[^\n]*[@^]"))
4075 (end-of-line)
4076 ;; Highlight the format line
4077 (cperl-postpone-fontification b1 (point)
4078 'face font-lock-string-face)
4079 (cperl-commentify b1 (point) nil)
4080 (cperl-put-do-not-fontify b1 (point) t))))
4081 ;; We do not search to max, since we may be called from
4082 ;; some hook of fontification, and max is random
4083 (re-search-forward "^[.;]$" stop-point 'toend))
4084 (beginning-of-line)
4085 (if (looking-at "^\\.$") ; ";" is not supported yet
4086 (progn
4087 ;; Highlight the ending delimiter
4088 (cperl-postpone-fontification (point) (+ (point) 2)
4089 'face font-lock-string-face)
4090 (cperl-commentify (point) (+ (point) 2) nil)
4091 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4092 (message "End of format `%s' not found." name)
4093 (or (car err-l) (setcar err-l b)))
4094 (forward-line)
4095 (if (> (point) max)
4096 (setq tmpend tb))
4097 (put-text-property b (point) 'syntax-type 'format))
4098 ;; qq-like String or Regexp:
4099 ((or (match-beginning 10) (match-beginning 11))
4100 ;; 1+6+2=9 extra () before this:
4101 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4102 ;; "\\|"
4103 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4104 (setq b1 (if (match-beginning 10) 10 11)
4105 argument (buffer-substring
4106 (match-beginning b1) (match-end b1))
4107 b (point) ; end of qq etc
4108 i b
4109 c (char-after (match-beginning b1))
4110 bb (char-after (1- (match-beginning b1))) ; tmp holder
4111 ;; bb == "Not a stringy"
4112 bb (if (eq b1 10) ; user variables/whatever
4113 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4114 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4115 ((eq bb ?\:) ; $opt::s
4116 (eq (char-after
4117 (- (match-beginning b1) 2))
4118 ?\:))
4119 ((eq bb ?\>) ; $foo->s
4120 (eq (char-after
4121 (- (match-beginning b1) 2))
4122 ?\-))
4123 ((eq bb ?\&)
4124 (not (eq (char-after ; &&m/blah/
4125 (- (match-beginning b1) 2))
4126 ?\&)))
4127 (t t)))
4128 ;; <file> or <$file>
4129 (and (eq c ?\<)
4130 ;; Do not stringify <FH>, <$fh> :
4131 (save-match-data
4132 (looking-at
4133 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4134 tb (match-beginning 0))
4135 (goto-char (match-beginning b1))
4136 (cperl-backward-to-noncomment (point-min))
4137 (or bb
4138 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4139 (setq argument ""
4140 b1 nil
4141 bb ; Not a regexp?
4142 (not
4143 ;; What is below: regexp-p?
4144 (and
4145 (or (memq (preceding-char)
4146 (append (if (memq c '(?\? ?\<))
4147 ;; $a++ ? 1 : 2
4148 "~{(=|&*!,;:["
4149 "~{(=|&+-*!,;:[") nil))
4150 (and (eq (preceding-char) ?\})
4151 (cperl-after-block-p (point-min)))
4152 (and (eq (char-syntax (preceding-char)) ?w)
4153 (progn
4154 (forward-sexp -1)
4155 ;; After these keywords `/' starts a RE. One should add all the
4156 ;; functions/builtins which expect an argument, but ...
4157 (if (eq (preceding-char) ?-)
4158 ;; -d ?foo? is a RE
4159 (looking-at "[a-zA-Z]\\>")
4160 (and
4161 (not (memq (preceding-char)
4162 '(?$ ?@ ?& ?%)))
4163 (looking-at
4164 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4165 (and (eq (preceding-char) ?.)
4166 (eq (char-after (- (point) 2)) ?.))
4167 (bobp))
4168 ;; m|blah| ? foo : bar;
4169 (not
4170 (and (eq c ?\?)
4171 cperl-use-syntax-table-text-property
4172 (not (bobp))
4173 (progn
4174 (forward-char -1)
4175 (looking-at "\\s|"))))))
4176 b (1- b))
4177 ;; s y tr m
4178 ;; Check for $a -> y
4179 (setq b1 (preceding-char)
4180 go (point))
4181 (if (and (eq b1 ?>)
4182 (eq (char-after (- go 2)) ?-))
4183 ;; Not a regexp
4184 (setq bb t))))
4185 (or bb
4186 (progn
4187 (goto-char b)
4188 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4189 (goto-char (match-end 0))
4190 (skip-chars-forward " \t\n\f"))
4191 (cond ((and (eq (following-char) ?\})
4192 (eq b1 ?\{))
4193 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4194 (goto-char (1- go))
4195 (skip-chars-backward " \t\n\f")
4196 (if (memq (preceding-char) (append "$@%&*" nil))
4197 (setq bb t) ; @{y}
4198 (condition-case nil
4199 (forward-sexp -1)
4200 (error nil)))
4201 (if (or bb
4202 (looking-at ; $foo -> {s}
4203 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4204 (and ; $foo[12] -> {s}
4205 (memq (following-char) '(?\{ ?\[))
4206 (progn
4207 (forward-sexp 1)
4208 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4209 (setq bb t)
4210 (goto-char b)))
4211 ((and (eq (following-char) ?=)
4212 (eq (char-after (1+ (point))) ?\>))
4213 ;; Check for { foo => 1, s => 2 }
4214 ;; Apparently s=> is never a substitution...
4215 (setq bb t))
4216 ((and (eq (following-char) ?:)
4217 (eq b1 ?\{) ; Check for $ { s::bar }
4218 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4219 (progn
4220 (goto-char (1- go))
4221 (skip-chars-backward " \t\n\f")
4222 (memq (preceding-char)
4223 (append "$@%&*" nil))))
4224 (setq bb t))
4225 ((eobp)
4226 (setq bb t)))))
4227 (if bb
4228 (goto-char i)
4229 ;; Skip whitespace and comments...
4230 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4231 (goto-char (match-end 0))
4232 (skip-chars-forward " \t\n\f"))
4233 (if (> (point) b)
4234 (put-text-property b (point) 'syntax-type 'prestring))
4235 ;; qtag means two-arg matcher, may be reset to
4236 ;; 2 or 3 later if some special quoting is needed.
4237 ;; e1 means matching-char matcher.
4238 (setq b (point) ; before the first delimiter
4239 ;; has 2 args
4240 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4241 ;; We do not search to max, since we may be called from
4242 ;; some hook of fontification, and max is random
4243 i (cperl-forward-re stop-point end
4244 i2
4245 st-l err-l argument)
4246 ;; If `go', then it is considered as 1-arg, `b1' is nil
4247 ;; as in s/foo//x; the point is before final "slash"
4248 b1 (nth 1 i) ; start of the second part
4249 tag (nth 2 i) ; ender-char, true if second part
4250 ; is with matching chars []
4251 go (nth 4 i) ; There is a 1-char part after the end
4252 i (car i) ; intermediate point
4253 e1 (point) ; end
4254 ;; Before end of the second part if non-matching: ///
4255 tail (if (and i (not tag))
4256 (1- e1))
4257 e (if i i e1) ; end of the first part
4258 qtag nil ; need to preserve backslashitis
4259 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4260 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4261 ;; Commenting \\ is dangerous, what about ( ?
4262 (and i tail
4263 (eq (char-after i) ?\\)
4264 (setq qtag t))
4265 (and (if go (looking-at ".\\sw*x")
4266 (looking-at "\\sw*x")) ; qr//x
4267 (setq is-x-REx t))
4268 (and (if go (looking-at ".\\sw*o")
4269 (looking-at "\\sw*o")) ; //o
4270 (setq is-o-REx t))
4271 (if (null i)
4272 ;; Considered as 1arg form
4273 (progn
4274 (cperl-commentify b (point) t)
4275 (put-text-property b (point) 'syntax-type 'string)
4276 (if (or is-x-REx
4277 ;; ignore other text properties:
4278 (string-match "^qw$" argument))
4279 (put-text-property b (point) 'indentable t))
4280 (and go
4281 (setq e1 (cperl-1+ e1))
4282 (or (eobp)
4283 (forward-char 1))))
4284 (cperl-commentify b i t)
4285 (if (looking-at "\\sw*e") ; s///e
4286 (progn
4287 ;; Cache the syntax info...
4288 (setq cperl-syntax-state (cons state-point state))
4289 (and
4290 ;; silent:
4291 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4292 ;; Error
4293 (goto-char (1+ max)))
4294 (if (and tag (eq (preceding-char) ?\>))
4295 (progn
4296 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4297 (cperl-modify-syntax-type i cperl-st-bra)))
4298 (put-text-property b i 'syntax-type 'string)
4299 (put-text-property i (point) 'syntax-type 'multiline)
4300 (if is-x-REx
4301 (put-text-property b i 'indentable t)))
4302 (cperl-commentify b1 (point) t)
4303 (put-text-property b (point) 'syntax-type 'string)
4304 (if is-x-REx
4305 (put-text-property b i 'indentable t))
4306 (if qtag
4307 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4308 (setq tail nil)))
4309 ;; Now: tail: if the second part is non-matching without ///e
4310 (if (eq (char-syntax (following-char)) ?w)
4311 (progn
4312 (forward-word 1) ; skip modifiers s///s
4313 (if tail (cperl-commentify tail (point) t))
4314 (cperl-postpone-fontification
4315 e1 (point) 'face my-cperl-REx-modifiers-face)))
4316 ;; Check whether it is m// which means "previous match"
4317 ;; and highlight differently
4318 (setq is-REx
4319 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4320 (or (not (= (length argument) 0))
4321 (not (eq c ?\<)))))
4322 (if (and is-REx
4323 (eq e (+ 2 b))
4324 ;; split // *is* using zero-pattern
4325 (save-excursion
4326 (condition-case nil
4327 (progn
4328 (goto-char tb)
4329 (forward-sexp -1)
4330 (not (looking-at "split\\>")))
4331 (error t))))
4332 (cperl-postpone-fontification
4333 b e 'face font-lock-warning-face)
4334 (if (or i2 ; Has 2 args
4335 (and cperl-fontify-m-as-s
4336 (or
4337 (string-match "^\\(m\\|qr\\)$" argument)
4338 (and (eq 0 (length argument))
4339 (not (eq ?\< (char-after b)))))))
4340 (progn
4341 (cperl-postpone-fontification
4342 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4343 (cperl-postpone-fontification
4344 (1- e) e 'face my-cperl-delimiters-face)))
4345 (if (and is-REx cperl-regexp-scan)
4346 ;; Process RExen: embedded comments, charclasses and ]
4347 ;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4348 ;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4349 ;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4350 ;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4351 ;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4352 ;;;m^a[\^b]c^ + m.a[^b]\.c.;
4353 (save-excursion
4354 (goto-char (1+ b))
4355 ;; First
4356 (cperl-look-at-leading-count is-x-REx e)
4357 (setq hairy-RE
4358 (concat
4359 (if is-x-REx
4360 (if (eq (char-after b) ?\#)
4361 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4362 "\\((\\?#\\)\\|\\(#\\)")
4363 ;; keep the same count: add a fake group
4364 (if (eq (char-after b) ?\#)
4365 "\\((\\?\\\\#\\)\\(\\)"
4366 "\\((\\?#\\)\\(\\)"))
4367 "\\|"
4368 "\\(\\[\\)" ; 3=[
4369 "\\|"
4370 "\\(]\\)" ; 4=]
4371 "\\|"
4372 ;; XXXX Will not be able to use it in s)))
4373 (if (eq (char-after b) ?\) )
4374 "\\())))\\)" ; Will never match
4375 (if (eq (char-after b) ?? )
4376 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4377 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4378 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4379 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4380 "\\(" ;; XXXX 1-char variables, exc. |()\s
4381 "[$@]"
4382 "\\("
4383 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4384 "\\|"
4385 "{[^{}]*}" ; only one-level allowed
4386 "\\|"
4387 "[^{(|) \t\r\n\f]"
4388 "\\)"
4389 "\\(" ;;8,9:code part of array/hash elt
4390 "\\(" "->" "\\)?"
4391 "\\[[^][]*\\]"
4392 "\\|"
4393 "{[^{}]*}"
4394 "\\)*"
4395 ;; XXXX: what if u is delim?
4396 "\\|"
4397 "[)^|$.*?+]"
4398 "\\|"
4399 "{[0-9]+}"
4400 "\\|"
4401 "{[0-9]+,[0-9]*}"
4402 "\\|"
4403 "\\\\[luLUEQbBAzZG]"
4404 "\\|"
4405 "(" ; Group opener
4406 "\\(" ; 10 group opener follower
4407 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4408 "\\|"
4409 "\\?[:=!>?{]" ; "?" something
4410 "\\|"
4411 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4412 "\\|"
4413 "\\?([0-9]+)" ; (?(1)foo|bar)
4414 "\\|"
4415 "\\?<[=!]"
4416 ;;;"\\|"
4417 ;;; "\\?"
4418 "\\)?"
4419 "\\)"
4420 "\\|"
4421 "\\\\\\(.\\)" ; 12=\SYMBOL
4422 ))
4423 (while
4424 (and (< (point) (1- e))
4425 (re-search-forward hairy-RE (1- e) 'to-end))
4426 (goto-char (match-beginning 0))
4427 (setq REx-subgr-start (point)
4428 was-subgr (following-char))
4429 (cond
4430 ((match-beginning 6) ; 0-length builtins, groups
4431 (goto-char (match-end 0))
4432 (if (match-beginning 11)
4433 (goto-char (match-beginning 11)))
4434 (if (>= (point) e)
4435 (goto-char (1- e)))
4436 (cperl-postpone-fontification
4437 (match-beginning 0) (point)
4438 'face
4439 (cond
4440 ((eq was-subgr ?\) )
4441 (condition-case nil
4442 (save-excursion
4443 (forward-sexp -1)
4444 (if (> (point) b)
4445 (if (if (eq (char-after b) ?? )
4446 (looking-at "(\\\\\\?")
4447 (eq (char-after (1+ (point))) ?\?))
4448 my-cperl-REx-0length-face
4449 my-cperl-REx-ctl-face)
4450 font-lock-warning-face))
4451 (error font-lock-warning-face)))
4452 ((eq was-subgr ?\| )
4453 my-cperl-REx-ctl-face)
4454 ((eq was-subgr ?\$ )
4455 (if (> (point) (1+ REx-subgr-start))
4456 (progn
4457 (put-text-property
4458 (match-beginning 0) (point)
4459 'REx-interpolated
4460 (if is-o-REx 0
4461 (if (and (eq (match-beginning 0)
4462 (1+ b))
4463 (eq (point)
4464 (1- e))) 1 t)))
4465 font-lock-variable-name-face)
4466 my-cperl-REx-spec-char-face))
4467 ((memq was-subgr (append "^." nil) )
4468 my-cperl-REx-spec-char-face)
4469 ((eq was-subgr ?\( )
4470 (if (not (match-beginning 10))
4471 my-cperl-REx-ctl-face
4472 my-cperl-REx-0length-face))
4473 (t my-cperl-REx-0length-face)))
4474 (if (and (memq was-subgr (append "(|" nil))
4475 (not (string-match "(\\?[-imsx]+)"
4476 (match-string 0))))
4477 (cperl-look-at-leading-count is-x-REx e))
4478 (setq was-subgr nil)) ; We do stuff here
4479 ((match-beginning 12) ; \SYMBOL
4480 (forward-char 2)
4481 (if (>= (point) e)
4482 (goto-char (1- e))
4483 ;; How many chars to not highlight:
4484 ;; 0-len special-alnums in other branch =>
4485 ;; Generic: \non-alnum (1), \alnum (1+face)
4486 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4487 (setq REx-subgr-start (point)
4488 qtag (preceding-char))
4489 (cperl-postpone-fontification
4490 (- (point) 2) (- (point) 1) 'face
4491 (if (memq qtag
4492 (append "ghijkmoqvFHIJKMORTVY" nil))
4493 font-lock-warning-face
4494 my-cperl-REx-0length-face))
4495 (if (and (eq (char-after b) qtag)
4496 (memq qtag (append ".])^$|*?+" nil)))
4497 (progn
4498 (if (and cperl-use-syntax-table-text-property
4499 (eq qtag ?\) ))
4500 (put-text-property
4501 REx-subgr-start (1- (point))
4502 'syntax-table cperl-st-punct))
4503 (cperl-postpone-fontification
4504 (1- (point)) (point) 'face
4505 ; \] can't appear below
4506 (if (memq qtag (append ".]^$" nil))
4507 'my-cperl-REx-spec-char-face
4508 (if (memq qtag (append "*?+" nil))
4509 'my-cperl-REx-0length-face
4510 'my-cperl-REx-ctl-face))))) ; )|
4511 ;; Test for arguments:
4512 (cond
4513 ;; This is not pretty: the 5.8.7 logic:
4514 ;; \0numx -> octal (up to total 3 dig)
4515 ;; \DIGIT -> backref unless \0
4516 ;; \DIGITs -> backref if valid
4517 ;; otherwise up to 3 -> octal
4518 ;; Do not try to distinguish, we guess
4519 ((or (and (memq qtag (append "01234567" nil))
4520 (re-search-forward
4521 "\\=[01234567]?[01234567]?"
4522 (1- e) 'to-end))
4523 (and (memq qtag (append "89" nil))
4524 (re-search-forward
4525 "\\=[0123456789]*" (1- e) 'to-end))
4526 (and (eq qtag ?x)
4527 (re-search-forward
4528 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4529 (1- e) 'to-end))
4530 (and (memq qtag (append "pPN" nil))
4531 (re-search-forward "\\={[^{}]+}\\|."
4532 (1- e) 'to-end))
4533 (eq (char-syntax qtag) ?w))
4534 (cperl-postpone-fontification
4535 (1- REx-subgr-start) (point)
4536 'face my-cperl-REx-length1-face))))
4537 (setq was-subgr nil)) ; We do stuff here
4538 ((match-beginning 3) ; [charclass]
4539 ;; Highlight leader, trailer, POSIX classes
4540 (forward-char 1)
4541 (if (eq (char-after b) ?^ )
4542 (and (eq (following-char) ?\\ )
4543 (eq (char-after (cperl-1+ (point)))
4544 ?^ )
4545 (forward-char 2))
4546 (and (eq (following-char) ?^ )
4547 (forward-char 1)))
4548 (setq argument b ; continue? & end of last POSIX
4549 tag nil ; list of POSIX classes
4550 qtag (point)) ; after leading ^ if present
4551 (if (eq (char-after b) ?\] )
4552 (and (eq (following-char) ?\\ )
4553 (eq (char-after (cperl-1+ (point)))
4554 ?\] )
4555 (setq qtag (1+ qtag))
4556 (forward-char 2))
4557 (and (eq (following-char) ?\] )
4558 (forward-char 1)))
4559 (setq REx-subgr-end qtag) ;EndOf smart-highlighed
4560 ;; Apparently, I can't put \] into a charclass
4561 ;; in m]]: m][\\\]\]] produces [\\]]
4562 ;;; POSIX? [:word:] [:^word:] only inside []
4563 ;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4564 (while ; look for unescaped ]
4565 (and argument
4566 (re-search-forward
4567 (if (eq (char-after b) ?\] )
4568 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4569 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4570 (1- e) 'toend))
4571 ;; Is this ] an end of POSIX class?
4572 (if (save-excursion
4573 (and
4574 (search-backward "[" argument t)
4575 (< REx-subgr-start (point))
4576 (setq argument (point)) ; POSIX-start
4577 (or ; Should work with delim = \
4578 (not (eq (preceding-char) ?\\ ))
4579 ;; XXXX Double \\ is needed with 19.33
4580 (= (% (skip-chars-backward "\\\\") 2) 0))
4581 (looking-at
4582 (cond
4583 ((eq (char-after b) ?\] )
4584 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4585 ((eq (char-after b) ?\: )
4586 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4587 ((eq (char-after b) ?^ )
4588 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4589 ((eq (char-syntax (char-after b))
4590 ?w)
4591 (concat
4592 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4593 (char-to-string (char-after b))
4594 "\\|\\sw\\)+:\]"))
4595 (t "\\\\*\\[:\\^?\\sw*:]")))
4596 (goto-char REx-subgr-end)
4597 (cperl-highlight-charclass
4598 argument my-cperl-REx-spec-char-face
4599 my-cperl-REx-0length-face my-cperl-REx-length1-face)))
4600 (setq tag (cons (cons argument (point))
4601 tag)
4602 argument (point)
4603 REx-subgr-end argument) ; continue
4604 (setq argument nil)))
4605 (and argument
4606 (message "Couldn't find end of charclass in a REx, pos=%s"
4607 REx-subgr-start))
4608 (setq argument (1- (point)))
4609 (goto-char REx-subgr-end)
4610 (cperl-highlight-charclass
4611 argument my-cperl-REx-spec-char-face
4612 my-cperl-REx-0length-face my-cperl-REx-length1-face)
4613 (forward-char 1)
4614 ;; Highlight starter, trailer, POSIX
4615 (if (and cperl-use-syntax-table-text-property
4616 (> (- (point) 2) REx-subgr-start))
4617 (put-text-property
4618 (1+ REx-subgr-start) (1- (point))
4619 'syntax-table cperl-st-punct))
4620 (cperl-postpone-fontification
4621 REx-subgr-start qtag
4622 'face my-cperl-REx-spec-char-face)
4623 (cperl-postpone-fontification
4624 (1- (point)) (point) 'face
4625 my-cperl-REx-spec-char-face)
4626 (if (eq (char-after b) ?\] )
4627 (cperl-postpone-fontification
4628 (- (point) 2) (1- (point))
4629 'face my-cperl-REx-0length-face))
4630 (while tag
4631 (cperl-postpone-fontification
4632 (car (car tag)) (cdr (car tag))
4633 'face font-lock-variable-name-face) ;my-cperl-REx-length1-face
4634 (setq tag (cdr tag)))
4635 (setq was-subgr nil)) ; did facing already
4636 ;; Now rare stuff:
4637 ((and (match-beginning 2) ; #-comment
4638 (/= (match-beginning 2) (match-end 2)))
4639 (beginning-of-line 2)
4640 (if (> (point) e)
4641 (goto-char (1- e))))
4642 ((match-beginning 4) ; character "]"
4643 (setq was-subgr nil) ; We do stuff here
4644 (goto-char (match-end 0))
4645 (if cperl-use-syntax-table-text-property
4646 (put-text-property
4647 (1- (point)) (point)
4648 'syntax-table cperl-st-punct))
4649 (cperl-postpone-fontification
4650 (1- (point)) (point)
4651 'face font-lock-warning-face))
4652 ((match-beginning 5) ; before (?{}) (??{})
4653 (setq tag (match-end 0))
4654 (if (or (setq qtag
4655 (cperl-forward-group-in-re st-l))
4656 (and (>= (point) e)
4657 (setq qtag "no matching `)' found"))
4658 (and (not (eq (char-after (- (point) 2))
4659 ?\} ))
4660 (setq qtag "Can't find })")))
4661 (progn
4662 (goto-char (1- e))
4663 (message "%s" qtag))
4664 (cperl-postpone-fontification
4665 (1- tag) (1- (point))
4666 'face font-lock-variable-name-face)
4667 (cperl-postpone-fontification
4668 REx-subgr-start (1- tag)
4669 'face my-cperl-REx-spec-char-face)
4670 (cperl-postpone-fontification
4671 (1- (point)) (point)
4672 'face my-cperl-REx-spec-char-face)
4673 (if cperl-use-syntax-table-text-property
4674 (progn
4675 (put-text-property
4676 (- (point) 2) (1- (point))
4677 'syntax-table cperl-st-cfence)
4678 (put-text-property
4679 (+ REx-subgr-start 2)
4680 (+ REx-subgr-start 3)
4681 'syntax-table cperl-st-cfence))))
4682 (setq was-subgr nil))
4683 (t ; (?#)-comment
4684 ;; Inside "(" and "\" arn't special in any way
4685 ;; Works also if the outside delimiters are ().
4686 (or;;(if (eq (char-after b) ?\) )
4687 ;;(re-search-forward
4688 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4689 ;; (1- e) 'toend)
4690 (search-forward ")" (1- e) 'toend)
4691 ;;)
4692 (message
4693 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4694 REx-subgr-start))))
4695 (if (>= (point) e)
4696 (goto-char (1- e)))
4697 (cond
4698 (was-subgr
4699 (setq REx-subgr-end (point))
4700 (cperl-commentify
4701 REx-subgr-start REx-subgr-end nil)
4702 (cperl-postpone-fontification
4703 REx-subgr-start REx-subgr-end
4704 'face font-lock-comment-face))))))
4705 (if (and is-REx is-x-REx)
4706 (put-text-property (1+ b) (1- e)
4707 'syntax-subtype 'x-REx)))
4708 (if (and i2 e1 (or (not b1) (> e1 b1)))
4709 (progn ; No errors finding the second part...
4710 (cperl-postpone-fontification
4711 (1- e1) e1 'face my-cperl-delimiters-face)
4712 (if (and (not (eobp))
4713 (assoc (char-after b) cperl-starters))
4714 (progn
4715 (cperl-postpone-fontification
4716 b1 (1+ b1) 'face my-cperl-delimiters-face)
4717 (put-text-property b1 (1+ b1)
4718 'REx-part2 t)))))
4719 (if (> (point) max)
4720 (setq tmpend tb))))
4721 ((match-beginning 17) ; sub with prototype or attribute
4722 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4723 ;;"\\<sub\\>\\(" ;12
4724 ;; cperl-white-and-comment-rex ;13
4725 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4726 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4727 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4728 (setq b1 (match-beginning 14) e1 (match-end 14))
4729 (if (memq (char-after (1- b))
4730 '(?\$ ?\@ ?\% ?\& ?\*))
4731 nil
4732 (goto-char b)
4733 (if (eq (char-after (match-beginning 17)) ?\( )
4734 (progn
4735 (cperl-commentify ; Prototypes; mark as string
4736 (match-beginning 17) (match-end 17) t)
4737 (goto-char (match-end 0))
4738 ;; Now look for attributes after prototype:
4739 (forward-comment (buffer-size))
4740 (and (looking-at ":[^:]")
4741 (cperl-find-sub-attrs st-l b1 e1 b)))
4742 ;; treat attributes without prototype
4743 (goto-char (match-beginning 17))
4744 (cperl-find-sub-attrs st-l b1 e1 b))))
4745 ;; 1+6+2+1+1+6+1=18 extra () before this:
4746 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4747 ((match-beginning 19) ; old $abc'efg syntax
4748 (setq bb (match-end 0))
4749 ;;;(if (nth 3 state) nil ; in string
4750 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4751 (goto-char bb))
4752 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4753 ;; "__\\(END\\|DATA\\)__"
4754 ((match-beginning 20) ; __END__, __DATA__
4755 (setq bb (match-end 0))
4756 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4757 (cperl-commentify b bb nil)
4758 (setq end t))
4759 ;; "\\\\\\(['`\"($]\\)"
4760 ((match-beginning 21)
4761 ;; Trailing backslash; make non-quoting outside string/comment
4762 (setq bb (match-end 0))
4763 (goto-char b)
4764 (skip-chars-backward "\\\\")
4765 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4766 (cperl-modify-syntax-type b cperl-st-punct)
4767 (goto-char bb))
4768 (t (error "Error in regexp of the sniffer")))
4769 (if (> (point) stop-point)
4770 (progn
4771 (if end
4772 (message "Garbage after __END__/__DATA__ ignored")
4773 (message "Unbalanced syntax found while scanning")
4774 (or (car err-l) (setcar err-l b)))
4775 (goto-char stop-point))))
4776 (setq cperl-syntax-state (cons state-point state)
4777 ;; Do not mark syntax as done past tmpend???
4778 cperl-syntax-done-to (or tmpend (max (point) max)))
4779 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4780 )
4781 (if (car err-l) (goto-char (car err-l))
4782 (or non-inter
4783 (message "Scanning for \"hard\" Perl constructions... done"))))
4784 (and (buffer-modified-p)
4785 (not modified)
4786 (set-buffer-modified-p nil))
4787 ;; I do not understand what this is doing here. It breaks font-locking
4788 ;; because it resets the syntax-table from font-lock-syntax-table to
4789 ;; cperl-mode-syntax-table.
4790 ;; (set-syntax-table cperl-mode-syntax-table)
4791 )
4792 (list (car err-l) overshoot)))
4793
4794 (defun cperl-find-pods-heres-region (min max)
4795 (interactive "r")
4796 (cperl-find-pods-heres min max))
4797
4798 (defun cperl-backward-to-noncomment (lim)
4799 ;; Stops at lim or after non-whitespace that is not in comment
4800 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4801 (let (stop p pr)
4802 (while (and (not stop) (> (point) (or lim (point-min))))
4803 (skip-chars-backward " \t\n\f" lim)
4804 (setq p (point))
4805 (beginning-of-line)
4806 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4807 '(pod here-doc here-doc-delim))
4808 (progn
4809 (cperl-unwind-to-safe nil)
4810 (setq pr (get-text-property (point) 'syntax-type))))
4811 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4812 (not (memq pr '(string prestring))))
4813 (progn (cperl-to-comment-or-eol) (bolp))
4814 (progn
4815 (skip-chars-backward " \t")
4816 (if (< p (point)) (goto-char p))
4817 (setq stop t))))))
4818
4819 ;; Used only in `cperl-calculate-indent'...
4820 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4821 ;; Positions is before ?\{. Checks whether it starts a block.
4822 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4823 (cperl-backward-to-noncomment (point-min))
4824 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4825 ; Label may be mixed up with `$blah :'
4826 (save-excursion (cperl-after-label))
4827 (get-text-property (cperl-1- (point)) 'attrib-group)
4828 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4829 (progn
4830 (backward-sexp)
4831 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4832 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4833 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4834 ;; sub bless::foo {}
4835 (progn
4836 (cperl-backward-to-noncomment (point-min))
4837 (and (eq (preceding-char) ?b)
4838 (progn
4839 (forward-sexp -1)
4840 (looking-at "sub[ \t\n\f#]")))))))))
4841
4842 ;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4843 ;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4844 ;;; may be a part of an in-statement construct, such as
4845 ;;; ${something()}, print {FH} $data.
4846 ;;; Moreover, one takes positive approach (looks for else,grep etc)
4847 ;;; another negative (looks for bless,tr etc)
4848 (defun cperl-after-block-p (lim &optional pre-block)
4849 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4850 Would not look before LIM. Assumes that LIM is a good place to begin a
4851 statement. The kind of block we treat here is one after which a new
4852 statement would start; thus the block in ${func()} does not count."
4853 (save-excursion
4854 (condition-case nil
4855 (progn
4856 (or pre-block (forward-sexp -1))
4857 (cperl-backward-to-noncomment lim)
4858 (or (eq (point) lim)
4859 ;; if () {} // sub f () {} // sub f :a(') {}
4860 (eq (preceding-char) ?\) )
4861 ;; label: {}
4862 (save-excursion (cperl-after-label))
4863 ;; sub :attr {}
4864 (get-text-property (cperl-1- (point)) 'attrib-group)
4865 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4866 (save-excursion
4867 (forward-sexp -1)
4868 ;; else {} but not else::func {}
4869 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4870 (not (looking-at "\\(\\sw\\|_\\)+::")))
4871 ;; sub f {}
4872 (progn
4873 (cperl-backward-to-noncomment lim)
4874 (and (eq (preceding-char) ?b)
4875 (progn
4876 (forward-sexp -1)
4877 (looking-at "sub[ \t\n\f#]"))))))
4878 ;; What preceeds is not word... XXXX Last statement in sub???
4879 (cperl-after-expr-p lim))))
4880 (error nil))))
4881
4882 (defun cperl-after-expr-p (&optional lim chars test)
4883 "Return true if the position is good for start of expression.
4884 TEST is the expression to evaluate at the found position. If absent,
4885 CHARS is a string that contains good characters to have before us (however,
4886 `}' is treated \"smartly\" if it is not in the list)."
4887 (let ((lim (or lim (point-min)))
4888 stop p pr)
4889 (cperl-update-syntaxification (point) (point))
4890 (save-excursion
4891 (while (and (not stop) (> (point) lim))
4892 (skip-chars-backward " \t\n\f" lim)
4893 (setq p (point))
4894 (beginning-of-line)
4895 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4896 ;; '(pod here-doc here-doc-delim))
4897 (if (get-text-property (point) 'here-doc-group)
4898 (progn
4899 (goto-char
4900 (cperl-beginning-of-property (point) 'here-doc-group))
4901 (beginning-of-line 0)))
4902 (if (get-text-property (point) 'in-pod)
4903 (progn
4904 (goto-char
4905 (cperl-beginning-of-property (point) 'in-pod))
4906 (beginning-of-line 0)))
4907 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4908 ;; Else: last iteration, or a label
4909 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4910 (skip-chars-backward " \t")
4911 (if (< p (point)) (goto-char p))
4912 (setq p (point))
4913 (if (and (eq (preceding-char) ?:)
4914 (progn
4915 (forward-char -1)
4916 (skip-chars-backward " \t\n\f" lim)
4917 (memq (char-syntax (preceding-char)) '(?w ?_))))
4918 (forward-sexp -1) ; Possibly label. Skip it
4919 (goto-char p)
4920 (setq stop t))))
4921 (or (bobp) ; ???? Needed
4922 (eq (point) lim)
4923 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4924 (progn
4925 (if test (eval test)
4926 (or (memq (preceding-char) (append (or chars "{;") nil))
4927 (and (eq (preceding-char) ?\})
4928 (cperl-after-block-p lim))
4929 (and (eq (following-char) ?.) ; in format: see comment above
4930 (eq (get-text-property (point) 'syntax-type)
4931 'format)))))))))
4932
4933 (defun cperl-backward-to-start-of-expr (&optional lim)
4934 (condition-case nil
4935 (progn
4936 (while (and (or (not lim)
4937 (> (point) lim))
4938 (not (cperl-after-expr-p lim)))
4939 (forward-sexp -1)
4940 ;; May be after $, @, $# etc of a variable
4941 (skip-chars-backward "$@%#")))
4942 (error nil)))
4943
4944 (defun cperl-at-end-of-expr (&optional lim)
4945 ;; Since the SEXP approach below is very fragile, do some overengineering
4946 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4947 (condition-case nil
4948 (save-excursion
4949 ;; If nothing interesting after, does as (forward-sexp -1);
4950 ;; otherwise fails, or ends at a start of following sexp.
4951 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4952 ;; may be stuck after @ or $; just put some stupid workaround now:
4953 (let ((p (point)))
4954 (forward-sexp 1)
4955 (forward-sexp -1)
4956 (while (memq (preceding-char) (append "%&@$*" nil))
4957 (forward-char -1))
4958 (or (< (point) p)
4959 (cperl-after-expr-p lim))))
4960 (error t))))
4961
4962 (defun cperl-forward-to-end-of-expr (&optional lim)
4963 (let ((p (point))))
4964 (condition-case nil
4965 (progn
4966 (while (and (< (point) (or lim (point-max)))
4967 (not (cperl-at-end-of-expr)))
4968 (forward-sexp 1)))
4969 (error nil)))
4970
4971 (defun cperl-backward-to-start-of-continued-exp (lim)
4972 (if (memq (preceding-char) (append ")]}\"'`" nil))
4973 (forward-sexp -1))
4974 (beginning-of-line)
4975 (if (<= (point) lim)
4976 (goto-char (1+ lim)))
4977 (skip-chars-forward " \t"))
4978
4979 (defun cperl-after-block-and-statement-beg (lim)
4980 ;; We assume that we are after ?\}
4981 (and
4982 (cperl-after-block-p lim)
4983 (save-excursion
4984 (forward-sexp -1)
4985 (cperl-backward-to-noncomment (point-min))
4986 (or (bobp)
4987 (eq (point) lim)
4988 (not (= (char-syntax (preceding-char)) ?w))
4989 (progn
4990 (forward-sexp -1)
4991 (not
4992 (looking-at
4993 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4994
4995 \f
4996 (defun cperl-indent-exp ()
4997 "Simple variant of indentation of continued-sexp.
4998
4999 Will not indent comment if it starts at `comment-indent' or looks like
5000 continuation of the comment on the previous line.
5001
5002 If `cperl-indent-region-fix-constructs', will improve spacing on
5003 conditional/loop constructs."
5004 (interactive)
5005 (save-excursion
5006 (let ((tmp-end (progn (end-of-line) (point))) top done)
5007 (save-excursion
5008 (beginning-of-line)
5009 (while (null done)
5010 (setq top (point))
5011 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5012 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
5013 (setq top (point))) ; Get the outermost parenths in line
5014 (goto-char top)
5015 (while (< (point) tmp-end)
5016 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5017 (or (eolp) (forward-sexp 1)))
5018 (if (> (point) tmp-end) ; Yes, there an unfinished block
5019 nil
5020 (if (eq ?\) (preceding-char))
5021 (progn ;; Plan B: find by REGEXP block followup this line
5022 (setq top (point))
5023 (condition-case nil
5024 (progn
5025 (forward-sexp -2)
5026 (if (eq (following-char) ?$ ) ; for my $var (list)
5027 (progn
5028 (forward-sexp -1)
5029 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5030 (forward-sexp -1))))
5031 (if (looking-at
5032 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5033 "\\|for\\(each\\)?\\>\\(\\("
5034 cperl-maybe-white-and-comment-rex
5035 "\\(my\\|local\\|our\\)\\)?"
5036 cperl-maybe-white-and-comment-rex
5037 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5038 (progn
5039 (goto-char top)
5040 (forward-sexp 1)
5041 (setq top (point)))))
5042 (error (setq done t)))
5043 (goto-char top))
5044 (if (looking-at ; Try Plan C: continuation block
5045 (concat cperl-maybe-white-and-comment-rex
5046 "\\<\\(else\\|elsif\|continue\\)\\>"))
5047 (progn
5048 (goto-char (match-end 0))
5049 (save-excursion
5050 (end-of-line)
5051 (setq tmp-end (point))))
5052 (setq done t))))
5053 (save-excursion
5054 (end-of-line)
5055 (setq tmp-end (point))))
5056 (goto-char tmp-end)
5057 (setq tmp-end (point-marker)))
5058 (if cperl-indent-region-fix-constructs
5059 (cperl-fix-line-spacing tmp-end))
5060 (cperl-indent-region (point) tmp-end))))
5061
5062 (defun cperl-fix-line-spacing (&optional end parse-data)
5063 "Improve whitespace in a conditional/loop construct.
5064 Returns some position at the last line."
5065 (interactive)
5066 (or end
5067 (setq end (point-max)))
5068 (let ((ee (save-excursion (end-of-line) (point)))
5069 (cperl-indent-region-fix-constructs
5070 (or cperl-indent-region-fix-constructs 1))
5071 p pp ml have-brace ret)
5072 (save-excursion
5073 (beginning-of-line)
5074 (setq ret (point))
5075 ;; }? continue
5076 ;; blah; }
5077 (if (not
5078 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5079 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5080 nil ; Do not need to do anything
5081 ;; Looking at:
5082 ;; }
5083 ;; else
5084 (if cperl-merge-trailing-else
5085 (if (looking-at
5086 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5087 (progn
5088 (search-forward "}")
5089 (setq p (point))
5090 (skip-chars-forward " \t\n")
5091 (delete-region p (point))
5092 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5093 (beginning-of-line)))
5094 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5095 (save-excursion
5096 (search-forward "}")
5097 (delete-horizontal-space)
5098 (insert "\n")
5099 (setq ret (point))
5100 (if (cperl-indent-line parse-data)
5101 (progn
5102 (cperl-fix-line-spacing end parse-data)
5103 (setq ret (point)))))))
5104 ;; Looking at:
5105 ;; } else
5106 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5107 (progn
5108 (search-forward "}")
5109 (delete-horizontal-space)
5110 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5111 (beginning-of-line)))
5112 ;; Looking at:
5113 ;; else {
5114 (if (looking-at
5115 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5116 (progn
5117 (forward-word 1)
5118 (delete-horizontal-space)
5119 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5120 (beginning-of-line)))
5121 ;; Looking at:
5122 ;; foreach my $var
5123 (if (looking-at
5124 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5125 (progn
5126 (forward-word 2)
5127 (delete-horizontal-space)
5128 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5129 (beginning-of-line)))
5130 ;; Looking at:
5131 ;; foreach my $var (
5132 (if (looking-at
5133 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5134 (progn
5135 (forward-sexp 3)
5136 (delete-horizontal-space)
5137 (insert
5138 (make-string cperl-indent-region-fix-constructs ?\s))
5139 (beginning-of-line)))
5140 ;; Looking at (with or without "}" at start, ending after "({"):
5141 ;; } foreach my $var () OR {
5142 (if (looking-at
5143 "[ \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]*{")
5144 (progn
5145 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5146 (re-search-forward "[({]")
5147 (forward-char -1)
5148 (setq p (point))
5149 (if (eq (following-char) ?\( )
5150 (progn
5151 (forward-sexp 1)
5152 (setq pp (point))) ; past parenth-group
5153 ;; after `else' or nothing
5154 (if ml ; after `else'
5155 (skip-chars-backward " \t\n")
5156 (beginning-of-line))
5157 (setq pp nil))
5158 ;; Now after the sexp before the brace
5159 ;; Multiline expr should be special
5160 (setq ml (and pp (save-excursion (goto-char p)
5161 (search-forward "\n" pp t))))
5162 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5163 (looking-at "[ \t\n]*{"))
5164 (progn
5165 (cond
5166 ((bolp) ; Were before `{', no if/else/etc
5167 nil)
5168 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5169 (delete-horizontal-space)
5170 (if (if ml
5171 cperl-extra-newline-before-brace-multiline
5172 cperl-extra-newline-before-brace)
5173 (progn
5174 (delete-horizontal-space)
5175 (insert "\n")
5176 (setq ret (point))
5177 (if (cperl-indent-line parse-data)
5178 (progn
5179 (cperl-fix-line-spacing end parse-data)
5180 (setq ret (point)))))
5181 (insert
5182 (make-string cperl-indent-region-fix-constructs ?\s))))
5183 ((and (looking-at "[ \t]*\n")
5184 (not (if ml
5185 cperl-extra-newline-before-brace-multiline
5186 cperl-extra-newline-before-brace)))
5187 (setq pp (point))
5188 (skip-chars-forward " \t\n")
5189 (delete-region pp (point))
5190 (insert
5191 (make-string cperl-indent-region-fix-constructs ?\ )))
5192 ((and (looking-at "[\t ]*{")
5193 (if ml cperl-extra-newline-before-brace-multiline
5194 cperl-extra-newline-before-brace))
5195 (delete-horizontal-space)
5196 (insert "\n")
5197 (setq ret (point))
5198 (if (cperl-indent-line parse-data)
5199 (progn
5200 (cperl-fix-line-spacing end parse-data)
5201 (setq ret (point))))))
5202 ;; Now we are before `{'
5203 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5204 (progn
5205 (skip-chars-forward " \t\n")
5206 (setq pp (point))
5207 (forward-sexp 1)
5208 (setq p (point))
5209 (goto-char pp)
5210 (setq ml (search-forward "\n" p t))
5211 (if (or cperl-break-one-line-blocks-when-indent ml)
5212 ;; not good: multi-line BLOCK
5213 (progn
5214 (goto-char (1+ pp))
5215 (delete-horizontal-space)
5216 (insert "\n")
5217 (setq ret (point))
5218 (if (cperl-indent-line parse-data)
5219 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5220 (beginning-of-line)
5221 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5222 ;; Now check whether there is a hanging `}'
5223 ;; Looking at:
5224 ;; } blah
5225 (if (and
5226 cperl-fix-hanging-brace-when-indent
5227 have-brace
5228 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5229 (condition-case nil
5230 (progn
5231 (up-list 1)
5232 (if (and (<= (point) pp)
5233 (eq (preceding-char) ?\} )
5234 (cperl-after-block-and-statement-beg (point-min)))
5235 t
5236 (goto-char p)
5237 nil))
5238 (error nil)))
5239 (progn
5240 (forward-char -1)
5241 (skip-chars-backward " \t")
5242 (if (bolp)
5243 ;; `}' was the first thing on the line, insert NL *after* it.
5244 (progn
5245 (cperl-indent-line parse-data)
5246 (search-forward "}")
5247 (delete-horizontal-space)
5248 (insert "\n"))
5249 (delete-horizontal-space)
5250 (or (eq (preceding-char) ?\;)
5251 (bolp)
5252 (and (eq (preceding-char) ?\} )
5253 (cperl-after-block-p (point-min)))
5254 (insert ";"))
5255 (insert "\n")
5256 (setq ret (point)))
5257 (if (cperl-indent-line parse-data)
5258 (setq ret (cperl-fix-line-spacing end parse-data)))
5259 (beginning-of-line)))))
5260 ret))
5261
5262 (defvar cperl-update-start) ; Do not need to make them local
5263 (defvar cperl-update-end)
5264 (defun cperl-delay-update-hook (beg end old-len)
5265 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5266 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5267
5268 (defun cperl-indent-region (start end)
5269 "Simple variant of indentation of region in CPerl mode.
5270 Should be slow. Will not indent comment if it starts at `comment-indent'
5271 or looks like continuation of the comment on the previous line.
5272 Indents all the lines whose first character is between START and END
5273 inclusive.
5274
5275 If `cperl-indent-region-fix-constructs', will improve spacing on
5276 conditional/loop constructs."
5277 (interactive "r")
5278 (cperl-update-syntaxification end end)
5279 (save-excursion
5280 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5281 (let ((indent-info (if cperl-emacs-can-parse
5282 (list nil nil nil) ; Cannot use '(), since will modify
5283 nil))
5284 (pm 0)
5285 after-change-functions ; Speed it up!
5286 st comm old-comm-indent new-comm-indent p pp i empty)
5287 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5288 (goto-char start)
5289 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5290 (current-column))
5291 new-comm-indent old-comm-indent)
5292 (goto-char start)
5293 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5294 (or (bolp) (beginning-of-line 2))
5295 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5296 (setq st (point))
5297 (if (or
5298 (setq empty (looking-at "[ \t]*\n"))
5299 (and (setq comm (looking-at "[ \t]*#"))
5300 (or (eq (current-indentation) (or old-comm-indent
5301 comment-column))
5302 (setq old-comm-indent nil))))
5303 (if (and old-comm-indent
5304 (not empty)
5305 (= (current-indentation) old-comm-indent)
5306 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5307 (not (eq (get-text-property (point) 'syntax-table)
5308 cperl-st-cfence)))
5309 (let ((comment-column new-comm-indent))
5310 (indent-for-comment)))
5311 (progn
5312 (setq i (cperl-indent-line indent-info))
5313 (or comm
5314 (not i)
5315 (progn
5316 (if cperl-indent-region-fix-constructs
5317 (goto-char (cperl-fix-line-spacing end indent-info)))
5318 (if (setq old-comm-indent
5319 (and (cperl-to-comment-or-eol)
5320 (not (memq (get-text-property (point)
5321 'syntax-type)
5322 '(pod here-doc)))
5323 (not (eq (get-text-property (point)
5324 'syntax-table)
5325 cperl-st-cfence))
5326 (current-column)))
5327 (progn (indent-for-comment)
5328 (skip-chars-backward " \t")
5329 (skip-chars-backward "#")
5330 (setq new-comm-indent (current-column))))))))
5331 (beginning-of-line 2)))
5332 ;; Now run the update hooks
5333 (and after-change-functions
5334 cperl-update-end
5335 (save-excursion
5336 (goto-char cperl-update-end)
5337 (insert " ")
5338 (delete-char -1)
5339 (goto-char cperl-update-start)
5340 (insert " ")
5341 (delete-char -1))))))
5342
5343 ;; Stolen from lisp-mode with a lot of improvements
5344
5345 (defun cperl-fill-paragraph (&optional justify iteration)
5346 "Like `fill-paragraph', but handle CPerl comments.
5347 If any of the current line is a comment, fill the comment or the
5348 block of it that point is in, preserving the comment's initial
5349 indentation and initial hashes. Behaves usually outside of comment."
5350 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5351 (let (;; Non-nil if the current line contains a comment.
5352 has-comment
5353 fill-paragraph-function ; do not recurse
5354 ;; If has-comment, the appropriate fill-prefix for the comment.
5355 comment-fill-prefix
5356 ;; Line that contains code and comment (or nil)
5357 start
5358 c spaces len dc (comment-column comment-column))
5359 ;; Figure out what kind of comment we are looking at.
5360 (save-excursion
5361 (beginning-of-line)
5362 (cond
5363
5364 ;; A line with nothing but a comment on it?
5365 ((looking-at "[ \t]*#[# \t]*")
5366 (setq has-comment t
5367 comment-fill-prefix (buffer-substring (match-beginning 0)
5368 (match-end 0))))
5369
5370 ;; A line with some code, followed by a comment? Remember that the
5371 ;; semi which starts the comment shouldn't be part of a string or
5372 ;; character.
5373 ((cperl-to-comment-or-eol)
5374 (setq has-comment t)
5375 (looking-at "#+[ \t]*")
5376 (setq start (point) c (current-column)
5377 comment-fill-prefix
5378 (concat (make-string (current-column) ?\s)
5379 (buffer-substring (match-beginning 0) (match-end 0)))
5380 spaces (progn (skip-chars-backward " \t")
5381 (buffer-substring (point) start))
5382 dc (- c (current-column)) len (- start (point))
5383 start (point-marker))
5384 (delete-char len)
5385 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5386 (if (not has-comment)
5387 (fill-paragraph justify) ; Do the usual thing outside of comment
5388 ;; Narrow to include only the comment, and then fill the region.
5389 (save-restriction
5390 (narrow-to-region
5391 ;; Find the first line we should include in the region to fill.
5392 (if start (progn (beginning-of-line) (point))
5393 (save-excursion
5394 (while (and (zerop (forward-line -1))
5395 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5396 ;; We may have gone to far. Go forward again.
5397 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5398 (forward-line 1))
5399 (point)))
5400 ;; Find the beginning of the first line past the region to fill.
5401 (save-excursion
5402 (while (progn (forward-line 1)
5403 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5404 (point)))
5405 ;; Remove existing hashes
5406 (goto-char (point-min))
5407 (save-excursion
5408 (while (progn (forward-line 1) (< (point) (point-max)))
5409 (skip-chars-forward " \t")
5410 (if (looking-at "#+")
5411 (progn
5412 (if (and (eq (point) (match-beginning 0))
5413 (not (eq (point) (match-end 0)))) nil
5414 (error
5415 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5416 (delete-char (- (match-end 0) (match-beginning 0)))))))
5417
5418 ;; Lines with only hashes on them can be paragraph boundaries.
5419 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5420 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5421 (fill-prefix comment-fill-prefix))
5422 (fill-paragraph justify)))
5423 (if (and start)
5424 (progn
5425 (goto-char start)
5426 (if (> dc 0)
5427 (progn (delete-char dc) (insert spaces)))
5428 (if (or (= (current-column) c) iteration) nil
5429 (setq comment-column c)
5430 (indent-for-comment)
5431 ;; Repeat once more, flagging as iteration
5432 (cperl-fill-paragraph justify t))))))
5433 t)
5434
5435 (defun cperl-do-auto-fill ()
5436 ;; Break out if the line is short enough
5437 (if (> (save-excursion
5438 (end-of-line)
5439 (current-column))
5440 fill-column)
5441 (let ((c (save-excursion (beginning-of-line)
5442 (cperl-to-comment-or-eol) (point)))
5443 (s (memq (following-char) '(?\s ?\t))) marker)
5444 (if (>= c (point))
5445 ;; Don't break line inside code: only inside comment.
5446 nil
5447 (setq marker (point-marker))
5448 (fill-paragraph nil)
5449 (goto-char marker)
5450 ;; Is not enough, sometimes marker is a start of line
5451 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5452 (goto-char (match-end 0))))
5453 ;; Following space could have gone:
5454 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5455 (insert " ")
5456 (backward-char 1))
5457 ;; Previous space could have gone:
5458 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5459
5460 (defun cperl-imenu-addback (lst &optional isback name)
5461 ;; We suppose that the lst is a DAG, unless the first element only
5462 ;; loops back, and ISBACK is set. Thus this function cannot be
5463 ;; applied twice without ISBACK set.
5464 (cond ((not cperl-imenu-addback) lst)
5465 (t
5466 (or name
5467 (setq name "+++BACK+++"))
5468 (mapc (lambda (elt)
5469 (if (and (listp elt) (listp (cdr elt)))
5470 (progn
5471 ;; In the other order it goes up
5472 ;; one level only ;-(
5473 (setcdr elt (cons (cons name lst)
5474 (cdr elt)))
5475 (cperl-imenu-addback (cdr elt) t name))))
5476 (if isback (cdr lst) lst))
5477 lst)))
5478
5479 (defun cperl-imenu--create-perl-index (&optional regexp)
5480 (require 'imenu) ; May be called from TAGS creator
5481 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5482 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5483 (index-meth-alist '()) meth
5484 packages ends-ranges p marker is-proto
5485 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5486 (goto-char (point-min))
5487 (cperl-update-syntaxification (point-max) (point-max))
5488 ;; Search for the function
5489 (progn ;;save-match-data
5490 (while (re-search-forward
5491 (or regexp cperl-imenu--function-name-regexp-perl)
5492 nil t)
5493 ;; 2=package-group, 5=package-name 8=sub-name
5494 (cond
5495 ((and ; Skip some noise if building tags
5496 (match-beginning 5) ; package name
5497 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5498 (not (save-match-data
5499 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5500 nil)
5501 ((and
5502 (or (match-beginning 2)
5503 (match-beginning 8)) ; package or sub
5504 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5505 (null (get-text-property (match-beginning 1) 'syntax-table))
5506 (null (get-text-property (match-beginning 1) 'syntax-type))
5507 (null (get-text-property (match-beginning 1) 'in-pod)))
5508 (setq is-pack (match-beginning 2))
5509 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5510 ;; (goto-char (match-end 0))) ; Messes what follows
5511 (setq meth nil
5512 p (point))
5513 (while (and ends-ranges (>= p (car ends-ranges)))
5514 ;; delete obsolete entries
5515 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5516 (setq package (or (car packages) "")
5517 end-range (or (car ends-ranges) 0))
5518 (if is-pack ; doing "package"
5519 (progn
5520 (if (match-beginning 5) ; named package
5521 (setq name (buffer-substring (match-beginning 5)
5522 (match-end 5))
5523 name (progn
5524 (set-text-properties 0 (length name) nil name)
5525 name)
5526 package (concat name "::")
5527 name (concat "package " name))
5528 ;; Support nameless packages
5529 (setq name "package;" package ""))
5530 (setq end-range
5531 (save-excursion
5532 (parse-partial-sexp (point) (point-max) -1) (point))
5533 ends-ranges (cons end-range ends-ranges)
5534 packages (cons package packages)))
5535 (setq is-proto
5536 (or (eq (following-char) ?\;)
5537 (eq 0 (get-text-property (point) 'attrib-group)))))
5538 ;; Skip this function name if it is a prototype declaration.
5539 (if (and is-proto (not is-pack)) nil
5540 (or is-pack
5541 (setq name
5542 (buffer-substring (match-beginning 8) (match-end 8)))
5543 (set-text-properties 0 (length name) nil name))
5544 (setq marker (make-marker))
5545 (set-marker marker (match-end (if is-pack 2 8)))
5546 (cond (is-pack nil)
5547 ((string-match "[:']" name)
5548 (setq meth t))
5549 ((> p end-range) nil)
5550 (t
5551 (setq name (concat package name) meth t)))
5552 (setq index (cons name marker))
5553 (if is-pack
5554 (push index index-pack-alist)
5555 (push index index-alist))
5556 (if meth (push index index-meth-alist))
5557 (push index index-unsorted-alist)))
5558 ((match-beginning 16) ; POD section
5559 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5560 marker (make-marker))
5561 (set-marker marker (match-beginning 17))
5562 (set-text-properties 0 (length name) nil name)
5563 (setq name (concat (make-string
5564 (* 3 (- (char-after (match-beginning 16)) ?1))
5565 ?\ )
5566 name)
5567 index (cons name marker))
5568 (setq index1 (cons (concat "=" name) (cdr index)))
5569 (push index index-pod-alist)
5570 (push index1 index-unsorted-alist)))))
5571 (setq index-alist
5572 (if (default-value 'imenu-sort-function)
5573 (sort index-alist (default-value 'imenu-sort-function))
5574 (nreverse index-alist)))
5575 (and index-pod-alist
5576 (push (cons "+POD headers+..."
5577 (nreverse index-pod-alist))
5578 index-alist))
5579 (and (or index-pack-alist index-meth-alist)
5580 (let ((lst index-pack-alist) hier-list pack elt group name)
5581 ;; Remove "package ", reverse and uniquify.
5582 (while lst
5583 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5584 (if (assoc name hier-list) nil
5585 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5586 (setq lst index-meth-alist)
5587 (while lst
5588 (setq elt (car lst) lst (cdr lst))
5589 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5590 (setq pack (substring (car elt) 0 (match-beginning 0)))
5591 (if (setq group (assoc pack hier-list))
5592 (if (listp (cdr group))
5593 ;; Have some functions already
5594 (setcdr group
5595 (cons (cons (substring
5596 (car elt)
5597 (+ 2 (match-beginning 0)))
5598 (cdr elt))
5599 (cdr group)))
5600 (setcdr group (list (cons (substring
5601 (car elt)
5602 (+ 2 (match-beginning 0)))
5603 (cdr elt)))))
5604 (setq hier-list
5605 (cons (cons pack
5606 (list (cons (substring
5607 (car elt)
5608 (+ 2 (match-beginning 0)))
5609 (cdr elt))))
5610 hier-list))))))
5611 (push (cons "+Hierarchy+..."
5612 hier-list)
5613 index-alist)))
5614 (and index-pack-alist
5615 (push (cons "+Packages+..."
5616 (nreverse index-pack-alist))
5617 index-alist))
5618 (and (or index-pack-alist index-pod-alist
5619 (default-value 'imenu-sort-function))
5620 index-unsorted-alist
5621 (push (cons "+Unsorted List+..."
5622 (nreverse index-unsorted-alist))
5623 index-alist))
5624 (cperl-imenu-addback index-alist)))
5625
5626 \f
5627 ;; Suggested by Mark A. Hershberger
5628 (defun cperl-outline-level ()
5629 (looking-at outline-regexp)
5630 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5631 ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5632 ((match-beginning 2) 0) ; package
5633 ((match-beginning 8) 1) ; sub
5634 ((match-beginning 16)
5635 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5636 (t 5))) ; should not happen
5637
5638 \f
5639 (defun cperl-windowed-init ()
5640 "Initialization under windowed version."
5641 (cond ((featurep 'ps-print)
5642 (or cperl-faces-init
5643 (progn
5644 (and (boundp 'font-lock-multiline)
5645 (setq cperl-font-lock-multiline t))
5646 (cperl-init-faces))))
5647 ((not cperl-faces-init)
5648 (add-hook 'font-lock-mode-hook
5649 (function
5650 (lambda ()
5651 (if (memq major-mode '(perl-mode cperl-mode))
5652 (progn
5653 (or cperl-faces-init (cperl-init-faces)))))))
5654 (if (fboundp 'eval-after-load)
5655 (eval-after-load
5656 "ps-print"
5657 '(or cperl-faces-init (cperl-init-faces)))))))
5658
5659 (defvar cperl-font-lock-keywords-1 nil
5660 "Additional expressions to highlight in Perl mode. Minimal set.")
5661 (defvar cperl-font-lock-keywords nil
5662 "Additional expressions to highlight in Perl mode. Default set.")
5663 (defvar cperl-font-lock-keywords-2 nil
5664 "Additional expressions to highlight in Perl mode. Maximal set")
5665
5666 (defun cperl-load-font-lock-keywords ()
5667 (or cperl-faces-init (cperl-init-faces))
5668 cperl-font-lock-keywords)
5669
5670 (defun cperl-load-font-lock-keywords-1 ()
5671 (or cperl-faces-init (cperl-init-faces))
5672 cperl-font-lock-keywords-1)
5673
5674 (defun cperl-load-font-lock-keywords-2 ()
5675 (or cperl-faces-init (cperl-init-faces))
5676 cperl-font-lock-keywords-2)
5677
5678 (defun cperl-init-faces-weak ()
5679 ;; Allow `cperl-find-pods-heres' to run.
5680 (or (boundp 'font-lock-constant-face)
5681 (cperl-force-face font-lock-constant-face
5682 "Face for constant and label names"))
5683 (or (boundp 'font-lock-warning-face)
5684 (cperl-force-face font-lock-warning-face
5685 "Face for things which should stand out"))
5686 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5687 )
5688
5689 (defun cperl-init-faces ()
5690 (condition-case errs
5691 (progn
5692 (require 'font-lock)
5693 (and (fboundp 'font-lock-fontify-anchored-keywords)
5694 (featurep 'font-lock-extra)
5695 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5696 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5697 (if (fboundp 'font-lock-fontify-anchored-keywords)
5698 (setq font-lock-anchored t))
5699 (setq
5700 t-font-lock-keywords
5701 (list
5702 `("[ \t]+$" 0 ',cperl-invalid-face t)
5703 (cons
5704 (concat
5705 "\\(^\\|[^$@%&\\]\\)\\<\\("
5706 (mapconcat
5707 'identity
5708 '("if" "until" "while" "elsif" "else" "unless" "for"
5709 "foreach" "continue" "exit" "die" "last" "goto" "next"
5710 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5711 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5712 "\\|") ; Flow control
5713 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5714 ; In what follows we use `type' style
5715 ; for overwritable builtins
5716 (list
5717 (concat
5718 "\\(^\\|[^$@%&\\]\\)\\<\\("
5719 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5720 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5721 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5722 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5723 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5724 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5725 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5726 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5727 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5728 ;; "gethostbyname" "gethostent" "getlogin"
5729 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5730 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5731 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5732 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5733 ;; "getservbyport" "getservent" "getsockname"
5734 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5735 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5736 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5737 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5738 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5739 ;; "quotemeta" "rand" "read" "readdir" "readline"
5740 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5741 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5742 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5743 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5744 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5745 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5746 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5747 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5748 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5749 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5750 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5751 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5752 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5753 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5754 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5755 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5756 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5757 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5758 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5759 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5760 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5761 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5762 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5763 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5764 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5765 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5766 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5767 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5768 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5769 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5770 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5771 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5772 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5773 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5774 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5775 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5776 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5777 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5778 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5779 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5780 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5781 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5782 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5783 "\\)\\>") 2 'font-lock-type-face)
5784 ;; In what follows we use `other' style
5785 ;; for nonoverwritable builtins
5786 ;; Somehow 's', 'm' are not auto-generated???
5787 (list
5788 (concat
5789 "\\(^\\|[^$@%&\\]\\)\\<\\("
5790 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5791 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5792 ;; "eval" "exists" "for" "foreach" "format" "goto"
5793 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5794 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5795 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5796 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5797 ;; "undef" "unless" "unshift" "untie" "until" "use"
5798 ;; "while" "y"
5799 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5800 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5801 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5802 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5803 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5804 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5805 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5806 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5807 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5808 "\\|[sm]" ; Added manually
5809 "\\)\\>") 2 'cperl-nonoverridable-face)
5810 ;; (mapconcat 'identity
5811 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5812 ;; "#include" "#define" "#undef")
5813 ;; "\\|")
5814 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5815 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5816 ;; This highlights declarations and definitions differenty.
5817 ;; We do not try to highlight in the case of attributes:
5818 ;; it is already done by `cperl-find-pods-heres'
5819 (list (concat "\\<sub"
5820 cperl-white-and-comment-rex ; whitespace/comments
5821 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5822 "\\("
5823 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5824 "([^()]*)\\)?" ; prototype
5825 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5826 "[{;]")
5827 2 (if cperl-font-lock-multiline
5828 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5829 'font-lock-function-name-face
5830 'font-lock-variable-name-face)
5831 ;; need to manually set 'multiline' for older font-locks
5832 '(progn
5833 (if (< 1 (count-lines (match-beginning 0)
5834 (match-end 0)))
5835 (put-text-property
5836 (+ 3 (match-beginning 0)) (match-end 0)
5837 'syntax-type 'multiline))
5838 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5839 'font-lock-function-name-face
5840 'font-lock-variable-name-face))))
5841 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5842 2 font-lock-function-name-face)
5843 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5844 1 font-lock-function-name-face)
5845 (cond ((featurep 'font-lock-extra)
5846 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5847 (2 font-lock-string-face t)
5848 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5849 (font-lock-anchored
5850 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5851 (2 font-lock-string-face t)
5852 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5853 nil nil
5854 (1 font-lock-string-face t))))
5855 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5856 2 font-lock-string-face t)))
5857 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5858 font-lock-string-face t)
5859 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5860 font-lock-constant-face) ; labels
5861 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5862 2 font-lock-constant-face)
5863 ;; Uncomment to get perl-mode-like vars
5864 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5865 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5866 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5867 (cond ((featurep 'font-lock-extra)
5868 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5869 (3 font-lock-variable-name-face)
5870 (4 '(another 4 nil
5871 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5872 (1 font-lock-variable-name-face)
5873 (2 '(restart 2 nil) nil t)))
5874 nil t))) ; local variables, multiple
5875 (font-lock-anchored
5876 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5877 `(,(concat "\\<\\(my\\|local\\|our\\)"
5878 cperl-maybe-white-and-comment-rex
5879 "\\(("
5880 cperl-maybe-white-and-comment-rex
5881 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5882 (5 ,(if cperl-font-lock-multiline
5883 'font-lock-variable-name-face
5884 '(progn (setq cperl-font-lock-multiline-start
5885 (match-beginning 0))
5886 'font-lock-variable-name-face)))
5887 (,(concat "\\="
5888 cperl-maybe-white-and-comment-rex
5889 ","
5890 cperl-maybe-white-and-comment-rex
5891 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5892 ;; Bug in font-lock: limit is used not only to limit
5893 ;; searches, but to set the "extend window for
5894 ;; facification" property. Thus we need to minimize.
5895 ,(if cperl-font-lock-multiline
5896 '(if (match-beginning 3)
5897 (save-excursion
5898 (goto-char (match-beginning 3))
5899 (condition-case nil
5900 (forward-sexp 1)
5901 (error
5902 (condition-case nil
5903 (forward-char 200)
5904 (error nil)))) ; typeahead
5905 (1- (point))) ; report limit
5906 (forward-char -2)) ; disable continued expr
5907 '(if (match-beginning 3)
5908 (point-max) ; No limit for continuation
5909 (forward-char -2))) ; disable continued expr
5910 ,(if cperl-font-lock-multiline
5911 nil
5912 '(progn ; Do at end
5913 ;; "my" may be already fontified (POD),
5914 ;; so cperl-font-lock-multiline-start is nil
5915 (if (or (not cperl-font-lock-multiline-start)
5916 (> 2 (count-lines
5917 cperl-font-lock-multiline-start
5918 (point))))
5919 nil
5920 (put-text-property
5921 (1+ cperl-font-lock-multiline-start) (point)
5922 'syntax-type 'multiline))
5923 (setq cperl-font-lock-multiline-start nil)))
5924 (3 font-lock-variable-name-face))))
5925 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5926 3 font-lock-variable-name-face)))
5927 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5928 4 font-lock-variable-name-face)
5929 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5930 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5931 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5932 (setq
5933 t-font-lock-keywords-1
5934 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
5935 ;; not yet as of XEmacs 19.12, works with 21.1.11
5936 (or
5937 (not (featurep 'xemacs))
5938 (string< "21.1.9" emacs-version)
5939 (and (string< "21.1.10" emacs-version)
5940 (string< emacs-version "21.1.2")))
5941 '(
5942 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5943 (if (eq (char-after (match-beginning 2)) ?%)
5944 'cperl-hash-face
5945 'cperl-array-face)
5946 t) ; arrays and hashes
5947 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5948 1
5949 (if (= (- (match-end 2) (match-beginning 2)) 1)
5950 (if (eq (char-after (match-beginning 3)) ?{)
5951 'cperl-hash-face
5952 'cperl-array-face) ; arrays and hashes
5953 font-lock-variable-name-face) ; Just to put something
5954 t)
5955 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5956 (1 cperl-array-face)
5957 (2 font-lock-variable-name-face))
5958 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5959 (1 cperl-hash-face)
5960 (2 font-lock-variable-name-face))
5961 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5962 ;;; Too much noise from \s* @s[ and friends
5963 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
5964 ;;(3 font-lock-function-name-face t t)
5965 ;;(4
5966 ;; (if (cperl-slash-is-regexp)
5967 ;; font-lock-function-name-face 'default) nil t))
5968 )))
5969 (if cperl-highlight-variables-indiscriminately
5970 (setq t-font-lock-keywords-1
5971 (append t-font-lock-keywords-1
5972 (list '("\\([$*]{?\\sw+\\)" 1
5973 font-lock-variable-name-face)))))
5974 (setq cperl-font-lock-keywords-1
5975 (if cperl-syntaxify-by-font-lock
5976 (cons 'cperl-fontify-update
5977 t-font-lock-keywords)
5978 t-font-lock-keywords)
5979 cperl-font-lock-keywords cperl-font-lock-keywords-1
5980 cperl-font-lock-keywords-2 (append
5981 cperl-font-lock-keywords-1
5982 t-font-lock-keywords-1)))
5983 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5984 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
5985 (eval ; Avoid a warning
5986 '(font-lock-require-faces
5987 (list
5988 ;; Color-light Color-dark Gray-light Gray-dark Mono
5989 (list 'font-lock-comment-face
5990 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5991 nil
5992 [nil nil t t t]
5993 [nil nil t t t]
5994 nil)
5995 (list 'font-lock-string-face
5996 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5997 nil
5998 nil
5999 [nil nil t t t]
6000 nil)
6001 (list 'font-lock-function-name-face
6002 (vector
6003 "Blue" "LightSkyBlue" "Gray50" "LightGray"
6004 (cdr (assq 'background-color ; if mono
6005 (frame-parameters))))
6006 (vector
6007 nil nil nil nil
6008 (cdr (assq 'foreground-color ; if mono
6009 (frame-parameters))))
6010 [nil nil t t t]
6011 nil
6012 nil)
6013 (list 'font-lock-variable-name-face
6014 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6015 nil
6016 [nil nil t t t]
6017 [nil nil t t t]
6018 nil)
6019 (list 'font-lock-type-face
6020 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6021 nil
6022 [nil nil t t t]
6023 nil
6024 [nil nil t t t])
6025 (list 'font-lock-warning-face
6026 ["Pink" "Red" "Gray50" "LightGray"]
6027 ["gray20" "gray90"
6028 "gray80" "gray20"]
6029 [nil nil t t t]
6030 nil
6031 [nil nil t t t]
6032 )
6033 (list 'font-lock-constant-face
6034 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6035 nil
6036 [nil nil t t t]
6037 nil
6038 [nil nil t t t])
6039 (list 'cperl-nonoverridable-face
6040 ["chartreuse3" ("orchid1" "orange")
6041 nil "Gray80"]
6042 [nil nil "gray90"]
6043 [nil nil nil t t]
6044 [nil nil t t]
6045 [nil nil t t t])
6046 (list 'cperl-array-face
6047 ["blue" "yellow" nil "Gray80"]
6048 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6049 "gray90"]
6050 t
6051 nil
6052 nil)
6053 (list 'cperl-hash-face
6054 ["red" "red" nil "Gray80"]
6055 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6056 "gray90"]
6057 t
6058 t
6059 nil))))
6060 ;; Do it the dull way, without choose-color
6061 (defvar cperl-guessed-background nil
6062 "Display characteristics as guessed by cperl.")
6063 ;; (or (fboundp 'x-color-defined-p)
6064 ;; (defalias 'x-color-defined-p
6065 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6066 ;; ;; XEmacs >= 19.12
6067 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6068 ;; ;; XEmacs 19.11
6069 ;; (t 'x-valid-color-name-p))))
6070 (cperl-force-face font-lock-constant-face
6071 "Face for constant and label names")
6072 (cperl-force-face font-lock-variable-name-face
6073 "Face for variable names")
6074 (cperl-force-face font-lock-type-face
6075 "Face for data types")
6076 (cperl-force-face cperl-nonoverridable-face
6077 "Face for data types from another group")
6078 (cperl-force-face font-lock-warning-face
6079 "Face for things which should stand out")
6080 (cperl-force-face font-lock-comment-face
6081 "Face for comments")
6082 (cperl-force-face font-lock-function-name-face
6083 "Face for function names")
6084 (cperl-force-face cperl-hash-face
6085 "Face for hashes")
6086 (cperl-force-face cperl-array-face
6087 "Face for arrays")
6088 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6089 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6090 ;;(or (boundp 'font-lock-type-face)
6091 ;; (defconst font-lock-type-face
6092 ;; 'font-lock-type-face
6093 ;; "Face to use for data types."))
6094 ;;(or (boundp 'cperl-nonoverridable-face)
6095 ;; (defconst cperl-nonoverridable-face
6096 ;; 'cperl-nonoverridable-face
6097 ;; "Face to use for data types from another group."))
6098 ;;(if (not (featurep 'xemacs)) nil
6099 ;; (or (boundp 'font-lock-comment-face)
6100 ;; (defconst font-lock-comment-face
6101 ;; 'font-lock-comment-face
6102 ;; "Face to use for comments."))
6103 ;; (or (boundp 'font-lock-keyword-face)
6104 ;; (defconst font-lock-keyword-face
6105 ;; 'font-lock-keyword-face
6106 ;; "Face to use for keywords."))
6107 ;; (or (boundp 'font-lock-function-name-face)
6108 ;; (defconst font-lock-function-name-face
6109 ;; 'font-lock-function-name-face
6110 ;; "Face to use for function names.")))
6111 (if (and
6112 (not (cperl-is-face 'cperl-array-face))
6113 (cperl-is-face 'font-lock-emphasized-face))
6114 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6115 (if (and
6116 (not (cperl-is-face 'cperl-hash-face))
6117 (cperl-is-face 'font-lock-other-emphasized-face))
6118 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6119 (if (and
6120 (not (cperl-is-face 'cperl-nonoverridable-face))
6121 (cperl-is-face 'font-lock-other-type-face))
6122 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6123 ;;(or (boundp 'cperl-hash-face)
6124 ;; (defconst cperl-hash-face
6125 ;; 'cperl-hash-face
6126 ;; "Face to use for hashes."))
6127 ;;(or (boundp 'cperl-array-face)
6128 ;; (defconst cperl-array-face
6129 ;; 'cperl-array-face
6130 ;; "Face to use for arrays."))
6131 ;; Here we try to guess background
6132 (let ((background
6133 (if (boundp 'font-lock-background-mode)
6134 font-lock-background-mode
6135 'light))
6136 (face-list (and (fboundp 'face-list) (face-list))))
6137 ;;;; (fset 'cperl-is-face
6138 ;;;; (cond ((fboundp 'find-face)
6139 ;;;; (symbol-function 'find-face))
6140 ;;;; (face-list
6141 ;;;; (function (lambda (face) (member face face-list))))
6142 ;;;; (t
6143 ;;;; (function (lambda (face) (boundp face))))))
6144 (defvar cperl-guessed-background
6145 (if (and (boundp 'font-lock-display-type)
6146 (eq font-lock-display-type 'grayscale))
6147 'gray
6148 background)
6149 "Background as guessed by CPerl mode")
6150 (and (not (cperl-is-face 'font-lock-constant-face))
6151 (cperl-is-face 'font-lock-reference-face)
6152 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6153 (if (cperl-is-face 'font-lock-type-face) nil
6154 (copy-face 'default 'font-lock-type-face)
6155 (cond
6156 ((eq background 'light)
6157 (set-face-foreground 'font-lock-type-face
6158 (if (x-color-defined-p "seagreen")
6159 "seagreen"
6160 "sea green")))
6161 ((eq background 'dark)
6162 (set-face-foreground 'font-lock-type-face
6163 (if (x-color-defined-p "os2pink")
6164 "os2pink"
6165 "pink")))
6166 (t
6167 (set-face-background 'font-lock-type-face "gray90"))))
6168 (if (cperl-is-face 'cperl-nonoverridable-face)
6169 nil
6170 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6171 (cond
6172 ((eq background 'light)
6173 (set-face-foreground 'cperl-nonoverridable-face
6174 (if (x-color-defined-p "chartreuse3")
6175 "chartreuse3"
6176 "chartreuse")))
6177 ((eq background 'dark)
6178 (set-face-foreground 'cperl-nonoverridable-face
6179 (if (x-color-defined-p "orchid1")
6180 "orchid1"
6181 "orange")))))
6182 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6183 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6184 ;;; (cond
6185 ;;; ((eq background 'light)
6186 ;;; (set-face-background 'font-lock-other-emphasized-face
6187 ;;; (if (x-color-defined-p "lightyellow2")
6188 ;;; "lightyellow2"
6189 ;;; (if (x-color-defined-p "lightyellow")
6190 ;;; "lightyellow"
6191 ;;; "light yellow"))))
6192 ;;; ((eq background 'dark)
6193 ;;; (set-face-background 'font-lock-other-emphasized-face
6194 ;;; (if (x-color-defined-p "navy")
6195 ;;; "navy"
6196 ;;; (if (x-color-defined-p "darkgreen")
6197 ;;; "darkgreen"
6198 ;;; "dark green"))))
6199 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6200 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6201 ;;; (copy-face 'bold 'font-lock-emphasized-face)
6202 ;;; (cond
6203 ;;; ((eq background 'light)
6204 ;;; (set-face-background 'font-lock-emphasized-face
6205 ;;; (if (x-color-defined-p "lightyellow2")
6206 ;;; "lightyellow2"
6207 ;;; "lightyellow")))
6208 ;;; ((eq background 'dark)
6209 ;;; (set-face-background 'font-lock-emphasized-face
6210 ;;; (if (x-color-defined-p "navy")
6211 ;;; "navy"
6212 ;;; (if (x-color-defined-p "darkgreen")
6213 ;;; "darkgreen"
6214 ;;; "dark green"))))
6215 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6216 (if (cperl-is-face 'font-lock-variable-name-face) nil
6217 (copy-face 'italic 'font-lock-variable-name-face))
6218 (if (cperl-is-face 'font-lock-constant-face) nil
6219 (copy-face 'italic 'font-lock-constant-face))))
6220 (setq cperl-faces-init t))
6221 (error (message "cperl-init-faces (ignored): %s" errs))))
6222
6223
6224 (defun cperl-ps-print-init ()
6225 "Initialization of `ps-print' components for faces used in CPerl."
6226 (eval-after-load "ps-print"
6227 '(setq ps-bold-faces
6228 ;; font-lock-variable-name-face
6229 ;; font-lock-constant-face
6230 (append '(cperl-array-face cperl-hash-face)
6231 ps-bold-faces)
6232 ps-italic-faces
6233 ;; font-lock-constant-face
6234 (append '(cperl-nonoverridable-face cperl-hash-face)
6235 ps-italic-faces)
6236 ps-underlined-faces
6237 ;; font-lock-type-face
6238 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6239 ps-underlined-faces))))
6240
6241 (defvar ps-print-face-extension-alist)
6242
6243 (defun cperl-ps-print (&optional file)
6244 "Pretty-print in CPerl style.
6245 If optional argument FILE is an empty string, prints to printer, otherwise
6246 to the file FILE. If FILE is nil, prompts for a file name.
6247
6248 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6249 (interactive)
6250 (or file
6251 (setq file (read-from-minibuffer
6252 "Print to file (if empty - to printer): "
6253 (concat (buffer-file-name) ".ps")
6254 nil nil 'file-name-history)))
6255 (or (> (length file) 0)
6256 (setq file nil))
6257 (require 'ps-print) ; To get ps-print-face-extension-alist
6258 (let ((ps-print-color-p t)
6259 (ps-print-face-extension-alist ps-print-face-extension-alist))
6260 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6261 (ps-print-buffer-with-faces file)))
6262
6263 ;;; (defun cperl-ps-print-init ()
6264 ;;; "Initialization of `ps-print' components for faces used in CPerl."
6265 ;;; ;; Guard against old versions
6266 ;;; (defvar ps-underlined-faces nil)
6267 ;;; (defvar ps-bold-faces nil)
6268 ;;; (defvar ps-italic-faces nil)
6269 ;;; (setq ps-bold-faces
6270 ;;; (append '(font-lock-emphasized-face
6271 ;;; cperl-array-face
6272 ;;; font-lock-keyword-face
6273 ;;; font-lock-variable-name-face
6274 ;;; font-lock-constant-face
6275 ;;; font-lock-reference-face
6276 ;;; font-lock-other-emphasized-face
6277 ;;; cperl-hash-face)
6278 ;;; ps-bold-faces))
6279 ;;; (setq ps-italic-faces
6280 ;;; (append '(cperl-nonoverridable-face
6281 ;;; font-lock-constant-face
6282 ;;; font-lock-reference-face
6283 ;;; font-lock-other-emphasized-face
6284 ;;; cperl-hash-face)
6285 ;;; ps-italic-faces))
6286 ;;; (setq ps-underlined-faces
6287 ;;; (append '(font-lock-emphasized-face
6288 ;;; cperl-array-face
6289 ;;; font-lock-other-emphasized-face
6290 ;;; cperl-hash-face
6291 ;;; cperl-nonoverridable-face font-lock-type-face)
6292 ;;; ps-underlined-faces))
6293 ;;; (cons 'font-lock-type-face ps-underlined-faces))
6294
6295
6296 (if (cperl-enable-font-lock) (cperl-windowed-init))
6297
6298 (defconst cperl-styles-entries
6299 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6300 cperl-label-offset cperl-extra-newline-before-brace
6301 cperl-extra-newline-before-brace-multiline
6302 cperl-merge-trailing-else
6303 cperl-continued-statement-offset))
6304
6305 (defconst cperl-style-examples
6306 "##### Numbers etc are: cperl-indent-level cperl-brace-offset
6307 ##### cperl-continued-brace-offset cperl-label-offset
6308 ##### cperl-continued-statement-offset
6309 ##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6310
6311 ########### (Do not forget cperl-extra-newline-before-brace-multiline)
6312
6313 ### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6314 if (foo) {
6315 bar
6316 baz;
6317 label:
6318 {
6319 boon;
6320 }
6321 } else {
6322 stop;
6323 }
6324
6325 ### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6326 if (foo) {
6327 bar
6328 baz;
6329 label:
6330 {
6331 boon;
6332 }
6333 } else {
6334 stop;
6335 }
6336
6337 ### GNU 2/0/0/-2/2/nil/t
6338 if (foo)
6339 {
6340 bar
6341 baz;
6342 label:
6343 {
6344 boon;
6345 }
6346 }
6347 else
6348 {
6349 stop;
6350 }
6351
6352 ### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6353 if (foo)
6354 {
6355 bar
6356 baz;
6357 label:
6358 {
6359 boon;
6360 }
6361 }
6362 else
6363 {
6364 stop;
6365 }
6366
6367 ### BSD (=C++, but will not change preexisting merge-trailing-else
6368 ### and extra-newline-before-brace ) 4/0/-4/-4/4
6369 if (foo)
6370 {
6371 bar
6372 baz;
6373 label:
6374 {
6375 boon;
6376 }
6377 }
6378 else
6379 {
6380 stop;
6381 }
6382
6383 ### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6384 ### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6385 if (foo)
6386 {
6387 bar
6388 baz;
6389 label:
6390 {
6391 boon;
6392 }
6393 }
6394 else
6395 {
6396 stop;
6397 }
6398
6399 ### Whitesmith (=PerlStyle, but will not change preexisting
6400 ### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6401 if (foo)
6402 {
6403 bar
6404 baz;
6405 label:
6406 {
6407 boon;
6408 }
6409 }
6410 else
6411 {
6412 stop;
6413 }
6414 "
6415 "Examples of if/else with different indent styles (with v4.23).")
6416
6417 (defconst cperl-style-alist
6418 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6419 (cperl-indent-level . 2)
6420 (cperl-brace-offset . 0)
6421 (cperl-continued-brace-offset . 0)
6422 (cperl-label-offset . -2)
6423 (cperl-continued-statement-offset . 2)
6424 (cperl-extra-newline-before-brace . nil)
6425 (cperl-extra-newline-before-brace-multiline . nil)
6426 (cperl-merge-trailing-else . t))
6427
6428 ("PerlStyle" ; CPerl with 4 as indent
6429 (cperl-indent-level . 4)
6430 (cperl-brace-offset . 0)
6431 (cperl-continued-brace-offset . 0)
6432 (cperl-label-offset . -4)
6433 (cperl-continued-statement-offset . 4)
6434 (cperl-extra-newline-before-brace . nil)
6435 (cperl-extra-newline-before-brace-multiline . nil)
6436 (cperl-merge-trailing-else . t))
6437
6438 ("GNU"
6439 (cperl-indent-level . 2)
6440 (cperl-brace-offset . 0)
6441 (cperl-continued-brace-offset . 0)
6442 (cperl-label-offset . -2)
6443 (cperl-continued-statement-offset . 2)
6444 (cperl-extra-newline-before-brace . t)
6445 (cperl-extra-newline-before-brace-multiline . t)
6446 (cperl-merge-trailing-else . nil))
6447
6448 ("K&R"
6449 (cperl-indent-level . 5)
6450 (cperl-brace-offset . 0)
6451 (cperl-continued-brace-offset . -5)
6452 (cperl-label-offset . -5)
6453 (cperl-continued-statement-offset . 5)
6454 ;;(cperl-extra-newline-before-brace . nil) ; ???
6455 ;;(cperl-extra-newline-before-brace-multiline . nil)
6456 (cperl-merge-trailing-else . nil))
6457
6458 ("BSD"
6459 (cperl-indent-level . 4)
6460 (cperl-brace-offset . 0)
6461 (cperl-continued-brace-offset . -4)
6462 (cperl-label-offset . -4)
6463 (cperl-continued-statement-offset . 4)
6464 ;;(cperl-extra-newline-before-brace . nil) ; ???
6465 ;;(cperl-extra-newline-before-brace-multiline . nil)
6466 ;;(cperl-merge-trailing-else . nil) ; ???
6467 )
6468
6469 ("C++"
6470 (cperl-indent-level . 4)
6471 (cperl-brace-offset . 0)
6472 (cperl-continued-brace-offset . -4)
6473 (cperl-label-offset . -4)
6474 (cperl-continued-statement-offset . 4)
6475 (cperl-extra-newline-before-brace . t)
6476 (cperl-extra-newline-before-brace-multiline . t)
6477 (cperl-merge-trailing-else . nil))
6478
6479 ("Whitesmith"
6480 (cperl-indent-level . 4)
6481 (cperl-brace-offset . 0)
6482 (cperl-continued-brace-offset . 0)
6483 (cperl-label-offset . -4)
6484 (cperl-continued-statement-offset . 4)
6485 ;;(cperl-extra-newline-before-brace . nil) ; ???
6486 ;;(cperl-extra-newline-before-brace-multiline . nil)
6487 ;;(cperl-merge-trailing-else . nil) ; ???
6488 )
6489 ("Current"))
6490 "List of variables to set to get a particular indentation style.
6491 Should be used via `cperl-set-style' or via Perl menu.
6492
6493 See examples in `cperl-style-examples'.")
6494
6495 (defun cperl-set-style (style)
6496 "Set CPerl mode variables to use one of several different indentation styles.
6497 The arguments are a string representing the desired style.
6498 The list of styles is in `cperl-style-alist', available styles
6499 are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6500
6501 The current value of style is memorized (unless there is a memorized
6502 data already), may be restored by `cperl-set-style-back'.
6503
6504 Chosing \"Current\" style will not change style, so this may be used for
6505 side-effect of memorizing only. Examples in `cperl-style-examples'."
6506 (interactive
6507 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6508 cperl-style-alist)))
6509 (list (completing-read "Enter style: " list nil 'insist))))
6510 (or cperl-old-style
6511 (setq cperl-old-style
6512 (mapcar (function
6513 (lambda (name)
6514 (cons name (eval name))))
6515 cperl-styles-entries)))
6516 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6517 (while style
6518 (setq setting (car style) style (cdr style))
6519 (set (car setting) (cdr setting)))))
6520
6521 (defun cperl-set-style-back ()
6522 "Restore a style memorized by `cperl-set-style'."
6523 (interactive)
6524 (or cperl-old-style (error "The style was not changed"))
6525 (let (setting)
6526 (while cperl-old-style
6527 (setq setting (car cperl-old-style)
6528 cperl-old-style (cdr cperl-old-style))
6529 (set (car setting) (cdr setting)))))
6530
6531 (defun cperl-check-syntax ()
6532 (interactive)
6533 (require 'mode-compile)
6534 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6535 (eval '(mode-compile)))) ; Avoid a warning
6536
6537 (defun cperl-info-buffer (type)
6538 ;; Returns buffer with documentation. Creates if missing.
6539 ;; If TYPE, this vars buffer.
6540 ;; Special care is taken to not stomp over an existing info buffer
6541 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6542 (info (get-buffer bname))
6543 (oldbuf (get-buffer "*info*")))
6544 (if info info
6545 (save-window-excursion
6546 ;; Get Info running
6547 (require 'info)
6548 (cond (oldbuf
6549 (set-buffer oldbuf)
6550 (rename-buffer "*info-perl-tmp*")))
6551 (save-window-excursion
6552 (info))
6553 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6554 (set-buffer "*info*")
6555 (rename-buffer bname)
6556 (cond (oldbuf
6557 (set-buffer "*info-perl-tmp*")
6558 (rename-buffer "*info*")
6559 (set-buffer bname)))
6560 (make-local-variable 'window-min-height)
6561 (setq window-min-height 2)
6562 (current-buffer)))))
6563
6564 (defun cperl-word-at-point (&optional p)
6565 "Return the word at point or at P."
6566 (save-excursion
6567 (if p (goto-char p))
6568 (or (cperl-word-at-point-hard)
6569 (progn
6570 (require 'etags)
6571 (funcall (or (and (boundp 'find-tag-default-function)
6572 find-tag-default-function)
6573 (get major-mode 'find-tag-default-function)
6574 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6575 ;; automatically used within `find-tag-default':
6576 'find-tag-default))))))
6577
6578 (defun cperl-info-on-command (command)
6579 "Show documentation for Perl command COMMAND in other window.
6580 If perl-info buffer is shown in some frame, uses this frame.
6581 Customized by setting variables `cperl-shrink-wrap-info-frame',
6582 `cperl-max-help-size'."
6583 (interactive
6584 (let* ((default (cperl-word-at-point))
6585 (read (read-string
6586 (format "Find doc for Perl function (default %s): "
6587 default))))
6588 (list (if (equal read "")
6589 default
6590 read))))
6591
6592 (let ((buffer (current-buffer))
6593 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6594 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6595 max-height char-height buf-list)
6596 (if (string-match "^-[a-zA-Z]$" command)
6597 (setq cmd-desc "^-X[ \t\n]"))
6598 (setq isvar (string-match "^[$@%]" command)
6599 buf (cperl-info-buffer isvar)
6600 iniwin (selected-window)
6601 fr1 (window-frame iniwin))
6602 (set-buffer buf)
6603 (goto-char (point-min))
6604 (or isvar
6605 (progn (re-search-forward "^-X[ \t\n]")
6606 (forward-line -1)))
6607 (if (re-search-forward cmd-desc nil t)
6608 (progn
6609 ;; Go back to beginning of the group (ex, for qq)
6610 (if (re-search-backward "^[ \t\n\f]")
6611 (forward-line 1))
6612 (beginning-of-line)
6613 ;; Get some of
6614 (setq pos (point)
6615 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6616 (while (and (not win) buf-list)
6617 (setq win (get-buffer-window (car buf-list) t))
6618 (setq buf-list (cdr buf-list)))
6619 (or (not win)
6620 (eq (window-buffer win) buf)
6621 (set-window-buffer win buf))
6622 (and win (setq fr2 (window-frame win)))
6623 (if (or (not fr2) (eq fr1 fr2))
6624 (pop-to-buffer buf)
6625 (special-display-popup-frame buf) ; Make it visible
6626 (select-window win))
6627 (goto-char pos) ; Needed (?!).
6628 ;; Resize
6629 (setq iniheight (window-height)
6630 frheight (frame-height)
6631 not-loner (< iniheight (1- frheight))) ; Are not alone
6632 (cond ((if not-loner cperl-max-help-size
6633 cperl-shrink-wrap-info-frame)
6634 (setq height
6635 (+ 2
6636 (count-lines
6637 pos
6638 (save-excursion
6639 (if (re-search-forward
6640 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6641 (match-beginning 0) (point-max)))))
6642 max-height
6643 (if not-loner
6644 (/ (* (- frheight 3) cperl-max-help-size) 100)
6645 (setq char-height (frame-char-height))
6646 ;; Non-functioning under OS/2:
6647 (if (eq char-height 1) (setq char-height 18))
6648 ;; Title, menubar, + 2 for slack
6649 (- (/ (display-pixel-height) char-height) 4)))
6650 (if (> height max-height) (setq height max-height))
6651 ;;(message "was %s doing %s" iniheight height)
6652 (if not-loner
6653 (enlarge-window (- height iniheight))
6654 (set-frame-height (window-frame win) (1+ height)))))
6655 (set-window-start (selected-window) pos))
6656 (message "No entry for %s found." command))
6657 ;;(pop-to-buffer buffer)
6658 (select-window iniwin)))
6659
6660 (defun cperl-info-on-current-command ()
6661 "Show documentation for Perl command at point in other window."
6662 (interactive)
6663 (cperl-info-on-command (cperl-word-at-point)))
6664
6665 (defun cperl-imenu-info-imenu-search ()
6666 (if (looking-at "^-X[ \t\n]") nil
6667 (re-search-backward
6668 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6669 (forward-line 1)))
6670
6671 (defun cperl-imenu-info-imenu-name ()
6672 (buffer-substring
6673 (match-beginning 1) (match-end 1)))
6674
6675 (defun cperl-imenu-on-info ()
6676 "Shows imenu for Perl Info Buffer.
6677 Opens Perl Info buffer if needed."
6678 (interactive)
6679 (let* ((buffer (current-buffer))
6680 imenu-create-index-function
6681 imenu-prev-index-position-function
6682 imenu-extract-index-name-function
6683 (index-item (save-restriction
6684 (save-window-excursion
6685 (set-buffer (cperl-info-buffer nil))
6686 (setq imenu-create-index-function
6687 'imenu-default-create-index-function
6688 imenu-prev-index-position-function
6689 'cperl-imenu-info-imenu-search
6690 imenu-extract-index-name-function
6691 'cperl-imenu-info-imenu-name)
6692 (imenu-choose-buffer-index)))))
6693 (and index-item
6694 (progn
6695 (push-mark)
6696 (pop-to-buffer "*info-perl*")
6697 (cond
6698 ((markerp (cdr index-item))
6699 (goto-char (marker-position (cdr index-item))))
6700 (t
6701 (goto-char (cdr index-item))))
6702 (set-window-start (selected-window) (point))
6703 (pop-to-buffer buffer)))))
6704
6705 (defun cperl-lineup (beg end &optional step minshift)
6706 "Lineup construction in a region.
6707 Beginning of region should be at the start of a construction.
6708 All first occurrences of this construction in the lines that are
6709 partially contained in the region are lined up at the same column.
6710
6711 MINSHIFT is the minimal amount of space to insert before the construction.
6712 STEP is the tabwidth to position constructions.
6713 If STEP is nil, `cperl-lineup-step' will be used
6714 \(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6715 Will not move the position at the start to the left."
6716 (interactive "r")
6717 (let (search col tcol seen b)
6718 (save-excursion
6719 (goto-char end)
6720 (end-of-line)
6721 (setq end (point-marker))
6722 (goto-char beg)
6723 (skip-chars-forward " \t\f")
6724 (setq beg (point-marker))
6725 (indent-region beg end nil)
6726 (goto-char beg)
6727 (setq col (current-column))
6728 (if (looking-at "[a-zA-Z0-9_]")
6729 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6730 (setq search
6731 (concat "\\<"
6732 (regexp-quote
6733 (buffer-substring (match-beginning 0)
6734 (match-end 0))) "\\>"))
6735 (error "Cannot line up in a middle of the word"))
6736 (if (looking-at "$")
6737 (error "Cannot line up end of line"))
6738 (setq search (regexp-quote (char-to-string (following-char)))))
6739 (setq step (or step cperl-lineup-step cperl-indent-level))
6740 (or minshift (setq minshift 1))
6741 (while (progn
6742 (beginning-of-line 2)
6743 (and (< (point) end)
6744 (re-search-forward search end t)
6745 (goto-char (match-beginning 0))))
6746 (setq tcol (current-column) seen t)
6747 (if (> tcol col) (setq col tcol)))
6748 (or seen
6749 (error "The construction to line up occurred only once"))
6750 (goto-char beg)
6751 (setq col (+ col minshift))
6752 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6753 (while
6754 (progn
6755 (cperl-make-indent col)
6756 (beginning-of-line 2)
6757 (and (< (point) end)
6758 (re-search-forward search end t)
6759 (goto-char (match-beginning 0)))))))) ; No body
6760
6761 (defun cperl-etags (&optional add all files) ;; NOT USED???
6762 "Run etags with appropriate options for Perl files.
6763 If optional argument ALL is `recursive', will process Perl files
6764 in subdirectories too."
6765 (interactive)
6766 (let ((cmd "etags")
6767 (args '("-l" "none" "-r"
6768 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6769 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6770 "-r"
6771 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6772 "-r"
6773 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6774 res)
6775 (if add (setq args (cons "-a" args)))
6776 (or files (setq files (list buffer-file-name)))
6777 (cond
6778 ((eq all 'recursive)
6779 ;;(error "Not implemented: recursive")
6780 (setq args (append (list "-e"
6781 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6782 use File::Find;
6783 find(\\&wanted, '.');
6784 exec @ARGV;"
6785 cmd) args)
6786 cmd "perl"))
6787 (all
6788 ;;(error "Not implemented: all")
6789 (setq args (append (list "-e"
6790 "push @ARGV, <*.PL *.pl *.pm>;
6791 exec @ARGV;"
6792 cmd) args)
6793 cmd "perl"))
6794 (t
6795 (setq args (append args files))))
6796 (setq res (apply 'call-process cmd nil nil nil args))
6797 (or (eq res 0)
6798 (message "etags returned \"%s\"" res))))
6799
6800 (defun cperl-toggle-auto-newline ()
6801 "Toggle the state of `cperl-auto-newline'."
6802 (interactive)
6803 (setq cperl-auto-newline (not cperl-auto-newline))
6804 (message "Newlines will %sbe auto-inserted now."
6805 (if cperl-auto-newline "" "not ")))
6806
6807 (defun cperl-toggle-abbrev ()
6808 "Toggle the state of automatic keyword expansion in CPerl mode."
6809 (interactive)
6810 (abbrev-mode (if abbrev-mode 0 1))
6811 (message "Perl control structure will %sbe auto-inserted now."
6812 (if abbrev-mode "" "not ")))
6813
6814
6815 (defun cperl-toggle-electric ()
6816 "Toggle the state of parentheses doubling in CPerl mode."
6817 (interactive)
6818 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6819 (message "Parentheses will %sbe auto-doubled now."
6820 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6821
6822 (defun cperl-toggle-autohelp ()
6823 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6824 Delay of auto-help controlled by `cperl-lazy-help-time'."
6825 (interactive)
6826 (if (fboundp 'run-with-idle-timer)
6827 (progn
6828 (if cperl-lazy-installed
6829 (cperl-lazy-unstall)
6830 (cperl-lazy-install))
6831 (message "Perl help messages will %sbe automatically shown now."
6832 (if cperl-lazy-installed "" "not ")))
6833 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6834
6835 (defun cperl-toggle-construct-fix ()
6836 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6837 (interactive)
6838 (setq cperl-indent-region-fix-constructs
6839 (if cperl-indent-region-fix-constructs
6840 nil
6841 1))
6842 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6843 (if cperl-indent-region-fix-constructs "" "not ")))
6844
6845 (defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6846 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6847 Nonpositive numeric argument disables debugging messages. The message
6848 summarizes which regions it was decided to rescan for syntactic constructs.
6849
6850 The message looks like this:
6851
6852 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6853
6854 Numbers are character positions in the buffer. REQ provides the range to
6855 rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6856 for correct operation it should start and end outside any special syntactic
6857 construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6858 by CPerl."
6859 (interactive "P")
6860 (or arg
6861 (setq arg (if (eq cperl-syntaxify-by-font-lock
6862 (if backtrace 'backtrace 'message)) 0 1)))
6863 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6864 (setq cperl-syntaxify-by-font-lock arg)
6865 (message "Debugging messages of syntax unwind %sabled."
6866 (if (eq arg t) "dis" "en")))
6867
6868 ;;;; Tags file creation.
6869
6870 (defvar cperl-tmp-buffer " *cperl-tmp*")
6871
6872 (defun cperl-setup-tmp-buf ()
6873 (set-buffer (get-buffer-create cperl-tmp-buffer))
6874 (set-syntax-table cperl-mode-syntax-table)
6875 (buffer-disable-undo)
6876 (auto-fill-mode 0)
6877 (if cperl-use-syntax-table-text-property-for-tags
6878 (progn
6879 (make-local-variable 'parse-sexp-lookup-properties)
6880 ;; Do not introduce variable if not needed, we check it!
6881 (set 'parse-sexp-lookup-properties t))))
6882
6883 ;; Copied from imenu-example--name-and-position.
6884 (defvar imenu-use-markers)
6885
6886 (defun cperl-imenu-name-and-position ()
6887 "Return the current/previous sexp and its (beginning) location.
6888 Does not move point."
6889 (save-excursion
6890 (forward-sexp -1)
6891 (let ((beg (if imenu-use-markers (point-marker) (point)))
6892 (end (progn (forward-sexp) (point))))
6893 (cons (buffer-substring beg end)
6894 beg))))
6895
6896 (defun cperl-xsub-scan ()
6897 (require 'imenu)
6898 (let ((index-alist '())
6899 (prev-pos 0) index index1 name package prefix)
6900 (goto-char (point-min))
6901 ;; Search for the function
6902 (progn ;;save-match-data
6903 (while (re-search-forward
6904 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6905 nil t)
6906 (cond
6907 ((match-beginning 2) ; SECTION
6908 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6909 (goto-char (match-beginning 0))
6910 (skip-chars-forward " \t")
6911 (forward-char 1)
6912 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6913 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6914 (setq prefix nil)))
6915 ((not package) nil) ; C language section
6916 ((match-beginning 3) ; XSUB
6917 (goto-char (1+ (match-beginning 3)))
6918 (setq index (cperl-imenu-name-and-position))
6919 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6920 (if (and prefix (string-match (concat "^" prefix) name))
6921 (setq name (substring name (length prefix))))
6922 (cond ((string-match "::" name) nil)
6923 (t
6924 (setq index1 (cons (concat package "::" name) (cdr index)))
6925 (push index1 index-alist)))
6926 (setcar index name)
6927 (push index index-alist))
6928 (t ; BOOT: section
6929 ;; (beginning-of-line)
6930 (setq index (cperl-imenu-name-and-position))
6931 (setcar index (concat package "::BOOT:"))
6932 (push index index-alist)))))
6933 index-alist))
6934
6935 (defvar cperl-unreadable-ok nil)
6936
6937 (defun cperl-find-tags (ifile xs topdir)
6938 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6939 (cperl-pod-here-fontify nil) f file)
6940 (save-excursion
6941 (if b (set-buffer b)
6942 (cperl-setup-tmp-buf))
6943 (erase-buffer)
6944 (condition-case err
6945 (setq file (car (insert-file-contents ifile)))
6946 (error (if cperl-unreadable-ok nil
6947 (if (y-or-n-p
6948 (format "File %s unreadable. Continue? " ifile))
6949 (setq cperl-unreadable-ok t)
6950 (error "Aborting: unreadable file %s" ifile)))))
6951 (if (not file)
6952 (message "Unreadable file %s" ifile)
6953 (message "Scanning file %s ..." file)
6954 (if (and cperl-use-syntax-table-text-property-for-tags
6955 (not xs))
6956 (condition-case err ; after __END__ may have garbage
6957 (cperl-find-pods-heres nil nil noninteractive)
6958 (error (message "While scanning for syntax: %s" err))))
6959 (if xs
6960 (setq lst (cperl-xsub-scan))
6961 (setq ind (cperl-imenu--create-perl-index))
6962 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6963 (setq lst
6964 (mapcar
6965 (function
6966 (lambda (elt)
6967 (cond ((string-match "^[_a-zA-Z]" (car elt))
6968 (goto-char (cdr elt))
6969 (beginning-of-line) ; pos should be of the start of the line
6970 (list (car elt)
6971 (point)
6972 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6973 (buffer-substring (progn
6974 (goto-char (cdr elt))
6975 ;; After name now...
6976 (or (eolp) (forward-char 1))
6977 (point))
6978 (progn
6979 (beginning-of-line)
6980 (point))))))))
6981 lst))
6982 (erase-buffer)
6983 (while lst
6984 (setq elt (car lst) lst (cdr lst))
6985 (if elt
6986 (progn
6987 (insert (elt elt 3)
6988 127
6989 (if (string-match "^package " (car elt))
6990 (substring (car elt) 8)
6991 (car elt) )
6992 1
6993 (number-to-string (elt elt 2)) ; Line
6994 ","
6995 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
6996 "\n")
6997 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
6998 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
6999 (elt elt 3)))
7000 ;; Need to insert the name without package as well
7001 (setq lst (cons (cons (substring (elt elt 3)
7002 (match-beginning 1)
7003 (match-end 1))
7004 (cdr elt))
7005 lst))))))
7006 (setq pos (point))
7007 (goto-char 1)
7008 (setq rel file)
7009 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7010 (set-text-properties 0 (length rel) nil rel)
7011 (and (equal topdir (substring rel 0 (length topdir)))
7012 (setq rel (substring file (length topdir))))
7013 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
7014 (setq ret (buffer-substring 1 (point-max)))
7015 (erase-buffer)
7016 (or noninteractive
7017 (message "Scanning file %s finished" file))
7018 ret))))
7019
7020 (defun cperl-add-tags-recurse-noxs ()
7021 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
7022 Use as
7023 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7024 -f cperl-add-tags-recurse-noxs
7025 "
7026 (cperl-write-tags nil nil t t nil t))
7027
7028 (defun cperl-add-tags-recurse-noxs-fullpath ()
7029 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7030 Writes down fullpath, so TAGS is relocatable (but if the build directory
7031 is relocated, the file TAGS inside it breaks). Use as
7032 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7033 -f cperl-add-tags-recurse-noxs-fullpath
7034 "
7035 (cperl-write-tags nil nil t t nil t ""))
7036
7037 (defun cperl-add-tags-recurse ()
7038 "Add to TAGS file data for Perl files in the current directory and kids.
7039 Use as
7040 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7041 -f cperl-add-tags-recurse
7042 "
7043 (cperl-write-tags nil nil t t))
7044
7045 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7046 ;; If INBUFFER, do not select buffer, and do not save
7047 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7048 (require 'etags)
7049 (if file nil
7050 (setq file (if dir default-directory (buffer-file-name)))
7051 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7052 (or topdir
7053 (setq topdir default-directory))
7054 (let ((tags-file-name "TAGS")
7055 (case-fold-search (eq system-type 'emx))
7056 xs rel tm)
7057 (save-excursion
7058 (cond (inbuffer nil) ; Already there
7059 ((file-exists-p tags-file-name)
7060 (if (featurep 'xemacs)
7061 (visit-tags-table-buffer)
7062 (visit-tags-table-buffer tags-file-name)))
7063 (t (set-buffer (find-file-noselect tags-file-name))))
7064 (cond
7065 (dir
7066 (cond ((eq erase 'ignore))
7067 (erase
7068 (erase-buffer)
7069 (setq erase 'ignore)))
7070 (let ((files
7071 (condition-case err
7072 (directory-files file t
7073 (if recurse nil cperl-scan-files-regexp)
7074 t)
7075 (error
7076 (if cperl-unreadable-ok nil
7077 (if (y-or-n-p
7078 (format "Directory %s unreadable. Continue? " file))
7079 (setq cperl-unreadable-ok t
7080 tm nil) ; Return empty list
7081 (error "Aborting: unreadable directory %s" file)))))))
7082 (mapc (function
7083 (lambda (file)
7084 (cond
7085 ((string-match cperl-noscan-files-regexp file)
7086 nil)
7087 ((not (file-directory-p file))
7088 (if (string-match cperl-scan-files-regexp file)
7089 (cperl-write-tags file erase recurse nil t noxs topdir)))
7090 ((not recurse) nil)
7091 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7092 files)))
7093 (t
7094 (setq xs (string-match "\\.xs$" file))
7095 (if (not (and xs noxs))
7096 (progn
7097 (cond ((eq erase 'ignore) (goto-char (point-max)))
7098 (erase (erase-buffer))
7099 (t
7100 (goto-char 1)
7101 (setq rel file)
7102 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7103 (set-text-properties 0 (length rel) nil rel)
7104 (and (equal topdir (substring rel 0 (length topdir)))
7105 (setq rel (substring file (length topdir))))
7106 (if (search-forward (concat "\f\n" rel ",") nil t)
7107 (progn
7108 (search-backward "\f\n")
7109 (delete-region (point)
7110 (save-excursion
7111 (forward-char 1)
7112 (if (search-forward "\f\n"
7113 nil 'toend)
7114 (- (point) 2)
7115 (point-max)))))
7116 (goto-char (point-max)))))
7117 (insert (cperl-find-tags file xs topdir))))))
7118 (if inbuffer nil ; Delegate to the caller
7119 (save-buffer 0) ; No backup
7120 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7121 (initialize-new-tags-table))))))
7122
7123 (defvar cperl-tags-hier-regexp-list
7124 (concat
7125 "^\\("
7126 "\\(package\\)\\>"
7127 "\\|"
7128 "sub\\>[^\n]+::"
7129 "\\|"
7130 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7131 "\\|"
7132 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7133 "\\)"))
7134
7135 (defvar cperl-hierarchy '(() ())
7136 "Global hierarchy of classes.")
7137
7138 (defun cperl-tags-hier-fill ()
7139 ;; Suppose we are in a tag table cooked by cperl.
7140 (goto-char 1)
7141 (let (type pack name pos line chunk ord cons1 file str info fileind)
7142 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7143 (setq pos (match-beginning 0)
7144 pack (match-beginning 2))
7145 (beginning-of-line)
7146 (if (looking-at (concat
7147 "\\([^\n]+\\)"
7148 "\C-?"
7149 "\\([^\n]+\\)"
7150 "\C-a"
7151 "\\([0-9]+\\)"
7152 ","
7153 "\\([0-9]+\\)"))
7154 (progn
7155 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7156 name (buffer-substring (match-beginning 2) (match-end 2))
7157 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7158 line (buffer-substring (match-beginning 3) (match-end 3))
7159 ord (if pack 1 0)
7160 file (file-of-tag)
7161 fileind (format "%s:%s" file line)
7162 ;; Moves to beginning of the next line:
7163 info (cperl-etags-snarf-tag file line))
7164 ;; Move back
7165 (forward-char -1)
7166 ;; Make new member of hierarchy name ==> file ==> pos if needed
7167 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7168 ;; Name known
7169 (setcdr cons1 (cons (cons fileind (vector file info))
7170 (cdr cons1)))
7171 ;; First occurrence of the name, start alist
7172 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7173 (if pack
7174 (setcar (cdr cperl-hierarchy)
7175 (cons cons1 (nth 1 cperl-hierarchy)))
7176 (setcar cperl-hierarchy
7177 (cons cons1 (car cperl-hierarchy)))))))
7178 (end-of-line))))
7179
7180 (declare-function x-popup-menu "menu.c" (position menu))
7181
7182 (defun cperl-tags-hier-init (&optional update)
7183 "Show hierarchical menu of classes and methods.
7184 Finds info about classes by a scan of loaded TAGS files.
7185 Supposes that the TAGS files contain fully qualified function names.
7186 One may build such TAGS files from CPerl mode menu."
7187 (interactive)
7188 (require 'etags)
7189 (require 'imenu)
7190 (if (or update (null (nth 2 cperl-hierarchy)))
7191 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7192 (or (nthcdr 2 elt)
7193 ;; Only in one file
7194 (setcdr elt (cdr (nth 1 elt)))))))
7195 pack name cons1 to l1 l2 l3 l4 b)
7196 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7197 (setq cperl-hierarchy (list l1 l2 l3))
7198 (if (featurep 'xemacs) ; Not checked
7199 (progn
7200 (or tags-file-name
7201 ;; Does this work in XEmacs?
7202 (call-interactively 'visit-tags-table))
7203 (message "Updating list of classes...")
7204 (set-buffer (get-file-buffer tags-file-name))
7205 (cperl-tags-hier-fill))
7206 (or tags-table-list
7207 (call-interactively 'visit-tags-table))
7208 (mapc
7209 (function
7210 (lambda (tagsfile)
7211 (message "Updating list of classes... %s" tagsfile)
7212 (set-buffer (get-file-buffer tagsfile))
7213 (cperl-tags-hier-fill)))
7214 tags-table-list)
7215 (message "Updating list of classes... postprocessing..."))
7216 (mapc remover (car cperl-hierarchy))
7217 (mapc remover (nth 1 cperl-hierarchy))
7218 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7219 (cons "Methods: " (car cperl-hierarchy))))
7220 (cperl-tags-treeify to 1)
7221 (setcar (nthcdr 2 cperl-hierarchy)
7222 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7223 (message "Updating list of classes: done, requesting display...")
7224 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7225 ))
7226 (or (nth 2 cperl-hierarchy)
7227 (error "No items found"))
7228 (setq update
7229 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7230 (if (if (fboundp 'display-popup-menus-p)
7231 (let ((f 'display-popup-menus-p))
7232 (funcall f))
7233 window-system)
7234 (x-popup-menu t (nth 2 cperl-hierarchy))
7235 (require 'tmm)
7236 (tmm-prompt (nth 2 cperl-hierarchy))))
7237 (if (and update (listp update))
7238 (progn (while (cdr update) (setq update (cdr update)))
7239 (setq update (car update)))) ; Get the last from the list
7240 (if (vectorp update)
7241 (progn
7242 (find-file (elt update 0))
7243 (cperl-etags-goto-tag-location (elt update 1))))
7244 (if (eq update -999) (cperl-tags-hier-init t)))
7245
7246 (defun cperl-tags-treeify (to level)
7247 ;; cadr of `to' is read-write. On start it is a cons
7248 (let* ((regexp (concat "^\\(" (mapconcat
7249 'identity
7250 (make-list level "[_a-zA-Z0-9]+")
7251 "::")
7252 "\\)\\(::\\)?"))
7253 (packages (cdr (nth 1 to)))
7254 (methods (cdr (nth 2 to)))
7255 l1 head tail cons1 cons2 ord writeto packs recurse
7256 root-packages root-functions ms many_ms same_name ps
7257 (move-deeper
7258 (function
7259 (lambda (elt)
7260 (cond ((and (string-match regexp (car elt))
7261 (or (eq ord 1) (match-end 2)))
7262 (setq head (substring (car elt) 0 (match-end 1))
7263 tail (if (match-end 2) (substring (car elt)
7264 (match-end 2)))
7265 recurse t)
7266 (if (setq cons1 (assoc head writeto)) nil
7267 ;; Need to init new head
7268 (setcdr writeto (cons (list head (list "Packages: ")
7269 (list "Methods: "))
7270 (cdr writeto)))
7271 (setq cons1 (nth 1 writeto)))
7272 (setq cons2 (nth ord cons1)) ; Either packs or meths
7273 (setcdr cons2 (cons elt (cdr cons2))))
7274 ((eq ord 2)
7275 (setq root-functions (cons elt root-functions)))
7276 (t
7277 (setq root-packages (cons elt root-packages))))))))
7278 (setcdr to l1) ; Init to dynamic space
7279 (setq writeto to)
7280 (setq ord 1)
7281 (mapc move-deeper packages)
7282 (setq ord 2)
7283 (mapc move-deeper methods)
7284 (if recurse
7285 (mapc (function (lambda (elt)
7286 (cperl-tags-treeify elt (1+ level))))
7287 (cdr to)))
7288 ;;Now clean up leaders with one child only
7289 (mapc (function (lambda (elt)
7290 (if (not (and (listp (cdr elt))
7291 (eq (length elt) 2))) nil
7292 (setcar elt (car (nth 1 elt)))
7293 (setcdr elt (cdr (nth 1 elt))))))
7294 (cdr to))
7295 ;; Sort the roots of subtrees
7296 (if (default-value 'imenu-sort-function)
7297 (setcdr to
7298 (sort (cdr to) (default-value 'imenu-sort-function))))
7299 ;; Now add back functions removed from display
7300 (mapc (function (lambda (elt)
7301 (setcdr to (cons elt (cdr to)))))
7302 (if (default-value 'imenu-sort-function)
7303 (nreverse
7304 (sort root-functions (default-value 'imenu-sort-function)))
7305 root-functions))
7306 ;; Now add back packages removed from display
7307 (mapc (function (lambda (elt)
7308 (setcdr to (cons (cons (concat "package " (car elt))
7309 (cdr elt))
7310 (cdr to)))))
7311 (if (default-value 'imenu-sort-function)
7312 (nreverse
7313 (sort root-packages (default-value 'imenu-sort-function)))
7314 root-packages))))
7315
7316 ;;;(x-popup-menu t
7317 ;;; '(keymap "Name1"
7318 ;;; ("Ret1" "aa")
7319 ;;; ("Head1" "ab"
7320 ;;; keymap "Name2"
7321 ;;; ("Tail1" "x") ("Tail2" "y"))))
7322
7323 (defun cperl-list-fold (list name limit)
7324 (let (list1 list2 elt1 (num 0))
7325 (if (<= (length list) limit) list
7326 (setq list1 nil list2 nil)
7327 (while list
7328 (setq num (1+ num)
7329 elt1 (car list)
7330 list (cdr list))
7331 (if (<= num imenu-max-items)
7332 (setq list2 (cons elt1 list2))
7333 (setq list1 (cons (cons name
7334 (nreverse list2))
7335 list1)
7336 list2 (list elt1)
7337 num 1)))
7338 (nreverse (cons (cons name
7339 (nreverse list2))
7340 list1)))))
7341
7342 (defun cperl-menu-to-keymap (menu &optional name)
7343 (let (list)
7344 (cons 'keymap
7345 (mapcar
7346 (function
7347 (lambda (elt)
7348 (cond ((listp (cdr elt))
7349 (setq list (cperl-list-fold
7350 (cdr elt) (car elt) imenu-max-items))
7351 (cons nil
7352 (cons (car elt)
7353 (cperl-menu-to-keymap list))))
7354 (t
7355 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7356 (cperl-list-fold menu "Root" imenu-max-items)))))
7357
7358 \f
7359 (defvar cperl-bad-style-regexp
7360 (mapconcat 'identity
7361 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7362 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7363 "\\|")
7364 "Finds places such that insertion of a whitespace may help a lot.")
7365
7366 (defvar cperl-not-bad-style-regexp
7367 (mapconcat
7368 'identity
7369 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7370 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7371 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7372 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7373 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7374 "-[0-9]" ; -5
7375 "\\+\\+" ; ++var
7376 "--" ; --var
7377 ".->" ; a->b
7378 "->" ; a SPACE ->b
7379 "\\[-" ; a[-1]
7380 "\\\\[&$@*\\\\]" ; \&func
7381 "^=" ; =head
7382 "\\$." ; $|
7383 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7384 "||"
7385 "&&"
7386 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7387 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7388 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7389 ;;"[*/+-|&<.]+="
7390 )
7391 "\\|")
7392 "If matches at the start of match found by `my-bad-c-style-regexp',
7393 insertion of a whitespace will not help.")
7394
7395 (defvar found-bad)
7396
7397 (defun cperl-find-bad-style ()
7398 "Find places in the buffer where insertion of a whitespace may help.
7399 Prompts user for insertion of spaces.
7400 Currently it is tuned to C and Perl syntax."
7401 (interactive)
7402 (let (found-bad (p (point)))
7403 (setq last-nonmenu-event 13) ; To disable popup
7404 (goto-char (point-min))
7405 (map-y-or-n-p "Insert space here? "
7406 (lambda (arg) (insert " "))
7407 'cperl-next-bad-style
7408 '("location" "locations" "insert a space into")
7409 '((?\C-r (lambda (arg)
7410 (let ((buffer-quit-function
7411 'exit-recursive-edit))
7412 (message "Exit with Esc Esc")
7413 (recursive-edit)
7414 t)) ; Consider acted upon
7415 "edit, exit with Esc Esc")
7416 (?e (lambda (arg)
7417 (let ((buffer-quit-function
7418 'exit-recursive-edit))
7419 (message "Exit with Esc Esc")
7420 (recursive-edit)
7421 t)) ; Consider acted upon
7422 "edit, exit with Esc Esc"))
7423 t)
7424 (if found-bad (goto-char found-bad)
7425 (goto-char p)
7426 (message "No appropriate place found"))))
7427
7428 (defun cperl-next-bad-style ()
7429 (let (p (not-found t) (point (point)) found)
7430 (while (and not-found
7431 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7432 (setq p (point))
7433 (goto-char (match-beginning 0))
7434 (if (or
7435 (looking-at cperl-not-bad-style-regexp)
7436 ;; Check for a < -b and friends
7437 (and (eq (following-char) ?\-)
7438 (save-excursion
7439 (skip-chars-backward " \t\n")
7440 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7441 ;; Now check for syntax type
7442 (save-match-data
7443 (setq found (point))
7444 (beginning-of-defun)
7445 (let ((pps (parse-partial-sexp (point) found)))
7446 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7447 (goto-char (match-end 0))
7448 (goto-char (1- p))
7449 (setq not-found nil
7450 found-bad found)))
7451 (not not-found)))
7452
7453 \f
7454 ;;; Getting help
7455 (defvar cperl-have-help-regexp
7456 ;;(concat "\\("
7457 (mapconcat
7458 'identity
7459 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7460 "[$@]\\^[a-zA-Z]" ; Special variable
7461 "[$@][^ \n\t]" ; Special variable
7462 "-[a-zA-Z]" ; File test
7463 "\\\\[a-zA-Z0]" ; Special chars
7464 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7465 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7466 "[a-zA-Z_0-9:]+" ; symbol or number
7467 "x="
7468 "#!")
7469 ;;"\\)\\|\\("
7470 "\\|")
7471 ;;"\\)"
7472 ;;)
7473 "Matches places in the buffer we can find help for.")
7474
7475 (defvar cperl-message-on-help-error t)
7476 (defvar cperl-help-from-timer nil)
7477
7478 (defun cperl-word-at-point-hard ()
7479 ;; Does not save-excursion
7480 ;; Get to the something meaningful
7481 (or (eobp) (eolp) (forward-char 1))
7482 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7483 (save-excursion (beginning-of-line) (point))
7484 'to-beg)
7485 ;; (cond
7486 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7487 ;; (skip-chars-backward " \n\t\r({[]});,")
7488 ;; (or (bobp) (backward-char 1))))
7489 ;; Try to backtrace
7490 (cond
7491 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7492 (skip-chars-backward "a-zA-Z0-9_:")
7493 (cond
7494 ((and (eq (preceding-char) ?^) ; $^I
7495 (eq (char-after (- (point) 2)) ?\$))
7496 (forward-char -2))
7497 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7498 (forward-char -1))
7499 ((and (eq (preceding-char) ?\=)
7500 (eq (current-column) 1))
7501 (forward-char -1))) ; =head1
7502 (if (and (eq (preceding-char) ?\<)
7503 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7504 (forward-char -1)))
7505 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7506 (forward-char -1))
7507 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7508 (forward-char -1))
7509 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7510 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7511 (cond
7512 ((and (eq (preceding-char) ?\$)
7513 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7514 (forward-char -1))
7515 ((and (eq (following-char) ?\>)
7516 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7517 (save-excursion
7518 (forward-sexp -1)
7519 (and (eq (preceding-char) ?\<)
7520 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7521 (search-backward "<"))))
7522 ((and (eq (following-char) ?\$)
7523 (eq (preceding-char) ?\<)
7524 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7525 (forward-char -1)))
7526 (if (looking-at cperl-have-help-regexp)
7527 (buffer-substring (match-beginning 0) (match-end 0))))
7528
7529 (defun cperl-get-help ()
7530 "Get one-line docs on the symbol at the point.
7531 The data for these docs is a little bit obsolete and may be in fact longer
7532 than a line. Your contribution to update/shorten it is appreciated."
7533 (interactive)
7534 (save-match-data ; May be called "inside" query-replace
7535 (save-excursion
7536 (let ((word (cperl-word-at-point-hard)))
7537 (if word
7538 (if (and cperl-help-from-timer ; Bail out if not in mainland
7539 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7540 (or (memq (get-text-property (point) 'face)
7541 '(font-lock-comment-face font-lock-string-face))
7542 (memq (get-text-property (point) 'syntax-type)
7543 '(pod here-doc format))))
7544 nil
7545 (cperl-describe-perl-symbol word))
7546 (if cperl-message-on-help-error
7547 (message "Nothing found for %s..."
7548 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7549
7550 ;;; Stolen from perl-descr.el by Johan Vromans:
7551
7552 (defvar cperl-doc-buffer " *perl-doc*"
7553 "Where the documentation can be found.")
7554
7555 (defun cperl-describe-perl-symbol (val)
7556 "Display the documentation of symbol at point, a Perl operator."
7557 (let ((enable-recursive-minibuffers t)
7558 args-file regexp)
7559 (cond
7560 ((string-match "^[&*][a-zA-Z_]" val)
7561 (setq val (concat (substring val 0 1) "NAME")))
7562 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7563 (setq val (concat "@" (substring val 1 (match-end 1)))))
7564 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7565 (setq val (concat "%" (substring val 1 (match-end 1)))))
7566 ((and (string= val "x") (string-match "^x=" val))
7567 (setq val "x="))
7568 ((string-match "^\\$[\C-a-\C-z]" val)
7569 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7570 ((string-match "^CORE::" val)
7571 (setq val "CORE::"))
7572 ((string-match "^SUPER::" val)
7573 (setq val "SUPER::"))
7574 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7575 (setq val "<NAME>")))
7576 (setq regexp (concat "^"
7577 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7578 (regexp-quote val)
7579 "\\([ \t([/]\\|$\\)"))
7580
7581 ;; get the buffer with the documentation text
7582 (cperl-switch-to-doc-buffer)
7583
7584 ;; lookup in the doc
7585 (goto-char (point-min))
7586 (let ((case-fold-search nil))
7587 (list
7588 (if (re-search-forward regexp (point-max) t)
7589 (save-excursion
7590 (beginning-of-line 1)
7591 (let ((lnstart (point)))
7592 (end-of-line)
7593 (message "%s" (buffer-substring lnstart (point)))))
7594 (if cperl-message-on-help-error
7595 (message "No definition for %s" val)))))))
7596
7597 (defvar cperl-short-docs 'please-ignore-this-line
7598 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7599 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7600 ... Range (list context); flip/flop [no flop when flip] (scalar context).
7601 ! ... Logical negation.
7602 ... != ... Numeric inequality.
7603 ... !~ ... Search pattern, substitution, or translation (negated).
7604 $! In numeric context: errno. In a string context: error string.
7605 $\" The separator which joins elements of arrays interpolated in strings.
7606 $# The output format for printed numbers. Default is %.15g or close.
7607 $$ Process number of this script. Changes in the fork()ed child process.
7608 $% The current page number of the currently selected output channel.
7609
7610 The following variables are always local to the current block:
7611
7612 $1 Match of the 1st set of parentheses in the last match (auto-local).
7613 $2 Match of the 2nd set of parentheses in the last match (auto-local).
7614 $3 Match of the 3rd set of parentheses in the last match (auto-local).
7615 $4 Match of the 4th set of parentheses in the last match (auto-local).
7616 $5 Match of the 5th set of parentheses in the last match (auto-local).
7617 $6 Match of the 6th set of parentheses in the last match (auto-local).
7618 $7 Match of the 7th set of parentheses in the last match (auto-local).
7619 $8 Match of the 8th set of parentheses in the last match (auto-local).
7620 $9 Match of the 9th set of parentheses in the last match (auto-local).
7621 $& The string matched by the last pattern match (auto-local).
7622 $' The string after what was matched by the last match (auto-local).
7623 $` The string before what was matched by the last match (auto-local).
7624
7625 $( The real gid of this process.
7626 $) The effective gid of this process.
7627 $* Deprecated: Set to 1 to do multiline matching within a string.
7628 $+ The last bracket matched by the last search pattern.
7629 $, The output field separator for the print operator.
7630 $- The number of lines left on the page.
7631 $. The current input line number of the last filehandle that was read.
7632 $/ The input record separator, newline by default.
7633 $0 Name of the file containing the current perl script (read/write).
7634 $: String may be broken after these characters to fill ^-lines in a format.
7635 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
7636 $< The real uid of this process.
7637 $= The page length of the current output channel. Default is 60 lines.
7638 $> The effective uid of this process.
7639 $? The status returned by the last ``, pipe close or `system'.
7640 $@ The perl error message from the last eval or do @var{EXPR} command.
7641 $ARGV The name of the current file used with <> .
7642 $[ Deprecated: The index of the first element/char in an array/string.
7643 $\\ The output record separator for the print operator.
7644 $] The perl version string as displayed with perl -v.
7645 $^ The name of the current top-of-page format.
7646 $^A The current value of the write() accumulator for format() lines.
7647 $^D The value of the perl debug (-D) flags.
7648 $^E Information about the last system error other than that provided by $!.
7649 $^F The highest system file descriptor, ordinarily 2.
7650 $^H The current set of syntax checks enabled by `use strict'.
7651 $^I The value of the in-place edit extension (perl -i option).
7652 $^L What formats output to perform a formfeed. Default is \\f.
7653 $^M A buffer for emergency memory allocation when running out of memory.
7654 $^O The operating system name under which this copy of Perl was built.
7655 $^P Internal debugging flag.
7656 $^T The time the script was started. Used by -A/-M/-C file tests.
7657 $^W True if warnings are requested (perl -w flag).
7658 $^X The name under which perl was invoked (argv[0] in C-speech).
7659 $_ The default input and pattern-searching space.
7660 $| Auto-flush after write/print on current output channel? Default 0.
7661 $~ The name of the current report format.
7662 ... % ... Modulo division.
7663 ... %= ... Modulo division assignment.
7664 %ENV Contains the current environment.
7665 %INC List of files that have been require-d or do-ne.
7666 %SIG Used to set signal handlers for various signals.
7667 ... & ... Bitwise and.
7668 ... && ... Logical and.
7669 ... &&= ... Logical and assignment.
7670 ... &= ... Bitwise and assignment.
7671 ... * ... Multiplication.
7672 ... ** ... Exponentiation.
7673 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7674 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
7675 ... + ... Addition. +EXPR Makes EXPR into scalar context.
7676 ++ Auto-increment (magical on strings). ++EXPR EXPR++
7677 ... += ... Addition assignment.
7678 , Comma operator.
7679 ... - ... Subtraction.
7680 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7681 ... -= ... Subtraction assignment.
7682 -A Access time in days since script started.
7683 -B File is a non-text (binary) file.
7684 -C Inode change time in days since script started.
7685 -M Age in days since script started.
7686 -O File is owned by real uid.
7687 -R File is readable by real uid.
7688 -S File is a socket .
7689 -T File is a text file.
7690 -W File is writable by real uid.
7691 -X File is executable by real uid.
7692 -b File is a block special file.
7693 -c File is a character special file.
7694 -d File is a directory.
7695 -e File exists .
7696 -f File is a plain file.
7697 -g File has setgid bit set.
7698 -k File has sticky bit set.
7699 -l File is a symbolic link.
7700 -o File is owned by effective uid.
7701 -p File is a named pipe (FIFO).
7702 -r File is readable by effective uid.
7703 -s File has non-zero size.
7704 -t Tests if filehandle (STDIN by default) is opened to a tty.
7705 -u File has setuid bit set.
7706 -w File is writable by effective uid.
7707 -x File is executable by effective uid.
7708 -z File has zero size.
7709 . Concatenate strings.
7710 .. Range (list context); flip/flop (scalar context) operator.
7711 .= Concatenate assignment strings
7712 ... / ... Division. /PATTERN/ioxsmg Pattern match
7713 ... /= ... Division assignment.
7714 /PATTERN/ioxsmg Pattern match.
7715 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7716 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7717 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7718 <> Reads line from union of files in @ARGV (= command line) and STDIN.
7719 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7720 ... <= ... Numeric less than or equal to.
7721 ... <=> ... Numeric compare.
7722 ... = ... Assignment.
7723 ... == ... Numeric equality.
7724 ... =~ ... Search pattern, substitution, or translation
7725 ... > ... Numeric greater than.
7726 ... >= ... Numeric greater than or equal to.
7727 ... >> ... Bitwise shift right.
7728 ... >>= ... Bitwise shift right assignment.
7729 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7730 ?PATTERN? One-time pattern match.
7731 @ARGV Command line arguments (not including the command name - see $0).
7732 @INC List of places to look for perl scripts during do/include/use.
7733 @_ Parameter array for subroutines; result of split() unless in list context.
7734 \\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7735 \\0 Octal char, e.g. \\033.
7736 \\E Case modification terminator. See \\Q, \\L, and \\U.
7737 \\L Lowercase until \\E . See also \\l, lc.
7738 \\U Upcase until \\E . See also \\u, uc.
7739 \\Q Quote metacharacters until \\E . See also quotemeta.
7740 \\a Alarm character (octal 007).
7741 \\b Backspace character (octal 010).
7742 \\c Control character, e.g. \\c[ .
7743 \\e Escape character (octal 033).
7744 \\f Formfeed character (octal 014).
7745 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
7746 \\n Newline character (octal 012 on most systems).
7747 \\r Return character (octal 015 on most systems).
7748 \\t Tab character (octal 011).
7749 \\u Upcase the next character. See also \\U and \\l, ucfirst.
7750 \\x Hex character, e.g. \\x1b.
7751 ... ^ ... Bitwise exclusive or.
7752 __END__ Ends program source.
7753 __DATA__ Ends program source.
7754 __FILE__ Current (source) filename.
7755 __LINE__ Current line in current source.
7756 __PACKAGE__ Current package.
7757 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7758 ARGVOUT Output filehandle with -i flag.
7759 BEGIN { ... } Immediately executed (during compilation) piece of code.
7760 END { ... } Pseudo-subroutine executed after the script finishes.
7761 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7762 INIT { ... } Pseudo-subroutine executed before the script starts running.
7763 DATA Input filehandle for what follows after __END__ or __DATA__.
7764 accept(NEWSOCKET,GENERICSOCKET)
7765 alarm(SECONDS)
7766 atan2(X,Y)
7767 bind(SOCKET,NAME)
7768 binmode(FILEHANDLE)
7769 caller[(LEVEL)]
7770 chdir(EXPR)
7771 chmod(LIST)
7772 chop[(LIST|VAR)]
7773 chown(LIST)
7774 chroot(FILENAME)
7775 close(FILEHANDLE)
7776 closedir(DIRHANDLE)
7777 ... cmp ... String compare.
7778 connect(SOCKET,NAME)
7779 continue of { block } continue { block }. Is executed after `next' or at end.
7780 cos(EXPR)
7781 crypt(PLAINTEXT,SALT)
7782 dbmclose(%HASH)
7783 dbmopen(%HASH,DBNAME,MODE)
7784 defined(EXPR)
7785 delete($HASH{KEY})
7786 die(LIST)
7787 do { ... }|SUBR while|until EXPR executes at least once
7788 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7789 dump LABEL
7790 each(%HASH)
7791 endgrent
7792 endhostent
7793 endnetent
7794 endprotoent
7795 endpwent
7796 endservent
7797 eof[([FILEHANDLE])]
7798 ... eq ... String equality.
7799 eval(EXPR) or eval { BLOCK }
7800 exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7801 exit(EXPR)
7802 exp(EXPR)
7803 fcntl(FILEHANDLE,FUNCTION,SCALAR)
7804 fileno(FILEHANDLE)
7805 flock(FILEHANDLE,OPERATION)
7806 for (EXPR;EXPR;EXPR) { ... }
7807 foreach [VAR] (@ARRAY) { ... }
7808 fork
7809 ... ge ... String greater than or equal.
7810 getc[(FILEHANDLE)]
7811 getgrent
7812 getgrgid(GID)
7813 getgrnam(NAME)
7814 gethostbyaddr(ADDR,ADDRTYPE)
7815 gethostbyname(NAME)
7816 gethostent
7817 getlogin
7818 getnetbyaddr(ADDR,ADDRTYPE)
7819 getnetbyname(NAME)
7820 getnetent
7821 getpeername(SOCKET)
7822 getpgrp(PID)
7823 getppid
7824 getpriority(WHICH,WHO)
7825 getprotobyname(NAME)
7826 getprotobynumber(NUMBER)
7827 getprotoent
7828 getpwent
7829 getpwnam(NAME)
7830 getpwuid(UID)
7831 getservbyname(NAME,PROTO)
7832 getservbyport(PORT,PROTO)
7833 getservent
7834 getsockname(SOCKET)
7835 getsockopt(SOCKET,LEVEL,OPTNAME)
7836 gmtime(EXPR)
7837 goto LABEL
7838 ... gt ... String greater than.
7839 hex(EXPR)
7840 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7841 index(STR,SUBSTR[,OFFSET])
7842 int(EXPR)
7843 ioctl(FILEHANDLE,FUNCTION,SCALAR)
7844 join(EXPR,LIST)
7845 keys(%HASH)
7846 kill(LIST)
7847 last [LABEL]
7848 ... le ... String less than or equal.
7849 length(EXPR)
7850 link(OLDFILE,NEWFILE)
7851 listen(SOCKET,QUEUESIZE)
7852 local(LIST)
7853 localtime(EXPR)
7854 log(EXPR)
7855 lstat(EXPR|FILEHANDLE|VAR)
7856 ... lt ... String less than.
7857 m/PATTERN/iogsmx
7858 mkdir(FILENAME,MODE)
7859 msgctl(ID,CMD,ARG)
7860 msgget(KEY,FLAGS)
7861 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7862 msgsnd(ID,MSG,FLAGS)
7863 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7864 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7865 ... ne ... String inequality.
7866 next [LABEL]
7867 oct(EXPR)
7868 open(FILEHANDLE[,EXPR])
7869 opendir(DIRHANDLE,EXPR)
7870 ord(EXPR) ASCII value of the first char of the string.
7871 pack(TEMPLATE,LIST)
7872 package NAME Introduces package context.
7873 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7874 pop(ARRAY)
7875 print [FILEHANDLE] [(LIST)]
7876 printf [FILEHANDLE] (FORMAT,LIST)
7877 push(ARRAY,LIST)
7878 q/STRING/ Synonym for 'STRING'
7879 qq/STRING/ Synonym for \"STRING\"
7880 qx/STRING/ Synonym for `STRING`
7881 rand[(EXPR)]
7882 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7883 readdir(DIRHANDLE)
7884 readlink(EXPR)
7885 recv(SOCKET,SCALAR,LEN,FLAGS)
7886 redo [LABEL]
7887 rename(OLDNAME,NEWNAME)
7888 require [FILENAME | PERL_VERSION]
7889 reset[(EXPR)]
7890 return(LIST)
7891 reverse(LIST)
7892 rewinddir(DIRHANDLE)
7893 rindex(STR,SUBSTR[,OFFSET])
7894 rmdir(FILENAME)
7895 s/PATTERN/REPLACEMENT/gieoxsm
7896 scalar(EXPR)
7897 seek(FILEHANDLE,POSITION,WHENCE)
7898 seekdir(DIRHANDLE,POS)
7899 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7900 semctl(ID,SEMNUM,CMD,ARG)
7901 semget(KEY,NSEMS,SIZE,FLAGS)
7902 semop(KEY,...)
7903 send(SOCKET,MSG,FLAGS[,TO])
7904 setgrent
7905 sethostent(STAYOPEN)
7906 setnetent(STAYOPEN)
7907 setpgrp(PID,PGRP)
7908 setpriority(WHICH,WHO,PRIORITY)
7909 setprotoent(STAYOPEN)
7910 setpwent
7911 setservent(STAYOPEN)
7912 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7913 shift[(ARRAY)]
7914 shmctl(ID,CMD,ARG)
7915 shmget(KEY,SIZE,FLAGS)
7916 shmread(ID,VAR,POS,SIZE)
7917 shmwrite(ID,STRING,POS,SIZE)
7918 shutdown(SOCKET,HOW)
7919 sin(EXPR)
7920 sleep[(EXPR)]
7921 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7922 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7923 sort [SUBROUTINE] (LIST)
7924 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7925 split[(/PATTERN/[,EXPR[,LIMIT]])]
7926 sprintf(FORMAT,LIST)
7927 sqrt(EXPR)
7928 srand(EXPR)
7929 stat(EXPR|FILEHANDLE|VAR)
7930 study[(SCALAR)]
7931 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7932 substr(EXPR,OFFSET[,LEN])
7933 symlink(OLDFILE,NEWFILE)
7934 syscall(LIST)
7935 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7936 system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7937 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7938 tell[(FILEHANDLE)]
7939 telldir(DIRHANDLE)
7940 time
7941 times
7942 tr/SEARCHLIST/REPLACEMENTLIST/cds
7943 truncate(FILE|EXPR,LENGTH)
7944 umask[(EXPR)]
7945 undef[(EXPR)]
7946 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7947 unlink(LIST)
7948 unpack(TEMPLATE,EXPR)
7949 unshift(ARRAY,LIST)
7950 until (EXPR) { ... } EXPR until EXPR
7951 utime(LIST)
7952 values(%HASH)
7953 vec(EXPR,OFFSET,BITS)
7954 wait
7955 waitpid(PID,FLAGS)
7956 wantarray Returns true if the sub/eval is called in list context.
7957 warn(LIST)
7958 while (EXPR) { ... } EXPR while EXPR
7959 write[(EXPR|FILEHANDLE)]
7960 ... x ... Repeat string or array.
7961 x= ... Repetition assignment.
7962 y/SEARCHLIST/REPLACEMENTLIST/
7963 ... | ... Bitwise or.
7964 ... || ... Logical or.
7965 ~ ... Unary bitwise complement.
7966 #! OS interpreter indicator. If contains `perl', used for options, and -x.
7967 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7968 CORE:: Prefix to access builtin function if imported sub obscures it.
7969 SUPER:: Prefix to lookup for a method in @ISA classes.
7970 DESTROY Shorthand for `sub DESTROY {...}'.
7971 ... EQ ... Obsolete synonym of `eq'.
7972 ... GE ... Obsolete synonym of `ge'.
7973 ... GT ... Obsolete synonym of `gt'.
7974 ... LE ... Obsolete synonym of `le'.
7975 ... LT ... Obsolete synonym of `lt'.
7976 ... NE ... Obsolete synonym of `ne'.
7977 abs [ EXPR ] absolute value
7978 ... and ... Low-precedence synonym for &&.
7979 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7980 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
7981 chr Converts a number to char with the same ordinal.
7982 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7983 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7984 exists $HASH{KEY} True if the key exists.
7985 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7986 formline PICTURE, LIST Backdoor into \"format\" processing.
7987 glob EXPR Synonym of <EXPR>.
7988 lc [ EXPR ] Returns lowercased EXPR.
7989 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
7990 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
7991 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
7992 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
7993 not ... Low-precedence synonym for ! - negation.
7994 ... or ... Low-precedence synonym for ||.
7995 pos STRING Set/Get end-position of the last match over this string, see \\G.
7996 quotemeta [ EXPR ] Quote regexp metacharacters.
7997 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
7998 readline FH Synonym of <FH>.
7999 readpipe CMD Synonym of `CMD`.
8000 ref [ EXPR ] Type of EXPR when dereferenced.
8001 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
8002 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
8003 tied Returns internal object for a tied data.
8004 uc [ EXPR ] Returns upcased EXPR.
8005 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
8006 untie VAR Unlink an object from a simple Perl variable.
8007 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
8008 ... xor ... Low-precedence synonym for exclusive or.
8009 prototype \\&SUB Returns prototype of the function given a reference.
8010 =head1 Top-level heading.
8011 =head2 Second-level heading.
8012 =head3 Third-level heading (is there such?).
8013 =over [ NUMBER ] Start list.
8014 =item [ TITLE ] Start new item in the list.
8015 =back End list.
8016 =cut Switch from POD to Perl.
8017 =pod Switch from Perl to POD.
8018 ")
8019
8020 (defun cperl-switch-to-doc-buffer (&optional interactive)
8021 "Go to the perl documentation buffer and insert the documentation."
8022 (interactive "p")
8023 (let ((buf (get-buffer-create cperl-doc-buffer)))
8024 (if interactive
8025 (switch-to-buffer-other-window buf)
8026 (set-buffer buf))
8027 (if (= (buffer-size) 0)
8028 (progn
8029 (insert (documentation-property 'cperl-short-docs
8030 'variable-documentation))
8031 (setq buffer-read-only t)))))
8032
8033 (defun cperl-beautify-regexp-piece (b e embed level)
8034 ;; b is before the starting delimiter, e before the ending
8035 ;; e should be a marker, may be changed, but remains "correct".
8036 ;; EMBED is nil if we process the whole REx.
8037 ;; The REx is guaranteed to have //x
8038 ;; LEVEL shows how many levels deep to go
8039 ;; position at enter and at leave is not defined
8040 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
8041 (if embed
8042 (progn
8043 (goto-char b)
8044 (setq c (if (eq embed t) (current-indentation) (current-column)))
8045 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
8046 (forward-char 2)
8047 (delete-char 1)
8048 (forward-char 1))
8049 ((looking-at "(\\?[^a-zA-Z]")
8050 (forward-char 3))
8051 ((looking-at "(\\?") ; (?i)
8052 (forward-char 2))
8053 (t
8054 (forward-char 1))))
8055 (goto-char (1+ b))
8056 (setq c (1- (current-column))))
8057 (setq c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
8058 (or (looking-at "[ \t]*[\n#]")
8059 (progn
8060 (insert "\n")))
8061 (goto-char e)
8062 (beginning-of-line)
8063 (if (re-search-forward "[^ \t]" e t)
8064 (progn ; Something before the ending delimiter
8065 (goto-char e)
8066 (delete-horizontal-space)
8067 (insert "\n")
8068 (cperl-make-indent c)
8069 (set-marker e (point))))
8070 (goto-char b)
8071 (end-of-line 2)
8072 (while (< (point) (marker-position e))
8073 (beginning-of-line)
8074 (setq s (point)
8075 inline t)
8076 (skip-chars-forward " \t")
8077 (delete-region s (point))
8078 (cperl-make-indent c1)
8079 (while (and
8080 inline
8081 (looking-at
8082 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8083 "\\|" ; Embedded variable
8084 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8085 "\\|" ; $ ^
8086 "[$^]"
8087 "\\|" ; simple-code simple-code*?
8088 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8089 "\\|" ; Class
8090 "\\(\\[\\)" ; 6
8091 "\\|" ; Grouping
8092 "\\((\\(\\?\\)?\\)" ; 7 8
8093 "\\|" ; |
8094 "\\(|\\)"))) ; 9
8095 (goto-char (match-end 0))
8096 (setq spaces t)
8097 (cond ((match-beginning 1) ; Alphanum word + junk
8098 (forward-char -1))
8099 ((or (match-beginning 3) ; $ab[12]
8100 (and (match-beginning 5) ; X* X+ X{2,3}
8101 (eq (preceding-char) ?\{)))
8102 (forward-char -1)
8103 (forward-sexp 1))
8104 ((and ; [], already syntaxified
8105 (match-beginning 6)
8106 cperl-regexp-scan
8107 cperl-use-syntax-table-text-property)
8108 (forward-char -1)
8109 (forward-sexp 1)
8110 (or (eq (preceding-char) ?\])
8111 (error "[]-group not terminated"))
8112 (re-search-forward
8113 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8114 ((match-beginning 6) ; []
8115 (setq tmp (point))
8116 (if (looking-at "\\^?\\]")
8117 (goto-char (match-end 0)))
8118 ;; XXXX POSIX classes?!
8119 (while (and (not pos)
8120 (re-search-forward "\\[:\\|\\]" e t))
8121 (if (eq (preceding-char) ?:)
8122 (or (re-search-forward ":\\]" e t)
8123 (error "[:POSIX:]-group in []-group not terminated"))
8124 (setq pos t)))
8125 (or (eq (preceding-char) ?\])
8126 (error "[]-group not terminated"))
8127 (re-search-forward
8128 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8129 ((match-beginning 7) ; ()
8130 (goto-char (match-beginning 0))
8131 (setq pos (current-column))
8132 (or (eq pos c1)
8133 (progn
8134 (delete-horizontal-space)
8135 (insert "\n")
8136 (cperl-make-indent c1)))
8137 (setq tmp (point))
8138 (forward-sexp 1)
8139 ;; (or (forward-sexp 1)
8140 ;; (progn
8141 ;; (goto-char tmp)
8142 ;; (error "()-group not terminated")))
8143 (set-marker m (1- (point)))
8144 (set-marker m1 (point))
8145 (if (= level 1)
8146 (if (progn ; indent rigidly if multiline
8147 ;; In fact does not make a lot of sense, since
8148 ;; the starting position can be already lost due
8149 ;; to insertion of "\n" and " "
8150 (goto-char tmp)
8151 (search-forward "\n" m1 t))
8152 (indent-rigidly (point) m1 (- c1 pos)))
8153 (setq level (1- level))
8154 (cond
8155 ((not (match-beginning 8))
8156 (cperl-beautify-regexp-piece tmp m t level))
8157 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8158 t)
8159 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8160 (goto-char (+ 2 tmp))
8161 (forward-sexp 1)
8162 (cperl-beautify-regexp-piece (point) m t level))
8163 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8164 (goto-char (+ 3 tmp))
8165 (cperl-beautify-regexp-piece (point) m t level))
8166 (t
8167 (cperl-beautify-regexp-piece tmp m t level))))
8168 (goto-char m1)
8169 (cond ((looking-at "[*+?]\\??")
8170 (goto-char (match-end 0)))
8171 ((eq (following-char) ?\{)
8172 (forward-sexp 1)
8173 (if (eq (following-char) ?\?)
8174 (forward-char))))
8175 (skip-chars-forward " \t")
8176 (setq spaces nil)
8177 (if (looking-at "[#\n]")
8178 (progn
8179 (or (eolp) (indent-for-comment))
8180 (beginning-of-line 2))
8181 (delete-horizontal-space)
8182 (insert "\n"))
8183 (end-of-line)
8184 (setq inline nil))
8185 ((match-beginning 9) ; |
8186 (forward-char -1)
8187 (setq tmp (point))
8188 (beginning-of-line)
8189 (if (re-search-forward "[^ \t]" tmp t)
8190 (progn
8191 (goto-char tmp)
8192 (delete-horizontal-space)
8193 (insert "\n"))
8194 ;; first at line
8195 (delete-region (point) tmp))
8196 (cperl-make-indent c)
8197 (forward-char 1)
8198 (skip-chars-forward " \t")
8199 (setq spaces nil)
8200 (if (looking-at "[#\n]")
8201 (beginning-of-line 2)
8202 (delete-horizontal-space)
8203 (insert "\n"))
8204 (end-of-line)
8205 (setq inline nil)))
8206 (or (looking-at "[ \t\n]")
8207 (not spaces)
8208 (insert " "))
8209 (skip-chars-forward " \t"))
8210 (or (looking-at "[#\n]")
8211 (error "Unknown code `%s' in a regexp"
8212 (buffer-substring (point) (1+ (point)))))
8213 (and inline (end-of-line 2)))
8214 ;; Special-case the last line of group
8215 (if (and (>= (point) (marker-position e))
8216 (/= (current-indentation) c))
8217 (progn
8218 (beginning-of-line)
8219 (cperl-make-indent c)))))
8220
8221 (defun cperl-make-regexp-x ()
8222 ;; Returns position of the start
8223 ;; XXX this is called too often! Need to cache the result!
8224 (save-excursion
8225 (or cperl-use-syntax-table-text-property
8226 (error "I need to have a regexp marked!"))
8227 ;; Find the start
8228 (if (looking-at "\\s|")
8229 nil ; good already
8230 (if (or (looking-at "\\([smy]\\|qr\\)\\s|")
8231 (and (eq (preceding-char) ?q)
8232 (looking-at "\\(r\\)\\s|")))
8233 (goto-char (match-end 1))
8234 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8235 ;;(forward-char 1)
8236 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8237 (sub-p (eq (preceding-char) ?s)) s)
8238 (forward-sexp 1)
8239 (set-marker e (1- (point)))
8240 (setq delim (preceding-char))
8241 (if (and sub-p (eq delim (char-after (- (point) 2))))
8242 (error "Possible s/blah// - do not know how to deal with"))
8243 (if sub-p (forward-sexp 1))
8244 (if (looking-at "\\sw*x")
8245 (setq have-x t)
8246 (insert "x"))
8247 ;; Protect fragile " ", "#"
8248 (if have-x nil
8249 (goto-char (1+ b))
8250 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8251 (forward-char -1)
8252 (insert "\\")
8253 (forward-char 1)))
8254 b)))
8255
8256 (defun cperl-beautify-regexp (&optional deep)
8257 "Do it. (Experimental, may change semantics, recheck the result.)
8258 We suppose that the regexp is scanned already."
8259 (interactive "P")
8260 (setq deep (if deep (prefix-numeric-value deep) -1))
8261 (save-excursion
8262 (goto-char (cperl-make-regexp-x))
8263 (let ((b (point)) (e (make-marker)))
8264 (forward-sexp 1)
8265 (set-marker e (1- (point)))
8266 (cperl-beautify-regexp-piece b e nil deep))))
8267
8268 (defun cperl-regext-to-level-start ()
8269 "Goto start of an enclosing group in regexp.
8270 We suppose that the regexp is scanned already."
8271 (interactive)
8272 (let ((limit (cperl-make-regexp-x)) done)
8273 (while (not done)
8274 (or (eq (following-char) ?\()
8275 (search-backward "(" (1+ limit) t)
8276 (error "Cannot find `(' which starts a group"))
8277 (setq done
8278 (save-excursion
8279 (skip-chars-backward "\\")
8280 (looking-at "\\(\\\\\\\\\\)*(")))
8281 (or done (forward-char -1)))))
8282
8283 (defun cperl-contract-level ()
8284 "Find an enclosing group in regexp and contract it.
8285 \(Experimental, may change semantics, recheck the result.)
8286 We suppose that the regexp is scanned already."
8287 (interactive)
8288 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8289 (cperl-regext-to-level-start)
8290 (let ((b (point)) (e (make-marker)) c)
8291 (forward-sexp 1)
8292 (set-marker e (1- (point)))
8293 (goto-char b)
8294 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8295 (cond
8296 ((match-beginning 1) ; #-comment
8297 (or c (setq c (current-indentation)))
8298 (beginning-of-line 2) ; Skip
8299 (cperl-make-indent c))
8300 (t
8301 (delete-char -1)
8302 (just-one-space))))))
8303
8304 (defun cperl-contract-levels ()
8305 "Find an enclosing group in regexp and contract all the kids.
8306 \(Experimental, may change semantics, recheck the result.)
8307 We suppose that the regexp is scanned already."
8308 (interactive)
8309 (save-excursion
8310 (condition-case nil
8311 (cperl-regext-to-level-start)
8312 (error ; We are outside outermost group
8313 (goto-char (cperl-make-regexp-x))))
8314 (let ((b (point)) (e (make-marker)) s c)
8315 (forward-sexp 1)
8316 (set-marker e (1- (point)))
8317 (goto-char (1+ b))
8318 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8319 (cond
8320 ((match-beginning 1) ; Skip
8321 nil)
8322 (t ; Group
8323 (cperl-contract-level)))))))
8324
8325 (defun cperl-beautify-level (&optional deep)
8326 "Find an enclosing group in regexp and beautify it.
8327 \(Experimental, may change semantics, recheck the result.)
8328 We suppose that the regexp is scanned already."
8329 (interactive "P")
8330 (setq deep (if deep (prefix-numeric-value deep) -1))
8331 (save-excursion
8332 (cperl-regext-to-level-start)
8333 (let ((b (point)) (e (make-marker)))
8334 (forward-sexp 1)
8335 (set-marker e (1- (point)))
8336 (cperl-beautify-regexp-piece b e 'level deep))))
8337
8338 (defun cperl-invert-if-unless-modifiers ()
8339 "Change `B if A;' into `if (A) {B}' etc if possible.
8340 \(Unfinished.)"
8341 (interactive)
8342 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8343 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8344 (and (= (char-syntax (preceding-char)) ?w)
8345 (forward-sexp -1))
8346 (setq pre-if (point))
8347 (cperl-backward-to-start-of-expr)
8348 (setq pre-B (point))
8349 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8350 (cperl-forward-to-end-of-expr)
8351 (setq post-A (point))
8352 (goto-char pre-if)
8353 (or (looking-at w-rex)
8354 ;; Find the position
8355 (progn (goto-char post-A)
8356 (while (and
8357 (not (looking-at w-rex))
8358 (> (point) pre-B))
8359 (forward-sexp -1))
8360 (setq pre-if (point))))
8361 (or (looking-at w-rex)
8362 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8363 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8364 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8365 ;; First, simple part: find code boundaries
8366 (forward-sexp 1)
8367 (setq post-if (point))
8368 (forward-sexp -2)
8369 (forward-sexp 1)
8370 (setq post-B (point))
8371 (cperl-backward-to-start-of-expr)
8372 (setq pre-B (point))
8373 (setq B (buffer-substring pre-B post-B))
8374 (goto-char pre-if)
8375 (forward-sexp 2)
8376 (forward-sexp -1)
8377 ;; May be after $, @, $# etc of a variable
8378 (skip-chars-backward "$@%#")
8379 (setq pre-A (point))
8380 (cperl-forward-to-end-of-expr)
8381 (setq post-A (point))
8382 (setq A (buffer-substring pre-A post-A))
8383 ;; Now modify (from end, to not break the stuff)
8384 (skip-chars-forward " \t;")
8385 (delete-region pre-A (point)) ; we move to pre-A
8386 (insert "\n" B ";\n}")
8387 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8388 (delete-region pre-if post-if)
8389 (delete-region pre-B post-B)
8390 (goto-char pre-B)
8391 (insert if-string " (" A ") {")
8392 (setq post-B (point))
8393 (if (looking-at "[ \t]+$")
8394 (delete-horizontal-space)
8395 (if (looking-at "[ \t]*#")
8396 (cperl-indent-for-comment)
8397 (just-one-space)))
8398 (forward-line 1)
8399 (if (looking-at "[ \t]*$")
8400 (progn ; delete line
8401 (delete-horizontal-space)
8402 (delete-region (point) (1+ (point)))))
8403 (cperl-indent-line)
8404 (goto-char (1- post-B))
8405 (forward-sexp 1)
8406 (cperl-indent-line)
8407 (goto-char pre-B)))
8408
8409 (defun cperl-invert-if-unless ()
8410 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8411 If the cursor is not on the leading keyword of the BLOCK flavor of
8412 construct, will assume it is the STATEMENT flavor, so will try to find
8413 the appropriate statement modifier."
8414 (interactive)
8415 (and (= (char-syntax (preceding-char)) ?w)
8416 (forward-sexp -1))
8417 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8418 (let ((pre-if (point))
8419 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8420 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8421 (forward-sexp 2)
8422 (setq post-A (point))
8423 (forward-sexp -1)
8424 (setq pre-A (point))
8425 (setq is-block (and (eq (following-char) ?\( )
8426 (save-excursion
8427 (condition-case nil
8428 (progn
8429 (forward-sexp 2)
8430 (forward-sexp -1)
8431 (eq (following-char) ?\{ ))
8432 (error nil)))))
8433 (if is-block
8434 (progn
8435 (goto-char post-A)
8436 (forward-sexp 1)
8437 (setq post-B (point))
8438 (forward-sexp -1)
8439 (setq pre-B (point))
8440 (if (and (eq (following-char) ?\{ )
8441 (progn
8442 (cperl-backward-to-noncomment post-A)
8443 (eq (preceding-char) ?\) )))
8444 (if (condition-case nil
8445 (progn
8446 (goto-char post-B)
8447 (forward-sexp 1)
8448 (forward-sexp -1)
8449 (looking-at "\\<els\\(e\\|if\\)\\>"))
8450 (error nil))
8451 (error
8452 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8453 (goto-char (1- post-B))
8454 (cperl-backward-to-noncomment pre-B)
8455 (if (eq (preceding-char) ?\;)
8456 (forward-char -1))
8457 (setq end-B-code (point))
8458 (goto-char pre-B)
8459 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8460 (setq p (match-beginning 0)
8461 A (buffer-substring p (match-end 0))
8462 state (parse-partial-sexp pre-B p))
8463 (or (nth 3 state)
8464 (nth 4 state)
8465 (nth 5 state)
8466 (error "`%s' inside `%s' BLOCK" A if-string))
8467 (goto-char (match-end 0)))
8468 ;; Finally got it
8469 (goto-char (1+ pre-B))
8470 (skip-chars-forward " \t\n")
8471 (setq B (buffer-substring (point) end-B-code))
8472 (goto-char end-B-code)
8473 (or (looking-at ";?[ \t\n]*}")
8474 (progn
8475 (skip-chars-forward "; \t\n")
8476 (setq B-comment
8477 (buffer-substring (point) (1- post-B)))))
8478 (and (equal B "")
8479 (setq B "1"))
8480 (goto-char (1- post-A))
8481 (cperl-backward-to-noncomment pre-A)
8482 (or (looking-at "[ \t\n]*)")
8483 (goto-char (1- post-A)))
8484 (setq p (point))
8485 (goto-char (1+ pre-A))
8486 (skip-chars-forward " \t\n")
8487 (setq A (buffer-substring (point) p))
8488 (delete-region pre-B post-B)
8489 (delete-region pre-A post-A)
8490 (goto-char pre-if)
8491 (insert B " ")
8492 (and B-comment (insert B-comment " "))
8493 (just-one-space)
8494 (forward-word 1)
8495 (setq pre-A (point))
8496 (insert " " A ";")
8497 (delete-horizontal-space)
8498 (setq post-B (point))
8499 (if (looking-at "#")
8500 (indent-for-comment))
8501 (goto-char post-B)
8502 (forward-char -1)
8503 (delete-horizontal-space)
8504 (goto-char pre-A)
8505 (just-one-space)
8506 (goto-char pre-if)
8507 (setq pre-A (set-marker (make-marker) pre-A))
8508 (while (<= (point) (marker-position pre-A))
8509 (cperl-indent-line)
8510 (forward-line 1))
8511 (goto-char (marker-position pre-A))
8512 (if B-comment
8513 (progn
8514 (forward-line -1)
8515 (indent-for-comment)
8516 (goto-char (marker-position pre-A)))))
8517 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8518 ;; (error "`%s' not with an (EXPR)" if-string)
8519 (forward-sexp -1)
8520 (cperl-invert-if-unless-modifiers)))
8521 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8522 (cperl-invert-if-unless-modifiers)))
8523
8524 ;;; By Anthony Foiani <afoiani@uswest.com>
8525 ;;; Getting help on modules in C-h f ?
8526 ;;; This is a modified version of `man'.
8527 ;;; Need to teach it how to lookup functions
8528 ;;;###autoload
8529 (defun cperl-perldoc (word)
8530 "Run `perldoc' on WORD."
8531 (interactive
8532 (list (let* ((default-entry (cperl-word-at-point))
8533 (input (read-string
8534 (format "perldoc entry%s: "
8535 (if (string= default-entry "")
8536 ""
8537 (format " (default %s)" default-entry))))))
8538 (if (string= input "")
8539 (if (string= default-entry "")
8540 (error "No perldoc args given")
8541 default-entry)
8542 input))))
8543 (require 'man)
8544 (let* ((case-fold-search nil)
8545 (is-func (and
8546 (string-match "^[a-z]+$" word)
8547 (string-match (concat "^" word "\\>")
8548 (documentation-property
8549 'cperl-short-docs
8550 'variable-documentation))))
8551 (Man-switches "")
8552 (manual-program (if is-func "perldoc -f" "perldoc")))
8553 (cond
8554 ((featurep 'xemacs)
8555 (let ((Manual-program "perldoc")
8556 (Manual-switches (if is-func (list "-f"))))
8557 (manual-entry word)))
8558 (t
8559 (Man-getpage-in-background word)))))
8560
8561 ;;;###autoload
8562 (defun cperl-perldoc-at-point ()
8563 "Run a `perldoc' on the word around point."
8564 (interactive)
8565 (cperl-perldoc (cperl-word-at-point)))
8566
8567 (defcustom pod2man-program "pod2man"
8568 "*File name for `pod2man'."
8569 :type 'file
8570 :group 'cperl)
8571
8572 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8573 (defun cperl-pod-to-manpage ()
8574 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8575 (interactive)
8576 (require 'man)
8577 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8578 (bufname (concat "Man " buffer-file-name))
8579 (buffer (generate-new-buffer bufname)))
8580 (with-current-buffer buffer
8581 (let ((process-environment (copy-sequence process-environment)))
8582 ;; Prevent any attempt to use display terminal fanciness.
8583 (setenv "TERM" "dumb")
8584 (set-process-sentinel
8585 (start-process pod2man-program buffer "sh" "-c"
8586 (format (cperl-pod2man-build-command) pod2man-args))
8587 'Man-bgproc-sentinel)))))
8588
8589 ;;; Updated version by him too
8590 (defun cperl-build-manpage ()
8591 "Create a virtual manpage in Emacs from the POD in the file."
8592 (interactive)
8593 (require 'man)
8594 (cond
8595 ((featurep 'xemacs)
8596 (let ((Manual-program "perldoc"))
8597 (manual-entry buffer-file-name)))
8598 (t
8599 (let* ((manual-program "perldoc")
8600 (Man-switches ""))
8601 (Man-getpage-in-background buffer-file-name)))))
8602
8603 (defun cperl-pod2man-build-command ()
8604 "Builds the entire background manpage and cleaning command."
8605 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8606 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8607 (while (and flist (car flist))
8608 (let ((pcom (car (car flist)))
8609 (pargs (cdr (car flist))))
8610 (setq command
8611 (concat command " | " pcom " "
8612 (mapconcat '(lambda (phrase)
8613 (if (not (stringp phrase))
8614 (error "Malformed Man-filter-list"))
8615 phrase)
8616 pargs " ")))
8617 (setq flist (cdr flist))))
8618 command))
8619
8620
8621 (defun cperl-next-interpolated-REx-1 ()
8622 "Move point to next REx which has interpolated parts without //o.
8623 Skips RExes consisting of one interpolated variable.
8624
8625 Note that skipped RExen are not performance hits."
8626 (interactive "")
8627 (cperl-next-interpolated-REx 1))
8628
8629 (defun cperl-next-interpolated-REx-0 ()
8630 "Move point to next REx which has interpolated parts without //o."
8631 (interactive "")
8632 (cperl-next-interpolated-REx 0))
8633
8634 (defun cperl-next-interpolated-REx (&optional skip beg limit)
8635 "Move point to next REx which has interpolated parts.
8636 SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8637 point and the limit of search (default to point and end of buffer).
8638
8639 SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8640 semantic may be used as a numeric argument.
8641
8642 Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8643 a result of qr//, this is not a performance hit), t for the rest."
8644 (interactive "P")
8645 (if (numberp skip) (setq skip (list 0 skip)))
8646 (or beg (setq beg (point)))
8647 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8648 (let (pp)
8649 (and (eq (get-text-property beg 'syntax-type) 'string)
8650 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8651 (cperl-map-pods-heres
8652 (function (lambda (s e p)
8653 (if (memq (get-text-property s 'REx-interpolated) skip)
8654 t
8655 (setq pp s)
8656 nil))) ; nil stops
8657 'REx-interpolated beg limit)
8658 (if pp (goto-char pp)
8659 (message "No more interpolated REx"))))
8660
8661 ;;; Initial version contributed by Trey Belew
8662 (defun cperl-here-doc-spell (&optional beg end)
8663 "Spell-check HERE-documents in the Perl buffer.
8664 If a region is highlighted, restricts to the region."
8665 (interactive "")
8666 (cperl-pod-spell t beg end))
8667
8668 (defun cperl-pod-spell (&optional do-heres beg end)
8669 "Spell-check POD documentation.
8670 If invoked with prefix argument, will do HERE-DOCs instead.
8671 If a region is highlighted, restricts to the region."
8672 (interactive "P")
8673 (save-excursion
8674 (let (beg end)
8675 (if (cperl-mark-active)
8676 (setq beg (min (mark) (point))
8677 end (max (mark) (point)))
8678 (setq beg (point-min)
8679 end (point-max)))
8680 (cperl-map-pods-heres (function
8681 (lambda (s e p)
8682 (if do-heres
8683 (setq e (save-excursion
8684 (goto-char e)
8685 (forward-line -1)
8686 (point))))
8687 (ispell-region s e)
8688 t))
8689 (if do-heres 'here-doc-group 'in-pod)
8690 beg end))))
8691
8692 (defun cperl-map-pods-heres (func &optional prop s end)
8693 "Executes a function over regions of pods or here-documents.
8694 PROP is the text-property to search for; default to `in-pod'. Stop when
8695 function returns nil."
8696 (let (pos posend has-prop (cont t))
8697 (or prop (setq prop 'in-pod))
8698 (or s (setq s (point-min)))
8699 (or end (setq end (point-max)))
8700 (cperl-update-syntaxification end end)
8701 (save-excursion
8702 (goto-char (setq pos s))
8703 (while (and cont (< pos end))
8704 (setq has-prop (get-text-property pos prop))
8705 (setq posend (next-single-property-change pos prop nil end))
8706 (and has-prop
8707 (setq cont (funcall func pos posend prop)))
8708 (setq pos posend)))))
8709
8710 ;;; Based on code by Masatake YAMATO:
8711 (defun cperl-get-here-doc-region (&optional pos pod)
8712 "Return HERE document region around the point.
8713 Return nil if the point is not in a HERE document region. If POD is non-nil,
8714 will return a POD section if point is in a POD section."
8715 (or pos (setq pos (point)))
8716 (cperl-update-syntaxification pos pos)
8717 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8718 (and pod
8719 (eq 'pod (get-text-property pos 'syntax-type))))
8720 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8721 (e (next-single-property-change pos 'syntax-type)))
8722 (cons b (or e (point-max))))))
8723
8724 (defun cperl-narrow-to-here-doc (&optional pos)
8725 "Narrows editing region to the HERE-DOC at POS.
8726 POS defaults to the point."
8727 (interactive "d")
8728 (or pos (setq pos (point)))
8729 (let ((p (cperl-get-here-doc-region pos)))
8730 (or p (error "Not inside a HERE document"))
8731 (narrow-to-region (car p) (cdr p))
8732 (message
8733 "When you are finished with narrow editing, type C-x n w")))
8734
8735 (defun cperl-select-this-pod-or-here-doc (&optional pos)
8736 "Select the HERE-DOC (or POD section) at POS.
8737 POS defaults to the point."
8738 (interactive "d")
8739 (let ((p (cperl-get-here-doc-region pos t)))
8740 (if p
8741 (progn
8742 (goto-char (car p))
8743 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8744 (message "I do not think POS is in POD or a HERE-doc..."))))
8745
8746 (defun cperl-facemenu-add-face-function (face end)
8747 "A callback to process user-initiated font-change requests.
8748 Translates `bold', `italic', and `bold-italic' requests to insertion of
8749 corresponding POD directives, and `underline' to C<> POD directive.
8750
8751 Such requests are usually bound to M-o LETTER."
8752 (or (get-text-property (point) 'in-pod)
8753 (error "Faces can only be set within POD"))
8754 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8755 (cdr (or (assq face '((bold . "B<")
8756 (italic . "I<")
8757 (bold-italic . "B<I<")
8758 (underline . "C<")))
8759 (error "Face %s not configured for cperl-mode"
8760 face))))
8761 \f
8762 (defun cperl-time-fontification (&optional l step lim)
8763 "Times how long it takes to do incremental fontification in a region.
8764 L is the line to start at, STEP is the number of lines to skip when
8765 doing next incremental fontification, LIM is the maximal number of
8766 incremental fontification to perform. Messages are accumulated in
8767 *Messages* buffer.
8768
8769 May be used for pinpointing which construct slows down buffer fontification:
8770 start with default arguments, then refine the slowdown regions."
8771 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8772 (or l (setq l 1))
8773 (or step (setq step 500))
8774 (or lim (setq lim 40))
8775 (let* ((timems (function (lambda ()
8776 (let ((tt (current-time)))
8777 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8778 (tt (funcall timems)) (c 0) delta tot)
8779 (goto-char (point-min))
8780 (forward-line (1- l))
8781 (cperl-mode)
8782 (setq tot (- (- tt (setq tt (funcall timems)))))
8783 (message "cperl-mode at %s: %s" l tot)
8784 (while (and (< c lim) (not (eobp)))
8785 (forward-line step)
8786 (setq l (+ l step))
8787 (setq c (1+ c))
8788 (cperl-update-syntaxification (point) (point))
8789 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8790 (message "to %s:%6s,%7s" l delta tot))
8791 tot))
8792
8793 (defvar font-lock-cache-position)
8794
8795 (defun cperl-emulate-lazy-lock (&optional window-size)
8796 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8797 Start fontifying the buffer from the start (or end) using the given
8798 WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8799 goes backwards; default is -50. This function is not CPerl-specific; it
8800 may be used to debug problems with delayed incremental fontification."
8801 (interactive
8802 "nSize of window for incremental fontification, negative goes backwards: ")
8803 (or window-size (setq window-size -50))
8804 (let ((pos (if (> window-size 0)
8805 (point-min)
8806 (point-max)))
8807 p)
8808 (goto-char pos)
8809 (normal-mode)
8810 ;; Why needed??? With older font-locks???
8811 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8812 (while (if (> window-size 0)
8813 (< pos (point-max))
8814 (> pos (point-min)))
8815 (setq p (progn
8816 (forward-line window-size)
8817 (point)))
8818 (font-lock-fontify-region (min p pos) (max p pos))
8819 (setq pos p))))
8820
8821 \f
8822 (defun cperl-lazy-install ()) ; Avoid a warning
8823 (defun cperl-lazy-unstall ()) ; Avoid a warning
8824
8825 (if (fboundp 'run-with-idle-timer)
8826 (progn
8827 (defvar cperl-help-shown nil
8828 "Non-nil means that the help was already shown now.")
8829
8830 (defvar cperl-lazy-installed nil
8831 "Non-nil means that the lazy-help handlers are installed now.")
8832
8833 (defun cperl-lazy-install ()
8834 "Switches on Auto-Help on Perl constructs (put in the message area).
8835 Delay of auto-help controlled by `cperl-lazy-help-time'."
8836 (interactive)
8837 (make-local-variable 'cperl-help-shown)
8838 (if (and (cperl-val 'cperl-lazy-help-time)
8839 (not cperl-lazy-installed))
8840 (progn
8841 (add-hook 'post-command-hook 'cperl-lazy-hook)
8842 (run-with-idle-timer
8843 (cperl-val 'cperl-lazy-help-time 1000000 5)
8844 t
8845 'cperl-get-help-defer)
8846 (setq cperl-lazy-installed t))))
8847
8848 (defun cperl-lazy-unstall ()
8849 "Switches off Auto-Help on Perl constructs (put in the message area).
8850 Delay of auto-help controlled by `cperl-lazy-help-time'."
8851 (interactive)
8852 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8853 (cancel-function-timers 'cperl-get-help-defer)
8854 (setq cperl-lazy-installed nil))
8855
8856 (defun cperl-lazy-hook ()
8857 (setq cperl-help-shown nil))
8858
8859 (defun cperl-get-help-defer ()
8860 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8861 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8862 (cperl-get-help)
8863 (setq cperl-help-shown t))))
8864 (cperl-lazy-install)))
8865
8866
8867 ;;; Plug for wrong font-lock:
8868
8869 (defun cperl-font-lock-unfontify-region-function (beg end)
8870 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8871 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8872 before-change-functions after-change-functions
8873 deactivate-mark buffer-file-name buffer-file-truename)
8874 (remove-text-properties beg end '(face nil))
8875 (if (and (not modified) (buffer-modified-p))
8876 (set-buffer-modified-p nil))))
8877
8878 (defun cperl-font-lock-fontify-region-function (beg end loudly)
8879 "Extends the region to safe positions, then calls the default function.
8880 Newer `font-lock's can do it themselves.
8881 We unwind only as far as needed for fontification. Syntaxification may
8882 do extra unwind via `cperl-unwind-to-safe'."
8883 (save-excursion
8884 (goto-char beg)
8885 (while (and beg
8886 (progn
8887 (beginning-of-line)
8888 (eq (get-text-property (setq beg (point)) 'syntax-type)
8889 'multiline)))
8890 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8891 (goto-char beg)))
8892 (setq beg (point))
8893 (goto-char end)
8894 (while (and end
8895 (progn
8896 (or (bolp) (condition-case nil
8897 (forward-line 1)
8898 (error nil)))
8899 (eq (get-text-property (setq end (point)) 'syntax-type)
8900 'multiline)))
8901 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8902 (goto-char end))
8903 (setq end (point)))
8904 (font-lock-default-fontify-region beg end loudly))
8905
8906 (defvar cperl-d-l nil)
8907 (defun cperl-fontify-syntaxically (end)
8908 ;; Some vars for debugging only
8909 ;; (message "Syntaxifying...")
8910 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8911 (istate (car cperl-syntax-state))
8912 start from-start edebug-backtrace-buffer)
8913 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8914 (progn
8915 (require 'edebug)
8916 (let ((f 'edebug-backtrace))
8917 (funcall f)))) ; Avoid compile-time warning
8918 (or cperl-syntax-done-to
8919 (setq cperl-syntax-done-to (point-min)
8920 from-start t))
8921 (setq start (if (and cperl-hook-after-change
8922 (not from-start))
8923 cperl-syntax-done-to ; Fontify without change; ignore start
8924 ;; Need to forget what is after `start'
8925 (min cperl-syntax-done-to (point))))
8926 (goto-char start)
8927 (beginning-of-line)
8928 (setq start (point))
8929 (and cperl-syntaxify-unwind
8930 (setq end (cperl-unwind-to-safe t end)
8931 start (point)))
8932 (and (> end start)
8933 (setq cperl-syntax-done-to start) ; In case what follows fails
8934 (cperl-find-pods-heres start end t nil t))
8935 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8936 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8937 dbg iend start end idone cperl-syntax-done-to
8938 istate (car cperl-syntax-state))) ; For debugging
8939 nil)) ; Do not iterate
8940
8941 (defun cperl-fontify-update (end)
8942 (let ((pos (point-min)) prop posend)
8943 (setq end (point-max))
8944 (while (< pos end)
8945 (setq prop (get-text-property pos 'cperl-postpone)
8946 posend (next-single-property-change pos 'cperl-postpone nil end))
8947 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8948 (setq pos posend)))
8949 nil) ; Do not iterate
8950
8951 (defun cperl-fontify-update-bad (end)
8952 ;; Since fontification happens with different region than syntaxification,
8953 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8954 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8955 (if prop
8956 (setq pos (or (cperl-beginning-of-property
8957 (cperl-1+ pos) 'cperl-postpone)
8958 (point-min))))
8959 (while (< pos end)
8960 (setq posend (next-single-property-change pos 'cperl-postpone))
8961 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8962 (setq pos posend)
8963 (setq prop (get-text-property pos 'cperl-postpone))))
8964 nil) ; Do not iterate
8965
8966 ;; Called when any modification is made to buffer text.
8967 (defun cperl-after-change-function (beg end old-len)
8968 ;; We should have been informed about changes by `font-lock'. Since it
8969 ;; does not inform as which calls are defered, do it ourselves
8970 (if cperl-syntax-done-to
8971 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8972
8973 (defun cperl-update-syntaxification (from to)
8974 (if (and cperl-use-syntax-table-text-property
8975 cperl-syntaxify-by-font-lock
8976 (or (null cperl-syntax-done-to)
8977 (< cperl-syntax-done-to to)))
8978 (progn
8979 (save-excursion
8980 (goto-char from)
8981 (cperl-fontify-syntaxically to)))))
8982
8983 (defvar cperl-version
8984 (let ((v "Revision: 6.2"))
8985 (string-match ":\\s *\\([0-9.]+\\)" v)
8986 (substring v (match-beginning 1) (match-end 1)))
8987 "Version of IZ-supported CPerl package this file is based on.")
8988
8989 (defun cperl-mode-unload-function ()
8990 "Unload the Cperl mode library."
8991 (let ((new-mode (if (eq (symbol-function 'perl-mode) 'cperl-mode)
8992 'fundamental-mode
8993 'perl-mode)))
8994 (dolist (buf (buffer-list))
8995 (with-current-buffer buf
8996 (when (eq major-mode 'cperl-mode)
8997 (funcall new-mode)))))
8998 ;; continue standard unloading
8999 nil)
9000
9001 (provide 'cperl-mode)
9002
9003 ;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
9004 ;;; cperl-mode.el ends here