* progmodes/cperl-mode.el: Merge upstream 6.2.
[bpt/emacs.git] / lisp / progmodes / cperl-mode.el
... / ...
CommitLineData
1;;; cperl-mode.el --- Perl code editing commands for Emacs
2
3;; Copyright (C) 1985, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4;; 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5;; 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
208and do constructs look like:
209
210 if ()
211 {
212 }
213
214instead 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
224for 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.
248If 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.
254An open brace following other text is treated as if it were this far
255to 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.
277This 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.
287Versions 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,
293and after colons and semicolons, inserted in CPerl code. The following
294\\[cperl-electric-backspace] will remove the inserted whitespace.
295Insertion 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.
302Active 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.
308Subject 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,
314regardless 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'.
320Can 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 ` '.
326Can 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.
332Closing 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.
338Can 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.
351Default 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.
357In any case these two mean plain and hairy linefeeds together.
358Can 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.
364Can be overwritten by `cperl-hairy' if nil.
365
366Uses `abbrev-mode' to do the expansion. If you want to use your
367own abbrevs in cperl-mode, but do not want keywords to be
368electric, you must redefine `cperl-mode-abbrev-table': do
369\\[edit-abbrevs], search for `cperl-mode-abbrev-table', and, in
370that paragraph, delete the words that appear at the ends of lines and
371that 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.
383Affects: `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
416(defcustom cperl-clobber-mode-lists
417 (not
418 (and
419 (boundp 'interpreter-mode-alist)
420 (assoc "miniperl" interpreter-mode-alist)
421 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
422 "*Whether to install us into `interpreter-' and `extension' mode lists."
423 :type 'boolean
424 :group 'cperl)
425
426(defcustom cperl-info-on-command-no-prompt nil
427 "*Not-nil (and non-null) means not to prompt on C-h f.
428The opposite behavior is always available if prefixed with C-c.
429Can be overwritten by `cperl-hairy' if nil."
430 :type '(choice (const null) boolean)
431 :group 'cperl-affected-by-hairy)
432
433(defcustom cperl-clobber-lisp-bindings nil
434 "*Not-nil (and non-null) means not overwrite C-h f.
435The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
436Can be overwritten by `cperl-hairy' if nil."
437 :type '(choice (const null) boolean)
438 :group 'cperl-affected-by-hairy)
439
440(defcustom cperl-lazy-help-time nil
441 "*Not-nil (and non-null) means to show lazy help after given idle time.
442Can be overwritten by `cperl-hairy' to be 5 sec if nil."
443 :type '(choice (const null) (const nil) integer)
444 :group 'cperl-affected-by-hairy)
445
446(defcustom cperl-pod-face 'font-lock-comment-face
447 "*Face for POD highlighting."
448 :type 'face
449 :group 'cperl-faces)
450
451(defcustom cperl-pod-head-face 'font-lock-variable-name-face
452 "*Face for POD highlighting.
453Font for POD headers."
454 :type 'face
455 :group 'cperl-faces)
456
457(defcustom cperl-here-face 'font-lock-string-face
458 "*Face for here-docs highlighting."
459 :type 'face
460 :group 'cperl-faces)
461
462;;; Some double-evaluation happened with font-locks... Needed with 21.2...
463(defvar cperl-singly-quote-face (featurep 'xemacs))
464
465(defcustom cperl-invalid-face 'underline
466 "*Face for highlighting trailing whitespace."
467 :type 'face
468 :version "21.1"
469 :group 'cperl-faces)
470
471(defcustom cperl-pod-here-fontify '(featurep 'font-lock)
472 "*Not-nil after evaluation means to highlight POD and here-docs sections."
473 :type 'boolean
474 :group 'cperl-faces)
475
476(defcustom cperl-fontify-m-as-s t
477 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
478 :type 'boolean
479 :group 'cperl-faces)
480
481(defcustom cperl-highlight-variables-indiscriminately nil
482 "*Non-nil means perform additional highlighting on variables.
483Currently only changes how scalar variables are highlighted.
484Note that that variable is only read at initialization time for
485the variable `cperl-font-lock-keywords-2', so changing it after you've
486entered CPerl mode the first time will have no effect."
487 :type 'boolean
488 :group 'cperl)
489
490(defcustom cperl-pod-here-scan t
491 "*Not-nil means look for POD and here-docs sections during startup.
492You can always make lookup from menu or using \\[cperl-find-pods-heres]."
493 :type 'boolean
494 :group 'cperl-speed)
495
496(defcustom cperl-regexp-scan t
497 "*Not-nil means make marking of regular expression more thorough.
498Effective only with `cperl-pod-here-scan'."
499 :type 'boolean
500 :group 'cperl-speed)
501
502(defcustom cperl-hook-after-change t
503 "*Not-nil means install hook to know which regions of buffer are changed.
504May significantly speed up delayed fontification. Changes take effect
505after reload."
506 :type 'boolean
507 :group 'cperl-speed)
508
509(defcustom cperl-imenu-addback nil
510 "*Not-nil means add backreferences to generated `imenu's.
511May require patched `imenu' and `imenu-go'. Obsolete."
512 :type 'boolean
513 :group 'cperl-help-system)
514
515(defcustom cperl-max-help-size 66
516 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
517 :type '(choice integer (const nil))
518 :group 'cperl-help-system)
519
520(defcustom cperl-shrink-wrap-info-frame t
521 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
522 :type 'boolean
523 :group 'cperl-help-system)
524
525(defcustom cperl-info-page "perl"
526 "*Name of the info page containing perl docs.
527Older version of this page was called `perl5', newer `perl'."
528 :type 'string
529 :group 'cperl-help-system)
530
531(defcustom cperl-use-syntax-table-text-property
532 (boundp 'parse-sexp-lookup-properties)
533 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
534 :type 'boolean
535 :group 'cperl-speed)
536
537(defcustom cperl-use-syntax-table-text-property-for-tags
538 cperl-use-syntax-table-text-property
539 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
540 :type 'boolean
541 :group 'cperl-speed)
542
543(defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
544 "*Regexp to match files to scan when generating TAGS."
545 :type 'regexp
546 :group 'cperl)
547
548(defcustom cperl-noscan-files-regexp
549 "/\\(\\.\\.?\\|SCCS\\|RCS\\|CVS\\|blib\\)$"
550 "*Regexp to match files/dirs to skip when generating TAGS."
551 :type 'regexp
552 :group 'cperl)
553
554(defcustom cperl-regexp-indent-step nil
555 "*Indentation used when beautifying regexps.
556If nil, the value of `cperl-indent-level' will be used."
557 :type '(choice integer (const nil))
558 :group 'cperl-indentation-details)
559
560(defcustom cperl-indent-left-aligned-comments t
561 "*Non-nil means that the comment starting in leftmost column should indent."
562 :type 'boolean
563 :group 'cperl-indentation-details)
564
565(defcustom cperl-under-as-char nil
566 "*Non-nil means that the _ (underline) should be treated as word char."
567 :type 'boolean
568 :group 'cperl)
569
570(defcustom cperl-extra-perl-args ""
571 "*Extra arguments to use when starting Perl.
572Currently used with `cperl-check-syntax' only."
573 :type 'string
574 :group 'cperl)
575
576(defcustom cperl-message-electric-keyword t
577 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
578 :type 'boolean
579 :group 'cperl-help-system)
580
581(defcustom cperl-indent-region-fix-constructs 1
582 "*Amount of space to insert between `}' and `else' or `elsif'
583in `cperl-indent-region'. Set to nil to leave as is. Values other
584than 1 and nil will probably not work."
585 :type '(choice (const nil) (const 1))
586 :group 'cperl-indentation-details)
587
588(defcustom cperl-break-one-line-blocks-when-indent t
589 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
590need to be reformatted into multiline ones when indenting a region."
591 :type 'boolean
592 :group 'cperl-indentation-details)
593
594(defcustom cperl-fix-hanging-brace-when-indent t
595 "*Non-nil means that BLOCK-end `}' may be put on a separate line
596when indenting a region.
597Braces followed by else/elsif/while/until are excepted."
598 :type 'boolean
599 :group 'cperl-indentation-details)
600
601(defcustom cperl-merge-trailing-else t
602 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
603may be merged to be on the same line when indenting a region."
604 :type 'boolean
605 :group 'cperl-indentation-details)
606
607(defcustom cperl-indent-parens-as-block nil
608 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
609but for trailing \",\" inside the group, which won't increase indentation.
610One should tune up `cperl-close-paren-offset' as well."
611 :type 'boolean
612 :group 'cperl-indentation-details)
613
614(defcustom cperl-syntaxify-by-font-lock
615 (and cperl-can-font-lock
616 (boundp 'parse-sexp-lookup-properties))
617 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
618 :type '(choice (const message) boolean)
619 :group 'cperl-speed)
620
621(defcustom cperl-syntaxify-unwind
622 t
623 "*Non-nil means that CPerl unwinds to a start of a long construction
624when syntaxifying a chunk of buffer."
625 :type 'boolean
626 :group 'cperl-speed)
627
628(defcustom cperl-syntaxify-for-menu
629 t
630 "*Non-nil means that CPerl syntaxifies up to the point before showing menu.
631This way enabling/disabling of menu items is more correct."
632 :type 'boolean
633 :group 'cperl-speed)
634
635(defcustom cperl-ps-print-face-properties
636 '((font-lock-keyword-face nil nil bold shadow)
637 (font-lock-variable-name-face nil nil bold)
638 (font-lock-function-name-face nil nil bold italic box)
639 (font-lock-constant-face nil "LightGray" bold)
640 (cperl-array-face nil "LightGray" bold underline)
641 (cperl-hash-face nil "LightGray" bold italic underline)
642 (font-lock-comment-face nil "LightGray" italic)
643 (font-lock-string-face nil nil italic underline)
644 (cperl-nonoverridable-face nil nil italic underline)
645 (font-lock-type-face nil nil underline)
646 (font-lock-warning-face nil "LightGray" bold italic box)
647 (underline nil "LightGray" strikeout))
648 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
649 :type '(repeat (cons symbol
650 (cons (choice (const nil) string)
651 (cons (choice (const nil) string)
652 (repeat symbol)))))
653 :group 'cperl-faces)
654
655(defvar cperl-dark-background
656 (cperl-choose-color "navy" "os2blue" "darkgreen"))
657(defvar cperl-dark-foreground
658 (cperl-choose-color "orchid1" "orange"))
659
660(defface cperl-nonoverridable-face
661 `((((class grayscale) (background light))
662 (:background "Gray90" :slant italic :underline t))
663 (((class grayscale) (background dark))
664 (:foreground "Gray80" :slant italic :underline t :weight bold))
665 (((class color) (background light))
666 (:foreground "chartreuse3"))
667 (((class color) (background dark))
668 (:foreground ,cperl-dark-foreground))
669 (t (:weight bold :underline t)))
670 "Font Lock mode face used non-overridable keywords and modifiers of regexps."
671 :group 'cperl-faces)
672
673(defface cperl-array-face
674 `((((class grayscale) (background light))
675 (:background "Gray90" :weight bold))
676 (((class grayscale) (background dark))
677 (:foreground "Gray80" :weight bold))
678 (((class color) (background light))
679 (:foreground "Blue" :background "lightyellow2" :weight bold))
680 (((class color) (background dark))
681 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
682 (t (:weight bold)))
683 "Font Lock mode face used to highlight array names."
684 :group 'cperl-faces)
685
686(defface cperl-hash-face
687 `((((class grayscale) (background light))
688 (:background "Gray90" :weight bold :slant italic))
689 (((class grayscale) (background dark))
690 (:foreground "Gray80" :weight bold :slant italic))
691 (((class color) (background light))
692 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
693 (((class color) (background dark))
694 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
695 (t (:weight bold :slant italic)))
696 "Font Lock mode face used to highlight hash names."
697 :group 'cperl-faces)
698
699\f
700
701;;; Short extra-docs.
702
703(defvar cperl-tips 'please-ignore-this-line
704 "Get maybe newer version of this package from
705 http://ilyaz.org/software/emacs
706Subdirectory `cperl-mode' may contain yet newer development releases and/or
707patches to related files.
708
709For best results apply to an older Emacs the patches from
710 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
711\(this upgrades syntax-parsing abilities of Emacsen v19.34 and
712v20.2 up to the level of Emacs v20.3 - a must for a good Perl
713mode.) As of beginning of 2003, XEmacs may provide a similar ability.
714
715Get support packages choose-color.el (or font-lock-extra.el before
71619.30), imenu-go.el from the same place. \(Look for other files there
717too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
718later you should use choose-color.el *instead* of font-lock-extra.el
719\(and you will not get smart highlighting in C :-().
720
721Note that to enable Compile choices in the menu you need to install
722mode-compile.el.
723
724If your Emacs does not default to `cperl-mode' on Perl files, and you
725want it to: put the following into your .emacs file:
726
727 (defalias 'perl-mode 'cperl-mode)
728
729Get perl5-info from
730 $CPAN/doc/manual/info/perl5-old/perl5-info.tar.gz
731Also, one can generate a newer documentation running `pod2texi' converter
732 $CPAN/doc/manual/info/perl5/pod2texi-0.1.tar.gz
733
734If you use imenu-go, run imenu on perl5-info buffer (you can do it
735from Perl menu). If many files are related, generate TAGS files from
736Tools/Tags submenu in Perl menu.
737
738If some class structure is too complicated, use Tools/Hierarchy-view
739from Perl menu, or hierarchic view of imenu. The second one uses the
740current buffer only, the first one requires generation of TAGS from
741Perl/Tools/Tags menu beforehand.
742
743Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
744
745Switch auto-help on/off with Perl/Tools/Auto-help.
746
747Though with contemporary Emaxen CPerl mode should maintain the correct
748parsing of Perl even when editing, sometimes it may be lost. Fix this by
749
750 \\[normal-mode]
751
752In cases of more severe confusion sometimes it is helpful to do
753
754 \\[load-library] cperl-mode RET
755 \\[normal-mode]
756
757Before reporting (non-)problems look in the problem section of online
758micro-docs on what I know about CPerl problems.")
759
760(defvar cperl-problems 'please-ignore-this-line
761 "Description of problems in CPerl mode.
762Some faces will not be shown on some versions of Emacs unless you
763install choose-color.el, available from
764 http://ilyaz.org/software/emacs
765
766`fill-paragraph' on a comment may leave the point behind the
767paragraph. It also triggers a bug in some versions of Emacs (CPerl tries
768to detect it and bulk out).
769
770See documentation of a variable `cperl-problems-old-emaxen' for the
771problems which disappear if you upgrade Emacs to a reasonably new
772version (20.3 for Emacs, and those of 2004 for XEmacs).")
773
774(defvar cperl-problems-old-emaxen 'please-ignore-this-line
775 "Description of problems in CPerl mode specific for older Emacs versions.
776
777Emacs had a _very_ restricted syntax parsing engine until version
77820.1. Most problems below are corrected starting from this version of
779Emacs, and all of them should be fixed in version 20.3. (Or apply
780patches to Emacs 19.33/34 - see tips.) XEmacs was very backward in
781this respect (until 2003).
782
783Note that even with newer Emacsen in some very rare cases the details
784of interaction of `font-lock' and syntaxification may be not cleaned
785up yet. You may get slightly different colors basing on the order of
786fontification and syntaxification. Say, the initial faces is correct,
787but editing the buffer breaks this.
788
789Even with older Emacsen CPerl mode tries to corrects some Emacs
790misunderstandings, however, for efficiency reasons the degree of
791correction is different for different operations. The partially
792corrected problems are: POD sections, here-documents, regexps. The
793operations are: highlighting, indentation, electric keywords, electric
794braces.
795
796This may be confusing, since the regexp s#//#/#\; may be highlighted
797as a comment, but it will be recognized as a regexp by the indentation
798code. Or the opposite case, when a POD section is highlighted, but
799may break the indentation of the following code (though indentation
800should work if the balance of delimiters is not broken by POD).
801
802The main trick (to make $ a \"backslash\") makes constructions like
803${aaa} look like unbalanced braces. The only trick I can think of is
804to insert it as $ {aaa} (valid in perl5, not in perl4).
805
806Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
807as /($|\\s)/. Note that such a transposition is not always possible.
808
809The solution is to upgrade your Emacs or patch an older one. Note
810that Emacs 20.2 has some bugs related to `syntax-table' text
811properties. Patches are available on the main CPerl download site,
812and on CPAN.
813
814If these bugs cannot be fixed on your machine (say, you have an inferior
815environment and cannot recompile), you may still disable all the fancy stuff
816via `cperl-use-syntax-table-text-property'.")
817
818(defvar cperl-praise 'please-ignore-this-line
819 "Advantages of CPerl mode.
820
8210) It uses the newest `syntax-table' property ;-);
822
8231) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
824mode - but the latter number may have improved too in last years) even
825with old Emaxen which do not support `syntax-table' property.
826
827When using `syntax-table' property for syntax assist hints, it should
828handle 99.995% of lines correct - or somesuch. It automatically
829updates syntax assist hints when you edit your script.
830
8312) It is generally believed to be \"the most user-friendly Emacs
832package\" whatever it may mean (I doubt that the people who say similar
833things tried _all_ the rest of Emacs ;-), but this was not a lonely
834voice);
835
8363) Everything is customizable, one-by-one or in a big sweep;
837
8384) It has many easily-accessible \"tools\":
839 a) Can run program, check syntax, start debugger;
840 b) Can lineup vertically \"middles\" of rows, like `=' in
841 a = b;
842 cc = d;
843 c) Can insert spaces where this impoves readability (in one
844 interactive sweep over the buffer);
845 d) Has support for imenu, including:
846 1) Separate unordered list of \"interesting places\";
847 2) Separate TOC of POD sections;
848 3) Separate list of packages;
849 4) Hierarchical view of methods in (sub)packages;
850 5) and functions (by the full name - with package);
851 e) Has an interface to INFO docs for Perl; The interface is
852 very flexible, including shrink-wrapping of
853 documentation buffer/frame;
854 f) Has a builtin list of one-line explanations for perl constructs.
855 g) Can show these explanations if you stay long enough at the
856 corresponding place (or on demand);
857 h) Has an enhanced fontification (using 3 or 4 additional faces
858 comparing to font-lock - basically, different
859 namespaces in Perl have different colors);
860 i) Can construct TAGS basing on its knowledge of Perl syntax,
861 the standard menu has 6 different way to generate
862 TAGS (if \"by directory\", .xs files - with C-language
863 bindings - are included in the scan);
864 j) Can build a hierarchical view of classes (via imenu) basing
865 on generated TAGS file;
866 k) Has electric parentheses, electric newlines, uses Abbrev
867 for electric logical constructs
868 while () {}
869 with different styles of expansion (context sensitive
870 to be not so bothering). Electric parentheses behave
871 \"as they should\" in a presence of a visible region.
872 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
873 m) Can convert from
874 if (A) { B }
875 to
876 B if A;
877
878 n) Highlights (by user-choice) either 3-delimiters constructs
879 (such as tr/a/b/), or regular expressions and `y/tr';
880 o) Highlights trailing whitespace;
881 p) Is able to manipulate Perl Regular Expressions to ease
882 conversion to a more readable form.
883 q) Can ispell POD sections and HERE-DOCs.
884 r) Understands comments and character classes inside regular
885 expressions; can find matching () and [] in a regular expression.
886 s) Allows indentation of //x-style regular expressions;
887 t) Highlights different symbols in regular expressions according
888 to their function; much less problems with backslashitis;
889 u) Allows to find regular expressions which contain interpolated parts.
890
8915) The indentation engine was very smart, but most of tricks may be
892not needed anymore with the support for `syntax-table' property. Has
893progress indicator for indentation (with `imenu' loaded).
894
8956) Indent-region improves inline-comments as well; also corrects
896whitespace *inside* the conditional/loop constructs.
897
8987) Fill-paragraph correctly handles multi-line comments;
899
9008) Can switch to different indentation styles by one command, and restore
901the settings present before the switch.
902
9039) When doing indentation of control constructs, may correct
904line-breaks/spacing between elements of the construct.
905
90610) Uses a linear-time algorith for indentation of regions (on Emaxen with
907capable syntax engines).
908
90911) Syntax-highlight, indentation, sexp-recognition inside regular expressions.
910")
911
912(defvar cperl-speed 'please-ignore-this-line
913 "This is an incomplete compendium of what is available in other parts
914of CPerl documentation. (Please inform me if I skept anything.)
915
916There is a perception that CPerl is slower than alternatives. This part
917of documentation is designed to overcome this misconception.
918
919*By default* CPerl tries to enable the most comfortable settings.
920From most points of view, correctly working package is infinitely more
921comfortable than a non-correctly working one, thus by default CPerl
922prefers correctness over speed. Below is the guide how to change
923settings if your preferences are different.
924
925A) Speed of loading the file. When loading file, CPerl may perform a
926scan which indicates places which cannot be parsed by primitive Emacs
927syntax-parsing routines, and marks them up so that either
928
929 A1) CPerl may work around these deficiencies (for big chunks, mostly
930 PODs and HERE-documents), or
931 A2) On capable Emaxen CPerl will use improved syntax-handlings
932 which reads mark-up hints directly.
933
934 The scan in case A2 is much more comprehensive, thus may be slower.
935
936 User can disable syntax-engine-helping scan of A2 by setting
937 `cperl-use-syntax-table-text-property'
938 variable to nil (if it is set to t).
939
940 One can disable the scan altogether (both A1 and A2) by setting
941 `cperl-pod-here-scan'
942 to nil.
943
944B) Speed of editing operations.
945
946 One can add a (minor) speedup to editing operations by setting
947 `cperl-use-syntax-table-text-property'
948 variable to nil (if it is set to t). This will disable
949 syntax-engine-helping scan, thus will make many more Perl
950 constructs be wrongly recognized by CPerl, thus may lead to
951 wrongly matched parentheses, wrong indentation, etc.
952
953 One can unset `cperl-syntaxify-unwind'. This might speed up editing
954 of, say, long POD sections.")
955
956(defvar cperl-tips-faces 'please-ignore-this-line
957 "CPerl mode uses following faces for highlighting:
958
959 `cperl-array-face' Array names
960 `cperl-hash-face' Hash names
961 `font-lock-comment-face' Comments, PODs and whatever is considered
962 syntaxically to be not code
963 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
964 2-arg operators s/y/tr/ or of RExen,
965 `font-lock-warning-face' Special-cased m// and s//foo/,
966 `font-lock-function-name-face' _ as a target of a file tests, file tests,
967 subroutine names at the moment of definition
968 (except those conflicting with Perl operators),
969 package names (when recognized), format names
970 `font-lock-keyword-face' Control flow switch constructs, declarators
971 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
972 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
973 literal parts and the terminator of formats
974 and whatever is syntaxically considered
975 as string literals
976 `font-lock-type-face' Overridable keywords
977 `font-lock-variable-name-face' Variable declarations, indirect array and
978 hash names, POD headers/item names
979 `cperl-invalid-face' Trailing whitespace
980
981Note that in several situations the highlighting tries to inform about
982possible confusion, such as different colors for function names in
983declarations depending on what they (do not) override, or special cases
984m// and s/// which do not do what one would expect them to do.
985
986Help with best setup of these faces for printout requested (for each of
987the faces: please specify bold, italic, underline, shadow and box.)
988
989In regular expressions (including character classes):
990 `font-lock-string-face' \"Normal\" stuff and non-0-length constructs
991 `font-lock-constant-face': Delimiters
992 `font-lock-warning-face' Special-cased m// and s//foo/,
993 Mismatched closing delimiters, parens
994 we couldn't match, misplaced quantifiers,
995 unrecognized escape sequences
996 `cperl-nonoverridable-face' Modifiers, as gism in m/REx/gism
997 `font-lock-type-face' escape sequences with arguments (\\x \\23 \\p \\N)
998 and others match-a-char escape sequences
999 `font-lock-keyword-face' Capturing parens, and |
1000 `font-lock-function-name-face' Special symbols: $ ^ . [ ] [^ ] (?{ }) (??{ })
1001 \"Range -\" in character classes
1002 `font-lock-builtin-face' \"Remaining\" 0-length constructs, multipliers
1003 ?+*{}, not-capturing parens, leading
1004 backslashes of escape sequences
1005 `font-lock-variable-name-face' Interpolated constructs, embedded code,
1006 POSIX classes (inside charclasses)
1007 `font-lock-comment-face' Embedded comments
1008
1009")
1010
1011\f
1012
1013;;; Portability stuff:
1014
1015(defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
1016 `(define-key cperl-mode-map
1017 ,(if xemacs-key
1018 `(if (featurep 'xemacs) ,xemacs-key ,emacs-key)
1019 emacs-key)
1020 ,definition))
1021
1022(defvar cperl-del-back-ch
1023 (car (append (where-is-internal 'delete-backward-char)
1024 (where-is-internal 'backward-delete-char-untabify)))
1025 "Character generated by key bound to `delete-backward-char'.")
1026
1027(and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
1028 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
1029
1030(defun cperl-mark-active () (mark)) ; Avoid undefined warning
1031(if (featurep 'xemacs)
1032 (progn
1033 ;; "Active regions" are on: use region only if active
1034 ;; "Active regions" are off: use region unconditionally
1035 (defun cperl-use-region-p ()
1036 (if zmacs-regions (mark) t)))
1037 (defun cperl-use-region-p ()
1038 (if transient-mark-mode mark-active t))
1039 (defun cperl-mark-active () mark-active))
1040
1041(defsubst cperl-enable-font-lock ()
1042 cperl-can-font-lock)
1043
1044(defun cperl-putback-char (c) ; Emacs 19
1045 (set 'unread-command-events (list c))) ; Avoid undefined warning
1046
1047(if (featurep 'xemacs)
1048 (defun cperl-putback-char (c) ; XEmacs >= 19.12
1049 (setq unread-command-events (list (eval '(character-to-event c))))))
1050
1051(or (fboundp 'uncomment-region)
1052 (defun uncomment-region (beg end)
1053 (interactive "r")
1054 (comment-region beg end -1)))
1055
1056(defvar cperl-do-not-fontify
1057 (if (string< emacs-version "19.30")
1058 'fontified
1059 'lazy-lock)
1060 "Text property which inhibits refontification.")
1061
1062(defsubst cperl-put-do-not-fontify (from to &optional post)
1063 ;; If POST, do not do it with postponed fontification
1064 (if (and post cperl-syntaxify-by-font-lock)
1065 nil
1066 (put-text-property (max (point-min) (1- from))
1067 to cperl-do-not-fontify t)))
1068
1069(defcustom cperl-mode-hook nil
1070 "Hook run by CPerl mode."
1071 :type 'hook
1072 :group 'cperl)
1073
1074(defvar cperl-syntax-state nil)
1075(defvar cperl-syntax-done-to nil)
1076(defvar cperl-emacs-can-parse (> (length (save-excursion
1077 (parse-partial-sexp (point) (point)))) 9))
1078\f
1079;; Make customization possible "in reverse"
1080(defsubst cperl-val (symbol &optional default hairy)
1081 (cond
1082 ((eq (symbol-value symbol) 'null) default)
1083 (cperl-hairy (or hairy t))
1084 (t (symbol-value symbol))))
1085\f
1086
1087(defun cperl-make-indent (column &optional minimum keep)
1088 "Makes indent of the current line the requested amount.
1089Unless KEEP, removes the old indentation. Works around a bug in ancient
1090versions of Emacs."
1091 (let ((prop (get-text-property (point) 'syntax-type)))
1092 (or keep
1093 (delete-horizontal-space))
1094 (indent-to column minimum)
1095 ;; In old versions (e.g., 19.33) `indent-to' would not inherit properties
1096 (and prop
1097 (> (current-column) 0)
1098 (save-excursion
1099 (beginning-of-line)
1100 (or (get-text-property (point) 'syntax-type)
1101 (and (looking-at "\\=[ \t]")
1102 (put-text-property (point) (match-end 0)
1103 'syntax-type prop)))))))
1104
1105;;; Probably it is too late to set these guys already, but it can help later:
1106
1107;;;(and cperl-clobber-mode-lists
1108;;;(setq auto-mode-alist
1109;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1110;;;(and (boundp 'interpreter-mode-alist)
1111;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1112;;; '(("miniperl" . perl-mode))))))
1113(eval-when-compile
1114 (mapc (lambda (p)
1115 (condition-case nil
1116 (require p)
1117 (error nil)))
1118 '(imenu easymenu etags timer man info))
1119 (if (fboundp 'ps-extend-face-list)
1120 (defmacro cperl-ps-extend-face-list (arg)
1121 `(ps-extend-face-list ,arg))
1122 (defmacro cperl-ps-extend-face-list (arg)
1123 `(error "This version of Emacs has no `ps-extend-face-list'")))
1124 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1125 ;; macros instead of defsubsts don't work on Emacs, so we do the
1126 ;; expansion manually. Any other suggestions?
1127 (require 'cl))
1128
1129(defvar cperl-mode-abbrev-table nil
1130 "Abbrev table in use in CPerl mode buffers.")
1131
1132(add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1133
1134(defvar cperl-mode-map () "Keymap used in CPerl mode.")
1135
1136(if cperl-mode-map nil
1137 (setq cperl-mode-map (make-sparse-keymap))
1138 (cperl-define-key "{" 'cperl-electric-lbrace)
1139 (cperl-define-key "[" 'cperl-electric-paren)
1140 (cperl-define-key "(" 'cperl-electric-paren)
1141 (cperl-define-key "<" 'cperl-electric-paren)
1142 (cperl-define-key "}" 'cperl-electric-brace)
1143 (cperl-define-key "]" 'cperl-electric-rparen)
1144 (cperl-define-key ")" 'cperl-electric-rparen)
1145 (cperl-define-key ";" 'cperl-electric-semi)
1146 (cperl-define-key ":" 'cperl-electric-terminator)
1147 (cperl-define-key "\C-j" 'newline-and-indent)
1148 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1149 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1150 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1151 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1152 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1153 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1154 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1155 (cperl-define-key "\C-c\C-b" 'cperl-find-bad-style)
1156 (cperl-define-key "\C-c\C-p" 'cperl-pod-spell)
1157 (cperl-define-key "\C-c\C-d" 'cperl-here-doc-spell)
1158 (cperl-define-key "\C-c\C-n" 'cperl-narrow-to-here-doc)
1159 (cperl-define-key "\C-c\C-v" 'cperl-next-interpolated-REx)
1160 (cperl-define-key "\C-c\C-x" 'cperl-next-interpolated-REx-0)
1161 (cperl-define-key "\C-c\C-y" 'cperl-next-interpolated-REx-1)
1162 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1163 (cperl-define-key "\C-c\C-hp" 'cperl-perldoc)
1164 (cperl-define-key "\C-c\C-hP" 'cperl-perldoc-at-point)
1165 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1166 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1167 [(control meta |)])
1168 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1169 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1170 (cperl-define-key "\177" 'cperl-electric-backspace)
1171 (cperl-define-key "\t" 'cperl-indent-command)
1172 ;; don't clobber the backspace binding:
1173 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1174 [(control c) (control h) F])
1175 (if (cperl-val 'cperl-clobber-lisp-bindings)
1176 (progn
1177 (cperl-define-key "\C-hf"
1178 ;;(concat (char-to-string help-char) "f") ; does not work
1179 'cperl-info-on-command
1180 [(control h) f])
1181 (cperl-define-key "\C-hv"
1182 ;;(concat (char-to-string help-char) "v") ; does not work
1183 'cperl-get-help
1184 [(control h) v])
1185 (cperl-define-key "\C-c\C-hf"
1186 ;;(concat (char-to-string help-char) "f") ; does not work
1187 (key-binding "\C-hf")
1188 [(control c) (control h) f])
1189 (cperl-define-key "\C-c\C-hv"
1190 ;;(concat (char-to-string help-char) "v") ; does not work
1191 (key-binding "\C-hv")
1192 [(control c) (control h) v]))
1193 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1194 [(control c) (control h) f])
1195 (cperl-define-key "\C-c\C-hv"
1196 ;;(concat (char-to-string help-char) "v") ; does not work
1197 'cperl-get-help
1198 [(control c) (control h) v]))
1199 (if (and (featurep 'xemacs)
1200 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1201 (progn
1202 ;; substitute-key-definition is usefulness-deenhanced...
1203 ;;;;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1204 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1205 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1206 (or (boundp 'fill-paragraph-function)
1207 (substitute-key-definition
1208 'fill-paragraph 'cperl-fill-paragraph
1209 cperl-mode-map global-map))
1210 (substitute-key-definition
1211 'indent-sexp 'cperl-indent-exp
1212 cperl-mode-map global-map)
1213 (substitute-key-definition
1214 'indent-region 'cperl-indent-region
1215 cperl-mode-map global-map)
1216 (substitute-key-definition
1217 'indent-for-comment 'cperl-indent-for-comment
1218 cperl-mode-map global-map)))
1219
1220(defvar cperl-menu)
1221(defvar cperl-lazy-installed)
1222(defvar cperl-old-style nil)
1223(condition-case nil
1224 (progn
1225 (require 'easymenu)
1226 (easy-menu-define
1227 cperl-menu cperl-mode-map "Menu for CPerl mode"
1228 '("Perl"
1229 ["Beginning of function" beginning-of-defun t]
1230 ["End of function" end-of-defun t]
1231 ["Mark function" mark-defun t]
1232 ["Indent expression" cperl-indent-exp t]
1233 ["Fill paragraph/comment" fill-paragraph t]
1234 "----"
1235 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1236 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1237 ("Regexp"
1238 ["Beautify" cperl-beautify-regexp
1239 cperl-use-syntax-table-text-property]
1240 ["Beautify one level deep" (cperl-beautify-regexp 1)
1241 cperl-use-syntax-table-text-property]
1242 ["Beautify a group" cperl-beautify-level
1243 cperl-use-syntax-table-text-property]
1244 ["Beautify a group one level deep" (cperl-beautify-level 1)
1245 cperl-use-syntax-table-text-property]
1246 ["Contract a group" cperl-contract-level
1247 cperl-use-syntax-table-text-property]
1248 ["Contract groups" cperl-contract-levels
1249 cperl-use-syntax-table-text-property]
1250 "----"
1251 ["Find next interpolated" cperl-next-interpolated-REx
1252 (next-single-property-change (point-min) 'REx-interpolated)]
1253 ["Find next interpolated (no //o)"
1254 cperl-next-interpolated-REx-0
1255 (or (text-property-any (point-min) (point-max) 'REx-interpolated t)
1256 (text-property-any (point-min) (point-max) 'REx-interpolated 1))]
1257 ["Find next interpolated (neither //o nor whole-REx)"
1258 cperl-next-interpolated-REx-1
1259 (text-property-any (point-min) (point-max) 'REx-interpolated t)])
1260 ["Insert spaces if needed to fix style" cperl-find-bad-style t]
1261 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1262 "----"
1263 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1264 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1265 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1266 "----"
1267 ["Run" mode-compile (fboundp 'mode-compile)]
1268 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1269 (get-buffer "*compilation*"))]
1270 ["Next error" next-error (get-buffer "*compilation*")]
1271 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1272 "----"
1273 ["Debugger" cperl-db t]
1274 "----"
1275 ("Tools"
1276 ["Imenu" imenu (fboundp 'imenu)]
1277 ["Imenu on Perl Info" cperl-imenu-on-info (featurep 'imenu)]
1278 "----"
1279 ["Ispell PODs" cperl-pod-spell
1280 ;; Better not to update syntaxification here:
1281 ;; debugging syntaxificatio can be broken by this???
1282 (or
1283 (get-text-property (point-min) 'in-pod)
1284 (< (progn
1285 (and cperl-syntaxify-for-menu
1286 (cperl-update-syntaxification (point-max) (point-max)))
1287 (next-single-property-change (point-min) 'in-pod nil (point-max)))
1288 (point-max)))]
1289 ["Ispell HERE-DOCs" cperl-here-doc-spell
1290 (< (progn
1291 (and cperl-syntaxify-for-menu
1292 (cperl-update-syntaxification (point-max) (point-max)))
1293 (next-single-property-change (point-min) 'here-doc-group nil (point-max)))
1294 (point-max))]
1295 ["Narrow to this HERE-DOC" cperl-narrow-to-here-doc
1296 (eq 'here-doc (progn
1297 (and cperl-syntaxify-for-menu
1298 (cperl-update-syntaxification (point) (point)))
1299 (get-text-property (point) 'syntax-type)))]
1300 ["Select this HERE-DOC or POD section"
1301 cperl-select-this-pod-or-here-doc
1302 (memq (progn
1303 (and cperl-syntaxify-for-menu
1304 (cperl-update-syntaxification (point) (point)))
1305 (get-text-property (point) 'syntax-type))
1306 '(here-doc pod))]
1307 "----"
1308 ["CPerl pretty print (exprmntl)" cperl-ps-print
1309 (fboundp 'ps-extend-face-list)]
1310 "----"
1311 ["Syntaxify region" cperl-find-pods-heres-region
1312 (cperl-use-region-p)]
1313 ["Profile syntaxification" cperl-time-fontification t]
1314 ["Debug errors in delayed fontification" cperl-emulate-lazy-lock t]
1315 ["Debug unwind for syntactic scan" cperl-toggle-set-debug-unwind t]
1316 ["Debug backtrace on syntactic scan (BEWARE!!!)"
1317 (cperl-toggle-set-debug-unwind nil t) t]
1318 "----"
1319 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1320 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1321 ("Tags"
1322;;; ["Create tags for current file" cperl-etags t]
1323;;; ["Add tags for current file" (cperl-etags t) t]
1324;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1325;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1326;;; ["Create tags for Perl files in (sub)directories"
1327;;; (cperl-etags nil 'recursive) t]
1328;;; ["Add tags for Perl files in (sub)directories"
1329;;; (cperl-etags t 'recursive) t])
1330;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1331 ["Create tags for current file" (cperl-write-tags nil t) t]
1332 ["Add tags for current file" (cperl-write-tags) t]
1333 ["Create tags for Perl files in directory"
1334 (cperl-write-tags nil t nil t) t]
1335 ["Add tags for Perl files in directory"
1336 (cperl-write-tags nil nil nil t) t]
1337 ["Create tags for Perl files in (sub)directories"
1338 (cperl-write-tags nil t t t) t]
1339 ["Add tags for Perl files in (sub)directories"
1340 (cperl-write-tags nil nil t t) t]))
1341 ("Perl docs"
1342 ["Define word at point" imenu-go-find-at-position
1343 (fboundp 'imenu-go-find-at-position)]
1344 ["Help on function" cperl-info-on-command t]
1345 ["Help on function at point" cperl-info-on-current-command t]
1346 ["Help on symbol at point" cperl-get-help t]
1347 ["Perldoc" cperl-perldoc t]
1348 ["Perldoc on word at point" cperl-perldoc-at-point t]
1349 ["View manpage of POD in this file" cperl-build-manpage t]
1350 ["Auto-help on" cperl-lazy-install
1351 (and (fboundp 'run-with-idle-timer)
1352 (not cperl-lazy-installed))]
1353 ["Auto-help off" cperl-lazy-unstall
1354 (and (fboundp 'run-with-idle-timer)
1355 cperl-lazy-installed)])
1356 ("Toggle..."
1357 ["Auto newline" cperl-toggle-auto-newline t]
1358 ["Electric parens" cperl-toggle-electric t]
1359 ["Electric keywords" cperl-toggle-abbrev t]
1360 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1361 ["Auto-help on Perl constructs" cperl-toggle-autohelp t]
1362 ["Auto fill" auto-fill-mode t])
1363 ("Indent styles..."
1364 ["CPerl" (cperl-set-style "CPerl") t]
1365 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1366 ["GNU" (cperl-set-style "GNU") t]
1367 ["C++" (cperl-set-style "C++") t]
1368 ["K&R" (cperl-set-style "K&R") t]
1369 ["BSD" (cperl-set-style "BSD") t]
1370 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1371 ["Memorize Current" (cperl-set-style "Current") t]
1372 ["Memorized" (cperl-set-style-back) cperl-old-style])
1373 ("Micro-docs"
1374 ["Tips" (describe-variable 'cperl-tips) t]
1375 ["Problems" (describe-variable 'cperl-problems) t]
1376 ["Speed" (describe-variable 'cperl-speed) t]
1377 ["Praise" (describe-variable 'cperl-praise) t]
1378 ["Faces" (describe-variable 'cperl-tips-faces) t]
1379 ["CPerl mode" (describe-function 'cperl-mode) t]
1380 ["CPerl version"
1381 (message "The version of master-file for this CPerl is %s-Emacs"
1382 cperl-version) t]))))
1383 (error nil))
1384
1385(autoload 'c-macro-expand "cmacexp"
1386 "Display the result of expanding all C macros occurring in the region.
1387The expansion is entirely correct because it uses the C preprocessor."
1388 t)
1389
1390;;; These two must be unwound, otherwise take exponential time
1391(defconst cperl-maybe-white-and-comment-rex "[ \t\n]*\\(#[^\n]*\n[ \t\n]*\\)*"
1392"Regular expression to match optional whitespace with interpspersed comments.
1393Should contain exactly one group.")
1394
1395;;; This one is tricky to unwind; still very inefficient...
1396(defconst cperl-white-and-comment-rex "\\([ \t\n]\\|#[^\n]*\n\\)+"
1397"Regular expression to match whitespace with interpspersed comments.
1398Should contain exactly one group.")
1399
1400
1401;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
1402;;; `cperl-outline-regexp', `defun-prompt-regexp'.
1403;;; Details of groups in this may be used in several functions; see comments
1404;;; near mentioned above variable(s)...
1405;;; sub($$):lvalue{} sub:lvalue{} Both allowed...
1406(defsubst cperl-after-sub-regexp (named attr) ; 9 groups without attr...
1407 "Match the text after `sub' in a subroutine declaration.
1408If NAMED is nil, allows anonymous subroutines. Matches up to the first \":\"
1409of attributes (if present), or end of the name or prototype (whatever is
1410the last)."
1411 (concat ; Assume n groups before this...
1412 "\\(" ; n+1=name-group
1413 cperl-white-and-comment-rex ; n+2=pre-name
1414 "\\(::[a-zA-Z_0-9:']+\\|[a-zA-Z_'][a-zA-Z_0-9:']*\\)" ; n+3=name
1415 "\\)" ; END n+1=name-group
1416 (if named "" "?")
1417 "\\(" ; n+4=proto-group
1418 cperl-maybe-white-and-comment-rex ; n+5=pre-proto
1419 "\\(([^()]*)\\)" ; n+6=prototype
1420 "\\)?" ; END n+4=proto-group
1421 "\\(" ; n+7=attr-group
1422 cperl-maybe-white-and-comment-rex ; n+8=pre-attr
1423 "\\(" ; n+9=start-attr
1424 ":"
1425 (if attr (concat
1426 "\\("
1427 cperl-maybe-white-and-comment-rex ; whitespace-comments
1428 "\\(\\sw\\|_\\)+" ; attr-name
1429 ;; attr-arg (1 level of internal parens allowed!)
1430 "\\((\\(\\\\.\\|[^\\\\()]\\|([^\\\\()]*)\\)*)\\)?"
1431 "\\(" ; optional : (XXX allows trailing???)
1432 cperl-maybe-white-and-comment-rex ; whitespace-comments
1433 ":\\)?"
1434 "\\)+")
1435 "[^:]")
1436 "\\)"
1437 "\\)?" ; END n+6=proto-group
1438 ))
1439
1440;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
1441;;; and `cperl-outline-level'.
1442;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
1443(defvar cperl-imenu--function-name-regexp-perl
1444 (concat
1445 "^\\(" ; 1 = all
1446 "\\([ \t]*package" ; 2 = package-group
1447 "\\(" ; 3 = package-name-group
1448 cperl-white-and-comment-rex ; 4 = pre-package-name
1449 "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
1450 "\\|"
1451 "[ \t]*sub"
1452 (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1453 cperl-maybe-white-and-comment-rex ; 15=pre-block
1454 "\\|"
1455 "=head\\([1-4]\\)[ \t]+" ; 16=level
1456 "\\([^\n]+\\)$" ; 17=text
1457 "\\)"))
1458
1459(defvar cperl-outline-regexp
1460 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1461
1462(defvar cperl-mode-syntax-table nil
1463 "Syntax table in use in CPerl mode buffers.")
1464
1465(defvar cperl-string-syntax-table nil
1466 "Syntax table in use in CPerl mode string-like chunks.")
1467
1468(defsubst cperl-1- (p)
1469 (max (point-min) (1- p)))
1470
1471(defsubst cperl-1+ (p)
1472 (min (point-max) (1+ p)))
1473
1474(if cperl-mode-syntax-table
1475 ()
1476 (setq cperl-mode-syntax-table (make-syntax-table))
1477 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1478 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1479 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1480 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1481 (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 ?\n ">" cperl-mode-syntax-table)
1489 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1490 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1491 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1492 (if cperl-under-as-char
1493 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1494 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1495 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1496 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1497 (modify-syntax-entry ?$ "." cperl-string-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)) ; (?# comment )
1504
1505
1506\f
1507(defvar cperl-faces-init nil)
1508;; Fix for msb.el
1509(defvar cperl-msb-fixed nil)
1510(defvar cperl-use-major-mode 'cperl-mode)
1511(defvar cperl-font-lock-multiline-start nil)
1512(defvar cperl-font-lock-multiline nil)
1513(defvar cperl-font-locking nil)
1514
1515;; NB as it stands the code in cperl-mode assumes this only has one
1516;; element. If Xemacs 19 support were dropped, this could all be simplified.
1517(defvar cperl-compilation-error-regexp-alist
1518 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORKS).
1519 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
1520 2 3))
1521 "Alist that specifies how to match errors in perl output.")
1522
1523(defvar compilation-error-regexp-alist)
1524
1525;;;###autoload
1526(defun cperl-mode ()
1527 "Major mode for editing Perl code.
1528Expression and list commands understand all C brackets.
1529Tab indents for Perl code.
1530Paragraphs are separated by blank lines only.
1531Delete converts tabs to spaces as it moves back.
1532
1533Various characters in Perl almost always come in pairs: {}, (), [],
1534sometimes <>. When the user types the first, she gets the second as
1535well, with optional special formatting done on {}. (Disabled by
1536default.) You can always quote (with \\[quoted-insert]) the left
1537\"paren\" to avoid the expansion. The processing of < is special,
1538since most the time you mean \"less\". CPerl mode tries to guess
1539whether you want to type pair <>, and inserts is if it
1540appropriate. You can set `cperl-electric-parens-string' to the string that
1541contains the parenths from the above list you want to be electrical.
1542Electricity of parenths is controlled by `cperl-electric-parens'.
1543You may also set `cperl-electric-parens-mark' to have electric parens
1544look for active mark and \"embrace\" a region if possible.'
1545
1546CPerl mode provides expansion of the Perl control constructs:
1547
1548 if, else, elsif, unless, while, until, continue, do,
1549 for, foreach, formy and foreachmy.
1550
1551and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1552
1553The user types the keyword immediately followed by a space, which
1554causes the construct to be expanded, and the point is positioned where
1555she is most likely to want to be. eg. when the user types a space
1556following \"if\" the following appears in the buffer: if () { or if ()
1557} { } and the cursor is between the parentheses. The user can then
1558type some boolean expression within the parens. Having done that,
1559typing \\[cperl-linefeed] places you - appropriately indented - on a
1560new line between the braces (if you typed \\[cperl-linefeed] in a POD
1561directive line, then appropriate number of new lines is inserted).
1562
1563If CPerl decides that you want to insert \"English\" style construct like
1564
1565 bite if angry;
1566
1567it will not do any expansion. See also help on variable
1568`cperl-extra-newline-before-brace'. (Note that one can switch the
1569help message on expansion by setting `cperl-message-electric-keyword'
1570to nil.)
1571
1572\\[cperl-linefeed] is a convenience replacement for typing carriage
1573return. It places you in the next line with proper indentation, or if
1574you type it inside the inline block of control construct, like
1575
1576 foreach (@lines) {print; print}
1577
1578and you are on a boundary of a statement inside braces, it will
1579transform the construct into a multiline and will place you into an
1580appropriately indented blank line. If you need a usual
1581`newline-and-indent' behavior, it is on \\[newline-and-indent],
1582see documentation on `cperl-electric-linefeed'.
1583
1584Use \\[cperl-invert-if-unless] to change a construction of the form
1585
1586 if (A) { B }
1587
1588into
1589
1590 B if A;
1591
1592\\{cperl-mode-map}
1593
1594Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1595\(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1596on electric space between $ and {, `cperl-electric-parens-string' is
1597the string that contains parentheses that should be electric in CPerl
1598\(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1599setting `cperl-electric-keywords' enables electric expansion of
1600control structures in CPerl. `cperl-electric-linefeed' governs which
1601one of two linefeed behavior is preferable. You can enable all these
1602options simultaneously (recommended mode of use) by setting
1603`cperl-hairy' to t. In this case you can switch separate options off
1604by setting them to `null'. Note that one may undo the extra
1605whitespace inserted by semis and braces in `auto-newline'-mode by
1606consequent \\[cperl-electric-backspace].
1607
1608If your site has perl5 documentation in info format, you can use commands
1609\\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1610These keys run commands `cperl-info-on-current-command' and
1611`cperl-info-on-command', which one is which is controlled by variable
1612`cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1613\(in turn affected by `cperl-hairy').
1614
1615Even if you have no info-format documentation, short one-liner-style
1616help is available on \\[cperl-get-help], and one can run perldoc or
1617man via menu.
1618
1619It is possible to show this help automatically after some idle time.
1620This is regulated by variable `cperl-lazy-help-time'. Default with
1621`cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1622secs idle time . It is also possible to switch this on/off from the
1623menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1624
1625Use \\[cperl-lineup] to vertically lineup some construction - put the
1626beginning of the region at the start of construction, and make region
1627span the needed amount of lines.
1628
1629Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1630`cperl-pod-face', `cperl-pod-head-face' control processing of POD and
1631here-docs sections. With capable Emaxen results of scan are used
1632for indentation too, otherwise they are used for highlighting only.
1633
1634Variables controlling indentation style:
1635 `cperl-tab-always-indent'
1636 Non-nil means TAB in CPerl mode should always reindent the current line,
1637 regardless of where in the line point is when the TAB command is used.
1638 `cperl-indent-left-aligned-comments'
1639 Non-nil means that the comment starting in leftmost column should indent.
1640 `cperl-auto-newline'
1641 Non-nil means automatically newline before and after braces,
1642 and after colons and semicolons, inserted in Perl code. The following
1643 \\[cperl-electric-backspace] will remove the inserted whitespace.
1644 Insertion after colons requires both this variable and
1645 `cperl-auto-newline-after-colon' set.
1646 `cperl-auto-newline-after-colon'
1647 Non-nil means automatically newline even after colons.
1648 Subject to `cperl-auto-newline' setting.
1649 `cperl-indent-level'
1650 Indentation of Perl statements within surrounding block.
1651 The surrounding block's indentation is the indentation
1652 of the line on which the open-brace appears.
1653 `cperl-continued-statement-offset'
1654 Extra indentation given to a substatement, such as the
1655 then-clause of an if, or body of a while, or just a statement continuation.
1656 `cperl-continued-brace-offset'
1657 Extra indentation given to a brace that starts a substatement.
1658 This is in addition to `cperl-continued-statement-offset'.
1659 `cperl-brace-offset'
1660 Extra indentation for line if it starts with an open brace.
1661 `cperl-brace-imaginary-offset'
1662 An open brace following other text is treated as if it the line started
1663 this far to the right of the actual line indentation.
1664 `cperl-label-offset'
1665 Extra indentation for line that is a label.
1666 `cperl-min-label-indent'
1667 Minimal indentation for line that is a label.
1668
1669Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
1670 `cperl-indent-level' 5 4 2 4
1671 `cperl-brace-offset' 0 0 0 0
1672 `cperl-continued-brace-offset' -5 -4 0 0
1673 `cperl-label-offset' -5 -4 -2 -4
1674 `cperl-continued-statement-offset' 5 4 2 4
1675
1676CPerl knows several indentation styles, and may bulk set the
1677corresponding variables. Use \\[cperl-set-style] to do this. Use
1678\\[cperl-set-style-back] to restore the memorized preexisting values
1679\(both available from menu). See examples in `cperl-style-examples'.
1680
1681Part of the indentation style is how different parts of if/elsif/else
1682statements are broken into lines; in CPerl, this is reflected on how
1683templates for these constructs are created (controlled by
1684`cperl-extra-newline-before-brace'), and how reflow-logic should treat
1685\"continuation\" blocks of else/elsif/continue, controlled by the same
1686variable, and by `cperl-extra-newline-before-brace-multiline',
1687`cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.
1688
1689If `cperl-indent-level' is 0, the statement after opening brace in
1690column 0 is indented on
1691`cperl-brace-offset'+`cperl-continued-statement-offset'.
1692
1693Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1694with no args.
1695
1696DO NOT FORGET to read micro-docs (available from `Perl' menu)
1697or as help on variables `cperl-tips', `cperl-problems',
1698`cperl-praise', `cperl-speed'."
1699 (interactive)
1700 (kill-all-local-variables)
1701 (use-local-map cperl-mode-map)
1702 (if (cperl-val 'cperl-electric-linefeed)
1703 (progn
1704 (local-set-key "\C-J" 'cperl-linefeed)
1705 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1706 (if (and
1707 (cperl-val 'cperl-clobber-lisp-bindings)
1708 (cperl-val 'cperl-info-on-command-no-prompt))
1709 (progn
1710 ;; don't clobber the backspace binding:
1711 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1712 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1713 [(control c) (control h) f])))
1714 (setq major-mode cperl-use-major-mode)
1715 (setq mode-name "CPerl")
1716 (let ((prev-a-c abbrevs-changed))
1717 (define-abbrev-table 'cperl-mode-abbrev-table '(
1718 ("if" "if" cperl-electric-keyword 0)
1719 ("elsif" "elsif" cperl-electric-keyword 0)
1720 ("while" "while" cperl-electric-keyword 0)
1721 ("until" "until" cperl-electric-keyword 0)
1722 ("unless" "unless" cperl-electric-keyword 0)
1723 ("else" "else" cperl-electric-else 0)
1724 ("continue" "continue" cperl-electric-else 0)
1725 ("for" "for" cperl-electric-keyword 0)
1726 ("foreach" "foreach" cperl-electric-keyword 0)
1727 ("formy" "formy" cperl-electric-keyword 0)
1728 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1729 ("do" "do" cperl-electric-keyword 0)
1730 ("=pod" "=pod" cperl-electric-pod 0)
1731 ("=over" "=over" cperl-electric-pod 0)
1732 ("=head1" "=head1" cperl-electric-pod 0)
1733 ("=head2" "=head2" cperl-electric-pod 0)
1734 ("pod" "pod" cperl-electric-pod 0)
1735 ("over" "over" cperl-electric-pod 0)
1736 ("head1" "head1" cperl-electric-pod 0)
1737 ("head2" "head2" cperl-electric-pod 0)))
1738 (setq abbrevs-changed prev-a-c))
1739 (setq local-abbrev-table cperl-mode-abbrev-table)
1740 (if (cperl-val 'cperl-electric-keywords)
1741 (abbrev-mode 1))
1742 (set-syntax-table cperl-mode-syntax-table)
1743 ;; Until Emacs is multi-threaded, we do not actually need it local:
1744 (make-local-variable 'cperl-font-lock-multiline-start)
1745 (make-local-variable 'cperl-font-locking)
1746 (make-local-variable 'outline-regexp)
1747 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1748 (setq outline-regexp cperl-outline-regexp)
1749 (make-local-variable 'outline-level)
1750 (setq outline-level 'cperl-outline-level)
1751 (make-local-variable 'paragraph-start)
1752 (setq paragraph-start (concat "^$\\|" page-delimiter))
1753 (make-local-variable 'paragraph-separate)
1754 (setq paragraph-separate paragraph-start)
1755 (make-local-variable 'paragraph-ignore-fill-prefix)
1756 (setq paragraph-ignore-fill-prefix t)
1757 (if (featurep 'xemacs)
1758 (progn
1759 (make-local-variable 'paren-backwards-message)
1760 (set 'paren-backwards-message t)))
1761 (make-local-variable 'indent-line-function)
1762 (setq indent-line-function 'cperl-indent-line)
1763 (make-local-variable 'require-final-newline)
1764 (setq require-final-newline mode-require-final-newline)
1765 (make-local-variable 'comment-start)
1766 (setq comment-start "# ")
1767 (make-local-variable 'comment-end)
1768 (setq comment-end "")
1769 (make-local-variable 'comment-column)
1770 (setq comment-column cperl-comment-column)
1771 (make-local-variable 'comment-start-skip)
1772 (setq comment-start-skip "#+ *")
1773 (make-local-variable 'defun-prompt-regexp)
1774;;; "[ \t]*sub"
1775;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
1776;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
1777 (setq defun-prompt-regexp
1778 (concat "^[ \t]*\\(sub"
1779 (cperl-after-sub-regexp 'named 'attr-groups)
1780 "\\|" ; per toke.c
1781 "\\(BEGIN\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
1782 "\\)"
1783 cperl-maybe-white-and-comment-rex))
1784 (make-local-variable 'comment-indent-function)
1785 (setq comment-indent-function 'cperl-comment-indent)
1786 (and (boundp 'fill-paragraph-function)
1787 (progn
1788 (make-local-variable 'fill-paragraph-function)
1789 (set 'fill-paragraph-function 'cperl-fill-paragraph)))
1790 (make-local-variable 'parse-sexp-ignore-comments)
1791 (setq parse-sexp-ignore-comments t)
1792 (make-local-variable 'indent-region-function)
1793 (setq indent-region-function 'cperl-indent-region)
1794 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1795 (make-local-variable 'imenu-create-index-function)
1796 (setq imenu-create-index-function
1797 (function cperl-imenu--create-perl-index))
1798 (make-local-variable 'imenu-sort-function)
1799 (setq imenu-sort-function nil)
1800 (make-local-variable 'vc-rcs-header)
1801 (set 'vc-rcs-header cperl-vc-rcs-header)
1802 (make-local-variable 'vc-sccs-header)
1803 (set 'vc-sccs-header cperl-vc-sccs-header)
1804 ;; This one is obsolete...
1805 (make-local-variable 'vc-header-alist)
1806 (with-no-warnings
1807 (set 'vc-header-alist (or cperl-vc-header-alist ; Avoid warning
1808 `((SCCS ,(car cperl-vc-sccs-header))
1809 (RCS ,(car cperl-vc-rcs-header)))))
1810 )
1811 (cond ((boundp 'compilation-error-regexp-alist-alist);; xemacs 20.x
1812 (make-local-variable 'compilation-error-regexp-alist-alist)
1813 (set 'compilation-error-regexp-alist-alist
1814 (cons (cons 'cperl (car cperl-compilation-error-regexp-alist))
1815 (symbol-value 'compilation-error-regexp-alist-alist)))
1816 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1817 (let ((f 'compilation-build-compilation-error-regexp-alist))
1818 (funcall f))
1819 (make-local-variable 'compilation-error-regexp-alist)
1820 (push 'cperl compilation-error-regexp-alist)))
1821 ((boundp 'compilation-error-regexp-alist);; xmeacs 19.x
1822 (make-local-variable 'compilation-error-regexp-alist)
1823 (set 'compilation-error-regexp-alist
1824 (append cperl-compilation-error-regexp-alist
1825 (symbol-value 'compilation-error-regexp-alist)))))
1826 (make-local-variable 'font-lock-defaults)
1827 (setq font-lock-defaults
1828 (cond
1829 ((string< emacs-version "19.30")
1830 '(cperl-font-lock-keywords-2 nil nil ((?_ . "w"))))
1831 ((string< emacs-version "19.33") ; Which one to use?
1832 '((cperl-font-lock-keywords
1833 cperl-font-lock-keywords-1
1834 cperl-font-lock-keywords-2) nil nil ((?_ . "w"))))
1835 (t
1836 '((cperl-load-font-lock-keywords
1837 cperl-load-font-lock-keywords-1
1838 cperl-load-font-lock-keywords-2) nil nil ((?_ . "w"))))))
1839 (make-local-variable 'cperl-syntax-state)
1840 (setq cperl-syntax-state nil) ; reset syntaxification cache
1841 (if cperl-use-syntax-table-text-property
1842 (progn
1843 (make-local-variable 'parse-sexp-lookup-properties)
1844 ;; Do not introduce variable if not needed, we check it!
1845 (set 'parse-sexp-lookup-properties t)
1846 ;; Fix broken font-lock:
1847 (or (boundp 'font-lock-unfontify-region-function)
1848 (set 'font-lock-unfontify-region-function
1849 'font-lock-default-unfontify-region))
1850 (unless (featurep 'xemacs) ; Our: just a plug for wrong font-lock
1851 (make-local-variable 'font-lock-unfontify-region-function)
1852 (set 'font-lock-unfontify-region-function ; not present with old Emacs
1853 'cperl-font-lock-unfontify-region-function))
1854 (make-local-variable 'cperl-syntax-done-to)
1855 (setq cperl-syntax-done-to nil) ; reset syntaxification cache
1856 (make-local-variable 'font-lock-syntactic-keywords)
1857 (setq font-lock-syntactic-keywords
1858 (if cperl-syntaxify-by-font-lock
1859 '((cperl-fontify-syntaxically))
1860 ;; unless font-lock-syntactic-keywords, font-lock (pre-22.1)
1861 ;; used to ignore syntax-table text-properties. (t) is a hack
1862 ;; to make font-lock think that font-lock-syntactic-keywords
1863 ;; are defined.
1864 '(t)))))
1865 (if (boundp 'font-lock-multiline) ; Newer font-lock; use its facilities
1866 (progn
1867 (setq cperl-font-lock-multiline t) ; Not localized...
1868 (set (make-local-variable 'font-lock-multiline) t))
1869 (make-local-variable 'font-lock-fontify-region-function)
1870 (set 'font-lock-fontify-region-function ; not present with old Emacs
1871 'cperl-font-lock-fontify-region-function))
1872 (make-local-variable 'font-lock-fontify-region-function)
1873 (set 'font-lock-fontify-region-function ; not present with old Emacs
1874 'cperl-font-lock-fontify-region-function)
1875 (make-local-variable 'cperl-old-style)
1876 (if (boundp 'normal-auto-fill-function) ; 19.33 and later
1877 (set (make-local-variable 'normal-auto-fill-function)
1878 'cperl-do-auto-fill)
1879 (or (fboundp 'cperl-old-auto-fill-mode)
1880 (progn
1881 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1882 (defun auto-fill-mode (&optional arg)
1883 (interactive "P")
1884 (eval '(cperl-old-auto-fill-mode arg)) ; Avoid a warning
1885 (and auto-fill-function (memq major-mode '(perl-mode cperl-mode))
1886 (setq auto-fill-function 'cperl-do-auto-fill))))))
1887 (if (cperl-enable-font-lock)
1888 (if (cperl-val 'cperl-font-lock)
1889 (progn (or cperl-faces-init (cperl-init-faces))
1890 (font-lock-mode 1))))
1891 (set (make-local-variable 'facemenu-add-face-function)
1892 'cperl-facemenu-add-face-function) ; XXXX What this guy is for???
1893 (and (boundp 'msb-menu-cond)
1894 (not cperl-msb-fixed)
1895 (cperl-msb-fix))
1896 (if (featurep 'easymenu)
1897 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1898 (run-mode-hooks 'cperl-mode-hook)
1899 (if cperl-hook-after-change
1900 (add-hook 'after-change-functions 'cperl-after-change-function nil t))
1901 ;; After hooks since fontification will break this
1902 (if cperl-pod-here-scan
1903 (or cperl-syntaxify-by-font-lock
1904 (progn (or cperl-faces-init (cperl-init-faces-weak))
1905 (cperl-find-pods-heres)))))
1906\f
1907;; Fix for perldb - make default reasonable
1908(defun cperl-db ()
1909 (interactive)
1910 (require 'gud)
1911 (perldb (read-from-minibuffer "Run perldb (like this): "
1912 (if (consp gud-perldb-history)
1913 (car gud-perldb-history)
1914 (concat "perl " ;;(file-name-nondirectory
1915 ;; I have problems
1916 ;; in OS/2
1917 ;; otherwise
1918 (buffer-file-name)))
1919 nil nil
1920 '(gud-perldb-history . 1))))
1921\f
1922(defun cperl-msb-fix ()
1923 ;; Adds perl files to msb menu, supposes that msb is already loaded
1924 (setq cperl-msb-fixed t)
1925 (let* ((l (length msb-menu-cond))
1926 (last (nth (1- l) msb-menu-cond))
1927 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1928 (handle (1- (nth 1 last))))
1929 (setcdr precdr (list
1930 (list
1931 '(memq major-mode '(cperl-mode perl-mode))
1932 handle
1933 "Perl Files (%d)")
1934 last))))
1935\f
1936;; This is used by indent-for-comment
1937;; to decide how much to indent a comment in CPerl code
1938;; based on its context. Do fallback if comment is found wrong.
1939
1940(defvar cperl-wrong-comment)
1941(defvar cperl-st-cfence '(14)) ; Comment-fence
1942(defvar cperl-st-sfence '(15)) ; String-fence
1943(defvar cperl-st-punct '(1))
1944(defvar cperl-st-word '(2))
1945(defvar cperl-st-bra '(4 . ?\>))
1946(defvar cperl-st-ket '(5 . ?\<))
1947
1948
1949(defun cperl-comment-indent () ; called at point at supposed comment
1950 (let ((p (point)) (c (current-column)) was phony)
1951 (if (and (not cperl-indent-comment-at-column-0)
1952 (looking-at "^#"))
1953 0 ; Existing comment at bol stays there.
1954 ;; Wrong comment found
1955 (save-excursion
1956 (setq was (cperl-to-comment-or-eol)
1957 phony (eq (get-text-property (point) 'syntax-table)
1958 cperl-st-cfence))
1959 (if phony
1960 (progn ; Too naive???
1961 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1962 (if (eq (preceding-char) ?\#)
1963 (forward-char -1))
1964 (setq was nil)))
1965 (if (= (point) p) ; Our caller found a correct place
1966 (progn
1967 (skip-chars-backward " \t")
1968 (setq was (current-column))
1969 (if (eq was 0)
1970 comment-column
1971 (max (1+ was) ; Else indent at comment column
1972 comment-column)))
1973 ;; No, the caller found a random place; we need to edit ourselves
1974 (if was nil
1975 (insert comment-start)
1976 (backward-char (length comment-start)))
1977 (setq cperl-wrong-comment t)
1978 (cperl-make-indent comment-column 1) ; Indent min 1
1979 c)))))
1980
1981;;;(defun cperl-comment-indent-fallback ()
1982;;; "Is called if the standard comment-search procedure fails.
1983;;;Point is at start of real comment."
1984;;; (let ((c (current-column)) target cnt prevc)
1985;;; (if (= c comment-column) nil
1986;;; (setq cnt (skip-chars-backward "[ \t]"))
1987;;; (setq target (max (1+ (setq prevc
1988;;; (current-column))) ; Else indent at comment column
1989;;; comment-column))
1990;;; (if (= c comment-column) nil
1991;;; (delete-backward-char cnt)
1992;;; (while (< prevc target)
1993;;; (insert "\t")
1994;;; (setq prevc (current-column)))
1995;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1996;;; (while (< prevc target)
1997;;; (insert " ")
1998;;; (setq prevc (current-column)))))))
1999
2000(defun cperl-indent-for-comment ()
2001 "Substitute for `indent-for-comment' in CPerl."
2002 (interactive)
2003 (let (cperl-wrong-comment)
2004 (indent-for-comment)
2005 (if cperl-wrong-comment ; set by `cperl-comment-indent'
2006 (progn (cperl-to-comment-or-eol)
2007 (forward-char (length comment-start))))))
2008
2009(defun cperl-comment-region (b e arg)
2010 "Comment or uncomment each line in the region in CPerl mode.
2011See `comment-region'."
2012 (interactive "r\np")
2013 (let ((comment-start "#"))
2014 (comment-region b e arg)))
2015
2016(defun cperl-uncomment-region (b e arg)
2017 "Uncomment or comment each line in the region in CPerl mode.
2018See `comment-region'."
2019 (interactive "r\np")
2020 (let ((comment-start "#"))
2021 (comment-region b e (- arg))))
2022
2023(defvar cperl-brace-recursing nil)
2024
2025(defun cperl-electric-brace (arg &optional only-before)
2026 "Insert character and correct line's indentation.
2027If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
2028place (even in empty line), but not after. If after \")\" and the inserted
2029char is \"{\", insert extra newline before only if
2030`cperl-extra-newline-before-brace'."
2031 (interactive "P")
2032 (let (insertpos
2033 (other-end (if (and cperl-electric-parens-mark
2034 (cperl-mark-active)
2035 (< (mark) (point)))
2036 (mark)
2037 nil)))
2038 (if (and other-end
2039 (not cperl-brace-recursing)
2040 (cperl-val 'cperl-electric-parens)
2041 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
2042 ;; Need to insert a matching pair
2043 (progn
2044 (save-excursion
2045 (setq insertpos (point-marker))
2046 (goto-char other-end)
2047 (setq last-command-event ?\{)
2048 (cperl-electric-lbrace arg insertpos))
2049 (forward-char 1))
2050 ;; Check whether we close something "usual" with `}'
2051 (if (and (eq last-command-event ?\})
2052 (not
2053 (condition-case nil
2054 (save-excursion
2055 (up-list (- (prefix-numeric-value arg)))
2056 ;;(cperl-after-block-p (point-min))
2057 (or (cperl-after-expr-p nil "{;)")
2058 ;; after sub, else, continue
2059 (cperl-after-block-p nil 'pre)))
2060 (error nil))))
2061 ;; Just insert the guy
2062 (self-insert-command (prefix-numeric-value arg))
2063 (if (and (not arg) ; No args, end (of empty line or auto)
2064 (eolp)
2065 (or (and (null only-before)
2066 (save-excursion
2067 (skip-chars-backward " \t")
2068 (bolp)))
2069 (and (eq last-command-event ?\{) ; Do not insert newline
2070 ;; if after ")" and `cperl-extra-newline-before-brace'
2071 ;; is nil, do not insert extra newline.
2072 (not cperl-extra-newline-before-brace)
2073 (save-excursion
2074 (skip-chars-backward " \t")
2075 (eq (preceding-char) ?\))))
2076 (if cperl-auto-newline
2077 (progn (cperl-indent-line) (newline) t) nil)))
2078 (progn
2079 (self-insert-command (prefix-numeric-value arg))
2080 (cperl-indent-line)
2081 (if cperl-auto-newline
2082 (setq insertpos (1- (point))))
2083 (if (and cperl-auto-newline (null only-before))
2084 (progn
2085 (newline)
2086 (cperl-indent-line)))
2087 (save-excursion
2088 (if insertpos (progn (goto-char insertpos)
2089 (search-forward (make-string
2090 1 last-command-event))
2091 (setq insertpos (1- (point)))))
2092 (delete-char -1))))
2093 (if insertpos
2094 (save-excursion
2095 (goto-char insertpos)
2096 (self-insert-command (prefix-numeric-value arg)))
2097 (self-insert-command (prefix-numeric-value arg)))))))
2098
2099(defun cperl-electric-lbrace (arg &optional end)
2100 "Insert character, correct line's indentation, correct quoting by space."
2101 (interactive "P")
2102 (let ((cperl-brace-recursing t)
2103 (cperl-auto-newline cperl-auto-newline)
2104 (other-end (or end
2105 (if (and cperl-electric-parens-mark
2106 (cperl-mark-active)
2107 (> (mark) (point)))
2108 (save-excursion
2109 (goto-char (mark))
2110 (point-marker))
2111 nil)))
2112 pos after)
2113 (and (cperl-val 'cperl-electric-lbrace-space)
2114 (eq (preceding-char) ?$)
2115 (save-excursion
2116 (skip-chars-backward "$")
2117 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
2118 (insert ?\s))
2119 ;; Check whether we are in comment
2120 (if (and
2121 (save-excursion
2122 (beginning-of-line)
2123 (not (looking-at "[ \t]*#")))
2124 (cperl-after-expr-p nil "{;)"))
2125 nil
2126 (setq cperl-auto-newline nil))
2127 (cperl-electric-brace arg)
2128 (and (cperl-val 'cperl-electric-parens)
2129 (eq last-command-event ?{)
2130 (memq last-command-event
2131 (append cperl-electric-parens-string nil))
2132 (or (if other-end (goto-char (marker-position other-end)))
2133 t)
2134 (setq last-command-event ?} pos (point))
2135 (progn (cperl-electric-brace arg t)
2136 (goto-char pos)))))
2137
2138(defun cperl-electric-paren (arg)
2139 "Insert an opening parenthesis or a matching pair of parentheses.
2140See `cperl-electric-parens'."
2141 (interactive "P")
2142 (let ((beg (save-excursion (beginning-of-line) (point)))
2143 (other-end (if (and cperl-electric-parens-mark
2144 (cperl-mark-active)
2145 (> (mark) (point)))
2146 (save-excursion
2147 (goto-char (mark))
2148 (point-marker))
2149 nil)))
2150 (if (and (cperl-val 'cperl-electric-parens)
2151 (memq last-command-event
2152 (append cperl-electric-parens-string nil))
2153 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2154 ;;(not (save-excursion (search-backward "#" beg t)))
2155 (if (eq last-command-event ?<)
2156 (progn
2157 (and abbrev-mode ; later it is too late, may be after `for'
2158 (expand-abbrev))
2159 (cperl-after-expr-p nil "{;(,:="))
2160 1))
2161 (progn
2162 (self-insert-command (prefix-numeric-value arg))
2163 (if other-end (goto-char (marker-position other-end)))
2164 (insert (make-string
2165 (prefix-numeric-value arg)
2166 (cdr (assoc last-command-event '((?{ .?})
2167 (?[ . ?])
2168 (?( . ?))
2169 (?< . ?>))))))
2170 (forward-char (- (prefix-numeric-value arg))))
2171 (self-insert-command (prefix-numeric-value arg)))))
2172
2173(defun cperl-electric-rparen (arg)
2174 "Insert a matching pair of parentheses if marking is active.
2175If not, or if we are not at the end of marking range, would self-insert.
2176Affected by `cperl-electric-parens'."
2177 (interactive "P")
2178 (let ((beg (save-excursion (beginning-of-line) (point)))
2179 (other-end (if (and cperl-electric-parens-mark
2180 (cperl-val 'cperl-electric-parens)
2181 (memq last-command-event
2182 (append cperl-electric-parens-string nil))
2183 (cperl-mark-active)
2184 (< (mark) (point)))
2185 (mark)
2186 nil))
2187 p)
2188 (if (and other-end
2189 (cperl-val 'cperl-electric-parens)
2190 (memq last-command-event '( ?\) ?\] ?\} ?\> ))
2191 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
2192 ;;(not (save-excursion (search-backward "#" beg t)))
2193 )
2194 (progn
2195 (self-insert-command (prefix-numeric-value arg))
2196 (setq p (point))
2197 (if other-end (goto-char other-end))
2198 (insert (make-string
2199 (prefix-numeric-value arg)
2200 (cdr (assoc last-command-event '((?\} . ?\{)
2201 (?\] . ?\[)
2202 (?\) . ?\()
2203 (?\> . ?\<))))))
2204 (goto-char (1+ p)))
2205 (self-insert-command (prefix-numeric-value arg)))))
2206
2207(defun cperl-electric-keyword ()
2208 "Insert a construction appropriate after a keyword.
2209Help message may be switched off by setting `cperl-message-electric-keyword'
2210to nil."
2211 (let ((beg (save-excursion (beginning-of-line) (point)))
2212 (dollar (and (eq last-command-event ?$)
2213 (eq this-command 'self-insert-command)))
2214 (delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2215 (memq this-command '(self-insert-command newline))))
2216 my do)
2217 (and (save-excursion
2218 (condition-case nil
2219 (progn
2220 (backward-sexp 1)
2221 (setq do (looking-at "do\\>")))
2222 (error nil))
2223 (cperl-after-expr-p nil "{;:"))
2224 (save-excursion
2225 (not
2226 (re-search-backward
2227 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2228 beg t)))
2229 (save-excursion (or (not (re-search-backward "^=" nil t))
2230 (or
2231 (looking-at "=cut")
2232 (and cperl-use-syntax-table-text-property
2233 (not (eq (get-text-property (point)
2234 'syntax-type)
2235 'pod))))))
2236 (save-excursion (forward-sexp -1)
2237 (not (memq (following-char) (append "$@%&*" nil))))
2238 (progn
2239 (and (eq (preceding-char) ?y)
2240 (progn ; "foreachmy"
2241 (forward-char -2)
2242 (insert " ")
2243 (forward-char 2)
2244 (setq my t dollar t
2245 delete
2246 (memq this-command '(self-insert-command newline)))))
2247 (and dollar (insert " $"))
2248 (cperl-indent-line)
2249 ;;(insert " () {\n}")
2250 (cond
2251 (cperl-extra-newline-before-brace
2252 (insert (if do "\n" " ()\n"))
2253 (insert "{")
2254 (cperl-indent-line)
2255 (insert "\n")
2256 (cperl-indent-line)
2257 (insert "\n}")
2258 (and do (insert " while ();")))
2259 (t
2260 (insert (if do " {\n} while ();" " () {\n}"))))
2261 (or (looking-at "[ \t]\\|$") (insert " "))
2262 (cperl-indent-line)
2263 (if dollar (progn (search-backward "$")
2264 (if my
2265 (forward-char 1)
2266 (delete-char 1)))
2267 (search-backward ")")
2268 (if (eq last-command-event ?\()
2269 (progn ; Avoid "if (())"
2270 (delete-backward-char 1)
2271 (delete-backward-char -1))))
2272 (if delete
2273 (cperl-putback-char cperl-del-back-ch))
2274 (if cperl-message-electric-keyword
2275 (message "Precede char by C-q to avoid expansion"))))))
2276
2277(defun cperl-ensure-newlines (n &optional pos)
2278 "Make sure there are N newlines after the point."
2279 (or pos (setq pos (point)))
2280 (if (looking-at "\n")
2281 (forward-char 1)
2282 (insert "\n"))
2283 (if (> n 1)
2284 (cperl-ensure-newlines (1- n) pos)
2285 (goto-char pos)))
2286
2287(defun cperl-electric-pod ()
2288 "Insert a POD chunk appropriate after a =POD directive."
2289 (let ((delete (and (memq last-command-event '(?\s ?\n ?\t ?\f))
2290 (memq this-command '(self-insert-command newline))))
2291 head1 notlast name p really-delete over)
2292 (and (save-excursion
2293 (forward-word -1)
2294 (and
2295 (eq (preceding-char) ?=)
2296 (progn
2297 (setq head1 (looking-at "head1\\>[ \t]*$"))
2298 (setq over (and (looking-at "over\\>[ \t]*$")
2299 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
2300 (forward-char -1)
2301 (bolp))
2302 (or
2303 (get-text-property (point) 'in-pod)
2304 (cperl-after-expr-p nil "{;:")
2305 (and (re-search-backward "\\(\\`\n?\\|^\n\\)=\\sw+" (point-min) t)
2306 (not (looking-at "\n*=cut"))
2307 (or (not cperl-use-syntax-table-text-property)
2308 (eq (get-text-property (point) 'syntax-type) 'pod))))))
2309 (progn
2310 (save-excursion
2311 (setq notlast (re-search-forward "^\n=" nil t)))
2312 (or notlast
2313 (progn
2314 (insert "\n\n=cut")
2315 (cperl-ensure-newlines 2)
2316 (forward-word -2)
2317 (if (and head1
2318 (not
2319 (save-excursion
2320 (forward-char -1)
2321 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2322 nil t)))) ; Only one
2323 (progn
2324 (forward-word 1)
2325 (setq name (file-name-sans-extension
2326 (file-name-nondirectory (buffer-file-name)))
2327 p (point))
2328 (insert " NAME\n\n" name
2329 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2330 "=head1 DESCRIPTION")
2331 (cperl-ensure-newlines 4)
2332 (goto-char p)
2333 (forward-word 2)
2334 (end-of-line)
2335 (setq really-delete t))
2336 (forward-word 1))))
2337 (if over
2338 (progn
2339 (setq p (point))
2340 (insert "\n\n=item \n\n\n\n"
2341 "=back")
2342 (cperl-ensure-newlines 2)
2343 (goto-char p)
2344 (forward-word 1)
2345 (end-of-line)
2346 (setq really-delete t)))
2347 (if (and delete really-delete)
2348 (cperl-putback-char cperl-del-back-ch))))))
2349
2350(defun cperl-electric-else ()
2351 "Insert a construction appropriate after a keyword.
2352Help message may be switched off by setting `cperl-message-electric-keyword'
2353to nil."
2354 (let ((beg (save-excursion (beginning-of-line) (point))))
2355 (and (save-excursion
2356 (backward-sexp 1)
2357 (cperl-after-expr-p nil "{;:"))
2358 (save-excursion
2359 (not
2360 (re-search-backward
2361 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2362 beg t)))
2363 (save-excursion (or (not (re-search-backward "^=" nil t))
2364 (looking-at "=cut")
2365 (and cperl-use-syntax-table-text-property
2366 (not (eq (get-text-property (point)
2367 'syntax-type)
2368 'pod)))))
2369 (progn
2370 (cperl-indent-line)
2371 ;;(insert " {\n\n}")
2372 (cond
2373 (cperl-extra-newline-before-brace
2374 (insert "\n")
2375 (insert "{")
2376 (cperl-indent-line)
2377 (insert "\n\n}"))
2378 (t
2379 (insert " {\n\n}")))
2380 (or (looking-at "[ \t]\\|$") (insert " "))
2381 (cperl-indent-line)
2382 (forward-line -1)
2383 (cperl-indent-line)
2384 (cperl-putback-char cperl-del-back-ch)
2385 (setq this-command 'cperl-electric-else)
2386 (if cperl-message-electric-keyword
2387 (message "Precede char by C-q to avoid expansion"))))))
2388
2389(defun cperl-linefeed ()
2390 "Go to end of line, open a new line and indent appropriately.
2391If in POD, insert appropriate lines."
2392 (interactive)
2393 (let ((beg (save-excursion (beginning-of-line) (point)))
2394 (end (save-excursion (end-of-line) (point)))
2395 (pos (point)) start over cut res)
2396 (if (and ; Check if we need to split:
2397 ; i.e., on a boundary and inside "{...}"
2398 (save-excursion (cperl-to-comment-or-eol)
2399 (>= (point) pos)) ; Not in a comment
2400 (or (save-excursion
2401 (skip-chars-backward " \t" beg)
2402 (forward-char -1)
2403 (looking-at "[;{]")) ; After { or ; + spaces
2404 (looking-at "[ \t]*}") ; Before }
2405 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2406 (save-excursion
2407 (and
2408 (eq (car (parse-partial-sexp pos end -1)) -1)
2409 ; Leave the level of parens
2410 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2411 ; Are at end
2412 (cperl-after-block-p (point-min))
2413 (progn
2414 (backward-sexp 1)
2415 (setq start (point-marker))
2416 (<= start pos))))) ; Redundant? Are after the
2417 ; start of parens group.
2418 (progn
2419 (skip-chars-backward " \t")
2420 (or (memq (preceding-char) (append ";{" nil))
2421 (insert ";"))
2422 (insert "\n")
2423 (forward-line -1)
2424 (cperl-indent-line)
2425 (goto-char start)
2426 (or (looking-at "{[ \t]*$") ; If there is a statement
2427 ; before, move it to separate line
2428 (progn
2429 (forward-char 1)
2430 (insert "\n")
2431 (cperl-indent-line)))
2432 (forward-line 1) ; We are on the target line
2433 (cperl-indent-line)
2434 (beginning-of-line)
2435 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2436 ; after, move it to separate line
2437 (progn
2438 (end-of-line)
2439 (search-backward "}" beg)
2440 (skip-chars-backward " \t")
2441 (or (memq (preceding-char) (append ";{" nil))
2442 (insert ";"))
2443 (insert "\n")
2444 (cperl-indent-line)
2445 (forward-line -1)))
2446 (forward-line -1) ; We are on the line before target
2447 (end-of-line)
2448 (newline-and-indent))
2449 (end-of-line) ; else - no splitting
2450 (cond
2451 ((and (looking-at "\n[ \t]*{$")
2452 (save-excursion
2453 (skip-chars-backward " \t")
2454 (eq (preceding-char) ?\)))) ; Probably if () {} group
2455 ; with an extra newline.
2456 (forward-line 2)
2457 (cperl-indent-line))
2458 ((save-excursion ; In POD header
2459 (forward-paragraph -1)
2460 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2461 ;; We are after \n now, so look for the rest
2462 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2463 (progn
2464 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2465 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2466 t)))
2467 (if (and over
2468 (progn
2469 (forward-paragraph -1)
2470 (forward-word 1)
2471 (setq pos (point))
2472 (setq cut (buffer-substring (point)
2473 (save-excursion
2474 (end-of-line)
2475 (point))))
2476 (delete-char (- (save-excursion (end-of-line) (point))
2477 (point)))
2478 (setq res (expand-abbrev))
2479 (save-excursion
2480 (goto-char pos)
2481 (insert cut))
2482 res))
2483 nil
2484 (cperl-ensure-newlines (if cut 2 4))
2485 (forward-line 2)))
2486 ((get-text-property (point) 'in-pod) ; In POD section
2487 (cperl-ensure-newlines 4)
2488 (forward-line 2))
2489 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2490 (forward-line 1)
2491 (cperl-indent-line))
2492 (t
2493 (newline-and-indent))))))
2494
2495(defun cperl-electric-semi (arg)
2496 "Insert character and correct line's indentation."
2497 (interactive "P")
2498 (if cperl-auto-newline
2499 (cperl-electric-terminator arg)
2500 (self-insert-command (prefix-numeric-value arg))
2501 (if cperl-autoindent-on-semi
2502 (cperl-indent-line))))
2503
2504(defun cperl-electric-terminator (arg)
2505 "Insert character and correct line's indentation."
2506 (interactive "P")
2507 (let ((end (point))
2508 (auto (and cperl-auto-newline
2509 (or (not (eq last-command-event ?:))
2510 cperl-auto-newline-after-colon)))
2511 insertpos)
2512 (if (and ;;(not arg)
2513 (eolp)
2514 (not (save-excursion
2515 (beginning-of-line)
2516 (skip-chars-forward " \t")
2517 (or
2518 ;; Ignore in comment lines
2519 (= (following-char) ?#)
2520 ;; Colon is special only after a label
2521 ;; So quickly rule out most other uses of colon
2522 ;; and do no indentation for them.
2523 (and (eq last-command-event ?:)
2524 (save-excursion
2525 (forward-word 1)
2526 (skip-chars-forward " \t")
2527 (and (< (point) end)
2528 (progn (goto-char (- end 1))
2529 (not (looking-at ":"))))))
2530 (progn
2531 (beginning-of-defun)
2532 (let ((pps (parse-partial-sexp (point) end)))
2533 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2534 (progn
2535 (self-insert-command (prefix-numeric-value arg))
2536 ;;(forward-char -1)
2537 (if auto (setq insertpos (point-marker)))
2538 ;;(forward-char 1)
2539 (cperl-indent-line)
2540 (if auto
2541 (progn
2542 (newline)
2543 (cperl-indent-line)))
2544 (save-excursion
2545 (if insertpos (goto-char (1- (marker-position insertpos)))
2546 (forward-char -1))
2547 (delete-char 1))))
2548 (if insertpos
2549 (save-excursion
2550 (goto-char insertpos)
2551 (self-insert-command (prefix-numeric-value arg)))
2552 (self-insert-command (prefix-numeric-value arg)))))
2553
2554(defun cperl-electric-backspace (arg)
2555 "Backspace, or remove whitespace around the point inserted by an electric key.
2556Will untabify if `cperl-electric-backspace-untabify' is non-nil."
2557 (interactive "p")
2558 (if (and cperl-auto-newline
2559 (memq last-command '(cperl-electric-semi
2560 cperl-electric-terminator
2561 cperl-electric-lbrace))
2562 (memq (preceding-char) '(?\s ?\t ?\n)))
2563 (let (p)
2564 (if (eq last-command 'cperl-electric-lbrace)
2565 (skip-chars-forward " \t\n"))
2566 (setq p (point))
2567 (skip-chars-backward " \t\n")
2568 (delete-region (point) p))
2569 (and (eq last-command 'cperl-electric-else)
2570 ;; We are removing the whitespace *inside* cperl-electric-else
2571 (setq this-command 'cperl-electric-else-really))
2572 (if (and cperl-auto-newline
2573 (eq last-command 'cperl-electric-else-really)
2574 (memq (preceding-char) '(?\s ?\t ?\n)))
2575 (let (p)
2576 (skip-chars-forward " \t\n")
2577 (setq p (point))
2578 (skip-chars-backward " \t\n")
2579 (delete-region (point) p))
2580 (if cperl-electric-backspace-untabify
2581 (backward-delete-char-untabify arg)
2582 (delete-backward-char arg)))))
2583
2584(put 'cperl-electric-backspace 'delete-selection 'supersede)
2585
2586(defun cperl-inside-parens-p () ;; NOT USED????
2587 (condition-case ()
2588 (save-excursion
2589 (save-restriction
2590 (narrow-to-region (point)
2591 (progn (beginning-of-defun) (point)))
2592 (goto-char (point-max))
2593 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2594 (error nil)))
2595\f
2596(defun cperl-indent-command (&optional whole-exp)
2597 "Indent current line as Perl code, or in some cases insert a tab character.
2598If `cperl-tab-always-indent' is non-nil (the default), always indent current
2599line. Otherwise, indent the current line only if point is at the left margin
2600or in the line's indentation; otherwise insert a tab.
2601
2602A numeric argument, regardless of its value,
2603means indent rigidly all the lines of the expression starting after point
2604so that this line becomes properly indented.
2605The relative indentation among the lines of the expression are preserved."
2606 (interactive "P")
2607 (cperl-update-syntaxification (point) (point))
2608 (if whole-exp
2609 ;; If arg, always indent this line as Perl
2610 ;; and shift remaining lines of expression the same amount.
2611 (let ((shift-amt (cperl-indent-line))
2612 beg end)
2613 (save-excursion
2614 (if cperl-tab-always-indent
2615 (beginning-of-line))
2616 (setq beg (point))
2617 (forward-sexp 1)
2618 (setq end (point))
2619 (goto-char beg)
2620 (forward-line 1)
2621 (setq beg (point)))
2622 (if (and shift-amt (> end beg))
2623 (indent-code-rigidly beg end shift-amt "#")))
2624 (if (and (not cperl-tab-always-indent)
2625 (save-excursion
2626 (skip-chars-backward " \t")
2627 (not (bolp))))
2628 (insert-tab)
2629 (cperl-indent-line))))
2630
2631(defun cperl-indent-line (&optional parse-data)
2632 "Indent current line as Perl code.
2633Return the amount the indentation changed by."
2634 (let ((case-fold-search nil)
2635 (pos (- (point-max) (point)))
2636 indent i beg shift-amt)
2637 (setq indent (cperl-calculate-indent parse-data)
2638 i indent)
2639 (beginning-of-line)
2640 (setq beg (point))
2641 (cond ((or (eq indent nil) (eq indent t))
2642 (setq indent (current-indentation) i nil))
2643 ;;((eq indent t) ; Never?
2644 ;; (setq indent (cperl-calculate-indent-within-comment)))
2645 ;;((looking-at "[ \t]*#")
2646 ;; (setq indent 0))
2647 (t
2648 (skip-chars-forward " \t")
2649 (if (listp indent) (setq indent (car indent)))
2650 (cond ((and (looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2651 (not (looking-at "[smy]:\\|tr:")))
2652 (and (> indent 0)
2653 (setq indent (max cperl-min-label-indent
2654 (+ indent cperl-label-offset)))))
2655 ((= (following-char) ?})
2656 (setq indent (- indent cperl-indent-level)))
2657 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2658 (setq indent (+ indent cperl-close-paren-offset)))
2659 ((= (following-char) ?{)
2660 (setq indent (+ indent cperl-brace-offset))))))
2661 (skip-chars-forward " \t")
2662 (setq shift-amt (and i (- indent (current-column))))
2663 (if (or (not shift-amt)
2664 (zerop shift-amt))
2665 (if (> (- (point-max) pos) (point))
2666 (goto-char (- (point-max) pos)))
2667 ;;;(delete-region beg (point))
2668 ;;;(indent-to indent)
2669 (cperl-make-indent indent)
2670 ;; If initial point was within line's indentation,
2671 ;; position after the indentation. Else stay at same point in text.
2672 (if (> (- (point-max) pos) (point))
2673 (goto-char (- (point-max) pos))))
2674 shift-amt))
2675
2676(defun cperl-after-label ()
2677 ;; Returns true if the point is after label. Does not do save-excursion.
2678 (and (eq (preceding-char) ?:)
2679 (memq (char-syntax (char-after (- (point) 2)))
2680 '(?w ?_))
2681 (progn
2682 (backward-sexp)
2683 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2684
2685(defun cperl-get-state (&optional parse-start start-state)
2686 ;; returns list (START STATE DEPTH PRESTART),
2687 ;; START is a good place to start parsing, or equal to
2688 ;; PARSE-START if preset,
2689 ;; STATE is what is returned by `parse-partial-sexp'.
2690 ;; DEPTH is true is we are immediately after end of block
2691 ;; which contains START.
2692 ;; PRESTART is the position basing on which START was found.
2693 (save-excursion
2694 (let ((start-point (point)) depth state start prestart)
2695 (if (and parse-start
2696 (<= parse-start start-point))
2697 (goto-char parse-start)
2698 (beginning-of-defun)
2699 (setq start-state nil))
2700 (setq prestart (point))
2701 (if start-state nil
2702 ;; Try to go out, if sub is not on the outermost level
2703 (while (< (point) start-point)
2704 (setq start (point) parse-start start depth nil
2705 state (parse-partial-sexp start start-point -1))
2706 (if (> (car state) -1) nil
2707 ;; The current line could start like }}}, so the indentation
2708 ;; corresponds to a different level than what we reached
2709 (setq depth t)
2710 (beginning-of-line 2))) ; Go to the next line.
2711 (if start (goto-char start))) ; Not at the start of file
2712 (setq start (point))
2713 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2714 (list start state depth prestart))))
2715
2716(defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2717
2718(defun cperl-beginning-of-property (p prop &optional lim)
2719 "Given that P has a property PROP, find where the property starts.
2720Will not look before LIM."
2721 ;;; XXXX What to do at point-max???
2722 (or (previous-single-property-change (cperl-1+ p) prop lim)
2723 (point-min))
2724;;; (cond ((eq p (point-min))
2725;;; p)
2726;;; ((and lim (<= p lim))
2727;;; p)
2728;;; ((not (get-text-property (1- p) prop))
2729;;; p)
2730;;; (t (or (previous-single-property-change p look-prop lim)
2731;;; (point-min))))
2732 )
2733
2734(defun cperl-sniff-for-indent (&optional parse-data) ; was parse-start
2735 ;; the sniffer logic to understand what the current line MEANS.
2736 (cperl-update-syntaxification (point) (point))
2737 (let ((res (get-text-property (point) 'syntax-type)))
2738 (save-excursion
2739 (cond
2740 ((and (memq res '(pod here-doc here-doc-delim format))
2741 (not (get-text-property (point) 'indentable)))
2742 (vector res))
2743 ;; before start of POD - whitespace found since do not have 'pod!
2744 ((looking-at "[ \t]*\n=")
2745 (error "Spaces before POD section!"))
2746 ((and (not cperl-indent-left-aligned-comments)
2747 (looking-at "^#"))
2748 [comment-special:at-beginning-of-line])
2749 ((get-text-property (point) 'in-pod)
2750 [in-pod])
2751 (t
2752 (beginning-of-line)
2753 (let* ((indent-point (point))
2754 (char-after-pos (save-excursion
2755 (skip-chars-forward " \t")
2756 (point)))
2757 (char-after (char-after char-after-pos))
2758 (pre-indent-point (point))
2759 p prop look-prop is-block delim)
2760 (save-excursion ; Know we are not in POD, find appropriate pos before
2761 (cperl-backward-to-noncomment nil)
2762 (setq p (max (point-min) (1- (point)))
2763 prop (get-text-property p 'syntax-type)
2764 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2765 'syntax-type))
2766 (if (memq prop '(pod here-doc format here-doc-delim))
2767 (progn
2768 (goto-char (cperl-beginning-of-property p look-prop))
2769 (beginning-of-line)
2770 (setq pre-indent-point (point)))))
2771 (goto-char pre-indent-point) ; Orig line skipping preceeding pod/etc
2772 (let* ((case-fold-search nil)
2773 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2774 (start (or (nth 2 parse-data) ; last complete sexp terminated
2775 (nth 0 s-s))) ; Good place to start parsing
2776 (state (nth 1 s-s))
2777 (containing-sexp (car (cdr state)))
2778 old-indent)
2779 (if (and
2780 ;;containing-sexp ;; We are buggy at toplevel :-(
2781 parse-data)
2782 (progn
2783 (setcar parse-data pre-indent-point)
2784 (setcar (cdr parse-data) state)
2785 (or (nth 2 parse-data)
2786 (setcar (cddr parse-data) start))
2787 ;; Before this point: end of statement
2788 (setq old-indent (nth 3 parse-data))))
2789 (cond ((get-text-property (point) 'indentable)
2790 ;; indent to "after" the surrounding open
2791 ;; (same offset as `cperl-beautify-regexp-piece'),
2792 ;; skip blanks if we do not close the expression.
2793 (setq delim ; We do not close the expression
2794 (get-text-property
2795 (cperl-1+ char-after-pos) 'indentable)
2796 p (1+ (cperl-beginning-of-property
2797 (point) 'indentable))
2798 is-block ; misused for: preceeding line in REx
2799 (save-excursion ; Find preceeding line
2800 (cperl-backward-to-noncomment p)
2801 (beginning-of-line)
2802 (if (<= (point) p)
2803 (progn ; get indent from the first line
2804 (goto-char p)
2805 (skip-chars-forward " \t")
2806 (if (memq (char-after (point))
2807 (append "#\n" nil))
2808 nil ; Can't use intentation of this line...
2809 (point)))
2810 (skip-chars-forward " \t")
2811 (point)))
2812 prop (parse-partial-sexp p char-after-pos))
2813 (cond ((not delim) ; End the REx, ignore is-block
2814 (vector 'indentable 'terminator p is-block))
2815 (is-block ; Indent w.r.t. preceeding line
2816 (vector 'indentable 'cont-line char-after-pos
2817 is-block char-after p))
2818 (t ; No preceeding line...
2819 (vector 'indentable 'first-line p))))
2820 ((get-text-property char-after-pos 'REx-part2)
2821 (vector 'REx-part2 (point)))
2822 ((nth 4 state)
2823 [comment])
2824 ((nth 3 state)
2825 [string])
2826 ;; XXXX Do we need to special-case this?
2827 ((null containing-sexp)
2828 ;; Line is at top level. May be data or function definition,
2829 ;; or may be function argument declaration.
2830 ;; Indent like the previous top level line
2831 ;; unless that ends in a closeparen without semicolon,
2832 ;; in which case this line is the first argument decl.
2833 (skip-chars-forward " \t")
2834 (cperl-backward-to-noncomment (or old-indent (point-min)))
2835 (setq state
2836 (or (bobp)
2837 (eq (point) old-indent) ; old-indent was at comment
2838 (eq (preceding-char) ?\;)
2839 ;; Had ?\) too
2840 (and (eq (preceding-char) ?\})
2841 (cperl-after-block-and-statement-beg
2842 (point-min))) ; Was start - too close
2843 (memq char-after (append ")]}" nil))
2844 (and (eq (preceding-char) ?\:) ; label
2845 (progn
2846 (forward-sexp -1)
2847 (skip-chars-backward " \t")
2848 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:")))
2849 (get-text-property (point) 'first-format-line)))
2850
2851 ;; Look at previous line that's at column 0
2852 ;; to determine whether we are in top-level decls
2853 ;; or function's arg decls. Set basic-indent accordingly.
2854 ;; Now add a little if this is a continuation line.
2855 (and state
2856 parse-data
2857 (not (eq char-after ?\C-j))
2858 (setcdr (cddr parse-data)
2859 (list pre-indent-point)))
2860 (vector 'toplevel start char-after state (nth 2 s-s)))
2861 ((not
2862 (or (setq is-block
2863 (and (setq delim (= (char-after containing-sexp) ?{))
2864 (save-excursion ; Is it a hash?
2865 (goto-char containing-sexp)
2866 (cperl-block-p))))
2867 cperl-indent-parens-as-block))
2868 ;; group is an expression, not a block:
2869 ;; indent to just after the surrounding open parens,
2870 ;; skip blanks if we do not close the expression.
2871 (goto-char (1+ containing-sexp))
2872 (or (memq char-after
2873 (append (if delim "}" ")]}") nil))
2874 (looking-at "[ \t]*\\(#\\|$\\)")
2875 (skip-chars-forward " \t"))
2876 (setq old-indent (point)) ; delim=is-brace
2877 (vector 'in-parens char-after (point) delim containing-sexp))
2878 (t
2879 ;; Statement level. Is it a continuation or a new statement?
2880 ;; Find previous non-comment character.
2881 (goto-char pre-indent-point) ; Skip one level of POD/etc
2882 (cperl-backward-to-noncomment containing-sexp)
2883 ;; Back up over label lines, since they don't
2884 ;; affect whether our line is a continuation.
2885 ;; (Had \, too)
2886 (while;;(or (eq (preceding-char) ?\,)
2887 (and (eq (preceding-char) ?:)
2888 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2889 (memq (char-syntax (char-after (- (point) 2)))
2890 '(?w ?_))))
2891 ;;)
2892 ;; This is always FALSE?
2893 (if (eq (preceding-char) ?\,)
2894 ;; Will go to beginning of line, essentially.
2895 ;; Will ignore embedded sexpr XXXX.
2896 (cperl-backward-to-start-of-continued-exp containing-sexp))
2897 (beginning-of-line)
2898 (cperl-backward-to-noncomment containing-sexp))
2899 ;; Now we get non-label preceeding the indent point
2900 (if (not (or (eq (1- (point)) containing-sexp)
2901 (memq (preceding-char)
2902 (append (if is-block " ;{" " ,;{") '(nil)))
2903 (and (eq (preceding-char) ?\})
2904 (cperl-after-block-and-statement-beg
2905 containing-sexp))
2906 (get-text-property (point) 'first-format-line)))
2907 ;; This line is continuation of preceding line's statement;
2908 ;; indent `cperl-continued-statement-offset' more than the
2909 ;; previous line of the statement.
2910 ;;
2911 ;; There might be a label on this line, just
2912 ;; consider it bad style and ignore it.
2913 (progn
2914 (cperl-backward-to-start-of-continued-exp containing-sexp)
2915 (vector 'continuation (point) char-after is-block delim))
2916 ;; This line starts a new statement.
2917 ;; Position following last unclosed open brace
2918 (goto-char containing-sexp)
2919 ;; Is line first statement after an open-brace?
2920 (or
2921 ;; If no, find that first statement and indent like
2922 ;; it. If the first statement begins with label, do
2923 ;; not believe when the indentation of the label is too
2924 ;; small.
2925 (save-excursion
2926 (forward-char 1)
2927 (let ((colon-line-end 0))
2928 (while
2929 (progn (skip-chars-forward " \t\n")
2930 ;; s: foo : bar :x is NOT label
2931 (and (looking-at "#\\|\\([a-zA-Z0-9_$]+\\):[^:]\\|=[a-zA-Z]")
2932 (not (looking-at "[sym]:\\|tr:"))))
2933 ;; Skip over comments and labels following openbrace.
2934 (cond ((= (following-char) ?\#)
2935 (forward-line 1))
2936 ((= (following-char) ?\=)
2937 (goto-char
2938 (or (next-single-property-change (point) 'in-pod)
2939 (point-max)))) ; do not loop if no syntaxification
2940 ;; label:
2941 (t
2942 (save-excursion (end-of-line)
2943 (setq colon-line-end (point)))
2944 (search-forward ":"))))
2945 ;; We are at beginning of code (NOT label or comment)
2946 ;; First, the following code counts
2947 ;; if it is before the line we want to indent.
2948 (and (< (point) indent-point)
2949 (vector 'have-prev-sibling (point) colon-line-end
2950 containing-sexp))))
2951 (progn
2952 ;; If no previous statement,
2953 ;; indent it relative to line brace is on.
2954
2955 ;; For open-braces not the first thing in a line,
2956 ;; add in cperl-brace-imaginary-offset.
2957
2958 ;; If first thing on a line: ?????
2959 ;; Move back over whitespace before the openbrace.
2960 (setq ; brace first thing on a line
2961 old-indent (progn (skip-chars-backward " \t") (bolp)))
2962 ;; Should we indent w.r.t. earlier than start?
2963 ;; Move to start of control group, possibly on a different line
2964 (or cperl-indent-wrt-brace
2965 (cperl-backward-to-noncomment (point-min)))
2966 ;; If the openbrace is preceded by a parenthesized exp,
2967 ;; move to the beginning of that;
2968 (if (eq (preceding-char) ?\))
2969 (progn
2970 (forward-sexp -1)
2971 (cperl-backward-to-noncomment (point-min))))
2972 ;; In the case it starts a subroutine, indent with
2973 ;; respect to `sub', not with respect to the
2974 ;; first thing on the line, say in the case of
2975 ;; anonymous sub in a hash.
2976 (if (and;; Is it a sub in group starting on this line?
2977 (cond ((get-text-property (point) 'attrib-group)
2978 (goto-char (cperl-beginning-of-property
2979 (point) 'attrib-group)))
2980 ((eq (preceding-char) ?b)
2981 (forward-sexp -1)
2982 (looking-at "sub\\>")))
2983 (setq p (nth 1 ; start of innermost containing list
2984 (parse-partial-sexp
2985 (save-excursion (beginning-of-line)
2986 (point))
2987 (point)))))
2988 (progn
2989 (goto-char (1+ p)) ; enclosing block on the same line
2990 (skip-chars-forward " \t")
2991 (vector 'code-start-in-block containing-sexp char-after
2992 (and delim (not is-block)) ; is a HASH
2993 old-indent ; brace first thing on a line
2994 t (point) ; have something before...
2995 )
2996 ;;(current-column)
2997 )
2998 ;; Get initial indentation of the line we are on.
2999 ;; If line starts with label, calculate label indentation
3000 (vector 'code-start-in-block containing-sexp char-after
3001 (and delim (not is-block)) ; is a HASH
3002 old-indent ; brace first thing on a line
3003 nil (point))))))))))))))) ; nothing interesting before
3004
3005(defvar cperl-indent-rules-alist
3006 '((pod nil) ; via `syntax-type' property
3007 (here-doc nil) ; via `syntax-type' property
3008 (here-doc-delim nil) ; via `syntax-type' property
3009 (format nil) ; via `syntax-type' property
3010 (in-pod nil) ; via `in-pod' property
3011 (comment-special:at-beginning-of-line nil)
3012 (string t)
3013 (comment nil))
3014 "Alist of indentation rules for CPerl mode.
3015The values mean:
3016 nil: do not indent;
3017 number: add this amount of indentation.")
3018
3019(defun cperl-calculate-indent (&optional parse-data) ; was parse-start
3020 "Return appropriate indentation for current line as Perl code.
3021In usual case returns an integer: the column to indent to.
3022Returns nil if line starts inside a string, t if in a comment.
3023
3024Will not correct the indentation for labels, but will correct it for braces
3025and closing parentheses and brackets."
3026 ;; This code is still a broken architecture: in some cases we need to
3027 ;; compensate for some modifications which `cperl-indent-line' will add later
3028 (save-excursion
3029 (let ((i (cperl-sniff-for-indent parse-data)) what p)
3030 (cond
3031 ;;((or (null i) (eq i t) (numberp i))
3032 ;; i)
3033 ((vectorp i)
3034 (setq what (assoc (elt i 0) cperl-indent-rules-alist))
3035 (cond
3036 (what (cadr what)) ; Load from table
3037 ;;
3038 ;; Indenters for regular expressions with //x and qw()
3039 ;;
3040 ((eq 'REx-part2 (elt i 0)) ;; [self start] start of /REP in s//REP/x
3041 (goto-char (elt i 1))
3042 (condition-case nil ; Use indentation of the 1st part
3043 (forward-sexp -1))
3044 (current-column))
3045 ((eq 'indentable (elt i 0)) ; Indenter for REGEXP qw() etc
3046 (cond ;;; [indentable terminator start-pos is-block]
3047 ((eq 'terminator (elt i 1)) ; Lone terminator of "indentable string"
3048 (goto-char (elt i 2)) ; After opening parens
3049 (1- (current-column)))
3050 ((eq 'first-line (elt i 1)); [indentable first-line start-pos]
3051 (goto-char (elt i 2))
3052 (+ (or cperl-regexp-indent-step cperl-indent-level)
3053 -1
3054 (current-column)))
3055 ((eq 'cont-line (elt i 1)); [indentable cont-line pos prev-pos first-char start-pos]
3056 ;; Indent as the level after closing parens
3057 (goto-char (elt i 2)) ; indent line
3058 (skip-chars-forward " \t)") ; Skip closing parens
3059 (setq p (point))
3060 (goto-char (elt i 3)) ; previous line
3061 (skip-chars-forward " \t)") ; Skip closing parens
3062 ;; Number of parens in between:
3063 (setq p (nth 0 (parse-partial-sexp (point) p))
3064 what (elt i 4)) ; First char on current line
3065 (goto-char (elt i 3)) ; previous line
3066 (+ (* p (or cperl-regexp-indent-step cperl-indent-level))
3067 (cond ((eq what ?\) )
3068 (- cperl-close-paren-offset)) ; compensate
3069 ((eq what ?\| )
3070 (- (or cperl-regexp-indent-step cperl-indent-level)))
3071 (t 0))
3072 (if (eq (following-char) ?\| )
3073 (or cperl-regexp-indent-step cperl-indent-level)
3074 0)
3075 (current-column)))
3076 (t
3077 (error "Unrecognized value of indent: %s" i))))
3078 ;;
3079 ;; Indenter for stuff at toplevel
3080 ;;
3081 ((eq 'toplevel (elt i 0)) ;; [toplevel start char-after state immed-after-block]
3082 (+ (save-excursion ; To beg-of-defun, or end of last sexp
3083 (goto-char (elt i 1)) ; start = Good place to start parsing
3084 (- (current-indentation) ;
3085 (if (elt i 4) cperl-indent-level 0))) ; immed-after-block
3086 (if (eq (elt i 2) ?{) cperl-continued-brace-offset 0) ; char-after
3087 ;; Look at previous line that's at column 0
3088 ;; to determine whether we are in top-level decls
3089 ;; or function's arg decls. Set basic-indent accordingly.
3090 ;; Now add a little if this is a continuation line.
3091 (if (elt i 3) ; state (XXX What is the semantic???)
3092 0
3093 cperl-continued-statement-offset)))
3094 ;;
3095 ;; Indenter for stuff in "parentheses" (or brackets, braces-as-hash)
3096 ;;
3097 ((eq 'in-parens (elt i 0))
3098 ;; in-parens char-after old-indent-point is-brace containing-sexp
3099
3100 ;; group is an expression, not a block:
3101 ;; indent to just after the surrounding open parens,
3102 ;; skip blanks if we do not close the expression.
3103 (+ (progn
3104 (goto-char (elt i 2)) ; old-indent-point
3105 (current-column))
3106 (if (and (elt i 3) ; is-brace
3107 (eq (elt i 1) ?\})) ; char-after
3108 ;; Correct indentation of trailing ?\}
3109 (+ cperl-indent-level cperl-close-paren-offset)
3110 0)))
3111 ;;
3112 ;; Indenter for continuation lines
3113 ;;
3114 ((eq 'continuation (elt i 0))
3115 ;; [continuation statement-start char-after is-block is-brace]
3116 (goto-char (elt i 1)) ; statement-start
3117 (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
3118 0 ; Closing parenth
3119 cperl-continued-statement-offset)
3120 (if (or (elt i 3) ; is-block
3121 (not (elt i 4)) ; is-brace
3122 (not (eq (elt i 2) ?\}))) ; char-after
3123 0
3124 ;; Now it is a hash reference
3125 (+ cperl-indent-level cperl-close-paren-offset))
3126 ;; Labels do not take :: ...
3127 (if (looking-at "\\(\\w\\|_\\)+[ \t]*:")
3128 (if (> (current-indentation) cperl-min-label-indent)
3129 (- (current-indentation) cperl-label-offset)
3130 ;; Do not move `parse-data', this should
3131 ;; be quick anyway (this comment comes
3132 ;; from different location):
3133 (cperl-calculate-indent))
3134 (current-column))
3135 (if (eq (elt i 2) ?\{) ; char-after
3136 cperl-continued-brace-offset 0)))
3137 ;;
3138 ;; Indenter for lines in a block which are not leading lines
3139 ;;
3140 ((eq 'have-prev-sibling (elt i 0))
3141 ;; [have-prev-sibling sibling-beg colon-line-end block-start]
3142 (goto-char (elt i 1)) ; sibling-beg
3143 (if (> (elt i 2) (point)) ; colon-line-end; have label before point
3144 (if (> (current-indentation)
3145 cperl-min-label-indent)
3146 (- (current-indentation) cperl-label-offset)
3147 ;; Do not believe: `max' was involved in calculation of indent
3148 (+ cperl-indent-level
3149 (save-excursion
3150 (goto-char (elt i 3)) ; block-start
3151 (current-indentation))))
3152 (current-column)))
3153 ;;
3154 ;; Indenter for the first line in a block
3155 ;;
3156 ((eq 'code-start-in-block (elt i 0))
3157 ;;[code-start-in-block before-brace char-after
3158 ;; is-a-HASH-ref brace-is-first-thing-on-a-line
3159 ;; group-starts-before-start-of-sub start-of-control-group]
3160 (goto-char (elt i 1))
3161 ;; For open brace in column zero, don't let statement
3162 ;; start there too. If cperl-indent-level=0,
3163 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
3164 (+ (if (and (bolp) (zerop cperl-indent-level))
3165 (+ cperl-brace-offset cperl-continued-statement-offset)
3166 cperl-indent-level)
3167 (if (and (elt i 3) ; is-a-HASH-ref
3168 (eq (elt i 2) ?\})) ; char-after: End of a hash reference
3169 (+ cperl-indent-level cperl-close-paren-offset)
3170 0)
3171 ;; Unless openbrace is the first nonwhite thing on the line,
3172 ;; add the cperl-brace-imaginary-offset.
3173 (if (elt i 4) 0 ; brace-is-first-thing-on-a-line
3174 cperl-brace-imaginary-offset)
3175 (progn
3176 (goto-char (elt i 6)) ; start-of-control-group
3177 (if (elt i 5) ; group-starts-before-start-of-sub
3178 (current-column)
3179 ;; Get initial indentation of the line we are on.
3180 ;; If line starts with label, calculate label indentation
3181 (if (save-excursion
3182 (beginning-of-line)
3183 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
3184 (if (> (current-indentation) cperl-min-label-indent)
3185 (- (current-indentation) cperl-label-offset)
3186 ;; Do not move `parse-data', this should
3187 ;; be quick anyway:
3188 (cperl-calculate-indent))
3189 (current-indentation))))))
3190 (t
3191 (error "Unrecognized value of indent: %s" i))))
3192 (t
3193 (error "Got strange value of indent: %s" i))))))
3194
3195(defun cperl-calculate-indent-within-comment ()
3196 "Return the indentation amount for line, assuming that
3197the current line is to be regarded as part of a block comment."
3198 (let (end star-start)
3199 (save-excursion
3200 (beginning-of-line)
3201 (skip-chars-forward " \t")
3202 (setq end (point))
3203 (and (= (following-char) ?#)
3204 (forward-line -1)
3205 (cperl-to-comment-or-eol)
3206 (setq end (point)))
3207 (goto-char end)
3208 (current-column))))
3209
3210
3211(defun cperl-to-comment-or-eol ()
3212 "Go to position before comment on the current line, or to end of line.
3213Returns true if comment is found. In POD will not move the point."
3214 ;; If the line is inside other syntax groups (qq-style strings, HERE-docs)
3215 ;; then looks for literal # or end-of-line.
3216 (let (state stop-in cpoint (lim (progn (end-of-line) (point))) pr e)
3217 (or cperl-font-locking
3218 (cperl-update-syntaxification lim lim))
3219 (beginning-of-line)
3220 (if (setq pr (get-text-property (point) 'syntax-type))
3221 (setq e (next-single-property-change (point) 'syntax-type nil (point-max))))
3222 (if (or (eq pr 'pod)
3223 (if (or (not e) (> e lim)) ; deep inside a group
3224 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)))
3225 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
3226 ;; Else - need to do it the hard way
3227 (and (and e (<= e lim))
3228 (goto-char e))
3229 (while (not stop-in)
3230 (setq state (parse-partial-sexp (point) lim nil nil nil t))
3231 ; stop at comment
3232 ;; If fails (beginning-of-line inside sexp), then contains not-comment
3233 (if (nth 4 state) ; After `#';
3234 ; (nth 2 state) can be
3235 ; beginning of m,s,qq and so
3236 ; on
3237 (if (nth 2 state)
3238 (progn
3239 (setq cpoint (point))
3240 (goto-char (nth 2 state))
3241 (cond
3242 ((looking-at "\\(s\\|tr\\)\\>")
3243 (or (re-search-forward
3244 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
3245 lim 'move)
3246 (setq stop-in t)))
3247 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
3248 (or (re-search-forward
3249 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
3250 lim 'move)
3251 (setq stop-in t)))
3252 (t ; It was fair comment
3253 (setq stop-in t) ; Finish
3254 (goto-char (1- cpoint)))))
3255 (setq stop-in t) ; Finish
3256 (forward-char -1))
3257 (setq stop-in t))) ; Finish
3258 (nth 4 state))))
3259
3260(defsubst cperl-modify-syntax-type (at how)
3261 (if (< at (point-max))
3262 (progn
3263 (put-text-property at (1+ at) 'syntax-table how)
3264 (put-text-property at (1+ at) 'rear-nonsticky '(syntax-table)))))
3265
3266(defun cperl-protect-defun-start (s e)
3267 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
3268 (save-excursion
3269 (goto-char s)
3270 (while (re-search-forward "^\\s(" e 'to-end)
3271 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
3272
3273(defun cperl-commentify (bb e string &optional noface)
3274 (if cperl-use-syntax-table-text-property
3275 (if (eq noface 'n) ; Only immediate
3276 nil
3277 ;; We suppose that e is _after_ the end of construction, as after eol.
3278 (setq string (if string cperl-st-sfence cperl-st-cfence))
3279 (if (> bb (- e 2))
3280 ;; one-char string/comment?!
3281 (cperl-modify-syntax-type bb cperl-st-punct)
3282 (cperl-modify-syntax-type bb string)
3283 (cperl-modify-syntax-type (1- e) string))
3284 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
3285 (put-text-property (1+ bb) (1- e)
3286 'syntax-table cperl-string-syntax-table))
3287 (cperl-protect-defun-start bb e))
3288 ;; Fontify
3289 (or noface
3290 (not cperl-pod-here-fontify)
3291 (put-text-property bb e 'face (if string 'font-lock-string-face
3292 'font-lock-comment-face)))))
3293
3294(defvar cperl-starters '(( ?\( . ?\) )
3295 ( ?\[ . ?\] )
3296 ( ?\{ . ?\} )
3297 ( ?\< . ?\> )))
3298
3299(defun cperl-cached-syntax-table (st)
3300 "Get a syntax table cached in ST, or create and cache into ST a syntax table.
3301All the entries of the syntax table are \".\", except for a backslash, which
3302is quoting."
3303 (if (car-safe st)
3304 (car st)
3305 (setcar st (make-syntax-table))
3306 (setq st (car st))
3307 (let ((i 0))
3308 (while (< i 256)
3309 (modify-syntax-entry i "." st)
3310 (setq i (1+ i))))
3311 (modify-syntax-entry ?\\ "\\" st)
3312 st))
3313
3314(defun cperl-forward-re (lim end is-2arg st-l err-l argument
3315 &optional ostart oend)
3316"Find the end of a regular expression or a stringish construct (q[] etc).
3317The point should be before the starting delimiter.
3318
3319Goes to LIM if none is found. If IS-2ARG is non-nil, assumes that it
3320is s/// or tr/// like expression. If END is nil, generates an error
3321message if needed. If SET-ST is non-nil, will use (or generate) a
3322cached syntax table in ST-L. If ERR-L is non-nil, will store the
3323error message in its CAR (unless it already contains some error
3324message). ARGUMENT should be the name of the construct (used in error
3325messages). OSTART, OEND may be set in recursive calls when processing
3326the second argument of 2ARG construct.
3327
3328Works *before* syntax recognition is done. In IS-2ARG situation may
3329modify syntax-type text property if the situation is too hard."
3330 (let (b starter ender st i i2 go-forward reset-st set-st)
3331 (skip-chars-forward " \t")
3332 ;; ender means matching-char matcher.
3333 (setq b (point)
3334 starter (if (eobp) 0 (char-after b))
3335 ender (cdr (assoc starter cperl-starters)))
3336 ;; What if starter == ?\\ ????
3337 (setq st (cperl-cached-syntax-table st-l))
3338 (setq set-st t)
3339 ;; Whether we have an intermediate point
3340 (setq i nil)
3341 ;; Prepare the syntax table:
3342 (if (not ender) ; m/blah/, s/x//, s/x/y/
3343 (modify-syntax-entry starter "$" st)
3344 (modify-syntax-entry starter (concat "(" (list ender)) st)
3345 (modify-syntax-entry ender (concat ")" (list starter)) st))
3346 (condition-case bb
3347 (progn
3348 ;; We use `$' syntax class to find matching stuff, but $$
3349 ;; is recognized the same as $, so we need to check this manually.
3350 (if (and (eq starter (char-after (cperl-1+ b)))
3351 (not ender))
3352 ;; $ has TeXish matching rules, so $$ equiv $...
3353 (forward-char 2)
3354 (setq reset-st (syntax-table))
3355 (set-syntax-table st)
3356 (forward-sexp 1)
3357 (if (<= (point) (1+ b))
3358 (error "Unfinished regular expression"))
3359 (set-syntax-table reset-st)
3360 (setq reset-st nil)
3361 ;; Now the problem is with m;blah;;
3362 (and (not ender)
3363 (eq (preceding-char)
3364 (char-after (- (point) 2)))
3365 (save-excursion
3366 (forward-char -2)
3367 (= 0 (% (skip-chars-backward "\\\\") 2)))
3368 (forward-char -1)))
3369 ;; Now we are after the first part.
3370 (and is-2arg ; Have trailing part
3371 (not ender)
3372 (eq (following-char) starter) ; Empty trailing part
3373 (progn
3374 (or (eq (char-syntax (following-char)) ?.)
3375 ;; Make trailing letter into punctuation
3376 (cperl-modify-syntax-type (point) cperl-st-punct))
3377 (setq is-2arg nil go-forward t))) ; Ignore the tail
3378 (if is-2arg ; Not number => have second part
3379 (progn
3380 (setq i (point) i2 i)
3381 (if ender
3382 (if (memq (following-char) '(?\s ?\t ?\n ?\f))
3383 (progn
3384 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3385 (goto-char (match-end 0))
3386 (skip-chars-forward " \t\n\f"))
3387 (setq i2 (point))))
3388 (forward-char -1))
3389 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3390 (if ender (modify-syntax-entry ender "." st))
3391 (setq set-st nil)
3392 (setq ender (cperl-forward-re lim end nil st-l err-l
3393 argument starter ender)
3394 ender (nth 2 ender)))))
3395 (error (goto-char lim)
3396 (setq set-st nil)
3397 (if reset-st
3398 (set-syntax-table reset-st))
3399 (or end
3400 (and cperl-brace-recursing
3401 (or (eq ostart ?\{)
3402 (eq starter ?\{)))
3403 (message
3404 "End of `%s%s%c ... %c' string/RE not found: %s"
3405 argument
3406 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3407 starter (or ender starter) bb)
3408 (or (car err-l) (setcar err-l b)))))
3409 (if set-st
3410 (progn
3411 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3412 (if ender (modify-syntax-entry ender "." st))))
3413 ;; i: have 2 args, after end of the first arg
3414 ;; i2: start of the second arg, if any (before delim if `ender').
3415 ;; ender: the last arg bounded by parens-like chars, the second one of them
3416 ;; starter: the starting delimiter of the first arg
3417 ;; go-forward: has 2 args, and the second part is empty
3418 (list i i2 ender starter go-forward)))
3419
3420(defun cperl-forward-group-in-re (&optional st-l)
3421 "Find the end of a group in a REx.
3422Return the error message (if any). Does not work if delimiter is `)'.
3423Works before syntax recognition is done."
3424 ;; Works *before* syntax recognition is done
3425 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3426 (let (st b reset-st)
3427 (condition-case b
3428 (progn
3429 (setq st (cperl-cached-syntax-table st-l))
3430 (modify-syntax-entry ?\( "()" st)
3431 (modify-syntax-entry ?\) ")(" st)
3432 (setq reset-st (syntax-table))
3433 (set-syntax-table st)
3434 (forward-sexp 1))
3435 (error (message
3436 "cperl-forward-group-in-re: error %s" b)))
3437 ;; now restore the initial state
3438 (if st
3439 (progn
3440 (modify-syntax-entry ?\( "." st)
3441 (modify-syntax-entry ?\) "." st)))
3442 (if reset-st
3443 (set-syntax-table reset-st))
3444 b))
3445
3446
3447(defvar font-lock-string-face)
3448;;(defvar font-lock-reference-face)
3449(defvar font-lock-constant-face)
3450(defsubst cperl-postpone-fontification (b e type val &optional now)
3451 ;; Do after syntactic fontification?
3452 (if cperl-syntaxify-by-font-lock
3453 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3454 (put-text-property b e type val)))
3455
3456;;; Here is how the global structures (those which cannot be
3457;;; recognized locally) are marked:
3458;; a) PODs:
3459;; Start-to-end is marked `in-pod' ==> t
3460;; Each non-literal part is marked `syntax-type' ==> `pod'
3461;; Each literal part is marked `syntax-type' ==> `in-pod'
3462;; b) HEREs:
3463;; Start-to-end is marked `here-doc-group' ==> t
3464;; The body is marked `syntax-type' ==> `here-doc'
3465;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3466;; c) FORMATs:
3467;; First line (to =) marked `first-format-line' ==> t
3468;; After-this--to-end is marked `syntax-type' ==> `format'
3469;; d) 'Q'uoted string:
3470;; part between markers inclusive is marked `syntax-type' ==> `string'
3471;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3472;; second part of s///e is marked `syntax-type' ==> `multiline'
3473;; e) Attributes of subroutines: `attrib-group' ==> t
3474;; (or 0 if declaration); up to `{' or ';': `syntax-type' => `sub-decl'.
3475;; f) Multiline my/our declaration lists etc: `syntax-type' => `multiline'
3476
3477;;; In addition, some parts of RExes may be marked as `REx-interpolated'
3478;;; (value: 0 in //o, 1 if "interpolated variable" is whole-REx, t otherwise).
3479
3480(defun cperl-unwind-to-safe (before &optional end)
3481 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3482 (let ((pos (point)) opos)
3483 (while (and pos (progn
3484 (beginning-of-line)
3485 (get-text-property (setq pos (point)) 'syntax-type)))
3486 (setq opos pos
3487 pos (cperl-beginning-of-property pos 'syntax-type))
3488 (if (eq pos (point-min))
3489 (setq pos nil))
3490 (if pos
3491 (if before
3492 (progn
3493 (goto-char (cperl-1- pos))
3494 (beginning-of-line)
3495 (setq pos (point)))
3496 (goto-char (setq pos (cperl-1- pos))))
3497 ;; Up to the start
3498 (goto-char (point-min))))
3499 ;; Skip empty lines
3500 (and (looking-at "\n*=")
3501 (/= 0 (skip-chars-backward "\n"))
3502 (forward-char))
3503 (setq pos (point))
3504 (if end
3505 ;; Do the same for end, going small steps
3506 (save-excursion
3507 (while (and end (get-text-property end 'syntax-type))
3508 (setq pos end
3509 end (next-single-property-change end 'syntax-type nil (point-max)))
3510 (if end (progn (goto-char end)
3511 (or (bolp) (forward-line 1))
3512 (setq end (point)))))
3513 (or end pos)))))
3514
3515;;; These are needed for byte-compile (at least with v19)
3516(defvar cperl-nonoverridable-face)
3517(defvar font-lock-variable-name-face)
3518(defvar font-lock-function-name-face)
3519(defvar font-lock-keyword-face)
3520(defvar font-lock-builtin-face)
3521(defvar font-lock-type-face)
3522(defvar font-lock-comment-face)
3523(defvar font-lock-warning-face)
3524
3525(defun cperl-find-sub-attrs (&optional st-l b-fname e-fname pos)
3526 "Syntaxically mark (and fontify) attributes of a subroutine.
3527Should be called with the point before leading colon of an attribute."
3528 ;; Works *before* syntax recognition is done
3529 (or st-l (setq st-l (list nil))) ; Avoid overwriting '()
3530 (let (st b p reset-st after-first (start (point)) start1 end1)
3531 (condition-case b
3532 (while (looking-at
3533 (concat
3534 "\\(" ; 1=optional? colon
3535 ":" cperl-maybe-white-and-comment-rex ; 2=whitespace/comment?
3536 "\\)"
3537 (if after-first "?" "")
3538 ;; No space between name and paren allowed...
3539 "\\(\\sw+\\)" ; 3=name
3540 "\\((\\)?")) ; 4=optional paren
3541 (and (match-beginning 1)
3542 (cperl-postpone-fontification
3543 (match-beginning 0) (cperl-1+ (match-beginning 0))
3544 'face font-lock-constant-face))
3545 (setq start1 (match-beginning 3) end1 (match-end 3))
3546 (cperl-postpone-fontification start1 end1
3547 'face font-lock-constant-face)
3548 (goto-char end1) ; end or before `('
3549 (if (match-end 4) ; Have attribute arguments...
3550 (progn
3551 (if st nil
3552 (setq st (cperl-cached-syntax-table st-l))
3553 (modify-syntax-entry ?\( "()" st)
3554 (modify-syntax-entry ?\) ")(" st))
3555 (setq reset-st (syntax-table) p (point))
3556 (set-syntax-table st)
3557 (forward-sexp 1)
3558 (set-syntax-table reset-st)
3559 (setq reset-st nil)
3560 (cperl-commentify p (point) t))) ; mark as string
3561 (forward-comment (buffer-size))
3562 (setq after-first t))
3563 (error (message
3564 "L%d: attribute `%s': %s"
3565 (count-lines (point-min) (point))
3566 (and start1 end1 (buffer-substring start1 end1)) b)
3567 (setq start nil)))
3568 (and start
3569 (progn
3570 (put-text-property start (point)
3571 'attrib-group (if (looking-at "{") t 0))
3572 (and pos
3573 (< 1 (count-lines (+ 3 pos) (point))) ; end of `sub'
3574 ;; Apparently, we do not need `multiline': faces added now
3575 (put-text-property (+ 3 pos) (cperl-1+ (point))
3576 'syntax-type 'sub-decl))
3577 (and b-fname ; Fontify here: the following condition
3578 (cperl-postpone-fontification ; is too hard to determine by
3579 b-fname e-fname 'face ; a REx, so do it here
3580 (if (looking-at "{")
3581 font-lock-function-name-face
3582 font-lock-variable-name-face)))))
3583 ;; now restore the initial state
3584 (if st
3585 (progn
3586 (modify-syntax-entry ?\( "." st)
3587 (modify-syntax-entry ?\) "." st)))
3588 (if reset-st
3589 (set-syntax-table reset-st))))
3590
3591(defsubst cperl-look-at-leading-count (is-x-REx e)
3592 (if (and
3593 (< (point) e)
3594 (re-search-forward (concat "\\=" (if is-x-REx "[ \t\n]*" "") "[{?+*]")
3595 (1- e) t)) ; return nil on failure, no moving
3596 (if (eq ?\{ (preceding-char)) nil
3597 (cperl-postpone-fontification
3598 (1- (point)) (point)
3599 'face font-lock-warning-face))))
3600
3601;; Do some smarter-highlighting
3602;; XXXX Currently ignores alphanum/dash delims,
3603(defsubst cperl-highlight-charclass (endbracket dashface bsface onec-space)
3604 (let ((l '(1 5 7)) ll lle lll
3605 ;; 2 groups, the first takes the whole match (include \[trnfabe])
3606 (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{[^{}]*}" "\\)")))
3607 (while ; look for unescaped - between non-classes
3608 (re-search-forward
3609 ;; On 19.33, certain simplifications lead
3610 ;; to bugs (as in [^a-z] \\| [trnfabe] )
3611 (concat ; 1: SingleChar (include \[trnfabe])
3612 singleChar
3613 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^cdg-mo-qsu-zA-Z0-9_]" "\\|" "\\\\c." "\\|" "\\\\x" "\\([0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}\\)" "\\|" "\\\\0?[0-7][0-7]?[0-7]?" "\\|" "\\\\N{[^{}]*}" "\\)"
3614 "\\(" ; 3: DASH SingleChar (match optionally)
3615 "\\(-\\)" ; 4: DASH
3616 singleChar ; 5: SingleChar
3617 ;;"\\(" "[^\\\\]" "\\|" "\\\\[^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{[^{}]*}" "\\)"
3618 "\\)?"
3619 "\\|"
3620 "\\(" ; 7: other escapes
3621 "\\\\[pP]" "\\([^{]\\|{[^{}]*}\\)"
3622 "\\|" "\\\\[^pP]" "\\)"
3623 )
3624 endbracket 'toend)
3625 (if (match-beginning 4)
3626 (cperl-postpone-fontification
3627 (match-beginning 4) (match-end 4)
3628 'face dashface))
3629 ;; save match data (for looking-at)
3630 (setq lll (mapcar (function (lambda (elt) (cons (match-beginning elt)
3631 (match-end elt)))) l))
3632 (while lll
3633 (setq ll (car lll))
3634 (setq lle (cdr ll)
3635 ll (car ll))
3636 ;; (message "Got %s of %s" ll l)
3637 (if (and ll (eq (char-after ll) ?\\ ))
3638 (save-excursion
3639 (goto-char ll)
3640 (cperl-postpone-fontification ll (1+ ll)
3641 'face bsface)
3642 (if (looking-at "\\\\[a-zA-Z0-9]")
3643 (cperl-postpone-fontification (1+ ll) lle
3644 'face onec-space))))
3645 (setq lll (cdr lll))))
3646 (goto-char endbracket) ; just in case something misbehaves???
3647 t))
3648
3649;;; Debugging this may require (setq max-specpdl-size 2000)...
3650(defun cperl-find-pods-heres (&optional min max non-inter end ignore-max end-of-here-doc)
3651 "Scans the buffer for hard-to-parse Perl constructions.
3652If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3653the sections using `cperl-pod-head-face', `cperl-pod-face',
3654`cperl-here-face'."
3655 (interactive)
3656 (or min (setq min (point-min)
3657 cperl-syntax-state nil
3658 cperl-syntax-done-to min))
3659 (or max (setq max (point-max)))
3660 (let* ((cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3661 face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3662 is-REx is-x-REx REx-subgr-start REx-subgr-end was-subgr i2 hairy-RE
3663 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3664 (modified (buffer-modified-p)) overshoot is-o-REx name
3665 (after-change-functions nil)
3666 (cperl-font-locking t)
3667 (use-syntax-state (and cperl-syntax-state
3668 (>= min (car cperl-syntax-state))))
3669 (state-point (if use-syntax-state
3670 (car cperl-syntax-state)
3671 (point-min)))
3672 (state (if use-syntax-state
3673 (cdr cperl-syntax-state)))
3674 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3675 (st-l (list nil)) (err-l (list nil))
3676 ;; Somehow font-lock may be not loaded yet...
3677 ;; (e.g., when building TAGS via command-line call)
3678 (font-lock-string-face (if (boundp 'font-lock-string-face)
3679 font-lock-string-face
3680 'font-lock-string-face))
3681 (my-cperl-delimiters-face (if (boundp 'font-lock-constant-face)
3682 font-lock-constant-face
3683 'font-lock-constant-face))
3684 (my-cperl-REx-spec-char-face ; [] ^.$ and wrapper-of ({})
3685 (if (boundp 'font-lock-function-name-face)
3686 font-lock-function-name-face
3687 'font-lock-function-name-face))
3688 (font-lock-variable-name-face ; interpolated vars and ({})-code
3689 (if (boundp 'font-lock-variable-name-face)
3690 font-lock-variable-name-face
3691 'font-lock-variable-name-face))
3692 (font-lock-function-name-face ; used in `cperl-find-sub-attrs'
3693 (if (boundp 'font-lock-function-name-face)
3694 font-lock-function-name-face
3695 'font-lock-function-name-face))
3696 (font-lock-constant-face ; used in `cperl-find-sub-attrs'
3697 (if (boundp 'font-lock-constant-face)
3698 font-lock-constant-face
3699 'font-lock-constant-face))
3700 (my-cperl-REx-0length-face ; 0-length, (?:)etc, non-literal \
3701 (if (boundp 'font-lock-builtin-face)
3702 font-lock-builtin-face
3703 'font-lock-builtin-face))
3704 (font-lock-comment-face
3705 (if (boundp 'font-lock-comment-face)
3706 font-lock-comment-face
3707 'font-lock-comment-face))
3708 (font-lock-warning-face
3709 (if (boundp 'font-lock-warning-face)
3710 font-lock-warning-face
3711 'font-lock-warning-face))
3712 (my-cperl-REx-ctl-face ; (|)
3713 (if (boundp 'font-lock-keyword-face)
3714 font-lock-keyword-face
3715 'font-lock-keyword-face))
3716 (my-cperl-REx-modifiers-face ; //gims
3717 (if (boundp 'cperl-nonoverridable-face)
3718 cperl-nonoverridable-face
3719 'cperl-nonoverridable-face))
3720 (my-cperl-REx-length1-face ; length=1 escaped chars, POSIX classes
3721 (if (boundp 'font-lock-type-face)
3722 font-lock-type-face
3723 'font-lock-type-face))
3724 (stop-point (if ignore-max
3725 (point-max)
3726 max))
3727 (search
3728 (concat
3729 "\\(\\`\n?\\|^\n\\)=" ; POD
3730 "\\|"
3731 ;; One extra () before this:
3732 "<<" ; HERE-DOC
3733 "\\(" ; 1 + 1
3734 ;; First variant "BLAH" or just ``.
3735 "[ \t]*" ; Yes, whitespace is allowed!
3736 "\\([\"'`]\\)" ; 2 + 1 = 3
3737 "\\([^\"'`\n]*\\)" ; 3 + 1
3738 "\\3"
3739 "\\|"
3740 ;; Second variant: Identifier or \ID (same as 'ID') or empty
3741 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3742 ;; Do not have <<= or << 30 or <<30 or << $blah.
3743 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3744 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3745 "\\)"
3746 "\\|"
3747 ;; 1+6 extra () before this:
3748 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$" ;FRMAT
3749 (if cperl-use-syntax-table-text-property
3750 (concat
3751 "\\|"
3752 ;; 1+6+2=9 extra () before this:
3753 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>" ; QUOTED CONSTRUCT
3754 "\\|"
3755 ;; 1+6+2+1=10 extra () before this:
3756 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3757 "\\|"
3758 ;; 1+6+2+1+1=11 extra () before this
3759 "\\<sub\\>" ; sub with proto/attr
3760 "\\("
3761 cperl-white-and-comment-rex
3762 "\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
3763 "\\("
3764 cperl-maybe-white-and-comment-rex
3765 "\\(([^()]*)\\|:[^:]\\)\\)" ; prototype or attribute start
3766 "\\|"
3767 ;; 1+6+2+1+1+6=17 extra () before this:
3768 "\\$\\(['{]\\)" ; $' or ${foo}
3769 "\\|"
3770 ;; 1+6+2+1+1+6+1=18 extra () before this (old pack'var syntax;
3771 ;; we do not support intervening comments...):
3772 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3773 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
3774 "\\|"
3775 "__\\(END\\|DATA\\)__" ; __END__ or __DATA__
3776 ;; 1+6+2+1+1+6+1+1+1=20 extra () before this:
3777 "\\|"
3778 "\\\\\\(['`\"($]\\)") ; BACKWACKED something-hairy
3779 ""))))
3780 (unwind-protect
3781 (progn
3782 (save-excursion
3783 (or non-inter
3784 (message "Scanning for \"hard\" Perl constructions..."))
3785 ;;(message "find: %s --> %s" min max)
3786 (and cperl-pod-here-fontify
3787 ;; We had evals here, do not know why...
3788 (setq face cperl-pod-face
3789 head-face cperl-pod-head-face
3790 here-face cperl-here-face))
3791 (remove-text-properties min max
3792 '(syntax-type t in-pod t syntax-table t
3793 attrib-group t
3794 REx-interpolated t
3795 cperl-postpone t
3796 syntax-subtype t
3797 rear-nonsticky t
3798 front-sticky t
3799 here-doc-group t
3800 first-format-line t
3801 REx-part2 t
3802 indentable t))
3803 ;; Need to remove face as well...
3804 (goto-char min)
3805 (and (eq system-type 'emx)
3806 (eq (point) 1)
3807 (let ((case-fold-search t))
3808 (looking-at "extproc[ \t]")) ; Analogue of #!
3809 (cperl-commentify min
3810 (save-excursion (end-of-line) (point))
3811 nil))
3812 (while (and
3813 (< (point) max)
3814 (re-search-forward search max t))
3815 (setq tmpend nil) ; Valid for most cases
3816 (setq b (match-beginning 0)
3817 state (save-excursion (parse-partial-sexp
3818 state-point b nil nil state))
3819 state-point b)
3820 (cond
3821 ;; 1+6+2+1+1+6=17 extra () before this:
3822 ;; "\\$\\(['{]\\)"
3823 ((match-beginning 18) ; $' or ${foo}
3824 (if (eq (preceding-char) ?\') ; $'
3825 (progn
3826 (setq b (1- (point))
3827 state (parse-partial-sexp
3828 state-point (1- b) nil nil state)
3829 state-point (1- b))
3830 (if (nth 3 state) ; in string
3831 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3832 (goto-char (1+ b)))
3833 ;; else: ${
3834 (setq bb (match-beginning 0))
3835 (cperl-modify-syntax-type bb cperl-st-punct)))
3836 ;; No processing in strings/comments beyond this point:
3837 ((or (nth 3 state) (nth 4 state))
3838 t) ; Do nothing in comment/string
3839 ((match-beginning 1) ; POD section
3840 ;; "\\(\\`\n?\\|^\n\\)="
3841 (setq b (match-beginning 0)
3842 state (parse-partial-sexp
3843 state-point b nil nil state)
3844 state-point b)
3845 (if (or (nth 3 state) (nth 4 state)
3846 (looking-at "cut\\>"))
3847 (if (or (nth 3 state) (nth 4 state) ignore-max)
3848 nil ; Doing a chunk only
3849 (message "=cut is not preceded by a POD section")
3850 (or (car err-l) (setcar err-l (point))))
3851 (beginning-of-line)
3852
3853 (setq b (point)
3854 bb b
3855 tb (match-beginning 0)
3856 b1 nil) ; error condition
3857 ;; We do not search to max, since we may be called from
3858 ;; some hook of fontification, and max is random
3859 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3860 (progn
3861 (goto-char b)
3862 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3863 (progn
3864 (message "=cut is not preceded by an empty line")
3865 (setq b1 t)
3866 (or (car err-l) (setcar err-l b))))))
3867 (beginning-of-line 2) ; An empty line after =cut is not POD!
3868 (setq e (point))
3869 (and (> e max)
3870 (progn
3871 (remove-text-properties
3872 max e '(syntax-type t in-pod t syntax-table t
3873 attrib-group t
3874 REx-interpolated t
3875 cperl-postpone t
3876 syntax-subtype t
3877 here-doc-group t
3878 rear-nonsticky t
3879 front-sticky t
3880 first-format-line t
3881 REx-part2 t
3882 indentable t))
3883 (setq tmpend tb)))
3884 (put-text-property b e 'in-pod t)
3885 (put-text-property b e 'syntax-type 'in-pod)
3886 (goto-char b)
3887 (while (re-search-forward "\n\n[ \t]" e t)
3888 ;; We start 'pod 1 char earlier to include the preceding line
3889 (beginning-of-line)
3890 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3891 (cperl-put-do-not-fontify b (point) t)
3892 ;; mark the non-literal parts as PODs
3893 (if cperl-pod-here-fontify
3894 (cperl-postpone-fontification b (point) 'face face t))
3895 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3896 (beginning-of-line)
3897 (setq b (point)))
3898 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3899 (cperl-put-do-not-fontify (point) e t)
3900 (if cperl-pod-here-fontify
3901 (progn
3902 ;; mark the non-literal parts as PODs
3903 (cperl-postpone-fontification (point) e 'face face t)
3904 (goto-char bb)
3905 (if (looking-at
3906 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3907 ;; mark the headers
3908 (cperl-postpone-fontification
3909 (match-beginning 1) (match-end 1)
3910 'face head-face))
3911 (while (re-search-forward
3912 ;; One paragraph
3913 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3914 e 'toend)
3915 ;; mark the headers
3916 (cperl-postpone-fontification
3917 (match-beginning 1) (match-end 1)
3918 'face head-face))))
3919 (cperl-commentify bb e nil)
3920 (goto-char e)
3921 (or (eq e (point-max))
3922 (forward-char -1)))) ; Prepare for immediate POD start.
3923 ;; Here document
3924 ;; We can do many here-per-line;
3925 ;; but multiline quote on the same line as <<HERE confuses us...
3926 ;; ;; One extra () before this:
3927 ;;"<<"
3928 ;; "\\(" ; 1 + 1
3929 ;; ;; First variant "BLAH" or just ``.
3930 ;; "[ \t]*" ; Yes, whitespace is allowed!
3931 ;; "\\([\"'`]\\)" ; 2 + 1
3932 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3933 ;; "\\3"
3934 ;; "\\|"
3935 ;; ;; Second variant: Identifier or \ID or empty
3936 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3937 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3938 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3939 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3940 ;; "\\)"
3941 ((match-beginning 2) ; 1 + 1
3942 (setq b (point)
3943 tb (match-beginning 0)
3944 c (and ; not HERE-DOC
3945 (match-beginning 5)
3946 (save-match-data
3947 (or (looking-at "[ \t]*(") ; << function_call()
3948 (save-excursion ; 1 << func_name, or $foo << 10
3949 (condition-case nil
3950 (progn
3951 (goto-char tb)
3952 ;;; XXX What to do: foo <<bar ???
3953 ;;; XXX Need to support print {a} <<B ???
3954 (forward-sexp -1)
3955 (save-match-data
3956 ; $foo << b; $f .= <<B;
3957 ; ($f+1) << b; a($f) . <<B;
3958 ; foo 1, <<B; $x{a} <<b;
3959 (cond
3960 ((looking-at "[0-9$({]")
3961 (forward-sexp 1)
3962 (and
3963 (looking-at "[ \t]*<<")
3964 (condition-case nil
3965 ;; print $foo <<EOF
3966 (progn
3967 (forward-sexp -2)
3968 (not
3969 (looking-at "\\(printf?\\|system\\|exec\\|sort\\)\\>")))
3970 (error t)))))))
3971 (error nil))) ; func(<<EOF)
3972 (and (not (match-beginning 6)) ; Empty
3973 (looking-at
3974 "[ \t]*[=0-9$@%&(]"))))))
3975 (if c ; Not here-doc
3976 nil ; Skip it.
3977 (setq c (match-end 2)) ; 1 + 1
3978 (if (match-beginning 5) ;4 + 1
3979 (setq b1 (match-beginning 5) ; 4 + 1
3980 e1 (match-end 5)) ; 4 + 1
3981 (setq b1 (match-beginning 4) ; 3 + 1
3982 e1 (match-end 4))) ; 3 + 1
3983 (setq tag (buffer-substring b1 e1)
3984 qtag (regexp-quote tag))
3985 (cond (cperl-pod-here-fontify
3986 ;; Highlight the starting delimiter
3987 (cperl-postpone-fontification
3988 b1 e1 'face my-cperl-delimiters-face)
3989 (cperl-put-do-not-fontify b1 e1 t)))
3990 (forward-line)
3991 (setq i (point))
3992 (if end-of-here-doc
3993 (goto-char end-of-here-doc))
3994 (setq b (point))
3995 ;; We do not search to max, since we may be called from
3996 ;; some hook of fontification, and max is random
3997 (or (and (re-search-forward (concat "^" qtag "$")
3998 stop-point 'toend)
3999 ;;;(eq (following-char) ?\n) ; XXXX WHY???
4000 )
4001 (progn ; Pretend we matched at the end
4002 (goto-char (point-max))
4003 (re-search-forward "\\'")
4004 (message "End of here-document `%s' not found." tag)
4005 (or (car err-l) (setcar err-l b))))
4006 (if cperl-pod-here-fontify
4007 (progn
4008 ;; Highlight the ending delimiter
4009 (cperl-postpone-fontification
4010 (match-beginning 0) (match-end 0)
4011 'face my-cperl-delimiters-face)
4012 (cperl-put-do-not-fontify b (match-end 0) t)
4013 ;; Highlight the HERE-DOC
4014 (cperl-postpone-fontification b (match-beginning 0)
4015 'face here-face)))
4016 (setq e1 (cperl-1+ (match-end 0)))
4017 (put-text-property b (match-beginning 0)
4018 'syntax-type 'here-doc)
4019 (put-text-property (match-beginning 0) e1
4020 'syntax-type 'here-doc-delim)
4021 (put-text-property b e1 'here-doc-group t)
4022 ;; This makes insertion at the start of HERE-DOC update
4023 ;; the whole construct:
4024 (put-text-property b (cperl-1+ b) 'front-sticky '(syntax-type))
4025 (cperl-commentify b e1 nil)
4026 (cperl-put-do-not-fontify b (match-end 0) t)
4027 ;; Cache the syntax info...
4028 (setq cperl-syntax-state (cons state-point state))
4029 ;; ... and process the rest of the line...
4030 (setq overshoot
4031 (elt ; non-inter ignore-max
4032 (cperl-find-pods-heres c i t end t e1) 1))
4033 (if (and overshoot (> overshoot (point)))
4034 (goto-char overshoot)
4035 (setq overshoot e1))
4036 (if (> e1 max)
4037 (setq tmpend tb))))
4038 ;; format
4039 ((match-beginning 8)
4040 ;; 1+6=7 extra () before this:
4041 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
4042 (setq b (point)
4043 name (if (match-beginning 8) ; 7 + 1
4044 (buffer-substring (match-beginning 8) ; 7 + 1
4045 (match-end 8)) ; 7 + 1
4046 "")
4047 tb (match-beginning 0))
4048 (setq argument nil)
4049 (put-text-property (save-excursion
4050 (beginning-of-line)
4051 (point))
4052 b 'first-format-line 't)
4053 (if cperl-pod-here-fontify
4054 (while (and (eq (forward-line) 0)
4055 (not (looking-at "^[.;]$")))
4056 (cond
4057 ((looking-at "^#")) ; Skip comments
4058 ((and argument ; Skip argument multi-lines
4059 (looking-at "^[ \t]*{"))
4060 (forward-sexp 1)
4061 (setq argument nil))
4062 (argument ; Skip argument lines
4063 (setq argument nil))
4064 (t ; Format line
4065 (setq b1 (point))
4066 (setq argument (looking-at "^[^\n]*[@^]"))
4067 (end-of-line)
4068 ;; Highlight the format line
4069 (cperl-postpone-fontification b1 (point)
4070 'face font-lock-string-face)
4071 (cperl-commentify b1 (point) nil)
4072 (cperl-put-do-not-fontify b1 (point) t))))
4073 ;; We do not search to max, since we may be called from
4074 ;; some hook of fontification, and max is random
4075 (re-search-forward "^[.;]$" stop-point 'toend))
4076 (beginning-of-line)
4077 (if (looking-at "^\\.$") ; ";" is not supported yet
4078 (progn
4079 ;; Highlight the ending delimiter
4080 (cperl-postpone-fontification (point) (+ (point) 2)
4081 'face font-lock-string-face)
4082 (cperl-commentify (point) (+ (point) 2) nil)
4083 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
4084 (message "End of format `%s' not found." name)
4085 (or (car err-l) (setcar err-l b)))
4086 (forward-line)
4087 (if (> (point) max)
4088 (setq tmpend tb))
4089 (put-text-property b (point) 'syntax-type 'format))
4090 ;; qq-like String or Regexp:
4091 ((or (match-beginning 10) (match-beginning 11))
4092 ;; 1+6+2=9 extra () before this:
4093 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
4094 ;; "\\|"
4095 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
4096 (setq b1 (if (match-beginning 10) 10 11)
4097 argument (buffer-substring
4098 (match-beginning b1) (match-end b1))
4099 b (point) ; end of qq etc
4100 i b
4101 c (char-after (match-beginning b1))
4102 bb (char-after (1- (match-beginning b1))) ; tmp holder
4103 ;; bb == "Not a stringy"
4104 bb (if (eq b1 10) ; user variables/whatever
4105 (and (memq bb (append "$@%*#_:-&>" nil)) ; $#y)
4106 (cond ((eq bb ?-) (eq c ?s)) ; -s file test
4107 ((eq bb ?\:) ; $opt::s
4108 (eq (char-after
4109 (- (match-beginning b1) 2))
4110 ?\:))
4111 ((eq bb ?\>) ; $foo->s
4112 (eq (char-after
4113 (- (match-beginning b1) 2))
4114 ?\-))
4115 ((eq bb ?\&)
4116 (not (eq (char-after ; &&m/blah/
4117 (- (match-beginning b1) 2))
4118 ?\&)))
4119 (t t)))
4120 ;; <file> or <$file>
4121 (and (eq c ?\<)
4122 ;; Do not stringify <FH>, <$fh> :
4123 (save-match-data
4124 (looking-at
4125 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
4126 tb (match-beginning 0))
4127 (goto-char (match-beginning b1))
4128 (cperl-backward-to-noncomment (point-min))
4129 (or bb
4130 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
4131 (setq argument ""
4132 b1 nil
4133 bb ; Not a regexp?
4134 (not
4135 ;; What is below: regexp-p?
4136 (and
4137 (or (memq (preceding-char)
4138 (append (if (memq c '(?\? ?\<))
4139 ;; $a++ ? 1 : 2
4140 "~{(=|&*!,;:["
4141 "~{(=|&+-*!,;:[") nil))
4142 (and (eq (preceding-char) ?\})
4143 (cperl-after-block-p (point-min)))
4144 (and (eq (char-syntax (preceding-char)) ?w)
4145 (progn
4146 (forward-sexp -1)
4147;; After these keywords `/' starts a RE. One should add all the
4148;; functions/builtins which expect an argument, but ...
4149 (if (eq (preceding-char) ?-)
4150 ;; -d ?foo? is a RE
4151 (looking-at "[a-zA-Z]\\>")
4152 (and
4153 (not (memq (preceding-char)
4154 '(?$ ?@ ?& ?%)))
4155 (looking-at
4156 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
4157 (and (eq (preceding-char) ?.)
4158 (eq (char-after (- (point) 2)) ?.))
4159 (bobp))
4160 ;; m|blah| ? foo : bar;
4161 (not
4162 (and (eq c ?\?)
4163 cperl-use-syntax-table-text-property
4164 (not (bobp))
4165 (progn
4166 (forward-char -1)
4167 (looking-at "\\s|"))))))
4168 b (1- b))
4169 ;; s y tr m
4170 ;; Check for $a -> y
4171 (setq b1 (preceding-char)
4172 go (point))
4173 (if (and (eq b1 ?>)
4174 (eq (char-after (- go 2)) ?-))
4175 ;; Not a regexp
4176 (setq bb t))))
4177 (or bb
4178 (progn
4179 (goto-char b)
4180 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4181 (goto-char (match-end 0))
4182 (skip-chars-forward " \t\n\f"))
4183 (cond ((and (eq (following-char) ?\})
4184 (eq b1 ?\{))
4185 ;; Check for $a[23]->{ s }, @{s} and *{s::foo}
4186 (goto-char (1- go))
4187 (skip-chars-backward " \t\n\f")
4188 (if (memq (preceding-char) (append "$@%&*" nil))
4189 (setq bb t) ; @{y}
4190 (condition-case nil
4191 (forward-sexp -1)
4192 (error nil)))
4193 (if (or bb
4194 (looking-at ; $foo -> {s}
4195 "[$@]\\$*\\([a-zA-Z0-9_:]+\\|[^{]\\)\\([ \t\n]*->\\)?[ \t\n]*{")
4196 (and ; $foo[12] -> {s}
4197 (memq (following-char) '(?\{ ?\[))
4198 (progn
4199 (forward-sexp 1)
4200 (looking-at "\\([ \t\n]*->\\)?[ \t\n]*{"))))
4201 (setq bb t)
4202 (goto-char b)))
4203 ((and (eq (following-char) ?=)
4204 (eq (char-after (1+ (point))) ?\>))
4205 ;; Check for { foo => 1, s => 2 }
4206 ;; Apparently s=> is never a substitution...
4207 (setq bb t))
4208 ((and (eq (following-char) ?:)
4209 (eq b1 ?\{) ; Check for $ { s::bar }
4210 (looking-at "::[a-zA-Z0-9_:]*[ \t\n\f]*}")
4211 (progn
4212 (goto-char (1- go))
4213 (skip-chars-backward " \t\n\f")
4214 (memq (preceding-char)
4215 (append "$@%&*" nil))))
4216 (setq bb t))
4217 ((eobp)
4218 (setq bb t)))))
4219 (if bb
4220 (goto-char i)
4221 ;; Skip whitespace and comments...
4222 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
4223 (goto-char (match-end 0))
4224 (skip-chars-forward " \t\n\f"))
4225 (if (> (point) b)
4226 (put-text-property b (point) 'syntax-type 'prestring))
4227 ;; qtag means two-arg matcher, may be reset to
4228 ;; 2 or 3 later if some special quoting is needed.
4229 ;; e1 means matching-char matcher.
4230 (setq b (point) ; before the first delimiter
4231 ;; has 2 args
4232 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
4233 ;; We do not search to max, since we may be called from
4234 ;; some hook of fontification, and max is random
4235 i (cperl-forward-re stop-point end
4236 i2
4237 st-l err-l argument)
4238 ;; If `go', then it is considered as 1-arg, `b1' is nil
4239 ;; as in s/foo//x; the point is before final "slash"
4240 b1 (nth 1 i) ; start of the second part
4241 tag (nth 2 i) ; ender-char, true if second part
4242 ; is with matching chars []
4243 go (nth 4 i) ; There is a 1-char part after the end
4244 i (car i) ; intermediate point
4245 e1 (point) ; end
4246 ;; Before end of the second part if non-matching: ///
4247 tail (if (and i (not tag))
4248 (1- e1))
4249 e (if i i e1) ; end of the first part
4250 qtag nil ; need to preserve backslashitis
4251 is-x-REx nil is-o-REx nil); REx has //x //o modifiers
4252 ;; If s{} (), then b/b1 are at "{", "(", e1/i after ")", "}"
4253 ;; Commenting \\ is dangerous, what about ( ?
4254 (and i tail
4255 (eq (char-after i) ?\\)
4256 (setq qtag t))
4257 (and (if go (looking-at ".\\sw*x")
4258 (looking-at "\\sw*x")) ; qr//x
4259 (setq is-x-REx t))
4260 (and (if go (looking-at ".\\sw*o")
4261 (looking-at "\\sw*o")) ; //o
4262 (setq is-o-REx t))
4263 (if (null i)
4264 ;; Considered as 1arg form
4265 (progn
4266 (cperl-commentify b (point) t)
4267 (put-text-property b (point) 'syntax-type 'string)
4268 (if (or is-x-REx
4269 ;; ignore other text properties:
4270 (string-match "^qw$" argument))
4271 (put-text-property b (point) 'indentable t))
4272 (and go
4273 (setq e1 (cperl-1+ e1))
4274 (or (eobp)
4275 (forward-char 1))))
4276 (cperl-commentify b i t)
4277 (if (looking-at "\\sw*e") ; s///e
4278 (progn
4279 ;; Cache the syntax info...
4280 (setq cperl-syntax-state (cons state-point state))
4281 (and
4282 ;; silent:
4283 (car (cperl-find-pods-heres b1 (1- (point)) t end))
4284 ;; Error
4285 (goto-char (1+ max)))
4286 (if (and tag (eq (preceding-char) ?\>))
4287 (progn
4288 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
4289 (cperl-modify-syntax-type i cperl-st-bra)))
4290 (put-text-property b i 'syntax-type 'string)
4291 (put-text-property i (point) 'syntax-type 'multiline)
4292 (if is-x-REx
4293 (put-text-property b i 'indentable t)))
4294 (cperl-commentify b1 (point) t)
4295 (put-text-property b (point) 'syntax-type 'string)
4296 (if is-x-REx
4297 (put-text-property b i 'indentable t))
4298 (if qtag
4299 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
4300 (setq tail nil)))
4301 ;; Now: tail: if the second part is non-matching without ///e
4302 (if (eq (char-syntax (following-char)) ?w)
4303 (progn
4304 (forward-word 1) ; skip modifiers s///s
4305 (if tail (cperl-commentify tail (point) t))
4306 (cperl-postpone-fontification
4307 e1 (point) 'face my-cperl-REx-modifiers-face)))
4308 ;; Check whether it is m// which means "previous match"
4309 ;; and highlight differently
4310 (setq is-REx
4311 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
4312 (or (not (= (length argument) 0))
4313 (not (eq c ?\<)))))
4314 (if (and is-REx
4315 (eq e (+ 2 b))
4316 ;; split // *is* using zero-pattern
4317 (save-excursion
4318 (condition-case nil
4319 (progn
4320 (goto-char tb)
4321 (forward-sexp -1)
4322 (not (looking-at "split\\>")))
4323 (error t))))
4324 (cperl-postpone-fontification
4325 b e 'face font-lock-warning-face)
4326 (if (or i2 ; Has 2 args
4327 (and cperl-fontify-m-as-s
4328 (or
4329 (string-match "^\\(m\\|qr\\)$" argument)
4330 (and (eq 0 (length argument))
4331 (not (eq ?\< (char-after b)))))))
4332 (progn
4333 (cperl-postpone-fontification
4334 b (cperl-1+ b) 'face my-cperl-delimiters-face)
4335 (cperl-postpone-fontification
4336 (1- e) e 'face my-cperl-delimiters-face)))
4337 (if (and is-REx cperl-regexp-scan)
4338 ;; Process RExen: embedded comments, charclasses and ]
4339;;;/\3333\xFg\x{FFF}a\ppp\PPP\qqq\C\99f(?{ foo })(??{ foo })/;
4340;;;/a\.b[^a[:ff:]b]x$ab->$[|$,$ab->[cd]->[ef]|$ab[xy].|^${a,b}{c,d}/;
4341;;;/(?<=foo)(?<!bar)(x)(?:$ab|\$\/)$|\\\b\x888\776\[\:$/xxx;
4342;;;m?(\?\?{b,a})? + m/(??{aa})(?(?=xx)aa|bb)(?#aac)/;
4343;;;m$(^ab[c]\$)$ + m+(^ab[c]\$\+)+ + m](^ab[c\]$|.+)] + m)(^ab[c]$|.+\));
4344;;;m^a[\^b]c^ + m.a[^b]\.c.;
4345 (save-excursion
4346 (goto-char (1+ b))
4347 ;; First
4348 (cperl-look-at-leading-count is-x-REx e)
4349 (setq hairy-RE
4350 (concat
4351 (if is-x-REx
4352 (if (eq (char-after b) ?\#)
4353 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
4354 "\\((\\?#\\)\\|\\(#\\)")
4355 ;; keep the same count: add a fake group
4356 (if (eq (char-after b) ?\#)
4357 "\\((\\?\\\\#\\)\\(\\)"
4358 "\\((\\?#\\)\\(\\)"))
4359 "\\|"
4360 "\\(\\[\\)" ; 3=[
4361 "\\|"
4362 "\\(]\\)" ; 4=]
4363 "\\|"
4364 ;; XXXX Will not be able to use it in s)))
4365 (if (eq (char-after b) ?\) )
4366 "\\())))\\)" ; Will never match
4367 (if (eq (char-after b) ?? )
4368 ;;"\\((\\\\\\?\\(\\\\\\?\\)?{\\)"
4369 "\\((\\\\\\?\\\\\\?{\\|()\\\\\\?{\\)"
4370 "\\((\\?\\??{\\)")) ; 5= (??{ (?{
4371 "\\|" ; 6= 0-length, 7: name, 8,9:code, 10:group
4372 "\\(" ;; XXXX 1-char variables, exc. |()\s
4373 "[$@]"
4374 "\\("
4375 "[_a-zA-Z:][_a-zA-Z0-9:]*"
4376 "\\|"
4377 "{[^{}]*}" ; only one-level allowed
4378 "\\|"
4379 "[^{(|) \t\r\n\f]"
4380 "\\)"
4381 "\\(" ;;8,9:code part of array/hash elt
4382 "\\(" "->" "\\)?"
4383 "\\[[^][]*\\]"
4384 "\\|"
4385 "{[^{}]*}"
4386 "\\)*"
4387 ;; XXXX: what if u is delim?
4388 "\\|"
4389 "[)^|$.*?+]"
4390 "\\|"
4391 "{[0-9]+}"
4392 "\\|"
4393 "{[0-9]+,[0-9]*}"
4394 "\\|"
4395 "\\\\[luLUEQbBAzZG]"
4396 "\\|"
4397 "(" ; Group opener
4398 "\\(" ; 10 group opener follower
4399 "\\?\\((\\?\\)" ; 11: in (?(?=C)A|B)
4400 "\\|"
4401 "\\?[:=!>?{]" ; "?" something
4402 "\\|"
4403 "\\?[-imsx]+[:)]" ; (?i) (?-s:.)
4404 "\\|"
4405 "\\?([0-9]+)" ; (?(1)foo|bar)
4406 "\\|"
4407 "\\?<[=!]"
4408 ;;;"\\|"
4409 ;;; "\\?"
4410 "\\)?"
4411 "\\)"
4412 "\\|"
4413 "\\\\\\(.\\)" ; 12=\SYMBOL
4414 ))
4415 (while
4416 (and (< (point) (1- e))
4417 (re-search-forward hairy-RE (1- e) 'to-end))
4418 (goto-char (match-beginning 0))
4419 (setq REx-subgr-start (point)
4420 was-subgr (following-char))
4421 (cond
4422 ((match-beginning 6) ; 0-length builtins, groups
4423 (goto-char (match-end 0))
4424 (if (match-beginning 11)
4425 (goto-char (match-beginning 11)))
4426 (if (>= (point) e)
4427 (goto-char (1- e)))
4428 (cperl-postpone-fontification
4429 (match-beginning 0) (point)
4430 'face
4431 (cond
4432 ((eq was-subgr ?\) )
4433 (condition-case nil
4434 (save-excursion
4435 (forward-sexp -1)
4436 (if (> (point) b)
4437 (if (if (eq (char-after b) ?? )
4438 (looking-at "(\\\\\\?")
4439 (eq (char-after (1+ (point))) ?\?))
4440 my-cperl-REx-0length-face
4441 my-cperl-REx-ctl-face)
4442 font-lock-warning-face))
4443 (error font-lock-warning-face)))
4444 ((eq was-subgr ?\| )
4445 my-cperl-REx-ctl-face)
4446 ((eq was-subgr ?\$ )
4447 (if (> (point) (1+ REx-subgr-start))
4448 (progn
4449 (put-text-property
4450 (match-beginning 0) (point)
4451 'REx-interpolated
4452 (if is-o-REx 0
4453 (if (and (eq (match-beginning 0)
4454 (1+ b))
4455 (eq (point)
4456 (1- e))) 1 t)))
4457 font-lock-variable-name-face)
4458 my-cperl-REx-spec-char-face))
4459 ((memq was-subgr (append "^." nil) )
4460 my-cperl-REx-spec-char-face)
4461 ((eq was-subgr ?\( )
4462 (if (not (match-beginning 10))
4463 my-cperl-REx-ctl-face
4464 my-cperl-REx-0length-face))
4465 (t my-cperl-REx-0length-face)))
4466 (if (and (memq was-subgr (append "(|" nil))
4467 (not (string-match "(\\?[-imsx]+)"
4468 (match-string 0))))
4469 (cperl-look-at-leading-count is-x-REx e))
4470 (setq was-subgr nil)) ; We do stuff here
4471 ((match-beginning 12) ; \SYMBOL
4472 (forward-char 2)
4473 (if (>= (point) e)
4474 (goto-char (1- e))
4475 ;; How many chars to not highlight:
4476 ;; 0-len special-alnums in other branch =>
4477 ;; Generic: \non-alnum (1), \alnum (1+face)
4478 ;; Is-delim: \non-alnum (1/spec-2) alnum-1 (=what hai)
4479 (setq REx-subgr-start (point)
4480 qtag (preceding-char))
4481 (cperl-postpone-fontification
4482 (- (point) 2) (- (point) 1) 'face
4483 (if (memq qtag
4484 (append "ghijkmoqvFHIJKMORTVY" nil))
4485 font-lock-warning-face
4486 my-cperl-REx-0length-face))
4487 (if (and (eq (char-after b) qtag)
4488 (memq qtag (append ".])^$|*?+" nil)))
4489 (progn
4490 (if (and cperl-use-syntax-table-text-property
4491 (eq qtag ?\) ))
4492 (put-text-property
4493 REx-subgr-start (1- (point))
4494 'syntax-table cperl-st-punct))
4495 (cperl-postpone-fontification
4496 (1- (point)) (point) 'face
4497 ; \] can't appear below
4498 (if (memq qtag (append ".]^$" nil))
4499 'my-cperl-REx-spec-char-face
4500 (if (memq qtag (append "*?+" nil))
4501 'my-cperl-REx-0length-face
4502 'my-cperl-REx-ctl-face))))) ; )|
4503 ;; Test for arguments:
4504 (cond
4505 ;; This is not pretty: the 5.8.7 logic:
4506 ;; \0numx -> octal (up to total 3 dig)
4507 ;; \DIGIT -> backref unless \0
4508 ;; \DIGITs -> backref if valid
4509 ;; otherwise up to 3 -> octal
4510 ;; Do not try to distinguish, we guess
4511 ((or (and (memq qtag (append "01234567" nil))
4512 (re-search-forward
4513 "\\=[01234567]?[01234567]?"
4514 (1- e) 'to-end))
4515 (and (memq qtag (append "89" nil))
4516 (re-search-forward
4517 "\\=[0123456789]*" (1- e) 'to-end))
4518 (and (eq qtag ?x)
4519 (re-search-forward
4520 "\\=[0-9a-fA-F][0-9a-fA-F]?\\|\\={[0-9a-fA-F]+}"
4521 (1- e) 'to-end))
4522 (and (memq qtag (append "pPN" nil))
4523 (re-search-forward "\\={[^{}]+}\\|."
4524 (1- e) 'to-end))
4525 (eq (char-syntax qtag) ?w))
4526 (cperl-postpone-fontification
4527 (1- REx-subgr-start) (point)
4528 'face my-cperl-REx-length1-face))))
4529 (setq was-subgr nil)) ; We do stuff here
4530 ((match-beginning 3) ; [charclass]
4531 ;; Highlight leader, trailer, POSIX classes
4532 (forward-char 1)
4533 (if (eq (char-after b) ?^ )
4534 (and (eq (following-char) ?\\ )
4535 (eq (char-after (cperl-1+ (point)))
4536 ?^ )
4537 (forward-char 2))
4538 (and (eq (following-char) ?^ )
4539 (forward-char 1)))
4540 (setq argument b ; continue? & end of last POSIX
4541 tag nil ; list of POSIX classes
4542 qtag (point)) ; after leading ^ if present
4543 (if (eq (char-after b) ?\] )
4544 (and (eq (following-char) ?\\ )
4545 (eq (char-after (cperl-1+ (point)))
4546 ?\] )
4547 (setq qtag (1+ qtag))
4548 (forward-char 2))
4549 (and (eq (following-char) ?\] )
4550 (forward-char 1)))
4551 (setq REx-subgr-end qtag) ;EndOf smart-highlighed
4552 ;; Apparently, I can't put \] into a charclass
4553 ;; in m]]: m][\\\]\]] produces [\\]]
4554;;; POSIX? [:word:] [:^word:] only inside []
4555;;; "\\=\\(\\\\.\\|[^][\\\\]\\|\\[:\\^?\sw+:]\\|\\[[^:]\\)*]")
4556 (while ; look for unescaped ]
4557 (and argument
4558 (re-search-forward
4559 (if (eq (char-after b) ?\] )
4560 "\\=\\(\\\\[^]]\\|[^]\\\\]\\)*\\\\]"
4561 "\\=\\(\\\\.\\|[^]\\\\]\\)*]")
4562 (1- e) 'toend))
4563 ;; Is this ] an end of POSIX class?
4564 (if (save-excursion
4565 (and
4566 (search-backward "[" argument t)
4567 (< REx-subgr-start (point))
4568 (setq argument (point)) ; POSIX-start
4569 (or ; Should work with delim = \
4570 (not (eq (preceding-char) ?\\ ))
4571 ;; XXXX Double \\ is needed with 19.33
4572 (= (% (skip-chars-backward "\\\\") 2) 0))
4573 (looking-at
4574 (cond
4575 ((eq (char-after b) ?\] )
4576 "\\\\*\\[:\\^?\\sw+:\\\\\\]")
4577 ((eq (char-after b) ?\: )
4578 "\\\\*\\[\\\\:\\^?\\sw+\\\\:]")
4579 ((eq (char-after b) ?^ )
4580 "\\\\*\\[:\\(\\\\\\^\\)?\\sw+:\]")
4581 ((eq (char-syntax (char-after b))
4582 ?w)
4583 (concat
4584 "\\\\*\\[:\\(\\\\\\^\\)?\\(\\\\"
4585 (char-to-string (char-after b))
4586 "\\|\\sw\\)+:\]"))
4587 (t "\\\\*\\[:\\^?\\sw*:]")))
4588 (goto-char REx-subgr-end)
4589 (cperl-highlight-charclass
4590 argument my-cperl-REx-spec-char-face
4591 my-cperl-REx-0length-face my-cperl-REx-length1-face)))
4592 (setq tag (cons (cons argument (point))
4593 tag)
4594 argument (point)
4595 REx-subgr-end argument) ; continue
4596 (setq argument nil)))
4597 (and argument
4598 (message "Couldn't find end of charclass in a REx, pos=%s"
4599 REx-subgr-start))
4600 (setq argument (1- (point)))
4601 (goto-char REx-subgr-end)
4602 (cperl-highlight-charclass
4603 argument my-cperl-REx-spec-char-face
4604 my-cperl-REx-0length-face my-cperl-REx-length1-face)
4605 (forward-char 1)
4606 ;; Highlight starter, trailer, POSIX
4607 (if (and cperl-use-syntax-table-text-property
4608 (> (- (point) 2) REx-subgr-start))
4609 (put-text-property
4610 (1+ REx-subgr-start) (1- (point))
4611 'syntax-table cperl-st-punct))
4612 (cperl-postpone-fontification
4613 REx-subgr-start qtag
4614 'face my-cperl-REx-spec-char-face)
4615 (cperl-postpone-fontification
4616 (1- (point)) (point) 'face
4617 my-cperl-REx-spec-char-face)
4618 (if (eq (char-after b) ?\] )
4619 (cperl-postpone-fontification
4620 (- (point) 2) (1- (point))
4621 'face my-cperl-REx-0length-face))
4622 (while tag
4623 (cperl-postpone-fontification
4624 (car (car tag)) (cdr (car tag))
4625 'face font-lock-variable-name-face) ;my-cperl-REx-length1-face
4626 (setq tag (cdr tag)))
4627 (setq was-subgr nil)) ; did facing already
4628 ;; Now rare stuff:
4629 ((and (match-beginning 2) ; #-comment
4630 (/= (match-beginning 2) (match-end 2)))
4631 (beginning-of-line 2)
4632 (if (> (point) e)
4633 (goto-char (1- e))))
4634 ((match-beginning 4) ; character "]"
4635 (setq was-subgr nil) ; We do stuff here
4636 (goto-char (match-end 0))
4637 (if cperl-use-syntax-table-text-property
4638 (put-text-property
4639 (1- (point)) (point)
4640 'syntax-table cperl-st-punct))
4641 (cperl-postpone-fontification
4642 (1- (point)) (point)
4643 'face font-lock-warning-face))
4644 ((match-beginning 5) ; before (?{}) (??{})
4645 (setq tag (match-end 0))
4646 (if (or (setq qtag
4647 (cperl-forward-group-in-re st-l))
4648 (and (>= (point) e)
4649 (setq qtag "no matching `)' found"))
4650 (and (not (eq (char-after (- (point) 2))
4651 ?\} ))
4652 (setq qtag "Can't find })")))
4653 (progn
4654 (goto-char (1- e))
4655 (message "%s" qtag))
4656 (cperl-postpone-fontification
4657 (1- tag) (1- (point))
4658 'face font-lock-variable-name-face)
4659 (cperl-postpone-fontification
4660 REx-subgr-start (1- tag)
4661 'face my-cperl-REx-spec-char-face)
4662 (cperl-postpone-fontification
4663 (1- (point)) (point)
4664 'face my-cperl-REx-spec-char-face)
4665 (if cperl-use-syntax-table-text-property
4666 (progn
4667 (put-text-property
4668 (- (point) 2) (1- (point))
4669 'syntax-table cperl-st-cfence)
4670 (put-text-property
4671 (+ REx-subgr-start 2)
4672 (+ REx-subgr-start 3)
4673 'syntax-table cperl-st-cfence))))
4674 (setq was-subgr nil))
4675 (t ; (?#)-comment
4676 ;; Inside "(" and "\" arn't special in any way
4677 ;; Works also if the outside delimiters are ().
4678 (or;;(if (eq (char-after b) ?\) )
4679 ;;(re-search-forward
4680 ;; "[^\\\\]\\(\\\\\\\\\\)*\\\\)"
4681 ;; (1- e) 'toend)
4682 (search-forward ")" (1- e) 'toend)
4683 ;;)
4684 (message
4685 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
4686 REx-subgr-start))))
4687 (if (>= (point) e)
4688 (goto-char (1- e)))
4689 (cond
4690 (was-subgr
4691 (setq REx-subgr-end (point))
4692 (cperl-commentify
4693 REx-subgr-start REx-subgr-end nil)
4694 (cperl-postpone-fontification
4695 REx-subgr-start REx-subgr-end
4696 'face font-lock-comment-face))))))
4697 (if (and is-REx is-x-REx)
4698 (put-text-property (1+ b) (1- e)
4699 'syntax-subtype 'x-REx)))
4700 (if (and i2 e1 (or (not b1) (> e1 b1)))
4701 (progn ; No errors finding the second part...
4702 (cperl-postpone-fontification
4703 (1- e1) e1 'face my-cperl-delimiters-face)
4704 (if (and (not (eobp))
4705 (assoc (char-after b) cperl-starters))
4706 (progn
4707 (cperl-postpone-fontification
4708 b1 (1+ b1) 'face my-cperl-delimiters-face)
4709 (put-text-property b1 (1+ b1)
4710 'REx-part2 t)))))
4711 (if (> (point) max)
4712 (setq tmpend tb))))
4713 ((match-beginning 17) ; sub with prototype or attribute
4714 ;; 1+6+2+1+1=11 extra () before this (sub with proto/attr):
4715 ;;"\\<sub\\>\\(" ;12
4716 ;; cperl-white-and-comment-rex ;13
4717 ;; "\\([a-zA-Z_:'0-9]+\\)\\)?" ; name ;14
4718 ;;"\\(" cperl-maybe-white-and-comment-rex ;15,16
4719 ;; "\\(([^()]*)\\|:[^:]\\)\\)" ; 17:proto or attribute start
4720 (setq b1 (match-beginning 14) e1 (match-end 14))
4721 (if (memq (char-after (1- b))
4722 '(?\$ ?\@ ?\% ?\& ?\*))
4723 nil
4724 (goto-char b)
4725 (if (eq (char-after (match-beginning 17)) ?\( )
4726 (progn
4727 (cperl-commentify ; Prototypes; mark as string
4728 (match-beginning 17) (match-end 17) t)
4729 (goto-char (match-end 0))
4730 ;; Now look for attributes after prototype:
4731 (forward-comment (buffer-size))
4732 (and (looking-at ":[^:]")
4733 (cperl-find-sub-attrs st-l b1 e1 b)))
4734 ;; treat attributes without prototype
4735 (goto-char (match-beginning 17))
4736 (cperl-find-sub-attrs st-l b1 e1 b))))
4737 ;; 1+6+2+1+1+6+1=18 extra () before this:
4738 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
4739 ((match-beginning 19) ; old $abc'efg syntax
4740 (setq bb (match-end 0))
4741 ;;;(if (nth 3 state) nil ; in string
4742 (put-text-property (1- bb) bb 'syntax-table cperl-st-word)
4743 (goto-char bb))
4744 ;; 1+6+2+1+1+6+1+1=19 extra () before this:
4745 ;; "__\\(END\\|DATA\\)__"
4746 ((match-beginning 20) ; __END__, __DATA__
4747 (setq bb (match-end 0))
4748 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
4749 (cperl-commentify b bb nil)
4750 (setq end t))
4751 ;; "\\\\\\(['`\"($]\\)"
4752 ((match-beginning 21)
4753 ;; Trailing backslash; make non-quoting outside string/comment
4754 (setq bb (match-end 0))
4755 (goto-char b)
4756 (skip-chars-backward "\\\\")
4757 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
4758 (cperl-modify-syntax-type b cperl-st-punct)
4759 (goto-char bb))
4760 (t (error "Error in regexp of the sniffer")))
4761 (if (> (point) stop-point)
4762 (progn
4763 (if end
4764 (message "Garbage after __END__/__DATA__ ignored")
4765 (message "Unbalanced syntax found while scanning")
4766 (or (car err-l) (setcar err-l b)))
4767 (goto-char stop-point))))
4768 (setq cperl-syntax-state (cons state-point state)
4769 ;; Do not mark syntax as done past tmpend???
4770 cperl-syntax-done-to (or tmpend (max (point) max)))
4771 ;;(message "state-at=%s, done-to=%s" state-point cperl-syntax-done-to)
4772 )
4773 (if (car err-l) (goto-char (car err-l))
4774 (or non-inter
4775 (message "Scanning for \"hard\" Perl constructions... done"))))
4776 (and (buffer-modified-p)
4777 (not modified)
4778 (set-buffer-modified-p nil))
4779 ;; I do not understand what this is doing here. It breaks font-locking
4780 ;; because it resets the syntax-table from font-lock-syntax-table to
4781 ;; cperl-mode-syntax-table.
4782 ;; (set-syntax-table cperl-mode-syntax-table)
4783 )
4784 (list (car err-l) overshoot)))
4785
4786(defun cperl-find-pods-heres-region (min max)
4787 (interactive "r")
4788 (cperl-find-pods-heres min max))
4789
4790(defun cperl-backward-to-noncomment (lim)
4791 ;; Stops at lim or after non-whitespace that is not in comment
4792 ;; XXXX Wrongly understands end-of-multiline strings with # as comment
4793 (let (stop p pr)
4794 (while (and (not stop) (> (point) (or lim (point-min))))
4795 (skip-chars-backward " \t\n\f" lim)
4796 (setq p (point))
4797 (beginning-of-line)
4798 (if (memq (setq pr (get-text-property (point) 'syntax-type))
4799 '(pod here-doc here-doc-delim))
4800 (progn
4801 (cperl-unwind-to-safe nil)
4802 (setq pr (get-text-property (point) 'syntax-type))))
4803 (or (and (looking-at "^[ \t]*\\(#\\|$\\)")
4804 (not (memq pr '(string prestring))))
4805 (progn (cperl-to-comment-or-eol) (bolp))
4806 (progn
4807 (skip-chars-backward " \t")
4808 (if (< p (point)) (goto-char p))
4809 (setq stop t))))))
4810
4811;; Used only in `cperl-calculate-indent'...
4812(defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
4813 ;; Positions is before ?\{. Checks whether it starts a block.
4814 ;; No save-excursion! This is more a distinguisher of a block/hash ref...
4815 (cperl-backward-to-noncomment (point-min))
4816 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4817 ; Label may be mixed up with `$blah :'
4818 (save-excursion (cperl-after-label))
4819 (get-text-property (cperl-1- (point)) 'attrib-group)
4820 (and (memq (char-syntax (preceding-char)) '(?w ?_))
4821 (progn
4822 (backward-sexp)
4823 ;; sub {BLK}, print {BLK} $data, but NOT `bless', `return', `tr'
4824 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
4825 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
4826 ;; sub bless::foo {}
4827 (progn
4828 (cperl-backward-to-noncomment (point-min))
4829 (and (eq (preceding-char) ?b)
4830 (progn
4831 (forward-sexp -1)
4832 (looking-at "sub[ \t\n\f#]")))))))))
4833
4834;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
4835;;; No save-excursion; condition-case ... In (cperl-block-p) the block
4836;;; may be a part of an in-statement construct, such as
4837;;; ${something()}, print {FH} $data.
4838;;; Moreover, one takes positive approach (looks for else,grep etc)
4839;;; another negative (looks for bless,tr etc)
4840(defun cperl-after-block-p (lim &optional pre-block)
4841 "Return true if the preceeding } (if PRE-BLOCK, following {) delimits a block.
4842Would not look before LIM. Assumes that LIM is a good place to begin a
4843statement. The kind of block we treat here is one after which a new
4844statement would start; thus the block in ${func()} does not count."
4845 (save-excursion
4846 (condition-case nil
4847 (progn
4848 (or pre-block (forward-sexp -1))
4849 (cperl-backward-to-noncomment lim)
4850 (or (eq (point) lim)
4851 ;; if () {} // sub f () {} // sub f :a(') {}
4852 (eq (preceding-char) ?\) )
4853 ;; label: {}
4854 (save-excursion (cperl-after-label))
4855 ;; sub :attr {}
4856 (get-text-property (cperl-1- (point)) 'attrib-group)
4857 (if (memq (char-syntax (preceding-char)) '(?w ?_)) ; else {}
4858 (save-excursion
4859 (forward-sexp -1)
4860 ;; else {} but not else::func {}
4861 (or (and (looking-at "\\(else\\|continue\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
4862 (not (looking-at "\\(\\sw\\|_\\)+::")))
4863 ;; sub f {}
4864 (progn
4865 (cperl-backward-to-noncomment lim)
4866 (and (eq (preceding-char) ?b)
4867 (progn
4868 (forward-sexp -1)
4869 (looking-at "sub[ \t\n\f#]"))))))
4870 ;; What preceeds is not word... XXXX Last statement in sub???
4871 (cperl-after-expr-p lim))))
4872 (error nil))))
4873
4874(defun cperl-after-expr-p (&optional lim chars test)
4875 "Return true if the position is good for start of expression.
4876TEST is the expression to evaluate at the found position. If absent,
4877CHARS is a string that contains good characters to have before us (however,
4878`}' is treated \"smartly\" if it is not in the list)."
4879 (let ((lim (or lim (point-min)))
4880 stop p pr)
4881 (cperl-update-syntaxification (point) (point))
4882 (save-excursion
4883 (while (and (not stop) (> (point) lim))
4884 (skip-chars-backward " \t\n\f" lim)
4885 (setq p (point))
4886 (beginning-of-line)
4887 ;;(memq (setq pr (get-text-property (point) 'syntax-type))
4888 ;; '(pod here-doc here-doc-delim))
4889 (if (get-text-property (point) 'here-doc-group)
4890 (progn
4891 (goto-char
4892 (cperl-beginning-of-property (point) 'here-doc-group))
4893 (beginning-of-line 0)))
4894 (if (get-text-property (point) 'in-pod)
4895 (progn
4896 (goto-char
4897 (cperl-beginning-of-property (point) 'in-pod))
4898 (beginning-of-line 0)))
4899 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
4900 ;; Else: last iteration, or a label
4901 (cperl-to-comment-or-eol) ; Will not move past "." after a format
4902 (skip-chars-backward " \t")
4903 (if (< p (point)) (goto-char p))
4904 (setq p (point))
4905 (if (and (eq (preceding-char) ?:)
4906 (progn
4907 (forward-char -1)
4908 (skip-chars-backward " \t\n\f" lim)
4909 (memq (char-syntax (preceding-char)) '(?w ?_))))
4910 (forward-sexp -1) ; Possibly label. Skip it
4911 (goto-char p)
4912 (setq stop t))))
4913 (or (bobp) ; ???? Needed
4914 (eq (point) lim)
4915 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
4916 (progn
4917 (if test (eval test)
4918 (or (memq (preceding-char) (append (or chars "{;") nil))
4919 (and (eq (preceding-char) ?\})
4920 (cperl-after-block-p lim))
4921 (and (eq (following-char) ?.) ; in format: see comment above
4922 (eq (get-text-property (point) 'syntax-type)
4923 'format)))))))))
4924
4925(defun cperl-backward-to-start-of-expr (&optional lim)
4926 (condition-case nil
4927 (progn
4928 (while (and (or (not lim)
4929 (> (point) lim))
4930 (not (cperl-after-expr-p lim)))
4931 (forward-sexp -1)
4932 ;; May be after $, @, $# etc of a variable
4933 (skip-chars-backward "$@%#")))
4934 (error nil)))
4935
4936(defun cperl-at-end-of-expr (&optional lim)
4937 ;; Since the SEXP approach below is very fragile, do some overengineering
4938 (or (looking-at (concat cperl-maybe-white-and-comment-rex "[;}]"))
4939 (condition-case nil
4940 (save-excursion
4941 ;; If nothing interesting after, does as (forward-sexp -1);
4942 ;; otherwise fails, or ends at a start of following sexp.
4943 ;; XXXX PROBLEMS: if what follows (after ";") @FOO, or ${bar}
4944 ;; may be stuck after @ or $; just put some stupid workaround now:
4945 (let ((p (point)))
4946 (forward-sexp 1)
4947 (forward-sexp -1)
4948 (while (memq (preceding-char) (append "%&@$*" nil))
4949 (forward-char -1))
4950 (or (< (point) p)
4951 (cperl-after-expr-p lim))))
4952 (error t))))
4953
4954(defun cperl-forward-to-end-of-expr (&optional lim)
4955 (let ((p (point))))
4956 (condition-case nil
4957 (progn
4958 (while (and (< (point) (or lim (point-max)))
4959 (not (cperl-at-end-of-expr)))
4960 (forward-sexp 1)))
4961 (error nil)))
4962
4963(defun cperl-backward-to-start-of-continued-exp (lim)
4964 (if (memq (preceding-char) (append ")]}\"'`" nil))
4965 (forward-sexp -1))
4966 (beginning-of-line)
4967 (if (<= (point) lim)
4968 (goto-char (1+ lim)))
4969 (skip-chars-forward " \t"))
4970
4971(defun cperl-after-block-and-statement-beg (lim)
4972 ;; We assume that we are after ?\}
4973 (and
4974 (cperl-after-block-p lim)
4975 (save-excursion
4976 (forward-sexp -1)
4977 (cperl-backward-to-noncomment (point-min))
4978 (or (bobp)
4979 (eq (point) lim)
4980 (not (= (char-syntax (preceding-char)) ?w))
4981 (progn
4982 (forward-sexp -1)
4983 (not
4984 (looking-at
4985 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
4986
4987\f
4988(defun cperl-indent-exp ()
4989 "Simple variant of indentation of continued-sexp.
4990
4991Will not indent comment if it starts at `comment-indent' or looks like
4992continuation of the comment on the previous line.
4993
4994If `cperl-indent-region-fix-constructs', will improve spacing on
4995conditional/loop constructs."
4996 (interactive)
4997 (save-excursion
4998 (let ((tmp-end (progn (end-of-line) (point))) top done)
4999 (save-excursion
5000 (beginning-of-line)
5001 (while (null done)
5002 (setq top (point))
5003 ;; Plan A: if line has an unfinished paren-group, go to end-of-group
5004 (while (= -1 (nth 0 (parse-partial-sexp (point) tmp-end -1)))
5005 (setq top (point))) ; Get the outermost parenths in line
5006 (goto-char top)
5007 (while (< (point) tmp-end)
5008 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
5009 (or (eolp) (forward-sexp 1)))
5010 (if (> (point) tmp-end) ; Yes, there an unfinished block
5011 nil
5012 (if (eq ?\) (preceding-char))
5013 (progn ;; Plan B: find by REGEXP block followup this line
5014 (setq top (point))
5015 (condition-case nil
5016 (progn
5017 (forward-sexp -2)
5018 (if (eq (following-char) ?$ ) ; for my $var (list)
5019 (progn
5020 (forward-sexp -1)
5021 (if (looking-at "\\(my\\|local\\|our\\)\\>")
5022 (forward-sexp -1))))
5023 (if (looking-at
5024 (concat "\\(\\elsif\\|if\\|unless\\|while\\|until"
5025 "\\|for\\(each\\)?\\>\\(\\("
5026 cperl-maybe-white-and-comment-rex
5027 "\\(my\\|local\\|our\\)\\)?"
5028 cperl-maybe-white-and-comment-rex
5029 "\\$[_a-zA-Z0-9]+\\)?\\)\\>"))
5030 (progn
5031 (goto-char top)
5032 (forward-sexp 1)
5033 (setq top (point)))))
5034 (error (setq done t)))
5035 (goto-char top))
5036 (if (looking-at ; Try Plan C: continuation block
5037 (concat cperl-maybe-white-and-comment-rex
5038 "\\<\\(else\\|elsif\|continue\\)\\>"))
5039 (progn
5040 (goto-char (match-end 0))
5041 (save-excursion
5042 (end-of-line)
5043 (setq tmp-end (point))))
5044 (setq done t))))
5045 (save-excursion
5046 (end-of-line)
5047 (setq tmp-end (point))))
5048 (goto-char tmp-end)
5049 (setq tmp-end (point-marker)))
5050 (if cperl-indent-region-fix-constructs
5051 (cperl-fix-line-spacing tmp-end))
5052 (cperl-indent-region (point) tmp-end))))
5053
5054(defun cperl-fix-line-spacing (&optional end parse-data)
5055 "Improve whitespace in a conditional/loop construct.
5056Returns some position at the last line."
5057 (interactive)
5058 (or end
5059 (setq end (point-max)))
5060 (let ((ee (save-excursion (end-of-line) (point)))
5061 (cperl-indent-region-fix-constructs
5062 (or cperl-indent-region-fix-constructs 1))
5063 p pp ml have-brace ret)
5064 (save-excursion
5065 (beginning-of-line)
5066 (setq ret (point))
5067 ;; }? continue
5068 ;; blah; }
5069 (if (not
5070 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
5071 (setq have-brace (save-excursion (search-forward "}" ee t)))))
5072 nil ; Do not need to do anything
5073 ;; Looking at:
5074 ;; }
5075 ;; else
5076 (if cperl-merge-trailing-else
5077 (if (looking-at
5078 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5079 (progn
5080 (search-forward "}")
5081 (setq p (point))
5082 (skip-chars-forward " \t\n")
5083 (delete-region p (point))
5084 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5085 (beginning-of-line)))
5086 (if (looking-at "[ \t]*}[ \t]*\\(els\\(e\\|if\\)\\|continue\\)\\>")
5087 (save-excursion
5088 (search-forward "}")
5089 (delete-horizontal-space)
5090 (insert "\n")
5091 (setq ret (point))
5092 (if (cperl-indent-line parse-data)
5093 (progn
5094 (cperl-fix-line-spacing end parse-data)
5095 (setq ret (point)))))))
5096 ;; Looking at:
5097 ;; } else
5098 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
5099 (progn
5100 (search-forward "}")
5101 (delete-horizontal-space)
5102 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5103 (beginning-of-line)))
5104 ;; Looking at:
5105 ;; else {
5106 (if (looking-at
5107 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5108 (progn
5109 (forward-word 1)
5110 (delete-horizontal-space)
5111 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5112 (beginning-of-line)))
5113 ;; Looking at:
5114 ;; foreach my $var
5115 (if (looking-at
5116 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
5117 (progn
5118 (forward-word 2)
5119 (delete-horizontal-space)
5120 (insert (make-string cperl-indent-region-fix-constructs ?\s))
5121 (beginning-of-line)))
5122 ;; Looking at:
5123 ;; foreach my $var (
5124 (if (looking-at
5125 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
5126 (progn
5127 (forward-sexp 3)
5128 (delete-horizontal-space)
5129 (insert
5130 (make-string cperl-indent-region-fix-constructs ?\s))
5131 (beginning-of-line)))
5132 ;; Looking at (with or without "}" at start, ending after "({"):
5133 ;; } foreach my $var () OR {
5134 (if (looking-at
5135 "[ \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]*{")
5136 (progn
5137 (setq ml (match-beginning 8)) ; "(" or "{" after control word
5138 (re-search-forward "[({]")
5139 (forward-char -1)
5140 (setq p (point))
5141 (if (eq (following-char) ?\( )
5142 (progn
5143 (forward-sexp 1)
5144 (setq pp (point))) ; past parenth-group
5145 ;; after `else' or nothing
5146 (if ml ; after `else'
5147 (skip-chars-backward " \t\n")
5148 (beginning-of-line))
5149 (setq pp nil))
5150 ;; Now after the sexp before the brace
5151 ;; Multiline expr should be special
5152 (setq ml (and pp (save-excursion (goto-char p)
5153 (search-forward "\n" pp t))))
5154 (if (and (or (not pp) (< pp end)) ; Do not go too far...
5155 (looking-at "[ \t\n]*{"))
5156 (progn
5157 (cond
5158 ((bolp) ; Were before `{', no if/else/etc
5159 nil)
5160 ((looking-at "\\(\t*\\| [ \t]+\\){") ; Not exactly 1 SPACE
5161 (delete-horizontal-space)
5162 (if (if ml
5163 cperl-extra-newline-before-brace-multiline
5164 cperl-extra-newline-before-brace)
5165 (progn
5166 (delete-horizontal-space)
5167 (insert "\n")
5168 (setq ret (point))
5169 (if (cperl-indent-line parse-data)
5170 (progn
5171 (cperl-fix-line-spacing end parse-data)
5172 (setq ret (point)))))
5173 (insert
5174 (make-string cperl-indent-region-fix-constructs ?\s))))
5175 ((and (looking-at "[ \t]*\n")
5176 (not (if ml
5177 cperl-extra-newline-before-brace-multiline
5178 cperl-extra-newline-before-brace)))
5179 (setq pp (point))
5180 (skip-chars-forward " \t\n")
5181 (delete-region pp (point))
5182 (insert
5183 (make-string cperl-indent-region-fix-constructs ?\ )))
5184 ((and (looking-at "[\t ]*{")
5185 (if ml cperl-extra-newline-before-brace-multiline
5186 cperl-extra-newline-before-brace))
5187 (delete-horizontal-space)
5188 (insert "\n")
5189 (setq ret (point))
5190 (if (cperl-indent-line parse-data)
5191 (progn
5192 (cperl-fix-line-spacing end parse-data)
5193 (setq ret (point))))))
5194 ;; Now we are before `{'
5195 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
5196 (progn
5197 (skip-chars-forward " \t\n")
5198 (setq pp (point))
5199 (forward-sexp 1)
5200 (setq p (point))
5201 (goto-char pp)
5202 (setq ml (search-forward "\n" p t))
5203 (if (or cperl-break-one-line-blocks-when-indent ml)
5204 ;; not good: multi-line BLOCK
5205 (progn
5206 (goto-char (1+ pp))
5207 (delete-horizontal-space)
5208 (insert "\n")
5209 (setq ret (point))
5210 (if (cperl-indent-line parse-data)
5211 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
5212 (beginning-of-line)
5213 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
5214 ;; Now check whether there is a hanging `}'
5215 ;; Looking at:
5216 ;; } blah
5217 (if (and
5218 cperl-fix-hanging-brace-when-indent
5219 have-brace
5220 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
5221 (condition-case nil
5222 (progn
5223 (up-list 1)
5224 (if (and (<= (point) pp)
5225 (eq (preceding-char) ?\} )
5226 (cperl-after-block-and-statement-beg (point-min)))
5227 t
5228 (goto-char p)
5229 nil))
5230 (error nil)))
5231 (progn
5232 (forward-char -1)
5233 (skip-chars-backward " \t")
5234 (if (bolp)
5235 ;; `}' was the first thing on the line, insert NL *after* it.
5236 (progn
5237 (cperl-indent-line parse-data)
5238 (search-forward "}")
5239 (delete-horizontal-space)
5240 (insert "\n"))
5241 (delete-horizontal-space)
5242 (or (eq (preceding-char) ?\;)
5243 (bolp)
5244 (and (eq (preceding-char) ?\} )
5245 (cperl-after-block-p (point-min)))
5246 (insert ";"))
5247 (insert "\n")
5248 (setq ret (point)))
5249 (if (cperl-indent-line parse-data)
5250 (setq ret (cperl-fix-line-spacing end parse-data)))
5251 (beginning-of-line)))))
5252 ret))
5253
5254(defvar cperl-update-start) ; Do not need to make them local
5255(defvar cperl-update-end)
5256(defun cperl-delay-update-hook (beg end old-len)
5257 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
5258 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
5259
5260(defun cperl-indent-region (start end)
5261 "Simple variant of indentation of region in CPerl mode.
5262Should be slow. Will not indent comment if it starts at `comment-indent'
5263or looks like continuation of the comment on the previous line.
5264Indents all the lines whose first character is between START and END
5265inclusive.
5266
5267If `cperl-indent-region-fix-constructs', will improve spacing on
5268conditional/loop constructs."
5269 (interactive "r")
5270 (cperl-update-syntaxification end end)
5271 (save-excursion
5272 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
5273 (let ((indent-info (if cperl-emacs-can-parse
5274 (list nil nil nil) ; Cannot use '(), since will modify
5275 nil))
5276 (pm 0)
5277 after-change-functions ; Speed it up!
5278 st comm old-comm-indent new-comm-indent p pp i empty)
5279 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
5280 (goto-char start)
5281 (setq old-comm-indent (and (cperl-to-comment-or-eol)
5282 (current-column))
5283 new-comm-indent old-comm-indent)
5284 (goto-char start)
5285 (setq end (set-marker (make-marker) end)) ; indentation changes pos
5286 (or (bolp) (beginning-of-line 2))
5287 (while (and (<= (point) end) (not (eobp))) ; bol to check start
5288 (setq st (point))
5289 (if (or
5290 (setq empty (looking-at "[ \t]*\n"))
5291 (and (setq comm (looking-at "[ \t]*#"))
5292 (or (eq (current-indentation) (or old-comm-indent
5293 comment-column))
5294 (setq old-comm-indent nil))))
5295 (if (and old-comm-indent
5296 (not empty)
5297 (= (current-indentation) old-comm-indent)
5298 (not (eq (get-text-property (point) 'syntax-type) 'pod))
5299 (not (eq (get-text-property (point) 'syntax-table)
5300 cperl-st-cfence)))
5301 (let ((comment-column new-comm-indent))
5302 (indent-for-comment)))
5303 (progn
5304 (setq i (cperl-indent-line indent-info))
5305 (or comm
5306 (not i)
5307 (progn
5308 (if cperl-indent-region-fix-constructs
5309 (goto-char (cperl-fix-line-spacing end indent-info)))
5310 (if (setq old-comm-indent
5311 (and (cperl-to-comment-or-eol)
5312 (not (memq (get-text-property (point)
5313 'syntax-type)
5314 '(pod here-doc)))
5315 (not (eq (get-text-property (point)
5316 'syntax-table)
5317 cperl-st-cfence))
5318 (current-column)))
5319 (progn (indent-for-comment)
5320 (skip-chars-backward " \t")
5321 (skip-chars-backward "#")
5322 (setq new-comm-indent (current-column))))))))
5323 (beginning-of-line 2)))
5324 ;; Now run the update hooks
5325 (and after-change-functions
5326 cperl-update-end
5327 (save-excursion
5328 (goto-char cperl-update-end)
5329 (insert " ")
5330 (delete-char -1)
5331 (goto-char cperl-update-start)
5332 (insert " ")
5333 (delete-char -1))))))
5334
5335;; Stolen from lisp-mode with a lot of improvements
5336
5337(defun cperl-fill-paragraph (&optional justify iteration)
5338 "Like `fill-paragraph', but handle CPerl comments.
5339If any of the current line is a comment, fill the comment or the
5340block of it that point is in, preserving the comment's initial
5341indentation and initial hashes. Behaves usually outside of comment."
5342 ;; (interactive "P") ; Only works when called from fill-paragraph. -stef
5343 (let (;; Non-nil if the current line contains a comment.
5344 has-comment
5345 fill-paragraph-function ; do not recurse
5346 ;; If has-comment, the appropriate fill-prefix for the comment.
5347 comment-fill-prefix
5348 ;; Line that contains code and comment (or nil)
5349 start
5350 c spaces len dc (comment-column comment-column))
5351 ;; Figure out what kind of comment we are looking at.
5352 (save-excursion
5353 (beginning-of-line)
5354 (cond
5355
5356 ;; A line with nothing but a comment on it?
5357 ((looking-at "[ \t]*#[# \t]*")
5358 (setq has-comment t
5359 comment-fill-prefix (buffer-substring (match-beginning 0)
5360 (match-end 0))))
5361
5362 ;; A line with some code, followed by a comment? Remember that the
5363 ;; semi which starts the comment shouldn't be part of a string or
5364 ;; character.
5365 ((cperl-to-comment-or-eol)
5366 (setq has-comment t)
5367 (looking-at "#+[ \t]*")
5368 (setq start (point) c (current-column)
5369 comment-fill-prefix
5370 (concat (make-string (current-column) ?\s)
5371 (buffer-substring (match-beginning 0) (match-end 0)))
5372 spaces (progn (skip-chars-backward " \t")
5373 (buffer-substring (point) start))
5374 dc (- c (current-column)) len (- start (point))
5375 start (point-marker))
5376 (delete-char len)
5377 (insert (make-string dc ?-))))) ; Placeholder (to avoid splitting???)
5378 (if (not has-comment)
5379 (fill-paragraph justify) ; Do the usual thing outside of comment
5380 ;; Narrow to include only the comment, and then fill the region.
5381 (save-restriction
5382 (narrow-to-region
5383 ;; Find the first line we should include in the region to fill.
5384 (if start (progn (beginning-of-line) (point))
5385 (save-excursion
5386 (while (and (zerop (forward-line -1))
5387 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5388 ;; We may have gone to far. Go forward again.
5389 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
5390 (forward-line 1))
5391 (point)))
5392 ;; Find the beginning of the first line past the region to fill.
5393 (save-excursion
5394 (while (progn (forward-line 1)
5395 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
5396 (point)))
5397 ;; Remove existing hashes
5398 (goto-char (point-min))
5399 (save-excursion
5400 (while (progn (forward-line 1) (< (point) (point-max)))
5401 (skip-chars-forward " \t")
5402 (if (looking-at "#+")
5403 (progn
5404 (if (and (eq (point) (match-beginning 0))
5405 (not (eq (point) (match-end 0)))) nil
5406 (error
5407 "Bug in Emacs: `looking-at' in `narrow-to-region': match-data is garbage"))
5408 (delete-char (- (match-end 0) (match-beginning 0)))))))
5409
5410 ;; Lines with only hashes on them can be paragraph boundaries.
5411 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
5412 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
5413 (fill-prefix comment-fill-prefix))
5414 (fill-paragraph justify)))
5415 (if (and start)
5416 (progn
5417 (goto-char start)
5418 (if (> dc 0)
5419 (progn (delete-char dc) (insert spaces)))
5420 (if (or (= (current-column) c) iteration) nil
5421 (setq comment-column c)
5422 (indent-for-comment)
5423 ;; Repeat once more, flagging as iteration
5424 (cperl-fill-paragraph justify t))))))
5425 t)
5426
5427(defun cperl-do-auto-fill ()
5428 ;; Break out if the line is short enough
5429 (if (> (save-excursion
5430 (end-of-line)
5431 (current-column))
5432 fill-column)
5433 (let ((c (save-excursion (beginning-of-line)
5434 (cperl-to-comment-or-eol) (point)))
5435 (s (memq (following-char) '(?\s ?\t))) marker)
5436 (if (>= c (point))
5437 ;; Don't break line inside code: only inside comment.
5438 nil
5439 (setq marker (point-marker))
5440 (fill-paragraph nil)
5441 (goto-char marker)
5442 ;; Is not enough, sometimes marker is a start of line
5443 (if (bolp) (progn (re-search-forward "#+[ \t]*")
5444 (goto-char (match-end 0))))
5445 ;; Following space could have gone:
5446 (if (or (not s) (memq (following-char) '(?\s ?\t))) nil
5447 (insert " ")
5448 (backward-char 1))
5449 ;; Previous space could have gone:
5450 (or (memq (preceding-char) '(?\s ?\t)) (insert " "))))))
5451
5452(defun cperl-imenu-addback (lst &optional isback name)
5453 ;; We suppose that the lst is a DAG, unless the first element only
5454 ;; loops back, and ISBACK is set. Thus this function cannot be
5455 ;; applied twice without ISBACK set.
5456 (cond ((not cperl-imenu-addback) lst)
5457 (t
5458 (or name
5459 (setq name "+++BACK+++"))
5460 (mapc (lambda (elt)
5461 (if (and (listp elt) (listp (cdr elt)))
5462 (progn
5463 ;; In the other order it goes up
5464 ;; one level only ;-(
5465 (setcdr elt (cons (cons name lst)
5466 (cdr elt)))
5467 (cperl-imenu-addback (cdr elt) t name))))
5468 (if isback (cdr lst) lst))
5469 lst)))
5470
5471(defun cperl-imenu--create-perl-index (&optional regexp)
5472 (require 'imenu) ; May be called from TAGS creator
5473 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
5474 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
5475 (index-meth-alist '()) meth
5476 packages ends-ranges p marker is-proto
5477 (prev-pos 0) is-pack index index1 name (end-range 0) package)
5478 (goto-char (point-min))
5479 (cperl-update-syntaxification (point-max) (point-max))
5480 ;; Search for the function
5481 (progn ;;save-match-data
5482 (while (re-search-forward
5483 (or regexp cperl-imenu--function-name-regexp-perl)
5484 nil t)
5485 ;; 2=package-group, 5=package-name 8=sub-name
5486 (cond
5487 ((and ; Skip some noise if building tags
5488 (match-beginning 5) ; package name
5489 ;;(eq (char-after (match-beginning 2)) ?p) ; package
5490 (not (save-match-data
5491 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
5492 nil)
5493 ((and
5494 (or (match-beginning 2)
5495 (match-beginning 8)) ; package or sub
5496 ;; Skip if quoted (will not skip multi-line ''-strings :-():
5497 (null (get-text-property (match-beginning 1) 'syntax-table))
5498 (null (get-text-property (match-beginning 1) 'syntax-type))
5499 (null (get-text-property (match-beginning 1) 'in-pod)))
5500 (setq is-pack (match-beginning 2))
5501 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
5502 ;; (goto-char (match-end 0))) ; Messes what follows
5503 (setq meth nil
5504 p (point))
5505 (while (and ends-ranges (>= p (car ends-ranges)))
5506 ;; delete obsolete entries
5507 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
5508 (setq package (or (car packages) "")
5509 end-range (or (car ends-ranges) 0))
5510 (if is-pack ; doing "package"
5511 (progn
5512 (if (match-beginning 5) ; named package
5513 (setq name (buffer-substring (match-beginning 5)
5514 (match-end 5))
5515 name (progn
5516 (set-text-properties 0 (length name) nil name)
5517 name)
5518 package (concat name "::")
5519 name (concat "package " name))
5520 ;; Support nameless packages
5521 (setq name "package;" package ""))
5522 (setq end-range
5523 (save-excursion
5524 (parse-partial-sexp (point) (point-max) -1) (point))
5525 ends-ranges (cons end-range ends-ranges)
5526 packages (cons package packages)))
5527 (setq is-proto
5528 (or (eq (following-char) ?\;)
5529 (eq 0 (get-text-property (point) 'attrib-group)))))
5530 ;; Skip this function name if it is a prototype declaration.
5531 (if (and is-proto (not is-pack)) nil
5532 (or is-pack
5533 (setq name
5534 (buffer-substring (match-beginning 8) (match-end 8)))
5535 (set-text-properties 0 (length name) nil name))
5536 (setq marker (make-marker))
5537 (set-marker marker (match-end (if is-pack 2 8)))
5538 (cond (is-pack nil)
5539 ((string-match "[:']" name)
5540 (setq meth t))
5541 ((> p end-range) nil)
5542 (t
5543 (setq name (concat package name) meth t)))
5544 (setq index (cons name marker))
5545 (if is-pack
5546 (push index index-pack-alist)
5547 (push index index-alist))
5548 (if meth (push index index-meth-alist))
5549 (push index index-unsorted-alist)))
5550 ((match-beginning 16) ; POD section
5551 (setq name (buffer-substring (match-beginning 17) (match-end 17))
5552 marker (make-marker))
5553 (set-marker marker (match-beginning 17))
5554 (set-text-properties 0 (length name) nil name)
5555 (setq name (concat (make-string
5556 (* 3 (- (char-after (match-beginning 16)) ?1))
5557 ?\ )
5558 name)
5559 index (cons name marker))
5560 (setq index1 (cons (concat "=" name) (cdr index)))
5561 (push index index-pod-alist)
5562 (push index1 index-unsorted-alist)))))
5563 (setq index-alist
5564 (if (default-value 'imenu-sort-function)
5565 (sort index-alist (default-value 'imenu-sort-function))
5566 (nreverse index-alist)))
5567 (and index-pod-alist
5568 (push (cons "+POD headers+..."
5569 (nreverse index-pod-alist))
5570 index-alist))
5571 (and (or index-pack-alist index-meth-alist)
5572 (let ((lst index-pack-alist) hier-list pack elt group name)
5573 ;; Remove "package ", reverse and uniquify.
5574 (while lst
5575 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
5576 (if (assoc name hier-list) nil
5577 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
5578 (setq lst index-meth-alist)
5579 (while lst
5580 (setq elt (car lst) lst (cdr lst))
5581 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
5582 (setq pack (substring (car elt) 0 (match-beginning 0)))
5583 (if (setq group (assoc pack hier-list))
5584 (if (listp (cdr group))
5585 ;; Have some functions already
5586 (setcdr group
5587 (cons (cons (substring
5588 (car elt)
5589 (+ 2 (match-beginning 0)))
5590 (cdr elt))
5591 (cdr group)))
5592 (setcdr group (list (cons (substring
5593 (car elt)
5594 (+ 2 (match-beginning 0)))
5595 (cdr elt)))))
5596 (setq hier-list
5597 (cons (cons pack
5598 (list (cons (substring
5599 (car elt)
5600 (+ 2 (match-beginning 0)))
5601 (cdr elt))))
5602 hier-list))))))
5603 (push (cons "+Hierarchy+..."
5604 hier-list)
5605 index-alist)))
5606 (and index-pack-alist
5607 (push (cons "+Packages+..."
5608 (nreverse index-pack-alist))
5609 index-alist))
5610 (and (or index-pack-alist index-pod-alist
5611 (default-value 'imenu-sort-function))
5612 index-unsorted-alist
5613 (push (cons "+Unsorted List+..."
5614 (nreverse index-unsorted-alist))
5615 index-alist))
5616 (cperl-imenu-addback index-alist)))
5617
5618\f
5619;; Suggested by Mark A. Hershberger
5620(defun cperl-outline-level ()
5621 (looking-at outline-regexp)
5622 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
5623;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level
5624 ((match-beginning 2) 0) ; package
5625 ((match-beginning 8) 1) ; sub
5626 ((match-beginning 16)
5627 (- (char-after (match-beginning 16)) ?0)) ; headN ==> N
5628 (t 5))) ; should not happen
5629
5630\f
5631(defun cperl-windowed-init ()
5632 "Initialization under windowed version."
5633 (cond ((featurep 'ps-print)
5634 (or cperl-faces-init
5635 (progn
5636 (and (boundp 'font-lock-multiline)
5637 (setq cperl-font-lock-multiline t))
5638 (cperl-init-faces))))
5639 ((not cperl-faces-init)
5640 (add-hook 'font-lock-mode-hook
5641 (function
5642 (lambda ()
5643 (if (memq major-mode '(perl-mode cperl-mode))
5644 (progn
5645 (or cperl-faces-init (cperl-init-faces)))))))
5646 (if (fboundp 'eval-after-load)
5647 (eval-after-load
5648 "ps-print"
5649 '(or cperl-faces-init (cperl-init-faces)))))))
5650
5651(defvar cperl-font-lock-keywords-1 nil
5652 "Additional expressions to highlight in Perl mode. Minimal set.")
5653(defvar cperl-font-lock-keywords nil
5654 "Additional expressions to highlight in Perl mode. Default set.")
5655(defvar cperl-font-lock-keywords-2 nil
5656 "Additional expressions to highlight in Perl mode. Maximal set")
5657
5658(defun cperl-load-font-lock-keywords ()
5659 (or cperl-faces-init (cperl-init-faces))
5660 cperl-font-lock-keywords)
5661
5662(defun cperl-load-font-lock-keywords-1 ()
5663 (or cperl-faces-init (cperl-init-faces))
5664 cperl-font-lock-keywords-1)
5665
5666(defun cperl-load-font-lock-keywords-2 ()
5667 (or cperl-faces-init (cperl-init-faces))
5668 cperl-font-lock-keywords-2)
5669
5670(defun cperl-init-faces-weak ()
5671 ;; Allow `cperl-find-pods-heres' to run.
5672 (or (boundp 'font-lock-constant-face)
5673 (cperl-force-face font-lock-constant-face
5674 "Face for constant and label names"))
5675 (or (boundp 'font-lock-warning-face)
5676 (cperl-force-face font-lock-warning-face
5677 "Face for things which should stand out"))
5678 ;;(setq font-lock-constant-face 'font-lock-constant-face)
5679 )
5680
5681(defun cperl-init-faces ()
5682 (condition-case errs
5683 (progn
5684 (require 'font-lock)
5685 (and (fboundp 'font-lock-fontify-anchored-keywords)
5686 (featurep 'font-lock-extra)
5687 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
5688 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
5689 (if (fboundp 'font-lock-fontify-anchored-keywords)
5690 (setq font-lock-anchored t))
5691 (setq
5692 t-font-lock-keywords
5693 (list
5694 (list "[ \t]+$" 0 cperl-invalid-face t)
5695 (cons
5696 (concat
5697 "\\(^\\|[^$@%&\\]\\)\\<\\("
5698 (mapconcat
5699 'identity
5700 '("if" "until" "while" "elsif" "else" "unless" "for"
5701 "foreach" "continue" "exit" "die" "last" "goto" "next"
5702 "redo" "return" "local" "exec" "sub" "do" "dump" "use" "our"
5703 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
5704 "\\|") ; Flow control
5705 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
5706 ; In what follows we use `type' style
5707 ; for overwritable builtins
5708 (list
5709 (concat
5710 "\\(^\\|[^$@%&\\]\\)\\<\\("
5711 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
5712 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
5713 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
5714 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
5715 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
5716 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
5717 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
5718 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
5719 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
5720 ;; "gethostbyname" "gethostent" "getlogin"
5721 ;; "getnetbyaddr" "getnetbyname" "getnetent"
5722 ;; "getpeername" "getpgrp" "getppid" "getpriority"
5723 ;; "getprotobyname" "getprotobynumber" "getprotoent"
5724 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
5725 ;; "getservbyport" "getservent" "getsockname"
5726 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
5727 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
5728 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
5729 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
5730 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
5731 ;; "quotemeta" "rand" "read" "readdir" "readline"
5732 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
5733 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
5734 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
5735 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
5736 ;; "setpriority" "setprotoent" "setpwent" "setservent"
5737 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
5738 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
5739 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
5740 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
5741 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
5742 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
5743 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5744 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
5745 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
5746 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
5747 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
5748 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
5749 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
5750 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
5751 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
5752 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
5753 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
5754 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
5755 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
5756 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
5757 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
5758 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
5759 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
5760 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
5761 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
5762 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
5763 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
5764 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
5765 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
5766 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
5767 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
5768 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
5769 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
5770 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
5771 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
5772 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
5773 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
5774 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
5775 "\\)\\>") 2 'font-lock-type-face)
5776 ;; In what follows we use `other' style
5777 ;; for nonoverwritable builtins
5778 ;; Somehow 's', 'm' are not auto-generated???
5779 (list
5780 (concat
5781 "\\(^\\|[^$@%&\\]\\)\\<\\("
5782 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
5783 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
5784 ;; "eval" "exists" "for" "foreach" "format" "goto"
5785 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
5786 ;; "no" "our" "package" "pop" "pos" "print" "printf" "push"
5787 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
5788 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
5789 ;; "undef" "unless" "unshift" "untie" "until" "use"
5790 ;; "while" "y"
5791 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
5792 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
5793 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
5794 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
5795 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5796 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
5797 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
5798 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
5799 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
5800 "\\|[sm]" ; Added manually
5801 "\\)\\>") 2 'cperl-nonoverridable-face)
5802 ;; (mapconcat 'identity
5803 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
5804 ;; "#include" "#define" "#undef")
5805 ;; "\\|")
5806 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
5807 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
5808 ;; This highlights declarations and definitions differenty.
5809 ;; We do not try to highlight in the case of attributes:
5810 ;; it is already done by `cperl-find-pods-heres'
5811 (list (concat "\\<sub"
5812 cperl-white-and-comment-rex ; whitespace/comments
5813 "\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
5814 "\\("
5815 cperl-maybe-white-and-comment-rex ;whitespace/comments?
5816 "([^()]*)\\)?" ; prototype
5817 cperl-maybe-white-and-comment-rex ; whitespace/comments?
5818 "[{;]")
5819 2 (if cperl-font-lock-multiline
5820 '(if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5821 'font-lock-function-name-face
5822 'font-lock-variable-name-face)
5823 ;; need to manually set 'multiline' for older font-locks
5824 '(progn
5825 (if (< 1 (count-lines (match-beginning 0)
5826 (match-end 0)))
5827 (put-text-property
5828 (+ 3 (match-beginning 0)) (match-end 0)
5829 'syntax-type 'multiline))
5830 (if (eq (char-after (cperl-1- (match-end 0))) ?\{ )
5831 'font-lock-function-name-face
5832 'font-lock-variable-name-face))))
5833 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
5834 2 font-lock-function-name-face)
5835 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
5836 1 font-lock-function-name-face)
5837 (cond ((featurep 'font-lock-extra)
5838 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5839 (2 font-lock-string-face t)
5840 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
5841 (font-lock-anchored
5842 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5843 (2 font-lock-string-face t)
5844 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5845 nil nil
5846 (1 font-lock-string-face t))))
5847 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
5848 2 font-lock-string-face t)))
5849 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
5850 font-lock-string-face t)
5851 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
5852 font-lock-constant-face) ; labels
5853 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
5854 2 font-lock-constant-face)
5855 ;; Uncomment to get perl-mode-like vars
5856 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
5857 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
5858 ;;; (2 (cons font-lock-variable-name-face '(underline))))
5859 (cond ((featurep 'font-lock-extra)
5860 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5861 (3 font-lock-variable-name-face)
5862 (4 '(another 4 nil
5863 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
5864 (1 font-lock-variable-name-face)
5865 (2 '(restart 2 nil) nil t)))
5866 nil t))) ; local variables, multiple
5867 (font-lock-anchored
5868 ;; 1=my_etc, 2=white? 3=(+white? 4=white? 5=var
5869 `(,(concat "\\<\\(my\\|local\\|our\\)"
5870 cperl-maybe-white-and-comment-rex
5871 "\\(("
5872 cperl-maybe-white-and-comment-rex
5873 "\\)?\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5874 (5 ,(if cperl-font-lock-multiline
5875 'font-lock-variable-name-face
5876 '(progn (setq cperl-font-lock-multiline-start
5877 (match-beginning 0))
5878 'font-lock-variable-name-face)))
5879 (,(concat "\\="
5880 cperl-maybe-white-and-comment-rex
5881 ","
5882 cperl-maybe-white-and-comment-rex
5883 "\\([$@%*]\\([a-zA-Z0-9_:]+\\|[^a-zA-Z0-9_]\\)\\)")
5884 ;; Bug in font-lock: limit is used not only to limit
5885 ;; searches, but to set the "extend window for
5886 ;; facification" property. Thus we need to minimize.
5887 ,(if cperl-font-lock-multiline
5888 '(if (match-beginning 3)
5889 (save-excursion
5890 (goto-char (match-beginning 3))
5891 (condition-case nil
5892 (forward-sexp 1)
5893 (error
5894 (condition-case nil
5895 (forward-char 200)
5896 (error nil)))) ; typeahead
5897 (1- (point))) ; report limit
5898 (forward-char -2)) ; disable continued expr
5899 '(if (match-beginning 3)
5900 (point-max) ; No limit for continuation
5901 (forward-char -2))) ; disable continued expr
5902 ,(if cperl-font-lock-multiline
5903 nil
5904 '(progn ; Do at end
5905 ;; "my" may be already fontified (POD),
5906 ;; so cperl-font-lock-multiline-start is nil
5907 (if (or (not cperl-font-lock-multiline-start)
5908 (> 2 (count-lines
5909 cperl-font-lock-multiline-start
5910 (point))))
5911 nil
5912 (put-text-property
5913 (1+ cperl-font-lock-multiline-start) (point)
5914 'syntax-type 'multiline))
5915 (setq cperl-font-lock-multiline-start nil)))
5916 (3 font-lock-variable-name-face))))
5917 (t '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
5918 3 font-lock-variable-name-face)))
5919 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
5920 4 font-lock-variable-name-face)
5921 ;; Avoid $!, and s!!, qq!! etc. when not fontifying syntaxically
5922 '("\\(?:^\\|[^smywqrx$]\\)\\(!\\)" 1 font-lock-negation-char-face)
5923 '("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
5924 (setq
5925 t-font-lock-keywords-1
5926 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
5927 ;; not yet as of XEmacs 19.12, works with 21.1.11
5928 (or
5929 (not (featurep 'xemacs))
5930 (string< "21.1.9" emacs-version)
5931 (and (string< "21.1.10" emacs-version)
5932 (string< emacs-version "21.1.2")))
5933 '(
5934 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
5935 (if (eq (char-after (match-beginning 2)) ?%)
5936 'cperl-hash-face
5937 'cperl-array-face)
5938 t) ; arrays and hashes
5939 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
5940 1
5941 (if (= (- (match-end 2) (match-beginning 2)) 1)
5942 (if (eq (char-after (match-beginning 3)) ?{)
5943 'cperl-hash-face
5944 'cperl-array-face) ; arrays and hashes
5945 font-lock-variable-name-face) ; Just to put something
5946 t)
5947 ("\\(@\\|\\$#\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5948 (1 cperl-array-face)
5949 (2 font-lock-variable-name-face))
5950 ("\\(%\\)\\(\\$+\\([a-zA-Z_:][a-zA-Z0-9_:]*\\|[^ \t\n]\\)\\)"
5951 (1 cperl-hash-face)
5952 (2 font-lock-variable-name-face))
5953 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
5954 ;;; Too much noise from \s* @s[ and friends
5955 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
5956 ;;(3 font-lock-function-name-face t t)
5957 ;;(4
5958 ;; (if (cperl-slash-is-regexp)
5959 ;; font-lock-function-name-face 'default) nil t))
5960 )))
5961 (if cperl-highlight-variables-indiscriminately
5962 (setq t-font-lock-keywords-1
5963 (append t-font-lock-keywords-1
5964 (list '("\\([$*]{?\\sw+\\)" 1
5965 font-lock-variable-name-face)))))
5966 (setq cperl-font-lock-keywords-1
5967 (if cperl-syntaxify-by-font-lock
5968 (cons 'cperl-fontify-update
5969 t-font-lock-keywords)
5970 t-font-lock-keywords)
5971 cperl-font-lock-keywords cperl-font-lock-keywords-1
5972 cperl-font-lock-keywords-2 (append
5973 cperl-font-lock-keywords-1
5974 t-font-lock-keywords-1)))
5975 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
5976 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
5977 (eval ; Avoid a warning
5978 '(font-lock-require-faces
5979 (list
5980 ;; Color-light Color-dark Gray-light Gray-dark Mono
5981 (list 'font-lock-comment-face
5982 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
5983 nil
5984 [nil nil t t t]
5985 [nil nil t t t]
5986 nil)
5987 (list 'font-lock-string-face
5988 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
5989 nil
5990 nil
5991 [nil nil t t t]
5992 nil)
5993 (list 'font-lock-function-name-face
5994 (vector
5995 "Blue" "LightSkyBlue" "Gray50" "LightGray"
5996 (cdr (assq 'background-color ; if mono
5997 (frame-parameters))))
5998 (vector
5999 nil nil nil nil
6000 (cdr (assq 'foreground-color ; if mono
6001 (frame-parameters))))
6002 [nil nil t t t]
6003 nil
6004 nil)
6005 (list 'font-lock-variable-name-face
6006 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
6007 nil
6008 [nil nil t t t]
6009 [nil nil t t t]
6010 nil)
6011 (list 'font-lock-type-face
6012 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
6013 nil
6014 [nil nil t t t]
6015 nil
6016 [nil nil t t t])
6017 (list 'font-lock-warning-face
6018 ["Pink" "Red" "Gray50" "LightGray"]
6019 ["gray20" "gray90"
6020 "gray80" "gray20"]
6021 [nil nil t t t]
6022 nil
6023 [nil nil t t t]
6024 )
6025 (list 'font-lock-constant-face
6026 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
6027 nil
6028 [nil nil t t t]
6029 nil
6030 [nil nil t t t])
6031 (list 'cperl-nonoverridable-face
6032 ["chartreuse3" ("orchid1" "orange")
6033 nil "Gray80"]
6034 [nil nil "gray90"]
6035 [nil nil nil t t]
6036 [nil nil t t]
6037 [nil nil t t t])
6038 (list 'cperl-array-face
6039 ["blue" "yellow" nil "Gray80"]
6040 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6041 "gray90"]
6042 t
6043 nil
6044 nil)
6045 (list 'cperl-hash-face
6046 ["red" "red" nil "Gray80"]
6047 ["lightyellow2" ("navy" "os2blue" "darkgreen")
6048 "gray90"]
6049 t
6050 t
6051 nil))))
6052 ;; Do it the dull way, without choose-color
6053 (defvar cperl-guessed-background nil
6054 "Display characteristics as guessed by cperl.")
6055 ;; (or (fboundp 'x-color-defined-p)
6056 ;; (defalias 'x-color-defined-p
6057 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
6058 ;; ;; XEmacs >= 19.12
6059 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
6060 ;; ;; XEmacs 19.11
6061 ;; (t 'x-valid-color-name-p))))
6062 (cperl-force-face font-lock-constant-face
6063 "Face for constant and label names")
6064 (cperl-force-face font-lock-variable-name-face
6065 "Face for variable names")
6066 (cperl-force-face font-lock-type-face
6067 "Face for data types")
6068 (cperl-force-face cperl-nonoverridable-face
6069 "Face for data types from another group")
6070 (cperl-force-face font-lock-warning-face
6071 "Face for things which should stand out")
6072 (cperl-force-face font-lock-comment-face
6073 "Face for comments")
6074 (cperl-force-face font-lock-function-name-face
6075 "Face for function names")
6076 (cperl-force-face cperl-hash-face
6077 "Face for hashes")
6078 (cperl-force-face cperl-array-face
6079 "Face for arrays")
6080 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
6081 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
6082 ;;(or (boundp 'font-lock-type-face)
6083 ;; (defconst font-lock-type-face
6084 ;; 'font-lock-type-face
6085 ;; "Face to use for data types."))
6086 ;;(or (boundp 'cperl-nonoverridable-face)
6087 ;; (defconst cperl-nonoverridable-face
6088 ;; 'cperl-nonoverridable-face
6089 ;; "Face to use for data types from another group."))
6090 ;;(if (not (featurep 'xemacs)) nil
6091 ;; (or (boundp 'font-lock-comment-face)
6092 ;; (defconst font-lock-comment-face
6093 ;; 'font-lock-comment-face
6094 ;; "Face to use for comments."))
6095 ;; (or (boundp 'font-lock-keyword-face)
6096 ;; (defconst font-lock-keyword-face
6097 ;; 'font-lock-keyword-face
6098 ;; "Face to use for keywords."))
6099 ;; (or (boundp 'font-lock-function-name-face)
6100 ;; (defconst font-lock-function-name-face
6101 ;; 'font-lock-function-name-face
6102 ;; "Face to use for function names.")))
6103 (if (and
6104 (not (cperl-is-face 'cperl-array-face))
6105 (cperl-is-face 'font-lock-emphasized-face))
6106 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
6107 (if (and
6108 (not (cperl-is-face 'cperl-hash-face))
6109 (cperl-is-face 'font-lock-other-emphasized-face))
6110 (copy-face 'font-lock-other-emphasized-face 'cperl-hash-face))
6111 (if (and
6112 (not (cperl-is-face 'cperl-nonoverridable-face))
6113 (cperl-is-face 'font-lock-other-type-face))
6114 (copy-face 'font-lock-other-type-face 'cperl-nonoverridable-face))
6115 ;;(or (boundp 'cperl-hash-face)
6116 ;; (defconst cperl-hash-face
6117 ;; 'cperl-hash-face
6118 ;; "Face to use for hashes."))
6119 ;;(or (boundp 'cperl-array-face)
6120 ;; (defconst cperl-array-face
6121 ;; 'cperl-array-face
6122 ;; "Face to use for arrays."))
6123 ;; Here we try to guess background
6124 (let ((background
6125 (if (boundp 'font-lock-background-mode)
6126 font-lock-background-mode
6127 'light))
6128 (face-list (and (fboundp 'face-list) (face-list))))
6129;;;; (fset 'cperl-is-face
6130;;;; (cond ((fboundp 'find-face)
6131;;;; (symbol-function 'find-face))
6132;;;; (face-list
6133;;;; (function (lambda (face) (member face face-list))))
6134;;;; (t
6135;;;; (function (lambda (face) (boundp face))))))
6136 (defvar cperl-guessed-background
6137 (if (and (boundp 'font-lock-display-type)
6138 (eq font-lock-display-type 'grayscale))
6139 'gray
6140 background)
6141 "Background as guessed by CPerl mode")
6142 (and (not (cperl-is-face 'font-lock-constant-face))
6143 (cperl-is-face 'font-lock-reference-face)
6144 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
6145 (if (cperl-is-face 'font-lock-type-face) nil
6146 (copy-face 'default 'font-lock-type-face)
6147 (cond
6148 ((eq background 'light)
6149 (set-face-foreground 'font-lock-type-face
6150 (if (x-color-defined-p "seagreen")
6151 "seagreen"
6152 "sea green")))
6153 ((eq background 'dark)
6154 (set-face-foreground 'font-lock-type-face
6155 (if (x-color-defined-p "os2pink")
6156 "os2pink"
6157 "pink")))
6158 (t
6159 (set-face-background 'font-lock-type-face "gray90"))))
6160 (if (cperl-is-face 'cperl-nonoverridable-face)
6161 nil
6162 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
6163 (cond
6164 ((eq background 'light)
6165 (set-face-foreground 'cperl-nonoverridable-face
6166 (if (x-color-defined-p "chartreuse3")
6167 "chartreuse3"
6168 "chartreuse")))
6169 ((eq background 'dark)
6170 (set-face-foreground 'cperl-nonoverridable-face
6171 (if (x-color-defined-p "orchid1")
6172 "orchid1"
6173 "orange")))))
6174;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
6175;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
6176;;; (cond
6177;;; ((eq background 'light)
6178;;; (set-face-background 'font-lock-other-emphasized-face
6179;;; (if (x-color-defined-p "lightyellow2")
6180;;; "lightyellow2"
6181;;; (if (x-color-defined-p "lightyellow")
6182;;; "lightyellow"
6183;;; "light yellow"))))
6184;;; ((eq background 'dark)
6185;;; (set-face-background 'font-lock-other-emphasized-face
6186;;; (if (x-color-defined-p "navy")
6187;;; "navy"
6188;;; (if (x-color-defined-p "darkgreen")
6189;;; "darkgreen"
6190;;; "dark green"))))
6191;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
6192;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
6193;;; (copy-face 'bold 'font-lock-emphasized-face)
6194;;; (cond
6195;;; ((eq background 'light)
6196;;; (set-face-background 'font-lock-emphasized-face
6197;;; (if (x-color-defined-p "lightyellow2")
6198;;; "lightyellow2"
6199;;; "lightyellow")))
6200;;; ((eq background 'dark)
6201;;; (set-face-background 'font-lock-emphasized-face
6202;;; (if (x-color-defined-p "navy")
6203;;; "navy"
6204;;; (if (x-color-defined-p "darkgreen")
6205;;; "darkgreen"
6206;;; "dark green"))))
6207;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
6208 (if (cperl-is-face 'font-lock-variable-name-face) nil
6209 (copy-face 'italic 'font-lock-variable-name-face))
6210 (if (cperl-is-face 'font-lock-constant-face) nil
6211 (copy-face 'italic 'font-lock-constant-face))))
6212 (setq cperl-faces-init t))
6213 (error (message "cperl-init-faces (ignored): %s" errs))))
6214
6215
6216(defun cperl-ps-print-init ()
6217 "Initialization of `ps-print' components for faces used in CPerl."
6218 (eval-after-load "ps-print"
6219 '(setq ps-bold-faces
6220 ;; font-lock-variable-name-face
6221 ;; font-lock-constant-face
6222 (append '(cperl-array-face cperl-hash-face)
6223 ps-bold-faces)
6224 ps-italic-faces
6225 ;; font-lock-constant-face
6226 (append '(cperl-nonoverridable-face cperl-hash-face)
6227 ps-italic-faces)
6228 ps-underlined-faces
6229 ;; font-lock-type-face
6230 (append '(cperl-array-face cperl-hash-face underline cperl-nonoverridable-face)
6231 ps-underlined-faces))))
6232
6233(defvar ps-print-face-extension-alist)
6234
6235(defun cperl-ps-print (&optional file)
6236 "Pretty-print in CPerl style.
6237If optional argument FILE is an empty string, prints to printer, otherwise
6238to the file FILE. If FILE is nil, prompts for a file name.
6239
6240Style of printout regulated by the variable `cperl-ps-print-face-properties'."
6241 (interactive)
6242 (or file
6243 (setq file (read-from-minibuffer
6244 "Print to file (if empty - to printer): "
6245 (concat (buffer-file-name) ".ps")
6246 nil nil 'file-name-history)))
6247 (or (> (length file) 0)
6248 (setq file nil))
6249 (require 'ps-print) ; To get ps-print-face-extension-alist
6250 (let ((ps-print-color-p t)
6251 (ps-print-face-extension-alist ps-print-face-extension-alist))
6252 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
6253 (ps-print-buffer-with-faces file)))
6254
6255;;; (defun cperl-ps-print-init ()
6256;;; "Initialization of `ps-print' components for faces used in CPerl."
6257;;; ;; Guard against old versions
6258;;; (defvar ps-underlined-faces nil)
6259;;; (defvar ps-bold-faces nil)
6260;;; (defvar ps-italic-faces nil)
6261;;; (setq ps-bold-faces
6262;;; (append '(font-lock-emphasized-face
6263;;; cperl-array-face
6264;;; font-lock-keyword-face
6265;;; font-lock-variable-name-face
6266;;; font-lock-constant-face
6267;;; font-lock-reference-face
6268;;; font-lock-other-emphasized-face
6269;;; cperl-hash-face)
6270;;; ps-bold-faces))
6271;;; (setq ps-italic-faces
6272;;; (append '(cperl-nonoverridable-face
6273;;; font-lock-constant-face
6274;;; font-lock-reference-face
6275;;; font-lock-other-emphasized-face
6276;;; cperl-hash-face)
6277;;; ps-italic-faces))
6278;;; (setq ps-underlined-faces
6279;;; (append '(font-lock-emphasized-face
6280;;; cperl-array-face
6281;;; font-lock-other-emphasized-face
6282;;; cperl-hash-face
6283;;; cperl-nonoverridable-face font-lock-type-face)
6284;;; ps-underlined-faces))
6285;;; (cons 'font-lock-type-face ps-underlined-faces))
6286
6287
6288(if (cperl-enable-font-lock) (cperl-windowed-init))
6289
6290(defconst cperl-styles-entries
6291 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
6292 cperl-label-offset cperl-extra-newline-before-brace
6293 cperl-extra-newline-before-brace-multiline
6294 cperl-merge-trailing-else
6295 cperl-continued-statement-offset))
6296
6297(defconst cperl-style-examples
6298"##### Numbers etc are: cperl-indent-level cperl-brace-offset
6299##### cperl-continued-brace-offset cperl-label-offset
6300##### cperl-continued-statement-offset
6301##### cperl-merge-trailing-else cperl-extra-newline-before-brace
6302
6303########### (Do not forget cperl-extra-newline-before-brace-multiline)
6304
6305### CPerl (=GNU - extra-newline-before-brace + merge-trailing-else) 2/0/0/-2/2/t/nil
6306if (foo) {
6307 bar
6308 baz;
6309 label:
6310 {
6311 boon;
6312 }
6313} else {
6314 stop;
6315}
6316
6317### PerlStyle (=CPerl with 4 as indent) 4/0/0/-4/4/t/nil
6318if (foo) {
6319 bar
6320 baz;
6321 label:
6322 {
6323 boon;
6324 }
6325} else {
6326 stop;
6327}
6328
6329### GNU 2/0/0/-2/2/nil/t
6330if (foo)
6331 {
6332 bar
6333 baz;
6334 label:
6335 {
6336 boon;
6337 }
6338 }
6339else
6340 {
6341 stop;
6342 }
6343
6344### C++ (=PerlStyle with braces aligned with control words) 4/0/-4/-4/4/nil/t
6345if (foo)
6346{
6347 bar
6348 baz;
6349 label:
6350 {
6351 boon;
6352 }
6353}
6354else
6355{
6356 stop;
6357}
6358
6359### BSD (=C++, but will not change preexisting merge-trailing-else
6360### and extra-newline-before-brace ) 4/0/-4/-4/4
6361if (foo)
6362{
6363 bar
6364 baz;
6365 label:
6366 {
6367 boon;
6368 }
6369}
6370else
6371{
6372 stop;
6373}
6374
6375### K&R (=C++ with indent 5 - merge-trailing-else, but will not
6376### change preexisting extra-newline-before-brace) 5/0/-5/-5/5/nil
6377if (foo)
6378{
6379 bar
6380 baz;
6381 label:
6382 {
6383 boon;
6384 }
6385}
6386else
6387{
6388 stop;
6389}
6390
6391### Whitesmith (=PerlStyle, but will not change preexisting
6392### extra-newline-before-brace and merge-trailing-else) 4/0/0/-4/4
6393if (foo)
6394 {
6395 bar
6396 baz;
6397 label:
6398 {
6399 boon;
6400 }
6401 }
6402else
6403 {
6404 stop;
6405 }
6406"
6407"Examples of if/else with different indent styles (with v4.23).")
6408
6409(defconst cperl-style-alist
6410 '(("CPerl" ;; =GNU - extra-newline-before-brace + cperl-merge-trailing-else
6411 (cperl-indent-level . 2)
6412 (cperl-brace-offset . 0)
6413 (cperl-continued-brace-offset . 0)
6414 (cperl-label-offset . -2)
6415 (cperl-continued-statement-offset . 2)
6416 (cperl-extra-newline-before-brace . nil)
6417 (cperl-extra-newline-before-brace-multiline . nil)
6418 (cperl-merge-trailing-else . t))
6419
6420 ("PerlStyle" ; CPerl with 4 as indent
6421 (cperl-indent-level . 4)
6422 (cperl-brace-offset . 0)
6423 (cperl-continued-brace-offset . 0)
6424 (cperl-label-offset . -4)
6425 (cperl-continued-statement-offset . 4)
6426 (cperl-extra-newline-before-brace . nil)
6427 (cperl-extra-newline-before-brace-multiline . nil)
6428 (cperl-merge-trailing-else . t))
6429
6430 ("GNU"
6431 (cperl-indent-level . 2)
6432 (cperl-brace-offset . 0)
6433 (cperl-continued-brace-offset . 0)
6434 (cperl-label-offset . -2)
6435 (cperl-continued-statement-offset . 2)
6436 (cperl-extra-newline-before-brace . t)
6437 (cperl-extra-newline-before-brace-multiline . t)
6438 (cperl-merge-trailing-else . nil))
6439
6440 ("K&R"
6441 (cperl-indent-level . 5)
6442 (cperl-brace-offset . 0)
6443 (cperl-continued-brace-offset . -5)
6444 (cperl-label-offset . -5)
6445 (cperl-continued-statement-offset . 5)
6446 ;;(cperl-extra-newline-before-brace . nil) ; ???
6447 ;;(cperl-extra-newline-before-brace-multiline . nil)
6448 (cperl-merge-trailing-else . nil))
6449
6450 ("BSD"
6451 (cperl-indent-level . 4)
6452 (cperl-brace-offset . 0)
6453 (cperl-continued-brace-offset . -4)
6454 (cperl-label-offset . -4)
6455 (cperl-continued-statement-offset . 4)
6456 ;;(cperl-extra-newline-before-brace . nil) ; ???
6457 ;;(cperl-extra-newline-before-brace-multiline . nil)
6458 ;;(cperl-merge-trailing-else . nil) ; ???
6459 )
6460
6461 ("C++"
6462 (cperl-indent-level . 4)
6463 (cperl-brace-offset . 0)
6464 (cperl-continued-brace-offset . -4)
6465 (cperl-label-offset . -4)
6466 (cperl-continued-statement-offset . 4)
6467 (cperl-extra-newline-before-brace . t)
6468 (cperl-extra-newline-before-brace-multiline . t)
6469 (cperl-merge-trailing-else . nil))
6470
6471 ("Whitesmith"
6472 (cperl-indent-level . 4)
6473 (cperl-brace-offset . 0)
6474 (cperl-continued-brace-offset . 0)
6475 (cperl-label-offset . -4)
6476 (cperl-continued-statement-offset . 4)
6477 ;;(cperl-extra-newline-before-brace . nil) ; ???
6478 ;;(cperl-extra-newline-before-brace-multiline . nil)
6479 ;;(cperl-merge-trailing-else . nil) ; ???
6480 )
6481 ("Current"))
6482 "List of variables to set to get a particular indentation style.
6483Should be used via `cperl-set-style' or via Perl menu.
6484
6485See examples in `cperl-style-examples'.")
6486
6487(defun cperl-set-style (style)
6488 "Set CPerl mode variables to use one of several different indentation styles.
6489The arguments are a string representing the desired style.
6490The list of styles is in `cperl-style-alist', available styles
6491are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
6492
6493The current value of style is memorized (unless there is a memorized
6494data already), may be restored by `cperl-set-style-back'.
6495
6496Chosing \"Current\" style will not change style, so this may be used for
6497side-effect of memorizing only. Examples in `cperl-style-examples'."
6498 (interactive
6499 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
6500 cperl-style-alist)))
6501 (list (completing-read "Enter style: " list nil 'insist))))
6502 (or cperl-old-style
6503 (setq cperl-old-style
6504 (mapcar (function
6505 (lambda (name)
6506 (cons name (eval name))))
6507 cperl-styles-entries)))
6508 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
6509 (while style
6510 (setq setting (car style) style (cdr style))
6511 (set (car setting) (cdr setting)))))
6512
6513(defun cperl-set-style-back ()
6514 "Restore a style memorized by `cperl-set-style'."
6515 (interactive)
6516 (or cperl-old-style (error "The style was not changed"))
6517 (let (setting)
6518 (while cperl-old-style
6519 (setq setting (car cperl-old-style)
6520 cperl-old-style (cdr cperl-old-style))
6521 (set (car setting) (cdr setting)))))
6522
6523(defun cperl-check-syntax ()
6524 (interactive)
6525 (require 'mode-compile)
6526 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
6527 (eval '(mode-compile)))) ; Avoid a warning
6528
6529(defun cperl-info-buffer (type)
6530 ;; Returns buffer with documentation. Creates if missing.
6531 ;; If TYPE, this vars buffer.
6532 ;; Special care is taken to not stomp over an existing info buffer
6533 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
6534 (info (get-buffer bname))
6535 (oldbuf (get-buffer "*info*")))
6536 (if info info
6537 (save-window-excursion
6538 ;; Get Info running
6539 (require 'info)
6540 (cond (oldbuf
6541 (set-buffer oldbuf)
6542 (rename-buffer "*info-perl-tmp*")))
6543 (save-window-excursion
6544 (info))
6545 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
6546 (set-buffer "*info*")
6547 (rename-buffer bname)
6548 (cond (oldbuf
6549 (set-buffer "*info-perl-tmp*")
6550 (rename-buffer "*info*")
6551 (set-buffer bname)))
6552 (make-local-variable 'window-min-height)
6553 (setq window-min-height 2)
6554 (current-buffer)))))
6555
6556(defun cperl-word-at-point (&optional p)
6557 "Return the word at point or at P."
6558 (save-excursion
6559 (if p (goto-char p))
6560 (or (cperl-word-at-point-hard)
6561 (progn
6562 (require 'etags)
6563 (funcall (or (and (boundp 'find-tag-default-function)
6564 find-tag-default-function)
6565 (get major-mode 'find-tag-default-function)
6566 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
6567 ;; automatically used within `find-tag-default':
6568 'find-tag-default))))))
6569
6570(defun cperl-info-on-command (command)
6571 "Show documentation for Perl command COMMAND in other window.
6572If perl-info buffer is shown in some frame, uses this frame.
6573Customized by setting variables `cperl-shrink-wrap-info-frame',
6574`cperl-max-help-size'."
6575 (interactive
6576 (let* ((default (cperl-word-at-point))
6577 (read (read-string
6578 (format "Find doc for Perl function (default %s): "
6579 default))))
6580 (list (if (equal read "")
6581 default
6582 read))))
6583
6584 (let ((buffer (current-buffer))
6585 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
6586 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
6587 max-height char-height buf-list)
6588 (if (string-match "^-[a-zA-Z]$" command)
6589 (setq cmd-desc "^-X[ \t\n]"))
6590 (setq isvar (string-match "^[$@%]" command)
6591 buf (cperl-info-buffer isvar)
6592 iniwin (selected-window)
6593 fr1 (window-frame iniwin))
6594 (set-buffer buf)
6595 (goto-char (point-min))
6596 (or isvar
6597 (progn (re-search-forward "^-X[ \t\n]")
6598 (forward-line -1)))
6599 (if (re-search-forward cmd-desc nil t)
6600 (progn
6601 ;; Go back to beginning of the group (ex, for qq)
6602 (if (re-search-backward "^[ \t\n\f]")
6603 (forward-line 1))
6604 (beginning-of-line)
6605 ;; Get some of
6606 (setq pos (point)
6607 buf-list (list buf "*info-perl-var*" "*info-perl*"))
6608 (while (and (not win) buf-list)
6609 (setq win (get-buffer-window (car buf-list) t))
6610 (setq buf-list (cdr buf-list)))
6611 (or (not win)
6612 (eq (window-buffer win) buf)
6613 (set-window-buffer win buf))
6614 (and win (setq fr2 (window-frame win)))
6615 (if (or (not fr2) (eq fr1 fr2))
6616 (pop-to-buffer buf)
6617 (special-display-popup-frame buf) ; Make it visible
6618 (select-window win))
6619 (goto-char pos) ; Needed (?!).
6620 ;; Resize
6621 (setq iniheight (window-height)
6622 frheight (frame-height)
6623 not-loner (< iniheight (1- frheight))) ; Are not alone
6624 (cond ((if not-loner cperl-max-help-size
6625 cperl-shrink-wrap-info-frame)
6626 (setq height
6627 (+ 2
6628 (count-lines
6629 pos
6630 (save-excursion
6631 (if (re-search-forward
6632 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
6633 (match-beginning 0) (point-max)))))
6634 max-height
6635 (if not-loner
6636 (/ (* (- frheight 3) cperl-max-help-size) 100)
6637 (setq char-height (frame-char-height))
6638 ;; Non-functioning under OS/2:
6639 (if (eq char-height 1) (setq char-height 18))
6640 ;; Title, menubar, + 2 for slack
6641 (- (/ (display-pixel-height) char-height) 4)))
6642 (if (> height max-height) (setq height max-height))
6643 ;;(message "was %s doing %s" iniheight height)
6644 (if not-loner
6645 (enlarge-window (- height iniheight))
6646 (set-frame-height (window-frame win) (1+ height)))))
6647 (set-window-start (selected-window) pos))
6648 (message "No entry for %s found." command))
6649 ;;(pop-to-buffer buffer)
6650 (select-window iniwin)))
6651
6652(defun cperl-info-on-current-command ()
6653 "Show documentation for Perl command at point in other window."
6654 (interactive)
6655 (cperl-info-on-command (cperl-word-at-point)))
6656
6657(defun cperl-imenu-info-imenu-search ()
6658 (if (looking-at "^-X[ \t\n]") nil
6659 (re-search-backward
6660 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
6661 (forward-line 1)))
6662
6663(defun cperl-imenu-info-imenu-name ()
6664 (buffer-substring
6665 (match-beginning 1) (match-end 1)))
6666
6667(defun cperl-imenu-on-info ()
6668 "Shows imenu for Perl Info Buffer.
6669Opens Perl Info buffer if needed."
6670 (interactive)
6671 (let* ((buffer (current-buffer))
6672 imenu-create-index-function
6673 imenu-prev-index-position-function
6674 imenu-extract-index-name-function
6675 (index-item (save-restriction
6676 (save-window-excursion
6677 (set-buffer (cperl-info-buffer nil))
6678 (setq imenu-create-index-function
6679 'imenu-default-create-index-function
6680 imenu-prev-index-position-function
6681 'cperl-imenu-info-imenu-search
6682 imenu-extract-index-name-function
6683 'cperl-imenu-info-imenu-name)
6684 (imenu-choose-buffer-index)))))
6685 (and index-item
6686 (progn
6687 (push-mark)
6688 (pop-to-buffer "*info-perl*")
6689 (cond
6690 ((markerp (cdr index-item))
6691 (goto-char (marker-position (cdr index-item))))
6692 (t
6693 (goto-char (cdr index-item))))
6694 (set-window-start (selected-window) (point))
6695 (pop-to-buffer buffer)))))
6696
6697(defun cperl-lineup (beg end &optional step minshift)
6698 "Lineup construction in a region.
6699Beginning of region should be at the start of a construction.
6700All first occurrences of this construction in the lines that are
6701partially contained in the region are lined up at the same column.
6702
6703MINSHIFT is the minimal amount of space to insert before the construction.
6704STEP is the tabwidth to position constructions.
6705If STEP is nil, `cperl-lineup-step' will be used
6706\(or `cperl-indent-level', if `cperl-lineup-step' is nil).
6707Will not move the position at the start to the left."
6708 (interactive "r")
6709 (let (search col tcol seen b)
6710 (save-excursion
6711 (goto-char end)
6712 (end-of-line)
6713 (setq end (point-marker))
6714 (goto-char beg)
6715 (skip-chars-forward " \t\f")
6716 (setq beg (point-marker))
6717 (indent-region beg end nil)
6718 (goto-char beg)
6719 (setq col (current-column))
6720 (if (looking-at "[a-zA-Z0-9_]")
6721 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
6722 (setq search
6723 (concat "\\<"
6724 (regexp-quote
6725 (buffer-substring (match-beginning 0)
6726 (match-end 0))) "\\>"))
6727 (error "Cannot line up in a middle of the word"))
6728 (if (looking-at "$")
6729 (error "Cannot line up end of line"))
6730 (setq search (regexp-quote (char-to-string (following-char)))))
6731 (setq step (or step cperl-lineup-step cperl-indent-level))
6732 (or minshift (setq minshift 1))
6733 (while (progn
6734 (beginning-of-line 2)
6735 (and (< (point) end)
6736 (re-search-forward search end t)
6737 (goto-char (match-beginning 0))))
6738 (setq tcol (current-column) seen t)
6739 (if (> tcol col) (setq col tcol)))
6740 (or seen
6741 (error "The construction to line up occurred only once"))
6742 (goto-char beg)
6743 (setq col (+ col minshift))
6744 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
6745 (while
6746 (progn
6747 (cperl-make-indent col)
6748 (beginning-of-line 2)
6749 (and (< (point) end)
6750 (re-search-forward search end t)
6751 (goto-char (match-beginning 0)))))))) ; No body
6752
6753(defun cperl-etags (&optional add all files) ;; NOT USED???
6754 "Run etags with appropriate options for Perl files.
6755If optional argument ALL is `recursive', will process Perl files
6756in subdirectories too."
6757 (interactive)
6758 (let ((cmd "etags")
6759 (args '("-l" "none" "-r"
6760 ;; 1=fullname 2=package? 3=name 4=proto? 5=attrs? (VERY APPROX!)
6761 "/\\<sub[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([ \t]*:[^#{;]*\\)?\\([{#]\\|$\\)/\\3/"
6762 "-r"
6763 "/\\<package[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)\\)[ \\t]*\\([#;]\\|$\\)/\\1/"
6764 "-r"
6765 "/\\<\\(package\\)[ \\t]*;/\\1;/"))
6766 res)
6767 (if add (setq args (cons "-a" args)))
6768 (or files (setq files (list buffer-file-name)))
6769 (cond
6770 ((eq all 'recursive)
6771 ;;(error "Not implemented: recursive")
6772 (setq args (append (list "-e"
6773 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
6774 use File::Find;
6775 find(\\&wanted, '.');
6776 exec @ARGV;"
6777 cmd) args)
6778 cmd "perl"))
6779 (all
6780 ;;(error "Not implemented: all")
6781 (setq args (append (list "-e"
6782 "push @ARGV, <*.PL *.pl *.pm>;
6783 exec @ARGV;"
6784 cmd) args)
6785 cmd "perl"))
6786 (t
6787 (setq args (append args files))))
6788 (setq res (apply 'call-process cmd nil nil nil args))
6789 (or (eq res 0)
6790 (message "etags returned \"%s\"" res))))
6791
6792(defun cperl-toggle-auto-newline ()
6793 "Toggle the state of `cperl-auto-newline'."
6794 (interactive)
6795 (setq cperl-auto-newline (not cperl-auto-newline))
6796 (message "Newlines will %sbe auto-inserted now."
6797 (if cperl-auto-newline "" "not ")))
6798
6799(defun cperl-toggle-abbrev ()
6800 "Toggle the state of automatic keyword expansion in CPerl mode."
6801 (interactive)
6802 (abbrev-mode (if abbrev-mode 0 1))
6803 (message "Perl control structure will %sbe auto-inserted now."
6804 (if abbrev-mode "" "not ")))
6805
6806
6807(defun cperl-toggle-electric ()
6808 "Toggle the state of parentheses doubling in CPerl mode."
6809 (interactive)
6810 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
6811 (message "Parentheses will %sbe auto-doubled now."
6812 (if (cperl-val 'cperl-electric-parens) "" "not ")))
6813
6814(defun cperl-toggle-autohelp ()
6815 "Toggle the state of Auto-Help on Perl constructs (put in the message area).
6816Delay of auto-help controlled by `cperl-lazy-help-time'."
6817 (interactive)
6818 (if (fboundp 'run-with-idle-timer)
6819 (progn
6820 (if cperl-lazy-installed
6821 (cperl-lazy-unstall)
6822 (cperl-lazy-install))
6823 (message "Perl help messages will %sbe automatically shown now."
6824 (if cperl-lazy-installed "" "not ")))
6825 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
6826
6827(defun cperl-toggle-construct-fix ()
6828 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
6829 (interactive)
6830 (setq cperl-indent-region-fix-constructs
6831 (if cperl-indent-region-fix-constructs
6832 nil
6833 1))
6834 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
6835 (if cperl-indent-region-fix-constructs "" "not ")))
6836
6837(defun cperl-toggle-set-debug-unwind (arg &optional backtrace)
6838 "Toggle (or, with numeric argument, set) debugging state of syntaxification.
6839Nonpositive numeric argument disables debugging messages. The message
6840summarizes which regions it was decided to rescan for syntactic constructs.
6841
6842The message looks like this:
6843
6844 Syxify req=123..138 actual=101..146 done-to: 112=>146 statepos: 73=>117
6845
6846Numbers are character positions in the buffer. REQ provides the range to
6847rescan requested by `font-lock'. ACTUAL is the range actually resyntaxified;
6848for correct operation it should start and end outside any special syntactic
6849construct. DONE-TO and STATEPOS indicate changes to internal caches maintained
6850by CPerl."
6851 (interactive "P")
6852 (or arg
6853 (setq arg (if (eq cperl-syntaxify-by-font-lock
6854 (if backtrace 'backtrace 'message)) 0 1)))
6855 (setq arg (if (> arg 0) (if backtrace 'backtrace 'message) t))
6856 (setq cperl-syntaxify-by-font-lock arg)
6857 (message "Debugging messages of syntax unwind %sabled."
6858 (if (eq arg t) "dis" "en")))
6859
6860;;;; Tags file creation.
6861
6862(defvar cperl-tmp-buffer " *cperl-tmp*")
6863
6864(defun cperl-setup-tmp-buf ()
6865 (set-buffer (get-buffer-create cperl-tmp-buffer))
6866 (set-syntax-table cperl-mode-syntax-table)
6867 (buffer-disable-undo)
6868 (auto-fill-mode 0)
6869 (if cperl-use-syntax-table-text-property-for-tags
6870 (progn
6871 (make-local-variable 'parse-sexp-lookup-properties)
6872 ;; Do not introduce variable if not needed, we check it!
6873 (set 'parse-sexp-lookup-properties t))))
6874
6875(defun cperl-xsub-scan ()
6876 (require 'imenu)
6877 (let ((index-alist '())
6878 (prev-pos 0) index index1 name package prefix)
6879 (goto-char (point-min))
6880 ;; Search for the function
6881 (progn ;;save-match-data
6882 (while (re-search-forward
6883 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
6884 nil t)
6885 (cond
6886 ((match-beginning 2) ; SECTION
6887 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
6888 (goto-char (match-beginning 0))
6889 (skip-chars-forward " \t")
6890 (forward-char 1)
6891 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
6892 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
6893 (setq prefix nil)))
6894 ((not package) nil) ; C language section
6895 ((match-beginning 3) ; XSUB
6896 (goto-char (1+ (match-beginning 3)))
6897 (setq index (imenu-example--name-and-position))
6898 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
6899 (if (and prefix (string-match (concat "^" prefix) name))
6900 (setq name (substring name (length prefix))))
6901 (cond ((string-match "::" name) nil)
6902 (t
6903 (setq index1 (cons (concat package "::" name) (cdr index)))
6904 (push index1 index-alist)))
6905 (setcar index name)
6906 (push index index-alist))
6907 (t ; BOOT: section
6908 ;; (beginning-of-line)
6909 (setq index (imenu-example--name-and-position))
6910 (setcar index (concat package "::BOOT:"))
6911 (push index index-alist)))))
6912 index-alist))
6913
6914(defvar cperl-unreadable-ok nil)
6915
6916(defun cperl-find-tags (ifile xs topdir)
6917 (let ((b (get-buffer cperl-tmp-buffer)) ind lst elt pos ret rel
6918 (cperl-pod-here-fontify nil) f file)
6919 (save-excursion
6920 (if b (set-buffer b)
6921 (cperl-setup-tmp-buf))
6922 (erase-buffer)
6923 (condition-case err
6924 (setq file (car (insert-file-contents ifile)))
6925 (error (if cperl-unreadable-ok nil
6926 (if (y-or-n-p
6927 (format "File %s unreadable. Continue? " ifile))
6928 (setq cperl-unreadable-ok t)
6929 (error "Aborting: unreadable file %s" ifile)))))
6930 (if (not file)
6931 (message "Unreadable file %s" ifile)
6932 (message "Scanning file %s ..." file)
6933 (if (and cperl-use-syntax-table-text-property-for-tags
6934 (not xs))
6935 (condition-case err ; after __END__ may have garbage
6936 (cperl-find-pods-heres nil nil noninteractive)
6937 (error (message "While scanning for syntax: %s" err))))
6938 (if xs
6939 (setq lst (cperl-xsub-scan))
6940 (setq ind (cperl-imenu--create-perl-index))
6941 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
6942 (setq lst
6943 (mapcar
6944 (function
6945 (lambda (elt)
6946 (cond ((string-match "^[_a-zA-Z]" (car elt))
6947 (goto-char (cdr elt))
6948 (beginning-of-line) ; pos should be of the start of the line
6949 (list (car elt)
6950 (point)
6951 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
6952 (buffer-substring (progn
6953 (goto-char (cdr elt))
6954 ;; After name now...
6955 (or (eolp) (forward-char 1))
6956 (point))
6957 (progn
6958 (beginning-of-line)
6959 (point))))))))
6960 lst))
6961 (erase-buffer)
6962 (while lst
6963 (setq elt (car lst) lst (cdr lst))
6964 (if elt
6965 (progn
6966 (insert (elt elt 3)
6967 127
6968 (if (string-match "^package " (car elt))
6969 (substring (car elt) 8)
6970 (car elt) )
6971 1
6972 (number-to-string (elt elt 2)) ; Line
6973 ","
6974 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
6975 "\n")
6976 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
6977 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
6978 (elt elt 3)))
6979 ;; Need to insert the name without package as well
6980 (setq lst (cons (cons (substring (elt elt 3)
6981 (match-beginning 1)
6982 (match-end 1))
6983 (cdr elt))
6984 lst))))))
6985 (setq pos (point))
6986 (goto-char 1)
6987 (setq rel file)
6988 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
6989 (set-text-properties 0 (length rel) nil rel)
6990 (and (equal topdir (substring rel 0 (length topdir)))
6991 (setq rel (substring file (length topdir))))
6992 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
6993 (setq ret (buffer-substring 1 (point-max)))
6994 (erase-buffer)
6995 (or noninteractive
6996 (message "Scanning file %s finished" file))
6997 ret))))
6998
6999(defun cperl-add-tags-recurse-noxs ()
7000 "Add to TAGS data for \"pure\" Perl files in the current directory and kids.
7001Use as
7002 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7003 -f cperl-add-tags-recurse-noxs
7004"
7005 (cperl-write-tags nil nil t t nil t))
7006
7007(defun cperl-add-tags-recurse-noxs-fullpath ()
7008 "Add to TAGS data for \"pure\" Perl in the current directory and kids.
7009Writes down fullpath, so TAGS is relocatable (but if the build directory
7010is relocated, the file TAGS inside it breaks). Use as
7011 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7012 -f cperl-add-tags-recurse-noxs-fullpath
7013"
7014 (cperl-write-tags nil nil t t nil t ""))
7015
7016(defun cperl-add-tags-recurse ()
7017 "Add to TAGS file data for Perl files in the current directory and kids.
7018Use as
7019 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
7020 -f cperl-add-tags-recurse
7021"
7022 (cperl-write-tags nil nil t t))
7023
7024(defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
7025 ;; If INBUFFER, do not select buffer, and do not save
7026 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
7027 (require 'etags)
7028 (if file nil
7029 (setq file (if dir default-directory (buffer-file-name)))
7030 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
7031 (or topdir
7032 (setq topdir default-directory))
7033 (let ((tags-file-name "TAGS")
7034 (case-fold-search (eq system-type 'emx))
7035 xs rel tm)
7036 (save-excursion
7037 (cond (inbuffer nil) ; Already there
7038 ((file-exists-p tags-file-name)
7039 (if (featurep 'xemacs)
7040 (visit-tags-table-buffer)
7041 (visit-tags-table-buffer tags-file-name)))
7042 (t (set-buffer (find-file-noselect tags-file-name))))
7043 (cond
7044 (dir
7045 (cond ((eq erase 'ignore))
7046 (erase
7047 (erase-buffer)
7048 (setq erase 'ignore)))
7049 (let ((files
7050 (condition-case err
7051 (directory-files file t
7052 (if recurse nil cperl-scan-files-regexp)
7053 t)
7054 (error
7055 (if cperl-unreadable-ok nil
7056 (if (y-or-n-p
7057 (format "Directory %s unreadable. Continue? " file))
7058 (setq cperl-unreadable-ok t
7059 tm nil) ; Return empty list
7060 (error "Aborting: unreadable directory %s" file)))))))
7061 (mapc (function
7062 (lambda (file)
7063 (cond
7064 ((string-match cperl-noscan-files-regexp file)
7065 nil)
7066 ((not (file-directory-p file))
7067 (if (string-match cperl-scan-files-regexp file)
7068 (cperl-write-tags file erase recurse nil t noxs topdir)))
7069 ((not recurse) nil)
7070 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
7071 files)))
7072 (t
7073 (setq xs (string-match "\\.xs$" file))
7074 (if (not (and xs noxs))
7075 (progn
7076 (cond ((eq erase 'ignore) (goto-char (point-max)))
7077 (erase (erase-buffer))
7078 (t
7079 (goto-char 1)
7080 (setq rel file)
7081 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
7082 (set-text-properties 0 (length rel) nil rel)
7083 (and (equal topdir (substring rel 0 (length topdir)))
7084 (setq rel (substring file (length topdir))))
7085 (if (search-forward (concat "\f\n" rel ",") nil t)
7086 (progn
7087 (search-backward "\f\n")
7088 (delete-region (point)
7089 (save-excursion
7090 (forward-char 1)
7091 (if (search-forward "\f\n"
7092 nil 'toend)
7093 (- (point) 2)
7094 (point-max)))))
7095 (goto-char (point-max)))))
7096 (insert (cperl-find-tags file xs topdir))))))
7097 (if inbuffer nil ; Delegate to the caller
7098 (save-buffer 0) ; No backup
7099 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
7100 (initialize-new-tags-table))))))
7101
7102(defvar cperl-tags-hier-regexp-list
7103 (concat
7104 "^\\("
7105 "\\(package\\)\\>"
7106 "\\|"
7107 "sub\\>[^\n]+::"
7108 "\\|"
7109 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
7110 "\\|"
7111 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
7112 "\\)"))
7113
7114(defvar cperl-hierarchy '(() ())
7115 "Global hierarchy of classes.")
7116
7117(defun cperl-tags-hier-fill ()
7118 ;; Suppose we are in a tag table cooked by cperl.
7119 (goto-char 1)
7120 (let (type pack name pos line chunk ord cons1 file str info fileind)
7121 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
7122 (setq pos (match-beginning 0)
7123 pack (match-beginning 2))
7124 (beginning-of-line)
7125 (if (looking-at (concat
7126 "\\([^\n]+\\)"
7127 "\C-?"
7128 "\\([^\n]+\\)"
7129 "\C-a"
7130 "\\([0-9]+\\)"
7131 ","
7132 "\\([0-9]+\\)"))
7133 (progn
7134 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
7135 name (buffer-substring (match-beginning 2) (match-end 2))
7136 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
7137 line (buffer-substring (match-beginning 3) (match-end 3))
7138 ord (if pack 1 0)
7139 file (file-of-tag)
7140 fileind (format "%s:%s" file line)
7141 ;; Moves to beginning of the next line:
7142 info (cperl-etags-snarf-tag file line))
7143 ;; Move back
7144 (forward-char -1)
7145 ;; Make new member of hierarchy name ==> file ==> pos if needed
7146 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
7147 ;; Name known
7148 (setcdr cons1 (cons (cons fileind (vector file info))
7149 (cdr cons1)))
7150 ;; First occurrence of the name, start alist
7151 (setq cons1 (cons name (list (cons fileind (vector file info)))))
7152 (if pack
7153 (setcar (cdr cperl-hierarchy)
7154 (cons cons1 (nth 1 cperl-hierarchy)))
7155 (setcar cperl-hierarchy
7156 (cons cons1 (car cperl-hierarchy)))))))
7157 (end-of-line))))
7158
7159(declare-function x-popup-menu "xmenu.c" (position menu))
7160
7161(defun cperl-tags-hier-init (&optional update)
7162 "Show hierarchical menu of classes and methods.
7163Finds info about classes by a scan of loaded TAGS files.
7164Supposes that the TAGS files contain fully qualified function names.
7165One may build such TAGS files from CPerl mode menu."
7166 (interactive)
7167 (require 'etags)
7168 (require 'imenu)
7169 (if (or update (null (nth 2 cperl-hierarchy)))
7170 (let ((remover (function (lambda (elt) ; (name (file1...) (file2..))
7171 (or (nthcdr 2 elt)
7172 ;; Only in one file
7173 (setcdr elt (cdr (nth 1 elt)))))))
7174 pack name cons1 to l1 l2 l3 l4 b)
7175 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
7176 (setq cperl-hierarchy (list l1 l2 l3))
7177 (if (featurep 'xemacs) ; Not checked
7178 (progn
7179 (or tags-file-name
7180 ;; Does this work in XEmacs?
7181 (call-interactively 'visit-tags-table))
7182 (message "Updating list of classes...")
7183 (set-buffer (get-file-buffer tags-file-name))
7184 (cperl-tags-hier-fill))
7185 (or tags-table-list
7186 (call-interactively 'visit-tags-table))
7187 (mapc
7188 (function
7189 (lambda (tagsfile)
7190 (message "Updating list of classes... %s" tagsfile)
7191 (set-buffer (get-file-buffer tagsfile))
7192 (cperl-tags-hier-fill)))
7193 tags-table-list)
7194 (message "Updating list of classes... postprocessing..."))
7195 (mapc remover (car cperl-hierarchy))
7196 (mapc remover (nth 1 cperl-hierarchy))
7197 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
7198 (cons "Methods: " (car cperl-hierarchy))))
7199 (cperl-tags-treeify to 1)
7200 (setcar (nthcdr 2 cperl-hierarchy)
7201 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
7202 (message "Updating list of classes: done, requesting display...")
7203 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
7204 ))
7205 (or (nth 2 cperl-hierarchy)
7206 (error "No items found"))
7207 (setq update
7208;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
7209 (if (if (fboundp 'display-popup-menus-p)
7210 (let ((f 'display-popup-menus-p))
7211 (funcall f))
7212 window-system)
7213 (x-popup-menu t (nth 2 cperl-hierarchy))
7214 (require 'tmm)
7215 (tmm-prompt (nth 2 cperl-hierarchy))))
7216 (if (and update (listp update))
7217 (progn (while (cdr update) (setq update (cdr update)))
7218 (setq update (car update)))) ; Get the last from the list
7219 (if (vectorp update)
7220 (progn
7221 (find-file (elt update 0))
7222 (cperl-etags-goto-tag-location (elt update 1))))
7223 (if (eq update -999) (cperl-tags-hier-init t)))
7224
7225(defun cperl-tags-treeify (to level)
7226 ;; cadr of `to' is read-write. On start it is a cons
7227 (let* ((regexp (concat "^\\(" (mapconcat
7228 'identity
7229 (make-list level "[_a-zA-Z0-9]+")
7230 "::")
7231 "\\)\\(::\\)?"))
7232 (packages (cdr (nth 1 to)))
7233 (methods (cdr (nth 2 to)))
7234 l1 head tail cons1 cons2 ord writeto packs recurse
7235 root-packages root-functions ms many_ms same_name ps
7236 (move-deeper
7237 (function
7238 (lambda (elt)
7239 (cond ((and (string-match regexp (car elt))
7240 (or (eq ord 1) (match-end 2)))
7241 (setq head (substring (car elt) 0 (match-end 1))
7242 tail (if (match-end 2) (substring (car elt)
7243 (match-end 2)))
7244 recurse t)
7245 (if (setq cons1 (assoc head writeto)) nil
7246 ;; Need to init new head
7247 (setcdr writeto (cons (list head (list "Packages: ")
7248 (list "Methods: "))
7249 (cdr writeto)))
7250 (setq cons1 (nth 1 writeto)))
7251 (setq cons2 (nth ord cons1)) ; Either packs or meths
7252 (setcdr cons2 (cons elt (cdr cons2))))
7253 ((eq ord 2)
7254 (setq root-functions (cons elt root-functions)))
7255 (t
7256 (setq root-packages (cons elt root-packages))))))))
7257 (setcdr to l1) ; Init to dynamic space
7258 (setq writeto to)
7259 (setq ord 1)
7260 (mapc move-deeper packages)
7261 (setq ord 2)
7262 (mapc move-deeper methods)
7263 (if recurse
7264 (mapc (function (lambda (elt)
7265 (cperl-tags-treeify elt (1+ level))))
7266 (cdr to)))
7267 ;;Now clean up leaders with one child only
7268 (mapc (function (lambda (elt)
7269 (if (not (and (listp (cdr elt))
7270 (eq (length elt) 2))) nil
7271 (setcar elt (car (nth 1 elt)))
7272 (setcdr elt (cdr (nth 1 elt))))))
7273 (cdr to))
7274 ;; Sort the roots of subtrees
7275 (if (default-value 'imenu-sort-function)
7276 (setcdr to
7277 (sort (cdr to) (default-value 'imenu-sort-function))))
7278 ;; Now add back functions removed from display
7279 (mapc (function (lambda (elt)
7280 (setcdr to (cons elt (cdr to)))))
7281 (if (default-value 'imenu-sort-function)
7282 (nreverse
7283 (sort root-functions (default-value 'imenu-sort-function)))
7284 root-functions))
7285 ;; Now add back packages removed from display
7286 (mapc (function (lambda (elt)
7287 (setcdr to (cons (cons (concat "package " (car elt))
7288 (cdr elt))
7289 (cdr to)))))
7290 (if (default-value 'imenu-sort-function)
7291 (nreverse
7292 (sort root-packages (default-value 'imenu-sort-function)))
7293 root-packages))))
7294
7295;;;(x-popup-menu t
7296;;; '(keymap "Name1"
7297;;; ("Ret1" "aa")
7298;;; ("Head1" "ab"
7299;;; keymap "Name2"
7300;;; ("Tail1" "x") ("Tail2" "y"))))
7301
7302(defun cperl-list-fold (list name limit)
7303 (let (list1 list2 elt1 (num 0))
7304 (if (<= (length list) limit) list
7305 (setq list1 nil list2 nil)
7306 (while list
7307 (setq num (1+ num)
7308 elt1 (car list)
7309 list (cdr list))
7310 (if (<= num imenu-max-items)
7311 (setq list2 (cons elt1 list2))
7312 (setq list1 (cons (cons name
7313 (nreverse list2))
7314 list1)
7315 list2 (list elt1)
7316 num 1)))
7317 (nreverse (cons (cons name
7318 (nreverse list2))
7319 list1)))))
7320
7321(defun cperl-menu-to-keymap (menu &optional name)
7322 (let (list)
7323 (cons 'keymap
7324 (mapcar
7325 (function
7326 (lambda (elt)
7327 (cond ((listp (cdr elt))
7328 (setq list (cperl-list-fold
7329 (cdr elt) (car elt) imenu-max-items))
7330 (cons nil
7331 (cons (car elt)
7332 (cperl-menu-to-keymap list))))
7333 (t
7334 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
7335 (cperl-list-fold menu "Root" imenu-max-items)))))
7336
7337\f
7338(defvar cperl-bad-style-regexp
7339 (mapconcat 'identity
7340 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
7341 "[-<>=+^&|]+[^- \t\n=+<>~]") ; sign+ char
7342 "\\|")
7343 "Finds places such that insertion of a whitespace may help a lot.")
7344
7345(defvar cperl-not-bad-style-regexp
7346 (mapconcat
7347 'identity
7348 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
7349 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
7350 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
7351 "<\\$?\\sw+\\(\\.\\(\\sw\\|_\\)+\\)?>" ; <IN> <stdin.h>
7352 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
7353 "-[0-9]" ; -5
7354 "\\+\\+" ; ++var
7355 "--" ; --var
7356 ".->" ; a->b
7357 "->" ; a SPACE ->b
7358 "\\[-" ; a[-1]
7359 "\\\\[&$@*\\\\]" ; \&func
7360 "^=" ; =head
7361 "\\$." ; $|
7362 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
7363 "||"
7364 "&&"
7365 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
7366 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
7367 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
7368 ;;"[*/+-|&<.]+="
7369 )
7370 "\\|")
7371 "If matches at the start of match found by `my-bad-c-style-regexp',
7372insertion of a whitespace will not help.")
7373
7374(defvar found-bad)
7375
7376(defun cperl-find-bad-style ()
7377 "Find places in the buffer where insertion of a whitespace may help.
7378Prompts user for insertion of spaces.
7379Currently it is tuned to C and Perl syntax."
7380 (interactive)
7381 (let (found-bad (p (point)))
7382 (setq last-nonmenu-event 13) ; To disable popup
7383 (goto-char (point-min))
7384 (map-y-or-n-p "Insert space here? "
7385 (lambda (arg) (insert " "))
7386 'cperl-next-bad-style
7387 '("location" "locations" "insert a space into")
7388 '((?\C-r (lambda (arg)
7389 (let ((buffer-quit-function
7390 'exit-recursive-edit))
7391 (message "Exit with Esc Esc")
7392 (recursive-edit)
7393 t)) ; Consider acted upon
7394 "edit, exit with Esc Esc")
7395 (?e (lambda (arg)
7396 (let ((buffer-quit-function
7397 'exit-recursive-edit))
7398 (message "Exit with Esc Esc")
7399 (recursive-edit)
7400 t)) ; Consider acted upon
7401 "edit, exit with Esc Esc"))
7402 t)
7403 (if found-bad (goto-char found-bad)
7404 (goto-char p)
7405 (message "No appropriate place found"))))
7406
7407(defun cperl-next-bad-style ()
7408 (let (p (not-found t) (point (point)) found)
7409 (while (and not-found
7410 (re-search-forward cperl-bad-style-regexp nil 'to-end))
7411 (setq p (point))
7412 (goto-char (match-beginning 0))
7413 (if (or
7414 (looking-at cperl-not-bad-style-regexp)
7415 ;; Check for a < -b and friends
7416 (and (eq (following-char) ?\-)
7417 (save-excursion
7418 (skip-chars-backward " \t\n")
7419 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\( ?\[ ?\{))))
7420 ;; Now check for syntax type
7421 (save-match-data
7422 (setq found (point))
7423 (beginning-of-defun)
7424 (let ((pps (parse-partial-sexp (point) found)))
7425 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
7426 (goto-char (match-end 0))
7427 (goto-char (1- p))
7428 (setq not-found nil
7429 found-bad found)))
7430 (not not-found)))
7431
7432\f
7433;;; Getting help
7434(defvar cperl-have-help-regexp
7435 ;;(concat "\\("
7436 (mapconcat
7437 'identity
7438 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
7439 "[$@]\\^[a-zA-Z]" ; Special variable
7440 "[$@][^ \n\t]" ; Special variable
7441 "-[a-zA-Z]" ; File test
7442 "\\\\[a-zA-Z0]" ; Special chars
7443 "^=[a-z][a-zA-Z0-9_]*" ; POD sections
7444 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
7445 "[a-zA-Z_0-9:]+" ; symbol or number
7446 "x="
7447 "#!")
7448 ;;"\\)\\|\\("
7449 "\\|")
7450 ;;"\\)"
7451 ;;)
7452 "Matches places in the buffer we can find help for.")
7453
7454(defvar cperl-message-on-help-error t)
7455(defvar cperl-help-from-timer nil)
7456
7457(defun cperl-word-at-point-hard ()
7458 ;; Does not save-excursion
7459 ;; Get to the something meaningful
7460 (or (eobp) (eolp) (forward-char 1))
7461 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
7462 (save-excursion (beginning-of-line) (point))
7463 'to-beg)
7464 ;; (cond
7465 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
7466 ;; (skip-chars-backward " \n\t\r({[]});,")
7467 ;; (or (bobp) (backward-char 1))))
7468 ;; Try to backtrace
7469 (cond
7470 ((looking-at "[a-zA-Z0-9_:]") ; symbol
7471 (skip-chars-backward "a-zA-Z0-9_:")
7472 (cond
7473 ((and (eq (preceding-char) ?^) ; $^I
7474 (eq (char-after (- (point) 2)) ?\$))
7475 (forward-char -2))
7476 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
7477 (forward-char -1))
7478 ((and (eq (preceding-char) ?\=)
7479 (eq (current-column) 1))
7480 (forward-char -1))) ; =head1
7481 (if (and (eq (preceding-char) ?\<)
7482 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
7483 (forward-char -1)))
7484 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
7485 (forward-char -1))
7486 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
7487 (forward-char -1))
7488 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
7489 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
7490 (cond
7491 ((and (eq (preceding-char) ?\$)
7492 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
7493 (forward-char -1))
7494 ((and (eq (following-char) ?\>)
7495 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
7496 (save-excursion
7497 (forward-sexp -1)
7498 (and (eq (preceding-char) ?\<)
7499 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
7500 (search-backward "<"))))
7501 ((and (eq (following-char) ?\$)
7502 (eq (preceding-char) ?\<)
7503 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
7504 (forward-char -1)))
7505 (if (looking-at cperl-have-help-regexp)
7506 (buffer-substring (match-beginning 0) (match-end 0))))
7507
7508(defun cperl-get-help ()
7509 "Get one-line docs on the symbol at the point.
7510The data for these docs is a little bit obsolete and may be in fact longer
7511than a line. Your contribution to update/shorten it is appreciated."
7512 (interactive)
7513 (save-match-data ; May be called "inside" query-replace
7514 (save-excursion
7515 (let ((word (cperl-word-at-point-hard)))
7516 (if word
7517 (if (and cperl-help-from-timer ; Bail out if not in mainland
7518 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
7519 (or (memq (get-text-property (point) 'face)
7520 '(font-lock-comment-face font-lock-string-face))
7521 (memq (get-text-property (point) 'syntax-type)
7522 '(pod here-doc format))))
7523 nil
7524 (cperl-describe-perl-symbol word))
7525 (if cperl-message-on-help-error
7526 (message "Nothing found for %s..."
7527 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
7528
7529;;; Stolen from perl-descr.el by Johan Vromans:
7530
7531(defvar cperl-doc-buffer " *perl-doc*"
7532 "Where the documentation can be found.")
7533
7534(defun cperl-describe-perl-symbol (val)
7535 "Display the documentation of symbol at point, a Perl operator."
7536 (let ((enable-recursive-minibuffers t)
7537 args-file regexp)
7538 (cond
7539 ((string-match "^[&*][a-zA-Z_]" val)
7540 (setq val (concat (substring val 0 1) "NAME")))
7541 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
7542 (setq val (concat "@" (substring val 1 (match-end 1)))))
7543 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
7544 (setq val (concat "%" (substring val 1 (match-end 1)))))
7545 ((and (string= val "x") (string-match "^x=" val))
7546 (setq val "x="))
7547 ((string-match "^\\$[\C-a-\C-z]" val)
7548 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
7549 ((string-match "^CORE::" val)
7550 (setq val "CORE::"))
7551 ((string-match "^SUPER::" val)
7552 (setq val "SUPER::"))
7553 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
7554 (setq val "<NAME>")))
7555 (setq regexp (concat "^"
7556 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
7557 (regexp-quote val)
7558 "\\([ \t([/]\\|$\\)"))
7559
7560 ;; get the buffer with the documentation text
7561 (cperl-switch-to-doc-buffer)
7562
7563 ;; lookup in the doc
7564 (goto-char (point-min))
7565 (let ((case-fold-search nil))
7566 (list
7567 (if (re-search-forward regexp (point-max) t)
7568 (save-excursion
7569 (beginning-of-line 1)
7570 (let ((lnstart (point)))
7571 (end-of-line)
7572 (message "%s" (buffer-substring lnstart (point)))))
7573 (if cperl-message-on-help-error
7574 (message "No definition for %s" val)))))))
7575
7576(defvar cperl-short-docs 'please-ignore-this-line
7577 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
7578 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
7579... Range (list context); flip/flop [no flop when flip] (scalar context).
7580! ... Logical negation.
7581... != ... Numeric inequality.
7582... !~ ... Search pattern, substitution, or translation (negated).
7583$! In numeric context: errno. In a string context: error string.
7584$\" The separator which joins elements of arrays interpolated in strings.
7585$# The output format for printed numbers. Default is %.15g or close.
7586$$ Process number of this script. Changes in the fork()ed child process.
7587$% The current page number of the currently selected output channel.
7588
7589 The following variables are always local to the current block:
7590
7591$1 Match of the 1st set of parentheses in the last match (auto-local).
7592$2 Match of the 2nd set of parentheses in the last match (auto-local).
7593$3 Match of the 3rd set of parentheses in the last match (auto-local).
7594$4 Match of the 4th set of parentheses in the last match (auto-local).
7595$5 Match of the 5th set of parentheses in the last match (auto-local).
7596$6 Match of the 6th set of parentheses in the last match (auto-local).
7597$7 Match of the 7th set of parentheses in the last match (auto-local).
7598$8 Match of the 8th set of parentheses in the last match (auto-local).
7599$9 Match of the 9th set of parentheses in the last match (auto-local).
7600$& The string matched by the last pattern match (auto-local).
7601$' The string after what was matched by the last match (auto-local).
7602$` The string before what was matched by the last match (auto-local).
7603
7604$( The real gid of this process.
7605$) The effective gid of this process.
7606$* Deprecated: Set to 1 to do multiline matching within a string.
7607$+ The last bracket matched by the last search pattern.
7608$, The output field separator for the print operator.
7609$- The number of lines left on the page.
7610$. The current input line number of the last filehandle that was read.
7611$/ The input record separator, newline by default.
7612$0 Name of the file containing the current perl script (read/write).
7613$: String may be broken after these characters to fill ^-lines in a format.
7614$; Subscript separator for multi-dim array emulation. Default \"\\034\".
7615$< The real uid of this process.
7616$= The page length of the current output channel. Default is 60 lines.
7617$> The effective uid of this process.
7618$? The status returned by the last ``, pipe close or `system'.
7619$@ The perl error message from the last eval or do @var{EXPR} command.
7620$ARGV The name of the current file used with <> .
7621$[ Deprecated: The index of the first element/char in an array/string.
7622$\\ The output record separator for the print operator.
7623$] The perl version string as displayed with perl -v.
7624$^ The name of the current top-of-page format.
7625$^A The current value of the write() accumulator for format() lines.
7626$^D The value of the perl debug (-D) flags.
7627$^E Information about the last system error other than that provided by $!.
7628$^F The highest system file descriptor, ordinarily 2.
7629$^H The current set of syntax checks enabled by `use strict'.
7630$^I The value of the in-place edit extension (perl -i option).
7631$^L What formats output to perform a formfeed. Default is \\f.
7632$^M A buffer for emergency memory allocation when running out of memory.
7633$^O The operating system name under which this copy of Perl was built.
7634$^P Internal debugging flag.
7635$^T The time the script was started. Used by -A/-M/-C file tests.
7636$^W True if warnings are requested (perl -w flag).
7637$^X The name under which perl was invoked (argv[0] in C-speech).
7638$_ The default input and pattern-searching space.
7639$| Auto-flush after write/print on current output channel? Default 0.
7640$~ The name of the current report format.
7641... % ... Modulo division.
7642... %= ... Modulo division assignment.
7643%ENV Contains the current environment.
7644%INC List of files that have been require-d or do-ne.
7645%SIG Used to set signal handlers for various signals.
7646... & ... Bitwise and.
7647... && ... Logical and.
7648... &&= ... Logical and assignment.
7649... &= ... Bitwise and assignment.
7650... * ... Multiplication.
7651... ** ... Exponentiation.
7652*NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
7653&NAME(arg0, ...) Subroutine call. Arguments go to @_.
7654... + ... Addition. +EXPR Makes EXPR into scalar context.
7655++ Auto-increment (magical on strings). ++EXPR EXPR++
7656... += ... Addition assignment.
7657, Comma operator.
7658... - ... Subtraction.
7659-- Auto-decrement (NOT magical on strings). --EXPR EXPR--
7660... -= ... Subtraction assignment.
7661-A Access time in days since script started.
7662-B File is a non-text (binary) file.
7663-C Inode change time in days since script started.
7664-M Age in days since script started.
7665-O File is owned by real uid.
7666-R File is readable by real uid.
7667-S File is a socket .
7668-T File is a text file.
7669-W File is writable by real uid.
7670-X File is executable by real uid.
7671-b File is a block special file.
7672-c File is a character special file.
7673-d File is a directory.
7674-e File exists .
7675-f File is a plain file.
7676-g File has setgid bit set.
7677-k File has sticky bit set.
7678-l File is a symbolic link.
7679-o File is owned by effective uid.
7680-p File is a named pipe (FIFO).
7681-r File is readable by effective uid.
7682-s File has non-zero size.
7683-t Tests if filehandle (STDIN by default) is opened to a tty.
7684-u File has setuid bit set.
7685-w File is writable by effective uid.
7686-x File is executable by effective uid.
7687-z File has zero size.
7688. Concatenate strings.
7689.. Range (list context); flip/flop (scalar context) operator.
7690.= Concatenate assignment strings
7691... / ... Division. /PATTERN/ioxsmg Pattern match
7692... /= ... Division assignment.
7693/PATTERN/ioxsmg Pattern match.
7694... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
7695<NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
7696<pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
7697<> Reads line from union of files in @ARGV (= command line) and STDIN.
7698... << ... Bitwise shift left. << start of HERE-DOCUMENT.
7699... <= ... Numeric less than or equal to.
7700... <=> ... Numeric compare.
7701... = ... Assignment.
7702... == ... Numeric equality.
7703... =~ ... Search pattern, substitution, or translation
7704... > ... Numeric greater than.
7705... >= ... Numeric greater than or equal to.
7706... >> ... Bitwise shift right.
7707... >>= ... Bitwise shift right assignment.
7708... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
7709?PATTERN? One-time pattern match.
7710@ARGV Command line arguments (not including the command name - see $0).
7711@INC List of places to look for perl scripts during do/include/use.
7712@_ Parameter array for subroutines; result of split() unless in list context.
7713\\ Creates reference to what follows, like \\$var, or quotes non-\\w in strings.
7714\\0 Octal char, e.g. \\033.
7715\\E Case modification terminator. See \\Q, \\L, and \\U.
7716\\L Lowercase until \\E . See also \\l, lc.
7717\\U Upcase until \\E . See also \\u, uc.
7718\\Q Quote metacharacters until \\E . See also quotemeta.
7719\\a Alarm character (octal 007).
7720\\b Backspace character (octal 010).
7721\\c Control character, e.g. \\c[ .
7722\\e Escape character (octal 033).
7723\\f Formfeed character (octal 014).
7724\\l Lowercase the next character. See also \\L and \\u, lcfirst.
7725\\n Newline character (octal 012 on most systems).
7726\\r Return character (octal 015 on most systems).
7727\\t Tab character (octal 011).
7728\\u Upcase the next character. See also \\U and \\l, ucfirst.
7729\\x Hex character, e.g. \\x1b.
7730... ^ ... Bitwise exclusive or.
7731__END__ Ends program source.
7732__DATA__ Ends program source.
7733__FILE__ Current (source) filename.
7734__LINE__ Current line in current source.
7735__PACKAGE__ Current package.
7736ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
7737ARGVOUT Output filehandle with -i flag.
7738BEGIN { ... } Immediately executed (during compilation) piece of code.
7739END { ... } Pseudo-subroutine executed after the script finishes.
7740CHECK { ... } Pseudo-subroutine executed after the script is compiled.
7741INIT { ... } Pseudo-subroutine executed before the script starts running.
7742DATA Input filehandle for what follows after __END__ or __DATA__.
7743accept(NEWSOCKET,GENERICSOCKET)
7744alarm(SECONDS)
7745atan2(X,Y)
7746bind(SOCKET,NAME)
7747binmode(FILEHANDLE)
7748caller[(LEVEL)]
7749chdir(EXPR)
7750chmod(LIST)
7751chop[(LIST|VAR)]
7752chown(LIST)
7753chroot(FILENAME)
7754close(FILEHANDLE)
7755closedir(DIRHANDLE)
7756... cmp ... String compare.
7757connect(SOCKET,NAME)
7758continue of { block } continue { block }. Is executed after `next' or at end.
7759cos(EXPR)
7760crypt(PLAINTEXT,SALT)
7761dbmclose(%HASH)
7762dbmopen(%HASH,DBNAME,MODE)
7763defined(EXPR)
7764delete($HASH{KEY})
7765die(LIST)
7766do { ... }|SUBR while|until EXPR executes at least once
7767do(EXPR|SUBR([LIST])) (with while|until executes at least once)
7768dump LABEL
7769each(%HASH)
7770endgrent
7771endhostent
7772endnetent
7773endprotoent
7774endpwent
7775endservent
7776eof[([FILEHANDLE])]
7777... eq ... String equality.
7778eval(EXPR) or eval { BLOCK }
7779exec([TRUENAME] ARGV0, ARGVs) or exec(SHELL_COMMAND_LINE)
7780exit(EXPR)
7781exp(EXPR)
7782fcntl(FILEHANDLE,FUNCTION,SCALAR)
7783fileno(FILEHANDLE)
7784flock(FILEHANDLE,OPERATION)
7785for (EXPR;EXPR;EXPR) { ... }
7786foreach [VAR] (@ARRAY) { ... }
7787fork
7788... ge ... String greater than or equal.
7789getc[(FILEHANDLE)]
7790getgrent
7791getgrgid(GID)
7792getgrnam(NAME)
7793gethostbyaddr(ADDR,ADDRTYPE)
7794gethostbyname(NAME)
7795gethostent
7796getlogin
7797getnetbyaddr(ADDR,ADDRTYPE)
7798getnetbyname(NAME)
7799getnetent
7800getpeername(SOCKET)
7801getpgrp(PID)
7802getppid
7803getpriority(WHICH,WHO)
7804getprotobyname(NAME)
7805getprotobynumber(NUMBER)
7806getprotoent
7807getpwent
7808getpwnam(NAME)
7809getpwuid(UID)
7810getservbyname(NAME,PROTO)
7811getservbyport(PORT,PROTO)
7812getservent
7813getsockname(SOCKET)
7814getsockopt(SOCKET,LEVEL,OPTNAME)
7815gmtime(EXPR)
7816goto LABEL
7817... gt ... String greater than.
7818hex(EXPR)
7819if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
7820index(STR,SUBSTR[,OFFSET])
7821int(EXPR)
7822ioctl(FILEHANDLE,FUNCTION,SCALAR)
7823join(EXPR,LIST)
7824keys(%HASH)
7825kill(LIST)
7826last [LABEL]
7827... le ... String less than or equal.
7828length(EXPR)
7829link(OLDFILE,NEWFILE)
7830listen(SOCKET,QUEUESIZE)
7831local(LIST)
7832localtime(EXPR)
7833log(EXPR)
7834lstat(EXPR|FILEHANDLE|VAR)
7835... lt ... String less than.
7836m/PATTERN/iogsmx
7837mkdir(FILENAME,MODE)
7838msgctl(ID,CMD,ARG)
7839msgget(KEY,FLAGS)
7840msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
7841msgsnd(ID,MSG,FLAGS)
7842my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
7843our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
7844... ne ... String inequality.
7845next [LABEL]
7846oct(EXPR)
7847open(FILEHANDLE[,EXPR])
7848opendir(DIRHANDLE,EXPR)
7849ord(EXPR) ASCII value of the first char of the string.
7850pack(TEMPLATE,LIST)
7851package NAME Introduces package context.
7852pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
7853pop(ARRAY)
7854print [FILEHANDLE] [(LIST)]
7855printf [FILEHANDLE] (FORMAT,LIST)
7856push(ARRAY,LIST)
7857q/STRING/ Synonym for 'STRING'
7858qq/STRING/ Synonym for \"STRING\"
7859qx/STRING/ Synonym for `STRING`
7860rand[(EXPR)]
7861read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7862readdir(DIRHANDLE)
7863readlink(EXPR)
7864recv(SOCKET,SCALAR,LEN,FLAGS)
7865redo [LABEL]
7866rename(OLDNAME,NEWNAME)
7867require [FILENAME | PERL_VERSION]
7868reset[(EXPR)]
7869return(LIST)
7870reverse(LIST)
7871rewinddir(DIRHANDLE)
7872rindex(STR,SUBSTR[,OFFSET])
7873rmdir(FILENAME)
7874s/PATTERN/REPLACEMENT/gieoxsm
7875scalar(EXPR)
7876seek(FILEHANDLE,POSITION,WHENCE)
7877seekdir(DIRHANDLE,POS)
7878select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
7879semctl(ID,SEMNUM,CMD,ARG)
7880semget(KEY,NSEMS,SIZE,FLAGS)
7881semop(KEY,...)
7882send(SOCKET,MSG,FLAGS[,TO])
7883setgrent
7884sethostent(STAYOPEN)
7885setnetent(STAYOPEN)
7886setpgrp(PID,PGRP)
7887setpriority(WHICH,WHO,PRIORITY)
7888setprotoent(STAYOPEN)
7889setpwent
7890setservent(STAYOPEN)
7891setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
7892shift[(ARRAY)]
7893shmctl(ID,CMD,ARG)
7894shmget(KEY,SIZE,FLAGS)
7895shmread(ID,VAR,POS,SIZE)
7896shmwrite(ID,STRING,POS,SIZE)
7897shutdown(SOCKET,HOW)
7898sin(EXPR)
7899sleep[(EXPR)]
7900socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
7901socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
7902sort [SUBROUTINE] (LIST)
7903splice(ARRAY,OFFSET[,LENGTH[,LIST]])
7904split[(/PATTERN/[,EXPR[,LIMIT]])]
7905sprintf(FORMAT,LIST)
7906sqrt(EXPR)
7907srand(EXPR)
7908stat(EXPR|FILEHANDLE|VAR)
7909study[(SCALAR)]
7910sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
7911substr(EXPR,OFFSET[,LEN])
7912symlink(OLDFILE,NEWFILE)
7913syscall(LIST)
7914sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7915system([TRUENAME] ARGV0 [,ARGV]) or system(SHELL_COMMAND_LINE)
7916syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
7917tell[(FILEHANDLE)]
7918telldir(DIRHANDLE)
7919time
7920times
7921tr/SEARCHLIST/REPLACEMENTLIST/cds
7922truncate(FILE|EXPR,LENGTH)
7923umask[(EXPR)]
7924undef[(EXPR)]
7925unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
7926unlink(LIST)
7927unpack(TEMPLATE,EXPR)
7928unshift(ARRAY,LIST)
7929until (EXPR) { ... } EXPR until EXPR
7930utime(LIST)
7931values(%HASH)
7932vec(EXPR,OFFSET,BITS)
7933wait
7934waitpid(PID,FLAGS)
7935wantarray Returns true if the sub/eval is called in list context.
7936warn(LIST)
7937while (EXPR) { ... } EXPR while EXPR
7938write[(EXPR|FILEHANDLE)]
7939... x ... Repeat string or array.
7940x= ... Repetition assignment.
7941y/SEARCHLIST/REPLACEMENTLIST/
7942... | ... Bitwise or.
7943... || ... Logical or.
7944~ ... Unary bitwise complement.
7945#! OS interpreter indicator. If contains `perl', used for options, and -x.
7946AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
7947CORE:: Prefix to access builtin function if imported sub obscures it.
7948SUPER:: Prefix to lookup for a method in @ISA classes.
7949DESTROY Shorthand for `sub DESTROY {...}'.
7950... EQ ... Obsolete synonym of `eq'.
7951... GE ... Obsolete synonym of `ge'.
7952... GT ... Obsolete synonym of `gt'.
7953... LE ... Obsolete synonym of `le'.
7954... LT ... Obsolete synonym of `lt'.
7955... NE ... Obsolete synonym of `ne'.
7956abs [ EXPR ] absolute value
7957... and ... Low-precedence synonym for &&.
7958bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
7959chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
7960chr Converts a number to char with the same ordinal.
7961else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7962elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
7963exists $HASH{KEY} True if the key exists.
7964format [NAME] = Start of output format. Ended by a single dot (.) on a line.
7965formline PICTURE, LIST Backdoor into \"format\" processing.
7966glob EXPR Synonym of <EXPR>.
7967lc [ EXPR ] Returns lowercased EXPR.
7968lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
7969grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
7970map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
7971no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
7972not ... Low-precedence synonym for ! - negation.
7973... or ... Low-precedence synonym for ||.
7974pos STRING Set/Get end-position of the last match over this string, see \\G.
7975quotemeta [ EXPR ] Quote regexp metacharacters.
7976qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
7977readline FH Synonym of <FH>.
7978readpipe CMD Synonym of `CMD`.
7979ref [ EXPR ] Type of EXPR when dereferenced.
7980sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
7981tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
7982tied Returns internal object for a tied data.
7983uc [ EXPR ] Returns upcased EXPR.
7984ucfirst [ EXPR ] Returns EXPR with upcased first letter.
7985untie VAR Unlink an object from a simple Perl variable.
7986use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
7987... xor ... Low-precedence synonym for exclusive or.
7988prototype \\&SUB Returns prototype of the function given a reference.
7989=head1 Top-level heading.
7990=head2 Second-level heading.
7991=head3 Third-level heading (is there such?).
7992=over [ NUMBER ] Start list.
7993=item [ TITLE ] Start new item in the list.
7994=back End list.
7995=cut Switch from POD to Perl.
7996=pod Switch from Perl to POD.
7997")
7998
7999(defun cperl-switch-to-doc-buffer (&optional interactive)
8000 "Go to the perl documentation buffer and insert the documentation."
8001 (interactive "p")
8002 (let ((buf (get-buffer-create cperl-doc-buffer)))
8003 (if interactive
8004 (switch-to-buffer-other-window buf)
8005 (set-buffer buf))
8006 (if (= (buffer-size) 0)
8007 (progn
8008 (insert (documentation-property 'cperl-short-docs
8009 'variable-documentation))
8010 (setq buffer-read-only t)))))
8011
8012(defun cperl-beautify-regexp-piece (b e embed level)
8013 ;; b is before the starting delimiter, e before the ending
8014 ;; e should be a marker, may be changed, but remains "correct".
8015 ;; EMBED is nil if we process the whole REx.
8016 ;; The REx is guaranteed to have //x
8017 ;; LEVEL shows how many levels deep to go
8018 ;; position at enter and at leave is not defined
8019 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
8020 (if embed
8021 (progn
8022 (goto-char b)
8023 (setq c (if (eq embed t) (current-indentation) (current-column)))
8024 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
8025 (forward-char 2)
8026 (delete-char 1)
8027 (forward-char 1))
8028 ((looking-at "(\\?[^a-zA-Z]")
8029 (forward-char 3))
8030 ((looking-at "(\\?") ; (?i)
8031 (forward-char 2))
8032 (t
8033 (forward-char 1))))
8034 (goto-char (1+ b))
8035 (setq c (1- (current-column))))
8036 (setq c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
8037 (or (looking-at "[ \t]*[\n#]")
8038 (progn
8039 (insert "\n")))
8040 (goto-char e)
8041 (beginning-of-line)
8042 (if (re-search-forward "[^ \t]" e t)
8043 (progn ; Something before the ending delimiter
8044 (goto-char e)
8045 (delete-horizontal-space)
8046 (insert "\n")
8047 (cperl-make-indent c)
8048 (set-marker e (point))))
8049 (goto-char b)
8050 (end-of-line 2)
8051 (while (< (point) (marker-position e))
8052 (beginning-of-line)
8053 (setq s (point)
8054 inline t)
8055 (skip-chars-forward " \t")
8056 (delete-region s (point))
8057 (cperl-make-indent c1)
8058 (while (and
8059 inline
8060 (looking-at
8061 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
8062 "\\|" ; Embedded variable
8063 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
8064 "\\|" ; $ ^
8065 "[$^]"
8066 "\\|" ; simple-code simple-code*?
8067 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
8068 "\\|" ; Class
8069 "\\(\\[\\)" ; 6
8070 "\\|" ; Grouping
8071 "\\((\\(\\?\\)?\\)" ; 7 8
8072 "\\|" ; |
8073 "\\(|\\)"))) ; 9
8074 (goto-char (match-end 0))
8075 (setq spaces t)
8076 (cond ((match-beginning 1) ; Alphanum word + junk
8077 (forward-char -1))
8078 ((or (match-beginning 3) ; $ab[12]
8079 (and (match-beginning 5) ; X* X+ X{2,3}
8080 (eq (preceding-char) ?\{)))
8081 (forward-char -1)
8082 (forward-sexp 1))
8083 ((and ; [], already syntaxified
8084 (match-beginning 6)
8085 cperl-regexp-scan
8086 cperl-use-syntax-table-text-property)
8087 (forward-char -1)
8088 (forward-sexp 1)
8089 (or (eq (preceding-char) ?\])
8090 (error "[]-group not terminated"))
8091 (re-search-forward
8092 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8093 ((match-beginning 6) ; []
8094 (setq tmp (point))
8095 (if (looking-at "\\^?\\]")
8096 (goto-char (match-end 0)))
8097 ;; XXXX POSIX classes?!
8098 (while (and (not pos)
8099 (re-search-forward "\\[:\\|\\]" e t))
8100 (if (eq (preceding-char) ?:)
8101 (or (re-search-forward ":\\]" e t)
8102 (error "[:POSIX:]-group in []-group not terminated"))
8103 (setq pos t)))
8104 (or (eq (preceding-char) ?\])
8105 (error "[]-group not terminated"))
8106 (re-search-forward
8107 "\\=\\([*+?]\\|{[0-9]+\\(,[0-9]*\\)?}\\)\\??" e t))
8108 ((match-beginning 7) ; ()
8109 (goto-char (match-beginning 0))
8110 (setq pos (current-column))
8111 (or (eq pos c1)
8112 (progn
8113 (delete-horizontal-space)
8114 (insert "\n")
8115 (cperl-make-indent c1)))
8116 (setq tmp (point))
8117 (forward-sexp 1)
8118 ;; (or (forward-sexp 1)
8119 ;; (progn
8120 ;; (goto-char tmp)
8121 ;; (error "()-group not terminated")))
8122 (set-marker m (1- (point)))
8123 (set-marker m1 (point))
8124 (if (= level 1)
8125 (if (progn ; indent rigidly if multiline
8126 ;; In fact does not make a lot of sense, since
8127 ;; the starting position can be already lost due
8128 ;; to insertion of "\n" and " "
8129 (goto-char tmp)
8130 (search-forward "\n" m1 t))
8131 (indent-rigidly (point) m1 (- c1 pos)))
8132 (setq level (1- level))
8133 (cond
8134 ((not (match-beginning 8))
8135 (cperl-beautify-regexp-piece tmp m t level))
8136 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
8137 t)
8138 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
8139 (goto-char (+ 2 tmp))
8140 (forward-sexp 1)
8141 (cperl-beautify-regexp-piece (point) m t level))
8142 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
8143 (goto-char (+ 3 tmp))
8144 (cperl-beautify-regexp-piece (point) m t level))
8145 (t
8146 (cperl-beautify-regexp-piece tmp m t level))))
8147 (goto-char m1)
8148 (cond ((looking-at "[*+?]\\??")
8149 (goto-char (match-end 0)))
8150 ((eq (following-char) ?\{)
8151 (forward-sexp 1)
8152 (if (eq (following-char) ?\?)
8153 (forward-char))))
8154 (skip-chars-forward " \t")
8155 (setq spaces nil)
8156 (if (looking-at "[#\n]")
8157 (progn
8158 (or (eolp) (indent-for-comment))
8159 (beginning-of-line 2))
8160 (delete-horizontal-space)
8161 (insert "\n"))
8162 (end-of-line)
8163 (setq inline nil))
8164 ((match-beginning 9) ; |
8165 (forward-char -1)
8166 (setq tmp (point))
8167 (beginning-of-line)
8168 (if (re-search-forward "[^ \t]" tmp t)
8169 (progn
8170 (goto-char tmp)
8171 (delete-horizontal-space)
8172 (insert "\n"))
8173 ;; first at line
8174 (delete-region (point) tmp))
8175 (cperl-make-indent c)
8176 (forward-char 1)
8177 (skip-chars-forward " \t")
8178 (setq spaces nil)
8179 (if (looking-at "[#\n]")
8180 (beginning-of-line 2)
8181 (delete-horizontal-space)
8182 (insert "\n"))
8183 (end-of-line)
8184 (setq inline nil)))
8185 (or (looking-at "[ \t\n]")
8186 (not spaces)
8187 (insert " "))
8188 (skip-chars-forward " \t"))
8189 (or (looking-at "[#\n]")
8190 (error "Unknown code `%s' in a regexp"
8191 (buffer-substring (point) (1+ (point)))))
8192 (and inline (end-of-line 2)))
8193 ;; Special-case the last line of group
8194 (if (and (>= (point) (marker-position e))
8195 (/= (current-indentation) c))
8196 (progn
8197 (beginning-of-line)
8198 (cperl-make-indent c)))))
8199
8200(defun cperl-make-regexp-x ()
8201 ;; Returns position of the start
8202 ;; XXX this is called too often! Need to cache the result!
8203 (save-excursion
8204 (or cperl-use-syntax-table-text-property
8205 (error "I need to have a regexp marked!"))
8206 ;; Find the start
8207 (if (looking-at "\\s|")
8208 nil ; good already
8209 (if (or (looking-at "\\([smy]\\|qr\\)\\s|")
8210 (and (eq (preceding-char) ?q)
8211 (looking-at "\\(r\\)\\s|")))
8212 (goto-char (match-end 1))
8213 (re-search-backward "\\s|"))) ; Assume it is scanned already.
8214 ;;(forward-char 1)
8215 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
8216 (sub-p (eq (preceding-char) ?s)) s)
8217 (forward-sexp 1)
8218 (set-marker e (1- (point)))
8219 (setq delim (preceding-char))
8220 (if (and sub-p (eq delim (char-after (- (point) 2))))
8221 (error "Possible s/blah// - do not know how to deal with"))
8222 (if sub-p (forward-sexp 1))
8223 (if (looking-at "\\sw*x")
8224 (setq have-x t)
8225 (insert "x"))
8226 ;; Protect fragile " ", "#"
8227 (if have-x nil
8228 (goto-char (1+ b))
8229 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
8230 (forward-char -1)
8231 (insert "\\")
8232 (forward-char 1)))
8233 b)))
8234
8235(defun cperl-beautify-regexp (&optional deep)
8236 "Do it. (Experimental, may change semantics, recheck the result.)
8237We suppose that the regexp is scanned already."
8238 (interactive "P")
8239 (setq deep (if deep (prefix-numeric-value deep) -1))
8240 (save-excursion
8241 (goto-char (cperl-make-regexp-x))
8242 (let ((b (point)) (e (make-marker)))
8243 (forward-sexp 1)
8244 (set-marker e (1- (point)))
8245 (cperl-beautify-regexp-piece b e nil deep))))
8246
8247(defun cperl-regext-to-level-start ()
8248 "Goto start of an enclosing group in regexp.
8249We suppose that the regexp is scanned already."
8250 (interactive)
8251 (let ((limit (cperl-make-regexp-x)) done)
8252 (while (not done)
8253 (or (eq (following-char) ?\()
8254 (search-backward "(" (1+ limit) t)
8255 (error "Cannot find `(' which starts a group"))
8256 (setq done
8257 (save-excursion
8258 (skip-chars-backward "\\")
8259 (looking-at "\\(\\\\\\\\\\)*(")))
8260 (or done (forward-char -1)))))
8261
8262(defun cperl-contract-level ()
8263 "Find an enclosing group in regexp and contract it.
8264\(Experimental, may change semantics, recheck the result.)
8265We suppose that the regexp is scanned already."
8266 (interactive)
8267 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
8268 (cperl-regext-to-level-start)
8269 (let ((b (point)) (e (make-marker)) c)
8270 (forward-sexp 1)
8271 (set-marker e (1- (point)))
8272 (goto-char b)
8273 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
8274 (cond
8275 ((match-beginning 1) ; #-comment
8276 (or c (setq c (current-indentation)))
8277 (beginning-of-line 2) ; Skip
8278 (cperl-make-indent c))
8279 (t
8280 (delete-char -1)
8281 (just-one-space))))))
8282
8283(defun cperl-contract-levels ()
8284 "Find an enclosing group in regexp and contract all the kids.
8285\(Experimental, may change semantics, recheck the result.)
8286We suppose that the regexp is scanned already."
8287 (interactive)
8288 (save-excursion
8289 (condition-case nil
8290 (cperl-regext-to-level-start)
8291 (error ; We are outside outermost group
8292 (goto-char (cperl-make-regexp-x))))
8293 (let ((b (point)) (e (make-marker)) s c)
8294 (forward-sexp 1)
8295 (set-marker e (1- (point)))
8296 (goto-char (1+ b))
8297 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
8298 (cond
8299 ((match-beginning 1) ; Skip
8300 nil)
8301 (t ; Group
8302 (cperl-contract-level)))))))
8303
8304(defun cperl-beautify-level (&optional deep)
8305 "Find an enclosing group in regexp and beautify it.
8306\(Experimental, may change semantics, recheck the result.)
8307We suppose that the regexp is scanned already."
8308 (interactive "P")
8309 (setq deep (if deep (prefix-numeric-value deep) -1))
8310 (save-excursion
8311 (cperl-regext-to-level-start)
8312 (let ((b (point)) (e (make-marker)))
8313 (forward-sexp 1)
8314 (set-marker e (1- (point)))
8315 (cperl-beautify-regexp-piece b e 'level deep))))
8316
8317(defun cperl-invert-if-unless-modifiers ()
8318 "Change `B if A;' into `if (A) {B}' etc if possible.
8319\(Unfinished.)"
8320 (interactive)
8321 (let (A B pre-B post-B pre-if post-if pre-A post-A if-string
8322 (w-rex "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>"))
8323 (and (= (char-syntax (preceding-char)) ?w)
8324 (forward-sexp -1))
8325 (setq pre-if (point))
8326 (cperl-backward-to-start-of-expr)
8327 (setq pre-B (point))
8328 (forward-sexp 1) ; otherwise forward-to-end-of-expr is NOP
8329 (cperl-forward-to-end-of-expr)
8330 (setq post-A (point))
8331 (goto-char pre-if)
8332 (or (looking-at w-rex)
8333 ;; Find the position
8334 (progn (goto-char post-A)
8335 (while (and
8336 (not (looking-at w-rex))
8337 (> (point) pre-B))
8338 (forward-sexp -1))
8339 (setq pre-if (point))))
8340 (or (looking-at w-rex)
8341 (error "Can't find `if', `unless', `while', `until', `for' or `foreach'"))
8342 ;; 1 B 2 ... 3 B-com ... 4 if 5 ... if-com 6 ... 7 A 8
8343 (setq if-string (buffer-substring (match-beginning 0) (match-end 0)))
8344 ;; First, simple part: find code boundaries
8345 (forward-sexp 1)
8346 (setq post-if (point))
8347 (forward-sexp -2)
8348 (forward-sexp 1)
8349 (setq post-B (point))
8350 (cperl-backward-to-start-of-expr)
8351 (setq pre-B (point))
8352 (setq B (buffer-substring pre-B post-B))
8353 (goto-char pre-if)
8354 (forward-sexp 2)
8355 (forward-sexp -1)
8356 ;; May be after $, @, $# etc of a variable
8357 (skip-chars-backward "$@%#")
8358 (setq pre-A (point))
8359 (cperl-forward-to-end-of-expr)
8360 (setq post-A (point))
8361 (setq A (buffer-substring pre-A post-A))
8362 ;; Now modify (from end, to not break the stuff)
8363 (skip-chars-forward " \t;")
8364 (delete-region pre-A (point)) ; we move to pre-A
8365 (insert "\n" B ";\n}")
8366 (and (looking-at "[ \t]*#") (cperl-indent-for-comment))
8367 (delete-region pre-if post-if)
8368 (delete-region pre-B post-B)
8369 (goto-char pre-B)
8370 (insert if-string " (" A ") {")
8371 (setq post-B (point))
8372 (if (looking-at "[ \t]+$")
8373 (delete-horizontal-space)
8374 (if (looking-at "[ \t]*#")
8375 (cperl-indent-for-comment)
8376 (just-one-space)))
8377 (forward-line 1)
8378 (if (looking-at "[ \t]*$")
8379 (progn ; delete line
8380 (delete-horizontal-space)
8381 (delete-region (point) (1+ (point)))))
8382 (cperl-indent-line)
8383 (goto-char (1- post-B))
8384 (forward-sexp 1)
8385 (cperl-indent-line)
8386 (goto-char pre-B)))
8387
8388(defun cperl-invert-if-unless ()
8389 "Change `if (A) {B}' into `B if A;' etc (or visa versa) if possible.
8390If the cursor is not on the leading keyword of the BLOCK flavor of
8391construct, will assume it is the STATEMENT flavor, so will try to find
8392the appropriate statement modifier."
8393 (interactive)
8394 (and (= (char-syntax (preceding-char)) ?w)
8395 (forward-sexp -1))
8396 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
8397 (let ((pre-if (point))
8398 pre-A post-A pre-B post-B A B state p end-B-code is-block B-comment
8399 (if-string (buffer-substring (match-beginning 0) (match-end 0))))
8400 (forward-sexp 2)
8401 (setq post-A (point))
8402 (forward-sexp -1)
8403 (setq pre-A (point))
8404 (setq is-block (and (eq (following-char) ?\( )
8405 (save-excursion
8406 (condition-case nil
8407 (progn
8408 (forward-sexp 2)
8409 (forward-sexp -1)
8410 (eq (following-char) ?\{ ))
8411 (error nil)))))
8412 (if is-block
8413 (progn
8414 (goto-char post-A)
8415 (forward-sexp 1)
8416 (setq post-B (point))
8417 (forward-sexp -1)
8418 (setq pre-B (point))
8419 (if (and (eq (following-char) ?\{ )
8420 (progn
8421 (cperl-backward-to-noncomment post-A)
8422 (eq (preceding-char) ?\) )))
8423 (if (condition-case nil
8424 (progn
8425 (goto-char post-B)
8426 (forward-sexp 1)
8427 (forward-sexp -1)
8428 (looking-at "\\<els\\(e\\|if\\)\\>"))
8429 (error nil))
8430 (error
8431 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" if-string)
8432 (goto-char (1- post-B))
8433 (cperl-backward-to-noncomment pre-B)
8434 (if (eq (preceding-char) ?\;)
8435 (forward-char -1))
8436 (setq end-B-code (point))
8437 (goto-char pre-B)
8438 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" end-B-code t)
8439 (setq p (match-beginning 0)
8440 A (buffer-substring p (match-end 0))
8441 state (parse-partial-sexp pre-B p))
8442 (or (nth 3 state)
8443 (nth 4 state)
8444 (nth 5 state)
8445 (error "`%s' inside `%s' BLOCK" A if-string))
8446 (goto-char (match-end 0)))
8447 ;; Finally got it
8448 (goto-char (1+ pre-B))
8449 (skip-chars-forward " \t\n")
8450 (setq B (buffer-substring (point) end-B-code))
8451 (goto-char end-B-code)
8452 (or (looking-at ";?[ \t\n]*}")
8453 (progn
8454 (skip-chars-forward "; \t\n")
8455 (setq B-comment
8456 (buffer-substring (point) (1- post-B)))))
8457 (and (equal B "")
8458 (setq B "1"))
8459 (goto-char (1- post-A))
8460 (cperl-backward-to-noncomment pre-A)
8461 (or (looking-at "[ \t\n]*)")
8462 (goto-char (1- post-A)))
8463 (setq p (point))
8464 (goto-char (1+ pre-A))
8465 (skip-chars-forward " \t\n")
8466 (setq A (buffer-substring (point) p))
8467 (delete-region pre-B post-B)
8468 (delete-region pre-A post-A)
8469 (goto-char pre-if)
8470 (insert B " ")
8471 (and B-comment (insert B-comment " "))
8472 (just-one-space)
8473 (forward-word 1)
8474 (setq pre-A (point))
8475 (insert " " A ";")
8476 (delete-horizontal-space)
8477 (setq post-B (point))
8478 (if (looking-at "#")
8479 (indent-for-comment))
8480 (goto-char post-B)
8481 (forward-char -1)
8482 (delete-horizontal-space)
8483 (goto-char pre-A)
8484 (just-one-space)
8485 (goto-char pre-if)
8486 (setq pre-A (set-marker (make-marker) pre-A))
8487 (while (<= (point) (marker-position pre-A))
8488 (cperl-indent-line)
8489 (forward-line 1))
8490 (goto-char (marker-position pre-A))
8491 (if B-comment
8492 (progn
8493 (forward-line -1)
8494 (indent-for-comment)
8495 (goto-char (marker-position pre-A)))))
8496 (error "`%s' (EXPR) not with an {BLOCK}" if-string)))
8497 ;; (error "`%s' not with an (EXPR)" if-string)
8498 (forward-sexp -1)
8499 (cperl-invert-if-unless-modifiers)))
8500 ;;(error "Not at `if', `unless', `while', `until', `for' or `foreach'")
8501 (cperl-invert-if-unless-modifiers)))
8502
8503;;; By Anthony Foiani <afoiani@uswest.com>
8504;;; Getting help on modules in C-h f ?
8505;;; This is a modified version of `man'.
8506;;; Need to teach it how to lookup functions
8507;;;###autoload
8508(defun cperl-perldoc (word)
8509 "Run `perldoc' on WORD."
8510 (interactive
8511 (list (let* ((default-entry (cperl-word-at-point))
8512 (input (read-string
8513 (format "perldoc entry%s: "
8514 (if (string= default-entry "")
8515 ""
8516 (format " (default %s)" default-entry))))))
8517 (if (string= input "")
8518 (if (string= default-entry "")
8519 (error "No perldoc args given")
8520 default-entry)
8521 input))))
8522 (require 'man)
8523 (let* ((case-fold-search nil)
8524 (is-func (and
8525 (string-match "^[a-z]+$" word)
8526 (string-match (concat "^" word "\\>")
8527 (documentation-property
8528 'cperl-short-docs
8529 'variable-documentation))))
8530 (Man-switches "")
8531 (manual-program (if is-func "perldoc -f" "perldoc")))
8532 (cond
8533 ((featurep 'xemacs)
8534 (let ((Manual-program "perldoc")
8535 (Manual-switches (if is-func (list "-f"))))
8536 (manual-entry word)))
8537 (t
8538 (Man-getpage-in-background word)))))
8539
8540;;;###autoload
8541(defun cperl-perldoc-at-point ()
8542 "Run a `perldoc' on the word around point."
8543 (interactive)
8544 (cperl-perldoc (cperl-word-at-point)))
8545
8546(defcustom pod2man-program "pod2man"
8547 "*File name for `pod2man'."
8548 :type 'file
8549 :group 'cperl)
8550
8551;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
8552(defun cperl-pod-to-manpage ()
8553 "Create a virtual manpage in Emacs from the Perl Online Documentation."
8554 (interactive)
8555 (require 'man)
8556 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
8557 (bufname (concat "Man " buffer-file-name))
8558 (buffer (generate-new-buffer bufname)))
8559 (save-excursion
8560 (set-buffer buffer)
8561 (let ((process-environment (copy-sequence process-environment)))
8562 ;; Prevent any attempt to use display terminal fanciness.
8563 (setenv "TERM" "dumb")
8564 (set-process-sentinel
8565 (start-process pod2man-program buffer "sh" "-c"
8566 (format (cperl-pod2man-build-command) pod2man-args))
8567 'Man-bgproc-sentinel)))))
8568
8569;;; Updated version by him too
8570(defun cperl-build-manpage ()
8571 "Create a virtual manpage in Emacs from the POD in the file."
8572 (interactive)
8573 (require 'man)
8574 (cond
8575 ((featurep 'xemacs)
8576 (let ((Manual-program "perldoc"))
8577 (manual-entry buffer-file-name)))
8578 (t
8579 (let* ((manual-program "perldoc")
8580 (Man-switches ""))
8581 (Man-getpage-in-background buffer-file-name)))))
8582
8583(defun cperl-pod2man-build-command ()
8584 "Builds the entire background manpage and cleaning command."
8585 (let ((command (concat pod2man-program " %s 2>/dev/null"))
8586 (flist (and (boundp 'Man-filter-list) Man-filter-list)))
8587 (while (and flist (car flist))
8588 (let ((pcom (car (car flist)))
8589 (pargs (cdr (car flist))))
8590 (setq command
8591 (concat command " | " pcom " "
8592 (mapconcat '(lambda (phrase)
8593 (if (not (stringp phrase))
8594 (error "Malformed Man-filter-list"))
8595 phrase)
8596 pargs " ")))
8597 (setq flist (cdr flist))))
8598 command))
8599
8600
8601(defun cperl-next-interpolated-REx-1 ()
8602 "Move point to next REx which has interpolated parts without //o.
8603Skips RExes consisting of one interpolated variable.
8604
8605Note that skipped RExen are not performance hits."
8606 (interactive "")
8607 (cperl-next-interpolated-REx 1))
8608
8609(defun cperl-next-interpolated-REx-0 ()
8610 "Move point to next REx which has interpolated parts without //o."
8611 (interactive "")
8612 (cperl-next-interpolated-REx 0))
8613
8614(defun cperl-next-interpolated-REx (&optional skip beg limit)
8615 "Move point to next REx which has interpolated parts.
8616SKIP is a list of possible types to skip, BEG and LIMIT are the starting
8617point and the limit of search (default to point and end of buffer).
8618
8619SKIP may be a number, then it behaves as list of numbers up to SKIP; this
8620semantic may be used as a numeric argument.
8621
8622Types are 0 for / $rex /o (interpolated once), 1 for /$rex/ (if $rex is
8623a result of qr//, this is not a performance hit), t for the rest."
8624 (interactive "P")
8625 (if (numberp skip) (setq skip (list 0 skip)))
8626 (or beg (setq beg (point)))
8627 (or limit (setq limit (point-max))) ; needed for n-s-p-c
8628 (let (pp)
8629 (and (eq (get-text-property beg 'syntax-type) 'string)
8630 (setq beg (next-single-property-change beg 'syntax-type nil limit)))
8631 (cperl-map-pods-heres
8632 (function (lambda (s e p)
8633 (if (memq (get-text-property s 'REx-interpolated) skip)
8634 t
8635 (setq pp s)
8636 nil))) ; nil stops
8637 'REx-interpolated beg limit)
8638 (if pp (goto-char pp)
8639 (message "No more interpolated REx"))))
8640
8641;;; Initial version contributed by Trey Belew
8642(defun cperl-here-doc-spell (&optional beg end)
8643 "Spell-check HERE-documents in the Perl buffer.
8644If a region is highlighted, restricts to the region."
8645 (interactive "")
8646 (cperl-pod-spell t beg end))
8647
8648(defun cperl-pod-spell (&optional do-heres beg end)
8649 "Spell-check POD documentation.
8650If invoked with prefix argument, will do HERE-DOCs instead.
8651If a region is highlighted, restricts to the region."
8652 (interactive "P")
8653 (save-excursion
8654 (let (beg end)
8655 (if (cperl-mark-active)
8656 (setq beg (min (mark) (point))
8657 end (max (mark) (point)))
8658 (setq beg (point-min)
8659 end (point-max)))
8660 (cperl-map-pods-heres (function
8661 (lambda (s e p)
8662 (if do-heres
8663 (setq e (save-excursion
8664 (goto-char e)
8665 (forward-line -1)
8666 (point))))
8667 (ispell-region s e)
8668 t))
8669 (if do-heres 'here-doc-group 'in-pod)
8670 beg end))))
8671
8672(defun cperl-map-pods-heres (func &optional prop s end)
8673 "Executes a function over regions of pods or here-documents.
8674PROP is the text-property to search for; default to `in-pod'. Stop when
8675function returns nil."
8676 (let (pos posend has-prop (cont t))
8677 (or prop (setq prop 'in-pod))
8678 (or s (setq s (point-min)))
8679 (or end (setq end (point-max)))
8680 (cperl-update-syntaxification end end)
8681 (save-excursion
8682 (goto-char (setq pos s))
8683 (while (and cont (< pos end))
8684 (setq has-prop (get-text-property pos prop))
8685 (setq posend (next-single-property-change pos prop nil end))
8686 (and has-prop
8687 (setq cont (funcall func pos posend prop)))
8688 (setq pos posend)))))
8689
8690;;; Based on code by Masatake YAMATO:
8691(defun cperl-get-here-doc-region (&optional pos pod)
8692 "Return HERE document region around the point.
8693Return nil if the point is not in a HERE document region. If POD is non-nil,
8694will return a POD section if point is in a POD section."
8695 (or pos (setq pos (point)))
8696 (cperl-update-syntaxification pos pos)
8697 (if (or (eq 'here-doc (get-text-property pos 'syntax-type))
8698 (and pod
8699 (eq 'pod (get-text-property pos 'syntax-type))))
8700 (let ((b (cperl-beginning-of-property pos 'syntax-type))
8701 (e (next-single-property-change pos 'syntax-type)))
8702 (cons b (or e (point-max))))))
8703
8704(defun cperl-narrow-to-here-doc (&optional pos)
8705 "Narrows editing region to the HERE-DOC at POS.
8706POS defaults to the point."
8707 (interactive "d")
8708 (or pos (setq pos (point)))
8709 (let ((p (cperl-get-here-doc-region pos)))
8710 (or p (error "Not inside a HERE document"))
8711 (narrow-to-region (car p) (cdr p))
8712 (message
8713 "When you are finished with narrow editing, type C-x n w")))
8714
8715(defun cperl-select-this-pod-or-here-doc (&optional pos)
8716 "Select the HERE-DOC (or POD section) at POS.
8717POS defaults to the point."
8718 (interactive "d")
8719 (let ((p (cperl-get-here-doc-region pos t)))
8720 (if p
8721 (progn
8722 (goto-char (car p))
8723 (push-mark (cdr p) nil t)) ; Message, activate in transient-mode
8724 (message "I do not think POS is in POD or a HERE-doc..."))))
8725
8726(defun cperl-facemenu-add-face-function (face end)
8727 "A callback to process user-initiated font-change requests.
8728Translates `bold', `italic', and `bold-italic' requests to insertion of
8729corresponding POD directives, and `underline' to C<> POD directive.
8730
8731Such requests are usually bound to M-o LETTER."
8732 (or (get-text-property (point) 'in-pod)
8733 (error "Faces can only be set within POD"))
8734 (setq facemenu-end-add-face (if (eq face 'bold-italic) ">>" ">"))
8735 (cdr (or (assq face '((bold . "B<")
8736 (italic . "I<")
8737 (bold-italic . "B<I<")
8738 (underline . "C<")))
8739 (error "Face %s not configured for cperl-mode"
8740 face))))
8741\f
8742(defun cperl-time-fontification (&optional l step lim)
8743 "Times how long it takes to do incremental fontification in a region.
8744L is the line to start at, STEP is the number of lines to skip when
8745doing next incremental fontification, LIM is the maximal number of
8746incremental fontification to perform. Messages are accumulated in
8747*Messages* buffer.
8748
8749May be used for pinpointing which construct slows down buffer fontification:
8750start with default arguments, then refine the slowdown regions."
8751 (interactive "nLine to start at: \nnStep to do incremental fontification: ")
8752 (or l (setq l 1))
8753 (or step (setq step 500))
8754 (or lim (setq lim 40))
8755 (let* ((timems (function (lambda ()
8756 (let ((tt (current-time)))
8757 (+ (* 1000 (nth 1 tt)) (/ (nth 2 tt) 1000))))))
8758 (tt (funcall timems)) (c 0) delta tot)
8759 (goto-line l)
8760 (cperl-mode)
8761 (setq tot (- (- tt (setq tt (funcall timems)))))
8762 (message "cperl-mode at %s: %s" l tot)
8763 (while (and (< c lim) (not (eobp)))
8764 (forward-line step)
8765 (setq l (+ l step))
8766 (setq c (1+ c))
8767 (cperl-update-syntaxification (point) (point))
8768 (setq delta (- (- tt (setq tt (funcall timems)))) tot (+ tot delta))
8769 (message "to %s:%6s,%7s" l delta tot))
8770 tot))
8771
8772(defvar font-lock-cache-position)
8773
8774(defun cperl-emulate-lazy-lock (&optional window-size)
8775 "Emulate `lazy-lock' without `condition-case', so `debug-on-error' works.
8776Start fontifying the buffer from the start (or end) using the given
8777WINDOW-SIZE (units is lines). Negative WINDOW-SIZE starts at end, and
8778goes backwards; default is -50. This function is not CPerl-specific; it
8779may be used to debug problems with delayed incremental fontification."
8780 (interactive
8781 "nSize of window for incremental fontification, negative goes backwards: ")
8782 (or window-size (setq window-size -50))
8783 (let ((pos (if (> window-size 0)
8784 (point-min)
8785 (point-max)))
8786 p)
8787 (goto-char pos)
8788 (normal-mode)
8789 ;; Why needed??? With older font-locks???
8790 (set (make-local-variable 'font-lock-cache-position) (make-marker))
8791 (while (if (> window-size 0)
8792 (< pos (point-max))
8793 (> pos (point-min)))
8794 (setq p (progn
8795 (forward-line window-size)
8796 (point)))
8797 (font-lock-fontify-region (min p pos) (max p pos))
8798 (setq pos p))))
8799
8800\f
8801(defun cperl-lazy-install ()) ; Avoid a warning
8802(defun cperl-lazy-unstall ()) ; Avoid a warning
8803
8804(if (fboundp 'run-with-idle-timer)
8805 (progn
8806 (defvar cperl-help-shown nil
8807 "Non-nil means that the help was already shown now.")
8808
8809 (defvar cperl-lazy-installed nil
8810 "Non-nil means that the lazy-help handlers are installed now.")
8811
8812 (defun cperl-lazy-install ()
8813 "Switches on Auto-Help on Perl constructs (put in the message area).
8814Delay of auto-help controlled by `cperl-lazy-help-time'."
8815 (interactive)
8816 (make-local-variable 'cperl-help-shown)
8817 (if (and (cperl-val 'cperl-lazy-help-time)
8818 (not cperl-lazy-installed))
8819 (progn
8820 (add-hook 'post-command-hook 'cperl-lazy-hook)
8821 (run-with-idle-timer
8822 (cperl-val 'cperl-lazy-help-time 1000000 5)
8823 t
8824 'cperl-get-help-defer)
8825 (setq cperl-lazy-installed t))))
8826
8827 (defun cperl-lazy-unstall ()
8828 "Switches off Auto-Help on Perl constructs (put in the message area).
8829Delay of auto-help controlled by `cperl-lazy-help-time'."
8830 (interactive)
8831 (remove-hook 'post-command-hook 'cperl-lazy-hook)
8832 (cancel-function-timers 'cperl-get-help-defer)
8833 (setq cperl-lazy-installed nil))
8834
8835 (defun cperl-lazy-hook ()
8836 (setq cperl-help-shown nil))
8837
8838 (defun cperl-get-help-defer ()
8839 (if (not (memq major-mode '(perl-mode cperl-mode))) nil
8840 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
8841 (cperl-get-help)
8842 (setq cperl-help-shown t))))
8843 (cperl-lazy-install)))
8844
8845
8846;;; Plug for wrong font-lock:
8847
8848(defun cperl-font-lock-unfontify-region-function (beg end)
8849 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
8850 (inhibit-read-only t) (inhibit-point-motion-hooks t)
8851 before-change-functions after-change-functions
8852 deactivate-mark buffer-file-name buffer-file-truename)
8853 (remove-text-properties beg end '(face nil))
8854 (if (and (not modified) (buffer-modified-p))
8855 (set-buffer-modified-p nil))))
8856
8857(defun cperl-font-lock-fontify-region-function (beg end loudly)
8858 "Extends the region to safe positions, then calls the default function.
8859Newer `font-lock's can do it themselves.
8860We unwind only as far as needed for fontification. Syntaxification may
8861do extra unwind via `cperl-unwind-to-safe'."
8862 (save-excursion
8863 (goto-char beg)
8864 (while (and beg
8865 (progn
8866 (beginning-of-line)
8867 (eq (get-text-property (setq beg (point)) 'syntax-type)
8868 'multiline)))
8869 (if (setq beg (cperl-beginning-of-property beg 'syntax-type))
8870 (goto-char beg)))
8871 (setq beg (point))
8872 (goto-char end)
8873 (while (and end
8874 (progn
8875 (or (bolp) (condition-case nil
8876 (forward-line 1)
8877 (error nil)))
8878 (eq (get-text-property (setq end (point)) 'syntax-type)
8879 'multiline)))
8880 (setq end (next-single-property-change end 'syntax-type nil (point-max)))
8881 (goto-char end))
8882 (setq end (point)))
8883 (font-lock-default-fontify-region beg end loudly))
8884
8885(defvar cperl-d-l nil)
8886(defun cperl-fontify-syntaxically (end)
8887 ;; Some vars for debugging only
8888 ;; (message "Syntaxifying...")
8889 (let ((dbg (point)) (iend end) (idone cperl-syntax-done-to)
8890 (istate (car cperl-syntax-state))
8891 start from-start edebug-backtrace-buffer)
8892 (if (eq cperl-syntaxify-by-font-lock 'backtrace)
8893 (progn
8894 (require 'edebug)
8895 (let ((f 'edebug-backtrace))
8896 (funcall f)))) ; Avoid compile-time warning
8897 (or cperl-syntax-done-to
8898 (setq cperl-syntax-done-to (point-min)
8899 from-start t))
8900 (setq start (if (and cperl-hook-after-change
8901 (not from-start))
8902 cperl-syntax-done-to ; Fontify without change; ignore start
8903 ;; Need to forget what is after `start'
8904 (min cperl-syntax-done-to (point))))
8905 (goto-char start)
8906 (beginning-of-line)
8907 (setq start (point))
8908 (and cperl-syntaxify-unwind
8909 (setq end (cperl-unwind-to-safe t end)
8910 start (point)))
8911 (and (> end start)
8912 (setq cperl-syntax-done-to start) ; In case what follows fails
8913 (cperl-find-pods-heres start end t nil t))
8914 (if (memq cperl-syntaxify-by-font-lock '(backtrace message))
8915 (message "Syxify req=%s..%s actual=%s..%s done-to: %s=>%s statepos: %s=>%s"
8916 dbg iend start end idone cperl-syntax-done-to
8917 istate (car cperl-syntax-state))) ; For debugging
8918 nil)) ; Do not iterate
8919
8920(defun cperl-fontify-update (end)
8921 (let ((pos (point-min)) prop posend)
8922 (setq end (point-max))
8923 (while (< pos end)
8924 (setq prop (get-text-property pos 'cperl-postpone)
8925 posend (next-single-property-change pos 'cperl-postpone nil end))
8926 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8927 (setq pos posend)))
8928 nil) ; Do not iterate
8929
8930(defun cperl-fontify-update-bad (end)
8931 ;; Since fontification happens with different region than syntaxification,
8932 ;; do to the end of buffer, not to END;;; likewise, start earlier if needed
8933 (let* ((pos (point)) (prop (get-text-property pos 'cperl-postpone)) posend)
8934 (if prop
8935 (setq pos (or (cperl-beginning-of-property
8936 (cperl-1+ pos) 'cperl-postpone)
8937 (point-min))))
8938 (while (< pos end)
8939 (setq posend (next-single-property-change pos 'cperl-postpone))
8940 (and prop (put-text-property pos posend (car prop) (cdr prop)))
8941 (setq pos posend)
8942 (setq prop (get-text-property pos 'cperl-postpone))))
8943 nil) ; Do not iterate
8944
8945;; Called when any modification is made to buffer text.
8946(defun cperl-after-change-function (beg end old-len)
8947 ;; We should have been informed about changes by `font-lock'. Since it
8948 ;; does not inform as which calls are defered, do it ourselves
8949 (if cperl-syntax-done-to
8950 (setq cperl-syntax-done-to (min cperl-syntax-done-to beg))))
8951
8952(defun cperl-update-syntaxification (from to)
8953 (if (and cperl-use-syntax-table-text-property
8954 cperl-syntaxify-by-font-lock
8955 (or (null cperl-syntax-done-to)
8956 (< cperl-syntax-done-to to)))
8957 (progn
8958 (save-excursion
8959 (goto-char from)
8960 (cperl-fontify-syntaxically to)))))
8961
8962(defvar cperl-version
8963 (let ((v "Revision: 6.2"))
8964 (string-match ":\\s *\\([0-9.]+\\)" v)
8965 (substring v (match-beginning 1) (match-end 1)))
8966 "Version of IZ-supported CPerl package this file is based on.")
8967
8968(provide 'cperl-mode)
8969
8970;; arch-tag: 42e5b19b-e187-4537-929f-1a7408980ce6
8971;;; cperl-mode.el ends here