1 ;;; This code started from the following message of long time ago (IZ):
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
8 ;; Perl code editing commands for Emacs
9 ;; Copyright (C) 1985-1996 Bob Olson, Ilya Zakharevich
11 ;; This file is not (yet) part of GNU Emacs. It may be distributed
12 ;; either under the same terms as GNU Emacs, or under the same terms
13 ;; as Perl. You should have recieved a copy of Perl Artistic license
14 ;; along with the Perl distribution.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;;; Corrections made by Ilya Zakharevich ilya@math.mps.ohio-state.edu
31 ;;; XEmacs changes by Peter Arius arius@informatik.uni-erlangen.de
33 ;; $Id: cperl-mode.el,v 1.25 1996/09/06 09:51:41 ilya Exp ilya $
35 ;;; To use this mode put the following into your .emacs file:
37 ;; (autoload 'perl-mode "cperl-mode" "alternate mode for editing Perl programs" t)
39 ;;; You can either fine-tune the bells and whistles of this mode or
40 ;;; bulk enable them by putting
42 ;; (setq cperl-hairy t)
44 ;;; in your .emacs file. (Emacs rulers do not consider it politically
45 ;;; correct to make whistles enabled by default.)
47 ;;; Additional useful commands to put into your .emacs file:
49 ;; (setq auto-mode-alist
50 ;; (append '(("\\.[pP][Llm]$" . perl-mode)) auto-mode-alist ))
51 ;; (setq interpreter-mode-alist (append interpreter-mode-alist
52 ;; '(("miniperl" . perl-mode))))
54 ;;; The mode information (on C-h m) provides customization help.
55 ;;; If you use font-lock feature of this mode, it is advisable to use
56 ;;; eather lazy-lock-mode or fast-lock-mode (available on ELisp
57 ;;; archive in files lazy-lock.el and fast-lock.el). I prefer lazy-lock.
59 ;;; Faces used now: three faces for first-class and second-class keywords
60 ;;; and control flow words, one for each: comments, string, labels,
61 ;;; functions definitions and packages, arrays, hashes, and variable
62 ;;; definitions. If you do not see all these faces, your font-lock does
63 ;;; not define them, so you need to define them manually. Maybe you have
64 ;;; an obsolete font-lock from 19.28 or earlier. Upgrade.
66 ;;; If you have grayscale monitor, and do not have the variable
67 ;;; font-lock-display-type bound to 'grayscale, insert
69 ;;; (setq font-lock-display-type 'grayscale)
71 ;;; to your .emacs file.
73 ;;;; This mode supports font-lock, imenu and mode-compile. In the
74 ;;;; hairy version font-lock is on, but you should activate imenu
75 ;;;; yourself (note that mode-compile is not standard yet). Well, you
76 ;;;; can use imenu from keyboard anyway (M-x imenu), but it is better
77 ;;;; to bind it like that:
79 ;; (define-key global-map [M-S-down-mouse-3] 'imenu)
81 ;;; In fact the version of font-lock that this version supports can be
82 ;;; much newer than the version you actually have. This means that a
83 ;;; lot of faces can be set up, but are not visible on your screen
84 ;;; since the coloring rules for this faces are not defined.
86 ;;; Updates: ========================================
88 ;;; Made less hairy by default: parentheses not electric,
89 ;;; linefeed not magic. Bug with abbrev-mode corrected.
92 ;;; Better indentation:
93 ;;; subs inside braces should work now,
94 ;;; Toplevel braces obey customization.
95 ;;; indent-for-comment knows about bad cases, cperl-indent-for-comment
96 ;;; moves cursor to a correct place.
97 ;;; cperl-indent-exp written from the scratch! Slow... (quadratic!) :-(
98 ;;; (50 secs on DB::DB (sub of 430 lines), 486/66)
99 ;;; Minor documentation fixes.
100 ;;; Imenu understands packages as prefixes (including nested).
101 ;;; Hairy options can be switched off one-by-one by setting to null.
102 ;;; Names of functions and variables changed to conform to `cperl-' style.
105 ;;; Some bugs with indentation of labels (and embedded subs) corrected.
106 ;;; `cperl-indent-region' done (slow :-()).
107 ;;; `cperl-fill-paragraph' done.
108 ;;; Better package support for `imenu'.
109 ;;; Progress indicator for indentation (with `imenu' loaded).
110 ;;; `Cperl-set' was busted, now setting the individual hairy option
111 ;;; should be better.
114 ;;; `cperl-set-style' done.
115 ;;; `cperl-check-syntax' done.
117 ;;; New config variables `cperl-close-paren-offset' and `cperl-comment-column'.
118 ;;; Bugs with `cperl-auto-newline' corrected.
119 ;;; `cperl-electric-lbrace' can work with `cperl-auto-newline' in situation
122 ;;;; 1.7 XEmacs (arius@informatik.uni-erlangen.de):
123 ;;; - use `next-command-event', if `next-command-events' does not exist
124 ;;; - use `find-face' as def. of `is-face'
125 ;;; - corrected def. of `x-color-defined-p'
126 ;;; - added const defs for font-lock-comment-face,
127 ;;; font-lock-keyword-face and font-lock-function-name-face
128 ;;; - added def. of font-lock-variable-name-face
129 ;;; - added (require 'easymenu) inside an `eval-when-compile'
130 ;;; - replaced 4-argument `substitute-key-definition' with ordinary
132 ;;; - replaced `mark-active' in menu definition by `cperl-use-region-p'.
134 ;;; - use emacs-vers.el (http://www.cs.utah.edu/~eeide/emacs/emacs-vers.el.gz)
135 ;;; for portable code?
136 ;;; - should `cperl-mode' do a
137 ;;; (if (featurep 'easymenu) (easy-menu-add cperl-menu))
138 ;;; or should this be left to the user's `cperl-mode-hook'?
140 ;;; Some bugs introduced by the above fix corrected (IZ ;-).
141 ;;; Some bugs under XEmacs introduced by the correction corrected.
143 ;;; Some more can remain since there are two many different variants.
146 ;;; We do not support fontification of arrays and hashes under
147 ;;; obsolete font-lock any more. Upgrade.
149 ;;;; after 1.8 Minor bug with parentheses.
150 ;;;; after 1.9 Improvements from Joe Marzot.
152 ;;; Does not need easymenu to compile under XEmacs.
153 ;;; `vc-insert-headers' should work better.
154 ;;; Should work with 19.29 and 19.12.
155 ;;; Small improvements to fontification.
156 ;;; Expansion of keywords does not depend on C-? being backspace.
159 ;;; 19.29 and 19.12 supported.
160 ;;; `cperl-font-lock-enhanced' deprecated. Use font-lock-extra.el.
161 ;;; Support for font-lock-extra.el.
165 ;;; Support for perl5-info.
166 ;;; `imenu-go-find-at-position' in Tools requires imenu-go.el (see hints above)
167 ;;; Imenu entries do not work with stock imenu.el. Patch sent to maintainers.
168 ;;; Fontifies `require a if b;', __DATA__.
169 ;;; Arglist for auto-fill-mode was incorrect.
172 ;;; `cperl-lineup-step' and `cperl-lineup' added: lineup constructions
174 ;;; `cperl-do-auto-fill' updated for 19.29 style.
175 ;;; `cperl-info-on-command' now has a default.
176 ;;; Workaround for broken C-h on XEmacs.
177 ;;; VC strings escaped.
178 ;;; C-h f now may prompt for function name instead of going on,
179 ;;; controlled by `cperl-info-on-command-no-prompt'.
182 ;;; Msb buffer list includes perl files
183 ;;; Indent-for-comment uses indent-to
184 ;;; Can write tag files using etags.
187 ;;; Recognizes (tries to ;-) {...} which are not blocks during indentation.
188 ;;; `cperl-close-paren-offset' affects ?\] too (and ?\} if not block)
189 ;;; Bug with auto-filling comments started with "##" corrected.
191 ;;;; Very slow now: on DB::DB 0.91, 486/66:
193 ;;;Function Name Call Count Elapsed Time Average Time
194 ;;;======================================== ========== ============ ============
195 ;;;cperl-block-p 469 3.7799999999 0.0080597014
196 ;;;cperl-get-state 505 163.39000000 0.3235445544
197 ;;;cperl-comment-indent 12 0.0299999999 0.0024999999
198 ;;;cperl-backward-to-noncomment 939 4.4599999999 0.0047497337
199 ;;;cperl-calculate-indent 505 172.22000000 0.3410297029
200 ;;;cperl-indent-line 505 172.88000000 0.3423366336
201 ;;;cperl-use-region-p 40 0.0299999999 0.0007499999
202 ;;;cperl-indent-exp 1 177.97000000 177.97000000
203 ;;;cperl-to-comment-or-eol 1453 3.9800000000 0.0027391603
204 ;;;cperl-backward-to-start-of-continued-exp 9 0.0300000000 0.0033333333
205 ;;;cperl-indent-region 1 177.94000000 177.94000000
208 ;;; Takes into account white space after opening parentheses during indent.
209 ;;; May highlight pods and here-documents: see `cperl-pod-here-scan',
210 ;;; `cperl-pod-here-fontify', `cperl-pod-face'. Does not use this info
211 ;;; for indentation so far.
212 ;;; Fontification updated to 19.30 style.
213 ;;; The change 19.29->30 did not add all the required functionality,
214 ;;; but broke "font-lock-extra.el". Get "choose-color.el" from
215 ;;; ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs
219 ;;; recognized as a start of a block.
220 ;;; Two different font-lock-levels provided.
221 ;;; `cperl-pod-head-face' introduced. Used for highlighting.
222 ;;; `imenu' marks pods, +Packages moved to the head.
225 ;;; Scan for pods highlights here-docs too.
226 ;;; Note that the tag of here-doc may be rehighlighted later by lazy-lock.
227 ;;; Only one here-doc-tag per line is supported, and one in comment
228 ;;; or a string may break fontification.
229 ;;; POD headers were supposed to fill one line only.
232 ;;; `font-lock-keywords' were set in 19.30 style _always_. Current scheme
233 ;;; may break under XEmacs.
234 ;;; `cperl-calculate-indent' dis suppose that `parse-start' was defined.
235 ;;; `fontified' tag is added to fontified text as well as `lazy-lock' (for
236 ;;; compatibility with older lazy-lock.el) (older one overfontifies
237 ;;; something nevertheless :-().
238 ;;; Will not indent something inside pod and here-documents.
239 ;;; Fontifies the package name after import/no/bootstrap.
240 ;;; Added new entry to menu with meta-info about the mode.
243 ;;; Prefontification works much better with 19.29. Should be checked
244 ;;; with 19.30 as well.
245 ;;; Some misprints in docs corrected.
246 ;;; Now $a{-text} and -text => "blah" are fontified as strings too.
247 ;;; Now the pod search is much stricter, so it can help you to find
248 ;;; pod sections which are broken because of whitespace before =blah
249 ;;; - just observe the fontification.
252 ;;; Anonymous subs are indented with respect to the level of
253 ;;; indentation of `sub' now.
254 ;;; {} is recognized as hash after `bless' and `return'.
255 ;;; Anonymous subs are split by `cperl-linefeed' as well.
256 ;;; Electric parens embrace a region if present.
257 ;;; To make `cperl-auto-newline' useful,
258 ;;; `cperl-auto-newline-after-colon' is introduced.
259 ;;; `cperl-electric-parens' is now t or nul. The old meaning is moved to
260 ;;; `cperl-electric-parens-string'.
261 ;;; `cperl-toggle-auto-newline' introduced, put on C-c C-a.
262 ;;; `cperl-toggle-abbrev' introduced, put on C-c C-k.
263 ;;; `cperl-toggle-electric' introduced, put on C-c C-e.
264 ;;; Beginning-of-defun-regexp was not anchored.
267 ;;; Auto-newline grants `cperl-extra-newline-before-brace' if "{" is typed
269 ;;; {} is recognized as expression after `tr' and friends.
272 ;;; Entry Hierarchy added to imenu. Very primitive so far.
273 ;;; One needs newer `imenu-go'.el. A patch to `imenu' is needed as well.
274 ;;; Writes its own TAGS files.
275 ;;; Class viewer based on TAGS files. Does not trace @ISA so far.
276 ;;; 19.31: Problems with scan for PODs corrected.
277 ;;; First POD header correctly fontified.
278 ;;; I needed (setq imenu-use-keymap-menu t) to get good imenu in 19.31.
279 ;;; Apparently it makes a lot of hierarchy code obsolete...
282 ;;; Tags filler now scans *.xs as well.
283 ;;; The info from *.xs scan is used by the hierarchy viewer.
284 ;;; Hierarchy viewer documented.
285 ;;; Bug in 19.31 imenu documented.
288 ;;; New location for info-files mentioned,
289 ;;; Electric-; should work better.
290 ;;; Minor bugs with POD marking.
293 ;;; `cperl-info-page' introduced.
294 ;;; To make `uncomment-region' working, `comment-region' would
295 ;;; not insert extra space.
296 ;;; Here documents delimiters better recognized
297 ;;; (empty one, and non-alphanums in quotes handled). May be wrong with 1<<14?
298 ;;; `cperl-db' added, used in menu.
299 ;;; imenu scan removes text-properties, for better debugging
300 ;;; - but the bug is in 19.31 imenu.
301 ;;; formats highlighted by font-lock and prescan, embedded comments
303 ;;; POD/friends scan merged in one pass.
304 ;;; Syntax class is not used for analyzing the code, only char-syntax
305 ;;; may be cecked against _ or'ed with w.
306 ;;; Syntax class of `:' changed to be _.
307 ;;; `cperl-find-bad-style' added.
309 (defvar cperl-extra-newline-before-brace nil
310 "*Non-nil means that if, elsif, while, until, else, for, foreach
311 and do constructs look like:
323 (defvar cperl-indent-level 2
324 "*Indentation of CPerl statements with respect to containing block.")
325 (defvar cperl-lineup-step nil
326 "*`cperl-lineup' will always lineup at multiple of this number.
327 If `nil', the value of `cperl-indent-level' will be used.")
328 (defvar cperl-brace-imaginary-offset 0
329 "*Imagined indentation of a Perl open brace that actually follows a statement.
330 An open brace following other text is treated as if it were this far
331 to the right of the start of its line.")
332 (defvar cperl-brace-offset 0
333 "*Extra indentation for braces, compared with other text in same context.")
334 (defvar cperl-label-offset -2
335 "*Offset of CPerl label lines relative to usual indentation.")
336 (defvar cperl-min-label-indent 1
337 "*Minimal offset of CPerl label lines.")
338 (defvar cperl-continued-statement-offset 2
339 "*Extra indent for lines not starting new statements.")
340 (defvar cperl-continued-brace-offset 0
341 "*Extra indent for substatements that start with open-braces.
342 This is in addition to cperl-continued-statement-offset.")
343 (defvar cperl-close-paren-offset -1
344 "*Extra indent for substatements that start with close-parenthesis.")
346 (defvar cperl-auto-newline nil
347 "*Non-nil means automatically newline before and after braces,
348 and after colons and semicolons, inserted in CPerl code. The following
349 \\[cperl-electric-backspace] will remove the inserted whitespace.
350 Insertion after colons requires both this variable and
351 `cperl-auto-newline-after-colon' set.")
353 (defvar cperl-auto-newline-after-colon nil
354 "*Non-nil means automatically newline even after colons.
355 Subject to `cperl-auto-newline' setting.")
357 (defvar cperl-tab-always-indent t
358 "*Non-nil means TAB in CPerl mode should always reindent the current line,
359 regardless of where in the line point is when the TAB command is used.")
361 (defvar cperl-font-lock nil
362 "*Non-nil (and non-null) means CPerl buffers will use font-lock-mode.
363 Can be overwritten by `cperl-hairy' if nil.")
365 (defvar cperl-electric-lbrace-space nil
366 "*Non-nil (and non-null) means { after $ in CPerl buffers should be preceeded by ` '.
367 Can be overwritten by `cperl-hairy' if nil.")
369 (defvar cperl-electric-parens-string "({[<"
370 "*String of parentheses that should be electric in CPerl.")
372 (defvar cperl-electric-parens nil
373 "*Non-nil (and non-null) means parentheses should be electric in CPerl.
374 Can be overwritten by `cperl-hairy' if nil.")
375 (defvar cperl-electric-parens-mark
377 (or (and (boundp 'transient-mark-mode) ; For Emacs
379 (and (boundp 'zmacs-regions) ; For XEmacs
381 "*Not-nil means that electric parens look for active mark.
382 Default is yes if there is visual feedback on mark.")
384 (defvar cperl-electric-parens-mark (and window-system transient-mark-mode)
385 "*Not-nil means that electric parens look for active mark.
386 Default is yes if there is visual feedback on mark.")
388 (defvar cperl-electric-linefeed nil
389 "*If true, LFD should be hairy in CPerl, otherwise C-c LFD is hairy.
390 In any case these two mean plain and hairy linefeeds together.
391 Can be overwritten by `cperl-hairy' if nil.")
393 (defvar cperl-electric-keywords nil
394 "*Not-nil (and non-null) means keywords are electric in CPerl.
395 Can be overwritten by `cperl-hairy' if nil.")
397 (defvar cperl-hairy nil
398 "*Not-nil means all the bells and whistles are enabled in CPerl.")
400 (defvar cperl-comment-column 32
401 "*Column to put comments in CPerl (use \\[cperl-indent]' to lineup with code).")
403 (defvar cperl-vc-header-alist '((SCCS "$sccs = '%W\%' ;")
404 (RCS "$rcs = ' $Id\$ ' ;"))
405 "*What to use as `vc-header-alist' in CPerl.")
407 (defvar cperl-info-on-command-no-prompt nil
408 "*Not-nil (and non-null) means not to prompt on C-h f.
409 The opposite behaviour is always available if prefixed with C-c.
410 Can be overwritten by `cperl-hairy' if nil.")
412 (defvar cperl-pod-face 'font-lock-comment-face
413 "*The result of evaluation of this expression is used for pod highlighting.")
415 (defvar cperl-pod-head-face 'font-lock-variable-name-face
416 "*The result of evaluation of this expression is used for pod highlighting.
417 Font for POD headers.")
419 (defvar cperl-here-face 'font-lock-string-face
420 "*The result of evaluation of this expression is used for here-docs highlighting.")
422 (defvar cperl-pod-here-fontify '(featurep 'font-lock)
423 "*Not-nil after evaluation means to highlight pod and here-docs sections.")
425 (defvar cperl-pod-here-scan t
426 "*Not-nil means look for pod and here-docs sections during startup.
427 You can always make lookup from menu or using \\[cperl-find-pods-heres].")
429 (defvar cperl-imenu-addback nil
430 "*Not-nil means add backreferences to generated `imenu's.
431 May require patched `imenu' and `imenu-go'.")
433 (defvar cperl-info-page "perl"
434 "Name of the info page containging perl docs.
435 Older version of this page was called `perl5', newer `perl'.")
439 ;;; Short extra-docs.
441 (defvar cperl-tips 'please-ignore-this-line
442 "Get newest version of this package from
443 ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs
445 ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
447 Get support packages choose-color.el (or font-lock-extra.el before
448 19.30), imenu-go.el from the same place. \(Look for other files there
449 too... ;-) Get a patch for imenu.el in 19.29. Note that for 19.30 and
450 later you should use choose-color.el *instead* of font-lock-extra.el
451 \(and you will not get smart highlighting in C :-().
453 Note that to enable Compile choices in the menu you need to install
457 $CPAN/doc/manual/info/perl-info.tar.gz
459 http://www.metronet.com:70/9/perlinfo/perl5/manual/perl5-info.tar.gz
461 If you use imenu-go, run imenu on perl5-info buffer (you can do it
462 from CPerl menu). If many files are related, generate TAGS files from
463 Tools/Tags submenu in CPerl menu.
465 If some class structure is too complicated, use Tools/Hierarchy-view
466 from CPerl menu, or hierarchic view of imenu. The second one uses the
467 current buffer only, the first one requires generation of TAGS from
468 CPerl/Tools/Tags menu beforehand.
470 Run CPerl/Tools/Insert-spaces-if-needed to fix your lazy typing.
472 Before reporting (non-)problems look in the problem section on what I
475 (defvar cperl-problems 'please-ignore-this-line
476 "Emacs has a _very_ restricted syntax parsing engine.
478 It may be corrected on the level of C code, please look in the
479 `non-problems' section if you want to volunteer.
481 CPerl mode tries to corrects some Emacs misunderstandings, however,
482 for effeciency reasons the degree of correction is different for
483 different operations. The partially corrected problems are: POD
484 sections, here-documents, regexps. The operations are: highlighting,
485 indentation, electric keywords, electric braces.
487 This may be confusing, since the regexp s#//#/#\; may be highlighted
488 as a comment, but it will recognized as a regexp by the indentation
489 code. Or the opposite case, when a pod section is highlighted, but
490 breaks the indentation of the following code.
492 The main trick (to make $ a \"backslash\") makes constructions like
493 ${aaa} look like unbalanced braces. The only trick I can think out is
494 to insert it as $ {aaa} (legal in perl5, not in perl4).
496 Similar problems arise in regexps, when /(\\s|$)/ should be rewritten
497 as /($|\\s)/. Note that such a transpositinon is not always possible
500 (defvar cperl-non-problems 'please-ignore-this-line
501 "As you know from `problems' section, Perl syntax too hard for CPerl.
503 Most the time, if you write your own code, you may find an equivalent
504 \(and almost as readable) expression.
506 Try to help it: add comments with embedded quotes to fix CPerl
507 misunderstandings about the end of quotation:
511 You won't need it too often. The reason: $ \"quotes\" the following
512 character (this saves a life a lot of times in CPerl), thus due to
513 Emacs parsing rules it does not consider tick after the dollar as a
514 closing one, but as a usual character.
516 Now the indentation code is pretty wise. The only drawback is that it
517 relies on Emacs parsing to find matching parentheses. And Emacs
518 *cannot* match parentheses in Perl 100% correctly. So
520 will not break indentation, but
524 If you still get wrong indentation in situation that you think the
525 code should be able to parse, try:
527 a) Check what Emacs thinks about balance of your parentheses.
528 b) Supply the code to me (IZ).
530 Pods are treated _very_ rudimentally. Here-documents are not treated
531 at all (except highlighting and inhibiting indentation). (This may
532 change some time. RMS approved making syntax lookup recognize text
533 attributes, but volonteers are needed to change Emacs C code.)
535 To speed up coloring the following compromises exist:
536 a) sub in $mypackage::sub may be highlighted.
537 b) -z in [a-z] may be highlighted.
538 c) if your regexp contains a keyword (like \"s\"), it may be highlighted.
541 Imenu in 19.31 is broken. Set `imenu-use-keymap-menu' to t, and remove
542 `car' before `imenu-choose-buffer-index' in `imenu'.
547 ;;; Portability stuff:
549 (defsubst cperl-xemacs-p ()
550 (string-match "XEmacs\\|Lucid" emacs-version))
552 (defvar del-back-ch (car (append (where-is-internal 'delete-backward-char)
553 (where-is-internal 'backward-delete-char-untabify)))
554 "Character generated by key bound to delete-backward-char.")
556 (and (vectorp del-back-ch) (= (length del-back-ch) 1)
557 (setq del-back-ch (aref del-back-ch 0)))
561 ;; "Active regions" are on: use region only if active
562 ;; "Active regions" are off: use region unconditionally
563 (defun cperl-use-region-p ()
564 (if zmacs-regions (mark) t))
565 (defun cperl-mark-active () (mark)))
566 (defun cperl-use-region-p ()
567 (if transient-mark-mode mark-active t))
568 (defun cperl-mark-active () mark-active))
570 (defsubst cperl-enable-font-lock ()
571 (or (cperl-xemacs-p) window-system))
573 (if (boundp 'unread-command-events)
575 (defun cperl-putback-char (c) ; XEmacs >= 19.12
576 (setq unread-command-events (list (character-to-event c))))
577 (defun cperl-putback-char (c) ; Emacs 19
578 (setq unread-command-events (list c))))
579 (defun cperl-putback-char (c) ; XEmacs <= 19.11
580 (setq unread-command-event (character-to-event c))))
582 (or (fboundp 'uncomment-region)
583 (defun uncomment-region (beg end)
585 (comment-region beg end -1)))
587 (defvar cperl-do-not-fontify
588 (if (string< emacs-version "19.30")
591 "Text property which inhibits refontification.")
593 (defsubst cperl-put-do-not-fontify (from to)
594 (put-text-property (max (point-min) (1- from))
595 to cperl-do-not-fontify t))
598 ;;; Probably it is too late to set these guys already, but it can help later:
600 (setq auto-mode-alist
601 (append '(("\\.[pP][Llm]$" . perl-mode)) auto-mode-alist ))
602 (and (boundp 'interpreter-mode-alist)
603 (setq interpreter-mode-alist (append interpreter-mode-alist
604 '(("miniperl" . perl-mode)))))
605 (if (fboundp 'eval-when-compile)
613 ;; Calling `cperl-enable-font-lock' below doesn't compile on XEmacs,
614 ;; macros instead of defsubsts don't work on Emacs, so we do the
615 ;; expansion manually. Any other suggestions?
616 (if (or (string-match "XEmacs\\|Lucid" emacs-version)
618 (require 'font-lock))
622 (defvar cperl-mode-abbrev-table nil
623 "Abbrev table in use in Cperl-mode buffers.")
625 (add-hook 'edit-var-mode-alist '(perl-mode (regexp . "^cperl-")))
627 (defvar cperl-mode-map () "Keymap used in CPerl mode.")
629 (if cperl-mode-map nil
630 (setq cperl-mode-map (make-sparse-keymap))
631 (define-key cperl-mode-map "{" 'cperl-electric-lbrace)
632 (define-key cperl-mode-map "[" 'cperl-electric-paren)
633 (define-key cperl-mode-map "(" 'cperl-electric-paren)
634 (define-key cperl-mode-map "<" 'cperl-electric-paren)
635 (define-key cperl-mode-map "}" 'cperl-electric-brace)
636 (define-key cperl-mode-map ";" 'cperl-electric-semi)
637 (define-key cperl-mode-map ":" 'cperl-electric-terminator)
638 (define-key cperl-mode-map "\C-j" 'newline-and-indent)
639 (define-key cperl-mode-map "\C-c\C-j" 'cperl-linefeed)
640 (define-key cperl-mode-map "\C-c\C-a" 'cperl-toggle-auto-newline)
641 (define-key cperl-mode-map "\C-c\C-k" 'cperl-toggle-abbrev)
642 (define-key cperl-mode-map "\C-c\C-e" 'cperl-toggle-electric)
643 (define-key cperl-mode-map "\e\C-q" 'cperl-indent-exp) ; Usually not bound
644 ;;(define-key cperl-mode-map "\M-q" 'cperl-fill-paragraph)
645 ;;(define-key cperl-mode-map "\e;" 'cperl-indent-for-comment)
646 (define-key cperl-mode-map "\177" 'cperl-electric-backspace)
647 (define-key cperl-mode-map "\t" 'cperl-indent-command)
649 ;; don't clobber the backspace binding:
650 (define-key cperl-mode-map [(control h) f] 'cperl-info-on-command)
651 (define-key cperl-mode-map "\C-hf" 'cperl-info-on-command))
653 ;; don't clobber the backspace binding:
654 (define-key cperl-mode-map [(control c) (control h) f]
655 'cperl-info-on-current-command)
656 (define-key cperl-mode-map "\C-c\C-hf" 'cperl-info-on-current-command))
657 (if (and (cperl-xemacs-p)
658 (<= emacs-minor-version 11) (<= emacs-major-version 19))
660 ;; substitute-key-definition is usefulness-deenhanced...
661 (define-key cperl-mode-map "\M-q" 'cperl-fill-paragraph)
662 (define-key cperl-mode-map "\e;" 'cperl-indent-for-comment)
663 (define-key cperl-mode-map "\e\C-\\" 'cperl-indent-region))
664 (substitute-key-definition
665 'indent-sexp 'cperl-indent-exp
666 cperl-mode-map global-map)
667 (substitute-key-definition
668 'fill-paragraph 'cperl-fill-paragraph
669 cperl-mode-map global-map)
670 (substitute-key-definition
671 'indent-region 'cperl-indent-region
672 cperl-mode-map global-map)
673 (substitute-key-definition
674 'indent-for-comment 'cperl-indent-for-comment
675 cperl-mode-map global-map)))
680 (easy-menu-define cperl-menu cperl-mode-map "Menu for CPerl mode"
682 ["Beginning of function" beginning-of-defun t]
683 ["End of function" end-of-defun t]
684 ["Mark function" mark-defun t]
685 ["Indent expression" cperl-indent-exp t]
686 ["Fill paragraph/comment" cperl-fill-paragraph t]
687 ["Line up a construction" cperl-lineup (cperl-use-region-p)]
689 ["Indent region" cperl-indent-region (cperl-use-region-p)]
690 ["Comment region" cperl-comment-region (cperl-use-region-p)]
691 ["Uncomment region" cperl-uncomment-region (cperl-use-region-p)]
693 ["Run" mode-compile (fboundp 'mode-compile)]
694 ["Kill" mode-compile-kill (and (fboundp 'mode-compile-kill)
695 (get-buffer "*compilation*"))]
696 ["Next error" next-error (get-buffer "*compilation*")]
697 ["Check syntax" cperl-check-syntax (fboundp 'mode-compile)]
699 ["Debugger" cperl-db t]
702 ["Imenu" imenu (fboundp 'imenu)]
703 ["Insert spaces if needed" cperl-find-bad-style t]
704 ["Class Hierarchy from TAGS" cperl-tags-hier-init t]
705 ;;["Update classes" (cperl-tags-hier-init t) tags-table-list]
706 ["Imenu on info" cperl-imenu-on-info (featurep 'imenu)]
708 ;;; ["Create tags for current file" cperl-etags t]
709 ;;; ["Add tags for current file" (cperl-etags t) t]
710 ;;; ["Create tags for Perl files in directory" (cperl-etags nil t) t]
711 ;;; ["Add tags for Perl files in directory" (cperl-etags t t) t]
712 ;;; ["Create tags for Perl files in (sub)directories"
713 ;;; (cperl-etags nil 'recursive) t]
714 ;;; ["Add tags for Perl files in (sub)directories"
715 ;;; (cperl-etags t 'recursive) t])
716 ;;;; cperl-write-tags (&optional file erase recurse dir inbuffer)
717 ["Create tags for current file" (cperl-write-tags nil t) t]
718 ["Add tags for current file" (cperl-write-tags) t]
719 ["Create tags for Perl files in directory"
720 (cperl-write-tags nil t nil t) t]
721 ["Add tags for Perl files in directory"
722 (cperl-write-tags nil nil nil t) t]
723 ["Create tags for Perl files in (sub)directories"
724 (cperl-write-tags nil t t t) t]
725 ["Add tags for Perl files in (sub)directories"
726 (cperl-write-tags nil nil t t) t])
727 ["Recalculate PODs and HEREs" cperl-find-pods-heres t]
728 ["Define word at point" imenu-go-find-at-position
729 (fboundp 'imenu-go-find-at-position)]
730 ["Help on function" cperl-info-on-command t]
731 ["Help on function at point" cperl-info-on-current-command t])
733 ["Auto newline" cperl-toggle-auto-newline t]
734 ["Electric parens" cperl-toggle-electric t]
735 ["Electric keywords" cperl-toggle-abbrev t]
738 ["GNU" (cperl-set-style "GNU") t]
739 ["C++" (cperl-set-style "C++") t]
740 ["FSF" (cperl-set-style "FSF") t]
741 ["BSD" (cperl-set-style "BSD") t]
742 ["Whitesmith" (cperl-set-style "Whitesmith") t])
744 ["Tips" (describe-variable 'cperl-tips) t]
745 ["Problems" (describe-variable 'cperl-problems) t]
746 ["Non-problems" (describe-variable 'cperl-non-problems) t]))))
749 (autoload 'c-macro-expand "cmacexp"
750 "Display the result of expanding all C macros occurring in the region.
751 The expansion is entirely correct because it uses the C preprocessor."
754 (defvar cperl-mode-syntax-table nil
755 "Syntax table in use in Cperl-mode buffers.")
757 (if cperl-mode-syntax-table
759 (setq cperl-mode-syntax-table (make-syntax-table))
760 (modify-syntax-entry ?\\ "\\" cperl-mode-syntax-table)
761 (modify-syntax-entry ?/ "." cperl-mode-syntax-table)
762 (modify-syntax-entry ?* "." cperl-mode-syntax-table)
763 (modify-syntax-entry ?+ "." cperl-mode-syntax-table)
764 (modify-syntax-entry ?- "." cperl-mode-syntax-table)
765 (modify-syntax-entry ?= "." cperl-mode-syntax-table)
766 (modify-syntax-entry ?% "." cperl-mode-syntax-table)
767 (modify-syntax-entry ?< "." cperl-mode-syntax-table)
768 (modify-syntax-entry ?> "." cperl-mode-syntax-table)
769 (modify-syntax-entry ?& "." cperl-mode-syntax-table)
770 (modify-syntax-entry ?$ "\\" cperl-mode-syntax-table)
771 (modify-syntax-entry ?\n ">" cperl-mode-syntax-table)
772 (modify-syntax-entry ?# "<" cperl-mode-syntax-table)
773 (modify-syntax-entry ?' "\"" cperl-mode-syntax-table)
774 (modify-syntax-entry ?` "\"" cperl-mode-syntax-table)
775 (modify-syntax-entry ?_ "w" cperl-mode-syntax-table)
776 (modify-syntax-entry ?: "_" cperl-mode-syntax-table)
777 (modify-syntax-entry ?| "." cperl-mode-syntax-table))
781 ;; Make customization possible "in reverse"
782 ;;(defun cperl-set (symbol to)
783 ;; (or (eq (symbol-value symbol) 'null) (set symbol to)))
784 (defsubst cperl-val (symbol &optional default hairy)
786 ((eq (symbol-value symbol) 'null) default)
787 (cperl-hairy (or hairy t))
788 (t (symbol-value symbol))))
790 ;; provide an alias for working with emacs 19. the perl-mode that comes
791 ;; with it is really bad, and this lets us seamlessly replace it.
792 (fset 'perl-mode 'cperl-mode)
794 "Major mode for editing Perl code.
795 Expression and list commands understand all C brackets.
796 Tab indents for Perl code.
797 Paragraphs are separated by blank lines only.
798 Delete converts tabs to spaces as it moves back.
800 Various characters in Perl almost always come in pairs: {}, (), [],
801 sometimes <>. When the user types the first, she gets the second as
802 well, with optional special formatting done on {}. (Disabled by
803 default.) You can always quote (with \\[quoted-insert]) the left
804 \"paren\" to avoid the expansion. The processing of < is special,
805 since most the time you mean \"less\". Cperl mode tries to guess
806 whether you want to type pair <>, and inserts is if it
807 appropriate. You can set `cperl-electric-parens-string' to the string that
808 contains the parenths from the above list you want to be electrical.
809 Electricity of parenths is controlled by `cperl-electric-parens'.
810 You may also set `cperl-electric-parens-mark' to have electric parens
811 look for active mark and \"embrace\" a region if possible.'
813 CPerl mode provides expansion of the Perl control constructs:
814 if, else, elsif, unless, while, until, for, and foreach.
815 =========(Disabled by default, see `cperl-electric-keywords'.)
816 The user types the keyword immediately followed by a space, which causes
817 the construct to be expanded, and the user is positioned where she is most
818 likely to want to be.
819 eg. when the user types a space following \"if\" the following appears in
824 and the cursor is between the parentheses. The user can then type some
825 boolean expression within the parens. Having done that, typing
826 \\[cperl-linefeed] places you, appropriately indented on a new line
827 between the braces. If CPerl decides that you want to insert
828 \"English\" style construct like
830 it will not do any expansion. See also help on variable
831 `cperl-extra-newline-before-brace'.
833 \\[cperl-linefeed] is a convinience replacement for typing carriage
834 return. It places you in the next line with proper indentation, or if
835 you type it inside the inline block of control construct, like
836 foreach (@lines) {print; print}
837 and you are on a boundary of a statement inside braces, it will
838 transform the construct into a multiline and will place you into an
839 apporpriately indented blank line. If you need a usual
840 `newline-and-indent' behaviour, it is on \\[newline-and-indent],
841 see documentation on `cperl-electric-linefeed'.
845 Setting the variable `cperl-font-lock' to t switches on
846 font-lock-mode, `cperl-electric-lbrace-space' to t switches on
847 electric space between $ and {, `cperl-electric-parens-string' is the
848 string that contains parentheses that should be electric in CPerl (see
849 also `cperl-electric-parens-mark' and `cperl-electric-parens'),
850 setting `cperl-electric-keywords' enables electric expansion of
851 control structures in CPerl. `cperl-electric-linefeed' governs which
852 one of two linefeed behavior is preferable. You can enable all these
853 options simultaneously (recommended mode of use) by setting
854 `cperl-hairy' to t. In this case you can switch separate options off
855 by setting them to `null'. Note that one may undo the extra whitespace
856 inserted by semis and braces in `auto-newline'-mode by consequent
857 \\[cperl-electric-backspace].
859 If your site has perl5 documentation in info format, you can use commands
860 \\[cperl-info-on-current-command] and \\[cperl-info-on-command] to access it.
861 These keys run commands `cperl-info-on-current-command' and
862 `cperl-info-on-command', which one is which is controlled by variable
863 `cperl-info-on-command-no-prompt' (in turn affected by `cperl-hairy').
865 Variables `cperl-pod-here-scan', `cperl-pod-here-fontify',
866 `cperl-pod-face', `cperl-pod-head-face' control processing of pod and
867 here-docs sections. In a future version results of scan may be used
868 for indentation too, currently they are used for highlighting only.
870 Variables controlling indentation style:
871 `cperl-tab-always-indent'
872 Non-nil means TAB in CPerl mode should always reindent the current line,
873 regardless of where in the line point is when the TAB command is used.
875 Non-nil means automatically newline before and after braces,
876 and after colons and semicolons, inserted in Perl code. The following
877 \\[cperl-electric-backspace] will remove the inserted whitespace.
878 Insertion after colons requires both this variable and
879 `cperl-auto-newline-after-colon' set.
880 `cperl-auto-newline-after-colon'
881 Non-nil means automatically newline even after colons.
882 Subject to `cperl-auto-newline' setting.
884 Indentation of Perl statements within surrounding block.
885 The surrounding block's indentation is the indentation
886 of the line on which the open-brace appears.
887 `cperl-continued-statement-offset'
888 Extra indentation given to a substatement, such as the
889 then-clause of an if, or body of a while, or just a statement continuation.
890 `cperl-continued-brace-offset'
891 Extra indentation given to a brace that starts a substatement.
892 This is in addition to `cperl-continued-statement-offset'.
894 Extra indentation for line if it starts with an open brace.
895 `cperl-brace-imaginary-offset'
896 An open brace following other text is treated as if it the line started
897 this far to the right of the actual line indentation.
899 Extra indentation for line that is a label.
900 `cperl-min-label-indent'
901 Minimal indentation for line that is a label.
903 Settings for K&R and BSD indentation styles are
904 `cperl-indent-level' 5 8
905 `cperl-continued-statement-offset' 5 8
906 `cperl-brace-offset' -5 -8
907 `cperl-label-offset' -5 -8
909 If `cperl-indent-level' is 0, the statement after opening brace in column 0 is indented on `cperl-brace-offset'+`cperl-continued-statement-offset'.
911 Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook'
914 (kill-all-local-variables)
917 ;; (cperl-set 'cperl-font-lock cperl-hairy)
918 ;; (cperl-set 'cperl-electric-lbrace-space cperl-hairy)
919 ;; (cperl-set 'cperl-electric-parens "{[(<")
920 ;; (cperl-set 'cperl-electric-keywords cperl-hairy)
921 ;; (cperl-set 'cperl-electric-linefeed cperl-hairy)))
922 (use-local-map cperl-mode-map)
923 (if (cperl-val 'cperl-electric-linefeed)
925 (local-set-key "\C-J" 'cperl-linefeed)
926 (local-set-key "\C-C\C-J" 'newline-and-indent)))
927 (if (cperl-val 'cperl-info-on-command-no-prompt)
930 ;; don't clobber the backspace binding:
931 (local-set-key [(control h) f] 'cperl-info-on-current-command)
932 (local-set-key "\C-hf" 'cperl-info-on-current-command))
934 ;; don't clobber the backspace binding:
935 (local-set-key [(control c) (control h) f]
936 'cperl-info-on-command)
937 (local-set-key "\C-c\C-hf" 'cperl-info-on-command))))
938 (setq major-mode 'perl-mode)
939 (setq mode-name "CPerl")
940 (if (not cperl-mode-abbrev-table)
941 (let ((prev-a-c abbrevs-changed))
942 (define-abbrev-table 'cperl-mode-abbrev-table '(
943 ("if" "if" cperl-electric-keyword 0)
944 ("elsif" "elsif" cperl-electric-keyword 0)
945 ("while" "while" cperl-electric-keyword 0)
946 ("until" "until" cperl-electric-keyword 0)
947 ("unless" "unless" cperl-electric-keyword 0)
948 ("else" "else" cperl-electric-else 0)
949 ("for" "for" cperl-electric-keyword 0)
950 ("foreach" "foreach" cperl-electric-keyword 0)
951 ("do" "do" cperl-electric-keyword 0)))
952 (setq abbrevs-changed prev-a-c)))
953 (setq local-abbrev-table cperl-mode-abbrev-table)
954 (abbrev-mode (if (cperl-val 'cperl-electric-keywords) 1 0))
955 (set-syntax-table cperl-mode-syntax-table)
956 (make-local-variable 'paragraph-start)
957 (setq paragraph-start (concat "^$\\|" page-delimiter))
958 (make-local-variable 'paragraph-separate)
959 (setq paragraph-separate paragraph-start)
960 (make-local-variable 'paragraph-ignore-fill-prefix)
961 (setq paragraph-ignore-fill-prefix t)
962 (make-local-variable 'indent-line-function)
963 (setq indent-line-function 'cperl-indent-line)
964 (make-local-variable 'require-final-newline)
965 (setq require-final-newline t)
966 (make-local-variable 'comment-start)
967 (setq comment-start "# ")
968 (make-local-variable 'comment-end)
969 (setq comment-end "")
970 (make-local-variable 'comment-column)
971 (setq comment-column cperl-comment-column)
972 (make-local-variable 'comment-start-skip)
973 (setq comment-start-skip "#+ *")
974 (make-local-variable 'defun-prompt-regexp)
975 (setq defun-prompt-regexp "^[ \t]*sub[ \t]+\\([^ \t\n{;]+\\)[ \t]*")
976 (make-local-variable 'comment-indent-function)
977 (setq comment-indent-function 'cperl-comment-indent)
978 (make-local-variable 'parse-sexp-ignore-comments)
979 (setq parse-sexp-ignore-comments t)
980 (make-local-variable 'indent-region-function)
981 (setq indent-region-function 'cperl-indent-region)
982 ;;(setq auto-fill-function 'cperl-do-auto-fill) ; Need to switch on and off!
983 (make-local-variable 'imenu-create-index-function)
984 (setq imenu-create-index-function
985 (function imenu-example--create-perl-index))
986 (make-local-variable 'imenu-sort-function)
987 (setq imenu-sort-function nil)
988 (make-local-variable 'vc-header-alist)
989 (setq vc-header-alist cperl-vc-header-alist)
990 (make-local-variable 'font-lock-defaults)
991 (setq font-lock-defaults
992 (if (string< emacs-version "19.30")
993 '(perl-font-lock-keywords-2)
994 '((perl-font-lock-keywords
995 perl-font-lock-keywords-1
996 perl-font-lock-keywords-2))))
997 (or (fboundp 'cperl-old-auto-fill-mode)
999 (fset 'cperl-old-auto-fill-mode (symbol-function 'auto-fill-mode))
1000 (defun auto-fill-mode (&optional arg)
1002 (cperl-old-auto-fill-mode arg)
1003 (and auto-fill-function (eq major-mode 'perl-mode)
1004 (setq auto-fill-function 'cperl-do-auto-fill)))))
1005 (if (cperl-enable-font-lock)
1006 (if (cperl-val 'cperl-font-lock)
1007 (progn (or cperl-faces-init (cperl-init-faces))
1008 (font-lock-mode 1))))
1009 (and (boundp 'msb-menu-cond)
1010 (not cperl-msb-fixed)
1012 (run-hooks 'cperl-mode-hook)
1013 ;; After hooks since fontification will break this
1014 (if cperl-pod-here-scan (cperl-find-pods-heres)))
1016 ;; Fix for perldb - make default reasonable
1020 (perldb (read-from-minibuffer "Run perldb (like this): "
1021 (if (consp gud-perldb-history)
1022 (car gud-perldb-history)
1023 (concat "perl " ;;(file-name-nondirectory
1027 (buffer-file-name)))
1029 '(gud-perldb-history . 1))))
1032 (defvar cperl-msb-fixed nil)
1034 (defun cperl-msb-fix ()
1035 ;; Adds perl files to msb menu, supposes that msb is already loaded
1036 (setq cperl-msb-fixed t)
1037 (let* ((l (length msb-menu-cond))
1038 (last (nth (1- l) msb-menu-cond))
1039 (precdr (nthcdr (- l 2) msb-menu-cond)) ; cdr of this is last
1040 (handle (1- (nth 1 last))))
1041 (setcdr precdr (list
1043 '(eq major-mode 'perl-mode)
1048 ;; This is used by indent-for-comment
1049 ;; to decide how much to indent a comment in CPerl code
1050 ;; based on its context. Do fallback if comment is found wrong.
1052 (defvar cperl-wrong-comment)
1054 (defun cperl-comment-indent ()
1055 (let ((p (point)) (c (current-column)) was)
1056 (if (looking-at "^#") 0 ; Existing comment at bol stays there.
1057 ;; Wrong comment found
1059 (setq was (cperl-to-comment-or-eol))
1062 (skip-chars-backward " \t")
1063 (max (1+ (current-column)) ; Else indent at comment column
1066 (insert comment-start)
1067 (backward-char (length comment-start)))
1068 (setq cperl-wrong-comment t)
1069 (indent-to comment-column 1) ; Indent minimum 1
1070 c))))) ; except leave at least one space.
1072 ;;;(defun cperl-comment-indent-fallback ()
1073 ;;; "Is called if the standard comment-search procedure fails.
1074 ;;;Point is at start of real comment."
1075 ;;; (let ((c (current-column)) target cnt prevc)
1076 ;;; (if (= c comment-column) nil
1077 ;;; (setq cnt (skip-chars-backward "[ \t]"))
1078 ;;; (setq target (max (1+ (setq prevc
1079 ;;; (current-column))) ; Else indent at comment column
1080 ;;; comment-column))
1081 ;;; (if (= c comment-column) nil
1082 ;;; (delete-backward-char cnt)
1083 ;;; (while (< prevc target)
1085 ;;; (setq prevc (current-column)))
1086 ;;; (if (> prevc target) (progn (delete-char -1) (setq prevc (current-column))))
1087 ;;; (while (< prevc target)
1089 ;;; (setq prevc (current-column)))))))
1091 (defun cperl-indent-for-comment ()
1092 "Substite for `indent-for-comment' in CPerl."
1094 (let (cperl-wrong-comment)
1095 (indent-for-comment)
1096 (if cperl-wrong-comment
1097 (progn (cperl-to-comment-or-eol)
1098 (forward-char (length comment-start))))))
1100 (defun cperl-comment-region (b e arg)
1101 "Comment or uncomment each line in the region in CPerl mode.
1102 See `comment-region'."
1103 (interactive "r\np")
1104 (let ((comment-start "#"))
1105 (comment-region b e arg)))
1107 (defun cperl-uncomment-region (b e arg)
1108 "Uncomment or comment each line in the region in CPerl mode.
1109 See `comment-region'."
1110 (interactive "r\np")
1111 (let ((comment-start "#"))
1112 (comment-region b e (- arg))))
1114 (defun cperl-electric-brace (arg &optional only-before)
1115 "Insert character and correct line's indentation.
1116 If ONLY-BEFORE and `cperl-auto-newline', will insert newline before the
1117 place (even in empty line), but not after. If after \")\" and the inserted
1118 char is \"{\", insert extra newline before only if
1119 `cperl-extra-newline-before-brace'."
1122 (if (and (not arg) ; No args, end (of empty line or auto)
1124 (or (and (null only-before)
1126 (skip-chars-backward " \t")
1128 (and (eq last-command-char ?\{) ; Do not insert newline
1129 ;; if after ")" and `cperl-extra-newline-before-brace'
1130 ;; is nil, do not insert extra newline.
1131 (not cperl-extra-newline-before-brace)
1133 (skip-chars-backward " \t")
1134 (eq (preceding-char) ?\))))
1135 (if cperl-auto-newline
1136 (progn (cperl-indent-line) (newline) t) nil)))
1138 (if cperl-auto-newline
1139 (setq insertpos (point)))
1140 (insert last-command-char)
1142 (if (and cperl-auto-newline (null only-before))
1145 (cperl-indent-line)))
1147 (if insertpos (progn (goto-char insertpos)
1148 (search-forward (make-string
1149 1 last-command-char))
1150 (setq insertpos (1- (point)))))
1154 (goto-char insertpos)
1155 (self-insert-command (prefix-numeric-value arg)))
1156 (self-insert-command (prefix-numeric-value arg)))))
1158 (defun cperl-electric-lbrace (arg)
1159 "Insert character, correct line's indentation, correct quoting by space."
1162 (cperl-auto-newline cperl-auto-newline)
1163 (other-end (if (and cperl-electric-parens-mark
1170 (and (cperl-val 'cperl-electric-lbrace-space)
1171 (eq (preceding-char) ?$)
1173 (skip-chars-backward "$")
1174 (looking-at "\\(\\$\\$\\)*\\$\\([^\\$]\\|$\\)"))
1176 (if (cperl-after-expr-p nil "{};)") nil (setq cperl-auto-newline nil))
1177 (cperl-electric-brace arg)
1178 (and (cperl-val 'cperl-electric-parens)
1179 (eq last-command-char ?{)
1180 (memq last-command-char
1181 (append cperl-electric-parens-string nil))
1182 (or (if other-end (goto-char (marker-position other-end)))
1184 (setq last-command-char ?} pos (point))
1185 (progn (cperl-electric-brace arg t)
1188 (defun cperl-electric-paren (arg)
1189 "Insert a matching pair of parentheses."
1191 (let ((beg (save-excursion (beginning-of-line) (point)))
1192 (other-end (if (and cperl-electric-parens-mark
1199 (if (and (cperl-val 'cperl-electric-parens)
1200 (memq last-command-char
1201 (append cperl-electric-parens-string nil))
1202 (>= (save-excursion (cperl-to-comment-or-eol) (point)) (point))
1203 ;;(not (save-excursion (search-backward "#" beg t)))
1204 (if (eq last-command-char ?<)
1205 (cperl-after-expr-p nil "{};(,:=")
1208 (insert last-command-char)
1209 (if other-end (goto-char (marker-position other-end)))
1210 (insert (cdr (assoc last-command-char '((?{ .?})
1215 (insert last-command-char)
1218 (defun cperl-electric-keyword ()
1219 "Insert a construction appropriate after a keyword."
1220 (let ((beg (save-excursion (beginning-of-line) (point)))
1221 (dollar (eq (preceding-char) ?$)))
1222 (and (save-excursion
1224 (cperl-after-expr-p nil "{};:"))
1228 "[#\"'`]\\|\\<q\\(\\|[wqx]\\)\\>"
1230 (save-excursion (or (not (re-search-backward "^=" nil t))
1231 (looking-at "=cut")))
1233 (and dollar (insert " $"))
1235 ;;(insert " () {\n}")
1237 (cperl-extra-newline-before-brace
1245 (insert " () {\n}"))
1247 (or (looking-at "[ \t]\\|$") (insert " "))
1249 (if dollar (progn (search-backward "$")
1251 (search-backward ")"))
1252 (cperl-putback-char del-back-ch)))))
1254 (defun cperl-electric-else ()
1255 "Insert a construction appropriate after a keyword."
1256 (let ((beg (save-excursion (beginning-of-line) (point))))
1257 (and (save-excursion
1259 (cperl-after-expr-p nil "{};:"))
1263 "[#\"'`]\\|\\<q\\(\\|[wqx]\\)\\>"
1265 (save-excursion (or (not (re-search-backward "^=" nil t))
1266 (looking-at "=cut")))
1269 ;;(insert " {\n\n}")
1271 (cperl-extra-newline-before-brace
1279 (or (looking-at "[ \t]\\|$") (insert " "))
1283 (cperl-putback-char del-back-ch)))))
1285 (defun cperl-linefeed ()
1286 "Go to end of line, open a new line and indent appropriately."
1288 (let ((beg (save-excursion (beginning-of-line) (point)))
1289 (end (save-excursion (end-of-line) (point)))
1290 (pos (point)) start)
1291 (if (and ; Check if we need to split:
1292 ; i.e., on a boundary and inside "{...}"
1293 (save-excursion (cperl-to-comment-or-eol)
1294 (>= (point) pos)) ; Not in a comment
1296 (skip-chars-backward " \t" beg)
1298 (looking-at "[;{]")) ; After { or ; + spaces
1299 (looking-at "[ \t]*}") ; Before }
1300 (re-search-forward "\\=[ \t]*;" end t)) ; Before spaces + ;
1303 (eq (car (parse-partial-sexp pos end -1)) -1)
1304 ; Leave the level of parens
1305 (looking-at "[,; \t]*\\($\\|#\\)") ; Comma to allow anon subr
1309 (setq start (point-marker))
1310 (<= start pos))))) ; Redundant? Are after the
1311 ; start of parens group.
1313 (skip-chars-backward " \t")
1314 (or (memq (preceding-char) (append ";{" nil))
1320 (or (looking-at "{[ \t]*$") ; If there is a statement
1321 ; before, move it to separate line
1325 (cperl-indent-line)))
1326 (forward-line 1) ; We are on the target line
1329 (or (looking-at "[ \t]*}[,; \t]*$") ; If there is a statement
1330 ; after, move it to separate line
1333 (search-backward "}" beg)
1334 (skip-chars-backward " \t")
1335 (or (memq (preceding-char) (append ";{" nil))
1340 (forward-line -1) ; We are on the line before target
1342 (newline-and-indent))
1343 (end-of-line) ; else
1345 ((and (looking-at "\n[ \t]*{$")
1347 (skip-chars-backward " \t")
1348 (eq (preceding-char) ?\)))) ; Probably if () {} group
1349 ; with an extra newline.
1351 (cperl-indent-line))
1352 ((looking-at "\n[ \t]*$") ; Next line is empty - use it.
1354 (cperl-indent-line))
1356 (newline-and-indent))))))
1358 (defun cperl-electric-semi (arg)
1359 "Insert character and correct line's indentation."
1361 (if cperl-auto-newline
1362 (cperl-electric-terminator arg)
1363 (self-insert-command (prefix-numeric-value arg))))
1365 (defun cperl-electric-terminator (arg)
1366 "Insert character and correct line's indentation."
1368 (let (insertpos (end (point))
1369 (auto (and cperl-auto-newline
1370 (or (not (eq last-command-char ?:))
1371 cperl-auto-newline-after-colon))))
1372 (if (and ;;(not arg)
1374 (not (save-excursion
1376 (skip-chars-forward " \t")
1378 ;; Ignore in comment lines
1379 (= (following-char) ?#)
1380 ;; Colon is special only after a label
1381 ;; So quickly rule out most other uses of colon
1382 ;; and do no indentation for them.
1383 (and (eq last-command-char ?:)
1386 (skip-chars-forward " \t")
1387 (and (< (point) end)
1388 (progn (goto-char (- end 1))
1389 (not (looking-at ":"))))))
1391 (beginning-of-defun)
1392 (let ((pps (parse-partial-sexp (point) end)))
1393 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
1395 (insert last-command-char)
1397 (if auto (setq insertpos (point-marker)))
1403 (cperl-indent-line)))
1405 ;; (if insertpos (progn (goto-char (marker-position insertpos))
1406 ;; (search-forward (make-string
1407 ;; 1 last-command-char))
1408 ;; (setq insertpos (1- (point)))))
1409 ;; (delete-char -1))))
1411 (if insertpos (goto-char (1- (marker-position insertpos)))
1416 (goto-char insertpos)
1417 (self-insert-command (prefix-numeric-value arg)))
1418 (self-insert-command (prefix-numeric-value arg)))))
1420 (defun cperl-electric-backspace (arg)
1421 "Backspace-untabify, or remove the whitespace inserted by an electric key."
1423 (if (and cperl-auto-newline
1424 (memq last-command '(cperl-electric-semi
1425 cperl-electric-terminator
1426 cperl-electric-lbrace))
1427 (memq (preceding-char) '(? ?\t ?\n)))
1429 (if (eq last-command 'cperl-electric-lbrace)
1430 (skip-chars-forward " \t\n"))
1432 (skip-chars-backward " \t\n")
1433 (delete-region (point) p))
1434 (backward-delete-char-untabify arg)))
1436 (defun cperl-inside-parens-p ()
1440 (narrow-to-region (point)
1441 (progn (beginning-of-defun) (point)))
1442 (goto-char (point-max))
1443 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
1446 (defun cperl-indent-command (&optional whole-exp)
1447 "Indent current line as Perl code, or in some cases insert a tab character.
1448 If `cperl-tab-always-indent' is non-nil (the default), always indent current line.
1449 Otherwise, indent the current line only if point is at the left margin
1450 or in the line's indentation; otherwise insert a tab.
1452 A numeric argument, regardless of its value,
1453 means indent rigidly all the lines of the expression starting after point
1454 so that this line becomes properly indented.
1455 The relative indentation among the lines of the expression are preserved."
1458 ;; If arg, always indent this line as Perl
1459 ;; and shift remaining lines of expression the same amount.
1460 (let ((shift-amt (cperl-indent-line))
1463 (if cperl-tab-always-indent
1464 (beginning-of-line))
1472 (indent-code-rigidly beg end shift-amt "#")))
1473 (if (and (not cperl-tab-always-indent)
1475 (skip-chars-backward " \t")
1478 (cperl-indent-line))))
1480 (defun cperl-indent-line (&optional symbol)
1481 "Indent current line as Perl code.
1482 Return the amount the indentation changed by."
1485 (case-fold-search nil)
1486 (pos (- (point-max) (point))))
1487 (setq indent (cperl-calculate-indent nil symbol))
1490 (cond ((eq indent nil)
1491 (setq indent (current-indentation)))
1492 ;;((eq indent t) ; Never?
1493 ;; (setq indent (cperl-calculate-indent-within-comment)))
1494 ;;((looking-at "[ \t]*#")
1497 (skip-chars-forward " \t")
1498 (if (listp indent) (setq indent (car indent)))
1499 (cond ((looking-at "[A-Za-z]+:[^:]")
1501 (setq indent (max cperl-min-label-indent
1502 (+ indent cperl-label-offset)))))
1503 ((= (following-char) ?})
1504 (setq indent (- indent cperl-indent-level)))
1505 ((memq (following-char) '(?\) ?\])) ; To line up with opening paren.
1506 (setq indent (+ indent cperl-close-paren-offset)))
1507 ((= (following-char) ?{)
1508 (setq indent (+ indent cperl-brace-offset))))))
1509 (skip-chars-forward " \t")
1510 (setq shift-amt (- indent (current-column)))
1511 (if (zerop shift-amt)
1512 (if (> (- (point-max) pos) (point))
1513 (goto-char (- (point-max) pos)))
1514 (delete-region beg (point))
1516 ;; If initial point was within line's indentation,
1517 ;; position after the indentation. Else stay at same point in text.
1518 (if (> (- (point-max) pos) (point))
1519 (goto-char (- (point-max) pos))))
1522 (defun cperl-after-label ()
1523 ;; Returns true if the point is after label. Does not do save-excursion.
1524 (and (eq (preceding-char) ?:)
1525 (memq (char-syntax (char-after (- (point) 2)))
1529 (looking-at "[a-zA-Z_][a-zA-Z0-9_]*:[^:]"))))
1531 (defun cperl-get-state (&optional parse-start start-state)
1532 ;; returns list (START STATE DEPTH PRESTART), START is a good place
1533 ;; to start parsing, STATE is what is returned by
1534 ;; `parse-partial-sexp'. DEPTH is true is we are immediately after
1535 ;; end of block which contains START. PRESTART is the position
1536 ;; basing on which START was found.
1538 (let ((start-point (point)) depth state start prestart)
1540 (goto-char parse-start)
1541 (beginning-of-defun))
1542 (setq prestart (point))
1544 ;; Try to go out, if sub is not on the outermost level
1545 (while (< (point) start-point)
1546 (setq start (point) parse-start start depth nil
1547 state (parse-partial-sexp start start-point -1))
1548 (if (> (car state) -1) nil
1549 ;; The current line could start like }}}, so the indentation
1550 ;; corresponds to a different level than what we reached
1552 (beginning-of-line 2))) ; Go to the next line.
1553 (if start (goto-char start))) ; Not at the start of file
1554 (setq start (point))
1555 (if (< start start-point) (setq parse-start start))
1556 (or state (setq state (parse-partial-sexp start start-point -1 nil start-state)))
1557 (list start state depth prestart))))
1559 (defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
1560 ;; Positions is before ?\{. Checks whether it starts a block.
1561 ;; No save-excursion!
1562 (cperl-backward-to-noncomment (point-min))
1563 ;;(skip-chars-backward " \t\n\f")
1564 (or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
1565 ; Label may be mixed up with `$blah :'
1566 (save-excursion (cperl-after-label))
1567 (and (memq (char-syntax (preceding-char)) '(?w ?_))
1570 ;; Need take into account `bless', `return', `tr',...
1571 (or (and (looking-at "[a-zA-Z0-9_:]+[ \t\n\f]*[{#]") ; Method call syntax
1572 (not (looking-at "\\(bless\\|return\\|qw\\|tr\\|[smy]\\)\\>")))
1574 (skip-chars-backward " \t\n\f")
1575 (and (memq (char-syntax (preceding-char)) '(?w ?_))
1579 "sub[ \t]+[a-zA-Z0-9_:]+[ \t\n\f]*[#{]")))))))))
1581 (defun cperl-calculate-indent (&optional parse-start symbol)
1582 "Return appropriate indentation for current line as Perl code.
1583 In usual case returns an integer: the column to indent to.
1584 Returns nil if line starts inside a string, t if in a comment."
1586 (if (memq (get-text-property (point) 'syntax-type) '(pod here-doc)) nil
1588 (let* ((indent-point (point))
1589 (case-fold-search nil)
1590 (s-s (cperl-get-state))
1593 (containing-sexp (car (cdr state)))
1594 (char-after (save-excursion
1595 (skip-chars-forward " \t")
1597 (start-indent (save-excursion
1599 (- (current-indentation)
1600 (if (nth 2 s-s) cperl-indent-level 0))))
1602 ;; (or parse-start (null symbol)
1603 ;; (setq parse-start (symbol-value symbol)
1604 ;; start-indent (nth 2 parse-start)
1605 ;; parse-start (car parse-start)))
1607 ;; (goto-char parse-start)
1608 ;; (beginning-of-defun))
1610 ;; (while (< (point) indent-point)
1611 ;; (setq start (point) parse-start start moved nil
1612 ;; state (parse-partial-sexp start indent-point -1))
1613 ;; (if (> (car state) -1) nil
1614 ;; ;; The current line could start like }}}, so the indentation
1615 ;; ;; corresponds to a different level than what we reached
1617 ;; (beginning-of-line 2))) ; Go to the next line.
1618 ;; (if start ; Not at the start of file
1620 ;; (goto-char start)
1621 ;; (setq start-indent (current-indentation))
1622 ;; (if moved ; Should correct...
1623 ;; (setq start-indent (- start-indent cperl-indent-level))))
1624 ;; (setq start-indent 0))
1625 ;; (if (< (point) indent-point) (setq parse-start (point)))
1626 ;; (or state (setq state (parse-partial-sexp
1627 ;; (point) indent-point -1 nil start-state)))
1628 ;; (setq containing-sexp
1629 ;; (or (car (cdr state))
1630 ;; (and (>= (nth 6 state) 0) old-containing-sexp))
1631 ;; old-containing-sexp nil start-state nil)
1632 ;;;; (while (< (point) indent-point)
1633 ;;;; (setq parse-start (point))
1634 ;;;; (setq state (parse-partial-sexp (point) indent-point -1 nil start-state))
1635 ;;;; (setq containing-sexp
1636 ;;;; (or (car (cdr state))
1637 ;;;; (and (>= (nth 6 state) 0) old-containing-sexp))
1638 ;;;; old-containing-sexp nil start-state nil))
1639 ;; (if symbol (set symbol (list indent-point state start-indent)))
1640 ;; (goto-char indent-point)
1641 (cond ((or (nth 3 state) (nth 4 state))
1642 ;; return nil or t if should not change this line
1644 ((null containing-sexp)
1645 ;; Line is at top level. May be data or function definition,
1646 ;; or may be function argument declaration.
1647 ;; Indent like the previous top level line
1648 ;; unless that ends in a closeparen without semicolon,
1649 ;; in which case this line is the first argument decl.
1650 (skip-chars-forward " \t")
1652 (if (= (following-char) ?{) cperl-continued-brace-offset 0)
1654 (cperl-backward-to-noncomment (or parse-start (point-min)))
1655 ;;(skip-chars-backward " \t\f\n")
1656 ;; Look at previous line that's at column 0
1657 ;; to determine whether we are in top-level decls
1658 ;; or function's arg decls. Set basic-indent accordingly.
1659 ;; Now add a little if this is a continuation line.
1661 (memq (preceding-char) (append " ;}" nil)) ; Was ?\)
1662 (memq char-after (append ")]}" nil)))
1664 cperl-continued-statement-offset))))
1665 ((/= (char-after containing-sexp) ?{)
1666 ;; line is expression, not statement:
1667 ;; indent to just after the surrounding open,
1668 ;; skip blanks if we do not close the expression.
1669 (goto-char (1+ containing-sexp))
1670 (or (memq char-after (append ")]}" nil))
1671 (looking-at "[ \t]*\\(#\\|$\\)")
1672 (skip-chars-forward " \t"))
1675 ;; Containing-expr starts with \{. Check whether it is a hash.
1676 (goto-char containing-sexp)
1677 (not (cperl-block-p)))
1678 (goto-char (1+ containing-sexp))
1679 (or (eq char-after ?\})
1680 (looking-at "[ \t]*\\(#\\|$\\)")
1681 (skip-chars-forward " \t"))
1682 (+ (current-column) ; Correct indentation of trailing ?\}
1683 (if (eq char-after ?\}) (+ cperl-indent-level
1684 cperl-close-paren-offset)
1687 ;; Statement level. Is it a continuation or a new statement?
1688 ;; Find previous non-comment character.
1689 (goto-char indent-point)
1690 (cperl-backward-to-noncomment containing-sexp)
1691 ;; Back up over label lines, since they don't
1692 ;; affect whether our line is a continuation.
1693 (while (or (eq (preceding-char) ?\,)
1694 (and (eq (preceding-char) ?:)
1695 (or;;(eq (char-after (- (point) 2)) ?\') ; ????
1696 (memq (char-syntax (char-after (- (point) 2)))
1698 (if (eq (preceding-char) ?\,)
1699 ;; Will go to beginning of line, essentially.
1700 ;; Will ignore embedded sexpr XXXX.
1701 (cperl-backward-to-start-of-continued-exp containing-sexp))
1703 (cperl-backward-to-noncomment containing-sexp))
1704 ;; Now we get the answer.
1705 (if (not (memq (preceding-char) (append ", ;}{" '(nil)))) ; Was ?\,
1706 ;; This line is continuation of preceding line's statement;
1707 ;; indent `cperl-continued-statement-offset' more than the
1708 ;; previous line of the statement.
1710 (cperl-backward-to-start-of-continued-exp containing-sexp)
1711 (+ (if (memq char-after (append "}])" nil))
1713 cperl-continued-statement-offset)
1715 (if (eq char-after ?\{)
1716 cperl-continued-brace-offset 0)))
1717 ;; This line starts a new statement.
1718 ;; Position following last unclosed open.
1719 (goto-char containing-sexp)
1720 ;; Is line first statement after an open-brace?
1722 ;; If no, find that first statement and indent like
1723 ;; it. If the first statement begins with label, do
1724 ;; not belive when the indentation of the label is too
1728 (setq old-indent (current-indentation))
1729 (let ((colon-line-end 0))
1730 (while (progn (skip-chars-forward " \t\n")
1731 (looking-at "#\\|[a-zA-Z0-9_$]*:[^:]"))
1732 ;; Skip over comments and labels following openbrace.
1733 (cond ((= (following-char) ?\#)
1737 (save-excursion (end-of-line)
1738 (setq colon-line-end (point)))
1739 (search-forward ":"))))
1740 ;; The first following code counts
1741 ;; if it is before the line we want to indent.
1742 (and (< (point) indent-point)
1743 (if (> colon-line-end (point)) ; After label
1744 (if (> (current-indentation)
1745 cperl-min-label-indent)
1746 (- (current-indentation) cperl-label-offset)
1747 ;; Do not belive: `max' is involved
1748 (+ old-indent cperl-indent-level))
1749 (current-column)))))
1750 ;; If no previous statement,
1751 ;; indent it relative to line brace is on.
1752 ;; For open brace in column zero, don't let statement
1753 ;; start there too. If cperl-indent-level is zero,
1754 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
1755 ;; For open-braces not the first thing in a line,
1756 ;; add in cperl-brace-imaginary-offset.
1758 ;; If first thing on a line: ?????
1759 (+ (if (and (bolp) (zerop cperl-indent-level))
1760 (+ cperl-brace-offset cperl-continued-statement-offset)
1762 ;; Move back over whitespace before the openbrace.
1763 ;; If openbrace is not first nonwhite thing on the line,
1764 ;; add the cperl-brace-imaginary-offset.
1765 (progn (skip-chars-backward " \t")
1766 (if (bolp) 0 cperl-brace-imaginary-offset))
1767 ;; If the openbrace is preceded by a parenthesized exp,
1768 ;; move to the beginning of that;
1769 ;; possibly a different line
1771 (if (eq (preceding-char) ?\))
1773 ;; In the case it starts a subroutine, indent with
1774 ;; respect to `sub', not with respect to the the
1775 ;; first thing on the line, say in the case of
1776 ;; anonymous sub in a hash.
1778 (skip-chars-backward " \t")
1779 (if (and (eq (preceding-char) ?b)
1782 (looking-at "sub\\>"))
1786 (save-excursion (beginning-of-line) (point))
1788 (progn (goto-char (1+ old-indent))
1789 (skip-chars-forward " \t")
1791 ;; Get initial indentation of the line we are on.
1792 ;; If line starts with label, calculate label indentation
1795 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_]*:[^:]"))
1796 (if (> (current-indentation) cperl-min-label-indent)
1797 (- (current-indentation) cperl-label-offset)
1798 (cperl-calculate-indent
1799 (if (and parse-start (<= parse-start (point)))
1801 (current-indentation)))))))))))))
1803 (defvar cperl-indent-alist
1807 (toplevel-after-parenth 2)
1808 (toplevel-continued 2)
1810 "Alist of indentation rules for CPerl mode.
1813 number: add this amount of indentation.")
1815 (defun cperl-where-am-i (&optional parse-start start-state)
1817 "Return a list of lists ((TYPE POS)...) of good points before the point.
1818 POS may be nil if it is hard to find, say, when TYPE is `string' or `comment'."
1820 (let* ((start-point (point))
1821 (s-s (cperl-get-state))
1824 (prestart (nth 3 s-s))
1825 (containing-sexp (car (cdr state)))
1826 (case-fold-search nil)
1827 (res (list (list 'parse-start start) (list 'parse-prestart prestart))))
1828 (cond ((nth 3 state) ; In string
1829 (setq res (cons (list 'string nil (nth 3 state)) res))) ; What started string
1830 ((nth 4 state) ; In comment
1831 (setq res (cons '(comment) res)))
1832 ((null containing-sexp)
1833 ;; Line is at top level.
1834 ;; Indent like the previous top level line
1835 ;; unless that ends in a closeparen without semicolon,
1836 ;; in which case this line is the first argument decl.
1837 (cperl-backward-to-noncomment (or parse-start (point-min)))
1838 ;;(skip-chars-backward " \t\f\n")
1841 (memq (preceding-char) (append ";}" nil)))
1842 (setq res (cons (list 'toplevel start) res)))
1843 ((eq (preceding-char) ?\) )
1844 (setq res (cons (list 'toplevel-after-parenth start) res)))
1846 (setq res (cons (list 'toplevel-continued start) res)))))
1847 ((/= (char-after containing-sexp) ?{)
1848 ;; line is expression, not statement:
1849 ;; indent to just after the surrounding open.
1850 ;; skip blanks if we do not close the expression.
1851 (setq res (cons (list 'expression-blanks
1853 (goto-char (1+ containing-sexp))
1854 (or (looking-at "[ \t]*\\(#\\|$\\)")
1855 (skip-chars-forward " \t"))
1857 (cons (list 'expression containing-sexp) res))))
1859 ;; Containing-expr starts with \{. Check whether it is a hash.
1860 (goto-char containing-sexp)
1861 (not (cperl-block-p)))
1862 (setq res (cons (list 'expression-blanks
1864 (goto-char (1+ containing-sexp))
1865 (or (looking-at "[ \t]*\\(#\\|$\\)")
1866 (skip-chars-forward " \t"))
1868 (cons (list 'expression containing-sexp) res))))
1871 (setq res (cons (list 'in-block containing-sexp) res))
1872 ;; Is it a continuation or a new statement?
1873 ;; Find previous non-comment character.
1874 (cperl-backward-to-noncomment containing-sexp)
1875 ;; Back up over label lines, since they don't
1876 ;; affect whether our line is a continuation.
1877 ;; Back up comma-delimited lines too ?????
1878 (while (or (eq (preceding-char) ?\,)
1879 (save-excursion (cperl-after-label)))
1880 (if (eq (preceding-char) ?\,)
1881 ;; Will go to beginning of line, essentially
1882 ;; Will ignore embedded sexpr XXXX.
1883 (cperl-backward-to-start-of-continued-exp containing-sexp))
1885 (cperl-backward-to-noncomment containing-sexp))
1886 ;; Now we get the answer.
1887 (if (not (memq (preceding-char) (append ";}{" '(nil)))) ; Was ?\,
1888 ;; This line is continuation of preceding line's statement.
1889 (list (list 'statement-continued containing-sexp))
1890 ;; This line starts a new statement.
1891 ;; Position following last unclosed open.
1892 (goto-char containing-sexp)
1893 ;; Is line first statement after an open-brace?
1895 ;; If no, find that first statement and indent like
1896 ;; it. If the first statement begins with label, do
1897 ;; not belive when the indentation of the label is too
1901 (let ((colon-line-end 0))
1902 (while (progn (skip-chars-forward " \t\n" start-point)
1903 (and (< (point) start-point)
1905 "#\\|[a-zA-Z_][a-zA-Z0-9_]*:[^:]")))
1906 ;; Skip over comments and labels following openbrace.
1907 (cond ((= (following-char) ?\#)
1912 (save-excursion (end-of-line)
1913 (setq colon-line-end (point)))
1914 (search-forward ":"))))
1915 ;; Now at the point, after label, or at start
1916 ;; of first statement in the block.
1917 (and (< (point) start-point)
1918 (if (> colon-line-end (point))
1919 ;; Before statement after label
1920 (if (> (current-indentation)
1921 cperl-min-label-indent)
1922 (list (list 'label-in-block (point)))
1923 ;; Do not belive: `max' is involved
1925 (list 'label-in-block-min-indent (point))))
1927 (list 'statement-in-block (point))))))
1928 ;; If no previous statement,
1929 ;; indent it relative to line brace is on.
1930 ;; For open brace in column zero, don't let statement
1931 ;; start there too. If cperl-indent-level is zero,
1932 ;; use cperl-brace-offset + cperl-continued-statement-offset instead.
1933 ;; For open-braces not the first thing in a line,
1934 ;; add in cperl-brace-imaginary-offset.
1936 ;; If first thing on a line: ?????
1937 (+ (if (and (bolp) (zerop cperl-indent-level))
1938 (+ cperl-brace-offset cperl-continued-statement-offset)
1940 ;; Move back over whitespace before the openbrace.
1941 ;; If openbrace is not first nonwhite thing on the line,
1942 ;; add the cperl-brace-imaginary-offset.
1943 (progn (skip-chars-backward " \t")
1944 (if (bolp) 0 cperl-brace-imaginary-offset))
1945 ;; If the openbrace is preceded by a parenthesized exp,
1946 ;; move to the beginning of that;
1947 ;; possibly a different line
1949 (if (eq (preceding-char) ?\))
1951 ;; Get initial indentation of the line we are on.
1952 ;; If line starts with label, calculate label indentation
1955 (looking-at "[ \t]*[a-zA-Z_][a-zA-Z_]*:[^:]"))
1956 (if (> (current-indentation) cperl-min-label-indent)
1957 (- (current-indentation) cperl-label-offset)
1958 (cperl-calculate-indent
1959 (if (and parse-start (<= parse-start (point)))
1961 (current-indentation))))))))
1964 (defun cperl-calculate-indent-within-comment ()
1965 "Return the indentation amount for line, assuming that
1966 the current line is to be regarded as part of a block comment."
1967 (let (end star-start)
1970 (skip-chars-forward " \t")
1972 (and (= (following-char) ?#)
1974 (cperl-to-comment-or-eol)
1980 (defun cperl-to-comment-or-eol ()
1981 "Goes to position before comment on the current line, or to end of line.
1982 Returns true if comment is found."
1983 (let (state stop-in cpoint (lim (progn (end-of-line) (point))))
1985 (if (re-search-forward "\\=[ \t]*\\(#\\|$\\)" lim t)
1986 (if (eq (preceding-char) ?\#) (progn (backward-char 1) t))
1988 (while (not stop-in)
1989 (setq state (parse-partial-sexp (point) lim nil nil nil t))
1991 ;; If fails (beginning-of-line inside sexp), then contains not-comment
1992 ;; Do simplified processing
1993 ;;(if (re-search-forward "[^$]#" lim 1)
1995 ;; (forward-char -1)
1996 ;; (skip-chars-backward " \t\n\f" lim))
1997 ;; (goto-char lim)) ; No `#' at all
1999 (if (nth 4 state) ; After `#';
2000 ; (nth 2 state) can be
2001 ; beginning of m,s,qq and so
2005 (setq cpoint (point))
2006 (goto-char (nth 2 state))
2008 ((looking-at "\\(s\\|tr\\)\\>")
2009 (or (re-search-forward
2010 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*"
2013 ((looking-at "\\(m\\|q\\([qxw]\\)?\\)\\>")
2014 (or (re-search-forward
2015 "\\=\\w+[ \t]*#\\([^\n\\\\#]\\|\\\\[\\\\#]\\)*#"
2018 (t ; It was fair comment
2019 (setq stop-in t) ; Finish
2020 (goto-char (1- cpoint)))))
2021 (setq stop-in t) ; Finish
2023 (setq stop-in t)) ; Finish
2027 (defun cperl-find-pods-heres (&optional min max)
2028 "Scans the buffer for POD sections and here-documents.
2029 If `cperl-pod-here-fontify' is not-nil after evaluation, will fontify
2030 the sections using `cperl-pod-head-face', `cperl-pod-face',
2033 (or min (setq min (point-min)))
2034 (or max (setq max (point-max)))
2035 (let (face head-face here-face b e bb tag qtag err b1 e1 argument
2036 (cperl-pod-here-fontify (eval cperl-pod-here-fontify))
2037 (case-fold-search nil) (inhibit-read-only t) (buffer-undo-list t)
2038 (modified (buffer-modified-p))
2039 (after-change-functions nil)
2042 "\\(\\`\n?\\|\n\n\\)="
2044 ;; One extra () before this:
2045 "<<\\(\\([\"'`]\\)\\([^\"'`\n]*\\)\\3\\|\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)\\)"
2047 ;; 1+5 extra () before this:
2048 "^[ \t]*format[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$")))
2052 (message "Scanning for pods, formats and here-docs...")
2053 (if cperl-pod-here-fontify
2054 ;; We had evals here, do not know why...
2055 (setq face cperl-pod-face
2056 head-face cperl-pod-head-face
2057 here-face cperl-here-face))
2058 (remove-text-properties min max '(syntax-type t))
2059 ;; Need to remove face as well...
2061 (while (re-search-forward search max t)
2063 ((match-beginning 1) ; POD section
2064 ;; "\\(\\`\n?\\|\n\n\\)="
2065 (if (looking-at "\n*cut\\>")
2067 (message "=cut is not preceeded by a pod section")
2071 (setq b (point) bb b)
2072 (or (re-search-forward "\n\n=cut\\>" max 'toend)
2073 (message "Cannot find the end of a pod section"))
2074 (beginning-of-line 3)
2076 (put-text-property b e 'in-pod t)
2078 (while (re-search-forward "\n\n[ \t]" e t)
2080 (put-text-property b (point) 'syntax-type 'pod)
2081 (cperl-put-do-not-fontify b (point))
2082 ;;(put-text-property (max (point-min) (1- b))
2083 ;; (point) cperl-do-not-fontify t)
2084 (if cperl-pod-here-fontify (put-text-property b (point) 'face face))
2085 (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
2088 (put-text-property (point) e 'syntax-type 'pod)
2089 (cperl-put-do-not-fontify (point) e)
2090 ;;(put-text-property (max (point-min) (1- (point)))
2091 ;; e cperl-do-not-fontify t)
2092 (if cperl-pod-here-fontify
2093 (progn (put-text-property (point) e 'face face)
2096 "=[a-zA-Z0-9]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
2098 (match-beginning 1) (match-end 1)
2100 (while (re-search-forward
2102 "\n\n=[a-zA-Z0-9]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
2105 (match-beginning 1) (match-end 1)
2109 ;; "<<\\(\\([\"'`]\\)\\([^\"'`\n]*\\)\\3\\|\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)\\)"
2110 ((match-beginning 2) ; 1 + 1
2111 (if (match-beginning 5) ;4 + 1
2112 (setq b1 (match-beginning 5) ; 4 + 1
2113 e1 (match-end 5)) ; 4 + 1
2114 (setq b1 (match-beginning 4) ; 3 + 1
2115 e1 (match-end 4))) ; 3 + 1
2116 (setq tag (buffer-substring b1 e1)
2117 qtag (regexp-quote tag))
2118 (cond (cperl-pod-here-fontify
2119 (put-text-property b1 e1 'face font-lock-reference-face)
2120 (cperl-put-do-not-fontify b1 e1)))
2123 (cond ((re-search-forward (concat "^" qtag "$") max 'toend)
2124 (if cperl-pod-here-fontify
2126 (put-text-property (match-beginning 0) (match-end 0)
2127 'face font-lock-reference-face)
2128 (cperl-put-do-not-fontify b (match-end 0))
2129 ;;(put-text-property (max (point-min) (1- b))
2131 ;; (1+ (match-end 0)))
2132 ;; cperl-do-not-fontify t)
2133 (put-text-property b (match-beginning 0)
2135 (put-text-property b (match-beginning 0)
2136 'syntax-type 'here-doc)
2137 (cperl-put-do-not-fontify b (match-beginning 0)))
2138 (t (message "End of here-document `%s' not found." tag))))
2140 ;; 1+5=6 extra () before this:
2141 ;; "^[ \t]*format[ \t]*\\([a-zA-Z0-9_]+\\)?[ \t]*=[ \t]*$")))
2143 name (if (match-beginning 7) ; 6 + 1
2144 (buffer-substring (match-beginning 7) ; 6 + 1
2145 (match-end 7)) ; 6 + 1
2148 (if cperl-pod-here-fontify
2149 (while (and (eq (forward-line) 0)
2150 (not (looking-at "^[.;]$")))
2152 ((looking-at "^#")) ; Skip comments
2153 ((and argument ; Skip argument multi-lines
2154 (looking-at "^[ \t]*{"))
2156 (setq argument nil))
2157 (argument ; Skip argument lines
2158 (setq argument nil))
2161 (setq argument (looking-at "^[^\n]*[@^]"))
2163 (put-text-property b1 (point)
2164 'face font-lock-string-face)
2165 (cperl-put-do-not-fontify b1 (point)))))
2166 (re-search-forward (concat "^[.;]$") max 'toend))
2168 (if (looking-at "^[.;]$")
2170 (put-text-property (point) (+ (point) 2)
2171 'face font-lock-string-face)
2172 (cperl-put-do-not-fontify (point) (+ (point) 2)))
2173 (message "End of format `%s' not found." name))
2175 (put-text-property b (point) 'syntax-type 'format)
2176 ;;; (cond ((re-search-forward (concat "^[.;]$") max 'toend)
2177 ;;; (if cperl-pod-here-fontify
2179 ;;; (put-text-property b (match-end 0)
2180 ;;; 'face font-lock-string-face)
2181 ;;; (cperl-put-do-not-fontify b (match-end 0))))
2182 ;;; (put-text-property b (match-end 0)
2183 ;;; 'syntax-type 'format)
2184 ;;; (cperl-put-do-not-fontify b (match-beginning 0)))
2185 ;;; (t (message "End of format `%s' not found." name)))
2187 ;;; (while (re-search-forward "\\(\\`\n?\\|\n\n\\)=" max t)
2188 ;;; (if (looking-at "\n*cut\\>")
2190 ;;; (message "=cut is not preceeded by a pod section")
2191 ;;; (setq err (point)))
2192 ;;; (beginning-of-line)
2194 ;;; (setq b (point) bb b)
2195 ;;; (or (re-search-forward "\n\n=cut\\>" max 'toend)
2196 ;;; (message "Cannot find the end of a pod section"))
2197 ;;; (beginning-of-line 3)
2198 ;;; (setq e (point))
2199 ;;; (put-text-property b e 'in-pod t)
2201 ;;; (while (re-search-forward "\n\n[ \t]" e t)
2202 ;;; (beginning-of-line)
2203 ;;; (put-text-property b (point) 'syntax-type 'pod)
2204 ;;; (cperl-put-do-not-fontify b (point))
2205 ;;; ;;(put-text-property (max (point-min) (1- b))
2206 ;;; ;; (point) cperl-do-not-fontify t)
2207 ;;; (if cperl-pod-here-fontify (put-text-property b (point) 'face face))
2208 ;;; (re-search-forward "\n\n[^ \t\f\n]" e 'toend)
2209 ;;; (beginning-of-line)
2210 ;;; (setq b (point)))
2211 ;;; (put-text-property (point) e 'syntax-type 'pod)
2212 ;;; (cperl-put-do-not-fontify (point) e)
2213 ;;; ;;(put-text-property (max (point-min) (1- (point)))
2214 ;;; ;; e cperl-do-not-fontify t)
2215 ;;; (if cperl-pod-here-fontify
2216 ;;; (progn (put-text-property (point) e 'face face)
2219 ;;; "=[a-zA-Z0-9]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$")
2220 ;;; (put-text-property
2221 ;;; (match-beginning 1) (match-end 1)
2222 ;;; 'face head-face))
2223 ;;; (while (re-search-forward
2224 ;;; ;; One paragraph
2225 ;;; "\n\n=[a-zA-Z0-9]+\\>[ \t]*\\(\\(\n?[^\n]\\)+\\)$"
2227 ;;; (put-text-property
2228 ;;; (match-beginning 1) (match-end 1)
2229 ;;; 'face head-face))))
2232 ;;; (while (re-search-forward
2233 ;;; ;; We exclude \n to avoid misrecognition inside quotes.
2234 ;;; "<<\\(\\([\"'`]\\)\\([^\"'`\n]*\\)\\2\\|\\(\\([a-zA-Z_][a-zA-Z_0-9]*\\)?\\)\\)"
2236 ;;; (if (match-beginning 4)
2237 ;;; (setq b1 (match-beginning 4)
2238 ;;; e1 (match-end 4))
2239 ;;; (setq b1 (match-beginning 3)
2240 ;;; e1 (match-end 3)))
2241 ;;; (setq tag (buffer-substring b1 e1)
2242 ;;; qtag (regexp-quote tag))
2243 ;;; (cond (cperl-pod-here-fontify
2244 ;;; (put-text-property b1 e1 'face font-lock-reference-face)
2245 ;;; (cperl-put-do-not-fontify b1 e1)))
2247 ;;; (setq b (point))
2248 ;;; (cond ((re-search-forward (concat "^" qtag "$") max 'toend)
2249 ;;; (if cperl-pod-here-fontify
2251 ;;; (put-text-property (match-beginning 0) (match-end 0)
2252 ;;; 'face font-lock-reference-face)
2253 ;;; (cperl-put-do-not-fontify b (match-end 0))
2254 ;;; ;;(put-text-property (max (point-min) (1- b))
2255 ;;; ;; (min (point-max)
2256 ;;; ;; (1+ (match-end 0)))
2257 ;;; ;; cperl-do-not-fontify t)
2258 ;;; (put-text-property b (match-beginning 0)
2259 ;;; 'face here-face)))
2260 ;;; (put-text-property b (match-beginning 0)
2261 ;;; 'syntax-type 'here-doc)
2262 ;;; (cperl-put-do-not-fontify b (match-beginning 0)))
2263 ;;; (t (message "End of here-document `%s' not found." tag))))
2265 ;;; (while (re-search-forward
2266 ;;; "^[ \t]*format[ \t]*\\(\\([a-zA-Z0-9_]+[ \t]*\\)?\\)=[ \t]*$"
2269 ;;; name (buffer-substring (match-beginning 1)
2271 ;;; (cond ((re-search-forward (concat "^[.;]$") max 'toend)
2272 ;;; (if cperl-pod-here-fontify
2274 ;;; (put-text-property b (match-end 0)
2275 ;;; 'face font-lock-string-face)
2276 ;;; (cperl-put-do-not-fontify b (match-end 0))))
2277 ;;; (put-text-property b (match-end 0)
2278 ;;; 'syntax-type 'format)
2279 ;;; (cperl-put-do-not-fontify b (match-beginning 0)))
2280 ;;; (t (message "End of format `%s' not found." name))))
2282 (if err (goto-char err)
2283 (message "Scan for pods, formats and here-docs completed.")))
2284 (and (buffer-modified-p)
2286 (set-buffer-modified-p nil)))))
2288 (defun cperl-backward-to-noncomment (lim)
2289 ;; Stops at lim or after non-whitespace that is not in comment
2291 (while (and (not stop) (> (point) (or lim 1)))
2292 (skip-chars-backward " \t\n\f" lim)
2295 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
2297 (cperl-to-comment-or-eol)
2298 (skip-chars-backward " \t")
2299 (if (< p (point)) (goto-char p))
2302 (defun cperl-after-expr-p (&optional lim chars test)
2303 "Returns true if the position is good for start of expression.
2304 TEST is the expression to evaluate at the found position. If absent,
2305 CHARS is a string that contains good characters to have before us."
2308 (while (and (not stop) (> (point) (or lim 1)))
2309 (skip-chars-backward " \t\n\f" lim)
2312 (if (looking-at "^[ \t]*\\(#\\|$\\)") nil ; Only comment, skip
2313 ;; Else: last iteration (What to do with labels?)
2314 (cperl-to-comment-or-eol)
2315 (skip-chars-backward " \t")
2316 (if (< p (point)) (goto-char p))
2321 (if test (eval test)
2322 (memq (following-char) (append (or chars "{};") nil))))))))
2324 (defun cperl-backward-to-start-of-continued-exp (lim)
2325 (if (memq (preceding-char) (append ")]}\"'`" nil))
2328 (if (<= (point) lim)
2329 (goto-char (1+ lim)))
2330 (skip-chars-forward " \t"))
2333 (defvar innerloop-done nil)
2334 (defvar last-depth nil)
2336 (defun cperl-indent-exp ()
2337 "Simple variant of indentation of continued-sexp.
2338 Should be slow. Will not indent comment if it starts at `comment-indent'
2339 or looks like continuation of the comment on the previous line."
2342 (let ((tmp-end (progn (end-of-line) (point))) top done)
2347 (while (= (nth 0 (parse-partial-sexp (point) tmp-end
2349 (setq top (point))) ; Get the outermost parenths in line
2351 (while (< (point) tmp-end)
2352 (parse-partial-sexp (point) tmp-end nil t) ; To start-sexp or eol
2353 (or (eolp) (forward-sexp 1)))
2354 (if (> (point) tmp-end) (progn (end-of-line) (setq tmp-end (point)))
2357 (setq tmp-end (point-marker)))
2358 (cperl-indent-region (point) tmp-end))))
2360 (defun cperl-indent-region (start end)
2361 "Simple variant of indentation of region in CPerl mode.
2362 Should be slow. Will not indent comment if it starts at `comment-indent'
2363 or looks like continuation of the comment on the previous line.
2364 Indents all the lines whose first character is between START and END
2368 (let (st comm indent-info old-comm-indent new-comm-indent
2369 (pm 0) (imenu-scanning-message "Indenting... (%3d%%)"))
2371 (setq old-comm-indent (and (cperl-to-comment-or-eol)
2373 new-comm-indent old-comm-indent)
2375 (or (bolp) (beginning-of-line 2))
2376 (or (fboundp 'imenu-progress-message)
2377 (message "Indenting... For feedback load `imenu'..."))
2378 (while (and (<= (point) end) (not (eobp))) ; bol to check start
2379 (and (fboundp 'imenu-progress-message)
2380 (imenu-progress-message
2381 pm (/ (* 100 (- (point) start)) (- end start -1))))
2384 ) ; Believe indentation of the current
2385 (if (and (setq comm (looking-at "[ \t]*#"))
2386 (or (eq (current-indentation) (or old-comm-indent
2388 (setq old-comm-indent nil)))
2389 (if (and old-comm-indent
2390 (= (current-indentation) old-comm-indent))
2391 (let ((comment-column new-comm-indent))
2392 (indent-for-comment)))
2394 (cperl-indent-line 'indent-info)
2397 (if (setq old-comm-indent (and (cperl-to-comment-or-eol)
2399 (progn (indent-for-comment)
2400 (skip-chars-backward " \t")
2401 (skip-chars-backward "#")
2402 (setq new-comm-indent (current-column))))))))
2403 (beginning-of-line 2))
2404 (if (fboundp 'imenu-progress-message)
2405 (imenu-progress-message pm 100)
2408 (defun cperl-slash-is-regexp (&optional pos)
2410 (goto-char (if pos pos (1- (point))))
2412 (not (memq (get-text-property (point) 'face)
2413 '(font-lock-string-face font-lock-comment-face)))
2414 (cperl-after-expr-p nil nil '
2415 (or (looking-at "[^]a-zA-Z0-9_)}]")
2416 (eq (get-text-property (point) 'face)
2417 'font-lock-keyword-face))))))
2419 ;; Stolen from lisp-mode with a lot of improvements
2421 (defun cperl-fill-paragraph (&optional justify iteration)
2422 "Like \\[fill-paragraph], but handle CPerl comments.
2423 If any of the current line is a comment, fill the comment or the
2424 block of it that point is in, preserving the comment's initial
2425 indentation and initial hashes. Behaves usually outside of comment."
2428 ;; Non-nil if the current line contains a comment.
2431 ;; If has-comment, the appropriate fill-prefix for the comment.
2433 ;; Line that contains code and comment (or nil)
2435 c spaces len dc (comment-column comment-column))
2436 ;; Figure out what kind of comment we are looking at.
2441 ;; A line with nothing but a comment on it?
2442 ((looking-at "[ \t]*#[# \t]*")
2444 comment-fill-prefix (buffer-substring (match-beginning 0)
2447 ;; A line with some code, followed by a comment? Remember that the
2448 ;; semi which starts the comment shouldn't be part of a string or
2450 ((cperl-to-comment-or-eol)
2451 (setq has-comment t)
2452 (looking-at "#+[ \t]*")
2453 (setq start (point) c (current-column)
2455 (concat (make-string (current-column) ?\ )
2456 (buffer-substring (match-beginning 0) (match-end 0)))
2457 spaces (progn (skip-chars-backward " \t")
2458 (buffer-substring (point) start))
2459 dc (- c (current-column)) len (- start (point))
2460 start (point-marker))
2462 (insert (make-string dc ?-)))))
2463 (if (not has-comment)
2464 (fill-paragraph justify) ; Do the usual thing outside of comment
2465 ;; Narrow to include only the comment, and then fill the region.
2468 ;; Find the first line we should include in the region to fill.
2469 (if start (progn (beginning-of-line) (point))
2471 (while (and (zerop (forward-line -1))
2472 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
2473 ;; We may have gone to far. Go forward again.
2474 (or (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")
2477 ;; Find the beginning of the first line past the region to fill.
2479 (while (progn (forward-line 1)
2480 (looking-at "^[ \t]*#+[ \t]*[^ \t\n#]")))
2482 ;; Remove existing hashes
2483 (goto-char (point-min))
2484 (while (progn (forward-line 1) (< (point) (point-max)))
2485 (skip-chars-forward " \t")
2486 (and (looking-at "#+")
2487 (delete-char (- (match-end 0) (match-beginning 0)))))
2489 ;; Lines with only hashes on them can be paragraph boundaries.
2490 (let ((paragraph-start (concat paragraph-start "\\|^[ \t#]*$"))
2491 (paragraph-separate (concat paragraph-start "\\|^[ \t#]*$"))
2492 (fill-prefix comment-fill-prefix))
2493 (fill-paragraph justify)))
2498 (progn (delete-char dc) (insert spaces)))
2499 (if (or (= (current-column) c) iteration) nil
2500 (setq comment-column c)
2501 (indent-for-comment)
2502 ;; Repeat once more, flagging as iteration
2503 (cperl-fill-paragraph justify t)))))))
2505 (defun cperl-do-auto-fill ()
2506 ;; Break out if the line is short enough
2507 (if (> (save-excursion
2511 (let ((c (save-excursion (beginning-of-line)
2512 (cperl-to-comment-or-eol) (point)))
2513 (s (memq (following-char) '(?\ ?\t))) marker)
2514 (if (>= c (point)) nil
2515 (setq marker (point-marker))
2516 (cperl-fill-paragraph)
2518 ;; Is not enough, sometimes marker is a start of line
2519 (if (bolp) (progn (re-search-forward "#+[ \t]*")
2520 (goto-char (match-end 0))))
2521 ;; Following space could have gone:
2522 (if (or (not s) (memq (following-char) '(?\ ?\t))) nil
2525 ;; Previous space could have gone:
2526 (or (memq (preceding-char) '(?\ ?\t)) (insert " "))))))
2528 (defvar imenu-example--function-name-regexp-perl
2529 "^\\([ \t]*\\(sub\\|package\\)[ \t\n]+\\([a-zA-Z_0-9:']+\\)[ \t]*\\|=head\\([12]\\)[ \t]+\\([^\n]+\\)$\\)")
2531 (defun cperl-imenu-addback (lst &optional isback name)
2532 ;; We suppose that the lst is a DAG, unless the first element only
2533 ;; loops back, and ISBACK is set. Thus this function cannot be
2534 ;; applied twice without ISBACK set.
2535 (cond ((not cperl-imenu-addback) lst)
2538 (setq name "+++BACK+++"))
2539 (mapcar (function (lambda (elt)
2540 (if (and (listp elt) (listp (cdr elt)))
2542 ;; In the other order it goes up
2543 ;; one level only ;-(
2544 (setcdr elt (cons (cons name lst)
2546 (cperl-imenu-addback (cdr elt) t name)
2548 (if isback (cdr lst) lst))
2551 (defun imenu-example--create-perl-index (&optional regexp)
2553 (let ((index-alist '()) (index-pack-alist '()) (index-pod-alist '())
2554 (index-unsorted-alist '()) (i-s-f (default-value 'imenu-sort-function))
2555 (index-meth-alist '()) meth
2556 packages ends-ranges p
2557 (prev-pos 0) char fchar index index1 name (end-range 0) package)
2558 (goto-char (point-min))
2559 (imenu-progress-message prev-pos 0)
2560 ;; Search for the function
2561 (progn ;;save-match-data
2562 (while (re-search-forward
2563 (or regexp imenu-example--function-name-regexp-perl)
2565 (imenu-progress-message prev-pos)
2566 ;;(backward-up-list 1)
2568 ((match-beginning 2) ; package or sub
2570 (goto-char (match-beginning 2))
2571 (setq fchar (following-char))
2573 (setq char (following-char) meth nil)
2575 (while (and ends-ranges (>= p (car ends-ranges)))
2576 ;; delete obsolete entries
2577 (setq ends-ranges (cdr ends-ranges) packages (cdr packages)))
2578 (setq package (or (car packages) "")
2579 end-range (or (car ends-ranges) 0))
2581 (setq name (buffer-substring (match-beginning 3) (match-end 3))
2583 (set-text-properties 0 (length name) nil name)
2585 package (concat name "::")
2586 name (concat "package " name)
2589 (parse-partial-sexp (point) (point-max) -1) (point))
2590 ends-ranges (cons end-range ends-ranges)
2591 packages (cons package packages)))
2593 ;; Skip this function name if it is a prototype declaration.
2594 (if (and (eq fchar ?s) (eq char ?\;)) nil
2595 (setq index (imenu-example--name-and-position))
2596 (if (eq fchar ?p) nil
2597 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
2598 (set-text-properties 0 (length name) nil name)
2599 (cond ((string-match "[:']" name)
2601 ((> p end-range) nil)
2603 (setq name (concat package name) meth t))))
2606 (push index index-pack-alist)
2607 (push index index-alist))
2608 (if meth (push index index-meth-alist))
2609 (push index index-unsorted-alist)))
2611 ;; (beginning-of-line)
2612 (setq index (imenu-example--name-and-position)
2613 name (buffer-substring (match-beginning 5) (match-end 5)))
2614 (set-text-properties 0 (length name) nil name)
2615 (if (eq (char-after (match-beginning 4)) ?2)
2616 (setq name (concat " " name)))
2618 (setq index1 (cons (concat "=" name) (cdr index)))
2619 (push index index-pod-alist)
2620 (push index1 index-unsorted-alist)))))
2621 (imenu-progress-message prev-pos 100)
2623 (if (default-value 'imenu-sort-function)
2624 (sort index-alist (default-value 'imenu-sort-function))
2625 (nreverse index-alist)))
2626 (and index-pod-alist
2627 (push (cons "+POD headers+..."
2628 (nreverse index-pod-alist))
2630 (and (or index-pack-alist index-meth-alist)
2631 (let ((lst index-pack-alist) hier-list pack elt group name)
2632 ;; Remove "package ", reverse and uniquify.
2634 (setq elt (car lst) lst (cdr lst) name (substring (car elt) 8))
2635 (if (assoc name hier-list) nil
2636 (setq hier-list (cons (cons name (cdr elt)) hier-list))))
2637 (setq lst index-meth-alist)
2639 (setq elt (car lst) lst (cdr lst))
2640 (cond ((string-match "\\(::\\|'\\)[_a-zA-Z0-9]+$" (car elt))
2641 (setq pack (substring (car elt) 0 (match-beginning 0)))
2642 (if (setq group (assoc pack hier-list))
2643 (if (listp (cdr group))
2644 ;; Have some functions already
2646 (cons (cons (substring
2648 (+ 2 (match-beginning 0)))
2651 (setcdr group (list (cons (substring
2653 (+ 2 (match-beginning 0)))
2657 (list (cons (substring
2659 (+ 2 (match-beginning 0)))
2662 (push (cons "+Hierarchy+..."
2665 (and index-pack-alist
2666 (push (cons "+Packages+..."
2667 (nreverse index-pack-alist))
2669 (and (or index-pack-alist index-pod-alist
2670 (default-value 'imenu-sort-function))
2671 index-unsorted-alist
2672 (push (cons "+Unsorted List+..."
2673 (nreverse index-unsorted-alist))
2675 (cperl-imenu-addback index-alist)))
2677 (defvar cperl-compilation-error-regexp-alist
2678 ;; This look like a paranoiac regexp: could anybody find a better one? (which WORK).
2679 '(("^[^\n]* \\(file\\|at\\) \\([^ \t\n]+\\) [^\n]*line \\([0-9]+\\)[\\., \n]"
2681 "Alist that specifies how to match errors in perl output.")
2683 (if (fboundp 'eval-after-load)
2686 '(setq perl-compilation-error-regexp-alist
2687 cperl-compilation-error-regexp-alist)))
2690 (defvar cperl-faces-init nil)
2692 (defun cperl-windowed-init ()
2693 "Initialization under windowed version."
2694 (add-hook 'font-lock-mode-hook
2698 (eq major-mode 'perl-mode)
2699 (eq major-mode 'cperl-mode))
2701 (or cperl-faces-init (cperl-init-faces))))))))
2703 (defvar perl-font-lock-keywords-1 nil
2704 "Additional expressions to highlight in Perl mode. Minimal set.")
2705 (defvar perl-font-lock-keywords nil
2706 "Additional expressions to highlight in Perl mode. Default set.")
2707 (defvar perl-font-lock-keywords-2 nil
2708 "Additional expressions to highlight in Perl mode. Maximal set")
2710 (defun cperl-init-faces ()
2713 (require 'font-lock)
2714 (and (fboundp 'font-lock-fontify-anchored-keywords)
2715 (featurep 'font-lock-extra)
2716 (message "You have an obsolete package `font-lock-extra'. Install `choose-color'."))
2717 (let (t-font-lock-keywords t-font-lock-keywords-1 font-lock-anchored)
2718 ;;(defvar cperl-font-lock-enhanced nil
2719 ;; "Set to be non-nil if font-lock allows active highlights.")
2720 (if (fboundp 'font-lock-fontify-anchored-keywords)
2721 (setq font-lock-anchored t))
2723 t-font-lock-keywords
2727 "\\(^\\|[^$@%&\\]\\)\\<\\("
2730 '("if" "until" "while" "elsif" "else" "unless" "for"
2731 "foreach" "continue" "exit" "die" "last" "goto" "next"
2732 "redo" "return" "local" "exec" "sub" "do" "dump" "use"
2733 "require" "package" "eval" "my" "BEGIN" "END")
2734 "\\|") ; Flow control
2735 "\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
2736 ; In what follows we use `type' style
2737 ; for overwritable buildins
2740 "\\(^\\|[^$@%&\\]\\)\\<\\("
2741 ;; "CORE" "__FILE__" "__LINE__" "abs" "accept" "alarm" "and" "atan2"
2742 ;; "bind" "binmode" "bless" "caller" "chdir" "chmod" "chown" "chr"
2743 ;; "chroot" "close" "closedir" "cmp" "connect" "continue" "cos"
2744 ;; "crypt" "dbmclose" "dbmopen" "die" "dump" "endgrent" "endhostent"
2745 ;; "endnetent" "endprotoent" "endpwent" "endservent" "eof" "eq" "exec"
2746 ;; "exit" "exp" "fcntl" "fileno" "flock" "fork" "formline" "ge" "getc"
2747 ;; "getgrent" "getgrgid" "getgrnam" "gethostbyaddr" "gethostbyname"
2748 ;; "gethostent" "getlogin" "getnetbyaddr" "getnetbyname" "getnetent"
2749 ;; "getpeername" "getpgrp" "getppid" "getpriority" "getprotobyname"
2750 ;; "getprotobynumber" "getprotoent" "getpwent" "getpwnam" "getpwuid"
2751 ;; "getservbyname" "getservbyport" "getservent" "getsockname"
2752 ;; "getsockopt" "glob" "gmtime" "gt" "hex" "index" "int" "ioctl"
2753 ;; "join" "kill" "lc" "lcfirst" "le" "length" "link" "listen"
2754 ;; "localtime" "log" "lstat" "lt" "mkdir" "msgctl" "msgget" "msgrcv"
2755 ;; "msgsnd" "ne" "not" "oct" "open" "opendir" "or" "ord" "pack" "pipe"
2756 ;; "quotemeta" "rand" "read" "readdir" "readline" "readlink"
2757 ;; "readpipe" "recv" "ref" "rename" "require" "reset" "reverse"
2758 ;; "rewinddir" "rindex" "rmdir" "seek" "seekdir" "select" "semctl"
2759 ;; "semget" "semop" "send" "setgrent" "sethostent" "setnetent"
2760 ;; "setpgrp" "setpriority" "setprotoent" "setpwent" "setservent"
2761 ;; "setsockopt" "shmctl" "shmget" "shmread" "shmwrite" "shutdown"
2762 ;; "sin" "sleep" "socket" "socketpair" "sprintf" "sqrt" "srand" "stat"
2763 ;; "substr" "symlink" "syscall" "sysread" "system" "syswrite" "tell"
2764 ;; "telldir" "time" "times" "truncate" "uc" "ucfirst" "umask" "unlink"
2765 ;; "unpack" "utime" "values" "vec" "wait" "waitpid" "wantarray" "warn"
2766 ;; "write" "x" "xor"
2767 "a\\(bs\\|ccept\\|tan2\\|larm\\|nd\\)\\|"
2768 "b\\(in\\(d\\|mode\\)\\|less\\)\\|"
2769 "c\\(h\\(r\\(\\|oot\\)\\|dir\\|mod\\|own\\)\\|aller\\|rypt\\|"
2770 "lose\\(\\|dir\\)\\|mp\\|o\\(s\\|n\\(tinue\\|nect\\)\\)\\)\\|"
2771 "CORE\\|d\\(ie\\|bm\\(close\\|open\\)\\|ump\\)\\|"
2772 "e\\(x\\(p\\|it\\|ec\\)\\|q\\|nd\\(p\\(rotoent\\|went\\)\\|"
2773 "hostent\\|servent\\|netent\\|grent\\)\\|of\\)\\|"
2774 "f\\(ileno\\|cntl\\|lock\\|or\\(k\\|mline\\)\\)\\|"
2775 "g\\(t\\|lob\\|mtime\\|e\\(\\|t\\(p\\(pid\\|r\\(iority\\|"
2776 "oto\\(byn\\(ame\\|umber\\)\\|ent\\)\\)\\|eername\\|w"
2777 "\\(uid\\|ent\\|nam\\)\\|grp\\)\\|host\\(by\\(addr\\|name\\)\\|"
2778 "ent\\)\\|s\\(erv\\(by\\(port\\|name\\)\\|ent\\)\\|"
2779 "ock\\(name\\|opt\\)\\)\\|c\\|login\\|net\\(by\\(addr\\|name\\)\\|"
2780 "ent\\)\\|gr\\(ent\\|nam\\|gid\\)\\)\\)\\)\\|"
2781 "hex\\|i\\(n\\(t\\|dex\\)\\|octl\\)\\|join\\|kill\\|"
2782 "l\\(i\\(sten\\|nk\\)\\|stat\\|c\\(\\|first\\)\\|t\\|e"
2783 "\\(\\|ngth\\)\\|o\\(caltime\\|g\\)\\)\\|m\\(sg\\(rcv\\|snd\\|"
2784 "ctl\\|get\\)\\|kdir\\)\\|n\\(e\\|ot\\)\\|o\\(pen\\(\\|dir\\)\\|"
2785 "r\\(\\|d\\)\\|ct\\)\\|p\\(ipe\\|ack\\)\\|quotemeta\\|"
2786 "r\\(index\\|and\\|mdir\\|e\\(quire\\|ad\\(pipe\\|\\|lin"
2787 "\\(k\\|e\\)\\|dir\\)\\|set\\|cv\\|verse\\|f\\|winddir\\|name"
2788 "\\)\\)\\|s\\(printf\\|qrt\\|rand\\|tat\\|ubstr\\|e\\(t\\(p\\(r"
2789 "\\(iority\\|otoent\\)\\|went\\|grp\\)\\|hostent\\|s\\(ervent\\|"
2790 "ockopt\\)\\|netent\\|grent\\)\\|ek\\(\\|dir\\)\\|lect\\|"
2791 "m\\(ctl\\|op\\|get\\)\\|nd\\)\\|h\\(utdown\\|m\\(read\\|ctl\\|"
2792 "write\\|get\\)\\)\\|y\\(s\\(read\\|call\\|tem\\|write\\)\\|"
2793 "mlink\\)\\|in\\|leep\\|ocket\\(pair\\|\\)\\)\\|t\\(runcate\\|"
2794 "ell\\(\\|dir\\)\\|ime\\(\\|s\\)\\)\\|u\\(c\\(\\|first\\)\\|"
2795 "time\\|mask\\|n\\(pack\\|link\\)\\)\\|v\\(alues\\|ec\\)\\|"
2796 "w\\(a\\(rn\\|it\\(pid\\|\\)\\|ntarray\\)\\|rite\\)\\|"
2797 "x\\(\\|or\\)\\|__\\(FILE__\\|LINE__\\)"
2798 "\\)\\>") 2 'font-lock-type-face)
2799 ;; In what follows we use `other' style
2800 ;; for nonoverwritable buildins
2801 ;; Somehow 's', 'm' are not autogenerated???
2804 "\\(^\\|[^$@%&\\]\\)\\<\\("
2805 ;; "AUTOLOAD" "BEGIN" "DESTROY" "END" "__END__" "chomp" "chop"
2806 ;; "defined" "delete" "do" "each" "else" "elsif" "eval" "exists" "for"
2807 ;; "foreach" "format" "goto" "grep" "if" "keys" "last" "local" "map"
2808 ;; "my" "next" "no" "package" "pop" "pos" "print" "printf" "push" "q"
2809 ;; "qq" "qw" "qx" "redo" "return" "scalar" "shift" "sort" "splice"
2810 ;; "split" "study" "sub" "tie" "tr" "undef" "unless" "unshift" "untie"
2811 ;; "until" "use" "while" "y"
2812 "AUTOLOAD\\|BEGIN\\|cho\\(p\\|mp\\)\\|d\\(e\\(fined\\|lete\\)\\|"
2813 "o\\)\\|DESTROY\\|e\\(ach\\|val\\|xists\\|ls\\(e\\|if\\)\\)\\|"
2814 "END\\|for\\(\\|each\\|mat\\)\\|g\\(rep\\|oto\\)\\|if\\|keys\\|"
2815 "l\\(ast\\|ocal\\)\\|m\\(ap\\|y\\)\\|n\\(ext\\|o\\)\\|"
2816 "p\\(ackage\\|rint\\(\\|f\\)\\|ush\\|o\\(p\\|s\\)\\)\\|"
2817 "q\\(\\|q\\|w\\|x\\)\\|re\\(turn\\|do\\)\\|s\\(pli\\(ce\\|t\\)\\|"
2818 "calar\\|tudy\\|ub\\|hift\\|ort\\)\\|t\\(r\\|ie\\)\\|"
2819 "u\\(se\\|n\\(shift\\|ti\\(l\\|e\\)\\|def\\|less\\)\\)\\|"
2820 "while\\|y\\|__\\(END\\|DATA\\)__" ;__DATA__ added manually
2821 "\\|[sm]" ; Added manually
2822 "\\)\\>") 2 'font-lock-other-type-face)
2823 ;; (mapconcat 'identity
2824 ;; '("#endif" "#else" "#ifdef" "#ifndef" "#if"
2825 ;; "#include" "#define" "#undef")
2827 '("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
2828 font-lock-function-name-face) ; Not very good, triggers at "[a-z]"
2829 '("\\<sub[ \t]+\\([^ \t{;]+\\)[ \t]*[{\n]" 1
2830 font-lock-function-name-face)
2831 '("\\<\\(package\\|require\\|use\\|import\\|no\\|bootstrap\\)[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t;]" ; require A if B;
2832 2 font-lock-function-name-face)
2833 '("^[ \t]*format[ \t]+\\([a-zA-z_][a-zA-z_0-9:]*\\)[ \t]*=[ \t]*$"
2834 1 font-lock-function-name-face)
2835 (cond ((featurep 'font-lock-extra)
2836 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
2837 (2 font-lock-string-face t)
2838 (0 '(restart 2 t)))) ; To highlight $a{bc}{ef}
2840 '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
2841 (2 font-lock-string-face t)
2842 ("\\=[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
2844 (1 font-lock-string-face t))))
2845 (t '("\\([]}\\\\%@>*&]\\|\\$[a-zA-Z0-9_:]*\\)[ \t]*{[ \t]*\\(-?[a-zA-Z0-9_:]+\\)[ \t]*}"
2846 2 font-lock-string-face t)))
2847 '("[ \t{,(]\\(-?[a-zA-Z0-9_:]+\\)[ \t]*=>" 1
2848 font-lock-string-face t)
2849 '("^[ \t]*\\([a-zA-Z0-9_]+[ \t]*:\\)[ \t]*\\($\\|{\\|\\<\\(until\\|while\\|for\\(each\\)?\\|do\\)\\>\\)" 1
2850 font-lock-reference-face) ; labels
2851 '("\\<\\(continue\\|next\\|last\\|redo\\|goto\\)\\>[ \t]+\\([a-zA-Z0-9_:]+\\)" ; labels as targets
2852 2 font-lock-reference-face)
2853 (cond ((featurep 'font-lock-extra)
2854 '("^[ \t]*\\(my\\|local\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
2855 (3 font-lock-variable-name-face)
2857 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)\\([ \t]*,\\)?"
2858 (1 font-lock-variable-name-face)
2859 (2 '(restart 2 nil) nil t)))
2860 nil t))) ; local variables, multiple
2862 '("^[ \t]*\\(my\\|local\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
2863 (3 font-lock-variable-name-face)
2864 ("\\=[ \t]*,[ \t]*\\([$@%*][a-zA-Z0-9_:]+\\)"
2866 (1 font-lock-variable-name-face))))
2867 (t '("^[ \t]*\\(my\\|local\\)[ \t]*\\(([ \t]*\\)?\\([$@%*][a-zA-Z0-9_:]+\\)"
2868 3 font-lock-variable-name-face)))
2869 '("\\<for\\(each\\)?[ \t]*\\(\\$[a-zA-Z_][a-zA-Z_0-9]*\\)[ \t]*("
2870 2 font-lock-variable-name-face)))
2872 t-font-lock-keywords-1
2873 (and (fboundp 'turn-on-font-lock) ; Check for newer font-lock
2874 (not (cperl-xemacs-p)) ; not yet as of XEmacs 19.12
2875 '(("\\(\\([$@]+\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)[ \t]*\\([[{]\\)"
2877 (if (= (- (match-end 2) (match-beginning 2)) 1)
2878 (if (eq (char-after (match-beginning 3)) ?{)
2879 font-lock-other-emphasized-face
2880 font-lock-emphasized-face) ; arrays and hashes
2881 font-lock-variable-name-face) ; Just to put something
2883 ("\\(\\([@%]\\|\$#\\)[a-zA-Z_:][a-zA-Z0-9_:]*\\)" 1
2884 (if (eq (char-after (match-beginning 2)) ?%)
2885 font-lock-other-emphasized-face
2886 font-lock-emphasized-face)
2887 t) ; arrays and hashes
2888 ;;("\\([smy]\\|tr\\)\\([^a-z_A-Z0-9]\\)\\(\\([^\n\\]*||\\)\\)\\2")
2889 ;;; Too much noise from \s* @s[ and friends
2890 ;;("\\(\\<\\([msy]\\|tr\\)[ \t]*\\([^ \t\na-zA-Z0-9_]\\)\\|\\(/\\)\\)"
2891 ;;(3 font-lock-function-name-face t t)
2893 ;; (if (cperl-slash-is-regexp)
2894 ;; font-lock-function-name-face 'default) nil t))
2896 (setq perl-font-lock-keywords-1 t-font-lock-keywords
2897 perl-font-lock-keywords perl-font-lock-keywords-1
2898 perl-font-lock-keywords-2 (append
2899 t-font-lock-keywords
2900 t-font-lock-keywords-1)))
2901 (if (fboundp 'ps-print-buffer) (cperl-ps-print-init))
2902 (if (or (featurep 'choose-color) (featurep 'font-lock-extra))
2903 (font-lock-require-faces
2905 ;; Color-light Color-dark Gray-light Gray-dark Mono
2906 (list 'font-lock-comment-face
2907 ["Firebrick" "OrangeRed" "DimGray" "Gray80"]
2912 (list 'font-lock-string-face
2913 ["RosyBrown" "LightSalmon" "Gray50" "LightGray"]
2918 (list 'font-lock-keyword-face
2919 ["Purple" "LightSteelBlue" "DimGray" "Gray90"]
2924 (list 'font-lock-function-name-face
2926 "Blue" "LightSkyBlue" "Gray50" "LightGray"
2927 (cdr (assq 'background-color ; if mono
2928 (frame-parameters))))
2931 (cdr (assq 'foreground-color ; if mono
2932 (frame-parameters))))
2936 (list 'font-lock-variable-name-face
2937 ["DarkGoldenrod" "LightGoldenrod" "DimGray" "Gray90"]
2942 (list 'font-lock-type-face
2943 ["DarkOliveGreen" "PaleGreen" "DimGray" "Gray80"]
2949 (list 'font-lock-reference-face
2950 ["CadetBlue" "Aquamarine" "Gray50" "LightGray"]
2956 (list 'font-lock-other-type-face
2957 ["chartreuse3" ("orchid1" "orange")
2964 (list 'font-lock-emphasized-face
2965 ["blue" "yellow" nil "Gray80"]
2966 ["lightyellow2" ("navy" "os2blue" "darkgreen")
2971 (list 'font-lock-other-emphasized-face
2972 ["red" "red" nil "Gray80"]
2973 ["lightyellow2" ("navy" "os2blue" "darkgreen")
2978 (defvar cperl-guessed-background nil
2979 "Display characteristics as guessed by cperl.")
2980 (or (fboundp 'x-color-defined-p)
2981 (defalias 'x-color-defined-p
2982 (cond ((fboundp 'color-defined-p) 'color-defined-p)
2984 ((fboundp 'valid-color-name-p) 'valid-color-name-p)
2986 (t 'x-valid-color-name-p))))
2987 (defvar font-lock-reference-face 'font-lock-reference-face)
2988 (defvar font-lock-variable-name-face 'font-lock-variable-name-face)
2989 (or (boundp 'font-lock-type-face)
2990 (defconst font-lock-type-face
2991 'font-lock-type-face
2992 "Face to use for data types.")
2994 (or (boundp 'font-lock-other-type-face)
2995 (defconst font-lock-other-type-face
2996 'font-lock-other-type-face
2997 "Face to use for data types from another group.")
2999 (if (not (cperl-xemacs-p)) nil
3000 (or (boundp 'font-lock-comment-face)
3001 (defconst font-lock-comment-face
3002 'font-lock-comment-face
3003 "Face to use for comments.")
3005 (or (boundp 'font-lock-keyword-face)
3006 (defconst font-lock-keyword-face
3007 'font-lock-keyword-face
3008 "Face to use for keywords.")
3010 (or (boundp 'font-lock-function-name-face)
3011 (defconst font-lock-function-name-face
3012 'font-lock-function-name-face
3013 "Face to use for function names.")
3016 ;;(if (featurep 'font-lock)
3017 (if (face-equal font-lock-type-face font-lock-comment-face)
3018 (defconst font-lock-type-face
3019 'font-lock-type-face
3020 "Face to use for basic data types.")
3022 ;;; (if (fboundp 'eval-after-load)
3023 ;;; (eval-after-load "font-lock"
3024 ;;; '(if (face-equal font-lock-type-face
3025 ;;; font-lock-comment-face)
3026 ;;; (defconst font-lock-type-face
3027 ;;; 'font-lock-type-face
3028 ;;; "Face to use for basic data types.")
3029 ;;; ))) ; This does not work :-( Why?!
3030 ;;; ; Workaround: added to font-lock-m-h
3032 (or (boundp 'font-lock-other-emphasized-face)
3033 (defconst font-lock-other-emphasized-face
3034 'font-lock-other-emphasized-face
3035 "Face to use for another type of emphasizing.")
3037 (or (boundp 'font-lock-emphasized-face)
3038 (defconst font-lock-emphasized-face
3039 'font-lock-emphasized-face
3040 "Face to use for emphasizing.")
3042 ;; Here we try to guess background
3044 (if (boundp 'font-lock-background-mode)
3045 font-lock-background-mode
3047 (face-list (and (fboundp 'face-list) (face-list)))
3050 (cond ((fboundp 'find-face)
3051 (symbol-function 'find-face))
3053 (function (lambda (face) (member face face-list))))
3055 (function (lambda (face) (boundp face))))))
3056 (defvar cperl-guessed-background
3057 (if (and (boundp 'font-lock-display-type)
3058 (eq font-lock-display-type 'grayscale))
3061 "Background as guessed by CPerl mode")
3062 (if (is-face 'font-lock-type-face) nil
3063 (copy-face 'default 'font-lock-type-face)
3065 ((eq background 'light)
3066 (set-face-foreground 'font-lock-type-face
3067 (if (x-color-defined-p "seagreen")
3070 ((eq background 'dark)
3071 (set-face-foreground 'font-lock-type-face
3072 (if (x-color-defined-p "os2pink")
3076 (set-face-background 'font-lock-type-face "gray90"))))
3077 (if (is-face 'font-lock-other-type-face)
3079 (copy-face 'font-lock-type-face 'font-lock-other-type-face)
3081 ((eq background 'light)
3082 (set-face-foreground 'font-lock-other-type-face
3083 (if (x-color-defined-p "chartreuse3")
3086 ((eq background 'dark)
3087 (set-face-foreground 'font-lock-other-type-face
3088 (if (x-color-defined-p "orchid1")
3091 (if (is-face 'font-lock-other-emphasized-face) nil
3092 (copy-face 'bold-italic 'font-lock-other-emphasized-face)
3094 ((eq background 'light)
3095 (set-face-background 'font-lock-other-emphasized-face
3096 (if (x-color-defined-p "lightyellow2")
3098 (if (x-color-defined-p "lightyellow")
3101 ((eq background 'dark)
3102 (set-face-background 'font-lock-other-emphasized-face
3103 (if (x-color-defined-p "navy")
3105 (if (x-color-defined-p "darkgreen")
3108 (t (set-face-background 'font-lock-other-emphasized-face "gray90"))))
3109 (if (is-face 'font-lock-emphasized-face) nil
3110 (copy-face 'bold 'font-lock-emphasized-face)
3112 ((eq background 'light)
3113 (set-face-background 'font-lock-emphasized-face
3114 (if (x-color-defined-p "lightyellow2")
3117 ((eq background 'dark)
3118 (set-face-background 'font-lock-emphasized-face
3119 (if (x-color-defined-p "navy")
3121 (if (x-color-defined-p "darkgreen")
3124 (t (set-face-background 'font-lock-emphasized-face "gray90"))))
3125 (if (is-face 'font-lock-variable-name-face) nil
3126 (copy-face 'italic 'font-lock-variable-name-face))
3127 (if (is-face 'font-lock-reference-face) nil
3128 (copy-face 'italic 'font-lock-reference-face))))
3129 (setq cperl-faces-init t))
3133 (defun cperl-ps-print-init ()
3134 "Initialization of `ps-print' components for faces used in CPerl."
3135 ;; Guard against old versions
3136 (defvar ps-underlined-faces nil)
3137 (defvar ps-bold-faces nil)
3138 (defvar ps-italic-faces nil)
3140 (append '(font-lock-emphasized-face
3141 font-lock-keyword-face
3142 font-lock-variable-name-face
3143 font-lock-reference-face
3144 font-lock-other-emphasized-face)
3146 (setq ps-italic-faces
3147 (append '(font-lock-other-type-face
3148 font-lock-reference-face
3149 font-lock-other-emphasized-face)
3151 (setq ps-underlined-faces
3152 (append '(font-lock-emphasized-face
3153 font-lock-other-emphasized-face
3154 font-lock-other-type-face font-lock-type-face)
3155 ps-underlined-faces))
3156 (cons 'font-lock-type-face ps-underlined-faces))
3159 (if (cperl-enable-font-lock) (cperl-windowed-init))
3161 (defun cperl-set-style (style)
3162 "Set CPerl-mode variables to use one of several different indentation styles.
3163 The arguments are a string representing the desired style.
3164 Available styles are GNU, K&R, BSD and Whitesmith."
3166 (let ((list (mapcar (function (lambda (elt) (list (car elt))))
3168 (list (completing-read "Enter style: " list nil 'insist))))
3169 (let ((style (cdr (assoc style c-style-alist))) setting str sym)
3171 (setq setting (car style) style (cdr style))
3172 (setq str (symbol-name (car setting)))
3173 (and (string-match "^c-" str)
3174 (setq str (concat "cperl-" (substring str 2)))
3175 (setq sym (intern-soft str))
3177 (set sym (cdr setting))))))
3179 (defun cperl-check-syntax ()
3181 (require 'mode-compile)
3182 (let ((perl-dbg-flags "-wc"))
3185 (defun cperl-info-buffer ()
3186 ;; Returns buffer with documentation. Creats if missing
3187 (let ((info (get-buffer "*info-perl*")))
3189 (save-window-excursion
3192 (save-window-excursion
3194 (Info-find-node cperl-info-page "perlfunc")
3195 (set-buffer "*info*")
3196 (rename-buffer "*info-perl*")
3197 (current-buffer)))))
3199 (defun cperl-word-at-point (&optional p)
3200 ;; Returns the word at point or at P.
3202 (if p (goto-char p))
3204 (funcall (or (and (boundp 'find-tag-default-function)
3205 find-tag-default-function)
3206 (get major-mode 'find-tag-default-function)
3207 ;; XEmacs 19.12 has `find-tag-default-hook'; it is
3208 ;; automatically used within `find-tag-default':
3209 'find-tag-default))))
3211 (defun cperl-info-on-command (command)
3212 "Shows documentation for Perl command in other window."
3214 (let* ((default (cperl-word-at-point))
3216 (format "Find doc for Perl function (default %s): "
3218 (list (if (equal read "")
3222 (let ((buffer (current-buffer))
3223 (cmd-desc (concat "^" (regexp-quote command) "[^a-zA-Z_0-9]")) ; "tr///"
3225 (if (string-match "^-[a-zA-Z]$" command)
3226 (setq cmd-desc "^-X[ \t\n]"))
3227 (set-buffer (cperl-info-buffer))
3228 (beginning-of-buffer)
3229 (re-search-forward "^-X[ \t\n]")
3231 (if (re-search-forward cmd-desc nil t)
3233 (setq pos (progn (beginning-of-line)
3235 (pop-to-buffer (cperl-info-buffer))
3236 (set-window-start (selected-window) pos))
3237 (message "No entry for %s found." command))
3238 (pop-to-buffer buffer)))
3240 (defun cperl-info-on-current-command ()
3241 "Shows documentation for Perl command at point in other window."
3243 (cperl-info-on-command (cperl-word-at-point)))
3245 (defun cperl-imenu-info-imenu-search ()
3246 (if (looking-at "^-X[ \t\n]") nil
3248 "^\n\\([-a-zA-Z]+\\)[ \t\n]")
3251 (defun cperl-imenu-info-imenu-name ()
3253 (match-beginning 1) (match-end 1)))
3255 (defun cperl-imenu-on-info ()
3257 (let* ((buffer (current-buffer))
3258 imenu-create-index-function
3259 imenu-prev-index-position-function
3260 imenu-extract-index-name-function
3261 (index-item (save-restriction
3262 (save-window-excursion
3263 (set-buffer (cperl-info-buffer))
3264 (setq imenu-create-index-function
3265 'imenu-default-create-index-function
3266 imenu-prev-index-position-function
3267 'cperl-imenu-info-imenu-search
3268 imenu-extract-index-name-function
3269 'cperl-imenu-info-imenu-name)
3270 (imenu-choose-buffer-index)))))
3274 (pop-to-buffer "*info-perl*")
3276 ((markerp (cdr index-item))
3277 (goto-char (marker-position (cdr index-item))))
3279 (goto-char (cdr index-item))))
3280 (set-window-start (selected-window) (point))
3281 (pop-to-buffer buffer)))))
3283 (defun cperl-lineup (beg end &optional step minshift)
3284 "Lineup construction in a region.
3285 Beginning of region should be at the start of a construction.
3286 All first occurences of this construction in the lines that are
3287 partially contained in the region are lined up at the same column.
3289 MINSHIFT is the minimal amount of space to insert before the construction.
3290 STEP is the tabwidth to position constructions.
3291 If STEP is `nil', `cperl-lineup-step' will be used
3292 \(or `cperl-indent-level', if `cperl-lineup-step' is `nil').
3293 Will not move the position at the start to the left."
3295 (let (search col tcol seen b e)
3299 (setq end (point-marker))
3301 (skip-chars-forward " \t\f")
3302 (setq beg (point-marker))
3303 (indent-region beg end nil)
3305 (setq col (current-column))
3306 (if (looking-at "[a-zA-Z0-9_]")
3307 (if (looking-at "\\<[a-zA-Z0-9_]+\\>")
3311 (buffer-substring (match-beginning 0)
3312 (match-end 0))) "\\>"))
3313 (error "Cannot line up in a middle of the word"))
3314 (if (looking-at "$")
3315 (error "Cannot line up end of line"))
3316 (setq search (regexp-quote (char-to-string (following-char)))))
3317 (setq step (or step cperl-lineup-step cperl-indent-level))
3318 (or minshift (setq minshift 1))
3320 (beginning-of-line 2)
3321 (and (< (point) end)
3322 (re-search-forward search end t)
3323 (goto-char (match-beginning 0))))
3324 (setq tcol (current-column) seen t)
3325 (if (> tcol col) (setq col tcol)))
3327 (error "The construction to line up occured only once"))
3329 (setq col (+ col minshift))
3330 (if (/= (% col step) 0) (setq step (* step (1+ (/ col step)))))
3334 (skip-chars-backward " \t")
3335 (delete-region (point) e)
3336 (indent-to-column col); (make-string (- col (current-column)) ?\ ))
3337 (beginning-of-line 2)
3338 (and (< (point) end)
3339 (re-search-forward search end t)
3340 (goto-char (match-beginning 0)))))))) ; No body
3342 (defun cperl-etags (&optional add all files)
3343 "Run etags with appropriate options for Perl files.
3344 If optional argument ALL is `recursive', will process Perl files
3345 in subdirectories too."
3348 (args '("-l" "none" "-r" "/\\<\\(package\\|sub\\)[ \\t]+\\(\\([a-zA-Z0-9:_]*::\\)?\\([a-zA-Z0-9_]+\\)[ \\t]*\\([{#]\\|$\\)\\)/\\4/"))
3350 (if add (setq args (cons "-a" args)))
3351 (or files (setq files (list buffer-file-name)))
3353 ((eq all 'recursive)
3354 ;;(error "Not implemented: recursive")
3355 (setq args (append (list "-e"
3356 "sub wanted {push @ARGV, $File::Find::name if /\\.[Pp][Llm]$/}
3358 find(\\&wanted, '.');
3363 ;;(error "Not implemented: all")
3364 (setq args (append (list "-e"
3365 "push @ARGV, <*.PL *.pl *.pm>;
3370 (setq args (append args files))))
3371 (setq res (apply 'call-process cmd nil nil nil args))
3373 (message "etags returned \"%s\"" res))))
3375 (defun cperl-toggle-auto-newline ()
3376 "Toggle the state of `cperl-auto-newline'."
3378 (setq cperl-auto-newline (not cperl-auto-newline))
3379 (message "Newlines will %sbe auto-inserted now."
3380 (if cperl-auto-newline "" "not ")))
3382 (defun cperl-toggle-abbrev ()
3383 "Toggle the state of automatic keyword expansion in CPerl mode."
3385 (abbrev-mode (if abbrev-mode 0 1))
3386 (message "Perl control structure will %sbe auto-inserted now."
3387 (if abbrev-mode "" "not ")))
3390 (defun cperl-toggle-electric ()
3391 "Toggle the state of parentheses doubling in CPerl mode."
3393 (setq cperl-electric-parens (if (cperl-val 'cperl-electric-parens) 'null t))
3394 (message "Parentheses will %sbe auto-doubled now."
3395 (if (cperl-val 'cperl-electric-parens) "" "not ")))
3397 ;;;; Tags file creation.
3399 (defvar cperl-tmp-buffer " *cperl-tmp*")
3401 (defun cperl-setup-tmp-buf ()
3402 (set-buffer (get-buffer-create cperl-tmp-buffer))
3403 (set-syntax-table cperl-mode-syntax-table)
3404 (buffer-disable-undo)
3407 (defun cperl-xsub-scan ()
3410 (let ((index-alist '())
3411 (prev-pos 0) index index1 name package prefix)
3412 (goto-char (point-min))
3413 (imenu-progress-message prev-pos 0)
3414 ;; Search for the function
3415 (progn ;;save-match-data
3416 (while (re-search-forward
3417 "^\\([ \t]*MODULE\\>[^\n]*\\<PACKAGE[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9:]*\\)\\>\\|\\([a-zA-Z_][a-zA-Z_0-9]*\\)(\\|[ \t]*BOOT:\\)"
3419 (imenu-progress-message prev-pos)
3421 ((match-beginning 2) ; SECTION
3422 (setq package (buffer-substring (match-beginning 2) (match-end 2)))
3423 (goto-char (match-beginning 0))
3424 (skip-chars-forward " \t")
3426 (if (looking-at "[^\n]*\\<PREFIX[ \t]*=[ \t]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\>")
3427 (setq prefix (buffer-substring (match-beginning 1) (match-end 1)))
3429 ((not package) nil) ; C language section
3430 ((match-beginning 3) ; XSUB
3431 (goto-char (1+ (match-beginning 3)))
3432 (setq index (imenu-example--name-and-position))
3433 (setq name (buffer-substring (match-beginning 3) (match-end 3)))
3434 (if (and prefix (string-match (concat "^" prefix) name))
3435 (setq name (substring name (length prefix))))
3436 (cond ((string-match "::" name) nil)
3438 (setq index1 (cons (concat package "::" name) (cdr index)))
3439 (push index1 index-alist)))
3441 (push index index-alist))
3443 ;; (beginning-of-line)
3444 (setq index (imenu-example--name-and-position))
3445 (setcar index (concat package "::BOOT:"))
3446 (push index index-alist)))))
3447 (imenu-progress-message prev-pos 100)
3449 ;; (if (default-value 'imenu-sort-function)
3450 ;; (sort index-alist (default-value 'imenu-sort-function))
3451 ;; (nreverse index-alist)))
3454 (defun cperl-find-tags (file xs)
3455 (let (ind (b (get-buffer cperl-tmp-buffer)) lst elt pos ret)
3457 (if b (set-buffer b)
3458 (cperl-setup-tmp-buf))
3460 (setq file (car (insert-file-contents file)))
3461 (message "Scanning file %s..." file)
3463 (setq lst (cperl-xsub-scan))
3464 (setq ind (imenu-example--create-perl-index))
3465 (setq lst (cdr (assoc "+Unsorted List+..." ind))))
3470 (cond ((string-match "^[_a-zA-Z]" (car elt))
3471 (goto-char (cdr elt))
3473 (point) (count-lines 1 (point))
3474 (buffer-substring (progn
3477 (or (eolp) (forward-char 1))
3485 (setq elt (car lst) lst (cdr lst))
3490 (if (string-match "^package " (car elt))
3491 (substring (car elt) 8)
3494 (number-to-string (elt elt 1))
3496 (number-to-string (elt elt 2))
3498 (if (and (string-match "^[_a-zA-Z]+::" (car elt))
3499 (string-match "^sub[ \t]+\\([_a-zA-Z]+\\)[^:_a-zA-Z]"
3501 ;; Need to insert the name without package as well
3502 (setq lst (cons (cons (substring (elt elt 3)
3509 (insert "\f\n" file "," (number-to-string (1- pos)) "\n")
3510 (setq ret (buffer-substring 1 (point-max)))
3512 (message "Scanning file %s finished" file)
3515 (defun cperl-write-tags (&optional file erase recurse dir inbuffer)
3516 ;; If INBUFFER, do not select buffer, and do not save
3517 ;; If ERASE is `ignore', do not erase, and do not try to delete old info.
3520 (setq file (if dir default-directory (buffer-file-name)))
3521 (if (and (not dir) (buffer-modified-p)) (error "Save buffer first!")))
3522 (let ((tags-file-name "TAGS")
3523 (case-fold-search (eq system-type 'emx))
3526 (cond (inbuffer nil) ; Already there
3527 ((file-exists-p tags-file-name)
3528 (visit-tags-table-buffer tags-file-name))
3529 (t (set-buffer (find-file-noselect tags-file-name))))
3532 (cond ((eq erase 'ignore))
3535 (setq erase 'ignore)))
3537 (directory-files file t (if recurse nil "\\.[Pp][Llm]$") t)))
3538 (mapcar (function (lambda (file)
3540 ((string-match "/\\.\\.?$" file) nil)
3541 ((not (file-directory-p file))
3542 (if (string-match "\\.\\([Pp][Llm]\\|xs\\)$" file)
3543 (cperl-write-tags file erase recurse nil t)))
3545 (t (cperl-write-tags file erase recurse t t)))))
3549 (setq xs (string-match "\\.xs$" file))
3550 (cond ((eq erase 'ignore) nil)
3551 (erase (erase-buffer))
3554 (if (search-forward (concat "\f\n" file ",") nil t)
3556 (search-backward "\f\n")
3557 (delete-region (point)
3560 (search-forward "\f\n" nil 'toend)
3563 (insert (cperl-find-tags file xs))))
3564 (if inbuffer nil ; Delegate to the caller
3565 (save-buffer 0) ; No backup
3566 (initialize-new-tags-table)))))
3568 (defvar cperl-tags-hier-regexp-list
3569 "^\\(\\(package\\)\\>\\|sub\\>[^\n]+::\\|[a-zA-Z_][a-zA-Z_0-9:]*(\C-?[^\n]+::\\|[ \t]*BOOT:\C-?[^\n]+::\\)")
3571 (defvar cperl-hierarchy '(() ())
3572 "Global hierarchy of classes")
3574 (defun cperl-tags-hier-fill ()
3575 ;; Suppose we are in a tag table cooked by cperl.
3577 (let (type pack name pos line chunk ord cons1 file str info fileind)
3578 (while (re-search-forward cperl-tags-hier-regexp-list nil t)
3579 (setq pos (match-beginning 0)
3580 pack (match-beginning 2))
3582 (if (looking-at "\\([^\n]+\\)\C-?\\([^\n]+\\)\C-a\\([0-9]+\\),\\([0-9]+\\)")
3584 (setq ;;str (buffer-substring (match-beginning 1) (match-end 1))
3585 name (buffer-substring (match-beginning 2) (match-end 2))
3586 ;;pos (buffer-substring (match-beginning 3) (match-end 3))
3587 line (buffer-substring (match-beginning 4) (match-end 4))
3589 info (etags-snarf-tag) ; Moves to beginning of the next line
3591 fileind (format "%s:%s" file line))
3594 ;; Make new member of hierarchy name ==> file ==> pos if needed
3595 (if (setq cons1 (assoc name (nth ord cperl-hierarchy)))
3597 (setcdr cons1 (cons (cons fileind (vector file info))
3599 ;; First occurence of the name, start alist
3600 (setq cons1 (cons name (list (cons fileind (vector file info)))))
3602 (setcar (cdr cperl-hierarchy)
3603 (cons cons1 (nth 1 cperl-hierarchy)))
3604 (setcar cperl-hierarchy
3605 (cons cons1 (car cperl-hierarchy)))))))
3608 (defun cperl-tags-hier-init (&optional update)
3609 "Show hierarchical menu of classes and methods.
3610 Finds info about classes by a scan of loaded TAGS files.
3611 Supposes that the TAGS files contain fully qualified function names.
3612 One may build such TAGS files from CPerl mode menu."
3616 (if (or update (null (nth 2 cperl-hierarchy)))
3617 (let (pack name cons1 to l1 l2 l3 l4
3618 (remover (function (lambda (elt) ; (name (file1...) (file2..))
3621 (setcdr elt (cdr (nth 1 elt))))))))
3622 ;; (setq cperl-hierarchy '(() () ())) ; Would write into '() later!
3623 (setq cperl-hierarchy (list l1 l2 l3))
3625 (call-interactively 'visit-tags-table))
3626 (message "Updating list of classes...")
3630 (set-buffer (get-file-buffer tagsfile))
3631 (cperl-tags-hier-fill)))
3633 (mapcar remover (car cperl-hierarchy))
3634 (mapcar remover (nth 1 cperl-hierarchy))
3635 (setq to (list nil (cons "Packages: " (nth 1 cperl-hierarchy))
3636 (cons "Methods: " (car cperl-hierarchy))))
3637 (cperl-tags-treeify to 1)
3638 (setcar (nthcdr 2 cperl-hierarchy)
3639 (cperl-menu-to-keymap (cons '("+++UPDATE+++" . -999) (cdr to))))
3640 (message "Updating list of classes: done, requesting display...")
3641 ;;(cperl-imenu-addback (nth 2 cperl-hierarchy))
3643 (or (nth 2 cperl-hierarchy)
3644 (error "No items found"))
3646 ;;; (imenu-choose-buffer-index "Packages: " (nth 2 cperl-hierarchy))
3648 (x-popup-menu t (nth 2 cperl-hierarchy))
3650 (tmm-prompt t (nth 2 cperl-hierarchy))))
3651 (if (and update (listp update))
3652 (progn (while (cdr update) (setq update (cdr update)))
3653 (setq update (car update)))) ; Get the last from the list
3654 (if (vectorp update)
3656 (find-file (elt update 0))
3657 (etags-goto-tag-location (elt update 1))))
3658 (if (eq update -999) (cperl-tags-hier-init t)))
3660 (defun cperl-tags-treeify (to level)
3661 ;; cadr of to is read-write. On start it is a cons
3662 (let* ((regexp (concat "^\\(" (mapconcat
3664 (make-list level "[_a-zA-Z0-9]+")
3667 (packages (cdr (nth 1 to)))
3668 (methods (cdr (nth 2 to)))
3669 l1 head tail cons1 cons2 ord writeto packs recurse
3670 root-packages root-functions ms many_ms same_name ps
3674 (cond ((and (string-match regexp (car elt))
3675 (or (eq ord 1) (match-end 2)))
3676 (setq head (substring (car elt) 0 (match-end 1))
3677 tail (if (match-end 2) (substring (car elt)
3680 (if (setq cons1 (assoc head writeto)) nil
3681 ;; Need to init new head
3682 (setcdr writeto (cons (list head (list "Packages: ")
3685 (setq cons1 (nth 1 writeto)))
3686 (setq cons2 (nth ord cons1)) ; Either packs or meths
3687 (setcdr cons2 (cons elt (cdr cons2))))
3689 (setq root-functions (cons elt root-functions)))
3691 (setq root-packages (cons elt root-packages))))))))
3692 (setcdr to l1) ; Init to dynamic space
3695 (mapcar move-deeper packages)
3697 (mapcar move-deeper methods)
3699 (mapcar (function (lambda (elt)
3700 (cperl-tags-treeify elt (1+ level))))
3702 ;; Now add back functions removed from display
3703 (mapcar (function (lambda (elt)
3704 (setcdr to (cons elt (cdr to)))))
3706 ;; Now add back packages removed from display
3707 (mapcar (function (lambda (elt)
3708 (setcdr to (cons (cons (concat "package " (car elt))
3712 ;;Now clean up leaders with one child only
3713 (mapcar (function (lambda (elt)
3714 (if (not (and (listp (cdr elt))
3715 (eq (length elt) 2))) nil
3716 (setcar elt (car (nth 1 elt)))
3717 (setcdr elt (cdr (nth 1 elt))))))
3722 ;;; '(keymap "Name1"
3726 ;;; ("Tail1" "x") ("Tail2" "y"))))
3728 (defun cperl-list-fold (list name limit)
3729 (let (list1 list2 elt1 (num 0))
3730 (if (<= (length list) limit) list
3731 (setq list1 nil list2 nil)
3736 (if (<= num imenu-max-items)
3737 (setq list2 (cons elt1 list2))
3738 (setq list1 (cons (cons name
3743 (nreverse (cons (cons name
3747 (defun cperl-menu-to-keymap (menu &optional name)
3753 (cond ((listp (cdr elt))
3754 (setq list (cperl-list-fold
3755 (cdr elt) (car elt) imenu-max-items))
3758 (cperl-menu-to-keymap list))))
3760 (list (cdr elt) (car elt))))))
3761 (cperl-list-fold menu "Root" imenu-max-items)))))
3764 (defvar cperl-bad-style-regexp
3765 (mapconcat 'identity
3766 '("[^-\n\t <>=+!.&|(*/'`\"#^][-=+<>!|&^]" ; char sign
3767 "[-<>=+^&|]+[^- \t\n=+<>~]" ; sign+ char
3770 "Finds places such that insertion of a whitespace may help a lot.")
3772 (defvar cperl-not-bad-style-regexp
3773 (mapconcat 'identity
3774 '("[^-\t <>=+]\\(--\\|\\+\\+\\)" ; var-- var++
3775 "[a-zA-Z0-9][|&][a-zA-Z0-9$]" ; abc|def abc&def are often used.
3776 "&[(a-zA-Z0-9$]" ; &subroutine &(var->field)
3777 "<\\$?\\sw+\\(\\.\\sw+\\)?>" ; <IN> <stdin.h>
3778 "-[a-zA-Z][ \t]+[_$\"'`]" ; -f file
3788 "[CBIXSLFZ]<\\(\\sw\\|\\s \\|\\s_\\|[\n]\\)*>" ; C<code like text>
3789 "-[a-zA-Z0-9]+[ \t]*=>" ; -option => value
3790 ;; Unaddressed trouble spots: = -abc, f(56, -abc) --- specialcased below
3794 "If matches at the start of match found by `my-bad-c-style-regexp',
3795 insertion of a whitespace will not help.")
3799 (defun cperl-find-bad-style ()
3800 "Find places in the buffer where insertion of a whitespace may help.
3801 Prompts user for insertion of spaces.
3802 Currently it is tuned to C and Perl syntax."
3804 (let (found-bad (p (point)))
3805 (setq last-nonmenu-event 13) ; To disable popup
3806 (beginning-of-buffer)
3807 (map-y-or-n-p "Insert space here? "
3808 (function (lambda (arg) (insert " ")))
3809 'cperl-next-bad-style
3810 '("location" "locations" "insert a space into")
3811 '((?\C-r (lambda (arg)
3812 (let ((buffer-quit-function
3813 'exit-recursive-edit))
3814 (message "Exit with Esc Esc")
3816 t)) ; Consider acted upon
3817 "edit, exit with Esc Esc")
3819 (let ((buffer-quit-function
3820 'exit-recursive-edit))
3821 (message "Exit with Esc Esc")
3823 t)) ; Consider acted upon
3824 "edit, exit with Esc Esc"))
3826 (if found-bad (goto-char found-bad)
3828 (message "No appropriate place found"))))
3830 (defun cperl-next-bad-style ()
3831 (let (p (not-found t) (point (point)) found)
3832 (while (and not-found
3833 (re-search-forward cperl-bad-style-regexp nil 'to-end))
3835 (goto-char (match-beginning 0))
3837 (looking-at cperl-not-bad-style-regexp)
3838 ;; Check for a < -b and friends
3839 (and (eq (following-char) ?\-)
3841 (skip-chars-backward " \t\n")
3842 (memq (preceding-char) '(?\= ?\> ?\< ?\, ?\(, ?\[, ?\{))))
3843 ;; Now check for syntax type
3845 (setq found (point))
3846 (beginning-of-defun)
3847 (let ((pps (parse-partial-sexp (point) found)))
3848 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))
3849 (goto-char (match-end 0))