perl 5.003_01: dump.c
[p5sagit/p5-mst-13.2.git] / emacs / cperl-mode.el
CommitLineData
4633a7c4 1;;; This code started from the following message of long time ago (IZ):
2
3;;; From: olson@mcs.anl.gov (Bob Olson)
4;;; Newsgroups: comp.lang.perl
5;;; Subject: cperl-mode: Another perl mode for Gnuemacs
6;;; Date: 14 Aug 91 15:20:01 GMT
7
8;; Perl code editing commands for Emacs
9;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
10
11;; This file is not (yet) part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to
25;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27;;; Corrections made by Ilya Zakharevich ilya@math.mps.ohio-state.edu
28;;; XEmacs changes by Peter Arius arius@informatik.uni-erlangen.de
29
29043b61 30;; $Id: cperl-mode.el,v 1.20 1996/02/09 03:40:01 ilya Exp ilya $
4633a7c4 31
32;;; To use this mode put the following into your .emacs file:
33
34;; (autoload 'perl-mode "cperl-mode" "alternate mode for editing Perl programs" t)
35
36;;; You can either fine-tune the bells and whistles of this mode or
37;;; bulk enable them by putting
38
39;; (setq cperl-hairy t)
40
41;;; in your .emacs file. (Emacs rulers do not consider it politically
42;;; correct to make whistles enabled by default.)
43
44;;; Additional useful commands to put into your .emacs file:
45
46;; (setq auto-mode-alist
47;; (append '(("\\.[pP][Llm]$" . perl-mode)) auto-mode-alist ))
48;; (setq interpreter-mode-alist (append interpreter-mode-alist
49;; '(("miniperl" . perl-mode))))
50
51;;; The mode information (on C-h m) provides customization help.
52;;; If you use font-lock feature of this mode, it is advisable to use
53;;; eather lazy-lock-mode or fast-lock-mode (available on ELisp
54;;; archive in files lazy-lock.el and fast-lock.el). I prefer lazy-lock.
55
56;;; Faces used now: three faces for first-class and second-class keywords
57;;; and control flow words, one for each: comments, string, labels,
58;;; functions definitions and packages, arrays, hashes, and variable
59;;; definitions. If you do not see all these faces, your font-lock does
60;;; not define them, so you need to define them manually. Maybe you have
61;;; an obsolete font-lock from 19.28 or earlier. Upgrade.
62
63;;; If you have grayscale monitor, and do not have the variable
64;;; font-lock-display-type bound to 'grayscale, insert
65
66;;; (setq font-lock-display-type 'grayscale)
67
68;;; to your .emacs file.
69
29043b61 70;;;; This mode supports font-lock, imenu and mode-compile. In the
4633a7c4 71;;;; hairy version font-lock is on, but you should activate imenu
29043b61 72;;;; yourself (note that mode-compile is not standard yet). Well, you
4633a7c4 73;;;; can use imenu from keyboard anyway (M-x imenu), but it is better
74;;;; to bind it like that:
75
76;; (define-key global-map [M-S-down-mouse-3] 'imenu)
77
78;;; In fact the version of font-lock that this version supports can be
79;;; much newer than the version you actually have. This means that a
80;;; lot of faces can be set up, but are not visible on your screen
81;;; since the coloring rules for this faces are not defined.
82
4633a7c4 83;;; Updates: ========================================
84
85;;; Made less hairy by default: parentheses not electric,
86;;; linefeed not magic. Bug with abbrev-mode corrected.
87
88;;;; After 1.4:
89;;; Better indentation:
90;;; subs inside braces should work now,
91;;; Toplevel braces obey customization.
92;;; indent-for-comment knows about bad cases, cperl-indent-for-comment
93;;; moves cursor to a correct place.
94;;; cperl-indent-exp written from the scratch! Slow... (quadratic!) :-(
95;;; (50 secs on DB::DB (sub of 430 lines), 486/66)
96;;; Minor documentation fixes.
97;;; Imenu understands packages as prefixes (including nested).
98;;; Hairy options can be switched off one-by-one by setting to null.
99;;; Names of functions and variables changed to conform to `cperl-' style.
100
101;;;; After 1.5:
102;;; Some bugs with indentation of labels (and embedded subs) corrected.
103;;; `cperl-indent-region' done (slow :-()).
104;;; `cperl-fill-paragraph' done.
105;;; Better package support for `imenu'.
106;;; Progress indicator for indentation (with `imenu' loaded).
107;;; `Cperl-set' was busted, now setting the individual hairy option
108;;; should be better.
109
110;;;; After 1.6:
111;;; `cperl-set-style' done.
112;;; `cperl-check-syntax' done.
113;;; Menu done.
114;;; New config variables `cperl-close-paren-offset' and `cperl-comment-column'.
115;;; Bugs with `cperl-auto-newline' corrected.
116;;; `cperl-electric-lbrace' can work with `cperl-auto-newline' in situation
117;;; like $hash{.
118
119;;;; 1.7 XEmacs (arius@informatik.uni-erlangen.de):
120;;; - use `next-command-event', if `next-command-events' does not exist
121;;; - use `find-face' as def. of `is-face'
122;;; - corrected def. of `x-color-defined-p'
123;;; - added const defs for font-lock-comment-face,
124;;; font-lock-keyword-face and font-lock-function-name-face
125;;; - added def. of font-lock-variable-name-face
126;;; - added (require 'easymenu) inside an `eval-when-compile'
127;;; - replaced 4-argument `substitute-key-definition' with ordinary
128;;; `define-key's
129;;; - replaced `mark-active' in menu definition by `cperl-use-region-p'.
130;;; Todo (at least):
131;;; - use emacs-vers.el (http://www.cs.utah.edu/~eeide/emacs/emacs-vers.el.gz)
132;;; for portable code?
133;;; - should `cperl-mode' do a
134;;; (if (featurep 'easymenu) (easy-menu-add cperl-menu))
135;;; or should this be left to the user's `cperl-mode-hook'?
136
137;;; Some bugs introduced by the above fix corrected (IZ ;-).
138;;; Some bugs under XEmacs introduced by the correction corrected.
139
140;;; Some more can remain since there are two many different variants.
141;;; Please feedback!
142
143;;; We do not support fontification of arrays and hashes under
144;;; obsolete font-lock any more. Upgrade.
145
146;;;; after 1.8 Minor bug with parentheses.
147;;;; after 1.9 Improvements from Joe Marzot.
148;;;; after 1.10
149;;; Does not need easymenu to compile under XEmacs.
150;;; `vc-insert-headers' should work better.
151;;; Should work with 19.29 and 19.12.
152;;; Small improvements to fontification.
153;;; Expansion of keywords does not depend on C-? being backspace.
154
155;;; after 1.10+
156;;; 19.29 and 19.12 supported.
157;;; `cperl-font-lock-enhanced' deprecated. Use font-lock-extra.el.
158;;; Support for font-lock-extra.el.
159
160;;;; After 1.11:
161;;; Tools submenu.
162;;; Support for perl5-info.
163;;; `imenu-go-find-at-position' in Tools requires imenu-go.el (see hints above)
164;;; Imenu entries do not work with stock imenu.el. Patch sent to maintainers.
165;;; Fontifies `require a if b;', __DATA__.
166;;; Arglist for auto-fill-mode was incorrect.
167
168;;;; After 1.12:
169;;; `cperl-lineup-step' and `cperl-lineup' added: lineup constructions
170;;; vertically.
171;;; `cperl-do-auto-fill' updated for 19.29 style.
172;;; `cperl-info-on-command' now has a default.
173;;; Workaround for broken C-h on XEmacs.
174;;; VC strings escaped.
175;;; C-h f now may prompt for function name instead of going on,
176;;; controlled by `cperl-info-on-command-no-prompt'.
177
178;;;; After 1.13:
179;;; Msb buffer list includes perl files
180;;; Indent-for-comment uses indent-to
181;;; Can write tag files using etags.
182
183;;;; After 1.14:
184;;; Recognizes (tries to ;-) {...} which are not blocks during indentation.
185;;; `cperl-close-paren-offset' affects ?\] too (and ?\} if not block)
c07a80fd 186;;; Bug with auto-filling comments started with "##" corrected.
187
188;;;; Very slow now: on DB::DB 0.91, 486/66:
189
190;;;Function Name Call Count Elapsed Time Average Time
191;;;======================================== ========== ============ ============
192;;;cperl-block-p 469 3.7799999999 0.0080597014
193;;;cperl-get-state 505 163.39000000 0.3235445544
194;;;cperl-comment-indent 12 0.0299999999 0.0024999999
195;;;cperl-backward-to-noncomment 939 4.4599999999 0.0047497337
196;;;cperl-calculate-indent 505 172.22000000 0.3410297029
197;;;cperl-indent-line 505 172.88000000 0.3423366336
198;;;cperl-use-region-p 40 0.0299999999 0.0007499999
199;;;cperl-indent-exp 1 177.97000000 177.97000000
200;;;cperl-to-comment-or-eol 1453 3.9800000000 0.0027391603
201;;;cperl-backward-to-start-of-continued-exp 9 0.0300000000 0.0033333333
202;;;cperl-indent-region 1 177.94000000 177.94000000
203
204;;;; After 1.15:
205;;; Takes into account white space after opening parentheses during indent.
206;;; May highlight pods and here-documents: see `cperl-pod-here-scan',
207;;; `cperl-pod-here-fontify', `cperl-pod-face'. Does not use this info
208;;; for indentation so far.
209;;; Fontification updated to 19.30 style.
210;;; The change 19.29->30 did not add all the required functionality,
211;;; but broke "font-lock-extra.el". Get "choose-color.el" from
212;;; ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs
213
214;;;; After 1.16:
215;;; else # comment
216;;; recognized as a start of a block.
217;;; Two different font-lock-levels provided.
218;;; `cperl-pod-head-face' introduced. Used for highlighting.
219;;; `imenu' marks pods, +Packages moved to the head.
220
221;;;; After 1.17:
222;;; Scan for pods highlights here-docs too.
223;;; Note that the tag of here-doc may be rehighlighted later by lazy-lock.
224;;; Only one here-doc-tag per line is supported, and one in comment
225;;; or a string may break fontification.
226;;; POD headers were supposed to fill one line only.
227
228;;;; After 1.18:
229;;; `font-lock-keywords' were set in 19.30 style _always_. Current scheme
230;;; may break under XEmacs.
231;;; `cperl-calculate-indent' dis suppose that `parse-start' was defined.
232;;; `fontified' tag is added to fontified text as well as `lazy-lock' (for
233;;; compatibility with older lazy-lock.el) (older one overfontifies
234;;; something nevertheless :-().
235;;; Will not indent something inside pod and here-documents.
236;;; Fontifies the package name after import/no/bootstrap.
237;;; Added new entry to menu with meta-info about the mode.
4633a7c4 238
29043b61 239;;;; After 1.19:
240;;; Prefontification works much better with 19.29. Should be checked
241;;; with 19.30 as well.
242;;; Some misprints in docs corrected.
243;;; Now $a{-text} and -text => "blah" are fontified as strings too.
244;;; Now the pod search is much stricter, so it can help you to find
245;;; pod sections which are broken because of whitespace before =blah
246;;; - just observe the fontification.
247
4633a7c4 248(defvar cperl-extra-newline-before-brace nil
249 "*Non-nil means that if, elsif, while, until, else, for, foreach
250and do constructs look like:
251
252 if ()
253 {
254 }
255
256instead of:
257
258 if () {
259 }
260")
c07a80fd 261
4633a7c4 262(defvar cperl-indent-level 2
263 "*Indentation of CPerl statements with respect to containing block.")
264(defvar cperl-lineup-step nil
265 "*`cperl-lineup' will always lineup at multiple of this number.
266If `nil', the value of `cperl-indent-level' will be used.")
267(defvar cperl-brace-imaginary-offset 0
268 "*Imagined indentation of a Perl open brace that actually follows a statement.
269An open brace following other text is treated as if it were this far
270to the right of the start of its line.")
271(defvar cperl-brace-offset 0
272 "*Extra indentation for braces, compared with other text in same context.")
273(defvar cperl-label-offset -2
274 "*Offset of CPerl label lines relative to usual indentation.")
275(defvar cperl-min-label-indent 1
276 "*Minimal offset of CPerl label lines.")
277(defvar cperl-continued-statement-offset 2
278 "*Extra indent for lines not starting new statements.")
279(defvar cperl-continued-brace-offset 0
280 "*Extra indent for substatements that start with open-braces.
281This is in addition to cperl-continued-statement-offset.")
282(defvar cperl-close-paren-offset -1
283 "*Extra indent for substatements that start with close-parenthesis.")
284
285(defvar cperl-auto-newline nil
286 "*Non-nil means automatically newline before and after braces,
287and after colons and semicolons, inserted in CPerl code.")
288
289(defvar cperl-tab-always-indent t
290 "*Non-nil means TAB in CPerl mode should always reindent the current line,
291regardless of where in the line point is when the TAB command is used.")
292
293(defvar cperl-font-lock nil
294 "*Non-nil (and non-null) means CPerl buffers will use font-lock-mode.
295Can be overwritten by `cperl-hairy' if nil.")
296
297(defvar cperl-electric-lbrace-space nil
298 "*Non-nil (and non-null) means { after $ in CPerl buffers should be preceeded by ` '.
299Can be overwritten by `cperl-hairy' if nil.")
300
301(defvar cperl-electric-parens ""
302 "*List of parentheses that should be electric in CPerl, or null.
303Can be overwritten by `cperl-hairy' to \"({[<\" if not 'null.")
304
305(defvar cperl-electric-linefeed nil
306 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
307In any case these two mean plain and hairy linefeeds together.
308Can be overwritten by `cperl-hairy' if nil.")
309
310(defvar cperl-electric-keywords nil
311 "*Not-nil (and non-null) means keywords are electric in CPerl.
312Can be overwritten by `cperl-hairy' if nil.")
313
314(defvar cperl-hairy nil
315 "*Not-nil means all the bells and whistles are enabled in CPerl.")
316
317(defvar cperl-comment-column 32
318 "*Column to put comments in CPerl (use \\[cperl-indent]' to lineup with code).")
319
320(defvar cperl-vc-header-alist '((SCCS "$sccs = '%W\%' ;")
321 (RCS "$rcs = ' $Id\$ ' ;"))
322 "*What to use as `vc-header-alist' in CPerl.")
323
324(defvar cperl-info-on-command-no-prompt nil
325 "*Not-nil (and non-null) means not to prompt on C-h f.
326The opposite behaviour is always available if prefixed with C-c.
327Can be overwritten by `cperl-hairy' if nil.")
c07a80fd 328
329(defvar cperl-pod-face 'font-lock-comment-face
330 "*The result of evaluation of this expression is used for pod highlighting.")
331
332(defvar cperl-pod-head-face 'font-lock-variable-name-face
333 "*The result of evaluation of this expression is used for pod highlighting.
334Font for POD headers.")
335
336(defvar cperl-here-face 'font-lock-string-face
337 "*The result of evaluation of this expression is used for here-docs highlighting.")
338
339(defvar cperl-pod-here-fontify '(featurep 'font-lock)
340 "*Not-nil after evaluation means to highlight pod and here-docs sections.")
341
342(defvar cperl-pod-here-scan t
343 "*Not-nil means look for pod and here-docs sections during startup.
344You can always make lookup from menu or using \\[cperl-find-pods-heres].")
345
346\f
347
348;;; Short extra-docs.
349
350(defvar cperl-tips 'please-ignore-this-line
351 "Get newest version of this package from
29043b61 352 ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs
c07a80fd 353and/or
354 ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
355
356Get support packages font-lock-extra.el, imenu-go.el from the same place.
357\(Look for other files there too... ;-) Get a patch for imenu.el in 19.29.
358Note that for 19.30 you should use choose-color.el *instead* of
359font-lock-extra.el (and you will not get smart highlighting in C :-().
360
361Note that to enable Compile choices in the menu you need to install
29043b61 362mode-compile.el.
c07a80fd 363
364Get perl5-info from
365 http://www.metronet.com:70/9/perlinfo/perl5/manual/perl5-info.tar.gz
366\(may be quite obsolete, but still useful).
367
368If you use imenu-go, run imenu on perl5-info buffer (you can do it from
369CPerl menu).
370
371Before reporting (non-)problems look in the problem section on what I
372know about them.")
373
374(defvar cperl-problems 'please-ignore-this-line
375"Emacs has a _very_ restricted syntax parsing engine.
376
377It may be corrected on the level of C ocde, please look in the
378`non-problems' section if you want to volonteer.
379
380CPerl mode tries to corrects some Emacs misunderstandings, however,
381for effeciency reasons the degree of correction is different for
382different operations. The partially corrected problems are: POD
383sections, here-documents, regexps. The operations are: highlighting,
384indentation, electric keywords, electric braces.
385
386This may be confusing, since the regexp s#//#/#\; may be highlighted
387as a comment, but it will recognized as a regexp by the indentation
388code. Or the opposite case, when a pod section is highlighted, but
389breaks the indentation of the following code.
390
391The main trick (to make $ a \"backslash\") makes constructions like
392${aaa} look like unbalanced braces. The only trick I can think out is
393to insert it as $ {aaa} (legal in perl5, not in perl4).
394
395Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
396as /($|\\s)/. Note that such a transpositinon is not always possible
397:-(. " )
398
399(defvar cperl-non-problems 'please-ignore-this-line
400"As you know from `problems' section, Perl syntax too hard for CPerl.
401
402Most the time, if you write your own code, you may find an equivalent
403\(and almost as readable) expression.
404
405Try to help it: add comments with embedded quotes to fix CPerl
406misunderstandings about the end of quotation:
407
408$a='500$'; # ';
409
410You won't need it too often. The reason: $ \"quotes\" the following
411character (this saves a life a lot of times in CPerl), thus due to
412Emacs parsing rules it does not consider tick after the dollar as a
413closing one, but as a usual character.
414
415Now the indentation code is pretty wise. The only drawback is that it
416relies on Emacs parsing to find matching parentheses. And Emacs
417*cannot* match parentheses in Perl 100% correctly. So
418 1 if s#//#/#;
419will not break indentation, but
420 1 if ( s#//#/# );
421will.
422
423If you still get wrong indentation in situation that you think the
424code should be able to parse, try:
425
426a) Check what Emacs thinks about balance of your parentheses.
427b) Supply the code to me (IZ).
428
429Pods are treated _very_ rudimentally. Here-documents are not treated
430at all (except highlighting and inhibiting indentation). (This may
431change some time. RMS approved making syntax lookup recognize text
432attributes, but volonteers are needed to change Emacs C code.)
433
434To speed up coloring the following compromises exist:
435 a) sub in $mypackage::sub may be highlighted.
436 b) -z in [a-z] may be highlighted.
437 c) if your regexp contains a keyword (like \"s\"), it may be highlighted.
438")
439
4633a7c4 440\f
441
442;;; Portability stuff:
443
444(defsubst cperl-xemacs-p ()
445 (string-match "XEmacs\\|Lucid" emacs-version))
446
447(defvar del-back-ch (car (append (where-is-internal 'delete-backward-char)
448 (where-is-internal 'backward-delete-char-untabify)))
449 "Character generated by key bound to delete-backward-char.")
450
451(and (vectorp del-back-ch) (= (length del-back-ch) 1)
452 (setq del-back-ch (aref del-back-ch 0)))
453
454(if (cperl-xemacs-p)
455 ;; "Active regions" are on: use region only if active
456 ;; "Active regions" are off: use region unconditionally
457 (defun cperl-use-region-p ()
458 (if zmacs-regions (mark) t))
459 (defun cperl-use-region-p ()
460 (if transient-mark-mode mark-active t)))
461
462(defsubst cperl-enable-font-lock ()
463 (or (cperl-xemacs-p) window-system))
464
465(if (boundp 'unread-command-events)
466 (if (cperl-xemacs-p)
467 (defun cperl-putback-char (c) ; XEmacs >= 19.12
468 (setq unread-command-events (list (character-to-event c))))
469 (defun cperl-putback-char (c) ; Emacs 19
470 (setq unread-command-events (list c))))
471 (defun cperl-putback-char (c) ; XEmacs <= 19.11
472 (setq unread-command-event (character-to-event c))))
473
474(or (fboundp 'uncomment-region)
475 (defun uncomment-region (beg end)
476 (interactive "r")
477 (comment-region beg end -1)))
478
29043b61 479(defvar cperl-do-not-fontify
480 (if (string< emacs-version "19.30")
481 'fontified
482 'lazy-lock)
483 "Text property which inhibits refontification.")
484
485\f
4633a7c4 486;;; Probably it is too late to set these guys already, but it can help later:
487
488(setq auto-mode-alist
489 (append '(("\\.[pP][Llm]$" . perl-mode)) auto-mode-alist ))
490(and (boundp 'interpreter-mode-alist)
491 (setq interpreter-mode-alist (append interpreter-mode-alist
492 '(("miniperl" . perl-mode)))))
493(if (fboundp 'eval-when-compile)
494 (eval-when-compile
495 (condition-case nil
496 (require 'imenu)
497 (error nil))
498 (condition-case nil
499 (require 'easymenu)
500 (error nil))
501 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
502 ;; macros instead of defsubsts don't work on Emacs, so we do the
503 ;; expansion manually. Any other suggestions?
504 (if (or (string-match "XEmacs\\|Lucid" emacs-version)
505 window-system)
506 (require 'font-lock))
507 (require 'cl)
508 ))
509
510(defvar cperl-mode-abbrev-table nil
511 "Abbrev table in use in Cperl-mode buffers.")
512
513(add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
514
515(defvar cperl-mode-map () "Keymap used in CPerl mode.")
516
517(if cperl-mode-map nil
518 (setq cperl-mode-map (make-sparse-keymap))
519 (define-key cperl-mode-map "{" 'cperl-electric-lbrace)
520 (define-key cperl-mode-map "[" 'cperl-electric-paren)
521 (define-key cperl-mode-map "(" 'cperl-electric-paren)
522 (define-key cperl-mode-map "<" 'cperl-electric-paren)
523 (define-key cperl-mode-map "}" 'cperl-electric-brace)
524 (define-key cperl-mode-map ";" 'cperl-electric-semi)
525 (define-key cperl-mode-map ":" 'cperl-electric-terminator)
526 (define-key cperl-mode-map "\C-j" 'newline-and-indent)
527 (define-key cperl-mode-map "\C-c\C-j" 'cperl-linefeed)
528 (define-key cperl-mode-map "\e\C-q" 'cperl-indent-exp) ; Usually not bound
529 ;;(define-key cperl-mode-map "\M-q" 'cperl-fill-paragraph)
530 ;;(define-key cperl-mode-map "\e;" 'cperl-indent-for-comment)
531 (define-key cperl-mode-map "\177" 'backward-delete-char-untabify)
532 (define-key cperl-mode-map "\t" 'cperl-indent-command)
533 (if (cperl-xemacs-p)
534 ;; don't clobber the backspace binding:
535 (define-key cperl-mode-map [(control h) f] 'cperl-info-on-command)
536 (define-key cperl-mode-map "\C-hf" 'cperl-info-on-command))
537 (if (cperl-xemacs-p)
538 ;; don't clobber the backspace binding:
539 (define-key cperl-mode-map [(control c) (control h) f]
540 'cperl-info-on-current-command)
541 (define-key cperl-mode-map "\C-c\C-hf" 'cperl-info-on-current-command))
542 (if (and (cperl-xemacs-p)
543 (<= emacs-minor-version 11) (<= emacs-major-version 19))
544 (progn
545 ;; substitute-key-definition is usefulness-deenhanced...
546 (define-key cperl-mode-map "\M-q" 'cperl-fill-paragraph)
547 (define-key cperl-mode-map "\e;" 'cperl-indent-for-comment)
548 (define-key cperl-mode-map "\e\C-\\" 'cperl-indent-region))
549 (substitute-key-definition
550 'indent-sexp 'cperl-indent-exp
551 cperl-mode-map global-map)
552 (substitute-key-definition
553 'fill-paragraph 'cperl-fill-paragraph
554 cperl-mode-map global-map)
555 (substitute-key-definition
556 'indent-region 'cperl-indent-region
557 cperl-mode-map global-map)
558 (substitute-key-definition
559 'indent-for-comment 'cperl-indent-for-comment
560 cperl-mode-map global-map)))
561
562(condition-case nil
563 (progn
564 (require 'easymenu)
565 (easy-menu-define cperl-menu cperl-mode-map "Menu for CPerl mode"
566 '("Perl"
567 ["Beginning of function" beginning-of-defun t]
568 ["End of function" end-of-defun t]
569 ["Mark function" mark-defun t]
570 ["Indent expression" cperl-indent-exp t]
571 ["Fill paragraph/comment" cperl-fill-paragraph t]
572 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
573 "----"
574 ["Indent region" cperl-indent-region (cperl-use-region-p)]
575 ["Comment region" comment-region (cperl-use-region-p)]
576 ["Uncomment region" uncomment-region (cperl-use-region-p)]
577 "----"
578 ["Run" mode-compile (fboundp 'mode-compile)]
579 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
580 (get-buffer "*compilation*"))]
581 ["Next error" next-error (get-buffer "*compilation*")]
582 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
583 "----"
584 ["Debugger" perldb t]
585 "----"
586 ("Tools"
587 ["Imenu" imenu (fboundp 'imenu)]
588 ["Imenu on info" cperl-imenu-on-info (featurep 'imenu)]
589 ("Tags"
590 ["Create tags for current file" cperl-etags t]
591 ["Add tags for current file" (cperl-etags t) t]
592 ["Create tags for Perl files in directory" (cperl-etags nil t) t]
593 ["Add tags for Perl files in directory" (cperl-etags t t) t]
594 ["Create tags for Perl files in (sub)directories"
595 (cperl-etags nil 'recursive) t]
596 ["Add tags for Perl files in (sub)directories"
597 (cperl-etags t 'recursive) t])
c07a80fd 598 ["Recalculate PODs" cperl-find-pods-heres t]
4633a7c4 599 ["Define word at point" imenu-go-find-at-position
600 (fboundp 'imenu-go-find-at-position)]
601 ["Help on function" cperl-info-on-command t]
602 ["Help on function at point" cperl-info-on-current-command t])
603 ("Indent styles..."
604 ["GNU" (cperl-set-style "GNU") t]
605 ["C++" (cperl-set-style "C++") t]
606 ["FSF" (cperl-set-style "FSF") t]
607 ["BSD" (cperl-set-style "BSD") t]
c07a80fd 608 ["Whitesmith" (cperl-set-style "Whitesmith") t])
609 ("Micro-docs"
610 ["Tips" (describe-variable 'cperl-tips) t]
611 ["Problems" (describe-variable 'cperl-problems) t]
612 ["Non-problems" (describe-variable 'cperl-non-problems) t]))))
4633a7c4 613 (error nil))
614
615(autoload 'c-macro-expand "cmacexp"
616 "Display the result of expanding all C macros occurring in the region.
617The expansion is entirely correct because it uses the C preprocessor."
618 t)
619
620(defvar cperl-mode-syntax-table nil
621 "Syntax table in use in Cperl-mode buffers.")
622
623(if cperl-mode-syntax-table
624 ()
625 (setq cperl-mode-syntax-table (make-syntax-table))
626 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
627 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
628 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
629 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
630 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
631 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
632 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
633 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
634 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
635 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
636 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
637 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
638 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
639 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
640 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
641 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table)
642 (modify-syntax-entry ?| "." cperl-mode-syntax-table))
643
644
645\f
646;; Make customization possible "in reverse"
647;;(defun cperl-set (symbol to)
648;; (or (eq (symbol-value symbol) 'null) (set symbol to)))
649(defsubst cperl-val (symbol &optional default hairy)
650 (cond
651 ((eq (symbol-value symbol) 'null) default)
652 (cperl-hairy (or hairy t))
653 (t (symbol-value symbol))))
654\f
655;; provide an alias for working with emacs 19. the perl-mode that comes
656;; with it is really bad, and this lets us seamlessly replace it.
657(fset 'perl-mode 'cperl-mode)
658(defun cperl-mode ()
659 "Major mode for editing Perl code.
660Expression and list commands understand all C brackets.
661Tab indents for Perl code.
662Paragraphs are separated by blank lines only.
663Delete converts tabs to spaces as it moves back.
664
665Various characters in Perl almost always come in pairs: {}, (), [],
666sometimes <>. When the user types the first, she gets the second as
667well, with optional special formatting done on {}. (Disabled by
668default.) You can always quote (with \\[quoted-insert]) the left
669\"paren\" to avoid the expansion. The processing of < is special,
670since most the time you mean \"less\". Cperl mode tries to guess
671whether you want to type pair <>, and inserts is if it
672appropriate. You can set `cperl-electric-parens' to the string that
673contains the parenths from the above list you want to be electrical.
674
675CPerl mode provides expansion of the Perl control constructs:
676 if, else, elsif, unless, while, until, for, and foreach.
677=========(Disabled by default, see `cperl-electric-keywords'.)
678The user types the keyword immediately followed by a space, which causes
679the construct to be expanded, and the user is positioned where she is most
680likely to want to be.
681eg. when the user types a space following \"if\" the following appears in
682the buffer:
683 if () { or if ()
684 } {
685 }
686and the cursor is between the parentheses. The user can then type some
687boolean expression within the parens. Having done that, typing
688\\[cperl-linefeed] places you, appropriately indented on a new line
689between the braces. If CPerl decides that you want to insert
690\"English\" style construct like
691 bite if angry;
692it will not do any expansion. See also help on variable
693`cperl-extra-newline-before-brace'.
694
695\\[cperl-linefeed] is a convinience replacement for typing carriage
696return. It places you in the next line with proper indentation, or if
697you type it inside the inline block of control construct, like
698 foreach (@lines) {print; print}
699and you are on a boundary of a statement inside braces, it will
700transform the construct into a multiline and will place you into an
701apporpriately indented blank line. If you need a usual
702`newline-and-indent' behaviour, it is on \\[newline-and-indent],
703see documentation on `cperl-electric-linefeed'.
704
705\\{cperl-mode-map}
706
707Setting the variable `cperl-font-lock' to t switches on
708font-lock-mode, `cperl-electric-lbrace-space' to t switches on
709electric space between $ and {, `cperl-electric-parens' is the string
710that contains parentheses that should be electric in CPerl, setting
711`cperl-electric-keywords' enables electric expansion of control
712structures in CPerl. `cperl-electric-linefeed' governs which one of
713two linefeed behavior is preferable. You can enable all these options
714simultaneously (recommended mode of use) by setting `cperl-hairy' to
715t. In this case you can switch separate options off by setting them
716to `null'.
717
718If your site has perl5 documentation in info format, you can use commands
719\\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
720These keys run commands `cperl-info-on-current-command' and
721`cperl-info-on-command', which one is which is controlled by variable
722`cperl-info-on-command-no-prompt' (in turn affected by `cperl-hairy').
723
c07a80fd 724Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
725`cperl-pod-face', `cperl-pod-head-face' control processing of pod and
726here-docs sections. In a future version results of scan may be used
727for indentation too, currently they are used for highlighting only.
728
4633a7c4 729Variables controlling indentation style:
730 `cperl-tab-always-indent'
731 Non-nil means TAB in CPerl mode should always reindent the current line,
732 regardless of where in the line point is when the TAB command is used.
733 `cperl-auto-newline'
734 Non-nil means automatically newline before and after braces,
735 and after colons and semicolons, inserted in Perl code.
736 `cperl-indent-level'
737 Indentation of Perl statements within surrounding block.
738 The surrounding block's indentation is the indentation
739 of the line on which the open-brace appears.
740 `cperl-continued-statement-offset'
741 Extra indentation given to a substatement, such as the
742 then-clause of an if, or body of a while, or just a statement continuation.
743 `cperl-continued-brace-offset'
744 Extra indentation given to a brace that starts a substatement.
745 This is in addition to `cperl-continued-statement-offset'.
746 `cperl-brace-offset'
747 Extra indentation for line if it starts with an open brace.
748 `cperl-brace-imaginary-offset'
749 An open brace following other text is treated as if it the line started
750 this far to the right of the actual line indentation.
751 `cperl-label-offset'
752 Extra indentation for line that is a label.
753 `cperl-min-label-indent'
754 Minimal indentation for line that is a label.
755
756Settings for K&R and BSD indentation styles are
757 `cperl-indent-level' 5 8
758 `cperl-continued-statement-offset' 5 8
759 `cperl-brace-offset' -5 -8
760 `cperl-label-offset' -5 -8
761
762If `cperl-indent-level' is 0, the statement after opening brace in column 0 is indented on `cperl-brace-offset'+`cperl-continued-statement-offset'.
763
764Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
765with no args."
766 (interactive)
767 (kill-all-local-variables)
768 ;;(if cperl-hairy
769 ;; (progn
770 ;; (cperl-set 'cperl-font-lock cperl-hairy)
771 ;; (cperl-set 'cperl-electric-lbrace-space cperl-hairy)
772 ;; (cperl-set 'cperl-electric-parens "{[(<")
773 ;; (cperl-set 'cperl-electric-keywords cperl-hairy)
774 ;; (cperl-set 'cperl-electric-linefeed cperl-hairy)))
775 (use-local-map cperl-mode-map)
776 (if (cperl-val 'cperl-electric-linefeed)
777 (progn
778 (local-set-key "\C-J" 'cperl-linefeed)
779 (local-set-key "\C-C\C-J" 'newline-and-indent)))
780 (if (cperl-val 'cperl-info-on-command-no-prompt)
781 (progn
782 (if (cperl-xemacs-p)
783 ;; don't clobber the backspace binding:
784 (local-set-key [(control h) f] 'cperl-info-on-current-command)
785 (local-set-key "\C-hf" 'cperl-info-on-current-command))
786 (if (cperl-xemacs-p)
787 ;; don't clobber the backspace binding:
788 (local-set-key [(control c) (control h) f]
789 'cperl-info-on-command)
790 (local-set-key "\C-c\C-hf" 'cperl-info-on-command))))
791 (setq major-mode 'perl-mode)
792 (setq mode-name "CPerl")
793 (if (not cperl-mode-abbrev-table)
794 (let ((prev-a-c abbrevs-changed))
795 (define-abbrev-table 'cperl-mode-abbrev-table '(
796 ("if" "if" cperl-electric-keyword 0)
797 ("elsif" "elsif" cperl-electric-keyword 0)
798 ("while" "while" cperl-electric-keyword 0)
799 ("until" "until" cperl-electric-keyword 0)
800 ("unless" "unless" cperl-electric-keyword 0)
801 ("else" "else" cperl-electric-else 0)
802 ("for" "for" cperl-electric-keyword 0)
803 ("foreach" "foreach" cperl-electric-keyword 0)
804 ("do" "do" cperl-electric-keyword 0)))
805 (setq abbrevs-changed prev-a-c)))
806 (setq local-abbrev-table cperl-mode-abbrev-table)
807 (abbrev-mode (if (cperl-val 'cperl-electric-keywords) 1 0))
808 (set-syntax-table cperl-mode-syntax-table)
809 (make-local-variable 'paragraph-start)
810 (setq paragraph-start (concat "^$\\|" page-delimiter))
811 (make-local-variable 'paragraph-separate)
812 (setq paragraph-separate paragraph-start)
813 (make-local-variable 'paragraph-ignore-fill-prefix)
814 (setq paragraph-ignore-fill-prefix t)
815 (make-local-variable 'indent-line-function)
816 (setq indent-line-function 'cperl-indent-line)
817 (make-local-variable 'require-final-newline)
818 (setq require-final-newline t)
819 (make-local-variable 'comment-start)
820 (setq comment-start "# ")
821 (make-local-variable 'comment-end)
822 (setq comment-end "")
823 (make-local-variable 'comment-column)
824 (setq comment-column cperl-comment-column)
825 (make-local-variable 'comment-start-skip)
826 (setq comment-start-skip "#+ *")
827 (make-local-variable 'defun-prompt-regexp)
828 (setq defun-prompt-regexp "[ \t]*sub\\s +\\([^ \t\n{;]+\\)\\s *")
829 (make-local-variable 'comment-indent-function)
830 (setq comment-indent-function 'cperl-comment-indent)
831 (make-local-variable 'parse-sexp-ignore-comments)
832 (setq parse-sexp-ignore-comments t)
833 (make-local-variable 'indent-region-function)
834 (setq indent-region-function 'cperl-indent-region)
835 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
836 (make-local-variable 'imenu-create-index-function)
837 (setq imenu-create-index-function
838 (function imenu-example--create-perl-index))
c07a80fd 839 (make-local-variable 'imenu-sort-function)
840 (setq imenu-sort-function nil)
4633a7c4 841 (make-local-variable 'vc-header-alist)
842 (setq vc-header-alist cperl-vc-header-alist)
c07a80fd 843 (make-local-variable 'font-lock-defaults)
844 (setq font-lock-defaults
845 (if (string< emacs-version "19.30")
846 '(perl-font-lock-keywords-2)
847 '((perl-font-lock-keywords
848 perl-font-lock-keywords-1
849 perl-font-lock-keywords-2))))
4633a7c4 850 (or (fboundp 'cperl-old-auto-fill-mode)
851 (progn
852 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
853 (defun auto-fill-mode (&optional arg)
854 (interactive "P")
855 (cperl-old-auto-fill-mode arg)
856 (and auto-fill-function (eq major-mode 'perl-mode)
857 (setq auto-fill-function 'cperl-do-auto-fill)))))
858 (if (cperl-enable-font-lock)
c07a80fd 859 (if (cperl-val 'cperl-font-lock)
860 (progn (or cperl-faces-init (cperl-init-faces))
861 (font-lock-mode 1))))
4633a7c4 862 (and (boundp 'msb-menu-cond)
863 (not cperl-msb-fixed)
864 (cperl-msb-fix))
c07a80fd 865 (run-hooks 'cperl-mode-hook)
866 ;; After hooks since fontification will break this
867 (if cperl-pod-here-scan (cperl-find-pods-heres)))
4633a7c4 868\f
869;; Fix for msb.el
870(defvar cperl-msb-fixed nil)
871
872(defun cperl-msb-fix ()
873 ;; Adds perl files to msb menu, supposes that msb is already loaded
874 (setq cperl-msb-fixed t)
875 (let* ((l (length msb-menu-cond))
876 (last (nth (1- l) msb-menu-cond))
877 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
878 (handle (1- (nth 1 last))))
879 (setcdr precdr (list
880 (list
881 '(eq major-mode 'perl-mode)
882 handle
883 "Perl Files (%d)")
884 last))))
885\f
886;; This is used by indent-for-comment
887;; to decide how much to indent a comment in CPerl code
888;; based on its context. Do fallback if comment is found wrong.
889
890(defvar cperl-wrong-comment)
891
892(defun cperl-comment-indent ()
893 (let ((p (point)) (c (current-column)) was)
894 (if (looking-at "^#") 0 ; Existing comment at bol stays there.
895 ;; Wrong comment found
896 (save-excursion
897 (setq was (cperl-to-comment-or-eol))
898 (if (= (point) p)
899 (progn
900 (skip-chars-backward " \t")
901 (max (1+ (current-column)) ; Else indent at comment column
902 comment-column))
903 (if was nil
904 (insert comment-start)
905 (backward-char (length comment-start)))
906 (setq cperl-wrong-comment t)
907 (indent-to comment-column 1) ; Indent minimum 1
908 c))))) ; except leave at least one space.
909
910;;;(defun cperl-comment-indent-fallback ()
911;;; "Is called if the standard comment-search procedure fails.
912;;;Point is at start of real comment."
913;;; (let ((c (current-column)) target cnt prevc)
914;;; (if (= c comment-column) nil
915;;; (setq cnt (skip-chars-backward "[ \t]"))
916;;; (setq target (max (1+ (setq prevc
917;;; (current-column))) ; Else indent at comment column
918;;; comment-column))
919;;; (if (= c comment-column) nil
920;;; (delete-backward-char cnt)
921;;; (while (< prevc target)
922;;; (insert "\t")
923;;; (setq prevc (current-column)))
924;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
925;;; (while (< prevc target)
926;;; (insert " ")
927;;; (setq prevc (current-column)))))))
928
929(defun cperl-indent-for-comment ()
930 "Substite for `indent-for-comment' in CPerl."
931 (interactive)
932 (let (cperl-wrong-comment)
933 (indent-for-comment)
934 (if cperl-wrong-comment
935 (progn (cperl-to-comment-or-eol)
936 (forward-char (length comment-start))))))
937
938(defun cperl-electric-brace (arg &optional only-before)
939 "Insert character and correct line's indentation.
940If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
941place (even in empty line), but not after."
942 (interactive "P")
943 (let (insertpos)
944 (if (and (not arg) ; No args, end (of empty line or auto)
945 (eolp)
946 (or (and (null only-before)
947 (save-excursion
948 (skip-chars-backward " \t")
949 (bolp)))
950 (if cperl-auto-newline
951 (progn (cperl-indent-line) (newline) t) nil)))
952 (progn
953 (if cperl-auto-newline
954 (setq insertpos (point)))
955 (insert last-command-char)
956 (cperl-indent-line)
957 (if (and cperl-auto-newline (null only-before))
958 (progn
959 (newline)
960 (cperl-indent-line)))
961 (save-excursion
962 (if insertpos (progn (goto-char insertpos)
963 (search-forward (make-string
964 1 last-command-char))
965 (setq insertpos (1- (point)))))
966 (delete-char -1))))
967 (if insertpos
968 (save-excursion
969 (goto-char insertpos)
970 (self-insert-command (prefix-numeric-value arg)))
971 (self-insert-command (prefix-numeric-value arg)))))
972
973(defun cperl-electric-lbrace (arg)
974 "Insert character, correct line's indentation, correct quoting by space."
975 (interactive "P")
976 (let (pos after (cperl-auto-newline cperl-auto-newline))
977 (and (cperl-val 'cperl-electric-lbrace-space)
978 (eq (preceding-char) ?$)
979 (save-excursion
980 (skip-chars-backward "$")
981 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
982 (insert ? ))
c07a80fd 983 (if (cperl-after-expr-p) nil (setq cperl-auto-newline nil))
4633a7c4 984 (cperl-electric-brace arg)
985 (and (eq last-command-char ?{)
986 (memq last-command-char
987 (append (cperl-val 'cperl-electric-parens "" "([{<") nil))
988 (setq last-command-char ?} pos (point))
989 (progn (cperl-electric-brace arg t)
990 (goto-char pos)))))
991
992(defun cperl-electric-paren (arg)
993 "Insert a matching pair of parentheses."
994 (interactive "P")
995 (let ((beg (save-excursion (beginning-of-line) (point))))
996 (if (and (memq last-command-char
997 (append (cperl-val 'cperl-electric-parens "" "([{<") nil))
998 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
999 ;;(not (save-excursion (search-backward "#" beg t)))
1000 (if (eq last-command-char ?<)
c07a80fd 1001 (cperl-after-expr-p nil "{};(,:=")
4633a7c4 1002 1))
1003 (progn
1004 (insert last-command-char)
1005 (insert (cdr (assoc last-command-char '((?{ .?})
1006 (?[ . ?])
1007 (?( . ?))
1008 (?< . ?>)))))
1009 (forward-char -1))
1010 (insert last-command-char)
1011 )))
1012
1013(defun cperl-electric-keyword ()
1014 "Insert a construction appropriate after a keyword."
1015 (let ((beg (save-excursion (beginning-of-line) (point))))
1016 (and (save-excursion
1017 (backward-sexp 1)
c07a80fd 1018 (cperl-after-expr-p nil "{};:"))
4633a7c4 1019 (save-excursion
1020 (not
1021 (re-search-backward
1022 "[#\"'`]\\|\\<q\\(\\|[wqx]\\)\\>"
1023 beg t)))
1024 (save-excursion (or (not (re-search-backward "^=" nil t))
1025 (looking-at "=cut")))
1026 (progn
1027 (cperl-indent-line)
1028 ;;(insert " () {\n}")
1029 (cond
1030 (cperl-extra-newline-before-brace
1031 (insert " ()\n")
1032 (insert "{")
1033 (cperl-indent-line)
1034 (insert "\n")
1035 (cperl-indent-line)
1036 (insert "\n}"))
1037 (t
1038 (insert " () {\n}"))
1039 )
1040 (or (looking-at "[ \t]\\|$") (insert " "))
1041 (cperl-indent-line)
1042 (search-backward ")")
1043 (cperl-putback-char del-back-ch)))))
1044
1045(defun cperl-electric-else ()
1046 "Insert a construction appropriate after a keyword."
1047 (let ((beg (save-excursion (beginning-of-line) (point))))
1048 (and (save-excursion
1049 (backward-sexp 1)
c07a80fd 1050 (cperl-after-expr-p nil "{};:"))
4633a7c4 1051 (save-excursion
1052 (not
1053 (re-search-backward
1054 "[#\"'`]\\|\\<q\\(\\|[wqx]\\)\\>"
1055 beg t)))
1056 (save-excursion (or (not (re-search-backward "^=" nil t))
1057 (looking-at "=cut")))
1058 (progn
1059 (cperl-indent-line)
1060 ;;(insert " {\n\n}")
1061 (cond
1062 (cperl-extra-newline-before-brace
1063 (insert "\n")
1064 (insert "{")
1065 (cperl-indent-line)
1066 (insert "\n\n}"))
1067 (t
1068 (insert " {\n\n}"))
1069 )
1070 (or (looking-at "[ \t]\\|$") (insert " "))
1071 (cperl-indent-line)
1072 (forward-line -1)
1073 (cperl-indent-line)
1074 (cperl-putback-char del-back-ch)))))
1075
1076(defun cperl-linefeed ()
1077 "Go to end of line, open a new line and indent appropriately."
1078 (interactive)
1079 (let ((beg (save-excursion (beginning-of-line) (point)))
1080 (end (save-excursion (end-of-line) (point)))
1081 (pos (point)) start)
1082 (if (and ; Check if we need to split:
1083 ; i.e., on a boundary and inside "{...}"
1084 ;;(not (search-backward "\\(^\\|[^$\\\\]\\)#" beg t))
1085 (save-excursion (cperl-to-comment-or-eol)
1086 (>= (point) pos))
1087 (or (save-excursion
1088 (skip-chars-backward " \t" beg)
1089 (forward-char -1)
1090 (looking-at "[;{]"))
1091 (looking-at "[ \t]*}")
1092 (re-search-forward "\\=[ \t]*;" end t))
1093 (save-excursion
1094 (and
1095 (eq (car (parse-partial-sexp pos end -1)) -1)
1096 (looking-at "[ \t]*\\($\\|#\\)")
1097 ;;(setq finish (point-marker))
1098 (progn
1099 (backward-sexp 1)
1100 (setq start (point-marker))
1101 (<= start pos))
1102 ;;(looking-at "[^{}\n]*}[ \t]*$") ; Will fail if there are intervening {}'s
1103 ;;(search-backward "{" beg t)
1104 ;;(looking-at "{[^{}\n]*}[ \t]*$")
1105 )))
1106 ;;(or (looking-at "[ \t]*}") ; and on a boundary of statements
1107 ;; (save-excursion
1108 ;; (skip-chars-backward " \t")
1109 ;; (forward-char -1)
1110 ;; (looking-at "[{;]"))))
1111 (progn
1112 (skip-chars-backward " \t")
1113 (or (memq (preceding-char) (append ";{" nil))
1114 (insert ";"))
1115 (insert "\n")
1116 (forward-line -1)
1117 (cperl-indent-line)
1118 ;;(end-of-line)
1119 ;;(search-backward "{" beg)
1120 (goto-char start)
1121 (or (looking-at "{[ \t]*$") ; If there is a statement
1122 ; before, move it to separate line
1123 (progn
1124 (forward-char 1)
1125 (insert "\n")
1126 (cperl-indent-line)))
1127 (forward-line 1) ; We are on the target line
1128 (cperl-indent-line)
1129 (beginning-of-line)
1130 (or (looking-at "[ \t]*}[ \t]*$") ; If there is a statement
1131 ; after, move it to separate line
1132 (progn
1133 (end-of-line)
1134 (search-backward "}" beg)
1135 (skip-chars-backward " \t")
1136 (or (memq (preceding-char) (append ";{" nil))
1137 (insert ";"))
1138 (insert "\n")
1139 (cperl-indent-line)
1140 (forward-line -1)))
1141 (forward-line -1) ; We are on the line before target
1142 (end-of-line)
1143 (newline-and-indent))
1144 (end-of-line) ; else
1145 (if (not (looking-at "\n[ \t]*$"))
1146 (newline-and-indent)
1147 (forward-line 1)
1148 (cperl-indent-line)))))
1149
1150(defun cperl-electric-semi (arg)
1151 "Insert character and correct line's indentation."
1152 (interactive "P")
1153 (if cperl-auto-newline
1154 (cperl-electric-terminator arg)
1155 (self-insert-command (prefix-numeric-value arg))))
1156
1157(defun cperl-electric-terminator (arg)
1158 "Insert character and correct line's indentation."
1159 (interactive "P")
1160 (let (insertpos (end (point)))
1161 (if (and (not arg) (eolp)
1162 (not (save-excursion
1163 (beginning-of-line)
1164 (skip-chars-forward " \t")
c07a80fd 1165 (or
1166 ;; Ignore in comment lines
1167 (= (following-char) ?#)
1168 ;; Colon is special only after a label
1169 ;; So quickly rule out most other uses of colon
1170 ;; and do no indentation for them.
1171 (and (eq last-command-char ?:)
1172 (save-excursion
1173 (forward-word 1)
1174 (skip-chars-forward " \t")
1175 (and (< (point) end)
1176 (progn (goto-char (- end 1))
1177 (not (looking-at ":"))))))
1178 (progn
1179 (beginning-of-defun)
1180 (let ((pps (parse-partial-sexp (point) end)))
1181 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
4633a7c4 1182 (progn
1183 (if cperl-auto-newline
1184 (setq insertpos (point)))
1185 (insert last-command-char)
1186 (cperl-indent-line)
1187 (if cperl-auto-newline
1188 (progn
1189 (newline)
1190 (cperl-indent-line)))
1191 (save-excursion
1192 (if insertpos (progn (goto-char insertpos)
1193 (search-forward (make-string
1194 1 last-command-char))
1195 (setq insertpos (1- (point)))))
1196 (delete-char -1))))
1197 (if insertpos
1198 (save-excursion
1199 (goto-char insertpos)
1200 (self-insert-command (prefix-numeric-value arg)))
1201 (self-insert-command (prefix-numeric-value arg)))))
1202
1203(defun cperl-inside-parens-p ()
1204 (condition-case ()
1205 (save-excursion
1206 (save-restriction
1207 (narrow-to-region (point)
1208 (progn (beginning-of-defun) (point)))
1209 (goto-char (point-max))
1210 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
1211 (error nil)))
1212\f
1213(defun cperl-indent-command (&optional whole-exp)
1214 (interactive "P")
1215 "Indent current line as Perl code, or in some cases insert a tab character.
1216If `cperl-tab-always-indent' is non-nil (the default), always indent current line.
1217Otherwise, indent the current line only if point is at the left margin
1218or in the line's indentation; otherwise insert a tab.
1219
1220A numeric argument, regardless of its value,
1221means indent rigidly all the lines of the expression starting after point
1222so that this line becomes properly indented.
1223The relative indentation among the lines of the expression are preserved."
1224 (if whole-exp
1225 ;; If arg, always indent this line as Perl
1226 ;; and shift remaining lines of expression the same amount.
1227 (let ((shift-amt (cperl-indent-line))
1228 beg end)
1229 (save-excursion
1230 (if cperl-tab-always-indent
1231 (beginning-of-line))
1232 (setq beg (point))
1233 (forward-sexp 1)
1234 (setq end (point))
1235 (goto-char beg)
1236 (forward-line 1)
1237 (setq beg (point)))
1238 (if (> end beg)
1239 (indent-code-rigidly beg end shift-amt "#")))
1240 (if (and (not cperl-tab-always-indent)
1241 (save-excursion
1242 (skip-chars-backward " \t")
1243 (not (bolp))))
1244 (insert-tab)
1245 (cperl-indent-line))))
1246
1247(defun cperl-indent-line (&optional symbol)
1248 "Indent current line as Perl code.
1249Return the amount the indentation changed by."
1250 (let (indent
1251 beg shift-amt
1252 (case-fold-search nil)
1253 (pos (- (point-max) (point))))
1254 (setq indent (cperl-calculate-indent nil symbol))
1255 (beginning-of-line)
1256 (setq beg (point))
1257 (cond ((eq indent nil)
1258 (setq indent (current-indentation)))
1259 ;;((eq indent t) ; Never?
1260 ;; (setq indent (cperl-calculate-indent-within-comment)))
1261 ;;((looking-at "[ \t]*#")
1262 ;; (setq indent 0))
1263 (t
1264 (skip-chars-forward " \t")
1265 (if (listp indent) (setq indent (car indent)))
1266 (cond ((looking-at "[A-Za-z]+:[^:]")
1267 (and (> indent 0)
1268 (setq indent (max cperl-min-label-indent
1269 (+ indent cperl-label-offset)))))
4633a7c4 1270 ((= (following-char) ?})
1271 (setq indent (- indent cperl-indent-level)))
1272 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
1273 (setq indent (+ indent cperl-close-paren-offset)))
1274 ((= (following-char) ?{)
1275 (setq indent (+ indent cperl-brace-offset))))))
1276 (skip-chars-forward " \t")
1277 (setq shift-amt (- indent (current-column)))
1278 (if (zerop shift-amt)
1279 (if (> (- (point-max) pos) (point))
1280 (goto-char (- (point-max) pos)))
1281 (delete-region beg (point))
1282 (indent-to indent)
1283 ;; If initial point was within line's indentation,
1284 ;; position after the indentation. Else stay at same point in text.
1285 (if (> (- (point-max) pos) (point))
1286 (goto-char (- (point-max) pos))))
1287 shift-amt))
1288
c07a80fd 1289(defun cperl-after-label ()
4633a7c4 1290 ;; Returns true if the point is after label. Does not do save-excursion.
1291 (and (eq (preceding-char) ?:)
1292 (memq (char-syntax (char-after (- (point) 2)))
1293 '(?w ?_))
1294 (progn
1295 (backward-sexp)
1296 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:"))))
1297
c07a80fd 1298(defun cperl-get-state (&optional parse-start start-state)
1299 ;; returns list (START STATE DEPTH PRESTART), START is a good place
1300 ;; to start parsing, STATE is what is returned by
1301 ;; `parse-partial-sexp'. DEPTH is true is we are immediately after
1302 ;; end of block which contains START. PRESTART is the position
1303 ;; basing on which START was found.
4633a7c4 1304 (save-excursion
c07a80fd 1305 (let ((start-point (point)) depth state start prestart)
4633a7c4 1306 (if parse-start
1307 (goto-char parse-start)
1308 (beginning-of-defun))
c07a80fd 1309 (setq prestart (point))
4633a7c4 1310 (if start-state nil
c07a80fd 1311 ;; Try to go out, if sub is not on the outermost level
1312 (while (< (point) start-point)
1313 (setq start (point) parse-start start depth nil
1314 state (parse-partial-sexp start start-point -1))
4633a7c4 1315 (if (> (car state) -1) nil
1316 ;; The current line could start like }}}, so the indentation
1317 ;; corresponds to a different level than what we reached
c07a80fd 1318 (setq depth t)
4633a7c4 1319 (beginning-of-line 2))) ; Go to the next line.
c07a80fd 1320 (if start (goto-char start))) ; Not at the start of file
1321 (setq start (point))
1322 (if (< start start-point) (setq parse-start start))
1323 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
1324 (list start state depth prestart))))
1325
1326(defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
1327 ;; Positions is before ?\{. Checks whether it starts a block.
1328 ;; No save-excursion!
1329 (cperl-backward-to-noncomment (point-min))
1330 ;;(skip-chars-backward " \t\n\f")
1331 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
4633a7c4 1332 ; Label may be mixed up with `$blah :'
c07a80fd 1333 (save-excursion (cperl-after-label))
1334 (and (eq (char-syntax (preceding-char)) ?w)
1335 (progn
1336 (backward-sexp)
1337 (or (looking-at "\\sw+[ \t\n\f]*[{#]") ; Method call syntax
4633a7c4 1338 (progn
c07a80fd 1339 (skip-chars-backward " \t\n\f")
1340 (and (eq (char-syntax (preceding-char)) ?w)
1341 (progn
1342 (backward-sexp)
1343 (looking-at
1344 "sub[ \t]+\\sw+[ \t\n\f]*[#{]")))))))))
4633a7c4 1345
c07a80fd 1346(defun cperl-calculate-indent (&optional parse-start symbol)
1347 "Return appropriate indentation for current line as Perl code.
1348In usual case returns an integer: the column to indent to.
1349Returns nil if line starts inside a string, t if in a comment."
1350 (save-excursion
1351 (if (memq (get-text-property (point) 'syntax-type) '(pod here-doc)) nil
1352 (beginning-of-line)
1353 (let* ((indent-point (point))
1354 (case-fold-search nil)
1355 (s-s (cperl-get-state))
1356 (start (nth 0 s-s))
1357 (state (nth 1 s-s))
1358 (containing-sexp (car (cdr state)))
1359 (char-after (save-excursion
1360 (skip-chars-forward " \t")
1361 (following-char)))
1362 (start-indent (save-excursion
1363 (goto-char start)
1364 (- (current-indentation)
1365 (if (nth 2 s-s) cperl-indent-level 0))))
1366 old-indent)
1367 ;; (or parse-start (null symbol)
1368 ;; (setq parse-start (symbol-value symbol)
1369 ;; start-indent (nth 2 parse-start)
1370 ;; parse-start (car parse-start)))
1371 ;; (if parse-start
1372 ;; (goto-char parse-start)
1373 ;; (beginning-of-defun))
1374 ;; ;; Try to go out
1375 ;; (while (< (point) indent-point)
1376 ;; (setq start (point) parse-start start moved nil
1377 ;; state (parse-partial-sexp start indent-point -1))
1378 ;; (if (> (car state) -1) nil
1379 ;; ;; The current line could start like }}}, so the indentation
1380 ;; ;; corresponds to a different level than what we reached
1381 ;; (setq moved t)
1382 ;; (beginning-of-line 2))) ; Go to the next line.
1383 ;; (if start ; Not at the start of file
1384 ;; (progn
1385 ;; (goto-char start)
1386 ;; (setq start-indent (current-indentation))
1387 ;; (if moved ; Should correct...
1388 ;; (setq start-indent (- start-indent cperl-indent-level))))
1389 ;; (setq start-indent 0))
1390 ;; (if (< (point) indent-point) (setq parse-start (point)))
1391 ;; (or state (setq state (parse-partial-sexp
1392 ;; (point) indent-point -1 nil start-state)))
1393 ;; (setq containing-sexp
1394 ;; (or (car (cdr state))
1395 ;; (and (>= (nth 6 state) 0) old-containing-sexp))
1396 ;; old-containing-sexp nil start-state nil)
1397;;;; (while (< (point) indent-point)
1398;;;; (setq parse-start (point))
1399;;;; (setq state (parse-partial-sexp (point) indent-point -1 nil start-state))
1400;;;; (setq containing-sexp
1401;;;; (or (car (cdr state))
1402;;;; (and (>= (nth 6 state) 0) old-containing-sexp))
1403;;;; old-containing-sexp nil start-state nil))
1404 ;; (if symbol (set symbol (list indent-point state start-indent)))
1405 ;; (goto-char indent-point)
1406 (cond ((or (nth 3 state) (nth 4 state))
1407 ;; return nil or t if should not change this line
1408 (nth 4 state))
1409 ((null containing-sexp)
1410 ;; Line is at top level. May be data or function definition,
1411 ;; or may be function argument declaration.
1412 ;; Indent like the previous top level line
1413 ;; unless that ends in a closeparen without semicolon,
1414 ;; in which case this line is the first argument decl.
1415 (skip-chars-forward " \t")
1416 (+ start-indent
1417 (if (= (following-char) ?{) cperl-continued-brace-offset 0)
1418 (progn
1419 (cperl-backward-to-noncomment (or parse-start (point-min)))
1420 ;;(skip-chars-backward " \t\f\n")
1421 ;; Look at previous line that's at column 0
1422 ;; to determine whether we are in top-level decls
1423 ;; or function's arg decls. Set basic-indent accordingly.
1424 ;; Now add a little if this is a continuation line.
1425 (if (or (bobp)
1426 (memq (preceding-char) (append " ;}" nil)) ; Was ?\)
1427 (memq char-after (append ")]}" nil)))
1428 0
1429 cperl-continued-statement-offset))))
1430 ((/= (char-after containing-sexp) ?{)
1431 ;; line is expression, not statement:
1432 ;; indent to just after the surrounding open,
1433 ;; skip blanks if we do not close the expression.
1434 (goto-char (1+ containing-sexp))
1435 (or (memq char-after (append ")]}" nil))
1436 (looking-at "[ \t]*\\(#\\|$\\)")
1437 (skip-chars-forward " \t"))
1438 (current-column))
1439 ((progn
1440 ;; Containing-expr starts with \{. Check whether it is a hash.
1441 (goto-char containing-sexp)
1442 (not (cperl-block-p)))
1443 (goto-char (1+ containing-sexp))
1444 (or (eq char-after ?\})
1445 (looking-at "[ \t]*\\(#\\|$\\)")
1446 (skip-chars-forward " \t"))
1447 (+ (current-column) ; Correct indentation of trailing ?\}
1448 (if (eq char-after ?\}) (+ cperl-indent-level
1449 cperl-close-paren-offset)
1450 0)))
1451 (t
1452 ;; Statement level. Is it a continuation or a new statement?
1453 ;; Find previous non-comment character.
1454 (goto-char indent-point)
1455 (cperl-backward-to-noncomment containing-sexp)
1456 ;; Back up over label lines, since they don't
1457 ;; affect whether our line is a continuation.
1458 (while (or (eq (preceding-char) ?\,)
1459 (and (eq (preceding-char) ?:)
1460 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
1461 (memq (char-syntax (char-after (- (point) 2)))
1462 '(?w ?_)))))
1463 (if (eq (preceding-char) ?\,)
1464 ;; Will go to beginning of line, essentially.
1465 ;; Will ignore embedded sexpr XXXX.
1466 (cperl-backward-to-start-of-continued-exp containing-sexp))
1467 (beginning-of-line)
1468 (cperl-backward-to-noncomment containing-sexp))
1469 ;; Now we get the answer.
1470 (if (not (memq (preceding-char) (append ", ;}{" '(nil)))) ; Was ?\,
1471 ;; This line is continuation of preceding line's statement;
1472 ;; indent `cperl-continued-statement-offset' more than the
1473 ;; previous line of the statement.
4633a7c4 1474 (progn
c07a80fd 1475 (cperl-backward-to-start-of-continued-exp containing-sexp)
1476 (+ (if (memq char-after (append "}])" nil))
1477 0 ; Closing parenth
1478 cperl-continued-statement-offset)
1479 (current-column)
1480 (if (eq char-after ?\{)
1481 cperl-continued-brace-offset 0)))
1482 ;; This line starts a new statement.
1483 ;; Position following last unclosed open.
1484 (goto-char containing-sexp)
1485 ;; Is line first statement after an open-brace?
1486 (or
1487 ;; If no, find that first statement and indent like
1488 ;; it. If the first statement begins with label, do
1489 ;; not belive when the indentation of the label is too
1490 ;; small.
1491 (save-excursion
1492 (forward-char 1)
1493 (setq old-indent (current-indentation))
1494 (let ((colon-line-end 0))
1495 (while (progn (skip-chars-forward " \t\n")
1496 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]"))
1497 ;; Skip over comments and labels following openbrace.
1498 (cond ((= (following-char) ?\#)
1499 (forward-line 1))
1500 ;; label:
1501 (t
1502 (save-excursion (end-of-line)
1503 (setq colon-line-end (point)))
1504 (search-forward ":"))))
1505 ;; The first following code counts
1506 ;; if it is before the line we want to indent.
1507 (and (< (point) indent-point)
1508 (if (> colon-line-end (point)) ; After label
1509 (if (> (current-indentation)
1510 cperl-min-label-indent)
1511 (- (current-indentation) cperl-label-offset)
1512 ;; Do not belive: `max' is involved
1513 (+ old-indent cperl-indent-level))
1514 (current-column)))))
1515 ;; If no previous statement,
1516 ;; indent it relative to line brace is on.
1517 ;; For open brace in column zero, don't let statement
1518 ;; start there too. If cperl-indent-level is zero,
1519 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
1520 ;; For open-braces not the first thing in a line,
1521 ;; add in cperl-brace-imaginary-offset.
1522
1523 ;; If first thing on a line: ?????
1524 (+ (if (and (bolp) (zerop cperl-indent-level))
1525 (+ cperl-brace-offset cperl-continued-statement-offset)
1526 cperl-indent-level)
1527 ;; Move back over whitespace before the openbrace.
1528 ;; If openbrace is not first nonwhite thing on the line,
1529 ;; add the cperl-brace-imaginary-offset.
1530 (progn (skip-chars-backward " \t")
1531 (if (bolp) 0 cperl-brace-imaginary-offset))
1532 ;; If the openbrace is preceded by a parenthesized exp,
1533 ;; move to the beginning of that;
1534 ;; possibly a different line
1535 (progn
1536 (if (eq (preceding-char) ?\))
1537 (forward-sexp -1))
1538 ;; Get initial indentation of the line we are on.
1539 ;; If line starts with label, calculate label indentation
1540 (if (save-excursion
1541 (beginning-of-line)
1542 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_]*:[^:]"))
1543 (if (> (current-indentation) cperl-min-label-indent)
1544 (- (current-indentation) cperl-label-offset)
1545 (cperl-calculate-indent
1546 (if (and parse-start (<= parse-start (point)))
1547 parse-start)))
1548 (current-indentation))))))))))))
4633a7c4 1549
1550(defvar cperl-indent-alist
1551 '((string nil)
1552 (comment nil)
1553 (toplevel 0)
1554 (toplevel-after-parenth 2)
1555 (toplevel-continued 2)
1556 (expression 1))
1557 "Alist of indentation rules for CPerl mode.
1558The values mean:
1559 nil: do not indent;
1560 number: add this amount of indentation.")
1561
1562(defun cperl-where-am-i (&optional parse-start start-state)
1563 ;; Unfinished
c07a80fd 1564 "Return a list of lists ((TYPE POS)...) of good points before the point.
4633a7c4 1565POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'."
1566 (save-excursion
c07a80fd 1567 (let* ((start-point (point))
1568 (s-s (cperl-get-state))
1569 (start (nth 0 s-s))
1570 (state (nth 1 s-s))
1571 (prestart (nth 3 s-s))
1572 (containing-sexp (car (cdr state)))
1573 (case-fold-search nil)
1574 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
4633a7c4 1575 (cond ((nth 3 state) ; In string
c07a80fd 1576 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
4633a7c4 1577 ((nth 4 state) ; In comment
c07a80fd 1578 (setq res (cons '(comment) res)))
4633a7c4 1579 ((null containing-sexp)
1580 ;; Line is at top level.
1581 ;; Indent like the previous top level line
1582 ;; unless that ends in a closeparen without semicolon,
1583 ;; in which case this line is the first argument decl.
1584 (cperl-backward-to-noncomment (or parse-start (point-min)))
c07a80fd 1585 ;;(skip-chars-backward " \t\f\n")
4633a7c4 1586 (cond
1587 ((or (bobp)
1588 (memq (preceding-char) (append ";}" nil)))
c07a80fd 1589 (setq res (cons (list 'toplevel start) res)))
4633a7c4 1590 ((eq (preceding-char) ?\) )
c07a80fd 1591 (setq res (cons (list 'toplevel-after-parenth start) res)))
1592 (t
1593 (setq res (cons (list 'toplevel-continued start) res)))))
4633a7c4 1594 ((/= (char-after containing-sexp) ?{)
1595 ;; line is expression, not statement:
1596 ;; indent to just after the surrounding open.
c07a80fd 1597 ;; skip blanks if we do not close the expression.
1598 (setq res (cons (list 'expression-blanks
1599 (progn
1600 (goto-char (1+ containing-sexp))
1601 (or (looking-at "[ \t]*\\(#\\|$\\)")
1602 (skip-chars-forward " \t"))
1603 (point)))
1604 (cons (list 'expression containing-sexp) res))))
4633a7c4 1605 ((progn
1606 ;; Containing-expr starts with \{. Check whether it is a hash.
1607 (goto-char containing-sexp)
c07a80fd 1608 (not (cperl-block-p)))
1609 (setq res (cons (list 'expression-blanks
1610 (progn
1611 (goto-char (1+ containing-sexp))
1612 (or (looking-at "[ \t]*\\(#\\|$\\)")
1613 (skip-chars-forward " \t"))
1614 (point)))
1615 (cons (list 'expression containing-sexp) res))))
4633a7c4 1616 (t
c07a80fd 1617 ;; Statement level.
1618 (setq res (cons (list 'in-block containing-sexp) res))
1619 ;; Is it a continuation or a new statement?
4633a7c4 1620 ;; Find previous non-comment character.
1621 (cperl-backward-to-noncomment containing-sexp)
1622 ;; Back up over label lines, since they don't
1623 ;; affect whether our line is a continuation.
c07a80fd 1624 ;; Back up comma-delimited lines too ?????
4633a7c4 1625 (while (or (eq (preceding-char) ?\,)
c07a80fd 1626 (save-excursion (cperl-after-label)))
4633a7c4 1627 (if (eq (preceding-char) ?\,)
c07a80fd 1628 ;; Will go to beginning of line, essentially
1629 ;; Will ignore embedded sexpr XXXX.
4633a7c4 1630 (cperl-backward-to-start-of-continued-exp containing-sexp))
1631 (beginning-of-line)
1632 (cperl-backward-to-noncomment containing-sexp))
1633 ;; Now we get the answer.
1634 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
1635 ;; This line is continuation of preceding line's statement.
c07a80fd 1636 (list (list 'statement-continued containing-sexp))
4633a7c4 1637 ;; This line starts a new statement.
1638 ;; Position following last unclosed open.
1639 (goto-char containing-sexp)
1640 ;; Is line first statement after an open-brace?
1641 (or
1642 ;; If no, find that first statement and indent like
1643 ;; it. If the first statement begins with label, do
1644 ;; not belive when the indentation of the label is too
1645 ;; small.
1646 (save-excursion
1647 (forward-char 1)
4633a7c4 1648 (let ((colon-line-end 0))
c07a80fd 1649 (while (progn (skip-chars-forward " \t\n" start-point)
1650 (and (< (point) start-point)
1651 (looking-at
1652 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
4633a7c4 1653 ;; Skip over comments and labels following openbrace.
1654 (cond ((= (following-char) ?\#)
c07a80fd 1655 ;;(forward-line 1)
1656 (end-of-line))
4633a7c4 1657 ;; label:
1658 (t
1659 (save-excursion (end-of-line)
1660 (setq colon-line-end (point)))
1661 (search-forward ":"))))
c07a80fd 1662 ;; Now at the point, after label, or at start
1663 ;; of first statement in the block.
4633a7c4 1664 (and (< (point) start-point)
c07a80fd 1665 (if (> colon-line-end (point))
1666 ;; Before statement after label
4633a7c4 1667 (if (> (current-indentation)
1668 cperl-min-label-indent)
c07a80fd 1669 (list (list 'label-in-block (point)))
4633a7c4 1670 ;; Do not belive: `max' is involved
c07a80fd 1671 (list
1672 (list 'label-in-block-min-indent (point))))
1673 ;; Before statement
1674 (list 'statement-in-block (point))))))
4633a7c4 1675 ;; If no previous statement,
1676 ;; indent it relative to line brace is on.
1677 ;; For open brace in column zero, don't let statement
1678 ;; start there too. If cperl-indent-level is zero,
1679 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
1680 ;; For open-braces not the first thing in a line,
1681 ;; add in cperl-brace-imaginary-offset.
1682
1683 ;; If first thing on a line: ?????
1684 (+ (if (and (bolp) (zerop cperl-indent-level))
1685 (+ cperl-brace-offset cperl-continued-statement-offset)
1686 cperl-indent-level)
1687 ;; Move back over whitespace before the openbrace.
1688 ;; If openbrace is not first nonwhite thing on the line,
1689 ;; add the cperl-brace-imaginary-offset.
1690 (progn (skip-chars-backward " \t")
1691 (if (bolp) 0 cperl-brace-imaginary-offset))
1692 ;; If the openbrace is preceded by a parenthesized exp,
1693 ;; move to the beginning of that;
1694 ;; possibly a different line
1695 (progn
1696 (if (eq (preceding-char) ?\))
1697 (forward-sexp -1))
1698 ;; Get initial indentation of the line we are on.
1699 ;; If line starts with label, calculate label indentation
1700 (if (save-excursion
1701 (beginning-of-line)
1702 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_]*:[^:]"))
1703 (if (> (current-indentation) cperl-min-label-indent)
1704 (- (current-indentation) cperl-label-offset)
1705 (cperl-calculate-indent
c07a80fd 1706 (if (and parse-start (<= parse-start (point)))
1707 parse-start)))
1708 (current-indentation))))))))
1709 res)))
4633a7c4 1710
1711(defun cperl-calculate-indent-within-comment ()
1712 "Return the indentation amount for line, assuming that
1713the current line is to be regarded as part of a block comment."
1714 (let (end star-start)
1715 (save-excursion
1716 (beginning-of-line)
1717 (skip-chars-forward " \t")
1718 (setq end (point))
1719 (and (= (following-char) ?#)
1720 (forward-line -1)
1721 (cperl-to-comment-or-eol)
1722 (setq end (point)))
1723 (goto-char end)
1724 (current-column))))
1725
1726
1727(defun cperl-to-comment-or-eol ()
1728 "Goes to position before comment on the current line, or to end of line.
1729Returns true if comment is found."
1730 (let (state stop-in cpoint (lim (progn (end-of-line) (point))))
1731 (beginning-of-line)
1732 (if (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)
1733 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
1734 ;; Else
1735 (while (not stop-in)
1736 (setq state (parse-partial-sexp (point) lim nil nil nil t))
1737 ; stop at comment
1738 ;; If fails (beginning-of-line inside sexp), then contains not-comment
1739 ;; Do simplified processing
1740 ;;(if (re-search-forward "[^$]#" lim 1)
1741 ;; (progn
1742 ;; (forward-char -1)
1743 ;; (skip-chars-backward " \t\n\f" lim))
1744 ;; (goto-char lim)) ; No `#' at all
1745 ;;)
1746 (if (nth 4 state) ; After `#';
1747 ; (nth 2 state) can be
1748 ; beginning of m,s,qq and so
1749 ; on
1750 (if (nth 2 state)
1751 (progn
1752 (setq cpoint (point))
1753 (goto-char (nth 2 state))
1754 (cond
1755 ((looking-at "\\(s\\|tr\\)\\>")
1756 (or (re-search-forward
1757 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
1758 lim 'move)
1759 (setq stop-in t)))
1760 ((looking-at "\\(m\\|q\\([qxw]\\)?\\)\\>")
1761 (or (re-search-forward
1762 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
1763 lim 'move)
1764 (setq stop-in t)))
1765 (t ; It was fair comment
1766 (setq stop-in t) ; Finish
1767 (goto-char (1- cpoint)))))
1768 (setq stop-in t) ; Finish
1769 (forward-char -1))
1770 (setq stop-in t)) ; Finish
1771 )
1772 (nth 4 state))))
1773
c07a80fd 1774(defun cperl-find-pods-heres (&optional min max)
1775 "Scans the buffer for POD sections and here-documents.
1776If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
1777the sections using `cperl-pod-head-face', `cperl-pod-face',
1778`cperl-here-face'."
1779 (interactive)
1780 (or min (setq min (point-min)))
1781 (or max (setq max (point-max)))
1782 (let (face head-face here-face b e bb tag err
1783 (cperl-pod-here-fontify (eval cperl-pod-here-fontify))
1784 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
1785 (modified (buffer-modified-p)))
1786 (unwind-protect
1787 (progn
1788 (save-excursion
1789 (message "Scanning for pods and here-docs...")
1790 (if cperl-pod-here-fontify
1791 (setq face (eval cperl-pod-face)
1792 head-face (eval cperl-pod-head-face)
1793 here-face (eval cperl-here-face)))
1794 (remove-text-properties min max '(syntax-type t))
1795 ;; Need to remove face as well...
1796 (goto-char min)
29043b61 1797 (while (re-search-forward "\\(\\`\n?\\|\n\n\\)=" max t)
1798 (if (looking-at "\n*cut\\>")
c07a80fd 1799 (progn
1800 (message "=cut is not preceeded by a pod section")
1801 (setq err (point)))
1802 (beginning-of-line)
1803 (setq b (point) bb b)
29043b61 1804 (or (re-search-forward "\n\n=cut\\>" max 'toend)
1805 (message "Cannot find the end of a pod section"))
1806 (beginning-of-line 4)
c07a80fd 1807 (setq e (point))
1808 (put-text-property b e 'in-pod t)
1809 (goto-char b)
1810 (while (re-search-forward "\n\n[ \t]" e t)
1811 (beginning-of-line)
1812 (put-text-property b (point) 'syntax-type 'pod)
29043b61 1813 (put-text-property (max (point-min) (1- b))
1814 (point) cperl-do-not-fontify t)
c07a80fd 1815 (if cperl-pod-here-fontify (put-text-property b (point) 'face face))
1816 (re-search-forward "\n\n[^ \t\f]" e 'toend)
1817 (beginning-of-line)
1818 (setq b (point)))
1819 (put-text-property (point) e 'syntax-type 'pod)
29043b61 1820 (put-text-property (max (point-min) (1- (point)))
1821 e cperl-do-not-fontify t)
c07a80fd 1822 (if cperl-pod-here-fontify
1823 (progn (put-text-property (point) e 'face face)
1824 (goto-char bb)
1825 (while (re-search-forward
1826 ;; One paragraph
29043b61 1827 "\n\n=[a-zA-Z0-9]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
c07a80fd 1828 e 'toend)
1829 (put-text-property
1830 (match-beginning 1) (match-end 1)
1831 'face head-face))))
1832 (goto-char e)))
1833 (goto-char min)
1834 (while (re-search-forward
1835 "<<\\(\\([\"'`]\\)?\\)\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\1"
1836 max t)
1837 (setq tag (buffer-substring (match-beginning 3)
1838 (match-end 3)))
1839 (if cperl-pod-here-fontify
1840 (put-text-property (match-beginning 3) (match-end 3)
1841 'face font-lock-reference-face))
1842 (forward-line)
1843 (setq b (point))
1844 (and (re-search-forward (concat "^" tag "$") max 'toend)
1845 (progn
1846 (if cperl-pod-here-fontify
1847 (progn
1848 (put-text-property (match-beginning 0) (match-end 0)
1849 'face font-lock-reference-face)
29043b61 1850 (put-text-property (max (point-min) (1- b))
1851 (min (point-mox)
1852 (1+ (match-end 0)))
1853 cperl-do-not-fontify t)
c07a80fd 1854 (put-text-property b (match-beginning 0)
29043b61 1855 'face here-face)))
c07a80fd 1856 (put-text-property b (match-beginning 0)
1857 'syntax-type 'here-doc)))))
1858 (if err (goto-char err)
1859 (message "Scan for pods and here-docs completed.")))
1860 (and (buffer-modified-p)
1861 (not modified)
1862 (set-buffer-modified-p nil)))))
1863
1864(defun cperl-backward-to-noncomment (lim)
1865 ;; Stops at lim or after non-whitespace that is not in comment
4633a7c4 1866 (let (stop p)
1867 (while (and (not stop) (> (point) (or lim 1)))
1868 (skip-chars-backward " \t\n\f" lim)
1869 (setq p (point))
1870 (beginning-of-line)
1871 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
1872 ;; Else
1873 (cperl-to-comment-or-eol)
1874 (skip-chars-backward " \t")
1875 (if (< p (point)) (goto-char p))
1876 (setq stop t)))))
1877
c07a80fd 1878(defun cperl-after-expr-p (&optional lim chars test)
4633a7c4 1879 "Returns true if the position is good for start of expression.
1880TEST is the expression to evaluate at the found position. If absent,
1881CHARS is a string that contains good characters to have before us."
1882 (let (stop p)
1883 (save-excursion
1884 (while (and (not stop) (> (point) (or lim 1)))
1885 (skip-chars-backward " \t\n\f" lim)
1886 (setq p (point))
1887 (beginning-of-line)
1888 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
1889 ;; Else: last iteration (What to do with labels?)
1890 (cperl-to-comment-or-eol)
1891 (skip-chars-backward " \t")
1892 (if (< p (point)) (goto-char p))
1893 (setq stop t)))
1894 (or (bobp)
1895 (progn
1896 (backward-char 1)
1897 (if test (eval test)
1898 (memq (following-char) (append (or chars "{};") nil))))))))
1899
1900(defun cperl-backward-to-start-of-continued-exp (lim)
c07a80fd 1901 (if (memq (preceding-char) (append ")]}\"'`" nil))
4633a7c4 1902 (forward-sexp -1))
1903 (beginning-of-line)
1904 (if (<= (point) lim)
1905 (goto-char (1+ lim)))
1906 (skip-chars-forward " \t"))
1907
4633a7c4 1908\f
1909(defvar innerloop-done nil)
1910(defvar last-depth nil)
1911
1912(defun cperl-indent-exp ()
1913 "Simple variant of indentation of continued-sexp.
1914Should be slow. Will not indent comment if it starts at `comment-indent'
1915or looks like continuation of the comment on the previous line."
1916 (interactive)
1917 (save-excursion
1918 (let ((tmp-end (progn (end-of-line) (point))) top done)
1919 (save-excursion
1920 (while (null done)
1921 (beginning-of-line)
1922 (setq top (point))
1923 (while (= (nth 0 (parse-partial-sexp (point) tmp-end
1924 -1)) -1)
1925 (setq top (point))) ; Get the outermost parenths in line
1926 (goto-char top)
1927 (while (< (point) tmp-end)
1928 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
1929 (or (eolp) (forward-sexp 1)))
1930 (if (> (point) tmp-end) (progn (end-of-line) (setq tmp-end (point)))
1931 (setq done t)))
1932 (goto-char tmp-end)
1933 (setq tmp-end (point-marker)))
1934 (cperl-indent-region (point) tmp-end))))
1935
1936(defun cperl-indent-region (start end)
1937 "Simple variant of indentation of region in CPerl mode.
1938Should be slow. Will not indent comment if it starts at `comment-indent'
1939or looks like continuation of the comment on the previous line.
1940Indents all the lines whose first character is between START and END
1941inclusive."
1942 (interactive "r")
1943 (save-excursion
1944 (let (st comm indent-info old-comm-indent new-comm-indent
1945 (pm 0) (imenu-scanning-message "Indenting... (%3d%%)"))
1946 (goto-char start)
1947 (setq old-comm-indent (and (cperl-to-comment-or-eol)
1948 (current-column))
1949 new-comm-indent old-comm-indent)
1950 (goto-char start)
1951 (or (bolp) (beginning-of-line 2))
1952 (or (fboundp 'imenu-progress-message)
1953 (message "Indenting... For feedback load `imenu'..."))
1954 (while (and (<= (point) end) (not (eobp))) ; bol to check start
1955 (and (fboundp 'imenu-progress-message)
1956 (imenu-progress-message
1957 pm (/ (* 100 (- (point) start)) (- end start -1))))
1958 (setq st (point)
1959 indent-info nil
1960 ) ; Believe indentation of the current
1961 (if (and (setq comm (looking-at "[ \t]*#"))
1962 (or (eq (current-indentation) (or old-comm-indent
1963 comment-column))
1964 (setq old-comm-indent nil)))
1965 (if (and old-comm-indent
1966 (= (current-indentation) old-comm-indent))
1967 (let ((comment-column new-comm-indent))
1968 (indent-for-comment)))
1969 (progn
1970 (cperl-indent-line 'indent-info)
1971 (or comm
1972 (progn
1973 (if (setq old-comm-indent (and (cperl-to-comment-or-eol)
1974 (current-column)))
1975 (progn (indent-for-comment)
1976 (skip-chars-backward " \t")
1977 (skip-chars-backward "#")
1978 (setq new-comm-indent (current-column))))))))
1979 (beginning-of-line 2))
1980 (if (fboundp 'imenu-progress-message)
1981 (imenu-progress-message pm 100)
1982 (message nil)))))
1983
1984(defun cperl-slash-is-regexp (&optional pos)
1985 (save-excursion
1986 (goto-char (if pos pos (1- (point))))
1987 (and
1988 (not (memq (get-text-property (point) 'face)
1989 '(font-lock-string-face font-lock-comment-face)))
c07a80fd 1990 (cperl-after-expr-p nil nil '
4633a7c4 1991 (or (looking-at "[^]a-zA-Z0-9_)}]")
1992 (eq (get-text-property (point) 'face)
1993 'font-lock-keyword-face))))))
1994
1995;; Stolen from lisp-mode with a lot of improvements
1996
1997(defun cperl-fill-paragraph (&optional justify iteration)
1998 "Like \\[fill-paragraph], but handle CPerl comments.
1999If any of the current line is a comment, fill the comment or the
2000block of it that point is in, preserving the comment's initial
2001indentation and initial hashes. Behaves usually outside of comment."
2002 (interactive "P")
2003 (let (
2004 ;; Non-nil if the current line contains a comment.
2005 has-comment
2006
2007 ;; If has-comment, the appropriate fill-prefix for the comment.
2008 comment-fill-prefix
2009 ;; Line that contains code and comment (or nil)
2010 start
2011 c spaces len dc (comment-column comment-column))
2012 ;; Figure out what kind of comment we are looking at.
2013 (save-excursion
2014 (beginning-of-line)
2015 (cond
2016
2017 ;; A line with nothing but a comment on it?
2018 ((looking-at "[ \t]*#[# \t]*")
2019 (setq has-comment t
2020 comment-fill-prefix (buffer-substring (match-beginning 0)
2021 (match-end 0))))
2022
2023 ;; A line with some code, followed by a comment? Remember that the
2024 ;; semi which starts the comment shouldn't be part of a string or
2025 ;; character.
2026 ((cperl-to-comment-or-eol)
2027 (setq has-comment t)
2028 (looking-at "#+[ \t]*")
2029 (setq start (point) c (current-column)
2030 comment-fill-prefix
2031 (concat (make-string (current-column) ?\ )
2032 (buffer-substring (match-beginning 0) (match-end 0)))
2033 spaces (progn (skip-chars-backward " \t")
2034 (buffer-substring (point) start))
2035 dc (- c (current-column)) len (- start (point))
2036 start (point-marker))
2037 (delete-char len)
2038 (insert (make-string dc ?-)))))
2039 (if (not has-comment)
2040 (fill-paragraph justify) ; Do the usual thing outside of comment
2041 ;; Narrow to include only the comment, and then fill the region.
2042 (save-restriction
2043 (narrow-to-region
2044 ;; Find the first line we should include in the region to fill.
2045 (if start (progn (beginning-of-line) (point))
2046 (save-excursion
2047 (while (and (zerop (forward-line -1))
c07a80fd 2048 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
4633a7c4 2049 ;; We may have gone to far. Go forward again.
c07a80fd 2050 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
4633a7c4 2051 (forward-line 1))
2052 (point)))
2053 ;; Find the beginning of the first line past the region to fill.
2054 (save-excursion
2055 (while (progn (forward-line 1)
c07a80fd 2056 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
4633a7c4 2057 (point)))
2058 ;; Remove existing hashes
2059 (goto-char (point-min))
2060 (while (progn (forward-line 1) (< (point) (point-max)))
2061 (skip-chars-forward " \t")
2062 (and (looking-at "#+")
2063 (delete-char (- (match-end 0) (match-beginning 0)))))
2064
2065 ;; Lines with only hashes on them can be paragraph boundaries.
2066 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
2067 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
2068 (fill-prefix comment-fill-prefix))
2069 (fill-paragraph justify)))
2070 (if (and start)
2071 (progn
2072 (goto-char start)
2073 (if (> dc 0)
2074 (progn (delete-char dc) (insert spaces)))
2075 (if (or (= (current-column) c) iteration) nil
2076 (setq comment-column c)
2077 (indent-for-comment)
2078 ;; Repeat once more, flagging as iteration
2079 (cperl-fill-paragraph justify t)))))))
2080
2081(defun cperl-do-auto-fill ()
2082 ;; Break out if the line is short enough
2083 (if (> (save-excursion
2084 (end-of-line)
2085 (current-column))
2086 fill-column)
2087 (let ((c (save-excursion (beginning-of-line)
2088 (cperl-to-comment-or-eol) (point)))
2089 (s (memq (following-char) '(?\ ?\t))) marker)
2090 (if (>= c (point)) nil
2091 (setq marker (point-marker))
2092 (cperl-fill-paragraph)
2093 (goto-char marker)
2094 ;; Is not enough, sometimes marker is a start of line
2095 (if (bolp) (progn (re-search-forward "#+[ \t]*")
2096 (goto-char (match-end 0))))
2097 ;; Following space could have gone:
2098 (if (or (not s) (memq (following-char) '(?\ ?\t))) nil
2099 (insert " ")
2100 (backward-char 1))
2101 ;; Previous space could have gone:
2102 (or (memq (preceding-char) '(?\ ?\t)) (insert " "))))))
2103
2104(defvar imenu-example--function-name-regexp-perl
c07a80fd 2105 "^\\([ \t]*\\(sub\\|package\\)[ \t\n]+\\([a-zA-Z_0-9:']+\\)[ \t]*\\|=head\\([12]\\)[ \t]+\\([^\n]+\\)$\\)")
4633a7c4 2106
2107(defun imenu-example--create-perl-index (&optional regexp)
2108 (require 'cl)
c07a80fd 2109 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
2110 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
2111 packages ends-ranges p
2112 (prev-pos 0) char fchar index index1 name (end-range 0) package)
4633a7c4 2113 (goto-char (point-min))
2114 (imenu-progress-message prev-pos 0)
2115 ;; Search for the function
2116 (save-match-data
2117 (while (re-search-forward
2118 (or regexp imenu-example--function-name-regexp-perl)
2119 nil t)
2120 (imenu-progress-message prev-pos)
2121 ;;(backward-up-list 1)
c07a80fd 2122 (cond
2123 ((match-beginning 2) ; package or sub
2124 (save-excursion
2125 (goto-char (match-beginning 2))
2126 (setq fchar (following-char))
2127 )
2128 (setq char (following-char))
2129 (setq p (point))
2130 (while (and ends-ranges (>= p (car ends-ranges)))
2131 ;; delete obsolete entries
2132 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
2133 (setq package (or (car packages) "")
2134 end-range (or (car ends-ranges) 0))
2135 (if (eq fchar ?p)
2136 (progn
2137 (setq name (buffer-substring (match-beginning 3) (match-end 3))
2138 package (concat name "::")
2139 name (concat "package " name)
2140 end-range
2141 (save-excursion
2142 (parse-partial-sexp (point) (point-max) -1) (point))
2143 ends-ranges (cons end-range ends-ranges)
2144 packages (cons package packages))))
2145 ;; )
2146 ;; Skip this function name if it is a prototype declaration.
2147 (if (and (eq fchar ?s) (eq char ?\;)) nil
2148 (if (eq fchar ?p) nil
2149 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
2150 (if (or (> p end-range) (string-match "[:']" name)) nil
2151 (setq name (concat package name))))
2152 (setq index (imenu-example--name-and-position))
2153 (setcar index name)
2154 (if (eq fchar ?p)
2155 (push index index-pack-alist)
2156 (push index index-alist))
2157 (push index index-unsorted-alist)))
2158 (t ; Pod section
2159 ;; (beginning-of-line)
2160 (setq index (imenu-example--name-and-position)
2161 name (buffer-substring (match-beginning 5) (match-end 5)))
2162 (if (eq (char-after (match-beginning 4)) ?2)
2163 (setq name (concat " " name)))
4633a7c4 2164 (setcar index name)
c07a80fd 2165 (setq index1 (cons (concat "=" name) (cdr index)))
2166 (push index index-pod-alist)
2167 (push index1 index-unsorted-alist)))))
4633a7c4 2168 (imenu-progress-message prev-pos 100)
c07a80fd 2169 (setq index-alist
2170 (if (default-value 'imenu-sort-function)
2171 (sort index-alist (default-value 'imenu-sort-function))
2172 (nreverse index-alist)))
2173 (and index-pod-alist
2174 (push (cons (imenu-create-submenu-name "+POD headers+")
2175 (nreverse index-pod-alist))
2176 index-alist))
4633a7c4 2177 (and index-pack-alist
c07a80fd 2178 (push (cons (imenu-create-submenu-name "+Packages+")
2179 (nreverse index-pack-alist))
2180 index-alist))
2181 (and (or index-pack-alist index-pod-alist
2182 (default-value 'imenu-sort-function))
2183 index-unsorted-alist
2184 (push (cons (imenu-create-submenu-name "+Unsorted List+")
2185 (nreverse index-unsorted-alist))
4633a7c4 2186 index-alist))
c07a80fd 2187 index-alist))
4633a7c4 2188
2189(defvar cperl-compilation-error-regexp-alist
2190 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORK).
2191 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
2192 2 3))
2193 "Alist that specifies how to match errors in perl output.")
2194
2195(if (fboundp 'eval-after-load)
2196 (eval-after-load
2197 "mode-compile"
2198 '(setq perl-compilation-error-regexp-alist
2199 cperl-compilation-error-regexp-alist)))
2200
2201
2202(defvar cperl-faces-init nil)
2203
2204(defun cperl-windowed-init ()
2205 "Initialization under windowed version."
2206 (add-hook 'font-lock-mode-hook
2207 (function
2208 (lambda ()
2209 (if (or
2210 (eq major-mode 'perl-mode)
2211 (eq major-mode 'cperl-mode))
2212 (progn
c07a80fd 2213 (or cperl-faces-init (cperl-init-faces))))))))
2214
2215(defvar perl-font-lock-keywords-1 nil
2216 "Additional expressions to highlight in Perl mode. Minimal set.")
2217(defvar perl-font-lock-keywords nil
2218 "Additional expressions to highlight in Perl mode. Default set.")
2219(defvar perl-font-lock-keywords-2 nil
2220 "Additional expressions to highlight in Perl mode. Maximal set")
4633a7c4 2221
2222(defun cperl-init-faces ()
2223 (condition-case nil
2224 (progn
2225 (require 'font-lock)
c07a80fd 2226 (and (fboundp 'font-lock-fontify-anchored-keywords)
2227 (featurep 'font-lock-extra)
2228 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
2229 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
4633a7c4 2230 ;;(defvar cperl-font-lock-enhanced nil
2231 ;; "Set to be non-nil if font-lock allows active highlights.")
c07a80fd 2232 (if (fboundp 'font-lock-fontify-anchored-keywords)
2233 (setq font-lock-anchored t))
4633a7c4 2234 (setq
2235 t-font-lock-keywords
2236 (list
2237 (cons
2238 (concat
2239 "\\(^\\|[^$@%&\\]\\)\\<\\("
2240 (mapconcat
2241 'identity
2242 '("if" "until" "while" "elsif" "else" "unless" "for"
2243 "foreach" "continue" "exit" "die" "last" "goto" "next"
2244 "redo" "return" "local" "exec" "sub" "do" "dump" "use"
2245 "require" "package" "eval" "my" "BEGIN" "END")
2246 "\\|") ; Flow control
2247 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
2248 ; In what follows we use `type' style
2249 ; for overwritable buildins
2250 (list
2251 (concat
2252 "\\(^\\|[^$@%&\\]\\)\\<\\("
2253 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm" "and" "atan2"
2254 ;; "bind" "binmode" "bless" "caller" "chdir" "chmod" "chown" "chr"
2255 ;; "chroot" "close" "closedir" "cmp" "connect" "continue" "cos"
2256 ;; "crypt" "dbmclose" "dbmopen" "die" "dump" "endgrent" "endhostent"
2257 ;; "endnetent" "endprotoent" "endpwent" "endservent" "eof" "eq" "exec"
2258 ;; "exit" "exp" "fcntl" "fileno" "flock" "fork" "formline" "ge" "getc"
2259 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr" "gethostbyname"
2260 ;; "gethostent" "getlogin" "getnetbyaddr" "getnetbyname" "getnetent"
2261 ;; "getpeername" "getpgrp" "getppid" "getpriority" "getprotobyname"
2262 ;; "getprotobynumber" "getprotoent" "getpwent" "getpwnam" "getpwuid"
2263 ;; "getservbyname" "getservbyport" "getservent" "getsockname"
2264 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int" "ioctl"
2265 ;; "join" "kill" "lc" "lcfirst" "le" "length" "link" "listen"
2266 ;; "localtime" "log" "lstat" "lt" "mkdir" "msgctl" "msgget" "msgrcv"
2267 ;; "msgsnd" "ne" "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
2268 ;; "quotemeta" "rand" "read" "readdir" "readline" "readlink"
2269 ;; "readpipe" "recv" "ref" "rename" "require" "reset" "reverse"
2270 ;; "rewinddir" "rindex" "rmdir" "seek" "seekdir" "select" "semctl"
2271 ;; "semget" "semop" "send" "setgrent" "sethostent" "setnetent"
2272 ;; "setpgrp" "setpriority" "setprotoent" "setpwent" "setservent"
2273 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite" "shutdown"
2274 ;; "sin" "sleep" "socket" "socketpair" "sprintf" "sqrt" "srand" "stat"
2275 ;; "substr" "symlink" "syscall" "sysread" "system" "syswrite" "tell"
2276 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst" "umask" "unlink"
2277 ;; "unpack" "utime" "values" "vec" "wait" "waitpid" "wantarray" "warn"
2278 ;; "write" "x" "xor"
2279 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
2280 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
2281 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
2282 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
2283 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
2284 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
2285 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
2286 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
2287 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
2288 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
2289 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
2290 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
2291 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
2292 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
2293 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
2294 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
2295 "\\(\\|ngth\\)\\|o\\(caltime\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
2296 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
2297 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
2298 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
2299 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
2300 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
2301 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
2302 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
2303 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
2304 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|tem\\|write\\)\\|"
2305 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
2306 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
2307 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
2308 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
2309 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\)"
2310 "\\)\\>") 2 'font-lock-type-face)
2311 ;; In what follows we use `other' style
2312 ;; for nonoverwritable buildins
2313 ;; Somehow 's', 'm' are not autogenerated???
2314 (list
2315 (concat
2316 "\\(^\\|[^$@%&\\]\\)\\<\\("
2317 ;; "AUTOLOAD" "BEGIN" "DESTROY" "END" "__END__" "chomp" "chop"
2318 ;; "defined" "delete" "do" "each" "else" "elsif" "eval" "exists" "for"
2319 ;; "foreach" "format" "goto" "grep" "if" "keys" "last" "local" "map"
2320 ;; "my" "next" "no" "package" "pop" "pos" "print" "printf" "push" "q"
2321 ;; "qq" "qw" "qx" "redo" "return" "scalar" "shift" "sort" "splice"
2322 ;; "split" "study" "sub" "tie" "tr" "undef" "unless" "unshift" "untie"
2323 ;; "until" "use" "while" "y"
2324 "AUTOLOAD\\|BEGIN\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
2325 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
2326 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|if\\|keys\\|"
2327 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|"
2328 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
2329 "q\\(\\|q\\|w\\|x\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
2330 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
2331 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
2332 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
2333 "\\|[sm]" ; Added manually
2334 "\\)\\>") 2 'font-lock-other-type-face)
2335 ;; (mapconcat 'identity
2336 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
2337 ;; "#include" "#define" "#undef")
2338 ;; "\\|")
2339 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
2340 font-lock-function-name-face) ; Not very good, triggers at "[a-z]"
c07a80fd 2341 '("\\<sub[ \t]+\\([^ \t{;]+\\)[ \t]*[{\n]" 1
4633a7c4 2342 font-lock-function-name-face)
c07a80fd 2343 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
4633a7c4 2344 2 font-lock-function-name-face)
c07a80fd 2345 (cond ((featurep 'font-lock-extra)
29043b61 2346 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
c07a80fd 2347 (2 font-lock-string-face t)
2348 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
2349 (font-lock-anchored
29043b61 2350 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
c07a80fd 2351 (2 font-lock-string-face t)
29043b61 2352 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
c07a80fd 2353 nil nil
2354 (1 font-lock-string-face t))))
29043b61 2355 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
c07a80fd 2356 2 font-lock-string-face t)))
29043b61 2357 '("[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
4633a7c4 2358 font-lock-string-face t)
2359 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
2360 font-lock-reference-face) ; labels
2361 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
2362 2 font-lock-reference-face)
c07a80fd 2363 (cond ((featurep 'font-lock-extra)
2364 '("^[ \t]*\\(my\\|local\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
2365 (3 font-lock-variable-name-face)
2366 (4 '(another 4 nil
2367 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
2368 (1 font-lock-variable-name-face)
2369 (2 '(restart 2 nil) nil t)))
2370 nil t))) ; local variables, multiple
2371 (font-lock-anchored
2372 '("^[ \t]*\\(my\\|local\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
2373 (3 font-lock-variable-name-face)
2374 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)"
2375 nil nil
2376 (1 font-lock-variable-name-face))))
2377 (t '("^[ \t]*\\(my\\|local\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
2378 3 font-lock-variable-name-face)))
4633a7c4 2379 '("\\<for\\(each\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
2380 2 font-lock-variable-name-face)))
c07a80fd 2381 (setq
2382 t-font-lock-keywords-1
2383 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
2384 (not (cperl-xemacs-p)) ; not yet as of XEmacs 19.12
2385 '(("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
2386 1
2387 (if (= (- (match-end 2) (match-beginning 2)) 1)
2388 (if (eq (char-after (match-beginning 3)) ?{)
2389 font-lock-other-emphasized-face
2390 font-lock-emphasized-face) ; arrays and hashes
2391 font-lock-variable-name-face) ; Just to put something
2392 t)
2393 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
2394 (if (eq (char-after (match-beginning 2)) ?%)
2395 font-lock-other-emphasized-face
2396 font-lock-emphasized-face)
2397 t) ; arrays and hashes
2398 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
4633a7c4 2399 ;;; Too much noise from \s* @s[ and friends
c07a80fd 2400 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
2401 ;;(3 font-lock-function-name-face t t)
2402 ;;(4
2403 ;; (if (cperl-slash-is-regexp)
2404 ;; font-lock-function-name-face 'default) nil t))
2405 )))
2406 (setq perl-font-lock-keywords-1 t-font-lock-keywords
2407 perl-font-lock-keywords perl-font-lock-keywords-1
2408 perl-font-lock-keywords-2 (append
2409 t-font-lock-keywords
2410 t-font-lock-keywords-1)))
4633a7c4 2411 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
c07a80fd 2412 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
4633a7c4 2413 (font-lock-require-faces
2414 (list
2415 ;; Color-light Color-dark Gray-light Gray-dark Mono
2416 (list 'font-lock-comment-face
2417 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
2418 nil
2419 [nil nil t t t]
2420 [nil nil t t t]
2421 nil)
2422 (list 'font-lock-string-face
2423 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
2424 nil
2425 nil
2426 [nil nil t t t]
2427 nil)
2428 (list 'font-lock-keyword-face
2429 ["Purple" "LightSteelBlue" "DimGray" "Gray90"]
2430 nil
2431 [nil nil t t t]
2432 nil
2433 nil)
2434 (list 'font-lock-function-name-face
2435 (vector
2436 "Blue" "LightSkyBlue" "Gray50" "LightGray"
2437 (cdr (assq 'background-color ; if mono
2438 (frame-parameters))))
2439 (vector
2440 nil nil nil nil
2441 (cdr (assq 'foreground-color ; if mono
2442 (frame-parameters))))
2443 [nil nil t t t]
2444 nil
2445 nil)
2446 (list 'font-lock-variable-name-face
2447 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
2448 nil
2449 [nil nil t t t]
2450 [nil nil t t t]
2451 nil)
2452 (list 'font-lock-type-face
2453 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
2454 nil
2455 [nil nil t t t]
2456 nil
2457 [nil nil t t t]
2458 )
2459 (list 'font-lock-reference-face
2460 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
2461 nil
2462 [nil nil t t t]
2463 nil
2464 [nil nil t t t]
2465 )
2466 (list 'font-lock-other-type-face
2467 ["chartreuse3" ("orchid1" "orange")
2468 nil "Gray80"]
2469 [nil nil "gray90"]
2470 [nil nil nil t t]
2471 [nil nil t t]
2472 [nil nil t t t]
2473 )
2474 (list 'font-lock-emphasized-face
2475 ["blue" "yellow" nil "Gray80"]
2476 ["lightyellow2" ("navy" "os2blue" "darkgreen")
2477 "gray90"]
2478 t
2479 nil
2480 nil)
2481 (list 'font-lock-other-emphasized-face
2482 ["red" "red" nil "Gray80"]
2483 ["lightyellow2" ("navy" "os2blue" "darkgreen")
2484 "gray90"]
2485 t
2486 t
2487 nil)))
2488 (defvar cperl-guessed-background nil
2489 "Display characteristics as guessed by cperl.")
2490 (or (fboundp 'x-color-defined-p)
2491 (defalias 'x-color-defined-p
2492 (cond ((fboundp 'color-defined-p) 'color-defined-p)
2493 ;; XEmacs >= 19.12
2494 ((fboundp 'valid-color-name-p) 'valid-color-name-p)
2495 ;; XEmacs 19.11
2496 (t 'x-valid-color-name-p))))
2497 (defvar font-lock-reference-face 'font-lock-reference-face)
2498 (defvar font-lock-variable-name-face 'font-lock-variable-name-face)
2499 (or (boundp 'font-lock-type-face)
2500 (defconst font-lock-type-face
2501 'font-lock-type-face
2502 "Face to use for data types.")
2503 )
2504 (or (boundp 'font-lock-other-type-face)
2505 (defconst font-lock-other-type-face
2506 'font-lock-other-type-face
2507 "Face to use for data types from another group.")
2508 )
2509 (if (not (cperl-xemacs-p)) nil
2510 (or (boundp 'font-lock-comment-face)
2511 (defconst font-lock-comment-face
2512 'font-lock-comment-face
2513 "Face to use for comments.")
2514 )
2515 (or (boundp 'font-lock-keyword-face)
2516 (defconst font-lock-keyword-face
2517 'font-lock-keyword-face
2518 "Face to use for keywords.")
2519 )
2520 (or (boundp 'font-lock-function-name-face)
2521 (defconst font-lock-function-name-face
2522 'font-lock-function-name-face
2523 "Face to use for function names.")
2524 )
2525 )
2526 ;;(if (featurep 'font-lock)
2527 (if (face-equal font-lock-type-face font-lock-comment-face)
2528 (defconst font-lock-type-face
2529 'font-lock-type-face
2530 "Face to use for basic data types.")
2531 )
2532;;; (if (fboundp 'eval-after-load)
2533;;; (eval-after-load "font-lock"
2534;;; '(if (face-equal font-lock-type-face
2535;;; font-lock-comment-face)
2536;;; (defconst font-lock-type-face
2537;;; 'font-lock-type-face
2538;;; "Face to use for basic data types.")
2539;;; ))) ; This does not work :-( Why?!
2540;;; ; Workaround: added to font-lock-m-h
2541;;; )
2542 (or (boundp 'font-lock-other-emphasized-face)
2543 (defconst font-lock-other-emphasized-face
2544 'font-lock-other-emphasized-face
2545 "Face to use for another type of emphasizing.")
2546 )
2547 (or (boundp 'font-lock-emphasized-face)
2548 (defconst font-lock-emphasized-face
2549 'font-lock-emphasized-face
2550 "Face to use for emphasizing.")
2551 )
2552 ;; Here we try to guess background
2553 (let ((background
2554 (if (boundp 'font-lock-background-mode)
2555 font-lock-background-mode
2556 'light))
2557 (face-list (and (fboundp 'face-list) (face-list)))
2558 is-face)
2559 (fset 'is-face
2560 (cond ((fboundp 'find-face)
2561 (symbol-function 'find-face))
2562 (face-list
2563 (function (lambda (face) (member face face-list))))
2564 (t
2565 (function (lambda (face) (boundp face))))))
2566 (defvar cperl-guessed-background
2567 (if (and (boundp 'font-lock-display-type)
2568 (eq font-lock-display-type 'grayscale))
2569 'gray
2570 background)
2571 "Background as guessed by CPerl mode")
2572 (if (is-face 'font-lock-type-face) nil
2573 (copy-face 'default 'font-lock-type-face)
2574 (cond
2575 ((eq background 'light)
2576 (set-face-foreground 'font-lock-type-face
2577 (if (x-color-defined-p "seagreen")
2578 "seagreen"
2579 "sea green")))
2580 ((eq background 'dark)
2581 (set-face-foreground 'font-lock-type-face
2582 (if (x-color-defined-p "os2pink")
2583 "os2pink"
2584 "pink")))
2585 (t
2586 (set-face-background 'font-lock-type-face "gray90"))))
2587 (if (is-face 'font-lock-other-type-face)
2588 nil
2589 (copy-face 'font-lock-type-face 'font-lock-other-type-face)
2590 (cond
2591 ((eq background 'light)
2592 (set-face-foreground 'font-lock-other-type-face
2593 (if (x-color-defined-p "chartreuse3")
2594 "chartreuse3"
2595 "chartreuse")))
2596 ((eq background 'dark)
2597 (set-face-foreground 'font-lock-other-type-face
2598 (if (x-color-defined-p "orchid1")
2599 "orchid1"
2600 "orange")))))
2601 (if (is-face 'font-lock-other-emphasized-face) nil
2602 (copy-face 'bold-italic 'font-lock-other-emphasized-face)
2603 (cond
2604 ((eq background 'light)
2605 (set-face-background 'font-lock-other-emphasized-face
2606 (if (x-color-defined-p "lightyellow2")
2607 "lightyellow2"
2608 (if (x-color-defined-p "lightyellow")
2609 "lightyellow"
2610 "light yellow"))))
2611 ((eq background 'dark)
2612 (set-face-background 'font-lock-other-emphasized-face
2613 (if (x-color-defined-p "navy")
2614 "navy"
2615 (if (x-color-defined-p "darkgreen")
2616 "darkgreen"
2617 "dark green"))))
2618 (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
2619 (if (is-face 'font-lock-emphasized-face) nil
2620 (copy-face 'bold 'font-lock-emphasized-face)
2621 (cond
2622 ((eq background 'light)
2623 (set-face-background 'font-lock-emphasized-face
2624 (if (x-color-defined-p "lightyellow2")
2625 "lightyellow2"
2626 "lightyellow")))
2627 ((eq background 'dark)
2628 (set-face-background 'font-lock-emphasized-face
2629 (if (x-color-defined-p "navy")
2630 "navy"
2631 (if (x-color-defined-p "darkgreen")
2632 "darkgreen"
2633 "dark green"))))
2634 (t (set-face-background 'font-lock-emphasized-face "gray90"))))
2635 (if (is-face 'font-lock-variable-name-face) nil
2636 (copy-face 'italic 'font-lock-variable-name-face))
2637 (if (is-face 'font-lock-reference-face) nil
c07a80fd 2638 (copy-face 'italic 'font-lock-reference-face))))
2639 (setq cperl-faces-init t))
4633a7c4 2640 (error nil)))
2641
2642
2643(defun cperl-ps-print-init ()
2644 "Initialization of `ps-print' components for faces used in CPerl."
2645 ;; Guard against old versions
2646 (defvar ps-underlined-faces nil)
2647 (defvar ps-bold-faces nil)
2648 (defvar ps-italic-faces nil)
2649 (setq ps-bold-faces
2650 (append '(font-lock-emphasized-face
2651 font-lock-keyword-face
2652 font-lock-variable-name-face
2653 font-lock-reference-face
2654 font-lock-other-emphasized-face)
2655 ps-bold-faces))
2656 (setq ps-italic-faces
2657 (append '(font-lock-other-type-face
2658 font-lock-reference-face
2659 font-lock-other-emphasized-face)
2660 ps-italic-faces))
2661 (setq ps-underlined-faces
2662 (append '(font-lock-emphasized-face
2663 font-lock-other-emphasized-face
2664 font-lock-other-type-face font-lock-type-face)
2665 ps-underlined-faces))
2666 (cons 'font-lock-type-face ps-underlined-faces))
2667
2668
2669(if (cperl-enable-font-lock) (cperl-windowed-init))
2670
2671(defun cperl-set-style (style)
2672 "Set CPerl-mode variables to use one of several different indentation styles.
2673The arguments are a string representing the desired style.
2674Available styles are GNU, K&R, BSD and Whitesmith."
2675 (interactive
2676 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
2677 c-style-alist)))
2678 (list (completing-read "Enter style: " list nil 'insist))))
2679 (let ((style (cdr (assoc style c-style-alist))) setting str sym)
2680 (while style
2681 (setq setting (car style) style (cdr style))
2682 (setq str (symbol-name (car setting)))
2683 (and (string-match "^c-" str)
2684 (setq str (concat "cperl-" (substring str 2)))
2685 (setq sym (intern-soft str))
2686 (boundp sym)
2687 (set sym (cdr setting))))))
2688
2689(defun cperl-check-syntax ()
2690 (interactive)
2691 (require 'mode-compile)
2692 (let ((perl-dbg-flags "-wc"))
2693 (mode-compile)))
2694
2695(defun cperl-info-buffer ()
2696 ;; Returns buffer with documentation. Creats if missing
2697 (let ((info (get-buffer "*info-perl*")))
2698 (if info info
2699 (save-window-excursion
2700 ;; Get Info running
2701 (require 'info)
2702 (save-window-excursion
2703 (info))
2704 (Info-find-node "perl5" "perlfunc")
2705 (set-buffer "*info*")
2706 (rename-buffer "*info-perl*")
2707 (current-buffer)))))
2708
2709(defun cperl-word-at-point (&optional p)
2710 ;; Returns the word at point or at P.
2711 (save-excursion
2712 (if p (goto-char p))
2713 (require 'etags)
2714 (funcall (or (and (boundp 'find-tag-default-function)
2715 find-tag-default-function)
2716 (get major-mode 'find-tag-default-function)
2717 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
2718 ;; automatically used within `find-tag-default':
2719 'find-tag-default))))
2720
2721(defun cperl-info-on-command (command)
2722 "Shows documentation for Perl command in other window."
2723 (interactive
2724 (let* ((default (cperl-word-at-point))
2725 (read (read-string
2726 (format "Find doc for Perl function (default %s): "
2727 default))))
2728 (list (if (equal read "")
2729 default
2730 read))))
2731
2732 (let ((buffer (current-buffer))
c07a80fd 2733 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
4633a7c4 2734 pos)
2735 (if (string-match "^-[a-zA-Z]$" command)
2736 (setq cmd-desc "^-X[ \t\n]"))
2737 (set-buffer (cperl-info-buffer))
2738 (beginning-of-buffer)
2739 (re-search-forward "^-X[ \t\n]")
2740 (forward-line -1)
2741 (if (re-search-forward cmd-desc nil t)
2742 (progn
2743 (setq pos (progn (beginning-of-line)
2744 (point)))
2745 (pop-to-buffer (cperl-info-buffer))
2746 (set-window-start (selected-window) pos))
2747 (message "No entry for %s found." command))
c07a80fd 2748 (pop-to-buffer buffer)))
4633a7c4 2749
2750(defun cperl-info-on-current-command ()
2751 "Shows documentation for Perl command at point in other window."
2752 (interactive)
2753 (cperl-info-on-command (cperl-word-at-point)))
2754
2755(defun cperl-imenu-info-imenu-search ()
2756 (if (looking-at "^-X[ \t\n]") nil
2757 (re-search-backward
2758 "^\n\\([-a-zA-Z]+\\)[ \t\n]")
2759 (forward-line 1)))
2760
2761(defun cperl-imenu-info-imenu-name ()
2762 (buffer-substring
2763 (match-beginning 1) (match-end 1)))
2764
2765(defun cperl-imenu-on-info ()
2766 (interactive)
2767 (let* ((buffer (current-buffer))
2768 imenu-create-index-function
2769 imenu-prev-index-position-function
2770 imenu-extract-index-name-function
2771 (index-item (save-restriction
2772 (save-window-excursion
2773 (set-buffer (cperl-info-buffer))
2774 (setq imenu-create-index-function
2775 'imenu-default-create-index-function
2776 imenu-prev-index-position-function
2777 'cperl-imenu-info-imenu-search
2778 imenu-extract-index-name-function
2779 'cperl-imenu-info-imenu-name)
2780 (imenu-choose-buffer-index)))))
2781 (and index-item
2782 (progn
2783 (push-mark)
2784 (pop-to-buffer "*info-perl*")
2785 (cond
2786 ((markerp (cdr index-item))
2787 (goto-char (marker-position (cdr index-item))))
2788 (t
2789 (goto-char (cdr index-item))))
2790 (set-window-start (selected-window) (point))
2791 (pop-to-buffer buffer)))))
2792
2793(defun cperl-lineup (beg end &optional step minshift)
2794 "Lineup construction in a region.
2795Beginning of region should be at the start of a construction.
2796All first occurences of this construction in the lines that are
2797partially contained in the region are lined up at the same column.
2798
2799MINSHIFT is the minimal amount of space to insert before the construction.
2800STEP is the tabwidth to position constructions.
2801If STEP is `nil', `cperl-lineup-step' will be used
2802\(or `cperl-indent-level', if `cperl-lineup-step' is `nil').
2803Will not move the position at the start to the left."
2804 (interactive "r")
2805 (let (search col tcol seen b e)
2806 (save-excursion
2807 (goto-char end)
2808 (end-of-line)
2809 (setq end (point-marker))
2810 (goto-char beg)
2811 (skip-chars-forward " \t\f")
2812 (setq beg (point-marker))
2813 (indent-region beg end nil)
2814 (goto-char beg)
2815 (setq col (current-column))
2816 (if (looking-at "\\sw")
2817 (if (looking-at "\\<\\sw+\\>")
2818 (setq search
2819 (concat "\\<"
2820 (regexp-quote
2821 (buffer-substring (match-beginning 0)
2822 (match-end 0))) "\\>"))
2823 (error "Cannot line up in a middle of the word"))
2824 (if (looking-at "$")
2825 (error "Cannot line up end of line"))
2826 (setq search (regexp-quote (char-to-string (following-char)))))
2827 (setq step (or step cperl-lineup-step cperl-indent-level))
2828 (or minshift (setq minshift 1))
2829 (while (progn
2830 (beginning-of-line 2)
2831 (and (< (point) end)
2832 (re-search-forward search end t)
2833 (goto-char (match-beginning 0))))
2834 (setq tcol (current-column) seen t)
2835 (if (> tcol col) (setq col tcol)))
2836 (or seen
2837 (error "The construction to line up occured only once"))
2838 (goto-char beg)
2839 (setq col (+ col minshift))
2840 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
2841 (while
2842 (progn
2843 (setq e (point))
2844 (skip-chars-backward " \t")
2845 (delete-region (point) e)
2846 (indent-to-column col); (make-string (- col (current-column)) ?\ ))
2847 (beginning-of-line 2)
2848 (and (< (point) end)
2849 (re-search-forward search end t)
2850 (goto-char (match-beginning 0)))))))) ; No body
2851
2852(defun cperl-etags (&optional add all files)
2853 "Run etags with appropriate options for Perl files.
2854If optional argument ALL is `recursive', will process Perl files
2855in subdirectories too."
2856 (interactive)
2857 (let ((cmd "etags")
2858 (args '("-l" "none" "-r" "/\\<\\(package\\|sub\\)[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)[ \\t]*\\([{#]\\|$\\)\\)/\\4/"))
2859 res)
2860 (if add (setq args (cons "-a" args)))
2861 (or files (setq files (list buffer-file-name)))
2862 (cond
2863 ((eq all 'recursive)
2864 ;;(error "Not implemented: recursive")
2865 (setq args (append (list "-e"
2866 "sub wanted {push @ARGV, $File::Find::name if /\\.[Pp][Llm]$/}
2867 use File::Find;
2868 find(\\&wanted, '.');
2869 exec @ARGV;"
2870 cmd) args)
2871 cmd "perl"))
2872 (all
2873 ;;(error "Not implemented: all")
2874 (setq args (append (list "-e"
2875 "push @ARGV, <*.PL *.pl *.pm>;
2876 exec @ARGV;"
2877 cmd) args)
2878 cmd "perl"))
2879 (t
2880 (setq args (append args files))))
2881 (setq res (apply 'call-process cmd nil nil nil args))
2882 (or (eq res 0)
2883 (message "etags returned \"%s\"" res))))