]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cperl-mode.el
CC Mode update to version 5.29. This is for testing; it's not a released
[gnu-emacs] / lisp / progmodes / cperl-mode.el
1 ;;; cperl-mode.el --- Perl code editing commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 87, 91, 92, 93, 94, 95, 96, 1997
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Ilya Zakharevich and Bob Olson
7 ;; Maintainer: Ilya Zakharevich <ilya@math.ohio-state.edu>
8 ;; Keywords: languages, Perl
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Corrections made by Ilya Zakharevich ilya@math.mps.ohio-state.edu
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-non-problems', `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 ;;; into your .emacs file.
54
55 ;;;; This mode supports font-lock, imenu and mode-compile. In the
56 ;;;; hairy version font-lock is on, but you should activate imenu
57 ;;;; yourself (note that mode-compile is not standard yet). Well, you
58 ;;;; can use imenu from keyboard anyway (M-x imenu), but it is better
59 ;;;; to bind it like that:
60
61 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
62
63 ;;; Code:
64
65 ;; Some macros are needed for `defcustom'
66 (eval-when-compile
67 (require 'font-lock)
68 (defvar msb-menu-cond)
69 (defvar gud-perldb-history)
70 (defvar font-lock-background-mode) ; not in Emacs
71 (defvar font-lock-display-type) ; ditto
72 (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
73 (defmacro cperl-is-face (arg) ; Takes quoted arg
74 (cond ((fboundp 'find-face)
75 `(find-face ,arg))
76 (;;(and (fboundp 'face-list)
77 ;; (face-list))
78 (fboundp 'face-list)
79 `(member ,arg (and (fboundp 'face-list)
80 (face-list))))
81 (t
82 `(boundp ,arg))))
83 (defmacro cperl-make-face (arg descr) ; Takes unquoted arg
84 (cond ((fboundp 'make-face)
85 `(make-face (quote ,arg)))
86 (t
87 `(defvar ,arg (quote ,arg) ,descr))))
88 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
89 `(progn
90 (or (cperl-is-face (quote ,arg))
91 (cperl-make-face ,arg ,descr))
92 (or (boundp (quote ,arg)) ; We use unquoted variants too
93 (defvar ,arg (quote ,arg) ,descr))))
94 (if cperl-xemacs-p
95 (defmacro cperl-etags-snarf-tag (file line)
96 `(progn
97 (beginning-of-line 2)
98 (list ,file ,line)))
99 (defmacro cperl-etags-snarf-tag (file line)
100 `(etags-snarf-tag)))
101 (if cperl-xemacs-p
102 (defmacro cperl-etags-goto-tag-location (elt)
103 ;;(progn
104 ;; (switch-to-buffer (get-file-buffer (elt (, elt) 0)))
105 ;; (set-buffer (get-file-buffer (elt (, elt) 0)))
106 ;; Probably will not work due to some save-excursion???
107 ;; Or save-file-position?
108 ;; (message "Did I get to line %s?" (elt (, elt) 1))
109 `(goto-line (string-to-int (elt ,elt 1))))
110 ;;)
111 (defmacro cperl-etags-goto-tag-location (elt)
112 `(etags-goto-tag-location ,elt)))
113 (autoload 'tmm-prompt "tmm"))
114
115 (defun cperl-choose-color (&rest list)
116 (let (answer)
117 (while list
118 (or answer
119 (if (or (x-color-defined-p (car list))
120 (null (cdr list)))
121 (setq answer (car list))))
122 (setq list (cdr list)))
123 answer))
124
125 (defgroup cperl nil
126 "Major mode for editing Perl code."
127 :prefix "cperl-"
128 :group 'languages
129 :version "20.3")
130
131 (defgroup cperl-indentation-details nil
132 "Indentation."
133 :prefix "cperl-"
134 :group 'cperl)
135
136 (defgroup cperl-affected-by-hairy nil
137 "Variables affected by `cperl-hairy'."
138 :prefix "cperl-"
139 :group 'cperl)
140
141 (defgroup cperl-autoinsert-details nil
142 "Auto-insert tuneup."
143 :prefix "cperl-"
144 :group 'cperl)
145
146 (defgroup cperl-faces nil
147 "Fontification colors."
148 :prefix "cperl-"
149 :group 'cperl)
150
151 (defgroup cperl-speed nil
152 "Speed vs. validity tuneup."
153 :prefix "cperl-"
154 :group 'cperl)
155
156 (defgroup cperl-help-system nil
157 "Help system tuneup."
158 :prefix "cperl-"
159 :group 'cperl)
160
161 \f
162 (defcustom cperl-extra-newline-before-brace nil
163 "*Non-nil means that if, elsif, while, until, else, for, foreach
164 and do constructs look like:
165
166 if ()
167 {
168 }
169
170 instead of:
171
172 if () {
173 }
174 "
175 :type 'boolean
176 :group 'cperl-autoinsert-details)
177
178 (defcustom cperl-extra-newline-before-brace-multiline
179 cperl-extra-newline-before-brace
180 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
181 for constructs with multiline if/unless/while/until/for/foreach condition."
182 :type 'boolean
183 :group 'cperl-autoinsert-details)
184
185 (defcustom cperl-indent-level 2
186 "*Indentation of CPerl statements with respect to containing block."
187 :type 'integer
188 :group 'cperl-indentation-details)
189
190 (defcustom cperl-lineup-step nil
191 "*`cperl-lineup' will always lineup at multiple of this number.
192 If nil, the value of `cperl-indent-level' will be used."
193 :type '(choice (const nil) integer)
194 :group 'cperl-indentation-details)
195
196 (defcustom cperl-brace-imaginary-offset 0
197 "*Imagined indentation of a Perl open brace that actually follows a statement.
198 An open brace following other text is treated as if it were this far
199 to the right of the start of its line."
200 :type 'integer
201 :group 'cperl-indentation-details)
202
203 (defcustom cperl-brace-offset 0
204 "*Extra indentation for braces, compared with other text in same context."
205 :type 'integer
206 :group 'cperl-indentation-details)
207 (defcustom cperl-label-offset -2
208 "*Offset of CPerl label lines relative to usual indentation."
209 :type 'integer
210 :group 'cperl-indentation-details)
211 (defcustom cperl-min-label-indent 1
212 "*Minimal offset of CPerl label lines."
213 :type 'integer
214 :group 'cperl-indentation-details)
215 (defcustom cperl-continued-statement-offset 2
216 "*Extra indent for lines not starting new statements."
217 :type 'integer
218 :group 'cperl-indentation-details)
219 (defcustom cperl-continued-brace-offset 0
220 "*Extra indent for substatements that start with open-braces.
221 This is in addition to cperl-continued-statement-offset."
222 :type 'integer
223 :group 'cperl-indentation-details)
224 (defcustom cperl-close-paren-offset -1
225 "*Extra indent for substatements that start with close-parenthesis."
226 :type 'integer
227 :group 'cperl-indentation-details)
228
229 (defcustom cperl-auto-newline nil
230 "*Non-nil means automatically newline before and after braces,
231 and after colons and semicolons, inserted in CPerl code. The following
232 \\[cperl-electric-backspace] will remove the inserted whitespace.
233 Insertion after colons requires both this variable and
234 `cperl-auto-newline-after-colon' set."
235 :type 'boolean
236 :group 'cperl-autoinsert-details)
237
238 (defcustom cperl-autoindent-on-semi nil
239 "*Non-nil means automatically indent after insertion of (semi)colon.
240 Active if `cperl-auto-newline' is false."
241 :type 'boolean
242 :group 'cperl-autoinsert-details)
243
244 (defcustom cperl-auto-newline-after-colon nil
245 "*Non-nil means automatically newline even after colons.
246 Subject to `cperl-auto-newline' setting."
247 :type 'boolean
248 :group 'cperl-autoinsert-details)
249
250 (defcustom cperl-tab-always-indent t
251 "*Non-nil means TAB in CPerl mode should always reindent the current line,
252 regardless of where in the line point is when the TAB command is used."
253 :type 'boolean
254 :group 'cperl-indentation-details)
255
256 (defcustom cperl-font-lock nil
257 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
258 Can be overwritten by `cperl-hairy' if nil."
259 :type '(choice (const null) boolean)
260 :group 'cperl-affected-by-hairy)
261
262 (defcustom cperl-electric-lbrace-space nil
263 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
264 Can be overwritten by `cperl-hairy' if nil."
265 :type '(choice (const null) boolean)
266 :group 'cperl-affected-by-hairy)
267
268 (defcustom cperl-electric-parens-string "({[]})<"
269 "*String of parentheses that should be electric in CPerl.
270 Closing ones are electric only if the region is highlighted."
271 :type 'string
272 :group 'cperl-affected-by-hairy)
273
274 (defcustom cperl-electric-parens nil
275 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
276 Can be overwritten by `cperl-hairy' if nil."
277 :type '(choice (const null) boolean)
278 :group 'cperl-affected-by-hairy)
279
280 (defvar zmacs-regions) ; Avoid warning
281
282 (defcustom cperl-electric-parens-mark
283 (and window-system
284 (or (and (boundp 'transient-mark-mode) ; For Emacs
285 transient-mark-mode)
286 (and (boundp 'zmacs-regions) ; For XEmacs
287 zmacs-regions)))
288 "*Not-nil means that electric parens look for active mark.
289 Default is yes if there is visual feedback on mark."
290 :type 'boolean
291 :group 'cperl-autoinsert-details)
292
293 (defcustom cperl-electric-linefeed nil
294 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
295 In any case these two mean plain and hairy linefeeds together.
296 Can be overwritten by `cperl-hairy' if nil."
297 :type '(choice (const null) boolean)
298 :group 'cperl-affected-by-hairy)
299
300 (defcustom cperl-electric-keywords nil
301 "*Not-nil (and non-null) means keywords are electric in CPerl.
302 Can be overwritten by `cperl-hairy' if nil."
303 :type '(choice (const null) boolean)
304 :group 'cperl-affected-by-hairy)
305
306 (defcustom cperl-hairy nil
307 "*Not-nil means most of the bells and whistles are enabled in CPerl.
308 Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
309 `cperl-electric-parens', `cperl-electric-linefeed', `cperl-electric-keywords',
310 `cperl-info-on-command-no-prompt', `cperl-clobber-lisp-bindings',
311 `cperl-lazy-help-time'."
312 :type 'boolean
313 :group 'cperl-affected-by-hairy)
314
315 (defcustom cperl-comment-column 32
316 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
317 :type 'integer
318 :group 'cperl-indentation-details)
319
320 (defcustom cperl-vc-header-alist '((SCCS "$sccs = '%W\%' ;")
321 (RCS "$rcs = ' $Id\$ ' ;"))
322 "*What to use as `vc-header-alist' in CPerl."
323 :type '(repeat (list symbol string))
324 :group 'cperl)
325
326 (defcustom cperl-clobber-mode-lists
327 (not
328 (and
329 (boundp 'interpreter-mode-alist)
330 (assoc "miniperl" interpreter-mode-alist)
331 (assoc "\\.\\([pP][Llm]\\|al\\)$" auto-mode-alist)))
332 "*Whether to install us into `interpreter-' and `extension' mode lists."
333 :type 'boolean
334 :group 'cperl)
335
336 (defcustom cperl-info-on-command-no-prompt nil
337 "*Not-nil (and non-null) means not to prompt on C-h f.
338 The opposite behaviour is always available if prefixed with C-c.
339 Can be overwritten by `cperl-hairy' if nil."
340 :type '(choice (const null) boolean)
341 :group 'cperl-affected-by-hairy)
342
343 (defcustom cperl-clobber-lisp-bindings nil
344 "*Not-nil (and non-null) means not overwrite C-h f.
345 The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
346 Can be overwritten by `cperl-hairy' if nil."
347 :type '(choice (const null) boolean)
348 :group 'cperl-affected-by-hairy)
349
350 (defcustom cperl-lazy-help-time nil
351 "*Not-nil (and non-null) means to show lazy help after given idle time.
352 Can be overwritten by `cperl-hairy' to be 5 sec if nil."
353 :type '(choice (const null) (const nil) integer)
354 :group 'cperl-affected-by-hairy)
355
356 (defcustom cperl-pod-face 'font-lock-comment-face
357 "*Face for pod highlighting."
358 :type 'face
359 :group 'cperl-faces)
360
361 (defcustom cperl-pod-head-face 'font-lock-variable-name-face
362 "*Face for pod highlighting.
363 Font for POD headers."
364 :type 'face
365 :group 'cperl-faces)
366
367 (defcustom cperl-here-face 'font-lock-string-face
368 "*Face for here-docs highlighting."
369 :type 'face
370 :group 'cperl-faces)
371
372 (defcustom cperl-invalid-face 'underline
373 "*Face for highlighting trailing whitespace."
374 :type 'face
375 :version "21.1"
376 :group 'cperl-faces)
377
378 (defcustom cperl-pod-here-fontify '(featurep 'font-lock)
379 "*Not-nil after evaluation means to highlight pod and here-docs sections."
380 :type 'boolean
381 :group 'cperl-faces)
382
383 (defcustom cperl-fontify-m-as-s t
384 "*Not-nil means highlight 1arg regular expressions operators same as 2arg."
385 :type 'boolean
386 :group 'cperl-faces)
387
388 (defcustom cperl-highlight-variables-indiscriminately nil
389 "*Non-nil means perform additional highlighting on variables.
390 Currently only changes how scalar variables are highlighted.
391 Note that that variable is only read at initialization time for
392 the variable `cperl-font-lock-keywords-2', so changing it after you've
393 entered `cperl-mode' the first time will have no effect."
394 :type 'boolean
395 :group 'cperl)
396
397 (defcustom cperl-pod-here-scan t
398 "*Not-nil means look for pod and here-docs sections during startup.
399 You can always make lookup from menu or using \\[cperl-find-pods-heres]."
400 :type 'boolean
401 :group 'cperl-speed)
402
403 (defcustom cperl-regexp-scan t
404 "*Not-nil means make marking of regular expression more thorough.
405 Effective only with `cperl-pod-here-scan'. Not implemented yet."
406 :type 'boolean
407 :group 'cperl-speed)
408
409 (defcustom cperl-imenu-addback nil
410 "*Not-nil means add backreferences to generated `imenu's.
411 May require patched `imenu' and `imenu-go'. Obsolete."
412 :type 'boolean
413 :group 'cperl-help-system)
414
415 (defcustom cperl-max-help-size 66
416 "*Non-nil means shrink-wrapping of info-buffer allowed up to these percents."
417 :type '(choice integer (const nil))
418 :group 'cperl-help-system)
419
420 (defcustom cperl-shrink-wrap-info-frame t
421 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
422 :type 'boolean
423 :group 'cperl-help-system)
424
425 (defcustom cperl-info-page "perl"
426 "*Name of the info page containing perl docs.
427 Older version of this page was called `perl5', newer `perl'."
428 :type 'string
429 :group 'cperl-help-system)
430
431 (defcustom cperl-use-syntax-table-text-property
432 (boundp 'parse-sexp-lookup-properties)
433 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
434 :type 'boolean
435 :group 'cperl-speed)
436
437 (defcustom cperl-use-syntax-table-text-property-for-tags
438 cperl-use-syntax-table-text-property
439 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
440 :type 'boolean
441 :group 'cperl-speed)
442
443 (defcustom cperl-scan-files-regexp "\\.\\([pP][Llm]\\|xs\\)$"
444 "*Regexp to match files to scan when generating TAGS."
445 :type 'regexp
446 :group 'cperl)
447
448 (defcustom cperl-noscan-files-regexp "/\\(\\.\\.?\\|SCCS\\|RCS\\|blib\\)$"
449 "*Regexp to match files/dirs to skip when generating TAGS."
450 :type 'regexp
451 :group 'cperl)
452
453 (defcustom cperl-regexp-indent-step nil
454 "*Indentation used when beautifying regexps.
455 If nil, the value of `cperl-indent-level' will be used."
456 :type '(choice integer (const nil))
457 :group 'cperl-indentation-details)
458
459 (defcustom cperl-indent-left-aligned-comments t
460 "*Non-nil means that the comment starting in leftmost column should indent."
461 :type 'boolean
462 :group 'cperl-indentation-details)
463
464 (defcustom cperl-under-as-char nil
465 "*Non-nil means that the _ (underline) should be treated as word char."
466 :type 'boolean
467 :group 'cperl)
468
469 (defcustom cperl-extra-perl-args ""
470 "*Extra arguments to use when starting Perl.
471 Currently used with `cperl-check-syntax' only."
472 :type 'string
473 :group 'cperl)
474
475 (defcustom cperl-message-electric-keyword t
476 "*Non-nil means that the `cperl-electric-keyword' prints a help message."
477 :type 'boolean
478 :group 'cperl-help-system)
479
480 (defcustom cperl-indent-region-fix-constructs 1
481 "*Amount of space to insert between `}' and `else' or `elsif'
482 in `cperl-indent-region'. Set to nil to leave as is. Values other
483 than 1 and nil will probably not work."
484 :type '(choice (const nil) (const 1))
485 :group 'cperl-indentation-details)
486
487 (defcustom cperl-break-one-line-blocks-when-indent t
488 "*Non-nil means that one-line if/unless/while/until/for/foreach BLOCKs
489 need to be reformated into multiline ones when indenting a region."
490 :type 'boolean
491 :group 'cperl-indentation-details)
492
493 (defcustom cperl-fix-hanging-brace-when-indent t
494 "*Non-nil means that BLOCK-end `}' may be put on a separate line
495 when indenting a region.
496 Braces followed by else/elsif/while/until are excepted."
497 :type 'boolean
498 :group 'cperl-indentation-details)
499
500 (defcustom cperl-merge-trailing-else t
501 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
502 may be merged to be on the same line when indenting a region."
503 :type 'boolean
504 :group 'cperl-indentation-details)
505
506 (defcustom cperl-indent-parens-as-block nil
507 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
508 but for trailing \",\" inside the group, which won't increase indentation.
509 One should tune up `cperl-close-paren-offset' as well."
510 :type 'boolean
511 :group 'cperl-indentation-details)
512
513 (defcustom cperl-syntaxify-by-font-lock
514 (and window-system
515 (boundp 'parse-sexp-lookup-properties))
516 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
517 :type '(choice (const message) boolean)
518 :group 'cperl-speed)
519
520 (defcustom cperl-syntaxify-unwind
521 t
522 "*Non-nil means that CPerl unwinds to a start of along construction
523 when syntaxifying a chunk of buffer."
524 :type 'boolean
525 :group 'cperl-speed)
526
527 (defcustom cperl-ps-print-face-properties
528 '((font-lock-keyword-face nil nil bold shadow)
529 (font-lock-variable-name-face nil nil bold)
530 (font-lock-function-name-face nil nil bold italic box)
531 (font-lock-constant-face nil "LightGray" bold)
532 (cperl-array-face nil "LightGray" bold underline)
533 (cperl-hash-face nil "LightGray" bold italic underline)
534 (font-lock-comment-face nil "LightGray" italic)
535 (font-lock-string-face nil nil italic underline)
536 (cperl-nonoverridable-face nil nil italic underline)
537 (font-lock-type-face nil nil underline)
538 (underline nil "LightGray" strikeout))
539 "List given as an argument to `ps-extend-face-list' in `cperl-ps-print'."
540 :type '(repeat (cons symbol
541 (cons (choice (const nil) string)
542 (cons (choice (const nil) string)
543 (repeat symbol)))))
544 :group 'cperl-faces)
545
546 (if window-system
547 (progn
548 (defvar cperl-dark-background
549 (cperl-choose-color "navy" "os2blue" "darkgreen"))
550 (defvar cperl-dark-foreground
551 (cperl-choose-color "orchid1" "orange"))
552
553 (defface cperl-nonoverridable-face
554 `((((class grayscale) (background light))
555 (:background "Gray90" :slant italic :underline t))
556 (((class grayscale) (background dark))
557 (:foreground "Gray80" :slant italic :underline t :weight bold))
558 (((class color) (background light))
559 (:foreground "chartreuse3"))
560 (((class color) (background dark))
561 (:foreground ,cperl-dark-foreground))
562 (t (:weight bold :underline t)))
563 "Font Lock mode face used to highlight array names."
564 :group 'cperl-faces)
565
566 (defface cperl-array-face
567 `((((class grayscale) (background light))
568 (:background "Gray90" :weight bold))
569 (((class grayscale) (background dark))
570 (:foreground "Gray80" :weight bold))
571 (((class color) (background light))
572 (:foreground "Blue" :background "lightyellow2" :weight bold))
573 (((class color) (background dark))
574 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
575 (t (:weight bold)))
576 "Font Lock mode face used to highlight array names."
577 :group 'cperl-faces)
578
579 (defface cperl-hash-face
580 `((((class grayscale) (background light))
581 (:background "Gray90" :weight bold :slant italic))
582 (((class grayscale) (background dark))
583 (:foreground "Gray80" :weight bold :slant italic))
584 (((class color) (background light))
585 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
586 (((class color) (background dark))
587 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
588 (t (:weight bold :slant italic)))
589 "Font Lock mode face used to highlight hash names."
590 :group 'cperl-faces)))
591
592 \f
593
594 ;;; Short extra-docs.
595
596 (defvar cperl-tips 'please-ignore-this-line
597 "Get newest version of this package from
598 ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs
599 and/or
600 ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
601 Subdirectory `cperl-mode' may contain yet newer development releases and/or
602 patches to related files.
603
604 For best results apply to an older Emacs the patches from
605 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
606 \(this upgrades syntax-parsing abilities of Emaxen v19.34 and
607 v20.2 up to the level of Emacs v20.3 - a must for a good Perl
608 mode.)
609
610 Get support packages choose-color.el (or font-lock-extra.el before
611 19.30), imenu-go.el from the same place. \(Look for other files there
612 too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
613 later you should use choose-color.el *instead* of font-lock-extra.el
614 \(and you will not get smart highlighting in C :-().
615
616 Note that to enable Compile choices in the menu you need to install
617 mode-compile.el.
618
619 If your Emacs does not default to `cperl-mode' on Perl files, and you
620 want it to: put the following into your .emacs file:
621
622 (defalias 'perl-mode 'cperl-mode)
623
624 Get perl5-info from
625 $CPAN/doc/manual/info/perl-info.tar.gz
626 older version was on
627 http://www.metronet.com:70/9/perlinfo/perl5/manual/perl5-info.tar.gz
628
629 If you use imenu-go, run imenu on perl5-info buffer (you can do it
630 from Perl menu). If many files are related, generate TAGS files from
631 Tools/Tags submenu in Perl menu.
632
633 If some class structure is too complicated, use Tools/Hierarchy-view
634 from Perl menu, or hierarchic view of imenu. The second one uses the
635 current buffer only, the first one requires generation of TAGS from
636 Perl/Tools/Tags menu beforehand.
637
638 Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
639
640 Switch auto-help on/off with Perl/Tools/Auto-help.
641
642 Though with contemporary Emaxen CPerl mode should maintain the correct
643 parsing of Perl even when editing, sometimes it may be lost. Fix this by
644
645 \\[normal-mode]
646
647 In cases of more severe confusion sometimes it is helpful to do
648
649 \\[load-library] cperl-mode RET
650 \\[normal-mode]
651
652 Before reporting (non-)problems look in the problem section of online
653 micro-docs on what I know about CPerl problems.")
654
655 (defvar cperl-problems 'please-ignore-this-line
656 "Some faces will not be shown on some versions of Emacs unless you
657 install choose-color.el, available from
658 ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs/
659
660 `fill-paragraph' on a comment may leave the point behind the
661 paragraph. Parsing of lines with several <<EOF is not implemented
662 yet.
663
664 Emacs had a _very_ restricted syntax parsing engine until version
665 20.1. Most problems below are corrected starting from this version of
666 Emacs, and all of them should be fixed in version 20.3. (Or apply
667 patches to Emacs 19.33/34 - see tips.)
668
669 Note that even with newer Emacsen in some very rare cases the details
670 of interaction of `font-lock' and syntaxification may be not cleaned
671 up yet. You may get slightly different colors basing on the order of
672 fontification and syntaxification. Say, the initial faces is correct,
673 but editing the buffer breaks this.
674
675 Even with older Emacsen CPerl mode tries to corrects some Emacs
676 misunderstandings, however, for efficiency reasons the degree of
677 correction is different for different operations. The partially
678 corrected problems are: POD sections, here-documents, regexps. The
679 operations are: highlighting, indentation, electric keywords, electric
680 braces.
681
682 This may be confusing, since the regexp s#//#/#\; may be highlighted
683 as a comment, but it will be recognized as a regexp by the indentation
684 code. Or the opposite case, when a pod section is highlighted, but
685 may break the indentation of the following code (though indentation
686 should work if the balance of delimiters is not broken by POD).
687
688 The main trick (to make $ a \"backslash\") makes constructions like
689 ${aaa} look like unbalanced braces. The only trick I can think of is
690 to insert it as $ {aaa} (legal in perl5, not in perl4).
691
692 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
693 as /($|\\s)/. Note that such a transposition is not always possible.
694
695 The solution is to upgrade your Emacs or patch an older one. Note
696 that Emacs 20.2 has some bugs related to `syntax-table' text
697 properties. Patches are available on the main CPerl download site,
698 and on CPAN.
699
700 If these bugs cannot be fixed on your machine (say, you have an inferior
701 environment and cannot recompile), you may still disable all the fancy stuff
702 via `cperl-use-syntax-table-text-property'." )
703
704 (defvar cperl-non-problems 'please-ignore-this-line
705 "As you know from `problems' section, Perl syntax is too hard for CPerl on
706 older Emacsen. Here is what you can do if you cannot upgrade, or if
707 you want to switch off these capabilities on Emacs 20.2 (+patches) or 20.3
708 or better. Please skip this docs if you run a capable Emacs already.
709
710 Most of the time, if you write your own code, you may find an equivalent
711 \(and almost as readable) expression (what is discussed below is usually
712 not relevant on newer Emacsen, since they can do it automatically).
713
714 Try to help CPerl: add comments with embedded quotes to fix CPerl
715 misunderstandings about the end of quotation:
716
717 $a='500$'; # ';
718
719 You won't need it too often. The reason: $ \"quotes\" the following
720 character (this saves a life a lot of times in CPerl), thus due to
721 Emacs parsing rules it does not consider tick (i.e., ' ) after a
722 dollar as a closing one, but as a usual character. This is usually
723 correct, but not in the above context.
724
725 Even with older Emacsen the indentation code is pretty wise. The only
726 drawback is that it relied on Emacs parsing to find matching
727 parentheses. And Emacs *could not* match parentheses in Perl 100%
728 correctly. So
729 1 if s#//#/#;
730 would not break indentation, but
731 1 if ( s#//#/# );
732 would. Upgrade.
733
734 By similar reasons
735 s\"abc\"def\";
736 could confuse CPerl a lot.
737
738 If you still get wrong indentation in situation that you think the
739 code should be able to parse, try:
740
741 a) Check what Emacs thinks about balance of your parentheses.
742 b) Supply the code to me (IZ).
743
744 Pods were treated _very_ rudimentally. Here-documents were not
745 treated at all (except highlighting and inhibiting indentation). Upgrade.
746
747 To speed up coloring the following compromises exist:
748 a) sub in $mypackage::sub may be highlighted.
749 b) -z in [a-z] may be highlighted.
750 c) if your regexp contains a keyword (like \"s\"), it may be highlighted.
751
752
753 Imenu in 19.31 is broken. Set `imenu-use-keymap-menu' to t, and remove
754 `car' before `imenu-choose-buffer-index' in `imenu'.
755 `imenu-add-to-menubar' in 20.2 is broken.
756
757 A lot of things on XEmacs may be broken too, judging by bug reports I
758 receive. Note that some releases of XEmacs are better than the others
759 as far as bugs reports I see are concerned.")
760
761 (defvar cperl-praise 'please-ignore-this-line
762 "Advantages of CPerl mode.
763
764 0) It uses the newest `syntax-table' property ;-);
765
766 1) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
767 mode - but the latter number may have improved too in last years) even
768 with old Emaxen which do not support `syntax-table' property.
769
770 When using `syntax-table' property for syntax assist hints, it should
771 handle 99.995% of lines correct - or somesuch. It automatically
772 updates syntax assist hints when you edit your script.
773
774 2) It is generally believed to be \"the most user-friendly Emacs
775 package\" whatever it may mean (I doubt that the people who say similar
776 things tried _all_ the rest of Emacs ;-), but this was not a lonely
777 voice);
778
779 3) Everything is customizable, one-by-one or in a big sweep;
780
781 4) It has many easily-accessable \"tools\":
782 a) Can run program, check syntax, start debugger;
783 b) Can lineup vertically \"middles\" of rows, like `=' in
784 a = b;
785 cc = d;
786 c) Can insert spaces where this impoves readability (in one
787 interactive sweep over the buffer);
788 d) Has support for imenu, including:
789 1) Separate unordered list of \"interesting places\";
790 2) Separate TOC of POD sections;
791 3) Separate list of packages;
792 4) Hierarchical view of methods in (sub)packages;
793 5) and functions (by the full name - with package);
794 e) Has an interface to INFO docs for Perl; The interface is
795 very flexible, including shrink-wrapping of
796 documentation buffer/frame;
797 f) Has a builtin list of one-line explanations for perl constructs.
798 g) Can show these explanations if you stay long enough at the
799 corresponding place (or on demand);
800 h) Has an enhanced fontification (using 3 or 4 additional faces
801 comparing to font-lock - basically, different
802 namespaces in Perl have different colors);
803 i) Can construct TAGS basing on its knowledge of Perl syntax,
804 the standard menu has 6 different way to generate
805 TAGS (if \"by directory\", .xs files - with C-language
806 bindings - are included in the scan);
807 j) Can build a hierarchical view of classes (via imenu) basing
808 on generated TAGS file;
809 k) Has electric parentheses, electric newlines, uses Abbrev
810 for electric logical constructs
811 while () {}
812 with different styles of expansion (context sensitive
813 to be not so bothering). Electric parentheses behave
814 \"as they should\" in a presence of a visible region.
815 l) Changes msb.el \"on the fly\" to insert a group \"Perl files\";
816 m) Can convert from
817 if (A) { B }
818 to
819 B if A;
820
821 n) Highlights (by user-choice) either 3-delimiters constructs
822 (such as tr/a/b/), or regular expressions and `y/tr';
823 o) Highlights trailing whitespace;
824 p) Is able to manipulate Perl Regular Expressions to ease
825 conversion to a more readable form.
826
827 5) The indentation engine was very smart, but most of tricks may be
828 not needed anymore with the support for `syntax-table' property. Has
829 progress indicator for indentation (with `imenu' loaded).
830
831 6) Indent-region improves inline-comments as well; also corrects
832 whitespace *inside* the conditional/loop constructs.
833
834 7) Fill-paragraph correctly handles multi-line comments;
835
836 8) Can switch to different indentation styles by one command, and restore
837 the settings present before the switch.
838
839 9) When doing indentation of control constructs, may correct
840 line-breaks/spacing between elements of the construct.
841
842 10) Uses a linear-time algorith for indentation of regions (on Emaxen with
843 capable syntax engines).
844 ")
845
846 (defvar cperl-speed 'please-ignore-this-line
847 "This is an incomplete compendium of what is available in other parts
848 of CPerl documentation. (Please inform me if I skept anything.)
849
850 There is a perception that CPerl is slower than alternatives. This part
851 of documentation is designed to overcome this misconception.
852
853 *By default* CPerl tries to enable the most comfortable settings.
854 From most points of view, correctly working package is infinitely more
855 comfortable than a non-correctly working one, thus by default CPerl
856 prefers correctness over speed. Below is the guide how to change
857 settings if your preferences are different.
858
859 A) Speed of loading the file. When loading file, CPerl may perform a
860 scan which indicates places which cannot be parsed by primitive Emacs
861 syntax-parsing routines, and marks them up so that either
862
863 A1) CPerl may work around these deficiencies (for big chunks, mostly
864 PODs and HERE-documents), or
865 A2) On capable Emaxen CPerl will use improved syntax-handlings
866 which reads mark-up hints directly.
867
868 The scan in case A2 is much more comprehensive, thus may be slower.
869
870 User can disable syntax-engine-helping scan of A2 by setting
871 `cperl-use-syntax-table-text-property'
872 variable to nil (if it is set to t).
873
874 One can disable the scan altogether (both A1 and A2) by setting
875 `cperl-pod-here-scan'
876 to nil.
877
878 B) Speed of editing operations.
879
880 One can add a (minor) speedup to editing operations by setting
881 `cperl-use-syntax-table-text-property'
882 variable to nil (if it is set to t). This will disable
883 syntax-engine-helping scan, thus will make many more Perl
884 constructs be wrongly recognized by CPerl, thus may lead to
885 wrongly matched parentheses, wrong indentation, etc.
886
887 One can unset `cperl-syntaxify-unwind'. This might speed up editing
888 of, say, long POD sections.
889 ")
890
891 (defvar cperl-tips-faces 'please-ignore-this-line
892 "CPerl mode uses following faces for highlighting:
893
894 `cperl-array-face' Array names
895 `cperl-hash-face' Hash names
896 `font-lock-comment-face' Comments, PODs and whatever is considered
897 syntaxically to be not code
898 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
899 2-arg operators s/y/tr/ or of RExen,
900 `font-lock-function-name-face' Special-cased m// and s//foo/, _ as
901 a target of a file tests, file tests,
902 subroutine names at the moment of definition
903 (except those conflicting with Perl operators),
904 package names (when recognized), format names
905 `font-lock-keyword-face' Control flow switch constructs, declarators
906 `cperl-nonoverridable-face' Non-overridable keywords, modifiers of RExen
907 `font-lock-string-face' Strings, qw() constructs, RExen, POD sections,
908 literal parts and the terminator of formats
909 and whatever is syntaxically considered
910 as string literals
911 `font-lock-type-face' Overridable keywords
912 `font-lock-variable-name-face' Variable declarations, indirect array and
913 hash names, POD headers/item names
914 `cperl-invalid-face' Trailing whitespace
915
916 Note that in several situations the highlighting tries to inform about
917 possible confusion, such as different colors for function names in
918 declarations depending on what they (do not) override, or special cases
919 m// and s/// which do not do what one would expect them to do.
920
921 Help with best setup of these faces for printout requested (for each of
922 the faces: please specify bold, italic, underline, shadow and box.)
923
924 \(Not finished.)")
925
926 \f
927
928 ;;; Portability stuff:
929
930 (defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
931
932 (defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
933 `(define-key cperl-mode-map
934 ,(if xemacs-key
935 `(if cperl-xemacs-p ,xemacs-key ,emacs-key)
936 emacs-key)
937 ,definition))
938
939 (defvar cperl-del-back-ch
940 (car (append (where-is-internal 'delete-backward-char)
941 (where-is-internal 'backward-delete-char-untabify)))
942 "Character generated by key bound to `delete-backward-char'.")
943
944 (and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
945 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
946
947 (defun cperl-mark-active () (mark)) ; Avoid undefined warning
948 (if cperl-xemacs-p
949 (progn
950 ;; "Active regions" are on: use region only if active
951 ;; "Active regions" are off: use region unconditionally
952 (defun cperl-use-region-p ()
953 (if zmacs-regions (mark) t)))
954 (defun cperl-use-region-p ()
955 (if transient-mark-mode mark-active t))
956 (defun cperl-mark-active () mark-active))
957
958 (defsubst cperl-enable-font-lock ()
959 (or cperl-xemacs-p window-system))
960
961 (defun cperl-putback-char (c) ; Emacs 19
962 (set 'unread-command-events (list c))) ; Avoid undefined warning
963
964 (if (boundp 'unread-command-events)
965 (if cperl-xemacs-p
966 (defun cperl-putback-char (c) ; XEmacs >= 19.12
967 (setq unread-command-events (list (eval '(character-to-event c))))))
968 (defun cperl-putback-char (c) ; XEmacs <= 19.11
969 (set 'unread-command-event (eval '(character-to-event c))))) ; Avoid warnings
970
971 (or (fboundp 'uncomment-region)
972 (defun uncomment-region (beg end)
973 (interactive "r")
974 (comment-region beg end -1)))
975
976 (defvar cperl-do-not-fontify
977 (if (string< emacs-version "19.30")
978 'fontified
979 'lazy-lock)
980 "Text property which inhibits refontification.")
981
982 (defsubst cperl-put-do-not-fontify (from to &optional post)
983 ;; If POST, do not do it with postponed fontification
984 (if (and post cperl-syntaxify-by-font-lock)
985 nil
986 (put-text-property (max (point-min) (1- from))
987 to cperl-do-not-fontify t)))
988
989 (defcustom cperl-mode-hook nil
990 "Hook run by `cperl-mode'."
991 :type 'hook
992 :group 'cperl)
993
994 (defvar cperl-syntax-state nil)
995 (defvar cperl-syntax-done-to nil)
996 (defvar cperl-emacs-can-parse (> (length (save-excursion
997 (parse-partial-sexp (point) (point)))) 9))
998 \f
999 ;; Make customization possible "in reverse"
1000 (defsubst cperl-val (symbol &optional default hairy)
1001 (cond
1002 ((eq (symbol-value symbol) 'null) default)
1003 (cperl-hairy (or hairy t))
1004 (t (symbol-value symbol))))
1005 \f
1006 ;;; Probably it is too late to set these guys already, but it can help later:
1007
1008 ;;;(and cperl-clobber-mode-lists
1009 ;;;(setq auto-mode-alist
1010 ;;; (append '(("\\.\\([pP][Llm]\\|al\\)$" . perl-mode)) auto-mode-alist ))
1011 ;;;(and (boundp 'interpreter-mode-alist)
1012 ;;; (setq interpreter-mode-alist (append interpreter-mode-alist
1013 ;;; '(("miniperl" . perl-mode))))))
1014 (eval-when-compile
1015 (condition-case nil
1016 (require 'imenu)
1017 (error nil))
1018 (condition-case nil
1019 (require 'easymenu)
1020 (error nil))
1021 (condition-case nil
1022 (require 'etags)
1023 (error nil))
1024 (condition-case nil
1025 (require 'timer)
1026 (error nil))
1027 (condition-case nil
1028 (require 'man)
1029 (error nil))
1030 (condition-case nil
1031 (require 'info)
1032 (error nil))
1033 (if (fboundp 'ps-extend-face-list)
1034 (defmacro cperl-ps-extend-face-list (arg)
1035 `(ps-extend-face-list ,arg))
1036 (defmacro cperl-ps-extend-face-list (arg)
1037 `(error "This version of Emacs has no `ps-extend-face-list'")))
1038 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
1039 ;; macros instead of defsubsts don't work on Emacs, so we do the
1040 ;; expansion manually. Any other suggestions?
1041 (require 'cl))
1042
1043 (defvar cperl-mode-abbrev-table nil
1044 "Abbrev table in use in Cperl-mode buffers.")
1045
1046 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
1047
1048 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
1049
1050 (if cperl-mode-map nil
1051 (setq cperl-mode-map (make-sparse-keymap))
1052 (cperl-define-key "{" 'cperl-electric-lbrace)
1053 (cperl-define-key "[" 'cperl-electric-paren)
1054 (cperl-define-key "(" 'cperl-electric-paren)
1055 (cperl-define-key "<" 'cperl-electric-paren)
1056 (cperl-define-key "}" 'cperl-electric-brace)
1057 (cperl-define-key "]" 'cperl-electric-rparen)
1058 (cperl-define-key ")" 'cperl-electric-rparen)
1059 (cperl-define-key ";" 'cperl-electric-semi)
1060 (cperl-define-key ":" 'cperl-electric-terminator)
1061 (cperl-define-key "\C-j" 'newline-and-indent)
1062 (cperl-define-key "\C-c\C-j" 'cperl-linefeed)
1063 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
1064 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1065 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
1066 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1067 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
1068 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
1069 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
1070 (cperl-define-key "\e\C-q" 'cperl-indent-exp) ; Usually not bound
1071 (cperl-define-key [?\C-\M-\|] 'cperl-lineup
1072 [(control meta |)])
1073 ;;(cperl-define-key "\M-q" 'cperl-fill-paragraph)
1074 ;;(cperl-define-key "\e;" 'cperl-indent-for-comment)
1075 (cperl-define-key "\177" 'cperl-electric-backspace)
1076 (cperl-define-key "\t" 'cperl-indent-command)
1077 ;; don't clobber the backspace binding:
1078 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1079 [(control c) (control h) F])
1080 (if (cperl-val 'cperl-clobber-lisp-bindings)
1081 (progn
1082 (cperl-define-key "\C-hf"
1083 ;;(concat (char-to-string help-char) "f") ; does not work
1084 'cperl-info-on-command
1085 [(control h) f])
1086 (cperl-define-key "\C-hv"
1087 ;;(concat (char-to-string help-char) "v") ; does not work
1088 'cperl-get-help
1089 [(control h) v])
1090 (cperl-define-key "\C-c\C-hf"
1091 ;;(concat (char-to-string help-char) "f") ; does not work
1092 (key-binding "\C-hf")
1093 [(control c) (control h) f])
1094 (cperl-define-key "\C-c\C-hv"
1095 ;;(concat (char-to-string help-char) "v") ; does not work
1096 (key-binding "\C-hv")
1097 [(control c) (control h) v]))
1098 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-current-command
1099 [(control c) (control h) f])
1100 (cperl-define-key "\C-c\C-hv"
1101 ;;(concat (char-to-string help-char) "v") ; does not work
1102 'cperl-get-help
1103 [(control c) (control h) v]))
1104 (if (and cperl-xemacs-p
1105 (<= emacs-minor-version 11) (<= emacs-major-version 19))
1106 (progn
1107 ;; substitute-key-definition is usefulness-deenhanced...
1108 (cperl-define-key "\M-q" 'cperl-fill-paragraph)
1109 (cperl-define-key "\e;" 'cperl-indent-for-comment)
1110 (cperl-define-key "\e\C-\\" 'cperl-indent-region))
1111 (substitute-key-definition
1112 'indent-sexp 'cperl-indent-exp
1113 cperl-mode-map global-map)
1114 (substitute-key-definition
1115 'fill-paragraph 'cperl-fill-paragraph
1116 cperl-mode-map global-map)
1117 (substitute-key-definition
1118 'indent-region 'cperl-indent-region
1119 cperl-mode-map global-map)
1120 (substitute-key-definition
1121 'indent-for-comment 'cperl-indent-for-comment
1122 cperl-mode-map global-map)))
1123
1124 (defvar cperl-menu)
1125 (defvar cperl-lazy-installed)
1126 (defvar cperl-old-style nil)
1127 (condition-case nil
1128 (progn
1129 (require 'easymenu)
1130 (easy-menu-define cperl-menu cperl-mode-map "Menu for CPerl mode"
1131 '("Perl"
1132 ["Beginning of function" beginning-of-defun t]
1133 ["End of function" end-of-defun t]
1134 ["Mark function" mark-defun t]
1135 ["Indent expression" cperl-indent-exp t]
1136 ["Fill paragraph/comment" cperl-fill-paragraph t]
1137 "----"
1138 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
1139 ["Invert if/unless/while etc" cperl-invert-if-unless t]
1140 ("Regexp"
1141 ["Beautify" cperl-beautify-regexp
1142 cperl-use-syntax-table-text-property]
1143 ["Beautify one level deep" (cperl-beautify-regexp 1)
1144 cperl-use-syntax-table-text-property]
1145 ["Beautify a group" cperl-beautify-level
1146 cperl-use-syntax-table-text-property]
1147 ["Beautify a group one level deep" (cperl-beautify-level 1)
1148 cperl-use-syntax-table-text-property]
1149 ["Contract a group" cperl-contract-level
1150 cperl-use-syntax-table-text-property]
1151 ["Contract groups" cperl-contract-levels
1152 cperl-use-syntax-table-text-property])
1153 ["Refresh \"hard\" constructions" cperl-find-pods-heres t]
1154 "----"
1155 ["Indent region" cperl-indent-region (cperl-use-region-p)]
1156 ["Comment region" cperl-comment-region (cperl-use-region-p)]
1157 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
1158 "----"
1159 ["Run" mode-compile (fboundp 'mode-compile)]
1160 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
1161 (get-buffer "*compilation*"))]
1162 ["Next error" next-error (get-buffer "*compilation*")]
1163 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
1164 "----"
1165 ["Debugger" cperl-db t]
1166 "----"
1167 ("Tools"
1168 ["Imenu" imenu (fboundp 'imenu)]
1169 ["Insert spaces if needed" cperl-find-bad-style t]
1170 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
1171 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
1172 ["CPerl pretty print (exprmntl)" cperl-ps-print
1173 (fboundp 'ps-extend-face-list)]
1174 ["Imenu on info" cperl-imenu-on-info (featurep 'imenu)]
1175 ("Tags"
1176 ;;; ["Create tags for current file" cperl-etags t]
1177 ;;; ["Add tags for current file" (cperl-etags t) t]
1178 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
1179 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
1180 ;;; ["Create tags for Perl files in (sub)directories"
1181 ;;; (cperl-etags nil 'recursive) t]
1182 ;;; ["Add tags for Perl files in (sub)directories"
1183 ;;; (cperl-etags t 'recursive) t])
1184 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
1185 ["Create tags for current file" (cperl-write-tags nil t) t]
1186 ["Add tags for current file" (cperl-write-tags) t]
1187 ["Create tags for Perl files in directory"
1188 (cperl-write-tags nil t nil t) t]
1189 ["Add tags for Perl files in directory"
1190 (cperl-write-tags nil nil nil t) t]
1191 ["Create tags for Perl files in (sub)directories"
1192 (cperl-write-tags nil t t t) t]
1193 ["Add tags for Perl files in (sub)directories"
1194 (cperl-write-tags nil nil t t) t]))
1195 ("Perl docs"
1196 ["Define word at point" imenu-go-find-at-position
1197 (fboundp 'imenu-go-find-at-position)]
1198 ["Help on function" cperl-info-on-command t]
1199 ["Help on function at point" cperl-info-on-current-command t]
1200 ["Help on symbol at point" cperl-get-help t]
1201 ["Perldoc" cperl-perldoc t]
1202 ["Perldoc on word at point" cperl-perldoc-at-point t]
1203 ["View manpage of POD in this file" cperl-pod-to-manpage t]
1204 ["Auto-help on" cperl-lazy-install
1205 (and (fboundp 'run-with-idle-timer)
1206 (not cperl-lazy-installed))]
1207 ["Auto-help off" (eval '(cperl-lazy-unstall))
1208 (and (fboundp 'run-with-idle-timer)
1209 cperl-lazy-installed)])
1210 ("Toggle..."
1211 ["Auto newline" cperl-toggle-auto-newline t]
1212 ["Electric parens" cperl-toggle-electric t]
1213 ["Electric keywords" cperl-toggle-abbrev t]
1214 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
1215 ["Auto fill" auto-fill-mode t])
1216 ("Indent styles..."
1217 ["CPerl" (cperl-set-style "CPerl") t]
1218 ["PerlStyle" (cperl-set-style "PerlStyle") t]
1219 ["GNU" (cperl-set-style "GNU") t]
1220 ["C++" (cperl-set-style "C++") t]
1221 ["FSF" (cperl-set-style "FSF") t]
1222 ["BSD" (cperl-set-style "BSD") t]
1223 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1224 ["Current" (cperl-set-style "Current") t]
1225 ["Memorized" (cperl-set-style-back) cperl-old-style])
1226 ("Micro-docs"
1227 ["Tips" (describe-variable 'cperl-tips) t]
1228 ["Problems" (describe-variable 'cperl-problems) t]
1229 ["Non-problems" (describe-variable 'cperl-non-problems) t]
1230 ["Speed" (describe-variable 'cperl-speed) t]
1231 ["Praise" (describe-variable 'cperl-praise) t]
1232 ["Faces" (describe-variable 'cperl-tips-faces) t]
1233 ["CPerl mode" (describe-function 'cperl-mode) t]
1234 ["CPerl version"
1235 (message "The version of master-file for this CPerl is %s-emacs"
1236 cperl-version) t]))))
1237 (error nil))
1238
1239 (autoload 'c-macro-expand "cmacexp"
1240 "Display the result of expanding all C macros occurring in the region.
1241 The expansion is entirely correct because it uses the C preprocessor."
1242 t)
1243
1244 (defvar cperl-imenu--function-name-regexp-perl
1245 (concat
1246 "^\\("
1247 "[ \t]*\\(sub\\|package\\)[ \t\n]+\\([a-zA-Z_0-9:']+\\)[ \t]*\\(([^()]*)[ \t]*\\)?"
1248 "\\|"
1249 "=head\\([12]\\)[ \t]+\\([^\n]+\\)$"
1250 "\\)"))
1251
1252 (defvar cperl-outline-regexp
1253 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1254
1255 (defvar cperl-mode-syntax-table nil
1256 "Syntax table in use in Cperl-mode buffers.")
1257
1258 (defvar cperl-string-syntax-table nil
1259 "Syntax table in use in Cperl-mode string-like chunks.")
1260
1261 (if cperl-mode-syntax-table
1262 ()
1263 (setq cperl-mode-syntax-table (make-syntax-table))
1264 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1265 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1266 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1267 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1268 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1269 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1270 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1271 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1272 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1273 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1274 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1275 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
1276 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
1277 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
1278 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
1279 (if cperl-under-as-char
1280 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1281 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1282 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1283 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1284 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1285 (modify-syntax-entry ?# "." cperl-string-syntax-table) ; (?# comment )
1286 )
1287
1288
1289 \f
1290 (defvar cperl-faces-init nil)
1291 ;; Fix for msb.el
1292 (defvar cperl-msb-fixed nil)
1293 ;;;###autoload
1294 (defun cperl-mode ()
1295 "Major mode for editing Perl code.
1296 Expression and list commands understand all C brackets.
1297 Tab indents for Perl code.
1298 Paragraphs are separated by blank lines only.
1299 Delete converts tabs to spaces as it moves back.
1300
1301 Various characters in Perl almost always come in pairs: {}, (), [],
1302 sometimes <>. When the user types the first, she gets the second as
1303 well, with optional special formatting done on {}. (Disabled by
1304 default.) You can always quote (with \\[quoted-insert]) the left
1305 \"paren\" to avoid the expansion. The processing of < is special,
1306 since most the time you mean \"less\". Cperl mode tries to guess
1307 whether you want to type pair <>, and inserts is if it
1308 appropriate. You can set `cperl-electric-parens-string' to the string that
1309 contains the parenths from the above list you want to be electrical.
1310 Electricity of parenths is controlled by `cperl-electric-parens'.
1311 You may also set `cperl-electric-parens-mark' to have electric parens
1312 look for active mark and \"embrace\" a region if possible.'
1313
1314 CPerl mode provides expansion of the Perl control constructs:
1315
1316 if, else, elsif, unless, while, until, continue, do,
1317 for, foreach, formy and foreachmy.
1318
1319 and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1320
1321 The user types the keyword immediately followed by a space, which
1322 causes the construct to be expanded, and the point is positioned where
1323 she is most likely to want to be. eg. when the user types a space
1324 following \"if\" the following appears in the buffer: if () { or if ()
1325 } { } and the cursor is between the parentheses. The user can then
1326 type some boolean expression within the parens. Having done that,
1327 typing \\[cperl-linefeed] places you - appropriately indented - on a
1328 new line between the braces (if you typed \\[cperl-linefeed] in a POD
1329 directive line, then appropriate number of new lines is inserted).
1330
1331 If CPerl decides that you want to insert \"English\" style construct like
1332
1333 bite if angry;
1334
1335 it will not do any expansion. See also help on variable
1336 `cperl-extra-newline-before-brace'. (Note that one can switch the
1337 help message on expansion by setting `cperl-message-electric-keyword'
1338 to nil.)
1339
1340 \\[cperl-linefeed] is a convenience replacement for typing carriage
1341 return. It places you in the next line with proper indentation, or if
1342 you type it inside the inline block of control construct, like
1343
1344 foreach (@lines) {print; print}
1345
1346 and you are on a boundary of a statement inside braces, it will
1347 transform the construct into a multiline and will place you into an
1348 appropriately indented blank line. If you need a usual
1349 `newline-and-indent' behaviour, it is on \\[newline-and-indent],
1350 see documentation on `cperl-electric-linefeed'.
1351
1352 Use \\[cperl-invert-if-unless] to change a construction of the form
1353
1354 if (A) { B }
1355
1356 into
1357
1358 B if A;
1359
1360 \\{cperl-mode-map}
1361
1362 Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1363 \(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1364 on electric space between $ and {, `cperl-electric-parens-string' is
1365 the string that contains parentheses that should be electric in CPerl
1366 \(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
1367 setting `cperl-electric-keywords' enables electric expansion of
1368 control structures in CPerl. `cperl-electric-linefeed' governs which
1369 one of two linefeed behavior is preferable. You can enable all these
1370 options simultaneously (recommended mode of use) by setting
1371 `cperl-hairy' to t. In this case you can switch separate options off
1372 by setting them to `null'. Note that one may undo the extra
1373 whitespace inserted by semis and braces in `auto-newline'-mode by
1374 consequent \\[cperl-electric-backspace].
1375
1376 If your site has perl5 documentation in info format, you can use commands
1377 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1378 These keys run commands `cperl-info-on-current-command' and
1379 `cperl-info-on-command', which one is which is controlled by variable
1380 `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
1381 \(in turn affected by `cperl-hairy').
1382
1383 Even if you have no info-format documentation, short one-liner-style
1384 help is available on \\[cperl-get-help], and one can run perldoc or
1385 man via menu.
1386
1387 It is possible to show this help automatically after some idle time.
1388 This is regulated by variable `cperl-lazy-help-time'. Default with
1389 `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1390 secs idle time . It is also possible to switch this on/off from the
1391 menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
1392
1393 Use \\[cperl-lineup] to vertically lineup some construction - put the
1394 beginning of the region at the start of construction, and make region
1395 span the needed amount of lines.
1396
1397 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1398 `cperl-pod-face', `cperl-pod-head-face' control processing of pod and
1399 here-docs sections. With capable Emaxen results of scan are used
1400 for indentation too, otherwise they are used for highlighting only.
1401
1402 Variables controlling indentation style:
1403 `cperl-tab-always-indent'
1404 Non-nil means TAB in CPerl mode should always reindent the current line,
1405 regardless of where in the line point is when the TAB command is used.
1406 `cperl-indent-left-aligned-comments'
1407 Non-nil means that the comment starting in leftmost column should indent.
1408 `cperl-auto-newline'
1409 Non-nil means automatically newline before and after braces,
1410 and after colons and semicolons, inserted in Perl code. The following
1411 \\[cperl-electric-backspace] will remove the inserted whitespace.
1412 Insertion after colons requires both this variable and
1413 `cperl-auto-newline-after-colon' set.
1414 `cperl-auto-newline-after-colon'
1415 Non-nil means automatically newline even after colons.
1416 Subject to `cperl-auto-newline' setting.
1417 `cperl-indent-level'
1418 Indentation of Perl statements within surrounding block.
1419 The surrounding block's indentation is the indentation
1420 of the line on which the open-brace appears.
1421 `cperl-continued-statement-offset'
1422 Extra indentation given to a substatement, such as the
1423 then-clause of an if, or body of a while, or just a statement continuation.
1424 `cperl-continued-brace-offset'
1425 Extra indentation given to a brace that starts a substatement.
1426 This is in addition to `cperl-continued-statement-offset'.
1427 `cperl-brace-offset'
1428 Extra indentation for line if it starts with an open brace.
1429 `cperl-brace-imaginary-offset'
1430 An open brace following other text is treated as if it the line started
1431 this far to the right of the actual line indentation.
1432 `cperl-label-offset'
1433 Extra indentation for line that is a label.
1434 `cperl-min-label-indent'
1435 Minimal indentation for line that is a label.
1436
1437 Settings for K&R and BSD indentation styles are
1438 `cperl-indent-level' 5 8
1439 `cperl-continued-statement-offset' 5 8
1440 `cperl-brace-offset' -5 -8
1441 `cperl-label-offset' -5 -8
1442
1443 CPerl knows several indentation styles, and may bulk set the
1444 corresponding variables. Use \\[cperl-set-style] to do this. Use
1445 \\[cperl-set-style-back] to restore the memorized preexisting values
1446 \(both available from menu).
1447
1448 If `cperl-indent-level' is 0, the statement after opening brace in
1449 column 0 is indented on
1450 `cperl-brace-offset'+`cperl-continued-statement-offset'.
1451
1452 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
1453 with no args.
1454
1455 DO NOT FORGET to read micro-docs (available from `Perl' menu)
1456 or as help on variables `cperl-tips', `cperl-problems',
1457 `cperl-non-problems', `cperl-praise', `cperl-speed'."
1458 (interactive)
1459 (kill-all-local-variables)
1460 (use-local-map cperl-mode-map)
1461 (if (cperl-val 'cperl-electric-linefeed)
1462 (progn
1463 (local-set-key "\C-J" 'cperl-linefeed)
1464 (local-set-key "\C-C\C-J" 'newline-and-indent)))
1465 (if (and
1466 (cperl-val 'cperl-clobber-lisp-bindings)
1467 (cperl-val 'cperl-info-on-command-no-prompt))
1468 (progn
1469 ;; don't clobber the backspace binding:
1470 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1471 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1472 [(control c) (control h) f])))
1473 (setq major-mode 'cperl-mode)
1474 (setq mode-name "CPerl")
1475 (if (not cperl-mode-abbrev-table)
1476 (let ((prev-a-c abbrevs-changed))
1477 (define-abbrev-table 'cperl-mode-abbrev-table '(
1478 ("if" "if" cperl-electric-keyword 0)
1479 ("elsif" "elsif" cperl-electric-keyword 0)
1480 ("while" "while" cperl-electric-keyword 0)
1481 ("until" "until" cperl-electric-keyword 0)
1482 ("unless" "unless" cperl-electric-keyword 0)
1483 ("else" "else" cperl-electric-else 0)
1484 ("continue" "continue" cperl-electric-else 0)
1485 ("for" "for" cperl-electric-keyword 0)
1486 ("foreach" "foreach" cperl-electric-keyword 0)
1487 ("formy" "formy" cperl-electric-keyword 0)
1488 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1489 ("do" "do" cperl-electric-keyword 0)
1490 ("=pod" "=pod" cperl-electric-pod 0)
1491 ("=over" "=over" cperl-electric-pod 0)
1492 ("=head1" "=head1" cperl-electric-pod 0)
1493 ("=head2" "=head2" cperl-electric-pod 0)
1494 ("pod" "pod" cperl-electric-pod 0)
1495 ("over" "over" cperl-electric-pod 0)
1496 ("head1" "head1" cperl-electric-pod 0)
1497 ("head2" "head2" cperl-electric-pod 0)))
1498 (setq abbrevs-changed prev-a-c)))
1499 (setq local-abbrev-table cperl-mode-abbrev-table)
1500 (abbrev-mode (if (cperl-val 'cperl-electric-keywords) 1 0))
1501 (set-syntax-table cperl-mode-syntax-table)
1502 (make-local-variable 'outline-regexp)
1503 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1504 (setq outline-regexp cperl-outline-regexp)
1505 (make-local-variable 'outline-level)
1506 (setq outline-level 'cperl-outline-level)
1507 (make-local-variable 'paragraph-start)
1508 (setq paragraph-start (concat "^$\\|" page-delimiter))
1509 (make-local-variable 'paragraph-separate)
1510 (setq paragraph-separate paragraph-start)
1511 (make-local-variable 'paragraph-ignore-fill-prefix)
1512 (setq paragraph-ignore-fill-prefix t)
1513 (make-local-variable 'indent-line-function)
1514 (setq indent-line-function 'cperl-indent-line)
1515 (make-local-variable 'require-final-newline)
1516 (setq require-final-newline t)
1517 (make-local-variable 'comment-start)
1518 (setq comment-start "# ")
1519 (make-local-variable 'comment-end)
1520 (setq comment-end "")
1521 (make-local-variable 'comment-column)
1522 (setq comment-column cperl-comment-column)
1523 (make-local-variable 'comment-start-skip)
1524 (setq comment-start-skip "#+ *")
1525 (make-local-variable 'defun-prompt-regexp)
1526 (setq defun-prompt-regexp "^[ \t]*sub[ \t]+\\([^ \t\n{(;]+\\)[ \t]*")
1527 (make-local-variable 'comment-indent-function)
1528 (setq comment-indent-function 'cperl-comment-indent)
1529 (make-local-variable 'parse-sexp-ignore-comments)
1530 (setq parse-sexp-ignore-comments t)
1531 (make-local-variable 'indent-region-function)
1532 (setq indent-region-function 'cperl-indent-region)
1533 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1534 (make-local-variable 'imenu-create-index-function)
1535 (setq imenu-create-index-function
1536 (function cperl-imenu--create-perl-index))
1537 (make-local-variable 'imenu-sort-function)
1538 (setq imenu-sort-function nil)
1539 (make-local-variable 'vc-header-alist)
1540 (set 'vc-header-alist cperl-vc-header-alist) ; Avoid warning
1541 (make-local-variable 'font-lock-defaults)
1542 (setq font-lock-defaults
1543 (cond
1544 ((string< emacs-version "19.30")
1545 '(cperl-font-lock-keywords-2))
1546 ((string< emacs-version "19.33") ; Which one to use?
1547 '((cperl-font-lock-keywords
1548 cperl-font-lock-keywords-1
1549 cperl-font-lock-keywords-2)))
1550 (t
1551 '((cperl-load-font-lock-keywords
1552 cperl-load-font-lock-keywords-1
1553 cperl-load-font-lock-keywords-2)))))
1554 (make-local-variable 'cperl-syntax-state)
1555 (if cperl-use-syntax-table-text-property
1556 (progn
1557 (make-local-variable 'parse-sexp-lookup-properties)
1558 ;; Do not introduce variable if not needed, we check it!
1559 (set 'parse-sexp-lookup-properties t)
1560 ;; Fix broken font-lock:
1561 (or (boundp 'font-lock-unfontify-region-function)
1562 (set 'font-lock-unfontify-region-function
1563 'font-lock-default-unfontify-region))
1564 (make-local-variable 'font-lock-unfontify-region-function)
1565 (set 'font-lock-unfontify-region-function
1566 'cperl-font-lock-unfontify-region-function)
1567 (make-local-variable 'cperl-syntax-done-to)
1568 ;; Another bug: unless font-lock-syntactic-keywords, font-lock
1569 ;; ignores syntax-table text-property. (t) is a hack
1570 ;; to make font-lock think that font-lock-syntactic-keywords
1571 ;; are defined
1572 (make-local-variable 'font-lock-syntactic-keywords)
1573 (setq font-lock-syntactic-keywords
1574 (if cperl-syntaxify-by-font-lock
1575 '(t (cperl-fontify-syntaxically))
1576 '(t)))))
1577 (make-local-variable 'cperl-old-style)
1578 (set (make-local-variable 'normal-auto-fill-function)
1579 #'cperl-do-auto-fill)
1580 (if (cperl-enable-font-lock)
1581 (if (cperl-val 'cperl-font-lock)
1582 (progn (or cperl-faces-init (cperl-init-faces))
1583 (font-lock-mode 1))))
1584 (and (boundp 'msb-menu-cond)
1585 (not cperl-msb-fixed)
1586 (cperl-msb-fix))
1587 (if (featurep 'easymenu)
1588 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
1589 (run-hooks 'cperl-mode-hook)
1590 ;; After hooks since fontification will break this
1591 (if cperl-pod-here-scan
1592 (or ;;(and (boundp 'font-lock-mode)
1593 ;; (eval 'font-lock-mode) ; Avoid warning
1594 ;; (boundp 'font-lock-hot-pass) ; Newer font-lock
1595 cperl-syntaxify-by-font-lock ;;)
1596 (progn (or cperl-faces-init (cperl-init-faces-weak))
1597 (cperl-find-pods-heres)))))
1598 \f
1599 ;; Fix for perldb - make default reasonable
1600 (defun cperl-db ()
1601 (interactive)
1602 (require 'gud)
1603 (perldb (read-from-minibuffer "Run perldb (like this): "
1604 (if (consp gud-perldb-history)
1605 (car gud-perldb-history)
1606 (concat "perl " ;;(file-name-nondirectory
1607 ;; I have problems
1608 ;; in OS/2
1609 ;; otherwise
1610 (buffer-file-name)))
1611 nil nil
1612 '(gud-perldb-history . 1))))
1613 \f
1614 (defun cperl-msb-fix ()
1615 ;; Adds perl files to msb menu, supposes that msb is already loaded
1616 (setq cperl-msb-fixed t)
1617 (let* ((l (length msb-menu-cond))
1618 (last (nth (1- l) msb-menu-cond))
1619 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1620 (handle (1- (nth 1 last))))
1621 (setcdr precdr (list
1622 (list
1623 '(memq major-mode '(cperl-mode perl-mode))
1624 handle
1625 "Perl Files (%d)")
1626 last))))
1627 \f
1628 ;; This is used by indent-for-comment
1629 ;; to decide how much to indent a comment in CPerl code
1630 ;; based on its context. Do fallback if comment is found wrong.
1631
1632 (defvar cperl-wrong-comment)
1633 (defvar cperl-st-cfence '(14)) ; Comment-fence
1634 (defvar cperl-st-sfence '(15)) ; String-fence
1635 (defvar cperl-st-punct '(1))
1636 (defvar cperl-st-word '(2))
1637 (defvar cperl-st-bra '(4 . ?\>))
1638 (defvar cperl-st-ket '(5 . ?\<))
1639
1640
1641 (defun cperl-comment-indent ()
1642 (let ((p (point)) (c (current-column)) was phony)
1643 (if (looking-at "^#") 0 ; Existing comment at bol stays there.
1644 ;; Wrong comment found
1645 (save-excursion
1646 (setq was (cperl-to-comment-or-eol)
1647 phony (eq (get-text-property (point) 'syntax-table)
1648 cperl-st-cfence))
1649 (if phony
1650 (progn
1651 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1652 (if (eq (preceding-char) ?\#)
1653 (forward-char -1))
1654 (setq was nil)))
1655 (if (= (point) p)
1656 (progn
1657 (skip-chars-backward " \t")
1658 (max (1+ (current-column)) ; Else indent at comment column
1659 comment-column))
1660 (if was nil
1661 (insert comment-start)
1662 (backward-char (length comment-start)))
1663 (setq cperl-wrong-comment t)
1664 (indent-to comment-column 1) ; Indent minimum 1
1665 c))))) ; except leave at least one space.
1666
1667 ;;;(defun cperl-comment-indent-fallback ()
1668 ;;; "Is called if the standard comment-search procedure fails.
1669 ;;;Point is at start of real comment."
1670 ;;; (let ((c (current-column)) target cnt prevc)
1671 ;;; (if (= c comment-column) nil
1672 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1673 ;;; (setq target (max (1+ (setq prevc
1674 ;;; (current-column))) ; Else indent at comment column
1675 ;;; comment-column))
1676 ;;; (if (= c comment-column) nil
1677 ;;; (delete-backward-char cnt)
1678 ;;; (while (< prevc target)
1679 ;;; (insert "\t")
1680 ;;; (setq prevc (current-column)))
1681 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1682 ;;; (while (< prevc target)
1683 ;;; (insert " ")
1684 ;;; (setq prevc (current-column)))))))
1685
1686 (defun cperl-indent-for-comment ()
1687 "Substitute for `indent-for-comment' in CPerl."
1688 (interactive)
1689 (let (cperl-wrong-comment)
1690 (indent-for-comment)
1691 (if cperl-wrong-comment
1692 (progn (cperl-to-comment-or-eol)
1693 (forward-char (length comment-start))))))
1694
1695 (defun cperl-comment-region (b e arg)
1696 "Comment or uncomment each line in the region in CPerl mode.
1697 See `comment-region'."
1698 (interactive "r\np")
1699 (let ((comment-start "#"))
1700 (comment-region b e arg)))
1701
1702 (defun cperl-uncomment-region (b e arg)
1703 "Uncomment or comment each line in the region in CPerl mode.
1704 See `comment-region'."
1705 (interactive "r\np")
1706 (let ((comment-start "#"))
1707 (comment-region b e (- arg))))
1708
1709 (defvar cperl-brace-recursing nil)
1710
1711 (defun cperl-electric-brace (arg &optional only-before)
1712 "Insert character and correct line's indentation.
1713 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
1714 place (even in empty line), but not after. If after \")\" and the inserted
1715 char is \"{\", insert extra newline before only if
1716 `cperl-extra-newline-before-brace'."
1717 (interactive "P")
1718 (let (insertpos
1719 (other-end (if (and cperl-electric-parens-mark
1720 (cperl-mark-active)
1721 (< (mark) (point)))
1722 (mark)
1723 nil)))
1724 (if (and other-end
1725 (not cperl-brace-recursing)
1726 (cperl-val 'cperl-electric-parens)
1727 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
1728 ;; Need to insert a matching pair
1729 (progn
1730 (save-excursion
1731 (setq insertpos (point-marker))
1732 (goto-char other-end)
1733 (setq last-command-char ?\{)
1734 (cperl-electric-lbrace arg insertpos))
1735 (forward-char 1))
1736 ;: Check whether we close something "usual" with `}'
1737 (if (and (eq last-command-char ?\})
1738 (not
1739 (condition-case nil
1740 (save-excursion
1741 (up-list (- (prefix-numeric-value arg)))
1742 ;;(cperl-after-block-p (point-min))
1743 (cperl-after-expr-p nil "{;)"))
1744 (error nil))))
1745 ;; Just insert the guy
1746 (self-insert-command (prefix-numeric-value arg))
1747 (if (and (not arg) ; No args, end (of empty line or auto)
1748 (eolp)
1749 (or (and (null only-before)
1750 (save-excursion
1751 (skip-chars-backward " \t")
1752 (bolp)))
1753 (and (eq last-command-char ?\{) ; Do not insert newline
1754 ;; if after ")" and `cperl-extra-newline-before-brace'
1755 ;; is nil, do not insert extra newline.
1756 (not cperl-extra-newline-before-brace)
1757 (save-excursion
1758 (skip-chars-backward " \t")
1759 (eq (preceding-char) ?\))))
1760 (if cperl-auto-newline
1761 (progn (cperl-indent-line) (newline) t) nil)))
1762 (progn
1763 (self-insert-command (prefix-numeric-value arg))
1764 (cperl-indent-line)
1765 (if cperl-auto-newline
1766 (setq insertpos (1- (point))))
1767 (if (and cperl-auto-newline (null only-before))
1768 (progn
1769 (newline)
1770 (cperl-indent-line)))
1771 (save-excursion
1772 (if insertpos (progn (goto-char insertpos)
1773 (search-forward (make-string
1774 1 last-command-char))
1775 (setq insertpos (1- (point)))))
1776 (delete-char -1))))
1777 (if insertpos
1778 (save-excursion
1779 (goto-char insertpos)
1780 (self-insert-command (prefix-numeric-value arg)))
1781 (self-insert-command (prefix-numeric-value arg)))))))
1782
1783 (defun cperl-electric-lbrace (arg &optional end)
1784 "Insert character, correct line's indentation, correct quoting by space."
1785 (interactive "P")
1786 (let (pos after
1787 (cperl-brace-recursing t)
1788 (cperl-auto-newline cperl-auto-newline)
1789 (other-end (or end
1790 (if (and cperl-electric-parens-mark
1791 (cperl-mark-active)
1792 (> (mark) (point)))
1793 (save-excursion
1794 (goto-char (mark))
1795 (point-marker))
1796 nil))))
1797 (and (cperl-val 'cperl-electric-lbrace-space)
1798 (eq (preceding-char) ?$)
1799 (save-excursion
1800 (skip-chars-backward "$")
1801 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
1802 (insert ?\ ))
1803 ;; Check whether we are in comment
1804 (if (and
1805 (save-excursion
1806 (beginning-of-line)
1807 (not (looking-at "[ \t]*#")))
1808 (cperl-after-expr-p nil "{;)"))
1809 nil
1810 (setq cperl-auto-newline nil))
1811 (cperl-electric-brace arg)
1812 (and (cperl-val 'cperl-electric-parens)
1813 (eq last-command-char ?{)
1814 (memq last-command-char
1815 (append cperl-electric-parens-string nil))
1816 (or (if other-end (goto-char (marker-position other-end)))
1817 t)
1818 (setq last-command-char ?} pos (point))
1819 (progn (cperl-electric-brace arg t)
1820 (goto-char pos)))))
1821
1822 (defun cperl-electric-paren (arg)
1823 "Insert a matching pair of parentheses."
1824 (interactive "P")
1825 (let ((beg (save-excursion (beginning-of-line) (point)))
1826 (other-end (if (and cperl-electric-parens-mark
1827 (cperl-mark-active)
1828 (> (mark) (point)))
1829 (save-excursion
1830 (goto-char (mark))
1831 (point-marker))
1832 nil)))
1833 (if (and (cperl-val 'cperl-electric-parens)
1834 (memq last-command-char
1835 (append cperl-electric-parens-string nil))
1836 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
1837 ;;(not (save-excursion (search-backward "#" beg t)))
1838 (if (eq last-command-char ?<)
1839 (progn
1840 (and abbrev-mode ; later it is too late, may be after `for'
1841 (expand-abbrev))
1842 (cperl-after-expr-p nil "{;(,:="))
1843 1))
1844 (progn
1845 (self-insert-command (prefix-numeric-value arg))
1846 (if other-end (goto-char (marker-position other-end)))
1847 (insert (make-string
1848 (prefix-numeric-value arg)
1849 (cdr (assoc last-command-char '((?{ .?})
1850 (?[ . ?])
1851 (?( . ?))
1852 (?< . ?>))))))
1853 (forward-char (- (prefix-numeric-value arg))))
1854 (self-insert-command (prefix-numeric-value arg)))))
1855
1856 (defun cperl-electric-rparen (arg)
1857 "Insert a matching pair of parentheses if marking is active.
1858 If not, or if we are not at the end of marking range, would self-insert."
1859 (interactive "P")
1860 (let ((beg (save-excursion (beginning-of-line) (point)))
1861 (other-end (if (and cperl-electric-parens-mark
1862 (cperl-val 'cperl-electric-parens)
1863 (memq last-command-char
1864 (append cperl-electric-parens-string nil))
1865 (cperl-mark-active)
1866 (< (mark) (point)))
1867 (mark)
1868 nil))
1869 p)
1870 (if (and other-end
1871 (cperl-val 'cperl-electric-parens)
1872 (memq last-command-char '( ?\) ?\] ?\} ?\> ))
1873 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
1874 ;;(not (save-excursion (search-backward "#" beg t)))
1875 )
1876 (progn
1877 (self-insert-command (prefix-numeric-value arg))
1878 (setq p (point))
1879 (if other-end (goto-char other-end))
1880 (insert (make-string
1881 (prefix-numeric-value arg)
1882 (cdr (assoc last-command-char '((?\} . ?\{)
1883 (?\] . ?\[)
1884 (?\) . ?\()
1885 (?\> . ?\<))))))
1886 (goto-char (1+ p)))
1887 (self-insert-command (prefix-numeric-value arg)))))
1888
1889 (defun cperl-electric-keyword ()
1890 "Insert a construction appropriate after a keyword.
1891 Help message may be switched off by setting `cperl-message-electric-keyword'
1892 to nil."
1893 (let ((beg (save-excursion (beginning-of-line) (point)))
1894 (dollar (and (eq last-command-char ?$)
1895 (eq this-command 'self-insert-command)))
1896 (delete (and (memq last-command-char '(?\ ?\n ?\t ?\f))
1897 (memq this-command '(self-insert-command newline))))
1898 my do)
1899 (and (save-excursion
1900 (condition-case nil
1901 (progn
1902 (backward-sexp 1)
1903 (setq do (looking-at "do\\>")))
1904 (error nil))
1905 (cperl-after-expr-p nil "{;:"))
1906 (save-excursion
1907 (not
1908 (re-search-backward
1909 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
1910 beg t)))
1911 (save-excursion (or (not (re-search-backward "^=" nil t))
1912 (or
1913 (looking-at "=cut")
1914 (and cperl-use-syntax-table-text-property
1915 (not (eq (get-text-property (point)
1916 'syntax-type)
1917 'pod))))))
1918 (progn
1919 (and (eq (preceding-char) ?y)
1920 (progn ; "foreachmy"
1921 (forward-char -2)
1922 (insert " ")
1923 (forward-char 2)
1924 (setq my t dollar t
1925 delete
1926 (memq this-command '(self-insert-command newline)))))
1927 (and dollar (insert " $"))
1928 (cperl-indent-line)
1929 ;;(insert " () {\n}")
1930 (cond
1931 (cperl-extra-newline-before-brace
1932 (insert (if do "\n" " ()\n"))
1933 (insert "{")
1934 (cperl-indent-line)
1935 (insert "\n")
1936 (cperl-indent-line)
1937 (insert "\n}")
1938 (and do (insert " while ();")))
1939 (t
1940 (insert (if do " {\n} while ();" " () {\n}")))
1941 )
1942 (or (looking-at "[ \t]\\|$") (insert " "))
1943 (cperl-indent-line)
1944 (if dollar (progn (search-backward "$")
1945 (if my
1946 (forward-char 1)
1947 (delete-char 1)))
1948 (search-backward ")"))
1949 (if delete
1950 (cperl-putback-char cperl-del-back-ch))
1951 (if cperl-message-electric-keyword
1952 (message "Precede char by C-q to avoid expansion"))))))
1953
1954 (defun cperl-ensure-newlines (n &optional pos)
1955 "Make sure there are N newlines after the point."
1956 (or pos (setq pos (point)))
1957 (if (looking-at "\n")
1958 (forward-char 1)
1959 (insert "\n"))
1960 (if (> n 1)
1961 (cperl-ensure-newlines (1- n) pos)
1962 (goto-char pos)))
1963
1964 (defun cperl-electric-pod ()
1965 "Insert a POD chunk appropriate after a =POD directive."
1966 (let ((delete (and (memq last-command-char '(?\ ?\n ?\t ?\f))
1967 (memq this-command '(self-insert-command newline))))
1968 head1 notlast name p really-delete over)
1969 (and (save-excursion
1970 (forward-word -1)
1971 (and
1972 (eq (preceding-char) ?=)
1973 (progn
1974 (setq head1 (looking-at "head1\\>[ \t]*$"))
1975 (setq over (and (looking-at "over\\>[ \t]*$")
1976 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
1977 (forward-char -1)
1978 (bolp))
1979 (or
1980 (get-text-property (point) 'in-pod)
1981 (cperl-after-expr-p nil "{;:")
1982 (and (re-search-backward
1983 ;; "\\(\\`\n?\\|\n\n\\)=\\sw+"
1984 "\\(\\`\n?\\|^\n\\)=\\sw+"
1985 (point-min) t)
1986 (not (or
1987 (looking-at "=cut")
1988 (and cperl-use-syntax-table-text-property
1989 (not (eq (get-text-property (point) 'syntax-type)
1990 'pod)))))))))
1991 (progn
1992 (save-excursion
1993 (setq notlast (re-search-forward "^\n=" nil t)))
1994 (or notlast
1995 (progn
1996 (insert "\n\n=cut")
1997 (cperl-ensure-newlines 2)
1998 (forward-word -2)
1999 (if (and head1
2000 (not
2001 (save-excursion
2002 (forward-char -1)
2003 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2004 nil t)))) ; Only one
2005 (progn
2006 (forward-word 1)
2007 (setq name (file-name-sans-extension
2008 (file-name-nondirectory (buffer-file-name)))
2009 p (point))
2010 (insert " NAME\n\n" name
2011 " - \n\n=head1 SYNOPSIS\n\n\n\n"
2012 "=head1 DESCRIPTION")
2013 (cperl-ensure-newlines 4)
2014 (goto-char p)
2015 (forward-word 2)
2016 (end-of-line)
2017 (setq really-delete t))
2018 (forward-word 1))))
2019 (if over
2020 (progn
2021 (setq p (point))
2022 (insert "\n\n=item \n\n\n\n"
2023 "=back")
2024 (cperl-ensure-newlines 2)
2025 (goto-char p)
2026 (forward-word 1)
2027 (end-of-line)
2028 (setq really-delete t)))
2029 (if (and delete really-delete)
2030 (cperl-putback-char cperl-del-back-ch))))))
2031
2032 (defun cperl-electric-else ()
2033 "Insert a construction appropriate after a keyword.
2034 Help message may be switched off by setting `cperl-message-electric-keyword'
2035 to nil."
2036 (let ((beg (save-excursion (beginning-of-line) (point))))
2037 (and (save-excursion
2038 (backward-sexp 1)
2039 (cperl-after-expr-p nil "{;:"))
2040 (save-excursion
2041 (not
2042 (re-search-backward
2043 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
2044 beg t)))
2045 (save-excursion (or (not (re-search-backward "^=" nil t))
2046 (looking-at "=cut")
2047 (and cperl-use-syntax-table-text-property
2048 (not (eq (get-text-property (point)
2049 'syntax-type)
2050 'pod)))))
2051 (progn
2052 (cperl-indent-line)
2053 ;;(insert " {\n\n}")
2054 (cond
2055 (cperl-extra-newline-before-brace
2056 (insert "\n")
2057 (insert "{")
2058 (cperl-indent-line)
2059 (insert "\n\n}"))
2060 (t
2061 (insert " {\n\n}"))
2062 )
2063 (or (looking-at "[ \t]\\|$") (insert " "))
2064 (cperl-indent-line)
2065 (forward-line -1)
2066 (cperl-indent-line)
2067 (cperl-putback-char cperl-del-back-ch)
2068 (setq this-command 'cperl-electric-else)
2069 (if cperl-message-electric-keyword
2070 (message "Precede char by C-q to avoid expansion"))))))
2071
2072 (defun cperl-linefeed ()
2073 "Go to end of line, open a new line and indent appropriately.
2074 If in POD, insert appropriate lines."
2075 (interactive)
2076 (let ((beg (save-excursion (beginning-of-line) (point)))
2077 (end (save-excursion (end-of-line) (point)))
2078 (pos (point)) start over cut res)
2079 (if (and ; Check if we need to split:
2080 ; i.e., on a boundary and inside "{...}"
2081 (save-excursion (cperl-to-comment-or-eol)
2082 (>= (point) pos)) ; Not in a comment
2083 (or (save-excursion
2084 (skip-chars-backward " \t" beg)
2085 (forward-char -1)
2086 (looking-at "[;{]")) ; After { or ; + spaces
2087 (looking-at "[ \t]*}") ; Before }
2088 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2089 (save-excursion
2090 (and
2091 (eq (car (parse-partial-sexp pos end -1)) -1)
2092 ; Leave the level of parens
2093 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2094 ; Are at end
2095 (cperl-after-block-p (point-min))
2096 (progn
2097 (backward-sexp 1)
2098 (setq start (point-marker))
2099 (<= start pos))))) ; Redundant? Are after the
2100 ; start of parens group.
2101 (progn
2102 (skip-chars-backward " \t")
2103 (or (memq (preceding-char) (append ";{" nil))
2104 (insert ";"))
2105 (insert "\n")
2106 (forward-line -1)
2107 (cperl-indent-line)
2108 (goto-char start)
2109 (or (looking-at "{[ \t]*$") ; If there is a statement
2110 ; before, move it to separate line
2111 (progn
2112 (forward-char 1)
2113 (insert "\n")
2114 (cperl-indent-line)))
2115 (forward-line 1) ; We are on the target line
2116 (cperl-indent-line)
2117 (beginning-of-line)
2118 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2119 ; after, move it to separate line
2120 (progn
2121 (end-of-line)
2122 (search-backward "}" beg)
2123 (skip-chars-backward " \t")
2124 (or (memq (preceding-char) (append ";{" nil))
2125 (insert ";"))
2126 (insert "\n")
2127 (cperl-indent-line)
2128 (forward-line -1)))
2129 (forward-line -1) ; We are on the line before target
2130 (end-of-line)
2131 (newline-and-indent))
2132 (end-of-line) ; else - no splitting
2133 (cond
2134 ((and (looking-at "\n[ \t]*{$")
2135 (save-excursion
2136 (skip-chars-backward " \t")
2137 (eq (preceding-char) ?\)))) ; Probably if () {} group
2138 ; with an extra newline.
2139 (forward-line 2)
2140 (cperl-indent-line))
2141 ((save-excursion ; In POD header
2142 (forward-paragraph -1)
2143 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2144 ;; We are after \n now, so look for the rest
2145 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
2146 (progn
2147 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2148 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2149 t)))
2150 (if (and over
2151 (progn
2152 (forward-paragraph -1)
2153 (forward-word 1)
2154 (setq pos (point))
2155 (setq cut (buffer-substring (point)
2156 (save-excursion
2157 (end-of-line)
2158 (point))))
2159 (delete-char (- (save-excursion (end-of-line) (point))
2160 (point)))
2161 (setq res (expand-abbrev))
2162 (save-excursion
2163 (goto-char pos)
2164 (insert cut))
2165 res))
2166 nil
2167 (cperl-ensure-newlines (if cut 2 4))
2168 (forward-line 2)))
2169 ((get-text-property (point) 'in-pod) ; In POD section
2170 (cperl-ensure-newlines 4)
2171 (forward-line 2))
2172 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2173 (forward-line 1)
2174 (cperl-indent-line))
2175 (t
2176 (newline-and-indent))))))
2177
2178 (defun cperl-electric-semi (arg)
2179 "Insert character and correct line's indentation."
2180 (interactive "P")
2181 (if cperl-auto-newline
2182 (cperl-electric-terminator arg)
2183 (self-insert-command (prefix-numeric-value arg))
2184 (if cperl-autoindent-on-semi
2185 (cperl-indent-line))))
2186
2187 (defun cperl-electric-terminator (arg)
2188 "Insert character and correct line's indentation."
2189 (interactive "P")
2190 (let (insertpos (end (point))
2191 (auto (and cperl-auto-newline
2192 (or (not (eq last-command-char ?:))
2193 cperl-auto-newline-after-colon))))
2194 (if (and ;;(not arg)
2195 (eolp)
2196 (not (save-excursion
2197 (beginning-of-line)
2198 (skip-chars-forward " \t")
2199 (or
2200 ;; Ignore in comment lines
2201 (= (following-char) ?#)
2202 ;; Colon is special only after a label
2203 ;; So quickly rule out most other uses of colon
2204 ;; and do no indentation for them.
2205 (and (eq last-command-char ?:)
2206 (save-excursion
2207 (forward-word 1)
2208 (skip-chars-forward " \t")
2209 (and (< (point) end)
2210 (progn (goto-char (- end 1))
2211 (not (looking-at ":"))))))
2212 (progn
2213 (beginning-of-defun)
2214 (let ((pps (parse-partial-sexp (point) end)))
2215 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2216 (progn
2217 (self-insert-command (prefix-numeric-value arg))
2218 ;;(forward-char -1)
2219 (if auto (setq insertpos (point-marker)))
2220 ;;(forward-char 1)
2221 (cperl-indent-line)
2222 (if auto
2223 (progn
2224 (newline)
2225 (cperl-indent-line)))
2226 (save-excursion
2227 (if insertpos (goto-char (1- (marker-position insertpos)))
2228 (forward-char -1))
2229 (delete-char 1))))
2230 (if insertpos
2231 (save-excursion
2232 (goto-char insertpos)
2233 (self-insert-command (prefix-numeric-value arg)))
2234 (self-insert-command (prefix-numeric-value arg)))))
2235
2236 (defun cperl-electric-backspace (arg)
2237 "Backspace-untabify, or remove the whitespace around the point inserted
2238 by an electric key."
2239 (interactive "p")
2240 (if (and cperl-auto-newline
2241 (memq last-command '(cperl-electric-semi
2242 cperl-electric-terminator
2243 cperl-electric-lbrace))
2244 (memq (preceding-char) '(?\ ?\t ?\n)))
2245 (let (p)
2246 (if (eq last-command 'cperl-electric-lbrace)
2247 (skip-chars-forward " \t\n"))
2248 (setq p (point))
2249 (skip-chars-backward " \t\n")
2250 (delete-region (point) p))
2251 (and (eq last-command 'cperl-electric-else)
2252 ;; We are removing the whitespace *inside* cperl-electric-else
2253 (setq this-command 'cperl-electric-else-really))
2254 (if (and cperl-auto-newline
2255 (eq last-command 'cperl-electric-else-really)
2256 (memq (preceding-char) '(?\ ?\t ?\n)))
2257 (let (p)
2258 (skip-chars-forward " \t\n")
2259 (setq p (point))
2260 (skip-chars-backward " \t\n")
2261 (delete-region (point) p))
2262 (backward-delete-char-untabify arg))))
2263
2264 (defun cperl-inside-parens-p ()
2265 (condition-case ()
2266 (save-excursion
2267 (save-restriction
2268 (narrow-to-region (point)
2269 (progn (beginning-of-defun) (point)))
2270 (goto-char (point-max))
2271 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2272 (error nil)))
2273 \f
2274 (defun cperl-indent-command (&optional whole-exp)
2275 "Indent current line as Perl code, or in some cases insert a tab character.
2276 If `cperl-tab-always-indent' is non-nil (the default), always indent current
2277 line. Otherwise, indent the current line only if point is at the left margin
2278 or in the line's indentation; otherwise insert a tab.
2279
2280 A numeric argument, regardless of its value,
2281 means indent rigidly all the lines of the expression starting after point
2282 so that this line becomes properly indented.
2283 The relative indentation among the lines of the expression are preserved."
2284 (interactive "P")
2285 (cperl-update-syntaxification (point) (point))
2286 (if whole-exp
2287 ;; If arg, always indent this line as Perl
2288 ;; and shift remaining lines of expression the same amount.
2289 (let ((shift-amt (cperl-indent-line))
2290 beg end)
2291 (save-excursion
2292 (if cperl-tab-always-indent
2293 (beginning-of-line))
2294 (setq beg (point))
2295 (forward-sexp 1)
2296 (setq end (point))
2297 (goto-char beg)
2298 (forward-line 1)
2299 (setq beg (point)))
2300 (if (and shift-amt (> end beg))
2301 (indent-code-rigidly beg end shift-amt "#")))
2302 (if (and (not cperl-tab-always-indent)
2303 (save-excursion
2304 (skip-chars-backward " \t")
2305 (not (bolp))))
2306 (insert-tab)
2307 (cperl-indent-line))))
2308
2309 (defun cperl-indent-line (&optional parse-data)
2310 "Indent current line as Perl code.
2311 Return the amount the indentation changed by."
2312 (let (indent i beg shift-amt
2313 (case-fold-search nil)
2314 (pos (- (point-max) (point))))
2315 (setq indent (cperl-calculate-indent parse-data)
2316 i indent)
2317 (beginning-of-line)
2318 (setq beg (point))
2319 (cond ((or (eq indent nil) (eq indent t))
2320 (setq indent (current-indentation) i nil))
2321 ;;((eq indent t) ; Never?
2322 ;; (setq indent (cperl-calculate-indent-within-comment)))
2323 ;;((looking-at "[ \t]*#")
2324 ;; (setq indent 0))
2325 (t
2326 (skip-chars-forward " \t")
2327 (if (listp indent) (setq indent (car indent)))
2328 (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2329 (and (> indent 0)
2330 (setq indent (max cperl-min-label-indent
2331 (+ indent cperl-label-offset)))))
2332 ((= (following-char) ?})
2333 (setq indent (- indent cperl-indent-level)))
2334 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2335 (setq indent (+ indent cperl-close-paren-offset)))
2336 ((= (following-char) ?{)
2337 (setq indent (+ indent cperl-brace-offset))))))
2338 (skip-chars-forward " \t")
2339 (setq shift-amt (and i (- indent (current-column))))
2340 (if (or (not shift-amt)
2341 (zerop shift-amt))
2342 (if (> (- (point-max) pos) (point))
2343 (goto-char (- (point-max) pos)))
2344 (delete-region beg (point))
2345 (indent-to indent)
2346 ;; If initial point was within line's indentation,
2347 ;; position after the indentation. Else stay at same point in text.
2348 (if (> (- (point-max) pos) (point))
2349 (goto-char (- (point-max) pos))))
2350 shift-amt))
2351
2352 (defun cperl-after-label ()
2353 ;; Returns true if the point is after label. Does not do save-excursion.
2354 (and (eq (preceding-char) ?:)
2355 (memq (char-syntax (char-after (- (point) 2)))
2356 '(?w ?_))
2357 (progn
2358 (backward-sexp)
2359 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2360
2361 (defun cperl-get-state (&optional parse-start start-state)
2362 ;; returns list (START STATE DEPTH PRESTART),
2363 ;; START is a good place to start parsing, or equal to
2364 ;; PARSE-START if preset,
2365 ;; STATE is what is returned by `parse-partial-sexp'.
2366 ;; DEPTH is true is we are immediately after end of block
2367 ;; which contains START.
2368 ;; PRESTART is the position basing on which START was found.
2369 (save-excursion
2370 (let ((start-point (point)) depth state start prestart)
2371 (if (and parse-start
2372 (<= parse-start start-point))
2373 (goto-char parse-start)
2374 (beginning-of-defun)
2375 (setq start-state nil))
2376 (setq prestart (point))
2377 (if start-state nil
2378 ;; Try to go out, if sub is not on the outermost level
2379 (while (< (point) start-point)
2380 (setq start (point) parse-start start depth nil
2381 state (parse-partial-sexp start start-point -1))
2382 (if (> (car state) -1) nil
2383 ;; The current line could start like }}}, so the indentation
2384 ;; corresponds to a different level than what we reached
2385 (setq depth t)
2386 (beginning-of-line 2))) ; Go to the next line.
2387 (if start (goto-char start))) ; Not at the start of file
2388 (setq start (point))
2389 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2390 (list start state depth prestart))))
2391
2392 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
2393 ;; Positions is before ?\{. Checks whether it starts a block.
2394 ;; No save-excursion!
2395 (cperl-backward-to-noncomment (point-min))
2396 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
2397 ; Label may be mixed up with `$blah :'
2398 (save-excursion (cperl-after-label))
2399 (and (memq (char-syntax (preceding-char)) '(?w ?_))
2400 (progn
2401 (backward-sexp)
2402 ;; Need take into account `bless', `return', `tr',...
2403 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
2404 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
2405 (progn
2406 (skip-chars-backward " \t\n\f")
2407 (and (memq (char-syntax (preceding-char)) '(?w ?_))
2408 (progn
2409 (backward-sexp)
2410 (looking-at
2411 "sub[ \t]+[a-zA-Z0-9_:]+[ \t\n\f]*\\(([^()]*)[ \t\n\f]*\\)?[#{]")))))))))
2412
2413 (defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2414
2415 (defun cperl-calculate-indent (&optional parse-data) ; was parse-start
2416 "Return appropriate indentation for current line as Perl code.
2417 In usual case returns an integer: the column to indent to.
2418 Returns nil if line starts inside a string, t if in a comment.
2419
2420 Will not correct the indentation for labels, but will correct it for braces
2421 and closing parentheses and brackets.."
2422 (save-excursion
2423 (if (or
2424 (and (memq (get-text-property (point) 'syntax-type)
2425 '(pod here-doc here-doc-delim format))
2426 (not (get-text-property (point) 'indentable)))
2427 ;; before start of POD - whitespace found since do not have 'pod!
2428 (and (looking-at "[ \t]*\n=")
2429 (error "Spaces before pod section!"))
2430 (and (not cperl-indent-left-aligned-comments)
2431 (looking-at "^#")))
2432 nil
2433 (beginning-of-line)
2434 (let ((indent-point (point))
2435 (char-after (save-excursion
2436 (skip-chars-forward " \t")
2437 (following-char)))
2438 (in-pod (get-text-property (point) 'in-pod))
2439 (pre-indent-point (point))
2440 p prop look-prop is-block delim)
2441 (cond
2442 (in-pod
2443 ;; In the verbatim part, probably code example. What to do???
2444 )
2445 (t
2446 (save-excursion
2447 ;; Not in pod
2448 (cperl-backward-to-noncomment nil)
2449 (setq p (max (point-min) (1- (point)))
2450 prop (get-text-property p 'syntax-type)
2451 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2452 'syntax-type))
2453 (if (memq prop '(pod here-doc format here-doc-delim))
2454 (progn
2455 (goto-char (or (previous-single-property-change p look-prop)
2456 (point-min)))
2457 (beginning-of-line)
2458 (setq pre-indent-point (point)))))))
2459 (goto-char pre-indent-point)
2460 (let* ((case-fold-search nil)
2461 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
2462 (start (or (nth 2 parse-data)
2463 (nth 0 s-s)))
2464 (state (nth 1 s-s))
2465 (containing-sexp (car (cdr state)))
2466 old-indent)
2467 (if (and
2468 ;;containing-sexp ;; We are buggy at toplevel :-(
2469 parse-data)
2470 (progn
2471 (setcar parse-data pre-indent-point)
2472 (setcar (cdr parse-data) state)
2473 (or (nth 2 parse-data)
2474 (setcar (cddr parse-data) start))
2475 ;; Before this point: end of statement
2476 (setq old-indent (nth 3 parse-data))))
2477 (cond ((get-text-property (point) 'indentable)
2478 ;; indent to just after the surrounding open,
2479 ;; skip blanks if we do not close the expression.
2480 (goto-char (1+ (previous-single-property-change (point) 'indentable)))
2481 (or (memq char-after (append ")]}" nil))
2482 (looking-at "[ \t]*\\(#\\|$\\)")
2483 (skip-chars-forward " \t"))
2484 (current-column))
2485 ((or (nth 3 state) (nth 4 state))
2486 ;; return nil or t if should not change this line
2487 (nth 4 state))
2488 ;; XXXX Do we need to special-case this?
2489 ((null containing-sexp)
2490 ;; Line is at top level. May be data or function definition,
2491 ;; or may be function argument declaration.
2492 ;; Indent like the previous top level line
2493 ;; unless that ends in a closeparen without semicolon,
2494 ;; in which case this line is the first argument decl.
2495 (skip-chars-forward " \t")
2496 (+ (save-excursion
2497 (goto-char start)
2498 (- (current-indentation)
2499 (if (nth 2 s-s) cperl-indent-level 0)))
2500 (if (= char-after ?{) cperl-continued-brace-offset 0)
2501 (progn
2502 (cperl-backward-to-noncomment (or old-indent (point-min)))
2503 ;; Look at previous line that's at column 0
2504 ;; to determine whether we are in top-level decls
2505 ;; or function's arg decls. Set basic-indent accordingly.
2506 ;; Now add a little if this is a continuation line.
2507 (if (or (bobp)
2508 (eq (point) old-indent) ; old-indent was at comment
2509 (eq (preceding-char) ?\;)
2510 ;; Had ?\) too
2511 (and (eq (preceding-char) ?\})
2512 (cperl-after-block-and-statement-beg
2513 (point-min))) ; Was start - too close
2514 (memq char-after (append ")]}" nil))
2515 (and (eq (preceding-char) ?\:) ; label
2516 (progn
2517 (forward-sexp -1)
2518 (skip-chars-backward " \t")
2519 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:"))))
2520 (progn
2521 (if (and parse-data
2522 (not (eq char-after ?\C-j)))
2523 (setcdr (cddr parse-data)
2524 (list pre-indent-point)))
2525 0)
2526 cperl-continued-statement-offset))))
2527 ((not
2528 (or (setq is-block
2529 (and (setq delim (= (char-after containing-sexp) ?{))
2530 (save-excursion ; Is it a hash?
2531 (goto-char containing-sexp)
2532 (cperl-block-p))))
2533 cperl-indent-parens-as-block))
2534 ;; group is an expression, not a block:
2535 ;; indent to just after the surrounding open parens,
2536 ;; skip blanks if we do not close the expression.
2537 (goto-char (1+ containing-sexp))
2538 (or (memq char-after (append ")]}" nil))
2539 (looking-at "[ \t]*\\(#\\|$\\)")
2540 (skip-chars-forward " \t"))
2541 (current-column))
2542 ((progn
2543 ;; Containing-expr starts with \{. Check whether it is a hash.
2544 (goto-char containing-sexp)
2545 (not (cperl-block-p)))
2546 (goto-char (1+ containing-sexp))
2547 (or (memq char-after
2548 (append (if delim "}" ")]}") nil))
2549 (looking-at "[ \t]*\\(#\\|$\\)")
2550 (skip-chars-forward " \t"))
2551 (+ (current-column)
2552 (if (and delim
2553 (eq char-after ?\}))
2554 ;; Correct indentation of trailing ?\}
2555 (+ cperl-indent-level cperl-close-paren-offset)
2556 0)))
2557 ;;; ((and (/= (char-after containing-sexp) ?{)
2558 ;;; (not cperl-indent-parens-as-block))
2559 ;;; ;; line is expression, not statement:
2560 ;;; ;; indent to just after the surrounding open,
2561 ;;; ;; skip blanks if we do not close the expression.
2562 ;;; (goto-char (1+ containing-sexp))
2563 ;;; (or (memq char-after (append ")]}" nil))
2564 ;;; (looking-at "[ \t]*\\(#\\|$\\)")
2565 ;;; (skip-chars-forward " \t"))
2566 ;;; (current-column))
2567 ;;; ((progn
2568 ;;; ;; Containing-expr starts with \{. Check whether it is a hash.
2569 ;;; (goto-char containing-sexp)
2570 ;;; (and (not (cperl-block-p))
2571 ;;; (not cperl-indent-parens-as-block)))
2572 ;;; (goto-char (1+ containing-sexp))
2573 ;;; (or (eq char-after ?\})
2574 ;;; (looking-at "[ \t]*\\(#\\|$\\)")
2575 ;;; (skip-chars-forward " \t"))
2576 ;;; (+ (current-column) ; Correct indentation of trailing ?\}
2577 ;;; (if (eq char-after ?\}) (+ cperl-indent-level
2578 ;;; cperl-close-paren-offset)
2579 ;;; 0)))
2580 (t
2581 ;; Statement level. Is it a continuation or a new statement?
2582 ;; Find previous non-comment character.
2583 (goto-char pre-indent-point)
2584 (cperl-backward-to-noncomment containing-sexp)
2585 ;; Back up over label lines, since they don't
2586 ;; affect whether our line is a continuation.
2587 ;; (Had \, too)
2588 (while ;;(or (eq (preceding-char) ?\,)
2589 (and (eq (preceding-char) ?:)
2590 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2591 (memq (char-syntax (char-after (- (point) 2)))
2592 '(?w ?_))))
2593 ;;)
2594 (if (eq (preceding-char) ?\,)
2595 ;; Will go to beginning of line, essentially.
2596 ;; Will ignore embedded sexpr XXXX.
2597 (cperl-backward-to-start-of-continued-exp containing-sexp))
2598 (beginning-of-line)
2599 (cperl-backward-to-noncomment containing-sexp))
2600 ;; Now we get the answer.
2601 (if (not (or (eq (1- (point)) containing-sexp)
2602 (memq (preceding-char)
2603 (append (if is-block " ;{" " ,;{") '(nil)))
2604 (and (eq (preceding-char) ?\})
2605 (cperl-after-block-and-statement-beg
2606 containing-sexp))))
2607 ;; This line is continuation of preceding line's statement;
2608 ;; indent `cperl-continued-statement-offset' more than the
2609 ;; previous line of the statement.
2610 ;;
2611 ;; There might be a label on this line, just
2612 ;; consider it bad style and ignore it.
2613 (progn
2614 (cperl-backward-to-start-of-continued-exp containing-sexp)
2615 (+ (if (memq char-after (append "}])" nil))
2616 0 ; Closing parenth
2617 cperl-continued-statement-offset)
2618 (if (or is-block
2619 (not delim)
2620 (not (eq char-after ?\})))
2621 0
2622 ;; Now it is a hash reference
2623 (+ cperl-indent-level cperl-close-paren-offset))
2624 (if (looking-at "\\w+[ \t]*:")
2625 (if (> (current-indentation) cperl-min-label-indent)
2626 (- (current-indentation) cperl-label-offset)
2627 ;; Do not move `parse-data', this should
2628 ;; be quick anyway (this comment comes
2629 ;;from different location):
2630 (cperl-calculate-indent))
2631 (current-column))
2632 (if (eq char-after ?\{)
2633 cperl-continued-brace-offset 0)))
2634 ;; This line starts a new statement.
2635 ;; Position following last unclosed open.
2636 (goto-char containing-sexp)
2637 ;; Is line first statement after an open-brace?
2638 (or
2639 ;; If no, find that first statement and indent like
2640 ;; it. If the first statement begins with label, do
2641 ;; not believe when the indentation of the label is too
2642 ;; small.
2643 (save-excursion
2644 (forward-char 1)
2645 (setq old-indent (current-indentation))
2646 (let ((colon-line-end 0))
2647 (while (progn (skip-chars-forward " \t\n")
2648 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]"))
2649 ;; Skip over comments and labels following openbrace.
2650 (cond ((= (following-char) ?\#)
2651 (forward-line 1))
2652 ;; label:
2653 (t
2654 (save-excursion (end-of-line)
2655 (setq colon-line-end (point)))
2656 (search-forward ":"))))
2657 ;; The first following code counts
2658 ;; if it is before the line we want to indent.
2659 (and (< (point) indent-point)
2660 (if (> colon-line-end (point)) ; After label
2661 (if (> (current-indentation)
2662 cperl-min-label-indent)
2663 (- (current-indentation) cperl-label-offset)
2664 ;; Do not believe: `max' is involved
2665 (+ old-indent cperl-indent-level))
2666 (current-column)))))
2667 ;; If no previous statement,
2668 ;; indent it relative to line brace is on.
2669 ;; For open brace in column zero, don't let statement
2670 ;; start there too. If cperl-indent-level is zero,
2671 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
2672 ;; For open-braces not the first thing in a line,
2673 ;; add in cperl-brace-imaginary-offset.
2674
2675 ;; If first thing on a line: ?????
2676 (+ (if (and (bolp) (zerop cperl-indent-level))
2677 (+ cperl-brace-offset cperl-continued-statement-offset)
2678 cperl-indent-level)
2679 (if (or is-block
2680 (not delim)
2681 (not (eq char-after ?\})))
2682 0
2683 ;; Now it is a hash reference
2684 (+ cperl-indent-level cperl-close-paren-offset))
2685 ;; Move back over whitespace before the openbrace.
2686 ;; If openbrace is not first nonwhite thing on the line,
2687 ;; add the cperl-brace-imaginary-offset.
2688 (progn (skip-chars-backward " \t")
2689 (if (bolp) 0 cperl-brace-imaginary-offset))
2690 ;; If the openbrace is preceded by a parenthesized exp,
2691 ;; move to the beginning of that;
2692 ;; possibly a different line
2693 (progn
2694 (if (eq (preceding-char) ?\))
2695 (forward-sexp -1))
2696 ;; In the case it starts a subroutine, indent with
2697 ;; respect to `sub', not with respect to the
2698 ;; first thing on the line, say in the case of
2699 ;; anonymous sub in a hash.
2700 ;;
2701 (skip-chars-backward " \t")
2702 (if (and (eq (preceding-char) ?b)
2703 (progn
2704 (forward-sexp -1)
2705 (looking-at "sub\\>"))
2706 (setq old-indent
2707 (nth 1
2708 (parse-partial-sexp
2709 (save-excursion (beginning-of-line) (point))
2710 (point)))))
2711 (progn (goto-char (1+ old-indent))
2712 (skip-chars-forward " \t")
2713 (current-column))
2714 ;; Get initial indentation of the line we are on.
2715 ;; If line starts with label, calculate label indentation
2716 (if (save-excursion
2717 (beginning-of-line)
2718 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
2719 (if (> (current-indentation) cperl-min-label-indent)
2720 (- (current-indentation) cperl-label-offset)
2721 ;; Do not move `parse-data', this should
2722 ;; be quick anyway:
2723 (cperl-calculate-indent))
2724 (current-indentation))))))))))))))
2725
2726 (defvar cperl-indent-alist
2727 '((string nil)
2728 (comment nil)
2729 (toplevel 0)
2730 (toplevel-after-parenth 2)
2731 (toplevel-continued 2)
2732 (expression 1))
2733 "Alist of indentation rules for CPerl mode.
2734 The values mean:
2735 nil: do not indent;
2736 number: add this amount of indentation.
2737
2738 Not finished, not used.")
2739
2740 (defun cperl-where-am-i (&optional parse-start start-state)
2741 ;; Unfinished
2742 "Return a list of lists ((TYPE POS)...) of good points before the point.
2743 POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'.
2744
2745 Not finished, not used."
2746 (save-excursion
2747 (let* ((start-point (point))
2748 (s-s (cperl-get-state))
2749 (start (nth 0 s-s))
2750 (state (nth 1 s-s))
2751 (prestart (nth 3 s-s))
2752 (containing-sexp (car (cdr state)))
2753 (case-fold-search nil)
2754 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
2755 (cond ((nth 3 state) ; In string
2756 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
2757 ((nth 4 state) ; In comment
2758 (setq res (cons '(comment) res)))
2759 ((null containing-sexp)
2760 ;; Line is at top level.
2761 ;; Indent like the previous top level line
2762 ;; unless that ends in a closeparen without semicolon,
2763 ;; in which case this line is the first argument decl.
2764 (cperl-backward-to-noncomment (or parse-start (point-min)))
2765 ;;(skip-chars-backward " \t\f\n")
2766 (cond
2767 ((or (bobp)
2768 (memq (preceding-char) (append ";}" nil)))
2769 (setq res (cons (list 'toplevel start) res)))
2770 ((eq (preceding-char) ?\) )
2771 (setq res (cons (list 'toplevel-after-parenth start) res)))
2772 (t
2773 (setq res (cons (list 'toplevel-continued start) res)))))
2774 ((/= (char-after containing-sexp) ?{)
2775 ;; line is expression, not statement:
2776 ;; indent to just after the surrounding open.
2777 ;; skip blanks if we do not close the expression.
2778 (setq res (cons (list 'expression-blanks
2779 (progn
2780 (goto-char (1+ containing-sexp))
2781 (or (looking-at "[ \t]*\\(#\\|$\\)")
2782 (skip-chars-forward " \t"))
2783 (point)))
2784 (cons (list 'expression containing-sexp) res))))
2785 ((progn
2786 ;; Containing-expr starts with \{. Check whether it is a hash.
2787 (goto-char containing-sexp)
2788 (not (cperl-block-p)))
2789 (setq res (cons (list 'expression-blanks
2790 (progn
2791 (goto-char (1+ containing-sexp))
2792 (or (looking-at "[ \t]*\\(#\\|$\\)")
2793 (skip-chars-forward " \t"))
2794 (point)))
2795 (cons (list 'expression containing-sexp) res))))
2796 (t
2797 ;; Statement level.
2798 (setq res (cons (list 'in-block containing-sexp) res))
2799 ;; Is it a continuation or a new statement?
2800 ;; Find previous non-comment character.
2801 (cperl-backward-to-noncomment containing-sexp)
2802 ;; Back up over label lines, since they don't
2803 ;; affect whether our line is a continuation.
2804 ;; Back up comma-delimited lines too ?????
2805 (while (or (eq (preceding-char) ?\,)
2806 (save-excursion (cperl-after-label)))
2807 (if (eq (preceding-char) ?\,)
2808 ;; Will go to beginning of line, essentially
2809 ;; Will ignore embedded sexpr XXXX.
2810 (cperl-backward-to-start-of-continued-exp containing-sexp))
2811 (beginning-of-line)
2812 (cperl-backward-to-noncomment containing-sexp))
2813 ;; Now we get the answer.
2814 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
2815 ;; This line is continuation of preceding line's statement.
2816 (list (list 'statement-continued containing-sexp))
2817 ;; This line starts a new statement.
2818 ;; Position following last unclosed open.
2819 (goto-char containing-sexp)
2820 ;; Is line first statement after an open-brace?
2821 (or
2822 ;; If no, find that first statement and indent like
2823 ;; it. If the first statement begins with label, do
2824 ;; not believe when the indentation of the label is too
2825 ;; small.
2826 (save-excursion
2827 (forward-char 1)
2828 (let ((colon-line-end 0))
2829 (while (progn (skip-chars-forward " \t\n" start-point)
2830 (and (< (point) start-point)
2831 (looking-at
2832 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
2833 ;; Skip over comments and labels following openbrace.
2834 (cond ((= (following-char) ?\#)
2835 ;;(forward-line 1)
2836 (end-of-line))
2837 ;; label:
2838 (t
2839 (save-excursion (end-of-line)
2840 (setq colon-line-end (point)))
2841 (search-forward ":"))))
2842 ;; Now at the point, after label, or at start
2843 ;; of first statement in the block.
2844 (and (< (point) start-point)
2845 (if (> colon-line-end (point))
2846 ;; Before statement after label
2847 (if (> (current-indentation)
2848 cperl-min-label-indent)
2849 (list (list 'label-in-block (point)))
2850 ;; Do not believe: `max' is involved
2851 (list
2852 (list 'label-in-block-min-indent (point))))
2853 ;; Before statement
2854 (list 'statement-in-block (point))))))
2855 ;; If no previous statement,
2856 ;; indent it relative to line brace is on.
2857 ;; For open brace in column zero, don't let statement
2858 ;; start there too. If cperl-indent-level is zero,
2859 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
2860 ;; For open-braces not the first thing in a line,
2861 ;; add in cperl-brace-imaginary-offset.
2862
2863 ;; If first thing on a line: ?????
2864 (+ (if (and (bolp) (zerop cperl-indent-level))
2865 (+ cperl-brace-offset cperl-continued-statement-offset)
2866 cperl-indent-level)
2867 ;; Move back over whitespace before the openbrace.
2868 ;; If openbrace is not first nonwhite thing on the line,
2869 ;; add the cperl-brace-imaginary-offset.
2870 (progn (skip-chars-backward " \t")
2871 (if (bolp) 0 cperl-brace-imaginary-offset))
2872 ;; If the openbrace is preceded by a parenthesized exp,
2873 ;; move to the beginning of that;
2874 ;; possibly a different line
2875 (progn
2876 (if (eq (preceding-char) ?\))
2877 (forward-sexp -1))
2878 ;; Get initial indentation of the line we are on.
2879 ;; If line starts with label, calculate label indentation
2880 (if (save-excursion
2881 (beginning-of-line)
2882 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
2883 (if (> (current-indentation) cperl-min-label-indent)
2884 (- (current-indentation) cperl-label-offset)
2885 (cperl-calculate-indent))
2886 (current-indentation))))))))
2887 res)))
2888
2889 (defun cperl-calculate-indent-within-comment ()
2890 "Return the indentation amount for line, assuming that
2891 the current line is to be regarded as part of a block comment."
2892 (let (end star-start)
2893 (save-excursion
2894 (beginning-of-line)
2895 (skip-chars-forward " \t")
2896 (setq end (point))
2897 (and (= (following-char) ?#)
2898 (forward-line -1)
2899 (cperl-to-comment-or-eol)
2900 (setq end (point)))
2901 (goto-char end)
2902 (current-column))))
2903
2904
2905 (defun cperl-to-comment-or-eol ()
2906 "Go to position before comment on the current line, or to end of line.
2907 Returns true if comment is found."
2908 (let (state stop-in cpoint (lim (progn (end-of-line) (point))))
2909 (beginning-of-line)
2910 (if (or
2911 (eq (get-text-property (point) 'syntax-type) 'pod)
2912 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t))
2913 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
2914 ;; Else
2915 (while (not stop-in)
2916 (setq state (parse-partial-sexp (point) lim nil nil nil t))
2917 ; stop at comment
2918 ;; If fails (beginning-of-line inside sexp), then contains not-comment
2919 (if (nth 4 state) ; After `#';
2920 ; (nth 2 state) can be
2921 ; beginning of m,s,qq and so
2922 ; on
2923 (if (nth 2 state)
2924 (progn
2925 (setq cpoint (point))
2926 (goto-char (nth 2 state))
2927 (cond
2928 ((looking-at "\\(s\\|tr\\)\\>")
2929 (or (re-search-forward
2930 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
2931 lim 'move)
2932 (setq stop-in t)))
2933 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
2934 (or (re-search-forward
2935 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
2936 lim 'move)
2937 (setq stop-in t)))
2938 (t ; It was fair comment
2939 (setq stop-in t) ; Finish
2940 (goto-char (1- cpoint)))))
2941 (setq stop-in t) ; Finish
2942 (forward-char -1))
2943 (setq stop-in t)) ; Finish
2944 )
2945 (nth 4 state))))
2946
2947 (defsubst cperl-1- (p)
2948 (max (point-min) (1- p)))
2949
2950 (defsubst cperl-1+ (p)
2951 (min (point-max) (1+ p)))
2952
2953 (defsubst cperl-modify-syntax-type (at how)
2954 (if (< at (point-max))
2955 (progn
2956 (put-text-property at (1+ at) 'syntax-table how)
2957 (put-text-property at (1+ at) 'rear-nonsticky t))))
2958
2959 (defun cperl-protect-defun-start (s e)
2960 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
2961 (save-excursion
2962 (goto-char s)
2963 (while (re-search-forward "^\\s(" e 'to-end)
2964 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
2965
2966 (defun cperl-commentify (bb e string &optional noface)
2967 (if cperl-use-syntax-table-text-property
2968 (if (eq noface 'n) ; Only immediate
2969 nil
2970 ;; We suppose that e is _after_ the end of construction, as after eol.
2971 (setq string (if string cperl-st-sfence cperl-st-cfence))
2972 (if (> bb (- e 2))
2973 ;; one-char string/comment?!
2974 (cperl-modify-syntax-type bb cperl-st-punct)
2975 (cperl-modify-syntax-type bb string)
2976 (cperl-modify-syntax-type (1- e) string))
2977 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
2978 (put-text-property (1+ bb) (1- e)
2979 'syntax-table cperl-string-syntax-table))
2980 (cperl-protect-defun-start bb e))
2981 ;; Fontify
2982 (or noface
2983 (not cperl-pod-here-fontify)
2984 (put-text-property bb e 'face (if string 'font-lock-string-face
2985 'font-lock-comment-face)))))
2986
2987 (defvar cperl-starters '(( ?\( . ?\) )
2988 ( ?\[ . ?\] )
2989 ( ?\{ . ?\} )
2990 ( ?\< . ?\> )))
2991
2992 (defun cperl-forward-re (lim end is-2arg set-st st-l err-l argument
2993 &optional ostart oend)
2994 ;; Works *before* syntax recognition is done
2995 ;; May modify syntax-type text property if the situation is too hard
2996 (let (b starter ender st i i2 go-forward reset-st)
2997 (skip-chars-forward " \t")
2998 ;; ender means matching-char matcher.
2999 (setq b (point)
3000 starter (if (eobp) 0 (char-after b))
3001 ender (cdr (assoc starter cperl-starters)))
3002 ;; What if starter == ?\\ ????
3003 (if set-st
3004 (if (car st-l)
3005 (setq st (car st-l))
3006 (setcar st-l (make-syntax-table))
3007 (setq i 0 st (car st-l))
3008 (while (< i 256)
3009 (modify-syntax-entry i "." st)
3010 (setq i (1+ i)))
3011 (modify-syntax-entry ?\\ "\\" st)))
3012 (setq set-st t)
3013 ;; Whether we have an intermediate point
3014 (setq i nil)
3015 ;; Prepare the syntax table:
3016 (and set-st
3017 (if (not ender) ; m/blah/, s/x//, s/x/y/
3018 (modify-syntax-entry starter "$" st)
3019 (modify-syntax-entry starter (concat "(" (list ender)) st)
3020 (modify-syntax-entry ender (concat ")" (list starter)) st)))
3021 (condition-case bb
3022 (progn
3023 ;; We use `$' syntax class to find matching stuff, but $$
3024 ;; is recognized the same as $, so we need to check this manually.
3025 (if (and (eq starter (char-after (cperl-1+ b)))
3026 (not ender))
3027 ;; $ has TeXish matching rules, so $$ equiv $...
3028 (forward-char 2)
3029 (setq reset-st (syntax-table))
3030 (set-syntax-table st)
3031 (forward-sexp 1)
3032 (if (<= (point) (1+ b))
3033 (error "Unfinished regular expression"))
3034 (set-syntax-table reset-st)
3035 (setq reset-st nil)
3036 ;; Now the problem is with m;blah;;
3037 (and (not ender)
3038 (eq (preceding-char)
3039 (char-after (- (point) 2)))
3040 (save-excursion
3041 (forward-char -2)
3042 (= 0 (% (skip-chars-backward "\\\\") 2)))
3043 (forward-char -1)))
3044 ;; Now we are after the first part.
3045 (and is-2arg ; Have trailing part
3046 (not ender)
3047 (eq (following-char) starter) ; Empty trailing part
3048 (progn
3049 (or (eq (char-syntax (following-char)) ?.)
3050 ;; Make trailing letter into punctuation
3051 (cperl-modify-syntax-type (point) cperl-st-punct))
3052 (setq is-2arg nil go-forward t))) ; Ignore the tail
3053 (if is-2arg ; Not number => have second part
3054 (progn
3055 (setq i (point) i2 i)
3056 (if ender
3057 (if (memq (following-char) '(?\ ?\t ?\n ?\f))
3058 (progn
3059 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3060 (goto-char (match-end 0))
3061 (skip-chars-forward " \t\n\f"))
3062 (setq i2 (point))))
3063 (forward-char -1))
3064 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3065 (if ender (modify-syntax-entry ender "." st))
3066 (setq set-st nil)
3067 (setq ender (cperl-forward-re lim end nil t st-l err-l
3068 argument starter ender)
3069 ender (nth 2 ender)))))
3070 (error (goto-char lim)
3071 (setq set-st nil)
3072 (if reset-st
3073 (set-syntax-table reset-st))
3074 (or end
3075 (message
3076 "End of `%s%s%c ... %c' string/RE not found: %s"
3077 argument
3078 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3079 starter (or ender starter) bb)
3080 (or (car err-l) (setcar err-l b)))))
3081 (if set-st
3082 (progn
3083 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3084 (if ender (modify-syntax-entry ender "." st))))
3085 ;; i: have 2 args, after end of the first arg
3086 ;; i2: start of the second arg, if any (before delim iff `ender').
3087 ;; ender: the last arg bounded by parens-like chars, the second one of them
3088 ;; starter: the starting delimiter of the first arg
3089 ;; go-forward: has 2 args, and the second part is empty
3090 (list i i2 ender starter go-forward)))
3091
3092 (defsubst cperl-postpone-fontification (b e type val &optional now)
3093 ;; Do after syntactic fontification?
3094 (if cperl-syntaxify-by-font-lock
3095 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3096 (put-text-property b e type val)))
3097
3098 ;;; Here is how the global structures (those which cannot be
3099 ;;; recognized locally) are marked:
3100 ;; a) PODs:
3101 ;; Start-to-end is marked `in-pod' ==> t
3102 ;; Each non-literal part is marked `syntax-type' ==> `pod'
3103 ;; Each literal part is marked `syntax-type' ==> `in-pod'
3104 ;; b) HEREs:
3105 ;; Start-to-end is marked `here-doc-group' ==> t
3106 ;; The body is marked `syntax-type' ==> `here-doc'
3107 ;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
3108 ;; c) FORMATs:
3109 ;; After-initial-line--to-end is marked `syntax-type' ==> `format'
3110 ;; d) 'Q'uoted string:
3111 ;; part between markers inclusive is marked `syntax-type' ==> `string'
3112 ;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
3113
3114 (defun cperl-unwind-to-safe (before &optional end)
3115 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3116 (let ((pos (point)) opos)
3117 (setq opos pos)
3118 (while (and pos (get-text-property pos 'syntax-type))
3119 (setq pos (previous-single-property-change pos 'syntax-type))
3120 (if pos
3121 (if before
3122 (progn
3123 (goto-char (cperl-1- pos))
3124 (beginning-of-line)
3125 (setq pos (point)))
3126 (goto-char (setq pos (cperl-1- pos))))
3127 ;; Up to the start
3128 (goto-char (point-min))))
3129 ;; Skip empty lines
3130 (and (looking-at "\n*=")
3131 (/= 0 (skip-chars-backward "\n"))
3132 (forward-char))
3133 (setq pos (point))
3134 (if end
3135 ;; Do the same for end, going small steps
3136 (progn
3137 (while (and end (get-text-property end 'syntax-type))
3138 (setq pos end
3139 end (next-single-property-change end 'syntax-type)))
3140 (or end pos)))))
3141
3142 (defvar cperl-nonoverridable-face)
3143 (defvar font-lock-function-name-face)
3144 (defvar font-lock-comment-face)
3145
3146 (defun cperl-find-pods-heres (&optional min max non-inter end ignore-max)
3147 "Scans the buffer for hard-to-parse Perl constructions.
3148 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3149 the sections using `cperl-pod-head-face', `cperl-pod-face',
3150 `cperl-here-face'."
3151 (interactive)
3152 (or min (setq min (point-min)
3153 cperl-syntax-state nil
3154 cperl-syntax-done-to min))
3155 (or max (setq max (point-max)))
3156 (let* (face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
3157 is-REx is-x-REx REx-comment-start REx-comment-end was-comment i2
3158 (cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
3159 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3160 (modified (buffer-modified-p))
3161 (after-change-functions nil)
3162 (use-syntax-state (and cperl-syntax-state
3163 (>= min (car cperl-syntax-state))))
3164 (state-point (if use-syntax-state
3165 (car cperl-syntax-state)
3166 (point-min)))
3167 (state (if use-syntax-state
3168 (cdr cperl-syntax-state)))
3169 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3170 (st-l (list nil)) (err-l (list nil))
3171 ;; Somehow font-lock may be not loaded yet...
3172 (font-lock-string-face (if (boundp 'font-lock-string-face)
3173 font-lock-string-face
3174 'font-lock-string-face))
3175 (font-lock-constant-face (if (boundp 'font-lock-constant-face)
3176 font-lock-constant-face
3177 'font-lock-constant-face))
3178 (font-lock-function-name-face
3179 (if (boundp 'font-lock-function-name-face)
3180 font-lock-function-name-face
3181 'font-lock-function-name-face))
3182 (font-lock-comment-face
3183 (if (boundp 'font-lock-comment-face)
3184 font-lock-comment-face
3185 'font-lock-comment-face))
3186 (cperl-nonoverridable-face
3187 (if (boundp 'cperl-nonoverridable-face)
3188 cperl-nonoverridable-face
3189 'cperl-nonoverridable-face))
3190 (stop-point (if ignore-max
3191 (point-max)
3192 max))
3193 (search
3194 (concat
3195 "\\(\\`\n?\\|^\n\\)="
3196 "\\|"
3197 ;; One extra () before this:
3198 "<<"
3199 "\\(" ; 1 + 1
3200 ;; First variant "BLAH" or just ``.
3201 "[ \t]*" ; Yes, whitespace is allowed!
3202 "\\([\"'`]\\)" ; 2 + 1 = 3
3203 "\\([^\"'`\n]*\\)" ; 3 + 1
3204 "\\3"
3205 "\\|"
3206 ;; Second variant: Identifier or \ID or empty
3207 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3208 ;; Do not have <<= or << 30 or <<30 or << $blah.
3209 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3210 "\\(\\)" ; To preserve count of pars :-( 6 + 1
3211 "\\)"
3212 "\\|"
3213 ;; 1+6 extra () before this:
3214 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3215 (if cperl-use-syntax-table-text-property
3216 (concat
3217 "\\|"
3218 ;; 1+6+2=9 extra () before this:
3219 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
3220 "\\|"
3221 ;; 1+6+2+1=10 extra () before this:
3222 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3223 "\\|"
3224 ;; 1+6+2+1+1=11 extra () before this:
3225 "\\<sub\\>[ \t]*\\([a-zA-Z_:'0-9]+[ \t]*\\)?\\(([^()]*)\\)"
3226 "\\|"
3227 ;; 1+6+2+1+1+2=13 extra () before this:
3228 "\\$\\(['{]\\)"
3229 "\\|"
3230 ;; 1+6+2+1+1+2+1=14 extra () before this:
3231 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3232 ;; 1+6+2+1+1+2+1+1=15 extra () before this:
3233 "\\|"
3234 "__\\(END\\|DATA\\)__"
3235 ;; 1+6+2+1+1+2+1+1+1=16 extra () before this:
3236 "\\|"
3237 "\\\\\\(['`\"]\\)"
3238 )
3239 ""))))
3240 (unwind-protect
3241 (progn
3242 (save-excursion
3243 (or non-inter
3244 (message "Scanning for \"hard\" Perl constructions..."))
3245 (and cperl-pod-here-fontify
3246 ;; We had evals here, do not know why...
3247 (setq face cperl-pod-face
3248 head-face cperl-pod-head-face
3249 here-face cperl-here-face))
3250 (remove-text-properties min max
3251 '(syntax-type t in-pod t syntax-table t
3252 cperl-postpone t
3253 syntax-subtype t
3254 rear-nonsticky t
3255 indentable t))
3256 ;; Need to remove face as well...
3257 (goto-char min)
3258 (and (eq system-type 'emx)
3259 (looking-at "extproc[ \t]") ; Analogue of #!
3260 (cperl-commentify min
3261 (save-excursion (end-of-line) (point))
3262 nil))
3263 (while (and
3264 (< (point) max)
3265 (re-search-forward search max t))
3266 (setq tmpend nil) ; Valid for most cases
3267 (cond
3268 ((match-beginning 1) ; POD section
3269 ;; "\\(\\`\n?\\|^\n\\)="
3270 (if (looking-at "cut\\>")
3271 (if ignore-max
3272 nil ; Doing a chunk only
3273 (message "=cut is not preceded by a POD section")
3274 (or (car err-l) (setcar err-l (point))))
3275 (beginning-of-line)
3276
3277 (setq b (point)
3278 bb b
3279 tb (match-beginning 0)
3280 b1 nil) ; error condition
3281 ;; We do not search to max, since we may be called from
3282 ;; some hook of fontification, and max is random
3283 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
3284 (progn
3285 (goto-char b)
3286 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3287 (progn
3288 (message "=cut is not preceded by an empty line")
3289 (setq b1 t)
3290 (or (car err-l) (setcar err-l b))))))
3291 (beginning-of-line 2) ; An empty line after =cut is not POD!
3292 (setq e (point))
3293 (and (> e max)
3294 (progn
3295 (remove-text-properties
3296 max e '(syntax-type t in-pod t syntax-table t
3297 cperl-postpone t
3298 syntax-subtype t
3299 rear-nonsticky t
3300 indentable t))
3301 (setq tmpend tb)))
3302 (put-text-property b e 'in-pod t)
3303 (put-text-property b e 'syntax-type 'in-pod)
3304 (goto-char b)
3305 (while (re-search-forward "\n\n[ \t]" e t)
3306 ;; We start 'pod 1 char earlier to include the preceding line
3307 (beginning-of-line)
3308 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
3309 (cperl-put-do-not-fontify b (point) t)
3310 ;; mark the non-literal parts as PODs
3311 (if cperl-pod-here-fontify
3312 (cperl-postpone-fontification b (point) 'face face t))
3313 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3314 (beginning-of-line)
3315 (setq b (point)))
3316 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
3317 (cperl-put-do-not-fontify (point) e t)
3318 (if cperl-pod-here-fontify
3319 (progn
3320 ;; mark the non-literal parts as PODs
3321 (cperl-postpone-fontification (point) e 'face face t)
3322 (goto-char bb)
3323 (if (looking-at
3324 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3325 ;; mark the headers
3326 (cperl-postpone-fontification
3327 (match-beginning 1) (match-end 1)
3328 'face head-face))
3329 (while (re-search-forward
3330 ;; One paragraph
3331 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3332 e 'toend)
3333 ;; mark the headers
3334 (cperl-postpone-fontification
3335 (match-beginning 1) (match-end 1)
3336 'face head-face))))
3337 (cperl-commentify bb e nil)
3338 (goto-char e)
3339 (or (eq e (point-max))
3340 (forward-char -1)))) ; Prepare for immediate pod start.
3341 ;; Here document
3342 ;; We do only one here-per-line
3343 ;; ;; One extra () before this:
3344 ;;"<<"
3345 ;; "\\(" ; 1 + 1
3346 ;; ;; First variant "BLAH" or just ``.
3347 ;; "\\([\"'`]\\)" ; 2 + 1
3348 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3349 ;; "\\3"
3350 ;; "\\|"
3351 ;; ;; Second variant: Identifier or \ID or empty
3352 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3353 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3354 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3355 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3356 ;; "\\)"
3357 ((match-beginning 2) ; 1 + 1
3358 ;; Abort in comment:
3359 (setq b (point))
3360 (setq state (parse-partial-sexp state-point b nil nil state)
3361 state-point b
3362 tb (match-beginning 0)
3363 i (or (nth 3 state) (nth 4 state)))
3364 (if i
3365 (setq c t)
3366 (setq c (and
3367 (match-beginning 5)
3368 (not (match-beginning 6)) ; Empty
3369 (looking-at
3370 "[ \t]*[=0-9$@%&(]"))))
3371 (if c ; Not here-doc
3372 nil ; Skip it.
3373 (if (match-beginning 5) ;4 + 1
3374 (setq b1 (match-beginning 5) ; 4 + 1
3375 e1 (match-end 5)) ; 4 + 1
3376 (setq b1 (match-beginning 4) ; 3 + 1
3377 e1 (match-end 4))) ; 3 + 1
3378 (setq tag (buffer-substring b1 e1)
3379 qtag (regexp-quote tag))
3380 (cond (cperl-pod-here-fontify
3381 ;; Highlight the starting delimiter
3382 (cperl-postpone-fontification b1 e1 'face font-lock-constant-face)
3383 (cperl-put-do-not-fontify b1 e1 t)))
3384 (forward-line)
3385 (setq b (point))
3386 ;; We do not search to max, since we may be called from
3387 ;; some hook of fontification, and max is random
3388 (cond ((re-search-forward (concat "^" qtag "$")
3389 stop-point 'toend)
3390 (if cperl-pod-here-fontify
3391 (progn
3392 ;; Highlight the ending delimiter
3393 (cperl-postpone-fontification (match-beginning 0) (match-end 0)
3394 'face font-lock-constant-face)
3395 (cperl-put-do-not-fontify b (match-end 0) t)
3396 ;; Highlight the HERE-DOC
3397 (cperl-postpone-fontification b (match-beginning 0)
3398 'face here-face)))
3399 (setq e1 (cperl-1+ (match-end 0)))
3400 (put-text-property b (match-beginning 0)
3401 'syntax-type 'here-doc)
3402 (put-text-property (match-beginning 0) e1
3403 'syntax-type 'here-doc-delim)
3404 (put-text-property b e1
3405 'here-doc-group t)
3406 (cperl-commentify b e1 nil)
3407 (cperl-put-do-not-fontify b (match-end 0) t)
3408 (if (> e1 max)
3409 (setq tmpend tb)))
3410 (t (message "End of here-document `%s' not found." tag)
3411 (or (car err-l) (setcar err-l b))))))
3412 ;; format
3413 ((match-beginning 8)
3414 ;; 1+6=7 extra () before this:
3415 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3416 (setq b (point)
3417 name (if (match-beginning 8) ; 7 + 1
3418 (buffer-substring (match-beginning 8) ; 7 + 1
3419 (match-end 8)) ; 7 + 1
3420 "")
3421 tb (match-beginning 0))
3422 (setq argument nil)
3423 (if cperl-pod-here-fontify
3424 (while (and (eq (forward-line) 0)
3425 (not (looking-at "^[.;]$")))
3426 (cond
3427 ((looking-at "^#")) ; Skip comments
3428 ((and argument ; Skip argument multi-lines
3429 (looking-at "^[ \t]*{"))
3430 (forward-sexp 1)
3431 (setq argument nil))
3432 (argument ; Skip argument lines
3433 (setq argument nil))
3434 (t ; Format line
3435 (setq b1 (point))
3436 (setq argument (looking-at "^[^\n]*[@^]"))
3437 (end-of-line)
3438 ;; Highlight the format line
3439 (cperl-postpone-fontification b1 (point)
3440 'face font-lock-string-face)
3441 (cperl-commentify b1 (point) nil)
3442 (cperl-put-do-not-fontify b1 (point) t))))
3443 ;; We do not search to max, since we may be called from
3444 ;; some hook of fontification, and max is random
3445 (re-search-forward "^[.;]$" stop-point 'toend))
3446 (beginning-of-line)
3447 (if (looking-at "^\\.$") ; ";" is not supported yet
3448 (progn
3449 ;; Highlight the ending delimiter
3450 (cperl-postpone-fontification (point) (+ (point) 2)
3451 'face font-lock-string-face)
3452 (cperl-commentify (point) (+ (point) 2) nil)
3453 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
3454 (message "End of format `%s' not found." name)
3455 (or (car err-l) (setcar err-l b)))
3456 (forward-line)
3457 (if (> (point) max)
3458 (setq tmpend tb))
3459 (put-text-property b (point) 'syntax-type 'format))
3460 ;; Regexp:
3461 ((or (match-beginning 10) (match-beginning 11))
3462 ;; 1+6+2=9 extra () before this:
3463 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
3464 ;; "\\|"
3465 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
3466 (setq b1 (if (match-beginning 10) 10 11)
3467 argument (buffer-substring
3468 (match-beginning b1) (match-end b1))
3469 b (point)
3470 i b
3471 c (char-after (match-beginning b1))
3472 bb (char-after (1- (match-beginning b1))) ; tmp holder
3473 ;; bb == "Not a stringy"
3474 bb (if (eq b1 10) ; user variables/whatever
3475 (or
3476 (memq bb '(?\$ ?\@ ?\% ?\* ?\#)) ; $#y
3477 (and (eq bb ?-) (eq c ?s)) ; -s file test
3478 (and (eq bb ?\&)
3479 (not (eq (char-after ; &&m/blah/
3480 (- (match-beginning b1) 2))
3481 ?\&))))
3482 ;; <file> or <$file>
3483 (and (eq c ?\<)
3484 ;; Do not stringify <FH>, <$fh> :
3485 (save-match-data
3486 (looking-at
3487 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
3488 tb (match-beginning 0))
3489 (goto-char (match-beginning b1))
3490 (cperl-backward-to-noncomment (point-min))
3491 (or bb
3492 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
3493 (setq argument ""
3494 bb ; Not a regexp?
3495 (progn
3496 (not
3497 ;; What is below: regexp-p?
3498 (and
3499 (or (memq (preceding-char)
3500 (append (if (memq c '(?\? ?\<))
3501 ;; $a++ ? 1 : 2
3502 "~{(=|&*!,;:"
3503 "~{(=|&+-*!,;:") nil))
3504 (and (eq (preceding-char) ?\})
3505 (cperl-after-block-p (point-min)))
3506 (and (eq (char-syntax (preceding-char)) ?w)
3507 (progn
3508 (forward-sexp -1)
3509 ;; After these keywords `/' starts a RE. One should add all the
3510 ;; functions/builtins which expect an argument, but ...
3511 (if (eq (preceding-char) ?-)
3512 ;; -d ?foo? is a RE
3513 (looking-at "[a-zA-Z]\\>")
3514 (and
3515 (not (memq (preceding-char)
3516 '(?$ ?@ ?& ?%)))
3517 (looking-at
3518 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
3519 (and (eq (preceding-char) ?.)
3520 (eq (char-after (- (point) 2)) ?.))
3521 (bobp))
3522 ;; m|blah| ? foo : bar;
3523 (not
3524 (and (eq c ?\?)
3525 cperl-use-syntax-table-text-property
3526 (not (bobp))
3527 (progn
3528 (forward-char -1)
3529 (looking-at "\\s|")))))))
3530 b (1- b))
3531 ;; s y tr m
3532 ;; Check for $a->y
3533 (if (and (eq (preceding-char) ?>)
3534 (eq (char-after (- (point) 2)) ?-))
3535 ;; Not a regexp
3536 (setq bb t))))
3537 (or bb (setq state (parse-partial-sexp
3538 state-point b nil nil state)
3539 state-point b))
3540 (goto-char b)
3541 (if (or bb (nth 3 state) (nth 4 state))
3542 (goto-char i)
3543 ;; Skip whitespace and comments...
3544 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3545 (goto-char (match-end 0))
3546 (skip-chars-forward " \t\n\f"))
3547 (if (> (point) b)
3548 (put-text-property b (point) 'syntax-type 'prestring))
3549 ;; qtag means two-arg matcher, may be reset to
3550 ;; 2 or 3 later if some special quoting is needed.
3551 ;; e1 means matching-char matcher.
3552 (setq b (point)
3553 ;; has 2 args
3554 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
3555 ;; We do not search to max, since we may be called from
3556 ;; some hook of fontification, and max is random
3557 i (cperl-forward-re stop-point end
3558 i2
3559 t st-l err-l argument)
3560 ;; Note that if `go', then it is considered as 1-arg
3561 b1 (nth 1 i) ; start of the second part
3562 tag (nth 2 i) ; ender-char, true if second part
3563 ; is with matching chars []
3564 go (nth 4 i) ; There is a 1-char part after the end
3565 i (car i) ; intermediate point
3566 e1 (point) ; end
3567 ;; Before end of the second part if non-matching: ///
3568 tail (if (and i (not tag))
3569 (1- e1))
3570 e (if i i e1) ; end of the first part
3571 qtag nil ; need to preserve backslashitis
3572 is-x-REx nil) ; REx has //x modifier
3573 ;; Commenting \\ is dangerous, what about ( ?
3574 (and i tail
3575 (eq (char-after i) ?\\)
3576 (setq qtag t))
3577 (if (looking-at "\\sw*x") ; qr//x
3578 (setq is-x-REx t))
3579 (if (null i)
3580 ;; Considered as 1arg form
3581 (progn
3582 (cperl-commentify b (point) t)
3583 (put-text-property b (point) 'syntax-type 'string)
3584 (if (or is-x-REx
3585 ;; ignore other text properties:
3586 (string-match "^qw$" argument))
3587 (put-text-property b (point) 'indentable t))
3588 (and go
3589 (setq e1 (cperl-1+ e1))
3590 (or (eobp)
3591 (forward-char 1))))
3592 (cperl-commentify b i t)
3593 (if (looking-at "\\sw*e") ; s///e
3594 (progn
3595 (and
3596 ;; silent:
3597 (cperl-find-pods-heres b1 (1- (point)) t end)
3598 ;; Error
3599 (goto-char (1+ max)))
3600 (if (and tag (eq (preceding-char) ?\>))
3601 (progn
3602 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
3603 (cperl-modify-syntax-type i cperl-st-bra)))
3604 (put-text-property b i 'syntax-type 'string)
3605 (if is-x-REx
3606 (put-text-property b i 'indentable t)))
3607 (cperl-commentify b1 (point) t)
3608 (put-text-property b (point) 'syntax-type 'string)
3609 (if is-x-REx
3610 (put-text-property b i 'indentable t))
3611 (if qtag
3612 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
3613 (setq tail nil)))
3614 ;; Now: tail: if the second part is non-matching without ///e
3615 (if (eq (char-syntax (following-char)) ?w)
3616 (progn
3617 (forward-word 1) ; skip modifiers s///s
3618 (if tail (cperl-commentify tail (point) t))
3619 (cperl-postpone-fontification
3620 e1 (point) 'face 'cperl-nonoverridable-face)))
3621 ;; Check whether it is m// which means "previous match"
3622 ;; and highlight differently
3623 (setq is-REx
3624 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
3625 (or (not (= (length argument) 0))
3626 (not (eq c ?\<)))))
3627 (if (and is-REx
3628 (eq e (+ 2 b))
3629 ;; split // *is* using zero-pattern
3630 (save-excursion
3631 (condition-case nil
3632 (progn
3633 (goto-char tb)
3634 (forward-sexp -1)
3635 (not (looking-at "split\\>")))
3636 (error t))))
3637 (cperl-postpone-fontification
3638 b e 'face font-lock-function-name-face)
3639 (if (or i2 ; Has 2 args
3640 (and cperl-fontify-m-as-s
3641 (or
3642 (string-match "^\\(m\\|qr\\)$" argument)
3643 (and (eq 0 (length argument))
3644 (not (eq ?\< (char-after b)))))))
3645 (progn
3646 (cperl-postpone-fontification
3647 b (cperl-1+ b) 'face font-lock-constant-face)
3648 (cperl-postpone-fontification
3649 (1- e) e 'face font-lock-constant-face)))
3650 (if (and is-REx cperl-regexp-scan)
3651 ;; Process RExen better
3652 (save-excursion
3653 (goto-char (1+ b))
3654 (while
3655 (and (< (point) e)
3656 (re-search-forward
3657 (if is-x-REx
3658 (if (eq (char-after b) ?\#)
3659 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
3660 "\\((\\?#\\)\\|\\(#\\)")
3661 (if (eq (char-after b) ?\#)
3662 "\\((\\?\\\\#\\)"
3663 "\\((\\?#\\)"))
3664 (1- e) 'to-end))
3665 (goto-char (match-beginning 0))
3666 (setq REx-comment-start (point)
3667 was-comment t)
3668 (if (save-excursion
3669 (and
3670 ;; XXX not working if outside delimiter is #
3671 (eq (preceding-char) ?\\)
3672 (= (% (skip-chars-backward "$\\\\") 2) -1)))
3673 ;; Not a comment, avoid loop:
3674 (progn (setq was-comment nil)
3675 (forward-char 1))
3676 (if (match-beginning 2)
3677 (progn
3678 (beginning-of-line 2)
3679 (if (> (point) e)
3680 (goto-char (1- e))))
3681 ;; Works also if the outside delimiters are ().
3682 (or (search-forward ")" (1- e) 'toend)
3683 (message
3684 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
3685 REx-comment-start))))
3686 (if (>= (point) e)
3687 (goto-char (1- e)))
3688 (if was-comment
3689 (progn
3690 (setq REx-comment-end (point))
3691 (cperl-commentify
3692 REx-comment-start REx-comment-end nil)
3693 (cperl-postpone-fontification
3694 REx-comment-start REx-comment-end
3695 'face font-lock-comment-face))))))
3696 (if (and is-REx is-x-REx)
3697 (put-text-property (1+ b) (1- e)
3698 'syntax-subtype 'x-REx)))
3699 (if i2
3700 (progn
3701 (cperl-postpone-fontification
3702 (1- e1) e1 'face font-lock-constant-face)
3703 (if (assoc (char-after b) cperl-starters)
3704 (cperl-postpone-fontification
3705 b1 (1+ b1) 'face font-lock-constant-face))))
3706 (if (> (point) max)
3707 (setq tmpend tb))))
3708 ((match-beginning 13) ; sub with prototypes
3709 (setq b (match-beginning 0))
3710 (if (memq (char-after (1- b))
3711 '(?\$ ?\@ ?\% ?\& ?\*))
3712 nil
3713 (setq state (parse-partial-sexp
3714 state-point b nil nil state)
3715 state-point b)
3716 (if (or (nth 3 state) (nth 4 state))
3717 nil
3718 ;; Mark as string
3719 (cperl-commentify (match-beginning 13) (match-end 13) t))
3720 (goto-char (match-end 0))))
3721 ;; 1+6+2+1+1+2=13 extra () before this:
3722 ;; "\\$\\(['{]\\)"
3723 ((and (match-beginning 14)
3724 (eq (preceding-char) ?\')) ; $'
3725 (setq b (1- (point))
3726 state (parse-partial-sexp
3727 state-point (1- b) nil nil state)
3728 state-point (1- b))
3729 (if (nth 3 state) ; in string
3730 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3731 (goto-char (1+ b)))
3732 ;; 1+6+2+1+1+2=13 extra () before this:
3733 ;; "\\$\\(['{]\\)"
3734 ((match-beginning 14) ; ${
3735 (setq bb (match-beginning 0))
3736 (cperl-modify-syntax-type bb cperl-st-punct))
3737 ;; 1+6+2+1+1+2+1=14 extra () before this:
3738 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
3739 ((match-beginning 15) ; old $abc'efg syntax
3740 (setq bb (match-end 0)
3741 b (match-beginning 0)
3742 state (parse-partial-sexp
3743 state-point b nil nil state)
3744 state-point b)
3745 (if (nth 3 state) ; in string
3746 nil
3747 (put-text-property (1- bb) bb 'syntax-table cperl-st-word))
3748 (goto-char bb))
3749 ;; 1+6+2+1+1+2+1+1=15 extra () before this:
3750 ;; "__\\(END\\|DATA\\)__"
3751 ((match-beginning 16) ; __END__, __DATA__
3752 (setq bb (match-end 0)
3753 b (match-beginning 0)
3754 state (parse-partial-sexp
3755 state-point b nil nil state)
3756 state-point b)
3757 (if (or (nth 3 state) (nth 4 state))
3758 nil
3759 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
3760 (cperl-commentify b bb nil)
3761 (setq end t))
3762 (goto-char bb))
3763 ((match-beginning 17) ; "\\\\\\(['`\"]\\)"
3764 (setq bb (match-end 0)
3765 b (match-beginning 0))
3766 (goto-char b)
3767 (skip-chars-backward "\\\\")
3768 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
3769 (setq state (parse-partial-sexp
3770 state-point b nil nil state)
3771 state-point b)
3772 (if (or (nth 3 state) (nth 4 state) )
3773 nil
3774 (cperl-modify-syntax-type b cperl-st-punct))
3775 (goto-char bb))
3776 (t (error "Error in regexp of the sniffer")))
3777 (if (> (point) stop-point)
3778 (progn
3779 (if end
3780 (message "Garbage after __END__/__DATA__ ignored")
3781 (message "Unbalanced syntax found while scanning")
3782 (or (car err-l) (setcar err-l b)))
3783 (goto-char stop-point))))
3784 (setq cperl-syntax-state (cons state-point state)
3785 cperl-syntax-done-to (or tmpend (max (point) max))))
3786 (if (car err-l) (goto-char (car err-l))
3787 (or non-inter
3788 (message "Scanning for \"hard\" Perl constructions... done"))))
3789 (and (buffer-modified-p)
3790 (not modified)
3791 (set-buffer-modified-p nil))
3792 (set-syntax-table cperl-mode-syntax-table))
3793 (car err-l)))
3794
3795 (defun cperl-backward-to-noncomment (lim)
3796 ;; Stops at lim or after non-whitespace that is not in comment
3797 (let (stop p pr)
3798 (while (and (not stop) (> (point) (or lim 1)))
3799 (skip-chars-backward " \t\n\f" lim)
3800 (setq p (point))
3801 (beginning-of-line)
3802 (if (memq (setq pr (get-text-property (point) 'syntax-type))
3803 '(pod here-doc here-doc-delim))
3804 (cperl-unwind-to-safe nil)
3805 (if (or (looking-at "^[ \t]*\\(#\\|$\\)")
3806 (progn (cperl-to-comment-or-eol) (bolp)))
3807 nil ; Only comment, skip
3808 ;; Else
3809 (skip-chars-backward " \t")
3810 (if (< p (point)) (goto-char p))
3811 (setq stop t))))))
3812
3813 (defun cperl-after-block-p (lim)
3814 ;; We suppose that the preceding char is }.
3815 (save-excursion
3816 (condition-case nil
3817 (progn
3818 (forward-sexp -1)
3819 (cperl-backward-to-noncomment lim)
3820 (or (eq (point) lim)
3821 (eq (preceding-char) ?\) ) ; if () {} sub f () {}
3822 (if (eq (char-syntax (preceding-char)) ?w) ; else {}
3823 (save-excursion
3824 (forward-sexp -1)
3825 (or (looking-at "\\(else\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
3826 ;; sub f {}
3827 (progn
3828 (cperl-backward-to-noncomment lim)
3829 (and (eq (char-syntax (preceding-char)) ?w)
3830 (progn
3831 (forward-sexp -1)
3832 (looking-at "sub\\>"))))))
3833 (cperl-after-expr-p lim))))
3834 (error nil))))
3835
3836 (defun cperl-after-expr-p (&optional lim chars test)
3837 "Return true if the position is good for start of expression.
3838 TEST is the expression to evaluate at the found position. If absent,
3839 CHARS is a string that contains good characters to have before us (however,
3840 `}' is treated \"smartly\" if it is not in the list)."
3841 (let (stop p
3842 (lim (or lim (point-min))))
3843 (save-excursion
3844 (while (and (not stop) (> (point) lim))
3845 (skip-chars-backward " \t\n\f" lim)
3846 (setq p (point))
3847 (beginning-of-line)
3848 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
3849 ;; Else: last iteration, or a label
3850 (cperl-to-comment-or-eol)
3851 (skip-chars-backward " \t")
3852 (if (< p (point)) (goto-char p))
3853 (setq p (point))
3854 (if (and (eq (preceding-char) ?:)
3855 (progn
3856 (forward-char -1)
3857 (skip-chars-backward " \t\n\f" lim)
3858 (eq (char-syntax (preceding-char)) ?w)))
3859 (forward-sexp -1) ; Possibly label. Skip it
3860 (goto-char p)
3861 (setq stop t))))
3862 (or (bobp) ; ???? Needed
3863 (eq (point) lim)
3864 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
3865 (progn
3866 (if test (eval test)
3867 (or (memq (preceding-char) (append (or chars "{;") nil))
3868 (and (eq (preceding-char) ?\})
3869 (cperl-after-block-p lim)))))))))
3870
3871 (defun cperl-backward-to-start-of-continued-exp (lim)
3872 (if (memq (preceding-char) (append ")]}\"'`" nil))
3873 (forward-sexp -1))
3874 (beginning-of-line)
3875 (if (<= (point) lim)
3876 (goto-char (1+ lim)))
3877 (skip-chars-forward " \t"))
3878
3879 (defun cperl-after-block-and-statement-beg (lim)
3880 ;; We assume that we are after ?\}
3881 (and
3882 (cperl-after-block-p lim)
3883 (save-excursion
3884 (forward-sexp -1)
3885 (cperl-backward-to-noncomment (point-min))
3886 (or (bobp)
3887 (eq (point) lim)
3888 (not (= (char-syntax (preceding-char)) ?w))
3889 (progn
3890 (forward-sexp -1)
3891 (not
3892 (looking-at
3893 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
3894
3895 \f
3896 (defun cperl-indent-exp ()
3897 "Simple variant of indentation of continued-sexp.
3898
3899 Will not indent comment if it starts at `comment-indent' or looks like
3900 continuation of the comment on the previous line.
3901
3902 If `cperl-indent-region-fix-constructs', will improve spacing on
3903 conditional/loop constructs."
3904 (interactive)
3905 (save-excursion
3906 (let ((tmp-end (progn (end-of-line) (point))) top done)
3907 (save-excursion
3908 (beginning-of-line)
3909 (while (null done)
3910 (setq top (point))
3911 (while (= (nth 0 (parse-partial-sexp (point) tmp-end
3912 -1)) -1)
3913 (setq top (point))) ; Get the outermost parenths in line
3914 (goto-char top)
3915 (while (< (point) tmp-end)
3916 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
3917 (or (eolp) (forward-sexp 1)))
3918 (if (> (point) tmp-end)
3919 (save-excursion
3920 (end-of-line)
3921 (setq tmp-end (point)))
3922 (setq done t)))
3923 (goto-char tmp-end)
3924 (setq tmp-end (point-marker)))
3925 (if cperl-indent-region-fix-constructs
3926 (cperl-fix-line-spacing tmp-end))
3927 (cperl-indent-region (point) tmp-end))))
3928
3929 (defun cperl-fix-line-spacing (&optional end parse-data)
3930 "Improve whitespace in a conditional/loop construct.
3931 Returns some position at the last line."
3932 (interactive)
3933 (or end
3934 (setq end (point-max)))
3935 (let (p pp ml have-brace ret
3936 (ee (save-excursion (end-of-line) (point)))
3937 (cperl-indent-region-fix-constructs
3938 (or cperl-indent-region-fix-constructs 1)))
3939 (save-excursion
3940 (beginning-of-line)
3941 (setq ret (point))
3942 ;; }? continue
3943 ;; blah; }
3944 (if (not
3945 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
3946 (setq have-brace (save-excursion (search-forward "}" ee t)))))
3947 nil ; Do not need to do anything
3948 ;; Looking at:
3949 ;; }
3950 ;; else
3951 (if (and cperl-merge-trailing-else
3952 (looking-at
3953 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>"))
3954 (progn
3955 (search-forward "}")
3956 (setq p (point))
3957 (skip-chars-forward " \t\n")
3958 (delete-region p (point))
3959 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3960 (beginning-of-line)))
3961 ;; Looking at:
3962 ;; } else
3963 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
3964 (progn
3965 (search-forward "}")
3966 (delete-horizontal-space)
3967 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3968 (beginning-of-line)))
3969 ;; Looking at:
3970 ;; else {
3971 (if (looking-at
3972 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
3973 (progn
3974 (forward-word 1)
3975 (delete-horizontal-space)
3976 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3977 (beginning-of-line)))
3978 ;; Looking at:
3979 ;; foreach my $var
3980 (if (looking-at
3981 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
3982 (progn
3983 (forward-word 2)
3984 (delete-horizontal-space)
3985 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3986 (beginning-of-line)))
3987 ;; Looking at:
3988 ;; foreach my $var (
3989 (if (looking-at
3990 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
3991 (progn
3992 (forward-word 3)
3993 (delete-horizontal-space)
3994 (insert
3995 (make-string cperl-indent-region-fix-constructs ?\ ))
3996 (beginning-of-line)))
3997 ;; Looking at:
3998 ;; } foreach my $var () {
3999 (if (looking-at
4000 "[ \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]*{")
4001 (progn
4002 (setq ml (match-beginning 8))
4003 (re-search-forward "[({]")
4004 (forward-char -1)
4005 (setq p (point))
4006 (if (eq (following-char) ?\( )
4007 (progn
4008 (forward-sexp 1)
4009 (setq pp (point)))
4010 ;; after `else' or nothing
4011 (if ml ; after `else'
4012 (skip-chars-backward " \t\n")
4013 (beginning-of-line))
4014 (setq pp nil))
4015 ;; Now after the sexp before the brace
4016 ;; Multiline expr should be special
4017 (setq ml (and pp (save-excursion (goto-char p)
4018 (search-forward "\n" pp t))))
4019 (if (and (or (not pp) (< pp end))
4020 (looking-at "[ \t\n]*{"))
4021 (progn
4022 (cond
4023 ((bolp) ; Were before `{', no if/else/etc
4024 nil)
4025 ((looking-at "\\(\t*\\| [ \t]+\\){")
4026 (delete-horizontal-space)
4027 (if (if ml
4028 cperl-extra-newline-before-brace-multiline
4029 cperl-extra-newline-before-brace)
4030 (progn
4031 (delete-horizontal-space)
4032 (insert "\n")
4033 (setq ret (point))
4034 (if (cperl-indent-line parse-data)
4035 (progn
4036 (cperl-fix-line-spacing end parse-data)
4037 (setq ret (point)))))
4038 (insert
4039 (make-string cperl-indent-region-fix-constructs ?\ ))))
4040 ((and (looking-at "[ \t]*\n")
4041 (not (if ml
4042 cperl-extra-newline-before-brace-multiline
4043 cperl-extra-newline-before-brace)))
4044 (setq pp (point))
4045 (skip-chars-forward " \t\n")
4046 (delete-region pp (point))
4047 (insert
4048 (make-string cperl-indent-region-fix-constructs ?\ ))))
4049 ;; Now we are before `{'
4050 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
4051 (progn
4052 (skip-chars-forward " \t\n")
4053 (setq pp (point))
4054 (forward-sexp 1)
4055 (setq p (point))
4056 (goto-char pp)
4057 (setq ml (search-forward "\n" p t))
4058 (if (or cperl-break-one-line-blocks-when-indent ml)
4059 ;; not good: multi-line BLOCK
4060 (progn
4061 (goto-char (1+ pp))
4062 (delete-horizontal-space)
4063 (insert "\n")
4064 (setq ret (point))
4065 (if (cperl-indent-line parse-data)
4066 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
4067 (beginning-of-line)
4068 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
4069 ;; Now check whether there is a hanging `}'
4070 ;; Looking at:
4071 ;; } blah
4072 (if (and
4073 cperl-fix-hanging-brace-when-indent
4074 have-brace
4075 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
4076 (condition-case nil
4077 (progn
4078 (up-list 1)
4079 (if (and (<= (point) pp)
4080 (eq (preceding-char) ?\} )
4081 (cperl-after-block-and-statement-beg (point-min)))
4082 t
4083 (goto-char p)
4084 nil))
4085 (error nil)))
4086 (progn
4087 (forward-char -1)
4088 (skip-chars-backward " \t")
4089 (if (bolp)
4090 ;; `}' was the first thing on the line, insert NL *after* it.
4091 (progn
4092 (cperl-indent-line parse-data)
4093 (search-forward "}")
4094 (delete-horizontal-space)
4095 (insert "\n"))
4096 (delete-horizontal-space)
4097 (or (eq (preceding-char) ?\;)
4098 (bolp)
4099 (and (eq (preceding-char) ?\} )
4100 (cperl-after-block-p (point-min)))
4101 (insert ";"))
4102 (insert "\n")
4103 (setq ret (point)))
4104 (if (cperl-indent-line parse-data)
4105 (setq ret (cperl-fix-line-spacing end parse-data)))
4106 (beginning-of-line)))))
4107 ret))
4108
4109 (defvar cperl-update-start) ; Do not need to make them local
4110 (defvar cperl-update-end)
4111 (defun cperl-delay-update-hook (beg end old-len)
4112 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
4113 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
4114
4115 (defun cperl-indent-region (start end)
4116 "Simple variant of indentation of region in CPerl mode.
4117 Should be slow. Will not indent comment if it starts at `comment-indent'
4118 or looks like continuation of the comment on the previous line.
4119 Indents all the lines whose first character is between START and END
4120 inclusive.
4121
4122 If `cperl-indent-region-fix-constructs', will improve spacing on
4123 conditional/loop constructs."
4124 (interactive "r")
4125 (cperl-update-syntaxification end end)
4126 (save-excursion
4127 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
4128 (let (st comm old-comm-indent new-comm-indent p pp i empty
4129 (indent-info (if cperl-emacs-can-parse
4130 (list nil nil nil) ; Cannot use '(), since will modify
4131 nil))
4132 after-change-functions ; Speed it up!
4133 (pm 0) (imenu-scanning-message "Indenting... (%3d%%)"))
4134 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
4135 (goto-char start)
4136 (setq old-comm-indent (and (cperl-to-comment-or-eol)
4137 (current-column))
4138 new-comm-indent old-comm-indent)
4139 (goto-char start)
4140 (setq end (set-marker (make-marker) end)) ; indentation changes pos
4141 (or (bolp) (beginning-of-line 2))
4142 (or (fboundp 'imenu-progress-message)
4143 (message "Indenting... For feedback load `imenu'..."))
4144 (while (and (<= (point) end) (not (eobp))) ; bol to check start
4145 (and (fboundp 'imenu-progress-message)
4146 (imenu-progress-message
4147 pm (/ (* 100 (- (point) start)) (- end start -1))))
4148 (setq st (point))
4149 (if (or
4150 (setq empty (looking-at "[ \t]*\n"))
4151 (and (setq comm (looking-at "[ \t]*#"))
4152 (or (eq (current-indentation) (or old-comm-indent
4153 comment-column))
4154 (setq old-comm-indent nil))))
4155 (if (and old-comm-indent
4156 (not empty)
4157 (= (current-indentation) old-comm-indent)
4158 (not (eq (get-text-property (point) 'syntax-type) 'pod))
4159 (not (eq (get-text-property (point) 'syntax-table)
4160 cperl-st-cfence)))
4161 (let ((comment-column new-comm-indent))
4162 (indent-for-comment)))
4163 (progn
4164 (setq i (cperl-indent-line indent-info))
4165 (or comm
4166 (not i)
4167 (progn
4168 (if cperl-indent-region-fix-constructs
4169 (goto-char (cperl-fix-line-spacing end indent-info)))
4170 (if (setq old-comm-indent
4171 (and (cperl-to-comment-or-eol)
4172 (not (memq (get-text-property (point)
4173 'syntax-type)
4174 '(pod here-doc)))
4175 (not (eq (get-text-property (point)
4176 'syntax-table)
4177 cperl-st-cfence))
4178 (current-column)))
4179 (progn (indent-for-comment)
4180 (skip-chars-backward " \t")
4181 (skip-chars-backward "#")
4182 (setq new-comm-indent (current-column))))))))
4183 (beginning-of-line 2))
4184 (if (fboundp 'imenu-progress-message)
4185 (imenu-progress-message pm 100)
4186 (message nil)))
4187 ;; Now run the update hooks
4188 (if after-change-functions
4189 (save-excursion
4190 (if cperl-update-end
4191 (progn
4192 (goto-char cperl-update-end)
4193 (insert " ")
4194 (delete-char -1)
4195 (goto-char cperl-update-start)
4196 (insert " ")
4197 (delete-char -1))))))))
4198
4199 ;; Stolen from lisp-mode with a lot of improvements
4200
4201 (defun cperl-fill-paragraph (&optional justify iteration)
4202 "Like \\[fill-paragraph], but handle CPerl comments.
4203 If any of the current line is a comment, fill the comment or the
4204 block of it that point is in, preserving the comment's initial
4205 indentation and initial hashes. Behaves usually outside of comment."
4206 (interactive "P")
4207 (let (
4208 ;; Non-nil if the current line contains a comment.
4209 has-comment
4210
4211 ;; If has-comment, the appropriate fill-prefix for the comment.
4212 comment-fill-prefix
4213 ;; Line that contains code and comment (or nil)
4214 start
4215 c spaces len dc (comment-column comment-column))
4216 ;; Figure out what kind of comment we are looking at.
4217 (save-excursion
4218 (beginning-of-line)
4219 (cond
4220
4221 ;; A line with nothing but a comment on it?
4222 ((looking-at "[ \t]*#[# \t]*")
4223 (setq has-comment t
4224 comment-fill-prefix (buffer-substring (match-beginning 0)
4225 (match-end 0))))
4226
4227 ;; A line with some code, followed by a comment? Remember that the
4228 ;; semi which starts the comment shouldn't be part of a string or
4229 ;; character.
4230 ((cperl-to-comment-or-eol)
4231 (setq has-comment t)
4232 (looking-at "#+[ \t]*")
4233 (setq start (point) c (current-column)
4234 comment-fill-prefix
4235 (concat (make-string (current-column) ?\ )
4236 (buffer-substring (match-beginning 0) (match-end 0)))
4237 spaces (progn (skip-chars-backward " \t")
4238 (buffer-substring (point) start))
4239 dc (- c (current-column)) len (- start (point))
4240 start (point-marker))
4241 (delete-char len)
4242 (insert (make-string dc ?-)))))
4243 (if (not has-comment)
4244 (fill-paragraph justify) ; Do the usual thing outside of comment
4245 ;; Narrow to include only the comment, and then fill the region.
4246 (save-restriction
4247 (narrow-to-region
4248 ;; Find the first line we should include in the region to fill.
4249 (if start (progn (beginning-of-line) (point))
4250 (save-excursion
4251 (while (and (zerop (forward-line -1))
4252 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
4253 ;; We may have gone to far. Go forward again.
4254 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
4255 (forward-line 1))
4256 (point)))
4257 ;; Find the beginning of the first line past the region to fill.
4258 (save-excursion
4259 (while (progn (forward-line 1)
4260 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
4261 (point)))
4262 ;; Remove existing hashes
4263 (goto-char (point-min))
4264 (while (progn (forward-line 1) (< (point) (point-max)))
4265 (skip-chars-forward " \t")
4266 (and (looking-at "#+")
4267 (delete-char (- (match-end 0) (match-beginning 0)))))
4268
4269 ;; Lines with only hashes on them can be paragraph boundaries.
4270 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
4271 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
4272 (fill-prefix comment-fill-prefix))
4273 (fill-paragraph justify)))
4274 (if (and start)
4275 (progn
4276 (goto-char start)
4277 (if (> dc 0)
4278 (progn (delete-char dc) (insert spaces)))
4279 (if (or (= (current-column) c) iteration) nil
4280 (setq comment-column c)
4281 (indent-for-comment)
4282 ;; Repeat once more, flagging as iteration
4283 (cperl-fill-paragraph justify t)))))))
4284
4285 (defun cperl-do-auto-fill ()
4286 ;; Break out if the line is short enough
4287 (if (> (save-excursion
4288 (end-of-line)
4289 (current-column))
4290 fill-column)
4291 (let ((c (save-excursion (beginning-of-line)
4292 (cperl-to-comment-or-eol) (point)))
4293 (s (memq (following-char) '(?\ ?\t))) marker)
4294 (if (>= c (point)) nil
4295 (setq marker (point-marker))
4296 (cperl-fill-paragraph)
4297 (goto-char marker)
4298 ;; Is not enough, sometimes marker is a start of line
4299 (if (bolp) (progn (re-search-forward "#+[ \t]*")
4300 (goto-char (match-end 0))))
4301 ;; Following space could have gone:
4302 (if (or (not s) (memq (following-char) '(?\ ?\t))) nil
4303 (insert " ")
4304 (backward-char 1))
4305 ;; Previous space could have gone:
4306 (or (memq (preceding-char) '(?\ ?\t)) (insert " "))))))
4307
4308 (defun cperl-imenu-addback (lst &optional isback name)
4309 ;; We suppose that the lst is a DAG, unless the first element only
4310 ;; loops back, and ISBACK is set. Thus this function cannot be
4311 ;; applied twice without ISBACK set.
4312 (cond ((not cperl-imenu-addback) lst)
4313 (t
4314 (or name
4315 (setq name "+++BACK+++"))
4316 (mapcar (function (lambda (elt)
4317 (if (and (listp elt) (listp (cdr elt)))
4318 (progn
4319 ;; In the other order it goes up
4320 ;; one level only ;-(
4321 (setcdr elt (cons (cons name lst)
4322 (cdr elt)))
4323 (cperl-imenu-addback (cdr elt) t name)
4324 ))))
4325 (if isback (cdr lst) lst))
4326 lst)))
4327
4328 (defun cperl-imenu--create-perl-index (&optional regexp)
4329 (require 'imenu) ; May be called from TAGS creator
4330 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
4331 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
4332 (index-meth-alist '()) meth
4333 packages ends-ranges p marker
4334 (prev-pos 0) char fchar index index1 name (end-range 0) package)
4335 (goto-char (point-min))
4336 (if noninteractive
4337 (message "Scanning Perl for index")
4338 (imenu-progress-message prev-pos 0))
4339 (cperl-update-syntaxification (point-max) (point-max))
4340 ;; Search for the function
4341 (progn ;;save-match-data
4342 (while (re-search-forward
4343 (or regexp cperl-imenu--function-name-regexp-perl)
4344 nil t)
4345 (or noninteractive
4346 (imenu-progress-message prev-pos))
4347 (cond
4348 ((and ; Skip some noise if building tags
4349 (match-beginning 2) ; package or sub
4350 (eq (char-after (match-beginning 2)) ?p) ; package
4351 (not (save-match-data
4352 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
4353 nil)
4354 ((and
4355 (match-beginning 2) ; package or sub
4356 ;; Skip if quoted (will not skip multi-line ''-strings :-():
4357 (null (get-text-property (match-beginning 1) 'syntax-table))
4358 (null (get-text-property (match-beginning 1) 'syntax-type))
4359 (null (get-text-property (match-beginning 1) 'in-pod)))
4360 (save-excursion
4361 (goto-char (match-beginning 2))
4362 (setq fchar (following-char))
4363 )
4364 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
4365 ;; (goto-char (match-end 0))) ; Messes what follows
4366 (setq char (following-char) ; ?\; for "sub foo () ;"
4367 meth nil
4368 p (point))
4369 (while (and ends-ranges (>= p (car ends-ranges)))
4370 ;; delete obsolete entries
4371 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
4372 (setq package (or (car packages) "")
4373 end-range (or (car ends-ranges) 0))
4374 (if (eq fchar ?p)
4375 (setq name (buffer-substring (match-beginning 3) (match-end 3))
4376 name (progn
4377 (set-text-properties 0 (length name) nil name)
4378 name)
4379 package (concat name "::")
4380 name (concat "package " name)
4381 end-range
4382 (save-excursion
4383 (parse-partial-sexp (point) (point-max) -1) (point))
4384 ends-ranges (cons end-range ends-ranges)
4385 packages (cons package packages)))
4386 ;; )
4387 ;; Skip this function name if it is a prototype declaration.
4388 (if (and (eq fchar ?s) (eq char ?\;)) nil
4389 (setq name (buffer-substring (match-beginning 3) (match-end 3))
4390 marker (make-marker))
4391 (set-text-properties 0 (length name) nil name)
4392 (set-marker marker (match-end 3))
4393 (if (eq fchar ?p)
4394 (setq name (concat "package " name))
4395 (cond ((string-match "[:']" name)
4396 (setq meth t))
4397 ((> p end-range) nil)
4398 (t
4399 (setq name (concat package name) meth t))))
4400 (setq index (cons name marker))
4401 (if (eq fchar ?p)
4402 (push index index-pack-alist)
4403 (push index index-alist))
4404 (if meth (push index index-meth-alist))
4405 (push index index-unsorted-alist)))
4406 ((match-beginning 5) ; Pod section
4407 ;; (beginning-of-line)
4408 (setq index (imenu-example--name-and-position)
4409 name (buffer-substring (match-beginning 6) (match-end 6)))
4410 (set-text-properties 0 (length name) nil name)
4411 (if (eq (char-after (match-beginning 5)) ?2)
4412 (setq name (concat " " name)))
4413 (setcar index name)
4414 (setq index1 (cons (concat "=" name) (cdr index)))
4415 (push index index-pod-alist)
4416 (push index1 index-unsorted-alist)))))
4417 (or noninteractive
4418 (imenu-progress-message prev-pos 100))
4419 (setq index-alist
4420 (if (default-value 'imenu-sort-function)
4421 (sort index-alist (default-value 'imenu-sort-function))
4422 (nreverse index-alist)))
4423 (and index-pod-alist
4424 (push (cons "+POD headers+..."
4425 (nreverse index-pod-alist))
4426 index-alist))
4427 (and (or index-pack-alist index-meth-alist)
4428 (let ((lst index-pack-alist) hier-list pack elt group name)
4429 ;; Remove "package ", reverse and uniquify.
4430 (while lst
4431 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
4432 (if (assoc name hier-list) nil
4433 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
4434 (setq lst index-meth-alist)
4435 (while lst
4436 (setq elt (car lst) lst (cdr lst))
4437 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
4438 (setq pack (substring (car elt) 0 (match-beginning 0)))
4439 (if (setq group (assoc pack hier-list))
4440 (if (listp (cdr group))
4441 ;; Have some functions already
4442 (setcdr group
4443 (cons (cons (substring
4444 (car elt)
4445 (+ 2 (match-beginning 0)))
4446 (cdr elt))
4447 (cdr group)))
4448 (setcdr group (list (cons (substring
4449 (car elt)
4450 (+ 2 (match-beginning 0)))
4451 (cdr elt)))))
4452 (setq hier-list
4453 (cons (cons pack
4454 (list (cons (substring
4455 (car elt)
4456 (+ 2 (match-beginning 0)))
4457 (cdr elt))))
4458 hier-list))))))
4459 (push (cons "+Hierarchy+..."
4460 hier-list)
4461 index-alist)))
4462 (and index-pack-alist
4463 (push (cons "+Packages+..."
4464 (nreverse index-pack-alist))
4465 index-alist))
4466 (and (or index-pack-alist index-pod-alist
4467 (default-value 'imenu-sort-function))
4468 index-unsorted-alist
4469 (push (cons "+Unsorted List+..."
4470 (nreverse index-unsorted-alist))
4471 index-alist))
4472 (cperl-imenu-addback index-alist)))
4473
4474 \f
4475 ;; Suggested by Mark A. Hershberger
4476 (defun cperl-outline-level ()
4477 (looking-at outline-regexp)
4478 (cond ((not (match-beginning 1)) 0) ; beginning-of-file
4479 ((match-beginning 2)
4480 (if (eq (char-after (match-beginning 2)) ?p)
4481 0 ; package
4482 1)) ; sub
4483 ((match-beginning 5)
4484 (if (eq (char-after (match-beginning 5)) ?1)
4485 1 ; head1
4486 2)) ; head2
4487 (t 3))) ; should not happen
4488
4489 \f
4490 (defvar cperl-compilation-error-regexp-alist
4491 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORK).
4492 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
4493 2 3))
4494 "Alist that specifies how to match errors in perl output.")
4495
4496 (if (fboundp 'eval-after-load)
4497 (eval-after-load
4498 "mode-compile"
4499 '(setq perl-compilation-error-regexp-alist
4500 cperl-compilation-error-regexp-alist)))
4501
4502
4503 (defun cperl-windowed-init ()
4504 "Initialization under windowed version."
4505 (if (or (featurep 'ps-print) cperl-faces-init)
4506 ;; Need to init anyway:
4507 (or cperl-faces-init (cperl-init-faces))
4508 (add-hook 'font-lock-mode-hook
4509 (function
4510 (lambda ()
4511 (if (memq major-mode '(perl-mode cperl-mode))
4512 (progn
4513 (or cperl-faces-init (cperl-init-faces)))))))
4514 (if (fboundp 'eval-after-load)
4515 (eval-after-load
4516 "ps-print"
4517 '(or cperl-faces-init (cperl-init-faces))))))
4518
4519 (defvar cperl-font-lock-keywords-1 nil
4520 "Additional expressions to highlight in Perl mode. Minimal set.")
4521 (defvar cperl-font-lock-keywords nil
4522 "Additional expressions to highlight in Perl mode. Default set.")
4523 (defvar cperl-font-lock-keywords-2 nil
4524 "Additional expressions to highlight in Perl mode. Maximal set")
4525
4526 (defun cperl-load-font-lock-keywords ()
4527 (or cperl-faces-init (cperl-init-faces))
4528 cperl-font-lock-keywords)
4529
4530 (defun cperl-load-font-lock-keywords-1 ()
4531 (or cperl-faces-init (cperl-init-faces))
4532 cperl-font-lock-keywords-1)
4533
4534 (defun cperl-load-font-lock-keywords-2 ()
4535 (or cperl-faces-init (cperl-init-faces))
4536 cperl-font-lock-keywords-2)
4537
4538 (defun cperl-init-faces-weak ()
4539 ;; Allow `cperl-find-pods-heres' to run.
4540 (or (boundp 'font-lock-constant-face)
4541 (cperl-force-face font-lock-constant-face
4542 "Face for constant and label names")
4543 ;;(setq font-lock-constant-face 'font-lock-constant-face)
4544 ))
4545
4546 (defun cperl-init-faces ()
4547 (condition-case errs
4548 (progn
4549 (require 'font-lock)
4550 (and (fboundp 'font-lock-fontify-anchored-keywords)
4551 (featurep 'font-lock-extra)
4552 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
4553 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
4554 (if (fboundp 'font-lock-fontify-anchored-keywords)
4555 (setq font-lock-anchored t))
4556 (setq
4557 t-font-lock-keywords
4558 (list
4559 `("[ \t]+$" 0 ',cperl-invalid-face t)
4560 (cons
4561 (concat
4562 "\\(^\\|[^$@%&\\]\\)\\<\\("
4563 (mapconcat
4564 'identity
4565 '("if" "until" "while" "elsif" "else" "unless" "for"
4566 "foreach" "continue" "exit" "die" "last" "goto" "next"
4567 "redo" "return" "local" "exec" "sub" "do" "dump" "use"
4568 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
4569 "\\|") ; Flow control
4570 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
4571 ; In what follows we use `type' style
4572 ; for overwritable builtins
4573 (list
4574 (concat
4575 "\\(^\\|[^$@%&\\]\\)\\<\\("
4576 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm"
4577 ;; "and" "atan2" "bind" "binmode" "bless" "caller"
4578 ;; "chdir" "chmod" "chown" "chr" "chroot" "close"
4579 ;; "closedir" "cmp" "connect" "continue" "cos" "crypt"
4580 ;; "dbmclose" "dbmopen" "die" "dump" "endgrent"
4581 ;; "endhostent" "endnetent" "endprotoent" "endpwent"
4582 ;; "endservent" "eof" "eq" "exec" "exit" "exp" "fcntl"
4583 ;; "fileno" "flock" "fork" "formline" "ge" "getc"
4584 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr"
4585 ;; "gethostbyname" "gethostent" "getlogin"
4586 ;; "getnetbyaddr" "getnetbyname" "getnetent"
4587 ;; "getpeername" "getpgrp" "getppid" "getpriority"
4588 ;; "getprotobyname" "getprotobynumber" "getprotoent"
4589 ;; "getpwent" "getpwnam" "getpwuid" "getservbyname"
4590 ;; "getservbyport" "getservent" "getsockname"
4591 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int"
4592 ;; "ioctl" "join" "kill" "lc" "lcfirst" "le" "length"
4593 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
4594 ;; "mkdir" "msgctl" "msgget" "msgrcv" "msgsnd" "ne"
4595 ;; "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
4596 ;; "quotemeta" "rand" "read" "readdir" "readline"
4597 ;; "readlink" "readpipe" "recv" "ref" "rename" "require"
4598 ;; "reset" "reverse" "rewinddir" "rindex" "rmdir" "seek"
4599 ;; "seekdir" "select" "semctl" "semget" "semop" "send"
4600 ;; "setgrent" "sethostent" "setnetent" "setpgrp"
4601 ;; "setpriority" "setprotoent" "setpwent" "setservent"
4602 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite"
4603 ;; "shutdown" "sin" "sleep" "socket" "socketpair"
4604 ;; "sprintf" "sqrt" "srand" "stat" "substr" "symlink"
4605 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
4606 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
4607 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
4608 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
4609 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
4610 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
4611 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
4612 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
4613 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
4614 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
4615 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
4616 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
4617 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
4618 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
4619 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
4620 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
4621 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
4622 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
4623 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
4624 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
4625 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
4626 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
4627 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
4628 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
4629 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
4630 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
4631 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
4632 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
4633 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
4634 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
4635 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
4636 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
4637 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
4638 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
4639 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\|PACKAGE__\\)"
4640 "\\)\\>") 2 'font-lock-type-face)
4641 ;; In what follows we use `other' style
4642 ;; for nonoverwritable builtins
4643 ;; Somehow 's', 'm' are not auto-generated???
4644 (list
4645 (concat
4646 "\\(^\\|[^$@%&\\]\\)\\<\\("
4647 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
4648 ;; "chop" "defined" "delete" "do" "each" "else" "elsif"
4649 ;; "eval" "exists" "for" "foreach" "format" "goto"
4650 ;; "grep" "if" "keys" "last" "local" "map" "my" "next"
4651 ;; "no" "package" "pop" "pos" "print" "printf" "push"
4652 ;; "q" "qq" "qw" "qx" "redo" "return" "scalar" "shift"
4653 ;; "sort" "splice" "split" "study" "sub" "tie" "tr"
4654 ;; "undef" "unless" "unshift" "untie" "until" "use"
4655 ;; "while" "y"
4656 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
4657 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
4658 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
4659 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
4660 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
4661 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
4662 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
4663 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
4664 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
4665 "\\|[sm]" ; Added manually
4666 "\\)\\>") 2 'cperl-nonoverridable-face)
4667 ;; (mapconcat 'identity
4668 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
4669 ;; "#include" "#define" "#undef")
4670 ;; "\\|")
4671 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
4672 font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
4673 '("\\<sub[ \t]+\\([^ \t{;()]+\\)[ \t]*\\(([^()]*)[ \t]*\\)?[#{\n]" 1
4674 font-lock-function-name-face)
4675 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
4676 2 font-lock-function-name-face)
4677 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
4678 1 font-lock-function-name-face)
4679 (cond ((featurep 'font-lock-extra)
4680 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
4681 (2 font-lock-string-face t)
4682 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
4683 (font-lock-anchored
4684 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
4685 (2 font-lock-string-face t)
4686 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
4687 nil nil
4688 (1 font-lock-string-face t))))
4689 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
4690 2 font-lock-string-face t)))
4691 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
4692 font-lock-string-face t)
4693 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
4694 font-lock-constant-face) ; labels
4695 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
4696 2 font-lock-constant-face)
4697 ;; Uncomment to get perl-mode-like vars
4698 ;;; '("[$*]{?\\(\\sw+\\)" 1 font-lock-variable-name-face)
4699 ;;; '("\\([@%]\\|\\$#\\)\\(\\sw+\\)"
4700 ;;; (2 (cons font-lock-variable-name-face '(underline))))
4701 (cond ((featurep 'font-lock-extra)
4702 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
4703 (3 font-lock-variable-name-face)
4704 (4 '(another 4 nil
4705 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
4706 (1 font-lock-variable-name-face)
4707 (2 '(restart 2 nil) nil t)))
4708 nil t))) ; local variables, multiple
4709 (font-lock-anchored
4710 '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
4711 (3 font-lock-variable-name-face)
4712 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)"
4713 nil nil
4714 (1 font-lock-variable-name-face))))
4715 (t '("^[ \t{}]*\\(my\\|local\\our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
4716 3 font-lock-variable-name-face)))
4717 '("\\<for\\(each\\)?\\([ \t]+\\(my\\|local\\|our\\)\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
4718 4 font-lock-variable-name-face)))
4719 (setq
4720 t-font-lock-keywords-1
4721 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
4722 (not cperl-xemacs-p) ; not yet as of XEmacs 19.12
4723 '(
4724 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
4725 (if (eq (char-after (match-beginning 2)) ?%)
4726 cperl-hash-face
4727 cperl-array-face)
4728 t) ; arrays and hashes
4729 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
4730 1
4731 (if (= (- (match-end 2) (match-beginning 2)) 1)
4732 (if (eq (char-after (match-beginning 3)) ?{)
4733 cperl-hash-face
4734 cperl-array-face) ; arrays and hashes
4735 font-lock-variable-name-face) ; Just to put something
4736 t)
4737 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
4738 ;;; Too much noise from \s* @s[ and friends
4739 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
4740 ;;(3 font-lock-function-name-face t t)
4741 ;;(4
4742 ;; (if (cperl-slash-is-regexp)
4743 ;; font-lock-function-name-face 'default) nil t))
4744 )))
4745 (if cperl-highlight-variables-indiscriminately
4746 (setq t-font-lock-keywords-1
4747 (append t-font-lock-keywords-1
4748 (list '("[$*]{?\\(\\sw+\\)" 1
4749 font-lock-variable-name-face)))))
4750 (setq cperl-font-lock-keywords-1
4751 (if cperl-syntaxify-by-font-lock
4752 (cons 'cperl-fontify-update
4753 t-font-lock-keywords)
4754 t-font-lock-keywords)
4755 cperl-font-lock-keywords cperl-font-lock-keywords-1
4756 cperl-font-lock-keywords-2 (append
4757 cperl-font-lock-keywords-1
4758 t-font-lock-keywords-1)))
4759 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
4760 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
4761 (eval ; Avoid a warning
4762 '(font-lock-require-faces
4763 (list
4764 ;; Color-light Color-dark Gray-light Gray-dark Mono
4765 (list 'font-lock-comment-face
4766 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
4767 nil
4768 [nil nil t t t]
4769 [nil nil t t t]
4770 nil)
4771 (list 'font-lock-string-face
4772 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
4773 nil
4774 nil
4775 [nil nil t t t]
4776 nil)
4777 (list 'font-lock-function-name-face
4778 (vector
4779 "Blue" "LightSkyBlue" "Gray50" "LightGray"
4780 (cdr (assq 'background-color ; if mono
4781 (frame-parameters))))
4782 (vector
4783 nil nil nil nil
4784 (cdr (assq 'foreground-color ; if mono
4785 (frame-parameters))))
4786 [nil nil t t t]
4787 nil
4788 nil)
4789 (list 'font-lock-variable-name-face
4790 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
4791 nil
4792 [nil nil t t t]
4793 [nil nil t t t]
4794 nil)
4795 (list 'font-lock-type-face
4796 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
4797 nil
4798 [nil nil t t t]
4799 nil
4800 [nil nil t t t]
4801 )
4802 (list 'font-lock-constant-face
4803 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
4804 nil
4805 [nil nil t t t]
4806 nil
4807 [nil nil t t t]
4808 )
4809 (list 'cperl-nonoverridable-face
4810 ["chartreuse3" ("orchid1" "orange")
4811 nil "Gray80"]
4812 [nil nil "gray90"]
4813 [nil nil nil t t]
4814 [nil nil t t]
4815 [nil nil t t t]
4816 )
4817 (list 'cperl-array-face
4818 ["blue" "yellow" nil "Gray80"]
4819 ["lightyellow2" ("navy" "os2blue" "darkgreen")
4820 "gray90"]
4821 t
4822 nil
4823 nil)
4824 (list 'cperl-hash-face
4825 ["red" "red" nil "Gray80"]
4826 ["lightyellow2" ("navy" "os2blue" "darkgreen")
4827 "gray90"]
4828 t
4829 t
4830 nil))))
4831 ;; Do it the dull way, without choose-color
4832 (defvar cperl-guessed-background nil
4833 "Display characteristics as guessed by cperl.")
4834 ;; (or (fboundp 'x-color-defined-p)
4835 ;; (defalias 'x-color-defined-p
4836 ;; (cond ((fboundp 'color-defined-p) 'color-defined-p)
4837 ;; ;; XEmacs >= 19.12
4838 ;; ((fboundp 'valid-color-name-p) 'valid-color-name-p)
4839 ;; ;; XEmacs 19.11
4840 ;; (t 'x-valid-color-name-p))))
4841 (cperl-force-face font-lock-constant-face
4842 "Face for constant and label names")
4843 (cperl-force-face font-lock-variable-name-face
4844 "Face for variable names")
4845 (cperl-force-face font-lock-type-face
4846 "Face for data types")
4847 (cperl-force-face cperl-nonoverridable-face
4848 "Face for data types from another group")
4849 (cperl-force-face font-lock-comment-face
4850 "Face for comments")
4851 (cperl-force-face font-lock-function-name-face
4852 "Face for function names")
4853 (cperl-force-face cperl-hash-face
4854 "Face for hashes")
4855 (cperl-force-face cperl-array-face
4856 "Face for arrays")
4857 ;;(defvar font-lock-constant-face 'font-lock-constant-face)
4858 ;;(defvar font-lock-variable-name-face 'font-lock-variable-name-face)
4859 ;;(or (boundp 'font-lock-type-face)
4860 ;; (defconst font-lock-type-face
4861 ;; 'font-lock-type-face
4862 ;; "Face to use for data types."))
4863 ;;(or (boundp 'cperl-nonoverridable-face)
4864 ;; (defconst cperl-nonoverridable-face
4865 ;; 'cperl-nonoverridable-face
4866 ;; "Face to use for data types from another group."))
4867 ;;(if (not cperl-xemacs-p) nil
4868 ;; (or (boundp 'font-lock-comment-face)
4869 ;; (defconst font-lock-comment-face
4870 ;; 'font-lock-comment-face
4871 ;; "Face to use for comments."))
4872 ;; (or (boundp 'font-lock-keyword-face)
4873 ;; (defconst font-lock-keyword-face
4874 ;; 'font-lock-keyword-face
4875 ;; "Face to use for keywords."))
4876 ;; (or (boundp 'font-lock-function-name-face)
4877 ;; (defconst font-lock-function-name-face
4878 ;; 'font-lock-function-name-face
4879 ;; "Face to use for function names.")))
4880 (if (and
4881 (not (cperl-is-face 'cperl-array-face))
4882 (cperl-is-face 'font-lock-emphasized-face))
4883 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
4884 (if (and
4885 (not (cperl-is-face 'cperl-hash-face))
4886 (cperl-is-face 'font-lock-other-emphasized-face))
4887 (copy-face 'font-lock-other-emphasized-face
4888 'cperl-hash-face))
4889 (if (and
4890 (not (cperl-is-face 'cperl-nonoverridable-face))
4891 (cperl-is-face 'font-lock-other-type-face))
4892 (copy-face 'font-lock-other-type-face
4893 'cperl-nonoverridable-face))
4894 ;;(or (boundp 'cperl-hash-face)
4895 ;; (defconst cperl-hash-face
4896 ;; 'cperl-hash-face
4897 ;; "Face to use for hashes."))
4898 ;;(or (boundp 'cperl-array-face)
4899 ;; (defconst cperl-array-face
4900 ;; 'cperl-array-face
4901 ;; "Face to use for arrays."))
4902 ;; Here we try to guess background
4903 (let ((background
4904 (if (boundp 'font-lock-background-mode)
4905 font-lock-background-mode
4906 'light))
4907 (face-list (and (fboundp 'face-list) (face-list)))
4908 ;; cperl-is-face
4909 )
4910 ;;;; (fset 'cperl-is-face
4911 ;;;; (cond ((fboundp 'find-face)
4912 ;;;; (symbol-function 'find-face))
4913 ;;;; (face-list
4914 ;;;; (function (lambda (face) (member face face-list))))
4915 ;;;; (t
4916 ;;;; (function (lambda (face) (boundp face))))))
4917 (defvar cperl-guessed-background
4918 (if (and (boundp 'font-lock-display-type)
4919 (eq font-lock-display-type 'grayscale))
4920 'gray
4921 background)
4922 "Background as guessed by CPerl mode")
4923 (if (and
4924 (not (cperl-is-face 'font-lock-constant-face))
4925 (cperl-is-face 'font-lock-reference-face))
4926 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
4927 (if (cperl-is-face 'font-lock-type-face) nil
4928 (copy-face 'default 'font-lock-type-face)
4929 (cond
4930 ((eq background 'light)
4931 (set-face-foreground 'font-lock-type-face
4932 (if (x-color-defined-p "seagreen")
4933 "seagreen"
4934 "sea green")))
4935 ((eq background 'dark)
4936 (set-face-foreground 'font-lock-type-face
4937 (if (x-color-defined-p "os2pink")
4938 "os2pink"
4939 "pink")))
4940 (t
4941 (set-face-background 'font-lock-type-face "gray90"))))
4942 (if (cperl-is-face 'cperl-nonoverridable-face)
4943 nil
4944 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
4945 (cond
4946 ((eq background 'light)
4947 (set-face-foreground 'cperl-nonoverridable-face
4948 (if (x-color-defined-p "chartreuse3")
4949 "chartreuse3"
4950 "chartreuse")))
4951 ((eq background 'dark)
4952 (set-face-foreground 'cperl-nonoverridable-face
4953 (if (x-color-defined-p "orchid1")
4954 "orchid1"
4955 "orange")))))
4956 ;;; (if (cperl-is-face 'font-lock-other-emphasized-face) nil
4957 ;;; (copy-face 'bold-italic 'font-lock-other-emphasized-face)
4958 ;;; (cond
4959 ;;; ((eq background 'light)
4960 ;;; (set-face-background 'font-lock-other-emphasized-face
4961 ;;; (if (x-color-defined-p "lightyellow2")
4962 ;;; "lightyellow2"
4963 ;;; (if (x-color-defined-p "lightyellow")
4964 ;;; "lightyellow"
4965 ;;; "light yellow"))))
4966 ;;; ((eq background 'dark)
4967 ;;; (set-face-background 'font-lock-other-emphasized-face
4968 ;;; (if (x-color-defined-p "navy")
4969 ;;; "navy"
4970 ;;; (if (x-color-defined-p "darkgreen")
4971 ;;; "darkgreen"
4972 ;;; "dark green"))))
4973 ;;; (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
4974 ;;; (if (cperl-is-face 'font-lock-emphasized-face) nil
4975 ;;; (copy-face 'bold 'font-lock-emphasized-face)
4976 ;;; (cond
4977 ;;; ((eq background 'light)
4978 ;;; (set-face-background 'font-lock-emphasized-face
4979 ;;; (if (x-color-defined-p "lightyellow2")
4980 ;;; "lightyellow2"
4981 ;;; "lightyellow")))
4982 ;;; ((eq background 'dark)
4983 ;;; (set-face-background 'font-lock-emphasized-face
4984 ;;; (if (x-color-defined-p "navy")
4985 ;;; "navy"
4986 ;;; (if (x-color-defined-p "darkgreen")
4987 ;;; "darkgreen"
4988 ;;; "dark green"))))
4989 ;;; (t (set-face-background 'font-lock-emphasized-face "gray90"))))
4990 (if (cperl-is-face 'font-lock-variable-name-face) nil
4991 (copy-face 'italic 'font-lock-variable-name-face))
4992 (if (cperl-is-face 'font-lock-constant-face) nil
4993 (copy-face 'italic 'font-lock-constant-face))))
4994 (setq cperl-faces-init t))
4995 (error (message "cperl-init-faces (ignored): %s" errs))))
4996
4997
4998 (defun cperl-ps-print-init ()
4999 "Initialization of `ps-print' components for faces used in CPerl."
5000 (eval-after-load "ps-print"
5001 '(setq ps-bold-faces
5002 ;; font-lock-variable-name-face
5003 ;; font-lock-constant-face
5004 (append '(cperl-array-face
5005 cperl-hash-face)
5006 ps-bold-faces)
5007 ps-italic-faces
5008 ;; font-lock-constant-face
5009 (append '(cperl-nonoverridable-face
5010 cperl-hash-face)
5011 ps-italic-faces)
5012 ps-underlined-faces
5013 ;; font-lock-type-face
5014 (append '(cperl-array-face
5015 cperl-hash-face
5016 underline
5017 cperl-nonoverridable-face)
5018 ps-underlined-faces))))
5019
5020 (defvar ps-print-face-extension-alist)
5021
5022 (defun cperl-ps-print (&optional file)
5023 "Pretty-print in CPerl style.
5024 If optional argument FILE is an empty string, prints to printer, otherwise
5025 to the file FILE. If FILE is nil, prompts for a file name.
5026
5027 Style of printout regulated by the variable `cperl-ps-print-face-properties'."
5028 (interactive)
5029 (or file
5030 (setq file (read-from-minibuffer
5031 "Print to file (if empty - to printer): "
5032 (concat (buffer-file-name) ".ps")
5033 nil nil 'file-name-history)))
5034 (or (> (length file) 0)
5035 (setq file nil))
5036 (require 'ps-print) ; To get ps-print-face-extension-alist
5037 (let ((ps-print-color-p t)
5038 (ps-print-face-extension-alist ps-print-face-extension-alist))
5039 (cperl-ps-extend-face-list cperl-ps-print-face-properties)
5040 (ps-print-buffer-with-faces file)))
5041
5042 ;;; (defun cperl-ps-print-init ()
5043 ;;; "Initialization of `ps-print' components for faces used in CPerl."
5044 ;;; ;; Guard against old versions
5045 ;;; (defvar ps-underlined-faces nil)
5046 ;;; (defvar ps-bold-faces nil)
5047 ;;; (defvar ps-italic-faces nil)
5048 ;;; (setq ps-bold-faces
5049 ;;; (append '(font-lock-emphasized-face
5050 ;;; cperl-array-face
5051 ;;; font-lock-keyword-face
5052 ;;; font-lock-variable-name-face
5053 ;;; font-lock-constant-face
5054 ;;; font-lock-reference-face
5055 ;;; font-lock-other-emphasized-face
5056 ;;; cperl-hash-face)
5057 ;;; ps-bold-faces))
5058 ;;; (setq ps-italic-faces
5059 ;;; (append '(cperl-nonoverridable-face
5060 ;;; font-lock-constant-face
5061 ;;; font-lock-reference-face
5062 ;;; font-lock-other-emphasized-face
5063 ;;; cperl-hash-face)
5064 ;;; ps-italic-faces))
5065 ;;; (setq ps-underlined-faces
5066 ;;; (append '(font-lock-emphasized-face
5067 ;;; cperl-array-face
5068 ;;; font-lock-other-emphasized-face
5069 ;;; cperl-hash-face
5070 ;;; cperl-nonoverridable-face font-lock-type-face)
5071 ;;; ps-underlined-faces))
5072 ;;; (cons 'font-lock-type-face ps-underlined-faces))
5073
5074
5075 (if (cperl-enable-font-lock) (cperl-windowed-init))
5076
5077 (defconst cperl-styles-entries
5078 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
5079 cperl-label-offset cperl-extra-newline-before-brace
5080 cperl-merge-trailing-else
5081 cperl-continued-statement-offset))
5082
5083 (defconst cperl-style-alist
5084 '(("CPerl" ; =GNU without extra-newline-before-brace
5085 (cperl-indent-level . 2)
5086 (cperl-brace-offset . 0)
5087 (cperl-continued-brace-offset . 0)
5088 (cperl-label-offset . -2)
5089 (cperl-extra-newline-before-brace . nil)
5090 (cperl-merge-trailing-else . t)
5091 (cperl-continued-statement-offset . 2))
5092 ("PerlStyle" ; CPerl with 4 as indent
5093 (cperl-indent-level . 4)
5094 (cperl-brace-offset . 0)
5095 (cperl-continued-brace-offset . 0)
5096 (cperl-label-offset . -4)
5097 (cperl-extra-newline-before-brace . nil)
5098 (cperl-merge-trailing-else . t)
5099 (cperl-continued-statement-offset . 4))
5100 ("GNU"
5101 (cperl-indent-level . 2)
5102 (cperl-brace-offset . 0)
5103 (cperl-continued-brace-offset . 0)
5104 (cperl-label-offset . -2)
5105 (cperl-extra-newline-before-brace . t)
5106 (cperl-merge-trailing-else . nil)
5107 (cperl-continued-statement-offset . 2))
5108 ("K&R"
5109 (cperl-indent-level . 5)
5110 (cperl-brace-offset . 0)
5111 (cperl-continued-brace-offset . -5)
5112 (cperl-label-offset . -5)
5113 ;;(cperl-extra-newline-before-brace . nil) ; ???
5114 (cperl-merge-trailing-else . nil)
5115 (cperl-continued-statement-offset . 5))
5116 ("BSD"
5117 (cperl-indent-level . 4)
5118 (cperl-brace-offset . 0)
5119 (cperl-continued-brace-offset . -4)
5120 (cperl-label-offset . -4)
5121 ;;(cperl-extra-newline-before-brace . nil) ; ???
5122 (cperl-continued-statement-offset . 4))
5123 ("C++"
5124 (cperl-indent-level . 4)
5125 (cperl-brace-offset . 0)
5126 (cperl-continued-brace-offset . -4)
5127 (cperl-label-offset . -4)
5128 (cperl-continued-statement-offset . 4)
5129 (cperl-merge-trailing-else . nil)
5130 (cperl-extra-newline-before-brace . t))
5131 ("Current")
5132 ("Whitesmith"
5133 (cperl-indent-level . 4)
5134 (cperl-brace-offset . 0)
5135 (cperl-continued-brace-offset . 0)
5136 (cperl-label-offset . -4)
5137 ;;(cperl-extra-newline-before-brace . nil) ; ???
5138 (cperl-continued-statement-offset . 4)))
5139 "(Experimental) list of variables to set to get a particular indentation style.
5140 Should be used via `cperl-set-style' or via Perl menu.")
5141
5142 (defun cperl-set-style (style)
5143 "Set CPerl-mode variables to use one of several different indentation styles.
5144 The arguments are a string representing the desired style.
5145 The list of styles is in `cperl-style-alist', available styles
5146 are GNU, K&R, BSD, C++ and Whitesmith.
5147
5148 The current value of style is memorized (unless there is a memorized
5149 data already), may be restored by `cperl-set-style-back'.
5150
5151 Chosing \"Current\" style will not change style, so this may be used for
5152 side-effect of memorizing only."
5153 (interactive
5154 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
5155 cperl-style-alist)))
5156 (list (completing-read "Enter style: " list nil 'insist))))
5157 (or cperl-old-style
5158 (setq cperl-old-style
5159 (mapcar (function
5160 (lambda (name)
5161 (cons name (eval name))))
5162 cperl-styles-entries)))
5163 (let ((style (cdr (assoc style cperl-style-alist))) setting str sym)
5164 (while style
5165 (setq setting (car style) style (cdr style))
5166 (set (car setting) (cdr setting)))))
5167
5168 (defun cperl-set-style-back ()
5169 "Restore a style memorised by `cperl-set-style'."
5170 (interactive)
5171 (or cperl-old-style (error "The style was not changed"))
5172 (let (setting)
5173 (while cperl-old-style
5174 (setq setting (car cperl-old-style)
5175 cperl-old-style (cdr cperl-old-style))
5176 (set (car setting) (cdr setting)))))
5177
5178 (defun cperl-check-syntax ()
5179 (interactive)
5180 (require 'mode-compile)
5181 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
5182 (eval '(mode-compile)))) ; Avoid a warning
5183
5184 (defun cperl-info-buffer (type)
5185 ;; Returns buffer with documentation. Creates if missing.
5186 ;; If TYPE, this vars buffer.
5187 ;; Special care is taken to not stomp over an existing info buffer
5188 (let* ((bname (if type "*info-perl-var*" "*info-perl*"))
5189 (info (get-buffer bname))
5190 (oldbuf (get-buffer "*info*")))
5191 (if info info
5192 (save-window-excursion
5193 ;; Get Info running
5194 (require 'info)
5195 (cond (oldbuf
5196 (set-buffer oldbuf)
5197 (rename-buffer "*info-perl-tmp*")))
5198 (save-window-excursion
5199 (info))
5200 (Info-find-node cperl-info-page (if type "perlvar" "perlfunc"))
5201 (set-buffer "*info*")
5202 (rename-buffer bname)
5203 (cond (oldbuf
5204 (set-buffer "*info-perl-tmp*")
5205 (rename-buffer "*info*")
5206 (set-buffer bname)))
5207 (make-local-variable 'window-min-height)
5208 (setq window-min-height 2)
5209 (current-buffer)))))
5210
5211 (defun cperl-word-at-point (&optional p)
5212 ;; Returns the word at point or at P.
5213 (save-excursion
5214 (if p (goto-char p))
5215 (or (cperl-word-at-point-hard)
5216 (progn
5217 (require 'etags)
5218 (funcall (or (and (boundp 'find-tag-default-function)
5219 find-tag-default-function)
5220 (get major-mode 'find-tag-default-function)
5221 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
5222 ;; automatically used within `find-tag-default':
5223 'find-tag-default))))))
5224
5225 (defun cperl-info-on-command (command)
5226 "Show documentation for Perl command in other window.
5227 If perl-info buffer is shown in some frame, uses this frame.
5228 Customized by setting variables `cperl-shrink-wrap-info-frame',
5229 `cperl-max-help-size'."
5230 (interactive
5231 (let* ((default (cperl-word-at-point))
5232 (read (read-string
5233 (format "Find doc for Perl function (default %s): "
5234 default))))
5235 (list (if (equal read "")
5236 default
5237 read))))
5238
5239 (let ((buffer (current-buffer))
5240 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
5241 pos isvar height iniheight frheight buf win fr1 fr2 iniwin not-loner
5242 max-height char-height buf-list)
5243 (if (string-match "^-[a-zA-Z]$" command)
5244 (setq cmd-desc "^-X[ \t\n]"))
5245 (setq isvar (string-match "^[$@%]" command)
5246 buf (cperl-info-buffer isvar)
5247 iniwin (selected-window)
5248 fr1 (window-frame iniwin))
5249 (set-buffer buf)
5250 (beginning-of-buffer)
5251 (or isvar
5252 (progn (re-search-forward "^-X[ \t\n]")
5253 (forward-line -1)))
5254 (if (re-search-forward cmd-desc nil t)
5255 (progn
5256 ;; Go back to beginning of the group (ex, for qq)
5257 (if (re-search-backward "^[ \t\n\f]")
5258 (forward-line 1))
5259 (beginning-of-line)
5260 ;; Get some of
5261 (setq pos (point)
5262 buf-list (list buf "*info-perl-var*" "*info-perl*"))
5263 (while (and (not win) buf-list)
5264 (setq win (get-buffer-window (car buf-list) t))
5265 (setq buf-list (cdr buf-list)))
5266 (or (not win)
5267 (eq (window-buffer win) buf)
5268 (set-window-buffer win buf))
5269 (and win (setq fr2 (window-frame win)))
5270 (if (or (not fr2) (eq fr1 fr2))
5271 (pop-to-buffer buf)
5272 (special-display-popup-frame buf) ; Make it visible
5273 (select-window win))
5274 (goto-char pos) ; Needed (?!).
5275 ;; Resize
5276 (setq iniheight (window-height)
5277 frheight (frame-height)
5278 not-loner (< iniheight (1- frheight))) ; Are not alone
5279 (cond ((if not-loner cperl-max-help-size
5280 cperl-shrink-wrap-info-frame)
5281 (setq height
5282 (+ 2
5283 (count-lines
5284 pos
5285 (save-excursion
5286 (if (re-search-forward
5287 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
5288 (match-beginning 0) (point-max)))))
5289 max-height
5290 (if not-loner
5291 (/ (* (- frheight 3) cperl-max-help-size) 100)
5292 (setq char-height (frame-char-height))
5293 ;; Non-functioning under OS/2:
5294 (if (eq char-height 1) (setq char-height 18))
5295 ;; Title, menubar, + 2 for slack
5296 (- (/ (x-display-pixel-height) char-height) 4)
5297 ))
5298 (if (> height max-height) (setq height max-height))
5299 ;;(message "was %s doing %s" iniheight height)
5300 (if not-loner
5301 (enlarge-window (- height iniheight))
5302 (set-frame-height (window-frame win) (1+ height)))))
5303 (set-window-start (selected-window) pos))
5304 (message "No entry for %s found." command))
5305 ;;(pop-to-buffer buffer)
5306 (select-window iniwin)))
5307
5308 (defun cperl-info-on-current-command ()
5309 "Show documentation for Perl command at point in other window."
5310 (interactive)
5311 (cperl-info-on-command (cperl-word-at-point)))
5312
5313 (defun cperl-imenu-info-imenu-search ()
5314 (if (looking-at "^-X[ \t\n]") nil
5315 (re-search-backward
5316 "^\n\\([-a-zA-Z_]+\\)[ \t\n]")
5317 (forward-line 1)))
5318
5319 (defun cperl-imenu-info-imenu-name ()
5320 (buffer-substring
5321 (match-beginning 1) (match-end 1)))
5322
5323 (defun cperl-imenu-on-info ()
5324 (interactive)
5325 (let* ((buffer (current-buffer))
5326 imenu-create-index-function
5327 imenu-prev-index-position-function
5328 imenu-extract-index-name-function
5329 (index-item (save-restriction
5330 (save-window-excursion
5331 (set-buffer (cperl-info-buffer nil))
5332 (setq imenu-create-index-function
5333 'imenu-default-create-index-function
5334 imenu-prev-index-position-function
5335 'cperl-imenu-info-imenu-search
5336 imenu-extract-index-name-function
5337 'cperl-imenu-info-imenu-name)
5338 (imenu-choose-buffer-index)))))
5339 (and index-item
5340 (progn
5341 (push-mark)
5342 (pop-to-buffer "*info-perl*")
5343 (cond
5344 ((markerp (cdr index-item))
5345 (goto-char (marker-position (cdr index-item))))
5346 (t
5347 (goto-char (cdr index-item))))
5348 (set-window-start (selected-window) (point))
5349 (pop-to-buffer buffer)))))
5350
5351 (defun cperl-lineup (beg end &optional step minshift)
5352 "Lineup construction in a region.
5353 Beginning of region should be at the start of a construction.
5354 All first occurrences of this construction in the lines that are
5355 partially contained in the region are lined up at the same column.
5356
5357 MINSHIFT is the minimal amount of space to insert before the construction.
5358 STEP is the tabwidth to position constructions.
5359 If STEP is nil, `cperl-lineup-step' will be used
5360 \(or `cperl-indent-level', if `cperl-lineup-step' is `nil').
5361 Will not move the position at the start to the left."
5362 (interactive "r")
5363 (let (search col tcol seen b e)
5364 (save-excursion
5365 (goto-char end)
5366 (end-of-line)
5367 (setq end (point-marker))
5368 (goto-char beg)
5369 (skip-chars-forward " \t\f")
5370 (setq beg (point-marker))
5371 (indent-region beg end nil)
5372 (goto-char beg)
5373 (setq col (current-column))
5374 (if (looking-at "[a-zA-Z0-9_]")
5375 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
5376 (setq search
5377 (concat "\\<"
5378 (regexp-quote
5379 (buffer-substring (match-beginning 0)
5380 (match-end 0))) "\\>"))
5381 (error "Cannot line up in a middle of the word"))
5382 (if (looking-at "$")
5383 (error "Cannot line up end of line"))
5384 (setq search (regexp-quote (char-to-string (following-char)))))
5385 (setq step (or step cperl-lineup-step cperl-indent-level))
5386 (or minshift (setq minshift 1))
5387 (while (progn
5388 (beginning-of-line 2)
5389 (and (< (point) end)
5390 (re-search-forward search end t)
5391 (goto-char (match-beginning 0))))
5392 (setq tcol (current-column) seen t)
5393 (if (> tcol col) (setq col tcol)))
5394 (or seen
5395 (error "The construction to line up occurred only once"))
5396 (goto-char beg)
5397 (setq col (+ col minshift))
5398 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
5399 (while
5400 (progn
5401 (setq e (point))
5402 (skip-chars-backward " \t")
5403 (delete-region (point) e)
5404 (indent-to-column col); (make-string (- col (current-column)) ?\ ))
5405 (beginning-of-line 2)
5406 (and (< (point) end)
5407 (re-search-forward search end t)
5408 (goto-char (match-beginning 0)))))))) ; No body
5409
5410 (defun cperl-etags (&optional add all files)
5411 "Run etags with appropriate options for Perl files.
5412 If optional argument ALL is `recursive', will process Perl files
5413 in subdirectories too."
5414 (interactive)
5415 (let ((cmd "etags")
5416 (args '("-l" "none" "-r" "/\\<\\(package\\|sub\\)[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)[ \\t]*\\(([^()]*)[ \t]*\\)?\\([{#]\\|$\\)\\)/\\4/"))
5417 res)
5418 (if add (setq args (cons "-a" args)))
5419 (or files (setq files (list buffer-file-name)))
5420 (cond
5421 ((eq all 'recursive)
5422 ;;(error "Not implemented: recursive")
5423 (setq args (append (list "-e"
5424 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
5425 use File::Find;
5426 find(\\&wanted, '.');
5427 exec @ARGV;"
5428 cmd) args)
5429 cmd "perl"))
5430 (all
5431 ;;(error "Not implemented: all")
5432 (setq args (append (list "-e"
5433 "push @ARGV, <*.PL *.pl *.pm>;
5434 exec @ARGV;"
5435 cmd) args)
5436 cmd "perl"))
5437 (t
5438 (setq args (append args files))))
5439 (setq res (apply 'call-process cmd nil nil nil args))
5440 (or (eq res 0)
5441 (message "etags returned \"%s\"" res))))
5442
5443 (defun cperl-toggle-auto-newline ()
5444 "Toggle the state of `cperl-auto-newline'."
5445 (interactive)
5446 (setq cperl-auto-newline (not cperl-auto-newline))
5447 (message "Newlines will %sbe auto-inserted now."
5448 (if cperl-auto-newline "" "not ")))
5449
5450 (defun cperl-toggle-abbrev ()
5451 "Toggle the state of automatic keyword expansion in CPerl mode."
5452 (interactive)
5453 (abbrev-mode (if abbrev-mode 0 1))
5454 (message "Perl control structure will %sbe auto-inserted now."
5455 (if abbrev-mode "" "not ")))
5456
5457
5458 (defun cperl-toggle-electric ()
5459 "Toggle the state of parentheses doubling in CPerl mode."
5460 (interactive)
5461 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
5462 (message "Parentheses will %sbe auto-doubled now."
5463 (if (cperl-val 'cperl-electric-parens) "" "not ")))
5464
5465 (defun cperl-toggle-autohelp ()
5466 "Toggle the state of automatic help message in CPerl mode.
5467 See `cperl-lazy-help-time' too."
5468 (interactive)
5469 (if (fboundp 'run-with-idle-timer)
5470 (progn
5471 (if cperl-lazy-installed
5472 (eval '(cperl-lazy-unstall))
5473 (cperl-lazy-install))
5474 (message "Perl help messages will %sbe automatically shown now."
5475 (if cperl-lazy-installed "" "not ")))
5476 (message "Cannot automatically show Perl help messages - run-with-idle-timer missing.")))
5477
5478 (defun cperl-toggle-construct-fix ()
5479 "Toggle whether `indent-region'/`indent-sexp' fix whitespace too."
5480 (interactive)
5481 (setq cperl-indent-region-fix-constructs
5482 (if cperl-indent-region-fix-constructs
5483 nil
5484 1))
5485 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
5486 (if cperl-indent-region-fix-constructs "" "not ")))
5487
5488 ;;;; Tags file creation.
5489
5490 (defvar cperl-tmp-buffer " *cperl-tmp*")
5491
5492 (defun cperl-setup-tmp-buf ()
5493 (set-buffer (get-buffer-create cperl-tmp-buffer))
5494 (set-syntax-table cperl-mode-syntax-table)
5495 (buffer-disable-undo)
5496 (auto-fill-mode 0)
5497 (if cperl-use-syntax-table-text-property-for-tags
5498 (progn
5499 (make-local-variable 'parse-sexp-lookup-properties)
5500 ;; Do not introduce variable if not needed, we check it!
5501 (set 'parse-sexp-lookup-properties t))))
5502
5503 (defun cperl-xsub-scan ()
5504 (require 'imenu)
5505 (let ((index-alist '())
5506 (prev-pos 0) index index1 name package prefix)
5507 (goto-char (point-min))
5508 (if noninteractive
5509 (message "Scanning XSUB for index")
5510 (imenu-progress-message prev-pos 0))
5511 ;; Search for the function
5512 (progn ;;save-match-data
5513 (while (re-search-forward
5514 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
5515 nil t)
5516 (or noninteractive
5517 (imenu-progress-message prev-pos))
5518 (cond
5519 ((match-beginning 2) ; SECTION
5520 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
5521 (goto-char (match-beginning 0))
5522 (skip-chars-forward " \t")
5523 (forward-char 1)
5524 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
5525 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
5526 (setq prefix nil)))
5527 ((not package) nil) ; C language section
5528 ((match-beginning 3) ; XSUB
5529 (goto-char (1+ (match-beginning 3)))
5530 (setq index (imenu-example--name-and-position))
5531 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
5532 (if (and prefix (string-match (concat "^" prefix) name))
5533 (setq name (substring name (length prefix))))
5534 (cond ((string-match "::" name) nil)
5535 (t
5536 (setq index1 (cons (concat package "::" name) (cdr index)))
5537 (push index1 index-alist)))
5538 (setcar index name)
5539 (push index index-alist))
5540 (t ; BOOT: section
5541 ;; (beginning-of-line)
5542 (setq index (imenu-example--name-and-position))
5543 (setcar index (concat package "::BOOT:"))
5544 (push index index-alist)))))
5545 (or noninteractive
5546 (imenu-progress-message prev-pos 100))
5547 index-alist))
5548
5549 (defvar cperl-unreadable-ok nil)
5550
5551 (defun cperl-find-tags (ifile xs topdir)
5552 (let (ind (b (get-buffer cperl-tmp-buffer)) lst elt pos ret rel
5553 (cperl-pod-here-fontify nil) f file)
5554 (save-excursion
5555 (if b (set-buffer b)
5556 (cperl-setup-tmp-buf))
5557 (erase-buffer)
5558 (condition-case err
5559 (setq file (car (insert-file-contents ifile)))
5560 (error (if cperl-unreadable-ok nil
5561 (if (y-or-n-p
5562 (format "File %s unreadable. Continue? " ifile))
5563 (setq cperl-unreadable-ok t)
5564 (error "Aborting: unreadable file %s" ifile)))))
5565 (if (not file)
5566 (message "Unreadable file %s" ifile)
5567 (message "Scanning file %s ..." file)
5568 (if (and cperl-use-syntax-table-text-property-for-tags
5569 (not xs))
5570 (condition-case err ; after __END__ may have garbage
5571 (cperl-find-pods-heres nil nil noninteractive)
5572 (error (message "While scanning for syntax: %s" err))))
5573 (if xs
5574 (setq lst (cperl-xsub-scan))
5575 (setq ind (cperl-imenu--create-perl-index))
5576 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
5577 (setq lst
5578 (mapcar
5579 (function
5580 (lambda (elt)
5581 (cond ((string-match "^[_a-zA-Z]" (car elt))
5582 (goto-char (cdr elt))
5583 (beginning-of-line) ; pos should be of the start of the line
5584 (list (car elt)
5585 (point)
5586 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
5587 (buffer-substring (progn
5588 (goto-char (cdr elt))
5589 ;; After name now...
5590 (or (eolp) (forward-char 1))
5591 (point))
5592 (progn
5593 (beginning-of-line)
5594 (point))))))))
5595 lst))
5596 (erase-buffer)
5597 (while lst
5598 (setq elt (car lst) lst (cdr lst))
5599 (if elt
5600 (progn
5601 (insert (elt elt 3)
5602 127
5603 (if (string-match "^package " (car elt))
5604 (substring (car elt) 8)
5605 (car elt) )
5606 1
5607 (number-to-string (elt elt 2)) ; Line
5608 ","
5609 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
5610 "\n")
5611 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
5612 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
5613 (elt elt 3)))
5614 ;; Need to insert the name without package as well
5615 (setq lst (cons (cons (substring (elt elt 3)
5616 (match-beginning 1)
5617 (match-end 1))
5618 (cdr elt))
5619 lst))))))
5620 (setq pos (point))
5621 (goto-char 1)
5622 (setq rel file)
5623 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
5624 (set-text-properties 0 (length rel) nil rel)
5625 (and (equal topdir (substring rel 0 (length topdir)))
5626 (setq rel (substring file (length topdir))))
5627 (insert "\f\n" rel "," (number-to-string (1- pos)) "\n")
5628 (setq ret (buffer-substring 1 (point-max)))
5629 (erase-buffer)
5630 (or noninteractive
5631 (message "Scanning file %s finished" file))
5632 ret))))
5633
5634 (defun cperl-add-tags-recurse-noxs ()
5635 "Add to TAGS data for Perl and XSUB files in the current directory and kids.
5636 Use as
5637 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
5638 -f cperl-add-tags-recurse
5639 "
5640 (cperl-write-tags nil nil t t nil t))
5641
5642 (defun cperl-add-tags-recurse ()
5643 "Add to TAGS file data for Perl files in the current directory and kids.
5644 Use as
5645 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
5646 -f cperl-add-tags-recurse
5647 "
5648 (cperl-write-tags nil nil t t))
5649
5650 (defun cperl-write-tags (&optional file erase recurse dir inbuffer noxs topdir)
5651 ;; If INBUFFER, do not select buffer, and do not save
5652 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
5653 (require 'etags)
5654 (if file nil
5655 (setq file (if dir default-directory (buffer-file-name)))
5656 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
5657 (or topdir
5658 (setq topdir default-directory))
5659 (let ((tags-file-name "TAGS")
5660 (case-fold-search (eq system-type 'emx))
5661 xs rel tm)
5662 (save-excursion
5663 (cond (inbuffer nil) ; Already there
5664 ((file-exists-p tags-file-name)
5665 (if cperl-xemacs-p
5666 (visit-tags-table-buffer)
5667 (visit-tags-table-buffer tags-file-name)))
5668 (t (set-buffer (find-file-noselect tags-file-name))))
5669 (cond
5670 (dir
5671 (cond ((eq erase 'ignore))
5672 (erase
5673 (erase-buffer)
5674 (setq erase 'ignore)))
5675 (let ((files
5676 (condition-case err
5677 (directory-files file t
5678 (if recurse nil cperl-scan-files-regexp)
5679 t)
5680 (error
5681 (if cperl-unreadable-ok nil
5682 (if (y-or-n-p
5683 (format "Directory %s unreadable. Continue? " file))
5684 (setq cperl-unreadable-ok t
5685 tm nil) ; Return empty list
5686 (error "Aborting: unreadable directory %s" file)))))))
5687 (mapcar (function (lambda (file)
5688 (cond
5689 ((string-match cperl-noscan-files-regexp file)
5690 nil)
5691 ((not (file-directory-p file))
5692 (if (string-match cperl-scan-files-regexp file)
5693 (cperl-write-tags file erase recurse nil t noxs topdir)))
5694 ((not recurse) nil)
5695 (t (cperl-write-tags file erase recurse t t noxs topdir)))))
5696 files))
5697 )
5698 (t
5699 (setq xs (string-match "\\.xs$" file))
5700 (if (not (and xs noxs))
5701 (progn
5702 (cond ((eq erase 'ignore) (goto-char (point-max)))
5703 (erase (erase-buffer))
5704 (t
5705 (goto-char 1)
5706 (setq rel file)
5707 ;; On case-preserving filesystems (EMX on OS/2) case might be encoded in properties
5708 (set-text-properties 0 (length rel) nil rel)
5709 (and (equal topdir (substring rel 0 (length topdir)))
5710 (setq rel (substring file (length topdir))))
5711 (if (search-forward (concat "\f\n" rel ",") nil t)
5712 (progn
5713 (search-backward "\f\n")
5714 (delete-region (point)
5715 (save-excursion
5716 (forward-char 1)
5717 (if (search-forward "\f\n"
5718 nil 'toend)
5719 (- (point) 2)
5720 (point-max)))))
5721 (goto-char (point-max)))))
5722 (insert (cperl-find-tags file xs topdir))))))
5723 (if inbuffer nil ; Delegate to the caller
5724 (save-buffer 0) ; No backup
5725 (if (fboundp 'initialize-new-tags-table) ; Do we need something special in XEmacs?
5726 (initialize-new-tags-table))))))
5727
5728 (defvar cperl-tags-hier-regexp-list
5729 (concat
5730 "^\\("
5731 "\\(package\\)\\>"
5732 "\\|"
5733 "sub\\>[^\n]+::"
5734 "\\|"
5735 "[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::" ; XSUB?
5736 "\\|"
5737 "[ \t]*BOOT:\C-?[^\n]+::" ; BOOT section
5738 "\\)"))
5739
5740 (defvar cperl-hierarchy '(() ())
5741 "Global hierarchy of classes")
5742
5743 (defun cperl-tags-hier-fill ()
5744 ;; Suppose we are in a tag table cooked by cperl.
5745 (goto-char 1)
5746 (let (type pack name pos line chunk ord cons1 file str info fileind)
5747 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
5748 (setq pos (match-beginning 0)
5749 pack (match-beginning 2))
5750 (beginning-of-line)
5751 (if (looking-at (concat
5752 "\\([^\n]+\\)"
5753 "\C-?"
5754 "\\([^\n]+\\)"
5755 "\C-a"
5756 "\\([0-9]+\\)"
5757 ","
5758 "\\([0-9]+\\)"))
5759 (progn
5760 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
5761 name (buffer-substring (match-beginning 2) (match-end 2))
5762 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
5763 line (buffer-substring (match-beginning 3) (match-end 3))
5764 ord (if pack 1 0)
5765 file (file-of-tag)
5766 fileind (format "%s:%s" file line)
5767 ;; Moves to beginning of the next line:
5768 info (cperl-etags-snarf-tag file line))
5769 ;; Move back
5770 (forward-char -1)
5771 ;; Make new member of hierarchy name ==> file ==> pos if needed
5772 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
5773 ;; Name known
5774 (setcdr cons1 (cons (cons fileind (vector file info))
5775 (cdr cons1)))
5776 ;; First occurrence of the name, start alist
5777 (setq cons1 (cons name (list (cons fileind (vector file info)))))
5778 (if pack
5779 (setcar (cdr cperl-hierarchy)
5780 (cons cons1 (nth 1 cperl-hierarchy)))
5781 (setcar cperl-hierarchy
5782 (cons cons1 (car cperl-hierarchy)))))))
5783 (end-of-line))))
5784
5785 (defun cperl-tags-hier-init (&optional update)
5786 "Show hierarchical menu of classes and methods.
5787 Finds info about classes by a scan of loaded TAGS files.
5788 Supposes that the TAGS files contain fully qualified function names.
5789 One may build such TAGS files from CPerl mode menu."
5790 (interactive)
5791 (require 'etags)
5792 (require 'imenu)
5793 (if (or update (null (nth 2 cperl-hierarchy)))
5794 (let (pack name cons1 to l1 l2 l3 l4 b
5795 (remover (function (lambda (elt) ; (name (file1...) (file2..))
5796 (or (nthcdr 2 elt)
5797 ;; Only in one file
5798 (setcdr elt (cdr (nth 1 elt))))))))
5799 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
5800 (setq cperl-hierarchy (list l1 l2 l3))
5801 (if cperl-xemacs-p ; Not checked
5802 (progn
5803 (or tags-file-name
5804 ;; Does this work in XEmacs?
5805 (call-interactively 'visit-tags-table))
5806 (message "Updating list of classes...")
5807 (set-buffer (get-file-buffer tags-file-name))
5808 (cperl-tags-hier-fill))
5809 (or tags-table-list
5810 (call-interactively 'visit-tags-table))
5811 (mapcar
5812 (function
5813 (lambda (tagsfile)
5814 (message "Updating list of classes... %s" tagsfile)
5815 (set-buffer (get-file-buffer tagsfile))
5816 (cperl-tags-hier-fill)))
5817 tags-table-list)
5818 (message "Updating list of classes... postprocessing..."))
5819 (mapcar remover (car cperl-hierarchy))
5820 (mapcar remover (nth 1 cperl-hierarchy))
5821 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
5822 (cons "Methods: " (car cperl-hierarchy))))
5823 (cperl-tags-treeify to 1)
5824 (setcar (nthcdr 2 cperl-hierarchy)
5825 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
5826 (message "Updating list of classes: done, requesting display...")
5827 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
5828 ))
5829 (or (nth 2 cperl-hierarchy)
5830 (error "No items found"))
5831 (setq update
5832 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
5833 (if window-system
5834 (x-popup-menu t (nth 2 cperl-hierarchy))
5835 (require 'tmm)
5836 (tmm-prompt (nth 2 cperl-hierarchy))))
5837 (if (and update (listp update))
5838 (progn (while (cdr update) (setq update (cdr update)))
5839 (setq update (car update)))) ; Get the last from the list
5840 (if (vectorp update)
5841 (progn
5842 (find-file (elt update 0))
5843 (cperl-etags-goto-tag-location (elt update 1))))
5844 (if (eq update -999) (cperl-tags-hier-init t)))
5845
5846 (defun cperl-tags-treeify (to level)
5847 ;; cadr of `to' is read-write. On start it is a cons
5848 (let* ((regexp (concat "^\\(" (mapconcat
5849 'identity
5850 (make-list level "[_a-zA-Z0-9]+")
5851 "::")
5852 "\\)\\(::\\)?"))
5853 (packages (cdr (nth 1 to)))
5854 (methods (cdr (nth 2 to)))
5855 l1 head tail cons1 cons2 ord writeto packs recurse
5856 root-packages root-functions ms many_ms same_name ps
5857 (move-deeper
5858 (function
5859 (lambda (elt)
5860 (cond ((and (string-match regexp (car elt))
5861 (or (eq ord 1) (match-end 2)))
5862 (setq head (substring (car elt) 0 (match-end 1))
5863 tail (if (match-end 2) (substring (car elt)
5864 (match-end 2)))
5865 recurse t)
5866 (if (setq cons1 (assoc head writeto)) nil
5867 ;; Need to init new head
5868 (setcdr writeto (cons (list head (list "Packages: ")
5869 (list "Methods: "))
5870 (cdr writeto)))
5871 (setq cons1 (nth 1 writeto)))
5872 (setq cons2 (nth ord cons1)) ; Either packs or meths
5873 (setcdr cons2 (cons elt (cdr cons2))))
5874 ((eq ord 2)
5875 (setq root-functions (cons elt root-functions)))
5876 (t
5877 (setq root-packages (cons elt root-packages))))))))
5878 (setcdr to l1) ; Init to dynamic space
5879 (setq writeto to)
5880 (setq ord 1)
5881 (mapcar move-deeper packages)
5882 (setq ord 2)
5883 (mapcar move-deeper methods)
5884 (if recurse
5885 (mapcar (function (lambda (elt)
5886 (cperl-tags-treeify elt (1+ level))))
5887 (cdr to)))
5888 ;;Now clean up leaders with one child only
5889 (mapcar (function (lambda (elt)
5890 (if (not (and (listp (cdr elt))
5891 (eq (length elt) 2))) nil
5892 (setcar elt (car (nth 1 elt)))
5893 (setcdr elt (cdr (nth 1 elt))))))
5894 (cdr to))
5895 ;; Sort the roots of subtrees
5896 (if (default-value 'imenu-sort-function)
5897 (setcdr to
5898 (sort (cdr to) (default-value 'imenu-sort-function))))
5899 ;; Now add back functions removed from display
5900 (mapcar (function (lambda (elt)
5901 (setcdr to (cons elt (cdr to)))))
5902 (if (default-value 'imenu-sort-function)
5903 (nreverse
5904 (sort root-functions (default-value 'imenu-sort-function)))
5905 root-functions))
5906 ;; Now add back packages removed from display
5907 (mapcar (function (lambda (elt)
5908 (setcdr to (cons (cons (concat "package " (car elt))
5909 (cdr elt))
5910 (cdr to)))))
5911 (if (default-value 'imenu-sort-function)
5912 (nreverse
5913 (sort root-packages (default-value 'imenu-sort-function)))
5914 root-packages))
5915 ))
5916
5917 ;;;(x-popup-menu t
5918 ;;; '(keymap "Name1"
5919 ;;; ("Ret1" "aa")
5920 ;;; ("Head1" "ab"
5921 ;;; keymap "Name2"
5922 ;;; ("Tail1" "x") ("Tail2" "y"))))
5923
5924 (defun cperl-list-fold (list name limit)
5925 (let (list1 list2 elt1 (num 0))
5926 (if (<= (length list) limit) list
5927 (setq list1 nil list2 nil)
5928 (while list
5929 (setq num (1+ num)
5930 elt1 (car list)
5931 list (cdr list))
5932 (if (<= num imenu-max-items)
5933 (setq list2 (cons elt1 list2))
5934 (setq list1 (cons (cons name
5935 (nreverse list2))
5936 list1)
5937 list2 (list elt1)
5938 num 1)))
5939 (nreverse (cons (cons name
5940 (nreverse list2))
5941 list1)))))
5942
5943 (defun cperl-menu-to-keymap (menu &optional name)
5944 (let (list)
5945 (cons 'keymap
5946 (mapcar
5947 (function
5948 (lambda (elt)
5949 (cond ((listp (cdr elt))
5950 (setq list (cperl-list-fold
5951 (cdr elt) (car elt) imenu-max-items))
5952 (cons nil
5953 (cons (car elt)
5954 (cperl-menu-to-keymap list))))
5955 (t
5956 (list (cdr elt) (car elt) t))))) ; t is needed in 19.34
5957 (cperl-list-fold menu "Root" imenu-max-items)))))
5958
5959 \f
5960 (defvar cperl-bad-style-regexp
5961 (mapconcat 'identity
5962 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
5963 "[-<>=+^&|]+[^- \t\n=+<>~]" ; sign+ char
5964 )
5965 "\\|")
5966 "Finds places such that insertion of a whitespace may help a lot.")
5967
5968 (defvar cperl-not-bad-style-regexp
5969 (mapconcat 'identity
5970 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
5971 "[a-zA-Z0-9_][|&][a-zA-Z0-9_$]" ; abc|def abc&def are often used.
5972 "&[(a-zA-Z0-9_$]" ; &subroutine &(var->field)
5973 "<\\$?\\sw+\\(\\.\\sw+\\)?>" ; <IN> <stdin.h>
5974 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
5975 "-[0-9]" ; -5
5976 "\\+\\+" ; ++var
5977 "--" ; --var
5978 ".->" ; a->b
5979 "->" ; a SPACE ->b
5980 "\\[-" ; a[-1]
5981 "\\\\[&$@*\\\\]" ; \&func
5982 "^=" ; =head
5983 "\\$." ; $|
5984 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
5985 "||"
5986 "&&"
5987 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
5988 "-[a-zA-Z_0-9]+[ \t]*=>" ; -option => value
5989 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
5990 ;;"[*/+-|&<.]+="
5991 )
5992 "\\|")
5993 "If matches at the start of match found by `my-bad-c-style-regexp',
5994 insertion of a whitespace will not help.")
5995
5996 (defvar found-bad)
5997
5998 (defun cperl-find-bad-style ()
5999 "Find places in the buffer where insertion of a whitespace may help.
6000 Prompts user for insertion of spaces.
6001 Currently it is tuned to C and Perl syntax."
6002 (interactive)
6003 (let (found-bad (p (point)))
6004 (setq last-nonmenu-event 13) ; To disable popup
6005 (beginning-of-buffer)
6006 (map-y-or-n-p "Insert space here? "
6007 (function (lambda (arg) (insert " ")))
6008 'cperl-next-bad-style
6009 '("location" "locations" "insert a space into")
6010 '((?\C-r (lambda (arg)
6011 (let ((buffer-quit-function
6012 'exit-recursive-edit))
6013 (message "Exit with Esc Esc")
6014 (recursive-edit)
6015 t)) ; Consider acted upon
6016 "edit, exit with Esc Esc")
6017 (?e (lambda (arg)
6018 (let ((buffer-quit-function
6019 'exit-recursive-edit))
6020 (message "Exit with Esc Esc")
6021 (recursive-edit)
6022 t)) ; Consider acted upon
6023 "edit, exit with Esc Esc"))
6024 t)
6025 (if found-bad (goto-char found-bad)
6026 (goto-char p)
6027 (message "No appropriate place found"))))
6028
6029 (defun cperl-next-bad-style ()
6030 (let (p (not-found t) (point (point)) found)
6031 (while (and not-found
6032 (re-search-forward cperl-bad-style-regexp nil 'to-end))
6033 (setq p (point))
6034 (goto-char (match-beginning 0))
6035 (if (or
6036 (looking-at cperl-not-bad-style-regexp)
6037 ;; Check for a < -b and friends
6038 (and (eq (following-char) ?\-)
6039 (save-excursion
6040 (skip-chars-backward " \t\n")
6041 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\(, ?\[, ?\{))))
6042 ;; Now check for syntax type
6043 (save-match-data
6044 (setq found (point))
6045 (beginning-of-defun)
6046 (let ((pps (parse-partial-sexp (point) found)))
6047 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
6048 (goto-char (match-end 0))
6049 (goto-char (1- p))
6050 (setq not-found nil
6051 found-bad found)))
6052 (not not-found)))
6053
6054 \f
6055 ;;; Getting help
6056 (defvar cperl-have-help-regexp
6057 ;;(concat "\\("
6058 (mapconcat
6059 'identity
6060 '("[$@%*&][0-9a-zA-Z_:]+\\([ \t]*[[{]\\)?" ; Usual variable
6061 "[$@]\\^[a-zA-Z]" ; Special variable
6062 "[$@][^ \n\t]" ; Special variable
6063 "-[a-zA-Z]" ; File test
6064 "\\\\[a-zA-Z0]" ; Special chars
6065 "^=[a-z][a-zA-Z0-9_]*" ; Pod sections
6066 "[-!&*+,-./<=>?\\\\^|~]+" ; Operator
6067 "[a-zA-Z_0-9:]+" ; symbol or number
6068 "x="
6069 "#!"
6070 )
6071 ;;"\\)\\|\\("
6072 "\\|"
6073 )
6074 ;;"\\)"
6075 ;;)
6076 "Matches places in the buffer we can find help for.")
6077
6078 (defvar cperl-message-on-help-error t)
6079 (defvar cperl-help-from-timer nil)
6080
6081 (defun cperl-word-at-point-hard ()
6082 ;; Does not save-excursion
6083 ;; Get to the something meaningful
6084 (or (eobp) (eolp) (forward-char 1))
6085 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
6086 (save-excursion (beginning-of-line) (point))
6087 'to-beg)
6088 ;; (cond
6089 ;; ((or (eobp) (looking-at "[][ \t\n{}();,]")) ; Not at a symbol
6090 ;; (skip-chars-backward " \n\t\r({[]});,")
6091 ;; (or (bobp) (backward-char 1))))
6092 ;; Try to backtrace
6093 (cond
6094 ((looking-at "[a-zA-Z0-9_:]") ; symbol
6095 (skip-chars-backward "a-zA-Z0-9_:")
6096 (cond
6097 ((and (eq (preceding-char) ?^) ; $^I
6098 (eq (char-after (- (point) 2)) ?\$))
6099 (forward-char -2))
6100 ((memq (preceding-char) (append "*$@%&\\" nil)) ; *glob
6101 (forward-char -1))
6102 ((and (eq (preceding-char) ?\=)
6103 (eq (current-column) 1))
6104 (forward-char -1))) ; =head1
6105 (if (and (eq (preceding-char) ?\<)
6106 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <FH>
6107 (forward-char -1)))
6108 ((and (looking-at "=") (eq (preceding-char) ?x)) ; x=
6109 (forward-char -1))
6110 ((and (looking-at "\\^") (eq (preceding-char) ?\$)) ; $^I
6111 (forward-char -1))
6112 ((looking-at "[-!&*+,-./<=>?\\\\^|~]")
6113 (skip-chars-backward "-!&*+,-./<=>?\\\\^|~")
6114 (cond
6115 ((and (eq (preceding-char) ?\$)
6116 (not (eq (char-after (- (point) 2)) ?\$))) ; $-
6117 (forward-char -1))
6118 ((and (eq (following-char) ?\>)
6119 (string-match "[a-zA-Z0-9_]" (char-to-string (preceding-char)))
6120 (save-excursion
6121 (forward-sexp -1)
6122 (and (eq (preceding-char) ?\<)
6123 (looking-at "\\$?[a-zA-Z0-9_:]+>")))) ; <FH>
6124 (search-backward "<"))))
6125 ((and (eq (following-char) ?\$)
6126 (eq (preceding-char) ?\<)
6127 (looking-at "\\$?[a-zA-Z0-9_:]+>")) ; <$fh>
6128 (forward-char -1)))
6129 (if (looking-at cperl-have-help-regexp)
6130 (buffer-substring (match-beginning 0) (match-end 0))))
6131
6132 (defun cperl-get-help ()
6133 "Get one-line docs on the symbol at the point.
6134 The data for these docs is a little bit obsolete and may be in fact longer
6135 than a line. Your contribution to update/shorten it is appreciated."
6136 (interactive)
6137 (save-match-data ; May be called "inside" query-replace
6138 (save-excursion
6139 (let ((word (cperl-word-at-point-hard)))
6140 (if word
6141 (if (and cperl-help-from-timer ; Bail out if not in mainland
6142 (not (string-match "^#!\\|\\\\\\|^=" word)) ; Show help even in comments/strings.
6143 (or (memq (get-text-property (point) 'face)
6144 '(font-lock-comment-face font-lock-string-face))
6145 (memq (get-text-property (point) 'syntax-type)
6146 '(pod here-doc format))))
6147 nil
6148 (cperl-describe-perl-symbol word))
6149 (if cperl-message-on-help-error
6150 (message "Nothing found for %s..."
6151 (buffer-substring (point) (min (+ 5 (point)) (point-max))))))))))
6152
6153 ;;; Stolen from perl-descr.el by Johan Vromans:
6154
6155 (defvar cperl-doc-buffer " *perl-doc*"
6156 "Where the documentation can be found.")
6157
6158 (defun cperl-describe-perl-symbol (val)
6159 "Display the documentation of symbol at point, a Perl operator."
6160 (let ((enable-recursive-minibuffers t)
6161 args-file regexp)
6162 (cond
6163 ((string-match "^[&*][a-zA-Z_]" val)
6164 (setq val (concat (substring val 0 1) "NAME")))
6165 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*\\[" val)
6166 (setq val (concat "@" (substring val 1 (match-end 1)))))
6167 ((string-match "^[$@]\\([a-zA-Z_:0-9]+\\)[ \t]*{" val)
6168 (setq val (concat "%" (substring val 1 (match-end 1)))))
6169 ((and (string= val "x") (string-match "^x=" val))
6170 (setq val "x="))
6171 ((string-match "^\\$[\C-a-\C-z]" val)
6172 (setq val (concat "$^" (char-to-string (+ ?A -1 (aref val 1))))))
6173 ((string-match "^CORE::" val)
6174 (setq val "CORE::"))
6175 ((string-match "^SUPER::" val)
6176 (setq val "SUPER::"))
6177 ((and (string= "<" val) (string-match "^<\\$?[a-zA-Z0-9_:]+>" val))
6178 (setq val "<NAME>")))
6179 (setq regexp (concat "^"
6180 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
6181 (regexp-quote val)
6182 "\\([ \t([/]\\|$\\)"))
6183
6184 ;; get the buffer with the documentation text
6185 (cperl-switch-to-doc-buffer)
6186
6187 ;; lookup in the doc
6188 (goto-char (point-min))
6189 (let ((case-fold-search nil))
6190 (list
6191 (if (re-search-forward regexp (point-max) t)
6192 (save-excursion
6193 (beginning-of-line 1)
6194 (let ((lnstart (point)))
6195 (end-of-line)
6196 (message "%s" (buffer-substring lnstart (point)))))
6197 (if cperl-message-on-help-error
6198 (message "No definition for %s" val)))))))
6199
6200 (defvar cperl-short-docs "Ignore my value"
6201 ;; Perl4 version was written by Johan Vromans (jvromans@squirrel.nl)
6202 "# based on '@(#)@ perl-descr.el 1.9 - describe-perl-symbol' [Perl 5]
6203 ! ... Logical negation.
6204 ... != ... Numeric inequality.
6205 ... !~ ... Search pattern, substitution, or translation (negated).
6206 $! In numeric context: errno. In a string context: error string.
6207 $\" The separator which joins elements of arrays interpolated in strings.
6208 $# The output format for printed numbers. Initial value is %.15g or close.
6209 $$ Process number of this script. Changes in the fork()ed child process.
6210 $% The current page number of the currently selected output channel.
6211
6212 The following variables are always local to the current block:
6213
6214 $1 Match of the 1st set of parentheses in the last match (auto-local).
6215 $2 Match of the 2nd set of parentheses in the last match (auto-local).
6216 $3 Match of the 3rd set of parentheses in the last match (auto-local).
6217 $4 Match of the 4th set of parentheses in the last match (auto-local).
6218 $5 Match of the 5th set of parentheses in the last match (auto-local).
6219 $6 Match of the 6th set of parentheses in the last match (auto-local).
6220 $7 Match of the 7th set of parentheses in the last match (auto-local).
6221 $8 Match of the 8th set of parentheses in the last match (auto-local).
6222 $9 Match of the 9th set of parentheses in the last match (auto-local).
6223 $& The string matched by the last pattern match (auto-local).
6224 $' The string after what was matched by the last match (auto-local).
6225 $` The string before what was matched by the last match (auto-local).
6226
6227 $( The real gid of this process.
6228 $) The effective gid of this process.
6229 $* Deprecated: Set to 1 to do multiline matching within a string.
6230 $+ The last bracket matched by the last search pattern.
6231 $, The output field separator for the print operator.
6232 $- The number of lines left on the page.
6233 $. The current input line number of the last filehandle that was read.
6234 $/ The input record separator, newline by default.
6235 $0 Name of the file containing the perl script being executed. May be set.
6236 $: String may be broken after these characters to fill ^-lines in a format.
6237 $; Subscript separator for multi-dim array emulation. Default \"\\034\".
6238 $< The real uid of this process.
6239 $= The page length of the current output channel. Default is 60 lines.
6240 $> The effective uid of this process.
6241 $? The status returned by the last ``, pipe close or `system'.
6242 $@ The perl error message from the last eval or do @var{EXPR} command.
6243 $ARGV The name of the current file used with <> .
6244 $[ Deprecated: The index of the first element/char in an array/string.
6245 $\\ The output record separator for the print operator.
6246 $] The perl version string as displayed with perl -v.
6247 $^ The name of the current top-of-page format.
6248 $^A The current value of the write() accumulator for format() lines.
6249 $^D The value of the perl debug (-D) flags.
6250 $^E Information about the last system error other than that provided by $!.
6251 $^F The highest system file descriptor, ordinarily 2.
6252 $^H The current set of syntax checks enabled by `use strict'.
6253 $^I The value of the in-place edit extension (perl -i option).
6254 $^L What formats output to perform a formfeed. Default is \f.
6255 $^M A buffer for emergency memory allocation when running out of memory.
6256 $^O The operating system name under which this copy of Perl was built.
6257 $^P Internal debugging flag.
6258 $^T The time the script was started. Used by -A/-M/-C file tests.
6259 $^W True if warnings are requested (perl -w flag).
6260 $^X The name under which perl was invoked (argv[0] in C-speech).
6261 $_ The default input and pattern-searching space.
6262 $| Auto-flush after write/print on current output channel? Default 0.
6263 $~ The name of the current report format.
6264 ... % ... Modulo division.
6265 ... %= ... Modulo division assignment.
6266 %ENV Contains the current environment.
6267 %INC List of files that have been require-d or do-ne.
6268 %SIG Used to set signal handlers for various signals.
6269 ... & ... Bitwise and.
6270 ... && ... Logical and.
6271 ... &&= ... Logical and assignment.
6272 ... &= ... Bitwise and assignment.
6273 ... * ... Multiplication.
6274 ... ** ... Exponentiation.
6275 *NAME Glob: all objects refered by NAME. *NAM1 = *NAM2 aliases NAM1 to NAM2.
6276 &NAME(arg0, ...) Subroutine call. Arguments go to @_.
6277 ... + ... Addition. +EXPR Makes EXPR into scalar context.
6278 ++ Auto-increment (magical on strings). ++EXPR EXPR++
6279 ... += ... Addition assignment.
6280 , Comma operator.
6281 ... - ... Subtraction.
6282 -- Auto-decrement (NOT magical on strings). --EXPR EXPR--
6283 ... -= ... Subtraction assignment.
6284 -A Access time in days since script started.
6285 -B File is a non-text (binary) file.
6286 -C Inode change time in days since script started.
6287 -M Age in days since script started.
6288 -O File is owned by real uid.
6289 -R File is readable by real uid.
6290 -S File is a socket .
6291 -T File is a text file.
6292 -W File is writable by real uid.
6293 -X File is executable by real uid.
6294 -b File is a block special file.
6295 -c File is a character special file.
6296 -d File is a directory.
6297 -e File exists .
6298 -f File is a plain file.
6299 -g File has setgid bit set.
6300 -k File has sticky bit set.
6301 -l File is a symbolic link.
6302 -o File is owned by effective uid.
6303 -p File is a named pipe (FIFO).
6304 -r File is readable by effective uid.
6305 -s File has non-zero size.
6306 -t Tests if filehandle (STDIN by default) is opened to a tty.
6307 -u File has setuid bit set.
6308 -w File is writable by effective uid.
6309 -x File is executable by effective uid.
6310 -z File has zero size.
6311 . Concatenate strings.
6312 .. Alternation, also range operator.
6313 .= Concatenate assignment strings
6314 ... / ... Division. /PATTERN/ioxsmg Pattern match
6315 ... /= ... Division assignment.
6316 /PATTERN/ioxsmg Pattern match.
6317 ... < ... Numeric less than. <pattern> Glob. See <NAME>, <> as well.
6318 <NAME> Reads line from filehandle NAME (a bareword or dollar-bareword).
6319 <pattern> Glob (Unless pattern is bareword/dollar-bareword - see <NAME>).
6320 <> Reads line from union of files in @ARGV (= command line) and STDIN.
6321 ... << ... Bitwise shift left. << start of HERE-DOCUMENT.
6322 ... <= ... Numeric less than or equal to.
6323 ... <=> ... Numeric compare.
6324 ... = ... Assignment.
6325 ... == ... Numeric equality.
6326 ... =~ ... Search pattern, substitution, or translation
6327 ... > ... Numeric greater than.
6328 ... >= ... Numeric greater than or equal to.
6329 ... >> ... Bitwise shift right.
6330 ... >>= ... Bitwise shift right assignment.
6331 ... ? ... : ... Condition=if-then-else operator. ?PAT? One-time pattern match.
6332 ?PATTERN? One-time pattern match.
6333 @ARGV Command line arguments (not including the command name - see $0).
6334 @INC List of places to look for perl scripts during do/include/use.
6335 @_ Parameter array for subroutines. Also used by split unless in array context.
6336 \\ Creates reference to what follows, like \$var, or quotes non-\w in strings.
6337 \\0 Octal char, e.g. \\033.
6338 \\E Case modification terminator. See \\Q, \\L, and \\U.
6339 \\L Lowercase until \\E . See also \l, lc.
6340 \\U Upcase until \\E . See also \u, uc.
6341 \\Q Quote metacharacters until \\E . See also quotemeta.
6342 \\a Alarm character (octal 007).
6343 \\b Backspace character (octal 010).
6344 \\c Control character, e.g. \\c[ .
6345 \\e Escape character (octal 033).
6346 \\f Formfeed character (octal 014).
6347 \\l Lowercase the next character. See also \\L and \\u, lcfirst.
6348 \\n Newline character (octal 012 on most systems).
6349 \\r Return character (octal 015 on most systems).
6350 \\t Tab character (octal 011).
6351 \\u Upcase the next character. See also \\U and \\l, ucfirst.
6352 \\x Hex character, e.g. \\x1b.
6353 ... ^ ... Bitwise exclusive or.
6354 __END__ Ends program source.
6355 __DATA__ Ends program source.
6356 __FILE__ Current (source) filename.
6357 __LINE__ Current line in current source.
6358 __PACKAGE__ Current package.
6359 ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
6360 ARGVOUT Output filehandle with -i flag.
6361 BEGIN { ... } Immediately executed (during compilation) piece of code.
6362 END { ... } Pseudo-subroutine executed after the script finishes.
6363 CHECK { ... } Pseudo-subroutine executed after the script is compiled.
6364 INIT { ... } Pseudo-subroutine executed before the script starts running.
6365 DATA Input filehandle for what follows after __END__ or __DATA__.
6366 accept(NEWSOCKET,GENERICSOCKET)
6367 alarm(SECONDS)
6368 atan2(X,Y)
6369 bind(SOCKET,NAME)
6370 binmode(FILEHANDLE)
6371 caller[(LEVEL)]
6372 chdir(EXPR)
6373 chmod(LIST)
6374 chop[(LIST|VAR)]
6375 chown(LIST)
6376 chroot(FILENAME)
6377 close(FILEHANDLE)
6378 closedir(DIRHANDLE)
6379 ... cmp ... String compare.
6380 connect(SOCKET,NAME)
6381 continue of { block } continue { block }. Is executed after `next' or at end.
6382 cos(EXPR)
6383 crypt(PLAINTEXT,SALT)
6384 dbmclose(%HASH)
6385 dbmopen(%HASH,DBNAME,MODE)
6386 defined(EXPR)
6387 delete($HASH{KEY})
6388 die(LIST)
6389 do { ... }|SUBR while|until EXPR executes at least once
6390 do(EXPR|SUBR([LIST])) (with while|until executes at least once)
6391 dump LABEL
6392 each(%HASH)
6393 endgrent
6394 endhostent
6395 endnetent
6396 endprotoent
6397 endpwent
6398 endservent
6399 eof[([FILEHANDLE])]
6400 ... eq ... String equality.
6401 eval(EXPR) or eval { BLOCK }
6402 exec(LIST)
6403 exit(EXPR)
6404 exp(EXPR)
6405 fcntl(FILEHANDLE,FUNCTION,SCALAR)
6406 fileno(FILEHANDLE)
6407 flock(FILEHANDLE,OPERATION)
6408 for (EXPR;EXPR;EXPR) { ... }
6409 foreach [VAR] (@ARRAY) { ... }
6410 fork
6411 ... ge ... String greater than or equal.
6412 getc[(FILEHANDLE)]
6413 getgrent
6414 getgrgid(GID)
6415 getgrnam(NAME)
6416 gethostbyaddr(ADDR,ADDRTYPE)
6417 gethostbyname(NAME)
6418 gethostent
6419 getlogin
6420 getnetbyaddr(ADDR,ADDRTYPE)
6421 getnetbyname(NAME)
6422 getnetent
6423 getpeername(SOCKET)
6424 getpgrp(PID)
6425 getppid
6426 getpriority(WHICH,WHO)
6427 getprotobyname(NAME)
6428 getprotobynumber(NUMBER)
6429 getprotoent
6430 getpwent
6431 getpwnam(NAME)
6432 getpwuid(UID)
6433 getservbyname(NAME,PROTO)
6434 getservbyport(PORT,PROTO)
6435 getservent
6436 getsockname(SOCKET)
6437 getsockopt(SOCKET,LEVEL,OPTNAME)
6438 gmtime(EXPR)
6439 goto LABEL
6440 ... gt ... String greater than.
6441 hex(EXPR)
6442 if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
6443 index(STR,SUBSTR[,OFFSET])
6444 int(EXPR)
6445 ioctl(FILEHANDLE,FUNCTION,SCALAR)
6446 join(EXPR,LIST)
6447 keys(%HASH)
6448 kill(LIST)
6449 last [LABEL]
6450 ... le ... String less than or equal.
6451 length(EXPR)
6452 link(OLDFILE,NEWFILE)
6453 listen(SOCKET,QUEUESIZE)
6454 local(LIST)
6455 localtime(EXPR)
6456 log(EXPR)
6457 lstat(EXPR|FILEHANDLE|VAR)
6458 ... lt ... String less than.
6459 m/PATTERN/iogsmx
6460 mkdir(FILENAME,MODE)
6461 msgctl(ID,CMD,ARG)
6462 msgget(KEY,FLAGS)
6463 msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
6464 msgsnd(ID,MSG,FLAGS)
6465 my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
6466 our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
6467 ... ne ... String inequality.
6468 next [LABEL]
6469 oct(EXPR)
6470 open(FILEHANDLE[,EXPR])
6471 opendir(DIRHANDLE,EXPR)
6472 ord(EXPR) ASCII value of the first char of the string.
6473 pack(TEMPLATE,LIST)
6474 package NAME Introduces package context.
6475 pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
6476 pop(ARRAY)
6477 print [FILEHANDLE] [(LIST)]
6478 printf [FILEHANDLE] (FORMAT,LIST)
6479 push(ARRAY,LIST)
6480 q/STRING/ Synonym for 'STRING'
6481 qq/STRING/ Synonym for \"STRING\"
6482 qx/STRING/ Synonym for `STRING`
6483 rand[(EXPR)]
6484 read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
6485 readdir(DIRHANDLE)
6486 readlink(EXPR)
6487 recv(SOCKET,SCALAR,LEN,FLAGS)
6488 redo [LABEL]
6489 rename(OLDNAME,NEWNAME)
6490 require [FILENAME | PERL_VERSION]
6491 reset[(EXPR)]
6492 return(LIST)
6493 reverse(LIST)
6494 rewinddir(DIRHANDLE)
6495 rindex(STR,SUBSTR[,OFFSET])
6496 rmdir(FILENAME)
6497 s/PATTERN/REPLACEMENT/gieoxsm
6498 scalar(EXPR)
6499 seek(FILEHANDLE,POSITION,WHENCE)
6500 seekdir(DIRHANDLE,POS)
6501 select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
6502 semctl(ID,SEMNUM,CMD,ARG)
6503 semget(KEY,NSEMS,SIZE,FLAGS)
6504 semop(KEY,...)
6505 send(SOCKET,MSG,FLAGS[,TO])
6506 setgrent
6507 sethostent(STAYOPEN)
6508 setnetent(STAYOPEN)
6509 setpgrp(PID,PGRP)
6510 setpriority(WHICH,WHO,PRIORITY)
6511 setprotoent(STAYOPEN)
6512 setpwent
6513 setservent(STAYOPEN)
6514 setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
6515 shift[(ARRAY)]
6516 shmctl(ID,CMD,ARG)
6517 shmget(KEY,SIZE,FLAGS)
6518 shmread(ID,VAR,POS,SIZE)
6519 shmwrite(ID,STRING,POS,SIZE)
6520 shutdown(SOCKET,HOW)
6521 sin(EXPR)
6522 sleep[(EXPR)]
6523 socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
6524 socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
6525 sort [SUBROUTINE] (LIST)
6526 splice(ARRAY,OFFSET[,LENGTH[,LIST]])
6527 split[(/PATTERN/[,EXPR[,LIMIT]])]
6528 sprintf(FORMAT,LIST)
6529 sqrt(EXPR)
6530 srand(EXPR)
6531 stat(EXPR|FILEHANDLE|VAR)
6532 study[(SCALAR)]
6533 sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
6534 substr(EXPR,OFFSET[,LEN])
6535 symlink(OLDFILE,NEWFILE)
6536 syscall(LIST)
6537 sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
6538 system(LIST)
6539 syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
6540 tell[(FILEHANDLE)]
6541 telldir(DIRHANDLE)
6542 time
6543 times
6544 tr/SEARCHLIST/REPLACEMENTLIST/cds
6545 truncate(FILE|EXPR,LENGTH)
6546 umask[(EXPR)]
6547 undef[(EXPR)]
6548 unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
6549 unlink(LIST)
6550 unpack(TEMPLATE,EXPR)
6551 unshift(ARRAY,LIST)
6552 until (EXPR) { ... } EXPR until EXPR
6553 utime(LIST)
6554 values(%HASH)
6555 vec(EXPR,OFFSET,BITS)
6556 wait
6557 waitpid(PID,FLAGS)
6558 wantarray Returns true if the sub/eval is called in list context.
6559 warn(LIST)
6560 while (EXPR) { ... } EXPR while EXPR
6561 write[(EXPR|FILEHANDLE)]
6562 ... x ... Repeat string or array.
6563 x= ... Repetition assignment.
6564 y/SEARCHLIST/REPLACEMENTLIST/
6565 ... | ... Bitwise or.
6566 ... || ... Logical or.
6567 ~ ... Unary bitwise complement.
6568 #! OS interpreter indicator. If contains `perl', used for options, and -x.
6569 AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
6570 CORE:: Prefix to access builtin function if imported sub obscures it.
6571 SUPER:: Prefix to lookup for a method in @ISA classes.
6572 DESTROY Shorthand for `sub DESTROY {...}'.
6573 ... EQ ... Obsolete synonym of `eq'.
6574 ... GE ... Obsolete synonym of `ge'.
6575 ... GT ... Obsolete synonym of `gt'.
6576 ... LE ... Obsolete synonym of `le'.
6577 ... LT ... Obsolete synonym of `lt'.
6578 ... NE ... Obsolete synonym of `ne'.
6579 abs [ EXPR ] absolute value
6580 ... and ... Low-precedence synonym for &&.
6581 bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
6582 chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
6583 chr Converts a number to char with the same ordinal.
6584 else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
6585 elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
6586 exists $HASH{KEY} True if the key exists.
6587 format [NAME] = Start of output format. Ended by a single dot (.) on a line.
6588 formline PICTURE, LIST Backdoor into \"format\" processing.
6589 glob EXPR Synonym of <EXPR>.
6590 lc [ EXPR ] Returns lowercased EXPR.
6591 lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
6592 grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
6593 map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
6594 no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
6595 not ... Low-precedence synonym for ! - negation.
6596 ... or ... Low-precedence synonym for ||.
6597 pos STRING Set/Get end-position of the last match over this string, see \\G.
6598 quotemeta [ EXPR ] Quote regexp metacharacters.
6599 qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
6600 readline FH Synonym of <FH>.
6601 readpipe CMD Synonym of `CMD`.
6602 ref [ EXPR ] Type of EXPR when dereferenced.
6603 sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
6604 tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
6605 tied Returns internal object for a tied data.
6606 uc [ EXPR ] Returns upcased EXPR.
6607 ucfirst [ EXPR ] Returns EXPR with upcased first letter.
6608 untie VAR Unlink an object from a simple Perl variable.
6609 use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
6610 ... xor ... Low-precedence synonym for exclusive or.
6611 prototype \&SUB Returns prototype of the function given a reference.
6612 =head1 Top-level heading.
6613 =head2 Second-level heading.
6614 =head3 Third-level heading (is there such?).
6615 =over [ NUMBER ] Start list.
6616 =item [ TITLE ] Start new item in the list.
6617 =back End list.
6618 =cut Switch from POD to Perl.
6619 =pod Switch from Perl to POD.
6620 ")
6621
6622 (defun cperl-switch-to-doc-buffer ()
6623 "Go to the perl documentation buffer and insert the documentation."
6624 (interactive)
6625 (let ((buf (get-buffer-create cperl-doc-buffer)))
6626 (if (interactive-p)
6627 (switch-to-buffer-other-window buf)
6628 (set-buffer buf))
6629 (if (= (buffer-size) 0)
6630 (progn
6631 (insert (documentation-property 'cperl-short-docs
6632 'variable-documentation))
6633 (setq buffer-read-only t)))))
6634
6635 (defun cperl-beautify-regexp-piece (b e embed level)
6636 ;; b is before the starting delimiter, e before the ending
6637 ;; e should be a marker, may be changed, but remains "correct".
6638 ;; EMBED is nil iff we process the whole REx.
6639 ;; The REx is guarantied to have //x
6640 ;; LEVEL shows how many levels deep to go
6641 ;; position at enter and at leave is not defined
6642 (let (s c tmp (m (make-marker)) (m1 (make-marker)) c1 spaces inline code pos)
6643 (if (not embed)
6644 (goto-char (1+ b))
6645 (goto-char b)
6646 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
6647 (forward-char 2)
6648 (delete-char 1)
6649 (forward-char 1))
6650 ((looking-at "(\\?[^a-zA-Z]")
6651 (forward-char 3))
6652 ((looking-at "(\\?") ; (?i)
6653 (forward-char 2))
6654 (t
6655 (forward-char 1))))
6656 (setq c (if embed (current-indentation) (1- (current-column)))
6657 c1 (+ c (or cperl-regexp-indent-step cperl-indent-level)))
6658 (or (looking-at "[ \t]*[\n#]")
6659 (progn
6660 (insert "\n")))
6661 (goto-char e)
6662 (beginning-of-line)
6663 (if (re-search-forward "[^ \t]" e t)
6664 (progn ; Something before the ending delimiter
6665 (goto-char e)
6666 (delete-horizontal-space)
6667 (insert "\n")
6668 (indent-to-column c)
6669 (set-marker e (point))))
6670 (goto-char b)
6671 (end-of-line 2)
6672 (while (< (point) (marker-position e))
6673 (beginning-of-line)
6674 (setq s (point)
6675 inline t)
6676 (skip-chars-forward " \t")
6677 (delete-region s (point))
6678 (indent-to-column c1)
6679 (while (and
6680 inline
6681 (looking-at
6682 (concat "\\([a-zA-Z0-9]+[^*+{?]\\)" ; 1 word
6683 "\\|" ; Embedded variable
6684 "\\$\\([a-zA-Z0-9_]+\\([[{]\\)?\\|[^\n \t)|]\\)" ; 2 3
6685 "\\|" ; $ ^
6686 "[$^]"
6687 "\\|" ; simple-code simple-code*?
6688 "\\(\\\\.\\|[^][()#|*+?\n]\\)\\([*+{?]\\??\\)?" ; 4 5
6689 "\\|" ; Class
6690 "\\(\\[\\)" ; 6
6691 "\\|" ; Grouping
6692 "\\((\\(\\?\\)?\\)" ; 7 8
6693 "\\|" ; |
6694 "\\(|\\)" ; 9
6695 )))
6696 (goto-char (match-end 0))
6697 (setq spaces t)
6698 (cond ((match-beginning 1) ; Alphanum word + junk
6699 (forward-char -1))
6700 ((or (match-beginning 3) ; $ab[12]
6701 (and (match-beginning 5) ; X* X+ X{2,3}
6702 (eq (preceding-char) ?\{)))
6703 (forward-char -1)
6704 (forward-sexp 1))
6705 ((match-beginning 6) ; []
6706 (setq tmp (point))
6707 (if (looking-at "\\^?\\]")
6708 (goto-char (match-end 0)))
6709 ;; XXXX POSIX classes?!
6710 (while (and (not pos)
6711 (re-search-forward "\\[:\\|\\]" e t))
6712 (if (eq (preceding-char) ?:)
6713 (or (re-search-forward ":\\]" e t)
6714 (error "[:POSIX:]-group in []-group not terminated"))
6715 (setq pos t)))
6716 (or (eq (preceding-char) ?\])
6717 (error "[]-group not terminated"))
6718 (if (eq (following-char) ?\{)
6719 (progn
6720 (forward-sexp 1)
6721 (and (eq (following-char) ??)
6722 (forward-char 1)))
6723 (re-search-forward "\\=\\([*+?]\\??\\)" e t)))
6724 ((match-beginning 7) ; ()
6725 (goto-char (match-beginning 0))
6726 (setq pos (current-column))
6727 (or (eq pos c1)
6728 (progn
6729 (delete-horizontal-space)
6730 (insert "\n")
6731 (indent-to-column c1)))
6732 (setq tmp (point))
6733 (forward-sexp 1)
6734 ;; (or (forward-sexp 1)
6735 ;; (progn
6736 ;; (goto-char tmp)
6737 ;; (error "()-group not terminated")))
6738 (set-marker m (1- (point)))
6739 (set-marker m1 (point))
6740 (if (= level 1)
6741 (if (progn ; indent rigidly if multiline
6742 ;; In fact does not make a lot of sense, since
6743 ;; the starting position can be already lost due
6744 ;; to insertion of "\n" and " "
6745 (goto-char tmp)
6746 (search-forward "\n" m1 t))
6747 (indent-rigidly (point) m1 (- c1 pos)))
6748 (setq level (1- level))
6749 (cond
6750 ((not (match-beginning 8))
6751 (cperl-beautify-regexp-piece tmp m t level))
6752 ((eq (char-after (+ 2 tmp)) ?\{) ; Code
6753 t)
6754 ((eq (char-after (+ 2 tmp)) ?\() ; Conditional
6755 (goto-char (+ 2 tmp))
6756 (forward-sexp 1)
6757 (cperl-beautify-regexp-piece (point) m t level))
6758 ((eq (char-after (+ 2 tmp)) ?<) ; Lookbehind
6759 (goto-char (+ 3 tmp))
6760 (cperl-beautify-regexp-piece (point) m t level))
6761 (t
6762 (cperl-beautify-regexp-piece tmp m t level))))
6763 (goto-char m1)
6764 (cond ((looking-at "[*+?]\\??")
6765 (goto-char (match-end 0)))
6766 ((eq (following-char) ?\{)
6767 (forward-sexp 1)
6768 (if (eq (following-char) ?\?)
6769 (forward-char))))
6770 (skip-chars-forward " \t")
6771 (setq spaces nil)
6772 (if (looking-at "[#\n]")
6773 (progn
6774 (or (eolp) (indent-for-comment))
6775 (beginning-of-line 2))
6776 (delete-horizontal-space)
6777 (insert "\n"))
6778 (end-of-line)
6779 (setq inline nil))
6780 ((match-beginning 9) ; |
6781 (forward-char -1)
6782 (setq tmp (point))
6783 (beginning-of-line)
6784 (if (re-search-forward "[^ \t]" tmp t)
6785 (progn
6786 (goto-char tmp)
6787 (delete-horizontal-space)
6788 (insert "\n"))
6789 ;; first at line
6790 (delete-region (point) tmp))
6791 (indent-to-column c)
6792 (forward-char 1)
6793 (skip-chars-forward " \t")
6794 (setq spaces nil)
6795 (if (looking-at "[#\n]")
6796 (beginning-of-line 2)
6797 (delete-horizontal-space)
6798 (insert "\n"))
6799 (end-of-line)
6800 (setq inline nil)))
6801 (or (looking-at "[ \t\n]")
6802 (not spaces)
6803 (insert " "))
6804 (skip-chars-forward " \t"))
6805 (or (looking-at "[#\n]")
6806 (error "unknown code \"%s\" in a regexp"
6807 (buffer-substring (point) (1+ (point)))))
6808 (and inline (end-of-line 2)))
6809 ;; Special-case the last line of group
6810 (if (and (>= (point) (marker-position e))
6811 (/= (current-indentation) c))
6812 (progn
6813 (beginning-of-line)
6814 (setq s (point))
6815 (skip-chars-forward " \t")
6816 (delete-region s (point))
6817 (indent-to-column c)))
6818 ))
6819
6820 (defun cperl-make-regexp-x ()
6821 ;; Returns position of the start
6822 ;; XXX this is called too often! Need to cache the result!
6823 (save-excursion
6824 (or cperl-use-syntax-table-text-property
6825 (error "I need to have a regexp marked!"))
6826 ;; Find the start
6827 (if (looking-at "\\s|")
6828 nil ; good already
6829 (if (looking-at "\\([smy]\\|qr\\)\\s|")
6830 (forward-char 1)
6831 (re-search-backward "\\s|"))) ; Assume it is scanned already.
6832 ;;(forward-char 1)
6833 (let ((b (point)) (e (make-marker)) have-x delim (c (current-column))
6834 (sub-p (eq (preceding-char) ?s)) s)
6835 (forward-sexp 1)
6836 (set-marker e (1- (point)))
6837 (setq delim (preceding-char))
6838 (if (and sub-p (eq delim (char-after (- (point) 2))))
6839 (error "Possible s/blah// - do not know how to deal with"))
6840 (if sub-p (forward-sexp 1))
6841 (if (looking-at "\\sw*x")
6842 (setq have-x t)
6843 (insert "x"))
6844 ;; Protect fragile " ", "#"
6845 (if have-x nil
6846 (goto-char (1+ b))
6847 (while (re-search-forward "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*[ \t\n#]" e t) ; Need to include (?#) too?
6848 (forward-char -1)
6849 (insert "\\")
6850 (forward-char 1)))
6851 b)))
6852
6853 (defun cperl-beautify-regexp (&optional deep)
6854 "do it. (Experimental, may change semantics, recheck the result.)
6855 We suppose that the regexp is scanned already."
6856 (interactive "P")
6857 (if deep
6858 (prefix-numeric-value deep)
6859 (setq deep -1))
6860 (save-excursion
6861 (goto-char (cperl-make-regexp-x))
6862 (let ((b (point)) (e (make-marker)))
6863 (forward-sexp 1)
6864 (set-marker e (1- (point)))
6865 (cperl-beautify-regexp-piece b e nil deep))))
6866
6867 (defun cperl-regext-to-level-start ()
6868 "Goto start of an enclosing group in regexp.
6869 We suppose that the regexp is scanned already."
6870 (interactive)
6871 (let ((limit (cperl-make-regexp-x)) done)
6872 (while (not done)
6873 (or (eq (following-char) ?\()
6874 (search-backward "(" (1+ limit) t)
6875 (error "Cannot find `(' which starts a group"))
6876 (setq done
6877 (save-excursion
6878 (skip-chars-backward "\\")
6879 (looking-at "\\(\\\\\\\\\\)*(")))
6880 (or done (forward-char -1)))))
6881
6882 (defun cperl-contract-level ()
6883 "Find an enclosing group in regexp and contract it.
6884 \(Experimental, may change semantics, recheck the result.)
6885 We suppose that the regexp is scanned already."
6886 (interactive)
6887 ;; (save-excursion ; Can't, breaks `cperl-contract-levels'
6888 (cperl-regext-to-level-start)
6889 (let ((b (point)) (e (make-marker)) s c)
6890 (forward-sexp 1)
6891 (set-marker e (1- (point)))
6892 (goto-char b)
6893 (while (re-search-forward "\\(#\\)\\|\n" e 'to-end)
6894 (cond
6895 ((match-beginning 1) ; #-comment
6896 (or c (setq c (current-indentation)))
6897 (beginning-of-line 2) ; Skip
6898 (setq s (point))
6899 (skip-chars-forward " \t")
6900 (delete-region s (point))
6901 (indent-to-column c))
6902 (t
6903 (delete-char -1)
6904 (just-one-space))))))
6905
6906 (defun cperl-contract-levels ()
6907 "Find an enclosing group in regexp and contract all the kids.
6908 \(Experimental, may change semantics, recheck the result.)
6909 We suppose that the regexp is scanned already."
6910 (interactive)
6911 (save-excursion
6912 (condition-case nil
6913 (cperl-regext-to-level-start)
6914 (error ; We are outside outermost group
6915 (goto-char (cperl-make-regexp-x))))
6916 (let ((b (point)) (e (make-marker)) s c)
6917 (forward-sexp 1)
6918 (set-marker e (1- (point)))
6919 (goto-char (1+ b))
6920 (while (re-search-forward "\\(\\\\\\\\\\)\\|(" e t)
6921 (cond
6922 ((match-beginning 1) ; Skip
6923 nil)
6924 (t ; Group
6925 (cperl-contract-level)))))))
6926
6927 (defun cperl-beautify-level (&optional deep)
6928 "Find an enclosing group in regexp and beautify it.
6929 \(Experimental, may change semantics, recheck the result.)
6930 We suppose that the regexp is scanned already."
6931 (interactive "P")
6932 (if deep
6933 (prefix-numeric-value deep)
6934 (setq deep -1))
6935 (save-excursion
6936 (cperl-regext-to-level-start)
6937 (let ((b (point)) (e (make-marker)))
6938 (forward-sexp 1)
6939 (set-marker e (1- (point)))
6940 (cperl-beautify-regexp-piece b e nil deep))))
6941
6942 (defun cperl-invert-if-unless ()
6943 "Change `if (A) {B}' into `B if A;' etc if possible."
6944 (interactive)
6945 (or (looking-at "\\<")
6946 (forward-sexp -1))
6947 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
6948 (let ((pos1 (point))
6949 pos2 pos3 pos4 pos5 s1 s2 state p pos45
6950 (s0 (buffer-substring (match-beginning 0) (match-end 0))))
6951 (forward-sexp 2)
6952 (setq pos3 (point))
6953 (forward-sexp -1)
6954 (setq pos2 (point))
6955 (if (eq (following-char) ?\( )
6956 (progn
6957 (goto-char pos3)
6958 (forward-sexp 1)
6959 (setq pos5 (point))
6960 (forward-sexp -1)
6961 (setq pos4 (point))
6962 ;; XXXX In fact may be `A if (B); {C}' ...
6963 (if (and (eq (following-char) ?\{ )
6964 (progn
6965 (cperl-backward-to-noncomment pos3)
6966 (eq (preceding-char) ?\) )))
6967 (if (condition-case nil
6968 (progn
6969 (goto-char pos5)
6970 (forward-sexp 1)
6971 (forward-sexp -1)
6972 (looking-at "\\<els\\(e\\|if\\)\\>"))
6973 (error nil))
6974 (error
6975 "`%s' (EXPR) {BLOCK} with `else'/`elsif'" s0)
6976 (goto-char (1- pos5))
6977 (cperl-backward-to-noncomment pos4)
6978 (if (eq (preceding-char) ?\;)
6979 (forward-char -1))
6980 (setq pos45 (point))
6981 (goto-char pos4)
6982 (while (re-search-forward "\\<\\(for\\|foreach\\|if\\|unless\\|while\\|until\\)\\>\\|;" pos45 t)
6983 (setq p (match-beginning 0)
6984 s1 (buffer-substring p (match-end 0))
6985 state (parse-partial-sexp pos4 p))
6986 (or (nth 3 state)
6987 (nth 4 state)
6988 (nth 5 state)
6989 (error "`%s' inside `%s' BLOCK" s1 s0))
6990 (goto-char (match-end 0)))
6991 ;; Finally got it
6992 (goto-char (1+ pos4))
6993 (skip-chars-forward " \t\n")
6994 (setq s2 (buffer-substring (point) pos45))
6995 (goto-char pos45)
6996 (or (looking-at ";?[ \t\n]*}")
6997 (progn
6998 (skip-chars-forward "; \t\n")
6999 (setq s2 (concat s2 "\n" (buffer-substring (point) (1- pos5))))))
7000 (and (equal s2 "")
7001 (setq s2 "1"))
7002 (goto-char (1- pos3))
7003 (cperl-backward-to-noncomment pos2)
7004 (or (looking-at "[ \t\n]*)")
7005 (goto-char (1- pos3)))
7006 (setq p (point))
7007 (goto-char (1+ pos2))
7008 (skip-chars-forward " \t\n")
7009 (setq s1 (buffer-substring (point) p))
7010 (delete-region pos4 pos5)
7011 (delete-region pos2 pos3)
7012 (goto-char pos1)
7013 (insert s2 " ")
7014 (just-one-space)
7015 (forward-word 1)
7016 (setq pos1 (point))
7017 (insert " " s1 ";")
7018 (delete-horizontal-space)
7019 (forward-char -1)
7020 (delete-horizontal-space)
7021 (goto-char pos1)
7022 (just-one-space)
7023 (cperl-indent-line))
7024 (error "`%s' (EXPR) not with an {BLOCK}" s0)))
7025 (error "`%s' not with an (EXPR)" s0)))
7026 (error "Not at `if', `unless', `while', `unless', `for' or `foreach'")))
7027
7028 ;;; By Anthony Foiani <afoiani@uswest.com>
7029 ;;; Getting help on modules in C-h f ?
7030 ;;; This is a modified version of `man'.
7031 ;;; Need to teach it how to lookup functions
7032 (defun cperl-perldoc (word)
7033 "Run `perldoc' on WORD."
7034 (interactive
7035 (list (let* ((default-entry (cperl-word-at-point))
7036 (input (read-string
7037 (format "perldoc entry%s: "
7038 (if (string= default-entry "")
7039 ""
7040 (format " (default %s)" default-entry))))))
7041 (if (string= input "")
7042 (if (string= default-entry "")
7043 (error "No perldoc args given")
7044 default-entry)
7045 input))))
7046 (require 'man)
7047 (let* ((is-func (and
7048 (string-match "^[a-z]+$" word)
7049 (string-match (concat "^" word "\\>")
7050 (documentation-property
7051 'cperl-short-docs
7052 'variable-documentation))))
7053 (manual-program (if is-func "perldoc -f" "perldoc")))
7054 (Man-getpage-in-background word)))
7055
7056 (defun cperl-perldoc-at-point ()
7057 "Run a `perldoc' on the word around point."
7058 (interactive)
7059 (cperl-perldoc (cperl-word-at-point)))
7060
7061 (defcustom pod2man-program "pod2man"
7062 "*File name for `pod2man'."
7063 :type 'file
7064 :group 'cperl)
7065
7066 ;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
7067 (defun cperl-pod-to-manpage ()
7068 "Create a virtual manpage in Emacs from the Perl Online Documentation."
7069 (interactive)
7070 (require 'man)
7071 (let* ((pod2man-args (concat buffer-file-name " | nroff -man "))
7072 (bufname (concat "Man " buffer-file-name))
7073 (buffer (generate-new-buffer bufname)))
7074 (save-excursion
7075 (set-buffer buffer)
7076 (let ((process-environment (copy-sequence process-environment)))
7077 ;; Prevent any attempt to use display terminal fanciness.
7078 (setenv "TERM" "dumb")
7079 (set-process-sentinel
7080 (start-process pod2man-program buffer "sh" "-c"
7081 (format (cperl-pod2man-build-command) pod2man-args))
7082 'Man-bgproc-sentinel)))))
7083
7084 (defun cperl-pod2man-build-command ()
7085 "Builds the entire background manpage and cleaning command."
7086 (let ((command (concat pod2man-program " %s 2>/dev/null"))
7087 (flist Man-filter-list))
7088 (while (and flist (car flist))
7089 (let ((pcom (car (car flist)))
7090 (pargs (cdr (car flist))))
7091 (setq command
7092 (concat command " | " pcom " "
7093 (mapconcat '(lambda (phrase)
7094 (if (not (stringp phrase))
7095 (error "Malformed Man-filter-list"))
7096 phrase)
7097 pargs " ")))
7098 (setq flist (cdr flist))))
7099 command))
7100
7101 (defun cperl-lazy-install ()) ; Avoid a warning
7102
7103 (if (fboundp 'run-with-idle-timer)
7104 (progn
7105 (defvar cperl-help-shown nil
7106 "Non-nil means that the help was already shown now.")
7107
7108 (defvar cperl-lazy-installed nil
7109 "Non-nil means that the lazy-help handlers are installed now.")
7110
7111 (defun cperl-lazy-install ()
7112 (interactive)
7113 (make-variable-buffer-local 'cperl-help-shown)
7114 (if (and (cperl-val 'cperl-lazy-help-time)
7115 (not cperl-lazy-installed))
7116 (progn
7117 (add-hook 'post-command-hook 'cperl-lazy-hook)
7118 (run-with-idle-timer
7119 (cperl-val 'cperl-lazy-help-time 1000000 5)
7120 t
7121 'cperl-get-help-defer)
7122 (setq cperl-lazy-installed t))))
7123
7124 (defun cperl-lazy-unstall ()
7125 (interactive)
7126 (remove-hook 'post-command-hook 'cperl-lazy-hook)
7127 (cancel-function-timers 'cperl-get-help-defer)
7128 (setq cperl-lazy-installed nil))
7129
7130 (defun cperl-lazy-hook ()
7131 (setq cperl-help-shown nil))
7132
7133 (defun cperl-get-help-defer ()
7134 (when (memq major-mode '(perl-mode cperl-mode))
7135 (let ((cperl-message-on-help-error nil) (cperl-help-from-timer t))
7136 (cperl-get-help)
7137 (setq cperl-help-shown t))))
7138 (cperl-lazy-install)))
7139
7140
7141 ;;; Plug for wrong font-lock:
7142
7143 (defun cperl-font-lock-unfontify-region-function (beg end)
7144 (let* ((modified (buffer-modified-p)) (buffer-undo-list t)
7145 (inhibit-read-only t) (inhibit-point-motion-hooks t)
7146 before-change-functions after-change-functions
7147 deactivate-mark buffer-file-name buffer-file-truename)
7148 (remove-text-properties beg end '(face nil))
7149 (when (and (not modified) (buffer-modified-p))
7150 (set-buffer-modified-p nil))))
7151
7152 (defvar cperl-d-l nil)
7153 (defun cperl-fontify-syntaxically (end)
7154 ;; Some vars for debugging only
7155 ;; (message "Syntaxifying...")
7156 (let (start (dbg (point)) (iend end)
7157 (istate (car cperl-syntax-state)))
7158 (and cperl-syntaxify-unwind
7159 (setq end (cperl-unwind-to-safe t end)))
7160 (setq start (point))
7161 (or cperl-syntax-done-to
7162 (setq cperl-syntax-done-to (point-min)))
7163 (if (or (not (boundp 'font-lock-hot-pass))
7164 (eval 'font-lock-hot-pass)
7165 t) ; Not debugged otherwise
7166 ;; Need to forget what is after `start'
7167 (setq start (min cperl-syntax-done-to start))
7168 ;; Fontification without a change
7169 (setq start (max cperl-syntax-done-to start)))
7170 (and (> end start)
7171 (setq cperl-syntax-done-to start) ; In case what follows fails
7172 (cperl-find-pods-heres start end t nil t))
7173 (if (eq cperl-syntaxify-by-font-lock 'message)
7174 (message "Syntaxified %s..%s from %s to %s(%s), state %s-->%s"
7175 dbg iend
7176 start end cperl-syntax-done-to
7177 istate (car cperl-syntax-state))) ; For debugging
7178 nil)) ; Do not iterate
7179
7180 (defun cperl-fontify-update (end)
7181 (let ((pos (point)) prop posend)
7182 (while (< pos end)
7183 (setq prop (get-text-property pos 'cperl-postpone))
7184 (setq posend (next-single-property-change pos 'cperl-postpone nil end))
7185 (and prop (put-text-property pos posend (car prop) (cdr prop)))
7186 (setq pos posend)))
7187 nil) ; Do not iterate
7188
7189 (defun cperl-update-syntaxification (from to)
7190 (if (and cperl-use-syntax-table-text-property
7191 cperl-syntaxify-by-font-lock
7192 (or (null cperl-syntax-done-to)
7193 (< cperl-syntax-done-to to)))
7194 (progn
7195 (save-excursion
7196 (goto-char from)
7197 (cperl-fontify-syntaxically to)))))
7198
7199 (defvar cperl-version
7200 (let ((v "Revision: 4.32"))
7201 (string-match ":\\s *\\([0-9.]+\\)" v)
7202 (substring v (match-beginning 1) (match-end 1)))
7203 "Version of IZ-supported CPerl package this file is based on.")
7204
7205 (provide 'cperl-mode)
7206
7207 ;;; cperl-mode.el ends here