(adjust_for_invis_intang): New function.
[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
87 `(defconst ,arg (quote ,arg) ,descr))))
88 (defmacro cperl-force-face (arg descr) ; Takes unquoted arg
89 `(progn
90 (or (cperl-is-face (quote ,arg))
91 (cperl-make-face ,arg ,descr))
92 (or (boundp (quote ,arg)) ; We use unquoted variants too
93 (defconst ,arg (quote ,arg) ,descr))))
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
RS
996(defvar cperl-emacs-can-parse (> (length (save-excursion
997 (parse-partial-sexp 1 1))) 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
1244(defvar cperl-mode-syntax-table nil
1245 "Syntax table in use in Cperl-mode buffers.")
1246
1247(defvar cperl-string-syntax-table nil
1248 "Syntax table in use in Cperl-mode string-like chunks.")
1249
1250(if cperl-mode-syntax-table
1251 ()
1252 (setq cperl-mode-syntax-table (make-syntax-table))
1253 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
1254 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
1255 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
1256 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
1257 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
1258 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
1259 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
1260 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
1261 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
1262 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
1263 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
1264 (modify-syntax-entry ?\n ">" 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 (if cperl-under-as-char
1269 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table))
1270 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
1271 (modify-syntax-entry ?| "." cperl-mode-syntax-table)
1272 (setq cperl-string-syntax-table (copy-syntax-table cperl-mode-syntax-table))
1273 (modify-syntax-entry ?$ "." cperl-string-syntax-table)
1274 (modify-syntax-entry ?# "." cperl-string-syntax-table) ; (?# comment )
1275)
1276
1277
1278\f
db133cb6 1279(defvar cperl-faces-init nil)
f83d2997
KH
1280;; Fix for msb.el
1281(defvar cperl-msb-fixed nil)
1282;;;###autoload
1283(defun cperl-mode ()
1284 "Major mode for editing Perl code.
1285Expression and list commands understand all C brackets.
1286Tab indents for Perl code.
1287Paragraphs are separated by blank lines only.
1288Delete converts tabs to spaces as it moves back.
1289
1290Various characters in Perl almost always come in pairs: {}, (), [],
1291sometimes <>. When the user types the first, she gets the second as
1292well, with optional special formatting done on {}. (Disabled by
1293default.) You can always quote (with \\[quoted-insert]) the left
1294\"paren\" to avoid the expansion. The processing of < is special,
1295since most the time you mean \"less\". Cperl mode tries to guess
1296whether you want to type pair <>, and inserts is if it
1297appropriate. You can set `cperl-electric-parens-string' to the string that
1298contains the parenths from the above list you want to be electrical.
1299Electricity of parenths is controlled by `cperl-electric-parens'.
1300You may also set `cperl-electric-parens-mark' to have electric parens
1301look for active mark and \"embrace\" a region if possible.'
1302
1303CPerl mode provides expansion of the Perl control constructs:
db133cb6 1304
5c8b7eaf 1305 if, else, elsif, unless, while, until, continue, do,
db133cb6
RS
1306 for, foreach, formy and foreachmy.
1307
1308and POD directives (Disabled by default, see `cperl-electric-keywords'.)
1309
1310The user types the keyword immediately followed by a space, which
1311causes the construct to be expanded, and the point is positioned where
1312she is most likely to want to be. eg. when the user types a space
1313following \"if\" the following appears in the buffer: if () { or if ()
1314} { } and the cursor is between the parentheses. The user can then
1315type some boolean expression within the parens. Having done that,
1316typing \\[cperl-linefeed] places you - appropriately indented - on a
1317new line between the braces (if you typed \\[cperl-linefeed] in a POD
5c8b7eaf 1318directive line, then appropriate number of new lines is inserted).
db133cb6
RS
1319
1320If CPerl decides that you want to insert \"English\" style construct like
1321
f83d2997 1322 bite if angry;
db133cb6
RS
1323
1324it will not do any expansion. See also help on variable
1325`cperl-extra-newline-before-brace'. (Note that one can switch the
1326help message on expansion by setting `cperl-message-electric-keyword'
1327to nil.)
f83d2997
KH
1328
1329\\[cperl-linefeed] is a convenience replacement for typing carriage
1330return. It places you in the next line with proper indentation, or if
1331you type it inside the inline block of control construct, like
db133cb6 1332
f83d2997 1333 foreach (@lines) {print; print}
db133cb6 1334
f83d2997
KH
1335and you are on a boundary of a statement inside braces, it will
1336transform the construct into a multiline and will place you into an
5c8b7eaf
SS
1337appropriately indented blank line. If you need a usual
1338`newline-and-indent' behaviour, it is on \\[newline-and-indent],
f83d2997
KH
1339see documentation on `cperl-electric-linefeed'.
1340
db133cb6
RS
1341Use \\[cperl-invert-if-unless] to change a construction of the form
1342
1343 if (A) { B }
1344
1345into
1346
1347 B if A;
1348
f83d2997
KH
1349\\{cperl-mode-map}
1350
db133cb6
RS
1351Setting the variable `cperl-font-lock' to t switches on font-lock-mode
1352\(even with older Emacsen), `cperl-electric-lbrace-space' to t switches
1353on electric space between $ and {, `cperl-electric-parens-string' is
1354the string that contains parentheses that should be electric in CPerl
1355\(see also `cperl-electric-parens-mark' and `cperl-electric-parens'),
f83d2997
KH
1356setting `cperl-electric-keywords' enables electric expansion of
1357control structures in CPerl. `cperl-electric-linefeed' governs which
1358one of two linefeed behavior is preferable. You can enable all these
1359options simultaneously (recommended mode of use) by setting
1360`cperl-hairy' to t. In this case you can switch separate options off
db133cb6
RS
1361by setting them to `null'. Note that one may undo the extra
1362whitespace inserted by semis and braces in `auto-newline'-mode by
1363consequent \\[cperl-electric-backspace].
f83d2997
KH
1364
1365If your site has perl5 documentation in info format, you can use commands
1366\\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
1367These keys run commands `cperl-info-on-current-command' and
1368`cperl-info-on-command', which one is which is controlled by variable
5c8b7eaf 1369`cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings'
db133cb6 1370\(in turn affected by `cperl-hairy').
f83d2997
KH
1371
1372Even if you have no info-format documentation, short one-liner-style
db133cb6
RS
1373help is available on \\[cperl-get-help], and one can run perldoc or
1374man via menu.
f83d2997 1375
db133cb6
RS
1376It is possible to show this help automatically after some idle time.
1377This is regulated by variable `cperl-lazy-help-time'. Default with
1378`cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5
1379secs idle time . It is also possible to switch this on/off from the
1380menu, or via \\[cperl-toggle-autohelp]. Requires `run-with-idle-timer'.
f83d2997
KH
1381
1382Use \\[cperl-lineup] to vertically lineup some construction - put the
1383beginning of the region at the start of construction, and make region
1384span the needed amount of lines.
1385
1386Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
1387`cperl-pod-face', `cperl-pod-head-face' control processing of pod and
db133cb6
RS
1388here-docs sections. With capable Emaxen results of scan are used
1389for indentation too, otherwise they are used for highlighting only.
f83d2997
KH
1390
1391Variables controlling indentation style:
1392 `cperl-tab-always-indent'
1393 Non-nil means TAB in CPerl mode should always reindent the current line,
1394 regardless of where in the line point is when the TAB command is used.
db133cb6
RS
1395 `cperl-indent-left-aligned-comments'
1396 Non-nil means that the comment starting in leftmost column should indent.
f83d2997
KH
1397 `cperl-auto-newline'
1398 Non-nil means automatically newline before and after braces,
1399 and after colons and semicolons, inserted in Perl code. The following
1400 \\[cperl-electric-backspace] will remove the inserted whitespace.
5c8b7eaf
SS
1401 Insertion after colons requires both this variable and
1402 `cperl-auto-newline-after-colon' set.
f83d2997
KH
1403 `cperl-auto-newline-after-colon'
1404 Non-nil means automatically newline even after colons.
1405 Subject to `cperl-auto-newline' setting.
1406 `cperl-indent-level'
1407 Indentation of Perl statements within surrounding block.
1408 The surrounding block's indentation is the indentation
1409 of the line on which the open-brace appears.
1410 `cperl-continued-statement-offset'
1411 Extra indentation given to a substatement, such as the
1412 then-clause of an if, or body of a while, or just a statement continuation.
1413 `cperl-continued-brace-offset'
1414 Extra indentation given to a brace that starts a substatement.
1415 This is in addition to `cperl-continued-statement-offset'.
1416 `cperl-brace-offset'
1417 Extra indentation for line if it starts with an open brace.
1418 `cperl-brace-imaginary-offset'
1419 An open brace following other text is treated as if it the line started
1420 this far to the right of the actual line indentation.
1421 `cperl-label-offset'
1422 Extra indentation for line that is a label.
1423 `cperl-min-label-indent'
1424 Minimal indentation for line that is a label.
1425
1426Settings for K&R and BSD indentation styles are
1427 `cperl-indent-level' 5 8
1428 `cperl-continued-statement-offset' 5 8
1429 `cperl-brace-offset' -5 -8
1430 `cperl-label-offset' -5 -8
1431
db133cb6
RS
1432CPerl knows several indentation styles, and may bulk set the
1433corresponding variables. Use \\[cperl-set-style] to do this. Use
1434\\[cperl-set-style-back] to restore the memorized preexisting values
1435\(both available from menu).
1436
1437If `cperl-indent-level' is 0, the statement after opening brace in
5c8b7eaf 1438column 0 is indented on
db133cb6 1439`cperl-brace-offset'+`cperl-continued-statement-offset'.
f83d2997
KH
1440
1441Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
db133cb6
RS
1442with no args.
1443
1444DO NOT FORGET to read micro-docs (available from `Perl' menu)
1445or as help on variables `cperl-tips', `cperl-problems',
1446`cperl-non-problems', `cperl-praise', `cperl-speed'."
f83d2997
KH
1447 (interactive)
1448 (kill-all-local-variables)
f83d2997
KH
1449 (use-local-map cperl-mode-map)
1450 (if (cperl-val 'cperl-electric-linefeed)
1451 (progn
1452 (local-set-key "\C-J" 'cperl-linefeed)
1453 (local-set-key "\C-C\C-J" 'newline-and-indent)))
db133cb6
RS
1454 (if (and
1455 (cperl-val 'cperl-clobber-lisp-bindings)
1456 (cperl-val 'cperl-info-on-command-no-prompt))
f83d2997
KH
1457 (progn
1458 ;; don't clobber the backspace binding:
1459 (cperl-define-key "\C-hf" 'cperl-info-on-current-command [(control h) f])
1460 (cperl-define-key "\C-c\C-hf" 'cperl-info-on-command
1461 [(control c) (control h) f])))
029cb4d5 1462 (setq major-mode 'cperl-mode)
f83d2997
KH
1463 (setq mode-name "CPerl")
1464 (if (not cperl-mode-abbrev-table)
1465 (let ((prev-a-c abbrevs-changed))
1466 (define-abbrev-table 'cperl-mode-abbrev-table '(
1467 ("if" "if" cperl-electric-keyword 0)
1468 ("elsif" "elsif" cperl-electric-keyword 0)
1469 ("while" "while" cperl-electric-keyword 0)
1470 ("until" "until" cperl-electric-keyword 0)
1471 ("unless" "unless" cperl-electric-keyword 0)
1472 ("else" "else" cperl-electric-else 0)
db133cb6 1473 ("continue" "continue" cperl-electric-else 0)
f83d2997
KH
1474 ("for" "for" cperl-electric-keyword 0)
1475 ("foreach" "foreach" cperl-electric-keyword 0)
db133cb6
RS
1476 ("formy" "formy" cperl-electric-keyword 0)
1477 ("foreachmy" "foreachmy" cperl-electric-keyword 0)
1478 ("do" "do" cperl-electric-keyword 0)
6c389151
SM
1479 ("=pod" "=pod" cperl-electric-pod 0)
1480 ("=over" "=over" cperl-electric-pod 0)
1481 ("=head1" "=head1" cperl-electric-pod 0)
1482 ("=head2" "=head2" cperl-electric-pod 0)
db133cb6
RS
1483 ("pod" "pod" cperl-electric-pod 0)
1484 ("over" "over" cperl-electric-pod 0)
1485 ("head1" "head1" cperl-electric-pod 0)
1486 ("head2" "head2" cperl-electric-pod 0)))
f83d2997
KH
1487 (setq abbrevs-changed prev-a-c)))
1488 (setq local-abbrev-table cperl-mode-abbrev-table)
1489 (abbrev-mode (if (cperl-val 'cperl-electric-keywords) 1 0))
1490 (set-syntax-table cperl-mode-syntax-table)
6c389151
SM
1491 (make-local-variable 'outline-regexp)
1492 ;; (setq outline-regexp imenu-example--function-name-regexp-perl)
1493 (setq outline-regexp cperl-outline-regexp)
1494 (make-local-variable 'outline-level)
1495 (setq outline-level 'cperl-outline-level)
f83d2997
KH
1496 (make-local-variable 'paragraph-start)
1497 (setq paragraph-start (concat "^$\\|" page-delimiter))
1498 (make-local-variable 'paragraph-separate)
1499 (setq paragraph-separate paragraph-start)
1500 (make-local-variable 'paragraph-ignore-fill-prefix)
1501 (setq paragraph-ignore-fill-prefix t)
1502 (make-local-variable 'indent-line-function)
1503 (setq indent-line-function 'cperl-indent-line)
1504 (make-local-variable 'require-final-newline)
1505 (setq require-final-newline t)
1506 (make-local-variable 'comment-start)
1507 (setq comment-start "# ")
1508 (make-local-variable 'comment-end)
1509 (setq comment-end "")
1510 (make-local-variable 'comment-column)
1511 (setq comment-column cperl-comment-column)
1512 (make-local-variable 'comment-start-skip)
1513 (setq comment-start-skip "#+ *")
1514 (make-local-variable 'defun-prompt-regexp)
1515 (setq defun-prompt-regexp "^[ \t]*sub[ \t]+\\([^ \t\n{(;]+\\)[ \t]*")
1516 (make-local-variable 'comment-indent-function)
1517 (setq comment-indent-function 'cperl-comment-indent)
1518 (make-local-variable 'parse-sexp-ignore-comments)
1519 (setq parse-sexp-ignore-comments t)
1520 (make-local-variable 'indent-region-function)
1521 (setq indent-region-function 'cperl-indent-region)
1522 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
1523 (make-local-variable 'imenu-create-index-function)
1524 (setq imenu-create-index-function
80585273 1525 (function cperl-imenu--create-perl-index))
f83d2997
KH
1526 (make-local-variable 'imenu-sort-function)
1527 (setq imenu-sort-function nil)
1528 (make-local-variable 'vc-header-alist)
db133cb6 1529 (set 'vc-header-alist cperl-vc-header-alist) ; Avoid warning
f83d2997
KH
1530 (make-local-variable 'font-lock-defaults)
1531 (setq font-lock-defaults
db133cb6
RS
1532 (cond
1533 ((string< emacs-version "19.30")
5efe6a56 1534 '(cperl-font-lock-keywords-2))
db133cb6 1535 ((string< emacs-version "19.33") ; Which one to use?
5efe6a56
SM
1536 '((cperl-font-lock-keywords
1537 cperl-font-lock-keywords-1
1538 cperl-font-lock-keywords-2)))
db133cb6
RS
1539 (t
1540 '((cperl-load-font-lock-keywords
1541 cperl-load-font-lock-keywords-1
1542 cperl-load-font-lock-keywords-2)))))
1543 (make-local-variable 'cperl-syntax-state)
f83d2997
KH
1544 (if cperl-use-syntax-table-text-property
1545 (progn
029cb4d5 1546 (make-local-variable 'parse-sexp-lookup-properties)
f83d2997 1547 ;; Do not introduce variable if not needed, we check it!
db133cb6
RS
1548 (set 'parse-sexp-lookup-properties t)
1549 ;; Fix broken font-lock:
1550 (or (boundp 'font-lock-unfontify-region-function)
1551 (set 'font-lock-unfontify-region-function
5bd52f0e 1552 'font-lock-default-unfontify-region))
029cb4d5 1553 (make-local-variable 'font-lock-unfontify-region-function)
5c8b7eaf 1554 (set 'font-lock-unfontify-region-function
db133cb6 1555 'cperl-font-lock-unfontify-region-function)
029cb4d5 1556 (make-local-variable 'cperl-syntax-done-to)
db133cb6
RS
1557 ;; Another bug: unless font-lock-syntactic-keywords, font-lock
1558 ;; ignores syntax-table text-property. (t) is a hack
1559 ;; to make font-lock think that font-lock-syntactic-keywords
1560 ;; are defined
029cb4d5 1561 (make-local-variable 'font-lock-syntactic-keywords)
5c8b7eaf 1562 (setq font-lock-syntactic-keywords
db133cb6
RS
1563 (if cperl-syntaxify-by-font-lock
1564 '(t (cperl-fontify-syntaxically))
1565 '(t)))))
1566 (make-local-variable 'cperl-old-style)
80585273 1567 (set (make-local-variable 'normal-auto-fill-function)
072cb6f9 1568 #'cperl-do-auto-fill)
f83d2997 1569 (if (cperl-enable-font-lock)
5c8b7eaf 1570 (if (cperl-val 'cperl-font-lock)
f83d2997
KH
1571 (progn (or cperl-faces-init (cperl-init-faces))
1572 (font-lock-mode 1))))
1573 (and (boundp 'msb-menu-cond)
1574 (not cperl-msb-fixed)
1575 (cperl-msb-fix))
1576 (if (featurep 'easymenu)
46c72468 1577 (easy-menu-add cperl-menu)) ; A NOP in Emacs.
f83d2997
KH
1578 (run-hooks 'cperl-mode-hook)
1579 ;; After hooks since fontification will break this
5c8b7eaf 1580 (if cperl-pod-here-scan
5bd52f0e
RS
1581 (or ;;(and (boundp 'font-lock-mode)
1582 ;; (eval 'font-lock-mode) ; Avoid warning
1583 ;; (boundp 'font-lock-hot-pass) ; Newer font-lock
1584 cperl-syntaxify-by-font-lock ;;)
1585 (progn (or cperl-faces-init (cperl-init-faces-weak))
1586 (cperl-find-pods-heres)))))
f83d2997
KH
1587\f
1588;; Fix for perldb - make default reasonable
1589(defun cperl-db ()
1590 (interactive)
1591 (require 'gud)
1592 (perldb (read-from-minibuffer "Run perldb (like this): "
1593 (if (consp gud-perldb-history)
1594 (car gud-perldb-history)
1595 (concat "perl " ;;(file-name-nondirectory
1596 ;; I have problems
1597 ;; in OS/2
1598 ;; otherwise
1599 (buffer-file-name)))
1600 nil nil
1601 '(gud-perldb-history . 1))))
1602\f
f83d2997
KH
1603(defun cperl-msb-fix ()
1604 ;; Adds perl files to msb menu, supposes that msb is already loaded
1605 (setq cperl-msb-fixed t)
1606 (let* ((l (length msb-menu-cond))
1607 (last (nth (1- l) msb-menu-cond))
1608 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1609 (handle (1- (nth 1 last))))
1610 (setcdr precdr (list
1611 (list
996e2616 1612 '(memq major-mode '(cperl-mode perl-mode))
f83d2997
KH
1613 handle
1614 "Perl Files (%d)")
1615 last))))
1616\f
1617;; This is used by indent-for-comment
1618;; to decide how much to indent a comment in CPerl code
1619;; based on its context. Do fallback if comment is found wrong.
1620
1621(defvar cperl-wrong-comment)
5bd52f0e
RS
1622(defvar cperl-st-cfence '(14)) ; Comment-fence
1623(defvar cperl-st-sfence '(15)) ; String-fence
1624(defvar cperl-st-punct '(1))
1625(defvar cperl-st-word '(2))
1626(defvar cperl-st-bra '(4 . ?\>))
1627(defvar cperl-st-ket '(5 . ?\<))
1628
f83d2997
KH
1629
1630(defun cperl-comment-indent ()
5bd52f0e 1631 (let ((p (point)) (c (current-column)) was phony)
f83d2997
KH
1632 (if (looking-at "^#") 0 ; Existing comment at bol stays there.
1633 ;; Wrong comment found
1634 (save-excursion
5bd52f0e
RS
1635 (setq was (cperl-to-comment-or-eol)
1636 phony (eq (get-text-property (point) 'syntax-table)
1637 cperl-st-cfence))
1638 (if phony
1639 (progn
1640 (re-search-forward "#\\|$") ; Hmm, what about embedded #?
1641 (if (eq (preceding-char) ?\#)
1642 (forward-char -1))
1643 (setq was nil)))
f83d2997
KH
1644 (if (= (point) p)
1645 (progn
1646 (skip-chars-backward " \t")
1647 (max (1+ (current-column)) ; Else indent at comment column
1648 comment-column))
1649 (if was nil
1650 (insert comment-start)
1651 (backward-char (length comment-start)))
1652 (setq cperl-wrong-comment t)
1653 (indent-to comment-column 1) ; Indent minimum 1
1654 c))))) ; except leave at least one space.
1655
1656;;;(defun cperl-comment-indent-fallback ()
1657;;; "Is called if the standard comment-search procedure fails.
1658;;;Point is at start of real comment."
1659;;; (let ((c (current-column)) target cnt prevc)
1660;;; (if (= c comment-column) nil
1661;;; (setq cnt (skip-chars-backward "[ \t]"))
5c8b7eaf 1662;;; (setq target (max (1+ (setq prevc
f83d2997
KH
1663;;; (current-column))) ; Else indent at comment column
1664;;; comment-column))
1665;;; (if (= c comment-column) nil
1666;;; (delete-backward-char cnt)
1667;;; (while (< prevc target)
1668;;; (insert "\t")
1669;;; (setq prevc (current-column)))
1670;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1671;;; (while (< prevc target)
1672;;; (insert " ")
1673;;; (setq prevc (current-column)))))))
1674
1675(defun cperl-indent-for-comment ()
1676 "Substitute for `indent-for-comment' in CPerl."
1677 (interactive)
1678 (let (cperl-wrong-comment)
1679 (indent-for-comment)
1680 (if cperl-wrong-comment
1681 (progn (cperl-to-comment-or-eol)
1682 (forward-char (length comment-start))))))
1683
1684(defun cperl-comment-region (b e arg)
1685 "Comment or uncomment each line in the region in CPerl mode.
1686See `comment-region'."
1687 (interactive "r\np")
1688 (let ((comment-start "#"))
1689 (comment-region b e arg)))
1690
1691(defun cperl-uncomment-region (b e arg)
1692 "Uncomment or comment each line in the region in CPerl mode.
1693See `comment-region'."
1694 (interactive "r\np")
1695 (let ((comment-start "#"))
1696 (comment-region b e (- arg))))
1697
1698(defvar cperl-brace-recursing nil)
1699
1700(defun cperl-electric-brace (arg &optional only-before)
1701 "Insert character and correct line's indentation.
1702If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
1703place (even in empty line), but not after. If after \")\" and the inserted
5c8b7eaf 1704char is \"{\", insert extra newline before only if
f83d2997
KH
1705`cperl-extra-newline-before-brace'."
1706 (interactive "P")
1707 (let (insertpos
1708 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 1709 (cperl-mark-active)
f83d2997 1710 (< (mark) (point)))
5c8b7eaf 1711 (mark)
f83d2997
KH
1712 nil)))
1713 (if (and other-end
1714 (not cperl-brace-recursing)
1715 (cperl-val 'cperl-electric-parens)
1716 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point)))
1717 ;; Need to insert a matching pair
1718 (progn
1719 (save-excursion
1720 (setq insertpos (point-marker))
1721 (goto-char other-end)
1722 (setq last-command-char ?\{)
1723 (cperl-electric-lbrace arg insertpos))
1724 (forward-char 1))
db133cb6
RS
1725 ;: Check whether we close something "usual" with `}'
1726 (if (and (eq last-command-char ?\})
5c8b7eaf 1727 (not
db133cb6
RS
1728 (condition-case nil
1729 (save-excursion
1730 (up-list (- (prefix-numeric-value arg)))
1731 ;;(cperl-after-block-p (point-min))
1732 (cperl-after-expr-p nil "{;)"))
1733 (error nil))))
1734 ;; Just insert the guy
1735 (self-insert-command (prefix-numeric-value arg))
1736 (if (and (not arg) ; No args, end (of empty line or auto)
1737 (eolp)
1738 (or (and (null only-before)
1739 (save-excursion
1740 (skip-chars-backward " \t")
1741 (bolp)))
1742 (and (eq last-command-char ?\{) ; Do not insert newline
1743 ;; if after ")" and `cperl-extra-newline-before-brace'
1744 ;; is nil, do not insert extra newline.
1745 (not cperl-extra-newline-before-brace)
1746 (save-excursion
1747 (skip-chars-backward " \t")
1748 (eq (preceding-char) ?\))))
5c8b7eaf 1749 (if cperl-auto-newline
db133cb6
RS
1750 (progn (cperl-indent-line) (newline) t) nil)))
1751 (progn
1752 (self-insert-command (prefix-numeric-value arg))
1753 (cperl-indent-line)
1754 (if cperl-auto-newline
1755 (setq insertpos (1- (point))))
1756 (if (and cperl-auto-newline (null only-before))
1757 (progn
1758 (newline)
1759 (cperl-indent-line)))
1760 (save-excursion
1761 (if insertpos (progn (goto-char insertpos)
5c8b7eaf 1762 (search-forward (make-string
db133cb6
RS
1763 1 last-command-char))
1764 (setq insertpos (1- (point)))))
1765 (delete-char -1))))
1766 (if insertpos
f83d2997 1767 (save-excursion
db133cb6
RS
1768 (goto-char insertpos)
1769 (self-insert-command (prefix-numeric-value arg)))
1770 (self-insert-command (prefix-numeric-value arg)))))))
f83d2997
KH
1771
1772(defun cperl-electric-lbrace (arg &optional end)
1773 "Insert character, correct line's indentation, correct quoting by space."
1774 (interactive "P")
5c8b7eaf 1775 (let (pos after
f83d2997
KH
1776 (cperl-brace-recursing t)
1777 (cperl-auto-newline cperl-auto-newline)
1778 (other-end (or end
1779 (if (and cperl-electric-parens-mark
1780 (cperl-mark-active)
1781 (> (mark) (point)))
1782 (save-excursion
1783 (goto-char (mark))
5c8b7eaf 1784 (point-marker))
f83d2997
KH
1785 nil))))
1786 (and (cperl-val 'cperl-electric-lbrace-space)
1787 (eq (preceding-char) ?$)
1788 (save-excursion
1789 (skip-chars-backward "$")
1790 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
1791 (insert ?\ ))
bab27c0c 1792 ;; Check whether we are in comment
5c8b7eaf 1793 (if (and
bab27c0c
RS
1794 (save-excursion
1795 (beginning-of-line)
1796 (not (looking-at "[ \t]*#")))
1797 (cperl-after-expr-p nil "{;)"))
1798 nil
1799 (setq cperl-auto-newline nil))
f83d2997
KH
1800 (cperl-electric-brace arg)
1801 (and (cperl-val 'cperl-electric-parens)
1802 (eq last-command-char ?{)
5c8b7eaf 1803 (memq last-command-char
f83d2997
KH
1804 (append cperl-electric-parens-string nil))
1805 (or (if other-end (goto-char (marker-position other-end)))
1806 t)
1807 (setq last-command-char ?} pos (point))
1808 (progn (cperl-electric-brace arg t)
1809 (goto-char pos)))))
1810
1811(defun cperl-electric-paren (arg)
1812 "Insert a matching pair of parentheses."
1813 (interactive "P")
1814 (let ((beg (save-excursion (beginning-of-line) (point)))
1815 (other-end (if (and cperl-electric-parens-mark
5c8b7eaf 1816 (cperl-mark-active)
f83d2997
KH
1817 (> (mark) (point)))
1818 (save-excursion
1819 (goto-char (mark))
5c8b7eaf 1820 (point-marker))
f83d2997
KH
1821 nil)))
1822 (if (and (cperl-val 'cperl-electric-parens)
1823 (memq last-command-char
1824 (append cperl-electric-parens-string nil))
1825 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
1826 ;;(not (save-excursion (search-backward "#" beg t)))
1827 (if (eq last-command-char ?<)
1828 (progn
1829 (and abbrev-mode ; later it is too late, may be after `for'
1830 (expand-abbrev))
1831 (cperl-after-expr-p nil "{;(,:="))
1832 1))
1833 (progn
1834 (self-insert-command (prefix-numeric-value arg))
1835 (if other-end (goto-char (marker-position other-end)))
5c8b7eaf 1836 (insert (make-string
f83d2997
KH
1837 (prefix-numeric-value arg)
1838 (cdr (assoc last-command-char '((?{ .?})
1839 (?[ . ?])
1840 (?( . ?))
1841 (?< . ?>))))))
1842 (forward-char (- (prefix-numeric-value arg))))
1843 (self-insert-command (prefix-numeric-value arg)))))
1844
1845(defun cperl-electric-rparen (arg)
1846 "Insert a matching pair of parentheses if marking is active.
1847If not, or if we are not at the end of marking range, would self-insert."
1848 (interactive "P")
1849 (let ((beg (save-excursion (beginning-of-line) (point)))
1850 (other-end (if (and cperl-electric-parens-mark
1851 (cperl-val 'cperl-electric-parens)
1852 (memq last-command-char
1853 (append cperl-electric-parens-string nil))
5c8b7eaf 1854 (cperl-mark-active)
f83d2997 1855 (< (mark) (point)))
5c8b7eaf 1856 (mark)
f83d2997
KH
1857 nil))
1858 p)
1859 (if (and other-end
1860 (cperl-val 'cperl-electric-parens)
1861 (memq last-command-char '( ?\) ?\] ?\} ?\> ))
1862 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
1863 ;;(not (save-excursion (search-backward "#" beg t)))
1864 )
1865 (progn
1866 (self-insert-command (prefix-numeric-value arg))
1867 (setq p (point))
1868 (if other-end (goto-char other-end))
1869 (insert (make-string
1870 (prefix-numeric-value arg)
1871 (cdr (assoc last-command-char '((?\} . ?\{)
1872 (?\] . ?\[)
1873 (?\) . ?\()
1874 (?\> . ?\<))))))
1875 (goto-char (1+ p)))
1876 (self-insert-command (prefix-numeric-value arg)))))
1877
1878(defun cperl-electric-keyword ()
db133cb6
RS
1879 "Insert a construction appropriate after a keyword.
1880Help message may be switched off by setting `cperl-message-electric-keyword'
1881to nil."
5c8b7eaf 1882 (let ((beg (save-excursion (beginning-of-line) (point)))
f83d2997
KH
1883 (dollar (and (eq last-command-char ?$)
1884 (eq this-command 'self-insert-command)))
1885 (delete (and (memq last-command-char '(?\ ?\n ?\t ?\f))
db133cb6
RS
1886 (memq this-command '(self-insert-command newline))))
1887 my do)
f83d2997 1888 (and (save-excursion
db133cb6
RS
1889 (condition-case nil
1890 (progn
1891 (backward-sexp 1)
1892 (setq do (looking-at "do\\>")))
1893 (error nil))
f83d2997 1894 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
1895 (save-excursion
1896 (not
f83d2997 1897 (re-search-backward
5bd52f0e 1898 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
1899 beg t)))
1900 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
1901 (or
1902 (looking-at "=cut")
1903 (and cperl-use-syntax-table-text-property
1904 (not (eq (get-text-property (point)
1905 'syntax-type)
1906 'pod))))))
f83d2997 1907 (progn
db133cb6
RS
1908 (and (eq (preceding-char) ?y)
1909 (progn ; "foreachmy"
1910 (forward-char -2)
1911 (insert " ")
1912 (forward-char 2)
5c8b7eaf
SS
1913 (setq my t dollar t
1914 delete
db133cb6 1915 (memq this-command '(self-insert-command newline)))))
f83d2997
KH
1916 (and dollar (insert " $"))
1917 (cperl-indent-line)
1918 ;;(insert " () {\n}")
1919 (cond
1920 (cperl-extra-newline-before-brace
db133cb6 1921 (insert (if do "\n" " ()\n"))
f83d2997
KH
1922 (insert "{")
1923 (cperl-indent-line)
1924 (insert "\n")
1925 (cperl-indent-line)
db133cb6
RS
1926 (insert "\n}")
1927 (and do (insert " while ();")))
f83d2997 1928 (t
db133cb6 1929 (insert (if do " {\n} while ();" " () {\n}")))
f83d2997
KH
1930 )
1931 (or (looking-at "[ \t]\\|$") (insert " "))
1932 (cperl-indent-line)
1933 (if dollar (progn (search-backward "$")
5c8b7eaf 1934 (if my
db133cb6
RS
1935 (forward-char 1)
1936 (delete-char 1)))
f83d2997
KH
1937 (search-backward ")"))
1938 (if delete
db133cb6
RS
1939 (cperl-putback-char cperl-del-back-ch))
1940 (if cperl-message-electric-keyword
1941 (message "Precede char by C-q to avoid expansion"))))))
1942
1943(defun cperl-ensure-newlines (n &optional pos)
1944 "Make sure there are N newlines after the point."
1945 (or pos (setq pos (point)))
1946 (if (looking-at "\n")
1947 (forward-char 1)
1948 (insert "\n"))
1949 (if (> n 1)
1950 (cperl-ensure-newlines (1- n) pos)
1951 (goto-char pos)))
1952
1953(defun cperl-electric-pod ()
1954 "Insert a POD chunk appropriate after a =POD directive."
1955 (let ((delete (and (memq last-command-char '(?\ ?\n ?\t ?\f))
1956 (memq this-command '(self-insert-command newline))))
1957 head1 notlast name p really-delete over)
1958 (and (save-excursion
6c389151
SM
1959 (forward-word -1)
1960 (and
db133cb6
RS
1961 (eq (preceding-char) ?=)
1962 (progn
6c389151
SM
1963 (setq head1 (looking-at "head1\\>[ \t]*$"))
1964 (setq over (and (looking-at "over\\>[ \t]*$")
1965 (not (looking-at "over[ \t]*\n\n\n*=item\\>"))))
db133cb6
RS
1966 (forward-char -1)
1967 (bolp))
5c8b7eaf 1968 (or
5bd52f0e 1969 (get-text-property (point) 'in-pod)
db133cb6
RS
1970 (cperl-after-expr-p nil "{;:")
1971 (and (re-search-backward
6c389151
SM
1972 ;; "\\(\\`\n?\\|\n\n\\)=\\sw+"
1973 "\\(\\`\n?\\|^\n\\)=\\sw+"
1974 (point-min) t)
db133cb6
RS
1975 (not (or
1976 (looking-at "=cut")
1977 (and cperl-use-syntax-table-text-property
1978 (not (eq (get-text-property (point) 'syntax-type)
1979 'pod)))))))))
1980 (progn
1981 (save-excursion
6c389151 1982 (setq notlast (re-search-forward "^\n=" nil t)))
db133cb6
RS
1983 (or notlast
1984 (progn
1985 (insert "\n\n=cut")
1986 (cperl-ensure-newlines 2)
6c389151
SM
1987 (forward-word -2)
1988 (if (and head1
1989 (not
db133cb6
RS
1990 (save-excursion
1991 (forward-char -1)
1992 (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\>"
1993 nil t)))) ; Only one
6c389151
SM
1994 (progn
1995 (forward-word 1)
db133cb6
RS
1996 (setq name (file-name-sans-extension
1997 (file-name-nondirectory (buffer-file-name)))
1998 p (point))
5c8b7eaf 1999 (insert " NAME\n\n" name
029cb4d5 2000 " - \n\n=head1 SYNOPSIS\n\n\n\n"
db133cb6
RS
2001 "=head1 DESCRIPTION")
2002 (cperl-ensure-newlines 4)
2003 (goto-char p)
6c389151 2004 (forward-word 2)
db133cb6
RS
2005 (end-of-line)
2006 (setq really-delete t))
6c389151 2007 (forward-word 1))))
db133cb6
RS
2008 (if over
2009 (progn
2010 (setq p (point))
2011 (insert "\n\n=item \n\n\n\n"
2012 "=back")
2013 (cperl-ensure-newlines 2)
2014 (goto-char p)
6c389151 2015 (forward-word 1)
db133cb6
RS
2016 (end-of-line)
2017 (setq really-delete t)))
2018 (if (and delete really-delete)
f83d2997
KH
2019 (cperl-putback-char cperl-del-back-ch))))))
2020
2021(defun cperl-electric-else ()
db133cb6
RS
2022 "Insert a construction appropriate after a keyword.
2023Help message may be switched off by setting `cperl-message-electric-keyword'
2024to nil."
f83d2997
KH
2025 (let ((beg (save-excursion (beginning-of-line) (point))))
2026 (and (save-excursion
2027 (backward-sexp 1)
2028 (cperl-after-expr-p nil "{;:"))
5c8b7eaf
SS
2029 (save-excursion
2030 (not
f83d2997 2031 (re-search-backward
5bd52f0e 2032 "[#\"'`]\\|\\<q\\(\\|[wqxr]\\)\\>"
f83d2997
KH
2033 beg t)))
2034 (save-excursion (or (not (re-search-backward "^=" nil t))
db133cb6
RS
2035 (looking-at "=cut")
2036 (and cperl-use-syntax-table-text-property
2037 (not (eq (get-text-property (point)
2038 'syntax-type)
2039 'pod)))))
f83d2997
KH
2040 (progn
2041 (cperl-indent-line)
2042 ;;(insert " {\n\n}")
2043 (cond
2044 (cperl-extra-newline-before-brace
2045 (insert "\n")
2046 (insert "{")
2047 (cperl-indent-line)
2048 (insert "\n\n}"))
2049 (t
2050 (insert " {\n\n}"))
2051 )
2052 (or (looking-at "[ \t]\\|$") (insert " "))
2053 (cperl-indent-line)
2054 (forward-line -1)
2055 (cperl-indent-line)
db133cb6
RS
2056 (cperl-putback-char cperl-del-back-ch)
2057 (setq this-command 'cperl-electric-else)
2058 (if cperl-message-electric-keyword
2059 (message "Precede char by C-q to avoid expansion"))))))
f83d2997
KH
2060
2061(defun cperl-linefeed ()
db133cb6
RS
2062 "Go to end of line, open a new line and indent appropriately.
2063If in POD, insert appropriate lines."
f83d2997
KH
2064 (interactive)
2065 (let ((beg (save-excursion (beginning-of-line) (point)))
2066 (end (save-excursion (end-of-line) (point)))
db133cb6 2067 (pos (point)) start over cut res)
f83d2997 2068 (if (and ; Check if we need to split:
5c8b7eaf 2069 ; i.e., on a boundary and inside "{...}"
f83d2997
KH
2070 (save-excursion (cperl-to-comment-or-eol)
2071 (>= (point) pos)) ; Not in a comment
2072 (or (save-excursion
2073 (skip-chars-backward " \t" beg)
2074 (forward-char -1)
2075 (looking-at "[;{]")) ; After { or ; + spaces
2076 (looking-at "[ \t]*}") ; Before }
2077 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
2078 (save-excursion
2079 (and
5c8b7eaf 2080 (eq (car (parse-partial-sexp pos end -1)) -1)
f83d2997
KH
2081 ; Leave the level of parens
2082 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
2083 ; Are at end
6c389151 2084 (cperl-after-block-p (point-min))
f83d2997
KH
2085 (progn
2086 (backward-sexp 1)
2087 (setq start (point-marker))
db133cb6 2088 (<= start pos))))) ; Redundant? Are after the
f83d2997
KH
2089 ; start of parens group.
2090 (progn
2091 (skip-chars-backward " \t")
2092 (or (memq (preceding-char) (append ";{" nil))
2093 (insert ";"))
2094 (insert "\n")
2095 (forward-line -1)
2096 (cperl-indent-line)
2097 (goto-char start)
2098 (or (looking-at "{[ \t]*$") ; If there is a statement
2099 ; before, move it to separate line
2100 (progn
2101 (forward-char 1)
2102 (insert "\n")
2103 (cperl-indent-line)))
2104 (forward-line 1) ; We are on the target line
2105 (cperl-indent-line)
2106 (beginning-of-line)
2107 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
2108 ; after, move it to separate line
2109 (progn
2110 (end-of-line)
2111 (search-backward "}" beg)
2112 (skip-chars-backward " \t")
2113 (or (memq (preceding-char) (append ";{" nil))
2114 (insert ";"))
2115 (insert "\n")
2116 (cperl-indent-line)
2117 (forward-line -1)))
5c8b7eaf 2118 (forward-line -1) ; We are on the line before target
f83d2997
KH
2119 (end-of-line)
2120 (newline-and-indent))
db133cb6 2121 (end-of-line) ; else - no splitting
f83d2997
KH
2122 (cond
2123 ((and (looking-at "\n[ \t]*{$")
2124 (save-excursion
2125 (skip-chars-backward " \t")
2126 (eq (preceding-char) ?\)))) ; Probably if () {} group
2127 ; with an extra newline.
2128 (forward-line 2)
2129 (cperl-indent-line))
db133cb6
RS
2130 ((save-excursion ; In POD header
2131 (forward-paragraph -1)
2132 ;; (re-search-backward "\\(\\`\n?\\|\n\n\\)=head1\\b")
2133 ;; We are after \n now, so look for the rest
2134 (if (looking-at "\\(\\`\n?\\|\n\\)=\\sw+")
5c8b7eaf 2135 (progn
db133cb6
RS
2136 (setq cut (looking-at "\\(\\`\n?\\|\n\\)=cut\\>"))
2137 (setq over (looking-at "\\(\\`\n?\\|\n\\)=over\\>"))
2138 t)))
2139 (if (and over
2140 (progn
2141 (forward-paragraph -1)
2142 (forward-word 1)
2143 (setq pos (point))
2144 (setq cut (buffer-substring (point)
2145 (save-excursion
2146 (end-of-line)
2147 (point))))
2148 (delete-char (- (save-excursion (end-of-line) (point))
2149 (point)))
2150 (setq res (expand-abbrev))
2151 (save-excursion
2152 (goto-char pos)
2153 (insert cut))
2154 res))
2155 nil
2156 (cperl-ensure-newlines (if cut 2 4))
2157 (forward-line 2)))
2158 ((get-text-property (point) 'in-pod) ; In POD section
2159 (cperl-ensure-newlines 4)
2160 (forward-line 2))
f83d2997
KH
2161 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
2162 (forward-line 1)
2163 (cperl-indent-line))
2164 (t
2165 (newline-and-indent))))))
2166
2167(defun cperl-electric-semi (arg)
2168 "Insert character and correct line's indentation."
2169 (interactive "P")
2170 (if cperl-auto-newline
2171 (cperl-electric-terminator arg)
6c389151
SM
2172 (self-insert-command (prefix-numeric-value arg))
2173 (if cperl-autoindent-on-semi
2174 (cperl-indent-line))))
f83d2997
KH
2175
2176(defun cperl-electric-terminator (arg)
2177 "Insert character and correct line's indentation."
2178 (interactive "P")
5c8b7eaf 2179 (let (insertpos (end (point))
f83d2997
KH
2180 (auto (and cperl-auto-newline
2181 (or (not (eq last-command-char ?:))
2182 cperl-auto-newline-after-colon))))
5c8b7eaf 2183 (if (and ;;(not arg)
f83d2997
KH
2184 (eolp)
2185 (not (save-excursion
2186 (beginning-of-line)
2187 (skip-chars-forward " \t")
2188 (or
2189 ;; Ignore in comment lines
2190 (= (following-char) ?#)
2191 ;; Colon is special only after a label
2192 ;; So quickly rule out most other uses of colon
2193 ;; and do no indentation for them.
2194 (and (eq last-command-char ?:)
2195 (save-excursion
2196 (forward-word 1)
2197 (skip-chars-forward " \t")
2198 (and (< (point) end)
2199 (progn (goto-char (- end 1))
2200 (not (looking-at ":"))))))
2201 (progn
2202 (beginning-of-defun)
2203 (let ((pps (parse-partial-sexp (point) end)))
2204 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
2205 (progn
2206 (self-insert-command (prefix-numeric-value arg))
2207 ;;(forward-char -1)
2208 (if auto (setq insertpos (point-marker)))
2209 ;;(forward-char 1)
2210 (cperl-indent-line)
2211 (if auto
2212 (progn
2213 (newline)
2214 (cperl-indent-line)))
f83d2997
KH
2215 (save-excursion
2216 (if insertpos (goto-char (1- (marker-position insertpos)))
2217 (forward-char -1))
2218 (delete-char 1))))
2219 (if insertpos
2220 (save-excursion
2221 (goto-char insertpos)
2222 (self-insert-command (prefix-numeric-value arg)))
2223 (self-insert-command (prefix-numeric-value arg)))))
2224
2225(defun cperl-electric-backspace (arg)
5c8b7eaf 2226 "Backspace-untabify, or remove the whitespace around the point inserted
db133cb6 2227by an electric key."
f83d2997 2228 (interactive "p")
5c8b7eaf
SS
2229 (if (and cperl-auto-newline
2230 (memq last-command '(cperl-electric-semi
f83d2997
KH
2231 cperl-electric-terminator
2232 cperl-electric-lbrace))
2233 (memq (preceding-char) '(?\ ?\t ?\n)))
2234 (let (p)
5c8b7eaf 2235 (if (eq last-command 'cperl-electric-lbrace)
f83d2997
KH
2236 (skip-chars-forward " \t\n"))
2237 (setq p (point))
2238 (skip-chars-backward " \t\n")
2239 (delete-region (point) p))
db133cb6
RS
2240 (and (eq last-command 'cperl-electric-else)
2241 ;; We are removing the whitespace *inside* cperl-electric-else
2242 (setq this-command 'cperl-electric-else-really))
5c8b7eaf 2243 (if (and cperl-auto-newline
db133cb6
RS
2244 (eq last-command 'cperl-electric-else-really)
2245 (memq (preceding-char) '(?\ ?\t ?\n)))
2246 (let (p)
2247 (skip-chars-forward " \t\n")
2248 (setq p (point))
2249 (skip-chars-backward " \t\n")
2250 (delete-region (point) p))
2251 (backward-delete-char-untabify arg))))
f83d2997
KH
2252
2253(defun cperl-inside-parens-p ()
2254 (condition-case ()
2255 (save-excursion
2256 (save-restriction
2257 (narrow-to-region (point)
2258 (progn (beginning-of-defun) (point)))
2259 (goto-char (point-max))
2260 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
2261 (error nil)))
2262\f
2263(defun cperl-indent-command (&optional whole-exp)
2264 "Indent current line as Perl code, or in some cases insert a tab character.
5c8b7eaf 2265If `cperl-tab-always-indent' is non-nil (the default), always indent current
db133cb6 2266line. Otherwise, indent the current line only if point is at the left margin
f83d2997
KH
2267or in the line's indentation; otherwise insert a tab.
2268
2269A numeric argument, regardless of its value,
2270means indent rigidly all the lines of the expression starting after point
2271so that this line becomes properly indented.
2272The relative indentation among the lines of the expression are preserved."
2273 (interactive "P")
5bd52f0e 2274 (cperl-update-syntaxification (point) (point))
f83d2997
KH
2275 (if whole-exp
2276 ;; If arg, always indent this line as Perl
2277 ;; and shift remaining lines of expression the same amount.
2278 (let ((shift-amt (cperl-indent-line))
2279 beg end)
2280 (save-excursion
2281 (if cperl-tab-always-indent
2282 (beginning-of-line))
2283 (setq beg (point))
2284 (forward-sexp 1)
2285 (setq end (point))
2286 (goto-char beg)
2287 (forward-line 1)
2288 (setq beg (point)))
db133cb6 2289 (if (and shift-amt (> end beg))
f83d2997
KH
2290 (indent-code-rigidly beg end shift-amt "#")))
2291 (if (and (not cperl-tab-always-indent)
2292 (save-excursion
2293 (skip-chars-backward " \t")
2294 (not (bolp))))
2295 (insert-tab)
2296 (cperl-indent-line))))
2297
5bd52f0e 2298(defun cperl-indent-line (&optional parse-data)
f83d2997
KH
2299 "Indent current line as Perl code.
2300Return the amount the indentation changed by."
db133cb6 2301 (let (indent i beg shift-amt
f83d2997
KH
2302 (case-fold-search nil)
2303 (pos (- (point-max) (point))))
5bd52f0e 2304 (setq indent (cperl-calculate-indent parse-data)
db133cb6 2305 i indent)
f83d2997
KH
2306 (beginning-of-line)
2307 (setq beg (point))
2308 (cond ((or (eq indent nil) (eq indent t))
db133cb6 2309 (setq indent (current-indentation) i nil))
f83d2997
KH
2310 ;;((eq indent t) ; Never?
2311 ;; (setq indent (cperl-calculate-indent-within-comment)))
2312 ;;((looking-at "[ \t]*#")
2313 ;; (setq indent 0))
2314 (t
2315 (skip-chars-forward " \t")
2316 (if (listp indent) (setq indent (car indent)))
2317 (cond ((looking-at "[A-Za-z_][A-Za-z_0-9]*:[^:]")
2318 (and (> indent 0)
2319 (setq indent (max cperl-min-label-indent
2320 (+ indent cperl-label-offset)))))
2321 ((= (following-char) ?})
2322 (setq indent (- indent cperl-indent-level)))
2323 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
2324 (setq indent (+ indent cperl-close-paren-offset)))
2325 ((= (following-char) ?{)
2326 (setq indent (+ indent cperl-brace-offset))))))
2327 (skip-chars-forward " \t")
db133cb6
RS
2328 (setq shift-amt (and i (- indent (current-column))))
2329 (if (or (not shift-amt)
2330 (zerop shift-amt))
f83d2997
KH
2331 (if (> (- (point-max) pos) (point))
2332 (goto-char (- (point-max) pos)))
2333 (delete-region beg (point))
2334 (indent-to indent)
2335 ;; If initial point was within line's indentation,
2336 ;; position after the indentation. Else stay at same point in text.
2337 (if (> (- (point-max) pos) (point))
2338 (goto-char (- (point-max) pos))))
2339 shift-amt))
2340
2341(defun cperl-after-label ()
2342 ;; Returns true if the point is after label. Does not do save-excursion.
2343 (and (eq (preceding-char) ?:)
2344 (memq (char-syntax (char-after (- (point) 2)))
2345 '(?w ?_))
2346 (progn
2347 (backward-sexp)
2348 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
2349
2350(defun cperl-get-state (&optional parse-start start-state)
5bd52f0e
RS
2351 ;; returns list (START STATE DEPTH PRESTART),
2352 ;; START is a good place to start parsing, or equal to
5c8b7eaf 2353 ;; PARSE-START if preset,
5bd52f0e
RS
2354 ;; STATE is what is returned by `parse-partial-sexp'.
2355 ;; DEPTH is true is we are immediately after end of block
2356 ;; which contains START.
2357 ;; PRESTART is the position basing on which START was found.
f83d2997
KH
2358 (save-excursion
2359 (let ((start-point (point)) depth state start prestart)
5bd52f0e
RS
2360 (if (and parse-start
2361 (<= parse-start start-point))
f83d2997 2362 (goto-char parse-start)
5bd52f0e
RS
2363 (beginning-of-defun)
2364 (setq start-state nil))
f83d2997
KH
2365 (setq prestart (point))
2366 (if start-state nil
2367 ;; Try to go out, if sub is not on the outermost level
2368 (while (< (point) start-point)
2369 (setq start (point) parse-start start depth nil
2370 state (parse-partial-sexp start start-point -1))
2371 (if (> (car state) -1) nil
2372 ;; The current line could start like }}}, so the indentation
2373 ;; corresponds to a different level than what we reached
2374 (setq depth t)
2375 (beginning-of-line 2))) ; Go to the next line.
2376 (if start (goto-char start))) ; Not at the start of file
2377 (setq start (point))
f83d2997
KH
2378 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
2379 (list start state depth prestart))))
2380
2381(defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
2382 ;; Positions is before ?\{. Checks whether it starts a block.
2383 ;; No save-excursion!
2384 (cperl-backward-to-noncomment (point-min))
f83d2997
KH
2385 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
2386 ; Label may be mixed up with `$blah :'
2387 (save-excursion (cperl-after-label))
2388 (and (memq (char-syntax (preceding-char)) '(?w ?_))
2389 (progn
2390 (backward-sexp)
2391 ;; Need take into account `bless', `return', `tr',...
2392 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
5bd52f0e 2393 (not (looking-at "\\(bless\\|return\\|q[wqrx]?\\|tr\\|[smy]\\)\\>")))
f83d2997
KH
2394 (progn
2395 (skip-chars-backward " \t\n\f")
2396 (and (memq (char-syntax (preceding-char)) '(?w ?_))
2397 (progn
2398 (backward-sexp)
5c8b7eaf 2399 (looking-at
f83d2997
KH
2400 "sub[ \t]+[a-zA-Z0-9_:]+[ \t\n\f]*\\(([^()]*)[ \t\n\f]*\\)?[#{]")))))))))
2401
2402(defvar cperl-look-for-prop '((pod in-pod) (here-doc-delim here-doc-group)))
2403
5bd52f0e 2404(defun cperl-calculate-indent (&optional parse-data) ; was parse-start
f83d2997
KH
2405 "Return appropriate indentation for current line as Perl code.
2406In usual case returns an integer: the column to indent to.
5bd52f0e
RS
2407Returns nil if line starts inside a string, t if in a comment.
2408
2409Will not correct the indentation for labels, but will correct it for braces
2410and closing parentheses and brackets.."
f83d2997
KH
2411 (save-excursion
2412 (if (or
6c389151
SM
2413 (and (memq (get-text-property (point) 'syntax-type)
2414 '(pod here-doc here-doc-delim format))
2415 (not (get-text-property (point) 'indentable)))
f83d2997
KH
2416 ;; before start of POD - whitespace found since do not have 'pod!
2417 (and (looking-at "[ \t]*\n=")
2418 (error "Spaces before pod section!"))
2419 (and (not cperl-indent-left-aligned-comments)
2420 (looking-at "^#")))
2421 nil
2422 (beginning-of-line)
2423 (let ((indent-point (point))
2424 (char-after (save-excursion
2425 (skip-chars-forward " \t")
2426 (following-char)))
2427 (in-pod (get-text-property (point) 'in-pod))
2428 (pre-indent-point (point))
6c389151 2429 p prop look-prop is-block delim)
f83d2997 2430 (cond
5c8b7eaf 2431 (in-pod
f83d2997
KH
2432 ;; In the verbatim part, probably code example. What to do???
2433 )
5c8b7eaf 2434 (t
f83d2997
KH
2435 (save-excursion
2436 ;; Not in pod
2437 (cperl-backward-to-noncomment nil)
2438 (setq p (max (point-min) (1- (point)))
2439 prop (get-text-property p 'syntax-type)
2440 look-prop (or (nth 1 (assoc prop cperl-look-for-prop))
2441 'syntax-type))
2442 (if (memq prop '(pod here-doc format here-doc-delim))
2443 (progn
5c8b7eaf 2444 (goto-char (or (previous-single-property-change p look-prop)
f83d2997
KH
2445 (point-min)))
2446 (beginning-of-line)
2447 (setq pre-indent-point (point)))))))
2448 (goto-char pre-indent-point)
2449 (let* ((case-fold-search nil)
5bd52f0e 2450 (s-s (cperl-get-state (car parse-data) (nth 1 parse-data)))
5c8b7eaf 2451 (start (or (nth 2 parse-data)
5bd52f0e 2452 (nth 0 s-s)))
f83d2997
KH
2453 (state (nth 1 s-s))
2454 (containing-sexp (car (cdr state)))
f83d2997 2455 old-indent)
5c8b7eaf 2456 (if (and
5bd52f0e 2457 ;;containing-sexp ;; We are buggy at toplevel :-(
5c8b7eaf 2458 parse-data)
5bd52f0e
RS
2459 (progn
2460 (setcar parse-data pre-indent-point)
2461 (setcar (cdr parse-data) state)
2462 (or (nth 2 parse-data)
2463 (setcar (cddr parse-data) start))
2464 ;; Before this point: end of statement
2465 (setq old-indent (nth 3 parse-data))))
6c389151
SM
2466 (cond ((get-text-property (point) 'indentable)
2467 ;; indent to just after the surrounding open,
2468 ;; skip blanks if we do not close the expression.
2469 (goto-char (1+ (previous-single-property-change (point) 'indentable)))
2470 (or (memq char-after (append ")]}" nil))
2471 (looking-at "[ \t]*\\(#\\|$\\)")
2472 (skip-chars-forward " \t"))
2473 (current-column))
2474 ((or (nth 3 state) (nth 4 state))
f83d2997
KH
2475 ;; return nil or t if should not change this line
2476 (nth 4 state))
6c389151 2477 ;; XXXX Do we need to special-case this?
f83d2997
KH
2478 ((null containing-sexp)
2479 ;; Line is at top level. May be data or function definition,
2480 ;; or may be function argument declaration.
2481 ;; Indent like the previous top level line
2482 ;; unless that ends in a closeparen without semicolon,
2483 ;; in which case this line is the first argument decl.
2484 (skip-chars-forward " \t")
5bd52f0e
RS
2485 (+ (save-excursion
2486 (goto-char start)
2487 (- (current-indentation)
2488 (if (nth 2 s-s) cperl-indent-level 0)))
2489 (if (= char-after ?{) cperl-continued-brace-offset 0)
f83d2997 2490 (progn
5bd52f0e 2491 (cperl-backward-to-noncomment (or old-indent (point-min)))
f83d2997
KH
2492 ;; Look at previous line that's at column 0
2493 ;; to determine whether we are in top-level decls
2494 ;; or function's arg decls. Set basic-indent accordingly.
2495 ;; Now add a little if this is a continuation line.
2496 (if (or (bobp)
5bd52f0e 2497 (eq (point) old-indent) ; old-indent was at comment
db133cb6
RS
2498 (eq (preceding-char) ?\;)
2499 ;; Had ?\) too
2500 (and (eq (preceding-char) ?\})
5bd52f0e
RS
2501 (cperl-after-block-and-statement-beg
2502 (point-min))) ; Was start - too close
f83d2997
KH
2503 (memq char-after (append ")]}" nil))
2504 (and (eq (preceding-char) ?\:) ; label
2505 (progn
2506 (forward-sexp -1)
2507 (skip-chars-backward " \t")
5c8b7eaf 2508 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*[ \t]*:"))))
5bd52f0e
RS
2509 (progn
2510 (if (and parse-data
2511 (not (eq char-after ?\C-j)))
2512 (setcdr (cddr parse-data)
2513 (list pre-indent-point)))
2514 0)
f83d2997 2515 cperl-continued-statement-offset))))
6c389151
SM
2516 ((not
2517 (or (setq is-block
2518 (and (setq delim (= (char-after containing-sexp) ?{))
2519 (save-excursion ; Is it a hash?
2520 (goto-char containing-sexp)
2521 (cperl-block-p))))
2522 cperl-indent-parens-as-block))
2523 ;; group is an expression, not a block:
2524 ;; indent to just after the surrounding open parens,
f83d2997
KH
2525 ;; skip blanks if we do not close the expression.
2526 (goto-char (1+ containing-sexp))
2527 (or (memq char-after (append ")]}" nil))
2528 (looking-at "[ \t]*\\(#\\|$\\)")
2529 (skip-chars-forward " \t"))
2530 (current-column))
2531 ((progn
2532 ;; Containing-expr starts with \{. Check whether it is a hash.
2533 (goto-char containing-sexp)
2534 (not (cperl-block-p)))
2535 (goto-char (1+ containing-sexp))
6c389151
SM
2536 (or (memq char-after
2537 (append (if delim "}" ")]}") nil))
f83d2997
KH
2538 (looking-at "[ \t]*\\(#\\|$\\)")
2539 (skip-chars-forward " \t"))
6c389151
SM
2540 (+ (current-column)
2541 (if (and delim
2542 (eq char-after ?\}))
2543 ;; Correct indentation of trailing ?\}
2544 (+ cperl-indent-level cperl-close-paren-offset)
f83d2997 2545 0)))
6c389151
SM
2546;;; ((and (/= (char-after containing-sexp) ?{)
2547;;; (not cperl-indent-parens-as-block))
2548;;; ;; line is expression, not statement:
2549;;; ;; indent to just after the surrounding open,
2550;;; ;; skip blanks if we do not close the expression.
2551;;; (goto-char (1+ containing-sexp))
2552;;; (or (memq char-after (append ")]}" nil))
2553;;; (looking-at "[ \t]*\\(#\\|$\\)")
2554;;; (skip-chars-forward " \t"))
2555;;; (current-column))
2556;;; ((progn
2557;;; ;; Containing-expr starts with \{. Check whether it is a hash.
2558;;; (goto-char containing-sexp)
2559;;; (and (not (cperl-block-p))
2560;;; (not cperl-indent-parens-as-block)))
2561;;; (goto-char (1+ containing-sexp))
2562;;; (or (eq char-after ?\})
2563;;; (looking-at "[ \t]*\\(#\\|$\\)")
2564;;; (skip-chars-forward " \t"))
2565;;; (+ (current-column) ; Correct indentation of trailing ?\}
2566;;; (if (eq char-after ?\}) (+ cperl-indent-level
2567;;; cperl-close-paren-offset)
2568;;; 0)))
f83d2997
KH
2569 (t
2570 ;; Statement level. Is it a continuation or a new statement?
2571 ;; Find previous non-comment character.
2572 (goto-char pre-indent-point)
2573 (cperl-backward-to-noncomment containing-sexp)
2574 ;; Back up over label lines, since they don't
2575 ;; affect whether our line is a continuation.
5bd52f0e
RS
2576 ;; (Had \, too)
2577 (while ;;(or (eq (preceding-char) ?\,)
f83d2997
KH
2578 (and (eq (preceding-char) ?:)
2579 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
2580 (memq (char-syntax (char-after (- (point) 2)))
5bd52f0e
RS
2581 '(?w ?_))))
2582 ;;)
f83d2997
KH
2583 (if (eq (preceding-char) ?\,)
2584 ;; Will go to beginning of line, essentially.
2585 ;; Will ignore embedded sexpr XXXX.
2586 (cperl-backward-to-start-of-continued-exp containing-sexp))
2587 (beginning-of-line)
2588 (cperl-backward-to-noncomment containing-sexp))
2589 ;; Now we get the answer.
6c389151
SM
2590 (if (not (or (eq (1- (point)) containing-sexp)
2591 (memq (preceding-char)
2592 (append (if is-block " ;{" " ,;{") '(nil)))
db133cb6 2593 (and (eq (preceding-char) ?\})
6c389151
SM
2594 (cperl-after-block-and-statement-beg
2595 containing-sexp))))
f83d2997
KH
2596 ;; This line is continuation of preceding line's statement;
2597 ;; indent `cperl-continued-statement-offset' more than the
2598 ;; previous line of the statement.
5bd52f0e
RS
2599 ;;
2600 ;; There might be a label on this line, just
2601 ;; consider it bad style and ignore it.
f83d2997
KH
2602 (progn
2603 (cperl-backward-to-start-of-continued-exp containing-sexp)
2604 (+ (if (memq char-after (append "}])" nil))
2605 0 ; Closing parenth
2606 cperl-continued-statement-offset)
6c389151
SM
2607 (if (or is-block
2608 (not delim)
2609 (not (eq char-after ?\})))
2610 0
2611 ;; Now it is a hash reference
2612 (+ cperl-indent-level cperl-close-paren-offset))
5bd52f0e
RS
2613 (if (looking-at "\\w+[ \t]*:")
2614 (if (> (current-indentation) cperl-min-label-indent)
2615 (- (current-indentation) cperl-label-offset)
2616 ;; Do not move `parse-data', this should
5c8b7eaf 2617 ;; be quick anyway (this comment comes
5bd52f0e
RS
2618 ;;from different location):
2619 (cperl-calculate-indent))
2620 (current-column))
f83d2997
KH
2621 (if (eq char-after ?\{)
2622 cperl-continued-brace-offset 0)))
2623 ;; This line starts a new statement.
2624 ;; Position following last unclosed open.
2625 (goto-char containing-sexp)
2626 ;; Is line first statement after an open-brace?
2627 (or
2628 ;; If no, find that first statement and indent like
2629 ;; it. If the first statement begins with label, do
2630 ;; not believe when the indentation of the label is too
2631 ;; small.
2632 (save-excursion
2633 (forward-char 1)
2634 (setq old-indent (current-indentation))
2635 (let ((colon-line-end 0))
2636 (while (progn (skip-chars-forward " \t\n")
2637 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]"))
2638 ;; Skip over comments and labels following openbrace.
2639 (cond ((= (following-char) ?\#)
2640 (forward-line 1))
2641 ;; label:
2642 (t
2643 (save-excursion (end-of-line)
2644 (setq colon-line-end (point)))
2645 (search-forward ":"))))
2646 ;; The first following code counts
2647 ;; if it is before the line we want to indent.
2648 (and (< (point) indent-point)
2649 (if (> colon-line-end (point)) ; After label
5c8b7eaf 2650 (if (> (current-indentation)
f83d2997
KH
2651 cperl-min-label-indent)
2652 (- (current-indentation) cperl-label-offset)
2653 ;; Do not believe: `max' is involved
2654 (+ old-indent cperl-indent-level))
2655 (current-column)))))
2656 ;; If no previous statement,
2657 ;; indent it relative to line brace is on.
2658 ;; For open brace in column zero, don't let statement
2659 ;; start there too. If cperl-indent-level is zero,
2660 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
2661 ;; For open-braces not the first thing in a line,
2662 ;; add in cperl-brace-imaginary-offset.
2663
2664 ;; If first thing on a line: ?????
2665 (+ (if (and (bolp) (zerop cperl-indent-level))
2666 (+ cperl-brace-offset cperl-continued-statement-offset)
2667 cperl-indent-level)
6c389151
SM
2668 (if (or is-block
2669 (not delim)
2670 (not (eq char-after ?\})))
2671 0
2672 ;; Now it is a hash reference
2673 (+ cperl-indent-level cperl-close-paren-offset))
f83d2997
KH
2674 ;; Move back over whitespace before the openbrace.
2675 ;; If openbrace is not first nonwhite thing on the line,
2676 ;; add the cperl-brace-imaginary-offset.
2677 (progn (skip-chars-backward " \t")
2678 (if (bolp) 0 cperl-brace-imaginary-offset))
2679 ;; If the openbrace is preceded by a parenthesized exp,
2680 ;; move to the beginning of that;
2681 ;; possibly a different line
2682 (progn
2683 (if (eq (preceding-char) ?\))
2684 (forward-sexp -1))
2685 ;; In the case it starts a subroutine, indent with
110c171f 2686 ;; respect to `sub', not with respect to the
f83d2997
KH
2687 ;; first thing on the line, say in the case of
2688 ;; anonymous sub in a hash.
2689 ;;
2690 (skip-chars-backward " \t")
2691 (if (and (eq (preceding-char) ?b)
2692 (progn
2693 (forward-sexp -1)
2694 (looking-at "sub\\>"))
5c8b7eaf
SS
2695 (setq old-indent
2696 (nth 1
2697 (parse-partial-sexp
2698 (save-excursion (beginning-of-line) (point))
f83d2997
KH
2699 (point)))))
2700 (progn (goto-char (1+ old-indent))
2701 (skip-chars-forward " \t")
2702 (current-column))
2703 ;; Get initial indentation of the line we are on.
2704 ;; If line starts with label, calculate label indentation
2705 (if (save-excursion
2706 (beginning-of-line)
2707 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
2708 (if (> (current-indentation) cperl-min-label-indent)
2709 (- (current-indentation) cperl-label-offset)
5bd52f0e
RS
2710 ;; Do not move `parse-data', this should
2711 ;; be quick anyway:
2712 (cperl-calculate-indent))
f83d2997
KH
2713 (current-indentation))))))))))))))
2714
2715(defvar cperl-indent-alist
2716 '((string nil)
2717 (comment nil)
2718 (toplevel 0)
2719 (toplevel-after-parenth 2)
2720 (toplevel-continued 2)
2721 (expression 1))
2722 "Alist of indentation rules for CPerl mode.
2723The values mean:
2724 nil: do not indent;
db133cb6
RS
2725 number: add this amount of indentation.
2726
2727Not finished, not used.")
f83d2997
KH
2728
2729(defun cperl-where-am-i (&optional parse-start start-state)
2730 ;; Unfinished
2731 "Return a list of lists ((TYPE POS)...) of good points before the point.
db133cb6
RS
2732POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'.
2733
2734Not finished, not used."
f83d2997
KH
2735 (save-excursion
2736 (let* ((start-point (point))
2737 (s-s (cperl-get-state))
2738 (start (nth 0 s-s))
2739 (state (nth 1 s-s))
2740 (prestart (nth 3 s-s))
2741 (containing-sexp (car (cdr state)))
2742 (case-fold-search nil)
2743 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
2744 (cond ((nth 3 state) ; In string
2745 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
2746 ((nth 4 state) ; In comment
2747 (setq res (cons '(comment) res)))
2748 ((null containing-sexp)
5c8b7eaf 2749 ;; Line is at top level.
f83d2997
KH
2750 ;; Indent like the previous top level line
2751 ;; unless that ends in a closeparen without semicolon,
2752 ;; in which case this line is the first argument decl.
2753 (cperl-backward-to-noncomment (or parse-start (point-min)))
2754 ;;(skip-chars-backward " \t\f\n")
2755 (cond
2756 ((or (bobp)
2757 (memq (preceding-char) (append ";}" nil)))
2758 (setq res (cons (list 'toplevel start) res)))
2759 ((eq (preceding-char) ?\) )
2760 (setq res (cons (list 'toplevel-after-parenth start) res)))
5c8b7eaf 2761 (t
f83d2997
KH
2762 (setq res (cons (list 'toplevel-continued start) res)))))
2763 ((/= (char-after containing-sexp) ?{)
2764 ;; line is expression, not statement:
2765 ;; indent to just after the surrounding open.
2766 ;; skip blanks if we do not close the expression.
2767 (setq res (cons (list 'expression-blanks
2768 (progn
2769 (goto-char (1+ containing-sexp))
2770 (or (looking-at "[ \t]*\\(#\\|$\\)")
2771 (skip-chars-forward " \t"))
2772 (point)))
2773 (cons (list 'expression containing-sexp) res))))
2774 ((progn
2775 ;; Containing-expr starts with \{. Check whether it is a hash.
2776 (goto-char containing-sexp)
2777 (not (cperl-block-p)))
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 (t
2786 ;; Statement level.
2787 (setq res (cons (list 'in-block containing-sexp) res))
2788 ;; Is it a continuation or a new statement?
2789 ;; Find previous non-comment character.
2790 (cperl-backward-to-noncomment containing-sexp)
2791 ;; Back up over label lines, since they don't
2792 ;; affect whether our line is a continuation.
2793 ;; Back up comma-delimited lines too ?????
2794 (while (or (eq (preceding-char) ?\,)
2795 (save-excursion (cperl-after-label)))
2796 (if (eq (preceding-char) ?\,)
2797 ;; Will go to beginning of line, essentially
2798 ;; Will ignore embedded sexpr XXXX.
2799 (cperl-backward-to-start-of-continued-exp containing-sexp))
2800 (beginning-of-line)
2801 (cperl-backward-to-noncomment containing-sexp))
2802 ;; Now we get the answer.
2803 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
2804 ;; This line is continuation of preceding line's statement.
2805 (list (list 'statement-continued containing-sexp))
2806 ;; This line starts a new statement.
2807 ;; Position following last unclosed open.
2808 (goto-char containing-sexp)
2809 ;; Is line first statement after an open-brace?
2810 (or
2811 ;; If no, find that first statement and indent like
2812 ;; it. If the first statement begins with label, do
2813 ;; not believe when the indentation of the label is too
2814 ;; small.
2815 (save-excursion
2816 (forward-char 1)
2817 (let ((colon-line-end 0))
2818 (while (progn (skip-chars-forward " \t\n" start-point)
2819 (and (< (point) start-point)
2820 (looking-at
2821 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
2822 ;; Skip over comments and labels following openbrace.
2823 (cond ((= (following-char) ?\#)
2824 ;;(forward-line 1)
2825 (end-of-line))
2826 ;; label:
2827 (t
2828 (save-excursion (end-of-line)
2829 (setq colon-line-end (point)))
2830 (search-forward ":"))))
5c8b7eaf 2831 ;; Now at the point, after label, or at start
f83d2997
KH
2832 ;; of first statement in the block.
2833 (and (< (point) start-point)
5c8b7eaf 2834 (if (> colon-line-end (point))
f83d2997 2835 ;; Before statement after label
5c8b7eaf 2836 (if (> (current-indentation)
f83d2997
KH
2837 cperl-min-label-indent)
2838 (list (list 'label-in-block (point)))
2839 ;; Do not believe: `max' is involved
2840 (list
2841 (list 'label-in-block-min-indent (point))))
2842 ;; Before statement
2843 (list 'statement-in-block (point))))))
2844 ;; If no previous statement,
2845 ;; indent it relative to line brace is on.
2846 ;; For open brace in column zero, don't let statement
2847 ;; start there too. If cperl-indent-level is zero,
2848 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
2849 ;; For open-braces not the first thing in a line,
2850 ;; add in cperl-brace-imaginary-offset.
2851
2852 ;; If first thing on a line: ?????
2853 (+ (if (and (bolp) (zerop cperl-indent-level))
2854 (+ cperl-brace-offset cperl-continued-statement-offset)
2855 cperl-indent-level)
2856 ;; Move back over whitespace before the openbrace.
2857 ;; If openbrace is not first nonwhite thing on the line,
2858 ;; add the cperl-brace-imaginary-offset.
2859 (progn (skip-chars-backward " \t")
2860 (if (bolp) 0 cperl-brace-imaginary-offset))
2861 ;; If the openbrace is preceded by a parenthesized exp,
2862 ;; move to the beginning of that;
2863 ;; possibly a different line
2864 (progn
2865 (if (eq (preceding-char) ?\))
2866 (forward-sexp -1))
2867 ;; Get initial indentation of the line we are on.
2868 ;; If line starts with label, calculate label indentation
2869 (if (save-excursion
2870 (beginning-of-line)
2871 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_0-9]*:[^:]"))
2872 (if (> (current-indentation) cperl-min-label-indent)
2873 (- (current-indentation) cperl-label-offset)
5bd52f0e 2874 (cperl-calculate-indent))
f83d2997
KH
2875 (current-indentation))))))))
2876 res)))
2877
2878(defun cperl-calculate-indent-within-comment ()
2879 "Return the indentation amount for line, assuming that
2880the current line is to be regarded as part of a block comment."
2881 (let (end star-start)
2882 (save-excursion
2883 (beginning-of-line)
2884 (skip-chars-forward " \t")
2885 (setq end (point))
2886 (and (= (following-char) ?#)
2887 (forward-line -1)
2888 (cperl-to-comment-or-eol)
2889 (setq end (point)))
2890 (goto-char end)
2891 (current-column))))
2892
2893
2894(defun cperl-to-comment-or-eol ()
029cb4d5 2895 "Go to position before comment on the current line, or to end of line.
f83d2997
KH
2896Returns true if comment is found."
2897 (let (state stop-in cpoint (lim (progn (end-of-line) (point))))
2898 (beginning-of-line)
5c8b7eaf 2899 (if (or
f83d2997
KH
2900 (eq (get-text-property (point) 'syntax-type) 'pod)
2901 (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t))
2902 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
2903 ;; Else
2904 (while (not stop-in)
2905 (setq state (parse-partial-sexp (point) lim nil nil nil t))
2906 ; stop at comment
2907 ;; If fails (beginning-of-line inside sexp), then contains not-comment
f83d2997
KH
2908 (if (nth 4 state) ; After `#';
2909 ; (nth 2 state) can be
2910 ; beginning of m,s,qq and so
2911 ; on
2912 (if (nth 2 state)
2913 (progn
2914 (setq cpoint (point))
2915 (goto-char (nth 2 state))
2916 (cond
2917 ((looking-at "\\(s\\|tr\\)\\>")
2918 (or (re-search-forward
2919 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
2920 lim 'move)
2921 (setq stop-in t)))
5bd52f0e 2922 ((looking-at "\\(m\\|q\\([qxwr]\\)?\\)\\>")
f83d2997
KH
2923 (or (re-search-forward
2924 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
2925 lim 'move)
2926 (setq stop-in t)))
2927 (t ; It was fair comment
2928 (setq stop-in t) ; Finish
2929 (goto-char (1- cpoint)))))
2930 (setq stop-in t) ; Finish
2931 (forward-char -1))
2932 (setq stop-in t)) ; Finish
2933 )
2934 (nth 4 state))))
2935
2936(defsubst cperl-1- (p)
2937 (max (point-min) (1- p)))
2938
2939(defsubst cperl-1+ (p)
2940 (min (point-max) (1+ p)))
2941
f83d2997
KH
2942(defsubst cperl-modify-syntax-type (at how)
2943 (if (< at (point-max))
2944 (progn
2945 (put-text-property at (1+ at) 'syntax-table how)
2946 (put-text-property at (1+ at) 'rear-nonsticky t))))
2947
2948(defun cperl-protect-defun-start (s e)
2949 ;; C code looks for "^\\s(" to skip comment backward in "hard" situations
2950 (save-excursion
2951 (goto-char s)
2952 (while (re-search-forward "^\\s(" e 'to-end)
2953 (put-text-property (1- (point)) (point) 'syntax-table cperl-st-punct))))
2954
5bd52f0e 2955(defun cperl-commentify (bb e string &optional noface)
5c8b7eaf 2956 (if cperl-use-syntax-table-text-property
5bd52f0e
RS
2957 (if (eq noface 'n) ; Only immediate
2958 nil
f83d2997
KH
2959 ;; We suppose that e is _after_ the end of construction, as after eol.
2960 (setq string (if string cperl-st-sfence cperl-st-cfence))
6c389151
SM
2961 (if (> bb (- e 2))
2962 ;; one-char string/comment?!
2963 (cperl-modify-syntax-type bb cperl-st-punct)
2964 (cperl-modify-syntax-type bb string)
2965 (cperl-modify-syntax-type (1- e) string))
f83d2997 2966 (if (and (eq string cperl-st-sfence) (> (- e 2) bb))
5c8b7eaf 2967 (put-text-property (1+ bb) (1- e)
f83d2997 2968 'syntax-table cperl-string-syntax-table))
5bd52f0e
RS
2969 (cperl-protect-defun-start bb e))
2970 ;; Fontify
2971 (or noface
2972 (not cperl-pod-here-fontify)
2973 (put-text-property bb e 'face (if string 'font-lock-string-face
2974 'font-lock-comment-face)))))
6c389151 2975
5bd52f0e
RS
2976(defvar cperl-starters '(( ?\( . ?\) )
2977 ( ?\[ . ?\] )
2978 ( ?\{ . ?\} )
2979 ( ?\< . ?\> )))
f83d2997
KH
2980
2981(defun cperl-forward-re (lim end is-2arg set-st st-l err-l argument
2982 &optional ostart oend)
2983 ;; Works *before* syntax recognition is done
2984 ;; May modify syntax-type text property if the situation is too hard
6c389151 2985 (let (b starter ender st i i2 go-forward reset-st)
f83d2997
KH
2986 (skip-chars-forward " \t")
2987 ;; ender means matching-char matcher.
5c8b7eaf 2988 (setq b (point)
5bd52f0e
RS
2989 starter (if (eobp) 0 (char-after b))
2990 ender (cdr (assoc starter cperl-starters)))
f83d2997
KH
2991 ;; What if starter == ?\\ ????
2992 (if set-st
2993 (if (car st-l)
2994 (setq st (car st-l))
2995 (setcar st-l (make-syntax-table))
2996 (setq i 0 st (car st-l))
2997 (while (< i 256)
2998 (modify-syntax-entry i "." st)
2999 (setq i (1+ i)))
3000 (modify-syntax-entry ?\\ "\\" st)))
3001 (setq set-st t)
3002 ;; Whether we have an intermediate point
3003 (setq i nil)
3004 ;; Prepare the syntax table:
3005 (and set-st
3006 (if (not ender) ; m/blah/, s/x//, s/x/y/
3007 (modify-syntax-entry starter "$" st)
3008 (modify-syntax-entry starter (concat "(" (list ender)) st)
3009 (modify-syntax-entry ender (concat ")" (list starter)) st)))
3010 (condition-case bb
3011 (progn
5bd52f0e
RS
3012 ;; We use `$' syntax class to find matching stuff, but $$
3013 ;; is recognized the same as $, so we need to check this manually.
f83d2997
KH
3014 (if (and (eq starter (char-after (cperl-1+ b)))
3015 (not ender))
3016 ;; $ has TeXish matching rules, so $$ equiv $...
3017 (forward-char 2)
6c389151 3018 (setq reset-st (syntax-table))
f83d2997
KH
3019 (set-syntax-table st)
3020 (forward-sexp 1)
6c389151
SM
3021 (if (<= (point) (1+ b))
3022 (error "Unfinished regular expression"))
3023 (set-syntax-table reset-st)
3024 (setq reset-st nil)
f83d2997
KH
3025 ;; Now the problem is with m;blah;;
3026 (and (not ender)
3027 (eq (preceding-char)
3028 (char-after (- (point) 2)))
3029 (save-excursion
3030 (forward-char -2)
3031 (= 0 (% (skip-chars-backward "\\\\") 2)))
3032 (forward-char -1)))
5bd52f0e 3033 ;; Now we are after the first part.
f83d2997
KH
3034 (and is-2arg ; Have trailing part
3035 (not ender)
3036 (eq (following-char) starter) ; Empty trailing part
3037 (progn
3038 (or (eq (char-syntax (following-char)) ?.)
3039 ;; Make trailing letter into punctuation
3040 (cperl-modify-syntax-type (point) cperl-st-punct))
3041 (setq is-2arg nil go-forward t))) ; Ignore the tail
3042 (if is-2arg ; Not number => have second part
3043 (progn
3044 (setq i (point) i2 i)
3045 (if ender
3046 (if (memq (following-char) '(?\ ?\t ?\n ?\f))
3047 (progn
3048 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3049 (goto-char (match-end 0))
3050 (skip-chars-forward " \t\n\f"))
3051 (setq i2 (point))))
3052 (forward-char -1))
3053 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
5c8b7eaf 3054 (if ender (modify-syntax-entry ender "." st))
f83d2997 3055 (setq set-st nil)
5bd52f0e
RS
3056 (setq ender (cperl-forward-re lim end nil t st-l err-l
3057 argument starter ender)
f83d2997
KH
3058 ender (nth 2 ender)))))
3059 (error (goto-char lim)
3060 (setq set-st nil)
6c389151
SM
3061 (if reset-st
3062 (set-syntax-table reset-st))
f83d2997
KH
3063 (or end
3064 (message
5bd52f0e 3065 "End of `%s%s%c ... %c' string/RE not found: %s"
f83d2997
KH
3066 argument
3067 (if ostart (format "%c ... %c" ostart (or oend ostart)) "")
3068 starter (or ender starter) bb)
3069 (or (car err-l) (setcar err-l b)))))
3070 (if set-st
3071 (progn
3072 (modify-syntax-entry starter (if (eq starter ?\\) "\\" ".") st)
3073 (if ender (modify-syntax-entry ender "." st))))
5bd52f0e
RS
3074 ;; i: have 2 args, after end of the first arg
3075 ;; i2: start of the second arg, if any (before delim iff `ender').
3076 ;; ender: the last arg bounded by parens-like chars, the second one of them
3077 ;; starter: the starting delimiter of the first arg
5efe6a56 3078 ;; go-forward: has 2 args, and the second part is empty
f83d2997
KH
3079 (list i i2 ender starter go-forward)))
3080
5c8b7eaf 3081(defsubst cperl-postpone-fontification (b e type val &optional now)
5bd52f0e
RS
3082 ;; Do after syntactic fontification?
3083 (if cperl-syntaxify-by-font-lock
3084 (or now (put-text-property b e 'cperl-postpone (cons type val)))
3085 (put-text-property b e type val)))
3086
3087;;; Here is how the global structures (those which cannot be
3088;;; recognized locally) are marked:
5c8b7eaf 3089;; a) PODs:
5bd52f0e
RS
3090;; Start-to-end is marked `in-pod' ==> t
3091;; Each non-literal part is marked `syntax-type' ==> `pod'
3092;; Each literal part is marked `syntax-type' ==> `in-pod'
5c8b7eaf 3093;; b) HEREs:
5bd52f0e
RS
3094;; Start-to-end is marked `here-doc-group' ==> t
3095;; The body is marked `syntax-type' ==> `here-doc'
3096;; The delimiter is marked `syntax-type' ==> `here-doc-delim'
5c8b7eaf 3097;; c) FORMATs:
5bd52f0e 3098;; After-initial-line--to-end is marked `syntax-type' ==> `format'
5c8b7eaf 3099;; d) 'Q'uoted string:
5bd52f0e 3100;; part between markers inclusive is marked `syntax-type' ==> `string'
6c389151 3101;; part between `q' and the first marker is marked `syntax-type' ==> `prestring'
5bd52f0e
RS
3102
3103(defun cperl-unwind-to-safe (before &optional end)
3104 ;; if BEFORE, go to the previous start-of-line on each step of unwinding
3105 (let ((pos (point)) opos)
3106 (setq opos pos)
3107 (while (and pos (get-text-property pos 'syntax-type))
3108 (setq pos (previous-single-property-change pos 'syntax-type))
3109 (if pos
3110 (if before
3111 (progn
3112 (goto-char (cperl-1- pos))
3113 (beginning-of-line)
3114 (setq pos (point)))
3115 (goto-char (setq pos (cperl-1- pos))))
3116 ;; Up to the start
3117 (goto-char (point-min))))
6c389151
SM
3118 ;; Skip empty lines
3119 (and (looking-at "\n*=")
3120 (/= 0 (skip-chars-backward "\n"))
3121 (forward-char))
3122 (setq pos (point))
5bd52f0e
RS
3123 (if end
3124 ;; Do the same for end, going small steps
3125 (progn
3126 (while (and end (get-text-property end 'syntax-type))
3127 (setq pos end
3128 end (next-single-property-change end 'syntax-type)))
3129 (or end pos)))))
3130
6c389151
SM
3131(defvar cperl-nonoverridable-face)
3132(defvar font-lock-function-name-face)
3133(defvar font-lock-comment-face)
3134
db133cb6 3135(defun cperl-find-pods-heres (&optional min max non-inter end ignore-max)
f83d2997 3136 "Scans the buffer for hard-to-parse Perl constructions.
5c8b7eaf
SS
3137If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
3138the sections using `cperl-pod-head-face', `cperl-pod-face',
f83d2997
KH
3139`cperl-here-face'."
3140 (interactive)
db133cb6
RS
3141 (or min (setq min (point-min)
3142 cperl-syntax-state nil
3143 cperl-syntax-done-to min))
f83d2997 3144 (or max (setq max (point-max)))
5bd52f0e 3145 (let* (face head-face here-face b e bb tag qtag b1 e1 argument i c tail tb
6c389151
SM
3146 is-REx is-x-REx REx-comment-start REx-comment-end was-comment i2
3147 (cperl-pod-here-fontify (eval cperl-pod-here-fontify)) go tmpend
db133cb6
RS
3148 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
3149 (modified (buffer-modified-p))
3150 (after-change-functions nil)
3151 (use-syntax-state (and cperl-syntax-state
3152 (>= min (car cperl-syntax-state))))
3153 (state-point (if use-syntax-state
3154 (car cperl-syntax-state)
3155 (point-min)))
3156 (state (if use-syntax-state
3157 (cdr cperl-syntax-state)))
6c389151
SM
3158 ;; (st-l '(nil)) (err-l '(nil)) ; Would overwrite - propagates from a function call to a function call!
3159 (st-l (list nil)) (err-l (list nil))
db133cb6
RS
3160 ;; Somehow font-lock may be not loaded yet...
3161 (font-lock-string-face (if (boundp 'font-lock-string-face)
3162 font-lock-string-face
3163 'font-lock-string-face))
5bd52f0e
RS
3164 (font-lock-constant-face (if (boundp 'font-lock-constant-face)
3165 font-lock-constant-face
3166 'font-lock-constant-face))
5c8b7eaf 3167 (font-lock-function-name-face
5bd52f0e
RS
3168 (if (boundp 'font-lock-function-name-face)
3169 font-lock-function-name-face
3170 'font-lock-function-name-face))
6c389151
SM
3171 (font-lock-comment-face
3172 (if (boundp 'font-lock-comment-face)
3173 font-lock-comment-face
3174 'font-lock-comment-face))
3175 (cperl-nonoverridable-face
5bd52f0e
RS
3176 (if (boundp 'cperl-nonoverridable-face)
3177 cperl-nonoverridable-face
3178 'cperl-nonoverridable-face))
5c8b7eaf 3179 (stop-point (if ignore-max
db133cb6
RS
3180 (point-max)
3181 max))
3182 (search
3183 (concat
6c389151 3184 "\\(\\`\n?\\|^\n\\)="
db133cb6
RS
3185 "\\|"
3186 ;; One extra () before this:
5c8b7eaf 3187 "<<"
5bd52f0e 3188 "\\(" ; 1 + 1
db133cb6 3189 ;; First variant "BLAH" or just ``.
6c389151
SM
3190 "[ \t]*" ; Yes, whitespace is allowed!
3191 "\\([\"'`]\\)" ; 2 + 1 = 3
5bd52f0e 3192 "\\([^\"'`\n]*\\)" ; 3 + 1
db133cb6
RS
3193 "\\3"
3194 "\\|"
5bd52f0e
RS
3195 ;; Second variant: Identifier or \ID or empty
3196 "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3197 ;; Do not have <<= or << 30 or <<30 or << $blah.
3198 ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3199 "\\(\\)" ; To preserve count of pars :-( 6 + 1
db133cb6
RS
3200 "\\)"
3201 "\\|"
3202 ;; 1+6 extra () before this:
3203 "^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3204 (if cperl-use-syntax-table-text-property
3205 (concat
3206 "\\|"
3207 ;; 1+6+2=9 extra () before this:
5bd52f0e 3208 "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
db133cb6
RS
3209 "\\|"
3210 ;; 1+6+2+1=10 extra () before this:
5bd52f0e 3211 "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
db133cb6
RS
3212 "\\|"
3213 ;; 1+6+2+1+1=11 extra () before this:
3214 "\\<sub\\>[ \t]*\\([a-zA-Z_:'0-9]+[ \t]*\\)?\\(([^()]*)\\)"
3215 "\\|"
3216 ;; 1+6+2+1+1+2=13 extra () before this:
3217 "\\$\\(['{]\\)"
3218 "\\|"
3219 ;; 1+6+2+1+1+2+1=14 extra () before this:
3220 "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'"
3221 ;; 1+6+2+1+1+2+1+1=15 extra () before this:
3222 "\\|"
6c389151
SM
3223 "__\\(END\\|DATA\\)__"
3224 ;; 1+6+2+1+1+2+1+1+1=16 extra () before this:
3225 "\\|"
3226 "\\\\\\(['`\"]\\)"
db133cb6
RS
3227 )
3228 ""))))
f83d2997
KH
3229 (unwind-protect
3230 (progn
3231 (save-excursion
3232 (or non-inter
3233 (message "Scanning for \"hard\" Perl constructions..."))
db133cb6 3234 (and cperl-pod-here-fontify
f83d2997
KH
3235 ;; We had evals here, do not know why...
3236 (setq face cperl-pod-face
3237 head-face cperl-pod-head-face
3238 here-face cperl-here-face))
5c8b7eaf 3239 (remove-text-properties min max
5bd52f0e 3240 '(syntax-type t in-pod t syntax-table t
6c389151
SM
3241 cperl-postpone t
3242 syntax-subtype t
3243 rear-nonsticky t
3244 indentable t))
f83d2997
KH
3245 ;; Need to remove face as well...
3246 (goto-char min)
db133cb6
RS
3247 (and (eq system-type 'emx)
3248 (looking-at "extproc[ \t]") ; Analogue of #!
5c8b7eaf 3249 (cperl-commentify min
db133cb6
RS
3250 (save-excursion (end-of-line) (point))
3251 nil))
3252 (while (and
3253 (< (point) max)
3254 (re-search-forward search max t))
5bd52f0e 3255 (setq tmpend nil) ; Valid for most cases
5c8b7eaf 3256 (cond
f83d2997 3257 ((match-beginning 1) ; POD section
6c389151
SM
3258 ;; "\\(\\`\n?\\|^\n\\)="
3259 (if (looking-at "cut\\>")
5bd52f0e
RS
3260 (if ignore-max
3261 nil ; Doing a chunk only
f83d2997
KH
3262 (message "=cut is not preceded by a POD section")
3263 (or (car err-l) (setcar err-l (point))))
3264 (beginning-of-line)
5c8b7eaf
SS
3265
3266 (setq b (point)
5bd52f0e
RS
3267 bb b
3268 tb (match-beginning 0)
3269 b1 nil) ; error condition
db133cb6
RS
3270 ;; We do not search to max, since we may be called from
3271 ;; some hook of fontification, and max is random
6c389151 3272 (or (re-search-forward "^\n=cut\\>" stop-point 'toend)
f83d2997 3273 (progn
6c389151
SM
3274 (goto-char b)
3275 (if (re-search-forward "\n=cut\\>" stop-point 'toend)
3276 (progn
3277 (message "=cut is not preceded by an empty line")
3278 (setq b1 t)
3279 (or (car err-l) (setcar err-l b))))))
f83d2997
KH
3280 (beginning-of-line 2) ; An empty line after =cut is not POD!
3281 (setq e (point))
db133cb6 3282 (and (> e max)
6c389151
SM
3283 (progn
3284 (remove-text-properties
3285 max e '(syntax-type t in-pod t syntax-table t
3286 cperl-postpone t
3287 syntax-subtype t
3288 rear-nonsticky t
3289 indentable t))
3290 (setq tmpend tb)))
f83d2997 3291 (put-text-property b e 'in-pod t)
6c389151 3292 (put-text-property b e 'syntax-type 'in-pod)
f83d2997
KH
3293 (goto-char b)
3294 (while (re-search-forward "\n\n[ \t]" e t)
3295 ;; We start 'pod 1 char earlier to include the preceding line
3296 (beginning-of-line)
3297 (put-text-property (cperl-1- b) (point) 'syntax-type 'pod)
5efe6a56
SM
3298 (cperl-put-do-not-fontify b (point) t)
3299 ;; mark the non-literal parts as PODs
3300 (if cperl-pod-here-fontify
3301 (cperl-postpone-fontification b (point) 'face face t))
f83d2997
KH
3302 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
3303 (beginning-of-line)
3304 (setq b (point)))
3305 (put-text-property (cperl-1- (point)) e 'syntax-type 'pod)
5efe6a56
SM
3306 (cperl-put-do-not-fontify (point) e t)
3307 (if cperl-pod-here-fontify
3308 (progn
3309 ;; mark the non-literal parts as PODs
3310 (cperl-postpone-fontification (point) e 'face face t)
3311 (goto-char bb)
3312 (if (looking-at
3313 "=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
3314 ;; mark the headers
3315 (cperl-postpone-fontification
3316 (match-beginning 1) (match-end 1)
6c389151
SM
3317 'face head-face))
3318 (while (re-search-forward
3319 ;; One paragraph
3320 "^\n=[a-zA-Z0-9_]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
3321 e 'toend)
3322 ;; mark the headers
3323 (cperl-postpone-fontification
5efe6a56
SM
3324 (match-beginning 1) (match-end 1)
3325 'face head-face))))
f83d2997
KH
3326 (cperl-commentify bb e nil)
3327 (goto-char e)
3328 (or (eq e (point-max))
6c389151 3329 (forward-char -1)))) ; Prepare for immediate pod start.
f83d2997
KH
3330 ;; Here document
3331 ;; We do only one here-per-line
5bd52f0e 3332 ;; ;; One extra () before this:
5c8b7eaf 3333 ;;"<<"
5bd52f0e
RS
3334 ;; "\\(" ; 1 + 1
3335 ;; ;; First variant "BLAH" or just ``.
3336 ;; "\\([\"'`]\\)" ; 2 + 1
3337 ;; "\\([^\"'`\n]*\\)" ; 3 + 1
3338 ;; "\\3"
3339 ;; "\\|"
3340 ;; ;; Second variant: Identifier or \ID or empty
3341 ;; "\\\\?\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)" ; 4 + 1, 5 + 1
3342 ;; ;; Do not have <<= or << 30 or <<30 or << $blah.
3343 ;; ;; "\\([^= \t0-9$@%&]\\|[ \t]+[^ \t\n0-9$@%&]\\)" ; 6 + 1
3344 ;; "\\(\\)" ; To preserve count of pars :-( 6 + 1
3345 ;; "\\)"
f83d2997
KH
3346 ((match-beginning 2) ; 1 + 1
3347 ;; Abort in comment:
3348 (setq b (point))
3349 (setq state (parse-partial-sexp state-point b nil nil state)
5bd52f0e
RS
3350 state-point b
3351 tb (match-beginning 0)
3352 i (or (nth 3 state) (nth 4 state)))
5c8b7eaf 3353 (if i
5bd52f0e
RS
3354 (setq c t)
3355 (setq c (and
3356 (match-beginning 5)
3357 (not (match-beginning 6)) ; Empty
3358 (looking-at
3359 "[ \t]*[=0-9$@%&(]"))))
3360 (if c ; Not here-doc
3361 nil ; Skip it.
f83d2997
KH
3362 (if (match-beginning 5) ;4 + 1
3363 (setq b1 (match-beginning 5) ; 4 + 1
3364 e1 (match-end 5)) ; 4 + 1
3365 (setq b1 (match-beginning 4) ; 3 + 1
3366 e1 (match-end 4))) ; 3 + 1
3367 (setq tag (buffer-substring b1 e1)
3368 qtag (regexp-quote tag))
5c8b7eaf 3369 (cond (cperl-pod-here-fontify
5bd52f0e
RS
3370 ;; Highlight the starting delimiter
3371 (cperl-postpone-fontification b1 e1 'face font-lock-constant-face)
3372 (cperl-put-do-not-fontify b1 e1 t)))
f83d2997
KH
3373 (forward-line)
3374 (setq b (point))
db133cb6
RS
3375 ;; We do not search to max, since we may be called from
3376 ;; some hook of fontification, and max is random
5c8b7eaf 3377 (cond ((re-search-forward (concat "^" qtag "$")
db133cb6 3378 stop-point 'toend)
5c8b7eaf 3379 (if cperl-pod-here-fontify
f83d2997 3380 (progn
5bd52f0e 3381 ;; Highlight the ending delimiter
5c8b7eaf 3382 (cperl-postpone-fontification (match-beginning 0) (match-end 0)
883212ce 3383 'face font-lock-constant-face)
5bd52f0e
RS
3384 (cperl-put-do-not-fontify b (match-end 0) t)
3385 ;; Highlight the HERE-DOC
5c8b7eaf 3386 (cperl-postpone-fontification b (match-beginning 0)
f83d2997
KH
3387 'face here-face)))
3388 (setq e1 (cperl-1+ (match-end 0)))
5c8b7eaf 3389 (put-text-property b (match-beginning 0)
f83d2997
KH
3390 'syntax-type 'here-doc)
3391 (put-text-property (match-beginning 0) e1
3392 'syntax-type 'here-doc-delim)
3393 (put-text-property b e1
3394 'here-doc-group t)
3395 (cperl-commentify b e1 nil)
5bd52f0e
RS
3396 (cperl-put-do-not-fontify b (match-end 0) t)
3397 (if (> e1 max)
3398 (setq tmpend tb)))
f83d2997
KH
3399 (t (message "End of here-document `%s' not found." tag)
3400 (or (car err-l) (setcar err-l b))))))
3401 ;; format
3402 ((match-beginning 8)
3403 ;; 1+6=7 extra () before this:
3404 ;;"^[ \t]*\\(format\\)[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$"
3405 (setq b (point)
3406 name (if (match-beginning 8) ; 7 + 1
3407 (buffer-substring (match-beginning 8) ; 7 + 1
3408 (match-end 8)) ; 7 + 1
5bd52f0e
RS
3409 "")
3410 tb (match-beginning 0))
f83d2997 3411 (setq argument nil)
5c8b7eaf 3412 (if cperl-pod-here-fontify
f83d2997
KH
3413 (while (and (eq (forward-line) 0)
3414 (not (looking-at "^[.;]$")))
3415 (cond
3416 ((looking-at "^#")) ; Skip comments
3417 ((and argument ; Skip argument multi-lines
5c8b7eaf 3418 (looking-at "^[ \t]*{"))
f83d2997
KH
3419 (forward-sexp 1)
3420 (setq argument nil))
3421 (argument ; Skip argument lines
3422 (setq argument nil))
3423 (t ; Format line
3424 (setq b1 (point))
3425 (setq argument (looking-at "^[^\n]*[@^]"))
3426 (end-of-line)
5bd52f0e 3427 ;; Highlight the format line
5c8b7eaf 3428 (cperl-postpone-fontification b1 (point)
f83d2997
KH
3429 'face font-lock-string-face)
3430 (cperl-commentify b1 (point) nil)
5bd52f0e 3431 (cperl-put-do-not-fontify b1 (point) t))))
db133cb6
RS
3432 ;; We do not search to max, since we may be called from
3433 ;; some hook of fontification, and max is random
3434 (re-search-forward "^[.;]$" stop-point 'toend))
f83d2997 3435 (beginning-of-line)
5bd52f0e 3436 (if (looking-at "^\\.$") ; ";" is not supported yet
f83d2997 3437 (progn
5bd52f0e
RS
3438 ;; Highlight the ending delimiter
3439 (cperl-postpone-fontification (point) (+ (point) 2)
f83d2997
KH
3440 'face font-lock-string-face)
3441 (cperl-commentify (point) (+ (point) 2) nil)
5bd52f0e 3442 (cperl-put-do-not-fontify (point) (+ (point) 2) t))
f83d2997
KH
3443 (message "End of format `%s' not found." name)
3444 (or (car err-l) (setcar err-l b)))
3445 (forward-line)
5bd52f0e
RS
3446 (if (> (point) max)
3447 (setq tmpend tb))
db133cb6 3448 (put-text-property b (point) 'syntax-type 'format))
f83d2997
KH
3449 ;; Regexp:
3450 ((or (match-beginning 10) (match-beginning 11))
3451 ;; 1+6+2=9 extra () before this:
5bd52f0e 3452 ;; "\\<\\(q[wxqr]?\\|[msy]\\|tr\\)\\>"
f83d2997 3453 ;; "\\|"
5bd52f0e 3454 ;; "\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
f83d2997
KH
3455 (setq b1 (if (match-beginning 10) 10 11)
3456 argument (buffer-substring
3457 (match-beginning b1) (match-end b1))
3458 b (point)
3459 i b
3460 c (char-after (match-beginning b1))
3461 bb (char-after (1- (match-beginning b1))) ; tmp holder
5bd52f0e
RS
3462 ;; bb == "Not a stringy"
3463 bb (if (eq b1 10) ; user variables/whatever
6c389151
SM
3464 (or
3465 (memq bb '(?\$ ?\@ ?\% ?\* ?\#)) ; $#y
3466 (and (eq bb ?-) (eq c ?s)) ; -s file test
3467 (and (eq bb ?\&)
3468 (not (eq (char-after ; &&m/blah/
3469 (- (match-beginning b1) 2))
5bd52f0e
RS
3470 ?\&))))
3471 ;; <file> or <$file>
3472 (and (eq c ?\<)
6c389151 3473 ;; Do not stringify <FH>, <$fh> :
5bd52f0e 3474 (save-match-data
5c8b7eaf 3475 (looking-at
6c389151 3476 "\\$?\\([_a-zA-Z:][_a-zA-Z0-9:]*\\)?>"))))
5bd52f0e 3477 tb (match-beginning 0))
db133cb6
RS
3478 (goto-char (match-beginning b1))
3479 (cperl-backward-to-noncomment (point-min))
f83d2997 3480 (or bb
5bd52f0e 3481 (if (eq b1 11) ; bare /blah/ or ?blah? or <foo>
f83d2997 3482 (setq argument ""
db133cb6
RS
3483 bb ; Not a regexp?
3484 (progn
5c8b7eaf 3485 (not
db133cb6
RS
3486 ;; What is below: regexp-p?
3487 (and
3488 (or (memq (preceding-char)
5bd52f0e 3489 (append (if (memq c '(?\? ?\<))
db133cb6 3490 ;; $a++ ? 1 : 2
5bd52f0e
RS
3491 "~{(=|&*!,;:"
3492 "~{(=|&+-*!,;:") nil))
db133cb6
RS
3493 (and (eq (preceding-char) ?\})
3494 (cperl-after-block-p (point-min)))
3495 (and (eq (char-syntax (preceding-char)) ?w)
3496 (progn
3497 (forward-sexp -1)
6c389151
SM
3498;; After these keywords `/' starts a RE. One should add all the
3499;; functions/builtins which expect an argument, but ...
5bd52f0e
RS
3500 (if (eq (preceding-char) ?-)
3501 ;; -d ?foo? is a RE
3502 (looking-at "[a-zA-Z]\\>")
029cb4d5
SM
3503 (and
3504 (not (memq (preceding-char)
3505 '(?$ ?@ ?& ?%)))
3506 (looking-at
3507 "\\(while\\|if\\|unless\\|until\\|and\\|or\\|not\\|xor\\|split\\|grep\\|map\\|print\\)\\>")))))
db133cb6
RS
3508 (and (eq (preceding-char) ?.)
3509 (eq (char-after (- (point) 2)) ?.))
3510 (bobp))
3511 ;; m|blah| ? foo : bar;
3512 (not
3513 (and (eq c ?\?)
5c8b7eaf 3514 cperl-use-syntax-table-text-property
db133cb6
RS
3515 (not (bobp))
3516 (progn
3517 (forward-char -1)
3518 (looking-at "\\s|")))))))
3519 b (1- b))
3520 ;; s y tr m
3521 ;; Check for $a->y
3522 (if (and (eq (preceding-char) ?>)
3523 (eq (char-after (- (point) 2)) ?-))
3524 ;; Not a regexp
3525 (setq bb t))))
5c8b7eaf 3526 (or bb (setq state (parse-partial-sexp
f83d2997
KH
3527 state-point b nil nil state)
3528 state-point b))
3529 (goto-char b)
3530 (if (or bb (nth 3 state) (nth 4 state))
3531 (goto-char i)
6c389151 3532 ;; Skip whitespace and comments...
f83d2997
KH
3533 (if (looking-at "[ \t\n\f]+\\(#[^\n]*\n[ \t\n\f]*\\)+")
3534 (goto-char (match-end 0))
3535 (skip-chars-forward " \t\n\f"))
6c389151
SM
3536 (if (> (point) b)
3537 (put-text-property b (point) 'syntax-type 'prestring))
f83d2997
KH
3538 ;; qtag means two-arg matcher, may be reset to
3539 ;; 2 or 3 later if some special quoting is needed.
3540 ;; e1 means matching-char matcher.
3541 (setq b (point)
5bd52f0e
RS
3542 ;; has 2 args
3543 i2 (string-match "^\\([sy]\\|tr\\)$" argument)
db133cb6
RS
3544 ;; We do not search to max, since we may be called from
3545 ;; some hook of fontification, and max is random
3546 i (cperl-forward-re stop-point end
5bd52f0e 3547 i2
db133cb6 3548 t st-l err-l argument)
5bd52f0e
RS
3549 ;; Note that if `go', then it is considered as 1-arg
3550 b1 (nth 1 i) ; start of the second part
5c8b7eaf 3551 tag (nth 2 i) ; ender-char, true if second part
5bd52f0e 3552 ; is with matching chars []
f83d2997
KH
3553 go (nth 4 i) ; There is a 1-char part after the end
3554 i (car i) ; intermediate point
5c8b7eaf 3555 e1 (point) ; end
5bd52f0e 3556 ;; Before end of the second part if non-matching: ///
5c8b7eaf 3557 tail (if (and i (not tag))
5bd52f0e
RS
3558 (1- e1))
3559 e (if i i e1) ; end of the first part
6c389151
SM
3560 qtag nil ; need to preserve backslashitis
3561 is-x-REx nil) ; REx has //x modifier
f83d2997
KH
3562 ;; Commenting \\ is dangerous, what about ( ?
3563 (and i tail
3564 (eq (char-after i) ?\\)
5bd52f0e 3565 (setq qtag t))
6c389151
SM
3566 (if (looking-at "\\sw*x") ; qr//x
3567 (setq is-x-REx t))
f83d2997 3568 (if (null i)
5bd52f0e 3569 ;; Considered as 1arg form
f83d2997
KH
3570 (progn
3571 (cperl-commentify b (point) t)
5bd52f0e 3572 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
3573 (if (or is-x-REx
3574 ;; ignore other text properties:
3575 (string-match "^qw$" argument))
3576 (put-text-property b (point) 'indentable t))
5bd52f0e
RS
3577 (and go
3578 (setq e1 (cperl-1+ e1))
3579 (or (eobp)
3580 (forward-char 1))))
f83d2997
KH
3581 (cperl-commentify b i t)
3582 (if (looking-at "\\sw*e") ; s///e
3583 (progn
3584 (and
3585 ;; silent:
5bd52f0e 3586 (cperl-find-pods-heres b1 (1- (point)) t end)
f83d2997
KH
3587 ;; Error
3588 (goto-char (1+ max)))
5bd52f0e 3589 (if (and tag (eq (preceding-char) ?\>))
f83d2997
KH
3590 (progn
3591 (cperl-modify-syntax-type (1- (point)) cperl-st-ket)
5bd52f0e 3592 (cperl-modify-syntax-type i cperl-st-bra)))
6c389151
SM
3593 (put-text-property b i 'syntax-type 'string)
3594 (if is-x-REx
3595 (put-text-property b i 'indentable t)))
5bd52f0e
RS
3596 (cperl-commentify b1 (point) t)
3597 (put-text-property b (point) 'syntax-type 'string)
6c389151
SM
3598 (if is-x-REx
3599 (put-text-property b i 'indentable t))
5bd52f0e 3600 (if qtag
db133cb6 3601 (cperl-modify-syntax-type (1+ i) cperl-st-punct))
f83d2997 3602 (setq tail nil)))
5bd52f0e 3603 ;; Now: tail: if the second part is non-matching without ///e
f83d2997
KH
3604 (if (eq (char-syntax (following-char)) ?w)
3605 (progn
3606 (forward-word 1) ; skip modifiers s///s
5bd52f0e 3607 (if tail (cperl-commentify tail (point) t))
6c389151
SM
3608 (cperl-postpone-fontification
3609 e1 (point) 'face 'cperl-nonoverridable-face)))
5bd52f0e
RS
3610 ;; Check whether it is m// which means "previous match"
3611 ;; and highlight differently
6c389151
SM
3612 (setq is-REx
3613 (and (string-match "^\\([sm]?\\|qr\\)$" argument)
3614 (or (not (= (length argument) 0))
3615 (not (eq c ?\<)))))
3616 (if (and is-REx
3617 (eq e (+ 2 b))
5bd52f0e
RS
3618 ;; split // *is* using zero-pattern
3619 (save-excursion
3620 (condition-case nil
3621 (progn
3622 (goto-char tb)
3623 (forward-sexp -1)
3624 (not (looking-at "split\\>")))
3625 (error t))))
5c8b7eaf 3626 (cperl-postpone-fontification
5bd52f0e
RS
3627 b e 'face font-lock-function-name-face)
3628 (if (or i2 ; Has 2 args
3629 (and cperl-fontify-m-as-s
3630 (or
3631 (string-match "^\\(m\\|qr\\)$" argument)
3632 (and (eq 0 (length argument))
3633 (not (eq ?\< (char-after b)))))))
3634 (progn
5c8b7eaf 3635 (cperl-postpone-fontification
5bd52f0e 3636 b (cperl-1+ b) 'face font-lock-constant-face)
5c8b7eaf 3637 (cperl-postpone-fontification
6c389151
SM
3638 (1- e) e 'face font-lock-constant-face)))
3639 (if (and is-REx cperl-regexp-scan)
3640 ;; Process RExen better
3641 (save-excursion
3642 (goto-char (1+ b))
3643 (while
3644 (and (< (point) e)
3645 (re-search-forward
3646 (if is-x-REx
3647 (if (eq (char-after b) ?\#)
3648 "\\((\\?\\\\#\\)\\|\\(\\\\#\\)"
3649 "\\((\\?#\\)\\|\\(#\\)")
3650 (if (eq (char-after b) ?\#)
3651 "\\((\\?\\\\#\\)"
3652 "\\((\\?#\\)"))
3653 (1- e) 'to-end))
3654 (goto-char (match-beginning 0))
3655 (setq REx-comment-start (point)
3656 was-comment t)
3657 (if (save-excursion
3658 (and
3659 ;; XXX not working if outside delimiter is #
3660 (eq (preceding-char) ?\\)
3661 (= (% (skip-chars-backward "$\\\\") 2) -1)))
3662 ;; Not a comment, avoid loop:
3663 (progn (setq was-comment nil)
3664 (forward-char 1))
3665 (if (match-beginning 2)
3666 (progn
3667 (beginning-of-line 2)
3668 (if (> (point) e)
3669 (goto-char (1- e))))
3670 ;; Works also if the outside delimiters are ().
3671 (or (search-forward ")" (1- e) 'toend)
3672 (message
3673 "Couldn't find end of (?#...)-comment in a REx, pos=%s"
3674 REx-comment-start))))
3675 (if (>= (point) e)
3676 (goto-char (1- e)))
3677 (if was-comment
3678 (progn
3679 (setq REx-comment-end (point))
3680 (cperl-commentify
3681 REx-comment-start REx-comment-end nil)
3682 (cperl-postpone-fontification
3683 REx-comment-start REx-comment-end
3684 'face font-lock-comment-face))))))
3685 (if (and is-REx is-x-REx)
3686 (put-text-property (1+ b) (1- e)
3687 'syntax-subtype 'x-REx)))
5bd52f0e
RS
3688 (if i2
3689 (progn
5c8b7eaf 3690 (cperl-postpone-fontification
5bd52f0e
RS
3691 (1- e1) e1 'face font-lock-constant-face)
3692 (if (assoc (char-after b) cperl-starters)
5c8b7eaf 3693 (cperl-postpone-fontification
5bd52f0e
RS
3694 b1 (1+ b1) 'face font-lock-constant-face))))
3695 (if (> (point) max)
3696 (setq tmpend tb))))
f83d2997
KH
3697 ((match-beginning 13) ; sub with prototypes
3698 (setq b (match-beginning 0))
3699 (if (memq (char-after (1- b))
3700 '(?\$ ?\@ ?\% ?\& ?\*))
3701 nil
5c8b7eaf 3702 (setq state (parse-partial-sexp
5bd52f0e
RS
3703 state-point b nil nil state)
3704 state-point b)
f83d2997
KH
3705 (if (or (nth 3 state) (nth 4 state))
3706 nil
3707 ;; Mark as string
3708 (cperl-commentify (match-beginning 13) (match-end 13) t))
3709 (goto-char (match-end 0))))
3710 ;; 1+6+2+1+1+2=13 extra () before this:
3711 ;; "\\$\\(['{]\\)"
3712 ((and (match-beginning 14)
db133cb6 3713 (eq (preceding-char) ?\')) ; $'
f83d2997 3714 (setq b (1- (point))
5c8b7eaf 3715 state (parse-partial-sexp
f83d2997
KH
3716 state-point (1- b) nil nil state)
3717 state-point (1- b))
3718 (if (nth 3 state) ; in string
3719 (cperl-modify-syntax-type (1- b) cperl-st-punct))
3720 (goto-char (1+ b)))
3721 ;; 1+6+2+1+1+2=13 extra () before this:
3722 ;; "\\$\\(['{]\\)"
3723 ((match-beginning 14) ; ${
3724 (setq bb (match-beginning 0))
3725 (cperl-modify-syntax-type bb cperl-st-punct))
3726 ;; 1+6+2+1+1+2+1=14 extra () before this:
3727 ;; "\\(\\<sub[ \t\n\f]+\\|[&*$@%]\\)[a-zA-Z0-9_]*'")
3728 ((match-beginning 15) ; old $abc'efg syntax
3729 (setq bb (match-end 0)
3730 b (match-beginning 0)
5c8b7eaf 3731 state (parse-partial-sexp
f83d2997
KH
3732 state-point b nil nil state)
3733 state-point b)
3734 (if (nth 3 state) ; in string
3735 nil
3736 (put-text-property (1- bb) bb 'syntax-table cperl-st-word))
3737 (goto-char bb))
3738 ;; 1+6+2+1+1+2+1+1=15 extra () before this:
3739 ;; "__\\(END\\|DATA\\)__"
6c389151 3740 ((match-beginning 16) ; __END__, __DATA__
f83d2997
KH
3741 (setq bb (match-end 0)
3742 b (match-beginning 0)
5c8b7eaf 3743 state (parse-partial-sexp
f83d2997
KH
3744 state-point b nil nil state)
3745 state-point b)
3746 (if (or (nth 3 state) (nth 4 state))
3747 nil
3748 ;; (put-text-property b (1+ bb) 'syntax-type 'pod) ; Cheat
3749 (cperl-commentify b bb nil)
3750 (setq end t))
6c389151
SM
3751 (goto-char bb))
3752 ((match-beginning 17) ; "\\\\\\(['`\"]\\)"
3753 (setq bb (match-end 0)
3754 b (match-beginning 0))
3755 (goto-char b)
3756 (skip-chars-backward "\\\\")
3757 ;;;(setq i2 (= (% (skip-chars-backward "\\\\") 2) -1))
3758 (setq state (parse-partial-sexp
3759 state-point b nil nil state)
3760 state-point b)
3761 (if (or (nth 3 state) (nth 4 state) )
3762 nil
3763 (cperl-modify-syntax-type b cperl-st-punct))
3764 (goto-char bb))
3765 (t (error "Error in regexp of the sniffer")))
db133cb6 3766 (if (> (point) stop-point)
f83d2997 3767 (progn
5c8b7eaf 3768 (if end
f83d2997
KH
3769 (message "Garbage after __END__/__DATA__ ignored")
3770 (message "Unbalanced syntax found while scanning")
3771 (or (car err-l) (setcar err-l b)))
db133cb6
RS
3772 (goto-char stop-point))))
3773 (setq cperl-syntax-state (cons state-point state)
5bd52f0e 3774 cperl-syntax-done-to (or tmpend (max (point) max))))
f83d2997 3775 (if (car err-l) (goto-char (car err-l))
db133cb6
RS
3776 (or non-inter
3777 (message "Scanning for \"hard\" Perl constructions... done"))))
f83d2997
KH
3778 (and (buffer-modified-p)
3779 (not modified)
3780 (set-buffer-modified-p nil))
3781 (set-syntax-table cperl-mode-syntax-table))
3782 (car err-l)))
3783
3784(defun cperl-backward-to-noncomment (lim)
3785 ;; Stops at lim or after non-whitespace that is not in comment
5bd52f0e 3786 (let (stop p pr)
f83d2997
KH
3787 (while (and (not stop) (> (point) (or lim 1)))
3788 (skip-chars-backward " \t\n\f" lim)
3789 (setq p (point))
3790 (beginning-of-line)
5bd52f0e
RS
3791 (if (memq (setq pr (get-text-property (point) 'syntax-type))
3792 '(pod here-doc here-doc-delim))
3793 (cperl-unwind-to-safe nil)
f83d2997
KH
3794 (if (or (looking-at "^[ \t]*\\(#\\|$\\)")
3795 (progn (cperl-to-comment-or-eol) (bolp)))
3796 nil ; Only comment, skip
3797 ;; Else
3798 (skip-chars-backward " \t")
3799 (if (< p (point)) (goto-char p))
5bd52f0e 3800 (setq stop t))))))
f83d2997
KH
3801
3802(defun cperl-after-block-p (lim)
3803 ;; We suppose that the preceding char is }.
3804 (save-excursion
3805 (condition-case nil
3806 (progn
3807 (forward-sexp -1)
3808 (cperl-backward-to-noncomment lim)
bab27c0c
RS
3809 (or (eq (point) lim)
3810 (eq (preceding-char) ?\) ) ; if () {} sub f () {}
db133cb6
RS
3811 (if (eq (char-syntax (preceding-char)) ?w) ; else {}
3812 (save-excursion
3813 (forward-sexp -1)
6c389151 3814 (or (looking-at "\\(else\\|grep\\|map\\|BEGIN\\|END\\|CHECK\\|INIT\\)\\>")
db133cb6
RS
3815 ;; sub f {}
3816 (progn
3817 (cperl-backward-to-noncomment lim)
3818 (and (eq (char-syntax (preceding-char)) ?w)
3819 (progn
3820 (forward-sexp -1)
3821 (looking-at "sub\\>"))))))
3822 (cperl-after-expr-p lim))))
f83d2997
KH
3823 (error nil))))
3824
3825(defun cperl-after-expr-p (&optional lim chars test)
029cb4d5 3826 "Return true if the position is good for start of expression.
f83d2997
KH
3827TEST is the expression to evaluate at the found position. If absent,
3828CHARS is a string that contains good characters to have before us (however,
3829`}' is treated \"smartly\" if it is not in the list)."
5c8b7eaf 3830 (let (stop p
f83d2997
KH
3831 (lim (or lim (point-min))))
3832 (save-excursion
3833 (while (and (not stop) (> (point) lim))
3834 (skip-chars-backward " \t\n\f" lim)
3835 (setq p (point))
3836 (beginning-of-line)
3837 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
5bd52f0e 3838 ;; Else: last iteration, or a label
5c8b7eaf 3839 (cperl-to-comment-or-eol)
f83d2997
KH
3840 (skip-chars-backward " \t")
3841 (if (< p (point)) (goto-char p))
5bd52f0e
RS
3842 (setq p (point))
3843 (if (and (eq (preceding-char) ?:)
3844 (progn
3845 (forward-char -1)
3846 (skip-chars-backward " \t\n\f" lim)
3847 (eq (char-syntax (preceding-char)) ?w)))
3848 (forward-sexp -1) ; Possibly label. Skip it
3849 (goto-char p)
3850 (setq stop t))))
bab27c0c
RS
3851 (or (bobp) ; ???? Needed
3852 (eq (point) lim)
029cb4d5 3853 (looking-at "[ \t]*__\\(END\\|DATA\\)__") ; After this anything goes
f83d2997
KH
3854 (progn
3855 (if test (eval test)
3856 (or (memq (preceding-char) (append (or chars "{;") nil))
3857 (and (eq (preceding-char) ?\})
3858 (cperl-after-block-p lim)))))))))
3859
3860(defun cperl-backward-to-start-of-continued-exp (lim)
3861 (if (memq (preceding-char) (append ")]}\"'`" nil))
3862 (forward-sexp -1))
3863 (beginning-of-line)
3864 (if (<= (point) lim)
3865 (goto-char (1+ lim)))
3866 (skip-chars-forward " \t"))
3867
db133cb6
RS
3868(defun cperl-after-block-and-statement-beg (lim)
3869 ;; We assume that we are after ?\}
5c8b7eaf 3870 (and
db133cb6
RS
3871 (cperl-after-block-p lim)
3872 (save-excursion
3873 (forward-sexp -1)
3874 (cperl-backward-to-noncomment (point-min))
3875 (or (bobp)
bab27c0c 3876 (eq (point) lim)
db133cb6
RS
3877 (not (= (char-syntax (preceding-char)) ?w))
3878 (progn
3879 (forward-sexp -1)
5c8b7eaf 3880 (not
db133cb6
RS
3881 (looking-at
3882 "\\(map\\|grep\\|printf?\\|system\\|exec\\|tr\\|s\\)\\>")))))))
3883
f83d2997 3884\f
f83d2997
KH
3885(defun cperl-indent-exp ()
3886 "Simple variant of indentation of continued-sexp.
5bd52f0e
RS
3887
3888Will not indent comment if it starts at `comment-indent' or looks like
3889continuation of the comment on the previous line.
db133cb6 3890
5c8b7eaf 3891If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 3892conditional/loop constructs."
f83d2997
KH
3893 (interactive)
3894 (save-excursion
3895 (let ((tmp-end (progn (end-of-line) (point))) top done)
3896 (save-excursion
3897 (beginning-of-line)
3898 (while (null done)
3899 (setq top (point))
3900 (while (= (nth 0 (parse-partial-sexp (point) tmp-end
3901 -1)) -1)
3902 (setq top (point))) ; Get the outermost parenths in line
3903 (goto-char top)
3904 (while (< (point) tmp-end)
3905 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
3906 (or (eolp) (forward-sexp 1)))
5bd52f0e
RS
3907 (if (> (point) tmp-end)
3908 (save-excursion
3909 (end-of-line)
3910 (setq tmp-end (point)))
f83d2997
KH
3911 (setq done t)))
3912 (goto-char tmp-end)
3913 (setq tmp-end (point-marker)))
db133cb6
RS
3914 (if cperl-indent-region-fix-constructs
3915 (cperl-fix-line-spacing tmp-end))
f83d2997
KH
3916 (cperl-indent-region (point) tmp-end))))
3917
5bd52f0e
RS
3918(defun cperl-fix-line-spacing (&optional end parse-data)
3919 "Improve whitespace in a conditional/loop construct.
3920Returns some position at the last line."
db133cb6
RS
3921 (interactive)
3922 (or end
3923 (setq end (point-max)))
5bd52f0e
RS
3924 (let (p pp ml have-brace ret
3925 (ee (save-excursion (end-of-line) (point)))
db133cb6
RS
3926 (cperl-indent-region-fix-constructs
3927 (or cperl-indent-region-fix-constructs 1)))
3928 (save-excursion
3929 (beginning-of-line)
5bd52f0e 3930 (setq ret (point))
5c8b7eaf 3931 ;; }? continue
5bd52f0e 3932 ;; blah; }
5c8b7eaf 3933 (if (not
5bd52f0e
RS
3934 (or (looking-at "[ \t]*\\(els\\(e\\|if\\)\\|continue\\|if\\|while\\|for\\(each\\)?\\|until\\)")
3935 (setq have-brace (save-excursion (search-forward "}" ee t)))))
3936 nil ; Do not need to do anything
db133cb6 3937 ;; Looking at:
5c8b7eaf 3938 ;; }
db133cb6
RS
3939 ;; else
3940 (if (and cperl-merge-trailing-else
3941 (looking-at
3942 "[ \t]*}[ \t]*\n[ \t\n]*\\(els\\(e\\|if\\)\\|continue\\)\\>"))
3943 (progn
3944 (search-forward "}")
3945 (setq p (point))
3946 (skip-chars-forward " \t\n")
3947 (delete-region p (point))
3948 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3949 (beginning-of-line)))
3950 ;; Looking at:
3951 ;; } else
3952 (if (looking-at "[ \t]*}\\(\t*\\|[ \t][ \t]+\\)\\<\\(els\\(e\\|if\\)\\|continue\\)\\>")
3953 (progn
3954 (search-forward "}")
3955 (delete-horizontal-space)
3956 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3957 (beginning-of-line)))
3958 ;; Looking at:
3959 ;; else {
5c8b7eaf 3960 (if (looking-at
5bd52f0e 3961 "[ \t]*}?[ \t]*\\<\\(\\els\\(e\\|if\\)\\|continue\\|unless\\|if\\|while\\|for\\(each\\)?\\|until\\)\\>\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
db133cb6
RS
3962 (progn
3963 (forward-word 1)
3964 (delete-horizontal-space)
3965 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3966 (beginning-of-line)))
3967 ;; Looking at:
3968 ;; foreach my $var
6c389151
SM
3969 (if (looking-at
3970 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)\\(\t*\\|[ \t][ \t]+\\)[^ \t\n]")
db133cb6
RS
3971 (progn
3972 (forward-word 2)
3973 (delete-horizontal-space)
3974 (insert (make-string cperl-indent-region-fix-constructs ?\ ))
3975 (beginning-of-line)))
3976 ;; Looking at:
3977 ;; foreach my $var (
6c389151
SM
3978 (if (looking-at
3979 "[ \t]*\\<for\\(each\\)?[ \t]+\\(my\\|local\\|our\\)[ \t]*\\$[_a-zA-Z0-9]+\\(\t*\\|[ \t][ \t]+\\)[^ \t\n#]")
db133cb6
RS
3980 (progn
3981 (forward-word 3)
3982 (delete-horizontal-space)
3983 (insert
3984 (make-string cperl-indent-region-fix-constructs ?\ ))
3985 (beginning-of-line)))
3986 ;; Looking at:
3987 ;; } foreach my $var () {
6c389151
SM
3988 (if (looking-at
3989 "[ \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
3990 (progn
3991 (setq ml (match-beginning 8))
3992 (re-search-forward "[({]")
3993 (forward-char -1)
3994 (setq p (point))
3995 (if (eq (following-char) ?\( )
3996 (progn
3997 (forward-sexp 1)
3998 (setq pp (point)))
3999 ;; after `else' or nothing
4000 (if ml ; after `else'
4001 (skip-chars-backward " \t\n")
4002 (beginning-of-line))
4003 (setq pp nil))
4004 ;; Now after the sexp before the brace
4005 ;; Multiline expr should be special
4006 (setq ml (and pp (save-excursion (goto-char p)
4007 (search-forward "\n" pp t))))
4008 (if (and (or (not pp) (< pp end))
4009 (looking-at "[ \t\n]*{"))
4010 (progn
5c8b7eaf 4011 (cond
db133cb6
RS
4012 ((bolp) ; Were before `{', no if/else/etc
4013 nil)
4014 ((looking-at "\\(\t*\\| [ \t]+\\){")
4015 (delete-horizontal-space)
5c8b7eaf 4016 (if (if ml
db133cb6
RS
4017 cperl-extra-newline-before-brace-multiline
4018 cperl-extra-newline-before-brace)
4019 (progn
4020 (delete-horizontal-space)
4021 (insert "\n")
5bd52f0e
RS
4022 (setq ret (point))
4023 (if (cperl-indent-line parse-data)
5c8b7eaf 4024 (progn
5bd52f0e
RS
4025 (cperl-fix-line-spacing end parse-data)
4026 (setq ret (point)))))
db133cb6
RS
4027 (insert
4028 (make-string cperl-indent-region-fix-constructs ?\ ))))
4029 ((and (looking-at "[ \t]*\n")
5c8b7eaf 4030 (not (if ml
db133cb6
RS
4031 cperl-extra-newline-before-brace-multiline
4032 cperl-extra-newline-before-brace)))
4033 (setq pp (point))
4034 (skip-chars-forward " \t\n")
4035 (delete-region pp (point))
4036 (insert
4037 (make-string cperl-indent-region-fix-constructs ?\ ))))
4038 ;; Now we are before `{'
4039 (if (looking-at "[ \t\n]*{[ \t]*[^ \t\n#]")
4040 (progn
4041 (skip-chars-forward " \t\n")
4042 (setq pp (point))
4043 (forward-sexp 1)
4044 (setq p (point))
4045 (goto-char pp)
4046 (setq ml (search-forward "\n" p t))
4047 (if (or cperl-break-one-line-blocks-when-indent ml)
4048 ;; not good: multi-line BLOCK
4049 (progn
4050 (goto-char (1+ pp))
4051 (delete-horizontal-space)
4052 (insert "\n")
5bd52f0e
RS
4053 (setq ret (point))
4054 (if (cperl-indent-line parse-data)
4055 (setq ret (cperl-fix-line-spacing end parse-data)))))))))))
db133cb6 4056 (beginning-of-line)
5bd52f0e 4057 (setq p (point) pp (save-excursion (end-of-line) (point))) ; May be different from ee.
db133cb6
RS
4058 ;; Now check whether there is a hanging `}'
4059 ;; Looking at:
4060 ;; } blah
5c8b7eaf 4061 (if (and
db133cb6 4062 cperl-fix-hanging-brace-when-indent
5bd52f0e 4063 have-brace
db133cb6
RS
4064 (not (looking-at "[ \t]*}[ \t]*\\(\\<\\(els\\(if\\|e\\)\\|continue\\|while\\|until\\)\\>\\|$\\|#\\)"))
4065 (condition-case nil
4066 (progn
4067 (up-list 1)
5c8b7eaf 4068 (if (and (<= (point) pp)
db133cb6 4069 (eq (preceding-char) ?\} )
5c8b7eaf 4070 (cperl-after-block-and-statement-beg (point-min)))
db133cb6
RS
4071 t
4072 (goto-char p)
4073 nil))
4074 (error nil)))
4075 (progn
4076 (forward-char -1)
4077 (skip-chars-backward " \t")
4078 (if (bolp)
4079 ;; `}' was the first thing on the line, insert NL *after* it.
4080 (progn
5bd52f0e 4081 (cperl-indent-line parse-data)
db133cb6
RS
4082 (search-forward "}")
4083 (delete-horizontal-space)
4084 (insert "\n"))
4085 (delete-horizontal-space)
4086 (or (eq (preceding-char) ?\;)
4087 (bolp)
4088 (and (eq (preceding-char) ?\} )
4089 (cperl-after-block-p (point-min)))
4090 (insert ";"))
5bd52f0e
RS
4091 (insert "\n")
4092 (setq ret (point)))
4093 (if (cperl-indent-line parse-data)
4094 (setq ret (cperl-fix-line-spacing end parse-data)))
4095 (beginning-of-line)))))
4096 ret))
4097
4098(defvar cperl-update-start) ; Do not need to make them local
4099(defvar cperl-update-end)
4100(defun cperl-delay-update-hook (beg end old-len)
4101 (setq cperl-update-start (min beg (or cperl-update-start (point-max))))
4102 (setq cperl-update-end (max end (or cperl-update-end (point-min)))))
db133cb6 4103
f83d2997
KH
4104(defun cperl-indent-region (start end)
4105 "Simple variant of indentation of region in CPerl mode.
5c8b7eaf 4106Should be slow. Will not indent comment if it starts at `comment-indent'
f83d2997 4107or looks like continuation of the comment on the previous line.
5c8b7eaf
SS
4108Indents all the lines whose first character is between START and END
4109inclusive.
db133cb6 4110
5c8b7eaf 4111If `cperl-indent-region-fix-constructs', will improve spacing on
db133cb6 4112conditional/loop constructs."
f83d2997 4113 (interactive "r")
5bd52f0e 4114 (cperl-update-syntaxification end end)
f83d2997 4115 (save-excursion
5bd52f0e
RS
4116 (let (cperl-update-start cperl-update-end (h-a-c after-change-functions))
4117 (let (st comm old-comm-indent new-comm-indent p pp i empty
4118 (indent-info (if cperl-emacs-can-parse
4119 (list nil nil nil) ; Cannot use '(), since will modify
4120 nil))
4121 after-change-functions ; Speed it up!
f83d2997 4122 (pm 0) (imenu-scanning-message "Indenting... (%3d%%)"))
5bd52f0e 4123 (if h-a-c (add-hook 'after-change-functions 'cperl-delay-update-hook))
f83d2997
KH
4124 (goto-char start)
4125 (setq old-comm-indent (and (cperl-to-comment-or-eol)
4126 (current-column))
4127 new-comm-indent old-comm-indent)
4128 (goto-char start)
bab27c0c 4129 (setq end (set-marker (make-marker) end)) ; indentation changes pos
f83d2997
KH
4130 (or (bolp) (beginning-of-line 2))
4131 (or (fboundp 'imenu-progress-message)
4132 (message "Indenting... For feedback load `imenu'..."))
4133 (while (and (<= (point) end) (not (eobp))) ; bol to check start
4134 (and (fboundp 'imenu-progress-message)
5c8b7eaf 4135 (imenu-progress-message
f83d2997 4136 pm (/ (* 100 (- (point) start)) (- end start -1))))
5bd52f0e
RS
4137 (setq st (point))
4138 (if (or
4139 (setq empty (looking-at "[ \t]*\n"))
4140 (and (setq comm (looking-at "[ \t]*#"))
5c8b7eaf 4141 (or (eq (current-indentation) (or old-comm-indent
f83d2997 4142 comment-column))
5bd52f0e 4143 (setq old-comm-indent nil))))
f83d2997 4144 (if (and old-comm-indent
5bd52f0e 4145 (not empty)
f83d2997 4146 (= (current-indentation) old-comm-indent)
5bd52f0e
RS
4147 (not (eq (get-text-property (point) 'syntax-type) 'pod))
4148 (not (eq (get-text-property (point) 'syntax-table)
4149 cperl-st-cfence)))
f83d2997
KH
4150 (let ((comment-column new-comm-indent))
4151 (indent-for-comment)))
5c8b7eaf 4152 (progn
5bd52f0e 4153 (setq i (cperl-indent-line indent-info))
f83d2997 4154 (or comm
db133cb6 4155 (not i)
f83d2997 4156 (progn
db133cb6 4157 (if cperl-indent-region-fix-constructs
5bd52f0e 4158 (goto-char (cperl-fix-line-spacing end indent-info)))
5c8b7eaf 4159 (if (setq old-comm-indent
f83d2997 4160 (and (cperl-to-comment-or-eol)
5c8b7eaf 4161 (not (memq (get-text-property (point)
f83d2997
KH
4162 'syntax-type)
4163 '(pod here-doc)))
5c8b7eaf 4164 (not (eq (get-text-property (point)
5bd52f0e
RS
4165 'syntax-table)
4166 cperl-st-cfence))
f83d2997
KH
4167 (current-column)))
4168 (progn (indent-for-comment)
4169 (skip-chars-backward " \t")
4170 (skip-chars-backward "#")
4171 (setq new-comm-indent (current-column))))))))
4172 (beginning-of-line 2))
4173 (if (fboundp 'imenu-progress-message)
4174 (imenu-progress-message pm 100)
5bd52f0e
RS
4175 (message nil)))
4176 ;; Now run the update hooks
4177 (if after-change-functions
4178 (save-excursion
4179 (if cperl-update-end
4180 (progn
4181 (goto-char cperl-update-end)
4182 (insert " ")
4183 (delete-char -1)
4184 (goto-char cperl-update-start)
4185 (insert " ")
4186 (delete-char -1))))))))
f83d2997 4187
f83d2997
KH
4188;; Stolen from lisp-mode with a lot of improvements
4189
4190(defun cperl-fill-paragraph (&optional justify iteration)
4191 "Like \\[fill-paragraph], but handle CPerl comments.
4192If any of the current line is a comment, fill the comment or the
4193block of it that point is in, preserving the comment's initial
4194indentation and initial hashes. Behaves usually outside of comment."
4195 (interactive "P")
4196 (let (
4197 ;; Non-nil if the current line contains a comment.
4198 has-comment
4199
4200 ;; If has-comment, the appropriate fill-prefix for the comment.
4201 comment-fill-prefix
4202 ;; Line that contains code and comment (or nil)
4203 start
4204 c spaces len dc (comment-column comment-column))
4205 ;; Figure out what kind of comment we are looking at.
4206 (save-excursion
4207 (beginning-of-line)
4208 (cond
4209
4210 ;; A line with nothing but a comment on it?
4211 ((looking-at "[ \t]*#[# \t]*")
4212 (setq has-comment t
4213 comment-fill-prefix (buffer-substring (match-beginning 0)
4214 (match-end 0))))
4215
4216 ;; A line with some code, followed by a comment? Remember that the
4217 ;; semi which starts the comment shouldn't be part of a string or
4218 ;; character.
4219 ((cperl-to-comment-or-eol)
4220 (setq has-comment t)
4221 (looking-at "#+[ \t]*")
5c8b7eaf 4222 (setq start (point) c (current-column)
f83d2997
KH
4223 comment-fill-prefix
4224 (concat (make-string (current-column) ?\ )
4225 (buffer-substring (match-beginning 0) (match-end 0)))
5c8b7eaf 4226 spaces (progn (skip-chars-backward " \t")
f83d2997 4227 (buffer-substring (point) start))
5c8b7eaf 4228 dc (- c (current-column)) len (- start (point))
f83d2997
KH
4229 start (point-marker))
4230 (delete-char len)
4231 (insert (make-string dc ?-)))))
4232 (if (not has-comment)
4233 (fill-paragraph justify) ; Do the usual thing outside of comment
4234 ;; Narrow to include only the comment, and then fill the region.
4235 (save-restriction
4236 (narrow-to-region
4237 ;; Find the first line we should include in the region to fill.
4238 (if start (progn (beginning-of-line) (point))
4239 (save-excursion
4240 (while (and (zerop (forward-line -1))
4241 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
4242 ;; We may have gone to far. Go forward again.
4243 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
4244 (forward-line 1))
4245 (point)))
4246 ;; Find the beginning of the first line past the region to fill.
4247 (save-excursion
4248 (while (progn (forward-line 1)
4249 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
4250 (point)))
4251 ;; Remove existing hashes
4252 (goto-char (point-min))
4253 (while (progn (forward-line 1) (< (point) (point-max)))
4254 (skip-chars-forward " \t")
5c8b7eaf 4255 (and (looking-at "#+")
f83d2997
KH
4256 (delete-char (- (match-end 0) (match-beginning 0)))))
4257
4258 ;; Lines with only hashes on them can be paragraph boundaries.
4259 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
4260 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
4261 (fill-prefix comment-fill-prefix))
4262 (fill-paragraph justify)))
4263 (if (and start)
5c8b7eaf 4264 (progn
f83d2997
KH
4265 (goto-char start)
4266 (if (> dc 0)
4267 (progn (delete-char dc) (insert spaces)))
4268 (if (or (= (current-column) c) iteration) nil
4269 (setq comment-column c)
4270 (indent-for-comment)
4271 ;; Repeat once more, flagging as iteration
4272 (cperl-fill-paragraph justify t)))))))
4273
4274(defun cperl-do-auto-fill ()
4275 ;; Break out if the line is short enough
4276 (if (> (save-excursion
4277 (end-of-line)
4278 (current-column))
4279 fill-column)
4280 (let ((c (save-excursion (beginning-of-line)
4281 (cperl-to-comment-or-eol) (point)))
4282 (s (memq (following-char) '(?\ ?\t))) marker)
4283 (if (>= c (point)) nil
4284 (setq marker (point-marker))
4285 (cperl-fill-paragraph)
4286 (goto-char marker)
4287 ;; Is not enough, sometimes marker is a start of line
5c8b7eaf 4288 (if (bolp) (progn (re-search-forward "#+[ \t]*")
f83d2997
KH
4289 (goto-char (match-end 0))))
4290 ;; Following space could have gone:
4291 (if (or (not s) (memq (following-char) '(?\ ?\t))) nil
4292 (insert " ")
4293 (backward-char 1))
4294 ;; Previous space could have gone:
4295 (or (memq (preceding-char) '(?\ ?\t)) (insert " "))))))
4296
80585273 4297(defvar cperl-imenu--function-name-regexp-perl
5c8b7eaf 4298 (concat
f83d2997
KH
4299 "^\\("
4300 "[ \t]*\\(sub\\|package\\)[ \t\n]+\\([a-zA-Z_0-9:']+\\)[ \t]*\\(([^()]*)[ \t]*\\)?"
4301 "\\|"
4302 "=head\\([12]\\)[ \t]+\\([^\n]+\\)$"
4303 "\\)"))
4304
4305(defun cperl-imenu-addback (lst &optional isback name)
4306 ;; We suppose that the lst is a DAG, unless the first element only
4307 ;; loops back, and ISBACK is set. Thus this function cannot be
4308 ;; applied twice without ISBACK set.
4309 (cond ((not cperl-imenu-addback) lst)
4310 (t
5c8b7eaf 4311 (or name
f83d2997
KH
4312 (setq name "+++BACK+++"))
4313 (mapcar (function (lambda (elt)
4314 (if (and (listp elt) (listp (cdr elt)))
4315 (progn
4316 ;; In the other order it goes up
4317 ;; one level only ;-(
4318 (setcdr elt (cons (cons name lst)
4319 (cdr elt)))
4320 (cperl-imenu-addback (cdr elt) t name)
4321 ))))
4322 (if isback (cdr lst) lst))
4323 lst)))
4324
80585273 4325(defun cperl-imenu--create-perl-index (&optional regexp)
f83d2997 4326 (require 'imenu) ; May be called from TAGS creator
5c8b7eaf 4327 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
f83d2997
KH
4328 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
4329 (index-meth-alist '()) meth
6c389151 4330 packages ends-ranges p marker
f83d2997
KH
4331 (prev-pos 0) char fchar index index1 name (end-range 0) package)
4332 (goto-char (point-min))
4333 (if noninteractive
4334 (message "Scanning Perl for index")
4335 (imenu-progress-message prev-pos 0))
6c389151 4336 (cperl-update-syntaxification (point-max) (point-max))
f83d2997
KH
4337 ;; Search for the function
4338 (progn ;;save-match-data
4339 (while (re-search-forward
80585273 4340 (or regexp cperl-imenu--function-name-regexp-perl)
f83d2997
KH
4341 nil t)
4342 (or noninteractive
4343 (imenu-progress-message prev-pos))
f83d2997
KH
4344 (cond
4345 ((and ; Skip some noise if building tags
4346 (match-beginning 2) ; package or sub
4347 (eq (char-after (match-beginning 2)) ?p) ; package
4348 (not (save-match-data
4349 (looking-at "[ \t\n]*;")))) ; Plain text word 'package'
4350 nil)
4351 ((and
4352 (match-beginning 2) ; package or sub
6c389151 4353 ;; Skip if quoted (will not skip multi-line ''-strings :-():
f83d2997
KH
4354 (null (get-text-property (match-beginning 1) 'syntax-table))
4355 (null (get-text-property (match-beginning 1) 'syntax-type))
4356 (null (get-text-property (match-beginning 1) 'in-pod)))
4357 (save-excursion
4358 (goto-char (match-beginning 2))
4359 (setq fchar (following-char))
4360 )
4361 ;; (if (looking-at "([^()]*)[ \t\n\f]*")
4362 ;; (goto-char (match-end 0))) ; Messes what follows
6c389151 4363 (setq char (following-char) ; ?\; for "sub foo () ;"
f83d2997
KH
4364 meth nil
4365 p (point))
4366 (while (and ends-ranges (>= p (car ends-ranges)))
4367 ;; delete obsolete entries
4368 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
4369 (setq package (or (car packages) "")
4370 end-range (or (car ends-ranges) 0))
4371 (if (eq fchar ?p)
4372 (setq name (buffer-substring (match-beginning 3) (match-end 3))
4373 name (progn
4374 (set-text-properties 0 (length name) nil name)
4375 name)
5c8b7eaf 4376 package (concat name "::")
f83d2997 4377 name (concat "package " name)
5c8b7eaf 4378 end-range
f83d2997
KH
4379 (save-excursion
4380 (parse-partial-sexp (point) (point-max) -1) (point))
4381 ends-ranges (cons end-range ends-ranges)
4382 packages (cons package packages)))
4383 ;; )
4384 ;; Skip this function name if it is a prototype declaration.
4385 (if (and (eq fchar ?s) (eq char ?\;)) nil
6c389151
SM
4386 (setq name (buffer-substring (match-beginning 3) (match-end 3))
4387 marker (make-marker))
4388 (set-text-properties 0 (length name) nil name)
4389 (set-marker marker (match-end 3))
4390 (if (eq fchar ?p)
4391 (setq name (concat "package " name))
f83d2997
KH
4392 (cond ((string-match "[:']" name)
4393 (setq meth t))
4394 ((> p end-range) nil)
5c8b7eaf 4395 (t
f83d2997 4396 (setq name (concat package name) meth t))))
6c389151
SM
4397 (setq index (cons name marker))
4398 (if (eq fchar ?p)
f83d2997
KH
4399 (push index index-pack-alist)
4400 (push index index-alist))
4401 (if meth (push index index-meth-alist))
4402 (push index index-unsorted-alist)))
4403 ((match-beginning 5) ; Pod section
4404 ;; (beginning-of-line)
4405 (setq index (imenu-example--name-and-position)
4406 name (buffer-substring (match-beginning 6) (match-end 6)))
4407 (set-text-properties 0 (length name) nil name)
4408 (if (eq (char-after (match-beginning 5)) ?2)
4409 (setq name (concat " " name)))
4410 (setcar index name)
4411 (setq index1 (cons (concat "=" name) (cdr index)))
4412 (push index index-pod-alist)
4413 (push index1 index-unsorted-alist)))))
4414 (or noninteractive
4415 (imenu-progress-message prev-pos 100))
5c8b7eaf 4416 (setq index-alist
f83d2997
KH
4417 (if (default-value 'imenu-sort-function)
4418 (sort index-alist (default-value 'imenu-sort-function))
4419 (nreverse index-alist)))
4420 (and index-pod-alist
4421 (push (cons "+POD headers+..."
4422 (nreverse index-pod-alist))
4423 index-alist))
4424 (and (or index-pack-alist index-meth-alist)
4425 (let ((lst index-pack-alist) hier-list pack elt group name)
4426 ;; Remove "package ", reverse and uniquify.
4427 (while lst
4428 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
4429 (if (assoc name hier-list) nil
4430 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
4431 (setq lst index-meth-alist)
4432 (while lst
4433 (setq elt (car lst) lst (cdr lst))
4434 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
4435 (setq pack (substring (car elt) 0 (match-beginning 0)))
5c8b7eaf 4436 (if (setq group (assoc pack hier-list))
f83d2997
KH
4437 (if (listp (cdr group))
4438 ;; Have some functions already
5c8b7eaf
SS
4439 (setcdr group
4440 (cons (cons (substring
f83d2997
KH
4441 (car elt)
4442 (+ 2 (match-beginning 0)))
4443 (cdr elt))
4444 (cdr group)))
5c8b7eaf 4445 (setcdr group (list (cons (substring
f83d2997
KH
4446 (car elt)
4447 (+ 2 (match-beginning 0)))
4448 (cdr elt)))))
5c8b7eaf
SS
4449 (setq hier-list
4450 (cons (cons pack
4451 (list (cons (substring
f83d2997
KH
4452 (car elt)
4453 (+ 2 (match-beginning 0)))
4454 (cdr elt))))
4455 hier-list))))))
4456 (push (cons "+Hierarchy+..."
4457 hier-list)
4458 index-alist)))
4459 (and index-pack-alist
4460 (push (cons "+Packages+..."
4461 (nreverse index-pack-alist))
4462 index-alist))
5c8b7eaf 4463 (and (or index-pack-alist index-pod-alist
f83d2997
KH
4464 (default-value 'imenu-sort-function))
4465 index-unsorted-alist
4466 (push (cons "+Unsorted List+..."
4467 (nreverse index-unsorted-alist))
4468 index-alist))
4469 (cperl-imenu-addback index-alist)))
4470
6c389151
SM
4471\f
4472(defvar cperl-outline-regexp
4473 (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`"))
4474
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