*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / cperl-mode.el
CommitLineData
f83d2997
KH
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
f83d2997
KH
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', <<<<<<
db133cb6 41;;; `cperl-non-problems', `cperl-praise', `cperl-speed'. <<<<<<
f83d2997
KH
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
5bd52f0e 65;; Some macros are needed for `defcustom'
80585273
DL
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
ce22dd53 87 `(defvar ,arg (quote ,arg) ,descr))))
80585273
DL
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
ce22dd53 93 (defvar ,arg (quote ,arg) ,descr))))
80585273
DL
94 (if cperl-xemacs-p
95 (defmacro cperl-etags-snarf-tag (file line)
b787fc05 96 `(progn
80585273
DL
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"))
5bd52f0e
RS
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
ccc3ce39
SE
125(defgroup cperl nil
126 "Major mode for editing Perl code."
127 :prefix "cperl-"
db133cb6
RS
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)
ccc3ce39 160
f83d2997 161\f
ccc3ce39 162(defcustom cperl-extra-newline-before-brace nil
f83d2997
KH
163 "*Non-nil means that if, elsif, while, until, else, for, foreach
164and do constructs look like:
165
166 if ()
167 {
168 }
169
170instead of:
171
172 if () {
173 }
ccc3ce39
SE
174"
175 :type 'boolean
db133cb6
RS
176 :group 'cperl-autoinsert-details)
177
5c8b7eaf 178(defcustom cperl-extra-newline-before-brace-multiline
db133cb6
RS
179 cperl-extra-newline-before-brace
180 "*Non-nil means the same as `cperl-extra-newline-before-brace', but
181for constructs with multiline if/unless/while/until/for/foreach condition."
182 :type 'boolean
183 :group 'cperl-autoinsert-details)
ccc3ce39
SE
184
185(defcustom cperl-indent-level 2
186 "*Indentation of CPerl statements with respect to containing block."
187 :type 'integer
db133cb6 188 :group 'cperl-indentation-details)
f83d2997 189
ccc3ce39 190(defcustom cperl-lineup-step nil
f83d2997 191 "*`cperl-lineup' will always lineup at multiple of this number.
029cb4d5 192If nil, the value of `cperl-indent-level' will be used."
ccc3ce39 193 :type '(choice (const nil) integer)
db133cb6
RS
194 :group 'cperl-indentation-details)
195
ccc3ce39 196(defcustom cperl-brace-imaginary-offset 0
f83d2997
KH
197 "*Imagined indentation of a Perl open brace that actually follows a statement.
198An open brace following other text is treated as if it were this far
ccc3ce39
SE
199to the right of the start of its line."
200 :type 'integer
db133cb6 201 :group 'cperl-indentation-details)
ccc3ce39
SE
202
203(defcustom cperl-brace-offset 0
204 "*Extra indentation for braces, compared with other text in same context."
205 :type 'integer
db133cb6 206 :group 'cperl-indentation-details)
ccc3ce39
SE
207(defcustom cperl-label-offset -2
208 "*Offset of CPerl label lines relative to usual indentation."
209 :type 'integer
db133cb6 210 :group 'cperl-indentation-details)
ccc3ce39
SE
211(defcustom cperl-min-label-indent 1
212 "*Minimal offset of CPerl label lines."
213 :type 'integer
db133cb6 214 :group 'cperl-indentation-details)
ccc3ce39
SE
215(defcustom cperl-continued-statement-offset 2
216 "*Extra indent for lines not starting new statements."
217 :type 'integer
db133cb6 218 :group 'cperl-indentation-details)
ccc3ce39 219(defcustom cperl-continued-brace-offset 0
f83d2997 220 "*Extra indent for substatements that start with open-braces.
ccc3ce39
SE
221This is in addition to cperl-continued-statement-offset."
222 :type 'integer
db133cb6 223 :group 'cperl-indentation-details)
ccc3ce39
SE
224(defcustom cperl-close-paren-offset -1
225 "*Extra indent for substatements that start with close-parenthesis."
226 :type 'integer
db133cb6 227 :group 'cperl-indentation-details)
ccc3ce39
SE
228
229(defcustom cperl-auto-newline nil
f83d2997
KH
230 "*Non-nil means automatically newline before and after braces,
231and after colons and semicolons, inserted in CPerl code. The following
232\\[cperl-electric-backspace] will remove the inserted whitespace.
5c8b7eaf 233Insertion after colons requires both this variable and
ccc3ce39
SE
234`cperl-auto-newline-after-colon' set."
235 :type 'boolean
db133cb6 236 :group 'cperl-autoinsert-details)
f83d2997 237
6c389151
SM
238(defcustom cperl-autoindent-on-semi nil
239 "*Non-nil means automatically indent after insertion of (semi)colon.
240Active if `cperl-auto-newline' is false."
241 :type 'boolean
242 :group 'cperl-autoinsert-details)
243
ccc3ce39 244(defcustom cperl-auto-newline-after-colon nil
f83d2997 245 "*Non-nil means automatically newline even after colons.
ccc3ce39
SE
246Subject to `cperl-auto-newline' setting."
247 :type 'boolean
db133cb6 248 :group 'cperl-autoinsert-details)
f83d2997 249
ccc3ce39 250(defcustom cperl-tab-always-indent t
f83d2997 251 "*Non-nil means TAB in CPerl mode should always reindent the current line,
ccc3ce39
SE
252regardless of where in the line point is when the TAB command is used."
253 :type 'boolean
db133cb6 254 :group 'cperl-indentation-details)
f83d2997 255
ccc3ce39 256(defcustom cperl-font-lock nil
029cb4d5 257 "*Non-nil (and non-null) means CPerl buffers will use `font-lock-mode'.
ccc3ce39 258Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
259 :type '(choice (const null) boolean)
260 :group 'cperl-affected-by-hairy)
f83d2997 261
ccc3ce39 262(defcustom cperl-electric-lbrace-space nil
029cb4d5 263 "*Non-nil (and non-null) means { after $ should be preceded by ` '.
ccc3ce39 264Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
265 :type '(choice (const null) boolean)
266 :group 'cperl-affected-by-hairy)
f83d2997 267
ccc3ce39 268(defcustom cperl-electric-parens-string "({[]})<"
f83d2997 269 "*String of parentheses that should be electric in CPerl.
ccc3ce39
SE
270Closing ones are electric only if the region is highlighted."
271 :type 'string
db133cb6 272 :group 'cperl-affected-by-hairy)
f83d2997 273
ccc3ce39 274(defcustom cperl-electric-parens nil
f83d2997 275 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
ccc3ce39 276Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
277 :type '(choice (const null) boolean)
278 :group 'cperl-affected-by-hairy)
279
280(defvar zmacs-regions) ; Avoid warning
281
5c8b7eaf 282(defcustom cperl-electric-parens-mark
f83d2997
KH
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.
ccc3ce39
SE
289Default is yes if there is visual feedback on mark."
290 :type 'boolean
db133cb6 291 :group 'cperl-autoinsert-details)
f83d2997 292
ccc3ce39 293(defcustom cperl-electric-linefeed nil
f83d2997
KH
294 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
295In any case these two mean plain and hairy linefeeds together.
ccc3ce39 296Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
297 :type '(choice (const null) boolean)
298 :group 'cperl-affected-by-hairy)
f83d2997 299
ccc3ce39 300(defcustom cperl-electric-keywords nil
f83d2997 301 "*Not-nil (and non-null) means keywords are electric in CPerl.
ccc3ce39 302Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
303 :type '(choice (const null) boolean)
304 :group 'cperl-affected-by-hairy)
ccc3ce39
SE
305
306(defcustom cperl-hairy nil
db133cb6 307 "*Not-nil means most of the bells and whistles are enabled in CPerl.
5c8b7eaf 308Affects: `cperl-font-lock', `cperl-electric-lbrace-space',
db133cb6
RS
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'."
ccc3ce39 312 :type 'boolean
db133cb6 313 :group 'cperl-affected-by-hairy)
ccc3ce39
SE
314
315(defcustom cperl-comment-column 32
316 "*Column to put comments in CPerl (use \\[cperl-indent] to lineup with code)."
317 :type 'integer
db133cb6 318 :group 'cperl-indentation-details)
ccc3ce39
SE
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
5c8b7eaf 326(defcustom cperl-clobber-mode-lists
5bd52f0e
RS
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
ccc3ce39 336(defcustom cperl-info-on-command-no-prompt nil
f83d2997
KH
337 "*Not-nil (and non-null) means not to prompt on C-h f.
338The opposite behaviour is always available if prefixed with C-c.
ccc3ce39 339Can be overwritten by `cperl-hairy' if nil."
db133cb6
RS
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.
345The function is available on \\[cperl-info-on-command], \\[cperl-get-help].
346Can be overwritten by `cperl-hairy' if nil."
347 :type '(choice (const null) boolean)
348 :group 'cperl-affected-by-hairy)
f83d2997 349
ccc3ce39 350(defcustom cperl-lazy-help-time nil
db133cb6
RS
351 "*Not-nil (and non-null) means to show lazy help after given idle time.
352Can be overwritten by `cperl-hairy' to be 5 sec if nil."
300f7bb3 353 :type '(choice (const null) (const nil) integer)
db133cb6 354 :group 'cperl-affected-by-hairy)
f83d2997 355
ccc3ce39 356(defcustom cperl-pod-face 'font-lock-comment-face
80585273 357 "*Face for pod highlighting."
ccc3ce39 358 :type 'face
db133cb6 359 :group 'cperl-faces)
f83d2997 360
ccc3ce39 361(defcustom cperl-pod-head-face 'font-lock-variable-name-face
80585273 362 "*Face for pod highlighting.
ccc3ce39
SE
363Font for POD headers."
364 :type 'face
db133cb6 365 :group 'cperl-faces)
f83d2997 366
ccc3ce39 367(defcustom cperl-here-face 'font-lock-string-face
80585273 368 "*Face for here-docs highlighting."
ccc3ce39 369 :type 'face
db133cb6 370 :group 'cperl-faces)
f83d2997 371
ac6857fb 372(defcustom cperl-invalid-face 'underline
80585273
DL
373 "*Face for highlighting trailing whitespace."
374 :type 'face
ac6857fb 375 :version "21.1"
5bd52f0e
RS
376 :group 'cperl-faces)
377
ccc3ce39
SE
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
db133cb6 381 :group 'cperl-faces)
f83d2997 382
5bd52f0e
RS
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
6c389151
SM
388(defcustom cperl-highlight-variables-indiscriminately nil
389 "*Non-nil means perform additional highlighting on variables.
390Currently only changes how scalar variables are highlighted.
391Note that that variable is only read at initialization time for
392the variable `cperl-font-lock-keywords-2', so changing it after you've
393entered `cperl-mode' the first time will have no effect."
394 :type 'boolean
395 :group 'cperl)
396
ccc3ce39 397(defcustom cperl-pod-here-scan t
f83d2997 398 "*Not-nil means look for pod and here-docs sections during startup.
ccc3ce39
SE
399You can always make lookup from menu or using \\[cperl-find-pods-heres]."
400 :type 'boolean
db133cb6 401 :group 'cperl-speed)
f83d2997 402
6c389151
SM
403(defcustom cperl-regexp-scan t
404 "*Not-nil means make marking of regular expression more thorough.
405Effective only with `cperl-pod-here-scan'. Not implemented yet."
406 :type 'boolean
407 :group 'cperl-speed)
408
ccc3ce39 409(defcustom cperl-imenu-addback nil
f83d2997 410 "*Not-nil means add backreferences to generated `imenu's.
db133cb6 411May require patched `imenu' and `imenu-go'. Obsolete."
ccc3ce39 412 :type 'boolean
db133cb6 413 :group 'cperl-help-system)
f83d2997 414
ccc3ce39
SE
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))
db133cb6 418 :group 'cperl-help-system)
f83d2997 419
ccc3ce39
SE
420(defcustom cperl-shrink-wrap-info-frame t
421 "*Non-nil means shrink-wrapping of info-buffer-frame allowed."
422 :type 'boolean
db133cb6 423 :group 'cperl-help-system)
f83d2997 424
ccc3ce39 425(defcustom cperl-info-page "perl"
f83d2997 426 "*Name of the info page containing perl docs.
ccc3ce39
SE
427Older version of this page was called `perl5', newer `perl'."
428 :type 'string
db133cb6 429 :group 'cperl-help-system)
f83d2997 430
5c8b7eaf 431(defcustom cperl-use-syntax-table-text-property
f83d2997 432 (boundp 'parse-sexp-lookup-properties)
ccc3ce39
SE
433 "*Non-nil means CPerl sets up and uses `syntax-table' text property."
434 :type 'boolean
db133cb6 435 :group 'cperl-speed)
f83d2997 436
5c8b7eaf 437(defcustom cperl-use-syntax-table-text-property-for-tags
f83d2997 438 cperl-use-syntax-table-text-property
ccc3ce39
SE
439 "*Non-nil means: set up and use `syntax-table' text property generating TAGS."
440 :type 'boolean
db133cb6 441 :group 'cperl-speed)
ccc3ce39
SE
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.
029cb4d5 455If nil, the value of `cperl-indent-level' will be used."
ccc3ce39 456 :type '(choice integer (const nil))
db133cb6 457 :group 'cperl-indentation-details)
ccc3ce39
SE
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
db133cb6 462 :group 'cperl-indentation-details)
ccc3ce39 463
8f222248 464(defcustom cperl-under-as-char nil
ccc3ce39
SE
465 "*Non-nil means that the _ (underline) should be treated as word char."
466 :type 'boolean
467 :group 'cperl)
f83d2997 468
db133cb6
RS
469(defcustom cperl-extra-perl-args ""
470 "*Extra arguments to use when starting Perl.
471Currently 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'
482in `cperl-indent-region'. Set to nil to leave as is. Values other
483than 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
489need 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
5c8b7eaf 495when indenting a region.
db133cb6
RS
496Braces followed by else/elsif/while/until are excepted."
497 :type 'boolean
498 :group 'cperl-indentation-details)
499
500(defcustom cperl-merge-trailing-else t
5c8b7eaf 501 "*Non-nil means that BLOCK-end `}' followed by else/elsif/continue
db133cb6
RS
502may be merged to be on the same line when indenting a region."
503 :type 'boolean
504 :group 'cperl-indentation-details)
505
6c389151
SM
506(defcustom cperl-indent-parens-as-block nil
507 "*Non-nil means that non-block ()-, {}- and []-groups are indented as blocks,
508but for trailing \",\" inside the group, which won't increase indentation.
509One 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
5bd52f0e 515 (boundp 'parse-sexp-lookup-properties))
6c389151 516 "*Non-nil means that CPerl uses `font-lock's routines for syntaxification."
5bd52f0e
RS
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
523when syntaxifying a chunk of buffer."
db133cb6
RS
524 :type 'boolean
525 :group 'cperl-speed)
526
5bd52f0e
RS
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'."
5c8b7eaf 540 :type '(repeat (cons symbol
5bd52f0e
RS
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
5c8b7eaf 548 (defvar cperl-dark-background
5bd52f0e 549 (cperl-choose-color "navy" "os2blue" "darkgreen"))
5c8b7eaf 550 (defvar cperl-dark-foreground
5bd52f0e
RS
551 (cperl-choose-color "orchid1" "orange"))
552
553 (defface cperl-nonoverridable-face
b787fc05 554 `((((class grayscale) (background light))
58b64ac7 555 (:background "Gray90" :slant italic :underline t))
b787fc05 556 (((class grayscale) (background dark))
58b64ac7 557 (:foreground "Gray80" :slant italic :underline t :weight bold))
5c8b7eaf 558 (((class color) (background light))
b787fc05 559 (:foreground "chartreuse3"))
5c8b7eaf 560 (((class color) (background dark))
b787fc05 561 (:foreground ,cperl-dark-foreground))
58b64ac7 562 (t (:weight bold :underline t)))
5bd52f0e
RS
563 "Font Lock mode face used to highlight array names."
564 :group 'cperl-faces)
565
566 (defface cperl-array-face
b787fc05 567 `((((class grayscale) (background light))
58b64ac7 568 (:background "Gray90" :weight bold))
b787fc05 569 (((class grayscale) (background dark))
58b64ac7 570 (:foreground "Gray80" :weight bold))
5c8b7eaf 571 (((class color) (background light))
58b64ac7 572 (:foreground "Blue" :background "lightyellow2" :weight bold))
5c8b7eaf 573 (((class color) (background dark))
58b64ac7
RS
574 (:foreground "yellow" :background ,cperl-dark-background :weight bold))
575 (t (:weight bold)))
5bd52f0e
RS
576 "Font Lock mode face used to highlight array names."
577 :group 'cperl-faces)
578
579 (defface cperl-hash-face
b787fc05 580 `((((class grayscale) (background light))
58b64ac7 581 (:background "Gray90" :weight bold :slant italic))
b787fc05 582 (((class grayscale) (background dark))
58b64ac7 583 (:foreground "Gray80" :weight bold :slant italic))
5c8b7eaf 584 (((class color) (background light))
58b64ac7 585 (:foreground "Red" :background "lightyellow2" :weight bold :slant italic))
5c8b7eaf 586 (((class color) (background dark))
58b64ac7
RS
587 (:foreground "Red" :background ,cperl-dark-background :weight bold :slant italic))
588 (t (:weight bold :slant italic)))
5bd52f0e
RS
589 "Font Lock mode face used to highlight hash names."
590 :group 'cperl-faces)))
591
f83d2997
KH
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
599and/or
600 ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
db133cb6
RS
601Subdirectory `cperl-mode' may contain yet newer development releases and/or
602patches to related files.
f83d2997 603
5bd52f0e
RS
604For best results apply to an older Emacs the patches from
605 ftp://ftp.math.ohio-state.edu/pub/users/ilya/cperl-mode/patches
8e3acc66
RS
606\(this upgrades syntax-parsing abilities of Emaxen v19.34 and
607v20.2 up to the level of Emacs v20.3 - a must for a good Perl
608mode.)
5bd52f0e 609
f83d2997
KH
610Get support packages choose-color.el (or font-lock-extra.el before
61119.30), imenu-go.el from the same place. \(Look for other files there
612too... ;-). Get a patch for imenu.el in 19.29. Note that for 19.30 and
5c8b7eaf 613later you should use choose-color.el *instead* of font-lock-extra.el
f83d2997
KH
614\(and you will not get smart highlighting in C :-().
615
616Note that to enable Compile choices in the menu you need to install
617mode-compile.el.
618
5efe6a56
SM
619If your Emacs does not default to `cperl-mode' on Perl files, and you
620want it to: put the following into your .emacs file:
621
622 (defalias 'perl-mode 'cperl-mode)
623
624Get perl5-info from
f83d2997
KH
625 $CPAN/doc/manual/info/perl-info.tar.gz
626older version was on
627 http://www.metronet.com:70/9/perlinfo/perl5/manual/perl5-info.tar.gz
628
629If you use imenu-go, run imenu on perl5-info buffer (you can do it
5bd52f0e
RS
630from Perl menu). If many files are related, generate TAGS files from
631Tools/Tags submenu in Perl menu.
f83d2997
KH
632
633If some class structure is too complicated, use Tools/Hierarchy-view
029cb4d5 634from Perl menu, or hierarchic view of imenu. The second one uses the
f83d2997 635current buffer only, the first one requires generation of TAGS from
5bd52f0e
RS
636Perl/Tools/Tags menu beforehand.
637
638Run Perl/Tools/Insert-spaces-if-needed to fix your lazy typing.
639
640Switch auto-help on/off with Perl/Tools/Auto-help.
641
642Though with contemporary Emaxen CPerl mode should maintain the correct
643parsing of Perl even when editing, sometimes it may be lost. Fix this by
644
029cb4d5 645 \\[normal-mode]
f83d2997 646
5bd52f0e 647In cases of more severe confusion sometimes it is helpful to do
f83d2997 648
029cb4d5
SM
649 \\[load-library] cperl-mode RET
650 \\[normal-mode]
f83d2997 651
5bd52f0e
RS
652Before reporting (non-)problems look in the problem section of online
653micro-docs on what I know about CPerl problems.")
f83d2997
KH
654
655(defvar cperl-problems 'please-ignore-this-line
bab27c0c
RS
656"Some faces will not be shown on some versions of Emacs unless you
657install choose-color.el, available from
658 ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs/
659
6c389151
SM
660`fill-paragraph' on a comment may leave the point behind the
661paragraph. Parsing of lines with several <<EOF is not implemented
662yet.
663
8e3acc66 664Emacs had a _very_ restricted syntax parsing engine until version
5bd52f0e 66520.1. Most problems below are corrected starting from this version of
8e3acc66
RS
666Emacs, and all of them should be fixed in version 20.3. (Or apply
667patches to Emacs 19.33/34 - see tips.)
5bd52f0e 668
6c389151
SM
669Note that even with newer Emacsen in some very rare cases the details
670of interaction of `font-lock' and syntaxification may be not cleaned
671up yet. You may get slightly different colors basing on the order of
672fontification and syntaxification. Say, the initial faces is correct,
673but editing the buffer breaks this.
f83d2997 674
db133cb6
RS
675Even with older Emacsen CPerl mode tries to corrects some Emacs
676misunderstandings, however, for efficiency reasons the degree of
677correction is different for different operations. The partially
678corrected problems are: POD sections, here-documents, regexps. The
679operations are: highlighting, indentation, electric keywords, electric
680braces.
f83d2997
KH
681
682This may be confusing, since the regexp s#//#/#\; may be highlighted
683as a comment, but it will be recognized as a regexp by the indentation
684code. Or the opposite case, when a pod section is highlighted, but
685may break the indentation of the following code (though indentation
686should work if the balance of delimiters is not broken by POD).
687
688The main trick (to make $ a \"backslash\") makes constructions like
689${aaa} look like unbalanced braces. The only trick I can think of is
5c8b7eaf 690to insert it as $ {aaa} (legal in perl5, not in perl4).
f83d2997
KH
691
692Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
db133cb6
RS
693as /($|\\s)/. Note that such a transposition is not always possible.
694
5bd52f0e 695The solution is to upgrade your Emacs or patch an older one. Note
8e3acc66 696that Emacs 20.2 has some bugs related to `syntax-table' text
5bd52f0e
RS
697properties. Patches are available on the main CPerl download site,
698and on CPAN.
db133cb6
RS
699
700If these bugs cannot be fixed on your machine (say, you have an inferior
701environment and cannot recompile), you may still disable all the fancy stuff
702via `cperl-use-syntax-table-text-property'." )
f83d2997
KH
703
704(defvar cperl-non-problems 'please-ignore-this-line
5c8b7eaf 705"As you know from `problems' section, Perl syntax is too hard for CPerl on
5bd52f0e 706older Emacsen. Here is what you can do if you cannot upgrade, or if
8e3acc66 707you want to switch off these capabilities on Emacs 20.2 (+patches) or 20.3
5bd52f0e 708or better. Please skip this docs if you run a capable Emacs already.
f83d2997 709
db133cb6
RS
710Most 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
712not relevant on newer Emacsen, since they can do it automatically).
f83d2997
KH
713
714Try to help CPerl: add comments with embedded quotes to fix CPerl
715misunderstandings about the end of quotation:
716
717$a='500$'; # ';
718
719You won't need it too often. The reason: $ \"quotes\" the following
720character (this saves a life a lot of times in CPerl), thus due to
721Emacs parsing rules it does not consider tick (i.e., ' ) after a
db133cb6
RS
722dollar as a closing one, but as a usual character. This is usually
723correct, but not in the above context.
f83d2997 724
db133cb6
RS
725Even with older Emacsen the indentation code is pretty wise. The only
726drawback is that it relied on Emacs parsing to find matching
727parentheses. And Emacs *could not* match parentheses in Perl 100%
728correctly. So
f83d2997 729 1 if s#//#/#;
db133cb6 730would not break indentation, but
f83d2997 731 1 if ( s#//#/# );
db133cb6 732would. Upgrade.
f83d2997
KH
733
734By similar reasons
735 s\"abc\"def\";
6c389151 736could confuse CPerl a lot.
f83d2997
KH
737
738If you still get wrong indentation in situation that you think the
739code should be able to parse, try:
740
741a) Check what Emacs thinks about balance of your parentheses.
742b) Supply the code to me (IZ).
743
db133cb6
RS
744Pods were treated _very_ rudimentally. Here-documents were not
745treated at all (except highlighting and inhibiting indentation). Upgrade.
f83d2997
KH
746
747To 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
753Imenu in 19.31 is broken. Set `imenu-use-keymap-menu' to t, and remove
754`car' before `imenu-choose-buffer-index' in `imenu'.
5c8b7eaf 755`imenu-add-to-menubar' in 20.2 is broken.
5bd52f0e 756
bab27c0c 757A lot of things on XEmacs may be broken too, judging by bug reports I
029cb4d5 758receive. Note that some releases of XEmacs are better than the others
bab27c0c 759as far as bugs reports I see are concerned.")
f83d2997
KH
760
761(defvar cperl-praise 'please-ignore-this-line
8e3acc66 762 "Advantages of CPerl mode.
f83d2997
KH
763
7640) It uses the newest `syntax-table' property ;-);
765
7661) It does 99% of Perl syntax correct (as opposed to 80-90% in Perl
5c8b7eaf 767mode - but the latter number may have improved too in last years) even
5bd52f0e
RS
768with old Emaxen which do not support `syntax-table' property.
769
770When using `syntax-table' property for syntax assist hints, it should
771handle 99.995% of lines correct - or somesuch. It automatically
772updates syntax assist hints when you edit your script.
f83d2997 773
bab27c0c 7742) It is generally believed to be \"the most user-friendly Emacs
f83d2997
KH
775package\" whatever it may mean (I doubt that the people who say similar
776things tried _all_ the rest of Emacs ;-), but this was not a lonely
777voice);
778
7793) Everything is customizable, one-by-one or in a big sweep;
780
7814) 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
db133cb6 805 TAGS (if \"by directory\", .xs files - with C-language
f83d2997
KH
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\";
db133cb6
RS
816 m) Can convert from
817 if (A) { B }
818 to
819 B if A;
f83d2997 820
5bd52f0e 821 n) Highlights (by user-choice) either 3-delimiters constructs
6c389151
SM
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.
5bd52f0e 826
f83d2997
KH
8275) The indentation engine was very smart, but most of tricks may be
828not needed anymore with the support for `syntax-table' property. Has
829progress indicator for indentation (with `imenu' loaded).
830
5c8b7eaf 8316) Indent-region improves inline-comments as well; also corrects
db133cb6 832whitespace *inside* the conditional/loop constructs.
f83d2997
KH
833
8347) Fill-paragraph correctly handles multi-line comments;
db133cb6
RS
835
8368) Can switch to different indentation styles by one command, and restore
837the settings present before the switch.
838
5c8b7eaf 8399) When doing indentation of control constructs, may correct
db133cb6 840line-breaks/spacing between elements of the construct.
029cb4d5
SM
841
84210) Uses a linear-time algorith for indentation of regions (on Emaxen with
843capable syntax engines).
db133cb6
RS
844")
845
846(defvar cperl-speed 'please-ignore-this-line
847 "This is an incomplete compendium of what is available in other parts
848of CPerl documentation. (Please inform me if I skept anything.)
849
850There is a perception that CPerl is slower than alternatives. This part
851of documentation is designed to overcome this misconception.
852
853*By default* CPerl tries to enable the most comfortable settings.
854From most points of view, correctly working package is infinitely more
855comfortable than a non-correctly working one, thus by default CPerl
856prefers correctness over speed. Below is the guide how to change
857settings if your preferences are different.
858
859A) Speed of loading the file. When loading file, CPerl may perform a
860scan which indicates places which cannot be parsed by primitive Emacs
861syntax-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
5c8b7eaf 878B) Speed of editing operations.
db133cb6
RS
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.
5bd52f0e
RS
886
887 One can unset `cperl-syntaxify-unwind'. This might speed up editing
888 of, say, long POD sections.
f83d2997
KH
889")
890
5bd52f0e
RS
891(defvar cperl-tips-faces 'please-ignore-this-line
892 "CPerl mode uses following faces for highlighting:
893
8661c643
DL
894 `cperl-array-face' Array names
895 `cperl-hash-face' Hash names
896 `font-lock-comment-face' Comments, PODs and whatever is considered
5bd52f0e 897 syntaxically to be not code
8661c643 898 `font-lock-constant-face' HERE-doc delimiters, labels, delimiters of
5bd52f0e 899 2-arg operators s/y/tr/ or of RExen,
8661c643 900 `font-lock-function-name-face' Special-cased m// and s//foo/, _ as
5bd52f0e
RS
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
8661c643
DL
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,
5bd52f0e
RS
908 literal parts and the terminator of formats
909 and whatever is syntaxically considered
910 as string literals
8661c643
DL
911 `font-lock-type-face' Overridable keywords
912 `font-lock-variable-name-face' Variable declarations, indirect array and
5bd52f0e 913 hash names, POD headers/item names
8661c643 914 `cperl-invalid-face' Trailing whitespace
5bd52f0e
RS
915
916Note that in several situations the highlighting tries to inform about
917possible confusion, such as different colors for function names in
918declarations depending on what they (do not) override, or special cases
919m// and s/// which do not do what one would expect them to do.
920
5c8b7eaf 921Help with best setup of these faces for printout requested (for each of
5bd52f0e
RS
922the faces: please specify bold, italic, underline, shadow and box.)
923
924\(Not finished.)")
925
f83d2997
KH
926\f
927
928;;; Portability stuff:
929
db133cb6
RS
930(defconst cperl-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
931
f83d2997 932(defmacro cperl-define-key (emacs-key definition &optional xemacs-key)
b787fc05
GM
933 `(define-key cperl-mode-map
934 ,(if xemacs-key
935 `(if cperl-xemacs-p ,xemacs-key ,emacs-key)
936 emacs-key)
937 ,definition))
f83d2997
KH
938
939(defvar cperl-del-back-ch
940 (car (append (where-is-internal 'delete-backward-char)
941 (where-is-internal 'backward-delete-char-untabify)))
029cb4d5 942 "Character generated by key bound to `delete-backward-char'.")
f83d2997 943
5c8b7eaf 944(and (vectorp cperl-del-back-ch) (= (length cperl-del-back-ch) 1)
f83d2997
KH
945 (setq cperl-del-back-ch (aref cperl-del-back-ch 0)))
946
db133cb6 947(defun cperl-mark-active () (mark)) ; Avoid undefined warning
f83d2997
KH
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 ()
db133cb6 953 (if zmacs-regions (mark) t)))
f83d2997
KH
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
db133cb6
RS
961(defun cperl-putback-char (c) ; Emacs 19
962 (set 'unread-command-events (list c))) ; Avoid undefined warning
963
f83d2997
KH
964(if (boundp 'unread-command-events)
965 (if cperl-xemacs-p
966 (defun cperl-putback-char (c) ; XEmacs >= 19.12
db133cb6 967 (setq unread-command-events (list (eval '(character-to-event c))))))
f83d2997 968 (defun cperl-putback-char (c) ; XEmacs <= 19.11
db133cb6 969 (set 'unread-command-event (eval '(character-to-event c))))) ; Avoid warnings
f83d2997
KH
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
5bd52f0e
RS
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
f83d2997 986 (put-text-property (max (point-min) (1- from))
5bd52f0e 987 to cperl-do-not-fontify t)))
f83d2997 988
ccc3ce39
SE
989(defcustom cperl-mode-hook nil
990 "Hook run by `cperl-mode'."
991 :type 'hook
992 :group 'cperl)
f83d2997 993
db133cb6
RS
994(defvar cperl-syntax-state nil)
995(defvar cperl-syntax-done-to nil)
5bd52f0e 996(defvar cperl-emacs-can-parse (> (length (save-excursion
ce22dd53 997 (parse-partial-sexp (point) (point)))) 9))
db133cb6
RS
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))))
f83d2997
KH
1005\f
1006;;; Probably it is too late to set these guys already, but it can help later:
1007
5bd52f0e 1008;;;(and cperl-clobber-mode-lists
f83d2997
KH
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
5bd52f0e 1013;;; '(("miniperl" . perl-mode))))))
80585273
DL
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)
e8af40ee 1037 `(error "This version of Emacs has no `ps-extend-face-list'")))
80585273
DL
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))
f83d2997
KH
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)
db133cb6 1063 (cperl-define-key "\C-c\C-t" 'cperl-invert-if-unless)
f83d2997
KH
1064 (cperl-define-key "\C-c\C-a" 'cperl-toggle-auto-newline)
1065 (cperl-define-key "\C-c\C-k" 'cperl-toggle-abbrev)
db133cb6
RS
1066 (cperl-define-key "\C-c\C-w" 'cperl-toggle-construct-fix)
1067 (cperl-define-key "\C-c\C-f" 'auto-fill-mode)
f83d2997 1068 (cperl-define-key "\C-c\C-e" 'cperl-toggle-electric)
db133cb6 1069 (cperl-define-key "\C-c\C-ha" 'cperl-toggle-autohelp)
f83d2997
KH
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:
db133cb6
RS
1078 (cperl-define-key "\C-c\C-hF" 'cperl-info-on-command
1079 [(control c) (control h) F])
db133cb6
RS
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
5bd52f0e
RS
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]))
5c8b7eaf 1104 (if (and cperl-xemacs-p
f83d2997
KH
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)
db133cb6
RS
1125(defvar cperl-lazy-installed)
1126(defvar cperl-old-style nil)
f83d2997
KH
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)]
6c389151 1139 ["Invert if/unless/while etc" cperl-invert-if-unless t]
db133cb6
RS
1140 ("Regexp"
1141 ["Beautify" cperl-beautify-regexp
1142 cperl-use-syntax-table-text-property]
6c389151
SM
1143 ["Beautify one level deep" (cperl-beautify-regexp 1)
1144 cperl-use-syntax-table-text-property]
db133cb6
RS
1145 ["Beautify a group" cperl-beautify-level
1146 cperl-use-syntax-table-text-property]
6c389151
SM
1147 ["Beautify a group one level deep" (cperl-beautify-level 1)
1148 cperl-use-syntax-table-text-property]
db133cb6
RS
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])
f83d2997
KH
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]
5c8b7eaf 1172 ["CPerl pretty print (exprmntl)" cperl-ps-print
5bd52f0e 1173 (fboundp 'ps-extend-face-list)]
f83d2997
KH
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]
5c8b7eaf 1180;;; ["Create tags for Perl files in (sub)directories"
f83d2997
KH
1181;;; (cperl-etags nil 'recursive) t]
1182;;; ["Add tags for Perl files in (sub)directories"
5c8b7eaf 1183;;; (cperl-etags t 'recursive) t])
f83d2997
KH
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]
5c8b7eaf 1187 ["Create tags for Perl files in directory"
f83d2997 1188 (cperl-write-tags nil t nil t) t]
5c8b7eaf 1189 ["Add tags for Perl files in directory"
f83d2997 1190 (cperl-write-tags nil nil nil t) t]
5c8b7eaf 1191 ["Create tags for Perl files in (sub)directories"
f83d2997
KH
1192 (cperl-write-tags nil t t t) t]
1193 ["Add tags for Perl files in (sub)directories"
db133cb6
RS
1194 (cperl-write-tags nil nil t t) t]))
1195 ("Perl docs"
5c8b7eaf 1196 ["Define word at point" imenu-go-find-at-position
f83d2997
KH
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]
db133cb6
RS
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]
5c8b7eaf 1204 ["Auto-help on" cperl-lazy-install
db133cb6
RS
1205 (and (fboundp 'run-with-idle-timer)
1206 (not cperl-lazy-installed))]
5c8b7eaf 1207 ["Auto-help off" (eval '(cperl-lazy-unstall))
db133cb6
RS
1208 (and (fboundp 'run-with-idle-timer)
1209 cperl-lazy-installed)])
f83d2997
KH
1210 ("Toggle..."
1211 ["Auto newline" cperl-toggle-auto-newline t]
1212 ["Electric parens" cperl-toggle-electric t]
1213 ["Electric keywords" cperl-toggle-abbrev t]
db133cb6 1214 ["Fix whitespace on indent" cperl-toggle-construct-fix t]
5c8b7eaf 1215 ["Auto fill" auto-fill-mode t])
f83d2997 1216 ("Indent styles..."
db133cb6
RS
1217 ["CPerl" (cperl-set-style "CPerl") t]
1218 ["PerlStyle" (cperl-set-style "PerlStyle") t]
f83d2997
KH
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]
db133cb6
RS
1223 ["Whitesmith" (cperl-set-style "Whitesmith") t]
1224 ["Current" (cperl-set-style "Current") t]
1225 ["Memorized" (cperl-set-style-back) cperl-old-style])
f83d2997
KH
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]
db133cb6
RS
1230 ["Speed" (describe-variable 'cperl-speed) t]
1231 ["Praise" (describe-variable 'cperl-praise) t]
5bd52f0e
RS
1232 ["Faces" (describe-variable 'cperl-tips-faces) t]
1233 ["CPerl mode" (describe-function 'cperl-mode) t]
5c8b7eaf 1234 ["CPerl version"
bedf0b73 1235 (message "The version of master-file for this CPerl is %s-emacs"
5bd52f0e 1236 cperl-version) t]))))
f83d2997
KH
1237 (error nil))
1238
1239(autoload 'c-macro-expand "cmacexp"
1240 "Display the result of expanding all C macros occurring in the region.
1241The expansion is entirely correct because it uses the C preprocessor."
1242 t)
1243
95f433f4
RS
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
8dd511f6
RS
1252(defvar cperl-outline-regexp
1253 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
1254
f83d2997
KH
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
db133cb6 1290(defvar cperl-faces-init nil)
f83d2997
KH
1291;; Fix for msb.el
1292(defvar cperl-msb-fixed nil)
1293;;;###autoload
1294(defun cperl-mode ()
1295 "Major mode for editing Perl code.
1296Expression and list commands understand all C brackets.
1297Tab indents for Perl code.
1298Paragraphs are separated by blank lines only.
1299Delete converts tabs to spaces as it moves back.
1300
1301Various characters in Perl almost always come in pairs: {}, (), [],
1302sometimes <>. When the user types the first, she gets the second as
1303well, with optional special formatting done on {}. (Disabled by
1304default.) You can always quote (with \\[quoted-insert]) the left
1305\"paren\" to avoid the expansion. The processing of < is special,
1306since most the time you mean \"less\". Cperl mode tries to guess
1307whether you want to type pair <>, and inserts is if it
1308appropriate. You can set `cperl-electric-parens-string' to the string that
1309contains the parenths from the above list you want to be electrical.
1310Electricity of parenths is controlled by `cperl-electric-parens'.
1311You may also set `cperl-electric-parens-mark' to have electric parens
1312look for active mark and \"embrace\" a region if possible.'
1313
1314CPerl mode provides expansion of the Perl control constructs:
db133cb6 1315
5c8b7eaf 1316 if, else, elsif, unless, while, until, continue, do,
db133cb6
RS
1317 for, foreach, formy and foreachmy.
1318
1319and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1320
1321The user types the keyword immediately followed by a space, which
1322causes the construct to be expanded, and the point is positioned where
1323she is most likely to want to be. eg. when the user types a space
1324following \"if\" the following appears in the buffer: if () { or if ()
1325} { } and the cursor is between the parentheses. The user can then
1326type some boolean expression within the parens. Having done that,
1327typing \\[cperl-linefeed] places you - appropriately indented - on a
1328new line between the braces (if you typed \\[cperl-linefeed] in a POD
5c8b7eaf 1329directive line, then appropriate number of new lines is inserted).
db133cb6
RS
1330
1331If CPerl decides that you want to insert \"English\" style construct like
1332
f83d2997 1333 bite if angry;
db133cb6
RS
1334
1335it will not do any expansion. See also help on variable
1336`cperl-extra-newline-before-brace'. (Note that one can switch the
1337help message on expansion by setting `cperl-message-electric-keyword'
1338to nil.)
f83d2997
KH
1339
1340\\[cperl-linefeed] is a convenience replacement for typing carriage
1341return. It places you in the next line with proper indentation, or if
1342you type it inside the inline block of control construct, like
db133cb6 1343
f83d2997 1344 foreach (@lines) {print; print}
db133cb6 1345
f83d2997
KH
1346and you are on a boundary of a statement inside braces, it will
1347transform the construct into a multiline and will place you into an
5c8b7eaf
SS
1348appropriately indented blank line. If you need a usual
1349`newline-and-indent' behaviour, it is on \\[newline-and-indent],
f83d2997
KH
1350see documentation on `cperl-electric-linefeed'.
1351
db133cb6
RS
1352Use \\[cperl-invert-if-unless] to change a construction of the form
1353
1354 if (A) { B }
1355
1356into
1357
1358 B if A;
1359
f83d2997
KH
1360\\{cperl-mode-map}
1361
db133cb6
RS
1362Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1363\(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1364on electric space between $ and {, `cperl-electric-parens-string' is
1365the string that contains parentheses that should be electric in CPerl
1366\(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
f83d2997
KH
1367setting `cperl-electric-keywords' enables electric expansion of
1368control structures in CPerl. `cperl-electric-linefeed' governs which
1369one of two linefeed behavior is preferable. You can enable all these
1370options simultaneously (recommended mode of use) by setting
1371`cperl-hairy' to t. In this case you can switch separate options off
db133cb6
RS
1372by setting them to `null'. Note that one may undo the extra
1373whitespace inserted by semis and braces in `auto-newline'-mode by
1374consequent \\[cperl-electric-backspace].
f83d2997
KH
1375
1376If 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.
1378These keys run commands `cperl-info-on-current-command' and
1379`cperl-info-on-command', which one is which is controlled by variable
5c8b7eaf 1380`cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
db133cb6 1381\(in turn affected by `cperl-hairy').
f83d2997
KH
1382
1383Even if you have no info-format documentation, short one-liner-style
db133cb6
RS
1384help is available on \\[cperl-get-help], and one can run perldoc or
1385man via menu.
f83d2997 1386
db133cb6
RS
1387It is possible to show this help automatically after some idle time.
1388This 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
1390secs idle time . It is also possible to switch this on/off from the
1391menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
f83d2997
KH
1392
1393Use \\[cperl-lineup] to vertically lineup some construction - put the
1394beginning of the region at the start of construction, and make region
1395span the needed amount of lines.
1396
1397Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1398`cperl-pod-face', `cperl-pod-head-face' control processing of pod and
db133cb6
RS
1399here-docs sections. With capable Emaxen results of scan are used
1400for indentation too, otherwise they are used for highlighting only.
f83d2997
KH
1401
1402Variables 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.
db133cb6
RS
1406 `cperl-indent-left-aligned-comments'
1407 Non-nil means that the comment starting in leftmost column should indent.
f83d2997
KH
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.
5c8b7eaf
SS
1412 Insertion after colons requires both this variable and
1413 `cperl-auto-newline-after-colon' set.
f83d2997
KH
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
1437Settings 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
db133cb6
RS
1443CPerl knows several indentation styles, and may bulk set the
1444corresponding 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
1448If `cperl-indent-level' is 0, the statement after opening brace in
5c8b7eaf 1449column 0 is indented on
db133cb6 1450`cperl-brace-offset'+`cperl-continued-statement-offset'.
f83d2997
KH
1451
1452Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
db133cb6
RS
1453with no args.
1454
1455DO NOT FORGET to read micro-docs (available from `Perl' menu)
1456or as help on variables `cperl-tips', `cperl-problems',
1457`cperl-non-problems', `cperl-praise', `cperl-speed'."
f83d2997
KH
1458 (interactive)
1459 (kill-all-local-variables)
f83d2997
KH
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)))
db133cb6
RS
1465 (if (and
1466 (cperl-val 'cperl-clobber-lisp-bindings)
1467 (cperl-val 'cperl-info-on-command-no-prompt))
f83d2997
KH
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])))
029cb4d5 1473 (setq major-mode 'cperl-mode)
f83d2997
KH
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)
db133cb6 1484 ("continue" "continue" cperl-electric-else 0)
f83d2997
KH
1485 ("for" "for" cperl-electric-keyword 0)
1486 ("foreach" "foreach" cperl-electric-keyword 0)
db133cb6
RS
1487 ("formy" "formy" cperl-electric-keyword 0)
1488 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1489 ("do" "do" cperl-electric-keyword 0)
6c389151
SM
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)
db133cb6
RS
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)))
f83d2997
KH
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)
6c389151
SM
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)
f83d2997
KH
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
80585273 1536 (function cperl-imenu--create-perl-index))
f83d2997
KH
1537 (make-local-variable 'imenu-sort-function)
1538 (setq imenu-sort-function nil)
1539 (make-local-variable 'vc-header-alist)
db133cb6 1540 (set 'vc-header-alist cperl-vc-header-alist) ; Avoid warning
f83d2997
KH
1541 (make-local-variable 'font-lock-defaults)
1542 (setq font-lock-defaults
db133cb6
RS
1543 (cond
1544 ((string< emacs-version "19.30")
5efe6a56 1545 '(cperl-font-lock-keywords-2))
db133cb6 1546 ((string< emacs-version "19.33") ; Which one to use?
5efe6a56
SM
1547 '((cperl-font-lock-keywords
1548 cperl-font-lock-keywords-1
1549 cperl-font-lock-keywords-2)))
db133cb6
RS
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)
f83d2997
KH
1555 (if cperl-use-syntax-table-text-property
1556 (progn
029cb4d5 1557 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997 1558 ;; Do not introduce variable if not needed, we check it!
db133cb6
RS
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
5bd52f0e 1563 'font-lock-default-unfontify-region))
029cb4d5 1564 (make-local-variable 'font-lock-unfontify-region-function)
5c8b7eaf 1565 (set 'font-lock-unfontify-region-function
db133cb6 1566 'cperl-font-lock-unfontify-region-function)
029cb4d5 1567 (make-local-variable 'cperl-syntax-done-to)
db133cb6
RS
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
029cb4d5 1572 (make-local-variable 'font-lock-syntactic-keywords)
5c8b7eaf 1573 (setq font-lock-syntactic-keywords
db133cb6
RS
1574 (if cperl-syntaxify-by-font-lock
1575 '(t (cperl-fontify-syntaxically))
1576 '(t)))))
1577 (make-local-variable 'cperl-old-style)
80585273 1578 (set (make-local-variable 'normal-auto-fill-function)
072cb6f9 1579 #'cperl-do-auto-fill)
f83d2997 1580 (if (cperl-enable-font-lock)
5c8b7eaf 1581 (if (cperl-val 'cperl-font-lock)
f83d2997
KH
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)
46c72468 1588 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
f83d2997
KH
1589 (run-hooks 'cperl-mode-hook)
1590 ;; After hooks since fontification will break this
5c8b7eaf 1591 (if cperl-pod-here-scan
5bd52f0e
RS
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)))))
f83d2997
KH
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
f83d2997
KH
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
996e2616 1623 '(memq major-mode '(cperl-mode perl-mode))
f83d2997
KH
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)
5bd52f0e
RS
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
f83d2997
KH
1640
1641(defun cperl-comment-indent ()
5bd52f0e 1642 (let ((p (point)) (c (current-column)) was phony)
f83d2997
KH
1643 (if (looking-at "^#") 0 ; Existing comment at bol stays there.
1644 ;; Wrong comment found
1645 (save-excursion
5bd52f0e
RS
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)))
f83d2997
KH
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]"))
5c8b7eaf 1673;;; (setq target (max (1+ (setq prevc
f83d2997
KH
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.
1697See `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.
1704See `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.
1713If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
1714place (even in empty line), but not after. If after \")\" and the inserted
5c8b7eaf 1715char is \"{\", insert extra newline before only if
f83d2997
KH
1716`cperl-extra-newline-before-brace'."
1717 (interactive "P")
1718 (let (insertpos
1719 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 1720 (cperl-mark-active)
f83d2997 1721 (< (mark) (point)))
5c8b7eaf 1722 (mark)
f83d2997
KH
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))
db133cb6
RS
1736 ;: Check whether we close something "usual" with `}'
1737 (if (and (eq last-command-char ?\})
5c8b7eaf 1738 (not
db133cb6
RS
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) ?\))))
5c8b7eaf 1760 (if cperl-auto-newline
db133cb6
RS
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)
5c8b7eaf 1773 (search-forward (make-string
db133cb6
RS
1774 1 last-command-char))
1775 (setq insertpos (1- (point)))))
1776 (delete-char -1))))
1777 (if insertpos
f83d2997 1778 (save-excursion
db133cb6
RS
1779 (goto-char insertpos)
1780 (self-insert-command (prefix-numeric-value arg)))
1781 (self-insert-command (prefix-numeric-value arg)))))))
f83d2997
KH
1782
1783(defun cperl-electric-lbrace (arg &optional end)
1784 "Insert character, correct line's indentation, correct quoting by space."
1785 (interactive "P")
5c8b7eaf 1786 (let (pos after
f83d2997
KH
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))
5c8b7eaf 1795 (point-marker))
f83d2997
KH
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 ?\ ))
bab27c0c 1803 ;; Check whether we are in comment
5c8b7eaf 1804 (if (and
bab27c0c
RS
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))
f83d2997
KH
1811 (cperl-electric-brace arg)
1812 (and (cperl-val 'cperl-electric-parens)
1813 (eq last-command-char ?{)
5c8b7eaf 1814 (memq last-command-char
f83d2997
KH
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
5c8b7eaf 1827 (cperl-mark-active)
f83d2997
KH
1828 (> (mark) (point)))
1829 (save-excursion
1830 (goto-char (mark))
5c8b7eaf 1831 (point-marker))
f83d2997
KH
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)))
5c8b7eaf 1847 (insert (make-string
f83d2997
KH
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.
1858If 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))
5c8b7eaf 1865 (cperl-mark-active)
f83d2997 1866 (< (mark) (point)))
5c8b7eaf 1867 (mark)
f83d2997
KH
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 ()
db133cb6
RS
1890 "Insert a construction appropriate after a keyword.
1891Help message may be switched off by setting `cperl-message-electric-keyword'
1892to nil."
5c8b7eaf 1893 (let ((beg (save-excursion (beginning-of-line) (point)))
f83d2997
KH
1894 (dollar (and (eq last-command-char ?$)
1895 (eq this-command 'self-insert-command)))
1896 (delete (and (memq last-command-char '(?\ ?\n ?\t ?\f))
db133cb6
RS
1897 (memq this-command '(self-insert-command newline))))
1898 my do)
f83d2997 1899 (and (save-excursion
db133cb6
RS
1900 (condition-case nil
1901 (progn
1902 (backward-sexp 1)
1903 (setq do (looking-at "do\\>")))
1904 (error nil))
f83d2997 1905 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
1906 (save-excursion
1907 (not
f83d2997 1908 (re-search-backward
5bd52f0e 1909 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
1910 beg t)))
1911 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
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))))))
f83d2997 1918 (progn
db133cb6
RS
1919 (and (eq (preceding-char) ?y)
1920 (progn ; "foreachmy"
1921 (forward-char -2)
1922 (insert " ")
1923 (forward-char 2)
5c8b7eaf
SS
1924 (setq my t dollar t
1925 delete
db133cb6 1926 (memq this-command '(self-insert-command newline)))))
f83d2997
KH
1927 (and dollar (insert " $"))
1928 (cperl-indent-line)
1929 ;;(insert " () {\n}")
1930 (cond
1931 (cperl-extra-newline-before-brace
db133cb6 1932 (insert (if do "\n" " ()\n"))
f83d2997
KH
1933 (insert "{")
1934 (cperl-indent-line)
1935 (insert "\n")
1936 (cperl-indent-line)
db133cb6
RS
1937 (insert "\n}")
1938 (and do (insert " while ();")))
f83d2997 1939 (t
db133cb6 1940 (insert (if do " {\n} while ();" " () {\n}")))
f83d2997
KH
1941 )
1942 (or (looking-at "[ \t]\\|$") (insert " "))
1943 (cperl-indent-line)
1944 (if dollar (progn (search-backward "$")
5c8b7eaf 1945 (if my
db133cb6
RS
1946 (forward-char 1)
1947 (delete-char 1)))
f83d2997
KH
1948 (search-backward ")"))
1949 (if delete
db133cb6
RS
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
6c389151
SM
1970 (forward-word -1)
1971 (and
db133cb6
RS
1972 (eq (preceding-char) ?=)
1973 (progn
6c389151
SM
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\\>"))))
db133cb6
RS
1977 (forward-char -1)
1978 (bolp))
5c8b7eaf 1979 (or
5bd52f0e 1980 (get-text-property (point) 'in-pod)
db133cb6
RS
1981 (cperl-after-expr-p nil "{;:")
1982 (and (re-search-backward
6c389151
SM
1983 ;; "\\(\\`\n?\\|\n\n\\)=\\sw+"
1984 "\\(\\`\n?\\|^\n\\)=\\sw+"
1985 (point-min) t)
db133cb6
RS
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
6c389151 1993 (setq notlast (re-search-forward "^\n=" nil t)))
db133cb6
RS
1994 (or notlast
1995 (progn
1996 (insert "\n\n=cut")
1997 (cperl-ensure-newlines 2)
6c389151
SM
1998 (forward-word -2)
1999 (if (and head1
2000 (not
db133cb6
RS
2001 (save-excursion
2002 (forward-char -1)
2003 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
2004 nil t)))) ; Only one
6c389151
SM
2005 (progn
2006 (forward-word 1)
db133cb6
RS
2007 (setq name (file-name-sans-extension
2008 (file-name-nondirectory (buffer-file-name)))
2009 p (point))
5c8b7eaf 2010 (insert " NAME\n\n" name
029cb4d5 2011 " - \n\n=head1 SYNOPSIS\n\n\n\n"
db133cb6
RS
2012 "=head1 DESCRIPTION")
2013 (cperl-ensure-newlines 4)
2014 (goto-char p)
6c389151 2015 (forward-word 2)
db133cb6
RS
2016 (end-of-line)
2017 (setq really-delete t))
6c389151 2018 (forward-word 1))))
db133cb6
RS
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)
6c389151 2026 (forward-word 1)
db133cb6
RS
2027 (end-of-line)
2028 (setq really-delete t)))
2029 (if (and delete really-delete)
f83d2997
KH
2030 (cperl-putback-char cperl-del-back-ch))))))
2031
2032(defun cperl-electric-else ()
db133cb6
RS
2033 "Insert a construction appropriate after a keyword.
2034Help message may be switched off by setting `cperl-message-electric-keyword'
2035to nil."
f83d2997
KH
2036 (let ((beg (save-excursion (beginning-of-line) (point))))
2037 (and (save-excursion
2038 (backward-sexp 1)
2039 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
2040 (save-excursion
2041 (not
f83d2997 2042 (re-search-backward
5bd52f0e 2043 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
2044 beg t)))
2045 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
2046 (looking-at "=cut")
2047 (and cperl-use-syntax-table-text-property
2048 (not (eq (get-text-property (point)
2049 'syntax-type)
2050 'pod)))))
f83d2997
KH
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)
db133cb6
RS
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"))))))
f83d2997
KH
2071
2072(defun cperl-linefeed ()
db133cb6
RS
2073 "Go to end of line, open a new line and indent appropriately.
2074If in POD, insert appropriate lines."
f83d2997
KH
2075 (interactive)
2076 (let ((beg (save-excursion (beginning-of-line) (point)))
2077 (end (save-excursion (end-of-line) (point)))
db133cb6 2078 (pos (point)) start over cut res)
f83d2997 2079 (if (and ; Check if we need to split:
5c8b7eaf 2080 ; i.e., on a boundary and inside "{...}"
f83d2997
KH
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
5c8b7eaf 2091 (eq (car (parse-partial-sexp pos end -1)) -1)
f83d2997
KH
2092 ; Leave the level of parens
2093 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2094 ; Are at end
6c389151 2095 (cperl-after-block-p (point-min))
f83d2997
KH
2096 (progn
2097 (backward-sexp 1)
2098 (setq start (point-marker))
db133cb6 2099 (<= start pos))))) ; Redundant? Are after the
f83d2997
KH
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)))
5c8b7eaf 2129 (forward-line -1) ; We are on the line before target
f83d2997
KH
2130 (end-of-line)
2131 (newline-and-indent))
db133cb6 2132 (end-of-line) ; else - no splitting
f83d2997
KH
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))
db133cb6
RS
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+")
5c8b7eaf 2146 (progn
db133cb6
RS
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))
f83d2997
KH
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)
6c389151
SM
2183 (self-insert-command (prefix-numeric-value arg))
2184 (if cperl-autoindent-on-semi
2185 (cperl-indent-line))))
f83d2997
KH
2186
2187(defun cperl-electric-terminator (arg)
2188 "Insert character and correct line's indentation."
2189 (interactive "P")
5c8b7eaf 2190 (let (insertpos (end (point))
f83d2997
KH
2191 (auto (and cperl-auto-newline
2192 (or (not (eq last-command-char ?:))
2193 cperl-auto-newline-after-colon))))
5c8b7eaf 2194 (if (and ;;(not arg)
f83d2997
KH
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)))
f83d2997
KH
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)
5c8b7eaf 2237 "Backspace-untabify, or remove the whitespace around the point inserted
db133cb6 2238by an electric key."
f83d2997 2239 (interactive "p")
5c8b7eaf
SS
2240 (if (and cperl-auto-newline
2241 (memq last-command '(cperl-electric-semi
f83d2997
KH
2242 cperl-electric-terminator
2243 cperl-electric-lbrace))
2244 (memq (preceding-char) '(?\ ?\t ?\n)))
2245 (let (p)
5c8b7eaf 2246 (if (eq last-command 'cperl-electric-lbrace)
f83d2997
KH
2247 (skip-chars-forward " \t\n"))
2248 (setq p (point))
2249 (skip-chars-backward " \t\n")
2250 (delete-region (point) p))
db133cb6
RS
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))
5c8b7eaf 2254 (if (and cperl-auto-newline
db133cb6
RS
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))))
f83d2997
KH
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.
5c8b7eaf 2276If `cperl-tab-always-indent' is non-nil (the default), always indent current
db133cb6 2277line. Otherwise, indent the current line only if point is at the left margin
f83d2997
KH
2278or in the line's indentation; otherwise insert a tab.
2279
2280A numeric argument, regardless of its value,
2281means indent rigidly all the lines of the expression starting after point
2282so that this line becomes properly indented.
2283The relative indentation among the lines of the expression are preserved."
2284 (interactive "P")
5bd52f0e 2285 (cperl-update-syntaxification (point) (point))
f83d2997
KH
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)))
db133cb6 2300 (if (and shift-amt (> end beg))
f83d2997
KH
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
5bd52f0e 2309(defun cperl-indent-line (&optional parse-data)
f83d2997
KH
2310 "Indent current line as Perl code.
2311Return the amount the indentation changed by."
db133cb6 2312 (let (indent i beg shift-amt
f83d2997
KH
2313 (case-fold-search nil)
2314 (pos (- (point-max) (point))))
5bd52f0e 2315 (setq indent (cperl-calculate-indent parse-data)
db133cb6 2316 i indent)
f83d2997
KH
2317 (beginning-of-line)
2318 (setq beg (point))
2319 (cond ((or (eq indent nil) (eq indent t))
db133cb6 2320 (setq indent (current-indentation) i nil))
f83d2997
KH
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")
db133cb6
RS
2339 (setq shift-amt (and i (- indent (current-column))))
2340 (if (or (not shift-amt)
2341 (zerop shift-amt))
f83d2997
KH
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)
5bd52f0e
RS
2362 ;; returns list (START STATE DEPTH PRESTART),
2363 ;; START is a good place to start parsing, or equal to
5c8b7eaf 2364 ;; PARSE-START if preset,
5bd52f0e
RS
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.
f83d2997
KH
2369 (save-excursion
2370 (let ((start-point (point)) depth state start prestart)
5bd52f0e
RS
2371 (if (and parse-start
2372 (<= parse-start start-point))
f83d2997 2373 (goto-char parse-start)
5bd52f0e
RS
2374 (beginning-of-defun)
2375 (setq start-state nil))
f83d2997
KH
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))
f83d2997
KH
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))
f83d2997
KH
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
5bd52f0e 2404 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
f83d2997
KH
2405 (progn
2406 (skip-chars-backward " \t\n\f")
2407 (and (memq (char-syntax (preceding-char)) '(?w ?_))
2408 (progn
2409 (backward-sexp)
5c8b7eaf 2410 (looking-at
f83d2997
KH
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
5bd52f0e 2415(defun cperl-calculate-indent (&optional parse-data) ; was parse-start
f83d2997
KH
2416 "Return appropriate indentation for current line as Perl code.
2417In usual case returns an integer: the column to indent to.
5bd52f0e
RS
2418Returns nil if line starts inside a string, t if in a comment.
2419
2420Will not correct the indentation for labels, but will correct it for braces
2421and closing parentheses and brackets.."
f83d2997
KH
2422 (save-excursion
2423 (if (or
6c389151
SM
2424 (and (memq (get-text-property (point) 'syntax-type)
2425 '(pod here-doc here-doc-delim format))
2426 (not (get-text-property (point) 'indentable)))
f83d2997
KH
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))
6c389151 2440 p prop look-prop is-block delim)
f83d2997 2441 (cond
5c8b7eaf 2442 (in-pod
f83d2997
KH
2443 ;; In the verbatim part, probably code example. What to do???
2444 )
5c8b7eaf 2445 (t
f83d2997
KH
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
5c8b7eaf 2455 (goto-char (or (previous-single-property-change p look-prop)
f83d2997
KH
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)
5bd52f0e 2461 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
5c8b7eaf 2462 (start (or (nth 2 parse-data)
5bd52f0e 2463 (nth 0 s-s)))
f83d2997
KH
2464 (state (nth 1 s-s))
2465 (containing-sexp (car (cdr state)))
f83d2997 2466 old-indent)
5c8b7eaf 2467 (if (and
5bd52f0e 2468 ;;containing-sexp ;; We are buggy at toplevel :-(
5c8b7eaf 2469 parse-data)
5bd52f0e
RS
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))))
6c389151
SM
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))
f83d2997
KH
2486 ;; return nil or t if should not change this line
2487 (nth 4 state))
6c389151 2488 ;; XXXX Do we need to special-case this?
f83d2997
KH
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")
5bd52f0e
RS
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)
f83d2997 2501 (progn
5bd52f0e 2502 (cperl-backward-to-noncomment (or old-indent (point-min)))
f83d2997
KH
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)
5bd52f0e 2508 (eq (point) old-indent) ; old-indent was at comment
db133cb6
RS
2509 (eq (preceding-char) ?\;)
2510 ;; Had ?\) too
2511 (and (eq (preceding-char) ?\})
5bd52f0e
RS
2512 (cperl-after-block-and-statement-beg
2513 (point-min))) ; Was start - too close
f83d2997
KH
2514 (memq char-after (append ")]}" nil))
2515 (and (eq (preceding-char) ?\:) ; label
2516 (progn
2517 (forward-sexp -1)
2518 (skip-chars-backward " \t")
5c8b7eaf 2519 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:"))))
5bd52f0e
RS
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)
f83d2997 2526 cperl-continued-statement-offset))))
6c389151
SM
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,
f83d2997
KH
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))
6c389151
SM
2547 (or (memq char-after
2548 (append (if delim "}" ")]}") nil))
f83d2997
KH
2549 (looking-at "[ \t]*\\(#\\|$\\)")
2550 (skip-chars-forward " \t"))
6c389151
SM
2551 (+ (current-column)
2552 (if (and delim
2553 (eq char-after ?\}))
2554 ;; Correct indentation of trailing ?\}
2555 (+ cperl-indent-level cperl-close-paren-offset)
f83d2997 2556 0)))
6c389151
SM
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)))
f83d2997
KH
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.
5bd52f0e
RS
2587 ;; (Had \, too)
2588 (while ;;(or (eq (preceding-char) ?\,)
f83d2997
KH
2589 (and (eq (preceding-char) ?:)
2590 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2591 (memq (char-syntax (char-after (- (point) 2)))
5bd52f0e
RS
2592 '(?w ?_))))
2593 ;;)
f83d2997
KH
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.
6c389151
SM
2601 (if (not (or (eq (1- (point)) containing-sexp)
2602 (memq (preceding-char)
2603 (append (if is-block " ;{" " ,;{") '(nil)))
db133cb6 2604 (and (eq (preceding-char) ?\})
6c389151
SM
2605 (cperl-after-block-and-statement-beg
2606 containing-sexp))))
f83d2997
KH
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.
5bd52f0e
RS
2610 ;;
2611 ;; There might be a label on this line, just
2612 ;; consider it bad style and ignore it.
f83d2997
KH
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)
6c389151
SM
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))
5bd52f0e
RS
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
5c8b7eaf 2628 ;; be quick anyway (this comment comes
5bd52f0e
RS
2629 ;;from different location):
2630 (cperl-calculate-indent))
2631 (current-column))
f83d2997
KH
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
5c8b7eaf 2661 (if (> (current-indentation)
f83d2997
KH
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)
6c389151
SM
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))
f83d2997
KH
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
110c171f 2697 ;; respect to `sub', not with respect to the
f83d2997
KH
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\\>"))
5c8b7eaf
SS
2706 (setq old-indent
2707 (nth 1
2708 (parse-partial-sexp
2709 (save-excursion (beginning-of-line) (point))
f83d2997
KH
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)
5bd52f0e
RS
2721 ;; Do not move `parse-data', this should
2722 ;; be quick anyway:
2723 (cperl-calculate-indent))
f83d2997
KH
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.
2734The values mean:
2735 nil: do not indent;
db133cb6
RS
2736 number: add this amount of indentation.
2737
2738Not finished, not used.")
f83d2997
KH
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.
db133cb6
RS
2743POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'.
2744
2745Not finished, not used."
f83d2997
KH
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)
5c8b7eaf 2760 ;; Line is at top level.
f83d2997
KH
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)))
5c8b7eaf 2772 (t
f83d2997
KH
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 ":"))))
5c8b7eaf 2842 ;; Now at the point, after label, or at start
f83d2997
KH
2843 ;; of first statement in the block.
2844 (and (< (point) start-point)
5c8b7eaf 2845 (if (> colon-line-end (point))
f83d2997 2846 ;; Before statement after label
5c8b7eaf 2847 (if (> (current-indentation)
f83d2997
KH
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)
5bd52f0e 2885 (cperl-calculate-indent))
f83d2997
KH
2886 (current-indentation))))))))
2887 res)))
2888
2889(defun cperl-calculate-indent-within-comment ()
2890 "Return the indentation amount for line, assuming that
2891the 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 ()
029cb4d5 2906 "Go to position before comment on the current line, or to end of line.
f83d2997
KH
2907Returns true if comment is found."
2908 (let (state stop-in cpoint (lim (progn (end-of-line) (point))))
2909 (beginning-of-line)
5c8b7eaf 2910 (if (or
f83d2997
KH
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
f83d2997
KH
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)))
5bd52f0e 2933 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
f83d2997
KH
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
f83d2997
KH
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
5bd52f0e 2966(defun cperl-commentify (bb e string &optional noface)
5c8b7eaf 2967 (if cperl-use-syntax-table-text-property
5bd52f0e
RS
2968 (if (eq noface 'n) ; Only immediate
2969 nil
f83d2997
KH
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))
6c389151
SM
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))
f83d2997 2977 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
5c8b7eaf 2978 (put-text-property (1+ bb) (1- e)
f83d2997 2979 'syntax-table cperl-string-syntax-table))
5bd52f0e
RS
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)))))
6c389151 2986
5bd52f0e
RS
2987(defvar cperl-starters '(( ?\( . ?\) )
2988 ( ?\[ . ?\] )
2989 ( ?\{ . ?\} )
2990 ( ?\< . ?\> )))
f83d2997
KH
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
6c389151 2996 (let (b starter ender st i i2 go-forward reset-st)
f83d2997
KH
2997 (skip-chars-forward " \t")
2998 ;; ender means matching-char matcher.
5c8b7eaf 2999 (setq b (point)
5bd52f0e
RS
3000 starter (if (eobp) 0 (char-after b))
3001 ender (cdr (assoc starter cperl-starters)))
f83d2997
KH
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
5bd52f0e
RS
3023 ;; We use `$' syntax class to find matching stuff, but $$
3024 ;; is recognized the same as $, so we need to check this manually.
f83d2997
KH
3025 (if (and (eq starter (char-after (cperl-1+ b)))
3026 (not ender))
3027 ;; $ has TeXish matching rules, so $$ equiv $...
3028 (forward-char 2)
6c389151 3029 (setq reset-st (syntax-table))
f83d2997
KH
3030 (set-syntax-table st)
3031 (forward-sexp 1)
6c389151
SM
3032 (if (<= (point) (1+ b))
3033 (error "Unfinished regular expression"))
3034 (set-syntax-table reset-st)
3035 (setq reset-st nil)
f83d2997
KH
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)))
5bd52f0e 3044 ;; Now we are after the first part.
f83d2997
KH
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)
5c8b7eaf 3065 (if ender (modify-syntax-entry ender "." st))
f83d2997 3066 (setq set-st nil)
5bd52f0e
RS
3067 (setq ender (cperl-forward-re lim end nil t st-l err-l
3068 argument starter ender)
f83d2997
KH
3069 ender (nth 2 ender)))))
3070 (error (goto-char lim)
3071 (setq set-st nil)
6c389151
SM
3072 (if reset-st
3073 (set-syntax-table reset-st))
f83d2997
KH
3074 (or end
3075 (message
5bd52f0e 3076 "End of `%s%s%c ... %c' string/RE not found: %s"
f83d2997
KH
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))))
5bd52f0e
RS
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
5efe6a56 3089 ;; go-forward: has 2 args, and the second part is empty
f83d2997
KH
3090 (list i i2 ender starter go-forward)))
3091
5c8b7eaf 3092(defsubst cperl-postpone-fontification (b e type val &optional now)
5bd52f0e
RS
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:
5c8b7eaf 3100;; a) PODs:
5bd52f0e
RS
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'
5c8b7eaf 3104;; b) HEREs:
5bd52f0e
RS
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'
5c8b7eaf 3108;; c) FORMATs:
5bd52f0e 3109;; After-initial-line--to-end is marked `syntax-type' ==> `format'
5c8b7eaf 3110;; d) 'Q'uoted string:
5bd52f0e 3111;; part between markers inclusive is marked `syntax-type' ==> `string'
6c389151 3112;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
5bd52f0e
RS
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))))
6c389151
SM
3129 ;; Skip empty lines
3130 (and (looking-at "\n*=")
3131 (/= 0 (skip-chars-backward "\n"))
3132 (forward-char))
3133 (setq pos (point))
5bd52f0e
RS
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
6c389151
SM
3142(defvar cperl-nonoverridable-face)
3143(defvar font-lock-function-name-face)
3144(defvar font-lock-comment-face)
3145
db133cb6 3146(defun cperl-find-pods-heres (&optional min max non-inter end ignore-max)
f83d2997 3147 "Scans the buffer for hard-to-parse Perl constructions.
5c8b7eaf
SS
3148If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3149the sections using `cperl-pod-head-face', `cperl-pod-face',
f83d2997
KH
3150`cperl-here-face'."
3151 (interactive)
db133cb6
RS
3152 (or min (setq min (point-min)
3153 cperl-syntax-state nil
3154 cperl-syntax-done-to min))
f83d2997 3155 (or max (setq max (point-max)))
5bd52f0e 3156 (let* (face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
6c389151
SM
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
db133cb6
RS
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)))
6c389151
SM
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))
db133cb6
RS
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))
5bd52f0e
RS
3175 (font-lock-constant-face (if (boundp 'font-lock-constant-face)
3176 font-lock-constant-face
3177 'font-lock-constant-face))
5c8b7eaf 3178 (font-lock-function-name-face
5bd52f0e
RS
3179 (if (boundp 'font-lock-function-name-face)
3180 font-lock-function-name-face
3181 'font-lock-function-name-face))
6c389151
SM
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
5bd52f0e
RS
3187 (if (boundp 'cperl-nonoverridable-face)
3188 cperl-nonoverridable-face
3189 'cperl-nonoverridable-face))
5c8b7eaf 3190 (stop-point (if ignore-max
db133cb6
RS
3191 (point-max)
3192 max))
3193 (search
3194 (concat
6c389151 3195 "\\(\\`\n?\\|^\n\\)="
db133cb6
RS
3196 "\\|"
3197 ;; One extra () before this:
5c8b7eaf 3198 "<<"
5bd52f0e 3199 "\\(" ; 1 + 1
db133cb6 3200 ;; First variant "BLAH" or just ``.
6c389151
SM
3201 "[ \t]*" ; Yes, whitespace is allowed!
3202 "\\([\"'`]\\)" ; 2 + 1 = 3
5bd52f0e 3203 "\\([^\"'`\n]*\\)" ; 3 + 1
db133cb6
RS
3204 "\\3"
3205 "\\|"
5bd52f0e
RS
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
db133cb6
RS
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:
5bd52f0e 3219 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
db133cb6
RS
3220 "\\|"
3221 ;; 1+6+2+1=10 extra () before this:
5bd52f0e 3222 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
db133cb6
RS
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 "\\|"
6c389151
SM
3234 "__\\(END\\|DATA\\)__"
3235 ;; 1+6+2+1+1+2+1+1+1=16 extra () before this:
3236 "\\|"
3237 "\\\\\\(['`\"]\\)"
db133cb6
RS
3238 )
3239 ""))))
f83d2997
KH
3240 (unwind-protect
3241 (progn
3242 (save-excursion
3243 (or non-inter
3244 (message "Scanning for \"hard\" Perl constructions..."))
db133cb6 3245 (and cperl-pod-here-fontify
f83d2997
KH
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))
5c8b7eaf 3250 (remove-text-properties min max
5bd52f0e 3251 '(syntax-type t in-pod t syntax-table t
6c389151
SM
3252 cperl-postpone t
3253 syntax-subtype t
3254 rear-nonsticky t
3255 indentable t))
f83d2997
KH
3256 ;; Need to remove face as well...
3257 (goto-char min)
db133cb6
RS
3258 (and (eq system-type 'emx)
3259 (looking-at "extproc[ \t]") ; Analogue of #!
5c8b7eaf 3260 (cperl-commentify min
db133cb6
RS
3261 (save-excursion (end-of-line) (point))
3262 nil))
3263 (while (and
3264 (< (point) max)
3265 (re-search-forward search max t))
5bd52f0e 3266 (setq tmpend nil) ; Valid for most cases
5c8b7eaf 3267 (cond
f83d2997 3268 ((match-beginning 1) ; POD section
6c389151
SM
3269 ;; "\\(\\`\n?\\|^\n\\)="
3270 (if (looking-at "cut\\>")
5bd52f0e
RS
3271 (if ignore-max
3272 nil ; Doing a chunk only
f83d2997
KH
3273 (message "=cut is not preceded by a POD section")
3274 (or (car err-l) (setcar err-l (point))))
3275 (beginning-of-line)
5c8b7eaf
SS
3276
3277 (setq b (point)
5bd52f0e
RS
3278 bb b
3279 tb (match-beginning 0)
3280 b1 nil) ; error condition
db133cb6
RS
3281 ;; We do not search to max, since we may be called from
3282 ;; some hook of fontification, and max is random
6c389151 3283 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
f83d2997 3284 (progn
6c389151
SM
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))))))
f83d2997
KH
3291 (beginning-of-line 2) ; An empty line after =cut is not POD!
3292 (setq e (point))
db133cb6 3293 (and (> e max)
6c389151
SM
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)))
f83d2997 3302 (put-text-property b e 'in-pod t)
6c389151 3303 (put-text-property b e 'syntax-type 'in-pod)
f83d2997
KH
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)
5efe6a56
SM
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))
f83d2997
KH
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)
5efe6a56
SM
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)
6c389151
SM
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
5efe6a56
SM
3335 (match-beginning 1) (match-end 1)
3336 'face head-face))))
f83d2997
KH
3337 (cperl-commentify bb e nil)
3338 (goto-char e)
3339 (or (eq e (point-max))
6c389151 3340 (forward-char -1)))) ; Prepare for immediate pod start.
f83d2997
KH
3341 ;; Here document
3342 ;; We do only one here-per-line
5bd52f0e 3343 ;; ;; One extra () before this:
5c8b7eaf 3344 ;;"<<"
5bd52f0e
RS
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 ;; "\\)"
f83d2997
KH
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)
5bd52f0e
RS
3361 state-point b
3362 tb (match-beginning 0)
3363 i (or (nth 3 state) (nth 4 state)))
5c8b7eaf 3364 (if i
5bd52f0e
RS
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.
f83d2997
KH
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))
5c8b7eaf 3380 (cond (cperl-pod-here-fontify
5bd52f0e
RS
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)))
f83d2997
KH
3384 (forward-line)
3385 (setq b (point))
db133cb6
RS
3386 ;; We do not search to max, since we may be called from
3387 ;; some hook of fontification, and max is random
5c8b7eaf 3388 (cond ((re-search-forward (concat "^" qtag "$")
db133cb6 3389 stop-point 'toend)
5c8b7eaf 3390 (if cperl-pod-here-fontify
f83d2997 3391 (progn
5bd52f0e 3392 ;; Highlight the ending delimiter
5c8b7eaf 3393 (cperl-postpone-fontification (match-beginning 0) (match-end 0)
883212ce 3394 'face font-lock-constant-face)
5bd52f0e
RS
3395 (cperl-put-do-not-fontify b (match-end 0) t)
3396 ;; Highlight the HERE-DOC
5c8b7eaf 3397 (cperl-postpone-fontification b (match-beginning 0)
f83d2997
KH
3398 'face here-face)))
3399 (setq e1 (cperl-1+ (match-end 0)))
5c8b7eaf 3400 (put-text-property b (match-beginning 0)
f83d2997
KH
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)
5bd52f0e
RS
3407 (cperl-put-do-not-fontify b (match-end 0) t)
3408 (if (> e1 max)
3409 (setq tmpend tb)))
f83d2997
KH
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
5bd52f0e
RS
3420 "")
3421 tb (match-beginning 0))
f83d2997 3422 (setq argument nil)
5c8b7eaf 3423 (if cperl-pod-here-fontify
f83d2997
KH
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
5c8b7eaf 3429 (looking-at "^[ \t]*{"))
f83d2997
KH
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)
5bd52f0e 3438 ;; Highlight the format line
5c8b7eaf 3439 (cperl-postpone-fontification b1 (point)
f83d2997
KH
3440 'face font-lock-string-face)
3441 (cperl-commentify b1 (point) nil)
5bd52f0e 3442 (cperl-put-do-not-fontify b1 (point) t))))
db133cb6
RS
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))
f83d2997 3446 (beginning-of-line)
5bd52f0e 3447 (if (looking-at "^\\.$") ; ";" is not supported yet
f83d2997 3448 (progn
5bd52f0e
RS
3449 ;; Highlight the ending delimiter
3450 (cperl-postpone-fontification (point) (+ (point) 2)
f83d2997
KH
3451 'face font-lock-string-face)
3452 (cperl-commentify (point) (+ (point) 2) nil)
5bd52f0e 3453 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
f83d2997
KH
3454 (message "End of format `%s' not found." name)
3455 (or (car err-l) (setcar err-l b)))
3456 (forward-line)
5bd52f0e
RS
3457 (if (> (point) max)
3458 (setq tmpend tb))
db133cb6 3459 (put-text-property b (point) 'syntax-type 'format))
f83d2997
KH
3460 ;; Regexp:
3461 ((or (match-beginning 10) (match-beginning 11))
3462 ;; 1+6+2=9 extra () before this:
5bd52f0e 3463 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
f83d2997 3464 ;; "\\|"
5bd52f0e 3465 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
f83d2997
KH
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
5bd52f0e
RS
3473 ;; bb == "Not a stringy"
3474 bb (if (eq b1 10) ; user variables/whatever
6c389151
SM
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))
5bd52f0e
RS
3481 ?\&))))
3482 ;; <file> or <$file>
3483 (and (eq c ?\<)
6c389151 3484 ;; Do not stringify <FH>, <$fh> :
5bd52f0e 3485 (save-match-data
5c8b7eaf 3486 (looking-at
6c389151 3487 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
5bd52f0e 3488 tb (match-beginning 0))
db133cb6
RS
3489 (goto-char (match-beginning b1))
3490 (cperl-backward-to-noncomment (point-min))
f83d2997 3491 (or bb
5bd52f0e 3492 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
f83d2997 3493 (setq argument ""
db133cb6
RS
3494 bb ; Not a regexp?
3495 (progn
5c8b7eaf 3496 (not
db133cb6
RS
3497 ;; What is below: regexp-p?
3498 (and
3499 (or (memq (preceding-char)
5bd52f0e 3500 (append (if (memq c '(?\? ?\<))
db133cb6 3501 ;; $a++ ? 1 : 2
5bd52f0e
RS
3502 "~{(=|&*!,;:"
3503 "~{(=|&+-*!,;:") nil))
db133cb6
RS
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)
6c389151
SM
3509;; After these keywords `/' starts a RE. One should add all the
3510;; functions/builtins which expect an argument, but ...
5bd52f0e
RS
3511 (if (eq (preceding-char) ?-)
3512 ;; -d ?foo? is a RE
3513 (looking-at "[a-zA-Z]\\>")
029cb4d5
SM
3514 (and
3515 (not (memq (preceding-char)
3516 '(?$ ?@ ?& ?%)))
3517 (looking-at
3518 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
db133cb6
RS
3519 (and (eq (preceding-char) ?.)
3520 (eq (char-after (- (point) 2)) ?.))
3521 (bobp))
3522 ;; m|blah| ? foo : bar;
3523 (not
3524 (and (eq c ?\?)
5c8b7eaf 3525 cperl-use-syntax-table-text-property
db133cb6
RS
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))))
5c8b7eaf 3537 (or bb (setq state (parse-partial-sexp
f83d2997
KH
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)
6c389151 3543 ;; Skip whitespace and comments...
f83d2997
KH
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"))
6c389151
SM
3547 (if (> (point) b)
3548 (put-text-property b (point) 'syntax-type 'prestring))
f83d2997
KH
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)
5bd52f0e
RS
3553 ;; has 2 args
3554 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
db133cb6
RS
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
5bd52f0e 3558 i2
db133cb6 3559 t st-l err-l argument)
5bd52f0e
RS
3560 ;; Note that if `go', then it is considered as 1-arg
3561 b1 (nth 1 i) ; start of the second part
5c8b7eaf 3562 tag (nth 2 i) ; ender-char, true if second part
5bd52f0e 3563 ; is with matching chars []
f83d2997
KH
3564 go (nth 4 i) ; There is a 1-char part after the end
3565 i (car i) ; intermediate point
5c8b7eaf 3566 e1 (point) ; end
5bd52f0e 3567 ;; Before end of the second part if non-matching: ///
5c8b7eaf 3568 tail (if (and i (not tag))
5bd52f0e
RS
3569 (1- e1))
3570 e (if i i e1) ; end of the first part
6c389151
SM
3571 qtag nil ; need to preserve backslashitis
3572 is-x-REx nil) ; REx has //x modifier
f83d2997
KH
3573 ;; Commenting \\ is dangerous, what about ( ?
3574 (and i tail
3575 (eq (char-after i) ?\\)
5bd52f0e 3576 (setq qtag t))
6c389151
SM
3577 (if (looking-at "\\sw*x") ; qr//x
3578 (setq is-x-REx t))
f83d2997 3579 (if (null i)
5bd52f0e 3580 ;; Considered as 1arg form
f83d2997
KH
3581 (progn
3582 (cperl-commentify b (point) t)
5bd52f0e 3583 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
3584 (if (or is-x-REx
3585 ;; ignore other text properties:
3586 (string-match "^qw$" argument))
3587 (put-text-property b (point) 'indentable t))
5bd52f0e
RS
3588 (and go
3589 (setq e1 (cperl-1+ e1))
3590 (or (eobp)
3591 (forward-char 1))))
f83d2997
KH
3592 (cperl-commentify b i t)
3593 (if (looking-at "\\sw*e") ; s///e
3594 (progn
3595 (and
3596 ;; silent:
5bd52f0e 3597 (cperl-find-pods-heres b1 (1- (point)) t end)
f83d2997
KH
3598 ;; Error
3599 (goto-char (1+ max)))
5bd52f0e 3600 (if (and tag (eq (preceding-char) ?\>))
f83d2997
KH
3601 (progn
3602 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
5bd52f0e 3603 (cperl-modify-syntax-type i cperl-st-bra)))
6c389151
SM
3604 (put-text-property b i 'syntax-type 'string)
3605 (if is-x-REx
3606 (put-text-property b i 'indentable t)))
5bd52f0e
RS
3607 (cperl-commentify b1 (point) t)
3608 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
3609 (if is-x-REx
3610 (put-text-property b i 'indentable t))
5bd52f0e 3611 (if qtag
db133cb6 3612 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
f83d2997 3613 (setq tail nil)))
5bd52f0e 3614 ;; Now: tail: if the second part is non-matching without ///e
f83d2997
KH
3615 (if (eq (char-syntax (following-char)) ?w)
3616 (progn
3617 (forward-word 1) ; skip modifiers s///s
5bd52f0e 3618 (if tail (cperl-commentify tail (point) t))
6c389151
SM
3619 (cperl-postpone-fontification
3620 e1 (point) 'face 'cperl-nonoverridable-face)))
5bd52f0e
RS
3621 ;; Check whether it is m// which means "previous match"
3622 ;; and highlight differently
6c389151
SM
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))
5bd52f0e
RS
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))))
5c8b7eaf 3637 (cperl-postpone-fontification
5bd52f0e
RS
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
5c8b7eaf 3646 (cperl-postpone-fontification
5bd52f0e 3647 b (cperl-1+ b) 'face font-lock-constant-face)
5c8b7eaf 3648 (cperl-postpone-fontification
6c389151
SM
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)))
5bd52f0e
RS
3699 (if i2
3700 (progn
5c8b7eaf 3701 (cperl-postpone-fontification
5bd52f0e
RS
3702 (1- e1) e1 'face font-lock-constant-face)
3703 (if (assoc (char-after b) cperl-starters)
5c8b7eaf 3704 (cperl-postpone-fontification
5bd52f0e
RS
3705 b1 (1+ b1) 'face font-lock-constant-face))))
3706 (if (> (point) max)
3707 (setq tmpend tb))))
f83d2997
KH
3708 ((match-beginning 13) ; sub with prototypes
3709 (setq b (match-beginning 0))
3710 (if (memq (char-after (1- b))
3711 '(?\$ ?\@ ?\% ?\& ?\*))
3712 nil
5c8b7eaf 3713 (setq state (parse-partial-sexp
5bd52f0e
RS
3714 state-point b nil nil state)
3715 state-point b)
f83d2997
KH
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)
db133cb6 3724 (eq (preceding-char) ?\')) ; $'
f83d2997 3725 (setq b (1- (point))
5c8b7eaf 3726 state (parse-partial-sexp
f83d2997
KH
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)
5c8b7eaf 3742 state (parse-partial-sexp
f83d2997
KH
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\\)__"
6c389151 3751 ((match-beginning 16) ; __END__, __DATA__
f83d2997
KH
3752 (setq bb (match-end 0)
3753 b (match-beginning 0)
5c8b7eaf 3754 state (parse-partial-sexp
f83d2997
KH
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))
6c389151
SM
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")))
db133cb6 3777 (if (> (point) stop-point)
f83d2997 3778 (progn
5c8b7eaf 3779 (if end
f83d2997
KH
3780 (message "Garbage after __END__/__DATA__ ignored")
3781 (message "Unbalanced syntax found while scanning")
3782 (or (car err-l) (setcar err-l b)))
db133cb6
RS
3783 (goto-char stop-point))))
3784 (setq cperl-syntax-state (cons state-point state)
5bd52f0e 3785 cperl-syntax-done-to (or tmpend (max (point) max))))
f83d2997 3786 (if (car err-l) (goto-char (car err-l))
db133cb6
RS
3787 (or non-inter
3788 (message "Scanning for \"hard\" Perl constructions... done"))))
f83d2997
KH
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
5bd52f0e 3797 (let (stop p pr)
f83d2997
KH
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)
5bd52f0e
RS
3802 (if (memq (setq pr (get-text-property (point) 'syntax-type))
3803 '(pod here-doc here-doc-delim))
3804 (cperl-unwind-to-safe nil)
f83d2997
KH
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))
5bd52f0e 3811 (setq stop t))))))
f83d2997
KH
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)
bab27c0c
RS
3820 (or (eq (point) lim)
3821 (eq (preceding-char) ?\) ) ; if () {} sub f () {}
db133cb6
RS
3822 (if (eq (char-syntax (preceding-char)) ?w) ; else {}
3823 (save-excursion
3824 (forward-sexp -1)
6c389151 3825 (or (looking-at "\\(else\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
db133cb6
RS
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))))
f83d2997
KH
3834 (error nil))))
3835
3836(defun cperl-after-expr-p (&optional lim chars test)
029cb4d5 3837 "Return true if the position is good for start of expression.
f83d2997
KH
3838TEST is the expression to evaluate at the found position. If absent,
3839CHARS is a string that contains good characters to have before us (however,
3840`}' is treated \"smartly\" if it is not in the list)."
5c8b7eaf 3841 (let (stop p
f83d2997
KH
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
5bd52f0e 3849 ;; Else: last iteration, or a label
5c8b7eaf 3850 (cperl-to-comment-or-eol)
f83d2997
KH
3851 (skip-chars-backward " \t")
3852 (if (< p (point)) (goto-char p))
5bd52f0e
RS
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))))
bab27c0c
RS
3862 (or (bobp) ; ???? Needed
3863 (eq (point) lim)
029cb4d5 3864 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
f83d2997
KH
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
db133cb6
RS
3879(defun cperl-after-block-and-statement-beg (lim)
3880 ;; We assume that we are after ?\}
5c8b7eaf 3881 (and
db133cb6
RS
3882 (cperl-after-block-p lim)
3883 (save-excursion
3884 (forward-sexp -1)
3885 (cperl-backward-to-noncomment (point-min))
3886 (or (bobp)
bab27c0c 3887 (eq (point) lim)
db133cb6
RS
3888 (not (= (char-syntax (preceding-char)) ?w))
3889 (progn
3890 (forward-sexp -1)
5c8b7eaf 3891 (not
db133cb6
RS
3892 (looking-at
3893 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
3894
f83d2997 3895\f
f83d2997
KH
3896(defun cperl-indent-exp ()
3897 "Simple variant of indentation of continued-sexp.
5bd52f0e
RS
3898
3899Will not indent comment if it starts at `comment-indent' or looks like
3900continuation of the comment on the previous line.
db133cb6 3901
5c8b7eaf 3902If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 3903conditional/loop constructs."
f83d2997
KH
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)))
5bd52f0e
RS
3918 (if (> (point) tmp-end)
3919 (save-excursion
3920 (end-of-line)
3921 (setq tmp-end (point)))
f83d2997
KH
3922 (setq done t)))
3923 (goto-char tmp-end)
3924 (setq tmp-end (point-marker)))
db133cb6
RS
3925 (if cperl-indent-region-fix-constructs
3926 (cperl-fix-line-spacing tmp-end))
f83d2997
KH
3927 (cperl-indent-region (point) tmp-end))))
3928
5bd52f0e
RS
3929(defun cperl-fix-line-spacing (&optional end parse-data)
3930 "Improve whitespace in a conditional/loop construct.
3931Returns some position at the last line."
db133cb6
RS
3932 (interactive)
3933 (or end
3934 (setq end (point-max)))
5bd52f0e
RS
3935 (let (p pp ml have-brace ret
3936 (ee (save-excursion (end-of-line) (point)))
db133cb6
RS
3937 (cperl-indent-region-fix-constructs
3938 (or cperl-indent-region-fix-constructs 1)))
3939 (save-excursion
3940 (beginning-of-line)
5bd52f0e 3941 (setq ret (point))
5c8b7eaf 3942 ;; }? continue
5bd52f0e 3943 ;; blah; }
5c8b7eaf 3944 (if (not
5bd52f0e
RS
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
db133cb6 3948 ;; Looking at:
5c8b7eaf 3949 ;; }
db133cb6
RS
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 {
5c8b7eaf 3971 (if (looking-at
5bd52f0e 3972 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
db133cb6
RS
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
6c389151
SM
3980 (if (looking-at
3981 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
db133cb6
RS
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 (
6c389151
SM
3989 (if (looking-at
3990 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
db133cb6
RS
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 () {
6c389151 3999 (if (looking-at
ce22dd53 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]*{")
db133cb6
RS
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
5c8b7eaf 4022 (cond
db133cb6
RS
4023 ((bolp) ; Were before `{', no if/else/etc
4024 nil)
4025 ((looking-at "\\(\t*\\| [ \t]+\\){")
4026 (delete-horizontal-space)
5c8b7eaf 4027 (if (if ml
db133cb6
RS
4028 cperl-extra-newline-before-brace-multiline
4029 cperl-extra-newline-before-brace)
4030 (progn
4031 (delete-horizontal-space)
4032 (insert "\n")
5bd52f0e
RS
4033 (setq ret (point))
4034 (if (cperl-indent-line parse-data)
5c8b7eaf 4035 (progn
5bd52f0e
RS
4036 (cperl-fix-line-spacing end parse-data)
4037 (setq ret (point)))))
db133cb6
RS
4038 (insert
4039 (make-string cperl-indent-region-fix-constructs ?\ ))))
4040 ((and (looking-at "[ \t]*\n")
5c8b7eaf 4041 (not (if ml
db133cb6
RS
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")
5bd52f0e
RS
4064 (setq ret (point))
4065 (if (cperl-indent-line parse-data)
4066 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
db133cb6 4067 (beginning-of-line)
5bd52f0e 4068 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
db133cb6
RS
4069 ;; Now check whether there is a hanging `}'
4070 ;; Looking at:
4071 ;; } blah
5c8b7eaf 4072 (if (and
db133cb6 4073 cperl-fix-hanging-brace-when-indent
5bd52f0e 4074 have-brace
db133cb6
RS
4075 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
4076 (condition-case nil
4077 (progn
4078 (up-list 1)
5c8b7eaf 4079 (if (and (<= (point) pp)
db133cb6 4080 (eq (preceding-char) ?\} )
5c8b7eaf 4081 (cperl-after-block-and-statement-beg (point-min)))
db133cb6
RS
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
5bd52f0e 4092 (cperl-indent-line parse-data)
db133cb6
RS
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 ";"))
5bd52f0e
RS
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)))))
db133cb6 4114
f83d2997
KH
4115(defun cperl-indent-region (start end)
4116 "Simple variant of indentation of region in CPerl mode.
5c8b7eaf 4117Should be slow. Will not indent comment if it starts at `comment-indent'
f83d2997 4118or looks like continuation of the comment on the previous line.
5c8b7eaf
SS
4119Indents all the lines whose first character is between START and END
4120inclusive.
db133cb6 4121
5c8b7eaf 4122If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 4123conditional/loop constructs."
f83d2997 4124 (interactive "r")
5bd52f0e 4125 (cperl-update-syntaxification end end)
f83d2997 4126 (save-excursion
5bd52f0e
RS
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!
f83d2997 4133 (pm 0) (imenu-scanning-message "Indenting... (%3d%%)"))
5bd52f0e 4134 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
f83d2997
KH
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)
bab27c0c 4140 (setq end (set-marker (make-marker) end)) ; indentation changes pos
f83d2997
KH
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)
5c8b7eaf 4146 (imenu-progress-message
f83d2997 4147 pm (/ (* 100 (- (point) start)) (- end start -1))))
5bd52f0e
RS
4148 (setq st (point))
4149 (if (or
4150 (setq empty (looking-at "[ \t]*\n"))
4151 (and (setq comm (looking-at "[ \t]*#"))
5c8b7eaf 4152 (or (eq (current-indentation) (or old-comm-indent
f83d2997 4153 comment-column))
5bd52f0e 4154 (setq old-comm-indent nil))))
f83d2997 4155 (if (and old-comm-indent
5bd52f0e 4156 (not empty)
f83d2997 4157 (= (current-indentation) old-comm-indent)
5bd52f0e
RS
4158 (not (eq (get-text-property (point) 'syntax-type) 'pod))
4159 (not (eq (get-text-property (point) 'syntax-table)
4160 cperl-st-cfence)))
f83d2997
KH
4161 (let ((comment-column new-comm-indent))
4162 (indent-for-comment)))
5c8b7eaf 4163 (progn
5bd52f0e 4164 (setq i (cperl-indent-line indent-info))
f83d2997 4165 (or comm
db133cb6 4166 (not i)
f83d2997 4167 (progn
db133cb6 4168 (if cperl-indent-region-fix-constructs
5bd52f0e 4169 (goto-char (cperl-fix-line-spacing end indent-info)))
5c8b7eaf 4170 (if (setq old-comm-indent
f83d2997 4171 (and (cperl-to-comment-or-eol)
5c8b7eaf 4172 (not (memq (get-text-property (point)
f83d2997
KH
4173 'syntax-type)
4174 '(pod here-doc)))
5c8b7eaf 4175 (not (eq (get-text-property (point)
5bd52f0e
RS
4176 'syntax-table)
4177 cperl-st-cfence))
f83d2997
KH
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)
5bd52f0e
RS
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))))))))
f83d2997 4198
f83d2997
KH
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.
4203If any of the current line is a comment, fill the comment or the
4204block of it that point is in, preserving the comment's initial
4205indentation 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]*")
5c8b7eaf 4233 (setq start (point) c (current-column)
f83d2997
KH
4234 comment-fill-prefix
4235 (concat (make-string (current-column) ?\ )
4236 (buffer-substring (match-beginning 0) (match-end 0)))
5c8b7eaf 4237 spaces (progn (skip-chars-backward " \t")
f83d2997 4238 (buffer-substring (point) start))
5c8b7eaf 4239 dc (- c (current-column)) len (- start (point))
f83d2997
KH
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")
5c8b7eaf 4266 (and (looking-at "#+")
f83d2997
KH
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)
5c8b7eaf 4275 (progn
f83d2997
KH
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
5c8b7eaf 4299 (if (bolp) (progn (re-search-forward "#+[ \t]*")
f83d2997
KH
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
f83d2997
KH
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
5c8b7eaf 4314 (or name
f83d2997
KH
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
80585273 4328(defun cperl-imenu--create-perl-index (&optional regexp)
f83d2997 4329 (require 'imenu) ; May be called from TAGS creator
5c8b7eaf 4330 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
f83d2997
KH
4331 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
4332 (index-meth-alist '()) meth
6c389151 4333 packages ends-ranges p marker
f83d2997
KH
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))
6c389151 4339 (cperl-update-syntaxification (point-max) (point-max))
f83d2997
KH
4340 ;; Search for the function
4341 (progn ;;save-match-data
4342 (while (re-search-forward
80585273 4343 (or regexp cperl-imenu--function-name-regexp-perl)
f83d2997
KH
4344 nil t)
4345 (or noninteractive
4346 (imenu-progress-message prev-pos))
f83d2997
KH
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
6c389151 4356 ;; Skip if quoted (will not skip multi-line ''-strings :-():
f83d2997
KH
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
6c389151 4366 (setq char (following-char) ; ?\; for "sub foo () ;"
f83d2997
KH
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)
5c8b7eaf 4379 package (concat name "::")
f83d2997 4380 name (concat "package " name)
5c8b7eaf 4381 end-range
f83d2997
KH
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
6c389151
SM
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))
f83d2997
KH
4395 (cond ((string-match "[:']" name)
4396 (setq meth t))
4397 ((> p end-range) nil)
5c8b7eaf 4398 (t
f83d2997 4399 (setq name (concat package name) meth t))))
6c389151
SM
4400 (setq index (cons name marker))
4401 (if (eq fchar ?p)
f83d2997
KH
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))
5c8b7eaf 4419 (setq index-alist
f83d2997
KH
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)))
5c8b7eaf 4439 (if (setq group (assoc pack hier-list))
f83d2997
KH
4440 (if (listp (cdr group))
4441 ;; Have some functions already
5c8b7eaf
SS
4442 (setcdr group
4443 (cons (cons (substring
f83d2997
KH
4444 (car elt)
4445 (+ 2 (match-beginning 0)))
4446 (cdr elt))
4447 (cdr group)))
5c8b7eaf 4448 (setcdr group (list (cons (substring
f83d2997
KH
4449 (car elt)
4450 (+ 2 (match-beginning 0)))
4451 (cdr elt)))))
5c8b7eaf
SS
4452 (setq hier-list
4453 (cons (cons pack
4454 (list (cons (substring
f83d2997
KH
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))
5c8b7eaf 4466 (and (or index-pack-alist index-pod-alist
f83d2997
KH
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
6c389151 4474\f
6c389151
SM
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
f83d2997
KH
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
f83d2997
KH
4503(defun cperl-windowed-init ()
4504 "Initialization under windowed version."
db133cb6
RS
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 ()
996e2616 4511 (if (memq major-mode '(perl-mode cperl-mode))
db133cb6
RS
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
5efe6a56 4519(defvar cperl-font-lock-keywords-1 nil
80585273 4520 "Additional expressions to highlight in Perl mode. Minimal set.")
5efe6a56 4521(defvar cperl-font-lock-keywords nil
80585273 4522 "Additional expressions to highlight in Perl mode. Default set.")
5efe6a56 4523(defvar cperl-font-lock-keywords-2 nil
80585273
DL
4524 "Additional expressions to highlight in Perl mode. Maximal set")
4525
db133cb6
RS
4526(defun cperl-load-font-lock-keywords ()
4527 (or cperl-faces-init (cperl-init-faces))
5efe6a56 4528 cperl-font-lock-keywords)
db133cb6
RS
4529
4530(defun cperl-load-font-lock-keywords-1 ()
4531 (or cperl-faces-init (cperl-init-faces))
5efe6a56 4532 cperl-font-lock-keywords-1)
db133cb6
RS
4533
4534(defun cperl-load-font-lock-keywords-2 ()
4535 (or cperl-faces-init (cperl-init-faces))
5efe6a56 4536 cperl-font-lock-keywords-2)
f83d2997 4537
5bd52f0e
RS
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
f83d2997 4546(defun cperl-init-faces ()
5bd52f0e 4547 (condition-case errs
f83d2997
KH
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)
f83d2997
KH
4554 (if (fboundp 'font-lock-fontify-anchored-keywords)
4555 (setq font-lock-anchored t))
5c8b7eaf 4556 (setq
f83d2997
KH
4557 t-font-lock-keywords
4558 (list
ac6857fb 4559 `("[ \t]+$" 0 ',cperl-invalid-face t)
f83d2997
KH
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"
6c389151 4568 "require" "package" "eval" "my" "BEGIN" "END" "CHECK" "INIT")
f83d2997
KH
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"
5bd52f0e 4593 ;; "link" "listen" "localtime" "lock" "log" "lstat" "lt"
f83d2997
KH
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"
6c389151 4605 ;; "syscall" "sysopen" "sysread" "system" "syswrite" "tell"
f83d2997
KH
4606 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst"
4607 ;; "umask" "unlink" "unpack" "utime" "values" "vec"
4608 ;; "wait" "waitpid" "wantarray" "warn" "write" "x" "xor"
5c8b7eaf 4609 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
f83d2997
KH
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"
5bd52f0e 4625 "\\(\\|ngth\\)\\|o\\(c\\(altime\\|k\\)\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
f83d2997
KH
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\\|"
6c389151 4634 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|open\\|tem\\|write\\)\\|"
f83d2997
KH
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 "\\(^\\|[^$@%&\\]\\)\\<\\("
6c389151 4647 ;; "AUTOLOAD" "BEGIN" "CHECK" "DESTROY" "END" "INIT" "__END__" "chomp"
f83d2997
KH
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"
6c389151 4656 "AUTOLOAD\\|BEGIN\\|CHECK\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
f83d2997 4657 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
6c389151
SM
4658 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|INIT\\|if\\|keys\\|"
4659 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|our\\|"
f83d2997 4660 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
5bd52f0e 4661 "q\\(\\|q\\|w\\|x\\|r\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
f83d2997
KH
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
5bd52f0e 4666 "\\)\\>") 2 'cperl-nonoverridable-face)
f83d2997
KH
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]"
5bd52f0e 4673 '("\\<sub[ \t]+\\([^ \t{;()]+\\)[ \t]*\\(([^()]*)[ \t]*\\)?[#{\n]" 1
f83d2997
KH
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)
5c8b7eaf 4680 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
f83d2997
KH
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)))
db133cb6 4691 '("[\[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
f83d2997 4692 font-lock-string-face t)
5c8b7eaf 4693 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
883212ce 4694 font-lock-constant-face) ; labels
f83d2997 4695 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
883212ce 4696 2 font-lock-constant-face)
6c389151
SM
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))))
f83d2997 4701 (cond ((featurep 'font-lock-extra)
6c389151 4702 '("^[ \t]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
f83d2997
KH
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)
5c8b7eaf 4707 (2 '(restart 2 nil) nil t)))
f83d2997
KH
4708 nil t))) ; local variables, multiple
4709 (font-lock-anchored
6c389151 4710 '("^[ \t{}]*\\(my\\|local\\|our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
f83d2997
KH
4711 (3 font-lock-variable-name-face)
4712 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)"
4713 nil nil
4714 (1 font-lock-variable-name-face))))
6c389151 4715 (t '("^[ \t{}]*\\(my\\|local\\our\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
f83d2997 4716 3 font-lock-variable-name-face)))
6c389151
SM
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
f83d2997
KH
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)) ?%)
5bd52f0e
RS
4726 cperl-hash-face
4727 cperl-array-face)
f83d2997
KH
4728 t) ; arrays and hashes
4729 ("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
4730 1
5c8b7eaf 4731 (if (= (- (match-end 2) (match-beginning 2)) 1)
f83d2997 4732 (if (eq (char-after (match-beginning 3)) ?{)
5bd52f0e
RS
4733 cperl-hash-face
4734 cperl-array-face) ; arrays and hashes
f83d2997
KH
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
5c8b7eaf 4739 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
f83d2997
KH
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 )))
6c389151
SM
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
5bd52f0e
RS
4751 (if cperl-syntaxify-by-font-lock
4752 (cons 'cperl-fontify-update
4753 t-font-lock-keywords)
4754 t-font-lock-keywords)
5efe6a56
SM
4755 cperl-font-lock-keywords cperl-font-lock-keywords-1
4756 cperl-font-lock-keywords-2 (append
6c389151
SM
4757 cperl-font-lock-keywords-1
4758 t-font-lock-keywords-1)))
f83d2997
KH
4759 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
4760 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
db133cb6
RS
4761 (eval ; Avoid a warning
4762 '(font-lock-require-faces
f83d2997
KH
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)
f83d2997
KH
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 )
883212ce 4802 (list 'font-lock-constant-face
f83d2997
KH
4803 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
4804 nil
4805 [nil nil t t t]
4806 nil
4807 [nil nil t t t]
4808 )
5bd52f0e 4809 (list 'cperl-nonoverridable-face
f83d2997
KH
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 )
5bd52f0e 4817 (list 'cperl-array-face
f83d2997
KH
4818 ["blue" "yellow" nil "Gray80"]
4819 ["lightyellow2" ("navy" "os2blue" "darkgreen")
4820 "gray90"]
4821 t
4822 nil
4823 nil)
5bd52f0e 4824 (list 'cperl-hash-face
f83d2997
KH
4825 ["red" "red" nil "Gray80"]
4826 ["lightyellow2" ("navy" "os2blue" "darkgreen")
4827 "gray90"]
4828 t
4829 t
db133cb6 4830 nil))))
5bd52f0e 4831 ;; Do it the dull way, without choose-color
f83d2997
KH
4832 (defvar cperl-guessed-background nil
4833 "Display characteristics as guessed by cperl.")
5bd52f0e 4834;; (or (fboundp 'x-color-defined-p)
5c8b7eaf 4835;; (defalias 'x-color-defined-p
5bd52f0e
RS
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))))
5c8b7eaf 4841 (cperl-force-face font-lock-constant-face
5bd52f0e
RS
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
5c8b7eaf
SS
4881 (not (cperl-is-face 'cperl-array-face))
4882 (cperl-is-face 'font-lock-emphasized-face))
5bd52f0e
RS
4883 (copy-face 'font-lock-emphasized-face 'cperl-array-face))
4884 (if (and
5c8b7eaf
SS
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
5bd52f0e
RS
4888 'cperl-hash-face))
4889 (if (and
5c8b7eaf
SS
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
5bd52f0e
RS
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."))
f83d2997
KH
4902 ;; Here we try to guess background
4903 (let ((background
4904 (if (boundp 'font-lock-background-mode)
4905 font-lock-background-mode
5c8b7eaf 4906 'light))
f83d2997 4907 (face-list (and (fboundp 'face-list) (face-list)))
5bd52f0e
RS
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))))))
f83d2997
KH
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")
5c8b7eaf
SS
4923 (if (and
4924 (not (cperl-is-face 'font-lock-constant-face))
4925 (cperl-is-face 'font-lock-reference-face))
db133cb6
RS
4926 (copy-face 'font-lock-reference-face 'font-lock-constant-face))
4927 (if (cperl-is-face 'font-lock-type-face) nil
f83d2997
KH
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"))))
5bd52f0e 4942 (if (cperl-is-face 'cperl-nonoverridable-face)
f83d2997 4943 nil
5bd52f0e 4944 (copy-face 'font-lock-type-face 'cperl-nonoverridable-face)
f83d2997
KH
4945 (cond
4946 ((eq background 'light)
5bd52f0e 4947 (set-face-foreground 'cperl-nonoverridable-face
f83d2997
KH
4948 (if (x-color-defined-p "chartreuse3")
4949 "chartreuse3"
4950 "chartreuse")))
4951 ((eq background 'dark)
5bd52f0e 4952 (set-face-foreground 'cperl-nonoverridable-face
f83d2997
KH
4953 (if (x-color-defined-p "orchid1")
4954 "orchid1"
4955 "orange")))))
5bd52f0e
RS
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"))))
db133cb6 4990 (if (cperl-is-face 'font-lock-variable-name-face) nil
f83d2997 4991 (copy-face 'italic 'font-lock-variable-name-face))
db133cb6 4992 (if (cperl-is-face 'font-lock-constant-face) nil
883212ce 4993 (copy-face 'italic 'font-lock-constant-face))))
f83d2997 4994 (setq cperl-faces-init t))
5bd52f0e 4995 (error (message "cperl-init-faces (ignored): %s" errs))))
f83d2997
KH
4996
4997
4998(defun cperl-ps-print-init ()
4999 "Initialization of `ps-print' components for faces used in CPerl."
5bd52f0e
RS
5000 (eval-after-load "ps-print"
5001 '(setq ps-bold-faces
5c8b7eaf 5002 ;; font-lock-variable-name-face
5bd52f0e
RS
5003 ;; font-lock-constant-face
5004 (append '(cperl-array-face
5c8b7eaf 5005 cperl-hash-face)
5bd52f0e
RS
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.
5024If optional argument FILE is an empty string, prints to printer, otherwise
5025to the file FILE. If FILE is nil, prompts for a file name.
5026
5027Style of printout regulated by the variable `cperl-ps-print-face-properties'."
5028 (interactive)
5c8b7eaf
SS
5029 (or file
5030 (setq file (read-from-minibuffer
5bd52f0e
RS
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
5c8b7eaf
SS
5051;;; font-lock-keyword-face
5052;;; font-lock-variable-name-face
5053;;; font-lock-constant-face
5054;;; font-lock-reference-face
5bd52f0e 5055;;; font-lock-other-emphasized-face
5c8b7eaf 5056;;; cperl-hash-face)
5bd52f0e
RS
5057;;; ps-bold-faces))
5058;;; (setq ps-italic-faces
5059;;; (append '(cperl-nonoverridable-face
5c8b7eaf
SS
5060;;; font-lock-constant-face
5061;;; font-lock-reference-face
5bd52f0e
RS
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))
f83d2997
KH
5073
5074
5075(if (cperl-enable-font-lock) (cperl-windowed-init))
5076
db133cb6 5077(defconst cperl-styles-entries
5c8b7eaf
SS
5078 '(cperl-indent-level cperl-brace-offset cperl-continued-brace-offset
5079 cperl-label-offset cperl-extra-newline-before-brace
bab27c0c 5080 cperl-merge-trailing-else
db133cb6
RS
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)
bab27c0c 5090 (cperl-merge-trailing-else . t)
db133cb6
RS
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)
bab27c0c 5098 (cperl-merge-trailing-else . t)
db133cb6
RS
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)
bab27c0c 5106 (cperl-merge-trailing-else . nil)
db133cb6
RS
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) ; ???
bab27c0c 5114 (cperl-merge-trailing-else . nil)
db133cb6
RS
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)
bab27c0c 5129 (cperl-merge-trailing-else . nil)
db133cb6
RS
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.
5bd52f0e 5140Should be used via `cperl-set-style' or via Perl menu.")
db133cb6 5141
f83d2997
KH
5142(defun cperl-set-style (style)
5143 "Set CPerl-mode variables to use one of several different indentation styles.
5144The arguments are a string representing the desired style.
5c8b7eaf 5145The list of styles is in `cperl-style-alist', available styles
db133cb6
RS
5146are GNU, K&R, BSD, C++ and Whitesmith.
5147
5148The current value of style is memorized (unless there is a memorized
5149data already), may be restored by `cperl-set-style-back'.
5150
5151Chosing \"Current\" style will not change style, so this may be used for
5152side-effect of memorizing only."
5c8b7eaf
SS
5153 (interactive
5154 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
db133cb6 5155 cperl-style-alist)))
f83d2997 5156 (list (completing-read "Enter style: " list nil 'insist))))
db133cb6
RS
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)
f83d2997
KH
5164 (while style
5165 (setq setting (car style) style (cdr style))
db133cb6
RS
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
5c8b7eaf 5174 (setq setting (car cperl-old-style)
db133cb6
RS
5175 cperl-old-style (cdr cperl-old-style))
5176 (set (car setting) (cdr setting)))))
f83d2997
KH
5177
5178(defun cperl-check-syntax ()
5179 (interactive)
5180 (require 'mode-compile)
db133cb6
RS
5181 (let ((perl-dbg-flags (concat cperl-extra-perl-args " -wc")))
5182 (eval '(mode-compile)))) ; Avoid a warning
f83d2997
KH
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)))
029cb4d5 5207 (make-local-variable 'window-min-height)
f83d2997
KH
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)
029cb4d5 5226 "Show documentation for Perl command in other window.
f83d2997
KH
5227If perl-info buffer is shown in some frame, uses this frame.
5228Customized by setting variables `cperl-shrink-wrap-info-frame',
5229`cperl-max-help-size'."
5c8b7eaf 5230 (interactive
f83d2997 5231 (let* ((default (cperl-word-at-point))
5c8b7eaf
SS
5232 (read (read-string
5233 (format "Find doc for Perl function (default %s): "
f83d2997 5234 default))))
5c8b7eaf
SS
5235 (list (if (equal read "")
5236 default
f83d2997
KH
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)
5c8b7eaf 5251 (or isvar
f83d2997
KH
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)
5c8b7eaf 5260 ;; Get some of
f83d2997
KH
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
5c8b7eaf 5279 (cond ((if not-loner cperl-max-help-size
f83d2997 5280 cperl-shrink-wrap-info-frame)
5c8b7eaf
SS
5281 (setq height
5282 (+ 2
5283 (count-lines
5284 pos
f83d2997
KH
5285 (save-excursion
5286 (if (re-search-forward
5287 "^[ \t][^\n]*\n+\\([^ \t\n\f]\\|\\'\\)" nil t)
5288 (match-beginning 0) (point-max)))))
5c8b7eaf 5289 max-height
f83d2997
KH
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 ()
029cb4d5 5309 "Show documentation for Perl command at point in other window."
f83d2997
KH
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
5c8b7eaf 5319(defun cperl-imenu-info-imenu-name ()
f83d2997
KH
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
5c8b7eaf
SS
5327 imenu-prev-index-position-function
5328 imenu-extract-index-name-function
f83d2997
KH
5329 (index-item (save-restriction
5330 (save-window-excursion
5331 (set-buffer (cperl-info-buffer nil))
5c8b7eaf 5332 (setq imenu-create-index-function
f83d2997
KH
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.
5353Beginning of region should be at the start of a construction.
5354All first occurrences of this construction in the lines that are
5355partially contained in the region are lined up at the same column.
5356
5357MINSHIFT is the minimal amount of space to insert before the construction.
5358STEP is the tabwidth to position constructions.
029cb4d5 5359If STEP is nil, `cperl-lineup-step' will be used
f83d2997
KH
5360\(or `cperl-indent-level', if `cperl-lineup-step' is `nil').
5361Will 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
5c8b7eaf
SS
5377 (concat "\\<"
5378 (regexp-quote
f83d2997
KH
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)
5c8b7eaf 5389 (and (< (point) end)
f83d2997
KH
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)))))
5c8b7eaf 5399 (while
f83d2997
KH
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)) ?\ ))
5c8b7eaf
SS
5405 (beginning-of-line 2)
5406 (and (< (point) end)
f83d2997
KH
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.
5412If optional argument ALL is `recursive', will process Perl files
5413in 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")
5c8b7eaf 5423 (setq args (append (list "-e"
f83d2997
KH
5424 "sub wanted {push @ARGV, $File::Find::name if /\\.[pP][Llm]$/}
5425 use File::Find;
5426 find(\\&wanted, '.');
5c8b7eaf 5427 exec @ARGV;"
f83d2997
KH
5428 cmd) args)
5429 cmd "perl"))
5c8b7eaf 5430 (all
f83d2997 5431 ;;(error "Not implemented: all")
5c8b7eaf 5432 (setq args (append (list "-e"
f83d2997 5433 "push @ARGV, <*.PL *.pl *.pm>;
5c8b7eaf 5434 exec @ARGV;"
f83d2997
KH
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))
5c8b7eaf 5447 (message "Newlines will %sbe auto-inserted now."
f83d2997
KH
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))
5c8b7eaf 5454 (message "Perl control structure will %sbe auto-inserted now."
f83d2997
KH
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))
5c8b7eaf 5462 (message "Parentheses will %sbe auto-doubled now."
f83d2997
KH
5463 (if (cperl-val 'cperl-electric-parens) "" "not ")))
5464
db133cb6
RS
5465(defun cperl-toggle-autohelp ()
5466 "Toggle the state of automatic help message in CPerl mode.
5467See `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))
5c8b7eaf 5474 (message "Perl help messages will %sbe automatically shown now."
db133cb6
RS
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)
5c8b7eaf 5481 (setq cperl-indent-region-fix-constructs
5bd52f0e
RS
5482 (if cperl-indent-region-fix-constructs
5483 nil
5484 1))
5c8b7eaf 5485 (message "indent-region/indent-sexp will %sbe automatically fix whitespace."
db133cb6
RS
5486 (if cperl-indent-region-fix-constructs "" "not ")))
5487
f83d2997
KH
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
029cb4d5 5499 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997
KH
5500 ;; Do not introduce variable if not needed, we check it!
5501 (set 'parse-sexp-lookup-properties t))))
5502
5503(defun cperl-xsub-scan ()
f83d2997 5504 (require 'imenu)
5c8b7eaf 5505 (let ((index-alist '())
f83d2997
KH
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))
f83d2997
KH
5547 index-alist))
5548
6c389151
SM
5549(defvar cperl-unreadable-ok nil)
5550
5551(defun cperl-find-tags (ifile xs topdir)
f83d2997 5552 (let (ind (b (get-buffer cperl-tmp-buffer)) lst elt pos ret rel
6c389151 5553 (cperl-pod-here-fontify nil) f file)
f83d2997
KH
5554 (save-excursion
5555 (if b (set-buffer b)
5556 (cperl-setup-tmp-buf))
5557 (erase-buffer)
6c389151
SM
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)
f83d2997
KH
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
6c389151 5571 (cperl-find-pods-heres nil nil noninteractive)
f83d2997
KH
5572 (error (message "While scanning for syntax: %s" err))))
5573 (if xs
5574 (setq lst (cperl-xsub-scan))
80585273 5575 (setq ind (cperl-imenu--create-perl-index))
f83d2997 5576 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
5c8b7eaf
SS
5577 (setq lst
5578 (mapcar
5579 (function
f83d2997
KH
5580 (lambda (elt)
5581 (cond ((string-match "^[_a-zA-Z]" (car elt))
5582 (goto-char (cdr elt))
5bd52f0e 5583 (beginning-of-line) ; pos should be of the start of the line
5c8b7eaf
SS
5584 (list (car elt)
5585 (point)
5bd52f0e 5586 (1+ (count-lines 1 (point))) ; 1+ since at beg-o-l
f83d2997 5587 (buffer-substring (progn
6c389151
SM
5588 (goto-char (cdr elt))
5589 ;; After name now...
f83d2997
KH
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
5c8b7eaf 5601 (insert (elt elt 3)
f83d2997
KH
5602 127
5603 (if (string-match "^package " (car elt))
5604 (substring (car elt) 8)
5605 (car elt) )
5606 1
5bd52f0e 5607 (number-to-string (elt elt 2)) ; Line
f83d2997 5608 ","
5bd52f0e 5609 (number-to-string (1- (elt elt 1))) ; Char pos 0-based
f83d2997
KH
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
5c8b7eaf 5615 (setq lst (cons (cons (substring (elt elt 3)
f83d2997
KH
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))
6c389151 5632 ret))))
f83d2997
KH
5633
5634(defun cperl-add-tags-recurse-noxs ()
5635 "Add to TAGS data for Perl and XSUB files in the current directory and kids.
5636Use as
5637 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
5c8b7eaf 5638 -f cperl-add-tags-recurse
f83d2997
KH
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.
5644Use as
5645 emacs -batch -q -no-site-file -l emacs/cperl-mode.el \
5c8b7eaf 5646 -f cperl-add-tags-recurse
f83d2997
KH
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))
6c389151 5661 xs rel tm)
f83d2997
KH
5662 (save-excursion
5663 (cond (inbuffer nil) ; Already there
5664 ((file-exists-p tags-file-name)
5bd52f0e
RS
5665 (if cperl-xemacs-p
5666 (visit-tags-table-buffer)
5667 (visit-tags-table-buffer tags-file-name)))
f83d2997
KH
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)))
6c389151
SM
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)))))))
f83d2997
KH
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)
5bd52f0e
RS
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)
f83d2997
KH
5712 (progn
5713 (search-backward "\f\n")
5714 (delete-region (point)
5715 (save-excursion
5716 (forward-char 1)
5c8b7eaf 5717 (if (search-forward "\f\n"
f83d2997
KH
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
5c8b7eaf 5729 (concat
f83d2997
KH
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)
5c8b7eaf 5748 (setq pos (match-beginning 0)
f83d2997
KH
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))
5bd52f0e 5763 line (buffer-substring (match-beginning 3) (match-end 3))
f83d2997 5764 ord (if pack 1 0)
f83d2997 5765 file (file-of-tag)
5bd52f0e
RS
5766 fileind (format "%s:%s" file line)
5767 ;; Moves to beginning of the next line:
5768 info (cperl-etags-snarf-tag file line))
f83d2997
KH
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)))))
5c8b7eaf 5778 (if pack
f83d2997
KH
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.
5787Finds info about classes by a scan of loaded TAGS files.
5788Supposes that the TAGS files contain fully qualified function names.
5789One 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)))
5bd52f0e 5794 (let (pack name cons1 to l1 l2 l3 l4 b
f83d2997
KH
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))
5bd52f0e
RS
5801 (if cperl-xemacs-p ; Not checked
5802 (progn
5803 (or tags-file-name
5804 ;; Does this work in XEmacs?
f83d2997
KH
5805 (call-interactively 'visit-tags-table))
5806 (message "Updating list of classes...")
5bd52f0e
RS
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))
5c8b7eaf 5811 (mapcar
f83d2997
KH
5812 (function
5813 (lambda (tagsfile)
5bd52f0e 5814 (message "Updating list of classes... %s" tagsfile)
f83d2997
KH
5815 (set-buffer (get-file-buffer tagsfile))
5816 (cperl-tags-hier-fill)))
5817 tags-table-list)
5bd52f0e 5818 (message "Updating list of classes... postprocessing..."))
f83d2997
KH
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
5c8b7eaf 5840 (if (vectorp update)
f83d2997
KH
5841 (progn
5842 (find-file (elt update 0))
5bd52f0e 5843 (cperl-etags-goto-tag-location (elt update 1))))
f83d2997
KH
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
5c8b7eaf 5848 (let* ((regexp (concat "^\\(" (mapconcat
f83d2997
KH
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
5c8b7eaf 5858 (function
f83d2997
KH
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))
5c8b7eaf 5863 tail (if (match-end 2) (substring (car elt)
f83d2997
KH
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)
5c8b7eaf 5890 (if (not (and (listp (cdr elt))
f83d2997
KH
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)
5c8b7eaf
SS
5908 (setcdr to (cons (cons (concat "package " (car elt))
5909 (cdr elt))
f83d2997
KH
5910 (cdr to)))))
5911 (if (default-value 'imenu-sort-function)
5c8b7eaf 5912 (nreverse
f83d2997
KH
5913 (sort root-packages (default-value 'imenu-sort-function)))
5914 root-packages))
5915 ))
5916
5917;;;(x-popup-menu t
5c8b7eaf 5918;;; '(keymap "Name1"
f83d2997 5919;;; ("Ret1" "aa")
5c8b7eaf
SS
5920;;; ("Head1" "ab"
5921;;; keymap "Name2"
f83d2997
KH
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
5c8b7eaf 5929 (setq num (1+ num)
f83d2997
KH
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)
5c8b7eaf
SS
5945 (cons 'keymap
5946 (mapcar
5947 (function
f83d2997
KH
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
5c8b7eaf 5968(defvar cperl-not-bad-style-regexp
f83d2997
KH
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>
5bd52f0e 5974 "-[a-zA-Z][ \t]+[_$\"'`a-zA-Z]" ; -f file, -t STDIN
f83d2997
KH
5975 "-[0-9]" ; -5
5976 "\\+\\+" ; ++var
5977 "--" ; --var
5978 ".->" ; a->b
5979 "->" ; a SPACE ->b
5980 "\\[-" ; a[-1]
5bd52f0e 5981 "\\\\[&$@*\\\\]" ; \&func
f83d2997 5982 "^=" ; =head
5bd52f0e
RS
5983 "\\$." ; $|
5984 "<<[a-zA-Z_'\"`]" ; <<FOO, <<'FOO'
f83d2997
KH
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',
5994insertion 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.
6000Prompts user for insertion of spaces.
6001Currently 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
5c8b7eaf 6009 '("location" "locations" "insert a space into")
f83d2997
KH
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
5c8b7eaf 6016 "edit, exit with Esc Esc")
f83d2997
KH
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
f1d851ae 6054\f
f83d2997 6055;;; Getting help
5c8b7eaf 6056(defvar cperl-have-help-regexp
f83d2997
KH
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))
5c8b7eaf 6085 (re-search-backward "[-a-zA-Z0-9_:!&*+,-./<=>?\\\\^|~$%@]"
f83d2997
KH
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_:")
5c8b7eaf 6096 (cond
f83d2997
KH
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.
6134The data for these docs is a little bit obsolete and may be in fact longer
6135than 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
5c8b7eaf 6150 (message "Nothing found for %s..."
f83d2997
KH
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>")))
5c8b7eaf 6179 (setq regexp (concat "^"
f83d2997 6180 "\\([^a-zA-Z0-9_:]+[ \t]+\\)?"
5c8b7eaf 6181 (regexp-quote val)
f83d2997
KH
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))
5c8b7eaf 6190 (list
f83d2997
KH
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]
5c8b7eaf 6203! ... Logical negation.
f83d2997
KH
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.
5bd52f0e 6255$^M A buffer for emergency memory allocation when running out of memory.
f83d2997
KH
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.
5c8b7eaf 6262$| Auto-flush after write/print on current output channel? Default 0.
f83d2997
KH
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.
6359ARGV Default multi-file input filehandle. <ARGV> is a synonym for <>.
6360ARGVOUT Output filehandle with -i flag.
6361BEGIN { ... } Immediately executed (during compilation) piece of code.
6362END { ... } Pseudo-subroutine executed after the script finishes.
6c389151
SM
6363CHECK { ... } Pseudo-subroutine executed after the script is compiled.
6364INIT { ... } Pseudo-subroutine executed before the script starts running.
f83d2997
KH
6365DATA Input filehandle for what follows after __END__ or __DATA__.
6366accept(NEWSOCKET,GENERICSOCKET)
6367alarm(SECONDS)
6368atan2(X,Y)
6369bind(SOCKET,NAME)
6370binmode(FILEHANDLE)
6371caller[(LEVEL)]
6372chdir(EXPR)
6373chmod(LIST)
6374chop[(LIST|VAR)]
6375chown(LIST)
6376chroot(FILENAME)
6377close(FILEHANDLE)
6378closedir(DIRHANDLE)
6379... cmp ... String compare.
6380connect(SOCKET,NAME)
6381continue of { block } continue { block }. Is executed after `next' or at end.
6382cos(EXPR)
6383crypt(PLAINTEXT,SALT)
6384dbmclose(%HASH)
6385dbmopen(%HASH,DBNAME,MODE)
6386defined(EXPR)
6387delete($HASH{KEY})
6388die(LIST)
6389do { ... }|SUBR while|until EXPR executes at least once
6390do(EXPR|SUBR([LIST])) (with while|until executes at least once)
6391dump LABEL
6392each(%HASH)
6393endgrent
6394endhostent
6395endnetent
6396endprotoent
6397endpwent
6398endservent
6399eof[([FILEHANDLE])]
6400... eq ... String equality.
6401eval(EXPR) or eval { BLOCK }
6402exec(LIST)
6403exit(EXPR)
6404exp(EXPR)
6405fcntl(FILEHANDLE,FUNCTION,SCALAR)
6406fileno(FILEHANDLE)
6407flock(FILEHANDLE,OPERATION)
6408for (EXPR;EXPR;EXPR) { ... }
6409foreach [VAR] (@ARRAY) { ... }
6410fork
6411... ge ... String greater than or equal.
6412getc[(FILEHANDLE)]
6413getgrent
6414getgrgid(GID)
6415getgrnam(NAME)
6416gethostbyaddr(ADDR,ADDRTYPE)
6417gethostbyname(NAME)
6418gethostent
6419getlogin
6420getnetbyaddr(ADDR,ADDRTYPE)
6421getnetbyname(NAME)
6422getnetent
6423getpeername(SOCKET)
6424getpgrp(PID)
6425getppid
6426getpriority(WHICH,WHO)
6427getprotobyname(NAME)
6428getprotobynumber(NUMBER)
6429getprotoent
6430getpwent
6431getpwnam(NAME)
6432getpwuid(UID)
6433getservbyname(NAME,PROTO)
6434getservbyport(PORT,PROTO)
6435getservent
6436getsockname(SOCKET)
6437getsockopt(SOCKET,LEVEL,OPTNAME)
6438gmtime(EXPR)
6439goto LABEL
f83d2997
KH
6440... gt ... String greater than.
6441hex(EXPR)
6442if (EXPR) { ... } [ elsif (EXPR) { ... } ... ] [ else { ... } ] or EXPR if EXPR
6443index(STR,SUBSTR[,OFFSET])
6444int(EXPR)
6445ioctl(FILEHANDLE,FUNCTION,SCALAR)
6446join(EXPR,LIST)
6447keys(%HASH)
6448kill(LIST)
6449last [LABEL]
6450... le ... String less than or equal.
6451length(EXPR)
6452link(OLDFILE,NEWFILE)
6453listen(SOCKET,QUEUESIZE)
6454local(LIST)
6455localtime(EXPR)
6456log(EXPR)
6457lstat(EXPR|FILEHANDLE|VAR)
6458... lt ... String less than.
6459m/PATTERN/iogsmx
6460mkdir(FILENAME,MODE)
6461msgctl(ID,CMD,ARG)
6462msgget(KEY,FLAGS)
6463msgrcv(ID,VAR,SIZE,TYPE.FLAGS)
6464msgsnd(ID,MSG,FLAGS)
6465my VAR or my (VAR1,...) Introduces a lexical variable ($VAR, @ARR, or %HASH).
6c389151 6466our VAR or our (VAR1,...) Lexically enable a global variable ($V, @A, or %H).
f83d2997
KH
6467... ne ... String inequality.
6468next [LABEL]
6469oct(EXPR)
6470open(FILEHANDLE[,EXPR])
6471opendir(DIRHANDLE,EXPR)
6472ord(EXPR) ASCII value of the first char of the string.
6473pack(TEMPLATE,LIST)
6474package NAME Introduces package context.
6475pipe(READHANDLE,WRITEHANDLE) Create a pair of filehandles on ends of a pipe.
6476pop(ARRAY)
6477print [FILEHANDLE] [(LIST)]
6478printf [FILEHANDLE] (FORMAT,LIST)
6479push(ARRAY,LIST)
6480q/STRING/ Synonym for 'STRING'
6481qq/STRING/ Synonym for \"STRING\"
6482qx/STRING/ Synonym for `STRING`
6483rand[(EXPR)]
6484read(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
6485readdir(DIRHANDLE)
6486readlink(EXPR)
6487recv(SOCKET,SCALAR,LEN,FLAGS)
6488redo [LABEL]
6489rename(OLDNAME,NEWNAME)
6490require [FILENAME | PERL_VERSION]
6491reset[(EXPR)]
6492return(LIST)
6493reverse(LIST)
6494rewinddir(DIRHANDLE)
6495rindex(STR,SUBSTR[,OFFSET])
6496rmdir(FILENAME)
6497s/PATTERN/REPLACEMENT/gieoxsm
6498scalar(EXPR)
6499seek(FILEHANDLE,POSITION,WHENCE)
6500seekdir(DIRHANDLE,POS)
6501select(FILEHANDLE | RBITS,WBITS,EBITS,TIMEOUT)
6502semctl(ID,SEMNUM,CMD,ARG)
6503semget(KEY,NSEMS,SIZE,FLAGS)
6504semop(KEY,...)
6505send(SOCKET,MSG,FLAGS[,TO])
6506setgrent
6507sethostent(STAYOPEN)
6508setnetent(STAYOPEN)
6509setpgrp(PID,PGRP)
6510setpriority(WHICH,WHO,PRIORITY)
6511setprotoent(STAYOPEN)
6512setpwent
6513setservent(STAYOPEN)
6514setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
6515shift[(ARRAY)]
6516shmctl(ID,CMD,ARG)
6517shmget(KEY,SIZE,FLAGS)
6518shmread(ID,VAR,POS,SIZE)
6519shmwrite(ID,STRING,POS,SIZE)
6520shutdown(SOCKET,HOW)
6521sin(EXPR)
6522sleep[(EXPR)]
6523socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
6524socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
6525sort [SUBROUTINE] (LIST)
6526splice(ARRAY,OFFSET[,LENGTH[,LIST]])
6527split[(/PATTERN/[,EXPR[,LIMIT]])]
6528sprintf(FORMAT,LIST)
6529sqrt(EXPR)
6530srand(EXPR)
6531stat(EXPR|FILEHANDLE|VAR)
6532study[(SCALAR)]
6533sub [NAME [(format)]] { BODY } sub NAME [(format)]; sub [(format)] {...}
6534substr(EXPR,OFFSET[,LEN])
6535symlink(OLDFILE,NEWFILE)
6536syscall(LIST)
6537sysread(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
6538system(LIST)
6539syswrite(FILEHANDLE,SCALAR,LENGTH[,OFFSET])
6540tell[(FILEHANDLE)]
6541telldir(DIRHANDLE)
6542time
6543times
6544tr/SEARCHLIST/REPLACEMENTLIST/cds
6545truncate(FILE|EXPR,LENGTH)
6546umask[(EXPR)]
6547undef[(EXPR)]
6548unless (EXPR) { ... } [ else { ... } ] or EXPR unless EXPR
6549unlink(LIST)
6550unpack(TEMPLATE,EXPR)
6551unshift(ARRAY,LIST)
6552until (EXPR) { ... } EXPR until EXPR
6553utime(LIST)
6554values(%HASH)
6555vec(EXPR,OFFSET,BITS)
6556wait
6557waitpid(PID,FLAGS)
6558wantarray Returns true if the sub/eval is called in list context.
6559warn(LIST)
6560while (EXPR) { ... } EXPR while EXPR
6561write[(EXPR|FILEHANDLE)]
6562... x ... Repeat string or array.
6563x= ... Repetition assignment.
6564y/SEARCHLIST/REPLACEMENTLIST/
6565... | ... Bitwise or.
6566... || ... Logical or.
6567~ ... Unary bitwise complement.
db133cb6 6568#! OS interpreter indicator. If contains `perl', used for options, and -x.
f83d2997
KH
6569AUTOLOAD {...} Shorthand for `sub AUTOLOAD {...}'.
6570CORE:: Prefix to access builtin function if imported sub obscures it.
6571SUPER:: Prefix to lookup for a method in @ISA classes.
6572DESTROY 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'.
6579abs [ EXPR ] absolute value
6580... and ... Low-precedence synonym for &&.
6581bless REFERENCE [, PACKAGE] Makes reference into an object of a package.
6582chomp [LIST] Strips $/ off LIST/$_. Returns count. Special if $/ eq ''!
6583chr Converts a number to char with the same ordinal.
6584else Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
6585elsif Part of if/unless {BLOCK} elsif {BLOCK} else {BLOCK}.
6586exists $HASH{KEY} True if the key exists.
6587format [NAME] = Start of output format. Ended by a single dot (.) on a line.
6588formline PICTURE, LIST Backdoor into \"format\" processing.
6589glob EXPR Synonym of <EXPR>.
6590lc [ EXPR ] Returns lowercased EXPR.
6591lcfirst [ EXPR ] Returns EXPR with lower-cased first letter.
db133cb6 6592grep EXPR,LIST or grep {BLOCK} LIST Filters LIST via EXPR/BLOCK.
f83d2997
KH
6593map EXPR, LIST or map {BLOCK} LIST Applies EXPR/BLOCK to elts of LIST.
6594no PACKAGE [SYMBOL1, ...] Partial reverse for `use'. Runs `unimport' method.
6595not ... Low-precedence synonym for ! - negation.
6596... or ... Low-precedence synonym for ||.
6597pos STRING Set/Get end-position of the last match over this string, see \\G.
6598quotemeta [ EXPR ] Quote regexp metacharacters.
6599qw/WORD1 .../ Synonym of split('', 'WORD1 ...')
6600readline FH Synonym of <FH>.
6601readpipe CMD Synonym of `CMD`.
6602ref [ EXPR ] Type of EXPR when dereferenced.
6603sysopen FH, FILENAME, MODE [, PERM] (MODE is numeric, see Fcntl.)
6604tie VAR, PACKAGE, LIST Hide an object behind a simple Perl variable.
6605tied Returns internal object for a tied data.
6606uc [ EXPR ] Returns upcased EXPR.
6607ucfirst [ EXPR ] Returns EXPR with upcased first letter.
6608untie VAR Unlink an object from a simple Perl variable.
6609use PACKAGE [SYMBOL1, ...] Compile-time `require' with consequent `import'.
6610... xor ... Low-precedence synonym for exclusive or.
6611prototype \&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
6c389151 6635(defun cperl-beautify-regexp-piece (b e embed level)
f83d2997
KH
6636 ;; b is before the starting delimiter, e before the ending
6637 ;; e should be a marker, may be changed, but remains "correct".
6c389151
SM
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)
f83d2997
KH
6643 (if (not embed)
6644 (goto-char (1+ b))
6645 (goto-char b)
6c389151 6646 (cond ((looking-at "(\\?\\\\#") ; (?#) wrongly commented when //x-ing
f83d2997
KH
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)
6c389151 6664 (progn ; Something before the ending delimiter
f83d2997 6665 (goto-char e)
6c389151 6666 (delete-horizontal-space)
f83d2997
KH
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
5c8b7eaf 6681 (looking-at
f83d2997
KH
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)))
6c389151
SM
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) ?\{)
f83d2997 6719 (progn
6c389151
SM
6720 (forward-sexp 1)
6721 (and (eq (following-char) ??)
6722 (forward-char 1)))
6723 (re-search-forward "\\=\\([*+?]\\??\\)" e t)))
f83d2997
KH
6724 ((match-beginning 7) ; ()
6725 (goto-char (match-beginning 0))
6c389151
SM
6726 (setq pos (current-column))
6727 (or (eq pos c1)
f83d2997 6728 (progn
6c389151 6729 (delete-horizontal-space)
f83d2997
KH
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))
6c389151
SM
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))))
f83d2997
KH
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))
6c389151 6776 (delete-horizontal-space)
f83d2997
KH
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)
6c389151 6787 (delete-horizontal-space)
f83d2997
KH
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)
6c389151 6797 (delete-horizontal-space)
f83d2997
KH
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]")
6c389151
SM
6806 (error "unknown code \"%s\" in a regexp"
6807 (buffer-substring (point) (1+ (point)))))
f83d2997
KH
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 ()
db133cb6 6821 ;; Returns position of the start
6c389151 6822 ;; XXX this is called too often! Need to cache the result!
f83d2997
KH
6823 (save-excursion
6824 (or cperl-use-syntax-table-text-property
5bd52f0e 6825 (error "I need to have a regexp marked!"))
f83d2997 6826 ;; Find the start
db133cb6
RS
6827 (if (looking-at "\\s|")
6828 nil ; good already
5bd52f0e 6829 (if (looking-at "\\([smy]\\|qr\\)\\s|")
db133cb6
RS
6830 (forward-char 1)
6831 (re-search-backward "\\s|"))) ; Assume it is scanned already.
f83d2997
KH
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))
5c8b7eaf 6841 (if (looking-at "\\sw*x")
f83d2997
KH
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
6c389151 6853(defun cperl-beautify-regexp (&optional deep)
f83d2997
KH
6854 "do it. (Experimental, may change semantics, recheck the result.)
6855We suppose that the regexp is scanned already."
6c389151
SM
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))))
f83d2997 6866
db133cb6
RS
6867(defun cperl-regext-to-level-start ()
6868 "Goto start of an enclosing group in regexp.
f83d2997
KH
6869We suppose that the regexp is scanned already."
6870 (interactive)
db133cb6 6871 (let ((limit (cperl-make-regexp-x)) done)
f83d2997
KH
6872 (while (not done)
6873 (or (eq (following-char) ?\()
db133cb6 6874 (search-backward "(" (1+ limit) t)
f83d2997
KH
6875 (error "Cannot find `(' which starts a group"))
6876 (setq done
6877 (save-excursion
6878 (skip-chars-backward "\\")
6879 (looking-at "\\(\\\\\\\\\\)*(")))
db133cb6
RS
6880 (or done (forward-char -1)))))
6881
6882(defun cperl-contract-level ()
5bd52f0e 6883 "Find an enclosing group in regexp and contract it.
db133cb6
RS
6884\(Experimental, may change semantics, recheck the result.)
6885We suppose that the regexp is scanned already."
6886 (interactive)
6c389151
SM
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)))
5efe6a56
SM
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))))))
db133cb6
RS
6905
6906(defun cperl-contract-levels ()
5bd52f0e 6907 "Find an enclosing group in regexp and contract all the kids.
db133cb6
RS
6908\(Experimental, may change semantics, recheck the result.)
6909We suppose that the regexp is scanned already."
6910 (interactive)
6c389151
SM
6911 (save-excursion
6912 (condition-case nil
6913 (cperl-regext-to-level-start)
6914 (error ; We are outside outermost group
5efe6a56
SM
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
6c389151
SM
6922 ((match-beginning 1) ; Skip
6923 nil)
6924 (t ; Group
6925 (cperl-contract-level)))))))
f83d2997 6926
6c389151 6927(defun cperl-beautify-level (&optional deep)
f83d2997
KH
6928 "Find an enclosing group in regexp and beautify it.
6929\(Experimental, may change semantics, recheck the result.)
6930We suppose that the regexp is scanned already."
6c389151
SM
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))))
db133cb6
RS
6941
6942(defun cperl-invert-if-unless ()
6c389151 6943 "Change `if (A) {B}' into `B if A;' etc if possible."
db133cb6
RS
6944 (interactive)
6945 (or (looking-at "\\<")
6946 (forward-sexp -1))
6c389151 6947 (if (looking-at "\\<\\(if\\|unless\\|while\\|until\\|for\\|foreach\\)\\>")
db133cb6
RS
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))
5c8b7eaf 6986 (or (nth 3 state)
db133cb6
RS
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 ";")
6c389151 7018 (delete-horizontal-space)
db133cb6
RS
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)))
6c389151 7026 (error "Not at `if', `unless', `while', `unless', `for' or `foreach'")))
db133cb6 7027
5bd52f0e 7028;;; By Anthony Foiani <afoiani@uswest.com>
b7ec9e59
RS
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))))
a8e1e57f 7046 (require 'man)
5c8b7eaf 7047 (let* ((is-func (and
b7ec9e59
RS
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")))
b7ec9e59
RS
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
5bd52f0e 7066;;; By Nick Roberts <Nick.Roberts@src.bae.co.uk> (with changes)
b7ec9e59
RS
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))
db133cb6
RS
7100
7101(defun cperl-lazy-install ()) ; Avoid a warning
f83d2997
KH
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)
5c8b7eaf
SS
7118 (run-with-idle-timer
7119 (cperl-val 'cperl-lazy-help-time 1000000 5)
7120 t
f83d2997
KH
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 ()
996e2616 7134 (when (memq major-mode '(perl-mode cperl-mode))
f83d2997
KH
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
db133cb6
RS
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)
5bd52f0e 7154 ;; Some vars for debugging only
6c389151
SM
7155 ;; (message "Syntaxifying...")
7156 (let (start (dbg (point)) (iend end)
5bd52f0e
RS
7157 (istate (car cperl-syntax-state)))
7158 (and cperl-syntaxify-unwind
7159 (setq end (cperl-unwind-to-safe t end)))
7160 (setq start (point))
db133cb6
RS
7161 (or cperl-syntax-done-to
7162 (setq cperl-syntax-done-to (point-min)))
7163 (if (or (not (boundp 'font-lock-hot-pass))
5bd52f0e
RS
7164 (eval 'font-lock-hot-pass)
7165 t) ; Not debugged otherwise
db133cb6
RS
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))
5bd52f0e 7173 (if (eq cperl-syntaxify-by-font-lock 'message)
5c8b7eaf
SS
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
db133cb6
RS
7178 nil)) ; Do not iterate
7179
5bd52f0e
RS
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
5c8b7eaf 7199(defvar cperl-version
6c389151 7200 (let ((v "Revision: 4.32"))
5bd52f0e
RS
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
f83d2997
KH
7205(provide 'cperl-mode)
7206
7207;;; cperl-mode.el ends here